sysfs.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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/paca.h>
  18. #include <asm/lppaca.h>
  19. #include <asm/machdep.h>
  20. #include <asm/smp.h>
  21. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  22. /* SMT stuff */
  23. #ifdef CONFIG_PPC_MULTIPLATFORM
  24. /* Time in microseconds we delay before sleeping in the idle loop */
  25. DEFINE_PER_CPU(unsigned long, smt_snooze_delay) = { 100 };
  26. static ssize_t store_smt_snooze_delay(struct sys_device *dev, const char *buf,
  27. size_t count)
  28. {
  29. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  30. ssize_t ret;
  31. unsigned long snooze;
  32. ret = sscanf(buf, "%lu", &snooze);
  33. if (ret != 1)
  34. return -EINVAL;
  35. per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
  36. return count;
  37. }
  38. static ssize_t show_smt_snooze_delay(struct sys_device *dev, char *buf)
  39. {
  40. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  41. return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
  42. }
  43. static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
  44. store_smt_snooze_delay);
  45. /* Only parse OF options if the matching cmdline option was not specified */
  46. static int smt_snooze_cmdline;
  47. static int __init smt_setup(void)
  48. {
  49. struct device_node *options;
  50. const unsigned int *val;
  51. unsigned int cpu;
  52. if (!cpu_has_feature(CPU_FTR_SMT))
  53. return -ENODEV;
  54. options = of_find_node_by_path("/options");
  55. if (!options)
  56. return -ENODEV;
  57. val = of_get_property(options, "ibm,smt-snooze-delay", NULL);
  58. if (!smt_snooze_cmdline && val) {
  59. for_each_possible_cpu(cpu)
  60. per_cpu(smt_snooze_delay, cpu) = *val;
  61. }
  62. of_node_put(options);
  63. return 0;
  64. }
  65. __initcall(smt_setup);
  66. static int __init setup_smt_snooze_delay(char *str)
  67. {
  68. unsigned int cpu;
  69. int snooze;
  70. if (!cpu_has_feature(CPU_FTR_SMT))
  71. return 1;
  72. smt_snooze_cmdline = 1;
  73. if (get_option(&str, &snooze)) {
  74. for_each_possible_cpu(cpu)
  75. per_cpu(smt_snooze_delay, cpu) = snooze;
  76. }
  77. return 1;
  78. }
  79. __setup("smt-snooze-delay=", setup_smt_snooze_delay);
  80. #endif /* CONFIG_PPC_MULTIPLATFORM */
  81. /*
  82. * Enabling PMCs will slow partition context switch times so we only do
  83. * it the first time we write to the PMCs.
  84. */
  85. static DEFINE_PER_CPU(char, pmcs_enabled);
  86. void ppc64_enable_pmcs(void)
  87. {
  88. /* Only need to enable them once */
  89. if (__get_cpu_var(pmcs_enabled))
  90. return;
  91. __get_cpu_var(pmcs_enabled) = 1;
  92. if (ppc_md.enable_pmcs)
  93. ppc_md.enable_pmcs();
  94. }
  95. EXPORT_SYMBOL(ppc64_enable_pmcs);
  96. /* XXX convert to rusty's on_one_cpu */
  97. static unsigned long run_on_cpu(unsigned long cpu,
  98. unsigned long (*func)(unsigned long),
  99. unsigned long arg)
  100. {
  101. cpumask_t old_affinity = current->cpus_allowed;
  102. unsigned long ret;
  103. /* should return -EINVAL to userspace */
  104. if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
  105. return 0;
  106. ret = func(arg);
  107. set_cpus_allowed(current, old_affinity);
  108. return ret;
  109. }
  110. #define SYSFS_PMCSETUP(NAME, ADDRESS) \
  111. static unsigned long read_##NAME(unsigned long junk) \
  112. { \
  113. return mfspr(ADDRESS); \
  114. } \
  115. static unsigned long write_##NAME(unsigned long val) \
  116. { \
  117. ppc64_enable_pmcs(); \
  118. mtspr(ADDRESS, val); \
  119. return 0; \
  120. } \
  121. static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
  122. { \
  123. struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
  124. unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
  125. return sprintf(buf, "%lx\n", val); \
  126. } \
  127. static ssize_t __attribute_used__ \
  128. store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
  129. { \
  130. struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
  131. unsigned long val; \
  132. int ret = sscanf(buf, "%lx", &val); \
  133. if (ret != 1) \
  134. return -EINVAL; \
  135. run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
  136. return count; \
  137. }
  138. /* Let's define all possible registers, we'll only hook up the ones
  139. * that are implemented on the current processor
  140. */
  141. SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
  142. SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
  143. SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
  144. SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
  145. SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
  146. SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
  147. SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
  148. SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
  149. SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
  150. SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
  151. SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
  152. SYSFS_PMCSETUP(purr, SPRN_PURR);
  153. SYSFS_PMCSETUP(spurr, SPRN_SPURR);
  154. SYSFS_PMCSETUP(dscr, SPRN_DSCR);
  155. SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0);
  156. SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1);
  157. SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2);
  158. SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3);
  159. SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4);
  160. SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5);
  161. static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
  162. static SYSDEV_ATTR(spurr, 0600, show_spurr, NULL);
  163. static SYSDEV_ATTR(dscr, 0600, show_dscr, store_dscr);
  164. static SYSDEV_ATTR(purr, 0600, show_purr, store_purr);
  165. static struct sysdev_attribute ibm_common_attrs[] = {
  166. _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
  167. _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
  168. };
  169. static struct sysdev_attribute ibm_pmc_attrs[] = {
  170. _SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1),
  171. _SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2),
  172. _SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3),
  173. _SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4),
  174. _SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5),
  175. _SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6),
  176. _SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7),
  177. _SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8),
  178. };
  179. static struct sysdev_attribute pa6t_attrs[] = {
  180. _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
  181. _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
  182. _SYSDEV_ATTR(pmc0, 0600, show_pa6t_pmc0, store_pa6t_pmc0),
  183. _SYSDEV_ATTR(pmc1, 0600, show_pa6t_pmc1, store_pa6t_pmc1),
  184. _SYSDEV_ATTR(pmc2, 0600, show_pa6t_pmc2, store_pa6t_pmc2),
  185. _SYSDEV_ATTR(pmc3, 0600, show_pa6t_pmc3, store_pa6t_pmc3),
  186. _SYSDEV_ATTR(pmc4, 0600, show_pa6t_pmc4, store_pa6t_pmc4),
  187. _SYSDEV_ATTR(pmc5, 0600, show_pa6t_pmc5, store_pa6t_pmc5),
  188. };
  189. static void register_cpu_online(unsigned int cpu)
  190. {
  191. struct cpu *c = &per_cpu(cpu_devices, cpu);
  192. struct sys_device *s = &c->sysdev;
  193. struct sysdev_attribute *attrs, *pmc_attrs;
  194. int i, nattrs;
  195. if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
  196. cpu_has_feature(CPU_FTR_SMT))
  197. sysdev_create_file(s, &attr_smt_snooze_delay);
  198. /* PMC stuff */
  199. switch (cur_cpu_spec->pmc_type) {
  200. case PPC_PMC_IBM:
  201. attrs = ibm_common_attrs;
  202. nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
  203. pmc_attrs = ibm_pmc_attrs;
  204. break;
  205. case PPC_PMC_PA6T:
  206. /* PA Semi starts counting at PMC0 */
  207. attrs = pa6t_attrs;
  208. nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
  209. pmc_attrs = NULL;
  210. break;
  211. default:
  212. attrs = NULL;
  213. nattrs = 0;
  214. pmc_attrs = NULL;
  215. }
  216. for (i = 0; i < nattrs; i++)
  217. sysdev_create_file(s, &attrs[i]);
  218. if (pmc_attrs)
  219. for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
  220. sysdev_create_file(s, &pmc_attrs[i]);
  221. if (cpu_has_feature(CPU_FTR_MMCRA))
  222. sysdev_create_file(s, &attr_mmcra);
  223. if (cpu_has_feature(CPU_FTR_PURR))
  224. sysdev_create_file(s, &attr_purr);
  225. if (cpu_has_feature(CPU_FTR_SPURR))
  226. sysdev_create_file(s, &attr_spurr);
  227. if (cpu_has_feature(CPU_FTR_DSCR))
  228. sysdev_create_file(s, &attr_dscr);
  229. }
  230. #ifdef CONFIG_HOTPLUG_CPU
  231. static void unregister_cpu_online(unsigned int cpu)
  232. {
  233. struct cpu *c = &per_cpu(cpu_devices, cpu);
  234. struct sys_device *s = &c->sysdev;
  235. struct sysdev_attribute *attrs, *pmc_attrs;
  236. int i, nattrs;
  237. BUG_ON(!c->hotpluggable);
  238. if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
  239. cpu_has_feature(CPU_FTR_SMT))
  240. sysdev_remove_file(s, &attr_smt_snooze_delay);
  241. /* PMC stuff */
  242. switch (cur_cpu_spec->pmc_type) {
  243. case PPC_PMC_IBM:
  244. attrs = ibm_common_attrs;
  245. nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
  246. pmc_attrs = ibm_pmc_attrs;
  247. break;
  248. case PPC_PMC_PA6T:
  249. /* PA Semi starts counting at PMC0 */
  250. attrs = pa6t_attrs;
  251. nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
  252. pmc_attrs = NULL;
  253. break;
  254. default:
  255. attrs = NULL;
  256. nattrs = 0;
  257. pmc_attrs = NULL;
  258. }
  259. for (i = 0; i < nattrs; i++)
  260. sysdev_remove_file(s, &attrs[i]);
  261. if (pmc_attrs)
  262. for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
  263. sysdev_remove_file(s, &pmc_attrs[i]);
  264. if (cpu_has_feature(CPU_FTR_MMCRA))
  265. sysdev_remove_file(s, &attr_mmcra);
  266. if (cpu_has_feature(CPU_FTR_PURR))
  267. sysdev_remove_file(s, &attr_purr);
  268. if (cpu_has_feature(CPU_FTR_SPURR))
  269. sysdev_remove_file(s, &attr_spurr);
  270. if (cpu_has_feature(CPU_FTR_DSCR))
  271. sysdev_remove_file(s, &attr_dscr);
  272. }
  273. #endif /* CONFIG_HOTPLUG_CPU */
  274. static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
  275. unsigned long action, void *hcpu)
  276. {
  277. unsigned int cpu = (unsigned int)(long)hcpu;
  278. switch (action) {
  279. case CPU_ONLINE:
  280. case CPU_ONLINE_FROZEN:
  281. register_cpu_online(cpu);
  282. break;
  283. #ifdef CONFIG_HOTPLUG_CPU
  284. case CPU_DEAD:
  285. case CPU_DEAD_FROZEN:
  286. unregister_cpu_online(cpu);
  287. break;
  288. #endif
  289. }
  290. return NOTIFY_OK;
  291. }
  292. static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
  293. .notifier_call = sysfs_cpu_notify,
  294. };
  295. static DEFINE_MUTEX(cpu_mutex);
  296. int cpu_add_sysdev_attr(struct sysdev_attribute *attr)
  297. {
  298. int cpu;
  299. mutex_lock(&cpu_mutex);
  300. for_each_possible_cpu(cpu) {
  301. sysdev_create_file(get_cpu_sysdev(cpu), attr);
  302. }
  303. mutex_unlock(&cpu_mutex);
  304. return 0;
  305. }
  306. EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr);
  307. int cpu_add_sysdev_attr_group(struct attribute_group *attrs)
  308. {
  309. int cpu;
  310. struct sys_device *sysdev;
  311. mutex_lock(&cpu_mutex);
  312. for_each_possible_cpu(cpu) {
  313. sysdev = get_cpu_sysdev(cpu);
  314. sysfs_create_group(&sysdev->kobj, attrs);
  315. }
  316. mutex_unlock(&cpu_mutex);
  317. return 0;
  318. }
  319. EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr_group);
  320. void cpu_remove_sysdev_attr(struct sysdev_attribute *attr)
  321. {
  322. int cpu;
  323. mutex_lock(&cpu_mutex);
  324. for_each_possible_cpu(cpu) {
  325. sysdev_remove_file(get_cpu_sysdev(cpu), attr);
  326. }
  327. mutex_unlock(&cpu_mutex);
  328. }
  329. EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr);
  330. void cpu_remove_sysdev_attr_group(struct attribute_group *attrs)
  331. {
  332. int cpu;
  333. struct sys_device *sysdev;
  334. mutex_lock(&cpu_mutex);
  335. for_each_possible_cpu(cpu) {
  336. sysdev = get_cpu_sysdev(cpu);
  337. sysfs_remove_group(&sysdev->kobj, attrs);
  338. }
  339. mutex_unlock(&cpu_mutex);
  340. }
  341. EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr_group);
  342. /* NUMA stuff */
  343. #ifdef CONFIG_NUMA
  344. static void register_nodes(void)
  345. {
  346. int i;
  347. for (i = 0; i < MAX_NUMNODES; i++)
  348. register_one_node(i);
  349. }
  350. int sysfs_add_device_to_node(struct sys_device *dev, int nid)
  351. {
  352. struct node *node = &node_devices[nid];
  353. return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
  354. kobject_name(&dev->kobj));
  355. }
  356. void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
  357. {
  358. struct node *node = &node_devices[nid];
  359. sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
  360. }
  361. #else
  362. static void register_nodes(void)
  363. {
  364. return;
  365. }
  366. #endif
  367. EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
  368. EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
  369. /* Only valid if CPU is present. */
  370. static ssize_t show_physical_id(struct sys_device *dev, char *buf)
  371. {
  372. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  373. return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
  374. }
  375. static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
  376. static int __init topology_init(void)
  377. {
  378. int cpu;
  379. register_nodes();
  380. register_cpu_notifier(&sysfs_cpu_nb);
  381. for_each_possible_cpu(cpu) {
  382. struct cpu *c = &per_cpu(cpu_devices, cpu);
  383. /*
  384. * For now, we just see if the system supports making
  385. * the RTAS calls for CPU hotplug. But, there may be a
  386. * more comprehensive way to do this for an individual
  387. * CPU. For instance, the boot cpu might never be valid
  388. * for hotplugging.
  389. */
  390. if (ppc_md.cpu_die)
  391. c->hotpluggable = 1;
  392. if (cpu_online(cpu) || c->hotpluggable) {
  393. register_cpu(c, cpu);
  394. sysdev_create_file(&c->sysdev, &attr_physical_id);
  395. }
  396. if (cpu_online(cpu))
  397. register_cpu_online(cpu);
  398. }
  399. return 0;
  400. }
  401. subsys_initcall(topology_init);