32ed8dbd93d6456bdd69c502ea706210508ca5f2
[cascardo/linux.git] / virt / kvm / arm / vgic / vgic-mmio.c
1 /*
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
24 unsigned long vgic_mmio_read_raz(struct kvm_vcpu *vcpu,
25                                  gpa_t addr, unsigned int len)
26 {
27         return 0;
28 }
29
30 unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu,
31                                  gpa_t addr, unsigned int len)
32 {
33         return -1UL;
34 }
35
36 void vgic_mmio_write_wi(struct kvm_vcpu *vcpu, gpa_t addr,
37                         unsigned int len, unsigned long val)
38 {
39         /* Ignore */
40 }
41
42 /*
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  */
46 unsigned 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
64 void 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
80 void 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
98 static int match_region(const void *key, const void *elt)
99 {
100         const unsigned int offset = (unsigned long)key;
101         const struct vgic_register_region *region = elt;
102
103         if (offset < region->reg_offset)
104                 return -1;
105
106         if (offset >= region->reg_offset + region->len)
107                 return 1;
108
109         return 0;
110 }
111
112 /* Find the proper register handler entry given a certain address offset. */
113 static const struct vgic_register_region *
114 vgic_find_mmio_region(const struct vgic_register_region *region, int nr_regions,
115                       unsigned int offset)
116 {
117         return bsearch((void *)(uintptr_t)offset, region, nr_regions,
118                        sizeof(region[0]), match_region);
119 }
120
121 /*
122  * kvm_mmio_read_buf() returns a value in a format where it can be converted
123  * to a byte array and be directly observed as the guest wanted it to appear
124  * in memory if it had done the store itself, which is LE for the GIC, as the
125  * guest knows the GIC is always LE.
126  *
127  * We convert this value to the CPUs native format to deal with it as a data
128  * value.
129  */
130 unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len)
131 {
132         unsigned long data = kvm_mmio_read_buf(val, len);
133
134         switch (len) {
135         case 1:
136                 return data;
137         case 2:
138                 return le16_to_cpu(data);
139         case 4:
140                 return le32_to_cpu(data);
141         default:
142                 return le64_to_cpu(data);
143         }
144 }
145
146 /*
147  * kvm_mmio_write_buf() expects a value in a format such that if converted to
148  * a byte array it is observed as the guest would see it if it could perform
149  * the load directly.  Since the GIC is LE, and the guest knows this, the
150  * guest expects a value in little endian format.
151  *
152  * We convert the data value from the CPUs native format to LE so that the
153  * value is returned in the proper format.
154  */
155 void vgic_data_host_to_mmio_bus(void *buf, unsigned int len,
156                                 unsigned long data)
157 {
158         switch (len) {
159         case 1:
160                 break;
161         case 2:
162                 data = cpu_to_le16(data);
163                 break;
164         case 4:
165                 data = cpu_to_le32(data);
166                 break;
167         default:
168                 data = cpu_to_le64(data);
169         }
170
171         kvm_mmio_write_buf(buf, len, data);
172 }
173
174 static
175 struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev)
176 {
177         return container_of(dev, struct vgic_io_device, dev);
178 }
179
180 static bool check_region(const struct vgic_register_region *region,
181                          gpa_t addr, int len)
182 {
183         if ((region->access_flags & VGIC_ACCESS_8bit) && len == 1)
184                 return true;
185         if ((region->access_flags & VGIC_ACCESS_32bit) &&
186             len == sizeof(u32) && !(addr & 3))
187                 return true;
188         if ((region->access_flags & VGIC_ACCESS_64bit) &&
189             len == sizeof(u64) && !(addr & 7))
190                 return true;
191
192         return false;
193 }
194
195 static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
196                               gpa_t addr, int len, void *val)
197 {
198         struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
199         const struct vgic_register_region *region;
200         struct kvm_vcpu *r_vcpu;
201         unsigned long data;
202
203         region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
204                                        addr - iodev->base_addr);
205         if (!region || !check_region(region, addr, len)) {
206                 memset(val, 0, len);
207                 return 0;
208         }
209
210         r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
211         data = region->read(r_vcpu, addr, len);
212         vgic_data_host_to_mmio_bus(val, len, data);
213         return 0;
214 }
215
216 static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev,
217                                gpa_t addr, int len, const void *val)
218 {
219         struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev);
220         const struct vgic_register_region *region;
221         struct kvm_vcpu *r_vcpu;
222         unsigned long data = vgic_data_mmio_bus_to_host(val, len);
223
224         region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions,
225                                        addr - iodev->base_addr);
226         if (!region)
227                 return 0;
228
229         if (!check_region(region, addr, len))
230                 return 0;
231
232         r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu;
233         region->write(r_vcpu, addr, len, data);
234         return 0;
235 }
236
237 struct kvm_io_device_ops kvm_io_gic_ops = {
238         .read = dispatch_mmio_read,
239         .write = dispatch_mmio_write,
240 };
241
242 int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,
243                              enum vgic_type type)
244 {
245         struct vgic_io_device *io_device = &kvm->arch.vgic.dist_iodev;
246         int ret = 0;
247         unsigned int len;
248
249         switch (type) {
250         case VGIC_V2:
251                 len = vgic_v2_init_dist_iodev(io_device);
252                 break;
253         default:
254                 BUG_ON(1);
255         }
256
257         io_device->base_addr = dist_base_address;
258         io_device->redist_vcpu = NULL;
259
260         mutex_lock(&kvm->slots_lock);
261         ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, dist_base_address,
262                                       len, &io_device->dev);
263         mutex_unlock(&kvm->slots_lock);
264
265         return ret;
266 }