1 /* $NetBSD: nouveau_nvkm_subdev_bios_disp.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_disp.c,v 1.3 2021/12/18 23:45:38 riastradh Exp $");
28
29 #include <subdev/bios.h>
30 #include <subdev/bios/bit.h>
31 #include <subdev/bios/disp.h>
32
33 u16
nvbios_disp_table(struct nvkm_bios * bios,u8 * ver,u8 * hdr,u8 * cnt,u8 * len,u8 * sub)34 nvbios_disp_table(struct nvkm_bios *bios,
35 u8 *ver, u8 *hdr, u8 *cnt, u8 *len, u8 *sub)
36 {
37 struct bit_entry U;
38
39 if (!bit_entry(bios, 'U', &U)) {
40 if (U.version == 1) {
41 u16 data = nvbios_rd16(bios, U.offset);
42 if (data) {
43 *ver = nvbios_rd08(bios, data + 0x00);
44 switch (*ver) {
45 case 0x20:
46 case 0x21:
47 case 0x22:
48 *hdr = nvbios_rd08(bios, data + 0x01);
49 *len = nvbios_rd08(bios, data + 0x02);
50 *cnt = nvbios_rd08(bios, data + 0x03);
51 *sub = nvbios_rd08(bios, data + 0x04);
52 return data;
53 default:
54 break;
55 }
56 }
57 }
58 }
59
60 return 0x0000;
61 }
62
63 u16
nvbios_disp_entry(struct nvkm_bios * bios,u8 idx,u8 * ver,u8 * len,u8 * sub)64 nvbios_disp_entry(struct nvkm_bios *bios, u8 idx, u8 *ver, u8 *len, u8 *sub)
65 {
66 u8 hdr, cnt;
67 u16 data = nvbios_disp_table(bios, ver, &hdr, &cnt, len, sub);
68 if (data && idx < cnt)
69 return data + hdr + (idx * *len);
70 *ver = 0x00;
71 return 0x0000;
72 }
73
74 u16
nvbios_disp_parse(struct nvkm_bios * bios,u8 idx,u8 * ver,u8 * len,u8 * sub,struct nvbios_disp * info)75 nvbios_disp_parse(struct nvkm_bios *bios, u8 idx, u8 *ver, u8 *len, u8 *sub,
76 struct nvbios_disp *info)
77 {
78 u16 data = nvbios_disp_entry(bios, idx, ver, len, sub);
79 if (data && *len >= 2) {
80 info->data = nvbios_rd16(bios, data + 0);
81 return data;
82 }
83 return 0x0000;
84 }
85
86 u16
nvbios_outp_entry(struct nvkm_bios * bios,u8 idx,u8 * ver,u8 * hdr,u8 * cnt,u8 * len)87 nvbios_outp_entry(struct nvkm_bios *bios, u8 idx,
88 u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
89 {
90 struct nvbios_disp info;
91 u16 data = nvbios_disp_parse(bios, idx, ver, len, hdr, &info);
92 if (data) {
93 *cnt = nvbios_rd08(bios, info.data + 0x05);
94 *len = 0x06;
95 data = info.data;
96 }
97 return data;
98 }
99
100 u16
nvbios_outp_parse(struct nvkm_bios * bios,u8 idx,u8 * ver,u8 * hdr,u8 * cnt,u8 * len,struct nvbios_outp * info)101 nvbios_outp_parse(struct nvkm_bios *bios, u8 idx,
102 u8 *ver, u8 *hdr, u8 *cnt, u8 *len, struct nvbios_outp *info)
103 {
104 u16 data = nvbios_outp_entry(bios, idx, ver, hdr, cnt, len);
105 if (data && *hdr >= 0x0a) {
106 info->type = nvbios_rd16(bios, data + 0x00);
107 info->mask = nvbios_rd32(bios, data + 0x02);
108 if (*ver <= 0x20) /* match any link */
109 info->mask |= 0x00c0;
110 info->script[0] = nvbios_rd16(bios, data + 0x06);
111 info->script[1] = nvbios_rd16(bios, data + 0x08);
112 info->script[2] = 0x0000;
113 if (*hdr >= 0x0c)
114 info->script[2] = nvbios_rd16(bios, data + 0x0a);
115 return data;
116 }
117 return 0x0000;
118 }
119
120 u16
nvbios_outp_match(struct nvkm_bios * bios,u16 type,u16 mask,u8 * ver,u8 * hdr,u8 * cnt,u8 * len,struct nvbios_outp * info)121 nvbios_outp_match(struct nvkm_bios *bios, u16 type, u16 mask,
122 u8 *ver, u8 *hdr, u8 *cnt, u8 *len, struct nvbios_outp *info)
123 {
124 u16 data, idx = 0;
125 while ((data = nvbios_outp_parse(bios, idx++, ver, hdr, cnt, len, info)) || *ver) {
126 if (data && info->type == type) {
127 if ((info->mask & mask) == mask)
128 break;
129 }
130 }
131 return data;
132 }
133
134 u16
nvbios_ocfg_entry(struct nvkm_bios * bios,u16 outp,u8 idx,u8 * ver,u8 * hdr,u8 * cnt,u8 * len)135 nvbios_ocfg_entry(struct nvkm_bios *bios, u16 outp, u8 idx,
136 u8 *ver, u8 *hdr, u8 *cnt, u8 *len)
137 {
138 if (idx < *cnt)
139 return outp + *hdr + (idx * *len);
140 return 0x0000;
141 }
142
143 u16
nvbios_ocfg_parse(struct nvkm_bios * bios,u16 outp,u8 idx,u8 * ver,u8 * hdr,u8 * cnt,u8 * len,struct nvbios_ocfg * info)144 nvbios_ocfg_parse(struct nvkm_bios *bios, u16 outp, u8 idx,
145 u8 *ver, u8 *hdr, u8 *cnt, u8 *len, struct nvbios_ocfg *info)
146 {
147 u16 data = nvbios_ocfg_entry(bios, outp, idx, ver, hdr, cnt, len);
148 if (data) {
149 info->proto = nvbios_rd08(bios, data + 0x00);
150 info->flags = nvbios_rd16(bios, data + 0x01);
151 info->clkcmp[0] = nvbios_rd16(bios, data + 0x02);
152 info->clkcmp[1] = nvbios_rd16(bios, data + 0x04);
153 }
154 return data;
155 }
156
157 u16
nvbios_ocfg_match(struct nvkm_bios * bios,u16 outp,u8 proto,u8 flags,u8 * ver,u8 * hdr,u8 * cnt,u8 * len,struct nvbios_ocfg * info)158 nvbios_ocfg_match(struct nvkm_bios *bios, u16 outp, u8 proto, u8 flags,
159 u8 *ver, u8 *hdr, u8 *cnt, u8 *len, struct nvbios_ocfg *info)
160 {
161 u16 data, idx = 0;
162 while ((data = nvbios_ocfg_parse(bios, outp, idx++, ver, hdr, cnt, len, info))) {
163 if ((info->proto == proto || info->proto == 0xff) &&
164 (info->flags == flags))
165 break;
166 }
167 return data;
168 }
169
170 u16
nvbios_oclk_match(struct nvkm_bios * bios,u16 cmp,u32 khz)171 nvbios_oclk_match(struct nvkm_bios *bios, u16 cmp, u32 khz)
172 {
173 while (cmp) {
174 if (khz / 10 >= nvbios_rd16(bios, cmp + 0x00))
175 return nvbios_rd16(bios, cmp + 0x02);
176 cmp += 0x04;
177 }
178 return 0x0000;
179 }
180