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