xref: /linux/drivers/gpu/drm/nouveau/nvkm/subdev/bios/power_budget.c (revision be239684b18e1cdcafcf8c7face4a2f562c745ad)
1 /*
2  * Copyright 2016 Karol Herbst
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * Authors: Karol Herbst
23  */
24 #include <subdev/bios.h>
25 #include <subdev/bios/bit.h>
26 #include <subdev/bios/power_budget.h>
27 
28 static u32
29 nvbios_power_budget_table(struct nvkm_bios *bios, u8 *ver, u8 *hdr, u8 *cnt,
30 			  u8 *len)
31 {
32 	struct bit_entry bit_P;
33 	u32 power_budget;
34 
35 	if (bit_entry(bios, 'P', &bit_P) || bit_P.version != 2 ||
36 	    bit_P.length < 0x30)
37 		return 0;
38 
39 	power_budget = nvbios_rd32(bios, bit_P.offset + 0x2c);
40 	if (!power_budget)
41 		return 0;
42 
43 	*ver = nvbios_rd08(bios, power_budget);
44 	switch (*ver) {
45 	case 0x20:
46 	case 0x30:
47 		*hdr = nvbios_rd08(bios, power_budget + 0x1);
48 		*len = nvbios_rd08(bios, power_budget + 0x2);
49 		*cnt = nvbios_rd08(bios, power_budget + 0x3);
50 		return power_budget;
51 	default:
52 		break;
53 	}
54 
55 	return 0;
56 }
57 
58 int
59 nvbios_power_budget_header(struct nvkm_bios *bios,
60                            struct nvbios_power_budget *budget)
61 {
62 	u8 ver, hdr, cnt, len, cap_entry;
63 	u32 header;
64 
65 	if (!bios || !budget)
66 		return -EINVAL;
67 
68 	header = nvbios_power_budget_table(bios, &ver, &hdr, &cnt, &len);
69 	if (!header || !cnt)
70 		return -ENODEV;
71 
72 	switch (ver) {
73 	case 0x20:
74 		cap_entry = nvbios_rd08(bios, header + 0x9);
75 		break;
76 	case 0x30:
77 		cap_entry = nvbios_rd08(bios, header + 0xa);
78 		break;
79 	default:
80 		cap_entry = 0xff;
81 	}
82 
83 	if (cap_entry >= cnt && cap_entry != 0xff) {
84 		nvkm_warn(&bios->subdev,
85 		          "invalid cap_entry in power budget table found\n");
86 		budget->cap_entry = 0xff;
87 		return -EINVAL;
88 	}
89 
90 	budget->offset = header;
91 	budget->ver = ver;
92 	budget->hlen = hdr;
93 	budget->elen = len;
94 	budget->ecount = cnt;
95 
96 	budget->cap_entry = cap_entry;
97 
98 	return 0;
99 }
100 
101 int
102 nvbios_power_budget_entry(struct nvkm_bios *bios,
103                           struct nvbios_power_budget *budget,
104                           u8 idx, struct nvbios_power_budget_entry *entry)
105 {
106 	u32 entry_offset;
107 
108 	if (!bios || !budget || !budget->offset || idx >= budget->ecount
109 		|| !entry)
110 		return -EINVAL;
111 
112 	entry_offset = budget->offset + budget->hlen + idx * budget->elen;
113 
114 	if (budget->ver >= 0x20) {
115 		entry->min_w = nvbios_rd32(bios, entry_offset + 0x2);
116 		entry->avg_w = nvbios_rd32(bios, entry_offset + 0x6);
117 		entry->max_w = nvbios_rd32(bios, entry_offset + 0xa);
118 	} else {
119 		entry->min_w = 0;
120 		entry->max_w = nvbios_rd32(bios, entry_offset + 0x2);
121 		entry->avg_w = entry->max_w;
122 	}
123 
124 	return 0;
125 }
126