sysfs.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945
  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. #ifdef CONFIG_PPC64
  20. #include <asm/paca.h>
  21. #include <asm/lppaca.h>
  22. #endif
  23. static DEFINE_PER_CPU(struct cpu, cpu_devices);
  24. static DEFINE_PER_CPU(struct kobject *, cache_toplevel);
  25. /*
  26. * SMT snooze delay stuff, 64-bit only for now
  27. */
  28. #ifdef CONFIG_PPC64
  29. /* Time in microseconds we delay before sleeping in the idle loop */
  30. DEFINE_PER_CPU(unsigned long, smt_snooze_delay) = { 100 };
  31. static ssize_t store_smt_snooze_delay(struct sys_device *dev,
  32. struct sysdev_attribute *attr,
  33. const char *buf,
  34. size_t count)
  35. {
  36. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  37. ssize_t ret;
  38. unsigned long snooze;
  39. ret = sscanf(buf, "%lu", &snooze);
  40. if (ret != 1)
  41. return -EINVAL;
  42. per_cpu(smt_snooze_delay, cpu->sysdev.id) = snooze;
  43. return count;
  44. }
  45. static ssize_t show_smt_snooze_delay(struct sys_device *dev,
  46. struct sysdev_attribute *attr,
  47. char *buf)
  48. {
  49. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  50. return sprintf(buf, "%lu\n", per_cpu(smt_snooze_delay, cpu->sysdev.id));
  51. }
  52. static SYSDEV_ATTR(smt_snooze_delay, 0644, show_smt_snooze_delay,
  53. store_smt_snooze_delay);
  54. /* Only parse OF options if the matching cmdline option was not specified */
  55. static int smt_snooze_cmdline;
  56. static int __init smt_setup(void)
  57. {
  58. struct device_node *options;
  59. const unsigned int *val;
  60. unsigned int cpu;
  61. if (!cpu_has_feature(CPU_FTR_SMT))
  62. return -ENODEV;
  63. options = of_find_node_by_path("/options");
  64. if (!options)
  65. return -ENODEV;
  66. val = of_get_property(options, "ibm,smt-snooze-delay", NULL);
  67. if (!smt_snooze_cmdline && val) {
  68. for_each_possible_cpu(cpu)
  69. per_cpu(smt_snooze_delay, cpu) = *val;
  70. }
  71. of_node_put(options);
  72. return 0;
  73. }
  74. __initcall(smt_setup);
  75. static int __init setup_smt_snooze_delay(char *str)
  76. {
  77. unsigned int cpu;
  78. int snooze;
  79. if (!cpu_has_feature(CPU_FTR_SMT))
  80. return 1;
  81. smt_snooze_cmdline = 1;
  82. if (get_option(&str, &snooze)) {
  83. for_each_possible_cpu(cpu)
  84. per_cpu(smt_snooze_delay, cpu) = snooze;
  85. }
  86. return 1;
  87. }
  88. __setup("smt-snooze-delay=", setup_smt_snooze_delay);
  89. #endif /* CONFIG_PPC64 */
  90. /*
  91. * Enabling PMCs will slow partition context switch times so we only do
  92. * it the first time we write to the PMCs.
  93. */
  94. static DEFINE_PER_CPU(char, pmcs_enabled);
  95. void ppc_enable_pmcs(void)
  96. {
  97. /* Only need to enable them once */
  98. if (__get_cpu_var(pmcs_enabled))
  99. return;
  100. __get_cpu_var(pmcs_enabled) = 1;
  101. if (ppc_md.enable_pmcs)
  102. ppc_md.enable_pmcs();
  103. }
  104. EXPORT_SYMBOL(ppc_enable_pmcs);
  105. /* XXX convert to rusty's on_one_cpu */
  106. static unsigned long run_on_cpu(unsigned long cpu,
  107. unsigned long (*func)(unsigned long),
  108. unsigned long arg)
  109. {
  110. cpumask_t old_affinity = current->cpus_allowed;
  111. unsigned long ret;
  112. /* should return -EINVAL to userspace */
  113. if (set_cpus_allowed(current, cpumask_of_cpu(cpu)))
  114. return 0;
  115. ret = func(arg);
  116. set_cpus_allowed(current, old_affinity);
  117. return ret;
  118. }
  119. #define SYSFS_PMCSETUP(NAME, ADDRESS) \
  120. static unsigned long read_##NAME(unsigned long junk) \
  121. { \
  122. return mfspr(ADDRESS); \
  123. } \
  124. static unsigned long write_##NAME(unsigned long val) \
  125. { \
  126. ppc_enable_pmcs(); \
  127. mtspr(ADDRESS, val); \
  128. return 0; \
  129. } \
  130. static ssize_t show_##NAME(struct sys_device *dev, \
  131. struct sysdev_attribute *attr, \
  132. char *buf) \
  133. { \
  134. struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
  135. unsigned long val = run_on_cpu(cpu->sysdev.id, read_##NAME, 0); \
  136. return sprintf(buf, "%lx\n", val); \
  137. } \
  138. static ssize_t __used \
  139. store_##NAME(struct sys_device *dev, struct sysdev_attribute *attr, \
  140. const char *buf, size_t count) \
  141. { \
  142. struct cpu *cpu = container_of(dev, struct cpu, sysdev); \
  143. unsigned long val; \
  144. int ret = sscanf(buf, "%lx", &val); \
  145. if (ret != 1) \
  146. return -EINVAL; \
  147. run_on_cpu(cpu->sysdev.id, write_##NAME, val); \
  148. return count; \
  149. }
  150. /* Let's define all possible registers, we'll only hook up the ones
  151. * that are implemented on the current processor
  152. */
  153. #ifdef CONFIG_PPC64
  154. #define HAS_PPC_PMC_CLASSIC 1
  155. #define HAS_PPC_PMC_IBM 1
  156. #define HAS_PPC_PMC_PA6T 1
  157. #elif CONFIG_6xx
  158. #define HAS_PPC_PMC_CLASSIC 1
  159. #define HAS_PPC_PMC_IBM 1
  160. #define HAS_PPC_PMC_G4 1
  161. #endif
  162. #ifdef HAS_PPC_PMC_CLASSIC
  163. SYSFS_PMCSETUP(mmcr0, SPRN_MMCR0);
  164. SYSFS_PMCSETUP(mmcr1, SPRN_MMCR1);
  165. SYSFS_PMCSETUP(pmc1, SPRN_PMC1);
  166. SYSFS_PMCSETUP(pmc2, SPRN_PMC2);
  167. SYSFS_PMCSETUP(pmc3, SPRN_PMC3);
  168. SYSFS_PMCSETUP(pmc4, SPRN_PMC4);
  169. SYSFS_PMCSETUP(pmc5, SPRN_PMC5);
  170. SYSFS_PMCSETUP(pmc6, SPRN_PMC6);
  171. #ifdef HAS_PPC_PMC_G4
  172. SYSFS_PMCSETUP(mmcr2, SPRN_MMCR2);
  173. #endif
  174. #ifdef CONFIG_PPC64
  175. SYSFS_PMCSETUP(pmc7, SPRN_PMC7);
  176. SYSFS_PMCSETUP(pmc8, SPRN_PMC8);
  177. SYSFS_PMCSETUP(mmcra, SPRN_MMCRA);
  178. SYSFS_PMCSETUP(purr, SPRN_PURR);
  179. SYSFS_PMCSETUP(spurr, SPRN_SPURR);
  180. SYSFS_PMCSETUP(dscr, SPRN_DSCR);
  181. static SYSDEV_ATTR(mmcra, 0600, show_mmcra, store_mmcra);
  182. static SYSDEV_ATTR(spurr, 0600, show_spurr, NULL);
  183. static SYSDEV_ATTR(dscr, 0600, show_dscr, store_dscr);
  184. static SYSDEV_ATTR(purr, 0600, show_purr, store_purr);
  185. #endif /* CONFIG_PPC64 */
  186. #ifdef HAS_PPC_PMC_PA6T
  187. SYSFS_PMCSETUP(pa6t_pmc0, SPRN_PA6T_PMC0);
  188. SYSFS_PMCSETUP(pa6t_pmc1, SPRN_PA6T_PMC1);
  189. SYSFS_PMCSETUP(pa6t_pmc2, SPRN_PA6T_PMC2);
  190. SYSFS_PMCSETUP(pa6t_pmc3, SPRN_PA6T_PMC3);
  191. SYSFS_PMCSETUP(pa6t_pmc4, SPRN_PA6T_PMC4);
  192. SYSFS_PMCSETUP(pa6t_pmc5, SPRN_PA6T_PMC5);
  193. #ifdef CONFIG_DEBUG_KERNEL
  194. SYSFS_PMCSETUP(hid0, SPRN_HID0);
  195. SYSFS_PMCSETUP(hid1, SPRN_HID1);
  196. SYSFS_PMCSETUP(hid4, SPRN_HID4);
  197. SYSFS_PMCSETUP(hid5, SPRN_HID5);
  198. SYSFS_PMCSETUP(ima0, SPRN_PA6T_IMA0);
  199. SYSFS_PMCSETUP(ima1, SPRN_PA6T_IMA1);
  200. SYSFS_PMCSETUP(ima2, SPRN_PA6T_IMA2);
  201. SYSFS_PMCSETUP(ima3, SPRN_PA6T_IMA3);
  202. SYSFS_PMCSETUP(ima4, SPRN_PA6T_IMA4);
  203. SYSFS_PMCSETUP(ima5, SPRN_PA6T_IMA5);
  204. SYSFS_PMCSETUP(ima6, SPRN_PA6T_IMA6);
  205. SYSFS_PMCSETUP(ima7, SPRN_PA6T_IMA7);
  206. SYSFS_PMCSETUP(ima8, SPRN_PA6T_IMA8);
  207. SYSFS_PMCSETUP(ima9, SPRN_PA6T_IMA9);
  208. SYSFS_PMCSETUP(imaat, SPRN_PA6T_IMAAT);
  209. SYSFS_PMCSETUP(btcr, SPRN_PA6T_BTCR);
  210. SYSFS_PMCSETUP(pccr, SPRN_PA6T_PCCR);
  211. SYSFS_PMCSETUP(rpccr, SPRN_PA6T_RPCCR);
  212. SYSFS_PMCSETUP(der, SPRN_PA6T_DER);
  213. SYSFS_PMCSETUP(mer, SPRN_PA6T_MER);
  214. SYSFS_PMCSETUP(ber, SPRN_PA6T_BER);
  215. SYSFS_PMCSETUP(ier, SPRN_PA6T_IER);
  216. SYSFS_PMCSETUP(sier, SPRN_PA6T_SIER);
  217. SYSFS_PMCSETUP(siar, SPRN_PA6T_SIAR);
  218. SYSFS_PMCSETUP(tsr0, SPRN_PA6T_TSR0);
  219. SYSFS_PMCSETUP(tsr1, SPRN_PA6T_TSR1);
  220. SYSFS_PMCSETUP(tsr2, SPRN_PA6T_TSR2);
  221. SYSFS_PMCSETUP(tsr3, SPRN_PA6T_TSR3);
  222. #endif /* CONFIG_DEBUG_KERNEL */
  223. #endif /* HAS_PPC_PMC_PA6T */
  224. #ifdef HAS_PPC_PMC_IBM
  225. static struct sysdev_attribute ibm_common_attrs[] = {
  226. _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
  227. _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
  228. };
  229. #endif /* HAS_PPC_PMC_G4 */
  230. #ifdef HAS_PPC_PMC_G4
  231. static struct sysdev_attribute g4_common_attrs[] = {
  232. _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
  233. _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
  234. _SYSDEV_ATTR(mmcr2, 0600, show_mmcr2, store_mmcr2),
  235. };
  236. #endif /* HAS_PPC_PMC_G4 */
  237. static struct sysdev_attribute classic_pmc_attrs[] = {
  238. _SYSDEV_ATTR(pmc1, 0600, show_pmc1, store_pmc1),
  239. _SYSDEV_ATTR(pmc2, 0600, show_pmc2, store_pmc2),
  240. _SYSDEV_ATTR(pmc3, 0600, show_pmc3, store_pmc3),
  241. _SYSDEV_ATTR(pmc4, 0600, show_pmc4, store_pmc4),
  242. _SYSDEV_ATTR(pmc5, 0600, show_pmc5, store_pmc5),
  243. _SYSDEV_ATTR(pmc6, 0600, show_pmc6, store_pmc6),
  244. #ifdef CONFIG_PPC64
  245. _SYSDEV_ATTR(pmc7, 0600, show_pmc7, store_pmc7),
  246. _SYSDEV_ATTR(pmc8, 0600, show_pmc8, store_pmc8),
  247. #endif
  248. };
  249. #ifdef HAS_PPC_PMC_PA6T
  250. static struct sysdev_attribute pa6t_attrs[] = {
  251. _SYSDEV_ATTR(mmcr0, 0600, show_mmcr0, store_mmcr0),
  252. _SYSDEV_ATTR(mmcr1, 0600, show_mmcr1, store_mmcr1),
  253. _SYSDEV_ATTR(pmc0, 0600, show_pa6t_pmc0, store_pa6t_pmc0),
  254. _SYSDEV_ATTR(pmc1, 0600, show_pa6t_pmc1, store_pa6t_pmc1),
  255. _SYSDEV_ATTR(pmc2, 0600, show_pa6t_pmc2, store_pa6t_pmc2),
  256. _SYSDEV_ATTR(pmc3, 0600, show_pa6t_pmc3, store_pa6t_pmc3),
  257. _SYSDEV_ATTR(pmc4, 0600, show_pa6t_pmc4, store_pa6t_pmc4),
  258. _SYSDEV_ATTR(pmc5, 0600, show_pa6t_pmc5, store_pa6t_pmc5),
  259. #ifdef CONFIG_DEBUG_KERNEL
  260. _SYSDEV_ATTR(hid0, 0600, show_hid0, store_hid0),
  261. _SYSDEV_ATTR(hid1, 0600, show_hid1, store_hid1),
  262. _SYSDEV_ATTR(hid4, 0600, show_hid4, store_hid4),
  263. _SYSDEV_ATTR(hid5, 0600, show_hid5, store_hid5),
  264. _SYSDEV_ATTR(ima0, 0600, show_ima0, store_ima0),
  265. _SYSDEV_ATTR(ima1, 0600, show_ima1, store_ima1),
  266. _SYSDEV_ATTR(ima2, 0600, show_ima2, store_ima2),
  267. _SYSDEV_ATTR(ima3, 0600, show_ima3, store_ima3),
  268. _SYSDEV_ATTR(ima4, 0600, show_ima4, store_ima4),
  269. _SYSDEV_ATTR(ima5, 0600, show_ima5, store_ima5),
  270. _SYSDEV_ATTR(ima6, 0600, show_ima6, store_ima6),
  271. _SYSDEV_ATTR(ima7, 0600, show_ima7, store_ima7),
  272. _SYSDEV_ATTR(ima8, 0600, show_ima8, store_ima8),
  273. _SYSDEV_ATTR(ima9, 0600, show_ima9, store_ima9),
  274. _SYSDEV_ATTR(imaat, 0600, show_imaat, store_imaat),
  275. _SYSDEV_ATTR(btcr, 0600, show_btcr, store_btcr),
  276. _SYSDEV_ATTR(pccr, 0600, show_pccr, store_pccr),
  277. _SYSDEV_ATTR(rpccr, 0600, show_rpccr, store_rpccr),
  278. _SYSDEV_ATTR(der, 0600, show_der, store_der),
  279. _SYSDEV_ATTR(mer, 0600, show_mer, store_mer),
  280. _SYSDEV_ATTR(ber, 0600, show_ber, store_ber),
  281. _SYSDEV_ATTR(ier, 0600, show_ier, store_ier),
  282. _SYSDEV_ATTR(sier, 0600, show_sier, store_sier),
  283. _SYSDEV_ATTR(siar, 0600, show_siar, store_siar),
  284. _SYSDEV_ATTR(tsr0, 0600, show_tsr0, store_tsr0),
  285. _SYSDEV_ATTR(tsr1, 0600, show_tsr1, store_tsr1),
  286. _SYSDEV_ATTR(tsr2, 0600, show_tsr2, store_tsr2),
  287. _SYSDEV_ATTR(tsr3, 0600, show_tsr3, store_tsr3),
  288. #endif /* CONFIG_DEBUG_KERNEL */
  289. };
  290. #endif /* HAS_PPC_PMC_PA6T */
  291. #endif /* HAS_PPC_PMC_CLASSIC */
  292. struct cache_desc {
  293. struct kobject kobj;
  294. struct cache_desc *next;
  295. const char *type; /* Instruction, Data, or Unified */
  296. u32 size; /* total cache size in KB */
  297. u32 line_size; /* in bytes */
  298. u32 nr_sets; /* number of sets */
  299. u32 level; /* e.g. 1, 2, 3... */
  300. u32 associativity; /* e.g. 8-way... 0 is fully associative */
  301. };
  302. DEFINE_PER_CPU(struct cache_desc *, cache_desc);
  303. static struct cache_desc *kobj_to_cache_desc(struct kobject *k)
  304. {
  305. return container_of(k, struct cache_desc, kobj);
  306. }
  307. static void cache_desc_release(struct kobject *k)
  308. {
  309. struct cache_desc *desc = kobj_to_cache_desc(k);
  310. pr_debug("%s: releasing %s\n", __func__, kobject_name(k));
  311. if (desc->next)
  312. kobject_put(&desc->next->kobj);
  313. kfree(kobj_to_cache_desc(k));
  314. }
  315. static ssize_t cache_desc_show(struct kobject *k, struct attribute *attr, char *buf)
  316. {
  317. struct kobj_attribute *kobj_attr;
  318. kobj_attr = container_of(attr, struct kobj_attribute, attr);
  319. return kobj_attr->show(k, kobj_attr, buf);
  320. }
  321. static struct sysfs_ops cache_desc_sysfs_ops = {
  322. .show = cache_desc_show,
  323. };
  324. static struct kobj_type cache_desc_type = {
  325. .release = cache_desc_release,
  326. .sysfs_ops = &cache_desc_sysfs_ops,
  327. };
  328. static ssize_t cache_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  329. {
  330. struct cache_desc *cache = kobj_to_cache_desc(k);
  331. return sprintf(buf, "%uK\n", cache->size);
  332. }
  333. static struct kobj_attribute cache_size_attr =
  334. __ATTR(size, 0444, cache_size_show, NULL);
  335. static ssize_t cache_line_size_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  336. {
  337. struct cache_desc *cache = kobj_to_cache_desc(k);
  338. return sprintf(buf, "%u\n", cache->line_size);
  339. }
  340. static struct kobj_attribute cache_line_size_attr =
  341. __ATTR(coherency_line_size, 0444, cache_line_size_show, NULL);
  342. static ssize_t cache_nr_sets_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  343. {
  344. struct cache_desc *cache = kobj_to_cache_desc(k);
  345. return sprintf(buf, "%u\n", cache->nr_sets);
  346. }
  347. static struct kobj_attribute cache_nr_sets_attr =
  348. __ATTR(number_of_sets, 0444, cache_nr_sets_show, NULL);
  349. static ssize_t cache_type_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  350. {
  351. struct cache_desc *cache = kobj_to_cache_desc(k);
  352. return sprintf(buf, "%s\n", cache->type);
  353. }
  354. static struct kobj_attribute cache_type_attr =
  355. __ATTR(type, 0444, cache_type_show, NULL);
  356. static ssize_t cache_level_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  357. {
  358. struct cache_desc *cache = kobj_to_cache_desc(k);
  359. return sprintf(buf, "%u\n", cache->level);
  360. }
  361. static struct kobj_attribute cache_level_attr =
  362. __ATTR(level, 0444, cache_level_show, NULL);
  363. static ssize_t cache_assoc_show(struct kobject *k, struct kobj_attribute *attr, char *buf)
  364. {
  365. struct cache_desc *cache = kobj_to_cache_desc(k);
  366. return sprintf(buf, "%u\n", cache->associativity);
  367. }
  368. static struct kobj_attribute cache_assoc_attr =
  369. __ATTR(ways_of_associativity, 0444, cache_assoc_show, NULL);
  370. struct cache_desc_info {
  371. const char *type;
  372. const char *size_prop;
  373. const char *line_size_prop;
  374. const char *nr_sets_prop;
  375. };
  376. /* PowerPC Processor binding says the [di]-cache-* must be equal on
  377. * unified caches, so just use d-cache properties. */
  378. static struct cache_desc_info ucache_info = {
  379. .type = "Unified",
  380. .size_prop = "d-cache-size",
  381. .line_size_prop = "d-cache-line-size",
  382. .nr_sets_prop = "d-cache-sets",
  383. };
  384. static struct cache_desc_info dcache_info = {
  385. .type = "Data",
  386. .size_prop = "d-cache-size",
  387. .line_size_prop = "d-cache-line-size",
  388. .nr_sets_prop = "d-cache-sets",
  389. };
  390. static struct cache_desc_info icache_info = {
  391. .type = "Instruction",
  392. .size_prop = "i-cache-size",
  393. .line_size_prop = "i-cache-line-size",
  394. .nr_sets_prop = "i-cache-sets",
  395. };
  396. static struct cache_desc * __cpuinit create_cache_desc(struct device_node *np, struct kobject *parent, int index, int level, struct cache_desc_info *info)
  397. {
  398. const u32 *cache_line_size;
  399. struct cache_desc *new;
  400. const u32 *cache_size;
  401. const u32 *nr_sets;
  402. int rc;
  403. new = kzalloc(sizeof(*new), GFP_KERNEL);
  404. if (!new)
  405. return NULL;
  406. rc = kobject_init_and_add(&new->kobj, &cache_desc_type, parent,
  407. "index%d", index);
  408. if (rc)
  409. goto err;
  410. /* type */
  411. new->type = info->type;
  412. rc = sysfs_create_file(&new->kobj, &cache_type_attr.attr);
  413. WARN_ON(rc);
  414. /* level */
  415. new->level = level;
  416. rc = sysfs_create_file(&new->kobj, &cache_level_attr.attr);
  417. WARN_ON(rc);
  418. /* size */
  419. cache_size = of_get_property(np, info->size_prop, NULL);
  420. if (cache_size) {
  421. new->size = *cache_size / 1024;
  422. rc = sysfs_create_file(&new->kobj,
  423. &cache_size_attr.attr);
  424. WARN_ON(rc);
  425. }
  426. /* coherency_line_size */
  427. cache_line_size = of_get_property(np, info->line_size_prop, NULL);
  428. if (cache_line_size) {
  429. new->line_size = *cache_line_size;
  430. rc = sysfs_create_file(&new->kobj,
  431. &cache_line_size_attr.attr);
  432. WARN_ON(rc);
  433. }
  434. /* number_of_sets */
  435. nr_sets = of_get_property(np, info->nr_sets_prop, NULL);
  436. if (nr_sets) {
  437. new->nr_sets = *nr_sets;
  438. rc = sysfs_create_file(&new->kobj,
  439. &cache_nr_sets_attr.attr);
  440. WARN_ON(rc);
  441. }
  442. /* ways_of_associativity */
  443. if (new->nr_sets == 1) {
  444. /* fully associative */
  445. new->associativity = 0;
  446. goto create_assoc;
  447. }
  448. if (new->nr_sets && new->size && new->line_size) {
  449. /* If we have values for all of these we can derive
  450. * the associativity. */
  451. new->associativity =
  452. ((new->size * 1024) / new->nr_sets) / new->line_size;
  453. create_assoc:
  454. rc = sysfs_create_file(&new->kobj,
  455. &cache_assoc_attr.attr);
  456. WARN_ON(rc);
  457. }
  458. return new;
  459. err:
  460. kfree(new);
  461. return NULL;
  462. }
  463. static bool cache_is_unified(struct device_node *np)
  464. {
  465. return of_get_property(np, "cache-unified", NULL);
  466. }
  467. static struct cache_desc * __cpuinit create_cache_index_info(struct device_node *np, struct kobject *parent, int index, int level)
  468. {
  469. const phandle *next_cache_phandle;
  470. struct device_node *next_cache;
  471. struct cache_desc *new, **end;
  472. pr_debug("%s(node = %s, index = %d)\n", __func__, np->full_name, index);
  473. if (cache_is_unified(np)) {
  474. new = create_cache_desc(np, parent, index, level,
  475. &ucache_info);
  476. } else {
  477. new = create_cache_desc(np, parent, index, level,
  478. &dcache_info);
  479. if (new) {
  480. index++;
  481. new->next = create_cache_desc(np, parent, index, level,
  482. &icache_info);
  483. }
  484. }
  485. if (!new)
  486. return NULL;
  487. end = &new->next;
  488. while (*end)
  489. end = &(*end)->next;
  490. next_cache_phandle = of_get_property(np, "l2-cache", NULL);
  491. if (!next_cache_phandle)
  492. goto out;
  493. next_cache = of_find_node_by_phandle(*next_cache_phandle);
  494. if (!next_cache)
  495. goto out;
  496. *end = create_cache_index_info(next_cache, parent, ++index, ++level);
  497. of_node_put(next_cache);
  498. out:
  499. return new;
  500. }
  501. static void __cpuinit create_cache_info(struct sys_device *sysdev)
  502. {
  503. struct kobject *cache_toplevel;
  504. struct device_node *np = NULL;
  505. int cpu = sysdev->id;
  506. cache_toplevel = kobject_create_and_add("cache", &sysdev->kobj);
  507. if (!cache_toplevel)
  508. return;
  509. per_cpu(cache_toplevel, cpu) = cache_toplevel;
  510. np = of_get_cpu_node(cpu, NULL);
  511. if (np != NULL) {
  512. per_cpu(cache_desc, cpu) =
  513. create_cache_index_info(np, cache_toplevel, 0, 1);
  514. of_node_put(np);
  515. }
  516. return;
  517. }
  518. static void __cpuinit register_cpu_online(unsigned int cpu)
  519. {
  520. struct cpu *c = &per_cpu(cpu_devices, cpu);
  521. struct sys_device *s = &c->sysdev;
  522. struct sysdev_attribute *attrs, *pmc_attrs;
  523. int i, nattrs;
  524. #ifdef CONFIG_PPC64
  525. if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
  526. cpu_has_feature(CPU_FTR_SMT))
  527. sysdev_create_file(s, &attr_smt_snooze_delay);
  528. #endif
  529. /* PMC stuff */
  530. switch (cur_cpu_spec->pmc_type) {
  531. #ifdef HAS_PPC_PMC_IBM
  532. case PPC_PMC_IBM:
  533. attrs = ibm_common_attrs;
  534. nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
  535. pmc_attrs = classic_pmc_attrs;
  536. break;
  537. #endif /* HAS_PPC_PMC_IBM */
  538. #ifdef HAS_PPC_PMC_G4
  539. case PPC_PMC_G4:
  540. attrs = g4_common_attrs;
  541. nattrs = sizeof(g4_common_attrs) / sizeof(struct sysdev_attribute);
  542. pmc_attrs = classic_pmc_attrs;
  543. break;
  544. #endif /* HAS_PPC_PMC_G4 */
  545. #ifdef HAS_PPC_PMC_PA6T
  546. case PPC_PMC_PA6T:
  547. /* PA Semi starts counting at PMC0 */
  548. attrs = pa6t_attrs;
  549. nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
  550. pmc_attrs = NULL;
  551. break;
  552. #endif /* HAS_PPC_PMC_PA6T */
  553. default:
  554. attrs = NULL;
  555. nattrs = 0;
  556. pmc_attrs = NULL;
  557. }
  558. for (i = 0; i < nattrs; i++)
  559. sysdev_create_file(s, &attrs[i]);
  560. if (pmc_attrs)
  561. for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
  562. sysdev_create_file(s, &pmc_attrs[i]);
  563. #ifdef CONFIG_PPC64
  564. if (cpu_has_feature(CPU_FTR_MMCRA))
  565. sysdev_create_file(s, &attr_mmcra);
  566. if (cpu_has_feature(CPU_FTR_PURR))
  567. sysdev_create_file(s, &attr_purr);
  568. if (cpu_has_feature(CPU_FTR_SPURR))
  569. sysdev_create_file(s, &attr_spurr);
  570. if (cpu_has_feature(CPU_FTR_DSCR))
  571. sysdev_create_file(s, &attr_dscr);
  572. #endif /* CONFIG_PPC64 */
  573. create_cache_info(s);
  574. }
  575. #ifdef CONFIG_HOTPLUG_CPU
  576. static void remove_cache_info(struct sys_device *sysdev)
  577. {
  578. struct kobject *cache_toplevel;
  579. struct cache_desc *cache_desc;
  580. int cpu = sysdev->id;
  581. cache_desc = per_cpu(cache_desc, cpu);
  582. if (cache_desc != NULL)
  583. kobject_put(&cache_desc->kobj);
  584. cache_toplevel = per_cpu(cache_toplevel, cpu);
  585. if (cache_toplevel != NULL)
  586. kobject_put(cache_toplevel);
  587. }
  588. static void unregister_cpu_online(unsigned int cpu)
  589. {
  590. struct cpu *c = &per_cpu(cpu_devices, cpu);
  591. struct sys_device *s = &c->sysdev;
  592. struct sysdev_attribute *attrs, *pmc_attrs;
  593. int i, nattrs;
  594. BUG_ON(!c->hotpluggable);
  595. if (!firmware_has_feature(FW_FEATURE_ISERIES) &&
  596. cpu_has_feature(CPU_FTR_SMT))
  597. sysdev_remove_file(s, &attr_smt_snooze_delay);
  598. /* PMC stuff */
  599. switch (cur_cpu_spec->pmc_type) {
  600. #ifdef HAS_PPC_PMC_IBM
  601. case PPC_PMC_IBM:
  602. attrs = ibm_common_attrs;
  603. nattrs = sizeof(ibm_common_attrs) / sizeof(struct sysdev_attribute);
  604. pmc_attrs = classic_pmc_attrs;
  605. break;
  606. #endif /* HAS_PPC_PMC_IBM */
  607. #ifdef HAS_PPC_PMC_G4
  608. case PPC_PMC_G4:
  609. attrs = g4_common_attrs;
  610. nattrs = sizeof(g4_common_attrs) / sizeof(struct sysdev_attribute);
  611. pmc_attrs = classic_pmc_attrs;
  612. break;
  613. #endif /* HAS_PPC_PMC_G4 */
  614. #ifdef HAS_PPC_PMC_PA6T
  615. case PPC_PMC_PA6T:
  616. /* PA Semi starts counting at PMC0 */
  617. attrs = pa6t_attrs;
  618. nattrs = sizeof(pa6t_attrs) / sizeof(struct sysdev_attribute);
  619. pmc_attrs = NULL;
  620. break;
  621. #endif /* HAS_PPC_PMC_PA6T */
  622. default:
  623. attrs = NULL;
  624. nattrs = 0;
  625. pmc_attrs = NULL;
  626. }
  627. for (i = 0; i < nattrs; i++)
  628. sysdev_remove_file(s, &attrs[i]);
  629. if (pmc_attrs)
  630. for (i = 0; i < cur_cpu_spec->num_pmcs; i++)
  631. sysdev_remove_file(s, &pmc_attrs[i]);
  632. #ifdef CONFIG_PPC64
  633. if (cpu_has_feature(CPU_FTR_MMCRA))
  634. sysdev_remove_file(s, &attr_mmcra);
  635. if (cpu_has_feature(CPU_FTR_PURR))
  636. sysdev_remove_file(s, &attr_purr);
  637. if (cpu_has_feature(CPU_FTR_SPURR))
  638. sysdev_remove_file(s, &attr_spurr);
  639. if (cpu_has_feature(CPU_FTR_DSCR))
  640. sysdev_remove_file(s, &attr_dscr);
  641. #endif /* CONFIG_PPC64 */
  642. remove_cache_info(s);
  643. }
  644. #endif /* CONFIG_HOTPLUG_CPU */
  645. static int __cpuinit sysfs_cpu_notify(struct notifier_block *self,
  646. unsigned long action, void *hcpu)
  647. {
  648. unsigned int cpu = (unsigned int)(long)hcpu;
  649. switch (action) {
  650. case CPU_ONLINE:
  651. case CPU_ONLINE_FROZEN:
  652. register_cpu_online(cpu);
  653. break;
  654. #ifdef CONFIG_HOTPLUG_CPU
  655. case CPU_DEAD:
  656. case CPU_DEAD_FROZEN:
  657. unregister_cpu_online(cpu);
  658. break;
  659. #endif
  660. }
  661. return NOTIFY_OK;
  662. }
  663. static struct notifier_block __cpuinitdata sysfs_cpu_nb = {
  664. .notifier_call = sysfs_cpu_notify,
  665. };
  666. static DEFINE_MUTEX(cpu_mutex);
  667. int cpu_add_sysdev_attr(struct sysdev_attribute *attr)
  668. {
  669. int cpu;
  670. mutex_lock(&cpu_mutex);
  671. for_each_possible_cpu(cpu) {
  672. sysdev_create_file(get_cpu_sysdev(cpu), attr);
  673. }
  674. mutex_unlock(&cpu_mutex);
  675. return 0;
  676. }
  677. EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr);
  678. int cpu_add_sysdev_attr_group(struct attribute_group *attrs)
  679. {
  680. int cpu;
  681. struct sys_device *sysdev;
  682. int ret;
  683. mutex_lock(&cpu_mutex);
  684. for_each_possible_cpu(cpu) {
  685. sysdev = get_cpu_sysdev(cpu);
  686. ret = sysfs_create_group(&sysdev->kobj, attrs);
  687. WARN_ON(ret != 0);
  688. }
  689. mutex_unlock(&cpu_mutex);
  690. return 0;
  691. }
  692. EXPORT_SYMBOL_GPL(cpu_add_sysdev_attr_group);
  693. void cpu_remove_sysdev_attr(struct sysdev_attribute *attr)
  694. {
  695. int cpu;
  696. mutex_lock(&cpu_mutex);
  697. for_each_possible_cpu(cpu) {
  698. sysdev_remove_file(get_cpu_sysdev(cpu), attr);
  699. }
  700. mutex_unlock(&cpu_mutex);
  701. }
  702. EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr);
  703. void cpu_remove_sysdev_attr_group(struct attribute_group *attrs)
  704. {
  705. int cpu;
  706. struct sys_device *sysdev;
  707. mutex_lock(&cpu_mutex);
  708. for_each_possible_cpu(cpu) {
  709. sysdev = get_cpu_sysdev(cpu);
  710. sysfs_remove_group(&sysdev->kobj, attrs);
  711. }
  712. mutex_unlock(&cpu_mutex);
  713. }
  714. EXPORT_SYMBOL_GPL(cpu_remove_sysdev_attr_group);
  715. /* NUMA stuff */
  716. #ifdef CONFIG_NUMA
  717. static void register_nodes(void)
  718. {
  719. int i;
  720. for (i = 0; i < MAX_NUMNODES; i++)
  721. register_one_node(i);
  722. }
  723. int sysfs_add_device_to_node(struct sys_device *dev, int nid)
  724. {
  725. struct node *node = &node_devices[nid];
  726. return sysfs_create_link(&node->sysdev.kobj, &dev->kobj,
  727. kobject_name(&dev->kobj));
  728. }
  729. EXPORT_SYMBOL_GPL(sysfs_add_device_to_node);
  730. void sysfs_remove_device_from_node(struct sys_device *dev, int nid)
  731. {
  732. struct node *node = &node_devices[nid];
  733. sysfs_remove_link(&node->sysdev.kobj, kobject_name(&dev->kobj));
  734. }
  735. EXPORT_SYMBOL_GPL(sysfs_remove_device_from_node);
  736. #else
  737. static void register_nodes(void)
  738. {
  739. return;
  740. }
  741. #endif
  742. /* Only valid if CPU is present. */
  743. static ssize_t show_physical_id(struct sys_device *dev,
  744. struct sysdev_attribute *attr, char *buf)
  745. {
  746. struct cpu *cpu = container_of(dev, struct cpu, sysdev);
  747. return sprintf(buf, "%d\n", get_hard_smp_processor_id(cpu->sysdev.id));
  748. }
  749. static SYSDEV_ATTR(physical_id, 0444, show_physical_id, NULL);
  750. static int __init topology_init(void)
  751. {
  752. int cpu;
  753. register_nodes();
  754. register_cpu_notifier(&sysfs_cpu_nb);
  755. for_each_possible_cpu(cpu) {
  756. struct cpu *c = &per_cpu(cpu_devices, cpu);
  757. /*
  758. * For now, we just see if the system supports making
  759. * the RTAS calls for CPU hotplug. But, there may be a
  760. * more comprehensive way to do this for an individual
  761. * CPU. For instance, the boot cpu might never be valid
  762. * for hotplugging.
  763. */
  764. if (ppc_md.cpu_die)
  765. c->hotpluggable = 1;
  766. if (cpu_online(cpu) || c->hotpluggable) {
  767. register_cpu(c, cpu);
  768. sysdev_create_file(&c->sysdev, &attr_physical_id);
  769. }
  770. if (cpu_online(cpu))
  771. register_cpu_online(cpu);
  772. }
  773. return 0;
  774. }
  775. subsys_initcall(topology_init);