2 * thermal support for the cell processor
4 * This module adds some sysfs attributes to cpu and spu nodes.
5 * Base for measurements are the digital thermal sensors (DTS)
7 * The accuracy is 2 degrees, starting from 65 up to 125 degrees celsius
8 * The attributes can be found under
9 * /sys/devices/system/cpu/cpuX/thermal
10 * /sys/devices/system/spu/spuX/thermal
12 * The following attributes are added for each node:
14 * contains the current temperature measured by the DTS
16 * throttling begins when temperature is greater or equal to
17 * throttle_begin. Setting this value to 125 prevents throttling.
19 * throttling is being ceased, if the temperature is lower than
20 * throttle_end. Due to a delay between applying throttling and
21 * a reduced temperature this value should be less than throttle_begin.
22 * A value equal to throttle_begin provides only a very little hysteresis.
24 * If the temperatrue is greater or equal to throttle_full_stop,
25 * full throttling is applied to the cpu or spu. This value should be
26 * greater than throttle_begin and throttle_end. Setting this value to
27 * 65 prevents the unit from running code at all.
29 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
31 * Author: Christian Krafft <krafft@de.ibm.com>
33 * This program is free software; you can redistribute it and/or modify
34 * it under the terms of the GNU General Public License as published by
35 * the Free Software Foundation; either version 2, or (at your option)
38 * This program is distributed in the hope that it will be useful,
39 * but WITHOUT ANY WARRANTY; without even the implied warranty of
40 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
41 * GNU General Public License for more details.
43 * You should have received a copy of the GNU General Public License
44 * along with this program; if not, write to the Free Software
45 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
48 #include <linux/module.h>
49 #include <linux/sysdev.h>
50 #include <linux/kernel.h>
51 #include <linux/cpu.h>
55 #include <asm/cell-regs.h>
57 #include "spu_priv1_mmio.h"
62 #define SYSDEV_PREFIX_ATTR(_prefix,_name,_mode) \
63 struct sysdev_attribute attr_ ## _prefix ## _ ## _name = { \
64 .attr = { .name = __stringify(_name), .mode = _mode }, \
65 .show = _prefix ## _show_ ## _name, \
66 .store = _prefix ## _store_ ## _name, \
69 static inline u8
reg_to_temp(u8 reg_value
)
71 return ((reg_value
& 0x3f) << 1) + TEMP_MIN
;
74 static inline u8
temp_to_reg(u8 temp
)
76 return ((temp
- TEMP_MIN
) >> 1) & 0x3f;
79 static struct cbe_pmd_regs __iomem
*get_pmd_regs(struct sys_device
*sysdev
)
83 spu
= container_of(sysdev
, struct spu
, sysdev
);
85 return cbe_get_pmd_regs(spu_devnode(spu
));
88 /* returns the value for a given spu in a given register */
89 static u8
spu_read_register_value(struct sys_device
*sysdev
, union spe_reg __iomem
*reg
)
94 spu
= container_of(sysdev
, struct spu
, sysdev
);
95 value
.val
= in_be64(®
->val
);
97 return value
.spe
[spu
->spe_id
];
100 static ssize_t
spu_show_temp(struct sys_device
*sysdev
, char *buf
)
103 struct cbe_pmd_regs __iomem
*pmd_regs
;
105 pmd_regs
= get_pmd_regs(sysdev
);
107 value
= spu_read_register_value(sysdev
, &pmd_regs
->ts_ctsr1
);
109 return sprintf(buf
, "%d\n", reg_to_temp(value
));
112 static ssize_t
show_throttle(struct cbe_pmd_regs __iomem
*pmd_regs
, char *buf
, int pos
)
116 value
= in_be64(&pmd_regs
->tm_tpr
.val
);
117 /* access the corresponding byte */
121 return sprintf(buf
, "%d\n", reg_to_temp(value
));
124 static ssize_t
store_throttle(struct cbe_pmd_regs __iomem
*pmd_regs
, const char *buf
, size_t size
, int pos
)
131 ret
= sscanf(buf
, "%u", &temp
);
133 if (ret
!= 1 || temp
< TEMP_MIN
|| temp
> TEMP_MAX
)
136 new_value
= temp_to_reg(temp
);
138 reg_value
= in_be64(&pmd_regs
->tm_tpr
.val
);
140 /* zero out bits for new value */
141 reg_value
&= ~(0xffull
<< pos
);
142 /* set bits to new value */
143 reg_value
|= new_value
<< pos
;
145 out_be64(&pmd_regs
->tm_tpr
.val
, reg_value
);
149 static ssize_t
spu_show_throttle_end(struct sys_device
*sysdev
, char *buf
)
151 return show_throttle(get_pmd_regs(sysdev
), buf
, 0);
154 static ssize_t
spu_show_throttle_begin(struct sys_device
*sysdev
, char *buf
)
156 return show_throttle(get_pmd_regs(sysdev
), buf
, 8);
159 static ssize_t
spu_show_throttle_full_stop(struct sys_device
*sysdev
, char *buf
)
161 return show_throttle(get_pmd_regs(sysdev
), buf
, 16);
164 static ssize_t
spu_store_throttle_end(struct sys_device
*sysdev
, const char *buf
, size_t size
)
166 return store_throttle(get_pmd_regs(sysdev
), buf
, size
, 0);
169 static ssize_t
spu_store_throttle_begin(struct sys_device
*sysdev
, const char *buf
, size_t size
)
171 return store_throttle(get_pmd_regs(sysdev
), buf
, size
, 8);
174 static ssize_t
spu_store_throttle_full_stop(struct sys_device
*sysdev
, const char *buf
, size_t size
)
176 return store_throttle(get_pmd_regs(sysdev
), buf
, size
, 16);
179 static ssize_t
ppe_show_temp(struct sys_device
*sysdev
, char *buf
, int pos
)
181 struct cbe_pmd_regs __iomem
*pmd_regs
;
184 pmd_regs
= cbe_get_cpu_pmd_regs(sysdev
->id
);
185 value
= in_be64(&pmd_regs
->ts_ctsr2
);
187 value
= (value
>> pos
) & 0x3f;
189 return sprintf(buf
, "%d\n", reg_to_temp(value
));
193 /* shows the temperature of the DTS on the PPE,
194 * located near the linear thermal sensor */
195 static ssize_t
ppe_show_temp0(struct sys_device
*sysdev
, char *buf
)
197 return ppe_show_temp(sysdev
, buf
, 32);
200 /* shows the temperature of the second DTS on the PPE */
201 static ssize_t
ppe_show_temp1(struct sys_device
*sysdev
, char *buf
)
203 return ppe_show_temp(sysdev
, buf
, 0);
206 static ssize_t
ppe_show_throttle_end(struct sys_device
*sysdev
, char *buf
)
208 return show_throttle(cbe_get_cpu_pmd_regs(sysdev
->id
), buf
, 32);
211 static ssize_t
ppe_show_throttle_begin(struct sys_device
*sysdev
, char *buf
)
213 return show_throttle(cbe_get_cpu_pmd_regs(sysdev
->id
), buf
, 40);
216 static ssize_t
ppe_show_throttle_full_stop(struct sys_device
*sysdev
, char *buf
)
218 return show_throttle(cbe_get_cpu_pmd_regs(sysdev
->id
), buf
, 48);
221 static ssize_t
ppe_store_throttle_end(struct sys_device
*sysdev
, const char *buf
, size_t size
)
223 return store_throttle(cbe_get_cpu_pmd_regs(sysdev
->id
), buf
, size
, 32);
226 static ssize_t
ppe_store_throttle_begin(struct sys_device
*sysdev
, const char *buf
, size_t size
)
228 return store_throttle(cbe_get_cpu_pmd_regs(sysdev
->id
), buf
, size
, 40);
231 static ssize_t
ppe_store_throttle_full_stop(struct sys_device
*sysdev
, const char *buf
, size_t size
)
233 return store_throttle(cbe_get_cpu_pmd_regs(sysdev
->id
), buf
, size
, 48);
237 static struct sysdev_attribute attr_spu_temperature
= {
238 .attr
= {.name
= "temperature", .mode
= 0400 },
239 .show
= spu_show_temp
,
242 static SYSDEV_PREFIX_ATTR(spu
, throttle_end
, 0600);
243 static SYSDEV_PREFIX_ATTR(spu
, throttle_begin
, 0600);
244 static SYSDEV_PREFIX_ATTR(spu
, throttle_full_stop
, 0600);
247 static struct attribute
*spu_attributes
[] = {
248 &attr_spu_temperature
.attr
,
249 &attr_spu_throttle_end
.attr
,
250 &attr_spu_throttle_begin
.attr
,
251 &attr_spu_throttle_full_stop
.attr
,
255 static struct attribute_group spu_attribute_group
= {
257 .attrs
= spu_attributes
,
260 static struct sysdev_attribute attr_ppe_temperature0
= {
261 .attr
= {.name
= "temperature0", .mode
= 0400 },
262 .show
= ppe_show_temp0
,
265 static struct sysdev_attribute attr_ppe_temperature1
= {
266 .attr
= {.name
= "temperature1", .mode
= 0400 },
267 .show
= ppe_show_temp1
,
270 static SYSDEV_PREFIX_ATTR(ppe
, throttle_end
, 0600);
271 static SYSDEV_PREFIX_ATTR(ppe
, throttle_begin
, 0600);
272 static SYSDEV_PREFIX_ATTR(ppe
, throttle_full_stop
, 0600);
274 static struct attribute
*ppe_attributes
[] = {
275 &attr_ppe_temperature0
.attr
,
276 &attr_ppe_temperature1
.attr
,
277 &attr_ppe_throttle_end
.attr
,
278 &attr_ppe_throttle_begin
.attr
,
279 &attr_ppe_throttle_full_stop
.attr
,
283 static struct attribute_group ppe_attribute_group
= {
285 .attrs
= ppe_attributes
,
289 * initialize throttling with default values
291 static int __init
init_default_values(void)
294 struct cbe_pmd_regs __iomem
*pmd_regs
;
295 struct sys_device
*sysdev
;
296 union ppe_spe_reg tpr
;
305 * 08 - dynamic throttling starts if over 80 degrees
306 * 03 - dynamic throttling ceases if below 70 degrees */
309 * 10 - full stopped when over 96 degrees
310 * 08 - dynamic throttling starts if over 80 degrees
311 * 03 - dynamic throttling ceases if below 70 degrees
317 * 10 - stop 16 of 32 cycles
319 str1
.val
= 0x1010101010101010ull
;
321 * 10 - stop 16 of 32 cycles
327 * 4 - normal operation
329 cr1
.val
= 0x0404040404040404ull
;
331 * 4 - normal operation
335 for_each_possible_cpu (cpu
) {
336 pr_debug("processing cpu %d\n", cpu
);
337 sysdev
= get_cpu_sysdev(cpu
);
340 pr_info("invalid sysdev pointer for cbe_thermal\n");
344 pmd_regs
= cbe_get_cpu_pmd_regs(sysdev
->id
);
347 pr_info("invalid CBE regs pointer for cbe_thermal\n");
351 out_be64(&pmd_regs
->tm_str2
, str2
);
352 out_be64(&pmd_regs
->tm_str1
.val
, str1
.val
);
353 out_be64(&pmd_regs
->tm_tpr
.val
, tpr
.val
);
354 out_be64(&pmd_regs
->tm_cr1
.val
, cr1
.val
);
355 out_be64(&pmd_regs
->tm_cr2
, cr2
);
362 static int __init
thermal_init(void)
364 int rc
= init_default_values();
367 spu_add_sysdev_attr_group(&spu_attribute_group
);
368 cpu_add_sysdev_attr_group(&ppe_attribute_group
);
373 module_init(thermal_init
);
375 static void __exit
thermal_exit(void)
377 spu_remove_sysdev_attr_group(&spu_attribute_group
);
378 cpu_remove_sysdev_attr_group(&ppe_attribute_group
);
380 module_exit(thermal_exit
);
382 MODULE_LICENSE("GPL");
383 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");