1 /*
2  * Copyright 2012 Red Hat Inc.
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: Ben Skeggs
23  */
24 #include <subdev/bios.h>
25 #include <subdev/bios/dcb.h>
26 
27 u16
28 dcb_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
29 {
30 	struct nvkm_device *device = nv_device(bios);
31 	u16 dcb = 0x0000;
32 
33 	if (device->card_type > NV_04)
34 		dcb = nv_ro16(bios, 0x36);
35 	if (!dcb) {
36 		nv_warn(bios, "DCB table not found\n");
37 		return dcb;
38 	}
39 
40 	*ver = nv_ro08(bios, dcb);
41 
42 	if (*ver >= 0x42) {
43 		nv_warn(bios, "DCB version 0x%02x unknown\n", *ver);
44 		return 0x0000;
45 	} else
46 	if (*ver >= 0x30) {
47 		if (nv_ro32(bios, dcb + 6) == 0x4edcbdcb) {
48 			*hdr = nv_ro08(bios, dcb + 1);
49 			*cnt = nv_ro08(bios, dcb + 2);
50 			*len = nv_ro08(bios, dcb + 3);
51 			return dcb;
52 		}
53 	} else
54 	if (*ver >= 0x20) {
55 		if (nv_ro32(bios, dcb + 4) == 0x4edcbdcb) {
56 			u16 i2c = nv_ro16(bios, dcb + 2);
57 			*hdr = 8;
58 			*cnt = (i2c - dcb) / 8;
59 			*len = 8;
60 			return dcb;
61 		}
62 	} else
63 	if (*ver >= 0x15) {
64 		if (!nv_memcmp(bios, dcb - 7, "DEV_REC", 7)) {
65 			u16 i2c = nv_ro16(bios, dcb + 2);
66 			*hdr = 4;
67 			*cnt = (i2c - dcb) / 10;
68 			*len = 10;
69 			return dcb;
70 		}
71 	} else {
72 		/*
73 		 * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but
74 		 * always has the same single (crt) entry, even when tv-out
75 		 * present, so the conclusion is this version cannot really
76 		 * be used.
77 		 *
78 		 * v1.2 tables (some NV6/10, and NV15+) normally have the
79 		 * same 5 entries, which are not specific to the card and so
80 		 * no use.
81 		 *
82 		 * v1.2 does have an I2C table that read_dcb_i2c_table can
83 		 * handle, but cards exist (nv11 in #14821) with a bad i2c
84 		 * table pointer, so use the indices parsed in
85 		 * parse_bmp_structure.
86 		 *
87 		 * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
88 		 */
89 		nv_warn(bios, "DCB contains no useful data\n");
90 		return 0x0000;
91 	}
92 
93 	nv_warn(bios, "DCB header validation failed\n");
94 	return 0x0000;
95 }
96 
97 u16
98 dcb_outp(struct nvkm_bios *bios, u8 idx, u8 *ver, u8 *len)
99 {
100 	u8  hdr, cnt;
101 	u16 dcb = dcb_table(bios, ver, &hdr, &cnt, len);
102 	if (dcb && idx < cnt)
103 		return dcb + hdr + (idx * *len);
104 	return 0x0000;
105 }
106 
107 static inline u16
108 dcb_outp_hasht(struct dcb_output *outp)
109 {
110 	return (outp->extdev << 8) | (outp->location << 4) | outp->type;
111 }
112 
113 static inline u16
114 dcb_outp_hashm(struct dcb_output *outp)
115 {
116 	return (outp->heads << 8) | (outp->link << 6) | outp->or;
117 }
118 
119 u16
120 dcb_outp_parse(struct nvkm_bios *bios, u8 idx, u8 *ver, u8 *len,
121 	       struct dcb_output *outp)
122 {
123 	u16 dcb = dcb_outp(bios, idx, ver, len);
124 	memset(outp, 0x00, sizeof(*outp));
125 	if (dcb) {
126 		if (*ver >= 0x20) {
127 			u32 conn = nv_ro32(bios, dcb + 0x00);
128 			outp->or        = (conn & 0x0f000000) >> 24;
129 			outp->location  = (conn & 0x00300000) >> 20;
130 			outp->bus       = (conn & 0x000f0000) >> 16;
131 			outp->connector = (conn & 0x0000f000) >> 12;
132 			outp->heads     = (conn & 0x00000f00) >> 8;
133 			outp->i2c_index = (conn & 0x000000f0) >> 4;
134 			outp->type      = (conn & 0x0000000f);
135 			outp->link      = 0;
136 		} else {
137 			dcb = 0x0000;
138 		}
139 
140 		if (*ver >= 0x40) {
141 			u32 conf = nv_ro32(bios, dcb + 0x04);
142 			switch (outp->type) {
143 			case DCB_OUTPUT_DP:
144 				switch (conf & 0x00e00000) {
145 				case 0x00000000:
146 					outp->dpconf.link_bw = 0x06;
147 					break;
148 				case 0x00200000:
149 					outp->dpconf.link_bw = 0x0a;
150 					break;
151 				case 0x00400000:
152 				default:
153 					outp->dpconf.link_bw = 0x14;
154 					break;
155 				}
156 
157 				switch ((conf & 0x0f000000) >> 24) {
158 				case 0xf:
159 				case 0x4:
160 					outp->dpconf.link_nr = 4;
161 					break;
162 				case 0x3:
163 				case 0x2:
164 					outp->dpconf.link_nr = 2;
165 					break;
166 				case 0x1:
167 				default:
168 					outp->dpconf.link_nr = 1;
169 					break;
170 				}
171 
172 				/* fall-through... */
173 			case DCB_OUTPUT_TMDS:
174 			case DCB_OUTPUT_LVDS:
175 				outp->link = (conf & 0x00000030) >> 4;
176 				outp->sorconf.link = outp->link; /*XXX*/
177 				outp->extdev = 0x00;
178 				if (outp->location != 0)
179 					outp->extdev = (conf & 0x0000ff00) >> 8;
180 				break;
181 			default:
182 				break;
183 			}
184 		}
185 
186 		outp->hasht = dcb_outp_hasht(outp);
187 		outp->hashm = dcb_outp_hashm(outp);
188 	}
189 	return dcb;
190 }
191 
192 u16
193 dcb_outp_match(struct nvkm_bios *bios, u16 type, u16 mask,
194 	       u8 *ver, u8 *len, struct dcb_output *outp)
195 {
196 	u16 dcb, idx = 0;
197 	while ((dcb = dcb_outp_parse(bios, idx++, ver, len, outp))) {
198 		if ((dcb_outp_hasht(outp) & 0x00ff) == (type & 0x00ff)) {
199 			if ((dcb_outp_hashm(outp) & mask) == mask)
200 				break;
201 		}
202 	}
203 	return dcb;
204 }
205 
206 int
207 dcb_outp_foreach(struct nvkm_bios *bios, void *data,
208 		 int (*exec)(struct nvkm_bios *, void *, int, u16))
209 {
210 	int ret, idx = -1;
211 	u8  ver, len;
212 	u16 outp;
213 
214 	while ((outp = dcb_outp(bios, ++idx, &ver, &len))) {
215 		if (nv_ro32(bios, outp) == 0x00000000)
216 			break; /* seen on an NV11 with DCB v1.5 */
217 		if (nv_ro32(bios, outp) == 0xffffffff)
218 			break; /* seen on an NV17 with DCB v2.0 */
219 
220 		if (nv_ro08(bios, outp) == DCB_OUTPUT_UNUSED)
221 			continue;
222 		if (nv_ro08(bios, outp) == DCB_OUTPUT_EOL)
223 			break;
224 
225 		ret = exec(bios, data, idx, outp);
226 		if (ret)
227 			return ret;
228 	}
229 
230 	return 0;
231 }
232