KVM: Simplify coalesced mmio initialization
[deliverable/linux.git] / virt / kvm / coalesced_mmio.c
CommitLineData
5f94c174
LV
1/*
2 * KVM coalesced MMIO
3 *
4 * Copyright (c) 2008 Bull S.A.S.
5 *
6 * Author: Laurent Vivier <Laurent.Vivier@bull.net>
7 *
8 */
9
10#include "iodev.h"
11
12#include <linux/kvm_host.h>
13#include <linux/kvm.h>
14
15#include "coalesced_mmio.h"
16
d76685c4
GH
17static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
18{
19 return container_of(dev, struct kvm_coalesced_mmio_dev, dev);
20}
21
bda9020e
MT
22static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
23 gpa_t addr, int len)
5f94c174 24{
5f94c174 25 struct kvm_coalesced_mmio_zone *zone;
105f8d40
AK
26 struct kvm_coalesced_mmio_ring *ring;
27 unsigned avail;
5f94c174
LV
28 int i;
29
5f94c174
LV
30 /* Are we able to batch it ? */
31
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
35 */
105f8d40
AK
36 ring = dev->kvm->coalesced_mmio_ring;
37 avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
64a2268d 38 if (avail < KVM_MAX_VCPUS) {
5f94c174
LV
39 /* full */
40 return 0;
41 }
42
43 /* is it in a batchable area ? */
44
45 for (i = 0; i < dev->nb_zones; i++) {
46 zone = &dev->zone[i];
47
48 /* (addr,len) is fully included in
49 * (zone->addr, zone->size)
50 */
51
52 if (zone->addr <= addr &&
53 addr + len <= zone->addr + zone->size)
54 return 1;
55 }
56 return 0;
57}
58
bda9020e
MT
59static int coalesced_mmio_write(struct kvm_io_device *this,
60 gpa_t addr, int len, const void *val)
5f94c174 61{
d76685c4 62 struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
5f94c174 63 struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
bda9020e
MT
64 if (!coalesced_mmio_in_range(dev, addr, len))
65 return -EOPNOTSUPP;
5f94c174 66
64a2268d 67 spin_lock(&dev->lock);
5f94c174
LV
68
69 /* copy data in first free entry of the ring */
70
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);
74 smp_wmb();
75 ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX;
64a2268d 76 spin_unlock(&dev->lock);
bda9020e 77 return 0;
5f94c174
LV
78}
79
80static void coalesced_mmio_destructor(struct kvm_io_device *this)
81{
d76685c4 82 struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
787a660a
GH
83
84 kfree(dev);
5f94c174
LV
85}
86
d76685c4
GH
87static const struct kvm_io_device_ops coalesced_mmio_ops = {
88 .write = coalesced_mmio_write,
d76685c4
GH
89 .destructor = coalesced_mmio_destructor,
90};
91
5f94c174
LV
92int kvm_coalesced_mmio_init(struct kvm *kvm)
93{
94 struct kvm_coalesced_mmio_dev *dev;
980da6ce 95 struct page *page;
090b7aff 96 int ret;
5f94c174 97
980da6ce
AK
98 ret = -ENOMEM;
99 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
100 if (!page)
101 goto out_err;
102 kvm->coalesced_mmio_ring = page_address(page);
103
104 ret = -ENOMEM;
5f94c174
LV
105 dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
106 if (!dev)
980da6ce 107 goto out_free_page;
64a2268d 108 spin_lock_init(&dev->lock);
d76685c4 109 kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
5f94c174
LV
110 dev->kvm = kvm;
111 kvm->coalesced_mmio_dev = dev;
5f94c174 112
090b7aff
GH
113 ret = kvm_io_bus_register_dev(kvm, &kvm->mmio_bus, &dev->dev);
114 if (ret < 0)
980da6ce
AK
115 goto out_free_dev;
116
117 return ret;
090b7aff 118
980da6ce
AK
119out_free_dev:
120 kfree(dev);
121out_free_page:
122 __free_page(page);
123out_err:
090b7aff 124 return ret;
5f94c174
LV
125}
126
980da6ce
AK
127void kvm_coalesced_mmio_free(struct kvm *kvm)
128{
129 if (kvm->coalesced_mmio_ring)
130 free_page((unsigned long)kvm->coalesced_mmio_ring);
131}
132
5f94c174
LV
133int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
134 struct kvm_coalesced_mmio_zone *zone)
135{
136 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
137
138 if (dev == NULL)
139 return -EINVAL;
140
d5c2dcc3 141 down_write(&kvm->slots_lock);
5f94c174 142 if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) {
d5c2dcc3 143 up_write(&kvm->slots_lock);
5f94c174
LV
144 return -ENOBUFS;
145 }
146
147 dev->zone[dev->nb_zones] = *zone;
148 dev->nb_zones++;
149
d5c2dcc3 150 up_write(&kvm->slots_lock);
5f94c174
LV
151 return 0;
152}
153
154int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
155 struct kvm_coalesced_mmio_zone *zone)
156{
157 int i;
158 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
159 struct kvm_coalesced_mmio_zone *z;
160
161 if (dev == NULL)
162 return -EINVAL;
163
d5c2dcc3 164 down_write(&kvm->slots_lock);
5f94c174
LV
165
166 i = dev->nb_zones;
167 while(i) {
168 z = &dev->zone[i - 1];
169
170 /* unregister all zones
171 * included in (zone->addr, zone->size)
172 */
173
174 if (zone->addr <= z->addr &&
175 z->addr + z->size <= zone->addr + zone->size) {
176 dev->nb_zones--;
177 *z = dev->zone[dev->nb_zones];
178 }
179 i--;
180 }
181
d5c2dcc3 182 up_write(&kvm->slots_lock);
5f94c174
LV
183
184 return 0;
185}
This page took 0.155623 seconds and 5 git commands to generate.