4 * Copyright IBM, Corp. 2008
7 * Anthony Liguori <aliguori@us.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
14 #include "qemu-common.h"
20 #include "virtio-balloon.h"
23 #if defined(__linux__)
27 typedef struct VirtIOBalloon
35 static VirtIOBalloon
*to_virtio_balloon(VirtIODevice
*vdev
)
37 return (VirtIOBalloon
*)vdev
;
40 static void balloon_page(void *addr
, int deflate
)
42 #if defined(__linux__)
43 if (!kvm_enabled() || kvm_has_sync_mmu())
44 madvise(addr
, TARGET_PAGE_SIZE
,
45 deflate
? MADV_WILLNEED
: MADV_DONTNEED
);
49 /* FIXME: once we do a virtio refactoring, this will get subsumed into common
51 static size_t memcpy_from_iovector(void *data
, size_t offset
, size_t size
,
52 struct iovec
*iov
, int iovlen
)
59 for (i
= 0; i
< iovlen
&& size
; i
++) {
60 if (offset
< (iov_off
+ iov
[i
].iov_len
)) {
61 size_t len
= MIN((iov_off
+ iov
[i
].iov_len
) - offset
, size
);
63 memcpy(ptr
+ data_off
, iov
[i
].iov_base
+ (offset
- iov_off
), len
);
70 iov_off
+= iov
[i
].iov_len
;
76 static void virtio_balloon_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
78 VirtIOBalloon
*s
= to_virtio_balloon(vdev
);
79 VirtQueueElement elem
;
81 while (virtqueue_pop(vq
, &elem
)) {
85 while (memcpy_from_iovector(&pfn
, offset
, 4,
86 elem
.out_sg
, elem
.out_num
) == 4) {
90 pa
= (ram_addr_t
)ldl_p(&pfn
) << VIRTIO_BALLOON_PFN_SHIFT
;
93 addr
= cpu_get_physical_page_desc(pa
);
94 if ((addr
& ~TARGET_PAGE_MASK
) != IO_MEM_RAM
)
97 /* Using qemu_get_ram_ptr is bending the rules a bit, but
98 should be OK because we only want a single page. */
99 balloon_page(qemu_get_ram_ptr(addr
), !!(vq
== s
->dvq
));
102 virtqueue_push(vq
, &elem
, offset
);
103 virtio_notify(vdev
, vq
);
107 static void virtio_balloon_get_config(VirtIODevice
*vdev
, uint8_t *config_data
)
109 VirtIOBalloon
*dev
= to_virtio_balloon(vdev
);
110 struct virtio_balloon_config config
;
112 config
.num_pages
= cpu_to_le32(dev
->num_pages
);
113 config
.actual
= cpu_to_le32(dev
->actual
);
115 memcpy(config_data
, &config
, 8);
118 static void virtio_balloon_set_config(VirtIODevice
*vdev
,
119 const uint8_t *config_data
)
121 VirtIOBalloon
*dev
= to_virtio_balloon(vdev
);
122 struct virtio_balloon_config config
;
123 memcpy(&config
, config_data
, 8);
124 dev
->actual
= config
.actual
;
127 static uint32_t virtio_balloon_get_features(VirtIODevice
*vdev
)
132 static ram_addr_t
virtio_balloon_to_target(void *opaque
, ram_addr_t target
)
134 VirtIOBalloon
*dev
= opaque
;
136 if (target
> ram_size
)
140 dev
->num_pages
= (ram_size
- target
) >> VIRTIO_BALLOON_PFN_SHIFT
;
141 virtio_notify_config(&dev
->vdev
);
144 return ram_size
- (dev
->actual
<< VIRTIO_BALLOON_PFN_SHIFT
);
147 static void virtio_balloon_save(QEMUFile
*f
, void *opaque
)
149 VirtIOBalloon
*s
= opaque
;
151 virtio_save(&s
->vdev
, f
);
153 qemu_put_be32(f
, s
->num_pages
);
154 qemu_put_be32(f
, s
->actual
);
157 static int virtio_balloon_load(QEMUFile
*f
, void *opaque
, int version_id
)
159 VirtIOBalloon
*s
= opaque
;
164 virtio_load(&s
->vdev
, f
);
166 s
->num_pages
= qemu_get_be32(f
);
167 s
->actual
= qemu_get_be32(f
);
172 static void virtio_balloon_init(PCIDevice
*pci_dev
)
176 s
= (VirtIOBalloon
*)virtio_init_pci(pci_dev
, "virtio-balloon",
177 PCI_VENDOR_ID_REDHAT_QUMRANET
,
178 PCI_DEVICE_ID_VIRTIO_BALLOON
,
179 PCI_VENDOR_ID_REDHAT_QUMRANET
,
181 PCI_CLASS_MEMORY_RAM
, 0x00,
184 s
->vdev
.get_config
= virtio_balloon_get_config
;
185 s
->vdev
.set_config
= virtio_balloon_set_config
;
186 s
->vdev
.get_features
= virtio_balloon_get_features
;
188 s
->ivq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_handle_output
);
189 s
->dvq
= virtio_add_queue(&s
->vdev
, 128, virtio_balloon_handle_output
);
191 qemu_add_balloon_handler(virtio_balloon_to_target
, s
);
193 register_savevm("virtio-balloon", -1, 1, virtio_balloon_save
, virtio_balloon_load
, s
);
196 static void virtio_balloon_register_devices(void)
198 pci_qdev_register("virtio-balloon", sizeof(VirtIOBalloon
),
199 virtio_balloon_init
);
202 device_init(virtio_balloon_register_devices
)