blob: d8dc8f6480dd10a5935e0eeafb2794705bc29916 [file] [log] [blame]
Marc Zyngier4493b1c2016-04-26 11:06:12 +01001/*
2 * VGIC MMIO handling functions
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/bitops.h>
15#include <linux/bsearch.h>
16#include <linux/kvm.h>
17#include <linux/kvm_host.h>
18#include <kvm/iodev.h>
19#include <kvm/arm_vgic.h>
20
21#include "vgic.h"
22#include "vgic-mmio.h"
23
24unsigned long vgic_mmio_read_raz(struct kvm_vcpu *vcpu,
25 gpa_t addr, unsigned int len)
26{
27 return 0;
28}
29
30unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu,
31 gpa_t addr, unsigned int len)
32{
33 return -1UL;
34}
35
36void vgic_mmio_write_wi(struct kvm_vcpu *vcpu, gpa_t addr,
37 unsigned int len, unsigned long val)
38{
39 /* Ignore */
40}
41
Andre Przywarafd122e62015-12-01 14:33:05 +000042/*
43 * Read accesses to both GICD_ICENABLER and GICD_ISENABLER return the value
44 * of the enabled bit, so there is only one function for both here.
45 */
46unsigned long vgic_mmio_read_enable(struct kvm_vcpu *vcpu,
47 gpa_t addr, unsigned int len)
48{
49 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
50 u32 value = 0;
51 int i;
52
53 /* Loop over all IRQs affected by this read */
54 for (i = 0; i < len * 8; i++) {
55 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
56
57 if (irq->enabled)
58 value |= (1U << i);
59 }
60
61 return value;
62}
63
64void vgic_mmio_write_senable(struct kvm_vcpu *vcpu,
65 gpa_t addr, unsigned int len,
66 unsigned long val)
67{
68 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
69 int i;
70
71 for_each_set_bit(i, &val, len * 8) {
72 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
73
74 spin_lock(&irq->irq_lock);
75 irq->enabled = true;
76 vgic_queue_irq_unlock(vcpu->kvm, irq);
77 }
78}
79
80void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu,
81 gpa_t addr, unsigned int len,
82 unsigned long val)
83{
84 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
85 int i;
86
87 for_each_set_bit(i, &val, len * 8) {
88 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
89
90 spin_lock(&irq->irq_lock);
91
92 irq->enabled = false;
93
94 spin_unlock(&irq->irq_lock);
95 }
96}
97
Andre Przywara96b29802015-12-01 14:33:41 +000098unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu,
99 gpa_t addr, unsigned int len)
100{
101 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
102 u32 value = 0;
103 int i;
104
105 /* Loop over all IRQs affected by this read */
106 for (i = 0; i < len * 8; i++) {
107 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
108
109 if (irq->pending)
110 value |= (1U << i);
111 }
112
113 return value;
114}
115
116void vgic_mmio_write_spending(struct kvm_vcpu *vcpu,
117 gpa_t addr, unsigned int len,
118 unsigned long val)
119{
120 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
121 int i;
122
123 for_each_set_bit(i, &val, len * 8) {
124 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
125
126 spin_lock(&irq->irq_lock);
127 irq->pending = true;
128 if (irq->config == VGIC_CONFIG_LEVEL)
129 irq->soft_pending = true;
130
131 vgic_queue_irq_unlock(vcpu->kvm, irq);
132 }
133}
134
135void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu,
136 gpa_t addr, unsigned int len,
137 unsigned long val)
138{
139 u32 intid = VGIC_ADDR_TO_INTID(addr, 1);
140 int i;
141
142 for_each_set_bit(i, &val, len * 8) {
143 struct vgic_irq *irq = vgic_get_irq(vcpu->kvm, vcpu, intid + i);
144
145 spin_lock(&irq->irq_lock);
146
147 if (irq->config == VGIC_CONFIG_LEVEL) {
148 irq->soft_pending = false;
149 irq->pending = irq->line_level;
150 } else {
151 irq->pending = false;
152 }
153
154 spin_unlock(&irq->irq_lock);
155 }
156}
157
Marc Zyngier4493b1c2016-04-26 11:06:12 +0100158static int match_region(const void *key, const void *elt)
159{
160 const unsigned int offset = (unsigned long)key;
161 const struct vgic_register_region *region = elt;
162
163 if (offset < region->reg_offset)
164 return -1;
165
166 if (offset >= region->reg_offset + region->len)
167 return 1;
168
169 return 0;
170}
171
172/* Find the proper register handler entry given a certain address offset. */
173static const struct vgic_register_region *
174vgic_find_mmio_region(const struct vgic_register_region *region, int nr_regions,
175 unsigned int offset)
176{
177 return bsearch((void *)(uintptr_t)offset, region, nr_regions,
178 sizeof(region[0]), match_region);
179}
180
181/*
182 * kvm_mmio_read_buf() returns a value in a format where it can be converted
183 * to a byte array and be directly observed as the guest wanted it to appear
184 * in memory if it had done the store itself, which is LE for the GIC, as the
185 * guest knows the GIC is always LE.
186 *
187 * We convert this value to the CPUs native format to deal with it as a data
188 * value.
189 */
190unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len)
191{
192 unsigned long data = kvm_mmio_read_buf(val, len);
193
194 switch (len) {
195 case 1:
196 return data;
197 case 2:
198 return le16_to_cpu(data);
199 case 4:
200 return le32_to_cpu(data);
201 default:
202 return le64_to_cpu(data);
203 }
204}
205
206/*
207 * kvm_mmio_write_buf() expects a value in a format such that if converted to
208 * a byte array it is observed as the guest would see it if it could perform
209 * the load directly. Since the GIC is LE, and the guest knows this, the
210 * guest expects a value in little endian format.
211 *
212 * We convert the data value from the CPUs native format to LE so that the
213 * value is returned in the proper format.
214 */
215void vgic_data_host_to_mmio_bus(void *buf, unsigned int len,
216 unsigned long data)
217{
218 switch (len) {
219 case 1:
220 break;
221 case 2:
222 data = cpu_to_le16(data);
223 break;
224 case 4:
225 data = cpu_to_le32(data);
226 break;
227 default:
228 data = cpu_to_le64(data);
229 }
230
231 kvm_mmio_write_buf(buf, len, data);
232}
233
234static
235struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev)
236{
237 return container_of(dev, struct vgic_io_device, dev);
238}
239
240static bool check_region(const struct vgic_register_region *region,
241 gpa_t addr, int len)
242{
243 if ((region->access_flags & VGIC_ACCESS_8bit) && len == 1)
244 return true;
245 if ((region->access_flags & VGIC_ACCESS_32bit) &&
246 len == sizeof(u32) && !(addr & 3))
247 return true;
248 if ((region->access_flags & VGIC_ACCESS_64bit) &&
249 len == sizeof(u64) && !(addr & 7))
250 return true;
251
252 return false;
253}
254
255static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
256 gpa_t addr, int len, void *val)
257{
258 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
259 const struct vgic_register_region *region;
260 struct kvm_vcpu *r_vcpu;
261 unsigned long data;
262
263 region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
264 addr - iodev->base_addr);
265 if (!region || !check_region(region, addr, len)) {
266 memset(val, 0, len);
267 return 0;
268 }
269
270 r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
271 data = region->read(r_vcpu, addr, len);
272 vgic_data_host_to_mmio_bus(val, len, data);
273 return 0;
274}
275
276static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
277 gpa_t addr, int len, const void *val)
278{
279 struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
280 const struct vgic_register_region *region;
281 struct kvm_vcpu *r_vcpu;
282 unsigned long data = vgic_data_mmio_bus_to_host(val, len);
283
284 region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
285 addr - iodev->base_addr);
286 if (!region)
287 return 0;
288
289 if (!check_region(region, addr, len))
290 return 0;
291
292 r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
293 region->write(r_vcpu, addr, len, data);
294 return 0;
295}
296
297struct kvm_io_device_ops kvm_io_gic_ops = {
298 .read = dispatch_mmio_read,
299 .write = dispatch_mmio_write,
300};
Andre Przywarafb848db2016-04-26 21:32:49 +0100301
302int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
303 enum vgic_type type)
304{
305 struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev;
306 int ret = 0;
307 unsigned int len;
308
309 switch (type) {
310 case VGIC_V2:
311 len = vgic_v2_init_dist_iodev(io_device);
312 break;
313 default:
314 BUG_ON(1);
315 }
316
317 io_device->base_addr = dist_base_address;
318 io_device->redist_vcpu = NULL;
319
320 mutex_lock(&kvm->slots_lock);
321 ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, dist_base_address,
322 len, &io_device->dev);
323 mutex_unlock(&kvm->slots_lock);
324
325 return ret;
326}