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