topology.c 10 KB

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