topology.c 7.3 KB

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