topology.c 9.8 KB

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