xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/bios/nouveau_nvkm_subdev_bios_dcb.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_nvkm_subdev_bios_dcb.c,v 1.3 2021/12/18 23:45:38 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2012 Red Hat Inc.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  *
24  * Authors: Ben Skeggs
25  */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_bios_dcb.c,v 1.3 2021/12/18 23:45:38 riastradh Exp $");
28 
29 #include <subdev/bios.h>
30 #include <subdev/bios/dcb.h>
31 
32 u16
dcb_table(struct nvkm_bios * bios,u8 * ver,u8 * hdr,u8 * cnt,u8 * len)33 dcb_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
34 {
35 	struct nvkm_subdev *subdev = &bios->subdev;
36 	struct nvkm_device *device = subdev->device;
37 	u16 dcb = 0x0000;
38 
39 	if (device->card_type > NV_04)
40 		dcb = nvbios_rd16(bios, 0x36);
41 	if (!dcb) {
42 		nvkm_warn(subdev, "DCB table not found\n");
43 		return dcb;
44 	}
45 
46 	*ver = nvbios_rd08(bios, dcb);
47 
48 	if (*ver >= 0x42) {
49 		nvkm_warn(subdev, "DCB version 0x%02x unknown\n", *ver);
50 		return 0x0000;
51 	} else
52 	if (*ver >= 0x30) {
53 		if (nvbios_rd32(bios, dcb + 6) == 0x4edcbdcb) {
54 			*hdr = nvbios_rd08(bios, dcb + 1);
55 			*cnt = nvbios_rd08(bios, dcb + 2);
56 			*len = nvbios_rd08(bios, dcb + 3);
57 			return dcb;
58 		}
59 	} else
60 	if (*ver >= 0x20) {
61 		if (nvbios_rd32(bios, dcb + 4) == 0x4edcbdcb) {
62 			u16 i2c = nvbios_rd16(bios, dcb + 2);
63 			*hdr = 8;
64 			*cnt = (i2c - dcb) / 8;
65 			*len = 8;
66 			return dcb;
67 		}
68 	} else
69 	if (*ver >= 0x15) {
70 		if (!nvbios_memcmp(bios, dcb - 7, "DEV_REC", 7)) {
71 			u16 i2c = nvbios_rd16(bios, dcb + 2);
72 			*hdr = 4;
73 			*cnt = (i2c - dcb) / 10;
74 			*len = 10;
75 			return dcb;
76 		}
77 	} else {
78 		/*
79 		 * v1.4 (some NV15/16, NV11+) seems the same as v1.5, but
80 		 * always has the same single (crt) entry, even when tv-out
81 		 * present, so the conclusion is this version cannot really
82 		 * be used.
83 		 *
84 		 * v1.2 tables (some NV6/10, and NV15+) normally have the
85 		 * same 5 entries, which are not specific to the card and so
86 		 * no use.
87 		 *
88 		 * v1.2 does have an I2C table that read_dcb_i2c_table can
89 		 * handle, but cards exist (nv11 in #14821) with a bad i2c
90 		 * table pointer, so use the indices parsed in
91 		 * parse_bmp_structure.
92 		 *
93 		 * v1.1 (NV5+, maybe some NV4) is entirely unhelpful
94 		 */
95 		nvkm_debug(subdev, "DCB contains no useful data\n");
96 		return 0x0000;
97 	}
98 
99 	nvkm_warn(subdev, "DCB header validation failed\n");
100 	return 0x0000;
101 }
102 
103 u16
dcb_outp(struct nvkm_bios * bios,u8 idx,u8 * ver,u8 * len)104 dcb_outp(struct nvkm_bios *bios, u8 idx, u8 *ver, u8 *len)
105 {
106 	u8  hdr, cnt;
107 	u16 dcb = dcb_table(bios, ver, &hdr, &cnt, len);
108 	if (dcb && idx < cnt)
109 		return dcb + hdr + (idx * *len);
110 	return 0x0000;
111 }
112 
113 static inline u16
dcb_outp_hasht(struct dcb_output * outp)114 dcb_outp_hasht(struct dcb_output *outp)
115 {
116 	return (outp->extdev << 8) | (outp->location << 4) | outp->type;
117 }
118 
119 static inline u16
dcb_outp_hashm(struct dcb_output * outp)120 dcb_outp_hashm(struct dcb_output *outp)
121 {
122 	return (outp->heads << 8) | (outp->link << 6) | outp->or;
123 }
124 
125 u16
dcb_outp_parse(struct nvkm_bios * bios,u8 idx,u8 * ver,u8 * len,struct dcb_output * outp)126 dcb_outp_parse(struct nvkm_bios *bios, u8 idx, u8 *ver, u8 *len,
127 	       struct dcb_output *outp)
128 {
129 	u16 dcb = dcb_outp(bios, idx, ver, len);
130 	memset(outp, 0x00, sizeof(*outp));
131 	if (dcb) {
132 		if (*ver >= 0x20) {
133 			u32 conn = nvbios_rd32(bios, dcb + 0x00);
134 			outp->or        = (conn & 0x0f000000) >> 24;
135 			outp->location  = (conn & 0x00300000) >> 20;
136 			outp->bus       = (conn & 0x000f0000) >> 16;
137 			outp->connector = (conn & 0x0000f000) >> 12;
138 			outp->heads     = (conn & 0x00000f00) >> 8;
139 			outp->i2c_index = (conn & 0x000000f0) >> 4;
140 			outp->type      = (conn & 0x0000000f);
141 			outp->link      = 0;
142 		} else {
143 			dcb = 0x0000;
144 		}
145 
146 		if (*ver >= 0x40) {
147 			u32 conf = nvbios_rd32(bios, dcb + 0x04);
148 			switch (outp->type) {
149 			case DCB_OUTPUT_DP:
150 				switch (conf & 0x00e00000) {
151 				case 0x00000000: /* 1.62 */
152 					outp->dpconf.link_bw = 0x06;
153 					break;
154 				case 0x00200000: /* 2.7 */
155 					outp->dpconf.link_bw = 0x0a;
156 					break;
157 				case 0x00400000: /* 5.4 */
158 					outp->dpconf.link_bw = 0x14;
159 					break;
160 				case 0x00600000: /* 8.1 */
161 				default:
162 					outp->dpconf.link_bw = 0x1e;
163 					break;
164 				}
165 
166 				switch ((conf & 0x0f000000) >> 24) {
167 				case 0xf:
168 				case 0x4:
169 					outp->dpconf.link_nr = 4;
170 					break;
171 				case 0x3:
172 				case 0x2:
173 					outp->dpconf.link_nr = 2;
174 					break;
175 				case 0x1:
176 				default:
177 					outp->dpconf.link_nr = 1;
178 					break;
179 				}
180 
181 				/* fall-through... */
182 			case DCB_OUTPUT_TMDS:
183 			case DCB_OUTPUT_LVDS:
184 				outp->link = (conf & 0x00000030) >> 4;
185 				outp->sorconf.link = outp->link; /*XXX*/
186 				outp->extdev = 0x00;
187 				if (outp->location != 0)
188 					outp->extdev = (conf & 0x0000ff00) >> 8;
189 				break;
190 			default:
191 				break;
192 			}
193 		}
194 
195 		outp->hasht = dcb_outp_hasht(outp);
196 		outp->hashm = dcb_outp_hashm(outp);
197 	}
198 	return dcb;
199 }
200 
201 u16
dcb_outp_match(struct nvkm_bios * bios,u16 type,u16 mask,u8 * ver,u8 * len,struct dcb_output * outp)202 dcb_outp_match(struct nvkm_bios *bios, u16 type, u16 mask,
203 	       u8 *ver, u8 *len, struct dcb_output *outp)
204 {
205 	u16 dcb, idx = 0;
206 	while ((dcb = dcb_outp_parse(bios, idx++, ver, len, outp))) {
207 		if ((dcb_outp_hasht(outp) & 0x00ff) == (type & 0x00ff)) {
208 			if ((dcb_outp_hashm(outp) & mask) == mask)
209 				break;
210 		}
211 	}
212 	return dcb;
213 }
214 
215 int
dcb_outp_foreach(struct nvkm_bios * bios,void * data,int (* exec)(struct nvkm_bios *,void *,int,u16))216 dcb_outp_foreach(struct nvkm_bios *bios, void *data,
217 		 int (*exec)(struct nvkm_bios *, void *, int, u16))
218 {
219 	int ret, idx = -1;
220 	u8  ver, len;
221 	u16 outp;
222 
223 	while ((outp = dcb_outp(bios, ++idx, &ver, &len))) {
224 		if (nvbios_rd32(bios, outp) == 0x00000000)
225 			break; /* seen on an NV11 with DCB v1.5 */
226 		if (nvbios_rd32(bios, outp) == 0xffffffff)
227 			break; /* seen on an NV17 with DCB v2.0 */
228 
229 		if (nvbios_rd08(bios, outp) == DCB_OUTPUT_UNUSED)
230 			continue;
231 		if (nvbios_rd08(bios, outp) == DCB_OUTPUT_EOL)
232 			break;
233 
234 		ret = exec(bios, data, idx, outp);
235 		if (ret)
236 			return ret;
237 	}
238 
239 	return 0;
240 }
241