blob: 53850177163fbda3a9fef4bda83aef31607ca4f4 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Laurent Vivier5f94c172008-05-30 16:05:54 +020014#include <linux/kvm.h>
15
16#include "coalesced_mmio.h"
17
Gregory Haskinsd76685c2009-06-01 12:54:50 -040018static 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
Michael S. Tsirkinbda90202009-06-29 22:24:32 +030023static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
24 gpa_t addr, int len)
Laurent Vivier5f94c172008-05-30 16:05:54 +020025{
Laurent Vivier5f94c172008-05-30 16:05:54 +020026 struct kvm_coalesced_mmio_zone *zone;
Avi Kivity105f8d42009-06-04 18:09:08 +030027 struct kvm_coalesced_mmio_ring *ring;
28 unsigned avail;
Laurent Vivier5f94c172008-05-30 16:05:54 +020029 int i;
30
Laurent Vivier5f94c172008-05-30 16:05:54 +020031 /* 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 */
Avi Kivity105f8d42009-06-04 18:09:08 +030037 ring = dev->kvm->coalesced_mmio_ring;
38 avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
Marcelo Tosatti64a22682009-06-04 15:08:22 -030039 if (avail < KVM_MAX_VCPUS) {
Laurent Vivier5f94c172008-05-30 16:05:54 +020040 /* 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
Michael S. Tsirkinbda90202009-06-29 22:24:32 +030060static int coalesced_mmio_write(struct kvm_io_device *this,
61 gpa_t addr, int len, const void *val)
Laurent Vivier5f94c172008-05-30 16:05:54 +020062{
Gregory Haskinsd76685c2009-06-01 12:54:50 -040063 struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
Laurent Vivier5f94c172008-05-30 16:05:54 +020064 struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
Michael S. Tsirkinbda90202009-06-29 22:24:32 +030065 if (!coalesced_mmio_in_range(dev, addr, len))
66 return -EOPNOTSUPP;
Laurent Vivier5f94c172008-05-30 16:05:54 +020067
Marcelo Tosatti64a22682009-06-04 15:08:22 -030068 spin_lock(&dev->lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +020069
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;
Marcelo Tosatti64a22682009-06-04 15:08:22 -030077 spin_unlock(&dev->lock);
Michael S. Tsirkinbda90202009-06-29 22:24:32 +030078 return 0;
Laurent Vivier5f94c172008-05-30 16:05:54 +020079}
80
81static void coalesced_mmio_destructor(struct kvm_io_device *this)
82{
Gregory Haskinsd76685c2009-06-01 12:54:50 -040083 struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
Gregory Haskins787a6602009-06-01 12:54:45 -040084
85 kfree(dev);
Laurent Vivier5f94c172008-05-30 16:05:54 +020086}
87
Gregory Haskinsd76685c2009-06-01 12:54:50 -040088static const struct kvm_io_device_ops coalesced_mmio_ops = {
89 .write = coalesced_mmio_write,
Gregory Haskinsd76685c2009-06-01 12:54:50 -040090 .destructor = coalesced_mmio_destructor,
91};
92
Laurent Vivier5f94c172008-05-30 16:05:54 +020093int kvm_coalesced_mmio_init(struct kvm *kvm)
94{
95 struct kvm_coalesced_mmio_dev *dev;
Avi Kivity980da6c2009-12-20 15:13:43 +020096 struct page *page;
Gregory Haskins090b7af2009-07-07 17:08:44 -040097 int ret;
Laurent Vivier5f94c172008-05-30 16:05:54 +020098
Avi Kivity980da6c2009-12-20 15:13:43 +020099 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;
Laurent Vivier5f94c172008-05-30 16:05:54 +0200106 dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
107 if (!dev)
Avi Kivity980da6c2009-12-20 15:13:43 +0200108 goto out_free_page;
Marcelo Tosatti64a22682009-06-04 15:08:22 -0300109 spin_lock_init(&dev->lock);
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400110 kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200111 dev->kvm = kvm;
112 kvm->coalesced_mmio_dev = dev;
Laurent Vivier5f94c172008-05-30 16:05:54 +0200113
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200114 mutex_lock(&kvm->slots_lock);
Marcelo Tosattie93f8a02009-12-23 14:35:24 -0200115 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev);
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200116 mutex_unlock(&kvm->slots_lock);
Gregory Haskins090b7af2009-07-07 17:08:44 -0400117 if (ret < 0)
Avi Kivity980da6c2009-12-20 15:13:43 +0200118 goto out_free_dev;
Gregory Haskins090b7af2009-07-07 17:08:44 -0400119
120 return ret;
Avi Kivity980da6c2009-12-20 15:13:43 +0200121
122out_free_dev:
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +0900123 kvm->coalesced_mmio_dev = NULL;
Avi Kivity980da6c2009-12-20 15:13:43 +0200124 kfree(dev);
125out_free_page:
Takuya Yoshikawa6ce5a092010-03-15 22:13:30 +0900126 kvm->coalesced_mmio_ring = NULL;
Avi Kivity980da6c2009-12-20 15:13:43 +0200127 __free_page(page);
128out_err:
129 return ret;
130}
131
132void kvm_coalesced_mmio_free(struct kvm *kvm)
133{
134 if (kvm->coalesced_mmio_ring)
135 free_page((unsigned long)kvm->coalesced_mmio_ring);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200136}
137
138int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
Jochen Maes43db6692010-02-08 11:29:33 +0100139 struct kvm_coalesced_mmio_zone *zone)
Laurent Vivier5f94c172008-05-30 16:05:54 +0200140{
141 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
142
143 if (dev == NULL)
Wei Yongjuna87fa352010-03-12 12:59:06 +0800144 return -ENXIO;
Laurent Vivier5f94c172008-05-30 16:05:54 +0200145
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200146 mutex_lock(&kvm->slots_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200147 if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) {
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200148 mutex_unlock(&kvm->slots_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200149 return -ENOBUFS;
150 }
151
152 dev->zone[dev->nb_zones] = *zone;
153 dev->nb_zones++;
154
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200155 mutex_unlock(&kvm->slots_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200156 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)
Wei Yongjuna87fa352010-03-12 12:59:06 +0800167 return -ENXIO;
Laurent Vivier5f94c172008-05-30 16:05:54 +0200168
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200169 mutex_lock(&kvm->slots_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200170
171 i = dev->nb_zones;
Jochen Maes43db6692010-02-08 11:29:33 +0100172 while (i) {
Laurent Vivier5f94c172008-05-30 16:05:54 +0200173 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
Marcelo Tosatti79fac952009-12-23 14:35:26 -0200187 mutex_unlock(&kvm->slots_lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200188
189 return 0;
190}