1 /* 2 * virtio-mem CCW implementation 3 * 4 * Copyright (C) 2024 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 "hw/qdev-properties.h" 15 #include "qapi/error.h" 16 #include "qemu/module.h" 17 #include "virtio-ccw-mem.h" 18 #include "hw/mem/memory-device.h" 19 #include "qapi/qapi-events-machine.h" 20 #include "qapi/qapi-events-misc.h" 21 22 static void virtio_ccw_mem_realize(VirtioCcwDevice *ccw_dev, Error **errp) 23 { 24 VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(ccw_dev); 25 DeviceState *vdev = DEVICE(&dev->vdev); 26 27 qdev_realize(vdev, BUS(&ccw_dev->bus), errp); 28 } 29 30 static void virtio_ccw_mem_set_addr(MemoryDeviceState *md, uint64_t addr, 31 Error **errp) 32 { 33 object_property_set_uint(OBJECT(md), VIRTIO_MEM_ADDR_PROP, addr, errp); 34 } 35 36 static uint64_t virtio_ccw_mem_get_addr(const MemoryDeviceState *md) 37 { 38 return object_property_get_uint(OBJECT(md), VIRTIO_MEM_ADDR_PROP, 39 &error_abort); 40 } 41 42 static MemoryRegion *virtio_ccw_mem_get_memory_region(MemoryDeviceState *md, 43 Error **errp) 44 { 45 VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(md); 46 VirtIOMEM *vmem = &dev->vdev; 47 VirtIOMEMClass *vmc = VIRTIO_MEM_GET_CLASS(vmem); 48 49 return vmc->get_memory_region(vmem, errp); 50 } 51 52 static void virtio_ccw_mem_decide_memslots(MemoryDeviceState *md, 53 unsigned int limit) 54 { 55 VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(md); 56 VirtIOMEM *vmem = VIRTIO_MEM(&dev->vdev); 57 VirtIOMEMClass *vmc = VIRTIO_MEM_GET_CLASS(vmem); 58 59 vmc->decide_memslots(vmem, limit); 60 } 61 62 static unsigned int virtio_ccw_mem_get_memslots(MemoryDeviceState *md) 63 { 64 VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(md); 65 VirtIOMEM *vmem = VIRTIO_MEM(&dev->vdev); 66 VirtIOMEMClass *vmc = VIRTIO_MEM_GET_CLASS(vmem); 67 68 return vmc->get_memslots(vmem); 69 } 70 71 static uint64_t virtio_ccw_mem_get_plugged_size(const MemoryDeviceState *md, 72 Error **errp) 73 { 74 return object_property_get_uint(OBJECT(md), VIRTIO_MEM_SIZE_PROP, 75 errp); 76 } 77 78 static void virtio_ccw_mem_fill_device_info(const MemoryDeviceState *md, 79 MemoryDeviceInfo *info) 80 { 81 VirtioMEMDeviceInfo *vi = g_new0(VirtioMEMDeviceInfo, 1); 82 VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(md); 83 VirtIOMEM *vmem = &dev->vdev; 84 VirtIOMEMClass *vpc = VIRTIO_MEM_GET_CLASS(vmem); 85 DeviceState *vdev = DEVICE(md); 86 87 if (vdev->id) { 88 vi->id = g_strdup(vdev->id); 89 } 90 91 /* let the real device handle everything else */ 92 vpc->fill_device_info(vmem, vi); 93 94 info->u.virtio_mem.data = vi; 95 info->type = MEMORY_DEVICE_INFO_KIND_VIRTIO_MEM; 96 } 97 98 static uint64_t virtio_ccw_mem_get_min_alignment(const MemoryDeviceState *md) 99 { 100 return object_property_get_uint(OBJECT(md), VIRTIO_MEM_BLOCK_SIZE_PROP, 101 &error_abort); 102 } 103 104 static void virtio_ccw_mem_size_change_notify(Notifier *notifier, void *data) 105 { 106 VirtIOMEMCcw *dev = container_of(notifier, VirtIOMEMCcw, 107 size_change_notifier); 108 DeviceState *vdev = DEVICE(dev); 109 char *qom_path = object_get_canonical_path(OBJECT(dev)); 110 const uint64_t * const size_p = data; 111 112 qapi_event_send_memory_device_size_change(vdev->id, *size_p, qom_path); 113 g_free(qom_path); 114 } 115 116 static void virtio_ccw_mem_unplug_request_check(VirtIOMDCcw *vmd, Error **errp) 117 { 118 VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(vmd); 119 VirtIOMEM *vmem = &dev->vdev; 120 VirtIOMEMClass *vpc = VIRTIO_MEM_GET_CLASS(vmem); 121 122 vpc->unplug_request_check(vmem, errp); 123 } 124 125 static void virtio_ccw_mem_get_requested_size(Object *obj, Visitor *v, 126 const char *name, void *opaque, 127 Error **errp) 128 { 129 VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(obj); 130 131 object_property_get(OBJECT(&dev->vdev), name, v, errp); 132 } 133 134 static void virtio_ccw_mem_set_requested_size(Object *obj, Visitor *v, 135 const char *name, void *opaque, 136 Error **errp) 137 { 138 VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(obj); 139 DeviceState *vdev = DEVICE(obj); 140 141 /* 142 * If we passed virtio_ccw_mem_unplug_request_check(), making sure that 143 * the requested size is 0, don't allow modifying the requested size 144 * anymore, otherwise the VM might end up hotplugging memory before 145 * handling the unplug request. 146 */ 147 if (vdev->pending_deleted_event) { 148 error_setg(errp, "'%s' cannot be changed if the device is in the" 149 " process of unplug", name); 150 return; 151 } 152 153 object_property_set(OBJECT(&dev->vdev), name, v, errp); 154 } 155 156 static const Property virtio_ccw_mem_properties[] = { 157 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 158 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 159 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev, 160 VIRTIO_CCW_MAX_REV), 161 }; 162 163 static void virtio_ccw_mem_class_init(ObjectClass *klass, void *data) 164 { 165 DeviceClass *dc = DEVICE_CLASS(klass); 166 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 167 MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(klass); 168 VirtIOMDCcwClass *vmdc = VIRTIO_MD_CCW_CLASS(klass); 169 170 k->realize = virtio_ccw_mem_realize; 171 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 172 device_class_set_props(dc, virtio_ccw_mem_properties); 173 174 mdc->get_addr = virtio_ccw_mem_get_addr; 175 mdc->set_addr = virtio_ccw_mem_set_addr; 176 mdc->get_plugged_size = virtio_ccw_mem_get_plugged_size; 177 mdc->get_memory_region = virtio_ccw_mem_get_memory_region; 178 mdc->decide_memslots = virtio_ccw_mem_decide_memslots; 179 mdc->get_memslots = virtio_ccw_mem_get_memslots; 180 mdc->fill_device_info = virtio_ccw_mem_fill_device_info; 181 mdc->get_min_alignment = virtio_ccw_mem_get_min_alignment; 182 183 vmdc->unplug_request_check = virtio_ccw_mem_unplug_request_check; 184 } 185 186 static void virtio_ccw_mem_instance_init(Object *obj) 187 { 188 VirtIOMEMCcw *dev = VIRTIO_MEM_CCW(obj); 189 VirtIOMEMClass *vmc; 190 VirtIOMEM *vmem; 191 192 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 193 TYPE_VIRTIO_MEM); 194 195 dev->size_change_notifier.notify = virtio_ccw_mem_size_change_notify; 196 vmem = &dev->vdev; 197 vmc = VIRTIO_MEM_GET_CLASS(vmem); 198 /* 199 * We never remove the notifier again, as we expect both devices to 200 * disappear at the same time. 201 */ 202 vmc->add_size_change_notifier(vmem, &dev->size_change_notifier); 203 204 object_property_add_alias(obj, VIRTIO_MEM_BLOCK_SIZE_PROP, 205 OBJECT(&dev->vdev), VIRTIO_MEM_BLOCK_SIZE_PROP); 206 object_property_add_alias(obj, VIRTIO_MEM_SIZE_PROP, OBJECT(&dev->vdev), 207 VIRTIO_MEM_SIZE_PROP); 208 object_property_add(obj, VIRTIO_MEM_REQUESTED_SIZE_PROP, "size", 209 virtio_ccw_mem_get_requested_size, 210 virtio_ccw_mem_set_requested_size, NULL, NULL); 211 } 212 213 static const TypeInfo virtio_ccw_mem = { 214 .name = TYPE_VIRTIO_MEM_CCW, 215 .parent = TYPE_VIRTIO_MD_CCW, 216 .instance_size = sizeof(VirtIOMEMCcw), 217 .instance_init = virtio_ccw_mem_instance_init, 218 .class_init = virtio_ccw_mem_class_init, 219 }; 220 221 static void virtio_ccw_mem_register_types(void) 222 { 223 type_register_static(&virtio_ccw_mem); 224 } 225 type_init(virtio_ccw_mem_register_types) 226