blob: 2ece77f49bc3d718ff5f4d2db18326368a7f5215 [file] [log] [blame]
Thomas Gleixnerde6cc652019-05-27 08:55:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Christian Krafftb3d7dc12006-10-24 18:31:25 +02002/*
3 * thermal support for the cell processor
4 *
Christian Krafft5f7bdae2007-04-23 21:35:41 +02005 * This module adds some sysfs attributes to cpu and spu nodes.
6 * Base for measurements are the digital thermal sensors (DTS)
7 * located on the chip.
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
12 *
13 * The following attributes are added for each node:
14 * temperature:
15 * contains the current temperature measured by the DTS
16 * throttle_begin:
17 * throttling begins when temperature is greater or equal to
18 * throttle_begin. Setting this value to 125 prevents throttling.
19 * throttle_end:
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.
24 * throttle_full_stop:
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.
29 *
Christian Krafftb3d7dc12006-10-24 18:31:25 +020030 * (C) Copyright IBM Deutschland Entwicklung GmbH 2005
31 *
32 * Author: Christian Krafft <krafft@de.ibm.com>
Christian Krafftb3d7dc12006-10-24 18:31:25 +020033 */
34
35#include <linux/module.h>
Kay Sievers8a25a2f2011-12-21 14:29:42 -080036#include <linux/device.h>
Christian Krafftb3d7dc12006-10-24 18:31:25 +020037#include <linux/kernel.h>
38#include <linux/cpu.h>
Christophe Leroy5c35a022018-07-05 16:24:59 +000039#include <linux/stringify.h>
Christian Krafftb3d7dc12006-10-24 18:31:25 +020040#include <asm/spu.h>
41#include <asm/io.h>
42#include <asm/prom.h>
Benjamin Herrenschmidteef686a02007-10-04 15:40:42 +100043#include <asm/cell-regs.h>
Christian Krafftb3d7dc12006-10-24 18:31:25 +020044
Geoff Levande28b0032006-11-23 00:46:49 +010045#include "spu_priv1_mmio.h"
Christian Krafftb3d7dc12006-10-24 18:31:25 +020046
Christian Krafft5f7bdae2007-04-23 21:35:41 +020047#define TEMP_MIN 65
48#define TEMP_MAX 125
49
Kay Sievers8a25a2f2011-12-21 14:29:42 -080050#define DEVICE_PREFIX_ATTR(_prefix,_name,_mode) \
51struct device_attribute attr_ ## _prefix ## _ ## _name = { \
Christian Krafft5f7bdae2007-04-23 21:35:41 +020052 .attr = { .name = __stringify(_name), .mode = _mode }, \
53 .show = _prefix ## _show_ ## _name, \
54 .store = _prefix ## _store_ ## _name, \
55};
56
Christian Krafft24d560d2007-04-23 21:35:40 +020057static inline u8 reg_to_temp(u8 reg_value)
58{
Christian Krafft5f7bdae2007-04-23 21:35:41 +020059 return ((reg_value & 0x3f) << 1) + TEMP_MIN;
60}
61
62static inline u8 temp_to_reg(u8 temp)
63{
64 return ((temp - TEMP_MIN) >> 1) & 0x3f;
Christian Krafft24d560d2007-04-23 21:35:40 +020065}
66
Kay Sievers8a25a2f2011-12-21 14:29:42 -080067static struct cbe_pmd_regs __iomem *get_pmd_regs(struct device *dev)
Christian Krafftb3d7dc12006-10-24 18:31:25 +020068{
69 struct spu *spu;
70
Kay Sievers8a25a2f2011-12-21 14:29:42 -080071 spu = container_of(dev, struct spu, dev);
Christian Krafftb3d7dc12006-10-24 18:31:25 +020072
Geoff Levande28b0032006-11-23 00:46:49 +010073 return cbe_get_pmd_regs(spu_devnode(spu));
Christian Krafftb3d7dc12006-10-24 18:31:25 +020074}
75
76/* returns the value for a given spu in a given register */
Kay Sievers8a25a2f2011-12-21 14:29:42 -080077static u8 spu_read_register_value(struct device *dev, union spe_reg __iomem *reg)
Christian Krafftb3d7dc12006-10-24 18:31:25 +020078{
Christian Krafftb3d7dc12006-10-24 18:31:25 +020079 union spe_reg value;
80 struct spu *spu;
81
Kay Sievers8a25a2f2011-12-21 14:29:42 -080082 spu = container_of(dev, struct spu, dev);
Christian Krafftb3d7dc12006-10-24 18:31:25 +020083 value.val = in_be64(&reg->val);
84
Christian Krafftfa7f3742007-08-23 03:01:25 +100085 return value.spe[spu->spe_id];
Christian Krafftb3d7dc12006-10-24 18:31:25 +020086}
87
Kay Sievers8a25a2f2011-12-21 14:29:42 -080088static ssize_t spu_show_temp(struct device *dev, struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +020089 char *buf)
Christian Krafftb3d7dc12006-10-24 18:31:25 +020090{
Christian Krafft24d560d2007-04-23 21:35:40 +020091 u8 value;
Christian Krafftb3d7dc12006-10-24 18:31:25 +020092 struct cbe_pmd_regs __iomem *pmd_regs;
93
Kay Sievers8a25a2f2011-12-21 14:29:42 -080094 pmd_regs = get_pmd_regs(dev);
Christian Krafftb3d7dc12006-10-24 18:31:25 +020095
Kay Sievers8a25a2f2011-12-21 14:29:42 -080096 value = spu_read_register_value(dev, &pmd_regs->ts_ctsr1);
Christian Krafftb3d7dc12006-10-24 18:31:25 +020097
Christian Krafft24d560d2007-04-23 21:35:40 +020098 return sprintf(buf, "%d\n", reg_to_temp(value));
Christian Krafftb3d7dc12006-10-24 18:31:25 +020099}
100
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200101static ssize_t show_throttle(struct cbe_pmd_regs __iomem *pmd_regs, char *buf, int pos)
102{
103 u64 value;
104
105 value = in_be64(&pmd_regs->tm_tpr.val);
106 /* access the corresponding byte */
107 value >>= pos;
108 value &= 0x3F;
109
110 return sprintf(buf, "%d\n", reg_to_temp(value));
111}
112
113static ssize_t store_throttle(struct cbe_pmd_regs __iomem *pmd_regs, const char *buf, size_t size, int pos)
114{
115 u64 reg_value;
Rickard Strandqvistf5fc8222014-06-14 18:25:11 +0200116 unsigned int temp;
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200117 u64 new_value;
118 int ret;
119
120 ret = sscanf(buf, "%u", &temp);
121
122 if (ret != 1 || temp < TEMP_MIN || temp > TEMP_MAX)
123 return -EINVAL;
124
125 new_value = temp_to_reg(temp);
126
127 reg_value = in_be64(&pmd_regs->tm_tpr.val);
128
129 /* zero out bits for new value */
130 reg_value &= ~(0xffull << pos);
131 /* set bits to new value */
132 reg_value |= new_value << pos;
133
134 out_be64(&pmd_regs->tm_tpr.val, reg_value);
135 return size;
136}
137
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800138static ssize_t spu_show_throttle_end(struct device *dev,
139 struct device_attribute *attr, char *buf)
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200140{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800141 return show_throttle(get_pmd_regs(dev), buf, 0);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200142}
143
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800144static ssize_t spu_show_throttle_begin(struct device *dev,
145 struct device_attribute *attr, char *buf)
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200146{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800147 return show_throttle(get_pmd_regs(dev), buf, 8);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200148}
149
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800150static ssize_t spu_show_throttle_full_stop(struct device *dev,
151 struct device_attribute *attr, char *buf)
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200152{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800153 return show_throttle(get_pmd_regs(dev), buf, 16);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200154}
155
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800156static ssize_t spu_store_throttle_end(struct device *dev,
157 struct device_attribute *attr, const char *buf, size_t size)
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200158{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800159 return store_throttle(get_pmd_regs(dev), buf, size, 0);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200160}
161
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800162static ssize_t spu_store_throttle_begin(struct device *dev,
163 struct device_attribute *attr, const char *buf, size_t size)
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200164{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800165 return store_throttle(get_pmd_regs(dev), buf, size, 8);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200166}
167
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800168static ssize_t spu_store_throttle_full_stop(struct device *dev,
169 struct device_attribute *attr, const char *buf, size_t size)
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200170{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800171 return store_throttle(get_pmd_regs(dev), buf, size, 16);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200172}
173
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800174static ssize_t ppe_show_temp(struct device *dev, char *buf, int pos)
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200175{
176 struct cbe_pmd_regs __iomem *pmd_regs;
177 u64 value;
178
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800179 pmd_regs = cbe_get_cpu_pmd_regs(dev->id);
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200180 value = in_be64(&pmd_regs->ts_ctsr2);
181
Christian Krafft24d560d2007-04-23 21:35:40 +0200182 value = (value >> pos) & 0x3f;
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200183
Christian Krafft24d560d2007-04-23 21:35:40 +0200184 return sprintf(buf, "%d\n", reg_to_temp(value));
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200185}
186
187
188/* shows the temperature of the DTS on the PPE,
189 * located near the linear thermal sensor */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800190static ssize_t ppe_show_temp0(struct device *dev,
191 struct device_attribute *attr, char *buf)
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200192{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800193 return ppe_show_temp(dev, buf, 32);
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200194}
195
196/* shows the temperature of the second DTS on the PPE */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800197static ssize_t ppe_show_temp1(struct device *dev,
198 struct device_attribute *attr, char *buf)
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200199{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800200 return ppe_show_temp(dev, buf, 0);
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200201}
202
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800203static ssize_t ppe_show_throttle_end(struct device *dev,
204 struct device_attribute *attr, char *buf)
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200205{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800206 return show_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, 32);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200207}
208
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800209static ssize_t ppe_show_throttle_begin(struct device *dev,
210 struct device_attribute *attr, char *buf)
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200211{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800212 return show_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, 40);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200213}
214
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800215static ssize_t ppe_show_throttle_full_stop(struct device *dev,
216 struct device_attribute *attr, char *buf)
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200217{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800218 return show_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, 48);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200219}
220
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800221static ssize_t ppe_store_throttle_end(struct device *dev,
222 struct device_attribute *attr, const char *buf, size_t size)
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200223{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800224 return store_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, size, 32);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200225}
226
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800227static ssize_t ppe_store_throttle_begin(struct device *dev,
228 struct device_attribute *attr, const char *buf, size_t size)
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200229{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800230 return store_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, size, 40);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200231}
232
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800233static ssize_t ppe_store_throttle_full_stop(struct device *dev,
234 struct device_attribute *attr, const char *buf, size_t size)
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200235{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800236 return store_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, size, 48);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200237}
238
239
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800240static struct device_attribute attr_spu_temperature = {
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200241 .attr = {.name = "temperature", .mode = 0400 },
242 .show = spu_show_temp,
243};
244
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800245static DEVICE_PREFIX_ATTR(spu, throttle_end, 0600);
246static DEVICE_PREFIX_ATTR(spu, throttle_begin, 0600);
247static DEVICE_PREFIX_ATTR(spu, throttle_full_stop, 0600);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200248
249
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200250static struct attribute *spu_attributes[] = {
251 &attr_spu_temperature.attr,
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200252 &attr_spu_throttle_end.attr,
253 &attr_spu_throttle_begin.attr,
254 &attr_spu_throttle_full_stop.attr,
Christian Krafft22b6e592006-12-07 19:01:16 +0100255 NULL,
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200256};
257
258static struct attribute_group spu_attribute_group = {
259 .name = "thermal",
260 .attrs = spu_attributes,
261};
262
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800263static struct device_attribute attr_ppe_temperature0 = {
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200264 .attr = {.name = "temperature0", .mode = 0400 },
265 .show = ppe_show_temp0,
266};
267
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800268static struct device_attribute attr_ppe_temperature1 = {
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200269 .attr = {.name = "temperature1", .mode = 0400 },
270 .show = ppe_show_temp1,
271};
272
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800273static DEVICE_PREFIX_ATTR(ppe, throttle_end, 0600);
274static DEVICE_PREFIX_ATTR(ppe, throttle_begin, 0600);
275static DEVICE_PREFIX_ATTR(ppe, throttle_full_stop, 0600);
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200276
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200277static struct attribute *ppe_attributes[] = {
278 &attr_ppe_temperature0.attr,
279 &attr_ppe_temperature1.attr,
Christian Krafft5f7bdae2007-04-23 21:35:41 +0200280 &attr_ppe_throttle_end.attr,
281 &attr_ppe_throttle_begin.attr,
282 &attr_ppe_throttle_full_stop.attr,
Christian Krafft22b6e592006-12-07 19:01:16 +0100283 NULL,
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200284};
285
286static struct attribute_group ppe_attribute_group = {
287 .name = "thermal",
288 .attrs = ppe_attributes,
289};
290
291/*
292 * initialize throttling with default values
293 */
Jean-Christophe DUBOIS827e3642007-07-20 21:39:24 +0200294static int __init init_default_values(void)
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200295{
296 int cpu;
297 struct cbe_pmd_regs __iomem *pmd_regs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800298 struct device *dev;
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200299 union ppe_spe_reg tpr;
300 union spe_reg str1;
301 u64 str2;
302 union spe_reg cr1;
303 u64 cr2;
304
305 /* TPR defaults */
306 /* ppe
307 * 1F - no full stop
308 * 08 - dynamic throttling starts if over 80 degrees
309 * 03 - dynamic throttling ceases if below 70 degrees */
310 tpr.ppe = 0x1F0803;
311 /* spe
312 * 10 - full stopped when over 96 degrees
313 * 08 - dynamic throttling starts if over 80 degrees
314 * 03 - dynamic throttling ceases if below 70 degrees
315 */
316 tpr.spe = 0x100803;
317
318 /* STR defaults */
319 /* str1
320 * 10 - stop 16 of 32 cycles
321 */
322 str1.val = 0x1010101010101010ull;
323 /* str2
324 * 10 - stop 16 of 32 cycles
325 */
326 str2 = 0x10;
327
328 /* CR defaults */
329 /* cr1
330 * 4 - normal operation
331 */
332 cr1.val = 0x0404040404040404ull;
333 /* cr2
334 * 4 - normal operation
335 */
336 cr2 = 0x04;
337
338 for_each_possible_cpu (cpu) {
339 pr_debug("processing cpu %d\n", cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800340 dev = get_cpu_device(cpu);
Jean-Christophe DUBOIS827e3642007-07-20 21:39:24 +0200341
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800342 if (!dev) {
343 pr_info("invalid dev pointer for cbe_thermal\n");
Jean-Christophe DUBOIS827e3642007-07-20 21:39:24 +0200344 return -EINVAL;
345 }
346
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800347 pmd_regs = cbe_get_cpu_pmd_regs(dev->id);
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200348
Jean-Christophe DUBOIS827e3642007-07-20 21:39:24 +0200349 if (!pmd_regs) {
350 pr_info("invalid CBE regs pointer for cbe_thermal\n");
351 return -EINVAL;
352 }
353
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200354 out_be64(&pmd_regs->tm_str2, str2);
355 out_be64(&pmd_regs->tm_str1.val, str1.val);
356 out_be64(&pmd_regs->tm_tpr.val, tpr.val);
357 out_be64(&pmd_regs->tm_cr1.val, cr1.val);
358 out_be64(&pmd_regs->tm_cr2, cr2);
359 }
Jean-Christophe DUBOIS827e3642007-07-20 21:39:24 +0200360
361 return 0;
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200362}
363
364
365static int __init thermal_init(void)
366{
Jean-Christophe DUBOIS827e3642007-07-20 21:39:24 +0200367 int rc = init_default_values();
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200368
Jean-Christophe DUBOIS827e3642007-07-20 21:39:24 +0200369 if (rc == 0) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800370 spu_add_dev_attr_group(&spu_attribute_group);
371 cpu_add_dev_attr_group(&ppe_attribute_group);
Jean-Christophe DUBOIS827e3642007-07-20 21:39:24 +0200372 }
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200373
Jean-Christophe DUBOIS827e3642007-07-20 21:39:24 +0200374 return rc;
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200375}
376module_init(thermal_init);
377
378static void __exit thermal_exit(void)
379{
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800380 spu_remove_dev_attr_group(&spu_attribute_group);
381 cpu_remove_dev_attr_group(&ppe_attribute_group);
Christian Krafftb3d7dc12006-10-24 18:31:25 +0200382}
383module_exit(thermal_exit);
384
385MODULE_LICENSE("GPL");
386MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
387