topology.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  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. kobject_uevent(&dev->kobj, KOBJ_CHANGE);
  278. }
  279. return rc;
  280. }
  281. static void topology_work_fn(struct work_struct *work)
  282. {
  283. rebuild_sched_domains();
  284. }
  285. void topology_schedule_update(void)
  286. {
  287. schedule_work(&topology_work);
  288. }
  289. static void topology_flush_work(void)
  290. {
  291. flush_work(&topology_work);
  292. }
  293. static void topology_timer_fn(struct timer_list *unused)
  294. {
  295. if (ptf(PTF_CHECK))
  296. topology_schedule_update();
  297. set_topology_timer();
  298. }
  299. static struct timer_list topology_timer;
  300. static atomic_t topology_poll = ATOMIC_INIT(0);
  301. static void set_topology_timer(void)
  302. {
  303. if (atomic_add_unless(&topology_poll, -1, 0))
  304. mod_timer(&topology_timer, jiffies + HZ / 10);
  305. else
  306. mod_timer(&topology_timer, jiffies + HZ * 60);
  307. }
  308. void topology_expect_change(void)
  309. {
  310. if (!MACHINE_HAS_TOPOLOGY)
  311. return;
  312. /* This is racy, but it doesn't matter since it is just a heuristic.
  313. * Worst case is that we poll in a higher frequency for a bit longer.
  314. */
  315. if (atomic_read(&topology_poll) > 60)
  316. return;
  317. atomic_add(60, &topology_poll);
  318. set_topology_timer();
  319. }
  320. static int cpu_management;
  321. static ssize_t dispatching_show(struct device *dev,
  322. struct device_attribute *attr,
  323. char *buf)
  324. {
  325. ssize_t count;
  326. mutex_lock(&smp_cpu_state_mutex);
  327. count = sprintf(buf, "%d\n", cpu_management);
  328. mutex_unlock(&smp_cpu_state_mutex);
  329. return count;
  330. }
  331. static ssize_t dispatching_store(struct device *dev,
  332. struct device_attribute *attr,
  333. const char *buf,
  334. size_t count)
  335. {
  336. int val, rc;
  337. char delim;
  338. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  339. return -EINVAL;
  340. if (val != 0 && val != 1)
  341. return -EINVAL;
  342. rc = 0;
  343. get_online_cpus();
  344. mutex_lock(&smp_cpu_state_mutex);
  345. if (cpu_management == val)
  346. goto out;
  347. rc = topology_set_cpu_management(val);
  348. if (rc)
  349. goto out;
  350. cpu_management = val;
  351. topology_expect_change();
  352. out:
  353. mutex_unlock(&smp_cpu_state_mutex);
  354. put_online_cpus();
  355. return rc ? rc : count;
  356. }
  357. static DEVICE_ATTR_RW(dispatching);
  358. static ssize_t cpu_polarization_show(struct device *dev,
  359. struct device_attribute *attr, char *buf)
  360. {
  361. int cpu = dev->id;
  362. ssize_t count;
  363. mutex_lock(&smp_cpu_state_mutex);
  364. switch (smp_cpu_get_polarization(cpu)) {
  365. case POLARIZATION_HRZ:
  366. count = sprintf(buf, "horizontal\n");
  367. break;
  368. case POLARIZATION_VL:
  369. count = sprintf(buf, "vertical:low\n");
  370. break;
  371. case POLARIZATION_VM:
  372. count = sprintf(buf, "vertical:medium\n");
  373. break;
  374. case POLARIZATION_VH:
  375. count = sprintf(buf, "vertical:high\n");
  376. break;
  377. default:
  378. count = sprintf(buf, "unknown\n");
  379. break;
  380. }
  381. mutex_unlock(&smp_cpu_state_mutex);
  382. return count;
  383. }
  384. static DEVICE_ATTR(polarization, 0444, cpu_polarization_show, NULL);
  385. static struct attribute *topology_cpu_attrs[] = {
  386. &dev_attr_polarization.attr,
  387. NULL,
  388. };
  389. static struct attribute_group topology_cpu_attr_group = {
  390. .attrs = topology_cpu_attrs,
  391. };
  392. static ssize_t cpu_dedicated_show(struct device *dev,
  393. struct device_attribute *attr, char *buf)
  394. {
  395. int cpu = dev->id;
  396. ssize_t count;
  397. mutex_lock(&smp_cpu_state_mutex);
  398. count = sprintf(buf, "%d\n", topology_cpu_dedicated(cpu));
  399. mutex_unlock(&smp_cpu_state_mutex);
  400. return count;
  401. }
  402. static DEVICE_ATTR(dedicated, 0444, cpu_dedicated_show, NULL);
  403. static struct attribute *topology_extra_cpu_attrs[] = {
  404. &dev_attr_dedicated.attr,
  405. NULL,
  406. };
  407. static struct attribute_group topology_extra_cpu_attr_group = {
  408. .attrs = topology_extra_cpu_attrs,
  409. };
  410. int topology_cpu_init(struct cpu *cpu)
  411. {
  412. int rc;
  413. rc = sysfs_create_group(&cpu->dev.kobj, &topology_cpu_attr_group);
  414. if (rc || !MACHINE_HAS_TOPOLOGY)
  415. return rc;
  416. rc = sysfs_create_group(&cpu->dev.kobj, &topology_extra_cpu_attr_group);
  417. if (rc)
  418. sysfs_remove_group(&cpu->dev.kobj, &topology_cpu_attr_group);
  419. return rc;
  420. }
  421. static const struct cpumask *cpu_thread_mask(int cpu)
  422. {
  423. return &cpu_topology[cpu].thread_mask;
  424. }
  425. const struct cpumask *cpu_coregroup_mask(int cpu)
  426. {
  427. return &cpu_topology[cpu].core_mask;
  428. }
  429. static const struct cpumask *cpu_book_mask(int cpu)
  430. {
  431. return &cpu_topology[cpu].book_mask;
  432. }
  433. static const struct cpumask *cpu_drawer_mask(int cpu)
  434. {
  435. return &cpu_topology[cpu].drawer_mask;
  436. }
  437. static struct sched_domain_topology_level s390_topology[] = {
  438. { cpu_thread_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
  439. { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
  440. { cpu_book_mask, SD_INIT_NAME(BOOK) },
  441. { cpu_drawer_mask, SD_INIT_NAME(DRAWER) },
  442. { cpu_cpu_mask, SD_INIT_NAME(DIE) },
  443. { NULL, },
  444. };
  445. static void __init alloc_masks(struct sysinfo_15_1_x *info,
  446. struct mask_info *mask, int offset)
  447. {
  448. int i, nr_masks;
  449. nr_masks = info->mag[TOPOLOGY_NR_MAG - offset];
  450. for (i = 0; i < info->mnest - offset; i++)
  451. nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i];
  452. nr_masks = max(nr_masks, 1);
  453. for (i = 0; i < nr_masks; i++) {
  454. mask->next = memblock_virt_alloc(sizeof(*mask->next), 8);
  455. mask = mask->next;
  456. }
  457. }
  458. void __init topology_init_early(void)
  459. {
  460. struct sysinfo_15_1_x *info;
  461. set_sched_topology(s390_topology);
  462. if (topology_mode == TOPOLOGY_MODE_UNINITIALIZED) {
  463. if (MACHINE_HAS_TOPOLOGY)
  464. topology_mode = TOPOLOGY_MODE_HW;
  465. else
  466. topology_mode = TOPOLOGY_MODE_SINGLE;
  467. }
  468. if (!MACHINE_HAS_TOPOLOGY)
  469. goto out;
  470. tl_info = memblock_virt_alloc(PAGE_SIZE, PAGE_SIZE);
  471. info = tl_info;
  472. store_topology(info);
  473. pr_info("The CPU configuration topology of the machine is: %d %d %d %d %d %d / %d\n",
  474. info->mag[0], info->mag[1], info->mag[2], info->mag[3],
  475. info->mag[4], info->mag[5], info->mnest);
  476. alloc_masks(info, &socket_info, 1);
  477. alloc_masks(info, &book_info, 2);
  478. alloc_masks(info, &drawer_info, 3);
  479. out:
  480. __arch_update_cpu_topology();
  481. __arch_update_dedicated_flag(NULL);
  482. }
  483. static inline int topology_get_mode(int enabled)
  484. {
  485. if (!enabled)
  486. return TOPOLOGY_MODE_SINGLE;
  487. return MACHINE_HAS_TOPOLOGY ? TOPOLOGY_MODE_HW : TOPOLOGY_MODE_PACKAGE;
  488. }
  489. static inline int topology_is_enabled(void)
  490. {
  491. return topology_mode != TOPOLOGY_MODE_SINGLE;
  492. }
  493. static int __init topology_setup(char *str)
  494. {
  495. bool enabled;
  496. int rc;
  497. rc = kstrtobool(str, &enabled);
  498. if (rc)
  499. return rc;
  500. topology_mode = topology_get_mode(enabled);
  501. return 0;
  502. }
  503. early_param("topology", topology_setup);
  504. static int topology_ctl_handler(struct ctl_table *ctl, int write,
  505. void __user *buffer, size_t *lenp, loff_t *ppos)
  506. {
  507. unsigned int len;
  508. int new_mode;
  509. char buf[2];
  510. if (!*lenp || *ppos) {
  511. *lenp = 0;
  512. return 0;
  513. }
  514. if (!write) {
  515. strncpy(buf, topology_is_enabled() ? "1\n" : "0\n",
  516. ARRAY_SIZE(buf));
  517. len = strnlen(buf, ARRAY_SIZE(buf));
  518. if (len > *lenp)
  519. len = *lenp;
  520. if (copy_to_user(buffer, buf, len))
  521. return -EFAULT;
  522. goto out;
  523. }
  524. len = *lenp;
  525. if (copy_from_user(buf, buffer, len > sizeof(buf) ? sizeof(buf) : len))
  526. return -EFAULT;
  527. if (buf[0] != '0' && buf[0] != '1')
  528. return -EINVAL;
  529. mutex_lock(&smp_cpu_state_mutex);
  530. new_mode = topology_get_mode(buf[0] == '1');
  531. if (topology_mode != new_mode) {
  532. topology_mode = new_mode;
  533. topology_schedule_update();
  534. }
  535. mutex_unlock(&smp_cpu_state_mutex);
  536. topology_flush_work();
  537. out:
  538. *lenp = len;
  539. *ppos += len;
  540. return 0;
  541. }
  542. static struct ctl_table topology_ctl_table[] = {
  543. {
  544. .procname = "topology",
  545. .mode = 0644,
  546. .proc_handler = topology_ctl_handler,
  547. },
  548. { },
  549. };
  550. static struct ctl_table topology_dir_table[] = {
  551. {
  552. .procname = "s390",
  553. .maxlen = 0,
  554. .mode = 0555,
  555. .child = topology_ctl_table,
  556. },
  557. { },
  558. };
  559. static int __init topology_init(void)
  560. {
  561. timer_setup(&topology_timer, topology_timer_fn, TIMER_DEFERRABLE);
  562. if (MACHINE_HAS_TOPOLOGY)
  563. set_topology_timer();
  564. else
  565. topology_update_polarization_simple();
  566. register_sysctl_table(topology_dir_table);
  567. return device_create_file(cpu_subsys.dev_root, &dev_attr_dispatching);
  568. }
  569. device_initcall(topology_init);