4 * Copyright (c) 2010 David Gibson, IBM Corporation <dwg@au1.ibm.com>
5 * Based on the s390 virtio bus code:
6 * Copyright (c) 2009 Alexander Graf <agraf@suse.de>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include "qemu/osdep.h"
23 #include "qemu/error-report.h"
24 #include "qapi/error.h"
25 #include "qapi/visitor.h"
28 #include "hw/loader.h"
30 #include "hw/sysbus.h"
31 #include "sysemu/kvm.h"
32 #include "sysemu/device_tree.h"
34 #include "migration/vmstate.h"
35 #include "sysemu/qtest.h"
37 #include "hw/ppc/spapr.h"
38 #include "hw/ppc/spapr_vio.h"
39 #include "hw/ppc/fdt.h"
44 #define SPAPR_VIO_REG_BASE 0x71000000
46 static char *spapr_vio_get_dev_name(DeviceState
*qdev
)
48 SpaprVioDevice
*dev
= VIO_SPAPR_DEVICE(qdev
);
49 SpaprVioDeviceClass
*pc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
51 /* Device tree style name device@reg */
52 return g_strdup_printf("%s@%x", pc
->dt_name
, dev
->reg
);
55 static void spapr_vio_bus_class_init(ObjectClass
*klass
, void *data
)
57 BusClass
*k
= BUS_CLASS(klass
);
59 k
->get_dev_path
= spapr_vio_get_dev_name
;
60 k
->get_fw_dev_path
= spapr_vio_get_dev_name
;
63 static const TypeInfo spapr_vio_bus_info
= {
64 .name
= TYPE_SPAPR_VIO_BUS
,
66 .class_init
= spapr_vio_bus_class_init
,
67 .instance_size
= sizeof(SpaprVioBus
),
70 SpaprVioDevice
*spapr_vio_find_by_reg(SpaprVioBus
*bus
, uint32_t reg
)
73 SpaprVioDevice
*dev
= NULL
;
75 QTAILQ_FOREACH(kid
, &bus
->bus
.children
, sibling
) {
76 dev
= (SpaprVioDevice
*)kid
->child
;
77 if (dev
->reg
== reg
) {
85 static int vio_make_devnode(SpaprVioDevice
*dev
,
88 SpaprVioDeviceClass
*pc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
89 int vdevice_off
, node_off
, ret
;
92 vdevice_off
= fdt_path_offset(fdt
, "/vdevice");
93 if (vdevice_off
< 0) {
97 dt_name
= spapr_vio_get_dev_name(DEVICE(dev
));
98 node_off
= fdt_add_subnode(fdt
, vdevice_off
, dt_name
);
104 ret
= fdt_setprop_cell(fdt
, node_off
, "reg", dev
->reg
);
110 ret
= fdt_setprop_string(fdt
, node_off
, "device_type",
117 if (pc
->dt_compatible
) {
118 ret
= fdt_setprop_string(fdt
, node_off
, "compatible",
126 uint32_t ints_prop
[2];
128 spapr_dt_irq(ints_prop
, dev
->irq
, false);
129 ret
= fdt_setprop(fdt
, node_off
, "interrupts", ints_prop
,
136 ret
= spapr_tcet_dma_dt(fdt
, node_off
, "ibm,my-dma-window", dev
->tcet
);
142 ret
= (pc
->devnode
)(dev
, fdt
, node_off
);
154 static target_ulong
h_reg_crq(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
155 target_ulong opcode
, target_ulong
*args
)
157 target_ulong reg
= args
[0];
158 target_ulong queue_addr
= args
[1];
159 target_ulong queue_len
= args
[2];
160 SpaprVioDevice
*dev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
163 hcall_dprintf("Unit 0x" TARGET_FMT_lx
" does not exist\n", reg
);
167 /* We can't grok a queue size bigger than 256M for now */
168 if (queue_len
< 0x1000 || queue_len
> 0x10000000) {
169 hcall_dprintf("Queue size too small or too big (0x" TARGET_FMT_lx
174 /* Check queue alignment */
175 if (queue_addr
& 0xfff) {
176 hcall_dprintf("Queue not aligned (0x" TARGET_FMT_lx
")\n", queue_addr
);
180 /* Check if device supports CRQs */
181 if (!dev
->crq
.SendFunc
) {
182 hcall_dprintf("Device does not support CRQ\n");
186 /* Already a queue ? */
187 if (dev
->crq
.qsize
) {
188 hcall_dprintf("CRQ already registered\n");
191 dev
->crq
.qladdr
= queue_addr
;
192 dev
->crq
.qsize
= queue_len
;
195 trace_spapr_vio_h_reg_crq(reg
, queue_addr
, queue_len
);
199 static target_ulong
free_crq(SpaprVioDevice
*dev
)
205 trace_spapr_vio_free_crq(dev
->reg
);
210 static target_ulong
h_free_crq(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
211 target_ulong opcode
, target_ulong
*args
)
213 target_ulong reg
= args
[0];
214 SpaprVioDevice
*dev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
217 hcall_dprintf("Unit 0x" TARGET_FMT_lx
" does not exist\n", reg
);
221 return free_crq(dev
);
224 static target_ulong
h_send_crq(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
225 target_ulong opcode
, target_ulong
*args
)
227 target_ulong reg
= args
[0];
228 target_ulong msg_hi
= args
[1];
229 target_ulong msg_lo
= args
[2];
230 SpaprVioDevice
*dev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
231 uint64_t crq_mangle
[2];
234 hcall_dprintf("Unit 0x" TARGET_FMT_lx
" does not exist\n", reg
);
237 crq_mangle
[0] = cpu_to_be64(msg_hi
);
238 crq_mangle
[1] = cpu_to_be64(msg_lo
);
240 if (dev
->crq
.SendFunc
) {
241 return dev
->crq
.SendFunc(dev
, (uint8_t *)crq_mangle
);
247 static target_ulong
h_enable_crq(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
248 target_ulong opcode
, target_ulong
*args
)
250 target_ulong reg
= args
[0];
251 SpaprVioDevice
*dev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
254 hcall_dprintf("Unit 0x" TARGET_FMT_lx
" does not exist\n", reg
);
261 /* Returns negative error, 0 success, or positive: queue full */
262 int spapr_vio_send_crq(SpaprVioDevice
*dev
, uint8_t *crq
)
267 if (!dev
->crq
.qsize
) {
268 error_report("spapr_vio_send_creq on uninitialized queue");
272 /* Maybe do a fast path for KVM just writing to the pages */
273 rc
= spapr_vio_dma_read(dev
, dev
->crq
.qladdr
+ dev
->crq
.qnext
, &byte
, 1);
281 rc
= spapr_vio_dma_write(dev
, dev
->crq
.qladdr
+ dev
->crq
.qnext
+ 8,
289 rc
= spapr_vio_dma_write(dev
, dev
->crq
.qladdr
+ dev
->crq
.qnext
, crq
, 8);
294 dev
->crq
.qnext
= (dev
->crq
.qnext
+ 16) % dev
->crq
.qsize
;
296 if (dev
->signal_state
& 1) {
297 qemu_irq_pulse(spapr_vio_qirq(dev
));
303 /* "quiesce" handling */
305 static void spapr_vio_quiesce_one(SpaprVioDevice
*dev
)
308 device_reset(DEVICE(dev
->tcet
));
313 void spapr_vio_set_bypass(SpaprVioDevice
*dev
, bool bypass
)
319 memory_region_set_enabled(&dev
->mrbypass
, bypass
);
320 memory_region_set_enabled(spapr_tce_get_iommu(dev
->tcet
), !bypass
);
322 dev
->tcet
->bypass
= bypass
;
325 static void rtas_set_tce_bypass(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
327 uint32_t nargs
, target_ulong args
,
328 uint32_t nret
, target_ulong rets
)
330 SpaprVioBus
*bus
= spapr
->vio_bus
;
332 uint32_t unit
, enable
;
335 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
338 unit
= rtas_ld(args
, 0);
339 enable
= rtas_ld(args
, 1);
340 dev
= spapr_vio_find_by_reg(bus
, unit
);
342 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
347 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
351 spapr_vio_set_bypass(dev
, !!enable
);
353 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
356 static void rtas_quiesce(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
358 uint32_t nargs
, target_ulong args
,
359 uint32_t nret
, target_ulong rets
)
361 SpaprVioBus
*bus
= spapr
->vio_bus
;
363 SpaprVioDevice
*dev
= NULL
;
366 rtas_st(rets
, 0, RTAS_OUT_PARAM_ERROR
);
370 QTAILQ_FOREACH(kid
, &bus
->bus
.children
, sibling
) {
371 dev
= (SpaprVioDevice
*)kid
->child
;
372 spapr_vio_quiesce_one(dev
);
375 rtas_st(rets
, 0, RTAS_OUT_SUCCESS
);
378 static SpaprVioDevice
*reg_conflict(SpaprVioDevice
*dev
)
380 SpaprVioBus
*bus
= SPAPR_VIO_BUS(dev
->qdev
.parent_bus
);
382 SpaprVioDevice
*other
;
385 * Check for a device other than the given one which is already
386 * using the requested address. We have to open code this because
387 * the given dev might already be in the list.
389 QTAILQ_FOREACH(kid
, &bus
->bus
.children
, sibling
) {
390 other
= VIO_SPAPR_DEVICE(kid
->child
);
392 if (other
!= dev
&& other
->reg
== dev
->reg
) {
400 static void spapr_vio_busdev_reset(DeviceState
*qdev
)
402 SpaprVioDevice
*dev
= VIO_SPAPR_DEVICE(qdev
);
403 SpaprVioDeviceClass
*pc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
405 /* Shut down the request queue and TCEs if necessary */
406 spapr_vio_quiesce_one(dev
);
408 dev
->signal_state
= 0;
410 spapr_vio_set_bypass(dev
, false);
417 * The register property of a VIO device is defined in livirt using
418 * 0x1000 as a base register number plus a 0x1000 increment. For the
419 * VIO tty device, the base number is changed to 0x30000000. QEMU uses
420 * a base register number of 0x71000000 and then a simple increment.
422 * The formula below tries to compute a unique index number from the
423 * register value that will be used to define the IRQ number of the
426 * A maximum of 256 VIO devices is covered. Collisions are possible
427 * but they will be detected when the IRQ is claimed.
429 static inline uint32_t spapr_vio_reg_to_irq(uint32_t reg
)
433 if (reg
>= SPAPR_VIO_REG_BASE
) {
435 * VIO device register values when allocated by QEMU. For
436 * these, we simply mask the high bits to fit the overall
437 * range: [0x00 - 0xff].
439 * The nvram VIO device (reg=0x71000000) is a static device of
440 * the pseries machine and so is always allocated by QEMU. Its
445 } else if (reg
>= 0x30000000) {
447 * VIO tty devices register values, when allocated by livirt,
448 * are mapped in range [0xf0 - 0xff], gives us a maximum of 16
451 irq
= 0xf0 | ((reg
>> 12) & 0xf);
455 * Other VIO devices register values, when allocated by
456 * livirt, should be mapped in range [0x00 - 0xef]. Conflicts
457 * will be detected when IRQ is claimed.
459 irq
= (reg
>> 12) & 0xff;
462 return SPAPR_IRQ_VIO
| irq
;
465 static void spapr_vio_busdev_realize(DeviceState
*qdev
, Error
**errp
)
467 SpaprMachineState
*spapr
= SPAPR_MACHINE(qdev_get_machine());
468 SpaprVioDevice
*dev
= (SpaprVioDevice
*)qdev
;
469 SpaprVioDeviceClass
*pc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
471 Error
*local_err
= NULL
;
473 if (dev
->reg
!= -1) {
475 * Explicitly assigned address, just verify that no-one else
476 * is using it. other mechanism). We have to open code this
477 * rather than using spapr_vio_find_by_reg() because sdev
478 * itself is already in the list.
480 SpaprVioDevice
*other
= reg_conflict(dev
);
483 error_setg(errp
, "%s and %s devices conflict at address %#x",
484 object_get_typename(OBJECT(qdev
)),
485 object_get_typename(OBJECT(&other
->qdev
)),
490 /* Need to assign an address */
491 SpaprVioBus
*bus
= SPAPR_VIO_BUS(dev
->qdev
.parent_bus
);
494 dev
->reg
= bus
->next_reg
++;
495 } while (reg_conflict(dev
));
498 /* Don't overwrite ids assigned on the command line */
500 id
= spapr_vio_get_dev_name(DEVICE(dev
));
504 dev
->irq
= spapr_vio_reg_to_irq(dev
->reg
);
506 if (SPAPR_MACHINE_GET_CLASS(spapr
)->legacy_irq_allocation
) {
507 dev
->irq
= spapr_irq_findone(spapr
, &local_err
);
509 error_propagate(errp
, local_err
);
514 spapr_irq_claim(spapr
, dev
->irq
, false, &local_err
);
516 error_propagate(errp
, local_err
);
520 if (pc
->rtce_window_size
) {
521 uint32_t liobn
= SPAPR_VIO_LIOBN(dev
->reg
);
523 memory_region_init(&dev
->mrroot
, OBJECT(dev
), "iommu-spapr-root",
525 memory_region_init_alias(&dev
->mrbypass
, OBJECT(dev
),
526 "iommu-spapr-bypass", get_system_memory(),
528 memory_region_add_subregion_overlap(&dev
->mrroot
, 0, &dev
->mrbypass
, 1);
529 address_space_init(&dev
->as
, &dev
->mrroot
, qdev
->id
);
531 dev
->tcet
= spapr_tce_new_table(qdev
, liobn
);
532 spapr_tce_table_enable(dev
->tcet
, SPAPR_TCE_PAGE_SHIFT
, 0,
533 pc
->rtce_window_size
>> SPAPR_TCE_PAGE_SHIFT
);
534 dev
->tcet
->vdev
= dev
;
535 memory_region_add_subregion_overlap(&dev
->mrroot
, 0,
536 spapr_tce_get_iommu(dev
->tcet
), 2);
539 pc
->realize(dev
, errp
);
542 static target_ulong
h_vio_signal(PowerPCCPU
*cpu
, SpaprMachineState
*spapr
,
546 target_ulong reg
= args
[0];
547 target_ulong mode
= args
[1];
548 SpaprVioDevice
*dev
= spapr_vio_find_by_reg(spapr
->vio_bus
, reg
);
549 SpaprVioDeviceClass
*pc
;
555 pc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
557 if (mode
& ~pc
->signal_mask
) {
561 dev
->signal_state
= mode
;
566 SpaprVioBus
*spapr_vio_bus_init(void)
572 /* Create bridge device */
573 dev
= qdev_create(NULL
, TYPE_SPAPR_VIO_BRIDGE
);
574 qdev_init_nofail(dev
);
576 /* Create bus on bridge device */
577 qbus
= qbus_create(TYPE_SPAPR_VIO_BUS
, dev
, "spapr-vio");
578 bus
= SPAPR_VIO_BUS(qbus
);
579 bus
->next_reg
= SPAPR_VIO_REG_BASE
;
582 spapr_register_hypercall(H_VIO_SIGNAL
, h_vio_signal
);
585 spapr_register_hypercall(H_REG_CRQ
, h_reg_crq
);
586 spapr_register_hypercall(H_FREE_CRQ
, h_free_crq
);
587 spapr_register_hypercall(H_SEND_CRQ
, h_send_crq
);
588 spapr_register_hypercall(H_ENABLE_CRQ
, h_enable_crq
);
591 spapr_rtas_register(RTAS_IBM_SET_TCE_BYPASS
, "ibm,set-tce-bypass",
592 rtas_set_tce_bypass
);
593 spapr_rtas_register(RTAS_QUIESCE
, "quiesce", rtas_quiesce
);
598 static void spapr_vio_bridge_class_init(ObjectClass
*klass
, void *data
)
600 DeviceClass
*dc
= DEVICE_CLASS(klass
);
602 dc
->fw_name
= "vdevice";
605 static const TypeInfo spapr_vio_bridge_info
= {
606 .name
= TYPE_SPAPR_VIO_BRIDGE
,
607 .parent
= TYPE_SYS_BUS_DEVICE
,
608 .class_init
= spapr_vio_bridge_class_init
,
611 const VMStateDescription vmstate_spapr_vio
= {
614 .minimum_version_id
= 1,
615 .fields
= (VMStateField
[]) {
617 VMSTATE_UINT32_EQUAL(reg
, SpaprVioDevice
, NULL
),
618 VMSTATE_UINT32_EQUAL(irq
, SpaprVioDevice
, NULL
),
620 /* General VIO device state */
621 VMSTATE_UINT64(signal_state
, SpaprVioDevice
),
622 VMSTATE_UINT64(crq
.qladdr
, SpaprVioDevice
),
623 VMSTATE_UINT32(crq
.qsize
, SpaprVioDevice
),
624 VMSTATE_UINT32(crq
.qnext
, SpaprVioDevice
),
626 VMSTATE_END_OF_LIST()
630 static void vio_spapr_device_class_init(ObjectClass
*klass
, void *data
)
632 DeviceClass
*k
= DEVICE_CLASS(klass
);
633 k
->realize
= spapr_vio_busdev_realize
;
634 k
->reset
= spapr_vio_busdev_reset
;
635 k
->bus_type
= TYPE_SPAPR_VIO_BUS
;
638 static const TypeInfo spapr_vio_type_info
= {
639 .name
= TYPE_VIO_SPAPR_DEVICE
,
640 .parent
= TYPE_DEVICE
,
641 .instance_size
= sizeof(SpaprVioDevice
),
643 .class_size
= sizeof(SpaprVioDeviceClass
),
644 .class_init
= vio_spapr_device_class_init
,
647 static void spapr_vio_register_types(void)
649 type_register_static(&spapr_vio_bus_info
);
650 type_register_static(&spapr_vio_bridge_info
);
651 type_register_static(&spapr_vio_type_info
);
654 type_init(spapr_vio_register_types
)
656 static int compare_reg(const void *p1
, const void *p2
)
658 SpaprVioDevice
const *dev1
, *dev2
;
660 dev1
= (SpaprVioDevice
*)*(DeviceState
**)p1
;
661 dev2
= (SpaprVioDevice
*)*(DeviceState
**)p2
;
663 if (dev1
->reg
< dev2
->reg
) {
666 if (dev1
->reg
== dev2
->reg
) {
670 /* dev1->reg > dev2->reg */
674 void spapr_dt_vdevice(SpaprVioBus
*bus
, void *fdt
)
676 DeviceState
*qdev
, **qdevs
;
681 _FDT(node
= fdt_add_subnode(fdt
, 0, "vdevice"));
683 _FDT(fdt_setprop_string(fdt
, node
, "device_type", "vdevice"));
684 _FDT(fdt_setprop_string(fdt
, node
, "compatible", "IBM,vdevice"));
685 _FDT(fdt_setprop_cell(fdt
, node
, "#address-cells", 1));
686 _FDT(fdt_setprop_cell(fdt
, node
, "#size-cells", 0));
687 _FDT(fdt_setprop_cell(fdt
, node
, "#interrupt-cells", 2));
688 _FDT(fdt_setprop(fdt
, node
, "interrupt-controller", NULL
, 0));
690 /* Count qdevs on the bus list */
692 QTAILQ_FOREACH(kid
, &bus
->bus
.children
, sibling
) {
696 /* Copy out into an array of pointers */
697 qdevs
= g_new(DeviceState
*, num
);
699 QTAILQ_FOREACH(kid
, &bus
->bus
.children
, sibling
) {
700 qdevs
[num
++] = kid
->child
;
704 qsort(qdevs
, num
, sizeof(qdev
), compare_reg
);
706 /* Hack alert. Give the devices to libfdt in reverse order, we happen
707 * to know that will mean they are in forward order in the tree. */
708 for (i
= num
- 1; i
>= 0; i
--) {
709 SpaprVioDevice
*dev
= (SpaprVioDevice
*)(qdevs
[i
]);
710 SpaprVioDeviceClass
*vdc
= VIO_SPAPR_DEVICE_GET_CLASS(dev
);
712 ret
= vio_make_devnode(dev
, fdt
);
714 error_report("Couldn't create device node /vdevice/%s@%"PRIx32
,
715 vdc
->dt_name
, dev
->reg
);
723 gchar
*spapr_vio_stdout_path(SpaprVioBus
*bus
)
728 dev
= spapr_vty_get_default(bus
);
733 name
= spapr_vio_get_dev_name(DEVICE(dev
));
734 path
= g_strdup_printf("/vdevice/%s", name
);