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