topology.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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 are used on
  7. * NUMA machines with contiguous memory.
  8. * 2002/08/07 Erich Focht <[email protected]>
  9. * Populate cpu entries in sysfs for non-numa systems as well
  10. * Intel Corporation - Ashok Raj
  11. * 02/27/2006 Zhang, Yanmin
  12. * Populate cpu cache entries in sysfs for cpu cache info
  13. */
  14. #include <linux/cpu.h>
  15. #include <linux/kernel.h>
  16. #include <linux/mm.h>
  17. #include <linux/node.h>
  18. #include <linux/slab.h>
  19. #include <linux/init.h>
  20. #include <linux/memblock.h>
  21. #include <linux/nodemask.h>
  22. #include <linux/notifier.h>
  23. #include <linux/export.h>
  24. #include <asm/mmzone.h>
  25. #include <asm/numa.h>
  26. #include <asm/cpu.h>
  27. static struct ia64_cpu *sysfs_cpus;
  28. void arch_fix_phys_package_id(int num, u32 slot)
  29. {
  30. #ifdef CONFIG_SMP
  31. if (cpu_data(num)->socket_id == -1)
  32. cpu_data(num)->socket_id = slot;
  33. #endif
  34. }
  35. EXPORT_SYMBOL_GPL(arch_fix_phys_package_id);
  36. #ifdef CONFIG_HOTPLUG_CPU
  37. int __ref arch_register_cpu(int num)
  38. {
  39. /*
  40. * If CPEI can be re-targeted 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. return register_cpu(&sysfs_cpus[num].cpu, num);
  47. }
  48. EXPORT_SYMBOL(arch_register_cpu);
  49. void __ref 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_unregister_cpu);
  55. #else
  56. static int __init arch_register_cpu(int num)
  57. {
  58. return register_cpu(&sysfs_cpus[num].cpu, num);
  59. }
  60. #endif /*CONFIG_HOTPLUG_CPU*/
  61. static int __init topology_init(void)
  62. {
  63. int i, err = 0;
  64. sysfs_cpus = kcalloc(NR_CPUS, sizeof(struct ia64_cpu), GFP_KERNEL);
  65. if (!sysfs_cpus)
  66. panic("kzalloc in topology_init failed - NR_CPUS too big?");
  67. for_each_present_cpu(i) {
  68. if((err = arch_register_cpu(i)))
  69. goto out;
  70. }
  71. out:
  72. return err;
  73. }
  74. subsys_initcall(topology_init);
  75. /*
  76. * Export cpu cache information through sysfs
  77. */
  78. /*
  79. * A bunch of string array to get pretty printing
  80. */
  81. static const char *cache_types[] = {
  82. "", /* not used */
  83. "Instruction",
  84. "Data",
  85. "Unified" /* unified */
  86. };
  87. static const char *cache_mattrib[]={
  88. "WriteThrough",
  89. "WriteBack",
  90. "", /* reserved */
  91. "" /* reserved */
  92. };
  93. struct cache_info {
  94. pal_cache_config_info_t cci;
  95. cpumask_t shared_cpu_map;
  96. int level;
  97. int type;
  98. struct kobject kobj;
  99. };
  100. struct cpu_cache_info {
  101. struct cache_info *cache_leaves;
  102. int num_cache_leaves;
  103. struct kobject kobj;
  104. };
  105. static struct cpu_cache_info all_cpu_cache_info[NR_CPUS];
  106. #define LEAF_KOBJECT_PTR(x,y) (&all_cpu_cache_info[x].cache_leaves[y])
  107. #ifdef CONFIG_SMP
  108. static void cache_shared_cpu_map_setup(unsigned int cpu,
  109. struct cache_info * this_leaf)
  110. {
  111. pal_cache_shared_info_t csi;
  112. int num_shared, i = 0;
  113. unsigned int j;
  114. if (cpu_data(cpu)->threads_per_core <= 1 &&
  115. cpu_data(cpu)->cores_per_socket <= 1) {
  116. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  117. return;
  118. }
  119. if (ia64_pal_cache_shared_info(this_leaf->level,
  120. this_leaf->type,
  121. 0,
  122. &csi) != PAL_STATUS_SUCCESS)
  123. return;
  124. num_shared = (int) csi.num_shared;
  125. do {
  126. for_each_possible_cpu(j)
  127. if (cpu_data(cpu)->socket_id == cpu_data(j)->socket_id
  128. && cpu_data(j)->core_id == csi.log1_cid
  129. && cpu_data(j)->thread_id == csi.log1_tid)
  130. cpumask_set_cpu(j, &this_leaf->shared_cpu_map);
  131. i++;
  132. } while (i < num_shared &&
  133. ia64_pal_cache_shared_info(this_leaf->level,
  134. this_leaf->type,
  135. i,
  136. &csi) == PAL_STATUS_SUCCESS);
  137. }
  138. #else
  139. static void cache_shared_cpu_map_setup(unsigned int cpu,
  140. struct cache_info * this_leaf)
  141. {
  142. cpumask_set_cpu(cpu, &this_leaf->shared_cpu_map);
  143. return;
  144. }
  145. #endif
  146. static ssize_t show_coherency_line_size(struct cache_info *this_leaf,
  147. char *buf)
  148. {
  149. return sprintf(buf, "%u\n", 1 << this_leaf->cci.pcci_line_size);
  150. }
  151. static ssize_t show_ways_of_associativity(struct cache_info *this_leaf,
  152. char *buf)
  153. {
  154. return sprintf(buf, "%u\n", this_leaf->cci.pcci_assoc);
  155. }
  156. static ssize_t show_attributes(struct cache_info *this_leaf, char *buf)
  157. {
  158. return sprintf(buf,
  159. "%s\n",
  160. cache_mattrib[this_leaf->cci.pcci_cache_attr]);
  161. }
  162. static ssize_t show_size(struct cache_info *this_leaf, char *buf)
  163. {
  164. return sprintf(buf, "%uK\n", this_leaf->cci.pcci_cache_size / 1024);
  165. }
  166. static ssize_t show_number_of_sets(struct cache_info *this_leaf, char *buf)
  167. {
  168. unsigned number_of_sets = this_leaf->cci.pcci_cache_size;
  169. number_of_sets /= this_leaf->cci.pcci_assoc;
  170. number_of_sets /= 1 << this_leaf->cci.pcci_line_size;
  171. return sprintf(buf, "%u\n", number_of_sets);
  172. }
  173. static ssize_t show_shared_cpu_map(struct cache_info *this_leaf, char *buf)
  174. {
  175. cpumask_t shared_cpu_map;
  176. cpumask_and(&shared_cpu_map,
  177. &this_leaf->shared_cpu_map, cpu_online_mask);
  178. return scnprintf(buf, PAGE_SIZE, "%*pb\n",
  179. cpumask_pr_args(&shared_cpu_map));
  180. }
  181. static ssize_t show_type(struct cache_info *this_leaf, char *buf)
  182. {
  183. int type = this_leaf->type + this_leaf->cci.pcci_unified;
  184. return sprintf(buf, "%s\n", cache_types[type]);
  185. }
  186. static ssize_t show_level(struct cache_info *this_leaf, char *buf)
  187. {
  188. return sprintf(buf, "%u\n", this_leaf->level);
  189. }
  190. struct cache_attr {
  191. struct attribute attr;
  192. ssize_t (*show)(struct cache_info *, char *);
  193. ssize_t (*store)(struct cache_info *, const char *, size_t count);
  194. };
  195. #ifdef define_one_ro
  196. #undef define_one_ro
  197. #endif
  198. #define define_one_ro(_name) \
  199. static struct cache_attr _name = \
  200. __ATTR(_name, 0444, show_##_name, NULL)
  201. define_one_ro(level);
  202. define_one_ro(type);
  203. define_one_ro(coherency_line_size);
  204. define_one_ro(ways_of_associativity);
  205. define_one_ro(size);
  206. define_one_ro(number_of_sets);
  207. define_one_ro(shared_cpu_map);
  208. define_one_ro(attributes);
  209. static struct attribute * cache_default_attrs[] = {
  210. &type.attr,
  211. &level.attr,
  212. &coherency_line_size.attr,
  213. &ways_of_associativity.attr,
  214. &attributes.attr,
  215. &size.attr,
  216. &number_of_sets.attr,
  217. &shared_cpu_map.attr,
  218. NULL
  219. };
  220. ATTRIBUTE_GROUPS(cache_default);
  221. #define to_object(k) container_of(k, struct cache_info, kobj)
  222. #define to_attr(a) container_of(a, struct cache_attr, attr)
  223. static ssize_t ia64_cache_show(struct kobject * kobj, struct attribute * attr, char * buf)
  224. {
  225. struct cache_attr *fattr = to_attr(attr);
  226. struct cache_info *this_leaf = to_object(kobj);
  227. ssize_t ret;
  228. ret = fattr->show ? fattr->show(this_leaf, buf) : 0;
  229. return ret;
  230. }
  231. static const struct sysfs_ops cache_sysfs_ops = {
  232. .show = ia64_cache_show
  233. };
  234. static struct kobj_type cache_ktype = {
  235. .sysfs_ops = &cache_sysfs_ops,
  236. .default_groups = cache_default_groups,
  237. };
  238. static struct kobj_type cache_ktype_percpu_entry = {
  239. .sysfs_ops = &cache_sysfs_ops,
  240. };
  241. static void cpu_cache_sysfs_exit(unsigned int cpu)
  242. {
  243. kfree(all_cpu_cache_info[cpu].cache_leaves);
  244. all_cpu_cache_info[cpu].cache_leaves = NULL;
  245. all_cpu_cache_info[cpu].num_cache_leaves = 0;
  246. memset(&all_cpu_cache_info[cpu].kobj, 0, sizeof(struct kobject));
  247. return;
  248. }
  249. static int cpu_cache_sysfs_init(unsigned int cpu)
  250. {
  251. unsigned long i, levels, unique_caches;
  252. pal_cache_config_info_t cci;
  253. int j;
  254. long status;
  255. struct cache_info *this_cache;
  256. int num_cache_leaves = 0;
  257. if ((status = ia64_pal_cache_summary(&levels, &unique_caches)) != 0) {
  258. printk(KERN_ERR "ia64_pal_cache_summary=%ld\n", status);
  259. return -1;
  260. }
  261. this_cache=kcalloc(unique_caches, sizeof(struct cache_info),
  262. GFP_KERNEL);
  263. if (this_cache == NULL)
  264. return -ENOMEM;
  265. for (i=0; i < levels; i++) {
  266. for (j=2; j >0 ; j--) {
  267. if ((status=ia64_pal_cache_config_info(i,j, &cci)) !=
  268. PAL_STATUS_SUCCESS)
  269. continue;
  270. this_cache[num_cache_leaves].cci = cci;
  271. this_cache[num_cache_leaves].level = i + 1;
  272. this_cache[num_cache_leaves].type = j;
  273. cache_shared_cpu_map_setup(cpu,
  274. &this_cache[num_cache_leaves]);
  275. num_cache_leaves ++;
  276. }
  277. }
  278. all_cpu_cache_info[cpu].cache_leaves = this_cache;
  279. all_cpu_cache_info[cpu].num_cache_leaves = num_cache_leaves;
  280. memset(&all_cpu_cache_info[cpu].kobj, 0, sizeof(struct kobject));
  281. return 0;
  282. }
  283. /* Add cache interface for CPU device */
  284. static int cache_add_dev(unsigned int cpu)
  285. {
  286. struct device *sys_dev = get_cpu_device(cpu);
  287. unsigned long i, j;
  288. struct cache_info *this_object;
  289. int retval = 0;
  290. if (all_cpu_cache_info[cpu].kobj.parent)
  291. return 0;
  292. retval = cpu_cache_sysfs_init(cpu);
  293. if (unlikely(retval < 0))
  294. return retval;
  295. retval = kobject_init_and_add(&all_cpu_cache_info[cpu].kobj,
  296. &cache_ktype_percpu_entry, &sys_dev->kobj,
  297. "%s", "cache");
  298. if (unlikely(retval < 0)) {
  299. cpu_cache_sysfs_exit(cpu);
  300. return retval;
  301. }
  302. for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++) {
  303. this_object = LEAF_KOBJECT_PTR(cpu,i);
  304. retval = kobject_init_and_add(&(this_object->kobj),
  305. &cache_ktype,
  306. &all_cpu_cache_info[cpu].kobj,
  307. "index%1lu", i);
  308. if (unlikely(retval)) {
  309. for (j = 0; j < i; j++) {
  310. kobject_put(&(LEAF_KOBJECT_PTR(cpu,j)->kobj));
  311. }
  312. kobject_put(&all_cpu_cache_info[cpu].kobj);
  313. cpu_cache_sysfs_exit(cpu);
  314. return retval;
  315. }
  316. kobject_uevent(&(this_object->kobj), KOBJ_ADD);
  317. }
  318. kobject_uevent(&all_cpu_cache_info[cpu].kobj, KOBJ_ADD);
  319. return retval;
  320. }
  321. /* Remove cache interface for CPU device */
  322. static int cache_remove_dev(unsigned int cpu)
  323. {
  324. unsigned long i;
  325. for (i = 0; i < all_cpu_cache_info[cpu].num_cache_leaves; i++)
  326. kobject_put(&(LEAF_KOBJECT_PTR(cpu,i)->kobj));
  327. if (all_cpu_cache_info[cpu].kobj.parent) {
  328. kobject_put(&all_cpu_cache_info[cpu].kobj);
  329. memset(&all_cpu_cache_info[cpu].kobj,
  330. 0,
  331. sizeof(struct kobject));
  332. }
  333. cpu_cache_sysfs_exit(cpu);
  334. return 0;
  335. }
  336. static int __init cache_sysfs_init(void)
  337. {
  338. int ret;
  339. ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "ia64/topology:online",
  340. cache_add_dev, cache_remove_dev);
  341. WARN_ON(ret < 0);
  342. return 0;
  343. }
  344. device_initcall(cache_sysfs_init);