1 /* 2 * Copyright IBM Corp. 2007, 2011 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/workqueue.h> 10 #include <linux/bootmem.h> 11 #include <linux/cpuset.h> 12 #include <linux/device.h> 13 #include <linux/export.h> 14 #include <linux/kernel.h> 15 #include <linux/sched.h> 16 #include <linux/delay.h> 17 #include <linux/init.h> 18 #include <linux/slab.h> 19 #include <linux/cpu.h> 20 #include <linux/smp.h> 21 #include <linux/mm.h> 22 #include <linux/nodemask.h> 23 #include <linux/node.h> 24 #include <asm/sysinfo.h> 25 #include <asm/numa.h> 26 27 #define PTF_HORIZONTAL (0UL) 28 #define PTF_VERTICAL (1UL) 29 #define PTF_CHECK (2UL) 30 31 struct mask_info { 32 struct mask_info *next; 33 unsigned char id; 34 cpumask_t mask; 35 }; 36 37 static void set_topology_timer(void); 38 static void topology_work_fn(struct work_struct *work); 39 static struct sysinfo_15_1_x *tl_info; 40 41 static DECLARE_WORK(topology_work, topology_work_fn); 42 43 /* 44 * Socket/Book linked lists and cpu_topology updates are 45 * protected by "sched_domains_mutex". 46 */ 47 static struct mask_info socket_info; 48 static struct mask_info book_info; 49 static struct mask_info drawer_info; 50 51 struct cpu_topology_s390 cpu_topology[NR_CPUS]; 52 EXPORT_SYMBOL_GPL(cpu_topology); 53 54 cpumask_t cpus_with_topology; 55 56 static cpumask_t cpu_group_map(struct mask_info *info, unsigned int cpu) 57 { 58 cpumask_t mask; 59 60 cpumask_copy(&mask, cpumask_of(cpu)); 61 if (!MACHINE_HAS_TOPOLOGY) 62 return mask; 63 for (; info; info = info->next) { 64 if (cpumask_test_cpu(cpu, &info->mask)) 65 return info->mask; 66 } 67 return mask; 68 } 69 70 static cpumask_t cpu_thread_map(unsigned int cpu) 71 { 72 cpumask_t mask; 73 int i; 74 75 cpumask_copy(&mask, cpumask_of(cpu)); 76 if (!MACHINE_HAS_TOPOLOGY) 77 return mask; 78 cpu -= cpu % (smp_cpu_mtid + 1); 79 for (i = 0; i <= smp_cpu_mtid; i++) 80 if (cpu_present(cpu + i)) 81 cpumask_set_cpu(cpu + i, &mask); 82 return mask; 83 } 84 85 static void add_cpus_to_mask(struct topology_core *tl_core, 86 struct mask_info *drawer, 87 struct mask_info *book, 88 struct mask_info *socket) 89 { 90 struct cpu_topology_s390 *topo; 91 unsigned int core; 92 93 for_each_set_bit(core, &tl_core->mask[0], TOPOLOGY_CORE_BITS) { 94 unsigned int rcore; 95 int lcpu, i; 96 97 rcore = TOPOLOGY_CORE_BITS - 1 - core + tl_core->origin; 98 lcpu = smp_find_processor_id(rcore << smp_cpu_mt_shift); 99 if (lcpu < 0) 100 continue; 101 for (i = 0; i <= smp_cpu_mtid; i++) { 102 topo = &cpu_topology[lcpu + i]; 103 topo->drawer_id = drawer->id; 104 topo->book_id = book->id; 105 topo->socket_id = socket->id; 106 topo->core_id = rcore; 107 topo->thread_id = lcpu + i; 108 cpumask_set_cpu(lcpu + i, &drawer->mask); 109 cpumask_set_cpu(lcpu + i, &book->mask); 110 cpumask_set_cpu(lcpu + i, &socket->mask); 111 cpumask_set_cpu(lcpu + i, &cpus_with_topology); 112 smp_cpu_set_polarization(lcpu + i, tl_core->pp); 113 } 114 } 115 } 116 117 static void clear_masks(void) 118 { 119 struct mask_info *info; 120 121 info = &socket_info; 122 while (info) { 123 cpumask_clear(&info->mask); 124 info = info->next; 125 } 126 info = &book_info; 127 while (info) { 128 cpumask_clear(&info->mask); 129 info = info->next; 130 } 131 info = &drawer_info; 132 while (info) { 133 cpumask_clear(&info->mask); 134 info = info->next; 135 } 136 } 137 138 static union topology_entry *next_tle(union topology_entry *tle) 139 { 140 if (!tle->nl) 141 return (union topology_entry *)((struct topology_core *)tle + 1); 142 return (union topology_entry *)((struct topology_container *)tle + 1); 143 } 144 145 static void tl_to_masks(struct sysinfo_15_1_x *info) 146 { 147 struct mask_info *socket = &socket_info; 148 struct mask_info *book = &book_info; 149 struct mask_info *drawer = &drawer_info; 150 union topology_entry *tle, *end; 151 152 clear_masks(); 153 tle = info->tle; 154 end = (union topology_entry *)((unsigned long)info + info->length); 155 while (tle < end) { 156 switch (tle->nl) { 157 case 3: 158 drawer = drawer->next; 159 drawer->id = tle->container.id; 160 break; 161 case 2: 162 book = book->next; 163 book->id = tle->container.id; 164 break; 165 case 1: 166 socket = socket->next; 167 socket->id = tle->container.id; 168 break; 169 case 0: 170 add_cpus_to_mask(&tle->cpu, drawer, book, socket); 171 break; 172 default: 173 clear_masks(); 174 return; 175 } 176 tle = next_tle(tle); 177 } 178 } 179 180 static void topology_update_polarization_simple(void) 181 { 182 int cpu; 183 184 mutex_lock(&smp_cpu_state_mutex); 185 for_each_possible_cpu(cpu) 186 smp_cpu_set_polarization(cpu, POLARIZATION_HRZ); 187 mutex_unlock(&smp_cpu_state_mutex); 188 } 189 190 static int ptf(unsigned long fc) 191 { 192 int rc; 193 194 asm volatile( 195 " .insn rre,0xb9a20000,%1,%1\n" 196 " ipm %0\n" 197 " srl %0,28\n" 198 : "=d" (rc) 199 : "d" (fc) : "cc"); 200 return rc; 201 } 202 203 int topology_set_cpu_management(int fc) 204 { 205 int cpu, rc; 206 207 if (!MACHINE_HAS_TOPOLOGY) 208 return -EOPNOTSUPP; 209 if (fc) 210 rc = ptf(PTF_VERTICAL); 211 else 212 rc = ptf(PTF_HORIZONTAL); 213 if (rc) 214 return -EBUSY; 215 for_each_possible_cpu(cpu) 216 smp_cpu_set_polarization(cpu, POLARIZATION_UNKNOWN); 217 return rc; 218 } 219 220 static void update_cpu_masks(void) 221 { 222 struct cpu_topology_s390 *topo; 223 int cpu; 224 225 for_each_possible_cpu(cpu) { 226 topo = &cpu_topology[cpu]; 227 topo->thread_mask = cpu_thread_map(cpu); 228 topo->core_mask = cpu_group_map(&socket_info, cpu); 229 topo->book_mask = cpu_group_map(&book_info, cpu); 230 topo->drawer_mask = cpu_group_map(&drawer_info, cpu); 231 if (!MACHINE_HAS_TOPOLOGY) { 232 topo->thread_id = cpu; 233 topo->core_id = cpu; 234 topo->socket_id = cpu; 235 topo->book_id = cpu; 236 topo->drawer_id = cpu; 237 if (cpu_present(cpu)) 238 cpumask_set_cpu(cpu, &cpus_with_topology); 239 } 240 } 241 numa_update_cpu_topology(); 242 } 243 244 void store_topology(struct sysinfo_15_1_x *info) 245 { 246 stsi(info, 15, 1, min(topology_max_mnest, 4)); 247 } 248 249 static int __arch_update_cpu_topology(void) 250 { 251 struct sysinfo_15_1_x *info = tl_info; 252 int rc = 0; 253 254 cpumask_clear(&cpus_with_topology); 255 if (MACHINE_HAS_TOPOLOGY) { 256 rc = 1; 257 store_topology(info); 258 tl_to_masks(info); 259 } 260 update_cpu_masks(); 261 if (!MACHINE_HAS_TOPOLOGY) 262 topology_update_polarization_simple(); 263 return rc; 264 } 265 266 int arch_update_cpu_topology(void) 267 { 268 struct device *dev; 269 int cpu, rc; 270 271 rc = __arch_update_cpu_topology(); 272 for_each_online_cpu(cpu) { 273 dev = get_cpu_device(cpu); 274 kobject_uevent(&dev->kobj, KOBJ_CHANGE); 275 } 276 return rc; 277 } 278 279 static void topology_work_fn(struct work_struct *work) 280 { 281 rebuild_sched_domains(); 282 } 283 284 void topology_schedule_update(void) 285 { 286 schedule_work(&topology_work); 287 } 288 289 static void topology_timer_fn(unsigned long ignored) 290 { 291 if (ptf(PTF_CHECK)) 292 topology_schedule_update(); 293 set_topology_timer(); 294 } 295 296 static struct timer_list topology_timer = 297 TIMER_DEFERRED_INITIALIZER(topology_timer_fn, 0, 0); 298 299 static atomic_t topology_poll = ATOMIC_INIT(0); 300 301 static void set_topology_timer(void) 302 { 303 if (atomic_add_unless(&topology_poll, -1, 0)) 304 mod_timer(&topology_timer, jiffies + HZ / 10); 305 else 306 mod_timer(&topology_timer, jiffies + HZ * 60); 307 } 308 309 void topology_expect_change(void) 310 { 311 if (!MACHINE_HAS_TOPOLOGY) 312 return; 313 /* This is racy, but it doesn't matter since it is just a heuristic. 314 * Worst case is that we poll in a higher frequency for a bit longer. 315 */ 316 if (atomic_read(&topology_poll) > 60) 317 return; 318 atomic_add(60, &topology_poll); 319 set_topology_timer(); 320 } 321 322 static int cpu_management; 323 324 static ssize_t dispatching_show(struct device *dev, 325 struct device_attribute *attr, 326 char *buf) 327 { 328 ssize_t count; 329 330 mutex_lock(&smp_cpu_state_mutex); 331 count = sprintf(buf, "%d\n", cpu_management); 332 mutex_unlock(&smp_cpu_state_mutex); 333 return count; 334 } 335 336 static ssize_t dispatching_store(struct device *dev, 337 struct device_attribute *attr, 338 const char *buf, 339 size_t count) 340 { 341 int val, rc; 342 char delim; 343 344 if (sscanf(buf, "%d %c", &val, &delim) != 1) 345 return -EINVAL; 346 if (val != 0 && val != 1) 347 return -EINVAL; 348 rc = 0; 349 get_online_cpus(); 350 mutex_lock(&smp_cpu_state_mutex); 351 if (cpu_management == val) 352 goto out; 353 rc = topology_set_cpu_management(val); 354 if (rc) 355 goto out; 356 cpu_management = val; 357 topology_expect_change(); 358 out: 359 mutex_unlock(&smp_cpu_state_mutex); 360 put_online_cpus(); 361 return rc ? rc : count; 362 } 363 static DEVICE_ATTR(dispatching, 0644, dispatching_show, 364 dispatching_store); 365 366 static ssize_t cpu_polarization_show(struct device *dev, 367 struct device_attribute *attr, char *buf) 368 { 369 int cpu = dev->id; 370 ssize_t count; 371 372 mutex_lock(&smp_cpu_state_mutex); 373 switch (smp_cpu_get_polarization(cpu)) { 374 case POLARIZATION_HRZ: 375 count = sprintf(buf, "horizontal\n"); 376 break; 377 case POLARIZATION_VL: 378 count = sprintf(buf, "vertical:low\n"); 379 break; 380 case POLARIZATION_VM: 381 count = sprintf(buf, "vertical:medium\n"); 382 break; 383 case POLARIZATION_VH: 384 count = sprintf(buf, "vertical:high\n"); 385 break; 386 default: 387 count = sprintf(buf, "unknown\n"); 388 break; 389 } 390 mutex_unlock(&smp_cpu_state_mutex); 391 return count; 392 } 393 static DEVICE_ATTR(polarization, 0444, cpu_polarization_show, NULL); 394 395 static struct attribute *topology_cpu_attrs[] = { 396 &dev_attr_polarization.attr, 397 NULL, 398 }; 399 400 static struct attribute_group topology_cpu_attr_group = { 401 .attrs = topology_cpu_attrs, 402 }; 403 404 int topology_cpu_init(struct cpu *cpu) 405 { 406 return sysfs_create_group(&cpu->dev.kobj, &topology_cpu_attr_group); 407 } 408 409 static const struct cpumask *cpu_thread_mask(int cpu) 410 { 411 return &cpu_topology[cpu].thread_mask; 412 } 413 414 415 const struct cpumask *cpu_coregroup_mask(int cpu) 416 { 417 return &cpu_topology[cpu].core_mask; 418 } 419 420 static const struct cpumask *cpu_book_mask(int cpu) 421 { 422 return &cpu_topology[cpu].book_mask; 423 } 424 425 static const struct cpumask *cpu_drawer_mask(int cpu) 426 { 427 return &cpu_topology[cpu].drawer_mask; 428 } 429 430 static struct sched_domain_topology_level s390_topology[] = { 431 { cpu_thread_mask, cpu_smt_flags, SD_INIT_NAME(SMT) }, 432 { cpu_coregroup_mask, cpu_core_flags, SD_INIT_NAME(MC) }, 433 { cpu_book_mask, SD_INIT_NAME(BOOK) }, 434 { cpu_drawer_mask, SD_INIT_NAME(DRAWER) }, 435 { cpu_cpu_mask, SD_INIT_NAME(DIE) }, 436 { NULL, }, 437 }; 438 439 static void __init alloc_masks(struct sysinfo_15_1_x *info, 440 struct mask_info *mask, int offset) 441 { 442 int i, nr_masks; 443 444 nr_masks = info->mag[TOPOLOGY_NR_MAG - offset]; 445 for (i = 0; i < info->mnest - offset; i++) 446 nr_masks *= info->mag[TOPOLOGY_NR_MAG - offset - 1 - i]; 447 nr_masks = max(nr_masks, 1); 448 for (i = 0; i < nr_masks; i++) { 449 mask->next = memblock_virt_alloc(sizeof(*mask->next), 8); 450 mask = mask->next; 451 } 452 } 453 454 void __init topology_init_early(void) 455 { 456 struct sysinfo_15_1_x *info; 457 458 set_sched_topology(s390_topology); 459 if (!MACHINE_HAS_TOPOLOGY) 460 goto out; 461 tl_info = memblock_virt_alloc(PAGE_SIZE, PAGE_SIZE); 462 info = tl_info; 463 store_topology(info); 464 pr_info("The CPU configuration topology of the machine is: %d %d %d %d %d %d / %d\n", 465 info->mag[0], info->mag[1], info->mag[2], info->mag[3], 466 info->mag[4], info->mag[5], info->mnest); 467 alloc_masks(info, &socket_info, 1); 468 alloc_masks(info, &book_info, 2); 469 alloc_masks(info, &drawer_info, 3); 470 out: 471 __arch_update_cpu_topology(); 472 } 473 474 static int __init topology_init(void) 475 { 476 if (MACHINE_HAS_TOPOLOGY) 477 set_topology_timer(); 478 else 479 topology_update_polarization_simple(); 480 return device_create_file(cpu_subsys.dev_root, &dev_attr_dispatching); 481 } 482 device_initcall(topology_init); 483