topology.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Copyright IBM Corp. 2007
  3. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/mm.h>
  7. #include <linux/init.h>
  8. #include <linux/device.h>
  9. #include <linux/bootmem.h>
  10. #include <linux/sched.h>
  11. #include <linux/kthread.h>
  12. #include <linux/workqueue.h>
  13. #include <linux/cpu.h>
  14. #include <linux/smp.h>
  15. #include <asm/delay.h>
  16. #include <asm/s390_ext.h>
  17. #include <asm/sysinfo.h>
  18. #define CPU_BITS 64
  19. #define NR_MAG 6
  20. #define PTF_HORIZONTAL (0UL)
  21. #define PTF_VERTICAL (1UL)
  22. #define PTF_CHECK (2UL)
  23. struct tl_cpu {
  24. unsigned char reserved0[4];
  25. unsigned char :6;
  26. unsigned char pp:2;
  27. unsigned char reserved1;
  28. unsigned short origin;
  29. unsigned long mask[CPU_BITS / BITS_PER_LONG];
  30. };
  31. struct tl_container {
  32. unsigned char reserved[8];
  33. };
  34. union tl_entry {
  35. unsigned char nl;
  36. struct tl_cpu cpu;
  37. struct tl_container container;
  38. };
  39. struct tl_info {
  40. unsigned char reserved0[2];
  41. unsigned short length;
  42. unsigned char mag[NR_MAG];
  43. unsigned char reserved1;
  44. unsigned char mnest;
  45. unsigned char reserved2[4];
  46. union tl_entry tle[0];
  47. };
  48. struct core_info {
  49. struct core_info *next;
  50. cpumask_t mask;
  51. };
  52. static void topology_work_fn(struct work_struct *work);
  53. static struct tl_info *tl_info;
  54. static struct core_info core_info;
  55. static int machine_has_topology;
  56. static int machine_has_topology_irq;
  57. static struct timer_list topology_timer;
  58. static void set_topology_timer(void);
  59. static DECLARE_WORK(topology_work, topology_work_fn);
  60. cpumask_t cpu_core_map[NR_CPUS];
  61. cpumask_t cpu_coregroup_map(unsigned int cpu)
  62. {
  63. struct core_info *core = &core_info;
  64. cpumask_t mask;
  65. cpus_clear(mask);
  66. if (!machine_has_topology)
  67. return cpu_present_map;
  68. mutex_lock(&smp_cpu_state_mutex);
  69. while (core) {
  70. if (cpu_isset(cpu, core->mask)) {
  71. mask = core->mask;
  72. break;
  73. }
  74. core = core->next;
  75. }
  76. mutex_unlock(&smp_cpu_state_mutex);
  77. if (cpus_empty(mask))
  78. mask = cpumask_of_cpu(cpu);
  79. return mask;
  80. }
  81. static void add_cpus_to_core(struct tl_cpu *tl_cpu, struct core_info *core)
  82. {
  83. unsigned int cpu;
  84. for (cpu = find_first_bit(&tl_cpu->mask[0], CPU_BITS);
  85. cpu < CPU_BITS;
  86. cpu = find_next_bit(&tl_cpu->mask[0], CPU_BITS, cpu + 1))
  87. {
  88. unsigned int rcpu, lcpu;
  89. rcpu = CPU_BITS - 1 - cpu + tl_cpu->origin;
  90. for_each_present_cpu(lcpu) {
  91. if (__cpu_logical_map[lcpu] == rcpu) {
  92. cpu_set(lcpu, core->mask);
  93. smp_cpu_polarization[lcpu] = tl_cpu->pp;
  94. }
  95. }
  96. }
  97. }
  98. static void clear_cores(void)
  99. {
  100. struct core_info *core = &core_info;
  101. while (core) {
  102. cpus_clear(core->mask);
  103. core = core->next;
  104. }
  105. }
  106. static union tl_entry *next_tle(union tl_entry *tle)
  107. {
  108. if (tle->nl)
  109. return (union tl_entry *)((struct tl_container *)tle + 1);
  110. else
  111. return (union tl_entry *)((struct tl_cpu *)tle + 1);
  112. }
  113. static void tl_to_cores(struct tl_info *info)
  114. {
  115. union tl_entry *tle, *end;
  116. struct core_info *core = &core_info;
  117. mutex_lock(&smp_cpu_state_mutex);
  118. clear_cores();
  119. tle = info->tle;
  120. end = (union tl_entry *)((unsigned long)info + info->length);
  121. while (tle < end) {
  122. switch (tle->nl) {
  123. case 5:
  124. case 4:
  125. case 3:
  126. case 2:
  127. break;
  128. case 1:
  129. core = core->next;
  130. break;
  131. case 0:
  132. add_cpus_to_core(&tle->cpu, core);
  133. break;
  134. default:
  135. clear_cores();
  136. machine_has_topology = 0;
  137. return;
  138. }
  139. tle = next_tle(tle);
  140. }
  141. mutex_unlock(&smp_cpu_state_mutex);
  142. }
  143. static void topology_update_polarization_simple(void)
  144. {
  145. int cpu;
  146. mutex_lock(&smp_cpu_state_mutex);
  147. for_each_present_cpu(cpu)
  148. smp_cpu_polarization[cpu] = POLARIZATION_HRZ;
  149. mutex_unlock(&smp_cpu_state_mutex);
  150. }
  151. static int ptf(unsigned long fc)
  152. {
  153. int rc;
  154. asm volatile(
  155. " .insn rre,0xb9a20000,%1,%1\n"
  156. " ipm %0\n"
  157. " srl %0,28\n"
  158. : "=d" (rc)
  159. : "d" (fc) : "cc");
  160. return rc;
  161. }
  162. int topology_set_cpu_management(int fc)
  163. {
  164. int cpu;
  165. int rc;
  166. if (!machine_has_topology)
  167. return -EOPNOTSUPP;
  168. if (fc)
  169. rc = ptf(PTF_VERTICAL);
  170. else
  171. rc = ptf(PTF_HORIZONTAL);
  172. if (rc)
  173. return -EBUSY;
  174. for_each_present_cpu(cpu)
  175. smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
  176. return rc;
  177. }
  178. static void update_cpu_core_map(void)
  179. {
  180. int cpu;
  181. for_each_present_cpu(cpu)
  182. cpu_core_map[cpu] = cpu_coregroup_map(cpu);
  183. }
  184. void arch_update_cpu_topology(void)
  185. {
  186. struct tl_info *info = tl_info;
  187. struct sys_device *sysdev;
  188. int cpu;
  189. if (!machine_has_topology) {
  190. update_cpu_core_map();
  191. topology_update_polarization_simple();
  192. return;
  193. }
  194. stsi(info, 15, 1, 2);
  195. tl_to_cores(info);
  196. update_cpu_core_map();
  197. for_each_online_cpu(cpu) {
  198. sysdev = get_cpu_sysdev(cpu);
  199. kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
  200. }
  201. }
  202. static int topology_kthread(void *data)
  203. {
  204. arch_reinit_sched_domains();
  205. return 0;
  206. }
  207. static void topology_work_fn(struct work_struct *work)
  208. {
  209. /* We can't call arch_reinit_sched_domains() from a multi-threaded
  210. * workqueue context since it may deadlock in case of cpu hotplug.
  211. * So we have to create a kernel thread in order to call
  212. * arch_reinit_sched_domains().
  213. */
  214. kthread_run(topology_kthread, NULL, "topology_update");
  215. }
  216. void topology_schedule_update(void)
  217. {
  218. schedule_work(&topology_work);
  219. }
  220. static void topology_timer_fn(unsigned long ignored)
  221. {
  222. if (ptf(PTF_CHECK))
  223. topology_schedule_update();
  224. set_topology_timer();
  225. }
  226. static void set_topology_timer(void)
  227. {
  228. topology_timer.function = topology_timer_fn;
  229. topology_timer.data = 0;
  230. topology_timer.expires = jiffies + 60 * HZ;
  231. add_timer(&topology_timer);
  232. }
  233. static void topology_interrupt(__u16 code)
  234. {
  235. schedule_work(&topology_work);
  236. }
  237. static int __init init_topology_update(void)
  238. {
  239. int rc;
  240. rc = 0;
  241. if (!machine_has_topology) {
  242. topology_update_polarization_simple();
  243. goto out;
  244. }
  245. init_timer_deferrable(&topology_timer);
  246. if (machine_has_topology_irq) {
  247. rc = register_external_interrupt(0x2005, topology_interrupt);
  248. if (rc)
  249. goto out;
  250. ctl_set_bit(0, 8);
  251. }
  252. else
  253. set_topology_timer();
  254. out:
  255. update_cpu_core_map();
  256. return rc;
  257. }
  258. __initcall(init_topology_update);
  259. void __init s390_init_cpu_topology(void)
  260. {
  261. unsigned long long facility_bits;
  262. struct tl_info *info;
  263. struct core_info *core;
  264. int nr_cores;
  265. int i;
  266. if (stfle(&facility_bits, 1) <= 0)
  267. return;
  268. if (!(facility_bits & (1ULL << 52)) || !(facility_bits & (1ULL << 61)))
  269. return;
  270. machine_has_topology = 1;
  271. if (facility_bits & (1ULL << 51))
  272. machine_has_topology_irq = 1;
  273. tl_info = alloc_bootmem_pages(PAGE_SIZE);
  274. info = tl_info;
  275. stsi(info, 15, 1, 2);
  276. nr_cores = info->mag[NR_MAG - 2];
  277. for (i = 0; i < info->mnest - 2; i++)
  278. nr_cores *= info->mag[NR_MAG - 3 - i];
  279. printk(KERN_INFO "CPU topology:");
  280. for (i = 0; i < NR_MAG; i++)
  281. printk(" %d", info->mag[i]);
  282. printk(" / %d\n", info->mnest);
  283. core = &core_info;
  284. for (i = 0; i < nr_cores; i++) {
  285. core->next = alloc_bootmem(sizeof(struct core_info));
  286. core = core->next;
  287. if (!core)
  288. goto error;
  289. }
  290. return;
  291. error:
  292. machine_has_topology = 0;
  293. machine_has_topology_irq = 0;
  294. }