topology.c 6.1 KB

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