blob: 4251d2d8e6e7c2e95e92f620a2ef84ebb1836ac7 [file] [log] [blame]
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001/*
Marc Zyngierd7276b82016-12-20 15:11:47 +00002 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
Marc Zyngiercc2d3212014-11-24 14:35:11 +00003 * 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 Nowicki3f010cf2016-09-12 20:32:25 +020018#include <linux/acpi.h>
Hanjun Guo8d3554b2017-03-07 20:39:59 +080019#include <linux/acpi_iort.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000020#include <linux/bitmap.h>
21#include <linux/cpu.h>
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +010022#include <linux/crash_dump.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000023#include <linux/delay.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010024#include <linux/dma-iommu.h>
Marc Zyngier3fb68fa2018-07-27 16:21:18 +010025#include <linux/efi.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000026#include <linux/interrupt.h>
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +020027#include <linux/irqdomain.h>
Marc Zyngier880cb3c2018-05-27 16:14:15 +010028#include <linux/list.h>
29#include <linux/list_sort.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000030#include <linux/log2.h>
Marc Zyngier5e2c9f92018-07-27 16:23:18 +010031#include <linux/memblock.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000032#include <linux/mm.h>
33#include <linux/msi.h>
34#include <linux/of.h>
35#include <linux/of_address.h>
36#include <linux/of_irq.h>
37#include <linux/of_pci.h>
38#include <linux/of_platform.h>
39#include <linux/percpu.h>
40#include <linux/slab.h>
Derek Basehoredba0bc72018-02-28 21:48:18 -080041#include <linux/syscore_ops.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000042
Joel Porquet41a83e062015-07-07 17:11:46 -040043#include <linux/irqchip.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000044#include <linux/irqchip/arm-gic-v3.h>
Marc Zyngierc808eea2016-12-20 09:31:20 +000045#include <linux/irqchip/arm-gic-v4.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000046
Marc Zyngiercc2d3212014-11-24 14:35:11 +000047#include <asm/cputype.h>
48#include <asm/exception.h>
49
Robert Richter67510cc2015-09-21 22:58:37 +020050#include "irq-gic-common.h"
51
Robert Richter94100972015-09-21 22:58:38 +020052#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
53#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020054#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
Derek Basehoredba0bc72018-02-28 21:48:18 -080055#define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3)
Marc Zyngiercc2d3212014-11-24 14:35:11 +000056
Marc Zyngierc48ed512014-11-24 14:35:12 +000057#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
Marc Zyngierc440a9d2018-07-27 15:40:13 +010058#define RDIST_FLAGS_RD_TABLES_PREALLOCATED (1 << 1)
Marc Zyngierc48ed512014-11-24 14:35:12 +000059
Marc Zyngiera13b0402016-12-19 17:15:24 +000060static u32 lpi_id_bits;
61
62/*
63 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
64 * deal with (one configuration byte per interrupt). PENDBASE has to
65 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
66 */
67#define LPI_NRBITS lpi_id_bits
68#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
69#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
70
71#define LPI_PROP_DEFAULT_PRIO 0xa0
72
Marc Zyngiercc2d3212014-11-24 14:35:11 +000073/*
74 * Collection structure - just an ID, and a redistributor address to
75 * ping. We use one per CPU as a bag of interrupts assigned to this
76 * CPU.
77 */
78struct its_collection {
79 u64 target_address;
80 u16 col_id;
81};
82
83/*
Shanker Donthineni93473592016-06-06 18:17:30 -050084 * The ITS_BASER structure - contains memory information, cached
85 * value of BASER register configuration and ITS page size.
Shanker Donthineni466b7d12016-03-09 22:10:49 -060086 */
87struct its_baser {
88 void *base;
89 u64 val;
90 u32 order;
Shanker Donthineni93473592016-06-06 18:17:30 -050091 u32 psz;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060092};
93
Ard Biesheuvel558b0162017-10-17 17:55:56 +010094struct its_device;
95
Shanker Donthineni466b7d12016-03-09 22:10:49 -060096/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000097 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010098 * top-level MSI domain, the command queue, the collections, and the
99 * list of devices writing to it.
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000100 */
101struct its_node {
102 raw_spinlock_t lock;
103 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000104 void __iomem *base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200105 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000106 struct its_cmd_block *cmd_base;
107 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600108 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000109 struct its_collection *collections;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100110 struct fwnode_handle *fwnode_handle;
111 u64 (*get_msi_base)(struct its_device *its_dev);
Derek Basehoredba0bc72018-02-28 21:48:18 -0800112 u64 cbaser_save;
113 u32 ctlr_save;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000114 struct list_head its_device_list;
115 u64 flags;
Marc Zyngierdebf6d02017-10-08 18:44:42 +0100116 unsigned long list_nr;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000117 u32 ite_size;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600118 u32 device_ids;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200119 int numa_node;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100120 unsigned int msi_domain_flags;
121 u32 pre_its_base; /* for Socionext Synquacer */
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000122 bool is_v4;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100123 int vlpi_redist_offset;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000124};
125
126#define ITS_ITT_ALIGN SZ_256
127
Shanker Donthineni32bd44d2017-10-07 15:43:48 -0500128/* The maximum number of VPEID bits supported by VLPI commands */
129#define ITS_MAX_VPEID_BITS (16)
130#define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
131
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600132/* Convert page order to size in bytes */
133#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
134
Marc Zyngier591e5be2015-07-17 10:46:42 +0100135struct event_lpi_map {
136 unsigned long *lpi_map;
137 u16 *col_map;
138 irq_hw_number_t lpi_base;
139 int nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000140 struct mutex vlpi_lock;
141 struct its_vm *vm;
142 struct its_vlpi_map *vlpi_maps;
143 int nr_vlpis;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100144};
145
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000146/*
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000147 * The ITS view of a device - belongs to an ITS, owns an interrupt
148 * translation table, and a list of interrupts. If it some of its
149 * LPIs are injected into a guest (GICv4), the event_map.vm field
150 * indicates which one.
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000151 */
152struct its_device {
153 struct list_head entry;
154 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100155 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000156 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000157 u32 nr_ites;
158 u32 device_id;
159};
160
Marc Zyngier20b3d542016-12-20 15:23:22 +0000161static struct {
162 raw_spinlock_t lock;
163 struct its_device *dev;
164 struct its_vpe **vpes;
165 int next_victim;
166} vpe_proxy;
167
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000168static LIST_HEAD(its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +0200169static DEFINE_RAW_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000170static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200171static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000172
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000173static unsigned long its_list_map;
Marc Zyngier3171a472016-12-20 15:17:28 +0000174static u16 vmovp_seq_num;
175static DEFINE_RAW_SPINLOCK(vmovp_lock);
176
Marc Zyngier7d75bbb2016-12-20 13:55:54 +0000177static DEFINE_IDA(its_vpeid_ida);
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000178
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000179#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
Marc Zyngier11e37d32018-07-27 13:38:54 +0100180#define gic_data_rdist_cpu(cpu) (per_cpu_ptr(gic_rdists->rdist, cpu))
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000181#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngiere643d802016-12-20 15:09:31 +0000182#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000183
Marc Zyngier591e5be2015-07-17 10:46:42 +0100184static struct its_collection *dev_event_to_col(struct its_device *its_dev,
185 u32 event)
186{
187 struct its_node *its = its_dev->its;
188
189 return its->collections + its_dev->event_map.col_map[event];
190}
191
Marc Zyngier83559b42018-06-22 10:52:52 +0100192static struct its_collection *valid_col(struct its_collection *col)
193{
194 if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(0, 15)))
195 return NULL;
196
197 return col;
198}
199
Marc Zyngier205e0652018-06-22 10:52:53 +0100200static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
201{
202 if (valid_col(its->collections + vpe->col_idx))
203 return vpe;
204
205 return NULL;
206}
207
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000208/*
209 * ITS command descriptors - parameters to be encoded in a command
210 * block.
211 */
212struct its_cmd_desc {
213 union {
214 struct {
215 struct its_device *dev;
216 u32 event_id;
217 } its_inv_cmd;
218
219 struct {
220 struct its_device *dev;
221 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000222 } its_clear_cmd;
223
224 struct {
225 struct its_device *dev;
226 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000227 } its_int_cmd;
228
229 struct {
230 struct its_device *dev;
231 int valid;
232 } its_mapd_cmd;
233
234 struct {
235 struct its_collection *col;
236 int valid;
237 } its_mapc_cmd;
238
239 struct {
240 struct its_device *dev;
241 u32 phys_id;
242 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000243 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000244
245 struct {
246 struct its_device *dev;
247 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100248 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000249 } its_movi_cmd;
250
251 struct {
252 struct its_device *dev;
253 u32 event_id;
254 } its_discard_cmd;
255
256 struct {
257 struct its_collection *col;
258 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000259
260 struct {
261 struct its_vpe *vpe;
Marc Zyngiereb781922016-12-20 14:47:05 +0000262 } its_vinvall_cmd;
263
264 struct {
265 struct its_vpe *vpe;
266 struct its_collection *col;
267 bool valid;
268 } its_vmapp_cmd;
269
270 struct {
271 struct its_vpe *vpe;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000272 struct its_device *dev;
273 u32 virt_id;
274 u32 event_id;
275 bool db_enabled;
276 } its_vmapti_cmd;
277
278 struct {
279 struct its_vpe *vpe;
280 struct its_device *dev;
281 u32 event_id;
282 bool db_enabled;
283 } its_vmovi_cmd;
Marc Zyngier3171a472016-12-20 15:17:28 +0000284
285 struct {
286 struct its_vpe *vpe;
287 struct its_collection *col;
288 u16 seq_num;
289 u16 its_list;
290 } its_vmovp_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000291 };
292};
293
294/*
295 * The ITS command block, which is what the ITS actually parses.
296 */
297struct its_cmd_block {
298 u64 raw_cmd[4];
299};
300
301#define ITS_CMD_QUEUE_SZ SZ_64K
302#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
303
Marc Zyngier67047f902017-07-28 21:16:58 +0100304typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
305 struct its_cmd_block *,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000306 struct its_cmd_desc *);
307
Marc Zyngier67047f902017-07-28 21:16:58 +0100308typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
309 struct its_cmd_block *,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000310 struct its_cmd_desc *);
311
Marc Zyngier4d36f132016-12-19 17:11:52 +0000312static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
313{
314 u64 mask = GENMASK_ULL(h, l);
315 *raw_cmd &= ~mask;
316 *raw_cmd |= (val << l) & mask;
317}
318
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000319static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
320{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000321 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000322}
323
324static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
325{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000326 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000327}
328
329static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
330{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000331 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000332}
333
334static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
335{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000336 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000337}
338
339static void its_encode_size(struct its_cmd_block *cmd, u8 size)
340{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000341 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000342}
343
344static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
345{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500346 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000347}
348
349static void its_encode_valid(struct its_cmd_block *cmd, int valid)
350{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000351 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000352}
353
354static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
355{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500356 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000357}
358
359static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
360{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000361 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000362}
363
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000364static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
365{
366 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
367}
368
369static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
370{
371 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
372}
373
374static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
375{
376 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
377}
378
379static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
380{
381 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
382}
383
Marc Zyngier3171a472016-12-20 15:17:28 +0000384static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
385{
386 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
387}
388
389static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
390{
391 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
392}
393
Marc Zyngiereb781922016-12-20 14:47:05 +0000394static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
395{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500396 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
Marc Zyngiereb781922016-12-20 14:47:05 +0000397}
398
399static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
400{
401 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
402}
403
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000404static inline void its_fixup_cmd(struct its_cmd_block *cmd)
405{
406 /* Let's fixup BE commands */
407 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
408 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
409 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
410 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
411}
412
Marc Zyngier67047f902017-07-28 21:16:58 +0100413static struct its_collection *its_build_mapd_cmd(struct its_node *its,
414 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000415 struct its_cmd_desc *desc)
416{
417 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000418 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000419
420 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
421 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
422
423 its_encode_cmd(cmd, GITS_CMD_MAPD);
424 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
425 its_encode_size(cmd, size - 1);
426 its_encode_itt(cmd, itt_addr);
427 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
428
429 its_fixup_cmd(cmd);
430
Marc Zyngier591e5be2015-07-17 10:46:42 +0100431 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000432}
433
Marc Zyngier67047f902017-07-28 21:16:58 +0100434static struct its_collection *its_build_mapc_cmd(struct its_node *its,
435 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000436 struct its_cmd_desc *desc)
437{
438 its_encode_cmd(cmd, GITS_CMD_MAPC);
439 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
440 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
441 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
442
443 its_fixup_cmd(cmd);
444
445 return desc->its_mapc_cmd.col;
446}
447
Marc Zyngier67047f902017-07-28 21:16:58 +0100448static struct its_collection *its_build_mapti_cmd(struct its_node *its,
449 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000450 struct its_cmd_desc *desc)
451{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100452 struct its_collection *col;
453
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000454 col = dev_event_to_col(desc->its_mapti_cmd.dev,
455 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100456
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000457 its_encode_cmd(cmd, GITS_CMD_MAPTI);
458 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
459 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
460 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100461 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000462
463 its_fixup_cmd(cmd);
464
Marc Zyngier83559b42018-06-22 10:52:52 +0100465 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000466}
467
Marc Zyngier67047f902017-07-28 21:16:58 +0100468static struct its_collection *its_build_movi_cmd(struct its_node *its,
469 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000470 struct its_cmd_desc *desc)
471{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100472 struct its_collection *col;
473
474 col = dev_event_to_col(desc->its_movi_cmd.dev,
475 desc->its_movi_cmd.event_id);
476
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000477 its_encode_cmd(cmd, GITS_CMD_MOVI);
478 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100479 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000480 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
481
482 its_fixup_cmd(cmd);
483
Marc Zyngier83559b42018-06-22 10:52:52 +0100484 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000485}
486
Marc Zyngier67047f902017-07-28 21:16:58 +0100487static struct its_collection *its_build_discard_cmd(struct its_node *its,
488 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000489 struct its_cmd_desc *desc)
490{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100491 struct its_collection *col;
492
493 col = dev_event_to_col(desc->its_discard_cmd.dev,
494 desc->its_discard_cmd.event_id);
495
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000496 its_encode_cmd(cmd, GITS_CMD_DISCARD);
497 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
498 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
499
500 its_fixup_cmd(cmd);
501
Marc Zyngier83559b42018-06-22 10:52:52 +0100502 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000503}
504
Marc Zyngier67047f902017-07-28 21:16:58 +0100505static struct its_collection *its_build_inv_cmd(struct its_node *its,
506 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000507 struct its_cmd_desc *desc)
508{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100509 struct its_collection *col;
510
511 col = dev_event_to_col(desc->its_inv_cmd.dev,
512 desc->its_inv_cmd.event_id);
513
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000514 its_encode_cmd(cmd, GITS_CMD_INV);
515 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
516 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
517
518 its_fixup_cmd(cmd);
519
Marc Zyngier83559b42018-06-22 10:52:52 +0100520 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000521}
522
Marc Zyngier67047f902017-07-28 21:16:58 +0100523static struct its_collection *its_build_int_cmd(struct its_node *its,
524 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000525 struct its_cmd_desc *desc)
526{
527 struct its_collection *col;
528
529 col = dev_event_to_col(desc->its_int_cmd.dev,
530 desc->its_int_cmd.event_id);
531
532 its_encode_cmd(cmd, GITS_CMD_INT);
533 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
534 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
535
536 its_fixup_cmd(cmd);
537
Marc Zyngier83559b42018-06-22 10:52:52 +0100538 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000539}
540
Marc Zyngier67047f902017-07-28 21:16:58 +0100541static struct its_collection *its_build_clear_cmd(struct its_node *its,
542 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000543 struct its_cmd_desc *desc)
544{
545 struct its_collection *col;
546
547 col = dev_event_to_col(desc->its_clear_cmd.dev,
548 desc->its_clear_cmd.event_id);
549
550 its_encode_cmd(cmd, GITS_CMD_CLEAR);
551 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
552 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
553
554 its_fixup_cmd(cmd);
555
Marc Zyngier83559b42018-06-22 10:52:52 +0100556 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000557}
558
Marc Zyngier67047f902017-07-28 21:16:58 +0100559static struct its_collection *its_build_invall_cmd(struct its_node *its,
560 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000561 struct its_cmd_desc *desc)
562{
563 its_encode_cmd(cmd, GITS_CMD_INVALL);
564 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
565
566 its_fixup_cmd(cmd);
567
568 return NULL;
569}
570
Marc Zyngier67047f902017-07-28 21:16:58 +0100571static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
572 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000573 struct its_cmd_desc *desc)
574{
575 its_encode_cmd(cmd, GITS_CMD_VINVALL);
576 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
577
578 its_fixup_cmd(cmd);
579
Marc Zyngier205e0652018-06-22 10:52:53 +0100580 return valid_vpe(its, desc->its_vinvall_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000581}
582
Marc Zyngier67047f902017-07-28 21:16:58 +0100583static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
584 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000585 struct its_cmd_desc *desc)
586{
587 unsigned long vpt_addr;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100588 u64 target;
Marc Zyngiereb781922016-12-20 14:47:05 +0000589
590 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100591 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngiereb781922016-12-20 14:47:05 +0000592
593 its_encode_cmd(cmd, GITS_CMD_VMAPP);
594 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
595 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100596 its_encode_target(cmd, target);
Marc Zyngiereb781922016-12-20 14:47:05 +0000597 its_encode_vpt_addr(cmd, vpt_addr);
598 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
599
600 its_fixup_cmd(cmd);
601
Marc Zyngier205e0652018-06-22 10:52:53 +0100602 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000603}
604
Marc Zyngier67047f902017-07-28 21:16:58 +0100605static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
606 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000607 struct its_cmd_desc *desc)
608{
609 u32 db;
610
611 if (desc->its_vmapti_cmd.db_enabled)
612 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
613 else
614 db = 1023;
615
616 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
617 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
618 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
619 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
620 its_encode_db_phys_id(cmd, db);
621 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
622
623 its_fixup_cmd(cmd);
624
Marc Zyngier205e0652018-06-22 10:52:53 +0100625 return valid_vpe(its, desc->its_vmapti_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000626}
627
Marc Zyngier67047f902017-07-28 21:16:58 +0100628static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
629 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000630 struct its_cmd_desc *desc)
631{
632 u32 db;
633
634 if (desc->its_vmovi_cmd.db_enabled)
635 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
636 else
637 db = 1023;
638
639 its_encode_cmd(cmd, GITS_CMD_VMOVI);
640 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
641 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
642 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
643 its_encode_db_phys_id(cmd, db);
644 its_encode_db_valid(cmd, true);
645
646 its_fixup_cmd(cmd);
647
Marc Zyngier205e0652018-06-22 10:52:53 +0100648 return valid_vpe(its, desc->its_vmovi_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000649}
650
Marc Zyngier67047f902017-07-28 21:16:58 +0100651static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
652 struct its_cmd_block *cmd,
Marc Zyngier3171a472016-12-20 15:17:28 +0000653 struct its_cmd_desc *desc)
654{
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100655 u64 target;
656
657 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngier3171a472016-12-20 15:17:28 +0000658 its_encode_cmd(cmd, GITS_CMD_VMOVP);
659 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
660 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
661 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100662 its_encode_target(cmd, target);
Marc Zyngier3171a472016-12-20 15:17:28 +0000663
664 its_fixup_cmd(cmd);
665
Marc Zyngier205e0652018-06-22 10:52:53 +0100666 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
Marc Zyngier3171a472016-12-20 15:17:28 +0000667}
668
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000669static u64 its_cmd_ptr_to_offset(struct its_node *its,
670 struct its_cmd_block *ptr)
671{
672 return (ptr - its->cmd_base) * sizeof(*ptr);
673}
674
675static int its_queue_full(struct its_node *its)
676{
677 int widx;
678 int ridx;
679
680 widx = its->cmd_write - its->cmd_base;
681 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
682
683 /* This is incredibly unlikely to happen, unless the ITS locks up. */
684 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
685 return 1;
686
687 return 0;
688}
689
690static struct its_cmd_block *its_allocate_entry(struct its_node *its)
691{
692 struct its_cmd_block *cmd;
693 u32 count = 1000000; /* 1s! */
694
695 while (its_queue_full(its)) {
696 count--;
697 if (!count) {
698 pr_err_ratelimited("ITS queue not draining\n");
699 return NULL;
700 }
701 cpu_relax();
702 udelay(1);
703 }
704
705 cmd = its->cmd_write++;
706
707 /* Handle queue wrapping */
708 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
709 its->cmd_write = its->cmd_base;
710
Marc Zyngier34d677a2016-12-19 17:16:45 +0000711 /* Clear command */
712 cmd->raw_cmd[0] = 0;
713 cmd->raw_cmd[1] = 0;
714 cmd->raw_cmd[2] = 0;
715 cmd->raw_cmd[3] = 0;
716
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000717 return cmd;
718}
719
720static struct its_cmd_block *its_post_commands(struct its_node *its)
721{
722 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
723
724 writel_relaxed(wr, its->base + GITS_CWRITER);
725
726 return its->cmd_write;
727}
728
729static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
730{
731 /*
732 * Make sure the commands written to memory are observable by
733 * the ITS.
734 */
735 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000736 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000737 else
738 dsb(ishst);
739}
740
Marc Zyngiera19b4622017-08-04 17:45:50 +0100741static int its_wait_for_range_completion(struct its_node *its,
742 struct its_cmd_block *from,
743 struct its_cmd_block *to)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000744{
745 u64 rd_idx, from_idx, to_idx;
746 u32 count = 1000000; /* 1s! */
747
748 from_idx = its_cmd_ptr_to_offset(its, from);
749 to_idx = its_cmd_ptr_to_offset(its, to);
750
751 while (1) {
752 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100753
754 /* Direct case */
755 if (from_idx < to_idx && rd_idx >= to_idx)
756 break;
757
758 /* Wrapped case */
759 if (from_idx >= to_idx && rd_idx >= to_idx && rd_idx < from_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000760 break;
761
762 count--;
763 if (!count) {
Marc Zyngiera19b4622017-08-04 17:45:50 +0100764 pr_err_ratelimited("ITS queue timeout (%llu %llu %llu)\n",
765 from_idx, to_idx, rd_idx);
766 return -1;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000767 }
768 cpu_relax();
769 udelay(1);
770 }
Marc Zyngiera19b4622017-08-04 17:45:50 +0100771
772 return 0;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000773}
774
Marc Zyngiere4f90942016-12-19 17:56:32 +0000775/* Warning, macro hell follows */
776#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
777void name(struct its_node *its, \
778 buildtype builder, \
779 struct its_cmd_desc *desc) \
780{ \
781 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
782 synctype *sync_obj; \
783 unsigned long flags; \
784 \
785 raw_spin_lock_irqsave(&its->lock, flags); \
786 \
787 cmd = its_allocate_entry(its); \
788 if (!cmd) { /* We're soooooo screewed... */ \
789 raw_spin_unlock_irqrestore(&its->lock, flags); \
790 return; \
791 } \
Marc Zyngier67047f902017-07-28 21:16:58 +0100792 sync_obj = builder(its, cmd, desc); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000793 its_flush_cmd(its, cmd); \
794 \
795 if (sync_obj) { \
796 sync_cmd = its_allocate_entry(its); \
797 if (!sync_cmd) \
798 goto post; \
799 \
Marc Zyngier67047f902017-07-28 21:16:58 +0100800 buildfn(its, sync_cmd, sync_obj); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000801 its_flush_cmd(its, sync_cmd); \
802 } \
803 \
804post: \
805 next_cmd = its_post_commands(its); \
806 raw_spin_unlock_irqrestore(&its->lock, flags); \
807 \
Marc Zyngiera19b4622017-08-04 17:45:50 +0100808 if (its_wait_for_range_completion(its, cmd, next_cmd)) \
809 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000810}
811
Marc Zyngier67047f902017-07-28 21:16:58 +0100812static void its_build_sync_cmd(struct its_node *its,
813 struct its_cmd_block *sync_cmd,
Marc Zyngiere4f90942016-12-19 17:56:32 +0000814 struct its_collection *sync_col)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000815{
Marc Zyngiere4f90942016-12-19 17:56:32 +0000816 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
817 its_encode_target(sync_cmd, sync_col->target_address);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000818
Marc Zyngiere4f90942016-12-19 17:56:32 +0000819 its_fixup_cmd(sync_cmd);
820}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000821
Marc Zyngiere4f90942016-12-19 17:56:32 +0000822static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
823 struct its_collection, its_build_sync_cmd)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000824
Marc Zyngier67047f902017-07-28 21:16:58 +0100825static void its_build_vsync_cmd(struct its_node *its,
826 struct its_cmd_block *sync_cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000827 struct its_vpe *sync_vpe)
828{
829 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
830 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000831
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000832 its_fixup_cmd(sync_cmd);
833}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000834
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000835static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
836 struct its_vpe, its_build_vsync_cmd)
837
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000838static void its_send_int(struct its_device *dev, u32 event_id)
839{
840 struct its_cmd_desc desc;
841
842 desc.its_int_cmd.dev = dev;
843 desc.its_int_cmd.event_id = event_id;
844
845 its_send_single_command(dev->its, its_build_int_cmd, &desc);
846}
847
848static void its_send_clear(struct its_device *dev, u32 event_id)
849{
850 struct its_cmd_desc desc;
851
852 desc.its_clear_cmd.dev = dev;
853 desc.its_clear_cmd.event_id = event_id;
854
855 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000856}
857
858static void its_send_inv(struct its_device *dev, u32 event_id)
859{
860 struct its_cmd_desc desc;
861
862 desc.its_inv_cmd.dev = dev;
863 desc.its_inv_cmd.event_id = event_id;
864
865 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
866}
867
868static void its_send_mapd(struct its_device *dev, int valid)
869{
870 struct its_cmd_desc desc;
871
872 desc.its_mapd_cmd.dev = dev;
873 desc.its_mapd_cmd.valid = !!valid;
874
875 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
876}
877
878static void its_send_mapc(struct its_node *its, struct its_collection *col,
879 int valid)
880{
881 struct its_cmd_desc desc;
882
883 desc.its_mapc_cmd.col = col;
884 desc.its_mapc_cmd.valid = !!valid;
885
886 its_send_single_command(its, its_build_mapc_cmd, &desc);
887}
888
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000889static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000890{
891 struct its_cmd_desc desc;
892
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000893 desc.its_mapti_cmd.dev = dev;
894 desc.its_mapti_cmd.phys_id = irq_id;
895 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000896
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000897 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000898}
899
900static void its_send_movi(struct its_device *dev,
901 struct its_collection *col, u32 id)
902{
903 struct its_cmd_desc desc;
904
905 desc.its_movi_cmd.dev = dev;
906 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100907 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000908
909 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
910}
911
912static void its_send_discard(struct its_device *dev, u32 id)
913{
914 struct its_cmd_desc desc;
915
916 desc.its_discard_cmd.dev = dev;
917 desc.its_discard_cmd.event_id = id;
918
919 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
920}
921
922static void its_send_invall(struct its_node *its, struct its_collection *col)
923{
924 struct its_cmd_desc desc;
925
926 desc.its_invall_cmd.col = col;
927
928 its_send_single_command(its, its_build_invall_cmd, &desc);
929}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000930
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000931static void its_send_vmapti(struct its_device *dev, u32 id)
932{
933 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
934 struct its_cmd_desc desc;
935
936 desc.its_vmapti_cmd.vpe = map->vpe;
937 desc.its_vmapti_cmd.dev = dev;
938 desc.its_vmapti_cmd.virt_id = map->vintid;
939 desc.its_vmapti_cmd.event_id = id;
940 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
941
942 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
943}
944
945static void its_send_vmovi(struct its_device *dev, u32 id)
946{
947 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
948 struct its_cmd_desc desc;
949
950 desc.its_vmovi_cmd.vpe = map->vpe;
951 desc.its_vmovi_cmd.dev = dev;
952 desc.its_vmovi_cmd.event_id = id;
953 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
954
955 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
956}
957
Marc Zyngier75fd9512017-10-08 18:46:39 +0100958static void its_send_vmapp(struct its_node *its,
959 struct its_vpe *vpe, bool valid)
Marc Zyngiereb781922016-12-20 14:47:05 +0000960{
961 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +0000962
963 desc.its_vmapp_cmd.vpe = vpe;
964 desc.its_vmapp_cmd.valid = valid;
Marc Zyngier75fd9512017-10-08 18:46:39 +0100965 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
Marc Zyngiereb781922016-12-20 14:47:05 +0000966
Marc Zyngier75fd9512017-10-08 18:46:39 +0100967 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +0000968}
969
Marc Zyngier3171a472016-12-20 15:17:28 +0000970static void its_send_vmovp(struct its_vpe *vpe)
971{
972 struct its_cmd_desc desc;
973 struct its_node *its;
974 unsigned long flags;
975 int col_id = vpe->col_idx;
976
977 desc.its_vmovp_cmd.vpe = vpe;
978 desc.its_vmovp_cmd.its_list = (u16)its_list_map;
979
980 if (!its_list_map) {
981 its = list_first_entry(&its_nodes, struct its_node, entry);
982 desc.its_vmovp_cmd.seq_num = 0;
983 desc.its_vmovp_cmd.col = &its->collections[col_id];
984 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
985 return;
986 }
987
988 /*
989 * Yet another marvel of the architecture. If using the
990 * its_list "feature", we need to make sure that all ITSs
991 * receive all VMOVP commands in the same order. The only way
992 * to guarantee this is to make vmovp a serialization point.
993 *
994 * Wall <-- Head.
995 */
996 raw_spin_lock_irqsave(&vmovp_lock, flags);
997
998 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
999
1000 /* Emit VMOVPs */
1001 list_for_each_entry(its, &its_nodes, entry) {
1002 if (!its->is_v4)
1003 continue;
1004
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001005 if (!vpe->its_vm->vlpi_count[its->list_nr])
1006 continue;
1007
Marc Zyngier3171a472016-12-20 15:17:28 +00001008 desc.its_vmovp_cmd.col = &its->collections[col_id];
1009 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1010 }
1011
1012 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1013}
1014
Marc Zyngier40619a22017-10-08 15:16:09 +01001015static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
Marc Zyngiereb781922016-12-20 14:47:05 +00001016{
1017 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +00001018
1019 desc.its_vinvall_cmd.vpe = vpe;
Marc Zyngier40619a22017-10-08 15:16:09 +01001020 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +00001021}
1022
Marc Zyngierc48ed512014-11-24 14:35:12 +00001023/*
1024 * irqchip functions - assumes MSI, mostly.
1025 */
1026
1027static inline u32 its_get_event_id(struct irq_data *d)
1028{
1029 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001030 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001031}
1032
Marc Zyngier015ec032016-12-20 09:54:57 +00001033static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +00001034{
Marc Zyngier015ec032016-12-20 09:54:57 +00001035 irq_hw_number_t hwirq;
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001036 void *va;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001037 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001038
Marc Zyngier015ec032016-12-20 09:54:57 +00001039 if (irqd_is_forwarded_to_vcpu(d)) {
1040 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1041 u32 event = its_get_event_id(d);
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001042 struct its_vlpi_map *map;
Marc Zyngier015ec032016-12-20 09:54:57 +00001043
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001044 va = page_address(its_dev->event_map.vm->vprop_page);
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001045 map = &its_dev->event_map.vlpi_maps[event];
1046 hwirq = map->vintid;
1047
1048 /* Remember the updated property */
1049 map->properties &= ~clr;
1050 map->properties |= set | LPI_PROP_GROUP1;
Marc Zyngier015ec032016-12-20 09:54:57 +00001051 } else {
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001052 va = gic_rdists->prop_table_va;
Marc Zyngier015ec032016-12-20 09:54:57 +00001053 hwirq = d->hwirq;
1054 }
Marc Zyngieradcdb942016-12-19 19:18:13 +00001055
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001056 cfg = va + hwirq - 8192;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001057 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +00001058 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001059
1060 /*
1061 * Make the above write visible to the redistributors.
1062 * And yes, we're flushing exactly: One. Single. Byte.
1063 * Humpf...
1064 */
1065 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001066 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001067 else
1068 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +00001069}
1070
1071static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1072{
1073 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1074
1075 lpi_write_config(d, clr, set);
Marc Zyngieradcdb942016-12-19 19:18:13 +00001076 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001077}
1078
Marc Zyngier015ec032016-12-20 09:54:57 +00001079static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1080{
1081 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1082 u32 event = its_get_event_id(d);
1083
1084 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1085 return;
1086
1087 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1088
1089 /*
1090 * More fun with the architecture:
1091 *
1092 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1093 * value or to 1023, depending on the enable bit. But that
1094 * would be issueing a mapping for an /existing/ DevID+EventID
1095 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1096 * to the /same/ vPE, using this opportunity to adjust the
1097 * doorbell. Mouahahahaha. We loves it, Precious.
1098 */
1099 its_send_vmovi(its_dev, event);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001100}
1101
1102static void its_mask_irq(struct irq_data *d)
1103{
Marc Zyngier015ec032016-12-20 09:54:57 +00001104 if (irqd_is_forwarded_to_vcpu(d))
1105 its_vlpi_set_doorbell(d, false);
1106
Marc Zyngieradcdb942016-12-19 19:18:13 +00001107 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001108}
1109
1110static void its_unmask_irq(struct irq_data *d)
1111{
Marc Zyngier015ec032016-12-20 09:54:57 +00001112 if (irqd_is_forwarded_to_vcpu(d))
1113 its_vlpi_set_doorbell(d, true);
1114
Marc Zyngieradcdb942016-12-19 19:18:13 +00001115 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001116}
1117
Marc Zyngierc48ed512014-11-24 14:35:12 +00001118static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1119 bool force)
1120{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001121 unsigned int cpu;
1122 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001123 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1124 struct its_collection *target_col;
1125 u32 id = its_get_event_id(d);
1126
Marc Zyngier015ec032016-12-20 09:54:57 +00001127 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1128 if (irqd_is_forwarded_to_vcpu(d))
1129 return -EINVAL;
1130
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001131 /* lpi cannot be routed to a redistributor that is on a foreign node */
1132 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1133 if (its_dev->its->numa_node >= 0) {
1134 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1135 if (!cpumask_intersects(mask_val, cpu_mask))
1136 return -EINVAL;
1137 }
1138 }
1139
1140 cpu = cpumask_any_and(mask_val, cpu_mask);
1141
Marc Zyngierc48ed512014-11-24 14:35:12 +00001142 if (cpu >= nr_cpu_ids)
1143 return -EINVAL;
1144
MaJun8b8d94a2017-05-18 16:19:13 +08001145 /* don't set the affinity when the target cpu is same as current one */
1146 if (cpu != its_dev->event_map.col_map[id]) {
1147 target_col = &its_dev->its->collections[cpu];
1148 its_send_movi(its_dev, target_col, id);
1149 its_dev->event_map.col_map[id] = cpu;
Marc Zyngier0d224d32017-08-18 09:39:18 +01001150 irq_data_update_effective_affinity(d, cpumask_of(cpu));
MaJun8b8d94a2017-05-18 16:19:13 +08001151 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001152
1153 return IRQ_SET_MASK_OK_DONE;
1154}
1155
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001156static u64 its_irq_get_msi_base(struct its_device *its_dev)
1157{
1158 struct its_node *its = its_dev->its;
1159
1160 return its->phys_base + GITS_TRANSLATER;
1161}
1162
Marc Zyngierb48ac832014-11-24 14:35:16 +00001163static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1164{
1165 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1166 struct its_node *its;
1167 u64 addr;
1168
1169 its = its_dev->its;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001170 addr = its->get_msi_base(its_dev);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001171
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001172 msg->address_lo = lower_32_bits(addr);
1173 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001174 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001175
1176 iommu_dma_map_msi_msg(d->irq, msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001177}
1178
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001179static int its_irq_set_irqchip_state(struct irq_data *d,
1180 enum irqchip_irq_state which,
1181 bool state)
1182{
1183 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1184 u32 event = its_get_event_id(d);
1185
1186 if (which != IRQCHIP_STATE_PENDING)
1187 return -EINVAL;
1188
1189 if (state)
1190 its_send_int(its_dev, event);
1191 else
1192 its_send_clear(its_dev, event);
1193
1194 return 0;
1195}
1196
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001197static void its_map_vm(struct its_node *its, struct its_vm *vm)
1198{
1199 unsigned long flags;
1200
1201 /* Not using the ITS list? Everything is always mapped. */
1202 if (!its_list_map)
1203 return;
1204
1205 raw_spin_lock_irqsave(&vmovp_lock, flags);
1206
1207 /*
1208 * If the VM wasn't mapped yet, iterate over the vpes and get
1209 * them mapped now.
1210 */
1211 vm->vlpi_count[its->list_nr]++;
1212
1213 if (vm->vlpi_count[its->list_nr] == 1) {
1214 int i;
1215
1216 for (i = 0; i < vm->nr_vpes; i++) {
1217 struct its_vpe *vpe = vm->vpes[i];
Marc Zyngier44c4c252017-10-19 10:11:34 +01001218 struct irq_data *d = irq_get_irq_data(vpe->irq);
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001219
1220 /* Map the VPE to the first possible CPU */
1221 vpe->col_idx = cpumask_first(cpu_online_mask);
1222 its_send_vmapp(its, vpe, true);
1223 its_send_vinvall(its, vpe);
Marc Zyngier44c4c252017-10-19 10:11:34 +01001224 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001225 }
1226 }
1227
1228 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1229}
1230
1231static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1232{
1233 unsigned long flags;
1234
1235 /* Not using the ITS list? Everything is always mapped. */
1236 if (!its_list_map)
1237 return;
1238
1239 raw_spin_lock_irqsave(&vmovp_lock, flags);
1240
1241 if (!--vm->vlpi_count[its->list_nr]) {
1242 int i;
1243
1244 for (i = 0; i < vm->nr_vpes; i++)
1245 its_send_vmapp(its, vm->vpes[i], false);
1246 }
1247
1248 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1249}
1250
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001251static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1252{
1253 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1254 u32 event = its_get_event_id(d);
1255 int ret = 0;
1256
1257 if (!info->map)
1258 return -EINVAL;
1259
1260 mutex_lock(&its_dev->event_map.vlpi_lock);
1261
1262 if (!its_dev->event_map.vm) {
1263 struct its_vlpi_map *maps;
1264
Kees Cook6396bb22018-06-12 14:03:40 -07001265 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001266 GFP_KERNEL);
1267 if (!maps) {
1268 ret = -ENOMEM;
1269 goto out;
1270 }
1271
1272 its_dev->event_map.vm = info->map->vm;
1273 its_dev->event_map.vlpi_maps = maps;
1274 } else if (its_dev->event_map.vm != info->map->vm) {
1275 ret = -EINVAL;
1276 goto out;
1277 }
1278
1279 /* Get our private copy of the mapping information */
1280 its_dev->event_map.vlpi_maps[event] = *info->map;
1281
1282 if (irqd_is_forwarded_to_vcpu(d)) {
1283 /* Already mapped, move it around */
1284 its_send_vmovi(its_dev, event);
1285 } else {
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001286 /* Ensure all the VPEs are mapped on this ITS */
1287 its_map_vm(its_dev->its, info->map->vm);
1288
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001289 /*
1290 * Flag the interrupt as forwarded so that we can
1291 * start poking the virtual property table.
1292 */
1293 irqd_set_forwarded_to_vcpu(d);
1294
1295 /* Write out the property to the prop table */
1296 lpi_write_config(d, 0xff, info->map->properties);
1297
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001298 /* Drop the physical mapping */
1299 its_send_discard(its_dev, event);
1300
1301 /* and install the virtual one */
1302 its_send_vmapti(its_dev, event);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001303
1304 /* Increment the number of VLPIs */
1305 its_dev->event_map.nr_vlpis++;
1306 }
1307
1308out:
1309 mutex_unlock(&its_dev->event_map.vlpi_lock);
1310 return ret;
1311}
1312
1313static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1314{
1315 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1316 u32 event = its_get_event_id(d);
1317 int ret = 0;
1318
1319 mutex_lock(&its_dev->event_map.vlpi_lock);
1320
1321 if (!its_dev->event_map.vm ||
1322 !its_dev->event_map.vlpi_maps[event].vm) {
1323 ret = -EINVAL;
1324 goto out;
1325 }
1326
1327 /* Copy our mapping information to the incoming request */
1328 *info->map = its_dev->event_map.vlpi_maps[event];
1329
1330out:
1331 mutex_unlock(&its_dev->event_map.vlpi_lock);
1332 return ret;
1333}
1334
1335static int its_vlpi_unmap(struct irq_data *d)
1336{
1337 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1338 u32 event = its_get_event_id(d);
1339 int ret = 0;
1340
1341 mutex_lock(&its_dev->event_map.vlpi_lock);
1342
1343 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1344 ret = -EINVAL;
1345 goto out;
1346 }
1347
1348 /* Drop the virtual mapping */
1349 its_send_discard(its_dev, event);
1350
1351 /* and restore the physical one */
1352 irqd_clr_forwarded_to_vcpu(d);
1353 its_send_mapti(its_dev, d->hwirq, event);
1354 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1355 LPI_PROP_ENABLED |
1356 LPI_PROP_GROUP1));
1357
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001358 /* Potentially unmap the VM from this ITS */
1359 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1360
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001361 /*
1362 * Drop the refcount and make the device available again if
1363 * this was the last VLPI.
1364 */
1365 if (!--its_dev->event_map.nr_vlpis) {
1366 its_dev->event_map.vm = NULL;
1367 kfree(its_dev->event_map.vlpi_maps);
1368 }
1369
1370out:
1371 mutex_unlock(&its_dev->event_map.vlpi_lock);
1372 return ret;
1373}
1374
Marc Zyngier015ec032016-12-20 09:54:57 +00001375static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1376{
1377 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1378
1379 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1380 return -EINVAL;
1381
1382 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1383 lpi_update_config(d, 0xff, info->config);
1384 else
1385 lpi_write_config(d, 0xff, info->config);
1386 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1387
1388 return 0;
1389}
1390
Marc Zyngierc808eea2016-12-20 09:31:20 +00001391static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1392{
1393 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1394 struct its_cmd_info *info = vcpu_info;
1395
1396 /* Need a v4 ITS */
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001397 if (!its_dev->its->is_v4)
Marc Zyngierc808eea2016-12-20 09:31:20 +00001398 return -EINVAL;
1399
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001400 /* Unmap request? */
1401 if (!info)
1402 return its_vlpi_unmap(d);
1403
Marc Zyngierc808eea2016-12-20 09:31:20 +00001404 switch (info->cmd_type) {
1405 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001406 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001407
1408 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001409 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001410
1411 case PROP_UPDATE_VLPI:
1412 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001413 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001414
1415 default:
1416 return -EINVAL;
1417 }
1418}
1419
Marc Zyngierc48ed512014-11-24 14:35:12 +00001420static struct irq_chip its_irq_chip = {
1421 .name = "ITS",
1422 .irq_mask = its_mask_irq,
1423 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001424 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001425 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001426 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001427 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001428 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001429};
1430
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001431
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001432/*
1433 * How we allocate LPIs:
1434 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001435 * lpi_range_list contains ranges of LPIs that are to available to
1436 * allocate from. To allocate LPIs, just pick the first range that
1437 * fits the required allocation, and reduce it by the required
1438 * amount. Once empty, remove the range from the list.
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001439 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001440 * To free a range of LPIs, add a free range to the list, sort it and
1441 * merge the result if the new range happens to be adjacent to an
1442 * already free block.
1443 *
1444 * The consequence of the above is that allocation is cost is low, but
1445 * freeing is expensive. We assumes that freeing rarely occurs.
1446 */
Jia He4cb205c2018-08-28 12:53:26 +08001447#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001448
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001449static DEFINE_MUTEX(lpi_range_lock);
1450static LIST_HEAD(lpi_range_list);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001451
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001452struct lpi_range {
1453 struct list_head entry;
1454 u32 base_id;
1455 u32 span;
1456};
1457
1458static struct lpi_range *mk_lpi_range(u32 base, u32 span)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001459{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001460 struct lpi_range *range;
1461
1462 range = kzalloc(sizeof(*range), GFP_KERNEL);
1463 if (range) {
1464 INIT_LIST_HEAD(&range->entry);
1465 range->base_id = base;
1466 range->span = span;
1467 }
1468
1469 return range;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001470}
1471
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001472static int lpi_range_cmp(void *priv, struct list_head *a, struct list_head *b)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001473{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001474 struct lpi_range *ra, *rb;
1475
1476 ra = container_of(a, struct lpi_range, entry);
1477 rb = container_of(b, struct lpi_range, entry);
1478
1479 return rb->base_id - ra->base_id;
1480}
1481
1482static void merge_lpi_ranges(void)
1483{
1484 struct lpi_range *range, *tmp;
1485
1486 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1487 if (!list_is_last(&range->entry, &lpi_range_list) &&
1488 (tmp->base_id == (range->base_id + range->span))) {
1489 tmp->base_id = range->base_id;
1490 tmp->span += range->span;
1491 list_del(&range->entry);
1492 kfree(range);
1493 }
1494 }
1495}
1496
1497static int alloc_lpi_range(u32 nr_lpis, u32 *base)
1498{
1499 struct lpi_range *range, *tmp;
1500 int err = -ENOSPC;
1501
1502 mutex_lock(&lpi_range_lock);
1503
1504 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1505 if (range->span >= nr_lpis) {
1506 *base = range->base_id;
1507 range->base_id += nr_lpis;
1508 range->span -= nr_lpis;
1509
1510 if (range->span == 0) {
1511 list_del(&range->entry);
1512 kfree(range);
1513 }
1514
1515 err = 0;
1516 break;
1517 }
1518 }
1519
1520 mutex_unlock(&lpi_range_lock);
1521
1522 pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
1523 return err;
1524}
1525
1526static int free_lpi_range(u32 base, u32 nr_lpis)
1527{
1528 struct lpi_range *new;
1529 int err = 0;
1530
1531 mutex_lock(&lpi_range_lock);
1532
1533 new = mk_lpi_range(base, nr_lpis);
1534 if (!new) {
1535 err = -ENOMEM;
1536 goto out;
1537 }
1538
1539 list_add(&new->entry, &lpi_range_list);
1540 list_sort(NULL, &lpi_range_list, lpi_range_cmp);
1541 merge_lpi_ranges();
1542out:
1543 mutex_unlock(&lpi_range_lock);
1544 return err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001545}
1546
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001547static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001548{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001549 u32 lpis = (1UL << id_bits) - 8192;
Marc Zyngier12b29052018-05-31 09:01:59 +01001550 u32 numlpis;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001551 int err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001552
Marc Zyngier12b29052018-05-31 09:01:59 +01001553 numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
1554
1555 if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
1556 lpis = numlpis;
1557 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
1558 lpis);
1559 }
1560
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001561 /*
1562 * Initializing the allocator is just the same as freeing the
1563 * full range of LPIs.
1564 */
1565 err = free_lpi_range(8192, lpis);
1566 pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
1567 return err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001568}
1569
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001570static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001571{
1572 unsigned long *bitmap = NULL;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001573 int err = 0;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001574
1575 do {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001576 err = alloc_lpi_range(nr_irqs, base);
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001577 if (!err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001578 break;
1579
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001580 nr_irqs /= 2;
1581 } while (nr_irqs > 0);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001582
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001583 if (err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001584 goto out;
1585
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001586 bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001587 if (!bitmap)
1588 goto out;
1589
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001590 *nr_ids = nr_irqs;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001591
1592out:
Marc Zyngierc8415b92015-10-02 16:44:05 +01001593 if (!bitmap)
1594 *base = *nr_ids = 0;
1595
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001596 return bitmap;
1597}
1598
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001599static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001600{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001601 WARN_ON(free_lpi_range(base, nr_ids));
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001602 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001603}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001604
Marc Zyngier053be482018-07-27 15:02:27 +01001605static void gic_reset_prop_table(void *va)
1606{
1607 /* Priority 0xa0, Group-1, disabled */
1608 memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ);
1609
1610 /* Make sure the GIC will observe the written configuration */
1611 gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ);
1612}
1613
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001614static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1615{
1616 struct page *prop_page;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001617
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001618 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1619 if (!prop_page)
1620 return NULL;
1621
Marc Zyngier053be482018-07-27 15:02:27 +01001622 gic_reset_prop_table(page_address(prop_page));
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001623
1624 return prop_page;
1625}
1626
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001627static void its_free_prop_table(struct page *prop_page)
1628{
1629 free_pages((unsigned long)page_address(prop_page),
1630 get_order(LPI_PROPBASE_SZ));
1631}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001632
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01001633static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
1634{
1635 phys_addr_t start, end, addr_end;
1636 u64 i;
1637
1638 /*
1639 * We don't bother checking for a kdump kernel as by
1640 * construction, the LPI tables are out of this kernel's
1641 * memory map.
1642 */
1643 if (is_kdump_kernel())
1644 return true;
1645
1646 addr_end = addr + size - 1;
1647
1648 for_each_reserved_mem_region(i, &start, &end) {
1649 if (addr >= start && addr_end <= end)
1650 return true;
1651 }
1652
1653 /* Not found, not a good sign... */
1654 pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n",
1655 &addr, &addr_end);
1656 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
1657 return false;
1658}
1659
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01001660static int gic_reserve_range(phys_addr_t addr, unsigned long size)
1661{
1662 if (efi_enabled(EFI_CONFIG_TABLES))
1663 return efi_mem_reserve_persistent(addr, size);
1664
1665 return 0;
1666}
1667
Marc Zyngier11e37d32018-07-27 13:38:54 +01001668static int __init its_setup_lpi_prop_table(void)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001669{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01001670 if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) {
1671 u64 val;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001672
Marc Zyngierc440a9d2018-07-27 15:40:13 +01001673 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
1674 lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1;
1675
1676 gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12);
1677 gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa,
1678 LPI_PROPBASE_SZ,
1679 MEMREMAP_WB);
1680 gic_reset_prop_table(gic_rdists->prop_table_va);
1681 } else {
1682 struct page *page;
1683
1684 lpi_id_bits = min_t(u32,
1685 GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
1686 ITS_MAX_LPI_NRBITS);
1687 page = its_allocate_prop_table(GFP_NOWAIT);
1688 if (!page) {
1689 pr_err("Failed to allocate PROPBASE\n");
1690 return -ENOMEM;
1691 }
1692
1693 gic_rdists->prop_table_pa = page_to_phys(page);
1694 gic_rdists->prop_table_va = page_address(page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01001695 WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa,
1696 LPI_PROPBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001697 }
1698
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001699 pr_info("GICv3: using LPI property table @%pa\n",
1700 &gic_rdists->prop_table_pa);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001701
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001702 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001703}
1704
1705static const char *its_base_type_string[] = {
1706 [GITS_BASER_TYPE_DEVICE] = "Devices",
1707 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00001708 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001709 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1710 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1711 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1712 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1713};
1714
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001715static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1716{
1717 u32 idx = baser - its->tables;
1718
Vladimir Murzin0968a612016-11-02 11:54:06 +00001719 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001720}
1721
1722static void its_write_baser(struct its_node *its, struct its_baser *baser,
1723 u64 val)
1724{
1725 u32 idx = baser - its->tables;
1726
Vladimir Murzin0968a612016-11-02 11:54:06 +00001727 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001728 baser->val = its_read_baser(its, baser);
1729}
1730
Shanker Donthineni93473592016-06-06 18:17:30 -05001731static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001732 u64 cache, u64 shr, u32 psz, u32 order,
1733 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05001734{
1735 u64 val = its_read_baser(its, baser);
1736 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1737 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001738 u64 baser_phys, tmp;
Shanker Donthineni93473592016-06-06 18:17:30 -05001739 u32 alloc_pages;
1740 void *base;
Shanker Donthineni93473592016-06-06 18:17:30 -05001741
1742retry_alloc_baser:
1743 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1744 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1745 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1746 &its->phys_base, its_base_type_string[type],
1747 alloc_pages, GITS_BASER_PAGES_MAX);
1748 alloc_pages = GITS_BASER_PAGES_MAX;
1749 order = get_order(GITS_BASER_PAGES_MAX * psz);
1750 }
1751
1752 base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, order);
1753 if (!base)
1754 return -ENOMEM;
1755
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001756 baser_phys = virt_to_phys(base);
1757
1758 /* Check if the physical address of the memory is above 48bits */
1759 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
1760
1761 /* 52bit PA is supported only when PageSize=64K */
1762 if (psz != SZ_64K) {
1763 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
1764 free_pages((unsigned long)base, order);
1765 return -ENXIO;
1766 }
1767
1768 /* Convert 52bit PA to 48bit field */
1769 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
1770 }
1771
Shanker Donthineni93473592016-06-06 18:17:30 -05001772retry_baser:
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001773 val = (baser_phys |
Shanker Donthineni93473592016-06-06 18:17:30 -05001774 (type << GITS_BASER_TYPE_SHIFT) |
1775 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1776 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1777 cache |
1778 shr |
1779 GITS_BASER_VALID);
1780
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001781 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1782
Shanker Donthineni93473592016-06-06 18:17:30 -05001783 switch (psz) {
1784 case SZ_4K:
1785 val |= GITS_BASER_PAGE_SIZE_4K;
1786 break;
1787 case SZ_16K:
1788 val |= GITS_BASER_PAGE_SIZE_16K;
1789 break;
1790 case SZ_64K:
1791 val |= GITS_BASER_PAGE_SIZE_64K;
1792 break;
1793 }
1794
1795 its_write_baser(its, baser, val);
1796 tmp = baser->val;
1797
1798 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1799 /*
1800 * Shareability didn't stick. Just use
1801 * whatever the read reported, which is likely
1802 * to be the only thing this redistributor
1803 * supports. If that's zero, make it
1804 * non-cacheable as well.
1805 */
1806 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1807 if (!shr) {
1808 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001809 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001810 }
1811 goto retry_baser;
1812 }
1813
1814 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1815 /*
1816 * Page size didn't stick. Let's try a smaller
1817 * size and retry. If we reach 4K, then
1818 * something is horribly wrong...
1819 */
1820 free_pages((unsigned long)base, order);
1821 baser->base = NULL;
1822
1823 switch (psz) {
1824 case SZ_16K:
1825 psz = SZ_4K;
1826 goto retry_alloc_baser;
1827 case SZ_64K:
1828 psz = SZ_16K;
1829 goto retry_alloc_baser;
1830 }
1831 }
1832
1833 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001834 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001835 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001836 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001837 free_pages((unsigned long)base, order);
1838 return -ENXIO;
1839 }
1840
1841 baser->order = order;
1842 baser->base = base;
1843 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001844 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001845
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001846 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001847 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001848 its_base_type_string[type],
1849 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001850 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001851 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1852
1853 return 0;
1854}
1855
Marc Zyngier4cacac52016-12-19 18:18:34 +00001856static bool its_parse_indirect_baser(struct its_node *its,
1857 struct its_baser *baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001858 u32 psz, u32 *order, u32 ids)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001859{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001860 u64 tmp = its_read_baser(its, baser);
1861 u64 type = GITS_BASER_TYPE(tmp);
1862 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001863 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001864 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001865 bool indirect = false;
1866
1867 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1868 if ((esz << ids) > (psz * 2)) {
1869 /*
1870 * Find out whether hw supports a single or two-level table by
1871 * table by reading bit at offset '62' after writing '1' to it.
1872 */
1873 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1874 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1875
1876 if (indirect) {
1877 /*
1878 * The size of the lvl2 table is equal to ITS page size
1879 * which is 'psz'. For computing lvl1 table size,
1880 * subtract ID bits that sparse lvl2 table from 'ids'
1881 * which is reported by ITS hardware times lvl1 table
1882 * entry size.
1883 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001884 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001885 esz = GITS_LVL1_ENTRY_SIZE;
1886 }
1887 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001888
1889 /*
1890 * Allocate as many entries as required to fit the
1891 * range of device IDs that the ITS can grok... The ID
1892 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001893 * massive waste of memory if two-level device table
1894 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001895 */
1896 new_order = max_t(u32, get_order(esz << ids), new_order);
1897 if (new_order >= MAX_ORDER) {
1898 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001899 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001900 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1901 &its->phys_base, its_base_type_string[type],
1902 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001903 }
1904
1905 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001906
1907 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001908}
1909
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001910static void its_free_tables(struct its_node *its)
1911{
1912 int i;
1913
1914 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001915 if (its->tables[i].base) {
1916 free_pages((unsigned long)its->tables[i].base,
1917 its->tables[i].order);
1918 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001919 }
1920 }
1921}
1922
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001923static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001924{
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001925 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001926 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001927 u32 psz = SZ_64K;
1928 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001929
Ard Biesheuvelfa150012017-10-17 17:55:54 +01001930 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
1931 /* erratum 24313: ignore memory access type */
1932 cache = GITS_BASER_nCnB;
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001933
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001934 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001935 struct its_baser *baser = its->tables + i;
1936 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001937 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001938 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001939 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001940
Marc Zyngier4cacac52016-12-19 18:18:34 +00001941 switch (type) {
1942 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001943 continue;
1944
Marc Zyngier4cacac52016-12-19 18:18:34 +00001945 case GITS_BASER_TYPE_DEVICE:
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001946 indirect = its_parse_indirect_baser(its, baser,
1947 psz, &order,
1948 its->device_ids);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001949 case GITS_BASER_TYPE_VCPU:
1950 indirect = its_parse_indirect_baser(its, baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001951 psz, &order,
1952 ITS_MAX_VPEID_BITS);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001953 break;
1954 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001955
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001956 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001957 if (err < 0) {
1958 its_free_tables(its);
1959 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001960 }
1961
Shanker Donthineni93473592016-06-06 18:17:30 -05001962 /* Update settings which will be used for next BASERn */
1963 psz = baser->psz;
1964 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1965 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001966 }
1967
1968 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001969}
1970
1971static int its_alloc_collections(struct its_node *its)
1972{
Marc Zyngier83559b42018-06-22 10:52:52 +01001973 int i;
1974
Kees Cook6396bb22018-06-12 14:03:40 -07001975 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001976 GFP_KERNEL);
1977 if (!its->collections)
1978 return -ENOMEM;
1979
Marc Zyngier83559b42018-06-22 10:52:52 +01001980 for (i = 0; i < nr_cpu_ids; i++)
1981 its->collections[i].target_address = ~0ULL;
1982
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001983 return 0;
1984}
1985
Marc Zyngier7c297a22016-12-19 18:34:38 +00001986static struct page *its_allocate_pending_table(gfp_t gfp_flags)
1987{
1988 struct page *pend_page;
Marc Zyngieradaab502018-07-17 18:06:39 +01001989
Marc Zyngier7c297a22016-12-19 18:34:38 +00001990 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
Marc Zyngieradaab502018-07-17 18:06:39 +01001991 get_order(LPI_PENDBASE_SZ));
Marc Zyngier7c297a22016-12-19 18:34:38 +00001992 if (!pend_page)
1993 return NULL;
1994
1995 /* Make sure the GIC will observe the zero-ed page */
1996 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
1997
1998 return pend_page;
1999}
2000
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002001static void its_free_pending_table(struct page *pt)
2002{
Marc Zyngieradaab502018-07-17 18:06:39 +01002003 free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ));
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002004}
2005
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002006/*
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002007 * Booting with kdump and LPIs enabled is generally fine. Any other
2008 * case is wrong in the absence of firmware/EFI support.
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002009 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002010static bool enabled_lpis_allowed(void)
2011{
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002012 phys_addr_t addr;
2013 u64 val;
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002014
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002015 /* Check whether the property table is in a reserved region */
2016 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2017 addr = val & GENMASK_ULL(51, 12);
2018
2019 return gic_check_reserved_range(addr, LPI_PROPBASE_SZ);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002020}
2021
Marc Zyngier11e37d32018-07-27 13:38:54 +01002022static int __init allocate_lpi_tables(void)
2023{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002024 u64 val;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002025 int err, cpu;
2026
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002027 /*
2028 * If LPIs are enabled while we run this from the boot CPU,
2029 * flag the RD tables as pre-allocated if the stars do align.
2030 */
2031 val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR);
2032 if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) {
2033 gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED |
2034 RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING);
2035 pr_info("GICv3: Using preallocated redistributor tables\n");
2036 }
2037
Marc Zyngier11e37d32018-07-27 13:38:54 +01002038 err = its_setup_lpi_prop_table();
2039 if (err)
2040 return err;
2041
2042 /*
2043 * We allocate all the pending tables anyway, as we may have a
2044 * mix of RDs that have had LPIs enabled, and some that
2045 * don't. We'll free the unused ones as each CPU comes online.
2046 */
2047 for_each_possible_cpu(cpu) {
2048 struct page *pend_page;
2049
2050 pend_page = its_allocate_pending_table(GFP_NOWAIT);
2051 if (!pend_page) {
2052 pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu);
2053 return -ENOMEM;
2054 }
2055
2056 gic_data_rdist_cpu(cpu)->pend_page = pend_page;
2057 }
2058
2059 return 0;
2060}
2061
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002062static void its_cpu_init_lpis(void)
2063{
2064 void __iomem *rbase = gic_data_rdist_rd_base();
2065 struct page *pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002066 phys_addr_t paddr;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002067 u64 val, tmp;
2068
Marc Zyngier11e37d32018-07-27 13:38:54 +01002069 if (gic_data_rdist()->lpi_enabled)
2070 return;
2071
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002072 val = readl_relaxed(rbase + GICR_CTLR);
2073 if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) &&
2074 (val & GICR_CTLR_ENABLE_LPIS)) {
Marc Zyngierf842ca82018-07-27 16:03:31 +01002075 /*
2076 * Check that we get the same property table on all
2077 * RDs. If we don't, this is hopeless.
2078 */
2079 paddr = gicr_read_propbaser(rbase + GICR_PROPBASER);
2080 paddr &= GENMASK_ULL(51, 12);
2081 if (WARN_ON(gic_rdists->prop_table_pa != paddr))
2082 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
2083
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002084 paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER);
2085 paddr &= GENMASK_ULL(51, 16);
2086
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002087 WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002088 its_free_pending_table(gic_data_rdist()->pend_page);
2089 gic_data_rdist()->pend_page = NULL;
2090
2091 goto out;
2092 }
2093
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002094 pend_page = gic_data_rdist()->pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002095 paddr = page_to_phys(pend_page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01002096 WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002097
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002098 /* set PROPBASE */
Marc Zyngiere1a2e202018-07-27 14:36:00 +01002099 val = (gic_rdists->prop_table_pa |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002100 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002101 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002102 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
2103
Vladimir Murzin0968a612016-11-02 11:54:06 +00002104 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2105 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002106
2107 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002108 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
2109 /*
2110 * The HW reports non-shareable, we must
2111 * remove the cacheability attributes as
2112 * well.
2113 */
2114 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
2115 GICR_PROPBASER_CACHEABILITY_MASK);
2116 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002117 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002118 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002119 pr_info_once("GIC: using cache flushing for LPI property table\n");
2120 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
2121 }
2122
2123 /* set PENDBASE */
2124 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002125 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002126 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002127
Vladimir Murzin0968a612016-11-02 11:54:06 +00002128 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2129 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002130
2131 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
2132 /*
2133 * The HW reports non-shareable, we must remove the
2134 * cacheability attributes as well.
2135 */
2136 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
2137 GICR_PENDBASER_CACHEABILITY_MASK);
2138 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002139 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002140 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002141
2142 /* Enable LPIs */
2143 val = readl_relaxed(rbase + GICR_CTLR);
2144 val |= GICR_CTLR_ENABLE_LPIS;
2145 writel_relaxed(val, rbase + GICR_CTLR);
2146
2147 /* Make sure the GIC has seen the above */
2148 dsb(sy);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002149out:
Marc Zyngier11e37d32018-07-27 13:38:54 +01002150 gic_data_rdist()->lpi_enabled = true;
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002151 pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n",
Marc Zyngier11e37d32018-07-27 13:38:54 +01002152 smp_processor_id(),
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002153 gic_data_rdist()->pend_page ? "allocated" : "reserved",
Marc Zyngier11e37d32018-07-27 13:38:54 +01002154 &paddr);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002155}
2156
Derek Basehore920181c2018-02-28 21:48:20 -08002157static void its_cpu_init_collection(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002158{
Derek Basehore920181c2018-02-28 21:48:20 -08002159 int cpu = smp_processor_id();
2160 u64 target;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002161
Derek Basehore920181c2018-02-28 21:48:20 -08002162 /* avoid cross node collections and its mapping */
2163 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
2164 struct device_node *cpu_node;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002165
Derek Basehore920181c2018-02-28 21:48:20 -08002166 cpu_node = of_get_cpu_node(cpu, NULL);
2167 if (its->numa_node != NUMA_NO_NODE &&
2168 its->numa_node != of_node_to_nid(cpu_node))
2169 return;
2170 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002171
Derek Basehore920181c2018-02-28 21:48:20 -08002172 /*
2173 * We now have to bind each collection to its target
2174 * redistributor.
2175 */
2176 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002177 /*
Derek Basehore920181c2018-02-28 21:48:20 -08002178 * This ITS wants the physical address of the
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002179 * redistributor.
2180 */
Derek Basehore920181c2018-02-28 21:48:20 -08002181 target = gic_data_rdist()->phys_base;
2182 } else {
2183 /* This ITS wants a linear CPU number. */
2184 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2185 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002186 }
2187
Derek Basehore920181c2018-02-28 21:48:20 -08002188 /* Perform collection mapping */
2189 its->collections[cpu].target_address = target;
2190 its->collections[cpu].col_id = cpu;
2191
2192 its_send_mapc(its, &its->collections[cpu], 1);
2193 its_send_invall(its, &its->collections[cpu]);
2194}
2195
2196static void its_cpu_init_collections(void)
2197{
2198 struct its_node *its;
2199
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02002200 raw_spin_lock(&its_lock);
Derek Basehore920181c2018-02-28 21:48:20 -08002201
2202 list_for_each_entry(its, &its_nodes, entry)
2203 its_cpu_init_collection(its);
2204
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02002205 raw_spin_unlock(&its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002206}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002207
2208static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
2209{
2210 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002211 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002212
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002213 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002214
2215 list_for_each_entry(tmp, &its->its_device_list, entry) {
2216 if (tmp->device_id == dev_id) {
2217 its_dev = tmp;
2218 break;
2219 }
2220 }
2221
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002222 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002223
2224 return its_dev;
2225}
2226
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002227static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2228{
2229 int i;
2230
2231 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2232 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2233 return &its->tables[i];
2234 }
2235
2236 return NULL;
2237}
2238
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002239static bool its_alloc_table_entry(struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002240{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002241 struct page *page;
2242 u32 esz, idx;
2243 __le64 *table;
2244
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002245 /* Don't allow device id that exceeds single, flat table limit */
2246 esz = GITS_BASER_ENTRY_SIZE(baser->val);
2247 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002248 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002249
2250 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002251 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002252 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
2253 return false;
2254
2255 table = baser->base;
2256
2257 /* Allocate memory for 2nd level table */
2258 if (!table[idx]) {
2259 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(baser->psz));
2260 if (!page)
2261 return false;
2262
2263 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2264 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00002265 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002266
2267 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2268
2269 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2270 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00002271 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002272
2273 /* Ensure updated table contents are visible to ITS hardware */
2274 dsb(sy);
2275 }
2276
2277 return true;
2278}
2279
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002280static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
2281{
2282 struct its_baser *baser;
2283
2284 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
2285
2286 /* Don't allow device id that exceeds ITS hardware limit */
2287 if (!baser)
2288 return (ilog2(dev_id) < its->device_ids);
2289
2290 return its_alloc_table_entry(baser, dev_id);
2291}
2292
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002293static bool its_alloc_vpe_table(u32 vpe_id)
2294{
2295 struct its_node *its;
2296
2297 /*
2298 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
2299 * could try and only do it on ITSs corresponding to devices
2300 * that have interrupts targeted at this VPE, but the
2301 * complexity becomes crazy (and you have tons of memory
2302 * anyway, right?).
2303 */
2304 list_for_each_entry(its, &its_nodes, entry) {
2305 struct its_baser *baser;
2306
2307 if (!its->is_v4)
2308 continue;
2309
2310 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
2311 if (!baser)
2312 return false;
2313
2314 if (!its_alloc_table_entry(baser, vpe_id))
2315 return false;
2316 }
2317
2318 return true;
2319}
2320
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002321static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002322 int nvecs, bool alloc_lpis)
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002323{
2324 struct its_device *dev;
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002325 unsigned long *lpi_map = NULL;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002326 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002327 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002328 void *itt;
2329 int lpi_base;
2330 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00002331 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002332 int sz;
2333
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002334 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002335 return NULL;
2336
Marc Zyngier147c8f32018-05-27 16:39:55 +01002337 if (WARN_ON(!is_power_of_2(nvecs)))
2338 nvecs = roundup_pow_of_two(nvecs);
2339
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002340 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00002341 /*
Marc Zyngier147c8f32018-05-27 16:39:55 +01002342 * Even if the device wants a single LPI, the ITT must be
2343 * sized as a power of two (and you need at least one bit...).
Marc Zyngierc8481262014-12-12 10:51:24 +00002344 */
Marc Zyngier147c8f32018-05-27 16:39:55 +01002345 nr_ites = max(2, nvecs);
Marc Zyngierc8481262014-12-12 10:51:24 +00002346 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002347 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Yun Wu6c834122015-03-06 16:37:46 +00002348 itt = kzalloc(sz, GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002349 if (alloc_lpis) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002350 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002351 if (lpi_map)
Kees Cook6396bb22018-06-12 14:03:40 -07002352 col_map = kcalloc(nr_lpis, sizeof(*col_map),
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002353 GFP_KERNEL);
2354 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07002355 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002356 nr_lpis = 0;
2357 lpi_base = 0;
2358 }
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002359
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002360 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002361 kfree(dev);
2362 kfree(itt);
2363 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002364 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002365 return NULL;
2366 }
2367
Vladimir Murzin328191c2016-11-02 11:54:05 +00002368 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01002369
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002370 dev->its = its;
2371 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00002372 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002373 dev->event_map.lpi_map = lpi_map;
2374 dev->event_map.col_map = col_map;
2375 dev->event_map.lpi_base = lpi_base;
2376 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00002377 mutex_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002378 dev->device_id = dev_id;
2379 INIT_LIST_HEAD(&dev->entry);
2380
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002381 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002382 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002383 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002384
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002385 /* Map device to its ITT */
2386 its_send_mapd(dev, 1);
2387
2388 return dev;
2389}
2390
2391static void its_free_device(struct its_device *its_dev)
2392{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002393 unsigned long flags;
2394
2395 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002396 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002397 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002398 kfree(its_dev->itt);
2399 kfree(its_dev);
2400}
Marc Zyngierb48ac832014-11-24 14:35:16 +00002401
2402static int its_alloc_device_irq(struct its_device *dev, irq_hw_number_t *hwirq)
2403{
2404 int idx;
2405
Marc Zyngier591e5be2015-07-17 10:46:42 +01002406 idx = find_first_zero_bit(dev->event_map.lpi_map,
2407 dev->event_map.nr_lpis);
2408 if (idx == dev->event_map.nr_lpis)
Marc Zyngierb48ac832014-11-24 14:35:16 +00002409 return -ENOSPC;
2410
Marc Zyngier591e5be2015-07-17 10:46:42 +01002411 *hwirq = dev->event_map.lpi_base + idx;
2412 set_bit(idx, dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002413
Marc Zyngierb48ac832014-11-24 14:35:16 +00002414 return 0;
2415}
2416
Marc Zyngier54456db2015-07-28 14:46:21 +01002417static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2418 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00002419{
Marc Zyngierb48ac832014-11-24 14:35:16 +00002420 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002421 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01002422 struct msi_domain_info *msi_info;
2423 u32 dev_id;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002424
Marc Zyngier54456db2015-07-28 14:46:21 +01002425 /*
2426 * We ignore "dev" entierely, and rely on the dev_id that has
2427 * been passed via the scratchpad. This limits this domain's
2428 * usefulness to upper layers that definitely know that they
2429 * are built on top of the ITS.
2430 */
2431 dev_id = info->scratchpad[0].ul;
2432
2433 msi_info = msi_get_domain_info(domain);
2434 its = msi_info->data;
2435
Marc Zyngier20b3d542016-12-20 15:23:22 +00002436 if (!gic_rdists->has_direct_lpi &&
2437 vpe_proxy.dev &&
2438 vpe_proxy.dev->its == its &&
2439 dev_id == vpe_proxy.dev->device_id) {
2440 /* Bad luck. Get yourself a better implementation */
2441 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
2442 dev_id);
2443 return -EINVAL;
2444 }
2445
Marc Zyngierf1304202015-07-28 14:46:18 +01002446 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002447 if (its_dev) {
2448 /*
2449 * We already have seen this ID, probably through
2450 * another alias (PCI bridge of some sort). No need to
2451 * create the device.
2452 */
Marc Zyngierf1304202015-07-28 14:46:18 +01002453 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002454 goto out;
2455 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002456
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002457 its_dev = its_create_device(its, dev_id, nvec, true);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002458 if (!its_dev)
2459 return -ENOMEM;
2460
Marc Zyngierf1304202015-07-28 14:46:18 +01002461 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00002462out:
Marc Zyngierb48ac832014-11-24 14:35:16 +00002463 info->scratchpad[0].ptr = its_dev;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002464 return 0;
2465}
2466
Marc Zyngier54456db2015-07-28 14:46:21 +01002467static struct msi_domain_ops its_msi_domain_ops = {
2468 .msi_prepare = its_msi_prepare,
2469};
2470
Marc Zyngierb48ac832014-11-24 14:35:16 +00002471static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2472 unsigned int virq,
2473 irq_hw_number_t hwirq)
2474{
Marc Zyngierf833f572015-10-13 12:51:33 +01002475 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002476
Marc Zyngierf833f572015-10-13 12:51:33 +01002477 if (irq_domain_get_of_node(domain->parent)) {
2478 fwspec.fwnode = domain->parent->fwnode;
2479 fwspec.param_count = 3;
2480 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2481 fwspec.param[1] = hwirq;
2482 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002483 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2484 fwspec.fwnode = domain->parent->fwnode;
2485 fwspec.param_count = 2;
2486 fwspec.param[0] = hwirq;
2487 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01002488 } else {
2489 return -EINVAL;
2490 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002491
Marc Zyngierf833f572015-10-13 12:51:33 +01002492 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002493}
2494
2495static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2496 unsigned int nr_irqs, void *args)
2497{
2498 msi_alloc_info_t *info = args;
2499 struct its_device *its_dev = info->scratchpad[0].ptr;
2500 irq_hw_number_t hwirq;
2501 int err;
2502 int i;
2503
2504 for (i = 0; i < nr_irqs; i++) {
2505 err = its_alloc_device_irq(its_dev, &hwirq);
2506 if (err)
2507 return err;
2508
2509 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq);
2510 if (err)
2511 return err;
2512
2513 irq_domain_set_hwirq_and_chip(domain, virq + i,
2514 hwirq, &its_irq_chip, its_dev);
Marc Zyngier0d224d32017-08-18 09:39:18 +01002515 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
Marc Zyngierf1304202015-07-28 14:46:18 +01002516 pr_debug("ID:%d pID:%d vID:%d\n",
2517 (int)(hwirq - its_dev->event_map.lpi_base),
2518 (int) hwirq, virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002519 }
2520
2521 return 0;
2522}
2523
Thomas Gleixner72491642017-09-13 23:29:10 +02002524static int its_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01002525 struct irq_data *d, bool reserve)
Marc Zyngieraca268d2014-12-12 10:51:23 +00002526{
2527 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2528 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002529 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngier0d224d32017-08-18 09:39:18 +01002530 int cpu;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002531
2532 /* get the cpu_mask of local node */
2533 if (its_dev->its->numa_node >= 0)
2534 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002535
Marc Zyngier591e5be2015-07-17 10:46:42 +01002536 /* Bind the LPI to the first possible CPU */
Yang Yingliangc1797b12018-06-22 10:52:51 +01002537 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
2538 if (cpu >= nr_cpu_ids) {
2539 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
2540 return -EINVAL;
2541
2542 cpu = cpumask_first(cpu_online_mask);
2543 }
2544
Marc Zyngier0d224d32017-08-18 09:39:18 +01002545 its_dev->event_map.col_map[event] = cpu;
2546 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngier591e5be2015-07-17 10:46:42 +01002547
Marc Zyngieraca268d2014-12-12 10:51:23 +00002548 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00002549 its_send_mapti(its_dev, d->hwirq, event);
Thomas Gleixner72491642017-09-13 23:29:10 +02002550 return 0;
Marc Zyngieraca268d2014-12-12 10:51:23 +00002551}
2552
2553static void its_irq_domain_deactivate(struct irq_domain *domain,
2554 struct irq_data *d)
2555{
2556 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2557 u32 event = its_get_event_id(d);
2558
2559 /* Stop the delivery of interrupts */
2560 its_send_discard(its_dev, event);
2561}
2562
Marc Zyngierb48ac832014-11-24 14:35:16 +00002563static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2564 unsigned int nr_irqs)
2565{
2566 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2567 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2568 int i;
2569
2570 for (i = 0; i < nr_irqs; i++) {
2571 struct irq_data *data = irq_domain_get_irq_data(domain,
2572 virq + i);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002573 u32 event = its_get_event_id(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002574
2575 /* Mark interrupt index as unused */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002576 clear_bit(event, its_dev->event_map.lpi_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002577
2578 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00002579 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002580 }
2581
2582 /* If all interrupts have been freed, start mopping the floor */
Marc Zyngier591e5be2015-07-17 10:46:42 +01002583 if (bitmap_empty(its_dev->event_map.lpi_map,
2584 its_dev->event_map.nr_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002585 its_lpi_free(its_dev->event_map.lpi_map,
2586 its_dev->event_map.lpi_base,
2587 its_dev->event_map.nr_lpis);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00002588 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002589
2590 /* Unmap device/itt */
2591 its_send_mapd(its_dev, 0);
2592 its_free_device(its_dev);
2593 }
2594
2595 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2596}
2597
2598static const struct irq_domain_ops its_domain_ops = {
2599 .alloc = its_irq_domain_alloc,
2600 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00002601 .activate = its_irq_domain_activate,
2602 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00002603};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002604
Marc Zyngier20b3d542016-12-20 15:23:22 +00002605/*
2606 * This is insane.
2607 *
2608 * If a GICv4 doesn't implement Direct LPIs (which is extremely
2609 * likely), the only way to perform an invalidate is to use a fake
2610 * device to issue an INV command, implying that the LPI has first
2611 * been mapped to some event on that device. Since this is not exactly
2612 * cheap, we try to keep that mapping around as long as possible, and
2613 * only issue an UNMAP if we're short on available slots.
2614 *
2615 * Broken by design(tm).
2616 */
2617static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
2618{
2619 /* Already unmapped? */
2620 if (vpe->vpe_proxy_event == -1)
2621 return;
2622
2623 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
2624 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
2625
2626 /*
2627 * We don't track empty slots at all, so let's move the
2628 * next_victim pointer if we can quickly reuse that slot
2629 * instead of nuking an existing entry. Not clear that this is
2630 * always a win though, and this might just generate a ripple
2631 * effect... Let's just hope VPEs don't migrate too often.
2632 */
2633 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2634 vpe_proxy.next_victim = vpe->vpe_proxy_event;
2635
2636 vpe->vpe_proxy_event = -1;
2637}
2638
2639static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
2640{
2641 if (!gic_rdists->has_direct_lpi) {
2642 unsigned long flags;
2643
2644 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2645 its_vpe_db_proxy_unmap_locked(vpe);
2646 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2647 }
2648}
2649
2650static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
2651{
2652 /* Already mapped? */
2653 if (vpe->vpe_proxy_event != -1)
2654 return;
2655
2656 /* This slot was already allocated. Kick the other VPE out. */
2657 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2658 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
2659
2660 /* Map the new VPE instead */
2661 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
2662 vpe->vpe_proxy_event = vpe_proxy.next_victim;
2663 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
2664
2665 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
2666 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
2667}
2668
Marc Zyngier958b90d2017-08-18 16:14:17 +01002669static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
2670{
2671 unsigned long flags;
2672 struct its_collection *target_col;
2673
2674 if (gic_rdists->has_direct_lpi) {
2675 void __iomem *rdbase;
2676
2677 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
2678 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2679 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2680 cpu_relax();
2681
2682 return;
2683 }
2684
2685 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2686
2687 its_vpe_db_proxy_map_locked(vpe);
2688
2689 target_col = &vpe_proxy.dev->its->collections[to];
2690 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
2691 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
2692
2693 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2694}
2695
Marc Zyngier3171a472016-12-20 15:17:28 +00002696static int its_vpe_set_affinity(struct irq_data *d,
2697 const struct cpumask *mask_val,
2698 bool force)
2699{
2700 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2701 int cpu = cpumask_first(mask_val);
2702
2703 /*
2704 * Changing affinity is mega expensive, so let's be as lazy as
Marc Zyngier20b3d542016-12-20 15:23:22 +00002705 * we can and only do it if we really have to. Also, if mapped
Marc Zyngier958b90d2017-08-18 16:14:17 +01002706 * into the proxy device, we need to move the doorbell
2707 * interrupt to its new location.
Marc Zyngier3171a472016-12-20 15:17:28 +00002708 */
2709 if (vpe->col_idx != cpu) {
Marc Zyngier958b90d2017-08-18 16:14:17 +01002710 int from = vpe->col_idx;
2711
Marc Zyngier3171a472016-12-20 15:17:28 +00002712 vpe->col_idx = cpu;
2713 its_send_vmovp(vpe);
Marc Zyngier958b90d2017-08-18 16:14:17 +01002714 its_vpe_db_proxy_move(vpe, from, cpu);
Marc Zyngier3171a472016-12-20 15:17:28 +00002715 }
2716
Marc Zyngier44c4c252017-10-19 10:11:34 +01002717 irq_data_update_effective_affinity(d, cpumask_of(cpu));
2718
Marc Zyngier3171a472016-12-20 15:17:28 +00002719 return IRQ_SET_MASK_OK_DONE;
2720}
2721
Marc Zyngiere643d802016-12-20 15:09:31 +00002722static void its_vpe_schedule(struct its_vpe *vpe)
2723{
Robin Murphy50c33092018-02-16 16:57:56 +00002724 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00002725 u64 val;
2726
2727 /* Schedule the VPE */
2728 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2729 GENMASK_ULL(51, 12);
2730 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2731 val |= GICR_VPROPBASER_RaWb;
2732 val |= GICR_VPROPBASER_InnerShareable;
2733 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2734
2735 val = virt_to_phys(page_address(vpe->vpt_page)) &
2736 GENMASK_ULL(51, 16);
2737 val |= GICR_VPENDBASER_RaWaWb;
2738 val |= GICR_VPENDBASER_NonShareable;
2739 /*
2740 * There is no good way of finding out if the pending table is
2741 * empty as we can race against the doorbell interrupt very
2742 * easily. So in the end, vpe->pending_last is only an
2743 * indication that the vcpu has something pending, not one
2744 * that the pending table is empty. A good implementation
2745 * would be able to read its coarse map pretty quickly anyway,
2746 * making this a tolerable issue.
2747 */
2748 val |= GICR_VPENDBASER_PendingLast;
2749 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2750 val |= GICR_VPENDBASER_Valid;
2751 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2752}
2753
2754static void its_vpe_deschedule(struct its_vpe *vpe)
2755{
Robin Murphy50c33092018-02-16 16:57:56 +00002756 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00002757 u32 count = 1000000; /* 1s! */
2758 bool clean;
2759 u64 val;
2760
2761 /* We're being scheduled out */
2762 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2763 val &= ~GICR_VPENDBASER_Valid;
2764 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2765
2766 do {
2767 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2768 clean = !(val & GICR_VPENDBASER_Dirty);
2769 if (!clean) {
2770 count--;
2771 cpu_relax();
2772 udelay(1);
2773 }
2774 } while (!clean && count);
2775
2776 if (unlikely(!clean && !count)) {
2777 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2778 vpe->idai = false;
2779 vpe->pending_last = true;
2780 } else {
2781 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2782 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2783 }
2784}
2785
Marc Zyngier40619a22017-10-08 15:16:09 +01002786static void its_vpe_invall(struct its_vpe *vpe)
2787{
2788 struct its_node *its;
2789
2790 list_for_each_entry(its, &its_nodes, entry) {
2791 if (!its->is_v4)
2792 continue;
2793
Marc Zyngier2247e1b2017-10-08 18:50:36 +01002794 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
2795 continue;
2796
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01002797 /*
2798 * Sending a VINVALL to a single ITS is enough, as all
2799 * we need is to reach the redistributors.
2800 */
Marc Zyngier40619a22017-10-08 15:16:09 +01002801 its_send_vinvall(its, vpe);
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01002802 return;
Marc Zyngier40619a22017-10-08 15:16:09 +01002803 }
2804}
2805
Marc Zyngiere643d802016-12-20 15:09:31 +00002806static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2807{
2808 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2809 struct its_cmd_info *info = vcpu_info;
2810
2811 switch (info->cmd_type) {
2812 case SCHEDULE_VPE:
2813 its_vpe_schedule(vpe);
2814 return 0;
2815
2816 case DESCHEDULE_VPE:
2817 its_vpe_deschedule(vpe);
2818 return 0;
2819
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002820 case INVALL_VPE:
Marc Zyngier40619a22017-10-08 15:16:09 +01002821 its_vpe_invall(vpe);
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002822 return 0;
2823
Marc Zyngiere643d802016-12-20 15:09:31 +00002824 default:
2825 return -EINVAL;
2826 }
2827}
2828
Marc Zyngier20b3d542016-12-20 15:23:22 +00002829static void its_vpe_send_cmd(struct its_vpe *vpe,
2830 void (*cmd)(struct its_device *, u32))
2831{
2832 unsigned long flags;
2833
2834 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2835
2836 its_vpe_db_proxy_map_locked(vpe);
2837 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
2838
2839 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2840}
2841
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002842static void its_vpe_send_inv(struct irq_data *d)
2843{
2844 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002845
Marc Zyngier20b3d542016-12-20 15:23:22 +00002846 if (gic_rdists->has_direct_lpi) {
2847 void __iomem *rdbase;
2848
2849 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2850 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
2851 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2852 cpu_relax();
2853 } else {
2854 its_vpe_send_cmd(vpe, its_send_inv);
2855 }
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002856}
2857
2858static void its_vpe_mask_irq(struct irq_data *d)
2859{
2860 /*
2861 * We need to unmask the LPI, which is described by the parent
2862 * irq_data. Instead of calling into the parent (which won't
2863 * exactly do the right thing, let's simply use the
2864 * parent_data pointer. Yes, I'm naughty.
2865 */
2866 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
2867 its_vpe_send_inv(d);
2868}
2869
2870static void its_vpe_unmask_irq(struct irq_data *d)
2871{
2872 /* Same hack as above... */
2873 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
2874 its_vpe_send_inv(d);
2875}
2876
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002877static int its_vpe_set_irqchip_state(struct irq_data *d,
2878 enum irqchip_irq_state which,
2879 bool state)
2880{
2881 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2882
2883 if (which != IRQCHIP_STATE_PENDING)
2884 return -EINVAL;
2885
2886 if (gic_rdists->has_direct_lpi) {
2887 void __iomem *rdbase;
2888
2889 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2890 if (state) {
2891 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
2892 } else {
2893 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2894 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2895 cpu_relax();
2896 }
2897 } else {
2898 if (state)
2899 its_vpe_send_cmd(vpe, its_send_int);
2900 else
2901 its_vpe_send_cmd(vpe, its_send_clear);
2902 }
2903
2904 return 0;
2905}
2906
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002907static struct irq_chip its_vpe_irq_chip = {
2908 .name = "GICv4-vpe",
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002909 .irq_mask = its_vpe_mask_irq,
2910 .irq_unmask = its_vpe_unmask_irq,
2911 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier3171a472016-12-20 15:17:28 +00002912 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002913 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
Marc Zyngiere643d802016-12-20 15:09:31 +00002914 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002915};
2916
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002917static int its_vpe_id_alloc(void)
2918{
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002919 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002920}
2921
2922static void its_vpe_id_free(u16 id)
2923{
2924 ida_simple_remove(&its_vpeid_ida, id);
2925}
2926
2927static int its_vpe_init(struct its_vpe *vpe)
2928{
2929 struct page *vpt_page;
2930 int vpe_id;
2931
2932 /* Allocate vpe_id */
2933 vpe_id = its_vpe_id_alloc();
2934 if (vpe_id < 0)
2935 return vpe_id;
2936
2937 /* Allocate VPT */
2938 vpt_page = its_allocate_pending_table(GFP_KERNEL);
2939 if (!vpt_page) {
2940 its_vpe_id_free(vpe_id);
2941 return -ENOMEM;
2942 }
2943
2944 if (!its_alloc_vpe_table(vpe_id)) {
2945 its_vpe_id_free(vpe_id);
2946 its_free_pending_table(vpe->vpt_page);
2947 return -ENOMEM;
2948 }
2949
2950 vpe->vpe_id = vpe_id;
2951 vpe->vpt_page = vpt_page;
Marc Zyngier20b3d542016-12-20 15:23:22 +00002952 vpe->vpe_proxy_event = -1;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002953
2954 return 0;
2955}
2956
2957static void its_vpe_teardown(struct its_vpe *vpe)
2958{
Marc Zyngier20b3d542016-12-20 15:23:22 +00002959 its_vpe_db_proxy_unmap(vpe);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002960 its_vpe_id_free(vpe->vpe_id);
2961 its_free_pending_table(vpe->vpt_page);
2962}
2963
2964static void its_vpe_irq_domain_free(struct irq_domain *domain,
2965 unsigned int virq,
2966 unsigned int nr_irqs)
2967{
2968 struct its_vm *vm = domain->host_data;
2969 int i;
2970
2971 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2972
2973 for (i = 0; i < nr_irqs; i++) {
2974 struct irq_data *data = irq_domain_get_irq_data(domain,
2975 virq + i);
2976 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
2977
2978 BUG_ON(vm != vpe->its_vm);
2979
2980 clear_bit(data->hwirq, vm->db_bitmap);
2981 its_vpe_teardown(vpe);
2982 irq_domain_reset_irq_data(data);
2983 }
2984
2985 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002986 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002987 its_free_prop_table(vm->vprop_page);
2988 }
2989}
2990
2991static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2992 unsigned int nr_irqs, void *args)
2993{
2994 struct its_vm *vm = args;
2995 unsigned long *bitmap;
2996 struct page *vprop_page;
2997 int base, nr_ids, i, err = 0;
2998
2999 BUG_ON(!vm);
3000
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003001 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003002 if (!bitmap)
3003 return -ENOMEM;
3004
3005 if (nr_ids < nr_irqs) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003006 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003007 return -ENOMEM;
3008 }
3009
3010 vprop_page = its_allocate_prop_table(GFP_KERNEL);
3011 if (!vprop_page) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003012 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003013 return -ENOMEM;
3014 }
3015
3016 vm->db_bitmap = bitmap;
3017 vm->db_lpi_base = base;
3018 vm->nr_db_lpis = nr_ids;
3019 vm->vprop_page = vprop_page;
3020
3021 for (i = 0; i < nr_irqs; i++) {
3022 vm->vpes[i]->vpe_db_lpi = base + i;
3023 err = its_vpe_init(vm->vpes[i]);
3024 if (err)
3025 break;
3026 err = its_irq_gic_domain_alloc(domain, virq + i,
3027 vm->vpes[i]->vpe_db_lpi);
3028 if (err)
3029 break;
3030 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
3031 &its_vpe_irq_chip, vm->vpes[i]);
3032 set_bit(i, bitmap);
3033 }
3034
3035 if (err) {
3036 if (i > 0)
3037 its_vpe_irq_domain_free(domain, virq, i - 1);
3038
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003039 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003040 its_free_prop_table(vprop_page);
3041 }
3042
3043 return err;
3044}
3045
Thomas Gleixner72491642017-09-13 23:29:10 +02003046static int its_vpe_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01003047 struct irq_data *d, bool reserve)
Marc Zyngiereb781922016-12-20 14:47:05 +00003048{
3049 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier40619a22017-10-08 15:16:09 +01003050 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00003051
Marc Zyngier2247e1b2017-10-08 18:50:36 +01003052 /* If we use the list map, we issue VMAPP on demand... */
3053 if (its_list_map)
Marc Zyngier6ef930f2017-11-07 10:04:38 +00003054 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00003055
3056 /* Map the VPE to the first possible CPU */
3057 vpe->col_idx = cpumask_first(cpu_online_mask);
Marc Zyngier40619a22017-10-08 15:16:09 +01003058
3059 list_for_each_entry(its, &its_nodes, entry) {
3060 if (!its->is_v4)
3061 continue;
3062
Marc Zyngier75fd9512017-10-08 18:46:39 +01003063 its_send_vmapp(its, vpe, true);
Marc Zyngier40619a22017-10-08 15:16:09 +01003064 its_send_vinvall(its, vpe);
3065 }
3066
Marc Zyngier44c4c252017-10-19 10:11:34 +01003067 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
3068
Thomas Gleixner72491642017-09-13 23:29:10 +02003069 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00003070}
3071
3072static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
3073 struct irq_data *d)
3074{
3075 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier75fd9512017-10-08 18:46:39 +01003076 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00003077
Marc Zyngier2247e1b2017-10-08 18:50:36 +01003078 /*
3079 * If we use the list map, we unmap the VPE once no VLPIs are
3080 * associated with the VM.
3081 */
3082 if (its_list_map)
3083 return;
3084
Marc Zyngier75fd9512017-10-08 18:46:39 +01003085 list_for_each_entry(its, &its_nodes, entry) {
3086 if (!its->is_v4)
3087 continue;
3088
3089 its_send_vmapp(its, vpe, false);
3090 }
Marc Zyngiereb781922016-12-20 14:47:05 +00003091}
3092
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003093static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003094 .alloc = its_vpe_irq_domain_alloc,
3095 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00003096 .activate = its_vpe_irq_domain_activate,
3097 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003098};
3099
Yun Wu4559fbb2015-03-06 16:37:50 +00003100static int its_force_quiescent(void __iomem *base)
3101{
3102 u32 count = 1000000; /* 1s */
3103 u32 val;
3104
3105 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07003106 /*
3107 * GIC architecture specification requires the ITS to be both
3108 * disabled and quiescent for writes to GITS_BASER<n> or
3109 * GITS_CBASER to not have UNPREDICTABLE results.
3110 */
3111 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00003112 return 0;
3113
3114 /* Disable the generation of all interrupts to this ITS */
Marc Zyngierd51c4b42017-06-27 21:24:25 +01003115 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
Yun Wu4559fbb2015-03-06 16:37:50 +00003116 writel_relaxed(val, base + GITS_CTLR);
3117
3118 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
3119 while (1) {
3120 val = readl_relaxed(base + GITS_CTLR);
3121 if (val & GITS_CTLR_QUIESCENT)
3122 return 0;
3123
3124 count--;
3125 if (!count)
3126 return -EBUSY;
3127
3128 cpu_relax();
3129 udelay(1);
3130 }
3131}
3132
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003133static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
Robert Richter94100972015-09-21 22:58:38 +02003134{
3135 struct its_node *its = data;
3136
Ard Biesheuvelfa150012017-10-17 17:55:54 +01003137 /* erratum 22375: only alloc 8MB table size */
3138 its->device_ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02003139 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003140
3141 return true;
Robert Richter94100972015-09-21 22:58:38 +02003142}
3143
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003144static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003145{
3146 struct its_node *its = data;
3147
3148 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003149
3150 return true;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003151}
3152
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003153static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
Shanker Donthineni90922a22017-03-07 08:20:38 -06003154{
3155 struct its_node *its = data;
3156
3157 /* On QDF2400, the size of the ITE is 16Bytes */
3158 its->ite_size = 16;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003159
3160 return true;
Shanker Donthineni90922a22017-03-07 08:20:38 -06003161}
3162
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003163static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
3164{
3165 struct its_node *its = its_dev->its;
3166
3167 /*
3168 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
3169 * which maps 32-bit writes targeted at a separate window of
3170 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
3171 * with device ID taken from bits [device_id_bits + 1:2] of
3172 * the window offset.
3173 */
3174 return its->pre_its_base + (its_dev->device_id << 2);
3175}
3176
3177static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
3178{
3179 struct its_node *its = data;
3180 u32 pre_its_window[2];
3181 u32 ids;
3182
3183 if (!fwnode_property_read_u32_array(its->fwnode_handle,
3184 "socionext,synquacer-pre-its",
3185 pre_its_window,
3186 ARRAY_SIZE(pre_its_window))) {
3187
3188 its->pre_its_base = pre_its_window[0];
3189 its->get_msi_base = its_irq_get_msi_base_pre_its;
3190
3191 ids = ilog2(pre_its_window[1]) - 2;
3192 if (its->device_ids > ids)
3193 its->device_ids = ids;
3194
3195 /* the pre-ITS breaks isolation, so disable MSI remapping */
3196 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
3197 return true;
3198 }
3199 return false;
3200}
3201
Marc Zyngier5c9a8822017-07-28 21:20:37 +01003202static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
3203{
3204 struct its_node *its = data;
3205
3206 /*
3207 * Hip07 insists on using the wrong address for the VLPI
3208 * page. Trick it into doing the right thing...
3209 */
3210 its->vlpi_redist_offset = SZ_128K;
3211 return true;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00003212}
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003213
Robert Richter67510cc2015-09-21 22:58:37 +02003214static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02003215#ifdef CONFIG_CAVIUM_ERRATUM_22375
3216 {
3217 .desc = "ITS: Cavium errata 22375, 24313",
3218 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3219 .mask = 0xffff0fff,
3220 .init = its_enable_quirk_cavium_22375,
3221 },
3222#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003223#ifdef CONFIG_CAVIUM_ERRATUM_23144
3224 {
3225 .desc = "ITS: Cavium erratum 23144",
3226 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3227 .mask = 0xffff0fff,
3228 .init = its_enable_quirk_cavium_23144,
3229 },
3230#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06003231#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
3232 {
3233 .desc = "ITS: QDF2400 erratum 0065",
3234 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
3235 .mask = 0xffffffff,
3236 .init = its_enable_quirk_qdf2400_e0065,
3237 },
3238#endif
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003239#ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
3240 {
3241 /*
3242 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
3243 * implementation, but with a 'pre-ITS' added that requires
3244 * special handling in software.
3245 */
3246 .desc = "ITS: Socionext Synquacer pre-ITS",
3247 .iidr = 0x0001143b,
3248 .mask = 0xffffffff,
3249 .init = its_enable_quirk_socionext_synquacer,
3250 },
3251#endif
Marc Zyngier5c9a8822017-07-28 21:20:37 +01003252#ifdef CONFIG_HISILICON_ERRATUM_161600802
3253 {
3254 .desc = "ITS: Hip07 erratum 161600802",
3255 .iidr = 0x00000004,
3256 .mask = 0xffffffff,
3257 .init = its_enable_quirk_hip07_161600802,
3258 },
3259#endif
Robert Richter67510cc2015-09-21 22:58:37 +02003260 {
3261 }
3262};
3263
3264static void its_enable_quirks(struct its_node *its)
3265{
3266 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
3267
3268 gic_enable_quirks(iidr, its_quirks, its);
3269}
3270
Derek Basehoredba0bc72018-02-28 21:48:18 -08003271static int its_save_disable(void)
3272{
3273 struct its_node *its;
3274 int err = 0;
3275
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003276 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003277 list_for_each_entry(its, &its_nodes, entry) {
3278 void __iomem *base;
3279
3280 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3281 continue;
3282
3283 base = its->base;
3284 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
3285 err = its_force_quiescent(base);
3286 if (err) {
3287 pr_err("ITS@%pa: failed to quiesce: %d\n",
3288 &its->phys_base, err);
3289 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3290 goto err;
3291 }
3292
3293 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
3294 }
3295
3296err:
3297 if (err) {
3298 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
3299 void __iomem *base;
3300
3301 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3302 continue;
3303
3304 base = its->base;
3305 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3306 }
3307 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003308 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003309
3310 return err;
3311}
3312
3313static void its_restore_enable(void)
3314{
3315 struct its_node *its;
3316 int ret;
3317
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003318 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003319 list_for_each_entry(its, &its_nodes, entry) {
3320 void __iomem *base;
3321 int i;
3322
3323 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3324 continue;
3325
3326 base = its->base;
3327
3328 /*
3329 * Make sure that the ITS is disabled. If it fails to quiesce,
3330 * don't restore it since writing to CBASER or BASER<n>
3331 * registers is undefined according to the GIC v3 ITS
3332 * Specification.
3333 */
3334 ret = its_force_quiescent(base);
3335 if (ret) {
3336 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
3337 &its->phys_base, ret);
3338 continue;
3339 }
3340
3341 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
3342
3343 /*
3344 * Writing CBASER resets CREADR to 0, so make CWRITER and
3345 * cmd_write line up with it.
3346 */
3347 its->cmd_write = its->cmd_base;
3348 gits_write_cwriter(0, base + GITS_CWRITER);
3349
3350 /* Restore GITS_BASER from the value cache. */
3351 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
3352 struct its_baser *baser = &its->tables[i];
3353
3354 if (!(baser->val & GITS_BASER_VALID))
3355 continue;
3356
3357 its_write_baser(its, baser, baser->val);
3358 }
3359 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
Derek Basehore920181c2018-02-28 21:48:20 -08003360
3361 /*
3362 * Reinit the collection if it's stored in the ITS. This is
3363 * indicated by the col_id being less than the HCC field.
3364 * CID < HCC as specified in the GIC v3 Documentation.
3365 */
3366 if (its->collections[smp_processor_id()].col_id <
3367 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
3368 its_cpu_init_collection(its);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003369 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003370 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003371}
3372
3373static struct syscore_ops its_syscore_ops = {
3374 .suspend = its_save_disable,
3375 .resume = its_restore_enable,
3376};
3377
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003378static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003379{
3380 struct irq_domain *inner_domain;
3381 struct msi_domain_info *info;
3382
3383 info = kzalloc(sizeof(*info), GFP_KERNEL);
3384 if (!info)
3385 return -ENOMEM;
3386
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003387 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003388 if (!inner_domain) {
3389 kfree(info);
3390 return -ENOMEM;
3391 }
3392
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003393 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01003394 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003395 inner_domain->flags |= its->msi_domain_flags;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003396 info->ops = &its_msi_domain_ops;
3397 info->data = its;
3398 inner_domain->host_data = info;
3399
3400 return 0;
3401}
3402
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003403static int its_init_vpe_domain(void)
3404{
Marc Zyngier20b3d542016-12-20 15:23:22 +00003405 struct its_node *its;
3406 u32 devid;
3407 int entries;
3408
3409 if (gic_rdists->has_direct_lpi) {
3410 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
3411 return 0;
3412 }
3413
3414 /* Any ITS will do, even if not v4 */
3415 its = list_first_entry(&its_nodes, struct its_node, entry);
3416
3417 entries = roundup_pow_of_two(nr_cpu_ids);
Kees Cook6396bb22018-06-12 14:03:40 -07003418 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
Marc Zyngier20b3d542016-12-20 15:23:22 +00003419 GFP_KERNEL);
3420 if (!vpe_proxy.vpes) {
3421 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
3422 return -ENOMEM;
3423 }
3424
3425 /* Use the last possible DevID */
3426 devid = GENMASK(its->device_ids - 1, 0);
3427 vpe_proxy.dev = its_create_device(its, devid, entries, false);
3428 if (!vpe_proxy.dev) {
3429 kfree(vpe_proxy.vpes);
3430 pr_err("ITS: Can't allocate GICv4 proxy device\n");
3431 return -ENOMEM;
3432 }
3433
Shanker Donthinenic427a472017-09-23 13:50:19 -05003434 BUG_ON(entries > vpe_proxy.dev->nr_ites);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003435
3436 raw_spin_lock_init(&vpe_proxy.lock);
3437 vpe_proxy.next_victim = 0;
3438 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
3439 devid, vpe_proxy.dev->nr_ites);
3440
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003441 return 0;
3442}
3443
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003444static int __init its_compute_its_list_map(struct resource *res,
3445 void __iomem *its_base)
3446{
3447 int its_number;
3448 u32 ctlr;
3449
3450 /*
3451 * This is assumed to be done early enough that we're
3452 * guaranteed to be single-threaded, hence no
3453 * locking. Should this change, we should address
3454 * this.
3455 */
Marc Zyngierab604912017-10-08 18:48:06 +01003456 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
3457 if (its_number >= GICv4_ITS_LIST_MAX) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003458 pr_err("ITS@%pa: No ITSList entry available!\n",
3459 &res->start);
3460 return -EINVAL;
3461 }
3462
3463 ctlr = readl_relaxed(its_base + GITS_CTLR);
3464 ctlr &= ~GITS_CTLR_ITS_NUMBER;
3465 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
3466 writel_relaxed(ctlr, its_base + GITS_CTLR);
3467 ctlr = readl_relaxed(its_base + GITS_CTLR);
3468 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
3469 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
3470 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
3471 }
3472
3473 if (test_and_set_bit(its_number, &its_list_map)) {
3474 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
3475 &res->start, its_number);
3476 return -EINVAL;
3477 }
3478
3479 return its_number;
3480}
3481
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003482static int __init its_probe_one(struct resource *res,
3483 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003484{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003485 struct its_node *its;
3486 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003487 u32 val, ctlr;
3488 u64 baser, tmp, typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003489 int err;
3490
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003491 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003492 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003493 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003494 return -ENOMEM;
3495 }
3496
3497 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
3498 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003499 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003500 err = -ENODEV;
3501 goto out_unmap;
3502 }
3503
Yun Wu4559fbb2015-03-06 16:37:50 +00003504 err = its_force_quiescent(its_base);
3505 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003506 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00003507 goto out_unmap;
3508 }
3509
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003510 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003511
3512 its = kzalloc(sizeof(*its), GFP_KERNEL);
3513 if (!its) {
3514 err = -ENOMEM;
3515 goto out_unmap;
3516 }
3517
3518 raw_spin_lock_init(&its->lock);
3519 INIT_LIST_HEAD(&its->entry);
3520 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003521 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003522 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003523 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003524 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
Ard Biesheuvelfa150012017-10-17 17:55:54 +01003525 its->device_ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003526 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
3527 if (its->is_v4) {
3528 if (!(typer & GITS_TYPER_VMOVP)) {
3529 err = its_compute_its_list_map(res, its_base);
3530 if (err < 0)
3531 goto out_free_its;
3532
Marc Zyngierdebf6d02017-10-08 18:44:42 +01003533 its->list_nr = err;
3534
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003535 pr_info("ITS@%pa: Using ITS number %d\n",
3536 &res->start, err);
3537 } else {
3538 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
3539 }
3540 }
3541
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003542 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003543
Robert Richter5bc13c22017-02-01 18:38:25 +01003544 its->cmd_base = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO,
3545 get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003546 if (!its->cmd_base) {
3547 err = -ENOMEM;
3548 goto out_free_its;
3549 }
3550 its->cmd_write = its->cmd_base;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003551 its->fwnode_handle = handle;
3552 its->get_msi_base = its_irq_get_msi_base;
3553 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003554
Robert Richter67510cc2015-09-21 22:58:37 +02003555 its_enable_quirks(its);
3556
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05003557 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003558 if (err)
3559 goto out_free_cmd;
3560
3561 err = its_alloc_collections(its);
3562 if (err)
3563 goto out_free_tables;
3564
3565 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06003566 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003567 GITS_CBASER_InnerShareable |
3568 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
3569 GITS_CBASER_VALID);
3570
Vladimir Murzin0968a612016-11-02 11:54:06 +00003571 gits_write_cbaser(baser, its->base + GITS_CBASER);
3572 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003573
Marc Zyngier4ad3e362015-03-27 14:15:04 +00003574 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00003575 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
3576 /*
3577 * The HW reports non-shareable, we must
3578 * remove the cacheability attributes as
3579 * well.
3580 */
3581 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
3582 GITS_CBASER_CACHEABILITY_MASK);
3583 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00003584 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00003585 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003586 pr_info("ITS: using cache flushing for cmd queue\n");
3587 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
3588 }
3589
Vladimir Murzin0968a612016-11-02 11:54:06 +00003590 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003591 ctlr = readl_relaxed(its->base + GITS_CTLR);
Marc Zyngierd51c4b42017-06-27 21:24:25 +01003592 ctlr |= GITS_CTLR_ENABLE;
3593 if (its->is_v4)
3594 ctlr |= GITS_CTLR_ImDe;
3595 writel_relaxed(ctlr, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00003596
Derek Basehoredba0bc72018-02-28 21:48:18 -08003597 if (GITS_TYPER_HCC(typer))
3598 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
3599
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003600 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003601 if (err)
3602 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003603
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003604 raw_spin_lock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003605 list_add(&its->entry, &its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003606 raw_spin_unlock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003607
3608 return 0;
3609
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003610out_free_tables:
3611 its_free_tables(its);
3612out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01003613 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003614out_free_its:
3615 kfree(its);
3616out_unmap:
3617 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003618 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003619 return err;
3620}
3621
3622static bool gic_rdists_supports_plpis(void)
3623{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01003624 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003625}
3626
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003627static int redist_disable_lpis(void)
3628{
3629 void __iomem *rbase = gic_data_rdist_rd_base();
3630 u64 timeout = USEC_PER_SEC;
3631 u64 val;
3632
3633 if (!gic_rdists_supports_plpis()) {
3634 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
3635 return -ENXIO;
3636 }
3637
3638 val = readl_relaxed(rbase + GICR_CTLR);
3639 if (!(val & GICR_CTLR_ENABLE_LPIS))
3640 return 0;
3641
Marc Zyngier11e37d32018-07-27 13:38:54 +01003642 /*
3643 * If coming via a CPU hotplug event, we don't need to disable
3644 * LPIs before trying to re-enable them. They are already
3645 * configured and all is well in the world.
Marc Zyngierc440a9d2018-07-27 15:40:13 +01003646 *
3647 * If running with preallocated tables, there is nothing to do.
Marc Zyngier11e37d32018-07-27 13:38:54 +01003648 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01003649 if (gic_data_rdist()->lpi_enabled ||
3650 (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED))
Marc Zyngier11e37d32018-07-27 13:38:54 +01003651 return 0;
3652
3653 /*
3654 * From that point on, we only try to do some damage control.
3655 */
3656 pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003657 smp_processor_id());
3658 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
3659
3660 /* Disable LPIs */
3661 val &= ~GICR_CTLR_ENABLE_LPIS;
3662 writel_relaxed(val, rbase + GICR_CTLR);
3663
3664 /* Make sure any change to GICR_CTLR is observable by the GIC */
3665 dsb(sy);
3666
3667 /*
3668 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
3669 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
3670 * Error out if we time out waiting for RWP to clear.
3671 */
3672 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
3673 if (!timeout) {
3674 pr_err("CPU%d: Timeout while disabling LPIs\n",
3675 smp_processor_id());
3676 return -ETIMEDOUT;
3677 }
3678 udelay(1);
3679 timeout--;
3680 }
3681
3682 /*
3683 * After it has been written to 1, it is IMPLEMENTATION
3684 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
3685 * cleared to 0. Error out if clearing the bit failed.
3686 */
3687 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
3688 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
3689 return -EBUSY;
3690 }
3691
3692 return 0;
3693}
3694
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003695int its_cpu_init(void)
3696{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003697 if (!list_empty(&its_nodes)) {
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003698 int ret;
3699
3700 ret = redist_disable_lpis();
3701 if (ret)
3702 return ret;
3703
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003704 its_cpu_init_lpis();
Derek Basehore920181c2018-02-28 21:48:20 -08003705 its_cpu_init_collections();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003706 }
3707
3708 return 0;
3709}
3710
Arvind Yadav935bba72017-06-22 16:05:30 +05303711static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003712 { .compatible = "arm,gic-v3-its", },
3713 {},
3714};
3715
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003716static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003717{
3718 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003719 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003720
3721 for (np = of_find_matching_node(node, its_device_id); np;
3722 np = of_find_matching_node(np, its_device_id)) {
Stephen Boyd95a25622018-02-01 09:03:29 -08003723 if (!of_device_is_available(np))
3724 continue;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003725 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003726 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
3727 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003728 continue;
3729 }
3730
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003731 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003732 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003733 continue;
3734 }
3735
3736 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003737 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003738 return 0;
3739}
3740
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003741#ifdef CONFIG_ACPI
3742
3743#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
3744
Robert Richterd1ce2632017-07-12 15:25:09 +02003745#ifdef CONFIG_ACPI_NUMA
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303746struct its_srat_map {
3747 /* numa node id */
3748 u32 numa_node;
3749 /* GIC ITS ID */
3750 u32 its_id;
3751};
3752
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003753static struct its_srat_map *its_srat_maps __initdata;
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303754static int its_in_srat __initdata;
3755
3756static int __init acpi_get_its_numa_node(u32 its_id)
3757{
3758 int i;
3759
3760 for (i = 0; i < its_in_srat; i++) {
3761 if (its_id == its_srat_maps[i].its_id)
3762 return its_srat_maps[i].numa_node;
3763 }
3764 return NUMA_NO_NODE;
3765}
3766
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003767static int __init gic_acpi_match_srat_its(struct acpi_subtable_header *header,
3768 const unsigned long end)
3769{
3770 return 0;
3771}
3772
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303773static int __init gic_acpi_parse_srat_its(struct acpi_subtable_header *header,
3774 const unsigned long end)
3775{
3776 int node;
3777 struct acpi_srat_gic_its_affinity *its_affinity;
3778
3779 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
3780 if (!its_affinity)
3781 return -EINVAL;
3782
3783 if (its_affinity->header.length < sizeof(*its_affinity)) {
3784 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
3785 its_affinity->header.length);
3786 return -EINVAL;
3787 }
3788
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303789 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
3790
3791 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
3792 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
3793 return 0;
3794 }
3795
3796 its_srat_maps[its_in_srat].numa_node = node;
3797 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
3798 its_in_srat++;
3799 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
3800 its_affinity->proximity_domain, its_affinity->its_id, node);
3801
3802 return 0;
3803}
3804
3805static void __init acpi_table_parse_srat_its(void)
3806{
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003807 int count;
3808
3809 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
3810 sizeof(struct acpi_table_srat),
3811 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3812 gic_acpi_match_srat_its, 0);
3813 if (count <= 0)
3814 return;
3815
Kees Cook6da2ec52018-06-12 13:55:00 -07003816 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
3817 GFP_KERNEL);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003818 if (!its_srat_maps) {
3819 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
3820 return;
3821 }
3822
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303823 acpi_table_parse_entries(ACPI_SIG_SRAT,
3824 sizeof(struct acpi_table_srat),
3825 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3826 gic_acpi_parse_srat_its, 0);
3827}
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003828
3829/* free the its_srat_maps after ITS probing */
3830static void __init acpi_its_srat_maps_free(void)
3831{
3832 kfree(its_srat_maps);
3833}
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303834#else
3835static void __init acpi_table_parse_srat_its(void) { }
3836static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003837static void __init acpi_its_srat_maps_free(void) { }
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303838#endif
3839
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003840static int __init gic_acpi_parse_madt_its(struct acpi_subtable_header *header,
3841 const unsigned long end)
3842{
3843 struct acpi_madt_generic_translator *its_entry;
3844 struct fwnode_handle *dom_handle;
3845 struct resource res;
3846 int err;
3847
3848 its_entry = (struct acpi_madt_generic_translator *)header;
3849 memset(&res, 0, sizeof(res));
3850 res.start = its_entry->base_address;
3851 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
3852 res.flags = IORESOURCE_MEM;
3853
3854 dom_handle = irq_domain_alloc_fwnode((void *)its_entry->base_address);
3855 if (!dom_handle) {
3856 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
3857 &res.start);
3858 return -ENOMEM;
3859 }
3860
Shameer Kolothum8b4282e2018-02-13 15:20:50 +00003861 err = iort_register_domain_token(its_entry->translation_id, res.start,
3862 dom_handle);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003863 if (err) {
3864 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
3865 &res.start, its_entry->translation_id);
3866 goto dom_err;
3867 }
3868
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303869 err = its_probe_one(&res, dom_handle,
3870 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003871 if (!err)
3872 return 0;
3873
3874 iort_deregister_domain_token(its_entry->translation_id);
3875dom_err:
3876 irq_domain_free_fwnode(dom_handle);
3877 return err;
3878}
3879
3880static void __init its_acpi_probe(void)
3881{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303882 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003883 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
3884 gic_acpi_parse_madt_its, 0);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003885 acpi_its_srat_maps_free();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003886}
3887#else
3888static void __init its_acpi_probe(void) { }
3889#endif
3890
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003891int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
3892 struct irq_domain *parent_domain)
3893{
3894 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003895 struct its_node *its;
3896 bool has_v4 = false;
3897 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003898
3899 its_parent = parent_domain;
3900 of_node = to_of_node(handle);
3901 if (of_node)
3902 its_of_probe(of_node);
3903 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003904 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003905
3906 if (list_empty(&its_nodes)) {
3907 pr_warn("ITS: No ITS available, not enabling LPIs\n");
3908 return -ENXIO;
3909 }
3910
3911 gic_rdists = rdists;
Marc Zyngier11e37d32018-07-27 13:38:54 +01003912
3913 err = allocate_lpi_tables();
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003914 if (err)
3915 return err;
3916
3917 list_for_each_entry(its, &its_nodes, entry)
3918 has_v4 |= its->is_v4;
3919
3920 if (has_v4 & rdists->has_vlpis) {
Marc Zyngier3d63cb52016-12-20 15:31:54 +00003921 if (its_init_vpe_domain() ||
3922 its_init_v4(parent_domain, &its_vpe_domain_ops)) {
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003923 rdists->has_vlpis = false;
3924 pr_err("ITS: Disabling GICv4 support\n");
3925 }
3926 }
3927
Derek Basehoredba0bc72018-02-28 21:48:18 -08003928 register_syscore_ops(&its_syscore_ops);
3929
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003930 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003931}