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