Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1 | /* |
| 2 | * GICv3 ITS emulation |
| 3 | * |
| 4 | * Copyright (C) 2015,2016 ARM Ltd. |
| 5 | * Author: Andre Przywara <andre.przywara@arm.com> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License version 2 as |
| 9 | * published by the Free Software Foundation. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/cpu.h> |
| 21 | #include <linux/kvm.h> |
| 22 | #include <linux/kvm_host.h> |
| 23 | #include <linux/interrupt.h> |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 24 | #include <linux/list.h> |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 25 | #include <linux/uaccess.h> |
Eric Auger | 57a9a11 | 2017-01-09 16:27:07 +0100 | [diff] [blame] | 26 | #include <linux/list_sort.h> |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 27 | |
| 28 | #include <linux/irqchip/arm-gic-v3.h> |
| 29 | |
| 30 | #include <asm/kvm_emulate.h> |
| 31 | #include <asm/kvm_arm.h> |
| 32 | #include <asm/kvm_mmu.h> |
| 33 | |
| 34 | #include "vgic.h" |
| 35 | #include "vgic-mmio.h" |
| 36 | |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 37 | static int vgic_its_save_tables_v0(struct vgic_its *its); |
| 38 | static int vgic_its_restore_tables_v0(struct vgic_its *its); |
| 39 | static int vgic_its_commit_v0(struct vgic_its *its); |
Eric Auger | 06bd535 | 2017-05-04 11:36:32 +0200 | [diff] [blame] | 40 | static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq, |
| 41 | struct kvm_vcpu *filter_vcpu); |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 42 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 43 | /* |
| 44 | * Creates a new (reference to a) struct vgic_irq for a given LPI. |
| 45 | * If this LPI is already mapped on another ITS, we increase its refcount |
| 46 | * and return a pointer to the existing structure. |
| 47 | * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq. |
| 48 | * This function returns a pointer to the _unlocked_ structure. |
| 49 | */ |
Eric Auger | 06bd535 | 2017-05-04 11:36:32 +0200 | [diff] [blame] | 50 | static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid, |
| 51 | struct kvm_vcpu *vcpu) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 52 | { |
| 53 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 54 | struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq; |
Eric Auger | 06bd535 | 2017-05-04 11:36:32 +0200 | [diff] [blame] | 55 | int ret; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 56 | |
| 57 | /* In this case there is no put, since we keep the reference. */ |
| 58 | if (irq) |
| 59 | return irq; |
| 60 | |
| 61 | irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL); |
| 62 | if (!irq) |
Christoffer Dall | 99e5e88 | 2016-08-01 20:25:33 +0200 | [diff] [blame] | 63 | return ERR_PTR(-ENOMEM); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 64 | |
| 65 | INIT_LIST_HEAD(&irq->lpi_list); |
| 66 | INIT_LIST_HEAD(&irq->ap_list); |
| 67 | spin_lock_init(&irq->irq_lock); |
| 68 | |
| 69 | irq->config = VGIC_CONFIG_EDGE; |
| 70 | kref_init(&irq->refcount); |
| 71 | irq->intid = intid; |
Eric Auger | 06bd535 | 2017-05-04 11:36:32 +0200 | [diff] [blame] | 72 | irq->target_vcpu = vcpu; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 73 | |
| 74 | spin_lock(&dist->lpi_list_lock); |
| 75 | |
| 76 | /* |
| 77 | * There could be a race with another vgic_add_lpi(), so we need to |
| 78 | * check that we don't add a second list entry with the same LPI. |
| 79 | */ |
| 80 | list_for_each_entry(oldirq, &dist->lpi_list_head, lpi_list) { |
| 81 | if (oldirq->intid != intid) |
| 82 | continue; |
| 83 | |
| 84 | /* Someone was faster with adding this LPI, lets use that. */ |
| 85 | kfree(irq); |
| 86 | irq = oldirq; |
| 87 | |
| 88 | /* |
| 89 | * This increases the refcount, the caller is expected to |
| 90 | * call vgic_put_irq() on the returned pointer once it's |
| 91 | * finished with the IRQ. |
| 92 | */ |
Marc Zyngier | d97594e | 2016-07-17 11:27:23 +0100 | [diff] [blame] | 93 | vgic_get_irq_kref(irq); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 94 | |
| 95 | goto out_unlock; |
| 96 | } |
| 97 | |
| 98 | list_add_tail(&irq->lpi_list, &dist->lpi_list_head); |
| 99 | dist->lpi_list_count++; |
| 100 | |
| 101 | out_unlock: |
| 102 | spin_unlock(&dist->lpi_list_lock); |
| 103 | |
Eric Auger | 06bd535 | 2017-05-04 11:36:32 +0200 | [diff] [blame] | 104 | /* |
| 105 | * We "cache" the configuration table entries in our struct vgic_irq's. |
| 106 | * However we only have those structs for mapped IRQs, so we read in |
| 107 | * the respective config data from memory here upon mapping the LPI. |
| 108 | */ |
| 109 | ret = update_lpi_config(kvm, irq, NULL); |
| 110 | if (ret) |
| 111 | return ERR_PTR(ret); |
| 112 | |
| 113 | ret = vgic_v3_lpi_sync_pending_status(kvm, irq); |
| 114 | if (ret) |
| 115 | return ERR_PTR(ret); |
| 116 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 117 | return irq; |
| 118 | } |
| 119 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 120 | struct its_device { |
| 121 | struct list_head dev_list; |
| 122 | |
| 123 | /* the head for the list of ITTEs */ |
| 124 | struct list_head itt_head; |
Eric Auger | 0d44cdb | 2016-12-22 18:14:14 +0100 | [diff] [blame] | 125 | u32 num_eventid_bits; |
Eric Auger | 7333cef | 2017-02-02 13:45:45 +0100 | [diff] [blame] | 126 | gpa_t itt_addr; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 127 | u32 device_id; |
| 128 | }; |
| 129 | |
| 130 | #define COLLECTION_NOT_MAPPED ((u32)~0) |
| 131 | |
| 132 | struct its_collection { |
| 133 | struct list_head coll_list; |
| 134 | |
| 135 | u32 collection_id; |
| 136 | u32 target_addr; |
| 137 | }; |
| 138 | |
| 139 | #define its_is_collection_mapped(coll) ((coll) && \ |
| 140 | ((coll)->target_addr != COLLECTION_NOT_MAPPED)) |
| 141 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 142 | struct its_ite { |
| 143 | struct list_head ite_list; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 144 | |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 145 | struct vgic_irq *irq; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 146 | struct its_collection *collection; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 147 | u32 event_id; |
| 148 | }; |
| 149 | |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 150 | /** |
| 151 | * struct vgic_its_abi - ITS abi ops and settings |
| 152 | * @cte_esz: collection table entry size |
| 153 | * @dte_esz: device table entry size |
| 154 | * @ite_esz: interrupt translation table entry size |
| 155 | * @save tables: save the ITS tables into guest RAM |
| 156 | * @restore_tables: restore the ITS internal structs from tables |
| 157 | * stored in guest RAM |
| 158 | * @commit: initialize the registers which expose the ABI settings, |
| 159 | * especially the entry sizes |
| 160 | */ |
| 161 | struct vgic_its_abi { |
| 162 | int cte_esz; |
| 163 | int dte_esz; |
| 164 | int ite_esz; |
| 165 | int (*save_tables)(struct vgic_its *its); |
| 166 | int (*restore_tables)(struct vgic_its *its); |
| 167 | int (*commit)(struct vgic_its *its); |
| 168 | }; |
| 169 | |
| 170 | static const struct vgic_its_abi its_table_abi_versions[] = { |
| 171 | [0] = {.cte_esz = 8, .dte_esz = 8, .ite_esz = 8, |
| 172 | .save_tables = vgic_its_save_tables_v0, |
| 173 | .restore_tables = vgic_its_restore_tables_v0, |
| 174 | .commit = vgic_its_commit_v0, |
| 175 | }, |
| 176 | }; |
| 177 | |
| 178 | #define NR_ITS_ABIS ARRAY_SIZE(its_table_abi_versions) |
| 179 | |
| 180 | inline const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its) |
| 181 | { |
| 182 | return &its_table_abi_versions[its->abi_rev]; |
| 183 | } |
| 184 | |
| 185 | int vgic_its_set_abi(struct vgic_its *its, int rev) |
| 186 | { |
| 187 | const struct vgic_its_abi *abi; |
| 188 | |
| 189 | its->abi_rev = rev; |
| 190 | abi = vgic_its_get_abi(its); |
| 191 | return abi->commit(its); |
| 192 | } |
| 193 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 194 | /* |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 195 | * Find and returns a device in the device table for an ITS. |
| 196 | * Must be called with the its_lock mutex held. |
| 197 | */ |
| 198 | static struct its_device *find_its_device(struct vgic_its *its, u32 device_id) |
| 199 | { |
| 200 | struct its_device *device; |
| 201 | |
| 202 | list_for_each_entry(device, &its->device_list, dev_list) |
| 203 | if (device_id == device->device_id) |
| 204 | return device; |
| 205 | |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Find and returns an interrupt translation table entry (ITTE) for a given |
| 211 | * Device ID/Event ID pair on an ITS. |
| 212 | * Must be called with the its_lock mutex held. |
| 213 | */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 214 | static struct its_ite *find_ite(struct vgic_its *its, u32 device_id, |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 215 | u32 event_id) |
| 216 | { |
| 217 | struct its_device *device; |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 218 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 219 | |
| 220 | device = find_its_device(its, device_id); |
| 221 | if (device == NULL) |
| 222 | return NULL; |
| 223 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 224 | list_for_each_entry(ite, &device->itt_head, ite_list) |
| 225 | if (ite->event_id == event_id) |
| 226 | return ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 227 | |
| 228 | return NULL; |
| 229 | } |
| 230 | |
| 231 | /* To be used as an iterator this macro misses the enclosing parentheses */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 232 | #define for_each_lpi_its(dev, ite, its) \ |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 233 | list_for_each_entry(dev, &(its)->device_list, dev_list) \ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 234 | list_for_each_entry(ite, &(dev)->itt_head, ite_list) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 235 | |
| 236 | /* |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 237 | * We only implement 48 bits of PA at the moment, although the ITS |
| 238 | * supports more. Let's be restrictive here. |
| 239 | */ |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 240 | #define BASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16)) |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 241 | #define CBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12)) |
Andre Przywara | f9f77af | 2016-07-15 12:43:35 +0100 | [diff] [blame] | 242 | |
| 243 | #define GIC_LPI_OFFSET 8192 |
| 244 | |
Eric Auger | 0d44cdb | 2016-12-22 18:14:14 +0100 | [diff] [blame] | 245 | #define VITS_TYPER_IDBITS 16 |
Eric Auger | 07a3e9a | 2017-02-02 14:37:33 +0100 | [diff] [blame] | 246 | #define VITS_TYPER_DEVBITS 16 |
Eric Auger | 920a7a8 | 2017-02-08 05:20:04 +0100 | [diff] [blame] | 247 | #define VITS_DTE_MAX_DEVID_OFFSET (BIT(14) - 1) |
| 248 | #define VITS_ITE_MAX_EVENTID_OFFSET (BIT(16) - 1) |
Eric Auger | 0d44cdb | 2016-12-22 18:14:14 +0100 | [diff] [blame] | 249 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 250 | /* |
| 251 | * Finds and returns a collection in the ITS collection table. |
| 252 | * Must be called with the its_lock mutex held. |
| 253 | */ |
| 254 | static struct its_collection *find_collection(struct vgic_its *its, int coll_id) |
| 255 | { |
| 256 | struct its_collection *collection; |
| 257 | |
| 258 | list_for_each_entry(collection, &its->collection_list, coll_list) { |
| 259 | if (coll_id == collection->collection_id) |
| 260 | return collection; |
| 261 | } |
| 262 | |
| 263 | return NULL; |
| 264 | } |
| 265 | |
Andre Przywara | f9f77af | 2016-07-15 12:43:35 +0100 | [diff] [blame] | 266 | #define LPI_PROP_ENABLE_BIT(p) ((p) & LPI_PROP_ENABLED) |
| 267 | #define LPI_PROP_PRIORITY(p) ((p) & 0xfc) |
| 268 | |
| 269 | /* |
| 270 | * Reads the configuration data for a given LPI from guest memory and |
| 271 | * updates the fields in struct vgic_irq. |
| 272 | * If filter_vcpu is not NULL, applies only if the IRQ is targeting this |
| 273 | * VCPU. Unconditionally applies if filter_vcpu is NULL. |
| 274 | */ |
| 275 | static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq, |
| 276 | struct kvm_vcpu *filter_vcpu) |
| 277 | { |
Eric Auger | 44de9d6 | 2017-05-04 11:19:52 +0200 | [diff] [blame] | 278 | u64 propbase = GICR_PROPBASER_ADDRESS(kvm->arch.vgic.propbaser); |
Andre Przywara | f9f77af | 2016-07-15 12:43:35 +0100 | [diff] [blame] | 279 | u8 prop; |
| 280 | int ret; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 281 | unsigned long flags; |
Andre Przywara | f9f77af | 2016-07-15 12:43:35 +0100 | [diff] [blame] | 282 | |
| 283 | ret = kvm_read_guest(kvm, propbase + irq->intid - GIC_LPI_OFFSET, |
| 284 | &prop, 1); |
| 285 | |
| 286 | if (ret) |
| 287 | return ret; |
| 288 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 289 | spin_lock_irqsave(&irq->irq_lock, flags); |
Andre Przywara | f9f77af | 2016-07-15 12:43:35 +0100 | [diff] [blame] | 290 | |
| 291 | if (!filter_vcpu || filter_vcpu == irq->target_vcpu) { |
| 292 | irq->priority = LPI_PROP_PRIORITY(prop); |
| 293 | irq->enabled = LPI_PROP_ENABLE_BIT(prop); |
| 294 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 295 | vgic_queue_irq_unlock(kvm, irq, flags); |
Andre Przywara | f9f77af | 2016-07-15 12:43:35 +0100 | [diff] [blame] | 296 | } else { |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 297 | spin_unlock_irqrestore(&irq->irq_lock, flags); |
Andre Przywara | f9f77af | 2016-07-15 12:43:35 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 302 | |
| 303 | /* |
Eric Auger | ccb1d79 | 2017-04-12 14:13:27 +0200 | [diff] [blame] | 304 | * Create a snapshot of the current LPIs targeting @vcpu, so that we can |
| 305 | * enumerate those LPIs without holding any lock. |
| 306 | * Returns their number and puts the kmalloc'ed array into intid_ptr. |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 307 | */ |
Eric Auger | ccb1d79 | 2017-04-12 14:13:27 +0200 | [diff] [blame] | 308 | static int vgic_copy_lpi_list(struct kvm_vcpu *vcpu, u32 **intid_ptr) |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 309 | { |
Eric Auger | ccb1d79 | 2017-04-12 14:13:27 +0200 | [diff] [blame] | 310 | struct vgic_dist *dist = &vcpu->kvm->arch.vgic; |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 311 | struct vgic_irq *irq; |
| 312 | u32 *intids; |
| 313 | int irq_count = dist->lpi_list_count, i = 0; |
| 314 | |
| 315 | /* |
| 316 | * We use the current value of the list length, which may change |
| 317 | * after the kmalloc. We don't care, because the guest shouldn't |
| 318 | * change anything while the command handling is still running, |
| 319 | * and in the worst case we would miss a new IRQ, which one wouldn't |
| 320 | * expect to be covered by this command anyway. |
| 321 | */ |
| 322 | intids = kmalloc_array(irq_count, sizeof(intids[0]), GFP_KERNEL); |
| 323 | if (!intids) |
| 324 | return -ENOMEM; |
| 325 | |
| 326 | spin_lock(&dist->lpi_list_lock); |
| 327 | list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { |
| 328 | /* We don't need to "get" the IRQ, as we hold the list lock. */ |
Eric Auger | ccb1d79 | 2017-04-12 14:13:27 +0200 | [diff] [blame] | 329 | if (irq->target_vcpu != vcpu) |
| 330 | continue; |
| 331 | intids[i++] = irq->intid; |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 332 | } |
| 333 | spin_unlock(&dist->lpi_list_lock); |
| 334 | |
| 335 | *intid_ptr = intids; |
Eric Auger | ccb1d79 | 2017-04-12 14:13:27 +0200 | [diff] [blame] | 336 | return i; |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | /* |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 340 | * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI |
| 341 | * is targeting) to the VGIC's view, which deals with target VCPUs. |
| 342 | * Needs to be called whenever either the collection for a LPIs has |
| 343 | * changed or the collection itself got retargeted. |
| 344 | */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 345 | static void update_affinity_ite(struct kvm *kvm, struct its_ite *ite) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 346 | { |
| 347 | struct kvm_vcpu *vcpu; |
| 348 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 349 | if (!its_is_collection_mapped(ite->collection)) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 350 | return; |
| 351 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 352 | vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 353 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 354 | spin_lock(&ite->irq->irq_lock); |
| 355 | ite->irq->target_vcpu = vcpu; |
| 356 | spin_unlock(&ite->irq->irq_lock); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Updates the target VCPU for every LPI targeting this collection. |
| 361 | * Must be called with the its_lock mutex held. |
| 362 | */ |
| 363 | static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its, |
| 364 | struct its_collection *coll) |
| 365 | { |
| 366 | struct its_device *device; |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 367 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 368 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 369 | for_each_lpi_its(device, ite, its) { |
| 370 | if (!ite->collection || coll != ite->collection) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 371 | continue; |
| 372 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 373 | update_affinity_ite(kvm, ite); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 374 | } |
| 375 | } |
| 376 | |
| 377 | static u32 max_lpis_propbaser(u64 propbaser) |
| 378 | { |
| 379 | int nr_idbits = (propbaser & 0x1f) + 1; |
| 380 | |
| 381 | return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS); |
| 382 | } |
| 383 | |
| 384 | /* |
Eric Auger | ccb1d79 | 2017-04-12 14:13:27 +0200 | [diff] [blame] | 385 | * Sync the pending table pending bit of LPIs targeting @vcpu |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 386 | * with our own data structures. This relies on the LPI being |
| 387 | * mapped before. |
| 388 | */ |
| 389 | static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu) |
| 390 | { |
Eric Auger | 44de9d6 | 2017-05-04 11:19:52 +0200 | [diff] [blame] | 391 | gpa_t pendbase = GICR_PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser); |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 392 | struct vgic_irq *irq; |
| 393 | int last_byte_offset = -1; |
| 394 | int ret = 0; |
| 395 | u32 *intids; |
| 396 | int nr_irqs, i; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 397 | unsigned long flags; |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 398 | |
Eric Auger | ccb1d79 | 2017-04-12 14:13:27 +0200 | [diff] [blame] | 399 | nr_irqs = vgic_copy_lpi_list(vcpu, &intids); |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 400 | if (nr_irqs < 0) |
| 401 | return nr_irqs; |
| 402 | |
| 403 | for (i = 0; i < nr_irqs; i++) { |
| 404 | int byte_offset, bit_nr; |
| 405 | u8 pendmask; |
| 406 | |
| 407 | byte_offset = intids[i] / BITS_PER_BYTE; |
| 408 | bit_nr = intids[i] % BITS_PER_BYTE; |
| 409 | |
| 410 | /* |
| 411 | * For contiguously allocated LPIs chances are we just read |
| 412 | * this very same byte in the last iteration. Reuse that. |
| 413 | */ |
| 414 | if (byte_offset != last_byte_offset) { |
| 415 | ret = kvm_read_guest(vcpu->kvm, pendbase + byte_offset, |
| 416 | &pendmask, 1); |
| 417 | if (ret) { |
| 418 | kfree(intids); |
| 419 | return ret; |
| 420 | } |
| 421 | last_byte_offset = byte_offset; |
| 422 | } |
| 423 | |
| 424 | irq = vgic_get_irq(vcpu->kvm, NULL, intids[i]); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 425 | spin_lock_irqsave(&irq->irq_lock, flags); |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 426 | irq->pending_latch = pendmask & (1U << bit_nr); |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 427 | vgic_queue_irq_unlock(vcpu->kvm, irq, flags); |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 428 | vgic_put_irq(vcpu->kvm, irq); |
| 429 | } |
| 430 | |
| 431 | kfree(intids); |
| 432 | |
| 433 | return ret; |
| 434 | } |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 435 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 436 | static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm, |
| 437 | struct vgic_its *its, |
| 438 | gpa_t addr, unsigned int len) |
| 439 | { |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 440 | const struct vgic_its_abi *abi = vgic_its_get_abi(its); |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 441 | u64 reg = GITS_TYPER_PLPIS; |
| 442 | |
| 443 | /* |
| 444 | * We use linear CPU numbers for redistributor addressing, |
| 445 | * so GITS_TYPER.PTA is 0. |
| 446 | * Also we force all PROPBASER registers to be the same, so |
| 447 | * CommonLPIAff is 0 as well. |
| 448 | * To avoid memory waste in the guest, we keep the number of IDBits and |
| 449 | * DevBits low - as least for the time being. |
| 450 | */ |
Eric Auger | 07a3e9a | 2017-02-02 14:37:33 +0100 | [diff] [blame] | 451 | reg |= GIC_ENCODE_SZ(VITS_TYPER_DEVBITS, 5) << GITS_TYPER_DEVBITS_SHIFT; |
Eric Auger | 0d44cdb | 2016-12-22 18:14:14 +0100 | [diff] [blame] | 452 | reg |= GIC_ENCODE_SZ(VITS_TYPER_IDBITS, 5) << GITS_TYPER_IDBITS_SHIFT; |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 453 | reg |= GIC_ENCODE_SZ(abi->ite_esz, 4) << GITS_TYPER_ITT_ENTRY_SIZE_SHIFT; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 454 | |
| 455 | return extract_bytes(reg, addr & 7, len); |
| 456 | } |
| 457 | |
| 458 | static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm, |
| 459 | struct vgic_its *its, |
| 460 | gpa_t addr, unsigned int len) |
| 461 | { |
Eric Auger | ab01c6b | 2017-03-23 15:14:00 +0100 | [diff] [blame] | 462 | u32 val; |
| 463 | |
| 464 | val = (its->abi_rev << GITS_IIDR_REV_SHIFT) & GITS_IIDR_REV_MASK; |
| 465 | val |= (PRODUCT_ID_KVM << GITS_IIDR_PRODUCTID_SHIFT) | IMPLEMENTER_ARM; |
| 466 | return val; |
| 467 | } |
| 468 | |
| 469 | static int vgic_mmio_uaccess_write_its_iidr(struct kvm *kvm, |
| 470 | struct vgic_its *its, |
| 471 | gpa_t addr, unsigned int len, |
| 472 | unsigned long val) |
| 473 | { |
| 474 | u32 rev = GITS_IIDR_REV(val); |
| 475 | |
| 476 | if (rev >= NR_ITS_ABIS) |
| 477 | return -EINVAL; |
| 478 | return vgic_its_set_abi(its, rev); |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm, |
| 482 | struct vgic_its *its, |
| 483 | gpa_t addr, unsigned int len) |
| 484 | { |
| 485 | switch (addr & 0xffff) { |
| 486 | case GITS_PIDR0: |
| 487 | return 0x92; /* part number, bits[7:0] */ |
| 488 | case GITS_PIDR1: |
| 489 | return 0xb4; /* part number, bits[11:8] */ |
| 490 | case GITS_PIDR2: |
| 491 | return GIC_PIDR2_ARCH_GICv3 | 0x0b; |
| 492 | case GITS_PIDR4: |
| 493 | return 0x40; /* This is a 64K software visible page */ |
| 494 | /* The following are the ID registers for (any) GIC. */ |
| 495 | case GITS_CIDR0: |
| 496 | return 0x0d; |
| 497 | case GITS_CIDR1: |
| 498 | return 0xf0; |
| 499 | case GITS_CIDR2: |
| 500 | return 0x05; |
| 501 | case GITS_CIDR3: |
| 502 | return 0xb1; |
| 503 | } |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 508 | /* |
| 509 | * Find the target VCPU and the LPI number for a given devid/eventid pair |
| 510 | * and make this IRQ pending, possibly injecting it. |
| 511 | * Must be called with the its_lock mutex held. |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 512 | * Returns 0 on success, a positive error value for any ITS mapping |
| 513 | * related errors and negative error values for generic errors. |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 514 | */ |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 515 | static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its, |
| 516 | u32 devid, u32 eventid) |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 517 | { |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 518 | struct kvm_vcpu *vcpu; |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 519 | struct its_ite *ite; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 520 | unsigned long flags; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 521 | |
| 522 | if (!its->enabled) |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 523 | return -EBUSY; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 524 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 525 | ite = find_ite(its, devid, eventid); |
| 526 | if (!ite || !its_is_collection_mapped(ite->collection)) |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 527 | return E_ITS_INT_UNMAPPED_INTERRUPT; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 528 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 529 | vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr); |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 530 | if (!vcpu) |
| 531 | return E_ITS_INT_UNMAPPED_INTERRUPT; |
| 532 | |
| 533 | if (!vcpu->arch.vgic_cpu.lpis_enabled) |
| 534 | return -EBUSY; |
| 535 | |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 536 | spin_lock_irqsave(&ite->irq->irq_lock, flags); |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 537 | ite->irq->pending_latch = true; |
Christoffer Dall | 006df0f | 2016-10-16 22:19:11 +0200 | [diff] [blame] | 538 | vgic_queue_irq_unlock(kvm, ite->irq, flags); |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 539 | |
| 540 | return 0; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 541 | } |
| 542 | |
Andre Przywara | 505a19e | 2016-08-09 10:54:29 +0100 | [diff] [blame] | 543 | static struct vgic_io_device *vgic_get_its_iodev(struct kvm_io_device *dev) |
| 544 | { |
| 545 | struct vgic_io_device *iodev; |
| 546 | |
| 547 | if (dev->ops != &kvm_io_gic_ops) |
| 548 | return NULL; |
| 549 | |
| 550 | iodev = container_of(dev, struct vgic_io_device, dev); |
| 551 | |
| 552 | if (iodev->iodev_type != IODEV_ITS) |
| 553 | return NULL; |
| 554 | |
| 555 | return iodev; |
| 556 | } |
| 557 | |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 558 | /* |
| 559 | * Queries the KVM IO bus framework to get the ITS pointer from the given |
| 560 | * doorbell address. |
| 561 | * We then call vgic_its_trigger_msi() with the decoded data. |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 562 | * According to the KVM_SIGNAL_MSI API description returns 1 on success. |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 563 | */ |
| 564 | int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi) |
| 565 | { |
| 566 | u64 address; |
| 567 | struct kvm_io_device *kvm_io_dev; |
| 568 | struct vgic_io_device *iodev; |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 569 | int ret; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 570 | |
| 571 | if (!vgic_has_its(kvm)) |
| 572 | return -ENODEV; |
| 573 | |
| 574 | if (!(msi->flags & KVM_MSI_VALID_DEVID)) |
| 575 | return -EINVAL; |
| 576 | |
| 577 | address = (u64)msi->address_hi << 32 | msi->address_lo; |
| 578 | |
| 579 | kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address); |
| 580 | if (!kvm_io_dev) |
Andre Przywara | 505a19e | 2016-08-09 10:54:29 +0100 | [diff] [blame] | 581 | return -EINVAL; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 582 | |
Andre Przywara | 505a19e | 2016-08-09 10:54:29 +0100 | [diff] [blame] | 583 | iodev = vgic_get_its_iodev(kvm_io_dev); |
| 584 | if (!iodev) |
| 585 | return -EINVAL; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 586 | |
| 587 | mutex_lock(&iodev->its->its_lock); |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 588 | ret = vgic_its_trigger_msi(kvm, iodev->its, msi->devid, msi->data); |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 589 | mutex_unlock(&iodev->its->its_lock); |
| 590 | |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 591 | if (ret < 0) |
| 592 | return ret; |
| 593 | |
| 594 | /* |
| 595 | * KVM_SIGNAL_MSI demands a return value > 0 for success and 0 |
| 596 | * if the guest has blocked the MSI. So we map any LPI mapping |
| 597 | * related error to that. |
| 598 | */ |
| 599 | if (ret) |
| 600 | return 0; |
| 601 | else |
| 602 | return 1; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 603 | } |
| 604 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 605 | /* Requires the its_lock to be held. */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 606 | static void its_free_ite(struct kvm *kvm, struct its_ite *ite) |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 607 | { |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 608 | list_del(&ite->ite_list); |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 609 | |
| 610 | /* This put matches the get in vgic_add_lpi. */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 611 | if (ite->irq) |
| 612 | vgic_put_irq(kvm, ite->irq); |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 613 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 614 | kfree(ite); |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 615 | } |
| 616 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 617 | static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size) |
| 618 | { |
| 619 | return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1); |
| 620 | } |
| 621 | |
| 622 | #define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8) |
| 623 | #define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32) |
Eric Auger | 0d44cdb | 2016-12-22 18:14:14 +0100 | [diff] [blame] | 624 | #define its_cmd_get_size(cmd) (its_cmd_mask_field(cmd, 1, 0, 5) + 1) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 625 | #define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32) |
| 626 | #define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32) |
| 627 | #define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16) |
Eric Auger | 7333cef | 2017-02-02 13:45:45 +0100 | [diff] [blame] | 628 | #define its_cmd_get_ittaddr(cmd) (its_cmd_mask_field(cmd, 2, 8, 44) << 8) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 629 | #define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32) |
| 630 | #define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1) |
| 631 | |
| 632 | /* |
| 633 | * The DISCARD command frees an Interrupt Translation Table Entry (ITTE). |
| 634 | * Must be called with the its_lock mutex held. |
| 635 | */ |
| 636 | static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its, |
| 637 | u64 *its_cmd) |
| 638 | { |
| 639 | u32 device_id = its_cmd_get_deviceid(its_cmd); |
| 640 | u32 event_id = its_cmd_get_id(its_cmd); |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 641 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 642 | |
| 643 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 644 | ite = find_ite(its, device_id, event_id); |
| 645 | if (ite && ite->collection) { |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 646 | /* |
| 647 | * Though the spec talks about removing the pending state, we |
| 648 | * don't bother here since we clear the ITTE anyway and the |
| 649 | * pending state is a property of the ITTE struct. |
| 650 | */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 651 | its_free_ite(kvm, ite); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | return E_ITS_DISCARD_UNMAPPED_INTERRUPT; |
| 656 | } |
| 657 | |
| 658 | /* |
| 659 | * The MOVI command moves an ITTE to a different collection. |
| 660 | * Must be called with the its_lock mutex held. |
| 661 | */ |
| 662 | static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its, |
| 663 | u64 *its_cmd) |
| 664 | { |
| 665 | u32 device_id = its_cmd_get_deviceid(its_cmd); |
| 666 | u32 event_id = its_cmd_get_id(its_cmd); |
| 667 | u32 coll_id = its_cmd_get_collection(its_cmd); |
| 668 | struct kvm_vcpu *vcpu; |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 669 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 670 | struct its_collection *collection; |
| 671 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 672 | ite = find_ite(its, device_id, event_id); |
| 673 | if (!ite) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 674 | return E_ITS_MOVI_UNMAPPED_INTERRUPT; |
| 675 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 676 | if (!its_is_collection_mapped(ite->collection)) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 677 | return E_ITS_MOVI_UNMAPPED_COLLECTION; |
| 678 | |
| 679 | collection = find_collection(its, coll_id); |
| 680 | if (!its_is_collection_mapped(collection)) |
| 681 | return E_ITS_MOVI_UNMAPPED_COLLECTION; |
| 682 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 683 | ite->collection = collection; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 684 | vcpu = kvm_get_vcpu(kvm, collection->target_addr); |
| 685 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 686 | spin_lock(&ite->irq->irq_lock); |
| 687 | ite->irq->target_vcpu = vcpu; |
| 688 | spin_unlock(&ite->irq->irq_lock); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 689 | |
| 690 | return 0; |
| 691 | } |
| 692 | |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 693 | /* |
| 694 | * Check whether an ID can be stored into the corresponding guest table. |
| 695 | * For a direct table this is pretty easy, but gets a bit nasty for |
| 696 | * indirect tables. We check whether the resulting guest physical address |
Eric Auger | 07a3e9a | 2017-02-02 14:37:33 +0100 | [diff] [blame] | 697 | * is actually valid (covered by a memslot and guest accessible). |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 698 | * For this we have to read the respective first level entry. |
| 699 | */ |
Eric Auger | dceff70 | 2017-01-31 14:36:14 +0100 | [diff] [blame] | 700 | static bool vgic_its_check_id(struct vgic_its *its, u64 baser, u32 id, |
| 701 | gpa_t *eaddr) |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 702 | { |
| 703 | int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K; |
Eric Auger | 07a3e9a | 2017-02-02 14:37:33 +0100 | [diff] [blame] | 704 | u64 indirect_ptr, type = GITS_BASER_TYPE(baser); |
Vladimir Murzin | e29bd6f | 2016-11-02 11:55:33 +0000 | [diff] [blame] | 705 | int esz = GITS_BASER_ENTRY_SIZE(baser); |
Eric Auger | 07a3e9a | 2017-02-02 14:37:33 +0100 | [diff] [blame] | 706 | int index; |
| 707 | gfn_t gfn; |
| 708 | |
| 709 | switch (type) { |
| 710 | case GITS_BASER_TYPE_DEVICE: |
| 711 | if (id >= BIT_ULL(VITS_TYPER_DEVBITS)) |
| 712 | return false; |
| 713 | break; |
| 714 | case GITS_BASER_TYPE_COLLECTION: |
| 715 | /* as GITS_TYPER.CIL == 0, ITS supports 16-bit collection ID */ |
| 716 | if (id >= BIT_ULL(16)) |
| 717 | return false; |
| 718 | break; |
| 719 | default: |
| 720 | return false; |
| 721 | } |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 722 | |
| 723 | if (!(baser & GITS_BASER_INDIRECT)) { |
| 724 | phys_addr_t addr; |
| 725 | |
Vladimir Murzin | e29bd6f | 2016-11-02 11:55:33 +0000 | [diff] [blame] | 726 | if (id >= (l1_tbl_size / esz)) |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 727 | return false; |
| 728 | |
Vladimir Murzin | e29bd6f | 2016-11-02 11:55:33 +0000 | [diff] [blame] | 729 | addr = BASER_ADDRESS(baser) + id * esz; |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 730 | gfn = addr >> PAGE_SHIFT; |
| 731 | |
Eric Auger | dceff70 | 2017-01-31 14:36:14 +0100 | [diff] [blame] | 732 | if (eaddr) |
| 733 | *eaddr = addr; |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 734 | return kvm_is_visible_gfn(its->dev->kvm, gfn); |
| 735 | } |
| 736 | |
| 737 | /* calculate and check the index into the 1st level */ |
Vladimir Murzin | e29bd6f | 2016-11-02 11:55:33 +0000 | [diff] [blame] | 738 | index = id / (SZ_64K / esz); |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 739 | if (index >= (l1_tbl_size / sizeof(u64))) |
| 740 | return false; |
| 741 | |
| 742 | /* Each 1st level entry is represented by a 64-bit value. */ |
| 743 | if (kvm_read_guest(its->dev->kvm, |
| 744 | BASER_ADDRESS(baser) + index * sizeof(indirect_ptr), |
| 745 | &indirect_ptr, sizeof(indirect_ptr))) |
| 746 | return false; |
| 747 | |
| 748 | indirect_ptr = le64_to_cpu(indirect_ptr); |
| 749 | |
| 750 | /* check the valid bit of the first level entry */ |
| 751 | if (!(indirect_ptr & BIT_ULL(63))) |
| 752 | return false; |
| 753 | |
| 754 | /* |
| 755 | * Mask the guest physical address and calculate the frame number. |
| 756 | * Any address beyond our supported 48 bits of PA will be caught |
| 757 | * by the actual check in the final step. |
| 758 | */ |
| 759 | indirect_ptr &= GENMASK_ULL(51, 16); |
| 760 | |
| 761 | /* Find the address of the actual entry */ |
Vladimir Murzin | e29bd6f | 2016-11-02 11:55:33 +0000 | [diff] [blame] | 762 | index = id % (SZ_64K / esz); |
| 763 | indirect_ptr += index * esz; |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 764 | gfn = indirect_ptr >> PAGE_SHIFT; |
| 765 | |
Eric Auger | dceff70 | 2017-01-31 14:36:14 +0100 | [diff] [blame] | 766 | if (eaddr) |
| 767 | *eaddr = indirect_ptr; |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 768 | return kvm_is_visible_gfn(its->dev->kvm, gfn); |
| 769 | } |
| 770 | |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 771 | static int vgic_its_alloc_collection(struct vgic_its *its, |
| 772 | struct its_collection **colp, |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 773 | u32 coll_id) |
| 774 | { |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 775 | struct its_collection *collection; |
| 776 | |
Eric Auger | dceff70 | 2017-01-31 14:36:14 +0100 | [diff] [blame] | 777 | if (!vgic_its_check_id(its, its->baser_coll_table, coll_id, NULL)) |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 778 | return E_ITS_MAPC_COLLECTION_OOR; |
| 779 | |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 780 | collection = kzalloc(sizeof(*collection), GFP_KERNEL); |
| 781 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 782 | collection->collection_id = coll_id; |
| 783 | collection->target_addr = COLLECTION_NOT_MAPPED; |
| 784 | |
| 785 | list_add_tail(&collection->coll_list, &its->collection_list); |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 786 | *colp = collection; |
| 787 | |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id) |
| 792 | { |
| 793 | struct its_collection *collection; |
| 794 | struct its_device *device; |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 795 | struct its_ite *ite; |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 796 | |
| 797 | /* |
| 798 | * Clearing the mapping for that collection ID removes the |
| 799 | * entry from the list. If there wasn't any before, we can |
| 800 | * go home early. |
| 801 | */ |
| 802 | collection = find_collection(its, coll_id); |
| 803 | if (!collection) |
| 804 | return; |
| 805 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 806 | for_each_lpi_its(device, ite, its) |
| 807 | if (ite->collection && |
| 808 | ite->collection->collection_id == coll_id) |
| 809 | ite->collection = NULL; |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 810 | |
| 811 | list_del(&collection->coll_list); |
| 812 | kfree(collection); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 813 | } |
| 814 | |
Eric Auger | 528297f | 2016-12-25 18:57:54 +0100 | [diff] [blame] | 815 | /* Must be called with its_lock mutex held */ |
| 816 | static struct its_ite *vgic_its_alloc_ite(struct its_device *device, |
| 817 | struct its_collection *collection, |
Marc Zyngier | 7c7d2fa | 2017-09-01 17:51:56 +0100 | [diff] [blame] | 818 | u32 event_id) |
Eric Auger | 528297f | 2016-12-25 18:57:54 +0100 | [diff] [blame] | 819 | { |
| 820 | struct its_ite *ite; |
| 821 | |
| 822 | ite = kzalloc(sizeof(*ite), GFP_KERNEL); |
| 823 | if (!ite) |
| 824 | return ERR_PTR(-ENOMEM); |
| 825 | |
| 826 | ite->event_id = event_id; |
| 827 | ite->collection = collection; |
Eric Auger | 528297f | 2016-12-25 18:57:54 +0100 | [diff] [blame] | 828 | |
| 829 | list_add_tail(&ite->ite_list, &device->itt_head); |
| 830 | return ite; |
| 831 | } |
| 832 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 833 | /* |
| 834 | * The MAPTI and MAPI commands map LPIs to ITTEs. |
| 835 | * Must be called with its_lock mutex held. |
| 836 | */ |
| 837 | static int vgic_its_cmd_handle_mapi(struct kvm *kvm, struct vgic_its *its, |
Marc Zyngier | a3e7aa2 | 2016-07-17 22:38:32 +0100 | [diff] [blame] | 838 | u64 *its_cmd) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 839 | { |
| 840 | u32 device_id = its_cmd_get_deviceid(its_cmd); |
| 841 | u32 event_id = its_cmd_get_id(its_cmd); |
| 842 | u32 coll_id = its_cmd_get_collection(its_cmd); |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 843 | struct its_ite *ite; |
Eric Auger | 06bd535 | 2017-05-04 11:36:32 +0200 | [diff] [blame] | 844 | struct kvm_vcpu *vcpu = NULL; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 845 | struct its_device *device; |
| 846 | struct its_collection *collection, *new_coll = NULL; |
Christoffer Dall | 99e5e88 | 2016-08-01 20:25:33 +0200 | [diff] [blame] | 847 | struct vgic_irq *irq; |
Eric Auger | 528297f | 2016-12-25 18:57:54 +0100 | [diff] [blame] | 848 | int lpi_nr; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 849 | |
| 850 | device = find_its_device(its, device_id); |
| 851 | if (!device) |
| 852 | return E_ITS_MAPTI_UNMAPPED_DEVICE; |
| 853 | |
Eric Auger | 0d44cdb | 2016-12-22 18:14:14 +0100 | [diff] [blame] | 854 | if (event_id >= BIT_ULL(device->num_eventid_bits)) |
| 855 | return E_ITS_MAPTI_ID_OOR; |
| 856 | |
Marc Zyngier | a3e7aa2 | 2016-07-17 22:38:32 +0100 | [diff] [blame] | 857 | if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 858 | lpi_nr = its_cmd_get_physical_id(its_cmd); |
| 859 | else |
| 860 | lpi_nr = event_id; |
| 861 | if (lpi_nr < GIC_LPI_OFFSET || |
Marc Zyngier | 3a88bded | 2016-07-18 16:27:14 +0100 | [diff] [blame] | 862 | lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser)) |
| 863 | return E_ITS_MAPTI_PHYSICALID_OOR; |
| 864 | |
Andre Przywara | 286054a | 2016-08-16 17:51:06 +0100 | [diff] [blame] | 865 | /* If there is an existing mapping, behavior is UNPREDICTABLE. */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 866 | if (find_ite(its, device_id, event_id)) |
Andre Przywara | 286054a | 2016-08-16 17:51:06 +0100 | [diff] [blame] | 867 | return 0; |
| 868 | |
Marc Zyngier | 3a88bded | 2016-07-18 16:27:14 +0100 | [diff] [blame] | 869 | collection = find_collection(its, coll_id); |
| 870 | if (!collection) { |
| 871 | int ret = vgic_its_alloc_collection(its, &collection, coll_id); |
| 872 | if (ret) |
| 873 | return ret; |
| 874 | new_coll = collection; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Marc Zyngier | 7c7d2fa | 2017-09-01 17:51:56 +0100 | [diff] [blame] | 877 | ite = vgic_its_alloc_ite(device, collection, event_id); |
Eric Auger | 528297f | 2016-12-25 18:57:54 +0100 | [diff] [blame] | 878 | if (IS_ERR(ite)) { |
Andre Przywara | 286054a | 2016-08-16 17:51:06 +0100 | [diff] [blame] | 879 | if (new_coll) |
| 880 | vgic_its_free_collection(its, coll_id); |
Eric Auger | 528297f | 2016-12-25 18:57:54 +0100 | [diff] [blame] | 881 | return PTR_ERR(ite); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 882 | } |
| 883 | |
Eric Auger | 06bd535 | 2017-05-04 11:36:32 +0200 | [diff] [blame] | 884 | if (its_is_collection_mapped(collection)) |
| 885 | vcpu = kvm_get_vcpu(kvm, collection->target_addr); |
| 886 | |
| 887 | irq = vgic_add_lpi(kvm, lpi_nr, vcpu); |
Christoffer Dall | 99e5e88 | 2016-08-01 20:25:33 +0200 | [diff] [blame] | 888 | if (IS_ERR(irq)) { |
| 889 | if (new_coll) |
| 890 | vgic_its_free_collection(its, coll_id); |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 891 | its_free_ite(kvm, ite); |
Christoffer Dall | 99e5e88 | 2016-08-01 20:25:33 +0200 | [diff] [blame] | 892 | return PTR_ERR(irq); |
| 893 | } |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 894 | ite->irq = irq; |
Christoffer Dall | 99e5e88 | 2016-08-01 20:25:33 +0200 | [diff] [blame] | 895 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | /* Requires the its_lock to be held. */ |
Eric Auger | 0a0d389 | 2017-10-26 17:23:07 +0200 | [diff] [blame] | 900 | static void vgic_its_free_device(struct kvm *kvm, struct its_device *device) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 901 | { |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 902 | struct its_ite *ite, *temp; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 903 | |
| 904 | /* |
| 905 | * The spec says that unmapping a device with still valid |
| 906 | * ITTEs associated is UNPREDICTABLE. We remove all ITTEs, |
| 907 | * since we cannot leave the memory unreferenced. |
| 908 | */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 909 | list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list) |
| 910 | its_free_ite(kvm, ite); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 911 | |
| 912 | list_del(&device->dev_list); |
| 913 | kfree(device); |
| 914 | } |
| 915 | |
wanghaibin | 2f609a0 | 2017-10-26 17:23:08 +0200 | [diff] [blame^] | 916 | /* its lock must be held */ |
| 917 | static void vgic_its_free_device_list(struct kvm *kvm, struct vgic_its *its) |
| 918 | { |
| 919 | struct its_device *cur, *temp; |
| 920 | |
| 921 | list_for_each_entry_safe(cur, temp, &its->device_list, dev_list) |
| 922 | vgic_its_free_device(kvm, cur); |
| 923 | } |
| 924 | |
| 925 | /* its lock must be held */ |
| 926 | static void vgic_its_free_collection_list(struct kvm *kvm, struct vgic_its *its) |
| 927 | { |
| 928 | struct its_collection *cur, *temp; |
| 929 | |
| 930 | list_for_each_entry_safe(cur, temp, &its->collection_list, coll_list) |
| 931 | vgic_its_free_collection(its, cur->collection_id); |
| 932 | } |
| 933 | |
Eric Auger | 528297f | 2016-12-25 18:57:54 +0100 | [diff] [blame] | 934 | /* Must be called with its_lock mutex held */ |
| 935 | static struct its_device *vgic_its_alloc_device(struct vgic_its *its, |
| 936 | u32 device_id, gpa_t itt_addr, |
| 937 | u8 num_eventid_bits) |
| 938 | { |
| 939 | struct its_device *device; |
| 940 | |
| 941 | device = kzalloc(sizeof(*device), GFP_KERNEL); |
| 942 | if (!device) |
| 943 | return ERR_PTR(-ENOMEM); |
| 944 | |
| 945 | device->device_id = device_id; |
| 946 | device->itt_addr = itt_addr; |
| 947 | device->num_eventid_bits = num_eventid_bits; |
| 948 | INIT_LIST_HEAD(&device->itt_head); |
| 949 | |
| 950 | list_add_tail(&device->dev_list, &its->device_list); |
| 951 | return device; |
| 952 | } |
| 953 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 954 | /* |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 955 | * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs). |
| 956 | * Must be called with the its_lock mutex held. |
| 957 | */ |
| 958 | static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, |
| 959 | u64 *its_cmd) |
| 960 | { |
| 961 | u32 device_id = its_cmd_get_deviceid(its_cmd); |
| 962 | bool valid = its_cmd_get_validbit(its_cmd); |
Eric Auger | 0d44cdb | 2016-12-22 18:14:14 +0100 | [diff] [blame] | 963 | u8 num_eventid_bits = its_cmd_get_size(its_cmd); |
Eric Auger | 7333cef | 2017-02-02 13:45:45 +0100 | [diff] [blame] | 964 | gpa_t itt_addr = its_cmd_get_ittaddr(its_cmd); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 965 | struct its_device *device; |
| 966 | |
Eric Auger | dceff70 | 2017-01-31 14:36:14 +0100 | [diff] [blame] | 967 | if (!vgic_its_check_id(its, its->baser_device_table, device_id, NULL)) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 968 | return E_ITS_MAPD_DEVICE_OOR; |
| 969 | |
Eric Auger | 0d44cdb | 2016-12-22 18:14:14 +0100 | [diff] [blame] | 970 | if (valid && num_eventid_bits > VITS_TYPER_IDBITS) |
| 971 | return E_ITS_MAPD_ITTSIZE_OOR; |
| 972 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 973 | device = find_its_device(its, device_id); |
| 974 | |
| 975 | /* |
| 976 | * The spec says that calling MAPD on an already mapped device |
| 977 | * invalidates all cached data for this device. We implement this |
| 978 | * by removing the mapping and re-establishing it. |
| 979 | */ |
| 980 | if (device) |
Eric Auger | 0a0d389 | 2017-10-26 17:23:07 +0200 | [diff] [blame] | 981 | vgic_its_free_device(kvm, device); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 982 | |
| 983 | /* |
| 984 | * The spec does not say whether unmapping a not-mapped device |
| 985 | * is an error, so we are done in any case. |
| 986 | */ |
| 987 | if (!valid) |
| 988 | return 0; |
| 989 | |
Eric Auger | 528297f | 2016-12-25 18:57:54 +0100 | [diff] [blame] | 990 | device = vgic_its_alloc_device(its, device_id, itt_addr, |
| 991 | num_eventid_bits); |
| 992 | if (IS_ERR(device)) |
| 993 | return PTR_ERR(device); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 994 | |
| 995 | return 0; |
| 996 | } |
| 997 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 998 | /* |
| 999 | * The MAPC command maps collection IDs to redistributors. |
| 1000 | * Must be called with the its_lock mutex held. |
| 1001 | */ |
| 1002 | static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its, |
| 1003 | u64 *its_cmd) |
| 1004 | { |
| 1005 | u16 coll_id; |
| 1006 | u32 target_addr; |
| 1007 | struct its_collection *collection; |
| 1008 | bool valid; |
| 1009 | |
| 1010 | valid = its_cmd_get_validbit(its_cmd); |
| 1011 | coll_id = its_cmd_get_collection(its_cmd); |
| 1012 | target_addr = its_cmd_get_target_addr(its_cmd); |
| 1013 | |
| 1014 | if (target_addr >= atomic_read(&kvm->online_vcpus)) |
| 1015 | return E_ITS_MAPC_PROCNUM_OOR; |
| 1016 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1017 | if (!valid) { |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 1018 | vgic_its_free_collection(its, coll_id); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1019 | } else { |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 1020 | collection = find_collection(its, coll_id); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1021 | |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 1022 | if (!collection) { |
| 1023 | int ret; |
| 1024 | |
| 1025 | ret = vgic_its_alloc_collection(its, &collection, |
| 1026 | coll_id); |
| 1027 | if (ret) |
| 1028 | return ret; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1029 | collection->target_addr = target_addr; |
| 1030 | } else { |
| 1031 | collection->target_addr = target_addr; |
| 1032 | update_affinity_collection(kvm, its, collection); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | return 0; |
| 1037 | } |
| 1038 | |
| 1039 | /* |
| 1040 | * The CLEAR command removes the pending state for a particular LPI. |
| 1041 | * Must be called with the its_lock mutex held. |
| 1042 | */ |
| 1043 | static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its, |
| 1044 | u64 *its_cmd) |
| 1045 | { |
| 1046 | u32 device_id = its_cmd_get_deviceid(its_cmd); |
| 1047 | u32 event_id = its_cmd_get_id(its_cmd); |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 1048 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1049 | |
| 1050 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 1051 | ite = find_ite(its, device_id, event_id); |
| 1052 | if (!ite) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1053 | return E_ITS_CLEAR_UNMAPPED_INTERRUPT; |
| 1054 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 1055 | ite->irq->pending_latch = false; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1056 | |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | /* |
| 1061 | * The INV command syncs the configuration bits from the memory table. |
| 1062 | * Must be called with the its_lock mutex held. |
| 1063 | */ |
| 1064 | static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its, |
| 1065 | u64 *its_cmd) |
| 1066 | { |
| 1067 | u32 device_id = its_cmd_get_deviceid(its_cmd); |
| 1068 | u32 event_id = its_cmd_get_id(its_cmd); |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 1069 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1070 | |
| 1071 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 1072 | ite = find_ite(its, device_id, event_id); |
| 1073 | if (!ite) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1074 | return E_ITS_INV_UNMAPPED_INTERRUPT; |
| 1075 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 1076 | return update_lpi_config(kvm, ite->irq, NULL); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /* |
| 1080 | * The INVALL command requests flushing of all IRQ data in this collection. |
| 1081 | * Find the VCPU mapped to that collection, then iterate over the VM's list |
| 1082 | * of mapped LPIs and update the configuration for each IRQ which targets |
| 1083 | * the specified vcpu. The configuration will be read from the in-memory |
| 1084 | * configuration table. |
| 1085 | * Must be called with the its_lock mutex held. |
| 1086 | */ |
| 1087 | static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its, |
| 1088 | u64 *its_cmd) |
| 1089 | { |
| 1090 | u32 coll_id = its_cmd_get_collection(its_cmd); |
| 1091 | struct its_collection *collection; |
| 1092 | struct kvm_vcpu *vcpu; |
| 1093 | struct vgic_irq *irq; |
| 1094 | u32 *intids; |
| 1095 | int irq_count, i; |
| 1096 | |
| 1097 | collection = find_collection(its, coll_id); |
| 1098 | if (!its_is_collection_mapped(collection)) |
| 1099 | return E_ITS_INVALL_UNMAPPED_COLLECTION; |
| 1100 | |
| 1101 | vcpu = kvm_get_vcpu(kvm, collection->target_addr); |
| 1102 | |
Eric Auger | ccb1d79 | 2017-04-12 14:13:27 +0200 | [diff] [blame] | 1103 | irq_count = vgic_copy_lpi_list(vcpu, &intids); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1104 | if (irq_count < 0) |
| 1105 | return irq_count; |
| 1106 | |
| 1107 | for (i = 0; i < irq_count; i++) { |
| 1108 | irq = vgic_get_irq(kvm, NULL, intids[i]); |
| 1109 | if (!irq) |
| 1110 | continue; |
| 1111 | update_lpi_config(kvm, irq, vcpu); |
| 1112 | vgic_put_irq(kvm, irq); |
| 1113 | } |
| 1114 | |
| 1115 | kfree(intids); |
| 1116 | |
| 1117 | return 0; |
| 1118 | } |
| 1119 | |
| 1120 | /* |
| 1121 | * The MOVALL command moves the pending state of all IRQs targeting one |
| 1122 | * redistributor to another. We don't hold the pending state in the VCPUs, |
| 1123 | * but in the IRQs instead, so there is really not much to do for us here. |
| 1124 | * However the spec says that no IRQ must target the old redistributor |
| 1125 | * afterwards, so we make sure that no LPI is using the associated target_vcpu. |
| 1126 | * This command affects all LPIs in the system that target that redistributor. |
| 1127 | */ |
| 1128 | static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its, |
| 1129 | u64 *its_cmd) |
| 1130 | { |
| 1131 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 1132 | u32 target1_addr = its_cmd_get_target_addr(its_cmd); |
| 1133 | u32 target2_addr = its_cmd_mask_field(its_cmd, 3, 16, 32); |
| 1134 | struct kvm_vcpu *vcpu1, *vcpu2; |
| 1135 | struct vgic_irq *irq; |
| 1136 | |
| 1137 | if (target1_addr >= atomic_read(&kvm->online_vcpus) || |
| 1138 | target2_addr >= atomic_read(&kvm->online_vcpus)) |
| 1139 | return E_ITS_MOVALL_PROCNUM_OOR; |
| 1140 | |
| 1141 | if (target1_addr == target2_addr) |
| 1142 | return 0; |
| 1143 | |
| 1144 | vcpu1 = kvm_get_vcpu(kvm, target1_addr); |
| 1145 | vcpu2 = kvm_get_vcpu(kvm, target2_addr); |
| 1146 | |
| 1147 | spin_lock(&dist->lpi_list_lock); |
| 1148 | |
| 1149 | list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { |
| 1150 | spin_lock(&irq->irq_lock); |
| 1151 | |
| 1152 | if (irq->target_vcpu == vcpu1) |
| 1153 | irq->target_vcpu = vcpu2; |
| 1154 | |
| 1155 | spin_unlock(&irq->irq_lock); |
| 1156 | } |
| 1157 | |
| 1158 | spin_unlock(&dist->lpi_list_lock); |
| 1159 | |
| 1160 | return 0; |
| 1161 | } |
| 1162 | |
| 1163 | /* |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 1164 | * The INT command injects the LPI associated with that DevID/EvID pair. |
| 1165 | * Must be called with the its_lock mutex held. |
| 1166 | */ |
| 1167 | static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its, |
| 1168 | u64 *its_cmd) |
| 1169 | { |
| 1170 | u32 msi_data = its_cmd_get_id(its_cmd); |
| 1171 | u64 msi_devid = its_cmd_get_deviceid(its_cmd); |
| 1172 | |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 1173 | return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data); |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 1174 | } |
| 1175 | |
| 1176 | /* |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1177 | * This function is called with the its_cmd lock held, but the ITS data |
| 1178 | * structure lock dropped. |
| 1179 | */ |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1180 | static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its, |
| 1181 | u64 *its_cmd) |
| 1182 | { |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1183 | int ret = -ENODEV; |
| 1184 | |
| 1185 | mutex_lock(&its->its_lock); |
Marc Zyngier | a3e7aa2 | 2016-07-17 22:38:32 +0100 | [diff] [blame] | 1186 | switch (its_cmd_get_command(its_cmd)) { |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1187 | case GITS_CMD_MAPD: |
| 1188 | ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd); |
| 1189 | break; |
| 1190 | case GITS_CMD_MAPC: |
| 1191 | ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd); |
| 1192 | break; |
| 1193 | case GITS_CMD_MAPI: |
Marc Zyngier | a3e7aa2 | 2016-07-17 22:38:32 +0100 | [diff] [blame] | 1194 | ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1195 | break; |
| 1196 | case GITS_CMD_MAPTI: |
Marc Zyngier | a3e7aa2 | 2016-07-17 22:38:32 +0100 | [diff] [blame] | 1197 | ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1198 | break; |
| 1199 | case GITS_CMD_MOVI: |
| 1200 | ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd); |
| 1201 | break; |
| 1202 | case GITS_CMD_DISCARD: |
| 1203 | ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd); |
| 1204 | break; |
| 1205 | case GITS_CMD_CLEAR: |
| 1206 | ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd); |
| 1207 | break; |
| 1208 | case GITS_CMD_MOVALL: |
| 1209 | ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd); |
| 1210 | break; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 1211 | case GITS_CMD_INT: |
| 1212 | ret = vgic_its_cmd_handle_int(kvm, its, its_cmd); |
| 1213 | break; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1214 | case GITS_CMD_INV: |
| 1215 | ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd); |
| 1216 | break; |
| 1217 | case GITS_CMD_INVALL: |
| 1218 | ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd); |
| 1219 | break; |
| 1220 | case GITS_CMD_SYNC: |
| 1221 | /* we ignore this command: we are in sync all of the time */ |
| 1222 | ret = 0; |
| 1223 | break; |
| 1224 | } |
| 1225 | mutex_unlock(&its->its_lock); |
| 1226 | |
| 1227 | return ret; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | static u64 vgic_sanitise_its_baser(u64 reg) |
| 1231 | { |
| 1232 | reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK, |
| 1233 | GITS_BASER_SHAREABILITY_SHIFT, |
| 1234 | vgic_sanitise_shareability); |
| 1235 | reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK, |
| 1236 | GITS_BASER_INNER_CACHEABILITY_SHIFT, |
| 1237 | vgic_sanitise_inner_cacheability); |
| 1238 | reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK, |
| 1239 | GITS_BASER_OUTER_CACHEABILITY_SHIFT, |
| 1240 | vgic_sanitise_outer_cacheability); |
| 1241 | |
| 1242 | /* Bits 15:12 contain bits 51:48 of the PA, which we don't support. */ |
| 1243 | reg &= ~GENMASK_ULL(15, 12); |
| 1244 | |
| 1245 | /* We support only one (ITS) page size: 64K */ |
| 1246 | reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K; |
| 1247 | |
| 1248 | return reg; |
| 1249 | } |
| 1250 | |
| 1251 | static u64 vgic_sanitise_its_cbaser(u64 reg) |
| 1252 | { |
| 1253 | reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK, |
| 1254 | GITS_CBASER_SHAREABILITY_SHIFT, |
| 1255 | vgic_sanitise_shareability); |
| 1256 | reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK, |
| 1257 | GITS_CBASER_INNER_CACHEABILITY_SHIFT, |
| 1258 | vgic_sanitise_inner_cacheability); |
| 1259 | reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK, |
| 1260 | GITS_CBASER_OUTER_CACHEABILITY_SHIFT, |
| 1261 | vgic_sanitise_outer_cacheability); |
| 1262 | |
| 1263 | /* |
| 1264 | * Sanitise the physical address to be 64k aligned. |
| 1265 | * Also limit the physical addresses to 48 bits. |
| 1266 | */ |
| 1267 | reg &= ~(GENMASK_ULL(51, 48) | GENMASK_ULL(15, 12)); |
| 1268 | |
| 1269 | return reg; |
| 1270 | } |
| 1271 | |
| 1272 | static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm, |
| 1273 | struct vgic_its *its, |
| 1274 | gpa_t addr, unsigned int len) |
| 1275 | { |
| 1276 | return extract_bytes(its->cbaser, addr & 7, len); |
| 1277 | } |
| 1278 | |
| 1279 | static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its, |
| 1280 | gpa_t addr, unsigned int len, |
| 1281 | unsigned long val) |
| 1282 | { |
| 1283 | /* When GITS_CTLR.Enable is 1, this register is RO. */ |
| 1284 | if (its->enabled) |
| 1285 | return; |
| 1286 | |
| 1287 | mutex_lock(&its->cmd_lock); |
| 1288 | its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val); |
| 1289 | its->cbaser = vgic_sanitise_its_cbaser(its->cbaser); |
| 1290 | its->creadr = 0; |
| 1291 | /* |
| 1292 | * CWRITER is architecturally UNKNOWN on reset, but we need to reset |
| 1293 | * it to CREADR to make sure we start with an empty command buffer. |
| 1294 | */ |
| 1295 | its->cwriter = its->creadr; |
| 1296 | mutex_unlock(&its->cmd_lock); |
| 1297 | } |
| 1298 | |
| 1299 | #define ITS_CMD_BUFFER_SIZE(baser) ((((baser) & 0xff) + 1) << 12) |
| 1300 | #define ITS_CMD_SIZE 32 |
| 1301 | #define ITS_CMD_OFFSET(reg) ((reg) & GENMASK(19, 5)) |
| 1302 | |
Andre Przywara | a5e1e6c | 2017-02-16 10:41:20 +0000 | [diff] [blame] | 1303 | /* Must be called with the cmd_lock held. */ |
| 1304 | static void vgic_its_process_commands(struct kvm *kvm, struct vgic_its *its) |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1305 | { |
| 1306 | gpa_t cbaser; |
| 1307 | u64 cmd_buf[4]; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1308 | |
Andre Przywara | a5e1e6c | 2017-02-16 10:41:20 +0000 | [diff] [blame] | 1309 | /* Commands are only processed when the ITS is enabled. */ |
| 1310 | if (!its->enabled) |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1311 | return; |
| 1312 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1313 | cbaser = CBASER_ADDRESS(its->cbaser); |
| 1314 | |
| 1315 | while (its->cwriter != its->creadr) { |
| 1316 | int ret = kvm_read_guest(kvm, cbaser + its->creadr, |
| 1317 | cmd_buf, ITS_CMD_SIZE); |
| 1318 | /* |
| 1319 | * If kvm_read_guest() fails, this could be due to the guest |
| 1320 | * programming a bogus value in CBASER or something else going |
| 1321 | * wrong from which we cannot easily recover. |
| 1322 | * According to section 6.3.2 in the GICv3 spec we can just |
| 1323 | * ignore that command then. |
| 1324 | */ |
| 1325 | if (!ret) |
| 1326 | vgic_its_handle_command(kvm, its, cmd_buf); |
| 1327 | |
| 1328 | its->creadr += ITS_CMD_SIZE; |
| 1329 | if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser)) |
| 1330 | its->creadr = 0; |
| 1331 | } |
Andre Przywara | a5e1e6c | 2017-02-16 10:41:20 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
| 1334 | /* |
| 1335 | * By writing to CWRITER the guest announces new commands to be processed. |
| 1336 | * To avoid any races in the first place, we take the its_cmd lock, which |
| 1337 | * protects our ring buffer variables, so that there is only one user |
| 1338 | * per ITS handling commands at a given time. |
| 1339 | */ |
| 1340 | static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its, |
| 1341 | gpa_t addr, unsigned int len, |
| 1342 | unsigned long val) |
| 1343 | { |
| 1344 | u64 reg; |
| 1345 | |
| 1346 | if (!its) |
| 1347 | return; |
| 1348 | |
| 1349 | mutex_lock(&its->cmd_lock); |
| 1350 | |
| 1351 | reg = update_64bit_reg(its->cwriter, addr & 7, len, val); |
| 1352 | reg = ITS_CMD_OFFSET(reg); |
| 1353 | if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) { |
| 1354 | mutex_unlock(&its->cmd_lock); |
| 1355 | return; |
| 1356 | } |
| 1357 | its->cwriter = reg; |
| 1358 | |
| 1359 | vgic_its_process_commands(kvm, its); |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1360 | |
| 1361 | mutex_unlock(&its->cmd_lock); |
| 1362 | } |
| 1363 | |
| 1364 | static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm, |
| 1365 | struct vgic_its *its, |
| 1366 | gpa_t addr, unsigned int len) |
| 1367 | { |
| 1368 | return extract_bytes(its->cwriter, addr & 0x7, len); |
| 1369 | } |
| 1370 | |
| 1371 | static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm, |
| 1372 | struct vgic_its *its, |
| 1373 | gpa_t addr, unsigned int len) |
| 1374 | { |
| 1375 | return extract_bytes(its->creadr, addr & 0x7, len); |
| 1376 | } |
| 1377 | |
Eric Auger | 0979bfa | 2017-01-04 11:58:41 +0100 | [diff] [blame] | 1378 | static int vgic_mmio_uaccess_write_its_creadr(struct kvm *kvm, |
| 1379 | struct vgic_its *its, |
| 1380 | gpa_t addr, unsigned int len, |
| 1381 | unsigned long val) |
| 1382 | { |
| 1383 | u32 cmd_offset; |
| 1384 | int ret = 0; |
| 1385 | |
| 1386 | mutex_lock(&its->cmd_lock); |
| 1387 | |
| 1388 | if (its->enabled) { |
| 1389 | ret = -EBUSY; |
| 1390 | goto out; |
| 1391 | } |
| 1392 | |
| 1393 | cmd_offset = ITS_CMD_OFFSET(val); |
| 1394 | if (cmd_offset >= ITS_CMD_BUFFER_SIZE(its->cbaser)) { |
| 1395 | ret = -EINVAL; |
| 1396 | goto out; |
| 1397 | } |
| 1398 | |
| 1399 | its->creadr = cmd_offset; |
| 1400 | out: |
| 1401 | mutex_unlock(&its->cmd_lock); |
| 1402 | return ret; |
| 1403 | } |
| 1404 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1405 | #define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7) |
| 1406 | static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm, |
| 1407 | struct vgic_its *its, |
| 1408 | gpa_t addr, unsigned int len) |
| 1409 | { |
| 1410 | u64 reg; |
| 1411 | |
| 1412 | switch (BASER_INDEX(addr)) { |
| 1413 | case 0: |
| 1414 | reg = its->baser_device_table; |
| 1415 | break; |
| 1416 | case 1: |
| 1417 | reg = its->baser_coll_table; |
| 1418 | break; |
| 1419 | default: |
| 1420 | reg = 0; |
| 1421 | break; |
| 1422 | } |
| 1423 | |
| 1424 | return extract_bytes(reg, addr & 7, len); |
| 1425 | } |
| 1426 | |
| 1427 | #define GITS_BASER_RO_MASK (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56)) |
| 1428 | static void vgic_mmio_write_its_baser(struct kvm *kvm, |
| 1429 | struct vgic_its *its, |
| 1430 | gpa_t addr, unsigned int len, |
| 1431 | unsigned long val) |
| 1432 | { |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 1433 | const struct vgic_its_abi *abi = vgic_its_get_abi(its); |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1434 | u64 entry_size, device_type; |
| 1435 | u64 reg, *regptr, clearbits = 0; |
| 1436 | |
| 1437 | /* When GITS_CTLR.Enable is 1, we ignore write accesses. */ |
| 1438 | if (its->enabled) |
| 1439 | return; |
| 1440 | |
| 1441 | switch (BASER_INDEX(addr)) { |
| 1442 | case 0: |
| 1443 | regptr = &its->baser_device_table; |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 1444 | entry_size = abi->dte_esz; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1445 | device_type = GITS_BASER_TYPE_DEVICE; |
| 1446 | break; |
| 1447 | case 1: |
| 1448 | regptr = &its->baser_coll_table; |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 1449 | entry_size = abi->cte_esz; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1450 | device_type = GITS_BASER_TYPE_COLLECTION; |
| 1451 | clearbits = GITS_BASER_INDIRECT; |
| 1452 | break; |
| 1453 | default: |
| 1454 | return; |
| 1455 | } |
| 1456 | |
| 1457 | reg = update_64bit_reg(*regptr, addr & 7, len, val); |
| 1458 | reg &= ~GITS_BASER_RO_MASK; |
| 1459 | reg &= ~clearbits; |
| 1460 | |
| 1461 | reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT; |
| 1462 | reg |= device_type << GITS_BASER_TYPE_SHIFT; |
| 1463 | reg = vgic_sanitise_its_baser(reg); |
| 1464 | |
| 1465 | *regptr = reg; |
| 1466 | } |
| 1467 | |
Andre Przywara | a5e1e6c | 2017-02-16 10:41:20 +0000 | [diff] [blame] | 1468 | static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu, |
| 1469 | struct vgic_its *its, |
| 1470 | gpa_t addr, unsigned int len) |
| 1471 | { |
| 1472 | u32 reg = 0; |
| 1473 | |
| 1474 | mutex_lock(&its->cmd_lock); |
| 1475 | if (its->creadr == its->cwriter) |
| 1476 | reg |= GITS_CTLR_QUIESCENT; |
| 1477 | if (its->enabled) |
| 1478 | reg |= GITS_CTLR_ENABLE; |
| 1479 | mutex_unlock(&its->cmd_lock); |
| 1480 | |
| 1481 | return reg; |
| 1482 | } |
| 1483 | |
| 1484 | static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its, |
| 1485 | gpa_t addr, unsigned int len, |
| 1486 | unsigned long val) |
| 1487 | { |
| 1488 | mutex_lock(&its->cmd_lock); |
| 1489 | |
| 1490 | its->enabled = !!(val & GITS_CTLR_ENABLE); |
| 1491 | |
| 1492 | /* |
| 1493 | * Try to process any pending commands. This function bails out early |
| 1494 | * if the ITS is disabled or no commands have been queued. |
| 1495 | */ |
| 1496 | vgic_its_process_commands(kvm, its); |
| 1497 | |
| 1498 | mutex_unlock(&its->cmd_lock); |
| 1499 | } |
| 1500 | |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1501 | #define REGISTER_ITS_DESC(off, rd, wr, length, acc) \ |
| 1502 | { \ |
| 1503 | .reg_offset = off, \ |
| 1504 | .len = length, \ |
| 1505 | .access_flags = acc, \ |
| 1506 | .its_read = rd, \ |
| 1507 | .its_write = wr, \ |
| 1508 | } |
| 1509 | |
Eric Auger | 0979bfa | 2017-01-04 11:58:41 +0100 | [diff] [blame] | 1510 | #define REGISTER_ITS_DESC_UACCESS(off, rd, wr, uwr, length, acc)\ |
| 1511 | { \ |
| 1512 | .reg_offset = off, \ |
| 1513 | .len = length, \ |
| 1514 | .access_flags = acc, \ |
| 1515 | .its_read = rd, \ |
| 1516 | .its_write = wr, \ |
| 1517 | .uaccess_its_write = uwr, \ |
| 1518 | } |
| 1519 | |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1520 | static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its, |
| 1521 | gpa_t addr, unsigned int len, unsigned long val) |
| 1522 | { |
| 1523 | /* Ignore */ |
| 1524 | } |
| 1525 | |
| 1526 | static struct vgic_register_region its_registers[] = { |
| 1527 | REGISTER_ITS_DESC(GITS_CTLR, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1528 | vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1529 | VGIC_ACCESS_32bit), |
Eric Auger | ab01c6b | 2017-03-23 15:14:00 +0100 | [diff] [blame] | 1530 | REGISTER_ITS_DESC_UACCESS(GITS_IIDR, |
| 1531 | vgic_mmio_read_its_iidr, its_mmio_write_wi, |
| 1532 | vgic_mmio_uaccess_write_its_iidr, 4, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1533 | VGIC_ACCESS_32bit), |
| 1534 | REGISTER_ITS_DESC(GITS_TYPER, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1535 | vgic_mmio_read_its_typer, its_mmio_write_wi, 8, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1536 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
| 1537 | REGISTER_ITS_DESC(GITS_CBASER, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1538 | vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1539 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
| 1540 | REGISTER_ITS_DESC(GITS_CWRITER, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1541 | vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1542 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
Eric Auger | 0979bfa | 2017-01-04 11:58:41 +0100 | [diff] [blame] | 1543 | REGISTER_ITS_DESC_UACCESS(GITS_CREADR, |
| 1544 | vgic_mmio_read_its_creadr, its_mmio_write_wi, |
| 1545 | vgic_mmio_uaccess_write_its_creadr, 8, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1546 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
| 1547 | REGISTER_ITS_DESC(GITS_BASER, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1548 | vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1549 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
| 1550 | REGISTER_ITS_DESC(GITS_IDREGS_BASE, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1551 | vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1552 | VGIC_ACCESS_32bit), |
| 1553 | }; |
| 1554 | |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 1555 | /* This is called on setting the LPI enable bit in the redistributor. */ |
| 1556 | void vgic_enable_lpis(struct kvm_vcpu *vcpu) |
| 1557 | { |
| 1558 | if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ)) |
| 1559 | its_sync_lpi_pending_table(vcpu); |
| 1560 | } |
| 1561 | |
Christoffer Dall | 30e1b68 | 2017-05-08 13:14:57 +0200 | [diff] [blame] | 1562 | static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its, |
| 1563 | u64 addr) |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1564 | { |
| 1565 | struct vgic_io_device *iodev = &its->iodev; |
| 1566 | int ret; |
| 1567 | |
Christoffer Dall | 30e1b68 | 2017-05-08 13:14:57 +0200 | [diff] [blame] | 1568 | mutex_lock(&kvm->slots_lock); |
| 1569 | if (!IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) { |
| 1570 | ret = -EBUSY; |
| 1571 | goto out; |
| 1572 | } |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1573 | |
Christoffer Dall | 30e1b68 | 2017-05-08 13:14:57 +0200 | [diff] [blame] | 1574 | its->vgic_its_base = addr; |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1575 | iodev->regions = its_registers; |
| 1576 | iodev->nr_regions = ARRAY_SIZE(its_registers); |
| 1577 | kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops); |
| 1578 | |
| 1579 | iodev->base_addr = its->vgic_its_base; |
| 1580 | iodev->iodev_type = IODEV_ITS; |
| 1581 | iodev->its = its; |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1582 | ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr, |
| 1583 | KVM_VGIC_V3_ITS_SIZE, &iodev->dev); |
Christoffer Dall | 30e1b68 | 2017-05-08 13:14:57 +0200 | [diff] [blame] | 1584 | out: |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1585 | mutex_unlock(&kvm->slots_lock); |
| 1586 | |
| 1587 | return ret; |
| 1588 | } |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1589 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1590 | #define INITIAL_BASER_VALUE \ |
| 1591 | (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \ |
| 1592 | GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \ |
| 1593 | GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \ |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1594 | GITS_BASER_PAGE_SIZE_64K) |
| 1595 | |
| 1596 | #define INITIAL_PROPBASER_VALUE \ |
| 1597 | (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb) | \ |
| 1598 | GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner) | \ |
| 1599 | GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable)) |
| 1600 | |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1601 | static int vgic_its_create(struct kvm_device *dev, u32 type) |
| 1602 | { |
| 1603 | struct vgic_its *its; |
| 1604 | |
| 1605 | if (type != KVM_DEV_TYPE_ARM_VGIC_ITS) |
| 1606 | return -ENODEV; |
| 1607 | |
| 1608 | its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL); |
| 1609 | if (!its) |
| 1610 | return -ENOMEM; |
| 1611 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1612 | mutex_init(&its->its_lock); |
| 1613 | mutex_init(&its->cmd_lock); |
| 1614 | |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1615 | its->vgic_its_base = VGIC_ADDR_UNDEF; |
| 1616 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1617 | INIT_LIST_HEAD(&its->device_list); |
| 1618 | INIT_LIST_HEAD(&its->collection_list); |
| 1619 | |
Shanker Donthineni | 79962a5 | 2017-07-08 08:48:30 -0500 | [diff] [blame] | 1620 | dev->kvm->arch.vgic.msis_require_devid = true; |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1621 | dev->kvm->arch.vgic.has_its = true; |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1622 | its->enabled = false; |
Marc Zyngier | bb71764 | 2016-07-17 21:35:07 +0100 | [diff] [blame] | 1623 | its->dev = dev; |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1624 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1625 | its->baser_device_table = INITIAL_BASER_VALUE | |
| 1626 | ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT); |
| 1627 | its->baser_coll_table = INITIAL_BASER_VALUE | |
| 1628 | ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT); |
| 1629 | dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE; |
| 1630 | |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1631 | dev->private = its; |
| 1632 | |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 1633 | return vgic_its_set_abi(its, NR_ITS_ABIS - 1); |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1634 | } |
| 1635 | |
| 1636 | static void vgic_its_destroy(struct kvm_device *kvm_dev) |
| 1637 | { |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1638 | struct kvm *kvm = kvm_dev->kvm; |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1639 | struct vgic_its *its = kvm_dev->private; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1640 | |
| 1641 | mutex_lock(&its->its_lock); |
Christoffer Dall | a2b19e6 | 2017-05-08 13:31:12 +0200 | [diff] [blame] | 1642 | |
wanghaibin | 2f609a0 | 2017-10-26 17:23:08 +0200 | [diff] [blame^] | 1643 | vgic_its_free_device_list(kvm, its); |
| 1644 | vgic_its_free_collection_list(kvm, its); |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1645 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1646 | mutex_unlock(&its->its_lock); |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1647 | kfree(its); |
| 1648 | } |
| 1649 | |
Eric Auger | 876ae23 | 2016-12-20 01:36:35 -0500 | [diff] [blame] | 1650 | int vgic_its_has_attr_regs(struct kvm_device *dev, |
| 1651 | struct kvm_device_attr *attr) |
| 1652 | { |
Eric Auger | 8331c23 | 2016-12-20 09:33:13 +0100 | [diff] [blame] | 1653 | const struct vgic_register_region *region; |
| 1654 | gpa_t offset = attr->attr; |
| 1655 | int align; |
| 1656 | |
| 1657 | align = (offset < GITS_TYPER) || (offset >= GITS_PIDR4) ? 0x3 : 0x7; |
| 1658 | |
| 1659 | if (offset & align) |
| 1660 | return -EINVAL; |
| 1661 | |
| 1662 | region = vgic_find_mmio_region(its_registers, |
| 1663 | ARRAY_SIZE(its_registers), |
| 1664 | offset); |
| 1665 | if (!region) |
| 1666 | return -ENXIO; |
| 1667 | |
| 1668 | return 0; |
Eric Auger | 876ae23 | 2016-12-20 01:36:35 -0500 | [diff] [blame] | 1669 | } |
| 1670 | |
| 1671 | int vgic_its_attr_regs_access(struct kvm_device *dev, |
| 1672 | struct kvm_device_attr *attr, |
| 1673 | u64 *reg, bool is_write) |
| 1674 | { |
Eric Auger | 8331c23 | 2016-12-20 09:33:13 +0100 | [diff] [blame] | 1675 | const struct vgic_register_region *region; |
| 1676 | struct vgic_its *its; |
| 1677 | gpa_t addr, offset; |
| 1678 | unsigned int len; |
| 1679 | int align, ret = 0; |
| 1680 | |
| 1681 | its = dev->private; |
| 1682 | offset = attr->attr; |
| 1683 | |
| 1684 | /* |
| 1685 | * Although the spec supports upper/lower 32-bit accesses to |
| 1686 | * 64-bit ITS registers, the userspace ABI requires 64-bit |
| 1687 | * accesses to all 64-bit wide registers. We therefore only |
| 1688 | * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID |
| 1689 | * registers |
| 1690 | */ |
| 1691 | if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4)) |
| 1692 | align = 0x3; |
| 1693 | else |
| 1694 | align = 0x7; |
| 1695 | |
| 1696 | if (offset & align) |
| 1697 | return -EINVAL; |
| 1698 | |
| 1699 | mutex_lock(&dev->kvm->lock); |
| 1700 | |
| 1701 | if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) { |
| 1702 | ret = -ENXIO; |
| 1703 | goto out; |
| 1704 | } |
| 1705 | |
| 1706 | region = vgic_find_mmio_region(its_registers, |
| 1707 | ARRAY_SIZE(its_registers), |
| 1708 | offset); |
| 1709 | if (!region) { |
| 1710 | ret = -ENXIO; |
| 1711 | goto out; |
| 1712 | } |
| 1713 | |
| 1714 | if (!lock_all_vcpus(dev->kvm)) { |
| 1715 | ret = -EBUSY; |
| 1716 | goto out; |
| 1717 | } |
| 1718 | |
| 1719 | addr = its->vgic_its_base + offset; |
| 1720 | |
| 1721 | len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4; |
| 1722 | |
| 1723 | if (is_write) { |
| 1724 | if (region->uaccess_its_write) |
| 1725 | ret = region->uaccess_its_write(dev->kvm, its, addr, |
| 1726 | len, *reg); |
| 1727 | else |
| 1728 | region->its_write(dev->kvm, its, addr, len, *reg); |
| 1729 | } else { |
| 1730 | *reg = region->its_read(dev->kvm, its, addr, len); |
| 1731 | } |
| 1732 | unlock_all_vcpus(dev->kvm); |
| 1733 | out: |
| 1734 | mutex_unlock(&dev->kvm->lock); |
| 1735 | return ret; |
Eric Auger | 876ae23 | 2016-12-20 01:36:35 -0500 | [diff] [blame] | 1736 | } |
| 1737 | |
Eric Auger | 57a9a11 | 2017-01-09 16:27:07 +0100 | [diff] [blame] | 1738 | static u32 compute_next_devid_offset(struct list_head *h, |
| 1739 | struct its_device *dev) |
Eric Auger | 920a7a8 | 2017-02-08 05:20:04 +0100 | [diff] [blame] | 1740 | { |
| 1741 | struct its_device *next; |
| 1742 | u32 next_offset; |
| 1743 | |
| 1744 | if (list_is_last(&dev->dev_list, h)) |
| 1745 | return 0; |
| 1746 | next = list_next_entry(dev, dev_list); |
| 1747 | next_offset = next->device_id - dev->device_id; |
| 1748 | |
| 1749 | return min_t(u32, next_offset, VITS_DTE_MAX_DEVID_OFFSET); |
| 1750 | } |
| 1751 | |
Eric Auger | eff484e | 2017-05-03 17:38:01 +0200 | [diff] [blame] | 1752 | static u32 compute_next_eventid_offset(struct list_head *h, struct its_ite *ite) |
Eric Auger | 920a7a8 | 2017-02-08 05:20:04 +0100 | [diff] [blame] | 1753 | { |
| 1754 | struct its_ite *next; |
| 1755 | u32 next_offset; |
| 1756 | |
| 1757 | if (list_is_last(&ite->ite_list, h)) |
| 1758 | return 0; |
| 1759 | next = list_next_entry(ite, ite_list); |
| 1760 | next_offset = next->event_id - ite->event_id; |
| 1761 | |
| 1762 | return min_t(u32, next_offset, VITS_ITE_MAX_EVENTID_OFFSET); |
| 1763 | } |
| 1764 | |
| 1765 | /** |
| 1766 | * entry_fn_t - Callback called on a table entry restore path |
| 1767 | * @its: its handle |
| 1768 | * @id: id of the entry |
| 1769 | * @entry: pointer to the entry |
| 1770 | * @opaque: pointer to an opaque data |
| 1771 | * |
| 1772 | * Return: < 0 on error, 0 if last element was identified, id offset to next |
| 1773 | * element otherwise |
| 1774 | */ |
| 1775 | typedef int (*entry_fn_t)(struct vgic_its *its, u32 id, void *entry, |
| 1776 | void *opaque); |
| 1777 | |
| 1778 | /** |
| 1779 | * scan_its_table - Scan a contiguous table in guest RAM and applies a function |
| 1780 | * to each entry |
| 1781 | * |
| 1782 | * @its: its handle |
| 1783 | * @base: base gpa of the table |
| 1784 | * @size: size of the table in bytes |
| 1785 | * @esz: entry size in bytes |
| 1786 | * @start_id: the ID of the first entry in the table |
| 1787 | * (non zero for 2d level tables) |
| 1788 | * @fn: function to apply on each entry |
| 1789 | * |
| 1790 | * Return: < 0 on error, 0 if last element was identified, 1 otherwise |
| 1791 | * (the last element may not be found on second level tables) |
| 1792 | */ |
Eric Auger | 57a9a11 | 2017-01-09 16:27:07 +0100 | [diff] [blame] | 1793 | static int scan_its_table(struct vgic_its *its, gpa_t base, int size, int esz, |
| 1794 | int start_id, entry_fn_t fn, void *opaque) |
Eric Auger | 920a7a8 | 2017-02-08 05:20:04 +0100 | [diff] [blame] | 1795 | { |
| 1796 | void *entry = kzalloc(esz, GFP_KERNEL); |
| 1797 | struct kvm *kvm = its->dev->kvm; |
| 1798 | unsigned long len = size; |
| 1799 | int id = start_id; |
| 1800 | gpa_t gpa = base; |
| 1801 | int ret; |
| 1802 | |
| 1803 | while (len > 0) { |
| 1804 | int next_offset; |
| 1805 | size_t byte_offset; |
| 1806 | |
| 1807 | ret = kvm_read_guest(kvm, gpa, entry, esz); |
| 1808 | if (ret) |
| 1809 | goto out; |
| 1810 | |
| 1811 | next_offset = fn(its, id, entry, opaque); |
| 1812 | if (next_offset <= 0) { |
| 1813 | ret = next_offset; |
| 1814 | goto out; |
| 1815 | } |
| 1816 | |
| 1817 | byte_offset = next_offset * esz; |
| 1818 | id += next_offset; |
| 1819 | gpa += byte_offset; |
| 1820 | len -= byte_offset; |
| 1821 | } |
| 1822 | ret = 1; |
| 1823 | |
| 1824 | out: |
| 1825 | kfree(entry); |
| 1826 | return ret; |
| 1827 | } |
| 1828 | |
Eric Auger | eff484e | 2017-05-03 17:38:01 +0200 | [diff] [blame] | 1829 | /** |
| 1830 | * vgic_its_save_ite - Save an interrupt translation entry at @gpa |
| 1831 | */ |
| 1832 | static int vgic_its_save_ite(struct vgic_its *its, struct its_device *dev, |
| 1833 | struct its_ite *ite, gpa_t gpa, int ite_esz) |
| 1834 | { |
| 1835 | struct kvm *kvm = its->dev->kvm; |
| 1836 | u32 next_offset; |
| 1837 | u64 val; |
| 1838 | |
| 1839 | next_offset = compute_next_eventid_offset(&dev->itt_head, ite); |
| 1840 | val = ((u64)next_offset << KVM_ITS_ITE_NEXT_SHIFT) | |
Marc Zyngier | 7c7d2fa | 2017-09-01 17:51:56 +0100 | [diff] [blame] | 1841 | ((u64)ite->irq->intid << KVM_ITS_ITE_PINTID_SHIFT) | |
Eric Auger | eff484e | 2017-05-03 17:38:01 +0200 | [diff] [blame] | 1842 | ite->collection->collection_id; |
| 1843 | val = cpu_to_le64(val); |
| 1844 | return kvm_write_guest(kvm, gpa, &val, ite_esz); |
| 1845 | } |
| 1846 | |
| 1847 | /** |
| 1848 | * vgic_its_restore_ite - restore an interrupt translation entry |
| 1849 | * @event_id: id used for indexing |
| 1850 | * @ptr: pointer to the ITE entry |
| 1851 | * @opaque: pointer to the its_device |
| 1852 | */ |
| 1853 | static int vgic_its_restore_ite(struct vgic_its *its, u32 event_id, |
| 1854 | void *ptr, void *opaque) |
| 1855 | { |
| 1856 | struct its_device *dev = (struct its_device *)opaque; |
| 1857 | struct its_collection *collection; |
| 1858 | struct kvm *kvm = its->dev->kvm; |
| 1859 | struct kvm_vcpu *vcpu = NULL; |
| 1860 | u64 val; |
| 1861 | u64 *p = (u64 *)ptr; |
| 1862 | struct vgic_irq *irq; |
| 1863 | u32 coll_id, lpi_id; |
| 1864 | struct its_ite *ite; |
| 1865 | u32 offset; |
| 1866 | |
| 1867 | val = *p; |
| 1868 | |
| 1869 | val = le64_to_cpu(val); |
| 1870 | |
| 1871 | coll_id = val & KVM_ITS_ITE_ICID_MASK; |
| 1872 | lpi_id = (val & KVM_ITS_ITE_PINTID_MASK) >> KVM_ITS_ITE_PINTID_SHIFT; |
| 1873 | |
| 1874 | if (!lpi_id) |
| 1875 | return 1; /* invalid entry, no choice but to scan next entry */ |
| 1876 | |
| 1877 | if (lpi_id < VGIC_MIN_LPI) |
| 1878 | return -EINVAL; |
| 1879 | |
| 1880 | offset = val >> KVM_ITS_ITE_NEXT_SHIFT; |
| 1881 | if (event_id + offset >= BIT_ULL(dev->num_eventid_bits)) |
| 1882 | return -EINVAL; |
| 1883 | |
| 1884 | collection = find_collection(its, coll_id); |
| 1885 | if (!collection) |
| 1886 | return -EINVAL; |
| 1887 | |
Marc Zyngier | 7c7d2fa | 2017-09-01 17:51:56 +0100 | [diff] [blame] | 1888 | ite = vgic_its_alloc_ite(dev, collection, event_id); |
Eric Auger | eff484e | 2017-05-03 17:38:01 +0200 | [diff] [blame] | 1889 | if (IS_ERR(ite)) |
| 1890 | return PTR_ERR(ite); |
| 1891 | |
| 1892 | if (its_is_collection_mapped(collection)) |
| 1893 | vcpu = kvm_get_vcpu(kvm, collection->target_addr); |
| 1894 | |
| 1895 | irq = vgic_add_lpi(kvm, lpi_id, vcpu); |
| 1896 | if (IS_ERR(irq)) |
| 1897 | return PTR_ERR(irq); |
| 1898 | ite->irq = irq; |
| 1899 | |
| 1900 | return offset; |
| 1901 | } |
| 1902 | |
| 1903 | static int vgic_its_ite_cmp(void *priv, struct list_head *a, |
| 1904 | struct list_head *b) |
| 1905 | { |
| 1906 | struct its_ite *itea = container_of(a, struct its_ite, ite_list); |
| 1907 | struct its_ite *iteb = container_of(b, struct its_ite, ite_list); |
| 1908 | |
| 1909 | if (itea->event_id < iteb->event_id) |
| 1910 | return -1; |
| 1911 | else |
| 1912 | return 1; |
| 1913 | } |
| 1914 | |
Eric Auger | 57a9a11 | 2017-01-09 16:27:07 +0100 | [diff] [blame] | 1915 | static int vgic_its_save_itt(struct vgic_its *its, struct its_device *device) |
| 1916 | { |
Eric Auger | eff484e | 2017-05-03 17:38:01 +0200 | [diff] [blame] | 1917 | const struct vgic_its_abi *abi = vgic_its_get_abi(its); |
| 1918 | gpa_t base = device->itt_addr; |
| 1919 | struct its_ite *ite; |
| 1920 | int ret; |
| 1921 | int ite_esz = abi->ite_esz; |
| 1922 | |
| 1923 | list_sort(NULL, &device->itt_head, vgic_its_ite_cmp); |
| 1924 | |
| 1925 | list_for_each_entry(ite, &device->itt_head, ite_list) { |
| 1926 | gpa_t gpa = base + ite->event_id * ite_esz; |
| 1927 | |
| 1928 | ret = vgic_its_save_ite(its, device, ite, gpa, ite_esz); |
| 1929 | if (ret) |
| 1930 | return ret; |
| 1931 | } |
| 1932 | return 0; |
Eric Auger | 57a9a11 | 2017-01-09 16:27:07 +0100 | [diff] [blame] | 1933 | } |
| 1934 | |
| 1935 | static int vgic_its_restore_itt(struct vgic_its *its, struct its_device *dev) |
| 1936 | { |
Eric Auger | eff484e | 2017-05-03 17:38:01 +0200 | [diff] [blame] | 1937 | const struct vgic_its_abi *abi = vgic_its_get_abi(its); |
| 1938 | gpa_t base = dev->itt_addr; |
| 1939 | int ret; |
| 1940 | int ite_esz = abi->ite_esz; |
| 1941 | size_t max_size = BIT_ULL(dev->num_eventid_bits) * ite_esz; |
| 1942 | |
| 1943 | ret = scan_its_table(its, base, max_size, ite_esz, 0, |
| 1944 | vgic_its_restore_ite, dev); |
| 1945 | |
| 1946 | return ret; |
Eric Auger | 57a9a11 | 2017-01-09 16:27:07 +0100 | [diff] [blame] | 1947 | } |
| 1948 | |
| 1949 | /** |
| 1950 | * vgic_its_save_dte - Save a device table entry at a given GPA |
| 1951 | * |
| 1952 | * @its: ITS handle |
| 1953 | * @dev: ITS device |
| 1954 | * @ptr: GPA |
| 1955 | */ |
| 1956 | static int vgic_its_save_dte(struct vgic_its *its, struct its_device *dev, |
| 1957 | gpa_t ptr, int dte_esz) |
| 1958 | { |
| 1959 | struct kvm *kvm = its->dev->kvm; |
| 1960 | u64 val, itt_addr_field; |
| 1961 | u32 next_offset; |
| 1962 | |
| 1963 | itt_addr_field = dev->itt_addr >> 8; |
| 1964 | next_offset = compute_next_devid_offset(&its->device_list, dev); |
| 1965 | val = (1ULL << KVM_ITS_DTE_VALID_SHIFT | |
| 1966 | ((u64)next_offset << KVM_ITS_DTE_NEXT_SHIFT) | |
| 1967 | (itt_addr_field << KVM_ITS_DTE_ITTADDR_SHIFT) | |
| 1968 | (dev->num_eventid_bits - 1)); |
| 1969 | val = cpu_to_le64(val); |
| 1970 | return kvm_write_guest(kvm, ptr, &val, dte_esz); |
| 1971 | } |
| 1972 | |
| 1973 | /** |
| 1974 | * vgic_its_restore_dte - restore a device table entry |
| 1975 | * |
| 1976 | * @its: its handle |
| 1977 | * @id: device id the DTE corresponds to |
| 1978 | * @ptr: kernel VA where the 8 byte DTE is located |
| 1979 | * @opaque: unused |
| 1980 | * |
| 1981 | * Return: < 0 on error, 0 if the dte is the last one, id offset to the |
| 1982 | * next dte otherwise |
| 1983 | */ |
| 1984 | static int vgic_its_restore_dte(struct vgic_its *its, u32 id, |
| 1985 | void *ptr, void *opaque) |
| 1986 | { |
| 1987 | struct its_device *dev; |
| 1988 | gpa_t itt_addr; |
| 1989 | u8 num_eventid_bits; |
| 1990 | u64 entry = *(u64 *)ptr; |
| 1991 | bool valid; |
| 1992 | u32 offset; |
| 1993 | int ret; |
| 1994 | |
| 1995 | entry = le64_to_cpu(entry); |
| 1996 | |
| 1997 | valid = entry >> KVM_ITS_DTE_VALID_SHIFT; |
| 1998 | num_eventid_bits = (entry & KVM_ITS_DTE_SIZE_MASK) + 1; |
| 1999 | itt_addr = ((entry & KVM_ITS_DTE_ITTADDR_MASK) |
| 2000 | >> KVM_ITS_DTE_ITTADDR_SHIFT) << 8; |
| 2001 | |
| 2002 | if (!valid) |
| 2003 | return 1; |
| 2004 | |
| 2005 | /* dte entry is valid */ |
| 2006 | offset = (entry & KVM_ITS_DTE_NEXT_MASK) >> KVM_ITS_DTE_NEXT_SHIFT; |
| 2007 | |
| 2008 | dev = vgic_its_alloc_device(its, id, itt_addr, num_eventid_bits); |
| 2009 | if (IS_ERR(dev)) |
| 2010 | return PTR_ERR(dev); |
| 2011 | |
| 2012 | ret = vgic_its_restore_itt(its, dev); |
Christoffer Dall | a2b19e6 | 2017-05-08 13:31:12 +0200 | [diff] [blame] | 2013 | if (ret) { |
| 2014 | vgic_its_free_device(its->dev->kvm, dev); |
Eric Auger | 57a9a11 | 2017-01-09 16:27:07 +0100 | [diff] [blame] | 2015 | return ret; |
Christoffer Dall | a2b19e6 | 2017-05-08 13:31:12 +0200 | [diff] [blame] | 2016 | } |
Eric Auger | 57a9a11 | 2017-01-09 16:27:07 +0100 | [diff] [blame] | 2017 | |
| 2018 | return offset; |
| 2019 | } |
| 2020 | |
| 2021 | static int vgic_its_device_cmp(void *priv, struct list_head *a, |
| 2022 | struct list_head *b) |
| 2023 | { |
| 2024 | struct its_device *deva = container_of(a, struct its_device, dev_list); |
| 2025 | struct its_device *devb = container_of(b, struct its_device, dev_list); |
| 2026 | |
| 2027 | if (deva->device_id < devb->device_id) |
| 2028 | return -1; |
| 2029 | else |
| 2030 | return 1; |
| 2031 | } |
| 2032 | |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 2033 | /** |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2034 | * vgic_its_save_device_tables - Save the device table and all ITT |
| 2035 | * into guest RAM |
Eric Auger | 57a9a11 | 2017-01-09 16:27:07 +0100 | [diff] [blame] | 2036 | * |
| 2037 | * L1/L2 handling is hidden by vgic_its_check_id() helper which directly |
| 2038 | * returns the GPA of the device entry |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2039 | */ |
| 2040 | static int vgic_its_save_device_tables(struct vgic_its *its) |
| 2041 | { |
Eric Auger | 57a9a11 | 2017-01-09 16:27:07 +0100 | [diff] [blame] | 2042 | const struct vgic_its_abi *abi = vgic_its_get_abi(its); |
| 2043 | struct its_device *dev; |
| 2044 | int dte_esz = abi->dte_esz; |
| 2045 | u64 baser; |
| 2046 | |
| 2047 | baser = its->baser_device_table; |
| 2048 | |
| 2049 | list_sort(NULL, &its->device_list, vgic_its_device_cmp); |
| 2050 | |
| 2051 | list_for_each_entry(dev, &its->device_list, dev_list) { |
| 2052 | int ret; |
| 2053 | gpa_t eaddr; |
| 2054 | |
| 2055 | if (!vgic_its_check_id(its, baser, |
| 2056 | dev->device_id, &eaddr)) |
| 2057 | return -EINVAL; |
| 2058 | |
| 2059 | ret = vgic_its_save_itt(its, dev); |
| 2060 | if (ret) |
| 2061 | return ret; |
| 2062 | |
| 2063 | ret = vgic_its_save_dte(its, dev, eaddr, dte_esz); |
| 2064 | if (ret) |
| 2065 | return ret; |
| 2066 | } |
| 2067 | return 0; |
| 2068 | } |
| 2069 | |
| 2070 | /** |
| 2071 | * handle_l1_dte - callback used for L1 device table entries (2 stage case) |
| 2072 | * |
| 2073 | * @its: its handle |
| 2074 | * @id: index of the entry in the L1 table |
| 2075 | * @addr: kernel VA |
| 2076 | * @opaque: unused |
| 2077 | * |
| 2078 | * L1 table entries are scanned by steps of 1 entry |
| 2079 | * Return < 0 if error, 0 if last dte was found when scanning the L2 |
| 2080 | * table, +1 otherwise (meaning next L1 entry must be scanned) |
| 2081 | */ |
| 2082 | static int handle_l1_dte(struct vgic_its *its, u32 id, void *addr, |
| 2083 | void *opaque) |
| 2084 | { |
| 2085 | const struct vgic_its_abi *abi = vgic_its_get_abi(its); |
| 2086 | int l2_start_id = id * (SZ_64K / abi->dte_esz); |
| 2087 | u64 entry = *(u64 *)addr; |
| 2088 | int dte_esz = abi->dte_esz; |
| 2089 | gpa_t gpa; |
| 2090 | int ret; |
| 2091 | |
| 2092 | entry = le64_to_cpu(entry); |
| 2093 | |
| 2094 | if (!(entry & KVM_ITS_L1E_VALID_MASK)) |
| 2095 | return 1; |
| 2096 | |
| 2097 | gpa = entry & KVM_ITS_L1E_ADDR_MASK; |
| 2098 | |
| 2099 | ret = scan_its_table(its, gpa, SZ_64K, dte_esz, |
| 2100 | l2_start_id, vgic_its_restore_dte, NULL); |
| 2101 | |
| 2102 | if (ret <= 0) |
| 2103 | return ret; |
| 2104 | |
| 2105 | return 1; |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2106 | } |
| 2107 | |
| 2108 | /** |
| 2109 | * vgic_its_restore_device_tables - Restore the device table and all ITT |
| 2110 | * from guest RAM to internal data structs |
| 2111 | */ |
| 2112 | static int vgic_its_restore_device_tables(struct vgic_its *its) |
| 2113 | { |
Eric Auger | 57a9a11 | 2017-01-09 16:27:07 +0100 | [diff] [blame] | 2114 | const struct vgic_its_abi *abi = vgic_its_get_abi(its); |
| 2115 | u64 baser = its->baser_device_table; |
| 2116 | int l1_esz, ret; |
| 2117 | int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K; |
| 2118 | gpa_t l1_gpa; |
| 2119 | |
| 2120 | if (!(baser & GITS_BASER_VALID)) |
| 2121 | return 0; |
| 2122 | |
| 2123 | l1_gpa = BASER_ADDRESS(baser); |
| 2124 | |
| 2125 | if (baser & GITS_BASER_INDIRECT) { |
| 2126 | l1_esz = GITS_LVL1_ENTRY_SIZE; |
| 2127 | ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0, |
| 2128 | handle_l1_dte, NULL); |
| 2129 | } else { |
| 2130 | l1_esz = abi->dte_esz; |
| 2131 | ret = scan_its_table(its, l1_gpa, l1_tbl_size, l1_esz, 0, |
| 2132 | vgic_its_restore_dte, NULL); |
| 2133 | } |
| 2134 | |
| 2135 | if (ret > 0) |
| 2136 | ret = -EINVAL; |
| 2137 | |
| 2138 | return ret; |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2139 | } |
| 2140 | |
Eric Auger | ea1ad53 | 2017-01-09 16:19:41 +0100 | [diff] [blame] | 2141 | static int vgic_its_save_cte(struct vgic_its *its, |
| 2142 | struct its_collection *collection, |
| 2143 | gpa_t gpa, int esz) |
| 2144 | { |
| 2145 | u64 val; |
| 2146 | |
| 2147 | val = (1ULL << KVM_ITS_CTE_VALID_SHIFT | |
| 2148 | ((u64)collection->target_addr << KVM_ITS_CTE_RDBASE_SHIFT) | |
| 2149 | collection->collection_id); |
| 2150 | val = cpu_to_le64(val); |
| 2151 | return kvm_write_guest(its->dev->kvm, gpa, &val, esz); |
| 2152 | } |
| 2153 | |
| 2154 | static int vgic_its_restore_cte(struct vgic_its *its, gpa_t gpa, int esz) |
| 2155 | { |
| 2156 | struct its_collection *collection; |
| 2157 | struct kvm *kvm = its->dev->kvm; |
| 2158 | u32 target_addr, coll_id; |
| 2159 | u64 val; |
| 2160 | int ret; |
| 2161 | |
| 2162 | BUG_ON(esz > sizeof(val)); |
| 2163 | ret = kvm_read_guest(kvm, gpa, &val, esz); |
| 2164 | if (ret) |
| 2165 | return ret; |
| 2166 | val = le64_to_cpu(val); |
| 2167 | if (!(val & KVM_ITS_CTE_VALID_MASK)) |
| 2168 | return 0; |
| 2169 | |
| 2170 | target_addr = (u32)(val >> KVM_ITS_CTE_RDBASE_SHIFT); |
| 2171 | coll_id = val & KVM_ITS_CTE_ICID_MASK; |
| 2172 | |
| 2173 | if (target_addr >= atomic_read(&kvm->online_vcpus)) |
| 2174 | return -EINVAL; |
| 2175 | |
| 2176 | collection = find_collection(its, coll_id); |
| 2177 | if (collection) |
| 2178 | return -EEXIST; |
| 2179 | ret = vgic_its_alloc_collection(its, &collection, coll_id); |
| 2180 | if (ret) |
| 2181 | return ret; |
| 2182 | collection->target_addr = target_addr; |
| 2183 | return 1; |
| 2184 | } |
| 2185 | |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2186 | /** |
| 2187 | * vgic_its_save_collection_table - Save the collection table into |
| 2188 | * guest RAM |
| 2189 | */ |
| 2190 | static int vgic_its_save_collection_table(struct vgic_its *its) |
| 2191 | { |
Eric Auger | ea1ad53 | 2017-01-09 16:19:41 +0100 | [diff] [blame] | 2192 | const struct vgic_its_abi *abi = vgic_its_get_abi(its); |
| 2193 | struct its_collection *collection; |
| 2194 | u64 val; |
| 2195 | gpa_t gpa; |
| 2196 | size_t max_size, filled = 0; |
| 2197 | int ret, cte_esz = abi->cte_esz; |
| 2198 | |
| 2199 | gpa = BASER_ADDRESS(its->baser_coll_table); |
| 2200 | if (!gpa) |
| 2201 | return 0; |
| 2202 | |
| 2203 | max_size = GITS_BASER_NR_PAGES(its->baser_coll_table) * SZ_64K; |
| 2204 | |
| 2205 | list_for_each_entry(collection, &its->collection_list, coll_list) { |
| 2206 | ret = vgic_its_save_cte(its, collection, gpa, cte_esz); |
| 2207 | if (ret) |
| 2208 | return ret; |
| 2209 | gpa += cte_esz; |
| 2210 | filled += cte_esz; |
| 2211 | } |
| 2212 | |
| 2213 | if (filled == max_size) |
| 2214 | return 0; |
| 2215 | |
| 2216 | /* |
| 2217 | * table is not fully filled, add a last dummy element |
| 2218 | * with valid bit unset |
| 2219 | */ |
| 2220 | val = 0; |
| 2221 | BUG_ON(cte_esz > sizeof(val)); |
| 2222 | ret = kvm_write_guest(its->dev->kvm, gpa, &val, cte_esz); |
| 2223 | return ret; |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2224 | } |
| 2225 | |
| 2226 | /** |
| 2227 | * vgic_its_restore_collection_table - reads the collection table |
| 2228 | * in guest memory and restores the ITS internal state. Requires the |
| 2229 | * BASER registers to be restored before. |
| 2230 | */ |
| 2231 | static int vgic_its_restore_collection_table(struct vgic_its *its) |
| 2232 | { |
Eric Auger | ea1ad53 | 2017-01-09 16:19:41 +0100 | [diff] [blame] | 2233 | const struct vgic_its_abi *abi = vgic_its_get_abi(its); |
| 2234 | int cte_esz = abi->cte_esz; |
| 2235 | size_t max_size, read = 0; |
| 2236 | gpa_t gpa; |
| 2237 | int ret; |
| 2238 | |
| 2239 | if (!(its->baser_coll_table & GITS_BASER_VALID)) |
| 2240 | return 0; |
| 2241 | |
| 2242 | gpa = BASER_ADDRESS(its->baser_coll_table); |
| 2243 | |
| 2244 | max_size = GITS_BASER_NR_PAGES(its->baser_coll_table) * SZ_64K; |
| 2245 | |
| 2246 | while (read < max_size) { |
| 2247 | ret = vgic_its_restore_cte(its, gpa, cte_esz); |
| 2248 | if (ret <= 0) |
| 2249 | break; |
| 2250 | gpa += cte_esz; |
| 2251 | read += cte_esz; |
| 2252 | } |
| 2253 | return ret; |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | /** |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 2257 | * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM |
| 2258 | * according to v0 ABI |
| 2259 | */ |
| 2260 | static int vgic_its_save_tables_v0(struct vgic_its *its) |
| 2261 | { |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2262 | struct kvm *kvm = its->dev->kvm; |
| 2263 | int ret; |
| 2264 | |
| 2265 | mutex_lock(&kvm->lock); |
| 2266 | mutex_lock(&its->its_lock); |
| 2267 | |
| 2268 | if (!lock_all_vcpus(kvm)) { |
| 2269 | mutex_unlock(&its->its_lock); |
| 2270 | mutex_unlock(&kvm->lock); |
| 2271 | return -EBUSY; |
| 2272 | } |
| 2273 | |
| 2274 | ret = vgic_its_save_device_tables(its); |
| 2275 | if (ret) |
| 2276 | goto out; |
| 2277 | |
| 2278 | ret = vgic_its_save_collection_table(its); |
| 2279 | |
| 2280 | out: |
| 2281 | unlock_all_vcpus(kvm); |
| 2282 | mutex_unlock(&its->its_lock); |
| 2283 | mutex_unlock(&kvm->lock); |
| 2284 | return ret; |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 2285 | } |
| 2286 | |
| 2287 | /** |
| 2288 | * vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM |
| 2289 | * to internal data structs according to V0 ABI |
| 2290 | * |
| 2291 | */ |
| 2292 | static int vgic_its_restore_tables_v0(struct vgic_its *its) |
| 2293 | { |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2294 | struct kvm *kvm = its->dev->kvm; |
| 2295 | int ret; |
| 2296 | |
| 2297 | mutex_lock(&kvm->lock); |
| 2298 | mutex_lock(&its->its_lock); |
| 2299 | |
| 2300 | if (!lock_all_vcpus(kvm)) { |
| 2301 | mutex_unlock(&its->its_lock); |
| 2302 | mutex_unlock(&kvm->lock); |
| 2303 | return -EBUSY; |
| 2304 | } |
| 2305 | |
| 2306 | ret = vgic_its_restore_collection_table(its); |
| 2307 | if (ret) |
| 2308 | goto out; |
| 2309 | |
| 2310 | ret = vgic_its_restore_device_tables(its); |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2311 | out: |
| 2312 | unlock_all_vcpus(kvm); |
| 2313 | mutex_unlock(&its->its_lock); |
| 2314 | mutex_unlock(&kvm->lock); |
| 2315 | |
Christoffer Dall | 67723c2 | 2017-05-08 14:43:45 +0200 | [diff] [blame] | 2316 | return ret; |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame] | 2317 | } |
| 2318 | |
| 2319 | static int vgic_its_commit_v0(struct vgic_its *its) |
| 2320 | { |
| 2321 | const struct vgic_its_abi *abi; |
| 2322 | |
| 2323 | abi = vgic_its_get_abi(its); |
| 2324 | its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK; |
| 2325 | its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK; |
| 2326 | |
| 2327 | its->baser_coll_table |= (GIC_ENCODE_SZ(abi->cte_esz, 5) |
| 2328 | << GITS_BASER_ENTRY_SIZE_SHIFT); |
| 2329 | |
| 2330 | its->baser_device_table |= (GIC_ENCODE_SZ(abi->dte_esz, 5) |
| 2331 | << GITS_BASER_ENTRY_SIZE_SHIFT); |
| 2332 | return 0; |
| 2333 | } |
| 2334 | |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 2335 | static int vgic_its_has_attr(struct kvm_device *dev, |
| 2336 | struct kvm_device_attr *attr) |
| 2337 | { |
| 2338 | switch (attr->group) { |
| 2339 | case KVM_DEV_ARM_VGIC_GRP_ADDR: |
| 2340 | switch (attr->attr) { |
| 2341 | case KVM_VGIC_ITS_ADDR_TYPE: |
| 2342 | return 0; |
| 2343 | } |
| 2344 | break; |
| 2345 | case KVM_DEV_ARM_VGIC_GRP_CTRL: |
| 2346 | switch (attr->attr) { |
| 2347 | case KVM_DEV_ARM_VGIC_CTRL_INIT: |
| 2348 | return 0; |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2349 | case KVM_DEV_ARM_ITS_SAVE_TABLES: |
| 2350 | return 0; |
| 2351 | case KVM_DEV_ARM_ITS_RESTORE_TABLES: |
| 2352 | return 0; |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 2353 | } |
| 2354 | break; |
Eric Auger | 876ae23 | 2016-12-20 01:36:35 -0500 | [diff] [blame] | 2355 | case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: |
| 2356 | return vgic_its_has_attr_regs(dev, attr); |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 2357 | } |
| 2358 | return -ENXIO; |
| 2359 | } |
| 2360 | |
| 2361 | static int vgic_its_set_attr(struct kvm_device *dev, |
| 2362 | struct kvm_device_attr *attr) |
| 2363 | { |
| 2364 | struct vgic_its *its = dev->private; |
| 2365 | int ret; |
| 2366 | |
| 2367 | switch (attr->group) { |
| 2368 | case KVM_DEV_ARM_VGIC_GRP_ADDR: { |
| 2369 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; |
| 2370 | unsigned long type = (unsigned long)attr->attr; |
| 2371 | u64 addr; |
| 2372 | |
| 2373 | if (type != KVM_VGIC_ITS_ADDR_TYPE) |
| 2374 | return -ENODEV; |
| 2375 | |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 2376 | if (copy_from_user(&addr, uaddr, sizeof(addr))) |
| 2377 | return -EFAULT; |
| 2378 | |
| 2379 | ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base, |
| 2380 | addr, SZ_64K); |
| 2381 | if (ret) |
| 2382 | return ret; |
| 2383 | |
Christoffer Dall | 30e1b68 | 2017-05-08 13:14:57 +0200 | [diff] [blame] | 2384 | return vgic_register_its_iodev(dev->kvm, its, addr); |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 2385 | } |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2386 | case KVM_DEV_ARM_VGIC_GRP_CTRL: { |
| 2387 | const struct vgic_its_abi *abi = vgic_its_get_abi(its); |
| 2388 | |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 2389 | switch (attr->attr) { |
| 2390 | case KVM_DEV_ARM_VGIC_CTRL_INIT: |
Marc Zyngier | 6cc40f2 | 2017-05-08 18:15:40 +0100 | [diff] [blame] | 2391 | /* Nothing to do */ |
Andre Przywara | c773576 | 2016-08-08 16:45:43 +0100 | [diff] [blame] | 2392 | return 0; |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2393 | case KVM_DEV_ARM_ITS_SAVE_TABLES: |
| 2394 | return abi->save_tables(its); |
| 2395 | case KVM_DEV_ARM_ITS_RESTORE_TABLES: |
| 2396 | return abi->restore_tables(its); |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 2397 | } |
Eric Auger | 3b65808 | 2016-12-24 18:48:04 +0100 | [diff] [blame] | 2398 | } |
Eric Auger | 876ae23 | 2016-12-20 01:36:35 -0500 | [diff] [blame] | 2399 | case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: { |
| 2400 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; |
| 2401 | u64 reg; |
| 2402 | |
| 2403 | if (get_user(reg, uaddr)) |
| 2404 | return -EFAULT; |
| 2405 | |
| 2406 | return vgic_its_attr_regs_access(dev, attr, ®, true); |
| 2407 | } |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 2408 | } |
| 2409 | return -ENXIO; |
| 2410 | } |
| 2411 | |
| 2412 | static int vgic_its_get_attr(struct kvm_device *dev, |
| 2413 | struct kvm_device_attr *attr) |
| 2414 | { |
| 2415 | switch (attr->group) { |
| 2416 | case KVM_DEV_ARM_VGIC_GRP_ADDR: { |
| 2417 | struct vgic_its *its = dev->private; |
| 2418 | u64 addr = its->vgic_its_base; |
| 2419 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; |
| 2420 | unsigned long type = (unsigned long)attr->attr; |
| 2421 | |
| 2422 | if (type != KVM_VGIC_ITS_ADDR_TYPE) |
| 2423 | return -ENODEV; |
| 2424 | |
| 2425 | if (copy_to_user(uaddr, &addr, sizeof(addr))) |
| 2426 | return -EFAULT; |
| 2427 | break; |
Eric Auger | 876ae23 | 2016-12-20 01:36:35 -0500 | [diff] [blame] | 2428 | } |
| 2429 | case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: { |
| 2430 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; |
| 2431 | u64 reg; |
| 2432 | int ret; |
| 2433 | |
| 2434 | ret = vgic_its_attr_regs_access(dev, attr, ®, false); |
| 2435 | if (ret) |
| 2436 | return ret; |
| 2437 | return put_user(reg, uaddr); |
| 2438 | } |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 2439 | default: |
| 2440 | return -ENXIO; |
| 2441 | } |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 2442 | |
| 2443 | return 0; |
| 2444 | } |
| 2445 | |
| 2446 | static struct kvm_device_ops kvm_arm_vgic_its_ops = { |
| 2447 | .name = "kvm-arm-vgic-its", |
| 2448 | .create = vgic_its_create, |
| 2449 | .destroy = vgic_its_destroy, |
| 2450 | .set_attr = vgic_its_set_attr, |
| 2451 | .get_attr = vgic_its_get_attr, |
| 2452 | .has_attr = vgic_its_has_attr, |
| 2453 | }; |
| 2454 | |
| 2455 | int kvm_vgic_register_its_device(void) |
| 2456 | { |
| 2457 | return kvm_register_device_ops(&kvm_arm_vgic_its_ops, |
| 2458 | KVM_DEV_TYPE_ARM_VGIC_ITS); |
| 2459 | } |