sclp.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright IBM Corp. 2015
  3. * Author(s): Martin Schwidefsky <schwidefsky@de.ibm.com>
  4. */
  5. #include <linux/kernel.h>
  6. #include <asm/ebcdic.h>
  7. #include <asm/irq.h>
  8. #include <asm/lowcore.h>
  9. #include <asm/processor.h>
  10. #include <asm/sclp.h>
  11. #define EVTYP_VT220MSG_MASK 0x00000040
  12. #define EVTYP_MSG_MASK 0x40000000
  13. static char _sclp_work_area[4096] __aligned(PAGE_SIZE) __section(data);
  14. static bool have_vt220 __section(data);
  15. static bool have_linemode __section(data);
  16. static void _sclp_wait_int(void)
  17. {
  18. unsigned long cr0, cr0_new, psw_mask, addr;
  19. psw_t psw_ext_save, psw_wait;
  20. __ctl_store(cr0, 0, 0);
  21. cr0_new = cr0 | 0x200;
  22. __ctl_load(cr0_new, 0, 0);
  23. psw_ext_save = S390_lowcore.external_new_psw;
  24. psw_mask = __extract_psw();
  25. S390_lowcore.external_new_psw.mask = psw_mask;
  26. psw_wait.mask = psw_mask | PSW_MASK_EXT | PSW_MASK_WAIT;
  27. S390_lowcore.ext_int_code = 0;
  28. do {
  29. asm volatile(
  30. " larl %[addr],0f\n"
  31. " stg %[addr],%[psw_wait_addr]\n"
  32. " stg %[addr],%[psw_ext_addr]\n"
  33. " lpswe %[psw_wait]\n"
  34. "0:\n"
  35. : [addr] "=&d" (addr),
  36. [psw_wait_addr] "=Q" (psw_wait.addr),
  37. [psw_ext_addr] "=Q" (S390_lowcore.external_new_psw.addr)
  38. : [psw_wait] "Q" (psw_wait)
  39. : "cc", "memory");
  40. } while (S390_lowcore.ext_int_code != EXT_IRQ_SERVICE_SIG);
  41. __ctl_load(cr0, 0, 0);
  42. S390_lowcore.external_new_psw = psw_ext_save;
  43. }
  44. static int _sclp_servc(unsigned int cmd, char *sccb)
  45. {
  46. unsigned int cc;
  47. do {
  48. asm volatile(
  49. " .insn rre,0xb2200000,%1,%2\n"
  50. " ipm %0\n"
  51. : "=d" (cc) : "d" (cmd), "a" (sccb)
  52. : "cc", "memory");
  53. cc >>= 28;
  54. if (cc == 3)
  55. return -EINVAL;
  56. _sclp_wait_int();
  57. } while (cc != 0);
  58. return (*(unsigned short *)(sccb + 6) == 0x20) ? 0 : -EIO;
  59. }
  60. static int _sclp_setup(int disable)
  61. {
  62. static unsigned char init_sccb[] = {
  63. 0x00, 0x1c,
  64. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  65. 0x00, 0x04,
  66. 0x80, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40,
  67. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  68. };
  69. unsigned int *masks;
  70. int rc;
  71. memcpy(_sclp_work_area, init_sccb, 28);
  72. masks = (unsigned int *)(_sclp_work_area + 12);
  73. if (disable)
  74. memset(masks, 0, 16);
  75. /* SCLP write mask */
  76. rc = _sclp_servc(0x00780005, _sclp_work_area);
  77. if (rc)
  78. return rc;
  79. have_vt220 = masks[2] & EVTYP_VT220MSG_MASK;
  80. have_linemode = masks[2] & EVTYP_MSG_MASK;
  81. return 0;
  82. }
  83. /* Output multi-line text using SCLP Message interface. */
  84. static void _sclp_print_lm(const char *str)
  85. {
  86. static unsigned char write_head[] = {
  87. /* sccb header */
  88. 0x00, 0x52, /* 0 */
  89. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 2 */
  90. /* evbuf */
  91. 0x00, 0x4a, /* 8 */
  92. 0x02, 0x00, 0x00, 0x00, /* 10 */
  93. /* mdb */
  94. 0x00, 0x44, /* 14 */
  95. 0x00, 0x01, /* 16 */
  96. 0xd4, 0xc4, 0xc2, 0x40, /* 18 */
  97. 0x00, 0x00, 0x00, 0x01, /* 22 */
  98. /* go */
  99. 0x00, 0x38, /* 26 */
  100. 0x00, 0x01, /* 28 */
  101. 0x00, 0x00, 0x00, 0x00, /* 30 */
  102. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 34 */
  103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 42 */
  104. 0x00, 0x00, 0x00, 0x00, /* 50 */
  105. 0x00, 0x00, /* 54 */
  106. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 56 */
  107. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 64 */
  108. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 72 */
  109. 0x00, 0x00, /* 80 */
  110. };
  111. static unsigned char write_mto[] = {
  112. /* mto */
  113. 0x00, 0x0a, /* 0 */
  114. 0x00, 0x04, /* 2 */
  115. 0x10, 0x00, /* 4 */
  116. 0x00, 0x00, 0x00, 0x00 /* 6 */
  117. };
  118. unsigned char *ptr, ch;
  119. unsigned int count;
  120. memcpy(_sclp_work_area, write_head, sizeof(write_head));
  121. ptr = _sclp_work_area + sizeof(write_head);
  122. do {
  123. memcpy(ptr, write_mto, sizeof(write_mto));
  124. for (count = sizeof(write_mto); (ch = *str++) != 0; count++) {
  125. if (ch == 0x0a)
  126. break;
  127. ptr[count] = _ascebc[ch];
  128. }
  129. /* Update length fields in mto, mdb, evbuf and sccb */
  130. *(unsigned short *) ptr = count;
  131. *(unsigned short *)(_sclp_work_area + 14) += count;
  132. *(unsigned short *)(_sclp_work_area + 8) += count;
  133. *(unsigned short *)(_sclp_work_area + 0) += count;
  134. ptr += count;
  135. } while (ch != 0);
  136. /* SCLP write data */
  137. _sclp_servc(0x00760005, _sclp_work_area);
  138. }
  139. /* Output multi-line text (plus a newline) using SCLP VT220
  140. * interface.
  141. */
  142. static void _sclp_print_vt220(const char *str)
  143. {
  144. static unsigned char const write_head[] = {
  145. /* sccb header */
  146. 0x00, 0x0e,
  147. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  148. /* evbuf header */
  149. 0x00, 0x06,
  150. 0x1a, 0x00, 0x00, 0x00,
  151. };
  152. size_t len = strlen(str);
  153. if (sizeof(write_head) + len >= sizeof(_sclp_work_area))
  154. len = sizeof(_sclp_work_area) - sizeof(write_head) - 1;
  155. memcpy(_sclp_work_area, write_head, sizeof(write_head));
  156. memcpy(_sclp_work_area + sizeof(write_head), str, len);
  157. _sclp_work_area[sizeof(write_head) + len] = '\n';
  158. /* Update length fields in evbuf and sccb headers */
  159. *(unsigned short *)(_sclp_work_area + 8) += len + 1;
  160. *(unsigned short *)(_sclp_work_area + 0) += len + 1;
  161. /* SCLP write data */
  162. (void)_sclp_servc(0x00760005, _sclp_work_area);
  163. }
  164. /* Output one or more lines of text on the SCLP console (VT220 and /
  165. * or line-mode). All lines get terminated; no need for a trailing LF.
  166. */
  167. void _sclp_print_early(const char *str)
  168. {
  169. if (_sclp_setup(0) != 0)
  170. return;
  171. if (have_linemode)
  172. _sclp_print_lm(str);
  173. if (have_vt220)
  174. _sclp_print_vt220(str);
  175. _sclp_setup(1);
  176. }