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