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; |
| 151 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 152 | #define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist)) |
| 153 | #define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base) |
| 154 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 155 | static struct its_collection *dev_event_to_col(struct its_device *its_dev, |
| 156 | u32 event) |
| 157 | { |
| 158 | struct its_node *its = its_dev->its; |
| 159 | |
| 160 | return its->collections + its_dev->event_map.col_map[event]; |
| 161 | } |
| 162 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 163 | /* |
| 164 | * ITS command descriptors - parameters to be encoded in a command |
| 165 | * block. |
| 166 | */ |
| 167 | struct its_cmd_desc { |
| 168 | union { |
| 169 | struct { |
| 170 | struct its_device *dev; |
| 171 | u32 event_id; |
| 172 | } its_inv_cmd; |
| 173 | |
| 174 | struct { |
| 175 | struct its_device *dev; |
| 176 | u32 event_id; |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 177 | } its_clear_cmd; |
| 178 | |
| 179 | struct { |
| 180 | struct its_device *dev; |
| 181 | u32 event_id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 182 | } its_int_cmd; |
| 183 | |
| 184 | struct { |
| 185 | struct its_device *dev; |
| 186 | int valid; |
| 187 | } its_mapd_cmd; |
| 188 | |
| 189 | struct { |
| 190 | struct its_collection *col; |
| 191 | int valid; |
| 192 | } its_mapc_cmd; |
| 193 | |
| 194 | struct { |
| 195 | struct its_device *dev; |
| 196 | u32 phys_id; |
| 197 | u32 event_id; |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 198 | } its_mapti_cmd; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 199 | |
| 200 | struct { |
| 201 | struct its_device *dev; |
| 202 | struct its_collection *col; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 203 | u32 event_id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 204 | } its_movi_cmd; |
| 205 | |
| 206 | struct { |
| 207 | struct its_device *dev; |
| 208 | u32 event_id; |
| 209 | } its_discard_cmd; |
| 210 | |
| 211 | struct { |
| 212 | struct its_collection *col; |
| 213 | } its_invall_cmd; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame^] | 214 | |
| 215 | struct { |
| 216 | struct its_vpe *vpe; |
| 217 | struct its_device *dev; |
| 218 | u32 virt_id; |
| 219 | u32 event_id; |
| 220 | bool db_enabled; |
| 221 | } its_vmapti_cmd; |
| 222 | |
| 223 | struct { |
| 224 | struct its_vpe *vpe; |
| 225 | struct its_device *dev; |
| 226 | u32 event_id; |
| 227 | bool db_enabled; |
| 228 | } its_vmovi_cmd; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 229 | }; |
| 230 | }; |
| 231 | |
| 232 | /* |
| 233 | * The ITS command block, which is what the ITS actually parses. |
| 234 | */ |
| 235 | struct its_cmd_block { |
| 236 | u64 raw_cmd[4]; |
| 237 | }; |
| 238 | |
| 239 | #define ITS_CMD_QUEUE_SZ SZ_64K |
| 240 | #define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block)) |
| 241 | |
| 242 | typedef struct its_collection *(*its_cmd_builder_t)(struct its_cmd_block *, |
| 243 | struct its_cmd_desc *); |
| 244 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame^] | 245 | typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_cmd_block *, |
| 246 | struct its_cmd_desc *); |
| 247 | |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 248 | static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l) |
| 249 | { |
| 250 | u64 mask = GENMASK_ULL(h, l); |
| 251 | *raw_cmd &= ~mask; |
| 252 | *raw_cmd |= (val << l) & mask; |
| 253 | } |
| 254 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 255 | static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr) |
| 256 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 257 | its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | static void its_encode_devid(struct its_cmd_block *cmd, u32 devid) |
| 261 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 262 | its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static void its_encode_event_id(struct its_cmd_block *cmd, u32 id) |
| 266 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 267 | its_mask_encode(&cmd->raw_cmd[1], id, 31, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id) |
| 271 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 272 | its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static void its_encode_size(struct its_cmd_block *cmd, u8 size) |
| 276 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 277 | its_mask_encode(&cmd->raw_cmd[1], size, 4, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr) |
| 281 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 282 | its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 50, 8); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | static void its_encode_valid(struct its_cmd_block *cmd, int valid) |
| 286 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 287 | its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr) |
| 291 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 292 | its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 50, 16); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static void its_encode_collection(struct its_cmd_block *cmd, u16 col) |
| 296 | { |
Marc Zyngier | 4d36f13 | 2016-12-19 17:11:52 +0000 | [diff] [blame] | 297 | its_mask_encode(&cmd->raw_cmd[2], col, 15, 0); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame^] | 300 | static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid) |
| 301 | { |
| 302 | its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32); |
| 303 | } |
| 304 | |
| 305 | static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id) |
| 306 | { |
| 307 | its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0); |
| 308 | } |
| 309 | |
| 310 | static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id) |
| 311 | { |
| 312 | its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32); |
| 313 | } |
| 314 | |
| 315 | static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid) |
| 316 | { |
| 317 | its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0); |
| 318 | } |
| 319 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 320 | static inline void its_fixup_cmd(struct its_cmd_block *cmd) |
| 321 | { |
| 322 | /* Let's fixup BE commands */ |
| 323 | cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]); |
| 324 | cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]); |
| 325 | cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]); |
| 326 | cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]); |
| 327 | } |
| 328 | |
| 329 | static struct its_collection *its_build_mapd_cmd(struct its_cmd_block *cmd, |
| 330 | struct its_cmd_desc *desc) |
| 331 | { |
| 332 | unsigned long itt_addr; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 333 | u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 334 | |
| 335 | itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt); |
| 336 | itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN); |
| 337 | |
| 338 | its_encode_cmd(cmd, GITS_CMD_MAPD); |
| 339 | its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id); |
| 340 | its_encode_size(cmd, size - 1); |
| 341 | its_encode_itt(cmd, itt_addr); |
| 342 | its_encode_valid(cmd, desc->its_mapd_cmd.valid); |
| 343 | |
| 344 | its_fixup_cmd(cmd); |
| 345 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 346 | return NULL; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | static struct its_collection *its_build_mapc_cmd(struct its_cmd_block *cmd, |
| 350 | struct its_cmd_desc *desc) |
| 351 | { |
| 352 | its_encode_cmd(cmd, GITS_CMD_MAPC); |
| 353 | its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id); |
| 354 | its_encode_target(cmd, desc->its_mapc_cmd.col->target_address); |
| 355 | its_encode_valid(cmd, desc->its_mapc_cmd.valid); |
| 356 | |
| 357 | its_fixup_cmd(cmd); |
| 358 | |
| 359 | return desc->its_mapc_cmd.col; |
| 360 | } |
| 361 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 362 | 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] | 363 | struct its_cmd_desc *desc) |
| 364 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 365 | struct its_collection *col; |
| 366 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 367 | col = dev_event_to_col(desc->its_mapti_cmd.dev, |
| 368 | desc->its_mapti_cmd.event_id); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 369 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 370 | its_encode_cmd(cmd, GITS_CMD_MAPTI); |
| 371 | its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id); |
| 372 | its_encode_event_id(cmd, desc->its_mapti_cmd.event_id); |
| 373 | its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 374 | its_encode_collection(cmd, col->col_id); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 375 | |
| 376 | its_fixup_cmd(cmd); |
| 377 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 378 | return col; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | static struct its_collection *its_build_movi_cmd(struct its_cmd_block *cmd, |
| 382 | struct its_cmd_desc *desc) |
| 383 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 384 | struct its_collection *col; |
| 385 | |
| 386 | col = dev_event_to_col(desc->its_movi_cmd.dev, |
| 387 | desc->its_movi_cmd.event_id); |
| 388 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 389 | its_encode_cmd(cmd, GITS_CMD_MOVI); |
| 390 | its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 391 | its_encode_event_id(cmd, desc->its_movi_cmd.event_id); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 392 | its_encode_collection(cmd, desc->its_movi_cmd.col->col_id); |
| 393 | |
| 394 | its_fixup_cmd(cmd); |
| 395 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 396 | return col; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static struct its_collection *its_build_discard_cmd(struct its_cmd_block *cmd, |
| 400 | struct its_cmd_desc *desc) |
| 401 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 402 | struct its_collection *col; |
| 403 | |
| 404 | col = dev_event_to_col(desc->its_discard_cmd.dev, |
| 405 | desc->its_discard_cmd.event_id); |
| 406 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 407 | its_encode_cmd(cmd, GITS_CMD_DISCARD); |
| 408 | its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id); |
| 409 | its_encode_event_id(cmd, desc->its_discard_cmd.event_id); |
| 410 | |
| 411 | its_fixup_cmd(cmd); |
| 412 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 413 | return col; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static struct its_collection *its_build_inv_cmd(struct its_cmd_block *cmd, |
| 417 | struct its_cmd_desc *desc) |
| 418 | { |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 419 | struct its_collection *col; |
| 420 | |
| 421 | col = dev_event_to_col(desc->its_inv_cmd.dev, |
| 422 | desc->its_inv_cmd.event_id); |
| 423 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 424 | its_encode_cmd(cmd, GITS_CMD_INV); |
| 425 | its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id); |
| 426 | its_encode_event_id(cmd, desc->its_inv_cmd.event_id); |
| 427 | |
| 428 | its_fixup_cmd(cmd); |
| 429 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 430 | return col; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 433 | static struct its_collection *its_build_int_cmd(struct its_cmd_block *cmd, |
| 434 | struct its_cmd_desc *desc) |
| 435 | { |
| 436 | struct its_collection *col; |
| 437 | |
| 438 | col = dev_event_to_col(desc->its_int_cmd.dev, |
| 439 | desc->its_int_cmd.event_id); |
| 440 | |
| 441 | its_encode_cmd(cmd, GITS_CMD_INT); |
| 442 | its_encode_devid(cmd, desc->its_int_cmd.dev->device_id); |
| 443 | its_encode_event_id(cmd, desc->its_int_cmd.event_id); |
| 444 | |
| 445 | its_fixup_cmd(cmd); |
| 446 | |
| 447 | return col; |
| 448 | } |
| 449 | |
| 450 | static struct its_collection *its_build_clear_cmd(struct its_cmd_block *cmd, |
| 451 | struct its_cmd_desc *desc) |
| 452 | { |
| 453 | struct its_collection *col; |
| 454 | |
| 455 | col = dev_event_to_col(desc->its_clear_cmd.dev, |
| 456 | desc->its_clear_cmd.event_id); |
| 457 | |
| 458 | its_encode_cmd(cmd, GITS_CMD_CLEAR); |
| 459 | its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id); |
| 460 | its_encode_event_id(cmd, desc->its_clear_cmd.event_id); |
| 461 | |
| 462 | its_fixup_cmd(cmd); |
| 463 | |
| 464 | return col; |
| 465 | } |
| 466 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 467 | static struct its_collection *its_build_invall_cmd(struct its_cmd_block *cmd, |
| 468 | struct its_cmd_desc *desc) |
| 469 | { |
| 470 | its_encode_cmd(cmd, GITS_CMD_INVALL); |
| 471 | its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id); |
| 472 | |
| 473 | its_fixup_cmd(cmd); |
| 474 | |
| 475 | return NULL; |
| 476 | } |
| 477 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame^] | 478 | static struct its_vpe *its_build_vmapti_cmd(struct its_cmd_block *cmd, |
| 479 | struct its_cmd_desc *desc) |
| 480 | { |
| 481 | u32 db; |
| 482 | |
| 483 | if (desc->its_vmapti_cmd.db_enabled) |
| 484 | db = desc->its_vmapti_cmd.vpe->vpe_db_lpi; |
| 485 | else |
| 486 | db = 1023; |
| 487 | |
| 488 | its_encode_cmd(cmd, GITS_CMD_VMAPTI); |
| 489 | its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id); |
| 490 | its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id); |
| 491 | its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id); |
| 492 | its_encode_db_phys_id(cmd, db); |
| 493 | its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id); |
| 494 | |
| 495 | its_fixup_cmd(cmd); |
| 496 | |
| 497 | return desc->its_vmapti_cmd.vpe; |
| 498 | } |
| 499 | |
| 500 | static struct its_vpe *its_build_vmovi_cmd(struct its_cmd_block *cmd, |
| 501 | struct its_cmd_desc *desc) |
| 502 | { |
| 503 | u32 db; |
| 504 | |
| 505 | if (desc->its_vmovi_cmd.db_enabled) |
| 506 | db = desc->its_vmovi_cmd.vpe->vpe_db_lpi; |
| 507 | else |
| 508 | db = 1023; |
| 509 | |
| 510 | its_encode_cmd(cmd, GITS_CMD_VMOVI); |
| 511 | its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id); |
| 512 | its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id); |
| 513 | its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id); |
| 514 | its_encode_db_phys_id(cmd, db); |
| 515 | its_encode_db_valid(cmd, true); |
| 516 | |
| 517 | its_fixup_cmd(cmd); |
| 518 | |
| 519 | return desc->its_vmovi_cmd.vpe; |
| 520 | } |
| 521 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 522 | static u64 its_cmd_ptr_to_offset(struct its_node *its, |
| 523 | struct its_cmd_block *ptr) |
| 524 | { |
| 525 | return (ptr - its->cmd_base) * sizeof(*ptr); |
| 526 | } |
| 527 | |
| 528 | static int its_queue_full(struct its_node *its) |
| 529 | { |
| 530 | int widx; |
| 531 | int ridx; |
| 532 | |
| 533 | widx = its->cmd_write - its->cmd_base; |
| 534 | ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block); |
| 535 | |
| 536 | /* This is incredibly unlikely to happen, unless the ITS locks up. */ |
| 537 | if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx) |
| 538 | return 1; |
| 539 | |
| 540 | return 0; |
| 541 | } |
| 542 | |
| 543 | static struct its_cmd_block *its_allocate_entry(struct its_node *its) |
| 544 | { |
| 545 | struct its_cmd_block *cmd; |
| 546 | u32 count = 1000000; /* 1s! */ |
| 547 | |
| 548 | while (its_queue_full(its)) { |
| 549 | count--; |
| 550 | if (!count) { |
| 551 | pr_err_ratelimited("ITS queue not draining\n"); |
| 552 | return NULL; |
| 553 | } |
| 554 | cpu_relax(); |
| 555 | udelay(1); |
| 556 | } |
| 557 | |
| 558 | cmd = its->cmd_write++; |
| 559 | |
| 560 | /* Handle queue wrapping */ |
| 561 | if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES)) |
| 562 | its->cmd_write = its->cmd_base; |
| 563 | |
Marc Zyngier | 34d677a | 2016-12-19 17:16:45 +0000 | [diff] [blame] | 564 | /* Clear command */ |
| 565 | cmd->raw_cmd[0] = 0; |
| 566 | cmd->raw_cmd[1] = 0; |
| 567 | cmd->raw_cmd[2] = 0; |
| 568 | cmd->raw_cmd[3] = 0; |
| 569 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 570 | return cmd; |
| 571 | } |
| 572 | |
| 573 | static struct its_cmd_block *its_post_commands(struct its_node *its) |
| 574 | { |
| 575 | u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write); |
| 576 | |
| 577 | writel_relaxed(wr, its->base + GITS_CWRITER); |
| 578 | |
| 579 | return its->cmd_write; |
| 580 | } |
| 581 | |
| 582 | static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd) |
| 583 | { |
| 584 | /* |
| 585 | * Make sure the commands written to memory are observable by |
| 586 | * the ITS. |
| 587 | */ |
| 588 | if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 589 | gic_flush_dcache_to_poc(cmd, sizeof(*cmd)); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 590 | else |
| 591 | dsb(ishst); |
| 592 | } |
| 593 | |
| 594 | static void its_wait_for_range_completion(struct its_node *its, |
| 595 | struct its_cmd_block *from, |
| 596 | struct its_cmd_block *to) |
| 597 | { |
| 598 | u64 rd_idx, from_idx, to_idx; |
| 599 | u32 count = 1000000; /* 1s! */ |
| 600 | |
| 601 | from_idx = its_cmd_ptr_to_offset(its, from); |
| 602 | to_idx = its_cmd_ptr_to_offset(its, to); |
| 603 | |
| 604 | while (1) { |
| 605 | rd_idx = readl_relaxed(its->base + GITS_CREADR); |
Marc Zyngier | 9bdd8b1 | 2017-08-19 10:16:02 +0100 | [diff] [blame] | 606 | |
| 607 | /* Direct case */ |
| 608 | if (from_idx < to_idx && rd_idx >= to_idx) |
| 609 | break; |
| 610 | |
| 611 | /* Wrapped case */ |
| 612 | 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] | 613 | break; |
| 614 | |
| 615 | count--; |
| 616 | if (!count) { |
| 617 | pr_err_ratelimited("ITS queue timeout\n"); |
| 618 | return; |
| 619 | } |
| 620 | cpu_relax(); |
| 621 | udelay(1); |
| 622 | } |
| 623 | } |
| 624 | |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 625 | /* Warning, macro hell follows */ |
| 626 | #define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \ |
| 627 | void name(struct its_node *its, \ |
| 628 | buildtype builder, \ |
| 629 | struct its_cmd_desc *desc) \ |
| 630 | { \ |
| 631 | struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \ |
| 632 | synctype *sync_obj; \ |
| 633 | unsigned long flags; \ |
| 634 | \ |
| 635 | raw_spin_lock_irqsave(&its->lock, flags); \ |
| 636 | \ |
| 637 | cmd = its_allocate_entry(its); \ |
| 638 | if (!cmd) { /* We're soooooo screewed... */ \ |
| 639 | raw_spin_unlock_irqrestore(&its->lock, flags); \ |
| 640 | return; \ |
| 641 | } \ |
| 642 | sync_obj = builder(cmd, desc); \ |
| 643 | its_flush_cmd(its, cmd); \ |
| 644 | \ |
| 645 | if (sync_obj) { \ |
| 646 | sync_cmd = its_allocate_entry(its); \ |
| 647 | if (!sync_cmd) \ |
| 648 | goto post; \ |
| 649 | \ |
| 650 | buildfn(sync_cmd, sync_obj); \ |
| 651 | its_flush_cmd(its, sync_cmd); \ |
| 652 | } \ |
| 653 | \ |
| 654 | post: \ |
| 655 | next_cmd = its_post_commands(its); \ |
| 656 | raw_spin_unlock_irqrestore(&its->lock, flags); \ |
| 657 | \ |
| 658 | its_wait_for_range_completion(its, cmd, next_cmd); \ |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 659 | } |
| 660 | |
Marc Zyngier | e4f9094 | 2016-12-19 17:56:32 +0000 | [diff] [blame] | 661 | static void its_build_sync_cmd(struct its_cmd_block *sync_cmd, |
| 662 | struct its_collection *sync_col) |
| 663 | { |
| 664 | its_encode_cmd(sync_cmd, GITS_CMD_SYNC); |
| 665 | its_encode_target(sync_cmd, sync_col->target_address); |
| 666 | |
| 667 | its_fixup_cmd(sync_cmd); |
| 668 | } |
| 669 | |
| 670 | static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t, |
| 671 | struct its_collection, its_build_sync_cmd) |
| 672 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame^] | 673 | static void its_build_vsync_cmd(struct its_cmd_block *sync_cmd, |
| 674 | struct its_vpe *sync_vpe) |
| 675 | { |
| 676 | its_encode_cmd(sync_cmd, GITS_CMD_VSYNC); |
| 677 | its_encode_vpeid(sync_cmd, sync_vpe->vpe_id); |
| 678 | |
| 679 | its_fixup_cmd(sync_cmd); |
| 680 | } |
| 681 | |
| 682 | static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t, |
| 683 | struct its_vpe, its_build_vsync_cmd) |
| 684 | |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 685 | static void its_send_int(struct its_device *dev, u32 event_id) |
| 686 | { |
| 687 | struct its_cmd_desc desc; |
| 688 | |
| 689 | desc.its_int_cmd.dev = dev; |
| 690 | desc.its_int_cmd.event_id = event_id; |
| 691 | |
| 692 | its_send_single_command(dev->its, its_build_int_cmd, &desc); |
| 693 | } |
| 694 | |
| 695 | static void its_send_clear(struct its_device *dev, u32 event_id) |
| 696 | { |
| 697 | struct its_cmd_desc desc; |
| 698 | |
| 699 | desc.its_clear_cmd.dev = dev; |
| 700 | desc.its_clear_cmd.event_id = event_id; |
| 701 | |
| 702 | its_send_single_command(dev->its, its_build_clear_cmd, &desc); |
| 703 | } |
| 704 | |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 705 | static void its_send_inv(struct its_device *dev, u32 event_id) |
| 706 | { |
| 707 | struct its_cmd_desc desc; |
| 708 | |
| 709 | desc.its_inv_cmd.dev = dev; |
| 710 | desc.its_inv_cmd.event_id = event_id; |
| 711 | |
| 712 | its_send_single_command(dev->its, its_build_inv_cmd, &desc); |
| 713 | } |
| 714 | |
| 715 | static void its_send_mapd(struct its_device *dev, int valid) |
| 716 | { |
| 717 | struct its_cmd_desc desc; |
| 718 | |
| 719 | desc.its_mapd_cmd.dev = dev; |
| 720 | desc.its_mapd_cmd.valid = !!valid; |
| 721 | |
| 722 | its_send_single_command(dev->its, its_build_mapd_cmd, &desc); |
| 723 | } |
| 724 | |
| 725 | static void its_send_mapc(struct its_node *its, struct its_collection *col, |
| 726 | int valid) |
| 727 | { |
| 728 | struct its_cmd_desc desc; |
| 729 | |
| 730 | desc.its_mapc_cmd.col = col; |
| 731 | desc.its_mapc_cmd.valid = !!valid; |
| 732 | |
| 733 | its_send_single_command(its, its_build_mapc_cmd, &desc); |
| 734 | } |
| 735 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 736 | 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] | 737 | { |
| 738 | struct its_cmd_desc desc; |
| 739 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 740 | desc.its_mapti_cmd.dev = dev; |
| 741 | desc.its_mapti_cmd.phys_id = irq_id; |
| 742 | desc.its_mapti_cmd.event_id = id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 743 | |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 744 | its_send_single_command(dev->its, its_build_mapti_cmd, &desc); |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | static void its_send_movi(struct its_device *dev, |
| 748 | struct its_collection *col, u32 id) |
| 749 | { |
| 750 | struct its_cmd_desc desc; |
| 751 | |
| 752 | desc.its_movi_cmd.dev = dev; |
| 753 | desc.its_movi_cmd.col = col; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 754 | desc.its_movi_cmd.event_id = id; |
Marc Zyngier | cc2d321 | 2014-11-24 14:35:11 +0000 | [diff] [blame] | 755 | |
| 756 | its_send_single_command(dev->its, its_build_movi_cmd, &desc); |
| 757 | } |
| 758 | |
| 759 | static void its_send_discard(struct its_device *dev, u32 id) |
| 760 | { |
| 761 | struct its_cmd_desc desc; |
| 762 | |
| 763 | desc.its_discard_cmd.dev = dev; |
| 764 | desc.its_discard_cmd.event_id = id; |
| 765 | |
| 766 | its_send_single_command(dev->its, its_build_discard_cmd, &desc); |
| 767 | } |
| 768 | |
| 769 | static void its_send_invall(struct its_node *its, struct its_collection *col) |
| 770 | { |
| 771 | struct its_cmd_desc desc; |
| 772 | |
| 773 | desc.its_invall_cmd.col = col; |
| 774 | |
| 775 | its_send_single_command(its, its_build_invall_cmd, &desc); |
| 776 | } |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 777 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame^] | 778 | static void its_send_vmapti(struct its_device *dev, u32 id) |
| 779 | { |
| 780 | struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id]; |
| 781 | struct its_cmd_desc desc; |
| 782 | |
| 783 | desc.its_vmapti_cmd.vpe = map->vpe; |
| 784 | desc.its_vmapti_cmd.dev = dev; |
| 785 | desc.its_vmapti_cmd.virt_id = map->vintid; |
| 786 | desc.its_vmapti_cmd.event_id = id; |
| 787 | desc.its_vmapti_cmd.db_enabled = map->db_enabled; |
| 788 | |
| 789 | its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc); |
| 790 | } |
| 791 | |
| 792 | static void its_send_vmovi(struct its_device *dev, u32 id) |
| 793 | { |
| 794 | struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id]; |
| 795 | struct its_cmd_desc desc; |
| 796 | |
| 797 | desc.its_vmovi_cmd.vpe = map->vpe; |
| 798 | desc.its_vmovi_cmd.dev = dev; |
| 799 | desc.its_vmovi_cmd.event_id = id; |
| 800 | desc.its_vmovi_cmd.db_enabled = map->db_enabled; |
| 801 | |
| 802 | its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc); |
| 803 | } |
| 804 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 805 | /* |
| 806 | * irqchip functions - assumes MSI, mostly. |
| 807 | */ |
| 808 | |
| 809 | static inline u32 its_get_event_id(struct irq_data *d) |
| 810 | { |
| 811 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 812 | return d->hwirq - its_dev->event_map.lpi_base; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 815 | static void lpi_update_config(struct irq_data *d, u8 clr, u8 set) |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 816 | { |
| 817 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 818 | irq_hw_number_t hwirq = d->hwirq; |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 819 | struct page *prop_page; |
| 820 | u8 *cfg; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 821 | |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 822 | prop_page = gic_rdists->prop_page; |
| 823 | |
| 824 | cfg = page_address(prop_page) + hwirq - 8192; |
| 825 | *cfg &= ~clr; |
| 826 | *cfg |= set; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 827 | |
| 828 | /* |
| 829 | * Make the above write visible to the redistributors. |
| 830 | * And yes, we're flushing exactly: One. Single. Byte. |
| 831 | * Humpf... |
| 832 | */ |
| 833 | if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 834 | gic_flush_dcache_to_poc(cfg, sizeof(*cfg)); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 835 | else |
| 836 | dsb(ishst); |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 837 | its_send_inv(its_dev, its_get_event_id(d)); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | static void its_mask_irq(struct irq_data *d) |
| 841 | { |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 842 | lpi_update_config(d, LPI_PROP_ENABLED, 0); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 843 | } |
| 844 | |
| 845 | static void its_unmask_irq(struct irq_data *d) |
| 846 | { |
Marc Zyngier | adcdb94 | 2016-12-19 19:18:13 +0000 | [diff] [blame] | 847 | lpi_update_config(d, 0, LPI_PROP_ENABLED); |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 850 | static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val, |
| 851 | bool force) |
| 852 | { |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 853 | unsigned int cpu; |
| 854 | const struct cpumask *cpu_mask = cpu_online_mask; |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 855 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 856 | struct its_collection *target_col; |
| 857 | u32 id = its_get_event_id(d); |
| 858 | |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 859 | /* lpi cannot be routed to a redistributor that is on a foreign node */ |
| 860 | if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) { |
| 861 | if (its_dev->its->numa_node >= 0) { |
| 862 | cpu_mask = cpumask_of_node(its_dev->its->numa_node); |
| 863 | if (!cpumask_intersects(mask_val, cpu_mask)) |
| 864 | return -EINVAL; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | cpu = cpumask_any_and(mask_val, cpu_mask); |
| 869 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 870 | if (cpu >= nr_cpu_ids) |
| 871 | return -EINVAL; |
| 872 | |
MaJun | 8b8d94a | 2017-05-18 16:19:13 +0800 | [diff] [blame] | 873 | /* don't set the affinity when the target cpu is same as current one */ |
| 874 | if (cpu != its_dev->event_map.col_map[id]) { |
| 875 | target_col = &its_dev->its->collections[cpu]; |
| 876 | its_send_movi(its_dev, target_col, id); |
| 877 | its_dev->event_map.col_map[id] = cpu; |
| 878 | } |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 879 | |
| 880 | return IRQ_SET_MASK_OK_DONE; |
| 881 | } |
| 882 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 883 | static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg) |
| 884 | { |
| 885 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 886 | struct its_node *its; |
| 887 | u64 addr; |
| 888 | |
| 889 | its = its_dev->its; |
| 890 | addr = its->phys_base + GITS_TRANSLATER; |
| 891 | |
Vladimir Murzin | b11283e | 2016-11-02 11:54:03 +0000 | [diff] [blame] | 892 | msg->address_lo = lower_32_bits(addr); |
| 893 | msg->address_hi = upper_32_bits(addr); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 894 | msg->data = its_get_event_id(d); |
Robin Murphy | 44bb7e2 | 2016-09-12 17:13:59 +0100 | [diff] [blame] | 895 | |
| 896 | iommu_dma_map_msi_msg(d->irq, msg); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 897 | } |
| 898 | |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 899 | static int its_irq_set_irqchip_state(struct irq_data *d, |
| 900 | enum irqchip_irq_state which, |
| 901 | bool state) |
| 902 | { |
| 903 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 904 | u32 event = its_get_event_id(d); |
| 905 | |
| 906 | if (which != IRQCHIP_STATE_PENDING) |
| 907 | return -EINVAL; |
| 908 | |
| 909 | if (state) |
| 910 | its_send_int(its_dev, event); |
| 911 | else |
| 912 | its_send_clear(its_dev, event); |
| 913 | |
| 914 | return 0; |
| 915 | } |
| 916 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame^] | 917 | static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info) |
| 918 | { |
| 919 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 920 | u32 event = its_get_event_id(d); |
| 921 | int ret = 0; |
| 922 | |
| 923 | if (!info->map) |
| 924 | return -EINVAL; |
| 925 | |
| 926 | mutex_lock(&its_dev->event_map.vlpi_lock); |
| 927 | |
| 928 | if (!its_dev->event_map.vm) { |
| 929 | struct its_vlpi_map *maps; |
| 930 | |
| 931 | maps = kzalloc(sizeof(*maps) * its_dev->event_map.nr_lpis, |
| 932 | GFP_KERNEL); |
| 933 | if (!maps) { |
| 934 | ret = -ENOMEM; |
| 935 | goto out; |
| 936 | } |
| 937 | |
| 938 | its_dev->event_map.vm = info->map->vm; |
| 939 | its_dev->event_map.vlpi_maps = maps; |
| 940 | } else if (its_dev->event_map.vm != info->map->vm) { |
| 941 | ret = -EINVAL; |
| 942 | goto out; |
| 943 | } |
| 944 | |
| 945 | /* Get our private copy of the mapping information */ |
| 946 | its_dev->event_map.vlpi_maps[event] = *info->map; |
| 947 | |
| 948 | if (irqd_is_forwarded_to_vcpu(d)) { |
| 949 | /* Already mapped, move it around */ |
| 950 | its_send_vmovi(its_dev, event); |
| 951 | } else { |
| 952 | /* Drop the physical mapping */ |
| 953 | its_send_discard(its_dev, event); |
| 954 | |
| 955 | /* and install the virtual one */ |
| 956 | its_send_vmapti(its_dev, event); |
| 957 | irqd_set_forwarded_to_vcpu(d); |
| 958 | |
| 959 | /* Increment the number of VLPIs */ |
| 960 | its_dev->event_map.nr_vlpis++; |
| 961 | } |
| 962 | |
| 963 | out: |
| 964 | mutex_unlock(&its_dev->event_map.vlpi_lock); |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info) |
| 969 | { |
| 970 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 971 | u32 event = its_get_event_id(d); |
| 972 | int ret = 0; |
| 973 | |
| 974 | mutex_lock(&its_dev->event_map.vlpi_lock); |
| 975 | |
| 976 | if (!its_dev->event_map.vm || |
| 977 | !its_dev->event_map.vlpi_maps[event].vm) { |
| 978 | ret = -EINVAL; |
| 979 | goto out; |
| 980 | } |
| 981 | |
| 982 | /* Copy our mapping information to the incoming request */ |
| 983 | *info->map = its_dev->event_map.vlpi_maps[event]; |
| 984 | |
| 985 | out: |
| 986 | mutex_unlock(&its_dev->event_map.vlpi_lock); |
| 987 | return ret; |
| 988 | } |
| 989 | |
| 990 | static int its_vlpi_unmap(struct irq_data *d) |
| 991 | { |
| 992 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 993 | u32 event = its_get_event_id(d); |
| 994 | int ret = 0; |
| 995 | |
| 996 | mutex_lock(&its_dev->event_map.vlpi_lock); |
| 997 | |
| 998 | if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) { |
| 999 | ret = -EINVAL; |
| 1000 | goto out; |
| 1001 | } |
| 1002 | |
| 1003 | /* Drop the virtual mapping */ |
| 1004 | its_send_discard(its_dev, event); |
| 1005 | |
| 1006 | /* and restore the physical one */ |
| 1007 | irqd_clr_forwarded_to_vcpu(d); |
| 1008 | its_send_mapti(its_dev, d->hwirq, event); |
| 1009 | lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO | |
| 1010 | LPI_PROP_ENABLED | |
| 1011 | LPI_PROP_GROUP1)); |
| 1012 | |
| 1013 | /* |
| 1014 | * Drop the refcount and make the device available again if |
| 1015 | * this was the last VLPI. |
| 1016 | */ |
| 1017 | if (!--its_dev->event_map.nr_vlpis) { |
| 1018 | its_dev->event_map.vm = NULL; |
| 1019 | kfree(its_dev->event_map.vlpi_maps); |
| 1020 | } |
| 1021 | |
| 1022 | out: |
| 1023 | mutex_unlock(&its_dev->event_map.vlpi_lock); |
| 1024 | return ret; |
| 1025 | } |
| 1026 | |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1027 | static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info) |
| 1028 | { |
| 1029 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1030 | struct its_cmd_info *info = vcpu_info; |
| 1031 | |
| 1032 | /* Need a v4 ITS */ |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame^] | 1033 | if (!its_dev->its->is_v4) |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1034 | return -EINVAL; |
| 1035 | |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame^] | 1036 | /* Unmap request? */ |
| 1037 | if (!info) |
| 1038 | return its_vlpi_unmap(d); |
| 1039 | |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1040 | switch (info->cmd_type) { |
| 1041 | case MAP_VLPI: |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame^] | 1042 | return its_vlpi_map(d, info); |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1043 | |
| 1044 | case GET_VLPI: |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame^] | 1045 | return its_vlpi_get(d, info); |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1046 | |
| 1047 | case PROP_UPDATE_VLPI: |
| 1048 | case PROP_UPDATE_AND_INV_VLPI: |
| 1049 | |
| 1050 | default: |
| 1051 | return -EINVAL; |
| 1052 | } |
| 1053 | } |
| 1054 | |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1055 | static struct irq_chip its_irq_chip = { |
| 1056 | .name = "ITS", |
| 1057 | .irq_mask = its_mask_irq, |
| 1058 | .irq_unmask = its_unmask_irq, |
Ashok Kumar | 004fa08 | 2016-02-11 05:38:53 -0800 | [diff] [blame] | 1059 | .irq_eoi = irq_chip_eoi_parent, |
Marc Zyngier | c48ed51 | 2014-11-24 14:35:12 +0000 | [diff] [blame] | 1060 | .irq_set_affinity = its_set_affinity, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1061 | .irq_compose_msi_msg = its_irq_compose_msi_msg, |
Marc Zyngier | 8d85dce | 2016-12-19 18:02:13 +0000 | [diff] [blame] | 1062 | .irq_set_irqchip_state = its_irq_set_irqchip_state, |
Marc Zyngier | c808eea | 2016-12-20 09:31:20 +0000 | [diff] [blame] | 1063 | .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1064 | }; |
| 1065 | |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1066 | /* |
| 1067 | * How we allocate LPIs: |
| 1068 | * |
| 1069 | * The GIC has id_bits bits for interrupt identifiers. From there, we |
| 1070 | * must subtract 8192 which are reserved for SGIs/PPIs/SPIs. Then, as |
| 1071 | * we allocate LPIs by chunks of 32, we can shift the whole thing by 5 |
| 1072 | * bits to the right. |
| 1073 | * |
| 1074 | * This gives us (((1UL << id_bits) - 8192) >> 5) possible allocations. |
| 1075 | */ |
| 1076 | #define IRQS_PER_CHUNK_SHIFT 5 |
| 1077 | #define IRQS_PER_CHUNK (1 << IRQS_PER_CHUNK_SHIFT) |
Shanker Donthineni | 6c31e12 | 2017-06-22 18:19:14 -0500 | [diff] [blame] | 1078 | #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */ |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1079 | |
| 1080 | static unsigned long *lpi_bitmap; |
| 1081 | static u32 lpi_chunks; |
| 1082 | static DEFINE_SPINLOCK(lpi_lock); |
| 1083 | |
| 1084 | static int its_lpi_to_chunk(int lpi) |
| 1085 | { |
| 1086 | return (lpi - 8192) >> IRQS_PER_CHUNK_SHIFT; |
| 1087 | } |
| 1088 | |
| 1089 | static int its_chunk_to_lpi(int chunk) |
| 1090 | { |
| 1091 | return (chunk << IRQS_PER_CHUNK_SHIFT) + 8192; |
| 1092 | } |
| 1093 | |
Tomasz Nowicki | 04a0e4d | 2016-01-19 14:11:18 +0100 | [diff] [blame] | 1094 | static int __init its_lpi_init(u32 id_bits) |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1095 | { |
| 1096 | lpi_chunks = its_lpi_to_chunk(1UL << id_bits); |
| 1097 | |
| 1098 | lpi_bitmap = kzalloc(BITS_TO_LONGS(lpi_chunks) * sizeof(long), |
| 1099 | GFP_KERNEL); |
| 1100 | if (!lpi_bitmap) { |
| 1101 | lpi_chunks = 0; |
| 1102 | return -ENOMEM; |
| 1103 | } |
| 1104 | |
| 1105 | pr_info("ITS: Allocated %d chunks for LPIs\n", (int)lpi_chunks); |
| 1106 | return 0; |
| 1107 | } |
| 1108 | |
| 1109 | static unsigned long *its_lpi_alloc_chunks(int nr_irqs, int *base, int *nr_ids) |
| 1110 | { |
| 1111 | unsigned long *bitmap = NULL; |
| 1112 | int chunk_id; |
| 1113 | int nr_chunks; |
| 1114 | int i; |
| 1115 | |
| 1116 | nr_chunks = DIV_ROUND_UP(nr_irqs, IRQS_PER_CHUNK); |
| 1117 | |
| 1118 | spin_lock(&lpi_lock); |
| 1119 | |
| 1120 | do { |
| 1121 | chunk_id = bitmap_find_next_zero_area(lpi_bitmap, lpi_chunks, |
| 1122 | 0, nr_chunks, 0); |
| 1123 | if (chunk_id < lpi_chunks) |
| 1124 | break; |
| 1125 | |
| 1126 | nr_chunks--; |
| 1127 | } while (nr_chunks > 0); |
| 1128 | |
| 1129 | if (!nr_chunks) |
| 1130 | goto out; |
| 1131 | |
| 1132 | bitmap = kzalloc(BITS_TO_LONGS(nr_chunks * IRQS_PER_CHUNK) * sizeof (long), |
| 1133 | GFP_ATOMIC); |
| 1134 | if (!bitmap) |
| 1135 | goto out; |
| 1136 | |
| 1137 | for (i = 0; i < nr_chunks; i++) |
| 1138 | set_bit(chunk_id + i, lpi_bitmap); |
| 1139 | |
| 1140 | *base = its_chunk_to_lpi(chunk_id); |
| 1141 | *nr_ids = nr_chunks * IRQS_PER_CHUNK; |
| 1142 | |
| 1143 | out: |
| 1144 | spin_unlock(&lpi_lock); |
| 1145 | |
Marc Zyngier | c8415b9 | 2015-10-02 16:44:05 +0100 | [diff] [blame] | 1146 | if (!bitmap) |
| 1147 | *base = *nr_ids = 0; |
| 1148 | |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1149 | return bitmap; |
| 1150 | } |
| 1151 | |
Marc Zyngier | cf2be8b | 2016-12-19 18:49:59 +0000 | [diff] [blame] | 1152 | 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] | 1153 | { |
| 1154 | int lpi; |
| 1155 | |
| 1156 | spin_lock(&lpi_lock); |
| 1157 | |
| 1158 | for (lpi = base; lpi < (base + nr_ids); lpi += IRQS_PER_CHUNK) { |
| 1159 | int chunk = its_lpi_to_chunk(lpi); |
Marc Zyngier | cf2be8b | 2016-12-19 18:49:59 +0000 | [diff] [blame] | 1160 | |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1161 | BUG_ON(chunk > lpi_chunks); |
| 1162 | if (test_bit(chunk, lpi_bitmap)) { |
| 1163 | clear_bit(chunk, lpi_bitmap); |
| 1164 | } else { |
| 1165 | pr_err("Bad LPI chunk %d\n", chunk); |
| 1166 | } |
| 1167 | } |
| 1168 | |
| 1169 | spin_unlock(&lpi_lock); |
| 1170 | |
Marc Zyngier | cf2be8b | 2016-12-19 18:49:59 +0000 | [diff] [blame] | 1171 | kfree(bitmap); |
Marc Zyngier | bf9529f | 2014-11-24 14:35:13 +0000 | [diff] [blame] | 1172 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1173 | |
Marc Zyngier | 0e5ccf9 | 2016-12-19 18:15:05 +0000 | [diff] [blame] | 1174 | static struct page *its_allocate_prop_table(gfp_t gfp_flags) |
| 1175 | { |
| 1176 | struct page *prop_page; |
| 1177 | |
| 1178 | prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ)); |
| 1179 | if (!prop_page) |
| 1180 | return NULL; |
| 1181 | |
| 1182 | /* Priority 0xa0, Group-1, disabled */ |
| 1183 | memset(page_address(prop_page), |
| 1184 | LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, |
| 1185 | LPI_PROPBASE_SZ); |
| 1186 | |
| 1187 | /* Make sure the GIC will observe the written configuration */ |
| 1188 | gic_flush_dcache_to_poc(page_address(prop_page), LPI_PROPBASE_SZ); |
| 1189 | |
| 1190 | return prop_page; |
| 1191 | } |
| 1192 | |
| 1193 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1194 | static int __init its_alloc_lpi_tables(void) |
| 1195 | { |
| 1196 | phys_addr_t paddr; |
| 1197 | |
Shanker Donthineni | 6c31e12 | 2017-06-22 18:19:14 -0500 | [diff] [blame] | 1198 | 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] | 1199 | gic_rdists->prop_page = its_allocate_prop_table(GFP_NOWAIT); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1200 | if (!gic_rdists->prop_page) { |
| 1201 | pr_err("Failed to allocate PROPBASE\n"); |
| 1202 | return -ENOMEM; |
| 1203 | } |
| 1204 | |
| 1205 | paddr = page_to_phys(gic_rdists->prop_page); |
| 1206 | pr_info("GIC: using LPI property table @%pa\n", &paddr); |
| 1207 | |
Shanker Donthineni | 6c31e12 | 2017-06-22 18:19:14 -0500 | [diff] [blame] | 1208 | return its_lpi_init(lpi_id_bits); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | static const char *its_base_type_string[] = { |
| 1212 | [GITS_BASER_TYPE_DEVICE] = "Devices", |
| 1213 | [GITS_BASER_TYPE_VCPU] = "Virtual CPUs", |
Marc Zyngier | 4f46de9 | 2016-12-20 15:50:14 +0000 | [diff] [blame] | 1214 | [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)", |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1215 | [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections", |
| 1216 | [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)", |
| 1217 | [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)", |
| 1218 | [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)", |
| 1219 | }; |
| 1220 | |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 1221 | static u64 its_read_baser(struct its_node *its, struct its_baser *baser) |
| 1222 | { |
| 1223 | u32 idx = baser - its->tables; |
| 1224 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1225 | return gits_read_baser(its->base + GITS_BASER + (idx << 3)); |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | static void its_write_baser(struct its_node *its, struct its_baser *baser, |
| 1229 | u64 val) |
| 1230 | { |
| 1231 | u32 idx = baser - its->tables; |
| 1232 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1233 | gits_write_baser(val, its->base + GITS_BASER + (idx << 3)); |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 1234 | baser->val = its_read_baser(its, baser); |
| 1235 | } |
| 1236 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1237 | 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] | 1238 | u64 cache, u64 shr, u32 psz, u32 order, |
| 1239 | bool indirect) |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1240 | { |
| 1241 | u64 val = its_read_baser(its, baser); |
| 1242 | u64 esz = GITS_BASER_ENTRY_SIZE(val); |
| 1243 | u64 type = GITS_BASER_TYPE(val); |
| 1244 | u32 alloc_pages; |
| 1245 | void *base; |
| 1246 | u64 tmp; |
| 1247 | |
| 1248 | retry_alloc_baser: |
| 1249 | alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz); |
| 1250 | if (alloc_pages > GITS_BASER_PAGES_MAX) { |
| 1251 | pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n", |
| 1252 | &its->phys_base, its_base_type_string[type], |
| 1253 | alloc_pages, GITS_BASER_PAGES_MAX); |
| 1254 | alloc_pages = GITS_BASER_PAGES_MAX; |
| 1255 | order = get_order(GITS_BASER_PAGES_MAX * psz); |
| 1256 | } |
| 1257 | |
| 1258 | base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order); |
| 1259 | if (!base) |
| 1260 | return -ENOMEM; |
| 1261 | |
| 1262 | retry_baser: |
| 1263 | val = (virt_to_phys(base) | |
| 1264 | (type << GITS_BASER_TYPE_SHIFT) | |
| 1265 | ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) | |
| 1266 | ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) | |
| 1267 | cache | |
| 1268 | shr | |
| 1269 | GITS_BASER_VALID); |
| 1270 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1271 | val |= indirect ? GITS_BASER_INDIRECT : 0x0; |
| 1272 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1273 | switch (psz) { |
| 1274 | case SZ_4K: |
| 1275 | val |= GITS_BASER_PAGE_SIZE_4K; |
| 1276 | break; |
| 1277 | case SZ_16K: |
| 1278 | val |= GITS_BASER_PAGE_SIZE_16K; |
| 1279 | break; |
| 1280 | case SZ_64K: |
| 1281 | val |= GITS_BASER_PAGE_SIZE_64K; |
| 1282 | break; |
| 1283 | } |
| 1284 | |
| 1285 | its_write_baser(its, baser, val); |
| 1286 | tmp = baser->val; |
| 1287 | |
| 1288 | if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) { |
| 1289 | /* |
| 1290 | * Shareability didn't stick. Just use |
| 1291 | * whatever the read reported, which is likely |
| 1292 | * to be the only thing this redistributor |
| 1293 | * supports. If that's zero, make it |
| 1294 | * non-cacheable as well. |
| 1295 | */ |
| 1296 | shr = tmp & GITS_BASER_SHAREABILITY_MASK; |
| 1297 | if (!shr) { |
| 1298 | cache = GITS_BASER_nC; |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 1299 | gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order)); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1300 | } |
| 1301 | goto retry_baser; |
| 1302 | } |
| 1303 | |
| 1304 | if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) { |
| 1305 | /* |
| 1306 | * Page size didn't stick. Let's try a smaller |
| 1307 | * size and retry. If we reach 4K, then |
| 1308 | * something is horribly wrong... |
| 1309 | */ |
| 1310 | free_pages((unsigned long)base, order); |
| 1311 | baser->base = NULL; |
| 1312 | |
| 1313 | switch (psz) { |
| 1314 | case SZ_16K: |
| 1315 | psz = SZ_4K; |
| 1316 | goto retry_alloc_baser; |
| 1317 | case SZ_64K: |
| 1318 | psz = SZ_16K; |
| 1319 | goto retry_alloc_baser; |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | if (val != tmp) { |
Vladimir Murzin | b11283e | 2016-11-02 11:54:03 +0000 | [diff] [blame] | 1324 | pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n", |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1325 | &its->phys_base, its_base_type_string[type], |
Vladimir Murzin | b11283e | 2016-11-02 11:54:03 +0000 | [diff] [blame] | 1326 | val, tmp); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1327 | free_pages((unsigned long)base, order); |
| 1328 | return -ENXIO; |
| 1329 | } |
| 1330 | |
| 1331 | baser->order = order; |
| 1332 | baser->base = base; |
| 1333 | baser->psz = psz; |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1334 | tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1335 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1336 | 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] | 1337 | &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp), |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1338 | its_base_type_string[type], |
| 1339 | (unsigned long)virt_to_phys(base), |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1340 | indirect ? "indirect" : "flat", (int)esz, |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1341 | psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT); |
| 1342 | |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1346 | static bool its_parse_indirect_baser(struct its_node *its, |
| 1347 | struct its_baser *baser, |
| 1348 | u32 psz, u32 *order) |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1349 | { |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1350 | u64 tmp = its_read_baser(its, baser); |
| 1351 | u64 type = GITS_BASER_TYPE(tmp); |
| 1352 | u64 esz = GITS_BASER_ENTRY_SIZE(tmp); |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 1353 | u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb; |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1354 | u32 ids = its->device_ids; |
| 1355 | u32 new_order = *order; |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1356 | bool indirect = false; |
| 1357 | |
| 1358 | /* No need to enable Indirection if memory requirement < (psz*2)bytes */ |
| 1359 | if ((esz << ids) > (psz * 2)) { |
| 1360 | /* |
| 1361 | * Find out whether hw supports a single or two-level table by |
| 1362 | * table by reading bit at offset '62' after writing '1' to it. |
| 1363 | */ |
| 1364 | its_write_baser(its, baser, val | GITS_BASER_INDIRECT); |
| 1365 | indirect = !!(baser->val & GITS_BASER_INDIRECT); |
| 1366 | |
| 1367 | if (indirect) { |
| 1368 | /* |
| 1369 | * The size of the lvl2 table is equal to ITS page size |
| 1370 | * which is 'psz'. For computing lvl1 table size, |
| 1371 | * subtract ID bits that sparse lvl2 table from 'ids' |
| 1372 | * which is reported by ITS hardware times lvl1 table |
| 1373 | * entry size. |
| 1374 | */ |
Vladimir Murzin | d524eaa | 2016-11-02 11:54:04 +0000 | [diff] [blame] | 1375 | ids -= ilog2(psz / (int)esz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1376 | esz = GITS_LVL1_ENTRY_SIZE; |
| 1377 | } |
| 1378 | } |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1379 | |
| 1380 | /* |
| 1381 | * Allocate as many entries as required to fit the |
| 1382 | * range of device IDs that the ITS can grok... The ID |
| 1383 | * space being incredibly sparse, this results in a |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1384 | * massive waste of memory if two-level device table |
| 1385 | * feature is not supported by hardware. |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1386 | */ |
| 1387 | new_order = max_t(u32, get_order(esz << ids), new_order); |
| 1388 | if (new_order >= MAX_ORDER) { |
| 1389 | new_order = MAX_ORDER - 1; |
Vladimir Murzin | d524eaa | 2016-11-02 11:54:04 +0000 | [diff] [blame] | 1390 | ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz); |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1391 | pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n", |
| 1392 | &its->phys_base, its_base_type_string[type], |
| 1393 | its->device_ids, ids); |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1394 | } |
| 1395 | |
| 1396 | *order = new_order; |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1397 | |
| 1398 | return indirect; |
Shanker Donthineni | 4b75c45 | 2016-06-06 18:17:29 -0500 | [diff] [blame] | 1399 | } |
| 1400 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1401 | static void its_free_tables(struct its_node *its) |
| 1402 | { |
| 1403 | int i; |
| 1404 | |
| 1405 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
Shanker Donthineni | 1a485f4 | 2016-02-01 20:19:44 -0600 | [diff] [blame] | 1406 | if (its->tables[i].base) { |
| 1407 | free_pages((unsigned long)its->tables[i].base, |
| 1408 | its->tables[i].order); |
| 1409 | its->tables[i].base = NULL; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | } |
| 1413 | |
Shanker Donthineni | 0e0b0f6 | 2016-06-06 18:17:31 -0500 | [diff] [blame] | 1414 | static int its_alloc_tables(struct its_node *its) |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1415 | { |
Marc Zyngier | 589ce5f | 2016-10-14 15:13:07 +0100 | [diff] [blame] | 1416 | u64 typer = gic_read_typer(its->base + GITS_TYPER); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1417 | u32 ids = GITS_TYPER_DEVBITS(typer); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1418 | u64 shr = GITS_BASER_InnerShareable; |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 1419 | u64 cache = GITS_BASER_RaWaWb; |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1420 | u32 psz = SZ_64K; |
| 1421 | int err, i; |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 1422 | |
| 1423 | if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375) { |
| 1424 | /* |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1425 | * erratum 22375: only alloc 8MB table size |
| 1426 | * erratum 24313: ignore memory access type |
| 1427 | */ |
| 1428 | cache = GITS_BASER_nCnB; |
| 1429 | ids = 0x14; /* 20 bits, 8MB */ |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 1430 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1431 | |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 1432 | its->device_ids = ids; |
| 1433 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1434 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
Shanker Donthineni | 2d81d42 | 2016-06-06 18:17:28 -0500 | [diff] [blame] | 1435 | struct its_baser *baser = its->tables + i; |
| 1436 | u64 val = its_read_baser(its, baser); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1437 | u64 type = GITS_BASER_TYPE(val); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1438 | u32 order = get_order(psz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1439 | bool indirect = false; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1440 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1441 | switch (type) { |
| 1442 | case GITS_BASER_TYPE_NONE: |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1443 | continue; |
| 1444 | |
Marc Zyngier | 4cacac5 | 2016-12-19 18:18:34 +0000 | [diff] [blame] | 1445 | case GITS_BASER_TYPE_DEVICE: |
| 1446 | case GITS_BASER_TYPE_VCPU: |
| 1447 | indirect = its_parse_indirect_baser(its, baser, |
| 1448 | psz, &order); |
| 1449 | break; |
| 1450 | } |
Marc Zyngier | f54b97e | 2015-03-06 16:37:41 +0000 | [diff] [blame] | 1451 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1452 | err = its_setup_baser(its, baser, cache, shr, psz, order, indirect); |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1453 | if (err < 0) { |
| 1454 | its_free_tables(its); |
| 1455 | return err; |
Robert Richter | 30f2136 | 2015-09-21 22:58:34 +0200 | [diff] [blame] | 1456 | } |
| 1457 | |
Shanker Donthineni | 9347359 | 2016-06-06 18:17:30 -0500 | [diff] [blame] | 1458 | /* Update settings which will be used for next BASERn */ |
| 1459 | psz = baser->psz; |
| 1460 | cache = baser->val & GITS_BASER_CACHEABILITY_MASK; |
| 1461 | shr = baser->val & GITS_BASER_SHAREABILITY_MASK; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
| 1464 | return 0; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | static int its_alloc_collections(struct its_node *its) |
| 1468 | { |
| 1469 | its->collections = kzalloc(nr_cpu_ids * sizeof(*its->collections), |
| 1470 | GFP_KERNEL); |
| 1471 | if (!its->collections) |
| 1472 | return -ENOMEM; |
| 1473 | |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
Marc Zyngier | 7c297a2 | 2016-12-19 18:34:38 +0000 | [diff] [blame] | 1477 | static struct page *its_allocate_pending_table(gfp_t gfp_flags) |
| 1478 | { |
| 1479 | struct page *pend_page; |
| 1480 | /* |
| 1481 | * The pending pages have to be at least 64kB aligned, |
| 1482 | * hence the 'max(LPI_PENDBASE_SZ, SZ_64K)' below. |
| 1483 | */ |
| 1484 | pend_page = alloc_pages(gfp_flags | __GFP_ZERO, |
| 1485 | get_order(max_t(u32, LPI_PENDBASE_SZ, SZ_64K))); |
| 1486 | if (!pend_page) |
| 1487 | return NULL; |
| 1488 | |
| 1489 | /* Make sure the GIC will observe the zero-ed page */ |
| 1490 | gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ); |
| 1491 | |
| 1492 | return pend_page; |
| 1493 | } |
| 1494 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1495 | static void its_cpu_init_lpis(void) |
| 1496 | { |
| 1497 | void __iomem *rbase = gic_data_rdist_rd_base(); |
| 1498 | struct page *pend_page; |
| 1499 | u64 val, tmp; |
| 1500 | |
| 1501 | /* If we didn't allocate the pending table yet, do it now */ |
| 1502 | pend_page = gic_data_rdist()->pend_page; |
| 1503 | if (!pend_page) { |
| 1504 | phys_addr_t paddr; |
Marc Zyngier | 7c297a2 | 2016-12-19 18:34:38 +0000 | [diff] [blame] | 1505 | |
| 1506 | pend_page = its_allocate_pending_table(GFP_NOWAIT); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1507 | if (!pend_page) { |
| 1508 | pr_err("Failed to allocate PENDBASE for CPU%d\n", |
| 1509 | smp_processor_id()); |
| 1510 | return; |
| 1511 | } |
| 1512 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1513 | paddr = page_to_phys(pend_page); |
| 1514 | pr_info("CPU%d: using LPI pending table @%pa\n", |
| 1515 | smp_processor_id(), &paddr); |
| 1516 | gic_data_rdist()->pend_page = pend_page; |
| 1517 | } |
| 1518 | |
| 1519 | /* Disable LPIs */ |
| 1520 | val = readl_relaxed(rbase + GICR_CTLR); |
| 1521 | val &= ~GICR_CTLR_ENABLE_LPIS; |
| 1522 | writel_relaxed(val, rbase + GICR_CTLR); |
| 1523 | |
| 1524 | /* |
| 1525 | * Make sure any change to the table is observable by the GIC. |
| 1526 | */ |
| 1527 | dsb(sy); |
| 1528 | |
| 1529 | /* set PROPBASE */ |
| 1530 | val = (page_to_phys(gic_rdists->prop_page) | |
| 1531 | GICR_PROPBASER_InnerShareable | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 1532 | GICR_PROPBASER_RaWaWb | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1533 | ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK)); |
| 1534 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1535 | gicr_write_propbaser(val, rbase + GICR_PROPBASER); |
| 1536 | tmp = gicr_read_propbaser(rbase + GICR_PROPBASER); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1537 | |
| 1538 | if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) { |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 1539 | if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) { |
| 1540 | /* |
| 1541 | * The HW reports non-shareable, we must |
| 1542 | * remove the cacheability attributes as |
| 1543 | * well. |
| 1544 | */ |
| 1545 | val &= ~(GICR_PROPBASER_SHAREABILITY_MASK | |
| 1546 | GICR_PROPBASER_CACHEABILITY_MASK); |
| 1547 | val |= GICR_PROPBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1548 | gicr_write_propbaser(val, rbase + GICR_PROPBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 1549 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1550 | pr_info_once("GIC: using cache flushing for LPI property table\n"); |
| 1551 | gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING; |
| 1552 | } |
| 1553 | |
| 1554 | /* set PENDBASE */ |
| 1555 | val = (page_to_phys(pend_page) | |
Marc Zyngier | 4ad3e36 | 2015-03-27 14:15:04 +0000 | [diff] [blame] | 1556 | GICR_PENDBASER_InnerShareable | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 1557 | GICR_PENDBASER_RaWaWb); |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1558 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1559 | gicr_write_pendbaser(val, rbase + GICR_PENDBASER); |
| 1560 | tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 1561 | |
| 1562 | if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) { |
| 1563 | /* |
| 1564 | * The HW reports non-shareable, we must remove the |
| 1565 | * cacheability attributes as well. |
| 1566 | */ |
| 1567 | val &= ~(GICR_PENDBASER_SHAREABILITY_MASK | |
| 1568 | GICR_PENDBASER_CACHEABILITY_MASK); |
| 1569 | val |= GICR_PENDBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 1570 | gicr_write_pendbaser(val, rbase + GICR_PENDBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 1571 | } |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1572 | |
| 1573 | /* Enable LPIs */ |
| 1574 | val = readl_relaxed(rbase + GICR_CTLR); |
| 1575 | val |= GICR_CTLR_ENABLE_LPIS; |
| 1576 | writel_relaxed(val, rbase + GICR_CTLR); |
| 1577 | |
| 1578 | /* Make sure the GIC has seen the above */ |
| 1579 | dsb(sy); |
| 1580 | } |
| 1581 | |
| 1582 | static void its_cpu_init_collection(void) |
| 1583 | { |
| 1584 | struct its_node *its; |
| 1585 | int cpu; |
| 1586 | |
| 1587 | spin_lock(&its_lock); |
| 1588 | cpu = smp_processor_id(); |
| 1589 | |
| 1590 | list_for_each_entry(its, &its_nodes, entry) { |
| 1591 | u64 target; |
| 1592 | |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 1593 | /* avoid cross node collections and its mapping */ |
| 1594 | if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) { |
| 1595 | struct device_node *cpu_node; |
| 1596 | |
| 1597 | cpu_node = of_get_cpu_node(cpu, NULL); |
| 1598 | if (its->numa_node != NUMA_NO_NODE && |
| 1599 | its->numa_node != of_node_to_nid(cpu_node)) |
| 1600 | continue; |
| 1601 | } |
| 1602 | |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1603 | /* |
| 1604 | * We now have to bind each collection to its target |
| 1605 | * redistributor. |
| 1606 | */ |
Marc Zyngier | 589ce5f | 2016-10-14 15:13:07 +0100 | [diff] [blame] | 1607 | if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) { |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1608 | /* |
| 1609 | * This ITS wants the physical address of the |
| 1610 | * redistributor. |
| 1611 | */ |
| 1612 | target = gic_data_rdist()->phys_base; |
| 1613 | } else { |
| 1614 | /* |
| 1615 | * This ITS wants a linear CPU number. |
| 1616 | */ |
Marc Zyngier | 589ce5f | 2016-10-14 15:13:07 +0100 | [diff] [blame] | 1617 | target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER); |
Marc Zyngier | 263fcd3 | 2015-03-27 14:15:02 +0000 | [diff] [blame] | 1618 | target = GICR_TYPER_CPU_NUMBER(target) << 16; |
Marc Zyngier | 1ac19ca | 2014-11-24 14:35:14 +0000 | [diff] [blame] | 1619 | } |
| 1620 | |
| 1621 | /* Perform collection mapping */ |
| 1622 | its->collections[cpu].target_address = target; |
| 1623 | its->collections[cpu].col_id = cpu; |
| 1624 | |
| 1625 | its_send_mapc(its, &its->collections[cpu], 1); |
| 1626 | its_send_invall(its, &its->collections[cpu]); |
| 1627 | } |
| 1628 | |
| 1629 | spin_unlock(&its_lock); |
| 1630 | } |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1631 | |
| 1632 | static struct its_device *its_find_device(struct its_node *its, u32 dev_id) |
| 1633 | { |
| 1634 | struct its_device *its_dev = NULL, *tmp; |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 1635 | unsigned long flags; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1636 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 1637 | raw_spin_lock_irqsave(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1638 | |
| 1639 | list_for_each_entry(tmp, &its->its_device_list, entry) { |
| 1640 | if (tmp->device_id == dev_id) { |
| 1641 | its_dev = tmp; |
| 1642 | break; |
| 1643 | } |
| 1644 | } |
| 1645 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 1646 | raw_spin_unlock_irqrestore(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1647 | |
| 1648 | return its_dev; |
| 1649 | } |
| 1650 | |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 1651 | static struct its_baser *its_get_baser(struct its_node *its, u32 type) |
| 1652 | { |
| 1653 | int i; |
| 1654 | |
| 1655 | for (i = 0; i < GITS_BASER_NR_REGS; i++) { |
| 1656 | if (GITS_BASER_TYPE(its->tables[i].val) == type) |
| 1657 | return &its->tables[i]; |
| 1658 | } |
| 1659 | |
| 1660 | return NULL; |
| 1661 | } |
| 1662 | |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 1663 | static bool its_alloc_table_entry(struct its_baser *baser, u32 id) |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1664 | { |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1665 | struct page *page; |
| 1666 | u32 esz, idx; |
| 1667 | __le64 *table; |
| 1668 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1669 | /* Don't allow device id that exceeds single, flat table limit */ |
| 1670 | esz = GITS_BASER_ENTRY_SIZE(baser->val); |
| 1671 | if (!(baser->val & GITS_BASER_INDIRECT)) |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 1672 | return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz)); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1673 | |
| 1674 | /* Compute 1st level table index & check if that exceeds table limit */ |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 1675 | idx = id >> ilog2(baser->psz / esz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1676 | if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE)) |
| 1677 | return false; |
| 1678 | |
| 1679 | table = baser->base; |
| 1680 | |
| 1681 | /* Allocate memory for 2nd level table */ |
| 1682 | if (!table[idx]) { |
| 1683 | page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz)); |
| 1684 | if (!page) |
| 1685 | return false; |
| 1686 | |
| 1687 | /* Flush Lvl2 table to PoC if hw doesn't support coherency */ |
| 1688 | if (!(baser->val & GITS_BASER_SHAREABILITY_MASK)) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 1689 | gic_flush_dcache_to_poc(page_address(page), baser->psz); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1690 | |
| 1691 | table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID); |
| 1692 | |
| 1693 | /* Flush Lvl1 entry to PoC if hw doesn't support coherency */ |
| 1694 | if (!(baser->val & GITS_BASER_SHAREABILITY_MASK)) |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 1695 | gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE); |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1696 | |
| 1697 | /* Ensure updated table contents are visible to ITS hardware */ |
| 1698 | dsb(sy); |
| 1699 | } |
| 1700 | |
| 1701 | return true; |
| 1702 | } |
| 1703 | |
Marc Zyngier | 70cc81e | 2016-12-19 18:53:02 +0000 | [diff] [blame] | 1704 | static bool its_alloc_device_table(struct its_node *its, u32 dev_id) |
| 1705 | { |
| 1706 | struct its_baser *baser; |
| 1707 | |
| 1708 | baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE); |
| 1709 | |
| 1710 | /* Don't allow device id that exceeds ITS hardware limit */ |
| 1711 | if (!baser) |
| 1712 | return (ilog2(dev_id) < its->device_ids); |
| 1713 | |
| 1714 | return its_alloc_table_entry(baser, dev_id); |
| 1715 | } |
| 1716 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1717 | static struct its_device *its_create_device(struct its_node *its, u32 dev_id, |
| 1718 | int nvecs) |
| 1719 | { |
| 1720 | struct its_device *dev; |
| 1721 | unsigned long *lpi_map; |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 1722 | unsigned long flags; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1723 | u16 *col_map = NULL; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1724 | void *itt; |
| 1725 | int lpi_base; |
| 1726 | int nr_lpis; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 1727 | int nr_ites; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1728 | int sz; |
| 1729 | |
Shanker Donthineni | 3faf24e | 2016-06-06 18:17:32 -0500 | [diff] [blame] | 1730 | if (!its_alloc_device_table(its, dev_id)) |
Shanker Donthineni | 466b7d1 | 2016-03-09 22:10:49 -0600 | [diff] [blame] | 1731 | return NULL; |
| 1732 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1733 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 1734 | /* |
| 1735 | * At least one bit of EventID is being used, hence a minimum |
| 1736 | * of two entries. No, the architecture doesn't let you |
| 1737 | * express an ITT with a single entry. |
| 1738 | */ |
Will Deacon | 96555c4 | 2014-12-17 14:11:09 +0000 | [diff] [blame] | 1739 | nr_ites = max(2UL, roundup_pow_of_two(nvecs)); |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 1740 | sz = nr_ites * its->ite_size; |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1741 | sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1; |
Yun Wu | 6c83412 | 2015-03-06 16:37:46 +0000 | [diff] [blame] | 1742 | itt = kzalloc(sz, GFP_KERNEL); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1743 | lpi_map = its_lpi_alloc_chunks(nvecs, &lpi_base, &nr_lpis); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1744 | if (lpi_map) |
| 1745 | col_map = kzalloc(sizeof(*col_map) * nr_lpis, GFP_KERNEL); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1746 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1747 | if (!dev || !itt || !lpi_map || !col_map) { |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1748 | kfree(dev); |
| 1749 | kfree(itt); |
| 1750 | kfree(lpi_map); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1751 | kfree(col_map); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1752 | return NULL; |
| 1753 | } |
| 1754 | |
Vladimir Murzin | 328191c | 2016-11-02 11:54:05 +0000 | [diff] [blame] | 1755 | gic_flush_dcache_to_poc(itt, sz); |
Marc Zyngier | 5a9a891 | 2015-09-13 12:14:32 +0100 | [diff] [blame] | 1756 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1757 | dev->its = its; |
| 1758 | dev->itt = itt; |
Marc Zyngier | c848126 | 2014-12-12 10:51:24 +0000 | [diff] [blame] | 1759 | dev->nr_ites = nr_ites; |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1760 | dev->event_map.lpi_map = lpi_map; |
| 1761 | dev->event_map.col_map = col_map; |
| 1762 | dev->event_map.lpi_base = lpi_base; |
| 1763 | dev->event_map.nr_lpis = nr_lpis; |
Marc Zyngier | d011e4e | 2016-12-20 09:44:41 +0000 | [diff] [blame^] | 1764 | mutex_init(&dev->event_map.vlpi_lock); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1765 | dev->device_id = dev_id; |
| 1766 | INIT_LIST_HEAD(&dev->entry); |
| 1767 | |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 1768 | raw_spin_lock_irqsave(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1769 | list_add(&dev->entry, &its->its_device_list); |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 1770 | raw_spin_unlock_irqrestore(&its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1771 | |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1772 | /* Map device to its ITT */ |
| 1773 | its_send_mapd(dev, 1); |
| 1774 | |
| 1775 | return dev; |
| 1776 | } |
| 1777 | |
| 1778 | static void its_free_device(struct its_device *its_dev) |
| 1779 | { |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 1780 | unsigned long flags; |
| 1781 | |
| 1782 | raw_spin_lock_irqsave(&its_dev->its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1783 | list_del(&its_dev->entry); |
Marc Zyngier | 3e39e8f5 | 2015-03-06 16:37:43 +0000 | [diff] [blame] | 1784 | raw_spin_unlock_irqrestore(&its_dev->its->lock, flags); |
Marc Zyngier | 84a6a2e | 2014-11-24 14:35:15 +0000 | [diff] [blame] | 1785 | kfree(its_dev->itt); |
| 1786 | kfree(its_dev); |
| 1787 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1788 | |
| 1789 | static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq) |
| 1790 | { |
| 1791 | int idx; |
| 1792 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1793 | idx = find_first_zero_bit(dev->event_map.lpi_map, |
| 1794 | dev->event_map.nr_lpis); |
| 1795 | if (idx == dev->event_map.nr_lpis) |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1796 | return -ENOSPC; |
| 1797 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1798 | *hwirq = dev->event_map.lpi_base + idx; |
| 1799 | set_bit(idx, dev->event_map.lpi_map); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1800 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1801 | return 0; |
| 1802 | } |
| 1803 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 1804 | static int its_msi_prepare(struct irq_domain *domain, struct device *dev, |
| 1805 | int nvec, msi_alloc_info_t *info) |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 1806 | { |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1807 | struct its_node *its; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1808 | struct its_device *its_dev; |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 1809 | struct msi_domain_info *msi_info; |
| 1810 | u32 dev_id; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1811 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 1812 | /* |
| 1813 | * We ignore "dev" entierely, and rely on the dev_id that has |
| 1814 | * been passed via the scratchpad. This limits this domain's |
| 1815 | * usefulness to upper layers that definitely know that they |
| 1816 | * are built on top of the ITS. |
| 1817 | */ |
| 1818 | dev_id = info->scratchpad[0].ul; |
| 1819 | |
| 1820 | msi_info = msi_get_domain_info(domain); |
| 1821 | its = msi_info->data; |
| 1822 | |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 1823 | its_dev = its_find_device(its, dev_id); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 1824 | if (its_dev) { |
| 1825 | /* |
| 1826 | * We already have seen this ID, probably through |
| 1827 | * another alias (PCI bridge of some sort). No need to |
| 1828 | * create the device. |
| 1829 | */ |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 1830 | pr_debug("Reusing ITT for devID %x\n", dev_id); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 1831 | goto out; |
| 1832 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1833 | |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 1834 | its_dev = its_create_device(its, dev_id, nvec); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1835 | if (!its_dev) |
| 1836 | return -ENOMEM; |
| 1837 | |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 1838 | pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec)); |
Marc Zyngier | e8137f4 | 2015-03-06 16:37:42 +0000 | [diff] [blame] | 1839 | out: |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1840 | info->scratchpad[0].ptr = its_dev; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1841 | return 0; |
| 1842 | } |
| 1843 | |
Marc Zyngier | 54456db | 2015-07-28 14:46:21 +0100 | [diff] [blame] | 1844 | static struct msi_domain_ops its_msi_domain_ops = { |
| 1845 | .msi_prepare = its_msi_prepare, |
| 1846 | }; |
| 1847 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1848 | static int its_irq_gic_domain_alloc(struct irq_domain *domain, |
| 1849 | unsigned int virq, |
| 1850 | irq_hw_number_t hwirq) |
| 1851 | { |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 1852 | struct irq_fwspec fwspec; |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1853 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 1854 | if (irq_domain_get_of_node(domain->parent)) { |
| 1855 | fwspec.fwnode = domain->parent->fwnode; |
| 1856 | fwspec.param_count = 3; |
| 1857 | fwspec.param[0] = GIC_IRQ_TYPE_LPI; |
| 1858 | fwspec.param[1] = hwirq; |
| 1859 | fwspec.param[2] = IRQ_TYPE_EDGE_RISING; |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 1860 | } else if (is_fwnode_irqchip(domain->parent->fwnode)) { |
| 1861 | fwspec.fwnode = domain->parent->fwnode; |
| 1862 | fwspec.param_count = 2; |
| 1863 | fwspec.param[0] = hwirq; |
| 1864 | fwspec.param[1] = IRQ_TYPE_EDGE_RISING; |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 1865 | } else { |
| 1866 | return -EINVAL; |
| 1867 | } |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1868 | |
Marc Zyngier | f833f57 | 2015-10-13 12:51:33 +0100 | [diff] [blame] | 1869 | return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 1873 | unsigned int nr_irqs, void *args) |
| 1874 | { |
| 1875 | msi_alloc_info_t *info = args; |
| 1876 | struct its_device *its_dev = info->scratchpad[0].ptr; |
| 1877 | irq_hw_number_t hwirq; |
| 1878 | int err; |
| 1879 | int i; |
| 1880 | |
| 1881 | for (i = 0; i < nr_irqs; i++) { |
| 1882 | err = its_alloc_device_irq(its_dev, &hwirq); |
| 1883 | if (err) |
| 1884 | return err; |
| 1885 | |
| 1886 | err = its_irq_gic_domain_alloc(domain, virq + i, hwirq); |
| 1887 | if (err) |
| 1888 | return err; |
| 1889 | |
| 1890 | irq_domain_set_hwirq_and_chip(domain, virq + i, |
| 1891 | hwirq, &its_irq_chip, its_dev); |
Marc Zyngier | f130420 | 2015-07-28 14:46:18 +0100 | [diff] [blame] | 1892 | pr_debug("ID:%d pID:%d vID:%d\n", |
| 1893 | (int)(hwirq - its_dev->event_map.lpi_base), |
| 1894 | (int) hwirq, virq + i); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
| 1897 | return 0; |
| 1898 | } |
| 1899 | |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 1900 | static void its_irq_domain_activate(struct irq_domain *domain, |
| 1901 | struct irq_data *d) |
| 1902 | { |
| 1903 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1904 | u32 event = its_get_event_id(d); |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 1905 | const struct cpumask *cpu_mask = cpu_online_mask; |
| 1906 | |
| 1907 | /* get the cpu_mask of local node */ |
| 1908 | if (its_dev->its->numa_node >= 0) |
| 1909 | cpu_mask = cpumask_of_node(its_dev->its->numa_node); |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 1910 | |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1911 | /* Bind the LPI to the first possible CPU */ |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 1912 | its_dev->event_map.col_map[event] = cpumask_first(cpu_mask); |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1913 | |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 1914 | /* Map the GIC IRQ and event to the device */ |
Marc Zyngier | 6a25ad3 | 2016-12-20 15:52:26 +0000 | [diff] [blame] | 1915 | its_send_mapti(its_dev, d->hwirq, event); |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | static void its_irq_domain_deactivate(struct irq_domain *domain, |
| 1919 | struct irq_data *d) |
| 1920 | { |
| 1921 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1922 | u32 event = its_get_event_id(d); |
| 1923 | |
| 1924 | /* Stop the delivery of interrupts */ |
| 1925 | its_send_discard(its_dev, event); |
| 1926 | } |
| 1927 | |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1928 | static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq, |
| 1929 | unsigned int nr_irqs) |
| 1930 | { |
| 1931 | struct irq_data *d = irq_domain_get_irq_data(domain, virq); |
| 1932 | struct its_device *its_dev = irq_data_get_irq_chip_data(d); |
| 1933 | int i; |
| 1934 | |
| 1935 | for (i = 0; i < nr_irqs; i++) { |
| 1936 | struct irq_data *data = irq_domain_get_irq_data(domain, |
| 1937 | virq + i); |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 1938 | u32 event = its_get_event_id(data); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1939 | |
| 1940 | /* Mark interrupt index as unused */ |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1941 | clear_bit(event, its_dev->event_map.lpi_map); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1942 | |
| 1943 | /* Nuke the entry in the domain */ |
Marc Zyngier | 2da3994 | 2014-12-12 10:51:22 +0000 | [diff] [blame] | 1944 | irq_domain_reset_irq_data(data); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1945 | } |
| 1946 | |
| 1947 | /* If all interrupts have been freed, start mopping the floor */ |
Marc Zyngier | 591e5be | 2015-07-17 10:46:42 +0100 | [diff] [blame] | 1948 | if (bitmap_empty(its_dev->event_map.lpi_map, |
| 1949 | its_dev->event_map.nr_lpis)) { |
Marc Zyngier | cf2be8b | 2016-12-19 18:49:59 +0000 | [diff] [blame] | 1950 | its_lpi_free_chunks(its_dev->event_map.lpi_map, |
| 1951 | its_dev->event_map.lpi_base, |
| 1952 | its_dev->event_map.nr_lpis); |
| 1953 | kfree(its_dev->event_map.col_map); |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1954 | |
| 1955 | /* Unmap device/itt */ |
| 1956 | its_send_mapd(its_dev, 0); |
| 1957 | its_free_device(its_dev); |
| 1958 | } |
| 1959 | |
| 1960 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 1961 | } |
| 1962 | |
| 1963 | static const struct irq_domain_ops its_domain_ops = { |
| 1964 | .alloc = its_irq_domain_alloc, |
| 1965 | .free = its_irq_domain_free, |
Marc Zyngier | aca268d | 2014-12-12 10:51:23 +0000 | [diff] [blame] | 1966 | .activate = its_irq_domain_activate, |
| 1967 | .deactivate = its_irq_domain_deactivate, |
Marc Zyngier | b48ac83 | 2014-11-24 14:35:16 +0000 | [diff] [blame] | 1968 | }; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 1969 | |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 1970 | static int its_force_quiescent(void __iomem *base) |
| 1971 | { |
| 1972 | u32 count = 1000000; /* 1s */ |
| 1973 | u32 val; |
| 1974 | |
| 1975 | val = readl_relaxed(base + GITS_CTLR); |
David Daney | 7611da8 | 2016-08-18 15:41:58 -0700 | [diff] [blame] | 1976 | /* |
| 1977 | * GIC architecture specification requires the ITS to be both |
| 1978 | * disabled and quiescent for writes to GITS_BASER<n> or |
| 1979 | * GITS_CBASER to not have UNPREDICTABLE results. |
| 1980 | */ |
| 1981 | if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE)) |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 1982 | return 0; |
| 1983 | |
| 1984 | /* Disable the generation of all interrupts to this ITS */ |
| 1985 | val &= ~GITS_CTLR_ENABLE; |
| 1986 | writel_relaxed(val, base + GITS_CTLR); |
| 1987 | |
| 1988 | /* Poll GITS_CTLR and wait until ITS becomes quiescent */ |
| 1989 | while (1) { |
| 1990 | val = readl_relaxed(base + GITS_CTLR); |
| 1991 | if (val & GITS_CTLR_QUIESCENT) |
| 1992 | return 0; |
| 1993 | |
| 1994 | count--; |
| 1995 | if (!count) |
| 1996 | return -EBUSY; |
| 1997 | |
| 1998 | cpu_relax(); |
| 1999 | udelay(1); |
| 2000 | } |
| 2001 | } |
| 2002 | |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 2003 | static void __maybe_unused its_enable_quirk_cavium_22375(void *data) |
| 2004 | { |
| 2005 | struct its_node *its = data; |
| 2006 | |
| 2007 | its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375; |
| 2008 | } |
| 2009 | |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 2010 | static void __maybe_unused its_enable_quirk_cavium_23144(void *data) |
| 2011 | { |
| 2012 | struct its_node *its = data; |
| 2013 | |
| 2014 | its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144; |
| 2015 | } |
| 2016 | |
Shanker Donthineni | 90922a2 | 2017-03-07 08:20:38 -0600 | [diff] [blame] | 2017 | static void __maybe_unused its_enable_quirk_qdf2400_e0065(void *data) |
| 2018 | { |
| 2019 | struct its_node *its = data; |
| 2020 | |
| 2021 | /* On QDF2400, the size of the ITE is 16Bytes */ |
| 2022 | its->ite_size = 16; |
| 2023 | } |
| 2024 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 2025 | static const struct gic_quirk its_quirks[] = { |
Robert Richter | 9410097 | 2015-09-21 22:58:38 +0200 | [diff] [blame] | 2026 | #ifdef CONFIG_CAVIUM_ERRATUM_22375 |
| 2027 | { |
| 2028 | .desc = "ITS: Cavium errata 22375, 24313", |
| 2029 | .iidr = 0xa100034c, /* ThunderX pass 1.x */ |
| 2030 | .mask = 0xffff0fff, |
| 2031 | .init = its_enable_quirk_cavium_22375, |
| 2032 | }, |
| 2033 | #endif |
Ganapatrao Kulkarni | fbf8f40 | 2016-05-25 15:29:20 +0200 | [diff] [blame] | 2034 | #ifdef CONFIG_CAVIUM_ERRATUM_23144 |
| 2035 | { |
| 2036 | .desc = "ITS: Cavium erratum 23144", |
| 2037 | .iidr = 0xa100034c, /* ThunderX pass 1.x */ |
| 2038 | .mask = 0xffff0fff, |
| 2039 | .init = its_enable_quirk_cavium_23144, |
| 2040 | }, |
| 2041 | #endif |
Shanker Donthineni | 90922a2 | 2017-03-07 08:20:38 -0600 | [diff] [blame] | 2042 | #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065 |
| 2043 | { |
| 2044 | .desc = "ITS: QDF2400 erratum 0065", |
| 2045 | .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */ |
| 2046 | .mask = 0xffffffff, |
| 2047 | .init = its_enable_quirk_qdf2400_e0065, |
| 2048 | }, |
| 2049 | #endif |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 2050 | { |
| 2051 | } |
| 2052 | }; |
| 2053 | |
| 2054 | static void its_enable_quirks(struct its_node *its) |
| 2055 | { |
| 2056 | u32 iidr = readl_relaxed(its->base + GITS_IIDR); |
| 2057 | |
| 2058 | gic_enable_quirks(iidr, its_quirks, its); |
| 2059 | } |
| 2060 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2061 | 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] | 2062 | { |
| 2063 | struct irq_domain *inner_domain; |
| 2064 | struct msi_domain_info *info; |
| 2065 | |
| 2066 | info = kzalloc(sizeof(*info), GFP_KERNEL); |
| 2067 | if (!info) |
| 2068 | return -ENOMEM; |
| 2069 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2070 | inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2071 | if (!inner_domain) { |
| 2072 | kfree(info); |
| 2073 | return -ENOMEM; |
| 2074 | } |
| 2075 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2076 | inner_domain->parent = its_parent; |
Marc Zyngier | 96f0d93 | 2017-06-22 11:42:50 +0100 | [diff] [blame] | 2077 | irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS); |
Eric Auger | 5976852 | 2017-01-19 20:58:00 +0000 | [diff] [blame] | 2078 | inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_REMAP; |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2079 | info->ops = &its_msi_domain_ops; |
| 2080 | info->data = its; |
| 2081 | inner_domain->host_data = info; |
| 2082 | |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2086 | static int __init its_compute_its_list_map(struct resource *res, |
| 2087 | void __iomem *its_base) |
| 2088 | { |
| 2089 | int its_number; |
| 2090 | u32 ctlr; |
| 2091 | |
| 2092 | /* |
| 2093 | * This is assumed to be done early enough that we're |
| 2094 | * guaranteed to be single-threaded, hence no |
| 2095 | * locking. Should this change, we should address |
| 2096 | * this. |
| 2097 | */ |
| 2098 | its_number = find_first_zero_bit(&its_list_map, ITS_LIST_MAX); |
| 2099 | if (its_number >= ITS_LIST_MAX) { |
| 2100 | pr_err("ITS@%pa: No ITSList entry available!\n", |
| 2101 | &res->start); |
| 2102 | return -EINVAL; |
| 2103 | } |
| 2104 | |
| 2105 | ctlr = readl_relaxed(its_base + GITS_CTLR); |
| 2106 | ctlr &= ~GITS_CTLR_ITS_NUMBER; |
| 2107 | ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT; |
| 2108 | writel_relaxed(ctlr, its_base + GITS_CTLR); |
| 2109 | ctlr = readl_relaxed(its_base + GITS_CTLR); |
| 2110 | if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) { |
| 2111 | its_number = ctlr & GITS_CTLR_ITS_NUMBER; |
| 2112 | its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT; |
| 2113 | } |
| 2114 | |
| 2115 | if (test_and_set_bit(its_number, &its_list_map)) { |
| 2116 | pr_err("ITS@%pa: Duplicate ITSList entry %d\n", |
| 2117 | &res->start, its_number); |
| 2118 | return -EINVAL; |
| 2119 | } |
| 2120 | |
| 2121 | return its_number; |
| 2122 | } |
| 2123 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2124 | static int __init its_probe_one(struct resource *res, |
| 2125 | struct fwnode_handle *handle, int numa_node) |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2126 | { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2127 | struct its_node *its; |
| 2128 | void __iomem *its_base; |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2129 | u32 val, ctlr; |
| 2130 | u64 baser, tmp, typer; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2131 | int err; |
| 2132 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2133 | its_base = ioremap(res->start, resource_size(res)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2134 | if (!its_base) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2135 | pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2136 | return -ENOMEM; |
| 2137 | } |
| 2138 | |
| 2139 | val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK; |
| 2140 | if (val != 0x30 && val != 0x40) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2141 | pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2142 | err = -ENODEV; |
| 2143 | goto out_unmap; |
| 2144 | } |
| 2145 | |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 2146 | err = its_force_quiescent(its_base); |
| 2147 | if (err) { |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2148 | pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start); |
Yun Wu | 4559fbb | 2015-03-06 16:37:50 +0000 | [diff] [blame] | 2149 | goto out_unmap; |
| 2150 | } |
| 2151 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2152 | pr_info("ITS %pR\n", res); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2153 | |
| 2154 | its = kzalloc(sizeof(*its), GFP_KERNEL); |
| 2155 | if (!its) { |
| 2156 | err = -ENOMEM; |
| 2157 | goto out_unmap; |
| 2158 | } |
| 2159 | |
| 2160 | raw_spin_lock_init(&its->lock); |
| 2161 | INIT_LIST_HEAD(&its->entry); |
| 2162 | INIT_LIST_HEAD(&its->its_device_list); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2163 | typer = gic_read_typer(its_base + GITS_TYPER); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2164 | its->base = its_base; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2165 | its->phys_base = res->start; |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2166 | its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer); |
| 2167 | its->is_v4 = !!(typer & GITS_TYPER_VLPIS); |
| 2168 | if (its->is_v4) { |
| 2169 | if (!(typer & GITS_TYPER_VMOVP)) { |
| 2170 | err = its_compute_its_list_map(res, its_base); |
| 2171 | if (err < 0) |
| 2172 | goto out_free_its; |
| 2173 | |
| 2174 | pr_info("ITS@%pa: Using ITS number %d\n", |
| 2175 | &res->start, err); |
| 2176 | } else { |
| 2177 | pr_info("ITS@%pa: Single VMOVP capable\n", &res->start); |
| 2178 | } |
| 2179 | } |
| 2180 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2181 | its->numa_node = numa_node; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2182 | |
Robert Richter | 5bc13c2 | 2017-02-01 18:38:25 +0100 | [diff] [blame] | 2183 | its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
| 2184 | get_order(ITS_CMD_QUEUE_SZ)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2185 | if (!its->cmd_base) { |
| 2186 | err = -ENOMEM; |
| 2187 | goto out_free_its; |
| 2188 | } |
| 2189 | its->cmd_write = its->cmd_base; |
| 2190 | |
Robert Richter | 67510cc | 2015-09-21 22:58:37 +0200 | [diff] [blame] | 2191 | its_enable_quirks(its); |
| 2192 | |
Shanker Donthineni | 0e0b0f6 | 2016-06-06 18:17:31 -0500 | [diff] [blame] | 2193 | err = its_alloc_tables(its); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2194 | if (err) |
| 2195 | goto out_free_cmd; |
| 2196 | |
| 2197 | err = its_alloc_collections(its); |
| 2198 | if (err) |
| 2199 | goto out_free_tables; |
| 2200 | |
| 2201 | baser = (virt_to_phys(its->cmd_base) | |
Shanker Donthineni | 2fd632a | 2017-01-25 21:51:41 -0600 | [diff] [blame] | 2202 | GITS_CBASER_RaWaWb | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2203 | GITS_CBASER_InnerShareable | |
| 2204 | (ITS_CMD_QUEUE_SZ / SZ_4K - 1) | |
| 2205 | GITS_CBASER_VALID); |
| 2206 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2207 | gits_write_cbaser(baser, its->base + GITS_CBASER); |
| 2208 | tmp = gits_read_cbaser(its->base + GITS_CBASER); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2209 | |
Marc Zyngier | 4ad3e36 | 2015-03-27 14:15:04 +0000 | [diff] [blame] | 2210 | if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) { |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2211 | if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) { |
| 2212 | /* |
| 2213 | * The HW reports non-shareable, we must |
| 2214 | * remove the cacheability attributes as |
| 2215 | * well. |
| 2216 | */ |
| 2217 | baser &= ~(GITS_CBASER_SHAREABILITY_MASK | |
| 2218 | GITS_CBASER_CACHEABILITY_MASK); |
| 2219 | baser |= GITS_CBASER_nC; |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2220 | gits_write_cbaser(baser, its->base + GITS_CBASER); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2221 | } |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2222 | pr_info("ITS: using cache flushing for cmd queue\n"); |
| 2223 | its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING; |
| 2224 | } |
| 2225 | |
Vladimir Murzin | 0968a61 | 2016-11-02 11:54:06 +0000 | [diff] [blame] | 2226 | gits_write_cwriter(0, its->base + GITS_CWRITER); |
Marc Zyngier | 3dfa576 | 2016-12-19 17:25:54 +0000 | [diff] [blame] | 2227 | ctlr = readl_relaxed(its->base + GITS_CTLR); |
| 2228 | writel_relaxed(ctlr | GITS_CTLR_ENABLE, its->base + GITS_CTLR); |
Marc Zyngier | 241a386 | 2015-03-27 14:15:05 +0000 | [diff] [blame] | 2229 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2230 | err = its_init_domain(handle, its); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2231 | if (err) |
| 2232 | goto out_free_tables; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2233 | |
| 2234 | spin_lock(&its_lock); |
| 2235 | list_add(&its->entry, &its_nodes); |
| 2236 | spin_unlock(&its_lock); |
| 2237 | |
| 2238 | return 0; |
| 2239 | |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2240 | out_free_tables: |
| 2241 | its_free_tables(its); |
| 2242 | out_free_cmd: |
Robert Richter | 5bc13c2 | 2017-02-01 18:38:25 +0100 | [diff] [blame] | 2243 | 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] | 2244 | out_free_its: |
| 2245 | kfree(its); |
| 2246 | out_unmap: |
| 2247 | iounmap(its_base); |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2248 | pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2249 | return err; |
| 2250 | } |
| 2251 | |
| 2252 | static bool gic_rdists_supports_plpis(void) |
| 2253 | { |
Marc Zyngier | 589ce5f | 2016-10-14 15:13:07 +0100 | [diff] [blame] | 2254 | 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] | 2255 | } |
| 2256 | |
| 2257 | int its_cpu_init(void) |
| 2258 | { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2259 | if (!list_empty(&its_nodes)) { |
Vladimir Murzin | 16acae7 | 2015-03-06 16:37:40 +0000 | [diff] [blame] | 2260 | if (!gic_rdists_supports_plpis()) { |
| 2261 | pr_info("CPU%d: LPIs not supported\n", smp_processor_id()); |
| 2262 | return -ENXIO; |
| 2263 | } |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2264 | its_cpu_init_lpis(); |
| 2265 | its_cpu_init_collection(); |
| 2266 | } |
| 2267 | |
| 2268 | return 0; |
| 2269 | } |
| 2270 | |
Arvind Yadav | 935bba7 | 2017-06-22 16:05:30 +0530 | [diff] [blame] | 2271 | static const struct of_device_id its_device_id[] = { |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2272 | { .compatible = "arm,gic-v3-its", }, |
| 2273 | {}, |
| 2274 | }; |
| 2275 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2276 | static int __init its_of_probe(struct device_node *node) |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2277 | { |
| 2278 | struct device_node *np; |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2279 | struct resource res; |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2280 | |
| 2281 | for (np = of_find_matching_node(node, its_device_id); np; |
| 2282 | np = of_find_matching_node(np, its_device_id)) { |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2283 | if (!of_property_read_bool(np, "msi-controller")) { |
Rob Herring | e81f54c | 2017-07-18 16:43:10 -0500 | [diff] [blame] | 2284 | pr_warn("%pOF: no msi-controller property, ITS ignored\n", |
| 2285 | np); |
Tomasz Nowicki | d14ae5e | 2016-09-12 20:32:23 +0200 | [diff] [blame] | 2286 | continue; |
| 2287 | } |
| 2288 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2289 | if (of_address_to_resource(np, 0, &res)) { |
Rob Herring | e81f54c | 2017-07-18 16:43:10 -0500 | [diff] [blame] | 2290 | pr_warn("%pOF: no regs?\n", np); |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2291 | continue; |
| 2292 | } |
| 2293 | |
| 2294 | its_probe_one(&res, &np->fwnode, of_node_to_nid(np)); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2295 | } |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2296 | return 0; |
| 2297 | } |
| 2298 | |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2299 | #ifdef CONFIG_ACPI |
| 2300 | |
| 2301 | #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K) |
| 2302 | |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 2303 | #if defined(CONFIG_ACPI_NUMA) && (ACPI_CA_VERSION >= 0x20170531) |
| 2304 | struct its_srat_map { |
| 2305 | /* numa node id */ |
| 2306 | u32 numa_node; |
| 2307 | /* GIC ITS ID */ |
| 2308 | u32 its_id; |
| 2309 | }; |
| 2310 | |
| 2311 | static struct its_srat_map its_srat_maps[MAX_NUMNODES] __initdata; |
| 2312 | static int its_in_srat __initdata; |
| 2313 | |
| 2314 | static int __init acpi_get_its_numa_node(u32 its_id) |
| 2315 | { |
| 2316 | int i; |
| 2317 | |
| 2318 | for (i = 0; i < its_in_srat; i++) { |
| 2319 | if (its_id == its_srat_maps[i].its_id) |
| 2320 | return its_srat_maps[i].numa_node; |
| 2321 | } |
| 2322 | return NUMA_NO_NODE; |
| 2323 | } |
| 2324 | |
| 2325 | static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header, |
| 2326 | const unsigned long end) |
| 2327 | { |
| 2328 | int node; |
| 2329 | struct acpi_srat_gic_its_affinity *its_affinity; |
| 2330 | |
| 2331 | its_affinity = (struct acpi_srat_gic_its_affinity *)header; |
| 2332 | if (!its_affinity) |
| 2333 | return -EINVAL; |
| 2334 | |
| 2335 | if (its_affinity->header.length < sizeof(*its_affinity)) { |
| 2336 | pr_err("SRAT: Invalid header length %d in ITS affinity\n", |
| 2337 | its_affinity->header.length); |
| 2338 | return -EINVAL; |
| 2339 | } |
| 2340 | |
| 2341 | if (its_in_srat >= MAX_NUMNODES) { |
| 2342 | pr_err("SRAT: ITS affinity exceeding max count[%d]\n", |
| 2343 | MAX_NUMNODES); |
| 2344 | return -EINVAL; |
| 2345 | } |
| 2346 | |
| 2347 | node = acpi_map_pxm_to_node(its_affinity->proximity_domain); |
| 2348 | |
| 2349 | if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) { |
| 2350 | pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node); |
| 2351 | return 0; |
| 2352 | } |
| 2353 | |
| 2354 | its_srat_maps[its_in_srat].numa_node = node; |
| 2355 | its_srat_maps[its_in_srat].its_id = its_affinity->its_id; |
| 2356 | its_in_srat++; |
| 2357 | pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n", |
| 2358 | its_affinity->proximity_domain, its_affinity->its_id, node); |
| 2359 | |
| 2360 | return 0; |
| 2361 | } |
| 2362 | |
| 2363 | static void __init acpi_table_parse_srat_its(void) |
| 2364 | { |
| 2365 | acpi_table_parse_entries(ACPI_SIG_SRAT, |
| 2366 | sizeof(struct acpi_table_srat), |
| 2367 | ACPI_SRAT_TYPE_GIC_ITS_AFFINITY, |
| 2368 | gic_acpi_parse_srat_its, 0); |
| 2369 | } |
| 2370 | #else |
| 2371 | static void __init acpi_table_parse_srat_its(void) { } |
| 2372 | static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; } |
| 2373 | #endif |
| 2374 | |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2375 | static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header, |
| 2376 | const unsigned long end) |
| 2377 | { |
| 2378 | struct acpi_madt_generic_translator *its_entry; |
| 2379 | struct fwnode_handle *dom_handle; |
| 2380 | struct resource res; |
| 2381 | int err; |
| 2382 | |
| 2383 | its_entry = (struct acpi_madt_generic_translator *)header; |
| 2384 | memset(&res, 0, sizeof(res)); |
| 2385 | res.start = its_entry->base_address; |
| 2386 | res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1; |
| 2387 | res.flags = IORESOURCE_MEM; |
| 2388 | |
| 2389 | dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address); |
| 2390 | if (!dom_handle) { |
| 2391 | pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n", |
| 2392 | &res.start); |
| 2393 | return -ENOMEM; |
| 2394 | } |
| 2395 | |
| 2396 | err = iort_register_domain_token(its_entry->translation_id, dom_handle); |
| 2397 | if (err) { |
| 2398 | pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n", |
| 2399 | &res.start, its_entry->translation_id); |
| 2400 | goto dom_err; |
| 2401 | } |
| 2402 | |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 2403 | err = its_probe_one(&res, dom_handle, |
| 2404 | acpi_get_its_numa_node(its_entry->translation_id)); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2405 | if (!err) |
| 2406 | return 0; |
| 2407 | |
| 2408 | iort_deregister_domain_token(its_entry->translation_id); |
| 2409 | dom_err: |
| 2410 | irq_domain_free_fwnode(dom_handle); |
| 2411 | return err; |
| 2412 | } |
| 2413 | |
| 2414 | static void __init its_acpi_probe(void) |
| 2415 | { |
Ganapatrao Kulkarni | dbd2b82 | 2017-06-22 11:40:12 +0530 | [diff] [blame] | 2416 | acpi_table_parse_srat_its(); |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2417 | acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR, |
| 2418 | gic_acpi_parse_madt_its, 0); |
| 2419 | } |
| 2420 | #else |
| 2421 | static void __init its_acpi_probe(void) { } |
| 2422 | #endif |
| 2423 | |
Tomasz Nowicki | db40f0a | 2016-09-12 20:32:24 +0200 | [diff] [blame] | 2424 | int __init its_init(struct fwnode_handle *handle, struct rdists *rdists, |
| 2425 | struct irq_domain *parent_domain) |
| 2426 | { |
| 2427 | struct device_node *of_node; |
| 2428 | |
| 2429 | its_parent = parent_domain; |
| 2430 | of_node = to_of_node(handle); |
| 2431 | if (of_node) |
| 2432 | its_of_probe(of_node); |
| 2433 | else |
Tomasz Nowicki | 3f010cf | 2016-09-12 20:32:25 +0200 | [diff] [blame] | 2434 | its_acpi_probe(); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2435 | |
| 2436 | if (list_empty(&its_nodes)) { |
| 2437 | pr_warn("ITS: No ITS available, not enabling LPIs\n"); |
| 2438 | return -ENXIO; |
| 2439 | } |
| 2440 | |
| 2441 | gic_rdists = rdists; |
Shanker Donthineni | 6c31e12 | 2017-06-22 18:19:14 -0500 | [diff] [blame] | 2442 | return its_alloc_lpi_tables(); |
Marc Zyngier | 4c21f3c | 2014-11-24 14:35:17 +0000 | [diff] [blame] | 2443 | } |