Thomas Gleixner | caab277 | 2019-06-03 07:44:50 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Marc Zyngier | 7de5c0a | 2016-12-20 15:27:52 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016,2017 ARM Limited, All Rights Reserved. |
| 4 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
Marc Zyngier | 7de5c0a | 2016-12-20 15:27:52 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <linux/interrupt.h> |
| 8 | #include <linux/irq.h> |
| 9 | #include <linux/irqdomain.h> |
| 10 | #include <linux/msi.h> |
| 11 | #include <linux/sched.h> |
| 12 | |
| 13 | #include <linux/irqchip/arm-gic-v4.h> |
| 14 | |
Marc Zyngier | 7954907 | 2016-12-21 17:40:16 +0000 | [diff] [blame] | 15 | /* |
| 16 | * WARNING: The blurb below assumes that you understand the |
| 17 | * intricacies of GICv3, GICv4, and how a guest's view of a GICv3 gets |
| 18 | * translated into GICv4 commands. So it effectively targets at most |
| 19 | * two individuals. You know who you are. |
| 20 | * |
| 21 | * The core GICv4 code is designed to *avoid* exposing too much of the |
| 22 | * core GIC code (that would in turn leak into the hypervisor code), |
| 23 | * and instead provide a hypervisor agnostic interface to the HW (of |
| 24 | * course, the astute reader will quickly realize that hypervisor |
| 25 | * agnostic actually means KVM-specific - what were you thinking?). |
| 26 | * |
| 27 | * In order to achieve a modicum of isolation, we try to hide most of |
| 28 | * the GICv4 "stuff" behind normal irqchip operations: |
| 29 | * |
| 30 | * - Any guest-visible VLPI is backed by a Linux interrupt (and a |
| 31 | * physical LPI which gets unmapped when the guest maps the |
| 32 | * VLPI). This allows the same DevID/EventID pair to be either |
| 33 | * mapped to the LPI (host) or the VLPI (guest). Note that this is |
| 34 | * exclusive, and you cannot have both. |
| 35 | * |
| 36 | * - Enabling/disabling a VLPI is done by issuing mask/unmask calls. |
| 37 | * |
| 38 | * - Guest INT/CLEAR commands are implemented through |
| 39 | * irq_set_irqchip_state(). |
| 40 | * |
| 41 | * - The *bizarre* stuff (mapping/unmapping an interrupt to a VLPI, or |
| 42 | * issuing an INV after changing a priority) gets shoved into the |
| 43 | * irq_set_vcpu_affinity() method. While this is quite horrible |
| 44 | * (let's face it, this is the irqchip version of an ioctl), it |
| 45 | * confines the crap to a single location. And map/unmap really is |
| 46 | * about setting the affinity of a VLPI to a vcpu, so only INV is |
| 47 | * majorly out of place. So there. |
| 48 | * |
| 49 | * A number of commands are simply not provided by this interface, as |
| 50 | * they do not make direct sense. For example, MAPD is purely local to |
| 51 | * the virtual ITS (because it references a virtual device, and the |
| 52 | * physical ITS is still very much in charge of the physical |
| 53 | * device). Same goes for things like MAPC (the physical ITS deals |
| 54 | * with the actual vPE affinity, and not the braindead concept of |
| 55 | * collection). SYNC is not provided either, as each and every command |
| 56 | * is followed by a VSYNC. This could be relaxed in the future, should |
| 57 | * this be seen as a bottleneck (yes, this means *never*). |
| 58 | * |
| 59 | * But handling VLPIs is only one side of the job of the GICv4 |
| 60 | * code. The other (darker) side is to take care of the doorbell |
| 61 | * interrupts which are delivered when a VLPI targeting a non-running |
| 62 | * vcpu is being made pending. |
| 63 | * |
| 64 | * The choice made here is that each vcpu (VPE in old northern GICv4 |
| 65 | * dialect) gets a single doorbell LPI, no matter how many interrupts |
| 66 | * are targeting it. This has a nice property, which is that the |
| 67 | * interrupt becomes a handle for the VPE, and that the hypervisor |
| 68 | * code can manipulate it through the normal interrupt API: |
| 69 | * |
| 70 | * - VMs (or rather the VM abstraction that matters to the GIC) |
| 71 | * contain an irq domain where each interrupt maps to a VPE. In |
| 72 | * turn, this domain sits on top of the normal LPI allocator, and a |
| 73 | * specially crafted irq_chip implementation. |
| 74 | * |
| 75 | * - mask/unmask do what is expected on the doorbell interrupt. |
| 76 | * |
| 77 | * - irq_set_affinity is used to move a VPE from one redistributor to |
| 78 | * another. |
| 79 | * |
| 80 | * - irq_set_vcpu_affinity once again gets hijacked for the purpose of |
| 81 | * creating a new sub-API, namely scheduling/descheduling a VPE |
| 82 | * (which involves programming GICR_V{PROP,PEND}BASER) and |
| 83 | * performing INVALL operations. |
| 84 | */ |
| 85 | |
Marc Zyngier | 7de5c0a | 2016-12-20 15:27:52 +0000 | [diff] [blame] | 86 | static struct irq_domain *gic_domain; |
| 87 | static const struct irq_domain_ops *vpe_domain_ops; |
Marc Zyngier | 166cba7 | 2020-03-04 20:33:15 +0000 | [diff] [blame] | 88 | static const struct irq_domain_ops *sgi_domain_ops; |
Marc Zyngier | 7de5c0a | 2016-12-20 15:27:52 +0000 | [diff] [blame] | 89 | |
Marc Zyngier | ae699ad | 2020-03-04 20:33:20 +0000 | [diff] [blame^] | 90 | static bool has_v4_1(void) |
| 91 | { |
| 92 | return !!sgi_domain_ops; |
| 93 | } |
| 94 | |
Marc Zyngier | 7de5c0a | 2016-12-20 15:27:52 +0000 | [diff] [blame] | 95 | int its_alloc_vcpu_irqs(struct its_vm *vm) |
| 96 | { |
| 97 | int vpe_base_irq, i; |
| 98 | |
| 99 | vm->fwnode = irq_domain_alloc_named_id_fwnode("GICv4-vpe", |
| 100 | task_pid_nr(current)); |
| 101 | if (!vm->fwnode) |
| 102 | goto err; |
| 103 | |
| 104 | vm->domain = irq_domain_create_hierarchy(gic_domain, 0, vm->nr_vpes, |
| 105 | vm->fwnode, vpe_domain_ops, |
| 106 | vm); |
| 107 | if (!vm->domain) |
| 108 | goto err; |
| 109 | |
| 110 | for (i = 0; i < vm->nr_vpes; i++) { |
| 111 | vm->vpes[i]->its_vm = vm; |
| 112 | vm->vpes[i]->idai = true; |
| 113 | } |
| 114 | |
| 115 | vpe_base_irq = __irq_domain_alloc_irqs(vm->domain, -1, vm->nr_vpes, |
| 116 | NUMA_NO_NODE, vm, |
| 117 | false, NULL); |
| 118 | if (vpe_base_irq <= 0) |
| 119 | goto err; |
| 120 | |
| 121 | for (i = 0; i < vm->nr_vpes; i++) |
| 122 | vm->vpes[i]->irq = vpe_base_irq + i; |
| 123 | |
| 124 | return 0; |
| 125 | |
| 126 | err: |
| 127 | if (vm->domain) |
| 128 | irq_domain_remove(vm->domain); |
| 129 | if (vm->fwnode) |
| 130 | irq_domain_free_fwnode(vm->fwnode); |
| 131 | |
| 132 | return -ENOMEM; |
| 133 | } |
| 134 | |
| 135 | void its_free_vcpu_irqs(struct its_vm *vm) |
| 136 | { |
| 137 | irq_domain_free_irqs(vm->vpes[0]->irq, vm->nr_vpes); |
| 138 | irq_domain_remove(vm->domain); |
| 139 | irq_domain_free_fwnode(vm->fwnode); |
| 140 | } |
Marc Zyngier | eab8431 | 2016-12-20 15:31:02 +0000 | [diff] [blame] | 141 | |
| 142 | static int its_send_vpe_cmd(struct its_vpe *vpe, struct its_cmd_info *info) |
| 143 | { |
| 144 | return irq_set_vcpu_affinity(vpe->irq, info); |
| 145 | } |
| 146 | |
Marc Zyngier | ae699ad | 2020-03-04 20:33:20 +0000 | [diff] [blame^] | 147 | int its_make_vpe_non_resident(struct its_vpe *vpe, bool db) |
Marc Zyngier | eab8431 | 2016-12-20 15:31:02 +0000 | [diff] [blame] | 148 | { |
Marc Zyngier | ae699ad | 2020-03-04 20:33:20 +0000 | [diff] [blame^] | 149 | struct irq_desc *desc = irq_to_desc(vpe->irq); |
| 150 | struct its_cmd_info info = { }; |
Marc Zyngier | 8e01d9a | 2019-10-27 14:41:59 +0000 | [diff] [blame] | 151 | int ret; |
Marc Zyngier | eab8431 | 2016-12-20 15:31:02 +0000 | [diff] [blame] | 152 | |
| 153 | WARN_ON(preemptible()); |
| 154 | |
Marc Zyngier | ae699ad | 2020-03-04 20:33:20 +0000 | [diff] [blame^] | 155 | info.cmd_type = DESCHEDULE_VPE; |
| 156 | if (has_v4_1()) { |
| 157 | /* GICv4.1 can directly deal with doorbells */ |
| 158 | info.req_db = db; |
| 159 | } else { |
| 160 | /* Undo the nested disable_irq() calls... */ |
| 161 | while (db && irqd_irq_disabled(&desc->irq_data)) |
| 162 | enable_irq(vpe->irq); |
| 163 | } |
Marc Zyngier | eab8431 | 2016-12-20 15:31:02 +0000 | [diff] [blame] | 164 | |
Marc Zyngier | 8e01d9a | 2019-10-27 14:41:59 +0000 | [diff] [blame] | 165 | ret = its_send_vpe_cmd(vpe, &info); |
| 166 | if (!ret) |
Marc Zyngier | ae699ad | 2020-03-04 20:33:20 +0000 | [diff] [blame^] | 167 | vpe->resident = false; |
| 168 | |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | int its_make_vpe_resident(struct its_vpe *vpe, bool g0en, bool g1en) |
| 173 | { |
| 174 | struct its_cmd_info info = { }; |
| 175 | int ret; |
| 176 | |
| 177 | WARN_ON(preemptible()); |
| 178 | |
| 179 | info.cmd_type = SCHEDULE_VPE; |
| 180 | if (has_v4_1()) { |
| 181 | info.g0en = g0en; |
| 182 | info.g1en = g1en; |
| 183 | } else { |
| 184 | /* Disabled the doorbell, as we're about to enter the guest */ |
| 185 | disable_irq_nosync(vpe->irq); |
| 186 | } |
| 187 | |
| 188 | ret = its_send_vpe_cmd(vpe, &info); |
| 189 | if (!ret) |
| 190 | vpe->resident = true; |
Marc Zyngier | 8e01d9a | 2019-10-27 14:41:59 +0000 | [diff] [blame] | 191 | |
| 192 | return ret; |
Marc Zyngier | eab8431 | 2016-12-20 15:31:02 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | int its_invall_vpe(struct its_vpe *vpe) |
| 196 | { |
| 197 | struct its_cmd_info info = { |
| 198 | .cmd_type = INVALL_VPE, |
| 199 | }; |
| 200 | |
| 201 | return its_send_vpe_cmd(vpe, &info); |
| 202 | } |
Marc Zyngier | f2eac75 | 2016-12-21 21:50:32 +0000 | [diff] [blame] | 203 | |
| 204 | int its_map_vlpi(int irq, struct its_vlpi_map *map) |
| 205 | { |
| 206 | struct its_cmd_info info = { |
| 207 | .cmd_type = MAP_VLPI, |
Arnd Bergmann | 6c09ffd | 2017-09-12 22:08:23 +0200 | [diff] [blame] | 208 | { |
| 209 | .map = map, |
| 210 | }, |
Marc Zyngier | f2eac75 | 2016-12-21 21:50:32 +0000 | [diff] [blame] | 211 | }; |
Marc Zyngier | 90dc712 | 2017-11-10 09:00:41 +0000 | [diff] [blame] | 212 | int ret; |
Marc Zyngier | f2eac75 | 2016-12-21 21:50:32 +0000 | [diff] [blame] | 213 | |
| 214 | /* |
| 215 | * The host will never see that interrupt firing again, so it |
| 216 | * is vital that we don't do any lazy masking. |
| 217 | */ |
| 218 | irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY); |
| 219 | |
Marc Zyngier | 90dc712 | 2017-11-10 09:00:41 +0000 | [diff] [blame] | 220 | ret = irq_set_vcpu_affinity(irq, &info); |
| 221 | if (ret) |
| 222 | irq_clear_status_flags(irq, IRQ_DISABLE_UNLAZY); |
| 223 | |
| 224 | return ret; |
Marc Zyngier | f2eac75 | 2016-12-21 21:50:32 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | int its_get_vlpi(int irq, struct its_vlpi_map *map) |
| 228 | { |
| 229 | struct its_cmd_info info = { |
| 230 | .cmd_type = GET_VLPI, |
Arnd Bergmann | 6c09ffd | 2017-09-12 22:08:23 +0200 | [diff] [blame] | 231 | { |
| 232 | .map = map, |
| 233 | }, |
Marc Zyngier | f2eac75 | 2016-12-21 21:50:32 +0000 | [diff] [blame] | 234 | }; |
| 235 | |
| 236 | return irq_set_vcpu_affinity(irq, &info); |
| 237 | } |
| 238 | |
| 239 | int its_unmap_vlpi(int irq) |
| 240 | { |
| 241 | irq_clear_status_flags(irq, IRQ_DISABLE_UNLAZY); |
| 242 | return irq_set_vcpu_affinity(irq, NULL); |
| 243 | } |
| 244 | |
| 245 | int its_prop_update_vlpi(int irq, u8 config, bool inv) |
| 246 | { |
| 247 | struct its_cmd_info info = { |
| 248 | .cmd_type = inv ? PROP_UPDATE_AND_INV_VLPI : PROP_UPDATE_VLPI, |
Arnd Bergmann | 6c09ffd | 2017-09-12 22:08:23 +0200 | [diff] [blame] | 249 | { |
| 250 | .config = config, |
| 251 | }, |
Marc Zyngier | f2eac75 | 2016-12-21 21:50:32 +0000 | [diff] [blame] | 252 | }; |
| 253 | |
| 254 | return irq_set_vcpu_affinity(irq, &info); |
| 255 | } |
Marc Zyngier | 3d63cb5 | 2016-12-20 15:31:54 +0000 | [diff] [blame] | 256 | |
Marc Zyngier | 166cba7 | 2020-03-04 20:33:15 +0000 | [diff] [blame] | 257 | int its_init_v4(struct irq_domain *domain, |
| 258 | const struct irq_domain_ops *vpe_ops, |
| 259 | const struct irq_domain_ops *sgi_ops) |
Marc Zyngier | 3d63cb5 | 2016-12-20 15:31:54 +0000 | [diff] [blame] | 260 | { |
| 261 | if (domain) { |
| 262 | pr_info("ITS: Enabling GICv4 support\n"); |
| 263 | gic_domain = domain; |
Marc Zyngier | 166cba7 | 2020-03-04 20:33:15 +0000 | [diff] [blame] | 264 | vpe_domain_ops = vpe_ops; |
| 265 | sgi_domain_ops = sgi_ops; |
Marc Zyngier | 3d63cb5 | 2016-12-20 15:31:54 +0000 | [diff] [blame] | 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | pr_err("ITS: No GICv4 VPE domain allocated\n"); |
| 270 | return -ENODEV; |
| 271 | } |