topology.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. #include <asm/s390_ext.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 int topology_enabled = 1;
  28. static void topology_work_fn(struct work_struct *work);
  29. static struct sysinfo_15_1_x *tl_info;
  30. static struct timer_list topology_timer;
  31. static void set_topology_timer(void);
  32. static DECLARE_WORK(topology_work, topology_work_fn);
  33. /* topology_lock protects the core linked list */
  34. static DEFINE_SPINLOCK(topology_lock);
  35. static struct mask_info core_info;
  36. cpumask_t cpu_core_map[NR_CPUS];
  37. unsigned char cpu_core_id[NR_CPUS];
  38. #ifdef CONFIG_SCHED_BOOK
  39. static struct mask_info book_info;
  40. cpumask_t cpu_book_map[NR_CPUS];
  41. unsigned char cpu_book_id[NR_CPUS];
  42. #endif
  43. static cpumask_t cpu_group_map(struct mask_info *info, unsigned int cpu)
  44. {
  45. cpumask_t mask;
  46. cpus_clear(mask);
  47. if (!topology_enabled || !MACHINE_HAS_TOPOLOGY) {
  48. cpumask_copy(&mask, cpumask_of(cpu));
  49. return mask;
  50. }
  51. while (info) {
  52. if (cpu_isset(cpu, info->mask)) {
  53. mask = info->mask;
  54. break;
  55. }
  56. info = info->next;
  57. }
  58. if (cpus_empty(mask))
  59. mask = cpumask_of_cpu(cpu);
  60. return mask;
  61. }
  62. static void add_cpus_to_mask(struct topology_cpu *tl_cpu,
  63. struct mask_info *book, struct mask_info *core)
  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, lcpu;
  71. rcpu = TOPOLOGY_CPU_BITS - 1 - cpu + tl_cpu->origin;
  72. for_each_present_cpu(lcpu) {
  73. if (cpu_logical_map(lcpu) != rcpu)
  74. continue;
  75. #ifdef CONFIG_SCHED_BOOK
  76. cpu_set(lcpu, book->mask);
  77. cpu_book_id[lcpu] = book->id;
  78. #endif
  79. cpu_set(lcpu, core->mask);
  80. cpu_core_id[lcpu] = core->id;
  81. smp_cpu_polarization[lcpu] = tl_cpu->pp;
  82. }
  83. }
  84. }
  85. static void clear_masks(void)
  86. {
  87. struct mask_info *info;
  88. info = &core_info;
  89. while (info) {
  90. cpus_clear(info->mask);
  91. info = info->next;
  92. }
  93. #ifdef CONFIG_SCHED_BOOK
  94. info = &book_info;
  95. while (info) {
  96. cpus_clear(info->mask);
  97. info = info->next;
  98. }
  99. #endif
  100. }
  101. static union topology_entry *next_tle(union topology_entry *tle)
  102. {
  103. if (!tle->nl)
  104. return (union topology_entry *)((struct topology_cpu *)tle + 1);
  105. return (union topology_entry *)((struct topology_container *)tle + 1);
  106. }
  107. static void tl_to_cores(struct sysinfo_15_1_x *info)
  108. {
  109. #ifdef CONFIG_SCHED_BOOK
  110. struct mask_info *book = &book_info;
  111. #else
  112. struct mask_info *book = NULL;
  113. #endif
  114. struct mask_info *core = &core_info;
  115. union topology_entry *tle, *end;
  116. spin_lock_irq(&topology_lock);
  117. clear_masks();
  118. tle = info->tle;
  119. end = (union topology_entry *)((unsigned long)info + info->length);
  120. while (tle < end) {
  121. switch (tle->nl) {
  122. #ifdef CONFIG_SCHED_BOOK
  123. case 2:
  124. book = book->next;
  125. book->id = tle->container.id;
  126. break;
  127. #endif
  128. case 1:
  129. core = core->next;
  130. core->id = tle->container.id;
  131. break;
  132. case 0:
  133. add_cpus_to_mask(&tle->cpu, book, core);
  134. break;
  135. default:
  136. clear_masks();
  137. goto out;
  138. }
  139. tle = next_tle(tle);
  140. }
  141. out:
  142. spin_unlock_irq(&topology_lock);
  143. }
  144. static void topology_update_polarization_simple(void)
  145. {
  146. int cpu;
  147. mutex_lock(&smp_cpu_state_mutex);
  148. for_each_possible_cpu(cpu)
  149. smp_cpu_polarization[cpu] = POLARIZATION_HRZ;
  150. mutex_unlock(&smp_cpu_state_mutex);
  151. }
  152. static int ptf(unsigned long fc)
  153. {
  154. int rc;
  155. asm volatile(
  156. " .insn rre,0xb9a20000,%1,%1\n"
  157. " ipm %0\n"
  158. " srl %0,28\n"
  159. : "=d" (rc)
  160. : "d" (fc) : "cc");
  161. return rc;
  162. }
  163. int topology_set_cpu_management(int fc)
  164. {
  165. int cpu;
  166. int rc;
  167. if (!MACHINE_HAS_TOPOLOGY)
  168. return -EOPNOTSUPP;
  169. if (fc)
  170. rc = ptf(PTF_VERTICAL);
  171. else
  172. rc = ptf(PTF_HORIZONTAL);
  173. if (rc)
  174. return -EBUSY;
  175. for_each_possible_cpu(cpu)
  176. smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
  177. return rc;
  178. }
  179. static void update_cpu_core_map(void)
  180. {
  181. unsigned long flags;
  182. int cpu;
  183. spin_lock_irqsave(&topology_lock, flags);
  184. for_each_possible_cpu(cpu) {
  185. cpu_core_map[cpu] = cpu_group_map(&core_info, cpu);
  186. #ifdef CONFIG_SCHED_BOOK
  187. cpu_book_map[cpu] = cpu_group_map(&book_info, cpu);
  188. #endif
  189. }
  190. spin_unlock_irqrestore(&topology_lock, flags);
  191. }
  192. void store_topology(struct sysinfo_15_1_x *info)
  193. {
  194. #ifdef CONFIG_SCHED_BOOK
  195. int rc;
  196. rc = stsi(info, 15, 1, 3);
  197. if (rc != -ENOSYS)
  198. return;
  199. #endif
  200. stsi(info, 15, 1, 2);
  201. }
  202. int arch_update_cpu_topology(void)
  203. {
  204. struct sysinfo_15_1_x *info = tl_info;
  205. struct sys_device *sysdev;
  206. int cpu;
  207. if (!MACHINE_HAS_TOPOLOGY) {
  208. update_cpu_core_map();
  209. topology_update_polarization_simple();
  210. return 0;
  211. }
  212. store_topology(info);
  213. tl_to_cores(info);
  214. update_cpu_core_map();
  215. for_each_online_cpu(cpu) {
  216. sysdev = get_cpu_sysdev(cpu);
  217. kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
  218. }
  219. return 1;
  220. }
  221. static void topology_work_fn(struct work_struct *work)
  222. {
  223. rebuild_sched_domains();
  224. }
  225. void topology_schedule_update(void)
  226. {
  227. schedule_work(&topology_work);
  228. }
  229. static void topology_timer_fn(unsigned long ignored)
  230. {
  231. if (ptf(PTF_CHECK))
  232. topology_schedule_update();
  233. set_topology_timer();
  234. }
  235. static void set_topology_timer(void)
  236. {
  237. topology_timer.function = topology_timer_fn;
  238. topology_timer.data = 0;
  239. topology_timer.expires = jiffies + 60 * HZ;
  240. add_timer(&topology_timer);
  241. }
  242. static int __init early_parse_topology(char *p)
  243. {
  244. if (strncmp(p, "off", 3))
  245. return 0;
  246. topology_enabled = 0;
  247. return 0;
  248. }
  249. early_param("topology", early_parse_topology);
  250. static int __init init_topology_update(void)
  251. {
  252. int rc;
  253. rc = 0;
  254. if (!MACHINE_HAS_TOPOLOGY) {
  255. topology_update_polarization_simple();
  256. goto out;
  257. }
  258. init_timer_deferrable(&topology_timer);
  259. set_topology_timer();
  260. out:
  261. update_cpu_core_map();
  262. return rc;
  263. }
  264. __initcall(init_topology_update);
  265. static void alloc_masks(struct sysinfo_15_1_x *info, struct mask_info *mask,
  266. int offset)
  267. {
  268. int i, nr_masks;
  269. nr_masks = info->mag[TOPOLOGY_NR_MAG - offset];
  270. for (i = 0; i < info->mnest - offset; i++)
  271. nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i];
  272. nr_masks = max(nr_masks, 1);
  273. for (i = 0; i < nr_masks; i++) {
  274. mask->next = alloc_bootmem(sizeof(struct mask_info));
  275. mask = mask->next;
  276. }
  277. }
  278. void __init s390_init_cpu_topology(void)
  279. {
  280. struct sysinfo_15_1_x *info;
  281. int i;
  282. if (!MACHINE_HAS_TOPOLOGY)
  283. return;
  284. tl_info = alloc_bootmem_pages(PAGE_SIZE);
  285. info = tl_info;
  286. store_topology(info);
  287. pr_info("The CPU configuration topology of the machine is:");
  288. for (i = 0; i < TOPOLOGY_NR_MAG; i++)
  289. printk(" %d", info->mag[i]);
  290. printk(" / %d\n", info->mnest);
  291. alloc_masks(info, &core_info, 2);
  292. #ifdef CONFIG_SCHED_BOOK
  293. alloc_masks(info, &book_info, 3);
  294. #endif
  295. }