topology.c 11 KB

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