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