Merge branch 'stable-4.5' of git://git.infradead.org/users/pcmoore/selinux into for...
[cascardo/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);
16 static bool have_vt220, have_linemode;
17
18 static 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;
28         psw_mask = __extract_psw();
29         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
51 static 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
69 static 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,
75                 0x80, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x40,
76                 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;
89         have_vt220 = masks[2] & EVTYP_VT220MSG_MASK;
90         have_linemode = masks[2] & EVTYP_MSG_MASK;
91         return 0;
92 }
93
94 /* Output multi-line text using SCLP Message interface. */
95 static void _sclp_print_lm(const char *str)
96 {
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 */
150         _sclp_servc(0x00760005, _sclp_work_area);
151 }
152
153 /* Output multi-line text (plus a newline) using SCLP VT220
154  * interface.
155  */
156 static void _sclp_print_vt220(const char *str)
157 {
158         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);
167
168         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  */
186 void _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);
195 }