1
2
3
4
5
6
7
8
9
10
11
12
13
14
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"
26
27static inline SCLPDevice *get_sclp_device(void)
28{
29 return SCLP(object_resolve_path_type("", TYPE_SCLP, NULL));
30}
31
32
33static void read_SCP_info(SCLPDevice *sclp, SCCB *sccb)
34{
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));
43
44 CPU_FOREACH(cpu) {
45 cpu_count++;
46 }
47
48
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);
52
53 for (i = 0; i < cpu_count; i++) {
54 read_info->entries[i].address = i;
55 read_info->entries[i].type = 0;
56 }
57
58 read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO |
59 SCLP_HAS_PCI_RECONFIG);
60
61
62 if (mhd) {
63 mhd->standby_subregion_size = MEM_SECTION_SIZE;
64
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;
69 }
70 }
71
72
73
74
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);
84 }
85 }
86 mhd->padded_ram_size = ram_size + mhd->pad_size;
87 mhd->rzm = 1 << mhd->increment_size;
88
89 read_info->facilities |= cpu_to_be64(SCLP_FC_ASSIGN_ATTACH_READ_STOR);
90 }
91
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);
98 }
99
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);
106 }
107
108 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
109}
110
111static void read_storage_element0_info(SCLPDevice *sclp, SCCB *sccb)
112{
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();
117
118 if (!mhd) {
119 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
120 return;
121 }
122
123 if ((ram_size >> mhd->increment_size) >= 0x10000) {
124 sccb->h.response_code = cpu_to_be16(SCLP_RC_SCCB_BOUNDARY_VIOLATION);
125 return;
126 }
127
128
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);
132
133 for (i = 0; i < assigned; i++) {
134 storage_info->entries[i] = cpu_to_be32(subincrement_id);
135 subincrement_id += SCLP_INCREMENT_UNIT;
136 }
137 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
138}
139
140static void read_storage_element1_info(SCLPDevice *sclp, SCCB *sccb)
141{
142 ReadStorageElementInfo *storage_info = (ReadStorageElementInfo *) sccb;
143 sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev();
144
145 if (!mhd) {
146 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
147 return;
148 }
149
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;
153 }
154
155
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);
162}
163
164static void attach_storage_element(SCLPDevice *sclp, SCCB *sccb,
165 uint16_t element)
166{
167 int i, assigned, subincrement_id;
168 AttachStorageElement *attach_info = (AttachStorageElement *) sccb;
169 sclpMemoryHotplugDev *mhd = get_sclp_memory_hotplug_dev();
170
171 if (!mhd) {
172 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
173 return;
174 }
175
176 if (element != 1) {
177 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
178 return;
179 }
180
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;
188 }
189 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION);
190}
191
192static void assign_storage(SCLPDevice *sclp, SCCB *sccb)
193{
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();
200
201 if (!mhd) {
202 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
203 return;
204 }
205 assign_addr = (assign_info->rn - 1) * mhd->rzm;
206
207 if ((assign_addr % MEM_SECTION_SIZE == 0) &&
208 (assign_addr >= mhd->padded_ram_size)) {
209
210 mr = memory_region_find(sysmem, assign_addr, 1).mr;
211 memory_region_unref(mr);
212 if (!mr) {
213
214 MemoryRegion *standby_ram = g_new(MemoryRegion, 1);
215
216
217 ram_addr_t offset = assign_addr -
218 (assign_addr - mhd->padded_ram_size)
219 % mhd->standby_subregion_size;
220
221
222 char id[16];
223 snprintf(id, 16, "standby.ram%d",
224 (int)((offset - mhd->padded_ram_size) /
225 mhd->standby_subregion_size) + 1);
226
227
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;
234 }
235
236 memory_region_init_ram(standby_ram, NULL, id, this_subregion_size,
237 &error_fatal);
238
239
240
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);
245 }
246
247 mhd->standby_state_map[(assign_addr - mhd->padded_ram_size)
248 / MEM_SECTION_SIZE] = 1;
249 }
250 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION);
251}
252
253static void unassign_storage(SCLPDevice *sclp, SCCB *sccb)
254{
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();
260
261 if (!mhd) {
262 sccb->h.response_code = cpu_to_be16(SCLP_RC_INVALID_SCLP_COMMAND);
263 return;
264 }
265 unassign_addr = (assign_info->rn - 1) * mhd->rzm;
266
267
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;
272
273
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
283 for (i = 0;
284 i < (mhd->standby_subregion_size / MEM_SECTION_SIZE);
285 i++) {
286
287 if (mhd->standby_state_map[i + map_offset / MEM_SECTION_SIZE]) {
288 is_removable = 0;
289 break;
290 }
291 }
292 if (is_removable) {
293 memory_region_del_subregion(sysmem, mr);
294 object_unref(OBJECT(mr));
295 }
296 }
297 }
298 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_COMPLETION);
299}
300
301
302static void sclp_read_cpu_info(SCLPDevice *sclp, SCCB *sccb)
303{
304 ReadCpuInfo *cpu_info = (ReadCpuInfo *) sccb;
305 CPUState *cpu;
306 int cpu_count = 0;
307 int i = 0;
308
309 CPU_FOREACH(cpu) {
310 cpu_count++;
311 }
312
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);
316
317
318 cpu_info->offset_standby = cpu_to_be16(cpu_info->offset_configured
319 + cpu_info->nr_configured*sizeof(CPUEntry));
320
321 for (i = 0; i < cpu_count; i++) {
322 cpu_info->entries[i].address = i;
323 cpu_info->entries[i].type = 0;
324 }
325
326 sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
327}
328
329static void sclp_execute(SCLPDevice *sclp, SCCB *sccb, uint32_t code)
330{
331 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
332 SCLPEventFacility *ef = sclp->event_facility;
333 SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
334
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);
348 }
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(1, sccb);
361 break;
362 case SCLP_CMDW_DECONFIGURE_PCI:
363 s390_pci_sclp_configure(0, sccb);
364 break;
365 default:
366 efc->command_handler(ef, sccb, code);
367 break;
368 }
369}
370
371int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code)
372{
373 SCLPDevice *sclp = get_sclp_device();
374 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
375 int r = 0;
376 SCCB work_sccb;
377
378 hwaddr sccb_len = sizeof(SCCB);
379
380
381 if (env->psw.mask & PSW_MASK_PSTATE) {
382 r = -PGM_PRIVILEGED;
383 goto out;
384 }
385 if (cpu_physical_memory_is_io(sccb)) {
386 r = -PGM_ADDRESSING;
387 goto out;
388 }
389 if ((sccb & ~0x1fffUL) == 0 || (sccb & ~0x1fffUL) == env->psa
390 || (sccb & ~0x7ffffff8UL) != 0) {
391 r = -PGM_SPECIFICATION;
392 goto out;
393 }
394
395
396
397
398
399
400 cpu_physical_memory_read(sccb, &work_sccb, sccb_len);
401
402
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;
407 }
408
409 sclp_c->execute(sclp, (SCCB *)&work_sccb, code);
410
411 cpu_physical_memory_write(sccb, &work_sccb,
412 be16_to_cpu(work_sccb.h.length));
413
414 sclp_c->service_interrupt(sclp, sccb);
415
416out:
417 return r;
418}
419
420static void service_interrupt(SCLPDevice *sclp, uint32_t sccb)
421{
422 SCLPEventFacility *ef = sclp->event_facility;
423 SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
424
425 uint32_t param = sccb & ~3;
426
427
428 param |= efc->event_pending(ef) ? 1 : 0;
429
430 if (!param) {
431
432 return;
433 }
434 s390_sclp_extint(param);
435}
436
437void sclp_service_interrupt(uint32_t sccb)
438{
439 SCLPDevice *sclp = get_sclp_device();
440 SCLPDeviceClass *sclp_c = SCLP_GET_CLASS(sclp);
441
442 sclp_c->service_interrupt(sclp, sccb);
443}
444
445
446
447void s390_sclp_init(void)
448{
449 Object *new = object_new(TYPE_SCLP);
450
451 object_property_add_child(qdev_get_machine(), TYPE_SCLP, new,
452 NULL);
453 object_unref(OBJECT(new));
454 qdev_init_nofail(DEVICE(new));
455}
456
457static void sclp_realize(DeviceState *dev, Error **errp)
458{
459 MachineState *machine = MACHINE(qdev_get_machine());
460 SCLPDevice *sclp = SCLP(dev);
461 Error *err = NULL;
462 uint64_t hw_limit;
463 int ret;
464
465 object_property_set_bool(OBJECT(sclp->event_facility), true, "realized",
466 &err);
467 if (err) {
468 goto out;
469 }
470
471
472
473
474
475 qdev_set_parent_bus(DEVICE(sclp->event_facility), sysbus_get_default());
476
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");
483 }
484
485out:
486 error_propagate(errp, err);
487}
488
489static void sclp_memory_init(SCLPDevice *sclp)
490{
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;
497
498
499
500
501
502 while ((initial_mem >> increment_size) > MAX_STORAGE_INCREMENTS) {
503 increment_size++;
504 }
505 if (machine->ram_slots) {
506 while ((standby_mem >> increment_size) > MAX_STORAGE_INCREMENTS) {
507 increment_size++;
508 }
509 }
510 sclp->increment_size = increment_size;
511
512
513
514
515
516 initial_mem = initial_mem >> increment_size << increment_size;
517 standby_mem = standby_mem >> increment_size << increment_size;
518
519
520
521 if (machine->ram_slots && standby_mem) {
522 sclpMemoryHotplugDev *mhd = init_sclp_memory_hotplug_dev();
523
524 if (initial_mem % MEM_SECTION_SIZE) {
525 pad_mem = MEM_SECTION_SIZE - initial_mem % MEM_SECTION_SIZE;
526 }
527 mhd->increment_size = increment_size;
528 mhd->pad_size = pad_mem;
529 mhd->standby_mem_size = standby_mem;
530 }
531 machine->ram_size = initial_mem;
532 machine->maxram_size = initial_mem + pad_mem + standby_mem;
533
534 ram_size = initial_mem;
535}
536
537static void sclp_init(Object *obj)
538{
539 SCLPDevice *sclp = SCLP(obj);
540 Object *new;
541
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);
546
547 sclp_memory_init(sclp);
548}
549
550static void sclp_class_init(ObjectClass *oc, void *data)
551{
552 SCLPDeviceClass *sc = SCLP_CLASS(oc);
553 DeviceClass *dc = DEVICE_CLASS(oc);
554
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);
559
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;
569}
570
571static 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),
578};
579
580sclpMemoryHotplugDev *init_sclp_memory_hotplug_dev(void)
581{
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));
590}
591
592sclpMemoryHotplugDev *get_sclp_memory_hotplug_dev(void)
593{
594 return SCLP_MEMORY_HOTPLUG_DEV(object_resolve_path(
595 TYPE_SCLP_MEMORY_HOTPLUG_DEV, NULL));
596}
597
598static void sclp_memory_hotplug_dev_class_init(ObjectClass *klass,
599 void *data)
600{
601 DeviceClass *dc = DEVICE_CLASS(klass);
602
603 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
604}
605
606static 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,
611};
612
613static void register_types(void)
614{
615 type_register_static(&sclp_memory_hotplug_dev_info);
616 type_register_static(&sclp_info);
617}
618type_init(register_types);
619