s390x/tcg: initialize machine check queue
[qemu.git] / hw / virtio / virtio-balloon.c
blob37cde389826eb72189e177446f686ff0ace84b15
1 /*
2 * Virtio Balloon Device
4 * Copyright IBM, Corp. 2008
5 * Copyright (C) 2011 Red Hat, Inc.
6 * Copyright (C) 2011 Amit Shah <amit.shah@redhat.com>
8 * Authors:
9 * Anthony Liguori <aliguori@us.ibm.com>
11 * This work is licensed under the terms of the GNU GPL, version 2. See
12 * the COPYING file in the top-level directory.
16 #include "qemu/osdep.h"
17 #include "qemu/iov.h"
18 #include "qemu/timer.h"
19 #include "qemu-common.h"
20 #include "hw/virtio/virtio.h"
21 #include "hw/i386/pc.h"
22 #include "sysemu/balloon.h"
23 #include "hw/virtio/virtio-balloon.h"
24 #include "sysemu/kvm.h"
25 #include "exec/address-spaces.h"
26 #include "qapi/visitor.h"
27 #include "qapi-event.h"
28 #include "trace.h"
29 #include "qemu/error-report.h"
31 #include "hw/virtio/virtio-bus.h"
32 #include "hw/virtio/virtio-access.h"
34 #define BALLOON_PAGE_SIZE (1 << VIRTIO_BALLOON_PFN_SHIFT)
36 static void balloon_page(void *addr, int deflate)
38 if (!qemu_balloon_is_inhibited() && (!kvm_enabled() ||
39 kvm_has_sync_mmu())) {
40 qemu_madvise(addr, BALLOON_PAGE_SIZE,
41 deflate ? QEMU_MADV_WILLNEED : QEMU_MADV_DONTNEED);
45 static const char *balloon_stat_names[] = {
46 [VIRTIO_BALLOON_S_SWAP_IN] = "stat-swap-in",
47 [VIRTIO_BALLOON_S_SWAP_OUT] = "stat-swap-out",
48 [VIRTIO_BALLOON_S_MAJFLT] = "stat-major-faults",
49 [VIRTIO_BALLOON_S_MINFLT] = "stat-minor-faults",
50 [VIRTIO_BALLOON_S_MEMFREE] = "stat-free-memory",
51 [VIRTIO_BALLOON_S_MEMTOT] = "stat-total-memory",
52 [VIRTIO_BALLOON_S_AVAIL] = "stat-available-memory",
53 [VIRTIO_BALLOON_S_NR] = NULL
57 * reset_stats - Mark all items in the stats array as unset
59 * This function needs to be called at device initialization and before
60 * updating to a set of newly-generated stats. This will ensure that no
61 * stale values stick around in case the guest reports a subset of the supported
62 * statistics.
64 static inline void reset_stats(VirtIOBalloon *dev)
66 int i;
67 for (i = 0; i < VIRTIO_BALLOON_S_NR; dev->stats[i++] = -1);
70 static bool balloon_stats_supported(const VirtIOBalloon *s)
72 VirtIODevice *vdev = VIRTIO_DEVICE(s);
73 return virtio_vdev_has_feature(vdev, VIRTIO_BALLOON_F_STATS_VQ);
76 static bool balloon_stats_enabled(const VirtIOBalloon *s)
78 return s->stats_poll_interval > 0;
81 static void balloon_stats_destroy_timer(VirtIOBalloon *s)
83 if (balloon_stats_enabled(s)) {
84 timer_del(s->stats_timer);
85 timer_free(s->stats_timer);
86 s->stats_timer = NULL;
87 s->stats_poll_interval = 0;
91 static void balloon_stats_change_timer(VirtIOBalloon *s, int64_t secs)
93 timer_mod(s->stats_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) + secs * 1000);
96 static void balloon_stats_poll_cb(void *opaque)
98 VirtIOBalloon *s = opaque;
99 VirtIODevice *vdev = VIRTIO_DEVICE(s);
101 if (s->stats_vq_elem == NULL || !balloon_stats_supported(s)) {
102 /* re-schedule */
103 balloon_stats_change_timer(s, s->stats_poll_interval);
104 return;
107 virtqueue_push(s->svq, s->stats_vq_elem, s->stats_vq_offset);
108 virtio_notify(vdev, s->svq);
109 g_free(s->stats_vq_elem);
110 s->stats_vq_elem = NULL;
113 static void balloon_stats_get_all(Object *obj, Visitor *v, const char *name,
114 void *opaque, Error **errp)
116 Error *err = NULL;
117 VirtIOBalloon *s = opaque;
118 int i;
120 visit_start_struct(v, name, NULL, 0, &err);
121 if (err) {
122 goto out;
124 visit_type_int(v, "last-update", &s->stats_last_update, &err);
125 if (err) {
126 goto out_end;
129 visit_start_struct(v, "stats", NULL, 0, &err);
130 if (err) {
131 goto out_end;
133 for (i = 0; i < VIRTIO_BALLOON_S_NR; i++) {
134 visit_type_uint64(v, balloon_stat_names[i], &s->stats[i], &err);
135 if (err) {
136 goto out_nested;
139 visit_check_struct(v, &err);
140 out_nested:
141 visit_end_struct(v, NULL);
143 if (!err) {
144 visit_check_struct(v, &err);
146 out_end:
147 visit_end_struct(v, NULL);
148 out:
149 error_propagate(errp, err);
152 static void balloon_stats_get_poll_interval(Object *obj, Visitor *v,
153 const char *name, void *opaque,
154 Error **errp)
156 VirtIOBalloon *s = opaque;
157 visit_type_int(v, name, &s->stats_poll_interval, errp);
160 static void balloon_stats_set_poll_interval(Object *obj, Visitor *v,
161 const char *name, void *opaque,
162 Error **errp)
164 VirtIOBalloon *s = opaque;
165 Error *local_err = NULL;
166 int64_t value;
168 visit_type_int(v, name, &value, &local_err);
169 if (local_err) {
170 error_propagate(errp, local_err);
171 return;
174 if (value < 0) {
175 error_setg(errp, "timer value must be greater than zero");
176 return;
179 if (value > UINT32_MAX) {
180 error_setg(errp, "timer value is too big");
181 return;
184 if (value == s->stats_poll_interval) {
185 return;
188 if (value == 0) {
189 /* timer=0 disables the timer */
190 balloon_stats_destroy_timer(s);
191 return;
194 if (balloon_stats_enabled(s)) {
195 /* timer interval change */
196 s->stats_poll_interval = value;
197 balloon_stats_change_timer(s, value);
198 return;
201 /* create a new timer */
202 g_assert(s->stats_timer == NULL);
203 s->stats_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL, balloon_stats_poll_cb, s);
204 s->stats_poll_interval = value;
205 balloon_stats_change_timer(s, 0);
208 static void virtio_balloon_handle_output(VirtIODevice *vdev, VirtQueue *vq)
210 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
211 VirtQueueElement *elem;
212 MemoryRegionSection section;
214 for (;;) {
215 size_t offset = 0;
216 uint32_t pfn;
217 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
218 if (!elem) {
219 return;
222 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &pfn, 4) == 4) {
223 ram_addr_t pa;
224 ram_addr_t addr;
225 int p = virtio_ldl_p(vdev, &pfn);
227 pa = (ram_addr_t) p << VIRTIO_BALLOON_PFN_SHIFT;
228 offset += 4;
230 /* FIXME: remove get_system_memory(), but how? */
231 section = memory_region_find(get_system_memory(), pa, 1);
232 if (!int128_nz(section.size) ||
233 !memory_region_is_ram(section.mr) ||
234 memory_region_is_rom(section.mr) ||
235 memory_region_is_romd(section.mr)) {
236 trace_virtio_balloon_bad_addr(pa);
237 continue;
240 trace_virtio_balloon_handle_output(memory_region_name(section.mr),
241 pa);
242 /* Using memory_region_get_ram_ptr is bending the rules a bit, but
243 should be OK because we only want a single page. */
244 addr = section.offset_within_region;
245 balloon_page(memory_region_get_ram_ptr(section.mr) + addr,
246 !!(vq == s->dvq));
247 memory_region_unref(section.mr);
250 virtqueue_push(vq, elem, offset);
251 virtio_notify(vdev, vq);
252 g_free(elem);
256 static void virtio_balloon_receive_stats(VirtIODevice *vdev, VirtQueue *vq)
258 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
259 VirtQueueElement *elem;
260 VirtIOBalloonStat stat;
261 size_t offset = 0;
262 qemu_timeval tv;
264 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
265 if (!elem) {
266 goto out;
269 if (s->stats_vq_elem != NULL) {
270 /* This should never happen if the driver follows the spec. */
271 virtqueue_push(vq, s->stats_vq_elem, 0);
272 virtio_notify(vdev, vq);
273 g_free(s->stats_vq_elem);
276 s->stats_vq_elem = elem;
278 /* Initialize the stats to get rid of any stale values. This is only
279 * needed to handle the case where a guest supports fewer stats than it
280 * used to (ie. it has booted into an old kernel).
282 reset_stats(s);
284 while (iov_to_buf(elem->out_sg, elem->out_num, offset, &stat, sizeof(stat))
285 == sizeof(stat)) {
286 uint16_t tag = virtio_tswap16(vdev, stat.tag);
287 uint64_t val = virtio_tswap64(vdev, stat.val);
289 offset += sizeof(stat);
290 if (tag < VIRTIO_BALLOON_S_NR)
291 s->stats[tag] = val;
293 s->stats_vq_offset = offset;
295 if (qemu_gettimeofday(&tv) < 0) {
296 warn_report("%s: failed to get time of day", __func__);
297 goto out;
300 s->stats_last_update = tv.tv_sec;
302 out:
303 if (balloon_stats_enabled(s)) {
304 balloon_stats_change_timer(s, s->stats_poll_interval);
308 static void virtio_balloon_get_config(VirtIODevice *vdev, uint8_t *config_data)
310 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
311 struct virtio_balloon_config config;
313 config.num_pages = cpu_to_le32(dev->num_pages);
314 config.actual = cpu_to_le32(dev->actual);
316 trace_virtio_balloon_get_config(config.num_pages, config.actual);
317 memcpy(config_data, &config, sizeof(struct virtio_balloon_config));
320 static int build_dimm_list(Object *obj, void *opaque)
322 GSList **list = opaque;
324 if (object_dynamic_cast(obj, TYPE_PC_DIMM)) {
325 DeviceState *dev = DEVICE(obj);
326 if (dev->realized) { /* only realized DIMMs matter */
327 *list = g_slist_prepend(*list, dev);
331 object_child_foreach(obj, build_dimm_list, opaque);
332 return 0;
335 static ram_addr_t get_current_ram_size(void)
337 GSList *list = NULL, *item;
338 ram_addr_t size = ram_size;
340 build_dimm_list(qdev_get_machine(), &list);
341 for (item = list; item; item = g_slist_next(item)) {
342 Object *obj = OBJECT(item->data);
343 if (!strcmp(object_get_typename(obj), TYPE_PC_DIMM)) {
344 size += object_property_get_int(obj, PC_DIMM_SIZE_PROP,
345 &error_abort);
348 g_slist_free(list);
350 return size;
353 static void virtio_balloon_set_config(VirtIODevice *vdev,
354 const uint8_t *config_data)
356 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
357 struct virtio_balloon_config config;
358 uint32_t oldactual = dev->actual;
359 ram_addr_t vm_ram_size = get_current_ram_size();
361 memcpy(&config, config_data, sizeof(struct virtio_balloon_config));
362 dev->actual = le32_to_cpu(config.actual);
363 if (dev->actual != oldactual) {
364 qapi_event_send_balloon_change(vm_ram_size -
365 ((ram_addr_t) dev->actual << VIRTIO_BALLOON_PFN_SHIFT),
366 &error_abort);
368 trace_virtio_balloon_set_config(dev->actual, oldactual);
371 static uint64_t virtio_balloon_get_features(VirtIODevice *vdev, uint64_t f,
372 Error **errp)
374 VirtIOBalloon *dev = VIRTIO_BALLOON(vdev);
375 f |= dev->host_features;
376 virtio_add_feature(&f, VIRTIO_BALLOON_F_STATS_VQ);
377 return f;
380 static void virtio_balloon_stat(void *opaque, BalloonInfo *info)
382 VirtIOBalloon *dev = opaque;
383 info->actual = get_current_ram_size() - ((uint64_t) dev->actual <<
384 VIRTIO_BALLOON_PFN_SHIFT);
387 static void virtio_balloon_to_target(void *opaque, ram_addr_t target)
389 VirtIOBalloon *dev = VIRTIO_BALLOON(opaque);
390 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
391 ram_addr_t vm_ram_size = get_current_ram_size();
393 if (target > vm_ram_size) {
394 target = vm_ram_size;
396 if (target) {
397 dev->num_pages = (vm_ram_size - target) >> VIRTIO_BALLOON_PFN_SHIFT;
398 virtio_notify_config(vdev);
400 trace_virtio_balloon_to_target(target, dev->num_pages);
403 static int virtio_balloon_post_load_device(void *opaque, int version_id)
405 VirtIOBalloon *s = VIRTIO_BALLOON(opaque);
407 if (balloon_stats_enabled(s)) {
408 balloon_stats_change_timer(s, s->stats_poll_interval);
410 return 0;
413 static const VMStateDescription vmstate_virtio_balloon_device = {
414 .name = "virtio-balloon-device",
415 .version_id = 1,
416 .minimum_version_id = 1,
417 .post_load = virtio_balloon_post_load_device,
418 .fields = (VMStateField[]) {
419 VMSTATE_UINT32(num_pages, VirtIOBalloon),
420 VMSTATE_UINT32(actual, VirtIOBalloon),
421 VMSTATE_END_OF_LIST()
425 static void virtio_balloon_device_realize(DeviceState *dev, Error **errp)
427 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
428 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
429 int ret;
431 virtio_init(vdev, "virtio-balloon", VIRTIO_ID_BALLOON,
432 sizeof(struct virtio_balloon_config));
434 ret = qemu_add_balloon_handler(virtio_balloon_to_target,
435 virtio_balloon_stat, s);
437 if (ret < 0) {
438 error_setg(errp, "Only one balloon device is supported");
439 virtio_cleanup(vdev);
440 return;
443 s->ivq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
444 s->dvq = virtio_add_queue(vdev, 128, virtio_balloon_handle_output);
445 s->svq = virtio_add_queue(vdev, 128, virtio_balloon_receive_stats);
447 reset_stats(s);
450 static void virtio_balloon_device_unrealize(DeviceState *dev, Error **errp)
452 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
453 VirtIOBalloon *s = VIRTIO_BALLOON(dev);
455 balloon_stats_destroy_timer(s);
456 qemu_remove_balloon_handler(s);
457 virtio_cleanup(vdev);
460 static void virtio_balloon_device_reset(VirtIODevice *vdev)
462 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
464 if (s->stats_vq_elem != NULL) {
465 virtqueue_unpop(s->svq, s->stats_vq_elem, 0);
466 g_free(s->stats_vq_elem);
467 s->stats_vq_elem = NULL;
471 static void virtio_balloon_set_status(VirtIODevice *vdev, uint8_t status)
473 VirtIOBalloon *s = VIRTIO_BALLOON(vdev);
475 if (!s->stats_vq_elem && vdev->vm_running &&
476 (status & VIRTIO_CONFIG_S_DRIVER_OK) && virtqueue_rewind(s->svq, 1)) {
477 /* poll stats queue for the element we have discarded when the VM
478 * was stopped */
479 virtio_balloon_receive_stats(vdev, s->svq);
483 static void virtio_balloon_instance_init(Object *obj)
485 VirtIOBalloon *s = VIRTIO_BALLOON(obj);
487 object_property_add(obj, "guest-stats", "guest statistics",
488 balloon_stats_get_all, NULL, NULL, s, NULL);
490 object_property_add(obj, "guest-stats-polling-interval", "int",
491 balloon_stats_get_poll_interval,
492 balloon_stats_set_poll_interval,
493 NULL, s, NULL);
496 static const VMStateDescription vmstate_virtio_balloon = {
497 .name = "virtio-balloon",
498 .minimum_version_id = 1,
499 .version_id = 1,
500 .fields = (VMStateField[]) {
501 VMSTATE_VIRTIO_DEVICE,
502 VMSTATE_END_OF_LIST()
506 static Property virtio_balloon_properties[] = {
507 DEFINE_PROP_BIT("deflate-on-oom", VirtIOBalloon, host_features,
508 VIRTIO_BALLOON_F_DEFLATE_ON_OOM, false),
509 DEFINE_PROP_END_OF_LIST(),
512 static void virtio_balloon_class_init(ObjectClass *klass, void *data)
514 DeviceClass *dc = DEVICE_CLASS(klass);
515 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
517 dc->props = virtio_balloon_properties;
518 dc->vmsd = &vmstate_virtio_balloon;
519 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
520 vdc->realize = virtio_balloon_device_realize;
521 vdc->unrealize = virtio_balloon_device_unrealize;
522 vdc->reset = virtio_balloon_device_reset;
523 vdc->get_config = virtio_balloon_get_config;
524 vdc->set_config = virtio_balloon_set_config;
525 vdc->get_features = virtio_balloon_get_features;
526 vdc->set_status = virtio_balloon_set_status;
527 vdc->vmsd = &vmstate_virtio_balloon_device;
530 static const TypeInfo virtio_balloon_info = {
531 .name = TYPE_VIRTIO_BALLOON,
532 .parent = TYPE_VIRTIO_DEVICE,
533 .instance_size = sizeof(VirtIOBalloon),
534 .instance_init = virtio_balloon_instance_init,
535 .class_init = virtio_balloon_class_init,
538 static void virtio_register_types(void)
540 type_register_static(&virtio_balloon_info);
543 type_init(virtio_register_types)