topology.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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 <asm/mmzone.h>
  25. #include <asm/numa.h>
  26. #include <asm/cpu.h>
  27. static struct ia64_cpu *sysfs_cpus;
  28. void arch_fix_phys_package_id(int num, u32 slot)
  29. {
  30. #ifdef CONFIG_SMP
  31. if (cpu_data(num)->socket_id == -1)
  32. cpu_data(num)->socket_id = slot;
  33. #endif
  34. }
  35. EXPORT_SYMBOL_GPL(arch_fix_phys_package_id);
  36. #ifdef CONFIG_HOTPLUG_CPU
  37. int __ref arch_register_cpu(int num)
  38. {
  39. #ifdef CONFIG_ACPI
  40. /*
  41. * If CPEI can be re-targetted or if this is not
  42. * CPEI target, then it is hotpluggable
  43. */
  44. if (can_cpei_retarget() || !is_cpu_cpei_target(num))
  45. sysfs_cpus[num].cpu.hotpluggable = 1;
  46. map_cpu_to_node(num, node_cpuid[num].nid);
  47. #endif
  48. return register_cpu(&sysfs_cpus[num].cpu, num);
  49. }
  50. EXPORT_SYMBOL(arch_register_cpu);
  51. void __ref arch_unregister_cpu(int num)
  52. {
  53. unregister_cpu(&sysfs_cpus[num].cpu);
  54. #ifdef CONFIG_ACPI
  55. unmap_cpu_from_node(num, cpu_to_node(num));
  56. #endif
  57. }
  58. EXPORT_SYMBOL(arch_unregister_cpu);
  59. #else
  60. static int __init arch_register_cpu(int num)
  61. {
  62. return register_cpu(&sysfs_cpus[num].cpu, num);
  63. }
  64. #endif /*CONFIG_HOTPLUG_CPU*/
  65. static int __init topology_init(void)
  66. {
  67. int i, err = 0;
  68. #ifdef CONFIG_NUMA
  69. /*
  70. * MCD - Do we want to register all ONLINE nodes, or all POSSIBLE nodes?
  71. */
  72. for_each_online_node(i) {
  73. if ((err = register_one_node(i)))
  74. goto out;
  75. }
  76. #endif
  77. sysfs_cpus = kzalloc(sizeof(struct ia64_cpu) * NR_CPUS, GFP_KERNEL);
  78. if (!sysfs_cpus)
  79. panic("kzalloc in topology_init failed - NR_CPUS too big?");
  80. for_each_present_cpu(i) {
  81. if((err = arch_register_cpu(i)))
  82. goto out;
  83. }
  84. out:
  85. return err;
  86. }
  87. subsys_initcall(topology_init);
  88. /*
  89. * Export cpu cache information through sysfs
  90. */
  91. /*
  92. * A bunch of string array to get pretty printing
  93. */
  94. static const char *cache_types[] = {
  95. "", /* not used */
  96. "Instruction",
  97. "Data",
  98. "Unified" /* unified */
  99. };
  100. static const char *cache_mattrib[]={
  101. "WriteThrough",
  102. "WriteBack",
  103. "", /* reserved */
  104. "" /* reserved */
  105. };
  106. struct cache_info {
  107. pal_cache_config_info_t cci;
  108. cpumask_t shared_cpu_map;
  109. int level;
  110. int type;
  111. struct kobject kobj;
  112. };
  113. struct cpu_cache_info {
  114. struct cache_info *cache_leaves;
  115. int num_cache_leaves;
  116. struct kobject kobj;
  117. };
  118. static struct cpu_cache_info all_cpu_cache_info[NR_CPUS] __cpuinitdata;
  119. #define LEAF_KOBJECT_PTR(x,y) (&all_cpu_cache_info[x].cache_leaves[y])
  120. #ifdef CONFIG_SMP
  121. static void __cpuinit cache_shared_cpu_map_setup( unsigned int cpu,
  122. struct cache_info * this_leaf)
  123. {
  124. pal_cache_shared_info_t csi;
  125. int num_shared, i = 0;
  126. unsigned int j;
  127. if (cpu_data(cpu)->threads_per_core <= 1 &&
  128. cpu_data(cpu)->cores_per_socket <= 1) {
  129. cpu_set(cpu, this_leaf->shared_cpu_map);
  130. return;
  131. }
  132. if (ia64_pal_cache_shared_info(this_leaf->level,
  133. this_leaf->type,
  134. 0,
  135. &csi) != PAL_STATUS_SUCCESS)
  136. return;
  137. num_shared = (int) csi.num_shared;
  138. do {
  139. for_each_possible_cpu(j)
  140. if (cpu_data(cpu)->socket_id == cpu_data(j)->socket_id
  141. && cpu_data(j)->core_id == csi.log1_cid
  142. && cpu_data(j)->thread_id == csi.log1_tid)
  143. cpu_set(j, this_leaf->shared_cpu_map);
  144. i++;
  145. } while (i < num_shared &&
  146. ia64_pal_cache_shared_info(this_leaf->level,
  147. this_leaf->type,
  148. i,
  149. &csi) == PAL_STATUS_SUCCESS);
  150. }
  151. #else
  152. static void __cpuinit cache_shared_cpu_map_setup(unsigned int cpu,
  153. struct cache_info * this_leaf)
  154. {
  155. cpu_set(cpu, this_leaf->shared_cpu_map);
  156. return;
  157. }
  158. #endif
  159. static ssize_t show_coherency_line_size(struct cache_info *this_leaf,
  160. char *buf)
  161. {
  162. return sprintf(buf, "%u\n", 1 << this_leaf->cci.pcci_line_size);
  163. }
  164. static ssize_t show_ways_of_associativity(struct cache_info *this_leaf,
  165. char *buf)
  166. {
  167. return sprintf(buf, "%u\n", this_leaf->cci.pcci_assoc);
  168. }
  169. static ssize_t show_attributes(struct cache_info *this_leaf, char *buf)
  170. {
  171. return sprintf(buf,
  172. "%s\n",
  173. cache_mattrib[this_leaf->cci.pcci_cache_attr]);
  174. }
  175. static ssize_t show_size(struct cache_info *this_leaf, char *buf)
  176. {
  177. return sprintf(buf, "%uK\n", this_leaf->cci.pcci_cache_size / 1024);
  178. }
  179. static ssize_t show_number_of_sets(struct cache_info *this_leaf, char *buf)
  180. {
  181. unsigned number_of_sets = this_leaf->cci.pcci_cache_size;
  182. number_of_sets /= this_leaf->cci.pcci_assoc;
  183. number_of_sets /= 1 << this_leaf->cci.pcci_line_size;
  184. return sprintf(buf, "%u\n", number_of_sets);
  185. }
  186. static ssize_t show_shared_cpu_map(struct cache_info *this_leaf, char *buf)
  187. {
  188. ssize_t len;
  189. cpumask_t shared_cpu_map;
  190. cpus_and(shared_cpu_map, this_leaf->shared_cpu_map, cpu_online_map);
  191. len = cpumask_scnprintf(buf, NR_CPUS+1, &shared_cpu_map);
  192. len += sprintf(buf+len, "\n");
  193. return len;
  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 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 = 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 __cpuinit 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 __cpuinit 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 __cpuinit cache_add_dev(struct sys_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 __cpuinit cache_remove_dev(struct sys_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 __cpuinit cache_cpu_callback(struct notifier_block *nfb,
  361. unsigned long action, void *hcpu)
  362. {
  363. unsigned int cpu = (unsigned long)hcpu;
  364. struct sys_device *sys_dev;
  365. sys_dev = get_cpu_sysdev(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 __cpuinitdata cache_cpu_notifier =
  379. {
  380. .notifier_call = cache_cpu_callback
  381. };
  382. static int __init cache_sysfs_init(void)
  383. {
  384. int i;
  385. for_each_online_cpu(i) {
  386. struct sys_device *sys_dev = get_cpu_sysdev((unsigned int)i);
  387. cache_add_dev(sys_dev);
  388. }
  389. register_hotcpu_notifier(&cache_cpu_notifier);
  390. return 0;
  391. }
  392. device_initcall(cache_sysfs_init);