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, |
| 1981 | int nvecs) |
| 1982 | { |
| 1983 | struct its_device *dev; |
| 1984 | unsigned long *lpi_map; |
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 | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2006 | lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2007 | if (lpi_map) |
| 2008 | col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2009 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2010 | if (!dev || !itt || !lpi_map || !col_map) { |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2011 | kfree(dev); |
| 2012 | kfree(itt); |
| 2013 | kfree(lpi_map); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2014 | kfree(col_map); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2015 | return NULL; |
| 2016 | } |
| 2017 | |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 2018 | gic_flush_dcache_to_poc(itt, sz); |
Marc Zyngier | 5a9a891 | 2015-09-13 12:14:32 +0100 | [diff] [blame] | 2019 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2020 | dev->its = its; |
| 2021 | dev->itt = itt; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 2022 | dev->nr_ites = nr_ites; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2023 | dev->event_map.lpi_map = lpi_map; |
| 2024 | dev->event_map.col_map = col_map; |
| 2025 | dev->event_map.lpi_base = lpi_base; |
| 2026 | dev->event_map.nr_lpis = nr_lpis; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame] | 2027 | mutex_init(&dev->event_map.vlpi_lock); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2028 | dev->device_id = dev_id; |
| 2029 | INIT_LIST_HEAD(&dev->entry); |
| 2030 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2031 | raw_spin_lock_irqsave(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2032 | list_add(&dev->entry, &its->its_device_list); |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2033 | raw_spin_unlock_irqrestore(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2034 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2035 | /* Map device to its ITT */ |
| 2036 | its_send_mapd(dev, 1); |
| 2037 | |
| 2038 | return dev; |
| 2039 | } |
| 2040 | |
| 2041 | static void its_free_device(struct its_device *its_dev) |
| 2042 | { |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2043 | unsigned long flags; |
| 2044 | |
| 2045 | raw_spin_lock_irqsave(&its_dev->its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2046 | list_del(&its_dev->entry); |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 2047 | raw_spin_unlock_irqrestore(&its_dev->its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 2048 | kfree(its_dev->itt); |
| 2049 | kfree(its_dev); |
| 2050 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2051 | |
| 2052 | static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq) |
| 2053 | { |
| 2054 | int idx; |
| 2055 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2056 | idx = find_first_zero_bit(dev->event_map.lpi_map, |
| 2057 | dev->event_map.nr_lpis); |
| 2058 | if (idx == dev->event_map.nr_lpis) |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2059 | return -ENOSPC; |
| 2060 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2061 | *hwirq = dev->event_map.lpi_base + idx; |
| 2062 | set_bit(idx, dev->event_map.lpi_map); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2063 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2064 | return 0; |
| 2065 | } |
| 2066 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2067 | static int its_msi_prepare(struct irq_domain *domain, struct device *dev, |
| 2068 | int nvec, msi_alloc_info_t *info) |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 2069 | { |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2070 | struct its_node *its; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2071 | struct its_device *its_dev; |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2072 | struct msi_domain_info *msi_info; |
| 2073 | u32 dev_id; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2074 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2075 | /* |
| 2076 | * We ignore "dev" entierely, and rely on the dev_id that has |
| 2077 | * been passed via the scratchpad. This limits this domain's |
| 2078 | * usefulness to upper layers that definitely know that they |
| 2079 | * are built on top of the ITS. |
| 2080 | */ |
| 2081 | dev_id = info->scratchpad[0].ul; |
| 2082 | |
| 2083 | msi_info = msi_get_domain_info(domain); |
| 2084 | its = msi_info->data; |
| 2085 | |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 2086 | its_dev = its_find_device(its, dev_id); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 2087 | if (its_dev) { |
| 2088 | /* |
| 2089 | * We already have seen this ID, probably through |
| 2090 | * another alias (PCI bridge of some sort). No need to |
| 2091 | * create the device. |
| 2092 | */ |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 2093 | pr_debug("Reusing ITT for devID %x\n", dev_id); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 2094 | goto out; |
| 2095 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2096 | |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 2097 | its_dev = its_create_device(its, dev_id, nvec); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2098 | if (!its_dev) |
| 2099 | return -ENOMEM; |
| 2100 | |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 2101 | pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec)); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 2102 | out: |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2103 | info->scratchpad[0].ptr = its_dev; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2104 | return 0; |
| 2105 | } |
| 2106 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 2107 | static struct msi_domain_ops its_msi_domain_ops = { |
| 2108 | .msi_prepare = its_msi_prepare, |
| 2109 | }; |
| 2110 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2111 | static int its_irq_gic_domain_alloc(struct irq_domain *domain, |
| 2112 | unsigned int virq, |
| 2113 | irq_hw_number_t hwirq) |
| 2114 | { |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 2115 | struct irq_fwspec fwspec; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2116 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 2117 | if (irq_domain_get_of_node(domain->parent)) { |
| 2118 | fwspec.fwnode = domain->parent->fwnode; |
| 2119 | fwspec.param_count = 3; |
| 2120 | fwspec.param[0] = GIC_IRQ_TYPE_LPI; |
| 2121 | fwspec.param[1] = hwirq; |
| 2122 | fwspec.param[2] = IRQ_TYPE_EDGE_RISING; |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2123 | } else if (is_fwnode_irqchip(domain->parent->fwnode)) { |
| 2124 | fwspec.fwnode = domain->parent->fwnode; |
| 2125 | fwspec.param_count = 2; |
| 2126 | fwspec.param[0] = hwirq; |
| 2127 | fwspec.param[1] = IRQ_TYPE_EDGE_RISING; |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 2128 | } else { |
| 2129 | return -EINVAL; |
| 2130 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2131 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 2132 | return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 2136 | unsigned int nr_irqs, void *args) |
| 2137 | { |
| 2138 | msi_alloc_info_t *info = args; |
| 2139 | struct its_device *its_dev = info->scratchpad[0].ptr; |
| 2140 | irq_hw_number_t hwirq; |
| 2141 | int err; |
| 2142 | int i; |
| 2143 | |
| 2144 | for (i = 0; i < nr_irqs; i++) { |
| 2145 | err = its_alloc_device_irq(its_dev, &hwirq); |
| 2146 | if (err) |
| 2147 | return err; |
| 2148 | |
| 2149 | err = its_irq_gic_domain_alloc(domain, virq + i, hwirq); |
| 2150 | if (err) |
| 2151 | return err; |
| 2152 | |
| 2153 | irq_domain_set_hwirq_and_chip(domain, virq + i, |
| 2154 | hwirq, &its_irq_chip, its_dev); |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 2155 | pr_debug("ID:%d pID:%d vID:%d\n", |
| 2156 | (int)(hwirq - its_dev->event_map.lpi_base), |
| 2157 | (int) hwirq, virq + i); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2163 | static void its_irq_domain_activate(struct irq_domain *domain, |
| 2164 | struct irq_data *d) |
| 2165 | { |
| 2166 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 2167 | u32 event = its_get_event_id(d); |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 2168 | const struct cpumask *cpu_mask = cpu_online_mask; |
| 2169 | |
| 2170 | /* get the cpu_mask of local node */ |
| 2171 | if (its_dev->its->numa_node >= 0) |
| 2172 | cpu_mask = cpumask_of_node(its_dev->its->numa_node); |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2173 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2174 | /* Bind the LPI to the first possible CPU */ |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 2175 | its_dev->event_map.col_map[event] = cpumask_first(cpu_mask); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2176 | |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2177 | /* Map the GIC IRQ and event to the device */ |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 2178 | its_send_mapti(its_dev, d->hwirq, event); |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2179 | } |
| 2180 | |
| 2181 | static void its_irq_domain_deactivate(struct irq_domain *domain, |
| 2182 | struct irq_data *d) |
| 2183 | { |
| 2184 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 2185 | u32 event = its_get_event_id(d); |
| 2186 | |
| 2187 | /* Stop the delivery of interrupts */ |
| 2188 | its_send_discard(its_dev, event); |
| 2189 | } |
| 2190 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2191 | static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq, |
| 2192 | unsigned int nr_irqs) |
| 2193 | { |
| 2194 | struct irq_data *d = irq_domain_get_irq_data(domain, virq); |
| 2195 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 2196 | int i; |
| 2197 | |
| 2198 | for (i = 0; i < nr_irqs; i++) { |
| 2199 | struct irq_data *data = irq_domain_get_irq_data(domain, |
| 2200 | virq + i); |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2201 | u32 event = its_get_event_id(data); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2202 | |
| 2203 | /* Mark interrupt index as unused */ |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2204 | clear_bit(event, its_dev->event_map.lpi_map); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2205 | |
| 2206 | /* Nuke the entry in the domain */ |
Marc Zyngier | 2da3994 | 2014-12-12 10:51:22 +0000 | [diff] [blame] | 2207 | irq_domain_reset_irq_data(data); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2208 | } |
| 2209 | |
| 2210 | /* If all interrupts have been freed, start mopping the floor */ |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 2211 | if (bitmap_empty(its_dev->event_map.lpi_map, |
| 2212 | its_dev->event_map.nr_lpis)) { |
Marc Zyngier | cf2be8b | 2016-12-19 18:49:59 +0000 | [diff] [blame] | 2213 | its_lpi_free_chunks(its_dev->event_map.lpi_map, |
| 2214 | its_dev->event_map.lpi_base, |
| 2215 | its_dev->event_map.nr_lpis); |
| 2216 | kfree(its_dev->event_map.col_map); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2217 | |
| 2218 | /* Unmap device/itt */ |
| 2219 | its_send_mapd(its_dev, 0); |
| 2220 | its_free_device(its_dev); |
| 2221 | } |
| 2222 | |
| 2223 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 2224 | } |
| 2225 | |
| 2226 | static const struct irq_domain_ops its_domain_ops = { |
| 2227 | .alloc = its_irq_domain_alloc, |
| 2228 | .free = its_irq_domain_free, |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 2229 | .activate = its_irq_domain_activate, |
| 2230 | .deactivate = its_irq_domain_deactivate, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 2231 | }; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2232 | |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame^] | 2233 | static int its_vpe_set_affinity(struct irq_data *d, |
| 2234 | const struct cpumask *mask_val, |
| 2235 | bool force) |
| 2236 | { |
| 2237 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2238 | int cpu = cpumask_first(mask_val); |
| 2239 | |
| 2240 | /* |
| 2241 | * Changing affinity is mega expensive, so let's be as lazy as |
| 2242 | * we can and only do it if we really have to. |
| 2243 | */ |
| 2244 | if (vpe->col_idx != cpu) { |
| 2245 | vpe->col_idx = cpu; |
| 2246 | its_send_vmovp(vpe); |
| 2247 | } |
| 2248 | |
| 2249 | return IRQ_SET_MASK_OK_DONE; |
| 2250 | } |
| 2251 | |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2252 | static void its_vpe_schedule(struct its_vpe *vpe) |
| 2253 | { |
| 2254 | void * __iomem vlpi_base = gic_data_rdist_vlpi_base(); |
| 2255 | u64 val; |
| 2256 | |
| 2257 | /* Schedule the VPE */ |
| 2258 | val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) & |
| 2259 | GENMASK_ULL(51, 12); |
| 2260 | val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK; |
| 2261 | val |= GICR_VPROPBASER_RaWb; |
| 2262 | val |= GICR_VPROPBASER_InnerShareable; |
| 2263 | gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER); |
| 2264 | |
| 2265 | val = virt_to_phys(page_address(vpe->vpt_page)) & |
| 2266 | GENMASK_ULL(51, 16); |
| 2267 | val |= GICR_VPENDBASER_RaWaWb; |
| 2268 | val |= GICR_VPENDBASER_NonShareable; |
| 2269 | /* |
| 2270 | * There is no good way of finding out if the pending table is |
| 2271 | * empty as we can race against the doorbell interrupt very |
| 2272 | * easily. So in the end, vpe->pending_last is only an |
| 2273 | * indication that the vcpu has something pending, not one |
| 2274 | * that the pending table is empty. A good implementation |
| 2275 | * would be able to read its coarse map pretty quickly anyway, |
| 2276 | * making this a tolerable issue. |
| 2277 | */ |
| 2278 | val |= GICR_VPENDBASER_PendingLast; |
| 2279 | val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0; |
| 2280 | val |= GICR_VPENDBASER_Valid; |
| 2281 | gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); |
| 2282 | } |
| 2283 | |
| 2284 | static void its_vpe_deschedule(struct its_vpe *vpe) |
| 2285 | { |
| 2286 | void * __iomem vlpi_base = gic_data_rdist_vlpi_base(); |
| 2287 | u32 count = 1000000; /* 1s! */ |
| 2288 | bool clean; |
| 2289 | u64 val; |
| 2290 | |
| 2291 | /* We're being scheduled out */ |
| 2292 | val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER); |
| 2293 | val &= ~GICR_VPENDBASER_Valid; |
| 2294 | gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER); |
| 2295 | |
| 2296 | do { |
| 2297 | val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER); |
| 2298 | clean = !(val & GICR_VPENDBASER_Dirty); |
| 2299 | if (!clean) { |
| 2300 | count--; |
| 2301 | cpu_relax(); |
| 2302 | udelay(1); |
| 2303 | } |
| 2304 | } while (!clean && count); |
| 2305 | |
| 2306 | if (unlikely(!clean && !count)) { |
| 2307 | pr_err_ratelimited("ITS virtual pending table not cleaning\n"); |
| 2308 | vpe->idai = false; |
| 2309 | vpe->pending_last = true; |
| 2310 | } else { |
| 2311 | vpe->idai = !!(val & GICR_VPENDBASER_IDAI); |
| 2312 | vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast); |
| 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) |
| 2317 | { |
| 2318 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2319 | struct its_cmd_info *info = vcpu_info; |
| 2320 | |
| 2321 | switch (info->cmd_type) { |
| 2322 | case SCHEDULE_VPE: |
| 2323 | its_vpe_schedule(vpe); |
| 2324 | return 0; |
| 2325 | |
| 2326 | case DESCHEDULE_VPE: |
| 2327 | its_vpe_deschedule(vpe); |
| 2328 | return 0; |
| 2329 | |
Marc Zyngier | 5e2f764 | 2016-12-20 15:10:50 +0000 | [diff] [blame] | 2330 | case INVALL_VPE: |
| 2331 | its_send_vinvall(vpe); |
| 2332 | return 0; |
| 2333 | |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2334 | default: |
| 2335 | return -EINVAL; |
| 2336 | } |
| 2337 | } |
| 2338 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2339 | static struct irq_chip its_vpe_irq_chip = { |
| 2340 | .name = "GICv4-vpe", |
Marc Zyngier | 3171a47 | 2016-12-20 15:17:28 +0000 | [diff] [blame^] | 2341 | .irq_set_affinity = its_vpe_set_affinity, |
Marc Zyngier | e643d80 | 2016-12-20 15:09:31 +0000 | [diff] [blame] | 2342 | .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity, |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2343 | }; |
| 2344 | |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 2345 | static int its_vpe_id_alloc(void) |
| 2346 | { |
| 2347 | return ida_simple_get(&its_vpeid_ida, 0, 1 << 16, GFP_KERNEL); |
| 2348 | } |
| 2349 | |
| 2350 | static void its_vpe_id_free(u16 id) |
| 2351 | { |
| 2352 | ida_simple_remove(&its_vpeid_ida, id); |
| 2353 | } |
| 2354 | |
| 2355 | static int its_vpe_init(struct its_vpe *vpe) |
| 2356 | { |
| 2357 | struct page *vpt_page; |
| 2358 | int vpe_id; |
| 2359 | |
| 2360 | /* Allocate vpe_id */ |
| 2361 | vpe_id = its_vpe_id_alloc(); |
| 2362 | if (vpe_id < 0) |
| 2363 | return vpe_id; |
| 2364 | |
| 2365 | /* Allocate VPT */ |
| 2366 | vpt_page = its_allocate_pending_table(GFP_KERNEL); |
| 2367 | if (!vpt_page) { |
| 2368 | its_vpe_id_free(vpe_id); |
| 2369 | return -ENOMEM; |
| 2370 | } |
| 2371 | |
| 2372 | if (!its_alloc_vpe_table(vpe_id)) { |
| 2373 | its_vpe_id_free(vpe_id); |
| 2374 | its_free_pending_table(vpe->vpt_page); |
| 2375 | return -ENOMEM; |
| 2376 | } |
| 2377 | |
| 2378 | vpe->vpe_id = vpe_id; |
| 2379 | vpe->vpt_page = vpt_page; |
| 2380 | |
| 2381 | return 0; |
| 2382 | } |
| 2383 | |
| 2384 | static void its_vpe_teardown(struct its_vpe *vpe) |
| 2385 | { |
| 2386 | its_vpe_id_free(vpe->vpe_id); |
| 2387 | its_free_pending_table(vpe->vpt_page); |
| 2388 | } |
| 2389 | |
| 2390 | static void its_vpe_irq_domain_free(struct irq_domain *domain, |
| 2391 | unsigned int virq, |
| 2392 | unsigned int nr_irqs) |
| 2393 | { |
| 2394 | struct its_vm *vm = domain->host_data; |
| 2395 | int i; |
| 2396 | |
| 2397 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 2398 | |
| 2399 | for (i = 0; i < nr_irqs; i++) { |
| 2400 | struct irq_data *data = irq_domain_get_irq_data(domain, |
| 2401 | virq + i); |
| 2402 | struct its_vpe *vpe = irq_data_get_irq_chip_data(data); |
| 2403 | |
| 2404 | BUG_ON(vm != vpe->its_vm); |
| 2405 | |
| 2406 | clear_bit(data->hwirq, vm->db_bitmap); |
| 2407 | its_vpe_teardown(vpe); |
| 2408 | irq_domain_reset_irq_data(data); |
| 2409 | } |
| 2410 | |
| 2411 | if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) { |
| 2412 | its_lpi_free_chunks(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis); |
| 2413 | its_free_prop_table(vm->vprop_page); |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 2418 | unsigned int nr_irqs, void *args) |
| 2419 | { |
| 2420 | struct its_vm *vm = args; |
| 2421 | unsigned long *bitmap; |
| 2422 | struct page *vprop_page; |
| 2423 | int base, nr_ids, i, err = 0; |
| 2424 | |
| 2425 | BUG_ON(!vm); |
| 2426 | |
| 2427 | bitmap = its_lpi_alloc_chunks(nr_irqs, &base, &nr_ids); |
| 2428 | if (!bitmap) |
| 2429 | return -ENOMEM; |
| 2430 | |
| 2431 | if (nr_ids < nr_irqs) { |
| 2432 | its_lpi_free_chunks(bitmap, base, nr_ids); |
| 2433 | return -ENOMEM; |
| 2434 | } |
| 2435 | |
| 2436 | vprop_page = its_allocate_prop_table(GFP_KERNEL); |
| 2437 | if (!vprop_page) { |
| 2438 | its_lpi_free_chunks(bitmap, base, nr_ids); |
| 2439 | return -ENOMEM; |
| 2440 | } |
| 2441 | |
| 2442 | vm->db_bitmap = bitmap; |
| 2443 | vm->db_lpi_base = base; |
| 2444 | vm->nr_db_lpis = nr_ids; |
| 2445 | vm->vprop_page = vprop_page; |
| 2446 | |
| 2447 | for (i = 0; i < nr_irqs; i++) { |
| 2448 | vm->vpes[i]->vpe_db_lpi = base + i; |
| 2449 | err = its_vpe_init(vm->vpes[i]); |
| 2450 | if (err) |
| 2451 | break; |
| 2452 | err = its_irq_gic_domain_alloc(domain, virq + i, |
| 2453 | vm->vpes[i]->vpe_db_lpi); |
| 2454 | if (err) |
| 2455 | break; |
| 2456 | irq_domain_set_hwirq_and_chip(domain, virq + i, i, |
| 2457 | &its_vpe_irq_chip, vm->vpes[i]); |
| 2458 | set_bit(i, bitmap); |
| 2459 | } |
| 2460 | |
| 2461 | if (err) { |
| 2462 | if (i > 0) |
| 2463 | its_vpe_irq_domain_free(domain, virq, i - 1); |
| 2464 | |
| 2465 | its_lpi_free_chunks(bitmap, base, nr_ids); |
| 2466 | its_free_prop_table(vprop_page); |
| 2467 | } |
| 2468 | |
| 2469 | return err; |
| 2470 | } |
| 2471 | |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 2472 | static void its_vpe_irq_domain_activate(struct irq_domain *domain, |
| 2473 | struct irq_data *d) |
| 2474 | { |
| 2475 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2476 | |
| 2477 | /* Map the VPE to the first possible CPU */ |
| 2478 | vpe->col_idx = cpumask_first(cpu_online_mask); |
| 2479 | its_send_vmapp(vpe, true); |
| 2480 | its_send_vinvall(vpe); |
| 2481 | } |
| 2482 | |
| 2483 | static void its_vpe_irq_domain_deactivate(struct irq_domain *domain, |
| 2484 | struct irq_data *d) |
| 2485 | { |
| 2486 | struct its_vpe *vpe = irq_data_get_irq_chip_data(d); |
| 2487 | |
| 2488 | its_send_vmapp(vpe, false); |
| 2489 | } |
| 2490 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2491 | static const struct irq_domain_ops its_vpe_domain_ops = { |
Marc Zyngier | 7d75bbb | 2016-12-20 13:55:54 +0000 | [diff] [blame] | 2492 | .alloc = its_vpe_irq_domain_alloc, |
| 2493 | .free = its_vpe_irq_domain_free, |
Marc Zyngier | eb78192 | 2016-12-20 14:47:05 +0000 | [diff] [blame] | 2494 | .activate = its_vpe_irq_domain_activate, |
| 2495 | .deactivate = its_vpe_irq_domain_deactivate, |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2496 | }; |
| 2497 | |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 2498 | static int its_force_quiescent(void __iomem *base) |
| 2499 | { |
| 2500 | u32 count = 1000000; /* 1s */ |
| 2501 | u32 val; |
| 2502 | |
| 2503 | val = readl_relaxed(base + GITS_CTLR); |
David Daney | 7611da8 | 2016-08-18 15:41:58 -0700 | [diff] [blame] | 2504 | /* |
| 2505 | * GIC architecture specification requires the ITS to be both |
| 2506 | * disabled and quiescent for writes to GITS_BASER<n> or |
| 2507 | * GITS_CBASER to not have UNPREDICTABLE results. |
| 2508 | */ |
| 2509 | if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE)) |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 2510 | return 0; |
| 2511 | |
| 2512 | /* Disable the generation of all interrupts to this ITS */ |
| 2513 | val &= ~GITS_CTLR_ENABLE; |
| 2514 | writel_relaxed(val, base + GITS_CTLR); |
| 2515 | |
| 2516 | /* Poll GITS_CTLR and wait until ITS becomes quiescent */ |
| 2517 | while (1) { |
| 2518 | val = readl_relaxed(base + GITS_CTLR); |
| 2519 | if (val & GITS_CTLR_QUIESCENT) |
| 2520 | return 0; |
| 2521 | |
| 2522 | count--; |
| 2523 | if (!count) |
| 2524 | return -EBUSY; |
| 2525 | |
| 2526 | cpu_relax(); |
| 2527 | udelay(1); |
| 2528 | } |
| 2529 | } |
| 2530 | |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 2531 | static void __maybe_unused its_enable_quirk_cavium_22375(void *data) |
| 2532 | { |
| 2533 | struct its_node *its = data; |
| 2534 | |
| 2535 | its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375; |
| 2536 | } |
| 2537 | |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 2538 | static void __maybe_unused its_enable_quirk_cavium_23144(void *data) |
| 2539 | { |
| 2540 | struct its_node *its = data; |
| 2541 | |
| 2542 | its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144; |
| 2543 | } |
| 2544 | |
Shanker Donthineni | 90922a2 | 2017-03-07 08:20:38 -0600 | [diff] [blame] | 2545 | static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data) |
| 2546 | { |
| 2547 | struct its_node *its = data; |
| 2548 | |
| 2549 | /* On QDF2400, the size of the ITE is 16Bytes */ |
| 2550 | its->ite_size = 16; |
| 2551 | } |
| 2552 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 2553 | static const struct gic_quirk its_quirks[] = { |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 2554 | #ifdef CONFIG_CAVIUM_ERRATUM_22375 |
| 2555 | { |
| 2556 | .desc = "ITS: Cavium errata 22375, 24313", |
| 2557 | .iidr = 0xa100034c, /* ThunderX pass 1.x */ |
| 2558 | .mask = 0xffff0fff, |
| 2559 | .init = its_enable_quirk_cavium_22375, |
| 2560 | }, |
| 2561 | #endif |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 2562 | #ifdef CONFIG_CAVIUM_ERRATUM_23144 |
| 2563 | { |
| 2564 | .desc = "ITS: Cavium erratum 23144", |
| 2565 | .iidr = 0xa100034c, /* ThunderX pass 1.x */ |
| 2566 | .mask = 0xffff0fff, |
| 2567 | .init = its_enable_quirk_cavium_23144, |
| 2568 | }, |
| 2569 | #endif |
Shanker Donthineni | 90922a2 | 2017-03-07 08:20:38 -0600 | [diff] [blame] | 2570 | #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065 |
| 2571 | { |
| 2572 | .desc = "ITS: QDF2400 erratum 0065", |
| 2573 | .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */ |
| 2574 | .mask = 0xffffffff, |
| 2575 | .init = its_enable_quirk_qdf2400_e0065, |
| 2576 | }, |
| 2577 | #endif |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 2578 | { |
| 2579 | } |
| 2580 | }; |
| 2581 | |
| 2582 | static void its_enable_quirks(struct its_node *its) |
| 2583 | { |
| 2584 | u32 iidr = readl_relaxed(its->base + GITS_IIDR); |
| 2585 | |
| 2586 | gic_enable_quirks(iidr, its_quirks, its); |
| 2587 | } |
| 2588 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2589 | 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] | 2590 | { |
| 2591 | struct irq_domain *inner_domain; |
| 2592 | struct msi_domain_info *info; |
| 2593 | |
| 2594 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 2595 | if (!info) |
| 2596 | return -ENOMEM; |
| 2597 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2598 | inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2599 | if (!inner_domain) { |
| 2600 | kfree(info); |
| 2601 | return -ENOMEM; |
| 2602 | } |
| 2603 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2604 | inner_domain->parent = its_parent; |
Marc Zyngier | 96f0d93 | 2017-06-22 11:42:50 +0100 | [diff] [blame] | 2605 | irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS); |
Eric Auger | 5976852 | 2017-01-19 20:58:00 +0000 | [diff] [blame] | 2606 | inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP; |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2607 | info->ops = &its_msi_domain_ops; |
| 2608 | info->data = its; |
| 2609 | inner_domain->host_data = info; |
| 2610 | |
| 2611 | return 0; |
| 2612 | } |
| 2613 | |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2614 | static int its_init_vpe_domain(void) |
| 2615 | { |
| 2616 | return 0; |
| 2617 | } |
| 2618 | |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2619 | static int __init its_compute_its_list_map(struct resource *res, |
| 2620 | void __iomem *its_base) |
| 2621 | { |
| 2622 | int its_number; |
| 2623 | u32 ctlr; |
| 2624 | |
| 2625 | /* |
| 2626 | * This is assumed to be done early enough that we're |
| 2627 | * guaranteed to be single-threaded, hence no |
| 2628 | * locking. Should this change, we should address |
| 2629 | * this. |
| 2630 | */ |
| 2631 | its_number = find_first_zero_bit(&its_list_map, ITS_LIST_MAX); |
| 2632 | if (its_number >= ITS_LIST_MAX) { |
| 2633 | pr_err("ITS@%pa: No ITSList entry available!\n", |
| 2634 | &res->start); |
| 2635 | return -EINVAL; |
| 2636 | } |
| 2637 | |
| 2638 | ctlr = readl_relaxed(its_base + GITS_CTLR); |
| 2639 | ctlr &= ~GITS_CTLR_ITS_NUMBER; |
| 2640 | ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT; |
| 2641 | writel_relaxed(ctlr, its_base + GITS_CTLR); |
| 2642 | ctlr = readl_relaxed(its_base + GITS_CTLR); |
| 2643 | if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) { |
| 2644 | its_number = ctlr & GITS_CTLR_ITS_NUMBER; |
| 2645 | its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT; |
| 2646 | } |
| 2647 | |
| 2648 | if (test_and_set_bit(its_number, &its_list_map)) { |
| 2649 | pr_err("ITS@%pa: Duplicate ITSList entry %d\n", |
| 2650 | &res->start, its_number); |
| 2651 | return -EINVAL; |
| 2652 | } |
| 2653 | |
| 2654 | return its_number; |
| 2655 | } |
| 2656 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2657 | static int __init its_probe_one(struct resource *res, |
| 2658 | struct fwnode_handle *handle, int numa_node) |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2659 | { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2660 | struct its_node *its; |
| 2661 | void __iomem *its_base; |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2662 | u32 val, ctlr; |
| 2663 | u64 baser, tmp, typer; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2664 | int err; |
| 2665 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2666 | its_base = ioremap(res->start, resource_size(res)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2667 | if (!its_base) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2668 | pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2669 | return -ENOMEM; |
| 2670 | } |
| 2671 | |
| 2672 | val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK; |
| 2673 | if (val != 0x30 && val != 0x40) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2674 | pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2675 | err = -ENODEV; |
| 2676 | goto out_unmap; |
| 2677 | } |
| 2678 | |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 2679 | err = its_force_quiescent(its_base); |
| 2680 | if (err) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2681 | pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start); |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 2682 | goto out_unmap; |
| 2683 | } |
| 2684 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2685 | pr_info("ITS %pR\n", res); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2686 | |
| 2687 | its = kzalloc(sizeof(*its), GFP_KERNEL); |
| 2688 | if (!its) { |
| 2689 | err = -ENOMEM; |
| 2690 | goto out_unmap; |
| 2691 | } |
| 2692 | |
| 2693 | raw_spin_lock_init(&its->lock); |
| 2694 | INIT_LIST_HEAD(&its->entry); |
| 2695 | INIT_LIST_HEAD(&its->its_device_list); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2696 | typer = gic_read_typer(its_base + GITS_TYPER); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2697 | its->base = its_base; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2698 | its->phys_base = res->start; |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2699 | its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer); |
| 2700 | its->is_v4 = !!(typer & GITS_TYPER_VLPIS); |
| 2701 | if (its->is_v4) { |
| 2702 | if (!(typer & GITS_TYPER_VMOVP)) { |
| 2703 | err = its_compute_its_list_map(res, its_base); |
| 2704 | if (err < 0) |
| 2705 | goto out_free_its; |
| 2706 | |
| 2707 | pr_info("ITS@%pa: Using ITS number %d\n", |
| 2708 | &res->start, err); |
| 2709 | } else { |
| 2710 | pr_info("ITS@%pa: Single VMOVP capable\n", &res->start); |
| 2711 | } |
| 2712 | } |
| 2713 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2714 | its->numa_node = numa_node; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2715 | |
Robert Richter | 5bc13c2 | 2017-02-01 18:38:25 +0100 | [diff] [blame] | 2716 | its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 2717 | get_order(ITS_CMD_QUEUE_SZ)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2718 | if (!its->cmd_base) { |
| 2719 | err = -ENOMEM; |
| 2720 | goto out_free_its; |
| 2721 | } |
| 2722 | its->cmd_write = its->cmd_base; |
| 2723 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 2724 | its_enable_quirks(its); |
| 2725 | |
Shanker Donthineni | 0e0b0f6 | 2016-06-06 18:17:31 -0500 | [diff] [blame] | 2726 | err = its_alloc_tables(its); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2727 | if (err) |
| 2728 | goto out_free_cmd; |
| 2729 | |
| 2730 | err = its_alloc_collections(its); |
| 2731 | if (err) |
| 2732 | goto out_free_tables; |
| 2733 | |
| 2734 | baser = (virt_to_phys(its->cmd_base) | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 2735 | GITS_CBASER_RaWaWb | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2736 | GITS_CBASER_InnerShareable | |
| 2737 | (ITS_CMD_QUEUE_SZ / SZ_4K - 1) | |
| 2738 | GITS_CBASER_VALID); |
| 2739 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2740 | gits_write_cbaser(baser, its->base + GITS_CBASER); |
| 2741 | tmp = gits_read_cbaser(its->base + GITS_CBASER); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2742 | |
Marc Zyngier | 4ad3e36 | 2015-03-27 14:15:04 +0000 | [diff] [blame] | 2743 | if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) { |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2744 | if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) { |
| 2745 | /* |
| 2746 | * The HW reports non-shareable, we must |
| 2747 | * remove the cacheability attributes as |
| 2748 | * well. |
| 2749 | */ |
| 2750 | baser &= ~(GITS_CBASER_SHAREABILITY_MASK | |
| 2751 | GITS_CBASER_CACHEABILITY_MASK); |
| 2752 | baser |= GITS_CBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2753 | gits_write_cbaser(baser, its->base + GITS_CBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2754 | } |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2755 | pr_info("ITS: using cache flushing for cmd queue\n"); |
| 2756 | its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING; |
| 2757 | } |
| 2758 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2759 | gits_write_cwriter(0, its->base + GITS_CWRITER); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2760 | ctlr = readl_relaxed(its->base + GITS_CTLR); |
| 2761 | writel_relaxed(ctlr | GITS_CTLR_ENABLE, its->base + GITS_CTLR); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2762 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2763 | err = its_init_domain(handle, its); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2764 | if (err) |
| 2765 | goto out_free_tables; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2766 | |
| 2767 | spin_lock(&its_lock); |
| 2768 | list_add(&its->entry, &its_nodes); |
| 2769 | spin_unlock(&its_lock); |
| 2770 | |
| 2771 | return 0; |
| 2772 | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2773 | out_free_tables: |
| 2774 | its_free_tables(its); |
| 2775 | out_free_cmd: |
Robert Richter | 5bc13c2 | 2017-02-01 18:38:25 +0100 | [diff] [blame] | 2776 | 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] | 2777 | out_free_its: |
| 2778 | kfree(its); |
| 2779 | out_unmap: |
| 2780 | iounmap(its_base); |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2781 | pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2782 | return err; |
| 2783 | } |
| 2784 | |
| 2785 | static bool gic_rdists_supports_plpis(void) |
| 2786 | { |
Marc Zyngier | 589ce5f | 2016-10-14 15:13:07 +0100 | [diff] [blame] | 2787 | 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] | 2788 | } |
| 2789 | |
| 2790 | int its_cpu_init(void) |
| 2791 | { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2792 | if (!list_empty(&its_nodes)) { |
Vladimir Murzin | 16acae7 | 2015-03-06 16:37:40 +0000 | [diff] [blame] | 2793 | if (!gic_rdists_supports_plpis()) { |
| 2794 | pr_info("CPU%d: LPIs not supported\n", smp_processor_id()); |
| 2795 | return -ENXIO; |
| 2796 | } |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2797 | its_cpu_init_lpis(); |
| 2798 | its_cpu_init_collection(); |
| 2799 | } |
| 2800 | |
| 2801 | return 0; |
| 2802 | } |
| 2803 | |
Arvind Yadav | 935bba7 | 2017-06-22 16:05:30 +0530 | [diff] [blame] | 2804 | static const struct of_device_id its_device_id[] = { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2805 | { .compatible = "arm,gic-v3-its", }, |
| 2806 | {}, |
| 2807 | }; |
| 2808 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2809 | static int __init its_of_probe(struct device_node *node) |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2810 | { |
| 2811 | struct device_node *np; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2812 | struct resource res; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2813 | |
| 2814 | for (np = of_find_matching_node(node, its_device_id); np; |
| 2815 | np = of_find_matching_node(np, its_device_id)) { |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2816 | if (!of_property_read_bool(np, "msi-controller")) { |
Rob Herring | e81f54c | 2017-07-18 16:43:10 -0500 | [diff] [blame] | 2817 | pr_warn("%pOF: no msi-controller property, ITS ignored\n", |
| 2818 | np); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2819 | continue; |
| 2820 | } |
| 2821 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2822 | if (of_address_to_resource(np, 0, &res)) { |
Rob Herring | e81f54c | 2017-07-18 16:43:10 -0500 | [diff] [blame] | 2823 | pr_warn("%pOF: no regs?\n", np); |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2824 | continue; |
| 2825 | } |
| 2826 | |
| 2827 | its_probe_one(&res, &np->fwnode, of_node_to_nid(np)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2828 | } |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2829 | return 0; |
| 2830 | } |
| 2831 | |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2832 | #ifdef CONFIG_ACPI |
| 2833 | |
| 2834 | #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K) |
| 2835 | |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 2836 | #if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531) |
| 2837 | struct its_srat_map { |
| 2838 | /* numa node id */ |
| 2839 | u32 numa_node; |
| 2840 | /* GIC ITS ID */ |
| 2841 | u32 its_id; |
| 2842 | }; |
| 2843 | |
| 2844 | static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata; |
| 2845 | static int its_in_srat __initdata; |
| 2846 | |
| 2847 | static int __init acpi_get_its_numa_node(u32 its_id) |
| 2848 | { |
| 2849 | int i; |
| 2850 | |
| 2851 | for (i = 0; i < its_in_srat; i++) { |
| 2852 | if (its_id == its_srat_maps[i].its_id) |
| 2853 | return its_srat_maps[i].numa_node; |
| 2854 | } |
| 2855 | return NUMA_NO_NODE; |
| 2856 | } |
| 2857 | |
| 2858 | static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header, |
| 2859 | const unsigned long end) |
| 2860 | { |
| 2861 | int node; |
| 2862 | struct acpi_srat_gic_its_affinity *its_affinity; |
| 2863 | |
| 2864 | its_affinity = (struct acpi_srat_gic_its_affinity *)header; |
| 2865 | if (!its_affinity) |
| 2866 | return -EINVAL; |
| 2867 | |
| 2868 | if (its_affinity->header.length < sizeof(*its_affinity)) { |
| 2869 | pr_err("SRAT: Invalid header length %d in ITS affinity\n", |
| 2870 | its_affinity->header.length); |
| 2871 | return -EINVAL; |
| 2872 | } |
| 2873 | |
| 2874 | if (its_in_srat >= MAX_NUMNODES) { |
| 2875 | pr_err("SRAT: ITS affinity exceeding max count[%d]\n", |
| 2876 | MAX_NUMNODES); |
| 2877 | return -EINVAL; |
| 2878 | } |
| 2879 | |
| 2880 | node = acpi_map_pxm_to_node(its_affinity->proximity_domain); |
| 2881 | |
| 2882 | if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) { |
| 2883 | pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node); |
| 2884 | return 0; |
| 2885 | } |
| 2886 | |
| 2887 | its_srat_maps[its_in_srat].numa_node = node; |
| 2888 | its_srat_maps[its_in_srat].its_id = its_affinity->its_id; |
| 2889 | its_in_srat++; |
| 2890 | pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n", |
| 2891 | its_affinity->proximity_domain, its_affinity->its_id, node); |
| 2892 | |
| 2893 | return 0; |
| 2894 | } |
| 2895 | |
| 2896 | static void __init acpi_table_parse_srat_its(void) |
| 2897 | { |
| 2898 | acpi_table_parse_entries(ACPI_SIG_SRAT, |
| 2899 | sizeof(struct acpi_table_srat), |
| 2900 | ACPI_SRAT_TYPE_GIC_ITS_AFFINITY, |
| 2901 | gic_acpi_parse_srat_its, 0); |
| 2902 | } |
| 2903 | #else |
| 2904 | static void __init acpi_table_parse_srat_its(void) { } |
| 2905 | static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; } |
| 2906 | #endif |
| 2907 | |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2908 | static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header, |
| 2909 | const unsigned long end) |
| 2910 | { |
| 2911 | struct acpi_madt_generic_translator *its_entry; |
| 2912 | struct fwnode_handle *dom_handle; |
| 2913 | struct resource res; |
| 2914 | int err; |
| 2915 | |
| 2916 | its_entry = (struct acpi_madt_generic_translator *)header; |
| 2917 | memset(&res, 0, sizeof(res)); |
| 2918 | res.start = its_entry->base_address; |
| 2919 | res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1; |
| 2920 | res.flags = IORESOURCE_MEM; |
| 2921 | |
| 2922 | dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address); |
| 2923 | if (!dom_handle) { |
| 2924 | pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n", |
| 2925 | &res.start); |
| 2926 | return -ENOMEM; |
| 2927 | } |
| 2928 | |
| 2929 | err = iort_register_domain_token(its_entry->translation_id, dom_handle); |
| 2930 | if (err) { |
| 2931 | pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n", |
| 2932 | &res.start, its_entry->translation_id); |
| 2933 | goto dom_err; |
| 2934 | } |
| 2935 | |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 2936 | err = its_probe_one(&res, dom_handle, |
| 2937 | acpi_get_its_numa_node(its_entry->translation_id)); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2938 | if (!err) |
| 2939 | return 0; |
| 2940 | |
| 2941 | iort_deregister_domain_token(its_entry->translation_id); |
| 2942 | dom_err: |
| 2943 | irq_domain_free_fwnode(dom_handle); |
| 2944 | return err; |
| 2945 | } |
| 2946 | |
| 2947 | static void __init its_acpi_probe(void) |
| 2948 | { |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 2949 | acpi_table_parse_srat_its(); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2950 | acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR, |
| 2951 | gic_acpi_parse_madt_its, 0); |
| 2952 | } |
| 2953 | #else |
| 2954 | static void __init its_acpi_probe(void) { } |
| 2955 | #endif |
| 2956 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2957 | int __init its_init(struct fwnode_handle *handle, struct rdists *rdists, |
| 2958 | struct irq_domain *parent_domain) |
| 2959 | { |
| 2960 | struct device_node *of_node; |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2961 | struct its_node *its; |
| 2962 | bool has_v4 = false; |
| 2963 | int err; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2964 | |
| 2965 | its_parent = parent_domain; |
| 2966 | of_node = to_of_node(handle); |
| 2967 | if (of_node) |
| 2968 | its_of_probe(of_node); |
| 2969 | else |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2970 | its_acpi_probe(); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2971 | |
| 2972 | if (list_empty(&its_nodes)) { |
| 2973 | pr_warn("ITS: No ITS available, not enabling LPIs\n"); |
| 2974 | return -ENXIO; |
| 2975 | } |
| 2976 | |
| 2977 | gic_rdists = rdists; |
Marc Zyngier | 8fff27a | 2016-12-20 13:41:55 +0000 | [diff] [blame] | 2978 | err = its_alloc_lpi_tables(); |
| 2979 | if (err) |
| 2980 | return err; |
| 2981 | |
| 2982 | list_for_each_entry(its, &its_nodes, entry) |
| 2983 | has_v4 |= its->is_v4; |
| 2984 | |
| 2985 | if (has_v4 & rdists->has_vlpis) { |
| 2986 | if (its_init_vpe_domain()) { |
| 2987 | rdists->has_vlpis = false; |
| 2988 | pr_err("ITS: Disabling GICv4 support\n"); |
| 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | return 0; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2993 | } |