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