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