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