topology.c 11 KB

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