block: Add bdrv_supports_compressed_writes()
[qemu.git] / hw / s390x / sclp.c
bloba0ce444b4bf2142732c5c20257d8c2b50f644b79
1 /*
2 * SCLP Support
4 * Copyright IBM, Corp. 2012
6 * Authors:
7 * Christian Borntraeger <borntraeger@de.ibm.com>
8 * Heinz Graalfs <graalfs@linux.vnet.ibm.com>
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.
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"
26 static inline SCLPDevice *get_sclp_device(void)
28 static SCLPDevice *sclp;
30 if (!sclp) {
31 sclp = SCLP(object_resolve_path_type("", TYPE_SCLP, NULL));
33 return sclp;
36 static inline bool sclp_command_code_valid(uint32_t code)
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;
49 return false;
52 static void prepare_cpu_entries(SCLPDevice *sclp, CPUEntry *entry, int *count)
54 MachineState *ms = MACHINE(qdev_get_machine());
55 uint8_t features[SCCB_CPU_FEATURE_LEN] = { 0 };
56 int i;
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;
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)++;
70 /* Provide information about the configuration, CPUs and storage */
71 static void read_SCP_info(SCLPDevice *sclp, SCCB *sccb)
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();
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);
85 read_info->ibc_val = cpu_to_be32(s390_get_ibc_val());
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;
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);
99 read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO |
100 SCLP_HAS_IOA_RECONFIG);
102 read_info->mha_pow = s390_get_mha_pow();
103 read_info->hmfai = cpu_to_be32(s390_get_hmfai());
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);
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);
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);
129 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
132 /* Provide information about the CPU */
133 static void sclp_read_cpu_info(SCLPDevice *sclp, SCCB *sccb)
135 ReadCpuInfo *cpu_info = (ReadCpuInfo *) sccb;
136 int cpu_count;
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);
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;
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));
154 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
157 static void sclp_configure_io_adapter(SCLPDevice *sclp, SCCB *sccb,
158 bool configure)
160 int rc;
162 if (be16_to_cpu(sccb->h.length) < 16) {
163 rc = SCLP_RC_INSUFFICIENT_SCCB_LENGTH;
164 goto out_err;
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);
175 return;
177 /* fallthrough */
178 default:
179 rc = SCLP_RC_ADAPTER_TYPE_NOT_RECOGNIZED;
182 out_err:
183 sccb->h.response_code = cpu_to_be16(rc);
186 static void sclp_execute(SCLPDevice *sclp, SCCB *sccb, uint32_t code)
188 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
189 SCLPEventFacility *ef = sclp->event_facility;
190 SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
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;
213 * We only need the address to have something valid for the
214 * service_interrupt call.
216 #define SCLP_PV_DUMMY_ADDR 0x4000
217 int sclp_service_call_protected(CPUS390XState *env, uint64_t sccb,
218 uint32_t code)
220 SCLPDevice *sclp = get_sclp_device();
221 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
222 SCCB work_sccb;
223 hwaddr sccb_len = sizeof(SCCB);
225 s390_cpu_pv_mem_read(env_archcpu(env), 0, &work_sccb, sccb_len);
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;
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;
240 int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code)
242 SCLPDevice *sclp = get_sclp_device();
243 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
244 SCCB work_sccb;
246 hwaddr sccb_len = sizeof(SCCB);
248 /* first some basic checks on program checks */
249 if (env->psw.mask & PSW_MASK_PSTATE) {
250 return -PGM_PRIVILEGED;
252 if (cpu_physical_memory_is_io(sccb)) {
253 return -PGM_ADDRESSING;
255 if ((sccb & ~0x1fffUL) == 0 || (sccb & ~0x1fffUL) == env->psa
256 || (sccb & ~0x7ffffff8UL) != 0) {
257 return -PGM_SPECIFICATION;
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
265 cpu_physical_memory_read(sccb, &work_sccb, sccb_len);
267 /* Valid sccb sizes */
268 if (be16_to_cpu(work_sccb.h.length) < sizeof(SCCBHeader)) {
269 return -PGM_SPECIFICATION;
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;
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;
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));
287 sclp_c->service_interrupt(sclp, sccb);
289 return 0;
292 static void service_interrupt(SCLPDevice *sclp, uint32_t sccb)
294 SCLPEventFacility *ef = sclp->event_facility;
295 SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
297 uint32_t param = sccb & ~3;
299 /* Indicate whether an event is still pending */
300 param |= efc->event_pending(ef) ? 1 : 0;
302 if (!param) {
303 /* No need to send an interrupt, there's nothing to be notified about */
304 return;
306 s390_sclp_extint(param);
309 void sclp_service_interrupt(uint32_t sccb)
311 SCLPDevice *sclp = get_sclp_device();
312 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
314 sclp_c->service_interrupt(sclp, sccb);
317 /* qemu object creation and initialization functions */
319 void s390_sclp_init(void)
321 Object *new = object_new(TYPE_SCLP);
323 object_property_add_child(qdev_get_machine(), TYPE_SCLP, new);
324 object_unref(new);
325 qdev_realize(DEVICE(new), NULL, &error_fatal);
328 static void sclp_realize(DeviceState *dev, Error **errp)
330 MachineState *machine = MACHINE(qdev_get_machine());
331 SCLPDevice *sclp = SCLP(dev);
332 uint64_t hw_limit;
333 int ret;
336 * qdev_device_add searches the sysbus for TYPE_SCLP_EVENTS_BUS. As long
337 * as we can't find a fitting bus via the qom tree, we have to add the
338 * event facility to the sysbus, so e.g. a sclp console can be created.
340 if (!sysbus_realize(SYS_BUS_DEVICE(sclp->event_facility), errp)) {
341 return;
344 ret = s390_set_memory_limit(machine->maxram_size, &hw_limit);
345 if (ret == -E2BIG) {
346 error_setg(errp, "host supports a maximum of %" PRIu64 " GB",
347 hw_limit / GiB);
348 } else if (ret) {
349 error_setg(errp, "setting the guest size failed");
353 static void sclp_memory_init(SCLPDevice *sclp)
355 MachineState *machine = MACHINE(qdev_get_machine());
356 MachineClass *machine_class = MACHINE_GET_CLASS(qdev_get_machine());
357 ram_addr_t initial_mem = machine->ram_size;
358 int increment_size = 20;
360 /* The storage increment size is a multiple of 1M and is a power of 2.
361 * For some machine types, the number of storage increments must be
362 * MAX_STORAGE_INCREMENTS or fewer.
363 * The variable 'increment_size' is an exponent of 2 that can be
364 * used to calculate the size (in bytes) of an increment. */
365 while (machine_class->fixup_ram_size != NULL &&
366 (initial_mem >> increment_size) > MAX_STORAGE_INCREMENTS) {
367 increment_size++;
369 sclp->increment_size = increment_size;
372 static void sclp_init(Object *obj)
374 SCLPDevice *sclp = SCLP(obj);
375 Object *new;
377 new = object_new(TYPE_SCLP_EVENT_FACILITY);
378 object_property_add_child(obj, TYPE_SCLP_EVENT_FACILITY, new);
379 object_unref(new);
380 sclp->event_facility = EVENT_FACILITY(new);
382 sclp_memory_init(sclp);
385 static void sclp_class_init(ObjectClass *oc, void *data)
387 SCLPDeviceClass *sc = SCLP_CLASS(oc);
388 DeviceClass *dc = DEVICE_CLASS(oc);
390 dc->desc = "SCLP (Service-Call Logical Processor)";
391 dc->realize = sclp_realize;
392 dc->hotpluggable = false;
393 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
395 * Reason: Creates TYPE_SCLP_EVENT_FACILITY in sclp_init
396 * which is a non-pluggable sysbus device
398 dc->user_creatable = false;
400 sc->read_SCP_info = read_SCP_info;
401 sc->read_cpu_info = sclp_read_cpu_info;
402 sc->execute = sclp_execute;
403 sc->service_interrupt = service_interrupt;
406 static TypeInfo sclp_info = {
407 .name = TYPE_SCLP,
408 .parent = TYPE_DEVICE,
409 .instance_init = sclp_init,
410 .instance_size = sizeof(SCLPDevice),
411 .class_init = sclp_class_init,
412 .class_size = sizeof(SCLPDeviceClass),
415 static void register_types(void)
417 type_register_static(&sclp_info);
419 type_init(register_types);