sysfs.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/firmware.h>
  16. #include <asm/hvcall.h>
  17. #include <asm/prom.h>
  18. #include <asm/systemcfg.h>
  19. #include <asm/paca.h>
  20. #include <asm/lppaca.h>
  21. #include <asm/machdep.h>
  22. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  23. /* SMT stuff */
  24. #ifdef CONFIG_PPC_MULTIPLATFORM
  25. /* default to snooze disabled */
  26. DEFINE_PER_CPU(unsigned long, smt_snooze_delay);
  27. static ssize_t store_smt_snooze_delay(struct sys_device *dev, const char *buf,
  28. size_t count)
  29. {
  30. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  31. ssize_t ret;
  32. unsigned long snooze;
  33. ret = sscanf(buf, "%lu", &snooze);
  34. if (ret != 1)
  35. return -EINVAL;
  36. per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
  37. return count;
  38. }
  39. static ssize_t show_smt_snooze_delay(struct sys_device *dev, char *buf)
  40. {
  41. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  42. return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
  43. }
  44. static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
  45. store_smt_snooze_delay);
  46. /* Only parse OF options if the matching cmdline option was not specified */
  47. static int smt_snooze_cmdline;
  48. static int __init smt_setup(void)
  49. {
  50. struct device_node *options;
  51. unsigned int *val;
  52. unsigned int cpu;
  53. if (!cpu_has_feature(CPU_FTR_SMT))
  54. return 1;
  55. options = find_path_device("/options");
  56. if (!options)
  57. return 1;
  58. val = (unsigned int *)get_property(options, "ibm,smt-snooze-delay",
  59. NULL);
  60. if (!smt_snooze_cmdline && val) {
  61. for_each_cpu(cpu)
  62. per_cpu(smt_snooze_delay, cpu) = *val;
  63. }
  64. return 1;
  65. }
  66. __initcall(smt_setup);
  67. static int __init setup_smt_snooze_delay(char *str)
  68. {
  69. unsigned int cpu;
  70. int snooze;
  71. if (!cpu_has_feature(CPU_FTR_SMT))
  72. return 1;
  73. smt_snooze_cmdline = 1;
  74. if (get_option(&str, &snooze)) {
  75. for_each_cpu(cpu)
  76. per_cpu(smt_snooze_delay, cpu) = snooze;
  77. }
  78. return 1;
  79. }
  80. __setup("smt-snooze-delay=", setup_smt_snooze_delay);
  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. unsigned long hid0;
  89. #ifdef CONFIG_PPC_PSERIES
  90. unsigned long set, reset;
  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. plpar_hcall_norets(H_PERFMON, set, reset);
  119. break;
  120. #endif /* CONFIG_PPC_PSERIES */
  121. default:
  122. break;
  123. }
  124. /* instruct hypervisor to maintain PMCs */
  125. if (firmware_has_feature(FW_FEATURE_SPLPAR))
  126. get_paca()->lppaca.pmcregs_in_use = 1;
  127. }
  128. #else
  129. /* PMC stuff */
  130. void ppc64_enable_pmcs(void)
  131. {
  132. /* XXX Implement for iseries */
  133. }
  134. #endif /* CONFIG_PPC_MULTIPLATFORM */
  135. EXPORT_SYMBOL(ppc64_enable_pmcs);
  136. /* XXX convert to rusty's on_one_cpu */
  137. static unsigned long run_on_cpu(unsigned long cpu,
  138. unsigned long (*func)(unsigned long),
  139. unsigned long arg)
  140. {
  141. cpumask_t old_affinity = current->cpus_allowed;
  142. unsigned long ret;
  143. /* should return -EINVAL to userspace */
  144. if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
  145. return 0;
  146. ret = func(arg);
  147. set_cpus_allowed(current, old_affinity);
  148. return ret;
  149. }
  150. #define SYSFS_PMCSETUP(NAME, ADDRESS) \
  151. static unsigned long read_##NAME(unsigned long junk) \
  152. { \
  153. return mfspr(ADDRESS); \
  154. } \
  155. static unsigned long write_##NAME(unsigned long val) \
  156. { \
  157. ppc64_enable_pmcs(); \
  158. mtspr(ADDRESS, val); \
  159. return 0; \
  160. } \
  161. static ssize_t show_##NAME(struct sys_device *dev, char *buf) \
  162. { \
  163. struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
  164. unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
  165. return sprintf(buf, "%lx\n", val); \
  166. } \
  167. static ssize_t __attribute_used__ \
  168. store_##NAME(struct sys_device *dev, const char *buf, size_t count) \
  169. { \
  170. struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
  171. unsigned long val; \
  172. int ret = sscanf(buf, "%lx", &val); \
  173. if (ret != 1) \
  174. return -EINVAL; \
  175. run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
  176. return count; \
  177. }
  178. SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
  179. SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
  180. SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
  181. SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
  182. SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
  183. SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
  184. SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
  185. SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
  186. SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
  187. SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
  188. SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
  189. SYSFS_PMCSETUP(purr, SPRN_PURR);
  190. static SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0);
  191. static SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1);
  192. static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
  193. static SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1);
  194. static SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2);
  195. static SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3);
  196. static SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4);
  197. static SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5);
  198. static SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6);
  199. static SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7);
  200. static SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8);
  201. static SYSDEV_ATTR(purr, 0600, show_purr, NULL);
  202. static void register_cpu_online(unsigned int cpu)
  203. {
  204. struct cpu *c = &per_cpu(cpu_devices, cpu);
  205. struct sys_device *s = &c->sysdev;
  206. #ifndef CONFIG_PPC_ISERIES
  207. if (cpu_has_feature(CPU_FTR_SMT))
  208. sysdev_create_file(s, &attr_smt_snooze_delay);
  209. #endif
  210. /* PMC stuff */
  211. sysdev_create_file(s, &attr_mmcr0);
  212. sysdev_create_file(s, &attr_mmcr1);
  213. if (cpu_has_feature(CPU_FTR_MMCRA))
  214. sysdev_create_file(s, &attr_mmcra);
  215. sysdev_create_file(s, &attr_pmc1);
  216. sysdev_create_file(s, &attr_pmc2);
  217. sysdev_create_file(s, &attr_pmc3);
  218. sysdev_create_file(s, &attr_pmc4);
  219. sysdev_create_file(s, &attr_pmc5);
  220. sysdev_create_file(s, &attr_pmc6);
  221. if (cpu_has_feature(CPU_FTR_PMC8)) {
  222. sysdev_create_file(s, &attr_pmc7);
  223. sysdev_create_file(s, &attr_pmc8);
  224. }
  225. if (cpu_has_feature(CPU_FTR_SMT))
  226. sysdev_create_file(s, &attr_purr);
  227. }
  228. #ifdef CONFIG_HOTPLUG_CPU
  229. static void unregister_cpu_online(unsigned int cpu)
  230. {
  231. struct cpu *c = &per_cpu(cpu_devices, cpu);
  232. struct sys_device *s = &c->sysdev;
  233. BUG_ON(c->no_control);
  234. #ifndef CONFIG_PPC_ISERIES
  235. if (cpu_has_feature(CPU_FTR_SMT))
  236. sysdev_remove_file(s, &attr_smt_snooze_delay);
  237. #endif
  238. /* PMC stuff */
  239. sysdev_remove_file(s, &attr_mmcr0);
  240. sysdev_remove_file(s, &attr_mmcr1);
  241. if (cpu_has_feature(CPU_FTR_MMCRA))
  242. sysdev_remove_file(s, &attr_mmcra);
  243. sysdev_remove_file(s, &attr_pmc1);
  244. sysdev_remove_file(s, &attr_pmc2);
  245. sysdev_remove_file(s, &attr_pmc3);
  246. sysdev_remove_file(s, &attr_pmc4);
  247. sysdev_remove_file(s, &attr_pmc5);
  248. sysdev_remove_file(s, &attr_pmc6);
  249. if (cpu_has_feature(CPU_FTR_PMC8)) {
  250. sysdev_remove_file(s, &attr_pmc7);
  251. sysdev_remove_file(s, &attr_pmc8);
  252. }
  253. if (cpu_has_feature(CPU_FTR_SMT))
  254. sysdev_remove_file(s, &attr_purr);
  255. }
  256. #endif /* CONFIG_HOTPLUG_CPU */
  257. static int __devinit sysfs_cpu_notify(struct notifier_block *self,
  258. unsigned long action, void *hcpu)
  259. {
  260. unsigned int cpu = (unsigned int)(long)hcpu;
  261. switch (action) {
  262. case CPU_ONLINE:
  263. register_cpu_online(cpu);
  264. break;
  265. #ifdef CONFIG_HOTPLUG_CPU
  266. case CPU_DEAD:
  267. unregister_cpu_online(cpu);
  268. break;
  269. #endif
  270. }
  271. return NOTIFY_OK;
  272. }
  273. static struct notifier_block __devinitdata sysfs_cpu_nb = {
  274. .notifier_call = sysfs_cpu_notify,
  275. };
  276. /* NUMA stuff */
  277. #ifdef CONFIG_NUMA
  278. static struct node node_devices[MAX_NUMNODES];
  279. static void register_nodes(void)
  280. {
  281. int i;
  282. for (i = 0; i < MAX_NUMNODES; i++) {
  283. if (node_online(i)) {
  284. int p_node = parent_node(i);
  285. struct node *parent = NULL;
  286. if (p_node != i)
  287. parent = &node_devices[p_node];
  288. register_node(&node_devices[i], i, parent);
  289. }
  290. }
  291. }
  292. #else
  293. static void register_nodes(void)
  294. {
  295. return;
  296. }
  297. #endif
  298. /* Only valid if CPU is present. */
  299. static ssize_t show_physical_id(struct sys_device *dev, char *buf)
  300. {
  301. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  302. return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
  303. }
  304. static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
  305. static int __init topology_init(void)
  306. {
  307. int cpu;
  308. struct node *parent = NULL;
  309. register_nodes();
  310. register_cpu_notifier(&sysfs_cpu_nb);
  311. for_each_cpu(cpu) {
  312. struct cpu *c = &per_cpu(cpu_devices, cpu);
  313. #ifdef CONFIG_NUMA
  314. /* The node to which a cpu belongs can't be known
  315. * until the cpu is made present.
  316. */
  317. parent = NULL;
  318. if (cpu_present(cpu))
  319. parent = &node_devices[cpu_to_node(cpu)];
  320. #endif
  321. /*
  322. * For now, we just see if the system supports making
  323. * the RTAS calls for CPU hotplug. But, there may be a
  324. * more comprehensive way to do this for an individual
  325. * CPU. For instance, the boot cpu might never be valid
  326. * for hotplugging.
  327. */
  328. if (!ppc_md.cpu_die)
  329. c->no_control = 1;
  330. if (cpu_online(cpu) || (c->no_control == 0)) {
  331. register_cpu(c, cpu, parent);
  332. sysdev_create_file(&c->sysdev, &attr_physical_id);
  333. }
  334. if (cpu_online(cpu))
  335. register_cpu_online(cpu);
  336. }
  337. return 0;
  338. }
  339. __initcall(topology_init);