Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 1 | /* |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 2 | * Copyright IBM Corp. 2007 |
| 3 | * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com> |
| 4 | */ |
| 5 | |
| 6 | #include <linux/kernel.h> |
| 7 | #include <linux/mm.h> |
| 8 | #include <linux/init.h> |
| 9 | #include <linux/device.h> |
| 10 | #include <linux/bootmem.h> |
| 11 | #include <linux/sched.h> |
Heiko Carstens | fd781fa | 2008-04-30 13:38:41 +0200 | [diff] [blame^] | 12 | #include <linux/kthread.h> |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 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 | |
| 20 | #define CPU_BITS 64 |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 21 | #define NR_MAG 6 |
| 22 | |
| 23 | #define PTF_HORIZONTAL (0UL) |
| 24 | #define PTF_VERTICAL (1UL) |
| 25 | #define PTF_CHECK (2UL) |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 26 | |
| 27 | struct tl_cpu { |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 28 | unsigned char reserved0[4]; |
| 29 | unsigned char :6; |
| 30 | unsigned char pp:2; |
| 31 | unsigned char reserved1; |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 32 | unsigned short origin; |
| 33 | unsigned long mask[CPU_BITS / BITS_PER_LONG]; |
| 34 | }; |
| 35 | |
| 36 | struct tl_container { |
| 37 | unsigned char reserved[8]; |
| 38 | }; |
| 39 | |
| 40 | union tl_entry { |
| 41 | unsigned char nl; |
| 42 | struct tl_cpu cpu; |
| 43 | struct tl_container container; |
| 44 | }; |
| 45 | |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 46 | struct tl_info { |
| 47 | unsigned char reserved0[2]; |
| 48 | unsigned short length; |
| 49 | unsigned char mag[NR_MAG]; |
| 50 | unsigned char reserved1; |
| 51 | unsigned char mnest; |
| 52 | unsigned char reserved2[4]; |
| 53 | union tl_entry tle[0]; |
| 54 | }; |
| 55 | |
| 56 | struct core_info { |
| 57 | struct core_info *next; |
| 58 | cpumask_t mask; |
| 59 | }; |
| 60 | |
| 61 | static void topology_work_fn(struct work_struct *work); |
| 62 | static struct tl_info *tl_info; |
| 63 | static struct core_info core_info; |
| 64 | static int machine_has_topology; |
| 65 | static int machine_has_topology_irq; |
| 66 | static struct timer_list topology_timer; |
| 67 | static void set_topology_timer(void); |
| 68 | static DECLARE_WORK(topology_work, topology_work_fn); |
| 69 | |
Heiko Carstens | d00aa4e | 2008-04-30 13:38:40 +0200 | [diff] [blame] | 70 | cpumask_t cpu_core_map[NR_CPUS]; |
| 71 | |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 72 | cpumask_t cpu_coregroup_map(unsigned int cpu) |
| 73 | { |
| 74 | struct core_info *core = &core_info; |
| 75 | cpumask_t mask; |
| 76 | |
| 77 | cpus_clear(mask); |
| 78 | if (!machine_has_topology) |
| 79 | return cpu_present_map; |
| 80 | mutex_lock(&smp_cpu_state_mutex); |
| 81 | while (core) { |
| 82 | if (cpu_isset(cpu, core->mask)) { |
| 83 | mask = core->mask; |
| 84 | break; |
| 85 | } |
| 86 | core = core->next; |
| 87 | } |
| 88 | mutex_unlock(&smp_cpu_state_mutex); |
| 89 | if (cpus_empty(mask)) |
| 90 | mask = cpumask_of_cpu(cpu); |
| 91 | return mask; |
| 92 | } |
| 93 | |
| 94 | static void add_cpus_to_core(struct tl_cpu *tl_cpu, struct core_info *core) |
| 95 | { |
| 96 | unsigned int cpu; |
| 97 | |
| 98 | for (cpu = find_first_bit(&tl_cpu->mask[0], CPU_BITS); |
| 99 | cpu < CPU_BITS; |
| 100 | cpu = find_next_bit(&tl_cpu->mask[0], CPU_BITS, cpu + 1)) |
| 101 | { |
| 102 | unsigned int rcpu, lcpu; |
| 103 | |
| 104 | rcpu = CPU_BITS - 1 - cpu + tl_cpu->origin; |
| 105 | for_each_present_cpu(lcpu) { |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 106 | if (__cpu_logical_map[lcpu] == rcpu) { |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 107 | cpu_set(lcpu, core->mask); |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 108 | smp_cpu_polarization[lcpu] = tl_cpu->pp; |
| 109 | } |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | static void clear_cores(void) |
| 115 | { |
| 116 | struct core_info *core = &core_info; |
| 117 | |
| 118 | while (core) { |
| 119 | cpus_clear(core->mask); |
| 120 | core = core->next; |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static union tl_entry *next_tle(union tl_entry *tle) |
| 125 | { |
| 126 | if (tle->nl) |
| 127 | return (union tl_entry *)((struct tl_container *)tle + 1); |
| 128 | else |
| 129 | return (union tl_entry *)((struct tl_cpu *)tle + 1); |
| 130 | } |
| 131 | |
| 132 | static void tl_to_cores(struct tl_info *info) |
| 133 | { |
| 134 | union tl_entry *tle, *end; |
| 135 | struct core_info *core = &core_info; |
| 136 | |
| 137 | mutex_lock(&smp_cpu_state_mutex); |
| 138 | clear_cores(); |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 139 | tle = info->tle; |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 140 | end = (union tl_entry *)((unsigned long)info + info->length); |
| 141 | while (tle < end) { |
| 142 | switch (tle->nl) { |
| 143 | case 5: |
| 144 | case 4: |
| 145 | case 3: |
| 146 | case 2: |
| 147 | break; |
| 148 | case 1: |
| 149 | core = core->next; |
| 150 | break; |
| 151 | case 0: |
| 152 | add_cpus_to_core(&tle->cpu, core); |
| 153 | break; |
| 154 | default: |
| 155 | clear_cores(); |
| 156 | machine_has_topology = 0; |
| 157 | return; |
| 158 | } |
| 159 | tle = next_tle(tle); |
| 160 | } |
| 161 | mutex_unlock(&smp_cpu_state_mutex); |
| 162 | } |
| 163 | |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 164 | static void topology_update_polarization_simple(void) |
| 165 | { |
| 166 | int cpu; |
| 167 | |
| 168 | mutex_lock(&smp_cpu_state_mutex); |
| 169 | for_each_present_cpu(cpu) |
| 170 | smp_cpu_polarization[cpu] = POLARIZATION_HRZ; |
| 171 | mutex_unlock(&smp_cpu_state_mutex); |
| 172 | } |
| 173 | |
| 174 | static int ptf(unsigned long fc) |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 175 | { |
| 176 | int rc; |
| 177 | |
| 178 | asm volatile( |
| 179 | " .insn rre,0xb9a20000,%1,%1\n" |
| 180 | " ipm %0\n" |
| 181 | " srl %0,28\n" |
| 182 | : "=d" (rc) |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 183 | : "d" (fc) : "cc"); |
| 184 | return rc; |
| 185 | } |
| 186 | |
| 187 | int topology_set_cpu_management(int fc) |
| 188 | { |
| 189 | int cpu; |
| 190 | int rc; |
| 191 | |
| 192 | if (!machine_has_topology) |
| 193 | return -EOPNOTSUPP; |
| 194 | if (fc) |
| 195 | rc = ptf(PTF_VERTICAL); |
| 196 | else |
| 197 | rc = ptf(PTF_HORIZONTAL); |
| 198 | if (rc) |
| 199 | return -EBUSY; |
| 200 | for_each_present_cpu(cpu) |
| 201 | smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN; |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 202 | return rc; |
| 203 | } |
| 204 | |
Heiko Carstens | d00aa4e | 2008-04-30 13:38:40 +0200 | [diff] [blame] | 205 | static void update_cpu_core_map(void) |
| 206 | { |
| 207 | int cpu; |
| 208 | |
| 209 | for_each_present_cpu(cpu) |
| 210 | cpu_core_map[cpu] = cpu_coregroup_map(cpu); |
| 211 | } |
| 212 | |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 213 | void arch_update_cpu_topology(void) |
| 214 | { |
| 215 | struct tl_info *info = tl_info; |
| 216 | struct sys_device *sysdev; |
| 217 | int cpu; |
| 218 | |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 219 | if (!machine_has_topology) { |
Heiko Carstens | d00aa4e | 2008-04-30 13:38:40 +0200 | [diff] [blame] | 220 | update_cpu_core_map(); |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 221 | topology_update_polarization_simple(); |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 222 | return; |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 223 | } |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 224 | stsi(info, 15, 1, 2); |
| 225 | tl_to_cores(info); |
Heiko Carstens | d00aa4e | 2008-04-30 13:38:40 +0200 | [diff] [blame] | 226 | update_cpu_core_map(); |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 227 | for_each_online_cpu(cpu) { |
| 228 | sysdev = get_cpu_sysdev(cpu); |
| 229 | kobject_uevent(&sysdev->kobj, KOBJ_CHANGE); |
| 230 | } |
| 231 | } |
| 232 | |
Heiko Carstens | fd781fa | 2008-04-30 13:38:41 +0200 | [diff] [blame^] | 233 | static int topology_kthread(void *data) |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 234 | { |
| 235 | arch_reinit_sched_domains(); |
Heiko Carstens | fd781fa | 2008-04-30 13:38:41 +0200 | [diff] [blame^] | 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static void topology_work_fn(struct work_struct *work) |
| 240 | { |
| 241 | /* We can't call arch_reinit_sched_domains() from a multi-threaded |
| 242 | * workqueue context since it may deadlock in case of cpu hotplug. |
| 243 | * So we have to create a kernel thread in order to call |
| 244 | * arch_reinit_sched_domains(). |
| 245 | */ |
| 246 | kthread_run(topology_kthread, NULL, "topology_update"); |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 247 | } |
| 248 | |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 249 | void topology_schedule_update(void) |
| 250 | { |
| 251 | schedule_work(&topology_work); |
| 252 | } |
| 253 | |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 254 | static void topology_timer_fn(unsigned long ignored) |
| 255 | { |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 256 | if (ptf(PTF_CHECK)) |
| 257 | topology_schedule_update(); |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 258 | set_topology_timer(); |
| 259 | } |
| 260 | |
| 261 | static void set_topology_timer(void) |
| 262 | { |
| 263 | topology_timer.function = topology_timer_fn; |
| 264 | topology_timer.data = 0; |
| 265 | topology_timer.expires = jiffies + 60 * HZ; |
| 266 | add_timer(&topology_timer); |
| 267 | } |
| 268 | |
| 269 | static void topology_interrupt(__u16 code) |
| 270 | { |
| 271 | schedule_work(&topology_work); |
| 272 | } |
| 273 | |
| 274 | static int __init init_topology_update(void) |
| 275 | { |
| 276 | int rc; |
| 277 | |
Heiko Carstens | d00aa4e | 2008-04-30 13:38:40 +0200 | [diff] [blame] | 278 | rc = 0; |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 279 | if (!machine_has_topology) { |
| 280 | topology_update_polarization_simple(); |
Heiko Carstens | d00aa4e | 2008-04-30 13:38:40 +0200 | [diff] [blame] | 281 | goto out; |
Heiko Carstens | c10fde0 | 2008-04-17 07:46:13 +0200 | [diff] [blame] | 282 | } |
| 283 | init_timer_deferrable(&topology_timer); |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 284 | if (machine_has_topology_irq) { |
| 285 | rc = register_external_interrupt(0x2005, topology_interrupt); |
| 286 | if (rc) |
Heiko Carstens | d00aa4e | 2008-04-30 13:38:40 +0200 | [diff] [blame] | 287 | goto out; |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 288 | ctl_set_bit(0, 8); |
| 289 | } |
| 290 | else |
| 291 | set_topology_timer(); |
Heiko Carstens | d00aa4e | 2008-04-30 13:38:40 +0200 | [diff] [blame] | 292 | out: |
| 293 | update_cpu_core_map(); |
| 294 | return rc; |
Heiko Carstens | dbd70fb | 2008-04-17 07:46:12 +0200 | [diff] [blame] | 295 | } |
| 296 | __initcall(init_topology_update); |
| 297 | |
| 298 | void __init s390_init_cpu_topology(void) |
| 299 | { |
| 300 | unsigned long long facility_bits; |
| 301 | struct tl_info *info; |
| 302 | struct core_info *core; |
| 303 | int nr_cores; |
| 304 | int i; |
| 305 | |
| 306 | if (stfle(&facility_bits, 1) <= 0) |
| 307 | return; |
| 308 | if (!(facility_bits & (1ULL << 52)) || !(facility_bits & (1ULL << 61))) |
| 309 | return; |
| 310 | machine_has_topology = 1; |
| 311 | |
| 312 | if (facility_bits & (1ULL << 51)) |
| 313 | machine_has_topology_irq = 1; |
| 314 | |
| 315 | tl_info = alloc_bootmem_pages(PAGE_SIZE); |
| 316 | if (!tl_info) |
| 317 | goto error; |
| 318 | info = tl_info; |
| 319 | stsi(info, 15, 1, 2); |
| 320 | |
| 321 | nr_cores = info->mag[NR_MAG - 2]; |
| 322 | for (i = 0; i < info->mnest - 2; i++) |
| 323 | nr_cores *= info->mag[NR_MAG - 3 - i]; |
| 324 | |
| 325 | printk(KERN_INFO "CPU topology:"); |
| 326 | for (i = 0; i < NR_MAG; i++) |
| 327 | printk(" %d", info->mag[i]); |
| 328 | printk(" / %d\n", info->mnest); |
| 329 | |
| 330 | core = &core_info; |
| 331 | for (i = 0; i < nr_cores; i++) { |
| 332 | core->next = alloc_bootmem(sizeof(struct core_info)); |
| 333 | core = core->next; |
| 334 | if (!core) |
| 335 | goto error; |
| 336 | } |
| 337 | return; |
| 338 | error: |
| 339 | machine_has_topology = 0; |
| 340 | machine_has_topology_irq = 0; |
| 341 | } |