topology.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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/delay.h>
  15. #include <linux/init.h>
  16. #include <linux/slab.h>
  17. #include <linux/cpu.h>
  18. #include <linux/smp.h>
  19. #include <linux/mm.h>
  20. #include <linux/nodemask.h>
  21. #include <linux/node.h>
  22. #include <asm/sysinfo.h>
  23. #include <asm/numa.h>
  24. #define PTF_HORIZONTAL (0UL)
  25. #define PTF_VERTICAL (1UL)
  26. #define PTF_CHECK (2UL)
  27. struct mask_info {
  28. struct mask_info *next;
  29. unsigned char id;
  30. cpumask_t mask;
  31. };
  32. static void set_topology_timer(void);
  33. static void topology_work_fn(struct work_struct *work);
  34. static struct sysinfo_15_1_x *tl_info;
  35. static bool topology_enabled = true;
  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 (!topology_enabled || !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 (!topology_enabled || !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. static void add_cpus_to_mask(struct topology_core *tl_core,
  73. struct mask_info *drawer,
  74. struct mask_info *book,
  75. struct mask_info *socket)
  76. {
  77. struct cpu_topology_s390 *topo;
  78. unsigned int core;
  79. for_each_set_bit(core, &tl_core->mask[0], TOPOLOGY_CORE_BITS) {
  80. unsigned int rcore;
  81. int lcpu, i;
  82. rcore = TOPOLOGY_CORE_BITS - 1 - core + tl_core->origin;
  83. lcpu = smp_find_processor_id(rcore << smp_cpu_mt_shift);
  84. if (lcpu < 0)
  85. continue;
  86. for (i = 0; i <= smp_cpu_mtid; i++) {
  87. topo = &cpu_topology[lcpu + i];
  88. topo->drawer_id = drawer->id;
  89. topo->book_id = book->id;
  90. topo->socket_id = socket->id;
  91. topo->core_id = rcore;
  92. topo->thread_id = lcpu + i;
  93. cpumask_set_cpu(lcpu + i, &drawer->mask);
  94. cpumask_set_cpu(lcpu + i, &book->mask);
  95. cpumask_set_cpu(lcpu + i, &socket->mask);
  96. cpumask_set_cpu(lcpu + i, &cpus_with_topology);
  97. smp_cpu_set_polarization(lcpu + i, tl_core->pp);
  98. }
  99. }
  100. }
  101. static void clear_masks(void)
  102. {
  103. struct mask_info *info;
  104. info = &socket_info;
  105. while (info) {
  106. cpumask_clear(&info->mask);
  107. info = info->next;
  108. }
  109. info = &book_info;
  110. while (info) {
  111. cpumask_clear(&info->mask);
  112. info = info->next;
  113. }
  114. info = &drawer_info;
  115. while (info) {
  116. cpumask_clear(&info->mask);
  117. info = info->next;
  118. }
  119. }
  120. static union topology_entry *next_tle(union topology_entry *tle)
  121. {
  122. if (!tle->nl)
  123. return (union topology_entry *)((struct topology_core *)tle + 1);
  124. return (union topology_entry *)((struct topology_container *)tle + 1);
  125. }
  126. static void tl_to_masks(struct sysinfo_15_1_x *info)
  127. {
  128. struct mask_info *socket = &socket_info;
  129. struct mask_info *book = &book_info;
  130. struct mask_info *drawer = &drawer_info;
  131. union topology_entry *tle, *end;
  132. clear_masks();
  133. tle = info->tle;
  134. end = (union topology_entry *)((unsigned long)info + info->length);
  135. while (tle < end) {
  136. switch (tle->nl) {
  137. case 3:
  138. drawer = drawer->next;
  139. drawer->id = tle->container.id;
  140. break;
  141. case 2:
  142. book = book->next;
  143. book->id = tle->container.id;
  144. break;
  145. case 1:
  146. socket = socket->next;
  147. socket->id = tle->container.id;
  148. break;
  149. case 0:
  150. add_cpus_to_mask(&tle->cpu, drawer, book, socket);
  151. break;
  152. default:
  153. clear_masks();
  154. return;
  155. }
  156. tle = next_tle(tle);
  157. }
  158. }
  159. static void topology_update_polarization_simple(void)
  160. {
  161. int cpu;
  162. mutex_lock(&smp_cpu_state_mutex);
  163. for_each_possible_cpu(cpu)
  164. smp_cpu_set_polarization(cpu, POLARIZATION_HRZ);
  165. mutex_unlock(&smp_cpu_state_mutex);
  166. }
  167. static int ptf(unsigned long fc)
  168. {
  169. int rc;
  170. asm volatile(
  171. " .insn rre,0xb9a20000,%1,%1\n"
  172. " ipm %0\n"
  173. " srl %0,28\n"
  174. : "=d" (rc)
  175. : "d" (fc) : "cc");
  176. return rc;
  177. }
  178. int topology_set_cpu_management(int fc)
  179. {
  180. int cpu, rc;
  181. if (!MACHINE_HAS_TOPOLOGY)
  182. return -EOPNOTSUPP;
  183. if (fc)
  184. rc = ptf(PTF_VERTICAL);
  185. else
  186. rc = ptf(PTF_HORIZONTAL);
  187. if (rc)
  188. return -EBUSY;
  189. for_each_possible_cpu(cpu)
  190. smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  191. return rc;
  192. }
  193. static void update_cpu_masks(void)
  194. {
  195. struct cpu_topology_s390 *topo;
  196. int cpu;
  197. for_each_possible_cpu(cpu) {
  198. topo = &cpu_topology[cpu];
  199. topo->thread_mask = cpu_thread_map(cpu);
  200. topo->core_mask = cpu_group_map(&socket_info, cpu);
  201. topo->book_mask = cpu_group_map(&book_info, cpu);
  202. topo->drawer_mask = cpu_group_map(&drawer_info, cpu);
  203. if (!MACHINE_HAS_TOPOLOGY) {
  204. topo->thread_id = cpu;
  205. topo->core_id = cpu;
  206. topo->socket_id = cpu;
  207. topo->book_id = cpu;
  208. topo->drawer_id = cpu;
  209. if (cpu_present(cpu))
  210. cpumask_set_cpu(cpu, &cpus_with_topology);
  211. }
  212. }
  213. numa_update_cpu_topology();
  214. }
  215. void store_topology(struct sysinfo_15_1_x *info)
  216. {
  217. stsi(info, 15, 1, min(topology_max_mnest, 4));
  218. }
  219. static int __arch_update_cpu_topology(void)
  220. {
  221. struct sysinfo_15_1_x *info = tl_info;
  222. int rc = 0;
  223. cpumask_clear(&cpus_with_topology);
  224. if (MACHINE_HAS_TOPOLOGY) {
  225. rc = 1;
  226. store_topology(info);
  227. tl_to_masks(info);
  228. }
  229. update_cpu_masks();
  230. if (!MACHINE_HAS_TOPOLOGY)
  231. topology_update_polarization_simple();
  232. return rc;
  233. }
  234. int arch_update_cpu_topology(void)
  235. {
  236. struct device *dev;
  237. int cpu, rc;
  238. rc = __arch_update_cpu_topology();
  239. for_each_online_cpu(cpu) {
  240. dev = get_cpu_device(cpu);
  241. kobject_uevent(&dev->kobj, KOBJ_CHANGE);
  242. }
  243. return rc;
  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 cpu_management;
  282. static ssize_t dispatching_show(struct device *dev,
  283. struct device_attribute *attr,
  284. char *buf)
  285. {
  286. ssize_t count;
  287. mutex_lock(&smp_cpu_state_mutex);
  288. count = sprintf(buf, "%d\n", cpu_management);
  289. mutex_unlock(&smp_cpu_state_mutex);
  290. return count;
  291. }
  292. static ssize_t dispatching_store(struct device *dev,
  293. struct device_attribute *attr,
  294. const char *buf,
  295. size_t count)
  296. {
  297. int val, rc;
  298. char delim;
  299. if (sscanf(buf, "%d %c", &val, &delim) != 1)
  300. return -EINVAL;
  301. if (val != 0 && val != 1)
  302. return -EINVAL;
  303. rc = 0;
  304. get_online_cpus();
  305. mutex_lock(&smp_cpu_state_mutex);
  306. if (cpu_management == val)
  307. goto out;
  308. rc = topology_set_cpu_management(val);
  309. if (rc)
  310. goto out;
  311. cpu_management = val;
  312. topology_expect_change();
  313. out:
  314. mutex_unlock(&smp_cpu_state_mutex);
  315. put_online_cpus();
  316. return rc ? rc : count;
  317. }
  318. static DEVICE_ATTR(dispatching, 0644, dispatching_show,
  319. dispatching_store);
  320. static ssize_t cpu_polarization_show(struct device *dev,
  321. struct device_attribute *attr, char *buf)
  322. {
  323. int cpu = dev->id;
  324. ssize_t count;
  325. mutex_lock(&smp_cpu_state_mutex);
  326. switch (smp_cpu_get_polarization(cpu)) {
  327. case POLARIZATION_HRZ:
  328. count = sprintf(buf, "horizontal\n");
  329. break;
  330. case POLARIZATION_VL:
  331. count = sprintf(buf, "vertical:low\n");
  332. break;
  333. case POLARIZATION_VM:
  334. count = sprintf(buf, "vertical:medium\n");
  335. break;
  336. case POLARIZATION_VH:
  337. count = sprintf(buf, "vertical:high\n");
  338. break;
  339. default:
  340. count = sprintf(buf, "unknown\n");
  341. break;
  342. }
  343. mutex_unlock(&smp_cpu_state_mutex);
  344. return count;
  345. }
  346. static DEVICE_ATTR(polarization, 0444, cpu_polarization_show, NULL);
  347. static struct attribute *topology_cpu_attrs[] = {
  348. &dev_attr_polarization.attr,
  349. NULL,
  350. };
  351. static struct attribute_group topology_cpu_attr_group = {
  352. .attrs = topology_cpu_attrs,
  353. };
  354. int topology_cpu_init(struct cpu *cpu)
  355. {
  356. return sysfs_create_group(&cpu->dev.kobj, &topology_cpu_attr_group);
  357. }
  358. static const struct cpumask *cpu_thread_mask(int cpu)
  359. {
  360. return &cpu_topology[cpu].thread_mask;
  361. }
  362. const struct cpumask *cpu_coregroup_mask(int cpu)
  363. {
  364. return &cpu_topology[cpu].core_mask;
  365. }
  366. static const struct cpumask *cpu_book_mask(int cpu)
  367. {
  368. return &cpu_topology[cpu].book_mask;
  369. }
  370. static const struct cpumask *cpu_drawer_mask(int cpu)
  371. {
  372. return &cpu_topology[cpu].drawer_mask;
  373. }
  374. static int __init early_parse_topology(char *p)
  375. {
  376. return kstrtobool(p, &topology_enabled);
  377. }
  378. early_param("topology", early_parse_topology);
  379. static struct sched_domain_topology_level s390_topology[] = {
  380. { cpu_thread_mask, cpu_smt_flags, SD_INIT_NAME(SMT) },
  381. { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) },
  382. { cpu_book_mask, SD_INIT_NAME(BOOK) },
  383. { cpu_drawer_mask, SD_INIT_NAME(DRAWER) },
  384. { cpu_cpu_mask, SD_INIT_NAME(DIE) },
  385. { NULL, },
  386. };
  387. static void __init alloc_masks(struct sysinfo_15_1_x *info,
  388. struct mask_info *mask, int offset)
  389. {
  390. int i, nr_masks;
  391. nr_masks = info->mag[TOPOLOGY_NR_MAG - offset];
  392. for (i = 0; i < info->mnest - offset; i++)
  393. nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i];
  394. nr_masks = max(nr_masks, 1);
  395. for (i = 0; i < nr_masks; i++) {
  396. mask->next = memblock_virt_alloc(sizeof(*mask->next), 8);
  397. mask = mask->next;
  398. }
  399. }
  400. void __init topology_init_early(void)
  401. {
  402. struct sysinfo_15_1_x *info;
  403. int i;
  404. set_sched_topology(s390_topology);
  405. if (!MACHINE_HAS_TOPOLOGY)
  406. goto out;
  407. tl_info = memblock_virt_alloc(sizeof(*tl_info), PAGE_SIZE);
  408. info = tl_info;
  409. store_topology(info);
  410. pr_info("The CPU configuration topology of the machine is:");
  411. for (i = 0; i < TOPOLOGY_NR_MAG; i++)
  412. printk(KERN_CONT " %d", info->mag[i]);
  413. printk(KERN_CONT " / %d\n", info->mnest);
  414. alloc_masks(info, &socket_info, 1);
  415. alloc_masks(info, &book_info, 2);
  416. alloc_masks(info, &drawer_info, 3);
  417. out:
  418. __arch_update_cpu_topology();
  419. }
  420. static int __init topology_init(void)
  421. {
  422. if (MACHINE_HAS_TOPOLOGY)
  423. set_topology_timer();
  424. else
  425. topology_update_polarization_simple();
  426. return device_create_file(cpu_subsys.dev_root, &dev_attr_dispatching);
  427. }
  428. device_initcall(topology_init);