sclp.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. static char _sclp_work_area[4096] __aligned(PAGE_SIZE);
  12. static void _sclp_wait_int(void)
  13. {
  14. unsigned long cr0, cr0_new, psw_mask, addr;
  15. psw_t psw_ext_save, psw_wait;
  16. __ctl_store(cr0, 0, 0);
  17. cr0_new = cr0 | 0x200;
  18. __ctl_load(cr0_new, 0, 0);
  19. psw_ext_save = S390_lowcore.external_new_psw;
  20. psw_mask = __extract_psw();
  21. S390_lowcore.external_new_psw.mask = psw_mask;
  22. psw_wait.mask = psw_mask | PSW_MASK_EXT | PSW_MASK_WAIT;
  23. S390_lowcore.ext_int_code = 0;
  24. do {
  25. asm volatile(
  26. " larl %[addr],0f\n"
  27. " stg %[addr],%[psw_wait_addr]\n"
  28. " stg %[addr],%[psw_ext_addr]\n"
  29. " lpswe %[psw_wait]\n"
  30. "0:\n"
  31. : [addr] "=&d" (addr),
  32. [psw_wait_addr] "=Q" (psw_wait.addr),
  33. [psw_ext_addr] "=Q" (S390_lowcore.external_new_psw.addr)
  34. : [psw_wait] "Q" (psw_wait)
  35. : "cc", "memory");
  36. } while (S390_lowcore.ext_int_code != EXT_IRQ_SERVICE_SIG);
  37. __ctl_load(cr0, 0, 0);
  38. S390_lowcore.external_new_psw = psw_ext_save;
  39. }
  40. static int _sclp_servc(unsigned int cmd, char *sccb)
  41. {
  42. unsigned int cc;
  43. do {
  44. asm volatile(
  45. " .insn rre,0xb2200000,%1,%2\n"
  46. " ipm %0\n"
  47. : "=d" (cc) : "d" (cmd), "a" (sccb)
  48. : "cc", "memory");
  49. cc >>= 28;
  50. if (cc == 3)
  51. return -EINVAL;
  52. _sclp_wait_int();
  53. } while (cc != 0);
  54. return (*(unsigned short *)(sccb + 6) == 0x20) ? 0 : -EIO;
  55. }
  56. static int _sclp_setup(int disable)
  57. {
  58. static unsigned char init_sccb[] = {
  59. 0x00, 0x1c,
  60. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61. 0x00, 0x04,
  62. 0x80, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  63. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  64. };
  65. unsigned int *masks;
  66. int rc;
  67. memcpy(_sclp_work_area, init_sccb, 28);
  68. masks = (unsigned int *)(_sclp_work_area + 12);
  69. if (disable)
  70. memset(masks, 0, 16);
  71. /* SCLP write mask */
  72. rc = _sclp_servc(0x00780005, _sclp_work_area);
  73. if (rc)
  74. return rc;
  75. if ((masks[0] & masks[3]) != masks[0] ||
  76. (masks[1] & masks[2]) != masks[1])
  77. return -EIO;
  78. return 0;
  79. }
  80. static int _sclp_print(const char *str)
  81. {
  82. static unsigned char write_head[] = {
  83. /* sccb header */
  84. 0x00, 0x52, /* 0 */
  85. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 2 */
  86. /* evbuf */
  87. 0x00, 0x4a, /* 8 */
  88. 0x02, 0x00, 0x00, 0x00, /* 10 */
  89. /* mdb */
  90. 0x00, 0x44, /* 14 */
  91. 0x00, 0x01, /* 16 */
  92. 0xd4, 0xc4, 0xc2, 0x40, /* 18 */
  93. 0x00, 0x00, 0x00, 0x01, /* 22 */
  94. /* go */
  95. 0x00, 0x38, /* 26 */
  96. 0x00, 0x01, /* 28 */
  97. 0x00, 0x00, 0x00, 0x00, /* 30 */
  98. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 34 */
  99. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 42 */
  100. 0x00, 0x00, 0x00, 0x00, /* 50 */
  101. 0x00, 0x00, /* 54 */
  102. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 56 */
  103. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 64 */
  104. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 72 */
  105. 0x00, 0x00, /* 80 */
  106. };
  107. static unsigned char write_mto[] = {
  108. /* mto */
  109. 0x00, 0x0a, /* 0 */
  110. 0x00, 0x04, /* 2 */
  111. 0x10, 0x00, /* 4 */
  112. 0x00, 0x00, 0x00, 0x00 /* 6 */
  113. };
  114. unsigned char *ptr, ch;
  115. unsigned int count;
  116. memcpy(_sclp_work_area, write_head, sizeof(write_head));
  117. ptr = _sclp_work_area + sizeof(write_head);
  118. do {
  119. memcpy(ptr, write_mto, sizeof(write_mto));
  120. for (count = sizeof(write_mto); (ch = *str++) != 0; count++) {
  121. if (ch == 0x0a)
  122. break;
  123. ptr[count] = _ascebc[ch];
  124. }
  125. /* Update length fields in mto, mdb, evbuf and sccb */
  126. *(unsigned short *) ptr = count;
  127. *(unsigned short *)(_sclp_work_area + 14) += count;
  128. *(unsigned short *)(_sclp_work_area + 8) += count;
  129. *(unsigned short *)(_sclp_work_area + 0) += count;
  130. ptr += count;
  131. } while (ch != 0);
  132. /* SCLP write data */
  133. return _sclp_servc(0x00760005, _sclp_work_area);
  134. }
  135. int _sclp_print_early(const char *str)
  136. {
  137. int rc;
  138. rc = _sclp_setup(0);
  139. if (rc)
  140. return rc;
  141. rc = _sclp_print(str);
  142. if (rc)
  143. return rc;
  144. return _sclp_setup(1);
  145. }