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