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