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"
17 #include "qapi/qapi-events-machine.h"
18 #include "qapi/qapi-events-misc.h"
19
virtio_mem_pci_realize(VirtIOPCIProxy * vpci_dev,Error ** errp)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
25 if (vpci_dev->nvectors == DEV_NVECTORS_UNSPECIFIED) {
26 vpci_dev->nvectors = 2;
27 }
28
29 virtio_pci_force_virtio_1(vpci_dev);
30 qdev_realize(vdev, BUS(&vpci_dev->bus), errp);
31 }
32
virtio_mem_pci_set_addr(MemoryDeviceState * md,uint64_t addr,Error ** errp)33 static void virtio_mem_pci_set_addr(MemoryDeviceState *md, uint64_t addr,
34 Error **errp)
35 {
36 object_property_set_uint(OBJECT(md), VIRTIO_MEM_ADDR_PROP, addr, errp);
37 }
38
virtio_mem_pci_get_addr(const MemoryDeviceState * md)39 static uint64_t virtio_mem_pci_get_addr(const MemoryDeviceState *md)
40 {
41 return object_property_get_uint(OBJECT(md), VIRTIO_MEM_ADDR_PROP,
42 &error_abort);
43 }
44
virtio_mem_pci_get_memory_region(MemoryDeviceState * md,Error ** errp)45 static MemoryRegion *virtio_mem_pci_get_memory_region(MemoryDeviceState *md,
46 Error **errp)
47 {
48 VirtIOMEMPCI *pci_mem = VIRTIO_MEM_PCI(md);
49 VirtIOMEM *vmem = &pci_mem->vdev;
50 VirtIOMEMClass *vmc = VIRTIO_MEM_GET_CLASS(vmem);
51
52 return vmc->get_memory_region(vmem, errp);
53 }
54
virtio_mem_pci_decide_memslots(MemoryDeviceState * md,unsigned int limit)55 static void virtio_mem_pci_decide_memslots(MemoryDeviceState *md,
56 unsigned int limit)
57 {
58 VirtIOMEMPCI *pci_mem = VIRTIO_MEM_PCI(md);
59 VirtIOMEM *vmem = VIRTIO_MEM(&pci_mem->vdev);
60 VirtIOMEMClass *vmc = VIRTIO_MEM_GET_CLASS(vmem);
61
62 vmc->decide_memslots(vmem, limit);
63 }
64
virtio_mem_pci_get_memslots(MemoryDeviceState * md)65 static unsigned int virtio_mem_pci_get_memslots(MemoryDeviceState *md)
66 {
67 VirtIOMEMPCI *pci_mem = VIRTIO_MEM_PCI(md);
68 VirtIOMEM *vmem = VIRTIO_MEM(&pci_mem->vdev);
69 VirtIOMEMClass *vmc = VIRTIO_MEM_GET_CLASS(vmem);
70
71 return vmc->get_memslots(vmem);
72 }
73
virtio_mem_pci_get_plugged_size(const MemoryDeviceState * md,Error ** errp)74 static uint64_t virtio_mem_pci_get_plugged_size(const MemoryDeviceState *md,
75 Error **errp)
76 {
77 return object_property_get_uint(OBJECT(md), VIRTIO_MEM_SIZE_PROP,
78 errp);
79 }
80
virtio_mem_pci_fill_device_info(const MemoryDeviceState * md,MemoryDeviceInfo * info)81 static void virtio_mem_pci_fill_device_info(const MemoryDeviceState *md,
82 MemoryDeviceInfo *info)
83 {
84 VirtioMEMDeviceInfo *vi = g_new0(VirtioMEMDeviceInfo, 1);
85 VirtIOMEMPCI *pci_mem = VIRTIO_MEM_PCI(md);
86 VirtIOMEM *vmem = &pci_mem->vdev;
87 VirtIOMEMClass *vpc = VIRTIO_MEM_GET_CLASS(vmem);
88 DeviceState *dev = DEVICE(md);
89
90 if (dev->id) {
91 vi->id = g_strdup(dev->id);
92 }
93
94 /* let the real device handle everything else */
95 vpc->fill_device_info(vmem, vi);
96
97 info->u.virtio_mem.data = vi;
98 info->type = MEMORY_DEVICE_INFO_KIND_VIRTIO_MEM;
99 }
100
virtio_mem_pci_get_min_alignment(const MemoryDeviceState * md)101 static uint64_t virtio_mem_pci_get_min_alignment(const MemoryDeviceState *md)
102 {
103 return object_property_get_uint(OBJECT(md), VIRTIO_MEM_BLOCK_SIZE_PROP,
104 &error_abort);
105 }
106
virtio_mem_pci_size_change_notify(Notifier * notifier,void * data)107 static void virtio_mem_pci_size_change_notify(Notifier *notifier, void *data)
108 {
109 VirtIOMEMPCI *pci_mem = container_of(notifier, VirtIOMEMPCI,
110 size_change_notifier);
111 DeviceState *dev = DEVICE(pci_mem);
112 char *qom_path = object_get_canonical_path(OBJECT(dev));
113 const uint64_t * const size_p = data;
114
115 qapi_event_send_memory_device_size_change(dev->id, *size_p, qom_path);
116 g_free(qom_path);
117 }
118
virtio_mem_pci_unplug_request_check(VirtIOMDPCI * vmd,Error ** errp)119 static void virtio_mem_pci_unplug_request_check(VirtIOMDPCI *vmd, Error **errp)
120 {
121 VirtIOMEMPCI *pci_mem = VIRTIO_MEM_PCI(vmd);
122 VirtIOMEM *vmem = &pci_mem->vdev;
123 VirtIOMEMClass *vpc = VIRTIO_MEM_GET_CLASS(vmem);
124
125 vpc->unplug_request_check(vmem, errp);
126 }
127
virtio_mem_pci_get_requested_size(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)128 static void virtio_mem_pci_get_requested_size(Object *obj, Visitor *v,
129 const char *name, void *opaque,
130 Error **errp)
131 {
132 VirtIOMEMPCI *pci_mem = VIRTIO_MEM_PCI(obj);
133
134 object_property_get(OBJECT(&pci_mem->vdev), name, v, errp);
135 }
136
virtio_mem_pci_set_requested_size(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)137 static void virtio_mem_pci_set_requested_size(Object *obj, Visitor *v,
138 const char *name, void *opaque,
139 Error **errp)
140 {
141 VirtIOMEMPCI *pci_mem = VIRTIO_MEM_PCI(obj);
142 DeviceState *dev = DEVICE(obj);
143
144 /*
145 * If we passed virtio_mem_pci_unplug_request_check(), making sure that
146 * the requested size is 0, don't allow modifying the requested size
147 * anymore, otherwise the VM might end up hotplugging memory before
148 * handling the unplug request.
149 */
150 if (dev->pending_deleted_event) {
151 error_setg(errp, "'%s' cannot be changed if the device is in the"
152 " process of unplug", name);
153 return;
154 }
155
156 object_property_set(OBJECT(&pci_mem->vdev), name, v, errp);
157 }
158
159 static const Property virtio_mem_pci_class_properties[] = {
160 DEFINE_PROP_BIT("ioeventfd", VirtIOPCIProxy, flags,
161 VIRTIO_PCI_FLAG_USE_IOEVENTFD_BIT, true),
162 DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors,
163 DEV_NVECTORS_UNSPECIFIED),
164 };
165
virtio_mem_pci_class_init(ObjectClass * klass,const void * data)166 static void virtio_mem_pci_class_init(ObjectClass *klass, const void *data)
167 {
168 DeviceClass *dc = DEVICE_CLASS(klass);
169 VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
170 PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
171 MemoryDeviceClass *mdc = MEMORY_DEVICE_CLASS(klass);
172 VirtIOMDPCIClass *vmdc = VIRTIO_MD_PCI_CLASS(klass);
173
174 k->realize = virtio_mem_pci_realize;
175 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
176 pcidev_k->revision = VIRTIO_PCI_ABI_VERSION;
177 pcidev_k->class_id = PCI_CLASS_OTHERS;
178 device_class_set_props(dc, virtio_mem_pci_class_properties);
179
180 mdc->get_addr = virtio_mem_pci_get_addr;
181 mdc->set_addr = virtio_mem_pci_set_addr;
182 mdc->get_plugged_size = virtio_mem_pci_get_plugged_size;
183 mdc->get_memory_region = virtio_mem_pci_get_memory_region;
184 mdc->decide_memslots = virtio_mem_pci_decide_memslots;
185 mdc->get_memslots = virtio_mem_pci_get_memslots;
186 mdc->fill_device_info = virtio_mem_pci_fill_device_info;
187 mdc->get_min_alignment = virtio_mem_pci_get_min_alignment;
188
189 vmdc->unplug_request_check = virtio_mem_pci_unplug_request_check;
190 }
191
virtio_mem_pci_instance_init(Object * obj)192 static void virtio_mem_pci_instance_init(Object *obj)
193 {
194 VirtIOMEMPCI *dev = VIRTIO_MEM_PCI(obj);
195 VirtIOMEMClass *vmc;
196 VirtIOMEM *vmem;
197
198 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
199 TYPE_VIRTIO_MEM);
200
201 dev->size_change_notifier.notify = virtio_mem_pci_size_change_notify;
202 vmem = &dev->vdev;
203 vmc = VIRTIO_MEM_GET_CLASS(vmem);
204 /*
205 * We never remove the notifier again, as we expect both devices to
206 * disappear at the same time.
207 */
208 vmc->add_size_change_notifier(vmem, &dev->size_change_notifier);
209
210 object_property_add_alias(obj, VIRTIO_MEM_BLOCK_SIZE_PROP,
211 OBJECT(&dev->vdev), VIRTIO_MEM_BLOCK_SIZE_PROP);
212 object_property_add_alias(obj, VIRTIO_MEM_SIZE_PROP, OBJECT(&dev->vdev),
213 VIRTIO_MEM_SIZE_PROP);
214 object_property_add(obj, VIRTIO_MEM_REQUESTED_SIZE_PROP, "size",
215 virtio_mem_pci_get_requested_size,
216 virtio_mem_pci_set_requested_size, NULL, NULL);
217 }
218
219 static const VirtioPCIDeviceTypeInfo virtio_mem_pci_info = {
220 .base_name = TYPE_VIRTIO_MEM_PCI,
221 .parent = TYPE_VIRTIO_MD_PCI,
222 .generic_name = "virtio-mem-pci",
223 .instance_size = sizeof(VirtIOMEMPCI),
224 .instance_init = virtio_mem_pci_instance_init,
225 .class_init = virtio_mem_pci_class_init,
226 };
227
virtio_mem_pci_register_types(void)228 static void virtio_mem_pci_register_types(void)
229 {
230 virtio_pci_types_register(&virtio_mem_pci_info);
231 }
232 type_init(virtio_mem_pci_register_types)
233