xref: /openbmc/linux/arch/s390/kernel/diag.c (revision b35565bb)
1 /*
2  * Implementation of s390 diagnose codes
3  *
4  * Copyright IBM Corp. 2007
5  * Author(s): Michael Holzheu <holzheu@de.ibm.com>
6  */
7 
8 #include <linux/export.h>
9 #include <linux/init.h>
10 #include <linux/cpu.h>
11 #include <linux/seq_file.h>
12 #include <linux/debugfs.h>
13 #include <asm/diag.h>
14 #include <asm/trace/diag.h>
15 
16 struct diag_stat {
17 	unsigned int counter[NR_DIAG_STAT];
18 };
19 
20 static DEFINE_PER_CPU(struct diag_stat, diag_stat);
21 
22 struct diag_desc {
23 	int code;
24 	char *name;
25 };
26 
27 static const struct diag_desc diag_map[NR_DIAG_STAT] = {
28 	[DIAG_STAT_X008] = { .code = 0x008, .name = "Console Function" },
29 	[DIAG_STAT_X00C] = { .code = 0x00c, .name = "Pseudo Timer" },
30 	[DIAG_STAT_X010] = { .code = 0x010, .name = "Release Pages" },
31 	[DIAG_STAT_X014] = { .code = 0x014, .name = "Spool File Services" },
32 	[DIAG_STAT_X044] = { .code = 0x044, .name = "Voluntary Timeslice End" },
33 	[DIAG_STAT_X064] = { .code = 0x064, .name = "NSS Manipulation" },
34 	[DIAG_STAT_X09C] = { .code = 0x09c, .name = "Relinquish Timeslice" },
35 	[DIAG_STAT_X0DC] = { .code = 0x0dc, .name = "Appldata Control" },
36 	[DIAG_STAT_X204] = { .code = 0x204, .name = "Logical-CPU Utilization" },
37 	[DIAG_STAT_X210] = { .code = 0x210, .name = "Device Information" },
38 	[DIAG_STAT_X224] = { .code = 0x224, .name = "EBCDIC-Name Table" },
39 	[DIAG_STAT_X250] = { .code = 0x250, .name = "Block I/O" },
40 	[DIAG_STAT_X258] = { .code = 0x258, .name = "Page-Reference Services" },
41 	[DIAG_STAT_X26C] = { .code = 0x26c, .name = "Certain System Information" },
42 	[DIAG_STAT_X288] = { .code = 0x288, .name = "Time Bomb" },
43 	[DIAG_STAT_X2C4] = { .code = 0x2c4, .name = "FTP Services" },
44 	[DIAG_STAT_X2FC] = { .code = 0x2fc, .name = "Guest Performance Data" },
45 	[DIAG_STAT_X304] = { .code = 0x304, .name = "Partition-Resource Service" },
46 	[DIAG_STAT_X308] = { .code = 0x308, .name = "List-Directed IPL" },
47 	[DIAG_STAT_X500] = { .code = 0x500, .name = "Virtio Service" },
48 };
49 
50 static int show_diag_stat(struct seq_file *m, void *v)
51 {
52 	struct diag_stat *stat;
53 	unsigned long n = (unsigned long) v - 1;
54 	int cpu, prec, tmp;
55 
56 	get_online_cpus();
57 	if (n == 0) {
58 		seq_puts(m, "         ");
59 
60 		for_each_online_cpu(cpu) {
61 			prec = 10;
62 			for (tmp = 10; cpu >= tmp; tmp *= 10)
63 				prec--;
64 			seq_printf(m, "%*s%d", prec, "CPU", cpu);
65 		}
66 		seq_putc(m, '\n');
67 	} else if (n <= NR_DIAG_STAT) {
68 		seq_printf(m, "diag %03x:", diag_map[n-1].code);
69 		for_each_online_cpu(cpu) {
70 			stat = &per_cpu(diag_stat, cpu);
71 			seq_printf(m, " %10u", stat->counter[n-1]);
72 		}
73 		seq_printf(m, "    %s\n", diag_map[n-1].name);
74 	}
75 	put_online_cpus();
76 	return 0;
77 }
78 
79 static void *show_diag_stat_start(struct seq_file *m, loff_t *pos)
80 {
81 	return *pos <= nr_cpu_ids ? (void *)((unsigned long) *pos + 1) : NULL;
82 }
83 
84 static void *show_diag_stat_next(struct seq_file *m, void *v, loff_t *pos)
85 {
86 	++*pos;
87 	return show_diag_stat_start(m, pos);
88 }
89 
90 static void show_diag_stat_stop(struct seq_file *m, void *v)
91 {
92 }
93 
94 static const struct seq_operations show_diag_stat_sops = {
95 	.start	= show_diag_stat_start,
96 	.next	= show_diag_stat_next,
97 	.stop	= show_diag_stat_stop,
98 	.show	= show_diag_stat,
99 };
100 
101 static int show_diag_stat_open(struct inode *inode, struct file *file)
102 {
103 	return seq_open(file, &show_diag_stat_sops);
104 }
105 
106 static const struct file_operations show_diag_stat_fops = {
107 	.open		= show_diag_stat_open,
108 	.read		= seq_read,
109 	.llseek		= seq_lseek,
110 	.release	= seq_release,
111 };
112 
113 
114 static int __init show_diag_stat_init(void)
115 {
116 	debugfs_create_file("diag_stat", 0400, NULL, NULL,
117 			    &show_diag_stat_fops);
118 	return 0;
119 }
120 
121 device_initcall(show_diag_stat_init);
122 
123 void diag_stat_inc(enum diag_stat_enum nr)
124 {
125 	this_cpu_inc(diag_stat.counter[nr]);
126 	trace_s390_diagnose(diag_map[nr].code);
127 }
128 EXPORT_SYMBOL(diag_stat_inc);
129 
130 void diag_stat_inc_norecursion(enum diag_stat_enum nr)
131 {
132 	this_cpu_inc(diag_stat.counter[nr]);
133 	trace_s390_diagnose_norecursion(diag_map[nr].code);
134 }
135 EXPORT_SYMBOL(diag_stat_inc_norecursion);
136 
137 /*
138  * Diagnose 14: Input spool file manipulation
139  */
140 static inline int __diag14(unsigned long rx, unsigned long ry1,
141 			   unsigned long subcode)
142 {
143 	register unsigned long _ry1 asm("2") = ry1;
144 	register unsigned long _ry2 asm("3") = subcode;
145 	int rc = 0;
146 
147 	asm volatile(
148 		"   sam31\n"
149 		"   diag    %2,2,0x14\n"
150 		"   sam64\n"
151 		"   ipm     %0\n"
152 		"   srl     %0,28\n"
153 		: "=d" (rc), "+d" (_ry2)
154 		: "d" (rx), "d" (_ry1)
155 		: "cc");
156 
157 	return rc;
158 }
159 
160 int diag14(unsigned long rx, unsigned long ry1, unsigned long subcode)
161 {
162 	diag_stat_inc(DIAG_STAT_X014);
163 	return __diag14(rx, ry1, subcode);
164 }
165 EXPORT_SYMBOL(diag14);
166 
167 static inline int __diag204(unsigned long *subcode, unsigned long size, void *addr)
168 {
169 	register unsigned long _subcode asm("0") = *subcode;
170 	register unsigned long _size asm("1") = size;
171 
172 	asm volatile(
173 		"	diag	%2,%0,0x204\n"
174 		"0:	nopr	%%r7\n"
175 		EX_TABLE(0b,0b)
176 		: "+d" (_subcode), "+d" (_size) : "d" (addr) : "memory");
177 	*subcode = _subcode;
178 	return _size;
179 }
180 
181 int diag204(unsigned long subcode, unsigned long size, void *addr)
182 {
183 	diag_stat_inc(DIAG_STAT_X204);
184 	size = __diag204(&subcode, size, addr);
185 	if (subcode)
186 		return -1;
187 	return size;
188 }
189 EXPORT_SYMBOL(diag204);
190 
191 /*
192  * Diagnose 210: Get information about a virtual device
193  */
194 int diag210(struct diag210 *addr)
195 {
196 	/*
197 	 * diag 210 needs its data below the 2GB border, so we
198 	 * use a static data area to be sure
199 	 */
200 	static struct diag210 diag210_tmp;
201 	static DEFINE_SPINLOCK(diag210_lock);
202 	unsigned long flags;
203 	int ccode;
204 
205 	spin_lock_irqsave(&diag210_lock, flags);
206 	diag210_tmp = *addr;
207 
208 	diag_stat_inc(DIAG_STAT_X210);
209 	asm volatile(
210 		"	lhi	%0,-1\n"
211 		"	sam31\n"
212 		"	diag	%1,0,0x210\n"
213 		"0:	ipm	%0\n"
214 		"	srl	%0,28\n"
215 		"1:	sam64\n"
216 		EX_TABLE(0b, 1b)
217 		: "=&d" (ccode) : "a" (&diag210_tmp) : "cc", "memory");
218 
219 	*addr = diag210_tmp;
220 	spin_unlock_irqrestore(&diag210_lock, flags);
221 
222 	return ccode;
223 }
224 EXPORT_SYMBOL(diag210);
225 
226 int diag224(void *ptr)
227 {
228 	int rc = -EOPNOTSUPP;
229 
230 	diag_stat_inc(DIAG_STAT_X224);
231 	asm volatile(
232 		"	diag	%1,%2,0x224\n"
233 		"0:	lhi	%0,0x0\n"
234 		"1:\n"
235 		EX_TABLE(0b,1b)
236 		: "+d" (rc) :"d" (0), "d" (ptr) : "memory");
237 	return rc;
238 }
239 EXPORT_SYMBOL(diag224);
240 
241 /*
242  * Diagnose 26C: Access Certain System Information
243  */
244 static inline int __diag26c(void *req, void *resp, enum diag26c_sc subcode)
245 {
246 	register unsigned long _req asm("2") = (addr_t) req;
247 	register unsigned long _resp asm("3") = (addr_t) resp;
248 	register unsigned long _subcode asm("4") = subcode;
249 	register unsigned long _rc asm("5") = -EOPNOTSUPP;
250 
251 	asm volatile(
252 		"	sam31\n"
253 		"	diag	%[rx],%[ry],0x26c\n"
254 		"0:	sam64\n"
255 		EX_TABLE(0b,0b)
256 		: "+d" (_rc)
257 		: [rx] "d" (_req), "d" (_resp), [ry] "d" (_subcode)
258 		: "cc", "memory");
259 	return _rc;
260 }
261 
262 int diag26c(void *req, void *resp, enum diag26c_sc subcode)
263 {
264 	diag_stat_inc(DIAG_STAT_X26C);
265 	return __diag26c(req, resp, subcode);
266 }
267 EXPORT_SYMBOL(diag26c);
268