| 1 | /* |
| 2 | * Machine for remote device |
| 3 | * |
| 4 | * This machine type is used by the remote device process in multi-process |
| 5 | * QEMU. QEMU device models depend on parent busses, interrupt controllers, |
| 6 | * memory regions, etc. The remote machine type offers this environment so |
| 7 | * that QEMU device models can be used as remote devices. |
| 8 | * |
| 9 | * Copyright © 2018, 2021 Oracle and/or its affiliates. |
| 10 | * |
| 11 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 12 | * See the COPYING file in the top-level directory. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include "qemu/osdep.h" |
| 17 | |
| 18 | #include "hw/remote/machine.h" |
| 19 | #include "system/memory.h" |
| 20 | #include "qapi/error.h" |
| 21 | #include "hw/pci/pci_host.h" |
| 22 | #include "hw/remote/iohub.h" |
| 23 | #include "hw/remote/iommu.h" |
| 24 | #include "hw/core/qdev.h" |
| 25 | #include "hw/remote/vfio-user-obj.h" |
| 26 | #include "hw/pci/msi.h" |
| 27 | #include "hw/arm/machines-qom.h" |
| 28 | |
| 29 | static void remote_machine_init(MachineState *machine) |
| 30 | { |
| 31 | MemoryRegion *system_memory, *system_io, *pci_memory; |
| 32 | RemoteMachineState *s = REMOTE_MACHINE(machine); |
| 33 | RemotePCIHost *rem_host; |
| 34 | PCIHostState *pci_host; |
| 35 | |
| 36 | system_memory = get_system_memory(); |
| 37 | system_io = get_system_io(); |
| 38 | |
| 39 | pci_memory = g_new(MemoryRegion, 1); |
| 40 | memory_region_init(pci_memory, NULL, "pci", UINT64_MAX); |
| 41 | |
| 42 | rem_host = REMOTE_PCIHOST(qdev_new(TYPE_REMOTE_PCIHOST)); |
| 43 | |
| 44 | rem_host->mr_pci_mem = pci_memory; |
| 45 | rem_host->mr_sys_mem = system_memory; |
| 46 | rem_host->mr_sys_io = system_io; |
| 47 | |
| 48 | s->host = rem_host; |
| 49 | |
| 50 | object_property_add_child(OBJECT(s), "remote-pcihost", OBJECT(rem_host)); |
| 51 | memory_region_add_subregion_overlap(system_memory, 0x0, pci_memory, -1); |
| 52 | |
| 53 | qdev_realize(DEVICE(rem_host), sysbus_get_default(), &error_fatal); |
| 54 | |
| 55 | pci_host = PCI_HOST_BRIDGE(rem_host); |
| 56 | |
| 57 | if (s->vfio_user) { |
| 58 | remote_iommu_setup(pci_host->bus); |
| 59 | |
| 60 | msi_nonbroken = true; |
| 61 | |
| 62 | vfu_object_set_bus_irq(pci_host->bus); |
| 63 | } else { |
| 64 | remote_iohub_init(&s->iohub); |
| 65 | |
| 66 | pci_bus_irqs(pci_host->bus, remote_iohub_set_irq, |
| 67 | &s->iohub, REMOTE_IOHUB_NB_PIRQS); |
| 68 | pci_bus_map_irqs(pci_host->bus, remote_iohub_map_irq); |
| 69 | } |
| 70 | |
| 71 | qbus_set_hotplug_handler(BUS(pci_host->bus), OBJECT(s)); |
| 72 | } |
| 73 | |
| 74 | static bool remote_machine_get_vfio_user(Object *obj, Error **errp) |
| 75 | { |
| 76 | RemoteMachineState *s = REMOTE_MACHINE(obj); |
| 77 | |
| 78 | return s->vfio_user; |
| 79 | } |
| 80 | |
| 81 | static void remote_machine_set_vfio_user(Object *obj, bool value, Error **errp) |
| 82 | { |
| 83 | RemoteMachineState *s = REMOTE_MACHINE(obj); |
| 84 | |
| 85 | if (phase_check(PHASE_MACHINE_CREATED)) { |
| 86 | error_setg(errp, "Error enabling vfio-user - machine already created"); |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | s->vfio_user = value; |
| 91 | } |
| 92 | |
| 93 | static bool remote_machine_get_auto_shutdown(Object *obj, Error **errp) |
| 94 | { |
| 95 | RemoteMachineState *s = REMOTE_MACHINE(obj); |
| 96 | |
| 97 | return s->auto_shutdown; |
| 98 | } |
| 99 | |
| 100 | static void remote_machine_set_auto_shutdown(Object *obj, bool value, |
| 101 | Error **errp) |
| 102 | { |
| 103 | RemoteMachineState *s = REMOTE_MACHINE(obj); |
| 104 | |
| 105 | s->auto_shutdown = value; |
| 106 | } |
| 107 | |
| 108 | static void remote_machine_instance_init(Object *obj) |
| 109 | { |
| 110 | RemoteMachineState *s = REMOTE_MACHINE(obj); |
| 111 | |
| 112 | s->auto_shutdown = true; |
| 113 | } |
| 114 | |
| 115 | static void remote_machine_dev_unplug_cb(HotplugHandler *hotplug_dev, |
| 116 | DeviceState *dev, Error **errp) |
| 117 | { |
| 118 | qdev_unrealize(dev); |
| 119 | |
| 120 | if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_DEVICE)) { |
| 121 | remote_iommu_unplug_dev(PCI_DEVICE(dev)); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | static void remote_machine_class_init(ObjectClass *oc, const void *data) |
| 126 | { |
| 127 | MachineClass *mc = MACHINE_CLASS(oc); |
| 128 | HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(oc); |
| 129 | |
| 130 | mc->init = remote_machine_init; |
| 131 | mc->desc = "Experimental remote machine"; |
| 132 | |
| 133 | hc->unplug = remote_machine_dev_unplug_cb; |
| 134 | |
| 135 | object_class_property_add_bool(oc, "vfio-user", |
| 136 | remote_machine_get_vfio_user, |
| 137 | remote_machine_set_vfio_user); |
| 138 | |
| 139 | object_class_property_add_bool(oc, "auto-shutdown", |
| 140 | remote_machine_get_auto_shutdown, |
| 141 | remote_machine_set_auto_shutdown); |
| 142 | } |
| 143 | |
| 144 | static const TypeInfo remote_machine = { |
| 145 | .name = TYPE_REMOTE_MACHINE, |
| 146 | .parent = TYPE_MACHINE, |
| 147 | .instance_size = sizeof(RemoteMachineState), |
| 148 | .instance_init = remote_machine_instance_init, |
| 149 | .class_init = remote_machine_class_init, |
| 150 | .interfaces = (const InterfaceInfo[]) { |
| 151 | { TYPE_HOTPLUG_HANDLER }, |
| 152 | { TYPE_TARGET_AARCH64_MACHINE }, |
| 153 | { TYPE_TARGET_ARM_MACHINE }, |
| 154 | { } |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | static void remote_machine_register_types(void) |
| 159 | { |
| 160 | type_register_static(&remote_machine); |
| 161 | } |
| 162 | |
| 163 | type_init(remote_machine_register_types); |