4 * Copyright (c) 2008 Bull S.A.S.
6 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
12 #include <linux/kvm_host.h>
13 #include <linux/kvm.h>
15 #include "coalesced_mmio.h"
17 static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
19 return container_of(dev, struct kvm_coalesced_mmio_dev, dev);
22 static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
25 struct kvm_coalesced_mmio_zone *zone;
26 struct kvm_coalesced_mmio_ring *ring;
30 /* Are we able to batch it ? */
32 /* last is the first free entry
33 * check if we don't meet the first used entry
34 * there is always one unused entry in the buffer
36 ring = dev->kvm->coalesced_mmio_ring;
37 avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
38 if (avail < KVM_MAX_VCPUS) {
43 /* is it in a batchable area ? */
45 for (i = 0; i < dev->nb_zones; i++) {
48 /* (addr,len) is fully included in
49 * (zone->addr, zone->size)
52 if (zone->addr <= addr &&
53 addr + len <= zone->addr + zone->size)
59 static int coalesced_mmio_write(struct kvm_io_device *this,
60 gpa_t addr, int len, const void *val)
62 struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
63 struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
64 if (!coalesced_mmio_in_range(dev, addr, len))
67 spin_lock(&dev->lock);
69 /* copy data in first free entry of the ring */
71 ring->coalesced_mmio[ring->last].phys_addr = addr;
72 ring->coalesced_mmio[ring->last].len = len;
73 memcpy(ring->coalesced_mmio[ring->last].data, val, len);
75 ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX;
76 spin_unlock(&dev->lock);
80 static void coalesced_mmio_destructor(struct kvm_io_device *this)
82 struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
87 static const struct kvm_io_device_ops coalesced_mmio_ops = {
88 .write = coalesced_mmio_write,
89 .destructor = coalesced_mmio_destructor,
92 int kvm_coalesced_mmio_init(struct kvm *kvm)
94 struct kvm_coalesced_mmio_dev *dev;
99 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
102 kvm->coalesced_mmio_ring = page_address(page);
105 dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
108 spin_lock_init(&dev->lock);
109 kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
111 kvm->coalesced_mmio_dev = dev;
113 mutex_lock(&kvm->slots_lock);
114 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev);
115 mutex_unlock(&kvm->slots_lock);
129 void kvm_coalesced_mmio_free(struct kvm *kvm)
131 if (kvm->coalesced_mmio_ring)
132 free_page((unsigned long)kvm->coalesced_mmio_ring);
135 int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
136 struct kvm_coalesced_mmio_zone *zone)
138 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
143 mutex_lock(&kvm->slots_lock);
144 if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) {
145 mutex_unlock(&kvm->slots_lock);
149 dev->zone[dev->nb_zones] = *zone;
152 mutex_unlock(&kvm->slots_lock);
156 int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
157 struct kvm_coalesced_mmio_zone *zone)
160 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
161 struct kvm_coalesced_mmio_zone *z;
166 mutex_lock(&kvm->slots_lock);
170 z = &dev->zone[i - 1];
172 /* unregister all zones
173 * included in (zone->addr, zone->size)
176 if (zone->addr <= z->addr &&
177 z->addr + z->size <= zone->addr + zone->size) {
179 *z = dev->zone[dev->nb_zones];
184 mutex_unlock(&kvm->slots_lock);