2 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <linux/acpi.h>
19 #include <linux/acpi_iort.h>
20 #include <linux/bitmap.h>
21 #include <linux/cpu.h>
22 #include <linux/delay.h>
23 #include <linux/dma-iommu.h>
24 #include <linux/interrupt.h>
25 #include <linux/irqdomain.h>
26 #include <linux/log2.h>
28 #include <linux/msi.h>
30 #include <linux/of_address.h>
31 #include <linux/of_irq.h>
32 #include <linux/of_pci.h>
33 #include <linux/of_platform.h>
34 #include <linux/percpu.h>
35 #include <linux/slab.h>
36 #include <linux/syscore_ops.h>
38 #include <linux/irqchip.h>
39 #include <linux/irqchip/arm-gic-v3.h>
40 #include <linux/irqchip/arm-gic-v4.h>
42 #include <asm/cputype.h>
43 #include <asm/exception.h>
45 #include "irq-gic-common.h"
47 #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
48 #define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
49 #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
50 #define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3)
52 #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
54 static u32 lpi_id_bits;
57 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
58 * deal with (one configuration byte per interrupt). PENDBASE has to
59 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
61 #define LPI_NRBITS lpi_id_bits
62 #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
63 #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
65 #define LPI_PROP_DEFAULT_PRIO 0xa0
68 * Collection structure - just an ID, and a redistributor address to
69 * ping. We use one per CPU as a bag of interrupts assigned to this
72 struct its_collection {
78 * The ITS_BASER structure - contains memory information, cached
79 * value of BASER register configuration and ITS page size.
91 * The ITS structure - contains most of the infrastructure, with the
92 * top-level MSI domain, the command queue, the collections, and the
93 * list of devices writing to it.
97 struct list_head entry;
99 phys_addr_t phys_base;
100 struct its_cmd_block *cmd_base;
101 struct its_cmd_block *cmd_write;
102 struct its_baser tables[GITS_BASER_NR_REGS];
103 struct its_collection *collections;
104 struct fwnode_handle *fwnode_handle;
105 u64 (*get_msi_base)(struct its_device *its_dev);
108 struct list_head its_device_list;
110 unsigned long list_nr;
114 unsigned int msi_domain_flags;
115 u32 pre_its_base; /* for Socionext Synquacer */
117 int vlpi_redist_offset;
120 #define ITS_ITT_ALIGN SZ_256
122 /* The maximum number of VPEID bits supported by VLPI commands */
123 #define ITS_MAX_VPEID_BITS (16)
124 #define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
126 /* Convert page order to size in bytes */
127 #define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
129 struct event_lpi_map {
130 unsigned long *lpi_map;
132 irq_hw_number_t lpi_base;
134 struct mutex vlpi_lock;
136 struct its_vlpi_map *vlpi_maps;
141 * The ITS view of a device - belongs to an ITS, owns an interrupt
142 * translation table, and a list of interrupts. If it some of its
143 * LPIs are injected into a guest (GICv4), the event_map.vm field
144 * indicates which one.
147 struct list_head entry;
148 struct its_node *its;
149 struct event_lpi_map event_map;
157 struct its_device *dev;
158 struct its_vpe **vpes;
162 static LIST_HEAD(its_nodes);
163 static DEFINE_SPINLOCK(its_lock);
164 static struct rdists *gic_rdists;
165 static struct irq_domain *its_parent;
167 static unsigned long its_list_map;
168 static u16 vmovp_seq_num;
169 static DEFINE_RAW_SPINLOCK(vmovp_lock);
171 static DEFINE_IDA(its_vpeid_ida);
173 #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
174 #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
175 #define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
177 static struct its_collection *dev_event_to_col(struct its_device *its_dev,
180 struct its_node *its = its_dev->its;
182 return its->collections + its_dev->event_map.col_map[event];
185 static struct its_collection *valid_col(struct its_collection *col)
187 if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(0, 15)))
193 static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
195 if (valid_col(its->collections + vpe->col_idx))
202 * ITS command descriptors - parameters to be encoded in a command
205 struct its_cmd_desc {
208 struct its_device *dev;
213 struct its_device *dev;
218 struct its_device *dev;
223 struct its_device *dev;
228 struct its_collection *col;
233 struct its_device *dev;
239 struct its_device *dev;
240 struct its_collection *col;
245 struct its_device *dev;
250 struct its_collection *col;
259 struct its_collection *col;
265 struct its_device *dev;
273 struct its_device *dev;
280 struct its_collection *col;
288 * The ITS command block, which is what the ITS actually parses.
290 struct its_cmd_block {
294 #define ITS_CMD_QUEUE_SZ SZ_64K
295 #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
297 typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
298 struct its_cmd_block *,
299 struct its_cmd_desc *);
301 typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
302 struct its_cmd_block *,
303 struct its_cmd_desc *);
305 static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
307 u64 mask = GENMASK_ULL(h, l);
309 *raw_cmd |= (val << l) & mask;
312 static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
314 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
317 static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
319 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
322 static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
324 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
327 static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
329 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
332 static void its_encode_size(struct its_cmd_block *cmd, u8 size)
334 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
337 static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
339 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
342 static void its_encode_valid(struct its_cmd_block *cmd, int valid)
344 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
347 static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
349 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
352 static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
354 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
357 static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
359 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
362 static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
364 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
367 static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
369 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
372 static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
374 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
377 static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
379 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
382 static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
384 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
387 static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
389 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
392 static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
394 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
397 static inline void its_fixup_cmd(struct its_cmd_block *cmd)
399 /* Let's fixup BE commands */
400 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
401 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
402 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
403 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
406 static struct its_collection *its_build_mapd_cmd(struct its_node *its,
407 struct its_cmd_block *cmd,
408 struct its_cmd_desc *desc)
410 unsigned long itt_addr;
411 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
413 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
414 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
416 its_encode_cmd(cmd, GITS_CMD_MAPD);
417 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
418 its_encode_size(cmd, size - 1);
419 its_encode_itt(cmd, itt_addr);
420 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
427 static struct its_collection *its_build_mapc_cmd(struct its_node *its,
428 struct its_cmd_block *cmd,
429 struct its_cmd_desc *desc)
431 its_encode_cmd(cmd, GITS_CMD_MAPC);
432 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
433 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
434 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
438 return desc->its_mapc_cmd.col;
441 static struct its_collection *its_build_mapti_cmd(struct its_node *its,
442 struct its_cmd_block *cmd,
443 struct its_cmd_desc *desc)
445 struct its_collection *col;
447 col = dev_event_to_col(desc->its_mapti_cmd.dev,
448 desc->its_mapti_cmd.event_id);
450 its_encode_cmd(cmd, GITS_CMD_MAPTI);
451 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
452 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
453 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
454 its_encode_collection(cmd, col->col_id);
458 return valid_col(col);
461 static struct its_collection *its_build_movi_cmd(struct its_node *its,
462 struct its_cmd_block *cmd,
463 struct its_cmd_desc *desc)
465 struct its_collection *col;
467 col = dev_event_to_col(desc->its_movi_cmd.dev,
468 desc->its_movi_cmd.event_id);
470 its_encode_cmd(cmd, GITS_CMD_MOVI);
471 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
472 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
473 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
477 return valid_col(col);
480 static struct its_collection *its_build_discard_cmd(struct its_node *its,
481 struct its_cmd_block *cmd,
482 struct its_cmd_desc *desc)
484 struct its_collection *col;
486 col = dev_event_to_col(desc->its_discard_cmd.dev,
487 desc->its_discard_cmd.event_id);
489 its_encode_cmd(cmd, GITS_CMD_DISCARD);
490 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
491 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
495 return valid_col(col);
498 static struct its_collection *its_build_inv_cmd(struct its_node *its,
499 struct its_cmd_block *cmd,
500 struct its_cmd_desc *desc)
502 struct its_collection *col;
504 col = dev_event_to_col(desc->its_inv_cmd.dev,
505 desc->its_inv_cmd.event_id);
507 its_encode_cmd(cmd, GITS_CMD_INV);
508 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
509 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
513 return valid_col(col);
516 static struct its_collection *its_build_int_cmd(struct its_node *its,
517 struct its_cmd_block *cmd,
518 struct its_cmd_desc *desc)
520 struct its_collection *col;
522 col = dev_event_to_col(desc->its_int_cmd.dev,
523 desc->its_int_cmd.event_id);
525 its_encode_cmd(cmd, GITS_CMD_INT);
526 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
527 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
531 return valid_col(col);
534 static struct its_collection *its_build_clear_cmd(struct its_node *its,
535 struct its_cmd_block *cmd,
536 struct its_cmd_desc *desc)
538 struct its_collection *col;
540 col = dev_event_to_col(desc->its_clear_cmd.dev,
541 desc->its_clear_cmd.event_id);
543 its_encode_cmd(cmd, GITS_CMD_CLEAR);
544 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
545 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
549 return valid_col(col);
552 static struct its_collection *its_build_invall_cmd(struct its_node *its,
553 struct its_cmd_block *cmd,
554 struct its_cmd_desc *desc)
556 its_encode_cmd(cmd, GITS_CMD_INVALL);
557 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
564 static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
565 struct its_cmd_block *cmd,
566 struct its_cmd_desc *desc)
568 its_encode_cmd(cmd, GITS_CMD_VINVALL);
569 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
573 return valid_vpe(its, desc->its_vinvall_cmd.vpe);
576 static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
577 struct its_cmd_block *cmd,
578 struct its_cmd_desc *desc)
580 unsigned long vpt_addr;
583 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
584 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
586 its_encode_cmd(cmd, GITS_CMD_VMAPP);
587 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
588 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
589 its_encode_target(cmd, target);
590 its_encode_vpt_addr(cmd, vpt_addr);
591 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
595 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
598 static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
599 struct its_cmd_block *cmd,
600 struct its_cmd_desc *desc)
604 if (desc->its_vmapti_cmd.db_enabled)
605 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
609 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
610 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
611 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
612 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
613 its_encode_db_phys_id(cmd, db);
614 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
618 return valid_vpe(its, desc->its_vmapti_cmd.vpe);
621 static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
622 struct its_cmd_block *cmd,
623 struct its_cmd_desc *desc)
627 if (desc->its_vmovi_cmd.db_enabled)
628 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
632 its_encode_cmd(cmd, GITS_CMD_VMOVI);
633 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
634 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
635 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
636 its_encode_db_phys_id(cmd, db);
637 its_encode_db_valid(cmd, true);
641 return valid_vpe(its, desc->its_vmovi_cmd.vpe);
644 static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
645 struct its_cmd_block *cmd,
646 struct its_cmd_desc *desc)
650 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
651 its_encode_cmd(cmd, GITS_CMD_VMOVP);
652 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
653 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
654 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
655 its_encode_target(cmd, target);
659 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
662 static u64 its_cmd_ptr_to_offset(struct its_node *its,
663 struct its_cmd_block *ptr)
665 return (ptr - its->cmd_base) * sizeof(*ptr);
668 static int its_queue_full(struct its_node *its)
673 widx = its->cmd_write - its->cmd_base;
674 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
676 /* This is incredibly unlikely to happen, unless the ITS locks up. */
677 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
683 static struct its_cmd_block *its_allocate_entry(struct its_node *its)
685 struct its_cmd_block *cmd;
686 u32 count = 1000000; /* 1s! */
688 while (its_queue_full(its)) {
691 pr_err_ratelimited("ITS queue not draining\n");
698 cmd = its->cmd_write++;
700 /* Handle queue wrapping */
701 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
702 its->cmd_write = its->cmd_base;
713 static struct its_cmd_block *its_post_commands(struct its_node *its)
715 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
717 writel_relaxed(wr, its->base + GITS_CWRITER);
719 return its->cmd_write;
722 static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
725 * Make sure the commands written to memory are observable by
728 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
729 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
734 static int its_wait_for_range_completion(struct its_node *its,
735 struct its_cmd_block *from,
736 struct its_cmd_block *to)
738 u64 rd_idx, from_idx, to_idx;
739 u32 count = 1000000; /* 1s! */
741 from_idx = its_cmd_ptr_to_offset(its, from);
742 to_idx = its_cmd_ptr_to_offset(its, to);
745 rd_idx = readl_relaxed(its->base + GITS_CREADR);
748 if (from_idx < to_idx && rd_idx >= to_idx)
752 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
757 pr_err_ratelimited("ITS queue timeout (%llu %llu %llu)\n",
758 from_idx, to_idx, rd_idx);
768 /* Warning, macro hell follows */
769 #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
770 void name(struct its_node *its, \
772 struct its_cmd_desc *desc) \
774 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
775 synctype *sync_obj; \
776 unsigned long flags; \
778 raw_spin_lock_irqsave(&its->lock, flags); \
780 cmd = its_allocate_entry(its); \
781 if (!cmd) { /* We're soooooo screewed... */ \
782 raw_spin_unlock_irqrestore(&its->lock, flags); \
785 sync_obj = builder(its, cmd, desc); \
786 its_flush_cmd(its, cmd); \
789 sync_cmd = its_allocate_entry(its); \
793 buildfn(its, sync_cmd, sync_obj); \
794 its_flush_cmd(its, sync_cmd); \
798 next_cmd = its_post_commands(its); \
799 raw_spin_unlock_irqrestore(&its->lock, flags); \
801 if (its_wait_for_range_completion(its, cmd, next_cmd)) \
802 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
805 static void its_build_sync_cmd(struct its_node *its,
806 struct its_cmd_block *sync_cmd,
807 struct its_collection *sync_col)
809 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
810 its_encode_target(sync_cmd, sync_col->target_address);
812 its_fixup_cmd(sync_cmd);
815 static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
816 struct its_collection, its_build_sync_cmd)
818 static void its_build_vsync_cmd(struct its_node *its,
819 struct its_cmd_block *sync_cmd,
820 struct its_vpe *sync_vpe)
822 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
823 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
825 its_fixup_cmd(sync_cmd);
828 static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
829 struct its_vpe, its_build_vsync_cmd)
831 static void its_send_int(struct its_device *dev, u32 event_id)
833 struct its_cmd_desc desc;
835 desc.its_int_cmd.dev = dev;
836 desc.its_int_cmd.event_id = event_id;
838 its_send_single_command(dev->its, its_build_int_cmd, &desc);
841 static void its_send_clear(struct its_device *dev, u32 event_id)
843 struct its_cmd_desc desc;
845 desc.its_clear_cmd.dev = dev;
846 desc.its_clear_cmd.event_id = event_id;
848 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
851 static void its_send_inv(struct its_device *dev, u32 event_id)
853 struct its_cmd_desc desc;
855 desc.its_inv_cmd.dev = dev;
856 desc.its_inv_cmd.event_id = event_id;
858 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
861 static void its_send_mapd(struct its_device *dev, int valid)
863 struct its_cmd_desc desc;
865 desc.its_mapd_cmd.dev = dev;
866 desc.its_mapd_cmd.valid = !!valid;
868 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
871 static void its_send_mapc(struct its_node *its, struct its_collection *col,
874 struct its_cmd_desc desc;
876 desc.its_mapc_cmd.col = col;
877 desc.its_mapc_cmd.valid = !!valid;
879 its_send_single_command(its, its_build_mapc_cmd, &desc);
882 static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
884 struct its_cmd_desc desc;
886 desc.its_mapti_cmd.dev = dev;
887 desc.its_mapti_cmd.phys_id = irq_id;
888 desc.its_mapti_cmd.event_id = id;
890 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
893 static void its_send_movi(struct its_device *dev,
894 struct its_collection *col, u32 id)
896 struct its_cmd_desc desc;
898 desc.its_movi_cmd.dev = dev;
899 desc.its_movi_cmd.col = col;
900 desc.its_movi_cmd.event_id = id;
902 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
905 static void its_send_discard(struct its_device *dev, u32 id)
907 struct its_cmd_desc desc;
909 desc.its_discard_cmd.dev = dev;
910 desc.its_discard_cmd.event_id = id;
912 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
915 static void its_send_invall(struct its_node *its, struct its_collection *col)
917 struct its_cmd_desc desc;
919 desc.its_invall_cmd.col = col;
921 its_send_single_command(its, its_build_invall_cmd, &desc);
924 static void its_send_vmapti(struct its_device *dev, u32 id)
926 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
927 struct its_cmd_desc desc;
929 desc.its_vmapti_cmd.vpe = map->vpe;
930 desc.its_vmapti_cmd.dev = dev;
931 desc.its_vmapti_cmd.virt_id = map->vintid;
932 desc.its_vmapti_cmd.event_id = id;
933 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
935 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
938 static void its_send_vmovi(struct its_device *dev, u32 id)
940 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
941 struct its_cmd_desc desc;
943 desc.its_vmovi_cmd.vpe = map->vpe;
944 desc.its_vmovi_cmd.dev = dev;
945 desc.its_vmovi_cmd.event_id = id;
946 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
948 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
951 static void its_send_vmapp(struct its_node *its,
952 struct its_vpe *vpe, bool valid)
954 struct its_cmd_desc desc;
956 desc.its_vmapp_cmd.vpe = vpe;
957 desc.its_vmapp_cmd.valid = valid;
958 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
960 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
963 static void its_send_vmovp(struct its_vpe *vpe)
965 struct its_cmd_desc desc;
966 struct its_node *its;
968 int col_id = vpe->col_idx;
970 desc.its_vmovp_cmd.vpe = vpe;
971 desc.its_vmovp_cmd.its_list = (u16)its_list_map;
974 its = list_first_entry(&its_nodes, struct its_node, entry);
975 desc.its_vmovp_cmd.seq_num = 0;
976 desc.its_vmovp_cmd.col = &its->collections[col_id];
977 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
982 * Yet another marvel of the architecture. If using the
983 * its_list "feature", we need to make sure that all ITSs
984 * receive all VMOVP commands in the same order. The only way
985 * to guarantee this is to make vmovp a serialization point.
989 raw_spin_lock_irqsave(&vmovp_lock, flags);
991 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
994 list_for_each_entry(its, &its_nodes, entry) {
998 if (!vpe->its_vm->vlpi_count[its->list_nr])
1001 desc.its_vmovp_cmd.col = &its->collections[col_id];
1002 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1005 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1008 static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
1010 struct its_cmd_desc desc;
1012 desc.its_vinvall_cmd.vpe = vpe;
1013 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
1017 * irqchip functions - assumes MSI, mostly.
1020 static inline u32 its_get_event_id(struct irq_data *d)
1022 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1023 return d->hwirq - its_dev->event_map.lpi_base;
1026 static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
1028 irq_hw_number_t hwirq;
1029 struct page *prop_page;
1032 if (irqd_is_forwarded_to_vcpu(d)) {
1033 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1034 u32 event = its_get_event_id(d);
1035 struct its_vlpi_map *map;
1037 prop_page = its_dev->event_map.vm->vprop_page;
1038 map = &its_dev->event_map.vlpi_maps[event];
1039 hwirq = map->vintid;
1041 /* Remember the updated property */
1042 map->properties &= ~clr;
1043 map->properties |= set | LPI_PROP_GROUP1;
1045 prop_page = gic_rdists->prop_page;
1049 cfg = page_address(prop_page) + hwirq - 8192;
1051 *cfg |= set | LPI_PROP_GROUP1;
1054 * Make the above write visible to the redistributors.
1055 * And yes, we're flushing exactly: One. Single. Byte.
1058 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
1059 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
1064 static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1066 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1068 lpi_write_config(d, clr, set);
1069 its_send_inv(its_dev, its_get_event_id(d));
1072 static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1074 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1075 u32 event = its_get_event_id(d);
1077 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1080 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1083 * More fun with the architecture:
1085 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1086 * value or to 1023, depending on the enable bit. But that
1087 * would be issueing a mapping for an /existing/ DevID+EventID
1088 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1089 * to the /same/ vPE, using this opportunity to adjust the
1090 * doorbell. Mouahahahaha. We loves it, Precious.
1092 its_send_vmovi(its_dev, event);
1095 static void its_mask_irq(struct irq_data *d)
1097 if (irqd_is_forwarded_to_vcpu(d))
1098 its_vlpi_set_doorbell(d, false);
1100 lpi_update_config(d, LPI_PROP_ENABLED, 0);
1103 static void its_unmask_irq(struct irq_data *d)
1105 if (irqd_is_forwarded_to_vcpu(d))
1106 its_vlpi_set_doorbell(d, true);
1108 lpi_update_config(d, 0, LPI_PROP_ENABLED);
1111 static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1115 const struct cpumask *cpu_mask = cpu_online_mask;
1116 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1117 struct its_collection *target_col;
1118 u32 id = its_get_event_id(d);
1120 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1121 if (irqd_is_forwarded_to_vcpu(d))
1124 /* lpi cannot be routed to a redistributor that is on a foreign node */
1125 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1126 if (its_dev->its->numa_node >= 0) {
1127 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1128 if (!cpumask_intersects(mask_val, cpu_mask))
1133 cpu = cpumask_any_and(mask_val, cpu_mask);
1135 if (cpu >= nr_cpu_ids)
1138 /* don't set the affinity when the target cpu is same as current one */
1139 if (cpu != its_dev->event_map.col_map[id]) {
1140 target_col = &its_dev->its->collections[cpu];
1141 its_send_movi(its_dev, target_col, id);
1142 its_dev->event_map.col_map[id] = cpu;
1143 irq_data_update_effective_affinity(d, cpumask_of(cpu));
1146 return IRQ_SET_MASK_OK_DONE;
1149 static u64 its_irq_get_msi_base(struct its_device *its_dev)
1151 struct its_node *its = its_dev->its;
1153 return its->phys_base + GITS_TRANSLATER;
1156 static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1158 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1159 struct its_node *its;
1163 addr = its->get_msi_base(its_dev);
1165 msg->address_lo = lower_32_bits(addr);
1166 msg->address_hi = upper_32_bits(addr);
1167 msg->data = its_get_event_id(d);
1169 iommu_dma_map_msi_msg(d->irq, msg);
1172 static int its_irq_set_irqchip_state(struct irq_data *d,
1173 enum irqchip_irq_state which,
1176 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1177 u32 event = its_get_event_id(d);
1179 if (which != IRQCHIP_STATE_PENDING)
1183 its_send_int(its_dev, event);
1185 its_send_clear(its_dev, event);
1190 static void its_map_vm(struct its_node *its, struct its_vm *vm)
1192 unsigned long flags;
1194 /* Not using the ITS list? Everything is always mapped. */
1198 raw_spin_lock_irqsave(&vmovp_lock, flags);
1201 * If the VM wasn't mapped yet, iterate over the vpes and get
1204 vm->vlpi_count[its->list_nr]++;
1206 if (vm->vlpi_count[its->list_nr] == 1) {
1209 for (i = 0; i < vm->nr_vpes; i++) {
1210 struct its_vpe *vpe = vm->vpes[i];
1211 struct irq_data *d = irq_get_irq_data(vpe->irq);
1213 /* Map the VPE to the first possible CPU */
1214 vpe->col_idx = cpumask_first(cpu_online_mask);
1215 its_send_vmapp(its, vpe, true);
1216 its_send_vinvall(its, vpe);
1217 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
1221 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1224 static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1226 unsigned long flags;
1228 /* Not using the ITS list? Everything is always mapped. */
1232 raw_spin_lock_irqsave(&vmovp_lock, flags);
1234 if (!--vm->vlpi_count[its->list_nr]) {
1237 for (i = 0; i < vm->nr_vpes; i++)
1238 its_send_vmapp(its, vm->vpes[i], false);
1241 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1244 static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1246 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1247 u32 event = its_get_event_id(d);
1253 mutex_lock(&its_dev->event_map.vlpi_lock);
1255 if (!its_dev->event_map.vm) {
1256 struct its_vlpi_map *maps;
1258 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
1265 its_dev->event_map.vm = info->map->vm;
1266 its_dev->event_map.vlpi_maps = maps;
1267 } else if (its_dev->event_map.vm != info->map->vm) {
1272 /* Get our private copy of the mapping information */
1273 its_dev->event_map.vlpi_maps[event] = *info->map;
1275 if (irqd_is_forwarded_to_vcpu(d)) {
1276 /* Already mapped, move it around */
1277 its_send_vmovi(its_dev, event);
1279 /* Ensure all the VPEs are mapped on this ITS */
1280 its_map_vm(its_dev->its, info->map->vm);
1283 * Flag the interrupt as forwarded so that we can
1284 * start poking the virtual property table.
1286 irqd_set_forwarded_to_vcpu(d);
1288 /* Write out the property to the prop table */
1289 lpi_write_config(d, 0xff, info->map->properties);
1291 /* Drop the physical mapping */
1292 its_send_discard(its_dev, event);
1294 /* and install the virtual one */
1295 its_send_vmapti(its_dev, event);
1297 /* Increment the number of VLPIs */
1298 its_dev->event_map.nr_vlpis++;
1302 mutex_unlock(&its_dev->event_map.vlpi_lock);
1306 static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1308 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1309 u32 event = its_get_event_id(d);
1312 mutex_lock(&its_dev->event_map.vlpi_lock);
1314 if (!its_dev->event_map.vm ||
1315 !its_dev->event_map.vlpi_maps[event].vm) {
1320 /* Copy our mapping information to the incoming request */
1321 *info->map = its_dev->event_map.vlpi_maps[event];
1324 mutex_unlock(&its_dev->event_map.vlpi_lock);
1328 static int its_vlpi_unmap(struct irq_data *d)
1330 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1331 u32 event = its_get_event_id(d);
1334 mutex_lock(&its_dev->event_map.vlpi_lock);
1336 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1341 /* Drop the virtual mapping */
1342 its_send_discard(its_dev, event);
1344 /* and restore the physical one */
1345 irqd_clr_forwarded_to_vcpu(d);
1346 its_send_mapti(its_dev, d->hwirq, event);
1347 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1351 /* Potentially unmap the VM from this ITS */
1352 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1355 * Drop the refcount and make the device available again if
1356 * this was the last VLPI.
1358 if (!--its_dev->event_map.nr_vlpis) {
1359 its_dev->event_map.vm = NULL;
1360 kfree(its_dev->event_map.vlpi_maps);
1364 mutex_unlock(&its_dev->event_map.vlpi_lock);
1368 static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1370 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1372 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1375 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1376 lpi_update_config(d, 0xff, info->config);
1378 lpi_write_config(d, 0xff, info->config);
1379 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1384 static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1386 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1387 struct its_cmd_info *info = vcpu_info;
1390 if (!its_dev->its->is_v4)
1393 /* Unmap request? */
1395 return its_vlpi_unmap(d);
1397 switch (info->cmd_type) {
1399 return its_vlpi_map(d, info);
1402 return its_vlpi_get(d, info);
1404 case PROP_UPDATE_VLPI:
1405 case PROP_UPDATE_AND_INV_VLPI:
1406 return its_vlpi_prop_update(d, info);
1413 static struct irq_chip its_irq_chip = {
1415 .irq_mask = its_mask_irq,
1416 .irq_unmask = its_unmask_irq,
1417 .irq_eoi = irq_chip_eoi_parent,
1418 .irq_set_affinity = its_set_affinity,
1419 .irq_compose_msi_msg = its_irq_compose_msi_msg,
1420 .irq_set_irqchip_state = its_irq_set_irqchip_state,
1421 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
1425 * How we allocate LPIs:
1427 * The GIC has id_bits bits for interrupt identifiers. From there, we
1428 * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as
1429 * we allocate LPIs by chunks of 32, we can shift the whole thing by 5
1430 * bits to the right.
1432 * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations.
1434 #define IRQS_PER_CHUNK_SHIFT 5
1435 #define IRQS_PER_CHUNK (1UL << IRQS_PER_CHUNK_SHIFT)
1436 #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
1438 static unsigned long *lpi_bitmap;
1439 static u32 lpi_chunks;
1440 static DEFINE_SPINLOCK(lpi_lock);
1442 static int its_lpi_to_chunk(int lpi)
1444 return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT;
1447 static int its_chunk_to_lpi(int chunk)
1449 return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192;
1452 static int __init its_lpi_init(u32 id_bits)
1454 lpi_chunks = its_lpi_to_chunk(1UL << id_bits);
1456 lpi_bitmap = kcalloc(BITS_TO_LONGS(lpi_chunks), sizeof(long),
1463 pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks);
1467 static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids)
1469 unsigned long *bitmap = NULL;
1474 nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK);
1476 spin_lock(&lpi_lock);
1479 chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks,
1481 if (chunk_id < lpi_chunks)
1485 } while (nr_chunks > 0);
1490 bitmap = kcalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK),
1496 for (i = 0; i < nr_chunks; i++)
1497 set_bit(chunk_id + i, lpi_bitmap);
1499 *base = its_chunk_to_lpi(chunk_id);
1500 *nr_ids = nr_chunks * IRQS_PER_CHUNK;
1503 spin_unlock(&lpi_lock);
1506 *base = *nr_ids = 0;
1511 static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids)
1515 spin_lock(&lpi_lock);
1517 for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) {
1518 int chunk = its_lpi_to_chunk(lpi);
1520 BUG_ON(chunk > lpi_chunks);
1521 if (test_bit(chunk, lpi_bitmap)) {
1522 clear_bit(chunk, lpi_bitmap);
1524 pr_err("Bad LPI chunk %d\n", chunk);
1528 spin_unlock(&lpi_lock);
1533 static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1535 struct page *prop_page;
1537 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1541 /* Priority 0xa0, Group-1, disabled */
1542 memset(page_address(prop_page),
1543 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
1546 /* Make sure the GIC will observe the written configuration */
1547 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
1552 static void its_free_prop_table(struct page *prop_page)
1554 free_pages((unsigned long)page_address(prop_page),
1555 get_order(LPI_PROPBASE_SZ));
1558 static int __init its_alloc_lpi_tables(void)
1562 lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS);
1563 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
1564 if (!gic_rdists->prop_page) {
1565 pr_err("Failed to allocate PROPBASE\n");
1569 paddr = page_to_phys(gic_rdists->prop_page);
1570 pr_info("GIC: using LPI property table @%pa\n", &paddr);
1572 return its_lpi_init(lpi_id_bits);
1575 static const char *its_base_type_string[] = {
1576 [GITS_BASER_TYPE_DEVICE] = "Devices",
1577 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
1578 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
1579 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1580 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1581 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1582 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1585 static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1587 u32 idx = baser - its->tables;
1589 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
1592 static void its_write_baser(struct its_node *its, struct its_baser *baser,
1595 u32 idx = baser - its->tables;
1597 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
1598 baser->val = its_read_baser(its, baser);
1601 static int its_setup_baser(struct its_node *its, struct its_baser *baser,
1602 u64 cache, u64 shr, u32 psz, u32 order,
1605 u64 val = its_read_baser(its, baser);
1606 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1607 u64 type = GITS_BASER_TYPE(val);
1608 u64 baser_phys, tmp;
1613 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1614 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1615 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1616 &its->phys_base, its_base_type_string[type],
1617 alloc_pages, GITS_BASER_PAGES_MAX);
1618 alloc_pages = GITS_BASER_PAGES_MAX;
1619 order = get_order(GITS_BASER_PAGES_MAX * psz);
1622 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1626 baser_phys = virt_to_phys(base);
1628 /* Check if the physical address of the memory is above 48bits */
1629 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
1631 /* 52bit PA is supported only when PageSize=64K */
1632 if (psz != SZ_64K) {
1633 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
1634 free_pages((unsigned long)base, order);
1638 /* Convert 52bit PA to 48bit field */
1639 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
1644 (type << GITS_BASER_TYPE_SHIFT) |
1645 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1646 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1651 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1655 val |= GITS_BASER_PAGE_SIZE_4K;
1658 val |= GITS_BASER_PAGE_SIZE_16K;
1661 val |= GITS_BASER_PAGE_SIZE_64K;
1665 its_write_baser(its, baser, val);
1668 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1670 * Shareability didn't stick. Just use
1671 * whatever the read reported, which is likely
1672 * to be the only thing this redistributor
1673 * supports. If that's zero, make it
1674 * non-cacheable as well.
1676 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1678 cache = GITS_BASER_nC;
1679 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
1684 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1686 * Page size didn't stick. Let's try a smaller
1687 * size and retry. If we reach 4K, then
1688 * something is horribly wrong...
1690 free_pages((unsigned long)base, order);
1696 goto retry_alloc_baser;
1699 goto retry_alloc_baser;
1704 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
1705 &its->phys_base, its_base_type_string[type],
1707 free_pages((unsigned long)base, order);
1711 baser->order = order;
1714 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
1716 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
1717 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
1718 its_base_type_string[type],
1719 (unsigned long)virt_to_phys(base),
1720 indirect ? "indirect" : "flat", (int)esz,
1721 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1726 static bool its_parse_indirect_baser(struct its_node *its,
1727 struct its_baser *baser,
1728 u32 psz, u32 *order, u32 ids)
1730 u64 tmp = its_read_baser(its, baser);
1731 u64 type = GITS_BASER_TYPE(tmp);
1732 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
1733 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
1734 u32 new_order = *order;
1735 bool indirect = false;
1737 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1738 if ((esz << ids) > (psz * 2)) {
1740 * Find out whether hw supports a single or two-level table by
1741 * table by reading bit at offset '62' after writing '1' to it.
1743 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1744 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1748 * The size of the lvl2 table is equal to ITS page size
1749 * which is 'psz'. For computing lvl1 table size,
1750 * subtract ID bits that sparse lvl2 table from 'ids'
1751 * which is reported by ITS hardware times lvl1 table
1754 ids -= ilog2(psz / (int)esz);
1755 esz = GITS_LVL1_ENTRY_SIZE;
1760 * Allocate as many entries as required to fit the
1761 * range of device IDs that the ITS can grok... The ID
1762 * space being incredibly sparse, this results in a
1763 * massive waste of memory if two-level device table
1764 * feature is not supported by hardware.
1766 new_order = max_t(u32, get_order(esz << ids), new_order);
1767 if (new_order >= MAX_ORDER) {
1768 new_order = MAX_ORDER - 1;
1769 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
1770 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1771 &its->phys_base, its_base_type_string[type],
1772 its->device_ids, ids);
1780 static void its_free_tables(struct its_node *its)
1784 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1785 if (its->tables[i].base) {
1786 free_pages((unsigned long)its->tables[i].base,
1787 its->tables[i].order);
1788 its->tables[i].base = NULL;
1793 static int its_alloc_tables(struct its_node *its)
1795 u64 shr = GITS_BASER_InnerShareable;
1796 u64 cache = GITS_BASER_RaWaWb;
1800 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
1801 /* erratum 24313: ignore memory access type */
1802 cache = GITS_BASER_nCnB;
1804 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1805 struct its_baser *baser = its->tables + i;
1806 u64 val = its_read_baser(its, baser);
1807 u64 type = GITS_BASER_TYPE(val);
1808 u32 order = get_order(psz);
1809 bool indirect = false;
1812 case GITS_BASER_TYPE_NONE:
1815 case GITS_BASER_TYPE_DEVICE:
1816 indirect = its_parse_indirect_baser(its, baser,
1819 case GITS_BASER_TYPE_VCPU:
1820 indirect = its_parse_indirect_baser(its, baser,
1822 ITS_MAX_VPEID_BITS);
1826 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
1828 its_free_tables(its);
1832 /* Update settings which will be used for next BASERn */
1834 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1835 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
1841 static int its_alloc_collections(struct its_node *its)
1845 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
1847 if (!its->collections)
1850 for (i = 0; i < nr_cpu_ids; i++)
1851 its->collections[i].target_address = ~0ULL;
1856 static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1858 struct page *pend_page;
1860 * The pending pages have to be at least 64kB aligned,
1861 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1863 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1864 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1868 /* Make sure the GIC will observe the zero-ed page */
1869 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1874 static void its_free_pending_table(struct page *pt)
1876 free_pages((unsigned long)page_address(pt),
1877 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1880 static void its_cpu_init_lpis(void)
1882 void __iomem *rbase = gic_data_rdist_rd_base();
1883 struct page *pend_page;
1886 /* If we didn't allocate the pending table yet, do it now */
1887 pend_page = gic_data_rdist()->pend_page;
1891 pend_page = its_allocate_pending_table(GFP_NOWAIT);
1893 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1894 smp_processor_id());
1898 paddr = page_to_phys(pend_page);
1899 pr_info("CPU%d: using LPI pending table @%pa\n",
1900 smp_processor_id(), &paddr);
1901 gic_data_rdist()->pend_page = pend_page;
1905 val = (page_to_phys(gic_rdists->prop_page) |
1906 GICR_PROPBASER_InnerShareable |
1907 GICR_PROPBASER_RaWaWb |
1908 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
1910 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1911 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
1913 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
1914 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
1916 * The HW reports non-shareable, we must
1917 * remove the cacheability attributes as
1920 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
1921 GICR_PROPBASER_CACHEABILITY_MASK);
1922 val |= GICR_PROPBASER_nC;
1923 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
1925 pr_info_once("GIC: using cache flushing for LPI property table\n");
1926 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
1930 val = (page_to_phys(pend_page) |
1931 GICR_PENDBASER_InnerShareable |
1932 GICR_PENDBASER_RaWaWb);
1934 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1935 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
1937 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
1939 * The HW reports non-shareable, we must remove the
1940 * cacheability attributes as well.
1942 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
1943 GICR_PENDBASER_CACHEABILITY_MASK);
1944 val |= GICR_PENDBASER_nC;
1945 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
1949 val = readl_relaxed(rbase + GICR_CTLR);
1950 val |= GICR_CTLR_ENABLE_LPIS;
1951 writel_relaxed(val, rbase + GICR_CTLR);
1953 /* Make sure the GIC has seen the above */
1957 static void its_cpu_init_collection(struct its_node *its)
1959 int cpu = smp_processor_id();
1962 /* avoid cross node collections and its mapping */
1963 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1964 struct device_node *cpu_node;
1966 cpu_node = of_get_cpu_node(cpu, NULL);
1967 if (its->numa_node != NUMA_NO_NODE &&
1968 its->numa_node != of_node_to_nid(cpu_node))
1973 * We now have to bind each collection to its target
1976 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
1978 * This ITS wants the physical address of the
1981 target = gic_data_rdist()->phys_base;
1983 /* This ITS wants a linear CPU number. */
1984 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
1985 target = GICR_TYPER_CPU_NUMBER(target) << 16;
1988 /* Perform collection mapping */
1989 its->collections[cpu].target_address = target;
1990 its->collections[cpu].col_id = cpu;
1992 its_send_mapc(its, &its->collections[cpu], 1);
1993 its_send_invall(its, &its->collections[cpu]);
1996 static void its_cpu_init_collections(void)
1998 struct its_node *its;
2000 spin_lock(&its_lock);
2002 list_for_each_entry(its, &its_nodes, entry)
2003 its_cpu_init_collection(its);
2005 spin_unlock(&its_lock);
2008 static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
2010 struct its_device *its_dev = NULL, *tmp;
2011 unsigned long flags;
2013 raw_spin_lock_irqsave(&its->lock, flags);
2015 list_for_each_entry(tmp, &its->its_device_list, entry) {
2016 if (tmp->device_id == dev_id) {
2022 raw_spin_unlock_irqrestore(&its->lock, flags);
2027 static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2031 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2032 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2033 return &its->tables[i];
2039 static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
2045 /* Don't allow device id that exceeds single, flat table limit */
2046 esz = GITS_BASER_ENTRY_SIZE(baser->val);
2047 if (!(baser->val & GITS_BASER_INDIRECT))
2048 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
2050 /* Compute 1st level table index & check if that exceeds table limit */
2051 idx = id >> ilog2(baser->psz / esz);
2052 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
2055 table = baser->base;
2057 /* Allocate memory for 2nd level table */
2059 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
2063 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2064 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
2065 gic_flush_dcache_to_poc(page_address(page), baser->psz);
2067 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2069 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2070 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
2071 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
2073 /* Ensure updated table contents are visible to ITS hardware */
2080 static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
2082 struct its_baser *baser;
2084 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
2086 /* Don't allow device id that exceeds ITS hardware limit */
2088 return (ilog2(dev_id) < its->device_ids);
2090 return its_alloc_table_entry(baser, dev_id);
2093 static bool its_alloc_vpe_table(u32 vpe_id)
2095 struct its_node *its;
2098 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
2099 * could try and only do it on ITSs corresponding to devices
2100 * that have interrupts targeted at this VPE, but the
2101 * complexity becomes crazy (and you have tons of memory
2104 list_for_each_entry(its, &its_nodes, entry) {
2105 struct its_baser *baser;
2110 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
2114 if (!its_alloc_table_entry(baser, vpe_id))
2121 static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
2122 int nvecs, bool alloc_lpis)
2124 struct its_device *dev;
2125 unsigned long *lpi_map = NULL;
2126 unsigned long flags;
2127 u16 *col_map = NULL;
2134 if (!its_alloc_device_table(its, dev_id))
2137 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2139 * We allocate at least one chunk worth of LPIs bet device,
2140 * and thus that many ITEs. The device may require less though.
2142 nr_ites = max(IRQS_PER_CHUNK, roundup_pow_of_two(nvecs));
2143 sz = nr_ites * its->ite_size;
2144 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
2145 itt = kzalloc(sz, GFP_KERNEL);
2147 lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis);
2149 col_map = kcalloc(nr_lpis, sizeof(*col_map),
2152 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
2157 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
2165 gic_flush_dcache_to_poc(itt, sz);
2169 dev->nr_ites = nr_ites;
2170 dev->event_map.lpi_map = lpi_map;
2171 dev->event_map.col_map = col_map;
2172 dev->event_map.lpi_base = lpi_base;
2173 dev->event_map.nr_lpis = nr_lpis;
2174 mutex_init(&dev->event_map.vlpi_lock);
2175 dev->device_id = dev_id;
2176 INIT_LIST_HEAD(&dev->entry);
2178 raw_spin_lock_irqsave(&its->lock, flags);
2179 list_add(&dev->entry, &its->its_device_list);
2180 raw_spin_unlock_irqrestore(&its->lock, flags);
2182 /* Map device to its ITT */
2183 its_send_mapd(dev, 1);
2188 static void its_free_device(struct its_device *its_dev)
2190 unsigned long flags;
2192 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
2193 list_del(&its_dev->entry);
2194 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
2195 kfree(its_dev->itt);
2199 static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
2203 idx = find_first_zero_bit(dev->event_map.lpi_map,
2204 dev->event_map.nr_lpis);
2205 if (idx == dev->event_map.nr_lpis)
2208 *hwirq = dev->event_map.lpi_base + idx;
2209 set_bit(idx, dev->event_map.lpi_map);
2214 static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2215 int nvec, msi_alloc_info_t *info)
2217 struct its_node *its;
2218 struct its_device *its_dev;
2219 struct msi_domain_info *msi_info;
2223 * We ignore "dev" entierely, and rely on the dev_id that has
2224 * been passed via the scratchpad. This limits this domain's
2225 * usefulness to upper layers that definitely know that they
2226 * are built on top of the ITS.
2228 dev_id = info->scratchpad[0].ul;
2230 msi_info = msi_get_domain_info(domain);
2231 its = msi_info->data;
2233 if (!gic_rdists->has_direct_lpi &&
2235 vpe_proxy.dev->its == its &&
2236 dev_id == vpe_proxy.dev->device_id) {
2237 /* Bad luck. Get yourself a better implementation */
2238 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
2243 its_dev = its_find_device(its, dev_id);
2246 * We already have seen this ID, probably through
2247 * another alias (PCI bridge of some sort). No need to
2248 * create the device.
2250 pr_debug("Reusing ITT for devID %x\n", dev_id);
2254 its_dev = its_create_device(its, dev_id, nvec, true);
2258 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
2260 info->scratchpad[0].ptr = its_dev;
2264 static struct msi_domain_ops its_msi_domain_ops = {
2265 .msi_prepare = its_msi_prepare,
2268 static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2270 irq_hw_number_t hwirq)
2272 struct irq_fwspec fwspec;
2274 if (irq_domain_get_of_node(domain->parent)) {
2275 fwspec.fwnode = domain->parent->fwnode;
2276 fwspec.param_count = 3;
2277 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2278 fwspec.param[1] = hwirq;
2279 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
2280 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2281 fwspec.fwnode = domain->parent->fwnode;
2282 fwspec.param_count = 2;
2283 fwspec.param[0] = hwirq;
2284 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
2289 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
2292 static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2293 unsigned int nr_irqs, void *args)
2295 msi_alloc_info_t *info = args;
2296 struct its_device *its_dev = info->scratchpad[0].ptr;
2297 irq_hw_number_t hwirq;
2301 for (i = 0; i < nr_irqs; i++) {
2302 err = its_alloc_device_irq(its_dev, &hwirq);
2306 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
2310 irq_domain_set_hwirq_and_chip(domain, virq + i,
2311 hwirq, &its_irq_chip, its_dev);
2312 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
2313 pr_debug("ID:%d pID:%d vID:%d\n",
2314 (int)(hwirq - its_dev->event_map.lpi_base),
2315 (int) hwirq, virq + i);
2321 static int its_irq_domain_activate(struct irq_domain *domain,
2322 struct irq_data *d, bool reserve)
2324 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2325 u32 event = its_get_event_id(d);
2326 const struct cpumask *cpu_mask = cpu_online_mask;
2329 /* get the cpu_mask of local node */
2330 if (its_dev->its->numa_node >= 0)
2331 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
2333 /* Bind the LPI to the first possible CPU */
2334 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
2335 if (cpu >= nr_cpu_ids) {
2336 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
2339 cpu = cpumask_first(cpu_online_mask);
2342 its_dev->event_map.col_map[event] = cpu;
2343 irq_data_update_effective_affinity(d, cpumask_of(cpu));
2345 /* Map the GIC IRQ and event to the device */
2346 its_send_mapti(its_dev, d->hwirq, event);
2350 static void its_irq_domain_deactivate(struct irq_domain *domain,
2353 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2354 u32 event = its_get_event_id(d);
2356 /* Stop the delivery of interrupts */
2357 its_send_discard(its_dev, event);
2360 static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2361 unsigned int nr_irqs)
2363 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2364 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2367 for (i = 0; i < nr_irqs; i++) {
2368 struct irq_data *data = irq_domain_get_irq_data(domain,
2370 u32 event = its_get_event_id(data);
2372 /* Mark interrupt index as unused */
2373 clear_bit(event, its_dev->event_map.lpi_map);
2375 /* Nuke the entry in the domain */
2376 irq_domain_reset_irq_data(data);
2379 /* If all interrupts have been freed, start mopping the floor */
2380 if (bitmap_empty(its_dev->event_map.lpi_map,
2381 its_dev->event_map.nr_lpis)) {
2382 its_lpi_free_chunks(its_dev->event_map.lpi_map,
2383 its_dev->event_map.lpi_base,
2384 its_dev->event_map.nr_lpis);
2385 kfree(its_dev->event_map.col_map);
2387 /* Unmap device/itt */
2388 its_send_mapd(its_dev, 0);
2389 its_free_device(its_dev);
2392 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2395 static const struct irq_domain_ops its_domain_ops = {
2396 .alloc = its_irq_domain_alloc,
2397 .free = its_irq_domain_free,
2398 .activate = its_irq_domain_activate,
2399 .deactivate = its_irq_domain_deactivate,
2405 * If a GICv4 doesn't implement Direct LPIs (which is extremely
2406 * likely), the only way to perform an invalidate is to use a fake
2407 * device to issue an INV command, implying that the LPI has first
2408 * been mapped to some event on that device. Since this is not exactly
2409 * cheap, we try to keep that mapping around as long as possible, and
2410 * only issue an UNMAP if we're short on available slots.
2412 * Broken by design(tm).
2414 static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
2416 /* Already unmapped? */
2417 if (vpe->vpe_proxy_event == -1)
2420 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
2421 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
2424 * We don't track empty slots at all, so let's move the
2425 * next_victim pointer if we can quickly reuse that slot
2426 * instead of nuking an existing entry. Not clear that this is
2427 * always a win though, and this might just generate a ripple
2428 * effect... Let's just hope VPEs don't migrate too often.
2430 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2431 vpe_proxy.next_victim = vpe->vpe_proxy_event;
2433 vpe->vpe_proxy_event = -1;
2436 static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
2438 if (!gic_rdists->has_direct_lpi) {
2439 unsigned long flags;
2441 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2442 its_vpe_db_proxy_unmap_locked(vpe);
2443 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2447 static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
2449 /* Already mapped? */
2450 if (vpe->vpe_proxy_event != -1)
2453 /* This slot was already allocated. Kick the other VPE out. */
2454 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2455 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
2457 /* Map the new VPE instead */
2458 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
2459 vpe->vpe_proxy_event = vpe_proxy.next_victim;
2460 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
2462 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
2463 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
2466 static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
2468 unsigned long flags;
2469 struct its_collection *target_col;
2471 if (gic_rdists->has_direct_lpi) {
2472 void __iomem *rdbase;
2474 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
2475 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2476 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2482 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2484 its_vpe_db_proxy_map_locked(vpe);
2486 target_col = &vpe_proxy.dev->its->collections[to];
2487 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
2488 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
2490 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2493 static int its_vpe_set_affinity(struct irq_data *d,
2494 const struct cpumask *mask_val,
2497 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2498 int cpu = cpumask_first(mask_val);
2501 * Changing affinity is mega expensive, so let's be as lazy as
2502 * we can and only do it if we really have to. Also, if mapped
2503 * into the proxy device, we need to move the doorbell
2504 * interrupt to its new location.
2506 if (vpe->col_idx != cpu) {
2507 int from = vpe->col_idx;
2510 its_send_vmovp(vpe);
2511 its_vpe_db_proxy_move(vpe, from, cpu);
2514 irq_data_update_effective_affinity(d, cpumask_of(cpu));
2516 return IRQ_SET_MASK_OK_DONE;
2519 static void its_vpe_schedule(struct its_vpe *vpe)
2521 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2524 /* Schedule the VPE */
2525 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2526 GENMASK_ULL(51, 12);
2527 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2528 val |= GICR_VPROPBASER_RaWb;
2529 val |= GICR_VPROPBASER_InnerShareable;
2530 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2532 val = virt_to_phys(page_address(vpe->vpt_page)) &
2533 GENMASK_ULL(51, 16);
2534 val |= GICR_VPENDBASER_RaWaWb;
2535 val |= GICR_VPENDBASER_NonShareable;
2537 * There is no good way of finding out if the pending table is
2538 * empty as we can race against the doorbell interrupt very
2539 * easily. So in the end, vpe->pending_last is only an
2540 * indication that the vcpu has something pending, not one
2541 * that the pending table is empty. A good implementation
2542 * would be able to read its coarse map pretty quickly anyway,
2543 * making this a tolerable issue.
2545 val |= GICR_VPENDBASER_PendingLast;
2546 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2547 val |= GICR_VPENDBASER_Valid;
2548 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2551 static void its_vpe_deschedule(struct its_vpe *vpe)
2553 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2554 u32 count = 1000000; /* 1s! */
2558 /* We're being scheduled out */
2559 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2560 val &= ~GICR_VPENDBASER_Valid;
2561 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2564 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2565 clean = !(val & GICR_VPENDBASER_Dirty);
2571 } while (!clean && count);
2573 if (unlikely(!clean && !count)) {
2574 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2576 vpe->pending_last = true;
2578 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2579 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2583 static void its_vpe_invall(struct its_vpe *vpe)
2585 struct its_node *its;
2587 list_for_each_entry(its, &its_nodes, entry) {
2591 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
2595 * Sending a VINVALL to a single ITS is enough, as all
2596 * we need is to reach the redistributors.
2598 its_send_vinvall(its, vpe);
2603 static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2605 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2606 struct its_cmd_info *info = vcpu_info;
2608 switch (info->cmd_type) {
2610 its_vpe_schedule(vpe);
2613 case DESCHEDULE_VPE:
2614 its_vpe_deschedule(vpe);
2618 its_vpe_invall(vpe);
2626 static void its_vpe_send_cmd(struct its_vpe *vpe,
2627 void (*cmd)(struct its_device *, u32))
2629 unsigned long flags;
2631 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2633 its_vpe_db_proxy_map_locked(vpe);
2634 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
2636 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2639 static void its_vpe_send_inv(struct irq_data *d)
2641 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2643 if (gic_rdists->has_direct_lpi) {
2644 void __iomem *rdbase;
2646 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2647 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
2648 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2651 its_vpe_send_cmd(vpe, its_send_inv);
2655 static void its_vpe_mask_irq(struct irq_data *d)
2658 * We need to unmask the LPI, which is described by the parent
2659 * irq_data. Instead of calling into the parent (which won't
2660 * exactly do the right thing, let's simply use the
2661 * parent_data pointer. Yes, I'm naughty.
2663 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
2664 its_vpe_send_inv(d);
2667 static void its_vpe_unmask_irq(struct irq_data *d)
2669 /* Same hack as above... */
2670 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
2671 its_vpe_send_inv(d);
2674 static int its_vpe_set_irqchip_state(struct irq_data *d,
2675 enum irqchip_irq_state which,
2678 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2680 if (which != IRQCHIP_STATE_PENDING)
2683 if (gic_rdists->has_direct_lpi) {
2684 void __iomem *rdbase;
2686 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2688 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
2690 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2691 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2696 its_vpe_send_cmd(vpe, its_send_int);
2698 its_vpe_send_cmd(vpe, its_send_clear);
2704 static struct irq_chip its_vpe_irq_chip = {
2705 .name = "GICv4-vpe",
2706 .irq_mask = its_vpe_mask_irq,
2707 .irq_unmask = its_vpe_unmask_irq,
2708 .irq_eoi = irq_chip_eoi_parent,
2709 .irq_set_affinity = its_vpe_set_affinity,
2710 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
2711 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
2714 static int its_vpe_id_alloc(void)
2716 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
2719 static void its_vpe_id_free(u16 id)
2721 ida_simple_remove(&its_vpeid_ida, id);
2724 static int its_vpe_init(struct its_vpe *vpe)
2726 struct page *vpt_page;
2729 /* Allocate vpe_id */
2730 vpe_id = its_vpe_id_alloc();
2735 vpt_page = its_allocate_pending_table(GFP_KERNEL);
2737 its_vpe_id_free(vpe_id);
2741 if (!its_alloc_vpe_table(vpe_id)) {
2742 its_vpe_id_free(vpe_id);
2743 its_free_pending_table(vpe->vpt_page);
2747 vpe->vpe_id = vpe_id;
2748 vpe->vpt_page = vpt_page;
2749 vpe->vpe_proxy_event = -1;
2754 static void its_vpe_teardown(struct its_vpe *vpe)
2756 its_vpe_db_proxy_unmap(vpe);
2757 its_vpe_id_free(vpe->vpe_id);
2758 its_free_pending_table(vpe->vpt_page);
2761 static void its_vpe_irq_domain_free(struct irq_domain *domain,
2763 unsigned int nr_irqs)
2765 struct its_vm *vm = domain->host_data;
2768 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2770 for (i = 0; i < nr_irqs; i++) {
2771 struct irq_data *data = irq_domain_get_irq_data(domain,
2773 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
2775 BUG_ON(vm != vpe->its_vm);
2777 clear_bit(data->hwirq, vm->db_bitmap);
2778 its_vpe_teardown(vpe);
2779 irq_domain_reset_irq_data(data);
2782 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
2783 its_lpi_free_chunks(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
2784 its_free_prop_table(vm->vprop_page);
2788 static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2789 unsigned int nr_irqs, void *args)
2791 struct its_vm *vm = args;
2792 unsigned long *bitmap;
2793 struct page *vprop_page;
2794 int base, nr_ids, i, err = 0;
2798 bitmap = its_lpi_alloc_chunks(nr_irqs, &base, &nr_ids);
2802 if (nr_ids < nr_irqs) {
2803 its_lpi_free_chunks(bitmap, base, nr_ids);
2807 vprop_page = its_allocate_prop_table(GFP_KERNEL);
2809 its_lpi_free_chunks(bitmap, base, nr_ids);
2813 vm->db_bitmap = bitmap;
2814 vm->db_lpi_base = base;
2815 vm->nr_db_lpis = nr_ids;
2816 vm->vprop_page = vprop_page;
2818 for (i = 0; i < nr_irqs; i++) {
2819 vm->vpes[i]->vpe_db_lpi = base + i;
2820 err = its_vpe_init(vm->vpes[i]);
2823 err = its_irq_gic_domain_alloc(domain, virq + i,
2824 vm->vpes[i]->vpe_db_lpi);
2827 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
2828 &its_vpe_irq_chip, vm->vpes[i]);
2834 its_vpe_irq_domain_free(domain, virq, i - 1);
2836 its_lpi_free_chunks(bitmap, base, nr_ids);
2837 its_free_prop_table(vprop_page);
2843 static int its_vpe_irq_domain_activate(struct irq_domain *domain,
2844 struct irq_data *d, bool reserve)
2846 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2847 struct its_node *its;
2849 /* If we use the list map, we issue VMAPP on demand... */
2853 /* Map the VPE to the first possible CPU */
2854 vpe->col_idx = cpumask_first(cpu_online_mask);
2856 list_for_each_entry(its, &its_nodes, entry) {
2860 its_send_vmapp(its, vpe, true);
2861 its_send_vinvall(its, vpe);
2864 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
2869 static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
2872 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2873 struct its_node *its;
2876 * If we use the list map, we unmap the VPE once no VLPIs are
2877 * associated with the VM.
2882 list_for_each_entry(its, &its_nodes, entry) {
2886 its_send_vmapp(its, vpe, false);
2890 static const struct irq_domain_ops its_vpe_domain_ops = {
2891 .alloc = its_vpe_irq_domain_alloc,
2892 .free = its_vpe_irq_domain_free,
2893 .activate = its_vpe_irq_domain_activate,
2894 .deactivate = its_vpe_irq_domain_deactivate,
2897 static int its_force_quiescent(void __iomem *base)
2899 u32 count = 1000000; /* 1s */
2902 val = readl_relaxed(base + GITS_CTLR);
2904 * GIC architecture specification requires the ITS to be both
2905 * disabled and quiescent for writes to GITS_BASER<n> or
2906 * GITS_CBASER to not have UNPREDICTABLE results.
2908 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
2911 /* Disable the generation of all interrupts to this ITS */
2912 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
2913 writel_relaxed(val, base + GITS_CTLR);
2915 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
2917 val = readl_relaxed(base + GITS_CTLR);
2918 if (val & GITS_CTLR_QUIESCENT)
2930 static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
2932 struct its_node *its = data;
2934 /* erratum 22375: only alloc 8MB table size */
2935 its->device_ids = 0x14; /* 20 bits, 8MB */
2936 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
2941 static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
2943 struct its_node *its = data;
2945 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
2950 static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
2952 struct its_node *its = data;
2954 /* On QDF2400, the size of the ITE is 16Bytes */
2960 static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
2962 struct its_node *its = its_dev->its;
2965 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
2966 * which maps 32-bit writes targeted at a separate window of
2967 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
2968 * with device ID taken from bits [device_id_bits + 1:2] of
2969 * the window offset.
2971 return its->pre_its_base + (its_dev->device_id << 2);
2974 static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
2976 struct its_node *its = data;
2977 u32 pre_its_window[2];
2980 if (!fwnode_property_read_u32_array(its->fwnode_handle,
2981 "socionext,synquacer-pre-its",
2983 ARRAY_SIZE(pre_its_window))) {
2985 its->pre_its_base = pre_its_window[0];
2986 its->get_msi_base = its_irq_get_msi_base_pre_its;
2988 ids = ilog2(pre_its_window[1]) - 2;
2989 if (its->device_ids > ids)
2990 its->device_ids = ids;
2992 /* the pre-ITS breaks isolation, so disable MSI remapping */
2993 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
2999 static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
3001 struct its_node *its = data;
3004 * Hip07 insists on using the wrong address for the VLPI
3005 * page. Trick it into doing the right thing...
3007 its->vlpi_redist_offset = SZ_128K;
3011 static const struct gic_quirk its_quirks[] = {
3012 #ifdef CONFIG_CAVIUM_ERRATUM_22375
3014 .desc = "ITS: Cavium errata 22375, 24313",
3015 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3017 .init = its_enable_quirk_cavium_22375,
3020 #ifdef CONFIG_CAVIUM_ERRATUM_23144
3022 .desc = "ITS: Cavium erratum 23144",
3023 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3025 .init = its_enable_quirk_cavium_23144,
3028 #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
3030 .desc = "ITS: QDF2400 erratum 0065",
3031 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
3033 .init = its_enable_quirk_qdf2400_e0065,
3036 #ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
3039 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
3040 * implementation, but with a 'pre-ITS' added that requires
3041 * special handling in software.
3043 .desc = "ITS: Socionext Synquacer pre-ITS",
3046 .init = its_enable_quirk_socionext_synquacer,
3049 #ifdef CONFIG_HISILICON_ERRATUM_161600802
3051 .desc = "ITS: Hip07 erratum 161600802",
3054 .init = its_enable_quirk_hip07_161600802,
3061 static void its_enable_quirks(struct its_node *its)
3063 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
3065 gic_enable_quirks(iidr, its_quirks, its);
3068 static int its_save_disable(void)
3070 struct its_node *its;
3073 spin_lock(&its_lock);
3074 list_for_each_entry(its, &its_nodes, entry) {
3077 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3081 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
3082 err = its_force_quiescent(base);
3084 pr_err("ITS@%pa: failed to quiesce: %d\n",
3085 &its->phys_base, err);
3086 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3090 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
3095 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
3098 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3102 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3105 spin_unlock(&its_lock);
3110 static void its_restore_enable(void)
3112 struct its_node *its;
3115 spin_lock(&its_lock);
3116 list_for_each_entry(its, &its_nodes, entry) {
3120 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3126 * Make sure that the ITS is disabled. If it fails to quiesce,
3127 * don't restore it since writing to CBASER or BASER<n>
3128 * registers is undefined according to the GIC v3 ITS
3131 ret = its_force_quiescent(base);
3133 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
3134 &its->phys_base, ret);
3138 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
3141 * Writing CBASER resets CREADR to 0, so make CWRITER and
3142 * cmd_write line up with it.
3144 its->cmd_write = its->cmd_base;
3145 gits_write_cwriter(0, base + GITS_CWRITER);
3147 /* Restore GITS_BASER from the value cache. */
3148 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
3149 struct its_baser *baser = &its->tables[i];
3151 if (!(baser->val & GITS_BASER_VALID))
3154 its_write_baser(its, baser, baser->val);
3156 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3159 * Reinit the collection if it's stored in the ITS. This is
3160 * indicated by the col_id being less than the HCC field.
3161 * CID < HCC as specified in the GIC v3 Documentation.
3163 if (its->collections[smp_processor_id()].col_id <
3164 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
3165 its_cpu_init_collection(its);
3167 spin_unlock(&its_lock);
3170 static struct syscore_ops its_syscore_ops = {
3171 .suspend = its_save_disable,
3172 .resume = its_restore_enable,
3175 static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
3177 struct irq_domain *inner_domain;
3178 struct msi_domain_info *info;
3180 info = kzalloc(sizeof(*info), GFP_KERNEL);
3184 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
3185 if (!inner_domain) {
3190 inner_domain->parent = its_parent;
3191 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
3192 inner_domain->flags |= its->msi_domain_flags;
3193 info->ops = &its_msi_domain_ops;
3195 inner_domain->host_data = info;
3200 static int its_init_vpe_domain(void)
3202 struct its_node *its;
3206 if (gic_rdists->has_direct_lpi) {
3207 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
3211 /* Any ITS will do, even if not v4 */
3212 its = list_first_entry(&its_nodes, struct its_node, entry);
3214 entries = roundup_pow_of_two(nr_cpu_ids);
3215 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
3217 if (!vpe_proxy.vpes) {
3218 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
3222 /* Use the last possible DevID */
3223 devid = GENMASK(its->device_ids - 1, 0);
3224 vpe_proxy.dev = its_create_device(its, devid, entries, false);
3225 if (!vpe_proxy.dev) {
3226 kfree(vpe_proxy.vpes);
3227 pr_err("ITS: Can't allocate GICv4 proxy device\n");
3231 BUG_ON(entries > vpe_proxy.dev->nr_ites);
3233 raw_spin_lock_init(&vpe_proxy.lock);
3234 vpe_proxy.next_victim = 0;
3235 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
3236 devid, vpe_proxy.dev->nr_ites);
3241 static int __init its_compute_its_list_map(struct resource *res,
3242 void __iomem *its_base)
3248 * This is assumed to be done early enough that we're
3249 * guaranteed to be single-threaded, hence no
3250 * locking. Should this change, we should address
3253 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
3254 if (its_number >= GICv4_ITS_LIST_MAX) {
3255 pr_err("ITS@%pa: No ITSList entry available!\n",
3260 ctlr = readl_relaxed(its_base + GITS_CTLR);
3261 ctlr &= ~GITS_CTLR_ITS_NUMBER;
3262 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
3263 writel_relaxed(ctlr, its_base + GITS_CTLR);
3264 ctlr = readl_relaxed(its_base + GITS_CTLR);
3265 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
3266 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
3267 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
3270 if (test_and_set_bit(its_number, &its_list_map)) {
3271 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
3272 &res->start, its_number);
3279 static int __init its_probe_one(struct resource *res,
3280 struct fwnode_handle *handle, int numa_node)
3282 struct its_node *its;
3283 void __iomem *its_base;
3285 u64 baser, tmp, typer;
3288 its_base = ioremap(res->start, resource_size(res));
3290 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
3294 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
3295 if (val != 0x30 && val != 0x40) {
3296 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
3301 err = its_force_quiescent(its_base);
3303 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
3307 pr_info("ITS %pR\n", res);
3309 its = kzalloc(sizeof(*its), GFP_KERNEL);
3315 raw_spin_lock_init(&its->lock);
3316 INIT_LIST_HEAD(&its->entry);
3317 INIT_LIST_HEAD(&its->its_device_list);
3318 typer = gic_read_typer(its_base + GITS_TYPER);
3319 its->base = its_base;
3320 its->phys_base = res->start;
3321 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
3322 its->device_ids = GITS_TYPER_DEVBITS(typer);
3323 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
3325 if (!(typer & GITS_TYPER_VMOVP)) {
3326 err = its_compute_its_list_map(res, its_base);
3332 pr_info("ITS@%pa: Using ITS number %d\n",
3335 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
3339 its->numa_node = numa_node;
3341 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
3342 get_order(ITS_CMD_QUEUE_SZ));
3343 if (!its->cmd_base) {
3347 its->cmd_write = its->cmd_base;
3348 its->fwnode_handle = handle;
3349 its->get_msi_base = its_irq_get_msi_base;
3350 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
3352 its_enable_quirks(its);
3354 err = its_alloc_tables(its);
3358 err = its_alloc_collections(its);
3360 goto out_free_tables;
3362 baser = (virt_to_phys(its->cmd_base) |
3363 GITS_CBASER_RaWaWb |
3364 GITS_CBASER_InnerShareable |