1 /*
2 * Copyright 2012 Nouveau Community
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 *
22 * Authors: Martin Peres
23 */
24
25 #include <subdev/bios.h>
26 #include <subdev/bios/dcb.h>
27 #include <subdev/bios/extdev.h>
28
29 static u16
extdev_table(struct nouveau_bios * bios,u8 * ver,u8 * hdr,u8 * len,u8 * cnt)30 extdev_table(struct nouveau_bios *bios, u8 *ver, u8 *hdr, u8 *len, u8 *cnt)
31 {
32 u8 dcb_ver, dcb_hdr, dcb_cnt, dcb_len;
33 u16 dcb, extdev = 0;
34
35 dcb = dcb_table(bios, &dcb_ver, &dcb_hdr, &dcb_cnt, &dcb_len);
36 if (!dcb || (dcb_ver != 0x30 && dcb_ver != 0x40))
37 return 0x0000;
38
39 extdev = nv_ro16(bios, dcb + 18);
40 if (!extdev)
41 return 0x0000;
42
43 *ver = nv_ro08(bios, extdev + 0);
44 *hdr = nv_ro08(bios, extdev + 1);
45 *cnt = nv_ro08(bios, extdev + 2);
46 *len = nv_ro08(bios, extdev + 3);
47
48 return extdev + *hdr;
49 }
50
51 static u16
nvbios_extdev_entry(struct nouveau_bios * bios,int idx,u8 * ver,u8 * len)52 nvbios_extdev_entry(struct nouveau_bios *bios, int idx, u8 *ver, u8 *len)
53 {
54 u8 hdr, cnt;
55 u16 extdev = extdev_table(bios, ver, &hdr, len, &cnt);
56 if (extdev && idx < cnt)
57 return extdev + idx * *len;
58 return 0x0000;
59 }
60
61 static void
extdev_parse_entry(struct nouveau_bios * bios,u16 offset,struct nvbios_extdev_func * entry)62 extdev_parse_entry(struct nouveau_bios *bios, u16 offset,
63 struct nvbios_extdev_func *entry)
64 {
65 entry->type = nv_ro08(bios, offset + 0);
66 entry->addr = nv_ro08(bios, offset + 1);
67 entry->bus = (nv_ro08(bios, offset + 2) >> 4) & 1;
68 }
69
70 int
nvbios_extdev_parse(struct nouveau_bios * bios,int idx,struct nvbios_extdev_func * func)71 nvbios_extdev_parse(struct nouveau_bios *bios, int idx,
72 struct nvbios_extdev_func *func)
73 {
74 u8 ver, len;
75 u16 entry;
76
77 if (!(entry = nvbios_extdev_entry(bios, idx, &ver, &len)))
78 return -EINVAL;
79
80 extdev_parse_entry(bios, entry, func);
81
82 return 0;
83 }
84
85 int
nvbios_extdev_find(struct nouveau_bios * bios,enum nvbios_extdev_type type,struct nvbios_extdev_func * func)86 nvbios_extdev_find(struct nouveau_bios *bios, enum nvbios_extdev_type type,
87 struct nvbios_extdev_func *func)
88 {
89 u8 ver, len, i;
90 u16 entry;
91
92 i = 0;
93 while (!(entry = nvbios_extdev_entry(bios, i++, &ver, &len))) {
94 extdev_parse_entry(bios, entry, func);
95 if (func->type == type)
96 return 0;
97 }
98
99 return -EINVAL;
100 }
101