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