topology.c 6.7 KB

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