topology.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2007, 2011
  4. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  5. */
  6. #define KMSG_COMPONENT "cpu"
  7. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  8. #include <linux/workqueue.h>
  9. #include <linux/bootmem.h>
  10. #include <linux/uaccess.h>
  11. #include <linux/sysctl.h>
  12. #include <linux/cpuset.h>
  13. #include <linux/device.h>
  14. #include <linux/export.h>
  15. #include <linux/kernel.h>
  16. #include <linux/sched.h>
  17. #include <linux/sched/topology.h>
  18. #include <linux/delay.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/cpu.h>
  22. #include <linux/smp.h>
  23. #include <linux/mm.h>
  24. #include <linux/nodemask.h>
  25. #include <linux/node.h>
  26. #include <asm/sysinfo.h>
  27. #include <asm/numa.h>
  28. #define PTF_HORIZONTAL (0UL)
  29. #define PTF_VERTICAL (1UL)
  30. #define PTF_CHECK (2UL)
  31. enum {
  32. TOPOLOGY_MODE_HW,
  33. TOPOLOGY_MODE_SINGLE,
  34. TOPOLOGY_MODE_PACKAGE,
  35. TOPOLOGY_MODE_UNINITIALIZED
  36. };
  37. struct mask_info {
  38. struct mask_info *next;
  39. unsigned char id;
  40. cpumask_t mask;
  41. };
  42. static int topology_mode = TOPOLOGY_MODE_UNINITIALIZED;
  43. static void set_topology_timer(void);
  44. static void topology_work_fn(struct work_struct *work);
  45. static struct sysinfo_15_1_x *tl_info;
  46. static DECLARE_WORK(topology_work, topology_work_fn);
  47. /*
  48. * Socket/Book linked lists and cpu_topology updates are
  49. * protected by "sched_domains_mutex".
  50. */
  51. static struct mask_info socket_info;
  52. static struct mask_info book_info;
  53. static struct mask_info drawer_info;
  54. struct cpu_topology_s390 cpu_topology[NR_CPUS];
  55. EXPORT_SYMBOL_GPL(cpu_topology);
  56. cpumask_t cpus_with_topology;
  57. static cpumask_t cpu_group_map(struct mask_info *info, unsigned int cpu)
  58. {
  59. cpumask_t mask;
  60. cpumask_copy(&mask, cpumask_of(cpu));
  61. switch (topology_mode) {
  62. case TOPOLOGY_MODE_HW:
  63. while (info) {
  64. if (cpumask_test_cpu(cpu, &info->mask)) {
  65. mask = info->mask;
  66. break;
  67. }
  68. info = info->next;
  69. }
  70. if (cpumask_empty(&mask))
  71. cpumask_copy(&mask, cpumask_of(cpu));
  72. break;
  73. case TOPOLOGY_MODE_PACKAGE:
  74. cpumask_copy(&mask, cpu_present_mask);
  75. break;
  76. default:
  77. /* fallthrough */
  78. case TOPOLOGY_MODE_SINGLE:
  79. cpumask_copy(&mask, cpumask_of(cpu));
  80. break;
  81. }
  82. return mask;
  83. }
  84. static cpumask_t cpu_thread_map(unsigned int cpu)
  85. {
  86. cpumask_t mask;
  87. int i;
  88. cpumask_copy(&mask, cpumask_of(cpu));
  89. if (topology_mode != TOPOLOGY_MODE_HW)
  90. return mask;
  91. cpu -= cpu % (smp_cpu_mtid + 1);
  92. for (i = 0; i <= smp_cpu_mtid; i++)
  93. if (cpu_present(cpu + i))
  94. cpumask_set_cpu(cpu + i, &mask);
  95. return mask;
  96. }
  97. #define TOPOLOGY_CORE_BITS 64
  98. static void add_cpus_to_mask(struct topology_core *tl_core,
  99. struct mask_info *drawer,
  100. struct mask_info *book,
  101. struct mask_info *socket)
  102. {
  103. struct cpu_topology_s390 *topo;
  104. unsigned int core;
  105. for_each_set_bit(core, &tl_core->mask, TOPOLOGY_CORE_BITS) {
  106. unsigned int rcore;
  107. int lcpu, i;
  108. rcore = TOPOLOGY_CORE_BITS - 1 - core + tl_core->origin;
  109. lcpu = smp_find_processor_id(rcore << smp_cpu_mt_shift);
  110. if (lcpu < 0)
  111. continue;
  112. for (i = 0; i <= smp_cpu_mtid; i++) {
  113. topo = &cpu_topology[lcpu + i];
  114. topo->drawer_id = drawer->id;
  115. topo->book_id = book->id;
  116. topo->socket_id = socket->id;
  117. topo->core_id = rcore;
  118. topo->thread_id = lcpu + i;
  119. topo->dedicated = tl_core->d;
  120. cpumask_set_cpu(lcpu + i, &drawer->mask);
  121. cpumask_set_cpu(lcpu + i, &book->mask);
  122. cpumask_set_cpu(lcpu + i, &socket->mask);
  123. cpumask_set_cpu(lcpu + i, &cpus_with_topology);
  124. smp_cpu_set_polarization(lcpu + i, tl_core->pp);
  125. }
  126. }
  127. }
  128. static void clear_masks(void)
  129. {
  130. struct mask_info *info;
  131. info = &socket_info;
  132. while (info) {
  133. cpumask_clear(&info->mask);
  134. info = info->next;
  135. }
  136. info = &book_info;
  137. while (info) {
  138. cpumask_clear(&info->mask);
  139. info = info->next;
  140. }
  141. info = &drawer_info;
  142. while (info) {
  143. cpumask_clear(&info->mask);
  144. info = info->next;
  145. }
  146. }
  147. static union topology_entry *next_tle(union topology_entry *tle)
  148. {
  149. if (!tle->nl)
  150. return (union topology_entry *)((struct topology_core *)tle + 1);
  151. return (union topology_entry *)((struct topology_container *)tle + 1);
  152. }
  153. static void tl_to_masks(struct sysinfo_15_1_x *info)
  154. {
  155. struct mask_info *socket = &socket_info;
  156. struct mask_info *book = &book_info;
  157. struct mask_info *drawer = &drawer_info;
  158. union topology_entry *tle, *end;
  159. clear_masks();
  160. tle = info->tle;
  161. end = (union topology_entry *)((unsigned long)info + info->length);
  162. while (tle < end) {
  163. switch (tle->nl) {
  164. case 3:
  165. drawer = drawer->next;
  166. drawer->id = tle->container.id;
  167. break;
  168. case 2:
  169. book = book->next;
  170. book->id = tle->container.id;
  171. break;
  172. case 1:
  173. socket = socket->next;
  174. socket->id = tle->container.id;
  175. break;
  176. case 0:
  177. add_cpus_to_mask(&tle->cpu, drawer, book, socket);
  178. break;
  179. default:
  180. clear_masks();
  181. return;
  182. }
  183. tle = next_tle(tle);
  184. }
  185. }
  186. static void topology_update_polarization_simple(void)
  187. {
  188. int cpu;
  189. for_each_possible_cpu(cpu)
  190. smp_cpu_set_polarization(cpu, POLARIZATION_HRZ);
  191. }
  192. static int ptf(unsigned long fc)
  193. {
  194. int rc;
  195. asm volatile(
  196. " .insn rre,0xb9a20000,%1,%1\n"
  197. " ipm %0\n"
  198. " srl %0,28\n"
  199. : "=d" (rc)
  200. : "d" (fc) : "cc");
  201. return rc;
  202. }
  203. int topology_set_cpu_management(int fc)
  204. {
  205. int cpu, rc;
  206. if (!MACHINE_HAS_TOPOLOGY)
  207. return -EOPNOTSUPP;
  208. if (fc)
  209. rc = ptf(PTF_VERTICAL);
  210. else
  211. rc = ptf(PTF_HORIZONTAL);
  212. if (rc)
  213. return -EBUSY;
  214. for_each_possible_cpu(cpu)
  215. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  216. return rc;
  217. }
  218. static void update_cpu_masks(void)
  219. {
  220. struct cpu_topology_s390 *topo;
  221. int cpu, id;
  222. for_each_possible_cpu(cpu) {
  223. topo = &cpu_topology[cpu];
  224. topo->thread_mask = cpu_thread_map(cpu);
  225. topo->core_mask = cpu_group_map(&socket_info, cpu);
  226. topo->book_mask = cpu_group_map(&book_info, cpu);
  227. topo->drawer_mask = cpu_group_map(&drawer_info, cpu);
  228. if (topology_mode != TOPOLOGY_MODE_HW) {
  229. id = topology_mode == TOPOLOGY_MODE_PACKAGE ? 0 : cpu;
  230. topo->thread_id = cpu;
  231. topo->core_id = cpu;
  232. topo->socket_id = id;
  233. topo->book_id = id;
  234. topo->drawer_id = id;
  235. if (cpu_present(cpu))
  236. cpumask_set_cpu(cpu, &cpus_with_topology);
  237. }
  238. }
  239. numa_update_cpu_topology();
  240. }
  241. void store_topology(struct sysinfo_15_1_x *info)
  242. {
  243. stsi(info, 15, 1, topology_mnest_limit());
  244. }
  245. static void __arch_update_dedicated_flag(void *arg)
  246. {
  247. if (topology_cpu_dedicated(smp_processor_id()))
  248. set_cpu_flag(CIF_DEDICATED_CPU);
  249. else
  250. clear_cpu_flag(CIF_DEDICATED_CPU);
  251. }
  252. static int __arch_update_cpu_topology(void)
  253. {
  254. struct sysinfo_15_1_x *info = tl_info;
  255. int rc = 0;
  256. mutex_lock(&smp_cpu_state_mutex);
  257. cpumask_clear(&cpus_with_topology);
  258. if (MACHINE_HAS_TOPOLOGY) {
  259. rc = 1;
  260. store_topology(info);
  261. tl_to_masks(info);
  262. }
  263. update_cpu_masks();
  264. if (!MACHINE_HAS_TOPOLOGY)
  265. topology_update_polarization_simple();
  266. mutex_unlock(&smp_cpu_state_mutex);
  267. return rc;
  268. }
  269. int arch_update_cpu_topology(void)
  270. {
  271. struct device *dev;
  272. int cpu, rc;
  273. rc = __arch_update_cpu_topology();
  274. on_each_cpu(__arch_update_dedicated_flag, NULL, 0);
  275. for_each_online_cpu(cpu) {
  276. dev = get_cpu_device(cpu);
  277. if (dev)
  278. kobject_uevent(&dev->kobj, KOBJ_CHANGE);
  279. }
  280. return rc;
  281. }
  282. static void topology_work_fn(struct work_struct *work)
  283. {
  284. rebuild_sched_domains();
  285. }
  286. void topology_schedule_update(void)
  287. {
  288. schedule_work(&topology_work);
  289. }
  290. static void topology_flush_work(void)
  291. {
  292. flush_work(&topology_work);
  293. }
  294. static void topology_timer_fn(struct timer_list *unused)
  295. {
  296. if (ptf(PTF_CHECK))
  297. topology_schedule_update();
  298. set_topology_timer();
  299. }
  300. static struct timer_list topology_timer;
  301. static atomic_t topology_poll = ATOMIC_INIT(0);
  302. static void set_topology_timer(void)
  303. {
  304. if (atomic_add_unless(&topology_poll, -1, 0))
  305. mod_timer(&topology_timer, jiffies + HZ / 10);
  306. else
  307. mod_timer(&topology_timer, jiffies + HZ * 60);
  308. }
  309. void topology_expect_change(void)
  310. {
  311. if (!MACHINE_HAS_TOPOLOGY)
  312. return;
  313. /* This is racy, but it doesn't matter since it is just a heuristic.
  314. * Worst case is that we poll in a higher frequency for a bit longer.
  315. */
  316. if (atomic_read(&topology_poll) > 60)
  317. return;
  318. atomic_add(60, &topology_poll);
  319. set_topology_timer();
  320. }
  321. static int cpu_management;
  322. static ssize_t dispatching_show(struct device *dev,
  323. struct device_attribute *attr,
  324. char *buf)
  325. {
  326. ssize_t count;
  327. mutex_lock(&smp_cpu_state_mutex);
  328. count = sprintf(buf, "%d\n", cpu_management);
  329. mutex_unlock(&smp_cpu_state_mutex);
  330. return count;
  331. }
  332. static ssize_t dispatching_store(struct device *dev,
  333. struct device_attribute *attr,
  334. const char *buf,
  335. size_t count)
  336. {
  337. int val, rc;
  338. char delim;
  339. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  340. return -EINVAL;
  341. if (val != 0 && val != 1)
  342. return -EINVAL;
  343. rc = 0;
  344. get_online_cpus();
  345. mutex_lock(&smp_cpu_state_mutex);
  346. if (cpu_management == val)
  347. goto out;
  348. rc = topology_set_cpu_management(val);
  349. if (rc)
  350. goto out;
  351. cpu_management = val;
  352. topology_expect_change();
  353. out:
  354. mutex_unlock(&smp_cpu_state_mutex);
  355. put_online_cpus();
  356. return rc ? rc : count;
  357. }
  358. static DEVICE_ATTR_RW(dispatching);
  359. static ssize_t cpu_polarization_show(struct device *dev,
  360. struct device_attribute *attr, char *buf)
  361. {
  362. int cpu = dev->id;
  363. ssize_t count;
  364. mutex_lock(&smp_cpu_state_mutex);
  365. switch (smp_cpu_get_polarization(cpu)) {
  366. case POLARIZATION_HRZ:
  367. count = sprintf(buf, "horizontal\n");
  368. break;
  369. case POLARIZATION_VL:
  370. count = sprintf(buf, "vertical:low\n");
  371. break;
  372. case POLARIZATION_VM:
  373. count = sprintf(buf, "vertical:medium\n");
  374. break;
  375. case POLARIZATION_VH:
  376. count = sprintf(buf, "vertical:high\n");
  377. break;
  378. default:
  379. count = sprintf(buf, "unknown\n");
  380. break;
  381. }
  382. mutex_unlock(&smp_cpu_state_mutex);
  383. return count;
  384. }
  385. static DEVICE_ATTR(polarization, 0444, cpu_polarization_show, NULL);
  386. static struct attribute *topology_cpu_attrs[] = {
  387. &dev_attr_polarization.attr,
  388. NULL,
  389. };
  390. static struct attribute_group topology_cpu_attr_group = {
  391. .attrs = topology_cpu_attrs,
  392. };
  393. static ssize_t cpu_dedicated_show(struct device *dev,
  394. struct device_attribute *attr, char *buf)
  395. {
  396. int cpu = dev->id;
  397. ssize_t count;
  398. mutex_lock(&smp_cpu_state_mutex);
  399. count = sprintf(buf, "%d\n", topology_cpu_dedicated(cpu));
  400. mutex_unlock(&smp_cpu_state_mutex);
  401. return count;
  402. }
  403. static DEVICE_ATTR(dedicated, 0444, cpu_dedicated_show, NULL);
  404. static struct attribute *topology_extra_cpu_attrs[] = {
  405. &dev_attr_dedicated.attr,
  406. NULL,
  407. };
  408. static struct attribute_group topology_extra_cpu_attr_group = {
  409. .attrs = topology_extra_cpu_attrs,
  410. };
  411. int topology_cpu_init(struct cpu *cpu)
  412. {
  413. int rc;
  414. rc = sysfs_create_group(&cpu->dev.kobj, &topology_cpu_attr_group);
  415. if (rc || !MACHINE_HAS_TOPOLOGY)
  416. return rc;
  417. rc = sysfs_create_group(&cpu->dev.kobj, &topology_extra_cpu_attr_group);
  418. if (rc)
  419. sysfs_remove_group(&cpu->dev.kobj, &topology_cpu_attr_group);
  420. return rc;
  421. }
  422. static const struct cpumask *cpu_thread_mask(int cpu)
  423. {
  424. return &cpu_topology[cpu].thread_mask;
  425. }
  426. const struct cpumask *cpu_coregroup_mask(int cpu)
  427. {
  428. return &cpu_topology[cpu].core_mask;
  429. }
  430. static const struct cpumask *cpu_book_mask(int cpu)
  431. {
  432. return &cpu_topology[cpu].book_mask;
  433. }
  434. static const struct cpumask *cpu_drawer_mask(int cpu)
  435. {
  436. return &cpu_topology[cpu].drawer_mask;
  437. }
  438. static struct sched_domain_topology_level s390_topology[] = {
  439. { cpu_thread_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
  440. { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
  441. { cpu_book_mask, SD_INIT_NAME(BOOK) },
  442. { cpu_drawer_mask, SD_INIT_NAME(DRAWER) },
  443. { cpu_cpu_mask, SD_INIT_NAME(DIE) },
  444. { NULL, },
  445. };
  446. static void __init alloc_masks(struct sysinfo_15_1_x *info,
  447. struct mask_info *mask, int offset)
  448. {
  449. int i, nr_masks;
  450. nr_masks = info->mag[TOPOLOGY_NR_MAG - offset];
  451. for (i = 0; i < info->mnest - offset; i++)
  452. nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i];
  453. nr_masks = max(nr_masks, 1);
  454. for (i = 0; i < nr_masks; i++) {
  455. mask->next = memblock_virt_alloc(sizeof(*mask->next), 8);
  456. mask = mask->next;
  457. }
  458. }
  459. void __init topology_init_early(void)
  460. {
  461. struct sysinfo_15_1_x *info;
  462. set_sched_topology(s390_topology);
  463. if (topology_mode == TOPOLOGY_MODE_UNINITIALIZED) {
  464. if (MACHINE_HAS_TOPOLOGY)
  465. topology_mode = TOPOLOGY_MODE_HW;
  466. else
  467. topology_mode = TOPOLOGY_MODE_SINGLE;
  468. }
  469. if (!MACHINE_HAS_TOPOLOGY)
  470. goto out;
  471. tl_info = memblock_virt_alloc(PAGE_SIZE, PAGE_SIZE);
  472. info = tl_info;
  473. store_topology(info);
  474. pr_info("The CPU configuration topology of the machine is: %d %d %d %d %d %d / %d\n",
  475. info->mag[0], info->mag[1], info->mag[2], info->mag[3],
  476. info->mag[4], info->mag[5], info->mnest);
  477. alloc_masks(info, &socket_info, 1);
  478. alloc_masks(info, &book_info, 2);
  479. alloc_masks(info, &drawer_info, 3);
  480. out:
  481. __arch_update_cpu_topology();
  482. __arch_update_dedicated_flag(NULL);
  483. }
  484. static inline int topology_get_mode(int enabled)
  485. {
  486. if (!enabled)
  487. return TOPOLOGY_MODE_SINGLE;
  488. return MACHINE_HAS_TOPOLOGY ? TOPOLOGY_MODE_HW : TOPOLOGY_MODE_PACKAGE;
  489. }
  490. static inline int topology_is_enabled(void)
  491. {
  492. return topology_mode != TOPOLOGY_MODE_SINGLE;
  493. }
  494. static int __init topology_setup(char *str)
  495. {
  496. bool enabled;
  497. int rc;
  498. rc = kstrtobool(str, &enabled);
  499. if (rc)
  500. return rc;
  501. topology_mode = topology_get_mode(enabled);
  502. return 0;
  503. }
  504. early_param("topology", topology_setup);
  505. static int topology_ctl_handler(struct ctl_table *ctl, int write,
  506. void __user *buffer, size_t *lenp, loff_t *ppos)
  507. {
  508. int enabled = topology_is_enabled();
  509. int new_mode;
  510. int zero = 0;
  511. int one = 1;
  512. int rc;
  513. struct ctl_table ctl_entry = {
  514. .procname = ctl->procname,
  515. .data = &enabled,
  516. .maxlen = sizeof(int),
  517. .extra1 = &zero,
  518. .extra2 = &one,
  519. };
  520. rc = proc_douintvec_minmax(&ctl_entry, write, buffer, lenp, ppos);
  521. if (rc < 0 || !write)
  522. return rc;
  523. mutex_lock(&smp_cpu_state_mutex);
  524. new_mode = topology_get_mode(enabled);
  525. if (topology_mode != new_mode) {
  526. topology_mode = new_mode;
  527. topology_schedule_update();
  528. }
  529. mutex_unlock(&smp_cpu_state_mutex);
  530. topology_flush_work();
  531. return rc;
  532. }
  533. static struct ctl_table topology_ctl_table[] = {
  534. {
  535. .procname = "topology",
  536. .mode = 0644,
  537. .proc_handler = topology_ctl_handler,
  538. },
  539. { },
  540. };
  541. static struct ctl_table topology_dir_table[] = {
  542. {
  543. .procname = "s390",
  544. .maxlen = 0,
  545. .mode = 0555,
  546. .child = topology_ctl_table,
  547. },
  548. { },
  549. };
  550. static int __init topology_init(void)
  551. {
  552. timer_setup(&topology_timer, topology_timer_fn, TIMER_DEFERRABLE);
  553. if (MACHINE_HAS_TOPOLOGY)
  554. set_topology_timer();
  555. else
  556. topology_update_polarization_simple();
  557. register_sysctl_table(topology_dir_table);
  558. return device_create_file(cpu_subsys.dev_root, &dev_attr_dispatching);
  559. }
  560. device_initcall(topology_init);