1 /* 2 * Copyright IBM Corp. 2007 3 * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com> 4 */ 5 6 #define KMSG_COMPONENT "cpu" 7 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt 8 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> 18 #include <linux/cpuset.h> 19 #include <asm/delay.h> 20 21 #define PTF_HORIZONTAL (0UL) 22 #define PTF_VERTICAL (1UL) 23 #define PTF_CHECK (2UL) 24 25 struct mask_info { 26 struct mask_info *next; 27 unsigned char id; 28 cpumask_t mask; 29 }; 30 31 static int topology_enabled = 1; 32 static void topology_work_fn(struct work_struct *work); 33 static struct sysinfo_15_1_x *tl_info; 34 static struct timer_list topology_timer; 35 static void set_topology_timer(void); 36 static DECLARE_WORK(topology_work, topology_work_fn); 37 /* topology_lock protects the core linked list */ 38 static DEFINE_SPINLOCK(topology_lock); 39 40 static struct mask_info core_info; 41 cpumask_t cpu_core_map[NR_CPUS]; 42 unsigned char cpu_core_id[NR_CPUS]; 43 44 #ifdef CONFIG_SCHED_BOOK 45 static struct mask_info book_info; 46 cpumask_t cpu_book_map[NR_CPUS]; 47 unsigned char cpu_book_id[NR_CPUS]; 48 #endif 49 50 static cpumask_t cpu_group_map(struct mask_info *info, unsigned int cpu) 51 { 52 cpumask_t mask; 53 54 cpumask_clear(&mask); 55 if (!topology_enabled || !MACHINE_HAS_TOPOLOGY) { 56 cpumask_copy(&mask, cpumask_of(cpu)); 57 return mask; 58 } 59 while (info) { 60 if (cpumask_test_cpu(cpu, &info->mask)) { 61 mask = info->mask; 62 break; 63 } 64 info = info->next; 65 } 66 if (cpumask_empty(&mask)) 67 cpumask_copy(&mask, cpumask_of(cpu)); 68 return mask; 69 } 70 71 static struct mask_info *add_cpus_to_mask(struct topology_cpu *tl_cpu, 72 struct mask_info *book, 73 struct mask_info *core, 74 int z10) 75 { 76 unsigned int cpu; 77 78 for (cpu = find_first_bit(&tl_cpu->mask[0], TOPOLOGY_CPU_BITS); 79 cpu < TOPOLOGY_CPU_BITS; 80 cpu = find_next_bit(&tl_cpu->mask[0], TOPOLOGY_CPU_BITS, cpu + 1)) 81 { 82 unsigned int rcpu, lcpu; 83 84 rcpu = TOPOLOGY_CPU_BITS - 1 - cpu + tl_cpu->origin; 85 for_each_present_cpu(lcpu) { 86 if (cpu_logical_map(lcpu) != rcpu) 87 continue; 88 #ifdef CONFIG_SCHED_BOOK 89 cpumask_set_cpu(lcpu, &book->mask); 90 cpu_book_id[lcpu] = book->id; 91 #endif 92 cpumask_set_cpu(lcpu, &core->mask); 93 if (z10) { 94 cpu_core_id[lcpu] = rcpu; 95 core = core->next; 96 } else { 97 cpu_core_id[lcpu] = core->id; 98 } 99 smp_cpu_polarization[lcpu] = tl_cpu->pp; 100 } 101 } 102 return core; 103 } 104 105 static void clear_masks(void) 106 { 107 struct mask_info *info; 108 109 info = &core_info; 110 while (info) { 111 cpumask_clear(&info->mask); 112 info = info->next; 113 } 114 #ifdef CONFIG_SCHED_BOOK 115 info = &book_info; 116 while (info) { 117 cpumask_clear(&info->mask); 118 info = info->next; 119 } 120 #endif 121 } 122 123 static union topology_entry *next_tle(union topology_entry *tle) 124 { 125 if (!tle->nl) 126 return (union topology_entry *)((struct topology_cpu *)tle + 1); 127 return (union topology_entry *)((struct topology_container *)tle + 1); 128 } 129 130 static void tl_to_cores(struct sysinfo_15_1_x *info) 131 { 132 #ifdef CONFIG_SCHED_BOOK 133 struct mask_info *book = &book_info; 134 struct cpuid cpu_id; 135 #else 136 struct mask_info *book = NULL; 137 #endif 138 struct mask_info *core = &core_info; 139 union topology_entry *tle, *end; 140 int z10 = 0; 141 142 #ifdef CONFIG_SCHED_BOOK 143 get_cpu_id(&cpu_id); 144 z10 = cpu_id.machine == 0x2097 || cpu_id.machine == 0x2098; 145 #endif 146 spin_lock_irq(&topology_lock); 147 clear_masks(); 148 tle = info->tle; 149 end = (union topology_entry *)((unsigned long)info + info->length); 150 while (tle < end) { 151 #ifdef CONFIG_SCHED_BOOK 152 if (z10) { 153 switch (tle->nl) { 154 case 1: 155 book = book->next; 156 book->id = tle->container.id; 157 break; 158 case 0: 159 core = add_cpus_to_mask(&tle->cpu, book, core, z10); 160 break; 161 default: 162 clear_masks(); 163 goto out; 164 } 165 tle = next_tle(tle); 166 continue; 167 } 168 #endif 169 switch (tle->nl) { 170 #ifdef CONFIG_SCHED_BOOK 171 case 2: 172 book = book->next; 173 book->id = tle->container.id; 174 break; 175 #endif 176 case 1: 177 core = core->next; 178 core->id = tle->container.id; 179 break; 180 case 0: 181 add_cpus_to_mask(&tle->cpu, book, core, z10); 182 break; 183 default: 184 clear_masks(); 185 goto out; 186 } 187 tle = next_tle(tle); 188 } 189 out: 190 spin_unlock_irq(&topology_lock); 191 } 192 193 static void topology_update_polarization_simple(void) 194 { 195 int cpu; 196 197 mutex_lock(&smp_cpu_state_mutex); 198 for_each_possible_cpu(cpu) 199 smp_cpu_polarization[cpu] = POLARIZATION_HRZ; 200 mutex_unlock(&smp_cpu_state_mutex); 201 } 202 203 static int ptf(unsigned long fc) 204 { 205 int rc; 206 207 asm volatile( 208 " .insn rre,0xb9a20000,%1,%1\n" 209 " ipm %0\n" 210 " srl %0,28\n" 211 : "=d" (rc) 212 : "d" (fc) : "cc"); 213 return rc; 214 } 215 216 int topology_set_cpu_management(int fc) 217 { 218 int cpu; 219 int rc; 220 221 if (!MACHINE_HAS_TOPOLOGY) 222 return -EOPNOTSUPP; 223 if (fc) 224 rc = ptf(PTF_VERTICAL); 225 else 226 rc = ptf(PTF_HORIZONTAL); 227 if (rc) 228 return -EBUSY; 229 for_each_possible_cpu(cpu) 230 smp_cpu_polarization[cpu] = POLARIZATION_UNKNWN; 231 return rc; 232 } 233 234 static void update_cpu_core_map(void) 235 { 236 unsigned long flags; 237 int cpu; 238 239 spin_lock_irqsave(&topology_lock, flags); 240 for_each_possible_cpu(cpu) { 241 cpu_core_map[cpu] = cpu_group_map(&core_info, cpu); 242 #ifdef CONFIG_SCHED_BOOK 243 cpu_book_map[cpu] = cpu_group_map(&book_info, cpu); 244 #endif 245 } 246 spin_unlock_irqrestore(&topology_lock, flags); 247 } 248 249 void store_topology(struct sysinfo_15_1_x *info) 250 { 251 #ifdef CONFIG_SCHED_BOOK 252 int rc; 253 254 rc = stsi(info, 15, 1, 3); 255 if (rc != -ENOSYS) 256 return; 257 #endif 258 stsi(info, 15, 1, 2); 259 } 260 261 int arch_update_cpu_topology(void) 262 { 263 struct sysinfo_15_1_x *info = tl_info; 264 struct sys_device *sysdev; 265 int cpu; 266 267 if (!MACHINE_HAS_TOPOLOGY) { 268 update_cpu_core_map(); 269 topology_update_polarization_simple(); 270 return 0; 271 } 272 store_topology(info); 273 tl_to_cores(info); 274 update_cpu_core_map(); 275 for_each_online_cpu(cpu) { 276 sysdev = get_cpu_sysdev(cpu); 277 kobject_uevent(&sysdev->kobj, KOBJ_CHANGE); 278 } 279 return 1; 280 } 281 282 static void topology_work_fn(struct work_struct *work) 283 { 284 rebuild_sched_domains(); 285 } 286 287 void topology_schedule_update(void) 288 { 289 schedule_work(&topology_work); 290 } 291 292 static void topology_timer_fn(unsigned long ignored) 293 { 294 if (ptf(PTF_CHECK)) 295 topology_schedule_update(); 296 set_topology_timer(); 297 } 298 299 static void set_topology_timer(void) 300 { 301 topology_timer.function = topology_timer_fn; 302 topology_timer.data = 0; 303 topology_timer.expires = jiffies + 60 * HZ; 304 add_timer(&topology_timer); 305 } 306 307 static int __init early_parse_topology(char *p) 308 { 309 if (strncmp(p, "off", 3)) 310 return 0; 311 topology_enabled = 0; 312 return 0; 313 } 314 early_param("topology", early_parse_topology); 315 316 static int __init init_topology_update(void) 317 { 318 int rc; 319 320 rc = 0; 321 if (!MACHINE_HAS_TOPOLOGY) { 322 topology_update_polarization_simple(); 323 goto out; 324 } 325 init_timer_deferrable(&topology_timer); 326 set_topology_timer(); 327 out: 328 update_cpu_core_map(); 329 return rc; 330 } 331 __initcall(init_topology_update); 332 333 static void __init alloc_masks(struct sysinfo_15_1_x *info, 334 struct mask_info *mask, int offset) 335 { 336 int i, nr_masks; 337 338 nr_masks = info->mag[TOPOLOGY_NR_MAG - offset]; 339 for (i = 0; i < info->mnest - offset; i++) 340 nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i]; 341 nr_masks = max(nr_masks, 1); 342 for (i = 0; i < nr_masks; i++) { 343 mask->next = alloc_bootmem(sizeof(struct mask_info)); 344 mask = mask->next; 345 } 346 } 347 348 void __init s390_init_cpu_topology(void) 349 { 350 struct sysinfo_15_1_x *info; 351 int i; 352 353 if (!MACHINE_HAS_TOPOLOGY) 354 return; 355 tl_info = alloc_bootmem_pages(PAGE_SIZE); 356 info = tl_info; 357 store_topology(info); 358 pr_info("The CPU configuration topology of the machine is:"); 359 for (i = 0; i < TOPOLOGY_NR_MAG; i++) 360 printk(" %d", info->mag[i]); 361 printk(" / %d\n", info->mnest); 362 alloc_masks(info, &core_info, 1); 363 #ifdef CONFIG_SCHED_BOOK 364 alloc_masks(info, &book_info, 2); 365 #endif 366 } 367