xref: /netbsd-src/sys/external/bsd/drm2/dist/drm/nouveau/nvkm/subdev/bios/nouveau_nvkm_subdev_bios_power_budget.c (revision 41ec02673d281bbb3d38e6c78504ce6e30c228c1)
1 /*	$NetBSD: nouveau_nvkm_subdev_bios_power_budget.c,v 1.2 2021/12/18 23:45:38 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2016 Karol Herbst
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: Karol Herbst
25  */
26 #include <sys/cdefs.h>
27 __KERNEL_RCSID(0, "$NetBSD: nouveau_nvkm_subdev_bios_power_budget.c,v 1.2 2021/12/18 23:45:38 riastradh Exp $");
28 
29 #include <subdev/bios.h>
30 #include <subdev/bios/bit.h>
31 #include <subdev/bios/power_budget.h>
32 
33 static u32
nvbios_power_budget_table(struct nvkm_bios * bios,u8 * ver,u8 * hdr,u8 * cnt,u8 * len)34 nvbios_power_budget_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt,
35 			  u8 *len)
36 {
37 	struct bit_entry bit_P;
38 	u32 power_budget;
39 
40 	if (bit_entry(bios, 'P', &bit_P) || bit_P.version != 2 ||
41 	    bit_P.length < 0x30)
42 		return 0;
43 
44 	power_budget = nvbios_rd32(bios, bit_P.offset + 0x2c);
45 	if (!power_budget)
46 		return 0;
47 
48 	*ver = nvbios_rd08(bios, power_budget);
49 	switch (*ver) {
50 	case 0x20:
51 	case 0x30:
52 		*hdr = nvbios_rd08(bios, power_budget + 0x1);
53 		*len = nvbios_rd08(bios, power_budget + 0x2);
54 		*cnt = nvbios_rd08(bios, power_budget + 0x3);
55 		return power_budget;
56 	default:
57 		break;
58 	}
59 
60 	return 0;
61 }
62 
63 int
nvbios_power_budget_header(struct nvkm_bios * bios,struct nvbios_power_budget * budget)64 nvbios_power_budget_header(struct nvkm_bios *bios,
65                            struct nvbios_power_budget *budget)
66 {
67 	struct nvkm_subdev *subdev = &bios->subdev;
68 	u8 ver, hdr, cnt, len, cap_entry;
69 	u32 header;
70 
71 	if (!bios || !budget)
72 		return -EINVAL;
73 
74 	header = nvbios_power_budget_table(bios, &ver, &hdr, &cnt, &len);
75 	if (!header || !cnt)
76 		return -ENODEV;
77 
78 	switch (ver) {
79 	case 0x20:
80 		cap_entry = nvbios_rd08(bios, header + 0x9);
81 		break;
82 	case 0x30:
83 		cap_entry = nvbios_rd08(bios, header + 0xa);
84 		break;
85 	default:
86 		cap_entry = 0xff;
87 	}
88 
89 	if (cap_entry >= cnt && cap_entry != 0xff) {
90 		nvkm_warn(subdev,
91 		          "invalid cap_entry in power budget table found\n");
92 		budget->cap_entry = 0xff;
93 		return -EINVAL;
94 	}
95 
96 	budget->offset = header;
97 	budget->ver = ver;
98 	budget->hlen = hdr;
99 	budget->elen = len;
100 	budget->ecount = cnt;
101 
102 	budget->cap_entry = cap_entry;
103 
104 	return 0;
105 }
106 
107 int
nvbios_power_budget_entry(struct nvkm_bios * bios,struct nvbios_power_budget * budget,u8 idx,struct nvbios_power_budget_entry * entry)108 nvbios_power_budget_entry(struct nvkm_bios *bios,
109                           struct nvbios_power_budget *budget,
110                           u8 idx, struct nvbios_power_budget_entry *entry)
111 {
112 	u32 entry_offset;
113 
114 	if (!bios || !budget || !budget->offset || idx >= budget->ecount
115 		|| !entry)
116 		return -EINVAL;
117 
118 	entry_offset = budget->offset + budget->hlen + idx * budget->elen;
119 
120 	if (budget->ver >= 0x20) {
121 		entry->min_w = nvbios_rd32(bios, entry_offset + 0x2);
122 		entry->avg_w = nvbios_rd32(bios, entry_offset + 0x6);
123 		entry->max_w = nvbios_rd32(bios, entry_offset + 0xa);
124 	} else {
125 		entry->min_w = 0;
126 		entry->max_w = nvbios_rd32(bios, entry_offset + 0x2);
127 		entry->avg_w = entry->max_w;
128 	}
129 
130 	return 0;
131 }
132