sysfs.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. #include <linux/sysdev.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/module.h>
  8. #include <linux/nodemask.h>
  9. #include <linux/cpumask.h>
  10. #include <linux/notifier.h>
  11. #include <asm/current.h>
  12. #include <asm/processor.h>
  13. #include <asm/cputable.h>
  14. #include <asm/firmware.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 "cacheinfo.h"
  21. #ifdef CONFIG_PPC64
  22. #include <asm/paca.h>
  23. #include <asm/lppaca.h>
  24. #endif
  25. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  26. /*
  27. * SMT snooze delay stuff, 64-bit only for now
  28. */
  29. #ifdef CONFIG_PPC64
  30. /* Time in microseconds we delay before sleeping in the idle loop */
  31. DEFINE_PER_CPU(long, smt_snooze_delay) = { 100 };
  32. static ssize_t store_smt_snooze_delay(struct sys_device *dev,
  33. struct sysdev_attribute *attr,
  34. const char *buf,
  35. size_t count)
  36. {
  37. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  38. ssize_t ret;
  39. long snooze;
  40. ret = sscanf(buf, "%ld", &snooze);
  41. if (ret != 1)
  42. return -EINVAL;
  43. per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
  44. return count;
  45. }
  46. static ssize_t show_smt_snooze_delay(struct sys_device *dev,
  47. struct sysdev_attribute *attr,
  48. char *buf)
  49. {
  50. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  51. return sprintf(buf, "%ld\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
  52. }
  53. static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
  54. store_smt_snooze_delay);
  55. static int __init setup_smt_snooze_delay(char *str)
  56. {
  57. unsigned int cpu;
  58. long snooze;
  59. if (!cpu_has_feature(CPU_FTR_SMT))
  60. return 1;
  61. snooze = simple_strtol(str, NULL, 10);
  62. for_each_possible_cpu(cpu)
  63. per_cpu(smt_snooze_delay, cpu) = snooze;
  64. return 1;
  65. }
  66. __setup("smt-snooze-delay=", setup_smt_snooze_delay);
  67. #endif /* CONFIG_PPC64 */
  68. /*
  69. * Enabling PMCs will slow partition context switch times so we only do
  70. * it the first time we write to the PMCs.
  71. */
  72. static DEFINE_PER_CPU(char, pmcs_enabled);
  73. void ppc_enable_pmcs(void)
  74. {
  75. ppc_set_pmu_inuse(1);
  76. /* Only need to enable them once */
  77. if (__get_cpu_var(pmcs_enabled))
  78. return;
  79. __get_cpu_var(pmcs_enabled) = 1;
  80. if (ppc_md.enable_pmcs)
  81. ppc_md.enable_pmcs();
  82. }
  83. EXPORT_SYMBOL(ppc_enable_pmcs);
  84. #define SYSFS_PMCSETUP(NAME, ADDRESS) \
  85. static void read_##NAME(void *val) \
  86. { \
  87. *(unsigned long *)val = mfspr(ADDRESS); \
  88. } \
  89. static void write_##NAME(void *val) \
  90. { \
  91. ppc_enable_pmcs(); \
  92. mtspr(ADDRESS, *(unsigned long *)val); \
  93. } \
  94. static ssize_t show_##NAME(struct sys_device *dev, \
  95. struct sysdev_attribute *attr, \
  96. char *buf) \
  97. { \
  98. struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
  99. unsigned long val; \
  100. smp_call_function_single(cpu->sysdev.id, read_##NAME, &val, 1); \
  101. return sprintf(buf, "%lx\n", val); \
  102. } \
  103. static ssize_t __used \
  104. store_##NAME(struct sys_device *dev, struct sysdev_attribute *attr, \
  105. const char *buf, size_t count) \
  106. { \
  107. struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
  108. unsigned long val; \
  109. int ret = sscanf(buf, "%lx", &val); \
  110. if (ret != 1) \
  111. return -EINVAL; \
  112. smp_call_function_single(cpu->sysdev.id, write_##NAME, &val, 1); \
  113. return count; \
  114. }
  115. /* Let's define all possible registers, we'll only hook up the ones
  116. * that are implemented on the current processor
  117. */
  118. #if defined(CONFIG_PPC64)
  119. #define HAS_PPC_PMC_CLASSIC 1
  120. #define HAS_PPC_PMC_IBM 1
  121. #define HAS_PPC_PMC_PA6T 1
  122. #elif defined(CONFIG_6xx)
  123. #define HAS_PPC_PMC_CLASSIC 1
  124. #define HAS_PPC_PMC_IBM 1
  125. #define HAS_PPC_PMC_G4 1
  126. #endif
  127. #ifdef HAS_PPC_PMC_CLASSIC
  128. SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
  129. SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
  130. SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
  131. SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
  132. SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
  133. SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
  134. SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
  135. SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
  136. #ifdef HAS_PPC_PMC_G4
  137. SYSFS_PMCSETUP(mmcr2, SPRN_MMCR2);
  138. #endif
  139. #ifdef CONFIG_PPC64
  140. SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
  141. SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
  142. SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
  143. SYSFS_PMCSETUP(purr, SPRN_PURR);
  144. SYSFS_PMCSETUP(spurr, SPRN_SPURR);
  145. SYSFS_PMCSETUP(dscr, SPRN_DSCR);
  146. static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
  147. static SYSDEV_ATTR(spurr, 0600, show_spurr, NULL);
  148. static SYSDEV_ATTR(dscr, 0600, show_dscr, store_dscr);
  149. static SYSDEV_ATTR(purr, 0600, show_purr, store_purr);
  150. #endif /* CONFIG_PPC64 */
  151. #ifdef HAS_PPC_PMC_PA6T
  152. SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0);
  153. SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1);
  154. SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2);
  155. SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3);
  156. SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4);
  157. SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5);
  158. #ifdef CONFIG_DEBUG_KERNEL
  159. SYSFS_PMCSETUP(hid0, SPRN_HID0);
  160. SYSFS_PMCSETUP(hid1, SPRN_HID1);
  161. SYSFS_PMCSETUP(hid4, SPRN_HID4);
  162. SYSFS_PMCSETUP(hid5, SPRN_HID5);
  163. SYSFS_PMCSETUP(ima0, SPRN_PA6T_IMA0);
  164. SYSFS_PMCSETUP(ima1, SPRN_PA6T_IMA1);
  165. SYSFS_PMCSETUP(ima2, SPRN_PA6T_IMA2);
  166. SYSFS_PMCSETUP(ima3, SPRN_PA6T_IMA3);
  167. SYSFS_PMCSETUP(ima4, SPRN_PA6T_IMA4);
  168. SYSFS_PMCSETUP(ima5, SPRN_PA6T_IMA5);
  169. SYSFS_PMCSETUP(ima6, SPRN_PA6T_IMA6);
  170. SYSFS_PMCSETUP(ima7, SPRN_PA6T_IMA7);
  171. SYSFS_PMCSETUP(ima8, SPRN_PA6T_IMA8);
  172. SYSFS_PMCSETUP(ima9, SPRN_PA6T_IMA9);
  173. SYSFS_PMCSETUP(imaat, SPRN_PA6T_IMAAT);
  174. SYSFS_PMCSETUP(btcr, SPRN_PA6T_BTCR);
  175. SYSFS_PMCSETUP(pccr, SPRN_PA6T_PCCR);
  176. SYSFS_PMCSETUP(rpccr, SPRN_PA6T_RPCCR);
  177. SYSFS_PMCSETUP(der, SPRN_PA6T_DER);
  178. SYSFS_PMCSETUP(mer, SPRN_PA6T_MER);
  179. SYSFS_PMCSETUP(ber, SPRN_PA6T_BER);
  180. SYSFS_PMCSETUP(ier, SPRN_PA6T_IER);
  181. SYSFS_PMCSETUP(sier, SPRN_PA6T_SIER);
  182. SYSFS_PMCSETUP(siar, SPRN_PA6T_SIAR);
  183. SYSFS_PMCSETUP(tsr0, SPRN_PA6T_TSR0);
  184. SYSFS_PMCSETUP(tsr1, SPRN_PA6T_TSR1);
  185. SYSFS_PMCSETUP(tsr2, SPRN_PA6T_TSR2);
  186. SYSFS_PMCSETUP(tsr3, SPRN_PA6T_TSR3);
  187. #endif /* CONFIG_DEBUG_KERNEL */
  188. #endif /* HAS_PPC_PMC_PA6T */
  189. #ifdef HAS_PPC_PMC_IBM
  190. static struct sysdev_attribute ibm_common_attrs[] = {
  191. _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
  192. _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
  193. };
  194. #endif /* HAS_PPC_PMC_G4 */
  195. #ifdef HAS_PPC_PMC_G4
  196. static struct sysdev_attribute g4_common_attrs[] = {
  197. _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
  198. _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
  199. _SYSDEV_ATTR(mmcr2, 0600, show_mmcr2, store_mmcr2),
  200. };
  201. #endif /* HAS_PPC_PMC_G4 */
  202. static struct sysdev_attribute classic_pmc_attrs[] = {
  203. _SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1),
  204. _SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2),
  205. _SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3),
  206. _SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4),
  207. _SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5),
  208. _SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6),
  209. #ifdef CONFIG_PPC64
  210. _SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7),
  211. _SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8),
  212. #endif
  213. };
  214. #ifdef HAS_PPC_PMC_PA6T
  215. static struct sysdev_attribute pa6t_attrs[] = {
  216. _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
  217. _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
  218. _SYSDEV_ATTR(pmc0, 0600, show_pa6t_pmc0, store_pa6t_pmc0),
  219. _SYSDEV_ATTR(pmc1, 0600, show_pa6t_pmc1, store_pa6t_pmc1),
  220. _SYSDEV_ATTR(pmc2, 0600, show_pa6t_pmc2, store_pa6t_pmc2),
  221. _SYSDEV_ATTR(pmc3, 0600, show_pa6t_pmc3, store_pa6t_pmc3),
  222. _SYSDEV_ATTR(pmc4, 0600, show_pa6t_pmc4, store_pa6t_pmc4),
  223. _SYSDEV_ATTR(pmc5, 0600, show_pa6t_pmc5, store_pa6t_pmc5),
  224. #ifdef CONFIG_DEBUG_KERNEL
  225. _SYSDEV_ATTR(hid0, 0600, show_hid0, store_hid0),
  226. _SYSDEV_ATTR(hid1, 0600, show_hid1, store_hid1),
  227. _SYSDEV_ATTR(hid4, 0600, show_hid4, store_hid4),
  228. _SYSDEV_ATTR(hid5, 0600, show_hid5, store_hid5),
  229. _SYSDEV_ATTR(ima0, 0600, show_ima0, store_ima0),
  230. _SYSDEV_ATTR(ima1, 0600, show_ima1, store_ima1),
  231. _SYSDEV_ATTR(ima2, 0600, show_ima2, store_ima2),
  232. _SYSDEV_ATTR(ima3, 0600, show_ima3, store_ima3),
  233. _SYSDEV_ATTR(ima4, 0600, show_ima4, store_ima4),
  234. _SYSDEV_ATTR(ima5, 0600, show_ima5, store_ima5),
  235. _SYSDEV_ATTR(ima6, 0600, show_ima6, store_ima6),
  236. _SYSDEV_ATTR(ima7, 0600, show_ima7, store_ima7),
  237. _SYSDEV_ATTR(ima8, 0600, show_ima8, store_ima8),
  238. _SYSDEV_ATTR(ima9, 0600, show_ima9, store_ima9),
  239. _SYSDEV_ATTR(imaat, 0600, show_imaat, store_imaat),
  240. _SYSDEV_ATTR(btcr, 0600, show_btcr, store_btcr),
  241. _SYSDEV_ATTR(pccr, 0600, show_pccr, store_pccr),
  242. _SYSDEV_ATTR(rpccr, 0600, show_rpccr, store_rpccr),
  243. _SYSDEV_ATTR(der, 0600, show_der, store_der),
  244. _SYSDEV_ATTR(mer, 0600, show_mer, store_mer),
  245. _SYSDEV_ATTR(ber, 0600, show_ber, store_ber),
  246. _SYSDEV_ATTR(ier, 0600, show_ier, store_ier),
  247. _SYSDEV_ATTR(sier, 0600, show_sier, store_sier),
  248. _SYSDEV_ATTR(siar, 0600, show_siar, store_siar),
  249. _SYSDEV_ATTR(tsr0, 0600, show_tsr0, store_tsr0),
  250. _SYSDEV_ATTR(tsr1, 0600, show_tsr1, store_tsr1),
  251. _SYSDEV_ATTR(tsr2, 0600, show_tsr2, store_tsr2),
  252. _SYSDEV_ATTR(tsr3, 0600, show_tsr3, store_tsr3),
  253. #endif /* CONFIG_DEBUG_KERNEL */
  254. };
  255. #endif /* HAS_PPC_PMC_PA6T */
  256. #endif /* HAS_PPC_PMC_CLASSIC */
  257. static void __cpuinit register_cpu_online(unsigned int cpu)
  258. {
  259. struct cpu *c = &per_cpu(cpu_devices, cpu);
  260. struct sys_device *s = &c->sysdev;
  261. struct sysdev_attribute *attrs, *pmc_attrs;
  262. int i, nattrs;
  263. #ifdef CONFIG_PPC64
  264. if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
  265. cpu_has_feature(CPU_FTR_SMT))
  266. sysdev_create_file(s, &attr_smt_snooze_delay);
  267. #endif
  268. /* PMC stuff */
  269. switch (cur_cpu_spec->pmc_type) {
  270. #ifdef HAS_PPC_PMC_IBM
  271. case PPC_PMC_IBM:
  272. attrs = ibm_common_attrs;
  273. nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
  274. pmc_attrs = classic_pmc_attrs;
  275. break;
  276. #endif /* HAS_PPC_PMC_IBM */
  277. #ifdef HAS_PPC_PMC_G4
  278. case PPC_PMC_G4:
  279. attrs = g4_common_attrs;
  280. nattrs = sizeof(g4_common_attrs) / sizeof(struct sysdev_attribute);
  281. pmc_attrs = classic_pmc_attrs;
  282. break;
  283. #endif /* HAS_PPC_PMC_G4 */
  284. #ifdef HAS_PPC_PMC_PA6T
  285. case PPC_PMC_PA6T:
  286. /* PA Semi starts counting at PMC0 */
  287. attrs = pa6t_attrs;
  288. nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
  289. pmc_attrs = NULL;
  290. break;
  291. #endif /* HAS_PPC_PMC_PA6T */
  292. default:
  293. attrs = NULL;
  294. nattrs = 0;
  295. pmc_attrs = NULL;
  296. }
  297. for (i = 0; i < nattrs; i++)
  298. sysdev_create_file(s, &attrs[i]);
  299. if (pmc_attrs)
  300. for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
  301. sysdev_create_file(s, &pmc_attrs[i]);
  302. #ifdef CONFIG_PPC64
  303. if (cpu_has_feature(CPU_FTR_MMCRA))
  304. sysdev_create_file(s, &attr_mmcra);
  305. if (cpu_has_feature(CPU_FTR_PURR))
  306. sysdev_create_file(s, &attr_purr);
  307. if (cpu_has_feature(CPU_FTR_SPURR))
  308. sysdev_create_file(s, &attr_spurr);
  309. if (cpu_has_feature(CPU_FTR_DSCR))
  310. sysdev_create_file(s, &attr_dscr);
  311. #endif /* CONFIG_PPC64 */
  312. cacheinfo_cpu_online(cpu);
  313. }
  314. #ifdef CONFIG_HOTPLUG_CPU
  315. static void unregister_cpu_online(unsigned int cpu)
  316. {
  317. struct cpu *c = &per_cpu(cpu_devices, cpu);
  318. struct sys_device *s = &c->sysdev;
  319. struct sysdev_attribute *attrs, *pmc_attrs;
  320. int i, nattrs;
  321. BUG_ON(!c->hotpluggable);
  322. #ifdef CONFIG_PPC64
  323. if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
  324. cpu_has_feature(CPU_FTR_SMT))
  325. sysdev_remove_file(s, &attr_smt_snooze_delay);
  326. #endif
  327. /* PMC stuff */
  328. switch (cur_cpu_spec->pmc_type) {
  329. #ifdef HAS_PPC_PMC_IBM
  330. case PPC_PMC_IBM:
  331. attrs = ibm_common_attrs;
  332. nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
  333. pmc_attrs = classic_pmc_attrs;
  334. break;
  335. #endif /* HAS_PPC_PMC_IBM */
  336. #ifdef HAS_PPC_PMC_G4
  337. case PPC_PMC_G4:
  338. attrs = g4_common_attrs;
  339. nattrs = sizeof(g4_common_attrs) / sizeof(struct sysdev_attribute);
  340. pmc_attrs = classic_pmc_attrs;
  341. break;
  342. #endif /* HAS_PPC_PMC_G4 */
  343. #ifdef HAS_PPC_PMC_PA6T
  344. case PPC_PMC_PA6T:
  345. /* PA Semi starts counting at PMC0 */
  346. attrs = pa6t_attrs;
  347. nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
  348. pmc_attrs = NULL;
  349. break;
  350. #endif /* HAS_PPC_PMC_PA6T */
  351. default:
  352. attrs = NULL;
  353. nattrs = 0;
  354. pmc_attrs = NULL;
  355. }
  356. for (i = 0; i < nattrs; i++)
  357. sysdev_remove_file(s, &attrs[i]);
  358. if (pmc_attrs)
  359. for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
  360. sysdev_remove_file(s, &pmc_attrs[i]);
  361. #ifdef CONFIG_PPC64
  362. if (cpu_has_feature(CPU_FTR_MMCRA))
  363. sysdev_remove_file(s, &attr_mmcra);
  364. if (cpu_has_feature(CPU_FTR_PURR))
  365. sysdev_remove_file(s, &attr_purr);
  366. if (cpu_has_feature(CPU_FTR_SPURR))
  367. sysdev_remove_file(s, &attr_spurr);
  368. if (cpu_has_feature(CPU_FTR_DSCR))
  369. sysdev_remove_file(s, &attr_dscr);
  370. #endif /* CONFIG_PPC64 */
  371. cacheinfo_cpu_offline(cpu);
  372. }
  373. #ifdef CONFIG_ARCH_CPU_PROBE_RELEASE
  374. ssize_t arch_cpu_probe(const char *buf, size_t count)
  375. {
  376. if (ppc_md.cpu_probe)
  377. return ppc_md.cpu_probe(buf, count);
  378. return -EINVAL;
  379. }
  380. ssize_t arch_cpu_release(const char *buf, size_t count)
  381. {
  382. if (ppc_md.cpu_release)
  383. return ppc_md.cpu_release(buf, count);
  384. return -EINVAL;
  385. }
  386. #endif /* CONFIG_ARCH_CPU_PROBE_RELEASE */
  387. #endif /* CONFIG_HOTPLUG_CPU */
  388. static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
  389. unsigned long action, void *hcpu)
  390. {
  391. unsigned int cpu = (unsigned int)(long)hcpu;
  392. switch (action) {
  393. case CPU_ONLINE:
  394. case CPU_ONLINE_FROZEN:
  395. register_cpu_online(cpu);
  396. break;
  397. #ifdef CONFIG_HOTPLUG_CPU
  398. case CPU_DEAD:
  399. case CPU_DEAD_FROZEN:
  400. unregister_cpu_online(cpu);
  401. break;
  402. #endif
  403. }
  404. return NOTIFY_OK;
  405. }
  406. static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
  407. .notifier_call = sysfs_cpu_notify,
  408. };
  409. static DEFINE_MUTEX(cpu_mutex);
  410. int cpu_add_sysdev_attr(struct sysdev_attribute *attr)
  411. {
  412. int cpu;
  413. mutex_lock(&cpu_mutex);
  414. for_each_possible_cpu(cpu) {
  415. sysdev_create_file(get_cpu_sysdev(cpu), attr);
  416. }
  417. mutex_unlock(&cpu_mutex);
  418. return 0;
  419. }
  420. EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr);
  421. int cpu_add_sysdev_attr_group(struct attribute_group *attrs)
  422. {
  423. int cpu;
  424. struct sys_device *sysdev;
  425. int ret;
  426. mutex_lock(&cpu_mutex);
  427. for_each_possible_cpu(cpu) {
  428. sysdev = get_cpu_sysdev(cpu);
  429. ret = sysfs_create_group(&sysdev->kobj, attrs);
  430. WARN_ON(ret != 0);
  431. }
  432. mutex_unlock(&cpu_mutex);
  433. return 0;
  434. }
  435. EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr_group);
  436. void cpu_remove_sysdev_attr(struct sysdev_attribute *attr)
  437. {
  438. int cpu;
  439. mutex_lock(&cpu_mutex);
  440. for_each_possible_cpu(cpu) {
  441. sysdev_remove_file(get_cpu_sysdev(cpu), attr);
  442. }
  443. mutex_unlock(&cpu_mutex);
  444. }
  445. EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr);
  446. void cpu_remove_sysdev_attr_group(struct attribute_group *attrs)
  447. {
  448. int cpu;
  449. struct sys_device *sysdev;
  450. mutex_lock(&cpu_mutex);
  451. for_each_possible_cpu(cpu) {
  452. sysdev = get_cpu_sysdev(cpu);
  453. sysfs_remove_group(&sysdev->kobj, attrs);
  454. }
  455. mutex_unlock(&cpu_mutex);
  456. }
  457. EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr_group);
  458. /* NUMA stuff */
  459. #ifdef CONFIG_NUMA
  460. static void register_nodes(void)
  461. {
  462. int i;
  463. for (i = 0; i < MAX_NUMNODES; i++)
  464. register_one_node(i);
  465. }
  466. int sysfs_add_device_to_node(struct sys_device *dev, int nid)
  467. {
  468. struct node *node = &node_devices[nid];
  469. return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
  470. kobject_name(&dev->kobj));
  471. }
  472. EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
  473. void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
  474. {
  475. struct node *node = &node_devices[nid];
  476. sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
  477. }
  478. EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
  479. #else
  480. static void register_nodes(void)
  481. {
  482. return;
  483. }
  484. #endif
  485. /* Only valid if CPU is present. */
  486. static ssize_t show_physical_id(struct sys_device *dev,
  487. struct sysdev_attribute *attr, char *buf)
  488. {
  489. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  490. return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
  491. }
  492. static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
  493. static int __init topology_init(void)
  494. {
  495. int cpu;
  496. register_nodes();
  497. register_cpu_notifier(&sysfs_cpu_nb);
  498. for_each_possible_cpu(cpu) {
  499. struct cpu *c = &per_cpu(cpu_devices, cpu);
  500. /*
  501. * For now, we just see if the system supports making
  502. * the RTAS calls for CPU hotplug. But, there may be a
  503. * more comprehensive way to do this for an individual
  504. * CPU. For instance, the boot cpu might never be valid
  505. * for hotplugging.
  506. */
  507. if (ppc_md.cpu_die)
  508. c->hotpluggable = 1;
  509. if (cpu_online(cpu) || c->hotpluggable) {
  510. register_cpu(c, cpu);
  511. sysdev_create_file(&c->sysdev, &attr_physical_id);
  512. }
  513. if (cpu_online(cpu))
  514. register_cpu_online(cpu);
  515. }
  516. return 0;
  517. }
  518. subsys_initcall(topology_init);