blob: e2147d7c9e72fb38f7ff86e5b2bc31b9263d895a [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Kay Sievers8a25a2f2011-12-21 14:29:42 -08002#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <linux/cpu.h>
4#include <linux/smp.h>
5#include <linux/percpu.h>
6#include <linux/init.h>
7#include <linux/sched.h>
Paul Gortmaker4b16f8e2011-07-22 18:24:23 -04008#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/nodemask.h>
10#include <linux/cpumask.h>
11#include <linux/notifier.h>
12
13#include <asm/current.h>
14#include <asm/processor.h>
15#include <asm/cputable.h>
16#include <asm/hvcall.h>
17#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <asm/machdep.h>
Paul Mackerras2249ca92005-11-07 13:18:13 +110019#include <asm/smp.h>
Paul Mackerrasa6dbf932009-09-09 01:26:03 +000020#include <asm/pmc.h>
Madhavan Srinivasand1211af2013-10-02 00:34:10 +053021#include <asm/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Nathan Lynch93197a32008-12-23 18:55:54 +000023#include "cacheinfo.h"
Nicholas Pigginc0abd0c2018-02-14 01:08:17 +100024#include "setup.h"
Nathan Lynch93197a32008-12-23 18:55:54 +000025
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +100026#ifdef CONFIG_PPC64
27#include <asm/paca.h>
28#include <asm/lppaca.h>
29#endif
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031static DEFINE_PER_CPU(struct cpu, cpu_devices);
32
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +100033/*
34 * SMT snooze delay stuff, 64-bit only for now
35 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +100037#ifdef CONFIG_PPC64
38
Anton Blanchard0ddd3e72006-09-22 20:30:14 +100039/* Time in microseconds we delay before sleeping in the idle loop */
Daniel Axtens34852ed2016-05-18 11:16:49 +100040static DEFINE_PER_CPU(long, smt_snooze_delay) = { 100 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Kay Sievers8a25a2f2011-12-21 14:29:42 -080042static ssize_t store_smt_snooze_delay(struct device *dev,
43 struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +020044 const char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 size_t count)
46{
Kay Sievers8a25a2f2011-12-21 14:29:42 -080047 struct cpu *cpu = container_of(dev, struct cpu, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 ssize_t ret;
Anton Blanchardb878dc02010-05-16 20:02:39 +000049 long snooze;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Anton Blanchardb878dc02010-05-16 20:02:39 +000051 ret = sscanf(buf, "%ld", &snooze);
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 if (ret != 1)
53 return -EINVAL;
54
Kay Sievers8a25a2f2011-12-21 14:29:42 -080055 per_cpu(smt_snooze_delay, cpu->dev.id) = snooze;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 return count;
57}
58
Kay Sievers8a25a2f2011-12-21 14:29:42 -080059static ssize_t show_smt_snooze_delay(struct device *dev,
60 struct device_attribute *attr,
Andi Kleen4a0b2b42008-07-01 18:48:41 +020061 char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
Kay Sievers8a25a2f2011-12-21 14:29:42 -080063 struct cpu *cpu = container_of(dev, struct cpu, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Kay Sievers8a25a2f2011-12-21 14:29:42 -080065 return sprintf(buf, "%ld\n", per_cpu(smt_snooze_delay, cpu->dev.id));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
Kay Sievers8a25a2f2011-12-21 14:29:42 -080068static DEVICE_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 store_smt_snooze_delay);
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071static int __init setup_smt_snooze_delay(char *str)
72{
73 unsigned int cpu;
Anton Blanchardb878dc02010-05-16 20:02:39 +000074 long snooze;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 if (!cpu_has_feature(CPU_FTR_SMT))
77 return 1;
78
Anton Blanchardb878dc02010-05-16 20:02:39 +000079 snooze = simple_strtol(str, NULL, 10);
80 for_each_possible_cpu(cpu)
81 per_cpu(smt_snooze_delay, cpu) = snooze;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 return 1;
84}
85__setup("smt-snooze-delay=", setup_smt_snooze_delay);
86
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +100087#endif /* CONFIG_PPC64 */
Michael Ellerman180a3362005-08-09 11:13:36 +100088
Wang Dongshenga7189482013-12-17 16:17:02 +080089#ifdef CONFIG_PPC_FSL_BOOK3E
90#define MAX_BIT 63
91
92static u64 pw20_wt;
93static u64 altivec_idle_wt;
94
95static unsigned int get_idle_ticks_bit(u64 ns)
96{
97 u64 cycle;
98
99 if (ns >= 10000)
100 cycle = div_u64(ns + 500, 1000) * tb_ticks_per_usec;
101 else
102 cycle = div_u64(ns * tb_ticks_per_usec, 1000);
103
104 if (!cycle)
105 return 0;
106
107 return ilog2(cycle);
108}
109
110static void do_show_pwrmgtcr0(void *val)
111{
112 u32 *value = val;
113
114 *value = mfspr(SPRN_PWRMGTCR0);
115}
116
117static ssize_t show_pw20_state(struct device *dev,
118 struct device_attribute *attr, char *buf)
119{
120 u32 value;
121 unsigned int cpu = dev->id;
122
123 smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
124
125 value &= PWRMGTCR0_PW20_WAIT;
126
127 return sprintf(buf, "%u\n", value ? 1 : 0);
128}
129
130static void do_store_pw20_state(void *val)
131{
132 u32 *value = val;
133 u32 pw20_state;
134
135 pw20_state = mfspr(SPRN_PWRMGTCR0);
136
137 if (*value)
138 pw20_state |= PWRMGTCR0_PW20_WAIT;
139 else
140 pw20_state &= ~PWRMGTCR0_PW20_WAIT;
141
142 mtspr(SPRN_PWRMGTCR0, pw20_state);
143}
144
145static ssize_t store_pw20_state(struct device *dev,
146 struct device_attribute *attr,
147 const char *buf, size_t count)
148{
149 u32 value;
150 unsigned int cpu = dev->id;
151
152 if (kstrtou32(buf, 0, &value))
153 return -EINVAL;
154
155 if (value > 1)
156 return -EINVAL;
157
158 smp_call_function_single(cpu, do_store_pw20_state, &value, 1);
159
160 return count;
161}
162
163static ssize_t show_pw20_wait_time(struct device *dev,
164 struct device_attribute *attr, char *buf)
165{
166 u32 value;
167 u64 tb_cycle = 1;
168 u64 time;
169
170 unsigned int cpu = dev->id;
171
172 if (!pw20_wt) {
173 smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
174 value = (value & PWRMGTCR0_PW20_ENT) >>
175 PWRMGTCR0_PW20_ENT_SHIFT;
176
177 tb_cycle = (tb_cycle << (MAX_BIT - value + 1));
178 /* convert ms to ns */
179 if (tb_ticks_per_usec > 1000) {
180 time = div_u64(tb_cycle, tb_ticks_per_usec / 1000);
181 } else {
182 u32 rem_us;
183
184 time = div_u64_rem(tb_cycle, tb_ticks_per_usec,
185 &rem_us);
186 time = time * 1000 + rem_us * 1000 / tb_ticks_per_usec;
187 }
188 } else {
189 time = pw20_wt;
190 }
191
192 return sprintf(buf, "%llu\n", time > 0 ? time : 0);
193}
194
195static void set_pw20_wait_entry_bit(void *val)
196{
197 u32 *value = val;
198 u32 pw20_idle;
199
200 pw20_idle = mfspr(SPRN_PWRMGTCR0);
201
202 /* Set Automatic PW20 Core Idle Count */
203 /* clear count */
204 pw20_idle &= ~PWRMGTCR0_PW20_ENT;
205
206 /* set count */
207 pw20_idle |= ((MAX_BIT - *value) << PWRMGTCR0_PW20_ENT_SHIFT);
208
209 mtspr(SPRN_PWRMGTCR0, pw20_idle);
210}
211
212static ssize_t store_pw20_wait_time(struct device *dev,
213 struct device_attribute *attr,
214 const char *buf, size_t count)
215{
216 u32 entry_bit;
217 u64 value;
218
219 unsigned int cpu = dev->id;
220
221 if (kstrtou64(buf, 0, &value))
222 return -EINVAL;
223
224 if (!value)
225 return -EINVAL;
226
227 entry_bit = get_idle_ticks_bit(value);
228 if (entry_bit > MAX_BIT)
229 return -EINVAL;
230
231 pw20_wt = value;
232
233 smp_call_function_single(cpu, set_pw20_wait_entry_bit,
234 &entry_bit, 1);
235
236 return count;
237}
238
239static ssize_t show_altivec_idle(struct device *dev,
240 struct device_attribute *attr, char *buf)
241{
242 u32 value;
243 unsigned int cpu = dev->id;
244
245 smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
246
247 value &= PWRMGTCR0_AV_IDLE_PD_EN;
248
249 return sprintf(buf, "%u\n", value ? 1 : 0);
250}
251
252static void do_store_altivec_idle(void *val)
253{
254 u32 *value = val;
255 u32 altivec_idle;
256
257 altivec_idle = mfspr(SPRN_PWRMGTCR0);
258
259 if (*value)
260 altivec_idle |= PWRMGTCR0_AV_IDLE_PD_EN;
261 else
262 altivec_idle &= ~PWRMGTCR0_AV_IDLE_PD_EN;
263
264 mtspr(SPRN_PWRMGTCR0, altivec_idle);
265}
266
267static ssize_t store_altivec_idle(struct device *dev,
268 struct device_attribute *attr,
269 const char *buf, size_t count)
270{
271 u32 value;
272 unsigned int cpu = dev->id;
273
274 if (kstrtou32(buf, 0, &value))
275 return -EINVAL;
276
277 if (value > 1)
278 return -EINVAL;
279
280 smp_call_function_single(cpu, do_store_altivec_idle, &value, 1);
281
282 return count;
283}
284
285static ssize_t show_altivec_idle_wait_time(struct device *dev,
286 struct device_attribute *attr, char *buf)
287{
288 u32 value;
289 u64 tb_cycle = 1;
290 u64 time;
291
292 unsigned int cpu = dev->id;
293
294 if (!altivec_idle_wt) {
295 smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
296 value = (value & PWRMGTCR0_AV_IDLE_CNT) >>
297 PWRMGTCR0_AV_IDLE_CNT_SHIFT;
298
299 tb_cycle = (tb_cycle << (MAX_BIT - value + 1));
300 /* convert ms to ns */
301 if (tb_ticks_per_usec > 1000) {
302 time = div_u64(tb_cycle, tb_ticks_per_usec / 1000);
303 } else {
304 u32 rem_us;
305
306 time = div_u64_rem(tb_cycle, tb_ticks_per_usec,
307 &rem_us);
308 time = time * 1000 + rem_us * 1000 / tb_ticks_per_usec;
309 }
310 } else {
311 time = altivec_idle_wt;
312 }
313
314 return sprintf(buf, "%llu\n", time > 0 ? time : 0);
315}
316
317static void set_altivec_idle_wait_entry_bit(void *val)
318{
319 u32 *value = val;
320 u32 altivec_idle;
321
322 altivec_idle = mfspr(SPRN_PWRMGTCR0);
323
324 /* Set Automatic AltiVec Idle Count */
325 /* clear count */
326 altivec_idle &= ~PWRMGTCR0_AV_IDLE_CNT;
327
328 /* set count */
329 altivec_idle |= ((MAX_BIT - *value) << PWRMGTCR0_AV_IDLE_CNT_SHIFT);
330
331 mtspr(SPRN_PWRMGTCR0, altivec_idle);
332}
333
334static ssize_t store_altivec_idle_wait_time(struct device *dev,
335 struct device_attribute *attr,
336 const char *buf, size_t count)
337{
338 u32 entry_bit;
339 u64 value;
340
341 unsigned int cpu = dev->id;
342
343 if (kstrtou64(buf, 0, &value))
344 return -EINVAL;
345
346 if (!value)
347 return -EINVAL;
348
349 entry_bit = get_idle_ticks_bit(value);
350 if (entry_bit > MAX_BIT)
351 return -EINVAL;
352
353 altivec_idle_wt = value;
354
355 smp_call_function_single(cpu, set_altivec_idle_wait_entry_bit,
356 &entry_bit, 1);
357
358 return count;
359}
360
361/*
362 * Enable/Disable interface:
363 * 0, disable. 1, enable.
364 */
365static DEVICE_ATTR(pw20_state, 0600, show_pw20_state, store_pw20_state);
366static DEVICE_ATTR(altivec_idle, 0600, show_altivec_idle, store_altivec_idle);
367
368/*
369 * Set wait time interface:(Nanosecond)
370 * Example: Base on TBfreq is 41MHZ.
371 * 1~48(ns): TB[63]
372 * 49~97(ns): TB[62]
373 * 98~195(ns): TB[61]
374 * 196~390(ns): TB[60]
375 * 391~780(ns): TB[59]
376 * 781~1560(ns): TB[58]
377 * ...
378 */
379static DEVICE_ATTR(pw20_wait_time, 0600,
380 show_pw20_wait_time,
381 store_pw20_wait_time);
382static DEVICE_ATTR(altivec_idle_wait_time, 0600,
383 show_altivec_idle_wait_time,
384 store_altivec_idle_wait_time);
385#endif
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/*
388 * Enabling PMCs will slow partition context switch times so we only do
389 * it the first time we write to the PMCs.
390 */
391
392static DEFINE_PER_CPU(char, pmcs_enabled);
393
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000394void ppc_enable_pmcs(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Paul Mackerrasa6dbf932009-09-09 01:26:03 +0000396 ppc_set_pmu_inuse(1);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 /* Only need to enable them once */
Christoph Lameter69111ba2014-10-21 15:23:25 -0500399 if (__this_cpu_read(pmcs_enabled))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 return;
401
Christoph Lameter69111ba2014-10-21 15:23:25 -0500402 __this_cpu_write(pmcs_enabled, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Michael Ellerman180a3362005-08-09 11:13:36 +1000404 if (ppc_md.enable_pmcs)
405 ppc_md.enable_pmcs();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000407EXPORT_SYMBOL(ppc_enable_pmcs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Sam bobroff39a360e2014-05-21 16:32:37 +1000409#define __SYSFS_SPRSETUP_READ_WRITE(NAME, ADDRESS, EXTRA) \
Rusty Russell9a371932009-03-11 12:20:05 +0000410static void read_##NAME(void *val) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{ \
Benjamin Herrenschmidtec78c8a2009-03-26 19:29:06 +0000412 *(unsigned long *)val = mfspr(ADDRESS); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413} \
Benjamin Herrenschmidtec78c8a2009-03-26 19:29:06 +0000414static void write_##NAME(void *val) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{ \
Madhavan Srinivasanfd7e4292013-10-03 14:57:35 +0530416 EXTRA; \
Rusty Russell9a371932009-03-11 12:20:05 +0000417 mtspr(ADDRESS, *(unsigned long *)val); \
Sam bobroff39a360e2014-05-21 16:32:37 +1000418}
419
420#define __SYSFS_SPRSETUP_SHOW_STORE(NAME) \
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800421static ssize_t show_##NAME(struct device *dev, \
422 struct device_attribute *attr, \
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200423 char *buf) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424{ \
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800425 struct cpu *cpu = container_of(dev, struct cpu, dev); \
Rusty Russell9a371932009-03-11 12:20:05 +0000426 unsigned long val; \
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800427 smp_call_function_single(cpu->dev.id, read_##NAME, &val, 1); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 return sprintf(buf, "%lx\n", val); \
429} \
Adrian Bunk3ff6eec2008-01-24 22:16:20 +0100430static ssize_t __used \
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800431 store_##NAME(struct device *dev, struct device_attribute *attr, \
Andi Kleen4a0b2b42008-07-01 18:48:41 +0200432 const char *buf, size_t count) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{ \
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800434 struct cpu *cpu = container_of(dev, struct cpu, dev); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 unsigned long val; \
436 int ret = sscanf(buf, "%lx", &val); \
437 if (ret != 1) \
438 return -EINVAL; \
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800439 smp_call_function_single(cpu->dev.id, write_##NAME, &val, 1); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 return count; \
441}
442
Sam bobroff39a360e2014-05-21 16:32:37 +1000443#define SYSFS_PMCSETUP(NAME, ADDRESS) \
444 __SYSFS_SPRSETUP_READ_WRITE(NAME, ADDRESS, ppc_enable_pmcs()) \
445 __SYSFS_SPRSETUP_SHOW_STORE(NAME)
446#define SYSFS_SPRSETUP(NAME, ADDRESS) \
447 __SYSFS_SPRSETUP_READ_WRITE(NAME, ADDRESS, ) \
448 __SYSFS_SPRSETUP_SHOW_STORE(NAME)
449
450#define SYSFS_SPRSETUP_SHOW_STORE(NAME) \
451 __SYSFS_SPRSETUP_SHOW_STORE(NAME)
Olof Johansson6529c132007-01-28 21:25:57 -0600452
453/* Let's define all possible registers, we'll only hook up the ones
454 * that are implemented on the current processor
455 */
456
Kumar Gala33a7f122008-09-18 17:50:42 -0500457#if defined(CONFIG_PPC64)
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000458#define HAS_PPC_PMC_CLASSIC 1
459#define HAS_PPC_PMC_IBM 1
460#define HAS_PPC_PMC_PA6T 1
Christophe Leroyd7cceda2018-11-17 10:24:56 +0000461#elif defined(CONFIG_PPC_BOOK3S_32)
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000462#define HAS_PPC_PMC_CLASSIC 1
463#define HAS_PPC_PMC_IBM 1
464#define HAS_PPC_PMC_G4 1
465#endif
466
467
468#ifdef HAS_PPC_PMC_CLASSIC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
470SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
472SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
473SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
474SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
475SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
476SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000477
478#ifdef HAS_PPC_PMC_G4
479SYSFS_PMCSETUP(mmcr2, SPRN_MMCR2);
480#endif
481
482#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
484SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000485
486SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
Madhavan Srinivasanfd7e4292013-10-03 14:57:35 +0530487SYSFS_SPRSETUP(purr, SPRN_PURR);
488SYSFS_SPRSETUP(spurr, SPRN_SPURR);
Madhavan Srinivasanfd7e4292013-10-03 14:57:35 +0530489SYSFS_SPRSETUP(pir, SPRN_PIR);
Anton Blanchardb6d34eb2017-09-08 03:11:12 +1000490SYSFS_SPRSETUP(tscr, SPRN_TSCR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Madhavan Srinivasand1211af2013-10-02 00:34:10 +0530492/*
493 Lets only enable read for phyp resources and
494 enable write when needed with a separate function.
495 Lets be conservative and default to pseries.
496*/
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800497static DEVICE_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
Benjamin Herrenschmidtd5dae7212013-05-06 15:02:40 +1000498static DEVICE_ATTR(spurr, 0400, show_spurr, NULL);
Madhavan Srinivasand1211af2013-10-02 00:34:10 +0530499static DEVICE_ATTR(purr, 0400, show_purr, store_purr);
Linus Torvalds7affca32012-01-07 12:03:30 -0800500static DEVICE_ATTR(pir, 0400, show_pir, NULL);
Anton Blanchardb6d34eb2017-09-08 03:11:12 +1000501static DEVICE_ATTR(tscr, 0600, show_tscr, store_tscr);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000502
Anshuman Khanduald3cb06e2015-05-21 12:13:04 +0530503/*
504 * This is the system wide DSCR register default value. Any
505 * change to this default value through the sysfs interface
506 * will update all per cpu DSCR default values across the
507 * system stored in their respective PACA structures.
508 */
Sam bobroff1739ea92014-05-21 16:32:38 +1000509static unsigned long dscr_default;
510
Anshuman Khanduald3cb06e2015-05-21 12:13:04 +0530511/**
512 * read_dscr() - Fetch the cpu specific DSCR default
513 * @val: Returned cpu specific DSCR default value
514 *
515 * This function returns the per cpu DSCR default value
516 * for any cpu which is contained in it's PACA structure.
517 */
Sam bobroff1739ea92014-05-21 16:32:38 +1000518static void read_dscr(void *val)
519{
520 *(unsigned long *)val = get_paca()->dscr_default;
521}
522
Anshuman Khanduald3cb06e2015-05-21 12:13:04 +0530523
524/**
525 * write_dscr() - Update the cpu specific DSCR default
526 * @val: New cpu specific DSCR default value to update
527 *
528 * This function updates the per cpu DSCR default value
529 * for any cpu which is contained in it's PACA structure.
530 */
Sam bobroff1739ea92014-05-21 16:32:38 +1000531static void write_dscr(void *val)
532{
533 get_paca()->dscr_default = *(unsigned long *)val;
534 if (!current->thread.dscr_inherit) {
535 current->thread.dscr = *(unsigned long *)val;
536 mtspr(SPRN_DSCR, *(unsigned long *)val);
537 }
538}
539
540SYSFS_SPRSETUP_SHOW_STORE(dscr);
541static DEVICE_ATTR(dscr, 0600, show_dscr, store_dscr);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000542
Madhavan Srinivasand1211af2013-10-02 00:34:10 +0530543static void add_write_permission_dev_attr(struct device_attribute *attr)
544{
545 attr->attr.mode |= 0200;
546}
547
Anshuman Khanduald3cb06e2015-05-21 12:13:04 +0530548/**
549 * show_dscr_default() - Fetch the system wide DSCR default
550 * @dev: Device structure
551 * @attr: Device attribute structure
552 * @buf: Interface buffer
553 *
554 * This function returns the system wide DSCR default value.
555 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800556static ssize_t show_dscr_default(struct device *dev,
557 struct device_attribute *attr, char *buf)
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000558{
559 return sprintf(buf, "%lx\n", dscr_default);
560}
561
Anshuman Khanduald3cb06e2015-05-21 12:13:04 +0530562/**
563 * store_dscr_default() - Update the system wide DSCR default
564 * @dev: Device structure
565 * @attr: Device attribute structure
566 * @buf: Interface buffer
567 * @count: Size of the update
568 *
569 * This function updates the system wide DSCR default value.
570 */
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800571static ssize_t __used store_dscr_default(struct device *dev,
572 struct device_attribute *attr, const char *buf,
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000573 size_t count)
574{
575 unsigned long val;
576 int ret = 0;
577
578 ret = sscanf(buf, "%lx", &val);
579 if (ret != 1)
580 return -EINVAL;
581 dscr_default = val;
582
Sam bobroff1739ea92014-05-21 16:32:38 +1000583 on_each_cpu(write_dscr, &val, 1);
Anton Blanchard1b6ca2a2012-09-03 16:47:56 +0000584
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000585 return count;
586}
587
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800588static DEVICE_ATTR(dscr_default, 0600,
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +0000589 show_dscr_default, store_dscr_default);
590
591static void sysfs_create_dscr_default(void)
592{
Nicholas Piggin1696d0f2017-10-24 21:44:44 +1000593 if (cpu_has_feature(CPU_FTR_DSCR)) {
Nicholas Pigginc0abd0c2018-02-14 01:08:17 +1000594 int err = 0;
595 int cpu;
596
597 dscr_default = spr_default_dscr;
598 for_each_possible_cpu(cpu)
Nicholas Piggind2e60072018-02-14 01:08:12 +1000599 paca_ptrs[cpu]->dscr_default = dscr_default;
Nicholas Pigginc0abd0c2018-02-14 01:08:17 +1000600
601 err = device_create_file(cpu_subsys.dev_root, &dev_attr_dscr_default);
Nicholas Piggin1696d0f2017-10-24 21:44:44 +1000602 }
603}
Nicholas Pigginc0abd0c2018-02-14 01:08:17 +1000604
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000605#endif /* CONFIG_PPC64 */
606
607#ifdef HAS_PPC_PMC_PA6T
Olof Johansson25fc5302007-04-18 16:38:21 +1000608SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0);
609SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1);
610SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2);
611SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3);
612SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4);
613SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5);
Sinan Kayaefb463c2019-05-14 15:44:04 -0700614#ifdef CONFIG_DEBUG_MISC
Madhavan Srinivasanfd7e4292013-10-03 14:57:35 +0530615SYSFS_SPRSETUP(hid0, SPRN_HID0);
616SYSFS_SPRSETUP(hid1, SPRN_HID1);
617SYSFS_SPRSETUP(hid4, SPRN_HID4);
618SYSFS_SPRSETUP(hid5, SPRN_HID5);
619SYSFS_SPRSETUP(ima0, SPRN_PA6T_IMA0);
620SYSFS_SPRSETUP(ima1, SPRN_PA6T_IMA1);
621SYSFS_SPRSETUP(ima2, SPRN_PA6T_IMA2);
622SYSFS_SPRSETUP(ima3, SPRN_PA6T_IMA3);
623SYSFS_SPRSETUP(ima4, SPRN_PA6T_IMA4);
624SYSFS_SPRSETUP(ima5, SPRN_PA6T_IMA5);
625SYSFS_SPRSETUP(ima6, SPRN_PA6T_IMA6);
626SYSFS_SPRSETUP(ima7, SPRN_PA6T_IMA7);
627SYSFS_SPRSETUP(ima8, SPRN_PA6T_IMA8);
628SYSFS_SPRSETUP(ima9, SPRN_PA6T_IMA9);
629SYSFS_SPRSETUP(imaat, SPRN_PA6T_IMAAT);
630SYSFS_SPRSETUP(btcr, SPRN_PA6T_BTCR);
631SYSFS_SPRSETUP(pccr, SPRN_PA6T_PCCR);
632SYSFS_SPRSETUP(rpccr, SPRN_PA6T_RPCCR);
633SYSFS_SPRSETUP(der, SPRN_PA6T_DER);
634SYSFS_SPRSETUP(mer, SPRN_PA6T_MER);
635SYSFS_SPRSETUP(ber, SPRN_PA6T_BER);
636SYSFS_SPRSETUP(ier, SPRN_PA6T_IER);
637SYSFS_SPRSETUP(sier, SPRN_PA6T_SIER);
638SYSFS_SPRSETUP(siar, SPRN_PA6T_SIAR);
639SYSFS_SPRSETUP(tsr0, SPRN_PA6T_TSR0);
640SYSFS_SPRSETUP(tsr1, SPRN_PA6T_TSR1);
641SYSFS_SPRSETUP(tsr2, SPRN_PA6T_TSR2);
642SYSFS_SPRSETUP(tsr3, SPRN_PA6T_TSR3);
Sinan Kayaefb463c2019-05-14 15:44:04 -0700643#endif /* CONFIG_DEBUG_MISC */
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000644#endif /* HAS_PPC_PMC_PA6T */
Olof Johansson6529c132007-01-28 21:25:57 -0600645
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000646#ifdef HAS_PPC_PMC_IBM
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800647static struct device_attribute ibm_common_attrs[] = {
648 __ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
649 __ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
Olof Johansson6529c132007-01-28 21:25:57 -0600650};
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000651#endif /* HAS_PPC_PMC_G4 */
Olof Johansson6529c132007-01-28 21:25:57 -0600652
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000653#ifdef HAS_PPC_PMC_G4
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800654static struct device_attribute g4_common_attrs[] = {
655 __ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
656 __ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
657 __ATTR(mmcr2, 0600, show_mmcr2, store_mmcr2),
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000658};
659#endif /* HAS_PPC_PMC_G4 */
660
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800661static struct device_attribute classic_pmc_attrs[] = {
662 __ATTR(pmc1, 0600, show_pmc1, store_pmc1),
663 __ATTR(pmc2, 0600, show_pmc2, store_pmc2),
664 __ATTR(pmc3, 0600, show_pmc3, store_pmc3),
665 __ATTR(pmc4, 0600, show_pmc4, store_pmc4),
666 __ATTR(pmc5, 0600, show_pmc5, store_pmc5),
667 __ATTR(pmc6, 0600, show_pmc6, store_pmc6),
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000668#ifdef CONFIG_PPC64
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800669 __ATTR(pmc7, 0600, show_pmc7, store_pmc7),
670 __ATTR(pmc8, 0600, show_pmc8, store_pmc8),
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000671#endif
Olof Johansson6529c132007-01-28 21:25:57 -0600672};
673
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000674#ifdef HAS_PPC_PMC_PA6T
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800675static struct device_attribute pa6t_attrs[] = {
676 __ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
677 __ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
678 __ATTR(pmc0, 0600, show_pa6t_pmc0, store_pa6t_pmc0),
679 __ATTR(pmc1, 0600, show_pa6t_pmc1, store_pa6t_pmc1),
680 __ATTR(pmc2, 0600, show_pa6t_pmc2, store_pa6t_pmc2),
681 __ATTR(pmc3, 0600, show_pa6t_pmc3, store_pa6t_pmc3),
682 __ATTR(pmc4, 0600, show_pa6t_pmc4, store_pa6t_pmc4),
683 __ATTR(pmc5, 0600, show_pa6t_pmc5, store_pa6t_pmc5),
Sinan Kayaefb463c2019-05-14 15:44:04 -0700684#ifdef CONFIG_DEBUG_MISC
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800685 __ATTR(hid0, 0600, show_hid0, store_hid0),
686 __ATTR(hid1, 0600, show_hid1, store_hid1),
687 __ATTR(hid4, 0600, show_hid4, store_hid4),
688 __ATTR(hid5, 0600, show_hid5, store_hid5),
689 __ATTR(ima0, 0600, show_ima0, store_ima0),
690 __ATTR(ima1, 0600, show_ima1, store_ima1),
691 __ATTR(ima2, 0600, show_ima2, store_ima2),
692 __ATTR(ima3, 0600, show_ima3, store_ima3),
693 __ATTR(ima4, 0600, show_ima4, store_ima4),
694 __ATTR(ima5, 0600, show_ima5, store_ima5),
695 __ATTR(ima6, 0600, show_ima6, store_ima6),
696 __ATTR(ima7, 0600, show_ima7, store_ima7),
697 __ATTR(ima8, 0600, show_ima8, store_ima8),
698 __ATTR(ima9, 0600, show_ima9, store_ima9),
699 __ATTR(imaat, 0600, show_imaat, store_imaat),
700 __ATTR(btcr, 0600, show_btcr, store_btcr),
701 __ATTR(pccr, 0600, show_pccr, store_pccr),
702 __ATTR(rpccr, 0600, show_rpccr, store_rpccr),
703 __ATTR(der, 0600, show_der, store_der),
704 __ATTR(mer, 0600, show_mer, store_mer),
705 __ATTR(ber, 0600, show_ber, store_ber),
706 __ATTR(ier, 0600, show_ier, store_ier),
707 __ATTR(sier, 0600, show_sier, store_sier),
708 __ATTR(siar, 0600, show_siar, store_siar),
709 __ATTR(tsr0, 0600, show_tsr0, store_tsr0),
710 __ATTR(tsr1, 0600, show_tsr1, store_tsr1),
711 __ATTR(tsr2, 0600, show_tsr2, store_tsr2),
712 __ATTR(tsr3, 0600, show_tsr3, store_tsr3),
Sinan Kayaefb463c2019-05-14 15:44:04 -0700713#endif /* CONFIG_DEBUG_MISC */
Olof Johansson6529c132007-01-28 21:25:57 -0600714};
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000715#endif /* HAS_PPC_PMC_PA6T */
716#endif /* HAS_PPC_PMC_CLASSIC */
Olof Johansson6529c132007-01-28 21:25:57 -0600717
Sebastian Andrzej Siewior977ab252016-11-17 19:35:37 +0100718static int register_cpu_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct cpu *c = &per_cpu(cpu_devices, cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800721 struct device *s = &c->dev;
722 struct device_attribute *attrs, *pmc_attrs;
Olof Johansson6529c132007-01-28 21:25:57 -0600723 int i, nattrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Tyrel Datwylere76ca272017-04-17 20:24:39 -0400725 /* For cpus present at boot a reference was already grabbed in register_cpu() */
726 if (!s->of_node)
727 s->of_node = of_get_cpu_node(cpu, NULL);
728
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000729#ifdef CONFIG_PPC64
Stephen Rothwellf5339272012-03-15 18:18:00 +0000730 if (cpu_has_feature(CPU_FTR_SMT))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800731 device_create_file(s, &dev_attr_smt_snooze_delay);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000732#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 /* PMC stuff */
Olof Johansson6529c132007-01-28 21:25:57 -0600735 switch (cur_cpu_spec->pmc_type) {
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000736#ifdef HAS_PPC_PMC_IBM
Olof Johansson6529c132007-01-28 21:25:57 -0600737 case PPC_PMC_IBM:
738 attrs = ibm_common_attrs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800739 nattrs = sizeof(ibm_common_attrs) / sizeof(struct device_attribute);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000740 pmc_attrs = classic_pmc_attrs;
Olof Johansson6529c132007-01-28 21:25:57 -0600741 break;
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000742#endif /* HAS_PPC_PMC_IBM */
743#ifdef HAS_PPC_PMC_G4
744 case PPC_PMC_G4:
745 attrs = g4_common_attrs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800746 nattrs = sizeof(g4_common_attrs) / sizeof(struct device_attribute);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000747 pmc_attrs = classic_pmc_attrs;
748 break;
749#endif /* HAS_PPC_PMC_G4 */
750#ifdef HAS_PPC_PMC_PA6T
Olof Johansson6529c132007-01-28 21:25:57 -0600751 case PPC_PMC_PA6T:
752 /* PA Semi starts counting at PMC0 */
753 attrs = pa6t_attrs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800754 nattrs = sizeof(pa6t_attrs) / sizeof(struct device_attribute);
Olof Johansson6529c132007-01-28 21:25:57 -0600755 pmc_attrs = NULL;
756 break;
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000757#endif /* HAS_PPC_PMC_PA6T */
Olof Johansson6529c132007-01-28 21:25:57 -0600758 default:
759 attrs = NULL;
760 nattrs = 0;
761 pmc_attrs = NULL;
762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Olof Johansson6529c132007-01-28 21:25:57 -0600764 for (i = 0; i < nattrs; i++)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800765 device_create_file(s, &attrs[i]);
Olof Johansson6529c132007-01-28 21:25:57 -0600766
767 if (pmc_attrs)
768 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800769 device_create_file(s, &pmc_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000771#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 if (cpu_has_feature(CPU_FTR_MMCRA))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800773 device_create_file(s, &dev_attr_mmcra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Madhavan Srinivasand1211af2013-10-02 00:34:10 +0530775 if (cpu_has_feature(CPU_FTR_PURR)) {
776 if (!firmware_has_feature(FW_FEATURE_LPAR))
777 add_write_permission_dev_attr(&dev_attr_purr);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800778 device_create_file(s, &dev_attr_purr);
Madhavan Srinivasand1211af2013-10-02 00:34:10 +0530779 }
Anton Blanchard4c1985572006-12-08 17:46:58 +1100780
Anton Blanchardf0509822006-12-08 17:51:13 +1100781 if (cpu_has_feature(CPU_FTR_SPURR))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800782 device_create_file(s, &dev_attr_spurr);
Anton Blanchardf0509822006-12-08 17:51:13 +1100783
Anton Blanchard4c1985572006-12-08 17:46:58 +1100784 if (cpu_has_feature(CPU_FTR_DSCR))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800785 device_create_file(s, &dev_attr_dscr);
Ananth N Mavinakayanahalli595fe912011-11-10 19:58:53 +0000786
787 if (cpu_has_feature(CPU_FTR_PPCAS_ARCH_V2))
Linus Torvalds7affca32012-01-07 12:03:30 -0800788 device_create_file(s, &dev_attr_pir);
Anton Blanchardb6d34eb2017-09-08 03:11:12 +1000789
Cyril Burc134f0d2018-02-14 14:27:06 +1100790 if (cpu_has_feature(CPU_FTR_ARCH_206) &&
791 !firmware_has_feature(FW_FEATURE_LPAR))
Anton Blanchardb6d34eb2017-09-08 03:11:12 +1000792 device_create_file(s, &dev_attr_tscr);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000793#endif /* CONFIG_PPC64 */
Nathan Lynch124c27d2008-07-27 15:24:55 +1000794
Wang Dongshenga7189482013-12-17 16:17:02 +0800795#ifdef CONFIG_PPC_FSL_BOOK3E
796 if (PVR_VER(cur_cpu_spec->pvr_value) == PVR_VER_E6500) {
797 device_create_file(s, &dev_attr_pw20_state);
798 device_create_file(s, &dev_attr_pw20_wait_time);
799
800 device_create_file(s, &dev_attr_altivec_idle);
801 device_create_file(s, &dev_attr_altivec_idle_wait_time);
802 }
803#endif
Nathan Lynch93197a32008-12-23 18:55:54 +0000804 cacheinfo_cpu_online(cpu);
Sebastian Andrzej Siewior977ab252016-11-17 19:35:37 +0100805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Michael Ellerman3f2290e2017-04-24 20:46:59 +1000808#ifdef CONFIG_HOTPLUG_CPU
Sebastian Andrzej Siewior977ab252016-11-17 19:35:37 +0100809static int unregister_cpu_online(unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
811 struct cpu *c = &per_cpu(cpu_devices, cpu);
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800812 struct device *s = &c->dev;
813 struct device_attribute *attrs, *pmc_attrs;
Olof Johansson6529c132007-01-28 21:25:57 -0600814 int i, nattrs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Siddha, Suresh B72486f12006-12-07 02:14:10 +0100816 BUG_ON(!c->hotpluggable);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Milton Millera1e0eb12008-11-16 11:44:42 +0000818#ifdef CONFIG_PPC64
Stephen Rothwellf5339272012-03-15 18:18:00 +0000819 if (cpu_has_feature(CPU_FTR_SMT))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800820 device_remove_file(s, &dev_attr_smt_snooze_delay);
Milton Millera1e0eb12008-11-16 11:44:42 +0000821#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* PMC stuff */
Olof Johansson6529c132007-01-28 21:25:57 -0600824 switch (cur_cpu_spec->pmc_type) {
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000825#ifdef HAS_PPC_PMC_IBM
Olof Johansson6529c132007-01-28 21:25:57 -0600826 case PPC_PMC_IBM:
827 attrs = ibm_common_attrs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800828 nattrs = sizeof(ibm_common_attrs) / sizeof(struct device_attribute);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000829 pmc_attrs = classic_pmc_attrs;
Olof Johansson6529c132007-01-28 21:25:57 -0600830 break;
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000831#endif /* HAS_PPC_PMC_IBM */
832#ifdef HAS_PPC_PMC_G4
833 case PPC_PMC_G4:
834 attrs = g4_common_attrs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800835 nattrs = sizeof(g4_common_attrs) / sizeof(struct device_attribute);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000836 pmc_attrs = classic_pmc_attrs;
837 break;
838#endif /* HAS_PPC_PMC_G4 */
839#ifdef HAS_PPC_PMC_PA6T
Olof Johansson6529c132007-01-28 21:25:57 -0600840 case PPC_PMC_PA6T:
841 /* PA Semi starts counting at PMC0 */
842 attrs = pa6t_attrs;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800843 nattrs = sizeof(pa6t_attrs) / sizeof(struct device_attribute);
Olof Johansson6529c132007-01-28 21:25:57 -0600844 pmc_attrs = NULL;
845 break;
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000846#endif /* HAS_PPC_PMC_PA6T */
Olof Johansson6529c132007-01-28 21:25:57 -0600847 default:
848 attrs = NULL;
849 nattrs = 0;
850 pmc_attrs = NULL;
851 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Olof Johansson6529c132007-01-28 21:25:57 -0600853 for (i = 0; i < nattrs; i++)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800854 device_remove_file(s, &attrs[i]);
Olof Johansson6529c132007-01-28 21:25:57 -0600855
856 if (pmc_attrs)
857 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800858 device_remove_file(s, &pmc_attrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000860#ifdef CONFIG_PPC64
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (cpu_has_feature(CPU_FTR_MMCRA))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800862 device_remove_file(s, &dev_attr_mmcra);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Michael Neulingafd05422006-07-28 13:58:37 +1000864 if (cpu_has_feature(CPU_FTR_PURR))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800865 device_remove_file(s, &dev_attr_purr);
Anton Blanchard4c1985572006-12-08 17:46:58 +1100866
Anton Blanchardf0509822006-12-08 17:51:13 +1100867 if (cpu_has_feature(CPU_FTR_SPURR))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800868 device_remove_file(s, &dev_attr_spurr);
Anton Blanchardf0509822006-12-08 17:51:13 +1100869
Anton Blanchard4c1985572006-12-08 17:46:58 +1100870 if (cpu_has_feature(CPU_FTR_DSCR))
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800871 device_remove_file(s, &dev_attr_dscr);
Ananth N Mavinakayanahalli595fe912011-11-10 19:58:53 +0000872
873 if (cpu_has_feature(CPU_FTR_PPCAS_ARCH_V2))
Linus Torvalds7affca32012-01-07 12:03:30 -0800874 device_remove_file(s, &dev_attr_pir);
Anton Blanchardb6d34eb2017-09-08 03:11:12 +1000875
Cyril Burc134f0d2018-02-14 14:27:06 +1100876 if (cpu_has_feature(CPU_FTR_ARCH_206) &&
877 !firmware_has_feature(FW_FEATURE_LPAR))
Anton Blanchardb6d34eb2017-09-08 03:11:12 +1000878 device_remove_file(s, &dev_attr_tscr);
Benjamin Herrenschmidtb950bdd2008-08-18 14:23:51 +1000879#endif /* CONFIG_PPC64 */
Nathan Lynch124c27d2008-07-27 15:24:55 +1000880
Wang Dongshenga7189482013-12-17 16:17:02 +0800881#ifdef CONFIG_PPC_FSL_BOOK3E
882 if (PVR_VER(cur_cpu_spec->pvr_value) == PVR_VER_E6500) {
883 device_remove_file(s, &dev_attr_pw20_state);
884 device_remove_file(s, &dev_attr_pw20_wait_time);
885
886 device_remove_file(s, &dev_attr_altivec_idle);
887 device_remove_file(s, &dev_attr_altivec_idle_wait_time);
888 }
889#endif
Nathan Lynch93197a32008-12-23 18:55:54 +0000890 cacheinfo_cpu_offline(cpu);
Tyrel Datwylere76ca272017-04-17 20:24:39 -0400891 of_node_put(s->of_node);
892 s->of_node = NULL;
Sebastian Andrzej Siewior977ab252016-11-17 19:35:37 +0100893 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
Michael Ellerman3f2290e2017-04-24 20:46:59 +1000895#else /* !CONFIG_HOTPLUG_CPU */
896#define unregister_cpu_online NULL
897#endif
Nathan Fontenot12633e82009-11-25 17:23:25 +0000898
899#ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
900ssize_t arch_cpu_probe(const char *buf, size_t count)
901{
902 if (ppc_md.cpu_probe)
903 return ppc_md.cpu_probe(buf, count);
904
905 return -EINVAL;
906}
907
908ssize_t arch_cpu_release(const char *buf, size_t count)
909{
910 if (ppc_md.cpu_release)
911 return ppc_md.cpu_release(buf, count);
912
913 return -EINVAL;
914}
915#endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
916
Christian Krafft0344c6c2006-10-24 18:31:24 +0200917static DEFINE_MUTEX(cpu_mutex);
918
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800919int cpu_add_dev_attr(struct device_attribute *attr)
Christian Krafft0344c6c2006-10-24 18:31:24 +0200920{
921 int cpu;
922
923 mutex_lock(&cpu_mutex);
924
925 for_each_possible_cpu(cpu) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800926 device_create_file(get_cpu_device(cpu), attr);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200927 }
928
929 mutex_unlock(&cpu_mutex);
930 return 0;
931}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800932EXPORT_SYMBOL_GPL(cpu_add_dev_attr);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200933
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800934int cpu_add_dev_attr_group(struct attribute_group *attrs)
Christian Krafft0344c6c2006-10-24 18:31:24 +0200935{
936 int cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800937 struct device *dev;
Olof Johansson6bcc4c02007-09-05 12:43:17 +1000938 int ret;
Christian Krafft0344c6c2006-10-24 18:31:24 +0200939
940 mutex_lock(&cpu_mutex);
941
942 for_each_possible_cpu(cpu) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800943 dev = get_cpu_device(cpu);
944 ret = sysfs_create_group(&dev->kobj, attrs);
Olof Johansson6bcc4c02007-09-05 12:43:17 +1000945 WARN_ON(ret != 0);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200946 }
947
948 mutex_unlock(&cpu_mutex);
949 return 0;
950}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800951EXPORT_SYMBOL_GPL(cpu_add_dev_attr_group);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200952
953
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800954void cpu_remove_dev_attr(struct device_attribute *attr)
Christian Krafft0344c6c2006-10-24 18:31:24 +0200955{
956 int cpu;
957
958 mutex_lock(&cpu_mutex);
959
960 for_each_possible_cpu(cpu) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800961 device_remove_file(get_cpu_device(cpu), attr);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200962 }
963
964 mutex_unlock(&cpu_mutex);
965}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800966EXPORT_SYMBOL_GPL(cpu_remove_dev_attr);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200967
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800968void cpu_remove_dev_attr_group(struct attribute_group *attrs)
Christian Krafft0344c6c2006-10-24 18:31:24 +0200969{
970 int cpu;
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800971 struct device *dev;
Christian Krafft0344c6c2006-10-24 18:31:24 +0200972
973 mutex_lock(&cpu_mutex);
974
975 for_each_possible_cpu(cpu) {
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800976 dev = get_cpu_device(cpu);
977 sysfs_remove_group(&dev->kobj, attrs);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200978 }
979
980 mutex_unlock(&cpu_mutex);
981}
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800982EXPORT_SYMBOL_GPL(cpu_remove_dev_attr_group);
Christian Krafft0344c6c2006-10-24 18:31:24 +0200983
984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985/* NUMA stuff */
986
987#ifdef CONFIG_NUMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988static void register_nodes(void)
989{
990 int i;
991
Yasunori Goto0fc44152006-06-27 02:53:38 -0700992 for (i = 0; i < MAX_NUMNODES; i++)
993 register_one_node(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994}
Jeremy Kerr953039c2006-05-01 12:16:12 -0700995
Kay Sievers8a25a2f2011-12-21 14:29:42 -0800996int sysfs_add_device_to_node(struct device *dev, int nid)
Jeremy Kerr953039c2006-05-01 12:16:12 -0700997{
Wen Congyang87327942012-12-11 16:00:56 -0800998 struct node *node = node_devices[nid];
Kay Sievers10fbcf42011-12-21 14:48:43 -0800999 return sysfs_create_link(&node->dev.kobj, &dev->kobj,
Jeremy Kerr953039c2006-05-01 12:16:12 -07001000 kobject_name(&dev->kobj));
1001}
Johannes Berg12654f72007-07-05 19:35:33 +10001002EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
Jeremy Kerr953039c2006-05-01 12:16:12 -07001003
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001004void sysfs_remove_device_from_node(struct device *dev, int nid)
Jeremy Kerr953039c2006-05-01 12:16:12 -07001005{
Wen Congyang87327942012-12-11 16:00:56 -08001006 struct node *node = node_devices[nid];
Kay Sievers10fbcf42011-12-21 14:48:43 -08001007 sysfs_remove_link(&node->dev.kobj, kobject_name(&dev->kobj));
Jeremy Kerr953039c2006-05-01 12:16:12 -07001008}
Johannes Berg12654f72007-07-05 19:35:33 +10001009EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
Jeremy Kerr953039c2006-05-01 12:16:12 -07001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011#else
1012static void register_nodes(void)
1013{
1014 return;
1015}
Jeremy Kerr953039c2006-05-01 12:16:12 -07001016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017#endif
1018
1019/* Only valid if CPU is present. */
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001020static ssize_t show_physical_id(struct device *dev,
1021 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022{
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001023 struct cpu *cpu = container_of(dev, struct cpu, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001025 return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->dev.id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001027static DEVICE_ATTR(physical_id, 0444, show_physical_id, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
1029static int __init topology_init(void)
1030{
Sebastian Andrzej Siewior977ab252016-11-17 19:35:37 +01001031 int cpu, r;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 register_nodes();
Srivatsa S. Bhatd1a55112014-03-11 02:06:31 +05301034
KAMEZAWA Hiroyuki0e551952006-03-28 14:50:51 -08001035 for_each_possible_cpu(cpu) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 struct cpu *c = &per_cpu(cpu_devices, cpu);
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /*
1039 * For now, we just see if the system supports making
1040 * the RTAS calls for CPU hotplug. But, there may be a
1041 * more comprehensive way to do this for an individual
1042 * CPU. For instance, the boot cpu might never be valid
1043 * for hotplugging.
1044 */
Siddha, Suresh B72486f12006-12-07 02:14:10 +01001045 if (ppc_md.cpu_die)
1046 c->hotpluggable = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Siddha, Suresh B72486f12006-12-07 02:14:10 +01001048 if (cpu_online(cpu) || c->hotpluggable) {
KAMEZAWA Hiroyuki76b67ed2006-06-27 02:53:41 -07001049 register_cpu(c, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050
Kay Sievers8a25a2f2011-12-21 14:29:42 -08001051 device_create_file(&c->dev, &dev_attr_physical_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
Sebastian Andrzej Siewior977ab252016-11-17 19:35:37 +01001054 r = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/topology:online",
1055 register_cpu_online, unregister_cpu_online);
1056 WARN_ON(r < 0);
Alexey Kardashevskiyefcac652011-03-02 15:18:48 +00001057#ifdef CONFIG_PPC64
1058 sysfs_create_dscr_default();
1059#endif /* CONFIG_PPC64 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060
1061 return 0;
1062}
Kevin Corrye9e77ce2007-05-03 03:11:49 +10001063subsys_initcall(topology_init);