KVM: MMIO: Lock coalesced device when checking for available entry
[pandora-kernel.git] / virt / kvm / coalesced_mmio.c
1 /*
2  * KVM coalesced MMIO
3  *
4  * Copyright (c) 2008 Bull S.A.S.
5  * Copyright 2009 Red Hat, Inc. and/or its affiliates.
6  *
7  *  Author: Laurent Vivier <Laurent.Vivier@bull.net>
8  *
9  */
10
11 #include "iodev.h"
12
13 #include <linux/kvm_host.h>
14 #include <linux/slab.h>
15 #include <linux/kvm.h>
16
17 #include "coalesced_mmio.h"
18
19 static inline struct kvm_coalesced_mmio_dev *to_mmio(struct kvm_io_device *dev)
20 {
21         return container_of(dev, struct kvm_coalesced_mmio_dev, dev);
22 }
23
24 static int coalesced_mmio_in_range(struct kvm_coalesced_mmio_dev *dev,
25                                    gpa_t addr, int len)
26 {
27         struct kvm_coalesced_mmio_zone *zone;
28         int i;
29
30         /* is it in a batchable area ? */
31
32         for (i = 0; i < dev->nb_zones; i++) {
33                 zone = &dev->zone[i];
34
35                 /* (addr,len) is fully included in
36                  * (zone->addr, zone->size)
37                  */
38
39                 if (zone->addr <= addr &&
40                     addr + len <= zone->addr + zone->size)
41                         return 1;
42         }
43         return 0;
44 }
45
46 static int coalesced_mmio_has_room(struct kvm_coalesced_mmio_dev *dev)
47 {
48         struct kvm_coalesced_mmio_ring *ring;
49         unsigned avail;
50
51         /* Are we able to batch it ? */
52
53         /* last is the first free entry
54          * check if we don't meet the first used entry
55          * there is always one unused entry in the buffer
56          */
57         ring = dev->kvm->coalesced_mmio_ring;
58         avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX;
59         if (avail == 0) {
60                 /* full */
61                 return 0;
62         }
63
64         return 1;
65 }
66
67 static int coalesced_mmio_write(struct kvm_io_device *this,
68                                 gpa_t addr, int len, const void *val)
69 {
70         struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
71         struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
72
73         if (!coalesced_mmio_in_range(dev, addr, len))
74                 return -EOPNOTSUPP;
75
76         spin_lock(&dev->lock);
77
78         if (!coalesced_mmio_has_room(dev)) {
79                 spin_unlock(&dev->lock);
80                 return -EOPNOTSUPP;
81         }
82
83         /* copy data in first free entry of the ring */
84
85         ring->coalesced_mmio[ring->last].phys_addr = addr;
86         ring->coalesced_mmio[ring->last].len = len;
87         memcpy(ring->coalesced_mmio[ring->last].data, val, len);
88         smp_wmb();
89         ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX;
90         spin_unlock(&dev->lock);
91         return 0;
92 }
93
94 static void coalesced_mmio_destructor(struct kvm_io_device *this)
95 {
96         struct kvm_coalesced_mmio_dev *dev = to_mmio(this);
97
98         kfree(dev);
99 }
100
101 static const struct kvm_io_device_ops coalesced_mmio_ops = {
102         .write      = coalesced_mmio_write,
103         .destructor = coalesced_mmio_destructor,
104 };
105
106 int kvm_coalesced_mmio_init(struct kvm *kvm)
107 {
108         struct kvm_coalesced_mmio_dev *dev;
109         struct page *page;
110         int ret;
111
112         ret = -ENOMEM;
113         page = alloc_page(GFP_KERNEL | __GFP_ZERO);
114         if (!page)
115                 goto out_err;
116         kvm->coalesced_mmio_ring = page_address(page);
117
118         ret = -ENOMEM;
119         dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
120         if (!dev)
121                 goto out_free_page;
122         spin_lock_init(&dev->lock);
123         kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops);
124         dev->kvm = kvm;
125         kvm->coalesced_mmio_dev = dev;
126
127         mutex_lock(&kvm->slots_lock);
128         ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, &dev->dev);
129         mutex_unlock(&kvm->slots_lock);
130         if (ret < 0)
131                 goto out_free_dev;
132
133         return ret;
134
135 out_free_dev:
136         kvm->coalesced_mmio_dev = NULL;
137         kfree(dev);
138 out_free_page:
139         kvm->coalesced_mmio_ring = NULL;
140         __free_page(page);
141 out_err:
142         return ret;
143 }
144
145 void kvm_coalesced_mmio_free(struct kvm *kvm)
146 {
147         if (kvm->coalesced_mmio_ring)
148                 free_page((unsigned long)kvm->coalesced_mmio_ring);
149 }
150
151 int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
152                                          struct kvm_coalesced_mmio_zone *zone)
153 {
154         struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
155
156         if (dev == NULL)
157                 return -ENXIO;
158
159         mutex_lock(&kvm->slots_lock);
160         if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) {
161                 mutex_unlock(&kvm->slots_lock);
162                 return -ENOBUFS;
163         }
164
165         dev->zone[dev->nb_zones] = *zone;
166         dev->nb_zones++;
167
168         mutex_unlock(&kvm->slots_lock);
169         return 0;
170 }
171
172 int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
173                                            struct kvm_coalesced_mmio_zone *zone)
174 {
175         int i;
176         struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
177         struct kvm_coalesced_mmio_zone *z;
178
179         if (dev == NULL)
180                 return -ENXIO;
181
182         mutex_lock(&kvm->slots_lock);
183
184         i = dev->nb_zones;
185         while (i) {
186                 z = &dev->zone[i - 1];
187
188                 /* unregister all zones
189                  * included in (zone->addr, zone->size)
190                  */
191
192                 if (zone->addr <= z->addr &&
193                     z->addr + z->size <= zone->addr + zone->size) {
194                         dev->nb_zones--;
195                         *z = dev->zone[dev->nb_zones];
196                 }
197                 i--;
198         }
199
200         mutex_unlock(&kvm->slots_lock);
201
202         return 0;
203 }