Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame^] | 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 | static int match_region(const void *key, const void *elt) |
| 43 | { |
| 44 | const unsigned int offset = (unsigned long)key; |
| 45 | const struct vgic_register_region *region = elt; |
| 46 | |
| 47 | if (offset < region->reg_offset) |
| 48 | return -1; |
| 49 | |
| 50 | if (offset >= region->reg_offset + region->len) |
| 51 | return 1; |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | /* Find the proper register handler entry given a certain address offset. */ |
| 57 | static const struct vgic_register_region * |
| 58 | vgic_find_mmio_region(const struct vgic_register_region *region, int nr_regions, |
| 59 | unsigned int offset) |
| 60 | { |
| 61 | return bsearch((void *)(uintptr_t)offset, region, nr_regions, |
| 62 | sizeof(region[0]), match_region); |
| 63 | } |
| 64 | |
| 65 | /* |
| 66 | * kvm_mmio_read_buf() returns a value in a format where it can be converted |
| 67 | * to a byte array and be directly observed as the guest wanted it to appear |
| 68 | * in memory if it had done the store itself, which is LE for the GIC, as the |
| 69 | * guest knows the GIC is always LE. |
| 70 | * |
| 71 | * We convert this value to the CPUs native format to deal with it as a data |
| 72 | * value. |
| 73 | */ |
| 74 | unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len) |
| 75 | { |
| 76 | unsigned long data = kvm_mmio_read_buf(val, len); |
| 77 | |
| 78 | switch (len) { |
| 79 | case 1: |
| 80 | return data; |
| 81 | case 2: |
| 82 | return le16_to_cpu(data); |
| 83 | case 4: |
| 84 | return le32_to_cpu(data); |
| 85 | default: |
| 86 | return le64_to_cpu(data); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * kvm_mmio_write_buf() expects a value in a format such that if converted to |
| 92 | * a byte array it is observed as the guest would see it if it could perform |
| 93 | * the load directly. Since the GIC is LE, and the guest knows this, the |
| 94 | * guest expects a value in little endian format. |
| 95 | * |
| 96 | * We convert the data value from the CPUs native format to LE so that the |
| 97 | * value is returned in the proper format. |
| 98 | */ |
| 99 | void vgic_data_host_to_mmio_bus(void *buf, unsigned int len, |
| 100 | unsigned long data) |
| 101 | { |
| 102 | switch (len) { |
| 103 | case 1: |
| 104 | break; |
| 105 | case 2: |
| 106 | data = cpu_to_le16(data); |
| 107 | break; |
| 108 | case 4: |
| 109 | data = cpu_to_le32(data); |
| 110 | break; |
| 111 | default: |
| 112 | data = cpu_to_le64(data); |
| 113 | } |
| 114 | |
| 115 | kvm_mmio_write_buf(buf, len, data); |
| 116 | } |
| 117 | |
| 118 | static |
| 119 | struct vgic_io_device *kvm_to_vgic_iodev(const struct kvm_io_device *dev) |
| 120 | { |
| 121 | return container_of(dev, struct vgic_io_device, dev); |
| 122 | } |
| 123 | |
| 124 | static bool check_region(const struct vgic_register_region *region, |
| 125 | gpa_t addr, int len) |
| 126 | { |
| 127 | if ((region->access_flags & VGIC_ACCESS_8bit) && len == 1) |
| 128 | return true; |
| 129 | if ((region->access_flags & VGIC_ACCESS_32bit) && |
| 130 | len == sizeof(u32) && !(addr & 3)) |
| 131 | return true; |
| 132 | if ((region->access_flags & VGIC_ACCESS_64bit) && |
| 133 | len == sizeof(u64) && !(addr & 7)) |
| 134 | return true; |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | static int dispatch_mmio_read(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, |
| 140 | gpa_t addr, int len, void *val) |
| 141 | { |
| 142 | struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev); |
| 143 | const struct vgic_register_region *region; |
| 144 | struct kvm_vcpu *r_vcpu; |
| 145 | unsigned long data; |
| 146 | |
| 147 | region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions, |
| 148 | addr - iodev->base_addr); |
| 149 | if (!region || !check_region(region, addr, len)) { |
| 150 | memset(val, 0, len); |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu; |
| 155 | data = region->read(r_vcpu, addr, len); |
| 156 | vgic_data_host_to_mmio_bus(val, len, data); |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | static int dispatch_mmio_write(struct kvm_vcpu *vcpu, struct kvm_io_device *dev, |
| 161 | gpa_t addr, int len, const void *val) |
| 162 | { |
| 163 | struct vgic_io_device *iodev = kvm_to_vgic_iodev(dev); |
| 164 | const struct vgic_register_region *region; |
| 165 | struct kvm_vcpu *r_vcpu; |
| 166 | unsigned long data = vgic_data_mmio_bus_to_host(val, len); |
| 167 | |
| 168 | region = vgic_find_mmio_region(iodev->regions, iodev->nr_regions, |
| 169 | addr - iodev->base_addr); |
| 170 | if (!region) |
| 171 | return 0; |
| 172 | |
| 173 | if (!check_region(region, addr, len)) |
| 174 | return 0; |
| 175 | |
| 176 | r_vcpu = iodev->redist_vcpu ? iodev->redist_vcpu : vcpu; |
| 177 | region->write(r_vcpu, addr, len, data); |
| 178 | return 0; |
| 179 | } |
| 180 | |
| 181 | struct kvm_io_device_ops kvm_io_gic_ops = { |
| 182 | .read = dispatch_mmio_read, |
| 183 | .write = dispatch_mmio_write, |
| 184 | }; |