topology.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. goto out;
  147. }
  148. tle = next_tle(tle);
  149. }
  150. out:
  151. spin_unlock_irq(&topology_lock);
  152. }
  153. static void topology_update_polarization_simple(void)
  154. {
  155. int cpu;
  156. mutex_lock(&smp_cpu_state_mutex);
  157. for_each_possible_cpu(cpu)
  158. smp_cpu_polarization[cpu] = POLARIZATION_HRZ;
  159. mutex_unlock(&smp_cpu_state_mutex);
  160. }
  161. static int ptf(unsigned long fc)
  162. {
  163. int rc;
  164. asm volatile(
  165. " .insn rre,0xb9a20000,%1,%1\n"
  166. " ipm %0\n"
  167. " srl %0,28\n"
  168. : "=d" (rc)
  169. : "d" (fc) : "cc");
  170. return rc;
  171. }
  172. int topology_set_cpu_management(int fc)
  173. {
  174. int cpu;
  175. int rc;
  176. if (!machine_has_topology)
  177. return -EOPNOTSUPP;
  178. if (fc)
  179. rc = ptf(PTF_VERTICAL);
  180. else
  181. rc = ptf(PTF_HORIZONTAL);
  182. if (rc)
  183. return -EBUSY;
  184. for_each_possible_cpu(cpu)
  185. smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN;
  186. return rc;
  187. }
  188. static void update_cpu_core_map(void)
  189. {
  190. int cpu;
  191. for_each_possible_cpu(cpu)
  192. cpu_core_map[cpu] = cpu_coregroup_map(cpu);
  193. }
  194. int arch_update_cpu_topology(void)
  195. {
  196. struct tl_info *info = tl_info;
  197. struct sys_device *sysdev;
  198. int cpu;
  199. if (!machine_has_topology) {
  200. update_cpu_core_map();
  201. topology_update_polarization_simple();
  202. return 0;
  203. }
  204. stsi(info, 15, 1, 2);
  205. tl_to_cores(info);
  206. update_cpu_core_map();
  207. for_each_online_cpu(cpu) {
  208. sysdev = get_cpu_sysdev(cpu);
  209. kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
  210. }
  211. return 1;
  212. }
  213. static void topology_work_fn(struct work_struct *work)
  214. {
  215. rebuild_sched_domains();
  216. }
  217. void topology_schedule_update(void)
  218. {
  219. schedule_work(&topology_work);
  220. }
  221. static void topology_timer_fn(unsigned long ignored)
  222. {
  223. if (ptf(PTF_CHECK))
  224. topology_schedule_update();
  225. set_topology_timer();
  226. }
  227. static void set_topology_timer(void)
  228. {
  229. topology_timer.function = topology_timer_fn;
  230. topology_timer.data = 0;
  231. topology_timer.expires = jiffies + 60 * HZ;
  232. add_timer(&topology_timer);
  233. }
  234. static int __init early_parse_topology(char *p)
  235. {
  236. if (strncmp(p, "on", 2))
  237. return 0;
  238. topology_enabled = 1;
  239. return 0;
  240. }
  241. early_param("topology", early_parse_topology);
  242. static int __init init_topology_update(void)
  243. {
  244. int rc;
  245. rc = 0;
  246. if (!machine_has_topology) {
  247. topology_update_polarization_simple();
  248. goto out;
  249. }
  250. init_timer_deferrable(&topology_timer);
  251. set_topology_timer();
  252. out:
  253. update_cpu_core_map();
  254. return rc;
  255. }
  256. __initcall(init_topology_update);
  257. void __init s390_init_cpu_topology(void)
  258. {
  259. unsigned long long facility_bits;
  260. struct tl_info *info;
  261. struct core_info *core;
  262. int nr_cores;
  263. int i;
  264. if (stfle(&facility_bits, 1) <= 0)
  265. return;
  266. if (!(facility_bits & (1ULL << 52)) || !(facility_bits & (1ULL << 61)))
  267. return;
  268. machine_has_topology = 1;
  269. tl_info = alloc_bootmem_pages(PAGE_SIZE);
  270. info = tl_info;
  271. stsi(info, 15, 1, 2);
  272. nr_cores = info->mag[NR_MAG - 2];
  273. for (i = 0; i < info->mnest - 2; i++)
  274. nr_cores *= info->mag[NR_MAG - 3 - i];
  275. pr_info("The CPU configuration topology of the machine is:");
  276. for (i = 0; i < NR_MAG; i++)
  277. printk(" %d", info->mag[i]);
  278. printk(" / %d\n", info->mnest);
  279. core = &core_info;
  280. for (i = 0; i < nr_cores; i++) {
  281. core->next = alloc_bootmem(sizeof(struct core_info));
  282. core = core->next;
  283. if (!core)
  284. goto error;
  285. }
  286. return;
  287. error:
  288. machine_has_topology = 0;
  289. }