topology.c 11 KB

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