topology.c 9.9 KB

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