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