blob: 787e8eec9a7f1862275587f079d2809715baeeec [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Marc Zyngiercc2d3212014-11-24 14:35:11 +00002/*
Marc Zyngierd7276b82016-12-20 15:11:47 +00003 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
Marc Zyngiercc2d3212014-11-24 14:35:11 +00004 * Author: Marc Zyngier <marc.zyngier@arm.com>
Marc Zyngiercc2d3212014-11-24 14:35:11 +00005 */
6
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02007#include <linux/acpi.h>
Hanjun Guo8d3554b2017-03-07 20:39:59 +08008#include <linux/acpi_iort.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +00009#include <linux/bitmap.h>
10#include <linux/cpu.h>
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +010011#include <linux/crash_dump.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000012#include <linux/delay.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010013#include <linux/dma-iommu.h>
Marc Zyngier3fb68fa2018-07-27 16:21:18 +010014#include <linux/efi.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000015#include <linux/interrupt.h>
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +020016#include <linux/irqdomain.h>
Marc Zyngier880cb3c2018-05-27 16:14:15 +010017#include <linux/list.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000018#include <linux/log2.h>
Marc Zyngier5e2c9f92018-07-27 16:23:18 +010019#include <linux/memblock.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000020#include <linux/mm.h>
21#include <linux/msi.h>
22#include <linux/of.h>
23#include <linux/of_address.h>
24#include <linux/of_irq.h>
25#include <linux/of_pci.h>
26#include <linux/of_platform.h>
27#include <linux/percpu.h>
28#include <linux/slab.h>
Derek Basehoredba0bc72018-02-28 21:48:18 -080029#include <linux/syscore_ops.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000030
Joel Porquet41a83e062015-07-07 17:11:46 -040031#include <linux/irqchip.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000032#include <linux/irqchip/arm-gic-v3.h>
Marc Zyngierc808eea2016-12-20 09:31:20 +000033#include <linux/irqchip/arm-gic-v4.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000034
Marc Zyngiercc2d3212014-11-24 14:35:11 +000035#include <asm/cputype.h>
36#include <asm/exception.h>
37
Robert Richter67510cc2015-09-21 22:58:37 +020038#include "irq-gic-common.h"
39
Robert Richter94100972015-09-21 22:58:38 +020040#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
41#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020042#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
Derek Basehoredba0bc72018-02-28 21:48:18 -080043#define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3)
Marc Zyngiercc2d3212014-11-24 14:35:11 +000044
Marc Zyngierc48ed512014-11-24 14:35:12 +000045#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
Marc Zyngierc440a9d2018-07-27 15:40:13 +010046#define RDIST_FLAGS_RD_TABLES_PREALLOCATED (1 << 1)
Marc Zyngierc48ed512014-11-24 14:35:12 +000047
Marc Zyngiera13b0402016-12-19 17:15:24 +000048static u32 lpi_id_bits;
49
50/*
51 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
52 * deal with (one configuration byte per interrupt). PENDBASE has to
53 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
54 */
55#define LPI_NRBITS lpi_id_bits
56#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
57#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
58
Julien Thierry2130b782018-08-28 16:51:18 +010059#define LPI_PROP_DEFAULT_PRIO GICD_INT_DEF_PRI
Marc Zyngiera13b0402016-12-19 17:15:24 +000060
Marc Zyngiercc2d3212014-11-24 14:35:11 +000061/*
62 * Collection structure - just an ID, and a redistributor address to
63 * ping. We use one per CPU as a bag of interrupts assigned to this
64 * CPU.
65 */
66struct its_collection {
67 u64 target_address;
68 u16 col_id;
69};
70
71/*
Shanker Donthineni93473592016-06-06 18:17:30 -050072 * The ITS_BASER structure - contains memory information, cached
73 * value of BASER register configuration and ITS page size.
Shanker Donthineni466b7d12016-03-09 22:10:49 -060074 */
75struct its_baser {
76 void *base;
77 u64 val;
78 u32 order;
Shanker Donthineni93473592016-06-06 18:17:30 -050079 u32 psz;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060080};
81
Ard Biesheuvel558b0162017-10-17 17:55:56 +010082struct its_device;
83
Shanker Donthineni466b7d12016-03-09 22:10:49 -060084/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000085 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010086 * top-level MSI domain, the command queue, the collections, and the
87 * list of devices writing to it.
Marc Zyngier9791ec72019-01-29 10:02:33 +000088 *
89 * dev_alloc_lock has to be taken for device allocations, while the
90 * spinlock must be taken to parse data structures such as the device
91 * list.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000092 */
93struct its_node {
94 raw_spinlock_t lock;
Marc Zyngier9791ec72019-01-29 10:02:33 +000095 struct mutex dev_alloc_lock;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000096 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000097 void __iomem *base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +020098 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000099 struct its_cmd_block *cmd_base;
100 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600101 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000102 struct its_collection *collections;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100103 struct fwnode_handle *fwnode_handle;
104 u64 (*get_msi_base)(struct its_device *its_dev);
Derek Basehoredba0bc72018-02-28 21:48:18 -0800105 u64 cbaser_save;
106 u32 ctlr_save;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000107 struct list_head its_device_list;
108 u64 flags;
Marc Zyngierdebf6d02017-10-08 18:44:42 +0100109 unsigned long list_nr;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000110 u32 ite_size;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600111 u32 device_ids;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200112 int numa_node;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100113 unsigned int msi_domain_flags;
114 u32 pre_its_base; /* for Socionext Synquacer */
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000115 bool is_v4;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100116 int vlpi_redist_offset;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000117};
118
119#define ITS_ITT_ALIGN SZ_256
120
Shanker Donthineni32bd44d2017-10-07 15:43:48 -0500121/* The maximum number of VPEID bits supported by VLPI commands */
122#define ITS_MAX_VPEID_BITS (16)
123#define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
124
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600125/* Convert page order to size in bytes */
126#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
127
Marc Zyngier591e5be2015-07-17 10:46:42 +0100128struct event_lpi_map {
129 unsigned long *lpi_map;
130 u16 *col_map;
131 irq_hw_number_t lpi_base;
132 int nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000133 struct mutex vlpi_lock;
134 struct its_vm *vm;
135 struct its_vlpi_map *vlpi_maps;
136 int nr_vlpis;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100137};
138
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000139/*
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000140 * The ITS view of a device - belongs to an ITS, owns an interrupt
141 * translation table, and a list of interrupts. If it some of its
142 * LPIs are injected into a guest (GICv4), the event_map.vm field
143 * indicates which one.
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000144 */
145struct its_device {
146 struct list_head entry;
147 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100148 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000149 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000150 u32 nr_ites;
151 u32 device_id;
Marc Zyngier9791ec72019-01-29 10:02:33 +0000152 bool shared;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000153};
154
Marc Zyngier20b3d542016-12-20 15:23:22 +0000155static struct {
156 raw_spinlock_t lock;
157 struct its_device *dev;
158 struct its_vpe **vpes;
159 int next_victim;
160} vpe_proxy;
161
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000162static LIST_HEAD(its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +0200163static DEFINE_RAW_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000164static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200165static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000166
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000167static unsigned long its_list_map;
Marc Zyngier3171a472016-12-20 15:17:28 +0000168static u16 vmovp_seq_num;
169static DEFINE_RAW_SPINLOCK(vmovp_lock);
170
Marc Zyngier7d75bbb2016-12-20 13:55:54 +0000171static DEFINE_IDA(its_vpeid_ida);
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000172
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000173#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
Marc Zyngier11e37d32018-07-27 13:38:54 +0100174#define gic_data_rdist_cpu(cpu) (per_cpu_ptr(gic_rdists->rdist, cpu))
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000175#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngiere643d802016-12-20 15:09:31 +0000176#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000177
Zenghui Yu84243122019-10-23 03:46:26 +0000178static u16 get_its_list(struct its_vm *vm)
179{
180 struct its_node *its;
181 unsigned long its_list = 0;
182
183 list_for_each_entry(its, &its_nodes, entry) {
184 if (!its->is_v4)
185 continue;
186
187 if (vm->vlpi_count[its->list_nr])
188 __set_bit(its->list_nr, &its_list);
189 }
190
191 return (u16)its_list;
192}
193
Marc Zyngier591e5be2015-07-17 10:46:42 +0100194static struct its_collection *dev_event_to_col(struct its_device *its_dev,
195 u32 event)
196{
197 struct its_node *its = its_dev->its;
198
199 return its->collections + its_dev->event_map.col_map[event];
200}
201
Marc Zyngier83559b42018-06-22 10:52:52 +0100202static struct its_collection *valid_col(struct its_collection *col)
203{
Joe Perches20faba82019-07-09 22:04:18 -0700204 if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(15, 0)))
Marc Zyngier83559b42018-06-22 10:52:52 +0100205 return NULL;
206
207 return col;
208}
209
Marc Zyngier205e0652018-06-22 10:52:53 +0100210static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
211{
212 if (valid_col(its->collections + vpe->col_idx))
213 return vpe;
214
215 return NULL;
216}
217
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000218/*
219 * ITS command descriptors - parameters to be encoded in a command
220 * block.
221 */
222struct its_cmd_desc {
223 union {
224 struct {
225 struct its_device *dev;
226 u32 event_id;
227 } its_inv_cmd;
228
229 struct {
230 struct its_device *dev;
231 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000232 } its_clear_cmd;
233
234 struct {
235 struct its_device *dev;
236 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000237 } its_int_cmd;
238
239 struct {
240 struct its_device *dev;
241 int valid;
242 } its_mapd_cmd;
243
244 struct {
245 struct its_collection *col;
246 int valid;
247 } its_mapc_cmd;
248
249 struct {
250 struct its_device *dev;
251 u32 phys_id;
252 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000253 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000254
255 struct {
256 struct its_device *dev;
257 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100258 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000259 } its_movi_cmd;
260
261 struct {
262 struct its_device *dev;
263 u32 event_id;
264 } its_discard_cmd;
265
266 struct {
267 struct its_collection *col;
268 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000269
270 struct {
271 struct its_vpe *vpe;
Marc Zyngiereb781922016-12-20 14:47:05 +0000272 } its_vinvall_cmd;
273
274 struct {
275 struct its_vpe *vpe;
276 struct its_collection *col;
277 bool valid;
278 } its_vmapp_cmd;
279
280 struct {
281 struct its_vpe *vpe;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000282 struct its_device *dev;
283 u32 virt_id;
284 u32 event_id;
285 bool db_enabled;
286 } its_vmapti_cmd;
287
288 struct {
289 struct its_vpe *vpe;
290 struct its_device *dev;
291 u32 event_id;
292 bool db_enabled;
293 } its_vmovi_cmd;
Marc Zyngier3171a472016-12-20 15:17:28 +0000294
295 struct {
296 struct its_vpe *vpe;
297 struct its_collection *col;
298 u16 seq_num;
299 u16 its_list;
300 } its_vmovp_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000301 };
302};
303
304/*
305 * The ITS command block, which is what the ITS actually parses.
306 */
307struct its_cmd_block {
308 u64 raw_cmd[4];
309};
310
311#define ITS_CMD_QUEUE_SZ SZ_64K
312#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
313
Marc Zyngier67047f902017-07-28 21:16:58 +0100314typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
315 struct its_cmd_block *,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000316 struct its_cmd_desc *);
317
Marc Zyngier67047f902017-07-28 21:16:58 +0100318typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
319 struct its_cmd_block *,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000320 struct its_cmd_desc *);
321
Marc Zyngier4d36f132016-12-19 17:11:52 +0000322static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
323{
324 u64 mask = GENMASK_ULL(h, l);
325 *raw_cmd &= ~mask;
326 *raw_cmd |= (val << l) & mask;
327}
328
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000329static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
330{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000331 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000332}
333
334static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
335{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000336 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000337}
338
339static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
340{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000341 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000342}
343
344static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
345{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000346 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000347}
348
349static void its_encode_size(struct its_cmd_block *cmd, u8 size)
350{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000351 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000352}
353
354static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
355{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500356 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000357}
358
359static void its_encode_valid(struct its_cmd_block *cmd, int valid)
360{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000361 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000362}
363
364static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
365{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500366 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000367}
368
369static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
370{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000371 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000372}
373
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000374static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
375{
376 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
377}
378
379static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
380{
381 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
382}
383
384static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
385{
386 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
387}
388
389static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
390{
391 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
392}
393
Marc Zyngier3171a472016-12-20 15:17:28 +0000394static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
395{
396 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
397}
398
399static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
400{
401 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
402}
403
Marc Zyngiereb781922016-12-20 14:47:05 +0000404static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
405{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500406 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
Marc Zyngiereb781922016-12-20 14:47:05 +0000407}
408
409static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
410{
411 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
412}
413
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000414static inline void its_fixup_cmd(struct its_cmd_block *cmd)
415{
416 /* Let's fixup BE commands */
417 cmd->raw_cmd[0] = cpu_to_le64(cmd->raw_cmd[0]);
418 cmd->raw_cmd[1] = cpu_to_le64(cmd->raw_cmd[1]);
419 cmd->raw_cmd[2] = cpu_to_le64(cmd->raw_cmd[2]);
420 cmd->raw_cmd[3] = cpu_to_le64(cmd->raw_cmd[3]);
421}
422
Marc Zyngier67047f902017-07-28 21:16:58 +0100423static struct its_collection *its_build_mapd_cmd(struct its_node *its,
424 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000425 struct its_cmd_desc *desc)
426{
427 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000428 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000429
430 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
431 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
432
433 its_encode_cmd(cmd, GITS_CMD_MAPD);
434 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
435 its_encode_size(cmd, size - 1);
436 its_encode_itt(cmd, itt_addr);
437 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
438
439 its_fixup_cmd(cmd);
440
Marc Zyngier591e5be2015-07-17 10:46:42 +0100441 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000442}
443
Marc Zyngier67047f902017-07-28 21:16:58 +0100444static struct its_collection *its_build_mapc_cmd(struct its_node *its,
445 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000446 struct its_cmd_desc *desc)
447{
448 its_encode_cmd(cmd, GITS_CMD_MAPC);
449 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
450 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
451 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
452
453 its_fixup_cmd(cmd);
454
455 return desc->its_mapc_cmd.col;
456}
457
Marc Zyngier67047f902017-07-28 21:16:58 +0100458static struct its_collection *its_build_mapti_cmd(struct its_node *its,
459 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000460 struct its_cmd_desc *desc)
461{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100462 struct its_collection *col;
463
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000464 col = dev_event_to_col(desc->its_mapti_cmd.dev,
465 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100466
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000467 its_encode_cmd(cmd, GITS_CMD_MAPTI);
468 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
469 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
470 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100471 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000472
473 its_fixup_cmd(cmd);
474
Marc Zyngier83559b42018-06-22 10:52:52 +0100475 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000476}
477
Marc Zyngier67047f902017-07-28 21:16:58 +0100478static struct its_collection *its_build_movi_cmd(struct its_node *its,
479 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000480 struct its_cmd_desc *desc)
481{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100482 struct its_collection *col;
483
484 col = dev_event_to_col(desc->its_movi_cmd.dev,
485 desc->its_movi_cmd.event_id);
486
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000487 its_encode_cmd(cmd, GITS_CMD_MOVI);
488 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100489 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000490 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
491
492 its_fixup_cmd(cmd);
493
Marc Zyngier83559b42018-06-22 10:52:52 +0100494 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000495}
496
Marc Zyngier67047f902017-07-28 21:16:58 +0100497static struct its_collection *its_build_discard_cmd(struct its_node *its,
498 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000499 struct its_cmd_desc *desc)
500{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100501 struct its_collection *col;
502
503 col = dev_event_to_col(desc->its_discard_cmd.dev,
504 desc->its_discard_cmd.event_id);
505
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000506 its_encode_cmd(cmd, GITS_CMD_DISCARD);
507 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
508 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
509
510 its_fixup_cmd(cmd);
511
Marc Zyngier83559b42018-06-22 10:52:52 +0100512 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000513}
514
Marc Zyngier67047f902017-07-28 21:16:58 +0100515static struct its_collection *its_build_inv_cmd(struct its_node *its,
516 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000517 struct its_cmd_desc *desc)
518{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100519 struct its_collection *col;
520
521 col = dev_event_to_col(desc->its_inv_cmd.dev,
522 desc->its_inv_cmd.event_id);
523
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000524 its_encode_cmd(cmd, GITS_CMD_INV);
525 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
526 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
527
528 its_fixup_cmd(cmd);
529
Marc Zyngier83559b42018-06-22 10:52:52 +0100530 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000531}
532
Marc Zyngier67047f902017-07-28 21:16:58 +0100533static struct its_collection *its_build_int_cmd(struct its_node *its,
534 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000535 struct its_cmd_desc *desc)
536{
537 struct its_collection *col;
538
539 col = dev_event_to_col(desc->its_int_cmd.dev,
540 desc->its_int_cmd.event_id);
541
542 its_encode_cmd(cmd, GITS_CMD_INT);
543 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
544 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
545
546 its_fixup_cmd(cmd);
547
Marc Zyngier83559b42018-06-22 10:52:52 +0100548 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000549}
550
Marc Zyngier67047f902017-07-28 21:16:58 +0100551static struct its_collection *its_build_clear_cmd(struct its_node *its,
552 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000553 struct its_cmd_desc *desc)
554{
555 struct its_collection *col;
556
557 col = dev_event_to_col(desc->its_clear_cmd.dev,
558 desc->its_clear_cmd.event_id);
559
560 its_encode_cmd(cmd, GITS_CMD_CLEAR);
561 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
562 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
563
564 its_fixup_cmd(cmd);
565
Marc Zyngier83559b42018-06-22 10:52:52 +0100566 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000567}
568
Marc Zyngier67047f902017-07-28 21:16:58 +0100569static struct its_collection *its_build_invall_cmd(struct its_node *its,
570 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000571 struct its_cmd_desc *desc)
572{
573 its_encode_cmd(cmd, GITS_CMD_INVALL);
574 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
575
576 its_fixup_cmd(cmd);
577
578 return NULL;
579}
580
Marc Zyngier67047f902017-07-28 21:16:58 +0100581static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
582 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000583 struct its_cmd_desc *desc)
584{
585 its_encode_cmd(cmd, GITS_CMD_VINVALL);
586 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
587
588 its_fixup_cmd(cmd);
589
Marc Zyngier205e0652018-06-22 10:52:53 +0100590 return valid_vpe(its, desc->its_vinvall_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000591}
592
Marc Zyngier67047f902017-07-28 21:16:58 +0100593static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
594 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000595 struct its_cmd_desc *desc)
596{
597 unsigned long vpt_addr;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100598 u64 target;
Marc Zyngiereb781922016-12-20 14:47:05 +0000599
600 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100601 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngiereb781922016-12-20 14:47:05 +0000602
603 its_encode_cmd(cmd, GITS_CMD_VMAPP);
604 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
605 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100606 its_encode_target(cmd, target);
Marc Zyngiereb781922016-12-20 14:47:05 +0000607 its_encode_vpt_addr(cmd, vpt_addr);
608 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
609
610 its_fixup_cmd(cmd);
611
Marc Zyngier205e0652018-06-22 10:52:53 +0100612 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000613}
614
Marc Zyngier67047f902017-07-28 21:16:58 +0100615static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
616 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000617 struct its_cmd_desc *desc)
618{
619 u32 db;
620
621 if (desc->its_vmapti_cmd.db_enabled)
622 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
623 else
624 db = 1023;
625
626 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
627 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
628 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
629 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
630 its_encode_db_phys_id(cmd, db);
631 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
632
633 its_fixup_cmd(cmd);
634
Marc Zyngier205e0652018-06-22 10:52:53 +0100635 return valid_vpe(its, desc->its_vmapti_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000636}
637
Marc Zyngier67047f902017-07-28 21:16:58 +0100638static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
639 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000640 struct its_cmd_desc *desc)
641{
642 u32 db;
643
644 if (desc->its_vmovi_cmd.db_enabled)
645 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
646 else
647 db = 1023;
648
649 its_encode_cmd(cmd, GITS_CMD_VMOVI);
650 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
651 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
652 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
653 its_encode_db_phys_id(cmd, db);
654 its_encode_db_valid(cmd, true);
655
656 its_fixup_cmd(cmd);
657
Marc Zyngier205e0652018-06-22 10:52:53 +0100658 return valid_vpe(its, desc->its_vmovi_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000659}
660
Marc Zyngier67047f902017-07-28 21:16:58 +0100661static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
662 struct its_cmd_block *cmd,
Marc Zyngier3171a472016-12-20 15:17:28 +0000663 struct its_cmd_desc *desc)
664{
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100665 u64 target;
666
667 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngier3171a472016-12-20 15:17:28 +0000668 its_encode_cmd(cmd, GITS_CMD_VMOVP);
669 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
670 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
671 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100672 its_encode_target(cmd, target);
Marc Zyngier3171a472016-12-20 15:17:28 +0000673
674 its_fixup_cmd(cmd);
675
Marc Zyngier205e0652018-06-22 10:52:53 +0100676 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
Marc Zyngier3171a472016-12-20 15:17:28 +0000677}
678
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000679static u64 its_cmd_ptr_to_offset(struct its_node *its,
680 struct its_cmd_block *ptr)
681{
682 return (ptr - its->cmd_base) * sizeof(*ptr);
683}
684
685static int its_queue_full(struct its_node *its)
686{
687 int widx;
688 int ridx;
689
690 widx = its->cmd_write - its->cmd_base;
691 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
692
693 /* This is incredibly unlikely to happen, unless the ITS locks up. */
694 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
695 return 1;
696
697 return 0;
698}
699
700static struct its_cmd_block *its_allocate_entry(struct its_node *its)
701{
702 struct its_cmd_block *cmd;
703 u32 count = 1000000; /* 1s! */
704
705 while (its_queue_full(its)) {
706 count--;
707 if (!count) {
708 pr_err_ratelimited("ITS queue not draining\n");
709 return NULL;
710 }
711 cpu_relax();
712 udelay(1);
713 }
714
715 cmd = its->cmd_write++;
716
717 /* Handle queue wrapping */
718 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
719 its->cmd_write = its->cmd_base;
720
Marc Zyngier34d677a2016-12-19 17:16:45 +0000721 /* Clear command */
722 cmd->raw_cmd[0] = 0;
723 cmd->raw_cmd[1] = 0;
724 cmd->raw_cmd[2] = 0;
725 cmd->raw_cmd[3] = 0;
726
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000727 return cmd;
728}
729
730static struct its_cmd_block *its_post_commands(struct its_node *its)
731{
732 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
733
734 writel_relaxed(wr, its->base + GITS_CWRITER);
735
736 return its->cmd_write;
737}
738
739static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
740{
741 /*
742 * Make sure the commands written to memory are observable by
743 * the ITS.
744 */
745 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000746 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000747 else
748 dsb(ishst);
749}
750
Marc Zyngiera19b4622017-08-04 17:45:50 +0100751static int its_wait_for_range_completion(struct its_node *its,
Heyi Guoa050fa52019-05-13 19:42:06 +0800752 u64 prev_idx,
Marc Zyngiera19b4622017-08-04 17:45:50 +0100753 struct its_cmd_block *to)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000754{
Heyi Guoa050fa52019-05-13 19:42:06 +0800755 u64 rd_idx, to_idx, linear_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000756 u32 count = 1000000; /* 1s! */
757
Heyi Guoa050fa52019-05-13 19:42:06 +0800758 /* Linearize to_idx if the command set has wrapped around */
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000759 to_idx = its_cmd_ptr_to_offset(its, to);
Heyi Guoa050fa52019-05-13 19:42:06 +0800760 if (to_idx < prev_idx)
761 to_idx += ITS_CMD_QUEUE_SZ;
762
763 linear_idx = prev_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000764
765 while (1) {
Heyi Guoa050fa52019-05-13 19:42:06 +0800766 s64 delta;
767
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000768 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100769
Heyi Guoa050fa52019-05-13 19:42:06 +0800770 /*
771 * Compute the read pointer progress, taking the
772 * potential wrap-around into account.
773 */
774 delta = rd_idx - prev_idx;
775 if (rd_idx < prev_idx)
776 delta += ITS_CMD_QUEUE_SZ;
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100777
Heyi Guoa050fa52019-05-13 19:42:06 +0800778 linear_idx += delta;
779 if (linear_idx >= to_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000780 break;
781
782 count--;
783 if (!count) {
Heyi Guoa050fa52019-05-13 19:42:06 +0800784 pr_err_ratelimited("ITS queue timeout (%llu %llu)\n",
785 to_idx, linear_idx);
Marc Zyngiera19b4622017-08-04 17:45:50 +0100786 return -1;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000787 }
Heyi Guoa050fa52019-05-13 19:42:06 +0800788 prev_idx = rd_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000789 cpu_relax();
790 udelay(1);
791 }
Marc Zyngiera19b4622017-08-04 17:45:50 +0100792
793 return 0;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000794}
795
Marc Zyngiere4f90942016-12-19 17:56:32 +0000796/* Warning, macro hell follows */
797#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
798void name(struct its_node *its, \
799 buildtype builder, \
800 struct its_cmd_desc *desc) \
801{ \
802 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
803 synctype *sync_obj; \
804 unsigned long flags; \
Heyi Guoa050fa52019-05-13 19:42:06 +0800805 u64 rd_idx; \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000806 \
807 raw_spin_lock_irqsave(&its->lock, flags); \
808 \
809 cmd = its_allocate_entry(its); \
810 if (!cmd) { /* We're soooooo screewed... */ \
811 raw_spin_unlock_irqrestore(&its->lock, flags); \
812 return; \
813 } \
Marc Zyngier67047f902017-07-28 21:16:58 +0100814 sync_obj = builder(its, cmd, desc); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000815 its_flush_cmd(its, cmd); \
816 \
817 if (sync_obj) { \
818 sync_cmd = its_allocate_entry(its); \
819 if (!sync_cmd) \
820 goto post; \
821 \
Marc Zyngier67047f902017-07-28 21:16:58 +0100822 buildfn(its, sync_cmd, sync_obj); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000823 its_flush_cmd(its, sync_cmd); \
824 } \
825 \
826post: \
Heyi Guoa050fa52019-05-13 19:42:06 +0800827 rd_idx = readl_relaxed(its->base + GITS_CREADR); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000828 next_cmd = its_post_commands(its); \
829 raw_spin_unlock_irqrestore(&its->lock, flags); \
830 \
Heyi Guoa050fa52019-05-13 19:42:06 +0800831 if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \
Marc Zyngiera19b4622017-08-04 17:45:50 +0100832 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000833}
834
Marc Zyngier67047f902017-07-28 21:16:58 +0100835static void its_build_sync_cmd(struct its_node *its,
836 struct its_cmd_block *sync_cmd,
Marc Zyngiere4f90942016-12-19 17:56:32 +0000837 struct its_collection *sync_col)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000838{
Marc Zyngiere4f90942016-12-19 17:56:32 +0000839 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
840 its_encode_target(sync_cmd, sync_col->target_address);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000841
Marc Zyngiere4f90942016-12-19 17:56:32 +0000842 its_fixup_cmd(sync_cmd);
843}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000844
Marc Zyngiere4f90942016-12-19 17:56:32 +0000845static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
846 struct its_collection, its_build_sync_cmd)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000847
Marc Zyngier67047f902017-07-28 21:16:58 +0100848static void its_build_vsync_cmd(struct its_node *its,
849 struct its_cmd_block *sync_cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000850 struct its_vpe *sync_vpe)
851{
852 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
853 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000854
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000855 its_fixup_cmd(sync_cmd);
856}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000857
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000858static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
859 struct its_vpe, its_build_vsync_cmd)
860
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000861static void its_send_int(struct its_device *dev, u32 event_id)
862{
863 struct its_cmd_desc desc;
864
865 desc.its_int_cmd.dev = dev;
866 desc.its_int_cmd.event_id = event_id;
867
868 its_send_single_command(dev->its, its_build_int_cmd, &desc);
869}
870
871static void its_send_clear(struct its_device *dev, u32 event_id)
872{
873 struct its_cmd_desc desc;
874
875 desc.its_clear_cmd.dev = dev;
876 desc.its_clear_cmd.event_id = event_id;
877
878 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000879}
880
881static void its_send_inv(struct its_device *dev, u32 event_id)
882{
883 struct its_cmd_desc desc;
884
885 desc.its_inv_cmd.dev = dev;
886 desc.its_inv_cmd.event_id = event_id;
887
888 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
889}
890
891static void its_send_mapd(struct its_device *dev, int valid)
892{
893 struct its_cmd_desc desc;
894
895 desc.its_mapd_cmd.dev = dev;
896 desc.its_mapd_cmd.valid = !!valid;
897
898 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
899}
900
901static void its_send_mapc(struct its_node *its, struct its_collection *col,
902 int valid)
903{
904 struct its_cmd_desc desc;
905
906 desc.its_mapc_cmd.col = col;
907 desc.its_mapc_cmd.valid = !!valid;
908
909 its_send_single_command(its, its_build_mapc_cmd, &desc);
910}
911
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000912static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000913{
914 struct its_cmd_desc desc;
915
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000916 desc.its_mapti_cmd.dev = dev;
917 desc.its_mapti_cmd.phys_id = irq_id;
918 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000919
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000920 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000921}
922
923static void its_send_movi(struct its_device *dev,
924 struct its_collection *col, u32 id)
925{
926 struct its_cmd_desc desc;
927
928 desc.its_movi_cmd.dev = dev;
929 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100930 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000931
932 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
933}
934
935static void its_send_discard(struct its_device *dev, u32 id)
936{
937 struct its_cmd_desc desc;
938
939 desc.its_discard_cmd.dev = dev;
940 desc.its_discard_cmd.event_id = id;
941
942 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
943}
944
945static void its_send_invall(struct its_node *its, struct its_collection *col)
946{
947 struct its_cmd_desc desc;
948
949 desc.its_invall_cmd.col = col;
950
951 its_send_single_command(its, its_build_invall_cmd, &desc);
952}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000953
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000954static void its_send_vmapti(struct its_device *dev, u32 id)
955{
956 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
957 struct its_cmd_desc desc;
958
959 desc.its_vmapti_cmd.vpe = map->vpe;
960 desc.its_vmapti_cmd.dev = dev;
961 desc.its_vmapti_cmd.virt_id = map->vintid;
962 desc.its_vmapti_cmd.event_id = id;
963 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
964
965 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
966}
967
968static void its_send_vmovi(struct its_device *dev, u32 id)
969{
970 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
971 struct its_cmd_desc desc;
972
973 desc.its_vmovi_cmd.vpe = map->vpe;
974 desc.its_vmovi_cmd.dev = dev;
975 desc.its_vmovi_cmd.event_id = id;
976 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
977
978 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
979}
980
Marc Zyngier75fd9512017-10-08 18:46:39 +0100981static void its_send_vmapp(struct its_node *its,
982 struct its_vpe *vpe, bool valid)
Marc Zyngiereb781922016-12-20 14:47:05 +0000983{
984 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +0000985
986 desc.its_vmapp_cmd.vpe = vpe;
987 desc.its_vmapp_cmd.valid = valid;
Marc Zyngier75fd9512017-10-08 18:46:39 +0100988 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
Marc Zyngiereb781922016-12-20 14:47:05 +0000989
Marc Zyngier75fd9512017-10-08 18:46:39 +0100990 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +0000991}
992
Marc Zyngier3171a472016-12-20 15:17:28 +0000993static void its_send_vmovp(struct its_vpe *vpe)
994{
Zenghui Yu84243122019-10-23 03:46:26 +0000995 struct its_cmd_desc desc = {};
Marc Zyngier3171a472016-12-20 15:17:28 +0000996 struct its_node *its;
997 unsigned long flags;
998 int col_id = vpe->col_idx;
999
1000 desc.its_vmovp_cmd.vpe = vpe;
Marc Zyngier3171a472016-12-20 15:17:28 +00001001
1002 if (!its_list_map) {
1003 its = list_first_entry(&its_nodes, struct its_node, entry);
Marc Zyngier3171a472016-12-20 15:17:28 +00001004 desc.its_vmovp_cmd.col = &its->collections[col_id];
1005 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1006 return;
1007 }
1008
1009 /*
1010 * Yet another marvel of the architecture. If using the
1011 * its_list "feature", we need to make sure that all ITSs
1012 * receive all VMOVP commands in the same order. The only way
1013 * to guarantee this is to make vmovp a serialization point.
1014 *
1015 * Wall <-- Head.
1016 */
1017 raw_spin_lock_irqsave(&vmovp_lock, flags);
1018
1019 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
Zenghui Yu84243122019-10-23 03:46:26 +00001020 desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm);
Marc Zyngier3171a472016-12-20 15:17:28 +00001021
1022 /* Emit VMOVPs */
1023 list_for_each_entry(its, &its_nodes, entry) {
1024 if (!its->is_v4)
1025 continue;
1026
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001027 if (!vpe->its_vm->vlpi_count[its->list_nr])
1028 continue;
1029
Marc Zyngier3171a472016-12-20 15:17:28 +00001030 desc.its_vmovp_cmd.col = &its->collections[col_id];
1031 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1032 }
1033
1034 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1035}
1036
Marc Zyngier40619a22017-10-08 15:16:09 +01001037static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
Marc Zyngiereb781922016-12-20 14:47:05 +00001038{
1039 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +00001040
1041 desc.its_vinvall_cmd.vpe = vpe;
Marc Zyngier40619a22017-10-08 15:16:09 +01001042 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +00001043}
1044
Marc Zyngierc48ed512014-11-24 14:35:12 +00001045/*
1046 * irqchip functions - assumes MSI, mostly.
1047 */
1048
1049static inline u32 its_get_event_id(struct irq_data *d)
1050{
1051 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001052 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001053}
1054
Marc Zyngier015ec032016-12-20 09:54:57 +00001055static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +00001056{
Marc Zyngier015ec032016-12-20 09:54:57 +00001057 irq_hw_number_t hwirq;
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001058 void *va;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001059 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001060
Marc Zyngier015ec032016-12-20 09:54:57 +00001061 if (irqd_is_forwarded_to_vcpu(d)) {
1062 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1063 u32 event = its_get_event_id(d);
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001064 struct its_vlpi_map *map;
Marc Zyngier015ec032016-12-20 09:54:57 +00001065
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001066 va = page_address(its_dev->event_map.vm->vprop_page);
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001067 map = &its_dev->event_map.vlpi_maps[event];
1068 hwirq = map->vintid;
1069
1070 /* Remember the updated property */
1071 map->properties &= ~clr;
1072 map->properties |= set | LPI_PROP_GROUP1;
Marc Zyngier015ec032016-12-20 09:54:57 +00001073 } else {
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001074 va = gic_rdists->prop_table_va;
Marc Zyngier015ec032016-12-20 09:54:57 +00001075 hwirq = d->hwirq;
1076 }
Marc Zyngieradcdb942016-12-19 19:18:13 +00001077
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001078 cfg = va + hwirq - 8192;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001079 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +00001080 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001081
1082 /*
1083 * Make the above write visible to the redistributors.
1084 * And yes, we're flushing exactly: One. Single. Byte.
1085 * Humpf...
1086 */
1087 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001088 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001089 else
1090 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +00001091}
1092
1093static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1094{
1095 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1096
1097 lpi_write_config(d, clr, set);
Marc Zyngieradcdb942016-12-19 19:18:13 +00001098 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001099}
1100
Marc Zyngier015ec032016-12-20 09:54:57 +00001101static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1102{
1103 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1104 u32 event = its_get_event_id(d);
1105
1106 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1107 return;
1108
1109 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1110
1111 /*
1112 * More fun with the architecture:
1113 *
1114 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1115 * value or to 1023, depending on the enable bit. But that
1116 * would be issueing a mapping for an /existing/ DevID+EventID
1117 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1118 * to the /same/ vPE, using this opportunity to adjust the
1119 * doorbell. Mouahahahaha. We loves it, Precious.
1120 */
1121 its_send_vmovi(its_dev, event);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001122}
1123
1124static void its_mask_irq(struct irq_data *d)
1125{
Marc Zyngier015ec032016-12-20 09:54:57 +00001126 if (irqd_is_forwarded_to_vcpu(d))
1127 its_vlpi_set_doorbell(d, false);
1128
Marc Zyngieradcdb942016-12-19 19:18:13 +00001129 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001130}
1131
1132static void its_unmask_irq(struct irq_data *d)
1133{
Marc Zyngier015ec032016-12-20 09:54:57 +00001134 if (irqd_is_forwarded_to_vcpu(d))
1135 its_vlpi_set_doorbell(d, true);
1136
Marc Zyngieradcdb942016-12-19 19:18:13 +00001137 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001138}
1139
Marc Zyngierc48ed512014-11-24 14:35:12 +00001140static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1141 bool force)
1142{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001143 unsigned int cpu;
1144 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001145 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1146 struct its_collection *target_col;
1147 u32 id = its_get_event_id(d);
1148
Marc Zyngier015ec032016-12-20 09:54:57 +00001149 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1150 if (irqd_is_forwarded_to_vcpu(d))
1151 return -EINVAL;
1152
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001153 /* lpi cannot be routed to a redistributor that is on a foreign node */
1154 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1155 if (its_dev->its->numa_node >= 0) {
1156 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1157 if (!cpumask_intersects(mask_val, cpu_mask))
1158 return -EINVAL;
1159 }
1160 }
1161
1162 cpu = cpumask_any_and(mask_val, cpu_mask);
1163
Marc Zyngierc48ed512014-11-24 14:35:12 +00001164 if (cpu >= nr_cpu_ids)
1165 return -EINVAL;
1166
MaJun8b8d94a2017-05-18 16:19:13 +08001167 /* don't set the affinity when the target cpu is same as current one */
1168 if (cpu != its_dev->event_map.col_map[id]) {
1169 target_col = &its_dev->its->collections[cpu];
1170 its_send_movi(its_dev, target_col, id);
1171 its_dev->event_map.col_map[id] = cpu;
Marc Zyngier0d224d32017-08-18 09:39:18 +01001172 irq_data_update_effective_affinity(d, cpumask_of(cpu));
MaJun8b8d94a2017-05-18 16:19:13 +08001173 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001174
1175 return IRQ_SET_MASK_OK_DONE;
1176}
1177
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001178static u64 its_irq_get_msi_base(struct its_device *its_dev)
1179{
1180 struct its_node *its = its_dev->its;
1181
1182 return its->phys_base + GITS_TRANSLATER;
1183}
1184
Marc Zyngierb48ac832014-11-24 14:35:16 +00001185static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1186{
1187 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1188 struct its_node *its;
1189 u64 addr;
1190
1191 its = its_dev->its;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001192 addr = its->get_msi_base(its_dev);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001193
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001194 msg->address_lo = lower_32_bits(addr);
1195 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001196 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001197
Julien Grall35ae7df2019-05-01 14:58:21 +01001198 iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001199}
1200
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001201static int its_irq_set_irqchip_state(struct irq_data *d,
1202 enum irqchip_irq_state which,
1203 bool state)
1204{
1205 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1206 u32 event = its_get_event_id(d);
1207
1208 if (which != IRQCHIP_STATE_PENDING)
1209 return -EINVAL;
1210
1211 if (state)
1212 its_send_int(its_dev, event);
1213 else
1214 its_send_clear(its_dev, event);
1215
1216 return 0;
1217}
1218
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001219static void its_map_vm(struct its_node *its, struct its_vm *vm)
1220{
1221 unsigned long flags;
1222
1223 /* Not using the ITS list? Everything is always mapped. */
1224 if (!its_list_map)
1225 return;
1226
1227 raw_spin_lock_irqsave(&vmovp_lock, flags);
1228
1229 /*
1230 * If the VM wasn't mapped yet, iterate over the vpes and get
1231 * them mapped now.
1232 */
1233 vm->vlpi_count[its->list_nr]++;
1234
1235 if (vm->vlpi_count[its->list_nr] == 1) {
1236 int i;
1237
1238 for (i = 0; i < vm->nr_vpes; i++) {
1239 struct its_vpe *vpe = vm->vpes[i];
Marc Zyngier44c4c252017-10-19 10:11:34 +01001240 struct irq_data *d = irq_get_irq_data(vpe->irq);
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001241
1242 /* Map the VPE to the first possible CPU */
1243 vpe->col_idx = cpumask_first(cpu_online_mask);
1244 its_send_vmapp(its, vpe, true);
1245 its_send_vinvall(its, vpe);
Marc Zyngier44c4c252017-10-19 10:11:34 +01001246 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001247 }
1248 }
1249
1250 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1251}
1252
1253static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1254{
1255 unsigned long flags;
1256
1257 /* Not using the ITS list? Everything is always mapped. */
1258 if (!its_list_map)
1259 return;
1260
1261 raw_spin_lock_irqsave(&vmovp_lock, flags);
1262
1263 if (!--vm->vlpi_count[its->list_nr]) {
1264 int i;
1265
1266 for (i = 0; i < vm->nr_vpes; i++)
1267 its_send_vmapp(its, vm->vpes[i], false);
1268 }
1269
1270 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1271}
1272
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001273static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1274{
1275 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1276 u32 event = its_get_event_id(d);
1277 int ret = 0;
1278
1279 if (!info->map)
1280 return -EINVAL;
1281
1282 mutex_lock(&its_dev->event_map.vlpi_lock);
1283
1284 if (!its_dev->event_map.vm) {
1285 struct its_vlpi_map *maps;
1286
Kees Cook6396bb22018-06-12 14:03:40 -07001287 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001288 GFP_KERNEL);
1289 if (!maps) {
1290 ret = -ENOMEM;
1291 goto out;
1292 }
1293
1294 its_dev->event_map.vm = info->map->vm;
1295 its_dev->event_map.vlpi_maps = maps;
1296 } else if (its_dev->event_map.vm != info->map->vm) {
1297 ret = -EINVAL;
1298 goto out;
1299 }
1300
1301 /* Get our private copy of the mapping information */
1302 its_dev->event_map.vlpi_maps[event] = *info->map;
1303
1304 if (irqd_is_forwarded_to_vcpu(d)) {
1305 /* Already mapped, move it around */
1306 its_send_vmovi(its_dev, event);
1307 } else {
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001308 /* Ensure all the VPEs are mapped on this ITS */
1309 its_map_vm(its_dev->its, info->map->vm);
1310
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001311 /*
1312 * Flag the interrupt as forwarded so that we can
1313 * start poking the virtual property table.
1314 */
1315 irqd_set_forwarded_to_vcpu(d);
1316
1317 /* Write out the property to the prop table */
1318 lpi_write_config(d, 0xff, info->map->properties);
1319
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001320 /* Drop the physical mapping */
1321 its_send_discard(its_dev, event);
1322
1323 /* and install the virtual one */
1324 its_send_vmapti(its_dev, event);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001325
1326 /* Increment the number of VLPIs */
1327 its_dev->event_map.nr_vlpis++;
1328 }
1329
1330out:
1331 mutex_unlock(&its_dev->event_map.vlpi_lock);
1332 return ret;
1333}
1334
1335static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
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 ||
1344 !its_dev->event_map.vlpi_maps[event].vm) {
1345 ret = -EINVAL;
1346 goto out;
1347 }
1348
1349 /* Copy our mapping information to the incoming request */
1350 *info->map = its_dev->event_map.vlpi_maps[event];
1351
1352out:
1353 mutex_unlock(&its_dev->event_map.vlpi_lock);
1354 return ret;
1355}
1356
1357static int its_vlpi_unmap(struct irq_data *d)
1358{
1359 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1360 u32 event = its_get_event_id(d);
1361 int ret = 0;
1362
1363 mutex_lock(&its_dev->event_map.vlpi_lock);
1364
1365 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1366 ret = -EINVAL;
1367 goto out;
1368 }
1369
1370 /* Drop the virtual mapping */
1371 its_send_discard(its_dev, event);
1372
1373 /* and restore the physical one */
1374 irqd_clr_forwarded_to_vcpu(d);
1375 its_send_mapti(its_dev, d->hwirq, event);
1376 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1377 LPI_PROP_ENABLED |
1378 LPI_PROP_GROUP1));
1379
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001380 /* Potentially unmap the VM from this ITS */
1381 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1382
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001383 /*
1384 * Drop the refcount and make the device available again if
1385 * this was the last VLPI.
1386 */
1387 if (!--its_dev->event_map.nr_vlpis) {
1388 its_dev->event_map.vm = NULL;
1389 kfree(its_dev->event_map.vlpi_maps);
1390 }
1391
1392out:
1393 mutex_unlock(&its_dev->event_map.vlpi_lock);
1394 return ret;
1395}
1396
Marc Zyngier015ec032016-12-20 09:54:57 +00001397static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1398{
1399 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1400
1401 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1402 return -EINVAL;
1403
1404 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1405 lpi_update_config(d, 0xff, info->config);
1406 else
1407 lpi_write_config(d, 0xff, info->config);
1408 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1409
1410 return 0;
1411}
1412
Marc Zyngierc808eea2016-12-20 09:31:20 +00001413static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1414{
1415 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1416 struct its_cmd_info *info = vcpu_info;
1417
1418 /* Need a v4 ITS */
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001419 if (!its_dev->its->is_v4)
Marc Zyngierc808eea2016-12-20 09:31:20 +00001420 return -EINVAL;
1421
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001422 /* Unmap request? */
1423 if (!info)
1424 return its_vlpi_unmap(d);
1425
Marc Zyngierc808eea2016-12-20 09:31:20 +00001426 switch (info->cmd_type) {
1427 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001428 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001429
1430 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001431 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001432
1433 case PROP_UPDATE_VLPI:
1434 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001435 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001436
1437 default:
1438 return -EINVAL;
1439 }
1440}
1441
Marc Zyngierc48ed512014-11-24 14:35:12 +00001442static struct irq_chip its_irq_chip = {
1443 .name = "ITS",
1444 .irq_mask = its_mask_irq,
1445 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001446 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001447 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001448 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001449 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001450 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001451};
1452
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001453
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001454/*
1455 * How we allocate LPIs:
1456 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001457 * lpi_range_list contains ranges of LPIs that are to available to
1458 * allocate from. To allocate LPIs, just pick the first range that
1459 * fits the required allocation, and reduce it by the required
1460 * amount. Once empty, remove the range from the list.
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001461 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001462 * To free a range of LPIs, add a free range to the list, sort it and
1463 * merge the result if the new range happens to be adjacent to an
1464 * already free block.
1465 *
1466 * The consequence of the above is that allocation is cost is low, but
1467 * freeing is expensive. We assumes that freeing rarely occurs.
1468 */
Jia He4cb205c2018-08-28 12:53:26 +08001469#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001470
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001471static DEFINE_MUTEX(lpi_range_lock);
1472static LIST_HEAD(lpi_range_list);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001473
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001474struct lpi_range {
1475 struct list_head entry;
1476 u32 base_id;
1477 u32 span;
1478};
1479
1480static struct lpi_range *mk_lpi_range(u32 base, u32 span)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001481{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001482 struct lpi_range *range;
1483
Rasmus Villemoes1c73fac2019-03-12 18:33:48 +01001484 range = kmalloc(sizeof(*range), GFP_KERNEL);
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001485 if (range) {
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001486 range->base_id = base;
1487 range->span = span;
1488 }
1489
1490 return range;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001491}
1492
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001493static int alloc_lpi_range(u32 nr_lpis, u32 *base)
1494{
1495 struct lpi_range *range, *tmp;
1496 int err = -ENOSPC;
1497
1498 mutex_lock(&lpi_range_lock);
1499
1500 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1501 if (range->span >= nr_lpis) {
1502 *base = range->base_id;
1503 range->base_id += nr_lpis;
1504 range->span -= nr_lpis;
1505
1506 if (range->span == 0) {
1507 list_del(&range->entry);
1508 kfree(range);
1509 }
1510
1511 err = 0;
1512 break;
1513 }
1514 }
1515
1516 mutex_unlock(&lpi_range_lock);
1517
1518 pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
1519 return err;
1520}
1521
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001522static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b)
1523{
1524 if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list)
1525 return;
1526 if (a->base_id + a->span != b->base_id)
1527 return;
1528 b->base_id = a->base_id;
1529 b->span += a->span;
1530 list_del(&a->entry);
1531 kfree(a);
1532}
1533
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001534static int free_lpi_range(u32 base, u32 nr_lpis)
1535{
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001536 struct lpi_range *new, *old;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001537
1538 new = mk_lpi_range(base, nr_lpis);
Rasmus Villemoesb31a3832019-03-12 18:33:47 +01001539 if (!new)
1540 return -ENOMEM;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001541
1542 mutex_lock(&lpi_range_lock);
1543
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001544 list_for_each_entry_reverse(old, &lpi_range_list, entry) {
1545 if (old->base_id < base)
1546 break;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001547 }
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001548 /*
1549 * old is the last element with ->base_id smaller than base,
1550 * so new goes right after it. If there are no elements with
1551 * ->base_id smaller than base, &old->entry ends up pointing
1552 * at the head of the list, and inserting new it the start of
1553 * the list is the right thing to do in that case as well.
1554 */
1555 list_add(&new->entry, &old->entry);
1556 /*
1557 * Now check if we can merge with the preceding and/or
1558 * following ranges.
1559 */
1560 merge_lpi_ranges(old, new);
1561 merge_lpi_ranges(new, list_next_entry(new, entry));
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001562
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001563 mutex_unlock(&lpi_range_lock);
Rasmus Villemoesb31a3832019-03-12 18:33:47 +01001564 return 0;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001565}
1566
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001567static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001568{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001569 u32 lpis = (1UL << id_bits) - 8192;
Marc Zyngier12b29052018-05-31 09:01:59 +01001570 u32 numlpis;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001571 int err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001572
Marc Zyngier12b29052018-05-31 09:01:59 +01001573 numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
1574
1575 if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
1576 lpis = numlpis;
1577 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
1578 lpis);
1579 }
1580
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001581 /*
1582 * Initializing the allocator is just the same as freeing the
1583 * full range of LPIs.
1584 */
1585 err = free_lpi_range(8192, lpis);
1586 pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
1587 return err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001588}
1589
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001590static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001591{
1592 unsigned long *bitmap = NULL;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001593 int err = 0;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001594
1595 do {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001596 err = alloc_lpi_range(nr_irqs, base);
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001597 if (!err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001598 break;
1599
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001600 nr_irqs /= 2;
1601 } while (nr_irqs > 0);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001602
Marc Zyngier45725e02019-01-29 15:19:23 +00001603 if (!nr_irqs)
1604 err = -ENOSPC;
1605
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001606 if (err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001607 goto out;
1608
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001609 bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001610 if (!bitmap)
1611 goto out;
1612
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001613 *nr_ids = nr_irqs;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001614
1615out:
Marc Zyngierc8415b92015-10-02 16:44:05 +01001616 if (!bitmap)
1617 *base = *nr_ids = 0;
1618
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001619 return bitmap;
1620}
1621
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001622static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001623{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001624 WARN_ON(free_lpi_range(base, nr_ids));
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001625 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001626}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001627
Marc Zyngier053be482018-07-27 15:02:27 +01001628static void gic_reset_prop_table(void *va)
1629{
1630 /* Priority 0xa0, Group-1, disabled */
1631 memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ);
1632
1633 /* Make sure the GIC will observe the written configuration */
1634 gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ);
1635}
1636
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001637static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1638{
1639 struct page *prop_page;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001640
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001641 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1642 if (!prop_page)
1643 return NULL;
1644
Marc Zyngier053be482018-07-27 15:02:27 +01001645 gic_reset_prop_table(page_address(prop_page));
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001646
1647 return prop_page;
1648}
1649
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001650static void its_free_prop_table(struct page *prop_page)
1651{
1652 free_pages((unsigned long)page_address(prop_page),
1653 get_order(LPI_PROPBASE_SZ));
1654}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001655
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01001656static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
1657{
1658 phys_addr_t start, end, addr_end;
1659 u64 i;
1660
1661 /*
1662 * We don't bother checking for a kdump kernel as by
1663 * construction, the LPI tables are out of this kernel's
1664 * memory map.
1665 */
1666 if (is_kdump_kernel())
1667 return true;
1668
1669 addr_end = addr + size - 1;
1670
1671 for_each_reserved_mem_region(i, &start, &end) {
1672 if (addr >= start && addr_end <= end)
1673 return true;
1674 }
1675
1676 /* Not found, not a good sign... */
1677 pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n",
1678 &addr, &addr_end);
1679 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
1680 return false;
1681}
1682
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01001683static int gic_reserve_range(phys_addr_t addr, unsigned long size)
1684{
1685 if (efi_enabled(EFI_CONFIG_TABLES))
1686 return efi_mem_reserve_persistent(addr, size);
1687
1688 return 0;
1689}
1690
Marc Zyngier11e37d32018-07-27 13:38:54 +01001691static int __init its_setup_lpi_prop_table(void)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001692{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01001693 if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) {
1694 u64 val;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001695
Marc Zyngierc440a9d2018-07-27 15:40:13 +01001696 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
1697 lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1;
1698
1699 gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12);
1700 gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa,
1701 LPI_PROPBASE_SZ,
1702 MEMREMAP_WB);
1703 gic_reset_prop_table(gic_rdists->prop_table_va);
1704 } else {
1705 struct page *page;
1706
1707 lpi_id_bits = min_t(u32,
1708 GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
1709 ITS_MAX_LPI_NRBITS);
1710 page = its_allocate_prop_table(GFP_NOWAIT);
1711 if (!page) {
1712 pr_err("Failed to allocate PROPBASE\n");
1713 return -ENOMEM;
1714 }
1715
1716 gic_rdists->prop_table_pa = page_to_phys(page);
1717 gic_rdists->prop_table_va = page_address(page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01001718 WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa,
1719 LPI_PROPBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001720 }
1721
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001722 pr_info("GICv3: using LPI property table @%pa\n",
1723 &gic_rdists->prop_table_pa);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001724
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001725 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001726}
1727
1728static const char *its_base_type_string[] = {
1729 [GITS_BASER_TYPE_DEVICE] = "Devices",
1730 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00001731 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001732 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1733 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1734 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1735 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1736};
1737
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001738static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1739{
1740 u32 idx = baser - its->tables;
1741
Vladimir Murzin0968a612016-11-02 11:54:06 +00001742 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001743}
1744
1745static void its_write_baser(struct its_node *its, struct its_baser *baser,
1746 u64 val)
1747{
1748 u32 idx = baser - its->tables;
1749
Vladimir Murzin0968a612016-11-02 11:54:06 +00001750 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001751 baser->val = its_read_baser(its, baser);
1752}
1753
Shanker Donthineni93473592016-06-06 18:17:30 -05001754static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001755 u64 cache, u64 shr, u32 psz, u32 order,
1756 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05001757{
1758 u64 val = its_read_baser(its, baser);
1759 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1760 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001761 u64 baser_phys, tmp;
Shanker Donthineni93473592016-06-06 18:17:30 -05001762 u32 alloc_pages;
Shanker Donthineni539d3782019-01-14 09:50:19 +00001763 struct page *page;
Shanker Donthineni93473592016-06-06 18:17:30 -05001764 void *base;
Shanker Donthineni93473592016-06-06 18:17:30 -05001765
1766retry_alloc_baser:
1767 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1768 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1769 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1770 &its->phys_base, its_base_type_string[type],
1771 alloc_pages, GITS_BASER_PAGES_MAX);
1772 alloc_pages = GITS_BASER_PAGES_MAX;
1773 order = get_order(GITS_BASER_PAGES_MAX * psz);
1774 }
1775
Shanker Donthineni539d3782019-01-14 09:50:19 +00001776 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order);
1777 if (!page)
Shanker Donthineni93473592016-06-06 18:17:30 -05001778 return -ENOMEM;
1779
Shanker Donthineni539d3782019-01-14 09:50:19 +00001780 base = (void *)page_address(page);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001781 baser_phys = virt_to_phys(base);
1782
1783 /* Check if the physical address of the memory is above 48bits */
1784 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
1785
1786 /* 52bit PA is supported only when PageSize=64K */
1787 if (psz != SZ_64K) {
1788 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
1789 free_pages((unsigned long)base, order);
1790 return -ENXIO;
1791 }
1792
1793 /* Convert 52bit PA to 48bit field */
1794 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
1795 }
1796
Shanker Donthineni93473592016-06-06 18:17:30 -05001797retry_baser:
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001798 val = (baser_phys |
Shanker Donthineni93473592016-06-06 18:17:30 -05001799 (type << GITS_BASER_TYPE_SHIFT) |
1800 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1801 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1802 cache |
1803 shr |
1804 GITS_BASER_VALID);
1805
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001806 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1807
Shanker Donthineni93473592016-06-06 18:17:30 -05001808 switch (psz) {
1809 case SZ_4K:
1810 val |= GITS_BASER_PAGE_SIZE_4K;
1811 break;
1812 case SZ_16K:
1813 val |= GITS_BASER_PAGE_SIZE_16K;
1814 break;
1815 case SZ_64K:
1816 val |= GITS_BASER_PAGE_SIZE_64K;
1817 break;
1818 }
1819
1820 its_write_baser(its, baser, val);
1821 tmp = baser->val;
1822
1823 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1824 /*
1825 * Shareability didn't stick. Just use
1826 * whatever the read reported, which is likely
1827 * to be the only thing this redistributor
1828 * supports. If that's zero, make it
1829 * non-cacheable as well.
1830 */
1831 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1832 if (!shr) {
1833 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001834 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001835 }
1836 goto retry_baser;
1837 }
1838
1839 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1840 /*
1841 * Page size didn't stick. Let's try a smaller
1842 * size and retry. If we reach 4K, then
1843 * something is horribly wrong...
1844 */
1845 free_pages((unsigned long)base, order);
1846 baser->base = NULL;
1847
1848 switch (psz) {
1849 case SZ_16K:
1850 psz = SZ_4K;
1851 goto retry_alloc_baser;
1852 case SZ_64K:
1853 psz = SZ_16K;
1854 goto retry_alloc_baser;
1855 }
1856 }
1857
1858 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001859 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001860 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001861 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001862 free_pages((unsigned long)base, order);
1863 return -ENXIO;
1864 }
1865
1866 baser->order = order;
1867 baser->base = base;
1868 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001869 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001870
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001871 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001872 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001873 its_base_type_string[type],
1874 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001875 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001876 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1877
1878 return 0;
1879}
1880
Marc Zyngier4cacac52016-12-19 18:18:34 +00001881static bool its_parse_indirect_baser(struct its_node *its,
1882 struct its_baser *baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001883 u32 psz, u32 *order, u32 ids)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001884{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001885 u64 tmp = its_read_baser(its, baser);
1886 u64 type = GITS_BASER_TYPE(tmp);
1887 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001888 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001889 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001890 bool indirect = false;
1891
1892 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1893 if ((esz << ids) > (psz * 2)) {
1894 /*
1895 * Find out whether hw supports a single or two-level table by
1896 * table by reading bit at offset '62' after writing '1' to it.
1897 */
1898 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1899 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1900
1901 if (indirect) {
1902 /*
1903 * The size of the lvl2 table is equal to ITS page size
1904 * which is 'psz'. For computing lvl1 table size,
1905 * subtract ID bits that sparse lvl2 table from 'ids'
1906 * which is reported by ITS hardware times lvl1 table
1907 * entry size.
1908 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001909 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001910 esz = GITS_LVL1_ENTRY_SIZE;
1911 }
1912 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001913
1914 /*
1915 * Allocate as many entries as required to fit the
1916 * range of device IDs that the ITS can grok... The ID
1917 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001918 * massive waste of memory if two-level device table
1919 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001920 */
1921 new_order = max_t(u32, get_order(esz << ids), new_order);
1922 if (new_order >= MAX_ORDER) {
1923 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001924 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001925 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1926 &its->phys_base, its_base_type_string[type],
1927 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001928 }
1929
1930 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001931
1932 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001933}
1934
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001935static void its_free_tables(struct its_node *its)
1936{
1937 int i;
1938
1939 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001940 if (its->tables[i].base) {
1941 free_pages((unsigned long)its->tables[i].base,
1942 its->tables[i].order);
1943 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001944 }
1945 }
1946}
1947
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001948static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001949{
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001950 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001951 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001952 u32 psz = SZ_64K;
1953 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001954
Ard Biesheuvelfa150012017-10-17 17:55:54 +01001955 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
1956 /* erratum 24313: ignore memory access type */
1957 cache = GITS_BASER_nCnB;
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001958
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001959 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001960 struct its_baser *baser = its->tables + i;
1961 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001962 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001963 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001964 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001965
Marc Zyngier4cacac52016-12-19 18:18:34 +00001966 switch (type) {
1967 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001968 continue;
1969
Marc Zyngier4cacac52016-12-19 18:18:34 +00001970 case GITS_BASER_TYPE_DEVICE:
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001971 indirect = its_parse_indirect_baser(its, baser,
1972 psz, &order,
1973 its->device_ids);
Zenghui Yu8d565742019-02-10 05:24:10 +00001974 break;
1975
Marc Zyngier4cacac52016-12-19 18:18:34 +00001976 case GITS_BASER_TYPE_VCPU:
1977 indirect = its_parse_indirect_baser(its, baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001978 psz, &order,
1979 ITS_MAX_VPEID_BITS);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001980 break;
1981 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001982
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001983 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001984 if (err < 0) {
1985 its_free_tables(its);
1986 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001987 }
1988
Shanker Donthineni93473592016-06-06 18:17:30 -05001989 /* Update settings which will be used for next BASERn */
1990 psz = baser->psz;
1991 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1992 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001993 }
1994
1995 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001996}
1997
1998static int its_alloc_collections(struct its_node *its)
1999{
Marc Zyngier83559b42018-06-22 10:52:52 +01002000 int i;
2001
Kees Cook6396bb22018-06-12 14:03:40 -07002002 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002003 GFP_KERNEL);
2004 if (!its->collections)
2005 return -ENOMEM;
2006
Marc Zyngier83559b42018-06-22 10:52:52 +01002007 for (i = 0; i < nr_cpu_ids; i++)
2008 its->collections[i].target_address = ~0ULL;
2009
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002010 return 0;
2011}
2012
Marc Zyngier7c297a22016-12-19 18:34:38 +00002013static struct page *its_allocate_pending_table(gfp_t gfp_flags)
2014{
2015 struct page *pend_page;
Marc Zyngieradaab502018-07-17 18:06:39 +01002016
Marc Zyngier7c297a22016-12-19 18:34:38 +00002017 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
Marc Zyngieradaab502018-07-17 18:06:39 +01002018 get_order(LPI_PENDBASE_SZ));
Marc Zyngier7c297a22016-12-19 18:34:38 +00002019 if (!pend_page)
2020 return NULL;
2021
2022 /* Make sure the GIC will observe the zero-ed page */
2023 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
2024
2025 return pend_page;
2026}
2027
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002028static void its_free_pending_table(struct page *pt)
2029{
Marc Zyngieradaab502018-07-17 18:06:39 +01002030 free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ));
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002031}
2032
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002033/*
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002034 * Booting with kdump and LPIs enabled is generally fine. Any other
2035 * case is wrong in the absence of firmware/EFI support.
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002036 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002037static bool enabled_lpis_allowed(void)
2038{
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002039 phys_addr_t addr;
2040 u64 val;
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002041
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002042 /* Check whether the property table is in a reserved region */
2043 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2044 addr = val & GENMASK_ULL(51, 12);
2045
2046 return gic_check_reserved_range(addr, LPI_PROPBASE_SZ);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002047}
2048
Marc Zyngier11e37d32018-07-27 13:38:54 +01002049static int __init allocate_lpi_tables(void)
2050{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002051 u64 val;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002052 int err, cpu;
2053
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002054 /*
2055 * If LPIs are enabled while we run this from the boot CPU,
2056 * flag the RD tables as pre-allocated if the stars do align.
2057 */
2058 val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR);
2059 if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) {
2060 gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED |
2061 RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING);
2062 pr_info("GICv3: Using preallocated redistributor tables\n");
2063 }
2064
Marc Zyngier11e37d32018-07-27 13:38:54 +01002065 err = its_setup_lpi_prop_table();
2066 if (err)
2067 return err;
2068
2069 /*
2070 * We allocate all the pending tables anyway, as we may have a
2071 * mix of RDs that have had LPIs enabled, and some that
2072 * don't. We'll free the unused ones as each CPU comes online.
2073 */
2074 for_each_possible_cpu(cpu) {
2075 struct page *pend_page;
2076
2077 pend_page = its_allocate_pending_table(GFP_NOWAIT);
2078 if (!pend_page) {
2079 pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu);
2080 return -ENOMEM;
2081 }
2082
2083 gic_data_rdist_cpu(cpu)->pend_page = pend_page;
2084 }
2085
2086 return 0;
2087}
2088
Heyi Guo64794502019-01-24 21:37:08 +08002089static u64 its_clear_vpend_valid(void __iomem *vlpi_base)
2090{
2091 u32 count = 1000000; /* 1s! */
2092 bool clean;
2093 u64 val;
2094
2095 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2096 val &= ~GICR_VPENDBASER_Valid;
2097 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2098
2099 do {
2100 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2101 clean = !(val & GICR_VPENDBASER_Dirty);
2102 if (!clean) {
2103 count--;
2104 cpu_relax();
2105 udelay(1);
2106 }
2107 } while (!clean && count);
2108
2109 return val;
2110}
2111
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002112static void its_cpu_init_lpis(void)
2113{
2114 void __iomem *rbase = gic_data_rdist_rd_base();
2115 struct page *pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002116 phys_addr_t paddr;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002117 u64 val, tmp;
2118
Marc Zyngier11e37d32018-07-27 13:38:54 +01002119 if (gic_data_rdist()->lpi_enabled)
2120 return;
2121
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002122 val = readl_relaxed(rbase + GICR_CTLR);
2123 if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) &&
2124 (val & GICR_CTLR_ENABLE_LPIS)) {
Marc Zyngierf842ca82018-07-27 16:03:31 +01002125 /*
2126 * Check that we get the same property table on all
2127 * RDs. If we don't, this is hopeless.
2128 */
2129 paddr = gicr_read_propbaser(rbase + GICR_PROPBASER);
2130 paddr &= GENMASK_ULL(51, 12);
2131 if (WARN_ON(gic_rdists->prop_table_pa != paddr))
2132 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
2133
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002134 paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER);
2135 paddr &= GENMASK_ULL(51, 16);
2136
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002137 WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002138 its_free_pending_table(gic_data_rdist()->pend_page);
2139 gic_data_rdist()->pend_page = NULL;
2140
2141 goto out;
2142 }
2143
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002144 pend_page = gic_data_rdist()->pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002145 paddr = page_to_phys(pend_page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01002146 WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002147
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002148 /* set PROPBASE */
Marc Zyngiere1a2e202018-07-27 14:36:00 +01002149 val = (gic_rdists->prop_table_pa |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002150 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002151 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002152 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
2153
Vladimir Murzin0968a612016-11-02 11:54:06 +00002154 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2155 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002156
2157 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002158 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
2159 /*
2160 * The HW reports non-shareable, we must
2161 * remove the cacheability attributes as
2162 * well.
2163 */
2164 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
2165 GICR_PROPBASER_CACHEABILITY_MASK);
2166 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002167 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002168 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002169 pr_info_once("GIC: using cache flushing for LPI property table\n");
2170 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
2171 }
2172
2173 /* set PENDBASE */
2174 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002175 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002176 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002177
Vladimir Murzin0968a612016-11-02 11:54:06 +00002178 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2179 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002180
2181 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
2182 /*
2183 * The HW reports non-shareable, we must remove the
2184 * cacheability attributes as well.
2185 */
2186 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
2187 GICR_PENDBASER_CACHEABILITY_MASK);
2188 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002189 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002190 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002191
2192 /* Enable LPIs */
2193 val = readl_relaxed(rbase + GICR_CTLR);
2194 val |= GICR_CTLR_ENABLE_LPIS;
2195 writel_relaxed(val, rbase + GICR_CTLR);
2196
Heyi Guo64794502019-01-24 21:37:08 +08002197 if (gic_rdists->has_vlpis) {
2198 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2199
2200 /*
2201 * It's possible for CPU to receive VLPIs before it is
2202 * sheduled as a vPE, especially for the first CPU, and the
2203 * VLPI with INTID larger than 2^(IDbits+1) will be considered
2204 * as out of range and dropped by GIC.
2205 * So we initialize IDbits to known value to avoid VLPI drop.
2206 */
2207 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2208 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
2209 smp_processor_id(), val);
2210 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2211
2212 /*
2213 * Also clear Valid bit of GICR_VPENDBASER, in case some
2214 * ancient programming gets left in and has possibility of
2215 * corrupting memory.
2216 */
2217 val = its_clear_vpend_valid(vlpi_base);
2218 WARN_ON(val & GICR_VPENDBASER_Dirty);
2219 }
2220
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002221 /* Make sure the GIC has seen the above */
2222 dsb(sy);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002223out:
Marc Zyngier11e37d32018-07-27 13:38:54 +01002224 gic_data_rdist()->lpi_enabled = true;
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002225 pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n",
Marc Zyngier11e37d32018-07-27 13:38:54 +01002226 smp_processor_id(),
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002227 gic_data_rdist()->pend_page ? "allocated" : "reserved",
Marc Zyngier11e37d32018-07-27 13:38:54 +01002228 &paddr);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002229}
2230
Derek Basehore920181c2018-02-28 21:48:20 -08002231static void its_cpu_init_collection(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002232{
Derek Basehore920181c2018-02-28 21:48:20 -08002233 int cpu = smp_processor_id();
2234 u64 target;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002235
Derek Basehore920181c2018-02-28 21:48:20 -08002236 /* avoid cross node collections and its mapping */
2237 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
2238 struct device_node *cpu_node;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002239
Derek Basehore920181c2018-02-28 21:48:20 -08002240 cpu_node = of_get_cpu_node(cpu, NULL);
2241 if (its->numa_node != NUMA_NO_NODE &&
2242 its->numa_node != of_node_to_nid(cpu_node))
2243 return;
2244 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002245
Derek Basehore920181c2018-02-28 21:48:20 -08002246 /*
2247 * We now have to bind each collection to its target
2248 * redistributor.
2249 */
2250 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002251 /*
Derek Basehore920181c2018-02-28 21:48:20 -08002252 * This ITS wants the physical address of the
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002253 * redistributor.
2254 */
Derek Basehore920181c2018-02-28 21:48:20 -08002255 target = gic_data_rdist()->phys_base;
2256 } else {
2257 /* This ITS wants a linear CPU number. */
2258 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2259 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002260 }
2261
Derek Basehore920181c2018-02-28 21:48:20 -08002262 /* Perform collection mapping */
2263 its->collections[cpu].target_address = target;
2264 its->collections[cpu].col_id = cpu;
2265
2266 its_send_mapc(its, &its->collections[cpu], 1);
2267 its_send_invall(its, &its->collections[cpu]);
2268}
2269
2270static void its_cpu_init_collections(void)
2271{
2272 struct its_node *its;
2273
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02002274 raw_spin_lock(&its_lock);
Derek Basehore920181c2018-02-28 21:48:20 -08002275
2276 list_for_each_entry(its, &its_nodes, entry)
2277 its_cpu_init_collection(its);
2278
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02002279 raw_spin_unlock(&its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002280}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002281
2282static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
2283{
2284 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002285 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002286
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002287 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002288
2289 list_for_each_entry(tmp, &its->its_device_list, entry) {
2290 if (tmp->device_id == dev_id) {
2291 its_dev = tmp;
2292 break;
2293 }
2294 }
2295
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002296 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002297
2298 return its_dev;
2299}
2300
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002301static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2302{
2303 int i;
2304
2305 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2306 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2307 return &its->tables[i];
2308 }
2309
2310 return NULL;
2311}
2312
Shanker Donthineni539d3782019-01-14 09:50:19 +00002313static bool its_alloc_table_entry(struct its_node *its,
2314 struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002315{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002316 struct page *page;
2317 u32 esz, idx;
2318 __le64 *table;
2319
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002320 /* Don't allow device id that exceeds single, flat table limit */
2321 esz = GITS_BASER_ENTRY_SIZE(baser->val);
2322 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002323 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002324
2325 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002326 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002327 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
2328 return false;
2329
2330 table = baser->base;
2331
2332 /* Allocate memory for 2nd level table */
2333 if (!table[idx]) {
Shanker Donthineni539d3782019-01-14 09:50:19 +00002334 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
2335 get_order(baser->psz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002336 if (!page)
2337 return false;
2338
2339 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2340 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00002341 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002342
2343 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2344
2345 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2346 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00002347 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002348
2349 /* Ensure updated table contents are visible to ITS hardware */
2350 dsb(sy);
2351 }
2352
2353 return true;
2354}
2355
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002356static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
2357{
2358 struct its_baser *baser;
2359
2360 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
2361
2362 /* Don't allow device id that exceeds ITS hardware limit */
2363 if (!baser)
2364 return (ilog2(dev_id) < its->device_ids);
2365
Shanker Donthineni539d3782019-01-14 09:50:19 +00002366 return its_alloc_table_entry(its, baser, dev_id);
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002367}
2368
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002369static bool its_alloc_vpe_table(u32 vpe_id)
2370{
2371 struct its_node *its;
2372
2373 /*
2374 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
2375 * could try and only do it on ITSs corresponding to devices
2376 * that have interrupts targeted at this VPE, but the
2377 * complexity becomes crazy (and you have tons of memory
2378 * anyway, right?).
2379 */
2380 list_for_each_entry(its, &its_nodes, entry) {
2381 struct its_baser *baser;
2382
2383 if (!its->is_v4)
2384 continue;
2385
2386 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
2387 if (!baser)
2388 return false;
2389
Shanker Donthineni539d3782019-01-14 09:50:19 +00002390 if (!its_alloc_table_entry(its, baser, vpe_id))
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002391 return false;
2392 }
2393
2394 return true;
2395}
2396
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002397static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002398 int nvecs, bool alloc_lpis)
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002399{
2400 struct its_device *dev;
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002401 unsigned long *lpi_map = NULL;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002402 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002403 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002404 void *itt;
2405 int lpi_base;
2406 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00002407 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002408 int sz;
2409
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002410 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002411 return NULL;
2412
Marc Zyngier147c8f32018-05-27 16:39:55 +01002413 if (WARN_ON(!is_power_of_2(nvecs)))
2414 nvecs = roundup_pow_of_two(nvecs);
2415
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002416 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00002417 /*
Marc Zyngier147c8f32018-05-27 16:39:55 +01002418 * Even if the device wants a single LPI, the ITT must be
2419 * sized as a power of two (and you need at least one bit...).
Marc Zyngierc8481262014-12-12 10:51:24 +00002420 */
Marc Zyngier147c8f32018-05-27 16:39:55 +01002421 nr_ites = max(2, nvecs);
Marc Zyngierc8481262014-12-12 10:51:24 +00002422 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002423 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Shanker Donthineni539d3782019-01-14 09:50:19 +00002424 itt = kzalloc_node(sz, GFP_KERNEL, its->numa_node);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002425 if (alloc_lpis) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002426 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002427 if (lpi_map)
Kees Cook6396bb22018-06-12 14:03:40 -07002428 col_map = kcalloc(nr_lpis, sizeof(*col_map),
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002429 GFP_KERNEL);
2430 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07002431 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002432 nr_lpis = 0;
2433 lpi_base = 0;
2434 }
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002435
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002436 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002437 kfree(dev);
2438 kfree(itt);
2439 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002440 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002441 return NULL;
2442 }
2443
Vladimir Murzin328191c2016-11-02 11:54:05 +00002444 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01002445
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002446 dev->its = its;
2447 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00002448 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002449 dev->event_map.lpi_map = lpi_map;
2450 dev->event_map.col_map = col_map;
2451 dev->event_map.lpi_base = lpi_base;
2452 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00002453 mutex_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002454 dev->device_id = dev_id;
2455 INIT_LIST_HEAD(&dev->entry);
2456
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002457 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002458 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002459 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002460
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002461 /* Map device to its ITT */
2462 its_send_mapd(dev, 1);
2463
2464 return dev;
2465}
2466
2467static void its_free_device(struct its_device *its_dev)
2468{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002469 unsigned long flags;
2470
2471 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002472 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002473 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002474 kfree(its_dev->itt);
2475 kfree(its_dev);
2476}
Marc Zyngierb48ac832014-11-24 14:35:16 +00002477
Marc Zyngier8208d172019-01-18 14:08:59 +00002478static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
Marc Zyngierb48ac832014-11-24 14:35:16 +00002479{
2480 int idx;
2481
Zenghui Yu342be102019-07-27 06:14:22 +00002482 /* Find a free LPI region in lpi_map and allocate them. */
Marc Zyngier8208d172019-01-18 14:08:59 +00002483 idx = bitmap_find_free_region(dev->event_map.lpi_map,
2484 dev->event_map.nr_lpis,
2485 get_count_order(nvecs));
2486 if (idx < 0)
Marc Zyngierb48ac832014-11-24 14:35:16 +00002487 return -ENOSPC;
2488
Marc Zyngier591e5be2015-07-17 10:46:42 +01002489 *hwirq = dev->event_map.lpi_base + idx;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002490
Marc Zyngierb48ac832014-11-24 14:35:16 +00002491 return 0;
2492}
2493
Marc Zyngier54456db2015-07-28 14:46:21 +01002494static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2495 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00002496{
Marc Zyngierb48ac832014-11-24 14:35:16 +00002497 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002498 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01002499 struct msi_domain_info *msi_info;
2500 u32 dev_id;
Marc Zyngier9791ec72019-01-29 10:02:33 +00002501 int err = 0;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002502
Marc Zyngier54456db2015-07-28 14:46:21 +01002503 /*
Julien Gralla7c90f52019-04-18 16:58:14 +01002504 * We ignore "dev" entirely, and rely on the dev_id that has
Marc Zyngier54456db2015-07-28 14:46:21 +01002505 * been passed via the scratchpad. This limits this domain's
2506 * usefulness to upper layers that definitely know that they
2507 * are built on top of the ITS.
2508 */
2509 dev_id = info->scratchpad[0].ul;
2510
2511 msi_info = msi_get_domain_info(domain);
2512 its = msi_info->data;
2513
Marc Zyngier20b3d542016-12-20 15:23:22 +00002514 if (!gic_rdists->has_direct_lpi &&
2515 vpe_proxy.dev &&
2516 vpe_proxy.dev->its == its &&
2517 dev_id == vpe_proxy.dev->device_id) {
2518 /* Bad luck. Get yourself a better implementation */
2519 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
2520 dev_id);
2521 return -EINVAL;
2522 }
2523
Marc Zyngier9791ec72019-01-29 10:02:33 +00002524 mutex_lock(&its->dev_alloc_lock);
Marc Zyngierf1304202015-07-28 14:46:18 +01002525 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002526 if (its_dev) {
2527 /*
2528 * We already have seen this ID, probably through
2529 * another alias (PCI bridge of some sort). No need to
2530 * create the device.
2531 */
Marc Zyngier9791ec72019-01-29 10:02:33 +00002532 its_dev->shared = true;
Marc Zyngierf1304202015-07-28 14:46:18 +01002533 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002534 goto out;
2535 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002536
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002537 its_dev = its_create_device(its, dev_id, nvec, true);
Marc Zyngier9791ec72019-01-29 10:02:33 +00002538 if (!its_dev) {
2539 err = -ENOMEM;
2540 goto out;
2541 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002542
Marc Zyngierf1304202015-07-28 14:46:18 +01002543 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00002544out:
Marc Zyngier9791ec72019-01-29 10:02:33 +00002545 mutex_unlock(&its->dev_alloc_lock);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002546 info->scratchpad[0].ptr = its_dev;
Marc Zyngier9791ec72019-01-29 10:02:33 +00002547 return err;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002548}
2549
Marc Zyngier54456db2015-07-28 14:46:21 +01002550static struct msi_domain_ops its_msi_domain_ops = {
2551 .msi_prepare = its_msi_prepare,
2552};
2553
Marc Zyngierb48ac832014-11-24 14:35:16 +00002554static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2555 unsigned int virq,
2556 irq_hw_number_t hwirq)
2557{
Marc Zyngierf833f572015-10-13 12:51:33 +01002558 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002559
Marc Zyngierf833f572015-10-13 12:51:33 +01002560 if (irq_domain_get_of_node(domain->parent)) {
2561 fwspec.fwnode = domain->parent->fwnode;
2562 fwspec.param_count = 3;
2563 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2564 fwspec.param[1] = hwirq;
2565 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002566 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2567 fwspec.fwnode = domain->parent->fwnode;
2568 fwspec.param_count = 2;
2569 fwspec.param[0] = hwirq;
2570 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01002571 } else {
2572 return -EINVAL;
2573 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002574
Marc Zyngierf833f572015-10-13 12:51:33 +01002575 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002576}
2577
2578static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2579 unsigned int nr_irqs, void *args)
2580{
2581 msi_alloc_info_t *info = args;
2582 struct its_device *its_dev = info->scratchpad[0].ptr;
Julien Grall35ae7df2019-05-01 14:58:21 +01002583 struct its_node *its = its_dev->its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002584 irq_hw_number_t hwirq;
2585 int err;
2586 int i;
2587
Marc Zyngier8208d172019-01-18 14:08:59 +00002588 err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
2589 if (err)
2590 return err;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002591
Julien Grall35ae7df2019-05-01 14:58:21 +01002592 err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev));
2593 if (err)
2594 return err;
2595
Marc Zyngier8208d172019-01-18 14:08:59 +00002596 for (i = 0; i < nr_irqs; i++) {
2597 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002598 if (err)
2599 return err;
2600
2601 irq_domain_set_hwirq_and_chip(domain, virq + i,
Marc Zyngier8208d172019-01-18 14:08:59 +00002602 hwirq + i, &its_irq_chip, its_dev);
Marc Zyngier0d224d32017-08-18 09:39:18 +01002603 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
Marc Zyngierf1304202015-07-28 14:46:18 +01002604 pr_debug("ID:%d pID:%d vID:%d\n",
Marc Zyngier8208d172019-01-18 14:08:59 +00002605 (int)(hwirq + i - its_dev->event_map.lpi_base),
2606 (int)(hwirq + i), virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002607 }
2608
2609 return 0;
2610}
2611
Thomas Gleixner72491642017-09-13 23:29:10 +02002612static int its_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01002613 struct irq_data *d, bool reserve)
Marc Zyngieraca268d2014-12-12 10:51:23 +00002614{
2615 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2616 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002617 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngier0d224d32017-08-18 09:39:18 +01002618 int cpu;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002619
2620 /* get the cpu_mask of local node */
2621 if (its_dev->its->numa_node >= 0)
2622 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002623
Marc Zyngier591e5be2015-07-17 10:46:42 +01002624 /* Bind the LPI to the first possible CPU */
Yang Yingliangc1797b12018-06-22 10:52:51 +01002625 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
2626 if (cpu >= nr_cpu_ids) {
2627 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
2628 return -EINVAL;
2629
2630 cpu = cpumask_first(cpu_online_mask);
2631 }
2632
Marc Zyngier0d224d32017-08-18 09:39:18 +01002633 its_dev->event_map.col_map[event] = cpu;
2634 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngier591e5be2015-07-17 10:46:42 +01002635
Marc Zyngieraca268d2014-12-12 10:51:23 +00002636 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00002637 its_send_mapti(its_dev, d->hwirq, event);
Thomas Gleixner72491642017-09-13 23:29:10 +02002638 return 0;
Marc Zyngieraca268d2014-12-12 10:51:23 +00002639}
2640
2641static void its_irq_domain_deactivate(struct irq_domain *domain,
2642 struct irq_data *d)
2643{
2644 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2645 u32 event = its_get_event_id(d);
2646
2647 /* Stop the delivery of interrupts */
2648 its_send_discard(its_dev, event);
2649}
2650
Marc Zyngierb48ac832014-11-24 14:35:16 +00002651static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2652 unsigned int nr_irqs)
2653{
2654 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2655 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier9791ec72019-01-29 10:02:33 +00002656 struct its_node *its = its_dev->its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002657 int i;
2658
Marc Zyngierc9c96e32019-09-05 14:56:47 +01002659 bitmap_release_region(its_dev->event_map.lpi_map,
2660 its_get_event_id(irq_domain_get_irq_data(domain, virq)),
2661 get_count_order(nr_irqs));
2662
Marc Zyngierb48ac832014-11-24 14:35:16 +00002663 for (i = 0; i < nr_irqs; i++) {
2664 struct irq_data *data = irq_domain_get_irq_data(domain,
2665 virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002666 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00002667 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002668 }
2669
Marc Zyngier9791ec72019-01-29 10:02:33 +00002670 mutex_lock(&its->dev_alloc_lock);
2671
2672 /*
2673 * If all interrupts have been freed, start mopping the
2674 * floor. This is conditionned on the device not being shared.
2675 */
2676 if (!its_dev->shared &&
2677 bitmap_empty(its_dev->event_map.lpi_map,
Marc Zyngier591e5be2015-07-17 10:46:42 +01002678 its_dev->event_map.nr_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002679 its_lpi_free(its_dev->event_map.lpi_map,
2680 its_dev->event_map.lpi_base,
2681 its_dev->event_map.nr_lpis);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00002682 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002683
2684 /* Unmap device/itt */
2685 its_send_mapd(its_dev, 0);
2686 its_free_device(its_dev);
2687 }
2688
Marc Zyngier9791ec72019-01-29 10:02:33 +00002689 mutex_unlock(&its->dev_alloc_lock);
2690
Marc Zyngierb48ac832014-11-24 14:35:16 +00002691 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2692}
2693
2694static const struct irq_domain_ops its_domain_ops = {
2695 .alloc = its_irq_domain_alloc,
2696 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00002697 .activate = its_irq_domain_activate,
2698 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00002699};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002700
Marc Zyngier20b3d542016-12-20 15:23:22 +00002701/*
2702 * This is insane.
2703 *
2704 * If a GICv4 doesn't implement Direct LPIs (which is extremely
2705 * likely), the only way to perform an invalidate is to use a fake
2706 * device to issue an INV command, implying that the LPI has first
2707 * been mapped to some event on that device. Since this is not exactly
2708 * cheap, we try to keep that mapping around as long as possible, and
2709 * only issue an UNMAP if we're short on available slots.
2710 *
2711 * Broken by design(tm).
2712 */
2713static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
2714{
2715 /* Already unmapped? */
2716 if (vpe->vpe_proxy_event == -1)
2717 return;
2718
2719 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
2720 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
2721
2722 /*
2723 * We don't track empty slots at all, so let's move the
2724 * next_victim pointer if we can quickly reuse that slot
2725 * instead of nuking an existing entry. Not clear that this is
2726 * always a win though, and this might just generate a ripple
2727 * effect... Let's just hope VPEs don't migrate too often.
2728 */
2729 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2730 vpe_proxy.next_victim = vpe->vpe_proxy_event;
2731
2732 vpe->vpe_proxy_event = -1;
2733}
2734
2735static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
2736{
2737 if (!gic_rdists->has_direct_lpi) {
2738 unsigned long flags;
2739
2740 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2741 its_vpe_db_proxy_unmap_locked(vpe);
2742 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2743 }
2744}
2745
2746static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
2747{
2748 /* Already mapped? */
2749 if (vpe->vpe_proxy_event != -1)
2750 return;
2751
2752 /* This slot was already allocated. Kick the other VPE out. */
2753 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2754 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
2755
2756 /* Map the new VPE instead */
2757 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
2758 vpe->vpe_proxy_event = vpe_proxy.next_victim;
2759 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
2760
2761 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
2762 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
2763}
2764
Marc Zyngier958b90d2017-08-18 16:14:17 +01002765static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
2766{
2767 unsigned long flags;
2768 struct its_collection *target_col;
2769
2770 if (gic_rdists->has_direct_lpi) {
2771 void __iomem *rdbase;
2772
2773 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
2774 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2775 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2776 cpu_relax();
2777
2778 return;
2779 }
2780
2781 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2782
2783 its_vpe_db_proxy_map_locked(vpe);
2784
2785 target_col = &vpe_proxy.dev->its->collections[to];
2786 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
2787 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
2788
2789 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2790}
2791
Marc Zyngier3171a472016-12-20 15:17:28 +00002792static int its_vpe_set_affinity(struct irq_data *d,
2793 const struct cpumask *mask_val,
2794 bool force)
2795{
2796 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2797 int cpu = cpumask_first(mask_val);
2798
2799 /*
2800 * Changing affinity is mega expensive, so let's be as lazy as
Marc Zyngier20b3d542016-12-20 15:23:22 +00002801 * we can and only do it if we really have to. Also, if mapped
Marc Zyngier958b90d2017-08-18 16:14:17 +01002802 * into the proxy device, we need to move the doorbell
2803 * interrupt to its new location.
Marc Zyngier3171a472016-12-20 15:17:28 +00002804 */
2805 if (vpe->col_idx != cpu) {
Marc Zyngier958b90d2017-08-18 16:14:17 +01002806 int from = vpe->col_idx;
2807
Marc Zyngier3171a472016-12-20 15:17:28 +00002808 vpe->col_idx = cpu;
2809 its_send_vmovp(vpe);
Marc Zyngier958b90d2017-08-18 16:14:17 +01002810 its_vpe_db_proxy_move(vpe, from, cpu);
Marc Zyngier3171a472016-12-20 15:17:28 +00002811 }
2812
Marc Zyngier44c4c252017-10-19 10:11:34 +01002813 irq_data_update_effective_affinity(d, cpumask_of(cpu));
2814
Marc Zyngier3171a472016-12-20 15:17:28 +00002815 return IRQ_SET_MASK_OK_DONE;
2816}
2817
Marc Zyngiere643d802016-12-20 15:09:31 +00002818static void its_vpe_schedule(struct its_vpe *vpe)
2819{
Robin Murphy50c33092018-02-16 16:57:56 +00002820 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00002821 u64 val;
2822
2823 /* Schedule the VPE */
2824 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2825 GENMASK_ULL(51, 12);
2826 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2827 val |= GICR_VPROPBASER_RaWb;
2828 val |= GICR_VPROPBASER_InnerShareable;
2829 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2830
2831 val = virt_to_phys(page_address(vpe->vpt_page)) &
2832 GENMASK_ULL(51, 16);
2833 val |= GICR_VPENDBASER_RaWaWb;
2834 val |= GICR_VPENDBASER_NonShareable;
2835 /*
2836 * There is no good way of finding out if the pending table is
2837 * empty as we can race against the doorbell interrupt very
2838 * easily. So in the end, vpe->pending_last is only an
2839 * indication that the vcpu has something pending, not one
2840 * that the pending table is empty. A good implementation
2841 * would be able to read its coarse map pretty quickly anyway,
2842 * making this a tolerable issue.
2843 */
2844 val |= GICR_VPENDBASER_PendingLast;
2845 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2846 val |= GICR_VPENDBASER_Valid;
2847 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2848}
2849
2850static void its_vpe_deschedule(struct its_vpe *vpe)
2851{
Robin Murphy50c33092018-02-16 16:57:56 +00002852 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00002853 u64 val;
2854
Heyi Guo64794502019-01-24 21:37:08 +08002855 val = its_clear_vpend_valid(vlpi_base);
Marc Zyngiere643d802016-12-20 15:09:31 +00002856
Heyi Guo64794502019-01-24 21:37:08 +08002857 if (unlikely(val & GICR_VPENDBASER_Dirty)) {
Marc Zyngiere643d802016-12-20 15:09:31 +00002858 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2859 vpe->idai = false;
2860 vpe->pending_last = true;
2861 } else {
2862 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2863 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2864 }
2865}
2866
Marc Zyngier40619a22017-10-08 15:16:09 +01002867static void its_vpe_invall(struct its_vpe *vpe)
2868{
2869 struct its_node *its;
2870
2871 list_for_each_entry(its, &its_nodes, entry) {
2872 if (!its->is_v4)
2873 continue;
2874
Marc Zyngier2247e1b2017-10-08 18:50:36 +01002875 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
2876 continue;
2877
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01002878 /*
2879 * Sending a VINVALL to a single ITS is enough, as all
2880 * we need is to reach the redistributors.
2881 */
Marc Zyngier40619a22017-10-08 15:16:09 +01002882 its_send_vinvall(its, vpe);
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01002883 return;
Marc Zyngier40619a22017-10-08 15:16:09 +01002884 }
2885}
2886
Marc Zyngiere643d802016-12-20 15:09:31 +00002887static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2888{
2889 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2890 struct its_cmd_info *info = vcpu_info;
2891
2892 switch (info->cmd_type) {
2893 case SCHEDULE_VPE:
2894 its_vpe_schedule(vpe);
2895 return 0;
2896
2897 case DESCHEDULE_VPE:
2898 its_vpe_deschedule(vpe);
2899 return 0;
2900
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002901 case INVALL_VPE:
Marc Zyngier40619a22017-10-08 15:16:09 +01002902 its_vpe_invall(vpe);
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002903 return 0;
2904
Marc Zyngiere643d802016-12-20 15:09:31 +00002905 default:
2906 return -EINVAL;
2907 }
2908}
2909
Marc Zyngier20b3d542016-12-20 15:23:22 +00002910static void its_vpe_send_cmd(struct its_vpe *vpe,
2911 void (*cmd)(struct its_device *, u32))
2912{
2913 unsigned long flags;
2914
2915 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2916
2917 its_vpe_db_proxy_map_locked(vpe);
2918 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
2919
2920 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2921}
2922
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002923static void its_vpe_send_inv(struct irq_data *d)
2924{
2925 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002926
Marc Zyngier20b3d542016-12-20 15:23:22 +00002927 if (gic_rdists->has_direct_lpi) {
2928 void __iomem *rdbase;
2929
2930 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2931 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
2932 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2933 cpu_relax();
2934 } else {
2935 its_vpe_send_cmd(vpe, its_send_inv);
2936 }
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002937}
2938
2939static void its_vpe_mask_irq(struct irq_data *d)
2940{
2941 /*
2942 * We need to unmask the LPI, which is described by the parent
2943 * irq_data. Instead of calling into the parent (which won't
2944 * exactly do the right thing, let's simply use the
2945 * parent_data pointer. Yes, I'm naughty.
2946 */
2947 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
2948 its_vpe_send_inv(d);
2949}
2950
2951static void its_vpe_unmask_irq(struct irq_data *d)
2952{
2953 /* Same hack as above... */
2954 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
2955 its_vpe_send_inv(d);
2956}
2957
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002958static int its_vpe_set_irqchip_state(struct irq_data *d,
2959 enum irqchip_irq_state which,
2960 bool state)
2961{
2962 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2963
2964 if (which != IRQCHIP_STATE_PENDING)
2965 return -EINVAL;
2966
2967 if (gic_rdists->has_direct_lpi) {
2968 void __iomem *rdbase;
2969
2970 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2971 if (state) {
2972 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
2973 } else {
2974 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2975 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2976 cpu_relax();
2977 }
2978 } else {
2979 if (state)
2980 its_vpe_send_cmd(vpe, its_send_int);
2981 else
2982 its_vpe_send_cmd(vpe, its_send_clear);
2983 }
2984
2985 return 0;
2986}
2987
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002988static struct irq_chip its_vpe_irq_chip = {
2989 .name = "GICv4-vpe",
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002990 .irq_mask = its_vpe_mask_irq,
2991 .irq_unmask = its_vpe_unmask_irq,
2992 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier3171a472016-12-20 15:17:28 +00002993 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002994 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
Marc Zyngiere643d802016-12-20 15:09:31 +00002995 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002996};
2997
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002998static int its_vpe_id_alloc(void)
2999{
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05003000 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003001}
3002
3003static void its_vpe_id_free(u16 id)
3004{
3005 ida_simple_remove(&its_vpeid_ida, id);
3006}
3007
3008static int its_vpe_init(struct its_vpe *vpe)
3009{
3010 struct page *vpt_page;
3011 int vpe_id;
3012
3013 /* Allocate vpe_id */
3014 vpe_id = its_vpe_id_alloc();
3015 if (vpe_id < 0)
3016 return vpe_id;
3017
3018 /* Allocate VPT */
3019 vpt_page = its_allocate_pending_table(GFP_KERNEL);
3020 if (!vpt_page) {
3021 its_vpe_id_free(vpe_id);
3022 return -ENOMEM;
3023 }
3024
3025 if (!its_alloc_vpe_table(vpe_id)) {
3026 its_vpe_id_free(vpe_id);
Nianyao Tang34f8eb92019-07-26 17:32:57 +08003027 its_free_pending_table(vpt_page);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003028 return -ENOMEM;
3029 }
3030
3031 vpe->vpe_id = vpe_id;
3032 vpe->vpt_page = vpt_page;
Marc Zyngier20b3d542016-12-20 15:23:22 +00003033 vpe->vpe_proxy_event = -1;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003034
3035 return 0;
3036}
3037
3038static void its_vpe_teardown(struct its_vpe *vpe)
3039{
Marc Zyngier20b3d542016-12-20 15:23:22 +00003040 its_vpe_db_proxy_unmap(vpe);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003041 its_vpe_id_free(vpe->vpe_id);
3042 its_free_pending_table(vpe->vpt_page);
3043}
3044
3045static void its_vpe_irq_domain_free(struct irq_domain *domain,
3046 unsigned int virq,
3047 unsigned int nr_irqs)
3048{
3049 struct its_vm *vm = domain->host_data;
3050 int i;
3051
3052 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3053
3054 for (i = 0; i < nr_irqs; i++) {
3055 struct irq_data *data = irq_domain_get_irq_data(domain,
3056 virq + i);
3057 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
3058
3059 BUG_ON(vm != vpe->its_vm);
3060
3061 clear_bit(data->hwirq, vm->db_bitmap);
3062 its_vpe_teardown(vpe);
3063 irq_domain_reset_irq_data(data);
3064 }
3065
3066 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003067 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003068 its_free_prop_table(vm->vprop_page);
3069 }
3070}
3071
3072static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
3073 unsigned int nr_irqs, void *args)
3074{
3075 struct its_vm *vm = args;
3076 unsigned long *bitmap;
3077 struct page *vprop_page;
3078 int base, nr_ids, i, err = 0;
3079
3080 BUG_ON(!vm);
3081
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003082 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003083 if (!bitmap)
3084 return -ENOMEM;
3085
3086 if (nr_ids < nr_irqs) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003087 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003088 return -ENOMEM;
3089 }
3090
3091 vprop_page = its_allocate_prop_table(GFP_KERNEL);
3092 if (!vprop_page) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003093 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003094 return -ENOMEM;
3095 }
3096
3097 vm->db_bitmap = bitmap;
3098 vm->db_lpi_base = base;
3099 vm->nr_db_lpis = nr_ids;
3100 vm->vprop_page = vprop_page;
3101
3102 for (i = 0; i < nr_irqs; i++) {
3103 vm->vpes[i]->vpe_db_lpi = base + i;
3104 err = its_vpe_init(vm->vpes[i]);
3105 if (err)
3106 break;
3107 err = its_irq_gic_domain_alloc(domain, virq + i,
3108 vm->vpes[i]->vpe_db_lpi);
3109 if (err)
3110 break;
3111 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
3112 &its_vpe_irq_chip, vm->vpes[i]);
3113 set_bit(i, bitmap);
3114 }
3115
3116 if (err) {
3117 if (i > 0)
3118 its_vpe_irq_domain_free(domain, virq, i - 1);
3119
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003120 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003121 its_free_prop_table(vprop_page);
3122 }
3123
3124 return err;
3125}
3126
Thomas Gleixner72491642017-09-13 23:29:10 +02003127static int its_vpe_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01003128 struct irq_data *d, bool reserve)
Marc Zyngiereb781922016-12-20 14:47:05 +00003129{
3130 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier40619a22017-10-08 15:16:09 +01003131 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00003132
Marc Zyngier2247e1b2017-10-08 18:50:36 +01003133 /* If we use the list map, we issue VMAPP on demand... */
3134 if (its_list_map)
Marc Zyngier6ef930f2017-11-07 10:04:38 +00003135 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00003136
3137 /* Map the VPE to the first possible CPU */
3138 vpe->col_idx = cpumask_first(cpu_online_mask);
Marc Zyngier40619a22017-10-08 15:16:09 +01003139
3140 list_for_each_entry(its, &its_nodes, entry) {
3141 if (!its->is_v4)
3142 continue;
3143
Marc Zyngier75fd9512017-10-08 18:46:39 +01003144 its_send_vmapp(its, vpe, true);
Marc Zyngier40619a22017-10-08 15:16:09 +01003145 its_send_vinvall(its, vpe);
3146 }
3147
Marc Zyngier44c4c252017-10-19 10:11:34 +01003148 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
3149
Thomas Gleixner72491642017-09-13 23:29:10 +02003150 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00003151}
3152
3153static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
3154 struct irq_data *d)
3155{
3156 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier75fd9512017-10-08 18:46:39 +01003157 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00003158
Marc Zyngier2247e1b2017-10-08 18:50:36 +01003159 /*
3160 * If we use the list map, we unmap the VPE once no VLPIs are
3161 * associated with the VM.
3162 */
3163 if (its_list_map)
3164 return;
3165
Marc Zyngier75fd9512017-10-08 18:46:39 +01003166 list_for_each_entry(its, &its_nodes, entry) {
3167 if (!its->is_v4)
3168 continue;
3169
3170 its_send_vmapp(its, vpe, false);
3171 }
Marc Zyngiereb781922016-12-20 14:47:05 +00003172}
3173
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003174static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003175 .alloc = its_vpe_irq_domain_alloc,
3176 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00003177 .activate = its_vpe_irq_domain_activate,
3178 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003179};
3180
Yun Wu4559fbb2015-03-06 16:37:50 +00003181static int its_force_quiescent(void __iomem *base)
3182{
3183 u32 count = 1000000; /* 1s */
3184 u32 val;
3185
3186 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07003187 /*
3188 * GIC architecture specification requires the ITS to be both
3189 * disabled and quiescent for writes to GITS_BASER<n> or
3190 * GITS_CBASER to not have UNPREDICTABLE results.
3191 */
3192 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00003193 return 0;
3194
3195 /* Disable the generation of all interrupts to this ITS */
Marc Zyngierd51c4b42017-06-27 21:24:25 +01003196 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
Yun Wu4559fbb2015-03-06 16:37:50 +00003197 writel_relaxed(val, base + GITS_CTLR);
3198
3199 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
3200 while (1) {
3201 val = readl_relaxed(base + GITS_CTLR);
3202 if (val & GITS_CTLR_QUIESCENT)
3203 return 0;
3204
3205 count--;
3206 if (!count)
3207 return -EBUSY;
3208
3209 cpu_relax();
3210 udelay(1);
3211 }
3212}
3213
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003214static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
Robert Richter94100972015-09-21 22:58:38 +02003215{
3216 struct its_node *its = data;
3217
Ard Biesheuvelfa150012017-10-17 17:55:54 +01003218 /* erratum 22375: only alloc 8MB table size */
3219 its->device_ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02003220 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003221
3222 return true;
Robert Richter94100972015-09-21 22:58:38 +02003223}
3224
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003225static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003226{
3227 struct its_node *its = data;
3228
3229 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003230
3231 return true;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003232}
3233
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003234static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
Shanker Donthineni90922a22017-03-07 08:20:38 -06003235{
3236 struct its_node *its = data;
3237
3238 /* On QDF2400, the size of the ITE is 16Bytes */
3239 its->ite_size = 16;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003240
3241 return true;
Shanker Donthineni90922a22017-03-07 08:20:38 -06003242}
3243
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003244static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
3245{
3246 struct its_node *its = its_dev->its;
3247
3248 /*
3249 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
3250 * which maps 32-bit writes targeted at a separate window of
3251 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
3252 * with device ID taken from bits [device_id_bits + 1:2] of
3253 * the window offset.
3254 */
3255 return its->pre_its_base + (its_dev->device_id << 2);
3256}
3257
3258static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
3259{
3260 struct its_node *its = data;
3261 u32 pre_its_window[2];
3262 u32 ids;
3263
3264 if (!fwnode_property_read_u32_array(its->fwnode_handle,
3265 "socionext,synquacer-pre-its",
3266 pre_its_window,
3267 ARRAY_SIZE(pre_its_window))) {
3268
3269 its->pre_its_base = pre_its_window[0];
3270 its->get_msi_base = its_irq_get_msi_base_pre_its;
3271
3272 ids = ilog2(pre_its_window[1]) - 2;
3273 if (its->device_ids > ids)
3274 its->device_ids = ids;
3275
3276 /* the pre-ITS breaks isolation, so disable MSI remapping */
3277 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
3278 return true;
3279 }
3280 return false;
3281}
3282
Marc Zyngier5c9a8822017-07-28 21:20:37 +01003283static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
3284{
3285 struct its_node *its = data;
3286
3287 /*
3288 * Hip07 insists on using the wrong address for the VLPI
3289 * page. Trick it into doing the right thing...
3290 */
3291 its->vlpi_redist_offset = SZ_128K;
3292 return true;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00003293}
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003294
Robert Richter67510cc2015-09-21 22:58:37 +02003295static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02003296#ifdef CONFIG_CAVIUM_ERRATUM_22375
3297 {
3298 .desc = "ITS: Cavium errata 22375, 24313",
3299 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3300 .mask = 0xffff0fff,
3301 .init = its_enable_quirk_cavium_22375,
3302 },
3303#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003304#ifdef CONFIG_CAVIUM_ERRATUM_23144
3305 {
3306 .desc = "ITS: Cavium erratum 23144",
3307 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3308 .mask = 0xffff0fff,
3309 .init = its_enable_quirk_cavium_23144,
3310 },
3311#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06003312#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
3313 {
3314 .desc = "ITS: QDF2400 erratum 0065",
3315 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
3316 .mask = 0xffffffff,
3317 .init = its_enable_quirk_qdf2400_e0065,
3318 },
3319#endif
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003320#ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
3321 {
3322 /*
3323 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
3324 * implementation, but with a 'pre-ITS' added that requires
3325 * special handling in software.
3326 */
3327 .desc = "ITS: Socionext Synquacer pre-ITS",
3328 .iidr = 0x0001143b,
3329 .mask = 0xffffffff,
3330 .init = its_enable_quirk_socionext_synquacer,
3331 },
3332#endif
Marc Zyngier5c9a8822017-07-28 21:20:37 +01003333#ifdef CONFIG_HISILICON_ERRATUM_161600802
3334 {
3335 .desc = "ITS: Hip07 erratum 161600802",
3336 .iidr = 0x00000004,
3337 .mask = 0xffffffff,
3338 .init = its_enable_quirk_hip07_161600802,
3339 },
3340#endif
Robert Richter67510cc2015-09-21 22:58:37 +02003341 {
3342 }
3343};
3344
3345static void its_enable_quirks(struct its_node *its)
3346{
3347 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
3348
3349 gic_enable_quirks(iidr, its_quirks, its);
3350}
3351
Derek Basehoredba0bc72018-02-28 21:48:18 -08003352static int its_save_disable(void)
3353{
3354 struct its_node *its;
3355 int err = 0;
3356
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003357 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003358 list_for_each_entry(its, &its_nodes, entry) {
3359 void __iomem *base;
3360
3361 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3362 continue;
3363
3364 base = its->base;
3365 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
3366 err = its_force_quiescent(base);
3367 if (err) {
3368 pr_err("ITS@%pa: failed to quiesce: %d\n",
3369 &its->phys_base, err);
3370 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3371 goto err;
3372 }
3373
3374 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
3375 }
3376
3377err:
3378 if (err) {
3379 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
3380 void __iomem *base;
3381
3382 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3383 continue;
3384
3385 base = its->base;
3386 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3387 }
3388 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003389 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003390
3391 return err;
3392}
3393
3394static void its_restore_enable(void)
3395{
3396 struct its_node *its;
3397 int ret;
3398
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003399 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003400 list_for_each_entry(its, &its_nodes, entry) {
3401 void __iomem *base;
3402 int i;
3403
3404 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3405 continue;
3406
3407 base = its->base;
3408
3409 /*
3410 * Make sure that the ITS is disabled. If it fails to quiesce,
3411 * don't restore it since writing to CBASER or BASER<n>
3412 * registers is undefined according to the GIC v3 ITS
3413 * Specification.
3414 */
3415 ret = its_force_quiescent(base);
3416 if (ret) {
3417 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
3418 &its->phys_base, ret);
3419 continue;
3420 }
3421
3422 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
3423
3424 /*
3425 * Writing CBASER resets CREADR to 0, so make CWRITER and
3426 * cmd_write line up with it.
3427 */
3428 its->cmd_write = its->cmd_base;
3429 gits_write_cwriter(0, base + GITS_CWRITER);
3430
3431 /* Restore GITS_BASER from the value cache. */
3432 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
3433 struct its_baser *baser = &its->tables[i];
3434
3435 if (!(baser->val & GITS_BASER_VALID))
3436 continue;
3437
3438 its_write_baser(its, baser, baser->val);
3439 }
3440 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
Derek Basehore920181c2018-02-28 21:48:20 -08003441
3442 /*
3443 * Reinit the collection if it's stored in the ITS. This is
3444 * indicated by the col_id being less than the HCC field.
3445 * CID < HCC as specified in the GIC v3 Documentation.
3446 */
3447 if (its->collections[smp_processor_id()].col_id <
3448 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
3449 its_cpu_init_collection(its);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003450 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003451 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003452}
3453
3454static struct syscore_ops its_syscore_ops = {
3455 .suspend = its_save_disable,
3456 .resume = its_restore_enable,
3457};
3458
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003459static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003460{
3461 struct irq_domain *inner_domain;
3462 struct msi_domain_info *info;
3463
3464 info = kzalloc(sizeof(*info), GFP_KERNEL);
3465 if (!info)
3466 return -ENOMEM;
3467
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003468 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003469 if (!inner_domain) {
3470 kfree(info);
3471 return -ENOMEM;
3472 }
3473
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003474 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01003475 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003476 inner_domain->flags |= its->msi_domain_flags;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003477 info->ops = &its_msi_domain_ops;
3478 info->data = its;
3479 inner_domain->host_data = info;
3480
3481 return 0;
3482}
3483
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003484static int its_init_vpe_domain(void)
3485{
Marc Zyngier20b3d542016-12-20 15:23:22 +00003486 struct its_node *its;
3487 u32 devid;
3488 int entries;
3489
3490 if (gic_rdists->has_direct_lpi) {
3491 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
3492 return 0;
3493 }
3494
3495 /* Any ITS will do, even if not v4 */
3496 its = list_first_entry(&its_nodes, struct its_node, entry);
3497
3498 entries = roundup_pow_of_two(nr_cpu_ids);
Kees Cook6396bb22018-06-12 14:03:40 -07003499 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
Marc Zyngier20b3d542016-12-20 15:23:22 +00003500 GFP_KERNEL);
3501 if (!vpe_proxy.vpes) {
3502 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
3503 return -ENOMEM;
3504 }
3505
3506 /* Use the last possible DevID */
3507 devid = GENMASK(its->device_ids - 1, 0);
3508 vpe_proxy.dev = its_create_device(its, devid, entries, false);
3509 if (!vpe_proxy.dev) {
3510 kfree(vpe_proxy.vpes);
3511 pr_err("ITS: Can't allocate GICv4 proxy device\n");
3512 return -ENOMEM;
3513 }
3514
Shanker Donthinenic427a472017-09-23 13:50:19 -05003515 BUG_ON(entries > vpe_proxy.dev->nr_ites);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003516
3517 raw_spin_lock_init(&vpe_proxy.lock);
3518 vpe_proxy.next_victim = 0;
3519 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
3520 devid, vpe_proxy.dev->nr_ites);
3521
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003522 return 0;
3523}
3524
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003525static int __init its_compute_its_list_map(struct resource *res,
3526 void __iomem *its_base)
3527{
3528 int its_number;
3529 u32 ctlr;
3530
3531 /*
3532 * This is assumed to be done early enough that we're
3533 * guaranteed to be single-threaded, hence no
3534 * locking. Should this change, we should address
3535 * this.
3536 */
Marc Zyngierab604912017-10-08 18:48:06 +01003537 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
3538 if (its_number >= GICv4_ITS_LIST_MAX) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003539 pr_err("ITS@%pa: No ITSList entry available!\n",
3540 &res->start);
3541 return -EINVAL;
3542 }
3543
3544 ctlr = readl_relaxed(its_base + GITS_CTLR);
3545 ctlr &= ~GITS_CTLR_ITS_NUMBER;
3546 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
3547 writel_relaxed(ctlr, its_base + GITS_CTLR);
3548 ctlr = readl_relaxed(its_base + GITS_CTLR);
3549 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
3550 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
3551 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
3552 }
3553
3554 if (test_and_set_bit(its_number, &its_list_map)) {
3555 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
3556 &res->start, its_number);
3557 return -EINVAL;
3558 }
3559
3560 return its_number;
3561}
3562
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003563static int __init its_probe_one(struct resource *res,
3564 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003565{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003566 struct its_node *its;
3567 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003568 u32 val, ctlr;
3569 u64 baser, tmp, typer;
Shanker Donthineni539d3782019-01-14 09:50:19 +00003570 struct page *page;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003571 int err;
3572
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003573 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003574 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003575 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003576 return -ENOMEM;
3577 }
3578
3579 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
3580 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003581 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003582 err = -ENODEV;
3583 goto out_unmap;
3584 }
3585
Yun Wu4559fbb2015-03-06 16:37:50 +00003586 err = its_force_quiescent(its_base);
3587 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003588 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00003589 goto out_unmap;
3590 }
3591
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003592 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003593
3594 its = kzalloc(sizeof(*its), GFP_KERNEL);
3595 if (!its) {
3596 err = -ENOMEM;
3597 goto out_unmap;
3598 }
3599
3600 raw_spin_lock_init(&its->lock);
Marc Zyngier9791ec72019-01-29 10:02:33 +00003601 mutex_init(&its->dev_alloc_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003602 INIT_LIST_HEAD(&its->entry);
3603 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003604 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003605 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003606 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003607 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
Ard Biesheuvelfa150012017-10-17 17:55:54 +01003608 its->device_ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003609 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
3610 if (its->is_v4) {
3611 if (!(typer & GITS_TYPER_VMOVP)) {
3612 err = its_compute_its_list_map(res, its_base);
3613 if (err < 0)
3614 goto out_free_its;
3615
Marc Zyngierdebf6d02017-10-08 18:44:42 +01003616 its->list_nr = err;
3617
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003618 pr_info("ITS@%pa: Using ITS number %d\n",
3619 &res->start, err);
3620 } else {
3621 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
3622 }
3623 }
3624
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003625 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003626
Shanker Donthineni539d3782019-01-14 09:50:19 +00003627 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
3628 get_order(ITS_CMD_QUEUE_SZ));
3629 if (!page) {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003630 err = -ENOMEM;
3631 goto out_free_its;
3632 }
Shanker Donthineni539d3782019-01-14 09:50:19 +00003633 its->cmd_base = (void *)page_address(page);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003634 its->cmd_write = its->cmd_base;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003635 its->fwnode_handle = handle;
3636 its->get_msi_base = its_irq_get_msi_base;
3637 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003638
Robert Richter67510cc2015-09-21 22:58:37 +02003639 its_enable_quirks(its);
3640
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05003641 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003642 if (err)
3643 goto out_free_cmd;
3644
3645 err = its_alloc_collections(its);
3646 if (err)
3647 goto out_free_tables;
3648
3649 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06003650 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003651 GITS_CBASER_InnerShareable |
3652 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
3653 GITS_CBASER_VALID);
3654
Vladimir Murzin0968a612016-11-02 11:54:06 +00003655 gits_write_cbaser(baser, its->base + GITS_CBASER);
3656 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003657
Marc Zyngier4ad3e362015-03-27 14:15:04 +00003658 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00003659 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
3660 /*
3661 * The HW reports non-shareable, we must
3662 * remove the cacheability attributes as
3663 * well.
3664 */
3665 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
3666 GITS_CBASER_CACHEABILITY_MASK);
3667 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00003668 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00003669 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003670 pr_info("ITS: using cache flushing for cmd queue\n");
3671 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
3672 }
3673
Vladimir Murzin0968a612016-11-02 11:54:06 +00003674 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003675 ctlr = readl_relaxed(its->base + GITS_CTLR);
Marc Zyngierd51c4b42017-06-27 21:24:25 +01003676 ctlr |= GITS_CTLR_ENABLE;
3677 if (its->is_v4)
3678 ctlr |= GITS_CTLR_ImDe;
3679 writel_relaxed(ctlr, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00003680
Derek Basehoredba0bc72018-02-28 21:48:18 -08003681 if (GITS_TYPER_HCC(typer))
3682 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
3683
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003684 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003685 if (err)
3686 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003687
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003688 raw_spin_lock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003689 list_add(&its->entry, &its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003690 raw_spin_unlock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003691
3692 return 0;
3693
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003694out_free_tables:
3695 its_free_tables(its);
3696out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01003697 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003698out_free_its:
3699 kfree(its);
3700out_unmap:
3701 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003702 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003703 return err;
3704}
3705
3706static bool gic_rdists_supports_plpis(void)
3707{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01003708 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003709}
3710
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003711static int redist_disable_lpis(void)
3712{
3713 void __iomem *rbase = gic_data_rdist_rd_base();
3714 u64 timeout = USEC_PER_SEC;
3715 u64 val;
3716
3717 if (!gic_rdists_supports_plpis()) {
3718 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
3719 return -ENXIO;
3720 }
3721
3722 val = readl_relaxed(rbase + GICR_CTLR);
3723 if (!(val & GICR_CTLR_ENABLE_LPIS))
3724 return 0;
3725
Marc Zyngier11e37d32018-07-27 13:38:54 +01003726 /*
3727 * If coming via a CPU hotplug event, we don't need to disable
3728 * LPIs before trying to re-enable them. They are already
3729 * configured and all is well in the world.
Marc Zyngierc440a9d2018-07-27 15:40:13 +01003730 *
3731 * If running with preallocated tables, there is nothing to do.
Marc Zyngier11e37d32018-07-27 13:38:54 +01003732 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01003733 if (gic_data_rdist()->lpi_enabled ||
3734 (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED))
Marc Zyngier11e37d32018-07-27 13:38:54 +01003735 return 0;
3736
3737 /*
3738 * From that point on, we only try to do some damage control.
3739 */
3740 pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003741 smp_processor_id());
3742 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
3743
3744 /* Disable LPIs */
3745 val &= ~GICR_CTLR_ENABLE_LPIS;
3746 writel_relaxed(val, rbase + GICR_CTLR);
3747
3748 /* Make sure any change to GICR_CTLR is observable by the GIC */
3749 dsb(sy);
3750
3751 /*
3752 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
3753 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
3754 * Error out if we time out waiting for RWP to clear.
3755 */
3756 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
3757 if (!timeout) {
3758 pr_err("CPU%d: Timeout while disabling LPIs\n",
3759 smp_processor_id());
3760 return -ETIMEDOUT;
3761 }
3762 udelay(1);
3763 timeout--;
3764 }
3765
3766 /*
3767 * After it has been written to 1, it is IMPLEMENTATION
3768 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
3769 * cleared to 0. Error out if clearing the bit failed.
3770 */
3771 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
3772 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
3773 return -EBUSY;
3774 }
3775
3776 return 0;
3777}
3778
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003779int its_cpu_init(void)
3780{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003781 if (!list_empty(&its_nodes)) {
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003782 int ret;
3783
3784 ret = redist_disable_lpis();
3785 if (ret)
3786 return ret;
3787
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003788 its_cpu_init_lpis();
Derek Basehore920181c2018-02-28 21:48:20 -08003789 its_cpu_init_collections();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003790 }
3791
3792 return 0;
3793}
3794
Arvind Yadav935bba72017-06-22 16:05:30 +05303795static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003796 { .compatible = "arm,gic-v3-its", },
3797 {},
3798};
3799
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003800static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003801{
3802 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003803 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003804
3805 for (np = of_find_matching_node(node, its_device_id); np;
3806 np = of_find_matching_node(np, its_device_id)) {
Stephen Boyd95a25622018-02-01 09:03:29 -08003807 if (!of_device_is_available(np))
3808 continue;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003809 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003810 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
3811 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003812 continue;
3813 }
3814
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003815 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003816 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003817 continue;
3818 }
3819
3820 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003821 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003822 return 0;
3823}
3824
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003825#ifdef CONFIG_ACPI
3826
3827#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
3828
Robert Richterd1ce2632017-07-12 15:25:09 +02003829#ifdef CONFIG_ACPI_NUMA
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303830struct its_srat_map {
3831 /* numa node id */
3832 u32 numa_node;
3833 /* GIC ITS ID */
3834 u32 its_id;
3835};
3836
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003837static struct its_srat_map *its_srat_maps __initdata;
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303838static int its_in_srat __initdata;
3839
3840static int __init acpi_get_its_numa_node(u32 its_id)
3841{
3842 int i;
3843
3844 for (i = 0; i < its_in_srat; i++) {
3845 if (its_id == its_srat_maps[i].its_id)
3846 return its_srat_maps[i].numa_node;
3847 }
3848 return NUMA_NO_NODE;
3849}
3850
Keith Busch60574d12019-03-11 14:55:57 -06003851static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header,
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003852 const unsigned long end)
3853{
3854 return 0;
3855}
3856
Keith Busch60574d12019-03-11 14:55:57 -06003857static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header,
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303858 const unsigned long end)
3859{
3860 int node;
3861 struct acpi_srat_gic_its_affinity *its_affinity;
3862
3863 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
3864 if (!its_affinity)
3865 return -EINVAL;
3866
3867 if (its_affinity->header.length < sizeof(*its_affinity)) {
3868 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
3869 its_affinity->header.length);
3870 return -EINVAL;
3871 }
3872
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303873 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
3874
3875 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
3876 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
3877 return 0;
3878 }
3879
3880 its_srat_maps[its_in_srat].numa_node = node;
3881 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
3882 its_in_srat++;
3883 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
3884 its_affinity->proximity_domain, its_affinity->its_id, node);
3885
3886 return 0;
3887}
3888
3889static void __init acpi_table_parse_srat_its(void)
3890{
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003891 int count;
3892
3893 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
3894 sizeof(struct acpi_table_srat),
3895 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3896 gic_acpi_match_srat_its, 0);
3897 if (count <= 0)
3898 return;
3899
Kees Cook6da2ec52018-06-12 13:55:00 -07003900 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
3901 GFP_KERNEL);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003902 if (!its_srat_maps) {
3903 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
3904 return;
3905 }
3906
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303907 acpi_table_parse_entries(ACPI_SIG_SRAT,
3908 sizeof(struct acpi_table_srat),
3909 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3910 gic_acpi_parse_srat_its, 0);
3911}
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003912
3913/* free the its_srat_maps after ITS probing */
3914static void __init acpi_its_srat_maps_free(void)
3915{
3916 kfree(its_srat_maps);
3917}
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303918#else
3919static void __init acpi_table_parse_srat_its(void) { }
3920static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003921static void __init acpi_its_srat_maps_free(void) { }
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303922#endif
3923
Keith Busch60574d12019-03-11 14:55:57 -06003924static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header,
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003925 const unsigned long end)
3926{
3927 struct acpi_madt_generic_translator *its_entry;
3928 struct fwnode_handle *dom_handle;
3929 struct resource res;
3930 int err;
3931
3932 its_entry = (struct acpi_madt_generic_translator *)header;
3933 memset(&res, 0, sizeof(res));
3934 res.start = its_entry->base_address;
3935 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
3936 res.flags = IORESOURCE_MEM;
3937
Marc Zyngier5778cc72019-07-31 16:13:42 +01003938 dom_handle = irq_domain_alloc_fwnode(&res.start);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003939 if (!dom_handle) {
3940 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
3941 &res.start);
3942 return -ENOMEM;
3943 }
3944
Shameer Kolothum8b4282e2018-02-13 15:20:50 +00003945 err = iort_register_domain_token(its_entry->translation_id, res.start,
3946 dom_handle);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003947 if (err) {
3948 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
3949 &res.start, its_entry->translation_id);
3950 goto dom_err;
3951 }
3952
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303953 err = its_probe_one(&res, dom_handle,
3954 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003955 if (!err)
3956 return 0;
3957
3958 iort_deregister_domain_token(its_entry->translation_id);
3959dom_err:
3960 irq_domain_free_fwnode(dom_handle);
3961 return err;
3962}
3963
3964static void __init its_acpi_probe(void)
3965{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303966 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003967 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
3968 gic_acpi_parse_madt_its, 0);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003969 acpi_its_srat_maps_free();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003970}
3971#else
3972static void __init its_acpi_probe(void) { }
3973#endif
3974
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003975int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
3976 struct irq_domain *parent_domain)
3977{
3978 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003979 struct its_node *its;
3980 bool has_v4 = false;
3981 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003982
3983 its_parent = parent_domain;
3984 of_node = to_of_node(handle);
3985 if (of_node)
3986 its_of_probe(of_node);
3987 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003988 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003989
3990 if (list_empty(&its_nodes)) {
3991 pr_warn("ITS: No ITS available, not enabling LPIs\n");
3992 return -ENXIO;
3993 }
3994
3995 gic_rdists = rdists;
Marc Zyngier11e37d32018-07-27 13:38:54 +01003996
3997 err = allocate_lpi_tables();
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003998 if (err)
3999 return err;
4000
4001 list_for_each_entry(its, &its_nodes, entry)
4002 has_v4 |= its->is_v4;
4003
4004 if (has_v4 & rdists->has_vlpis) {
Marc Zyngier3d63cb52016-12-20 15:31:54 +00004005 if (its_init_vpe_domain() ||
4006 its_init_v4(parent_domain, &its_vpe_domain_ops)) {
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004007 rdists->has_vlpis = false;
4008 pr_err("ITS: Disabling GICv4 support\n");
4009 }
4010 }
4011
Derek Basehoredba0bc72018-02-28 21:48:18 -08004012 register_syscore_ops(&its_syscore_ops);
4013
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004014 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004015}