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