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