David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Virtio MEM PCI device |
| 3 | * |
| 4 | * Copyright (C) 2020 Red Hat, Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * David Hildenbrand <david@redhat.com> |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | |
| 13 | #include "qemu/osdep.h" |
| 14 | #include "virtio-mem-pci.h" |
| 15 | #include "hw/mem/memory-device.h" |
| 16 | #include "qapi/error.h" |
Philippe Mathieu-Daudé | b495ec6 | 2020-09-13 21:53:46 +0200 | [diff] [blame] | 17 | #include "qapi/qapi-events-machine.h" |
David Hildenbrand | 722a3c7 | 2020-06-26 09:22:44 +0200 | [diff] [blame] | 18 | #include "qapi/qapi-events-misc.h" |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 19 | |
| 20 | static void virtio_mem_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp) |
| 21 | { |
| 22 | VirtIOMEMPCI *mem_pci = VIRTIO_MEM_PCI(vpci_dev); |
| 23 | DeviceState *vdev = DEVICE(&mem_pci->vdev); |
| 24 | |
David Hildenbrand | c22aba1 | 2020-07-27 13:59:05 +0200 | [diff] [blame] | 25 | virtio_pci_force_virtio_1(vpci_dev); |
| 26 | qdev_realize(vdev, BUS(&vpci_dev->bus), errp); |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | static void virtio_mem_pci_set_addr(MemoryDeviceState *md, uint64_t addr, |
| 30 | Error **errp) |
| 31 | { |
Markus Armbruster | 5325cc3 | 2020-07-07 18:05:54 +0200 | [diff] [blame] | 32 | object_property_set_uint(OBJECT(md), VIRTIO_MEM_ADDR_PROP, addr, errp); |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static uint64_t virtio_mem_pci_get_addr(const MemoryDeviceState *md) |
| 36 | { |
| 37 | return object_property_get_uint(OBJECT(md), VIRTIO_MEM_ADDR_PROP, |
| 38 | &error_abort); |
| 39 | } |
| 40 | |
| 41 | static MemoryRegion *virtio_mem_pci_get_memory_region(MemoryDeviceState *md, |
| 42 | Error **errp) |
| 43 | { |
| 44 | VirtIOMEMPCI *pci_mem = VIRTIO_MEM_PCI(md); |
Philippe Mathieu-Daudé | 7d5b0d6 | 2023-06-01 11:34:52 +0200 | [diff] [blame] | 45 | VirtIOMEM *vmem = &pci_mem->vdev; |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 46 | VirtIOMEMClass *vmc = VIRTIO_MEM_GET_CLASS(vmem); |
| 47 | |
| 48 | return vmc->get_memory_region(vmem, errp); |
| 49 | } |
| 50 | |
| 51 | static uint64_t virtio_mem_pci_get_plugged_size(const MemoryDeviceState *md, |
| 52 | Error **errp) |
| 53 | { |
| 54 | return object_property_get_uint(OBJECT(md), VIRTIO_MEM_SIZE_PROP, |
| 55 | errp); |
| 56 | } |
| 57 | |
| 58 | static void virtio_mem_pci_fill_device_info(const MemoryDeviceState *md, |
| 59 | MemoryDeviceInfo *info) |
| 60 | { |
| 61 | VirtioMEMDeviceInfo *vi = g_new0(VirtioMEMDeviceInfo, 1); |
| 62 | VirtIOMEMPCI *pci_mem = VIRTIO_MEM_PCI(md); |
Philippe Mathieu-Daudé | 7d5b0d6 | 2023-06-01 11:34:52 +0200 | [diff] [blame] | 63 | VirtIOMEM *vmem = &pci_mem->vdev; |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 64 | VirtIOMEMClass *vpc = VIRTIO_MEM_GET_CLASS(vmem); |
| 65 | DeviceState *dev = DEVICE(md); |
| 66 | |
| 67 | if (dev->id) { |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 68 | vi->id = g_strdup(dev->id); |
| 69 | } |
| 70 | |
| 71 | /* let the real device handle everything else */ |
| 72 | vpc->fill_device_info(vmem, vi); |
| 73 | |
| 74 | info->u.virtio_mem.data = vi; |
| 75 | info->type = MEMORY_DEVICE_INFO_KIND_VIRTIO_MEM; |
| 76 | } |
| 77 | |
David Hildenbrand | 296e88f | 2020-10-08 10:30:29 +0200 | [diff] [blame] | 78 | static uint64_t virtio_mem_pci_get_min_alignment(const MemoryDeviceState *md) |
| 79 | { |
| 80 | return object_property_get_uint(OBJECT(md), VIRTIO_MEM_BLOCK_SIZE_PROP, |
| 81 | &error_abort); |
| 82 | } |
| 83 | |
David Hildenbrand | 722a3c7 | 2020-06-26 09:22:44 +0200 | [diff] [blame] | 84 | static void virtio_mem_pci_size_change_notify(Notifier *notifier, void *data) |
| 85 | { |
| 86 | VirtIOMEMPCI *pci_mem = container_of(notifier, VirtIOMEMPCI, |
| 87 | size_change_notifier); |
| 88 | DeviceState *dev = DEVICE(pci_mem); |
David Hildenbrand | d89dd28 | 2021-09-29 18:24:44 +0200 | [diff] [blame] | 89 | char *qom_path = object_get_canonical_path(OBJECT(dev)); |
David Hildenbrand | 722a3c7 | 2020-06-26 09:22:44 +0200 | [diff] [blame] | 90 | const uint64_t * const size_p = data; |
David Hildenbrand | 722a3c7 | 2020-06-26 09:22:44 +0200 | [diff] [blame] | 91 | |
Markus Armbruster | fe8ac1f | 2022-11-04 17:06:57 +0100 | [diff] [blame] | 92 | qapi_event_send_memory_device_size_change(dev->id, *size_p, qom_path); |
David Hildenbrand | d89dd28 | 2021-09-29 18:24:44 +0200 | [diff] [blame] | 93 | g_free(qom_path); |
David Hildenbrand | 722a3c7 | 2020-06-26 09:22:44 +0200 | [diff] [blame] | 94 | } |
| 95 | |
David Hildenbrand | 339a8bb | 2023-07-11 17:34:45 +0200 | [diff] [blame] | 96 | static void virtio_mem_pci_unplug_request_check(VirtIOMDPCI *vmd, Error **errp) |
| 97 | { |
| 98 | VirtIOMEMPCI *pci_mem = VIRTIO_MEM_PCI(vmd); |
| 99 | VirtIOMEM *vmem = &pci_mem->vdev; |
| 100 | VirtIOMEMClass *vpc = VIRTIO_MEM_GET_CLASS(vmem); |
| 101 | |
| 102 | vpc->unplug_request_check(vmem, errp); |
| 103 | } |
| 104 | |
| 105 | static void virtio_mem_pci_get_requested_size(Object *obj, Visitor *v, |
| 106 | const char *name, void *opaque, |
| 107 | Error **errp) |
| 108 | { |
| 109 | VirtIOMEMPCI *pci_mem = VIRTIO_MEM_PCI(obj); |
| 110 | |
| 111 | object_property_get(OBJECT(&pci_mem->vdev), name, v, errp); |
| 112 | } |
| 113 | |
| 114 | static void virtio_mem_pci_set_requested_size(Object *obj, Visitor *v, |
| 115 | const char *name, void *opaque, |
| 116 | Error **errp) |
| 117 | { |
| 118 | VirtIOMEMPCI *pci_mem = VIRTIO_MEM_PCI(obj); |
| 119 | DeviceState *dev = DEVICE(obj); |
| 120 | |
| 121 | /* |
| 122 | * If we passed virtio_mem_pci_unplug_request_check(), making sure that |
| 123 | * the requested size is 0, don't allow modifying the requested size |
| 124 | * anymore, otherwise the VM might end up hotplugging memory before |
| 125 | * handling the unplug request. |
| 126 | */ |
| 127 | if (dev->pending_deleted_event) { |
| 128 | error_setg(errp, "'%s' cannot be changed if the device is in the" |
| 129 | " process of unplug", name); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | object_property_set(OBJECT(&pci_mem->vdev), name, v, errp); |
| 134 | } |
| 135 | |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 136 | static void virtio_mem_pci_class_init(ObjectClass *klass, void *data) |
| 137 | { |
| 138 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 139 | VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass); |
| 140 | PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass); |
| 141 | MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(klass); |
David Hildenbrand | 339a8bb | 2023-07-11 17:34:45 +0200 | [diff] [blame] | 142 | VirtIOMDPCIClass *vmdc = VIRTIO_MD_PCI_CLASS(klass); |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 143 | |
| 144 | k->realize = virtio_mem_pci_realize; |
| 145 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 146 | pcidev_k->revision = VIRTIO_PCI_ABI_VERSION; |
| 147 | pcidev_k->class_id = PCI_CLASS_OTHERS; |
| 148 | |
| 149 | mdc->get_addr = virtio_mem_pci_get_addr; |
| 150 | mdc->set_addr = virtio_mem_pci_set_addr; |
| 151 | mdc->get_plugged_size = virtio_mem_pci_get_plugged_size; |
| 152 | mdc->get_memory_region = virtio_mem_pci_get_memory_region; |
| 153 | mdc->fill_device_info = virtio_mem_pci_fill_device_info; |
David Hildenbrand | 296e88f | 2020-10-08 10:30:29 +0200 | [diff] [blame] | 154 | mdc->get_min_alignment = virtio_mem_pci_get_min_alignment; |
David Hildenbrand | 339a8bb | 2023-07-11 17:34:45 +0200 | [diff] [blame] | 155 | |
| 156 | vmdc->unplug_request_check = virtio_mem_pci_unplug_request_check; |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void virtio_mem_pci_instance_init(Object *obj) |
| 160 | { |
| 161 | VirtIOMEMPCI *dev = VIRTIO_MEM_PCI(obj); |
David Hildenbrand | 722a3c7 | 2020-06-26 09:22:44 +0200 | [diff] [blame] | 162 | VirtIOMEMClass *vmc; |
| 163 | VirtIOMEM *vmem; |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 164 | |
| 165 | virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), |
| 166 | TYPE_VIRTIO_MEM); |
David Hildenbrand | 722a3c7 | 2020-06-26 09:22:44 +0200 | [diff] [blame] | 167 | |
| 168 | dev->size_change_notifier.notify = virtio_mem_pci_size_change_notify; |
Philippe Mathieu-Daudé | 7d5b0d6 | 2023-06-01 11:34:52 +0200 | [diff] [blame] | 169 | vmem = &dev->vdev; |
David Hildenbrand | 722a3c7 | 2020-06-26 09:22:44 +0200 | [diff] [blame] | 170 | vmc = VIRTIO_MEM_GET_CLASS(vmem); |
| 171 | /* |
| 172 | * We never remove the notifier again, as we expect both devices to |
| 173 | * disappear at the same time. |
| 174 | */ |
| 175 | vmc->add_size_change_notifier(vmem, &dev->size_change_notifier); |
| 176 | |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 177 | object_property_add_alias(obj, VIRTIO_MEM_BLOCK_SIZE_PROP, |
| 178 | OBJECT(&dev->vdev), VIRTIO_MEM_BLOCK_SIZE_PROP); |
| 179 | object_property_add_alias(obj, VIRTIO_MEM_SIZE_PROP, OBJECT(&dev->vdev), |
| 180 | VIRTIO_MEM_SIZE_PROP); |
David Hildenbrand | 339a8bb | 2023-07-11 17:34:45 +0200 | [diff] [blame] | 181 | object_property_add(obj, VIRTIO_MEM_REQUESTED_SIZE_PROP, "size", |
| 182 | virtio_mem_pci_get_requested_size, |
| 183 | virtio_mem_pci_set_requested_size, NULL, NULL); |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static const VirtioPCIDeviceTypeInfo virtio_mem_pci_info = { |
| 187 | .base_name = TYPE_VIRTIO_MEM_PCI, |
David Hildenbrand | 18129c1 | 2023-07-11 17:34:39 +0200 | [diff] [blame] | 188 | .parent = TYPE_VIRTIO_MD_PCI, |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 189 | .generic_name = "virtio-mem-pci", |
| 190 | .instance_size = sizeof(VirtIOMEMPCI), |
| 191 | .instance_init = virtio_mem_pci_instance_init, |
| 192 | .class_init = virtio_mem_pci_class_init, |
David Hildenbrand | 0b9a244 | 2020-06-26 09:22:38 +0200 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | static void virtio_mem_pci_register_types(void) |
| 196 | { |
| 197 | virtio_pci_types_register(&virtio_mem_pci_info); |
| 198 | } |
| 199 | type_init(virtio_mem_pci_register_types) |