1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * thermal support for the cell processor
5 * This module adds some sysfs attributes to cpu and spu nodes.
6 * Base for measurements are the digital thermal sensors (DTS)
8 * The accuracy is 2 degrees, starting from 65 up to 125 degrees celsius
9 * The attributes can be found under
10 * /sys/devices/system/cpu/cpuX/thermal
11 * /sys/devices/system/spu/spuX/thermal
13 * The following attributes are added for each node:
15 * contains the current temperature measured by the DTS
17 * throttling begins when temperature is greater or equal to
18 * throttle_begin. Setting this value to 125 prevents throttling.
20 * throttling is being ceased, if the temperature is lower than
21 * throttle_end. Due to a delay between applying throttling and
22 * a reduced temperature this value should be less than throttle_begin.
23 * A value equal to throttle_begin provides only a very little hysteresis.
25 * If the temperatrue is greater or equal to throttle_full_stop,
26 * full throttling is applied to the cpu or spu. This value should be
27 * greater than throttle_begin and throttle_end. Setting this value to
28 * 65 prevents the unit from running code at all.
30 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
32 * Author: Christian Krafft <krafft@de.ibm.com>
35 #include <linux/module.h>
36 #include <linux/device.h>
37 #include <linux/kernel.h>
38 #include <linux/cpu.h>
39 #include <linux/stringify.h>
42 #include <asm/cell-regs.h>
44 #include "spu_priv1_mmio.h"
49 #define DEVICE_PREFIX_ATTR(_prefix,_name,_mode) \
50 struct device_attribute attr_ ## _prefix ## _ ## _name = { \
51 .attr = { .name = __stringify(_name), .mode = _mode }, \
52 .show = _prefix ## _show_ ## _name, \
53 .store = _prefix ## _store_ ## _name, \
56 static inline u8 reg_to_temp(u8 reg_value)
58 return ((reg_value & 0x3f) << 1) + TEMP_MIN;
61 static inline u8 temp_to_reg(u8 temp)
63 return ((temp - TEMP_MIN) >> 1) & 0x3f;
66 static struct cbe_pmd_regs __iomem *get_pmd_regs(struct device *dev)
70 spu = container_of(dev, struct spu, dev);
72 return cbe_get_pmd_regs(spu_devnode(spu));
75 /* returns the value for a given spu in a given register */
76 static u8 spu_read_register_value(struct device *dev, union spe_reg __iomem *reg)
81 spu = container_of(dev, struct spu, dev);
82 value.val = in_be64(®->val);
84 return value.spe[spu->spe_id];
87 static ssize_t spu_show_temp(struct device *dev, struct device_attribute *attr,
91 struct cbe_pmd_regs __iomem *pmd_regs;
93 pmd_regs = get_pmd_regs(dev);
95 value = spu_read_register_value(dev, &pmd_regs->ts_ctsr1);
97 return sprintf(buf, "%d\n", reg_to_temp(value));
100 static ssize_t show_throttle(struct cbe_pmd_regs __iomem *pmd_regs, char *buf, int pos)
104 value = in_be64(&pmd_regs->tm_tpr.val);
105 /* access the corresponding byte */
109 return sprintf(buf, "%d\n", reg_to_temp(value));
112 static ssize_t store_throttle(struct cbe_pmd_regs __iomem *pmd_regs, const char *buf, size_t size, int pos)
119 ret = sscanf(buf, "%u", &temp);
121 if (ret != 1 || temp < TEMP_MIN || temp > TEMP_MAX)
124 new_value = temp_to_reg(temp);
126 reg_value = in_be64(&pmd_regs->tm_tpr.val);
128 /* zero out bits for new value */
129 reg_value &= ~(0xffull << pos);
130 /* set bits to new value */
131 reg_value |= new_value << pos;
133 out_be64(&pmd_regs->tm_tpr.val, reg_value);
137 static ssize_t spu_show_throttle_end(struct device *dev,
138 struct device_attribute *attr, char *buf)
140 return show_throttle(get_pmd_regs(dev), buf, 0);
143 static ssize_t spu_show_throttle_begin(struct device *dev,
144 struct device_attribute *attr, char *buf)
146 return show_throttle(get_pmd_regs(dev), buf, 8);
149 static ssize_t spu_show_throttle_full_stop(struct device *dev,
150 struct device_attribute *attr, char *buf)
152 return show_throttle(get_pmd_regs(dev), buf, 16);
155 static ssize_t spu_store_throttle_end(struct device *dev,
156 struct device_attribute *attr, const char *buf, size_t size)
158 return store_throttle(get_pmd_regs(dev), buf, size, 0);
161 static ssize_t spu_store_throttle_begin(struct device *dev,
162 struct device_attribute *attr, const char *buf, size_t size)
164 return store_throttle(get_pmd_regs(dev), buf, size, 8);
167 static ssize_t spu_store_throttle_full_stop(struct device *dev,
168 struct device_attribute *attr, const char *buf, size_t size)
170 return store_throttle(get_pmd_regs(dev), buf, size, 16);
173 static ssize_t ppe_show_temp(struct device *dev, char *buf, int pos)
175 struct cbe_pmd_regs __iomem *pmd_regs;
178 pmd_regs = cbe_get_cpu_pmd_regs(dev->id);
179 value = in_be64(&pmd_regs->ts_ctsr2);
181 value = (value >> pos) & 0x3f;
183 return sprintf(buf, "%d\n", reg_to_temp(value));
187 /* shows the temperature of the DTS on the PPE,
188 * located near the linear thermal sensor */
189 static ssize_t ppe_show_temp0(struct device *dev,
190 struct device_attribute *attr, char *buf)
192 return ppe_show_temp(dev, buf, 32);
195 /* shows the temperature of the second DTS on the PPE */
196 static ssize_t ppe_show_temp1(struct device *dev,
197 struct device_attribute *attr, char *buf)
199 return ppe_show_temp(dev, buf, 0);
202 static ssize_t ppe_show_throttle_end(struct device *dev,
203 struct device_attribute *attr, char *buf)
205 return show_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, 32);
208 static ssize_t ppe_show_throttle_begin(struct device *dev,
209 struct device_attribute *attr, char *buf)
211 return show_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, 40);
214 static ssize_t ppe_show_throttle_full_stop(struct device *dev,
215 struct device_attribute *attr, char *buf)
217 return show_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, 48);
220 static ssize_t ppe_store_throttle_end(struct device *dev,
221 struct device_attribute *attr, const char *buf, size_t size)
223 return store_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, size, 32);
226 static ssize_t ppe_store_throttle_begin(struct device *dev,
227 struct device_attribute *attr, const char *buf, size_t size)
229 return store_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, size, 40);
232 static ssize_t ppe_store_throttle_full_stop(struct device *dev,
233 struct device_attribute *attr, const char *buf, size_t size)
235 return store_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, size, 48);
239 static struct device_attribute attr_spu_temperature = {
240 .attr = {.name = "temperature", .mode = 0400 },
241 .show = spu_show_temp,
244 static DEVICE_PREFIX_ATTR(spu, throttle_end, 0600);
245 static DEVICE_PREFIX_ATTR(spu, throttle_begin, 0600);
246 static DEVICE_PREFIX_ATTR(spu, throttle_full_stop, 0600);
249 static struct attribute *spu_attributes[] = {
250 &attr_spu_temperature.attr,
251 &attr_spu_throttle_end.attr,
252 &attr_spu_throttle_begin.attr,
253 &attr_spu_throttle_full_stop.attr,
257 static const struct attribute_group spu_attribute_group = {
259 .attrs = spu_attributes,
262 static struct device_attribute attr_ppe_temperature0 = {
263 .attr = {.name = "temperature0", .mode = 0400 },
264 .show = ppe_show_temp0,
267 static struct device_attribute attr_ppe_temperature1 = {
268 .attr = {.name = "temperature1", .mode = 0400 },
269 .show = ppe_show_temp1,
272 static DEVICE_PREFIX_ATTR(ppe, throttle_end, 0600);
273 static DEVICE_PREFIX_ATTR(ppe, throttle_begin, 0600);
274 static DEVICE_PREFIX_ATTR(ppe, throttle_full_stop, 0600);
276 static struct attribute *ppe_attributes[] = {
277 &attr_ppe_temperature0.attr,
278 &attr_ppe_temperature1.attr,
279 &attr_ppe_throttle_end.attr,
280 &attr_ppe_throttle_begin.attr,
281 &attr_ppe_throttle_full_stop.attr,
285 static struct attribute_group ppe_attribute_group = {
287 .attrs = ppe_attributes,
291 * initialize throttling with default values
293 static int __init init_default_values(void)
296 struct cbe_pmd_regs __iomem *pmd_regs;
298 union ppe_spe_reg tpr;
307 * 08 - dynamic throttling starts if over 80 degrees
308 * 03 - dynamic throttling ceases if below 70 degrees */
311 * 10 - full stopped when over 96 degrees
312 * 08 - dynamic throttling starts if over 80 degrees
313 * 03 - dynamic throttling ceases if below 70 degrees
319 * 10 - stop 16 of 32 cycles
321 str1.val = 0x1010101010101010ull;
323 * 10 - stop 16 of 32 cycles
329 * 4 - normal operation
331 cr1.val = 0x0404040404040404ull;
333 * 4 - normal operation
337 for_each_possible_cpu (cpu) {
338 pr_debug("processing cpu %d\n", cpu);
339 dev = get_cpu_device(cpu);
342 pr_info("invalid dev pointer for cbe_thermal\n");
346 pmd_regs = cbe_get_cpu_pmd_regs(dev->id);
349 pr_info("invalid CBE regs pointer for cbe_thermal\n");
353 out_be64(&pmd_regs->tm_str2, str2);
354 out_be64(&pmd_regs->tm_str1.val, str1.val);
355 out_be64(&pmd_regs->tm_tpr.val, tpr.val);
356 out_be64(&pmd_regs->tm_cr1.val, cr1.val);
357 out_be64(&pmd_regs->tm_cr2, cr2);
364 static int __init thermal_init(void)
366 int rc = init_default_values();
369 spu_add_dev_attr_group(&spu_attribute_group);
370 cpu_add_dev_attr_group(&ppe_attribute_group);
375 module_init(thermal_init);
377 static void __exit thermal_exit(void)
379 spu_remove_dev_attr_group(&spu_attribute_group);
380 cpu_remove_dev_attr_group(&ppe_attribute_group);
382 module_exit(thermal_exit);
384 MODULE_LICENSE("GPL");
385 MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");