topology.c 11 KB

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