blob: 397f419366987ce86059af806258f41132870a77 [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
Laurent Vivier5f94c172008-05-30 16:05:54 +020022static int coalesced_mmio_in_range(struct kvm_io_device *this,
23 gpa_t addr, int len, int is_write)
24{
Gregory Haskinsd76685c2009-06-01 12:54:50 -040025 struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
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
31 if (!is_write)
32 return 0;
33
Laurent Vivier5f94c172008-05-30 16:05:54 +020034 /* Are we able to batch it ? */
35
36 /* last is the first free entry
37 * check if we don't meet the first used entry
38 * there is always one unused entry in the buffer
39 */
Avi Kivity105f8d42009-06-04 18:09:08 +030040 ring = dev->kvm->coalesced_mmio_ring;
41 avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
Marcelo Tosatti64a22682009-06-04 15:08:22 -030042 if (avail < KVM_MAX_VCPUS) {
Laurent Vivier5f94c172008-05-30 16:05:54 +020043 /* full */
44 return 0;
45 }
46
47 /* is it in a batchable area ? */
48
49 for (i = 0; i < dev->nb_zones; i++) {
50 zone = &dev->zone[i];
51
52 /* (addr,len) is fully included in
53 * (zone->addr, zone->size)
54 */
55
56 if (zone->addr <= addr &&
57 addr + len <= zone->addr + zone->size)
58 return 1;
59 }
60 return 0;
61}
62
63static void coalesced_mmio_write(struct kvm_io_device *this,
64 gpa_t addr, int len, const void *val)
65{
Gregory Haskinsd76685c2009-06-01 12:54:50 -040066 struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
Laurent Vivier5f94c172008-05-30 16:05:54 +020067 struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
68
Marcelo Tosatti64a22682009-06-04 15:08:22 -030069 spin_lock(&dev->lock);
Laurent Vivier5f94c172008-05-30 16:05:54 +020070
71 /* copy data in first free entry of the ring */
72
73 ring->coalesced_mmio[ring->last].phys_addr = addr;
74 ring->coalesced_mmio[ring->last].len = len;
75 memcpy(ring->coalesced_mmio[ring->last].data, val, len);
76 smp_wmb();
77 ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX;
Marcelo Tosatti64a22682009-06-04 15:08:22 -030078 spin_unlock(&dev->lock);
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,
90 .in_range = coalesced_mmio_in_range,
91 .destructor = coalesced_mmio_destructor,
92};
93
Laurent Vivier5f94c172008-05-30 16:05:54 +020094int kvm_coalesced_mmio_init(struct kvm *kvm)
95{
96 struct kvm_coalesced_mmio_dev *dev;
97
98 dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
99 if (!dev)
100 return -ENOMEM;
Marcelo Tosatti64a22682009-06-04 15:08:22 -0300101 spin_lock_init(&dev->lock);
Gregory Haskinsd76685c2009-06-01 12:54:50 -0400102 kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
Laurent Vivier5f94c172008-05-30 16:05:54 +0200103 dev->kvm = kvm;
104 kvm->coalesced_mmio_dev = dev;
105 kvm_io_bus_register_dev(&kvm->mmio_bus, &dev->dev);
106
107 return 0;
108}
109
110int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
111 struct kvm_coalesced_mmio_zone *zone)
112{
113 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
114
115 if (dev == NULL)
116 return -EINVAL;
117
118 mutex_lock(&kvm->lock);
119 if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) {
120 mutex_unlock(&kvm->lock);
121 return -ENOBUFS;
122 }
123
124 dev->zone[dev->nb_zones] = *zone;
125 dev->nb_zones++;
126
127 mutex_unlock(&kvm->lock);
128 return 0;
129}
130
131int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
132 struct kvm_coalesced_mmio_zone *zone)
133{
134 int i;
135 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
136 struct kvm_coalesced_mmio_zone *z;
137
138 if (dev == NULL)
139 return -EINVAL;
140
141 mutex_lock(&kvm->lock);
142
143 i = dev->nb_zones;
144 while(i) {
145 z = &dev->zone[i - 1];
146
147 /* unregister all zones
148 * included in (zone->addr, zone->size)
149 */
150
151 if (zone->addr <= z->addr &&
152 z->addr + z->size <= zone->addr + zone->size) {
153 dev->nb_zones--;
154 *z = dev->zone[dev->nb_zones];
155 }
156 i--;
157 }
158
159 mutex_unlock(&kvm->lock);
160
161 return 0;
162}