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