arm64: futex: Avoid copying out uninitialised stack in failed cmpxchg()
[linux/fpc-iii.git] / drivers / irqchip / irq-gic-v3-its.c
blob65ab2c80529ce58001cf7ddd2df791979dae7e43
1 /*
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/list.h>
27 #include <linux/list_sort.h>
28 #include <linux/log2.h>
29 #include <linux/mm.h>
30 #include <linux/msi.h>
31 #include <linux/of.h>
32 #include <linux/of_address.h>
33 #include <linux/of_irq.h>
34 #include <linux/of_pci.h>
35 #include <linux/of_platform.h>
36 #include <linux/percpu.h>
37 #include <linux/slab.h>
38 #include <linux/syscore_ops.h>
40 #include <linux/irqchip.h>
41 #include <linux/irqchip/arm-gic-v3.h>
42 #include <linux/irqchip/arm-gic-v4.h>
44 #include <asm/cputype.h>
45 #include <asm/exception.h>
47 #include "irq-gic-common.h"
49 #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
50 #define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
51 #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
52 #define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3)
54 #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
56 static u32 lpi_id_bits;
59 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
60 * deal with (one configuration byte per interrupt). PENDBASE has to
61 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
63 #define LPI_NRBITS lpi_id_bits
64 #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
65 #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
67 #define LPI_PROP_DEFAULT_PRIO 0xa0
70 * Collection structure - just an ID, and a redistributor address to
71 * ping. We use one per CPU as a bag of interrupts assigned to this
72 * CPU.
74 struct its_collection {
75 u64 target_address;
76 u16 col_id;
80 * The ITS_BASER structure - contains memory information, cached
81 * value of BASER register configuration and ITS page size.
83 struct its_baser {
84 void *base;
85 u64 val;
86 u32 order;
87 u32 psz;
90 struct its_device;
93 * The ITS structure - contains most of the infrastructure, with the
94 * top-level MSI domain, the command queue, the collections, and the
95 * list of devices writing to it.
97 * dev_alloc_lock has to be taken for device allocations, while the
98 * spinlock must be taken to parse data structures such as the device
99 * list.
101 struct its_node {
102 raw_spinlock_t lock;
103 struct mutex dev_alloc_lock;
104 struct list_head entry;
105 void __iomem *base;
106 phys_addr_t phys_base;
107 struct its_cmd_block *cmd_base;
108 struct its_cmd_block *cmd_write;
109 struct its_baser tables[GITS_BASER_NR_REGS];
110 struct its_collection *collections;
111 struct fwnode_handle *fwnode_handle;
112 u64 (*get_msi_base)(struct its_device *its_dev);
113 u64 cbaser_save;
114 u32 ctlr_save;
115 struct list_head its_device_list;
116 u64 flags;
117 unsigned long list_nr;
118 u32 ite_size;
119 u32 device_ids;
120 int numa_node;
121 unsigned int msi_domain_flags;
122 u32 pre_its_base; /* for Socionext Synquacer */
123 bool is_v4;
124 int vlpi_redist_offset;
127 #define ITS_ITT_ALIGN SZ_256
129 /* The maximum number of VPEID bits supported by VLPI commands */
130 #define ITS_MAX_VPEID_BITS (16)
131 #define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
133 /* Convert page order to size in bytes */
134 #define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
136 struct event_lpi_map {
137 unsigned long *lpi_map;
138 u16 *col_map;
139 irq_hw_number_t lpi_base;
140 int nr_lpis;
141 struct mutex vlpi_lock;
142 struct its_vm *vm;
143 struct its_vlpi_map *vlpi_maps;
144 int nr_vlpis;
148 * The ITS view of a device - belongs to an ITS, owns an interrupt
149 * translation table, and a list of interrupts. If it some of its
150 * LPIs are injected into a guest (GICv4), the event_map.vm field
151 * indicates which one.
153 struct its_device {
154 struct list_head entry;
155 struct its_node *its;
156 struct event_lpi_map event_map;
157 void *itt;
158 u32 nr_ites;
159 u32 device_id;
160 bool shared;
163 static struct {
164 raw_spinlock_t lock;
165 struct its_device *dev;
166 struct its_vpe **vpes;
167 int next_victim;
168 } vpe_proxy;
170 static LIST_HEAD(its_nodes);
171 static DEFINE_RAW_SPINLOCK(its_lock);
172 static struct rdists *gic_rdists;
173 static struct irq_domain *its_parent;
175 static unsigned long its_list_map;
176 static u16 vmovp_seq_num;
177 static DEFINE_RAW_SPINLOCK(vmovp_lock);
179 static DEFINE_IDA(its_vpeid_ida);
181 #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
182 #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
183 #define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
185 static struct its_collection *dev_event_to_col(struct its_device *its_dev,
186 u32 event)
188 struct its_node *its = its_dev->its;
190 return its->collections + its_dev->event_map.col_map[event];
193 static struct its_collection *valid_col(struct its_collection *col)
195 if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(0, 15)))
196 return NULL;
198 return col;
201 static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
203 if (valid_col(its->collections + vpe->col_idx))
204 return vpe;
206 return NULL;
210 * ITS command descriptors - parameters to be encoded in a command
211 * block.
213 struct its_cmd_desc {
214 union {
215 struct {
216 struct its_device *dev;
217 u32 event_id;
218 } its_inv_cmd;
220 struct {
221 struct its_device *dev;
222 u32 event_id;
223 } its_clear_cmd;
225 struct {
226 struct its_device *dev;
227 u32 event_id;
228 } its_int_cmd;
230 struct {
231 struct its_device *dev;
232 int valid;
233 } its_mapd_cmd;
235 struct {
236 struct its_collection *col;
237 int valid;
238 } its_mapc_cmd;
240 struct {
241 struct its_device *dev;
242 u32 phys_id;
243 u32 event_id;
244 } its_mapti_cmd;
246 struct {
247 struct its_device *dev;
248 struct its_collection *col;
249 u32 event_id;
250 } its_movi_cmd;
252 struct {
253 struct its_device *dev;
254 u32 event_id;
255 } its_discard_cmd;
257 struct {
258 struct its_collection *col;
259 } its_invall_cmd;
261 struct {
262 struct its_vpe *vpe;
263 } its_vinvall_cmd;
265 struct {
266 struct its_vpe *vpe;
267 struct its_collection *col;
268 bool valid;
269 } its_vmapp_cmd;
271 struct {
272 struct its_vpe *vpe;
273 struct its_device *dev;
274 u32 virt_id;
275 u32 event_id;
276 bool db_enabled;
277 } its_vmapti_cmd;
279 struct {
280 struct its_vpe *vpe;
281 struct its_device *dev;
282 u32 event_id;
283 bool db_enabled;
284 } its_vmovi_cmd;
286 struct {
287 struct its_vpe *vpe;
288 struct its_collection *col;
289 u16 seq_num;
290 u16 its_list;
291 } its_vmovp_cmd;
296 * The ITS command block, which is what the ITS actually parses.
298 struct its_cmd_block {
299 u64 raw_cmd[4];
302 #define ITS_CMD_QUEUE_SZ SZ_64K
303 #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
305 typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
306 struct its_cmd_block *,
307 struct its_cmd_desc *);
309 typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
310 struct its_cmd_block *,
311 struct its_cmd_desc *);
313 static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
315 u64 mask = GENMASK_ULL(h, l);
316 *raw_cmd &= ~mask;
317 *raw_cmd |= (val << l) & mask;
320 static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
322 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
325 static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
327 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
330 static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
332 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
335 static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
337 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
340 static void its_encode_size(struct its_cmd_block *cmd, u8 size)
342 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
345 static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
347 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
350 static void its_encode_valid(struct its_cmd_block *cmd, int valid)
352 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
355 static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
357 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
360 static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
362 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
365 static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
367 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
370 static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
372 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
375 static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
377 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
380 static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
382 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
385 static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
387 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
390 static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
392 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
395 static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
397 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
400 static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
402 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
405 static inline void its_fixup_cmd(struct its_cmd_block *cmd)
407 /* Let's fixup BE commands */
408 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
409 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
410 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
411 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
414 static struct its_collection *its_build_mapd_cmd(struct its_node *its,
415 struct its_cmd_block *cmd,
416 struct its_cmd_desc *desc)
418 unsigned long itt_addr;
419 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
421 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
422 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
424 its_encode_cmd(cmd, GITS_CMD_MAPD);
425 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
426 its_encode_size(cmd, size - 1);
427 its_encode_itt(cmd, itt_addr);
428 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
430 its_fixup_cmd(cmd);
432 return NULL;
435 static struct its_collection *its_build_mapc_cmd(struct its_node *its,
436 struct its_cmd_block *cmd,
437 struct its_cmd_desc *desc)
439 its_encode_cmd(cmd, GITS_CMD_MAPC);
440 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
441 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
442 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
444 its_fixup_cmd(cmd);
446 return desc->its_mapc_cmd.col;
449 static struct its_collection *its_build_mapti_cmd(struct its_node *its,
450 struct its_cmd_block *cmd,
451 struct its_cmd_desc *desc)
453 struct its_collection *col;
455 col = dev_event_to_col(desc->its_mapti_cmd.dev,
456 desc->its_mapti_cmd.event_id);
458 its_encode_cmd(cmd, GITS_CMD_MAPTI);
459 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
460 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
461 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
462 its_encode_collection(cmd, col->col_id);
464 its_fixup_cmd(cmd);
466 return valid_col(col);
469 static struct its_collection *its_build_movi_cmd(struct its_node *its,
470 struct its_cmd_block *cmd,
471 struct its_cmd_desc *desc)
473 struct its_collection *col;
475 col = dev_event_to_col(desc->its_movi_cmd.dev,
476 desc->its_movi_cmd.event_id);
478 its_encode_cmd(cmd, GITS_CMD_MOVI);
479 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
480 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
481 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
483 its_fixup_cmd(cmd);
485 return valid_col(col);
488 static struct its_collection *its_build_discard_cmd(struct its_node *its,
489 struct its_cmd_block *cmd,
490 struct its_cmd_desc *desc)
492 struct its_collection *col;
494 col = dev_event_to_col(desc->its_discard_cmd.dev,
495 desc->its_discard_cmd.event_id);
497 its_encode_cmd(cmd, GITS_CMD_DISCARD);
498 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
499 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
501 its_fixup_cmd(cmd);
503 return valid_col(col);
506 static struct its_collection *its_build_inv_cmd(struct its_node *its,
507 struct its_cmd_block *cmd,
508 struct its_cmd_desc *desc)
510 struct its_collection *col;
512 col = dev_event_to_col(desc->its_inv_cmd.dev,
513 desc->its_inv_cmd.event_id);
515 its_encode_cmd(cmd, GITS_CMD_INV);
516 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
517 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
519 its_fixup_cmd(cmd);
521 return valid_col(col);
524 static struct its_collection *its_build_int_cmd(struct its_node *its,
525 struct its_cmd_block *cmd,
526 struct its_cmd_desc *desc)
528 struct its_collection *col;
530 col = dev_event_to_col(desc->its_int_cmd.dev,
531 desc->its_int_cmd.event_id);
533 its_encode_cmd(cmd, GITS_CMD_INT);
534 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
535 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
537 its_fixup_cmd(cmd);
539 return valid_col(col);
542 static struct its_collection *its_build_clear_cmd(struct its_node *its,
543 struct its_cmd_block *cmd,
544 struct its_cmd_desc *desc)
546 struct its_collection *col;
548 col = dev_event_to_col(desc->its_clear_cmd.dev,
549 desc->its_clear_cmd.event_id);
551 its_encode_cmd(cmd, GITS_CMD_CLEAR);
552 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
553 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
555 its_fixup_cmd(cmd);
557 return valid_col(col);
560 static struct its_collection *its_build_invall_cmd(struct its_node *its,
561 struct its_cmd_block *cmd,
562 struct its_cmd_desc *desc)
564 its_encode_cmd(cmd, GITS_CMD_INVALL);
565 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
567 its_fixup_cmd(cmd);
569 return NULL;
572 static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
573 struct its_cmd_block *cmd,
574 struct its_cmd_desc *desc)
576 its_encode_cmd(cmd, GITS_CMD_VINVALL);
577 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
579 its_fixup_cmd(cmd);
581 return valid_vpe(its, desc->its_vinvall_cmd.vpe);
584 static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
585 struct its_cmd_block *cmd,
586 struct its_cmd_desc *desc)
588 unsigned long vpt_addr;
589 u64 target;
591 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
592 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
594 its_encode_cmd(cmd, GITS_CMD_VMAPP);
595 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
596 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
597 its_encode_target(cmd, target);
598 its_encode_vpt_addr(cmd, vpt_addr);
599 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
601 its_fixup_cmd(cmd);
603 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
606 static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
607 struct its_cmd_block *cmd,
608 struct its_cmd_desc *desc)
610 u32 db;
612 if (desc->its_vmapti_cmd.db_enabled)
613 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
614 else
615 db = 1023;
617 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
618 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
619 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
620 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
621 its_encode_db_phys_id(cmd, db);
622 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
624 its_fixup_cmd(cmd);
626 return valid_vpe(its, desc->its_vmapti_cmd.vpe);
629 static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
630 struct its_cmd_block *cmd,
631 struct its_cmd_desc *desc)
633 u32 db;
635 if (desc->its_vmovi_cmd.db_enabled)
636 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
637 else
638 db = 1023;
640 its_encode_cmd(cmd, GITS_CMD_VMOVI);
641 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
642 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
643 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
644 its_encode_db_phys_id(cmd, db);
645 its_encode_db_valid(cmd, true);
647 its_fixup_cmd(cmd);
649 return valid_vpe(its, desc->its_vmovi_cmd.vpe);
652 static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
653 struct its_cmd_block *cmd,
654 struct its_cmd_desc *desc)
656 u64 target;
658 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
659 its_encode_cmd(cmd, GITS_CMD_VMOVP);
660 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
661 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
662 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
663 its_encode_target(cmd, target);
665 its_fixup_cmd(cmd);
667 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
670 static u64 its_cmd_ptr_to_offset(struct its_node *its,
671 struct its_cmd_block *ptr)
673 return (ptr - its->cmd_base) * sizeof(*ptr);
676 static int its_queue_full(struct its_node *its)
678 int widx;
679 int ridx;
681 widx = its->cmd_write - its->cmd_base;
682 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
684 /* This is incredibly unlikely to happen, unless the ITS locks up. */
685 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
686 return 1;
688 return 0;
691 static struct its_cmd_block *its_allocate_entry(struct its_node *its)
693 struct its_cmd_block *cmd;
694 u32 count = 1000000; /* 1s! */
696 while (its_queue_full(its)) {
697 count--;
698 if (!count) {
699 pr_err_ratelimited("ITS queue not draining\n");
700 return NULL;
702 cpu_relax();
703 udelay(1);
706 cmd = its->cmd_write++;
708 /* Handle queue wrapping */
709 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
710 its->cmd_write = its->cmd_base;
712 /* Clear command */
713 cmd->raw_cmd[0] = 0;
714 cmd->raw_cmd[1] = 0;
715 cmd->raw_cmd[2] = 0;
716 cmd->raw_cmd[3] = 0;
718 return cmd;
721 static struct its_cmd_block *its_post_commands(struct its_node *its)
723 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
725 writel_relaxed(wr, its->base + GITS_CWRITER);
727 return its->cmd_write;
730 static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
733 * Make sure the commands written to memory are observable by
734 * the ITS.
736 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
737 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
738 else
739 dsb(ishst);
742 static int its_wait_for_range_completion(struct its_node *its,
743 struct its_cmd_block *from,
744 struct its_cmd_block *to)
746 u64 rd_idx, from_idx, to_idx;
747 u32 count = 1000000; /* 1s! */
749 from_idx = its_cmd_ptr_to_offset(its, from);
750 to_idx = its_cmd_ptr_to_offset(its, to);
752 while (1) {
753 rd_idx = readl_relaxed(its->base + GITS_CREADR);
755 /* Direct case */
756 if (from_idx < to_idx && rd_idx >= to_idx)
757 break;
759 /* Wrapped case */
760 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
761 break;
763 count--;
764 if (!count) {
765 pr_err_ratelimited("ITS queue timeout (%llu %llu %llu)\n",
766 from_idx, to_idx, rd_idx);
767 return -1;
769 cpu_relax();
770 udelay(1);
773 return 0;
776 /* Warning, macro hell follows */
777 #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
778 void name(struct its_node *its, \
779 buildtype builder, \
780 struct its_cmd_desc *desc) \
782 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
783 synctype *sync_obj; \
784 unsigned long flags; \
786 raw_spin_lock_irqsave(&its->lock, flags); \
788 cmd = its_allocate_entry(its); \
789 if (!cmd) { /* We're soooooo screewed... */ \
790 raw_spin_unlock_irqrestore(&its->lock, flags); \
791 return; \
793 sync_obj = builder(its, cmd, desc); \
794 its_flush_cmd(its, cmd); \
796 if (sync_obj) { \
797 sync_cmd = its_allocate_entry(its); \
798 if (!sync_cmd) \
799 goto post; \
801 buildfn(its, sync_cmd, sync_obj); \
802 its_flush_cmd(its, sync_cmd); \
805 post: \
806 next_cmd = its_post_commands(its); \
807 raw_spin_unlock_irqrestore(&its->lock, flags); \
809 if (its_wait_for_range_completion(its, cmd, next_cmd)) \
810 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
813 static void its_build_sync_cmd(struct its_node *its,
814 struct its_cmd_block *sync_cmd,
815 struct its_collection *sync_col)
817 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
818 its_encode_target(sync_cmd, sync_col->target_address);
820 its_fixup_cmd(sync_cmd);
823 static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
824 struct its_collection, its_build_sync_cmd)
826 static void its_build_vsync_cmd(struct its_node *its,
827 struct its_cmd_block *sync_cmd,
828 struct its_vpe *sync_vpe)
830 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
831 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
833 its_fixup_cmd(sync_cmd);
836 static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
837 struct its_vpe, its_build_vsync_cmd)
839 static void its_send_int(struct its_device *dev, u32 event_id)
841 struct its_cmd_desc desc;
843 desc.its_int_cmd.dev = dev;
844 desc.its_int_cmd.event_id = event_id;
846 its_send_single_command(dev->its, its_build_int_cmd, &desc);
849 static void its_send_clear(struct its_device *dev, u32 event_id)
851 struct its_cmd_desc desc;
853 desc.its_clear_cmd.dev = dev;
854 desc.its_clear_cmd.event_id = event_id;
856 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
859 static void its_send_inv(struct its_device *dev, u32 event_id)
861 struct its_cmd_desc desc;
863 desc.its_inv_cmd.dev = dev;
864 desc.its_inv_cmd.event_id = event_id;
866 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
869 static void its_send_mapd(struct its_device *dev, int valid)
871 struct its_cmd_desc desc;
873 desc.its_mapd_cmd.dev = dev;
874 desc.its_mapd_cmd.valid = !!valid;
876 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
879 static void its_send_mapc(struct its_node *its, struct its_collection *col,
880 int valid)
882 struct its_cmd_desc desc;
884 desc.its_mapc_cmd.col = col;
885 desc.its_mapc_cmd.valid = !!valid;
887 its_send_single_command(its, its_build_mapc_cmd, &desc);
890 static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
892 struct its_cmd_desc desc;
894 desc.its_mapti_cmd.dev = dev;
895 desc.its_mapti_cmd.phys_id = irq_id;
896 desc.its_mapti_cmd.event_id = id;
898 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
901 static void its_send_movi(struct its_device *dev,
902 struct its_collection *col, u32 id)
904 struct its_cmd_desc desc;
906 desc.its_movi_cmd.dev = dev;
907 desc.its_movi_cmd.col = col;
908 desc.its_movi_cmd.event_id = id;
910 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
913 static void its_send_discard(struct its_device *dev, u32 id)
915 struct its_cmd_desc desc;
917 desc.its_discard_cmd.dev = dev;
918 desc.its_discard_cmd.event_id = id;
920 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
923 static void its_send_invall(struct its_node *its, struct its_collection *col)
925 struct its_cmd_desc desc;
927 desc.its_invall_cmd.col = col;
929 its_send_single_command(its, its_build_invall_cmd, &desc);
932 static void its_send_vmapti(struct its_device *dev, u32 id)
934 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
935 struct its_cmd_desc desc;
937 desc.its_vmapti_cmd.vpe = map->vpe;
938 desc.its_vmapti_cmd.dev = dev;
939 desc.its_vmapti_cmd.virt_id = map->vintid;
940 desc.its_vmapti_cmd.event_id = id;
941 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
943 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
946 static void its_send_vmovi(struct its_device *dev, u32 id)
948 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
949 struct its_cmd_desc desc;
951 desc.its_vmovi_cmd.vpe = map->vpe;
952 desc.its_vmovi_cmd.dev = dev;
953 desc.its_vmovi_cmd.event_id = id;
954 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
956 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
959 static void its_send_vmapp(struct its_node *its,
960 struct its_vpe *vpe, bool valid)
962 struct its_cmd_desc desc;
964 desc.its_vmapp_cmd.vpe = vpe;
965 desc.its_vmapp_cmd.valid = valid;
966 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
968 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
971 static void its_send_vmovp(struct its_vpe *vpe)
973 struct its_cmd_desc desc;
974 struct its_node *its;
975 unsigned long flags;
976 int col_id = vpe->col_idx;
978 desc.its_vmovp_cmd.vpe = vpe;
979 desc.its_vmovp_cmd.its_list = (u16)its_list_map;
981 if (!its_list_map) {
982 its = list_first_entry(&its_nodes, struct its_node, entry);
983 desc.its_vmovp_cmd.seq_num = 0;
984 desc.its_vmovp_cmd.col = &its->collections[col_id];
985 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
986 return;
990 * Yet another marvel of the architecture. If using the
991 * its_list "feature", we need to make sure that all ITSs
992 * receive all VMOVP commands in the same order. The only way
993 * to guarantee this is to make vmovp a serialization point.
995 * Wall <-- Head.
997 raw_spin_lock_irqsave(&vmovp_lock, flags);
999 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
1001 /* Emit VMOVPs */
1002 list_for_each_entry(its, &its_nodes, entry) {
1003 if (!its->is_v4)
1004 continue;
1006 if (!vpe->its_vm->vlpi_count[its->list_nr])
1007 continue;
1009 desc.its_vmovp_cmd.col = &its->collections[col_id];
1010 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1013 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1016 static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
1018 struct its_cmd_desc desc;
1020 desc.its_vinvall_cmd.vpe = vpe;
1021 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
1025 * irqchip functions - assumes MSI, mostly.
1028 static inline u32 its_get_event_id(struct irq_data *d)
1030 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1031 return d->hwirq - its_dev->event_map.lpi_base;
1034 static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
1036 irq_hw_number_t hwirq;
1037 struct page *prop_page;
1038 u8 *cfg;
1040 if (irqd_is_forwarded_to_vcpu(d)) {
1041 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1042 u32 event = its_get_event_id(d);
1043 struct its_vlpi_map *map;
1045 prop_page = its_dev->event_map.vm->vprop_page;
1046 map = &its_dev->event_map.vlpi_maps[event];
1047 hwirq = map->vintid;
1049 /* Remember the updated property */
1050 map->properties &= ~clr;
1051 map->properties |= set | LPI_PROP_GROUP1;
1052 } else {
1053 prop_page = gic_rdists->prop_page;
1054 hwirq = d->hwirq;
1057 cfg = page_address(prop_page) + hwirq - 8192;
1058 *cfg &= ~clr;
1059 *cfg |= set | LPI_PROP_GROUP1;
1062 * Make the above write visible to the redistributors.
1063 * And yes, we're flushing exactly: One. Single. Byte.
1064 * Humpf...
1066 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
1067 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
1068 else
1069 dsb(ishst);
1072 static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1074 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1076 lpi_write_config(d, clr, set);
1077 its_send_inv(its_dev, its_get_event_id(d));
1080 static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1082 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1083 u32 event = its_get_event_id(d);
1085 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1086 return;
1088 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1091 * More fun with the architecture:
1093 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1094 * value or to 1023, depending on the enable bit. But that
1095 * would be issueing a mapping for an /existing/ DevID+EventID
1096 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1097 * to the /same/ vPE, using this opportunity to adjust the
1098 * doorbell. Mouahahahaha. We loves it, Precious.
1100 its_send_vmovi(its_dev, event);
1103 static void its_mask_irq(struct irq_data *d)
1105 if (irqd_is_forwarded_to_vcpu(d))
1106 its_vlpi_set_doorbell(d, false);
1108 lpi_update_config(d, LPI_PROP_ENABLED, 0);
1111 static void its_unmask_irq(struct irq_data *d)
1113 if (irqd_is_forwarded_to_vcpu(d))
1114 its_vlpi_set_doorbell(d, true);
1116 lpi_update_config(d, 0, LPI_PROP_ENABLED);
1119 static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1120 bool force)
1122 unsigned int cpu;
1123 const struct cpumask *cpu_mask = cpu_online_mask;
1124 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1125 struct its_collection *target_col;
1126 u32 id = its_get_event_id(d);
1128 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1129 if (irqd_is_forwarded_to_vcpu(d))
1130 return -EINVAL;
1132 /* lpi cannot be routed to a redistributor that is on a foreign node */
1133 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1134 if (its_dev->its->numa_node >= 0) {
1135 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1136 if (!cpumask_intersects(mask_val, cpu_mask))
1137 return -EINVAL;
1141 cpu = cpumask_any_and(mask_val, cpu_mask);
1143 if (cpu >= nr_cpu_ids)
1144 return -EINVAL;
1146 /* don't set the affinity when the target cpu is same as current one */
1147 if (cpu != its_dev->event_map.col_map[id]) {
1148 target_col = &its_dev->its->collections[cpu];
1149 its_send_movi(its_dev, target_col, id);
1150 its_dev->event_map.col_map[id] = cpu;
1151 irq_data_update_effective_affinity(d, cpumask_of(cpu));
1154 return IRQ_SET_MASK_OK_DONE;
1157 static u64 its_irq_get_msi_base(struct its_device *its_dev)
1159 struct its_node *its = its_dev->its;
1161 return its->phys_base + GITS_TRANSLATER;
1164 static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1166 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1167 struct its_node *its;
1168 u64 addr;
1170 its = its_dev->its;
1171 addr = its->get_msi_base(its_dev);
1173 msg->address_lo = lower_32_bits(addr);
1174 msg->address_hi = upper_32_bits(addr);
1175 msg->data = its_get_event_id(d);
1177 iommu_dma_map_msi_msg(d->irq, msg);
1180 static int its_irq_set_irqchip_state(struct irq_data *d,
1181 enum irqchip_irq_state which,
1182 bool state)
1184 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1185 u32 event = its_get_event_id(d);
1187 if (which != IRQCHIP_STATE_PENDING)
1188 return -EINVAL;
1190 if (state)
1191 its_send_int(its_dev, event);
1192 else
1193 its_send_clear(its_dev, event);
1195 return 0;
1198 static void its_map_vm(struct its_node *its, struct its_vm *vm)
1200 unsigned long flags;
1202 /* Not using the ITS list? Everything is always mapped. */
1203 if (!its_list_map)
1204 return;
1206 raw_spin_lock_irqsave(&vmovp_lock, flags);
1209 * If the VM wasn't mapped yet, iterate over the vpes and get
1210 * them mapped now.
1212 vm->vlpi_count[its->list_nr]++;
1214 if (vm->vlpi_count[its->list_nr] == 1) {
1215 int i;
1217 for (i = 0; i < vm->nr_vpes; i++) {
1218 struct its_vpe *vpe = vm->vpes[i];
1219 struct irq_data *d = irq_get_irq_data(vpe->irq);
1221 /* Map the VPE to the first possible CPU */
1222 vpe->col_idx = cpumask_first(cpu_online_mask);
1223 its_send_vmapp(its, vpe, true);
1224 its_send_vinvall(its, vpe);
1225 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
1229 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1232 static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1234 unsigned long flags;
1236 /* Not using the ITS list? Everything is always mapped. */
1237 if (!its_list_map)
1238 return;
1240 raw_spin_lock_irqsave(&vmovp_lock, flags);
1242 if (!--vm->vlpi_count[its->list_nr]) {
1243 int i;
1245 for (i = 0; i < vm->nr_vpes; i++)
1246 its_send_vmapp(its, vm->vpes[i], false);
1249 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1252 static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1254 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1255 u32 event = its_get_event_id(d);
1256 int ret = 0;
1258 if (!info->map)
1259 return -EINVAL;
1261 mutex_lock(&its_dev->event_map.vlpi_lock);
1263 if (!its_dev->event_map.vm) {
1264 struct its_vlpi_map *maps;
1266 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
1267 GFP_KERNEL);
1268 if (!maps) {
1269 ret = -ENOMEM;
1270 goto out;
1273 its_dev->event_map.vm = info->map->vm;
1274 its_dev->event_map.vlpi_maps = maps;
1275 } else if (its_dev->event_map.vm != info->map->vm) {
1276 ret = -EINVAL;
1277 goto out;
1280 /* Get our private copy of the mapping information */
1281 its_dev->event_map.vlpi_maps[event] = *info->map;
1283 if (irqd_is_forwarded_to_vcpu(d)) {
1284 /* Already mapped, move it around */
1285 its_send_vmovi(its_dev, event);
1286 } else {
1287 /* Ensure all the VPEs are mapped on this ITS */
1288 its_map_vm(its_dev->its, info->map->vm);
1291 * Flag the interrupt as forwarded so that we can
1292 * start poking the virtual property table.
1294 irqd_set_forwarded_to_vcpu(d);
1296 /* Write out the property to the prop table */
1297 lpi_write_config(d, 0xff, info->map->properties);
1299 /* Drop the physical mapping */
1300 its_send_discard(its_dev, event);
1302 /* and install the virtual one */
1303 its_send_vmapti(its_dev, event);
1305 /* Increment the number of VLPIs */
1306 its_dev->event_map.nr_vlpis++;
1309 out:
1310 mutex_unlock(&its_dev->event_map.vlpi_lock);
1311 return ret;
1314 static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1316 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1317 u32 event = its_get_event_id(d);
1318 int ret = 0;
1320 mutex_lock(&its_dev->event_map.vlpi_lock);
1322 if (!its_dev->event_map.vm ||
1323 !its_dev->event_map.vlpi_maps[event].vm) {
1324 ret = -EINVAL;
1325 goto out;
1328 /* Copy our mapping information to the incoming request */
1329 *info->map = its_dev->event_map.vlpi_maps[event];
1331 out:
1332 mutex_unlock(&its_dev->event_map.vlpi_lock);
1333 return ret;
1336 static int its_vlpi_unmap(struct irq_data *d)
1338 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1339 u32 event = its_get_event_id(d);
1340 int ret = 0;
1342 mutex_lock(&its_dev->event_map.vlpi_lock);
1344 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1345 ret = -EINVAL;
1346 goto out;
1349 /* Drop the virtual mapping */
1350 its_send_discard(its_dev, event);
1352 /* and restore the physical one */
1353 irqd_clr_forwarded_to_vcpu(d);
1354 its_send_mapti(its_dev, d->hwirq, event);
1355 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1356 LPI_PROP_ENABLED |
1357 LPI_PROP_GROUP1));
1359 /* Potentially unmap the VM from this ITS */
1360 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1363 * Drop the refcount and make the device available again if
1364 * this was the last VLPI.
1366 if (!--its_dev->event_map.nr_vlpis) {
1367 its_dev->event_map.vm = NULL;
1368 kfree(its_dev->event_map.vlpi_maps);
1371 out:
1372 mutex_unlock(&its_dev->event_map.vlpi_lock);
1373 return ret;
1376 static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1378 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1380 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1381 return -EINVAL;
1383 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1384 lpi_update_config(d, 0xff, info->config);
1385 else
1386 lpi_write_config(d, 0xff, info->config);
1387 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1389 return 0;
1392 static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1394 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1395 struct its_cmd_info *info = vcpu_info;
1397 /* Need a v4 ITS */
1398 if (!its_dev->its->is_v4)
1399 return -EINVAL;
1401 /* Unmap request? */
1402 if (!info)
1403 return its_vlpi_unmap(d);
1405 switch (info->cmd_type) {
1406 case MAP_VLPI:
1407 return its_vlpi_map(d, info);
1409 case GET_VLPI:
1410 return its_vlpi_get(d, info);
1412 case PROP_UPDATE_VLPI:
1413 case PROP_UPDATE_AND_INV_VLPI:
1414 return its_vlpi_prop_update(d, info);
1416 default:
1417 return -EINVAL;
1421 static struct irq_chip its_irq_chip = {
1422 .name = "ITS",
1423 .irq_mask = its_mask_irq,
1424 .irq_unmask = its_unmask_irq,
1425 .irq_eoi = irq_chip_eoi_parent,
1426 .irq_set_affinity = its_set_affinity,
1427 .irq_compose_msi_msg = its_irq_compose_msi_msg,
1428 .irq_set_irqchip_state = its_irq_set_irqchip_state,
1429 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
1434 * How we allocate LPIs:
1436 * lpi_range_list contains ranges of LPIs that are to available to
1437 * allocate from. To allocate LPIs, just pick the first range that
1438 * fits the required allocation, and reduce it by the required
1439 * amount. Once empty, remove the range from the list.
1441 * To free a range of LPIs, add a free range to the list, sort it and
1442 * merge the result if the new range happens to be adjacent to an
1443 * already free block.
1445 * The consequence of the above is that allocation is cost is low, but
1446 * freeing is expensive. We assumes that freeing rarely occurs.
1448 #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
1450 static DEFINE_MUTEX(lpi_range_lock);
1451 static LIST_HEAD(lpi_range_list);
1453 struct lpi_range {
1454 struct list_head entry;
1455 u32 base_id;
1456 u32 span;
1459 static struct lpi_range *mk_lpi_range(u32 base, u32 span)
1461 struct lpi_range *range;
1463 range = kzalloc(sizeof(*range), GFP_KERNEL);
1464 if (range) {
1465 INIT_LIST_HEAD(&range->entry);
1466 range->base_id = base;
1467 range->span = span;
1470 return range;
1473 static int lpi_range_cmp(void *priv, struct list_head *a, struct list_head *b)
1475 struct lpi_range *ra, *rb;
1477 ra = container_of(a, struct lpi_range, entry);
1478 rb = container_of(b, struct lpi_range, entry);
1480 return ra->base_id - rb->base_id;
1483 static void merge_lpi_ranges(void)
1485 struct lpi_range *range, *tmp;
1487 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1488 if (!list_is_last(&range->entry, &lpi_range_list) &&
1489 (tmp->base_id == (range->base_id + range->span))) {
1490 tmp->base_id = range->base_id;
1491 tmp->span += range->span;
1492 list_del(&range->entry);
1493 kfree(range);
1498 static int alloc_lpi_range(u32 nr_lpis, u32 *base)
1500 struct lpi_range *range, *tmp;
1501 int err = -ENOSPC;
1503 mutex_lock(&lpi_range_lock);
1505 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1506 if (range->span >= nr_lpis) {
1507 *base = range->base_id;
1508 range->base_id += nr_lpis;
1509 range->span -= nr_lpis;
1511 if (range->span == 0) {
1512 list_del(&range->entry);
1513 kfree(range);
1516 err = 0;
1517 break;
1521 mutex_unlock(&lpi_range_lock);
1523 pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
1524 return err;
1527 static int free_lpi_range(u32 base, u32 nr_lpis)
1529 struct lpi_range *new;
1530 int err = 0;
1532 mutex_lock(&lpi_range_lock);
1534 new = mk_lpi_range(base, nr_lpis);
1535 if (!new) {
1536 err = -ENOMEM;
1537 goto out;
1540 list_add(&new->entry, &lpi_range_list);
1541 list_sort(NULL, &lpi_range_list, lpi_range_cmp);
1542 merge_lpi_ranges();
1543 out:
1544 mutex_unlock(&lpi_range_lock);
1545 return err;
1548 static int __init its_lpi_init(u32 id_bits)
1550 u32 lpis = (1UL << id_bits) - 8192;
1551 u32 numlpis;
1552 int err;
1554 numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
1556 if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
1557 lpis = numlpis;
1558 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
1559 lpis);
1563 * Initializing the allocator is just the same as freeing the
1564 * full range of LPIs.
1566 err = free_lpi_range(8192, lpis);
1567 pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
1568 return err;
1571 static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
1573 unsigned long *bitmap = NULL;
1574 int err = 0;
1576 do {
1577 err = alloc_lpi_range(nr_irqs, base);
1578 if (!err)
1579 break;
1581 nr_irqs /= 2;
1582 } while (nr_irqs > 0);
1584 if (!nr_irqs)
1585 err = -ENOSPC;
1587 if (err)
1588 goto out;
1590 bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC);
1591 if (!bitmap)
1592 goto out;
1594 *nr_ids = nr_irqs;
1596 out:
1597 if (!bitmap)
1598 *base = *nr_ids = 0;
1600 return bitmap;
1603 static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
1605 WARN_ON(free_lpi_range(base, nr_ids));
1606 kfree(bitmap);
1609 static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1611 struct page *prop_page;
1613 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1614 if (!prop_page)
1615 return NULL;
1617 /* Priority 0xa0, Group-1, disabled */
1618 memset(page_address(prop_page),
1619 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
1620 LPI_PROPBASE_SZ);
1622 /* Make sure the GIC will observe the written configuration */
1623 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
1625 return prop_page;
1628 static void its_free_prop_table(struct page *prop_page)
1630 free_pages((unsigned long)page_address(prop_page),
1631 get_order(LPI_PROPBASE_SZ));
1634 static int __init its_alloc_lpi_tables(void)
1636 phys_addr_t paddr;
1638 lpi_id_bits = min_t(u32, GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
1639 ITS_MAX_LPI_NRBITS);
1640 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
1641 if (!gic_rdists->prop_page) {
1642 pr_err("Failed to allocate PROPBASE\n");
1643 return -ENOMEM;
1646 paddr = page_to_phys(gic_rdists->prop_page);
1647 pr_info("GIC: using LPI property table @%pa\n", &paddr);
1649 return its_lpi_init(lpi_id_bits);
1652 static const char *its_base_type_string[] = {
1653 [GITS_BASER_TYPE_DEVICE] = "Devices",
1654 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
1655 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
1656 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1657 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1658 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1659 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1662 static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1664 u32 idx = baser - its->tables;
1666 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
1669 static void its_write_baser(struct its_node *its, struct its_baser *baser,
1670 u64 val)
1672 u32 idx = baser - its->tables;
1674 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
1675 baser->val = its_read_baser(its, baser);
1678 static int its_setup_baser(struct its_node *its, struct its_baser *baser,
1679 u64 cache, u64 shr, u32 psz, u32 order,
1680 bool indirect)
1682 u64 val = its_read_baser(its, baser);
1683 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1684 u64 type = GITS_BASER_TYPE(val);
1685 u64 baser_phys, tmp;
1686 u32 alloc_pages;
1687 void *base;
1689 retry_alloc_baser:
1690 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1691 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1692 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1693 &its->phys_base, its_base_type_string[type],
1694 alloc_pages, GITS_BASER_PAGES_MAX);
1695 alloc_pages = GITS_BASER_PAGES_MAX;
1696 order = get_order(GITS_BASER_PAGES_MAX * psz);
1699 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1700 if (!base)
1701 return -ENOMEM;
1703 baser_phys = virt_to_phys(base);
1705 /* Check if the physical address of the memory is above 48bits */
1706 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
1708 /* 52bit PA is supported only when PageSize=64K */
1709 if (psz != SZ_64K) {
1710 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
1711 free_pages((unsigned long)base, order);
1712 return -ENXIO;
1715 /* Convert 52bit PA to 48bit field */
1716 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
1719 retry_baser:
1720 val = (baser_phys |
1721 (type << GITS_BASER_TYPE_SHIFT) |
1722 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1723 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1724 cache |
1725 shr |
1726 GITS_BASER_VALID);
1728 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1730 switch (psz) {
1731 case SZ_4K:
1732 val |= GITS_BASER_PAGE_SIZE_4K;
1733 break;
1734 case SZ_16K:
1735 val |= GITS_BASER_PAGE_SIZE_16K;
1736 break;
1737 case SZ_64K:
1738 val |= GITS_BASER_PAGE_SIZE_64K;
1739 break;
1742 its_write_baser(its, baser, val);
1743 tmp = baser->val;
1745 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1747 * Shareability didn't stick. Just use
1748 * whatever the read reported, which is likely
1749 * to be the only thing this redistributor
1750 * supports. If that's zero, make it
1751 * non-cacheable as well.
1753 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1754 if (!shr) {
1755 cache = GITS_BASER_nC;
1756 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
1758 goto retry_baser;
1761 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1763 * Page size didn't stick. Let's try a smaller
1764 * size and retry. If we reach 4K, then
1765 * something is horribly wrong...
1767 free_pages((unsigned long)base, order);
1768 baser->base = NULL;
1770 switch (psz) {
1771 case SZ_16K:
1772 psz = SZ_4K;
1773 goto retry_alloc_baser;
1774 case SZ_64K:
1775 psz = SZ_16K;
1776 goto retry_alloc_baser;
1780 if (val != tmp) {
1781 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
1782 &its->phys_base, its_base_type_string[type],
1783 val, tmp);
1784 free_pages((unsigned long)base, order);
1785 return -ENXIO;
1788 baser->order = order;
1789 baser->base = base;
1790 baser->psz = psz;
1791 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
1793 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
1794 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
1795 its_base_type_string[type],
1796 (unsigned long)virt_to_phys(base),
1797 indirect ? "indirect" : "flat", (int)esz,
1798 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1800 return 0;
1803 static bool its_parse_indirect_baser(struct its_node *its,
1804 struct its_baser *baser,
1805 u32 psz, u32 *order, u32 ids)
1807 u64 tmp = its_read_baser(its, baser);
1808 u64 type = GITS_BASER_TYPE(tmp);
1809 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
1810 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
1811 u32 new_order = *order;
1812 bool indirect = false;
1814 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1815 if ((esz << ids) > (psz * 2)) {
1817 * Find out whether hw supports a single or two-level table by
1818 * table by reading bit at offset '62' after writing '1' to it.
1820 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1821 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1823 if (indirect) {
1825 * The size of the lvl2 table is equal to ITS page size
1826 * which is 'psz'. For computing lvl1 table size,
1827 * subtract ID bits that sparse lvl2 table from 'ids'
1828 * which is reported by ITS hardware times lvl1 table
1829 * entry size.
1831 ids -= ilog2(psz / (int)esz);
1832 esz = GITS_LVL1_ENTRY_SIZE;
1837 * Allocate as many entries as required to fit the
1838 * range of device IDs that the ITS can grok... The ID
1839 * space being incredibly sparse, this results in a
1840 * massive waste of memory if two-level device table
1841 * feature is not supported by hardware.
1843 new_order = max_t(u32, get_order(esz << ids), new_order);
1844 if (new_order >= MAX_ORDER) {
1845 new_order = MAX_ORDER - 1;
1846 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
1847 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1848 &its->phys_base, its_base_type_string[type],
1849 its->device_ids, ids);
1852 *order = new_order;
1854 return indirect;
1857 static void its_free_tables(struct its_node *its)
1859 int i;
1861 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1862 if (its->tables[i].base) {
1863 free_pages((unsigned long)its->tables[i].base,
1864 its->tables[i].order);
1865 its->tables[i].base = NULL;
1870 static int its_alloc_tables(struct its_node *its)
1872 u64 shr = GITS_BASER_InnerShareable;
1873 u64 cache = GITS_BASER_RaWaWb;
1874 u32 psz = SZ_64K;
1875 int err, i;
1877 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
1878 /* erratum 24313: ignore memory access type */
1879 cache = GITS_BASER_nCnB;
1881 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1882 struct its_baser *baser = its->tables + i;
1883 u64 val = its_read_baser(its, baser);
1884 u64 type = GITS_BASER_TYPE(val);
1885 u32 order = get_order(psz);
1886 bool indirect = false;
1888 switch (type) {
1889 case GITS_BASER_TYPE_NONE:
1890 continue;
1892 case GITS_BASER_TYPE_DEVICE:
1893 indirect = its_parse_indirect_baser(its, baser,
1894 psz, &order,
1895 its->device_ids);
1896 break;
1898 case GITS_BASER_TYPE_VCPU:
1899 indirect = its_parse_indirect_baser(its, baser,
1900 psz, &order,
1901 ITS_MAX_VPEID_BITS);
1902 break;
1905 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
1906 if (err < 0) {
1907 its_free_tables(its);
1908 return err;
1911 /* Update settings which will be used for next BASERn */
1912 psz = baser->psz;
1913 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1914 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
1917 return 0;
1920 static int its_alloc_collections(struct its_node *its)
1922 int i;
1924 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
1925 GFP_KERNEL);
1926 if (!its->collections)
1927 return -ENOMEM;
1929 for (i = 0; i < nr_cpu_ids; i++)
1930 its->collections[i].target_address = ~0ULL;
1932 return 0;
1935 static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1937 struct page *pend_page;
1939 * The pending pages have to be at least 64kB aligned,
1940 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1942 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1943 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1944 if (!pend_page)
1945 return NULL;
1947 /* Make sure the GIC will observe the zero-ed page */
1948 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1950 return pend_page;
1953 static void its_free_pending_table(struct page *pt)
1955 free_pages((unsigned long)page_address(pt),
1956 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1959 static u64 its_clear_vpend_valid(void __iomem *vlpi_base)
1961 u32 count = 1000000; /* 1s! */
1962 bool clean;
1963 u64 val;
1965 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
1966 val &= ~GICR_VPENDBASER_Valid;
1967 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
1969 do {
1970 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
1971 clean = !(val & GICR_VPENDBASER_Dirty);
1972 if (!clean) {
1973 count--;
1974 cpu_relax();
1975 udelay(1);
1977 } while (!clean && count);
1979 return val;
1982 static void its_cpu_init_lpis(void)
1984 void __iomem *rbase = gic_data_rdist_rd_base();
1985 struct page *pend_page;
1986 u64 val, tmp;
1988 /* If we didn't allocate the pending table yet, do it now */
1989 pend_page = gic_data_rdist()->pend_page;
1990 if (!pend_page) {
1991 phys_addr_t paddr;
1993 pend_page = its_allocate_pending_table(GFP_NOWAIT);
1994 if (!pend_page) {
1995 pr_err("Failed to allocate PENDBASE for CPU%d\n",
1996 smp_processor_id());
1997 return;
2000 paddr = page_to_phys(pend_page);
2001 pr_info("CPU%d: using LPI pending table @%pa\n",
2002 smp_processor_id(), &paddr);
2003 gic_data_rdist()->pend_page = pend_page;
2006 /* set PROPBASE */
2007 val = (page_to_phys(gic_rdists->prop_page) |
2008 GICR_PROPBASER_InnerShareable |
2009 GICR_PROPBASER_RaWaWb |
2010 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
2012 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2013 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
2015 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
2016 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
2018 * The HW reports non-shareable, we must
2019 * remove the cacheability attributes as
2020 * well.
2022 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
2023 GICR_PROPBASER_CACHEABILITY_MASK);
2024 val |= GICR_PROPBASER_nC;
2025 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2027 pr_info_once("GIC: using cache flushing for LPI property table\n");
2028 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
2031 /* set PENDBASE */
2032 val = (page_to_phys(pend_page) |
2033 GICR_PENDBASER_InnerShareable |
2034 GICR_PENDBASER_RaWaWb);
2036 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2037 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
2039 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
2041 * The HW reports non-shareable, we must remove the
2042 * cacheability attributes as well.
2044 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
2045 GICR_PENDBASER_CACHEABILITY_MASK);
2046 val |= GICR_PENDBASER_nC;
2047 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2050 /* Enable LPIs */
2051 val = readl_relaxed(rbase + GICR_CTLR);
2052 val |= GICR_CTLR_ENABLE_LPIS;
2053 writel_relaxed(val, rbase + GICR_CTLR);
2055 if (gic_rdists->has_vlpis) {
2056 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2059 * It's possible for CPU to receive VLPIs before it is
2060 * sheduled as a vPE, especially for the first CPU, and the
2061 * VLPI with INTID larger than 2^(IDbits+1) will be considered
2062 * as out of range and dropped by GIC.
2063 * So we initialize IDbits to known value to avoid VLPI drop.
2065 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2066 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
2067 smp_processor_id(), val);
2068 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2071 * Also clear Valid bit of GICR_VPENDBASER, in case some
2072 * ancient programming gets left in and has possibility of
2073 * corrupting memory.
2075 val = its_clear_vpend_valid(vlpi_base);
2076 WARN_ON(val & GICR_VPENDBASER_Dirty);
2079 /* Make sure the GIC has seen the above */
2080 dsb(sy);
2083 static void its_cpu_init_collection(struct its_node *its)
2085 int cpu = smp_processor_id();
2086 u64 target;
2088 /* avoid cross node collections and its mapping */
2089 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
2090 struct device_node *cpu_node;
2092 cpu_node = of_get_cpu_node(cpu, NULL);
2093 if (its->numa_node != NUMA_NO_NODE &&
2094 its->numa_node != of_node_to_nid(cpu_node))
2095 return;
2099 * We now have to bind each collection to its target
2100 * redistributor.
2102 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
2104 * This ITS wants the physical address of the
2105 * redistributor.
2107 target = gic_data_rdist()->phys_base;
2108 } else {
2109 /* This ITS wants a linear CPU number. */
2110 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2111 target = GICR_TYPER_CPU_NUMBER(target) << 16;
2114 /* Perform collection mapping */
2115 its->collections[cpu].target_address = target;
2116 its->collections[cpu].col_id = cpu;
2118 its_send_mapc(its, &its->collections[cpu], 1);
2119 its_send_invall(its, &its->collections[cpu]);
2122 static void its_cpu_init_collections(void)
2124 struct its_node *its;
2126 raw_spin_lock(&its_lock);
2128 list_for_each_entry(its, &its_nodes, entry)
2129 its_cpu_init_collection(its);
2131 raw_spin_unlock(&its_lock);
2134 static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
2136 struct its_device *its_dev = NULL, *tmp;
2137 unsigned long flags;
2139 raw_spin_lock_irqsave(&its->lock, flags);
2141 list_for_each_entry(tmp, &its->its_device_list, entry) {
2142 if (tmp->device_id == dev_id) {
2143 its_dev = tmp;
2144 break;
2148 raw_spin_unlock_irqrestore(&its->lock, flags);
2150 return its_dev;
2153 static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2155 int i;
2157 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2158 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2159 return &its->tables[i];
2162 return NULL;
2165 static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
2167 struct page *page;
2168 u32 esz, idx;
2169 __le64 *table;
2171 /* Don't allow device id that exceeds single, flat table limit */
2172 esz = GITS_BASER_ENTRY_SIZE(baser->val);
2173 if (!(baser->val & GITS_BASER_INDIRECT))
2174 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
2176 /* Compute 1st level table index & check if that exceeds table limit */
2177 idx = id >> ilog2(baser->psz / esz);
2178 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
2179 return false;
2181 table = baser->base;
2183 /* Allocate memory for 2nd level table */
2184 if (!table[idx]) {
2185 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
2186 if (!page)
2187 return false;
2189 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2190 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
2191 gic_flush_dcache_to_poc(page_address(page), baser->psz);
2193 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2195 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2196 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
2197 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
2199 /* Ensure updated table contents are visible to ITS hardware */
2200 dsb(sy);
2203 return true;
2206 static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
2208 struct its_baser *baser;
2210 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
2212 /* Don't allow device id that exceeds ITS hardware limit */
2213 if (!baser)
2214 return (ilog2(dev_id) < its->device_ids);
2216 return its_alloc_table_entry(baser, dev_id);
2219 static bool its_alloc_vpe_table(u32 vpe_id)
2221 struct its_node *its;
2224 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
2225 * could try and only do it on ITSs corresponding to devices
2226 * that have interrupts targeted at this VPE, but the
2227 * complexity becomes crazy (and you have tons of memory
2228 * anyway, right?).
2230 list_for_each_entry(its, &its_nodes, entry) {
2231 struct its_baser *baser;
2233 if (!its->is_v4)
2234 continue;
2236 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
2237 if (!baser)
2238 return false;
2240 if (!its_alloc_table_entry(baser, vpe_id))
2241 return false;
2244 return true;
2247 static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
2248 int nvecs, bool alloc_lpis)
2250 struct its_device *dev;
2251 unsigned long *lpi_map = NULL;
2252 unsigned long flags;
2253 u16 *col_map = NULL;
2254 void *itt;
2255 int lpi_base;
2256 int nr_lpis;
2257 int nr_ites;
2258 int sz;
2260 if (!its_alloc_device_table(its, dev_id))
2261 return NULL;
2263 if (WARN_ON(!is_power_of_2(nvecs)))
2264 nvecs = roundup_pow_of_two(nvecs);
2266 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2268 * Even if the device wants a single LPI, the ITT must be
2269 * sized as a power of two (and you need at least one bit...).
2271 nr_ites = max(2, nvecs);
2272 sz = nr_ites * its->ite_size;
2273 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
2274 itt = kzalloc(sz, GFP_KERNEL);
2275 if (alloc_lpis) {
2276 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
2277 if (lpi_map)
2278 col_map = kcalloc(nr_lpis, sizeof(*col_map),
2279 GFP_KERNEL);
2280 } else {
2281 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
2282 nr_lpis = 0;
2283 lpi_base = 0;
2286 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
2287 kfree(dev);
2288 kfree(itt);
2289 kfree(lpi_map);
2290 kfree(col_map);
2291 return NULL;
2294 gic_flush_dcache_to_poc(itt, sz);
2296 dev->its = its;
2297 dev->itt = itt;
2298 dev->nr_ites = nr_ites;
2299 dev->event_map.lpi_map = lpi_map;
2300 dev->event_map.col_map = col_map;
2301 dev->event_map.lpi_base = lpi_base;
2302 dev->event_map.nr_lpis = nr_lpis;
2303 mutex_init(&dev->event_map.vlpi_lock);
2304 dev->device_id = dev_id;
2305 INIT_LIST_HEAD(&dev->entry);
2307 raw_spin_lock_irqsave(&its->lock, flags);
2308 list_add(&dev->entry, &its->its_device_list);
2309 raw_spin_unlock_irqrestore(&its->lock, flags);
2311 /* Map device to its ITT */
2312 its_send_mapd(dev, 1);
2314 return dev;
2317 static void its_free_device(struct its_device *its_dev)
2319 unsigned long flags;
2321 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
2322 list_del(&its_dev->entry);
2323 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
2324 kfree(its_dev->itt);
2325 kfree(its_dev);
2328 static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
2330 int idx;
2332 idx = bitmap_find_free_region(dev->event_map.lpi_map,
2333 dev->event_map.nr_lpis,
2334 get_count_order(nvecs));
2335 if (idx < 0)
2336 return -ENOSPC;
2338 *hwirq = dev->event_map.lpi_base + idx;
2339 set_bit(idx, dev->event_map.lpi_map);
2341 return 0;
2344 static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2345 int nvec, msi_alloc_info_t *info)
2347 struct its_node *its;
2348 struct its_device *its_dev;
2349 struct msi_domain_info *msi_info;
2350 u32 dev_id;
2351 int err = 0;
2354 * We ignore "dev" entierely, and rely on the dev_id that has
2355 * been passed via the scratchpad. This limits this domain's
2356 * usefulness to upper layers that definitely know that they
2357 * are built on top of the ITS.
2359 dev_id = info->scratchpad[0].ul;
2361 msi_info = msi_get_domain_info(domain);
2362 its = msi_info->data;
2364 if (!gic_rdists->has_direct_lpi &&
2365 vpe_proxy.dev &&
2366 vpe_proxy.dev->its == its &&
2367 dev_id == vpe_proxy.dev->device_id) {
2368 /* Bad luck. Get yourself a better implementation */
2369 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
2370 dev_id);
2371 return -EINVAL;
2374 mutex_lock(&its->dev_alloc_lock);
2375 its_dev = its_find_device(its, dev_id);
2376 if (its_dev) {
2378 * We already have seen this ID, probably through
2379 * another alias (PCI bridge of some sort). No need to
2380 * create the device.
2382 its_dev->shared = true;
2383 pr_debug("Reusing ITT for devID %x\n", dev_id);
2384 goto out;
2387 its_dev = its_create_device(its, dev_id, nvec, true);
2388 if (!its_dev) {
2389 err = -ENOMEM;
2390 goto out;
2393 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
2394 out:
2395 mutex_unlock(&its->dev_alloc_lock);
2396 info->scratchpad[0].ptr = its_dev;
2397 return err;
2400 static struct msi_domain_ops its_msi_domain_ops = {
2401 .msi_prepare = its_msi_prepare,
2404 static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2405 unsigned int virq,
2406 irq_hw_number_t hwirq)
2408 struct irq_fwspec fwspec;
2410 if (irq_domain_get_of_node(domain->parent)) {
2411 fwspec.fwnode = domain->parent->fwnode;
2412 fwspec.param_count = 3;
2413 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2414 fwspec.param[1] = hwirq;
2415 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
2416 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2417 fwspec.fwnode = domain->parent->fwnode;
2418 fwspec.param_count = 2;
2419 fwspec.param[0] = hwirq;
2420 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
2421 } else {
2422 return -EINVAL;
2425 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
2428 static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2429 unsigned int nr_irqs, void *args)
2431 msi_alloc_info_t *info = args;
2432 struct its_device *its_dev = info->scratchpad[0].ptr;
2433 irq_hw_number_t hwirq;
2434 int err;
2435 int i;
2437 err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
2438 if (err)
2439 return err;
2441 for (i = 0; i < nr_irqs; i++) {
2442 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
2443 if (err)
2444 return err;
2446 irq_domain_set_hwirq_and_chip(domain, virq + i,
2447 hwirq + i, &its_irq_chip, its_dev);
2448 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
2449 pr_debug("ID:%d pID:%d vID:%d\n",
2450 (int)(hwirq + i - its_dev->event_map.lpi_base),
2451 (int)(hwirq + i), virq + i);
2454 return 0;
2457 static int its_irq_domain_activate(struct irq_domain *domain,
2458 struct irq_data *d, bool reserve)
2460 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2461 u32 event = its_get_event_id(d);
2462 const struct cpumask *cpu_mask = cpu_online_mask;
2463 int cpu;
2465 /* get the cpu_mask of local node */
2466 if (its_dev->its->numa_node >= 0)
2467 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
2469 /* Bind the LPI to the first possible CPU */
2470 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
2471 if (cpu >= nr_cpu_ids) {
2472 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
2473 return -EINVAL;
2475 cpu = cpumask_first(cpu_online_mask);
2478 its_dev->event_map.col_map[event] = cpu;
2479 irq_data_update_effective_affinity(d, cpumask_of(cpu));
2481 /* Map the GIC IRQ and event to the device */
2482 its_send_mapti(its_dev, d->hwirq, event);
2483 return 0;
2486 static void its_irq_domain_deactivate(struct irq_domain *domain,
2487 struct irq_data *d)
2489 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2490 u32 event = its_get_event_id(d);
2492 /* Stop the delivery of interrupts */
2493 its_send_discard(its_dev, event);
2496 static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2497 unsigned int nr_irqs)
2499 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2500 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2501 struct its_node *its = its_dev->its;
2502 int i;
2504 for (i = 0; i < nr_irqs; i++) {
2505 struct irq_data *data = irq_domain_get_irq_data(domain,
2506 virq + i);
2507 u32 event = its_get_event_id(data);
2509 /* Mark interrupt index as unused */
2510 clear_bit(event, its_dev->event_map.lpi_map);
2512 /* Nuke the entry in the domain */
2513 irq_domain_reset_irq_data(data);
2516 mutex_lock(&its->dev_alloc_lock);
2519 * If all interrupts have been freed, start mopping the
2520 * floor. This is conditionned on the device not being shared.
2522 if (!its_dev->shared &&
2523 bitmap_empty(its_dev->event_map.lpi_map,
2524 its_dev->event_map.nr_lpis)) {
2525 its_lpi_free(its_dev->event_map.lpi_map,
2526 its_dev->event_map.lpi_base,
2527 its_dev->event_map.nr_lpis);
2528 kfree(its_dev->event_map.col_map);
2530 /* Unmap device/itt */
2531 its_send_mapd(its_dev, 0);
2532 its_free_device(its_dev);
2535 mutex_unlock(&its->dev_alloc_lock);
2537 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2540 static const struct irq_domain_ops its_domain_ops = {
2541 .alloc = its_irq_domain_alloc,
2542 .free = its_irq_domain_free,
2543 .activate = its_irq_domain_activate,
2544 .deactivate = its_irq_domain_deactivate,
2548 * This is insane.
2550 * If a GICv4 doesn't implement Direct LPIs (which is extremely
2551 * likely), the only way to perform an invalidate is to use a fake
2552 * device to issue an INV command, implying that the LPI has first
2553 * been mapped to some event on that device. Since this is not exactly
2554 * cheap, we try to keep that mapping around as long as possible, and
2555 * only issue an UNMAP if we're short on available slots.
2557 * Broken by design(tm).
2559 static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
2561 /* Already unmapped? */
2562 if (vpe->vpe_proxy_event == -1)
2563 return;
2565 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
2566 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
2569 * We don't track empty slots at all, so let's move the
2570 * next_victim pointer if we can quickly reuse that slot
2571 * instead of nuking an existing entry. Not clear that this is
2572 * always a win though, and this might just generate a ripple
2573 * effect... Let's just hope VPEs don't migrate too often.
2575 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2576 vpe_proxy.next_victim = vpe->vpe_proxy_event;
2578 vpe->vpe_proxy_event = -1;
2581 static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
2583 if (!gic_rdists->has_direct_lpi) {
2584 unsigned long flags;
2586 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2587 its_vpe_db_proxy_unmap_locked(vpe);
2588 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2592 static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
2594 /* Already mapped? */
2595 if (vpe->vpe_proxy_event != -1)
2596 return;
2598 /* This slot was already allocated. Kick the other VPE out. */
2599 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2600 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
2602 /* Map the new VPE instead */
2603 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
2604 vpe->vpe_proxy_event = vpe_proxy.next_victim;
2605 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
2607 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
2608 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
2611 static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
2613 unsigned long flags;
2614 struct its_collection *target_col;
2616 if (gic_rdists->has_direct_lpi) {
2617 void __iomem *rdbase;
2619 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
2620 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2621 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2622 cpu_relax();
2624 return;
2627 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2629 its_vpe_db_proxy_map_locked(vpe);
2631 target_col = &vpe_proxy.dev->its->collections[to];
2632 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
2633 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
2635 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2638 static int its_vpe_set_affinity(struct irq_data *d,
2639 const struct cpumask *mask_val,
2640 bool force)
2642 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2643 int cpu = cpumask_first(mask_val);
2646 * Changing affinity is mega expensive, so let's be as lazy as
2647 * we can and only do it if we really have to. Also, if mapped
2648 * into the proxy device, we need to move the doorbell
2649 * interrupt to its new location.
2651 if (vpe->col_idx != cpu) {
2652 int from = vpe->col_idx;
2654 vpe->col_idx = cpu;
2655 its_send_vmovp(vpe);
2656 its_vpe_db_proxy_move(vpe, from, cpu);
2659 irq_data_update_effective_affinity(d, cpumask_of(cpu));
2661 return IRQ_SET_MASK_OK_DONE;
2664 static void its_vpe_schedule(struct its_vpe *vpe)
2666 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2667 u64 val;
2669 /* Schedule the VPE */
2670 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2671 GENMASK_ULL(51, 12);
2672 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2673 val |= GICR_VPROPBASER_RaWb;
2674 val |= GICR_VPROPBASER_InnerShareable;
2675 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2677 val = virt_to_phys(page_address(vpe->vpt_page)) &
2678 GENMASK_ULL(51, 16);
2679 val |= GICR_VPENDBASER_RaWaWb;
2680 val |= GICR_VPENDBASER_NonShareable;
2682 * There is no good way of finding out if the pending table is
2683 * empty as we can race against the doorbell interrupt very
2684 * easily. So in the end, vpe->pending_last is only an
2685 * indication that the vcpu has something pending, not one
2686 * that the pending table is empty. A good implementation
2687 * would be able to read its coarse map pretty quickly anyway,
2688 * making this a tolerable issue.
2690 val |= GICR_VPENDBASER_PendingLast;
2691 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2692 val |= GICR_VPENDBASER_Valid;
2693 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2696 static void its_vpe_deschedule(struct its_vpe *vpe)
2698 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2699 u64 val;
2701 val = its_clear_vpend_valid(vlpi_base);
2703 if (unlikely(val & GICR_VPENDBASER_Dirty)) {
2704 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2705 vpe->idai = false;
2706 vpe->pending_last = true;
2707 } else {
2708 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2709 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2713 static void its_vpe_invall(struct its_vpe *vpe)
2715 struct its_node *its;
2717 list_for_each_entry(its, &its_nodes, entry) {
2718 if (!its->is_v4)
2719 continue;
2721 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
2722 continue;
2725 * Sending a VINVALL to a single ITS is enough, as all
2726 * we need is to reach the redistributors.
2728 its_send_vinvall(its, vpe);
2729 return;
2733 static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2735 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2736 struct its_cmd_info *info = vcpu_info;
2738 switch (info->cmd_type) {
2739 case SCHEDULE_VPE:
2740 its_vpe_schedule(vpe);
2741 return 0;
2743 case DESCHEDULE_VPE:
2744 its_vpe_deschedule(vpe);
2745 return 0;
2747 case INVALL_VPE:
2748 its_vpe_invall(vpe);
2749 return 0;
2751 default:
2752 return -EINVAL;
2756 static void its_vpe_send_cmd(struct its_vpe *vpe,
2757 void (*cmd)(struct its_device *, u32))
2759 unsigned long flags;
2761 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2763 its_vpe_db_proxy_map_locked(vpe);
2764 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
2766 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2769 static void its_vpe_send_inv(struct irq_data *d)
2771 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2773 if (gic_rdists->has_direct_lpi) {
2774 void __iomem *rdbase;
2776 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2777 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
2778 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2779 cpu_relax();
2780 } else {
2781 its_vpe_send_cmd(vpe, its_send_inv);
2785 static void its_vpe_mask_irq(struct irq_data *d)
2788 * We need to unmask the LPI, which is described by the parent
2789 * irq_data. Instead of calling into the parent (which won't
2790 * exactly do the right thing, let's simply use the
2791 * parent_data pointer. Yes, I'm naughty.
2793 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
2794 its_vpe_send_inv(d);
2797 static void its_vpe_unmask_irq(struct irq_data *d)
2799 /* Same hack as above... */
2800 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
2801 its_vpe_send_inv(d);
2804 static int its_vpe_set_irqchip_state(struct irq_data *d,
2805 enum irqchip_irq_state which,
2806 bool state)
2808 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2810 if (which != IRQCHIP_STATE_PENDING)
2811 return -EINVAL;
2813 if (gic_rdists->has_direct_lpi) {
2814 void __iomem *rdbase;
2816 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2817 if (state) {
2818 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
2819 } else {
2820 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2821 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2822 cpu_relax();
2824 } else {
2825 if (state)
2826 its_vpe_send_cmd(vpe, its_send_int);
2827 else
2828 its_vpe_send_cmd(vpe, its_send_clear);
2831 return 0;
2834 static struct irq_chip its_vpe_irq_chip = {
2835 .name = "GICv4-vpe",
2836 .irq_mask = its_vpe_mask_irq,
2837 .irq_unmask = its_vpe_unmask_irq,
2838 .irq_eoi = irq_chip_eoi_parent,
2839 .irq_set_affinity = its_vpe_set_affinity,
2840 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
2841 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
2844 static int its_vpe_id_alloc(void)
2846 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
2849 static void its_vpe_id_free(u16 id)
2851 ida_simple_remove(&its_vpeid_ida, id);
2854 static int its_vpe_init(struct its_vpe *vpe)
2856 struct page *vpt_page;
2857 int vpe_id;
2859 /* Allocate vpe_id */
2860 vpe_id = its_vpe_id_alloc();
2861 if (vpe_id < 0)
2862 return vpe_id;
2864 /* Allocate VPT */
2865 vpt_page = its_allocate_pending_table(GFP_KERNEL);
2866 if (!vpt_page) {
2867 its_vpe_id_free(vpe_id);
2868 return -ENOMEM;
2871 if (!its_alloc_vpe_table(vpe_id)) {
2872 its_vpe_id_free(vpe_id);
2873 its_free_pending_table(vpe->vpt_page);
2874 return -ENOMEM;
2877 vpe->vpe_id = vpe_id;
2878 vpe->vpt_page = vpt_page;
2879 vpe->vpe_proxy_event = -1;
2881 return 0;
2884 static void its_vpe_teardown(struct its_vpe *vpe)
2886 its_vpe_db_proxy_unmap(vpe);
2887 its_vpe_id_free(vpe->vpe_id);
2888 its_free_pending_table(vpe->vpt_page);
2891 static void its_vpe_irq_domain_free(struct irq_domain *domain,
2892 unsigned int virq,
2893 unsigned int nr_irqs)
2895 struct its_vm *vm = domain->host_data;
2896 int i;
2898 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2900 for (i = 0; i < nr_irqs; i++) {
2901 struct irq_data *data = irq_domain_get_irq_data(domain,
2902 virq + i);
2903 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
2905 BUG_ON(vm != vpe->its_vm);
2907 clear_bit(data->hwirq, vm->db_bitmap);
2908 its_vpe_teardown(vpe);
2909 irq_domain_reset_irq_data(data);
2912 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
2913 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
2914 its_free_prop_table(vm->vprop_page);
2918 static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2919 unsigned int nr_irqs, void *args)
2921 struct its_vm *vm = args;
2922 unsigned long *bitmap;
2923 struct page *vprop_page;
2924 int base, nr_ids, i, err = 0;
2926 BUG_ON(!vm);
2928 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
2929 if (!bitmap)
2930 return -ENOMEM;
2932 if (nr_ids < nr_irqs) {
2933 its_lpi_free(bitmap, base, nr_ids);
2934 return -ENOMEM;
2937 vprop_page = its_allocate_prop_table(GFP_KERNEL);
2938 if (!vprop_page) {
2939 its_lpi_free(bitmap, base, nr_ids);
2940 return -ENOMEM;
2943 vm->db_bitmap = bitmap;
2944 vm->db_lpi_base = base;
2945 vm->nr_db_lpis = nr_ids;
2946 vm->vprop_page = vprop_page;
2948 for (i = 0; i < nr_irqs; i++) {
2949 vm->vpes[i]->vpe_db_lpi = base + i;
2950 err = its_vpe_init(vm->vpes[i]);
2951 if (err)
2952 break;
2953 err = its_irq_gic_domain_alloc(domain, virq + i,
2954 vm->vpes[i]->vpe_db_lpi);
2955 if (err)
2956 break;
2957 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
2958 &its_vpe_irq_chip, vm->vpes[i]);
2959 set_bit(i, bitmap);
2962 if (err) {
2963 if (i > 0)
2964 its_vpe_irq_domain_free(domain, virq, i - 1);
2966 its_lpi_free(bitmap, base, nr_ids);
2967 its_free_prop_table(vprop_page);
2970 return err;
2973 static int its_vpe_irq_domain_activate(struct irq_domain *domain,
2974 struct irq_data *d, bool reserve)
2976 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2977 struct its_node *its;
2979 /* If we use the list map, we issue VMAPP on demand... */
2980 if (its_list_map)
2981 return 0;
2983 /* Map the VPE to the first possible CPU */
2984 vpe->col_idx = cpumask_first(cpu_online_mask);
2986 list_for_each_entry(its, &its_nodes, entry) {
2987 if (!its->is_v4)
2988 continue;
2990 its_send_vmapp(its, vpe, true);
2991 its_send_vinvall(its, vpe);
2994 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
2996 return 0;
2999 static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
3000 struct irq_data *d)
3002 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3003 struct its_node *its;
3006 * If we use the list map, we unmap the VPE once no VLPIs are
3007 * associated with the VM.
3009 if (its_list_map)
3010 return;
3012 list_for_each_entry(its, &its_nodes, entry) {
3013 if (!its->is_v4)
3014 continue;
3016 its_send_vmapp(its, vpe, false);
3020 static const struct irq_domain_ops its_vpe_domain_ops = {
3021 .alloc = its_vpe_irq_domain_alloc,
3022 .free = its_vpe_irq_domain_free,
3023 .activate = its_vpe_irq_domain_activate,
3024 .deactivate = its_vpe_irq_domain_deactivate,
3027 static int its_force_quiescent(void __iomem *base)
3029 u32 count = 1000000; /* 1s */
3030 u32 val;
3032 val = readl_relaxed(base + GITS_CTLR);
3034 * GIC architecture specification requires the ITS to be both
3035 * disabled and quiescent for writes to GITS_BASER<n> or
3036 * GITS_CBASER to not have UNPREDICTABLE results.
3038 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
3039 return 0;
3041 /* Disable the generation of all interrupts to this ITS */
3042 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
3043 writel_relaxed(val, base + GITS_CTLR);
3045 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
3046 while (1) {
3047 val = readl_relaxed(base + GITS_CTLR);
3048 if (val & GITS_CTLR_QUIESCENT)
3049 return 0;
3051 count--;
3052 if (!count)
3053 return -EBUSY;
3055 cpu_relax();
3056 udelay(1);
3060 static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
3062 struct its_node *its = data;
3064 /* erratum 22375: only alloc 8MB table size */
3065 its->device_ids = 0x14; /* 20 bits, 8MB */
3066 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
3068 return true;
3071 static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
3073 struct its_node *its = data;
3075 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
3077 return true;
3080 static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
3082 struct its_node *its = data;
3084 /* On QDF2400, the size of the ITE is 16Bytes */
3085 its->ite_size = 16;
3087 return true;
3090 static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
3092 struct its_node *its = its_dev->its;
3095 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
3096 * which maps 32-bit writes targeted at a separate window of
3097 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
3098 * with device ID taken from bits [device_id_bits + 1:2] of
3099 * the window offset.
3101 return its->pre_its_base + (its_dev->device_id << 2);
3104 static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
3106 struct its_node *its = data;
3107 u32 pre_its_window[2];
3108 u32 ids;
3110 if (!fwnode_property_read_u32_array(its->fwnode_handle,
3111 "socionext,synquacer-pre-its",
3112 pre_its_window,
3113 ARRAY_SIZE(pre_its_window))) {
3115 its->pre_its_base = pre_its_window[0];
3116 its->get_msi_base = its_irq_get_msi_base_pre_its;
3118 ids = ilog2(pre_its_window[1]) - 2;
3119 if (its->device_ids > ids)
3120 its->device_ids = ids;
3122 /* the pre-ITS breaks isolation, so disable MSI remapping */
3123 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
3124 return true;
3126 return false;
3129 static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
3131 struct its_node *its = data;
3134 * Hip07 insists on using the wrong address for the VLPI
3135 * page. Trick it into doing the right thing...
3137 its->vlpi_redist_offset = SZ_128K;
3138 return true;
3141 static const struct gic_quirk its_quirks[] = {
3142 #ifdef CONFIG_CAVIUM_ERRATUM_22375
3144 .desc = "ITS: Cavium errata 22375, 24313",
3145 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3146 .mask = 0xffff0fff,
3147 .init = its_enable_quirk_cavium_22375,
3149 #endif
3150 #ifdef CONFIG_CAVIUM_ERRATUM_23144
3152 .desc = "ITS: Cavium erratum 23144",
3153 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3154 .mask = 0xffff0fff,
3155 .init = its_enable_quirk_cavium_23144,
3157 #endif
3158 #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
3160 .desc = "ITS: QDF2400 erratum 0065",
3161 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
3162 .mask = 0xffffffff,
3163 .init = its_enable_quirk_qdf2400_e0065,
3165 #endif
3166 #ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
3169 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
3170 * implementation, but with a 'pre-ITS' added that requires
3171 * special handling in software.
3173 .desc = "ITS: Socionext Synquacer pre-ITS",
3174 .iidr = 0x0001143b,
3175 .mask = 0xffffffff,
3176 .init = its_enable_quirk_socionext_synquacer,
3178 #endif
3179 #ifdef CONFIG_HISILICON_ERRATUM_161600802
3181 .desc = "ITS: Hip07 erratum 161600802",
3182 .iidr = 0x00000004,
3183 .mask = 0xffffffff,
3184 .init = its_enable_quirk_hip07_161600802,
3186 #endif
3191 static void its_enable_quirks(struct its_node *its)
3193 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
3195 gic_enable_quirks(iidr, its_quirks, its);
3198 static int its_save_disable(void)
3200 struct its_node *its;
3201 int err = 0;
3203 raw_spin_lock(&its_lock);
3204 list_for_each_entry(its, &its_nodes, entry) {
3205 void __iomem *base;
3207 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3208 continue;
3210 base = its->base;
3211 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
3212 err = its_force_quiescent(base);
3213 if (err) {
3214 pr_err("ITS@%pa: failed to quiesce: %d\n",
3215 &its->phys_base, err);
3216 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3217 goto err;
3220 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
3223 err:
3224 if (err) {
3225 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
3226 void __iomem *base;
3228 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3229 continue;
3231 base = its->base;
3232 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3235 raw_spin_unlock(&its_lock);
3237 return err;
3240 static void its_restore_enable(void)
3242 struct its_node *its;
3243 int ret;
3245 raw_spin_lock(&its_lock);
3246 list_for_each_entry(its, &its_nodes, entry) {
3247 void __iomem *base;
3248 int i;
3250 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3251 continue;
3253 base = its->base;
3256 * Make sure that the ITS is disabled. If it fails to quiesce,
3257 * don't restore it since writing to CBASER or BASER<n>
3258 * registers is undefined according to the GIC v3 ITS
3259 * Specification.
3261 ret = its_force_quiescent(base);
3262 if (ret) {
3263 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
3264 &its->phys_base, ret);
3265 continue;
3268 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
3271 * Writing CBASER resets CREADR to 0, so make CWRITER and
3272 * cmd_write line up with it.
3274 its->cmd_write = its->cmd_base;
3275 gits_write_cwriter(0, base + GITS_CWRITER);
3277 /* Restore GITS_BASER from the value cache. */
3278 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
3279 struct its_baser *baser = &its->tables[i];
3281 if (!(baser->val & GITS_BASER_VALID))
3282 continue;
3284 its_write_baser(its, baser, baser->val);
3286 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3289 * Reinit the collection if it's stored in the ITS. This is
3290 * indicated by the col_id being less than the HCC field.
3291 * CID < HCC as specified in the GIC v3 Documentation.
3293 if (its->collections[smp_processor_id()].col_id <
3294 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
3295 its_cpu_init_collection(its);
3297 raw_spin_unlock(&its_lock);
3300 static struct syscore_ops its_syscore_ops = {
3301 .suspend = its_save_disable,
3302 .resume = its_restore_enable,
3305 static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
3307 struct irq_domain *inner_domain;
3308 struct msi_domain_info *info;
3310 info = kzalloc(sizeof(*info), GFP_KERNEL);
3311 if (!info)
3312 return -ENOMEM;
3314 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
3315 if (!inner_domain) {
3316 kfree(info);
3317 return -ENOMEM;
3320 inner_domain->parent = its_parent;
3321 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
3322 inner_domain->flags |= its->msi_domain_flags;
3323 info->ops = &its_msi_domain_ops;
3324 info->data = its;
3325 inner_domain->host_data = info;
3327 return 0;
3330 static int its_init_vpe_domain(void)
3332 struct its_node *its;
3333 u32 devid;
3334 int entries;
3336 if (gic_rdists->has_direct_lpi) {
3337 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
3338 return 0;
3341 /* Any ITS will do, even if not v4 */
3342 its = list_first_entry(&its_nodes, struct its_node, entry);
3344 entries = roundup_pow_of_two(nr_cpu_ids);
3345 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
3346 GFP_KERNEL);
3347 if (!vpe_proxy.vpes) {
3348 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
3349 return -ENOMEM;
3352 /* Use the last possible DevID */
3353 devid = GENMASK(its->device_ids - 1, 0);
3354 vpe_proxy.dev = its_create_device(its, devid, entries, false);
3355 if (!vpe_proxy.dev) {
3356 kfree(vpe_proxy.vpes);
3357 pr_err("ITS: Can't allocate GICv4 proxy device\n");
3358 return -ENOMEM;
3361 BUG_ON(entries > vpe_proxy.dev->nr_ites);
3363 raw_spin_lock_init(&vpe_proxy.lock);
3364 vpe_proxy.next_victim = 0;
3365 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
3366 devid, vpe_proxy.dev->nr_ites);
3368 return 0;
3371 static int __init its_compute_its_list_map(struct resource *res,
3372 void __iomem *its_base)
3374 int its_number;
3375 u32 ctlr;
3378 * This is assumed to be done early enough that we're
3379 * guaranteed to be single-threaded, hence no
3380 * locking. Should this change, we should address
3381 * this.
3383 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
3384 if (its_number >= GICv4_ITS_LIST_MAX) {
3385 pr_err("ITS@%pa: No ITSList entry available!\n",
3386 &res->start);
3387 return -EINVAL;
3390 ctlr = readl_relaxed(its_base + GITS_CTLR);
3391 ctlr &= ~GITS_CTLR_ITS_NUMBER;
3392 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
3393 writel_relaxed(ctlr, its_base + GITS_CTLR);
3394 ctlr = readl_relaxed(its_base + GITS_CTLR);
3395 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
3396 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
3397 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
3400 if (test_and_set_bit(its_number, &its_list_map)) {
3401 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
3402 &res->start, its_number);
3403 return -EINVAL;
3406 return its_number;
3409 static int __init its_probe_one(struct resource *res,
3410 struct fwnode_handle *handle, int numa_node)
3412 struct its_node *its;
3413 void __iomem *its_base;
3414 u32 val, ctlr;
3415 u64 baser, tmp, typer;
3416 int err;
3418 its_base = ioremap(res->start, resource_size(res));
3419 if (!its_base) {
3420 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
3421 return -ENOMEM;
3424 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
3425 if (val != 0x30 && val != 0x40) {
3426 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
3427 err = -ENODEV;
3428 goto out_unmap;
3431 err = its_force_quiescent(its_base);
3432 if (err) {
3433 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
3434 goto out_unmap;
3437 pr_info("ITS %pR\n", res);
3439 its = kzalloc(sizeof(*its), GFP_KERNEL);
3440 if (!its) {
3441 err = -ENOMEM;
3442 goto out_unmap;
3445 raw_spin_lock_init(&its->lock);
3446 mutex_init(&its->dev_alloc_lock);
3447 INIT_LIST_HEAD(&its->entry);
3448 INIT_LIST_HEAD(&its->its_device_list);
3449 typer = gic_read_typer(its_base + GITS_TYPER);
3450 its->base = its_base;
3451 its->phys_base = res->start;
3452 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
3453 its->device_ids = GITS_TYPER_DEVBITS(typer);
3454 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
3455 if (its->is_v4) {
3456 if (!(typer & GITS_TYPER_VMOVP)) {
3457 err = its_compute_its_list_map(res, its_base);
3458 if (err < 0)
3459 goto out_free_its;
3461 its->list_nr = err;
3463 pr_info("ITS@%pa: Using ITS number %d\n",
3464 &res->start, err);
3465 } else {
3466 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
3470 its->numa_node = numa_node;
3472 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
3473 get_order(ITS_CMD_QUEUE_SZ));
3474 if (!its->cmd_base) {
3475 err = -ENOMEM;
3476 goto out_free_its;
3478 its->cmd_write = its->cmd_base;
3479 its->fwnode_handle = handle;
3480 its->get_msi_base = its_irq_get_msi_base;
3481 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
3483 its_enable_quirks(its);
3485 err = its_alloc_tables(its);
3486 if (err)
3487 goto out_free_cmd;
3489 err = its_alloc_collections(its);
3490 if (err)
3491 goto out_free_tables;
3493 baser = (virt_to_phys(its->cmd_base) |
3494 GITS_CBASER_RaWaWb |
3495 GITS_CBASER_InnerShareable |
3496 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
3497 GITS_CBASER_VALID);
3499 gits_write_cbaser(baser, its->base + GITS_CBASER);
3500 tmp = gits_read_cbaser(its->base + GITS_CBASER);
3502 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
3503 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
3505 * The HW reports non-shareable, we must
3506 * remove the cacheability attributes as
3507 * well.
3509 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
3510 GITS_CBASER_CACHEABILITY_MASK);
3511 baser |= GITS_CBASER_nC;
3512 gits_write_cbaser(baser, its->base + GITS_CBASER);
3514 pr_info("ITS: using cache flushing for cmd queue\n");
3515 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
3518 gits_write_cwriter(0, its->base + GITS_CWRITER);
3519 ctlr = readl_relaxed(its->base + GITS_CTLR);
3520 ctlr |= GITS_CTLR_ENABLE;
3521 if (its->is_v4)
3522 ctlr |= GITS_CTLR_ImDe;
3523 writel_relaxed(ctlr, its->base + GITS_CTLR);
3525 if (GITS_TYPER_HCC(typer))
3526 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
3528 err = its_init_domain(handle, its);
3529 if (err)
3530 goto out_free_tables;
3532 raw_spin_lock(&its_lock);
3533 list_add(&its->entry, &its_nodes);
3534 raw_spin_unlock(&its_lock);
3536 return 0;
3538 out_free_tables:
3539 its_free_tables(its);
3540 out_free_cmd:
3541 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
3542 out_free_its:
3543 kfree(its);
3544 out_unmap:
3545 iounmap(its_base);
3546 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
3547 return err;
3550 static bool gic_rdists_supports_plpis(void)
3552 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
3555 static int redist_disable_lpis(void)
3557 void __iomem *rbase = gic_data_rdist_rd_base();
3558 u64 timeout = USEC_PER_SEC;
3559 u64 val;
3562 * If coming via a CPU hotplug event, we don't need to disable
3563 * LPIs before trying to re-enable them. They are already
3564 * configured and all is well in the world. Detect this case
3565 * by checking the allocation of the pending table for the
3566 * current CPU.
3568 if (gic_data_rdist()->pend_page)
3569 return 0;
3571 if (!gic_rdists_supports_plpis()) {
3572 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
3573 return -ENXIO;
3576 val = readl_relaxed(rbase + GICR_CTLR);
3577 if (!(val & GICR_CTLR_ENABLE_LPIS))
3578 return 0;
3580 pr_warn("CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
3581 smp_processor_id());
3582 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
3584 /* Disable LPIs */
3585 val &= ~GICR_CTLR_ENABLE_LPIS;
3586 writel_relaxed(val, rbase + GICR_CTLR);
3588 /* Make sure any change to GICR_CTLR is observable by the GIC */
3589 dsb(sy);
3592 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
3593 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
3594 * Error out if we time out waiting for RWP to clear.
3596 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
3597 if (!timeout) {
3598 pr_err("CPU%d: Timeout while disabling LPIs\n",
3599 smp_processor_id());
3600 return -ETIMEDOUT;
3602 udelay(1);
3603 timeout--;
3607 * After it has been written to 1, it is IMPLEMENTATION
3608 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
3609 * cleared to 0. Error out if clearing the bit failed.
3611 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
3612 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
3613 return -EBUSY;
3616 return 0;
3619 int its_cpu_init(void)
3621 if (!list_empty(&its_nodes)) {
3622 int ret;
3624 ret = redist_disable_lpis();
3625 if (ret)
3626 return ret;
3628 its_cpu_init_lpis();
3629 its_cpu_init_collections();
3632 return 0;
3635 static const struct of_device_id its_device_id[] = {
3636 { .compatible = "arm,gic-v3-its", },
3640 static int __init its_of_probe(struct device_node *node)
3642 struct device_node *np;
3643 struct resource res;
3645 for (np = of_find_matching_node(node, its_device_id); np;
3646 np = of_find_matching_node(np, its_device_id)) {
3647 if (!of_device_is_available(np))
3648 continue;
3649 if (!of_property_read_bool(np, "msi-controller")) {
3650 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
3651 np);
3652 continue;
3655 if (of_address_to_resource(np, 0, &res)) {
3656 pr_warn("%pOF: no regs?\n", np);
3657 continue;
3660 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
3662 return 0;
3665 #ifdef CONFIG_ACPI
3667 #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
3669 #ifdef CONFIG_ACPI_NUMA
3670 struct its_srat_map {
3671 /* numa node id */
3672 u32 numa_node;
3673 /* GIC ITS ID */
3674 u32 its_id;
3677 static struct its_srat_map *its_srat_maps __initdata;
3678 static int its_in_srat __initdata;
3680 static int __init acpi_get_its_numa_node(u32 its_id)
3682 int i;
3684 for (i = 0; i < its_in_srat; i++) {
3685 if (its_id == its_srat_maps[i].its_id)
3686 return its_srat_maps[i].numa_node;
3688 return NUMA_NO_NODE;
3691 static int __init gic_acpi_match_srat_its(struct acpi_subtable_header *header,
3692 const unsigned long end)
3694 return 0;
3697 static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
3698 const unsigned long end)
3700 int node;
3701 struct acpi_srat_gic_its_affinity *its_affinity;
3703 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
3704 if (!its_affinity)
3705 return -EINVAL;
3707 if (its_affinity->header.length < sizeof(*its_affinity)) {
3708 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
3709 its_affinity->header.length);
3710 return -EINVAL;
3713 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
3715 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
3716 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
3717 return 0;
3720 its_srat_maps[its_in_srat].numa_node = node;
3721 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
3722 its_in_srat++;
3723 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
3724 its_affinity->proximity_domain, its_affinity->its_id, node);
3726 return 0;
3729 static void __init acpi_table_parse_srat_its(void)
3731 int count;
3733 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
3734 sizeof(struct acpi_table_srat),
3735 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3736 gic_acpi_match_srat_its, 0);
3737 if (count <= 0)
3738 return;
3740 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
3741 GFP_KERNEL);
3742 if (!its_srat_maps) {
3743 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
3744 return;
3747 acpi_table_parse_entries(ACPI_SIG_SRAT,
3748 sizeof(struct acpi_table_srat),
3749 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3750 gic_acpi_parse_srat_its, 0);
3753 /* free the its_srat_maps after ITS probing */
3754 static void __init acpi_its_srat_maps_free(void)
3756 kfree(its_srat_maps);
3758 #else
3759 static void __init acpi_table_parse_srat_its(void) { }
3760 static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
3761 static void __init acpi_its_srat_maps_free(void) { }
3762 #endif
3764 static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
3765 const unsigned long end)
3767 struct acpi_madt_generic_translator *its_entry;
3768 struct fwnode_handle *dom_handle;
3769 struct resource res;
3770 int err;
3772 its_entry = (struct acpi_madt_generic_translator *)header;
3773 memset(&res, 0, sizeof(res));
3774 res.start = its_entry->base_address;
3775 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
3776 res.flags = IORESOURCE_MEM;
3778 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
3779 if (!dom_handle) {
3780 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
3781 &res.start);
3782 return -ENOMEM;
3785 err = iort_register_domain_token(its_entry->translation_id, res.start,
3786 dom_handle);
3787 if (err) {
3788 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
3789 &res.start, its_entry->translation_id);
3790 goto dom_err;
3793 err = its_probe_one(&res, dom_handle,
3794 acpi_get_its_numa_node(its_entry->translation_id));
3795 if (!err)
3796 return 0;
3798 iort_deregister_domain_token(its_entry->translation_id);
3799 dom_err:
3800 irq_domain_free_fwnode(dom_handle);
3801 return err;
3804 static void __init its_acpi_probe(void)
3806 acpi_table_parse_srat_its();
3807 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
3808 gic_acpi_parse_madt_its, 0);
3809 acpi_its_srat_maps_free();
3811 #else
3812 static void __init its_acpi_probe(void) { }
3813 #endif
3815 int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
3816 struct irq_domain *parent_domain)
3818 struct device_node *of_node;
3819 struct its_node *its;
3820 bool has_v4 = false;
3821 int err;
3823 its_parent = parent_domain;
3824 of_node = to_of_node(handle);
3825 if (of_node)
3826 its_of_probe(of_node);
3827 else
3828 its_acpi_probe();
3830 if (list_empty(&its_nodes)) {
3831 pr_warn("ITS: No ITS available, not enabling LPIs\n");
3832 return -ENXIO;
3835 gic_rdists = rdists;
3836 err = its_alloc_lpi_tables();
3837 if (err)
3838 return err;
3840 list_for_each_entry(its, &its_nodes, entry)
3841 has_v4 |= its->is_v4;
3843 if (has_v4 & rdists->has_vlpis) {
3844 if (its_init_vpe_domain() ||
3845 its_init_v4(parent_domain, &its_vpe_domain_ops)) {
3846 rdists->has_vlpis = false;
3847 pr_err("ITS: Disabling GICv4 support\n");
3851 register_syscore_ops(&its_syscore_ops);
3853 return 0;