Committer: Michael Beasley <mike@snafu.setup>
[mikesnafu-overlay.git] / arch / powerpc / platforms / cell / cbe_thermal.c
blob4852bf312d83c0d60a224eb9ab292dd832c96a64
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, char *buf)
102 u8 value;
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)
114 u64 value;
116 value = in_be64(&pmd_regs->tm_tpr.val);
117 /* access the corresponding byte */
118 value >>= pos;
119 value &= 0x3F;
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)
126 u64 reg_value;
127 int temp;
128 u64 new_value;
129 int ret;
131 ret = sscanf(buf, "%u", &temp);
133 if (ret != 1 || temp < TEMP_MIN || temp > TEMP_MAX)
134 return -EINVAL;
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);
146 return size;
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;
182 u64 value;
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,
252 NULL,
255 static struct attribute_group spu_attribute_group = {
256 .name = "thermal",
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,
280 NULL,
283 static struct attribute_group ppe_attribute_group = {
284 .name = "thermal",
285 .attrs = ppe_attributes,
289 * initialize throttling with default values
291 static int __init init_default_values(void)
293 int cpu;
294 struct cbe_pmd_regs __iomem *pmd_regs;
295 struct sys_device *sysdev;
296 union ppe_spe_reg tpr;
297 union spe_reg str1;
298 u64 str2;
299 union spe_reg cr1;
300 u64 cr2;
302 /* TPR defaults */
303 /* ppe
304 * 1F - no full stop
305 * 08 - dynamic throttling starts if over 80 degrees
306 * 03 - dynamic throttling ceases if below 70 degrees */
307 tpr.ppe = 0x1F0803;
308 /* spe
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
313 tpr.spe = 0x100803;
315 /* STR defaults */
316 /* str1
317 * 10 - stop 16 of 32 cycles
319 str1.val = 0x1010101010101010ull;
320 /* str2
321 * 10 - stop 16 of 32 cycles
323 str2 = 0x10;
325 /* CR defaults */
326 /* cr1
327 * 4 - normal operation
329 cr1.val = 0x0404040404040404ull;
330 /* cr2
331 * 4 - normal operation
333 cr2 = 0x04;
335 for_each_possible_cpu (cpu) {
336 pr_debug("processing cpu %d\n", cpu);
337 sysdev = get_cpu_sysdev(cpu);
339 if (!sysdev) {
340 pr_info("invalid sysdev pointer for cbe_thermal\n");
341 return -EINVAL;
344 pmd_regs = cbe_get_cpu_pmd_regs(sysdev->id);
346 if (!pmd_regs) {
347 pr_info("invalid CBE regs pointer for cbe_thermal\n");
348 return -EINVAL;
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);
358 return 0;
362 static int __init thermal_init(void)
364 int rc = init_default_values();
366 if (rc == 0) {
367 spu_add_sysdev_attr_group(&spu_attribute_group);
368 cpu_add_sysdev_attr_group(&ppe_attribute_group);
371 return rc;
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>");