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