diag.c 8.8 KB

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