topology.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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] __cpuinitdata;
  120. #define LEAF_KOBJECT_PTR(x,y) (&all_cpu_cache_info[x].cache_leaves[y])
  121. #ifdef CONFIG_SMP
  122. static void __cpuinit 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. cpu_set(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. cpu_set(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 __cpuinit cache_shared_cpu_map_setup(unsigned int cpu,
  154. struct cache_info * this_leaf)
  155. {
  156. cpu_set(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. ssize_t len;
  190. cpumask_t shared_cpu_map;
  191. cpus_and(shared_cpu_map, this_leaf->shared_cpu_map, cpu_online_map);
  192. len = cpumask_scnprintf(buf, NR_CPUS+1, &shared_cpu_map);
  193. len += sprintf(buf+len, "\n");
  194. return len;
  195. }
  196. static ssize_t show_type(struct cache_info *this_leaf, char *buf)
  197. {
  198. int type = this_leaf->type + this_leaf->cci.pcci_unified;
  199. return sprintf(buf, "%s\n", cache_types[type]);
  200. }
  201. static ssize_t show_level(struct cache_info *this_leaf, char *buf)
  202. {
  203. return sprintf(buf, "%u\n", this_leaf->level);
  204. }
  205. struct cache_attr {
  206. struct attribute attr;
  207. ssize_t (*show)(struct cache_info *, char *);
  208. ssize_t (*store)(struct cache_info *, const char *, size_t count);
  209. };
  210. #ifdef define_one_ro
  211. #undef define_one_ro
  212. #endif
  213. #define define_one_ro(_name) \
  214. static struct cache_attr _name = \
  215. __ATTR(_name, 0444, show_##_name, NULL)
  216. define_one_ro(level);
  217. define_one_ro(type);
  218. define_one_ro(coherency_line_size);
  219. define_one_ro(ways_of_associativity);
  220. define_one_ro(size);
  221. define_one_ro(number_of_sets);
  222. define_one_ro(shared_cpu_map);
  223. define_one_ro(attributes);
  224. static struct attribute * cache_default_attrs[] = {
  225. &type.attr,
  226. &level.attr,
  227. &coherency_line_size.attr,
  228. &ways_of_associativity.attr,
  229. &attributes.attr,
  230. &size.attr,
  231. &number_of_sets.attr,
  232. &shared_cpu_map.attr,
  233. NULL
  234. };
  235. #define to_object(k) container_of(k, struct cache_info, kobj)
  236. #define to_attr(a) container_of(a, struct cache_attr, attr)
  237. static ssize_t cache_show(struct kobject * kobj, struct attribute * attr, char * buf)
  238. {
  239. struct cache_attr *fattr = to_attr(attr);
  240. struct cache_info *this_leaf = to_object(kobj);
  241. ssize_t ret;
  242. ret = fattr->show ? fattr->show(this_leaf, buf) : 0;
  243. return ret;
  244. }
  245. static const struct sysfs_ops cache_sysfs_ops = {
  246. .show = cache_show
  247. };
  248. static struct kobj_type cache_ktype = {
  249. .sysfs_ops = &cache_sysfs_ops,
  250. .default_attrs = cache_default_attrs,
  251. };
  252. static struct kobj_type cache_ktype_percpu_entry = {
  253. .sysfs_ops = &cache_sysfs_ops,
  254. };
  255. static void __cpuinit cpu_cache_sysfs_exit(unsigned int cpu)
  256. {
  257. kfree(all_cpu_cache_info[cpu].cache_leaves);
  258. all_cpu_cache_info[cpu].cache_leaves = NULL;
  259. all_cpu_cache_info[cpu].num_cache_leaves = 0;
  260. memset(&all_cpu_cache_info[cpu].kobj, 0, sizeof(struct kobject));
  261. return;
  262. }
  263. static int __cpuinit cpu_cache_sysfs_init(unsigned int cpu)
  264. {
  265. unsigned long i, levels, unique_caches;
  266. pal_cache_config_info_t cci;
  267. int j;
  268. long status;
  269. struct cache_info *this_cache;
  270. int num_cache_leaves = 0;
  271. if ((status = ia64_pal_cache_summary(&levels, &unique_caches)) != 0) {
  272. printk(KERN_ERR "ia64_pal_cache_summary=%ld\n", status);
  273. return -1;
  274. }
  275. this_cache=kzalloc(sizeof(struct cache_info)*unique_caches,
  276. GFP_KERNEL);
  277. if (this_cache == NULL)
  278. return -ENOMEM;
  279. for (i=0; i < levels; i++) {
  280. for (j=2; j >0 ; j--) {
  281. if ((status=ia64_pal_cache_config_info(i,j, &cci)) !=
  282. PAL_STATUS_SUCCESS)
  283. continue;
  284. this_cache[num_cache_leaves].cci = cci;
  285. this_cache[num_cache_leaves].level = i + 1;
  286. this_cache[num_cache_leaves].type = j;
  287. cache_shared_cpu_map_setup(cpu,
  288. &this_cache[num_cache_leaves]);
  289. num_cache_leaves ++;
  290. }
  291. }
  292. all_cpu_cache_info[cpu].cache_leaves = this_cache;
  293. all_cpu_cache_info[cpu].num_cache_leaves = num_cache_leaves;
  294. memset(&all_cpu_cache_info[cpu].kobj, 0, sizeof(struct kobject));
  295. return 0;
  296. }
  297. /* Add cache interface for CPU device */
  298. static int __cpuinit cache_add_dev(struct sys_device * sys_dev)
  299. {
  300. unsigned int cpu = sys_dev->id;
  301. unsigned long i, j;
  302. struct cache_info *this_object;
  303. int retval = 0;
  304. cpumask_t oldmask;
  305. if (all_cpu_cache_info[cpu].kobj.parent)
  306. return 0;
  307. oldmask = current->cpus_allowed;
  308. retval = set_cpus_allowed_ptr(current, cpumask_of(cpu));
  309. if (unlikely(retval))
  310. return retval;
  311. retval = cpu_cache_sysfs_init(cpu);
  312. set_cpus_allowed_ptr(current, &oldmask);
  313. if (unlikely(retval < 0))
  314. return retval;
  315. retval = kobject_init_and_add(&all_cpu_cache_info[cpu].kobj,
  316. &cache_ktype_percpu_entry, &sys_dev->kobj,
  317. "%s", "cache");
  318. if (unlikely(retval < 0)) {
  319. cpu_cache_sysfs_exit(cpu);
  320. return retval;
  321. }
  322. for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++) {
  323. this_object = LEAF_KOBJECT_PTR(cpu,i);
  324. retval = kobject_init_and_add(&(this_object->kobj),
  325. &cache_ktype,
  326. &all_cpu_cache_info[cpu].kobj,
  327. "index%1lu", i);
  328. if (unlikely(retval)) {
  329. for (j = 0; j < i; j++) {
  330. kobject_put(&(LEAF_KOBJECT_PTR(cpu,j)->kobj));
  331. }
  332. kobject_put(&all_cpu_cache_info[cpu].kobj);
  333. cpu_cache_sysfs_exit(cpu);
  334. return retval;
  335. }
  336. kobject_uevent(&(this_object->kobj), KOBJ_ADD);
  337. }
  338. kobject_uevent(&all_cpu_cache_info[cpu].kobj, KOBJ_ADD);
  339. return retval;
  340. }
  341. /* Remove cache interface for CPU device */
  342. static int __cpuinit cache_remove_dev(struct sys_device * sys_dev)
  343. {
  344. unsigned int cpu = sys_dev->id;
  345. unsigned long i;
  346. for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++)
  347. kobject_put(&(LEAF_KOBJECT_PTR(cpu,i)->kobj));
  348. if (all_cpu_cache_info[cpu].kobj.parent) {
  349. kobject_put(&all_cpu_cache_info[cpu].kobj);
  350. memset(&all_cpu_cache_info[cpu].kobj,
  351. 0,
  352. sizeof(struct kobject));
  353. }
  354. cpu_cache_sysfs_exit(cpu);
  355. return 0;
  356. }
  357. /*
  358. * When a cpu is hot-plugged, do a check and initiate
  359. * cache kobject if necessary
  360. */
  361. static int __cpuinit cache_cpu_callback(struct notifier_block *nfb,
  362. unsigned long action, void *hcpu)
  363. {
  364. unsigned int cpu = (unsigned long)hcpu;
  365. struct sys_device *sys_dev;
  366. sys_dev = get_cpu_sysdev(cpu);
  367. switch (action) {
  368. case CPU_ONLINE:
  369. case CPU_ONLINE_FROZEN:
  370. cache_add_dev(sys_dev);
  371. break;
  372. case CPU_DEAD:
  373. case CPU_DEAD_FROZEN:
  374. cache_remove_dev(sys_dev);
  375. break;
  376. }
  377. return NOTIFY_OK;
  378. }
  379. static struct notifier_block __cpuinitdata cache_cpu_notifier =
  380. {
  381. .notifier_call = cache_cpu_callback
  382. };
  383. static int __init cache_sysfs_init(void)
  384. {
  385. int i;
  386. for_each_online_cpu(i) {
  387. struct sys_device *sys_dev = get_cpu_sysdev((unsigned int)i);
  388. cache_add_dev(sys_dev);
  389. }
  390. register_hotcpu_notifier(&cache_cpu_notifier);
  391. return 0;
  392. }
  393. device_initcall(cache_sysfs_init);