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