1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2016,2017 ARM Limited, All Rights Reserved. 4 * Author: Marc Zyngier <marc.zyngier@arm.com> 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/pid.h> 12 #include <linux/sched.h> 13 14 #include <linux/irqchip/arm-gic-v4.h> 15 16 /* 17 * WARNING: The blurb below assumes that you understand the 18 * intricacies of GICv3, GICv4, and how a guest's view of a GICv3 gets 19 * translated into GICv4 commands. So it effectively targets at most 20 * two individuals. You know who you are. 21 * 22 * The core GICv4 code is designed to *avoid* exposing too much of the 23 * core GIC code (that would in turn leak into the hypervisor code), 24 * and instead provide a hypervisor agnostic interface to the HW (of 25 * course, the astute reader will quickly realize that hypervisor 26 * agnostic actually means KVM-specific - what were you thinking?). 27 * 28 * In order to achieve a modicum of isolation, we try to hide most of 29 * the GICv4 "stuff" behind normal irqchip operations: 30 * 31 * - Any guest-visible VLPI is backed by a Linux interrupt (and a 32 * physical LPI which gets unmapped when the guest maps the 33 * VLPI). This allows the same DevID/EventID pair to be either 34 * mapped to the LPI (host) or the VLPI (guest). Note that this is 35 * exclusive, and you cannot have both. 36 * 37 * - Enabling/disabling a VLPI is done by issuing mask/unmask calls. 38 * 39 * - Guest INT/CLEAR commands are implemented through 40 * irq_set_irqchip_state(). 41 * 42 * - The *bizarre* stuff (mapping/unmapping an interrupt to a VLPI, or 43 * issuing an INV after changing a priority) gets shoved into the 44 * irq_set_vcpu_affinity() method. While this is quite horrible 45 * (let's face it, this is the irqchip version of an ioctl), it 46 * confines the crap to a single location. And map/unmap really is 47 * about setting the affinity of a VLPI to a vcpu, so only INV is 48 * majorly out of place. So there. 49 * 50 * A number of commands are simply not provided by this interface, as 51 * they do not make direct sense. For example, MAPD is purely local to 52 * the virtual ITS (because it references a virtual device, and the 53 * physical ITS is still very much in charge of the physical 54 * device). Same goes for things like MAPC (the physical ITS deals 55 * with the actual vPE affinity, and not the braindead concept of 56 * collection). SYNC is not provided either, as each and every command 57 * is followed by a VSYNC. This could be relaxed in the future, should 58 * this be seen as a bottleneck (yes, this means *never*). 59 * 60 * But handling VLPIs is only one side of the job of the GICv4 61 * code. The other (darker) side is to take care of the doorbell 62 * interrupts which are delivered when a VLPI targeting a non-running 63 * vcpu is being made pending. 64 * 65 * The choice made here is that each vcpu (VPE in old northern GICv4 66 * dialect) gets a single doorbell LPI, no matter how many interrupts 67 * are targeting it. This has a nice property, which is that the 68 * interrupt becomes a handle for the VPE, and that the hypervisor 69 * code can manipulate it through the normal interrupt API: 70 * 71 * - VMs (or rather the VM abstraction that matters to the GIC) 72 * contain an irq domain where each interrupt maps to a VPE. In 73 * turn, this domain sits on top of the normal LPI allocator, and a 74 * specially crafted irq_chip implementation. 75 * 76 * - mask/unmask do what is expected on the doorbell interrupt. 77 * 78 * - irq_set_affinity is used to move a VPE from one redistributor to 79 * another. 80 * 81 * - irq_set_vcpu_affinity once again gets hijacked for the purpose of 82 * creating a new sub-API, namely scheduling/descheduling a VPE 83 * (which involves programming GICR_V{PROP,PEND}BASER) and 84 * performing INVALL operations. 85 */ 86 87 static struct irq_domain *gic_domain; 88 static const struct irq_domain_ops *vpe_domain_ops; 89 static const struct irq_domain_ops *sgi_domain_ops; 90 91 #ifdef CONFIG_ARM64 92 #include <asm/cpufeature.h> 93 94 bool gic_cpuif_has_vsgi(void) 95 { 96 unsigned long fld, reg = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1); 97 98 fld = cpuid_feature_extract_unsigned_field(reg, ID_AA64PFR0_EL1_GIC_SHIFT); 99 100 return fld >= ID_AA64PFR0_EL1_GIC_V4P1; 101 } 102 #else 103 bool gic_cpuif_has_vsgi(void) 104 { 105 return false; 106 } 107 #endif 108 109 static bool has_v4_1(void) 110 { 111 return !!sgi_domain_ops; 112 } 113 114 static bool has_v4_1_sgi(void) 115 { 116 return has_v4_1() && gic_cpuif_has_vsgi(); 117 } 118 119 static int its_alloc_vcpu_sgis(struct its_vpe *vpe, int idx) 120 { 121 char *name; 122 int sgi_base; 123 124 if (!has_v4_1_sgi()) 125 return 0; 126 127 name = kasprintf(GFP_KERNEL, "GICv4-sgi-%d", task_pid_nr(current)); 128 if (!name) 129 goto err; 130 131 vpe->fwnode = irq_domain_alloc_named_id_fwnode(name, idx); 132 if (!vpe->fwnode) 133 goto err; 134 135 kfree(name); 136 name = NULL; 137 138 vpe->sgi_domain = irq_domain_create_linear(vpe->fwnode, 16, 139 sgi_domain_ops, vpe); 140 if (!vpe->sgi_domain) 141 goto err; 142 143 sgi_base = irq_domain_alloc_irqs(vpe->sgi_domain, 16, NUMA_NO_NODE, vpe); 144 if (sgi_base <= 0) 145 goto err; 146 147 return 0; 148 149 err: 150 if (vpe->sgi_domain) { 151 irq_domain_remove(vpe->sgi_domain); 152 vpe->sgi_domain = NULL; 153 } 154 if (vpe->fwnode) { 155 irq_domain_free_fwnode(vpe->fwnode); 156 vpe->fwnode = NULL; 157 } 158 kfree(name); 159 return -ENOMEM; 160 } 161 162 static void its_free_sgi_irqs(struct its_vm *vm); 163 164 int its_alloc_vcpu_irqs(struct its_vm *vm) 165 { 166 static struct lock_class_key vpe_irq_lock_class; 167 static struct lock_class_key vpe_irq_request_class; 168 int vpe_base_irq, i; 169 170 vm->fwnode = irq_domain_alloc_named_id_fwnode("GICv4-vpe", 171 task_pid_nr(current)); 172 if (!vm->fwnode) 173 goto err; 174 175 vm->domain = irq_domain_create_hierarchy(gic_domain, 0, vm->nr_vpes, 176 vm->fwnode, vpe_domain_ops, 177 vm); 178 if (!vm->domain) 179 goto err; 180 181 for (i = 0; i < vm->nr_vpes; i++) { 182 vm->vpes[i]->its_vm = vm; 183 vm->vpes[i]->idai = true; 184 } 185 186 vpe_base_irq = irq_domain_alloc_irqs(vm->domain, vm->nr_vpes, 187 NUMA_NO_NODE, vm); 188 if (vpe_base_irq <= 0) 189 goto err; 190 191 for (i = 0; i < vm->nr_vpes; i++) { 192 int ret; 193 vm->vpes[i]->irq = vpe_base_irq + i; 194 irq_set_lockdep_class(vm->vpes[i]->irq, &vpe_irq_lock_class, 195 &vpe_irq_request_class); 196 irq_set_nothread(vm->vpes[i]->irq); 197 ret = its_alloc_vcpu_sgis(vm->vpes[i], i); 198 if (ret) 199 goto err_free_irqs; 200 } 201 202 return 0; 203 204 err_free_irqs: 205 its_free_sgi_irqs(vm); 206 irq_domain_free_irqs(vpe_base_irq, vm->nr_vpes); 207 err: 208 if (vm->domain) { 209 irq_domain_remove(vm->domain); 210 vm->domain = NULL; 211 } 212 if (vm->fwnode) { 213 irq_domain_free_fwnode(vm->fwnode); 214 vm->fwnode = NULL; 215 } 216 217 return -ENOMEM; 218 } 219 220 static void its_free_sgi_irqs(struct its_vm *vm) 221 { 222 int i; 223 224 if (!has_v4_1_sgi()) 225 return; 226 227 for (i = 0; i < vm->nr_vpes; i++) { 228 unsigned int irq; 229 230 /* irq_find_mapping() falls back to the default domain on NULL. */ 231 if (!vm->vpes[i]->sgi_domain) 232 continue; 233 234 irq = irq_find_mapping(vm->vpes[i]->sgi_domain, 0); 235 if (WARN_ON(!irq)) 236 continue; 237 238 irq_domain_free_irqs(irq, 16); 239 irq_domain_remove(vm->vpes[i]->sgi_domain); 240 irq_domain_free_fwnode(vm->vpes[i]->fwnode); 241 vm->vpes[i]->sgi_domain = NULL; 242 vm->vpes[i]->fwnode = NULL; 243 } 244 } 245 246 void its_free_vcpu_irqs(struct its_vm *vm) 247 { 248 its_free_sgi_irqs(vm); 249 irq_domain_free_irqs(vm->vpes[0]->irq, vm->nr_vpes); 250 irq_domain_remove(vm->domain); 251 irq_domain_free_fwnode(vm->fwnode); 252 vm->domain = NULL; 253 vm->fwnode = NULL; 254 } 255 256 static int its_send_vpe_cmd(struct its_vpe *vpe, struct its_cmd_info *info) 257 { 258 return irq_set_vcpu_affinity(vpe->irq, info); 259 } 260 261 int its_make_vpe_non_resident(struct its_vpe *vpe, bool db) 262 { 263 struct irq_desc *desc = irq_to_desc(vpe->irq); 264 struct its_cmd_info info = { }; 265 int ret; 266 267 WARN_ON(preemptible()); 268 269 info.cmd_type = DESCHEDULE_VPE; 270 if (has_v4_1()) { 271 /* GICv4.1 can directly deal with doorbells */ 272 info.req_db = db; 273 } else { 274 /* Undo the nested disable_irq() calls... */ 275 while (db && irqd_irq_disabled(&desc->irq_data)) 276 enable_irq(vpe->irq); 277 } 278 279 ret = its_send_vpe_cmd(vpe, &info); 280 if (!ret) 281 vpe->resident = false; 282 283 vpe->ready = false; 284 285 return ret; 286 } 287 288 int its_make_vpe_resident(struct its_vpe *vpe, bool g0en, bool g1en) 289 { 290 struct its_cmd_info info = { }; 291 int ret; 292 293 WARN_ON(preemptible()); 294 295 info.cmd_type = SCHEDULE_VPE; 296 if (has_v4_1()) { 297 info.g0en = g0en; 298 info.g1en = g1en; 299 } else { 300 /* Disabled the doorbell, as we're about to enter the guest */ 301 disable_irq_nosync(vpe->irq); 302 } 303 304 ret = its_send_vpe_cmd(vpe, &info); 305 if (!ret) 306 vpe->resident = true; 307 308 return ret; 309 } 310 311 int its_commit_vpe(struct its_vpe *vpe) 312 { 313 struct its_cmd_info info = { 314 .cmd_type = COMMIT_VPE, 315 }; 316 int ret; 317 318 WARN_ON(preemptible()); 319 320 ret = its_send_vpe_cmd(vpe, &info); 321 if (!ret) 322 vpe->ready = true; 323 324 return ret; 325 } 326 327 328 int its_invall_vpe(struct its_vpe *vpe) 329 { 330 struct its_cmd_info info = { 331 .cmd_type = INVALL_VPE, 332 }; 333 334 return its_send_vpe_cmd(vpe, &info); 335 } 336 337 int its_map_vlpi(int irq, struct its_vlpi_map *map) 338 { 339 struct its_cmd_info info = { 340 .cmd_type = MAP_VLPI, 341 { 342 .map = map, 343 }, 344 }; 345 int ret; 346 347 /* 348 * The host will never see that interrupt firing again, so it 349 * is vital that we don't do any lazy masking. 350 */ 351 irq_set_status_flags(irq, IRQ_DISABLE_UNLAZY); 352 353 ret = irq_set_vcpu_affinity(irq, &info); 354 if (ret) 355 irq_clear_status_flags(irq, IRQ_DISABLE_UNLAZY); 356 357 return ret; 358 } 359 360 int its_get_vlpi(int irq, struct its_vlpi_map *map) 361 { 362 struct its_cmd_info info = { 363 .cmd_type = GET_VLPI, 364 { 365 .map = map, 366 }, 367 }; 368 369 return irq_set_vcpu_affinity(irq, &info); 370 } 371 372 void its_unmap_vlpi(int irq) 373 { 374 irq_clear_status_flags(irq, IRQ_DISABLE_UNLAZY); 375 WARN_ON_ONCE(irq_set_vcpu_affinity(irq, NULL)); 376 } 377 378 int its_prop_update_vlpi(int irq, u8 config, bool inv) 379 { 380 struct its_cmd_info info = { 381 .cmd_type = inv ? PROP_UPDATE_AND_INV_VLPI : PROP_UPDATE_VLPI, 382 { 383 .config = config, 384 }, 385 }; 386 387 return irq_set_vcpu_affinity(irq, &info); 388 } 389 390 int its_prop_update_vsgi(int irq, u8 priority, bool group) 391 { 392 struct its_cmd_info info = { 393 .cmd_type = PROP_UPDATE_VSGI, 394 { 395 .priority = priority, 396 .group = group, 397 }, 398 }; 399 400 return irq_set_vcpu_affinity(irq, &info); 401 } 402 403 int its_init_v4(struct irq_domain *domain, 404 const struct irq_domain_ops *vpe_ops, 405 const struct irq_domain_ops *sgi_ops) 406 { 407 if (domain) { 408 pr_info("ITS: Enabling GICv4 support\n"); 409 gic_domain = domain; 410 vpe_domain_ops = vpe_ops; 411 sgi_domain_ops = sgi_ops; 412 return 0; 413 } 414 415 pr_err("ITS: No GICv4 VPE domain allocated\n"); 416 return -ENODEV; 417 } 418