]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - virt/kvm/coalesced_mmio.c
KVM: SVM: correctly trace irq injection
[mirror_ubuntu-zesty-kernel.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>
5a0e3ad6 13#include <linux/slab.h>
5f94c174
LV
14#include <linux/kvm.h>
15
16#include "coalesced_mmio.h"
17
d76685c4
GH
18static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
19{
20 return container_of(dev, struct kvm_coalesced_mmio_dev, dev);
21}
22
bda9020e
MT
23static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
24 gpa_t addr, int len)
5f94c174 25{
5f94c174 26 struct kvm_coalesced_mmio_zone *zone;
105f8d40
AK
27 struct kvm_coalesced_mmio_ring *ring;
28 unsigned avail;
5f94c174
LV
29 int i;
30
5f94c174
LV
31 /* Are we able to batch it ? */
32
33 /* last is the first free entry
34 * check if we don't meet the first used entry
35 * there is always one unused entry in the buffer
36 */
105f8d40
AK
37 ring = dev->kvm->coalesced_mmio_ring;
38 avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
64a2268d 39 if (avail < KVM_MAX_VCPUS) {
5f94c174
LV
40 /* full */
41 return 0;
42 }
43
44 /* is it in a batchable area ? */
45
46 for (i = 0; i < dev->nb_zones; i++) {
47 zone = &dev->zone[i];
48
49 /* (addr,len) is fully included in
50 * (zone->addr, zone->size)
51 */
52
53 if (zone->addr <= addr &&
54 addr + len <= zone->addr + zone->size)
55 return 1;
56 }
57 return 0;
58}
59
bda9020e
MT
60static int coalesced_mmio_write(struct kvm_io_device *this,
61 gpa_t addr, int len, const void *val)
5f94c174 62{
d76685c4 63 struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
5f94c174 64 struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
bda9020e
MT
65 if (!coalesced_mmio_in_range(dev, addr, len))
66 return -EOPNOTSUPP;
5f94c174 67
64a2268d 68 spin_lock(&dev->lock);
5f94c174
LV
69
70 /* copy data in first free entry of the ring */
71
72 ring->coalesced_mmio[ring->last].phys_addr = addr;
73 ring->coalesced_mmio[ring->last].len = len;
74 memcpy(ring->coalesced_mmio[ring->last].data, val, len);
75 smp_wmb();
76 ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX;
64a2268d 77 spin_unlock(&dev->lock);
bda9020e 78 return 0;
5f94c174
LV
79}
80
81static void coalesced_mmio_destructor(struct kvm_io_device *this)
82{
d76685c4 83 struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
787a660a
GH
84
85 kfree(dev);
5f94c174
LV
86}
87
d76685c4
GH
88static const struct kvm_io_device_ops coalesced_mmio_ops = {
89 .write = coalesced_mmio_write,
d76685c4
GH
90 .destructor = coalesced_mmio_destructor,
91};
92
5f94c174
LV
93int kvm_coalesced_mmio_init(struct kvm *kvm)
94{
95 struct kvm_coalesced_mmio_dev *dev;
980da6ce 96 struct page *page;
090b7aff 97 int ret;
5f94c174 98
980da6ce
AK
99 ret = -ENOMEM;
100 page = alloc_page(GFP_KERNEL | __GFP_ZERO);
101 if (!page)
102 goto out_err;
103 kvm->coalesced_mmio_ring = page_address(page);
104
105 ret = -ENOMEM;
5f94c174
LV
106 dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
107 if (!dev)
980da6ce 108 goto out_free_page;
64a2268d 109 spin_lock_init(&dev->lock);
d76685c4 110 kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
5f94c174
LV
111 dev->kvm = kvm;
112 kvm->coalesced_mmio_dev = dev;
5f94c174 113
79fac95e 114 mutex_lock(&kvm->slots_lock);
e93f8a0f 115 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev);
79fac95e 116 mutex_unlock(&kvm->slots_lock);
090b7aff 117 if (ret < 0)
980da6ce
AK
118 goto out_free_dev;
119
120 return ret;
090b7aff 121
980da6ce 122out_free_dev:
6ce5a090 123 kvm->coalesced_mmio_dev = NULL;
980da6ce
AK
124 kfree(dev);
125out_free_page:
6ce5a090 126 kvm->coalesced_mmio_ring = NULL;
980da6ce
AK
127 __free_page(page);
128out_err:
090b7aff 129 return ret;
5f94c174
LV
130}
131
980da6ce
AK
132void kvm_coalesced_mmio_free(struct kvm *kvm)
133{
134 if (kvm->coalesced_mmio_ring)
135 free_page((unsigned long)kvm->coalesced_mmio_ring);
136}
137
5f94c174 138int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
43db6697 139 struct kvm_coalesced_mmio_zone *zone)
5f94c174
LV
140{
141 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
142
143 if (dev == NULL)
a87fa355 144 return -ENXIO;
5f94c174 145
79fac95e 146 mutex_lock(&kvm->slots_lock);
5f94c174 147 if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) {
79fac95e 148 mutex_unlock(&kvm->slots_lock);
5f94c174
LV
149 return -ENOBUFS;
150 }
151
152 dev->zone[dev->nb_zones] = *zone;
153 dev->nb_zones++;
154
79fac95e 155 mutex_unlock(&kvm->slots_lock);
5f94c174
LV
156 return 0;
157}
158
159int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
160 struct kvm_coalesced_mmio_zone *zone)
161{
162 int i;
163 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
164 struct kvm_coalesced_mmio_zone *z;
165
166 if (dev == NULL)
a87fa355 167 return -ENXIO;
5f94c174 168
79fac95e 169 mutex_lock(&kvm->slots_lock);
5f94c174
LV
170
171 i = dev->nb_zones;
43db6697 172 while (i) {
5f94c174
LV
173 z = &dev->zone[i - 1];
174
175 /* unregister all zones
176 * included in (zone->addr, zone->size)
177 */
178
179 if (zone->addr <= z->addr &&
180 z->addr + z->size <= zone->addr + zone->size) {
181 dev->nb_zones--;
182 *z = dev->zone[dev->nb_zones];
183 }
184 i--;
185 }
186
79fac95e 187 mutex_unlock(&kvm->slots_lock);
5f94c174
LV
188
189 return 0;
190}