xref: /openbmc/linux/arch/s390/kernel/diag.c (revision 25b892b5)
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 <asm/diag.h>
15 #include <asm/trace/diag.h>
16 #include <asm/sections.h>
17 #include "entry.h"
18 
19 struct diag_stat {
20 	unsigned int counter[NR_DIAG_STAT];
21 };
22 
23 static DEFINE_PER_CPU(struct diag_stat, diag_stat);
24 
25 struct diag_desc {
26 	int code;
27 	char *name;
28 };
29 
30 static const struct diag_desc diag_map[NR_DIAG_STAT] = {
31 	[DIAG_STAT_X008] = { .code = 0x008, .name = "Console Function" },
32 	[DIAG_STAT_X00C] = { .code = 0x00c, .name = "Pseudo Timer" },
33 	[DIAG_STAT_X010] = { .code = 0x010, .name = "Release Pages" },
34 	[DIAG_STAT_X014] = { .code = 0x014, .name = "Spool File Services" },
35 	[DIAG_STAT_X044] = { .code = 0x044, .name = "Voluntary Timeslice End" },
36 	[DIAG_STAT_X064] = { .code = 0x064, .name = "NSS Manipulation" },
37 	[DIAG_STAT_X09C] = { .code = 0x09c, .name = "Relinquish Timeslice" },
38 	[DIAG_STAT_X0DC] = { .code = 0x0dc, .name = "Appldata Control" },
39 	[DIAG_STAT_X204] = { .code = 0x204, .name = "Logical-CPU Utilization" },
40 	[DIAG_STAT_X210] = { .code = 0x210, .name = "Device Information" },
41 	[DIAG_STAT_X224] = { .code = 0x224, .name = "EBCDIC-Name Table" },
42 	[DIAG_STAT_X250] = { .code = 0x250, .name = "Block I/O" },
43 	[DIAG_STAT_X258] = { .code = 0x258, .name = "Page-Reference Services" },
44 	[DIAG_STAT_X26C] = { .code = 0x26c, .name = "Certain System Information" },
45 	[DIAG_STAT_X288] = { .code = 0x288, .name = "Time Bomb" },
46 	[DIAG_STAT_X2C4] = { .code = 0x2c4, .name = "FTP Services" },
47 	[DIAG_STAT_X2FC] = { .code = 0x2fc, .name = "Guest Performance Data" },
48 	[DIAG_STAT_X304] = { .code = 0x304, .name = "Partition-Resource Service" },
49 	[DIAG_STAT_X308] = { .code = 0x308, .name = "List-Directed IPL" },
50 	[DIAG_STAT_X318] = { .code = 0x318, .name = "CP Name and Version Codes" },
51 	[DIAG_STAT_X500] = { .code = 0x500, .name = "Virtio Service" },
52 };
53 
54 struct diag_ops __amode31_ref diag_amode31_ops = {
55 	.diag210 = _diag210_amode31,
56 	.diag26c = _diag26c_amode31,
57 	.diag14 = _diag14_amode31,
58 	.diag0c = _diag0c_amode31,
59 	.diag308_reset = _diag308_reset_amode31
60 };
61 
62 static struct diag210 _diag210_tmp_amode31 __section(".amode31.data");
63 struct diag210 __amode31_ref *__diag210_tmp_amode31 = &_diag210_tmp_amode31;
64 
65 static int show_diag_stat(struct seq_file *m, void *v)
66 {
67 	struct diag_stat *stat;
68 	unsigned long n = (unsigned long) v - 1;
69 	int cpu, prec, tmp;
70 
71 	cpus_read_lock();
72 	if (n == 0) {
73 		seq_puts(m, "         ");
74 
75 		for_each_online_cpu(cpu) {
76 			prec = 10;
77 			for (tmp = 10; cpu >= tmp; tmp *= 10)
78 				prec--;
79 			seq_printf(m, "%*s%d", prec, "CPU", cpu);
80 		}
81 		seq_putc(m, '\n');
82 	} else if (n <= NR_DIAG_STAT) {
83 		seq_printf(m, "diag %03x:", diag_map[n-1].code);
84 		for_each_online_cpu(cpu) {
85 			stat = &per_cpu(diag_stat, cpu);
86 			seq_printf(m, " %10u", stat->counter[n-1]);
87 		}
88 		seq_printf(m, "    %s\n", diag_map[n-1].name);
89 	}
90 	cpus_read_unlock();
91 	return 0;
92 }
93 
94 static void *show_diag_stat_start(struct seq_file *m, loff_t *pos)
95 {
96 	return *pos <= NR_DIAG_STAT ? (void *)((unsigned long) *pos + 1) : NULL;
97 }
98 
99 static void *show_diag_stat_next(struct seq_file *m, void *v, loff_t *pos)
100 {
101 	++*pos;
102 	return show_diag_stat_start(m, pos);
103 }
104 
105 static void show_diag_stat_stop(struct seq_file *m, void *v)
106 {
107 }
108 
109 static const struct seq_operations show_diag_stat_sops = {
110 	.start	= show_diag_stat_start,
111 	.next	= show_diag_stat_next,
112 	.stop	= show_diag_stat_stop,
113 	.show	= show_diag_stat,
114 };
115 
116 DEFINE_SEQ_ATTRIBUTE(show_diag_stat);
117 
118 static int __init show_diag_stat_init(void)
119 {
120 	debugfs_create_file("diag_stat", 0400, NULL, NULL,
121 			    &show_diag_stat_fops);
122 	return 0;
123 }
124 
125 device_initcall(show_diag_stat_init);
126 
127 void diag_stat_inc(enum diag_stat_enum nr)
128 {
129 	this_cpu_inc(diag_stat.counter[nr]);
130 	trace_s390_diagnose(diag_map[nr].code);
131 }
132 EXPORT_SYMBOL(diag_stat_inc);
133 
134 void notrace diag_stat_inc_norecursion(enum diag_stat_enum nr)
135 {
136 	this_cpu_inc(diag_stat.counter[nr]);
137 	trace_s390_diagnose_norecursion(diag_map[nr].code);
138 }
139 EXPORT_SYMBOL(diag_stat_inc_norecursion);
140 
141 /*
142  * Diagnose 14: Input spool file manipulation
143  */
144 int diag14(unsigned long rx, unsigned long ry1, unsigned long subcode)
145 {
146 	diag_stat_inc(DIAG_STAT_X014);
147 	return diag_amode31_ops.diag14(rx, ry1, subcode);
148 }
149 EXPORT_SYMBOL(diag14);
150 
151 static inline int __diag204(unsigned long *subcode, unsigned long size, void *addr)
152 {
153 	union register_pair rp = { .even = *subcode, .odd = size };
154 
155 	asm volatile(
156 		"	diag	%[addr],%[rp],0x204\n"
157 		"0:	nopr	%%r7\n"
158 		EX_TABLE(0b,0b)
159 		: [rp] "+&d" (rp.pair) : [addr] "d" (addr) : "memory");
160 	*subcode = rp.even;
161 	return rp.odd;
162 }
163 
164 int diag204(unsigned long subcode, unsigned long size, void *addr)
165 {
166 	diag_stat_inc(DIAG_STAT_X204);
167 	size = __diag204(&subcode, size, addr);
168 	if (subcode)
169 		return -1;
170 	return size;
171 }
172 EXPORT_SYMBOL(diag204);
173 
174 /*
175  * Diagnose 210: Get information about a virtual device
176  */
177 int diag210(struct diag210 *addr)
178 {
179 	static DEFINE_SPINLOCK(diag210_lock);
180 	unsigned long flags;
181 	int ccode;
182 
183 	spin_lock_irqsave(&diag210_lock, flags);
184 	*__diag210_tmp_amode31 = *addr;
185 
186 	diag_stat_inc(DIAG_STAT_X210);
187 	ccode = diag_amode31_ops.diag210(__diag210_tmp_amode31);
188 
189 	*addr = *__diag210_tmp_amode31;
190 	spin_unlock_irqrestore(&diag210_lock, flags);
191 
192 	return ccode;
193 }
194 EXPORT_SYMBOL(diag210);
195 
196 int diag224(void *ptr)
197 {
198 	int rc = -EOPNOTSUPP;
199 
200 	diag_stat_inc(DIAG_STAT_X224);
201 	asm volatile(
202 		"	diag	%1,%2,0x224\n"
203 		"0:	lhi	%0,0x0\n"
204 		"1:\n"
205 		EX_TABLE(0b,1b)
206 		: "+d" (rc) :"d" (0), "d" (ptr) : "memory");
207 	return rc;
208 }
209 EXPORT_SYMBOL(diag224);
210 
211 /*
212  * Diagnose 26C: Access Certain System Information
213  */
214 int diag26c(void *req, void *resp, enum diag26c_sc subcode)
215 {
216 	diag_stat_inc(DIAG_STAT_X26C);
217 	return diag_amode31_ops.diag26c(req, resp, subcode);
218 }
219 EXPORT_SYMBOL(diag26c);
220