Commit | Line | Data |
---|---|---|
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 | ||
d76685c4 GH |
17 | static 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 | ||
5f94c174 LV |
22 | static int coalesced_mmio_in_range(struct kvm_io_device *this, |
23 | gpa_t addr, int len, int is_write) | |
24 | { | |
d76685c4 | 25 | struct kvm_coalesced_mmio_dev *dev = to_mmio(this); |
5f94c174 | 26 | struct kvm_coalesced_mmio_zone *zone; |
105f8d40 AK |
27 | struct kvm_coalesced_mmio_ring *ring; |
28 | unsigned avail; | |
5f94c174 LV |
29 | int i; |
30 | ||
31 | if (!is_write) | |
32 | return 0; | |
33 | ||
5f94c174 LV |
34 | /* 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 | */ | |
105f8d40 AK |
40 | ring = dev->kvm->coalesced_mmio_ring; |
41 | avail = (ring->first - ring->last - 1) % KVM_COALESCED_MMIO_MAX; | |
64a2268d | 42 | if (avail < KVM_MAX_VCPUS) { |
5f94c174 LV |
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 | ||
63 | static void coalesced_mmio_write(struct kvm_io_device *this, | |
64 | gpa_t addr, int len, const void *val) | |
65 | { | |
d76685c4 | 66 | struct kvm_coalesced_mmio_dev *dev = to_mmio(this); |
5f94c174 LV |
67 | struct kvm_coalesced_mmio_ring *ring = dev->kvm->coalesced_mmio_ring; |
68 | ||
64a2268d | 69 | spin_lock(&dev->lock); |
5f94c174 LV |
70 | |
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; | |
64a2268d | 78 | spin_unlock(&dev->lock); |
5f94c174 LV |
79 | } |
80 | ||
81 | static void coalesced_mmio_destructor(struct kvm_io_device *this) | |
82 | { | |
d76685c4 | 83 | struct kvm_coalesced_mmio_dev *dev = to_mmio(this); |
787a660a GH |
84 | |
85 | kfree(dev); | |
5f94c174 LV |
86 | } |
87 | ||
d76685c4 GH |
88 | static 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 | ||
5f94c174 LV |
94 | int 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; | |
64a2268d | 101 | spin_lock_init(&dev->lock); |
d76685c4 | 102 | kvm_iodevice_init(&dev->dev, &coalesced_mmio_ops); |
5f94c174 LV |
103 | 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 | ||
110 | int 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 | ||
131 | int 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 | } |