Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015, 2016 ARM Ltd. |
| 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 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 15 | */ |
| 16 | #ifndef __KVM_ARM_VGIC_MMIO_H__ |
| 17 | #define __KVM_ARM_VGIC_MMIO_H__ |
| 18 | |
| 19 | struct vgic_register_region { |
| 20 | unsigned int reg_offset; |
| 21 | unsigned int len; |
| 22 | unsigned int bits_per_irq; |
| 23 | unsigned int access_flags; |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 24 | union { |
| 25 | unsigned long (*read)(struct kvm_vcpu *vcpu, gpa_t addr, |
| 26 | unsigned int len); |
| 27 | unsigned long (*its_read)(struct kvm *kvm, struct vgic_its *its, |
| 28 | gpa_t addr, unsigned int len); |
| 29 | }; |
| 30 | union { |
| 31 | void (*write)(struct kvm_vcpu *vcpu, gpa_t addr, |
| 32 | unsigned int len, unsigned long val); |
| 33 | void (*its_write)(struct kvm *kvm, struct vgic_its *its, |
| 34 | gpa_t addr, unsigned int len, |
| 35 | unsigned long val); |
| 36 | }; |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 37 | unsigned long (*uaccess_read)(struct kvm_vcpu *vcpu, gpa_t addr, |
| 38 | unsigned int len); |
| 39 | void (*uaccess_write)(struct kvm_vcpu *vcpu, gpa_t addr, |
| 40 | unsigned int len, unsigned long val); |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | extern struct kvm_io_device_ops kvm_io_gic_ops; |
| 44 | |
| 45 | #define VGIC_ACCESS_8bit 1 |
| 46 | #define VGIC_ACCESS_32bit 2 |
| 47 | #define VGIC_ACCESS_64bit 4 |
| 48 | |
| 49 | /* |
| 50 | * Generate a mask that covers the number of bytes required to address |
| 51 | * up to 1024 interrupts, each represented by <bits> bits. This assumes |
| 52 | * that <bits> is a power of two. |
| 53 | */ |
| 54 | #define VGIC_ADDR_IRQ_MASK(bits) (((bits) * 1024 / 8) - 1) |
| 55 | |
| 56 | /* |
Andre Przywara | 112b0b8 | 2016-11-01 18:00:08 +0000 | [diff] [blame] | 57 | * (addr & mask) gives us the _byte_ offset for the INT ID. |
| 58 | * We multiply this by 8 the get the _bit_ offset, then divide this by |
| 59 | * the number of bits to learn the actual INT ID. |
| 60 | * But instead of a division (which requires a "long long div" implementation), |
| 61 | * we shift by the binary logarithm of <bits>. |
| 62 | * This assumes that <bits> is a power of two. |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 63 | */ |
| 64 | #define VGIC_ADDR_TO_INTID(addr, bits) (((addr) & VGIC_ADDR_IRQ_MASK(bits)) * \ |
Andre Przywara | 112b0b8 | 2016-11-01 18:00:08 +0000 | [diff] [blame] | 65 | 8 >> ilog2(bits)) |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 66 | |
| 67 | /* |
| 68 | * Some VGIC registers store per-IRQ information, with a different number |
| 69 | * of bits per IRQ. For those registers this macro is used. |
| 70 | * The _WITH_LENGTH version instantiates registers with a fixed length |
| 71 | * and is mutually exclusive with the _PER_IRQ version. |
| 72 | */ |
| 73 | #define REGISTER_DESC_WITH_BITS_PER_IRQ(off, rd, wr, bpi, acc) \ |
| 74 | { \ |
| 75 | .reg_offset = off, \ |
| 76 | .bits_per_irq = bpi, \ |
| 77 | .len = bpi * 1024 / 8, \ |
| 78 | .access_flags = acc, \ |
| 79 | .read = rd, \ |
| 80 | .write = wr, \ |
| 81 | } |
| 82 | |
| 83 | #define REGISTER_DESC_WITH_LENGTH(off, rd, wr, length, acc) \ |
| 84 | { \ |
| 85 | .reg_offset = off, \ |
| 86 | .bits_per_irq = 0, \ |
| 87 | .len = length, \ |
| 88 | .access_flags = acc, \ |
| 89 | .read = rd, \ |
| 90 | .write = wr, \ |
| 91 | } |
| 92 | |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 93 | #define REGISTER_DESC_WITH_LENGTH_UACCESS(off, rd, wr, urd, uwr, length, acc) \ |
| 94 | { \ |
| 95 | .reg_offset = off, \ |
| 96 | .bits_per_irq = 0, \ |
| 97 | .len = length, \ |
| 98 | .access_flags = acc, \ |
| 99 | .read = rd, \ |
| 100 | .write = wr, \ |
| 101 | .uaccess_read = urd, \ |
| 102 | .uaccess_write = uwr, \ |
| 103 | } |
| 104 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 105 | int kvm_vgic_register_mmio_region(struct kvm *kvm, struct kvm_vcpu *vcpu, |
| 106 | struct vgic_register_region *reg_desc, |
| 107 | struct vgic_io_device *region, |
| 108 | int nr_irqs, bool offset_private); |
| 109 | |
| 110 | unsigned long vgic_data_mmio_bus_to_host(const void *val, unsigned int len); |
| 111 | |
| 112 | void vgic_data_host_to_mmio_bus(void *buf, unsigned int len, |
| 113 | unsigned long data); |
| 114 | |
Vladimir Murzin | d7d0a11 | 2016-09-12 15:49:20 +0100 | [diff] [blame] | 115 | unsigned long extract_bytes(u64 data, unsigned int offset, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 116 | unsigned int num); |
| 117 | |
| 118 | u64 update_64bit_reg(u64 reg, unsigned int offset, unsigned int len, |
| 119 | unsigned long val); |
| 120 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 121 | unsigned long vgic_mmio_read_raz(struct kvm_vcpu *vcpu, |
| 122 | gpa_t addr, unsigned int len); |
| 123 | |
| 124 | unsigned long vgic_mmio_read_rao(struct kvm_vcpu *vcpu, |
| 125 | gpa_t addr, unsigned int len); |
| 126 | |
| 127 | void vgic_mmio_write_wi(struct kvm_vcpu *vcpu, gpa_t addr, |
| 128 | unsigned int len, unsigned long val); |
| 129 | |
Andre Przywara | fd122e6 | 2015-12-01 14:33:05 +0000 | [diff] [blame] | 130 | unsigned long vgic_mmio_read_enable(struct kvm_vcpu *vcpu, |
| 131 | gpa_t addr, unsigned int len); |
| 132 | |
| 133 | void vgic_mmio_write_senable(struct kvm_vcpu *vcpu, |
| 134 | gpa_t addr, unsigned int len, |
| 135 | unsigned long val); |
| 136 | |
| 137 | void vgic_mmio_write_cenable(struct kvm_vcpu *vcpu, |
| 138 | gpa_t addr, unsigned int len, |
| 139 | unsigned long val); |
| 140 | |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 141 | unsigned long vgic_mmio_read_pending(struct kvm_vcpu *vcpu, |
| 142 | gpa_t addr, unsigned int len); |
| 143 | |
| 144 | void vgic_mmio_write_spending(struct kvm_vcpu *vcpu, |
| 145 | gpa_t addr, unsigned int len, |
| 146 | unsigned long val); |
| 147 | |
| 148 | void vgic_mmio_write_cpending(struct kvm_vcpu *vcpu, |
| 149 | gpa_t addr, unsigned int len, |
| 150 | unsigned long val); |
| 151 | |
Andre Przywara | 69b6fe0 | 2015-12-01 12:40:58 +0000 | [diff] [blame] | 152 | unsigned long vgic_mmio_read_active(struct kvm_vcpu *vcpu, |
| 153 | gpa_t addr, unsigned int len); |
| 154 | |
| 155 | void vgic_mmio_write_cactive(struct kvm_vcpu *vcpu, |
| 156 | gpa_t addr, unsigned int len, |
| 157 | unsigned long val); |
| 158 | |
| 159 | void vgic_mmio_write_sactive(struct kvm_vcpu *vcpu, |
| 160 | gpa_t addr, unsigned int len, |
| 161 | unsigned long val); |
Andre Przywara | 96b2980 | 2015-12-01 14:33:41 +0000 | [diff] [blame] | 162 | |
Andre Przywara | 055658b | 2015-12-01 14:34:02 +0000 | [diff] [blame] | 163 | unsigned long vgic_mmio_read_priority(struct kvm_vcpu *vcpu, |
| 164 | gpa_t addr, unsigned int len); |
| 165 | |
| 166 | void vgic_mmio_write_priority(struct kvm_vcpu *vcpu, |
| 167 | gpa_t addr, unsigned int len, |
| 168 | unsigned long val); |
| 169 | |
Andre Przywara | 79717e4 | 2015-12-01 12:41:31 +0000 | [diff] [blame] | 170 | unsigned long vgic_mmio_read_config(struct kvm_vcpu *vcpu, |
| 171 | gpa_t addr, unsigned int len); |
| 172 | |
| 173 | void vgic_mmio_write_config(struct kvm_vcpu *vcpu, |
| 174 | gpa_t addr, unsigned int len, |
| 175 | unsigned long val); |
| 176 | |
Vijaya Kumar K | 2df903a | 2017-01-26 19:50:46 +0530 | [diff] [blame] | 177 | int vgic_uaccess(struct kvm_vcpu *vcpu, struct vgic_io_device *dev, |
| 178 | bool is_write, int offset, u32 *val); |
| 179 | |
Vijaya Kumar K | e96a006 | 2017-01-26 19:50:52 +0530 | [diff] [blame] | 180 | u64 vgic_read_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid); |
| 181 | |
| 182 | void vgic_write_irq_line_level_info(struct kvm_vcpu *vcpu, u32 intid, |
| 183 | const u64 val); |
| 184 | |
Andre Przywara | fb848db | 2016-04-26 21:32:49 +0100 | [diff] [blame] | 185 | unsigned int vgic_v2_init_dist_iodev(struct vgic_io_device *dev); |
| 186 | |
Andre Przywara | ed9b8ce | 2015-12-01 14:34:34 +0000 | [diff] [blame] | 187 | unsigned int vgic_v3_init_dist_iodev(struct vgic_io_device *dev); |
| 188 | |
Andre Przywara | 0aa1de5 | 2016-07-15 12:43:29 +0100 | [diff] [blame] | 189 | u64 vgic_sanitise_outer_cacheability(u64 reg); |
| 190 | u64 vgic_sanitise_inner_cacheability(u64 reg); |
| 191 | u64 vgic_sanitise_shareability(u64 reg); |
| 192 | u64 vgic_sanitise_field(u64 reg, u64 field_mask, int field_shift, |
| 193 | u64 (*sanitise_fn)(u64)); |
Andre Przywara | 0aa1de5 | 2016-07-15 12:43:29 +0100 | [diff] [blame] | 194 | |
Eric Auger | 4b7171a | 2016-12-20 09:20:00 +0100 | [diff] [blame^] | 195 | /* Find the proper register handler entry given a certain address offset */ |
| 196 | const struct vgic_register_region * |
| 197 | vgic_find_mmio_region(const struct vgic_register_region *regions, |
| 198 | int nr_regions, unsigned int offset); |
| 199 | |
Marc Zyngier | 4493b1c | 2016-04-26 11:06:12 +0100 | [diff] [blame] | 200 | #endif |