sysfs.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. #include <linux/config.h>
  2. #include <linux/sysdev.h>
  3. #include <linux/cpu.h>
  4. #include <linux/smp.h>
  5. #include <linux/percpu.h>
  6. #include <linux/init.h>
  7. #include <linux/sched.h>
  8. #include <linux/module.h>
  9. #include <linux/nodemask.h>
  10. #include <linux/cpumask.h>
  11. #include <linux/notifier.h>
  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/systemcfg.h>
  18. #include <asm/paca.h>
  19. #include <asm/lppaca.h>
  20. #include <asm/machdep.h>
  21. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  22. /* SMT stuff */
  23. #ifdef CONFIG_PPC_MULTIPLATFORM
  24. /* default to snooze disabled */
  25. DEFINE_PER_CPU(unsigned long, smt_snooze_delay);
  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. unsigned int *val;
  51. unsigned int cpu;
  52. if (!cpu_has_feature(CPU_FTR_SMT))
  53. return 1;
  54. options = find_path_device("/options");
  55. if (!options)
  56. return 1;
  57. val = (unsigned int *)get_property(options, "ibm,smt-snooze-delay",
  58. NULL);
  59. if (!smt_snooze_cmdline && val) {
  60. for_each_cpu(cpu)
  61. per_cpu(smt_snooze_delay, cpu) = *val;
  62. }
  63. return 1;
  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_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. /*
  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. unsigned long hid0;
  88. #ifdef CONFIG_PPC_PSERIES
  89. unsigned long set, reset;
  90. int ret;
  91. #endif /* CONFIG_PPC_PSERIES */
  92. /* Only need to enable them once */
  93. if (__get_cpu_var(pmcs_enabled))
  94. return;
  95. __get_cpu_var(pmcs_enabled) = 1;
  96. switch (systemcfg->platform) {
  97. case PLATFORM_PSERIES:
  98. case PLATFORM_POWERMAC:
  99. hid0 = mfspr(HID0);
  100. hid0 |= 1UL << (63 - 20);
  101. /* POWER4 requires the following sequence */
  102. asm volatile(
  103. "sync\n"
  104. "mtspr %1, %0\n"
  105. "mfspr %0, %1\n"
  106. "mfspr %0, %1\n"
  107. "mfspr %0, %1\n"
  108. "mfspr %0, %1\n"
  109. "mfspr %0, %1\n"
  110. "mfspr %0, %1\n"
  111. "isync" : "=&r" (hid0) : "i" (HID0), "0" (hid0):
  112. "memory");
  113. break;
  114. #ifdef CONFIG_PPC_PSERIES
  115. case PLATFORM_PSERIES_LPAR:
  116. set = 1UL << 63;
  117. reset = 0;
  118. ret = plpar_hcall_norets(H_PERFMON, set, reset);
  119. if (ret)
  120. printk(KERN_ERR "H_PERFMON call on cpu %u "
  121. "returned %d\n",
  122. smp_processor_id(), ret);
  123. break;
  124. #endif /* CONFIG_PPC_PSERIES */
  125. default:
  126. break;
  127. }
  128. #ifdef CONFIG_PPC_PSERIES
  129. /* instruct hypervisor to maintain PMCs */
  130. if (cur_cpu_spec->firmware_features & FW_FEATURE_SPLPAR)
  131. get_paca()->lppaca.pmcregs_in_use = 1;
  132. /*
  133. * On SMT machines we have to set the run latch in the ctrl register
  134. * in order to make PMC6 spin.
  135. */
  136. if (cpu_has_feature(CPU_FTR_SMT))
  137. ppc64_runlatch_on();
  138. #endif /* CONFIG_PPC_PSERIES */
  139. }
  140. #else
  141. /* PMC stuff */
  142. void ppc64_enable_pmcs(void)
  143. {
  144. /* XXX Implement for iseries */
  145. }
  146. #endif /* CONFIG_PPC_MULTIPLATFORM */
  147. EXPORT_SYMBOL(ppc64_enable_pmcs);
  148. /* XXX convert to rusty's on_one_cpu */
  149. static unsigned long run_on_cpu(unsigned long cpu,
  150. unsigned long (*func)(unsigned long),
  151. unsigned long arg)
  152. {
  153. cpumask_t old_affinity = current->cpus_allowed;
  154. unsigned long ret;
  155. /* should return -EINVAL to userspace */
  156. if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
  157. return 0;
  158. ret = func(arg);
  159. set_cpus_allowed(current, old_affinity);
  160. return ret;
  161. }
  162. #define SYSFS_PMCSETUP(NAME, ADDRESS) \
  163. static unsigned long read_##NAME(unsigned long junk) \
  164. { \
  165. return mfspr(ADDRESS); \
  166. } \
  167. static unsigned long write_##NAME(unsigned long val) \
  168. { \
  169. ppc64_enable_pmcs(); \
  170. mtspr(ADDRESS, val); \
  171. return 0; \
  172. } \
  173. static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
  174. { \
  175. struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
  176. unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
  177. return sprintf(buf, "%lx\n", val); \
  178. } \
  179. static ssize_t __attribute_used__ \
  180. store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
  181. { \
  182. struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
  183. unsigned long val; \
  184. int ret = sscanf(buf, "%lx", &val); \
  185. if (ret != 1) \
  186. return -EINVAL; \
  187. run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
  188. return count; \
  189. }
  190. SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
  191. SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
  192. SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
  193. SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
  194. SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
  195. SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
  196. SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
  197. SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
  198. SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
  199. SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
  200. SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
  201. SYSFS_PMCSETUP(purr, SPRN_PURR);
  202. static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
  203. static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
  204. static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
  205. static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
  206. static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
  207. static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
  208. static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
  209. static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
  210. static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
  211. static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
  212. static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
  213. static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
  214. static void register_cpu_online(unsigned int cpu)
  215. {
  216. struct cpu *c = &per_cpu(cpu_devices, cpu);
  217. struct sys_device *s = &c->sysdev;
  218. #ifndef CONFIG_PPC_ISERIES
  219. if (cpu_has_feature(CPU_FTR_SMT))
  220. sysdev_create_file(s, &attr_smt_snooze_delay);
  221. #endif
  222. /* PMC stuff */
  223. sysdev_create_file(s, &attr_mmcr0);
  224. sysdev_create_file(s, &attr_mmcr1);
  225. if (cpu_has_feature(CPU_FTR_MMCRA))
  226. sysdev_create_file(s, &attr_mmcra);
  227. sysdev_create_file(s, &attr_pmc1);
  228. sysdev_create_file(s, &attr_pmc2);
  229. sysdev_create_file(s, &attr_pmc3);
  230. sysdev_create_file(s, &attr_pmc4);
  231. sysdev_create_file(s, &attr_pmc5);
  232. sysdev_create_file(s, &attr_pmc6);
  233. if (cpu_has_feature(CPU_FTR_PMC8)) {
  234. sysdev_create_file(s, &attr_pmc7);
  235. sysdev_create_file(s, &attr_pmc8);
  236. }
  237. if (cpu_has_feature(CPU_FTR_SMT))
  238. sysdev_create_file(s, &attr_purr);
  239. }
  240. #ifdef CONFIG_HOTPLUG_CPU
  241. static void unregister_cpu_online(unsigned int cpu)
  242. {
  243. struct cpu *c = &per_cpu(cpu_devices, cpu);
  244. struct sys_device *s = &c->sysdev;
  245. BUG_ON(c->no_control);
  246. #ifndef CONFIG_PPC_ISERIES
  247. if (cpu_has_feature(CPU_FTR_SMT))
  248. sysdev_remove_file(s, &attr_smt_snooze_delay);
  249. #endif
  250. /* PMC stuff */
  251. sysdev_remove_file(s, &attr_mmcr0);
  252. sysdev_remove_file(s, &attr_mmcr1);
  253. if (cpu_has_feature(CPU_FTR_MMCRA))
  254. sysdev_remove_file(s, &attr_mmcra);
  255. sysdev_remove_file(s, &attr_pmc1);
  256. sysdev_remove_file(s, &attr_pmc2);
  257. sysdev_remove_file(s, &attr_pmc3);
  258. sysdev_remove_file(s, &attr_pmc4);
  259. sysdev_remove_file(s, &attr_pmc5);
  260. sysdev_remove_file(s, &attr_pmc6);
  261. if (cpu_has_feature(CPU_FTR_PMC8)) {
  262. sysdev_remove_file(s, &attr_pmc7);
  263. sysdev_remove_file(s, &attr_pmc8);
  264. }
  265. if (cpu_has_feature(CPU_FTR_SMT))
  266. sysdev_remove_file(s, &attr_purr);
  267. }
  268. #endif /* CONFIG_HOTPLUG_CPU */
  269. static int __devinit sysfs_cpu_notify(struct notifier_block *self,
  270. unsigned long action, void *hcpu)
  271. {
  272. unsigned int cpu = (unsigned int)(long)hcpu;
  273. switch (action) {
  274. case CPU_ONLINE:
  275. register_cpu_online(cpu);
  276. break;
  277. #ifdef CONFIG_HOTPLUG_CPU
  278. case CPU_DEAD:
  279. unregister_cpu_online(cpu);
  280. break;
  281. #endif
  282. }
  283. return NOTIFY_OK;
  284. }
  285. static struct notifier_block __devinitdata sysfs_cpu_nb = {
  286. .notifier_call = sysfs_cpu_notify,
  287. };
  288. /* NUMA stuff */
  289. #ifdef CONFIG_NUMA
  290. static struct node node_devices[MAX_NUMNODES];
  291. static void register_nodes(void)
  292. {
  293. int i;
  294. for (i = 0; i < MAX_NUMNODES; i++) {
  295. if (node_online(i)) {
  296. int p_node = parent_node(i);
  297. struct node *parent = NULL;
  298. if (p_node != i)
  299. parent = &node_devices[p_node];
  300. register_node(&node_devices[i], i, parent);
  301. }
  302. }
  303. }
  304. #else
  305. static void register_nodes(void)
  306. {
  307. return;
  308. }
  309. #endif
  310. /* Only valid if CPU is present. */
  311. static ssize_t show_physical_id(struct sys_device *dev, char *buf)
  312. {
  313. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  314. return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
  315. }
  316. static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
  317. static int __init topology_init(void)
  318. {
  319. int cpu;
  320. struct node *parent = NULL;
  321. register_nodes();
  322. register_cpu_notifier(&sysfs_cpu_nb);
  323. for_each_cpu(cpu) {
  324. struct cpu *c = &per_cpu(cpu_devices, cpu);
  325. #ifdef CONFIG_NUMA
  326. /* The node to which a cpu belongs can't be known
  327. * until the cpu is made present.
  328. */
  329. parent = NULL;
  330. if (cpu_present(cpu))
  331. parent = &node_devices[cpu_to_node(cpu)];
  332. #endif
  333. /*
  334. * For now, we just see if the system supports making
  335. * the RTAS calls for CPU hotplug. But, there may be a
  336. * more comprehensive way to do this for an individual
  337. * CPU. For instance, the boot cpu might never be valid
  338. * for hotplugging.
  339. */
  340. if (!ppc_md.cpu_die)
  341. c->no_control = 1;
  342. if (cpu_online(cpu) || (c->no_control == 0)) {
  343. register_cpu(c, cpu, parent);
  344. sysdev_create_file(&c->sysdev, &attr_physical_id);
  345. }
  346. if (cpu_online(cpu))
  347. register_cpu_online(cpu);
  348. }
  349. return 0;
  350. }
  351. __initcall(topology_init);