topology.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /*
  2. * Copyright IBM Corp. 2007
  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/kernel.h>
  8. #include <linux/mm.h>
  9. #include <linux/init.h>
  10. #include <linux/device.h>
  11. #include <linux/bootmem.h>
  12. #include <linux/sched.h>
  13. #include <linux/workqueue.h>
  14. #include <linux/cpu.h>
  15. #include <linux/smp.h>
  16. #include <linux/cpuset.h>
  17. #include <asm/delay.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 struct timer_list topology_timer;
  30. static void set_topology_timer(void);
  31. static DECLARE_WORK(topology_work, topology_work_fn);
  32. /* topology_lock protects the core linked list */
  33. static DEFINE_SPINLOCK(topology_lock);
  34. static struct mask_info core_info;
  35. cpumask_t cpu_core_map[NR_CPUS];
  36. unsigned char cpu_core_id[NR_CPUS];
  37. #ifdef CONFIG_SCHED_BOOK
  38. static struct mask_info book_info;
  39. cpumask_t cpu_book_map[NR_CPUS];
  40. unsigned char cpu_book_id[NR_CPUS];
  41. #endif
  42. static cpumask_t cpu_group_map(struct mask_info *info, unsigned int cpu)
  43. {
  44. cpumask_t mask;
  45. cpumask_clear(&mask);
  46. if (!topology_enabled || !MACHINE_HAS_TOPOLOGY) {
  47. cpumask_copy(&mask, cpumask_of(cpu));
  48. return mask;
  49. }
  50. while (info) {
  51. if (cpumask_test_cpu(cpu, &info->mask)) {
  52. mask = info->mask;
  53. break;
  54. }
  55. info = info->next;
  56. }
  57. if (cpumask_empty(&mask))
  58. cpumask_copy(&mask, cpumask_of(cpu));
  59. return mask;
  60. }
  61. static struct mask_info *add_cpus_to_mask(struct topology_cpu *tl_cpu,
  62. struct mask_info *book,
  63. struct mask_info *core,
  64. int z10)
  65. {
  66. unsigned int cpu;
  67. for (cpu = find_first_bit(&tl_cpu->mask[0], TOPOLOGY_CPU_BITS);
  68. cpu < TOPOLOGY_CPU_BITS;
  69. cpu = find_next_bit(&tl_cpu->mask[0], TOPOLOGY_CPU_BITS, cpu + 1))
  70. {
  71. unsigned int rcpu, lcpu;
  72. rcpu = TOPOLOGY_CPU_BITS - 1 - cpu + tl_cpu->origin;
  73. for_each_present_cpu(lcpu) {
  74. if (cpu_logical_map(lcpu) != rcpu)
  75. continue;
  76. #ifdef CONFIG_SCHED_BOOK
  77. cpumask_set_cpu(lcpu, &book->mask);
  78. cpu_book_id[lcpu] = book->id;
  79. #endif
  80. cpumask_set_cpu(lcpu, &core->mask);
  81. if (z10) {
  82. cpu_core_id[lcpu] = rcpu;
  83. core = core->next;
  84. } else {
  85. cpu_core_id[lcpu] = core->id;
  86. }
  87. smp_cpu_polarization[lcpu] = tl_cpu->pp;
  88. }
  89. }
  90. return core;
  91. }
  92. static void clear_masks(void)
  93. {
  94. struct mask_info *info;
  95. info = &core_info;
  96. while (info) {
  97. cpumask_clear(&info->mask);
  98. info = info->next;
  99. }
  100. #ifdef CONFIG_SCHED_BOOK
  101. info = &book_info;
  102. while (info) {
  103. cpumask_clear(&info->mask);
  104. info = info->next;
  105. }
  106. #endif
  107. }
  108. static union topology_entry *next_tle(union topology_entry *tle)
  109. {
  110. if (!tle->nl)
  111. return (union topology_entry *)((struct topology_cpu *)tle + 1);
  112. return (union topology_entry *)((struct topology_container *)tle + 1);
  113. }
  114. static void tl_to_cores(struct sysinfo_15_1_x *info)
  115. {
  116. #ifdef CONFIG_SCHED_BOOK
  117. struct mask_info *book = &book_info;
  118. struct cpuid cpu_id;
  119. #else
  120. struct mask_info *book = NULL;
  121. #endif
  122. struct mask_info *core = &core_info;
  123. union topology_entry *tle, *end;
  124. int z10 = 0;
  125. #ifdef CONFIG_SCHED_BOOK
  126. get_cpu_id(&cpu_id);
  127. z10 = cpu_id.machine == 0x2097 || cpu_id.machine == 0x2098;
  128. #endif
  129. spin_lock_irq(&topology_lock);
  130. clear_masks();
  131. tle = info->tle;
  132. end = (union topology_entry *)((unsigned long)info + info->length);
  133. while (tle < end) {
  134. #ifdef CONFIG_SCHED_BOOK
  135. if (z10) {
  136. switch (tle->nl) {
  137. case 1:
  138. book = book->next;
  139. book->id = tle->container.id;
  140. break;
  141. case 0:
  142. core = add_cpus_to_mask(&tle->cpu, book, core, z10);
  143. break;
  144. default:
  145. clear_masks();
  146. goto out;
  147. }
  148. tle = next_tle(tle);
  149. continue;
  150. }
  151. #endif
  152. switch (tle->nl) {
  153. #ifdef CONFIG_SCHED_BOOK
  154. case 2:
  155. book = book->next;
  156. book->id = tle->container.id;
  157. break;
  158. #endif
  159. case 1:
  160. core = core->next;
  161. core->id = tle->container.id;
  162. break;
  163. case 0:
  164. add_cpus_to_mask(&tle->cpu, book, core, z10);
  165. break;
  166. default:
  167. clear_masks();
  168. goto out;
  169. }
  170. tle = next_tle(tle);
  171. }
  172. out:
  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. smp_cpu_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;
  197. int rc;
  198. if (!MACHINE_HAS_TOPOLOGY)
  199. return -EOPNOTSUPP;
  200. if (fc)
  201. rc = ptf(PTF_VERTICAL);
  202. else
  203. rc = ptf(PTF_HORIZONTAL);
  204. if (rc)
  205. return -EBUSY;
  206. for_each_possible_cpu(cpu)
  207. smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
  208. return rc;
  209. }
  210. static void update_cpu_core_map(void)
  211. {
  212. unsigned long flags;
  213. int cpu;
  214. spin_lock_irqsave(&topology_lock, flags);
  215. for_each_possible_cpu(cpu) {
  216. cpu_core_map[cpu] = cpu_group_map(&core_info, cpu);
  217. #ifdef CONFIG_SCHED_BOOK
  218. cpu_book_map[cpu] = cpu_group_map(&book_info, cpu);
  219. #endif
  220. }
  221. spin_unlock_irqrestore(&topology_lock, flags);
  222. }
  223. void store_topology(struct sysinfo_15_1_x *info)
  224. {
  225. #ifdef CONFIG_SCHED_BOOK
  226. int rc;
  227. rc = stsi(info, 15, 1, 3);
  228. if (rc != -ENOSYS)
  229. return;
  230. #endif
  231. stsi(info, 15, 1, 2);
  232. }
  233. int arch_update_cpu_topology(void)
  234. {
  235. struct sysinfo_15_1_x *info = tl_info;
  236. struct sys_device *sysdev;
  237. int cpu;
  238. if (!MACHINE_HAS_TOPOLOGY) {
  239. update_cpu_core_map();
  240. topology_update_polarization_simple();
  241. return 0;
  242. }
  243. store_topology(info);
  244. tl_to_cores(info);
  245. update_cpu_core_map();
  246. for_each_online_cpu(cpu) {
  247. sysdev = get_cpu_sysdev(cpu);
  248. kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
  249. }
  250. return 1;
  251. }
  252. static void topology_work_fn(struct work_struct *work)
  253. {
  254. rebuild_sched_domains();
  255. }
  256. void topology_schedule_update(void)
  257. {
  258. schedule_work(&topology_work);
  259. }
  260. static void topology_timer_fn(unsigned long ignored)
  261. {
  262. if (ptf(PTF_CHECK))
  263. topology_schedule_update();
  264. set_topology_timer();
  265. }
  266. static void set_topology_timer(void)
  267. {
  268. topology_timer.function = topology_timer_fn;
  269. topology_timer.data = 0;
  270. topology_timer.expires = jiffies + 60 * HZ;
  271. add_timer(&topology_timer);
  272. }
  273. static int __init early_parse_topology(char *p)
  274. {
  275. if (strncmp(p, "off", 3))
  276. return 0;
  277. topology_enabled = 0;
  278. return 0;
  279. }
  280. early_param("topology", early_parse_topology);
  281. static int __init init_topology_update(void)
  282. {
  283. int rc;
  284. rc = 0;
  285. if (!MACHINE_HAS_TOPOLOGY) {
  286. topology_update_polarization_simple();
  287. goto out;
  288. }
  289. init_timer_deferrable(&topology_timer);
  290. set_topology_timer();
  291. out:
  292. update_cpu_core_map();
  293. return rc;
  294. }
  295. __initcall(init_topology_update);
  296. static void __init alloc_masks(struct sysinfo_15_1_x *info,
  297. struct mask_info *mask, int offset)
  298. {
  299. int i, nr_masks;
  300. nr_masks = info->mag[TOPOLOGY_NR_MAG - offset];
  301. for (i = 0; i < info->mnest - offset; i++)
  302. nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i];
  303. nr_masks = max(nr_masks, 1);
  304. for (i = 0; i < nr_masks; i++) {
  305. mask->next = alloc_bootmem(sizeof(struct mask_info));
  306. mask = mask->next;
  307. }
  308. }
  309. void __init s390_init_cpu_topology(void)
  310. {
  311. struct sysinfo_15_1_x *info;
  312. int i;
  313. if (!MACHINE_HAS_TOPOLOGY)
  314. return;
  315. tl_info = alloc_bootmem_pages(PAGE_SIZE);
  316. info = tl_info;
  317. store_topology(info);
  318. pr_info("The CPU configuration topology of the machine is:");
  319. for (i = 0; i < TOPOLOGY_NR_MAG; i++)
  320. printk(" %d", info->mag[i]);
  321. printk(" / %d\n", info->mnest);
  322. alloc_masks(info, &core_info, 1);
  323. #ifdef CONFIG_SCHED_BOOK
  324. alloc_masks(info, &book_info, 2);
  325. #endif
  326. }