GNU Linux-libre 4.14.266-gnu1
[releases.git] / arch / powerpc / kernel / sysfs.c
1 #include <linux/device.h>
2 #include <linux/cpu.h>
3 #include <linux/smp.h>
4 #include <linux/percpu.h>
5 #include <linux/init.h>
6 #include <linux/sched.h>
7 #include <linux/export.h>
8 #include <linux/nodemask.h>
9 #include <linux/cpumask.h>
10 #include <linux/notifier.h>
11
12 #include <asm/current.h>
13 #include <asm/processor.h>
14 #include <asm/cputable.h>
15 #include <asm/hvcall.h>
16 #include <asm/prom.h>
17 #include <asm/machdep.h>
18 #include <asm/smp.h>
19 #include <asm/pmc.h>
20 #include <asm/firmware.h>
21
22 #include "cacheinfo.h"
23
24 #ifdef CONFIG_PPC64
25 #include <asm/paca.h>
26 #include <asm/lppaca.h>
27 #endif
28
29 static DEFINE_PER_CPU(struct cpu, cpu_devices);
30
31 #ifdef CONFIG_PPC64
32
33 /*
34  * Snooze delay has not been hooked up since 3fa8cad82b94 ("powerpc/pseries/cpuidle:
35  * smt-snooze-delay cleanup.") and has been broken even longer. As was foretold in
36  * 2014:
37  *
38  *  "ppc64_util currently utilises it. Once we fix ppc64_util, propose to clean
39  *  up the kernel code."
40  *
41  * powerpc-utils stopped using it as of 1.3.8. At some point in the future this
42  * code should be removed.
43  */
44
45 static ssize_t store_smt_snooze_delay(struct device *dev,
46                                       struct device_attribute *attr,
47                                       const char *buf,
48                                       size_t count)
49 {
50         pr_warn_once("%s (%d) stored to unsupported smt_snooze_delay, which has no effect.\n",
51                      current->comm, current->pid);
52         return count;
53 }
54
55 static ssize_t show_smt_snooze_delay(struct device *dev,
56                                      struct device_attribute *attr,
57                                      char *buf)
58 {
59         pr_warn_once("%s (%d) read from unsupported smt_snooze_delay\n",
60                      current->comm, current->pid);
61         return sprintf(buf, "100\n");
62 }
63
64 static DEVICE_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
65                    store_smt_snooze_delay);
66
67 static int __init setup_smt_snooze_delay(char *str)
68 {
69         if (!cpu_has_feature(CPU_FTR_SMT))
70                 return 1;
71
72         pr_warn("smt-snooze-delay command line option has no effect\n");
73         return 1;
74 }
75 __setup("smt-snooze-delay=", setup_smt_snooze_delay);
76
77 #endif /* CONFIG_PPC64 */
78
79 #ifdef CONFIG_PPC_FSL_BOOK3E
80 #define MAX_BIT                         63
81
82 static u64 pw20_wt;
83 static u64 altivec_idle_wt;
84
85 static unsigned int get_idle_ticks_bit(u64 ns)
86 {
87         u64 cycle;
88
89         if (ns >= 10000)
90                 cycle = div_u64(ns + 500, 1000) * tb_ticks_per_usec;
91         else
92                 cycle = div_u64(ns * tb_ticks_per_usec, 1000);
93
94         if (!cycle)
95                 return 0;
96
97         return ilog2(cycle);
98 }
99
100 static void do_show_pwrmgtcr0(void *val)
101 {
102         u32 *value = val;
103
104         *value = mfspr(SPRN_PWRMGTCR0);
105 }
106
107 static ssize_t show_pw20_state(struct device *dev,
108                                 struct device_attribute *attr, char *buf)
109 {
110         u32 value;
111         unsigned int cpu = dev->id;
112
113         smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
114
115         value &= PWRMGTCR0_PW20_WAIT;
116
117         return sprintf(buf, "%u\n", value ? 1 : 0);
118 }
119
120 static void do_store_pw20_state(void *val)
121 {
122         u32 *value = val;
123         u32 pw20_state;
124
125         pw20_state = mfspr(SPRN_PWRMGTCR0);
126
127         if (*value)
128                 pw20_state |= PWRMGTCR0_PW20_WAIT;
129         else
130                 pw20_state &= ~PWRMGTCR0_PW20_WAIT;
131
132         mtspr(SPRN_PWRMGTCR0, pw20_state);
133 }
134
135 static ssize_t store_pw20_state(struct device *dev,
136                                 struct device_attribute *attr,
137                                 const char *buf, size_t count)
138 {
139         u32 value;
140         unsigned int cpu = dev->id;
141
142         if (kstrtou32(buf, 0, &value))
143                 return -EINVAL;
144
145         if (value > 1)
146                 return -EINVAL;
147
148         smp_call_function_single(cpu, do_store_pw20_state, &value, 1);
149
150         return count;
151 }
152
153 static ssize_t show_pw20_wait_time(struct device *dev,
154                                 struct device_attribute *attr, char *buf)
155 {
156         u32 value;
157         u64 tb_cycle = 1;
158         u64 time;
159
160         unsigned int cpu = dev->id;
161
162         if (!pw20_wt) {
163                 smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
164                 value = (value & PWRMGTCR0_PW20_ENT) >>
165                                         PWRMGTCR0_PW20_ENT_SHIFT;
166
167                 tb_cycle = (tb_cycle << (MAX_BIT - value + 1));
168                 /* convert ms to ns */
169                 if (tb_ticks_per_usec > 1000) {
170                         time = div_u64(tb_cycle, tb_ticks_per_usec / 1000);
171                 } else {
172                         u32 rem_us;
173
174                         time = div_u64_rem(tb_cycle, tb_ticks_per_usec,
175                                                 &rem_us);
176                         time = time * 1000 + rem_us * 1000 / tb_ticks_per_usec;
177                 }
178         } else {
179                 time = pw20_wt;
180         }
181
182         return sprintf(buf, "%llu\n", time > 0 ? time : 0);
183 }
184
185 static void set_pw20_wait_entry_bit(void *val)
186 {
187         u32 *value = val;
188         u32 pw20_idle;
189
190         pw20_idle = mfspr(SPRN_PWRMGTCR0);
191
192         /* Set Automatic PW20 Core Idle Count */
193         /* clear count */
194         pw20_idle &= ~PWRMGTCR0_PW20_ENT;
195
196         /* set count */
197         pw20_idle |= ((MAX_BIT - *value) << PWRMGTCR0_PW20_ENT_SHIFT);
198
199         mtspr(SPRN_PWRMGTCR0, pw20_idle);
200 }
201
202 static ssize_t store_pw20_wait_time(struct device *dev,
203                                 struct device_attribute *attr,
204                                 const char *buf, size_t count)
205 {
206         u32 entry_bit;
207         u64 value;
208
209         unsigned int cpu = dev->id;
210
211         if (kstrtou64(buf, 0, &value))
212                 return -EINVAL;
213
214         if (!value)
215                 return -EINVAL;
216
217         entry_bit = get_idle_ticks_bit(value);
218         if (entry_bit > MAX_BIT)
219                 return -EINVAL;
220
221         pw20_wt = value;
222
223         smp_call_function_single(cpu, set_pw20_wait_entry_bit,
224                                 &entry_bit, 1);
225
226         return count;
227 }
228
229 static ssize_t show_altivec_idle(struct device *dev,
230                                 struct device_attribute *attr, char *buf)
231 {
232         u32 value;
233         unsigned int cpu = dev->id;
234
235         smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
236
237         value &= PWRMGTCR0_AV_IDLE_PD_EN;
238
239         return sprintf(buf, "%u\n", value ? 1 : 0);
240 }
241
242 static void do_store_altivec_idle(void *val)
243 {
244         u32 *value = val;
245         u32 altivec_idle;
246
247         altivec_idle = mfspr(SPRN_PWRMGTCR0);
248
249         if (*value)
250                 altivec_idle |= PWRMGTCR0_AV_IDLE_PD_EN;
251         else
252                 altivec_idle &= ~PWRMGTCR0_AV_IDLE_PD_EN;
253
254         mtspr(SPRN_PWRMGTCR0, altivec_idle);
255 }
256
257 static ssize_t store_altivec_idle(struct device *dev,
258                                 struct device_attribute *attr,
259                                 const char *buf, size_t count)
260 {
261         u32 value;
262         unsigned int cpu = dev->id;
263
264         if (kstrtou32(buf, 0, &value))
265                 return -EINVAL;
266
267         if (value > 1)
268                 return -EINVAL;
269
270         smp_call_function_single(cpu, do_store_altivec_idle, &value, 1);
271
272         return count;
273 }
274
275 static ssize_t show_altivec_idle_wait_time(struct device *dev,
276                                 struct device_attribute *attr, char *buf)
277 {
278         u32 value;
279         u64 tb_cycle = 1;
280         u64 time;
281
282         unsigned int cpu = dev->id;
283
284         if (!altivec_idle_wt) {
285                 smp_call_function_single(cpu, do_show_pwrmgtcr0, &value, 1);
286                 value = (value & PWRMGTCR0_AV_IDLE_CNT) >>
287                                         PWRMGTCR0_AV_IDLE_CNT_SHIFT;
288
289                 tb_cycle = (tb_cycle << (MAX_BIT - value + 1));
290                 /* convert ms to ns */
291                 if (tb_ticks_per_usec > 1000) {
292                         time = div_u64(tb_cycle, tb_ticks_per_usec / 1000);
293                 } else {
294                         u32 rem_us;
295
296                         time = div_u64_rem(tb_cycle, tb_ticks_per_usec,
297                                                 &rem_us);
298                         time = time * 1000 + rem_us * 1000 / tb_ticks_per_usec;
299                 }
300         } else {
301                 time = altivec_idle_wt;
302         }
303
304         return sprintf(buf, "%llu\n", time > 0 ? time : 0);
305 }
306
307 static void set_altivec_idle_wait_entry_bit(void *val)
308 {
309         u32 *value = val;
310         u32 altivec_idle;
311
312         altivec_idle = mfspr(SPRN_PWRMGTCR0);
313
314         /* Set Automatic AltiVec Idle Count */
315         /* clear count */
316         altivec_idle &= ~PWRMGTCR0_AV_IDLE_CNT;
317
318         /* set count */
319         altivec_idle |= ((MAX_BIT - *value) << PWRMGTCR0_AV_IDLE_CNT_SHIFT);
320
321         mtspr(SPRN_PWRMGTCR0, altivec_idle);
322 }
323
324 static ssize_t store_altivec_idle_wait_time(struct device *dev,
325                                 struct device_attribute *attr,
326                                 const char *buf, size_t count)
327 {
328         u32 entry_bit;
329         u64 value;
330
331         unsigned int cpu = dev->id;
332
333         if (kstrtou64(buf, 0, &value))
334                 return -EINVAL;
335
336         if (!value)
337                 return -EINVAL;
338
339         entry_bit = get_idle_ticks_bit(value);
340         if (entry_bit > MAX_BIT)
341                 return -EINVAL;
342
343         altivec_idle_wt = value;
344
345         smp_call_function_single(cpu, set_altivec_idle_wait_entry_bit,
346                                 &entry_bit, 1);
347
348         return count;
349 }
350
351 /*
352  * Enable/Disable interface:
353  * 0, disable. 1, enable.
354  */
355 static DEVICE_ATTR(pw20_state, 0600, show_pw20_state, store_pw20_state);
356 static DEVICE_ATTR(altivec_idle, 0600, show_altivec_idle, store_altivec_idle);
357
358 /*
359  * Set wait time interface:(Nanosecond)
360  * Example: Base on TBfreq is 41MHZ.
361  * 1~48(ns): TB[63]
362  * 49~97(ns): TB[62]
363  * 98~195(ns): TB[61]
364  * 196~390(ns): TB[60]
365  * 391~780(ns): TB[59]
366  * 781~1560(ns): TB[58]
367  * ...
368  */
369 static DEVICE_ATTR(pw20_wait_time, 0600,
370                         show_pw20_wait_time,
371                         store_pw20_wait_time);
372 static DEVICE_ATTR(altivec_idle_wait_time, 0600,
373                         show_altivec_idle_wait_time,
374                         store_altivec_idle_wait_time);
375 #endif
376
377 /*
378  * Enabling PMCs will slow partition context switch times so we only do
379  * it the first time we write to the PMCs.
380  */
381
382 static DEFINE_PER_CPU(char, pmcs_enabled);
383
384 void ppc_enable_pmcs(void)
385 {
386         ppc_set_pmu_inuse(1);
387
388         /* Only need to enable them once */
389         if (__this_cpu_read(pmcs_enabled))
390                 return;
391
392         __this_cpu_write(pmcs_enabled, 1);
393
394         if (ppc_md.enable_pmcs)
395                 ppc_md.enable_pmcs();
396 }
397 EXPORT_SYMBOL(ppc_enable_pmcs);
398
399 #define __SYSFS_SPRSETUP_READ_WRITE(NAME, ADDRESS, EXTRA) \
400 static void read_##NAME(void *val) \
401 { \
402         *(unsigned long *)val = mfspr(ADDRESS); \
403 } \
404 static void write_##NAME(void *val) \
405 { \
406         EXTRA; \
407         mtspr(ADDRESS, *(unsigned long *)val);  \
408 }
409
410 #define __SYSFS_SPRSETUP_SHOW_STORE(NAME) \
411 static ssize_t show_##NAME(struct device *dev, \
412                         struct device_attribute *attr, \
413                         char *buf) \
414 { \
415         struct cpu *cpu = container_of(dev, struct cpu, dev); \
416         unsigned long val; \
417         smp_call_function_single(cpu->dev.id, read_##NAME, &val, 1);    \
418         return sprintf(buf, "%lx\n", val); \
419 } \
420 static ssize_t __used \
421         store_##NAME(struct device *dev, struct device_attribute *attr, \
422                         const char *buf, size_t count) \
423 { \
424         struct cpu *cpu = container_of(dev, struct cpu, dev); \
425         unsigned long val; \
426         int ret = sscanf(buf, "%lx", &val); \
427         if (ret != 1) \
428                 return -EINVAL; \
429         smp_call_function_single(cpu->dev.id, write_##NAME, &val, 1); \
430         return count; \
431 }
432
433 #define SYSFS_PMCSETUP(NAME, ADDRESS) \
434         __SYSFS_SPRSETUP_READ_WRITE(NAME, ADDRESS, ppc_enable_pmcs()) \
435         __SYSFS_SPRSETUP_SHOW_STORE(NAME)
436 #define SYSFS_SPRSETUP(NAME, ADDRESS) \
437         __SYSFS_SPRSETUP_READ_WRITE(NAME, ADDRESS, ) \
438         __SYSFS_SPRSETUP_SHOW_STORE(NAME)
439
440 #define SYSFS_SPRSETUP_SHOW_STORE(NAME) \
441         __SYSFS_SPRSETUP_SHOW_STORE(NAME)
442
443 /* Let's define all possible registers, we'll only hook up the ones
444  * that are implemented on the current processor
445  */
446
447 #if defined(CONFIG_PPC64)
448 #define HAS_PPC_PMC_CLASSIC     1
449 #define HAS_PPC_PMC_IBM         1
450 #define HAS_PPC_PMC_PA6T        1
451 #elif defined(CONFIG_6xx)
452 #define HAS_PPC_PMC_CLASSIC     1
453 #define HAS_PPC_PMC_IBM         1
454 #define HAS_PPC_PMC_G4          1
455 #endif
456
457
458 #ifdef HAS_PPC_PMC_CLASSIC
459 SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
460 SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
461 SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
462 SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
463 SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
464 SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
465 SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
466 SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
467
468 #ifdef HAS_PPC_PMC_G4
469 SYSFS_PMCSETUP(mmcr2, SPRN_MMCR2);
470 #endif
471
472 #ifdef CONFIG_PPC64
473 SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
474 SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
475
476 SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
477 SYSFS_SPRSETUP(purr, SPRN_PURR);
478 SYSFS_SPRSETUP(spurr, SPRN_SPURR);
479 SYSFS_SPRSETUP(pir, SPRN_PIR);
480
481 /*
482   Lets only enable read for phyp resources and
483   enable write when needed with a separate function.
484   Lets be conservative and default to pseries.
485 */
486 static DEVICE_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
487 static DEVICE_ATTR(spurr, 0400, show_spurr, NULL);
488 static DEVICE_ATTR(purr, 0400, show_purr, store_purr);
489 static DEVICE_ATTR(pir, 0400, show_pir, NULL);
490
491 /*
492  * This is the system wide DSCR register default value. Any
493  * change to this default value through the sysfs interface
494  * will update all per cpu DSCR default values across the
495  * system stored in their respective PACA structures.
496  */
497 static unsigned long dscr_default;
498
499 /**
500  * read_dscr() - Fetch the cpu specific DSCR default
501  * @val:        Returned cpu specific DSCR default value
502  *
503  * This function returns the per cpu DSCR default value
504  * for any cpu which is contained in it's PACA structure.
505  */
506 static void read_dscr(void *val)
507 {
508         *(unsigned long *)val = get_paca()->dscr_default;
509 }
510
511
512 /**
513  * write_dscr() - Update the cpu specific DSCR default
514  * @val:        New cpu specific DSCR default value to update
515  *
516  * This function updates the per cpu DSCR default value
517  * for any cpu which is contained in it's PACA structure.
518  */
519 static void write_dscr(void *val)
520 {
521         get_paca()->dscr_default = *(unsigned long *)val;
522         if (!current->thread.dscr_inherit) {
523                 current->thread.dscr = *(unsigned long *)val;
524                 mtspr(SPRN_DSCR, *(unsigned long *)val);
525         }
526 }
527
528 SYSFS_SPRSETUP_SHOW_STORE(dscr);
529 static DEVICE_ATTR(dscr, 0600, show_dscr, store_dscr);
530
531 static void add_write_permission_dev_attr(struct device_attribute *attr)
532 {
533         attr->attr.mode |= 0200;
534 }
535
536 /**
537  * show_dscr_default() - Fetch the system wide DSCR default
538  * @dev:        Device structure
539  * @attr:       Device attribute structure
540  * @buf:        Interface buffer
541  *
542  * This function returns the system wide DSCR default value.
543  */
544 static ssize_t show_dscr_default(struct device *dev,
545                 struct device_attribute *attr, char *buf)
546 {
547         return sprintf(buf, "%lx\n", dscr_default);
548 }
549
550 /**
551  * store_dscr_default() - Update the system wide DSCR default
552  * @dev:        Device structure
553  * @attr:       Device attribute structure
554  * @buf:        Interface buffer
555  * @count:      Size of the update
556  *
557  * This function updates the system wide DSCR default value.
558  */
559 static ssize_t __used store_dscr_default(struct device *dev,
560                 struct device_attribute *attr, const char *buf,
561                 size_t count)
562 {
563         unsigned long val;
564         int ret = 0;
565         
566         ret = sscanf(buf, "%lx", &val);
567         if (ret != 1)
568                 return -EINVAL;
569         dscr_default = val;
570
571         on_each_cpu(write_dscr, &val, 1);
572
573         return count;
574 }
575
576 static DEVICE_ATTR(dscr_default, 0600,
577                 show_dscr_default, store_dscr_default);
578
579 static void sysfs_create_dscr_default(void)
580 {
581         int err = 0;
582         if (cpu_has_feature(CPU_FTR_DSCR))
583                 err = device_create_file(cpu_subsys.dev_root, &dev_attr_dscr_default);
584 }
585 #endif /* CONFIG_PPC64 */
586
587 #ifdef HAS_PPC_PMC_PA6T
588 SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0);
589 SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1);
590 SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2);
591 SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3);
592 SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4);
593 SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5);
594 #ifdef CONFIG_DEBUG_KERNEL
595 SYSFS_SPRSETUP(hid0, SPRN_HID0);
596 SYSFS_SPRSETUP(hid1, SPRN_HID1);
597 SYSFS_SPRSETUP(hid4, SPRN_HID4);
598 SYSFS_SPRSETUP(hid5, SPRN_HID5);
599 SYSFS_SPRSETUP(ima0, SPRN_PA6T_IMA0);
600 SYSFS_SPRSETUP(ima1, SPRN_PA6T_IMA1);
601 SYSFS_SPRSETUP(ima2, SPRN_PA6T_IMA2);
602 SYSFS_SPRSETUP(ima3, SPRN_PA6T_IMA3);
603 SYSFS_SPRSETUP(ima4, SPRN_PA6T_IMA4);
604 SYSFS_SPRSETUP(ima5, SPRN_PA6T_IMA5);
605 SYSFS_SPRSETUP(ima6, SPRN_PA6T_IMA6);
606 SYSFS_SPRSETUP(ima7, SPRN_PA6T_IMA7);
607 SYSFS_SPRSETUP(ima8, SPRN_PA6T_IMA8);
608 SYSFS_SPRSETUP(ima9, SPRN_PA6T_IMA9);
609 SYSFS_SPRSETUP(imaat, SPRN_PA6T_IMAAT);
610 SYSFS_SPRSETUP(btcr, SPRN_PA6T_BTCR);
611 SYSFS_SPRSETUP(pccr, SPRN_PA6T_PCCR);
612 SYSFS_SPRSETUP(rpccr, SPRN_PA6T_RPCCR);
613 SYSFS_SPRSETUP(der, SPRN_PA6T_DER);
614 SYSFS_SPRSETUP(mer, SPRN_PA6T_MER);
615 SYSFS_SPRSETUP(ber, SPRN_PA6T_BER);
616 SYSFS_SPRSETUP(ier, SPRN_PA6T_IER);
617 SYSFS_SPRSETUP(sier, SPRN_PA6T_SIER);
618 SYSFS_SPRSETUP(siar, SPRN_PA6T_SIAR);
619 SYSFS_SPRSETUP(tsr0, SPRN_PA6T_TSR0);
620 SYSFS_SPRSETUP(tsr1, SPRN_PA6T_TSR1);
621 SYSFS_SPRSETUP(tsr2, SPRN_PA6T_TSR2);
622 SYSFS_SPRSETUP(tsr3, SPRN_PA6T_TSR3);
623 #endif /* CONFIG_DEBUG_KERNEL */
624 #endif /* HAS_PPC_PMC_PA6T */
625
626 #ifdef HAS_PPC_PMC_IBM
627 static struct device_attribute ibm_common_attrs[] = {
628         __ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
629         __ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
630 };
631 #endif /* HAS_PPC_PMC_G4 */
632
633 #ifdef HAS_PPC_PMC_G4
634 static struct device_attribute g4_common_attrs[] = {
635         __ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
636         __ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
637         __ATTR(mmcr2, 0600, show_mmcr2, store_mmcr2),
638 };
639 #endif /* HAS_PPC_PMC_G4 */
640
641 static struct device_attribute classic_pmc_attrs[] = {
642         __ATTR(pmc1, 0600, show_pmc1, store_pmc1),
643         __ATTR(pmc2, 0600, show_pmc2, store_pmc2),
644         __ATTR(pmc3, 0600, show_pmc3, store_pmc3),
645         __ATTR(pmc4, 0600, show_pmc4, store_pmc4),
646         __ATTR(pmc5, 0600, show_pmc5, store_pmc5),
647         __ATTR(pmc6, 0600, show_pmc6, store_pmc6),
648 #ifdef CONFIG_PPC64
649         __ATTR(pmc7, 0600, show_pmc7, store_pmc7),
650         __ATTR(pmc8, 0600, show_pmc8, store_pmc8),
651 #endif
652 };
653
654 #ifdef HAS_PPC_PMC_PA6T
655 static struct device_attribute pa6t_attrs[] = {
656         __ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
657         __ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
658         __ATTR(pmc0, 0600, show_pa6t_pmc0, store_pa6t_pmc0),
659         __ATTR(pmc1, 0600, show_pa6t_pmc1, store_pa6t_pmc1),
660         __ATTR(pmc2, 0600, show_pa6t_pmc2, store_pa6t_pmc2),
661         __ATTR(pmc3, 0600, show_pa6t_pmc3, store_pa6t_pmc3),
662         __ATTR(pmc4, 0600, show_pa6t_pmc4, store_pa6t_pmc4),
663         __ATTR(pmc5, 0600, show_pa6t_pmc5, store_pa6t_pmc5),
664 #ifdef CONFIG_DEBUG_KERNEL
665         __ATTR(hid0, 0600, show_hid0, store_hid0),
666         __ATTR(hid1, 0600, show_hid1, store_hid1),
667         __ATTR(hid4, 0600, show_hid4, store_hid4),
668         __ATTR(hid5, 0600, show_hid5, store_hid5),
669         __ATTR(ima0, 0600, show_ima0, store_ima0),
670         __ATTR(ima1, 0600, show_ima1, store_ima1),
671         __ATTR(ima2, 0600, show_ima2, store_ima2),
672         __ATTR(ima3, 0600, show_ima3, store_ima3),
673         __ATTR(ima4, 0600, show_ima4, store_ima4),
674         __ATTR(ima5, 0600, show_ima5, store_ima5),
675         __ATTR(ima6, 0600, show_ima6, store_ima6),
676         __ATTR(ima7, 0600, show_ima7, store_ima7),
677         __ATTR(ima8, 0600, show_ima8, store_ima8),
678         __ATTR(ima9, 0600, show_ima9, store_ima9),
679         __ATTR(imaat, 0600, show_imaat, store_imaat),
680         __ATTR(btcr, 0600, show_btcr, store_btcr),
681         __ATTR(pccr, 0600, show_pccr, store_pccr),
682         __ATTR(rpccr, 0600, show_rpccr, store_rpccr),
683         __ATTR(der, 0600, show_der, store_der),
684         __ATTR(mer, 0600, show_mer, store_mer),
685         __ATTR(ber, 0600, show_ber, store_ber),
686         __ATTR(ier, 0600, show_ier, store_ier),
687         __ATTR(sier, 0600, show_sier, store_sier),
688         __ATTR(siar, 0600, show_siar, store_siar),
689         __ATTR(tsr0, 0600, show_tsr0, store_tsr0),
690         __ATTR(tsr1, 0600, show_tsr1, store_tsr1),
691         __ATTR(tsr2, 0600, show_tsr2, store_tsr2),
692         __ATTR(tsr3, 0600, show_tsr3, store_tsr3),
693 #endif /* CONFIG_DEBUG_KERNEL */
694 };
695 #endif /* HAS_PPC_PMC_PA6T */
696 #endif /* HAS_PPC_PMC_CLASSIC */
697
698 static int register_cpu_online(unsigned int cpu)
699 {
700         struct cpu *c = &per_cpu(cpu_devices, cpu);
701         struct device *s = &c->dev;
702         struct device_attribute *attrs, *pmc_attrs;
703         int i, nattrs;
704
705         /* For cpus present at boot a reference was already grabbed in register_cpu() */
706         if (!s->of_node)
707                 s->of_node = of_get_cpu_node(cpu, NULL);
708
709 #ifdef CONFIG_PPC64
710         if (cpu_has_feature(CPU_FTR_SMT))
711                 device_create_file(s, &dev_attr_smt_snooze_delay);
712 #endif
713
714         /* PMC stuff */
715         switch (cur_cpu_spec->pmc_type) {
716 #ifdef HAS_PPC_PMC_IBM
717         case PPC_PMC_IBM:
718                 attrs = ibm_common_attrs;
719                 nattrs = sizeof(ibm_common_attrs) / sizeof(struct device_attribute);
720                 pmc_attrs = classic_pmc_attrs;
721                 break;
722 #endif /* HAS_PPC_PMC_IBM */
723 #ifdef HAS_PPC_PMC_G4
724         case PPC_PMC_G4:
725                 attrs = g4_common_attrs;
726                 nattrs = sizeof(g4_common_attrs) / sizeof(struct device_attribute);
727                 pmc_attrs = classic_pmc_attrs;
728                 break;
729 #endif /* HAS_PPC_PMC_G4 */
730 #ifdef HAS_PPC_PMC_PA6T
731         case PPC_PMC_PA6T:
732                 /* PA Semi starts counting at PMC0 */
733                 attrs = pa6t_attrs;
734                 nattrs = sizeof(pa6t_attrs) / sizeof(struct device_attribute);
735                 pmc_attrs = NULL;
736                 break;
737 #endif /* HAS_PPC_PMC_PA6T */
738         default:
739                 attrs = NULL;
740                 nattrs = 0;
741                 pmc_attrs = NULL;
742         }
743
744         for (i = 0; i < nattrs; i++)
745                 device_create_file(s, &attrs[i]);
746
747         if (pmc_attrs)
748                 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
749                         device_create_file(s, &pmc_attrs[i]);
750
751 #ifdef CONFIG_PPC64
752         if (cpu_has_feature(CPU_FTR_MMCRA))
753                 device_create_file(s, &dev_attr_mmcra);
754
755         if (cpu_has_feature(CPU_FTR_PURR)) {
756                 if (!firmware_has_feature(FW_FEATURE_LPAR))
757                         add_write_permission_dev_attr(&dev_attr_purr);
758                 device_create_file(s, &dev_attr_purr);
759         }
760
761         if (cpu_has_feature(CPU_FTR_SPURR))
762                 device_create_file(s, &dev_attr_spurr);
763
764         if (cpu_has_feature(CPU_FTR_DSCR))
765                 device_create_file(s, &dev_attr_dscr);
766
767         if (cpu_has_feature(CPU_FTR_PPCAS_ARCH_V2))
768                 device_create_file(s, &dev_attr_pir);
769 #endif /* CONFIG_PPC64 */
770
771 #ifdef CONFIG_PPC_FSL_BOOK3E
772         if (PVR_VER(cur_cpu_spec->pvr_value) == PVR_VER_E6500) {
773                 device_create_file(s, &dev_attr_pw20_state);
774                 device_create_file(s, &dev_attr_pw20_wait_time);
775
776                 device_create_file(s, &dev_attr_altivec_idle);
777                 device_create_file(s, &dev_attr_altivec_idle_wait_time);
778         }
779 #endif
780         cacheinfo_cpu_online(cpu);
781         return 0;
782 }
783
784 #ifdef CONFIG_HOTPLUG_CPU
785 static int unregister_cpu_online(unsigned int cpu)
786 {
787         struct cpu *c = &per_cpu(cpu_devices, cpu);
788         struct device *s = &c->dev;
789         struct device_attribute *attrs, *pmc_attrs;
790         int i, nattrs;
791
792         BUG_ON(!c->hotpluggable);
793
794 #ifdef CONFIG_PPC64
795         if (cpu_has_feature(CPU_FTR_SMT))
796                 device_remove_file(s, &dev_attr_smt_snooze_delay);
797 #endif
798
799         /* PMC stuff */
800         switch (cur_cpu_spec->pmc_type) {
801 #ifdef HAS_PPC_PMC_IBM
802         case PPC_PMC_IBM:
803                 attrs = ibm_common_attrs;
804                 nattrs = sizeof(ibm_common_attrs) / sizeof(struct device_attribute);
805                 pmc_attrs = classic_pmc_attrs;
806                 break;
807 #endif /* HAS_PPC_PMC_IBM */
808 #ifdef HAS_PPC_PMC_G4
809         case PPC_PMC_G4:
810                 attrs = g4_common_attrs;
811                 nattrs = sizeof(g4_common_attrs) / sizeof(struct device_attribute);
812                 pmc_attrs = classic_pmc_attrs;
813                 break;
814 #endif /* HAS_PPC_PMC_G4 */
815 #ifdef HAS_PPC_PMC_PA6T
816         case PPC_PMC_PA6T:
817                 /* PA Semi starts counting at PMC0 */
818                 attrs = pa6t_attrs;
819                 nattrs = sizeof(pa6t_attrs) / sizeof(struct device_attribute);
820                 pmc_attrs = NULL;
821                 break;
822 #endif /* HAS_PPC_PMC_PA6T */
823         default:
824                 attrs = NULL;
825                 nattrs = 0;
826                 pmc_attrs = NULL;
827         }
828
829         for (i = 0; i < nattrs; i++)
830                 device_remove_file(s, &attrs[i]);
831
832         if (pmc_attrs)
833                 for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
834                         device_remove_file(s, &pmc_attrs[i]);
835
836 #ifdef CONFIG_PPC64
837         if (cpu_has_feature(CPU_FTR_MMCRA))
838                 device_remove_file(s, &dev_attr_mmcra);
839
840         if (cpu_has_feature(CPU_FTR_PURR))
841                 device_remove_file(s, &dev_attr_purr);
842
843         if (cpu_has_feature(CPU_FTR_SPURR))
844                 device_remove_file(s, &dev_attr_spurr);
845
846         if (cpu_has_feature(CPU_FTR_DSCR))
847                 device_remove_file(s, &dev_attr_dscr);
848
849         if (cpu_has_feature(CPU_FTR_PPCAS_ARCH_V2))
850                 device_remove_file(s, &dev_attr_pir);
851 #endif /* CONFIG_PPC64 */
852
853 #ifdef CONFIG_PPC_FSL_BOOK3E
854         if (PVR_VER(cur_cpu_spec->pvr_value) == PVR_VER_E6500) {
855                 device_remove_file(s, &dev_attr_pw20_state);
856                 device_remove_file(s, &dev_attr_pw20_wait_time);
857
858                 device_remove_file(s, &dev_attr_altivec_idle);
859                 device_remove_file(s, &dev_attr_altivec_idle_wait_time);
860         }
861 #endif
862         cacheinfo_cpu_offline(cpu);
863         of_node_put(s->of_node);
864         s->of_node = NULL;
865         return 0;
866 }
867 #else /* !CONFIG_HOTPLUG_CPU */
868 #define unregister_cpu_online NULL
869 #endif
870
871 #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
872 ssize_t arch_cpu_probe(const char *buf, size_t count)
873 {
874         if (ppc_md.cpu_probe)
875                 return ppc_md.cpu_probe(buf, count);
876
877         return -EINVAL;
878 }
879
880 ssize_t arch_cpu_release(const char *buf, size_t count)
881 {
882         if (ppc_md.cpu_release)
883                 return ppc_md.cpu_release(buf, count);
884
885         return -EINVAL;
886 }
887 #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
888
889 static DEFINE_MUTEX(cpu_mutex);
890
891 int cpu_add_dev_attr(struct device_attribute *attr)
892 {
893         int cpu;
894
895         mutex_lock(&cpu_mutex);
896
897         for_each_possible_cpu(cpu) {
898                 device_create_file(get_cpu_device(cpu), attr);
899         }
900
901         mutex_unlock(&cpu_mutex);
902         return 0;
903 }
904 EXPORT_SYMBOL_GPL(cpu_add_dev_attr);
905
906 int cpu_add_dev_attr_group(struct attribute_group *attrs)
907 {
908         int cpu;
909         struct device *dev;
910         int ret;
911
912         mutex_lock(&cpu_mutex);
913
914         for_each_possible_cpu(cpu) {
915                 dev = get_cpu_device(cpu);
916                 ret = sysfs_create_group(&dev->kobj, attrs);
917                 WARN_ON(ret != 0);
918         }
919
920         mutex_unlock(&cpu_mutex);
921         return 0;
922 }
923 EXPORT_SYMBOL_GPL(cpu_add_dev_attr_group);
924
925
926 void cpu_remove_dev_attr(struct device_attribute *attr)
927 {
928         int cpu;
929
930         mutex_lock(&cpu_mutex);
931
932         for_each_possible_cpu(cpu) {
933                 device_remove_file(get_cpu_device(cpu), attr);
934         }
935
936         mutex_unlock(&cpu_mutex);
937 }
938 EXPORT_SYMBOL_GPL(cpu_remove_dev_attr);
939
940 void cpu_remove_dev_attr_group(struct attribute_group *attrs)
941 {
942         int cpu;
943         struct device *dev;
944
945         mutex_lock(&cpu_mutex);
946
947         for_each_possible_cpu(cpu) {
948                 dev = get_cpu_device(cpu);
949                 sysfs_remove_group(&dev->kobj, attrs);
950         }
951
952         mutex_unlock(&cpu_mutex);
953 }
954 EXPORT_SYMBOL_GPL(cpu_remove_dev_attr_group);
955
956
957 /* NUMA stuff */
958
959 #ifdef CONFIG_NUMA
960 static void register_nodes(void)
961 {
962         int i;
963
964         for (i = 0; i < MAX_NUMNODES; i++)
965                 register_one_node(i);
966 }
967
968 int sysfs_add_device_to_node(struct device *dev, int nid)
969 {
970         struct node *node = node_devices[nid];
971         return sysfs_create_link(&node->dev.kobj, &dev->kobj,
972                         kobject_name(&dev->kobj));
973 }
974 EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
975
976 void sysfs_remove_device_from_node(struct device *dev, int nid)
977 {
978         struct node *node = node_devices[nid];
979         sysfs_remove_link(&node->dev.kobj, kobject_name(&dev->kobj));
980 }
981 EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
982
983 #else
984 static void register_nodes(void)
985 {
986         return;
987 }
988
989 #endif
990
991 /* Only valid if CPU is present. */
992 static ssize_t show_physical_id(struct device *dev,
993                                 struct device_attribute *attr, char *buf)
994 {
995         struct cpu *cpu = container_of(dev, struct cpu, dev);
996
997         return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->dev.id));
998 }
999 static DEVICE_ATTR(physical_id, 0444, show_physical_id, NULL);
1000
1001 static int __init topology_init(void)
1002 {
1003         int cpu, r;
1004
1005         register_nodes();
1006
1007         for_each_possible_cpu(cpu) {
1008                 struct cpu *c = &per_cpu(cpu_devices, cpu);
1009
1010                 /*
1011                  * For now, we just see if the system supports making
1012                  * the RTAS calls for CPU hotplug.  But, there may be a
1013                  * more comprehensive way to do this for an individual
1014                  * CPU.  For instance, the boot cpu might never be valid
1015                  * for hotplugging.
1016                  */
1017                 if (ppc_md.cpu_die)
1018                         c->hotpluggable = 1;
1019
1020                 if (cpu_online(cpu) || c->hotpluggable) {
1021                         register_cpu(c, cpu);
1022
1023                         device_create_file(&c->dev, &dev_attr_physical_id);
1024                 }
1025         }
1026         r = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "powerpc/topology:online",
1027                               register_cpu_online, unregister_cpu_online);
1028         WARN_ON(r < 0);
1029 #ifdef CONFIG_PPC64
1030         sysfs_create_dscr_default();
1031 #endif /* CONFIG_PPC64 */
1032
1033         return 0;
1034 }
1035 subsys_initcall(topology_init);