Merge branch 'kvm-updates/2.6.36' of git://git.kernel.org/pub/scm/virt/kvm/kvm
[linux-2.6/next.git] / arch / powerpc / platforms / cell / cbe_thermal.c
blob4d4c8c169124eb8899315bf97c895c4e22a8919f
1 /*
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)
6 * located on the chip.
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:
13 * temperature:
14 * contains the current temperature measured by the DTS
15 * throttle_begin:
16 * throttling begins when temperature is greater or equal to
17 * throttle_begin. Setting this value to 125 prevents throttling.
18 * throttle_end:
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.
23 * throttle_full_stop:
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)
36 * any later version.
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>
52 #include <asm/spu.h>
53 #include <asm/io.h>
54 #include <asm/prom.h>
55 #include <asm/cell-regs.h>
57 #include "spu_priv1_mmio.h"
59 #define TEMP_MIN 65
60 #define TEMP_MAX 125
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)
81 struct spu *spu;
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)
91 union spe_reg value;
92 struct spu *spu;
94 spu = container_of(sysdev, struct spu, sysdev);
95 value.val = in_be64(&reg->val);
97 return value.spe[spu->spe_id];
100 static ssize_t spu_show_temp(struct sys_device *sysdev, struct sysdev_attribute *attr,
101 char *buf)
103 u8 value;
104 struct cbe_pmd_regs __iomem *pmd_regs;
106 pmd_regs = get_pmd_regs(sysdev);
108 value = spu_read_register_value(sysdev, &pmd_regs->ts_ctsr1);
110 return sprintf(buf, "%d\n", reg_to_temp(value));
113 static ssize_t show_throttle(struct cbe_pmd_regs __iomem *pmd_regs, char *buf, int pos)
115 u64 value;
117 value = in_be64(&pmd_regs->tm_tpr.val);
118 /* access the corresponding byte */
119 value >>= pos;
120 value &= 0x3F;
122 return sprintf(buf, "%d\n", reg_to_temp(value));
125 static ssize_t store_throttle(struct cbe_pmd_regs __iomem *pmd_regs, const char *buf, size_t size, int pos)
127 u64 reg_value;
128 int temp;
129 u64 new_value;
130 int ret;
132 ret = sscanf(buf, "%u", &temp);
134 if (ret != 1 || temp < TEMP_MIN || temp > TEMP_MAX)
135 return -EINVAL;
137 new_value = temp_to_reg(temp);
139 reg_value = in_be64(&pmd_regs->tm_tpr.val);
141 /* zero out bits for new value */
142 reg_value &= ~(0xffull << pos);
143 /* set bits to new value */
144 reg_value |= new_value << pos;
146 out_be64(&pmd_regs->tm_tpr.val, reg_value);
147 return size;
150 static ssize_t spu_show_throttle_end(struct sys_device *sysdev,
151 struct sysdev_attribute *attr, char *buf)
153 return show_throttle(get_pmd_regs(sysdev), buf, 0);
156 static ssize_t spu_show_throttle_begin(struct sys_device *sysdev,
157 struct sysdev_attribute *attr, char *buf)
159 return show_throttle(get_pmd_regs(sysdev), buf, 8);
162 static ssize_t spu_show_throttle_full_stop(struct sys_device *sysdev,
163 struct sysdev_attribute *attr, char *buf)
165 return show_throttle(get_pmd_regs(sysdev), buf, 16);
168 static ssize_t spu_store_throttle_end(struct sys_device *sysdev,
169 struct sysdev_attribute *attr, const char *buf, size_t size)
171 return store_throttle(get_pmd_regs(sysdev), buf, size, 0);
174 static ssize_t spu_store_throttle_begin(struct sys_device *sysdev,
175 struct sysdev_attribute *attr, const char *buf, size_t size)
177 return store_throttle(get_pmd_regs(sysdev), buf, size, 8);
180 static ssize_t spu_store_throttle_full_stop(struct sys_device *sysdev,
181 struct sysdev_attribute *attr, const char *buf, size_t size)
183 return store_throttle(get_pmd_regs(sysdev), buf, size, 16);
186 static ssize_t ppe_show_temp(struct sys_device *sysdev, char *buf, int pos)
188 struct cbe_pmd_regs __iomem *pmd_regs;
189 u64 value;
191 pmd_regs = cbe_get_cpu_pmd_regs(sysdev->id);
192 value = in_be64(&pmd_regs->ts_ctsr2);
194 value = (value >> pos) & 0x3f;
196 return sprintf(buf, "%d\n", reg_to_temp(value));
200 /* shows the temperature of the DTS on the PPE,
201 * located near the linear thermal sensor */
202 static ssize_t ppe_show_temp0(struct sys_device *sysdev,
203 struct sysdev_attribute *attr, char *buf)
205 return ppe_show_temp(sysdev, buf, 32);
208 /* shows the temperature of the second DTS on the PPE */
209 static ssize_t ppe_show_temp1(struct sys_device *sysdev,
210 struct sysdev_attribute *attr, char *buf)
212 return ppe_show_temp(sysdev, buf, 0);
215 static ssize_t ppe_show_throttle_end(struct sys_device *sysdev,
216 struct sysdev_attribute *attr, char *buf)
218 return show_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, 32);
221 static ssize_t ppe_show_throttle_begin(struct sys_device *sysdev,
222 struct sysdev_attribute *attr, char *buf)
224 return show_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, 40);
227 static ssize_t ppe_show_throttle_full_stop(struct sys_device *sysdev,
228 struct sysdev_attribute *attr, char *buf)
230 return show_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, 48);
233 static ssize_t ppe_store_throttle_end(struct sys_device *sysdev,
234 struct sysdev_attribute *attr, const char *buf, size_t size)
236 return store_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, size, 32);
239 static ssize_t ppe_store_throttle_begin(struct sys_device *sysdev,
240 struct sysdev_attribute *attr, const char *buf, size_t size)
242 return store_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, size, 40);
245 static ssize_t ppe_store_throttle_full_stop(struct sys_device *sysdev,
246 struct sysdev_attribute *attr, const char *buf, size_t size)
248 return store_throttle(cbe_get_cpu_pmd_regs(sysdev->id), buf, size, 48);
252 static struct sysdev_attribute attr_spu_temperature = {
253 .attr = {.name = "temperature", .mode = 0400 },
254 .show = spu_show_temp,
257 static SYSDEV_PREFIX_ATTR(spu, throttle_end, 0600);
258 static SYSDEV_PREFIX_ATTR(spu, throttle_begin, 0600);
259 static SYSDEV_PREFIX_ATTR(spu, throttle_full_stop, 0600);
262 static struct attribute *spu_attributes[] = {
263 &attr_spu_temperature.attr,
264 &attr_spu_throttle_end.attr,
265 &attr_spu_throttle_begin.attr,
266 &attr_spu_throttle_full_stop.attr,
267 NULL,
270 static struct attribute_group spu_attribute_group = {
271 .name = "thermal",
272 .attrs = spu_attributes,
275 static struct sysdev_attribute attr_ppe_temperature0 = {
276 .attr = {.name = "temperature0", .mode = 0400 },
277 .show = ppe_show_temp0,
280 static struct sysdev_attribute attr_ppe_temperature1 = {
281 .attr = {.name = "temperature1", .mode = 0400 },
282 .show = ppe_show_temp1,
285 static SYSDEV_PREFIX_ATTR(ppe, throttle_end, 0600);
286 static SYSDEV_PREFIX_ATTR(ppe, throttle_begin, 0600);
287 static SYSDEV_PREFIX_ATTR(ppe, throttle_full_stop, 0600);
289 static struct attribute *ppe_attributes[] = {
290 &attr_ppe_temperature0.attr,
291 &attr_ppe_temperature1.attr,
292 &attr_ppe_throttle_end.attr,
293 &attr_ppe_throttle_begin.attr,
294 &attr_ppe_throttle_full_stop.attr,
295 NULL,
298 static struct attribute_group ppe_attribute_group = {
299 .name = "thermal",
300 .attrs = ppe_attributes,
304 * initialize throttling with default values
306 static int __init init_default_values(void)
308 int cpu;
309 struct cbe_pmd_regs __iomem *pmd_regs;
310 struct sys_device *sysdev;
311 union ppe_spe_reg tpr;
312 union spe_reg str1;
313 u64 str2;
314 union spe_reg cr1;
315 u64 cr2;
317 /* TPR defaults */
318 /* ppe
319 * 1F - no full stop
320 * 08 - dynamic throttling starts if over 80 degrees
321 * 03 - dynamic throttling ceases if below 70 degrees */
322 tpr.ppe = 0x1F0803;
323 /* spe
324 * 10 - full stopped when over 96 degrees
325 * 08 - dynamic throttling starts if over 80 degrees
326 * 03 - dynamic throttling ceases if below 70 degrees
328 tpr.spe = 0x100803;
330 /* STR defaults */
331 /* str1
332 * 10 - stop 16 of 32 cycles
334 str1.val = 0x1010101010101010ull;
335 /* str2
336 * 10 - stop 16 of 32 cycles
338 str2 = 0x10;
340 /* CR defaults */
341 /* cr1
342 * 4 - normal operation
344 cr1.val = 0x0404040404040404ull;
345 /* cr2
346 * 4 - normal operation
348 cr2 = 0x04;
350 for_each_possible_cpu (cpu) {
351 pr_debug("processing cpu %d\n", cpu);
352 sysdev = get_cpu_sysdev(cpu);
354 if (!sysdev) {
355 pr_info("invalid sysdev pointer for cbe_thermal\n");
356 return -EINVAL;
359 pmd_regs = cbe_get_cpu_pmd_regs(sysdev->id);
361 if (!pmd_regs) {
362 pr_info("invalid CBE regs pointer for cbe_thermal\n");
363 return -EINVAL;
366 out_be64(&pmd_regs->tm_str2, str2);
367 out_be64(&pmd_regs->tm_str1.val, str1.val);
368 out_be64(&pmd_regs->tm_tpr.val, tpr.val);
369 out_be64(&pmd_regs->tm_cr1.val, cr1.val);
370 out_be64(&pmd_regs->tm_cr2, cr2);
373 return 0;
377 static int __init thermal_init(void)
379 int rc = init_default_values();
381 if (rc == 0) {
382 spu_add_sysdev_attr_group(&spu_attribute_group);
383 cpu_add_sysdev_attr_group(&ppe_attribute_group);
386 return rc;
388 module_init(thermal_init);
390 static void __exit thermal_exit(void)
392 spu_remove_sysdev_attr_group(&spu_attribute_group);
393 cpu_remove_sysdev_attr_group(&ppe_attribute_group);
395 module_exit(thermal_exit);
397 MODULE_LICENSE("GPL");
398 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");