xref: /openbmc/qemu/hw/virtio/virtio-mem.c (revision 2e70874b)
1 /*
2  * Virtio MEM 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 "qemu-common.h"
15 #include "qemu/iov.h"
16 #include "qemu/cutils.h"
17 #include "qemu/error-report.h"
18 #include "qemu/units.h"
19 #include "sysemu/numa.h"
20 #include "sysemu/sysemu.h"
21 #include "sysemu/reset.h"
22 #include "hw/virtio/virtio.h"
23 #include "hw/virtio/virtio-bus.h"
24 #include "hw/virtio/virtio-access.h"
25 #include "hw/virtio/virtio-mem.h"
26 #include "qapi/error.h"
27 #include "qapi/visitor.h"
28 #include "exec/ram_addr.h"
29 #include "migration/misc.h"
30 #include "hw/boards.h"
31 #include "hw/qdev-properties.h"
32 #include "config-devices.h"
33 
34 /*
35  * Use QEMU_VMALLOC_ALIGN, so no THP will have to be split when unplugging
36  * memory (e.g., 2MB on x86_64).
37  */
38 #define VIRTIO_MEM_MIN_BLOCK_SIZE QEMU_VMALLOC_ALIGN
39 /*
40  * Size the usable region bigger than the requested size if possible. Esp.
41  * Linux guests will only add (aligned) memory blocks in case they fully
42  * fit into the usable region, but plug+online only a subset of the pages.
43  * The memory block size corresponds mostly to the section size.
44  *
45  * This allows e.g., to add 20MB with a section size of 128MB on x86_64, and
46  * a section size of 1GB on arm64 (as long as the start address is properly
47  * aligned, similar to ordinary DIMMs).
48  *
49  * We can change this at any time and maybe even make it configurable if
50  * necessary (as the section size can change). But it's more likely that the
51  * section size will rather get smaller and not bigger over time.
52  */
53 #if defined(TARGET_X86_64) || defined(TARGET_I386)
54 #define VIRTIO_MEM_USABLE_EXTENT (2 * (128 * MiB))
55 #else
56 #error VIRTIO_MEM_USABLE_EXTENT not defined
57 #endif
58 
59 static bool virtio_mem_is_busy(void)
60 {
61     /*
62      * Postcopy cannot handle concurrent discards and we don't want to migrate
63      * pages on-demand with stale content when plugging new blocks.
64      */
65     return migration_in_incoming_postcopy();
66 }
67 
68 static bool virtio_mem_test_bitmap(VirtIOMEM *vmem, uint64_t start_gpa,
69                                    uint64_t size, bool plugged)
70 {
71     const unsigned long first_bit = (start_gpa - vmem->addr) / vmem->block_size;
72     const unsigned long last_bit = first_bit + (size / vmem->block_size) - 1;
73     unsigned long found_bit;
74 
75     /* We fake a shorter bitmap to avoid searching too far. */
76     if (plugged) {
77         found_bit = find_next_zero_bit(vmem->bitmap, last_bit + 1, first_bit);
78     } else {
79         found_bit = find_next_bit(vmem->bitmap, last_bit + 1, first_bit);
80     }
81     return found_bit > last_bit;
82 }
83 
84 static void virtio_mem_set_bitmap(VirtIOMEM *vmem, uint64_t start_gpa,
85                                   uint64_t size, bool plugged)
86 {
87     const unsigned long bit = (start_gpa - vmem->addr) / vmem->block_size;
88     const unsigned long nbits = size / vmem->block_size;
89 
90     if (plugged) {
91         bitmap_set(vmem->bitmap, bit, nbits);
92     } else {
93         bitmap_clear(vmem->bitmap, bit, nbits);
94     }
95 }
96 
97 static void virtio_mem_send_response(VirtIOMEM *vmem, VirtQueueElement *elem,
98                                      struct virtio_mem_resp *resp)
99 {
100     VirtIODevice *vdev = VIRTIO_DEVICE(vmem);
101     VirtQueue *vq = vmem->vq;
102 
103     iov_from_buf(elem->in_sg, elem->in_num, 0, resp, sizeof(*resp));
104 
105     virtqueue_push(vq, elem, sizeof(*resp));
106     virtio_notify(vdev, vq);
107 }
108 
109 static void virtio_mem_send_response_simple(VirtIOMEM *vmem,
110                                             VirtQueueElement *elem,
111                                             uint16_t type)
112 {
113     struct virtio_mem_resp resp = {
114         .type = cpu_to_le16(type),
115     };
116 
117     virtio_mem_send_response(vmem, elem, &resp);
118 }
119 
120 static bool virtio_mem_valid_range(VirtIOMEM *vmem, uint64_t gpa, uint64_t size)
121 {
122     if (!QEMU_IS_ALIGNED(gpa, vmem->block_size)) {
123         return false;
124     }
125     if (gpa + size < gpa || !size) {
126         return false;
127     }
128     if (gpa < vmem->addr || gpa >= vmem->addr + vmem->usable_region_size) {
129         return false;
130     }
131     if (gpa + size > vmem->addr + vmem->usable_region_size) {
132         return false;
133     }
134     return true;
135 }
136 
137 static int virtio_mem_set_block_state(VirtIOMEM *vmem, uint64_t start_gpa,
138                                       uint64_t size, bool plug)
139 {
140     const uint64_t offset = start_gpa - vmem->addr;
141     int ret;
142 
143     if (virtio_mem_is_busy()) {
144         return -EBUSY;
145     }
146 
147     if (!plug) {
148         ret = ram_block_discard_range(vmem->memdev->mr.ram_block, offset, size);
149         if (ret) {
150             error_report("Unexpected error discarding RAM: %s",
151                          strerror(-ret));
152             return -EBUSY;
153         }
154     }
155     virtio_mem_set_bitmap(vmem, start_gpa, size, plug);
156     return 0;
157 }
158 
159 static int virtio_mem_state_change_request(VirtIOMEM *vmem, uint64_t gpa,
160                                            uint16_t nb_blocks, bool plug)
161 {
162     const uint64_t size = nb_blocks * vmem->block_size;
163     int ret;
164 
165     if (!virtio_mem_valid_range(vmem, gpa, size)) {
166         return VIRTIO_MEM_RESP_ERROR;
167     }
168 
169     if (plug && (vmem->size + size > vmem->requested_size)) {
170         return VIRTIO_MEM_RESP_NACK;
171     }
172 
173     /* test if really all blocks are in the opposite state */
174     if (!virtio_mem_test_bitmap(vmem, gpa, size, !plug)) {
175         return VIRTIO_MEM_RESP_ERROR;
176     }
177 
178     ret = virtio_mem_set_block_state(vmem, gpa, size, plug);
179     if (ret) {
180         return VIRTIO_MEM_RESP_BUSY;
181     }
182     if (plug) {
183         vmem->size += size;
184     } else {
185         vmem->size -= size;
186     }
187     return VIRTIO_MEM_RESP_ACK;
188 }
189 
190 static void virtio_mem_plug_request(VirtIOMEM *vmem, VirtQueueElement *elem,
191                                     struct virtio_mem_req *req)
192 {
193     const uint64_t gpa = le64_to_cpu(req->u.plug.addr);
194     const uint16_t nb_blocks = le16_to_cpu(req->u.plug.nb_blocks);
195     uint16_t type;
196 
197     type = virtio_mem_state_change_request(vmem, gpa, nb_blocks, true);
198     virtio_mem_send_response_simple(vmem, elem, type);
199 }
200 
201 static void virtio_mem_unplug_request(VirtIOMEM *vmem, VirtQueueElement *elem,
202                                       struct virtio_mem_req *req)
203 {
204     const uint64_t gpa = le64_to_cpu(req->u.unplug.addr);
205     const uint16_t nb_blocks = le16_to_cpu(req->u.unplug.nb_blocks);
206     uint16_t type;
207 
208     type = virtio_mem_state_change_request(vmem, gpa, nb_blocks, false);
209     virtio_mem_send_response_simple(vmem, elem, type);
210 }
211 
212 static void virtio_mem_resize_usable_region(VirtIOMEM *vmem,
213                                             uint64_t requested_size,
214                                             bool can_shrink)
215 {
216     uint64_t newsize = MIN(memory_region_size(&vmem->memdev->mr),
217                            requested_size + VIRTIO_MEM_USABLE_EXTENT);
218 
219     if (!requested_size) {
220         newsize = 0;
221     }
222 
223     if (newsize < vmem->usable_region_size && !can_shrink) {
224         return;
225     }
226 
227     vmem->usable_region_size = newsize;
228 }
229 
230 static int virtio_mem_unplug_all(VirtIOMEM *vmem)
231 {
232     RAMBlock *rb = vmem->memdev->mr.ram_block;
233     int ret;
234 
235     if (virtio_mem_is_busy()) {
236         return -EBUSY;
237     }
238 
239     ret = ram_block_discard_range(rb, 0, qemu_ram_get_used_length(rb));
240     if (ret) {
241         error_report("Unexpected error discarding RAM: %s", strerror(-ret));
242         return -EBUSY;
243     }
244     bitmap_clear(vmem->bitmap, 0, vmem->bitmap_size);
245     vmem->size = 0;
246 
247     virtio_mem_resize_usable_region(vmem, vmem->requested_size, true);
248     return 0;
249 }
250 
251 static void virtio_mem_unplug_all_request(VirtIOMEM *vmem,
252                                           VirtQueueElement *elem)
253 {
254     if (virtio_mem_unplug_all(vmem)) {
255         virtio_mem_send_response_simple(vmem, elem, VIRTIO_MEM_RESP_BUSY);
256     } else {
257         virtio_mem_send_response_simple(vmem, elem, VIRTIO_MEM_RESP_ACK);
258     }
259 }
260 
261 static void virtio_mem_state_request(VirtIOMEM *vmem, VirtQueueElement *elem,
262                                      struct virtio_mem_req *req)
263 {
264     const uint16_t nb_blocks = le16_to_cpu(req->u.state.nb_blocks);
265     const uint64_t gpa = le64_to_cpu(req->u.state.addr);
266     const uint64_t size = nb_blocks * vmem->block_size;
267     struct virtio_mem_resp resp = {
268         .type = cpu_to_le16(VIRTIO_MEM_RESP_ACK),
269     };
270 
271     if (!virtio_mem_valid_range(vmem, gpa, size)) {
272         virtio_mem_send_response_simple(vmem, elem, VIRTIO_MEM_RESP_ERROR);
273         return;
274     }
275 
276     if (virtio_mem_test_bitmap(vmem, gpa, size, true)) {
277         resp.u.state.state = cpu_to_le16(VIRTIO_MEM_STATE_PLUGGED);
278     } else if (virtio_mem_test_bitmap(vmem, gpa, size, false)) {
279         resp.u.state.state = cpu_to_le16(VIRTIO_MEM_STATE_UNPLUGGED);
280     } else {
281         resp.u.state.state = cpu_to_le16(VIRTIO_MEM_STATE_MIXED);
282     }
283     virtio_mem_send_response(vmem, elem, &resp);
284 }
285 
286 static void virtio_mem_handle_request(VirtIODevice *vdev, VirtQueue *vq)
287 {
288     const int len = sizeof(struct virtio_mem_req);
289     VirtIOMEM *vmem = VIRTIO_MEM(vdev);
290     VirtQueueElement *elem;
291     struct virtio_mem_req req;
292     uint16_t type;
293 
294     while (true) {
295         elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
296         if (!elem) {
297             return;
298         }
299 
300         if (iov_to_buf(elem->out_sg, elem->out_num, 0, &req, len) < len) {
301             virtio_error(vdev, "virtio-mem protocol violation: invalid request"
302                          " size: %d", len);
303             g_free(elem);
304             return;
305         }
306 
307         if (iov_size(elem->in_sg, elem->in_num) <
308             sizeof(struct virtio_mem_resp)) {
309             virtio_error(vdev, "virtio-mem protocol violation: not enough space"
310                          " for response: %zu",
311                          iov_size(elem->in_sg, elem->in_num));
312             g_free(elem);
313             return;
314         }
315 
316         type = le16_to_cpu(req.type);
317         switch (type) {
318         case VIRTIO_MEM_REQ_PLUG:
319             virtio_mem_plug_request(vmem, elem, &req);
320             break;
321         case VIRTIO_MEM_REQ_UNPLUG:
322             virtio_mem_unplug_request(vmem, elem, &req);
323             break;
324         case VIRTIO_MEM_REQ_UNPLUG_ALL:
325             virtio_mem_unplug_all_request(vmem, elem);
326             break;
327         case VIRTIO_MEM_REQ_STATE:
328             virtio_mem_state_request(vmem, elem, &req);
329             break;
330         default:
331             virtio_error(vdev, "virtio-mem protocol violation: unknown request"
332                          " type: %d", type);
333             g_free(elem);
334             return;
335         }
336 
337         g_free(elem);
338     }
339 }
340 
341 static void virtio_mem_get_config(VirtIODevice *vdev, uint8_t *config_data)
342 {
343     VirtIOMEM *vmem = VIRTIO_MEM(vdev);
344     struct virtio_mem_config *config = (void *) config_data;
345 
346     config->block_size = cpu_to_le64(vmem->block_size);
347     config->node_id = cpu_to_le16(vmem->node);
348     config->requested_size = cpu_to_le64(vmem->requested_size);
349     config->plugged_size = cpu_to_le64(vmem->size);
350     config->addr = cpu_to_le64(vmem->addr);
351     config->region_size = cpu_to_le64(memory_region_size(&vmem->memdev->mr));
352     config->usable_region_size = cpu_to_le64(vmem->usable_region_size);
353 }
354 
355 static uint64_t virtio_mem_get_features(VirtIODevice *vdev, uint64_t features,
356                                         Error **errp)
357 {
358     MachineState *ms = MACHINE(qdev_get_machine());
359 
360     if (ms->numa_state) {
361 #if defined(CONFIG_ACPI)
362         virtio_add_feature(&features, VIRTIO_MEM_F_ACPI_PXM);
363 #endif
364     }
365     return features;
366 }
367 
368 static void virtio_mem_system_reset(void *opaque)
369 {
370     VirtIOMEM *vmem = VIRTIO_MEM(opaque);
371 
372     /*
373      * During usual resets, we will unplug all memory and shrink the usable
374      * region size. This is, however, not possible in all scenarios. Then,
375      * the guest has to deal with this manually (VIRTIO_MEM_REQ_UNPLUG_ALL).
376      */
377     virtio_mem_unplug_all(vmem);
378 }
379 
380 static void virtio_mem_device_realize(DeviceState *dev, Error **errp)
381 {
382     MachineState *ms = MACHINE(qdev_get_machine());
383     int nb_numa_nodes = ms->numa_state ? ms->numa_state->num_nodes : 0;
384     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
385     VirtIOMEM *vmem = VIRTIO_MEM(dev);
386     uint64_t page_size;
387     RAMBlock *rb;
388     int ret;
389 
390     if (!vmem->memdev) {
391         error_setg(errp, "'%s' property is not set", VIRTIO_MEM_MEMDEV_PROP);
392         return;
393     } else if (host_memory_backend_is_mapped(vmem->memdev)) {
394         char *path = object_get_canonical_path_component(OBJECT(vmem->memdev));
395 
396         error_setg(errp, "'%s' property specifies a busy memdev: %s",
397                    VIRTIO_MEM_MEMDEV_PROP, path);
398         g_free(path);
399         return;
400     } else if (!memory_region_is_ram(&vmem->memdev->mr) ||
401         memory_region_is_rom(&vmem->memdev->mr) ||
402         !vmem->memdev->mr.ram_block) {
403         error_setg(errp, "'%s' property specifies an unsupported memdev",
404                    VIRTIO_MEM_MEMDEV_PROP);
405         return;
406     }
407 
408     if ((nb_numa_nodes && vmem->node >= nb_numa_nodes) ||
409         (!nb_numa_nodes && vmem->node)) {
410         error_setg(errp, "'%s' property has value '%" PRIu32 "', which exceeds"
411                    "the number of numa nodes: %d", VIRTIO_MEM_NODE_PROP,
412                    vmem->node, nb_numa_nodes ? nb_numa_nodes : 1);
413         return;
414     }
415 
416     if (enable_mlock) {
417         error_setg(errp, "Incompatible with mlock");
418         return;
419     }
420 
421     rb = vmem->memdev->mr.ram_block;
422     page_size = qemu_ram_pagesize(rb);
423 
424     if (vmem->block_size < page_size) {
425         error_setg(errp, "'%s' property has to be at least the page size (0x%"
426                    PRIx64 ")", VIRTIO_MEM_BLOCK_SIZE_PROP, page_size);
427         return;
428     } else if (!QEMU_IS_ALIGNED(vmem->requested_size, vmem->block_size)) {
429         error_setg(errp, "'%s' property has to be multiples of '%s' (0x%" PRIx64
430                    ")", VIRTIO_MEM_REQUESTED_SIZE_PROP,
431                    VIRTIO_MEM_BLOCK_SIZE_PROP, vmem->block_size);
432         return;
433     } else if (!QEMU_IS_ALIGNED(memory_region_size(&vmem->memdev->mr),
434                                 vmem->block_size)) {
435         error_setg(errp, "'%s' property memdev size has to be multiples of"
436                    "'%s' (0x%" PRIx64 ")", VIRTIO_MEM_MEMDEV_PROP,
437                    VIRTIO_MEM_BLOCK_SIZE_PROP, vmem->block_size);
438         return;
439     }
440 
441     if (ram_block_discard_require(true)) {
442         error_setg(errp, "Discarding RAM is disabled");
443         return;
444     }
445 
446     ret = ram_block_discard_range(rb, 0, qemu_ram_get_used_length(rb));
447     if (ret) {
448         error_setg_errno(errp, -ret, "Unexpected error discarding RAM");
449         ram_block_discard_require(false);
450         return;
451     }
452 
453     virtio_mem_resize_usable_region(vmem, vmem->requested_size, true);
454 
455     vmem->bitmap_size = memory_region_size(&vmem->memdev->mr) /
456                         vmem->block_size;
457     vmem->bitmap = bitmap_new(vmem->bitmap_size);
458 
459     virtio_init(vdev, TYPE_VIRTIO_MEM, VIRTIO_ID_MEM,
460                 sizeof(struct virtio_mem_config));
461     vmem->vq = virtio_add_queue(vdev, 128, virtio_mem_handle_request);
462 
463     host_memory_backend_set_mapped(vmem->memdev, true);
464     vmstate_register_ram(&vmem->memdev->mr, DEVICE(vmem));
465     qemu_register_reset(virtio_mem_system_reset, vmem);
466 }
467 
468 static void virtio_mem_device_unrealize(DeviceState *dev)
469 {
470     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
471     VirtIOMEM *vmem = VIRTIO_MEM(dev);
472 
473     qemu_unregister_reset(virtio_mem_system_reset, vmem);
474     vmstate_unregister_ram(&vmem->memdev->mr, DEVICE(vmem));
475     host_memory_backend_set_mapped(vmem->memdev, false);
476     virtio_del_queue(vdev, 0);
477     virtio_cleanup(vdev);
478     g_free(vmem->bitmap);
479     ram_block_discard_require(false);
480 }
481 
482 static int virtio_mem_restore_unplugged(VirtIOMEM *vmem)
483 {
484     RAMBlock *rb = vmem->memdev->mr.ram_block;
485     unsigned long first_zero_bit, last_zero_bit;
486     uint64_t offset, length;
487     int ret;
488 
489     /* Find consecutive unplugged blocks and discard the consecutive range. */
490     first_zero_bit = find_first_zero_bit(vmem->bitmap, vmem->bitmap_size);
491     while (first_zero_bit < vmem->bitmap_size) {
492         offset = first_zero_bit * vmem->block_size;
493         last_zero_bit = find_next_bit(vmem->bitmap, vmem->bitmap_size,
494                                       first_zero_bit + 1) - 1;
495         length = (last_zero_bit - first_zero_bit + 1) * vmem->block_size;
496 
497         ret = ram_block_discard_range(rb, offset, length);
498         if (ret) {
499             error_report("Unexpected error discarding RAM: %s",
500                          strerror(-ret));
501             return -EINVAL;
502         }
503         first_zero_bit = find_next_zero_bit(vmem->bitmap, vmem->bitmap_size,
504                                             last_zero_bit + 2);
505     }
506     return 0;
507 }
508 
509 static int virtio_mem_post_load(void *opaque, int version_id)
510 {
511     if (migration_in_incoming_postcopy()) {
512         return 0;
513     }
514 
515     return virtio_mem_restore_unplugged(VIRTIO_MEM(opaque));
516 }
517 
518 static const VMStateDescription vmstate_virtio_mem_device = {
519     .name = "virtio-mem-device",
520     .minimum_version_id = 1,
521     .version_id = 1,
522     .post_load = virtio_mem_post_load,
523     .fields = (VMStateField[]) {
524         VMSTATE_UINT64(usable_region_size, VirtIOMEM),
525         VMSTATE_UINT64(size, VirtIOMEM),
526         VMSTATE_UINT64(requested_size, VirtIOMEM),
527         VMSTATE_BITMAP(bitmap, VirtIOMEM, 0, bitmap_size),
528         VMSTATE_END_OF_LIST()
529     },
530 };
531 
532 static const VMStateDescription vmstate_virtio_mem = {
533     .name = "virtio-mem",
534     .minimum_version_id = 1,
535     .version_id = 1,
536     .fields = (VMStateField[]) {
537         VMSTATE_VIRTIO_DEVICE,
538         VMSTATE_END_OF_LIST()
539     },
540 };
541 
542 static void virtio_mem_fill_device_info(const VirtIOMEM *vmem,
543                                         VirtioMEMDeviceInfo *vi)
544 {
545     vi->memaddr = vmem->addr;
546     vi->node = vmem->node;
547     vi->requested_size = vmem->requested_size;
548     vi->size = vmem->size;
549     vi->max_size = memory_region_size(&vmem->memdev->mr);
550     vi->block_size = vmem->block_size;
551     vi->memdev = object_get_canonical_path(OBJECT(vmem->memdev));
552 }
553 
554 static MemoryRegion *virtio_mem_get_memory_region(VirtIOMEM *vmem, Error **errp)
555 {
556     if (!vmem->memdev) {
557         error_setg(errp, "'%s' property must be set", VIRTIO_MEM_MEMDEV_PROP);
558         return NULL;
559     }
560 
561     return &vmem->memdev->mr;
562 }
563 
564 static void virtio_mem_get_size(Object *obj, Visitor *v, const char *name,
565                                 void *opaque, Error **errp)
566 {
567     const VirtIOMEM *vmem = VIRTIO_MEM(obj);
568     uint64_t value = vmem->size;
569 
570     visit_type_size(v, name, &value, errp);
571 }
572 
573 static void virtio_mem_get_requested_size(Object *obj, Visitor *v,
574                                           const char *name, void *opaque,
575                                           Error **errp)
576 {
577     const VirtIOMEM *vmem = VIRTIO_MEM(obj);
578     uint64_t value = vmem->requested_size;
579 
580     visit_type_size(v, name, &value, errp);
581 }
582 
583 static void virtio_mem_set_requested_size(Object *obj, Visitor *v,
584                                           const char *name, void *opaque,
585                                           Error **errp)
586 {
587     VirtIOMEM *vmem = VIRTIO_MEM(obj);
588     Error *err = NULL;
589     uint64_t value;
590 
591     visit_type_size(v, name, &value, &err);
592     if (err) {
593         error_propagate(errp, err);
594         return;
595     }
596 
597     /*
598      * The block size and memory backend are not fixed until the device was
599      * realized. realize() will verify these properties then.
600      */
601     if (DEVICE(obj)->realized) {
602         if (!QEMU_IS_ALIGNED(value, vmem->block_size)) {
603             error_setg(errp, "'%s' has to be multiples of '%s' (0x%" PRIx64
604                        ")", name, VIRTIO_MEM_BLOCK_SIZE_PROP,
605                        vmem->block_size);
606             return;
607         } else if (value > memory_region_size(&vmem->memdev->mr)) {
608             error_setg(errp, "'%s' cannot exceed the memory backend size"
609                        "(0x%" PRIx64 ")", name,
610                        memory_region_size(&vmem->memdev->mr));
611             return;
612         }
613 
614         if (value != vmem->requested_size) {
615             virtio_mem_resize_usable_region(vmem, value, false);
616             vmem->requested_size = value;
617         }
618         /*
619          * Trigger a config update so the guest gets notified. We trigger
620          * even if the size didn't change (especially helpful for debugging).
621          */
622         virtio_notify_config(VIRTIO_DEVICE(vmem));
623     } else {
624         vmem->requested_size = value;
625     }
626 }
627 
628 static void virtio_mem_get_block_size(Object *obj, Visitor *v, const char *name,
629                                       void *opaque, Error **errp)
630 {
631     const VirtIOMEM *vmem = VIRTIO_MEM(obj);
632     uint64_t value = vmem->block_size;
633 
634     visit_type_size(v, name, &value, errp);
635 }
636 
637 static void virtio_mem_set_block_size(Object *obj, Visitor *v, const char *name,
638                                       void *opaque, Error **errp)
639 {
640     VirtIOMEM *vmem = VIRTIO_MEM(obj);
641     Error *err = NULL;
642     uint64_t value;
643 
644     if (DEVICE(obj)->realized) {
645         error_setg(errp, "'%s' cannot be changed", name);
646         return;
647     }
648 
649     visit_type_size(v, name, &value, &err);
650     if (err) {
651         error_propagate(errp, err);
652         return;
653     }
654 
655     if (value < VIRTIO_MEM_MIN_BLOCK_SIZE) {
656         error_setg(errp, "'%s' property has to be at least 0x%" PRIx32, name,
657                    VIRTIO_MEM_MIN_BLOCK_SIZE);
658         return;
659     } else if (!is_power_of_2(value)) {
660         error_setg(errp, "'%s' property has to be a power of two", name);
661         return;
662     }
663     vmem->block_size = value;
664 }
665 
666 static void virtio_mem_instance_init(Object *obj)
667 {
668     VirtIOMEM *vmem = VIRTIO_MEM(obj);
669 
670     vmem->block_size = VIRTIO_MEM_MIN_BLOCK_SIZE;
671 
672     object_property_add(obj, VIRTIO_MEM_SIZE_PROP, "size", virtio_mem_get_size,
673                         NULL, NULL, NULL);
674     object_property_add(obj, VIRTIO_MEM_REQUESTED_SIZE_PROP, "size",
675                         virtio_mem_get_requested_size,
676                         virtio_mem_set_requested_size, NULL, NULL);
677     object_property_add(obj, VIRTIO_MEM_BLOCK_SIZE_PROP, "size",
678                         virtio_mem_get_block_size, virtio_mem_set_block_size,
679                         NULL, NULL);
680 }
681 
682 static Property virtio_mem_properties[] = {
683     DEFINE_PROP_UINT64(VIRTIO_MEM_ADDR_PROP, VirtIOMEM, addr, 0),
684     DEFINE_PROP_UINT32(VIRTIO_MEM_NODE_PROP, VirtIOMEM, node, 0),
685     DEFINE_PROP_LINK(VIRTIO_MEM_MEMDEV_PROP, VirtIOMEM, memdev,
686                      TYPE_MEMORY_BACKEND, HostMemoryBackend *),
687     DEFINE_PROP_END_OF_LIST(),
688 };
689 
690 static void virtio_mem_class_init(ObjectClass *klass, void *data)
691 {
692     DeviceClass *dc = DEVICE_CLASS(klass);
693     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
694     VirtIOMEMClass *vmc = VIRTIO_MEM_CLASS(klass);
695 
696     device_class_set_props(dc, virtio_mem_properties);
697     dc->vmsd = &vmstate_virtio_mem;
698 
699     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
700     vdc->realize = virtio_mem_device_realize;
701     vdc->unrealize = virtio_mem_device_unrealize;
702     vdc->get_config = virtio_mem_get_config;
703     vdc->get_features = virtio_mem_get_features;
704     vdc->vmsd = &vmstate_virtio_mem_device;
705 
706     vmc->fill_device_info = virtio_mem_fill_device_info;
707     vmc->get_memory_region = virtio_mem_get_memory_region;
708 }
709 
710 static const TypeInfo virtio_mem_info = {
711     .name = TYPE_VIRTIO_MEM,
712     .parent = TYPE_VIRTIO_DEVICE,
713     .instance_size = sizeof(VirtIOMEM),
714     .instance_init = virtio_mem_instance_init,
715     .class_init = virtio_mem_class_init,
716     .class_size = sizeof(VirtIOMEMClass),
717 };
718 
719 static void virtio_register_types(void)
720 {
721     type_register_static(&virtio_mem_info);
722 }
723 
724 type_init(virtio_register_types)
725