topology.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright IBM Corp. 2007, 2011
  3. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  4. */
  5. #define KMSG_COMPONENT "cpu"
  6. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  7. #include <linux/workqueue.h>
  8. #include <linux/bootmem.h>
  9. #include <linux/cpuset.h>
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/init.h>
  14. #include <linux/delay.h>
  15. #include <linux/cpu.h>
  16. #include <linux/smp.h>
  17. #include <linux/mm.h>
  18. #include <asm/sysinfo.h>
  19. #define PTF_HORIZONTAL (0UL)
  20. #define PTF_VERTICAL (1UL)
  21. #define PTF_CHECK (2UL)
  22. struct mask_info {
  23. struct mask_info *next;
  24. unsigned char id;
  25. cpumask_t mask;
  26. };
  27. static int topology_enabled = 1;
  28. static void topology_work_fn(struct work_struct *work);
  29. static struct sysinfo_15_1_x *tl_info;
  30. static void set_topology_timer(void);
  31. static DECLARE_WORK(topology_work, topology_work_fn);
  32. /* topology_lock protects the core linked list */
  33. static DEFINE_SPINLOCK(topology_lock);
  34. static struct mask_info core_info;
  35. cpumask_t cpu_core_map[NR_CPUS];
  36. unsigned char cpu_core_id[NR_CPUS];
  37. static struct mask_info book_info;
  38. cpumask_t cpu_book_map[NR_CPUS];
  39. unsigned char cpu_book_id[NR_CPUS];
  40. static cpumask_t cpu_group_map(struct mask_info *info, unsigned int cpu)
  41. {
  42. cpumask_t mask;
  43. cpumask_clear(&mask);
  44. if (!topology_enabled || !MACHINE_HAS_TOPOLOGY) {
  45. cpumask_copy(&mask, cpumask_of(cpu));
  46. return mask;
  47. }
  48. while (info) {
  49. if (cpumask_test_cpu(cpu, &info->mask)) {
  50. mask = info->mask;
  51. break;
  52. }
  53. info = info->next;
  54. }
  55. if (cpumask_empty(&mask))
  56. cpumask_copy(&mask, cpumask_of(cpu));
  57. return mask;
  58. }
  59. static struct mask_info *add_cpus_to_mask(struct topology_cpu *tl_cpu,
  60. struct mask_info *book,
  61. struct mask_info *core,
  62. int one_core_per_cpu)
  63. {
  64. unsigned int cpu;
  65. for (cpu = find_first_bit(&tl_cpu->mask[0], TOPOLOGY_CPU_BITS);
  66. cpu < TOPOLOGY_CPU_BITS;
  67. cpu = find_next_bit(&tl_cpu->mask[0], TOPOLOGY_CPU_BITS, cpu + 1))
  68. {
  69. unsigned int rcpu;
  70. int lcpu;
  71. rcpu = TOPOLOGY_CPU_BITS - 1 - cpu + tl_cpu->origin;
  72. lcpu = smp_find_processor_id(rcpu);
  73. if (lcpu >= 0) {
  74. cpumask_set_cpu(lcpu, &book->mask);
  75. cpu_book_id[lcpu] = book->id;
  76. cpumask_set_cpu(lcpu, &core->mask);
  77. if (one_core_per_cpu) {
  78. cpu_core_id[lcpu] = rcpu;
  79. core = core->next;
  80. } else {
  81. cpu_core_id[lcpu] = core->id;
  82. }
  83. smp_cpu_set_polarization(lcpu, tl_cpu->pp);
  84. }
  85. }
  86. return core;
  87. }
  88. static void clear_masks(void)
  89. {
  90. struct mask_info *info;
  91. info = &core_info;
  92. while (info) {
  93. cpumask_clear(&info->mask);
  94. info = info->next;
  95. }
  96. info = &book_info;
  97. while (info) {
  98. cpumask_clear(&info->mask);
  99. info = info->next;
  100. }
  101. }
  102. static union topology_entry *next_tle(union topology_entry *tle)
  103. {
  104. if (!tle->nl)
  105. return (union topology_entry *)((struct topology_cpu *)tle + 1);
  106. return (union topology_entry *)((struct topology_container *)tle + 1);
  107. }
  108. static void __tl_to_cores_generic(struct sysinfo_15_1_x *info)
  109. {
  110. struct mask_info *core = &core_info;
  111. struct mask_info *book = &book_info;
  112. union topology_entry *tle, *end;
  113. tle = info->tle;
  114. end = (union topology_entry *)((unsigned long)info + info->length);
  115. while (tle < end) {
  116. switch (tle->nl) {
  117. case 2:
  118. book = book->next;
  119. book->id = tle->container.id;
  120. break;
  121. case 1:
  122. core = core->next;
  123. core->id = tle->container.id;
  124. break;
  125. case 0:
  126. add_cpus_to_mask(&tle->cpu, book, core, 0);
  127. break;
  128. default:
  129. clear_masks();
  130. return;
  131. }
  132. tle = next_tle(tle);
  133. }
  134. }
  135. static void __tl_to_cores_z10(struct sysinfo_15_1_x *info)
  136. {
  137. struct mask_info *core = &core_info;
  138. struct mask_info *book = &book_info;
  139. union topology_entry *tle, *end;
  140. tle = info->tle;
  141. end = (union topology_entry *)((unsigned long)info + info->length);
  142. while (tle < end) {
  143. switch (tle->nl) {
  144. case 1:
  145. book = book->next;
  146. book->id = tle->container.id;
  147. break;
  148. case 0:
  149. core = add_cpus_to_mask(&tle->cpu, book, core, 1);
  150. break;
  151. default:
  152. clear_masks();
  153. return;
  154. }
  155. tle = next_tle(tle);
  156. }
  157. }
  158. static void tl_to_cores(struct sysinfo_15_1_x *info)
  159. {
  160. struct cpuid cpu_id;
  161. get_cpu_id(&cpu_id);
  162. spin_lock_irq(&topology_lock);
  163. clear_masks();
  164. switch (cpu_id.machine) {
  165. case 0x2097:
  166. case 0x2098:
  167. __tl_to_cores_z10(info);
  168. break;
  169. default:
  170. __tl_to_cores_generic(info);
  171. }
  172. spin_unlock_irq(&topology_lock);
  173. }
  174. static void topology_update_polarization_simple(void)
  175. {
  176. int cpu;
  177. mutex_lock(&smp_cpu_state_mutex);
  178. for_each_possible_cpu(cpu)
  179. smp_cpu_set_polarization(cpu, POLARIZATION_HRZ);
  180. mutex_unlock(&smp_cpu_state_mutex);
  181. }
  182. static int ptf(unsigned long fc)
  183. {
  184. int rc;
  185. asm volatile(
  186. " .insn rre,0xb9a20000,%1,%1\n"
  187. " ipm %0\n"
  188. " srl %0,28\n"
  189. : "=d" (rc)
  190. : "d" (fc) : "cc");
  191. return rc;
  192. }
  193. int topology_set_cpu_management(int fc)
  194. {
  195. int cpu, rc;
  196. if (!MACHINE_HAS_TOPOLOGY)
  197. return -EOPNOTSUPP;
  198. if (fc)
  199. rc = ptf(PTF_VERTICAL);
  200. else
  201. rc = ptf(PTF_HORIZONTAL);
  202. if (rc)
  203. return -EBUSY;
  204. for_each_possible_cpu(cpu)
  205. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  206. return rc;
  207. }
  208. static void update_cpu_core_map(void)
  209. {
  210. unsigned long flags;
  211. int cpu;
  212. spin_lock_irqsave(&topology_lock, flags);
  213. for_each_possible_cpu(cpu) {
  214. cpu_core_map[cpu] = cpu_group_map(&core_info, cpu);
  215. cpu_book_map[cpu] = cpu_group_map(&book_info, cpu);
  216. }
  217. spin_unlock_irqrestore(&topology_lock, flags);
  218. }
  219. void store_topology(struct sysinfo_15_1_x *info)
  220. {
  221. if (topology_max_mnest >= 3)
  222. stsi(info, 15, 1, 3);
  223. else
  224. stsi(info, 15, 1, 2);
  225. }
  226. int arch_update_cpu_topology(void)
  227. {
  228. struct sysinfo_15_1_x *info = tl_info;
  229. struct device *dev;
  230. int cpu;
  231. if (!MACHINE_HAS_TOPOLOGY) {
  232. update_cpu_core_map();
  233. topology_update_polarization_simple();
  234. return 0;
  235. }
  236. store_topology(info);
  237. tl_to_cores(info);
  238. update_cpu_core_map();
  239. for_each_online_cpu(cpu) {
  240. dev = get_cpu_device(cpu);
  241. kobject_uevent(&dev->kobj, KOBJ_CHANGE);
  242. }
  243. return 1;
  244. }
  245. static void topology_work_fn(struct work_struct *work)
  246. {
  247. rebuild_sched_domains();
  248. }
  249. void topology_schedule_update(void)
  250. {
  251. schedule_work(&topology_work);
  252. }
  253. static void topology_timer_fn(unsigned long ignored)
  254. {
  255. if (ptf(PTF_CHECK))
  256. topology_schedule_update();
  257. set_topology_timer();
  258. }
  259. static struct timer_list topology_timer =
  260. TIMER_DEFERRED_INITIALIZER(topology_timer_fn, 0, 0);
  261. static atomic_t topology_poll = ATOMIC_INIT(0);
  262. static void set_topology_timer(void)
  263. {
  264. if (atomic_add_unless(&topology_poll, -1, 0))
  265. mod_timer(&topology_timer, jiffies + HZ / 10);
  266. else
  267. mod_timer(&topology_timer, jiffies + HZ * 60);
  268. }
  269. void topology_expect_change(void)
  270. {
  271. if (!MACHINE_HAS_TOPOLOGY)
  272. return;
  273. /* This is racy, but it doesn't matter since it is just a heuristic.
  274. * Worst case is that we poll in a higher frequency for a bit longer.
  275. */
  276. if (atomic_read(&topology_poll) > 60)
  277. return;
  278. atomic_add(60, &topology_poll);
  279. set_topology_timer();
  280. }
  281. static int __init early_parse_topology(char *p)
  282. {
  283. if (strncmp(p, "off", 3))
  284. return 0;
  285. topology_enabled = 0;
  286. return 0;
  287. }
  288. early_param("topology", early_parse_topology);
  289. static void __init alloc_masks(struct sysinfo_15_1_x *info,
  290. struct mask_info *mask, int offset)
  291. {
  292. int i, nr_masks;
  293. nr_masks = info->mag[TOPOLOGY_NR_MAG - offset];
  294. for (i = 0; i < info->mnest - offset; i++)
  295. nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i];
  296. nr_masks = max(nr_masks, 1);
  297. for (i = 0; i < nr_masks; i++) {
  298. mask->next = alloc_bootmem(sizeof(struct mask_info));
  299. mask = mask->next;
  300. }
  301. }
  302. void __init s390_init_cpu_topology(void)
  303. {
  304. struct sysinfo_15_1_x *info;
  305. int i;
  306. if (!MACHINE_HAS_TOPOLOGY)
  307. return;
  308. tl_info = alloc_bootmem_pages(PAGE_SIZE);
  309. info = tl_info;
  310. store_topology(info);
  311. pr_info("The CPU configuration topology of the machine is:");
  312. for (i = 0; i < TOPOLOGY_NR_MAG; i++)
  313. printk(KERN_CONT " %d", info->mag[i]);
  314. printk(KERN_CONT " / %d\n", info->mnest);
  315. alloc_masks(info, &core_info, 1);
  316. alloc_masks(info, &book_info, 2);
  317. }
  318. static int cpu_management;
  319. static ssize_t dispatching_show(struct device *dev,
  320. struct device_attribute *attr,
  321. char *buf)
  322. {
  323. ssize_t count;
  324. mutex_lock(&smp_cpu_state_mutex);
  325. count = sprintf(buf, "%d\n", cpu_management);
  326. mutex_unlock(&smp_cpu_state_mutex);
  327. return count;
  328. }
  329. static ssize_t dispatching_store(struct device *dev,
  330. struct device_attribute *attr,
  331. const char *buf,
  332. size_t count)
  333. {
  334. int val, rc;
  335. char delim;
  336. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  337. return -EINVAL;
  338. if (val != 0 && val != 1)
  339. return -EINVAL;
  340. rc = 0;
  341. get_online_cpus();
  342. mutex_lock(&smp_cpu_state_mutex);
  343. if (cpu_management == val)
  344. goto out;
  345. rc = topology_set_cpu_management(val);
  346. if (rc)
  347. goto out;
  348. cpu_management = val;
  349. topology_expect_change();
  350. out:
  351. mutex_unlock(&smp_cpu_state_mutex);
  352. put_online_cpus();
  353. return rc ? rc : count;
  354. }
  355. static DEVICE_ATTR(dispatching, 0644, dispatching_show,
  356. dispatching_store);
  357. static ssize_t cpu_polarization_show(struct device *dev,
  358. struct device_attribute *attr, char *buf)
  359. {
  360. int cpu = dev->id;
  361. ssize_t count;
  362. mutex_lock(&smp_cpu_state_mutex);
  363. switch (smp_cpu_get_polarization(cpu)) {
  364. case POLARIZATION_HRZ:
  365. count = sprintf(buf, "horizontal\n");
  366. break;
  367. case POLARIZATION_VL:
  368. count = sprintf(buf, "vertical:low\n");
  369. break;
  370. case POLARIZATION_VM:
  371. count = sprintf(buf, "vertical:medium\n");
  372. break;
  373. case POLARIZATION_VH:
  374. count = sprintf(buf, "vertical:high\n");
  375. break;
  376. default:
  377. count = sprintf(buf, "unknown\n");
  378. break;
  379. }
  380. mutex_unlock(&smp_cpu_state_mutex);
  381. return count;
  382. }
  383. static DEVICE_ATTR(polarization, 0444, cpu_polarization_show, NULL);
  384. static struct attribute *topology_cpu_attrs[] = {
  385. &dev_attr_polarization.attr,
  386. NULL,
  387. };
  388. static struct attribute_group topology_cpu_attr_group = {
  389. .attrs = topology_cpu_attrs,
  390. };
  391. int topology_cpu_init(struct cpu *cpu)
  392. {
  393. return sysfs_create_group(&cpu->dev.kobj, &topology_cpu_attr_group);
  394. }
  395. static int __init topology_init(void)
  396. {
  397. if (!MACHINE_HAS_TOPOLOGY) {
  398. topology_update_polarization_simple();
  399. goto out;
  400. }
  401. set_topology_timer();
  402. out:
  403. update_cpu_core_map();
  404. return device_create_file(cpu_subsys.dev_root, &dev_attr_dispatching);
  405. }
  406. device_initcall(topology_init);