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