topology.c 10 KB

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