topology.c 6.5 KB

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