xref: /linux/arch/s390/kernel/diag.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Implementation of s390 diagnose codes
4  *
5  * Copyright IBM Corp. 2007
6  * Author(s): Michael Holzheu <holzheu@de.ibm.com>
7  */
8 
9 #include <linux/export.h>
10 #include <linux/init.h>
11 #include <linux/cpu.h>
12 #include <linux/seq_file.h>
13 #include <linux/debugfs.h>
14 #include <linux/vmalloc.h>
15 #include <asm/asm-extable.h>
16 #include <asm/diag.h>
17 #include <asm/trace/diag.h>
18 #include <asm/sections.h>
19 #include "entry.h"
20 
21 struct diag_stat {
22 	unsigned int counter[NR_DIAG_STAT];
23 };
24 
25 static DEFINE_PER_CPU(struct diag_stat, diag_stat);
26 
27 struct diag_desc {
28 	int code;
29 	char *name;
30 };
31 
32 static const struct diag_desc diag_map[NR_DIAG_STAT] = {
33 	[DIAG_STAT_X008] = { .code = 0x008, .name = "Console Function" },
34 	[DIAG_STAT_X00C] = { .code = 0x00c, .name = "Pseudo Timer" },
35 	[DIAG_STAT_X010] = { .code = 0x010, .name = "Release Pages" },
36 	[DIAG_STAT_X014] = { .code = 0x014, .name = "Spool File Services" },
37 	[DIAG_STAT_X044] = { .code = 0x044, .name = "Voluntary Timeslice End" },
38 	[DIAG_STAT_X064] = { .code = 0x064, .name = "NSS Manipulation" },
39 	[DIAG_STAT_X08C] = { .code = 0x08c, .name = "Access 3270 Display Device Information" },
40 	[DIAG_STAT_X09C] = { .code = 0x09c, .name = "Relinquish Timeslice" },
41 	[DIAG_STAT_X0DC] = { .code = 0x0dc, .name = "Appldata Control" },
42 	[DIAG_STAT_X204] = { .code = 0x204, .name = "Logical-CPU Utilization" },
43 	[DIAG_STAT_X210] = { .code = 0x210, .name = "Device Information" },
44 	[DIAG_STAT_X224] = { .code = 0x224, .name = "EBCDIC-Name Table" },
45 	[DIAG_STAT_X250] = { .code = 0x250, .name = "Block I/O" },
46 	[DIAG_STAT_X258] = { .code = 0x258, .name = "Page-Reference Services" },
47 	[DIAG_STAT_X26C] = { .code = 0x26c, .name = "Certain System Information" },
48 	[DIAG_STAT_X288] = { .code = 0x288, .name = "Time Bomb" },
49 	[DIAG_STAT_X2C4] = { .code = 0x2c4, .name = "FTP Services" },
50 	[DIAG_STAT_X2FC] = { .code = 0x2fc, .name = "Guest Performance Data" },
51 	[DIAG_STAT_X304] = { .code = 0x304, .name = "Partition-Resource Service" },
52 	[DIAG_STAT_X308] = { .code = 0x308, .name = "List-Directed IPL" },
53 	[DIAG_STAT_X318] = { .code = 0x318, .name = "CP Name and Version Codes" },
54 	[DIAG_STAT_X320] = { .code = 0x320, .name = "Certificate Store" },
55 	[DIAG_STAT_X49C] = { .code = 0x49c, .name = "Warning-Track Interruption" },
56 	[DIAG_STAT_X500] = { .code = 0x500, .name = "Virtio Service" },
57 };
58 
59 struct diag_ops __amode31_ref diag_amode31_ops = {
60 	.diag210 = _diag210_amode31,
61 	.diag26c = _diag26c_amode31,
62 	.diag14 = _diag14_amode31,
63 	.diag0c = _diag0c_amode31,
64 	.diag8c = _diag8c_amode31,
65 	.diag308_reset = _diag308_reset_amode31
66 };
67 
68 static struct diag210 _diag210_tmp_amode31 __section(".amode31.data");
69 struct diag210 __amode31_ref *__diag210_tmp_amode31 = &_diag210_tmp_amode31;
70 
71 static struct diag8c _diag8c_tmp_amode31 __section(".amode31.data");
72 static struct diag8c __amode31_ref *__diag8c_tmp_amode31 = &_diag8c_tmp_amode31;
73 
74 static int show_diag_stat(struct seq_file *m, void *v)
75 {
76 	struct diag_stat *stat;
77 	unsigned long n = (unsigned long) v - 1;
78 	int cpu, prec, tmp;
79 
80 	cpus_read_lock();
81 	if (n == 0) {
82 		seq_puts(m, "         ");
83 
84 		for_each_online_cpu(cpu) {
85 			prec = 10;
86 			for (tmp = 10; cpu >= tmp; tmp *= 10)
87 				prec--;
88 			seq_printf(m, "%*s%d", prec, "CPU", cpu);
89 		}
90 		seq_putc(m, '\n');
91 	} else if (n <= NR_DIAG_STAT) {
92 		seq_printf(m, "diag %03x:", diag_map[n-1].code);
93 		for_each_online_cpu(cpu) {
94 			stat = &per_cpu(diag_stat, cpu);
95 			seq_printf(m, " %10u", stat->counter[n-1]);
96 		}
97 		seq_printf(m, "    %s\n", diag_map[n-1].name);
98 	}
99 	cpus_read_unlock();
100 	return 0;
101 }
102 
103 static void *show_diag_stat_start(struct seq_file *m, loff_t *pos)
104 {
105 	return *pos <= NR_DIAG_STAT ? (void *)((unsigned long) *pos + 1) : NULL;
106 }
107 
108 static void *show_diag_stat_next(struct seq_file *m, void *v, loff_t *pos)
109 {
110 	++*pos;
111 	return show_diag_stat_start(m, pos);
112 }
113 
114 static void show_diag_stat_stop(struct seq_file *m, void *v)
115 {
116 }
117 
118 static const struct seq_operations show_diag_stat_sops = {
119 	.start	= show_diag_stat_start,
120 	.next	= show_diag_stat_next,
121 	.stop	= show_diag_stat_stop,
122 	.show	= show_diag_stat,
123 };
124 
125 DEFINE_SEQ_ATTRIBUTE(show_diag_stat);
126 
127 static int __init show_diag_stat_init(void)
128 {
129 	debugfs_create_file("diag_stat", 0400, NULL, NULL,
130 			    &show_diag_stat_fops);
131 	return 0;
132 }
133 
134 device_initcall(show_diag_stat_init);
135 
136 void diag_stat_inc(enum diag_stat_enum nr)
137 {
138 	this_cpu_inc(diag_stat.counter[nr]);
139 	trace_s390_diagnose(diag_map[nr].code);
140 }
141 EXPORT_SYMBOL(diag_stat_inc);
142 
143 void notrace diag_stat_inc_norecursion(enum diag_stat_enum nr)
144 {
145 	this_cpu_inc(diag_stat.counter[nr]);
146 	trace_s390_diagnose_norecursion(diag_map[nr].code);
147 }
148 EXPORT_SYMBOL(diag_stat_inc_norecursion);
149 
150 /*
151  * Diagnose 0c: Pseudo Timer
152  */
153 void diag0c(struct hypfs_diag0c_entry *data)
154 {
155 	diag_stat_inc(DIAG_STAT_X00C);
156 	diag_amode31_ops.diag0c(virt_to_phys(data));
157 }
158 
159 /*
160  * Diagnose 14: Input spool file manipulation
161  *
162  * The subcode parameter determines the type of the first parameter rx.
163  * Currently used are the following 3 subcommands:
164  * 0x0:   Read the Next Spool File Buffer (Data Record)
165  * 0x28:  Position a Spool File to the Designated Record
166  * 0xfff: Retrieve Next File Descriptor
167  *
168  * For subcommands 0x0 and 0xfff, the value of the first parameter is
169  * a virtual address of a memory buffer and needs virtual to physical
170  * address translation. For other subcommands the rx parameter is not
171  * a virtual address.
172  */
173 int diag14(unsigned long rx, unsigned long ry1, unsigned long subcode)
174 {
175 	diag_stat_inc(DIAG_STAT_X014);
176 	switch (subcode) {
177 	case 0x0:
178 	case 0xfff:
179 		rx = virt_to_phys((void *)rx);
180 		break;
181 	default:
182 		/* Do nothing */
183 		break;
184 	}
185 	return diag_amode31_ops.diag14(rx, ry1, subcode);
186 }
187 EXPORT_SYMBOL(diag14);
188 
189 #define DIAG204_BUSY_RC 8
190 
191 static inline int __diag204(unsigned long *subcode, unsigned long size, void *addr)
192 {
193 	union register_pair rp = { .even = *subcode, .odd = size };
194 
195 	asm volatile(
196 		"	diag	%[addr],%[rp],0x204\n"
197 		"0:	nopr	%%r7\n"
198 		EX_TABLE(0b,0b)
199 		: [rp] "+&d" (rp.pair) : [addr] "d" (addr) : "memory");
200 	*subcode = rp.even;
201 	return rp.odd;
202 }
203 
204 /**
205  * diag204() - Issue diagnose 204 call.
206  * @subcode: Subcode of diagnose 204 to be executed.
207  * @size: Size of area in pages which @area points to, if given.
208  * @addr: Vmalloc'ed memory area where the result is written to.
209  *
210  * Execute diagnose 204 with the given subcode and write the result to the
211  * memory area specified with @addr. For subcodes which do not write a
212  * result to memory both @size and @addr must be zero. If @addr is
213  * specified it must be page aligned and must have been allocated with
214  * vmalloc(). Conversion to real / physical addresses will be handled by
215  * this function if required.
216  */
217 int diag204(unsigned long subcode, unsigned long size, void *addr)
218 {
219 	if (addr) {
220 		if (WARN_ON_ONCE(!is_vmalloc_addr(addr)))
221 			return -EINVAL;
222 		if (WARN_ON_ONCE(!IS_ALIGNED((unsigned long)addr, PAGE_SIZE)))
223 			return -EINVAL;
224 	}
225 	if ((subcode & DIAG204_SUBCODE_MASK) == DIAG204_SUBC_STIB4)
226 		addr = (void *)pfn_to_phys(vmalloc_to_pfn(addr));
227 	diag_stat_inc(DIAG_STAT_X204);
228 	size = __diag204(&subcode, size, addr);
229 	if (subcode == DIAG204_BUSY_RC)
230 		return -EBUSY;
231 	else if (subcode)
232 		return -EOPNOTSUPP;
233 	return size;
234 }
235 EXPORT_SYMBOL(diag204);
236 
237 /*
238  * Diagnose 210: Get information about a virtual device
239  */
240 int diag210(struct diag210 *addr)
241 {
242 	static DEFINE_SPINLOCK(diag210_lock);
243 	unsigned long flags;
244 	int ccode;
245 
246 	spin_lock_irqsave(&diag210_lock, flags);
247 	*__diag210_tmp_amode31 = *addr;
248 
249 	diag_stat_inc(DIAG_STAT_X210);
250 	ccode = diag_amode31_ops.diag210(__diag210_tmp_amode31);
251 
252 	*addr = *__diag210_tmp_amode31;
253 	spin_unlock_irqrestore(&diag210_lock, flags);
254 
255 	return ccode;
256 }
257 EXPORT_SYMBOL(diag210);
258 
259 /*
260  * Diagnose 8C: Access 3270 Display Device Information
261  */
262 int diag8c(struct diag8c *addr, struct ccw_dev_id *devno)
263 {
264 	static DEFINE_SPINLOCK(diag8c_lock);
265 	unsigned long flags;
266 	int ccode;
267 
268 	spin_lock_irqsave(&diag8c_lock, flags);
269 
270 	diag_stat_inc(DIAG_STAT_X08C);
271 	ccode = diag_amode31_ops.diag8c(__diag8c_tmp_amode31, devno, sizeof(*addr));
272 
273 	*addr = *__diag8c_tmp_amode31;
274 	spin_unlock_irqrestore(&diag8c_lock, flags);
275 
276 	return ccode;
277 }
278 EXPORT_SYMBOL(diag8c);
279 
280 int diag224(void *ptr)
281 {
282 	unsigned long addr = __pa(ptr);
283 	int rc = -EOPNOTSUPP;
284 
285 	diag_stat_inc(DIAG_STAT_X224);
286 	asm volatile("\n"
287 		"	diag	%[type],%[addr],0x224\n"
288 		"0:	lhi	%[rc],0\n"
289 		"1:\n"
290 		EX_TABLE(0b,1b)
291 		: [rc] "+d" (rc)
292 		, "=m" (*(struct { char buf[PAGE_SIZE]; } *)ptr)
293 		: [type] "d" (0), [addr] "d" (addr));
294 	return rc;
295 }
296 EXPORT_SYMBOL(diag224);
297 
298 /*
299  * Diagnose 26C: Access Certain System Information
300  */
301 int diag26c(void *req, void *resp, enum diag26c_sc subcode)
302 {
303 	diag_stat_inc(DIAG_STAT_X26C);
304 	return diag_amode31_ops.diag26c(virt_to_phys(req), virt_to_phys(resp), subcode);
305 }
306 EXPORT_SYMBOL(diag26c);
307 
308 int diag49c(unsigned long subcode)
309 {
310 	int rc;
311 
312 	diag_stat_inc(DIAG_STAT_X49C);
313 	asm volatile(
314 		"	diag	%[subcode],0,0x49c\n"
315 		"	ipm	%[rc]\n"
316 		"	srl	%[rc],28\n"
317 		: [rc] "=d" (rc)
318 		: [subcode] "d" (subcode)
319 		: "cc");
320 	return rc;
321 }
322 EXPORT_SYMBOL(diag49c);
323