]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - virt/kvm/coalesced_mmio.c
KVM: Clean up coalesced_mmio destruction
[mirror_ubuntu-bionic-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>
13#include <linux/kvm.h>
14
15#include "coalesced_mmio.h"
16
17static int coalesced_mmio_in_range(struct kvm_io_device *this,
18 gpa_t addr, int len, int is_write)
19{
20 struct kvm_coalesced_mmio_dev *dev =
21 (struct kvm_coalesced_mmio_dev*)this->private;
22 struct kvm_coalesced_mmio_zone *zone;
23 int next;
24 int i;
25
26 if (!is_write)
27 return 0;
28
29 /* kvm->lock is taken by the caller and must be not released before
30 * dev.read/write
31 */
32
33 /* Are we able to batch it ? */
34
35 /* last is the first free entry
36 * check if we don't meet the first used entry
37 * there is always one unused entry in the buffer
38 */
39
40 next = (dev->kvm->coalesced_mmio_ring->last + 1) %
41 KVM_COALESCED_MMIO_MAX;
42 if (next == dev->kvm->coalesced_mmio_ring->first) {
43 /* 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{
66 struct kvm_coalesced_mmio_dev *dev =
67 (struct kvm_coalesced_mmio_dev*)this->private;
68 struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring;
69
70 /* kvm->lock must be taken by caller before call to in_range()*/
71
72 /* copy data in first free entry of the ring */
73
74 ring->coalesced_mmio[ring->last].phys_addr = addr;
75 ring->coalesced_mmio[ring->last].len = len;
76 memcpy(ring->coalesced_mmio[ring->last].data, val, len);
77 smp_wmb();
78 ring->last = (ring->last + 1) % KVM_COALESCED_MMIO_MAX;
79}
80
81static void coalesced_mmio_destructor(struct kvm_io_device *this)
82{
787a660a
GH
83 struct kvm_coalesced_mmio_dev *dev =
84 (struct kvm_coalesced_mmio_dev *)this->private;
85
86 kfree(dev);
5f94c174
LV
87}
88
89int kvm_coalesced_mmio_init(struct kvm *kvm)
90{
91 struct kvm_coalesced_mmio_dev *dev;
92
93 dev = kzalloc(sizeof(struct kvm_coalesced_mmio_dev), GFP_KERNEL);
94 if (!dev)
95 return -ENOMEM;
96 dev->dev.write = coalesced_mmio_write;
97 dev->dev.in_range = coalesced_mmio_in_range;
98 dev->dev.destructor = coalesced_mmio_destructor;
99 dev->dev.private = dev;
100 dev->kvm = kvm;
101 kvm->coalesced_mmio_dev = dev;
102 kvm_io_bus_register_dev(&kvm->mmio_bus, &dev->dev);
103
104 return 0;
105}
106
107int kvm_vm_ioctl_register_coalesced_mmio(struct kvm *kvm,
108 struct kvm_coalesced_mmio_zone *zone)
109{
110 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
111
112 if (dev == NULL)
113 return -EINVAL;
114
115 mutex_lock(&kvm->lock);
116 if (dev->nb_zones >= KVM_COALESCED_MMIO_ZONE_MAX) {
117 mutex_unlock(&kvm->lock);
118 return -ENOBUFS;
119 }
120
121 dev->zone[dev->nb_zones] = *zone;
122 dev->nb_zones++;
123
124 mutex_unlock(&kvm->lock);
125 return 0;
126}
127
128int kvm_vm_ioctl_unregister_coalesced_mmio(struct kvm *kvm,
129 struct kvm_coalesced_mmio_zone *zone)
130{
131 int i;
132 struct kvm_coalesced_mmio_dev *dev = kvm->coalesced_mmio_dev;
133 struct kvm_coalesced_mmio_zone *z;
134
135 if (dev == NULL)
136 return -EINVAL;
137
138 mutex_lock(&kvm->lock);
139
140 i = dev->nb_zones;
141 while(i) {
142 z = &dev->zone[i - 1];
143
144 /* unregister all zones
145 * included in (zone->addr, zone->size)
146 */
147
148 if (zone->addr <= z->addr &&
149 z->addr + z->size <= zone->addr + zone->size) {
150 dev->nb_zones--;
151 *z = dev->zone[dev->nb_zones];
152 }
153 i--;
154 }
155
156 mutex_unlock(&kvm->lock);
157
158 return 0;
159}