1 /* $NetBSD: nouveau_nvkm_subdev_bios_cstep.c,v 1.3 2021/12/18 23:45:38 riastradh Exp $ */
2
3 /*
4 * Copyright 2013 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_cstep.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/cstep.h>
32
33 u32
nvbios_cstepTe(struct nvkm_bios * bios,u8 * ver,u8 * hdr,u8 * cnt,u8 * len,u8 * xnr,u8 * xsz)34 nvbios_cstepTe(struct nvkm_bios *bios,
35 u8 *ver, u8 *hdr, u8 *cnt, u8 *len, u8 *xnr, u8 *xsz)
36 {
37 struct bit_entry bit_P;
38 u32 cstep = 0;
39
40 if (!bit_entry(bios, 'P', &bit_P)) {
41 if (bit_P.version == 2 && bit_P.length >= 0x38)
42 cstep = nvbios_rd32(bios, bit_P.offset + 0x34);
43
44 if (cstep) {
45 *ver = nvbios_rd08(bios, cstep + 0);
46 switch (*ver) {
47 case 0x10:
48 *hdr = nvbios_rd08(bios, cstep + 1);
49 *cnt = nvbios_rd08(bios, cstep + 3);
50 *len = nvbios_rd08(bios, cstep + 2);
51 *xnr = nvbios_rd08(bios, cstep + 5);
52 *xsz = nvbios_rd08(bios, cstep + 4);
53 return cstep;
54 default:
55 break;
56 }
57 }
58 }
59
60 return 0;
61 }
62
63 u32
nvbios_cstepEe(struct nvkm_bios * bios,int idx,u8 * ver,u8 * hdr)64 nvbios_cstepEe(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr)
65 {
66 u8 cnt, len, xnr, xsz;
67 u32 data = nvbios_cstepTe(bios, ver, hdr, &cnt, &len, &xnr, &xsz);
68 if (data && idx < cnt) {
69 data = data + *hdr + (idx * len);
70 *hdr = len;
71 return data;
72 }
73 return 0;
74 }
75
76 u32
nvbios_cstepEp(struct nvkm_bios * bios,int idx,u8 * ver,u8 * hdr,struct nvbios_cstepE * info)77 nvbios_cstepEp(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr,
78 struct nvbios_cstepE *info)
79 {
80 u32 data = nvbios_cstepEe(bios, idx, ver, hdr);
81 memset(info, 0x00, sizeof(*info));
82 if (data) {
83 info->pstate = (nvbios_rd16(bios, data + 0x00) & 0x01e0) >> 5;
84 info->index = nvbios_rd08(bios, data + 0x03);
85 }
86 return data;
87 }
88
89 u32
nvbios_cstepEm(struct nvkm_bios * bios,u8 pstate,u8 * ver,u8 * hdr,struct nvbios_cstepE * info)90 nvbios_cstepEm(struct nvkm_bios *bios, u8 pstate, u8 *ver, u8 *hdr,
91 struct nvbios_cstepE *info)
92 {
93 u32 data, idx = 0;
94 while ((data = nvbios_cstepEp(bios, idx++, ver, hdr, info))) {
95 if (info->pstate == pstate)
96 break;
97 }
98 return data;
99 }
100
101 u32
nvbios_cstepXe(struct nvkm_bios * bios,int idx,u8 * ver,u8 * hdr)102 nvbios_cstepXe(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr)
103 {
104 u8 cnt, len, xnr, xsz;
105 u32 data = nvbios_cstepTe(bios, ver, hdr, &cnt, &len, &xnr, &xsz);
106 if (data && idx < xnr) {
107 data = data + *hdr + (cnt * len) + (idx * xsz);
108 *hdr = xsz;
109 return data;
110 }
111 return 0;
112 }
113
114 u32
nvbios_cstepXp(struct nvkm_bios * bios,int idx,u8 * ver,u8 * hdr,struct nvbios_cstepX * info)115 nvbios_cstepXp(struct nvkm_bios *bios, int idx, u8 *ver, u8 *hdr,
116 struct nvbios_cstepX *info)
117 {
118 u32 data = nvbios_cstepXe(bios, idx, ver, hdr);
119 memset(info, 0x00, sizeof(*info));
120 if (data) {
121 info->freq = nvbios_rd16(bios, data + 0x00) * 1000;
122 info->unkn[0] = nvbios_rd08(bios, data + 0x02);
123 info->unkn[1] = nvbios_rd08(bios, data + 0x03);
124 info->voltage = nvbios_rd08(bios, data + 0x04);
125 }
126 return data;
127 }
128