topology.c 6.4 KB

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