]> Git Repo - J-linux.git/blob - drivers/s390/char/sclp_early_core.c
Merge tag 'amd-drm-next-6.5-2023-06-09' of https://gitlab.freedesktop.org/agd5f/linux...
[J-linux.git] / drivers / s390 / char / sclp_early_core.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *    Copyright IBM Corp. 2015
4  *    Author(s): Martin Schwidefsky <[email protected]>
5  */
6
7 #include <linux/kernel.h>
8 #include <asm/processor.h>
9 #include <asm/lowcore.h>
10 #include <asm/ebcdic.h>
11 #include <asm/irq.h>
12 #include <asm/sections.h>
13 #include <asm/physmem_info.h>
14 #include <asm/facility.h>
15 #include "sclp.h"
16 #include "sclp_rw.h"
17
18 static struct read_info_sccb __bootdata(sclp_info_sccb);
19 static int __bootdata(sclp_info_sccb_valid);
20 char *__bootdata_preserved(sclp_early_sccb);
21 int sclp_init_state = sclp_init_state_uninitialized;
22 /*
23  * Used to keep track of the size of the event masks. Qemu until version 2.11
24  * only supports 4 and needs a workaround.
25  */
26 bool sclp_mask_compat_mode;
27
28 void sclp_early_wait_irq(void)
29 {
30         unsigned long psw_mask, addr;
31         psw_t psw_ext_save, psw_wait;
32         union ctlreg0 cr0, cr0_new;
33
34         __ctl_store(cr0.val, 0, 0);
35         cr0_new.val = cr0.val & ~CR0_IRQ_SUBCLASS_MASK;
36         cr0_new.lap = 0;
37         cr0_new.sssm = 1;
38         __ctl_load(cr0_new.val, 0, 0);
39
40         psw_ext_save = S390_lowcore.external_new_psw;
41         psw_mask = __extract_psw();
42         S390_lowcore.external_new_psw.mask = psw_mask;
43         psw_wait.mask = psw_mask | PSW_MASK_EXT | PSW_MASK_WAIT;
44         S390_lowcore.ext_int_code = 0;
45
46         do {
47                 asm volatile(
48                         "       larl    %[addr],0f\n"
49                         "       stg     %[addr],%[psw_wait_addr]\n"
50                         "       stg     %[addr],%[psw_ext_addr]\n"
51                         "       lpswe   %[psw_wait]\n"
52                         "0:\n"
53                         : [addr] "=&d" (addr),
54                           [psw_wait_addr] "=Q" (psw_wait.addr),
55                           [psw_ext_addr] "=Q" (S390_lowcore.external_new_psw.addr)
56                         : [psw_wait] "Q" (psw_wait)
57                         : "cc", "memory");
58         } while (S390_lowcore.ext_int_code != EXT_IRQ_SERVICE_SIG);
59
60         S390_lowcore.external_new_psw = psw_ext_save;
61         __ctl_load(cr0.val, 0, 0);
62 }
63
64 int sclp_early_cmd(sclp_cmdw_t cmd, void *sccb)
65 {
66         unsigned long flags;
67         int rc;
68
69         flags = arch_local_irq_save();
70         rc = sclp_service_call(cmd, sccb);
71         if (rc)
72                 goto out;
73         sclp_early_wait_irq();
74 out:
75         arch_local_irq_restore(flags);
76         return rc;
77 }
78
79 struct write_sccb {
80         struct sccb_header header;
81         struct msg_buf msg;
82 } __packed;
83
84 /* Output multi-line text using SCLP Message interface. */
85 static void sclp_early_print_lm(const char *str, unsigned int len)
86 {
87         unsigned char *ptr, *end, ch;
88         unsigned int count, offset;
89         struct write_sccb *sccb;
90         struct msg_buf *msg;
91         struct mdb *mdb;
92         struct mto *mto;
93         struct go *go;
94
95         sccb = (struct write_sccb *) sclp_early_sccb;
96         end = (unsigned char *) sccb + EARLY_SCCB_SIZE - 1;
97         memset(sccb, 0, sizeof(*sccb));
98         ptr = (unsigned char *) &sccb->msg.mdb.mto;
99         offset = 0;
100         do {
101                 for (count = sizeof(*mto); offset < len; count++) {
102                         ch = str[offset++];
103                         if ((ch == 0x0a) || (ptr + count > end))
104                                 break;
105                         ptr[count] = _ascebc[ch];
106                 }
107                 mto = (struct mto *) ptr;
108                 memset(mto, 0, sizeof(*mto));
109                 mto->length = count;
110                 mto->type = 4;
111                 mto->line_type_flags = LNTPFLGS_ENDTEXT;
112                 ptr += count;
113         } while ((offset < len) && (ptr + sizeof(*mto) <= end));
114         len = ptr - (unsigned char *) sccb;
115         sccb->header.length = len - offsetof(struct write_sccb, header);
116         msg = &sccb->msg;
117         msg->header.type = EVTYP_MSG;
118         msg->header.length = len - offsetof(struct write_sccb, msg.header);
119         mdb = &msg->mdb;
120         mdb->header.type = 1;
121         mdb->header.tag = 0xD4C4C240;
122         mdb->header.revision_code = 1;
123         mdb->header.length = len - offsetof(struct write_sccb, msg.mdb.header);
124         go = &mdb->go;
125         go->length = sizeof(*go);
126         go->type = 1;
127         sclp_early_cmd(SCLP_CMDW_WRITE_EVENT_DATA, sccb);
128 }
129
130 struct vt220_sccb {
131         struct sccb_header header;
132         struct {
133                 struct evbuf_header header;
134                 char data[];
135         } msg;
136 } __packed;
137
138 /* Output multi-line text using SCLP VT220 interface. */
139 static void sclp_early_print_vt220(const char *str, unsigned int len)
140 {
141         struct vt220_sccb *sccb;
142
143         sccb = (struct vt220_sccb *) sclp_early_sccb;
144         if (sizeof(*sccb) + len >= EARLY_SCCB_SIZE)
145                 len = EARLY_SCCB_SIZE - sizeof(*sccb);
146         memset(sccb, 0, sizeof(*sccb));
147         memcpy(&sccb->msg.data, str, len);
148         sccb->header.length = sizeof(*sccb) + len;
149         sccb->msg.header.length = sizeof(sccb->msg) + len;
150         sccb->msg.header.type = EVTYP_VT220MSG;
151         sclp_early_cmd(SCLP_CMDW_WRITE_EVENT_DATA, sccb);
152 }
153
154 int sclp_early_set_event_mask(struct init_sccb *sccb,
155                               sccb_mask_t receive_mask,
156                               sccb_mask_t send_mask)
157 {
158 retry:
159         memset(sccb, 0, sizeof(*sccb));
160         sccb->header.length = sizeof(*sccb);
161         if (sclp_mask_compat_mode)
162                 sccb->mask_length = SCLP_MASK_SIZE_COMPAT;
163         else
164                 sccb->mask_length = sizeof(sccb_mask_t);
165         sccb_set_recv_mask(sccb, receive_mask);
166         sccb_set_send_mask(sccb, send_mask);
167         if (sclp_early_cmd(SCLP_CMDW_WRITE_EVENT_MASK, sccb))
168                 return -EIO;
169         if ((sccb->header.response_code == 0x74f0) && !sclp_mask_compat_mode) {
170                 sclp_mask_compat_mode = true;
171                 goto retry;
172         }
173         if (sccb->header.response_code != 0x20)
174                 return -EIO;
175         return 0;
176 }
177
178 unsigned int sclp_early_con_check_linemode(struct init_sccb *sccb)
179 {
180         if (!(sccb_get_sclp_send_mask(sccb) & EVTYP_OPCMD_MASK))
181                 return 0;
182         if (!(sccb_get_sclp_recv_mask(sccb) & (EVTYP_MSG_MASK | EVTYP_PMSGCMD_MASK)))
183                 return 0;
184         return 1;
185 }
186
187 unsigned int sclp_early_con_check_vt220(struct init_sccb *sccb)
188 {
189         if (sccb_get_sclp_send_mask(sccb) & EVTYP_VT220MSG_MASK)
190                 return 1;
191         return 0;
192 }
193
194 static int sclp_early_setup(int disable, int *have_linemode, int *have_vt220)
195 {
196         unsigned long receive_mask, send_mask;
197         struct init_sccb *sccb;
198         int rc;
199
200         BUILD_BUG_ON(sizeof(struct init_sccb) > PAGE_SIZE);
201
202         *have_linemode = *have_vt220 = 0;
203         sccb = (struct init_sccb *) sclp_early_sccb;
204         receive_mask = disable ? 0 : EVTYP_OPCMD_MASK;
205         send_mask = disable ? 0 : EVTYP_VT220MSG_MASK | EVTYP_MSG_MASK;
206         rc = sclp_early_set_event_mask(sccb, receive_mask, send_mask);
207         if (rc)
208                 return rc;
209         *have_linemode = sclp_early_con_check_linemode(sccb);
210         *have_vt220 = !!(sccb_get_send_mask(sccb) & EVTYP_VT220MSG_MASK);
211         return rc;
212 }
213
214 void sclp_early_set_buffer(void *sccb)
215 {
216         sclp_early_sccb = sccb;
217 }
218
219 /*
220  * Output one or more lines of text on the SCLP console (VT220 and /
221  * or line-mode).
222  */
223 void __sclp_early_printk(const char *str, unsigned int len)
224 {
225         int have_linemode, have_vt220;
226
227         if (sclp_init_state != sclp_init_state_uninitialized)
228                 return;
229         if (sclp_early_setup(0, &have_linemode, &have_vt220) != 0)
230                 return;
231         if (have_linemode)
232                 sclp_early_print_lm(str, len);
233         if (have_vt220)
234                 sclp_early_print_vt220(str, len);
235         sclp_early_setup(1, &have_linemode, &have_vt220);
236 }
237
238 void sclp_early_printk(const char *str)
239 {
240         __sclp_early_printk(str, strlen(str));
241 }
242
243 /*
244  * Use sclp_emergency_printk() to print a string when the system is in a
245  * state where regular console drivers cannot be assumed to work anymore.
246  *
247  * Callers must make sure that no concurrent SCLP requests are outstanding
248  * and all other CPUs are stopped, or at least disabled for external
249  * interrupts.
250  */
251 void sclp_emergency_printk(const char *str)
252 {
253         int have_linemode, have_vt220;
254         unsigned int len;
255
256         len = strlen(str);
257         /*
258          * Don't care about return values; if requests fail, just ignore and
259          * continue to have a rather high chance that anything is printed.
260          */
261         sclp_early_setup(0, &have_linemode, &have_vt220);
262         sclp_early_print_lm(str, len);
263         sclp_early_print_vt220(str, len);
264         sclp_early_setup(1, &have_linemode, &have_vt220);
265 }
266
267 /*
268  * We can't pass sclp_info_sccb to sclp_early_cmd() here directly,
269  * because it might not fulfil the requiremets for a SCLP communication buffer:
270  *   - lie below 2G in memory
271  *   - be page-aligned
272  * Therefore, we use the buffer sclp_early_sccb (which fulfils all those
273  * requirements) temporarily for communication and copy a received response
274  * back into the buffer sclp_info_sccb upon successful completion.
275  */
276 int __init sclp_early_read_info(void)
277 {
278         int i;
279         int length = test_facility(140) ? EXT_SCCB_READ_SCP : PAGE_SIZE;
280         struct read_info_sccb *sccb = (struct read_info_sccb *)sclp_early_sccb;
281         sclp_cmdw_t commands[] = {SCLP_CMDW_READ_SCP_INFO_FORCED,
282                                   SCLP_CMDW_READ_SCP_INFO};
283
284         for (i = 0; i < ARRAY_SIZE(commands); i++) {
285                 memset(sccb, 0, length);
286                 sccb->header.length = length;
287                 sccb->header.function_code = 0x80;
288                 sccb->header.control_mask[2] = 0x80;
289                 if (sclp_early_cmd(commands[i], sccb))
290                         break;
291                 if (sccb->header.response_code == 0x10) {
292                         memcpy(&sclp_info_sccb, sccb, length);
293                         sclp_info_sccb_valid = 1;
294                         return 0;
295                 }
296                 if (sccb->header.response_code != 0x1f0)
297                         break;
298         }
299         return -EIO;
300 }
301
302 struct read_info_sccb * __init sclp_early_get_info(void)
303 {
304         if (!sclp_info_sccb_valid)
305                 return NULL;
306
307         return &sclp_info_sccb;
308 }
309
310 int __init sclp_early_get_memsize(unsigned long *mem)
311 {
312         unsigned long rnmax;
313         unsigned long rnsize;
314         struct read_info_sccb *sccb = &sclp_info_sccb;
315
316         if (!sclp_info_sccb_valid)
317                 return -EIO;
318
319         rnmax = sccb->rnmax ? sccb->rnmax : sccb->rnmax2;
320         rnsize = sccb->rnsize ? sccb->rnsize : sccb->rnsize2;
321         rnsize <<= 20;
322         *mem = rnsize * rnmax;
323         return 0;
324 }
325
326 int __init sclp_early_get_hsa_size(unsigned long *hsa_size)
327 {
328         if (!sclp_info_sccb_valid)
329                 return -EIO;
330
331         *hsa_size = 0;
332         if (sclp_info_sccb.hsa_size)
333                 *hsa_size = (sclp_info_sccb.hsa_size - 1) * PAGE_SIZE;
334         return 0;
335 }
336
337 #define SCLP_STORAGE_INFO_FACILITY     0x0000400000000000UL
338
339 void __weak __init add_physmem_online_range(u64 start, u64 end) {}
340 int __init sclp_early_read_storage_info(void)
341 {
342         struct read_storage_sccb *sccb = (struct read_storage_sccb *)sclp_early_sccb;
343         int rc, id, max_id = 0;
344         unsigned long rn, rzm;
345         sclp_cmdw_t command;
346         u16 sn;
347
348         if (!sclp_info_sccb_valid)
349                 return -EIO;
350
351         if (!(sclp_info_sccb.facilities & SCLP_STORAGE_INFO_FACILITY))
352                 return -EOPNOTSUPP;
353
354         rzm = sclp_info_sccb.rnsize ?: sclp_info_sccb.rnsize2;
355         rzm <<= 20;
356
357         for (id = 0; id <= max_id; id++) {
358                 memset(sclp_early_sccb, 0, EARLY_SCCB_SIZE);
359                 sccb->header.length = EARLY_SCCB_SIZE;
360                 command = SCLP_CMDW_READ_STORAGE_INFO | (id << 8);
361                 rc = sclp_early_cmd(command, sccb);
362                 if (rc)
363                         goto fail;
364
365                 max_id = sccb->max_id;
366                 switch (sccb->header.response_code) {
367                 case 0x0010:
368                         for (sn = 0; sn < sccb->assigned; sn++) {
369                                 if (!sccb->entries[sn])
370                                         continue;
371                                 rn = sccb->entries[sn] >> 16;
372                                 add_physmem_online_range((rn - 1) * rzm, rn * rzm);
373                         }
374                         break;
375                 case 0x0310:
376                 case 0x0410:
377                         break;
378                 default:
379                         goto fail;
380                 }
381         }
382
383         return 0;
384 fail:
385         physmem_info.range_count = 0;
386         return -EIO;
387 }
This page took 0.054146 seconds and 4 git commands to generate.