topology.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * arch/s390/kernel/topology.c
  3. *
  4. * Copyright IBM Corp. 2007
  5. * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>
  6. */
  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 <asm/delay.h>
  17. #include <asm/s390_ext.h>
  18. #include <asm/sysinfo.h>
  19. #define CPU_BITS 64
  20. struct tl_cpu {
  21. unsigned char reserved[6];
  22. unsigned short origin;
  23. unsigned long mask[CPU_BITS / BITS_PER_LONG];
  24. };
  25. struct tl_container {
  26. unsigned char reserved[8];
  27. };
  28. union tl_entry {
  29. unsigned char nl;
  30. struct tl_cpu cpu;
  31. struct tl_container container;
  32. };
  33. #define NR_MAG 6
  34. struct tl_info {
  35. unsigned char reserved0[2];
  36. unsigned short length;
  37. unsigned char mag[NR_MAG];
  38. unsigned char reserved1;
  39. unsigned char mnest;
  40. unsigned char reserved2[4];
  41. union tl_entry tle[0];
  42. };
  43. struct core_info {
  44. struct core_info *next;
  45. cpumask_t mask;
  46. };
  47. static void topology_work_fn(struct work_struct *work);
  48. static struct tl_info *tl_info;
  49. static struct core_info core_info;
  50. static int machine_has_topology;
  51. static int machine_has_topology_irq;
  52. static struct timer_list topology_timer;
  53. static void set_topology_timer(void);
  54. static DECLARE_WORK(topology_work, topology_work_fn);
  55. cpumask_t cpu_coregroup_map(unsigned int cpu)
  56. {
  57. struct core_info *core = &core_info;
  58. cpumask_t mask;
  59. cpus_clear(mask);
  60. if (!machine_has_topology)
  61. return cpu_present_map;
  62. mutex_lock(&smp_cpu_state_mutex);
  63. while (core) {
  64. if (cpu_isset(cpu, core->mask)) {
  65. mask = core->mask;
  66. break;
  67. }
  68. core = core->next;
  69. }
  70. mutex_unlock(&smp_cpu_state_mutex);
  71. if (cpus_empty(mask))
  72. mask = cpumask_of_cpu(cpu);
  73. return mask;
  74. }
  75. static void add_cpus_to_core(struct tl_cpu *tl_cpu, struct core_info *core)
  76. {
  77. unsigned int cpu;
  78. for (cpu = find_first_bit(&tl_cpu->mask[0], CPU_BITS);
  79. cpu < CPU_BITS;
  80. cpu = find_next_bit(&tl_cpu->mask[0], CPU_BITS, cpu + 1))
  81. {
  82. unsigned int rcpu, lcpu;
  83. rcpu = CPU_BITS - 1 - cpu + tl_cpu->origin;
  84. for_each_present_cpu(lcpu) {
  85. if (__cpu_logical_map[lcpu] == rcpu)
  86. cpu_set(lcpu, core->mask);
  87. }
  88. }
  89. }
  90. static void clear_cores(void)
  91. {
  92. struct core_info *core = &core_info;
  93. while (core) {
  94. cpus_clear(core->mask);
  95. core = core->next;
  96. }
  97. }
  98. static union tl_entry *next_tle(union tl_entry *tle)
  99. {
  100. if (tle->nl)
  101. return (union tl_entry *)((struct tl_container *)tle + 1);
  102. else
  103. return (union tl_entry *)((struct tl_cpu *)tle + 1);
  104. }
  105. static void tl_to_cores(struct tl_info *info)
  106. {
  107. union tl_entry *tle, *end;
  108. struct core_info *core = &core_info;
  109. mutex_lock(&smp_cpu_state_mutex);
  110. clear_cores();
  111. tle = (union tl_entry *)&info->tle;
  112. end = (union tl_entry *)((unsigned long)info + info->length);
  113. while (tle < end) {
  114. switch (tle->nl) {
  115. case 5:
  116. case 4:
  117. case 3:
  118. case 2:
  119. break;
  120. case 1:
  121. core = core->next;
  122. break;
  123. case 0:
  124. add_cpus_to_core(&tle->cpu, core);
  125. break;
  126. default:
  127. clear_cores();
  128. machine_has_topology = 0;
  129. return;
  130. }
  131. tle = next_tle(tle);
  132. }
  133. mutex_unlock(&smp_cpu_state_mutex);
  134. }
  135. static int ptf(void)
  136. {
  137. int rc;
  138. asm volatile(
  139. " .insn rre,0xb9a20000,%1,%1\n"
  140. " ipm %0\n"
  141. " srl %0,28\n"
  142. : "=d" (rc)
  143. : "d" (2UL) : "cc");
  144. return rc;
  145. }
  146. void arch_update_cpu_topology(void)
  147. {
  148. struct tl_info *info = tl_info;
  149. struct sys_device *sysdev;
  150. int cpu;
  151. if (!machine_has_topology)
  152. return;
  153. ptf();
  154. stsi(info, 15, 1, 2);
  155. tl_to_cores(info);
  156. for_each_online_cpu(cpu) {
  157. sysdev = get_cpu_sysdev(cpu);
  158. kobject_uevent(&sysdev->kobj, KOBJ_CHANGE);
  159. }
  160. }
  161. static void topology_work_fn(struct work_struct *work)
  162. {
  163. arch_reinit_sched_domains();
  164. }
  165. static void topology_timer_fn(unsigned long ignored)
  166. {
  167. if (ptf())
  168. schedule_work(&topology_work);
  169. set_topology_timer();
  170. }
  171. static void set_topology_timer(void)
  172. {
  173. topology_timer.function = topology_timer_fn;
  174. topology_timer.data = 0;
  175. topology_timer.expires = jiffies + 60 * HZ;
  176. add_timer(&topology_timer);
  177. }
  178. static void topology_interrupt(__u16 code)
  179. {
  180. schedule_work(&topology_work);
  181. }
  182. static int __init init_topology_update(void)
  183. {
  184. int rc;
  185. if (!machine_has_topology)
  186. return 0;
  187. init_timer(&topology_timer);
  188. if (machine_has_topology_irq) {
  189. rc = register_external_interrupt(0x2005, topology_interrupt);
  190. if (rc)
  191. return rc;
  192. ctl_set_bit(0, 8);
  193. }
  194. else
  195. set_topology_timer();
  196. return 0;
  197. }
  198. __initcall(init_topology_update);
  199. void __init s390_init_cpu_topology(void)
  200. {
  201. unsigned long long facility_bits;
  202. struct tl_info *info;
  203. struct core_info *core;
  204. int nr_cores;
  205. int i;
  206. if (stfle(&facility_bits, 1) <= 0)
  207. return;
  208. if (!(facility_bits & (1ULL << 52)) || !(facility_bits & (1ULL << 61)))
  209. return;
  210. machine_has_topology = 1;
  211. if (facility_bits & (1ULL << 51))
  212. machine_has_topology_irq = 1;
  213. tl_info = alloc_bootmem_pages(PAGE_SIZE);
  214. if (!tl_info)
  215. goto error;
  216. info = tl_info;
  217. stsi(info, 15, 1, 2);
  218. nr_cores = info->mag[NR_MAG - 2];
  219. for (i = 0; i < info->mnest - 2; i++)
  220. nr_cores *= info->mag[NR_MAG - 3 - i];
  221. printk(KERN_INFO "CPU topology:");
  222. for (i = 0; i < NR_MAG; i++)
  223. printk(" %d", info->mag[i]);
  224. printk(" / %d\n", info->mnest);
  225. core = &core_info;
  226. for (i = 0; i < nr_cores; i++) {
  227. core->next = alloc_bootmem(sizeof(struct core_info));
  228. core = core->next;
  229. if (!core)
  230. goto error;
  231. }
  232. return;
  233. error:
  234. machine_has_topology = 0;
  235. machine_has_topology_irq = 0;
  236. }