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 | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 9 | #include <linux/bitmap.h> |
| 10 | #include <linux/cpu.h> |
Marc Zyngier | c6e2ccb | 2018-06-26 11:21:11 +0100 | [diff] [blame] | 11 | #include <linux/crash_dump.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 12 | #include <linux/delay.h> |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 13 | #include <linux/dma-iommu.h> |
Marc Zyngier | 3fb68fa | 2018-07-27 16:21:18 +0100 | [diff] [blame] | 14 | #include <linux/efi.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 15 | #include <linux/interrupt.h> |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 16 | #include <linux/irqdomain.h> |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 17 | #include <linux/list.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 18 | #include <linux/log2.h> |
Marc Zyngier | 5e2c9f9 | 2018-07-27 16:23:18 +0100 | [diff] [blame] | 19 | #include <linux/memblock.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 20 | #include <linux/mm.h> |
| 21 | #include <linux/msi.h> |
| 22 | #include <linux/of.h> |
| 23 | #include <linux/of_address.h> |
| 24 | #include <linux/of_irq.h> |
| 25 | #include <linux/of_pci.h> |
| 26 | #include <linux/of_platform.h> |
| 27 | #include <linux/percpu.h> |
| 28 | #include <linux/slab.h> |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 29 | #include <linux/syscore_ops.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 30 | |
Joel Porquet | 41a83e06 | 2015-07-07 17:11:46 -0400 | [diff] [blame] | 31 | #include <linux/irqchip.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 32 | #include <linux/irqchip/arm-gic-v3.h> |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 33 | #include <linux/irqchip/arm-gic-v4.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 34 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 35 | #include <asm/cputype.h> |
| 36 | #include <asm/exception.h> |
| 37 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 38 | #include "irq-gic-common.h" |
| 39 | |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 40 | #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0) |
| 41 | #define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1) |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 42 | #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2) |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 43 | #define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 44 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 45 | #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0) |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 46 | #define RDIST_FLAGS_RD_TABLES_PREALLOCATED (1 << 1) |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 47 | |
Marc Zyngier | a13b040 | 2016-12-19 17:15:24 +0000 | [diff] [blame] | 48 | static u32 lpi_id_bits; |
| 49 | |
| 50 | /* |
| 51 | * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to |
| 52 | * deal with (one configuration byte per interrupt). PENDBASE has to |
| 53 | * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI). |
| 54 | */ |
| 55 | #define LPI_NRBITS lpi_id_bits |
| 56 | #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K) |
| 57 | #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K) |
| 58 | |
Julien Thierry | 2130b78 | 2018-08-28 16:51:18 +0100 | [diff] [blame] | 59 | #define LPI_PROP_DEFAULT_PRIO GICD_INT_DEF_PRI |
Marc Zyngier | a13b040 | 2016-12-19 17:15:24 +0000 | [diff] [blame] | 60 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 61 | /* |
| 62 | * Collection structure - just an ID, and a redistributor address to |
| 63 | * ping. We use one per CPU as a bag of interrupts assigned to this |
| 64 | * CPU. |
| 65 | */ |
| 66 | struct its_collection { |
| 67 | u64 target_address; |
| 68 | u16 col_id; |
| 69 | }; |
| 70 | |
| 71 | /* |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 72 | * The ITS_BASER structure - contains memory information, cached |
| 73 | * value of BASER register configuration and ITS page size. |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 74 | */ |
| 75 | struct its_baser { |
| 76 | void *base; |
| 77 | u64 val; |
| 78 | u32 order; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 79 | u32 psz; |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 80 | }; |
| 81 | |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 82 | struct its_device; |
| 83 | |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 84 | /* |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 85 | * The ITS structure - contains most of the infrastructure, with the |
Marc Zyngier | 841514a | 2015-07-28 14:46:20 +0100 | [diff] [blame] | 86 | * top-level MSI domain, the command queue, the collections, and the |
| 87 | * list of devices writing to it. |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 88 | * |
| 89 | * dev_alloc_lock has to be taken for device allocations, while the |
| 90 | * spinlock must be taken to parse data structures such as the device |
| 91 | * list. |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 92 | */ |
| 93 | struct its_node { |
| 94 | raw_spinlock_t lock; |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 95 | struct mutex dev_alloc_lock; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 96 | struct list_head entry; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 97 | void __iomem *base; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 98 | phys_addr_t phys_base; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 99 | struct its_cmd_block *cmd_base; |
| 100 | struct its_cmd_block *cmd_write; |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 101 | struct its_baser tables[GITS_BASER_NR_REGS]; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 102 | struct its_collection *collections; |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 103 | struct fwnode_handle *fwnode_handle; |
| 104 | u64 (*get_msi_base)(struct its_device *its_dev); |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 105 | u64 cbaser_save; |
| 106 | u32 ctlr_save; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 107 | struct list_head its_device_list; |
| 108 | u64 flags; |
Marc Zyngier | debf6d0 | 2017-10-08 18:44:42 +0100 | [diff] [blame] | 109 | unsigned long list_nr; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 110 | u32 ite_size; |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 111 | u32 device_ids; |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 112 | int numa_node; |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 113 | unsigned int msi_domain_flags; |
| 114 | u32 pre_its_base; /* for Socionext Synquacer */ |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 115 | bool is_v4; |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 116 | int vlpi_redist_offset; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | #define ITS_ITT_ALIGN SZ_256 |
| 120 | |
Shanker Donthineni | 32bd44d | 2017-10-07 15:43:48 -0500 | [diff] [blame] | 121 | /* The maximum number of VPEID bits supported by VLPI commands */ |
| 122 | #define ITS_MAX_VPEID_BITS (16) |
| 123 | #define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS)) |
| 124 | |
Shanker Donthineni | 2eca0d6 | 2016-02-16 18:00:36 -0600 | [diff] [blame] | 125 | /* Convert page order to size in bytes */ |
| 126 | #define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o)) |
| 127 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 128 | struct event_lpi_map { |
| 129 | unsigned long *lpi_map; |
| 130 | u16 *col_map; |
| 131 | irq_hw_number_t lpi_base; |
| 132 | int nr_lpis; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 133 | struct mutex vlpi_lock; |
| 134 | struct its_vm *vm; |
| 135 | struct its_vlpi_map *vlpi_maps; |
| 136 | int nr_vlpis; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 137 | }; |
| 138 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 139 | /* |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 140 | * The ITS view of a device - belongs to an ITS, owns an interrupt |
| 141 | * translation table, and a list of interrupts. If it some of its |
| 142 | * LPIs are injected into a guest (GICv4), the event_map.vm field |
| 143 | * indicates which one. |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 144 | */ |
| 145 | struct its_device { |
| 146 | struct list_head entry; |
| 147 | struct its_node *its; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 148 | struct event_lpi_map event_map; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 149 | void *itt; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 150 | u32 nr_ites; |
| 151 | u32 device_id; |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 152 | bool shared; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 155 | static struct { |
| 156 | raw_spinlock_t lock; |
| 157 | struct its_device *dev; |
| 158 | struct its_vpe **vpes; |
| 159 | int next_victim; |
| 160 | } vpe_proxy; |
| 161 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 162 | static LIST_HEAD(its_nodes); |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 163 | static DEFINE_RAW_SPINLOCK(its_lock); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 164 | static struct rdists *gic_rdists; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 165 | static struct irq_domain *its_parent; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 166 | |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 167 | static unsigned long its_list_map; |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 168 | static u16 vmovp_seq_num; |
| 169 | static DEFINE_RAW_SPINLOCK(vmovp_lock); |
| 170 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 171 | static DEFINE_IDA(its_vpeid_ida); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 172 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 173 | #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist)) |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 174 | #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] | 175 | #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base) |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 176 | #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] | 177 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 178 | static struct its_collection *dev_event_to_col(struct its_device *its_dev, |
| 179 | u32 event) |
| 180 | { |
| 181 | struct its_node *its = its_dev->its; |
| 182 | |
| 183 | return its->collections + its_dev->event_map.col_map[event]; |
| 184 | } |
| 185 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 186 | static struct its_collection *valid_col(struct its_collection *col) |
| 187 | { |
Joe Perches | 20faba8 | 2019-07-09 22:04:18 -0700 | [diff] [blame] | 188 | if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(15, 0))) |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 189 | return NULL; |
| 190 | |
| 191 | return col; |
| 192 | } |
| 193 | |
Marc Zyngier | 205e065 | 2018-06-22 10:52:53 +0100 | [diff] [blame] | 194 | static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe) |
| 195 | { |
| 196 | if (valid_col(its->collections + vpe->col_idx)) |
| 197 | return vpe; |
| 198 | |
| 199 | return NULL; |
| 200 | } |
| 201 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 202 | /* |
| 203 | * ITS command descriptors - parameters to be encoded in a command |
| 204 | * block. |
| 205 | */ |
| 206 | struct its_cmd_desc { |
| 207 | union { |
| 208 | struct { |
| 209 | struct its_device *dev; |
| 210 | u32 event_id; |
| 211 | } its_inv_cmd; |
| 212 | |
| 213 | struct { |
| 214 | struct its_device *dev; |
| 215 | u32 event_id; |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 216 | } its_clear_cmd; |
| 217 | |
| 218 | struct { |
| 219 | struct its_device *dev; |
| 220 | u32 event_id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 221 | } its_int_cmd; |
| 222 | |
| 223 | struct { |
| 224 | struct its_device *dev; |
| 225 | int valid; |
| 226 | } its_mapd_cmd; |
| 227 | |
| 228 | struct { |
| 229 | struct its_collection *col; |
| 230 | int valid; |
| 231 | } its_mapc_cmd; |
| 232 | |
| 233 | struct { |
| 234 | struct its_device *dev; |
| 235 | u32 phys_id; |
| 236 | u32 event_id; |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 237 | } its_mapti_cmd; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 238 | |
| 239 | struct { |
| 240 | struct its_device *dev; |
| 241 | struct its_collection *col; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 242 | u32 event_id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 243 | } its_movi_cmd; |
| 244 | |
| 245 | struct { |
| 246 | struct its_device *dev; |
| 247 | u32 event_id; |
| 248 | } its_discard_cmd; |
| 249 | |
| 250 | struct { |
| 251 | struct its_collection *col; |
| 252 | } its_invall_cmd; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 253 | |
| 254 | struct { |
| 255 | struct its_vpe *vpe; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 256 | } its_vinvall_cmd; |
| 257 | |
| 258 | struct { |
| 259 | struct its_vpe *vpe; |
| 260 | struct its_collection *col; |
| 261 | bool valid; |
| 262 | } its_vmapp_cmd; |
| 263 | |
| 264 | struct { |
| 265 | struct its_vpe *vpe; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 266 | struct its_device *dev; |
| 267 | u32 virt_id; |
| 268 | u32 event_id; |
| 269 | bool db_enabled; |
| 270 | } its_vmapti_cmd; |
| 271 | |
| 272 | struct { |
| 273 | struct its_vpe *vpe; |
| 274 | struct its_device *dev; |
| 275 | u32 event_id; |
| 276 | bool db_enabled; |
| 277 | } its_vmovi_cmd; |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 278 | |
| 279 | struct { |
| 280 | struct its_vpe *vpe; |
| 281 | struct its_collection *col; |
| 282 | u16 seq_num; |
| 283 | u16 its_list; |
| 284 | } its_vmovp_cmd; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 285 | }; |
| 286 | }; |
| 287 | |
| 288 | /* |
| 289 | * The ITS command block, which is what the ITS actually parses. |
| 290 | */ |
| 291 | struct its_cmd_block { |
| 292 | u64 raw_cmd[4]; |
| 293 | }; |
| 294 | |
| 295 | #define ITS_CMD_QUEUE_SZ SZ_64K |
| 296 | #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block)) |
| 297 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 298 | typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *, |
| 299 | struct its_cmd_block *, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 300 | struct its_cmd_desc *); |
| 301 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 302 | typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *, |
| 303 | struct its_cmd_block *, |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 304 | struct its_cmd_desc *); |
| 305 | |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 306 | static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l) |
| 307 | { |
| 308 | u64 mask = GENMASK_ULL(h, l); |
| 309 | *raw_cmd &= ~mask; |
| 310 | *raw_cmd |= (val << l) & mask; |
| 311 | } |
| 312 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 313 | static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr) |
| 314 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 315 | its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static void its_encode_devid(struct its_cmd_block *cmd, u32 devid) |
| 319 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 320 | its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | static void its_encode_event_id(struct its_cmd_block *cmd, u32 id) |
| 324 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 325 | its_mask_encode(&cmd->raw_cmd[1], id, 31, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id) |
| 329 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 330 | its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 331 | } |
| 332 | |
| 333 | static void its_encode_size(struct its_cmd_block *cmd, u8 size) |
| 334 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 335 | its_mask_encode(&cmd->raw_cmd[1], size, 4, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr) |
| 339 | { |
Shanker Donthineni | 30ae961 | 2017-10-09 11:46:55 -0500 | [diff] [blame] | 340 | its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | static void its_encode_valid(struct its_cmd_block *cmd, int valid) |
| 344 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 345 | its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr) |
| 349 | { |
Shanker Donthineni | 30ae961 | 2017-10-09 11:46:55 -0500 | [diff] [blame] | 350 | its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | static void its_encode_collection(struct its_cmd_block *cmd, u16 col) |
| 354 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 355 | its_mask_encode(&cmd->raw_cmd[2], col, 15, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 356 | } |
| 357 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 358 | static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid) |
| 359 | { |
| 360 | its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32); |
| 361 | } |
| 362 | |
| 363 | static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id) |
| 364 | { |
| 365 | its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0); |
| 366 | } |
| 367 | |
| 368 | static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id) |
| 369 | { |
| 370 | its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32); |
| 371 | } |
| 372 | |
| 373 | static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid) |
| 374 | { |
| 375 | its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0); |
| 376 | } |
| 377 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 378 | static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num) |
| 379 | { |
| 380 | its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32); |
| 381 | } |
| 382 | |
| 383 | static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list) |
| 384 | { |
| 385 | its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0); |
| 386 | } |
| 387 | |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 388 | static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa) |
| 389 | { |
Shanker Donthineni | 30ae961 | 2017-10-09 11:46:55 -0500 | [diff] [blame] | 390 | its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16); |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size) |
| 394 | { |
| 395 | its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0); |
| 396 | } |
| 397 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 398 | static inline void its_fixup_cmd(struct its_cmd_block *cmd) |
| 399 | { |
| 400 | /* Let's fixup BE commands */ |
| 401 | cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]); |
| 402 | cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]); |
| 403 | cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]); |
| 404 | cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]); |
| 405 | } |
| 406 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 407 | static struct its_collection *its_build_mapd_cmd(struct its_node *its, |
| 408 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 409 | struct its_cmd_desc *desc) |
| 410 | { |
| 411 | unsigned long itt_addr; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 412 | u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 413 | |
| 414 | itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt); |
| 415 | itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN); |
| 416 | |
| 417 | its_encode_cmd(cmd, GITS_CMD_MAPD); |
| 418 | its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id); |
| 419 | its_encode_size(cmd, size - 1); |
| 420 | its_encode_itt(cmd, itt_addr); |
| 421 | its_encode_valid(cmd, desc->its_mapd_cmd.valid); |
| 422 | |
| 423 | its_fixup_cmd(cmd); |
| 424 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 425 | return NULL; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 428 | static struct its_collection *its_build_mapc_cmd(struct its_node *its, |
| 429 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 430 | struct its_cmd_desc *desc) |
| 431 | { |
| 432 | its_encode_cmd(cmd, GITS_CMD_MAPC); |
| 433 | its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id); |
| 434 | its_encode_target(cmd, desc->its_mapc_cmd.col->target_address); |
| 435 | its_encode_valid(cmd, desc->its_mapc_cmd.valid); |
| 436 | |
| 437 | its_fixup_cmd(cmd); |
| 438 | |
| 439 | return desc->its_mapc_cmd.col; |
| 440 | } |
| 441 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 442 | static struct its_collection *its_build_mapti_cmd(struct its_node *its, |
| 443 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 444 | struct its_cmd_desc *desc) |
| 445 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 446 | struct its_collection *col; |
| 447 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 448 | col = dev_event_to_col(desc->its_mapti_cmd.dev, |
| 449 | desc->its_mapti_cmd.event_id); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 450 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 451 | its_encode_cmd(cmd, GITS_CMD_MAPTI); |
| 452 | its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id); |
| 453 | its_encode_event_id(cmd, desc->its_mapti_cmd.event_id); |
| 454 | its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 455 | its_encode_collection(cmd, col->col_id); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 456 | |
| 457 | its_fixup_cmd(cmd); |
| 458 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 459 | return valid_col(col); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 462 | static struct its_collection *its_build_movi_cmd(struct its_node *its, |
| 463 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 464 | struct its_cmd_desc *desc) |
| 465 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 466 | struct its_collection *col; |
| 467 | |
| 468 | col = dev_event_to_col(desc->its_movi_cmd.dev, |
| 469 | desc->its_movi_cmd.event_id); |
| 470 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 471 | its_encode_cmd(cmd, GITS_CMD_MOVI); |
| 472 | its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 473 | its_encode_event_id(cmd, desc->its_movi_cmd.event_id); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 474 | its_encode_collection(cmd, desc->its_movi_cmd.col->col_id); |
| 475 | |
| 476 | its_fixup_cmd(cmd); |
| 477 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 478 | return valid_col(col); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 481 | static struct its_collection *its_build_discard_cmd(struct its_node *its, |
| 482 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 483 | struct its_cmd_desc *desc) |
| 484 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 485 | struct its_collection *col; |
| 486 | |
| 487 | col = dev_event_to_col(desc->its_discard_cmd.dev, |
| 488 | desc->its_discard_cmd.event_id); |
| 489 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 490 | its_encode_cmd(cmd, GITS_CMD_DISCARD); |
| 491 | its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id); |
| 492 | its_encode_event_id(cmd, desc->its_discard_cmd.event_id); |
| 493 | |
| 494 | its_fixup_cmd(cmd); |
| 495 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 496 | return valid_col(col); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 499 | static struct its_collection *its_build_inv_cmd(struct its_node *its, |
| 500 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 501 | struct its_cmd_desc *desc) |
| 502 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 503 | struct its_collection *col; |
| 504 | |
| 505 | col = dev_event_to_col(desc->its_inv_cmd.dev, |
| 506 | desc->its_inv_cmd.event_id); |
| 507 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 508 | its_encode_cmd(cmd, GITS_CMD_INV); |
| 509 | its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id); |
| 510 | its_encode_event_id(cmd, desc->its_inv_cmd.event_id); |
| 511 | |
| 512 | its_fixup_cmd(cmd); |
| 513 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 514 | return valid_col(col); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 517 | static struct its_collection *its_build_int_cmd(struct its_node *its, |
| 518 | struct its_cmd_block *cmd, |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 519 | struct its_cmd_desc *desc) |
| 520 | { |
| 521 | struct its_collection *col; |
| 522 | |
| 523 | col = dev_event_to_col(desc->its_int_cmd.dev, |
| 524 | desc->its_int_cmd.event_id); |
| 525 | |
| 526 | its_encode_cmd(cmd, GITS_CMD_INT); |
| 527 | its_encode_devid(cmd, desc->its_int_cmd.dev->device_id); |
| 528 | its_encode_event_id(cmd, desc->its_int_cmd.event_id); |
| 529 | |
| 530 | its_fixup_cmd(cmd); |
| 531 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 532 | return valid_col(col); |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 535 | static struct its_collection *its_build_clear_cmd(struct its_node *its, |
| 536 | struct its_cmd_block *cmd, |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 537 | struct its_cmd_desc *desc) |
| 538 | { |
| 539 | struct its_collection *col; |
| 540 | |
| 541 | col = dev_event_to_col(desc->its_clear_cmd.dev, |
| 542 | desc->its_clear_cmd.event_id); |
| 543 | |
| 544 | its_encode_cmd(cmd, GITS_CMD_CLEAR); |
| 545 | its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id); |
| 546 | its_encode_event_id(cmd, desc->its_clear_cmd.event_id); |
| 547 | |
| 548 | its_fixup_cmd(cmd); |
| 549 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 550 | return valid_col(col); |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 551 | } |
| 552 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 553 | static struct its_collection *its_build_invall_cmd(struct its_node *its, |
| 554 | struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 555 | struct its_cmd_desc *desc) |
| 556 | { |
| 557 | its_encode_cmd(cmd, GITS_CMD_INVALL); |
| 558 | its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id); |
| 559 | |
| 560 | its_fixup_cmd(cmd); |
| 561 | |
| 562 | return NULL; |
| 563 | } |
| 564 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 565 | static struct its_vpe *its_build_vinvall_cmd(struct its_node *its, |
| 566 | struct its_cmd_block *cmd, |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 567 | struct its_cmd_desc *desc) |
| 568 | { |
| 569 | its_encode_cmd(cmd, GITS_CMD_VINVALL); |
| 570 | its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id); |
| 571 | |
| 572 | its_fixup_cmd(cmd); |
| 573 | |
Marc Zyngier | 205e065 | 2018-06-22 10:52:53 +0100 | [diff] [blame] | 574 | return valid_vpe(its, desc->its_vinvall_cmd.vpe); |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 577 | static struct its_vpe *its_build_vmapp_cmd(struct its_node *its, |
| 578 | struct its_cmd_block *cmd, |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 579 | struct its_cmd_desc *desc) |
| 580 | { |
| 581 | unsigned long vpt_addr; |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 582 | u64 target; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 583 | |
| 584 | vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page)); |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 585 | target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 586 | |
| 587 | its_encode_cmd(cmd, GITS_CMD_VMAPP); |
| 588 | its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id); |
| 589 | its_encode_valid(cmd, desc->its_vmapp_cmd.valid); |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 590 | its_encode_target(cmd, target); |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 591 | its_encode_vpt_addr(cmd, vpt_addr); |
| 592 | its_encode_vpt_size(cmd, LPI_NRBITS - 1); |
| 593 | |
| 594 | its_fixup_cmd(cmd); |
| 595 | |
Marc Zyngier | 205e065 | 2018-06-22 10:52:53 +0100 | [diff] [blame] | 596 | return valid_vpe(its, desc->its_vmapp_cmd.vpe); |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 597 | } |
| 598 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 599 | static struct its_vpe *its_build_vmapti_cmd(struct its_node *its, |
| 600 | struct its_cmd_block *cmd, |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 601 | struct its_cmd_desc *desc) |
| 602 | { |
| 603 | u32 db; |
| 604 | |
| 605 | if (desc->its_vmapti_cmd.db_enabled) |
| 606 | db = desc->its_vmapti_cmd.vpe->vpe_db_lpi; |
| 607 | else |
| 608 | db = 1023; |
| 609 | |
| 610 | its_encode_cmd(cmd, GITS_CMD_VMAPTI); |
| 611 | its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id); |
| 612 | its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id); |
| 613 | its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id); |
| 614 | its_encode_db_phys_id(cmd, db); |
| 615 | its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id); |
| 616 | |
| 617 | its_fixup_cmd(cmd); |
| 618 | |
Marc Zyngier | 205e065 | 2018-06-22 10:52:53 +0100 | [diff] [blame] | 619 | return valid_vpe(its, desc->its_vmapti_cmd.vpe); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 620 | } |
| 621 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 622 | static struct its_vpe *its_build_vmovi_cmd(struct its_node *its, |
| 623 | struct its_cmd_block *cmd, |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 624 | struct its_cmd_desc *desc) |
| 625 | { |
| 626 | u32 db; |
| 627 | |
| 628 | if (desc->its_vmovi_cmd.db_enabled) |
| 629 | db = desc->its_vmovi_cmd.vpe->vpe_db_lpi; |
| 630 | else |
| 631 | db = 1023; |
| 632 | |
| 633 | its_encode_cmd(cmd, GITS_CMD_VMOVI); |
| 634 | its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id); |
| 635 | its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id); |
| 636 | its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id); |
| 637 | its_encode_db_phys_id(cmd, db); |
| 638 | its_encode_db_valid(cmd, true); |
| 639 | |
| 640 | its_fixup_cmd(cmd); |
| 641 | |
Marc Zyngier | 205e065 | 2018-06-22 10:52:53 +0100 | [diff] [blame] | 642 | return valid_vpe(its, desc->its_vmovi_cmd.vpe); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 643 | } |
| 644 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 645 | static struct its_vpe *its_build_vmovp_cmd(struct its_node *its, |
| 646 | struct its_cmd_block *cmd, |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 647 | struct its_cmd_desc *desc) |
| 648 | { |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 649 | u64 target; |
| 650 | |
| 651 | target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset; |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 652 | its_encode_cmd(cmd, GITS_CMD_VMOVP); |
| 653 | its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num); |
| 654 | its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list); |
| 655 | its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id); |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 656 | its_encode_target(cmd, target); |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 657 | |
| 658 | its_fixup_cmd(cmd); |
| 659 | |
Marc Zyngier | 205e065 | 2018-06-22 10:52:53 +0100 | [diff] [blame] | 660 | return valid_vpe(its, desc->its_vmovp_cmd.vpe); |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 663 | static u64 its_cmd_ptr_to_offset(struct its_node *its, |
| 664 | struct its_cmd_block *ptr) |
| 665 | { |
| 666 | return (ptr - its->cmd_base) * sizeof(*ptr); |
| 667 | } |
| 668 | |
| 669 | static int its_queue_full(struct its_node *its) |
| 670 | { |
| 671 | int widx; |
| 672 | int ridx; |
| 673 | |
| 674 | widx = its->cmd_write - its->cmd_base; |
| 675 | ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block); |
| 676 | |
| 677 | /* This is incredibly unlikely to happen, unless the ITS locks up. */ |
| 678 | if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx) |
| 679 | return 1; |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | static struct its_cmd_block *its_allocate_entry(struct its_node *its) |
| 685 | { |
| 686 | struct its_cmd_block *cmd; |
| 687 | u32 count = 1000000; /* 1s! */ |
| 688 | |
| 689 | while (its_queue_full(its)) { |
| 690 | count--; |
| 691 | if (!count) { |
| 692 | pr_err_ratelimited("ITS queue not draining\n"); |
| 693 | return NULL; |
| 694 | } |
| 695 | cpu_relax(); |
| 696 | udelay(1); |
| 697 | } |
| 698 | |
| 699 | cmd = its->cmd_write++; |
| 700 | |
| 701 | /* Handle queue wrapping */ |
| 702 | if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES)) |
| 703 | its->cmd_write = its->cmd_base; |
| 704 | |
Marc Zyngier | 34d677a | 2016-12-19 17:16:45 +0000 | [diff] [blame] | 705 | /* Clear command */ |
| 706 | cmd->raw_cmd[0] = 0; |
| 707 | cmd->raw_cmd[1] = 0; |
| 708 | cmd->raw_cmd[2] = 0; |
| 709 | cmd->raw_cmd[3] = 0; |
| 710 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 711 | return cmd; |
| 712 | } |
| 713 | |
| 714 | static struct its_cmd_block *its_post_commands(struct its_node *its) |
| 715 | { |
| 716 | u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write); |
| 717 | |
| 718 | writel_relaxed(wr, its->base + GITS_CWRITER); |
| 719 | |
| 720 | return its->cmd_write; |
| 721 | } |
| 722 | |
| 723 | static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd) |
| 724 | { |
| 725 | /* |
| 726 | * Make sure the commands written to memory are observable by |
| 727 | * the ITS. |
| 728 | */ |
| 729 | if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 730 | gic_flush_dcache_to_poc(cmd, sizeof(*cmd)); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 731 | else |
| 732 | dsb(ishst); |
| 733 | } |
| 734 | |
Marc Zyngier | a19b462 | 2017-08-04 17:45:50 +0100 | [diff] [blame] | 735 | static int its_wait_for_range_completion(struct its_node *its, |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 736 | u64 prev_idx, |
Marc Zyngier | a19b462 | 2017-08-04 17:45:50 +0100 | [diff] [blame] | 737 | struct its_cmd_block *to) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 738 | { |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 739 | u64 rd_idx, to_idx, linear_idx; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 740 | u32 count = 1000000; /* 1s! */ |
| 741 | |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 742 | /* Linearize to_idx if the command set has wrapped around */ |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 743 | to_idx = its_cmd_ptr_to_offset(its, to); |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 744 | if (to_idx < prev_idx) |
| 745 | to_idx += ITS_CMD_QUEUE_SZ; |
| 746 | |
| 747 | linear_idx = prev_idx; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 748 | |
| 749 | while (1) { |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 750 | s64 delta; |
| 751 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 752 | rd_idx = readl_relaxed(its->base + GITS_CREADR); |
Marc Zyngier | 9bdd8b1 | 2017-08-19 10:16:02 +0100 | [diff] [blame] | 753 | |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 754 | /* |
| 755 | * Compute the read pointer progress, taking the |
| 756 | * potential wrap-around into account. |
| 757 | */ |
| 758 | delta = rd_idx - prev_idx; |
| 759 | if (rd_idx < prev_idx) |
| 760 | delta += ITS_CMD_QUEUE_SZ; |
Marc Zyngier | 9bdd8b1 | 2017-08-19 10:16:02 +0100 | [diff] [blame] | 761 | |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 762 | linear_idx += delta; |
| 763 | if (linear_idx >= to_idx) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 764 | break; |
| 765 | |
| 766 | count--; |
| 767 | if (!count) { |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 768 | pr_err_ratelimited("ITS queue timeout (%llu %llu)\n", |
| 769 | to_idx, linear_idx); |
Marc Zyngier | a19b462 | 2017-08-04 17:45:50 +0100 | [diff] [blame] | 770 | return -1; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 771 | } |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 772 | prev_idx = rd_idx; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 773 | cpu_relax(); |
| 774 | udelay(1); |
| 775 | } |
Marc Zyngier | a19b462 | 2017-08-04 17:45:50 +0100 | [diff] [blame] | 776 | |
| 777 | return 0; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 778 | } |
| 779 | |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 780 | /* Warning, macro hell follows */ |
| 781 | #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \ |
| 782 | void name(struct its_node *its, \ |
| 783 | buildtype builder, \ |
| 784 | struct its_cmd_desc *desc) \ |
| 785 | { \ |
| 786 | struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \ |
| 787 | synctype *sync_obj; \ |
| 788 | unsigned long flags; \ |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 789 | u64 rd_idx; \ |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 790 | \ |
| 791 | raw_spin_lock_irqsave(&its->lock, flags); \ |
| 792 | \ |
| 793 | cmd = its_allocate_entry(its); \ |
| 794 | if (!cmd) { /* We're soooooo screewed... */ \ |
| 795 | raw_spin_unlock_irqrestore(&its->lock, flags); \ |
| 796 | return; \ |
| 797 | } \ |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 798 | sync_obj = builder(its, cmd, desc); \ |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 799 | its_flush_cmd(its, cmd); \ |
| 800 | \ |
| 801 | if (sync_obj) { \ |
| 802 | sync_cmd = its_allocate_entry(its); \ |
| 803 | if (!sync_cmd) \ |
| 804 | goto post; \ |
| 805 | \ |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 806 | buildfn(its, sync_cmd, sync_obj); \ |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 807 | its_flush_cmd(its, sync_cmd); \ |
| 808 | } \ |
| 809 | \ |
| 810 | post: \ |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 811 | rd_idx = readl_relaxed(its->base + GITS_CREADR); \ |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 812 | next_cmd = its_post_commands(its); \ |
| 813 | raw_spin_unlock_irqrestore(&its->lock, flags); \ |
| 814 | \ |
Heyi Guo | a050fa5 | 2019-05-13 19:42:06 +0800 | [diff] [blame] | 815 | if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \ |
Marc Zyngier | a19b462 | 2017-08-04 17:45:50 +0100 | [diff] [blame] | 816 | pr_err_ratelimited("ITS cmd %ps failed\n", builder); \ |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 817 | } |
| 818 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 819 | static void its_build_sync_cmd(struct its_node *its, |
| 820 | struct its_cmd_block *sync_cmd, |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 821 | struct its_collection *sync_col) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 822 | { |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 823 | its_encode_cmd(sync_cmd, GITS_CMD_SYNC); |
| 824 | its_encode_target(sync_cmd, sync_col->target_address); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 825 | |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 826 | its_fixup_cmd(sync_cmd); |
| 827 | } |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 828 | |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 829 | static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t, |
| 830 | struct its_collection, its_build_sync_cmd) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 831 | |
Marc Zyngier | 67047f90 | 2017-07-28 21:16:58 +0100 | [diff] [blame] | 832 | static void its_build_vsync_cmd(struct its_node *its, |
| 833 | struct its_cmd_block *sync_cmd, |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 834 | struct its_vpe *sync_vpe) |
| 835 | { |
| 836 | its_encode_cmd(sync_cmd, GITS_CMD_VSYNC); |
| 837 | its_encode_vpeid(sync_cmd, sync_vpe->vpe_id); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 838 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 839 | its_fixup_cmd(sync_cmd); |
| 840 | } |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 841 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 842 | static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t, |
| 843 | struct its_vpe, its_build_vsync_cmd) |
| 844 | |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 845 | static void its_send_int(struct its_device *dev, u32 event_id) |
| 846 | { |
| 847 | struct its_cmd_desc desc; |
| 848 | |
| 849 | desc.its_int_cmd.dev = dev; |
| 850 | desc.its_int_cmd.event_id = event_id; |
| 851 | |
| 852 | its_send_single_command(dev->its, its_build_int_cmd, &desc); |
| 853 | } |
| 854 | |
| 855 | static void its_send_clear(struct its_device *dev, u32 event_id) |
| 856 | { |
| 857 | struct its_cmd_desc desc; |
| 858 | |
| 859 | desc.its_clear_cmd.dev = dev; |
| 860 | desc.its_clear_cmd.event_id = event_id; |
| 861 | |
| 862 | its_send_single_command(dev->its, its_build_clear_cmd, &desc); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | static void its_send_inv(struct its_device *dev, u32 event_id) |
| 866 | { |
| 867 | struct its_cmd_desc desc; |
| 868 | |
| 869 | desc.its_inv_cmd.dev = dev; |
| 870 | desc.its_inv_cmd.event_id = event_id; |
| 871 | |
| 872 | its_send_single_command(dev->its, its_build_inv_cmd, &desc); |
| 873 | } |
| 874 | |
| 875 | static void its_send_mapd(struct its_device *dev, int valid) |
| 876 | { |
| 877 | struct its_cmd_desc desc; |
| 878 | |
| 879 | desc.its_mapd_cmd.dev = dev; |
| 880 | desc.its_mapd_cmd.valid = !!valid; |
| 881 | |
| 882 | its_send_single_command(dev->its, its_build_mapd_cmd, &desc); |
| 883 | } |
| 884 | |
| 885 | static void its_send_mapc(struct its_node *its, struct its_collection *col, |
| 886 | int valid) |
| 887 | { |
| 888 | struct its_cmd_desc desc; |
| 889 | |
| 890 | desc.its_mapc_cmd.col = col; |
| 891 | desc.its_mapc_cmd.valid = !!valid; |
| 892 | |
| 893 | its_send_single_command(its, its_build_mapc_cmd, &desc); |
| 894 | } |
| 895 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 896 | 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] | 897 | { |
| 898 | struct its_cmd_desc desc; |
| 899 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 900 | desc.its_mapti_cmd.dev = dev; |
| 901 | desc.its_mapti_cmd.phys_id = irq_id; |
| 902 | desc.its_mapti_cmd.event_id = id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 903 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 904 | its_send_single_command(dev->its, its_build_mapti_cmd, &desc); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 905 | } |
| 906 | |
| 907 | static void its_send_movi(struct its_device *dev, |
| 908 | struct its_collection *col, u32 id) |
| 909 | { |
| 910 | struct its_cmd_desc desc; |
| 911 | |
| 912 | desc.its_movi_cmd.dev = dev; |
| 913 | desc.its_movi_cmd.col = col; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 914 | desc.its_movi_cmd.event_id = id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 915 | |
| 916 | its_send_single_command(dev->its, its_build_movi_cmd, &desc); |
| 917 | } |
| 918 | |
| 919 | static void its_send_discard(struct its_device *dev, u32 id) |
| 920 | { |
| 921 | struct its_cmd_desc desc; |
| 922 | |
| 923 | desc.its_discard_cmd.dev = dev; |
| 924 | desc.its_discard_cmd.event_id = id; |
| 925 | |
| 926 | its_send_single_command(dev->its, its_build_discard_cmd, &desc); |
| 927 | } |
| 928 | |
| 929 | static void its_send_invall(struct its_node *its, struct its_collection *col) |
| 930 | { |
| 931 | struct its_cmd_desc desc; |
| 932 | |
| 933 | desc.its_invall_cmd.col = col; |
| 934 | |
| 935 | its_send_single_command(its, its_build_invall_cmd, &desc); |
| 936 | } |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 937 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 938 | static void its_send_vmapti(struct its_device *dev, u32 id) |
| 939 | { |
| 940 | struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id]; |
| 941 | struct its_cmd_desc desc; |
| 942 | |
| 943 | desc.its_vmapti_cmd.vpe = map->vpe; |
| 944 | desc.its_vmapti_cmd.dev = dev; |
| 945 | desc.its_vmapti_cmd.virt_id = map->vintid; |
| 946 | desc.its_vmapti_cmd.event_id = id; |
| 947 | desc.its_vmapti_cmd.db_enabled = map->db_enabled; |
| 948 | |
| 949 | its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc); |
| 950 | } |
| 951 | |
| 952 | static void its_send_vmovi(struct its_device *dev, u32 id) |
| 953 | { |
| 954 | struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id]; |
| 955 | struct its_cmd_desc desc; |
| 956 | |
| 957 | desc.its_vmovi_cmd.vpe = map->vpe; |
| 958 | desc.its_vmovi_cmd.dev = dev; |
| 959 | desc.its_vmovi_cmd.event_id = id; |
| 960 | desc.its_vmovi_cmd.db_enabled = map->db_enabled; |
| 961 | |
| 962 | its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc); |
| 963 | } |
| 964 | |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 965 | static void its_send_vmapp(struct its_node *its, |
| 966 | struct its_vpe *vpe, bool valid) |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 967 | { |
| 968 | struct its_cmd_desc desc; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 969 | |
| 970 | desc.its_vmapp_cmd.vpe = vpe; |
| 971 | desc.its_vmapp_cmd.valid = valid; |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 972 | desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx]; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 973 | |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 974 | its_send_single_vcommand(its, its_build_vmapp_cmd, &desc); |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 975 | } |
| 976 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 977 | static void its_send_vmovp(struct its_vpe *vpe) |
| 978 | { |
| 979 | struct its_cmd_desc desc; |
| 980 | struct its_node *its; |
| 981 | unsigned long flags; |
| 982 | int col_id = vpe->col_idx; |
| 983 | |
| 984 | desc.its_vmovp_cmd.vpe = vpe; |
| 985 | desc.its_vmovp_cmd.its_list = (u16)its_list_map; |
| 986 | |
| 987 | if (!its_list_map) { |
| 988 | its = list_first_entry(&its_nodes, struct its_node, entry); |
| 989 | desc.its_vmovp_cmd.seq_num = 0; |
| 990 | desc.its_vmovp_cmd.col = &its->collections[col_id]; |
| 991 | its_send_single_vcommand(its, its_build_vmovp_cmd, &desc); |
| 992 | return; |
| 993 | } |
| 994 | |
| 995 | /* |
| 996 | * Yet another marvel of the architecture. If using the |
| 997 | * its_list "feature", we need to make sure that all ITSs |
| 998 | * receive all VMOVP commands in the same order. The only way |
| 999 | * to guarantee this is to make vmovp a serialization point. |
| 1000 | * |
| 1001 | * Wall <-- Head. |
| 1002 | */ |
| 1003 | raw_spin_lock_irqsave(&vmovp_lock, flags); |
| 1004 | |
| 1005 | desc.its_vmovp_cmd.seq_num = vmovp_seq_num++; |
| 1006 | |
| 1007 | /* Emit VMOVPs */ |
| 1008 | list_for_each_entry(its, &its_nodes, entry) { |
| 1009 | if (!its->is_v4) |
| 1010 | continue; |
| 1011 | |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 1012 | if (!vpe->its_vm->vlpi_count[its->list_nr]) |
| 1013 | continue; |
| 1014 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 1015 | desc.its_vmovp_cmd.col = &its->collections[col_id]; |
| 1016 | its_send_single_vcommand(its, its_build_vmovp_cmd, &desc); |
| 1017 | } |
| 1018 | |
| 1019 | raw_spin_unlock_irqrestore(&vmovp_lock, flags); |
| 1020 | } |
| 1021 | |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 1022 | 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] | 1023 | { |
| 1024 | struct its_cmd_desc desc; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 1025 | |
| 1026 | desc.its_vinvall_cmd.vpe = vpe; |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 1027 | its_send_single_vcommand(its, its_build_vinvall_cmd, &desc); |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1030 | /* |
| 1031 | * irqchip functions - assumes MSI, mostly. |
| 1032 | */ |
| 1033 | |
| 1034 | static inline u32 its_get_event_id(struct irq_data *d) |
| 1035 | { |
| 1036 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1037 | return d->hwirq - its_dev->event_map.lpi_base; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1038 | } |
| 1039 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1040 | 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] | 1041 | { |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1042 | irq_hw_number_t hwirq; |
Marc Zyngier | e1a2e20 | 2018-07-27 14:36:00 +0100 | [diff] [blame] | 1043 | void *va; |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1044 | u8 *cfg; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1045 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1046 | if (irqd_is_forwarded_to_vcpu(d)) { |
| 1047 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1048 | u32 event = its_get_event_id(d); |
Marc Zyngier | d4d7b4a | 2017-10-26 10:44:07 +0100 | [diff] [blame] | 1049 | struct its_vlpi_map *map; |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1050 | |
Marc Zyngier | e1a2e20 | 2018-07-27 14:36:00 +0100 | [diff] [blame] | 1051 | va = page_address(its_dev->event_map.vm->vprop_page); |
Marc Zyngier | d4d7b4a | 2017-10-26 10:44:07 +0100 | [diff] [blame] | 1052 | map = &its_dev->event_map.vlpi_maps[event]; |
| 1053 | hwirq = map->vintid; |
| 1054 | |
| 1055 | /* Remember the updated property */ |
| 1056 | map->properties &= ~clr; |
| 1057 | map->properties |= set | LPI_PROP_GROUP1; |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1058 | } else { |
Marc Zyngier | e1a2e20 | 2018-07-27 14:36:00 +0100 | [diff] [blame] | 1059 | va = gic_rdists->prop_table_va; |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1060 | hwirq = d->hwirq; |
| 1061 | } |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1062 | |
Marc Zyngier | e1a2e20 | 2018-07-27 14:36:00 +0100 | [diff] [blame] | 1063 | cfg = va + hwirq - 8192; |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1064 | *cfg &= ~clr; |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1065 | *cfg |= set | LPI_PROP_GROUP1; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1066 | |
| 1067 | /* |
| 1068 | * Make the above write visible to the redistributors. |
| 1069 | * And yes, we're flushing exactly: One. Single. Byte. |
| 1070 | * Humpf... |
| 1071 | */ |
| 1072 | if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 1073 | gic_flush_dcache_to_poc(cfg, sizeof(*cfg)); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1074 | else |
| 1075 | dsb(ishst); |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | static void lpi_update_config(struct irq_data *d, u8 clr, u8 set) |
| 1079 | { |
| 1080 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1081 | |
| 1082 | lpi_write_config(d, clr, set); |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1083 | its_send_inv(its_dev, its_get_event_id(d)); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1086 | static void its_vlpi_set_doorbell(struct irq_data *d, bool enable) |
| 1087 | { |
| 1088 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1089 | u32 event = its_get_event_id(d); |
| 1090 | |
| 1091 | if (its_dev->event_map.vlpi_maps[event].db_enabled == enable) |
| 1092 | return; |
| 1093 | |
| 1094 | its_dev->event_map.vlpi_maps[event].db_enabled = enable; |
| 1095 | |
| 1096 | /* |
| 1097 | * More fun with the architecture: |
| 1098 | * |
| 1099 | * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI |
| 1100 | * value or to 1023, depending on the enable bit. But that |
| 1101 | * would be issueing a mapping for an /existing/ DevID+EventID |
| 1102 | * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI |
| 1103 | * to the /same/ vPE, using this opportunity to adjust the |
| 1104 | * doorbell. Mouahahahaha. We loves it, Precious. |
| 1105 | */ |
| 1106 | its_send_vmovi(its_dev, event); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
| 1109 | static void its_mask_irq(struct irq_data *d) |
| 1110 | { |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1111 | if (irqd_is_forwarded_to_vcpu(d)) |
| 1112 | its_vlpi_set_doorbell(d, false); |
| 1113 | |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1114 | lpi_update_config(d, LPI_PROP_ENABLED, 0); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1115 | } |
| 1116 | |
| 1117 | static void its_unmask_irq(struct irq_data *d) |
| 1118 | { |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1119 | if (irqd_is_forwarded_to_vcpu(d)) |
| 1120 | its_vlpi_set_doorbell(d, true); |
| 1121 | |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1122 | lpi_update_config(d, 0, LPI_PROP_ENABLED); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1125 | static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val, |
| 1126 | bool force) |
| 1127 | { |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 1128 | unsigned int cpu; |
| 1129 | const struct cpumask *cpu_mask = cpu_online_mask; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1130 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1131 | struct its_collection *target_col; |
| 1132 | u32 id = its_get_event_id(d); |
| 1133 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1134 | /* A forwarded interrupt should use irq_set_vcpu_affinity */ |
| 1135 | if (irqd_is_forwarded_to_vcpu(d)) |
| 1136 | return -EINVAL; |
| 1137 | |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 1138 | /* lpi cannot be routed to a redistributor that is on a foreign node */ |
| 1139 | if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) { |
| 1140 | if (its_dev->its->numa_node >= 0) { |
| 1141 | cpu_mask = cpumask_of_node(its_dev->its->numa_node); |
| 1142 | if (!cpumask_intersects(mask_val, cpu_mask)) |
| 1143 | return -EINVAL; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | cpu = cpumask_any_and(mask_val, cpu_mask); |
| 1148 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1149 | if (cpu >= nr_cpu_ids) |
| 1150 | return -EINVAL; |
| 1151 | |
MaJun | 8b8d94a | 2017-05-18 16:19:13 +0800 | [diff] [blame] | 1152 | /* don't set the affinity when the target cpu is same as current one */ |
| 1153 | if (cpu != its_dev->event_map.col_map[id]) { |
| 1154 | target_col = &its_dev->its->collections[cpu]; |
| 1155 | its_send_movi(its_dev, target_col, id); |
| 1156 | its_dev->event_map.col_map[id] = cpu; |
Marc Zyngier | 0d224d3 | 2017-08-18 09:39:18 +0100 | [diff] [blame] | 1157 | irq_data_update_effective_affinity(d, cpumask_of(cpu)); |
MaJun | 8b8d94a | 2017-05-18 16:19:13 +0800 | [diff] [blame] | 1158 | } |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1159 | |
| 1160 | return IRQ_SET_MASK_OK_DONE; |
| 1161 | } |
| 1162 | |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 1163 | static u64 its_irq_get_msi_base(struct its_device *its_dev) |
| 1164 | { |
| 1165 | struct its_node *its = its_dev->its; |
| 1166 | |
| 1167 | return its->phys_base + GITS_TRANSLATER; |
| 1168 | } |
| 1169 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1170 | static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg) |
| 1171 | { |
| 1172 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1173 | struct its_node *its; |
| 1174 | u64 addr; |
| 1175 | |
| 1176 | its = its_dev->its; |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 1177 | addr = its->get_msi_base(its_dev); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1178 | |
Vladimir Murzin | b11283e | 2016-11-02 11:54:03 +0000 | [diff] [blame] | 1179 | msg->address_lo = lower_32_bits(addr); |
| 1180 | msg->address_hi = upper_32_bits(addr); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1181 | msg->data = its_get_event_id(d); |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 1182 | |
Julien Grall | 35ae7df | 2019-05-01 14:58:21 +0100 | [diff] [blame] | 1183 | iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 1186 | static int its_irq_set_irqchip_state(struct irq_data *d, |
| 1187 | enum irqchip_irq_state which, |
| 1188 | bool state) |
| 1189 | { |
| 1190 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1191 | u32 event = its_get_event_id(d); |
| 1192 | |
| 1193 | if (which != IRQCHIP_STATE_PENDING) |
| 1194 | return -EINVAL; |
| 1195 | |
| 1196 | if (state) |
| 1197 | its_send_int(its_dev, event); |
| 1198 | else |
| 1199 | its_send_clear(its_dev, event); |
| 1200 | |
| 1201 | return 0; |
| 1202 | } |
| 1203 | |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 1204 | static void its_map_vm(struct its_node *its, struct its_vm *vm) |
| 1205 | { |
| 1206 | unsigned long flags; |
| 1207 | |
| 1208 | /* Not using the ITS list? Everything is always mapped. */ |
| 1209 | if (!its_list_map) |
| 1210 | return; |
| 1211 | |
| 1212 | raw_spin_lock_irqsave(&vmovp_lock, flags); |
| 1213 | |
| 1214 | /* |
| 1215 | * If the VM wasn't mapped yet, iterate over the vpes and get |
| 1216 | * them mapped now. |
| 1217 | */ |
| 1218 | vm->vlpi_count[its->list_nr]++; |
| 1219 | |
| 1220 | if (vm->vlpi_count[its->list_nr] == 1) { |
| 1221 | int i; |
| 1222 | |
| 1223 | for (i = 0; i < vm->nr_vpes; i++) { |
| 1224 | struct its_vpe *vpe = vm->vpes[i]; |
Marc Zyngier | 44c4c25 | 2017-10-19 10:11:34 +0100 | [diff] [blame] | 1225 | struct irq_data *d = irq_get_irq_data(vpe->irq); |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 1226 | |
| 1227 | /* Map the VPE to the first possible CPU */ |
| 1228 | vpe->col_idx = cpumask_first(cpu_online_mask); |
| 1229 | its_send_vmapp(its, vpe, true); |
| 1230 | its_send_vinvall(its, vpe); |
Marc Zyngier | 44c4c25 | 2017-10-19 10:11:34 +0100 | [diff] [blame] | 1231 | irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx)); |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | raw_spin_unlock_irqrestore(&vmovp_lock, flags); |
| 1236 | } |
| 1237 | |
| 1238 | static void its_unmap_vm(struct its_node *its, struct its_vm *vm) |
| 1239 | { |
| 1240 | unsigned long flags; |
| 1241 | |
| 1242 | /* Not using the ITS list? Everything is always mapped. */ |
| 1243 | if (!its_list_map) |
| 1244 | return; |
| 1245 | |
| 1246 | raw_spin_lock_irqsave(&vmovp_lock, flags); |
| 1247 | |
| 1248 | if (!--vm->vlpi_count[its->list_nr]) { |
| 1249 | int i; |
| 1250 | |
| 1251 | for (i = 0; i < vm->nr_vpes; i++) |
| 1252 | its_send_vmapp(its, vm->vpes[i], false); |
| 1253 | } |
| 1254 | |
| 1255 | raw_spin_unlock_irqrestore(&vmovp_lock, flags); |
| 1256 | } |
| 1257 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1258 | static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) |
| 1259 | { |
| 1260 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1261 | u32 event = its_get_event_id(d); |
| 1262 | int ret = 0; |
| 1263 | |
| 1264 | if (!info->map) |
| 1265 | return -EINVAL; |
| 1266 | |
| 1267 | mutex_lock(&its_dev->event_map.vlpi_lock); |
| 1268 | |
| 1269 | if (!its_dev->event_map.vm) { |
| 1270 | struct its_vlpi_map *maps; |
| 1271 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 1272 | maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps), |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1273 | GFP_KERNEL); |
| 1274 | if (!maps) { |
| 1275 | ret = -ENOMEM; |
| 1276 | goto out; |
| 1277 | } |
| 1278 | |
| 1279 | its_dev->event_map.vm = info->map->vm; |
| 1280 | its_dev->event_map.vlpi_maps = maps; |
| 1281 | } else if (its_dev->event_map.vm != info->map->vm) { |
| 1282 | ret = -EINVAL; |
| 1283 | goto out; |
| 1284 | } |
| 1285 | |
| 1286 | /* Get our private copy of the mapping information */ |
| 1287 | its_dev->event_map.vlpi_maps[event] = *info->map; |
| 1288 | |
| 1289 | if (irqd_is_forwarded_to_vcpu(d)) { |
| 1290 | /* Already mapped, move it around */ |
| 1291 | its_send_vmovi(its_dev, event); |
| 1292 | } else { |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 1293 | /* Ensure all the VPEs are mapped on this ITS */ |
| 1294 | its_map_vm(its_dev->its, info->map->vm); |
| 1295 | |
Marc Zyngier | d4d7b4a | 2017-10-26 10:44:07 +0100 | [diff] [blame] | 1296 | /* |
| 1297 | * Flag the interrupt as forwarded so that we can |
| 1298 | * start poking the virtual property table. |
| 1299 | */ |
| 1300 | irqd_set_forwarded_to_vcpu(d); |
| 1301 | |
| 1302 | /* Write out the property to the prop table */ |
| 1303 | lpi_write_config(d, 0xff, info->map->properties); |
| 1304 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1305 | /* Drop the physical mapping */ |
| 1306 | its_send_discard(its_dev, event); |
| 1307 | |
| 1308 | /* and install the virtual one */ |
| 1309 | its_send_vmapti(its_dev, event); |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1310 | |
| 1311 | /* Increment the number of VLPIs */ |
| 1312 | its_dev->event_map.nr_vlpis++; |
| 1313 | } |
| 1314 | |
| 1315 | out: |
| 1316 | mutex_unlock(&its_dev->event_map.vlpi_lock); |
| 1317 | return ret; |
| 1318 | } |
| 1319 | |
| 1320 | static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info) |
| 1321 | { |
| 1322 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1323 | u32 event = its_get_event_id(d); |
| 1324 | int ret = 0; |
| 1325 | |
| 1326 | mutex_lock(&its_dev->event_map.vlpi_lock); |
| 1327 | |
| 1328 | if (!its_dev->event_map.vm || |
| 1329 | !its_dev->event_map.vlpi_maps[event].vm) { |
| 1330 | ret = -EINVAL; |
| 1331 | goto out; |
| 1332 | } |
| 1333 | |
| 1334 | /* Copy our mapping information to the incoming request */ |
| 1335 | *info->map = its_dev->event_map.vlpi_maps[event]; |
| 1336 | |
| 1337 | out: |
| 1338 | mutex_unlock(&its_dev->event_map.vlpi_lock); |
| 1339 | return ret; |
| 1340 | } |
| 1341 | |
| 1342 | static int its_vlpi_unmap(struct irq_data *d) |
| 1343 | { |
| 1344 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1345 | u32 event = its_get_event_id(d); |
| 1346 | int ret = 0; |
| 1347 | |
| 1348 | mutex_lock(&its_dev->event_map.vlpi_lock); |
| 1349 | |
| 1350 | if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) { |
| 1351 | ret = -EINVAL; |
| 1352 | goto out; |
| 1353 | } |
| 1354 | |
| 1355 | /* Drop the virtual mapping */ |
| 1356 | its_send_discard(its_dev, event); |
| 1357 | |
| 1358 | /* and restore the physical one */ |
| 1359 | irqd_clr_forwarded_to_vcpu(d); |
| 1360 | its_send_mapti(its_dev, d->hwirq, event); |
| 1361 | lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO | |
| 1362 | LPI_PROP_ENABLED | |
| 1363 | LPI_PROP_GROUP1)); |
| 1364 | |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 1365 | /* Potentially unmap the VM from this ITS */ |
| 1366 | its_unmap_vm(its_dev->its, its_dev->event_map.vm); |
| 1367 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1368 | /* |
| 1369 | * Drop the refcount and make the device available again if |
| 1370 | * this was the last VLPI. |
| 1371 | */ |
| 1372 | if (!--its_dev->event_map.nr_vlpis) { |
| 1373 | its_dev->event_map.vm = NULL; |
| 1374 | kfree(its_dev->event_map.vlpi_maps); |
| 1375 | } |
| 1376 | |
| 1377 | out: |
| 1378 | mutex_unlock(&its_dev->event_map.vlpi_lock); |
| 1379 | return ret; |
| 1380 | } |
| 1381 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1382 | static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info) |
| 1383 | { |
| 1384 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1385 | |
| 1386 | if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) |
| 1387 | return -EINVAL; |
| 1388 | |
| 1389 | if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI) |
| 1390 | lpi_update_config(d, 0xff, info->config); |
| 1391 | else |
| 1392 | lpi_write_config(d, 0xff, info->config); |
| 1393 | its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED)); |
| 1394 | |
| 1395 | return 0; |
| 1396 | } |
| 1397 | |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1398 | static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) |
| 1399 | { |
| 1400 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1401 | struct its_cmd_info *info = vcpu_info; |
| 1402 | |
| 1403 | /* Need a v4 ITS */ |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1404 | if (!its_dev->its->is_v4) |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1405 | return -EINVAL; |
| 1406 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1407 | /* Unmap request? */ |
| 1408 | if (!info) |
| 1409 | return its_vlpi_unmap(d); |
| 1410 | |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1411 | switch (info->cmd_type) { |
| 1412 | case MAP_VLPI: |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1413 | return its_vlpi_map(d, info); |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1414 | |
| 1415 | case GET_VLPI: |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1416 | return its_vlpi_get(d, info); |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1417 | |
| 1418 | case PROP_UPDATE_VLPI: |
| 1419 | case PROP_UPDATE_AND_INV_VLPI: |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1420 | return its_vlpi_prop_update(d, info); |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1421 | |
| 1422 | default: |
| 1423 | return -EINVAL; |
| 1424 | } |
| 1425 | } |
| 1426 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1427 | static struct irq_chip its_irq_chip = { |
| 1428 | .name = "ITS", |
| 1429 | .irq_mask = its_mask_irq, |
| 1430 | .irq_unmask = its_unmask_irq, |
Ashok Kumar | 004fa08 | 2016-02-11 05:38:53 -0800 | [diff] [blame] | 1431 | .irq_eoi = irq_chip_eoi_parent, |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1432 | .irq_set_affinity = its_set_affinity, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1433 | .irq_compose_msi_msg = its_irq_compose_msi_msg, |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 1434 | .irq_set_irqchip_state = its_irq_set_irqchip_state, |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1435 | .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1436 | }; |
| 1437 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1438 | |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1439 | /* |
| 1440 | * How we allocate LPIs: |
| 1441 | * |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1442 | * lpi_range_list contains ranges of LPIs that are to available to |
| 1443 | * allocate from. To allocate LPIs, just pick the first range that |
| 1444 | * fits the required allocation, and reduce it by the required |
| 1445 | * amount. Once empty, remove the range from the list. |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1446 | * |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1447 | * To free a range of LPIs, add a free range to the list, sort it and |
| 1448 | * merge the result if the new range happens to be adjacent to an |
| 1449 | * already free block. |
| 1450 | * |
| 1451 | * The consequence of the above is that allocation is cost is low, but |
| 1452 | * freeing is expensive. We assumes that freeing rarely occurs. |
| 1453 | */ |
Jia He | 4cb205c | 2018-08-28 12:53:26 +0800 | [diff] [blame] | 1454 | #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */ |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1455 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1456 | static DEFINE_MUTEX(lpi_range_lock); |
| 1457 | static LIST_HEAD(lpi_range_list); |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1458 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1459 | struct lpi_range { |
| 1460 | struct list_head entry; |
| 1461 | u32 base_id; |
| 1462 | u32 span; |
| 1463 | }; |
| 1464 | |
| 1465 | static struct lpi_range *mk_lpi_range(u32 base, u32 span) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1466 | { |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1467 | struct lpi_range *range; |
| 1468 | |
Rasmus Villemoes | 1c73fac | 2019-03-12 18:33:48 +0100 | [diff] [blame] | 1469 | range = kmalloc(sizeof(*range), GFP_KERNEL); |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1470 | if (range) { |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1471 | range->base_id = base; |
| 1472 | range->span = span; |
| 1473 | } |
| 1474 | |
| 1475 | return range; |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1476 | } |
| 1477 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1478 | static int alloc_lpi_range(u32 nr_lpis, u32 *base) |
| 1479 | { |
| 1480 | struct lpi_range *range, *tmp; |
| 1481 | int err = -ENOSPC; |
| 1482 | |
| 1483 | mutex_lock(&lpi_range_lock); |
| 1484 | |
| 1485 | list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) { |
| 1486 | if (range->span >= nr_lpis) { |
| 1487 | *base = range->base_id; |
| 1488 | range->base_id += nr_lpis; |
| 1489 | range->span -= nr_lpis; |
| 1490 | |
| 1491 | if (range->span == 0) { |
| 1492 | list_del(&range->entry); |
| 1493 | kfree(range); |
| 1494 | } |
| 1495 | |
| 1496 | err = 0; |
| 1497 | break; |
| 1498 | } |
| 1499 | } |
| 1500 | |
| 1501 | mutex_unlock(&lpi_range_lock); |
| 1502 | |
| 1503 | pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis); |
| 1504 | return err; |
| 1505 | } |
| 1506 | |
Rasmus Villemoes | 12eade1 | 2019-03-12 18:33:49 +0100 | [diff] [blame] | 1507 | static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b) |
| 1508 | { |
| 1509 | if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list) |
| 1510 | return; |
| 1511 | if (a->base_id + a->span != b->base_id) |
| 1512 | return; |
| 1513 | b->base_id = a->base_id; |
| 1514 | b->span += a->span; |
| 1515 | list_del(&a->entry); |
| 1516 | kfree(a); |
| 1517 | } |
| 1518 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1519 | static int free_lpi_range(u32 base, u32 nr_lpis) |
| 1520 | { |
Rasmus Villemoes | 12eade1 | 2019-03-12 18:33:49 +0100 | [diff] [blame] | 1521 | struct lpi_range *new, *old; |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1522 | |
| 1523 | new = mk_lpi_range(base, nr_lpis); |
Rasmus Villemoes | b31a383 | 2019-03-12 18:33:47 +0100 | [diff] [blame] | 1524 | if (!new) |
| 1525 | return -ENOMEM; |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1526 | |
| 1527 | mutex_lock(&lpi_range_lock); |
| 1528 | |
Rasmus Villemoes | 12eade1 | 2019-03-12 18:33:49 +0100 | [diff] [blame] | 1529 | list_for_each_entry_reverse(old, &lpi_range_list, entry) { |
| 1530 | if (old->base_id < base) |
| 1531 | break; |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1532 | } |
Rasmus Villemoes | 12eade1 | 2019-03-12 18:33:49 +0100 | [diff] [blame] | 1533 | /* |
| 1534 | * old is the last element with ->base_id smaller than base, |
| 1535 | * so new goes right after it. If there are no elements with |
| 1536 | * ->base_id smaller than base, &old->entry ends up pointing |
| 1537 | * at the head of the list, and inserting new it the start of |
| 1538 | * the list is the right thing to do in that case as well. |
| 1539 | */ |
| 1540 | list_add(&new->entry, &old->entry); |
| 1541 | /* |
| 1542 | * Now check if we can merge with the preceding and/or |
| 1543 | * following ranges. |
| 1544 | */ |
| 1545 | merge_lpi_ranges(old, new); |
| 1546 | merge_lpi_ranges(new, list_next_entry(new, entry)); |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1547 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1548 | mutex_unlock(&lpi_range_lock); |
Rasmus Villemoes | b31a383 | 2019-03-12 18:33:47 +0100 | [diff] [blame] | 1549 | return 0; |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1550 | } |
| 1551 | |
Tomasz Nowicki | 04a0e4d | 2016-01-19 14:11:18 +0100 | [diff] [blame] | 1552 | static int __init its_lpi_init(u32 id_bits) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1553 | { |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1554 | u32 lpis = (1UL << id_bits) - 8192; |
Marc Zyngier | 12b2905 | 2018-05-31 09:01:59 +0100 | [diff] [blame] | 1555 | u32 numlpis; |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1556 | int err; |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1557 | |
Marc Zyngier | 12b2905 | 2018-05-31 09:01:59 +0100 | [diff] [blame] | 1558 | numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer); |
| 1559 | |
| 1560 | if (numlpis > 2 && !WARN_ON(numlpis > lpis)) { |
| 1561 | lpis = numlpis; |
| 1562 | pr_info("ITS: Using hypervisor restricted LPI range [%u]\n", |
| 1563 | lpis); |
| 1564 | } |
| 1565 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1566 | /* |
| 1567 | * Initializing the allocator is just the same as freeing the |
| 1568 | * full range of LPIs. |
| 1569 | */ |
| 1570 | err = free_lpi_range(8192, lpis); |
| 1571 | pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis); |
| 1572 | return err; |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 1575 | 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] | 1576 | { |
| 1577 | unsigned long *bitmap = NULL; |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1578 | int err = 0; |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1579 | |
| 1580 | do { |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 1581 | err = alloc_lpi_range(nr_irqs, base); |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1582 | if (!err) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1583 | break; |
| 1584 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 1585 | nr_irqs /= 2; |
| 1586 | } while (nr_irqs > 0); |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1587 | |
Marc Zyngier | 45725e0 | 2019-01-29 15:19:23 +0000 | [diff] [blame] | 1588 | if (!nr_irqs) |
| 1589 | err = -ENOSPC; |
| 1590 | |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1591 | if (err) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1592 | goto out; |
| 1593 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 1594 | bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC); |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1595 | if (!bitmap) |
| 1596 | goto out; |
| 1597 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 1598 | *nr_ids = nr_irqs; |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1599 | |
| 1600 | out: |
Marc Zyngier | c8415b9 | 2015-10-02 16:44:05 +0100 | [diff] [blame] | 1601 | if (!bitmap) |
| 1602 | *base = *nr_ids = 0; |
| 1603 | |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1604 | return bitmap; |
| 1605 | } |
| 1606 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 1607 | 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] | 1608 | { |
Marc Zyngier | 880cb3c | 2018-05-27 16:14:15 +0100 | [diff] [blame] | 1609 | WARN_ON(free_lpi_range(base, nr_ids)); |
Marc Zyngier | cf2be8b | 2016-12-19 18:49:59 +0000 | [diff] [blame] | 1610 | kfree(bitmap); |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1611 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1612 | |
Marc Zyngier | 053be48 | 2018-07-27 15:02:27 +0100 | [diff] [blame] | 1613 | static void gic_reset_prop_table(void *va) |
| 1614 | { |
| 1615 | /* Priority 0xa0, Group-1, disabled */ |
| 1616 | memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ); |
| 1617 | |
| 1618 | /* Make sure the GIC will observe the written configuration */ |
| 1619 | gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ); |
| 1620 | } |
| 1621 | |
Marc Zyngier | 0e5ccf9 | 2016-12-19 18:15:05 +0000 | [diff] [blame] | 1622 | static struct page *its_allocate_prop_table(gfp_t gfp_flags) |
| 1623 | { |
| 1624 | struct page *prop_page; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1625 | |
Marc Zyngier | 0e5ccf9 | 2016-12-19 18:15:05 +0000 | [diff] [blame] | 1626 | prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ)); |
| 1627 | if (!prop_page) |
| 1628 | return NULL; |
| 1629 | |
Marc Zyngier | 053be48 | 2018-07-27 15:02:27 +0100 | [diff] [blame] | 1630 | gic_reset_prop_table(page_address(prop_page)); |
Marc Zyngier | 0e5ccf9 | 2016-12-19 18:15:05 +0000 | [diff] [blame] | 1631 | |
| 1632 | return prop_page; |
| 1633 | } |
| 1634 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 1635 | static void its_free_prop_table(struct page *prop_page) |
| 1636 | { |
| 1637 | free_pages((unsigned long)page_address(prop_page), |
| 1638 | get_order(LPI_PROPBASE_SZ)); |
| 1639 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1640 | |
Marc Zyngier | 5e2c9f9 | 2018-07-27 16:23:18 +0100 | [diff] [blame] | 1641 | static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size) |
| 1642 | { |
| 1643 | phys_addr_t start, end, addr_end; |
| 1644 | u64 i; |
| 1645 | |
| 1646 | /* |
| 1647 | * We don't bother checking for a kdump kernel as by |
| 1648 | * construction, the LPI tables are out of this kernel's |
| 1649 | * memory map. |
| 1650 | */ |
| 1651 | if (is_kdump_kernel()) |
| 1652 | return true; |
| 1653 | |
| 1654 | addr_end = addr + size - 1; |
| 1655 | |
| 1656 | for_each_reserved_mem_region(i, &start, &end) { |
| 1657 | if (addr >= start && addr_end <= end) |
| 1658 | return true; |
| 1659 | } |
| 1660 | |
| 1661 | /* Not found, not a good sign... */ |
| 1662 | pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n", |
| 1663 | &addr, &addr_end); |
| 1664 | add_taint(TAINT_CRAP, LOCKDEP_STILL_OK); |
| 1665 | return false; |
| 1666 | } |
| 1667 | |
Marc Zyngier | 3fb68fa | 2018-07-27 16:21:18 +0100 | [diff] [blame] | 1668 | static int gic_reserve_range(phys_addr_t addr, unsigned long size) |
| 1669 | { |
| 1670 | if (efi_enabled(EFI_CONFIG_TABLES)) |
| 1671 | return efi_mem_reserve_persistent(addr, size); |
| 1672 | |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 1676 | static int __init its_setup_lpi_prop_table(void) |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1677 | { |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 1678 | if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) { |
| 1679 | u64 val; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1680 | |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 1681 | val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER); |
| 1682 | lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1; |
| 1683 | |
| 1684 | gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12); |
| 1685 | gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa, |
| 1686 | LPI_PROPBASE_SZ, |
| 1687 | MEMREMAP_WB); |
| 1688 | gic_reset_prop_table(gic_rdists->prop_table_va); |
| 1689 | } else { |
| 1690 | struct page *page; |
| 1691 | |
| 1692 | lpi_id_bits = min_t(u32, |
| 1693 | GICD_TYPER_ID_BITS(gic_rdists->gicd_typer), |
| 1694 | ITS_MAX_LPI_NRBITS); |
| 1695 | page = its_allocate_prop_table(GFP_NOWAIT); |
| 1696 | if (!page) { |
| 1697 | pr_err("Failed to allocate PROPBASE\n"); |
| 1698 | return -ENOMEM; |
| 1699 | } |
| 1700 | |
| 1701 | gic_rdists->prop_table_pa = page_to_phys(page); |
| 1702 | gic_rdists->prop_table_va = page_address(page); |
Marc Zyngier | 3fb68fa | 2018-07-27 16:21:18 +0100 | [diff] [blame] | 1703 | WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa, |
| 1704 | LPI_PROPBASE_SZ)); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1705 | } |
| 1706 | |
Marc Zyngier | e1a2e20 | 2018-07-27 14:36:00 +0100 | [diff] [blame] | 1707 | pr_info("GICv3: using LPI property table @%pa\n", |
| 1708 | &gic_rdists->prop_table_pa); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1709 | |
Shanker Donthineni | 6c31e12 | 2017-06-22 18:19:14 -0500 | [diff] [blame] | 1710 | return its_lpi_init(lpi_id_bits); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | static const char *its_base_type_string[] = { |
| 1714 | [GITS_BASER_TYPE_DEVICE] = "Devices", |
| 1715 | [GITS_BASER_TYPE_VCPU] = "Virtual CPUs", |
Marc Zyngier | 4f46de9 | 2016-12-20 15:50:14 +0000 | [diff] [blame] | 1716 | [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)", |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1717 | [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections", |
| 1718 | [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)", |
| 1719 | [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)", |
| 1720 | [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)", |
| 1721 | }; |
| 1722 | |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 1723 | static u64 its_read_baser(struct its_node *its, struct its_baser *baser) |
| 1724 | { |
| 1725 | u32 idx = baser - its->tables; |
| 1726 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1727 | return gits_read_baser(its->base + GITS_BASER + (idx << 3)); |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 1728 | } |
| 1729 | |
| 1730 | static void its_write_baser(struct its_node *its, struct its_baser *baser, |
| 1731 | u64 val) |
| 1732 | { |
| 1733 | u32 idx = baser - its->tables; |
| 1734 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1735 | gits_write_baser(val, its->base + GITS_BASER + (idx << 3)); |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 1736 | baser->val = its_read_baser(its, baser); |
| 1737 | } |
| 1738 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1739 | static int its_setup_baser(struct its_node *its, struct its_baser *baser, |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1740 | u64 cache, u64 shr, u32 psz, u32 order, |
| 1741 | bool indirect) |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1742 | { |
| 1743 | u64 val = its_read_baser(its, baser); |
| 1744 | u64 esz = GITS_BASER_ENTRY_SIZE(val); |
| 1745 | u64 type = GITS_BASER_TYPE(val); |
Shanker Donthineni | 30ae961 | 2017-10-09 11:46:55 -0500 | [diff] [blame] | 1746 | u64 baser_phys, tmp; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1747 | u32 alloc_pages; |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 1748 | struct page *page; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1749 | void *base; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1750 | |
| 1751 | retry_alloc_baser: |
| 1752 | alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz); |
| 1753 | if (alloc_pages > GITS_BASER_PAGES_MAX) { |
| 1754 | pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n", |
| 1755 | &its->phys_base, its_base_type_string[type], |
| 1756 | alloc_pages, GITS_BASER_PAGES_MAX); |
| 1757 | alloc_pages = GITS_BASER_PAGES_MAX; |
| 1758 | order = get_order(GITS_BASER_PAGES_MAX * psz); |
| 1759 | } |
| 1760 | |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 1761 | page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order); |
| 1762 | if (!page) |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1763 | return -ENOMEM; |
| 1764 | |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 1765 | base = (void *)page_address(page); |
Shanker Donthineni | 30ae961 | 2017-10-09 11:46:55 -0500 | [diff] [blame] | 1766 | baser_phys = virt_to_phys(base); |
| 1767 | |
| 1768 | /* Check if the physical address of the memory is above 48bits */ |
| 1769 | if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) { |
| 1770 | |
| 1771 | /* 52bit PA is supported only when PageSize=64K */ |
| 1772 | if (psz != SZ_64K) { |
| 1773 | pr_err("ITS: no 52bit PA support when psz=%d\n", psz); |
| 1774 | free_pages((unsigned long)base, order); |
| 1775 | return -ENXIO; |
| 1776 | } |
| 1777 | |
| 1778 | /* Convert 52bit PA to 48bit field */ |
| 1779 | baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys); |
| 1780 | } |
| 1781 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1782 | retry_baser: |
Shanker Donthineni | 30ae961 | 2017-10-09 11:46:55 -0500 | [diff] [blame] | 1783 | val = (baser_phys | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1784 | (type << GITS_BASER_TYPE_SHIFT) | |
| 1785 | ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) | |
| 1786 | ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) | |
| 1787 | cache | |
| 1788 | shr | |
| 1789 | GITS_BASER_VALID); |
| 1790 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1791 | val |= indirect ? GITS_BASER_INDIRECT : 0x0; |
| 1792 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1793 | switch (psz) { |
| 1794 | case SZ_4K: |
| 1795 | val |= GITS_BASER_PAGE_SIZE_4K; |
| 1796 | break; |
| 1797 | case SZ_16K: |
| 1798 | val |= GITS_BASER_PAGE_SIZE_16K; |
| 1799 | break; |
| 1800 | case SZ_64K: |
| 1801 | val |= GITS_BASER_PAGE_SIZE_64K; |
| 1802 | break; |
| 1803 | } |
| 1804 | |
| 1805 | its_write_baser(its, baser, val); |
| 1806 | tmp = baser->val; |
| 1807 | |
| 1808 | if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) { |
| 1809 | /* |
| 1810 | * Shareability didn't stick. Just use |
| 1811 | * whatever the read reported, which is likely |
| 1812 | * to be the only thing this redistributor |
| 1813 | * supports. If that's zero, make it |
| 1814 | * non-cacheable as well. |
| 1815 | */ |
| 1816 | shr = tmp & GITS_BASER_SHAREABILITY_MASK; |
| 1817 | if (!shr) { |
| 1818 | cache = GITS_BASER_nC; |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 1819 | gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order)); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1820 | } |
| 1821 | goto retry_baser; |
| 1822 | } |
| 1823 | |
| 1824 | if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) { |
| 1825 | /* |
| 1826 | * Page size didn't stick. Let's try a smaller |
| 1827 | * size and retry. If we reach 4K, then |
| 1828 | * something is horribly wrong... |
| 1829 | */ |
| 1830 | free_pages((unsigned long)base, order); |
| 1831 | baser->base = NULL; |
| 1832 | |
| 1833 | switch (psz) { |
| 1834 | case SZ_16K: |
| 1835 | psz = SZ_4K; |
| 1836 | goto retry_alloc_baser; |
| 1837 | case SZ_64K: |
| 1838 | psz = SZ_16K; |
| 1839 | goto retry_alloc_baser; |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | if (val != tmp) { |
Vladimir Murzin | b11283e | 2016-11-02 11:54:03 +0000 | [diff] [blame] | 1844 | pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n", |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1845 | &its->phys_base, its_base_type_string[type], |
Vladimir Murzin | b11283e | 2016-11-02 11:54:03 +0000 | [diff] [blame] | 1846 | val, tmp); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1847 | free_pages((unsigned long)base, order); |
| 1848 | return -ENXIO; |
| 1849 | } |
| 1850 | |
| 1851 | baser->order = order; |
| 1852 | baser->base = base; |
| 1853 | baser->psz = psz; |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1854 | tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1855 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1856 | 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] | 1857 | &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp), |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1858 | its_base_type_string[type], |
| 1859 | (unsigned long)virt_to_phys(base), |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1860 | indirect ? "indirect" : "flat", (int)esz, |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1861 | psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT); |
| 1862 | |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1866 | static bool its_parse_indirect_baser(struct its_node *its, |
| 1867 | struct its_baser *baser, |
Shanker Donthineni | 32bd44d | 2017-10-07 15:43:48 -0500 | [diff] [blame] | 1868 | u32 psz, u32 *order, u32 ids) |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1869 | { |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1870 | u64 tmp = its_read_baser(its, baser); |
| 1871 | u64 type = GITS_BASER_TYPE(tmp); |
| 1872 | u64 esz = GITS_BASER_ENTRY_SIZE(tmp); |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 1873 | u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb; |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1874 | u32 new_order = *order; |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1875 | bool indirect = false; |
| 1876 | |
| 1877 | /* No need to enable Indirection if memory requirement < (psz*2)bytes */ |
| 1878 | if ((esz << ids) > (psz * 2)) { |
| 1879 | /* |
| 1880 | * Find out whether hw supports a single or two-level table by |
| 1881 | * table by reading bit at offset '62' after writing '1' to it. |
| 1882 | */ |
| 1883 | its_write_baser(its, baser, val | GITS_BASER_INDIRECT); |
| 1884 | indirect = !!(baser->val & GITS_BASER_INDIRECT); |
| 1885 | |
| 1886 | if (indirect) { |
| 1887 | /* |
| 1888 | * The size of the lvl2 table is equal to ITS page size |
| 1889 | * which is 'psz'. For computing lvl1 table size, |
| 1890 | * subtract ID bits that sparse lvl2 table from 'ids' |
| 1891 | * which is reported by ITS hardware times lvl1 table |
| 1892 | * entry size. |
| 1893 | */ |
Vladimir Murzin | d524eaa | 2016-11-02 11:54:04 +0000 | [diff] [blame] | 1894 | ids -= ilog2(psz / (int)esz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1895 | esz = GITS_LVL1_ENTRY_SIZE; |
| 1896 | } |
| 1897 | } |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1898 | |
| 1899 | /* |
| 1900 | * Allocate as many entries as required to fit the |
| 1901 | * range of device IDs that the ITS can grok... The ID |
| 1902 | * space being incredibly sparse, this results in a |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1903 | * massive waste of memory if two-level device table |
| 1904 | * feature is not supported by hardware. |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1905 | */ |
| 1906 | new_order = max_t(u32, get_order(esz << ids), new_order); |
| 1907 | if (new_order >= MAX_ORDER) { |
| 1908 | new_order = MAX_ORDER - 1; |
Vladimir Murzin | d524eaa | 2016-11-02 11:54:04 +0000 | [diff] [blame] | 1909 | ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz); |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1910 | pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n", |
| 1911 | &its->phys_base, its_base_type_string[type], |
| 1912 | its->device_ids, ids); |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | *order = new_order; |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1916 | |
| 1917 | return indirect; |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1918 | } |
| 1919 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1920 | static void its_free_tables(struct its_node *its) |
| 1921 | { |
| 1922 | int i; |
| 1923 | |
| 1924 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
Shanker Donthineni | 1a485f4 | 2016-02-01 20:19:44 -0600 | [diff] [blame] | 1925 | if (its->tables[i].base) { |
| 1926 | free_pages((unsigned long)its->tables[i].base, |
| 1927 | its->tables[i].order); |
| 1928 | its->tables[i].base = NULL; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1929 | } |
| 1930 | } |
| 1931 | } |
| 1932 | |
Shanker Donthineni | 0e0b0f6 | 2016-06-06 18:17:31 -0500 | [diff] [blame] | 1933 | static int its_alloc_tables(struct its_node *its) |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1934 | { |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1935 | u64 shr = GITS_BASER_InnerShareable; |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 1936 | u64 cache = GITS_BASER_RaWaWb; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1937 | u32 psz = SZ_64K; |
| 1938 | int err, i; |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 1939 | |
Ard Biesheuvel | fa15001 | 2017-10-17 17:55:54 +0100 | [diff] [blame] | 1940 | if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) |
| 1941 | /* erratum 24313: ignore memory access type */ |
| 1942 | cache = GITS_BASER_nCnB; |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 1943 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1944 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 1945 | struct its_baser *baser = its->tables + i; |
| 1946 | u64 val = its_read_baser(its, baser); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1947 | u64 type = GITS_BASER_TYPE(val); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1948 | u32 order = get_order(psz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1949 | bool indirect = false; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1950 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1951 | switch (type) { |
| 1952 | case GITS_BASER_TYPE_NONE: |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1953 | continue; |
| 1954 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1955 | case GITS_BASER_TYPE_DEVICE: |
Shanker Donthineni | 32bd44d | 2017-10-07 15:43:48 -0500 | [diff] [blame] | 1956 | indirect = its_parse_indirect_baser(its, baser, |
| 1957 | psz, &order, |
| 1958 | its->device_ids); |
Zenghui Yu | 8d56574 | 2019-02-10 05:24:10 +0000 | [diff] [blame] | 1959 | break; |
| 1960 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1961 | case GITS_BASER_TYPE_VCPU: |
| 1962 | indirect = its_parse_indirect_baser(its, baser, |
Shanker Donthineni | 32bd44d | 2017-10-07 15:43:48 -0500 | [diff] [blame] | 1963 | psz, &order, |
| 1964 | ITS_MAX_VPEID_BITS); |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1965 | break; |
| 1966 | } |
Marc Zyngier | f54b97e | 2015-03-06 16:37:41 +0000 | [diff] [blame] | 1967 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1968 | err = its_setup_baser(its, baser, cache, shr, psz, order, indirect); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1969 | if (err < 0) { |
| 1970 | its_free_tables(its); |
| 1971 | return err; |
Robert Richter | 30f2136 | 2015-09-21 22:58:34 +0200 | [diff] [blame] | 1972 | } |
| 1973 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1974 | /* Update settings which will be used for next BASERn */ |
| 1975 | psz = baser->psz; |
| 1976 | cache = baser->val & GITS_BASER_CACHEABILITY_MASK; |
| 1977 | shr = baser->val & GITS_BASER_SHAREABILITY_MASK; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | return 0; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
| 1983 | static int its_alloc_collections(struct its_node *its) |
| 1984 | { |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 1985 | int i; |
| 1986 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 1987 | its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections), |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1988 | GFP_KERNEL); |
| 1989 | if (!its->collections) |
| 1990 | return -ENOMEM; |
| 1991 | |
Marc Zyngier | 83559b4 | 2018-06-22 10:52:52 +0100 | [diff] [blame] | 1992 | for (i = 0; i < nr_cpu_ids; i++) |
| 1993 | its->collections[i].target_address = ~0ULL; |
| 1994 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1995 | return 0; |
| 1996 | } |
| 1997 | |
Marc Zyngier | 7c297a2 | 2016-12-19 18:34:38 +0000 | [diff] [blame] | 1998 | static struct page *its_allocate_pending_table(gfp_t gfp_flags) |
| 1999 | { |
| 2000 | struct page *pend_page; |
Marc Zyngier | adaab50 | 2018-07-17 18:06:39 +0100 | [diff] [blame] | 2001 | |
Marc Zyngier | 7c297a2 | 2016-12-19 18:34:38 +0000 | [diff] [blame] | 2002 | pend_page = alloc_pages(gfp_flags | __GFP_ZERO, |
Marc Zyngier | adaab50 | 2018-07-17 18:06:39 +0100 | [diff] [blame] | 2003 | get_order(LPI_PENDBASE_SZ)); |
Marc Zyngier | 7c297a2 | 2016-12-19 18:34:38 +0000 | [diff] [blame] | 2004 | if (!pend_page) |
| 2005 | return NULL; |
| 2006 | |
| 2007 | /* Make sure the GIC will observe the zero-ed page */ |
| 2008 | gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ); |
| 2009 | |
| 2010 | return pend_page; |
| 2011 | } |
| 2012 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 2013 | static void its_free_pending_table(struct page *pt) |
| 2014 | { |
Marc Zyngier | adaab50 | 2018-07-17 18:06:39 +0100 | [diff] [blame] | 2015 | free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ)); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 2016 | } |
| 2017 | |
Marc Zyngier | c6e2ccb | 2018-06-26 11:21:11 +0100 | [diff] [blame] | 2018 | /* |
Marc Zyngier | 5e2c9f9 | 2018-07-27 16:23:18 +0100 | [diff] [blame] | 2019 | * Booting with kdump and LPIs enabled is generally fine. Any other |
| 2020 | * case is wrong in the absence of firmware/EFI support. |
Marc Zyngier | c6e2ccb | 2018-06-26 11:21:11 +0100 | [diff] [blame] | 2021 | */ |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2022 | static bool enabled_lpis_allowed(void) |
| 2023 | { |
Marc Zyngier | 5e2c9f9 | 2018-07-27 16:23:18 +0100 | [diff] [blame] | 2024 | phys_addr_t addr; |
| 2025 | u64 val; |
Marc Zyngier | c6e2ccb | 2018-06-26 11:21:11 +0100 | [diff] [blame] | 2026 | |
Marc Zyngier | 5e2c9f9 | 2018-07-27 16:23:18 +0100 | [diff] [blame] | 2027 | /* Check whether the property table is in a reserved region */ |
| 2028 | val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER); |
| 2029 | addr = val & GENMASK_ULL(51, 12); |
| 2030 | |
| 2031 | return gic_check_reserved_range(addr, LPI_PROPBASE_SZ); |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2032 | } |
| 2033 | |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2034 | static int __init allocate_lpi_tables(void) |
| 2035 | { |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2036 | u64 val; |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2037 | int err, cpu; |
| 2038 | |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2039 | /* |
| 2040 | * If LPIs are enabled while we run this from the boot CPU, |
| 2041 | * flag the RD tables as pre-allocated if the stars do align. |
| 2042 | */ |
| 2043 | val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR); |
| 2044 | if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) { |
| 2045 | gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED | |
| 2046 | RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING); |
| 2047 | pr_info("GICv3: Using preallocated redistributor tables\n"); |
| 2048 | } |
| 2049 | |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2050 | err = its_setup_lpi_prop_table(); |
| 2051 | if (err) |
| 2052 | return err; |
| 2053 | |
| 2054 | /* |
| 2055 | * We allocate all the pending tables anyway, as we may have a |
| 2056 | * mix of RDs that have had LPIs enabled, and some that |
| 2057 | * don't. We'll free the unused ones as each CPU comes online. |
| 2058 | */ |
| 2059 | for_each_possible_cpu(cpu) { |
| 2060 | struct page *pend_page; |
| 2061 | |
| 2062 | pend_page = its_allocate_pending_table(GFP_NOWAIT); |
| 2063 | if (!pend_page) { |
| 2064 | pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu); |
| 2065 | return -ENOMEM; |
| 2066 | } |
| 2067 | |
| 2068 | gic_data_rdist_cpu(cpu)->pend_page = pend_page; |
| 2069 | } |
| 2070 | |
| 2071 | return 0; |
| 2072 | } |
| 2073 | |
Heyi Guo | 6479450 | 2019-01-24 21:37:08 +0800 | [diff] [blame] | 2074 | static u64 its_clear_vpend_valid(void __iomem *vlpi_base) |
| 2075 | { |
| 2076 | u32 count = 1000000; /* 1s! */ |
| 2077 | bool clean; |
| 2078 | u64 val; |
| 2079 | |
| 2080 | val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER); |
| 2081 | val &= ~GICR_VPENDBASER_Valid; |
| 2082 | gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); |
| 2083 | |
| 2084 | do { |
| 2085 | val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER); |
| 2086 | clean = !(val & GICR_VPENDBASER_Dirty); |
| 2087 | if (!clean) { |
| 2088 | count--; |
| 2089 | cpu_relax(); |
| 2090 | udelay(1); |
| 2091 | } |
| 2092 | } while (!clean && count); |
| 2093 | |
| 2094 | return val; |
| 2095 | } |
| 2096 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2097 | static void its_cpu_init_lpis(void) |
| 2098 | { |
| 2099 | void __iomem *rbase = gic_data_rdist_rd_base(); |
| 2100 | struct page *pend_page; |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2101 | phys_addr_t paddr; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2102 | u64 val, tmp; |
| 2103 | |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2104 | if (gic_data_rdist()->lpi_enabled) |
| 2105 | return; |
| 2106 | |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2107 | val = readl_relaxed(rbase + GICR_CTLR); |
| 2108 | if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) && |
| 2109 | (val & GICR_CTLR_ENABLE_LPIS)) { |
Marc Zyngier | f842ca8 | 2018-07-27 16:03:31 +0100 | [diff] [blame] | 2110 | /* |
| 2111 | * Check that we get the same property table on all |
| 2112 | * RDs. If we don't, this is hopeless. |
| 2113 | */ |
| 2114 | paddr = gicr_read_propbaser(rbase + GICR_PROPBASER); |
| 2115 | paddr &= GENMASK_ULL(51, 12); |
| 2116 | if (WARN_ON(gic_rdists->prop_table_pa != paddr)) |
| 2117 | add_taint(TAINT_CRAP, LOCKDEP_STILL_OK); |
| 2118 | |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2119 | paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER); |
| 2120 | paddr &= GENMASK_ULL(51, 16); |
| 2121 | |
Marc Zyngier | 5e2c9f9 | 2018-07-27 16:23:18 +0100 | [diff] [blame] | 2122 | WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ)); |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2123 | its_free_pending_table(gic_data_rdist()->pend_page); |
| 2124 | gic_data_rdist()->pend_page = NULL; |
| 2125 | |
| 2126 | goto out; |
| 2127 | } |
| 2128 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2129 | pend_page = gic_data_rdist()->pend_page; |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2130 | paddr = page_to_phys(pend_page); |
Marc Zyngier | 3fb68fa | 2018-07-27 16:21:18 +0100 | [diff] [blame] | 2131 | WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ)); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2132 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2133 | /* set PROPBASE */ |
Marc Zyngier | e1a2e20 | 2018-07-27 14:36:00 +0100 | [diff] [blame] | 2134 | val = (gic_rdists->prop_table_pa | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2135 | GICR_PROPBASER_InnerShareable | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 2136 | GICR_PROPBASER_RaWaWb | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2137 | ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK)); |
| 2138 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2139 | gicr_write_propbaser(val, rbase + GICR_PROPBASER); |
| 2140 | tmp = gicr_read_propbaser(rbase + GICR_PROPBASER); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2141 | |
| 2142 | if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) { |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2143 | if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) { |
| 2144 | /* |
| 2145 | * The HW reports non-shareable, we must |
| 2146 | * remove the cacheability attributes as |
| 2147 | * well. |
| 2148 | */ |
| 2149 | val &= ~(GICR_PROPBASER_SHAREABILITY_MASK | |
| 2150 | GICR_PROPBASER_CACHEABILITY_MASK); |
| 2151 | val |= GICR_PROPBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2152 | gicr_write_propbaser(val, rbase + GICR_PROPBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2153 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2154 | pr_info_once("GIC: using cache flushing for LPI property table\n"); |
| 2155 | gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING; |
| 2156 | } |
| 2157 | |
| 2158 | /* set PENDBASE */ |
| 2159 | val = (page_to_phys(pend_page) | |
Marc Zyngier | 4ad3e36 | 2015-03-27 14:15:04 +0000 | [diff] [blame] | 2160 | GICR_PENDBASER_InnerShareable | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 2161 | GICR_PENDBASER_RaWaWb); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2162 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2163 | gicr_write_pendbaser(val, rbase + GICR_PENDBASER); |
| 2164 | tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2165 | |
| 2166 | if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) { |
| 2167 | /* |
| 2168 | * The HW reports non-shareable, we must remove the |
| 2169 | * cacheability attributes as well. |
| 2170 | */ |
| 2171 | val &= ~(GICR_PENDBASER_SHAREABILITY_MASK | |
| 2172 | GICR_PENDBASER_CACHEABILITY_MASK); |
| 2173 | val |= GICR_PENDBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2174 | gicr_write_pendbaser(val, rbase + GICR_PENDBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2175 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2176 | |
| 2177 | /* Enable LPIs */ |
| 2178 | val = readl_relaxed(rbase + GICR_CTLR); |
| 2179 | val |= GICR_CTLR_ENABLE_LPIS; |
| 2180 | writel_relaxed(val, rbase + GICR_CTLR); |
| 2181 | |
Heyi Guo | 6479450 | 2019-01-24 21:37:08 +0800 | [diff] [blame] | 2182 | if (gic_rdists->has_vlpis) { |
| 2183 | void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); |
| 2184 | |
| 2185 | /* |
| 2186 | * It's possible for CPU to receive VLPIs before it is |
| 2187 | * sheduled as a vPE, especially for the first CPU, and the |
| 2188 | * VLPI with INTID larger than 2^(IDbits+1) will be considered |
| 2189 | * as out of range and dropped by GIC. |
| 2190 | * So we initialize IDbits to known value to avoid VLPI drop. |
| 2191 | */ |
| 2192 | val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK; |
| 2193 | pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n", |
| 2194 | smp_processor_id(), val); |
| 2195 | gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); |
| 2196 | |
| 2197 | /* |
| 2198 | * Also clear Valid bit of GICR_VPENDBASER, in case some |
| 2199 | * ancient programming gets left in and has possibility of |
| 2200 | * corrupting memory. |
| 2201 | */ |
| 2202 | val = its_clear_vpend_valid(vlpi_base); |
| 2203 | WARN_ON(val & GICR_VPENDBASER_Dirty); |
| 2204 | } |
| 2205 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2206 | /* Make sure the GIC has seen the above */ |
| 2207 | dsb(sy); |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2208 | out: |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2209 | gic_data_rdist()->lpi_enabled = true; |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2210 | pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n", |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2211 | smp_processor_id(), |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 2212 | gic_data_rdist()->pend_page ? "allocated" : "reserved", |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 2213 | &paddr); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2216 | static void its_cpu_init_collection(struct its_node *its) |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2217 | { |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2218 | int cpu = smp_processor_id(); |
| 2219 | u64 target; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2220 | |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2221 | /* avoid cross node collections and its mapping */ |
| 2222 | if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) { |
| 2223 | struct device_node *cpu_node; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2224 | |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2225 | cpu_node = of_get_cpu_node(cpu, NULL); |
| 2226 | if (its->numa_node != NUMA_NO_NODE && |
| 2227 | its->numa_node != of_node_to_nid(cpu_node)) |
| 2228 | return; |
| 2229 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2230 | |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2231 | /* |
| 2232 | * We now have to bind each collection to its target |
| 2233 | * redistributor. |
| 2234 | */ |
| 2235 | if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) { |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2236 | /* |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2237 | * This ITS wants the physical address of the |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2238 | * redistributor. |
| 2239 | */ |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2240 | target = gic_data_rdist()->phys_base; |
| 2241 | } else { |
| 2242 | /* This ITS wants a linear CPU number. */ |
| 2243 | target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER); |
| 2244 | target = GICR_TYPER_CPU_NUMBER(target) << 16; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2247 | /* Perform collection mapping */ |
| 2248 | its->collections[cpu].target_address = target; |
| 2249 | its->collections[cpu].col_id = cpu; |
| 2250 | |
| 2251 | its_send_mapc(its, &its->collections[cpu], 1); |
| 2252 | its_send_invall(its, &its->collections[cpu]); |
| 2253 | } |
| 2254 | |
| 2255 | static void its_cpu_init_collections(void) |
| 2256 | { |
| 2257 | struct its_node *its; |
| 2258 | |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 2259 | raw_spin_lock(&its_lock); |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 2260 | |
| 2261 | list_for_each_entry(its, &its_nodes, entry) |
| 2262 | its_cpu_init_collection(its); |
| 2263 | |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 2264 | raw_spin_unlock(&its_lock); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 2265 | } |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2266 | |
| 2267 | static struct its_device *its_find_device(struct its_node *its, u32 dev_id) |
| 2268 | { |
| 2269 | struct its_device *its_dev = NULL, *tmp; |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2270 | unsigned long flags; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2271 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2272 | raw_spin_lock_irqsave(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2273 | |
| 2274 | list_for_each_entry(tmp, &its->its_device_list, entry) { |
| 2275 | if (tmp->device_id == dev_id) { |
| 2276 | its_dev = tmp; |
| 2277 | break; |
| 2278 | } |
| 2279 | } |
| 2280 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2281 | raw_spin_unlock_irqrestore(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2282 | |
| 2283 | return its_dev; |
| 2284 | } |
| 2285 | |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 2286 | static struct its_baser *its_get_baser(struct its_node *its, u32 type) |
| 2287 | { |
| 2288 | int i; |
| 2289 | |
| 2290 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
| 2291 | if (GITS_BASER_TYPE(its->tables[i].val) == type) |
| 2292 | return &its->tables[i]; |
| 2293 | } |
| 2294 | |
| 2295 | return NULL; |
| 2296 | } |
| 2297 | |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 2298 | static bool its_alloc_table_entry(struct its_node *its, |
| 2299 | struct its_baser *baser, u32 id) |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2300 | { |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2301 | struct page *page; |
| 2302 | u32 esz, idx; |
| 2303 | __le64 *table; |
| 2304 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2305 | /* Don't allow device id that exceeds single, flat table limit */ |
| 2306 | esz = GITS_BASER_ENTRY_SIZE(baser->val); |
| 2307 | if (!(baser->val & GITS_BASER_INDIRECT)) |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 2308 | return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz)); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2309 | |
| 2310 | /* Compute 1st level table index & check if that exceeds table limit */ |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 2311 | idx = id >> ilog2(baser->psz / esz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2312 | if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE)) |
| 2313 | return false; |
| 2314 | |
| 2315 | table = baser->base; |
| 2316 | |
| 2317 | /* Allocate memory for 2nd level table */ |
| 2318 | if (!table[idx]) { |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 2319 | page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, |
| 2320 | get_order(baser->psz)); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2321 | if (!page) |
| 2322 | return false; |
| 2323 | |
| 2324 | /* Flush Lvl2 table to PoC if hw doesn't support coherency */ |
| 2325 | if (!(baser->val & GITS_BASER_SHAREABILITY_MASK)) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 2326 | gic_flush_dcache_to_poc(page_address(page), baser->psz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2327 | |
| 2328 | table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID); |
| 2329 | |
| 2330 | /* Flush Lvl1 entry to PoC if hw doesn't support coherency */ |
| 2331 | if (!(baser->val & GITS_BASER_SHAREABILITY_MASK)) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 2332 | gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2333 | |
| 2334 | /* Ensure updated table contents are visible to ITS hardware */ |
| 2335 | dsb(sy); |
| 2336 | } |
| 2337 | |
| 2338 | return true; |
| 2339 | } |
| 2340 | |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 2341 | static bool its_alloc_device_table(struct its_node *its, u32 dev_id) |
| 2342 | { |
| 2343 | struct its_baser *baser; |
| 2344 | |
| 2345 | baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE); |
| 2346 | |
| 2347 | /* Don't allow device id that exceeds ITS hardware limit */ |
| 2348 | if (!baser) |
| 2349 | return (ilog2(dev_id) < its->device_ids); |
| 2350 | |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 2351 | return its_alloc_table_entry(its, baser, dev_id); |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 2352 | } |
| 2353 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 2354 | static bool its_alloc_vpe_table(u32 vpe_id) |
| 2355 | { |
| 2356 | struct its_node *its; |
| 2357 | |
| 2358 | /* |
| 2359 | * Make sure the L2 tables are allocated on *all* v4 ITSs. We |
| 2360 | * could try and only do it on ITSs corresponding to devices |
| 2361 | * that have interrupts targeted at this VPE, but the |
| 2362 | * complexity becomes crazy (and you have tons of memory |
| 2363 | * anyway, right?). |
| 2364 | */ |
| 2365 | list_for_each_entry(its, &its_nodes, entry) { |
| 2366 | struct its_baser *baser; |
| 2367 | |
| 2368 | if (!its->is_v4) |
| 2369 | continue; |
| 2370 | |
| 2371 | baser = its_get_baser(its, GITS_BASER_TYPE_VCPU); |
| 2372 | if (!baser) |
| 2373 | return false; |
| 2374 | |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 2375 | if (!its_alloc_table_entry(its, baser, vpe_id)) |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 2376 | return false; |
| 2377 | } |
| 2378 | |
| 2379 | return true; |
| 2380 | } |
| 2381 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2382 | 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] | 2383 | int nvecs, bool alloc_lpis) |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2384 | { |
| 2385 | struct its_device *dev; |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 2386 | unsigned long *lpi_map = NULL; |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2387 | unsigned long flags; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2388 | u16 *col_map = NULL; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2389 | void *itt; |
| 2390 | int lpi_base; |
| 2391 | int nr_lpis; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 2392 | int nr_ites; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2393 | int sz; |
| 2394 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 2395 | if (!its_alloc_device_table(its, dev_id)) |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 2396 | return NULL; |
| 2397 | |
Marc Zyngier | 147c8f3 | 2018-05-27 16:39:55 +0100 | [diff] [blame] | 2398 | if (WARN_ON(!is_power_of_2(nvecs))) |
| 2399 | nvecs = roundup_pow_of_two(nvecs); |
| 2400 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2401 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 2402 | /* |
Marc Zyngier | 147c8f3 | 2018-05-27 16:39:55 +0100 | [diff] [blame] | 2403 | * Even if the device wants a single LPI, the ITT must be |
| 2404 | * 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] | 2405 | */ |
Marc Zyngier | 147c8f3 | 2018-05-27 16:39:55 +0100 | [diff] [blame] | 2406 | nr_ites = max(2, nvecs); |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 2407 | sz = nr_ites * its->ite_size; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2408 | sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1; |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 2409 | itt = kzalloc_node(sz, GFP_KERNEL, its->numa_node); |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 2410 | if (alloc_lpis) { |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 2411 | lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis); |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 2412 | if (lpi_map) |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2413 | col_map = kcalloc(nr_lpis, sizeof(*col_map), |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 2414 | GFP_KERNEL); |
| 2415 | } else { |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2416 | col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL); |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 2417 | nr_lpis = 0; |
| 2418 | lpi_base = 0; |
| 2419 | } |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2420 | |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 2421 | if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) { |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2422 | kfree(dev); |
| 2423 | kfree(itt); |
| 2424 | kfree(lpi_map); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2425 | kfree(col_map); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2426 | return NULL; |
| 2427 | } |
| 2428 | |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 2429 | gic_flush_dcache_to_poc(itt, sz); |
Marc Zyngier | 5a9a891 | 2015-09-13 12:14:32 +0100 | [diff] [blame] | 2430 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2431 | dev->its = its; |
| 2432 | dev->itt = itt; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 2433 | dev->nr_ites = nr_ites; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2434 | dev->event_map.lpi_map = lpi_map; |
| 2435 | dev->event_map.col_map = col_map; |
| 2436 | dev->event_map.lpi_base = lpi_base; |
| 2437 | dev->event_map.nr_lpis = nr_lpis; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 2438 | mutex_init(&dev->event_map.vlpi_lock); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2439 | dev->device_id = dev_id; |
| 2440 | INIT_LIST_HEAD(&dev->entry); |
| 2441 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2442 | raw_spin_lock_irqsave(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2443 | list_add(&dev->entry, &its->its_device_list); |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2444 | raw_spin_unlock_irqrestore(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2445 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2446 | /* Map device to its ITT */ |
| 2447 | its_send_mapd(dev, 1); |
| 2448 | |
| 2449 | return dev; |
| 2450 | } |
| 2451 | |
| 2452 | static void its_free_device(struct its_device *its_dev) |
| 2453 | { |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2454 | unsigned long flags; |
| 2455 | |
| 2456 | raw_spin_lock_irqsave(&its_dev->its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2457 | list_del(&its_dev->entry); |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2458 | raw_spin_unlock_irqrestore(&its_dev->its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2459 | kfree(its_dev->itt); |
| 2460 | kfree(its_dev); |
| 2461 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2462 | |
Marc Zyngier | 8208d17 | 2019-01-18 14:08:59 +0000 | [diff] [blame] | 2463 | 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] | 2464 | { |
| 2465 | int idx; |
| 2466 | |
Zenghui Yu | 342be10 | 2019-07-27 06:14:22 +0000 | [diff] [blame] | 2467 | /* Find a free LPI region in lpi_map and allocate them. */ |
Marc Zyngier | 8208d17 | 2019-01-18 14:08:59 +0000 | [diff] [blame] | 2468 | idx = bitmap_find_free_region(dev->event_map.lpi_map, |
| 2469 | dev->event_map.nr_lpis, |
| 2470 | get_count_order(nvecs)); |
| 2471 | if (idx < 0) |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2472 | return -ENOSPC; |
| 2473 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2474 | *hwirq = dev->event_map.lpi_base + idx; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2475 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2476 | return 0; |
| 2477 | } |
| 2478 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2479 | static int its_msi_prepare(struct irq_domain *domain, struct device *dev, |
| 2480 | int nvec, msi_alloc_info_t *info) |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 2481 | { |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2482 | struct its_node *its; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2483 | struct its_device *its_dev; |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2484 | struct msi_domain_info *msi_info; |
| 2485 | u32 dev_id; |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 2486 | int err = 0; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2487 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2488 | /* |
Julien Grall | a7c90f5 | 2019-04-18 16:58:14 +0100 | [diff] [blame] | 2489 | * We ignore "dev" entirely, and rely on the dev_id that has |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2490 | * been passed via the scratchpad. This limits this domain's |
| 2491 | * usefulness to upper layers that definitely know that they |
| 2492 | * are built on top of the ITS. |
| 2493 | */ |
| 2494 | dev_id = info->scratchpad[0].ul; |
| 2495 | |
| 2496 | msi_info = msi_get_domain_info(domain); |
| 2497 | its = msi_info->data; |
| 2498 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 2499 | if (!gic_rdists->has_direct_lpi && |
| 2500 | vpe_proxy.dev && |
| 2501 | vpe_proxy.dev->its == its && |
| 2502 | dev_id == vpe_proxy.dev->device_id) { |
| 2503 | /* Bad luck. Get yourself a better implementation */ |
| 2504 | WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n", |
| 2505 | dev_id); |
| 2506 | return -EINVAL; |
| 2507 | } |
| 2508 | |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 2509 | mutex_lock(&its->dev_alloc_lock); |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 2510 | its_dev = its_find_device(its, dev_id); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 2511 | if (its_dev) { |
| 2512 | /* |
| 2513 | * We already have seen this ID, probably through |
| 2514 | * another alias (PCI bridge of some sort). No need to |
| 2515 | * create the device. |
| 2516 | */ |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 2517 | its_dev->shared = true; |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 2518 | pr_debug("Reusing ITT for devID %x\n", dev_id); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 2519 | goto out; |
| 2520 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2521 | |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame] | 2522 | its_dev = its_create_device(its, dev_id, nvec, true); |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 2523 | if (!its_dev) { |
| 2524 | err = -ENOMEM; |
| 2525 | goto out; |
| 2526 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2527 | |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 2528 | pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec)); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 2529 | out: |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 2530 | mutex_unlock(&its->dev_alloc_lock); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2531 | info->scratchpad[0].ptr = its_dev; |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 2532 | return err; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2535 | static struct msi_domain_ops its_msi_domain_ops = { |
| 2536 | .msi_prepare = its_msi_prepare, |
| 2537 | }; |
| 2538 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2539 | static int its_irq_gic_domain_alloc(struct irq_domain *domain, |
| 2540 | unsigned int virq, |
| 2541 | irq_hw_number_t hwirq) |
| 2542 | { |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 2543 | struct irq_fwspec fwspec; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2544 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 2545 | if (irq_domain_get_of_node(domain->parent)) { |
| 2546 | fwspec.fwnode = domain->parent->fwnode; |
| 2547 | fwspec.param_count = 3; |
| 2548 | fwspec.param[0] = GIC_IRQ_TYPE_LPI; |
| 2549 | fwspec.param[1] = hwirq; |
| 2550 | fwspec.param[2] = IRQ_TYPE_EDGE_RISING; |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2551 | } else if (is_fwnode_irqchip(domain->parent->fwnode)) { |
| 2552 | fwspec.fwnode = domain->parent->fwnode; |
| 2553 | fwspec.param_count = 2; |
| 2554 | fwspec.param[0] = hwirq; |
| 2555 | fwspec.param[1] = IRQ_TYPE_EDGE_RISING; |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 2556 | } else { |
| 2557 | return -EINVAL; |
| 2558 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2559 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 2560 | return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 2564 | unsigned int nr_irqs, void *args) |
| 2565 | { |
| 2566 | msi_alloc_info_t *info = args; |
| 2567 | struct its_device *its_dev = info->scratchpad[0].ptr; |
Julien Grall | 35ae7df | 2019-05-01 14:58:21 +0100 | [diff] [blame] | 2568 | struct its_node *its = its_dev->its; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2569 | irq_hw_number_t hwirq; |
| 2570 | int err; |
| 2571 | int i; |
| 2572 | |
Marc Zyngier | 8208d17 | 2019-01-18 14:08:59 +0000 | [diff] [blame] | 2573 | err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq); |
| 2574 | if (err) |
| 2575 | return err; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2576 | |
Julien Grall | 35ae7df | 2019-05-01 14:58:21 +0100 | [diff] [blame] | 2577 | err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev)); |
| 2578 | if (err) |
| 2579 | return err; |
| 2580 | |
Marc Zyngier | 8208d17 | 2019-01-18 14:08:59 +0000 | [diff] [blame] | 2581 | for (i = 0; i < nr_irqs; i++) { |
| 2582 | err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2583 | if (err) |
| 2584 | return err; |
| 2585 | |
| 2586 | irq_domain_set_hwirq_and_chip(domain, virq + i, |
Marc Zyngier | 8208d17 | 2019-01-18 14:08:59 +0000 | [diff] [blame] | 2587 | hwirq + i, &its_irq_chip, its_dev); |
Marc Zyngier | 0d224d3 | 2017-08-18 09:39:18 +0100 | [diff] [blame] | 2588 | 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] | 2589 | pr_debug("ID:%d pID:%d vID:%d\n", |
Marc Zyngier | 8208d17 | 2019-01-18 14:08:59 +0000 | [diff] [blame] | 2590 | (int)(hwirq + i - its_dev->event_map.lpi_base), |
| 2591 | (int)(hwirq + i), virq + i); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2592 | } |
| 2593 | |
| 2594 | return 0; |
| 2595 | } |
| 2596 | |
Thomas Gleixner | 7249164 | 2017-09-13 23:29:10 +0200 | [diff] [blame] | 2597 | static int its_irq_domain_activate(struct irq_domain *domain, |
Thomas Gleixner | 702cb0a | 2017-12-29 16:59:06 +0100 | [diff] [blame] | 2598 | struct irq_data *d, bool reserve) |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2599 | { |
| 2600 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 2601 | u32 event = its_get_event_id(d); |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 2602 | const struct cpumask *cpu_mask = cpu_online_mask; |
Marc Zyngier | 0d224d3 | 2017-08-18 09:39:18 +0100 | [diff] [blame] | 2603 | int cpu; |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 2604 | |
| 2605 | /* get the cpu_mask of local node */ |
| 2606 | if (its_dev->its->numa_node >= 0) |
| 2607 | cpu_mask = cpumask_of_node(its_dev->its->numa_node); |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2608 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2609 | /* Bind the LPI to the first possible CPU */ |
Yang Yingliang | c1797b1 | 2018-06-22 10:52:51 +0100 | [diff] [blame] | 2610 | cpu = cpumask_first_and(cpu_mask, cpu_online_mask); |
| 2611 | if (cpu >= nr_cpu_ids) { |
| 2612 | if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) |
| 2613 | return -EINVAL; |
| 2614 | |
| 2615 | cpu = cpumask_first(cpu_online_mask); |
| 2616 | } |
| 2617 | |
Marc Zyngier | 0d224d3 | 2017-08-18 09:39:18 +0100 | [diff] [blame] | 2618 | its_dev->event_map.col_map[event] = cpu; |
| 2619 | irq_data_update_effective_affinity(d, cpumask_of(cpu)); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2620 | |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2621 | /* Map the GIC IRQ and event to the device */ |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 2622 | its_send_mapti(its_dev, d->hwirq, event); |
Thomas Gleixner | 7249164 | 2017-09-13 23:29:10 +0200 | [diff] [blame] | 2623 | return 0; |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | static void its_irq_domain_deactivate(struct irq_domain *domain, |
| 2627 | struct irq_data *d) |
| 2628 | { |
| 2629 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 2630 | u32 event = its_get_event_id(d); |
| 2631 | |
| 2632 | /* Stop the delivery of interrupts */ |
| 2633 | its_send_discard(its_dev, event); |
| 2634 | } |
| 2635 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2636 | static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq, |
| 2637 | unsigned int nr_irqs) |
| 2638 | { |
| 2639 | struct irq_data *d = irq_domain_get_irq_data(domain, virq); |
| 2640 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 2641 | struct its_node *its = its_dev->its; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2642 | int i; |
| 2643 | |
Marc Zyngier | c9c96e3 | 2019-09-05 14:56:47 +0100 | [diff] [blame^] | 2644 | bitmap_release_region(its_dev->event_map.lpi_map, |
| 2645 | its_get_event_id(irq_domain_get_irq_data(domain, virq)), |
| 2646 | get_count_order(nr_irqs)); |
| 2647 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2648 | for (i = 0; i < nr_irqs; i++) { |
| 2649 | struct irq_data *data = irq_domain_get_irq_data(domain, |
| 2650 | virq + i); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2651 | /* Nuke the entry in the domain */ |
Marc Zyngier | 2da3994 | 2014-12-12 10:51:22 +0000 | [diff] [blame] | 2652 | irq_domain_reset_irq_data(data); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 2655 | mutex_lock(&its->dev_alloc_lock); |
| 2656 | |
| 2657 | /* |
| 2658 | * If all interrupts have been freed, start mopping the |
| 2659 | * floor. This is conditionned on the device not being shared. |
| 2660 | */ |
| 2661 | if (!its_dev->shared && |
| 2662 | bitmap_empty(its_dev->event_map.lpi_map, |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2663 | its_dev->event_map.nr_lpis)) { |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 2664 | its_lpi_free(its_dev->event_map.lpi_map, |
| 2665 | its_dev->event_map.lpi_base, |
| 2666 | its_dev->event_map.nr_lpis); |
Marc Zyngier | cf2be8b | 2016-12-19 18:49:59 +0000 | [diff] [blame] | 2667 | kfree(its_dev->event_map.col_map); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2668 | |
| 2669 | /* Unmap device/itt */ |
| 2670 | its_send_mapd(its_dev, 0); |
| 2671 | its_free_device(its_dev); |
| 2672 | } |
| 2673 | |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 2674 | mutex_unlock(&its->dev_alloc_lock); |
| 2675 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2676 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 2677 | } |
| 2678 | |
| 2679 | static const struct irq_domain_ops its_domain_ops = { |
| 2680 | .alloc = its_irq_domain_alloc, |
| 2681 | .free = its_irq_domain_free, |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2682 | .activate = its_irq_domain_activate, |
| 2683 | .deactivate = its_irq_domain_deactivate, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2684 | }; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2685 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 2686 | /* |
| 2687 | * This is insane. |
| 2688 | * |
| 2689 | * If a GICv4 doesn't implement Direct LPIs (which is extremely |
| 2690 | * likely), the only way to perform an invalidate is to use a fake |
| 2691 | * device to issue an INV command, implying that the LPI has first |
| 2692 | * been mapped to some event on that device. Since this is not exactly |
| 2693 | * cheap, we try to keep that mapping around as long as possible, and |
| 2694 | * only issue an UNMAP if we're short on available slots. |
| 2695 | * |
| 2696 | * Broken by design(tm). |
| 2697 | */ |
| 2698 | static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe) |
| 2699 | { |
| 2700 | /* Already unmapped? */ |
| 2701 | if (vpe->vpe_proxy_event == -1) |
| 2702 | return; |
| 2703 | |
| 2704 | its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event); |
| 2705 | vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL; |
| 2706 | |
| 2707 | /* |
| 2708 | * We don't track empty slots at all, so let's move the |
| 2709 | * next_victim pointer if we can quickly reuse that slot |
| 2710 | * instead of nuking an existing entry. Not clear that this is |
| 2711 | * always a win though, and this might just generate a ripple |
| 2712 | * effect... Let's just hope VPEs don't migrate too often. |
| 2713 | */ |
| 2714 | if (vpe_proxy.vpes[vpe_proxy.next_victim]) |
| 2715 | vpe_proxy.next_victim = vpe->vpe_proxy_event; |
| 2716 | |
| 2717 | vpe->vpe_proxy_event = -1; |
| 2718 | } |
| 2719 | |
| 2720 | static void its_vpe_db_proxy_unmap(struct its_vpe *vpe) |
| 2721 | { |
| 2722 | if (!gic_rdists->has_direct_lpi) { |
| 2723 | unsigned long flags; |
| 2724 | |
| 2725 | raw_spin_lock_irqsave(&vpe_proxy.lock, flags); |
| 2726 | its_vpe_db_proxy_unmap_locked(vpe); |
| 2727 | raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags); |
| 2728 | } |
| 2729 | } |
| 2730 | |
| 2731 | static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe) |
| 2732 | { |
| 2733 | /* Already mapped? */ |
| 2734 | if (vpe->vpe_proxy_event != -1) |
| 2735 | return; |
| 2736 | |
| 2737 | /* This slot was already allocated. Kick the other VPE out. */ |
| 2738 | if (vpe_proxy.vpes[vpe_proxy.next_victim]) |
| 2739 | its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]); |
| 2740 | |
| 2741 | /* Map the new VPE instead */ |
| 2742 | vpe_proxy.vpes[vpe_proxy.next_victim] = vpe; |
| 2743 | vpe->vpe_proxy_event = vpe_proxy.next_victim; |
| 2744 | vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites; |
| 2745 | |
| 2746 | vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx; |
| 2747 | its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event); |
| 2748 | } |
| 2749 | |
Marc Zyngier | 958b90d | 2017-08-18 16:14:17 +0100 | [diff] [blame] | 2750 | static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to) |
| 2751 | { |
| 2752 | unsigned long flags; |
| 2753 | struct its_collection *target_col; |
| 2754 | |
| 2755 | if (gic_rdists->has_direct_lpi) { |
| 2756 | void __iomem *rdbase; |
| 2757 | |
| 2758 | rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base; |
| 2759 | gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR); |
| 2760 | while (gic_read_lpir(rdbase + GICR_SYNCR) & 1) |
| 2761 | cpu_relax(); |
| 2762 | |
| 2763 | return; |
| 2764 | } |
| 2765 | |
| 2766 | raw_spin_lock_irqsave(&vpe_proxy.lock, flags); |
| 2767 | |
| 2768 | its_vpe_db_proxy_map_locked(vpe); |
| 2769 | |
| 2770 | target_col = &vpe_proxy.dev->its->collections[to]; |
| 2771 | its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event); |
| 2772 | vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to; |
| 2773 | |
| 2774 | raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags); |
| 2775 | } |
| 2776 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 2777 | static int its_vpe_set_affinity(struct irq_data *d, |
| 2778 | const struct cpumask *mask_val, |
| 2779 | bool force) |
| 2780 | { |
| 2781 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2782 | int cpu = cpumask_first(mask_val); |
| 2783 | |
| 2784 | /* |
| 2785 | * Changing affinity is mega expensive, so let's be as lazy as |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 2786 | * 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] | 2787 | * into the proxy device, we need to move the doorbell |
| 2788 | * interrupt to its new location. |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 2789 | */ |
| 2790 | if (vpe->col_idx != cpu) { |
Marc Zyngier | 958b90d | 2017-08-18 16:14:17 +0100 | [diff] [blame] | 2791 | int from = vpe->col_idx; |
| 2792 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 2793 | vpe->col_idx = cpu; |
| 2794 | its_send_vmovp(vpe); |
Marc Zyngier | 958b90d | 2017-08-18 16:14:17 +0100 | [diff] [blame] | 2795 | its_vpe_db_proxy_move(vpe, from, cpu); |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Marc Zyngier | 44c4c25 | 2017-10-19 10:11:34 +0100 | [diff] [blame] | 2798 | irq_data_update_effective_affinity(d, cpumask_of(cpu)); |
| 2799 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 2800 | return IRQ_SET_MASK_OK_DONE; |
| 2801 | } |
| 2802 | |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2803 | static void its_vpe_schedule(struct its_vpe *vpe) |
| 2804 | { |
Robin Murphy | 50c3309 | 2018-02-16 16:57:56 +0000 | [diff] [blame] | 2805 | void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2806 | u64 val; |
| 2807 | |
| 2808 | /* Schedule the VPE */ |
| 2809 | val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) & |
| 2810 | GENMASK_ULL(51, 12); |
| 2811 | val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK; |
| 2812 | val |= GICR_VPROPBASER_RaWb; |
| 2813 | val |= GICR_VPROPBASER_InnerShareable; |
| 2814 | gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); |
| 2815 | |
| 2816 | val = virt_to_phys(page_address(vpe->vpt_page)) & |
| 2817 | GENMASK_ULL(51, 16); |
| 2818 | val |= GICR_VPENDBASER_RaWaWb; |
| 2819 | val |= GICR_VPENDBASER_NonShareable; |
| 2820 | /* |
| 2821 | * There is no good way of finding out if the pending table is |
| 2822 | * empty as we can race against the doorbell interrupt very |
| 2823 | * easily. So in the end, vpe->pending_last is only an |
| 2824 | * indication that the vcpu has something pending, not one |
| 2825 | * that the pending table is empty. A good implementation |
| 2826 | * would be able to read its coarse map pretty quickly anyway, |
| 2827 | * making this a tolerable issue. |
| 2828 | */ |
| 2829 | val |= GICR_VPENDBASER_PendingLast; |
| 2830 | val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0; |
| 2831 | val |= GICR_VPENDBASER_Valid; |
| 2832 | gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); |
| 2833 | } |
| 2834 | |
| 2835 | static void its_vpe_deschedule(struct its_vpe *vpe) |
| 2836 | { |
Robin Murphy | 50c3309 | 2018-02-16 16:57:56 +0000 | [diff] [blame] | 2837 | void __iomem *vlpi_base = gic_data_rdist_vlpi_base(); |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2838 | u64 val; |
| 2839 | |
Heyi Guo | 6479450 | 2019-01-24 21:37:08 +0800 | [diff] [blame] | 2840 | val = its_clear_vpend_valid(vlpi_base); |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2841 | |
Heyi Guo | 6479450 | 2019-01-24 21:37:08 +0800 | [diff] [blame] | 2842 | if (unlikely(val & GICR_VPENDBASER_Dirty)) { |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2843 | pr_err_ratelimited("ITS virtual pending table not cleaning\n"); |
| 2844 | vpe->idai = false; |
| 2845 | vpe->pending_last = true; |
| 2846 | } else { |
| 2847 | vpe->idai = !!(val & GICR_VPENDBASER_IDAI); |
| 2848 | vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast); |
| 2849 | } |
| 2850 | } |
| 2851 | |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 2852 | static void its_vpe_invall(struct its_vpe *vpe) |
| 2853 | { |
| 2854 | struct its_node *its; |
| 2855 | |
| 2856 | list_for_each_entry(its, &its_nodes, entry) { |
| 2857 | if (!its->is_v4) |
| 2858 | continue; |
| 2859 | |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 2860 | if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr]) |
| 2861 | continue; |
| 2862 | |
Marc Zyngier | 3c1ccee | 2017-10-09 13:17:43 +0100 | [diff] [blame] | 2863 | /* |
| 2864 | * Sending a VINVALL to a single ITS is enough, as all |
| 2865 | * we need is to reach the redistributors. |
| 2866 | */ |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 2867 | its_send_vinvall(its, vpe); |
Marc Zyngier | 3c1ccee | 2017-10-09 13:17:43 +0100 | [diff] [blame] | 2868 | return; |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 2869 | } |
| 2870 | } |
| 2871 | |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2872 | static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) |
| 2873 | { |
| 2874 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2875 | struct its_cmd_info *info = vcpu_info; |
| 2876 | |
| 2877 | switch (info->cmd_type) { |
| 2878 | case SCHEDULE_VPE: |
| 2879 | its_vpe_schedule(vpe); |
| 2880 | return 0; |
| 2881 | |
| 2882 | case DESCHEDULE_VPE: |
| 2883 | its_vpe_deschedule(vpe); |
| 2884 | return 0; |
| 2885 | |
Marc Zyngier | 5e2f764 | 2016-12-20 15:10:50 +0000 | [diff] [blame] | 2886 | case INVALL_VPE: |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 2887 | its_vpe_invall(vpe); |
Marc Zyngier | 5e2f764 | 2016-12-20 15:10:50 +0000 | [diff] [blame] | 2888 | return 0; |
| 2889 | |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2890 | default: |
| 2891 | return -EINVAL; |
| 2892 | } |
| 2893 | } |
| 2894 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 2895 | static void its_vpe_send_cmd(struct its_vpe *vpe, |
| 2896 | void (*cmd)(struct its_device *, u32)) |
| 2897 | { |
| 2898 | unsigned long flags; |
| 2899 | |
| 2900 | raw_spin_lock_irqsave(&vpe_proxy.lock, flags); |
| 2901 | |
| 2902 | its_vpe_db_proxy_map_locked(vpe); |
| 2903 | cmd(vpe_proxy.dev, vpe->vpe_proxy_event); |
| 2904 | |
| 2905 | raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags); |
| 2906 | } |
| 2907 | |
Marc Zyngier | f6a91da | 2016-12-20 15:20:38 +0000 | [diff] [blame] | 2908 | static void its_vpe_send_inv(struct irq_data *d) |
| 2909 | { |
| 2910 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
Marc Zyngier | f6a91da | 2016-12-20 15:20:38 +0000 | [diff] [blame] | 2911 | |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 2912 | if (gic_rdists->has_direct_lpi) { |
| 2913 | void __iomem *rdbase; |
| 2914 | |
| 2915 | rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base; |
| 2916 | gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR); |
| 2917 | while (gic_read_lpir(rdbase + GICR_SYNCR) & 1) |
| 2918 | cpu_relax(); |
| 2919 | } else { |
| 2920 | its_vpe_send_cmd(vpe, its_send_inv); |
| 2921 | } |
Marc Zyngier | f6a91da | 2016-12-20 15:20:38 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | static void its_vpe_mask_irq(struct irq_data *d) |
| 2925 | { |
| 2926 | /* |
| 2927 | * We need to unmask the LPI, which is described by the parent |
| 2928 | * irq_data. Instead of calling into the parent (which won't |
| 2929 | * exactly do the right thing, let's simply use the |
| 2930 | * parent_data pointer. Yes, I'm naughty. |
| 2931 | */ |
| 2932 | lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0); |
| 2933 | its_vpe_send_inv(d); |
| 2934 | } |
| 2935 | |
| 2936 | static void its_vpe_unmask_irq(struct irq_data *d) |
| 2937 | { |
| 2938 | /* Same hack as above... */ |
| 2939 | lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED); |
| 2940 | its_vpe_send_inv(d); |
| 2941 | } |
| 2942 | |
Marc Zyngier | e57a3e28 | 2017-07-31 14:47:24 +0100 | [diff] [blame] | 2943 | static int its_vpe_set_irqchip_state(struct irq_data *d, |
| 2944 | enum irqchip_irq_state which, |
| 2945 | bool state) |
| 2946 | { |
| 2947 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2948 | |
| 2949 | if (which != IRQCHIP_STATE_PENDING) |
| 2950 | return -EINVAL; |
| 2951 | |
| 2952 | if (gic_rdists->has_direct_lpi) { |
| 2953 | void __iomem *rdbase; |
| 2954 | |
| 2955 | rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base; |
| 2956 | if (state) { |
| 2957 | gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR); |
| 2958 | } else { |
| 2959 | gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR); |
| 2960 | while (gic_read_lpir(rdbase + GICR_SYNCR) & 1) |
| 2961 | cpu_relax(); |
| 2962 | } |
| 2963 | } else { |
| 2964 | if (state) |
| 2965 | its_vpe_send_cmd(vpe, its_send_int); |
| 2966 | else |
| 2967 | its_vpe_send_cmd(vpe, its_send_clear); |
| 2968 | } |
| 2969 | |
| 2970 | return 0; |
| 2971 | } |
| 2972 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2973 | static struct irq_chip its_vpe_irq_chip = { |
| 2974 | .name = "GICv4-vpe", |
Marc Zyngier | f6a91da | 2016-12-20 15:20:38 +0000 | [diff] [blame] | 2975 | .irq_mask = its_vpe_mask_irq, |
| 2976 | .irq_unmask = its_vpe_unmask_irq, |
| 2977 | .irq_eoi = irq_chip_eoi_parent, |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 2978 | .irq_set_affinity = its_vpe_set_affinity, |
Marc Zyngier | e57a3e28 | 2017-07-31 14:47:24 +0100 | [diff] [blame] | 2979 | .irq_set_irqchip_state = its_vpe_set_irqchip_state, |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2980 | .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity, |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2981 | }; |
| 2982 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 2983 | static int its_vpe_id_alloc(void) |
| 2984 | { |
Shanker Donthineni | 32bd44d | 2017-10-07 15:43:48 -0500 | [diff] [blame] | 2985 | 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] | 2986 | } |
| 2987 | |
| 2988 | static void its_vpe_id_free(u16 id) |
| 2989 | { |
| 2990 | ida_simple_remove(&its_vpeid_ida, id); |
| 2991 | } |
| 2992 | |
| 2993 | static int its_vpe_init(struct its_vpe *vpe) |
| 2994 | { |
| 2995 | struct page *vpt_page; |
| 2996 | int vpe_id; |
| 2997 | |
| 2998 | /* Allocate vpe_id */ |
| 2999 | vpe_id = its_vpe_id_alloc(); |
| 3000 | if (vpe_id < 0) |
| 3001 | return vpe_id; |
| 3002 | |
| 3003 | /* Allocate VPT */ |
| 3004 | vpt_page = its_allocate_pending_table(GFP_KERNEL); |
| 3005 | if (!vpt_page) { |
| 3006 | its_vpe_id_free(vpe_id); |
| 3007 | return -ENOMEM; |
| 3008 | } |
| 3009 | |
| 3010 | if (!its_alloc_vpe_table(vpe_id)) { |
| 3011 | its_vpe_id_free(vpe_id); |
Nianyao Tang | 34f8eb9 | 2019-07-26 17:32:57 +0800 | [diff] [blame] | 3012 | its_free_pending_table(vpt_page); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3013 | return -ENOMEM; |
| 3014 | } |
| 3015 | |
| 3016 | vpe->vpe_id = vpe_id; |
| 3017 | vpe->vpt_page = vpt_page; |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3018 | vpe->vpe_proxy_event = -1; |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3019 | |
| 3020 | return 0; |
| 3021 | } |
| 3022 | |
| 3023 | static void its_vpe_teardown(struct its_vpe *vpe) |
| 3024 | { |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3025 | its_vpe_db_proxy_unmap(vpe); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3026 | its_vpe_id_free(vpe->vpe_id); |
| 3027 | its_free_pending_table(vpe->vpt_page); |
| 3028 | } |
| 3029 | |
| 3030 | static void its_vpe_irq_domain_free(struct irq_domain *domain, |
| 3031 | unsigned int virq, |
| 3032 | unsigned int nr_irqs) |
| 3033 | { |
| 3034 | struct its_vm *vm = domain->host_data; |
| 3035 | int i; |
| 3036 | |
| 3037 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 3038 | |
| 3039 | for (i = 0; i < nr_irqs; i++) { |
| 3040 | struct irq_data *data = irq_domain_get_irq_data(domain, |
| 3041 | virq + i); |
| 3042 | struct its_vpe *vpe = irq_data_get_irq_chip_data(data); |
| 3043 | |
| 3044 | BUG_ON(vm != vpe->its_vm); |
| 3045 | |
| 3046 | clear_bit(data->hwirq, vm->db_bitmap); |
| 3047 | its_vpe_teardown(vpe); |
| 3048 | irq_domain_reset_irq_data(data); |
| 3049 | } |
| 3050 | |
| 3051 | if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) { |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 3052 | 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] | 3053 | its_free_prop_table(vm->vprop_page); |
| 3054 | } |
| 3055 | } |
| 3056 | |
| 3057 | static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 3058 | unsigned int nr_irqs, void *args) |
| 3059 | { |
| 3060 | struct its_vm *vm = args; |
| 3061 | unsigned long *bitmap; |
| 3062 | struct page *vprop_page; |
| 3063 | int base, nr_ids, i, err = 0; |
| 3064 | |
| 3065 | BUG_ON(!vm); |
| 3066 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 3067 | 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] | 3068 | if (!bitmap) |
| 3069 | return -ENOMEM; |
| 3070 | |
| 3071 | if (nr_ids < nr_irqs) { |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 3072 | its_lpi_free(bitmap, base, nr_ids); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3073 | return -ENOMEM; |
| 3074 | } |
| 3075 | |
| 3076 | vprop_page = its_allocate_prop_table(GFP_KERNEL); |
| 3077 | if (!vprop_page) { |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 3078 | its_lpi_free(bitmap, base, nr_ids); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3079 | return -ENOMEM; |
| 3080 | } |
| 3081 | |
| 3082 | vm->db_bitmap = bitmap; |
| 3083 | vm->db_lpi_base = base; |
| 3084 | vm->nr_db_lpis = nr_ids; |
| 3085 | vm->vprop_page = vprop_page; |
| 3086 | |
| 3087 | for (i = 0; i < nr_irqs; i++) { |
| 3088 | vm->vpes[i]->vpe_db_lpi = base + i; |
| 3089 | err = its_vpe_init(vm->vpes[i]); |
| 3090 | if (err) |
| 3091 | break; |
| 3092 | err = its_irq_gic_domain_alloc(domain, virq + i, |
| 3093 | vm->vpes[i]->vpe_db_lpi); |
| 3094 | if (err) |
| 3095 | break; |
| 3096 | irq_domain_set_hwirq_and_chip(domain, virq + i, i, |
| 3097 | &its_vpe_irq_chip, vm->vpes[i]); |
| 3098 | set_bit(i, bitmap); |
| 3099 | } |
| 3100 | |
| 3101 | if (err) { |
| 3102 | if (i > 0) |
| 3103 | its_vpe_irq_domain_free(domain, virq, i - 1); |
| 3104 | |
Marc Zyngier | 38dd7c4 | 2018-05-27 17:03:03 +0100 | [diff] [blame] | 3105 | its_lpi_free(bitmap, base, nr_ids); |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3106 | its_free_prop_table(vprop_page); |
| 3107 | } |
| 3108 | |
| 3109 | return err; |
| 3110 | } |
| 3111 | |
Thomas Gleixner | 7249164 | 2017-09-13 23:29:10 +0200 | [diff] [blame] | 3112 | static int its_vpe_irq_domain_activate(struct irq_domain *domain, |
Thomas Gleixner | 702cb0a | 2017-12-29 16:59:06 +0100 | [diff] [blame] | 3113 | struct irq_data *d, bool reserve) |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 3114 | { |
| 3115 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 3116 | struct its_node *its; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 3117 | |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 3118 | /* If we use the list map, we issue VMAPP on demand... */ |
| 3119 | if (its_list_map) |
Marc Zyngier | 6ef930f | 2017-11-07 10:04:38 +0000 | [diff] [blame] | 3120 | return 0; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 3121 | |
| 3122 | /* Map the VPE to the first possible CPU */ |
| 3123 | vpe->col_idx = cpumask_first(cpu_online_mask); |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 3124 | |
| 3125 | list_for_each_entry(its, &its_nodes, entry) { |
| 3126 | if (!its->is_v4) |
| 3127 | continue; |
| 3128 | |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 3129 | its_send_vmapp(its, vpe, true); |
Marc Zyngier | 40619a2 | 2017-10-08 15:16:09 +0100 | [diff] [blame] | 3130 | its_send_vinvall(its, vpe); |
| 3131 | } |
| 3132 | |
Marc Zyngier | 44c4c25 | 2017-10-19 10:11:34 +0100 | [diff] [blame] | 3133 | irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx)); |
| 3134 | |
Thomas Gleixner | 7249164 | 2017-09-13 23:29:10 +0200 | [diff] [blame] | 3135 | return 0; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
| 3138 | static void its_vpe_irq_domain_deactivate(struct irq_domain *domain, |
| 3139 | struct irq_data *d) |
| 3140 | { |
| 3141 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 3142 | struct its_node *its; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 3143 | |
Marc Zyngier | 2247e1b | 2017-10-08 18:50:36 +0100 | [diff] [blame] | 3144 | /* |
| 3145 | * If we use the list map, we unmap the VPE once no VLPIs are |
| 3146 | * associated with the VM. |
| 3147 | */ |
| 3148 | if (its_list_map) |
| 3149 | return; |
| 3150 | |
Marc Zyngier | 75fd951 | 2017-10-08 18:46:39 +0100 | [diff] [blame] | 3151 | list_for_each_entry(its, &its_nodes, entry) { |
| 3152 | if (!its->is_v4) |
| 3153 | continue; |
| 3154 | |
| 3155 | its_send_vmapp(its, vpe, false); |
| 3156 | } |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 3157 | } |
| 3158 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 3159 | static const struct irq_domain_ops its_vpe_domain_ops = { |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 3160 | .alloc = its_vpe_irq_domain_alloc, |
| 3161 | .free = its_vpe_irq_domain_free, |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 3162 | .activate = its_vpe_irq_domain_activate, |
| 3163 | .deactivate = its_vpe_irq_domain_deactivate, |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 3164 | }; |
| 3165 | |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 3166 | static int its_force_quiescent(void __iomem *base) |
| 3167 | { |
| 3168 | u32 count = 1000000; /* 1s */ |
| 3169 | u32 val; |
| 3170 | |
| 3171 | val = readl_relaxed(base + GITS_CTLR); |
David Daney | 7611da8 | 2016-08-18 15:41:58 -0700 | [diff] [blame] | 3172 | /* |
| 3173 | * GIC architecture specification requires the ITS to be both |
| 3174 | * disabled and quiescent for writes to GITS_BASER<n> or |
| 3175 | * GITS_CBASER to not have UNPREDICTABLE results. |
| 3176 | */ |
| 3177 | if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE)) |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 3178 | return 0; |
| 3179 | |
| 3180 | /* Disable the generation of all interrupts to this ITS */ |
Marc Zyngier | d51c4b4 | 2017-06-27 21:24:25 +0100 | [diff] [blame] | 3181 | val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe); |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 3182 | writel_relaxed(val, base + GITS_CTLR); |
| 3183 | |
| 3184 | /* Poll GITS_CTLR and wait until ITS becomes quiescent */ |
| 3185 | while (1) { |
| 3186 | val = readl_relaxed(base + GITS_CTLR); |
| 3187 | if (val & GITS_CTLR_QUIESCENT) |
| 3188 | return 0; |
| 3189 | |
| 3190 | count--; |
| 3191 | if (!count) |
| 3192 | return -EBUSY; |
| 3193 | |
| 3194 | cpu_relax(); |
| 3195 | udelay(1); |
| 3196 | } |
| 3197 | } |
| 3198 | |
Ard Biesheuvel | 9d111d4 | 2017-10-17 17:55:55 +0100 | [diff] [blame] | 3199 | static bool __maybe_unused its_enable_quirk_cavium_22375(void *data) |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 3200 | { |
| 3201 | struct its_node *its = data; |
| 3202 | |
Ard Biesheuvel | fa15001 | 2017-10-17 17:55:54 +0100 | [diff] [blame] | 3203 | /* erratum 22375: only alloc 8MB table size */ |
| 3204 | its->device_ids = 0x14; /* 20 bits, 8MB */ |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 3205 | its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375; |
Ard Biesheuvel | 9d111d4 | 2017-10-17 17:55:55 +0100 | [diff] [blame] | 3206 | |
| 3207 | return true; |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 3208 | } |
| 3209 | |
Ard Biesheuvel | 9d111d4 | 2017-10-17 17:55:55 +0100 | [diff] [blame] | 3210 | static bool __maybe_unused its_enable_quirk_cavium_23144(void *data) |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 3211 | { |
| 3212 | struct its_node *its = data; |
| 3213 | |
| 3214 | its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144; |
Ard Biesheuvel | 9d111d4 | 2017-10-17 17:55:55 +0100 | [diff] [blame] | 3215 | |
| 3216 | return true; |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 3217 | } |
| 3218 | |
Ard Biesheuvel | 9d111d4 | 2017-10-17 17:55:55 +0100 | [diff] [blame] | 3219 | static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data) |
Shanker Donthineni | 90922a2 | 2017-03-07 08:20:38 -0600 | [diff] [blame] | 3220 | { |
| 3221 | struct its_node *its = data; |
| 3222 | |
| 3223 | /* On QDF2400, the size of the ITE is 16Bytes */ |
| 3224 | its->ite_size = 16; |
Ard Biesheuvel | 9d111d4 | 2017-10-17 17:55:55 +0100 | [diff] [blame] | 3225 | |
| 3226 | return true; |
Shanker Donthineni | 90922a2 | 2017-03-07 08:20:38 -0600 | [diff] [blame] | 3227 | } |
| 3228 | |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 3229 | static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev) |
| 3230 | { |
| 3231 | struct its_node *its = its_dev->its; |
| 3232 | |
| 3233 | /* |
| 3234 | * The Socionext Synquacer SoC has a so-called 'pre-ITS', |
| 3235 | * which maps 32-bit writes targeted at a separate window of |
| 3236 | * size '4 << device_id_bits' onto writes to GITS_TRANSLATER |
| 3237 | * with device ID taken from bits [device_id_bits + 1:2] of |
| 3238 | * the window offset. |
| 3239 | */ |
| 3240 | return its->pre_its_base + (its_dev->device_id << 2); |
| 3241 | } |
| 3242 | |
| 3243 | static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data) |
| 3244 | { |
| 3245 | struct its_node *its = data; |
| 3246 | u32 pre_its_window[2]; |
| 3247 | u32 ids; |
| 3248 | |
| 3249 | if (!fwnode_property_read_u32_array(its->fwnode_handle, |
| 3250 | "socionext,synquacer-pre-its", |
| 3251 | pre_its_window, |
| 3252 | ARRAY_SIZE(pre_its_window))) { |
| 3253 | |
| 3254 | its->pre_its_base = pre_its_window[0]; |
| 3255 | its->get_msi_base = its_irq_get_msi_base_pre_its; |
| 3256 | |
| 3257 | ids = ilog2(pre_its_window[1]) - 2; |
| 3258 | if (its->device_ids > ids) |
| 3259 | its->device_ids = ids; |
| 3260 | |
| 3261 | /* the pre-ITS breaks isolation, so disable MSI remapping */ |
| 3262 | its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP; |
| 3263 | return true; |
| 3264 | } |
| 3265 | return false; |
| 3266 | } |
| 3267 | |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 3268 | static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data) |
| 3269 | { |
| 3270 | struct its_node *its = data; |
| 3271 | |
| 3272 | /* |
| 3273 | * Hip07 insists on using the wrong address for the VLPI |
| 3274 | * page. Trick it into doing the right thing... |
| 3275 | */ |
| 3276 | its->vlpi_redist_offset = SZ_128K; |
| 3277 | return true; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 3278 | } |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3279 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 3280 | static const struct gic_quirk its_quirks[] = { |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 3281 | #ifdef CONFIG_CAVIUM_ERRATUM_22375 |
| 3282 | { |
| 3283 | .desc = "ITS: Cavium errata 22375, 24313", |
| 3284 | .iidr = 0xa100034c, /* ThunderX pass 1.x */ |
| 3285 | .mask = 0xffff0fff, |
| 3286 | .init = its_enable_quirk_cavium_22375, |
| 3287 | }, |
| 3288 | #endif |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 3289 | #ifdef CONFIG_CAVIUM_ERRATUM_23144 |
| 3290 | { |
| 3291 | .desc = "ITS: Cavium erratum 23144", |
| 3292 | .iidr = 0xa100034c, /* ThunderX pass 1.x */ |
| 3293 | .mask = 0xffff0fff, |
| 3294 | .init = its_enable_quirk_cavium_23144, |
| 3295 | }, |
| 3296 | #endif |
Shanker Donthineni | 90922a2 | 2017-03-07 08:20:38 -0600 | [diff] [blame] | 3297 | #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065 |
| 3298 | { |
| 3299 | .desc = "ITS: QDF2400 erratum 0065", |
| 3300 | .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */ |
| 3301 | .mask = 0xffffffff, |
| 3302 | .init = its_enable_quirk_qdf2400_e0065, |
| 3303 | }, |
| 3304 | #endif |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 3305 | #ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS |
| 3306 | { |
| 3307 | /* |
| 3308 | * The Socionext Synquacer SoC incorporates ARM's own GIC-500 |
| 3309 | * implementation, but with a 'pre-ITS' added that requires |
| 3310 | * special handling in software. |
| 3311 | */ |
| 3312 | .desc = "ITS: Socionext Synquacer pre-ITS", |
| 3313 | .iidr = 0x0001143b, |
| 3314 | .mask = 0xffffffff, |
| 3315 | .init = its_enable_quirk_socionext_synquacer, |
| 3316 | }, |
| 3317 | #endif |
Marc Zyngier | 5c9a882 | 2017-07-28 21:20:37 +0100 | [diff] [blame] | 3318 | #ifdef CONFIG_HISILICON_ERRATUM_161600802 |
| 3319 | { |
| 3320 | .desc = "ITS: Hip07 erratum 161600802", |
| 3321 | .iidr = 0x00000004, |
| 3322 | .mask = 0xffffffff, |
| 3323 | .init = its_enable_quirk_hip07_161600802, |
| 3324 | }, |
| 3325 | #endif |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 3326 | { |
| 3327 | } |
| 3328 | }; |
| 3329 | |
| 3330 | static void its_enable_quirks(struct its_node *its) |
| 3331 | { |
| 3332 | u32 iidr = readl_relaxed(its->base + GITS_IIDR); |
| 3333 | |
| 3334 | gic_enable_quirks(iidr, its_quirks, its); |
| 3335 | } |
| 3336 | |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 3337 | static int its_save_disable(void) |
| 3338 | { |
| 3339 | struct its_node *its; |
| 3340 | int err = 0; |
| 3341 | |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 3342 | raw_spin_lock(&its_lock); |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 3343 | list_for_each_entry(its, &its_nodes, entry) { |
| 3344 | void __iomem *base; |
| 3345 | |
| 3346 | if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE)) |
| 3347 | continue; |
| 3348 | |
| 3349 | base = its->base; |
| 3350 | its->ctlr_save = readl_relaxed(base + GITS_CTLR); |
| 3351 | err = its_force_quiescent(base); |
| 3352 | if (err) { |
| 3353 | pr_err("ITS@%pa: failed to quiesce: %d\n", |
| 3354 | &its->phys_base, err); |
| 3355 | writel_relaxed(its->ctlr_save, base + GITS_CTLR); |
| 3356 | goto err; |
| 3357 | } |
| 3358 | |
| 3359 | its->cbaser_save = gits_read_cbaser(base + GITS_CBASER); |
| 3360 | } |
| 3361 | |
| 3362 | err: |
| 3363 | if (err) { |
| 3364 | list_for_each_entry_continue_reverse(its, &its_nodes, entry) { |
| 3365 | void __iomem *base; |
| 3366 | |
| 3367 | if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE)) |
| 3368 | continue; |
| 3369 | |
| 3370 | base = its->base; |
| 3371 | writel_relaxed(its->ctlr_save, base + GITS_CTLR); |
| 3372 | } |
| 3373 | } |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 3374 | raw_spin_unlock(&its_lock); |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 3375 | |
| 3376 | return err; |
| 3377 | } |
| 3378 | |
| 3379 | static void its_restore_enable(void) |
| 3380 | { |
| 3381 | struct its_node *its; |
| 3382 | int ret; |
| 3383 | |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 3384 | raw_spin_lock(&its_lock); |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 3385 | list_for_each_entry(its, &its_nodes, entry) { |
| 3386 | void __iomem *base; |
| 3387 | int i; |
| 3388 | |
| 3389 | if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE)) |
| 3390 | continue; |
| 3391 | |
| 3392 | base = its->base; |
| 3393 | |
| 3394 | /* |
| 3395 | * Make sure that the ITS is disabled. If it fails to quiesce, |
| 3396 | * don't restore it since writing to CBASER or BASER<n> |
| 3397 | * registers is undefined according to the GIC v3 ITS |
| 3398 | * Specification. |
| 3399 | */ |
| 3400 | ret = its_force_quiescent(base); |
| 3401 | if (ret) { |
| 3402 | pr_err("ITS@%pa: failed to quiesce on resume: %d\n", |
| 3403 | &its->phys_base, ret); |
| 3404 | continue; |
| 3405 | } |
| 3406 | |
| 3407 | gits_write_cbaser(its->cbaser_save, base + GITS_CBASER); |
| 3408 | |
| 3409 | /* |
| 3410 | * Writing CBASER resets CREADR to 0, so make CWRITER and |
| 3411 | * cmd_write line up with it. |
| 3412 | */ |
| 3413 | its->cmd_write = its->cmd_base; |
| 3414 | gits_write_cwriter(0, base + GITS_CWRITER); |
| 3415 | |
| 3416 | /* Restore GITS_BASER from the value cache. */ |
| 3417 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
| 3418 | struct its_baser *baser = &its->tables[i]; |
| 3419 | |
| 3420 | if (!(baser->val & GITS_BASER_VALID)) |
| 3421 | continue; |
| 3422 | |
| 3423 | its_write_baser(its, baser, baser->val); |
| 3424 | } |
| 3425 | writel_relaxed(its->ctlr_save, base + GITS_CTLR); |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 3426 | |
| 3427 | /* |
| 3428 | * Reinit the collection if it's stored in the ITS. This is |
| 3429 | * indicated by the col_id being less than the HCC field. |
| 3430 | * CID < HCC as specified in the GIC v3 Documentation. |
| 3431 | */ |
| 3432 | if (its->collections[smp_processor_id()].col_id < |
| 3433 | GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER))) |
| 3434 | its_cpu_init_collection(its); |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 3435 | } |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 3436 | raw_spin_unlock(&its_lock); |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | static struct syscore_ops its_syscore_ops = { |
| 3440 | .suspend = its_save_disable, |
| 3441 | .resume = its_restore_enable, |
| 3442 | }; |
| 3443 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3444 | 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] | 3445 | { |
| 3446 | struct irq_domain *inner_domain; |
| 3447 | struct msi_domain_info *info; |
| 3448 | |
| 3449 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 3450 | if (!info) |
| 3451 | return -ENOMEM; |
| 3452 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3453 | inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 3454 | if (!inner_domain) { |
| 3455 | kfree(info); |
| 3456 | return -ENOMEM; |
| 3457 | } |
| 3458 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3459 | inner_domain->parent = its_parent; |
Marc Zyngier | 96f0d93 | 2017-06-22 11:42:50 +0100 | [diff] [blame] | 3460 | irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS); |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 3461 | inner_domain->flags |= its->msi_domain_flags; |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 3462 | info->ops = &its_msi_domain_ops; |
| 3463 | info->data = its; |
| 3464 | inner_domain->host_data = info; |
| 3465 | |
| 3466 | return 0; |
| 3467 | } |
| 3468 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 3469 | static int its_init_vpe_domain(void) |
| 3470 | { |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3471 | struct its_node *its; |
| 3472 | u32 devid; |
| 3473 | int entries; |
| 3474 | |
| 3475 | if (gic_rdists->has_direct_lpi) { |
| 3476 | pr_info("ITS: Using DirectLPI for VPE invalidation\n"); |
| 3477 | return 0; |
| 3478 | } |
| 3479 | |
| 3480 | /* Any ITS will do, even if not v4 */ |
| 3481 | its = list_first_entry(&its_nodes, struct its_node, entry); |
| 3482 | |
| 3483 | entries = roundup_pow_of_two(nr_cpu_ids); |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 3484 | vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes), |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3485 | GFP_KERNEL); |
| 3486 | if (!vpe_proxy.vpes) { |
| 3487 | pr_err("ITS: Can't allocate GICv4 proxy device array\n"); |
| 3488 | return -ENOMEM; |
| 3489 | } |
| 3490 | |
| 3491 | /* Use the last possible DevID */ |
| 3492 | devid = GENMASK(its->device_ids - 1, 0); |
| 3493 | vpe_proxy.dev = its_create_device(its, devid, entries, false); |
| 3494 | if (!vpe_proxy.dev) { |
| 3495 | kfree(vpe_proxy.vpes); |
| 3496 | pr_err("ITS: Can't allocate GICv4 proxy device\n"); |
| 3497 | return -ENOMEM; |
| 3498 | } |
| 3499 | |
Shanker Donthineni | c427a47 | 2017-09-23 13:50:19 -0500 | [diff] [blame] | 3500 | BUG_ON(entries > vpe_proxy.dev->nr_ites); |
Marc Zyngier | 20b3d54 | 2016-12-20 15:23:22 +0000 | [diff] [blame] | 3501 | |
| 3502 | raw_spin_lock_init(&vpe_proxy.lock); |
| 3503 | vpe_proxy.next_victim = 0; |
| 3504 | pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n", |
| 3505 | devid, vpe_proxy.dev->nr_ites); |
| 3506 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 3507 | return 0; |
| 3508 | } |
| 3509 | |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 3510 | static int __init its_compute_its_list_map(struct resource *res, |
| 3511 | void __iomem *its_base) |
| 3512 | { |
| 3513 | int its_number; |
| 3514 | u32 ctlr; |
| 3515 | |
| 3516 | /* |
| 3517 | * This is assumed to be done early enough that we're |
| 3518 | * guaranteed to be single-threaded, hence no |
| 3519 | * locking. Should this change, we should address |
| 3520 | * this. |
| 3521 | */ |
Marc Zyngier | ab60491 | 2017-10-08 18:48:06 +0100 | [diff] [blame] | 3522 | its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX); |
| 3523 | if (its_number >= GICv4_ITS_LIST_MAX) { |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 3524 | pr_err("ITS@%pa: No ITSList entry available!\n", |
| 3525 | &res->start); |
| 3526 | return -EINVAL; |
| 3527 | } |
| 3528 | |
| 3529 | ctlr = readl_relaxed(its_base + GITS_CTLR); |
| 3530 | ctlr &= ~GITS_CTLR_ITS_NUMBER; |
| 3531 | ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT; |
| 3532 | writel_relaxed(ctlr, its_base + GITS_CTLR); |
| 3533 | ctlr = readl_relaxed(its_base + GITS_CTLR); |
| 3534 | if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) { |
| 3535 | its_number = ctlr & GITS_CTLR_ITS_NUMBER; |
| 3536 | its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT; |
| 3537 | } |
| 3538 | |
| 3539 | if (test_and_set_bit(its_number, &its_list_map)) { |
| 3540 | pr_err("ITS@%pa: Duplicate ITSList entry %d\n", |
| 3541 | &res->start, its_number); |
| 3542 | return -EINVAL; |
| 3543 | } |
| 3544 | |
| 3545 | return its_number; |
| 3546 | } |
| 3547 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3548 | static int __init its_probe_one(struct resource *res, |
| 3549 | struct fwnode_handle *handle, int numa_node) |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3550 | { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3551 | struct its_node *its; |
| 3552 | void __iomem *its_base; |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 3553 | u32 val, ctlr; |
| 3554 | u64 baser, tmp, typer; |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 3555 | struct page *page; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3556 | int err; |
| 3557 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3558 | its_base = ioremap(res->start, resource_size(res)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3559 | if (!its_base) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3560 | pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3561 | return -ENOMEM; |
| 3562 | } |
| 3563 | |
| 3564 | val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK; |
| 3565 | if (val != 0x30 && val != 0x40) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3566 | pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3567 | err = -ENODEV; |
| 3568 | goto out_unmap; |
| 3569 | } |
| 3570 | |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 3571 | err = its_force_quiescent(its_base); |
| 3572 | if (err) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3573 | pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start); |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 3574 | goto out_unmap; |
| 3575 | } |
| 3576 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3577 | pr_info("ITS %pR\n", res); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3578 | |
| 3579 | its = kzalloc(sizeof(*its), GFP_KERNEL); |
| 3580 | if (!its) { |
| 3581 | err = -ENOMEM; |
| 3582 | goto out_unmap; |
| 3583 | } |
| 3584 | |
| 3585 | raw_spin_lock_init(&its->lock); |
Marc Zyngier | 9791ec7 | 2019-01-29 10:02:33 +0000 | [diff] [blame] | 3586 | mutex_init(&its->dev_alloc_lock); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3587 | INIT_LIST_HEAD(&its->entry); |
| 3588 | INIT_LIST_HEAD(&its->its_device_list); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 3589 | typer = gic_read_typer(its_base + GITS_TYPER); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3590 | its->base = its_base; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3591 | its->phys_base = res->start; |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 3592 | its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer); |
Ard Biesheuvel | fa15001 | 2017-10-17 17:55:54 +0100 | [diff] [blame] | 3593 | its->device_ids = GITS_TYPER_DEVBITS(typer); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 3594 | its->is_v4 = !!(typer & GITS_TYPER_VLPIS); |
| 3595 | if (its->is_v4) { |
| 3596 | if (!(typer & GITS_TYPER_VMOVP)) { |
| 3597 | err = its_compute_its_list_map(res, its_base); |
| 3598 | if (err < 0) |
| 3599 | goto out_free_its; |
| 3600 | |
Marc Zyngier | debf6d0 | 2017-10-08 18:44:42 +0100 | [diff] [blame] | 3601 | its->list_nr = err; |
| 3602 | |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 3603 | pr_info("ITS@%pa: Using ITS number %d\n", |
| 3604 | &res->start, err); |
| 3605 | } else { |
| 3606 | pr_info("ITS@%pa: Single VMOVP capable\n", &res->start); |
| 3607 | } |
| 3608 | } |
| 3609 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3610 | its->numa_node = numa_node; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3611 | |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 3612 | page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, |
| 3613 | get_order(ITS_CMD_QUEUE_SZ)); |
| 3614 | if (!page) { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3615 | err = -ENOMEM; |
| 3616 | goto out_free_its; |
| 3617 | } |
Shanker Donthineni | 539d378 | 2019-01-14 09:50:19 +0000 | [diff] [blame] | 3618 | its->cmd_base = (void *)page_address(page); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3619 | its->cmd_write = its->cmd_base; |
Ard Biesheuvel | 558b016 | 2017-10-17 17:55:56 +0100 | [diff] [blame] | 3620 | its->fwnode_handle = handle; |
| 3621 | its->get_msi_base = its_irq_get_msi_base; |
| 3622 | its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3623 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 3624 | its_enable_quirks(its); |
| 3625 | |
Shanker Donthineni | 0e0b0f6 | 2016-06-06 18:17:31 -0500 | [diff] [blame] | 3626 | err = its_alloc_tables(its); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3627 | if (err) |
| 3628 | goto out_free_cmd; |
| 3629 | |
| 3630 | err = its_alloc_collections(its); |
| 3631 | if (err) |
| 3632 | goto out_free_tables; |
| 3633 | |
| 3634 | baser = (virt_to_phys(its->cmd_base) | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 3635 | GITS_CBASER_RaWaWb | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3636 | GITS_CBASER_InnerShareable | |
| 3637 | (ITS_CMD_QUEUE_SZ / SZ_4K - 1) | |
| 3638 | GITS_CBASER_VALID); |
| 3639 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 3640 | gits_write_cbaser(baser, its->base + GITS_CBASER); |
| 3641 | tmp = gits_read_cbaser(its->base + GITS_CBASER); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3642 | |
Marc Zyngier | 4ad3e36 | 2015-03-27 14:15:04 +0000 | [diff] [blame] | 3643 | if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) { |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 3644 | if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) { |
| 3645 | /* |
| 3646 | * The HW reports non-shareable, we must |
| 3647 | * remove the cacheability attributes as |
| 3648 | * well. |
| 3649 | */ |
| 3650 | baser &= ~(GITS_CBASER_SHAREABILITY_MASK | |
| 3651 | GITS_CBASER_CACHEABILITY_MASK); |
| 3652 | baser |= GITS_CBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 3653 | gits_write_cbaser(baser, its->base + GITS_CBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 3654 | } |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3655 | pr_info("ITS: using cache flushing for cmd queue\n"); |
| 3656 | its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING; |
| 3657 | } |
| 3658 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 3659 | gits_write_cwriter(0, its->base + GITS_CWRITER); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 3660 | ctlr = readl_relaxed(its->base + GITS_CTLR); |
Marc Zyngier | d51c4b4 | 2017-06-27 21:24:25 +0100 | [diff] [blame] | 3661 | ctlr |= GITS_CTLR_ENABLE; |
| 3662 | if (its->is_v4) |
| 3663 | ctlr |= GITS_CTLR_ImDe; |
| 3664 | writel_relaxed(ctlr, its->base + GITS_CTLR); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 3665 | |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 3666 | if (GITS_TYPER_HCC(typer)) |
| 3667 | its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE; |
| 3668 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3669 | err = its_init_domain(handle, its); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 3670 | if (err) |
| 3671 | goto out_free_tables; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3672 | |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 3673 | raw_spin_lock(&its_lock); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3674 | list_add(&its->entry, &its_nodes); |
Sebastian Andrzej Siewior | a8db745 | 2018-07-18 17:42:04 +0200 | [diff] [blame] | 3675 | raw_spin_unlock(&its_lock); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3676 | |
| 3677 | return 0; |
| 3678 | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3679 | out_free_tables: |
| 3680 | its_free_tables(its); |
| 3681 | out_free_cmd: |
Robert Richter | 5bc13c2 | 2017-02-01 18:38:25 +0100 | [diff] [blame] | 3682 | free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3683 | out_free_its: |
| 3684 | kfree(its); |
| 3685 | out_unmap: |
| 3686 | iounmap(its_base); |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3687 | pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3688 | return err; |
| 3689 | } |
| 3690 | |
| 3691 | static bool gic_rdists_supports_plpis(void) |
| 3692 | { |
Marc Zyngier | 589ce5f | 2016-10-14 15:13:07 +0100 | [diff] [blame] | 3693 | 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] | 3694 | } |
| 3695 | |
Shanker Donthineni | 6eb486b | 2018-03-21 20:58:49 -0500 | [diff] [blame] | 3696 | static int redist_disable_lpis(void) |
| 3697 | { |
| 3698 | void __iomem *rbase = gic_data_rdist_rd_base(); |
| 3699 | u64 timeout = USEC_PER_SEC; |
| 3700 | u64 val; |
| 3701 | |
| 3702 | if (!gic_rdists_supports_plpis()) { |
| 3703 | pr_info("CPU%d: LPIs not supported\n", smp_processor_id()); |
| 3704 | return -ENXIO; |
| 3705 | } |
| 3706 | |
| 3707 | val = readl_relaxed(rbase + GICR_CTLR); |
| 3708 | if (!(val & GICR_CTLR_ENABLE_LPIS)) |
| 3709 | return 0; |
| 3710 | |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 3711 | /* |
| 3712 | * If coming via a CPU hotplug event, we don't need to disable |
| 3713 | * LPIs before trying to re-enable them. They are already |
| 3714 | * configured and all is well in the world. |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 3715 | * |
| 3716 | * If running with preallocated tables, there is nothing to do. |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 3717 | */ |
Marc Zyngier | c440a9d | 2018-07-27 15:40:13 +0100 | [diff] [blame] | 3718 | if (gic_data_rdist()->lpi_enabled || |
| 3719 | (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED)) |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 3720 | return 0; |
| 3721 | |
| 3722 | /* |
| 3723 | * From that point on, we only try to do some damage control. |
| 3724 | */ |
| 3725 | 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] | 3726 | smp_processor_id()); |
| 3727 | add_taint(TAINT_CRAP, LOCKDEP_STILL_OK); |
| 3728 | |
| 3729 | /* Disable LPIs */ |
| 3730 | val &= ~GICR_CTLR_ENABLE_LPIS; |
| 3731 | writel_relaxed(val, rbase + GICR_CTLR); |
| 3732 | |
| 3733 | /* Make sure any change to GICR_CTLR is observable by the GIC */ |
| 3734 | dsb(sy); |
| 3735 | |
| 3736 | /* |
| 3737 | * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs |
| 3738 | * from 1 to 0 before programming GICR_PEND{PROP}BASER registers. |
| 3739 | * Error out if we time out waiting for RWP to clear. |
| 3740 | */ |
| 3741 | while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) { |
| 3742 | if (!timeout) { |
| 3743 | pr_err("CPU%d: Timeout while disabling LPIs\n", |
| 3744 | smp_processor_id()); |
| 3745 | return -ETIMEDOUT; |
| 3746 | } |
| 3747 | udelay(1); |
| 3748 | timeout--; |
| 3749 | } |
| 3750 | |
| 3751 | /* |
| 3752 | * After it has been written to 1, it is IMPLEMENTATION |
| 3753 | * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be |
| 3754 | * cleared to 0. Error out if clearing the bit failed. |
| 3755 | */ |
| 3756 | if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) { |
| 3757 | pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id()); |
| 3758 | return -EBUSY; |
| 3759 | } |
| 3760 | |
| 3761 | return 0; |
| 3762 | } |
| 3763 | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3764 | int its_cpu_init(void) |
| 3765 | { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3766 | if (!list_empty(&its_nodes)) { |
Shanker Donthineni | 6eb486b | 2018-03-21 20:58:49 -0500 | [diff] [blame] | 3767 | int ret; |
| 3768 | |
| 3769 | ret = redist_disable_lpis(); |
| 3770 | if (ret) |
| 3771 | return ret; |
| 3772 | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3773 | its_cpu_init_lpis(); |
Derek Basehore | 920181c | 2018-02-28 21:48:20 -0800 | [diff] [blame] | 3774 | its_cpu_init_collections(); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3775 | } |
| 3776 | |
| 3777 | return 0; |
| 3778 | } |
| 3779 | |
Arvind Yadav | 935bba7 | 2017-06-22 16:05:30 +0530 | [diff] [blame] | 3780 | static const struct of_device_id its_device_id[] = { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3781 | { .compatible = "arm,gic-v3-its", }, |
| 3782 | {}, |
| 3783 | }; |
| 3784 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3785 | static int __init its_of_probe(struct device_node *node) |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3786 | { |
| 3787 | struct device_node *np; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3788 | struct resource res; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3789 | |
| 3790 | for (np = of_find_matching_node(node, its_device_id); np; |
| 3791 | np = of_find_matching_node(np, its_device_id)) { |
Stephen Boyd | 95a2562 | 2018-02-01 09:03:29 -0800 | [diff] [blame] | 3792 | if (!of_device_is_available(np)) |
| 3793 | continue; |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 3794 | if (!of_property_read_bool(np, "msi-controller")) { |
Rob Herring | e81f54c | 2017-07-18 16:43:10 -0500 | [diff] [blame] | 3795 | pr_warn("%pOF: no msi-controller property, ITS ignored\n", |
| 3796 | np); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 3797 | continue; |
| 3798 | } |
| 3799 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3800 | if (of_address_to_resource(np, 0, &res)) { |
Rob Herring | e81f54c | 2017-07-18 16:43:10 -0500 | [diff] [blame] | 3801 | pr_warn("%pOF: no regs?\n", np); |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3802 | continue; |
| 3803 | } |
| 3804 | |
| 3805 | its_probe_one(&res, &np->fwnode, of_node_to_nid(np)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3806 | } |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3807 | return 0; |
| 3808 | } |
| 3809 | |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 3810 | #ifdef CONFIG_ACPI |
| 3811 | |
| 3812 | #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K) |
| 3813 | |
Robert Richter | d1ce263 | 2017-07-12 15:25:09 +0200 | [diff] [blame] | 3814 | #ifdef CONFIG_ACPI_NUMA |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 3815 | struct its_srat_map { |
| 3816 | /* numa node id */ |
| 3817 | u32 numa_node; |
| 3818 | /* GIC ITS ID */ |
| 3819 | u32 its_id; |
| 3820 | }; |
| 3821 | |
Hanjun Guo | fdf6e7a | 2017-07-26 18:15:49 +0800 | [diff] [blame] | 3822 | static struct its_srat_map *its_srat_maps __initdata; |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 3823 | static int its_in_srat __initdata; |
| 3824 | |
| 3825 | static int __init acpi_get_its_numa_node(u32 its_id) |
| 3826 | { |
| 3827 | int i; |
| 3828 | |
| 3829 | for (i = 0; i < its_in_srat; i++) { |
| 3830 | if (its_id == its_srat_maps[i].its_id) |
| 3831 | return its_srat_maps[i].numa_node; |
| 3832 | } |
| 3833 | return NUMA_NO_NODE; |
| 3834 | } |
| 3835 | |
Keith Busch | 60574d1 | 2019-03-11 14:55:57 -0600 | [diff] [blame] | 3836 | 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] | 3837 | const unsigned long end) |
| 3838 | { |
| 3839 | return 0; |
| 3840 | } |
| 3841 | |
Keith Busch | 60574d1 | 2019-03-11 14:55:57 -0600 | [diff] [blame] | 3842 | 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] | 3843 | const unsigned long end) |
| 3844 | { |
| 3845 | int node; |
| 3846 | struct acpi_srat_gic_its_affinity *its_affinity; |
| 3847 | |
| 3848 | its_affinity = (struct acpi_srat_gic_its_affinity *)header; |
| 3849 | if (!its_affinity) |
| 3850 | return -EINVAL; |
| 3851 | |
| 3852 | if (its_affinity->header.length < sizeof(*its_affinity)) { |
| 3853 | pr_err("SRAT: Invalid header length %d in ITS affinity\n", |
| 3854 | its_affinity->header.length); |
| 3855 | return -EINVAL; |
| 3856 | } |
| 3857 | |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 3858 | node = acpi_map_pxm_to_node(its_affinity->proximity_domain); |
| 3859 | |
| 3860 | if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) { |
| 3861 | pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node); |
| 3862 | return 0; |
| 3863 | } |
| 3864 | |
| 3865 | its_srat_maps[its_in_srat].numa_node = node; |
| 3866 | its_srat_maps[its_in_srat].its_id = its_affinity->its_id; |
| 3867 | its_in_srat++; |
| 3868 | pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n", |
| 3869 | its_affinity->proximity_domain, its_affinity->its_id, node); |
| 3870 | |
| 3871 | return 0; |
| 3872 | } |
| 3873 | |
| 3874 | static void __init acpi_table_parse_srat_its(void) |
| 3875 | { |
Hanjun Guo | fdf6e7a | 2017-07-26 18:15:49 +0800 | [diff] [blame] | 3876 | int count; |
| 3877 | |
| 3878 | count = acpi_table_parse_entries(ACPI_SIG_SRAT, |
| 3879 | sizeof(struct acpi_table_srat), |
| 3880 | ACPI_SRAT_TYPE_GIC_ITS_AFFINITY, |
| 3881 | gic_acpi_match_srat_its, 0); |
| 3882 | if (count <= 0) |
| 3883 | return; |
| 3884 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 3885 | its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map), |
| 3886 | GFP_KERNEL); |
Hanjun Guo | fdf6e7a | 2017-07-26 18:15:49 +0800 | [diff] [blame] | 3887 | if (!its_srat_maps) { |
| 3888 | pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n"); |
| 3889 | return; |
| 3890 | } |
| 3891 | |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 3892 | acpi_table_parse_entries(ACPI_SIG_SRAT, |
| 3893 | sizeof(struct acpi_table_srat), |
| 3894 | ACPI_SRAT_TYPE_GIC_ITS_AFFINITY, |
| 3895 | gic_acpi_parse_srat_its, 0); |
| 3896 | } |
Hanjun Guo | fdf6e7a | 2017-07-26 18:15:49 +0800 | [diff] [blame] | 3897 | |
| 3898 | /* free the its_srat_maps after ITS probing */ |
| 3899 | static void __init acpi_its_srat_maps_free(void) |
| 3900 | { |
| 3901 | kfree(its_srat_maps); |
| 3902 | } |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 3903 | #else |
| 3904 | static void __init acpi_table_parse_srat_its(void) { } |
| 3905 | 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] | 3906 | static void __init acpi_its_srat_maps_free(void) { } |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 3907 | #endif |
| 3908 | |
Keith Busch | 60574d1 | 2019-03-11 14:55:57 -0600 | [diff] [blame] | 3909 | 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] | 3910 | const unsigned long end) |
| 3911 | { |
| 3912 | struct acpi_madt_generic_translator *its_entry; |
| 3913 | struct fwnode_handle *dom_handle; |
| 3914 | struct resource res; |
| 3915 | int err; |
| 3916 | |
| 3917 | its_entry = (struct acpi_madt_generic_translator *)header; |
| 3918 | memset(&res, 0, sizeof(res)); |
| 3919 | res.start = its_entry->base_address; |
| 3920 | res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1; |
| 3921 | res.flags = IORESOURCE_MEM; |
| 3922 | |
Marc Zyngier | 5778cc7 | 2019-07-31 16:13:42 +0100 | [diff] [blame] | 3923 | dom_handle = irq_domain_alloc_fwnode(&res.start); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 3924 | if (!dom_handle) { |
| 3925 | pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n", |
| 3926 | &res.start); |
| 3927 | return -ENOMEM; |
| 3928 | } |
| 3929 | |
Shameer Kolothum | 8b4282e | 2018-02-13 15:20:50 +0000 | [diff] [blame] | 3930 | err = iort_register_domain_token(its_entry->translation_id, res.start, |
| 3931 | dom_handle); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 3932 | if (err) { |
| 3933 | pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n", |
| 3934 | &res.start, its_entry->translation_id); |
| 3935 | goto dom_err; |
| 3936 | } |
| 3937 | |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 3938 | err = its_probe_one(&res, dom_handle, |
| 3939 | acpi_get_its_numa_node(its_entry->translation_id)); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 3940 | if (!err) |
| 3941 | return 0; |
| 3942 | |
| 3943 | iort_deregister_domain_token(its_entry->translation_id); |
| 3944 | dom_err: |
| 3945 | irq_domain_free_fwnode(dom_handle); |
| 3946 | return err; |
| 3947 | } |
| 3948 | |
| 3949 | static void __init its_acpi_probe(void) |
| 3950 | { |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 3951 | acpi_table_parse_srat_its(); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 3952 | acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR, |
| 3953 | gic_acpi_parse_madt_its, 0); |
Hanjun Guo | fdf6e7a | 2017-07-26 18:15:49 +0800 | [diff] [blame] | 3954 | acpi_its_srat_maps_free(); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 3955 | } |
| 3956 | #else |
| 3957 | static void __init its_acpi_probe(void) { } |
| 3958 | #endif |
| 3959 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3960 | int __init its_init(struct fwnode_handle *handle, struct rdists *rdists, |
| 3961 | struct irq_domain *parent_domain) |
| 3962 | { |
| 3963 | struct device_node *of_node; |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 3964 | struct its_node *its; |
| 3965 | bool has_v4 = false; |
| 3966 | int err; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3967 | |
| 3968 | its_parent = parent_domain; |
| 3969 | of_node = to_of_node(handle); |
| 3970 | if (of_node) |
| 3971 | its_of_probe(of_node); |
| 3972 | else |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 3973 | its_acpi_probe(); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3974 | |
| 3975 | if (list_empty(&its_nodes)) { |
| 3976 | pr_warn("ITS: No ITS available, not enabling LPIs\n"); |
| 3977 | return -ENXIO; |
| 3978 | } |
| 3979 | |
| 3980 | gic_rdists = rdists; |
Marc Zyngier | 11e37d3 | 2018-07-27 13:38:54 +0100 | [diff] [blame] | 3981 | |
| 3982 | err = allocate_lpi_tables(); |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 3983 | if (err) |
| 3984 | return err; |
| 3985 | |
| 3986 | list_for_each_entry(its, &its_nodes, entry) |
| 3987 | has_v4 |= its->is_v4; |
| 3988 | |
| 3989 | if (has_v4 & rdists->has_vlpis) { |
Marc Zyngier | 3d63cb5 | 2016-12-20 15:31:54 +0000 | [diff] [blame] | 3990 | if (its_init_vpe_domain() || |
| 3991 | its_init_v4(parent_domain, &its_vpe_domain_ops)) { |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 3992 | rdists->has_vlpis = false; |
| 3993 | pr_err("ITS: Disabling GICv4 support\n"); |
| 3994 | } |
| 3995 | } |
| 3996 | |
Derek Basehore | dba0bc7 | 2018-02-28 21:48:18 -0800 | [diff] [blame] | 3997 | register_syscore_ops(&its_syscore_ops); |
| 3998 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 3999 | return 0; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 4000 | } |