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 #include <subdev/bios.h>
25 #include <subdev/bios/dcb.h>
26 #include <subdev/bios/extdev.h>
27 
28 static u16
29 extdev_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *len, u8 *cnt)
30 {
31 	u8  dcb_ver, dcb_hdr, dcb_cnt, dcb_len;
32 	u16 dcb, extdev = 0;
33 
34 	dcb = dcb_table(bios, &dcb_ver, &dcb_hdr, &dcb_cnt, &dcb_len);
35 	if (!dcb || (dcb_ver != 0x30 && dcb_ver != 0x40 && dcb_ver != 0x41))
36 		return 0x0000;
37 
38 	extdev = nvbios_rd16(bios, dcb + 18);
39 	if (!extdev)
40 		return 0x0000;
41 
42 	*ver = nvbios_rd08(bios, extdev + 0);
43 	*hdr = nvbios_rd08(bios, extdev + 1);
44 	*cnt = nvbios_rd08(bios, extdev + 2);
45 	*len = nvbios_rd08(bios, extdev + 3);
46 	return extdev + *hdr;
47 }
48 
49 bool
50 nvbios_extdev_skip_probe(struct nvkm_bios *bios)
51 {
52 	u8  ver, hdr, len, cnt;
53 	u16 data = extdev_table(bios, &ver, &hdr, &len, &cnt);
54 	if (data && ver == 0x40 && hdr >= 5) {
55 		u8 flags = nvbios_rd08(bios, data - hdr + 4);
56 		if (flags & 1)
57 			return true;
58 	}
59 	return false;
60 }
61 
62 static u16
63 nvbios_extdev_entry(struct nvkm_bios *bios, int idx, u8 *ver, u8 *len)
64 {
65 	u8 hdr, cnt;
66 	u16 extdev = extdev_table(bios, ver, &hdr, len, &cnt);
67 	if (extdev && idx < cnt)
68 		return extdev + idx * *len;
69 	return 0x0000;
70 }
71 
72 static void
73 extdev_parse_entry(struct nvkm_bios *bios, u16 offset,
74 		   struct nvbios_extdev_func *entry)
75 {
76 	entry->type = nvbios_rd08(bios, offset + 0);
77 	entry->addr = nvbios_rd08(bios, offset + 1);
78 	entry->bus = (nvbios_rd08(bios, offset + 2) >> 4) & 1;
79 }
80 
81 int
82 nvbios_extdev_parse(struct nvkm_bios *bios, int idx,
83 		    struct nvbios_extdev_func *func)
84 {
85 	u8 ver, len;
86 	u16 entry;
87 
88 	if (!(entry = nvbios_extdev_entry(bios, idx, &ver, &len)))
89 		return -EINVAL;
90 
91 	extdev_parse_entry(bios, entry, func);
92 	return 0;
93 }
94 
95 int
96 nvbios_extdev_find(struct nvkm_bios *bios, enum nvbios_extdev_type type,
97 		   struct nvbios_extdev_func *func)
98 {
99 	u8 ver, len, i;
100 	u16 entry;
101 
102 	i = 0;
103 	while ((entry = nvbios_extdev_entry(bios, i++, &ver, &len))) {
104 		extdev_parse_entry(bios, entry, func);
105 		if (func->type == type)
106 			return 0;
107 	}
108 
109 	return -EINVAL;
110 }
111