1 /* 2 * Copyright IBM Corp. 2001, 2009 3 * Author(s): Ulrich Weigand <Ulrich.Weigand@de.ibm.com>, 4 * Martin Schwidefsky <schwidefsky@de.ibm.com>, 5 */ 6 7 #include <linux/kernel.h> 8 #include <linux/mm.h> 9 #include <linux/proc_fs.h> 10 #include <linux/seq_file.h> 11 #include <linux/init.h> 12 #include <linux/delay.h> 13 #include <linux/module.h> 14 #include <linux/slab.h> 15 #include <asm/ebcdic.h> 16 #include <asm/sysinfo.h> 17 #include <asm/cpcmd.h> 18 #include <asm/topology.h> 19 #include <asm/fpu/api.h> 20 21 int topology_max_mnest; 22 23 /* 24 * stsi - store system information 25 * 26 * Returns the current configuration level if function code 0 was specified. 27 * Otherwise returns 0 on success or a negative value on error. 28 */ 29 int stsi(void *sysinfo, int fc, int sel1, int sel2) 30 { 31 register int r0 asm("0") = (fc << 28) | sel1; 32 register int r1 asm("1") = sel2; 33 int rc = 0; 34 35 asm volatile( 36 " stsi 0(%3)\n" 37 "0: jz 2f\n" 38 "1: lhi %1,%4\n" 39 "2:\n" 40 EX_TABLE(0b, 1b) 41 : "+d" (r0), "+d" (rc) 42 : "d" (r1), "a" (sysinfo), "K" (-EOPNOTSUPP) 43 : "cc", "memory"); 44 if (rc) 45 return rc; 46 return fc ? 0 : ((unsigned int) r0) >> 28; 47 } 48 EXPORT_SYMBOL(stsi); 49 50 static void stsi_1_1_1(struct seq_file *m, struct sysinfo_1_1_1 *info) 51 { 52 int i; 53 54 if (stsi(info, 1, 1, 1)) 55 return; 56 EBCASC(info->manufacturer, sizeof(info->manufacturer)); 57 EBCASC(info->type, sizeof(info->type)); 58 EBCASC(info->model, sizeof(info->model)); 59 EBCASC(info->sequence, sizeof(info->sequence)); 60 EBCASC(info->plant, sizeof(info->plant)); 61 EBCASC(info->model_capacity, sizeof(info->model_capacity)); 62 EBCASC(info->model_perm_cap, sizeof(info->model_perm_cap)); 63 EBCASC(info->model_temp_cap, sizeof(info->model_temp_cap)); 64 seq_printf(m, "Manufacturer: %-16.16s\n", info->manufacturer); 65 seq_printf(m, "Type: %-4.4s\n", info->type); 66 /* 67 * Sigh: the model field has been renamed with System z9 68 * to model_capacity and a new model field has been added 69 * after the plant field. To avoid confusing older programs 70 * the "Model:" prints "model_capacity model" or just 71 * "model_capacity" if the model string is empty . 72 */ 73 seq_printf(m, "Model: %-16.16s", info->model_capacity); 74 if (info->model[0] != '\0') 75 seq_printf(m, " %-16.16s", info->model); 76 seq_putc(m, '\n'); 77 seq_printf(m, "Sequence Code: %-16.16s\n", info->sequence); 78 seq_printf(m, "Plant: %-4.4s\n", info->plant); 79 seq_printf(m, "Model Capacity: %-16.16s %08u\n", 80 info->model_capacity, info->model_cap_rating); 81 if (info->model_perm_cap_rating) 82 seq_printf(m, "Model Perm. Capacity: %-16.16s %08u\n", 83 info->model_perm_cap, 84 info->model_perm_cap_rating); 85 if (info->model_temp_cap_rating) 86 seq_printf(m, "Model Temp. Capacity: %-16.16s %08u\n", 87 info->model_temp_cap, 88 info->model_temp_cap_rating); 89 if (info->ncr) 90 seq_printf(m, "Nominal Cap. Rating: %08u\n", info->ncr); 91 if (info->npr) 92 seq_printf(m, "Nominal Perm. Rating: %08u\n", info->npr); 93 if (info->ntr) 94 seq_printf(m, "Nominal Temp. Rating: %08u\n", info->ntr); 95 if (info->cai) { 96 seq_printf(m, "Capacity Adj. Ind.: %d\n", info->cai); 97 seq_printf(m, "Capacity Ch. Reason: %d\n", info->ccr); 98 seq_printf(m, "Capacity Transient: %d\n", info->t); 99 } 100 if (info->p) { 101 for (i = 1; i <= ARRAY_SIZE(info->typepct); i++) { 102 seq_printf(m, "Type %d Percentage: %d\n", 103 i, info->typepct[i - 1]); 104 } 105 } 106 } 107 108 static void stsi_15_1_x(struct seq_file *m, struct sysinfo_15_1_x *info) 109 { 110 int i; 111 112 seq_putc(m, '\n'); 113 if (!MACHINE_HAS_TOPOLOGY) 114 return; 115 if (stsi(info, 15, 1, topology_max_mnest)) 116 return; 117 seq_printf(m, "CPU Topology HW: "); 118 for (i = 0; i < TOPOLOGY_NR_MAG; i++) 119 seq_printf(m, " %d", info->mag[i]); 120 seq_putc(m, '\n'); 121 #ifdef CONFIG_SCHED_TOPOLOGY 122 store_topology(info); 123 seq_printf(m, "CPU Topology SW: "); 124 for (i = 0; i < TOPOLOGY_NR_MAG; i++) 125 seq_printf(m, " %d", info->mag[i]); 126 seq_putc(m, '\n'); 127 #endif 128 } 129 130 static void stsi_1_2_2(struct seq_file *m, struct sysinfo_1_2_2 *info) 131 { 132 struct sysinfo_1_2_2_extension *ext; 133 int i; 134 135 if (stsi(info, 1, 2, 2)) 136 return; 137 ext = (struct sysinfo_1_2_2_extension *) 138 ((unsigned long) info + info->acc_offset); 139 seq_printf(m, "CPUs Total: %d\n", info->cpus_total); 140 seq_printf(m, "CPUs Configured: %d\n", info->cpus_configured); 141 seq_printf(m, "CPUs Standby: %d\n", info->cpus_standby); 142 seq_printf(m, "CPUs Reserved: %d\n", info->cpus_reserved); 143 if (info->mt_installed) { 144 seq_printf(m, "CPUs G-MTID: %d\n", info->mt_gtid); 145 seq_printf(m, "CPUs S-MTID: %d\n", info->mt_stid); 146 } 147 /* 148 * Sigh 2. According to the specification the alternate 149 * capability field is a 32 bit floating point number 150 * if the higher order 8 bits are not zero. Printing 151 * a floating point number in the kernel is a no-no, 152 * always print the number as 32 bit unsigned integer. 153 * The user-space needs to know about the strange 154 * encoding of the alternate cpu capability. 155 */ 156 seq_printf(m, "Capability: %u", info->capability); 157 if (info->format == 1) 158 seq_printf(m, " %u", ext->alt_capability); 159 seq_putc(m, '\n'); 160 if (info->nominal_cap) 161 seq_printf(m, "Nominal Capability: %d\n", info->nominal_cap); 162 if (info->secondary_cap) 163 seq_printf(m, "Secondary Capability: %d\n", info->secondary_cap); 164 for (i = 2; i <= info->cpus_total; i++) { 165 seq_printf(m, "Adjustment %02d-way: %u", 166 i, info->adjustment[i-2]); 167 if (info->format == 1) 168 seq_printf(m, " %u", ext->alt_adjustment[i-2]); 169 seq_putc(m, '\n'); 170 } 171 } 172 173 static void stsi_2_2_2(struct seq_file *m, struct sysinfo_2_2_2 *info) 174 { 175 if (stsi(info, 2, 2, 2)) 176 return; 177 EBCASC(info->name, sizeof(info->name)); 178 seq_putc(m, '\n'); 179 seq_printf(m, "LPAR Number: %d\n", info->lpar_number); 180 seq_printf(m, "LPAR Characteristics: "); 181 if (info->characteristics & LPAR_CHAR_DEDICATED) 182 seq_printf(m, "Dedicated "); 183 if (info->characteristics & LPAR_CHAR_SHARED) 184 seq_printf(m, "Shared "); 185 if (info->characteristics & LPAR_CHAR_LIMITED) 186 seq_printf(m, "Limited "); 187 seq_putc(m, '\n'); 188 seq_printf(m, "LPAR Name: %-8.8s\n", info->name); 189 seq_printf(m, "LPAR Adjustment: %d\n", info->caf); 190 seq_printf(m, "LPAR CPUs Total: %d\n", info->cpus_total); 191 seq_printf(m, "LPAR CPUs Configured: %d\n", info->cpus_configured); 192 seq_printf(m, "LPAR CPUs Standby: %d\n", info->cpus_standby); 193 seq_printf(m, "LPAR CPUs Reserved: %d\n", info->cpus_reserved); 194 seq_printf(m, "LPAR CPUs Dedicated: %d\n", info->cpus_dedicated); 195 seq_printf(m, "LPAR CPUs Shared: %d\n", info->cpus_shared); 196 if (info->mt_installed) { 197 seq_printf(m, "LPAR CPUs G-MTID: %d\n", info->mt_gtid); 198 seq_printf(m, "LPAR CPUs S-MTID: %d\n", info->mt_stid); 199 seq_printf(m, "LPAR CPUs PS-MTID: %d\n", info->mt_psmtid); 200 } 201 } 202 203 static void print_ext_name(struct seq_file *m, int lvl, 204 struct sysinfo_3_2_2 *info) 205 { 206 if (info->vm[lvl].ext_name_encoding == 0) 207 return; 208 if (info->ext_names[lvl][0] == 0) 209 return; 210 switch (info->vm[lvl].ext_name_encoding) { 211 case 1: /* EBCDIC */ 212 EBCASC(info->ext_names[lvl], sizeof(info->ext_names[lvl])); 213 break; 214 case 2: /* UTF-8 */ 215 break; 216 default: 217 return; 218 } 219 seq_printf(m, "VM%02d Extended Name: %-.256s\n", lvl, 220 info->ext_names[lvl]); 221 } 222 223 static void print_uuid(struct seq_file *m, int i, struct sysinfo_3_2_2 *info) 224 { 225 if (!memcmp(&info->vm[i].uuid, &NULL_UUID_BE, sizeof(uuid_be))) 226 return; 227 seq_printf(m, "VM%02d UUID: %pUb\n", i, &info->vm[i].uuid); 228 } 229 230 static void stsi_3_2_2(struct seq_file *m, struct sysinfo_3_2_2 *info) 231 { 232 int i; 233 234 if (stsi(info, 3, 2, 2)) 235 return; 236 for (i = 0; i < info->count; i++) { 237 EBCASC(info->vm[i].name, sizeof(info->vm[i].name)); 238 EBCASC(info->vm[i].cpi, sizeof(info->vm[i].cpi)); 239 seq_putc(m, '\n'); 240 seq_printf(m, "VM%02d Name: %-8.8s\n", i, info->vm[i].name); 241 seq_printf(m, "VM%02d Control Program: %-16.16s\n", i, info->vm[i].cpi); 242 seq_printf(m, "VM%02d Adjustment: %d\n", i, info->vm[i].caf); 243 seq_printf(m, "VM%02d CPUs Total: %d\n", i, info->vm[i].cpus_total); 244 seq_printf(m, "VM%02d CPUs Configured: %d\n", i, info->vm[i].cpus_configured); 245 seq_printf(m, "VM%02d CPUs Standby: %d\n", i, info->vm[i].cpus_standby); 246 seq_printf(m, "VM%02d CPUs Reserved: %d\n", i, info->vm[i].cpus_reserved); 247 print_ext_name(m, i, info); 248 print_uuid(m, i, info); 249 } 250 } 251 252 static int sysinfo_show(struct seq_file *m, void *v) 253 { 254 void *info = (void *)get_zeroed_page(GFP_KERNEL); 255 int level; 256 257 if (!info) 258 return 0; 259 level = stsi(NULL, 0, 0, 0); 260 if (level >= 1) 261 stsi_1_1_1(m, info); 262 if (level >= 1) 263 stsi_15_1_x(m, info); 264 if (level >= 1) 265 stsi_1_2_2(m, info); 266 if (level >= 2) 267 stsi_2_2_2(m, info); 268 if (level >= 3) 269 stsi_3_2_2(m, info); 270 free_page((unsigned long)info); 271 return 0; 272 } 273 274 static int sysinfo_open(struct inode *inode, struct file *file) 275 { 276 return single_open(file, sysinfo_show, NULL); 277 } 278 279 static const struct file_operations sysinfo_fops = { 280 .open = sysinfo_open, 281 .read = seq_read, 282 .llseek = seq_lseek, 283 .release = single_release, 284 }; 285 286 static int __init sysinfo_create_proc(void) 287 { 288 proc_create("sysinfo", 0444, NULL, &sysinfo_fops); 289 return 0; 290 } 291 device_initcall(sysinfo_create_proc); 292 293 /* 294 * Service levels interface. 295 */ 296 297 static DECLARE_RWSEM(service_level_sem); 298 static LIST_HEAD(service_level_list); 299 300 int register_service_level(struct service_level *slr) 301 { 302 struct service_level *ptr; 303 304 down_write(&service_level_sem); 305 list_for_each_entry(ptr, &service_level_list, list) 306 if (ptr == slr) { 307 up_write(&service_level_sem); 308 return -EEXIST; 309 } 310 list_add_tail(&slr->list, &service_level_list); 311 up_write(&service_level_sem); 312 return 0; 313 } 314 EXPORT_SYMBOL(register_service_level); 315 316 int unregister_service_level(struct service_level *slr) 317 { 318 struct service_level *ptr, *next; 319 int rc = -ENOENT; 320 321 down_write(&service_level_sem); 322 list_for_each_entry_safe(ptr, next, &service_level_list, list) { 323 if (ptr != slr) 324 continue; 325 list_del(&ptr->list); 326 rc = 0; 327 break; 328 } 329 up_write(&service_level_sem); 330 return rc; 331 } 332 EXPORT_SYMBOL(unregister_service_level); 333 334 static void *service_level_start(struct seq_file *m, loff_t *pos) 335 { 336 down_read(&service_level_sem); 337 return seq_list_start(&service_level_list, *pos); 338 } 339 340 static void *service_level_next(struct seq_file *m, void *p, loff_t *pos) 341 { 342 return seq_list_next(p, &service_level_list, pos); 343 } 344 345 static void service_level_stop(struct seq_file *m, void *p) 346 { 347 up_read(&service_level_sem); 348 } 349 350 static int service_level_show(struct seq_file *m, void *p) 351 { 352 struct service_level *slr; 353 354 slr = list_entry(p, struct service_level, list); 355 slr->seq_print(m, slr); 356 return 0; 357 } 358 359 static const struct seq_operations service_level_seq_ops = { 360 .start = service_level_start, 361 .next = service_level_next, 362 .stop = service_level_stop, 363 .show = service_level_show 364 }; 365 366 static int service_level_open(struct inode *inode, struct file *file) 367 { 368 return seq_open(file, &service_level_seq_ops); 369 } 370 371 static const struct file_operations service_level_ops = { 372 .open = service_level_open, 373 .read = seq_read, 374 .llseek = seq_lseek, 375 .release = seq_release 376 }; 377 378 static void service_level_vm_print(struct seq_file *m, 379 struct service_level *slr) 380 { 381 char *query_buffer, *str; 382 383 query_buffer = kmalloc(1024, GFP_KERNEL | GFP_DMA); 384 if (!query_buffer) 385 return; 386 cpcmd("QUERY CPLEVEL", query_buffer, 1024, NULL); 387 str = strchr(query_buffer, '\n'); 388 if (str) 389 *str = 0; 390 seq_printf(m, "VM: %s\n", query_buffer); 391 kfree(query_buffer); 392 } 393 394 static struct service_level service_level_vm = { 395 .seq_print = service_level_vm_print 396 }; 397 398 static __init int create_proc_service_level(void) 399 { 400 proc_create("service_levels", 0, NULL, &service_level_ops); 401 if (MACHINE_IS_VM) 402 register_service_level(&service_level_vm); 403 return 0; 404 } 405 subsys_initcall(create_proc_service_level); 406 407 /* 408 * CPU capability might have changed. Therefore recalculate loops_per_jiffy. 409 */ 410 void s390_adjust_jiffies(void) 411 { 412 struct sysinfo_1_2_2 *info; 413 unsigned long capability; 414 struct kernel_fpu fpu; 415 416 info = (void *) get_zeroed_page(GFP_KERNEL); 417 if (!info) 418 return; 419 420 if (stsi(info, 1, 2, 2) == 0) { 421 /* 422 * Major sigh. The cpu capability encoding is "special". 423 * If the first 9 bits of info->capability are 0 then it 424 * is a 32 bit unsigned integer in the range 0 .. 2^23. 425 * If the first 9 bits are != 0 then it is a 32 bit float. 426 * In addition a lower value indicates a proportionally 427 * higher cpu capacity. Bogomips are the other way round. 428 * To get to a halfway suitable number we divide 1e7 429 * by the cpu capability number. Yes, that means a floating 430 * point division .. 431 */ 432 kernel_fpu_begin(&fpu, KERNEL_FPR); 433 asm volatile( 434 " sfpc %3\n" 435 " l %0,%1\n" 436 " tmlh %0,0xff80\n" 437 " jnz 0f\n" 438 " cefbr %%f2,%0\n" 439 " j 1f\n" 440 "0: le %%f2,%1\n" 441 "1: cefbr %%f0,%2\n" 442 " debr %%f0,%%f2\n" 443 " cgebr %0,5,%%f0\n" 444 : "=&d" (capability) 445 : "Q" (info->capability), "d" (10000000), "d" (0) 446 : "cc" 447 ); 448 kernel_fpu_end(&fpu); 449 } else 450 /* 451 * Really old machine without stsi block for basic 452 * cpu information. Report 42.0 bogomips. 453 */ 454 capability = 42; 455 loops_per_jiffy = capability * (500000/HZ); 456 free_page((unsigned long) info); 457 } 458 459 /* 460 * calibrate the delay loop 461 */ 462 void calibrate_delay(void) 463 { 464 s390_adjust_jiffies(); 465 /* Print the good old Bogomips line .. */ 466 printk(KERN_DEBUG "Calibrating delay loop (skipped)... " 467 "%lu.%02lu BogoMIPS preset\n", loops_per_jiffy/(500000/HZ), 468 (loops_per_jiffy/(5000/HZ)) % 100); 469 } 470