topology.c 6.5 KB

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