topology.c 7.5 KB

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