topology.c 11 KB

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