blob: 5169736377a3b88cb6d2c7abc7639e2da439cf64 [file] [log] [blame]
Laurent Vivier5f94c172008-05-30 16:05:54 +02001/*
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
Gregory Haskinsd76685c2009-06-01 12:54:50 -040017static 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
Michael S. Tsirkinbda90202009-06-29 22:24:32 +030022static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
23 gpa_t addr, int len)
Laurent Vivier5f94c172008-05-30 16:05:54 +020024{
Laurent Vivier5f94c172008-05-30 16:05:54 +020025 struct kvm_coalesced_mmio_zone *zone;
Avi Kivity105f8d42009-06-04 18:09:08 +030026 struct kvm_coalesced_mmio_ring *ring;
27 unsigned avail;
Laurent Vivier5f94c172008-05-30 16:05:54 +020028 int i;
29
Laurent Vivier5f94c172008-05-30 16:05:54 +020030 /* 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 */
Avi Kivity105f8d42009-06-04 18:09:08 +030036 ring = dev->kvm->coalesced_mmio_ring;
37 avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
Marcelo Tosatti64a22682009-06-04 15:08:22 -030038 if (avail < KVM_MAX_VCPUS) {
Laurent Vivier5f94c172008-05-30 16:05:54 +020039 /* 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
Michael S. Tsirkinbda90202009-06-29 22:24:32 +030059static int coalesced_mmio_write(struct kvm_io_device *this,
60 gpa_t addr, int len, const void *val)
Laurent Vivier5f94c172008-05-30 16:05:54 +020061{
Gregory Haskinsd76685c2009-06-01 12:54:50 -040062 struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
Laurent Vivier5f94c172008-05-30 16:05:54 +020063 struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +030064 if (!coalesced_mmio_in_range(dev, addr, len))
65 return -EOPNOTSUPP;
Laurent Vivier5f94c172008-05-30 16:05:54 +020066
Marcelo Tosatti64a22682009-06-04 15:08:22 -030067 spin_lock(&dev->lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +020068
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;
Marcelo Tosatti64a22682009-06-04 15:08:22 -030076 spin_unlock(&dev->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +030077 return 0;
Laurent Vivier5f94c172008-05-30 16:05:54 +020078}
79
80static void coalesced_mmio_destructor(struct kvm_io_device *this)
81{
Gregory Haskinsd76685c2009-06-01 12:54:50 -040082 struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
Gregory Haskins787a6602009-06-01 12:54:45 -040083
84 kfree(dev);
Laurent Vivier5f94c172008-05-30 16:05:54 +020085}
86
Gregory Haskinsd76685c2009-06-01 12:54:50 -040087static const struct kvm_io_device_ops coalesced_mmio_ops = {
88 .write = coalesced_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -040089 .destructor = coalesced_mmio_destructor,
90};
91
Laurent Vivier5f94c172008-05-30 16:05:54 +020092int kvm_coalesced_mmio_init(struct kvm *kvm)
93{
94 struct kvm_coalesced_mmio_dev *dev;
Avi Kivity980da6c2009-12-20 15:13:43 +020095 struct page *page;
Gregory Haskins090b7af2009-07-07 17:08:44 -040096 int ret;
Laurent Vivier5f94c172008-05-30 16:05:54 +020097
Avi Kivity980da6c2009-12-20 15:13:43 +020098 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;
Laurent Vivier5f94c172008-05-30 16:05:54 +0200105 dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
106 if (!dev)
Avi Kivity980da6c2009-12-20 15:13:43 +0200107 goto out_free_page;
Marcelo Tosatti64a22682009-06-04 15:08:22 -0300108 spin_lock_init(&dev->lock);
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400109 kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200110 dev->kvm = kvm;
111 kvm->coalesced_mmio_dev = dev;
Laurent Vivier5f94c172008-05-30 16:05:54 +0200112
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200113 mutex_lock(&kvm->slots_lock);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200114 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200115 mutex_unlock(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400116 if (ret < 0)
Avi Kivity980da6c2009-12-20 15:13:43 +0200117 goto out_free_dev;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400118
119 return ret;
Avi Kivity980da6c2009-12-20 15:13:43 +0200120
121out_free_dev:
122 kfree(dev);
123out_free_page:
124 __free_page(page);
125out_err:
126 return ret;
127}
128
129void kvm_coalesced_mmio_free(struct kvm *kvm)
130{
131 if (kvm->coalesced_mmio_ring)
132 free_page((unsigned long)kvm->coalesced_mmio_ring);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200133}
134
135int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
Jochen Maes43db6692010-02-08 11:29:33 +0100136 struct kvm_coalesced_mmio_zone *zone)
Laurent Vivier5f94c172008-05-30 16:05:54 +0200137{
138 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
139
140 if (dev == NULL)
141 return -EINVAL;
142
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200143 mutex_lock(&kvm->slots_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200144 if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) {
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200145 mutex_unlock(&kvm->slots_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200146 return -ENOBUFS;
147 }
148
149 dev->zone[dev->nb_zones] = *zone;
150 dev->nb_zones++;
151
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200152 mutex_unlock(&kvm->slots_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200153 return 0;
154}
155
156int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
157 struct kvm_coalesced_mmio_zone *zone)
158{
159 int i;
160 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
161 struct kvm_coalesced_mmio_zone *z;
162
163 if (dev == NULL)
164 return -EINVAL;
165
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200166 mutex_lock(&kvm->slots_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200167
168 i = dev->nb_zones;
Jochen Maes43db6692010-02-08 11:29:33 +0100169 while (i) {
Laurent Vivier5f94c172008-05-30 16:05:54 +0200170 z = &dev->zone[i - 1];
171
172 /* unregister all zones
173 * included in (zone->addr, zone->size)
174 */
175
176 if (zone->addr <= z->addr &&
177 z->addr + z->size <= zone->addr + zone->size) {
178 dev->nb_zones--;
179 *z = dev->zone[dev->nb_zones];
180 }
181 i--;
182 }
183
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200184 mutex_unlock(&kvm->slots_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200185
186 return 0;
187}