1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved. 4 * Author: Marc Zyngier <marc.zyngier@arm.com> 5 */ 6 7 #include <linux/acpi.h> 8 #include <linux/acpi_iort.h> 9 #include <linux/bitfield.h> 10 #include <linux/bitmap.h> 11 #include <linux/cpu.h> 12 #include <linux/crash_dump.h> 13 #include <linux/delay.h> 14 #include <linux/dma-iommu.h> 15 #include <linux/efi.h> 16 #include <linux/interrupt.h> 17 #include <linux/iopoll.h> 18 #include <linux/irqdomain.h> 19 #include <linux/list.h> 20 #include <linux/log2.h> 21 #include <linux/memblock.h> 22 #include <linux/mm.h> 23 #include <linux/msi.h> 24 #include <linux/of.h> 25 #include <linux/of_address.h> 26 #include <linux/of_irq.h> 27 #include <linux/of_pci.h> 28 #include <linux/of_platform.h> 29 #include <linux/percpu.h> 30 #include <linux/slab.h> 31 #include <linux/syscore_ops.h> 32 33 #include <linux/irqchip.h> 34 #include <linux/irqchip/arm-gic-v3.h> 35 #include <linux/irqchip/arm-gic-v4.h> 36 37 #include <asm/cputype.h> 38 #include <asm/exception.h> 39 40 #include "irq-gic-common.h" 41 42 #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0) 43 #define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1) 44 #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2) 45 46 #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0) 47 #define RDIST_FLAGS_RD_TABLES_PREALLOCATED (1 << 1) 48 49 #define RD_LOCAL_LPI_ENABLED BIT(0) 50 51 static u32 lpi_id_bits; 52 53 /* 54 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to 55 * deal with (one configuration byte per interrupt). PENDBASE has to 56 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI). 57 */ 58 #define LPI_NRBITS lpi_id_bits 59 #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K) 60 #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K) 61 62 #define LPI_PROP_DEFAULT_PRIO GICD_INT_DEF_PRI 63 64 /* 65 * Collection structure - just an ID, and a redistributor address to 66 * ping. We use one per CPU as a bag of interrupts assigned to this 67 * CPU. 68 */ 69 struct its_collection { 70 u64 target_address; 71 u16 col_id; 72 }; 73 74 /* 75 * The ITS_BASER structure - contains memory information, cached 76 * value of BASER register configuration and ITS page size. 77 */ 78 struct its_baser { 79 void *base; 80 u64 val; 81 u32 order; 82 u32 psz; 83 }; 84 85 struct its_device; 86 87 /* 88 * The ITS structure - contains most of the infrastructure, with the 89 * top-level MSI domain, the command queue, the collections, and the 90 * list of devices writing to it. 91 * 92 * dev_alloc_lock has to be taken for device allocations, while the 93 * spinlock must be taken to parse data structures such as the device 94 * list. 95 */ 96 struct its_node { 97 raw_spinlock_t lock; 98 struct mutex dev_alloc_lock; 99 struct list_head entry; 100 void __iomem *base; 101 void __iomem *sgir_base; 102 phys_addr_t phys_base; 103 struct its_cmd_block *cmd_base; 104 struct its_cmd_block *cmd_write; 105 struct its_baser tables[GITS_BASER_NR_REGS]; 106 struct its_collection *collections; 107 struct fwnode_handle *fwnode_handle; 108 u64 (*get_msi_base)(struct its_device *its_dev); 109 u64 typer; 110 u64 cbaser_save; 111 u32 ctlr_save; 112 u32 mpidr; 113 struct list_head its_device_list; 114 u64 flags; 115 unsigned long list_nr; 116 int numa_node; 117 unsigned int msi_domain_flags; 118 u32 pre_its_base; /* for Socionext Synquacer */ 119 int vlpi_redist_offset; 120 }; 121 122 #define is_v4(its) (!!((its)->typer & GITS_TYPER_VLPIS)) 123 #define is_v4_1(its) (!!((its)->typer & GITS_TYPER_VMAPP)) 124 #define device_ids(its) (FIELD_GET(GITS_TYPER_DEVBITS, (its)->typer) + 1) 125 126 #define ITS_ITT_ALIGN SZ_256 127 128 /* The maximum number of VPEID bits supported by VLPI commands */ 129 #define ITS_MAX_VPEID_BITS \ 130 ({ \ 131 int nvpeid = 16; \ 132 if (gic_rdists->has_rvpeid && \ 133 gic_rdists->gicd_typer2 & GICD_TYPER2_VIL) \ 134 nvpeid = 1 + (gic_rdists->gicd_typer2 & \ 135 GICD_TYPER2_VID); \ 136 \ 137 nvpeid; \ 138 }) 139 #define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS)) 140 141 /* Convert page order to size in bytes */ 142 #define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o)) 143 144 struct event_lpi_map { 145 unsigned long *lpi_map; 146 u16 *col_map; 147 irq_hw_number_t lpi_base; 148 int nr_lpis; 149 raw_spinlock_t vlpi_lock; 150 struct its_vm *vm; 151 struct its_vlpi_map *vlpi_maps; 152 int nr_vlpis; 153 }; 154 155 /* 156 * The ITS view of a device - belongs to an ITS, owns an interrupt 157 * translation table, and a list of interrupts. If it some of its 158 * LPIs are injected into a guest (GICv4), the event_map.vm field 159 * indicates which one. 160 */ 161 struct its_device { 162 struct list_head entry; 163 struct its_node *its; 164 struct event_lpi_map event_map; 165 void *itt; 166 u32 nr_ites; 167 u32 device_id; 168 bool shared; 169 }; 170 171 static struct { 172 raw_spinlock_t lock; 173 struct its_device *dev; 174 struct its_vpe **vpes; 175 int next_victim; 176 } vpe_proxy; 177 178 struct cpu_lpi_count { 179 atomic_t managed; 180 atomic_t unmanaged; 181 }; 182 183 static DEFINE_PER_CPU(struct cpu_lpi_count, cpu_lpi_count); 184 185 static LIST_HEAD(its_nodes); 186 static DEFINE_RAW_SPINLOCK(its_lock); 187 static struct rdists *gic_rdists; 188 static struct irq_domain *its_parent; 189 190 static unsigned long its_list_map; 191 static u16 vmovp_seq_num; 192 static DEFINE_RAW_SPINLOCK(vmovp_lock); 193 194 static DEFINE_IDA(its_vpeid_ida); 195 196 #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist)) 197 #define gic_data_rdist_cpu(cpu) (per_cpu_ptr(gic_rdists->rdist, cpu)) 198 #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base) 199 #define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K) 200 201 /* 202 * Skip ITSs that have no vLPIs mapped, unless we're on GICv4.1, as we 203 * always have vSGIs mapped. 204 */ 205 static bool require_its_list_vmovp(struct its_vm *vm, struct its_node *its) 206 { 207 return (gic_rdists->has_rvpeid || vm->vlpi_count[its->list_nr]); 208 } 209 210 static u16 get_its_list(struct its_vm *vm) 211 { 212 struct its_node *its; 213 unsigned long its_list = 0; 214 215 list_for_each_entry(its, &its_nodes, entry) { 216 if (!is_v4(its)) 217 continue; 218 219 if (require_its_list_vmovp(vm, its)) 220 __set_bit(its->list_nr, &its_list); 221 } 222 223 return (u16)its_list; 224 } 225 226 static inline u32 its_get_event_id(struct irq_data *d) 227 { 228 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 229 return d->hwirq - its_dev->event_map.lpi_base; 230 } 231 232 static struct its_collection *dev_event_to_col(struct its_device *its_dev, 233 u32 event) 234 { 235 struct its_node *its = its_dev->its; 236 237 return its->collections + its_dev->event_map.col_map[event]; 238 } 239 240 static struct its_vlpi_map *dev_event_to_vlpi_map(struct its_device *its_dev, 241 u32 event) 242 { 243 if (WARN_ON_ONCE(event >= its_dev->event_map.nr_lpis)) 244 return NULL; 245 246 return &its_dev->event_map.vlpi_maps[event]; 247 } 248 249 static struct its_vlpi_map *get_vlpi_map(struct irq_data *d) 250 { 251 if (irqd_is_forwarded_to_vcpu(d)) { 252 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 253 u32 event = its_get_event_id(d); 254 255 return dev_event_to_vlpi_map(its_dev, event); 256 } 257 258 return NULL; 259 } 260 261 static int vpe_to_cpuid_lock(struct its_vpe *vpe, unsigned long *flags) 262 { 263 raw_spin_lock_irqsave(&vpe->vpe_lock, *flags); 264 return vpe->col_idx; 265 } 266 267 static void vpe_to_cpuid_unlock(struct its_vpe *vpe, unsigned long flags) 268 { 269 raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags); 270 } 271 272 static int irq_to_cpuid_lock(struct irq_data *d, unsigned long *flags) 273 { 274 struct its_vlpi_map *map = get_vlpi_map(d); 275 int cpu; 276 277 if (map) { 278 cpu = vpe_to_cpuid_lock(map->vpe, flags); 279 } else { 280 /* Physical LPIs are already locked via the irq_desc lock */ 281 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 282 cpu = its_dev->event_map.col_map[its_get_event_id(d)]; 283 /* Keep GCC quiet... */ 284 *flags = 0; 285 } 286 287 return cpu; 288 } 289 290 static void irq_to_cpuid_unlock(struct irq_data *d, unsigned long flags) 291 { 292 struct its_vlpi_map *map = get_vlpi_map(d); 293 294 if (map) 295 vpe_to_cpuid_unlock(map->vpe, flags); 296 } 297 298 static struct its_collection *valid_col(struct its_collection *col) 299 { 300 if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(15, 0))) 301 return NULL; 302 303 return col; 304 } 305 306 static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe) 307 { 308 if (valid_col(its->collections + vpe->col_idx)) 309 return vpe; 310 311 return NULL; 312 } 313 314 /* 315 * ITS command descriptors - parameters to be encoded in a command 316 * block. 317 */ 318 struct its_cmd_desc { 319 union { 320 struct { 321 struct its_device *dev; 322 u32 event_id; 323 } its_inv_cmd; 324 325 struct { 326 struct its_device *dev; 327 u32 event_id; 328 } its_clear_cmd; 329 330 struct { 331 struct its_device *dev; 332 u32 event_id; 333 } its_int_cmd; 334 335 struct { 336 struct its_device *dev; 337 int valid; 338 } its_mapd_cmd; 339 340 struct { 341 struct its_collection *col; 342 int valid; 343 } its_mapc_cmd; 344 345 struct { 346 struct its_device *dev; 347 u32 phys_id; 348 u32 event_id; 349 } its_mapti_cmd; 350 351 struct { 352 struct its_device *dev; 353 struct its_collection *col; 354 u32 event_id; 355 } its_movi_cmd; 356 357 struct { 358 struct its_device *dev; 359 u32 event_id; 360 } its_discard_cmd; 361 362 struct { 363 struct its_collection *col; 364 } its_invall_cmd; 365 366 struct { 367 struct its_vpe *vpe; 368 } its_vinvall_cmd; 369 370 struct { 371 struct its_vpe *vpe; 372 struct its_collection *col; 373 bool valid; 374 } its_vmapp_cmd; 375 376 struct { 377 struct its_vpe *vpe; 378 struct its_device *dev; 379 u32 virt_id; 380 u32 event_id; 381 bool db_enabled; 382 } its_vmapti_cmd; 383 384 struct { 385 struct its_vpe *vpe; 386 struct its_device *dev; 387 u32 event_id; 388 bool db_enabled; 389 } its_vmovi_cmd; 390 391 struct { 392 struct its_vpe *vpe; 393 struct its_collection *col; 394 u16 seq_num; 395 u16 its_list; 396 } its_vmovp_cmd; 397 398 struct { 399 struct its_vpe *vpe; 400 } its_invdb_cmd; 401 402 struct { 403 struct its_vpe *vpe; 404 u8 sgi; 405 u8 priority; 406 bool enable; 407 bool group; 408 bool clear; 409 } its_vsgi_cmd; 410 }; 411 }; 412 413 /* 414 * The ITS command block, which is what the ITS actually parses. 415 */ 416 struct its_cmd_block { 417 union { 418 u64 raw_cmd[4]; 419 __le64 raw_cmd_le[4]; 420 }; 421 }; 422 423 #define ITS_CMD_QUEUE_SZ SZ_64K 424 #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block)) 425 426 typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *, 427 struct its_cmd_block *, 428 struct its_cmd_desc *); 429 430 typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *, 431 struct its_cmd_block *, 432 struct its_cmd_desc *); 433 434 static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l) 435 { 436 u64 mask = GENMASK_ULL(h, l); 437 *raw_cmd &= ~mask; 438 *raw_cmd |= (val << l) & mask; 439 } 440 441 static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr) 442 { 443 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0); 444 } 445 446 static void its_encode_devid(struct its_cmd_block *cmd, u32 devid) 447 { 448 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32); 449 } 450 451 static void its_encode_event_id(struct its_cmd_block *cmd, u32 id) 452 { 453 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0); 454 } 455 456 static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id) 457 { 458 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32); 459 } 460 461 static void its_encode_size(struct its_cmd_block *cmd, u8 size) 462 { 463 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0); 464 } 465 466 static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr) 467 { 468 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8); 469 } 470 471 static void its_encode_valid(struct its_cmd_block *cmd, int valid) 472 { 473 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63); 474 } 475 476 static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr) 477 { 478 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16); 479 } 480 481 static void its_encode_collection(struct its_cmd_block *cmd, u16 col) 482 { 483 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0); 484 } 485 486 static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid) 487 { 488 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32); 489 } 490 491 static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id) 492 { 493 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0); 494 } 495 496 static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id) 497 { 498 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32); 499 } 500 501 static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid) 502 { 503 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0); 504 } 505 506 static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num) 507 { 508 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32); 509 } 510 511 static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list) 512 { 513 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0); 514 } 515 516 static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa) 517 { 518 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16); 519 } 520 521 static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size) 522 { 523 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0); 524 } 525 526 static void its_encode_vconf_addr(struct its_cmd_block *cmd, u64 vconf_pa) 527 { 528 its_mask_encode(&cmd->raw_cmd[0], vconf_pa >> 16, 51, 16); 529 } 530 531 static void its_encode_alloc(struct its_cmd_block *cmd, bool alloc) 532 { 533 its_mask_encode(&cmd->raw_cmd[0], alloc, 8, 8); 534 } 535 536 static void its_encode_ptz(struct its_cmd_block *cmd, bool ptz) 537 { 538 its_mask_encode(&cmd->raw_cmd[0], ptz, 9, 9); 539 } 540 541 static void its_encode_vmapp_default_db(struct its_cmd_block *cmd, 542 u32 vpe_db_lpi) 543 { 544 its_mask_encode(&cmd->raw_cmd[1], vpe_db_lpi, 31, 0); 545 } 546 547 static void its_encode_vmovp_default_db(struct its_cmd_block *cmd, 548 u32 vpe_db_lpi) 549 { 550 its_mask_encode(&cmd->raw_cmd[3], vpe_db_lpi, 31, 0); 551 } 552 553 static void its_encode_db(struct its_cmd_block *cmd, bool db) 554 { 555 its_mask_encode(&cmd->raw_cmd[2], db, 63, 63); 556 } 557 558 static void its_encode_sgi_intid(struct its_cmd_block *cmd, u8 sgi) 559 { 560 its_mask_encode(&cmd->raw_cmd[0], sgi, 35, 32); 561 } 562 563 static void its_encode_sgi_priority(struct its_cmd_block *cmd, u8 prio) 564 { 565 its_mask_encode(&cmd->raw_cmd[0], prio >> 4, 23, 20); 566 } 567 568 static void its_encode_sgi_group(struct its_cmd_block *cmd, bool grp) 569 { 570 its_mask_encode(&cmd->raw_cmd[0], grp, 10, 10); 571 } 572 573 static void its_encode_sgi_clear(struct its_cmd_block *cmd, bool clr) 574 { 575 its_mask_encode(&cmd->raw_cmd[0], clr, 9, 9); 576 } 577 578 static void its_encode_sgi_enable(struct its_cmd_block *cmd, bool en) 579 { 580 its_mask_encode(&cmd->raw_cmd[0], en, 8, 8); 581 } 582 583 static inline void its_fixup_cmd(struct its_cmd_block *cmd) 584 { 585 /* Let's fixup BE commands */ 586 cmd->raw_cmd_le[0] = cpu_to_le64(cmd->raw_cmd[0]); 587 cmd->raw_cmd_le[1] = cpu_to_le64(cmd->raw_cmd[1]); 588 cmd->raw_cmd_le[2] = cpu_to_le64(cmd->raw_cmd[2]); 589 cmd->raw_cmd_le[3] = cpu_to_le64(cmd->raw_cmd[3]); 590 } 591 592 static struct its_collection *its_build_mapd_cmd(struct its_node *its, 593 struct its_cmd_block *cmd, 594 struct its_cmd_desc *desc) 595 { 596 unsigned long itt_addr; 597 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites); 598 599 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt); 600 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN); 601 602 its_encode_cmd(cmd, GITS_CMD_MAPD); 603 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id); 604 its_encode_size(cmd, size - 1); 605 its_encode_itt(cmd, itt_addr); 606 its_encode_valid(cmd, desc->its_mapd_cmd.valid); 607 608 its_fixup_cmd(cmd); 609 610 return NULL; 611 } 612 613 static struct its_collection *its_build_mapc_cmd(struct its_node *its, 614 struct its_cmd_block *cmd, 615 struct its_cmd_desc *desc) 616 { 617 its_encode_cmd(cmd, GITS_CMD_MAPC); 618 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id); 619 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address); 620 its_encode_valid(cmd, desc->its_mapc_cmd.valid); 621 622 its_fixup_cmd(cmd); 623 624 return desc->its_mapc_cmd.col; 625 } 626 627 static struct its_collection *its_build_mapti_cmd(struct its_node *its, 628 struct its_cmd_block *cmd, 629 struct its_cmd_desc *desc) 630 { 631 struct its_collection *col; 632 633 col = dev_event_to_col(desc->its_mapti_cmd.dev, 634 desc->its_mapti_cmd.event_id); 635 636 its_encode_cmd(cmd, GITS_CMD_MAPTI); 637 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id); 638 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id); 639 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id); 640 its_encode_collection(cmd, col->col_id); 641 642 its_fixup_cmd(cmd); 643 644 return valid_col(col); 645 } 646 647 static struct its_collection *its_build_movi_cmd(struct its_node *its, 648 struct its_cmd_block *cmd, 649 struct its_cmd_desc *desc) 650 { 651 struct its_collection *col; 652 653 col = dev_event_to_col(desc->its_movi_cmd.dev, 654 desc->its_movi_cmd.event_id); 655 656 its_encode_cmd(cmd, GITS_CMD_MOVI); 657 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id); 658 its_encode_event_id(cmd, desc->its_movi_cmd.event_id); 659 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id); 660 661 its_fixup_cmd(cmd); 662 663 return valid_col(col); 664 } 665 666 static struct its_collection *its_build_discard_cmd(struct its_node *its, 667 struct its_cmd_block *cmd, 668 struct its_cmd_desc *desc) 669 { 670 struct its_collection *col; 671 672 col = dev_event_to_col(desc->its_discard_cmd.dev, 673 desc->its_discard_cmd.event_id); 674 675 its_encode_cmd(cmd, GITS_CMD_DISCARD); 676 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id); 677 its_encode_event_id(cmd, desc->its_discard_cmd.event_id); 678 679 its_fixup_cmd(cmd); 680 681 return valid_col(col); 682 } 683 684 static struct its_collection *its_build_inv_cmd(struct its_node *its, 685 struct its_cmd_block *cmd, 686 struct its_cmd_desc *desc) 687 { 688 struct its_collection *col; 689 690 col = dev_event_to_col(desc->its_inv_cmd.dev, 691 desc->its_inv_cmd.event_id); 692 693 its_encode_cmd(cmd, GITS_CMD_INV); 694 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id); 695 its_encode_event_id(cmd, desc->its_inv_cmd.event_id); 696 697 its_fixup_cmd(cmd); 698 699 return valid_col(col); 700 } 701 702 static struct its_collection *its_build_int_cmd(struct its_node *its, 703 struct its_cmd_block *cmd, 704 struct its_cmd_desc *desc) 705 { 706 struct its_collection *col; 707 708 col = dev_event_to_col(desc->its_int_cmd.dev, 709 desc->its_int_cmd.event_id); 710 711 its_encode_cmd(cmd, GITS_CMD_INT); 712 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id); 713 its_encode_event_id(cmd, desc->its_int_cmd.event_id); 714 715 its_fixup_cmd(cmd); 716 717 return valid_col(col); 718 } 719 720 static struct its_collection *its_build_clear_cmd(struct its_node *its, 721 struct its_cmd_block *cmd, 722 struct its_cmd_desc *desc) 723 { 724 struct its_collection *col; 725 726 col = dev_event_to_col(desc->its_clear_cmd.dev, 727 desc->its_clear_cmd.event_id); 728 729 its_encode_cmd(cmd, GITS_CMD_CLEAR); 730 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id); 731 its_encode_event_id(cmd, desc->its_clear_cmd.event_id); 732 733 its_fixup_cmd(cmd); 734 735 return valid_col(col); 736 } 737 738 static struct its_collection *its_build_invall_cmd(struct its_node *its, 739 struct its_cmd_block *cmd, 740 struct its_cmd_desc *desc) 741 { 742 its_encode_cmd(cmd, GITS_CMD_INVALL); 743 its_encode_collection(cmd, desc->its_invall_cmd.col->col_id); 744 745 its_fixup_cmd(cmd); 746 747 return NULL; 748 } 749 750 static struct its_vpe *its_build_vinvall_cmd(struct its_node *its, 751 struct its_cmd_block *cmd, 752 struct its_cmd_desc *desc) 753 { 754 its_encode_cmd(cmd, GITS_CMD_VINVALL); 755 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id); 756 757 its_fixup_cmd(cmd); 758 759 return valid_vpe(its, desc->its_vinvall_cmd.vpe); 760 } 761 762 static struct its_vpe *its_build_vmapp_cmd(struct its_node *its, 763 struct its_cmd_block *cmd, 764 struct its_cmd_desc *desc) 765 { 766 unsigned long vpt_addr, vconf_addr; 767 u64 target; 768 bool alloc; 769 770 its_encode_cmd(cmd, GITS_CMD_VMAPP); 771 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id); 772 its_encode_valid(cmd, desc->its_vmapp_cmd.valid); 773 774 if (!desc->its_vmapp_cmd.valid) { 775 if (is_v4_1(its)) { 776 alloc = !atomic_dec_return(&desc->its_vmapp_cmd.vpe->vmapp_count); 777 its_encode_alloc(cmd, alloc); 778 } 779 780 goto out; 781 } 782 783 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page)); 784 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset; 785 786 its_encode_target(cmd, target); 787 its_encode_vpt_addr(cmd, vpt_addr); 788 its_encode_vpt_size(cmd, LPI_NRBITS - 1); 789 790 if (!is_v4_1(its)) 791 goto out; 792 793 vconf_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->its_vm->vprop_page)); 794 795 alloc = !atomic_fetch_inc(&desc->its_vmapp_cmd.vpe->vmapp_count); 796 797 its_encode_alloc(cmd, alloc); 798 799 /* 800 * GICv4.1 provides a way to get the VLPI state, which needs the vPE 801 * to be unmapped first, and in this case, we may remap the vPE 802 * back while the VPT is not empty. So we can't assume that the 803 * VPT is empty on map. This is why we never advertise PTZ. 804 */ 805 its_encode_ptz(cmd, false); 806 its_encode_vconf_addr(cmd, vconf_addr); 807 its_encode_vmapp_default_db(cmd, desc->its_vmapp_cmd.vpe->vpe_db_lpi); 808 809 out: 810 its_fixup_cmd(cmd); 811 812 return valid_vpe(its, desc->its_vmapp_cmd.vpe); 813 } 814 815 static struct its_vpe *its_build_vmapti_cmd(struct its_node *its, 816 struct its_cmd_block *cmd, 817 struct its_cmd_desc *desc) 818 { 819 u32 db; 820 821 if (!is_v4_1(its) && desc->its_vmapti_cmd.db_enabled) 822 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi; 823 else 824 db = 1023; 825 826 its_encode_cmd(cmd, GITS_CMD_VMAPTI); 827 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id); 828 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id); 829 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id); 830 its_encode_db_phys_id(cmd, db); 831 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id); 832 833 its_fixup_cmd(cmd); 834 835 return valid_vpe(its, desc->its_vmapti_cmd.vpe); 836 } 837 838 static struct its_vpe *its_build_vmovi_cmd(struct its_node *its, 839 struct its_cmd_block *cmd, 840 struct its_cmd_desc *desc) 841 { 842 u32 db; 843 844 if (!is_v4_1(its) && desc->its_vmovi_cmd.db_enabled) 845 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi; 846 else 847 db = 1023; 848 849 its_encode_cmd(cmd, GITS_CMD_VMOVI); 850 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id); 851 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id); 852 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id); 853 its_encode_db_phys_id(cmd, db); 854 its_encode_db_valid(cmd, true); 855 856 its_fixup_cmd(cmd); 857 858 return valid_vpe(its, desc->its_vmovi_cmd.vpe); 859 } 860 861 static struct its_vpe *its_build_vmovp_cmd(struct its_node *its, 862 struct its_cmd_block *cmd, 863 struct its_cmd_desc *desc) 864 { 865 u64 target; 866 867 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset; 868 its_encode_cmd(cmd, GITS_CMD_VMOVP); 869 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num); 870 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list); 871 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id); 872 its_encode_target(cmd, target); 873 874 if (is_v4_1(its)) { 875 its_encode_db(cmd, true); 876 its_encode_vmovp_default_db(cmd, desc->its_vmovp_cmd.vpe->vpe_db_lpi); 877 } 878 879 its_fixup_cmd(cmd); 880 881 return valid_vpe(its, desc->its_vmovp_cmd.vpe); 882 } 883 884 static struct its_vpe *its_build_vinv_cmd(struct its_node *its, 885 struct its_cmd_block *cmd, 886 struct its_cmd_desc *desc) 887 { 888 struct its_vlpi_map *map; 889 890 map = dev_event_to_vlpi_map(desc->its_inv_cmd.dev, 891 desc->its_inv_cmd.event_id); 892 893 its_encode_cmd(cmd, GITS_CMD_INV); 894 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id); 895 its_encode_event_id(cmd, desc->its_inv_cmd.event_id); 896 897 its_fixup_cmd(cmd); 898 899 return valid_vpe(its, map->vpe); 900 } 901 902 static struct its_vpe *its_build_vint_cmd(struct its_node *its, 903 struct its_cmd_block *cmd, 904 struct its_cmd_desc *desc) 905 { 906 struct its_vlpi_map *map; 907 908 map = dev_event_to_vlpi_map(desc->its_int_cmd.dev, 909 desc->its_int_cmd.event_id); 910 911 its_encode_cmd(cmd, GITS_CMD_INT); 912 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id); 913 its_encode_event_id(cmd, desc->its_int_cmd.event_id); 914 915 its_fixup_cmd(cmd); 916 917 return valid_vpe(its, map->vpe); 918 } 919 920 static struct its_vpe *its_build_vclear_cmd(struct its_node *its, 921 struct its_cmd_block *cmd, 922 struct its_cmd_desc *desc) 923 { 924 struct its_vlpi_map *map; 925 926 map = dev_event_to_vlpi_map(desc->its_clear_cmd.dev, 927 desc->its_clear_cmd.event_id); 928 929 its_encode_cmd(cmd, GITS_CMD_CLEAR); 930 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id); 931 its_encode_event_id(cmd, desc->its_clear_cmd.event_id); 932 933 its_fixup_cmd(cmd); 934 935 return valid_vpe(its, map->vpe); 936 } 937 938 static struct its_vpe *its_build_invdb_cmd(struct its_node *its, 939 struct its_cmd_block *cmd, 940 struct its_cmd_desc *desc) 941 { 942 if (WARN_ON(!is_v4_1(its))) 943 return NULL; 944 945 its_encode_cmd(cmd, GITS_CMD_INVDB); 946 its_encode_vpeid(cmd, desc->its_invdb_cmd.vpe->vpe_id); 947 948 its_fixup_cmd(cmd); 949 950 return valid_vpe(its, desc->its_invdb_cmd.vpe); 951 } 952 953 static struct its_vpe *its_build_vsgi_cmd(struct its_node *its, 954 struct its_cmd_block *cmd, 955 struct its_cmd_desc *desc) 956 { 957 if (WARN_ON(!is_v4_1(its))) 958 return NULL; 959 960 its_encode_cmd(cmd, GITS_CMD_VSGI); 961 its_encode_vpeid(cmd, desc->its_vsgi_cmd.vpe->vpe_id); 962 its_encode_sgi_intid(cmd, desc->its_vsgi_cmd.sgi); 963 its_encode_sgi_priority(cmd, desc->its_vsgi_cmd.priority); 964 its_encode_sgi_group(cmd, desc->its_vsgi_cmd.group); 965 its_encode_sgi_clear(cmd, desc->its_vsgi_cmd.clear); 966 its_encode_sgi_enable(cmd, desc->its_vsgi_cmd.enable); 967 968 its_fixup_cmd(cmd); 969 970 return valid_vpe(its, desc->its_vsgi_cmd.vpe); 971 } 972 973 static u64 its_cmd_ptr_to_offset(struct its_node *its, 974 struct its_cmd_block *ptr) 975 { 976 return (ptr - its->cmd_base) * sizeof(*ptr); 977 } 978 979 static int its_queue_full(struct its_node *its) 980 { 981 int widx; 982 int ridx; 983 984 widx = its->cmd_write - its->cmd_base; 985 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block); 986 987 /* This is incredibly unlikely to happen, unless the ITS locks up. */ 988 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx) 989 return 1; 990 991 return 0; 992 } 993 994 static struct its_cmd_block *its_allocate_entry(struct its_node *its) 995 { 996 struct its_cmd_block *cmd; 997 u32 count = 1000000; /* 1s! */ 998 999 while (its_queue_full(its)) { 1000 count--; 1001 if (!count) { 1002 pr_err_ratelimited("ITS queue not draining\n"); 1003 return NULL; 1004 } 1005 cpu_relax(); 1006 udelay(1); 1007 } 1008 1009 cmd = its->cmd_write++; 1010 1011 /* Handle queue wrapping */ 1012 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES)) 1013 its->cmd_write = its->cmd_base; 1014 1015 /* Clear command */ 1016 cmd->raw_cmd[0] = 0; 1017 cmd->raw_cmd[1] = 0; 1018 cmd->raw_cmd[2] = 0; 1019 cmd->raw_cmd[3] = 0; 1020 1021 return cmd; 1022 } 1023 1024 static struct its_cmd_block *its_post_commands(struct its_node *its) 1025 { 1026 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write); 1027 1028 writel_relaxed(wr, its->base + GITS_CWRITER); 1029 1030 return its->cmd_write; 1031 } 1032 1033 static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd) 1034 { 1035 /* 1036 * Make sure the commands written to memory are observable by 1037 * the ITS. 1038 */ 1039 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING) 1040 gic_flush_dcache_to_poc(cmd, sizeof(*cmd)); 1041 else 1042 dsb(ishst); 1043 } 1044 1045 static int its_wait_for_range_completion(struct its_node *its, 1046 u64 prev_idx, 1047 struct its_cmd_block *to) 1048 { 1049 u64 rd_idx, to_idx, linear_idx; 1050 u32 count = 1000000; /* 1s! */ 1051 1052 /* Linearize to_idx if the command set has wrapped around */ 1053 to_idx = its_cmd_ptr_to_offset(its, to); 1054 if (to_idx < prev_idx) 1055 to_idx += ITS_CMD_QUEUE_SZ; 1056 1057 linear_idx = prev_idx; 1058 1059 while (1) { 1060 s64 delta; 1061 1062 rd_idx = readl_relaxed(its->base + GITS_CREADR); 1063 1064 /* 1065 * Compute the read pointer progress, taking the 1066 * potential wrap-around into account. 1067 */ 1068 delta = rd_idx - prev_idx; 1069 if (rd_idx < prev_idx) 1070 delta += ITS_CMD_QUEUE_SZ; 1071 1072 linear_idx += delta; 1073 if (linear_idx >= to_idx) 1074 break; 1075 1076 count--; 1077 if (!count) { 1078 pr_err_ratelimited("ITS queue timeout (%llu %llu)\n", 1079 to_idx, linear_idx); 1080 return -1; 1081 } 1082 prev_idx = rd_idx; 1083 cpu_relax(); 1084 udelay(1); 1085 } 1086 1087 return 0; 1088 } 1089 1090 /* Warning, macro hell follows */ 1091 #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \ 1092 void name(struct its_node *its, \ 1093 buildtype builder, \ 1094 struct its_cmd_desc *desc) \ 1095 { \ 1096 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \ 1097 synctype *sync_obj; \ 1098 unsigned long flags; \ 1099 u64 rd_idx; \ 1100 \ 1101 raw_spin_lock_irqsave(&its->lock, flags); \ 1102 \ 1103 cmd = its_allocate_entry(its); \ 1104 if (!cmd) { /* We're soooooo screewed... */ \ 1105 raw_spin_unlock_irqrestore(&its->lock, flags); \ 1106 return; \ 1107 } \ 1108 sync_obj = builder(its, cmd, desc); \ 1109 its_flush_cmd(its, cmd); \ 1110 \ 1111 if (sync_obj) { \ 1112 sync_cmd = its_allocate_entry(its); \ 1113 if (!sync_cmd) \ 1114 goto post; \ 1115 \ 1116 buildfn(its, sync_cmd, sync_obj); \ 1117 its_flush_cmd(its, sync_cmd); \ 1118 } \ 1119 \ 1120 post: \ 1121 rd_idx = readl_relaxed(its->base + GITS_CREADR); \ 1122 next_cmd = its_post_commands(its); \ 1123 raw_spin_unlock_irqrestore(&its->lock, flags); \ 1124 \ 1125 if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \ 1126 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \ 1127 } 1128 1129 static void its_build_sync_cmd(struct its_node *its, 1130 struct its_cmd_block *sync_cmd, 1131 struct its_collection *sync_col) 1132 { 1133 its_encode_cmd(sync_cmd, GITS_CMD_SYNC); 1134 its_encode_target(sync_cmd, sync_col->target_address); 1135 1136 its_fixup_cmd(sync_cmd); 1137 } 1138 1139 static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t, 1140 struct its_collection, its_build_sync_cmd) 1141 1142 static void its_build_vsync_cmd(struct its_node *its, 1143 struct its_cmd_block *sync_cmd, 1144 struct its_vpe *sync_vpe) 1145 { 1146 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC); 1147 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id); 1148 1149 its_fixup_cmd(sync_cmd); 1150 } 1151 1152 static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t, 1153 struct its_vpe, its_build_vsync_cmd) 1154 1155 static void its_send_int(struct its_device *dev, u32 event_id) 1156 { 1157 struct its_cmd_desc desc; 1158 1159 desc.its_int_cmd.dev = dev; 1160 desc.its_int_cmd.event_id = event_id; 1161 1162 its_send_single_command(dev->its, its_build_int_cmd, &desc); 1163 } 1164 1165 static void its_send_clear(struct its_device *dev, u32 event_id) 1166 { 1167 struct its_cmd_desc desc; 1168 1169 desc.its_clear_cmd.dev = dev; 1170 desc.its_clear_cmd.event_id = event_id; 1171 1172 its_send_single_command(dev->its, its_build_clear_cmd, &desc); 1173 } 1174 1175 static void its_send_inv(struct its_device *dev, u32 event_id) 1176 { 1177 struct its_cmd_desc desc; 1178 1179 desc.its_inv_cmd.dev = dev; 1180 desc.its_inv_cmd.event_id = event_id; 1181 1182 its_send_single_command(dev->its, its_build_inv_cmd, &desc); 1183 } 1184 1185 static void its_send_mapd(struct its_device *dev, int valid) 1186 { 1187 struct its_cmd_desc desc; 1188 1189 desc.its_mapd_cmd.dev = dev; 1190 desc.its_mapd_cmd.valid = !!valid; 1191 1192 its_send_single_command(dev->its, its_build_mapd_cmd, &desc); 1193 } 1194 1195 static void its_send_mapc(struct its_node *its, struct its_collection *col, 1196 int valid) 1197 { 1198 struct its_cmd_desc desc; 1199 1200 desc.its_mapc_cmd.col = col; 1201 desc.its_mapc_cmd.valid = !!valid; 1202 1203 its_send_single_command(its, its_build_mapc_cmd, &desc); 1204 } 1205 1206 static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id) 1207 { 1208 struct its_cmd_desc desc; 1209 1210 desc.its_mapti_cmd.dev = dev; 1211 desc.its_mapti_cmd.phys_id = irq_id; 1212 desc.its_mapti_cmd.event_id = id; 1213 1214 its_send_single_command(dev->its, its_build_mapti_cmd, &desc); 1215 } 1216 1217 static void its_send_movi(struct its_device *dev, 1218 struct its_collection *col, u32 id) 1219 { 1220 struct its_cmd_desc desc; 1221 1222 desc.its_movi_cmd.dev = dev; 1223 desc.its_movi_cmd.col = col; 1224 desc.its_movi_cmd.event_id = id; 1225 1226 its_send_single_command(dev->its, its_build_movi_cmd, &desc); 1227 } 1228 1229 static void its_send_discard(struct its_device *dev, u32 id) 1230 { 1231 struct its_cmd_desc desc; 1232 1233 desc.its_discard_cmd.dev = dev; 1234 desc.its_discard_cmd.event_id = id; 1235 1236 its_send_single_command(dev->its, its_build_discard_cmd, &desc); 1237 } 1238 1239 static void its_send_invall(struct its_node *its, struct its_collection *col) 1240 { 1241 struct its_cmd_desc desc; 1242 1243 desc.its_invall_cmd.col = col; 1244 1245 its_send_single_command(its, its_build_invall_cmd, &desc); 1246 } 1247 1248 static void its_send_vmapti(struct its_device *dev, u32 id) 1249 { 1250 struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id); 1251 struct its_cmd_desc desc; 1252 1253 desc.its_vmapti_cmd.vpe = map->vpe; 1254 desc.its_vmapti_cmd.dev = dev; 1255 desc.its_vmapti_cmd.virt_id = map->vintid; 1256 desc.its_vmapti_cmd.event_id = id; 1257 desc.its_vmapti_cmd.db_enabled = map->db_enabled; 1258 1259 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc); 1260 } 1261 1262 static void its_send_vmovi(struct its_device *dev, u32 id) 1263 { 1264 struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id); 1265 struct its_cmd_desc desc; 1266 1267 desc.its_vmovi_cmd.vpe = map->vpe; 1268 desc.its_vmovi_cmd.dev = dev; 1269 desc.its_vmovi_cmd.event_id = id; 1270 desc.its_vmovi_cmd.db_enabled = map->db_enabled; 1271 1272 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc); 1273 } 1274 1275 static void its_send_vmapp(struct its_node *its, 1276 struct its_vpe *vpe, bool valid) 1277 { 1278 struct its_cmd_desc desc; 1279 1280 desc.its_vmapp_cmd.vpe = vpe; 1281 desc.its_vmapp_cmd.valid = valid; 1282 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx]; 1283 1284 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc); 1285 } 1286 1287 static void its_send_vmovp(struct its_vpe *vpe) 1288 { 1289 struct its_cmd_desc desc = {}; 1290 struct its_node *its; 1291 unsigned long flags; 1292 int col_id = vpe->col_idx; 1293 1294 desc.its_vmovp_cmd.vpe = vpe; 1295 1296 if (!its_list_map) { 1297 its = list_first_entry(&its_nodes, struct its_node, entry); 1298 desc.its_vmovp_cmd.col = &its->collections[col_id]; 1299 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc); 1300 return; 1301 } 1302 1303 /* 1304 * Yet another marvel of the architecture. If using the 1305 * its_list "feature", we need to make sure that all ITSs 1306 * receive all VMOVP commands in the same order. The only way 1307 * to guarantee this is to make vmovp a serialization point. 1308 * 1309 * Wall <-- Head. 1310 */ 1311 raw_spin_lock_irqsave(&vmovp_lock, flags); 1312 1313 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++; 1314 desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm); 1315 1316 /* Emit VMOVPs */ 1317 list_for_each_entry(its, &its_nodes, entry) { 1318 if (!is_v4(its)) 1319 continue; 1320 1321 if (!require_its_list_vmovp(vpe->its_vm, its)) 1322 continue; 1323 1324 desc.its_vmovp_cmd.col = &its->collections[col_id]; 1325 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc); 1326 } 1327 1328 raw_spin_unlock_irqrestore(&vmovp_lock, flags); 1329 } 1330 1331 static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe) 1332 { 1333 struct its_cmd_desc desc; 1334 1335 desc.its_vinvall_cmd.vpe = vpe; 1336 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc); 1337 } 1338 1339 static void its_send_vinv(struct its_device *dev, u32 event_id) 1340 { 1341 struct its_cmd_desc desc; 1342 1343 /* 1344 * There is no real VINV command. This is just a normal INV, 1345 * with a VSYNC instead of a SYNC. 1346 */ 1347 desc.its_inv_cmd.dev = dev; 1348 desc.its_inv_cmd.event_id = event_id; 1349 1350 its_send_single_vcommand(dev->its, its_build_vinv_cmd, &desc); 1351 } 1352 1353 static void its_send_vint(struct its_device *dev, u32 event_id) 1354 { 1355 struct its_cmd_desc desc; 1356 1357 /* 1358 * There is no real VINT command. This is just a normal INT, 1359 * with a VSYNC instead of a SYNC. 1360 */ 1361 desc.its_int_cmd.dev = dev; 1362 desc.its_int_cmd.event_id = event_id; 1363 1364 its_send_single_vcommand(dev->its, its_build_vint_cmd, &desc); 1365 } 1366 1367 static void its_send_vclear(struct its_device *dev, u32 event_id) 1368 { 1369 struct its_cmd_desc desc; 1370 1371 /* 1372 * There is no real VCLEAR command. This is just a normal CLEAR, 1373 * with a VSYNC instead of a SYNC. 1374 */ 1375 desc.its_clear_cmd.dev = dev; 1376 desc.its_clear_cmd.event_id = event_id; 1377 1378 its_send_single_vcommand(dev->its, its_build_vclear_cmd, &desc); 1379 } 1380 1381 static void its_send_invdb(struct its_node *its, struct its_vpe *vpe) 1382 { 1383 struct its_cmd_desc desc; 1384 1385 desc.its_invdb_cmd.vpe = vpe; 1386 its_send_single_vcommand(its, its_build_invdb_cmd, &desc); 1387 } 1388 1389 /* 1390 * irqchip functions - assumes MSI, mostly. 1391 */ 1392 static void lpi_write_config(struct irq_data *d, u8 clr, u8 set) 1393 { 1394 struct its_vlpi_map *map = get_vlpi_map(d); 1395 irq_hw_number_t hwirq; 1396 void *va; 1397 u8 *cfg; 1398 1399 if (map) { 1400 va = page_address(map->vm->vprop_page); 1401 hwirq = map->vintid; 1402 1403 /* Remember the updated property */ 1404 map->properties &= ~clr; 1405 map->properties |= set | LPI_PROP_GROUP1; 1406 } else { 1407 va = gic_rdists->prop_table_va; 1408 hwirq = d->hwirq; 1409 } 1410 1411 cfg = va + hwirq - 8192; 1412 *cfg &= ~clr; 1413 *cfg |= set | LPI_PROP_GROUP1; 1414 1415 /* 1416 * Make the above write visible to the redistributors. 1417 * And yes, we're flushing exactly: One. Single. Byte. 1418 * Humpf... 1419 */ 1420 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING) 1421 gic_flush_dcache_to_poc(cfg, sizeof(*cfg)); 1422 else 1423 dsb(ishst); 1424 } 1425 1426 static void wait_for_syncr(void __iomem *rdbase) 1427 { 1428 while (readl_relaxed(rdbase + GICR_SYNCR) & 1) 1429 cpu_relax(); 1430 } 1431 1432 static void direct_lpi_inv(struct irq_data *d) 1433 { 1434 struct its_vlpi_map *map = get_vlpi_map(d); 1435 void __iomem *rdbase; 1436 unsigned long flags; 1437 u64 val; 1438 int cpu; 1439 1440 if (map) { 1441 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 1442 1443 WARN_ON(!is_v4_1(its_dev->its)); 1444 1445 val = GICR_INVLPIR_V; 1446 val |= FIELD_PREP(GICR_INVLPIR_VPEID, map->vpe->vpe_id); 1447 val |= FIELD_PREP(GICR_INVLPIR_INTID, map->vintid); 1448 } else { 1449 val = d->hwirq; 1450 } 1451 1452 /* Target the redistributor this LPI is currently routed to */ 1453 cpu = irq_to_cpuid_lock(d, &flags); 1454 raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock); 1455 rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base; 1456 gic_write_lpir(val, rdbase + GICR_INVLPIR); 1457 1458 wait_for_syncr(rdbase); 1459 raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock); 1460 irq_to_cpuid_unlock(d, flags); 1461 } 1462 1463 static void lpi_update_config(struct irq_data *d, u8 clr, u8 set) 1464 { 1465 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 1466 1467 lpi_write_config(d, clr, set); 1468 if (gic_rdists->has_direct_lpi && 1469 (is_v4_1(its_dev->its) || !irqd_is_forwarded_to_vcpu(d))) 1470 direct_lpi_inv(d); 1471 else if (!irqd_is_forwarded_to_vcpu(d)) 1472 its_send_inv(its_dev, its_get_event_id(d)); 1473 else 1474 its_send_vinv(its_dev, its_get_event_id(d)); 1475 } 1476 1477 static void its_vlpi_set_doorbell(struct irq_data *d, bool enable) 1478 { 1479 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 1480 u32 event = its_get_event_id(d); 1481 struct its_vlpi_map *map; 1482 1483 /* 1484 * GICv4.1 does away with the per-LPI nonsense, nothing to do 1485 * here. 1486 */ 1487 if (is_v4_1(its_dev->its)) 1488 return; 1489 1490 map = dev_event_to_vlpi_map(its_dev, event); 1491 1492 if (map->db_enabled == enable) 1493 return; 1494 1495 map->db_enabled = enable; 1496 1497 /* 1498 * More fun with the architecture: 1499 * 1500 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI 1501 * value or to 1023, depending on the enable bit. But that 1502 * would be issuing a mapping for an /existing/ DevID+EventID 1503 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI 1504 * to the /same/ vPE, using this opportunity to adjust the 1505 * doorbell. Mouahahahaha. We loves it, Precious. 1506 */ 1507 its_send_vmovi(its_dev, event); 1508 } 1509 1510 static void its_mask_irq(struct irq_data *d) 1511 { 1512 if (irqd_is_forwarded_to_vcpu(d)) 1513 its_vlpi_set_doorbell(d, false); 1514 1515 lpi_update_config(d, LPI_PROP_ENABLED, 0); 1516 } 1517 1518 static void its_unmask_irq(struct irq_data *d) 1519 { 1520 if (irqd_is_forwarded_to_vcpu(d)) 1521 its_vlpi_set_doorbell(d, true); 1522 1523 lpi_update_config(d, 0, LPI_PROP_ENABLED); 1524 } 1525 1526 static __maybe_unused u32 its_read_lpi_count(struct irq_data *d, int cpu) 1527 { 1528 if (irqd_affinity_is_managed(d)) 1529 return atomic_read(&per_cpu_ptr(&cpu_lpi_count, cpu)->managed); 1530 1531 return atomic_read(&per_cpu_ptr(&cpu_lpi_count, cpu)->unmanaged); 1532 } 1533 1534 static void its_inc_lpi_count(struct irq_data *d, int cpu) 1535 { 1536 if (irqd_affinity_is_managed(d)) 1537 atomic_inc(&per_cpu_ptr(&cpu_lpi_count, cpu)->managed); 1538 else 1539 atomic_inc(&per_cpu_ptr(&cpu_lpi_count, cpu)->unmanaged); 1540 } 1541 1542 static void its_dec_lpi_count(struct irq_data *d, int cpu) 1543 { 1544 if (irqd_affinity_is_managed(d)) 1545 atomic_dec(&per_cpu_ptr(&cpu_lpi_count, cpu)->managed); 1546 else 1547 atomic_dec(&per_cpu_ptr(&cpu_lpi_count, cpu)->unmanaged); 1548 } 1549 1550 static unsigned int cpumask_pick_least_loaded(struct irq_data *d, 1551 const struct cpumask *cpu_mask) 1552 { 1553 unsigned int cpu = nr_cpu_ids, tmp; 1554 int count = S32_MAX; 1555 1556 for_each_cpu(tmp, cpu_mask) { 1557 int this_count = its_read_lpi_count(d, tmp); 1558 if (this_count < count) { 1559 cpu = tmp; 1560 count = this_count; 1561 } 1562 } 1563 1564 return cpu; 1565 } 1566 1567 /* 1568 * As suggested by Thomas Gleixner in: 1569 * https://lore.kernel.org/r/87h80q2aoc.fsf@nanos.tec.linutronix.de 1570 */ 1571 static int its_select_cpu(struct irq_data *d, 1572 const struct cpumask *aff_mask) 1573 { 1574 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 1575 cpumask_var_t tmpmask; 1576 int cpu, node; 1577 1578 if (!alloc_cpumask_var(&tmpmask, GFP_ATOMIC)) 1579 return -ENOMEM; 1580 1581 node = its_dev->its->numa_node; 1582 1583 if (!irqd_affinity_is_managed(d)) { 1584 /* First try the NUMA node */ 1585 if (node != NUMA_NO_NODE) { 1586 /* 1587 * Try the intersection of the affinity mask and the 1588 * node mask (and the online mask, just to be safe). 1589 */ 1590 cpumask_and(tmpmask, cpumask_of_node(node), aff_mask); 1591 cpumask_and(tmpmask, tmpmask, cpu_online_mask); 1592 1593 /* 1594 * Ideally, we would check if the mask is empty, and 1595 * try again on the full node here. 1596 * 1597 * But it turns out that the way ACPI describes the 1598 * affinity for ITSs only deals about memory, and 1599 * not target CPUs, so it cannot describe a single 1600 * ITS placed next to two NUMA nodes. 1601 * 1602 * Instead, just fallback on the online mask. This 1603 * diverges from Thomas' suggestion above. 1604 */ 1605 cpu = cpumask_pick_least_loaded(d, tmpmask); 1606 if (cpu < nr_cpu_ids) 1607 goto out; 1608 1609 /* If we can't cross sockets, give up */ 1610 if ((its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)) 1611 goto out; 1612 1613 /* If the above failed, expand the search */ 1614 } 1615 1616 /* Try the intersection of the affinity and online masks */ 1617 cpumask_and(tmpmask, aff_mask, cpu_online_mask); 1618 1619 /* If that doesn't fly, the online mask is the last resort */ 1620 if (cpumask_empty(tmpmask)) 1621 cpumask_copy(tmpmask, cpu_online_mask); 1622 1623 cpu = cpumask_pick_least_loaded(d, tmpmask); 1624 } else { 1625 cpumask_and(tmpmask, irq_data_get_affinity_mask(d), cpu_online_mask); 1626 1627 /* If we cannot cross sockets, limit the search to that node */ 1628 if ((its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) && 1629 node != NUMA_NO_NODE) 1630 cpumask_and(tmpmask, tmpmask, cpumask_of_node(node)); 1631 1632 cpu = cpumask_pick_least_loaded(d, tmpmask); 1633 } 1634 out: 1635 free_cpumask_var(tmpmask); 1636 1637 pr_debug("IRQ%d -> %*pbl CPU%d\n", d->irq, cpumask_pr_args(aff_mask), cpu); 1638 return cpu; 1639 } 1640 1641 static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val, 1642 bool force) 1643 { 1644 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 1645 struct its_collection *target_col; 1646 u32 id = its_get_event_id(d); 1647 int cpu, prev_cpu; 1648 1649 /* A forwarded interrupt should use irq_set_vcpu_affinity */ 1650 if (irqd_is_forwarded_to_vcpu(d)) 1651 return -EINVAL; 1652 1653 prev_cpu = its_dev->event_map.col_map[id]; 1654 its_dec_lpi_count(d, prev_cpu); 1655 1656 if (!force) 1657 cpu = its_select_cpu(d, mask_val); 1658 else 1659 cpu = cpumask_pick_least_loaded(d, mask_val); 1660 1661 if (cpu < 0 || cpu >= nr_cpu_ids) 1662 goto err; 1663 1664 /* don't set the affinity when the target cpu is same as current one */ 1665 if (cpu != prev_cpu) { 1666 target_col = &its_dev->its->collections[cpu]; 1667 its_send_movi(its_dev, target_col, id); 1668 its_dev->event_map.col_map[id] = cpu; 1669 irq_data_update_effective_affinity(d, cpumask_of(cpu)); 1670 } 1671 1672 its_inc_lpi_count(d, cpu); 1673 1674 return IRQ_SET_MASK_OK_DONE; 1675 1676 err: 1677 its_inc_lpi_count(d, prev_cpu); 1678 return -EINVAL; 1679 } 1680 1681 static u64 its_irq_get_msi_base(struct its_device *its_dev) 1682 { 1683 struct its_node *its = its_dev->its; 1684 1685 return its->phys_base + GITS_TRANSLATER; 1686 } 1687 1688 static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg) 1689 { 1690 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 1691 struct its_node *its; 1692 u64 addr; 1693 1694 its = its_dev->its; 1695 addr = its->get_msi_base(its_dev); 1696 1697 msg->address_lo = lower_32_bits(addr); 1698 msg->address_hi = upper_32_bits(addr); 1699 msg->data = its_get_event_id(d); 1700 1701 iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg); 1702 } 1703 1704 static int its_irq_set_irqchip_state(struct irq_data *d, 1705 enum irqchip_irq_state which, 1706 bool state) 1707 { 1708 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 1709 u32 event = its_get_event_id(d); 1710 1711 if (which != IRQCHIP_STATE_PENDING) 1712 return -EINVAL; 1713 1714 if (irqd_is_forwarded_to_vcpu(d)) { 1715 if (state) 1716 its_send_vint(its_dev, event); 1717 else 1718 its_send_vclear(its_dev, event); 1719 } else { 1720 if (state) 1721 its_send_int(its_dev, event); 1722 else 1723 its_send_clear(its_dev, event); 1724 } 1725 1726 return 0; 1727 } 1728 1729 static int its_irq_retrigger(struct irq_data *d) 1730 { 1731 return !its_irq_set_irqchip_state(d, IRQCHIP_STATE_PENDING, true); 1732 } 1733 1734 /* 1735 * Two favourable cases: 1736 * 1737 * (a) Either we have a GICv4.1, and all vPEs have to be mapped at all times 1738 * for vSGI delivery 1739 * 1740 * (b) Or the ITSs do not use a list map, meaning that VMOVP is cheap enough 1741 * and we're better off mapping all VPEs always 1742 * 1743 * If neither (a) nor (b) is true, then we map vPEs on demand. 1744 * 1745 */ 1746 static bool gic_requires_eager_mapping(void) 1747 { 1748 if (!its_list_map || gic_rdists->has_rvpeid) 1749 return true; 1750 1751 return false; 1752 } 1753 1754 static void its_map_vm(struct its_node *its, struct its_vm *vm) 1755 { 1756 unsigned long flags; 1757 1758 if (gic_requires_eager_mapping()) 1759 return; 1760 1761 raw_spin_lock_irqsave(&vmovp_lock, flags); 1762 1763 /* 1764 * If the VM wasn't mapped yet, iterate over the vpes and get 1765 * them mapped now. 1766 */ 1767 vm->vlpi_count[its->list_nr]++; 1768 1769 if (vm->vlpi_count[its->list_nr] == 1) { 1770 int i; 1771 1772 for (i = 0; i < vm->nr_vpes; i++) { 1773 struct its_vpe *vpe = vm->vpes[i]; 1774 struct irq_data *d = irq_get_irq_data(vpe->irq); 1775 1776 /* Map the VPE to the first possible CPU */ 1777 vpe->col_idx = cpumask_first(cpu_online_mask); 1778 its_send_vmapp(its, vpe, true); 1779 its_send_vinvall(its, vpe); 1780 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx)); 1781 } 1782 } 1783 1784 raw_spin_unlock_irqrestore(&vmovp_lock, flags); 1785 } 1786 1787 static void its_unmap_vm(struct its_node *its, struct its_vm *vm) 1788 { 1789 unsigned long flags; 1790 1791 /* Not using the ITS list? Everything is always mapped. */ 1792 if (gic_requires_eager_mapping()) 1793 return; 1794 1795 raw_spin_lock_irqsave(&vmovp_lock, flags); 1796 1797 if (!--vm->vlpi_count[its->list_nr]) { 1798 int i; 1799 1800 for (i = 0; i < vm->nr_vpes; i++) 1801 its_send_vmapp(its, vm->vpes[i], false); 1802 } 1803 1804 raw_spin_unlock_irqrestore(&vmovp_lock, flags); 1805 } 1806 1807 static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) 1808 { 1809 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 1810 u32 event = its_get_event_id(d); 1811 int ret = 0; 1812 1813 if (!info->map) 1814 return -EINVAL; 1815 1816 raw_spin_lock(&its_dev->event_map.vlpi_lock); 1817 1818 if (!its_dev->event_map.vm) { 1819 struct its_vlpi_map *maps; 1820 1821 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps), 1822 GFP_ATOMIC); 1823 if (!maps) { 1824 ret = -ENOMEM; 1825 goto out; 1826 } 1827 1828 its_dev->event_map.vm = info->map->vm; 1829 its_dev->event_map.vlpi_maps = maps; 1830 } else if (its_dev->event_map.vm != info->map->vm) { 1831 ret = -EINVAL; 1832 goto out; 1833 } 1834 1835 /* Get our private copy of the mapping information */ 1836 its_dev->event_map.vlpi_maps[event] = *info->map; 1837 1838 if (irqd_is_forwarded_to_vcpu(d)) { 1839 /* Already mapped, move it around */ 1840 its_send_vmovi(its_dev, event); 1841 } else { 1842 /* Ensure all the VPEs are mapped on this ITS */ 1843 its_map_vm(its_dev->its, info->map->vm); 1844 1845 /* 1846 * Flag the interrupt as forwarded so that we can 1847 * start poking the virtual property table. 1848 */ 1849 irqd_set_forwarded_to_vcpu(d); 1850 1851 /* Write out the property to the prop table */ 1852 lpi_write_config(d, 0xff, info->map->properties); 1853 1854 /* Drop the physical mapping */ 1855 its_send_discard(its_dev, event); 1856 1857 /* and install the virtual one */ 1858 its_send_vmapti(its_dev, event); 1859 1860 /* Increment the number of VLPIs */ 1861 its_dev->event_map.nr_vlpis++; 1862 } 1863 1864 out: 1865 raw_spin_unlock(&its_dev->event_map.vlpi_lock); 1866 return ret; 1867 } 1868 1869 static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info) 1870 { 1871 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 1872 struct its_vlpi_map *map; 1873 int ret = 0; 1874 1875 raw_spin_lock(&its_dev->event_map.vlpi_lock); 1876 1877 map = get_vlpi_map(d); 1878 1879 if (!its_dev->event_map.vm || !map) { 1880 ret = -EINVAL; 1881 goto out; 1882 } 1883 1884 /* Copy our mapping information to the incoming request */ 1885 *info->map = *map; 1886 1887 out: 1888 raw_spin_unlock(&its_dev->event_map.vlpi_lock); 1889 return ret; 1890 } 1891 1892 static int its_vlpi_unmap(struct irq_data *d) 1893 { 1894 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 1895 u32 event = its_get_event_id(d); 1896 int ret = 0; 1897 1898 raw_spin_lock(&its_dev->event_map.vlpi_lock); 1899 1900 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) { 1901 ret = -EINVAL; 1902 goto out; 1903 } 1904 1905 /* Drop the virtual mapping */ 1906 its_send_discard(its_dev, event); 1907 1908 /* and restore the physical one */ 1909 irqd_clr_forwarded_to_vcpu(d); 1910 its_send_mapti(its_dev, d->hwirq, event); 1911 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO | 1912 LPI_PROP_ENABLED | 1913 LPI_PROP_GROUP1)); 1914 1915 /* Potentially unmap the VM from this ITS */ 1916 its_unmap_vm(its_dev->its, its_dev->event_map.vm); 1917 1918 /* 1919 * Drop the refcount and make the device available again if 1920 * this was the last VLPI. 1921 */ 1922 if (!--its_dev->event_map.nr_vlpis) { 1923 its_dev->event_map.vm = NULL; 1924 kfree(its_dev->event_map.vlpi_maps); 1925 } 1926 1927 out: 1928 raw_spin_unlock(&its_dev->event_map.vlpi_lock); 1929 return ret; 1930 } 1931 1932 static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info) 1933 { 1934 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 1935 1936 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) 1937 return -EINVAL; 1938 1939 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI) 1940 lpi_update_config(d, 0xff, info->config); 1941 else 1942 lpi_write_config(d, 0xff, info->config); 1943 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED)); 1944 1945 return 0; 1946 } 1947 1948 static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) 1949 { 1950 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 1951 struct its_cmd_info *info = vcpu_info; 1952 1953 /* Need a v4 ITS */ 1954 if (!is_v4(its_dev->its)) 1955 return -EINVAL; 1956 1957 /* Unmap request? */ 1958 if (!info) 1959 return its_vlpi_unmap(d); 1960 1961 switch (info->cmd_type) { 1962 case MAP_VLPI: 1963 return its_vlpi_map(d, info); 1964 1965 case GET_VLPI: 1966 return its_vlpi_get(d, info); 1967 1968 case PROP_UPDATE_VLPI: 1969 case PROP_UPDATE_AND_INV_VLPI: 1970 return its_vlpi_prop_update(d, info); 1971 1972 default: 1973 return -EINVAL; 1974 } 1975 } 1976 1977 static struct irq_chip its_irq_chip = { 1978 .name = "ITS", 1979 .irq_mask = its_mask_irq, 1980 .irq_unmask = its_unmask_irq, 1981 .irq_eoi = irq_chip_eoi_parent, 1982 .irq_set_affinity = its_set_affinity, 1983 .irq_compose_msi_msg = its_irq_compose_msi_msg, 1984 .irq_set_irqchip_state = its_irq_set_irqchip_state, 1985 .irq_retrigger = its_irq_retrigger, 1986 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity, 1987 }; 1988 1989 1990 /* 1991 * How we allocate LPIs: 1992 * 1993 * lpi_range_list contains ranges of LPIs that are to available to 1994 * allocate from. To allocate LPIs, just pick the first range that 1995 * fits the required allocation, and reduce it by the required 1996 * amount. Once empty, remove the range from the list. 1997 * 1998 * To free a range of LPIs, add a free range to the list, sort it and 1999 * merge the result if the new range happens to be adjacent to an 2000 * already free block. 2001 * 2002 * The consequence of the above is that allocation is cost is low, but 2003 * freeing is expensive. We assumes that freeing rarely occurs. 2004 */ 2005 #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */ 2006 2007 static DEFINE_MUTEX(lpi_range_lock); 2008 static LIST_HEAD(lpi_range_list); 2009 2010 struct lpi_range { 2011 struct list_head entry; 2012 u32 base_id; 2013 u32 span; 2014 }; 2015 2016 static struct lpi_range *mk_lpi_range(u32 base, u32 span) 2017 { 2018 struct lpi_range *range; 2019 2020 range = kmalloc(sizeof(*range), GFP_KERNEL); 2021 if (range) { 2022 range->base_id = base; 2023 range->span = span; 2024 } 2025 2026 return range; 2027 } 2028 2029 static int alloc_lpi_range(u32 nr_lpis, u32 *base) 2030 { 2031 struct lpi_range *range, *tmp; 2032 int err = -ENOSPC; 2033 2034 mutex_lock(&lpi_range_lock); 2035 2036 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) { 2037 if (range->span >= nr_lpis) { 2038 *base = range->base_id; 2039 range->base_id += nr_lpis; 2040 range->span -= nr_lpis; 2041 2042 if (range->span == 0) { 2043 list_del(&range->entry); 2044 kfree(range); 2045 } 2046 2047 err = 0; 2048 break; 2049 } 2050 } 2051 2052 mutex_unlock(&lpi_range_lock); 2053 2054 pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis); 2055 return err; 2056 } 2057 2058 static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b) 2059 { 2060 if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list) 2061 return; 2062 if (a->base_id + a->span != b->base_id) 2063 return; 2064 b->base_id = a->base_id; 2065 b->span += a->span; 2066 list_del(&a->entry); 2067 kfree(a); 2068 } 2069 2070 static int free_lpi_range(u32 base, u32 nr_lpis) 2071 { 2072 struct lpi_range *new, *old; 2073 2074 new = mk_lpi_range(base, nr_lpis); 2075 if (!new) 2076 return -ENOMEM; 2077 2078 mutex_lock(&lpi_range_lock); 2079 2080 list_for_each_entry_reverse(old, &lpi_range_list, entry) { 2081 if (old->base_id < base) 2082 break; 2083 } 2084 /* 2085 * old is the last element with ->base_id smaller than base, 2086 * so new goes right after it. If there are no elements with 2087 * ->base_id smaller than base, &old->entry ends up pointing 2088 * at the head of the list, and inserting new it the start of 2089 * the list is the right thing to do in that case as well. 2090 */ 2091 list_add(&new->entry, &old->entry); 2092 /* 2093 * Now check if we can merge with the preceding and/or 2094 * following ranges. 2095 */ 2096 merge_lpi_ranges(old, new); 2097 merge_lpi_ranges(new, list_next_entry(new, entry)); 2098 2099 mutex_unlock(&lpi_range_lock); 2100 return 0; 2101 } 2102 2103 static int __init its_lpi_init(u32 id_bits) 2104 { 2105 u32 lpis = (1UL << id_bits) - 8192; 2106 u32 numlpis; 2107 int err; 2108 2109 numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer); 2110 2111 if (numlpis > 2 && !WARN_ON(numlpis > lpis)) { 2112 lpis = numlpis; 2113 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n", 2114 lpis); 2115 } 2116 2117 /* 2118 * Initializing the allocator is just the same as freeing the 2119 * full range of LPIs. 2120 */ 2121 err = free_lpi_range(8192, lpis); 2122 pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis); 2123 return err; 2124 } 2125 2126 static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids) 2127 { 2128 unsigned long *bitmap = NULL; 2129 int err = 0; 2130 2131 do { 2132 err = alloc_lpi_range(nr_irqs, base); 2133 if (!err) 2134 break; 2135 2136 nr_irqs /= 2; 2137 } while (nr_irqs > 0); 2138 2139 if (!nr_irqs) 2140 err = -ENOSPC; 2141 2142 if (err) 2143 goto out; 2144 2145 bitmap = bitmap_zalloc(nr_irqs, GFP_ATOMIC); 2146 if (!bitmap) 2147 goto out; 2148 2149 *nr_ids = nr_irqs; 2150 2151 out: 2152 if (!bitmap) 2153 *base = *nr_ids = 0; 2154 2155 return bitmap; 2156 } 2157 2158 static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids) 2159 { 2160 WARN_ON(free_lpi_range(base, nr_ids)); 2161 bitmap_free(bitmap); 2162 } 2163 2164 static void gic_reset_prop_table(void *va) 2165 { 2166 /* Priority 0xa0, Group-1, disabled */ 2167 memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ); 2168 2169 /* Make sure the GIC will observe the written configuration */ 2170 gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ); 2171 } 2172 2173 static struct page *its_allocate_prop_table(gfp_t gfp_flags) 2174 { 2175 struct page *prop_page; 2176 2177 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ)); 2178 if (!prop_page) 2179 return NULL; 2180 2181 gic_reset_prop_table(page_address(prop_page)); 2182 2183 return prop_page; 2184 } 2185 2186 static void its_free_prop_table(struct page *prop_page) 2187 { 2188 free_pages((unsigned long)page_address(prop_page), 2189 get_order(LPI_PROPBASE_SZ)); 2190 } 2191 2192 static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size) 2193 { 2194 phys_addr_t start, end, addr_end; 2195 u64 i; 2196 2197 /* 2198 * We don't bother checking for a kdump kernel as by 2199 * construction, the LPI tables are out of this kernel's 2200 * memory map. 2201 */ 2202 if (is_kdump_kernel()) 2203 return true; 2204 2205 addr_end = addr + size - 1; 2206 2207 for_each_reserved_mem_range(i, &start, &end) { 2208 if (addr >= start && addr_end <= end) 2209 return true; 2210 } 2211 2212 /* Not found, not a good sign... */ 2213 pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n", 2214 &addr, &addr_end); 2215 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK); 2216 return false; 2217 } 2218 2219 static int gic_reserve_range(phys_addr_t addr, unsigned long size) 2220 { 2221 if (efi_enabled(EFI_CONFIG_TABLES)) 2222 return efi_mem_reserve_persistent(addr, size); 2223 2224 return 0; 2225 } 2226 2227 static int __init its_setup_lpi_prop_table(void) 2228 { 2229 if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) { 2230 u64 val; 2231 2232 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER); 2233 lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1; 2234 2235 gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12); 2236 gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa, 2237 LPI_PROPBASE_SZ, 2238 MEMREMAP_WB); 2239 gic_reset_prop_table(gic_rdists->prop_table_va); 2240 } else { 2241 struct page *page; 2242 2243 lpi_id_bits = min_t(u32, 2244 GICD_TYPER_ID_BITS(gic_rdists->gicd_typer), 2245 ITS_MAX_LPI_NRBITS); 2246 page = its_allocate_prop_table(GFP_NOWAIT); 2247 if (!page) { 2248 pr_err("Failed to allocate PROPBASE\n"); 2249 return -ENOMEM; 2250 } 2251 2252 gic_rdists->prop_table_pa = page_to_phys(page); 2253 gic_rdists->prop_table_va = page_address(page); 2254 WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa, 2255 LPI_PROPBASE_SZ)); 2256 } 2257 2258 pr_info("GICv3: using LPI property table @%pa\n", 2259 &gic_rdists->prop_table_pa); 2260 2261 return its_lpi_init(lpi_id_bits); 2262 } 2263 2264 static const char *its_base_type_string[] = { 2265 [GITS_BASER_TYPE_DEVICE] = "Devices", 2266 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs", 2267 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)", 2268 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections", 2269 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)", 2270 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)", 2271 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)", 2272 }; 2273 2274 static u64 its_read_baser(struct its_node *its, struct its_baser *baser) 2275 { 2276 u32 idx = baser - its->tables; 2277 2278 return gits_read_baser(its->base + GITS_BASER + (idx << 3)); 2279 } 2280 2281 static void its_write_baser(struct its_node *its, struct its_baser *baser, 2282 u64 val) 2283 { 2284 u32 idx = baser - its->tables; 2285 2286 gits_write_baser(val, its->base + GITS_BASER + (idx << 3)); 2287 baser->val = its_read_baser(its, baser); 2288 } 2289 2290 static int its_setup_baser(struct its_node *its, struct its_baser *baser, 2291 u64 cache, u64 shr, u32 order, bool indirect) 2292 { 2293 u64 val = its_read_baser(its, baser); 2294 u64 esz = GITS_BASER_ENTRY_SIZE(val); 2295 u64 type = GITS_BASER_TYPE(val); 2296 u64 baser_phys, tmp; 2297 u32 alloc_pages, psz; 2298 struct page *page; 2299 void *base; 2300 2301 psz = baser->psz; 2302 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz); 2303 if (alloc_pages > GITS_BASER_PAGES_MAX) { 2304 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n", 2305 &its->phys_base, its_base_type_string[type], 2306 alloc_pages, GITS_BASER_PAGES_MAX); 2307 alloc_pages = GITS_BASER_PAGES_MAX; 2308 order = get_order(GITS_BASER_PAGES_MAX * psz); 2309 } 2310 2311 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order); 2312 if (!page) 2313 return -ENOMEM; 2314 2315 base = (void *)page_address(page); 2316 baser_phys = virt_to_phys(base); 2317 2318 /* Check if the physical address of the memory is above 48bits */ 2319 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) { 2320 2321 /* 52bit PA is supported only when PageSize=64K */ 2322 if (psz != SZ_64K) { 2323 pr_err("ITS: no 52bit PA support when psz=%d\n", psz); 2324 free_pages((unsigned long)base, order); 2325 return -ENXIO; 2326 } 2327 2328 /* Convert 52bit PA to 48bit field */ 2329 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys); 2330 } 2331 2332 retry_baser: 2333 val = (baser_phys | 2334 (type << GITS_BASER_TYPE_SHIFT) | 2335 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) | 2336 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) | 2337 cache | 2338 shr | 2339 GITS_BASER_VALID); 2340 2341 val |= indirect ? GITS_BASER_INDIRECT : 0x0; 2342 2343 switch (psz) { 2344 case SZ_4K: 2345 val |= GITS_BASER_PAGE_SIZE_4K; 2346 break; 2347 case SZ_16K: 2348 val |= GITS_BASER_PAGE_SIZE_16K; 2349 break; 2350 case SZ_64K: 2351 val |= GITS_BASER_PAGE_SIZE_64K; 2352 break; 2353 } 2354 2355 its_write_baser(its, baser, val); 2356 tmp = baser->val; 2357 2358 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) { 2359 /* 2360 * Shareability didn't stick. Just use 2361 * whatever the read reported, which is likely 2362 * to be the only thing this redistributor 2363 * supports. If that's zero, make it 2364 * non-cacheable as well. 2365 */ 2366 shr = tmp & GITS_BASER_SHAREABILITY_MASK; 2367 if (!shr) { 2368 cache = GITS_BASER_nC; 2369 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order)); 2370 } 2371 goto retry_baser; 2372 } 2373 2374 if (val != tmp) { 2375 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n", 2376 &its->phys_base, its_base_type_string[type], 2377 val, tmp); 2378 free_pages((unsigned long)base, order); 2379 return -ENXIO; 2380 } 2381 2382 baser->order = order; 2383 baser->base = base; 2384 baser->psz = psz; 2385 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz; 2386 2387 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n", 2388 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp), 2389 its_base_type_string[type], 2390 (unsigned long)virt_to_phys(base), 2391 indirect ? "indirect" : "flat", (int)esz, 2392 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT); 2393 2394 return 0; 2395 } 2396 2397 static bool its_parse_indirect_baser(struct its_node *its, 2398 struct its_baser *baser, 2399 u32 *order, u32 ids) 2400 { 2401 u64 tmp = its_read_baser(its, baser); 2402 u64 type = GITS_BASER_TYPE(tmp); 2403 u64 esz = GITS_BASER_ENTRY_SIZE(tmp); 2404 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb; 2405 u32 new_order = *order; 2406 u32 psz = baser->psz; 2407 bool indirect = false; 2408 2409 /* No need to enable Indirection if memory requirement < (psz*2)bytes */ 2410 if ((esz << ids) > (psz * 2)) { 2411 /* 2412 * Find out whether hw supports a single or two-level table by 2413 * table by reading bit at offset '62' after writing '1' to it. 2414 */ 2415 its_write_baser(its, baser, val | GITS_BASER_INDIRECT); 2416 indirect = !!(baser->val & GITS_BASER_INDIRECT); 2417 2418 if (indirect) { 2419 /* 2420 * The size of the lvl2 table is equal to ITS page size 2421 * which is 'psz'. For computing lvl1 table size, 2422 * subtract ID bits that sparse lvl2 table from 'ids' 2423 * which is reported by ITS hardware times lvl1 table 2424 * entry size. 2425 */ 2426 ids -= ilog2(psz / (int)esz); 2427 esz = GITS_LVL1_ENTRY_SIZE; 2428 } 2429 } 2430 2431 /* 2432 * Allocate as many entries as required to fit the 2433 * range of device IDs that the ITS can grok... The ID 2434 * space being incredibly sparse, this results in a 2435 * massive waste of memory if two-level device table 2436 * feature is not supported by hardware. 2437 */ 2438 new_order = max_t(u32, get_order(esz << ids), new_order); 2439 if (new_order >= MAX_ORDER) { 2440 new_order = MAX_ORDER - 1; 2441 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz); 2442 pr_warn("ITS@%pa: %s Table too large, reduce ids %llu->%u\n", 2443 &its->phys_base, its_base_type_string[type], 2444 device_ids(its), ids); 2445 } 2446 2447 *order = new_order; 2448 2449 return indirect; 2450 } 2451 2452 static u32 compute_common_aff(u64 val) 2453 { 2454 u32 aff, clpiaff; 2455 2456 aff = FIELD_GET(GICR_TYPER_AFFINITY, val); 2457 clpiaff = FIELD_GET(GICR_TYPER_COMMON_LPI_AFF, val); 2458 2459 return aff & ~(GENMASK(31, 0) >> (clpiaff * 8)); 2460 } 2461 2462 static u32 compute_its_aff(struct its_node *its) 2463 { 2464 u64 val; 2465 u32 svpet; 2466 2467 /* 2468 * Reencode the ITS SVPET and MPIDR as a GICR_TYPER, and compute 2469 * the resulting affinity. We then use that to see if this match 2470 * our own affinity. 2471 */ 2472 svpet = FIELD_GET(GITS_TYPER_SVPET, its->typer); 2473 val = FIELD_PREP(GICR_TYPER_COMMON_LPI_AFF, svpet); 2474 val |= FIELD_PREP(GICR_TYPER_AFFINITY, its->mpidr); 2475 return compute_common_aff(val); 2476 } 2477 2478 static struct its_node *find_sibling_its(struct its_node *cur_its) 2479 { 2480 struct its_node *its; 2481 u32 aff; 2482 2483 if (!FIELD_GET(GITS_TYPER_SVPET, cur_its->typer)) 2484 return NULL; 2485 2486 aff = compute_its_aff(cur_its); 2487 2488 list_for_each_entry(its, &its_nodes, entry) { 2489 u64 baser; 2490 2491 if (!is_v4_1(its) || its == cur_its) 2492 continue; 2493 2494 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer)) 2495 continue; 2496 2497 if (aff != compute_its_aff(its)) 2498 continue; 2499 2500 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */ 2501 baser = its->tables[2].val; 2502 if (!(baser & GITS_BASER_VALID)) 2503 continue; 2504 2505 return its; 2506 } 2507 2508 return NULL; 2509 } 2510 2511 static void its_free_tables(struct its_node *its) 2512 { 2513 int i; 2514 2515 for (i = 0; i < GITS_BASER_NR_REGS; i++) { 2516 if (its->tables[i].base) { 2517 free_pages((unsigned long)its->tables[i].base, 2518 its->tables[i].order); 2519 its->tables[i].base = NULL; 2520 } 2521 } 2522 } 2523 2524 static int its_probe_baser_psz(struct its_node *its, struct its_baser *baser) 2525 { 2526 u64 psz = SZ_64K; 2527 2528 while (psz) { 2529 u64 val, gpsz; 2530 2531 val = its_read_baser(its, baser); 2532 val &= ~GITS_BASER_PAGE_SIZE_MASK; 2533 2534 switch (psz) { 2535 case SZ_64K: 2536 gpsz = GITS_BASER_PAGE_SIZE_64K; 2537 break; 2538 case SZ_16K: 2539 gpsz = GITS_BASER_PAGE_SIZE_16K; 2540 break; 2541 case SZ_4K: 2542 default: 2543 gpsz = GITS_BASER_PAGE_SIZE_4K; 2544 break; 2545 } 2546 2547 gpsz >>= GITS_BASER_PAGE_SIZE_SHIFT; 2548 2549 val |= FIELD_PREP(GITS_BASER_PAGE_SIZE_MASK, gpsz); 2550 its_write_baser(its, baser, val); 2551 2552 if (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser->val) == gpsz) 2553 break; 2554 2555 switch (psz) { 2556 case SZ_64K: 2557 psz = SZ_16K; 2558 break; 2559 case SZ_16K: 2560 psz = SZ_4K; 2561 break; 2562 case SZ_4K: 2563 default: 2564 return -1; 2565 } 2566 } 2567 2568 baser->psz = psz; 2569 return 0; 2570 } 2571 2572 static int its_alloc_tables(struct its_node *its) 2573 { 2574 u64 shr = GITS_BASER_InnerShareable; 2575 u64 cache = GITS_BASER_RaWaWb; 2576 int err, i; 2577 2578 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) 2579 /* erratum 24313: ignore memory access type */ 2580 cache = GITS_BASER_nCnB; 2581 2582 for (i = 0; i < GITS_BASER_NR_REGS; i++) { 2583 struct its_baser *baser = its->tables + i; 2584 u64 val = its_read_baser(its, baser); 2585 u64 type = GITS_BASER_TYPE(val); 2586 bool indirect = false; 2587 u32 order; 2588 2589 if (type == GITS_BASER_TYPE_NONE) 2590 continue; 2591 2592 if (its_probe_baser_psz(its, baser)) { 2593 its_free_tables(its); 2594 return -ENXIO; 2595 } 2596 2597 order = get_order(baser->psz); 2598 2599 switch (type) { 2600 case GITS_BASER_TYPE_DEVICE: 2601 indirect = its_parse_indirect_baser(its, baser, &order, 2602 device_ids(its)); 2603 break; 2604 2605 case GITS_BASER_TYPE_VCPU: 2606 if (is_v4_1(its)) { 2607 struct its_node *sibling; 2608 2609 WARN_ON(i != 2); 2610 if ((sibling = find_sibling_its(its))) { 2611 *baser = sibling->tables[2]; 2612 its_write_baser(its, baser, baser->val); 2613 continue; 2614 } 2615 } 2616 2617 indirect = its_parse_indirect_baser(its, baser, &order, 2618 ITS_MAX_VPEID_BITS); 2619 break; 2620 } 2621 2622 err = its_setup_baser(its, baser, cache, shr, order, indirect); 2623 if (err < 0) { 2624 its_free_tables(its); 2625 return err; 2626 } 2627 2628 /* Update settings which will be used for next BASERn */ 2629 cache = baser->val & GITS_BASER_CACHEABILITY_MASK; 2630 shr = baser->val & GITS_BASER_SHAREABILITY_MASK; 2631 } 2632 2633 return 0; 2634 } 2635 2636 static u64 inherit_vpe_l1_table_from_its(void) 2637 { 2638 struct its_node *its; 2639 u64 val; 2640 u32 aff; 2641 2642 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER); 2643 aff = compute_common_aff(val); 2644 2645 list_for_each_entry(its, &its_nodes, entry) { 2646 u64 baser, addr; 2647 2648 if (!is_v4_1(its)) 2649 continue; 2650 2651 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer)) 2652 continue; 2653 2654 if (aff != compute_its_aff(its)) 2655 continue; 2656 2657 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */ 2658 baser = its->tables[2].val; 2659 if (!(baser & GITS_BASER_VALID)) 2660 continue; 2661 2662 /* We have a winner! */ 2663 gic_data_rdist()->vpe_l1_base = its->tables[2].base; 2664 2665 val = GICR_VPROPBASER_4_1_VALID; 2666 if (baser & GITS_BASER_INDIRECT) 2667 val |= GICR_VPROPBASER_4_1_INDIRECT; 2668 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, 2669 FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser)); 2670 switch (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser)) { 2671 case GIC_PAGE_SIZE_64K: 2672 addr = GITS_BASER_ADDR_48_to_52(baser); 2673 break; 2674 default: 2675 addr = baser & GENMASK_ULL(47, 12); 2676 break; 2677 } 2678 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, addr >> 12); 2679 val |= FIELD_PREP(GICR_VPROPBASER_SHAREABILITY_MASK, 2680 FIELD_GET(GITS_BASER_SHAREABILITY_MASK, baser)); 2681 val |= FIELD_PREP(GICR_VPROPBASER_INNER_CACHEABILITY_MASK, 2682 FIELD_GET(GITS_BASER_INNER_CACHEABILITY_MASK, baser)); 2683 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, GITS_BASER_NR_PAGES(baser) - 1); 2684 2685 return val; 2686 } 2687 2688 return 0; 2689 } 2690 2691 static u64 inherit_vpe_l1_table_from_rd(cpumask_t **mask) 2692 { 2693 u32 aff; 2694 u64 val; 2695 int cpu; 2696 2697 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER); 2698 aff = compute_common_aff(val); 2699 2700 for_each_possible_cpu(cpu) { 2701 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base; 2702 2703 if (!base || cpu == smp_processor_id()) 2704 continue; 2705 2706 val = gic_read_typer(base + GICR_TYPER); 2707 if (aff != compute_common_aff(val)) 2708 continue; 2709 2710 /* 2711 * At this point, we have a victim. This particular CPU 2712 * has already booted, and has an affinity that matches 2713 * ours wrt CommonLPIAff. Let's use its own VPROPBASER. 2714 * Make sure we don't write the Z bit in that case. 2715 */ 2716 val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER); 2717 val &= ~GICR_VPROPBASER_4_1_Z; 2718 2719 gic_data_rdist()->vpe_l1_base = gic_data_rdist_cpu(cpu)->vpe_l1_base; 2720 *mask = gic_data_rdist_cpu(cpu)->vpe_table_mask; 2721 2722 return val; 2723 } 2724 2725 return 0; 2726 } 2727 2728 static bool allocate_vpe_l2_table(int cpu, u32 id) 2729 { 2730 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base; 2731 unsigned int psz, esz, idx, npg, gpsz; 2732 u64 val; 2733 struct page *page; 2734 __le64 *table; 2735 2736 if (!gic_rdists->has_rvpeid) 2737 return true; 2738 2739 /* Skip non-present CPUs */ 2740 if (!base) 2741 return true; 2742 2743 val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER); 2744 2745 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val) + 1; 2746 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val); 2747 npg = FIELD_GET(GICR_VPROPBASER_4_1_SIZE, val) + 1; 2748 2749 switch (gpsz) { 2750 default: 2751 WARN_ON(1); 2752 fallthrough; 2753 case GIC_PAGE_SIZE_4K: 2754 psz = SZ_4K; 2755 break; 2756 case GIC_PAGE_SIZE_16K: 2757 psz = SZ_16K; 2758 break; 2759 case GIC_PAGE_SIZE_64K: 2760 psz = SZ_64K; 2761 break; 2762 } 2763 2764 /* Don't allow vpe_id that exceeds single, flat table limit */ 2765 if (!(val & GICR_VPROPBASER_4_1_INDIRECT)) 2766 return (id < (npg * psz / (esz * SZ_8))); 2767 2768 /* Compute 1st level table index & check if that exceeds table limit */ 2769 idx = id >> ilog2(psz / (esz * SZ_8)); 2770 if (idx >= (npg * psz / GITS_LVL1_ENTRY_SIZE)) 2771 return false; 2772 2773 table = gic_data_rdist_cpu(cpu)->vpe_l1_base; 2774 2775 /* Allocate memory for 2nd level table */ 2776 if (!table[idx]) { 2777 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(psz)); 2778 if (!page) 2779 return false; 2780 2781 /* Flush Lvl2 table to PoC if hw doesn't support coherency */ 2782 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK)) 2783 gic_flush_dcache_to_poc(page_address(page), psz); 2784 2785 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID); 2786 2787 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */ 2788 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK)) 2789 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE); 2790 2791 /* Ensure updated table contents are visible to RD hardware */ 2792 dsb(sy); 2793 } 2794 2795 return true; 2796 } 2797 2798 static int allocate_vpe_l1_table(void) 2799 { 2800 void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); 2801 u64 val, gpsz, npg, pa; 2802 unsigned int psz = SZ_64K; 2803 unsigned int np, epp, esz; 2804 struct page *page; 2805 2806 if (!gic_rdists->has_rvpeid) 2807 return 0; 2808 2809 /* 2810 * if VPENDBASER.Valid is set, disable any previously programmed 2811 * VPE by setting PendingLast while clearing Valid. This has the 2812 * effect of making sure no doorbell will be generated and we can 2813 * then safely clear VPROPBASER.Valid. 2814 */ 2815 if (gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER) & GICR_VPENDBASER_Valid) 2816 gicr_write_vpendbaser(GICR_VPENDBASER_PendingLast, 2817 vlpi_base + GICR_VPENDBASER); 2818 2819 /* 2820 * If we can inherit the configuration from another RD, let's do 2821 * so. Otherwise, we have to go through the allocation process. We 2822 * assume that all RDs have the exact same requirements, as 2823 * nothing will work otherwise. 2824 */ 2825 val = inherit_vpe_l1_table_from_rd(&gic_data_rdist()->vpe_table_mask); 2826 if (val & GICR_VPROPBASER_4_1_VALID) 2827 goto out; 2828 2829 gic_data_rdist()->vpe_table_mask = kzalloc(sizeof(cpumask_t), GFP_ATOMIC); 2830 if (!gic_data_rdist()->vpe_table_mask) 2831 return -ENOMEM; 2832 2833 val = inherit_vpe_l1_table_from_its(); 2834 if (val & GICR_VPROPBASER_4_1_VALID) 2835 goto out; 2836 2837 /* First probe the page size */ 2838 val = FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, GIC_PAGE_SIZE_64K); 2839 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); 2840 val = gicr_read_vpropbaser(vlpi_base + GICR_VPROPBASER); 2841 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val); 2842 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val); 2843 2844 switch (gpsz) { 2845 default: 2846 gpsz = GIC_PAGE_SIZE_4K; 2847 fallthrough; 2848 case GIC_PAGE_SIZE_4K: 2849 psz = SZ_4K; 2850 break; 2851 case GIC_PAGE_SIZE_16K: 2852 psz = SZ_16K; 2853 break; 2854 case GIC_PAGE_SIZE_64K: 2855 psz = SZ_64K; 2856 break; 2857 } 2858 2859 /* 2860 * Start populating the register from scratch, including RO fields 2861 * (which we want to print in debug cases...) 2862 */ 2863 val = 0; 2864 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, gpsz); 2865 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ENTRY_SIZE, esz); 2866 2867 /* How many entries per GIC page? */ 2868 esz++; 2869 epp = psz / (esz * SZ_8); 2870 2871 /* 2872 * If we need more than just a single L1 page, flag the table 2873 * as indirect and compute the number of required L1 pages. 2874 */ 2875 if (epp < ITS_MAX_VPEID) { 2876 int nl2; 2877 2878 val |= GICR_VPROPBASER_4_1_INDIRECT; 2879 2880 /* Number of L2 pages required to cover the VPEID space */ 2881 nl2 = DIV_ROUND_UP(ITS_MAX_VPEID, epp); 2882 2883 /* Number of L1 pages to point to the L2 pages */ 2884 npg = DIV_ROUND_UP(nl2 * SZ_8, psz); 2885 } else { 2886 npg = 1; 2887 } 2888 2889 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, npg - 1); 2890 2891 /* Right, that's the number of CPU pages we need for L1 */ 2892 np = DIV_ROUND_UP(npg * psz, PAGE_SIZE); 2893 2894 pr_debug("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n", 2895 np, npg, psz, epp, esz); 2896 page = alloc_pages(GFP_ATOMIC | __GFP_ZERO, get_order(np * PAGE_SIZE)); 2897 if (!page) 2898 return -ENOMEM; 2899 2900 gic_data_rdist()->vpe_l1_base = page_address(page); 2901 pa = virt_to_phys(page_address(page)); 2902 WARN_ON(!IS_ALIGNED(pa, psz)); 2903 2904 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, pa >> 12); 2905 val |= GICR_VPROPBASER_RaWb; 2906 val |= GICR_VPROPBASER_InnerShareable; 2907 val |= GICR_VPROPBASER_4_1_Z; 2908 val |= GICR_VPROPBASER_4_1_VALID; 2909 2910 out: 2911 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); 2912 cpumask_set_cpu(smp_processor_id(), gic_data_rdist()->vpe_table_mask); 2913 2914 pr_debug("CPU%d: VPROPBASER = %llx %*pbl\n", 2915 smp_processor_id(), val, 2916 cpumask_pr_args(gic_data_rdist()->vpe_table_mask)); 2917 2918 return 0; 2919 } 2920 2921 static int its_alloc_collections(struct its_node *its) 2922 { 2923 int i; 2924 2925 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections), 2926 GFP_KERNEL); 2927 if (!its->collections) 2928 return -ENOMEM; 2929 2930 for (i = 0; i < nr_cpu_ids; i++) 2931 its->collections[i].target_address = ~0ULL; 2932 2933 return 0; 2934 } 2935 2936 static struct page *its_allocate_pending_table(gfp_t gfp_flags) 2937 { 2938 struct page *pend_page; 2939 2940 pend_page = alloc_pages(gfp_flags | __GFP_ZERO, 2941 get_order(LPI_PENDBASE_SZ)); 2942 if (!pend_page) 2943 return NULL; 2944 2945 /* Make sure the GIC will observe the zero-ed page */ 2946 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ); 2947 2948 return pend_page; 2949 } 2950 2951 static void its_free_pending_table(struct page *pt) 2952 { 2953 free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ)); 2954 } 2955 2956 /* 2957 * Booting with kdump and LPIs enabled is generally fine. Any other 2958 * case is wrong in the absence of firmware/EFI support. 2959 */ 2960 static bool enabled_lpis_allowed(void) 2961 { 2962 phys_addr_t addr; 2963 u64 val; 2964 2965 /* Check whether the property table is in a reserved region */ 2966 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER); 2967 addr = val & GENMASK_ULL(51, 12); 2968 2969 return gic_check_reserved_range(addr, LPI_PROPBASE_SZ); 2970 } 2971 2972 static int __init allocate_lpi_tables(void) 2973 { 2974 u64 val; 2975 int err, cpu; 2976 2977 /* 2978 * If LPIs are enabled while we run this from the boot CPU, 2979 * flag the RD tables as pre-allocated if the stars do align. 2980 */ 2981 val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR); 2982 if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) { 2983 gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED | 2984 RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING); 2985 pr_info("GICv3: Using preallocated redistributor tables\n"); 2986 } 2987 2988 err = its_setup_lpi_prop_table(); 2989 if (err) 2990 return err; 2991 2992 /* 2993 * We allocate all the pending tables anyway, as we may have a 2994 * mix of RDs that have had LPIs enabled, and some that 2995 * don't. We'll free the unused ones as each CPU comes online. 2996 */ 2997 for_each_possible_cpu(cpu) { 2998 struct page *pend_page; 2999 3000 pend_page = its_allocate_pending_table(GFP_NOWAIT); 3001 if (!pend_page) { 3002 pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu); 3003 return -ENOMEM; 3004 } 3005 3006 gic_data_rdist_cpu(cpu)->pend_page = pend_page; 3007 } 3008 3009 return 0; 3010 } 3011 3012 static u64 its_clear_vpend_valid(void __iomem *vlpi_base, u64 clr, u64 set) 3013 { 3014 u32 count = 1000000; /* 1s! */ 3015 bool clean; 3016 u64 val; 3017 3018 val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER); 3019 val &= ~GICR_VPENDBASER_Valid; 3020 val &= ~clr; 3021 val |= set; 3022 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); 3023 3024 do { 3025 val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER); 3026 clean = !(val & GICR_VPENDBASER_Dirty); 3027 if (!clean) { 3028 count--; 3029 cpu_relax(); 3030 udelay(1); 3031 } 3032 } while (!clean && count); 3033 3034 if (unlikely(val & GICR_VPENDBASER_Dirty)) { 3035 pr_err_ratelimited("ITS virtual pending table not cleaning\n"); 3036 val |= GICR_VPENDBASER_PendingLast; 3037 } 3038 3039 return val; 3040 } 3041 3042 static void its_cpu_init_lpis(void) 3043 { 3044 void __iomem *rbase = gic_data_rdist_rd_base(); 3045 struct page *pend_page; 3046 phys_addr_t paddr; 3047 u64 val, tmp; 3048 3049 if (gic_data_rdist()->flags & RD_LOCAL_LPI_ENABLED) 3050 return; 3051 3052 val = readl_relaxed(rbase + GICR_CTLR); 3053 if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) && 3054 (val & GICR_CTLR_ENABLE_LPIS)) { 3055 /* 3056 * Check that we get the same property table on all 3057 * RDs. If we don't, this is hopeless. 3058 */ 3059 paddr = gicr_read_propbaser(rbase + GICR_PROPBASER); 3060 paddr &= GENMASK_ULL(51, 12); 3061 if (WARN_ON(gic_rdists->prop_table_pa != paddr)) 3062 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK); 3063 3064 paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER); 3065 paddr &= GENMASK_ULL(51, 16); 3066 3067 WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ)); 3068 its_free_pending_table(gic_data_rdist()->pend_page); 3069 gic_data_rdist()->pend_page = NULL; 3070 3071 goto out; 3072 } 3073 3074 pend_page = gic_data_rdist()->pend_page; 3075 paddr = page_to_phys(pend_page); 3076 WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ)); 3077 3078 /* set PROPBASE */ 3079 val = (gic_rdists->prop_table_pa | 3080 GICR_PROPBASER_InnerShareable | 3081 GICR_PROPBASER_RaWaWb | 3082 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK)); 3083 3084 gicr_write_propbaser(val, rbase + GICR_PROPBASER); 3085 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER); 3086 3087 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) { 3088 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) { 3089 /* 3090 * The HW reports non-shareable, we must 3091 * remove the cacheability attributes as 3092 * well. 3093 */ 3094 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK | 3095 GICR_PROPBASER_CACHEABILITY_MASK); 3096 val |= GICR_PROPBASER_nC; 3097 gicr_write_propbaser(val, rbase + GICR_PROPBASER); 3098 } 3099 pr_info_once("GIC: using cache flushing for LPI property table\n"); 3100 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING; 3101 } 3102 3103 /* set PENDBASE */ 3104 val = (page_to_phys(pend_page) | 3105 GICR_PENDBASER_InnerShareable | 3106 GICR_PENDBASER_RaWaWb); 3107 3108 gicr_write_pendbaser(val, rbase + GICR_PENDBASER); 3109 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER); 3110 3111 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) { 3112 /* 3113 * The HW reports non-shareable, we must remove the 3114 * cacheability attributes as well. 3115 */ 3116 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK | 3117 GICR_PENDBASER_CACHEABILITY_MASK); 3118 val |= GICR_PENDBASER_nC; 3119 gicr_write_pendbaser(val, rbase + GICR_PENDBASER); 3120 } 3121 3122 /* Enable LPIs */ 3123 val = readl_relaxed(rbase + GICR_CTLR); 3124 val |= GICR_CTLR_ENABLE_LPIS; 3125 writel_relaxed(val, rbase + GICR_CTLR); 3126 3127 if (gic_rdists->has_vlpis && !gic_rdists->has_rvpeid) { 3128 void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); 3129 3130 /* 3131 * It's possible for CPU to receive VLPIs before it is 3132 * scheduled as a vPE, especially for the first CPU, and the 3133 * VLPI with INTID larger than 2^(IDbits+1) will be considered 3134 * as out of range and dropped by GIC. 3135 * So we initialize IDbits to known value to avoid VLPI drop. 3136 */ 3137 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK; 3138 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n", 3139 smp_processor_id(), val); 3140 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); 3141 3142 /* 3143 * Also clear Valid bit of GICR_VPENDBASER, in case some 3144 * ancient programming gets left in and has possibility of 3145 * corrupting memory. 3146 */ 3147 val = its_clear_vpend_valid(vlpi_base, 0, 0); 3148 } 3149 3150 if (allocate_vpe_l1_table()) { 3151 /* 3152 * If the allocation has failed, we're in massive trouble. 3153 * Disable direct injection, and pray that no VM was 3154 * already running... 3155 */ 3156 gic_rdists->has_rvpeid = false; 3157 gic_rdists->has_vlpis = false; 3158 } 3159 3160 /* Make sure the GIC has seen the above */ 3161 dsb(sy); 3162 out: 3163 gic_data_rdist()->flags |= RD_LOCAL_LPI_ENABLED; 3164 pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n", 3165 smp_processor_id(), 3166 gic_data_rdist()->pend_page ? "allocated" : "reserved", 3167 &paddr); 3168 } 3169 3170 static void its_cpu_init_collection(struct its_node *its) 3171 { 3172 int cpu = smp_processor_id(); 3173 u64 target; 3174 3175 /* avoid cross node collections and its mapping */ 3176 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) { 3177 struct device_node *cpu_node; 3178 3179 cpu_node = of_get_cpu_node(cpu, NULL); 3180 if (its->numa_node != NUMA_NO_NODE && 3181 its->numa_node != of_node_to_nid(cpu_node)) 3182 return; 3183 } 3184 3185 /* 3186 * We now have to bind each collection to its target 3187 * redistributor. 3188 */ 3189 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) { 3190 /* 3191 * This ITS wants the physical address of the 3192 * redistributor. 3193 */ 3194 target = gic_data_rdist()->phys_base; 3195 } else { 3196 /* This ITS wants a linear CPU number. */ 3197 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER); 3198 target = GICR_TYPER_CPU_NUMBER(target) << 16; 3199 } 3200 3201 /* Perform collection mapping */ 3202 its->collections[cpu].target_address = target; 3203 its->collections[cpu].col_id = cpu; 3204 3205 its_send_mapc(its, &its->collections[cpu], 1); 3206 its_send_invall(its, &its->collections[cpu]); 3207 } 3208 3209 static void its_cpu_init_collections(void) 3210 { 3211 struct its_node *its; 3212 3213 raw_spin_lock(&its_lock); 3214 3215 list_for_each_entry(its, &its_nodes, entry) 3216 its_cpu_init_collection(its); 3217 3218 raw_spin_unlock(&its_lock); 3219 } 3220 3221 static struct its_device *its_find_device(struct its_node *its, u32 dev_id) 3222 { 3223 struct its_device *its_dev = NULL, *tmp; 3224 unsigned long flags; 3225 3226 raw_spin_lock_irqsave(&its->lock, flags); 3227 3228 list_for_each_entry(tmp, &its->its_device_list, entry) { 3229 if (tmp->device_id == dev_id) { 3230 its_dev = tmp; 3231 break; 3232 } 3233 } 3234 3235 raw_spin_unlock_irqrestore(&its->lock, flags); 3236 3237 return its_dev; 3238 } 3239 3240 static struct its_baser *its_get_baser(struct its_node *its, u32 type) 3241 { 3242 int i; 3243 3244 for (i = 0; i < GITS_BASER_NR_REGS; i++) { 3245 if (GITS_BASER_TYPE(its->tables[i].val) == type) 3246 return &its->tables[i]; 3247 } 3248 3249 return NULL; 3250 } 3251 3252 static bool its_alloc_table_entry(struct its_node *its, 3253 struct its_baser *baser, u32 id) 3254 { 3255 struct page *page; 3256 u32 esz, idx; 3257 __le64 *table; 3258 3259 /* Don't allow device id that exceeds single, flat table limit */ 3260 esz = GITS_BASER_ENTRY_SIZE(baser->val); 3261 if (!(baser->val & GITS_BASER_INDIRECT)) 3262 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz)); 3263 3264 /* Compute 1st level table index & check if that exceeds table limit */ 3265 idx = id >> ilog2(baser->psz / esz); 3266 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE)) 3267 return false; 3268 3269 table = baser->base; 3270 3271 /* Allocate memory for 2nd level table */ 3272 if (!table[idx]) { 3273 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, 3274 get_order(baser->psz)); 3275 if (!page) 3276 return false; 3277 3278 /* Flush Lvl2 table to PoC if hw doesn't support coherency */ 3279 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK)) 3280 gic_flush_dcache_to_poc(page_address(page), baser->psz); 3281 3282 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID); 3283 3284 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */ 3285 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK)) 3286 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE); 3287 3288 /* Ensure updated table contents are visible to ITS hardware */ 3289 dsb(sy); 3290 } 3291 3292 return true; 3293 } 3294 3295 static bool its_alloc_device_table(struct its_node *its, u32 dev_id) 3296 { 3297 struct its_baser *baser; 3298 3299 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE); 3300 3301 /* Don't allow device id that exceeds ITS hardware limit */ 3302 if (!baser) 3303 return (ilog2(dev_id) < device_ids(its)); 3304 3305 return its_alloc_table_entry(its, baser, dev_id); 3306 } 3307 3308 static bool its_alloc_vpe_table(u32 vpe_id) 3309 { 3310 struct its_node *its; 3311 int cpu; 3312 3313 /* 3314 * Make sure the L2 tables are allocated on *all* v4 ITSs. We 3315 * could try and only do it on ITSs corresponding to devices 3316 * that have interrupts targeted at this VPE, but the 3317 * complexity becomes crazy (and you have tons of memory 3318 * anyway, right?). 3319 */ 3320 list_for_each_entry(its, &its_nodes, entry) { 3321 struct its_baser *baser; 3322 3323 if (!is_v4(its)) 3324 continue; 3325 3326 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU); 3327 if (!baser) 3328 return false; 3329 3330 if (!its_alloc_table_entry(its, baser, vpe_id)) 3331 return false; 3332 } 3333 3334 /* Non v4.1? No need to iterate RDs and go back early. */ 3335 if (!gic_rdists->has_rvpeid) 3336 return true; 3337 3338 /* 3339 * Make sure the L2 tables are allocated for all copies of 3340 * the L1 table on *all* v4.1 RDs. 3341 */ 3342 for_each_possible_cpu(cpu) { 3343 if (!allocate_vpe_l2_table(cpu, vpe_id)) 3344 return false; 3345 } 3346 3347 return true; 3348 } 3349 3350 static struct its_device *its_create_device(struct its_node *its, u32 dev_id, 3351 int nvecs, bool alloc_lpis) 3352 { 3353 struct its_device *dev; 3354 unsigned long *lpi_map = NULL; 3355 unsigned long flags; 3356 u16 *col_map = NULL; 3357 void *itt; 3358 int lpi_base; 3359 int nr_lpis; 3360 int nr_ites; 3361 int sz; 3362 3363 if (!its_alloc_device_table(its, dev_id)) 3364 return NULL; 3365 3366 if (WARN_ON(!is_power_of_2(nvecs))) 3367 nvecs = roundup_pow_of_two(nvecs); 3368 3369 dev = kzalloc(sizeof(*dev), GFP_KERNEL); 3370 /* 3371 * Even if the device wants a single LPI, the ITT must be 3372 * sized as a power of two (and you need at least one bit...). 3373 */ 3374 nr_ites = max(2, nvecs); 3375 sz = nr_ites * (FIELD_GET(GITS_TYPER_ITT_ENTRY_SIZE, its->typer) + 1); 3376 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1; 3377 itt = kzalloc_node(sz, GFP_KERNEL, its->numa_node); 3378 if (alloc_lpis) { 3379 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis); 3380 if (lpi_map) 3381 col_map = kcalloc(nr_lpis, sizeof(*col_map), 3382 GFP_KERNEL); 3383 } else { 3384 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL); 3385 nr_lpis = 0; 3386 lpi_base = 0; 3387 } 3388 3389 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) { 3390 kfree(dev); 3391 kfree(itt); 3392 bitmap_free(lpi_map); 3393 kfree(col_map); 3394 return NULL; 3395 } 3396 3397 gic_flush_dcache_to_poc(itt, sz); 3398 3399 dev->its = its; 3400 dev->itt = itt; 3401 dev->nr_ites = nr_ites; 3402 dev->event_map.lpi_map = lpi_map; 3403 dev->event_map.col_map = col_map; 3404 dev->event_map.lpi_base = lpi_base; 3405 dev->event_map.nr_lpis = nr_lpis; 3406 raw_spin_lock_init(&dev->event_map.vlpi_lock); 3407 dev->device_id = dev_id; 3408 INIT_LIST_HEAD(&dev->entry); 3409 3410 raw_spin_lock_irqsave(&its->lock, flags); 3411 list_add(&dev->entry, &its->its_device_list); 3412 raw_spin_unlock_irqrestore(&its->lock, flags); 3413 3414 /* Map device to its ITT */ 3415 its_send_mapd(dev, 1); 3416 3417 return dev; 3418 } 3419 3420 static void its_free_device(struct its_device *its_dev) 3421 { 3422 unsigned long flags; 3423 3424 raw_spin_lock_irqsave(&its_dev->its->lock, flags); 3425 list_del(&its_dev->entry); 3426 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags); 3427 kfree(its_dev->event_map.col_map); 3428 kfree(its_dev->itt); 3429 kfree(its_dev); 3430 } 3431 3432 static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq) 3433 { 3434 int idx; 3435 3436 /* Find a free LPI region in lpi_map and allocate them. */ 3437 idx = bitmap_find_free_region(dev->event_map.lpi_map, 3438 dev->event_map.nr_lpis, 3439 get_count_order(nvecs)); 3440 if (idx < 0) 3441 return -ENOSPC; 3442 3443 *hwirq = dev->event_map.lpi_base + idx; 3444 3445 return 0; 3446 } 3447 3448 static int its_msi_prepare(struct irq_domain *domain, struct device *dev, 3449 int nvec, msi_alloc_info_t *info) 3450 { 3451 struct its_node *its; 3452 struct its_device *its_dev; 3453 struct msi_domain_info *msi_info; 3454 u32 dev_id; 3455 int err = 0; 3456 3457 /* 3458 * We ignore "dev" entirely, and rely on the dev_id that has 3459 * been passed via the scratchpad. This limits this domain's 3460 * usefulness to upper layers that definitely know that they 3461 * are built on top of the ITS. 3462 */ 3463 dev_id = info->scratchpad[0].ul; 3464 3465 msi_info = msi_get_domain_info(domain); 3466 its = msi_info->data; 3467 3468 if (!gic_rdists->has_direct_lpi && 3469 vpe_proxy.dev && 3470 vpe_proxy.dev->its == its && 3471 dev_id == vpe_proxy.dev->device_id) { 3472 /* Bad luck. Get yourself a better implementation */ 3473 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n", 3474 dev_id); 3475 return -EINVAL; 3476 } 3477 3478 mutex_lock(&its->dev_alloc_lock); 3479 its_dev = its_find_device(its, dev_id); 3480 if (its_dev) { 3481 /* 3482 * We already have seen this ID, probably through 3483 * another alias (PCI bridge of some sort). No need to 3484 * create the device. 3485 */ 3486 its_dev->shared = true; 3487 pr_debug("Reusing ITT for devID %x\n", dev_id); 3488 goto out; 3489 } 3490 3491 its_dev = its_create_device(its, dev_id, nvec, true); 3492 if (!its_dev) { 3493 err = -ENOMEM; 3494 goto out; 3495 } 3496 3497 if (info->flags & MSI_ALLOC_FLAGS_PROXY_DEVICE) 3498 its_dev->shared = true; 3499 3500 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec)); 3501 out: 3502 mutex_unlock(&its->dev_alloc_lock); 3503 info->scratchpad[0].ptr = its_dev; 3504 return err; 3505 } 3506 3507 static struct msi_domain_ops its_msi_domain_ops = { 3508 .msi_prepare = its_msi_prepare, 3509 }; 3510 3511 static int its_irq_gic_domain_alloc(struct irq_domain *domain, 3512 unsigned int virq, 3513 irq_hw_number_t hwirq) 3514 { 3515 struct irq_fwspec fwspec; 3516 3517 if (irq_domain_get_of_node(domain->parent)) { 3518 fwspec.fwnode = domain->parent->fwnode; 3519 fwspec.param_count = 3; 3520 fwspec.param[0] = GIC_IRQ_TYPE_LPI; 3521 fwspec.param[1] = hwirq; 3522 fwspec.param[2] = IRQ_TYPE_EDGE_RISING; 3523 } else if (is_fwnode_irqchip(domain->parent->fwnode)) { 3524 fwspec.fwnode = domain->parent->fwnode; 3525 fwspec.param_count = 2; 3526 fwspec.param[0] = hwirq; 3527 fwspec.param[1] = IRQ_TYPE_EDGE_RISING; 3528 } else { 3529 return -EINVAL; 3530 } 3531 3532 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec); 3533 } 3534 3535 static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, 3536 unsigned int nr_irqs, void *args) 3537 { 3538 msi_alloc_info_t *info = args; 3539 struct its_device *its_dev = info->scratchpad[0].ptr; 3540 struct its_node *its = its_dev->its; 3541 struct irq_data *irqd; 3542 irq_hw_number_t hwirq; 3543 int err; 3544 int i; 3545 3546 err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq); 3547 if (err) 3548 return err; 3549 3550 err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev)); 3551 if (err) 3552 return err; 3553 3554 for (i = 0; i < nr_irqs; i++) { 3555 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i); 3556 if (err) 3557 return err; 3558 3559 irq_domain_set_hwirq_and_chip(domain, virq + i, 3560 hwirq + i, &its_irq_chip, its_dev); 3561 irqd = irq_get_irq_data(virq + i); 3562 irqd_set_single_target(irqd); 3563 irqd_set_affinity_on_activate(irqd); 3564 pr_debug("ID:%d pID:%d vID:%d\n", 3565 (int)(hwirq + i - its_dev->event_map.lpi_base), 3566 (int)(hwirq + i), virq + i); 3567 } 3568 3569 return 0; 3570 } 3571 3572 static int its_irq_domain_activate(struct irq_domain *domain, 3573 struct irq_data *d, bool reserve) 3574 { 3575 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 3576 u32 event = its_get_event_id(d); 3577 int cpu; 3578 3579 cpu = its_select_cpu(d, cpu_online_mask); 3580 if (cpu < 0 || cpu >= nr_cpu_ids) 3581 return -EINVAL; 3582 3583 its_inc_lpi_count(d, cpu); 3584 its_dev->event_map.col_map[event] = cpu; 3585 irq_data_update_effective_affinity(d, cpumask_of(cpu)); 3586 3587 /* Map the GIC IRQ and event to the device */ 3588 its_send_mapti(its_dev, d->hwirq, event); 3589 return 0; 3590 } 3591 3592 static void its_irq_domain_deactivate(struct irq_domain *domain, 3593 struct irq_data *d) 3594 { 3595 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 3596 u32 event = its_get_event_id(d); 3597 3598 its_dec_lpi_count(d, its_dev->event_map.col_map[event]); 3599 /* Stop the delivery of interrupts */ 3600 its_send_discard(its_dev, event); 3601 } 3602 3603 static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq, 3604 unsigned int nr_irqs) 3605 { 3606 struct irq_data *d = irq_domain_get_irq_data(domain, virq); 3607 struct its_device *its_dev = irq_data_get_irq_chip_data(d); 3608 struct its_node *its = its_dev->its; 3609 int i; 3610 3611 bitmap_release_region(its_dev->event_map.lpi_map, 3612 its_get_event_id(irq_domain_get_irq_data(domain, virq)), 3613 get_count_order(nr_irqs)); 3614 3615 for (i = 0; i < nr_irqs; i++) { 3616 struct irq_data *data = irq_domain_get_irq_data(domain, 3617 virq + i); 3618 /* Nuke the entry in the domain */ 3619 irq_domain_reset_irq_data(data); 3620 } 3621 3622 mutex_lock(&its->dev_alloc_lock); 3623 3624 /* 3625 * If all interrupts have been freed, start mopping the 3626 * floor. This is conditioned on the device not being shared. 3627 */ 3628 if (!its_dev->shared && 3629 bitmap_empty(its_dev->event_map.lpi_map, 3630 its_dev->event_map.nr_lpis)) { 3631 its_lpi_free(its_dev->event_map.lpi_map, 3632 its_dev->event_map.lpi_base, 3633 its_dev->event_map.nr_lpis); 3634 3635 /* Unmap device/itt */ 3636 its_send_mapd(its_dev, 0); 3637 its_free_device(its_dev); 3638 } 3639 3640 mutex_unlock(&its->dev_alloc_lock); 3641 3642 irq_domain_free_irqs_parent(domain, virq, nr_irqs); 3643 } 3644 3645 static const struct irq_domain_ops its_domain_ops = { 3646 .alloc = its_irq_domain_alloc, 3647 .free = its_irq_domain_free, 3648 .activate = its_irq_domain_activate, 3649 .deactivate = its_irq_domain_deactivate, 3650 }; 3651 3652 /* 3653 * This is insane. 3654 * 3655 * If a GICv4.0 doesn't implement Direct LPIs (which is extremely 3656 * likely), the only way to perform an invalidate is to use a fake 3657 * device to issue an INV command, implying that the LPI has first 3658 * been mapped to some event on that device. Since this is not exactly 3659 * cheap, we try to keep that mapping around as long as possible, and 3660 * only issue an UNMAP if we're short on available slots. 3661 * 3662 * Broken by design(tm). 3663 * 3664 * GICv4.1, on the other hand, mandates that we're able to invalidate 3665 * by writing to a MMIO register. It doesn't implement the whole of 3666 * DirectLPI, but that's good enough. And most of the time, we don't 3667 * even have to invalidate anything, as the redistributor can be told 3668 * whether to generate a doorbell or not (we thus leave it enabled, 3669 * always). 3670 */ 3671 static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe) 3672 { 3673 /* GICv4.1 doesn't use a proxy, so nothing to do here */ 3674 if (gic_rdists->has_rvpeid) 3675 return; 3676 3677 /* Already unmapped? */ 3678 if (vpe->vpe_proxy_event == -1) 3679 return; 3680 3681 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event); 3682 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL; 3683 3684 /* 3685 * We don't track empty slots at all, so let's move the 3686 * next_victim pointer if we can quickly reuse that slot 3687 * instead of nuking an existing entry. Not clear that this is 3688 * always a win though, and this might just generate a ripple 3689 * effect... Let's just hope VPEs don't migrate too often. 3690 */ 3691 if (vpe_proxy.vpes[vpe_proxy.next_victim]) 3692 vpe_proxy.next_victim = vpe->vpe_proxy_event; 3693 3694 vpe->vpe_proxy_event = -1; 3695 } 3696 3697 static void its_vpe_db_proxy_unmap(struct its_vpe *vpe) 3698 { 3699 /* GICv4.1 doesn't use a proxy, so nothing to do here */ 3700 if (gic_rdists->has_rvpeid) 3701 return; 3702 3703 if (!gic_rdists->has_direct_lpi) { 3704 unsigned long flags; 3705 3706 raw_spin_lock_irqsave(&vpe_proxy.lock, flags); 3707 its_vpe_db_proxy_unmap_locked(vpe); 3708 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags); 3709 } 3710 } 3711 3712 static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe) 3713 { 3714 /* GICv4.1 doesn't use a proxy, so nothing to do here */ 3715 if (gic_rdists->has_rvpeid) 3716 return; 3717 3718 /* Already mapped? */ 3719 if (vpe->vpe_proxy_event != -1) 3720 return; 3721 3722 /* This slot was already allocated. Kick the other VPE out. */ 3723 if (vpe_proxy.vpes[vpe_proxy.next_victim]) 3724 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]); 3725 3726 /* Map the new VPE instead */ 3727 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe; 3728 vpe->vpe_proxy_event = vpe_proxy.next_victim; 3729 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites; 3730 3731 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx; 3732 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event); 3733 } 3734 3735 static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to) 3736 { 3737 unsigned long flags; 3738 struct its_collection *target_col; 3739 3740 /* GICv4.1 doesn't use a proxy, so nothing to do here */ 3741 if (gic_rdists->has_rvpeid) 3742 return; 3743 3744 if (gic_rdists->has_direct_lpi) { 3745 void __iomem *rdbase; 3746 3747 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base; 3748 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR); 3749 wait_for_syncr(rdbase); 3750 3751 return; 3752 } 3753 3754 raw_spin_lock_irqsave(&vpe_proxy.lock, flags); 3755 3756 its_vpe_db_proxy_map_locked(vpe); 3757 3758 target_col = &vpe_proxy.dev->its->collections[to]; 3759 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event); 3760 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to; 3761 3762 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags); 3763 } 3764 3765 static int its_vpe_set_affinity(struct irq_data *d, 3766 const struct cpumask *mask_val, 3767 bool force) 3768 { 3769 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 3770 int from, cpu = cpumask_first(mask_val); 3771 unsigned long flags; 3772 3773 /* 3774 * Changing affinity is mega expensive, so let's be as lazy as 3775 * we can and only do it if we really have to. Also, if mapped 3776 * into the proxy device, we need to move the doorbell 3777 * interrupt to its new location. 3778 * 3779 * Another thing is that changing the affinity of a vPE affects 3780 * *other interrupts* such as all the vLPIs that are routed to 3781 * this vPE. This means that the irq_desc lock is not enough to 3782 * protect us, and that we must ensure nobody samples vpe->col_idx 3783 * during the update, hence the lock below which must also be 3784 * taken on any vLPI handling path that evaluates vpe->col_idx. 3785 */ 3786 from = vpe_to_cpuid_lock(vpe, &flags); 3787 if (from == cpu) 3788 goto out; 3789 3790 vpe->col_idx = cpu; 3791 3792 /* 3793 * GICv4.1 allows us to skip VMOVP if moving to a cpu whose RD 3794 * is sharing its VPE table with the current one. 3795 */ 3796 if (gic_data_rdist_cpu(cpu)->vpe_table_mask && 3797 cpumask_test_cpu(from, gic_data_rdist_cpu(cpu)->vpe_table_mask)) 3798 goto out; 3799 3800 its_send_vmovp(vpe); 3801 its_vpe_db_proxy_move(vpe, from, cpu); 3802 3803 out: 3804 irq_data_update_effective_affinity(d, cpumask_of(cpu)); 3805 vpe_to_cpuid_unlock(vpe, flags); 3806 3807 return IRQ_SET_MASK_OK_DONE; 3808 } 3809 3810 static void its_wait_vpt_parse_complete(void) 3811 { 3812 void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); 3813 u64 val; 3814 3815 if (!gic_rdists->has_vpend_valid_dirty) 3816 return; 3817 3818 WARN_ON_ONCE(readq_relaxed_poll_timeout_atomic(vlpi_base + GICR_VPENDBASER, 3819 val, 3820 !(val & GICR_VPENDBASER_Dirty), 3821 1, 500)); 3822 } 3823 3824 static void its_vpe_schedule(struct its_vpe *vpe) 3825 { 3826 void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); 3827 u64 val; 3828 3829 /* Schedule the VPE */ 3830 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) & 3831 GENMASK_ULL(51, 12); 3832 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK; 3833 val |= GICR_VPROPBASER_RaWb; 3834 val |= GICR_VPROPBASER_InnerShareable; 3835 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); 3836 3837 val = virt_to_phys(page_address(vpe->vpt_page)) & 3838 GENMASK_ULL(51, 16); 3839 val |= GICR_VPENDBASER_RaWaWb; 3840 val |= GICR_VPENDBASER_InnerShareable; 3841 /* 3842 * There is no good way of finding out if the pending table is 3843 * empty as we can race against the doorbell interrupt very 3844 * easily. So in the end, vpe->pending_last is only an 3845 * indication that the vcpu has something pending, not one 3846 * that the pending table is empty. A good implementation 3847 * would be able to read its coarse map pretty quickly anyway, 3848 * making this a tolerable issue. 3849 */ 3850 val |= GICR_VPENDBASER_PendingLast; 3851 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0; 3852 val |= GICR_VPENDBASER_Valid; 3853 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); 3854 } 3855 3856 static void its_vpe_deschedule(struct its_vpe *vpe) 3857 { 3858 void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); 3859 u64 val; 3860 3861 val = its_clear_vpend_valid(vlpi_base, 0, 0); 3862 3863 vpe->idai = !!(val & GICR_VPENDBASER_IDAI); 3864 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast); 3865 } 3866 3867 static void its_vpe_invall(struct its_vpe *vpe) 3868 { 3869 struct its_node *its; 3870 3871 list_for_each_entry(its, &its_nodes, entry) { 3872 if (!is_v4(its)) 3873 continue; 3874 3875 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr]) 3876 continue; 3877 3878 /* 3879 * Sending a VINVALL to a single ITS is enough, as all 3880 * we need is to reach the redistributors. 3881 */ 3882 its_send_vinvall(its, vpe); 3883 return; 3884 } 3885 } 3886 3887 static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) 3888 { 3889 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 3890 struct its_cmd_info *info = vcpu_info; 3891 3892 switch (info->cmd_type) { 3893 case SCHEDULE_VPE: 3894 its_vpe_schedule(vpe); 3895 return 0; 3896 3897 case DESCHEDULE_VPE: 3898 its_vpe_deschedule(vpe); 3899 return 0; 3900 3901 case COMMIT_VPE: 3902 its_wait_vpt_parse_complete(); 3903 return 0; 3904 3905 case INVALL_VPE: 3906 its_vpe_invall(vpe); 3907 return 0; 3908 3909 default: 3910 return -EINVAL; 3911 } 3912 } 3913 3914 static void its_vpe_send_cmd(struct its_vpe *vpe, 3915 void (*cmd)(struct its_device *, u32)) 3916 { 3917 unsigned long flags; 3918 3919 raw_spin_lock_irqsave(&vpe_proxy.lock, flags); 3920 3921 its_vpe_db_proxy_map_locked(vpe); 3922 cmd(vpe_proxy.dev, vpe->vpe_proxy_event); 3923 3924 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags); 3925 } 3926 3927 static void its_vpe_send_inv(struct irq_data *d) 3928 { 3929 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 3930 3931 if (gic_rdists->has_direct_lpi) { 3932 void __iomem *rdbase; 3933 3934 /* Target the redistributor this VPE is currently known on */ 3935 raw_spin_lock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock); 3936 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base; 3937 gic_write_lpir(d->parent_data->hwirq, rdbase + GICR_INVLPIR); 3938 wait_for_syncr(rdbase); 3939 raw_spin_unlock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock); 3940 } else { 3941 its_vpe_send_cmd(vpe, its_send_inv); 3942 } 3943 } 3944 3945 static void its_vpe_mask_irq(struct irq_data *d) 3946 { 3947 /* 3948 * We need to unmask the LPI, which is described by the parent 3949 * irq_data. Instead of calling into the parent (which won't 3950 * exactly do the right thing, let's simply use the 3951 * parent_data pointer. Yes, I'm naughty. 3952 */ 3953 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0); 3954 its_vpe_send_inv(d); 3955 } 3956 3957 static void its_vpe_unmask_irq(struct irq_data *d) 3958 { 3959 /* Same hack as above... */ 3960 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED); 3961 its_vpe_send_inv(d); 3962 } 3963 3964 static int its_vpe_set_irqchip_state(struct irq_data *d, 3965 enum irqchip_irq_state which, 3966 bool state) 3967 { 3968 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 3969 3970 if (which != IRQCHIP_STATE_PENDING) 3971 return -EINVAL; 3972 3973 if (gic_rdists->has_direct_lpi) { 3974 void __iomem *rdbase; 3975 3976 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base; 3977 if (state) { 3978 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR); 3979 } else { 3980 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR); 3981 wait_for_syncr(rdbase); 3982 } 3983 } else { 3984 if (state) 3985 its_vpe_send_cmd(vpe, its_send_int); 3986 else 3987 its_vpe_send_cmd(vpe, its_send_clear); 3988 } 3989 3990 return 0; 3991 } 3992 3993 static int its_vpe_retrigger(struct irq_data *d) 3994 { 3995 return !its_vpe_set_irqchip_state(d, IRQCHIP_STATE_PENDING, true); 3996 } 3997 3998 static struct irq_chip its_vpe_irq_chip = { 3999 .name = "GICv4-vpe", 4000 .irq_mask = its_vpe_mask_irq, 4001 .irq_unmask = its_vpe_unmask_irq, 4002 .irq_eoi = irq_chip_eoi_parent, 4003 .irq_set_affinity = its_vpe_set_affinity, 4004 .irq_retrigger = its_vpe_retrigger, 4005 .irq_set_irqchip_state = its_vpe_set_irqchip_state, 4006 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity, 4007 }; 4008 4009 static struct its_node *find_4_1_its(void) 4010 { 4011 static struct its_node *its = NULL; 4012 4013 if (!its) { 4014 list_for_each_entry(its, &its_nodes, entry) { 4015 if (is_v4_1(its)) 4016 return its; 4017 } 4018 4019 /* Oops? */ 4020 its = NULL; 4021 } 4022 4023 return its; 4024 } 4025 4026 static void its_vpe_4_1_send_inv(struct irq_data *d) 4027 { 4028 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 4029 struct its_node *its; 4030 4031 /* 4032 * GICv4.1 wants doorbells to be invalidated using the 4033 * INVDB command in order to be broadcast to all RDs. Send 4034 * it to the first valid ITS, and let the HW do its magic. 4035 */ 4036 its = find_4_1_its(); 4037 if (its) 4038 its_send_invdb(its, vpe); 4039 } 4040 4041 static void its_vpe_4_1_mask_irq(struct irq_data *d) 4042 { 4043 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0); 4044 its_vpe_4_1_send_inv(d); 4045 } 4046 4047 static void its_vpe_4_1_unmask_irq(struct irq_data *d) 4048 { 4049 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED); 4050 its_vpe_4_1_send_inv(d); 4051 } 4052 4053 static void its_vpe_4_1_schedule(struct its_vpe *vpe, 4054 struct its_cmd_info *info) 4055 { 4056 void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); 4057 u64 val = 0; 4058 4059 /* Schedule the VPE */ 4060 val |= GICR_VPENDBASER_Valid; 4061 val |= info->g0en ? GICR_VPENDBASER_4_1_VGRP0EN : 0; 4062 val |= info->g1en ? GICR_VPENDBASER_4_1_VGRP1EN : 0; 4063 val |= FIELD_PREP(GICR_VPENDBASER_4_1_VPEID, vpe->vpe_id); 4064 4065 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); 4066 } 4067 4068 static void its_vpe_4_1_deschedule(struct its_vpe *vpe, 4069 struct its_cmd_info *info) 4070 { 4071 void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); 4072 u64 val; 4073 4074 if (info->req_db) { 4075 unsigned long flags; 4076 4077 /* 4078 * vPE is going to block: make the vPE non-resident with 4079 * PendingLast clear and DB set. The GIC guarantees that if 4080 * we read-back PendingLast clear, then a doorbell will be 4081 * delivered when an interrupt comes. 4082 * 4083 * Note the locking to deal with the concurrent update of 4084 * pending_last from the doorbell interrupt handler that can 4085 * run concurrently. 4086 */ 4087 raw_spin_lock_irqsave(&vpe->vpe_lock, flags); 4088 val = its_clear_vpend_valid(vlpi_base, 4089 GICR_VPENDBASER_PendingLast, 4090 GICR_VPENDBASER_4_1_DB); 4091 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast); 4092 raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags); 4093 } else { 4094 /* 4095 * We're not blocking, so just make the vPE non-resident 4096 * with PendingLast set, indicating that we'll be back. 4097 */ 4098 val = its_clear_vpend_valid(vlpi_base, 4099 0, 4100 GICR_VPENDBASER_PendingLast); 4101 vpe->pending_last = true; 4102 } 4103 } 4104 4105 static void its_vpe_4_1_invall(struct its_vpe *vpe) 4106 { 4107 void __iomem *rdbase; 4108 unsigned long flags; 4109 u64 val; 4110 int cpu; 4111 4112 val = GICR_INVALLR_V; 4113 val |= FIELD_PREP(GICR_INVALLR_VPEID, vpe->vpe_id); 4114 4115 /* Target the redistributor this vPE is currently known on */ 4116 cpu = vpe_to_cpuid_lock(vpe, &flags); 4117 raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock); 4118 rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base; 4119 gic_write_lpir(val, rdbase + GICR_INVALLR); 4120 4121 wait_for_syncr(rdbase); 4122 raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock); 4123 vpe_to_cpuid_unlock(vpe, flags); 4124 } 4125 4126 static int its_vpe_4_1_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) 4127 { 4128 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 4129 struct its_cmd_info *info = vcpu_info; 4130 4131 switch (info->cmd_type) { 4132 case SCHEDULE_VPE: 4133 its_vpe_4_1_schedule(vpe, info); 4134 return 0; 4135 4136 case DESCHEDULE_VPE: 4137 its_vpe_4_1_deschedule(vpe, info); 4138 return 0; 4139 4140 case COMMIT_VPE: 4141 its_wait_vpt_parse_complete(); 4142 return 0; 4143 4144 case INVALL_VPE: 4145 its_vpe_4_1_invall(vpe); 4146 return 0; 4147 4148 default: 4149 return -EINVAL; 4150 } 4151 } 4152 4153 static struct irq_chip its_vpe_4_1_irq_chip = { 4154 .name = "GICv4.1-vpe", 4155 .irq_mask = its_vpe_4_1_mask_irq, 4156 .irq_unmask = its_vpe_4_1_unmask_irq, 4157 .irq_eoi = irq_chip_eoi_parent, 4158 .irq_set_affinity = its_vpe_set_affinity, 4159 .irq_set_vcpu_affinity = its_vpe_4_1_set_vcpu_affinity, 4160 }; 4161 4162 static void its_configure_sgi(struct irq_data *d, bool clear) 4163 { 4164 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 4165 struct its_cmd_desc desc; 4166 4167 desc.its_vsgi_cmd.vpe = vpe; 4168 desc.its_vsgi_cmd.sgi = d->hwirq; 4169 desc.its_vsgi_cmd.priority = vpe->sgi_config[d->hwirq].priority; 4170 desc.its_vsgi_cmd.enable = vpe->sgi_config[d->hwirq].enabled; 4171 desc.its_vsgi_cmd.group = vpe->sgi_config[d->hwirq].group; 4172 desc.its_vsgi_cmd.clear = clear; 4173 4174 /* 4175 * GICv4.1 allows us to send VSGI commands to any ITS as long as the 4176 * destination VPE is mapped there. Since we map them eagerly at 4177 * activation time, we're pretty sure the first GICv4.1 ITS will do. 4178 */ 4179 its_send_single_vcommand(find_4_1_its(), its_build_vsgi_cmd, &desc); 4180 } 4181 4182 static void its_sgi_mask_irq(struct irq_data *d) 4183 { 4184 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 4185 4186 vpe->sgi_config[d->hwirq].enabled = false; 4187 its_configure_sgi(d, false); 4188 } 4189 4190 static void its_sgi_unmask_irq(struct irq_data *d) 4191 { 4192 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 4193 4194 vpe->sgi_config[d->hwirq].enabled = true; 4195 its_configure_sgi(d, false); 4196 } 4197 4198 static int its_sgi_set_affinity(struct irq_data *d, 4199 const struct cpumask *mask_val, 4200 bool force) 4201 { 4202 /* 4203 * There is no notion of affinity for virtual SGIs, at least 4204 * not on the host (since they can only be targeting a vPE). 4205 * Tell the kernel we've done whatever it asked for. 4206 */ 4207 irq_data_update_effective_affinity(d, mask_val); 4208 return IRQ_SET_MASK_OK; 4209 } 4210 4211 static int its_sgi_set_irqchip_state(struct irq_data *d, 4212 enum irqchip_irq_state which, 4213 bool state) 4214 { 4215 if (which != IRQCHIP_STATE_PENDING) 4216 return -EINVAL; 4217 4218 if (state) { 4219 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 4220 struct its_node *its = find_4_1_its(); 4221 u64 val; 4222 4223 val = FIELD_PREP(GITS_SGIR_VPEID, vpe->vpe_id); 4224 val |= FIELD_PREP(GITS_SGIR_VINTID, d->hwirq); 4225 writeq_relaxed(val, its->sgir_base + GITS_SGIR - SZ_128K); 4226 } else { 4227 its_configure_sgi(d, true); 4228 } 4229 4230 return 0; 4231 } 4232 4233 static int its_sgi_get_irqchip_state(struct irq_data *d, 4234 enum irqchip_irq_state which, bool *val) 4235 { 4236 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 4237 void __iomem *base; 4238 unsigned long flags; 4239 u32 count = 1000000; /* 1s! */ 4240 u32 status; 4241 int cpu; 4242 4243 if (which != IRQCHIP_STATE_PENDING) 4244 return -EINVAL; 4245 4246 /* 4247 * Locking galore! We can race against two different events: 4248 * 4249 * - Concurrent vPE affinity change: we must make sure it cannot 4250 * happen, or we'll talk to the wrong redistributor. This is 4251 * identical to what happens with vLPIs. 4252 * 4253 * - Concurrent VSGIPENDR access: As it involves accessing two 4254 * MMIO registers, this must be made atomic one way or another. 4255 */ 4256 cpu = vpe_to_cpuid_lock(vpe, &flags); 4257 raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock); 4258 base = gic_data_rdist_cpu(cpu)->rd_base + SZ_128K; 4259 writel_relaxed(vpe->vpe_id, base + GICR_VSGIR); 4260 do { 4261 status = readl_relaxed(base + GICR_VSGIPENDR); 4262 if (!(status & GICR_VSGIPENDR_BUSY)) 4263 goto out; 4264 4265 count--; 4266 if (!count) { 4267 pr_err_ratelimited("Unable to get SGI status\n"); 4268 goto out; 4269 } 4270 cpu_relax(); 4271 udelay(1); 4272 } while (count); 4273 4274 out: 4275 raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock); 4276 vpe_to_cpuid_unlock(vpe, flags); 4277 4278 if (!count) 4279 return -ENXIO; 4280 4281 *val = !!(status & (1 << d->hwirq)); 4282 4283 return 0; 4284 } 4285 4286 static int its_sgi_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) 4287 { 4288 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 4289 struct its_cmd_info *info = vcpu_info; 4290 4291 switch (info->cmd_type) { 4292 case PROP_UPDATE_VSGI: 4293 vpe->sgi_config[d->hwirq].priority = info->priority; 4294 vpe->sgi_config[d->hwirq].group = info->group; 4295 its_configure_sgi(d, false); 4296 return 0; 4297 4298 default: 4299 return -EINVAL; 4300 } 4301 } 4302 4303 static struct irq_chip its_sgi_irq_chip = { 4304 .name = "GICv4.1-sgi", 4305 .irq_mask = its_sgi_mask_irq, 4306 .irq_unmask = its_sgi_unmask_irq, 4307 .irq_set_affinity = its_sgi_set_affinity, 4308 .irq_set_irqchip_state = its_sgi_set_irqchip_state, 4309 .irq_get_irqchip_state = its_sgi_get_irqchip_state, 4310 .irq_set_vcpu_affinity = its_sgi_set_vcpu_affinity, 4311 }; 4312 4313 static int its_sgi_irq_domain_alloc(struct irq_domain *domain, 4314 unsigned int virq, unsigned int nr_irqs, 4315 void *args) 4316 { 4317 struct its_vpe *vpe = args; 4318 int i; 4319 4320 /* Yes, we do want 16 SGIs */ 4321 WARN_ON(nr_irqs != 16); 4322 4323 for (i = 0; i < 16; i++) { 4324 vpe->sgi_config[i].priority = 0; 4325 vpe->sgi_config[i].enabled = false; 4326 vpe->sgi_config[i].group = false; 4327 4328 irq_domain_set_hwirq_and_chip(domain, virq + i, i, 4329 &its_sgi_irq_chip, vpe); 4330 irq_set_status_flags(virq + i, IRQ_DISABLE_UNLAZY); 4331 } 4332 4333 return 0; 4334 } 4335 4336 static void its_sgi_irq_domain_free(struct irq_domain *domain, 4337 unsigned int virq, 4338 unsigned int nr_irqs) 4339 { 4340 /* Nothing to do */ 4341 } 4342 4343 static int its_sgi_irq_domain_activate(struct irq_domain *domain, 4344 struct irq_data *d, bool reserve) 4345 { 4346 /* Write out the initial SGI configuration */ 4347 its_configure_sgi(d, false); 4348 return 0; 4349 } 4350 4351 static void its_sgi_irq_domain_deactivate(struct irq_domain *domain, 4352 struct irq_data *d) 4353 { 4354 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 4355 4356 /* 4357 * The VSGI command is awkward: 4358 * 4359 * - To change the configuration, CLEAR must be set to false, 4360 * leaving the pending bit unchanged. 4361 * - To clear the pending bit, CLEAR must be set to true, leaving 4362 * the configuration unchanged. 4363 * 4364 * You just can't do both at once, hence the two commands below. 4365 */ 4366 vpe->sgi_config[d->hwirq].enabled = false; 4367 its_configure_sgi(d, false); 4368 its_configure_sgi(d, true); 4369 } 4370 4371 static const struct irq_domain_ops its_sgi_domain_ops = { 4372 .alloc = its_sgi_irq_domain_alloc, 4373 .free = its_sgi_irq_domain_free, 4374 .activate = its_sgi_irq_domain_activate, 4375 .deactivate = its_sgi_irq_domain_deactivate, 4376 }; 4377 4378 static int its_vpe_id_alloc(void) 4379 { 4380 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL); 4381 } 4382 4383 static void its_vpe_id_free(u16 id) 4384 { 4385 ida_simple_remove(&its_vpeid_ida, id); 4386 } 4387 4388 static int its_vpe_init(struct its_vpe *vpe) 4389 { 4390 struct page *vpt_page; 4391 int vpe_id; 4392 4393 /* Allocate vpe_id */ 4394 vpe_id = its_vpe_id_alloc(); 4395 if (vpe_id < 0) 4396 return vpe_id; 4397 4398 /* Allocate VPT */ 4399 vpt_page = its_allocate_pending_table(GFP_KERNEL); 4400 if (!vpt_page) { 4401 its_vpe_id_free(vpe_id); 4402 return -ENOMEM; 4403 } 4404 4405 if (!its_alloc_vpe_table(vpe_id)) { 4406 its_vpe_id_free(vpe_id); 4407 its_free_pending_table(vpt_page); 4408 return -ENOMEM; 4409 } 4410 4411 raw_spin_lock_init(&vpe->vpe_lock); 4412 vpe->vpe_id = vpe_id; 4413 vpe->vpt_page = vpt_page; 4414 if (gic_rdists->has_rvpeid) 4415 atomic_set(&vpe->vmapp_count, 0); 4416 else 4417 vpe->vpe_proxy_event = -1; 4418 4419 return 0; 4420 } 4421 4422 static void its_vpe_teardown(struct its_vpe *vpe) 4423 { 4424 its_vpe_db_proxy_unmap(vpe); 4425 its_vpe_id_free(vpe->vpe_id); 4426 its_free_pending_table(vpe->vpt_page); 4427 } 4428 4429 static void its_vpe_irq_domain_free(struct irq_domain *domain, 4430 unsigned int virq, 4431 unsigned int nr_irqs) 4432 { 4433 struct its_vm *vm = domain->host_data; 4434 int i; 4435 4436 irq_domain_free_irqs_parent(domain, virq, nr_irqs); 4437 4438 for (i = 0; i < nr_irqs; i++) { 4439 struct irq_data *data = irq_domain_get_irq_data(domain, 4440 virq + i); 4441 struct its_vpe *vpe = irq_data_get_irq_chip_data(data); 4442 4443 BUG_ON(vm != vpe->its_vm); 4444 4445 clear_bit(data->hwirq, vm->db_bitmap); 4446 its_vpe_teardown(vpe); 4447 irq_domain_reset_irq_data(data); 4448 } 4449 4450 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) { 4451 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis); 4452 its_free_prop_table(vm->vprop_page); 4453 } 4454 } 4455 4456 static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, 4457 unsigned int nr_irqs, void *args) 4458 { 4459 struct irq_chip *irqchip = &its_vpe_irq_chip; 4460 struct its_vm *vm = args; 4461 unsigned long *bitmap; 4462 struct page *vprop_page; 4463 int base, nr_ids, i, err = 0; 4464 4465 BUG_ON(!vm); 4466 4467 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids); 4468 if (!bitmap) 4469 return -ENOMEM; 4470 4471 if (nr_ids < nr_irqs) { 4472 its_lpi_free(bitmap, base, nr_ids); 4473 return -ENOMEM; 4474 } 4475 4476 vprop_page = its_allocate_prop_table(GFP_KERNEL); 4477 if (!vprop_page) { 4478 its_lpi_free(bitmap, base, nr_ids); 4479 return -ENOMEM; 4480 } 4481 4482 vm->db_bitmap = bitmap; 4483 vm->db_lpi_base = base; 4484 vm->nr_db_lpis = nr_ids; 4485 vm->vprop_page = vprop_page; 4486 4487 if (gic_rdists->has_rvpeid) 4488 irqchip = &its_vpe_4_1_irq_chip; 4489 4490 for (i = 0; i < nr_irqs; i++) { 4491 vm->vpes[i]->vpe_db_lpi = base + i; 4492 err = its_vpe_init(vm->vpes[i]); 4493 if (err) 4494 break; 4495 err = its_irq_gic_domain_alloc(domain, virq + i, 4496 vm->vpes[i]->vpe_db_lpi); 4497 if (err) 4498 break; 4499 irq_domain_set_hwirq_and_chip(domain, virq + i, i, 4500 irqchip, vm->vpes[i]); 4501 set_bit(i, bitmap); 4502 } 4503 4504 if (err) { 4505 if (i > 0) 4506 its_vpe_irq_domain_free(domain, virq, i); 4507 4508 its_lpi_free(bitmap, base, nr_ids); 4509 its_free_prop_table(vprop_page); 4510 } 4511 4512 return err; 4513 } 4514 4515 static int its_vpe_irq_domain_activate(struct irq_domain *domain, 4516 struct irq_data *d, bool reserve) 4517 { 4518 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 4519 struct its_node *its; 4520 4521 /* 4522 * If we use the list map, we issue VMAPP on demand... Unless 4523 * we're on a GICv4.1 and we eagerly map the VPE on all ITSs 4524 * so that VSGIs can work. 4525 */ 4526 if (!gic_requires_eager_mapping()) 4527 return 0; 4528 4529 /* Map the VPE to the first possible CPU */ 4530 vpe->col_idx = cpumask_first(cpu_online_mask); 4531 4532 list_for_each_entry(its, &its_nodes, entry) { 4533 if (!is_v4(its)) 4534 continue; 4535 4536 its_send_vmapp(its, vpe, true); 4537 its_send_vinvall(its, vpe); 4538 } 4539 4540 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx)); 4541 4542 return 0; 4543 } 4544 4545 static void its_vpe_irq_domain_deactivate(struct irq_domain *domain, 4546 struct irq_data *d) 4547 { 4548 struct its_vpe *vpe = irq_data_get_irq_chip_data(d); 4549 struct its_node *its; 4550 4551 /* 4552 * If we use the list map on GICv4.0, we unmap the VPE once no 4553 * VLPIs are associated with the VM. 4554 */ 4555 if (!gic_requires_eager_mapping()) 4556 return; 4557 4558 list_for_each_entry(its, &its_nodes, entry) { 4559 if (!is_v4(its)) 4560 continue; 4561 4562 its_send_vmapp(its, vpe, false); 4563 } 4564 4565 /* 4566 * There may be a direct read to the VPT after unmapping the 4567 * vPE, to guarantee the validity of this, we make the VPT 4568 * memory coherent with the CPU caches here. 4569 */ 4570 if (find_4_1_its() && !atomic_read(&vpe->vmapp_count)) 4571 gic_flush_dcache_to_poc(page_address(vpe->vpt_page), 4572 LPI_PENDBASE_SZ); 4573 } 4574 4575 static const struct irq_domain_ops its_vpe_domain_ops = { 4576 .alloc = its_vpe_irq_domain_alloc, 4577 .free = its_vpe_irq_domain_free, 4578 .activate = its_vpe_irq_domain_activate, 4579 .deactivate = its_vpe_irq_domain_deactivate, 4580 }; 4581 4582 static int its_force_quiescent(void __iomem *base) 4583 { 4584 u32 count = 1000000; /* 1s */ 4585 u32 val; 4586 4587 val = readl_relaxed(base + GITS_CTLR); 4588 /* 4589 * GIC architecture specification requires the ITS to be both 4590 * disabled and quiescent for writes to GITS_BASER<n> or 4591 * GITS_CBASER to not have UNPREDICTABLE results. 4592 */ 4593 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE)) 4594 return 0; 4595 4596 /* Disable the generation of all interrupts to this ITS */ 4597 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe); 4598 writel_relaxed(val, base + GITS_CTLR); 4599 4600 /* Poll GITS_CTLR and wait until ITS becomes quiescent */ 4601 while (1) { 4602 val = readl_relaxed(base + GITS_CTLR); 4603 if (val & GITS_CTLR_QUIESCENT) 4604 return 0; 4605 4606 count--; 4607 if (!count) 4608 return -EBUSY; 4609 4610 cpu_relax(); 4611 udelay(1); 4612 } 4613 } 4614 4615 static bool __maybe_unused its_enable_quirk_cavium_22375(void *data) 4616 { 4617 struct its_node *its = data; 4618 4619 /* erratum 22375: only alloc 8MB table size (20 bits) */ 4620 its->typer &= ~GITS_TYPER_DEVBITS; 4621 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, 20 - 1); 4622 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375; 4623 4624 return true; 4625 } 4626 4627 static bool __maybe_unused its_enable_quirk_cavium_23144(void *data) 4628 { 4629 struct its_node *its = data; 4630 4631 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144; 4632 4633 return true; 4634 } 4635 4636 static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data) 4637 { 4638 struct its_node *its = data; 4639 4640 /* On QDF2400, the size of the ITE is 16Bytes */ 4641 its->typer &= ~GITS_TYPER_ITT_ENTRY_SIZE; 4642 its->typer |= FIELD_PREP(GITS_TYPER_ITT_ENTRY_SIZE, 16 - 1); 4643 4644 return true; 4645 } 4646 4647 static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev) 4648 { 4649 struct its_node *its = its_dev->its; 4650 4651 /* 4652 * The Socionext Synquacer SoC has a so-called 'pre-ITS', 4653 * which maps 32-bit writes targeted at a separate window of 4654 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER 4655 * with device ID taken from bits [device_id_bits + 1:2] of 4656 * the window offset. 4657 */ 4658 return its->pre_its_base + (its_dev->device_id << 2); 4659 } 4660 4661 static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data) 4662 { 4663 struct its_node *its = data; 4664 u32 pre_its_window[2]; 4665 u32 ids; 4666 4667 if (!fwnode_property_read_u32_array(its->fwnode_handle, 4668 "socionext,synquacer-pre-its", 4669 pre_its_window, 4670 ARRAY_SIZE(pre_its_window))) { 4671 4672 its->pre_its_base = pre_its_window[0]; 4673 its->get_msi_base = its_irq_get_msi_base_pre_its; 4674 4675 ids = ilog2(pre_its_window[1]) - 2; 4676 if (device_ids(its) > ids) { 4677 its->typer &= ~GITS_TYPER_DEVBITS; 4678 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, ids - 1); 4679 } 4680 4681 /* the pre-ITS breaks isolation, so disable MSI remapping */ 4682 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP; 4683 return true; 4684 } 4685 return false; 4686 } 4687 4688 static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data) 4689 { 4690 struct its_node *its = data; 4691 4692 /* 4693 * Hip07 insists on using the wrong address for the VLPI 4694 * page. Trick it into doing the right thing... 4695 */ 4696 its->vlpi_redist_offset = SZ_128K; 4697 return true; 4698 } 4699 4700 static const struct gic_quirk its_quirks[] = { 4701 #ifdef CONFIG_CAVIUM_ERRATUM_22375 4702 { 4703 .desc = "ITS: Cavium errata 22375, 24313", 4704 .iidr = 0xa100034c, /* ThunderX pass 1.x */ 4705 .mask = 0xffff0fff, 4706 .init = its_enable_quirk_cavium_22375, 4707 }, 4708 #endif 4709 #ifdef CONFIG_CAVIUM_ERRATUM_23144 4710 { 4711 .desc = "ITS: Cavium erratum 23144", 4712 .iidr = 0xa100034c, /* ThunderX pass 1.x */ 4713 .mask = 0xffff0fff, 4714 .init = its_enable_quirk_cavium_23144, 4715 }, 4716 #endif 4717 #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065 4718 { 4719 .desc = "ITS: QDF2400 erratum 0065", 4720 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */ 4721 .mask = 0xffffffff, 4722 .init = its_enable_quirk_qdf2400_e0065, 4723 }, 4724 #endif 4725 #ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS 4726 { 4727 /* 4728 * The Socionext Synquacer SoC incorporates ARM's own GIC-500 4729 * implementation, but with a 'pre-ITS' added that requires 4730 * special handling in software. 4731 */ 4732 .desc = "ITS: Socionext Synquacer pre-ITS", 4733 .iidr = 0x0001143b, 4734 .mask = 0xffffffff, 4735 .init = its_enable_quirk_socionext_synquacer, 4736 }, 4737 #endif 4738 #ifdef CONFIG_HISILICON_ERRATUM_161600802 4739 { 4740 .desc = "ITS: Hip07 erratum 161600802", 4741 .iidr = 0x00000004, 4742 .mask = 0xffffffff, 4743 .init = its_enable_quirk_hip07_161600802, 4744 }, 4745 #endif 4746 { 4747 } 4748 }; 4749 4750 static void its_enable_quirks(struct its_node *its) 4751 { 4752 u32 iidr = readl_relaxed(its->base + GITS_IIDR); 4753 4754 gic_enable_quirks(iidr, its_quirks, its); 4755 } 4756 4757 static int its_save_disable(void) 4758 { 4759 struct its_node *its; 4760 int err = 0; 4761 4762 raw_spin_lock(&its_lock); 4763 list_for_each_entry(its, &its_nodes, entry) { 4764 void __iomem *base; 4765 4766 base = its->base; 4767 its->ctlr_save = readl_relaxed(base + GITS_CTLR); 4768 err = its_force_quiescent(base); 4769 if (err) { 4770 pr_err("ITS@%pa: failed to quiesce: %d\n", 4771 &its->phys_base, err); 4772 writel_relaxed(its->ctlr_save, base + GITS_CTLR); 4773 goto err; 4774 } 4775 4776 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER); 4777 } 4778 4779 err: 4780 if (err) { 4781 list_for_each_entry_continue_reverse(its, &its_nodes, entry) { 4782 void __iomem *base; 4783 4784 base = its->base; 4785 writel_relaxed(its->ctlr_save, base + GITS_CTLR); 4786 } 4787 } 4788 raw_spin_unlock(&its_lock); 4789 4790 return err; 4791 } 4792 4793 static void its_restore_enable(void) 4794 { 4795 struct its_node *its; 4796 int ret; 4797 4798 raw_spin_lock(&its_lock); 4799 list_for_each_entry(its, &its_nodes, entry) { 4800 void __iomem *base; 4801 int i; 4802 4803 base = its->base; 4804 4805 /* 4806 * Make sure that the ITS is disabled. If it fails to quiesce, 4807 * don't restore it since writing to CBASER or BASER<n> 4808 * registers is undefined according to the GIC v3 ITS 4809 * Specification. 4810 * 4811 * Firmware resuming with the ITS enabled is terminally broken. 4812 */ 4813 WARN_ON(readl_relaxed(base + GITS_CTLR) & GITS_CTLR_ENABLE); 4814 ret = its_force_quiescent(base); 4815 if (ret) { 4816 pr_err("ITS@%pa: failed to quiesce on resume: %d\n", 4817 &its->phys_base, ret); 4818 continue; 4819 } 4820 4821 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER); 4822 4823 /* 4824 * Writing CBASER resets CREADR to 0, so make CWRITER and 4825 * cmd_write line up with it. 4826 */ 4827 its->cmd_write = its->cmd_base; 4828 gits_write_cwriter(0, base + GITS_CWRITER); 4829 4830 /* Restore GITS_BASER from the value cache. */ 4831 for (i = 0; i < GITS_BASER_NR_REGS; i++) { 4832 struct its_baser *baser = &its->tables[i]; 4833 4834 if (!(baser->val & GITS_BASER_VALID)) 4835 continue; 4836 4837 its_write_baser(its, baser, baser->val); 4838 } 4839 writel_relaxed(its->ctlr_save, base + GITS_CTLR); 4840 4841 /* 4842 * Reinit the collection if it's stored in the ITS. This is 4843 * indicated by the col_id being less than the HCC field. 4844 * CID < HCC as specified in the GIC v3 Documentation. 4845 */ 4846 if (its->collections[smp_processor_id()].col_id < 4847 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER))) 4848 its_cpu_init_collection(its); 4849 } 4850 raw_spin_unlock(&its_lock); 4851 } 4852 4853 static struct syscore_ops its_syscore_ops = { 4854 .suspend = its_save_disable, 4855 .resume = its_restore_enable, 4856 }; 4857 4858 static int its_init_domain(struct fwnode_handle *handle, struct its_node *its) 4859 { 4860 struct irq_domain *inner_domain; 4861 struct msi_domain_info *info; 4862 4863 info = kzalloc(sizeof(*info), GFP_KERNEL); 4864 if (!info) 4865 return -ENOMEM; 4866 4867 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its); 4868 if (!inner_domain) { 4869 kfree(info); 4870 return -ENOMEM; 4871 } 4872 4873 inner_domain->parent = its_parent; 4874 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS); 4875 inner_domain->flags |= its->msi_domain_flags; 4876 info->ops = &its_msi_domain_ops; 4877 info->data = its; 4878 inner_domain->host_data = info; 4879 4880 return 0; 4881 } 4882 4883 static int its_init_vpe_domain(void) 4884 { 4885 struct its_node *its; 4886 u32 devid; 4887 int entries; 4888 4889 if (gic_rdists->has_direct_lpi) { 4890 pr_info("ITS: Using DirectLPI for VPE invalidation\n"); 4891 return 0; 4892 } 4893 4894 /* Any ITS will do, even if not v4 */ 4895 its = list_first_entry(&its_nodes, struct its_node, entry); 4896 4897 entries = roundup_pow_of_two(nr_cpu_ids); 4898 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes), 4899 GFP_KERNEL); 4900 if (!vpe_proxy.vpes) 4901 return -ENOMEM; 4902 4903 /* Use the last possible DevID */ 4904 devid = GENMASK(device_ids(its) - 1, 0); 4905 vpe_proxy.dev = its_create_device(its, devid, entries, false); 4906 if (!vpe_proxy.dev) { 4907 kfree(vpe_proxy.vpes); 4908 pr_err("ITS: Can't allocate GICv4 proxy device\n"); 4909 return -ENOMEM; 4910 } 4911 4912 BUG_ON(entries > vpe_proxy.dev->nr_ites); 4913 4914 raw_spin_lock_init(&vpe_proxy.lock); 4915 vpe_proxy.next_victim = 0; 4916 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n", 4917 devid, vpe_proxy.dev->nr_ites); 4918 4919 return 0; 4920 } 4921 4922 static int __init its_compute_its_list_map(struct resource *res, 4923 void __iomem *its_base) 4924 { 4925 int its_number; 4926 u32 ctlr; 4927 4928 /* 4929 * This is assumed to be done early enough that we're 4930 * guaranteed to be single-threaded, hence no 4931 * locking. Should this change, we should address 4932 * this. 4933 */ 4934 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX); 4935 if (its_number >= GICv4_ITS_LIST_MAX) { 4936 pr_err("ITS@%pa: No ITSList entry available!\n", 4937 &res->start); 4938 return -EINVAL; 4939 } 4940 4941 ctlr = readl_relaxed(its_base + GITS_CTLR); 4942 ctlr &= ~GITS_CTLR_ITS_NUMBER; 4943 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT; 4944 writel_relaxed(ctlr, its_base + GITS_CTLR); 4945 ctlr = readl_relaxed(its_base + GITS_CTLR); 4946 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) { 4947 its_number = ctlr & GITS_CTLR_ITS_NUMBER; 4948 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT; 4949 } 4950 4951 if (test_and_set_bit(its_number, &its_list_map)) { 4952 pr_err("ITS@%pa: Duplicate ITSList entry %d\n", 4953 &res->start, its_number); 4954 return -EINVAL; 4955 } 4956 4957 return its_number; 4958 } 4959 4960 static int __init its_probe_one(struct resource *res, 4961 struct fwnode_handle *handle, int numa_node) 4962 { 4963 struct its_node *its; 4964 void __iomem *its_base; 4965 u32 val, ctlr; 4966 u64 baser, tmp, typer; 4967 struct page *page; 4968 int err; 4969 4970 its_base = ioremap(res->start, SZ_64K); 4971 if (!its_base) { 4972 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start); 4973 return -ENOMEM; 4974 } 4975 4976 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK; 4977 if (val != 0x30 && val != 0x40) { 4978 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start); 4979 err = -ENODEV; 4980 goto out_unmap; 4981 } 4982 4983 err = its_force_quiescent(its_base); 4984 if (err) { 4985 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start); 4986 goto out_unmap; 4987 } 4988 4989 pr_info("ITS %pR\n", res); 4990 4991 its = kzalloc(sizeof(*its), GFP_KERNEL); 4992 if (!its) { 4993 err = -ENOMEM; 4994 goto out_unmap; 4995 } 4996 4997 raw_spin_lock_init(&its->lock); 4998 mutex_init(&its->dev_alloc_lock); 4999 INIT_LIST_HEAD(&its->entry); 5000 INIT_LIST_HEAD(&its->its_device_list); 5001 typer = gic_read_typer(its_base + GITS_TYPER); 5002 its->typer = typer; 5003 its->base = its_base; 5004 its->phys_base = res->start; 5005 if (is_v4(its)) { 5006 if (!(typer & GITS_TYPER_VMOVP)) { 5007 err = its_compute_its_list_map(res, its_base); 5008 if (err < 0) 5009 goto out_free_its; 5010 5011 its->list_nr = err; 5012 5013 pr_info("ITS@%pa: Using ITS number %d\n", 5014 &res->start, err); 5015 } else { 5016 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start); 5017 } 5018 5019 if (is_v4_1(its)) { 5020 u32 svpet = FIELD_GET(GITS_TYPER_SVPET, typer); 5021 5022 its->sgir_base = ioremap(res->start + SZ_128K, SZ_64K); 5023 if (!its->sgir_base) { 5024 err = -ENOMEM; 5025 goto out_free_its; 5026 } 5027 5028 its->mpidr = readl_relaxed(its_base + GITS_MPIDR); 5029 5030 pr_info("ITS@%pa: Using GICv4.1 mode %08x %08x\n", 5031 &res->start, its->mpidr, svpet); 5032 } 5033 } 5034 5035 its->numa_node = numa_node; 5036 5037 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, 5038 get_order(ITS_CMD_QUEUE_SZ)); 5039 if (!page) { 5040 err = -ENOMEM; 5041 goto out_unmap_sgir; 5042 } 5043 its->cmd_base = (void *)page_address(page); 5044 its->cmd_write = its->cmd_base; 5045 its->fwnode_handle = handle; 5046 its->get_msi_base = its_irq_get_msi_base; 5047 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP; 5048 5049 its_enable_quirks(its); 5050 5051 err = its_alloc_tables(its); 5052 if (err) 5053 goto out_free_cmd; 5054 5055 err = its_alloc_collections(its); 5056 if (err) 5057 goto out_free_tables; 5058 5059 baser = (virt_to_phys(its->cmd_base) | 5060 GITS_CBASER_RaWaWb | 5061 GITS_CBASER_InnerShareable | 5062 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) | 5063 GITS_CBASER_VALID); 5064 5065 gits_write_cbaser(baser, its->base + GITS_CBASER); 5066 tmp = gits_read_cbaser(its->base + GITS_CBASER); 5067 5068 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) { 5069 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) { 5070 /* 5071 * The HW reports non-shareable, we must 5072 * remove the cacheability attributes as 5073 * well. 5074 */ 5075 baser &= ~(GITS_CBASER_SHAREABILITY_MASK | 5076 GITS_CBASER_CACHEABILITY_MASK); 5077 baser |= GITS_CBASER_nC; 5078 gits_write_cbaser(baser, its->base + GITS_CBASER); 5079 } 5080 pr_info("ITS: using cache flushing for cmd queue\n"); 5081 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING; 5082 } 5083 5084 gits_write_cwriter(0, its->base + GITS_CWRITER); 5085 ctlr = readl_relaxed(its->base + GITS_CTLR); 5086 ctlr |= GITS_CTLR_ENABLE; 5087 if (is_v4(its)) 5088 ctlr |= GITS_CTLR_ImDe; 5089 writel_relaxed(ctlr, its->base + GITS_CTLR); 5090 5091 err = its_init_domain(handle, its); 5092 if (err) 5093 goto out_free_tables; 5094 5095 raw_spin_lock(&its_lock); 5096 list_add(&its->entry, &its_nodes); 5097 raw_spin_unlock(&its_lock); 5098 5099 return 0; 5100 5101 out_free_tables: 5102 its_free_tables(its); 5103 out_free_cmd: 5104 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ)); 5105 out_unmap_sgir: 5106 if (its->sgir_base) 5107 iounmap(its->sgir_base); 5108 out_free_its: 5109 kfree(its); 5110 out_unmap: 5111 iounmap(its_base); 5112 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err); 5113 return err; 5114 } 5115 5116 static bool gic_rdists_supports_plpis(void) 5117 { 5118 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS); 5119 } 5120 5121 static int redist_disable_lpis(void) 5122 { 5123 void __iomem *rbase = gic_data_rdist_rd_base(); 5124 u64 timeout = USEC_PER_SEC; 5125 u64 val; 5126 5127 if (!gic_rdists_supports_plpis()) { 5128 pr_info("CPU%d: LPIs not supported\n", smp_processor_id()); 5129 return -ENXIO; 5130 } 5131 5132 val = readl_relaxed(rbase + GICR_CTLR); 5133 if (!(val & GICR_CTLR_ENABLE_LPIS)) 5134 return 0; 5135 5136 /* 5137 * If coming via a CPU hotplug event, we don't need to disable 5138 * LPIs before trying to re-enable them. They are already 5139 * configured and all is well in the world. 5140 * 5141 * If running with preallocated tables, there is nothing to do. 5142 */ 5143 if ((gic_data_rdist()->flags & RD_LOCAL_LPI_ENABLED) || 5144 (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED)) 5145 return 0; 5146 5147 /* 5148 * From that point on, we only try to do some damage control. 5149 */ 5150 pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n", 5151 smp_processor_id()); 5152 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK); 5153 5154 /* Disable LPIs */ 5155 val &= ~GICR_CTLR_ENABLE_LPIS; 5156 writel_relaxed(val, rbase + GICR_CTLR); 5157 5158 /* Make sure any change to GICR_CTLR is observable by the GIC */ 5159 dsb(sy); 5160 5161 /* 5162 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs 5163 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers. 5164 * Error out if we time out waiting for RWP to clear. 5165 */ 5166 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) { 5167 if (!timeout) { 5168 pr_err("CPU%d: Timeout while disabling LPIs\n", 5169 smp_processor_id()); 5170 return -ETIMEDOUT; 5171 } 5172 udelay(1); 5173 timeout--; 5174 } 5175 5176 /* 5177 * After it has been written to 1, it is IMPLEMENTATION 5178 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be 5179 * cleared to 0. Error out if clearing the bit failed. 5180 */ 5181 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) { 5182 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id()); 5183 return -EBUSY; 5184 } 5185 5186 return 0; 5187 } 5188 5189 int its_cpu_init(void) 5190 { 5191 if (!list_empty(&its_nodes)) { 5192 int ret; 5193 5194 ret = redist_disable_lpis(); 5195 if (ret) 5196 return ret; 5197 5198 its_cpu_init_lpis(); 5199 its_cpu_init_collections(); 5200 } 5201 5202 return 0; 5203 } 5204 5205 static const struct of_device_id its_device_id[] = { 5206 { .compatible = "arm,gic-v3-its", }, 5207 {}, 5208 }; 5209 5210 static int __init its_of_probe(struct device_node *node) 5211 { 5212 struct device_node *np; 5213 struct resource res; 5214 5215 for (np = of_find_matching_node(node, its_device_id); np; 5216 np = of_find_matching_node(np, its_device_id)) { 5217 if (!of_device_is_available(np)) 5218 continue; 5219 if (!of_property_read_bool(np, "msi-controller")) { 5220 pr_warn("%pOF: no msi-controller property, ITS ignored\n", 5221 np); 5222 continue; 5223 } 5224 5225 if (of_address_to_resource(np, 0, &res)) { 5226 pr_warn("%pOF: no regs?\n", np); 5227 continue; 5228 } 5229 5230 its_probe_one(&res, &np->fwnode, of_node_to_nid(np)); 5231 } 5232 return 0; 5233 } 5234 5235 #ifdef CONFIG_ACPI 5236 5237 #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K) 5238 5239 #ifdef CONFIG_ACPI_NUMA 5240 struct its_srat_map { 5241 /* numa node id */ 5242 u32 numa_node; 5243 /* GIC ITS ID */ 5244 u32 its_id; 5245 }; 5246 5247 static struct its_srat_map *its_srat_maps __initdata; 5248 static int its_in_srat __initdata; 5249 5250 static int __init acpi_get_its_numa_node(u32 its_id) 5251 { 5252 int i; 5253 5254 for (i = 0; i < its_in_srat; i++) { 5255 if (its_id == its_srat_maps[i].its_id) 5256 return its_srat_maps[i].numa_node; 5257 } 5258 return NUMA_NO_NODE; 5259 } 5260 5261 static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header, 5262 const unsigned long end) 5263 { 5264 return 0; 5265 } 5266 5267 static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header, 5268 const unsigned long end) 5269 { 5270 int node; 5271 struct acpi_srat_gic_its_affinity *its_affinity; 5272 5273 its_affinity = (struct acpi_srat_gic_its_affinity *)header; 5274 if (!its_affinity) 5275 return -EINVAL; 5276 5277 if (its_affinity->header.length < sizeof(*its_affinity)) { 5278 pr_err("SRAT: Invalid header length %d in ITS affinity\n", 5279 its_affinity->header.length); 5280 return -EINVAL; 5281 } 5282 5283 /* 5284 * Note that in theory a new proximity node could be created by this 5285 * entry as it is an SRAT resource allocation structure. 5286 * We do not currently support doing so. 5287 */ 5288 node = pxm_to_node(its_affinity->proximity_domain); 5289 5290 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) { 5291 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node); 5292 return 0; 5293 } 5294 5295 its_srat_maps[its_in_srat].numa_node = node; 5296 its_srat_maps[its_in_srat].its_id = its_affinity->its_id; 5297 its_in_srat++; 5298 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n", 5299 its_affinity->proximity_domain, its_affinity->its_id, node); 5300 5301 return 0; 5302 } 5303 5304 static void __init acpi_table_parse_srat_its(void) 5305 { 5306 int count; 5307 5308 count = acpi_table_parse_entries(ACPI_SIG_SRAT, 5309 sizeof(struct acpi_table_srat), 5310 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY, 5311 gic_acpi_match_srat_its, 0); 5312 if (count <= 0) 5313 return; 5314 5315 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map), 5316 GFP_KERNEL); 5317 if (!its_srat_maps) 5318 return; 5319 5320 acpi_table_parse_entries(ACPI_SIG_SRAT, 5321 sizeof(struct acpi_table_srat), 5322 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY, 5323 gic_acpi_parse_srat_its, 0); 5324 } 5325 5326 /* free the its_srat_maps after ITS probing */ 5327 static void __init acpi_its_srat_maps_free(void) 5328 { 5329 kfree(its_srat_maps); 5330 } 5331 #else 5332 static void __init acpi_table_parse_srat_its(void) { } 5333 static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; } 5334 static void __init acpi_its_srat_maps_free(void) { } 5335 #endif 5336 5337 static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header, 5338 const unsigned long end) 5339 { 5340 struct acpi_madt_generic_translator *its_entry; 5341 struct fwnode_handle *dom_handle; 5342 struct resource res; 5343 int err; 5344 5345 its_entry = (struct acpi_madt_generic_translator *)header; 5346 memset(&res, 0, sizeof(res)); 5347 res.start = its_entry->base_address; 5348 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1; 5349 res.flags = IORESOURCE_MEM; 5350 5351 dom_handle = irq_domain_alloc_fwnode(&res.start); 5352 if (!dom_handle) { 5353 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n", 5354 &res.start); 5355 return -ENOMEM; 5356 } 5357 5358 err = iort_register_domain_token(its_entry->translation_id, res.start, 5359 dom_handle); 5360 if (err) { 5361 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n", 5362 &res.start, its_entry->translation_id); 5363 goto dom_err; 5364 } 5365 5366 err = its_probe_one(&res, dom_handle, 5367 acpi_get_its_numa_node(its_entry->translation_id)); 5368 if (!err) 5369 return 0; 5370 5371 iort_deregister_domain_token(its_entry->translation_id); 5372 dom_err: 5373 irq_domain_free_fwnode(dom_handle); 5374 return err; 5375 } 5376 5377 static void __init its_acpi_probe(void) 5378 { 5379 acpi_table_parse_srat_its(); 5380 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR, 5381 gic_acpi_parse_madt_its, 0); 5382 acpi_its_srat_maps_free(); 5383 } 5384 #else 5385 static void __init its_acpi_probe(void) { } 5386 #endif 5387 5388 int __init its_init(struct fwnode_handle *handle, struct rdists *rdists, 5389 struct irq_domain *parent_domain) 5390 { 5391 struct device_node *of_node; 5392 struct its_node *its; 5393 bool has_v4 = false; 5394 bool has_v4_1 = false; 5395 int err; 5396 5397 gic_rdists = rdists; 5398 5399 its_parent = parent_domain; 5400 of_node = to_of_node(handle); 5401 if (of_node) 5402 its_of_probe(of_node); 5403 else 5404 its_acpi_probe(); 5405 5406 if (list_empty(&its_nodes)) { 5407 pr_warn("ITS: No ITS available, not enabling LPIs\n"); 5408 return -ENXIO; 5409 } 5410 5411 err = allocate_lpi_tables(); 5412 if (err) 5413 return err; 5414 5415 list_for_each_entry(its, &its_nodes, entry) { 5416 has_v4 |= is_v4(its); 5417 has_v4_1 |= is_v4_1(its); 5418 } 5419 5420 /* Don't bother with inconsistent systems */ 5421 if (WARN_ON(!has_v4_1 && rdists->has_rvpeid)) 5422 rdists->has_rvpeid = false; 5423 5424 if (has_v4 & rdists->has_vlpis) { 5425 const struct irq_domain_ops *sgi_ops; 5426 5427 if (has_v4_1) 5428 sgi_ops = &its_sgi_domain_ops; 5429 else 5430 sgi_ops = NULL; 5431 5432 if (its_init_vpe_domain() || 5433 its_init_v4(parent_domain, &its_vpe_domain_ops, sgi_ops)) { 5434 rdists->has_vlpis = false; 5435 pr_err("ITS: Disabling GICv4 support\n"); 5436 } 5437 } 5438 5439 register_syscore_ops(&its_syscore_ops); 5440 5441 return 0; 5442 } 5443