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> |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 26 | |
| 27 | #include <linux/irqchip/arm-gic-v3.h> |
| 28 | |
| 29 | #include <asm/kvm_emulate.h> |
| 30 | #include <asm/kvm_arm.h> |
| 31 | #include <asm/kvm_mmu.h> |
| 32 | |
| 33 | #include "vgic.h" |
| 34 | #include "vgic-mmio.h" |
| 35 | |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame^] | 36 | static int vgic_its_save_tables_v0(struct vgic_its *its); |
| 37 | static int vgic_its_restore_tables_v0(struct vgic_its *its); |
| 38 | static int vgic_its_commit_v0(struct vgic_its *its); |
| 39 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 40 | /* |
| 41 | * Creates a new (reference to a) struct vgic_irq for a given LPI. |
| 42 | * If this LPI is already mapped on another ITS, we increase its refcount |
| 43 | * and return a pointer to the existing structure. |
| 44 | * If this is a "new" LPI, we allocate and initialize a new struct vgic_irq. |
| 45 | * This function returns a pointer to the _unlocked_ structure. |
| 46 | */ |
| 47 | static struct vgic_irq *vgic_add_lpi(struct kvm *kvm, u32 intid) |
| 48 | { |
| 49 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 50 | struct vgic_irq *irq = vgic_get_irq(kvm, NULL, intid), *oldirq; |
| 51 | |
| 52 | /* In this case there is no put, since we keep the reference. */ |
| 53 | if (irq) |
| 54 | return irq; |
| 55 | |
| 56 | irq = kzalloc(sizeof(struct vgic_irq), GFP_KERNEL); |
| 57 | if (!irq) |
Christoffer Dall | 99e5e88 | 2016-08-01 20:25:33 +0200 | [diff] [blame] | 58 | return ERR_PTR(-ENOMEM); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 59 | |
| 60 | INIT_LIST_HEAD(&irq->lpi_list); |
| 61 | INIT_LIST_HEAD(&irq->ap_list); |
| 62 | spin_lock_init(&irq->irq_lock); |
| 63 | |
| 64 | irq->config = VGIC_CONFIG_EDGE; |
| 65 | kref_init(&irq->refcount); |
| 66 | irq->intid = intid; |
| 67 | |
| 68 | spin_lock(&dist->lpi_list_lock); |
| 69 | |
| 70 | /* |
| 71 | * There could be a race with another vgic_add_lpi(), so we need to |
| 72 | * check that we don't add a second list entry with the same LPI. |
| 73 | */ |
| 74 | list_for_each_entry(oldirq, &dist->lpi_list_head, lpi_list) { |
| 75 | if (oldirq->intid != intid) |
| 76 | continue; |
| 77 | |
| 78 | /* Someone was faster with adding this LPI, lets use that. */ |
| 79 | kfree(irq); |
| 80 | irq = oldirq; |
| 81 | |
| 82 | /* |
| 83 | * This increases the refcount, the caller is expected to |
| 84 | * call vgic_put_irq() on the returned pointer once it's |
| 85 | * finished with the IRQ. |
| 86 | */ |
Marc Zyngier | d97594e | 2016-07-17 11:27:23 +0100 | [diff] [blame] | 87 | vgic_get_irq_kref(irq); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 88 | |
| 89 | goto out_unlock; |
| 90 | } |
| 91 | |
| 92 | list_add_tail(&irq->lpi_list, &dist->lpi_list_head); |
| 93 | dist->lpi_list_count++; |
| 94 | |
| 95 | out_unlock: |
| 96 | spin_unlock(&dist->lpi_list_lock); |
| 97 | |
| 98 | return irq; |
| 99 | } |
| 100 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 101 | struct its_device { |
| 102 | struct list_head dev_list; |
| 103 | |
| 104 | /* the head for the list of ITTEs */ |
| 105 | struct list_head itt_head; |
| 106 | u32 device_id; |
| 107 | }; |
| 108 | |
| 109 | #define COLLECTION_NOT_MAPPED ((u32)~0) |
| 110 | |
| 111 | struct its_collection { |
| 112 | struct list_head coll_list; |
| 113 | |
| 114 | u32 collection_id; |
| 115 | u32 target_addr; |
| 116 | }; |
| 117 | |
| 118 | #define its_is_collection_mapped(coll) ((coll) && \ |
| 119 | ((coll)->target_addr != COLLECTION_NOT_MAPPED)) |
| 120 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 121 | struct its_ite { |
| 122 | struct list_head ite_list; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 123 | |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 124 | struct vgic_irq *irq; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 125 | struct its_collection *collection; |
| 126 | u32 lpi; |
| 127 | u32 event_id; |
| 128 | }; |
| 129 | |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame^] | 130 | /** |
| 131 | * struct vgic_its_abi - ITS abi ops and settings |
| 132 | * @cte_esz: collection table entry size |
| 133 | * @dte_esz: device table entry size |
| 134 | * @ite_esz: interrupt translation table entry size |
| 135 | * @save tables: save the ITS tables into guest RAM |
| 136 | * @restore_tables: restore the ITS internal structs from tables |
| 137 | * stored in guest RAM |
| 138 | * @commit: initialize the registers which expose the ABI settings, |
| 139 | * especially the entry sizes |
| 140 | */ |
| 141 | struct vgic_its_abi { |
| 142 | int cte_esz; |
| 143 | int dte_esz; |
| 144 | int ite_esz; |
| 145 | int (*save_tables)(struct vgic_its *its); |
| 146 | int (*restore_tables)(struct vgic_its *its); |
| 147 | int (*commit)(struct vgic_its *its); |
| 148 | }; |
| 149 | |
| 150 | static const struct vgic_its_abi its_table_abi_versions[] = { |
| 151 | [0] = {.cte_esz = 8, .dte_esz = 8, .ite_esz = 8, |
| 152 | .save_tables = vgic_its_save_tables_v0, |
| 153 | .restore_tables = vgic_its_restore_tables_v0, |
| 154 | .commit = vgic_its_commit_v0, |
| 155 | }, |
| 156 | }; |
| 157 | |
| 158 | #define NR_ITS_ABIS ARRAY_SIZE(its_table_abi_versions) |
| 159 | |
| 160 | inline const struct vgic_its_abi *vgic_its_get_abi(struct vgic_its *its) |
| 161 | { |
| 162 | return &its_table_abi_versions[its->abi_rev]; |
| 163 | } |
| 164 | |
| 165 | int vgic_its_set_abi(struct vgic_its *its, int rev) |
| 166 | { |
| 167 | const struct vgic_its_abi *abi; |
| 168 | |
| 169 | its->abi_rev = rev; |
| 170 | abi = vgic_its_get_abi(its); |
| 171 | return abi->commit(its); |
| 172 | } |
| 173 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 174 | /* |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 175 | * Find and returns a device in the device table for an ITS. |
| 176 | * Must be called with the its_lock mutex held. |
| 177 | */ |
| 178 | static struct its_device *find_its_device(struct vgic_its *its, u32 device_id) |
| 179 | { |
| 180 | struct its_device *device; |
| 181 | |
| 182 | list_for_each_entry(device, &its->device_list, dev_list) |
| 183 | if (device_id == device->device_id) |
| 184 | return device; |
| 185 | |
| 186 | return NULL; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * Find and returns an interrupt translation table entry (ITTE) for a given |
| 191 | * Device ID/Event ID pair on an ITS. |
| 192 | * Must be called with the its_lock mutex held. |
| 193 | */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 194 | 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] | 195 | u32 event_id) |
| 196 | { |
| 197 | struct its_device *device; |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 198 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 199 | |
| 200 | device = find_its_device(its, device_id); |
| 201 | if (device == NULL) |
| 202 | return NULL; |
| 203 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 204 | list_for_each_entry(ite, &device->itt_head, ite_list) |
| 205 | if (ite->event_id == event_id) |
| 206 | return ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 207 | |
| 208 | return NULL; |
| 209 | } |
| 210 | |
| 211 | /* To be used as an iterator this macro misses the enclosing parentheses */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 212 | #define for_each_lpi_its(dev, ite, its) \ |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 213 | list_for_each_entry(dev, &(its)->device_list, dev_list) \ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 214 | list_for_each_entry(ite, &(dev)->itt_head, ite_list) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 215 | |
| 216 | /* |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 217 | * We only implement 48 bits of PA at the moment, although the ITS |
| 218 | * supports more. Let's be restrictive here. |
| 219 | */ |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 220 | #define BASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16)) |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 221 | #define CBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12)) |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 222 | #define PENDBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 16)) |
Andre Przywara | f9f77af | 2016-07-15 12:43:35 +0100 | [diff] [blame] | 223 | #define PROPBASER_ADDRESS(x) ((x) & GENMASK_ULL(47, 12)) |
| 224 | |
| 225 | #define GIC_LPI_OFFSET 8192 |
| 226 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 227 | /* |
| 228 | * Finds and returns a collection in the ITS collection table. |
| 229 | * Must be called with the its_lock mutex held. |
| 230 | */ |
| 231 | static struct its_collection *find_collection(struct vgic_its *its, int coll_id) |
| 232 | { |
| 233 | struct its_collection *collection; |
| 234 | |
| 235 | list_for_each_entry(collection, &its->collection_list, coll_list) { |
| 236 | if (coll_id == collection->collection_id) |
| 237 | return collection; |
| 238 | } |
| 239 | |
| 240 | return NULL; |
| 241 | } |
| 242 | |
Andre Przywara | f9f77af | 2016-07-15 12:43:35 +0100 | [diff] [blame] | 243 | #define LPI_PROP_ENABLE_BIT(p) ((p) & LPI_PROP_ENABLED) |
| 244 | #define LPI_PROP_PRIORITY(p) ((p) & 0xfc) |
| 245 | |
| 246 | /* |
| 247 | * Reads the configuration data for a given LPI from guest memory and |
| 248 | * updates the fields in struct vgic_irq. |
| 249 | * If filter_vcpu is not NULL, applies only if the IRQ is targeting this |
| 250 | * VCPU. Unconditionally applies if filter_vcpu is NULL. |
| 251 | */ |
| 252 | static int update_lpi_config(struct kvm *kvm, struct vgic_irq *irq, |
| 253 | struct kvm_vcpu *filter_vcpu) |
| 254 | { |
| 255 | u64 propbase = PROPBASER_ADDRESS(kvm->arch.vgic.propbaser); |
| 256 | u8 prop; |
| 257 | int ret; |
| 258 | |
| 259 | ret = kvm_read_guest(kvm, propbase + irq->intid - GIC_LPI_OFFSET, |
| 260 | &prop, 1); |
| 261 | |
| 262 | if (ret) |
| 263 | return ret; |
| 264 | |
| 265 | spin_lock(&irq->irq_lock); |
| 266 | |
| 267 | if (!filter_vcpu || filter_vcpu == irq->target_vcpu) { |
| 268 | irq->priority = LPI_PROP_PRIORITY(prop); |
| 269 | irq->enabled = LPI_PROP_ENABLE_BIT(prop); |
| 270 | |
| 271 | vgic_queue_irq_unlock(kvm, irq); |
| 272 | } else { |
| 273 | spin_unlock(&irq->irq_lock); |
| 274 | } |
| 275 | |
| 276 | return 0; |
| 277 | } |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 278 | |
| 279 | /* |
| 280 | * Create a snapshot of the current LPI list, so that we can enumerate all |
| 281 | * LPIs without holding any lock. |
| 282 | * Returns the array length and puts the kmalloc'ed array into intid_ptr. |
| 283 | */ |
| 284 | static int vgic_copy_lpi_list(struct kvm *kvm, u32 **intid_ptr) |
| 285 | { |
| 286 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 287 | struct vgic_irq *irq; |
| 288 | u32 *intids; |
| 289 | int irq_count = dist->lpi_list_count, i = 0; |
| 290 | |
| 291 | /* |
| 292 | * We use the current value of the list length, which may change |
| 293 | * after the kmalloc. We don't care, because the guest shouldn't |
| 294 | * change anything while the command handling is still running, |
| 295 | * and in the worst case we would miss a new IRQ, which one wouldn't |
| 296 | * expect to be covered by this command anyway. |
| 297 | */ |
| 298 | intids = kmalloc_array(irq_count, sizeof(intids[0]), GFP_KERNEL); |
| 299 | if (!intids) |
| 300 | return -ENOMEM; |
| 301 | |
| 302 | spin_lock(&dist->lpi_list_lock); |
| 303 | list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { |
| 304 | /* We don't need to "get" the IRQ, as we hold the list lock. */ |
| 305 | intids[i] = irq->intid; |
| 306 | if (++i == irq_count) |
| 307 | break; |
| 308 | } |
| 309 | spin_unlock(&dist->lpi_list_lock); |
| 310 | |
| 311 | *intid_ptr = intids; |
| 312 | return irq_count; |
| 313 | } |
| 314 | |
| 315 | /* |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 316 | * Promotes the ITS view of affinity of an ITTE (which redistributor this LPI |
| 317 | * is targeting) to the VGIC's view, which deals with target VCPUs. |
| 318 | * Needs to be called whenever either the collection for a LPIs has |
| 319 | * changed or the collection itself got retargeted. |
| 320 | */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 321 | static void update_affinity_ite(struct kvm *kvm, struct its_ite *ite) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 322 | { |
| 323 | struct kvm_vcpu *vcpu; |
| 324 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 325 | if (!its_is_collection_mapped(ite->collection)) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 326 | return; |
| 327 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 328 | vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 329 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 330 | spin_lock(&ite->irq->irq_lock); |
| 331 | ite->irq->target_vcpu = vcpu; |
| 332 | spin_unlock(&ite->irq->irq_lock); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | /* |
| 336 | * Updates the target VCPU for every LPI targeting this collection. |
| 337 | * Must be called with the its_lock mutex held. |
| 338 | */ |
| 339 | static void update_affinity_collection(struct kvm *kvm, struct vgic_its *its, |
| 340 | struct its_collection *coll) |
| 341 | { |
| 342 | struct its_device *device; |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 343 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 344 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 345 | for_each_lpi_its(device, ite, its) { |
| 346 | if (!ite->collection || coll != ite->collection) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 347 | continue; |
| 348 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 349 | update_affinity_ite(kvm, ite); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | static u32 max_lpis_propbaser(u64 propbaser) |
| 354 | { |
| 355 | int nr_idbits = (propbaser & 0x1f) + 1; |
| 356 | |
| 357 | return 1U << min(nr_idbits, INTERRUPT_ID_BITS_ITS); |
| 358 | } |
| 359 | |
| 360 | /* |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 361 | * Scan the whole LPI pending table and sync the pending bit in there |
| 362 | * with our own data structures. This relies on the LPI being |
| 363 | * mapped before. |
| 364 | */ |
| 365 | static int its_sync_lpi_pending_table(struct kvm_vcpu *vcpu) |
| 366 | { |
| 367 | gpa_t pendbase = PENDBASER_ADDRESS(vcpu->arch.vgic_cpu.pendbaser); |
| 368 | struct vgic_irq *irq; |
| 369 | int last_byte_offset = -1; |
| 370 | int ret = 0; |
| 371 | u32 *intids; |
| 372 | int nr_irqs, i; |
| 373 | |
| 374 | nr_irqs = vgic_copy_lpi_list(vcpu->kvm, &intids); |
| 375 | if (nr_irqs < 0) |
| 376 | return nr_irqs; |
| 377 | |
| 378 | for (i = 0; i < nr_irqs; i++) { |
| 379 | int byte_offset, bit_nr; |
| 380 | u8 pendmask; |
| 381 | |
| 382 | byte_offset = intids[i] / BITS_PER_BYTE; |
| 383 | bit_nr = intids[i] % BITS_PER_BYTE; |
| 384 | |
| 385 | /* |
| 386 | * For contiguously allocated LPIs chances are we just read |
| 387 | * this very same byte in the last iteration. Reuse that. |
| 388 | */ |
| 389 | if (byte_offset != last_byte_offset) { |
| 390 | ret = kvm_read_guest(vcpu->kvm, pendbase + byte_offset, |
| 391 | &pendmask, 1); |
| 392 | if (ret) { |
| 393 | kfree(intids); |
| 394 | return ret; |
| 395 | } |
| 396 | last_byte_offset = byte_offset; |
| 397 | } |
| 398 | |
| 399 | irq = vgic_get_irq(vcpu->kvm, NULL, intids[i]); |
| 400 | spin_lock(&irq->irq_lock); |
Christoffer Dall | 8694e4d | 2017-01-23 14:07:18 +0100 | [diff] [blame] | 401 | irq->pending_latch = pendmask & (1U << bit_nr); |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 402 | vgic_queue_irq_unlock(vcpu->kvm, irq); |
| 403 | vgic_put_irq(vcpu->kvm, irq); |
| 404 | } |
| 405 | |
| 406 | kfree(intids); |
| 407 | |
| 408 | return ret; |
| 409 | } |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 410 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 411 | static unsigned long vgic_mmio_read_its_typer(struct kvm *kvm, |
| 412 | struct vgic_its *its, |
| 413 | gpa_t addr, unsigned int len) |
| 414 | { |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame^] | 415 | const struct vgic_its_abi *abi = vgic_its_get_abi(its); |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 416 | u64 reg = GITS_TYPER_PLPIS; |
| 417 | |
| 418 | /* |
| 419 | * We use linear CPU numbers for redistributor addressing, |
| 420 | * so GITS_TYPER.PTA is 0. |
| 421 | * Also we force all PROPBASER registers to be the same, so |
| 422 | * CommonLPIAff is 0 as well. |
| 423 | * To avoid memory waste in the guest, we keep the number of IDBits and |
| 424 | * DevBits low - as least for the time being. |
| 425 | */ |
| 426 | reg |= 0x0f << GITS_TYPER_DEVBITS_SHIFT; |
| 427 | reg |= 0x0f << GITS_TYPER_IDBITS_SHIFT; |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame^] | 428 | 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] | 429 | |
| 430 | return extract_bytes(reg, addr & 7, len); |
| 431 | } |
| 432 | |
| 433 | static unsigned long vgic_mmio_read_its_iidr(struct kvm *kvm, |
| 434 | struct vgic_its *its, |
| 435 | gpa_t addr, unsigned int len) |
| 436 | { |
| 437 | return (PRODUCT_ID_KVM << 24) | (IMPLEMENTER_ARM << 0); |
| 438 | } |
| 439 | |
| 440 | static unsigned long vgic_mmio_read_its_idregs(struct kvm *kvm, |
| 441 | struct vgic_its *its, |
| 442 | gpa_t addr, unsigned int len) |
| 443 | { |
| 444 | switch (addr & 0xffff) { |
| 445 | case GITS_PIDR0: |
| 446 | return 0x92; /* part number, bits[7:0] */ |
| 447 | case GITS_PIDR1: |
| 448 | return 0xb4; /* part number, bits[11:8] */ |
| 449 | case GITS_PIDR2: |
| 450 | return GIC_PIDR2_ARCH_GICv3 | 0x0b; |
| 451 | case GITS_PIDR4: |
| 452 | return 0x40; /* This is a 64K software visible page */ |
| 453 | /* The following are the ID registers for (any) GIC. */ |
| 454 | case GITS_CIDR0: |
| 455 | return 0x0d; |
| 456 | case GITS_CIDR1: |
| 457 | return 0xf0; |
| 458 | case GITS_CIDR2: |
| 459 | return 0x05; |
| 460 | case GITS_CIDR3: |
| 461 | return 0xb1; |
| 462 | } |
| 463 | |
| 464 | return 0; |
| 465 | } |
| 466 | |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 467 | /* |
| 468 | * Find the target VCPU and the LPI number for a given devid/eventid pair |
| 469 | * and make this IRQ pending, possibly injecting it. |
| 470 | * Must be called with the its_lock mutex held. |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 471 | * Returns 0 on success, a positive error value for any ITS mapping |
| 472 | * related errors and negative error values for generic errors. |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 473 | */ |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 474 | static int vgic_its_trigger_msi(struct kvm *kvm, struct vgic_its *its, |
| 475 | u32 devid, u32 eventid) |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 476 | { |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 477 | struct kvm_vcpu *vcpu; |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 478 | struct its_ite *ite; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 479 | |
| 480 | if (!its->enabled) |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 481 | return -EBUSY; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 482 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 483 | ite = find_ite(its, devid, eventid); |
| 484 | if (!ite || !its_is_collection_mapped(ite->collection)) |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 485 | return E_ITS_INT_UNMAPPED_INTERRUPT; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 486 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 487 | vcpu = kvm_get_vcpu(kvm, ite->collection->target_addr); |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 488 | if (!vcpu) |
| 489 | return E_ITS_INT_UNMAPPED_INTERRUPT; |
| 490 | |
| 491 | if (!vcpu->arch.vgic_cpu.lpis_enabled) |
| 492 | return -EBUSY; |
| 493 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 494 | spin_lock(&ite->irq->irq_lock); |
| 495 | ite->irq->pending_latch = true; |
| 496 | vgic_queue_irq_unlock(kvm, ite->irq); |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 497 | |
| 498 | return 0; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 499 | } |
| 500 | |
Andre Przywara | 505a19e | 2016-08-09 10:54:29 +0100 | [diff] [blame] | 501 | static struct vgic_io_device *vgic_get_its_iodev(struct kvm_io_device *dev) |
| 502 | { |
| 503 | struct vgic_io_device *iodev; |
| 504 | |
| 505 | if (dev->ops != &kvm_io_gic_ops) |
| 506 | return NULL; |
| 507 | |
| 508 | iodev = container_of(dev, struct vgic_io_device, dev); |
| 509 | |
| 510 | if (iodev->iodev_type != IODEV_ITS) |
| 511 | return NULL; |
| 512 | |
| 513 | return iodev; |
| 514 | } |
| 515 | |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 516 | /* |
| 517 | * Queries the KVM IO bus framework to get the ITS pointer from the given |
| 518 | * doorbell address. |
| 519 | * We then call vgic_its_trigger_msi() with the decoded data. |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 520 | * According to the KVM_SIGNAL_MSI API description returns 1 on success. |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 521 | */ |
| 522 | int vgic_its_inject_msi(struct kvm *kvm, struct kvm_msi *msi) |
| 523 | { |
| 524 | u64 address; |
| 525 | struct kvm_io_device *kvm_io_dev; |
| 526 | struct vgic_io_device *iodev; |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 527 | int ret; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 528 | |
| 529 | if (!vgic_has_its(kvm)) |
| 530 | return -ENODEV; |
| 531 | |
| 532 | if (!(msi->flags & KVM_MSI_VALID_DEVID)) |
| 533 | return -EINVAL; |
| 534 | |
| 535 | address = (u64)msi->address_hi << 32 | msi->address_lo; |
| 536 | |
| 537 | kvm_io_dev = kvm_io_bus_get_dev(kvm, KVM_MMIO_BUS, address); |
| 538 | if (!kvm_io_dev) |
Andre Przywara | 505a19e | 2016-08-09 10:54:29 +0100 | [diff] [blame] | 539 | return -EINVAL; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 540 | |
Andre Przywara | 505a19e | 2016-08-09 10:54:29 +0100 | [diff] [blame] | 541 | iodev = vgic_get_its_iodev(kvm_io_dev); |
| 542 | if (!iodev) |
| 543 | return -EINVAL; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 544 | |
| 545 | mutex_lock(&iodev->its->its_lock); |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 546 | ret = vgic_its_trigger_msi(kvm, iodev->its, msi->devid, msi->data); |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 547 | mutex_unlock(&iodev->its->its_lock); |
| 548 | |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 549 | if (ret < 0) |
| 550 | return ret; |
| 551 | |
| 552 | /* |
| 553 | * KVM_SIGNAL_MSI demands a return value > 0 for success and 0 |
| 554 | * if the guest has blocked the MSI. So we map any LPI mapping |
| 555 | * related error to that. |
| 556 | */ |
| 557 | if (ret) |
| 558 | return 0; |
| 559 | else |
| 560 | return 1; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 561 | } |
| 562 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 563 | /* Requires the its_lock to be held. */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 564 | static void its_free_ite(struct kvm *kvm, struct its_ite *ite) |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 565 | { |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 566 | list_del(&ite->ite_list); |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 567 | |
| 568 | /* This put matches the get in vgic_add_lpi. */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 569 | if (ite->irq) |
| 570 | vgic_put_irq(kvm, ite->irq); |
Andre Przywara | 3802411 | 2016-07-15 12:43:33 +0100 | [diff] [blame] | 571 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 572 | kfree(ite); |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 573 | } |
| 574 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 575 | static u64 its_cmd_mask_field(u64 *its_cmd, int word, int shift, int size) |
| 576 | { |
| 577 | return (le64_to_cpu(its_cmd[word]) >> shift) & (BIT_ULL(size) - 1); |
| 578 | } |
| 579 | |
| 580 | #define its_cmd_get_command(cmd) its_cmd_mask_field(cmd, 0, 0, 8) |
| 581 | #define its_cmd_get_deviceid(cmd) its_cmd_mask_field(cmd, 0, 32, 32) |
| 582 | #define its_cmd_get_id(cmd) its_cmd_mask_field(cmd, 1, 0, 32) |
| 583 | #define its_cmd_get_physical_id(cmd) its_cmd_mask_field(cmd, 1, 32, 32) |
| 584 | #define its_cmd_get_collection(cmd) its_cmd_mask_field(cmd, 2, 0, 16) |
| 585 | #define its_cmd_get_target_addr(cmd) its_cmd_mask_field(cmd, 2, 16, 32) |
| 586 | #define its_cmd_get_validbit(cmd) its_cmd_mask_field(cmd, 2, 63, 1) |
| 587 | |
| 588 | /* |
| 589 | * The DISCARD command frees an Interrupt Translation Table Entry (ITTE). |
| 590 | * Must be called with the its_lock mutex held. |
| 591 | */ |
| 592 | static int vgic_its_cmd_handle_discard(struct kvm *kvm, struct vgic_its *its, |
| 593 | u64 *its_cmd) |
| 594 | { |
| 595 | u32 device_id = its_cmd_get_deviceid(its_cmd); |
| 596 | u32 event_id = its_cmd_get_id(its_cmd); |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 597 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 598 | |
| 599 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 600 | ite = find_ite(its, device_id, event_id); |
| 601 | if (ite && ite->collection) { |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 602 | /* |
| 603 | * Though the spec talks about removing the pending state, we |
| 604 | * don't bother here since we clear the ITTE anyway and the |
| 605 | * pending state is a property of the ITTE struct. |
| 606 | */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 607 | its_free_ite(kvm, ite); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 608 | return 0; |
| 609 | } |
| 610 | |
| 611 | return E_ITS_DISCARD_UNMAPPED_INTERRUPT; |
| 612 | } |
| 613 | |
| 614 | /* |
| 615 | * The MOVI command moves an ITTE to a different collection. |
| 616 | * Must be called with the its_lock mutex held. |
| 617 | */ |
| 618 | static int vgic_its_cmd_handle_movi(struct kvm *kvm, struct vgic_its *its, |
| 619 | u64 *its_cmd) |
| 620 | { |
| 621 | u32 device_id = its_cmd_get_deviceid(its_cmd); |
| 622 | u32 event_id = its_cmd_get_id(its_cmd); |
| 623 | u32 coll_id = its_cmd_get_collection(its_cmd); |
| 624 | struct kvm_vcpu *vcpu; |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 625 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 626 | struct its_collection *collection; |
| 627 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 628 | ite = find_ite(its, device_id, event_id); |
| 629 | if (!ite) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 630 | return E_ITS_MOVI_UNMAPPED_INTERRUPT; |
| 631 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 632 | if (!its_is_collection_mapped(ite->collection)) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 633 | return E_ITS_MOVI_UNMAPPED_COLLECTION; |
| 634 | |
| 635 | collection = find_collection(its, coll_id); |
| 636 | if (!its_is_collection_mapped(collection)) |
| 637 | return E_ITS_MOVI_UNMAPPED_COLLECTION; |
| 638 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 639 | ite->collection = collection; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 640 | vcpu = kvm_get_vcpu(kvm, collection->target_addr); |
| 641 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 642 | spin_lock(&ite->irq->irq_lock); |
| 643 | ite->irq->target_vcpu = vcpu; |
| 644 | spin_unlock(&ite->irq->irq_lock); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 649 | /* |
| 650 | * Check whether an ID can be stored into the corresponding guest table. |
| 651 | * For a direct table this is pretty easy, but gets a bit nasty for |
| 652 | * indirect tables. We check whether the resulting guest physical address |
| 653 | * is actually valid (covered by a memslot and guest accessbible). |
| 654 | * For this we have to read the respective first level entry. |
| 655 | */ |
| 656 | static bool vgic_its_check_id(struct vgic_its *its, u64 baser, int id) |
| 657 | { |
| 658 | int l1_tbl_size = GITS_BASER_NR_PAGES(baser) * SZ_64K; |
| 659 | int index; |
| 660 | u64 indirect_ptr; |
| 661 | gfn_t gfn; |
Vladimir Murzin | e29bd6f | 2016-11-02 11:55:33 +0000 | [diff] [blame] | 662 | int esz = GITS_BASER_ENTRY_SIZE(baser); |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 663 | |
| 664 | if (!(baser & GITS_BASER_INDIRECT)) { |
| 665 | phys_addr_t addr; |
| 666 | |
Vladimir Murzin | e29bd6f | 2016-11-02 11:55:33 +0000 | [diff] [blame] | 667 | if (id >= (l1_tbl_size / esz)) |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 668 | return false; |
| 669 | |
Vladimir Murzin | e29bd6f | 2016-11-02 11:55:33 +0000 | [diff] [blame] | 670 | addr = BASER_ADDRESS(baser) + id * esz; |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 671 | gfn = addr >> PAGE_SHIFT; |
| 672 | |
| 673 | return kvm_is_visible_gfn(its->dev->kvm, gfn); |
| 674 | } |
| 675 | |
| 676 | /* calculate and check the index into the 1st level */ |
Vladimir Murzin | e29bd6f | 2016-11-02 11:55:33 +0000 | [diff] [blame] | 677 | index = id / (SZ_64K / esz); |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 678 | if (index >= (l1_tbl_size / sizeof(u64))) |
| 679 | return false; |
| 680 | |
| 681 | /* Each 1st level entry is represented by a 64-bit value. */ |
| 682 | if (kvm_read_guest(its->dev->kvm, |
| 683 | BASER_ADDRESS(baser) + index * sizeof(indirect_ptr), |
| 684 | &indirect_ptr, sizeof(indirect_ptr))) |
| 685 | return false; |
| 686 | |
| 687 | indirect_ptr = le64_to_cpu(indirect_ptr); |
| 688 | |
| 689 | /* check the valid bit of the first level entry */ |
| 690 | if (!(indirect_ptr & BIT_ULL(63))) |
| 691 | return false; |
| 692 | |
| 693 | /* |
| 694 | * Mask the guest physical address and calculate the frame number. |
| 695 | * Any address beyond our supported 48 bits of PA will be caught |
| 696 | * by the actual check in the final step. |
| 697 | */ |
| 698 | indirect_ptr &= GENMASK_ULL(51, 16); |
| 699 | |
| 700 | /* Find the address of the actual entry */ |
Vladimir Murzin | e29bd6f | 2016-11-02 11:55:33 +0000 | [diff] [blame] | 701 | index = id % (SZ_64K / esz); |
| 702 | indirect_ptr += index * esz; |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 703 | gfn = indirect_ptr >> PAGE_SHIFT; |
| 704 | |
| 705 | return kvm_is_visible_gfn(its->dev->kvm, gfn); |
| 706 | } |
| 707 | |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 708 | static int vgic_its_alloc_collection(struct vgic_its *its, |
| 709 | struct its_collection **colp, |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 710 | u32 coll_id) |
| 711 | { |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 712 | struct its_collection *collection; |
| 713 | |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 714 | if (!vgic_its_check_id(its, its->baser_coll_table, coll_id)) |
| 715 | return E_ITS_MAPC_COLLECTION_OOR; |
| 716 | |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 717 | collection = kzalloc(sizeof(*collection), GFP_KERNEL); |
| 718 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 719 | collection->collection_id = coll_id; |
| 720 | collection->target_addr = COLLECTION_NOT_MAPPED; |
| 721 | |
| 722 | list_add_tail(&collection->coll_list, &its->collection_list); |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 723 | *colp = collection; |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | static void vgic_its_free_collection(struct vgic_its *its, u32 coll_id) |
| 729 | { |
| 730 | struct its_collection *collection; |
| 731 | struct its_device *device; |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 732 | struct its_ite *ite; |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 733 | |
| 734 | /* |
| 735 | * Clearing the mapping for that collection ID removes the |
| 736 | * entry from the list. If there wasn't any before, we can |
| 737 | * go home early. |
| 738 | */ |
| 739 | collection = find_collection(its, coll_id); |
| 740 | if (!collection) |
| 741 | return; |
| 742 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 743 | for_each_lpi_its(device, ite, its) |
| 744 | if (ite->collection && |
| 745 | ite->collection->collection_id == coll_id) |
| 746 | ite->collection = NULL; |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 747 | |
| 748 | list_del(&collection->coll_list); |
| 749 | kfree(collection); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | /* |
| 753 | * The MAPTI and MAPI commands map LPIs to ITTEs. |
| 754 | * Must be called with its_lock mutex held. |
| 755 | */ |
| 756 | 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] | 757 | u64 *its_cmd) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 758 | { |
| 759 | u32 device_id = its_cmd_get_deviceid(its_cmd); |
| 760 | u32 event_id = its_cmd_get_id(its_cmd); |
| 761 | u32 coll_id = its_cmd_get_collection(its_cmd); |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 762 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 763 | struct its_device *device; |
| 764 | struct its_collection *collection, *new_coll = NULL; |
| 765 | int lpi_nr; |
Christoffer Dall | 99e5e88 | 2016-08-01 20:25:33 +0200 | [diff] [blame] | 766 | struct vgic_irq *irq; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 767 | |
| 768 | device = find_its_device(its, device_id); |
| 769 | if (!device) |
| 770 | return E_ITS_MAPTI_UNMAPPED_DEVICE; |
| 771 | |
Marc Zyngier | a3e7aa2 | 2016-07-17 22:38:32 +0100 | [diff] [blame] | 772 | if (its_cmd_get_command(its_cmd) == GITS_CMD_MAPTI) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 773 | lpi_nr = its_cmd_get_physical_id(its_cmd); |
| 774 | else |
| 775 | lpi_nr = event_id; |
| 776 | if (lpi_nr < GIC_LPI_OFFSET || |
Marc Zyngier | 3a88bde | 2016-07-18 16:27:14 +0100 | [diff] [blame] | 777 | lpi_nr >= max_lpis_propbaser(kvm->arch.vgic.propbaser)) |
| 778 | return E_ITS_MAPTI_PHYSICALID_OOR; |
| 779 | |
Andre Przywara | 286054a | 2016-08-16 17:51:06 +0100 | [diff] [blame] | 780 | /* If there is an existing mapping, behavior is UNPREDICTABLE. */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 781 | if (find_ite(its, device_id, event_id)) |
Andre Przywara | 286054a | 2016-08-16 17:51:06 +0100 | [diff] [blame] | 782 | return 0; |
| 783 | |
Marc Zyngier | 3a88bde | 2016-07-18 16:27:14 +0100 | [diff] [blame] | 784 | collection = find_collection(its, coll_id); |
| 785 | if (!collection) { |
| 786 | int ret = vgic_its_alloc_collection(its, &collection, coll_id); |
| 787 | if (ret) |
| 788 | return ret; |
| 789 | new_coll = collection; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 790 | } |
| 791 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 792 | ite = kzalloc(sizeof(struct its_ite), GFP_KERNEL); |
| 793 | if (!ite) { |
Andre Przywara | 286054a | 2016-08-16 17:51:06 +0100 | [diff] [blame] | 794 | if (new_coll) |
| 795 | vgic_its_free_collection(its, coll_id); |
| 796 | return -ENOMEM; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 797 | } |
| 798 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 799 | ite->event_id = event_id; |
| 800 | list_add_tail(&ite->ite_list, &device->itt_head); |
Andre Przywara | 286054a | 2016-08-16 17:51:06 +0100 | [diff] [blame] | 801 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 802 | ite->collection = collection; |
| 803 | ite->lpi = lpi_nr; |
Christoffer Dall | 99e5e88 | 2016-08-01 20:25:33 +0200 | [diff] [blame] | 804 | |
| 805 | irq = vgic_add_lpi(kvm, lpi_nr); |
| 806 | if (IS_ERR(irq)) { |
| 807 | if (new_coll) |
| 808 | vgic_its_free_collection(its, coll_id); |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 809 | its_free_ite(kvm, ite); |
Christoffer Dall | 99e5e88 | 2016-08-01 20:25:33 +0200 | [diff] [blame] | 810 | return PTR_ERR(irq); |
| 811 | } |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 812 | ite->irq = irq; |
Christoffer Dall | 99e5e88 | 2016-08-01 20:25:33 +0200 | [diff] [blame] | 813 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 814 | update_affinity_ite(kvm, ite); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 815 | |
| 816 | /* |
| 817 | * We "cache" the configuration table entries in out struct vgic_irq's. |
| 818 | * However we only have those structs for mapped IRQs, so we read in |
| 819 | * the respective config data from memory here upon mapping the LPI. |
| 820 | */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 821 | update_lpi_config(kvm, ite->irq, NULL); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 822 | |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | /* Requires the its_lock to be held. */ |
| 827 | static void vgic_its_unmap_device(struct kvm *kvm, struct its_device *device) |
| 828 | { |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 829 | struct its_ite *ite, *temp; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 830 | |
| 831 | /* |
| 832 | * The spec says that unmapping a device with still valid |
| 833 | * ITTEs associated is UNPREDICTABLE. We remove all ITTEs, |
| 834 | * since we cannot leave the memory unreferenced. |
| 835 | */ |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 836 | list_for_each_entry_safe(ite, temp, &device->itt_head, ite_list) |
| 837 | its_free_ite(kvm, ite); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 838 | |
| 839 | list_del(&device->dev_list); |
| 840 | kfree(device); |
| 841 | } |
| 842 | |
| 843 | /* |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 844 | * MAPD maps or unmaps a device ID to Interrupt Translation Tables (ITTs). |
| 845 | * Must be called with the its_lock mutex held. |
| 846 | */ |
| 847 | static int vgic_its_cmd_handle_mapd(struct kvm *kvm, struct vgic_its *its, |
| 848 | u64 *its_cmd) |
| 849 | { |
| 850 | u32 device_id = its_cmd_get_deviceid(its_cmd); |
| 851 | bool valid = its_cmd_get_validbit(its_cmd); |
| 852 | struct its_device *device; |
| 853 | |
Marc Zyngier | 6d03a68f | 2016-07-17 21:52:55 +0100 | [diff] [blame] | 854 | if (!vgic_its_check_id(its, its->baser_device_table, device_id)) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 855 | return E_ITS_MAPD_DEVICE_OOR; |
| 856 | |
| 857 | device = find_its_device(its, device_id); |
| 858 | |
| 859 | /* |
| 860 | * The spec says that calling MAPD on an already mapped device |
| 861 | * invalidates all cached data for this device. We implement this |
| 862 | * by removing the mapping and re-establishing it. |
| 863 | */ |
| 864 | if (device) |
| 865 | vgic_its_unmap_device(kvm, device); |
| 866 | |
| 867 | /* |
| 868 | * The spec does not say whether unmapping a not-mapped device |
| 869 | * is an error, so we are done in any case. |
| 870 | */ |
| 871 | if (!valid) |
| 872 | return 0; |
| 873 | |
| 874 | device = kzalloc(sizeof(struct its_device), GFP_KERNEL); |
| 875 | if (!device) |
| 876 | return -ENOMEM; |
| 877 | |
| 878 | device->device_id = device_id; |
| 879 | INIT_LIST_HEAD(&device->itt_head); |
| 880 | |
| 881 | list_add_tail(&device->dev_list, &its->device_list); |
| 882 | |
| 883 | return 0; |
| 884 | } |
| 885 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 886 | /* |
| 887 | * The MAPC command maps collection IDs to redistributors. |
| 888 | * Must be called with the its_lock mutex held. |
| 889 | */ |
| 890 | static int vgic_its_cmd_handle_mapc(struct kvm *kvm, struct vgic_its *its, |
| 891 | u64 *its_cmd) |
| 892 | { |
| 893 | u16 coll_id; |
| 894 | u32 target_addr; |
| 895 | struct its_collection *collection; |
| 896 | bool valid; |
| 897 | |
| 898 | valid = its_cmd_get_validbit(its_cmd); |
| 899 | coll_id = its_cmd_get_collection(its_cmd); |
| 900 | target_addr = its_cmd_get_target_addr(its_cmd); |
| 901 | |
| 902 | if (target_addr >= atomic_read(&kvm->online_vcpus)) |
| 903 | return E_ITS_MAPC_PROCNUM_OOR; |
| 904 | |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 905 | if (!valid) { |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 906 | vgic_its_free_collection(its, coll_id); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 907 | } else { |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 908 | collection = find_collection(its, coll_id); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 909 | |
Marc Zyngier | 17a21f5 | 2016-07-17 20:01:46 +0100 | [diff] [blame] | 910 | if (!collection) { |
| 911 | int ret; |
| 912 | |
| 913 | ret = vgic_its_alloc_collection(its, &collection, |
| 914 | coll_id); |
| 915 | if (ret) |
| 916 | return ret; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 917 | collection->target_addr = target_addr; |
| 918 | } else { |
| 919 | collection->target_addr = target_addr; |
| 920 | update_affinity_collection(kvm, its, collection); |
| 921 | } |
| 922 | } |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
| 927 | /* |
| 928 | * The CLEAR command removes the pending state for a particular LPI. |
| 929 | * Must be called with the its_lock mutex held. |
| 930 | */ |
| 931 | static int vgic_its_cmd_handle_clear(struct kvm *kvm, struct vgic_its *its, |
| 932 | u64 *its_cmd) |
| 933 | { |
| 934 | u32 device_id = its_cmd_get_deviceid(its_cmd); |
| 935 | u32 event_id = its_cmd_get_id(its_cmd); |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 936 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 937 | |
| 938 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 939 | ite = find_ite(its, device_id, event_id); |
| 940 | if (!ite) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 941 | return E_ITS_CLEAR_UNMAPPED_INTERRUPT; |
| 942 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 943 | ite->irq->pending_latch = false; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 944 | |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | /* |
| 949 | * The INV command syncs the configuration bits from the memory table. |
| 950 | * Must be called with the its_lock mutex held. |
| 951 | */ |
| 952 | static int vgic_its_cmd_handle_inv(struct kvm *kvm, struct vgic_its *its, |
| 953 | u64 *its_cmd) |
| 954 | { |
| 955 | u32 device_id = its_cmd_get_deviceid(its_cmd); |
| 956 | u32 event_id = its_cmd_get_id(its_cmd); |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 957 | struct its_ite *ite; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 958 | |
| 959 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 960 | ite = find_ite(its, device_id, event_id); |
| 961 | if (!ite) |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 962 | return E_ITS_INV_UNMAPPED_INTERRUPT; |
| 963 | |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 964 | return update_lpi_config(kvm, ite->irq, NULL); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | /* |
| 968 | * The INVALL command requests flushing of all IRQ data in this collection. |
| 969 | * Find the VCPU mapped to that collection, then iterate over the VM's list |
| 970 | * of mapped LPIs and update the configuration for each IRQ which targets |
| 971 | * the specified vcpu. The configuration will be read from the in-memory |
| 972 | * configuration table. |
| 973 | * Must be called with the its_lock mutex held. |
| 974 | */ |
| 975 | static int vgic_its_cmd_handle_invall(struct kvm *kvm, struct vgic_its *its, |
| 976 | u64 *its_cmd) |
| 977 | { |
| 978 | u32 coll_id = its_cmd_get_collection(its_cmd); |
| 979 | struct its_collection *collection; |
| 980 | struct kvm_vcpu *vcpu; |
| 981 | struct vgic_irq *irq; |
| 982 | u32 *intids; |
| 983 | int irq_count, i; |
| 984 | |
| 985 | collection = find_collection(its, coll_id); |
| 986 | if (!its_is_collection_mapped(collection)) |
| 987 | return E_ITS_INVALL_UNMAPPED_COLLECTION; |
| 988 | |
| 989 | vcpu = kvm_get_vcpu(kvm, collection->target_addr); |
| 990 | |
| 991 | irq_count = vgic_copy_lpi_list(kvm, &intids); |
| 992 | if (irq_count < 0) |
| 993 | return irq_count; |
| 994 | |
| 995 | for (i = 0; i < irq_count; i++) { |
| 996 | irq = vgic_get_irq(kvm, NULL, intids[i]); |
| 997 | if (!irq) |
| 998 | continue; |
| 999 | update_lpi_config(kvm, irq, vcpu); |
| 1000 | vgic_put_irq(kvm, irq); |
| 1001 | } |
| 1002 | |
| 1003 | kfree(intids); |
| 1004 | |
| 1005 | return 0; |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * The MOVALL command moves the pending state of all IRQs targeting one |
| 1010 | * redistributor to another. We don't hold the pending state in the VCPUs, |
| 1011 | * but in the IRQs instead, so there is really not much to do for us here. |
| 1012 | * However the spec says that no IRQ must target the old redistributor |
| 1013 | * afterwards, so we make sure that no LPI is using the associated target_vcpu. |
| 1014 | * This command affects all LPIs in the system that target that redistributor. |
| 1015 | */ |
| 1016 | static int vgic_its_cmd_handle_movall(struct kvm *kvm, struct vgic_its *its, |
| 1017 | u64 *its_cmd) |
| 1018 | { |
| 1019 | struct vgic_dist *dist = &kvm->arch.vgic; |
| 1020 | u32 target1_addr = its_cmd_get_target_addr(its_cmd); |
| 1021 | u32 target2_addr = its_cmd_mask_field(its_cmd, 3, 16, 32); |
| 1022 | struct kvm_vcpu *vcpu1, *vcpu2; |
| 1023 | struct vgic_irq *irq; |
| 1024 | |
| 1025 | if (target1_addr >= atomic_read(&kvm->online_vcpus) || |
| 1026 | target2_addr >= atomic_read(&kvm->online_vcpus)) |
| 1027 | return E_ITS_MOVALL_PROCNUM_OOR; |
| 1028 | |
| 1029 | if (target1_addr == target2_addr) |
| 1030 | return 0; |
| 1031 | |
| 1032 | vcpu1 = kvm_get_vcpu(kvm, target1_addr); |
| 1033 | vcpu2 = kvm_get_vcpu(kvm, target2_addr); |
| 1034 | |
| 1035 | spin_lock(&dist->lpi_list_lock); |
| 1036 | |
| 1037 | list_for_each_entry(irq, &dist->lpi_list_head, lpi_list) { |
| 1038 | spin_lock(&irq->irq_lock); |
| 1039 | |
| 1040 | if (irq->target_vcpu == vcpu1) |
| 1041 | irq->target_vcpu = vcpu2; |
| 1042 | |
| 1043 | spin_unlock(&irq->irq_lock); |
| 1044 | } |
| 1045 | |
| 1046 | spin_unlock(&dist->lpi_list_lock); |
| 1047 | |
| 1048 | return 0; |
| 1049 | } |
| 1050 | |
| 1051 | /* |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 1052 | * The INT command injects the LPI associated with that DevID/EvID pair. |
| 1053 | * Must be called with the its_lock mutex held. |
| 1054 | */ |
| 1055 | static int vgic_its_cmd_handle_int(struct kvm *kvm, struct vgic_its *its, |
| 1056 | u64 *its_cmd) |
| 1057 | { |
| 1058 | u32 msi_data = its_cmd_get_id(its_cmd); |
| 1059 | u64 msi_devid = its_cmd_get_deviceid(its_cmd); |
| 1060 | |
Andre Przywara | fd837b0 | 2016-08-08 17:29:28 +0100 | [diff] [blame] | 1061 | return vgic_its_trigger_msi(kvm, its, msi_devid, msi_data); |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 1062 | } |
| 1063 | |
| 1064 | /* |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1065 | * This function is called with the its_cmd lock held, but the ITS data |
| 1066 | * structure lock dropped. |
| 1067 | */ |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1068 | static int vgic_its_handle_command(struct kvm *kvm, struct vgic_its *its, |
| 1069 | u64 *its_cmd) |
| 1070 | { |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1071 | int ret = -ENODEV; |
| 1072 | |
| 1073 | mutex_lock(&its->its_lock); |
Marc Zyngier | a3e7aa2 | 2016-07-17 22:38:32 +0100 | [diff] [blame] | 1074 | switch (its_cmd_get_command(its_cmd)) { |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1075 | case GITS_CMD_MAPD: |
| 1076 | ret = vgic_its_cmd_handle_mapd(kvm, its, its_cmd); |
| 1077 | break; |
| 1078 | case GITS_CMD_MAPC: |
| 1079 | ret = vgic_its_cmd_handle_mapc(kvm, its, its_cmd); |
| 1080 | break; |
| 1081 | case GITS_CMD_MAPI: |
Marc Zyngier | a3e7aa2 | 2016-07-17 22:38:32 +0100 | [diff] [blame] | 1082 | ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1083 | break; |
| 1084 | case GITS_CMD_MAPTI: |
Marc Zyngier | a3e7aa2 | 2016-07-17 22:38:32 +0100 | [diff] [blame] | 1085 | ret = vgic_its_cmd_handle_mapi(kvm, its, its_cmd); |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1086 | break; |
| 1087 | case GITS_CMD_MOVI: |
| 1088 | ret = vgic_its_cmd_handle_movi(kvm, its, its_cmd); |
| 1089 | break; |
| 1090 | case GITS_CMD_DISCARD: |
| 1091 | ret = vgic_its_cmd_handle_discard(kvm, its, its_cmd); |
| 1092 | break; |
| 1093 | case GITS_CMD_CLEAR: |
| 1094 | ret = vgic_its_cmd_handle_clear(kvm, its, its_cmd); |
| 1095 | break; |
| 1096 | case GITS_CMD_MOVALL: |
| 1097 | ret = vgic_its_cmd_handle_movall(kvm, its, its_cmd); |
| 1098 | break; |
Andre Przywara | 2891a7d | 2016-07-15 12:43:37 +0100 | [diff] [blame] | 1099 | case GITS_CMD_INT: |
| 1100 | ret = vgic_its_cmd_handle_int(kvm, its, its_cmd); |
| 1101 | break; |
Andre Przywara | df9f58f | 2016-07-15 12:43:36 +0100 | [diff] [blame] | 1102 | case GITS_CMD_INV: |
| 1103 | ret = vgic_its_cmd_handle_inv(kvm, its, its_cmd); |
| 1104 | break; |
| 1105 | case GITS_CMD_INVALL: |
| 1106 | ret = vgic_its_cmd_handle_invall(kvm, its, its_cmd); |
| 1107 | break; |
| 1108 | case GITS_CMD_SYNC: |
| 1109 | /* we ignore this command: we are in sync all of the time */ |
| 1110 | ret = 0; |
| 1111 | break; |
| 1112 | } |
| 1113 | mutex_unlock(&its->its_lock); |
| 1114 | |
| 1115 | return ret; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | static u64 vgic_sanitise_its_baser(u64 reg) |
| 1119 | { |
| 1120 | reg = vgic_sanitise_field(reg, GITS_BASER_SHAREABILITY_MASK, |
| 1121 | GITS_BASER_SHAREABILITY_SHIFT, |
| 1122 | vgic_sanitise_shareability); |
| 1123 | reg = vgic_sanitise_field(reg, GITS_BASER_INNER_CACHEABILITY_MASK, |
| 1124 | GITS_BASER_INNER_CACHEABILITY_SHIFT, |
| 1125 | vgic_sanitise_inner_cacheability); |
| 1126 | reg = vgic_sanitise_field(reg, GITS_BASER_OUTER_CACHEABILITY_MASK, |
| 1127 | GITS_BASER_OUTER_CACHEABILITY_SHIFT, |
| 1128 | vgic_sanitise_outer_cacheability); |
| 1129 | |
| 1130 | /* Bits 15:12 contain bits 51:48 of the PA, which we don't support. */ |
| 1131 | reg &= ~GENMASK_ULL(15, 12); |
| 1132 | |
| 1133 | /* We support only one (ITS) page size: 64K */ |
| 1134 | reg = (reg & ~GITS_BASER_PAGE_SIZE_MASK) | GITS_BASER_PAGE_SIZE_64K; |
| 1135 | |
| 1136 | return reg; |
| 1137 | } |
| 1138 | |
| 1139 | static u64 vgic_sanitise_its_cbaser(u64 reg) |
| 1140 | { |
| 1141 | reg = vgic_sanitise_field(reg, GITS_CBASER_SHAREABILITY_MASK, |
| 1142 | GITS_CBASER_SHAREABILITY_SHIFT, |
| 1143 | vgic_sanitise_shareability); |
| 1144 | reg = vgic_sanitise_field(reg, GITS_CBASER_INNER_CACHEABILITY_MASK, |
| 1145 | GITS_CBASER_INNER_CACHEABILITY_SHIFT, |
| 1146 | vgic_sanitise_inner_cacheability); |
| 1147 | reg = vgic_sanitise_field(reg, GITS_CBASER_OUTER_CACHEABILITY_MASK, |
| 1148 | GITS_CBASER_OUTER_CACHEABILITY_SHIFT, |
| 1149 | vgic_sanitise_outer_cacheability); |
| 1150 | |
| 1151 | /* |
| 1152 | * Sanitise the physical address to be 64k aligned. |
| 1153 | * Also limit the physical addresses to 48 bits. |
| 1154 | */ |
| 1155 | reg &= ~(GENMASK_ULL(51, 48) | GENMASK_ULL(15, 12)); |
| 1156 | |
| 1157 | return reg; |
| 1158 | } |
| 1159 | |
| 1160 | static unsigned long vgic_mmio_read_its_cbaser(struct kvm *kvm, |
| 1161 | struct vgic_its *its, |
| 1162 | gpa_t addr, unsigned int len) |
| 1163 | { |
| 1164 | return extract_bytes(its->cbaser, addr & 7, len); |
| 1165 | } |
| 1166 | |
| 1167 | static void vgic_mmio_write_its_cbaser(struct kvm *kvm, struct vgic_its *its, |
| 1168 | gpa_t addr, unsigned int len, |
| 1169 | unsigned long val) |
| 1170 | { |
| 1171 | /* When GITS_CTLR.Enable is 1, this register is RO. */ |
| 1172 | if (its->enabled) |
| 1173 | return; |
| 1174 | |
| 1175 | mutex_lock(&its->cmd_lock); |
| 1176 | its->cbaser = update_64bit_reg(its->cbaser, addr & 7, len, val); |
| 1177 | its->cbaser = vgic_sanitise_its_cbaser(its->cbaser); |
| 1178 | its->creadr = 0; |
| 1179 | /* |
| 1180 | * CWRITER is architecturally UNKNOWN on reset, but we need to reset |
| 1181 | * it to CREADR to make sure we start with an empty command buffer. |
| 1182 | */ |
| 1183 | its->cwriter = its->creadr; |
| 1184 | mutex_unlock(&its->cmd_lock); |
| 1185 | } |
| 1186 | |
| 1187 | #define ITS_CMD_BUFFER_SIZE(baser) ((((baser) & 0xff) + 1) << 12) |
| 1188 | #define ITS_CMD_SIZE 32 |
| 1189 | #define ITS_CMD_OFFSET(reg) ((reg) & GENMASK(19, 5)) |
| 1190 | |
Andre Przywara | a5e1e6c | 2017-02-16 10:41:20 +0000 | [diff] [blame] | 1191 | /* Must be called with the cmd_lock held. */ |
| 1192 | 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] | 1193 | { |
| 1194 | gpa_t cbaser; |
| 1195 | u64 cmd_buf[4]; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1196 | |
Andre Przywara | a5e1e6c | 2017-02-16 10:41:20 +0000 | [diff] [blame] | 1197 | /* Commands are only processed when the ITS is enabled. */ |
| 1198 | if (!its->enabled) |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1199 | return; |
| 1200 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1201 | cbaser = CBASER_ADDRESS(its->cbaser); |
| 1202 | |
| 1203 | while (its->cwriter != its->creadr) { |
| 1204 | int ret = kvm_read_guest(kvm, cbaser + its->creadr, |
| 1205 | cmd_buf, ITS_CMD_SIZE); |
| 1206 | /* |
| 1207 | * If kvm_read_guest() fails, this could be due to the guest |
| 1208 | * programming a bogus value in CBASER or something else going |
| 1209 | * wrong from which we cannot easily recover. |
| 1210 | * According to section 6.3.2 in the GICv3 spec we can just |
| 1211 | * ignore that command then. |
| 1212 | */ |
| 1213 | if (!ret) |
| 1214 | vgic_its_handle_command(kvm, its, cmd_buf); |
| 1215 | |
| 1216 | its->creadr += ITS_CMD_SIZE; |
| 1217 | if (its->creadr == ITS_CMD_BUFFER_SIZE(its->cbaser)) |
| 1218 | its->creadr = 0; |
| 1219 | } |
Andre Przywara | a5e1e6c | 2017-02-16 10:41:20 +0000 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | /* |
| 1223 | * By writing to CWRITER the guest announces new commands to be processed. |
| 1224 | * To avoid any races in the first place, we take the its_cmd lock, which |
| 1225 | * protects our ring buffer variables, so that there is only one user |
| 1226 | * per ITS handling commands at a given time. |
| 1227 | */ |
| 1228 | static void vgic_mmio_write_its_cwriter(struct kvm *kvm, struct vgic_its *its, |
| 1229 | gpa_t addr, unsigned int len, |
| 1230 | unsigned long val) |
| 1231 | { |
| 1232 | u64 reg; |
| 1233 | |
| 1234 | if (!its) |
| 1235 | return; |
| 1236 | |
| 1237 | mutex_lock(&its->cmd_lock); |
| 1238 | |
| 1239 | reg = update_64bit_reg(its->cwriter, addr & 7, len, val); |
| 1240 | reg = ITS_CMD_OFFSET(reg); |
| 1241 | if (reg >= ITS_CMD_BUFFER_SIZE(its->cbaser)) { |
| 1242 | mutex_unlock(&its->cmd_lock); |
| 1243 | return; |
| 1244 | } |
| 1245 | its->cwriter = reg; |
| 1246 | |
| 1247 | vgic_its_process_commands(kvm, its); |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1248 | |
| 1249 | mutex_unlock(&its->cmd_lock); |
| 1250 | } |
| 1251 | |
| 1252 | static unsigned long vgic_mmio_read_its_cwriter(struct kvm *kvm, |
| 1253 | struct vgic_its *its, |
| 1254 | gpa_t addr, unsigned int len) |
| 1255 | { |
| 1256 | return extract_bytes(its->cwriter, addr & 0x7, len); |
| 1257 | } |
| 1258 | |
| 1259 | static unsigned long vgic_mmio_read_its_creadr(struct kvm *kvm, |
| 1260 | struct vgic_its *its, |
| 1261 | gpa_t addr, unsigned int len) |
| 1262 | { |
| 1263 | return extract_bytes(its->creadr, addr & 0x7, len); |
| 1264 | } |
| 1265 | |
Eric Auger | 0979bfa | 2017-01-04 11:58:41 +0100 | [diff] [blame] | 1266 | static int vgic_mmio_uaccess_write_its_creadr(struct kvm *kvm, |
| 1267 | struct vgic_its *its, |
| 1268 | gpa_t addr, unsigned int len, |
| 1269 | unsigned long val) |
| 1270 | { |
| 1271 | u32 cmd_offset; |
| 1272 | int ret = 0; |
| 1273 | |
| 1274 | mutex_lock(&its->cmd_lock); |
| 1275 | |
| 1276 | if (its->enabled) { |
| 1277 | ret = -EBUSY; |
| 1278 | goto out; |
| 1279 | } |
| 1280 | |
| 1281 | cmd_offset = ITS_CMD_OFFSET(val); |
| 1282 | if (cmd_offset >= ITS_CMD_BUFFER_SIZE(its->cbaser)) { |
| 1283 | ret = -EINVAL; |
| 1284 | goto out; |
| 1285 | } |
| 1286 | |
| 1287 | its->creadr = cmd_offset; |
| 1288 | out: |
| 1289 | mutex_unlock(&its->cmd_lock); |
| 1290 | return ret; |
| 1291 | } |
| 1292 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1293 | #define BASER_INDEX(addr) (((addr) / sizeof(u64)) & 0x7) |
| 1294 | static unsigned long vgic_mmio_read_its_baser(struct kvm *kvm, |
| 1295 | struct vgic_its *its, |
| 1296 | gpa_t addr, unsigned int len) |
| 1297 | { |
| 1298 | u64 reg; |
| 1299 | |
| 1300 | switch (BASER_INDEX(addr)) { |
| 1301 | case 0: |
| 1302 | reg = its->baser_device_table; |
| 1303 | break; |
| 1304 | case 1: |
| 1305 | reg = its->baser_coll_table; |
| 1306 | break; |
| 1307 | default: |
| 1308 | reg = 0; |
| 1309 | break; |
| 1310 | } |
| 1311 | |
| 1312 | return extract_bytes(reg, addr & 7, len); |
| 1313 | } |
| 1314 | |
| 1315 | #define GITS_BASER_RO_MASK (GENMASK_ULL(52, 48) | GENMASK_ULL(58, 56)) |
| 1316 | static void vgic_mmio_write_its_baser(struct kvm *kvm, |
| 1317 | struct vgic_its *its, |
| 1318 | gpa_t addr, unsigned int len, |
| 1319 | unsigned long val) |
| 1320 | { |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame^] | 1321 | const struct vgic_its_abi *abi = vgic_its_get_abi(its); |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1322 | u64 entry_size, device_type; |
| 1323 | u64 reg, *regptr, clearbits = 0; |
| 1324 | |
| 1325 | /* When GITS_CTLR.Enable is 1, we ignore write accesses. */ |
| 1326 | if (its->enabled) |
| 1327 | return; |
| 1328 | |
| 1329 | switch (BASER_INDEX(addr)) { |
| 1330 | case 0: |
| 1331 | regptr = &its->baser_device_table; |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame^] | 1332 | entry_size = abi->dte_esz; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1333 | device_type = GITS_BASER_TYPE_DEVICE; |
| 1334 | break; |
| 1335 | case 1: |
| 1336 | regptr = &its->baser_coll_table; |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame^] | 1337 | entry_size = abi->cte_esz; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1338 | device_type = GITS_BASER_TYPE_COLLECTION; |
| 1339 | clearbits = GITS_BASER_INDIRECT; |
| 1340 | break; |
| 1341 | default: |
| 1342 | return; |
| 1343 | } |
| 1344 | |
| 1345 | reg = update_64bit_reg(*regptr, addr & 7, len, val); |
| 1346 | reg &= ~GITS_BASER_RO_MASK; |
| 1347 | reg &= ~clearbits; |
| 1348 | |
| 1349 | reg |= (entry_size - 1) << GITS_BASER_ENTRY_SIZE_SHIFT; |
| 1350 | reg |= device_type << GITS_BASER_TYPE_SHIFT; |
| 1351 | reg = vgic_sanitise_its_baser(reg); |
| 1352 | |
| 1353 | *regptr = reg; |
| 1354 | } |
| 1355 | |
Andre Przywara | a5e1e6c | 2017-02-16 10:41:20 +0000 | [diff] [blame] | 1356 | static unsigned long vgic_mmio_read_its_ctlr(struct kvm *vcpu, |
| 1357 | struct vgic_its *its, |
| 1358 | gpa_t addr, unsigned int len) |
| 1359 | { |
| 1360 | u32 reg = 0; |
| 1361 | |
| 1362 | mutex_lock(&its->cmd_lock); |
| 1363 | if (its->creadr == its->cwriter) |
| 1364 | reg |= GITS_CTLR_QUIESCENT; |
| 1365 | if (its->enabled) |
| 1366 | reg |= GITS_CTLR_ENABLE; |
| 1367 | mutex_unlock(&its->cmd_lock); |
| 1368 | |
| 1369 | return reg; |
| 1370 | } |
| 1371 | |
| 1372 | static void vgic_mmio_write_its_ctlr(struct kvm *kvm, struct vgic_its *its, |
| 1373 | gpa_t addr, unsigned int len, |
| 1374 | unsigned long val) |
| 1375 | { |
| 1376 | mutex_lock(&its->cmd_lock); |
| 1377 | |
| 1378 | its->enabled = !!(val & GITS_CTLR_ENABLE); |
| 1379 | |
| 1380 | /* |
| 1381 | * Try to process any pending commands. This function bails out early |
| 1382 | * if the ITS is disabled or no commands have been queued. |
| 1383 | */ |
| 1384 | vgic_its_process_commands(kvm, its); |
| 1385 | |
| 1386 | mutex_unlock(&its->cmd_lock); |
| 1387 | } |
| 1388 | |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1389 | #define REGISTER_ITS_DESC(off, rd, wr, length, acc) \ |
| 1390 | { \ |
| 1391 | .reg_offset = off, \ |
| 1392 | .len = length, \ |
| 1393 | .access_flags = acc, \ |
| 1394 | .its_read = rd, \ |
| 1395 | .its_write = wr, \ |
| 1396 | } |
| 1397 | |
Eric Auger | 0979bfa | 2017-01-04 11:58:41 +0100 | [diff] [blame] | 1398 | #define REGISTER_ITS_DESC_UACCESS(off, rd, wr, uwr, length, acc)\ |
| 1399 | { \ |
| 1400 | .reg_offset = off, \ |
| 1401 | .len = length, \ |
| 1402 | .access_flags = acc, \ |
| 1403 | .its_read = rd, \ |
| 1404 | .its_write = wr, \ |
| 1405 | .uaccess_its_write = uwr, \ |
| 1406 | } |
| 1407 | |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1408 | static void its_mmio_write_wi(struct kvm *kvm, struct vgic_its *its, |
| 1409 | gpa_t addr, unsigned int len, unsigned long val) |
| 1410 | { |
| 1411 | /* Ignore */ |
| 1412 | } |
| 1413 | |
| 1414 | static struct vgic_register_region its_registers[] = { |
| 1415 | REGISTER_ITS_DESC(GITS_CTLR, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1416 | vgic_mmio_read_its_ctlr, vgic_mmio_write_its_ctlr, 4, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1417 | VGIC_ACCESS_32bit), |
| 1418 | REGISTER_ITS_DESC(GITS_IIDR, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1419 | vgic_mmio_read_its_iidr, its_mmio_write_wi, 4, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1420 | VGIC_ACCESS_32bit), |
| 1421 | REGISTER_ITS_DESC(GITS_TYPER, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1422 | vgic_mmio_read_its_typer, its_mmio_write_wi, 8, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1423 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
| 1424 | REGISTER_ITS_DESC(GITS_CBASER, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1425 | vgic_mmio_read_its_cbaser, vgic_mmio_write_its_cbaser, 8, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1426 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
| 1427 | REGISTER_ITS_DESC(GITS_CWRITER, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1428 | vgic_mmio_read_its_cwriter, vgic_mmio_write_its_cwriter, 8, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1429 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
Eric Auger | 0979bfa | 2017-01-04 11:58:41 +0100 | [diff] [blame] | 1430 | REGISTER_ITS_DESC_UACCESS(GITS_CREADR, |
| 1431 | vgic_mmio_read_its_creadr, its_mmio_write_wi, |
| 1432 | vgic_mmio_uaccess_write_its_creadr, 8, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1433 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
| 1434 | REGISTER_ITS_DESC(GITS_BASER, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1435 | vgic_mmio_read_its_baser, vgic_mmio_write_its_baser, 0x40, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1436 | VGIC_ACCESS_64bit | VGIC_ACCESS_32bit), |
| 1437 | REGISTER_ITS_DESC(GITS_IDREGS_BASE, |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1438 | vgic_mmio_read_its_idregs, its_mmio_write_wi, 0x30, |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1439 | VGIC_ACCESS_32bit), |
| 1440 | }; |
| 1441 | |
Andre Przywara | 33d3bc9 | 2016-07-15 12:43:34 +0100 | [diff] [blame] | 1442 | /* This is called on setting the LPI enable bit in the redistributor. */ |
| 1443 | void vgic_enable_lpis(struct kvm_vcpu *vcpu) |
| 1444 | { |
| 1445 | if (!(vcpu->arch.vgic_cpu.pendbaser & GICR_PENDBASER_PTZ)) |
| 1446 | its_sync_lpi_pending_table(vcpu); |
| 1447 | } |
| 1448 | |
Andre Przywara | c773576 | 2016-08-08 16:45:43 +0100 | [diff] [blame] | 1449 | static int vgic_register_its_iodev(struct kvm *kvm, struct vgic_its *its) |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1450 | { |
| 1451 | struct vgic_io_device *iodev = &its->iodev; |
| 1452 | int ret; |
| 1453 | |
Andre Przywara | c773576 | 2016-08-08 16:45:43 +0100 | [diff] [blame] | 1454 | if (!its->initialized) |
| 1455 | return -EBUSY; |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1456 | |
Andre Przywara | 59c5ab4 | 2016-07-15 12:43:30 +0100 | [diff] [blame] | 1457 | if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) |
| 1458 | return -ENXIO; |
| 1459 | |
| 1460 | iodev->regions = its_registers; |
| 1461 | iodev->nr_regions = ARRAY_SIZE(its_registers); |
| 1462 | kvm_iodevice_init(&iodev->dev, &kvm_io_gic_ops); |
| 1463 | |
| 1464 | iodev->base_addr = its->vgic_its_base; |
| 1465 | iodev->iodev_type = IODEV_ITS; |
| 1466 | iodev->its = its; |
| 1467 | mutex_lock(&kvm->slots_lock); |
| 1468 | ret = kvm_io_bus_register_dev(kvm, KVM_MMIO_BUS, iodev->base_addr, |
| 1469 | KVM_VGIC_V3_ITS_SIZE, &iodev->dev); |
| 1470 | mutex_unlock(&kvm->slots_lock); |
| 1471 | |
| 1472 | return ret; |
| 1473 | } |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1474 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1475 | #define INITIAL_BASER_VALUE \ |
| 1476 | (GIC_BASER_CACHEABILITY(GITS_BASER, INNER, RaWb) | \ |
| 1477 | GIC_BASER_CACHEABILITY(GITS_BASER, OUTER, SameAsInner) | \ |
| 1478 | GIC_BASER_SHAREABILITY(GITS_BASER, InnerShareable) | \ |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1479 | GITS_BASER_PAGE_SIZE_64K) |
| 1480 | |
| 1481 | #define INITIAL_PROPBASER_VALUE \ |
| 1482 | (GIC_BASER_CACHEABILITY(GICR_PROPBASER, INNER, RaWb) | \ |
| 1483 | GIC_BASER_CACHEABILITY(GICR_PROPBASER, OUTER, SameAsInner) | \ |
| 1484 | GIC_BASER_SHAREABILITY(GICR_PROPBASER, InnerShareable)) |
| 1485 | |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1486 | static int vgic_its_create(struct kvm_device *dev, u32 type) |
| 1487 | { |
| 1488 | struct vgic_its *its; |
| 1489 | |
| 1490 | if (type != KVM_DEV_TYPE_ARM_VGIC_ITS) |
| 1491 | return -ENODEV; |
| 1492 | |
| 1493 | its = kzalloc(sizeof(struct vgic_its), GFP_KERNEL); |
| 1494 | if (!its) |
| 1495 | return -ENOMEM; |
| 1496 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1497 | mutex_init(&its->its_lock); |
| 1498 | mutex_init(&its->cmd_lock); |
| 1499 | |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1500 | its->vgic_its_base = VGIC_ADDR_UNDEF; |
| 1501 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1502 | INIT_LIST_HEAD(&its->device_list); |
| 1503 | INIT_LIST_HEAD(&its->collection_list); |
| 1504 | |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1505 | dev->kvm->arch.vgic.has_its = true; |
| 1506 | its->initialized = false; |
| 1507 | its->enabled = false; |
Marc Zyngier | bb71764 | 2016-07-17 21:35:07 +0100 | [diff] [blame] | 1508 | its->dev = dev; |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1509 | |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1510 | its->baser_device_table = INITIAL_BASER_VALUE | |
| 1511 | ((u64)GITS_BASER_TYPE_DEVICE << GITS_BASER_TYPE_SHIFT); |
| 1512 | its->baser_coll_table = INITIAL_BASER_VALUE | |
| 1513 | ((u64)GITS_BASER_TYPE_COLLECTION << GITS_BASER_TYPE_SHIFT); |
| 1514 | dev->kvm->arch.vgic.propbaser = INITIAL_PROPBASER_VALUE; |
| 1515 | |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1516 | dev->private = its; |
| 1517 | |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame^] | 1518 | return vgic_its_set_abi(its, NR_ITS_ABIS - 1); |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1519 | } |
| 1520 | |
| 1521 | static void vgic_its_destroy(struct kvm_device *kvm_dev) |
| 1522 | { |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1523 | struct kvm *kvm = kvm_dev->kvm; |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1524 | struct vgic_its *its = kvm_dev->private; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1525 | struct its_device *dev; |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 1526 | struct its_ite *ite; |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1527 | struct list_head *dev_cur, *dev_temp; |
| 1528 | struct list_head *cur, *temp; |
| 1529 | |
| 1530 | /* |
| 1531 | * We may end up here without the lists ever having been initialized. |
| 1532 | * Check this and bail out early to avoid dereferencing a NULL pointer. |
| 1533 | */ |
| 1534 | if (!its->device_list.next) |
| 1535 | return; |
| 1536 | |
| 1537 | mutex_lock(&its->its_lock); |
| 1538 | list_for_each_safe(dev_cur, dev_temp, &its->device_list) { |
| 1539 | dev = container_of(dev_cur, struct its_device, dev_list); |
| 1540 | list_for_each_safe(cur, temp, &dev->itt_head) { |
Eric Auger | 9ce91c7 | 2017-02-08 06:09:29 +0100 | [diff] [blame] | 1541 | ite = (container_of(cur, struct its_ite, ite_list)); |
| 1542 | its_free_ite(kvm, ite); |
Andre Przywara | 424c338 | 2016-07-15 12:43:32 +0100 | [diff] [blame] | 1543 | } |
| 1544 | list_del(dev_cur); |
| 1545 | kfree(dev); |
| 1546 | } |
| 1547 | |
| 1548 | list_for_each_safe(cur, temp, &its->collection_list) { |
| 1549 | list_del(cur); |
| 1550 | kfree(container_of(cur, struct its_collection, coll_list)); |
| 1551 | } |
| 1552 | mutex_unlock(&its->its_lock); |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1553 | |
| 1554 | kfree(its); |
| 1555 | } |
| 1556 | |
Eric Auger | 876ae23 | 2016-12-20 01:36:35 -0500 | [diff] [blame] | 1557 | int vgic_its_has_attr_regs(struct kvm_device *dev, |
| 1558 | struct kvm_device_attr *attr) |
| 1559 | { |
Eric Auger | 8331c23 | 2016-12-20 09:33:13 +0100 | [diff] [blame] | 1560 | const struct vgic_register_region *region; |
| 1561 | gpa_t offset = attr->attr; |
| 1562 | int align; |
| 1563 | |
| 1564 | align = (offset < GITS_TYPER) || (offset >= GITS_PIDR4) ? 0x3 : 0x7; |
| 1565 | |
| 1566 | if (offset & align) |
| 1567 | return -EINVAL; |
| 1568 | |
| 1569 | region = vgic_find_mmio_region(its_registers, |
| 1570 | ARRAY_SIZE(its_registers), |
| 1571 | offset); |
| 1572 | if (!region) |
| 1573 | return -ENXIO; |
| 1574 | |
| 1575 | return 0; |
Eric Auger | 876ae23 | 2016-12-20 01:36:35 -0500 | [diff] [blame] | 1576 | } |
| 1577 | |
| 1578 | int vgic_its_attr_regs_access(struct kvm_device *dev, |
| 1579 | struct kvm_device_attr *attr, |
| 1580 | u64 *reg, bool is_write) |
| 1581 | { |
Eric Auger | 8331c23 | 2016-12-20 09:33:13 +0100 | [diff] [blame] | 1582 | const struct vgic_register_region *region; |
| 1583 | struct vgic_its *its; |
| 1584 | gpa_t addr, offset; |
| 1585 | unsigned int len; |
| 1586 | int align, ret = 0; |
| 1587 | |
| 1588 | its = dev->private; |
| 1589 | offset = attr->attr; |
| 1590 | |
| 1591 | /* |
| 1592 | * Although the spec supports upper/lower 32-bit accesses to |
| 1593 | * 64-bit ITS registers, the userspace ABI requires 64-bit |
| 1594 | * accesses to all 64-bit wide registers. We therefore only |
| 1595 | * support 32-bit accesses to GITS_CTLR, GITS_IIDR and GITS ID |
| 1596 | * registers |
| 1597 | */ |
| 1598 | if ((offset < GITS_TYPER) || (offset >= GITS_PIDR4)) |
| 1599 | align = 0x3; |
| 1600 | else |
| 1601 | align = 0x7; |
| 1602 | |
| 1603 | if (offset & align) |
| 1604 | return -EINVAL; |
| 1605 | |
| 1606 | mutex_lock(&dev->kvm->lock); |
| 1607 | |
| 1608 | if (IS_VGIC_ADDR_UNDEF(its->vgic_its_base)) { |
| 1609 | ret = -ENXIO; |
| 1610 | goto out; |
| 1611 | } |
| 1612 | |
| 1613 | region = vgic_find_mmio_region(its_registers, |
| 1614 | ARRAY_SIZE(its_registers), |
| 1615 | offset); |
| 1616 | if (!region) { |
| 1617 | ret = -ENXIO; |
| 1618 | goto out; |
| 1619 | } |
| 1620 | |
| 1621 | if (!lock_all_vcpus(dev->kvm)) { |
| 1622 | ret = -EBUSY; |
| 1623 | goto out; |
| 1624 | } |
| 1625 | |
| 1626 | addr = its->vgic_its_base + offset; |
| 1627 | |
| 1628 | len = region->access_flags & VGIC_ACCESS_64bit ? 8 : 4; |
| 1629 | |
| 1630 | if (is_write) { |
| 1631 | if (region->uaccess_its_write) |
| 1632 | ret = region->uaccess_its_write(dev->kvm, its, addr, |
| 1633 | len, *reg); |
| 1634 | else |
| 1635 | region->its_write(dev->kvm, its, addr, len, *reg); |
| 1636 | } else { |
| 1637 | *reg = region->its_read(dev->kvm, its, addr, len); |
| 1638 | } |
| 1639 | unlock_all_vcpus(dev->kvm); |
| 1640 | out: |
| 1641 | mutex_unlock(&dev->kvm->lock); |
| 1642 | return ret; |
Eric Auger | 876ae23 | 2016-12-20 01:36:35 -0500 | [diff] [blame] | 1643 | } |
| 1644 | |
Eric Auger | 71afe47 | 2017-04-13 09:06:20 +0200 | [diff] [blame^] | 1645 | /** |
| 1646 | * vgic_its_save_tables_v0 - Save the ITS tables into guest ARM |
| 1647 | * according to v0 ABI |
| 1648 | */ |
| 1649 | static int vgic_its_save_tables_v0(struct vgic_its *its) |
| 1650 | { |
| 1651 | return -ENXIO; |
| 1652 | } |
| 1653 | |
| 1654 | /** |
| 1655 | * vgic_its_restore_tables_v0 - Restore the ITS tables from guest RAM |
| 1656 | * to internal data structs according to V0 ABI |
| 1657 | * |
| 1658 | */ |
| 1659 | static int vgic_its_restore_tables_v0(struct vgic_its *its) |
| 1660 | { |
| 1661 | return -ENXIO; |
| 1662 | } |
| 1663 | |
| 1664 | static int vgic_its_commit_v0(struct vgic_its *its) |
| 1665 | { |
| 1666 | const struct vgic_its_abi *abi; |
| 1667 | |
| 1668 | abi = vgic_its_get_abi(its); |
| 1669 | its->baser_coll_table &= ~GITS_BASER_ENTRY_SIZE_MASK; |
| 1670 | its->baser_device_table &= ~GITS_BASER_ENTRY_SIZE_MASK; |
| 1671 | |
| 1672 | its->baser_coll_table |= (GIC_ENCODE_SZ(abi->cte_esz, 5) |
| 1673 | << GITS_BASER_ENTRY_SIZE_SHIFT); |
| 1674 | |
| 1675 | its->baser_device_table |= (GIC_ENCODE_SZ(abi->dte_esz, 5) |
| 1676 | << GITS_BASER_ENTRY_SIZE_SHIFT); |
| 1677 | return 0; |
| 1678 | } |
| 1679 | |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1680 | static int vgic_its_has_attr(struct kvm_device *dev, |
| 1681 | struct kvm_device_attr *attr) |
| 1682 | { |
| 1683 | switch (attr->group) { |
| 1684 | case KVM_DEV_ARM_VGIC_GRP_ADDR: |
| 1685 | switch (attr->attr) { |
| 1686 | case KVM_VGIC_ITS_ADDR_TYPE: |
| 1687 | return 0; |
| 1688 | } |
| 1689 | break; |
| 1690 | case KVM_DEV_ARM_VGIC_GRP_CTRL: |
| 1691 | switch (attr->attr) { |
| 1692 | case KVM_DEV_ARM_VGIC_CTRL_INIT: |
| 1693 | return 0; |
| 1694 | } |
| 1695 | break; |
Eric Auger | 876ae23 | 2016-12-20 01:36:35 -0500 | [diff] [blame] | 1696 | case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: |
| 1697 | return vgic_its_has_attr_regs(dev, attr); |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1698 | } |
| 1699 | return -ENXIO; |
| 1700 | } |
| 1701 | |
| 1702 | static int vgic_its_set_attr(struct kvm_device *dev, |
| 1703 | struct kvm_device_attr *attr) |
| 1704 | { |
| 1705 | struct vgic_its *its = dev->private; |
| 1706 | int ret; |
| 1707 | |
| 1708 | switch (attr->group) { |
| 1709 | case KVM_DEV_ARM_VGIC_GRP_ADDR: { |
| 1710 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; |
| 1711 | unsigned long type = (unsigned long)attr->attr; |
| 1712 | u64 addr; |
| 1713 | |
| 1714 | if (type != KVM_VGIC_ITS_ADDR_TYPE) |
| 1715 | return -ENODEV; |
| 1716 | |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1717 | if (copy_from_user(&addr, uaddr, sizeof(addr))) |
| 1718 | return -EFAULT; |
| 1719 | |
| 1720 | ret = vgic_check_ioaddr(dev->kvm, &its->vgic_its_base, |
| 1721 | addr, SZ_64K); |
| 1722 | if (ret) |
| 1723 | return ret; |
| 1724 | |
| 1725 | its->vgic_its_base = addr; |
| 1726 | |
| 1727 | return 0; |
| 1728 | } |
| 1729 | case KVM_DEV_ARM_VGIC_GRP_CTRL: |
| 1730 | switch (attr->attr) { |
| 1731 | case KVM_DEV_ARM_VGIC_CTRL_INIT: |
Andre Przywara | c773576 | 2016-08-08 16:45:43 +0100 | [diff] [blame] | 1732 | its->initialized = true; |
| 1733 | |
| 1734 | return 0; |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1735 | } |
| 1736 | break; |
Eric Auger | 876ae23 | 2016-12-20 01:36:35 -0500 | [diff] [blame] | 1737 | case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: { |
| 1738 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; |
| 1739 | u64 reg; |
| 1740 | |
| 1741 | if (get_user(reg, uaddr)) |
| 1742 | return -EFAULT; |
| 1743 | |
| 1744 | return vgic_its_attr_regs_access(dev, attr, ®, true); |
| 1745 | } |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1746 | } |
| 1747 | return -ENXIO; |
| 1748 | } |
| 1749 | |
| 1750 | static int vgic_its_get_attr(struct kvm_device *dev, |
| 1751 | struct kvm_device_attr *attr) |
| 1752 | { |
| 1753 | switch (attr->group) { |
| 1754 | case KVM_DEV_ARM_VGIC_GRP_ADDR: { |
| 1755 | struct vgic_its *its = dev->private; |
| 1756 | u64 addr = its->vgic_its_base; |
| 1757 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; |
| 1758 | unsigned long type = (unsigned long)attr->attr; |
| 1759 | |
| 1760 | if (type != KVM_VGIC_ITS_ADDR_TYPE) |
| 1761 | return -ENODEV; |
| 1762 | |
| 1763 | if (copy_to_user(uaddr, &addr, sizeof(addr))) |
| 1764 | return -EFAULT; |
| 1765 | break; |
Eric Auger | 876ae23 | 2016-12-20 01:36:35 -0500 | [diff] [blame] | 1766 | } |
| 1767 | case KVM_DEV_ARM_VGIC_GRP_ITS_REGS: { |
| 1768 | u64 __user *uaddr = (u64 __user *)(long)attr->addr; |
| 1769 | u64 reg; |
| 1770 | int ret; |
| 1771 | |
| 1772 | ret = vgic_its_attr_regs_access(dev, attr, ®, false); |
| 1773 | if (ret) |
| 1774 | return ret; |
| 1775 | return put_user(reg, uaddr); |
| 1776 | } |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1777 | default: |
| 1778 | return -ENXIO; |
| 1779 | } |
Andre Przywara | 1085fdc | 2016-07-15 12:43:31 +0100 | [diff] [blame] | 1780 | |
| 1781 | return 0; |
| 1782 | } |
| 1783 | |
| 1784 | static struct kvm_device_ops kvm_arm_vgic_its_ops = { |
| 1785 | .name = "kvm-arm-vgic-its", |
| 1786 | .create = vgic_its_create, |
| 1787 | .destroy = vgic_its_destroy, |
| 1788 | .set_attr = vgic_its_set_attr, |
| 1789 | .get_attr = vgic_its_get_attr, |
| 1790 | .has_attr = vgic_its_has_attr, |
| 1791 | }; |
| 1792 | |
| 1793 | int kvm_vgic_register_its_device(void) |
| 1794 | { |
| 1795 | return kvm_register_device_ops(&kvm_arm_vgic_its_ops, |
| 1796 | KVM_DEV_TYPE_ARM_VGIC_ITS); |
| 1797 | } |
Andre Przywara | c773576 | 2016-08-08 16:45:43 +0100 | [diff] [blame] | 1798 | |
| 1799 | /* |
| 1800 | * Registers all ITSes with the kvm_io_bus framework. |
| 1801 | * To follow the existing VGIC initialization sequence, this has to be |
| 1802 | * done as late as possible, just before the first VCPU runs. |
| 1803 | */ |
| 1804 | int vgic_register_its_iodevs(struct kvm *kvm) |
| 1805 | { |
| 1806 | struct kvm_device *dev; |
| 1807 | int ret = 0; |
| 1808 | |
| 1809 | list_for_each_entry(dev, &kvm->devices, vm_node) { |
| 1810 | if (dev->ops != &kvm_arm_vgic_its_ops) |
| 1811 | continue; |
| 1812 | |
| 1813 | ret = vgic_register_its_iodev(kvm, dev->private); |
| 1814 | if (ret) |
| 1815 | return ret; |
| 1816 | /* |
| 1817 | * We don't need to care about tearing down previously |
| 1818 | * registered ITSes, as the kvm_io_bus framework removes |
| 1819 | * them for us if the VM gets destroyed. |
| 1820 | */ |
| 1821 | } |
| 1822 | |
| 1823 | return ret; |
| 1824 | } |