topology.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * This file contains NUMA specific variables and functions which can
  7. * be split away from DISCONTIGMEM and are used on NUMA machines with
  8. * contiguous memory.
  9. * 2002/08/07 Erich Focht <efocht@ess.nec.de>
  10. * Populate cpu entries in sysfs for non-numa systems as well
  11. * Intel Corporation - Ashok Raj
  12. * 02/27/2006 Zhang, Yanmin
  13. * Populate cpu cache entries in sysfs for cpu cache info
  14. */
  15. #include <linux/cpu.h>
  16. #include <linux/kernel.h>
  17. #include <linux/mm.h>
  18. #include <linux/node.h>
  19. #include <linux/slab.h>
  20. #include <linux/init.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/nodemask.h>
  23. #include <linux/notifier.h>
  24. #include <linux/export.h>
  25. #include <asm/mmzone.h>
  26. #include <asm/numa.h>
  27. #include <asm/cpu.h>
  28. static struct ia64_cpu *sysfs_cpus;
  29. void arch_fix_phys_package_id(int num, u32 slot)
  30. {
  31. #ifdef CONFIG_SMP
  32. if (cpu_data(num)->socket_id == -1)
  33. cpu_data(num)->socket_id = slot;
  34. #endif
  35. }
  36. EXPORT_SYMBOL_GPL(arch_fix_phys_package_id);
  37. #ifdef CONFIG_HOTPLUG_CPU
  38. int __ref arch_register_cpu(int num)
  39. {
  40. #ifdef CONFIG_ACPI
  41. /*
  42. * If CPEI can be re-targeted or if this is not
  43. * CPEI target, then it is hotpluggable
  44. */
  45. if (can_cpei_retarget() || !is_cpu_cpei_target(num))
  46. sysfs_cpus[num].cpu.hotpluggable = 1;
  47. map_cpu_to_node(num, node_cpuid[num].nid);
  48. #endif
  49. return register_cpu(&sysfs_cpus[num].cpu, num);
  50. }
  51. EXPORT_SYMBOL(arch_register_cpu);
  52. void __ref arch_unregister_cpu(int num)
  53. {
  54. unregister_cpu(&sysfs_cpus[num].cpu);
  55. #ifdef CONFIG_ACPI
  56. unmap_cpu_from_node(num, cpu_to_node(num));
  57. #endif
  58. }
  59. EXPORT_SYMBOL(arch_unregister_cpu);
  60. #else
  61. static int __init arch_register_cpu(int num)
  62. {
  63. return register_cpu(&sysfs_cpus[num].cpu, num);
  64. }
  65. #endif /*CONFIG_HOTPLUG_CPU*/
  66. static int __init topology_init(void)
  67. {
  68. int i, err = 0;
  69. #ifdef CONFIG_NUMA
  70. /*
  71. * MCD - Do we want to register all ONLINE nodes, or all POSSIBLE nodes?
  72. */
  73. for_each_online_node(i) {
  74. if ((err = register_one_node(i)))
  75. goto out;
  76. }
  77. #endif
  78. sysfs_cpus = kzalloc(sizeof(struct ia64_cpu) * NR_CPUS, GFP_KERNEL);
  79. if (!sysfs_cpus)
  80. panic("kzalloc in topology_init failed - NR_CPUS too big?");
  81. for_each_present_cpu(i) {
  82. if((err = arch_register_cpu(i)))
  83. goto out;
  84. }
  85. out:
  86. return err;
  87. }
  88. subsys_initcall(topology_init);
  89. /*
  90. * Export cpu cache information through sysfs
  91. */
  92. /*
  93. * A bunch of string array to get pretty printing
  94. */
  95. static const char *cache_types[] = {
  96. "", /* not used */
  97. "Instruction",
  98. "Data",
  99. "Unified" /* unified */
  100. };
  101. static const char *cache_mattrib[]={
  102. "WriteThrough",
  103. "WriteBack",
  104. "", /* reserved */
  105. "" /* reserved */
  106. };
  107. struct cache_info {
  108. pal_cache_config_info_t cci;
  109. cpumask_t shared_cpu_map;
  110. int level;
  111. int type;
  112. struct kobject kobj;
  113. };
  114. struct cpu_cache_info {
  115. struct cache_info *cache_leaves;
  116. int num_cache_leaves;
  117. struct kobject kobj;
  118. };
  119. static struct cpu_cache_info all_cpu_cache_info[NR_CPUS];
  120. #define LEAF_KOBJECT_PTR(x,y) (&all_cpu_cache_info[x].cache_leaves[y])
  121. #ifdef CONFIG_SMP
  122. static void cache_shared_cpu_map_setup(unsigned int cpu,
  123. struct cache_info * this_leaf)
  124. {
  125. pal_cache_shared_info_t csi;
  126. int num_shared, i = 0;
  127. unsigned int j;
  128. if (cpu_data(cpu)->threads_per_core <= 1 &&
  129. cpu_data(cpu)->cores_per_socket <= 1) {
  130. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  131. return;
  132. }
  133. if (ia64_pal_cache_shared_info(this_leaf->level,
  134. this_leaf->type,
  135. 0,
  136. &csi) != PAL_STATUS_SUCCESS)
  137. return;
  138. num_shared = (int) csi.num_shared;
  139. do {
  140. for_each_possible_cpu(j)
  141. if (cpu_data(cpu)->socket_id == cpu_data(j)->socket_id
  142. && cpu_data(j)->core_id == csi.log1_cid
  143. && cpu_data(j)->thread_id == csi.log1_tid)
  144. cpumask_set_cpu(j, &this_leaf->shared_cpu_map);
  145. i++;
  146. } while (i < num_shared &&
  147. ia64_pal_cache_shared_info(this_leaf->level,
  148. this_leaf->type,
  149. i,
  150. &csi) == PAL_STATUS_SUCCESS);
  151. }
  152. #else
  153. static void cache_shared_cpu_map_setup(unsigned int cpu,
  154. struct cache_info * this_leaf)
  155. {
  156. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  157. return;
  158. }
  159. #endif
  160. static ssize_t show_coherency_line_size(struct cache_info *this_leaf,
  161. char *buf)
  162. {
  163. return sprintf(buf, "%u\n", 1 << this_leaf->cci.pcci_line_size);
  164. }
  165. static ssize_t show_ways_of_associativity(struct cache_info *this_leaf,
  166. char *buf)
  167. {
  168. return sprintf(buf, "%u\n", this_leaf->cci.pcci_assoc);
  169. }
  170. static ssize_t show_attributes(struct cache_info *this_leaf, char *buf)
  171. {
  172. return sprintf(buf,
  173. "%s\n",
  174. cache_mattrib[this_leaf->cci.pcci_cache_attr]);
  175. }
  176. static ssize_t show_size(struct cache_info *this_leaf, char *buf)
  177. {
  178. return sprintf(buf, "%uK\n", this_leaf->cci.pcci_cache_size / 1024);
  179. }
  180. static ssize_t show_number_of_sets(struct cache_info *this_leaf, char *buf)
  181. {
  182. unsigned number_of_sets = this_leaf->cci.pcci_cache_size;
  183. number_of_sets /= this_leaf->cci.pcci_assoc;
  184. number_of_sets /= 1 << this_leaf->cci.pcci_line_size;
  185. return sprintf(buf, "%u\n", number_of_sets);
  186. }
  187. static ssize_t show_shared_cpu_map(struct cache_info *this_leaf, char *buf)
  188. {
  189. cpumask_t shared_cpu_map;
  190. cpumask_and(&shared_cpu_map,
  191. &this_leaf->shared_cpu_map, cpu_online_mask);
  192. return scnprintf(buf, PAGE_SIZE, "%*pb\n",
  193. cpumask_pr_args(&shared_cpu_map));
  194. }
  195. static ssize_t show_type(struct cache_info *this_leaf, char *buf)
  196. {
  197. int type = this_leaf->type + this_leaf->cci.pcci_unified;
  198. return sprintf(buf, "%s\n", cache_types[type]);
  199. }
  200. static ssize_t show_level(struct cache_info *this_leaf, char *buf)
  201. {
  202. return sprintf(buf, "%u\n", this_leaf->level);
  203. }
  204. struct cache_attr {
  205. struct attribute attr;
  206. ssize_t (*show)(struct cache_info *, char *);
  207. ssize_t (*store)(struct cache_info *, const char *, size_t count);
  208. };
  209. #ifdef define_one_ro
  210. #undef define_one_ro
  211. #endif
  212. #define define_one_ro(_name) \
  213. static struct cache_attr _name = \
  214. __ATTR(_name, 0444, show_##_name, NULL)
  215. define_one_ro(level);
  216. define_one_ro(type);
  217. define_one_ro(coherency_line_size);
  218. define_one_ro(ways_of_associativity);
  219. define_one_ro(size);
  220. define_one_ro(number_of_sets);
  221. define_one_ro(shared_cpu_map);
  222. define_one_ro(attributes);
  223. static struct attribute * cache_default_attrs[] = {
  224. &type.attr,
  225. &level.attr,
  226. &coherency_line_size.attr,
  227. &ways_of_associativity.attr,
  228. &attributes.attr,
  229. &size.attr,
  230. &number_of_sets.attr,
  231. &shared_cpu_map.attr,
  232. NULL
  233. };
  234. #define to_object(k) container_of(k, struct cache_info, kobj)
  235. #define to_attr(a) container_of(a, struct cache_attr, attr)
  236. static ssize_t ia64_cache_show(struct kobject * kobj, struct attribute * attr, char * buf)
  237. {
  238. struct cache_attr *fattr = to_attr(attr);
  239. struct cache_info *this_leaf = to_object(kobj);
  240. ssize_t ret;
  241. ret = fattr->show ? fattr->show(this_leaf, buf) : 0;
  242. return ret;
  243. }
  244. static const struct sysfs_ops cache_sysfs_ops = {
  245. .show = ia64_cache_show
  246. };
  247. static struct kobj_type cache_ktype = {
  248. .sysfs_ops = &cache_sysfs_ops,
  249. .default_attrs = cache_default_attrs,
  250. };
  251. static struct kobj_type cache_ktype_percpu_entry = {
  252. .sysfs_ops = &cache_sysfs_ops,
  253. };
  254. static void cpu_cache_sysfs_exit(unsigned int cpu)
  255. {
  256. kfree(all_cpu_cache_info[cpu].cache_leaves);
  257. all_cpu_cache_info[cpu].cache_leaves = NULL;
  258. all_cpu_cache_info[cpu].num_cache_leaves = 0;
  259. memset(&all_cpu_cache_info[cpu].kobj, 0, sizeof(struct kobject));
  260. return;
  261. }
  262. static int cpu_cache_sysfs_init(unsigned int cpu)
  263. {
  264. unsigned long i, levels, unique_caches;
  265. pal_cache_config_info_t cci;
  266. int j;
  267. long status;
  268. struct cache_info *this_cache;
  269. int num_cache_leaves = 0;
  270. if ((status = ia64_pal_cache_summary(&levels, &unique_caches)) != 0) {
  271. printk(KERN_ERR "ia64_pal_cache_summary=%ld\n", status);
  272. return -1;
  273. }
  274. this_cache=kzalloc(sizeof(struct cache_info)*unique_caches,
  275. GFP_KERNEL);
  276. if (this_cache == NULL)
  277. return -ENOMEM;
  278. for (i=0; i < levels; i++) {
  279. for (j=2; j >0 ; j--) {
  280. if ((status=ia64_pal_cache_config_info(i,j, &cci)) !=
  281. PAL_STATUS_SUCCESS)
  282. continue;
  283. this_cache[num_cache_leaves].cci = cci;
  284. this_cache[num_cache_leaves].level = i + 1;
  285. this_cache[num_cache_leaves].type = j;
  286. cache_shared_cpu_map_setup(cpu,
  287. &this_cache[num_cache_leaves]);
  288. num_cache_leaves ++;
  289. }
  290. }
  291. all_cpu_cache_info[cpu].cache_leaves = this_cache;
  292. all_cpu_cache_info[cpu].num_cache_leaves = num_cache_leaves;
  293. memset(&all_cpu_cache_info[cpu].kobj, 0, sizeof(struct kobject));
  294. return 0;
  295. }
  296. /* Add cache interface for CPU device */
  297. static int cache_add_dev(struct device *sys_dev)
  298. {
  299. unsigned int cpu = sys_dev->id;
  300. unsigned long i, j;
  301. struct cache_info *this_object;
  302. int retval = 0;
  303. cpumask_t oldmask;
  304. if (all_cpu_cache_info[cpu].kobj.parent)
  305. return 0;
  306. oldmask = current->cpus_allowed;
  307. retval = set_cpus_allowed_ptr(current, cpumask_of(cpu));
  308. if (unlikely(retval))
  309. return retval;
  310. retval = cpu_cache_sysfs_init(cpu);
  311. set_cpus_allowed_ptr(current, &oldmask);
  312. if (unlikely(retval < 0))
  313. return retval;
  314. retval = kobject_init_and_add(&all_cpu_cache_info[cpu].kobj,
  315. &cache_ktype_percpu_entry, &sys_dev->kobj,
  316. "%s", "cache");
  317. if (unlikely(retval < 0)) {
  318. cpu_cache_sysfs_exit(cpu);
  319. return retval;
  320. }
  321. for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++) {
  322. this_object = LEAF_KOBJECT_PTR(cpu,i);
  323. retval = kobject_init_and_add(&(this_object->kobj),
  324. &cache_ktype,
  325. &all_cpu_cache_info[cpu].kobj,
  326. "index%1lu", i);
  327. if (unlikely(retval)) {
  328. for (j = 0; j < i; j++) {
  329. kobject_put(&(LEAF_KOBJECT_PTR(cpu,j)->kobj));
  330. }
  331. kobject_put(&all_cpu_cache_info[cpu].kobj);
  332. cpu_cache_sysfs_exit(cpu);
  333. return retval;
  334. }
  335. kobject_uevent(&(this_object->kobj), KOBJ_ADD);
  336. }
  337. kobject_uevent(&all_cpu_cache_info[cpu].kobj, KOBJ_ADD);
  338. return retval;
  339. }
  340. /* Remove cache interface for CPU device */
  341. static int cache_remove_dev(struct device *sys_dev)
  342. {
  343. unsigned int cpu = sys_dev->id;
  344. unsigned long i;
  345. for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++)
  346. kobject_put(&(LEAF_KOBJECT_PTR(cpu,i)->kobj));
  347. if (all_cpu_cache_info[cpu].kobj.parent) {
  348. kobject_put(&all_cpu_cache_info[cpu].kobj);
  349. memset(&all_cpu_cache_info[cpu].kobj,
  350. 0,
  351. sizeof(struct kobject));
  352. }
  353. cpu_cache_sysfs_exit(cpu);
  354. return 0;
  355. }
  356. /*
  357. * When a cpu is hot-plugged, do a check and initiate
  358. * cache kobject if necessary
  359. */
  360. static int cache_cpu_callback(struct notifier_block *nfb,
  361. unsigned long action, void *hcpu)
  362. {
  363. unsigned int cpu = (unsigned long)hcpu;
  364. struct device *sys_dev;
  365. sys_dev = get_cpu_device(cpu);
  366. switch (action) {
  367. case CPU_ONLINE:
  368. case CPU_ONLINE_FROZEN:
  369. cache_add_dev(sys_dev);
  370. break;
  371. case CPU_DEAD:
  372. case CPU_DEAD_FROZEN:
  373. cache_remove_dev(sys_dev);
  374. break;
  375. }
  376. return NOTIFY_OK;
  377. }
  378. static struct notifier_block cache_cpu_notifier =
  379. {
  380. .notifier_call = cache_cpu_callback
  381. };
  382. static int __init cache_sysfs_init(void)
  383. {
  384. int i;
  385. cpu_notifier_register_begin();
  386. for_each_online_cpu(i) {
  387. struct device *sys_dev = get_cpu_device((unsigned int)i);
  388. cache_add_dev(sys_dev);
  389. }
  390. __register_hotcpu_notifier(&cache_cpu_notifier);
  391. cpu_notifier_register_done();
  392. return 0;
  393. }
  394. device_initcall(cache_sysfs_init);