Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 1 | /* |
Marc Zyngier | d7276b8 | 2016-12-20 15:11:47 +0000 | [diff] [blame] | 2 | * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved. |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 3 | * Author: Marc Zyngier <marc.zyngier@arm.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 18 | #include <linux/acpi.h> |
Hanjun Guo | 8d3554b | 2017-03-07 20:39:59 +0800 | [diff] [blame] | 19 | #include <linux/acpi_iort.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 20 | #include <linux/bitmap.h> |
| 21 | #include <linux/cpu.h> |
| 22 | #include <linux/delay.h> |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 23 | #include <linux/dma-iommu.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 24 | #include <linux/interrupt.h> |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 25 | #include <linux/irqdomain.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 26 | #include <linux/log2.h> |
| 27 | #include <linux/mm.h> |
| 28 | #include <linux/msi.h> |
| 29 | #include <linux/of.h> |
| 30 | #include <linux/of_address.h> |
| 31 | #include <linux/of_irq.h> |
| 32 | #include <linux/of_pci.h> |
| 33 | #include <linux/of_platform.h> |
| 34 | #include <linux/percpu.h> |
| 35 | #include <linux/slab.h> |
| 36 | |
Joel Porquet | 41a83e06 | 2015-07-07 17:11:46 -0400 | [diff] [blame] | 37 | #include <linux/irqchip.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 38 | #include <linux/irqchip/arm-gic-v3.h> |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 39 | #include <linux/irqchip/arm-gic-v4.h> |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 40 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 41 | #include <asm/cputype.h> |
| 42 | #include <asm/exception.h> |
| 43 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 44 | #include "irq-gic-common.h" |
| 45 | |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 46 | #define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0) |
| 47 | #define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1) |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 48 | #define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 49 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 50 | #define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0) |
| 51 | |
Marc Zyngier | a13b040 | 2016-12-19 17:15:24 +0000 | [diff] [blame] | 52 | static u32 lpi_id_bits; |
| 53 | |
| 54 | /* |
| 55 | * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to |
| 56 | * deal with (one configuration byte per interrupt). PENDBASE has to |
| 57 | * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI). |
| 58 | */ |
| 59 | #define LPI_NRBITS lpi_id_bits |
| 60 | #define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K) |
| 61 | #define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K) |
| 62 | |
| 63 | #define LPI_PROP_DEFAULT_PRIO 0xa0 |
| 64 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 65 | /* |
| 66 | * Collection structure - just an ID, and a redistributor address to |
| 67 | * ping. We use one per CPU as a bag of interrupts assigned to this |
| 68 | * CPU. |
| 69 | */ |
| 70 | struct its_collection { |
| 71 | u64 target_address; |
| 72 | u16 col_id; |
| 73 | }; |
| 74 | |
| 75 | /* |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 76 | * The ITS_BASER structure - contains memory information, cached |
| 77 | * value of BASER register configuration and ITS page size. |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 78 | */ |
| 79 | struct its_baser { |
| 80 | void *base; |
| 81 | u64 val; |
| 82 | u32 order; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 83 | u32 psz; |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | /* |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 87 | * The ITS structure - contains most of the infrastructure, with the |
Marc Zyngier | 841514a | 2015-07-28 14:46:20 +0100 | [diff] [blame] | 88 | * top-level MSI domain, the command queue, the collections, and the |
| 89 | * list of devices writing to it. |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 90 | */ |
| 91 | struct its_node { |
| 92 | raw_spinlock_t lock; |
| 93 | struct list_head entry; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 94 | void __iomem *base; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 95 | phys_addr_t phys_base; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 96 | struct its_cmd_block *cmd_base; |
| 97 | struct its_cmd_block *cmd_write; |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 98 | struct its_baser tables[GITS_BASER_NR_REGS]; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 99 | struct its_collection *collections; |
| 100 | struct list_head its_device_list; |
| 101 | u64 flags; |
| 102 | u32 ite_size; |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 103 | u32 device_ids; |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 104 | int numa_node; |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 105 | bool is_v4; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | #define ITS_ITT_ALIGN SZ_256 |
| 109 | |
Shanker Donthineni | 2eca0d6 | 2016-02-16 18:00:36 -0600 | [diff] [blame] | 110 | /* Convert page order to size in bytes */ |
| 111 | #define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o)) |
| 112 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 113 | struct event_lpi_map { |
| 114 | unsigned long *lpi_map; |
| 115 | u16 *col_map; |
| 116 | irq_hw_number_t lpi_base; |
| 117 | int nr_lpis; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 118 | struct mutex vlpi_lock; |
| 119 | struct its_vm *vm; |
| 120 | struct its_vlpi_map *vlpi_maps; |
| 121 | int nr_vlpis; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 122 | }; |
| 123 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 124 | /* |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 125 | * The ITS view of a device - belongs to an ITS, owns an interrupt |
| 126 | * translation table, and a list of interrupts. If it some of its |
| 127 | * LPIs are injected into a guest (GICv4), the event_map.vm field |
| 128 | * indicates which one. |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 129 | */ |
| 130 | struct its_device { |
| 131 | struct list_head entry; |
| 132 | struct its_node *its; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 133 | struct event_lpi_map event_map; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 134 | void *itt; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 135 | u32 nr_ites; |
| 136 | u32 device_id; |
| 137 | }; |
| 138 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 139 | static LIST_HEAD(its_nodes); |
| 140 | static DEFINE_SPINLOCK(its_lock); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 141 | static struct rdists *gic_rdists; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 142 | static struct irq_domain *its_parent; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 143 | |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 144 | /* |
| 145 | * We have a maximum number of 16 ITSs in the whole system if we're |
| 146 | * using the ITSList mechanism |
| 147 | */ |
| 148 | #define ITS_LIST_MAX 16 |
| 149 | |
| 150 | static unsigned long its_list_map; |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 151 | static u16 vmovp_seq_num; |
| 152 | static DEFINE_RAW_SPINLOCK(vmovp_lock); |
| 153 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 154 | static DEFINE_IDA(its_vpeid_ida); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 155 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 156 | #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist)) |
| 157 | #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base) |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 158 | #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] | 159 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 160 | static struct its_collection *dev_event_to_col(struct its_device *its_dev, |
| 161 | u32 event) |
| 162 | { |
| 163 | struct its_node *its = its_dev->its; |
| 164 | |
| 165 | return its->collections + its_dev->event_map.col_map[event]; |
| 166 | } |
| 167 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 168 | /* |
| 169 | * ITS command descriptors - parameters to be encoded in a command |
| 170 | * block. |
| 171 | */ |
| 172 | struct its_cmd_desc { |
| 173 | union { |
| 174 | struct { |
| 175 | struct its_device *dev; |
| 176 | u32 event_id; |
| 177 | } its_inv_cmd; |
| 178 | |
| 179 | struct { |
| 180 | struct its_device *dev; |
| 181 | u32 event_id; |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 182 | } its_clear_cmd; |
| 183 | |
| 184 | struct { |
| 185 | struct its_device *dev; |
| 186 | u32 event_id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 187 | } its_int_cmd; |
| 188 | |
| 189 | struct { |
| 190 | struct its_device *dev; |
| 191 | int valid; |
| 192 | } its_mapd_cmd; |
| 193 | |
| 194 | struct { |
| 195 | struct its_collection *col; |
| 196 | int valid; |
| 197 | } its_mapc_cmd; |
| 198 | |
| 199 | struct { |
| 200 | struct its_device *dev; |
| 201 | u32 phys_id; |
| 202 | u32 event_id; |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 203 | } its_mapti_cmd; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 204 | |
| 205 | struct { |
| 206 | struct its_device *dev; |
| 207 | struct its_collection *col; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 208 | u32 event_id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 209 | } its_movi_cmd; |
| 210 | |
| 211 | struct { |
| 212 | struct its_device *dev; |
| 213 | u32 event_id; |
| 214 | } its_discard_cmd; |
| 215 | |
| 216 | struct { |
| 217 | struct its_collection *col; |
| 218 | } its_invall_cmd; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 219 | |
| 220 | struct { |
| 221 | struct its_vpe *vpe; |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 222 | } its_vinvall_cmd; |
| 223 | |
| 224 | struct { |
| 225 | struct its_vpe *vpe; |
| 226 | struct its_collection *col; |
| 227 | bool valid; |
| 228 | } its_vmapp_cmd; |
| 229 | |
| 230 | struct { |
| 231 | struct its_vpe *vpe; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 232 | struct its_device *dev; |
| 233 | u32 virt_id; |
| 234 | u32 event_id; |
| 235 | bool db_enabled; |
| 236 | } its_vmapti_cmd; |
| 237 | |
| 238 | struct { |
| 239 | struct its_vpe *vpe; |
| 240 | struct its_device *dev; |
| 241 | u32 event_id; |
| 242 | bool db_enabled; |
| 243 | } its_vmovi_cmd; |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 244 | |
| 245 | struct { |
| 246 | struct its_vpe *vpe; |
| 247 | struct its_collection *col; |
| 248 | u16 seq_num; |
| 249 | u16 its_list; |
| 250 | } its_vmovp_cmd; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 251 | }; |
| 252 | }; |
| 253 | |
| 254 | /* |
| 255 | * The ITS command block, which is what the ITS actually parses. |
| 256 | */ |
| 257 | struct its_cmd_block { |
| 258 | u64 raw_cmd[4]; |
| 259 | }; |
| 260 | |
| 261 | #define ITS_CMD_QUEUE_SZ SZ_64K |
| 262 | #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block)) |
| 263 | |
| 264 | typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *, |
| 265 | struct its_cmd_desc *); |
| 266 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 267 | typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_cmd_block *, |
| 268 | struct its_cmd_desc *); |
| 269 | |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 270 | static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l) |
| 271 | { |
| 272 | u64 mask = GENMASK_ULL(h, l); |
| 273 | *raw_cmd &= ~mask; |
| 274 | *raw_cmd |= (val << l) & mask; |
| 275 | } |
| 276 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 277 | static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr) |
| 278 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 279 | its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | static void its_encode_devid(struct its_cmd_block *cmd, u32 devid) |
| 283 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 284 | its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static void its_encode_event_id(struct its_cmd_block *cmd, u32 id) |
| 288 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 289 | its_mask_encode(&cmd->raw_cmd[1], id, 31, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id) |
| 293 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 294 | its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | static void its_encode_size(struct its_cmd_block *cmd, u8 size) |
| 298 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 299 | its_mask_encode(&cmd->raw_cmd[1], size, 4, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr) |
| 303 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 304 | its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static void its_encode_valid(struct its_cmd_block *cmd, int valid) |
| 308 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 309 | its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr) |
| 313 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 314 | its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | static void its_encode_collection(struct its_cmd_block *cmd, u16 col) |
| 318 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 319 | its_mask_encode(&cmd->raw_cmd[2], col, 15, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 320 | } |
| 321 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 322 | static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid) |
| 323 | { |
| 324 | its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32); |
| 325 | } |
| 326 | |
| 327 | static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id) |
| 328 | { |
| 329 | its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0); |
| 330 | } |
| 331 | |
| 332 | static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id) |
| 333 | { |
| 334 | its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32); |
| 335 | } |
| 336 | |
| 337 | static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid) |
| 338 | { |
| 339 | its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0); |
| 340 | } |
| 341 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 342 | static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num) |
| 343 | { |
| 344 | its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32); |
| 345 | } |
| 346 | |
| 347 | static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list) |
| 348 | { |
| 349 | its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0); |
| 350 | } |
| 351 | |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 352 | static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa) |
| 353 | { |
| 354 | its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 50, 16); |
| 355 | } |
| 356 | |
| 357 | static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size) |
| 358 | { |
| 359 | its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0); |
| 360 | } |
| 361 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 362 | static inline void its_fixup_cmd(struct its_cmd_block *cmd) |
| 363 | { |
| 364 | /* Let's fixup BE commands */ |
| 365 | cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]); |
| 366 | cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]); |
| 367 | cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]); |
| 368 | cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]); |
| 369 | } |
| 370 | |
| 371 | static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd, |
| 372 | struct its_cmd_desc *desc) |
| 373 | { |
| 374 | unsigned long itt_addr; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 375 | u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 376 | |
| 377 | itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt); |
| 378 | itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN); |
| 379 | |
| 380 | its_encode_cmd(cmd, GITS_CMD_MAPD); |
| 381 | its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id); |
| 382 | its_encode_size(cmd, size - 1); |
| 383 | its_encode_itt(cmd, itt_addr); |
| 384 | its_encode_valid(cmd, desc->its_mapd_cmd.valid); |
| 385 | |
| 386 | its_fixup_cmd(cmd); |
| 387 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 388 | return NULL; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd, |
| 392 | struct its_cmd_desc *desc) |
| 393 | { |
| 394 | its_encode_cmd(cmd, GITS_CMD_MAPC); |
| 395 | its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id); |
| 396 | its_encode_target(cmd, desc->its_mapc_cmd.col->target_address); |
| 397 | its_encode_valid(cmd, desc->its_mapc_cmd.valid); |
| 398 | |
| 399 | its_fixup_cmd(cmd); |
| 400 | |
| 401 | return desc->its_mapc_cmd.col; |
| 402 | } |
| 403 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 404 | static struct its_collection *its_build_mapti_cmd(struct its_cmd_block *cmd, |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 405 | struct its_cmd_desc *desc) |
| 406 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 407 | struct its_collection *col; |
| 408 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 409 | col = dev_event_to_col(desc->its_mapti_cmd.dev, |
| 410 | desc->its_mapti_cmd.event_id); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 411 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 412 | its_encode_cmd(cmd, GITS_CMD_MAPTI); |
| 413 | its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id); |
| 414 | its_encode_event_id(cmd, desc->its_mapti_cmd.event_id); |
| 415 | its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 416 | its_encode_collection(cmd, col->col_id); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 417 | |
| 418 | its_fixup_cmd(cmd); |
| 419 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 420 | return col; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 421 | } |
| 422 | |
| 423 | static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd, |
| 424 | struct its_cmd_desc *desc) |
| 425 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 426 | struct its_collection *col; |
| 427 | |
| 428 | col = dev_event_to_col(desc->its_movi_cmd.dev, |
| 429 | desc->its_movi_cmd.event_id); |
| 430 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 431 | its_encode_cmd(cmd, GITS_CMD_MOVI); |
| 432 | its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 433 | its_encode_event_id(cmd, desc->its_movi_cmd.event_id); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 434 | its_encode_collection(cmd, desc->its_movi_cmd.col->col_id); |
| 435 | |
| 436 | its_fixup_cmd(cmd); |
| 437 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 438 | return col; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd, |
| 442 | struct its_cmd_desc *desc) |
| 443 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 444 | struct its_collection *col; |
| 445 | |
| 446 | col = dev_event_to_col(desc->its_discard_cmd.dev, |
| 447 | desc->its_discard_cmd.event_id); |
| 448 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 449 | its_encode_cmd(cmd, GITS_CMD_DISCARD); |
| 450 | its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id); |
| 451 | its_encode_event_id(cmd, desc->its_discard_cmd.event_id); |
| 452 | |
| 453 | its_fixup_cmd(cmd); |
| 454 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 455 | return col; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd, |
| 459 | struct its_cmd_desc *desc) |
| 460 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 461 | struct its_collection *col; |
| 462 | |
| 463 | col = dev_event_to_col(desc->its_inv_cmd.dev, |
| 464 | desc->its_inv_cmd.event_id); |
| 465 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 466 | its_encode_cmd(cmd, GITS_CMD_INV); |
| 467 | its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id); |
| 468 | its_encode_event_id(cmd, desc->its_inv_cmd.event_id); |
| 469 | |
| 470 | its_fixup_cmd(cmd); |
| 471 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 472 | return col; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 473 | } |
| 474 | |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 475 | static struct its_collection *its_build_int_cmd(struct its_cmd_block *cmd, |
| 476 | struct its_cmd_desc *desc) |
| 477 | { |
| 478 | struct its_collection *col; |
| 479 | |
| 480 | col = dev_event_to_col(desc->its_int_cmd.dev, |
| 481 | desc->its_int_cmd.event_id); |
| 482 | |
| 483 | its_encode_cmd(cmd, GITS_CMD_INT); |
| 484 | its_encode_devid(cmd, desc->its_int_cmd.dev->device_id); |
| 485 | its_encode_event_id(cmd, desc->its_int_cmd.event_id); |
| 486 | |
| 487 | its_fixup_cmd(cmd); |
| 488 | |
| 489 | return col; |
| 490 | } |
| 491 | |
| 492 | static struct its_collection *its_build_clear_cmd(struct its_cmd_block *cmd, |
| 493 | struct its_cmd_desc *desc) |
| 494 | { |
| 495 | struct its_collection *col; |
| 496 | |
| 497 | col = dev_event_to_col(desc->its_clear_cmd.dev, |
| 498 | desc->its_clear_cmd.event_id); |
| 499 | |
| 500 | its_encode_cmd(cmd, GITS_CMD_CLEAR); |
| 501 | its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id); |
| 502 | its_encode_event_id(cmd, desc->its_clear_cmd.event_id); |
| 503 | |
| 504 | its_fixup_cmd(cmd); |
| 505 | |
| 506 | return col; |
| 507 | } |
| 508 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 509 | static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd, |
| 510 | struct its_cmd_desc *desc) |
| 511 | { |
| 512 | its_encode_cmd(cmd, GITS_CMD_INVALL); |
| 513 | its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id); |
| 514 | |
| 515 | its_fixup_cmd(cmd); |
| 516 | |
| 517 | return NULL; |
| 518 | } |
| 519 | |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 520 | static struct its_vpe *its_build_vinvall_cmd(struct its_cmd_block *cmd, |
| 521 | struct its_cmd_desc *desc) |
| 522 | { |
| 523 | its_encode_cmd(cmd, GITS_CMD_VINVALL); |
| 524 | its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id); |
| 525 | |
| 526 | its_fixup_cmd(cmd); |
| 527 | |
| 528 | return desc->its_vinvall_cmd.vpe; |
| 529 | } |
| 530 | |
| 531 | static struct its_vpe *its_build_vmapp_cmd(struct its_cmd_block *cmd, |
| 532 | struct its_cmd_desc *desc) |
| 533 | { |
| 534 | unsigned long vpt_addr; |
| 535 | |
| 536 | vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page)); |
| 537 | |
| 538 | its_encode_cmd(cmd, GITS_CMD_VMAPP); |
| 539 | its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id); |
| 540 | its_encode_valid(cmd, desc->its_vmapp_cmd.valid); |
| 541 | its_encode_target(cmd, desc->its_vmapp_cmd.col->target_address); |
| 542 | its_encode_vpt_addr(cmd, vpt_addr); |
| 543 | its_encode_vpt_size(cmd, LPI_NRBITS - 1); |
| 544 | |
| 545 | its_fixup_cmd(cmd); |
| 546 | |
| 547 | return desc->its_vmapp_cmd.vpe; |
| 548 | } |
| 549 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 550 | static struct its_vpe *its_build_vmapti_cmd(struct its_cmd_block *cmd, |
| 551 | struct its_cmd_desc *desc) |
| 552 | { |
| 553 | u32 db; |
| 554 | |
| 555 | if (desc->its_vmapti_cmd.db_enabled) |
| 556 | db = desc->its_vmapti_cmd.vpe->vpe_db_lpi; |
| 557 | else |
| 558 | db = 1023; |
| 559 | |
| 560 | its_encode_cmd(cmd, GITS_CMD_VMAPTI); |
| 561 | its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id); |
| 562 | its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id); |
| 563 | its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id); |
| 564 | its_encode_db_phys_id(cmd, db); |
| 565 | its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id); |
| 566 | |
| 567 | its_fixup_cmd(cmd); |
| 568 | |
| 569 | return desc->its_vmapti_cmd.vpe; |
| 570 | } |
| 571 | |
| 572 | static struct its_vpe *its_build_vmovi_cmd(struct its_cmd_block *cmd, |
| 573 | struct its_cmd_desc *desc) |
| 574 | { |
| 575 | u32 db; |
| 576 | |
| 577 | if (desc->its_vmovi_cmd.db_enabled) |
| 578 | db = desc->its_vmovi_cmd.vpe->vpe_db_lpi; |
| 579 | else |
| 580 | db = 1023; |
| 581 | |
| 582 | its_encode_cmd(cmd, GITS_CMD_VMOVI); |
| 583 | its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id); |
| 584 | its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id); |
| 585 | its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id); |
| 586 | its_encode_db_phys_id(cmd, db); |
| 587 | its_encode_db_valid(cmd, true); |
| 588 | |
| 589 | its_fixup_cmd(cmd); |
| 590 | |
| 591 | return desc->its_vmovi_cmd.vpe; |
| 592 | } |
| 593 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 594 | static struct its_vpe *its_build_vmovp_cmd(struct its_cmd_block *cmd, |
| 595 | struct its_cmd_desc *desc) |
| 596 | { |
| 597 | its_encode_cmd(cmd, GITS_CMD_VMOVP); |
| 598 | its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num); |
| 599 | its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list); |
| 600 | its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id); |
| 601 | its_encode_target(cmd, desc->its_vmovp_cmd.col->target_address); |
| 602 | |
| 603 | its_fixup_cmd(cmd); |
| 604 | |
| 605 | return desc->its_vmovp_cmd.vpe; |
| 606 | } |
| 607 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 608 | static u64 its_cmd_ptr_to_offset(struct its_node *its, |
| 609 | struct its_cmd_block *ptr) |
| 610 | { |
| 611 | return (ptr - its->cmd_base) * sizeof(*ptr); |
| 612 | } |
| 613 | |
| 614 | static int its_queue_full(struct its_node *its) |
| 615 | { |
| 616 | int widx; |
| 617 | int ridx; |
| 618 | |
| 619 | widx = its->cmd_write - its->cmd_base; |
| 620 | ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block); |
| 621 | |
| 622 | /* This is incredibly unlikely to happen, unless the ITS locks up. */ |
| 623 | if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx) |
| 624 | return 1; |
| 625 | |
| 626 | return 0; |
| 627 | } |
| 628 | |
| 629 | static struct its_cmd_block *its_allocate_entry(struct its_node *its) |
| 630 | { |
| 631 | struct its_cmd_block *cmd; |
| 632 | u32 count = 1000000; /* 1s! */ |
| 633 | |
| 634 | while (its_queue_full(its)) { |
| 635 | count--; |
| 636 | if (!count) { |
| 637 | pr_err_ratelimited("ITS queue not draining\n"); |
| 638 | return NULL; |
| 639 | } |
| 640 | cpu_relax(); |
| 641 | udelay(1); |
| 642 | } |
| 643 | |
| 644 | cmd = its->cmd_write++; |
| 645 | |
| 646 | /* Handle queue wrapping */ |
| 647 | if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES)) |
| 648 | its->cmd_write = its->cmd_base; |
| 649 | |
Marc Zyngier | 34d677a | 2016-12-19 17:16:45 +0000 | [diff] [blame] | 650 | /* Clear command */ |
| 651 | cmd->raw_cmd[0] = 0; |
| 652 | cmd->raw_cmd[1] = 0; |
| 653 | cmd->raw_cmd[2] = 0; |
| 654 | cmd->raw_cmd[3] = 0; |
| 655 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 656 | return cmd; |
| 657 | } |
| 658 | |
| 659 | static struct its_cmd_block *its_post_commands(struct its_node *its) |
| 660 | { |
| 661 | u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write); |
| 662 | |
| 663 | writel_relaxed(wr, its->base + GITS_CWRITER); |
| 664 | |
| 665 | return its->cmd_write; |
| 666 | } |
| 667 | |
| 668 | static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd) |
| 669 | { |
| 670 | /* |
| 671 | * Make sure the commands written to memory are observable by |
| 672 | * the ITS. |
| 673 | */ |
| 674 | if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 675 | gic_flush_dcache_to_poc(cmd, sizeof(*cmd)); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 676 | else |
| 677 | dsb(ishst); |
| 678 | } |
| 679 | |
| 680 | static void its_wait_for_range_completion(struct its_node *its, |
| 681 | struct its_cmd_block *from, |
| 682 | struct its_cmd_block *to) |
| 683 | { |
| 684 | u64 rd_idx, from_idx, to_idx; |
| 685 | u32 count = 1000000; /* 1s! */ |
| 686 | |
| 687 | from_idx = its_cmd_ptr_to_offset(its, from); |
| 688 | to_idx = its_cmd_ptr_to_offset(its, to); |
| 689 | |
| 690 | while (1) { |
| 691 | rd_idx = readl_relaxed(its->base + GITS_CREADR); |
Marc Zyngier | 9bdd8b1 | 2017-08-19 10:16:02 +0100 | [diff] [blame] | 692 | |
| 693 | /* Direct case */ |
| 694 | if (from_idx < to_idx && rd_idx >= to_idx) |
| 695 | break; |
| 696 | |
| 697 | /* Wrapped case */ |
| 698 | if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx) |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 699 | break; |
| 700 | |
| 701 | count--; |
| 702 | if (!count) { |
| 703 | pr_err_ratelimited("ITS queue timeout\n"); |
| 704 | return; |
| 705 | } |
| 706 | cpu_relax(); |
| 707 | udelay(1); |
| 708 | } |
| 709 | } |
| 710 | |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 711 | /* Warning, macro hell follows */ |
| 712 | #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \ |
| 713 | void name(struct its_node *its, \ |
| 714 | buildtype builder, \ |
| 715 | struct its_cmd_desc *desc) \ |
| 716 | { \ |
| 717 | struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \ |
| 718 | synctype *sync_obj; \ |
| 719 | unsigned long flags; \ |
| 720 | \ |
| 721 | raw_spin_lock_irqsave(&its->lock, flags); \ |
| 722 | \ |
| 723 | cmd = its_allocate_entry(its); \ |
| 724 | if (!cmd) { /* We're soooooo screewed... */ \ |
| 725 | raw_spin_unlock_irqrestore(&its->lock, flags); \ |
| 726 | return; \ |
| 727 | } \ |
| 728 | sync_obj = builder(cmd, desc); \ |
| 729 | its_flush_cmd(its, cmd); \ |
| 730 | \ |
| 731 | if (sync_obj) { \ |
| 732 | sync_cmd = its_allocate_entry(its); \ |
| 733 | if (!sync_cmd) \ |
| 734 | goto post; \ |
| 735 | \ |
| 736 | buildfn(sync_cmd, sync_obj); \ |
| 737 | its_flush_cmd(its, sync_cmd); \ |
| 738 | } \ |
| 739 | \ |
| 740 | post: \ |
| 741 | next_cmd = its_post_commands(its); \ |
| 742 | raw_spin_unlock_irqrestore(&its->lock, flags); \ |
| 743 | \ |
| 744 | its_wait_for_range_completion(its, cmd, next_cmd); \ |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 747 | static void its_build_sync_cmd(struct its_cmd_block *sync_cmd, |
| 748 | struct its_collection *sync_col) |
| 749 | { |
| 750 | its_encode_cmd(sync_cmd, GITS_CMD_SYNC); |
| 751 | its_encode_target(sync_cmd, sync_col->target_address); |
| 752 | |
| 753 | its_fixup_cmd(sync_cmd); |
| 754 | } |
| 755 | |
| 756 | static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t, |
| 757 | struct its_collection, its_build_sync_cmd) |
| 758 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 759 | static void its_build_vsync_cmd(struct its_cmd_block *sync_cmd, |
| 760 | struct its_vpe *sync_vpe) |
| 761 | { |
| 762 | its_encode_cmd(sync_cmd, GITS_CMD_VSYNC); |
| 763 | its_encode_vpeid(sync_cmd, sync_vpe->vpe_id); |
| 764 | |
| 765 | its_fixup_cmd(sync_cmd); |
| 766 | } |
| 767 | |
| 768 | static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t, |
| 769 | struct its_vpe, its_build_vsync_cmd) |
| 770 | |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 771 | static void its_send_int(struct its_device *dev, u32 event_id) |
| 772 | { |
| 773 | struct its_cmd_desc desc; |
| 774 | |
| 775 | desc.its_int_cmd.dev = dev; |
| 776 | desc.its_int_cmd.event_id = event_id; |
| 777 | |
| 778 | its_send_single_command(dev->its, its_build_int_cmd, &desc); |
| 779 | } |
| 780 | |
| 781 | static void its_send_clear(struct its_device *dev, u32 event_id) |
| 782 | { |
| 783 | struct its_cmd_desc desc; |
| 784 | |
| 785 | desc.its_clear_cmd.dev = dev; |
| 786 | desc.its_clear_cmd.event_id = event_id; |
| 787 | |
| 788 | its_send_single_command(dev->its, its_build_clear_cmd, &desc); |
| 789 | } |
| 790 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 791 | static void its_send_inv(struct its_device *dev, u32 event_id) |
| 792 | { |
| 793 | struct its_cmd_desc desc; |
| 794 | |
| 795 | desc.its_inv_cmd.dev = dev; |
| 796 | desc.its_inv_cmd.event_id = event_id; |
| 797 | |
| 798 | its_send_single_command(dev->its, its_build_inv_cmd, &desc); |
| 799 | } |
| 800 | |
| 801 | static void its_send_mapd(struct its_device *dev, int valid) |
| 802 | { |
| 803 | struct its_cmd_desc desc; |
| 804 | |
| 805 | desc.its_mapd_cmd.dev = dev; |
| 806 | desc.its_mapd_cmd.valid = !!valid; |
| 807 | |
| 808 | its_send_single_command(dev->its, its_build_mapd_cmd, &desc); |
| 809 | } |
| 810 | |
| 811 | static void its_send_mapc(struct its_node *its, struct its_collection *col, |
| 812 | int valid) |
| 813 | { |
| 814 | struct its_cmd_desc desc; |
| 815 | |
| 816 | desc.its_mapc_cmd.col = col; |
| 817 | desc.its_mapc_cmd.valid = !!valid; |
| 818 | |
| 819 | its_send_single_command(its, its_build_mapc_cmd, &desc); |
| 820 | } |
| 821 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 822 | 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] | 823 | { |
| 824 | struct its_cmd_desc desc; |
| 825 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 826 | desc.its_mapti_cmd.dev = dev; |
| 827 | desc.its_mapti_cmd.phys_id = irq_id; |
| 828 | desc.its_mapti_cmd.event_id = id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 829 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 830 | its_send_single_command(dev->its, its_build_mapti_cmd, &desc); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | static void its_send_movi(struct its_device *dev, |
| 834 | struct its_collection *col, u32 id) |
| 835 | { |
| 836 | struct its_cmd_desc desc; |
| 837 | |
| 838 | desc.its_movi_cmd.dev = dev; |
| 839 | desc.its_movi_cmd.col = col; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 840 | desc.its_movi_cmd.event_id = id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 841 | |
| 842 | its_send_single_command(dev->its, its_build_movi_cmd, &desc); |
| 843 | } |
| 844 | |
| 845 | static void its_send_discard(struct its_device *dev, u32 id) |
| 846 | { |
| 847 | struct its_cmd_desc desc; |
| 848 | |
| 849 | desc.its_discard_cmd.dev = dev; |
| 850 | desc.its_discard_cmd.event_id = id; |
| 851 | |
| 852 | its_send_single_command(dev->its, its_build_discard_cmd, &desc); |
| 853 | } |
| 854 | |
| 855 | static void its_send_invall(struct its_node *its, struct its_collection *col) |
| 856 | { |
| 857 | struct its_cmd_desc desc; |
| 858 | |
| 859 | desc.its_invall_cmd.col = col; |
| 860 | |
| 861 | its_send_single_command(its, its_build_invall_cmd, &desc); |
| 862 | } |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 863 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 864 | static void its_send_vmapti(struct its_device *dev, u32 id) |
| 865 | { |
| 866 | struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id]; |
| 867 | struct its_cmd_desc desc; |
| 868 | |
| 869 | desc.its_vmapti_cmd.vpe = map->vpe; |
| 870 | desc.its_vmapti_cmd.dev = dev; |
| 871 | desc.its_vmapti_cmd.virt_id = map->vintid; |
| 872 | desc.its_vmapti_cmd.event_id = id; |
| 873 | desc.its_vmapti_cmd.db_enabled = map->db_enabled; |
| 874 | |
| 875 | its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc); |
| 876 | } |
| 877 | |
| 878 | static void its_send_vmovi(struct its_device *dev, u32 id) |
| 879 | { |
| 880 | struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id]; |
| 881 | struct its_cmd_desc desc; |
| 882 | |
| 883 | desc.its_vmovi_cmd.vpe = map->vpe; |
| 884 | desc.its_vmovi_cmd.dev = dev; |
| 885 | desc.its_vmovi_cmd.event_id = id; |
| 886 | desc.its_vmovi_cmd.db_enabled = map->db_enabled; |
| 887 | |
| 888 | its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc); |
| 889 | } |
| 890 | |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 891 | static void its_send_vmapp(struct its_vpe *vpe, bool valid) |
| 892 | { |
| 893 | struct its_cmd_desc desc; |
| 894 | struct its_node *its; |
| 895 | |
| 896 | desc.its_vmapp_cmd.vpe = vpe; |
| 897 | desc.its_vmapp_cmd.valid = valid; |
| 898 | |
| 899 | list_for_each_entry(its, &its_nodes, entry) { |
| 900 | if (!its->is_v4) |
| 901 | continue; |
| 902 | |
| 903 | desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx]; |
| 904 | its_send_single_vcommand(its, its_build_vmapp_cmd, &desc); |
| 905 | } |
| 906 | } |
| 907 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 908 | static void its_send_vmovp(struct its_vpe *vpe) |
| 909 | { |
| 910 | struct its_cmd_desc desc; |
| 911 | struct its_node *its; |
| 912 | unsigned long flags; |
| 913 | int col_id = vpe->col_idx; |
| 914 | |
| 915 | desc.its_vmovp_cmd.vpe = vpe; |
| 916 | desc.its_vmovp_cmd.its_list = (u16)its_list_map; |
| 917 | |
| 918 | if (!its_list_map) { |
| 919 | its = list_first_entry(&its_nodes, struct its_node, entry); |
| 920 | desc.its_vmovp_cmd.seq_num = 0; |
| 921 | desc.its_vmovp_cmd.col = &its->collections[col_id]; |
| 922 | its_send_single_vcommand(its, its_build_vmovp_cmd, &desc); |
| 923 | return; |
| 924 | } |
| 925 | |
| 926 | /* |
| 927 | * Yet another marvel of the architecture. If using the |
| 928 | * its_list "feature", we need to make sure that all ITSs |
| 929 | * receive all VMOVP commands in the same order. The only way |
| 930 | * to guarantee this is to make vmovp a serialization point. |
| 931 | * |
| 932 | * Wall <-- Head. |
| 933 | */ |
| 934 | raw_spin_lock_irqsave(&vmovp_lock, flags); |
| 935 | |
| 936 | desc.its_vmovp_cmd.seq_num = vmovp_seq_num++; |
| 937 | |
| 938 | /* Emit VMOVPs */ |
| 939 | list_for_each_entry(its, &its_nodes, entry) { |
| 940 | if (!its->is_v4) |
| 941 | continue; |
| 942 | |
| 943 | desc.its_vmovp_cmd.col = &its->collections[col_id]; |
| 944 | its_send_single_vcommand(its, its_build_vmovp_cmd, &desc); |
| 945 | } |
| 946 | |
| 947 | raw_spin_unlock_irqrestore(&vmovp_lock, flags); |
| 948 | } |
| 949 | |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 950 | static void its_send_vinvall(struct its_vpe *vpe) |
| 951 | { |
| 952 | struct its_cmd_desc desc; |
| 953 | struct its_node *its; |
| 954 | |
| 955 | desc.its_vinvall_cmd.vpe = vpe; |
| 956 | |
| 957 | list_for_each_entry(its, &its_nodes, entry) { |
| 958 | if (!its->is_v4) |
| 959 | continue; |
| 960 | its_send_single_vcommand(its, its_build_vinvall_cmd, &desc); |
| 961 | } |
| 962 | } |
| 963 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 964 | /* |
| 965 | * irqchip functions - assumes MSI, mostly. |
| 966 | */ |
| 967 | |
| 968 | static inline u32 its_get_event_id(struct irq_data *d) |
| 969 | { |
| 970 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 971 | return d->hwirq - its_dev->event_map.lpi_base; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 972 | } |
| 973 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 974 | 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] | 975 | { |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 976 | irq_hw_number_t hwirq; |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 977 | struct page *prop_page; |
| 978 | u8 *cfg; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 979 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 980 | if (irqd_is_forwarded_to_vcpu(d)) { |
| 981 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 982 | u32 event = its_get_event_id(d); |
| 983 | |
| 984 | prop_page = its_dev->event_map.vm->vprop_page; |
| 985 | hwirq = its_dev->event_map.vlpi_maps[event].vintid; |
| 986 | } else { |
| 987 | prop_page = gic_rdists->prop_page; |
| 988 | hwirq = d->hwirq; |
| 989 | } |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 990 | |
| 991 | cfg = page_address(prop_page) + hwirq - 8192; |
| 992 | *cfg &= ~clr; |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 993 | *cfg |= set | LPI_PROP_GROUP1; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 994 | |
| 995 | /* |
| 996 | * Make the above write visible to the redistributors. |
| 997 | * And yes, we're flushing exactly: One. Single. Byte. |
| 998 | * Humpf... |
| 999 | */ |
| 1000 | if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 1001 | gic_flush_dcache_to_poc(cfg, sizeof(*cfg)); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1002 | else |
| 1003 | dsb(ishst); |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
| 1006 | static void lpi_update_config(struct irq_data *d, u8 clr, u8 set) |
| 1007 | { |
| 1008 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1009 | |
| 1010 | lpi_write_config(d, clr, set); |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1011 | its_send_inv(its_dev, its_get_event_id(d)); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1014 | static void its_vlpi_set_doorbell(struct irq_data *d, bool enable) |
| 1015 | { |
| 1016 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1017 | u32 event = its_get_event_id(d); |
| 1018 | |
| 1019 | if (its_dev->event_map.vlpi_maps[event].db_enabled == enable) |
| 1020 | return; |
| 1021 | |
| 1022 | its_dev->event_map.vlpi_maps[event].db_enabled = enable; |
| 1023 | |
| 1024 | /* |
| 1025 | * More fun with the architecture: |
| 1026 | * |
| 1027 | * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI |
| 1028 | * value or to 1023, depending on the enable bit. But that |
| 1029 | * would be issueing a mapping for an /existing/ DevID+EventID |
| 1030 | * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI |
| 1031 | * to the /same/ vPE, using this opportunity to adjust the |
| 1032 | * doorbell. Mouahahahaha. We loves it, Precious. |
| 1033 | */ |
| 1034 | its_send_vmovi(its_dev, event); |
| 1035 | } |
| 1036 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1037 | static void its_mask_irq(struct irq_data *d) |
| 1038 | { |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1039 | if (irqd_is_forwarded_to_vcpu(d)) |
| 1040 | its_vlpi_set_doorbell(d, false); |
| 1041 | |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1042 | lpi_update_config(d, LPI_PROP_ENABLED, 0); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
| 1045 | static void its_unmask_irq(struct irq_data *d) |
| 1046 | { |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1047 | if (irqd_is_forwarded_to_vcpu(d)) |
| 1048 | its_vlpi_set_doorbell(d, true); |
| 1049 | |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 1050 | lpi_update_config(d, 0, LPI_PROP_ENABLED); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1053 | static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val, |
| 1054 | bool force) |
| 1055 | { |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 1056 | unsigned int cpu; |
| 1057 | const struct cpumask *cpu_mask = cpu_online_mask; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1058 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1059 | struct its_collection *target_col; |
| 1060 | u32 id = its_get_event_id(d); |
| 1061 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1062 | /* A forwarded interrupt should use irq_set_vcpu_affinity */ |
| 1063 | if (irqd_is_forwarded_to_vcpu(d)) |
| 1064 | return -EINVAL; |
| 1065 | |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 1066 | /* lpi cannot be routed to a redistributor that is on a foreign node */ |
| 1067 | if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) { |
| 1068 | if (its_dev->its->numa_node >= 0) { |
| 1069 | cpu_mask = cpumask_of_node(its_dev->its->numa_node); |
| 1070 | if (!cpumask_intersects(mask_val, cpu_mask)) |
| 1071 | return -EINVAL; |
| 1072 | } |
| 1073 | } |
| 1074 | |
| 1075 | cpu = cpumask_any_and(mask_val, cpu_mask); |
| 1076 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1077 | if (cpu >= nr_cpu_ids) |
| 1078 | return -EINVAL; |
| 1079 | |
MaJun | 8b8d94a | 2017-05-18 16:19:13 +0800 | [diff] [blame] | 1080 | /* don't set the affinity when the target cpu is same as current one */ |
| 1081 | if (cpu != its_dev->event_map.col_map[id]) { |
| 1082 | target_col = &its_dev->its->collections[cpu]; |
| 1083 | its_send_movi(its_dev, target_col, id); |
| 1084 | its_dev->event_map.col_map[id] = cpu; |
| 1085 | } |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1086 | |
| 1087 | return IRQ_SET_MASK_OK_DONE; |
| 1088 | } |
| 1089 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1090 | static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg) |
| 1091 | { |
| 1092 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1093 | struct its_node *its; |
| 1094 | u64 addr; |
| 1095 | |
| 1096 | its = its_dev->its; |
| 1097 | addr = its->phys_base + GITS_TRANSLATER; |
| 1098 | |
Vladimir Murzin | b11283e | 2016-11-02 11:54:03 +0000 | [diff] [blame] | 1099 | msg->address_lo = lower_32_bits(addr); |
| 1100 | msg->address_hi = upper_32_bits(addr); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1101 | msg->data = its_get_event_id(d); |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 1102 | |
| 1103 | iommu_dma_map_msi_msg(d->irq, msg); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 1106 | static int its_irq_set_irqchip_state(struct irq_data *d, |
| 1107 | enum irqchip_irq_state which, |
| 1108 | bool state) |
| 1109 | { |
| 1110 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1111 | u32 event = its_get_event_id(d); |
| 1112 | |
| 1113 | if (which != IRQCHIP_STATE_PENDING) |
| 1114 | return -EINVAL; |
| 1115 | |
| 1116 | if (state) |
| 1117 | its_send_int(its_dev, event); |
| 1118 | else |
| 1119 | its_send_clear(its_dev, event); |
| 1120 | |
| 1121 | return 0; |
| 1122 | } |
| 1123 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1124 | static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) |
| 1125 | { |
| 1126 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1127 | u32 event = its_get_event_id(d); |
| 1128 | int ret = 0; |
| 1129 | |
| 1130 | if (!info->map) |
| 1131 | return -EINVAL; |
| 1132 | |
| 1133 | mutex_lock(&its_dev->event_map.vlpi_lock); |
| 1134 | |
| 1135 | if (!its_dev->event_map.vm) { |
| 1136 | struct its_vlpi_map *maps; |
| 1137 | |
| 1138 | maps = kzalloc(sizeof(*maps) * its_dev->event_map.nr_lpis, |
| 1139 | GFP_KERNEL); |
| 1140 | if (!maps) { |
| 1141 | ret = -ENOMEM; |
| 1142 | goto out; |
| 1143 | } |
| 1144 | |
| 1145 | its_dev->event_map.vm = info->map->vm; |
| 1146 | its_dev->event_map.vlpi_maps = maps; |
| 1147 | } else if (its_dev->event_map.vm != info->map->vm) { |
| 1148 | ret = -EINVAL; |
| 1149 | goto out; |
| 1150 | } |
| 1151 | |
| 1152 | /* Get our private copy of the mapping information */ |
| 1153 | its_dev->event_map.vlpi_maps[event] = *info->map; |
| 1154 | |
| 1155 | if (irqd_is_forwarded_to_vcpu(d)) { |
| 1156 | /* Already mapped, move it around */ |
| 1157 | its_send_vmovi(its_dev, event); |
| 1158 | } else { |
| 1159 | /* Drop the physical mapping */ |
| 1160 | its_send_discard(its_dev, event); |
| 1161 | |
| 1162 | /* and install the virtual one */ |
| 1163 | its_send_vmapti(its_dev, event); |
| 1164 | irqd_set_forwarded_to_vcpu(d); |
| 1165 | |
| 1166 | /* Increment the number of VLPIs */ |
| 1167 | its_dev->event_map.nr_vlpis++; |
| 1168 | } |
| 1169 | |
| 1170 | out: |
| 1171 | mutex_unlock(&its_dev->event_map.vlpi_lock); |
| 1172 | return ret; |
| 1173 | } |
| 1174 | |
| 1175 | static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info) |
| 1176 | { |
| 1177 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1178 | u32 event = its_get_event_id(d); |
| 1179 | int ret = 0; |
| 1180 | |
| 1181 | mutex_lock(&its_dev->event_map.vlpi_lock); |
| 1182 | |
| 1183 | if (!its_dev->event_map.vm || |
| 1184 | !its_dev->event_map.vlpi_maps[event].vm) { |
| 1185 | ret = -EINVAL; |
| 1186 | goto out; |
| 1187 | } |
| 1188 | |
| 1189 | /* Copy our mapping information to the incoming request */ |
| 1190 | *info->map = its_dev->event_map.vlpi_maps[event]; |
| 1191 | |
| 1192 | out: |
| 1193 | mutex_unlock(&its_dev->event_map.vlpi_lock); |
| 1194 | return ret; |
| 1195 | } |
| 1196 | |
| 1197 | static int its_vlpi_unmap(struct irq_data *d) |
| 1198 | { |
| 1199 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1200 | u32 event = its_get_event_id(d); |
| 1201 | int ret = 0; |
| 1202 | |
| 1203 | mutex_lock(&its_dev->event_map.vlpi_lock); |
| 1204 | |
| 1205 | if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) { |
| 1206 | ret = -EINVAL; |
| 1207 | goto out; |
| 1208 | } |
| 1209 | |
| 1210 | /* Drop the virtual mapping */ |
| 1211 | its_send_discard(its_dev, event); |
| 1212 | |
| 1213 | /* and restore the physical one */ |
| 1214 | irqd_clr_forwarded_to_vcpu(d); |
| 1215 | its_send_mapti(its_dev, d->hwirq, event); |
| 1216 | lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO | |
| 1217 | LPI_PROP_ENABLED | |
| 1218 | LPI_PROP_GROUP1)); |
| 1219 | |
| 1220 | /* |
| 1221 | * Drop the refcount and make the device available again if |
| 1222 | * this was the last VLPI. |
| 1223 | */ |
| 1224 | if (!--its_dev->event_map.nr_vlpis) { |
| 1225 | its_dev->event_map.vm = NULL; |
| 1226 | kfree(its_dev->event_map.vlpi_maps); |
| 1227 | } |
| 1228 | |
| 1229 | out: |
| 1230 | mutex_unlock(&its_dev->event_map.vlpi_lock); |
| 1231 | return ret; |
| 1232 | } |
| 1233 | |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1234 | static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info) |
| 1235 | { |
| 1236 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1237 | |
| 1238 | if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) |
| 1239 | return -EINVAL; |
| 1240 | |
| 1241 | if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI) |
| 1242 | lpi_update_config(d, 0xff, info->config); |
| 1243 | else |
| 1244 | lpi_write_config(d, 0xff, info->config); |
| 1245 | its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED)); |
| 1246 | |
| 1247 | return 0; |
| 1248 | } |
| 1249 | |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1250 | static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) |
| 1251 | { |
| 1252 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1253 | struct its_cmd_info *info = vcpu_info; |
| 1254 | |
| 1255 | /* Need a v4 ITS */ |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1256 | if (!its_dev->its->is_v4) |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1257 | return -EINVAL; |
| 1258 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1259 | /* Unmap request? */ |
| 1260 | if (!info) |
| 1261 | return its_vlpi_unmap(d); |
| 1262 | |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1263 | switch (info->cmd_type) { |
| 1264 | case MAP_VLPI: |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1265 | return its_vlpi_map(d, info); |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1266 | |
| 1267 | case GET_VLPI: |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 1268 | return its_vlpi_get(d, info); |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1269 | |
| 1270 | case PROP_UPDATE_VLPI: |
| 1271 | case PROP_UPDATE_AND_INV_VLPI: |
Marc Zyngier | 015ec03 | 2016-12-20 09:54:57 +0000 | [diff] [blame] | 1272 | return its_vlpi_prop_update(d, info); |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1273 | |
| 1274 | default: |
| 1275 | return -EINVAL; |
| 1276 | } |
| 1277 | } |
| 1278 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1279 | static struct irq_chip its_irq_chip = { |
| 1280 | .name = "ITS", |
| 1281 | .irq_mask = its_mask_irq, |
| 1282 | .irq_unmask = its_unmask_irq, |
Ashok Kumar | 004fa08 | 2016-02-11 05:38:53 -0800 | [diff] [blame] | 1283 | .irq_eoi = irq_chip_eoi_parent, |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1284 | .irq_set_affinity = its_set_affinity, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1285 | .irq_compose_msi_msg = its_irq_compose_msi_msg, |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 1286 | .irq_set_irqchip_state = its_irq_set_irqchip_state, |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1287 | .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1288 | }; |
| 1289 | |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1290 | /* |
| 1291 | * How we allocate LPIs: |
| 1292 | * |
| 1293 | * The GIC has id_bits bits for interrupt identifiers. From there, we |
| 1294 | * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as |
| 1295 | * we allocate LPIs by chunks of 32, we can shift the whole thing by 5 |
| 1296 | * bits to the right. |
| 1297 | * |
| 1298 | * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations. |
| 1299 | */ |
| 1300 | #define IRQS_PER_CHUNK_SHIFT 5 |
| 1301 | #define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT) |
Shanker Donthineni | 6c31e12 | 2017-06-22 18:19:14 -0500 | [diff] [blame] | 1302 | #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */ |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1303 | |
| 1304 | static unsigned long *lpi_bitmap; |
| 1305 | static u32 lpi_chunks; |
| 1306 | static DEFINE_SPINLOCK(lpi_lock); |
| 1307 | |
| 1308 | static int its_lpi_to_chunk(int lpi) |
| 1309 | { |
| 1310 | return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT; |
| 1311 | } |
| 1312 | |
| 1313 | static int its_chunk_to_lpi(int chunk) |
| 1314 | { |
| 1315 | return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192; |
| 1316 | } |
| 1317 | |
Tomasz Nowicki | 04a0e4d | 2016-01-19 14:11:18 +0100 | [diff] [blame] | 1318 | static int __init its_lpi_init(u32 id_bits) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1319 | { |
| 1320 | lpi_chunks = its_lpi_to_chunk(1UL << id_bits); |
| 1321 | |
| 1322 | lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long), |
| 1323 | GFP_KERNEL); |
| 1324 | if (!lpi_bitmap) { |
| 1325 | lpi_chunks = 0; |
| 1326 | return -ENOMEM; |
| 1327 | } |
| 1328 | |
| 1329 | pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks); |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
| 1333 | static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids) |
| 1334 | { |
| 1335 | unsigned long *bitmap = NULL; |
| 1336 | int chunk_id; |
| 1337 | int nr_chunks; |
| 1338 | int i; |
| 1339 | |
| 1340 | nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK); |
| 1341 | |
| 1342 | spin_lock(&lpi_lock); |
| 1343 | |
| 1344 | do { |
| 1345 | chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks, |
| 1346 | 0, nr_chunks, 0); |
| 1347 | if (chunk_id < lpi_chunks) |
| 1348 | break; |
| 1349 | |
| 1350 | nr_chunks--; |
| 1351 | } while (nr_chunks > 0); |
| 1352 | |
| 1353 | if (!nr_chunks) |
| 1354 | goto out; |
| 1355 | |
| 1356 | bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long), |
| 1357 | GFP_ATOMIC); |
| 1358 | if (!bitmap) |
| 1359 | goto out; |
| 1360 | |
| 1361 | for (i = 0; i < nr_chunks; i++) |
| 1362 | set_bit(chunk_id + i, lpi_bitmap); |
| 1363 | |
| 1364 | *base = its_chunk_to_lpi(chunk_id); |
| 1365 | *nr_ids = nr_chunks * IRQS_PER_CHUNK; |
| 1366 | |
| 1367 | out: |
| 1368 | spin_unlock(&lpi_lock); |
| 1369 | |
Marc Zyngier | c8415b9 | 2015-10-02 16:44:05 +0100 | [diff] [blame] | 1370 | if (!bitmap) |
| 1371 | *base = *nr_ids = 0; |
| 1372 | |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1373 | return bitmap; |
| 1374 | } |
| 1375 | |
Marc Zyngier | cf2be8b | 2016-12-19 18:49:59 +0000 | [diff] [blame] | 1376 | static void its_lpi_free_chunks(unsigned long *bitmap, int base, int nr_ids) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1377 | { |
| 1378 | int lpi; |
| 1379 | |
| 1380 | spin_lock(&lpi_lock); |
| 1381 | |
| 1382 | for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) { |
| 1383 | int chunk = its_lpi_to_chunk(lpi); |
Marc Zyngier | cf2be8b | 2016-12-19 18:49:59 +0000 | [diff] [blame] | 1384 | |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1385 | BUG_ON(chunk > lpi_chunks); |
| 1386 | if (test_bit(chunk, lpi_bitmap)) { |
| 1387 | clear_bit(chunk, lpi_bitmap); |
| 1388 | } else { |
| 1389 | pr_err("Bad LPI chunk %d\n", chunk); |
| 1390 | } |
| 1391 | } |
| 1392 | |
| 1393 | spin_unlock(&lpi_lock); |
| 1394 | |
Marc Zyngier | cf2be8b | 2016-12-19 18:49:59 +0000 | [diff] [blame] | 1395 | kfree(bitmap); |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1396 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1397 | |
Marc Zyngier | 0e5ccf9 | 2016-12-19 18:15:05 +0000 | [diff] [blame] | 1398 | static struct page *its_allocate_prop_table(gfp_t gfp_flags) |
| 1399 | { |
| 1400 | struct page *prop_page; |
| 1401 | |
| 1402 | prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ)); |
| 1403 | if (!prop_page) |
| 1404 | return NULL; |
| 1405 | |
| 1406 | /* Priority 0xa0, Group-1, disabled */ |
| 1407 | memset(page_address(prop_page), |
| 1408 | LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, |
| 1409 | LPI_PROPBASE_SZ); |
| 1410 | |
| 1411 | /* Make sure the GIC will observe the written configuration */ |
| 1412 | gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ); |
| 1413 | |
| 1414 | return prop_page; |
| 1415 | } |
| 1416 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 1417 | static void its_free_prop_table(struct page *prop_page) |
| 1418 | { |
| 1419 | free_pages((unsigned long)page_address(prop_page), |
| 1420 | get_order(LPI_PROPBASE_SZ)); |
| 1421 | } |
Marc Zyngier | 0e5ccf9 | 2016-12-19 18:15:05 +0000 | [diff] [blame] | 1422 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1423 | static int __init its_alloc_lpi_tables(void) |
| 1424 | { |
| 1425 | phys_addr_t paddr; |
| 1426 | |
Shanker Donthineni | 6c31e12 | 2017-06-22 18:19:14 -0500 | [diff] [blame] | 1427 | lpi_id_bits = min_t(u32, gic_rdists->id_bits, ITS_MAX_LPI_NRBITS); |
Marc Zyngier | 0e5ccf9 | 2016-12-19 18:15:05 +0000 | [diff] [blame] | 1428 | gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1429 | if (!gic_rdists->prop_page) { |
| 1430 | pr_err("Failed to allocate PROPBASE\n"); |
| 1431 | return -ENOMEM; |
| 1432 | } |
| 1433 | |
| 1434 | paddr = page_to_phys(gic_rdists->prop_page); |
| 1435 | pr_info("GIC: using LPI property table @%pa\n", &paddr); |
| 1436 | |
Shanker Donthineni | 6c31e12 | 2017-06-22 18:19:14 -0500 | [diff] [blame] | 1437 | return its_lpi_init(lpi_id_bits); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | static const char *its_base_type_string[] = { |
| 1441 | [GITS_BASER_TYPE_DEVICE] = "Devices", |
| 1442 | [GITS_BASER_TYPE_VCPU] = "Virtual CPUs", |
Marc Zyngier | 4f46de9 | 2016-12-20 15:50:14 +0000 | [diff] [blame] | 1443 | [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)", |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1444 | [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections", |
| 1445 | [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)", |
| 1446 | [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)", |
| 1447 | [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)", |
| 1448 | }; |
| 1449 | |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 1450 | static u64 its_read_baser(struct its_node *its, struct its_baser *baser) |
| 1451 | { |
| 1452 | u32 idx = baser - its->tables; |
| 1453 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1454 | return gits_read_baser(its->base + GITS_BASER + (idx << 3)); |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 1455 | } |
| 1456 | |
| 1457 | static void its_write_baser(struct its_node *its, struct its_baser *baser, |
| 1458 | u64 val) |
| 1459 | { |
| 1460 | u32 idx = baser - its->tables; |
| 1461 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1462 | gits_write_baser(val, its->base + GITS_BASER + (idx << 3)); |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 1463 | baser->val = its_read_baser(its, baser); |
| 1464 | } |
| 1465 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1466 | 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] | 1467 | u64 cache, u64 shr, u32 psz, u32 order, |
| 1468 | bool indirect) |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1469 | { |
| 1470 | u64 val = its_read_baser(its, baser); |
| 1471 | u64 esz = GITS_BASER_ENTRY_SIZE(val); |
| 1472 | u64 type = GITS_BASER_TYPE(val); |
| 1473 | u32 alloc_pages; |
| 1474 | void *base; |
| 1475 | u64 tmp; |
| 1476 | |
| 1477 | retry_alloc_baser: |
| 1478 | alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz); |
| 1479 | if (alloc_pages > GITS_BASER_PAGES_MAX) { |
| 1480 | pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n", |
| 1481 | &its->phys_base, its_base_type_string[type], |
| 1482 | alloc_pages, GITS_BASER_PAGES_MAX); |
| 1483 | alloc_pages = GITS_BASER_PAGES_MAX; |
| 1484 | order = get_order(GITS_BASER_PAGES_MAX * psz); |
| 1485 | } |
| 1486 | |
| 1487 | base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 1488 | if (!base) |
| 1489 | return -ENOMEM; |
| 1490 | |
| 1491 | retry_baser: |
| 1492 | val = (virt_to_phys(base) | |
| 1493 | (type << GITS_BASER_TYPE_SHIFT) | |
| 1494 | ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) | |
| 1495 | ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) | |
| 1496 | cache | |
| 1497 | shr | |
| 1498 | GITS_BASER_VALID); |
| 1499 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1500 | val |= indirect ? GITS_BASER_INDIRECT : 0x0; |
| 1501 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1502 | switch (psz) { |
| 1503 | case SZ_4K: |
| 1504 | val |= GITS_BASER_PAGE_SIZE_4K; |
| 1505 | break; |
| 1506 | case SZ_16K: |
| 1507 | val |= GITS_BASER_PAGE_SIZE_16K; |
| 1508 | break; |
| 1509 | case SZ_64K: |
| 1510 | val |= GITS_BASER_PAGE_SIZE_64K; |
| 1511 | break; |
| 1512 | } |
| 1513 | |
| 1514 | its_write_baser(its, baser, val); |
| 1515 | tmp = baser->val; |
| 1516 | |
| 1517 | if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) { |
| 1518 | /* |
| 1519 | * Shareability didn't stick. Just use |
| 1520 | * whatever the read reported, which is likely |
| 1521 | * to be the only thing this redistributor |
| 1522 | * supports. If that's zero, make it |
| 1523 | * non-cacheable as well. |
| 1524 | */ |
| 1525 | shr = tmp & GITS_BASER_SHAREABILITY_MASK; |
| 1526 | if (!shr) { |
| 1527 | cache = GITS_BASER_nC; |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 1528 | gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order)); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1529 | } |
| 1530 | goto retry_baser; |
| 1531 | } |
| 1532 | |
| 1533 | if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) { |
| 1534 | /* |
| 1535 | * Page size didn't stick. Let's try a smaller |
| 1536 | * size and retry. If we reach 4K, then |
| 1537 | * something is horribly wrong... |
| 1538 | */ |
| 1539 | free_pages((unsigned long)base, order); |
| 1540 | baser->base = NULL; |
| 1541 | |
| 1542 | switch (psz) { |
| 1543 | case SZ_16K: |
| 1544 | psz = SZ_4K; |
| 1545 | goto retry_alloc_baser; |
| 1546 | case SZ_64K: |
| 1547 | psz = SZ_16K; |
| 1548 | goto retry_alloc_baser; |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | if (val != tmp) { |
Vladimir Murzin | b11283e | 2016-11-02 11:54:03 +0000 | [diff] [blame] | 1553 | pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n", |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1554 | &its->phys_base, its_base_type_string[type], |
Vladimir Murzin | b11283e | 2016-11-02 11:54:03 +0000 | [diff] [blame] | 1555 | val, tmp); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1556 | free_pages((unsigned long)base, order); |
| 1557 | return -ENXIO; |
| 1558 | } |
| 1559 | |
| 1560 | baser->order = order; |
| 1561 | baser->base = base; |
| 1562 | baser->psz = psz; |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1563 | tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1564 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1565 | 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] | 1566 | &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp), |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1567 | its_base_type_string[type], |
| 1568 | (unsigned long)virt_to_phys(base), |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1569 | indirect ? "indirect" : "flat", (int)esz, |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1570 | psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT); |
| 1571 | |
| 1572 | return 0; |
| 1573 | } |
| 1574 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1575 | static bool its_parse_indirect_baser(struct its_node *its, |
| 1576 | struct its_baser *baser, |
| 1577 | u32 psz, u32 *order) |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1578 | { |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1579 | u64 tmp = its_read_baser(its, baser); |
| 1580 | u64 type = GITS_BASER_TYPE(tmp); |
| 1581 | u64 esz = GITS_BASER_ENTRY_SIZE(tmp); |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 1582 | u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb; |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1583 | u32 ids = its->device_ids; |
| 1584 | u32 new_order = *order; |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1585 | bool indirect = false; |
| 1586 | |
| 1587 | /* No need to enable Indirection if memory requirement < (psz*2)bytes */ |
| 1588 | if ((esz << ids) > (psz * 2)) { |
| 1589 | /* |
| 1590 | * Find out whether hw supports a single or two-level table by |
| 1591 | * table by reading bit at offset '62' after writing '1' to it. |
| 1592 | */ |
| 1593 | its_write_baser(its, baser, val | GITS_BASER_INDIRECT); |
| 1594 | indirect = !!(baser->val & GITS_BASER_INDIRECT); |
| 1595 | |
| 1596 | if (indirect) { |
| 1597 | /* |
| 1598 | * The size of the lvl2 table is equal to ITS page size |
| 1599 | * which is 'psz'. For computing lvl1 table size, |
| 1600 | * subtract ID bits that sparse lvl2 table from 'ids' |
| 1601 | * which is reported by ITS hardware times lvl1 table |
| 1602 | * entry size. |
| 1603 | */ |
Vladimir Murzin | d524eaa | 2016-11-02 11:54:04 +0000 | [diff] [blame] | 1604 | ids -= ilog2(psz / (int)esz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1605 | esz = GITS_LVL1_ENTRY_SIZE; |
| 1606 | } |
| 1607 | } |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1608 | |
| 1609 | /* |
| 1610 | * Allocate as many entries as required to fit the |
| 1611 | * range of device IDs that the ITS can grok... The ID |
| 1612 | * space being incredibly sparse, this results in a |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1613 | * massive waste of memory if two-level device table |
| 1614 | * feature is not supported by hardware. |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1615 | */ |
| 1616 | new_order = max_t(u32, get_order(esz << ids), new_order); |
| 1617 | if (new_order >= MAX_ORDER) { |
| 1618 | new_order = MAX_ORDER - 1; |
Vladimir Murzin | d524eaa | 2016-11-02 11:54:04 +0000 | [diff] [blame] | 1619 | ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz); |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1620 | pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n", |
| 1621 | &its->phys_base, its_base_type_string[type], |
| 1622 | its->device_ids, ids); |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | *order = new_order; |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1626 | |
| 1627 | return indirect; |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1628 | } |
| 1629 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1630 | static void its_free_tables(struct its_node *its) |
| 1631 | { |
| 1632 | int i; |
| 1633 | |
| 1634 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
Shanker Donthineni | 1a485f4 | 2016-02-01 20:19:44 -0600 | [diff] [blame] | 1635 | if (its->tables[i].base) { |
| 1636 | free_pages((unsigned long)its->tables[i].base, |
| 1637 | its->tables[i].order); |
| 1638 | its->tables[i].base = NULL; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1639 | } |
| 1640 | } |
| 1641 | } |
| 1642 | |
Shanker Donthineni | 0e0b0f6 | 2016-06-06 18:17:31 -0500 | [diff] [blame] | 1643 | static int its_alloc_tables(struct its_node *its) |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1644 | { |
Marc Zyngier | 589ce5f | 2016-10-14 15:13:07 +0100 | [diff] [blame] | 1645 | u64 typer = gic_read_typer(its->base + GITS_TYPER); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1646 | u32 ids = GITS_TYPER_DEVBITS(typer); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1647 | u64 shr = GITS_BASER_InnerShareable; |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 1648 | u64 cache = GITS_BASER_RaWaWb; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1649 | u32 psz = SZ_64K; |
| 1650 | int err, i; |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 1651 | |
| 1652 | if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) { |
| 1653 | /* |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1654 | * erratum 22375: only alloc 8MB table size |
| 1655 | * erratum 24313: ignore memory access type |
| 1656 | */ |
| 1657 | cache = GITS_BASER_nCnB; |
| 1658 | ids = 0x14; /* 20 bits, 8MB */ |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 1659 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1660 | |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 1661 | its->device_ids = ids; |
| 1662 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1663 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 1664 | struct its_baser *baser = its->tables + i; |
| 1665 | u64 val = its_read_baser(its, baser); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1666 | u64 type = GITS_BASER_TYPE(val); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1667 | u32 order = get_order(psz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1668 | bool indirect = false; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1669 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1670 | switch (type) { |
| 1671 | case GITS_BASER_TYPE_NONE: |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1672 | continue; |
| 1673 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1674 | case GITS_BASER_TYPE_DEVICE: |
| 1675 | case GITS_BASER_TYPE_VCPU: |
| 1676 | indirect = its_parse_indirect_baser(its, baser, |
| 1677 | psz, &order); |
| 1678 | break; |
| 1679 | } |
Marc Zyngier | f54b97e | 2015-03-06 16:37:41 +0000 | [diff] [blame] | 1680 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1681 | err = its_setup_baser(its, baser, cache, shr, psz, order, indirect); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1682 | if (err < 0) { |
| 1683 | its_free_tables(its); |
| 1684 | return err; |
Robert Richter | 30f2136 | 2015-09-21 22:58:34 +0200 | [diff] [blame] | 1685 | } |
| 1686 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1687 | /* Update settings which will be used for next BASERn */ |
| 1688 | psz = baser->psz; |
| 1689 | cache = baser->val & GITS_BASER_CACHEABILITY_MASK; |
| 1690 | shr = baser->val & GITS_BASER_SHAREABILITY_MASK; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | return 0; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
| 1696 | static int its_alloc_collections(struct its_node *its) |
| 1697 | { |
| 1698 | its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections), |
| 1699 | GFP_KERNEL); |
| 1700 | if (!its->collections) |
| 1701 | return -ENOMEM; |
| 1702 | |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
Marc Zyngier | 7c297a2 | 2016-12-19 18:34:38 +0000 | [diff] [blame] | 1706 | static struct page *its_allocate_pending_table(gfp_t gfp_flags) |
| 1707 | { |
| 1708 | struct page *pend_page; |
| 1709 | /* |
| 1710 | * The pending pages have to be at least 64kB aligned, |
| 1711 | * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below. |
| 1712 | */ |
| 1713 | pend_page = alloc_pages(gfp_flags | __GFP_ZERO, |
| 1714 | get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K))); |
| 1715 | if (!pend_page) |
| 1716 | return NULL; |
| 1717 | |
| 1718 | /* Make sure the GIC will observe the zero-ed page */ |
| 1719 | gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ); |
| 1720 | |
| 1721 | return pend_page; |
| 1722 | } |
| 1723 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 1724 | static void its_free_pending_table(struct page *pt) |
| 1725 | { |
| 1726 | free_pages((unsigned long)page_address(pt), |
| 1727 | get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K))); |
| 1728 | } |
| 1729 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1730 | static void its_cpu_init_lpis(void) |
| 1731 | { |
| 1732 | void __iomem *rbase = gic_data_rdist_rd_base(); |
| 1733 | struct page *pend_page; |
| 1734 | u64 val, tmp; |
| 1735 | |
| 1736 | /* If we didn't allocate the pending table yet, do it now */ |
| 1737 | pend_page = gic_data_rdist()->pend_page; |
| 1738 | if (!pend_page) { |
| 1739 | phys_addr_t paddr; |
Marc Zyngier | 7c297a2 | 2016-12-19 18:34:38 +0000 | [diff] [blame] | 1740 | |
| 1741 | pend_page = its_allocate_pending_table(GFP_NOWAIT); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1742 | if (!pend_page) { |
| 1743 | pr_err("Failed to allocate PENDBASE for CPU%d\n", |
| 1744 | smp_processor_id()); |
| 1745 | return; |
| 1746 | } |
| 1747 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1748 | paddr = page_to_phys(pend_page); |
| 1749 | pr_info("CPU%d: using LPI pending table @%pa\n", |
| 1750 | smp_processor_id(), &paddr); |
| 1751 | gic_data_rdist()->pend_page = pend_page; |
| 1752 | } |
| 1753 | |
| 1754 | /* Disable LPIs */ |
| 1755 | val = readl_relaxed(rbase + GICR_CTLR); |
| 1756 | val &= ~GICR_CTLR_ENABLE_LPIS; |
| 1757 | writel_relaxed(val, rbase + GICR_CTLR); |
| 1758 | |
| 1759 | /* |
| 1760 | * Make sure any change to the table is observable by the GIC. |
| 1761 | */ |
| 1762 | dsb(sy); |
| 1763 | |
| 1764 | /* set PROPBASE */ |
| 1765 | val = (page_to_phys(gic_rdists->prop_page) | |
| 1766 | GICR_PROPBASER_InnerShareable | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 1767 | GICR_PROPBASER_RaWaWb | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1768 | ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK)); |
| 1769 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1770 | gicr_write_propbaser(val, rbase + GICR_PROPBASER); |
| 1771 | tmp = gicr_read_propbaser(rbase + GICR_PROPBASER); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1772 | |
| 1773 | if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) { |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 1774 | if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) { |
| 1775 | /* |
| 1776 | * The HW reports non-shareable, we must |
| 1777 | * remove the cacheability attributes as |
| 1778 | * well. |
| 1779 | */ |
| 1780 | val &= ~(GICR_PROPBASER_SHAREABILITY_MASK | |
| 1781 | GICR_PROPBASER_CACHEABILITY_MASK); |
| 1782 | val |= GICR_PROPBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1783 | gicr_write_propbaser(val, rbase + GICR_PROPBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 1784 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1785 | pr_info_once("GIC: using cache flushing for LPI property table\n"); |
| 1786 | gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING; |
| 1787 | } |
| 1788 | |
| 1789 | /* set PENDBASE */ |
| 1790 | val = (page_to_phys(pend_page) | |
Marc Zyngier | 4ad3e36 | 2015-03-27 14:15:04 +0000 | [diff] [blame] | 1791 | GICR_PENDBASER_InnerShareable | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 1792 | GICR_PENDBASER_RaWaWb); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1793 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1794 | gicr_write_pendbaser(val, rbase + GICR_PENDBASER); |
| 1795 | tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 1796 | |
| 1797 | if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) { |
| 1798 | /* |
| 1799 | * The HW reports non-shareable, we must remove the |
| 1800 | * cacheability attributes as well. |
| 1801 | */ |
| 1802 | val &= ~(GICR_PENDBASER_SHAREABILITY_MASK | |
| 1803 | GICR_PENDBASER_CACHEABILITY_MASK); |
| 1804 | val |= GICR_PENDBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1805 | gicr_write_pendbaser(val, rbase + GICR_PENDBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 1806 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1807 | |
| 1808 | /* Enable LPIs */ |
| 1809 | val = readl_relaxed(rbase + GICR_CTLR); |
| 1810 | val |= GICR_CTLR_ENABLE_LPIS; |
| 1811 | writel_relaxed(val, rbase + GICR_CTLR); |
| 1812 | |
| 1813 | /* Make sure the GIC has seen the above */ |
| 1814 | dsb(sy); |
| 1815 | } |
| 1816 | |
| 1817 | static void its_cpu_init_collection(void) |
| 1818 | { |
| 1819 | struct its_node *its; |
| 1820 | int cpu; |
| 1821 | |
| 1822 | spin_lock(&its_lock); |
| 1823 | cpu = smp_processor_id(); |
| 1824 | |
| 1825 | list_for_each_entry(its, &its_nodes, entry) { |
| 1826 | u64 target; |
| 1827 | |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 1828 | /* avoid cross node collections and its mapping */ |
| 1829 | if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) { |
| 1830 | struct device_node *cpu_node; |
| 1831 | |
| 1832 | cpu_node = of_get_cpu_node(cpu, NULL); |
| 1833 | if (its->numa_node != NUMA_NO_NODE && |
| 1834 | its->numa_node != of_node_to_nid(cpu_node)) |
| 1835 | continue; |
| 1836 | } |
| 1837 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1838 | /* |
| 1839 | * We now have to bind each collection to its target |
| 1840 | * redistributor. |
| 1841 | */ |
Marc Zyngier | 589ce5f | 2016-10-14 15:13:07 +0100 | [diff] [blame] | 1842 | if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) { |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1843 | /* |
| 1844 | * This ITS wants the physical address of the |
| 1845 | * redistributor. |
| 1846 | */ |
| 1847 | target = gic_data_rdist()->phys_base; |
| 1848 | } else { |
| 1849 | /* |
| 1850 | * This ITS wants a linear CPU number. |
| 1851 | */ |
Marc Zyngier | 589ce5f | 2016-10-14 15:13:07 +0100 | [diff] [blame] | 1852 | target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER); |
Marc Zyngier | 263fcd3 | 2015-03-27 14:15:02 +0000 | [diff] [blame] | 1853 | target = GICR_TYPER_CPU_NUMBER(target) << 16; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
| 1856 | /* Perform collection mapping */ |
| 1857 | its->collections[cpu].target_address = target; |
| 1858 | its->collections[cpu].col_id = cpu; |
| 1859 | |
| 1860 | its_send_mapc(its, &its->collections[cpu], 1); |
| 1861 | its_send_invall(its, &its->collections[cpu]); |
| 1862 | } |
| 1863 | |
| 1864 | spin_unlock(&its_lock); |
| 1865 | } |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1866 | |
| 1867 | static struct its_device *its_find_device(struct its_node *its, u32 dev_id) |
| 1868 | { |
| 1869 | struct its_device *its_dev = NULL, *tmp; |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 1870 | unsigned long flags; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1871 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 1872 | raw_spin_lock_irqsave(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1873 | |
| 1874 | list_for_each_entry(tmp, &its->its_device_list, entry) { |
| 1875 | if (tmp->device_id == dev_id) { |
| 1876 | its_dev = tmp; |
| 1877 | break; |
| 1878 | } |
| 1879 | } |
| 1880 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 1881 | raw_spin_unlock_irqrestore(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1882 | |
| 1883 | return its_dev; |
| 1884 | } |
| 1885 | |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 1886 | static struct its_baser *its_get_baser(struct its_node *its, u32 type) |
| 1887 | { |
| 1888 | int i; |
| 1889 | |
| 1890 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
| 1891 | if (GITS_BASER_TYPE(its->tables[i].val) == type) |
| 1892 | return &its->tables[i]; |
| 1893 | } |
| 1894 | |
| 1895 | return NULL; |
| 1896 | } |
| 1897 | |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 1898 | static bool its_alloc_table_entry(struct its_baser *baser, u32 id) |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1899 | { |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1900 | struct page *page; |
| 1901 | u32 esz, idx; |
| 1902 | __le64 *table; |
| 1903 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1904 | /* Don't allow device id that exceeds single, flat table limit */ |
| 1905 | esz = GITS_BASER_ENTRY_SIZE(baser->val); |
| 1906 | if (!(baser->val & GITS_BASER_INDIRECT)) |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 1907 | return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz)); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1908 | |
| 1909 | /* Compute 1st level table index & check if that exceeds table limit */ |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 1910 | idx = id >> ilog2(baser->psz / esz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1911 | if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE)) |
| 1912 | return false; |
| 1913 | |
| 1914 | table = baser->base; |
| 1915 | |
| 1916 | /* Allocate memory for 2nd level table */ |
| 1917 | if (!table[idx]) { |
| 1918 | page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz)); |
| 1919 | if (!page) |
| 1920 | return false; |
| 1921 | |
| 1922 | /* Flush Lvl2 table to PoC if hw doesn't support coherency */ |
| 1923 | if (!(baser->val & GITS_BASER_SHAREABILITY_MASK)) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 1924 | gic_flush_dcache_to_poc(page_address(page), baser->psz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1925 | |
| 1926 | table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID); |
| 1927 | |
| 1928 | /* Flush Lvl1 entry to PoC if hw doesn't support coherency */ |
| 1929 | if (!(baser->val & GITS_BASER_SHAREABILITY_MASK)) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 1930 | gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1931 | |
| 1932 | /* Ensure updated table contents are visible to ITS hardware */ |
| 1933 | dsb(sy); |
| 1934 | } |
| 1935 | |
| 1936 | return true; |
| 1937 | } |
| 1938 | |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 1939 | static bool its_alloc_device_table(struct its_node *its, u32 dev_id) |
| 1940 | { |
| 1941 | struct its_baser *baser; |
| 1942 | |
| 1943 | baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE); |
| 1944 | |
| 1945 | /* Don't allow device id that exceeds ITS hardware limit */ |
| 1946 | if (!baser) |
| 1947 | return (ilog2(dev_id) < its->device_ids); |
| 1948 | |
| 1949 | return its_alloc_table_entry(baser, dev_id); |
| 1950 | } |
| 1951 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 1952 | static bool its_alloc_vpe_table(u32 vpe_id) |
| 1953 | { |
| 1954 | struct its_node *its; |
| 1955 | |
| 1956 | /* |
| 1957 | * Make sure the L2 tables are allocated on *all* v4 ITSs. We |
| 1958 | * could try and only do it on ITSs corresponding to devices |
| 1959 | * that have interrupts targeted at this VPE, but the |
| 1960 | * complexity becomes crazy (and you have tons of memory |
| 1961 | * anyway, right?). |
| 1962 | */ |
| 1963 | list_for_each_entry(its, &its_nodes, entry) { |
| 1964 | struct its_baser *baser; |
| 1965 | |
| 1966 | if (!its->is_v4) |
| 1967 | continue; |
| 1968 | |
| 1969 | baser = its_get_baser(its, GITS_BASER_TYPE_VCPU); |
| 1970 | if (!baser) |
| 1971 | return false; |
| 1972 | |
| 1973 | if (!its_alloc_table_entry(baser, vpe_id)) |
| 1974 | return false; |
| 1975 | } |
| 1976 | |
| 1977 | return true; |
| 1978 | } |
| 1979 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1980 | 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^] | 1981 | int nvecs, bool alloc_lpis) |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1982 | { |
| 1983 | struct its_device *dev; |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame^] | 1984 | unsigned long *lpi_map = NULL; |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 1985 | unsigned long flags; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1986 | u16 *col_map = NULL; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1987 | void *itt; |
| 1988 | int lpi_base; |
| 1989 | int nr_lpis; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 1990 | int nr_ites; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1991 | int sz; |
| 1992 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1993 | if (!its_alloc_device_table(its, dev_id)) |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 1994 | return NULL; |
| 1995 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1996 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 1997 | /* |
| 1998 | * At least one bit of EventID is being used, hence a minimum |
| 1999 | * of two entries. No, the architecture doesn't let you |
| 2000 | * express an ITT with a single entry. |
| 2001 | */ |
Will Deacon | 96555c4 | 2014-12-17 14:11:09 +0000 | [diff] [blame] | 2002 | nr_ites = max(2UL, roundup_pow_of_two(nvecs)); |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 2003 | sz = nr_ites * its->ite_size; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2004 | sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1; |
Yun Wu | 6c83412 | 2015-03-06 16:37:46 +0000 | [diff] [blame] | 2005 | itt = kzalloc(sz, GFP_KERNEL); |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame^] | 2006 | if (alloc_lpis) { |
| 2007 | lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis); |
| 2008 | if (lpi_map) |
| 2009 | col_map = kzalloc(sizeof(*col_map) * nr_lpis, |
| 2010 | GFP_KERNEL); |
| 2011 | } else { |
| 2012 | col_map = kzalloc(sizeof(*col_map) * nr_ites, GFP_KERNEL); |
| 2013 | nr_lpis = 0; |
| 2014 | lpi_base = 0; |
| 2015 | } |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2016 | |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame^] | 2017 | if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) { |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2018 | kfree(dev); |
| 2019 | kfree(itt); |
| 2020 | kfree(lpi_map); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2021 | kfree(col_map); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2022 | return NULL; |
| 2023 | } |
| 2024 | |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 2025 | gic_flush_dcache_to_poc(itt, sz); |
Marc Zyngier | 5a9a891 | 2015-09-13 12:14:32 +0100 | [diff] [blame] | 2026 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2027 | dev->its = its; |
| 2028 | dev->itt = itt; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 2029 | dev->nr_ites = nr_ites; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2030 | dev->event_map.lpi_map = lpi_map; |
| 2031 | dev->event_map.col_map = col_map; |
| 2032 | dev->event_map.lpi_base = lpi_base; |
| 2033 | dev->event_map.nr_lpis = nr_lpis; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 2034 | mutex_init(&dev->event_map.vlpi_lock); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2035 | dev->device_id = dev_id; |
| 2036 | INIT_LIST_HEAD(&dev->entry); |
| 2037 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2038 | raw_spin_lock_irqsave(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2039 | list_add(&dev->entry, &its->its_device_list); |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2040 | raw_spin_unlock_irqrestore(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2041 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2042 | /* Map device to its ITT */ |
| 2043 | its_send_mapd(dev, 1); |
| 2044 | |
| 2045 | return dev; |
| 2046 | } |
| 2047 | |
| 2048 | static void its_free_device(struct its_device *its_dev) |
| 2049 | { |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2050 | unsigned long flags; |
| 2051 | |
| 2052 | raw_spin_lock_irqsave(&its_dev->its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2053 | list_del(&its_dev->entry); |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2054 | raw_spin_unlock_irqrestore(&its_dev->its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2055 | kfree(its_dev->itt); |
| 2056 | kfree(its_dev); |
| 2057 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2058 | |
| 2059 | static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq) |
| 2060 | { |
| 2061 | int idx; |
| 2062 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2063 | idx = find_first_zero_bit(dev->event_map.lpi_map, |
| 2064 | dev->event_map.nr_lpis); |
| 2065 | if (idx == dev->event_map.nr_lpis) |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2066 | return -ENOSPC; |
| 2067 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2068 | *hwirq = dev->event_map.lpi_base + idx; |
| 2069 | set_bit(idx, dev->event_map.lpi_map); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2070 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2071 | return 0; |
| 2072 | } |
| 2073 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2074 | static int its_msi_prepare(struct irq_domain *domain, struct device *dev, |
| 2075 | int nvec, msi_alloc_info_t *info) |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 2076 | { |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2077 | struct its_node *its; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2078 | struct its_device *its_dev; |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2079 | struct msi_domain_info *msi_info; |
| 2080 | u32 dev_id; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2081 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2082 | /* |
| 2083 | * We ignore "dev" entierely, and rely on the dev_id that has |
| 2084 | * been passed via the scratchpad. This limits this domain's |
| 2085 | * usefulness to upper layers that definitely know that they |
| 2086 | * are built on top of the ITS. |
| 2087 | */ |
| 2088 | dev_id = info->scratchpad[0].ul; |
| 2089 | |
| 2090 | msi_info = msi_get_domain_info(domain); |
| 2091 | its = msi_info->data; |
| 2092 | |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 2093 | its_dev = its_find_device(its, dev_id); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 2094 | if (its_dev) { |
| 2095 | /* |
| 2096 | * We already have seen this ID, probably through |
| 2097 | * another alias (PCI bridge of some sort). No need to |
| 2098 | * create the device. |
| 2099 | */ |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 2100 | pr_debug("Reusing ITT for devID %x\n", dev_id); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 2101 | goto out; |
| 2102 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2103 | |
Marc Zyngier | 93f94ea | 2017-08-04 18:37:09 +0100 | [diff] [blame^] | 2104 | its_dev = its_create_device(its, dev_id, nvec, true); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2105 | if (!its_dev) |
| 2106 | return -ENOMEM; |
| 2107 | |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 2108 | pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec)); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 2109 | out: |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2110 | info->scratchpad[0].ptr = its_dev; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2111 | return 0; |
| 2112 | } |
| 2113 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2114 | static struct msi_domain_ops its_msi_domain_ops = { |
| 2115 | .msi_prepare = its_msi_prepare, |
| 2116 | }; |
| 2117 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2118 | static int its_irq_gic_domain_alloc(struct irq_domain *domain, |
| 2119 | unsigned int virq, |
| 2120 | irq_hw_number_t hwirq) |
| 2121 | { |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 2122 | struct irq_fwspec fwspec; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2123 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 2124 | if (irq_domain_get_of_node(domain->parent)) { |
| 2125 | fwspec.fwnode = domain->parent->fwnode; |
| 2126 | fwspec.param_count = 3; |
| 2127 | fwspec.param[0] = GIC_IRQ_TYPE_LPI; |
| 2128 | fwspec.param[1] = hwirq; |
| 2129 | fwspec.param[2] = IRQ_TYPE_EDGE_RISING; |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2130 | } else if (is_fwnode_irqchip(domain->parent->fwnode)) { |
| 2131 | fwspec.fwnode = domain->parent->fwnode; |
| 2132 | fwspec.param_count = 2; |
| 2133 | fwspec.param[0] = hwirq; |
| 2134 | fwspec.param[1] = IRQ_TYPE_EDGE_RISING; |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 2135 | } else { |
| 2136 | return -EINVAL; |
| 2137 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2138 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 2139 | return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2140 | } |
| 2141 | |
| 2142 | static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 2143 | unsigned int nr_irqs, void *args) |
| 2144 | { |
| 2145 | msi_alloc_info_t *info = args; |
| 2146 | struct its_device *its_dev = info->scratchpad[0].ptr; |
| 2147 | irq_hw_number_t hwirq; |
| 2148 | int err; |
| 2149 | int i; |
| 2150 | |
| 2151 | for (i = 0; i < nr_irqs; i++) { |
| 2152 | err = its_alloc_device_irq(its_dev, &hwirq); |
| 2153 | if (err) |
| 2154 | return err; |
| 2155 | |
| 2156 | err = its_irq_gic_domain_alloc(domain, virq + i, hwirq); |
| 2157 | if (err) |
| 2158 | return err; |
| 2159 | |
| 2160 | irq_domain_set_hwirq_and_chip(domain, virq + i, |
| 2161 | hwirq, &its_irq_chip, its_dev); |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 2162 | pr_debug("ID:%d pID:%d vID:%d\n", |
| 2163 | (int)(hwirq - its_dev->event_map.lpi_base), |
| 2164 | (int) hwirq, virq + i); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2165 | } |
| 2166 | |
| 2167 | return 0; |
| 2168 | } |
| 2169 | |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2170 | static void its_irq_domain_activate(struct irq_domain *domain, |
| 2171 | struct irq_data *d) |
| 2172 | { |
| 2173 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 2174 | u32 event = its_get_event_id(d); |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 2175 | const struct cpumask *cpu_mask = cpu_online_mask; |
| 2176 | |
| 2177 | /* get the cpu_mask of local node */ |
| 2178 | if (its_dev->its->numa_node >= 0) |
| 2179 | cpu_mask = cpumask_of_node(its_dev->its->numa_node); |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2180 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2181 | /* Bind the LPI to the first possible CPU */ |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 2182 | its_dev->event_map.col_map[event] = cpumask_first(cpu_mask); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2183 | |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2184 | /* Map the GIC IRQ and event to the device */ |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 2185 | its_send_mapti(its_dev, d->hwirq, event); |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | static void its_irq_domain_deactivate(struct irq_domain *domain, |
| 2189 | struct irq_data *d) |
| 2190 | { |
| 2191 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 2192 | u32 event = its_get_event_id(d); |
| 2193 | |
| 2194 | /* Stop the delivery of interrupts */ |
| 2195 | its_send_discard(its_dev, event); |
| 2196 | } |
| 2197 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2198 | static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq, |
| 2199 | unsigned int nr_irqs) |
| 2200 | { |
| 2201 | struct irq_data *d = irq_domain_get_irq_data(domain, virq); |
| 2202 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 2203 | int i; |
| 2204 | |
| 2205 | for (i = 0; i < nr_irqs; i++) { |
| 2206 | struct irq_data *data = irq_domain_get_irq_data(domain, |
| 2207 | virq + i); |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2208 | u32 event = its_get_event_id(data); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2209 | |
| 2210 | /* Mark interrupt index as unused */ |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2211 | clear_bit(event, its_dev->event_map.lpi_map); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2212 | |
| 2213 | /* Nuke the entry in the domain */ |
Marc Zyngier | 2da3994 | 2014-12-12 10:51:22 +0000 | [diff] [blame] | 2214 | irq_domain_reset_irq_data(data); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | /* If all interrupts have been freed, start mopping the floor */ |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2218 | if (bitmap_empty(its_dev->event_map.lpi_map, |
| 2219 | its_dev->event_map.nr_lpis)) { |
Marc Zyngier | cf2be8b | 2016-12-19 18:49:59 +0000 | [diff] [blame] | 2220 | its_lpi_free_chunks(its_dev->event_map.lpi_map, |
| 2221 | its_dev->event_map.lpi_base, |
| 2222 | its_dev->event_map.nr_lpis); |
| 2223 | kfree(its_dev->event_map.col_map); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2224 | |
| 2225 | /* Unmap device/itt */ |
| 2226 | its_send_mapd(its_dev, 0); |
| 2227 | its_free_device(its_dev); |
| 2228 | } |
| 2229 | |
| 2230 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 2231 | } |
| 2232 | |
| 2233 | static const struct irq_domain_ops its_domain_ops = { |
| 2234 | .alloc = its_irq_domain_alloc, |
| 2235 | .free = its_irq_domain_free, |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2236 | .activate = its_irq_domain_activate, |
| 2237 | .deactivate = its_irq_domain_deactivate, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2238 | }; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2239 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 2240 | static int its_vpe_set_affinity(struct irq_data *d, |
| 2241 | const struct cpumask *mask_val, |
| 2242 | bool force) |
| 2243 | { |
| 2244 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2245 | int cpu = cpumask_first(mask_val); |
| 2246 | |
| 2247 | /* |
| 2248 | * Changing affinity is mega expensive, so let's be as lazy as |
| 2249 | * we can and only do it if we really have to. |
| 2250 | */ |
| 2251 | if (vpe->col_idx != cpu) { |
| 2252 | vpe->col_idx = cpu; |
| 2253 | its_send_vmovp(vpe); |
| 2254 | } |
| 2255 | |
| 2256 | return IRQ_SET_MASK_OK_DONE; |
| 2257 | } |
| 2258 | |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2259 | static void its_vpe_schedule(struct its_vpe *vpe) |
| 2260 | { |
| 2261 | void * __iomem vlpi_base = gic_data_rdist_vlpi_base(); |
| 2262 | u64 val; |
| 2263 | |
| 2264 | /* Schedule the VPE */ |
| 2265 | val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) & |
| 2266 | GENMASK_ULL(51, 12); |
| 2267 | val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK; |
| 2268 | val |= GICR_VPROPBASER_RaWb; |
| 2269 | val |= GICR_VPROPBASER_InnerShareable; |
| 2270 | gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); |
| 2271 | |
| 2272 | val = virt_to_phys(page_address(vpe->vpt_page)) & |
| 2273 | GENMASK_ULL(51, 16); |
| 2274 | val |= GICR_VPENDBASER_RaWaWb; |
| 2275 | val |= GICR_VPENDBASER_NonShareable; |
| 2276 | /* |
| 2277 | * There is no good way of finding out if the pending table is |
| 2278 | * empty as we can race against the doorbell interrupt very |
| 2279 | * easily. So in the end, vpe->pending_last is only an |
| 2280 | * indication that the vcpu has something pending, not one |
| 2281 | * that the pending table is empty. A good implementation |
| 2282 | * would be able to read its coarse map pretty quickly anyway, |
| 2283 | * making this a tolerable issue. |
| 2284 | */ |
| 2285 | val |= GICR_VPENDBASER_PendingLast; |
| 2286 | val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0; |
| 2287 | val |= GICR_VPENDBASER_Valid; |
| 2288 | gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); |
| 2289 | } |
| 2290 | |
| 2291 | static void its_vpe_deschedule(struct its_vpe *vpe) |
| 2292 | { |
| 2293 | void * __iomem vlpi_base = gic_data_rdist_vlpi_base(); |
| 2294 | u32 count = 1000000; /* 1s! */ |
| 2295 | bool clean; |
| 2296 | u64 val; |
| 2297 | |
| 2298 | /* We're being scheduled out */ |
| 2299 | val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER); |
| 2300 | val &= ~GICR_VPENDBASER_Valid; |
| 2301 | gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); |
| 2302 | |
| 2303 | do { |
| 2304 | val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER); |
| 2305 | clean = !(val & GICR_VPENDBASER_Dirty); |
| 2306 | if (!clean) { |
| 2307 | count--; |
| 2308 | cpu_relax(); |
| 2309 | udelay(1); |
| 2310 | } |
| 2311 | } while (!clean && count); |
| 2312 | |
| 2313 | if (unlikely(!clean && !count)) { |
| 2314 | pr_err_ratelimited("ITS virtual pending table not cleaning\n"); |
| 2315 | vpe->idai = false; |
| 2316 | vpe->pending_last = true; |
| 2317 | } else { |
| 2318 | vpe->idai = !!(val & GICR_VPENDBASER_IDAI); |
| 2319 | vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast); |
| 2320 | } |
| 2321 | } |
| 2322 | |
| 2323 | static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) |
| 2324 | { |
| 2325 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2326 | struct its_cmd_info *info = vcpu_info; |
| 2327 | |
| 2328 | switch (info->cmd_type) { |
| 2329 | case SCHEDULE_VPE: |
| 2330 | its_vpe_schedule(vpe); |
| 2331 | return 0; |
| 2332 | |
| 2333 | case DESCHEDULE_VPE: |
| 2334 | its_vpe_deschedule(vpe); |
| 2335 | return 0; |
| 2336 | |
Marc Zyngier | 5e2f764 | 2016-12-20 15:10:50 +0000 | [diff] [blame] | 2337 | case INVALL_VPE: |
| 2338 | its_send_vinvall(vpe); |
| 2339 | return 0; |
| 2340 | |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2341 | default: |
| 2342 | return -EINVAL; |
| 2343 | } |
| 2344 | } |
| 2345 | |
Marc Zyngier | f6a91da | 2016-12-20 15:20:38 +0000 | [diff] [blame] | 2346 | static void its_vpe_send_inv(struct irq_data *d) |
| 2347 | { |
| 2348 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2349 | void __iomem *rdbase; |
| 2350 | |
| 2351 | rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base; |
| 2352 | gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR); |
| 2353 | while (gic_read_lpir(rdbase + GICR_SYNCR) & 1) |
| 2354 | cpu_relax(); |
| 2355 | } |
| 2356 | |
| 2357 | static void its_vpe_mask_irq(struct irq_data *d) |
| 2358 | { |
| 2359 | /* |
| 2360 | * We need to unmask the LPI, which is described by the parent |
| 2361 | * irq_data. Instead of calling into the parent (which won't |
| 2362 | * exactly do the right thing, let's simply use the |
| 2363 | * parent_data pointer. Yes, I'm naughty. |
| 2364 | */ |
| 2365 | lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0); |
| 2366 | its_vpe_send_inv(d); |
| 2367 | } |
| 2368 | |
| 2369 | static void its_vpe_unmask_irq(struct irq_data *d) |
| 2370 | { |
| 2371 | /* Same hack as above... */ |
| 2372 | lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED); |
| 2373 | its_vpe_send_inv(d); |
| 2374 | } |
| 2375 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2376 | static struct irq_chip its_vpe_irq_chip = { |
| 2377 | .name = "GICv4-vpe", |
Marc Zyngier | f6a91da | 2016-12-20 15:20:38 +0000 | [diff] [blame] | 2378 | .irq_mask = its_vpe_mask_irq, |
| 2379 | .irq_unmask = its_vpe_unmask_irq, |
| 2380 | .irq_eoi = irq_chip_eoi_parent, |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame] | 2381 | .irq_set_affinity = its_vpe_set_affinity, |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2382 | .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity, |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2383 | }; |
| 2384 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 2385 | static int its_vpe_id_alloc(void) |
| 2386 | { |
| 2387 | return ida_simple_get(&its_vpeid_ida, 0, 1 << 16, GFP_KERNEL); |
| 2388 | } |
| 2389 | |
| 2390 | static void its_vpe_id_free(u16 id) |
| 2391 | { |
| 2392 | ida_simple_remove(&its_vpeid_ida, id); |
| 2393 | } |
| 2394 | |
| 2395 | static int its_vpe_init(struct its_vpe *vpe) |
| 2396 | { |
| 2397 | struct page *vpt_page; |
| 2398 | int vpe_id; |
| 2399 | |
| 2400 | /* Allocate vpe_id */ |
| 2401 | vpe_id = its_vpe_id_alloc(); |
| 2402 | if (vpe_id < 0) |
| 2403 | return vpe_id; |
| 2404 | |
| 2405 | /* Allocate VPT */ |
| 2406 | vpt_page = its_allocate_pending_table(GFP_KERNEL); |
| 2407 | if (!vpt_page) { |
| 2408 | its_vpe_id_free(vpe_id); |
| 2409 | return -ENOMEM; |
| 2410 | } |
| 2411 | |
| 2412 | if (!its_alloc_vpe_table(vpe_id)) { |
| 2413 | its_vpe_id_free(vpe_id); |
| 2414 | its_free_pending_table(vpe->vpt_page); |
| 2415 | return -ENOMEM; |
| 2416 | } |
| 2417 | |
| 2418 | vpe->vpe_id = vpe_id; |
| 2419 | vpe->vpt_page = vpt_page; |
| 2420 | |
| 2421 | return 0; |
| 2422 | } |
| 2423 | |
| 2424 | static void its_vpe_teardown(struct its_vpe *vpe) |
| 2425 | { |
| 2426 | its_vpe_id_free(vpe->vpe_id); |
| 2427 | its_free_pending_table(vpe->vpt_page); |
| 2428 | } |
| 2429 | |
| 2430 | static void its_vpe_irq_domain_free(struct irq_domain *domain, |
| 2431 | unsigned int virq, |
| 2432 | unsigned int nr_irqs) |
| 2433 | { |
| 2434 | struct its_vm *vm = domain->host_data; |
| 2435 | int i; |
| 2436 | |
| 2437 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 2438 | |
| 2439 | for (i = 0; i < nr_irqs; i++) { |
| 2440 | struct irq_data *data = irq_domain_get_irq_data(domain, |
| 2441 | virq + i); |
| 2442 | struct its_vpe *vpe = irq_data_get_irq_chip_data(data); |
| 2443 | |
| 2444 | BUG_ON(vm != vpe->its_vm); |
| 2445 | |
| 2446 | clear_bit(data->hwirq, vm->db_bitmap); |
| 2447 | its_vpe_teardown(vpe); |
| 2448 | irq_domain_reset_irq_data(data); |
| 2449 | } |
| 2450 | |
| 2451 | if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) { |
| 2452 | its_lpi_free_chunks(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis); |
| 2453 | its_free_prop_table(vm->vprop_page); |
| 2454 | } |
| 2455 | } |
| 2456 | |
| 2457 | static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 2458 | unsigned int nr_irqs, void *args) |
| 2459 | { |
| 2460 | struct its_vm *vm = args; |
| 2461 | unsigned long *bitmap; |
| 2462 | struct page *vprop_page; |
| 2463 | int base, nr_ids, i, err = 0; |
| 2464 | |
| 2465 | BUG_ON(!vm); |
| 2466 | |
| 2467 | bitmap = its_lpi_alloc_chunks(nr_irqs, &base, &nr_ids); |
| 2468 | if (!bitmap) |
| 2469 | return -ENOMEM; |
| 2470 | |
| 2471 | if (nr_ids < nr_irqs) { |
| 2472 | its_lpi_free_chunks(bitmap, base, nr_ids); |
| 2473 | return -ENOMEM; |
| 2474 | } |
| 2475 | |
| 2476 | vprop_page = its_allocate_prop_table(GFP_KERNEL); |
| 2477 | if (!vprop_page) { |
| 2478 | its_lpi_free_chunks(bitmap, base, nr_ids); |
| 2479 | return -ENOMEM; |
| 2480 | } |
| 2481 | |
| 2482 | vm->db_bitmap = bitmap; |
| 2483 | vm->db_lpi_base = base; |
| 2484 | vm->nr_db_lpis = nr_ids; |
| 2485 | vm->vprop_page = vprop_page; |
| 2486 | |
| 2487 | for (i = 0; i < nr_irqs; i++) { |
| 2488 | vm->vpes[i]->vpe_db_lpi = base + i; |
| 2489 | err = its_vpe_init(vm->vpes[i]); |
| 2490 | if (err) |
| 2491 | break; |
| 2492 | err = its_irq_gic_domain_alloc(domain, virq + i, |
| 2493 | vm->vpes[i]->vpe_db_lpi); |
| 2494 | if (err) |
| 2495 | break; |
| 2496 | irq_domain_set_hwirq_and_chip(domain, virq + i, i, |
| 2497 | &its_vpe_irq_chip, vm->vpes[i]); |
| 2498 | set_bit(i, bitmap); |
| 2499 | } |
| 2500 | |
| 2501 | if (err) { |
| 2502 | if (i > 0) |
| 2503 | its_vpe_irq_domain_free(domain, virq, i - 1); |
| 2504 | |
| 2505 | its_lpi_free_chunks(bitmap, base, nr_ids); |
| 2506 | its_free_prop_table(vprop_page); |
| 2507 | } |
| 2508 | |
| 2509 | return err; |
| 2510 | } |
| 2511 | |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 2512 | static void its_vpe_irq_domain_activate(struct irq_domain *domain, |
| 2513 | struct irq_data *d) |
| 2514 | { |
| 2515 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2516 | |
| 2517 | /* Map the VPE to the first possible CPU */ |
| 2518 | vpe->col_idx = cpumask_first(cpu_online_mask); |
| 2519 | its_send_vmapp(vpe, true); |
| 2520 | its_send_vinvall(vpe); |
| 2521 | } |
| 2522 | |
| 2523 | static void its_vpe_irq_domain_deactivate(struct irq_domain *domain, |
| 2524 | struct irq_data *d) |
| 2525 | { |
| 2526 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2527 | |
| 2528 | its_send_vmapp(vpe, false); |
| 2529 | } |
| 2530 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2531 | static const struct irq_domain_ops its_vpe_domain_ops = { |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 2532 | .alloc = its_vpe_irq_domain_alloc, |
| 2533 | .free = its_vpe_irq_domain_free, |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 2534 | .activate = its_vpe_irq_domain_activate, |
| 2535 | .deactivate = its_vpe_irq_domain_deactivate, |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2536 | }; |
| 2537 | |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 2538 | static int its_force_quiescent(void __iomem *base) |
| 2539 | { |
| 2540 | u32 count = 1000000; /* 1s */ |
| 2541 | u32 val; |
| 2542 | |
| 2543 | val = readl_relaxed(base + GITS_CTLR); |
David Daney | 7611da8 | 2016-08-18 15:41:58 -0700 | [diff] [blame] | 2544 | /* |
| 2545 | * GIC architecture specification requires the ITS to be both |
| 2546 | * disabled and quiescent for writes to GITS_BASER<n> or |
| 2547 | * GITS_CBASER to not have UNPREDICTABLE results. |
| 2548 | */ |
| 2549 | if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE)) |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 2550 | return 0; |
| 2551 | |
| 2552 | /* Disable the generation of all interrupts to this ITS */ |
| 2553 | val &= ~GITS_CTLR_ENABLE; |
| 2554 | writel_relaxed(val, base + GITS_CTLR); |
| 2555 | |
| 2556 | /* Poll GITS_CTLR and wait until ITS becomes quiescent */ |
| 2557 | while (1) { |
| 2558 | val = readl_relaxed(base + GITS_CTLR); |
| 2559 | if (val & GITS_CTLR_QUIESCENT) |
| 2560 | return 0; |
| 2561 | |
| 2562 | count--; |
| 2563 | if (!count) |
| 2564 | return -EBUSY; |
| 2565 | |
| 2566 | cpu_relax(); |
| 2567 | udelay(1); |
| 2568 | } |
| 2569 | } |
| 2570 | |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 2571 | static void __maybe_unused its_enable_quirk_cavium_22375(void *data) |
| 2572 | { |
| 2573 | struct its_node *its = data; |
| 2574 | |
| 2575 | its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375; |
| 2576 | } |
| 2577 | |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 2578 | static void __maybe_unused its_enable_quirk_cavium_23144(void *data) |
| 2579 | { |
| 2580 | struct its_node *its = data; |
| 2581 | |
| 2582 | its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144; |
| 2583 | } |
| 2584 | |
Shanker Donthineni | 90922a2 | 2017-03-07 08:20:38 -0600 | [diff] [blame] | 2585 | static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data) |
| 2586 | { |
| 2587 | struct its_node *its = data; |
| 2588 | |
| 2589 | /* On QDF2400, the size of the ITE is 16Bytes */ |
| 2590 | its->ite_size = 16; |
| 2591 | } |
| 2592 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 2593 | static const struct gic_quirk its_quirks[] = { |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 2594 | #ifdef CONFIG_CAVIUM_ERRATUM_22375 |
| 2595 | { |
| 2596 | .desc = "ITS: Cavium errata 22375, 24313", |
| 2597 | .iidr = 0xa100034c, /* ThunderX pass 1.x */ |
| 2598 | .mask = 0xffff0fff, |
| 2599 | .init = its_enable_quirk_cavium_22375, |
| 2600 | }, |
| 2601 | #endif |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 2602 | #ifdef CONFIG_CAVIUM_ERRATUM_23144 |
| 2603 | { |
| 2604 | .desc = "ITS: Cavium erratum 23144", |
| 2605 | .iidr = 0xa100034c, /* ThunderX pass 1.x */ |
| 2606 | .mask = 0xffff0fff, |
| 2607 | .init = its_enable_quirk_cavium_23144, |
| 2608 | }, |
| 2609 | #endif |
Shanker Donthineni | 90922a2 | 2017-03-07 08:20:38 -0600 | [diff] [blame] | 2610 | #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065 |
| 2611 | { |
| 2612 | .desc = "ITS: QDF2400 erratum 0065", |
| 2613 | .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */ |
| 2614 | .mask = 0xffffffff, |
| 2615 | .init = its_enable_quirk_qdf2400_e0065, |
| 2616 | }, |
| 2617 | #endif |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 2618 | { |
| 2619 | } |
| 2620 | }; |
| 2621 | |
| 2622 | static void its_enable_quirks(struct its_node *its) |
| 2623 | { |
| 2624 | u32 iidr = readl_relaxed(its->base + GITS_IIDR); |
| 2625 | |
| 2626 | gic_enable_quirks(iidr, its_quirks, its); |
| 2627 | } |
| 2628 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2629 | 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] | 2630 | { |
| 2631 | struct irq_domain *inner_domain; |
| 2632 | struct msi_domain_info *info; |
| 2633 | |
| 2634 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 2635 | if (!info) |
| 2636 | return -ENOMEM; |
| 2637 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2638 | inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2639 | if (!inner_domain) { |
| 2640 | kfree(info); |
| 2641 | return -ENOMEM; |
| 2642 | } |
| 2643 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2644 | inner_domain->parent = its_parent; |
Marc Zyngier | 96f0d93 | 2017-06-22 11:42:50 +0100 | [diff] [blame] | 2645 | irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS); |
Eric Auger | 5976852 | 2017-01-19 20:58:00 +0000 | [diff] [blame] | 2646 | inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP; |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2647 | info->ops = &its_msi_domain_ops; |
| 2648 | info->data = its; |
| 2649 | inner_domain->host_data = info; |
| 2650 | |
| 2651 | return 0; |
| 2652 | } |
| 2653 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2654 | static int its_init_vpe_domain(void) |
| 2655 | { |
| 2656 | return 0; |
| 2657 | } |
| 2658 | |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2659 | static int __init its_compute_its_list_map(struct resource *res, |
| 2660 | void __iomem *its_base) |
| 2661 | { |
| 2662 | int its_number; |
| 2663 | u32 ctlr; |
| 2664 | |
| 2665 | /* |
| 2666 | * This is assumed to be done early enough that we're |
| 2667 | * guaranteed to be single-threaded, hence no |
| 2668 | * locking. Should this change, we should address |
| 2669 | * this. |
| 2670 | */ |
| 2671 | its_number = find_first_zero_bit(&its_list_map, ITS_LIST_MAX); |
| 2672 | if (its_number >= ITS_LIST_MAX) { |
| 2673 | pr_err("ITS@%pa: No ITSList entry available!\n", |
| 2674 | &res->start); |
| 2675 | return -EINVAL; |
| 2676 | } |
| 2677 | |
| 2678 | ctlr = readl_relaxed(its_base + GITS_CTLR); |
| 2679 | ctlr &= ~GITS_CTLR_ITS_NUMBER; |
| 2680 | ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT; |
| 2681 | writel_relaxed(ctlr, its_base + GITS_CTLR); |
| 2682 | ctlr = readl_relaxed(its_base + GITS_CTLR); |
| 2683 | if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) { |
| 2684 | its_number = ctlr & GITS_CTLR_ITS_NUMBER; |
| 2685 | its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT; |
| 2686 | } |
| 2687 | |
| 2688 | if (test_and_set_bit(its_number, &its_list_map)) { |
| 2689 | pr_err("ITS@%pa: Duplicate ITSList entry %d\n", |
| 2690 | &res->start, its_number); |
| 2691 | return -EINVAL; |
| 2692 | } |
| 2693 | |
| 2694 | return its_number; |
| 2695 | } |
| 2696 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2697 | static int __init its_probe_one(struct resource *res, |
| 2698 | struct fwnode_handle *handle, int numa_node) |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2699 | { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2700 | struct its_node *its; |
| 2701 | void __iomem *its_base; |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2702 | u32 val, ctlr; |
| 2703 | u64 baser, tmp, typer; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2704 | int err; |
| 2705 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2706 | its_base = ioremap(res->start, resource_size(res)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2707 | if (!its_base) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2708 | pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2709 | return -ENOMEM; |
| 2710 | } |
| 2711 | |
| 2712 | val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK; |
| 2713 | if (val != 0x30 && val != 0x40) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2714 | pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2715 | err = -ENODEV; |
| 2716 | goto out_unmap; |
| 2717 | } |
| 2718 | |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 2719 | err = its_force_quiescent(its_base); |
| 2720 | if (err) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2721 | pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start); |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 2722 | goto out_unmap; |
| 2723 | } |
| 2724 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2725 | pr_info("ITS %pR\n", res); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2726 | |
| 2727 | its = kzalloc(sizeof(*its), GFP_KERNEL); |
| 2728 | if (!its) { |
| 2729 | err = -ENOMEM; |
| 2730 | goto out_unmap; |
| 2731 | } |
| 2732 | |
| 2733 | raw_spin_lock_init(&its->lock); |
| 2734 | INIT_LIST_HEAD(&its->entry); |
| 2735 | INIT_LIST_HEAD(&its->its_device_list); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2736 | typer = gic_read_typer(its_base + GITS_TYPER); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2737 | its->base = its_base; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2738 | its->phys_base = res->start; |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2739 | its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer); |
| 2740 | its->is_v4 = !!(typer & GITS_TYPER_VLPIS); |
| 2741 | if (its->is_v4) { |
| 2742 | if (!(typer & GITS_TYPER_VMOVP)) { |
| 2743 | err = its_compute_its_list_map(res, its_base); |
| 2744 | if (err < 0) |
| 2745 | goto out_free_its; |
| 2746 | |
| 2747 | pr_info("ITS@%pa: Using ITS number %d\n", |
| 2748 | &res->start, err); |
| 2749 | } else { |
| 2750 | pr_info("ITS@%pa: Single VMOVP capable\n", &res->start); |
| 2751 | } |
| 2752 | } |
| 2753 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2754 | its->numa_node = numa_node; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2755 | |
Robert Richter | 5bc13c2 | 2017-02-01 18:38:25 +0100 | [diff] [blame] | 2756 | its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 2757 | get_order(ITS_CMD_QUEUE_SZ)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2758 | if (!its->cmd_base) { |
| 2759 | err = -ENOMEM; |
| 2760 | goto out_free_its; |
| 2761 | } |
| 2762 | its->cmd_write = its->cmd_base; |
| 2763 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 2764 | its_enable_quirks(its); |
| 2765 | |
Shanker Donthineni | 0e0b0f6 | 2016-06-06 18:17:31 -0500 | [diff] [blame] | 2766 | err = its_alloc_tables(its); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2767 | if (err) |
| 2768 | goto out_free_cmd; |
| 2769 | |
| 2770 | err = its_alloc_collections(its); |
| 2771 | if (err) |
| 2772 | goto out_free_tables; |
| 2773 | |
| 2774 | baser = (virt_to_phys(its->cmd_base) | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 2775 | GITS_CBASER_RaWaWb | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2776 | GITS_CBASER_InnerShareable | |
| 2777 | (ITS_CMD_QUEUE_SZ / SZ_4K - 1) | |
| 2778 | GITS_CBASER_VALID); |
| 2779 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2780 | gits_write_cbaser(baser, its->base + GITS_CBASER); |
| 2781 | tmp = gits_read_cbaser(its->base + GITS_CBASER); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2782 | |
Marc Zyngier | 4ad3e36 | 2015-03-27 14:15:04 +0000 | [diff] [blame] | 2783 | if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) { |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2784 | if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) { |
| 2785 | /* |
| 2786 | * The HW reports non-shareable, we must |
| 2787 | * remove the cacheability attributes as |
| 2788 | * well. |
| 2789 | */ |
| 2790 | baser &= ~(GITS_CBASER_SHAREABILITY_MASK | |
| 2791 | GITS_CBASER_CACHEABILITY_MASK); |
| 2792 | baser |= GITS_CBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2793 | gits_write_cbaser(baser, its->base + GITS_CBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2794 | } |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2795 | pr_info("ITS: using cache flushing for cmd queue\n"); |
| 2796 | its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING; |
| 2797 | } |
| 2798 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2799 | gits_write_cwriter(0, its->base + GITS_CWRITER); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2800 | ctlr = readl_relaxed(its->base + GITS_CTLR); |
| 2801 | writel_relaxed(ctlr | GITS_CTLR_ENABLE, its->base + GITS_CTLR); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2802 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2803 | err = its_init_domain(handle, its); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2804 | if (err) |
| 2805 | goto out_free_tables; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2806 | |
| 2807 | spin_lock(&its_lock); |
| 2808 | list_add(&its->entry, &its_nodes); |
| 2809 | spin_unlock(&its_lock); |
| 2810 | |
| 2811 | return 0; |
| 2812 | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2813 | out_free_tables: |
| 2814 | its_free_tables(its); |
| 2815 | out_free_cmd: |
Robert Richter | 5bc13c2 | 2017-02-01 18:38:25 +0100 | [diff] [blame] | 2816 | 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] | 2817 | out_free_its: |
| 2818 | kfree(its); |
| 2819 | out_unmap: |
| 2820 | iounmap(its_base); |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2821 | pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2822 | return err; |
| 2823 | } |
| 2824 | |
| 2825 | static bool gic_rdists_supports_plpis(void) |
| 2826 | { |
Marc Zyngier | 589ce5f | 2016-10-14 15:13:07 +0100 | [diff] [blame] | 2827 | 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] | 2828 | } |
| 2829 | |
| 2830 | int its_cpu_init(void) |
| 2831 | { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2832 | if (!list_empty(&its_nodes)) { |
Vladimir Murzin | 16acae7 | 2015-03-06 16:37:40 +0000 | [diff] [blame] | 2833 | if (!gic_rdists_supports_plpis()) { |
| 2834 | pr_info("CPU%d: LPIs not supported\n", smp_processor_id()); |
| 2835 | return -ENXIO; |
| 2836 | } |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2837 | its_cpu_init_lpis(); |
| 2838 | its_cpu_init_collection(); |
| 2839 | } |
| 2840 | |
| 2841 | return 0; |
| 2842 | } |
| 2843 | |
Arvind Yadav | 935bba7 | 2017-06-22 16:05:30 +0530 | [diff] [blame] | 2844 | static const struct of_device_id its_device_id[] = { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2845 | { .compatible = "arm,gic-v3-its", }, |
| 2846 | {}, |
| 2847 | }; |
| 2848 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2849 | static int __init its_of_probe(struct device_node *node) |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2850 | { |
| 2851 | struct device_node *np; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2852 | struct resource res; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2853 | |
| 2854 | for (np = of_find_matching_node(node, its_device_id); np; |
| 2855 | np = of_find_matching_node(np, its_device_id)) { |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2856 | if (!of_property_read_bool(np, "msi-controller")) { |
Rob Herring | e81f54c | 2017-07-18 16:43:10 -0500 | [diff] [blame] | 2857 | pr_warn("%pOF: no msi-controller property, ITS ignored\n", |
| 2858 | np); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2859 | continue; |
| 2860 | } |
| 2861 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2862 | if (of_address_to_resource(np, 0, &res)) { |
Rob Herring | e81f54c | 2017-07-18 16:43:10 -0500 | [diff] [blame] | 2863 | pr_warn("%pOF: no regs?\n", np); |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2864 | continue; |
| 2865 | } |
| 2866 | |
| 2867 | its_probe_one(&res, &np->fwnode, of_node_to_nid(np)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2868 | } |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2869 | return 0; |
| 2870 | } |
| 2871 | |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2872 | #ifdef CONFIG_ACPI |
| 2873 | |
| 2874 | #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K) |
| 2875 | |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 2876 | #if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531) |
| 2877 | struct its_srat_map { |
| 2878 | /* numa node id */ |
| 2879 | u32 numa_node; |
| 2880 | /* GIC ITS ID */ |
| 2881 | u32 its_id; |
| 2882 | }; |
| 2883 | |
| 2884 | static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata; |
| 2885 | static int its_in_srat __initdata; |
| 2886 | |
| 2887 | static int __init acpi_get_its_numa_node(u32 its_id) |
| 2888 | { |
| 2889 | int i; |
| 2890 | |
| 2891 | for (i = 0; i < its_in_srat; i++) { |
| 2892 | if (its_id == its_srat_maps[i].its_id) |
| 2893 | return its_srat_maps[i].numa_node; |
| 2894 | } |
| 2895 | return NUMA_NO_NODE; |
| 2896 | } |
| 2897 | |
| 2898 | static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header, |
| 2899 | const unsigned long end) |
| 2900 | { |
| 2901 | int node; |
| 2902 | struct acpi_srat_gic_its_affinity *its_affinity; |
| 2903 | |
| 2904 | its_affinity = (struct acpi_srat_gic_its_affinity *)header; |
| 2905 | if (!its_affinity) |
| 2906 | return -EINVAL; |
| 2907 | |
| 2908 | if (its_affinity->header.length < sizeof(*its_affinity)) { |
| 2909 | pr_err("SRAT: Invalid header length %d in ITS affinity\n", |
| 2910 | its_affinity->header.length); |
| 2911 | return -EINVAL; |
| 2912 | } |
| 2913 | |
| 2914 | if (its_in_srat >= MAX_NUMNODES) { |
| 2915 | pr_err("SRAT: ITS affinity exceeding max count[%d]\n", |
| 2916 | MAX_NUMNODES); |
| 2917 | return -EINVAL; |
| 2918 | } |
| 2919 | |
| 2920 | node = acpi_map_pxm_to_node(its_affinity->proximity_domain); |
| 2921 | |
| 2922 | if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) { |
| 2923 | pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node); |
| 2924 | return 0; |
| 2925 | } |
| 2926 | |
| 2927 | its_srat_maps[its_in_srat].numa_node = node; |
| 2928 | its_srat_maps[its_in_srat].its_id = its_affinity->its_id; |
| 2929 | its_in_srat++; |
| 2930 | pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n", |
| 2931 | its_affinity->proximity_domain, its_affinity->its_id, node); |
| 2932 | |
| 2933 | return 0; |
| 2934 | } |
| 2935 | |
| 2936 | static void __init acpi_table_parse_srat_its(void) |
| 2937 | { |
| 2938 | acpi_table_parse_entries(ACPI_SIG_SRAT, |
| 2939 | sizeof(struct acpi_table_srat), |
| 2940 | ACPI_SRAT_TYPE_GIC_ITS_AFFINITY, |
| 2941 | gic_acpi_parse_srat_its, 0); |
| 2942 | } |
| 2943 | #else |
| 2944 | static void __init acpi_table_parse_srat_its(void) { } |
| 2945 | static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; } |
| 2946 | #endif |
| 2947 | |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2948 | static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header, |
| 2949 | const unsigned long end) |
| 2950 | { |
| 2951 | struct acpi_madt_generic_translator *its_entry; |
| 2952 | struct fwnode_handle *dom_handle; |
| 2953 | struct resource res; |
| 2954 | int err; |
| 2955 | |
| 2956 | its_entry = (struct acpi_madt_generic_translator *)header; |
| 2957 | memset(&res, 0, sizeof(res)); |
| 2958 | res.start = its_entry->base_address; |
| 2959 | res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1; |
| 2960 | res.flags = IORESOURCE_MEM; |
| 2961 | |
| 2962 | dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address); |
| 2963 | if (!dom_handle) { |
| 2964 | pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n", |
| 2965 | &res.start); |
| 2966 | return -ENOMEM; |
| 2967 | } |
| 2968 | |
| 2969 | err = iort_register_domain_token(its_entry->translation_id, dom_handle); |
| 2970 | if (err) { |
| 2971 | pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n", |
| 2972 | &res.start, its_entry->translation_id); |
| 2973 | goto dom_err; |
| 2974 | } |
| 2975 | |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 2976 | err = its_probe_one(&res, dom_handle, |
| 2977 | acpi_get_its_numa_node(its_entry->translation_id)); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2978 | if (!err) |
| 2979 | return 0; |
| 2980 | |
| 2981 | iort_deregister_domain_token(its_entry->translation_id); |
| 2982 | dom_err: |
| 2983 | irq_domain_free_fwnode(dom_handle); |
| 2984 | return err; |
| 2985 | } |
| 2986 | |
| 2987 | static void __init its_acpi_probe(void) |
| 2988 | { |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 2989 | acpi_table_parse_srat_its(); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2990 | acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR, |
| 2991 | gic_acpi_parse_madt_its, 0); |
| 2992 | } |
| 2993 | #else |
| 2994 | static void __init its_acpi_probe(void) { } |
| 2995 | #endif |
| 2996 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2997 | int __init its_init(struct fwnode_handle *handle, struct rdists *rdists, |
| 2998 | struct irq_domain *parent_domain) |
| 2999 | { |
| 3000 | struct device_node *of_node; |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 3001 | struct its_node *its; |
| 3002 | bool has_v4 = false; |
| 3003 | int err; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 3004 | |
| 3005 | its_parent = parent_domain; |
| 3006 | of_node = to_of_node(handle); |
| 3007 | if (of_node) |
| 3008 | its_of_probe(of_node); |
| 3009 | else |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 3010 | its_acpi_probe(); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3011 | |
| 3012 | if (list_empty(&its_nodes)) { |
| 3013 | pr_warn("ITS: No ITS available, not enabling LPIs\n"); |
| 3014 | return -ENXIO; |
| 3015 | } |
| 3016 | |
| 3017 | gic_rdists = rdists; |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 3018 | err = its_alloc_lpi_tables(); |
| 3019 | if (err) |
| 3020 | return err; |
| 3021 | |
| 3022 | list_for_each_entry(its, &its_nodes, entry) |
| 3023 | has_v4 |= its->is_v4; |
| 3024 | |
| 3025 | if (has_v4 & rdists->has_vlpis) { |
| 3026 | if (its_init_vpe_domain()) { |
| 3027 | rdists->has_vlpis = false; |
| 3028 | pr_err("ITS: Disabling GICv4 support\n"); |
| 3029 | } |
| 3030 | } |
| 3031 | |
| 3032 | return 0; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 3033 | } |