]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - arch/s390/kernel/sclp.c
s390: provide sclp based boot console
[karo-tx-linux.git] / arch / s390 / kernel / sclp.c
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
12 #define EVTYP_VT220MSG_MASK     0x00000040
13 #define EVTYP_MSG_MASK          0x40000000
14
15 static char _sclp_work_area[4096] __aligned(PAGE_SIZE) __section(data);
16 static bool have_vt220 __section(data);
17 static bool have_linemode __section(data);
18
19 static void _sclp_wait_int(void)
20 {
21         unsigned long psw_mask, addr, flags;
22         psw_t psw_ext_save, psw_wait;
23         union ctlreg0 cr0, cr0_new;
24
25         raw_local_irq_save(flags);
26         __ctl_store(cr0.val, 0, 0);
27         cr0_new.val = cr0.val & ~CR0_IRQ_SUBCLASS_MASK;
28         cr0_new.lap = 0;
29         cr0_new.sssm = 1;
30         __ctl_load(cr0_new.val, 0, 0);
31
32         psw_ext_save = S390_lowcore.external_new_psw;
33         psw_mask = __extract_psw();
34         S390_lowcore.external_new_psw.mask = psw_mask;
35         psw_wait.mask = psw_mask | PSW_MASK_EXT | PSW_MASK_WAIT;
36         S390_lowcore.ext_int_code = 0;
37
38         do {
39                 asm volatile(
40                         "       larl    %[addr],0f\n"
41                         "       stg     %[addr],%[psw_wait_addr]\n"
42                         "       stg     %[addr],%[psw_ext_addr]\n"
43                         "       lpswe   %[psw_wait]\n"
44                         "0:\n"
45                         : [addr] "=&d" (addr),
46                           [psw_wait_addr] "=Q" (psw_wait.addr),
47                           [psw_ext_addr] "=Q" (S390_lowcore.external_new_psw.addr)
48                         : [psw_wait] "Q" (psw_wait)
49                         : "cc", "memory");
50         } while (S390_lowcore.ext_int_code != EXT_IRQ_SERVICE_SIG);
51
52         S390_lowcore.external_new_psw = psw_ext_save;
53         __ctl_load(cr0.val, 0, 0);
54         raw_local_irq_restore(flags);
55 }
56
57 static int _sclp_servc(unsigned int cmd, char *sccb)
58 {
59         unsigned int cc;
60
61         do {
62                 asm volatile(
63                         "       .insn   rre,0xb2200000,%1,%2\n"
64                         "       ipm     %0\n"
65                         : "=d" (cc) : "d" (cmd), "a" (sccb)
66                         : "cc", "memory");
67                 cc >>= 28;
68                 if (cc == 3)
69                         return -EINVAL;
70                 _sclp_wait_int();
71         } while (cc != 0);
72         return (*(unsigned short *)(sccb + 6) == 0x20) ? 0 : -EIO;
73 }
74
75 static int _sclp_setup(int disable)
76 {
77         static unsigned char init_sccb[] = {
78                 0x00, 0x1c,
79                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80                 0x00, 0x04,
81                 0x80, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40,
82                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
83         };
84         unsigned int *masks;
85         int rc;
86
87         memcpy(_sclp_work_area, init_sccb, 28);
88         masks = (unsigned int *)(_sclp_work_area + 12);
89         if (disable)
90                 memset(masks, 0, 16);
91         /* SCLP write mask */
92         rc = _sclp_servc(0x00780005, _sclp_work_area);
93         if (rc)
94                 return rc;
95         have_vt220 = masks[2] & EVTYP_VT220MSG_MASK;
96         have_linemode = masks[2] & EVTYP_MSG_MASK;
97         return 0;
98 }
99
100 /* Output multi-line text using SCLP Message interface. */
101 static void _sclp_print_lm(const char *str, unsigned int len)
102 {
103         static unsigned char write_head[] = {
104                 /* sccb header */
105                 0x00, 0x52,                                     /* 0 */
106                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,             /* 2 */
107                 /* evbuf */
108                 0x00, 0x4a,                                     /* 8 */
109                 0x02, 0x00, 0x00, 0x00,                         /* 10 */
110                 /* mdb */
111                 0x00, 0x44,                                     /* 14 */
112                 0x00, 0x01,                                     /* 16 */
113                 0xd4, 0xc4, 0xc2, 0x40,                         /* 18 */
114                 0x00, 0x00, 0x00, 0x01,                         /* 22 */
115                 /* go */
116                 0x00, 0x38,                                     /* 26 */
117                 0x00, 0x01,                                     /* 28 */
118                 0x00, 0x00, 0x00, 0x00,                         /* 30 */
119                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 34 */
120                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 42 */
121                 0x00, 0x00, 0x00, 0x00,                         /* 50 */
122                 0x00, 0x00,                                     /* 54 */
123                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 56 */
124                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 64 */
125                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* 72 */
126                 0x00, 0x00,                                     /* 80 */
127         };
128         static unsigned char write_mto[] = {
129                 /* mto  */
130                 0x00, 0x0a,                                     /* 0 */
131                 0x00, 0x04,                                     /* 2 */
132                 0x10, 0x00,                                     /* 4 */
133                 0x00, 0x00, 0x00, 0x00                          /* 6 */
134         };
135         unsigned char *ptr, *end_ptr, ch;
136         unsigned int count, num;
137
138         num = 0;
139         memcpy(_sclp_work_area, write_head, sizeof(write_head));
140         ptr = _sclp_work_area + sizeof(write_head);
141         end_ptr = _sclp_work_area + sizeof(_sclp_work_area) - 1;
142         do {
143                 if (ptr + sizeof(write_mto) > end_ptr)
144                         break;
145                 memcpy(ptr, write_mto, sizeof(write_mto));
146                 for (count = sizeof(write_mto); num < len; count++) {
147                         num++;
148                         ch = *str++;
149                         if (ch == 0x0a)
150                                 break;
151                         if (ptr > end_ptr)
152                                 break;
153                         ptr[count] = _ascebc[ch];
154                 }
155                 /* Update length fields in mto, mdb, evbuf and sccb */
156                 *(unsigned short *) ptr = count;
157                 *(unsigned short *)(_sclp_work_area + 14) += count;
158                 *(unsigned short *)(_sclp_work_area + 8) += count;
159                 *(unsigned short *)(_sclp_work_area + 0) += count;
160                 ptr += count;
161         } while (num < len);
162
163         /* SCLP write data */
164         _sclp_servc(0x00760005, _sclp_work_area);
165 }
166
167 /* Output multi-line text (plus a newline) using SCLP VT220
168  * interface.
169  */
170 static void _sclp_print_vt220(const char *str, unsigned int len)
171 {
172         static unsigned char const write_head[] = {
173                 /* sccb header */
174                 0x00, 0x0e,
175                 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
176                 /* evbuf header */
177                 0x00, 0x06,
178                 0x1a, 0x00, 0x00, 0x00,
179         };
180
181         if (sizeof(write_head) + len >= sizeof(_sclp_work_area))
182                 len = sizeof(_sclp_work_area) - sizeof(write_head) - 1;
183
184         memcpy(_sclp_work_area, write_head, sizeof(write_head));
185         memcpy(_sclp_work_area + sizeof(write_head), str, len);
186         _sclp_work_area[sizeof(write_head) + len] = '\n';
187
188         /* Update length fields in evbuf and sccb headers */
189         *(unsigned short *)(_sclp_work_area + 8) += len + 1;
190         *(unsigned short *)(_sclp_work_area + 0) += len + 1;
191
192         /* SCLP write data */
193         (void)_sclp_servc(0x00760005, _sclp_work_area);
194 }
195
196 /* Output one or more lines of text on the SCLP console (VT220 and /
197  * or line-mode). All lines get terminated; no need for a trailing LF.
198  */
199 void __sclp_print_early(const char *str, unsigned int len)
200 {
201         if (_sclp_setup(0) != 0)
202                 return;
203         if (have_linemode)
204                 _sclp_print_lm(str, len);
205         if (have_vt220)
206                 _sclp_print_vt220(str, len);
207         _sclp_setup(1);
208 }
209
210 void _sclp_print_early(const char *str)
211 {
212         __sclp_print_early(str, strlen(str));
213 }