sysfs.c 22 KB

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