1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48#include <linux/module.h>
49#include <linux/device.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>
56
57#include "spu_priv1_mmio.h"
58
59#define TEMP_MIN 65
60#define TEMP_MAX 125
61
62#define DEVICE_PREFIX_ATTR(_prefix,_name,_mode) \
63struct device_attribute attr_ ## _prefix ## _ ## _name = { \
64 .attr = { .name = __stringify(_name), .mode = _mode }, \
65 .show = _prefix ## _show_ ## _name, \
66 .store = _prefix ## _store_ ## _name, \
67};
68
69static inline u8 reg_to_temp(u8 reg_value)
70{
71 return ((reg_value & 0x3f) << 1) + TEMP_MIN;
72}
73
74static inline u8 temp_to_reg(u8 temp)
75{
76 return ((temp - TEMP_MIN) >> 1) & 0x3f;
77}
78
79static struct cbe_pmd_regs __iomem *get_pmd_regs(struct device *dev)
80{
81 struct spu *spu;
82
83 spu = container_of(dev, struct spu, dev);
84
85 return cbe_get_pmd_regs(spu_devnode(spu));
86}
87
88
89static u8 spu_read_register_value(struct device *dev, union spe_reg __iomem *reg)
90{
91 union spe_reg value;
92 struct spu *spu;
93
94 spu = container_of(dev, struct spu, dev);
95 value.val = in_be64(®->val);
96
97 return value.spe[spu->spe_id];
98}
99
100static ssize_t spu_show_temp(struct device *dev, struct device_attribute *attr,
101 char *buf)
102{
103 u8 value;
104 struct cbe_pmd_regs __iomem *pmd_regs;
105
106 pmd_regs = get_pmd_regs(dev);
107
108 value = spu_read_register_value(dev, &pmd_regs->ts_ctsr1);
109
110 return sprintf(buf, "%d\n", reg_to_temp(value));
111}
112
113static ssize_t show_throttle(struct cbe_pmd_regs __iomem *pmd_regs, char *buf, int pos)
114{
115 u64 value;
116
117 value = in_be64(&pmd_regs->tm_tpr.val);
118
119 value >>= pos;
120 value &= 0x3F;
121
122 return sprintf(buf, "%d\n", reg_to_temp(value));
123}
124
125static ssize_t store_throttle(struct cbe_pmd_regs __iomem *pmd_regs, const char *buf, size_t size, int pos)
126{
127 u64 reg_value;
128 unsigned int temp;
129 u64 new_value;
130 int ret;
131
132 ret = sscanf(buf, "%u", &temp);
133
134 if (ret != 1 || temp < TEMP_MIN || temp > TEMP_MAX)
135 return -EINVAL;
136
137 new_value = temp_to_reg(temp);
138
139 reg_value = in_be64(&pmd_regs->tm_tpr.val);
140
141
142 reg_value &= ~(0xffull << pos);
143
144 reg_value |= new_value << pos;
145
146 out_be64(&pmd_regs->tm_tpr.val, reg_value);
147 return size;
148}
149
150static ssize_t spu_show_throttle_end(struct device *dev,
151 struct device_attribute *attr, char *buf)
152{
153 return show_throttle(get_pmd_regs(dev), buf, 0);
154}
155
156static ssize_t spu_show_throttle_begin(struct device *dev,
157 struct device_attribute *attr, char *buf)
158{
159 return show_throttle(get_pmd_regs(dev), buf, 8);
160}
161
162static ssize_t spu_show_throttle_full_stop(struct device *dev,
163 struct device_attribute *attr, char *buf)
164{
165 return show_throttle(get_pmd_regs(dev), buf, 16);
166}
167
168static ssize_t spu_store_throttle_end(struct device *dev,
169 struct device_attribute *attr, const char *buf, size_t size)
170{
171 return store_throttle(get_pmd_regs(dev), buf, size, 0);
172}
173
174static ssize_t spu_store_throttle_begin(struct device *dev,
175 struct device_attribute *attr, const char *buf, size_t size)
176{
177 return store_throttle(get_pmd_regs(dev), buf, size, 8);
178}
179
180static ssize_t spu_store_throttle_full_stop(struct device *dev,
181 struct device_attribute *attr, const char *buf, size_t size)
182{
183 return store_throttle(get_pmd_regs(dev), buf, size, 16);
184}
185
186static ssize_t ppe_show_temp(struct device *dev, char *buf, int pos)
187{
188 struct cbe_pmd_regs __iomem *pmd_regs;
189 u64 value;
190
191 pmd_regs = cbe_get_cpu_pmd_regs(dev->id);
192 value = in_be64(&pmd_regs->ts_ctsr2);
193
194 value = (value >> pos) & 0x3f;
195
196 return sprintf(buf, "%d\n", reg_to_temp(value));
197}
198
199
200
201
202static ssize_t ppe_show_temp0(struct device *dev,
203 struct device_attribute *attr, char *buf)
204{
205 return ppe_show_temp(dev, buf, 32);
206}
207
208
209static ssize_t ppe_show_temp1(struct device *dev,
210 struct device_attribute *attr, char *buf)
211{
212 return ppe_show_temp(dev, buf, 0);
213}
214
215static ssize_t ppe_show_throttle_end(struct device *dev,
216 struct device_attribute *attr, char *buf)
217{
218 return show_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, 32);
219}
220
221static ssize_t ppe_show_throttle_begin(struct device *dev,
222 struct device_attribute *attr, char *buf)
223{
224 return show_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, 40);
225}
226
227static ssize_t ppe_show_throttle_full_stop(struct device *dev,
228 struct device_attribute *attr, char *buf)
229{
230 return show_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, 48);
231}
232
233static ssize_t ppe_store_throttle_end(struct device *dev,
234 struct device_attribute *attr, const char *buf, size_t size)
235{
236 return store_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, size, 32);
237}
238
239static ssize_t ppe_store_throttle_begin(struct device *dev,
240 struct device_attribute *attr, const char *buf, size_t size)
241{
242 return store_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, size, 40);
243}
244
245static ssize_t ppe_store_throttle_full_stop(struct device *dev,
246 struct device_attribute *attr, const char *buf, size_t size)
247{
248 return store_throttle(cbe_get_cpu_pmd_regs(dev->id), buf, size, 48);
249}
250
251
252static struct device_attribute attr_spu_temperature = {
253 .attr = {.name = "temperature", .mode = 0400 },
254 .show = spu_show_temp,
255};
256
257static DEVICE_PREFIX_ATTR(spu, throttle_end, 0600);
258static DEVICE_PREFIX_ATTR(spu, throttle_begin, 0600);
259static DEVICE_PREFIX_ATTR(spu, throttle_full_stop, 0600);
260
261
262static 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,
268};
269
270static struct attribute_group spu_attribute_group = {
271 .name = "thermal",
272 .attrs = spu_attributes,
273};
274
275static struct device_attribute attr_ppe_temperature0 = {
276 .attr = {.name = "temperature0", .mode = 0400 },
277 .show = ppe_show_temp0,
278};
279
280static struct device_attribute attr_ppe_temperature1 = {
281 .attr = {.name = "temperature1", .mode = 0400 },
282 .show = ppe_show_temp1,
283};
284
285static DEVICE_PREFIX_ATTR(ppe, throttle_end, 0600);
286static DEVICE_PREFIX_ATTR(ppe, throttle_begin, 0600);
287static DEVICE_PREFIX_ATTR(ppe, throttle_full_stop, 0600);
288
289static 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,
296};
297
298static struct attribute_group ppe_attribute_group = {
299 .name = "thermal",
300 .attrs = ppe_attributes,
301};
302
303
304
305
306static int __init init_default_values(void)
307{
308 int cpu;
309 struct cbe_pmd_regs __iomem *pmd_regs;
310 struct device *dev;
311 union ppe_spe_reg tpr;
312 union spe_reg str1;
313 u64 str2;
314 union spe_reg cr1;
315 u64 cr2;
316
317
318
319
320
321
322 tpr.ppe = 0x1F0803;
323
324
325
326
327
328 tpr.spe = 0x100803;
329
330
331
332
333
334 str1.val = 0x1010101010101010ull;
335
336
337
338 str2 = 0x10;
339
340
341
342
343
344 cr1.val = 0x0404040404040404ull;
345
346
347
348 cr2 = 0x04;
349
350 for_each_possible_cpu (cpu) {
351 pr_debug("processing cpu %d\n", cpu);
352 dev = get_cpu_device(cpu);
353
354 if (!dev) {
355 pr_info("invalid dev pointer for cbe_thermal\n");
356 return -EINVAL;
357 }
358
359 pmd_regs = cbe_get_cpu_pmd_regs(dev->id);
360
361 if (!pmd_regs) {
362 pr_info("invalid CBE regs pointer for cbe_thermal\n");
363 return -EINVAL;
364 }
365
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);
371 }
372
373 return 0;
374}
375
376
377static int __init thermal_init(void)
378{
379 int rc = init_default_values();
380
381 if (rc == 0) {
382 spu_add_dev_attr_group(&spu_attribute_group);
383 cpu_add_dev_attr_group(&ppe_attribute_group);
384 }
385
386 return rc;
387}
388module_init(thermal_init);
389
390static void __exit thermal_exit(void)
391{
392 spu_remove_dev_attr_group(&spu_attribute_group);
393 cpu_remove_dev_attr_group(&ppe_attribute_group);
394}
395module_exit(thermal_exit);
396
397MODULE_LICENSE("GPL");
398MODULE_AUTHOR("Christian Krafft <krafft@de.ibm.com>");
399
400