blob: d88db40bdf1591897326d05a6096d6653935bde3 [file] [log] [blame]
Martin Schwidefsky22362a02015-07-08 10:20:04 +02001/*
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
Sascha Silbe3f975df2015-11-24 16:28:55 +010012#define EVTYP_VT220MSG_MASK 0x00000040
13#define EVTYP_MSG_MASK 0x40000000
14
Martin Schwidefsky22362a02015-07-08 10:20:04 +020015static char _sclp_work_area[4096] __aligned(PAGE_SIZE);
Sascha Silbe3f975df2015-11-24 16:28:55 +010016static bool have_vt220, have_linemode;
Martin Schwidefsky22362a02015-07-08 10:20:04 +020017
18static void _sclp_wait_int(void)
19{
20 unsigned long cr0, cr0_new, psw_mask, addr;
21 psw_t psw_ext_save, psw_wait;
22
23 __ctl_store(cr0, 0, 0);
24 cr0_new = cr0 | 0x200;
25 __ctl_load(cr0_new, 0, 0);
26
27 psw_ext_save = S390_lowcore.external_new_psw;
Sascha Silbef07f21b2015-11-04 14:16:57 +010028 psw_mask = __extract_psw();
Martin Schwidefsky22362a02015-07-08 10:20:04 +020029 S390_lowcore.external_new_psw.mask = psw_mask;
30 psw_wait.mask = psw_mask | PSW_MASK_EXT | PSW_MASK_WAIT;
31 S390_lowcore.ext_int_code = 0;
32
33 do {
34 asm volatile(
35 " larl %[addr],0f\n"
36 " stg %[addr],%[psw_wait_addr]\n"
37 " stg %[addr],%[psw_ext_addr]\n"
38 " lpswe %[psw_wait]\n"
39 "0:\n"
40 : [addr] "=&d" (addr),
41 [psw_wait_addr] "=Q" (psw_wait.addr),
42 [psw_ext_addr] "=Q" (S390_lowcore.external_new_psw.addr)
43 : [psw_wait] "Q" (psw_wait)
44 : "cc", "memory");
45 } while (S390_lowcore.ext_int_code != EXT_IRQ_SERVICE_SIG);
46
47 __ctl_load(cr0, 0, 0);
48 S390_lowcore.external_new_psw = psw_ext_save;
49}
50
51static int _sclp_servc(unsigned int cmd, char *sccb)
52{
53 unsigned int cc;
54
55 do {
56 asm volatile(
57 " .insn rre,0xb2200000,%1,%2\n"
58 " ipm %0\n"
59 : "=d" (cc) : "d" (cmd), "a" (sccb)
60 : "cc", "memory");
61 cc >>= 28;
62 if (cc == 3)
63 return -EINVAL;
64 _sclp_wait_int();
65 } while (cc != 0);
66 return (*(unsigned short *)(sccb + 6) == 0x20) ? 0 : -EIO;
67}
68
69static int _sclp_setup(int disable)
70{
71 static unsigned char init_sccb[] = {
72 0x00, 0x1c,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x04,
Sascha Silbe3f975df2015-11-24 16:28:55 +010075 0x80, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40,
Martin Schwidefsky22362a02015-07-08 10:20:04 +020076 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
77 };
78 unsigned int *masks;
79 int rc;
80
81 memcpy(_sclp_work_area, init_sccb, 28);
82 masks = (unsigned int *)(_sclp_work_area + 12);
83 if (disable)
84 memset(masks, 0, 16);
85 /* SCLP write mask */
86 rc = _sclp_servc(0x00780005, _sclp_work_area);
87 if (rc)
88 return rc;
Sascha Silbe3f975df2015-11-24 16:28:55 +010089 have_vt220 = masks[2] & EVTYP_VT220MSG_MASK;
90 have_linemode = masks[2] & EVTYP_MSG_MASK;
Martin Schwidefsky22362a02015-07-08 10:20:04 +020091 return 0;
92}
93
Sascha Silbe3f975df2015-11-24 16:28:55 +010094/* Output multi-line text using SCLP Message interface. */
95static void _sclp_print_lm(const char *str)
Martin Schwidefsky22362a02015-07-08 10:20:04 +020096{
97 static unsigned char write_head[] = {
98 /* sccb header */
99 0x00, 0x52, /* 0 */
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 2 */
101 /* evbuf */
102 0x00, 0x4a, /* 8 */
103 0x02, 0x00, 0x00, 0x00, /* 10 */
104 /* mdb */
105 0x00, 0x44, /* 14 */
106 0x00, 0x01, /* 16 */
107 0xd4, 0xc4, 0xc2, 0x40, /* 18 */
108 0x00, 0x00, 0x00, 0x01, /* 22 */
109 /* go */
110 0x00, 0x38, /* 26 */
111 0x00, 0x01, /* 28 */
112 0x00, 0x00, 0x00, 0x00, /* 30 */
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 34 */
114 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 42 */
115 0x00, 0x00, 0x00, 0x00, /* 50 */
116 0x00, 0x00, /* 54 */
117 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 56 */
118 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 64 */
119 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 72 */
120 0x00, 0x00, /* 80 */
121 };
122 static unsigned char write_mto[] = {
123 /* mto */
124 0x00, 0x0a, /* 0 */
125 0x00, 0x04, /* 2 */
126 0x10, 0x00, /* 4 */
127 0x00, 0x00, 0x00, 0x00 /* 6 */
128 };
129 unsigned char *ptr, ch;
130 unsigned int count;
131
132 memcpy(_sclp_work_area, write_head, sizeof(write_head));
133 ptr = _sclp_work_area + sizeof(write_head);
134 do {
135 memcpy(ptr, write_mto, sizeof(write_mto));
136 for (count = sizeof(write_mto); (ch = *str++) != 0; count++) {
137 if (ch == 0x0a)
138 break;
139 ptr[count] = _ascebc[ch];
140 }
141 /* Update length fields in mto, mdb, evbuf and sccb */
142 *(unsigned short *) ptr = count;
143 *(unsigned short *)(_sclp_work_area + 14) += count;
144 *(unsigned short *)(_sclp_work_area + 8) += count;
145 *(unsigned short *)(_sclp_work_area + 0) += count;
146 ptr += count;
147 } while (ch != 0);
148
149 /* SCLP write data */
Sascha Silbe3f975df2015-11-24 16:28:55 +0100150 _sclp_servc(0x00760005, _sclp_work_area);
Martin Schwidefsky22362a02015-07-08 10:20:04 +0200151}
152
Sascha Silbe3f975df2015-11-24 16:28:55 +0100153/* Output multi-line text (plus a newline) using SCLP VT220
154 * interface.
155 */
156static void _sclp_print_vt220(const char *str)
Martin Schwidefsky22362a02015-07-08 10:20:04 +0200157{
Sascha Silbe3f975df2015-11-24 16:28:55 +0100158 static unsigned char const write_head[] = {
159 /* sccb header */
160 0x00, 0x0e,
161 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162 /* evbuf header */
163 0x00, 0x06,
164 0x1a, 0x00, 0x00, 0x00,
165 };
166 size_t len = strlen(str);
Martin Schwidefsky22362a02015-07-08 10:20:04 +0200167
Sascha Silbe3f975df2015-11-24 16:28:55 +0100168 if (sizeof(write_head) + len >= sizeof(_sclp_work_area))
169 len = sizeof(_sclp_work_area) - sizeof(write_head) - 1;
170
171 memcpy(_sclp_work_area, write_head, sizeof(write_head));
172 memcpy(_sclp_work_area + sizeof(write_head), str, len);
173 _sclp_work_area[sizeof(write_head) + len] = '\n';
174
175 /* Update length fields in evbuf and sccb headers */
176 *(unsigned short *)(_sclp_work_area + 8) += len + 1;
177 *(unsigned short *)(_sclp_work_area + 0) += len + 1;
178
179 /* SCLP write data */
180 (void)_sclp_servc(0x00760005, _sclp_work_area);
181}
182
183/* Output one or more lines of text on the SCLP console (VT220 and /
184 * or line-mode). All lines get terminated; no need for a trailing LF.
185 */
186void _sclp_print_early(const char *str)
187{
188 if (_sclp_setup(0) != 0)
189 return;
190 if (have_linemode)
191 _sclp_print_lm(str);
192 if (have_vt220)
193 _sclp_print_vt220(str);
194 _sclp_setup(1);
Martin Schwidefsky22362a02015-07-08 10:20:04 +0200195}