sysfs.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 = find_path_device("/options");
  55. if (!options)
  56. return -ENODEV;
  57. val = 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. return 0;
  63. }
  64. __initcall(smt_setup);
  65. static int __init setup_smt_snooze_delay(char *str)
  66. {
  67. unsigned int cpu;
  68. int snooze;
  69. if (!cpu_has_feature(CPU_FTR_SMT))
  70. return 1;
  71. smt_snooze_cmdline = 1;
  72. if (get_option(&str, &snooze)) {
  73. for_each_possible_cpu(cpu)
  74. per_cpu(smt_snooze_delay, cpu) = snooze;
  75. }
  76. return 1;
  77. }
  78. __setup("smt-snooze-delay=", setup_smt_snooze_delay);
  79. #endif /* CONFIG_PPC_MULTIPLATFORM */
  80. /*
  81. * Enabling PMCs will slow partition context switch times so we only do
  82. * it the first time we write to the PMCs.
  83. */
  84. static DEFINE_PER_CPU(char, pmcs_enabled);
  85. void ppc64_enable_pmcs(void)
  86. {
  87. /* Only need to enable them once */
  88. if (__get_cpu_var(pmcs_enabled))
  89. return;
  90. __get_cpu_var(pmcs_enabled) = 1;
  91. if (ppc_md.enable_pmcs)
  92. ppc_md.enable_pmcs();
  93. }
  94. EXPORT_SYMBOL(ppc64_enable_pmcs);
  95. /* XXX convert to rusty's on_one_cpu */
  96. static unsigned long run_on_cpu(unsigned long cpu,
  97. unsigned long (*func)(unsigned long),
  98. unsigned long arg)
  99. {
  100. cpumask_t old_affinity = current->cpus_allowed;
  101. unsigned long ret;
  102. /* should return -EINVAL to userspace */
  103. if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
  104. return 0;
  105. ret = func(arg);
  106. set_cpus_allowed(current, old_affinity);
  107. return ret;
  108. }
  109. #define SYSFS_PMCSETUP(NAME, ADDRESS) \
  110. static unsigned long read_##NAME(unsigned long junk) \
  111. { \
  112. return mfspr(ADDRESS); \
  113. } \
  114. static unsigned long write_##NAME(unsigned long val) \
  115. { \
  116. ppc64_enable_pmcs(); \
  117. mtspr(ADDRESS, val); \
  118. return 0; \
  119. } \
  120. static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
  121. { \
  122. struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
  123. unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
  124. return sprintf(buf, "%lx\n", val); \
  125. } \
  126. static ssize_t __attribute_used__ \
  127. store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
  128. { \
  129. struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
  130. unsigned long val; \
  131. int ret = sscanf(buf, "%lx", &val); \
  132. if (ret != 1) \
  133. return -EINVAL; \
  134. run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
  135. return count; \
  136. }
  137. SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
  138. SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
  139. SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
  140. SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
  141. SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
  142. SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
  143. SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
  144. SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
  145. SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
  146. SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
  147. SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
  148. SYSFS_PMCSETUP(purr, SPRN_PURR);
  149. static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
  150. static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
  151. static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
  152. static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
  153. static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
  154. static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
  155. static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
  156. static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
  157. static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
  158. static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
  159. static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
  160. static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
  161. static void register_cpu_online(unsigned int cpu)
  162. {
  163. struct cpu *c = &per_cpu(cpu_devices, cpu);
  164. struct sys_device *s = &c->sysdev;
  165. #ifndef CONFIG_PPC_ISERIES
  166. if (cpu_has_feature(CPU_FTR_SMT))
  167. sysdev_create_file(s, &attr_smt_snooze_delay);
  168. #endif
  169. /* PMC stuff */
  170. sysdev_create_file(s, &attr_mmcr0);
  171. sysdev_create_file(s, &attr_mmcr1);
  172. if (cpu_has_feature(CPU_FTR_MMCRA))
  173. sysdev_create_file(s, &attr_mmcra);
  174. if (cur_cpu_spec->num_pmcs >= 1)
  175. sysdev_create_file(s, &attr_pmc1);
  176. if (cur_cpu_spec->num_pmcs >= 2)
  177. sysdev_create_file(s, &attr_pmc2);
  178. if (cur_cpu_spec->num_pmcs >= 3)
  179. sysdev_create_file(s, &attr_pmc3);
  180. if (cur_cpu_spec->num_pmcs >= 4)
  181. sysdev_create_file(s, &attr_pmc4);
  182. if (cur_cpu_spec->num_pmcs >= 5)
  183. sysdev_create_file(s, &attr_pmc5);
  184. if (cur_cpu_spec->num_pmcs >= 6)
  185. sysdev_create_file(s, &attr_pmc6);
  186. if (cur_cpu_spec->num_pmcs >= 7)
  187. sysdev_create_file(s, &attr_pmc7);
  188. if (cur_cpu_spec->num_pmcs >= 8)
  189. sysdev_create_file(s, &attr_pmc8);
  190. if (cpu_has_feature(CPU_FTR_PURR))
  191. sysdev_create_file(s, &attr_purr);
  192. }
  193. #ifdef CONFIG_HOTPLUG_CPU
  194. static void unregister_cpu_online(unsigned int cpu)
  195. {
  196. struct cpu *c = &per_cpu(cpu_devices, cpu);
  197. struct sys_device *s = &c->sysdev;
  198. BUG_ON(c->no_control);
  199. #ifndef CONFIG_PPC_ISERIES
  200. if (cpu_has_feature(CPU_FTR_SMT))
  201. sysdev_remove_file(s, &attr_smt_snooze_delay);
  202. #endif
  203. /* PMC stuff */
  204. sysdev_remove_file(s, &attr_mmcr0);
  205. sysdev_remove_file(s, &attr_mmcr1);
  206. if (cpu_has_feature(CPU_FTR_MMCRA))
  207. sysdev_remove_file(s, &attr_mmcra);
  208. if (cur_cpu_spec->num_pmcs >= 1)
  209. sysdev_remove_file(s, &attr_pmc1);
  210. if (cur_cpu_spec->num_pmcs >= 2)
  211. sysdev_remove_file(s, &attr_pmc2);
  212. if (cur_cpu_spec->num_pmcs >= 3)
  213. sysdev_remove_file(s, &attr_pmc3);
  214. if (cur_cpu_spec->num_pmcs >= 4)
  215. sysdev_remove_file(s, &attr_pmc4);
  216. if (cur_cpu_spec->num_pmcs >= 5)
  217. sysdev_remove_file(s, &attr_pmc5);
  218. if (cur_cpu_spec->num_pmcs >= 6)
  219. sysdev_remove_file(s, &attr_pmc6);
  220. if (cur_cpu_spec->num_pmcs >= 7)
  221. sysdev_remove_file(s, &attr_pmc7);
  222. if (cur_cpu_spec->num_pmcs >= 8)
  223. sysdev_remove_file(s, &attr_pmc8);
  224. if (cpu_has_feature(CPU_FTR_PURR))
  225. sysdev_remove_file(s, &attr_purr);
  226. }
  227. #endif /* CONFIG_HOTPLUG_CPU */
  228. static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
  229. unsigned long action, void *hcpu)
  230. {
  231. unsigned int cpu = (unsigned int)(long)hcpu;
  232. switch (action) {
  233. case CPU_ONLINE:
  234. register_cpu_online(cpu);
  235. break;
  236. #ifdef CONFIG_HOTPLUG_CPU
  237. case CPU_DEAD:
  238. unregister_cpu_online(cpu);
  239. break;
  240. #endif
  241. }
  242. return NOTIFY_OK;
  243. }
  244. static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
  245. .notifier_call = sysfs_cpu_notify,
  246. };
  247. /* NUMA stuff */
  248. #ifdef CONFIG_NUMA
  249. static void register_nodes(void)
  250. {
  251. int i;
  252. for (i = 0; i < MAX_NUMNODES; i++)
  253. register_one_node(i);
  254. }
  255. int sysfs_add_device_to_node(struct sys_device *dev, int nid)
  256. {
  257. struct node *node = &node_devices[nid];
  258. return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
  259. kobject_name(&dev->kobj));
  260. }
  261. void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
  262. {
  263. struct node *node = &node_devices[nid];
  264. sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
  265. }
  266. #else
  267. static void register_nodes(void)
  268. {
  269. return;
  270. }
  271. #endif
  272. EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
  273. EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
  274. /* Only valid if CPU is present. */
  275. static ssize_t show_physical_id(struct sys_device *dev, char *buf)
  276. {
  277. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  278. return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
  279. }
  280. static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
  281. static int __init topology_init(void)
  282. {
  283. int cpu;
  284. register_nodes();
  285. register_cpu_notifier(&sysfs_cpu_nb);
  286. for_each_possible_cpu(cpu) {
  287. struct cpu *c = &per_cpu(cpu_devices, cpu);
  288. /*
  289. * For now, we just see if the system supports making
  290. * the RTAS calls for CPU hotplug. But, there may be a
  291. * more comprehensive way to do this for an individual
  292. * CPU. For instance, the boot cpu might never be valid
  293. * for hotplugging.
  294. */
  295. if (!ppc_md.cpu_die)
  296. c->no_control = 1;
  297. if (cpu_online(cpu) || (c->no_control == 0)) {
  298. register_cpu(c, cpu);
  299. sysdev_create_file(&c->sysdev, &attr_physical_id);
  300. }
  301. if (cpu_online(cpu))
  302. register_cpu_online(cpu);
  303. }
  304. return 0;
  305. }
  306. __initcall(topology_init);