topology.c 11 KB

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