xref: /openbmc/qemu/hw/s390x/sclp.c (revision bf616ce4)
1 /*
2  * SCLP Support
3  *
4  * Copyright IBM, Corp. 2012
5  *
6  * Authors:
7  *  Christian Borntraeger <borntraeger@de.ibm.com>
8  *  Heinz Graalfs <graalfs@linux.vnet.ibm.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or (at your
11  * option) any later version.  See the COPYING file in the top-level directory.
12  *
13  */
14 
15 #include "qemu/osdep.h"
16 #include "qemu/units.h"
17 #include "qapi/error.h"
18 #include "hw/boards.h"
19 #include "hw/s390x/sclp.h"
20 #include "hw/s390x/event-facility.h"
21 #include "hw/s390x/s390-pci-bus.h"
22 #include "hw/s390x/ipl.h"
23 #include "hw/s390x/cpu-topology.h"
24 
25 static inline SCLPDevice *get_sclp_device(void)
26 {
27     static SCLPDevice *sclp;
28 
29     if (!sclp) {
30         sclp = SCLP(object_resolve_path_type("", TYPE_SCLP, NULL));
31     }
32     return sclp;
33 }
34 
35 static inline bool sclp_command_code_valid(uint32_t code)
36 {
37     switch (code & SCLP_CMD_CODE_MASK) {
38     case SCLP_CMDW_READ_SCP_INFO:
39     case SCLP_CMDW_READ_SCP_INFO_FORCED:
40     case SCLP_CMDW_READ_CPU_INFO:
41     case SCLP_CMDW_CONFIGURE_IOA:
42     case SCLP_CMDW_DECONFIGURE_IOA:
43     case SCLP_CMD_READ_EVENT_DATA:
44     case SCLP_CMD_WRITE_EVENT_DATA:
45     case SCLP_CMD_WRITE_EVENT_MASK:
46         return true;
47     }
48     return false;
49 }
50 
51 static bool sccb_verify_boundary(uint64_t sccb_addr, uint16_t sccb_len,
52                                  uint32_t code)
53 {
54     uint64_t sccb_max_addr = sccb_addr + sccb_len - 1;
55     uint64_t sccb_boundary = (sccb_addr & TARGET_PAGE_MASK) + TARGET_PAGE_SIZE;
56 
57     switch (code & SCLP_CMD_CODE_MASK) {
58     case SCLP_CMDW_READ_SCP_INFO:
59     case SCLP_CMDW_READ_SCP_INFO_FORCED:
60     case SCLP_CMDW_READ_CPU_INFO:
61         /*
62          * An extended-length SCCB is only allowed for Read SCP/CPU Info and
63          * is allowed to exceed the 4k boundary. The respective commands will
64          * set the length field to the required length if an insufficient
65          * SCCB length is provided.
66          */
67         if (s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB)) {
68             return true;
69         }
70         /* fallthrough */
71     default:
72         if (sccb_max_addr < sccb_boundary) {
73             return true;
74         }
75     }
76 
77     return false;
78 }
79 
80 static void prepare_cpu_entries(MachineState *ms, CPUEntry *entry, int *count)
81 {
82     uint8_t features[SCCB_CPU_FEATURE_LEN] = { 0 };
83     int i;
84 
85     s390_get_feat_block(S390_FEAT_TYPE_SCLP_CPU, features);
86     for (i = 0, *count = 0; i < ms->possible_cpus->len; i++) {
87         if (!ms->possible_cpus->cpus[i].cpu) {
88             continue;
89         }
90         entry[*count].address = ms->possible_cpus->cpus[i].arch_id;
91         entry[*count].type = 0;
92         memcpy(entry[*count].features, features, sizeof(features));
93         (*count)++;
94     }
95 }
96 
97 #define SCCB_REQ_LEN(s, max_cpus) (sizeof(s) + max_cpus * sizeof(CPUEntry))
98 
99 static inline bool ext_len_sccb_supported(SCCBHeader header)
100 {
101     return s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB) &&
102            header.control_mask[2] & SCLP_VARIABLE_LENGTH_RESPONSE;
103 }
104 
105 /* Provide information about the configuration, CPUs and storage */
106 static void read_SCP_info(SCLPDevice *sclp, SCCB *sccb)
107 {
108     ReadInfo *read_info = (ReadInfo *) sccb;
109     MachineState *machine = MACHINE(qdev_get_machine());
110     int cpu_count;
111     int rnsize, rnmax;
112     IplParameterBlock *ipib = s390_ipl_get_iplb();
113     int required_len = SCCB_REQ_LEN(ReadInfo, machine->possible_cpus->len);
114     int offset_cpu = s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB) ?
115                      offsetof(ReadInfo, entries) :
116                      SCLP_READ_SCP_INFO_FIXED_CPU_OFFSET;
117     CPUEntry *entries_start = (void *)sccb + offset_cpu;
118 
119     if (be16_to_cpu(sccb->h.length) < required_len) {
120         if (ext_len_sccb_supported(sccb->h)) {
121             sccb->h.length = cpu_to_be16(required_len);
122         }
123         sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
124         return;
125     }
126 
127     if (s390_has_topology()) {
128         read_info->stsi_parm = SCLP_READ_SCP_INFO_MNEST;
129     }
130 
131     /* CPU information */
132     prepare_cpu_entries(machine, entries_start, &cpu_count);
133     read_info->entries_cpu = cpu_to_be16(cpu_count);
134     read_info->offset_cpu = cpu_to_be16(offset_cpu);
135     read_info->highest_cpu = cpu_to_be16(machine->smp.max_cpus - 1);
136 
137     read_info->ibc_val = cpu_to_be32(s390_get_ibc_val());
138 
139     /* Configuration Characteristic (Extension) */
140     s390_get_feat_block(S390_FEAT_TYPE_SCLP_CONF_CHAR,
141                          read_info->conf_char);
142     s390_get_feat_block(S390_FEAT_TYPE_SCLP_CONF_CHAR_EXT,
143                          read_info->conf_char_ext);
144 
145     if (s390_has_feat(S390_FEAT_EXTENDED_LENGTH_SCCB)) {
146         s390_get_feat_block(S390_FEAT_TYPE_SCLP_FAC134,
147                             &read_info->fac134);
148     }
149 
150     read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO |
151                                         SCLP_HAS_IOA_RECONFIG);
152 
153     read_info->mha_pow = s390_get_mha_pow();
154     read_info->hmfai = cpu_to_be32(s390_get_hmfai());
155 
156     rnsize = 1 << (sclp->increment_size - 20);
157     if (rnsize <= 128) {
158         read_info->rnsize = rnsize;
159     } else {
160         read_info->rnsize = 0;
161         read_info->rnsize2 = cpu_to_be32(rnsize);
162     }
163 
164     /* we don't support standby memory, maxram_size is never exposed */
165     rnmax = machine->ram_size >> sclp->increment_size;
166     if (rnmax < 0x10000) {
167         read_info->rnmax = cpu_to_be16(rnmax);
168     } else {
169         read_info->rnmax = cpu_to_be16(0);
170         read_info->rnmax2 = cpu_to_be64(rnmax);
171     }
172 
173     if (ipib && ipib->flags & DIAG308_FLAGS_LP_VALID) {
174         memcpy(&read_info->loadparm, &ipib->loadparm,
175                sizeof(read_info->loadparm));
176     } else {
177         s390_ipl_set_loadparm(read_info->loadparm);
178     }
179 
180     sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
181 }
182 
183 /* Provide information about the CPU */
184 static void sclp_read_cpu_info(SCLPDevice *sclp, SCCB *sccb)
185 {
186     MachineState *machine = MACHINE(qdev_get_machine());
187     ReadCpuInfo *cpu_info = (ReadCpuInfo *) sccb;
188     int cpu_count;
189     int required_len = SCCB_REQ_LEN(ReadCpuInfo, machine->possible_cpus->len);
190 
191     if (be16_to_cpu(sccb->h.length) < required_len) {
192         if (ext_len_sccb_supported(sccb->h)) {
193             sccb->h.length = cpu_to_be16(required_len);
194         }
195         sccb->h.response_code = cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH);
196         return;
197     }
198 
199     prepare_cpu_entries(machine, cpu_info->entries, &cpu_count);
200     cpu_info->nr_configured = cpu_to_be16(cpu_count);
201     cpu_info->offset_configured = cpu_to_be16(offsetof(ReadCpuInfo, entries));
202     cpu_info->nr_standby = cpu_to_be16(0);
203 
204     /* The standby offset is 16-byte for each CPU */
205     cpu_info->offset_standby = cpu_to_be16(cpu_info->offset_configured
206         + cpu_info->nr_configured*sizeof(CPUEntry));
207 
208 
209     sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
210 }
211 
212 static void sclp_configure_io_adapter(SCLPDevice *sclp, SCCB *sccb,
213                                       bool configure)
214 {
215     int rc;
216 
217     if (be16_to_cpu(sccb->h.length) < 16) {
218         rc = SCLP_RC_INSUFFICIENT_SCCB_LENGTH;
219         goto out_err;
220     }
221 
222     switch (((IoaCfgSccb *)sccb)->atype) {
223     case SCLP_RECONFIG_PCI_ATYPE:
224         if (s390_has_feat(S390_FEAT_ZPCI)) {
225             if (configure) {
226                 s390_pci_sclp_configure(sccb);
227             } else {
228                 s390_pci_sclp_deconfigure(sccb);
229             }
230             return;
231         }
232         /* fallthrough */
233     default:
234         rc = SCLP_RC_ADAPTER_TYPE_NOT_RECOGNIZED;
235     }
236 
237  out_err:
238     sccb->h.response_code = cpu_to_be16(rc);
239 }
240 
241 static void sclp_execute(SCLPDevice *sclp, SCCB *sccb, uint32_t code)
242 {
243     SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
244     SCLPEventFacility *ef = sclp->event_facility;
245     SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
246 
247     switch (code & SCLP_CMD_CODE_MASK) {
248     case SCLP_CMDW_READ_SCP_INFO:
249     case SCLP_CMDW_READ_SCP_INFO_FORCED:
250         sclp_c->read_SCP_info(sclp, sccb);
251         break;
252     case SCLP_CMDW_READ_CPU_INFO:
253         sclp_c->read_cpu_info(sclp, sccb);
254         break;
255     case SCLP_CMDW_CONFIGURE_IOA:
256         sclp_configure_io_adapter(sclp, sccb, true);
257         break;
258     case SCLP_CMDW_DECONFIGURE_IOA:
259         sclp_configure_io_adapter(sclp, sccb, false);
260         break;
261     default:
262         efc->command_handler(ef, sccb, code);
263         break;
264     }
265 }
266 
267 /*
268  * We only need the address to have something valid for the
269  * service_interrupt call.
270  */
271 #define SCLP_PV_DUMMY_ADDR 0x4000
272 int sclp_service_call_protected(S390CPU *cpu, uint64_t sccb, uint32_t code)
273 {
274     CPUS390XState *env = &cpu->env;
275     SCLPDevice *sclp = get_sclp_device();
276     SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
277     SCCBHeader header;
278     g_autofree SCCB *work_sccb = NULL;
279 
280     s390_cpu_pv_mem_read(env_archcpu(env), 0, &header, sizeof(SCCBHeader));
281 
282     work_sccb = g_malloc0(be16_to_cpu(header.length));
283     s390_cpu_pv_mem_read(env_archcpu(env), 0, work_sccb,
284                          be16_to_cpu(header.length));
285 
286     if (!sclp_command_code_valid(code)) {
287         work_sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
288         goto out_write;
289     }
290 
291     sclp_c->execute(sclp, work_sccb, code);
292 out_write:
293     s390_cpu_pv_mem_write(env_archcpu(env), 0, work_sccb,
294                           be16_to_cpu(work_sccb->h.length));
295     sclp_c->service_interrupt(sclp, SCLP_PV_DUMMY_ADDR);
296     return 0;
297 }
298 
299 int sclp_service_call(S390CPU *cpu, uint64_t sccb, uint32_t code)
300 {
301     CPUS390XState *env = &cpu->env;
302     SCLPDevice *sclp = get_sclp_device();
303     SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
304     SCCBHeader header;
305     g_autofree SCCB *work_sccb = NULL;
306 
307     /* first some basic checks on program checks */
308     if (env->psw.mask & PSW_MASK_PSTATE) {
309         return -PGM_PRIVILEGED;
310     }
311     if (cpu_physical_memory_is_io(sccb)) {
312         return -PGM_ADDRESSING;
313     }
314     if ((sccb & ~0x1fffUL) == 0 || (sccb & ~0x1fffUL) == env->psa
315         || (sccb & ~0x7ffffff8UL) != 0) {
316         return -PGM_SPECIFICATION;
317     }
318 
319     /* the header contains the actual length of the sccb */
320     cpu_physical_memory_read(sccb, &header, sizeof(SCCBHeader));
321 
322     /* Valid sccb sizes */
323     if (be16_to_cpu(header.length) < sizeof(SCCBHeader)) {
324         return -PGM_SPECIFICATION;
325     }
326 
327     /*
328      * we want to work on a private copy of the sccb, to prevent guests
329      * from playing dirty tricks by modifying the memory content after
330      * the host has checked the values
331      */
332     work_sccb = g_malloc0(be16_to_cpu(header.length));
333     cpu_physical_memory_read(sccb, work_sccb, be16_to_cpu(header.length));
334 
335     if (!sclp_command_code_valid(code)) {
336         work_sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
337         goto out_write;
338     }
339 
340     if (!sccb_verify_boundary(sccb, be16_to_cpu(work_sccb->h.length), code)) {
341         work_sccb->h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION);
342         goto out_write;
343     }
344 
345     sclp_c->execute(sclp, work_sccb, code);
346 out_write:
347     cpu_physical_memory_write(sccb, work_sccb,
348                               be16_to_cpu(work_sccb->h.length));
349 
350     sclp_c->service_interrupt(sclp, sccb);
351 
352     return 0;
353 }
354 
355 static void service_interrupt(SCLPDevice *sclp, uint32_t sccb)
356 {
357     SCLPEventFacility *ef = sclp->event_facility;
358     SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
359 
360     uint32_t param = sccb & ~3;
361 
362     /* Indicate whether an event is still pending */
363     param |= efc->event_pending(ef) ? 1 : 0;
364 
365     if (!param) {
366         /* No need to send an interrupt, there's nothing to be notified about */
367         return;
368     }
369     s390_sclp_extint(param);
370 }
371 
372 void sclp_service_interrupt(uint32_t sccb)
373 {
374     SCLPDevice *sclp = get_sclp_device();
375     SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
376 
377     sclp_c->service_interrupt(sclp, sccb);
378 }
379 
380 /* qemu object creation and initialization functions */
381 
382 void s390_sclp_init(void)
383 {
384     Object *new = object_new(TYPE_SCLP);
385 
386     object_property_add_child(qdev_get_machine(), TYPE_SCLP, new);
387     object_unref(new);
388     qdev_realize(DEVICE(new), NULL, &error_fatal);
389 }
390 
391 static void sclp_realize(DeviceState *dev, Error **errp)
392 {
393     MachineState *machine = MACHINE(qdev_get_machine());
394     SCLPDevice *sclp = SCLP(dev);
395     uint64_t hw_limit;
396     int ret;
397 
398     /*
399      * qdev_device_add searches the sysbus for TYPE_SCLP_EVENTS_BUS. As long
400      * as we can't find a fitting bus via the qom tree, we have to add the
401      * event facility to the sysbus, so e.g. a sclp console can be created.
402      */
403     if (!sysbus_realize(SYS_BUS_DEVICE(sclp->event_facility), errp)) {
404         return;
405     }
406 
407     ret = s390_set_memory_limit(machine->maxram_size, &hw_limit);
408     if (ret == -E2BIG) {
409         error_setg(errp, "host supports a maximum of %" PRIu64 " GB",
410                    hw_limit / GiB);
411     } else if (ret) {
412         error_setg(errp, "setting the guest size failed");
413     }
414 }
415 
416 static void sclp_memory_init(SCLPDevice *sclp)
417 {
418     MachineState *machine = MACHINE(qdev_get_machine());
419     MachineClass *machine_class = MACHINE_GET_CLASS(qdev_get_machine());
420     ram_addr_t initial_mem = machine->ram_size;
421     int increment_size = 20;
422 
423     /* The storage increment size is a multiple of 1M and is a power of 2.
424      * For some machine types, the number of storage increments must be
425      * MAX_STORAGE_INCREMENTS or fewer.
426      * The variable 'increment_size' is an exponent of 2 that can be
427      * used to calculate the size (in bytes) of an increment. */
428     while (machine_class->fixup_ram_size != NULL &&
429            (initial_mem >> increment_size) > MAX_STORAGE_INCREMENTS) {
430         increment_size++;
431     }
432     sclp->increment_size = increment_size;
433 }
434 
435 static void sclp_init(Object *obj)
436 {
437     SCLPDevice *sclp = SCLP(obj);
438     Object *new;
439 
440     new = object_new(TYPE_SCLP_EVENT_FACILITY);
441     object_property_add_child(obj, TYPE_SCLP_EVENT_FACILITY, new);
442     object_unref(new);
443     sclp->event_facility = EVENT_FACILITY(new);
444 
445     sclp_memory_init(sclp);
446 }
447 
448 static void sclp_class_init(ObjectClass *oc, void *data)
449 {
450     SCLPDeviceClass *sc = SCLP_CLASS(oc);
451     DeviceClass *dc = DEVICE_CLASS(oc);
452 
453     dc->desc = "SCLP (Service-Call Logical Processor)";
454     dc->realize = sclp_realize;
455     dc->hotpluggable = false;
456     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
457     /*
458      * Reason: Creates TYPE_SCLP_EVENT_FACILITY in sclp_init
459      * which is a non-pluggable sysbus device
460      */
461     dc->user_creatable = false;
462 
463     sc->read_SCP_info = read_SCP_info;
464     sc->read_cpu_info = sclp_read_cpu_info;
465     sc->execute = sclp_execute;
466     sc->service_interrupt = service_interrupt;
467 }
468 
469 static const TypeInfo sclp_info = {
470     .name = TYPE_SCLP,
471     .parent = TYPE_DEVICE,
472     .instance_init = sclp_init,
473     .instance_size = sizeof(SCLPDevice),
474     .class_init = sclp_class_init,
475     .class_size = sizeof(SCLPDeviceClass),
476 };
477 
478 static void register_types(void)
479 {
480     type_register_static(&sclp_info);
481 }
482 type_init(register_types);
483