topology.c 6.4 KB

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