topology.c 10 KB

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