topology.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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/slab.h>
  20. #include <linux/init.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/nodemask.h>
  23. #include <linux/notifier.h>
  24. #include <linux/export.h>
  25. #include <asm/mmzone.h>
  26. #include <asm/numa.h>
  27. #include <asm/cpu.h>
  28. static struct ia64_cpu *sysfs_cpus;
  29. void arch_fix_phys_package_id(int num, u32 slot)
  30. {
  31. #ifdef CONFIG_SMP
  32. if (cpu_data(num)->socket_id == -1)
  33. cpu_data(num)->socket_id = slot;
  34. #endif
  35. }
  36. EXPORT_SYMBOL_GPL(arch_fix_phys_package_id);
  37. #ifdef CONFIG_HOTPLUG_CPU
  38. int __ref arch_register_cpu(int num)
  39. {
  40. #ifdef CONFIG_ACPI
  41. /*
  42. * If CPEI can be re-targeted or if this is not
  43. * CPEI target, then it is hotpluggable
  44. */
  45. if (can_cpei_retarget() || !is_cpu_cpei_target(num))
  46. sysfs_cpus[num].cpu.hotpluggable = 1;
  47. map_cpu_to_node(num, node_cpuid[num].nid);
  48. #endif
  49. return register_cpu(&sysfs_cpus[num].cpu, num);
  50. }
  51. EXPORT_SYMBOL(arch_register_cpu);
  52. void __ref arch_unregister_cpu(int num)
  53. {
  54. unregister_cpu(&sysfs_cpus[num].cpu);
  55. #ifdef CONFIG_ACPI
  56. unmap_cpu_from_node(num, cpu_to_node(num));
  57. #endif
  58. }
  59. EXPORT_SYMBOL(arch_unregister_cpu);
  60. #else
  61. static int __init arch_register_cpu(int num)
  62. {
  63. return register_cpu(&sysfs_cpus[num].cpu, num);
  64. }
  65. #endif /*CONFIG_HOTPLUG_CPU*/
  66. static int __init topology_init(void)
  67. {
  68. int i, err = 0;
  69. #ifdef CONFIG_NUMA
  70. /*
  71. * MCD - Do we want to register all ONLINE nodes, or all POSSIBLE nodes?
  72. */
  73. for_each_online_node(i) {
  74. if ((err = register_one_node(i)))
  75. goto out;
  76. }
  77. #endif
  78. sysfs_cpus = kzalloc(sizeof(struct ia64_cpu) * NR_CPUS, GFP_KERNEL);
  79. if (!sysfs_cpus)
  80. panic("kzalloc in topology_init failed - NR_CPUS too big?");
  81. for_each_present_cpu(i) {
  82. if((err = arch_register_cpu(i)))
  83. goto out;
  84. }
  85. out:
  86. return err;
  87. }
  88. subsys_initcall(topology_init);
  89. /*
  90. * Export cpu cache information through sysfs
  91. */
  92. /*
  93. * A bunch of string array to get pretty printing
  94. */
  95. static const char *cache_types[] = {
  96. "", /* not used */
  97. "Instruction",
  98. "Data",
  99. "Unified" /* unified */
  100. };
  101. static const char *cache_mattrib[]={
  102. "WriteThrough",
  103. "WriteBack",
  104. "", /* reserved */
  105. "" /* reserved */
  106. };
  107. struct cache_info {
  108. pal_cache_config_info_t cci;
  109. cpumask_t shared_cpu_map;
  110. int level;
  111. int type;
  112. struct kobject kobj;
  113. };
  114. struct cpu_cache_info {
  115. struct cache_info *cache_leaves;
  116. int num_cache_leaves;
  117. struct kobject kobj;
  118. };
  119. static struct cpu_cache_info all_cpu_cache_info[NR_CPUS] __cpuinitdata;
  120. #define LEAF_KOBJECT_PTR(x,y) (&all_cpu_cache_info[x].cache_leaves[y])
  121. #ifdef CONFIG_SMP
  122. static void __cpuinit cache_shared_cpu_map_setup( unsigned int cpu,
  123. struct cache_info * this_leaf)
  124. {
  125. pal_cache_shared_info_t csi;
  126. int num_shared, i = 0;
  127. unsigned int j;
  128. if (cpu_data(cpu)->threads_per_core <= 1 &&
  129. cpu_data(cpu)->cores_per_socket <= 1) {
  130. cpu_set(cpu, this_leaf->shared_cpu_map);
  131. return;
  132. }
  133. if (ia64_pal_cache_shared_info(this_leaf->level,
  134. this_leaf->type,
  135. 0,
  136. &csi) != PAL_STATUS_SUCCESS)
  137. return;
  138. num_shared = (int) csi.num_shared;
  139. do {
  140. for_each_possible_cpu(j)
  141. if (cpu_data(cpu)->socket_id == cpu_data(j)->socket_id
  142. && cpu_data(j)->core_id == csi.log1_cid
  143. && cpu_data(j)->thread_id == csi.log1_tid)
  144. cpu_set(j, this_leaf->shared_cpu_map);
  145. i++;
  146. } while (i < num_shared &&
  147. ia64_pal_cache_shared_info(this_leaf->level,
  148. this_leaf->type,
  149. i,
  150. &csi) == PAL_STATUS_SUCCESS);
  151. }
  152. #else
  153. static void __cpuinit cache_shared_cpu_map_setup(unsigned int cpu,
  154. struct cache_info * this_leaf)
  155. {
  156. cpu_set(cpu, this_leaf->shared_cpu_map);
  157. return;
  158. }
  159. #endif
  160. static ssize_t show_coherency_line_size(struct cache_info *this_leaf,
  161. char *buf)
  162. {
  163. return sprintf(buf, "%u\n", 1 << this_leaf->cci.pcci_line_size);
  164. }
  165. static ssize_t show_ways_of_associativity(struct cache_info *this_leaf,
  166. char *buf)
  167. {
  168. return sprintf(buf, "%u\n", this_leaf->cci.pcci_assoc);
  169. }
  170. static ssize_t show_attributes(struct cache_info *this_leaf, char *buf)
  171. {
  172. return sprintf(buf,
  173. "%s\n",
  174. cache_mattrib[this_leaf->cci.pcci_cache_attr]);
  175. }
  176. static ssize_t show_size(struct cache_info *this_leaf, char *buf)
  177. {
  178. return sprintf(buf, "%uK\n", this_leaf->cci.pcci_cache_size / 1024);
  179. }
  180. static ssize_t show_number_of_sets(struct cache_info *this_leaf, char *buf)
  181. {
  182. unsigned number_of_sets = this_leaf->cci.pcci_cache_size;
  183. number_of_sets /= this_leaf->cci.pcci_assoc;
  184. number_of_sets /= 1 << this_leaf->cci.pcci_line_size;
  185. return sprintf(buf, "%u\n", number_of_sets);
  186. }
  187. static ssize_t show_shared_cpu_map(struct cache_info *this_leaf, char *buf)
  188. {
  189. ssize_t len;
  190. cpumask_t shared_cpu_map;
  191. cpumask_and(&shared_cpu_map,
  192. &this_leaf->shared_cpu_map, cpu_online_mask);
  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 const 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. kfree(all_cpu_cache_info[cpu].cache_leaves);
  259. all_cpu_cache_info[cpu].cache_leaves = NULL;
  260. all_cpu_cache_info[cpu].num_cache_leaves = 0;
  261. memset(&all_cpu_cache_info[cpu].kobj, 0, sizeof(struct kobject));
  262. return;
  263. }
  264. static int __cpuinit cpu_cache_sysfs_init(unsigned int cpu)
  265. {
  266. unsigned long i, levels, unique_caches;
  267. pal_cache_config_info_t cci;
  268. int j;
  269. long status;
  270. struct cache_info *this_cache;
  271. int num_cache_leaves = 0;
  272. if ((status = ia64_pal_cache_summary(&levels, &unique_caches)) != 0) {
  273. printk(KERN_ERR "ia64_pal_cache_summary=%ld\n", status);
  274. return -1;
  275. }
  276. this_cache=kzalloc(sizeof(struct cache_info)*unique_caches,
  277. GFP_KERNEL);
  278. if (this_cache == NULL)
  279. return -ENOMEM;
  280. for (i=0; i < levels; i++) {
  281. for (j=2; j >0 ; j--) {
  282. if ((status=ia64_pal_cache_config_info(i,j, &cci)) !=
  283. PAL_STATUS_SUCCESS)
  284. continue;
  285. this_cache[num_cache_leaves].cci = cci;
  286. this_cache[num_cache_leaves].level = i + 1;
  287. this_cache[num_cache_leaves].type = j;
  288. cache_shared_cpu_map_setup(cpu,
  289. &this_cache[num_cache_leaves]);
  290. num_cache_leaves ++;
  291. }
  292. }
  293. all_cpu_cache_info[cpu].cache_leaves = this_cache;
  294. all_cpu_cache_info[cpu].num_cache_leaves = num_cache_leaves;
  295. memset(&all_cpu_cache_info[cpu].kobj, 0, sizeof(struct kobject));
  296. return 0;
  297. }
  298. /* Add cache interface for CPU device */
  299. static int __cpuinit cache_add_dev(struct device * sys_dev)
  300. {
  301. unsigned int cpu = sys_dev->id;
  302. unsigned long i, j;
  303. struct cache_info *this_object;
  304. int retval = 0;
  305. cpumask_t oldmask;
  306. if (all_cpu_cache_info[cpu].kobj.parent)
  307. return 0;
  308. oldmask = current->cpus_allowed;
  309. retval = set_cpus_allowed_ptr(current, cpumask_of(cpu));
  310. if (unlikely(retval))
  311. return retval;
  312. retval = cpu_cache_sysfs_init(cpu);
  313. set_cpus_allowed_ptr(current, &oldmask);
  314. if (unlikely(retval < 0))
  315. return retval;
  316. retval = kobject_init_and_add(&all_cpu_cache_info[cpu].kobj,
  317. &cache_ktype_percpu_entry, &sys_dev->kobj,
  318. "%s", "cache");
  319. if (unlikely(retval < 0)) {
  320. cpu_cache_sysfs_exit(cpu);
  321. return retval;
  322. }
  323. for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++) {
  324. this_object = LEAF_KOBJECT_PTR(cpu,i);
  325. retval = kobject_init_and_add(&(this_object->kobj),
  326. &cache_ktype,
  327. &all_cpu_cache_info[cpu].kobj,
  328. "index%1lu", i);
  329. if (unlikely(retval)) {
  330. for (j = 0; j < i; j++) {
  331. kobject_put(&(LEAF_KOBJECT_PTR(cpu,j)->kobj));
  332. }
  333. kobject_put(&all_cpu_cache_info[cpu].kobj);
  334. cpu_cache_sysfs_exit(cpu);
  335. return retval;
  336. }
  337. kobject_uevent(&(this_object->kobj), KOBJ_ADD);
  338. }
  339. kobject_uevent(&all_cpu_cache_info[cpu].kobj, KOBJ_ADD);
  340. return retval;
  341. }
  342. /* Remove cache interface for CPU device */
  343. static int __cpuinit cache_remove_dev(struct device * sys_dev)
  344. {
  345. unsigned int cpu = sys_dev->id;
  346. unsigned long i;
  347. for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++)
  348. kobject_put(&(LEAF_KOBJECT_PTR(cpu,i)->kobj));
  349. if (all_cpu_cache_info[cpu].kobj.parent) {
  350. kobject_put(&all_cpu_cache_info[cpu].kobj);
  351. memset(&all_cpu_cache_info[cpu].kobj,
  352. 0,
  353. sizeof(struct kobject));
  354. }
  355. cpu_cache_sysfs_exit(cpu);
  356. return 0;
  357. }
  358. /*
  359. * When a cpu is hot-plugged, do a check and initiate
  360. * cache kobject if necessary
  361. */
  362. static int __cpuinit cache_cpu_callback(struct notifier_block *nfb,
  363. unsigned long action, void *hcpu)
  364. {
  365. unsigned int cpu = (unsigned long)hcpu;
  366. struct device *sys_dev;
  367. sys_dev = get_cpu_device(cpu);
  368. switch (action) {
  369. case CPU_ONLINE:
  370. case CPU_ONLINE_FROZEN:
  371. cache_add_dev(sys_dev);
  372. break;
  373. case CPU_DEAD:
  374. case CPU_DEAD_FROZEN:
  375. cache_remove_dev(sys_dev);
  376. break;
  377. }
  378. return NOTIFY_OK;
  379. }
  380. static struct notifier_block __cpuinitdata cache_cpu_notifier =
  381. {
  382. .notifier_call = cache_cpu_callback
  383. };
  384. static int __init cache_sysfs_init(void)
  385. {
  386. int i;
  387. for_each_online_cpu(i) {
  388. struct device *sys_dev = get_cpu_device((unsigned int)i);
  389. cache_add_dev(sys_dev);
  390. }
  391. register_hotcpu_notifier(&cache_cpu_notifier);
  392. return 0;
  393. }
  394. device_initcall(cache_sysfs_init);