topology.c 10 KB

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