4 * Copyright IBM, Corp. 2012
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"
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
;
31 sclp
= SCLP(object_resolve_path_type("", TYPE_SCLP
, NULL
));
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
:
52 static bool sccb_verify_boundary(uint64_t sccb_addr
, uint16_t sccb_len
)
54 uint64_t sccb_max_addr
= sccb_addr
+ sccb_len
- 1;
55 uint64_t sccb_boundary
= (sccb_addr
& PAGE_MASK
) + PAGE_SIZE
;
57 if (sccb_max_addr
< sccb_boundary
) {
64 static void prepare_cpu_entries(MachineState
*ms
, CPUEntry
*entry
, int *count
)
66 uint8_t features
[SCCB_CPU_FEATURE_LEN
] = { 0 };
69 s390_get_feat_block(S390_FEAT_TYPE_SCLP_CPU
, features
);
70 for (i
= 0, *count
= 0; i
< ms
->possible_cpus
->len
; i
++) {
71 if (!ms
->possible_cpus
->cpus
[i
].cpu
) {
74 entry
[*count
].address
= ms
->possible_cpus
->cpus
[i
].arch_id
;
75 entry
[*count
].type
= 0;
76 memcpy(entry
[*count
].features
, features
, sizeof(features
));
81 #define SCCB_REQ_LEN(s, max_cpus) (sizeof(s) + max_cpus * sizeof(CPUEntry))
83 /* Provide information about the configuration, CPUs and storage */
84 static void read_SCP_info(SCLPDevice
*sclp
, SCCB
*sccb
)
86 ReadInfo
*read_info
= (ReadInfo
*) sccb
;
87 MachineState
*machine
= MACHINE(qdev_get_machine());
90 IplParameterBlock
*ipib
= s390_ipl_get_iplb();
91 int required_len
= SCCB_REQ_LEN(ReadInfo
, machine
->possible_cpus
->len
);
92 int offset_cpu
= offsetof(ReadInfo
, entries
);
93 CPUEntry
*entries_start
= (void *)sccb
+ offset_cpu
;
95 if (be16_to_cpu(sccb
->h
.length
) < required_len
) {
96 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH
);
100 /* CPU information */
101 prepare_cpu_entries(machine
, entries_start
, &cpu_count
);
102 read_info
->entries_cpu
= cpu_to_be16(cpu_count
);
103 read_info
->offset_cpu
= cpu_to_be16(offset_cpu
);
104 read_info
->highest_cpu
= cpu_to_be16(machine
->smp
.max_cpus
- 1);
106 read_info
->ibc_val
= cpu_to_be32(s390_get_ibc_val());
108 /* Configuration Characteristic (Extension) */
109 s390_get_feat_block(S390_FEAT_TYPE_SCLP_CONF_CHAR
,
110 read_info
->conf_char
);
111 s390_get_feat_block(S390_FEAT_TYPE_SCLP_CONF_CHAR_EXT
,
112 read_info
->conf_char_ext
);
114 read_info
->facilities
= cpu_to_be64(SCLP_HAS_CPU_INFO
|
115 SCLP_HAS_IOA_RECONFIG
);
117 read_info
->mha_pow
= s390_get_mha_pow();
118 read_info
->hmfai
= cpu_to_be32(s390_get_hmfai());
120 rnsize
= 1 << (sclp
->increment_size
- 20);
122 read_info
->rnsize
= rnsize
;
124 read_info
->rnsize
= 0;
125 read_info
->rnsize2
= cpu_to_be32(rnsize
);
128 /* we don't support standby memory, maxram_size is never exposed */
129 rnmax
= machine
->ram_size
>> sclp
->increment_size
;
130 if (rnmax
< 0x10000) {
131 read_info
->rnmax
= cpu_to_be16(rnmax
);
133 read_info
->rnmax
= cpu_to_be16(0);
134 read_info
->rnmax2
= cpu_to_be64(rnmax
);
137 if (ipib
&& ipib
->flags
& DIAG308_FLAGS_LP_VALID
) {
138 memcpy(&read_info
->loadparm
, &ipib
->loadparm
,
139 sizeof(read_info
->loadparm
));
141 s390_ipl_set_loadparm(read_info
->loadparm
);
144 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION
);
147 /* Provide information about the CPU */
148 static void sclp_read_cpu_info(SCLPDevice
*sclp
, SCCB
*sccb
)
150 MachineState
*machine
= MACHINE(qdev_get_machine());
151 ReadCpuInfo
*cpu_info
= (ReadCpuInfo
*) sccb
;
153 int required_len
= SCCB_REQ_LEN(ReadCpuInfo
, machine
->possible_cpus
->len
);
155 if (be16_to_cpu(sccb
->h
.length
) < required_len
) {
156 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INSUFFICIENT_SCCB_LENGTH
);
160 prepare_cpu_entries(machine
, cpu_info
->entries
, &cpu_count
);
161 cpu_info
->nr_configured
= cpu_to_be16(cpu_count
);
162 cpu_info
->offset_configured
= cpu_to_be16(offsetof(ReadCpuInfo
, entries
));
163 cpu_info
->nr_standby
= cpu_to_be16(0);
165 /* The standby offset is 16-byte for each CPU */
166 cpu_info
->offset_standby
= cpu_to_be16(cpu_info
->offset_configured
167 + cpu_info
->nr_configured
*sizeof(CPUEntry
));
170 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION
);
173 static void sclp_configure_io_adapter(SCLPDevice
*sclp
, SCCB
*sccb
,
178 if (be16_to_cpu(sccb
->h
.length
) < 16) {
179 rc
= SCLP_RC_INSUFFICIENT_SCCB_LENGTH
;
183 switch (((IoaCfgSccb
*)sccb
)->atype
) {
184 case SCLP_RECONFIG_PCI_ATYPE
:
185 if (s390_has_feat(S390_FEAT_ZPCI
)) {
187 s390_pci_sclp_configure(sccb
);
189 s390_pci_sclp_deconfigure(sccb
);
195 rc
= SCLP_RC_ADAPTER_TYPE_NOT_RECOGNIZED
;
199 sccb
->h
.response_code
= cpu_to_be16(rc
);
202 static void sclp_execute(SCLPDevice
*sclp
, SCCB
*sccb
, uint32_t code
)
204 SCLPDeviceClass
*sclp_c
= SCLP_GET_CLASS(sclp
);
205 SCLPEventFacility
*ef
= sclp
->event_facility
;
206 SCLPEventFacilityClass
*efc
= EVENT_FACILITY_GET_CLASS(ef
);
208 switch (code
& SCLP_CMD_CODE_MASK
) {
209 case SCLP_CMDW_READ_SCP_INFO
:
210 case SCLP_CMDW_READ_SCP_INFO_FORCED
:
211 sclp_c
->read_SCP_info(sclp
, sccb
);
213 case SCLP_CMDW_READ_CPU_INFO
:
214 sclp_c
->read_cpu_info(sclp
, sccb
);
216 case SCLP_CMDW_CONFIGURE_IOA
:
217 sclp_configure_io_adapter(sclp
, sccb
, true);
219 case SCLP_CMDW_DECONFIGURE_IOA
:
220 sclp_configure_io_adapter(sclp
, sccb
, false);
223 efc
->command_handler(ef
, sccb
, code
);
229 * We only need the address to have something valid for the
230 * service_interrupt call.
232 #define SCLP_PV_DUMMY_ADDR 0x4000
233 int sclp_service_call_protected(CPUS390XState
*env
, uint64_t sccb
,
236 SCLPDevice
*sclp
= get_sclp_device();
237 SCLPDeviceClass
*sclp_c
= SCLP_GET_CLASS(sclp
);
239 g_autofree SCCB
*work_sccb
= NULL
;
241 s390_cpu_pv_mem_read(env_archcpu(env
), 0, &header
, sizeof(SCCBHeader
));
243 work_sccb
= g_malloc0(be16_to_cpu(header
.length
));
244 s390_cpu_pv_mem_read(env_archcpu(env
), 0, work_sccb
,
245 be16_to_cpu(header
.length
));
247 if (!sclp_command_code_valid(code
)) {
248 work_sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND
);
252 if (!sccb_verify_boundary(sccb
, be16_to_cpu(work_sccb
->h
.length
))) {
253 work_sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION
);
257 sclp_c
->execute(sclp
, work_sccb
, code
);
259 s390_cpu_pv_mem_write(env_archcpu(env
), 0, work_sccb
,
260 be16_to_cpu(work_sccb
->h
.length
));
261 sclp_c
->service_interrupt(sclp
, SCLP_PV_DUMMY_ADDR
);
265 int sclp_service_call(CPUS390XState
*env
, uint64_t sccb
, uint32_t code
)
267 SCLPDevice
*sclp
= get_sclp_device();
268 SCLPDeviceClass
*sclp_c
= SCLP_GET_CLASS(sclp
);
270 g_autofree SCCB
*work_sccb
= NULL
;
272 /* first some basic checks on program checks */
273 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
274 return -PGM_PRIVILEGED
;
276 if (cpu_physical_memory_is_io(sccb
)) {
277 return -PGM_ADDRESSING
;
279 if ((sccb
& ~0x1fffUL
) == 0 || (sccb
& ~0x1fffUL
) == env
->psa
280 || (sccb
& ~0x7ffffff8UL
) != 0) {
281 return -PGM_SPECIFICATION
;
284 /* the header contains the actual length of the sccb */
285 cpu_physical_memory_read(sccb
, &header
, sizeof(SCCBHeader
));
287 /* Valid sccb sizes */
288 if (be16_to_cpu(header
.length
) < sizeof(SCCBHeader
)) {
289 return -PGM_SPECIFICATION
;
293 * we want to work on a private copy of the sccb, to prevent guests
294 * from playing dirty tricks by modifying the memory content after
295 * the host has checked the values
297 work_sccb
= g_malloc0(be16_to_cpu(header
.length
));
298 cpu_physical_memory_read(sccb
, work_sccb
, be16_to_cpu(header
.length
));
300 if (!sclp_command_code_valid(code
)) {
301 work_sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND
);
305 if (!sccb_verify_boundary(sccb
, be16_to_cpu(work_sccb
->h
.length
))) {
306 work_sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION
);
310 sclp_c
->execute(sclp
, work_sccb
, code
);
312 cpu_physical_memory_write(sccb
, work_sccb
,
313 be16_to_cpu(work_sccb
->h
.length
));
315 sclp_c
->service_interrupt(sclp
, sccb
);
320 static void service_interrupt(SCLPDevice
*sclp
, uint32_t sccb
)
322 SCLPEventFacility
*ef
= sclp
->event_facility
;
323 SCLPEventFacilityClass
*efc
= EVENT_FACILITY_GET_CLASS(ef
);
325 uint32_t param
= sccb
& ~3;
327 /* Indicate whether an event is still pending */
328 param
|= efc
->event_pending(ef
) ? 1 : 0;
331 /* No need to send an interrupt, there's nothing to be notified about */
334 s390_sclp_extint(param
);
337 void sclp_service_interrupt(uint32_t sccb
)
339 SCLPDevice
*sclp
= get_sclp_device();
340 SCLPDeviceClass
*sclp_c
= SCLP_GET_CLASS(sclp
);
342 sclp_c
->service_interrupt(sclp
, sccb
);
345 /* qemu object creation and initialization functions */
347 void s390_sclp_init(void)
349 Object
*new = object_new(TYPE_SCLP
);
351 object_property_add_child(qdev_get_machine(), TYPE_SCLP
, new);
353 qdev_realize(DEVICE(new), NULL
, &error_fatal
);
356 static void sclp_realize(DeviceState
*dev
, Error
**errp
)
358 MachineState
*machine
= MACHINE(qdev_get_machine());
359 SCLPDevice
*sclp
= SCLP(dev
);
364 * qdev_device_add searches the sysbus for TYPE_SCLP_EVENTS_BUS. As long
365 * as we can't find a fitting bus via the qom tree, we have to add the
366 * event facility to the sysbus, so e.g. a sclp console can be created.
368 if (!sysbus_realize(SYS_BUS_DEVICE(sclp
->event_facility
), errp
)) {
372 ret
= s390_set_memory_limit(machine
->maxram_size
, &hw_limit
);
374 error_setg(errp
, "host supports a maximum of %" PRIu64
" GB",
377 error_setg(errp
, "setting the guest size failed");
381 static void sclp_memory_init(SCLPDevice
*sclp
)
383 MachineState
*machine
= MACHINE(qdev_get_machine());
384 MachineClass
*machine_class
= MACHINE_GET_CLASS(qdev_get_machine());
385 ram_addr_t initial_mem
= machine
->ram_size
;
386 int increment_size
= 20;
388 /* The storage increment size is a multiple of 1M and is a power of 2.
389 * For some machine types, the number of storage increments must be
390 * MAX_STORAGE_INCREMENTS or fewer.
391 * The variable 'increment_size' is an exponent of 2 that can be
392 * used to calculate the size (in bytes) of an increment. */
393 while (machine_class
->fixup_ram_size
!= NULL
&&
394 (initial_mem
>> increment_size
) > MAX_STORAGE_INCREMENTS
) {
397 sclp
->increment_size
= increment_size
;
400 static void sclp_init(Object
*obj
)
402 SCLPDevice
*sclp
= SCLP(obj
);
405 new = object_new(TYPE_SCLP_EVENT_FACILITY
);
406 object_property_add_child(obj
, TYPE_SCLP_EVENT_FACILITY
, new);
408 sclp
->event_facility
= EVENT_FACILITY(new);
410 sclp_memory_init(sclp
);
413 static void sclp_class_init(ObjectClass
*oc
, void *data
)
415 SCLPDeviceClass
*sc
= SCLP_CLASS(oc
);
416 DeviceClass
*dc
= DEVICE_CLASS(oc
);
418 dc
->desc
= "SCLP (Service-Call Logical Processor)";
419 dc
->realize
= sclp_realize
;
420 dc
->hotpluggable
= false;
421 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
423 * Reason: Creates TYPE_SCLP_EVENT_FACILITY in sclp_init
424 * which is a non-pluggable sysbus device
426 dc
->user_creatable
= false;
428 sc
->read_SCP_info
= read_SCP_info
;
429 sc
->read_cpu_info
= sclp_read_cpu_info
;
430 sc
->execute
= sclp_execute
;
431 sc
->service_interrupt
= service_interrupt
;
434 static TypeInfo sclp_info
= {
436 .parent
= TYPE_DEVICE
,
437 .instance_init
= sclp_init
,
438 .instance_size
= sizeof(SCLPDevice
),
439 .class_init
= sclp_class_init
,
440 .class_size
= sizeof(SCLPDeviceClass
),
443 static void register_types(void)
445 type_register_static(&sclp_info
);
447 type_init(register_types
);