topology.c 6.3 KB

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