topology.c 11 KB

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