1 /*	$NetBSD: nouveau_subdev_bios_therm.c,v 1.1.1.1 2014/08/06 12:36:29 riastradh Exp $	*/
2 
3 /*
4  * Copyright 2012 Nouveau Community
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: Martin Peres
25  */
26 
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: nouveau_subdev_bios_therm.c,v 1.1.1.1 2014/08/06 12:36:29 riastradh Exp $");
29 
30 #include <subdev/bios.h>
31 #include <subdev/bios/bit.h>
32 #include <subdev/bios/therm.h>
33 
34 static u16
therm_table(struct nouveau_bios * bios,u8 * ver,u8 * hdr,u8 * len,u8 * cnt)35 therm_table(struct nouveau_bios *bios, u8 *ver, u8 *hdr, u8 *len, u8 *cnt)
36 {
37 	struct bit_entry bit_P;
38 	u16 therm = 0;
39 
40 	if (!bit_entry(bios, 'P', &bit_P)) {
41 		if (bit_P.version == 1)
42 			therm = nv_ro16(bios, bit_P.offset + 12);
43 		else if (bit_P.version == 2)
44 			therm = nv_ro16(bios, bit_P.offset + 16);
45 		else
46 			nv_error(bios,
47 				"unknown offset for thermal in BIT P %d\n",
48 				bit_P.version);
49 	}
50 
51 	/* exit now if we haven't found the thermal table */
52 	if (!therm)
53 		return 0x0000;
54 
55 	*ver = nv_ro08(bios, therm + 0);
56 	*hdr = nv_ro08(bios, therm + 1);
57 	*len = nv_ro08(bios, therm + 2);
58 	*cnt = nv_ro08(bios, therm + 3);
59 
60 	return therm + nv_ro08(bios, therm + 1);
61 }
62 
63 static u16
nvbios_therm_entry(struct nouveau_bios * bios,int idx,u8 * ver,u8 * len)64 nvbios_therm_entry(struct nouveau_bios *bios, int idx, u8 *ver, u8 *len)
65 {
66 	u8 hdr, cnt;
67 	u16 therm = therm_table(bios, ver, &hdr, len, &cnt);
68 	if (therm && idx < cnt)
69 		return therm + idx * *len;
70 	return 0x0000;
71 }
72 
73 int
nvbios_therm_sensor_parse(struct nouveau_bios * bios,enum nvbios_therm_domain domain,struct nvbios_therm_sensor * sensor)74 nvbios_therm_sensor_parse(struct nouveau_bios *bios,
75 			  enum nvbios_therm_domain domain,
76 			  struct nvbios_therm_sensor *sensor)
77 {
78 	s8 thrs_section, sensor_section, offset;
79 	u8 ver, len, i;
80 	u16 entry;
81 
82 	/* we only support the core domain for now */
83 	if (domain != NVBIOS_THERM_DOMAIN_CORE)
84 		return -EINVAL;
85 
86 	/* Read the entries from the table */
87 	thrs_section = 0;
88 	sensor_section = -1;
89 	i = 0;
90 	while ((entry = nvbios_therm_entry(bios, i++, &ver, &len))) {
91 		s16 value = nv_ro16(bios, entry + 1);
92 
93 		switch (nv_ro08(bios, entry + 0)) {
94 		case 0x0:
95 			thrs_section = value;
96 			if (value > 0)
97 				return 0; /* we do not try to support ambient */
98 			break;
99 		case 0x01:
100 			sensor_section++;
101 			if (sensor_section == 0) {
102 				offset = ((s8) nv_ro08(bios, entry + 2)) / 2;
103 				sensor->offset_constant = offset;
104 			}
105 			break;
106 
107 		case 0x04:
108 			if (thrs_section == 0) {
109 				sensor->thrs_critical.temp = (value & 0xff0) >> 4;
110 				sensor->thrs_critical.hysteresis = value & 0xf;
111 			}
112 			break;
113 
114 		case 0x07:
115 			if (thrs_section == 0) {
116 				sensor->thrs_down_clock.temp = (value & 0xff0) >> 4;
117 				sensor->thrs_down_clock.hysteresis = value & 0xf;
118 			}
119 			break;
120 
121 		case 0x08:
122 			if (thrs_section == 0) {
123 				sensor->thrs_fan_boost.temp = (value & 0xff0) >> 4;
124 				sensor->thrs_fan_boost.hysteresis = value & 0xf;
125 			}
126 			break;
127 
128 		case 0x10:
129 			if (sensor_section == 0)
130 				sensor->offset_num = value;
131 			break;
132 
133 		case 0x11:
134 			if (sensor_section == 0)
135 				sensor->offset_den = value;
136 			break;
137 
138 		case 0x12:
139 			if (sensor_section == 0)
140 				sensor->slope_mult = value;
141 			break;
142 
143 		case 0x13:
144 			if (sensor_section == 0)
145 				sensor->slope_div = value;
146 			break;
147 		case 0x32:
148 			if (thrs_section == 0) {
149 				sensor->thrs_shutdown.temp = (value & 0xff0) >> 4;
150 				sensor->thrs_shutdown.hysteresis = value & 0xf;
151 			}
152 			break;
153 		}
154 	}
155 
156 	return 0;
157 }
158 
159 int
nvbios_therm_fan_parse(struct nouveau_bios * bios,struct nvbios_therm_fan * fan)160 nvbios_therm_fan_parse(struct nouveau_bios *bios,
161 			  struct nvbios_therm_fan *fan)
162 {
163 	struct nouveau_therm_trip_point *cur_trip = NULL;
164 	u8 ver, len, i;
165 	u16 entry;
166 
167 	uint8_t duty_lut[] = { 0, 0, 25, 0, 40, 0, 50, 0,
168 				75, 0, 85, 0, 100, 0, 100, 0 };
169 
170 	i = 0;
171 	fan->nr_fan_trip = 0;
172 	fan->fan_mode = NVBIOS_THERM_FAN_OTHER;
173 	while ((entry = nvbios_therm_entry(bios, i++, &ver, &len))) {
174 		s16 value = nv_ro16(bios, entry + 1);
175 
176 		switch (nv_ro08(bios, entry + 0)) {
177 		case 0x22:
178 			fan->min_duty = value & 0xff;
179 			fan->max_duty = (value & 0xff00) >> 8;
180 			break;
181 		case 0x24:
182 			fan->nr_fan_trip++;
183 			if (fan->fan_mode > NVBIOS_THERM_FAN_TRIP)
184 				fan->fan_mode = NVBIOS_THERM_FAN_TRIP;
185 			cur_trip = &fan->trip[fan->nr_fan_trip - 1];
186 			cur_trip->hysteresis = value & 0xf;
187 			cur_trip->temp = (value & 0xff0) >> 4;
188 			cur_trip->fan_duty = duty_lut[(value & 0xf000) >> 12];
189 			break;
190 		case 0x25:
191 			cur_trip = &fan->trip[fan->nr_fan_trip - 1];
192 			cur_trip->fan_duty = value;
193 			break;
194 		case 0x26:
195 			if (!fan->pwm_freq)
196 				fan->pwm_freq = value;
197 			break;
198 		case 0x3b:
199 			fan->bump_period = value;
200 			break;
201 		case 0x3c:
202 			fan->slow_down_period = value;
203 			break;
204 		case 0x46:
205 			if (fan->fan_mode > NVBIOS_THERM_FAN_LINEAR)
206 				fan->fan_mode = NVBIOS_THERM_FAN_LINEAR;
207 			fan->linear_min_temp = nv_ro08(bios, entry + 1);
208 			fan->linear_max_temp = nv_ro08(bios, entry + 2);
209 			break;
210 		}
211 	}
212 
213 	/* starting from fermi, fan management is always linear */
214 	if (nv_device(bios)->card_type >= NV_C0 &&
215 		fan->fan_mode == NVBIOS_THERM_FAN_OTHER) {
216 		fan->fan_mode = NVBIOS_THERM_FAN_LINEAR;
217 	}
218 
219 	return 0;
220 }
221