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"
17 #include "sysemu/kvm.h"
18 #include "exec/memory.h"
19 #include "sysemu/sysemu.h"
20 #include "exec/address-spaces.h"
21 #include "hw/boards.h"
22 #include "hw/s390x/sclp.h"
23 #include "hw/s390x/event-facility.h"
24 #include "hw/s390x/s390-pci-bus.h"
26 static inline SCLPDevice
*get_sclp_device(void)
28 return SCLP(object_resolve_path_type("", TYPE_SCLP
, NULL
));
31 /* Provide information about the configuration, CPUs and storage */
32 static void read_SCP_info(SCLPDevice
*sclp
, SCCB
*sccb
)
34 ReadInfo
*read_info
= (ReadInfo
*) sccb
;
35 MachineState
*machine
= MACHINE(qdev_get_machine());
36 sclpMemoryHotplugDev
*mhd
= get_sclp_memory_hotplug_dev();
41 int slots
= MIN(machine
->ram_slots
, s390_get_memslot_count(kvm_state
));
48 read_info
->entries_cpu
= cpu_to_be16(cpu_count
);
49 read_info
->offset_cpu
= cpu_to_be16(offsetof(ReadInfo
, entries
));
50 read_info
->highest_cpu
= cpu_to_be16(max_cpus
);
52 for (i
= 0; i
< cpu_count
; i
++) {
53 read_info
->entries
[i
].address
= i
;
54 read_info
->entries
[i
].type
= 0;
57 read_info
->facilities
= cpu_to_be64(SCLP_HAS_CPU_INFO
|
58 SCLP_HAS_PCI_RECONFIG
);
60 /* Memory Hotplug is only supported for the ccw machine type */
62 mhd
->standby_subregion_size
= MEM_SECTION_SIZE
;
63 /* Deduct the memory slot already used for core */
65 while ((mhd
->standby_subregion_size
* (slots
- 1)
66 < mhd
->standby_mem_size
)) {
67 mhd
->standby_subregion_size
= mhd
->standby_subregion_size
<< 1;
71 * Initialize mapping of guest standby memory sections indicating which
72 * are and are not online. Assume all standby memory begins offline.
74 if (mhd
->standby_state_map
== 0) {
75 if (mhd
->standby_mem_size
% mhd
->standby_subregion_size
) {
76 mhd
->standby_state_map
= g_malloc0((mhd
->standby_mem_size
/
77 mhd
->standby_subregion_size
+ 1) *
78 (mhd
->standby_subregion_size
/
81 mhd
->standby_state_map
= g_malloc0(mhd
->standby_mem_size
/
85 mhd
->padded_ram_size
= ram_size
+ mhd
->pad_size
;
86 mhd
->rzm
= 1 << mhd
->increment_size
;
88 read_info
->facilities
|= cpu_to_be64(SCLP_FC_ASSIGN_ATTACH_READ_STOR
);
91 rnsize
= 1 << (sclp
->increment_size
- 20);
93 read_info
->rnsize
= rnsize
;
95 read_info
->rnsize
= 0;
96 read_info
->rnsize2
= cpu_to_be32(rnsize
);
99 rnmax
= machine
->maxram_size
>> sclp
->increment_size
;
100 if (rnmax
< 0x10000) {
101 read_info
->rnmax
= cpu_to_be16(rnmax
);
103 read_info
->rnmax
= cpu_to_be16(0);
104 read_info
->rnmax2
= cpu_to_be64(rnmax
);
107 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION
);
110 static void read_storage_element0_info(SCLPDevice
*sclp
, SCCB
*sccb
)
113 int subincrement_id
= SCLP_STARTING_SUBINCREMENT_ID
;
114 ReadStorageElementInfo
*storage_info
= (ReadStorageElementInfo
*) sccb
;
115 sclpMemoryHotplugDev
*mhd
= get_sclp_memory_hotplug_dev();
118 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND
);
122 if ((ram_size
>> mhd
->increment_size
) >= 0x10000) {
123 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION
);
127 /* Return information regarding core memory */
128 storage_info
->max_id
= cpu_to_be16(mhd
->standby_mem_size
? 1 : 0);
129 assigned
= ram_size
>> mhd
->increment_size
;
130 storage_info
->assigned
= cpu_to_be16(assigned
);
132 for (i
= 0; i
< assigned
; i
++) {
133 storage_info
->entries
[i
] = cpu_to_be32(subincrement_id
);
134 subincrement_id
+= SCLP_INCREMENT_UNIT
;
136 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION
);
139 static void read_storage_element1_info(SCLPDevice
*sclp
, SCCB
*sccb
)
141 ReadStorageElementInfo
*storage_info
= (ReadStorageElementInfo
*) sccb
;
142 sclpMemoryHotplugDev
*mhd
= get_sclp_memory_hotplug_dev();
145 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND
);
149 if ((mhd
->standby_mem_size
>> mhd
->increment_size
) >= 0x10000) {
150 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION
);
154 /* Return information regarding standby memory */
155 storage_info
->max_id
= cpu_to_be16(mhd
->standby_mem_size
? 1 : 0);
156 storage_info
->assigned
= cpu_to_be16(mhd
->standby_mem_size
>>
157 mhd
->increment_size
);
158 storage_info
->standby
= cpu_to_be16(mhd
->standby_mem_size
>>
159 mhd
->increment_size
);
160 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_STANDBY_READ_COMPLETION
);
163 static void attach_storage_element(SCLPDevice
*sclp
, SCCB
*sccb
,
166 int i
, assigned
, subincrement_id
;
167 AttachStorageElement
*attach_info
= (AttachStorageElement
*) sccb
;
168 sclpMemoryHotplugDev
*mhd
= get_sclp_memory_hotplug_dev();
171 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND
);
176 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND
);
180 assigned
= mhd
->standby_mem_size
>> mhd
->increment_size
;
181 attach_info
->assigned
= cpu_to_be16(assigned
);
182 subincrement_id
= ((ram_size
>> mhd
->increment_size
) << 16)
183 + SCLP_STARTING_SUBINCREMENT_ID
;
184 for (i
= 0; i
< assigned
; i
++) {
185 attach_info
->entries
[i
] = cpu_to_be32(subincrement_id
);
186 subincrement_id
+= SCLP_INCREMENT_UNIT
;
188 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_NORMAL_COMPLETION
);
191 static void assign_storage(SCLPDevice
*sclp
, SCCB
*sccb
)
193 MemoryRegion
*mr
= NULL
;
194 uint64_t this_subregion_size
;
195 AssignStorage
*assign_info
= (AssignStorage
*) sccb
;
196 sclpMemoryHotplugDev
*mhd
= get_sclp_memory_hotplug_dev();
197 ram_addr_t assign_addr
;
198 MemoryRegion
*sysmem
= get_system_memory();
201 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND
);
204 assign_addr
= (assign_info
->rn
- 1) * mhd
->rzm
;
206 if ((assign_addr
% MEM_SECTION_SIZE
== 0) &&
207 (assign_addr
>= mhd
->padded_ram_size
)) {
208 /* Re-use existing memory region if found */
209 mr
= memory_region_find(sysmem
, assign_addr
, 1).mr
;
210 memory_region_unref(mr
);
213 MemoryRegion
*standby_ram
= g_new(MemoryRegion
, 1);
215 /* offset to align to standby_subregion_size for allocation */
216 ram_addr_t offset
= assign_addr
-
217 (assign_addr
- mhd
->padded_ram_size
)
218 % mhd
->standby_subregion_size
;
220 /* strlen("standby.ram") + 4 (Max of KVM_MEMORY_SLOTS) + NULL */
222 snprintf(id
, 16, "standby.ram%d",
223 (int)((offset
- mhd
->padded_ram_size
) /
224 mhd
->standby_subregion_size
) + 1);
226 /* Allocate a subregion of the calculated standby_subregion_size */
227 if (offset
+ mhd
->standby_subregion_size
>
228 mhd
->padded_ram_size
+ mhd
->standby_mem_size
) {
229 this_subregion_size
= mhd
->padded_ram_size
+
230 mhd
->standby_mem_size
- offset
;
232 this_subregion_size
= mhd
->standby_subregion_size
;
235 memory_region_init_ram(standby_ram
, NULL
, id
, this_subregion_size
,
237 /* This is a hack to make memory hotunplug work again. Once we have
238 * subdevices, we have to unparent them when unassigning memory,
239 * instead of doing it via the ref count of the MemoryRegion. */
240 object_ref(OBJECT(standby_ram
));
241 object_unparent(OBJECT(standby_ram
));
242 vmstate_register_ram_global(standby_ram
);
243 memory_region_add_subregion(sysmem
, offset
, standby_ram
);
245 /* The specified subregion is no longer in standby */
246 mhd
->standby_state_map
[(assign_addr
- mhd
->padded_ram_size
)
247 / MEM_SECTION_SIZE
] = 1;
249 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_NORMAL_COMPLETION
);
252 static void unassign_storage(SCLPDevice
*sclp
, SCCB
*sccb
)
254 MemoryRegion
*mr
= NULL
;
255 AssignStorage
*assign_info
= (AssignStorage
*) sccb
;
256 sclpMemoryHotplugDev
*mhd
= get_sclp_memory_hotplug_dev();
257 ram_addr_t unassign_addr
;
258 MemoryRegion
*sysmem
= get_system_memory();
261 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND
);
264 unassign_addr
= (assign_info
->rn
- 1) * mhd
->rzm
;
266 /* if the addr is a multiple of 256 MB */
267 if ((unassign_addr
% MEM_SECTION_SIZE
== 0) &&
268 (unassign_addr
>= mhd
->padded_ram_size
)) {
269 mhd
->standby_state_map
[(unassign_addr
-
270 mhd
->padded_ram_size
) / MEM_SECTION_SIZE
] = 0;
272 /* find the specified memory region and destroy it */
273 mr
= memory_region_find(sysmem
, unassign_addr
, 1).mr
;
274 memory_region_unref(mr
);
277 int is_removable
= 1;
278 ram_addr_t map_offset
= (unassign_addr
- mhd
->padded_ram_size
-
279 (unassign_addr
- mhd
->padded_ram_size
)
280 % mhd
->standby_subregion_size
);
281 /* Mark all affected subregions as 'standby' once again */
283 i
< (mhd
->standby_subregion_size
/ MEM_SECTION_SIZE
);
286 if (mhd
->standby_state_map
[i
+ map_offset
/ MEM_SECTION_SIZE
]) {
292 memory_region_del_subregion(sysmem
, mr
);
293 object_unref(OBJECT(mr
));
297 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_NORMAL_COMPLETION
);
300 /* Provide information about the CPU */
301 static void sclp_read_cpu_info(SCLPDevice
*sclp
, SCCB
*sccb
)
303 ReadCpuInfo
*cpu_info
= (ReadCpuInfo
*) sccb
;
312 cpu_info
->nr_configured
= cpu_to_be16(cpu_count
);
313 cpu_info
->offset_configured
= cpu_to_be16(offsetof(ReadCpuInfo
, entries
));
314 cpu_info
->nr_standby
= cpu_to_be16(0);
316 /* The standby offset is 16-byte for each CPU */
317 cpu_info
->offset_standby
= cpu_to_be16(cpu_info
->offset_configured
318 + cpu_info
->nr_configured
*sizeof(CPUEntry
));
320 for (i
= 0; i
< cpu_count
; i
++) {
321 cpu_info
->entries
[i
].address
= i
;
322 cpu_info
->entries
[i
].type
= 0;
325 sccb
->h
.response_code
= cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION
);
328 static void sclp_execute(SCLPDevice
*sclp
, SCCB
*sccb
, uint32_t code
)
330 SCLPDeviceClass
*sclp_c
= SCLP_GET_CLASS(sclp
);
331 SCLPEventFacility
*ef
= sclp
->event_facility
;
332 SCLPEventFacilityClass
*efc
= EVENT_FACILITY_GET_CLASS(ef
);
334 switch (code
& SCLP_CMD_CODE_MASK
) {
335 case SCLP_CMDW_READ_SCP_INFO
:
336 case SCLP_CMDW_READ_SCP_INFO_FORCED
:
337 sclp_c
->read_SCP_info(sclp
, sccb
);
339 case SCLP_CMDW_READ_CPU_INFO
:
340 sclp_c
->read_cpu_info(sclp
, sccb
);
342 case SCLP_READ_STORAGE_ELEMENT_INFO
:
344 sclp_c
->read_storage_element1_info(sclp
, sccb
);
346 sclp_c
->read_storage_element0_info(sclp
, sccb
);
349 case SCLP_ATTACH_STORAGE_ELEMENT
:
350 sclp_c
->attach_storage_element(sclp
, sccb
, (code
& 0xff00) >> 8);
352 case SCLP_ASSIGN_STORAGE
:
353 sclp_c
->assign_storage(sclp
, sccb
);
355 case SCLP_UNASSIGN_STORAGE
:
356 sclp_c
->unassign_storage(sclp
, sccb
);
358 case SCLP_CMDW_CONFIGURE_PCI
:
359 s390_pci_sclp_configure(1, sccb
);
361 case SCLP_CMDW_DECONFIGURE_PCI
:
362 s390_pci_sclp_configure(0, sccb
);
365 efc
->command_handler(ef
, sccb
, code
);
370 int sclp_service_call(CPUS390XState
*env
, uint64_t sccb
, uint32_t code
)
372 SCLPDevice
*sclp
= get_sclp_device();
373 SCLPDeviceClass
*sclp_c
= SCLP_GET_CLASS(sclp
);
377 hwaddr sccb_len
= sizeof(SCCB
);
379 /* first some basic checks on program checks */
380 if (env
->psw
.mask
& PSW_MASK_PSTATE
) {
384 if (cpu_physical_memory_is_io(sccb
)) {
388 if ((sccb
& ~0x1fffUL
) == 0 || (sccb
& ~0x1fffUL
) == env
->psa
389 || (sccb
& ~0x7ffffff8UL
) != 0) {
390 r
= -PGM_SPECIFICATION
;
395 * we want to work on a private copy of the sccb, to prevent guests
396 * from playing dirty tricks by modifying the memory content after
397 * the host has checked the values
399 cpu_physical_memory_read(sccb
, &work_sccb
, sccb_len
);
401 /* Valid sccb sizes */
402 if (be16_to_cpu(work_sccb
.h
.length
) < sizeof(SCCBHeader
) ||
403 be16_to_cpu(work_sccb
.h
.length
) > SCCB_SIZE
) {
404 r
= -PGM_SPECIFICATION
;
408 sclp_c
->execute(sclp
, (SCCB
*)&work_sccb
, code
);
410 cpu_physical_memory_write(sccb
, &work_sccb
,
411 be16_to_cpu(work_sccb
.h
.length
));
413 sclp_c
->service_interrupt(sclp
, sccb
);
419 static void service_interrupt(SCLPDevice
*sclp
, uint32_t sccb
)
421 SCLPEventFacility
*ef
= sclp
->event_facility
;
422 SCLPEventFacilityClass
*efc
= EVENT_FACILITY_GET_CLASS(ef
);
424 uint32_t param
= sccb
& ~3;
426 /* Indicate whether an event is still pending */
427 param
|= efc
->event_pending(ef
) ? 1 : 0;
430 /* No need to send an interrupt, there's nothing to be notified about */
433 s390_sclp_extint(param
);
436 void sclp_service_interrupt(uint32_t sccb
)
438 SCLPDevice
*sclp
= get_sclp_device();
439 SCLPDeviceClass
*sclp_c
= SCLP_GET_CLASS(sclp
);
441 sclp_c
->service_interrupt(sclp
, sccb
);
444 /* qemu object creation and initialization functions */
446 void s390_sclp_init(void)
448 Object
*new = object_new(TYPE_SCLP
);
450 object_property_add_child(qdev_get_machine(), TYPE_SCLP
, new,
452 object_unref(OBJECT(new));
453 qdev_init_nofail(DEVICE(new));
456 static void sclp_realize(DeviceState
*dev
, Error
**errp
)
458 MachineState
*machine
= MACHINE(qdev_get_machine());
459 SCLPDevice
*sclp
= SCLP(dev
);
464 object_property_set_bool(OBJECT(sclp
->event_facility
), true, "realized",
470 * qdev_device_add searches the sysbus for TYPE_SCLP_EVENTS_BUS. As long
471 * as we can't find a fitting bus via the qom tree, we have to add the
472 * event facility to the sysbus, so e.g. a sclp console can be created.
474 qdev_set_parent_bus(DEVICE(sclp
->event_facility
), sysbus_get_default());
476 ret
= s390_set_memory_limit(machine
->maxram_size
, &hw_limit
);
478 error_setg(&err
, "qemu: host supports a maximum of %" PRIu64
" GB",
481 error_setg(&err
, "qemu: setting the guest size failed");
485 error_propagate(errp
, err
);
488 static void sclp_memory_init(SCLPDevice
*sclp
)
490 MachineState
*machine
= MACHINE(qdev_get_machine());
491 ram_addr_t initial_mem
= machine
->ram_size
;
492 ram_addr_t max_mem
= machine
->maxram_size
;
493 ram_addr_t standby_mem
= max_mem
- initial_mem
;
494 ram_addr_t pad_mem
= 0;
495 int increment_size
= 20;
497 /* The storage increment size is a multiple of 1M and is a power of 2.
498 * The number of storage increments must be MAX_STORAGE_INCREMENTS or fewer.
499 * The variable 'increment_size' is an exponent of 2 that can be
500 * used to calculate the size (in bytes) of an increment. */
501 while ((initial_mem
>> increment_size
) > MAX_STORAGE_INCREMENTS
) {
504 if (machine
->ram_slots
) {
505 while ((standby_mem
>> increment_size
) > MAX_STORAGE_INCREMENTS
) {
509 sclp
->increment_size
= increment_size
;
511 /* The core and standby memory areas need to be aligned with
512 * the increment size. In effect, this can cause the
513 * user-specified memory size to be rounded down to align
514 * with the nearest increment boundary. */
515 initial_mem
= initial_mem
>> increment_size
<< increment_size
;
516 standby_mem
= standby_mem
>> increment_size
<< increment_size
;
518 /* If the size of ram is not on a MEM_SECTION_SIZE boundary,
519 calculate the pad size necessary to force this boundary. */
520 if (machine
->ram_slots
&& standby_mem
) {
521 sclpMemoryHotplugDev
*mhd
= init_sclp_memory_hotplug_dev();
523 if (initial_mem
% MEM_SECTION_SIZE
) {
524 pad_mem
= MEM_SECTION_SIZE
- initial_mem
% MEM_SECTION_SIZE
;
526 mhd
->increment_size
= increment_size
;
527 mhd
->pad_size
= pad_mem
;
528 mhd
->standby_mem_size
= standby_mem
;
530 machine
->ram_size
= initial_mem
;
531 machine
->maxram_size
= initial_mem
+ pad_mem
+ standby_mem
;
532 /* let's propagate the changed ram size into the global variable. */
533 ram_size
= initial_mem
;
536 static void sclp_init(Object
*obj
)
538 SCLPDevice
*sclp
= SCLP(obj
);
541 new = object_new(TYPE_SCLP_EVENT_FACILITY
);
542 object_property_add_child(obj
, TYPE_SCLP_EVENT_FACILITY
, new, NULL
);
544 sclp
->event_facility
= EVENT_FACILITY(new);
546 sclp_memory_init(sclp
);
549 static void sclp_class_init(ObjectClass
*oc
, void *data
)
551 SCLPDeviceClass
*sc
= SCLP_CLASS(oc
);
552 DeviceClass
*dc
= DEVICE_CLASS(oc
);
554 dc
->desc
= "SCLP (Service-Call Logical Processor)";
555 dc
->realize
= sclp_realize
;
556 dc
->hotpluggable
= false;
557 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
559 sc
->read_SCP_info
= read_SCP_info
;
560 sc
->read_storage_element0_info
= read_storage_element0_info
;
561 sc
->read_storage_element1_info
= read_storage_element1_info
;
562 sc
->attach_storage_element
= attach_storage_element
;
563 sc
->assign_storage
= assign_storage
;
564 sc
->unassign_storage
= unassign_storage
;
565 sc
->read_cpu_info
= sclp_read_cpu_info
;
566 sc
->execute
= sclp_execute
;
567 sc
->service_interrupt
= service_interrupt
;
570 static TypeInfo sclp_info
= {
572 .parent
= TYPE_DEVICE
,
573 .instance_init
= sclp_init
,
574 .instance_size
= sizeof(SCLPDevice
),
575 .class_init
= sclp_class_init
,
576 .class_size
= sizeof(SCLPDeviceClass
),
579 sclpMemoryHotplugDev
*init_sclp_memory_hotplug_dev(void)
582 dev
= qdev_create(NULL
, TYPE_SCLP_MEMORY_HOTPLUG_DEV
);
583 object_property_add_child(qdev_get_machine(),
584 TYPE_SCLP_MEMORY_HOTPLUG_DEV
,
586 qdev_init_nofail(dev
);
587 return SCLP_MEMORY_HOTPLUG_DEV(object_resolve_path(
588 TYPE_SCLP_MEMORY_HOTPLUG_DEV
, NULL
));
591 sclpMemoryHotplugDev
*get_sclp_memory_hotplug_dev(void)
593 return SCLP_MEMORY_HOTPLUG_DEV(object_resolve_path(
594 TYPE_SCLP_MEMORY_HOTPLUG_DEV
, NULL
));
597 static void sclp_memory_hotplug_dev_class_init(ObjectClass
*klass
,
600 DeviceClass
*dc
= DEVICE_CLASS(klass
);
602 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
605 static TypeInfo sclp_memory_hotplug_dev_info
= {
606 .name
= TYPE_SCLP_MEMORY_HOTPLUG_DEV
,
607 .parent
= TYPE_SYS_BUS_DEVICE
,
608 .instance_size
= sizeof(sclpMemoryHotplugDev
),
609 .class_init
= sclp_memory_hotplug_dev_class_init
,
612 static void register_types(void)
614 type_register_static(&sclp_memory_hotplug_dev_info
);
615 type_register_static(&sclp_info
);
617 type_init(register_types
);