topology.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  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/kernel.h>
  12. #include <linux/sched.h>
  13. #include <linux/init.h>
  14. #include <linux/delay.h>
  15. #include <linux/cpu.h>
  16. #include <linux/smp.h>
  17. #include <linux/mm.h>
  18. #define PTF_HORIZONTAL (0UL)
  19. #define PTF_VERTICAL (1UL)
  20. #define PTF_CHECK (2UL)
  21. struct mask_info {
  22. struct mask_info *next;
  23. unsigned char id;
  24. cpumask_t mask;
  25. };
  26. static int topology_enabled = 1;
  27. static void topology_work_fn(struct work_struct *work);
  28. static struct sysinfo_15_1_x *tl_info;
  29. static void set_topology_timer(void);
  30. static DECLARE_WORK(topology_work, topology_work_fn);
  31. /* topology_lock protects the core linked list */
  32. static DEFINE_SPINLOCK(topology_lock);
  33. static struct mask_info core_info;
  34. cpumask_t cpu_core_map[NR_CPUS];
  35. unsigned char cpu_core_id[NR_CPUS];
  36. static struct mask_info book_info;
  37. cpumask_t cpu_book_map[NR_CPUS];
  38. unsigned char cpu_book_id[NR_CPUS];
  39. /* smp_cpu_state_mutex must be held when accessing this array */
  40. int cpu_polarization[NR_CPUS];
  41. static cpumask_t cpu_group_map(struct mask_info *info, unsigned int cpu)
  42. {
  43. cpumask_t mask;
  44. cpumask_clear(&mask);
  45. if (!topology_enabled || !MACHINE_HAS_TOPOLOGY) {
  46. cpumask_copy(&mask, cpumask_of(cpu));
  47. return mask;
  48. }
  49. while (info) {
  50. if (cpumask_test_cpu(cpu, &info->mask)) {
  51. mask = info->mask;
  52. break;
  53. }
  54. info = info->next;
  55. }
  56. if (cpumask_empty(&mask))
  57. cpumask_copy(&mask, cpumask_of(cpu));
  58. return mask;
  59. }
  60. static struct mask_info *add_cpus_to_mask(struct topology_cpu *tl_cpu,
  61. struct mask_info *book,
  62. struct mask_info *core,
  63. int one_core_per_cpu)
  64. {
  65. unsigned int cpu;
  66. for (cpu = find_first_bit(&tl_cpu->mask[0], TOPOLOGY_CPU_BITS);
  67. cpu < TOPOLOGY_CPU_BITS;
  68. cpu = find_next_bit(&tl_cpu->mask[0], TOPOLOGY_CPU_BITS, cpu + 1))
  69. {
  70. unsigned int rcpu;
  71. int lcpu;
  72. rcpu = TOPOLOGY_CPU_BITS - 1 - cpu + tl_cpu->origin;
  73. lcpu = smp_find_processor_id(rcpu);
  74. if (lcpu >= 0) {
  75. cpumask_set_cpu(lcpu, &book->mask);
  76. cpu_book_id[lcpu] = book->id;
  77. cpumask_set_cpu(lcpu, &core->mask);
  78. if (one_core_per_cpu) {
  79. cpu_core_id[lcpu] = rcpu;
  80. core = core->next;
  81. } else {
  82. cpu_core_id[lcpu] = core->id;
  83. }
  84. cpu_set_polarization(lcpu, tl_cpu->pp);
  85. }
  86. }
  87. return core;
  88. }
  89. static void clear_masks(void)
  90. {
  91. struct mask_info *info;
  92. info = &core_info;
  93. while (info) {
  94. cpumask_clear(&info->mask);
  95. info = info->next;
  96. }
  97. info = &book_info;
  98. while (info) {
  99. cpumask_clear(&info->mask);
  100. info = info->next;
  101. }
  102. }
  103. static union topology_entry *next_tle(union topology_entry *tle)
  104. {
  105. if (!tle->nl)
  106. return (union topology_entry *)((struct topology_cpu *)tle + 1);
  107. return (union topology_entry *)((struct topology_container *)tle + 1);
  108. }
  109. static void __tl_to_cores_generic(struct sysinfo_15_1_x *info)
  110. {
  111. struct mask_info *core = &core_info;
  112. struct mask_info *book = &book_info;
  113. union topology_entry *tle, *end;
  114. tle = info->tle;
  115. end = (union topology_entry *)((unsigned long)info + info->length);
  116. while (tle < end) {
  117. switch (tle->nl) {
  118. case 2:
  119. book = book->next;
  120. book->id = tle->container.id;
  121. break;
  122. case 1:
  123. core = core->next;
  124. core->id = tle->container.id;
  125. break;
  126. case 0:
  127. add_cpus_to_mask(&tle->cpu, book, core, 0);
  128. break;
  129. default:
  130. clear_masks();
  131. return;
  132. }
  133. tle = next_tle(tle);
  134. }
  135. }
  136. static void __tl_to_cores_z10(struct sysinfo_15_1_x *info)
  137. {
  138. struct mask_info *core = &core_info;
  139. struct mask_info *book = &book_info;
  140. union topology_entry *tle, *end;
  141. tle = info->tle;
  142. end = (union topology_entry *)((unsigned long)info + info->length);
  143. while (tle < end) {
  144. switch (tle->nl) {
  145. case 1:
  146. book = book->next;
  147. book->id = tle->container.id;
  148. break;
  149. case 0:
  150. core = add_cpus_to_mask(&tle->cpu, book, core, 1);
  151. break;
  152. default:
  153. clear_masks();
  154. return;
  155. }
  156. tle = next_tle(tle);
  157. }
  158. }
  159. static void tl_to_cores(struct sysinfo_15_1_x *info)
  160. {
  161. struct cpuid cpu_id;
  162. get_cpu_id(&cpu_id);
  163. spin_lock_irq(&topology_lock);
  164. clear_masks();
  165. switch (cpu_id.machine) {
  166. case 0x2097:
  167. case 0x2098:
  168. __tl_to_cores_z10(info);
  169. break;
  170. default:
  171. __tl_to_cores_generic(info);
  172. }
  173. spin_unlock_irq(&topology_lock);
  174. }
  175. static void topology_update_polarization_simple(void)
  176. {
  177. int cpu;
  178. mutex_lock(&smp_cpu_state_mutex);
  179. for_each_possible_cpu(cpu)
  180. cpu_set_polarization(cpu, POLARIZATION_HRZ);
  181. mutex_unlock(&smp_cpu_state_mutex);
  182. }
  183. static int ptf(unsigned long fc)
  184. {
  185. int rc;
  186. asm volatile(
  187. " .insn rre,0xb9a20000,%1,%1\n"
  188. " ipm %0\n"
  189. " srl %0,28\n"
  190. : "=d" (rc)
  191. : "d" (fc) : "cc");
  192. return rc;
  193. }
  194. int topology_set_cpu_management(int fc)
  195. {
  196. int cpu, rc;
  197. if (!MACHINE_HAS_TOPOLOGY)
  198. return -EOPNOTSUPP;
  199. if (fc)
  200. rc = ptf(PTF_VERTICAL);
  201. else
  202. rc = ptf(PTF_HORIZONTAL);
  203. if (rc)
  204. return -EBUSY;
  205. for_each_possible_cpu(cpu)
  206. cpu_set_polarization(cpu, POLARIZATION_UNKNOWN);
  207. return rc;
  208. }
  209. static void update_cpu_core_map(void)
  210. {
  211. unsigned long flags;
  212. int cpu;
  213. spin_lock_irqsave(&topology_lock, flags);
  214. for_each_possible_cpu(cpu) {
  215. cpu_core_map[cpu] = cpu_group_map(&core_info, cpu);
  216. cpu_book_map[cpu] = cpu_group_map(&book_info, cpu);
  217. }
  218. spin_unlock_irqrestore(&topology_lock, flags);
  219. }
  220. void store_topology(struct sysinfo_15_1_x *info)
  221. {
  222. int rc;
  223. rc = stsi(info, 15, 1, 3);
  224. if (rc != -ENOSYS)
  225. return;
  226. stsi(info, 15, 1, 2);
  227. }
  228. int arch_update_cpu_topology(void)
  229. {
  230. struct sysinfo_15_1_x *info = tl_info;
  231. struct device *dev;
  232. int cpu;
  233. if (!MACHINE_HAS_TOPOLOGY) {
  234. update_cpu_core_map();
  235. topology_update_polarization_simple();
  236. return 0;
  237. }
  238. store_topology(info);
  239. tl_to_cores(info);
  240. update_cpu_core_map();
  241. for_each_online_cpu(cpu) {
  242. dev = get_cpu_device(cpu);
  243. kobject_uevent(&dev->kobj, KOBJ_CHANGE);
  244. }
  245. return 1;
  246. }
  247. static void topology_work_fn(struct work_struct *work)
  248. {
  249. rebuild_sched_domains();
  250. }
  251. void topology_schedule_update(void)
  252. {
  253. schedule_work(&topology_work);
  254. }
  255. static void topology_timer_fn(unsigned long ignored)
  256. {
  257. if (ptf(PTF_CHECK))
  258. topology_schedule_update();
  259. set_topology_timer();
  260. }
  261. static struct timer_list topology_timer =
  262. TIMER_DEFERRED_INITIALIZER(topology_timer_fn, 0, 0);
  263. static atomic_t topology_poll = ATOMIC_INIT(0);
  264. static void set_topology_timer(void)
  265. {
  266. if (atomic_add_unless(&topology_poll, -1, 0))
  267. mod_timer(&topology_timer, jiffies + HZ / 10);
  268. else
  269. mod_timer(&topology_timer, jiffies + HZ * 60);
  270. }
  271. void topology_expect_change(void)
  272. {
  273. if (!MACHINE_HAS_TOPOLOGY)
  274. return;
  275. /* This is racy, but it doesn't matter since it is just a heuristic.
  276. * Worst case is that we poll in a higher frequency for a bit longer.
  277. */
  278. if (atomic_read(&topology_poll) > 60)
  279. return;
  280. atomic_add(60, &topology_poll);
  281. set_topology_timer();
  282. }
  283. static int __init early_parse_topology(char *p)
  284. {
  285. if (strncmp(p, "off", 3))
  286. return 0;
  287. topology_enabled = 0;
  288. return 0;
  289. }
  290. early_param("topology", early_parse_topology);
  291. static void __init alloc_masks(struct sysinfo_15_1_x *info,
  292. struct mask_info *mask, int offset)
  293. {
  294. int i, nr_masks;
  295. nr_masks = info->mag[TOPOLOGY_NR_MAG - offset];
  296. for (i = 0; i < info->mnest - offset; i++)
  297. nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i];
  298. nr_masks = max(nr_masks, 1);
  299. for (i = 0; i < nr_masks; i++) {
  300. mask->next = alloc_bootmem(sizeof(struct mask_info));
  301. mask = mask->next;
  302. }
  303. }
  304. void __init s390_init_cpu_topology(void)
  305. {
  306. struct sysinfo_15_1_x *info;
  307. int i;
  308. if (!MACHINE_HAS_TOPOLOGY)
  309. return;
  310. tl_info = alloc_bootmem_pages(PAGE_SIZE);
  311. info = tl_info;
  312. store_topology(info);
  313. pr_info("The CPU configuration topology of the machine is:");
  314. for (i = 0; i < TOPOLOGY_NR_MAG; i++)
  315. printk(KERN_CONT " %d", info->mag[i]);
  316. printk(KERN_CONT " / %d\n", info->mnest);
  317. alloc_masks(info, &core_info, 1);
  318. alloc_masks(info, &book_info, 2);
  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(dispatching, 0644, dispatching_show,
  358. dispatching_store);
  359. static ssize_t cpu_polarization_show(struct device *dev,
  360. struct device_attribute *attr, char *buf)
  361. {
  362. int cpu = dev->id;
  363. ssize_t count;
  364. mutex_lock(&smp_cpu_state_mutex);
  365. switch (cpu_read_polarization(cpu)) {
  366. case POLARIZATION_HRZ:
  367. count = sprintf(buf, "horizontal\n");
  368. break;
  369. case POLARIZATION_VL:
  370. count = sprintf(buf, "vertical:low\n");
  371. break;
  372. case POLARIZATION_VM:
  373. count = sprintf(buf, "vertical:medium\n");
  374. break;
  375. case POLARIZATION_VH:
  376. count = sprintf(buf, "vertical:high\n");
  377. break;
  378. default:
  379. count = sprintf(buf, "unknown\n");
  380. break;
  381. }
  382. mutex_unlock(&smp_cpu_state_mutex);
  383. return count;
  384. }
  385. static DEVICE_ATTR(polarization, 0444, cpu_polarization_show, NULL);
  386. static struct attribute *topology_cpu_attrs[] = {
  387. &dev_attr_polarization.attr,
  388. NULL,
  389. };
  390. static struct attribute_group topology_cpu_attr_group = {
  391. .attrs = topology_cpu_attrs,
  392. };
  393. int topology_cpu_init(struct cpu *cpu)
  394. {
  395. return sysfs_create_group(&cpu->dev.kobj, &topology_cpu_attr_group);
  396. }
  397. static int __init topology_init(void)
  398. {
  399. if (!MACHINE_HAS_TOPOLOGY) {
  400. topology_update_polarization_simple();
  401. goto out;
  402. }
  403. set_topology_timer();
  404. out:
  405. update_cpu_core_map();
  406. return device_create_file(cpu_subsys.dev_root, &dev_attr_dispatching);
  407. }
  408. device_initcall(topology_init);