vt: vt_ioctl: fix VT_DISALLOCATE freeing in-use virtual console
[linux/fpc-iii.git] / drivers / irqchip / irq-gic-v3-its.c
blobf9b73336a39eee92fc8f600914809446234997bb
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 u16 get_its_list(struct its_vm *vm)
187 struct its_node *its;
188 unsigned long its_list = 0;
190 list_for_each_entry(its, &its_nodes, entry) {
191 if (!its->is_v4)
192 continue;
194 if (vm->vlpi_count[its->list_nr])
195 __set_bit(its->list_nr, &its_list);
198 return (u16)its_list;
201 static struct its_collection *dev_event_to_col(struct its_device *its_dev,
202 u32 event)
204 struct its_node *its = its_dev->its;
206 return its->collections + its_dev->event_map.col_map[event];
209 static struct its_collection *valid_col(struct its_collection *col)
211 if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(15, 0)))
212 return NULL;
214 return col;
217 static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
219 if (valid_col(its->collections + vpe->col_idx))
220 return vpe;
222 return NULL;
226 * ITS command descriptors - parameters to be encoded in a command
227 * block.
229 struct its_cmd_desc {
230 union {
231 struct {
232 struct its_device *dev;
233 u32 event_id;
234 } its_inv_cmd;
236 struct {
237 struct its_device *dev;
238 u32 event_id;
239 } its_clear_cmd;
241 struct {
242 struct its_device *dev;
243 u32 event_id;
244 } its_int_cmd;
246 struct {
247 struct its_device *dev;
248 int valid;
249 } its_mapd_cmd;
251 struct {
252 struct its_collection *col;
253 int valid;
254 } its_mapc_cmd;
256 struct {
257 struct its_device *dev;
258 u32 phys_id;
259 u32 event_id;
260 } its_mapti_cmd;
262 struct {
263 struct its_device *dev;
264 struct its_collection *col;
265 u32 event_id;
266 } its_movi_cmd;
268 struct {
269 struct its_device *dev;
270 u32 event_id;
271 } its_discard_cmd;
273 struct {
274 struct its_collection *col;
275 } its_invall_cmd;
277 struct {
278 struct its_vpe *vpe;
279 } its_vinvall_cmd;
281 struct {
282 struct its_vpe *vpe;
283 struct its_collection *col;
284 bool valid;
285 } its_vmapp_cmd;
287 struct {
288 struct its_vpe *vpe;
289 struct its_device *dev;
290 u32 virt_id;
291 u32 event_id;
292 bool db_enabled;
293 } its_vmapti_cmd;
295 struct {
296 struct its_vpe *vpe;
297 struct its_device *dev;
298 u32 event_id;
299 bool db_enabled;
300 } its_vmovi_cmd;
302 struct {
303 struct its_vpe *vpe;
304 struct its_collection *col;
305 u16 seq_num;
306 u16 its_list;
307 } its_vmovp_cmd;
312 * The ITS command block, which is what the ITS actually parses.
314 struct its_cmd_block {
315 u64 raw_cmd[4];
318 #define ITS_CMD_QUEUE_SZ SZ_64K
319 #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
321 typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
322 struct its_cmd_block *,
323 struct its_cmd_desc *);
325 typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
326 struct its_cmd_block *,
327 struct its_cmd_desc *);
329 static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
331 u64 mask = GENMASK_ULL(h, l);
332 *raw_cmd &= ~mask;
333 *raw_cmd |= (val << l) & mask;
336 static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
338 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
341 static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
343 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
346 static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
348 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
351 static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
353 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
356 static void its_encode_size(struct its_cmd_block *cmd, u8 size)
358 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
361 static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
363 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
366 static void its_encode_valid(struct its_cmd_block *cmd, int valid)
368 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
371 static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
373 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
376 static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
378 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
381 static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
383 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
386 static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
388 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
391 static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
393 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
396 static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
398 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
401 static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
403 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
406 static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
408 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
411 static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
413 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
416 static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
418 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
421 static inline void its_fixup_cmd(struct its_cmd_block *cmd)
423 /* Let's fixup BE commands */
424 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
425 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
426 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
427 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
430 static struct its_collection *its_build_mapd_cmd(struct its_node *its,
431 struct its_cmd_block *cmd,
432 struct its_cmd_desc *desc)
434 unsigned long itt_addr;
435 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
437 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
438 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
440 its_encode_cmd(cmd, GITS_CMD_MAPD);
441 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
442 its_encode_size(cmd, size - 1);
443 its_encode_itt(cmd, itt_addr);
444 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
446 its_fixup_cmd(cmd);
448 return NULL;
451 static struct its_collection *its_build_mapc_cmd(struct its_node *its,
452 struct its_cmd_block *cmd,
453 struct its_cmd_desc *desc)
455 its_encode_cmd(cmd, GITS_CMD_MAPC);
456 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
457 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
458 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
460 its_fixup_cmd(cmd);
462 return desc->its_mapc_cmd.col;
465 static struct its_collection *its_build_mapti_cmd(struct its_node *its,
466 struct its_cmd_block *cmd,
467 struct its_cmd_desc *desc)
469 struct its_collection *col;
471 col = dev_event_to_col(desc->its_mapti_cmd.dev,
472 desc->its_mapti_cmd.event_id);
474 its_encode_cmd(cmd, GITS_CMD_MAPTI);
475 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
476 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
477 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
478 its_encode_collection(cmd, col->col_id);
480 its_fixup_cmd(cmd);
482 return valid_col(col);
485 static struct its_collection *its_build_movi_cmd(struct its_node *its,
486 struct its_cmd_block *cmd,
487 struct its_cmd_desc *desc)
489 struct its_collection *col;
491 col = dev_event_to_col(desc->its_movi_cmd.dev,
492 desc->its_movi_cmd.event_id);
494 its_encode_cmd(cmd, GITS_CMD_MOVI);
495 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
496 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
497 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
499 its_fixup_cmd(cmd);
501 return valid_col(col);
504 static struct its_collection *its_build_discard_cmd(struct its_node *its,
505 struct its_cmd_block *cmd,
506 struct its_cmd_desc *desc)
508 struct its_collection *col;
510 col = dev_event_to_col(desc->its_discard_cmd.dev,
511 desc->its_discard_cmd.event_id);
513 its_encode_cmd(cmd, GITS_CMD_DISCARD);
514 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
515 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
517 its_fixup_cmd(cmd);
519 return valid_col(col);
522 static struct its_collection *its_build_inv_cmd(struct its_node *its,
523 struct its_cmd_block *cmd,
524 struct its_cmd_desc *desc)
526 struct its_collection *col;
528 col = dev_event_to_col(desc->its_inv_cmd.dev,
529 desc->its_inv_cmd.event_id);
531 its_encode_cmd(cmd, GITS_CMD_INV);
532 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
533 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
535 its_fixup_cmd(cmd);
537 return valid_col(col);
540 static struct its_collection *its_build_int_cmd(struct its_node *its,
541 struct its_cmd_block *cmd,
542 struct its_cmd_desc *desc)
544 struct its_collection *col;
546 col = dev_event_to_col(desc->its_int_cmd.dev,
547 desc->its_int_cmd.event_id);
549 its_encode_cmd(cmd, GITS_CMD_INT);
550 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
551 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
553 its_fixup_cmd(cmd);
555 return valid_col(col);
558 static struct its_collection *its_build_clear_cmd(struct its_node *its,
559 struct its_cmd_block *cmd,
560 struct its_cmd_desc *desc)
562 struct its_collection *col;
564 col = dev_event_to_col(desc->its_clear_cmd.dev,
565 desc->its_clear_cmd.event_id);
567 its_encode_cmd(cmd, GITS_CMD_CLEAR);
568 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
569 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
571 its_fixup_cmd(cmd);
573 return valid_col(col);
576 static struct its_collection *its_build_invall_cmd(struct its_node *its,
577 struct its_cmd_block *cmd,
578 struct its_cmd_desc *desc)
580 its_encode_cmd(cmd, GITS_CMD_INVALL);
581 its_encode_collection(cmd, desc->its_invall_cmd.col->col_id);
583 its_fixup_cmd(cmd);
585 return NULL;
588 static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
589 struct its_cmd_block *cmd,
590 struct its_cmd_desc *desc)
592 its_encode_cmd(cmd, GITS_CMD_VINVALL);
593 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
595 its_fixup_cmd(cmd);
597 return valid_vpe(its, desc->its_vinvall_cmd.vpe);
600 static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
601 struct its_cmd_block *cmd,
602 struct its_cmd_desc *desc)
604 unsigned long vpt_addr;
605 u64 target;
607 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
608 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
610 its_encode_cmd(cmd, GITS_CMD_VMAPP);
611 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
612 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
613 its_encode_target(cmd, target);
614 its_encode_vpt_addr(cmd, vpt_addr);
615 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
617 its_fixup_cmd(cmd);
619 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
622 static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
623 struct its_cmd_block *cmd,
624 struct its_cmd_desc *desc)
626 u32 db;
628 if (desc->its_vmapti_cmd.db_enabled)
629 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
630 else
631 db = 1023;
633 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
634 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
635 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
636 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
637 its_encode_db_phys_id(cmd, db);
638 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
640 its_fixup_cmd(cmd);
642 return valid_vpe(its, desc->its_vmapti_cmd.vpe);
645 static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
646 struct its_cmd_block *cmd,
647 struct its_cmd_desc *desc)
649 u32 db;
651 if (desc->its_vmovi_cmd.db_enabled)
652 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
653 else
654 db = 1023;
656 its_encode_cmd(cmd, GITS_CMD_VMOVI);
657 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
658 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
659 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
660 its_encode_db_phys_id(cmd, db);
661 its_encode_db_valid(cmd, true);
663 its_fixup_cmd(cmd);
665 return valid_vpe(its, desc->its_vmovi_cmd.vpe);
668 static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
669 struct its_cmd_block *cmd,
670 struct its_cmd_desc *desc)
672 u64 target;
674 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
675 its_encode_cmd(cmd, GITS_CMD_VMOVP);
676 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
677 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
678 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
679 its_encode_target(cmd, target);
681 its_fixup_cmd(cmd);
683 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
686 static u64 its_cmd_ptr_to_offset(struct its_node *its,
687 struct its_cmd_block *ptr)
689 return (ptr - its->cmd_base) * sizeof(*ptr);
692 static int its_queue_full(struct its_node *its)
694 int widx;
695 int ridx;
697 widx = its->cmd_write - its->cmd_base;
698 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
700 /* This is incredibly unlikely to happen, unless the ITS locks up. */
701 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
702 return 1;
704 return 0;
707 static struct its_cmd_block *its_allocate_entry(struct its_node *its)
709 struct its_cmd_block *cmd;
710 u32 count = 1000000; /* 1s! */
712 while (its_queue_full(its)) {
713 count--;
714 if (!count) {
715 pr_err_ratelimited("ITS queue not draining\n");
716 return NULL;
718 cpu_relax();
719 udelay(1);
722 cmd = its->cmd_write++;
724 /* Handle queue wrapping */
725 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
726 its->cmd_write = its->cmd_base;
728 /* Clear command */
729 cmd->raw_cmd[0] = 0;
730 cmd->raw_cmd[1] = 0;
731 cmd->raw_cmd[2] = 0;
732 cmd->raw_cmd[3] = 0;
734 return cmd;
737 static struct its_cmd_block *its_post_commands(struct its_node *its)
739 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
741 writel_relaxed(wr, its->base + GITS_CWRITER);
743 return its->cmd_write;
746 static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
749 * Make sure the commands written to memory are observable by
750 * the ITS.
752 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
753 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
754 else
755 dsb(ishst);
758 static int its_wait_for_range_completion(struct its_node *its,
759 u64 prev_idx,
760 struct its_cmd_block *to)
762 u64 rd_idx, to_idx, linear_idx;
763 u32 count = 1000000; /* 1s! */
765 /* Linearize to_idx if the command set has wrapped around */
766 to_idx = its_cmd_ptr_to_offset(its, to);
767 if (to_idx < prev_idx)
768 to_idx += ITS_CMD_QUEUE_SZ;
770 linear_idx = prev_idx;
772 while (1) {
773 s64 delta;
775 rd_idx = readl_relaxed(its->base + GITS_CREADR);
778 * Compute the read pointer progress, taking the
779 * potential wrap-around into account.
781 delta = rd_idx - prev_idx;
782 if (rd_idx < prev_idx)
783 delta += ITS_CMD_QUEUE_SZ;
785 linear_idx += delta;
786 if (linear_idx >= to_idx)
787 break;
789 count--;
790 if (!count) {
791 pr_err_ratelimited("ITS queue timeout (%llu %llu)\n",
792 to_idx, linear_idx);
793 return -1;
795 prev_idx = rd_idx;
796 cpu_relax();
797 udelay(1);
800 return 0;
803 /* Warning, macro hell follows */
804 #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
805 void name(struct its_node *its, \
806 buildtype builder, \
807 struct its_cmd_desc *desc) \
809 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
810 synctype *sync_obj; \
811 unsigned long flags; \
812 u64 rd_idx; \
814 raw_spin_lock_irqsave(&its->lock, flags); \
816 cmd = its_allocate_entry(its); \
817 if (!cmd) { /* We're soooooo screewed... */ \
818 raw_spin_unlock_irqrestore(&its->lock, flags); \
819 return; \
821 sync_obj = builder(its, cmd, desc); \
822 its_flush_cmd(its, cmd); \
824 if (sync_obj) { \
825 sync_cmd = its_allocate_entry(its); \
826 if (!sync_cmd) \
827 goto post; \
829 buildfn(its, sync_cmd, sync_obj); \
830 its_flush_cmd(its, sync_cmd); \
833 post: \
834 rd_idx = readl_relaxed(its->base + GITS_CREADR); \
835 next_cmd = its_post_commands(its); \
836 raw_spin_unlock_irqrestore(&its->lock, flags); \
838 if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \
839 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
842 static void its_build_sync_cmd(struct its_node *its,
843 struct its_cmd_block *sync_cmd,
844 struct its_collection *sync_col)
846 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
847 its_encode_target(sync_cmd, sync_col->target_address);
849 its_fixup_cmd(sync_cmd);
852 static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
853 struct its_collection, its_build_sync_cmd)
855 static void its_build_vsync_cmd(struct its_node *its,
856 struct its_cmd_block *sync_cmd,
857 struct its_vpe *sync_vpe)
859 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
860 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
862 its_fixup_cmd(sync_cmd);
865 static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
866 struct its_vpe, its_build_vsync_cmd)
868 static void its_send_int(struct its_device *dev, u32 event_id)
870 struct its_cmd_desc desc;
872 desc.its_int_cmd.dev = dev;
873 desc.its_int_cmd.event_id = event_id;
875 its_send_single_command(dev->its, its_build_int_cmd, &desc);
878 static void its_send_clear(struct its_device *dev, u32 event_id)
880 struct its_cmd_desc desc;
882 desc.its_clear_cmd.dev = dev;
883 desc.its_clear_cmd.event_id = event_id;
885 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
888 static void its_send_inv(struct its_device *dev, u32 event_id)
890 struct its_cmd_desc desc;
892 desc.its_inv_cmd.dev = dev;
893 desc.its_inv_cmd.event_id = event_id;
895 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
898 static void its_send_mapd(struct its_device *dev, int valid)
900 struct its_cmd_desc desc;
902 desc.its_mapd_cmd.dev = dev;
903 desc.its_mapd_cmd.valid = !!valid;
905 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
908 static void its_send_mapc(struct its_node *its, struct its_collection *col,
909 int valid)
911 struct its_cmd_desc desc;
913 desc.its_mapc_cmd.col = col;
914 desc.its_mapc_cmd.valid = !!valid;
916 its_send_single_command(its, its_build_mapc_cmd, &desc);
919 static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
921 struct its_cmd_desc desc;
923 desc.its_mapti_cmd.dev = dev;
924 desc.its_mapti_cmd.phys_id = irq_id;
925 desc.its_mapti_cmd.event_id = id;
927 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
930 static void its_send_movi(struct its_device *dev,
931 struct its_collection *col, u32 id)
933 struct its_cmd_desc desc;
935 desc.its_movi_cmd.dev = dev;
936 desc.its_movi_cmd.col = col;
937 desc.its_movi_cmd.event_id = id;
939 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
942 static void its_send_discard(struct its_device *dev, u32 id)
944 struct its_cmd_desc desc;
946 desc.its_discard_cmd.dev = dev;
947 desc.its_discard_cmd.event_id = id;
949 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
952 static void its_send_invall(struct its_node *its, struct its_collection *col)
954 struct its_cmd_desc desc;
956 desc.its_invall_cmd.col = col;
958 its_send_single_command(its, its_build_invall_cmd, &desc);
961 static void its_send_vmapti(struct its_device *dev, u32 id)
963 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
964 struct its_cmd_desc desc;
966 desc.its_vmapti_cmd.vpe = map->vpe;
967 desc.its_vmapti_cmd.dev = dev;
968 desc.its_vmapti_cmd.virt_id = map->vintid;
969 desc.its_vmapti_cmd.event_id = id;
970 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
972 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
975 static void its_send_vmovi(struct its_device *dev, u32 id)
977 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
978 struct its_cmd_desc desc;
980 desc.its_vmovi_cmd.vpe = map->vpe;
981 desc.its_vmovi_cmd.dev = dev;
982 desc.its_vmovi_cmd.event_id = id;
983 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
985 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
988 static void its_send_vmapp(struct its_node *its,
989 struct its_vpe *vpe, bool valid)
991 struct its_cmd_desc desc;
993 desc.its_vmapp_cmd.vpe = vpe;
994 desc.its_vmapp_cmd.valid = valid;
995 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
997 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
1000 static void its_send_vmovp(struct its_vpe *vpe)
1002 struct its_cmd_desc desc = {};
1003 struct its_node *its;
1004 unsigned long flags;
1005 int col_id = vpe->col_idx;
1007 desc.its_vmovp_cmd.vpe = vpe;
1009 if (!its_list_map) {
1010 its = list_first_entry(&its_nodes, struct its_node, entry);
1011 desc.its_vmovp_cmd.col = &its->collections[col_id];
1012 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1013 return;
1017 * Yet another marvel of the architecture. If using the
1018 * its_list "feature", we need to make sure that all ITSs
1019 * receive all VMOVP commands in the same order. The only way
1020 * to guarantee this is to make vmovp a serialization point.
1022 * Wall <-- Head.
1024 raw_spin_lock_irqsave(&vmovp_lock, flags);
1026 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
1027 desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm);
1029 /* Emit VMOVPs */
1030 list_for_each_entry(its, &its_nodes, entry) {
1031 if (!its->is_v4)
1032 continue;
1034 if (!vpe->its_vm->vlpi_count[its->list_nr])
1035 continue;
1037 desc.its_vmovp_cmd.col = &its->collections[col_id];
1038 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1041 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1044 static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
1046 struct its_cmd_desc desc;
1048 desc.its_vinvall_cmd.vpe = vpe;
1049 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
1053 * irqchip functions - assumes MSI, mostly.
1056 static inline u32 its_get_event_id(struct irq_data *d)
1058 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1059 return d->hwirq - its_dev->event_map.lpi_base;
1062 static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
1064 irq_hw_number_t hwirq;
1065 struct page *prop_page;
1066 u8 *cfg;
1068 if (irqd_is_forwarded_to_vcpu(d)) {
1069 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1070 u32 event = its_get_event_id(d);
1071 struct its_vlpi_map *map;
1073 prop_page = its_dev->event_map.vm->vprop_page;
1074 map = &its_dev->event_map.vlpi_maps[event];
1075 hwirq = map->vintid;
1077 /* Remember the updated property */
1078 map->properties &= ~clr;
1079 map->properties |= set | LPI_PROP_GROUP1;
1080 } else {
1081 prop_page = gic_rdists->prop_page;
1082 hwirq = d->hwirq;
1085 cfg = page_address(prop_page) + hwirq - 8192;
1086 *cfg &= ~clr;
1087 *cfg |= set | LPI_PROP_GROUP1;
1090 * Make the above write visible to the redistributors.
1091 * And yes, we're flushing exactly: One. Single. Byte.
1092 * Humpf...
1094 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
1095 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
1096 else
1097 dsb(ishst);
1100 static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1102 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1104 lpi_write_config(d, clr, set);
1105 its_send_inv(its_dev, its_get_event_id(d));
1108 static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1110 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1111 u32 event = its_get_event_id(d);
1113 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1114 return;
1116 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1119 * More fun with the architecture:
1121 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1122 * value or to 1023, depending on the enable bit. But that
1123 * would be issueing a mapping for an /existing/ DevID+EventID
1124 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1125 * to the /same/ vPE, using this opportunity to adjust the
1126 * doorbell. Mouahahahaha. We loves it, Precious.
1128 its_send_vmovi(its_dev, event);
1131 static void its_mask_irq(struct irq_data *d)
1133 if (irqd_is_forwarded_to_vcpu(d))
1134 its_vlpi_set_doorbell(d, false);
1136 lpi_update_config(d, LPI_PROP_ENABLED, 0);
1139 static void its_unmask_irq(struct irq_data *d)
1141 if (irqd_is_forwarded_to_vcpu(d))
1142 its_vlpi_set_doorbell(d, true);
1144 lpi_update_config(d, 0, LPI_PROP_ENABLED);
1147 static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1148 bool force)
1150 unsigned int cpu;
1151 const struct cpumask *cpu_mask = cpu_online_mask;
1152 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1153 struct its_collection *target_col;
1154 u32 id = its_get_event_id(d);
1156 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1157 if (irqd_is_forwarded_to_vcpu(d))
1158 return -EINVAL;
1160 /* lpi cannot be routed to a redistributor that is on a foreign node */
1161 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1162 if (its_dev->its->numa_node >= 0) {
1163 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1164 if (!cpumask_intersects(mask_val, cpu_mask))
1165 return -EINVAL;
1169 cpu = cpumask_any_and(mask_val, cpu_mask);
1171 if (cpu >= nr_cpu_ids)
1172 return -EINVAL;
1174 /* don't set the affinity when the target cpu is same as current one */
1175 if (cpu != its_dev->event_map.col_map[id]) {
1176 target_col = &its_dev->its->collections[cpu];
1177 its_send_movi(its_dev, target_col, id);
1178 its_dev->event_map.col_map[id] = cpu;
1179 irq_data_update_effective_affinity(d, cpumask_of(cpu));
1182 return IRQ_SET_MASK_OK_DONE;
1185 static u64 its_irq_get_msi_base(struct its_device *its_dev)
1187 struct its_node *its = its_dev->its;
1189 return its->phys_base + GITS_TRANSLATER;
1192 static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1194 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1195 struct its_node *its;
1196 u64 addr;
1198 its = its_dev->its;
1199 addr = its->get_msi_base(its_dev);
1201 msg->address_lo = lower_32_bits(addr);
1202 msg->address_hi = upper_32_bits(addr);
1203 msg->data = its_get_event_id(d);
1205 iommu_dma_map_msi_msg(d->irq, msg);
1208 static int its_irq_set_irqchip_state(struct irq_data *d,
1209 enum irqchip_irq_state which,
1210 bool state)
1212 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1213 u32 event = its_get_event_id(d);
1215 if (which != IRQCHIP_STATE_PENDING)
1216 return -EINVAL;
1218 if (state)
1219 its_send_int(its_dev, event);
1220 else
1221 its_send_clear(its_dev, event);
1223 return 0;
1226 static void its_map_vm(struct its_node *its, struct its_vm *vm)
1228 unsigned long flags;
1230 /* Not using the ITS list? Everything is always mapped. */
1231 if (!its_list_map)
1232 return;
1234 raw_spin_lock_irqsave(&vmovp_lock, flags);
1237 * If the VM wasn't mapped yet, iterate over the vpes and get
1238 * them mapped now.
1240 vm->vlpi_count[its->list_nr]++;
1242 if (vm->vlpi_count[its->list_nr] == 1) {
1243 int i;
1245 for (i = 0; i < vm->nr_vpes; i++) {
1246 struct its_vpe *vpe = vm->vpes[i];
1247 struct irq_data *d = irq_get_irq_data(vpe->irq);
1249 /* Map the VPE to the first possible CPU */
1250 vpe->col_idx = cpumask_first(cpu_online_mask);
1251 its_send_vmapp(its, vpe, true);
1252 its_send_vinvall(its, vpe);
1253 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
1257 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1260 static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1262 unsigned long flags;
1264 /* Not using the ITS list? Everything is always mapped. */
1265 if (!its_list_map)
1266 return;
1268 raw_spin_lock_irqsave(&vmovp_lock, flags);
1270 if (!--vm->vlpi_count[its->list_nr]) {
1271 int i;
1273 for (i = 0; i < vm->nr_vpes; i++)
1274 its_send_vmapp(its, vm->vpes[i], false);
1277 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1280 static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1282 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1283 u32 event = its_get_event_id(d);
1284 int ret = 0;
1286 if (!info->map)
1287 return -EINVAL;
1289 mutex_lock(&its_dev->event_map.vlpi_lock);
1291 if (!its_dev->event_map.vm) {
1292 struct its_vlpi_map *maps;
1294 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
1295 GFP_KERNEL);
1296 if (!maps) {
1297 ret = -ENOMEM;
1298 goto out;
1301 its_dev->event_map.vm = info->map->vm;
1302 its_dev->event_map.vlpi_maps = maps;
1303 } else if (its_dev->event_map.vm != info->map->vm) {
1304 ret = -EINVAL;
1305 goto out;
1308 /* Get our private copy of the mapping information */
1309 its_dev->event_map.vlpi_maps[event] = *info->map;
1311 if (irqd_is_forwarded_to_vcpu(d)) {
1312 /* Already mapped, move it around */
1313 its_send_vmovi(its_dev, event);
1314 } else {
1315 /* Ensure all the VPEs are mapped on this ITS */
1316 its_map_vm(its_dev->its, info->map->vm);
1319 * Flag the interrupt as forwarded so that we can
1320 * start poking the virtual property table.
1322 irqd_set_forwarded_to_vcpu(d);
1324 /* Write out the property to the prop table */
1325 lpi_write_config(d, 0xff, info->map->properties);
1327 /* Drop the physical mapping */
1328 its_send_discard(its_dev, event);
1330 /* and install the virtual one */
1331 its_send_vmapti(its_dev, event);
1333 /* Increment the number of VLPIs */
1334 its_dev->event_map.nr_vlpis++;
1337 out:
1338 mutex_unlock(&its_dev->event_map.vlpi_lock);
1339 return ret;
1342 static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1344 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1345 u32 event = its_get_event_id(d);
1346 int ret = 0;
1348 mutex_lock(&its_dev->event_map.vlpi_lock);
1350 if (!its_dev->event_map.vm ||
1351 !its_dev->event_map.vlpi_maps[event].vm) {
1352 ret = -EINVAL;
1353 goto out;
1356 /* Copy our mapping information to the incoming request */
1357 *info->map = its_dev->event_map.vlpi_maps[event];
1359 out:
1360 mutex_unlock(&its_dev->event_map.vlpi_lock);
1361 return ret;
1364 static int its_vlpi_unmap(struct irq_data *d)
1366 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1367 u32 event = its_get_event_id(d);
1368 int ret = 0;
1370 mutex_lock(&its_dev->event_map.vlpi_lock);
1372 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1373 ret = -EINVAL;
1374 goto out;
1377 /* Drop the virtual mapping */
1378 its_send_discard(its_dev, event);
1380 /* and restore the physical one */
1381 irqd_clr_forwarded_to_vcpu(d);
1382 its_send_mapti(its_dev, d->hwirq, event);
1383 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1384 LPI_PROP_ENABLED |
1385 LPI_PROP_GROUP1));
1387 /* Potentially unmap the VM from this ITS */
1388 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1391 * Drop the refcount and make the device available again if
1392 * this was the last VLPI.
1394 if (!--its_dev->event_map.nr_vlpis) {
1395 its_dev->event_map.vm = NULL;
1396 kfree(its_dev->event_map.vlpi_maps);
1399 out:
1400 mutex_unlock(&its_dev->event_map.vlpi_lock);
1401 return ret;
1404 static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1406 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1408 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1409 return -EINVAL;
1411 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1412 lpi_update_config(d, 0xff, info->config);
1413 else
1414 lpi_write_config(d, 0xff, info->config);
1415 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1417 return 0;
1420 static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1422 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1423 struct its_cmd_info *info = vcpu_info;
1425 /* Need a v4 ITS */
1426 if (!its_dev->its->is_v4)
1427 return -EINVAL;
1429 /* Unmap request? */
1430 if (!info)
1431 return its_vlpi_unmap(d);
1433 switch (info->cmd_type) {
1434 case MAP_VLPI:
1435 return its_vlpi_map(d, info);
1437 case GET_VLPI:
1438 return its_vlpi_get(d, info);
1440 case PROP_UPDATE_VLPI:
1441 case PROP_UPDATE_AND_INV_VLPI:
1442 return its_vlpi_prop_update(d, info);
1444 default:
1445 return -EINVAL;
1449 static struct irq_chip its_irq_chip = {
1450 .name = "ITS",
1451 .irq_mask = its_mask_irq,
1452 .irq_unmask = its_unmask_irq,
1453 .irq_eoi = irq_chip_eoi_parent,
1454 .irq_set_affinity = its_set_affinity,
1455 .irq_compose_msi_msg = its_irq_compose_msi_msg,
1456 .irq_set_irqchip_state = its_irq_set_irqchip_state,
1457 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
1462 * How we allocate LPIs:
1464 * lpi_range_list contains ranges of LPIs that are to available to
1465 * allocate from. To allocate LPIs, just pick the first range that
1466 * fits the required allocation, and reduce it by the required
1467 * amount. Once empty, remove the range from the list.
1469 * To free a range of LPIs, add a free range to the list, sort it and
1470 * merge the result if the new range happens to be adjacent to an
1471 * already free block.
1473 * The consequence of the above is that allocation is cost is low, but
1474 * freeing is expensive. We assumes that freeing rarely occurs.
1476 #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
1478 static DEFINE_MUTEX(lpi_range_lock);
1479 static LIST_HEAD(lpi_range_list);
1481 struct lpi_range {
1482 struct list_head entry;
1483 u32 base_id;
1484 u32 span;
1487 static struct lpi_range *mk_lpi_range(u32 base, u32 span)
1489 struct lpi_range *range;
1491 range = kzalloc(sizeof(*range), GFP_KERNEL);
1492 if (range) {
1493 INIT_LIST_HEAD(&range->entry);
1494 range->base_id = base;
1495 range->span = span;
1498 return range;
1501 static int lpi_range_cmp(void *priv, struct list_head *a, struct list_head *b)
1503 struct lpi_range *ra, *rb;
1505 ra = container_of(a, struct lpi_range, entry);
1506 rb = container_of(b, struct lpi_range, entry);
1508 return ra->base_id - rb->base_id;
1511 static void merge_lpi_ranges(void)
1513 struct lpi_range *range, *tmp;
1515 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1516 if (!list_is_last(&range->entry, &lpi_range_list) &&
1517 (tmp->base_id == (range->base_id + range->span))) {
1518 tmp->base_id = range->base_id;
1519 tmp->span += range->span;
1520 list_del(&range->entry);
1521 kfree(range);
1526 static int alloc_lpi_range(u32 nr_lpis, u32 *base)
1528 struct lpi_range *range, *tmp;
1529 int err = -ENOSPC;
1531 mutex_lock(&lpi_range_lock);
1533 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1534 if (range->span >= nr_lpis) {
1535 *base = range->base_id;
1536 range->base_id += nr_lpis;
1537 range->span -= nr_lpis;
1539 if (range->span == 0) {
1540 list_del(&range->entry);
1541 kfree(range);
1544 err = 0;
1545 break;
1549 mutex_unlock(&lpi_range_lock);
1551 pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
1552 return err;
1555 static int free_lpi_range(u32 base, u32 nr_lpis)
1557 struct lpi_range *new;
1558 int err = 0;
1560 mutex_lock(&lpi_range_lock);
1562 new = mk_lpi_range(base, nr_lpis);
1563 if (!new) {
1564 err = -ENOMEM;
1565 goto out;
1568 list_add(&new->entry, &lpi_range_list);
1569 list_sort(NULL, &lpi_range_list, lpi_range_cmp);
1570 merge_lpi_ranges();
1571 out:
1572 mutex_unlock(&lpi_range_lock);
1573 return err;
1576 static int __init its_lpi_init(u32 id_bits)
1578 u32 lpis = (1UL << id_bits) - 8192;
1579 u32 numlpis;
1580 int err;
1582 numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
1584 if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
1585 lpis = numlpis;
1586 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
1587 lpis);
1591 * Initializing the allocator is just the same as freeing the
1592 * full range of LPIs.
1594 err = free_lpi_range(8192, lpis);
1595 pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
1596 return err;
1599 static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
1601 unsigned long *bitmap = NULL;
1602 int err = 0;
1604 do {
1605 err = alloc_lpi_range(nr_irqs, base);
1606 if (!err)
1607 break;
1609 nr_irqs /= 2;
1610 } while (nr_irqs > 0);
1612 if (!nr_irqs)
1613 err = -ENOSPC;
1615 if (err)
1616 goto out;
1618 bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC);
1619 if (!bitmap)
1620 goto out;
1622 *nr_ids = nr_irqs;
1624 out:
1625 if (!bitmap)
1626 *base = *nr_ids = 0;
1628 return bitmap;
1631 static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
1633 WARN_ON(free_lpi_range(base, nr_ids));
1634 kfree(bitmap);
1637 static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1639 struct page *prop_page;
1641 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1642 if (!prop_page)
1643 return NULL;
1645 /* Priority 0xa0, Group-1, disabled */
1646 memset(page_address(prop_page),
1647 LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1,
1648 LPI_PROPBASE_SZ);
1650 /* Make sure the GIC will observe the written configuration */
1651 gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ);
1653 return prop_page;
1656 static void its_free_prop_table(struct page *prop_page)
1658 free_pages((unsigned long)page_address(prop_page),
1659 get_order(LPI_PROPBASE_SZ));
1662 static int __init its_alloc_lpi_tables(void)
1664 phys_addr_t paddr;
1666 lpi_id_bits = min_t(u32, GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
1667 ITS_MAX_LPI_NRBITS);
1668 gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT);
1669 if (!gic_rdists->prop_page) {
1670 pr_err("Failed to allocate PROPBASE\n");
1671 return -ENOMEM;
1674 paddr = page_to_phys(gic_rdists->prop_page);
1675 pr_info("GIC: using LPI property table @%pa\n", &paddr);
1677 return its_lpi_init(lpi_id_bits);
1680 static const char *its_base_type_string[] = {
1681 [GITS_BASER_TYPE_DEVICE] = "Devices",
1682 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
1683 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
1684 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1685 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1686 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1687 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1690 static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1692 u32 idx = baser - its->tables;
1694 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
1697 static void its_write_baser(struct its_node *its, struct its_baser *baser,
1698 u64 val)
1700 u32 idx = baser - its->tables;
1702 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
1703 baser->val = its_read_baser(its, baser);
1706 static int its_setup_baser(struct its_node *its, struct its_baser *baser,
1707 u64 cache, u64 shr, u32 psz, u32 order,
1708 bool indirect)
1710 u64 val = its_read_baser(its, baser);
1711 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1712 u64 type = GITS_BASER_TYPE(val);
1713 u64 baser_phys, tmp;
1714 u32 alloc_pages;
1715 void *base;
1717 retry_alloc_baser:
1718 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1719 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1720 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1721 &its->phys_base, its_base_type_string[type],
1722 alloc_pages, GITS_BASER_PAGES_MAX);
1723 alloc_pages = GITS_BASER_PAGES_MAX;
1724 order = get_order(GITS_BASER_PAGES_MAX * psz);
1727 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1728 if (!base)
1729 return -ENOMEM;
1731 baser_phys = virt_to_phys(base);
1733 /* Check if the physical address of the memory is above 48bits */
1734 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
1736 /* 52bit PA is supported only when PageSize=64K */
1737 if (psz != SZ_64K) {
1738 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
1739 free_pages((unsigned long)base, order);
1740 return -ENXIO;
1743 /* Convert 52bit PA to 48bit field */
1744 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
1747 retry_baser:
1748 val = (baser_phys |
1749 (type << GITS_BASER_TYPE_SHIFT) |
1750 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1751 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1752 cache |
1753 shr |
1754 GITS_BASER_VALID);
1756 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1758 switch (psz) {
1759 case SZ_4K:
1760 val |= GITS_BASER_PAGE_SIZE_4K;
1761 break;
1762 case SZ_16K:
1763 val |= GITS_BASER_PAGE_SIZE_16K;
1764 break;
1765 case SZ_64K:
1766 val |= GITS_BASER_PAGE_SIZE_64K;
1767 break;
1770 its_write_baser(its, baser, val);
1771 tmp = baser->val;
1773 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1775 * Shareability didn't stick. Just use
1776 * whatever the read reported, which is likely
1777 * to be the only thing this redistributor
1778 * supports. If that's zero, make it
1779 * non-cacheable as well.
1781 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1782 if (!shr) {
1783 cache = GITS_BASER_nC;
1784 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
1786 goto retry_baser;
1789 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1791 * Page size didn't stick. Let's try a smaller
1792 * size and retry. If we reach 4K, then
1793 * something is horribly wrong...
1795 free_pages((unsigned long)base, order);
1796 baser->base = NULL;
1798 switch (psz) {
1799 case SZ_16K:
1800 psz = SZ_4K;
1801 goto retry_alloc_baser;
1802 case SZ_64K:
1803 psz = SZ_16K;
1804 goto retry_alloc_baser;
1808 if (val != tmp) {
1809 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
1810 &its->phys_base, its_base_type_string[type],
1811 val, tmp);
1812 free_pages((unsigned long)base, order);
1813 return -ENXIO;
1816 baser->order = order;
1817 baser->base = base;
1818 baser->psz = psz;
1819 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
1821 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
1822 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
1823 its_base_type_string[type],
1824 (unsigned long)virt_to_phys(base),
1825 indirect ? "indirect" : "flat", (int)esz,
1826 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1828 return 0;
1831 static bool its_parse_indirect_baser(struct its_node *its,
1832 struct its_baser *baser,
1833 u32 psz, u32 *order, u32 ids)
1835 u64 tmp = its_read_baser(its, baser);
1836 u64 type = GITS_BASER_TYPE(tmp);
1837 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
1838 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
1839 u32 new_order = *order;
1840 bool indirect = false;
1842 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1843 if ((esz << ids) > (psz * 2)) {
1845 * Find out whether hw supports a single or two-level table by
1846 * table by reading bit at offset '62' after writing '1' to it.
1848 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1849 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1851 if (indirect) {
1853 * The size of the lvl2 table is equal to ITS page size
1854 * which is 'psz'. For computing lvl1 table size,
1855 * subtract ID bits that sparse lvl2 table from 'ids'
1856 * which is reported by ITS hardware times lvl1 table
1857 * entry size.
1859 ids -= ilog2(psz / (int)esz);
1860 esz = GITS_LVL1_ENTRY_SIZE;
1865 * Allocate as many entries as required to fit the
1866 * range of device IDs that the ITS can grok... The ID
1867 * space being incredibly sparse, this results in a
1868 * massive waste of memory if two-level device table
1869 * feature is not supported by hardware.
1871 new_order = max_t(u32, get_order(esz << ids), new_order);
1872 if (new_order >= MAX_ORDER) {
1873 new_order = MAX_ORDER - 1;
1874 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
1875 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1876 &its->phys_base, its_base_type_string[type],
1877 its->device_ids, ids);
1880 *order = new_order;
1882 return indirect;
1885 static void its_free_tables(struct its_node *its)
1887 int i;
1889 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1890 if (its->tables[i].base) {
1891 free_pages((unsigned long)its->tables[i].base,
1892 its->tables[i].order);
1893 its->tables[i].base = NULL;
1898 static int its_alloc_tables(struct its_node *its)
1900 u64 shr = GITS_BASER_InnerShareable;
1901 u64 cache = GITS_BASER_RaWaWb;
1902 u32 psz = SZ_64K;
1903 int err, i;
1905 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
1906 /* erratum 24313: ignore memory access type */
1907 cache = GITS_BASER_nCnB;
1909 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1910 struct its_baser *baser = its->tables + i;
1911 u64 val = its_read_baser(its, baser);
1912 u64 type = GITS_BASER_TYPE(val);
1913 u32 order = get_order(psz);
1914 bool indirect = false;
1916 switch (type) {
1917 case GITS_BASER_TYPE_NONE:
1918 continue;
1920 case GITS_BASER_TYPE_DEVICE:
1921 indirect = its_parse_indirect_baser(its, baser,
1922 psz, &order,
1923 its->device_ids);
1924 break;
1926 case GITS_BASER_TYPE_VCPU:
1927 indirect = its_parse_indirect_baser(its, baser,
1928 psz, &order,
1929 ITS_MAX_VPEID_BITS);
1930 break;
1933 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
1934 if (err < 0) {
1935 its_free_tables(its);
1936 return err;
1939 /* Update settings which will be used for next BASERn */
1940 psz = baser->psz;
1941 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1942 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
1945 return 0;
1948 static int its_alloc_collections(struct its_node *its)
1950 int i;
1952 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
1953 GFP_KERNEL);
1954 if (!its->collections)
1955 return -ENOMEM;
1957 for (i = 0; i < nr_cpu_ids; i++)
1958 its->collections[i].target_address = ~0ULL;
1960 return 0;
1963 static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1965 struct page *pend_page;
1967 * The pending pages have to be at least 64kB aligned,
1968 * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below.
1970 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
1971 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1972 if (!pend_page)
1973 return NULL;
1975 /* Make sure the GIC will observe the zero-ed page */
1976 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1978 return pend_page;
1981 static void its_free_pending_table(struct page *pt)
1983 free_pages((unsigned long)page_address(pt),
1984 get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K)));
1987 static u64 its_clear_vpend_valid(void __iomem *vlpi_base)
1989 u32 count = 1000000; /* 1s! */
1990 bool clean;
1991 u64 val;
1993 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
1994 val &= ~GICR_VPENDBASER_Valid;
1995 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
1997 do {
1998 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
1999 clean = !(val & GICR_VPENDBASER_Dirty);
2000 if (!clean) {
2001 count--;
2002 cpu_relax();
2003 udelay(1);
2005 } while (!clean && count);
2007 return val;
2010 static void its_cpu_init_lpis(void)
2012 void __iomem *rbase = gic_data_rdist_rd_base();
2013 struct page *pend_page;
2014 u64 val, tmp;
2016 /* If we didn't allocate the pending table yet, do it now */
2017 pend_page = gic_data_rdist()->pend_page;
2018 if (!pend_page) {
2019 phys_addr_t paddr;
2021 pend_page = its_allocate_pending_table(GFP_NOWAIT);
2022 if (!pend_page) {
2023 pr_err("Failed to allocate PENDBASE for CPU%d\n",
2024 smp_processor_id());
2025 return;
2028 paddr = page_to_phys(pend_page);
2029 pr_info("CPU%d: using LPI pending table @%pa\n",
2030 smp_processor_id(), &paddr);
2031 gic_data_rdist()->pend_page = pend_page;
2034 /* set PROPBASE */
2035 val = (page_to_phys(gic_rdists->prop_page) |
2036 GICR_PROPBASER_InnerShareable |
2037 GICR_PROPBASER_RaWaWb |
2038 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
2040 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2041 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
2043 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
2044 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
2046 * The HW reports non-shareable, we must
2047 * remove the cacheability attributes as
2048 * well.
2050 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
2051 GICR_PROPBASER_CACHEABILITY_MASK);
2052 val |= GICR_PROPBASER_nC;
2053 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2055 pr_info_once("GIC: using cache flushing for LPI property table\n");
2056 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
2059 /* set PENDBASE */
2060 val = (page_to_phys(pend_page) |
2061 GICR_PENDBASER_InnerShareable |
2062 GICR_PENDBASER_RaWaWb);
2064 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2065 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
2067 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
2069 * The HW reports non-shareable, we must remove the
2070 * cacheability attributes as well.
2072 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
2073 GICR_PENDBASER_CACHEABILITY_MASK);
2074 val |= GICR_PENDBASER_nC;
2075 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2078 /* Enable LPIs */
2079 val = readl_relaxed(rbase + GICR_CTLR);
2080 val |= GICR_CTLR_ENABLE_LPIS;
2081 writel_relaxed(val, rbase + GICR_CTLR);
2083 if (gic_rdists->has_vlpis) {
2084 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2087 * It's possible for CPU to receive VLPIs before it is
2088 * sheduled as a vPE, especially for the first CPU, and the
2089 * VLPI with INTID larger than 2^(IDbits+1) will be considered
2090 * as out of range and dropped by GIC.
2091 * So we initialize IDbits to known value to avoid VLPI drop.
2093 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2094 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
2095 smp_processor_id(), val);
2096 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2099 * Also clear Valid bit of GICR_VPENDBASER, in case some
2100 * ancient programming gets left in and has possibility of
2101 * corrupting memory.
2103 val = its_clear_vpend_valid(vlpi_base);
2104 WARN_ON(val & GICR_VPENDBASER_Dirty);
2107 /* Make sure the GIC has seen the above */
2108 dsb(sy);
2111 static void its_cpu_init_collection(struct its_node *its)
2113 int cpu = smp_processor_id();
2114 u64 target;
2116 /* avoid cross node collections and its mapping */
2117 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
2118 struct device_node *cpu_node;
2120 cpu_node = of_get_cpu_node(cpu, NULL);
2121 if (its->numa_node != NUMA_NO_NODE &&
2122 its->numa_node != of_node_to_nid(cpu_node))
2123 return;
2127 * We now have to bind each collection to its target
2128 * redistributor.
2130 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
2132 * This ITS wants the physical address of the
2133 * redistributor.
2135 target = gic_data_rdist()->phys_base;
2136 } else {
2137 /* This ITS wants a linear CPU number. */
2138 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2139 target = GICR_TYPER_CPU_NUMBER(target) << 16;
2142 /* Perform collection mapping */
2143 its->collections[cpu].target_address = target;
2144 its->collections[cpu].col_id = cpu;
2146 its_send_mapc(its, &its->collections[cpu], 1);
2147 its_send_invall(its, &its->collections[cpu]);
2150 static void its_cpu_init_collections(void)
2152 struct its_node *its;
2154 raw_spin_lock(&its_lock);
2156 list_for_each_entry(its, &its_nodes, entry)
2157 its_cpu_init_collection(its);
2159 raw_spin_unlock(&its_lock);
2162 static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
2164 struct its_device *its_dev = NULL, *tmp;
2165 unsigned long flags;
2167 raw_spin_lock_irqsave(&its->lock, flags);
2169 list_for_each_entry(tmp, &its->its_device_list, entry) {
2170 if (tmp->device_id == dev_id) {
2171 its_dev = tmp;
2172 break;
2176 raw_spin_unlock_irqrestore(&its->lock, flags);
2178 return its_dev;
2181 static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2183 int i;
2185 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2186 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2187 return &its->tables[i];
2190 return NULL;
2193 static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
2195 struct page *page;
2196 u32 esz, idx;
2197 __le64 *table;
2199 /* Don't allow device id that exceeds single, flat table limit */
2200 esz = GITS_BASER_ENTRY_SIZE(baser->val);
2201 if (!(baser->val & GITS_BASER_INDIRECT))
2202 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
2204 /* Compute 1st level table index & check if that exceeds table limit */
2205 idx = id >> ilog2(baser->psz / esz);
2206 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
2207 return false;
2209 table = baser->base;
2211 /* Allocate memory for 2nd level table */
2212 if (!table[idx]) {
2213 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
2214 if (!page)
2215 return false;
2217 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2218 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
2219 gic_flush_dcache_to_poc(page_address(page), baser->psz);
2221 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2223 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2224 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
2225 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
2227 /* Ensure updated table contents are visible to ITS hardware */
2228 dsb(sy);
2231 return true;
2234 static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
2236 struct its_baser *baser;
2238 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
2240 /* Don't allow device id that exceeds ITS hardware limit */
2241 if (!baser)
2242 return (ilog2(dev_id) < its->device_ids);
2244 return its_alloc_table_entry(baser, dev_id);
2247 static bool its_alloc_vpe_table(u32 vpe_id)
2249 struct its_node *its;
2252 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
2253 * could try and only do it on ITSs corresponding to devices
2254 * that have interrupts targeted at this VPE, but the
2255 * complexity becomes crazy (and you have tons of memory
2256 * anyway, right?).
2258 list_for_each_entry(its, &its_nodes, entry) {
2259 struct its_baser *baser;
2261 if (!its->is_v4)
2262 continue;
2264 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
2265 if (!baser)
2266 return false;
2268 if (!its_alloc_table_entry(baser, vpe_id))
2269 return false;
2272 return true;
2275 static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
2276 int nvecs, bool alloc_lpis)
2278 struct its_device *dev;
2279 unsigned long *lpi_map = NULL;
2280 unsigned long flags;
2281 u16 *col_map = NULL;
2282 void *itt;
2283 int lpi_base;
2284 int nr_lpis;
2285 int nr_ites;
2286 int sz;
2288 if (!its_alloc_device_table(its, dev_id))
2289 return NULL;
2291 if (WARN_ON(!is_power_of_2(nvecs)))
2292 nvecs = roundup_pow_of_two(nvecs);
2294 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2296 * Even if the device wants a single LPI, the ITT must be
2297 * sized as a power of two (and you need at least one bit...).
2299 nr_ites = max(2, nvecs);
2300 sz = nr_ites * its->ite_size;
2301 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
2302 itt = kzalloc(sz, GFP_KERNEL);
2303 if (alloc_lpis) {
2304 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
2305 if (lpi_map)
2306 col_map = kcalloc(nr_lpis, sizeof(*col_map),
2307 GFP_KERNEL);
2308 } else {
2309 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
2310 nr_lpis = 0;
2311 lpi_base = 0;
2314 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
2315 kfree(dev);
2316 kfree(itt);
2317 kfree(lpi_map);
2318 kfree(col_map);
2319 return NULL;
2322 gic_flush_dcache_to_poc(itt, sz);
2324 dev->its = its;
2325 dev->itt = itt;
2326 dev->nr_ites = nr_ites;
2327 dev->event_map.lpi_map = lpi_map;
2328 dev->event_map.col_map = col_map;
2329 dev->event_map.lpi_base = lpi_base;
2330 dev->event_map.nr_lpis = nr_lpis;
2331 mutex_init(&dev->event_map.vlpi_lock);
2332 dev->device_id = dev_id;
2333 INIT_LIST_HEAD(&dev->entry);
2335 raw_spin_lock_irqsave(&its->lock, flags);
2336 list_add(&dev->entry, &its->its_device_list);
2337 raw_spin_unlock_irqrestore(&its->lock, flags);
2339 /* Map device to its ITT */
2340 its_send_mapd(dev, 1);
2342 return dev;
2345 static void its_free_device(struct its_device *its_dev)
2347 unsigned long flags;
2349 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
2350 list_del(&its_dev->entry);
2351 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
2352 kfree(its_dev->itt);
2353 kfree(its_dev);
2356 static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
2358 int idx;
2360 idx = bitmap_find_free_region(dev->event_map.lpi_map,
2361 dev->event_map.nr_lpis,
2362 get_count_order(nvecs));
2363 if (idx < 0)
2364 return -ENOSPC;
2366 *hwirq = dev->event_map.lpi_base + idx;
2367 set_bit(idx, dev->event_map.lpi_map);
2369 return 0;
2372 static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2373 int nvec, msi_alloc_info_t *info)
2375 struct its_node *its;
2376 struct its_device *its_dev;
2377 struct msi_domain_info *msi_info;
2378 u32 dev_id;
2379 int err = 0;
2382 * We ignore "dev" entierely, and rely on the dev_id that has
2383 * been passed via the scratchpad. This limits this domain's
2384 * usefulness to upper layers that definitely know that they
2385 * are built on top of the ITS.
2387 dev_id = info->scratchpad[0].ul;
2389 msi_info = msi_get_domain_info(domain);
2390 its = msi_info->data;
2392 if (!gic_rdists->has_direct_lpi &&
2393 vpe_proxy.dev &&
2394 vpe_proxy.dev->its == its &&
2395 dev_id == vpe_proxy.dev->device_id) {
2396 /* Bad luck. Get yourself a better implementation */
2397 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
2398 dev_id);
2399 return -EINVAL;
2402 mutex_lock(&its->dev_alloc_lock);
2403 its_dev = its_find_device(its, dev_id);
2404 if (its_dev) {
2406 * We already have seen this ID, probably through
2407 * another alias (PCI bridge of some sort). No need to
2408 * create the device.
2410 its_dev->shared = true;
2411 pr_debug("Reusing ITT for devID %x\n", dev_id);
2412 goto out;
2415 its_dev = its_create_device(its, dev_id, nvec, true);
2416 if (!its_dev) {
2417 err = -ENOMEM;
2418 goto out;
2421 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
2422 out:
2423 mutex_unlock(&its->dev_alloc_lock);
2424 info->scratchpad[0].ptr = its_dev;
2425 return err;
2428 static struct msi_domain_ops its_msi_domain_ops = {
2429 .msi_prepare = its_msi_prepare,
2432 static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2433 unsigned int virq,
2434 irq_hw_number_t hwirq)
2436 struct irq_fwspec fwspec;
2438 if (irq_domain_get_of_node(domain->parent)) {
2439 fwspec.fwnode = domain->parent->fwnode;
2440 fwspec.param_count = 3;
2441 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2442 fwspec.param[1] = hwirq;
2443 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
2444 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2445 fwspec.fwnode = domain->parent->fwnode;
2446 fwspec.param_count = 2;
2447 fwspec.param[0] = hwirq;
2448 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
2449 } else {
2450 return -EINVAL;
2453 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
2456 static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2457 unsigned int nr_irqs, void *args)
2459 msi_alloc_info_t *info = args;
2460 struct its_device *its_dev = info->scratchpad[0].ptr;
2461 irq_hw_number_t hwirq;
2462 int err;
2463 int i;
2465 err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
2466 if (err)
2467 return err;
2469 for (i = 0; i < nr_irqs; i++) {
2470 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
2471 if (err)
2472 return err;
2474 irq_domain_set_hwirq_and_chip(domain, virq + i,
2475 hwirq + i, &its_irq_chip, its_dev);
2476 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
2477 pr_debug("ID:%d pID:%d vID:%d\n",
2478 (int)(hwirq + i - its_dev->event_map.lpi_base),
2479 (int)(hwirq + i), virq + i);
2482 return 0;
2485 static int its_irq_domain_activate(struct irq_domain *domain,
2486 struct irq_data *d, bool reserve)
2488 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2489 u32 event = its_get_event_id(d);
2490 const struct cpumask *cpu_mask = cpu_online_mask;
2491 int cpu;
2493 /* get the cpu_mask of local node */
2494 if (its_dev->its->numa_node >= 0)
2495 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
2497 /* Bind the LPI to the first possible CPU */
2498 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
2499 if (cpu >= nr_cpu_ids) {
2500 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
2501 return -EINVAL;
2503 cpu = cpumask_first(cpu_online_mask);
2506 its_dev->event_map.col_map[event] = cpu;
2507 irq_data_update_effective_affinity(d, cpumask_of(cpu));
2509 /* Map the GIC IRQ and event to the device */
2510 its_send_mapti(its_dev, d->hwirq, event);
2511 return 0;
2514 static void its_irq_domain_deactivate(struct irq_domain *domain,
2515 struct irq_data *d)
2517 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2518 u32 event = its_get_event_id(d);
2520 /* Stop the delivery of interrupts */
2521 its_send_discard(its_dev, event);
2524 static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2525 unsigned int nr_irqs)
2527 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2528 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2529 struct its_node *its = its_dev->its;
2530 int i;
2532 bitmap_release_region(its_dev->event_map.lpi_map,
2533 its_get_event_id(irq_domain_get_irq_data(domain, virq)),
2534 get_count_order(nr_irqs));
2536 for (i = 0; i < nr_irqs; i++) {
2537 struct irq_data *data = irq_domain_get_irq_data(domain,
2538 virq + i);
2539 /* Nuke the entry in the domain */
2540 irq_domain_reset_irq_data(data);
2543 mutex_lock(&its->dev_alloc_lock);
2546 * If all interrupts have been freed, start mopping the
2547 * floor. This is conditionned on the device not being shared.
2549 if (!its_dev->shared &&
2550 bitmap_empty(its_dev->event_map.lpi_map,
2551 its_dev->event_map.nr_lpis)) {
2552 its_lpi_free(its_dev->event_map.lpi_map,
2553 its_dev->event_map.lpi_base,
2554 its_dev->event_map.nr_lpis);
2555 kfree(its_dev->event_map.col_map);
2557 /* Unmap device/itt */
2558 its_send_mapd(its_dev, 0);
2559 its_free_device(its_dev);
2562 mutex_unlock(&its->dev_alloc_lock);
2564 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2567 static const struct irq_domain_ops its_domain_ops = {
2568 .alloc = its_irq_domain_alloc,
2569 .free = its_irq_domain_free,
2570 .activate = its_irq_domain_activate,
2571 .deactivate = its_irq_domain_deactivate,
2575 * This is insane.
2577 * If a GICv4 doesn't implement Direct LPIs (which is extremely
2578 * likely), the only way to perform an invalidate is to use a fake
2579 * device to issue an INV command, implying that the LPI has first
2580 * been mapped to some event on that device. Since this is not exactly
2581 * cheap, we try to keep that mapping around as long as possible, and
2582 * only issue an UNMAP if we're short on available slots.
2584 * Broken by design(tm).
2586 static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
2588 /* Already unmapped? */
2589 if (vpe->vpe_proxy_event == -1)
2590 return;
2592 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
2593 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
2596 * We don't track empty slots at all, so let's move the
2597 * next_victim pointer if we can quickly reuse that slot
2598 * instead of nuking an existing entry. Not clear that this is
2599 * always a win though, and this might just generate a ripple
2600 * effect... Let's just hope VPEs don't migrate too often.
2602 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2603 vpe_proxy.next_victim = vpe->vpe_proxy_event;
2605 vpe->vpe_proxy_event = -1;
2608 static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
2610 if (!gic_rdists->has_direct_lpi) {
2611 unsigned long flags;
2613 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2614 its_vpe_db_proxy_unmap_locked(vpe);
2615 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2619 static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
2621 /* Already mapped? */
2622 if (vpe->vpe_proxy_event != -1)
2623 return;
2625 /* This slot was already allocated. Kick the other VPE out. */
2626 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2627 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
2629 /* Map the new VPE instead */
2630 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
2631 vpe->vpe_proxy_event = vpe_proxy.next_victim;
2632 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
2634 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
2635 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
2638 static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
2640 unsigned long flags;
2641 struct its_collection *target_col;
2643 if (gic_rdists->has_direct_lpi) {
2644 void __iomem *rdbase;
2646 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
2647 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2648 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2649 cpu_relax();
2651 return;
2654 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2656 its_vpe_db_proxy_map_locked(vpe);
2658 target_col = &vpe_proxy.dev->its->collections[to];
2659 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
2660 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
2662 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2665 static int its_vpe_set_affinity(struct irq_data *d,
2666 const struct cpumask *mask_val,
2667 bool force)
2669 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2670 int cpu = cpumask_first(mask_val);
2673 * Changing affinity is mega expensive, so let's be as lazy as
2674 * we can and only do it if we really have to. Also, if mapped
2675 * into the proxy device, we need to move the doorbell
2676 * interrupt to its new location.
2678 if (vpe->col_idx != cpu) {
2679 int from = vpe->col_idx;
2681 vpe->col_idx = cpu;
2682 its_send_vmovp(vpe);
2683 its_vpe_db_proxy_move(vpe, from, cpu);
2686 irq_data_update_effective_affinity(d, cpumask_of(cpu));
2688 return IRQ_SET_MASK_OK_DONE;
2691 static void its_vpe_schedule(struct its_vpe *vpe)
2693 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2694 u64 val;
2696 /* Schedule the VPE */
2697 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2698 GENMASK_ULL(51, 12);
2699 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2700 val |= GICR_VPROPBASER_RaWb;
2701 val |= GICR_VPROPBASER_InnerShareable;
2702 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2704 val = virt_to_phys(page_address(vpe->vpt_page)) &
2705 GENMASK_ULL(51, 16);
2706 val |= GICR_VPENDBASER_RaWaWb;
2707 val |= GICR_VPENDBASER_NonShareable;
2709 * There is no good way of finding out if the pending table is
2710 * empty as we can race against the doorbell interrupt very
2711 * easily. So in the end, vpe->pending_last is only an
2712 * indication that the vcpu has something pending, not one
2713 * that the pending table is empty. A good implementation
2714 * would be able to read its coarse map pretty quickly anyway,
2715 * making this a tolerable issue.
2717 val |= GICR_VPENDBASER_PendingLast;
2718 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2719 val |= GICR_VPENDBASER_Valid;
2720 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2723 static void its_vpe_deschedule(struct its_vpe *vpe)
2725 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2726 u64 val;
2728 val = its_clear_vpend_valid(vlpi_base);
2730 if (unlikely(val & GICR_VPENDBASER_Dirty)) {
2731 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2732 vpe->idai = false;
2733 vpe->pending_last = true;
2734 } else {
2735 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2736 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2740 static void its_vpe_invall(struct its_vpe *vpe)
2742 struct its_node *its;
2744 list_for_each_entry(its, &its_nodes, entry) {
2745 if (!its->is_v4)
2746 continue;
2748 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
2749 continue;
2752 * Sending a VINVALL to a single ITS is enough, as all
2753 * we need is to reach the redistributors.
2755 its_send_vinvall(its, vpe);
2756 return;
2760 static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2762 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2763 struct its_cmd_info *info = vcpu_info;
2765 switch (info->cmd_type) {
2766 case SCHEDULE_VPE:
2767 its_vpe_schedule(vpe);
2768 return 0;
2770 case DESCHEDULE_VPE:
2771 its_vpe_deschedule(vpe);
2772 return 0;
2774 case INVALL_VPE:
2775 its_vpe_invall(vpe);
2776 return 0;
2778 default:
2779 return -EINVAL;
2783 static void its_vpe_send_cmd(struct its_vpe *vpe,
2784 void (*cmd)(struct its_device *, u32))
2786 unsigned long flags;
2788 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2790 its_vpe_db_proxy_map_locked(vpe);
2791 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
2793 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2796 static void its_vpe_send_inv(struct irq_data *d)
2798 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2800 if (gic_rdists->has_direct_lpi) {
2801 void __iomem *rdbase;
2803 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2804 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
2805 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2806 cpu_relax();
2807 } else {
2808 its_vpe_send_cmd(vpe, its_send_inv);
2812 static void its_vpe_mask_irq(struct irq_data *d)
2815 * We need to unmask the LPI, which is described by the parent
2816 * irq_data. Instead of calling into the parent (which won't
2817 * exactly do the right thing, let's simply use the
2818 * parent_data pointer. Yes, I'm naughty.
2820 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
2821 its_vpe_send_inv(d);
2824 static void its_vpe_unmask_irq(struct irq_data *d)
2826 /* Same hack as above... */
2827 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
2828 its_vpe_send_inv(d);
2831 static int its_vpe_set_irqchip_state(struct irq_data *d,
2832 enum irqchip_irq_state which,
2833 bool state)
2835 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2837 if (which != IRQCHIP_STATE_PENDING)
2838 return -EINVAL;
2840 if (gic_rdists->has_direct_lpi) {
2841 void __iomem *rdbase;
2843 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2844 if (state) {
2845 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
2846 } else {
2847 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2848 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2849 cpu_relax();
2851 } else {
2852 if (state)
2853 its_vpe_send_cmd(vpe, its_send_int);
2854 else
2855 its_vpe_send_cmd(vpe, its_send_clear);
2858 return 0;
2861 static struct irq_chip its_vpe_irq_chip = {
2862 .name = "GICv4-vpe",
2863 .irq_mask = its_vpe_mask_irq,
2864 .irq_unmask = its_vpe_unmask_irq,
2865 .irq_eoi = irq_chip_eoi_parent,
2866 .irq_set_affinity = its_vpe_set_affinity,
2867 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
2868 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
2871 static int its_vpe_id_alloc(void)
2873 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
2876 static void its_vpe_id_free(u16 id)
2878 ida_simple_remove(&its_vpeid_ida, id);
2881 static int its_vpe_init(struct its_vpe *vpe)
2883 struct page *vpt_page;
2884 int vpe_id;
2886 /* Allocate vpe_id */
2887 vpe_id = its_vpe_id_alloc();
2888 if (vpe_id < 0)
2889 return vpe_id;
2891 /* Allocate VPT */
2892 vpt_page = its_allocate_pending_table(GFP_KERNEL);
2893 if (!vpt_page) {
2894 its_vpe_id_free(vpe_id);
2895 return -ENOMEM;
2898 if (!its_alloc_vpe_table(vpe_id)) {
2899 its_vpe_id_free(vpe_id);
2900 its_free_pending_table(vpt_page);
2901 return -ENOMEM;
2904 vpe->vpe_id = vpe_id;
2905 vpe->vpt_page = vpt_page;
2906 vpe->vpe_proxy_event = -1;
2908 return 0;
2911 static void its_vpe_teardown(struct its_vpe *vpe)
2913 its_vpe_db_proxy_unmap(vpe);
2914 its_vpe_id_free(vpe->vpe_id);
2915 its_free_pending_table(vpe->vpt_page);
2918 static void its_vpe_irq_domain_free(struct irq_domain *domain,
2919 unsigned int virq,
2920 unsigned int nr_irqs)
2922 struct its_vm *vm = domain->host_data;
2923 int i;
2925 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2927 for (i = 0; i < nr_irqs; i++) {
2928 struct irq_data *data = irq_domain_get_irq_data(domain,
2929 virq + i);
2930 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
2932 BUG_ON(vm != vpe->its_vm);
2934 clear_bit(data->hwirq, vm->db_bitmap);
2935 its_vpe_teardown(vpe);
2936 irq_domain_reset_irq_data(data);
2939 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
2940 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
2941 its_free_prop_table(vm->vprop_page);
2945 static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2946 unsigned int nr_irqs, void *args)
2948 struct its_vm *vm = args;
2949 unsigned long *bitmap;
2950 struct page *vprop_page;
2951 int base, nr_ids, i, err = 0;
2953 BUG_ON(!vm);
2955 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
2956 if (!bitmap)
2957 return -ENOMEM;
2959 if (nr_ids < nr_irqs) {
2960 its_lpi_free(bitmap, base, nr_ids);
2961 return -ENOMEM;
2964 vprop_page = its_allocate_prop_table(GFP_KERNEL);
2965 if (!vprop_page) {
2966 its_lpi_free(bitmap, base, nr_ids);
2967 return -ENOMEM;
2970 vm->db_bitmap = bitmap;
2971 vm->db_lpi_base = base;
2972 vm->nr_db_lpis = nr_ids;
2973 vm->vprop_page = vprop_page;
2975 for (i = 0; i < nr_irqs; i++) {
2976 vm->vpes[i]->vpe_db_lpi = base + i;
2977 err = its_vpe_init(vm->vpes[i]);
2978 if (err)
2979 break;
2980 err = its_irq_gic_domain_alloc(domain, virq + i,
2981 vm->vpes[i]->vpe_db_lpi);
2982 if (err)
2983 break;
2984 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
2985 &its_vpe_irq_chip, vm->vpes[i]);
2986 set_bit(i, bitmap);
2989 if (err) {
2990 if (i > 0)
2991 its_vpe_irq_domain_free(domain, virq, i - 1);
2993 its_lpi_free(bitmap, base, nr_ids);
2994 its_free_prop_table(vprop_page);
2997 return err;
3000 static int its_vpe_irq_domain_activate(struct irq_domain *domain,
3001 struct irq_data *d, bool reserve)
3003 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3004 struct its_node *its;
3006 /* If we use the list map, we issue VMAPP on demand... */
3007 if (its_list_map)
3008 return 0;
3010 /* Map the VPE to the first possible CPU */
3011 vpe->col_idx = cpumask_first(cpu_online_mask);
3013 list_for_each_entry(its, &its_nodes, entry) {
3014 if (!its->is_v4)
3015 continue;
3017 its_send_vmapp(its, vpe, true);
3018 its_send_vinvall(its, vpe);
3021 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
3023 return 0;
3026 static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
3027 struct irq_data *d)
3029 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3030 struct its_node *its;
3033 * If we use the list map, we unmap the VPE once no VLPIs are
3034 * associated with the VM.
3036 if (its_list_map)
3037 return;
3039 list_for_each_entry(its, &its_nodes, entry) {
3040 if (!its->is_v4)
3041 continue;
3043 its_send_vmapp(its, vpe, false);
3047 static const struct irq_domain_ops its_vpe_domain_ops = {
3048 .alloc = its_vpe_irq_domain_alloc,
3049 .free = its_vpe_irq_domain_free,
3050 .activate = its_vpe_irq_domain_activate,
3051 .deactivate = its_vpe_irq_domain_deactivate,
3054 static int its_force_quiescent(void __iomem *base)
3056 u32 count = 1000000; /* 1s */
3057 u32 val;
3059 val = readl_relaxed(base + GITS_CTLR);
3061 * GIC architecture specification requires the ITS to be both
3062 * disabled and quiescent for writes to GITS_BASER<n> or
3063 * GITS_CBASER to not have UNPREDICTABLE results.
3065 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
3066 return 0;
3068 /* Disable the generation of all interrupts to this ITS */
3069 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
3070 writel_relaxed(val, base + GITS_CTLR);
3072 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
3073 while (1) {
3074 val = readl_relaxed(base + GITS_CTLR);
3075 if (val & GITS_CTLR_QUIESCENT)
3076 return 0;
3078 count--;
3079 if (!count)
3080 return -EBUSY;
3082 cpu_relax();
3083 udelay(1);
3087 static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
3089 struct its_node *its = data;
3091 /* erratum 22375: only alloc 8MB table size */
3092 its->device_ids = 0x14; /* 20 bits, 8MB */
3093 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
3095 return true;
3098 static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
3100 struct its_node *its = data;
3102 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
3104 return true;
3107 static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
3109 struct its_node *its = data;
3111 /* On QDF2400, the size of the ITE is 16Bytes */
3112 its->ite_size = 16;
3114 return true;
3117 static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
3119 struct its_node *its = its_dev->its;
3122 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
3123 * which maps 32-bit writes targeted at a separate window of
3124 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
3125 * with device ID taken from bits [device_id_bits + 1:2] of
3126 * the window offset.
3128 return its->pre_its_base + (its_dev->device_id << 2);
3131 static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
3133 struct its_node *its = data;
3134 u32 pre_its_window[2];
3135 u32 ids;
3137 if (!fwnode_property_read_u32_array(its->fwnode_handle,
3138 "socionext,synquacer-pre-its",
3139 pre_its_window,
3140 ARRAY_SIZE(pre_its_window))) {
3142 its->pre_its_base = pre_its_window[0];
3143 its->get_msi_base = its_irq_get_msi_base_pre_its;
3145 ids = ilog2(pre_its_window[1]) - 2;
3146 if (its->device_ids > ids)
3147 its->device_ids = ids;
3149 /* the pre-ITS breaks isolation, so disable MSI remapping */
3150 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
3151 return true;
3153 return false;
3156 static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
3158 struct its_node *its = data;
3161 * Hip07 insists on using the wrong address for the VLPI
3162 * page. Trick it into doing the right thing...
3164 its->vlpi_redist_offset = SZ_128K;
3165 return true;
3168 static const struct gic_quirk its_quirks[] = {
3169 #ifdef CONFIG_CAVIUM_ERRATUM_22375
3171 .desc = "ITS: Cavium errata 22375, 24313",
3172 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3173 .mask = 0xffff0fff,
3174 .init = its_enable_quirk_cavium_22375,
3176 #endif
3177 #ifdef CONFIG_CAVIUM_ERRATUM_23144
3179 .desc = "ITS: Cavium erratum 23144",
3180 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3181 .mask = 0xffff0fff,
3182 .init = its_enable_quirk_cavium_23144,
3184 #endif
3185 #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
3187 .desc = "ITS: QDF2400 erratum 0065",
3188 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
3189 .mask = 0xffffffff,
3190 .init = its_enable_quirk_qdf2400_e0065,
3192 #endif
3193 #ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
3196 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
3197 * implementation, but with a 'pre-ITS' added that requires
3198 * special handling in software.
3200 .desc = "ITS: Socionext Synquacer pre-ITS",
3201 .iidr = 0x0001143b,
3202 .mask = 0xffffffff,
3203 .init = its_enable_quirk_socionext_synquacer,
3205 #endif
3206 #ifdef CONFIG_HISILICON_ERRATUM_161600802
3208 .desc = "ITS: Hip07 erratum 161600802",
3209 .iidr = 0x00000004,
3210 .mask = 0xffffffff,
3211 .init = its_enable_quirk_hip07_161600802,
3213 #endif
3218 static void its_enable_quirks(struct its_node *its)
3220 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
3222 gic_enable_quirks(iidr, its_quirks, its);
3225 static int its_save_disable(void)
3227 struct its_node *its;
3228 int err = 0;
3230 raw_spin_lock(&its_lock);
3231 list_for_each_entry(its, &its_nodes, entry) {
3232 void __iomem *base;
3234 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3235 continue;
3237 base = its->base;
3238 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
3239 err = its_force_quiescent(base);
3240 if (err) {
3241 pr_err("ITS@%pa: failed to quiesce: %d\n",
3242 &its->phys_base, err);
3243 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3244 goto err;
3247 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
3250 err:
3251 if (err) {
3252 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
3253 void __iomem *base;
3255 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3256 continue;
3258 base = its->base;
3259 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3262 raw_spin_unlock(&its_lock);
3264 return err;
3267 static void its_restore_enable(void)
3269 struct its_node *its;
3270 int ret;
3272 raw_spin_lock(&its_lock);
3273 list_for_each_entry(its, &its_nodes, entry) {
3274 void __iomem *base;
3275 int i;
3277 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3278 continue;
3280 base = its->base;
3283 * Make sure that the ITS is disabled. If it fails to quiesce,
3284 * don't restore it since writing to CBASER or BASER<n>
3285 * registers is undefined according to the GIC v3 ITS
3286 * Specification.
3288 ret = its_force_quiescent(base);
3289 if (ret) {
3290 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
3291 &its->phys_base, ret);
3292 continue;
3295 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
3298 * Writing CBASER resets CREADR to 0, so make CWRITER and
3299 * cmd_write line up with it.
3301 its->cmd_write = its->cmd_base;
3302 gits_write_cwriter(0, base + GITS_CWRITER);
3304 /* Restore GITS_BASER from the value cache. */
3305 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
3306 struct its_baser *baser = &its->tables[i];
3308 if (!(baser->val & GITS_BASER_VALID))
3309 continue;
3311 its_write_baser(its, baser, baser->val);
3313 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3316 * Reinit the collection if it's stored in the ITS. This is
3317 * indicated by the col_id being less than the HCC field.
3318 * CID < HCC as specified in the GIC v3 Documentation.
3320 if (its->collections[smp_processor_id()].col_id <
3321 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
3322 its_cpu_init_collection(its);
3324 raw_spin_unlock(&its_lock);
3327 static struct syscore_ops its_syscore_ops = {
3328 .suspend = its_save_disable,
3329 .resume = its_restore_enable,
3332 static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
3334 struct irq_domain *inner_domain;
3335 struct msi_domain_info *info;
3337 info = kzalloc(sizeof(*info), GFP_KERNEL);
3338 if (!info)
3339 return -ENOMEM;
3341 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
3342 if (!inner_domain) {
3343 kfree(info);
3344 return -ENOMEM;
3347 inner_domain->parent = its_parent;
3348 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
3349 inner_domain->flags |= its->msi_domain_flags;
3350 info->ops = &its_msi_domain_ops;
3351 info->data = its;
3352 inner_domain->host_data = info;
3354 return 0;
3357 static int its_init_vpe_domain(void)
3359 struct its_node *its;
3360 u32 devid;
3361 int entries;
3363 if (gic_rdists->has_direct_lpi) {
3364 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
3365 return 0;
3368 /* Any ITS will do, even if not v4 */
3369 its = list_first_entry(&its_nodes, struct its_node, entry);
3371 entries = roundup_pow_of_two(nr_cpu_ids);
3372 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
3373 GFP_KERNEL);
3374 if (!vpe_proxy.vpes) {
3375 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
3376 return -ENOMEM;
3379 /* Use the last possible DevID */
3380 devid = GENMASK(its->device_ids - 1, 0);
3381 vpe_proxy.dev = its_create_device(its, devid, entries, false);
3382 if (!vpe_proxy.dev) {
3383 kfree(vpe_proxy.vpes);
3384 pr_err("ITS: Can't allocate GICv4 proxy device\n");
3385 return -ENOMEM;
3388 BUG_ON(entries > vpe_proxy.dev->nr_ites);
3390 raw_spin_lock_init(&vpe_proxy.lock);
3391 vpe_proxy.next_victim = 0;
3392 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
3393 devid, vpe_proxy.dev->nr_ites);
3395 return 0;
3398 static int __init its_compute_its_list_map(struct resource *res,
3399 void __iomem *its_base)
3401 int its_number;
3402 u32 ctlr;
3405 * This is assumed to be done early enough that we're
3406 * guaranteed to be single-threaded, hence no
3407 * locking. Should this change, we should address
3408 * this.
3410 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
3411 if (its_number >= GICv4_ITS_LIST_MAX) {
3412 pr_err("ITS@%pa: No ITSList entry available!\n",
3413 &res->start);
3414 return -EINVAL;
3417 ctlr = readl_relaxed(its_base + GITS_CTLR);
3418 ctlr &= ~GITS_CTLR_ITS_NUMBER;
3419 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
3420 writel_relaxed(ctlr, its_base + GITS_CTLR);
3421 ctlr = readl_relaxed(its_base + GITS_CTLR);
3422 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
3423 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
3424 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
3427 if (test_and_set_bit(its_number, &its_list_map)) {
3428 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
3429 &res->start, its_number);
3430 return -EINVAL;
3433 return its_number;
3436 static int __init its_probe_one(struct resource *res,
3437 struct fwnode_handle *handle, int numa_node)
3439 struct its_node *its;
3440 void __iomem *its_base;
3441 u32 val, ctlr;
3442 u64 baser, tmp, typer;
3443 int err;
3445 its_base = ioremap(res->start, resource_size(res));
3446 if (!its_base) {
3447 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
3448 return -ENOMEM;
3451 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
3452 if (val != 0x30 && val != 0x40) {
3453 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
3454 err = -ENODEV;
3455 goto out_unmap;
3458 err = its_force_quiescent(its_base);
3459 if (err) {
3460 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
3461 goto out_unmap;
3464 pr_info("ITS %pR\n", res);
3466 its = kzalloc(sizeof(*its), GFP_KERNEL);
3467 if (!its) {
3468 err = -ENOMEM;
3469 goto out_unmap;
3472 raw_spin_lock_init(&its->lock);
3473 mutex_init(&its->dev_alloc_lock);
3474 INIT_LIST_HEAD(&its->entry);
3475 INIT_LIST_HEAD(&its->its_device_list);
3476 typer = gic_read_typer(its_base + GITS_TYPER);
3477 its->base = its_base;
3478 its->phys_base = res->start;
3479 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
3480 its->device_ids = GITS_TYPER_DEVBITS(typer);
3481 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
3482 if (its->is_v4) {
3483 if (!(typer & GITS_TYPER_VMOVP)) {
3484 err = its_compute_its_list_map(res, its_base);
3485 if (err < 0)
3486 goto out_free_its;
3488 its->list_nr = err;
3490 pr_info("ITS@%pa: Using ITS number %d\n",
3491 &res->start, err);
3492 } else {
3493 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
3497 its->numa_node = numa_node;
3499 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
3500 get_order(ITS_CMD_QUEUE_SZ));
3501 if (!its->cmd_base) {
3502 err = -ENOMEM;
3503 goto out_free_its;
3505 its->cmd_write = its->cmd_base;
3506 its->fwnode_handle = handle;
3507 its->get_msi_base = its_irq_get_msi_base;
3508 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
3510 its_enable_quirks(its);
3512 err = its_alloc_tables(its);
3513 if (err)
3514 goto out_free_cmd;
3516 err = its_alloc_collections(its);
3517 if (err)
3518 goto out_free_tables;
3520 baser = (virt_to_phys(its->cmd_base) |
3521 GITS_CBASER_RaWaWb |
3522 GITS_CBASER_InnerShareable |
3523 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
3524 GITS_CBASER_VALID);
3526 gits_write_cbaser(baser, its->base + GITS_CBASER);
3527 tmp = gits_read_cbaser(its->base + GITS_CBASER);
3529 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
3530 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
3532 * The HW reports non-shareable, we must
3533 * remove the cacheability attributes as
3534 * well.
3536 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
3537 GITS_CBASER_CACHEABILITY_MASK);
3538 baser |= GITS_CBASER_nC;
3539 gits_write_cbaser(baser, its->base + GITS_CBASER);
3541 pr_info("ITS: using cache flushing for cmd queue\n");
3542 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
3545 gits_write_cwriter(0, its->base + GITS_CWRITER);
3546 ctlr = readl_relaxed(its->base + GITS_CTLR);
3547 ctlr |= GITS_CTLR_ENABLE;
3548 if (its->is_v4)
3549 ctlr |= GITS_CTLR_ImDe;
3550 writel_relaxed(ctlr, its->base + GITS_CTLR);
3552 if (GITS_TYPER_HCC(typer))
3553 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
3555 err = its_init_domain(handle, its);
3556 if (err)
3557 goto out_free_tables;
3559 raw_spin_lock(&its_lock);
3560 list_add(&its->entry, &its_nodes);
3561 raw_spin_unlock(&its_lock);
3563 return 0;
3565 out_free_tables:
3566 its_free_tables(its);
3567 out_free_cmd:
3568 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
3569 out_free_its:
3570 kfree(its);
3571 out_unmap:
3572 iounmap(its_base);
3573 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
3574 return err;
3577 static bool gic_rdists_supports_plpis(void)
3579 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
3582 static int redist_disable_lpis(void)
3584 void __iomem *rbase = gic_data_rdist_rd_base();
3585 u64 timeout = USEC_PER_SEC;
3586 u64 val;
3589 * If coming via a CPU hotplug event, we don't need to disable
3590 * LPIs before trying to re-enable them. They are already
3591 * configured and all is well in the world. Detect this case
3592 * by checking the allocation of the pending table for the
3593 * current CPU.
3595 if (gic_data_rdist()->pend_page)
3596 return 0;
3598 if (!gic_rdists_supports_plpis()) {
3599 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
3600 return -ENXIO;
3603 val = readl_relaxed(rbase + GICR_CTLR);
3604 if (!(val & GICR_CTLR_ENABLE_LPIS))
3605 return 0;
3607 pr_warn("CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
3608 smp_processor_id());
3609 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
3611 /* Disable LPIs */
3612 val &= ~GICR_CTLR_ENABLE_LPIS;
3613 writel_relaxed(val, rbase + GICR_CTLR);
3615 /* Make sure any change to GICR_CTLR is observable by the GIC */
3616 dsb(sy);
3619 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
3620 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
3621 * Error out if we time out waiting for RWP to clear.
3623 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
3624 if (!timeout) {
3625 pr_err("CPU%d: Timeout while disabling LPIs\n",
3626 smp_processor_id());
3627 return -ETIMEDOUT;
3629 udelay(1);
3630 timeout--;
3634 * After it has been written to 1, it is IMPLEMENTATION
3635 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
3636 * cleared to 0. Error out if clearing the bit failed.
3638 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
3639 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
3640 return -EBUSY;
3643 return 0;
3646 int its_cpu_init(void)
3648 if (!list_empty(&its_nodes)) {
3649 int ret;
3651 ret = redist_disable_lpis();
3652 if (ret)
3653 return ret;
3655 its_cpu_init_lpis();
3656 its_cpu_init_collections();
3659 return 0;
3662 static const struct of_device_id its_device_id[] = {
3663 { .compatible = "arm,gic-v3-its", },
3667 static int __init its_of_probe(struct device_node *node)
3669 struct device_node *np;
3670 struct resource res;
3672 for (np = of_find_matching_node(node, its_device_id); np;
3673 np = of_find_matching_node(np, its_device_id)) {
3674 if (!of_device_is_available(np))
3675 continue;
3676 if (!of_property_read_bool(np, "msi-controller")) {
3677 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
3678 np);
3679 continue;
3682 if (of_address_to_resource(np, 0, &res)) {
3683 pr_warn("%pOF: no regs?\n", np);
3684 continue;
3687 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
3689 return 0;
3692 #ifdef CONFIG_ACPI
3694 #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
3696 #ifdef CONFIG_ACPI_NUMA
3697 struct its_srat_map {
3698 /* numa node id */
3699 u32 numa_node;
3700 /* GIC ITS ID */
3701 u32 its_id;
3704 static struct its_srat_map *its_srat_maps __initdata;
3705 static int its_in_srat __initdata;
3707 static int __init acpi_get_its_numa_node(u32 its_id)
3709 int i;
3711 for (i = 0; i < its_in_srat; i++) {
3712 if (its_id == its_srat_maps[i].its_id)
3713 return its_srat_maps[i].numa_node;
3715 return NUMA_NO_NODE;
3718 static int __init gic_acpi_match_srat_its(struct acpi_subtable_header *header,
3719 const unsigned long end)
3721 return 0;
3724 static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
3725 const unsigned long end)
3727 int node;
3728 struct acpi_srat_gic_its_affinity *its_affinity;
3730 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
3731 if (!its_affinity)
3732 return -EINVAL;
3734 if (its_affinity->header.length < sizeof(*its_affinity)) {
3735 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
3736 its_affinity->header.length);
3737 return -EINVAL;
3740 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
3742 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
3743 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
3744 return 0;
3747 its_srat_maps[its_in_srat].numa_node = node;
3748 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
3749 its_in_srat++;
3750 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
3751 its_affinity->proximity_domain, its_affinity->its_id, node);
3753 return 0;
3756 static void __init acpi_table_parse_srat_its(void)
3758 int count;
3760 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
3761 sizeof(struct acpi_table_srat),
3762 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3763 gic_acpi_match_srat_its, 0);
3764 if (count <= 0)
3765 return;
3767 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
3768 GFP_KERNEL);
3769 if (!its_srat_maps) {
3770 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
3771 return;
3774 acpi_table_parse_entries(ACPI_SIG_SRAT,
3775 sizeof(struct acpi_table_srat),
3776 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3777 gic_acpi_parse_srat_its, 0);
3780 /* free the its_srat_maps after ITS probing */
3781 static void __init acpi_its_srat_maps_free(void)
3783 kfree(its_srat_maps);
3785 #else
3786 static void __init acpi_table_parse_srat_its(void) { }
3787 static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
3788 static void __init acpi_its_srat_maps_free(void) { }
3789 #endif
3791 static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
3792 const unsigned long end)
3794 struct acpi_madt_generic_translator *its_entry;
3795 struct fwnode_handle *dom_handle;
3796 struct resource res;
3797 int err;
3799 its_entry = (struct acpi_madt_generic_translator *)header;
3800 memset(&res, 0, sizeof(res));
3801 res.start = its_entry->base_address;
3802 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
3803 res.flags = IORESOURCE_MEM;
3805 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
3806 if (!dom_handle) {
3807 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
3808 &res.start);
3809 return -ENOMEM;
3812 err = iort_register_domain_token(its_entry->translation_id, res.start,
3813 dom_handle);
3814 if (err) {
3815 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
3816 &res.start, its_entry->translation_id);
3817 goto dom_err;
3820 err = its_probe_one(&res, dom_handle,
3821 acpi_get_its_numa_node(its_entry->translation_id));
3822 if (!err)
3823 return 0;
3825 iort_deregister_domain_token(its_entry->translation_id);
3826 dom_err:
3827 irq_domain_free_fwnode(dom_handle);
3828 return err;
3831 static void __init its_acpi_probe(void)
3833 acpi_table_parse_srat_its();
3834 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
3835 gic_acpi_parse_madt_its, 0);
3836 acpi_its_srat_maps_free();
3838 #else
3839 static void __init its_acpi_probe(void) { }
3840 #endif
3842 int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
3843 struct irq_domain *parent_domain)
3845 struct device_node *of_node;
3846 struct its_node *its;
3847 bool has_v4 = false;
3848 int err;
3850 its_parent = parent_domain;
3851 of_node = to_of_node(handle);
3852 if (of_node)
3853 its_of_probe(of_node);
3854 else
3855 its_acpi_probe();
3857 if (list_empty(&its_nodes)) {
3858 pr_warn("ITS: No ITS available, not enabling LPIs\n");
3859 return -ENXIO;
3862 gic_rdists = rdists;
3863 err = its_alloc_lpi_tables();
3864 if (err)
3865 return err;
3867 list_for_each_entry(its, &its_nodes, entry)
3868 has_v4 |= its->is_v4;
3870 if (has_v4 & rdists->has_vlpis) {
3871 if (its_init_vpe_domain() ||
3872 its_init_v4(parent_domain, &its_vpe_domain_ops)) {
3873 rdists->has_vlpis = false;
3874 pr_err("ITS: Disabling GICv4 support\n");
3878 register_syscore_ops(&its_syscore_ops);
3880 return 0;