1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright IBM Corp. 2001, 2009 4 * Author(s): Ulrich Weigand <[email protected]>, 5 * Martin Schwidefsky <[email protected]>, 6 */ 7 8 #include <linux/cpufeature.h> 9 #include <linux/debugfs.h> 10 #include <linux/kernel.h> 11 #include <linux/mm.h> 12 #include <linux/proc_fs.h> 13 #include <linux/seq_file.h> 14 #include <linux/init.h> 15 #include <linux/delay.h> 16 #include <linux/export.h> 17 #include <linux/slab.h> 18 #include <asm/asm-extable.h> 19 #include <asm/ebcdic.h> 20 #include <asm/debug.h> 21 #include <asm/sysinfo.h> 22 #include <asm/cpcmd.h> 23 #include <asm/topology.h> 24 #include <asm/fpu.h> 25 26 int topology_max_mnest; 27 28 static inline int __stsi(void *sysinfo, int fc, int sel1, int sel2, int *lvl) 29 { 30 int r0 = (fc << 28) | sel1; 31 int rc = 0; 32 33 asm volatile( 34 " lr 0,%[r0]\n" 35 " lr 1,%[r1]\n" 36 " stsi 0(%[sysinfo])\n" 37 "0: jz 2f\n" 38 "1: lhi %[rc],%[retval]\n" 39 "2: lr %[r0],0\n" 40 EX_TABLE(0b, 1b) 41 : [r0] "+d" (r0), [rc] "+d" (rc) 42 : [r1] "d" (sel2), 43 [sysinfo] "a" (sysinfo), 44 [retval] "K" (-EOPNOTSUPP) 45 : "cc", "0", "1", "memory"); 46 *lvl = ((unsigned int) r0) >> 28; 47 return rc; 48 } 49 50 /* 51 * stsi - store system information 52 * 53 * Returns the current configuration level if function code 0 was specified. 54 * Otherwise returns 0 on success or a negative value on error. 55 */ 56 int stsi(void *sysinfo, int fc, int sel1, int sel2) 57 { 58 int lvl, rc; 59 60 rc = __stsi(sysinfo, fc, sel1, sel2, &lvl); 61 if (rc) 62 return rc; 63 return fc ? 0 : lvl; 64 } 65 EXPORT_SYMBOL(stsi); 66 67 #ifdef CONFIG_PROC_FS 68 69 static bool convert_ext_name(unsigned char encoding, char *name, size_t len) 70 { 71 switch (encoding) { 72 case 1: /* EBCDIC */ 73 EBCASC(name, len); 74 break; 75 case 2: /* UTF-8 */ 76 break; 77 default: 78 return false; 79 } 80 return true; 81 } 82 83 static void stsi_1_1_1(struct seq_file *m, struct sysinfo_1_1_1 *info) 84 { 85 bool has_var_cap; 86 int i; 87 88 if (stsi(info, 1, 1, 1)) 89 return; 90 has_var_cap = !!info->model_var_cap[0]; 91 EBCASC(info->manufacturer, sizeof(info->manufacturer)); 92 EBCASC(info->type, sizeof(info->type)); 93 EBCASC(info->model, sizeof(info->model)); 94 EBCASC(info->sequence, sizeof(info->sequence)); 95 EBCASC(info->plant, sizeof(info->plant)); 96 EBCASC(info->model_capacity, sizeof(info->model_capacity)); 97 EBCASC(info->model_perm_cap, sizeof(info->model_perm_cap)); 98 EBCASC(info->model_temp_cap, sizeof(info->model_temp_cap)); 99 if (has_var_cap) 100 EBCASC(info->model_var_cap, sizeof(info->model_var_cap)); 101 seq_printf(m, "Manufacturer: %-16.16s\n", info->manufacturer); 102 seq_printf(m, "Type: %-4.4s\n", info->type); 103 if (info->lic) 104 seq_printf(m, "LIC Identifier: %016lx\n", info->lic); 105 /* 106 * Sigh: the model field has been renamed with System z9 107 * to model_capacity and a new model field has been added 108 * after the plant field. To avoid confusing older programs 109 * the "Model:" prints "model_capacity model" or just 110 * "model_capacity" if the model string is empty . 111 */ 112 seq_printf(m, "Model: %-16.16s", info->model_capacity); 113 if (info->model[0] != '\0') 114 seq_printf(m, " %-16.16s", info->model); 115 seq_putc(m, '\n'); 116 seq_printf(m, "Sequence Code: %-16.16s\n", info->sequence); 117 seq_printf(m, "Plant: %-4.4s\n", info->plant); 118 seq_printf(m, "Model Capacity: %-16.16s %08u\n", 119 info->model_capacity, info->model_cap_rating); 120 if (info->model_perm_cap_rating) 121 seq_printf(m, "Model Perm. Capacity: %-16.16s %08u\n", 122 info->model_perm_cap, 123 info->model_perm_cap_rating); 124 if (info->model_temp_cap_rating) 125 seq_printf(m, "Model Temp. Capacity: %-16.16s %08u\n", 126 info->model_temp_cap, 127 info->model_temp_cap_rating); 128 if (has_var_cap && info->model_var_cap_rating) 129 seq_printf(m, "Model Var. Capacity: %-16.16s %08u\n", 130 info->model_var_cap, 131 info->model_var_cap_rating); 132 if (info->ncr) 133 seq_printf(m, "Nominal Cap. Rating: %08u\n", info->ncr); 134 if (info->npr) 135 seq_printf(m, "Nominal Perm. Rating: %08u\n", info->npr); 136 if (info->ntr) 137 seq_printf(m, "Nominal Temp. Rating: %08u\n", info->ntr); 138 if (has_var_cap && info->nvr) 139 seq_printf(m, "Nominal Var. Rating: %08u\n", info->nvr); 140 if (info->cai) { 141 seq_printf(m, "Capacity Adj. Ind.: %d\n", info->cai); 142 seq_printf(m, "Capacity Ch. Reason: %d\n", info->ccr); 143 seq_printf(m, "Capacity Transient: %d\n", info->t); 144 } 145 if (info->p) { 146 for (i = 1; i <= ARRAY_SIZE(info->typepct); i++) { 147 seq_printf(m, "Type %d Percentage: %d\n", 148 i, info->typepct[i - 1]); 149 } 150 } 151 } 152 153 static void stsi_15_1_x(struct seq_file *m, struct sysinfo_15_1_x *info) 154 { 155 int i; 156 157 seq_putc(m, '\n'); 158 if (!cpu_has_topology()) 159 return; 160 if (stsi(info, 15, 1, topology_max_mnest)) 161 return; 162 seq_printf(m, "CPU Topology HW: "); 163 for (i = 0; i < TOPOLOGY_NR_MAG; i++) 164 seq_printf(m, " %d", info->mag[i]); 165 seq_putc(m, '\n'); 166 #ifdef CONFIG_SCHED_TOPOLOGY 167 store_topology(info); 168 seq_printf(m, "CPU Topology SW: "); 169 for (i = 0; i < TOPOLOGY_NR_MAG; i++) 170 seq_printf(m, " %d", info->mag[i]); 171 seq_putc(m, '\n'); 172 #endif 173 } 174 175 static void stsi_1_2_2(struct seq_file *m, struct sysinfo_1_2_2 *info) 176 { 177 struct sysinfo_1_2_2_extension *ext; 178 int i; 179 180 if (stsi(info, 1, 2, 2)) 181 return; 182 ext = (struct sysinfo_1_2_2_extension *) 183 ((unsigned long) info + info->acc_offset); 184 seq_printf(m, "CPUs Total: %d\n", info->cpus_total); 185 seq_printf(m, "CPUs Configured: %d\n", info->cpus_configured); 186 seq_printf(m, "CPUs Standby: %d\n", info->cpus_standby); 187 seq_printf(m, "CPUs Reserved: %d\n", info->cpus_reserved); 188 if (info->mt_installed) { 189 seq_printf(m, "CPUs G-MTID: %d\n", info->mt_gtid); 190 seq_printf(m, "CPUs S-MTID: %d\n", info->mt_stid); 191 } 192 /* 193 * Sigh 2. According to the specification the alternate 194 * capability field is a 32 bit floating point number 195 * if the higher order 8 bits are not zero. Printing 196 * a floating point number in the kernel is a no-no, 197 * always print the number as 32 bit unsigned integer. 198 * The user-space needs to know about the strange 199 * encoding of the alternate cpu capability. 200 */ 201 seq_printf(m, "Capability: %u", info->capability); 202 if (info->format == 1) 203 seq_printf(m, " %u", ext->alt_capability); 204 seq_putc(m, '\n'); 205 if (info->nominal_cap) 206 seq_printf(m, "Nominal Capability: %d\n", info->nominal_cap); 207 if (info->secondary_cap) 208 seq_printf(m, "Secondary Capability: %d\n", info->secondary_cap); 209 for (i = 2; i <= info->cpus_total; i++) { 210 seq_printf(m, "Adjustment %02d-way: %u", 211 i, info->adjustment[i-2]); 212 if (info->format == 1) 213 seq_printf(m, " %u", ext->alt_adjustment[i-2]); 214 seq_putc(m, '\n'); 215 } 216 } 217 218 static void stsi_2_2_2(struct seq_file *m, struct sysinfo_2_2_2 *info) 219 { 220 if (stsi(info, 2, 2, 2)) 221 return; 222 EBCASC(info->name, sizeof(info->name)); 223 seq_putc(m, '\n'); 224 seq_printf(m, "LPAR Number: %d\n", info->lpar_number); 225 seq_printf(m, "LPAR Characteristics: "); 226 if (info->characteristics & LPAR_CHAR_DEDICATED) 227 seq_printf(m, "Dedicated "); 228 if (info->characteristics & LPAR_CHAR_SHARED) 229 seq_printf(m, "Shared "); 230 if (info->characteristics & LPAR_CHAR_LIMITED) 231 seq_printf(m, "Limited "); 232 seq_putc(m, '\n'); 233 seq_printf(m, "LPAR Name: %-8.8s\n", info->name); 234 seq_printf(m, "LPAR Adjustment: %d\n", info->caf); 235 seq_printf(m, "LPAR CPUs Total: %d\n", info->cpus_total); 236 seq_printf(m, "LPAR CPUs Configured: %d\n", info->cpus_configured); 237 seq_printf(m, "LPAR CPUs Standby: %d\n", info->cpus_standby); 238 seq_printf(m, "LPAR CPUs Reserved: %d\n", info->cpus_reserved); 239 seq_printf(m, "LPAR CPUs Dedicated: %d\n", info->cpus_dedicated); 240 seq_printf(m, "LPAR CPUs Shared: %d\n", info->cpus_shared); 241 if (info->mt_installed) { 242 seq_printf(m, "LPAR CPUs G-MTID: %d\n", info->mt_gtid); 243 seq_printf(m, "LPAR CPUs S-MTID: %d\n", info->mt_stid); 244 seq_printf(m, "LPAR CPUs PS-MTID: %d\n", info->mt_psmtid); 245 } 246 if (convert_ext_name(info->vsne, info->ext_name, sizeof(info->ext_name))) { 247 seq_printf(m, "LPAR Extended Name: %-.256s\n", info->ext_name); 248 seq_printf(m, "LPAR UUID: %pUb\n", &info->uuid); 249 } 250 } 251 252 static void print_ext_name(struct seq_file *m, int lvl, 253 struct sysinfo_3_2_2 *info) 254 { 255 size_t len = sizeof(info->ext_names[lvl]); 256 257 if (!convert_ext_name(info->vm[lvl].evmne, info->ext_names[lvl], len)) 258 return; 259 seq_printf(m, "VM%02d Extended Name: %-.256s\n", lvl, 260 info->ext_names[lvl]); 261 } 262 263 static void print_uuid(struct seq_file *m, int i, struct sysinfo_3_2_2 *info) 264 { 265 if (uuid_is_null(&info->vm[i].uuid)) 266 return; 267 seq_printf(m, "VM%02d UUID: %pUb\n", i, &info->vm[i].uuid); 268 } 269 270 static void stsi_3_2_2(struct seq_file *m, struct sysinfo_3_2_2 *info) 271 { 272 int i; 273 274 if (stsi(info, 3, 2, 2)) 275 return; 276 for (i = 0; i < info->count; i++) { 277 EBCASC(info->vm[i].name, sizeof(info->vm[i].name)); 278 EBCASC(info->vm[i].cpi, sizeof(info->vm[i].cpi)); 279 seq_putc(m, '\n'); 280 seq_printf(m, "VM%02d Name: %-8.8s\n", i, info->vm[i].name); 281 seq_printf(m, "VM%02d Control Program: %-16.16s\n", i, info->vm[i].cpi); 282 seq_printf(m, "VM%02d Adjustment: %d\n", i, info->vm[i].caf); 283 seq_printf(m, "VM%02d CPUs Total: %d\n", i, info->vm[i].cpus_total); 284 seq_printf(m, "VM%02d CPUs Configured: %d\n", i, info->vm[i].cpus_configured); 285 seq_printf(m, "VM%02d CPUs Standby: %d\n", i, info->vm[i].cpus_standby); 286 seq_printf(m, "VM%02d CPUs Reserved: %d\n", i, info->vm[i].cpus_reserved); 287 print_ext_name(m, i, info); 288 print_uuid(m, i, info); 289 } 290 } 291 292 static int sysinfo_show(struct seq_file *m, void *v) 293 { 294 void *info = (void *)get_zeroed_page(GFP_KERNEL); 295 int level; 296 297 if (!info) 298 return 0; 299 level = stsi(NULL, 0, 0, 0); 300 if (level >= 1) 301 stsi_1_1_1(m, info); 302 if (level >= 1) 303 stsi_15_1_x(m, info); 304 if (level >= 1) 305 stsi_1_2_2(m, info); 306 if (level >= 2) 307 stsi_2_2_2(m, info); 308 if (level >= 3) 309 stsi_3_2_2(m, info); 310 free_page((unsigned long)info); 311 return 0; 312 } 313 314 static int __init sysinfo_create_proc(void) 315 { 316 proc_create_single("sysinfo", 0444, NULL, sysinfo_show); 317 return 0; 318 } 319 device_initcall(sysinfo_create_proc); 320 321 #endif /* CONFIG_PROC_FS */ 322 323 /* 324 * Service levels interface. 325 */ 326 327 static DECLARE_RWSEM(service_level_sem); 328 static LIST_HEAD(service_level_list); 329 330 int register_service_level(struct service_level *slr) 331 { 332 struct service_level *ptr; 333 334 down_write(&service_level_sem); 335 list_for_each_entry(ptr, &service_level_list, list) 336 if (ptr == slr) { 337 up_write(&service_level_sem); 338 return -EEXIST; 339 } 340 list_add_tail(&slr->list, &service_level_list); 341 up_write(&service_level_sem); 342 return 0; 343 } 344 EXPORT_SYMBOL(register_service_level); 345 346 int unregister_service_level(struct service_level *slr) 347 { 348 struct service_level *ptr, *next; 349 int rc = -ENOENT; 350 351 down_write(&service_level_sem); 352 list_for_each_entry_safe(ptr, next, &service_level_list, list) { 353 if (ptr != slr) 354 continue; 355 list_del(&ptr->list); 356 rc = 0; 357 break; 358 } 359 up_write(&service_level_sem); 360 return rc; 361 } 362 EXPORT_SYMBOL(unregister_service_level); 363 364 static void *service_level_start(struct seq_file *m, loff_t *pos) 365 { 366 down_read(&service_level_sem); 367 return seq_list_start(&service_level_list, *pos); 368 } 369 370 static void *service_level_next(struct seq_file *m, void *p, loff_t *pos) 371 { 372 return seq_list_next(p, &service_level_list, pos); 373 } 374 375 static void service_level_stop(struct seq_file *m, void *p) 376 { 377 up_read(&service_level_sem); 378 } 379 380 static int service_level_show(struct seq_file *m, void *p) 381 { 382 struct service_level *slr; 383 384 slr = list_entry(p, struct service_level, list); 385 slr->seq_print(m, slr); 386 return 0; 387 } 388 389 static const struct seq_operations service_level_seq_ops = { 390 .start = service_level_start, 391 .next = service_level_next, 392 .stop = service_level_stop, 393 .show = service_level_show 394 }; 395 396 static void service_level_vm_print(struct seq_file *m, 397 struct service_level *slr) 398 { 399 char *query_buffer, *str; 400 401 query_buffer = kmalloc(1024, GFP_KERNEL); 402 if (!query_buffer) 403 return; 404 cpcmd("QUERY CPLEVEL", query_buffer, 1024, NULL); 405 str = strchr(query_buffer, '\n'); 406 if (str) 407 *str = 0; 408 seq_printf(m, "VM: %s\n", query_buffer); 409 kfree(query_buffer); 410 } 411 412 static struct service_level service_level_vm = { 413 .seq_print = service_level_vm_print 414 }; 415 416 static __init int create_proc_service_level(void) 417 { 418 proc_create_seq("service_levels", 0, NULL, &service_level_seq_ops); 419 if (MACHINE_IS_VM) 420 register_service_level(&service_level_vm); 421 return 0; 422 } 423 subsys_initcall(create_proc_service_level); 424 425 /* 426 * CPU capability might have changed. Therefore recalculate loops_per_jiffy. 427 */ 428 void s390_adjust_jiffies(void) 429 { 430 DECLARE_KERNEL_FPU_ONSTACK16(fpu); 431 struct sysinfo_1_2_2 *info; 432 unsigned long capability; 433 434 info = (void *) get_zeroed_page(GFP_KERNEL); 435 if (!info) 436 return; 437 438 if (stsi(info, 1, 2, 2) == 0) { 439 /* 440 * Major sigh. The cpu capability encoding is "special". 441 * If the first 9 bits of info->capability are 0 then it 442 * is a 32 bit unsigned integer in the range 0 .. 2^23. 443 * If the first 9 bits are != 0 then it is a 32 bit float. 444 * In addition a lower value indicates a proportionally 445 * higher cpu capacity. Bogomips are the other way round. 446 * To get to a halfway suitable number we divide 1e7 447 * by the cpu capability number. Yes, that means a floating 448 * point division .. 449 */ 450 kernel_fpu_begin(&fpu, KERNEL_FPR); 451 fpu_sfpc(0); 452 if (info->capability & 0xff800000) 453 fpu_ldgr(2, info->capability); 454 else 455 fpu_cefbr(2, info->capability); 456 fpu_cefbr(0, 10000000); 457 fpu_debr(0, 2); 458 capability = fpu_cgebr(0, 5); 459 kernel_fpu_end(&fpu, KERNEL_FPR); 460 } else 461 /* 462 * Really old machine without stsi block for basic 463 * cpu information. Report 42.0 bogomips. 464 */ 465 capability = 42; 466 loops_per_jiffy = capability * (500000/HZ); 467 free_page((unsigned long) info); 468 } 469 470 /* 471 * calibrate the delay loop 472 */ 473 void calibrate_delay(void) 474 { 475 s390_adjust_jiffies(); 476 /* Print the good old Bogomips line .. */ 477 printk(KERN_DEBUG "Calibrating delay loop (skipped)... " 478 "%lu.%02lu BogoMIPS preset\n", loops_per_jiffy/(500000/HZ), 479 (loops_per_jiffy/(5000/HZ)) % 100); 480 } 481 482 #ifdef CONFIG_DEBUG_FS 483 484 #define STSI_FILE(fc, s1, s2) \ 485 static int stsi_open_##fc##_##s1##_##s2(struct inode *inode, struct file *file)\ 486 { \ 487 file->private_data = (void *) get_zeroed_page(GFP_KERNEL); \ 488 if (!file->private_data) \ 489 return -ENOMEM; \ 490 if (stsi(file->private_data, fc, s1, s2)) { \ 491 free_page((unsigned long)file->private_data); \ 492 file->private_data = NULL; \ 493 return -EACCES; \ 494 } \ 495 return nonseekable_open(inode, file); \ 496 } \ 497 \ 498 static const struct file_operations stsi_##fc##_##s1##_##s2##_fs_ops = { \ 499 .open = stsi_open_##fc##_##s1##_##s2, \ 500 .release = stsi_release, \ 501 .read = stsi_read, \ 502 }; 503 504 static int stsi_release(struct inode *inode, struct file *file) 505 { 506 free_page((unsigned long)file->private_data); 507 return 0; 508 } 509 510 static ssize_t stsi_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) 511 { 512 return simple_read_from_buffer(buf, size, ppos, file->private_data, PAGE_SIZE); 513 } 514 515 STSI_FILE( 1, 1, 1); 516 STSI_FILE( 1, 2, 1); 517 STSI_FILE( 1, 2, 2); 518 STSI_FILE( 2, 2, 1); 519 STSI_FILE( 2, 2, 2); 520 STSI_FILE( 3, 2, 2); 521 STSI_FILE(15, 1, 2); 522 STSI_FILE(15, 1, 3); 523 STSI_FILE(15, 1, 4); 524 STSI_FILE(15, 1, 5); 525 STSI_FILE(15, 1, 6); 526 527 struct stsi_file { 528 const struct file_operations *fops; 529 char *name; 530 }; 531 532 static struct stsi_file stsi_file[] __initdata = { 533 {.fops = &stsi_1_1_1_fs_ops, .name = "1_1_1"}, 534 {.fops = &stsi_1_2_1_fs_ops, .name = "1_2_1"}, 535 {.fops = &stsi_1_2_2_fs_ops, .name = "1_2_2"}, 536 {.fops = &stsi_2_2_1_fs_ops, .name = "2_2_1"}, 537 {.fops = &stsi_2_2_2_fs_ops, .name = "2_2_2"}, 538 {.fops = &stsi_3_2_2_fs_ops, .name = "3_2_2"}, 539 {.fops = &stsi_15_1_2_fs_ops, .name = "15_1_2"}, 540 {.fops = &stsi_15_1_3_fs_ops, .name = "15_1_3"}, 541 {.fops = &stsi_15_1_4_fs_ops, .name = "15_1_4"}, 542 {.fops = &stsi_15_1_5_fs_ops, .name = "15_1_5"}, 543 {.fops = &stsi_15_1_6_fs_ops, .name = "15_1_6"}, 544 }; 545 546 static u8 stsi_0_0_0; 547 548 static __init int stsi_init_debugfs(void) 549 { 550 struct dentry *stsi_root; 551 struct stsi_file *sf; 552 int lvl, i; 553 554 stsi_root = debugfs_create_dir("stsi", arch_debugfs_dir); 555 lvl = stsi(NULL, 0, 0, 0); 556 if (lvl > 0) 557 stsi_0_0_0 = lvl; 558 debugfs_create_u8("0_0_0", 0400, stsi_root, &stsi_0_0_0); 559 for (i = 0; i < ARRAY_SIZE(stsi_file); i++) { 560 sf = &stsi_file[i]; 561 debugfs_create_file(sf->name, 0400, stsi_root, NULL, sf->fops); 562 } 563 if (IS_ENABLED(CONFIG_SCHED_TOPOLOGY) && cpu_has_topology()) { 564 char link_to[10]; 565 566 sprintf(link_to, "15_1_%d", topology_mnest_limit()); 567 debugfs_create_symlink("topology", stsi_root, link_to); 568 } 569 return 0; 570 } 571 device_initcall(stsi_init_debugfs); 572 573 #endif /* CONFIG_DEBUG_FS */ 574