ppc: simplify max_smt initialization in ppc_cpu_realizefn()
[qemu/ar7.git] / hw / s390x / sclp.c
blobfca37f511e1dd48313c4484d39f07843a2f568d9
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 "qapi/error.h"
17 #include "cpu.h"
18 #include "sysemu/kvm.h"
19 #include "exec/memory.h"
20 #include "sysemu/sysemu.h"
21 #include "exec/address-spaces.h"
22 #include "hw/boards.h"
23 #include "hw/s390x/sclp.h"
24 #include "hw/s390x/event-facility.h"
25 #include "hw/s390x/s390-pci-bus.h"
27 static inline SCLPDevice *get_sclp_device(void)
29 return SCLP(object_resolve_path_type("", TYPE_SCLP, NULL));
32 /* Provide information about the configuration, CPUs and storage */
33 static void read_SCP_info(SCLPDevice *sclp, SCCB *sccb)
35 ReadInfo *read_info = (ReadInfo *) sccb;
36 MachineState *machine = MACHINE(qdev_get_machine());
37 sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev();
38 CPUState *cpu;
39 int cpu_count = 0;
40 int i = 0;
41 int rnsize, rnmax;
42 int slots = MIN(machine->ram_slots, s390_get_memslot_count(kvm_state));
44 CPU_FOREACH(cpu) {
45 cpu_count++;
48 /* CPU information */
49 read_info->entries_cpu = cpu_to_be16(cpu_count);
50 read_info->offset_cpu = cpu_to_be16(offsetof(ReadInfo, entries));
51 read_info->highest_cpu = cpu_to_be16(max_cpus);
53 for (i = 0; i < cpu_count; i++) {
54 read_info->entries[i].address = i;
55 read_info->entries[i].type = 0;
58 read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO |
59 SCLP_HAS_PCI_RECONFIG);
61 /* Memory Hotplug is only supported for the ccw machine type */
62 if (mhd) {
63 mhd->standby_subregion_size = MEM_SECTION_SIZE;
64 /* Deduct the memory slot already used for core */
65 if (slots > 0) {
66 while ((mhd->standby_subregion_size * (slots - 1)
67 < mhd->standby_mem_size)) {
68 mhd->standby_subregion_size = mhd->standby_subregion_size << 1;
72 * Initialize mapping of guest standby memory sections indicating which
73 * are and are not online. Assume all standby memory begins offline.
75 if (mhd->standby_state_map == 0) {
76 if (mhd->standby_mem_size % mhd->standby_subregion_size) {
77 mhd->standby_state_map = g_malloc0((mhd->standby_mem_size /
78 mhd->standby_subregion_size + 1) *
79 (mhd->standby_subregion_size /
80 MEM_SECTION_SIZE));
81 } else {
82 mhd->standby_state_map = g_malloc0(mhd->standby_mem_size /
83 MEM_SECTION_SIZE);
86 mhd->padded_ram_size = ram_size + mhd->pad_size;
87 mhd->rzm = 1 << mhd->increment_size;
89 read_info->facilities |= cpu_to_be64(SCLP_FC_ASSIGN_ATTACH_READ_STOR);
92 rnsize = 1 << (sclp->increment_size - 20);
93 if (rnsize <= 128) {
94 read_info->rnsize = rnsize;
95 } else {
96 read_info->rnsize = 0;
97 read_info->rnsize2 = cpu_to_be32(rnsize);
100 rnmax = machine->maxram_size >> sclp->increment_size;
101 if (rnmax < 0x10000) {
102 read_info->rnmax = cpu_to_be16(rnmax);
103 } else {
104 read_info->rnmax = cpu_to_be16(0);
105 read_info->rnmax2 = cpu_to_be64(rnmax);
108 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
111 static void read_storage_element0_info(SCLPDevice *sclp, SCCB *sccb)
113 int i, assigned;
114 int subincrement_id = SCLP_STARTING_SUBINCREMENT_ID;
115 ReadStorageElementInfo *storage_info = (ReadStorageElementInfo *) sccb;
116 sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev();
118 if (!mhd) {
119 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
120 return;
123 if ((ram_size >> mhd->increment_size) >= 0x10000) {
124 sccb->h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION);
125 return;
128 /* Return information regarding core memory */
129 storage_info->max_id = cpu_to_be16(mhd->standby_mem_size ? 1 : 0);
130 assigned = ram_size >> mhd->increment_size;
131 storage_info->assigned = cpu_to_be16(assigned);
133 for (i = 0; i < assigned; i++) {
134 storage_info->entries[i] = cpu_to_be32(subincrement_id);
135 subincrement_id += SCLP_INCREMENT_UNIT;
137 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
140 static void read_storage_element1_info(SCLPDevice *sclp, SCCB *sccb)
142 ReadStorageElementInfo *storage_info = (ReadStorageElementInfo *) sccb;
143 sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev();
145 if (!mhd) {
146 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
147 return;
150 if ((mhd->standby_mem_size >> mhd->increment_size) >= 0x10000) {
151 sccb->h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION);
152 return;
155 /* Return information regarding standby memory */
156 storage_info->max_id = cpu_to_be16(mhd->standby_mem_size ? 1 : 0);
157 storage_info->assigned = cpu_to_be16(mhd->standby_mem_size >>
158 mhd->increment_size);
159 storage_info->standby = cpu_to_be16(mhd->standby_mem_size >>
160 mhd->increment_size);
161 sccb->h.response_code = cpu_to_be16(SCLP_RC_STANDBY_READ_COMPLETION);
164 static void attach_storage_element(SCLPDevice *sclp, SCCB *sccb,
165 uint16_t element)
167 int i, assigned, subincrement_id;
168 AttachStorageElement *attach_info = (AttachStorageElement *) sccb;
169 sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev();
171 if (!mhd) {
172 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
173 return;
176 if (element != 1) {
177 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
178 return;
181 assigned = mhd->standby_mem_size >> mhd->increment_size;
182 attach_info->assigned = cpu_to_be16(assigned);
183 subincrement_id = ((ram_size >> mhd->increment_size) << 16)
184 + SCLP_STARTING_SUBINCREMENT_ID;
185 for (i = 0; i < assigned; i++) {
186 attach_info->entries[i] = cpu_to_be32(subincrement_id);
187 subincrement_id += SCLP_INCREMENT_UNIT;
189 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION);
192 static void assign_storage(SCLPDevice *sclp, SCCB *sccb)
194 MemoryRegion *mr = NULL;
195 uint64_t this_subregion_size;
196 AssignStorage *assign_info = (AssignStorage *) sccb;
197 sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev();
198 ram_addr_t assign_addr;
199 MemoryRegion *sysmem = get_system_memory();
201 if (!mhd) {
202 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
203 return;
205 assign_addr = (assign_info->rn - 1) * mhd->rzm;
207 if ((assign_addr % MEM_SECTION_SIZE == 0) &&
208 (assign_addr >= mhd->padded_ram_size)) {
209 /* Re-use existing memory region if found */
210 mr = memory_region_find(sysmem, assign_addr, 1).mr;
211 memory_region_unref(mr);
212 if (!mr) {
214 MemoryRegion *standby_ram = g_new(MemoryRegion, 1);
216 /* offset to align to standby_subregion_size for allocation */
217 ram_addr_t offset = assign_addr -
218 (assign_addr - mhd->padded_ram_size)
219 % mhd->standby_subregion_size;
221 /* strlen("standby.ram") + 4 (Max of KVM_MEMORY_SLOTS) + NULL */
222 char id[16];
223 snprintf(id, 16, "standby.ram%d",
224 (int)((offset - mhd->padded_ram_size) /
225 mhd->standby_subregion_size) + 1);
227 /* Allocate a subregion of the calculated standby_subregion_size */
228 if (offset + mhd->standby_subregion_size >
229 mhd->padded_ram_size + mhd->standby_mem_size) {
230 this_subregion_size = mhd->padded_ram_size +
231 mhd->standby_mem_size - offset;
232 } else {
233 this_subregion_size = mhd->standby_subregion_size;
236 memory_region_init_ram(standby_ram, NULL, id, this_subregion_size,
237 &error_fatal);
238 /* This is a hack to make memory hotunplug work again. Once we have
239 * subdevices, we have to unparent them when unassigning memory,
240 * instead of doing it via the ref count of the MemoryRegion. */
241 object_ref(OBJECT(standby_ram));
242 object_unparent(OBJECT(standby_ram));
243 vmstate_register_ram_global(standby_ram);
244 memory_region_add_subregion(sysmem, offset, standby_ram);
246 /* The specified subregion is no longer in standby */
247 mhd->standby_state_map[(assign_addr - mhd->padded_ram_size)
248 / MEM_SECTION_SIZE] = 1;
250 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION);
253 static void unassign_storage(SCLPDevice *sclp, SCCB *sccb)
255 MemoryRegion *mr = NULL;
256 AssignStorage *assign_info = (AssignStorage *) sccb;
257 sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev();
258 ram_addr_t unassign_addr;
259 MemoryRegion *sysmem = get_system_memory();
261 if (!mhd) {
262 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
263 return;
265 unassign_addr = (assign_info->rn - 1) * mhd->rzm;
267 /* if the addr is a multiple of 256 MB */
268 if ((unassign_addr % MEM_SECTION_SIZE == 0) &&
269 (unassign_addr >= mhd->padded_ram_size)) {
270 mhd->standby_state_map[(unassign_addr -
271 mhd->padded_ram_size) / MEM_SECTION_SIZE] = 0;
273 /* find the specified memory region and destroy it */
274 mr = memory_region_find(sysmem, unassign_addr, 1).mr;
275 memory_region_unref(mr);
276 if (mr) {
277 int i;
278 int is_removable = 1;
279 ram_addr_t map_offset = (unassign_addr - mhd->padded_ram_size -
280 (unassign_addr - mhd->padded_ram_size)
281 % mhd->standby_subregion_size);
282 /* Mark all affected subregions as 'standby' once again */
283 for (i = 0;
284 i < (mhd->standby_subregion_size / MEM_SECTION_SIZE);
285 i++) {
287 if (mhd->standby_state_map[i + map_offset / MEM_SECTION_SIZE]) {
288 is_removable = 0;
289 break;
292 if (is_removable) {
293 memory_region_del_subregion(sysmem, mr);
294 object_unref(OBJECT(mr));
298 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION);
301 /* Provide information about the CPU */
302 static void sclp_read_cpu_info(SCLPDevice *sclp, SCCB *sccb)
304 ReadCpuInfo *cpu_info = (ReadCpuInfo *) sccb;
305 CPUState *cpu;
306 int cpu_count = 0;
307 int i = 0;
309 CPU_FOREACH(cpu) {
310 cpu_count++;
313 cpu_info->nr_configured = cpu_to_be16(cpu_count);
314 cpu_info->offset_configured = cpu_to_be16(offsetof(ReadCpuInfo, entries));
315 cpu_info->nr_standby = cpu_to_be16(0);
317 /* The standby offset is 16-byte for each CPU */
318 cpu_info->offset_standby = cpu_to_be16(cpu_info->offset_configured
319 + cpu_info->nr_configured*sizeof(CPUEntry));
321 for (i = 0; i < cpu_count; i++) {
322 cpu_info->entries[i].address = i;
323 cpu_info->entries[i].type = 0;
326 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
329 static void sclp_execute(SCLPDevice *sclp, SCCB *sccb, uint32_t code)
331 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
332 SCLPEventFacility *ef = sclp->event_facility;
333 SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
335 switch (code & SCLP_CMD_CODE_MASK) {
336 case SCLP_CMDW_READ_SCP_INFO:
337 case SCLP_CMDW_READ_SCP_INFO_FORCED:
338 sclp_c->read_SCP_info(sclp, sccb);
339 break;
340 case SCLP_CMDW_READ_CPU_INFO:
341 sclp_c->read_cpu_info(sclp, sccb);
342 break;
343 case SCLP_READ_STORAGE_ELEMENT_INFO:
344 if (code & 0xff00) {
345 sclp_c->read_storage_element1_info(sclp, sccb);
346 } else {
347 sclp_c->read_storage_element0_info(sclp, sccb);
349 break;
350 case SCLP_ATTACH_STORAGE_ELEMENT:
351 sclp_c->attach_storage_element(sclp, sccb, (code & 0xff00) >> 8);
352 break;
353 case SCLP_ASSIGN_STORAGE:
354 sclp_c->assign_storage(sclp, sccb);
355 break;
356 case SCLP_UNASSIGN_STORAGE:
357 sclp_c->unassign_storage(sclp, sccb);
358 break;
359 case SCLP_CMDW_CONFIGURE_PCI:
360 s390_pci_sclp_configure(sccb);
361 break;
362 case SCLP_CMDW_DECONFIGURE_PCI:
363 s390_pci_sclp_deconfigure(sccb);
364 break;
365 default:
366 efc->command_handler(ef, sccb, code);
367 break;
371 int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code)
373 SCLPDevice *sclp = get_sclp_device();
374 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
375 int r = 0;
376 SCCB work_sccb;
378 hwaddr sccb_len = sizeof(SCCB);
380 /* first some basic checks on program checks */
381 if (env->psw.mask & PSW_MASK_PSTATE) {
382 r = -PGM_PRIVILEGED;
383 goto out;
385 if (cpu_physical_memory_is_io(sccb)) {
386 r = -PGM_ADDRESSING;
387 goto out;
389 if ((sccb & ~0x1fffUL) == 0 || (sccb & ~0x1fffUL) == env->psa
390 || (sccb & ~0x7ffffff8UL) != 0) {
391 r = -PGM_SPECIFICATION;
392 goto out;
396 * we want to work on a private copy of the sccb, to prevent guests
397 * from playing dirty tricks by modifying the memory content after
398 * the host has checked the values
400 cpu_physical_memory_read(sccb, &work_sccb, sccb_len);
402 /* Valid sccb sizes */
403 if (be16_to_cpu(work_sccb.h.length) < sizeof(SCCBHeader) ||
404 be16_to_cpu(work_sccb.h.length) > SCCB_SIZE) {
405 r = -PGM_SPECIFICATION;
406 goto out;
409 sclp_c->execute(sclp, (SCCB *)&work_sccb, code);
411 cpu_physical_memory_write(sccb, &work_sccb,
412 be16_to_cpu(work_sccb.h.length));
414 sclp_c->service_interrupt(sclp, sccb);
416 out:
417 return r;
420 static void service_interrupt(SCLPDevice *sclp, uint32_t sccb)
422 SCLPEventFacility *ef = sclp->event_facility;
423 SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
425 uint32_t param = sccb & ~3;
427 /* Indicate whether an event is still pending */
428 param |= efc->event_pending(ef) ? 1 : 0;
430 if (!param) {
431 /* No need to send an interrupt, there's nothing to be notified about */
432 return;
434 s390_sclp_extint(param);
437 void sclp_service_interrupt(uint32_t sccb)
439 SCLPDevice *sclp = get_sclp_device();
440 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
442 sclp_c->service_interrupt(sclp, sccb);
445 /* qemu object creation and initialization functions */
447 void s390_sclp_init(void)
449 Object *new = object_new(TYPE_SCLP);
451 object_property_add_child(qdev_get_machine(), TYPE_SCLP, new,
452 NULL);
453 object_unref(OBJECT(new));
454 qdev_init_nofail(DEVICE(new));
457 static void sclp_realize(DeviceState *dev, Error **errp)
459 MachineState *machine = MACHINE(qdev_get_machine());
460 SCLPDevice *sclp = SCLP(dev);
461 Error *err = NULL;
462 uint64_t hw_limit;
463 int ret;
465 object_property_set_bool(OBJECT(sclp->event_facility), true, "realized",
466 &err);
467 if (err) {
468 goto out;
471 * qdev_device_add searches the sysbus for TYPE_SCLP_EVENTS_BUS. As long
472 * as we can't find a fitting bus via the qom tree, we have to add the
473 * event facility to the sysbus, so e.g. a sclp console can be created.
475 qdev_set_parent_bus(DEVICE(sclp->event_facility), sysbus_get_default());
477 ret = s390_set_memory_limit(machine->maxram_size, &hw_limit);
478 if (ret == -E2BIG) {
479 error_setg(&err, "qemu: host supports a maximum of %" PRIu64 " GB",
480 hw_limit >> 30);
481 } else if (ret) {
482 error_setg(&err, "qemu: setting the guest size failed");
485 out:
486 error_propagate(errp, err);
489 static void sclp_memory_init(SCLPDevice *sclp)
491 MachineState *machine = MACHINE(qdev_get_machine());
492 ram_addr_t initial_mem = machine->ram_size;
493 ram_addr_t max_mem = machine->maxram_size;
494 ram_addr_t standby_mem = max_mem - initial_mem;
495 ram_addr_t pad_mem = 0;
496 int increment_size = 20;
498 /* The storage increment size is a multiple of 1M and is a power of 2.
499 * The number of storage increments must be MAX_STORAGE_INCREMENTS or fewer.
500 * The variable 'increment_size' is an exponent of 2 that can be
501 * used to calculate the size (in bytes) of an increment. */
502 while ((initial_mem >> increment_size) > MAX_STORAGE_INCREMENTS) {
503 increment_size++;
505 if (machine->ram_slots) {
506 while ((standby_mem >> increment_size) > MAX_STORAGE_INCREMENTS) {
507 increment_size++;
510 sclp->increment_size = increment_size;
512 /* The core and standby memory areas need to be aligned with
513 * the increment size. In effect, this can cause the
514 * user-specified memory size to be rounded down to align
515 * with the nearest increment boundary. */
516 initial_mem = initial_mem >> increment_size << increment_size;
517 standby_mem = standby_mem >> increment_size << increment_size;
519 /* If the size of ram is not on a MEM_SECTION_SIZE boundary,
520 calculate the pad size necessary to force this boundary. */
521 if (machine->ram_slots && standby_mem) {
522 sclpMemoryHotplugDev *mhd = init_sclp_memory_hotplug_dev();
524 if (initial_mem % MEM_SECTION_SIZE) {
525 pad_mem = MEM_SECTION_SIZE - initial_mem % MEM_SECTION_SIZE;
527 mhd->increment_size = increment_size;
528 mhd->pad_size = pad_mem;
529 mhd->standby_mem_size = standby_mem;
531 machine->ram_size = initial_mem;
532 machine->maxram_size = initial_mem + pad_mem + standby_mem;
533 /* let's propagate the changed ram size into the global variable. */
534 ram_size = initial_mem;
537 static void sclp_init(Object *obj)
539 SCLPDevice *sclp = SCLP(obj);
540 Object *new;
542 new = object_new(TYPE_SCLP_EVENT_FACILITY);
543 object_property_add_child(obj, TYPE_SCLP_EVENT_FACILITY, new, NULL);
544 object_unref(new);
545 sclp->event_facility = EVENT_FACILITY(new);
547 sclp_memory_init(sclp);
550 static void sclp_class_init(ObjectClass *oc, void *data)
552 SCLPDeviceClass *sc = SCLP_CLASS(oc);
553 DeviceClass *dc = DEVICE_CLASS(oc);
555 dc->desc = "SCLP (Service-Call Logical Processor)";
556 dc->realize = sclp_realize;
557 dc->hotpluggable = false;
558 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
560 sc->read_SCP_info = read_SCP_info;
561 sc->read_storage_element0_info = read_storage_element0_info;
562 sc->read_storage_element1_info = read_storage_element1_info;
563 sc->attach_storage_element = attach_storage_element;
564 sc->assign_storage = assign_storage;
565 sc->unassign_storage = unassign_storage;
566 sc->read_cpu_info = sclp_read_cpu_info;
567 sc->execute = sclp_execute;
568 sc->service_interrupt = service_interrupt;
571 static TypeInfo sclp_info = {
572 .name = TYPE_SCLP,
573 .parent = TYPE_DEVICE,
574 .instance_init = sclp_init,
575 .instance_size = sizeof(SCLPDevice),
576 .class_init = sclp_class_init,
577 .class_size = sizeof(SCLPDeviceClass),
580 sclpMemoryHotplugDev *init_sclp_memory_hotplug_dev(void)
582 DeviceState *dev;
583 dev = qdev_create(NULL, TYPE_SCLP_MEMORY_HOTPLUG_DEV);
584 object_property_add_child(qdev_get_machine(),
585 TYPE_SCLP_MEMORY_HOTPLUG_DEV,
586 OBJECT(dev), NULL);
587 qdev_init_nofail(dev);
588 return SCLP_MEMORY_HOTPLUG_DEV(object_resolve_path(
589 TYPE_SCLP_MEMORY_HOTPLUG_DEV, NULL));
592 sclpMemoryHotplugDev *get_sclp_memory_hotplug_dev(void)
594 return SCLP_MEMORY_HOTPLUG_DEV(object_resolve_path(
595 TYPE_SCLP_MEMORY_HOTPLUG_DEV, NULL));
598 static void sclp_memory_hotplug_dev_class_init(ObjectClass *klass,
599 void *data)
601 DeviceClass *dc = DEVICE_CLASS(klass);
603 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
606 static TypeInfo sclp_memory_hotplug_dev_info = {
607 .name = TYPE_SCLP_MEMORY_HOTPLUG_DEV,
608 .parent = TYPE_SYS_BUS_DEVICE,
609 .instance_size = sizeof(sclpMemoryHotplugDev),
610 .class_init = sclp_memory_hotplug_dev_class_init,
613 static void register_types(void)
615 type_register_static(&sclp_memory_hotplug_dev_info);
616 type_register_static(&sclp_info);
618 type_init(register_types);