blob: 021e0c70e87c02feb00f74e4e3ae9b6d7b595824 [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 {
Ben Dooks (Codethink)2bbdfcc2019-10-17 12:29:55 +0100308 union {
309 u64 raw_cmd[4];
310 __le64 raw_cmd_le[4];
311 };
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000312};
313
314#define ITS_CMD_QUEUE_SZ SZ_64K
315#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
316
Marc Zyngier67047f902017-07-28 21:16:58 +0100317typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
318 struct its_cmd_block *,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000319 struct its_cmd_desc *);
320
Marc Zyngier67047f902017-07-28 21:16:58 +0100321typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
322 struct its_cmd_block *,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000323 struct its_cmd_desc *);
324
Marc Zyngier4d36f132016-12-19 17:11:52 +0000325static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
326{
327 u64 mask = GENMASK_ULL(h, l);
328 *raw_cmd &= ~mask;
329 *raw_cmd |= (val << l) & mask;
330}
331
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000332static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
333{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000334 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000335}
336
337static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
338{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000339 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000340}
341
342static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
343{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000344 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000345}
346
347static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
348{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000349 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000350}
351
352static void its_encode_size(struct its_cmd_block *cmd, u8 size)
353{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000354 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000355}
356
357static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
358{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500359 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000360}
361
362static void its_encode_valid(struct its_cmd_block *cmd, int valid)
363{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000364 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000365}
366
367static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
368{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500369 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000370}
371
372static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
373{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000374 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000375}
376
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000377static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
378{
379 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
380}
381
382static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
383{
384 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
385}
386
387static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
388{
389 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
390}
391
392static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
393{
394 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
395}
396
Marc Zyngier3171a472016-12-20 15:17:28 +0000397static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
398{
399 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
400}
401
402static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
403{
404 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
405}
406
Marc Zyngiereb781922016-12-20 14:47:05 +0000407static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
408{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500409 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
Marc Zyngiereb781922016-12-20 14:47:05 +0000410}
411
412static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
413{
414 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
415}
416
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000417static inline void its_fixup_cmd(struct its_cmd_block *cmd)
418{
419 /* Let's fixup BE commands */
Ben Dooks (Codethink)2bbdfcc2019-10-17 12:29:55 +0100420 cmd->raw_cmd_le[0] = cpu_to_le64(cmd->raw_cmd[0]);
421 cmd->raw_cmd_le[1] = cpu_to_le64(cmd->raw_cmd[1]);
422 cmd->raw_cmd_le[2] = cpu_to_le64(cmd->raw_cmd[2]);
423 cmd->raw_cmd_le[3] = cpu_to_le64(cmd->raw_cmd[3]);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000424}
425
Marc Zyngier67047f902017-07-28 21:16:58 +0100426static struct its_collection *its_build_mapd_cmd(struct its_node *its,
427 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000428 struct its_cmd_desc *desc)
429{
430 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000431 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000432
433 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
434 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
435
436 its_encode_cmd(cmd, GITS_CMD_MAPD);
437 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
438 its_encode_size(cmd, size - 1);
439 its_encode_itt(cmd, itt_addr);
440 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
441
442 its_fixup_cmd(cmd);
443
Marc Zyngier591e5be2015-07-17 10:46:42 +0100444 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000445}
446
Marc Zyngier67047f902017-07-28 21:16:58 +0100447static struct its_collection *its_build_mapc_cmd(struct its_node *its,
448 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000449 struct its_cmd_desc *desc)
450{
451 its_encode_cmd(cmd, GITS_CMD_MAPC);
452 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
453 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
454 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
455
456 its_fixup_cmd(cmd);
457
458 return desc->its_mapc_cmd.col;
459}
460
Marc Zyngier67047f902017-07-28 21:16:58 +0100461static struct its_collection *its_build_mapti_cmd(struct its_node *its,
462 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000463 struct its_cmd_desc *desc)
464{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100465 struct its_collection *col;
466
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000467 col = dev_event_to_col(desc->its_mapti_cmd.dev,
468 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100469
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000470 its_encode_cmd(cmd, GITS_CMD_MAPTI);
471 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
472 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
473 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100474 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000475
476 its_fixup_cmd(cmd);
477
Marc Zyngier83559b42018-06-22 10:52:52 +0100478 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000479}
480
Marc Zyngier67047f902017-07-28 21:16:58 +0100481static struct its_collection *its_build_movi_cmd(struct its_node *its,
482 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000483 struct its_cmd_desc *desc)
484{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100485 struct its_collection *col;
486
487 col = dev_event_to_col(desc->its_movi_cmd.dev,
488 desc->its_movi_cmd.event_id);
489
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000490 its_encode_cmd(cmd, GITS_CMD_MOVI);
491 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100492 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000493 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
494
495 its_fixup_cmd(cmd);
496
Marc Zyngier83559b42018-06-22 10:52:52 +0100497 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000498}
499
Marc Zyngier67047f902017-07-28 21:16:58 +0100500static struct its_collection *its_build_discard_cmd(struct its_node *its,
501 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000502 struct its_cmd_desc *desc)
503{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100504 struct its_collection *col;
505
506 col = dev_event_to_col(desc->its_discard_cmd.dev,
507 desc->its_discard_cmd.event_id);
508
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000509 its_encode_cmd(cmd, GITS_CMD_DISCARD);
510 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
511 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
512
513 its_fixup_cmd(cmd);
514
Marc Zyngier83559b42018-06-22 10:52:52 +0100515 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000516}
517
Marc Zyngier67047f902017-07-28 21:16:58 +0100518static struct its_collection *its_build_inv_cmd(struct its_node *its,
519 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000520 struct its_cmd_desc *desc)
521{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100522 struct its_collection *col;
523
524 col = dev_event_to_col(desc->its_inv_cmd.dev,
525 desc->its_inv_cmd.event_id);
526
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000527 its_encode_cmd(cmd, GITS_CMD_INV);
528 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
529 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
530
531 its_fixup_cmd(cmd);
532
Marc Zyngier83559b42018-06-22 10:52:52 +0100533 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000534}
535
Marc Zyngier67047f902017-07-28 21:16:58 +0100536static struct its_collection *its_build_int_cmd(struct its_node *its,
537 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000538 struct its_cmd_desc *desc)
539{
540 struct its_collection *col;
541
542 col = dev_event_to_col(desc->its_int_cmd.dev,
543 desc->its_int_cmd.event_id);
544
545 its_encode_cmd(cmd, GITS_CMD_INT);
546 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
547 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
548
549 its_fixup_cmd(cmd);
550
Marc Zyngier83559b42018-06-22 10:52:52 +0100551 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000552}
553
Marc Zyngier67047f902017-07-28 21:16:58 +0100554static struct its_collection *its_build_clear_cmd(struct its_node *its,
555 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000556 struct its_cmd_desc *desc)
557{
558 struct its_collection *col;
559
560 col = dev_event_to_col(desc->its_clear_cmd.dev,
561 desc->its_clear_cmd.event_id);
562
563 its_encode_cmd(cmd, GITS_CMD_CLEAR);
564 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
565 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
566
567 its_fixup_cmd(cmd);
568
Marc Zyngier83559b42018-06-22 10:52:52 +0100569 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000570}
571
Marc Zyngier67047f902017-07-28 21:16:58 +0100572static struct its_collection *its_build_invall_cmd(struct its_node *its,
573 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000574 struct its_cmd_desc *desc)
575{
576 its_encode_cmd(cmd, GITS_CMD_INVALL);
577 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
578
579 its_fixup_cmd(cmd);
580
581 return NULL;
582}
583
Marc Zyngier67047f902017-07-28 21:16:58 +0100584static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
585 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000586 struct its_cmd_desc *desc)
587{
588 its_encode_cmd(cmd, GITS_CMD_VINVALL);
589 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
590
591 its_fixup_cmd(cmd);
592
Marc Zyngier205e0652018-06-22 10:52:53 +0100593 return valid_vpe(its, desc->its_vinvall_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000594}
595
Marc Zyngier67047f902017-07-28 21:16:58 +0100596static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
597 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000598 struct its_cmd_desc *desc)
599{
600 unsigned long vpt_addr;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100601 u64 target;
Marc Zyngiereb781922016-12-20 14:47:05 +0000602
603 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100604 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngiereb781922016-12-20 14:47:05 +0000605
606 its_encode_cmd(cmd, GITS_CMD_VMAPP);
607 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
608 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100609 its_encode_target(cmd, target);
Marc Zyngiereb781922016-12-20 14:47:05 +0000610 its_encode_vpt_addr(cmd, vpt_addr);
611 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
612
613 its_fixup_cmd(cmd);
614
Marc Zyngier205e0652018-06-22 10:52:53 +0100615 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000616}
617
Marc Zyngier67047f902017-07-28 21:16:58 +0100618static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
619 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000620 struct its_cmd_desc *desc)
621{
622 u32 db;
623
624 if (desc->its_vmapti_cmd.db_enabled)
625 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
626 else
627 db = 1023;
628
629 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
630 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
631 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
632 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
633 its_encode_db_phys_id(cmd, db);
634 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
635
636 its_fixup_cmd(cmd);
637
Marc Zyngier205e0652018-06-22 10:52:53 +0100638 return valid_vpe(its, desc->its_vmapti_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000639}
640
Marc Zyngier67047f902017-07-28 21:16:58 +0100641static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
642 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000643 struct its_cmd_desc *desc)
644{
645 u32 db;
646
647 if (desc->its_vmovi_cmd.db_enabled)
648 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
649 else
650 db = 1023;
651
652 its_encode_cmd(cmd, GITS_CMD_VMOVI);
653 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
654 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
655 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
656 its_encode_db_phys_id(cmd, db);
657 its_encode_db_valid(cmd, true);
658
659 its_fixup_cmd(cmd);
660
Marc Zyngier205e0652018-06-22 10:52:53 +0100661 return valid_vpe(its, desc->its_vmovi_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000662}
663
Marc Zyngier67047f902017-07-28 21:16:58 +0100664static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
665 struct its_cmd_block *cmd,
Marc Zyngier3171a472016-12-20 15:17:28 +0000666 struct its_cmd_desc *desc)
667{
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100668 u64 target;
669
670 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngier3171a472016-12-20 15:17:28 +0000671 its_encode_cmd(cmd, GITS_CMD_VMOVP);
672 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
673 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
674 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100675 its_encode_target(cmd, target);
Marc Zyngier3171a472016-12-20 15:17:28 +0000676
677 its_fixup_cmd(cmd);
678
Marc Zyngier205e0652018-06-22 10:52:53 +0100679 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
Marc Zyngier3171a472016-12-20 15:17:28 +0000680}
681
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000682static u64 its_cmd_ptr_to_offset(struct its_node *its,
683 struct its_cmd_block *ptr)
684{
685 return (ptr - its->cmd_base) * sizeof(*ptr);
686}
687
688static int its_queue_full(struct its_node *its)
689{
690 int widx;
691 int ridx;
692
693 widx = its->cmd_write - its->cmd_base;
694 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
695
696 /* This is incredibly unlikely to happen, unless the ITS locks up. */
697 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
698 return 1;
699
700 return 0;
701}
702
703static struct its_cmd_block *its_allocate_entry(struct its_node *its)
704{
705 struct its_cmd_block *cmd;
706 u32 count = 1000000; /* 1s! */
707
708 while (its_queue_full(its)) {
709 count--;
710 if (!count) {
711 pr_err_ratelimited("ITS queue not draining\n");
712 return NULL;
713 }
714 cpu_relax();
715 udelay(1);
716 }
717
718 cmd = its->cmd_write++;
719
720 /* Handle queue wrapping */
721 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
722 its->cmd_write = its->cmd_base;
723
Marc Zyngier34d677a2016-12-19 17:16:45 +0000724 /* Clear command */
725 cmd->raw_cmd[0] = 0;
726 cmd->raw_cmd[1] = 0;
727 cmd->raw_cmd[2] = 0;
728 cmd->raw_cmd[3] = 0;
729
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000730 return cmd;
731}
732
733static struct its_cmd_block *its_post_commands(struct its_node *its)
734{
735 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
736
737 writel_relaxed(wr, its->base + GITS_CWRITER);
738
739 return its->cmd_write;
740}
741
742static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
743{
744 /*
745 * Make sure the commands written to memory are observable by
746 * the ITS.
747 */
748 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000749 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000750 else
751 dsb(ishst);
752}
753
Marc Zyngiera19b4622017-08-04 17:45:50 +0100754static int its_wait_for_range_completion(struct its_node *its,
Heyi Guoa050fa52019-05-13 19:42:06 +0800755 u64 prev_idx,
Marc Zyngiera19b4622017-08-04 17:45:50 +0100756 struct its_cmd_block *to)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000757{
Heyi Guoa050fa52019-05-13 19:42:06 +0800758 u64 rd_idx, to_idx, linear_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000759 u32 count = 1000000; /* 1s! */
760
Heyi Guoa050fa52019-05-13 19:42:06 +0800761 /* Linearize to_idx if the command set has wrapped around */
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000762 to_idx = its_cmd_ptr_to_offset(its, to);
Heyi Guoa050fa52019-05-13 19:42:06 +0800763 if (to_idx < prev_idx)
764 to_idx += ITS_CMD_QUEUE_SZ;
765
766 linear_idx = prev_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000767
768 while (1) {
Heyi Guoa050fa52019-05-13 19:42:06 +0800769 s64 delta;
770
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000771 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100772
Heyi Guoa050fa52019-05-13 19:42:06 +0800773 /*
774 * Compute the read pointer progress, taking the
775 * potential wrap-around into account.
776 */
777 delta = rd_idx - prev_idx;
778 if (rd_idx < prev_idx)
779 delta += ITS_CMD_QUEUE_SZ;
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100780
Heyi Guoa050fa52019-05-13 19:42:06 +0800781 linear_idx += delta;
782 if (linear_idx >= to_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000783 break;
784
785 count--;
786 if (!count) {
Heyi Guoa050fa52019-05-13 19:42:06 +0800787 pr_err_ratelimited("ITS queue timeout (%llu %llu)\n",
788 to_idx, linear_idx);
Marc Zyngiera19b4622017-08-04 17:45:50 +0100789 return -1;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000790 }
Heyi Guoa050fa52019-05-13 19:42:06 +0800791 prev_idx = rd_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000792 cpu_relax();
793 udelay(1);
794 }
Marc Zyngiera19b4622017-08-04 17:45:50 +0100795
796 return 0;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000797}
798
Marc Zyngiere4f90942016-12-19 17:56:32 +0000799/* Warning, macro hell follows */
800#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
801void name(struct its_node *its, \
802 buildtype builder, \
803 struct its_cmd_desc *desc) \
804{ \
805 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
806 synctype *sync_obj; \
807 unsigned long flags; \
Heyi Guoa050fa52019-05-13 19:42:06 +0800808 u64 rd_idx; \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000809 \
810 raw_spin_lock_irqsave(&its->lock, flags); \
811 \
812 cmd = its_allocate_entry(its); \
813 if (!cmd) { /* We're soooooo screewed... */ \
814 raw_spin_unlock_irqrestore(&its->lock, flags); \
815 return; \
816 } \
Marc Zyngier67047f902017-07-28 21:16:58 +0100817 sync_obj = builder(its, cmd, desc); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000818 its_flush_cmd(its, cmd); \
819 \
820 if (sync_obj) { \
821 sync_cmd = its_allocate_entry(its); \
822 if (!sync_cmd) \
823 goto post; \
824 \
Marc Zyngier67047f902017-07-28 21:16:58 +0100825 buildfn(its, sync_cmd, sync_obj); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000826 its_flush_cmd(its, sync_cmd); \
827 } \
828 \
829post: \
Heyi Guoa050fa52019-05-13 19:42:06 +0800830 rd_idx = readl_relaxed(its->base + GITS_CREADR); \
Marc Zyngiere4f90942016-12-19 17:56:32 +0000831 next_cmd = its_post_commands(its); \
832 raw_spin_unlock_irqrestore(&its->lock, flags); \
833 \
Heyi Guoa050fa52019-05-13 19:42:06 +0800834 if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \
Marc Zyngiera19b4622017-08-04 17:45:50 +0100835 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000836}
837
Marc Zyngier67047f902017-07-28 21:16:58 +0100838static void its_build_sync_cmd(struct its_node *its,
839 struct its_cmd_block *sync_cmd,
Marc Zyngiere4f90942016-12-19 17:56:32 +0000840 struct its_collection *sync_col)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000841{
Marc Zyngiere4f90942016-12-19 17:56:32 +0000842 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
843 its_encode_target(sync_cmd, sync_col->target_address);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000844
Marc Zyngiere4f90942016-12-19 17:56:32 +0000845 its_fixup_cmd(sync_cmd);
846}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000847
Marc Zyngiere4f90942016-12-19 17:56:32 +0000848static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
849 struct its_collection, its_build_sync_cmd)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000850
Marc Zyngier67047f902017-07-28 21:16:58 +0100851static void its_build_vsync_cmd(struct its_node *its,
852 struct its_cmd_block *sync_cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000853 struct its_vpe *sync_vpe)
854{
855 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
856 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000857
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000858 its_fixup_cmd(sync_cmd);
859}
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000860
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000861static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
862 struct its_vpe, its_build_vsync_cmd)
863
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000864static void its_send_int(struct its_device *dev, u32 event_id)
865{
866 struct its_cmd_desc desc;
867
868 desc.its_int_cmd.dev = dev;
869 desc.its_int_cmd.event_id = event_id;
870
871 its_send_single_command(dev->its, its_build_int_cmd, &desc);
872}
873
874static void its_send_clear(struct its_device *dev, u32 event_id)
875{
876 struct its_cmd_desc desc;
877
878 desc.its_clear_cmd.dev = dev;
879 desc.its_clear_cmd.event_id = event_id;
880
881 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000882}
883
884static void its_send_inv(struct its_device *dev, u32 event_id)
885{
886 struct its_cmd_desc desc;
887
888 desc.its_inv_cmd.dev = dev;
889 desc.its_inv_cmd.event_id = event_id;
890
891 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
892}
893
894static void its_send_mapd(struct its_device *dev, int valid)
895{
896 struct its_cmd_desc desc;
897
898 desc.its_mapd_cmd.dev = dev;
899 desc.its_mapd_cmd.valid = !!valid;
900
901 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
902}
903
904static void its_send_mapc(struct its_node *its, struct its_collection *col,
905 int valid)
906{
907 struct its_cmd_desc desc;
908
909 desc.its_mapc_cmd.col = col;
910 desc.its_mapc_cmd.valid = !!valid;
911
912 its_send_single_command(its, its_build_mapc_cmd, &desc);
913}
914
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000915static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000916{
917 struct its_cmd_desc desc;
918
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000919 desc.its_mapti_cmd.dev = dev;
920 desc.its_mapti_cmd.phys_id = irq_id;
921 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000922
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000923 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000924}
925
926static void its_send_movi(struct its_device *dev,
927 struct its_collection *col, u32 id)
928{
929 struct its_cmd_desc desc;
930
931 desc.its_movi_cmd.dev = dev;
932 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100933 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000934
935 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
936}
937
938static void its_send_discard(struct its_device *dev, u32 id)
939{
940 struct its_cmd_desc desc;
941
942 desc.its_discard_cmd.dev = dev;
943 desc.its_discard_cmd.event_id = id;
944
945 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
946}
947
948static void its_send_invall(struct its_node *its, struct its_collection *col)
949{
950 struct its_cmd_desc desc;
951
952 desc.its_invall_cmd.col = col;
953
954 its_send_single_command(its, its_build_invall_cmd, &desc);
955}
Marc Zyngierc48ed512014-11-24 14:35:12 +0000956
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000957static void its_send_vmapti(struct its_device *dev, u32 id)
958{
959 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
960 struct its_cmd_desc desc;
961
962 desc.its_vmapti_cmd.vpe = map->vpe;
963 desc.its_vmapti_cmd.dev = dev;
964 desc.its_vmapti_cmd.virt_id = map->vintid;
965 desc.its_vmapti_cmd.event_id = id;
966 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
967
968 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
969}
970
971static void its_send_vmovi(struct its_device *dev, u32 id)
972{
973 struct its_vlpi_map *map = &dev->event_map.vlpi_maps[id];
974 struct its_cmd_desc desc;
975
976 desc.its_vmovi_cmd.vpe = map->vpe;
977 desc.its_vmovi_cmd.dev = dev;
978 desc.its_vmovi_cmd.event_id = id;
979 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
980
981 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
982}
983
Marc Zyngier75fd9512017-10-08 18:46:39 +0100984static void its_send_vmapp(struct its_node *its,
985 struct its_vpe *vpe, bool valid)
Marc Zyngiereb781922016-12-20 14:47:05 +0000986{
987 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +0000988
989 desc.its_vmapp_cmd.vpe = vpe;
990 desc.its_vmapp_cmd.valid = valid;
Marc Zyngier75fd9512017-10-08 18:46:39 +0100991 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
Marc Zyngiereb781922016-12-20 14:47:05 +0000992
Marc Zyngier75fd9512017-10-08 18:46:39 +0100993 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +0000994}
995
Marc Zyngier3171a472016-12-20 15:17:28 +0000996static void its_send_vmovp(struct its_vpe *vpe)
997{
Zenghui Yu84243122019-10-23 03:46:26 +0000998 struct its_cmd_desc desc = {};
Marc Zyngier3171a472016-12-20 15:17:28 +0000999 struct its_node *its;
1000 unsigned long flags;
1001 int col_id = vpe->col_idx;
1002
1003 desc.its_vmovp_cmd.vpe = vpe;
Marc Zyngier3171a472016-12-20 15:17:28 +00001004
1005 if (!its_list_map) {
1006 its = list_first_entry(&its_nodes, struct its_node, entry);
Marc Zyngier3171a472016-12-20 15:17:28 +00001007 desc.its_vmovp_cmd.col = &its->collections[col_id];
1008 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1009 return;
1010 }
1011
1012 /*
1013 * Yet another marvel of the architecture. If using the
1014 * its_list "feature", we need to make sure that all ITSs
1015 * receive all VMOVP commands in the same order. The only way
1016 * to guarantee this is to make vmovp a serialization point.
1017 *
1018 * Wall <-- Head.
1019 */
1020 raw_spin_lock_irqsave(&vmovp_lock, flags);
1021
1022 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
Zenghui Yu84243122019-10-23 03:46:26 +00001023 desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm);
Marc Zyngier3171a472016-12-20 15:17:28 +00001024
1025 /* Emit VMOVPs */
1026 list_for_each_entry(its, &its_nodes, entry) {
1027 if (!its->is_v4)
1028 continue;
1029
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001030 if (!vpe->its_vm->vlpi_count[its->list_nr])
1031 continue;
1032
Marc Zyngier3171a472016-12-20 15:17:28 +00001033 desc.its_vmovp_cmd.col = &its->collections[col_id];
1034 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1035 }
1036
1037 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1038}
1039
Marc Zyngier40619a22017-10-08 15:16:09 +01001040static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
Marc Zyngiereb781922016-12-20 14:47:05 +00001041{
1042 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +00001043
1044 desc.its_vinvall_cmd.vpe = vpe;
Marc Zyngier40619a22017-10-08 15:16:09 +01001045 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +00001046}
1047
Marc Zyngierc48ed512014-11-24 14:35:12 +00001048/*
1049 * irqchip functions - assumes MSI, mostly.
1050 */
1051
1052static inline u32 its_get_event_id(struct irq_data *d)
1053{
1054 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier591e5be2015-07-17 10:46:42 +01001055 return d->hwirq - its_dev->event_map.lpi_base;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001056}
1057
Marc Zyngier015ec032016-12-20 09:54:57 +00001058static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +00001059{
Marc Zyngier015ec032016-12-20 09:54:57 +00001060 irq_hw_number_t hwirq;
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001061 void *va;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001062 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001063
Marc Zyngier015ec032016-12-20 09:54:57 +00001064 if (irqd_is_forwarded_to_vcpu(d)) {
1065 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1066 u32 event = its_get_event_id(d);
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001067 struct its_vlpi_map *map;
Marc Zyngier015ec032016-12-20 09:54:57 +00001068
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001069 va = page_address(its_dev->event_map.vm->vprop_page);
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001070 map = &its_dev->event_map.vlpi_maps[event];
1071 hwirq = map->vintid;
1072
1073 /* Remember the updated property */
1074 map->properties &= ~clr;
1075 map->properties |= set | LPI_PROP_GROUP1;
Marc Zyngier015ec032016-12-20 09:54:57 +00001076 } else {
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001077 va = gic_rdists->prop_table_va;
Marc Zyngier015ec032016-12-20 09:54:57 +00001078 hwirq = d->hwirq;
1079 }
Marc Zyngieradcdb942016-12-19 19:18:13 +00001080
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001081 cfg = va + hwirq - 8192;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001082 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +00001083 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001084
1085 /*
1086 * Make the above write visible to the redistributors.
1087 * And yes, we're flushing exactly: One. Single. Byte.
1088 * Humpf...
1089 */
1090 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001091 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001092 else
1093 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +00001094}
1095
1096static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1097{
1098 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1099
1100 lpi_write_config(d, clr, set);
Marc Zyngieradcdb942016-12-19 19:18:13 +00001101 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001102}
1103
Marc Zyngier015ec032016-12-20 09:54:57 +00001104static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1105{
1106 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1107 u32 event = its_get_event_id(d);
1108
1109 if (its_dev->event_map.vlpi_maps[event].db_enabled == enable)
1110 return;
1111
1112 its_dev->event_map.vlpi_maps[event].db_enabled = enable;
1113
1114 /*
1115 * More fun with the architecture:
1116 *
1117 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1118 * value or to 1023, depending on the enable bit. But that
1119 * would be issueing a mapping for an /existing/ DevID+EventID
1120 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1121 * to the /same/ vPE, using this opportunity to adjust the
1122 * doorbell. Mouahahahaha. We loves it, Precious.
1123 */
1124 its_send_vmovi(its_dev, event);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001125}
1126
1127static void its_mask_irq(struct irq_data *d)
1128{
Marc Zyngier015ec032016-12-20 09:54:57 +00001129 if (irqd_is_forwarded_to_vcpu(d))
1130 its_vlpi_set_doorbell(d, false);
1131
Marc Zyngieradcdb942016-12-19 19:18:13 +00001132 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001133}
1134
1135static void its_unmask_irq(struct irq_data *d)
1136{
Marc Zyngier015ec032016-12-20 09:54:57 +00001137 if (irqd_is_forwarded_to_vcpu(d))
1138 its_vlpi_set_doorbell(d, true);
1139
Marc Zyngieradcdb942016-12-19 19:18:13 +00001140 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001141}
1142
Marc Zyngierc48ed512014-11-24 14:35:12 +00001143static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1144 bool force)
1145{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001146 unsigned int cpu;
1147 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001148 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1149 struct its_collection *target_col;
1150 u32 id = its_get_event_id(d);
1151
Marc Zyngier015ec032016-12-20 09:54:57 +00001152 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1153 if (irqd_is_forwarded_to_vcpu(d))
1154 return -EINVAL;
1155
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001156 /* lpi cannot be routed to a redistributor that is on a foreign node */
1157 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1158 if (its_dev->its->numa_node >= 0) {
1159 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1160 if (!cpumask_intersects(mask_val, cpu_mask))
1161 return -EINVAL;
1162 }
1163 }
1164
1165 cpu = cpumask_any_and(mask_val, cpu_mask);
1166
Marc Zyngierc48ed512014-11-24 14:35:12 +00001167 if (cpu >= nr_cpu_ids)
1168 return -EINVAL;
1169
MaJun8b8d94a2017-05-18 16:19:13 +08001170 /* don't set the affinity when the target cpu is same as current one */
1171 if (cpu != its_dev->event_map.col_map[id]) {
1172 target_col = &its_dev->its->collections[cpu];
1173 its_send_movi(its_dev, target_col, id);
1174 its_dev->event_map.col_map[id] = cpu;
Marc Zyngier0d224d32017-08-18 09:39:18 +01001175 irq_data_update_effective_affinity(d, cpumask_of(cpu));
MaJun8b8d94a2017-05-18 16:19:13 +08001176 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001177
1178 return IRQ_SET_MASK_OK_DONE;
1179}
1180
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001181static u64 its_irq_get_msi_base(struct its_device *its_dev)
1182{
1183 struct its_node *its = its_dev->its;
1184
1185 return its->phys_base + GITS_TRANSLATER;
1186}
1187
Marc Zyngierb48ac832014-11-24 14:35:16 +00001188static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1189{
1190 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1191 struct its_node *its;
1192 u64 addr;
1193
1194 its = its_dev->its;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001195 addr = its->get_msi_base(its_dev);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001196
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001197 msg->address_lo = lower_32_bits(addr);
1198 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001199 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001200
Julien Grall35ae7df2019-05-01 14:58:21 +01001201 iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001202}
1203
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001204static int its_irq_set_irqchip_state(struct irq_data *d,
1205 enum irqchip_irq_state which,
1206 bool state)
1207{
1208 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1209 u32 event = its_get_event_id(d);
1210
1211 if (which != IRQCHIP_STATE_PENDING)
1212 return -EINVAL;
1213
1214 if (state)
1215 its_send_int(its_dev, event);
1216 else
1217 its_send_clear(its_dev, event);
1218
1219 return 0;
1220}
1221
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001222static void its_map_vm(struct its_node *its, struct its_vm *vm)
1223{
1224 unsigned long flags;
1225
1226 /* Not using the ITS list? Everything is always mapped. */
1227 if (!its_list_map)
1228 return;
1229
1230 raw_spin_lock_irqsave(&vmovp_lock, flags);
1231
1232 /*
1233 * If the VM wasn't mapped yet, iterate over the vpes and get
1234 * them mapped now.
1235 */
1236 vm->vlpi_count[its->list_nr]++;
1237
1238 if (vm->vlpi_count[its->list_nr] == 1) {
1239 int i;
1240
1241 for (i = 0; i < vm->nr_vpes; i++) {
1242 struct its_vpe *vpe = vm->vpes[i];
Marc Zyngier44c4c252017-10-19 10:11:34 +01001243 struct irq_data *d = irq_get_irq_data(vpe->irq);
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001244
1245 /* Map the VPE to the first possible CPU */
1246 vpe->col_idx = cpumask_first(cpu_online_mask);
1247 its_send_vmapp(its, vpe, true);
1248 its_send_vinvall(its, vpe);
Marc Zyngier44c4c252017-10-19 10:11:34 +01001249 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001250 }
1251 }
1252
1253 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1254}
1255
1256static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1257{
1258 unsigned long flags;
1259
1260 /* Not using the ITS list? Everything is always mapped. */
1261 if (!its_list_map)
1262 return;
1263
1264 raw_spin_lock_irqsave(&vmovp_lock, flags);
1265
1266 if (!--vm->vlpi_count[its->list_nr]) {
1267 int i;
1268
1269 for (i = 0; i < vm->nr_vpes; i++)
1270 its_send_vmapp(its, vm->vpes[i], false);
1271 }
1272
1273 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1274}
1275
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001276static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1277{
1278 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1279 u32 event = its_get_event_id(d);
1280 int ret = 0;
1281
1282 if (!info->map)
1283 return -EINVAL;
1284
1285 mutex_lock(&its_dev->event_map.vlpi_lock);
1286
1287 if (!its_dev->event_map.vm) {
1288 struct its_vlpi_map *maps;
1289
Kees Cook6396bb22018-06-12 14:03:40 -07001290 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001291 GFP_KERNEL);
1292 if (!maps) {
1293 ret = -ENOMEM;
1294 goto out;
1295 }
1296
1297 its_dev->event_map.vm = info->map->vm;
1298 its_dev->event_map.vlpi_maps = maps;
1299 } else if (its_dev->event_map.vm != info->map->vm) {
1300 ret = -EINVAL;
1301 goto out;
1302 }
1303
1304 /* Get our private copy of the mapping information */
1305 its_dev->event_map.vlpi_maps[event] = *info->map;
1306
1307 if (irqd_is_forwarded_to_vcpu(d)) {
1308 /* Already mapped, move it around */
1309 its_send_vmovi(its_dev, event);
1310 } else {
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001311 /* Ensure all the VPEs are mapped on this ITS */
1312 its_map_vm(its_dev->its, info->map->vm);
1313
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001314 /*
1315 * Flag the interrupt as forwarded so that we can
1316 * start poking the virtual property table.
1317 */
1318 irqd_set_forwarded_to_vcpu(d);
1319
1320 /* Write out the property to the prop table */
1321 lpi_write_config(d, 0xff, info->map->properties);
1322
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001323 /* Drop the physical mapping */
1324 its_send_discard(its_dev, event);
1325
1326 /* and install the virtual one */
1327 its_send_vmapti(its_dev, event);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001328
1329 /* Increment the number of VLPIs */
1330 its_dev->event_map.nr_vlpis++;
1331 }
1332
1333out:
1334 mutex_unlock(&its_dev->event_map.vlpi_lock);
1335 return ret;
1336}
1337
1338static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1339{
1340 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1341 u32 event = its_get_event_id(d);
1342 int ret = 0;
1343
1344 mutex_lock(&its_dev->event_map.vlpi_lock);
1345
1346 if (!its_dev->event_map.vm ||
1347 !its_dev->event_map.vlpi_maps[event].vm) {
1348 ret = -EINVAL;
1349 goto out;
1350 }
1351
1352 /* Copy our mapping information to the incoming request */
1353 *info->map = its_dev->event_map.vlpi_maps[event];
1354
1355out:
1356 mutex_unlock(&its_dev->event_map.vlpi_lock);
1357 return ret;
1358}
1359
1360static int its_vlpi_unmap(struct irq_data *d)
1361{
1362 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1363 u32 event = its_get_event_id(d);
1364 int ret = 0;
1365
1366 mutex_lock(&its_dev->event_map.vlpi_lock);
1367
1368 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1369 ret = -EINVAL;
1370 goto out;
1371 }
1372
1373 /* Drop the virtual mapping */
1374 its_send_discard(its_dev, event);
1375
1376 /* and restore the physical one */
1377 irqd_clr_forwarded_to_vcpu(d);
1378 its_send_mapti(its_dev, d->hwirq, event);
1379 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1380 LPI_PROP_ENABLED |
1381 LPI_PROP_GROUP1));
1382
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001383 /* Potentially unmap the VM from this ITS */
1384 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1385
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001386 /*
1387 * Drop the refcount and make the device available again if
1388 * this was the last VLPI.
1389 */
1390 if (!--its_dev->event_map.nr_vlpis) {
1391 its_dev->event_map.vm = NULL;
1392 kfree(its_dev->event_map.vlpi_maps);
1393 }
1394
1395out:
1396 mutex_unlock(&its_dev->event_map.vlpi_lock);
1397 return ret;
1398}
1399
Marc Zyngier015ec032016-12-20 09:54:57 +00001400static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1401{
1402 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1403
1404 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1405 return -EINVAL;
1406
1407 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1408 lpi_update_config(d, 0xff, info->config);
1409 else
1410 lpi_write_config(d, 0xff, info->config);
1411 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1412
1413 return 0;
1414}
1415
Marc Zyngierc808eea2016-12-20 09:31:20 +00001416static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1417{
1418 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1419 struct its_cmd_info *info = vcpu_info;
1420
1421 /* Need a v4 ITS */
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001422 if (!its_dev->its->is_v4)
Marc Zyngierc808eea2016-12-20 09:31:20 +00001423 return -EINVAL;
1424
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001425 /* Unmap request? */
1426 if (!info)
1427 return its_vlpi_unmap(d);
1428
Marc Zyngierc808eea2016-12-20 09:31:20 +00001429 switch (info->cmd_type) {
1430 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001431 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001432
1433 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001434 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001435
1436 case PROP_UPDATE_VLPI:
1437 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001438 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001439
1440 default:
1441 return -EINVAL;
1442 }
1443}
1444
Marc Zyngierc48ed512014-11-24 14:35:12 +00001445static struct irq_chip its_irq_chip = {
1446 .name = "ITS",
1447 .irq_mask = its_mask_irq,
1448 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001449 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001450 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001451 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001452 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001453 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001454};
1455
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001456
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001457/*
1458 * How we allocate LPIs:
1459 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001460 * lpi_range_list contains ranges of LPIs that are to available to
1461 * allocate from. To allocate LPIs, just pick the first range that
1462 * fits the required allocation, and reduce it by the required
1463 * amount. Once empty, remove the range from the list.
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001464 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001465 * To free a range of LPIs, add a free range to the list, sort it and
1466 * merge the result if the new range happens to be adjacent to an
1467 * already free block.
1468 *
1469 * The consequence of the above is that allocation is cost is low, but
1470 * freeing is expensive. We assumes that freeing rarely occurs.
1471 */
Jia He4cb205c2018-08-28 12:53:26 +08001472#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001473
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001474static DEFINE_MUTEX(lpi_range_lock);
1475static LIST_HEAD(lpi_range_list);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001476
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001477struct lpi_range {
1478 struct list_head entry;
1479 u32 base_id;
1480 u32 span;
1481};
1482
1483static struct lpi_range *mk_lpi_range(u32 base, u32 span)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001484{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001485 struct lpi_range *range;
1486
Rasmus Villemoes1c73fac2019-03-12 18:33:48 +01001487 range = kmalloc(sizeof(*range), GFP_KERNEL);
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001488 if (range) {
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001489 range->base_id = base;
1490 range->span = span;
1491 }
1492
1493 return range;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001494}
1495
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001496static int alloc_lpi_range(u32 nr_lpis, u32 *base)
1497{
1498 struct lpi_range *range, *tmp;
1499 int err = -ENOSPC;
1500
1501 mutex_lock(&lpi_range_lock);
1502
1503 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1504 if (range->span >= nr_lpis) {
1505 *base = range->base_id;
1506 range->base_id += nr_lpis;
1507 range->span -= nr_lpis;
1508
1509 if (range->span == 0) {
1510 list_del(&range->entry);
1511 kfree(range);
1512 }
1513
1514 err = 0;
1515 break;
1516 }
1517 }
1518
1519 mutex_unlock(&lpi_range_lock);
1520
1521 pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
1522 return err;
1523}
1524
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001525static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b)
1526{
1527 if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list)
1528 return;
1529 if (a->base_id + a->span != b->base_id)
1530 return;
1531 b->base_id = a->base_id;
1532 b->span += a->span;
1533 list_del(&a->entry);
1534 kfree(a);
1535}
1536
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001537static int free_lpi_range(u32 base, u32 nr_lpis)
1538{
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001539 struct lpi_range *new, *old;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001540
1541 new = mk_lpi_range(base, nr_lpis);
Rasmus Villemoesb31a3832019-03-12 18:33:47 +01001542 if (!new)
1543 return -ENOMEM;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001544
1545 mutex_lock(&lpi_range_lock);
1546
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001547 list_for_each_entry_reverse(old, &lpi_range_list, entry) {
1548 if (old->base_id < base)
1549 break;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001550 }
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001551 /*
1552 * old is the last element with ->base_id smaller than base,
1553 * so new goes right after it. If there are no elements with
1554 * ->base_id smaller than base, &old->entry ends up pointing
1555 * at the head of the list, and inserting new it the start of
1556 * the list is the right thing to do in that case as well.
1557 */
1558 list_add(&new->entry, &old->entry);
1559 /*
1560 * Now check if we can merge with the preceding and/or
1561 * following ranges.
1562 */
1563 merge_lpi_ranges(old, new);
1564 merge_lpi_ranges(new, list_next_entry(new, entry));
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001565
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001566 mutex_unlock(&lpi_range_lock);
Rasmus Villemoesb31a3832019-03-12 18:33:47 +01001567 return 0;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001568}
1569
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001570static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001571{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001572 u32 lpis = (1UL << id_bits) - 8192;
Marc Zyngier12b29052018-05-31 09:01:59 +01001573 u32 numlpis;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001574 int err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001575
Marc Zyngier12b29052018-05-31 09:01:59 +01001576 numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
1577
1578 if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
1579 lpis = numlpis;
1580 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
1581 lpis);
1582 }
1583
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001584 /*
1585 * Initializing the allocator is just the same as freeing the
1586 * full range of LPIs.
1587 */
1588 err = free_lpi_range(8192, lpis);
1589 pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
1590 return err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001591}
1592
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001593static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001594{
1595 unsigned long *bitmap = NULL;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001596 int err = 0;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001597
1598 do {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001599 err = alloc_lpi_range(nr_irqs, base);
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001600 if (!err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001601 break;
1602
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001603 nr_irqs /= 2;
1604 } while (nr_irqs > 0);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001605
Marc Zyngier45725e02019-01-29 15:19:23 +00001606 if (!nr_irqs)
1607 err = -ENOSPC;
1608
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001609 if (err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001610 goto out;
1611
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001612 bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001613 if (!bitmap)
1614 goto out;
1615
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001616 *nr_ids = nr_irqs;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001617
1618out:
Marc Zyngierc8415b92015-10-02 16:44:05 +01001619 if (!bitmap)
1620 *base = *nr_ids = 0;
1621
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001622 return bitmap;
1623}
1624
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001625static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001626{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001627 WARN_ON(free_lpi_range(base, nr_ids));
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001628 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001629}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001630
Marc Zyngier053be482018-07-27 15:02:27 +01001631static void gic_reset_prop_table(void *va)
1632{
1633 /* Priority 0xa0, Group-1, disabled */
1634 memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ);
1635
1636 /* Make sure the GIC will observe the written configuration */
1637 gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ);
1638}
1639
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001640static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1641{
1642 struct page *prop_page;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001643
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001644 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1645 if (!prop_page)
1646 return NULL;
1647
Marc Zyngier053be482018-07-27 15:02:27 +01001648 gic_reset_prop_table(page_address(prop_page));
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001649
1650 return prop_page;
1651}
1652
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001653static void its_free_prop_table(struct page *prop_page)
1654{
1655 free_pages((unsigned long)page_address(prop_page),
1656 get_order(LPI_PROPBASE_SZ));
1657}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001658
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01001659static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
1660{
1661 phys_addr_t start, end, addr_end;
1662 u64 i;
1663
1664 /*
1665 * We don't bother checking for a kdump kernel as by
1666 * construction, the LPI tables are out of this kernel's
1667 * memory map.
1668 */
1669 if (is_kdump_kernel())
1670 return true;
1671
1672 addr_end = addr + size - 1;
1673
1674 for_each_reserved_mem_region(i, &start, &end) {
1675 if (addr >= start && addr_end <= end)
1676 return true;
1677 }
1678
1679 /* Not found, not a good sign... */
1680 pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n",
1681 &addr, &addr_end);
1682 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
1683 return false;
1684}
1685
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01001686static int gic_reserve_range(phys_addr_t addr, unsigned long size)
1687{
1688 if (efi_enabled(EFI_CONFIG_TABLES))
1689 return efi_mem_reserve_persistent(addr, size);
1690
1691 return 0;
1692}
1693
Marc Zyngier11e37d32018-07-27 13:38:54 +01001694static int __init its_setup_lpi_prop_table(void)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001695{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01001696 if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) {
1697 u64 val;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001698
Marc Zyngierc440a9d2018-07-27 15:40:13 +01001699 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
1700 lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1;
1701
1702 gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12);
1703 gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa,
1704 LPI_PROPBASE_SZ,
1705 MEMREMAP_WB);
1706 gic_reset_prop_table(gic_rdists->prop_table_va);
1707 } else {
1708 struct page *page;
1709
1710 lpi_id_bits = min_t(u32,
1711 GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
1712 ITS_MAX_LPI_NRBITS);
1713 page = its_allocate_prop_table(GFP_NOWAIT);
1714 if (!page) {
1715 pr_err("Failed to allocate PROPBASE\n");
1716 return -ENOMEM;
1717 }
1718
1719 gic_rdists->prop_table_pa = page_to_phys(page);
1720 gic_rdists->prop_table_va = page_address(page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01001721 WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa,
1722 LPI_PROPBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001723 }
1724
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001725 pr_info("GICv3: using LPI property table @%pa\n",
1726 &gic_rdists->prop_table_pa);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001727
Shanker Donthineni6c31e122017-06-22 18:19:14 -05001728 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001729}
1730
1731static const char *its_base_type_string[] = {
1732 [GITS_BASER_TYPE_DEVICE] = "Devices",
1733 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00001734 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001735 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
1736 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
1737 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
1738 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
1739};
1740
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001741static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
1742{
1743 u32 idx = baser - its->tables;
1744
Vladimir Murzin0968a612016-11-02 11:54:06 +00001745 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001746}
1747
1748static void its_write_baser(struct its_node *its, struct its_baser *baser,
1749 u64 val)
1750{
1751 u32 idx = baser - its->tables;
1752
Vladimir Murzin0968a612016-11-02 11:54:06 +00001753 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001754 baser->val = its_read_baser(its, baser);
1755}
1756
Shanker Donthineni93473592016-06-06 18:17:30 -05001757static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001758 u64 cache, u64 shr, u32 psz, u32 order,
1759 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05001760{
1761 u64 val = its_read_baser(its, baser);
1762 u64 esz = GITS_BASER_ENTRY_SIZE(val);
1763 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001764 u64 baser_phys, tmp;
Shanker Donthineni93473592016-06-06 18:17:30 -05001765 u32 alloc_pages;
Shanker Donthineni539d3782019-01-14 09:50:19 +00001766 struct page *page;
Shanker Donthineni93473592016-06-06 18:17:30 -05001767 void *base;
Shanker Donthineni93473592016-06-06 18:17:30 -05001768
1769retry_alloc_baser:
1770 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
1771 if (alloc_pages > GITS_BASER_PAGES_MAX) {
1772 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
1773 &its->phys_base, its_base_type_string[type],
1774 alloc_pages, GITS_BASER_PAGES_MAX);
1775 alloc_pages = GITS_BASER_PAGES_MAX;
1776 order = get_order(GITS_BASER_PAGES_MAX * psz);
1777 }
1778
Shanker Donthineni539d3782019-01-14 09:50:19 +00001779 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order);
1780 if (!page)
Shanker Donthineni93473592016-06-06 18:17:30 -05001781 return -ENOMEM;
1782
Shanker Donthineni539d3782019-01-14 09:50:19 +00001783 base = (void *)page_address(page);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001784 baser_phys = virt_to_phys(base);
1785
1786 /* Check if the physical address of the memory is above 48bits */
1787 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
1788
1789 /* 52bit PA is supported only when PageSize=64K */
1790 if (psz != SZ_64K) {
1791 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
1792 free_pages((unsigned long)base, order);
1793 return -ENXIO;
1794 }
1795
1796 /* Convert 52bit PA to 48bit field */
1797 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
1798 }
1799
Shanker Donthineni93473592016-06-06 18:17:30 -05001800retry_baser:
Shanker Donthineni30ae9612017-10-09 11:46:55 -05001801 val = (baser_phys |
Shanker Donthineni93473592016-06-06 18:17:30 -05001802 (type << GITS_BASER_TYPE_SHIFT) |
1803 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
1804 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
1805 cache |
1806 shr |
1807 GITS_BASER_VALID);
1808
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001809 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
1810
Shanker Donthineni93473592016-06-06 18:17:30 -05001811 switch (psz) {
1812 case SZ_4K:
1813 val |= GITS_BASER_PAGE_SIZE_4K;
1814 break;
1815 case SZ_16K:
1816 val |= GITS_BASER_PAGE_SIZE_16K;
1817 break;
1818 case SZ_64K:
1819 val |= GITS_BASER_PAGE_SIZE_64K;
1820 break;
1821 }
1822
1823 its_write_baser(its, baser, val);
1824 tmp = baser->val;
1825
1826 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
1827 /*
1828 * Shareability didn't stick. Just use
1829 * whatever the read reported, which is likely
1830 * to be the only thing this redistributor
1831 * supports. If that's zero, make it
1832 * non-cacheable as well.
1833 */
1834 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
1835 if (!shr) {
1836 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00001837 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05001838 }
1839 goto retry_baser;
1840 }
1841
1842 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
1843 /*
1844 * Page size didn't stick. Let's try a smaller
1845 * size and retry. If we reach 4K, then
1846 * something is horribly wrong...
1847 */
1848 free_pages((unsigned long)base, order);
1849 baser->base = NULL;
1850
1851 switch (psz) {
1852 case SZ_16K:
1853 psz = SZ_4K;
1854 goto retry_alloc_baser;
1855 case SZ_64K:
1856 psz = SZ_16K;
1857 goto retry_alloc_baser;
1858 }
1859 }
1860
1861 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001862 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05001863 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001864 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05001865 free_pages((unsigned long)base, order);
1866 return -ENXIO;
1867 }
1868
1869 baser->order = order;
1870 baser->base = base;
1871 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001872 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05001873
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001874 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001875 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05001876 its_base_type_string[type],
1877 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001878 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05001879 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
1880
1881 return 0;
1882}
1883
Marc Zyngier4cacac52016-12-19 18:18:34 +00001884static bool its_parse_indirect_baser(struct its_node *its,
1885 struct its_baser *baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001886 u32 psz, u32 *order, u32 ids)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001887{
Marc Zyngier4cacac52016-12-19 18:18:34 +00001888 u64 tmp = its_read_baser(its, baser);
1889 u64 type = GITS_BASER_TYPE(tmp);
1890 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001891 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001892 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001893 bool indirect = false;
1894
1895 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
1896 if ((esz << ids) > (psz * 2)) {
1897 /*
1898 * Find out whether hw supports a single or two-level table by
1899 * table by reading bit at offset '62' after writing '1' to it.
1900 */
1901 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
1902 indirect = !!(baser->val & GITS_BASER_INDIRECT);
1903
1904 if (indirect) {
1905 /*
1906 * The size of the lvl2 table is equal to ITS page size
1907 * which is 'psz'. For computing lvl1 table size,
1908 * subtract ID bits that sparse lvl2 table from 'ids'
1909 * which is reported by ITS hardware times lvl1 table
1910 * entry size.
1911 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001912 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001913 esz = GITS_LVL1_ENTRY_SIZE;
1914 }
1915 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001916
1917 /*
1918 * Allocate as many entries as required to fit the
1919 * range of device IDs that the ITS can grok... The ID
1920 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001921 * massive waste of memory if two-level device table
1922 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001923 */
1924 new_order = max_t(u32, get_order(esz << ids), new_order);
1925 if (new_order >= MAX_ORDER) {
1926 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00001927 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001928 pr_warn("ITS@%pa: %s Table too large, reduce ids %u->%u\n",
1929 &its->phys_base, its_base_type_string[type],
1930 its->device_ids, ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001931 }
1932
1933 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001934
1935 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05001936}
1937
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001938static void its_free_tables(struct its_node *its)
1939{
1940 int i;
1941
1942 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06001943 if (its->tables[i].base) {
1944 free_pages((unsigned long)its->tables[i].base,
1945 its->tables[i].order);
1946 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001947 }
1948 }
1949}
1950
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05001951static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001952{
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001953 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06001954 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05001955 u32 psz = SZ_64K;
1956 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02001957
Ard Biesheuvelfa150012017-10-17 17:55:54 +01001958 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
1959 /* erratum 24313: ignore memory access type */
1960 cache = GITS_BASER_nCnB;
Shanker Donthineni466b7d12016-03-09 22:10:49 -06001961
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001962 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05001963 struct its_baser *baser = its->tables + i;
1964 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001965 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05001966 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001967 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001968
Marc Zyngier4cacac52016-12-19 18:18:34 +00001969 switch (type) {
1970 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001971 continue;
1972
Marc Zyngier4cacac52016-12-19 18:18:34 +00001973 case GITS_BASER_TYPE_DEVICE:
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001974 indirect = its_parse_indirect_baser(its, baser,
1975 psz, &order,
1976 its->device_ids);
Zenghui Yu8d565742019-02-10 05:24:10 +00001977 break;
1978
Marc Zyngier4cacac52016-12-19 18:18:34 +00001979 case GITS_BASER_TYPE_VCPU:
1980 indirect = its_parse_indirect_baser(its, baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05001981 psz, &order,
1982 ITS_MAX_VPEID_BITS);
Marc Zyngier4cacac52016-12-19 18:18:34 +00001983 break;
1984 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00001985
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05001986 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05001987 if (err < 0) {
1988 its_free_tables(its);
1989 return err;
Robert Richter30f21362015-09-21 22:58:34 +02001990 }
1991
Shanker Donthineni93473592016-06-06 18:17:30 -05001992 /* Update settings which will be used for next BASERn */
1993 psz = baser->psz;
1994 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
1995 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001996 }
1997
1998 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001999}
2000
2001static int its_alloc_collections(struct its_node *its)
2002{
Marc Zyngier83559b42018-06-22 10:52:52 +01002003 int i;
2004
Kees Cook6396bb22018-06-12 14:03:40 -07002005 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002006 GFP_KERNEL);
2007 if (!its->collections)
2008 return -ENOMEM;
2009
Marc Zyngier83559b42018-06-22 10:52:52 +01002010 for (i = 0; i < nr_cpu_ids; i++)
2011 its->collections[i].target_address = ~0ULL;
2012
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002013 return 0;
2014}
2015
Marc Zyngier7c297a22016-12-19 18:34:38 +00002016static struct page *its_allocate_pending_table(gfp_t gfp_flags)
2017{
2018 struct page *pend_page;
Marc Zyngieradaab502018-07-17 18:06:39 +01002019
Marc Zyngier7c297a22016-12-19 18:34:38 +00002020 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
Marc Zyngieradaab502018-07-17 18:06:39 +01002021 get_order(LPI_PENDBASE_SZ));
Marc Zyngier7c297a22016-12-19 18:34:38 +00002022 if (!pend_page)
2023 return NULL;
2024
2025 /* Make sure the GIC will observe the zero-ed page */
2026 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
2027
2028 return pend_page;
2029}
2030
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002031static void its_free_pending_table(struct page *pt)
2032{
Marc Zyngieradaab502018-07-17 18:06:39 +01002033 free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ));
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002034}
2035
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002036/*
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002037 * Booting with kdump and LPIs enabled is generally fine. Any other
2038 * case is wrong in the absence of firmware/EFI support.
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002039 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002040static bool enabled_lpis_allowed(void)
2041{
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002042 phys_addr_t addr;
2043 u64 val;
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002044
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002045 /* Check whether the property table is in a reserved region */
2046 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2047 addr = val & GENMASK_ULL(51, 12);
2048
2049 return gic_check_reserved_range(addr, LPI_PROPBASE_SZ);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002050}
2051
Marc Zyngier11e37d32018-07-27 13:38:54 +01002052static int __init allocate_lpi_tables(void)
2053{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002054 u64 val;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002055 int err, cpu;
2056
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002057 /*
2058 * If LPIs are enabled while we run this from the boot CPU,
2059 * flag the RD tables as pre-allocated if the stars do align.
2060 */
2061 val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR);
2062 if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) {
2063 gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED |
2064 RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING);
2065 pr_info("GICv3: Using preallocated redistributor tables\n");
2066 }
2067
Marc Zyngier11e37d32018-07-27 13:38:54 +01002068 err = its_setup_lpi_prop_table();
2069 if (err)
2070 return err;
2071
2072 /*
2073 * We allocate all the pending tables anyway, as we may have a
2074 * mix of RDs that have had LPIs enabled, and some that
2075 * don't. We'll free the unused ones as each CPU comes online.
2076 */
2077 for_each_possible_cpu(cpu) {
2078 struct page *pend_page;
2079
2080 pend_page = its_allocate_pending_table(GFP_NOWAIT);
2081 if (!pend_page) {
2082 pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu);
2083 return -ENOMEM;
2084 }
2085
2086 gic_data_rdist_cpu(cpu)->pend_page = pend_page;
2087 }
2088
2089 return 0;
2090}
2091
Heyi Guo64794502019-01-24 21:37:08 +08002092static u64 its_clear_vpend_valid(void __iomem *vlpi_base)
2093{
2094 u32 count = 1000000; /* 1s! */
2095 bool clean;
2096 u64 val;
2097
2098 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2099 val &= ~GICR_VPENDBASER_Valid;
2100 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2101
2102 do {
2103 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2104 clean = !(val & GICR_VPENDBASER_Dirty);
2105 if (!clean) {
2106 count--;
2107 cpu_relax();
2108 udelay(1);
2109 }
2110 } while (!clean && count);
2111
2112 return val;
2113}
2114
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002115static void its_cpu_init_lpis(void)
2116{
2117 void __iomem *rbase = gic_data_rdist_rd_base();
2118 struct page *pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002119 phys_addr_t paddr;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002120 u64 val, tmp;
2121
Marc Zyngier11e37d32018-07-27 13:38:54 +01002122 if (gic_data_rdist()->lpi_enabled)
2123 return;
2124
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002125 val = readl_relaxed(rbase + GICR_CTLR);
2126 if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) &&
2127 (val & GICR_CTLR_ENABLE_LPIS)) {
Marc Zyngierf842ca82018-07-27 16:03:31 +01002128 /*
2129 * Check that we get the same property table on all
2130 * RDs. If we don't, this is hopeless.
2131 */
2132 paddr = gicr_read_propbaser(rbase + GICR_PROPBASER);
2133 paddr &= GENMASK_ULL(51, 12);
2134 if (WARN_ON(gic_rdists->prop_table_pa != paddr))
2135 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
2136
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002137 paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER);
2138 paddr &= GENMASK_ULL(51, 16);
2139
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002140 WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002141 its_free_pending_table(gic_data_rdist()->pend_page);
2142 gic_data_rdist()->pend_page = NULL;
2143
2144 goto out;
2145 }
2146
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002147 pend_page = gic_data_rdist()->pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002148 paddr = page_to_phys(pend_page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01002149 WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002150
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002151 /* set PROPBASE */
Marc Zyngiere1a2e202018-07-27 14:36:00 +01002152 val = (gic_rdists->prop_table_pa |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002153 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002154 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002155 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
2156
Vladimir Murzin0968a612016-11-02 11:54:06 +00002157 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2158 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002159
2160 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002161 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
2162 /*
2163 * The HW reports non-shareable, we must
2164 * remove the cacheability attributes as
2165 * well.
2166 */
2167 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
2168 GICR_PROPBASER_CACHEABILITY_MASK);
2169 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002170 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002171 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002172 pr_info_once("GIC: using cache flushing for LPI property table\n");
2173 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
2174 }
2175
2176 /* set PENDBASE */
2177 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002178 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002179 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002180
Vladimir Murzin0968a612016-11-02 11:54:06 +00002181 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2182 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002183
2184 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
2185 /*
2186 * The HW reports non-shareable, we must remove the
2187 * cacheability attributes as well.
2188 */
2189 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
2190 GICR_PENDBASER_CACHEABILITY_MASK);
2191 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002192 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002193 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002194
2195 /* Enable LPIs */
2196 val = readl_relaxed(rbase + GICR_CTLR);
2197 val |= GICR_CTLR_ENABLE_LPIS;
2198 writel_relaxed(val, rbase + GICR_CTLR);
2199
Heyi Guo64794502019-01-24 21:37:08 +08002200 if (gic_rdists->has_vlpis) {
2201 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2202
2203 /*
2204 * It's possible for CPU to receive VLPIs before it is
2205 * sheduled as a vPE, especially for the first CPU, and the
2206 * VLPI with INTID larger than 2^(IDbits+1) will be considered
2207 * as out of range and dropped by GIC.
2208 * So we initialize IDbits to known value to avoid VLPI drop.
2209 */
2210 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2211 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
2212 smp_processor_id(), val);
2213 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2214
2215 /*
2216 * Also clear Valid bit of GICR_VPENDBASER, in case some
2217 * ancient programming gets left in and has possibility of
2218 * corrupting memory.
2219 */
2220 val = its_clear_vpend_valid(vlpi_base);
2221 WARN_ON(val & GICR_VPENDBASER_Dirty);
2222 }
2223
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002224 /* Make sure the GIC has seen the above */
2225 dsb(sy);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002226out:
Marc Zyngier11e37d32018-07-27 13:38:54 +01002227 gic_data_rdist()->lpi_enabled = true;
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002228 pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n",
Marc Zyngier11e37d32018-07-27 13:38:54 +01002229 smp_processor_id(),
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002230 gic_data_rdist()->pend_page ? "allocated" : "reserved",
Marc Zyngier11e37d32018-07-27 13:38:54 +01002231 &paddr);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002232}
2233
Derek Basehore920181c2018-02-28 21:48:20 -08002234static void its_cpu_init_collection(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002235{
Derek Basehore920181c2018-02-28 21:48:20 -08002236 int cpu = smp_processor_id();
2237 u64 target;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002238
Derek Basehore920181c2018-02-28 21:48:20 -08002239 /* avoid cross node collections and its mapping */
2240 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
2241 struct device_node *cpu_node;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002242
Derek Basehore920181c2018-02-28 21:48:20 -08002243 cpu_node = of_get_cpu_node(cpu, NULL);
2244 if (its->numa_node != NUMA_NO_NODE &&
2245 its->numa_node != of_node_to_nid(cpu_node))
2246 return;
2247 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002248
Derek Basehore920181c2018-02-28 21:48:20 -08002249 /*
2250 * We now have to bind each collection to its target
2251 * redistributor.
2252 */
2253 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002254 /*
Derek Basehore920181c2018-02-28 21:48:20 -08002255 * This ITS wants the physical address of the
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002256 * redistributor.
2257 */
Derek Basehore920181c2018-02-28 21:48:20 -08002258 target = gic_data_rdist()->phys_base;
2259 } else {
2260 /* This ITS wants a linear CPU number. */
2261 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2262 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002263 }
2264
Derek Basehore920181c2018-02-28 21:48:20 -08002265 /* Perform collection mapping */
2266 its->collections[cpu].target_address = target;
2267 its->collections[cpu].col_id = cpu;
2268
2269 its_send_mapc(its, &its->collections[cpu], 1);
2270 its_send_invall(its, &its->collections[cpu]);
2271}
2272
2273static void its_cpu_init_collections(void)
2274{
2275 struct its_node *its;
2276
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02002277 raw_spin_lock(&its_lock);
Derek Basehore920181c2018-02-28 21:48:20 -08002278
2279 list_for_each_entry(its, &its_nodes, entry)
2280 its_cpu_init_collection(its);
2281
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02002282 raw_spin_unlock(&its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002283}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002284
2285static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
2286{
2287 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002288 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002289
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002290 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002291
2292 list_for_each_entry(tmp, &its->its_device_list, entry) {
2293 if (tmp->device_id == dev_id) {
2294 its_dev = tmp;
2295 break;
2296 }
2297 }
2298
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002299 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002300
2301 return its_dev;
2302}
2303
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002304static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2305{
2306 int i;
2307
2308 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2309 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2310 return &its->tables[i];
2311 }
2312
2313 return NULL;
2314}
2315
Shanker Donthineni539d3782019-01-14 09:50:19 +00002316static bool its_alloc_table_entry(struct its_node *its,
2317 struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002318{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002319 struct page *page;
2320 u32 esz, idx;
2321 __le64 *table;
2322
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002323 /* Don't allow device id that exceeds single, flat table limit */
2324 esz = GITS_BASER_ENTRY_SIZE(baser->val);
2325 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002326 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002327
2328 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002329 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002330 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
2331 return false;
2332
2333 table = baser->base;
2334
2335 /* Allocate memory for 2nd level table */
2336 if (!table[idx]) {
Shanker Donthineni539d3782019-01-14 09:50:19 +00002337 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
2338 get_order(baser->psz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002339 if (!page)
2340 return false;
2341
2342 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2343 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00002344 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002345
2346 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2347
2348 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2349 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00002350 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002351
2352 /* Ensure updated table contents are visible to ITS hardware */
2353 dsb(sy);
2354 }
2355
2356 return true;
2357}
2358
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002359static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
2360{
2361 struct its_baser *baser;
2362
2363 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
2364
2365 /* Don't allow device id that exceeds ITS hardware limit */
2366 if (!baser)
2367 return (ilog2(dev_id) < its->device_ids);
2368
Shanker Donthineni539d3782019-01-14 09:50:19 +00002369 return its_alloc_table_entry(its, baser, dev_id);
Marc Zyngier70cc81e2016-12-19 18:53:02 +00002370}
2371
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002372static bool its_alloc_vpe_table(u32 vpe_id)
2373{
2374 struct its_node *its;
2375
2376 /*
2377 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
2378 * could try and only do it on ITSs corresponding to devices
2379 * that have interrupts targeted at this VPE, but the
2380 * complexity becomes crazy (and you have tons of memory
2381 * anyway, right?).
2382 */
2383 list_for_each_entry(its, &its_nodes, entry) {
2384 struct its_baser *baser;
2385
2386 if (!its->is_v4)
2387 continue;
2388
2389 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
2390 if (!baser)
2391 return false;
2392
Shanker Donthineni539d3782019-01-14 09:50:19 +00002393 if (!its_alloc_table_entry(its, baser, vpe_id))
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002394 return false;
2395 }
2396
2397 return true;
2398}
2399
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002400static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002401 int nvecs, bool alloc_lpis)
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002402{
2403 struct its_device *dev;
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002404 unsigned long *lpi_map = NULL;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002405 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002406 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002407 void *itt;
2408 int lpi_base;
2409 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00002410 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002411 int sz;
2412
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002413 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002414 return NULL;
2415
Marc Zyngier147c8f32018-05-27 16:39:55 +01002416 if (WARN_ON(!is_power_of_2(nvecs)))
2417 nvecs = roundup_pow_of_two(nvecs);
2418
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002419 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00002420 /*
Marc Zyngier147c8f32018-05-27 16:39:55 +01002421 * Even if the device wants a single LPI, the ITT must be
2422 * sized as a power of two (and you need at least one bit...).
Marc Zyngierc8481262014-12-12 10:51:24 +00002423 */
Marc Zyngier147c8f32018-05-27 16:39:55 +01002424 nr_ites = max(2, nvecs);
Marc Zyngierc8481262014-12-12 10:51:24 +00002425 sz = nr_ites * its->ite_size;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002426 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Shanker Donthineni539d3782019-01-14 09:50:19 +00002427 itt = kzalloc_node(sz, GFP_KERNEL, its->numa_node);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002428 if (alloc_lpis) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002429 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002430 if (lpi_map)
Kees Cook6396bb22018-06-12 14:03:40 -07002431 col_map = kcalloc(nr_lpis, sizeof(*col_map),
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002432 GFP_KERNEL);
2433 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07002434 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002435 nr_lpis = 0;
2436 lpi_base = 0;
2437 }
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002438
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002439 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002440 kfree(dev);
2441 kfree(itt);
2442 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01002443 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002444 return NULL;
2445 }
2446
Vladimir Murzin328191c2016-11-02 11:54:05 +00002447 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01002448
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002449 dev->its = its;
2450 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00002451 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01002452 dev->event_map.lpi_map = lpi_map;
2453 dev->event_map.col_map = col_map;
2454 dev->event_map.lpi_base = lpi_base;
2455 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00002456 mutex_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002457 dev->device_id = dev_id;
2458 INIT_LIST_HEAD(&dev->entry);
2459
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002460 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002461 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002462 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002463
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002464 /* Map device to its ITT */
2465 its_send_mapd(dev, 1);
2466
2467 return dev;
2468}
2469
2470static void its_free_device(struct its_device *its_dev)
2471{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002472 unsigned long flags;
2473
2474 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002475 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002476 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002477 kfree(its_dev->itt);
2478 kfree(its_dev);
2479}
Marc Zyngierb48ac832014-11-24 14:35:16 +00002480
Marc Zyngier8208d172019-01-18 14:08:59 +00002481static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
Marc Zyngierb48ac832014-11-24 14:35:16 +00002482{
2483 int idx;
2484
Zenghui Yu342be102019-07-27 06:14:22 +00002485 /* Find a free LPI region in lpi_map and allocate them. */
Marc Zyngier8208d172019-01-18 14:08:59 +00002486 idx = bitmap_find_free_region(dev->event_map.lpi_map,
2487 dev->event_map.nr_lpis,
2488 get_count_order(nvecs));
2489 if (idx < 0)
Marc Zyngierb48ac832014-11-24 14:35:16 +00002490 return -ENOSPC;
2491
Marc Zyngier591e5be2015-07-17 10:46:42 +01002492 *hwirq = dev->event_map.lpi_base + idx;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002493
Marc Zyngierb48ac832014-11-24 14:35:16 +00002494 return 0;
2495}
2496
Marc Zyngier54456db2015-07-28 14:46:21 +01002497static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
2498 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00002499{
Marc Zyngierb48ac832014-11-24 14:35:16 +00002500 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002501 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01002502 struct msi_domain_info *msi_info;
2503 u32 dev_id;
Marc Zyngier9791ec72019-01-29 10:02:33 +00002504 int err = 0;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002505
Marc Zyngier54456db2015-07-28 14:46:21 +01002506 /*
Julien Gralla7c90f52019-04-18 16:58:14 +01002507 * We ignore "dev" entirely, and rely on the dev_id that has
Marc Zyngier54456db2015-07-28 14:46:21 +01002508 * been passed via the scratchpad. This limits this domain's
2509 * usefulness to upper layers that definitely know that they
2510 * are built on top of the ITS.
2511 */
2512 dev_id = info->scratchpad[0].ul;
2513
2514 msi_info = msi_get_domain_info(domain);
2515 its = msi_info->data;
2516
Marc Zyngier20b3d542016-12-20 15:23:22 +00002517 if (!gic_rdists->has_direct_lpi &&
2518 vpe_proxy.dev &&
2519 vpe_proxy.dev->its == its &&
2520 dev_id == vpe_proxy.dev->device_id) {
2521 /* Bad luck. Get yourself a better implementation */
2522 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
2523 dev_id);
2524 return -EINVAL;
2525 }
2526
Marc Zyngier9791ec72019-01-29 10:02:33 +00002527 mutex_lock(&its->dev_alloc_lock);
Marc Zyngierf1304202015-07-28 14:46:18 +01002528 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002529 if (its_dev) {
2530 /*
2531 * We already have seen this ID, probably through
2532 * another alias (PCI bridge of some sort). No need to
2533 * create the device.
2534 */
Marc Zyngier9791ec72019-01-29 10:02:33 +00002535 its_dev->shared = true;
Marc Zyngierf1304202015-07-28 14:46:18 +01002536 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00002537 goto out;
2538 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002539
Marc Zyngier93f94ea2017-08-04 18:37:09 +01002540 its_dev = its_create_device(its, dev_id, nvec, true);
Marc Zyngier9791ec72019-01-29 10:02:33 +00002541 if (!its_dev) {
2542 err = -ENOMEM;
2543 goto out;
2544 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002545
Marc Zyngierf1304202015-07-28 14:46:18 +01002546 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00002547out:
Marc Zyngier9791ec72019-01-29 10:02:33 +00002548 mutex_unlock(&its->dev_alloc_lock);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002549 info->scratchpad[0].ptr = its_dev;
Marc Zyngier9791ec72019-01-29 10:02:33 +00002550 return err;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002551}
2552
Marc Zyngier54456db2015-07-28 14:46:21 +01002553static struct msi_domain_ops its_msi_domain_ops = {
2554 .msi_prepare = its_msi_prepare,
2555};
2556
Marc Zyngierb48ac832014-11-24 14:35:16 +00002557static int its_irq_gic_domain_alloc(struct irq_domain *domain,
2558 unsigned int virq,
2559 irq_hw_number_t hwirq)
2560{
Marc Zyngierf833f572015-10-13 12:51:33 +01002561 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002562
Marc Zyngierf833f572015-10-13 12:51:33 +01002563 if (irq_domain_get_of_node(domain->parent)) {
2564 fwspec.fwnode = domain->parent->fwnode;
2565 fwspec.param_count = 3;
2566 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
2567 fwspec.param[1] = hwirq;
2568 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02002569 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
2570 fwspec.fwnode = domain->parent->fwnode;
2571 fwspec.param_count = 2;
2572 fwspec.param[0] = hwirq;
2573 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01002574 } else {
2575 return -EINVAL;
2576 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00002577
Marc Zyngierf833f572015-10-13 12:51:33 +01002578 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002579}
2580
2581static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
2582 unsigned int nr_irqs, void *args)
2583{
2584 msi_alloc_info_t *info = args;
2585 struct its_device *its_dev = info->scratchpad[0].ptr;
Julien Grall35ae7df2019-05-01 14:58:21 +01002586 struct its_node *its = its_dev->its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002587 irq_hw_number_t hwirq;
2588 int err;
2589 int i;
2590
Marc Zyngier8208d172019-01-18 14:08:59 +00002591 err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
2592 if (err)
2593 return err;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002594
Julien Grall35ae7df2019-05-01 14:58:21 +01002595 err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev));
2596 if (err)
2597 return err;
2598
Marc Zyngier8208d172019-01-18 14:08:59 +00002599 for (i = 0; i < nr_irqs; i++) {
2600 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002601 if (err)
2602 return err;
2603
2604 irq_domain_set_hwirq_and_chip(domain, virq + i,
Marc Zyngier8208d172019-01-18 14:08:59 +00002605 hwirq + i, &its_irq_chip, its_dev);
Marc Zyngier0d224d32017-08-18 09:39:18 +01002606 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
Marc Zyngierf1304202015-07-28 14:46:18 +01002607 pr_debug("ID:%d pID:%d vID:%d\n",
Marc Zyngier8208d172019-01-18 14:08:59 +00002608 (int)(hwirq + i - its_dev->event_map.lpi_base),
2609 (int)(hwirq + i), virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002610 }
2611
2612 return 0;
2613}
2614
Thomas Gleixner72491642017-09-13 23:29:10 +02002615static int its_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01002616 struct irq_data *d, bool reserve)
Marc Zyngieraca268d2014-12-12 10:51:23 +00002617{
2618 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2619 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002620 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngier0d224d32017-08-18 09:39:18 +01002621 int cpu;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02002622
2623 /* get the cpu_mask of local node */
2624 if (its_dev->its->numa_node >= 0)
2625 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00002626
Marc Zyngier591e5be2015-07-17 10:46:42 +01002627 /* Bind the LPI to the first possible CPU */
Yang Yingliangc1797b12018-06-22 10:52:51 +01002628 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
2629 if (cpu >= nr_cpu_ids) {
2630 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
2631 return -EINVAL;
2632
2633 cpu = cpumask_first(cpu_online_mask);
2634 }
2635
Marc Zyngier0d224d32017-08-18 09:39:18 +01002636 its_dev->event_map.col_map[event] = cpu;
2637 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngier591e5be2015-07-17 10:46:42 +01002638
Marc Zyngieraca268d2014-12-12 10:51:23 +00002639 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00002640 its_send_mapti(its_dev, d->hwirq, event);
Thomas Gleixner72491642017-09-13 23:29:10 +02002641 return 0;
Marc Zyngieraca268d2014-12-12 10:51:23 +00002642}
2643
2644static void its_irq_domain_deactivate(struct irq_domain *domain,
2645 struct irq_data *d)
2646{
2647 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2648 u32 event = its_get_event_id(d);
2649
2650 /* Stop the delivery of interrupts */
2651 its_send_discard(its_dev, event);
2652}
2653
Marc Zyngierb48ac832014-11-24 14:35:16 +00002654static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
2655 unsigned int nr_irqs)
2656{
2657 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
2658 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier9791ec72019-01-29 10:02:33 +00002659 struct its_node *its = its_dev->its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00002660 int i;
2661
Marc Zyngierc9c96e32019-09-05 14:56:47 +01002662 bitmap_release_region(its_dev->event_map.lpi_map,
2663 its_get_event_id(irq_domain_get_irq_data(domain, virq)),
2664 get_count_order(nr_irqs));
2665
Marc Zyngierb48ac832014-11-24 14:35:16 +00002666 for (i = 0; i < nr_irqs; i++) {
2667 struct irq_data *data = irq_domain_get_irq_data(domain,
2668 virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002669 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00002670 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002671 }
2672
Marc Zyngier9791ec72019-01-29 10:02:33 +00002673 mutex_lock(&its->dev_alloc_lock);
2674
2675 /*
2676 * If all interrupts have been freed, start mopping the
2677 * floor. This is conditionned on the device not being shared.
2678 */
2679 if (!its_dev->shared &&
2680 bitmap_empty(its_dev->event_map.lpi_map,
Marc Zyngier591e5be2015-07-17 10:46:42 +01002681 its_dev->event_map.nr_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002682 its_lpi_free(its_dev->event_map.lpi_map,
2683 its_dev->event_map.lpi_base,
2684 its_dev->event_map.nr_lpis);
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00002685 kfree(its_dev->event_map.col_map);
Marc Zyngierb48ac832014-11-24 14:35:16 +00002686
2687 /* Unmap device/itt */
2688 its_send_mapd(its_dev, 0);
2689 its_free_device(its_dev);
2690 }
2691
Marc Zyngier9791ec72019-01-29 10:02:33 +00002692 mutex_unlock(&its->dev_alloc_lock);
2693
Marc Zyngierb48ac832014-11-24 14:35:16 +00002694 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
2695}
2696
2697static const struct irq_domain_ops its_domain_ops = {
2698 .alloc = its_irq_domain_alloc,
2699 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00002700 .activate = its_irq_domain_activate,
2701 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00002702};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00002703
Marc Zyngier20b3d542016-12-20 15:23:22 +00002704/*
2705 * This is insane.
2706 *
2707 * If a GICv4 doesn't implement Direct LPIs (which is extremely
2708 * likely), the only way to perform an invalidate is to use a fake
2709 * device to issue an INV command, implying that the LPI has first
2710 * been mapped to some event on that device. Since this is not exactly
2711 * cheap, we try to keep that mapping around as long as possible, and
2712 * only issue an UNMAP if we're short on available slots.
2713 *
2714 * Broken by design(tm).
2715 */
2716static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
2717{
2718 /* Already unmapped? */
2719 if (vpe->vpe_proxy_event == -1)
2720 return;
2721
2722 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
2723 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
2724
2725 /*
2726 * We don't track empty slots at all, so let's move the
2727 * next_victim pointer if we can quickly reuse that slot
2728 * instead of nuking an existing entry. Not clear that this is
2729 * always a win though, and this might just generate a ripple
2730 * effect... Let's just hope VPEs don't migrate too often.
2731 */
2732 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2733 vpe_proxy.next_victim = vpe->vpe_proxy_event;
2734
2735 vpe->vpe_proxy_event = -1;
2736}
2737
2738static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
2739{
2740 if (!gic_rdists->has_direct_lpi) {
2741 unsigned long flags;
2742
2743 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2744 its_vpe_db_proxy_unmap_locked(vpe);
2745 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2746 }
2747}
2748
2749static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
2750{
2751 /* Already mapped? */
2752 if (vpe->vpe_proxy_event != -1)
2753 return;
2754
2755 /* This slot was already allocated. Kick the other VPE out. */
2756 if (vpe_proxy.vpes[vpe_proxy.next_victim])
2757 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
2758
2759 /* Map the new VPE instead */
2760 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
2761 vpe->vpe_proxy_event = vpe_proxy.next_victim;
2762 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
2763
2764 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
2765 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
2766}
2767
Marc Zyngier958b90d2017-08-18 16:14:17 +01002768static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
2769{
2770 unsigned long flags;
2771 struct its_collection *target_col;
2772
2773 if (gic_rdists->has_direct_lpi) {
2774 void __iomem *rdbase;
2775
2776 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
2777 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2778 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2779 cpu_relax();
2780
2781 return;
2782 }
2783
2784 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2785
2786 its_vpe_db_proxy_map_locked(vpe);
2787
2788 target_col = &vpe_proxy.dev->its->collections[to];
2789 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
2790 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
2791
2792 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2793}
2794
Marc Zyngier3171a472016-12-20 15:17:28 +00002795static int its_vpe_set_affinity(struct irq_data *d,
2796 const struct cpumask *mask_val,
2797 bool force)
2798{
2799 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2800 int cpu = cpumask_first(mask_val);
2801
2802 /*
2803 * Changing affinity is mega expensive, so let's be as lazy as
Marc Zyngier20b3d542016-12-20 15:23:22 +00002804 * we can and only do it if we really have to. Also, if mapped
Marc Zyngier958b90d2017-08-18 16:14:17 +01002805 * into the proxy device, we need to move the doorbell
2806 * interrupt to its new location.
Marc Zyngier3171a472016-12-20 15:17:28 +00002807 */
2808 if (vpe->col_idx != cpu) {
Marc Zyngier958b90d2017-08-18 16:14:17 +01002809 int from = vpe->col_idx;
2810
Marc Zyngier3171a472016-12-20 15:17:28 +00002811 vpe->col_idx = cpu;
2812 its_send_vmovp(vpe);
Marc Zyngier958b90d2017-08-18 16:14:17 +01002813 its_vpe_db_proxy_move(vpe, from, cpu);
Marc Zyngier3171a472016-12-20 15:17:28 +00002814 }
2815
Marc Zyngier44c4c252017-10-19 10:11:34 +01002816 irq_data_update_effective_affinity(d, cpumask_of(cpu));
2817
Marc Zyngier3171a472016-12-20 15:17:28 +00002818 return IRQ_SET_MASK_OK_DONE;
2819}
2820
Marc Zyngiere643d802016-12-20 15:09:31 +00002821static void its_vpe_schedule(struct its_vpe *vpe)
2822{
Robin Murphy50c33092018-02-16 16:57:56 +00002823 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00002824 u64 val;
2825
2826 /* Schedule the VPE */
2827 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
2828 GENMASK_ULL(51, 12);
2829 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2830 val |= GICR_VPROPBASER_RaWb;
2831 val |= GICR_VPROPBASER_InnerShareable;
2832 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2833
2834 val = virt_to_phys(page_address(vpe->vpt_page)) &
2835 GENMASK_ULL(51, 16);
2836 val |= GICR_VPENDBASER_RaWaWb;
2837 val |= GICR_VPENDBASER_NonShareable;
2838 /*
2839 * There is no good way of finding out if the pending table is
2840 * empty as we can race against the doorbell interrupt very
2841 * easily. So in the end, vpe->pending_last is only an
2842 * indication that the vcpu has something pending, not one
2843 * that the pending table is empty. A good implementation
2844 * would be able to read its coarse map pretty quickly anyway,
2845 * making this a tolerable issue.
2846 */
2847 val |= GICR_VPENDBASER_PendingLast;
2848 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
2849 val |= GICR_VPENDBASER_Valid;
2850 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2851}
2852
2853static void its_vpe_deschedule(struct its_vpe *vpe)
2854{
Robin Murphy50c33092018-02-16 16:57:56 +00002855 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00002856 u64 val;
2857
Heyi Guo64794502019-01-24 21:37:08 +08002858 val = its_clear_vpend_valid(vlpi_base);
Marc Zyngiere643d802016-12-20 15:09:31 +00002859
Heyi Guo64794502019-01-24 21:37:08 +08002860 if (unlikely(val & GICR_VPENDBASER_Dirty)) {
Marc Zyngiere643d802016-12-20 15:09:31 +00002861 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2862 vpe->idai = false;
2863 vpe->pending_last = true;
2864 } else {
2865 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
2866 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
2867 }
2868}
2869
Marc Zyngier40619a22017-10-08 15:16:09 +01002870static void its_vpe_invall(struct its_vpe *vpe)
2871{
2872 struct its_node *its;
2873
2874 list_for_each_entry(its, &its_nodes, entry) {
2875 if (!its->is_v4)
2876 continue;
2877
Marc Zyngier2247e1b2017-10-08 18:50:36 +01002878 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
2879 continue;
2880
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01002881 /*
2882 * Sending a VINVALL to a single ITS is enough, as all
2883 * we need is to reach the redistributors.
2884 */
Marc Zyngier40619a22017-10-08 15:16:09 +01002885 its_send_vinvall(its, vpe);
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01002886 return;
Marc Zyngier40619a22017-10-08 15:16:09 +01002887 }
2888}
2889
Marc Zyngiere643d802016-12-20 15:09:31 +00002890static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2891{
2892 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2893 struct its_cmd_info *info = vcpu_info;
2894
2895 switch (info->cmd_type) {
2896 case SCHEDULE_VPE:
2897 its_vpe_schedule(vpe);
2898 return 0;
2899
2900 case DESCHEDULE_VPE:
2901 its_vpe_deschedule(vpe);
2902 return 0;
2903
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002904 case INVALL_VPE:
Marc Zyngier40619a22017-10-08 15:16:09 +01002905 its_vpe_invall(vpe);
Marc Zyngier5e2f7642016-12-20 15:10:50 +00002906 return 0;
2907
Marc Zyngiere643d802016-12-20 15:09:31 +00002908 default:
2909 return -EINVAL;
2910 }
2911}
2912
Marc Zyngier20b3d542016-12-20 15:23:22 +00002913static void its_vpe_send_cmd(struct its_vpe *vpe,
2914 void (*cmd)(struct its_device *, u32))
2915{
2916 unsigned long flags;
2917
2918 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
2919
2920 its_vpe_db_proxy_map_locked(vpe);
2921 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
2922
2923 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
2924}
2925
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002926static void its_vpe_send_inv(struct irq_data *d)
2927{
2928 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002929
Marc Zyngier20b3d542016-12-20 15:23:22 +00002930 if (gic_rdists->has_direct_lpi) {
2931 void __iomem *rdbase;
2932
2933 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2934 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_INVLPIR);
2935 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2936 cpu_relax();
2937 } else {
2938 its_vpe_send_cmd(vpe, its_send_inv);
2939 }
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002940}
2941
2942static void its_vpe_mask_irq(struct irq_data *d)
2943{
2944 /*
2945 * We need to unmask the LPI, which is described by the parent
2946 * irq_data. Instead of calling into the parent (which won't
2947 * exactly do the right thing, let's simply use the
2948 * parent_data pointer. Yes, I'm naughty.
2949 */
2950 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
2951 its_vpe_send_inv(d);
2952}
2953
2954static void its_vpe_unmask_irq(struct irq_data *d)
2955{
2956 /* Same hack as above... */
2957 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
2958 its_vpe_send_inv(d);
2959}
2960
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002961static int its_vpe_set_irqchip_state(struct irq_data *d,
2962 enum irqchip_irq_state which,
2963 bool state)
2964{
2965 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
2966
2967 if (which != IRQCHIP_STATE_PENDING)
2968 return -EINVAL;
2969
2970 if (gic_rdists->has_direct_lpi) {
2971 void __iomem *rdbase;
2972
2973 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
2974 if (state) {
2975 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
2976 } else {
2977 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2978 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
2979 cpu_relax();
2980 }
2981 } else {
2982 if (state)
2983 its_vpe_send_cmd(vpe, its_send_int);
2984 else
2985 its_vpe_send_cmd(vpe, its_send_clear);
2986 }
2987
2988 return 0;
2989}
2990
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002991static struct irq_chip its_vpe_irq_chip = {
2992 .name = "GICv4-vpe",
Marc Zyngierf6a91da2016-12-20 15:20:38 +00002993 .irq_mask = its_vpe_mask_irq,
2994 .irq_unmask = its_vpe_unmask_irq,
2995 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier3171a472016-12-20 15:17:28 +00002996 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngiere57a3e282017-07-31 14:47:24 +01002997 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
Marc Zyngiere643d802016-12-20 15:09:31 +00002998 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00002999};
3000
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003001static int its_vpe_id_alloc(void)
3002{
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05003003 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003004}
3005
3006static void its_vpe_id_free(u16 id)
3007{
3008 ida_simple_remove(&its_vpeid_ida, id);
3009}
3010
3011static int its_vpe_init(struct its_vpe *vpe)
3012{
3013 struct page *vpt_page;
3014 int vpe_id;
3015
3016 /* Allocate vpe_id */
3017 vpe_id = its_vpe_id_alloc();
3018 if (vpe_id < 0)
3019 return vpe_id;
3020
3021 /* Allocate VPT */
3022 vpt_page = its_allocate_pending_table(GFP_KERNEL);
3023 if (!vpt_page) {
3024 its_vpe_id_free(vpe_id);
3025 return -ENOMEM;
3026 }
3027
3028 if (!its_alloc_vpe_table(vpe_id)) {
3029 its_vpe_id_free(vpe_id);
Nianyao Tang34f8eb92019-07-26 17:32:57 +08003030 its_free_pending_table(vpt_page);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003031 return -ENOMEM;
3032 }
3033
3034 vpe->vpe_id = vpe_id;
3035 vpe->vpt_page = vpt_page;
Marc Zyngier20b3d542016-12-20 15:23:22 +00003036 vpe->vpe_proxy_event = -1;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003037
3038 return 0;
3039}
3040
3041static void its_vpe_teardown(struct its_vpe *vpe)
3042{
Marc Zyngier20b3d542016-12-20 15:23:22 +00003043 its_vpe_db_proxy_unmap(vpe);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003044 its_vpe_id_free(vpe->vpe_id);
3045 its_free_pending_table(vpe->vpt_page);
3046}
3047
3048static void its_vpe_irq_domain_free(struct irq_domain *domain,
3049 unsigned int virq,
3050 unsigned int nr_irqs)
3051{
3052 struct its_vm *vm = domain->host_data;
3053 int i;
3054
3055 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3056
3057 for (i = 0; i < nr_irqs; i++) {
3058 struct irq_data *data = irq_domain_get_irq_data(domain,
3059 virq + i);
3060 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
3061
3062 BUG_ON(vm != vpe->its_vm);
3063
3064 clear_bit(data->hwirq, vm->db_bitmap);
3065 its_vpe_teardown(vpe);
3066 irq_domain_reset_irq_data(data);
3067 }
3068
3069 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003070 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003071 its_free_prop_table(vm->vprop_page);
3072 }
3073}
3074
3075static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
3076 unsigned int nr_irqs, void *args)
3077{
3078 struct its_vm *vm = args;
3079 unsigned long *bitmap;
3080 struct page *vprop_page;
3081 int base, nr_ids, i, err = 0;
3082
3083 BUG_ON(!vm);
3084
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003085 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003086 if (!bitmap)
3087 return -ENOMEM;
3088
3089 if (nr_ids < nr_irqs) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003090 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003091 return -ENOMEM;
3092 }
3093
3094 vprop_page = its_allocate_prop_table(GFP_KERNEL);
3095 if (!vprop_page) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003096 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003097 return -ENOMEM;
3098 }
3099
3100 vm->db_bitmap = bitmap;
3101 vm->db_lpi_base = base;
3102 vm->nr_db_lpis = nr_ids;
3103 vm->vprop_page = vprop_page;
3104
3105 for (i = 0; i < nr_irqs; i++) {
3106 vm->vpes[i]->vpe_db_lpi = base + i;
3107 err = its_vpe_init(vm->vpes[i]);
3108 if (err)
3109 break;
3110 err = its_irq_gic_domain_alloc(domain, virq + i,
3111 vm->vpes[i]->vpe_db_lpi);
3112 if (err)
3113 break;
3114 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
3115 &its_vpe_irq_chip, vm->vpes[i]);
3116 set_bit(i, bitmap);
3117 }
3118
3119 if (err) {
3120 if (i > 0)
3121 its_vpe_irq_domain_free(domain, virq, i - 1);
3122
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003123 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003124 its_free_prop_table(vprop_page);
3125 }
3126
3127 return err;
3128}
3129
Thomas Gleixner72491642017-09-13 23:29:10 +02003130static int its_vpe_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01003131 struct irq_data *d, bool reserve)
Marc Zyngiereb781922016-12-20 14:47:05 +00003132{
3133 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier40619a22017-10-08 15:16:09 +01003134 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00003135
Marc Zyngier2247e1b2017-10-08 18:50:36 +01003136 /* If we use the list map, we issue VMAPP on demand... */
3137 if (its_list_map)
Marc Zyngier6ef930f2017-11-07 10:04:38 +00003138 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00003139
3140 /* Map the VPE to the first possible CPU */
3141 vpe->col_idx = cpumask_first(cpu_online_mask);
Marc Zyngier40619a22017-10-08 15:16:09 +01003142
3143 list_for_each_entry(its, &its_nodes, entry) {
3144 if (!its->is_v4)
3145 continue;
3146
Marc Zyngier75fd9512017-10-08 18:46:39 +01003147 its_send_vmapp(its, vpe, true);
Marc Zyngier40619a22017-10-08 15:16:09 +01003148 its_send_vinvall(its, vpe);
3149 }
3150
Marc Zyngier44c4c252017-10-19 10:11:34 +01003151 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
3152
Thomas Gleixner72491642017-09-13 23:29:10 +02003153 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00003154}
3155
3156static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
3157 struct irq_data *d)
3158{
3159 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier75fd9512017-10-08 18:46:39 +01003160 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00003161
Marc Zyngier2247e1b2017-10-08 18:50:36 +01003162 /*
3163 * If we use the list map, we unmap the VPE once no VLPIs are
3164 * associated with the VM.
3165 */
3166 if (its_list_map)
3167 return;
3168
Marc Zyngier75fd9512017-10-08 18:46:39 +01003169 list_for_each_entry(its, &its_nodes, entry) {
3170 if (!its->is_v4)
3171 continue;
3172
3173 its_send_vmapp(its, vpe, false);
3174 }
Marc Zyngiereb781922016-12-20 14:47:05 +00003175}
3176
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003177static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003178 .alloc = its_vpe_irq_domain_alloc,
3179 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00003180 .activate = its_vpe_irq_domain_activate,
3181 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003182};
3183
Yun Wu4559fbb2015-03-06 16:37:50 +00003184static int its_force_quiescent(void __iomem *base)
3185{
3186 u32 count = 1000000; /* 1s */
3187 u32 val;
3188
3189 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07003190 /*
3191 * GIC architecture specification requires the ITS to be both
3192 * disabled and quiescent for writes to GITS_BASER<n> or
3193 * GITS_CBASER to not have UNPREDICTABLE results.
3194 */
3195 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00003196 return 0;
3197
3198 /* Disable the generation of all interrupts to this ITS */
Marc Zyngierd51c4b42017-06-27 21:24:25 +01003199 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
Yun Wu4559fbb2015-03-06 16:37:50 +00003200 writel_relaxed(val, base + GITS_CTLR);
3201
3202 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
3203 while (1) {
3204 val = readl_relaxed(base + GITS_CTLR);
3205 if (val & GITS_CTLR_QUIESCENT)
3206 return 0;
3207
3208 count--;
3209 if (!count)
3210 return -EBUSY;
3211
3212 cpu_relax();
3213 udelay(1);
3214 }
3215}
3216
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003217static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
Robert Richter94100972015-09-21 22:58:38 +02003218{
3219 struct its_node *its = data;
3220
Ard Biesheuvelfa150012017-10-17 17:55:54 +01003221 /* erratum 22375: only alloc 8MB table size */
3222 its->device_ids = 0x14; /* 20 bits, 8MB */
Robert Richter94100972015-09-21 22:58:38 +02003223 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003224
3225 return true;
Robert Richter94100972015-09-21 22:58:38 +02003226}
3227
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003228static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003229{
3230 struct its_node *its = data;
3231
3232 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003233
3234 return true;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003235}
3236
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003237static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
Shanker Donthineni90922a22017-03-07 08:20:38 -06003238{
3239 struct its_node *its = data;
3240
3241 /* On QDF2400, the size of the ITE is 16Bytes */
3242 its->ite_size = 16;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01003243
3244 return true;
Shanker Donthineni90922a22017-03-07 08:20:38 -06003245}
3246
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003247static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
3248{
3249 struct its_node *its = its_dev->its;
3250
3251 /*
3252 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
3253 * which maps 32-bit writes targeted at a separate window of
3254 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
3255 * with device ID taken from bits [device_id_bits + 1:2] of
3256 * the window offset.
3257 */
3258 return its->pre_its_base + (its_dev->device_id << 2);
3259}
3260
3261static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
3262{
3263 struct its_node *its = data;
3264 u32 pre_its_window[2];
3265 u32 ids;
3266
3267 if (!fwnode_property_read_u32_array(its->fwnode_handle,
3268 "socionext,synquacer-pre-its",
3269 pre_its_window,
3270 ARRAY_SIZE(pre_its_window))) {
3271
3272 its->pre_its_base = pre_its_window[0];
3273 its->get_msi_base = its_irq_get_msi_base_pre_its;
3274
3275 ids = ilog2(pre_its_window[1]) - 2;
3276 if (its->device_ids > ids)
3277 its->device_ids = ids;
3278
3279 /* the pre-ITS breaks isolation, so disable MSI remapping */
3280 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
3281 return true;
3282 }
3283 return false;
3284}
3285
Marc Zyngier5c9a8822017-07-28 21:20:37 +01003286static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
3287{
3288 struct its_node *its = data;
3289
3290 /*
3291 * Hip07 insists on using the wrong address for the VLPI
3292 * page. Trick it into doing the right thing...
3293 */
3294 its->vlpi_redist_offset = SZ_128K;
3295 return true;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00003296}
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003297
Robert Richter67510cc2015-09-21 22:58:37 +02003298static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02003299#ifdef CONFIG_CAVIUM_ERRATUM_22375
3300 {
3301 .desc = "ITS: Cavium errata 22375, 24313",
3302 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3303 .mask = 0xffff0fff,
3304 .init = its_enable_quirk_cavium_22375,
3305 },
3306#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003307#ifdef CONFIG_CAVIUM_ERRATUM_23144
3308 {
3309 .desc = "ITS: Cavium erratum 23144",
3310 .iidr = 0xa100034c, /* ThunderX pass 1.x */
3311 .mask = 0xffff0fff,
3312 .init = its_enable_quirk_cavium_23144,
3313 },
3314#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06003315#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
3316 {
3317 .desc = "ITS: QDF2400 erratum 0065",
3318 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
3319 .mask = 0xffffffff,
3320 .init = its_enable_quirk_qdf2400_e0065,
3321 },
3322#endif
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003323#ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
3324 {
3325 /*
3326 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
3327 * implementation, but with a 'pre-ITS' added that requires
3328 * special handling in software.
3329 */
3330 .desc = "ITS: Socionext Synquacer pre-ITS",
3331 .iidr = 0x0001143b,
3332 .mask = 0xffffffff,
3333 .init = its_enable_quirk_socionext_synquacer,
3334 },
3335#endif
Marc Zyngier5c9a8822017-07-28 21:20:37 +01003336#ifdef CONFIG_HISILICON_ERRATUM_161600802
3337 {
3338 .desc = "ITS: Hip07 erratum 161600802",
3339 .iidr = 0x00000004,
3340 .mask = 0xffffffff,
3341 .init = its_enable_quirk_hip07_161600802,
3342 },
3343#endif
Robert Richter67510cc2015-09-21 22:58:37 +02003344 {
3345 }
3346};
3347
3348static void its_enable_quirks(struct its_node *its)
3349{
3350 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
3351
3352 gic_enable_quirks(iidr, its_quirks, its);
3353}
3354
Derek Basehoredba0bc72018-02-28 21:48:18 -08003355static int its_save_disable(void)
3356{
3357 struct its_node *its;
3358 int err = 0;
3359
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003360 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003361 list_for_each_entry(its, &its_nodes, entry) {
3362 void __iomem *base;
3363
3364 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3365 continue;
3366
3367 base = its->base;
3368 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
3369 err = its_force_quiescent(base);
3370 if (err) {
3371 pr_err("ITS@%pa: failed to quiesce: %d\n",
3372 &its->phys_base, err);
3373 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3374 goto err;
3375 }
3376
3377 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
3378 }
3379
3380err:
3381 if (err) {
3382 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
3383 void __iomem *base;
3384
3385 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3386 continue;
3387
3388 base = its->base;
3389 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
3390 }
3391 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003392 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003393
3394 return err;
3395}
3396
3397static void its_restore_enable(void)
3398{
3399 struct its_node *its;
3400 int ret;
3401
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003402 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003403 list_for_each_entry(its, &its_nodes, entry) {
3404 void __iomem *base;
3405 int i;
3406
3407 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
3408 continue;
3409
3410 base = its->base;
3411
3412 /*
3413 * Make sure that the ITS is disabled. If it fails to quiesce,
3414 * don't restore it since writing to CBASER or BASER<n>
3415 * registers is undefined according to the GIC v3 ITS
3416 * Specification.
3417 */
3418 ret = its_force_quiescent(base);
3419 if (ret) {
3420 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
3421 &its->phys_base, ret);
3422 continue;
3423 }
3424
3425 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
3426
3427 /*
3428 * Writing CBASER resets CREADR to 0, so make CWRITER and
3429 * cmd_write line up with it.
3430 */
3431 its->cmd_write = its->cmd_base;
3432 gits_write_cwriter(0, base + GITS_CWRITER);
3433
3434 /* Restore GITS_BASER from the value cache. */
3435 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
3436 struct its_baser *baser = &its->tables[i];
3437
3438 if (!(baser->val & GITS_BASER_VALID))
3439 continue;
3440
3441 its_write_baser(its, baser, baser->val);
3442 }
3443 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
Derek Basehore920181c2018-02-28 21:48:20 -08003444
3445 /*
3446 * Reinit the collection if it's stored in the ITS. This is
3447 * indicated by the col_id being less than the HCC field.
3448 * CID < HCC as specified in the GIC v3 Documentation.
3449 */
3450 if (its->collections[smp_processor_id()].col_id <
3451 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
3452 its_cpu_init_collection(its);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003453 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003454 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08003455}
3456
3457static struct syscore_ops its_syscore_ops = {
3458 .suspend = its_save_disable,
3459 .resume = its_restore_enable,
3460};
3461
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003462static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003463{
3464 struct irq_domain *inner_domain;
3465 struct msi_domain_info *info;
3466
3467 info = kzalloc(sizeof(*info), GFP_KERNEL);
3468 if (!info)
3469 return -ENOMEM;
3470
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003471 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003472 if (!inner_domain) {
3473 kfree(info);
3474 return -ENOMEM;
3475 }
3476
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003477 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01003478 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003479 inner_domain->flags |= its->msi_domain_flags;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003480 info->ops = &its_msi_domain_ops;
3481 info->data = its;
3482 inner_domain->host_data = info;
3483
3484 return 0;
3485}
3486
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003487static int its_init_vpe_domain(void)
3488{
Marc Zyngier20b3d542016-12-20 15:23:22 +00003489 struct its_node *its;
3490 u32 devid;
3491 int entries;
3492
3493 if (gic_rdists->has_direct_lpi) {
3494 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
3495 return 0;
3496 }
3497
3498 /* Any ITS will do, even if not v4 */
3499 its = list_first_entry(&its_nodes, struct its_node, entry);
3500
3501 entries = roundup_pow_of_two(nr_cpu_ids);
Kees Cook6396bb22018-06-12 14:03:40 -07003502 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
Marc Zyngier20b3d542016-12-20 15:23:22 +00003503 GFP_KERNEL);
3504 if (!vpe_proxy.vpes) {
3505 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
3506 return -ENOMEM;
3507 }
3508
3509 /* Use the last possible DevID */
3510 devid = GENMASK(its->device_ids - 1, 0);
3511 vpe_proxy.dev = its_create_device(its, devid, entries, false);
3512 if (!vpe_proxy.dev) {
3513 kfree(vpe_proxy.vpes);
3514 pr_err("ITS: Can't allocate GICv4 proxy device\n");
3515 return -ENOMEM;
3516 }
3517
Shanker Donthinenic427a472017-09-23 13:50:19 -05003518 BUG_ON(entries > vpe_proxy.dev->nr_ites);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003519
3520 raw_spin_lock_init(&vpe_proxy.lock);
3521 vpe_proxy.next_victim = 0;
3522 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
3523 devid, vpe_proxy.dev->nr_ites);
3524
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003525 return 0;
3526}
3527
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003528static int __init its_compute_its_list_map(struct resource *res,
3529 void __iomem *its_base)
3530{
3531 int its_number;
3532 u32 ctlr;
3533
3534 /*
3535 * This is assumed to be done early enough that we're
3536 * guaranteed to be single-threaded, hence no
3537 * locking. Should this change, we should address
3538 * this.
3539 */
Marc Zyngierab604912017-10-08 18:48:06 +01003540 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
3541 if (its_number >= GICv4_ITS_LIST_MAX) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003542 pr_err("ITS@%pa: No ITSList entry available!\n",
3543 &res->start);
3544 return -EINVAL;
3545 }
3546
3547 ctlr = readl_relaxed(its_base + GITS_CTLR);
3548 ctlr &= ~GITS_CTLR_ITS_NUMBER;
3549 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
3550 writel_relaxed(ctlr, its_base + GITS_CTLR);
3551 ctlr = readl_relaxed(its_base + GITS_CTLR);
3552 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
3553 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
3554 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
3555 }
3556
3557 if (test_and_set_bit(its_number, &its_list_map)) {
3558 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
3559 &res->start, its_number);
3560 return -EINVAL;
3561 }
3562
3563 return its_number;
3564}
3565
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003566static int __init its_probe_one(struct resource *res,
3567 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003568{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003569 struct its_node *its;
3570 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003571 u32 val, ctlr;
3572 u64 baser, tmp, typer;
Shanker Donthineni539d3782019-01-14 09:50:19 +00003573 struct page *page;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003574 int err;
3575
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003576 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003577 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003578 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003579 return -ENOMEM;
3580 }
3581
3582 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
3583 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003584 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003585 err = -ENODEV;
3586 goto out_unmap;
3587 }
3588
Yun Wu4559fbb2015-03-06 16:37:50 +00003589 err = its_force_quiescent(its_base);
3590 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003591 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00003592 goto out_unmap;
3593 }
3594
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003595 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003596
3597 its = kzalloc(sizeof(*its), GFP_KERNEL);
3598 if (!its) {
3599 err = -ENOMEM;
3600 goto out_unmap;
3601 }
3602
3603 raw_spin_lock_init(&its->lock);
Marc Zyngier9791ec72019-01-29 10:02:33 +00003604 mutex_init(&its->dev_alloc_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003605 INIT_LIST_HEAD(&its->entry);
3606 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003607 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003608 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003609 its->phys_base = res->start;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003610 its->ite_size = GITS_TYPER_ITT_ENTRY_SIZE(typer);
Ard Biesheuvelfa150012017-10-17 17:55:54 +01003611 its->device_ids = GITS_TYPER_DEVBITS(typer);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003612 its->is_v4 = !!(typer & GITS_TYPER_VLPIS);
3613 if (its->is_v4) {
3614 if (!(typer & GITS_TYPER_VMOVP)) {
3615 err = its_compute_its_list_map(res, its_base);
3616 if (err < 0)
3617 goto out_free_its;
3618
Marc Zyngierdebf6d02017-10-08 18:44:42 +01003619 its->list_nr = err;
3620
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003621 pr_info("ITS@%pa: Using ITS number %d\n",
3622 &res->start, err);
3623 } else {
3624 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
3625 }
3626 }
3627
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003628 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003629
Shanker Donthineni539d3782019-01-14 09:50:19 +00003630 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
3631 get_order(ITS_CMD_QUEUE_SZ));
3632 if (!page) {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003633 err = -ENOMEM;
3634 goto out_free_its;
3635 }
Shanker Donthineni539d3782019-01-14 09:50:19 +00003636 its->cmd_base = (void *)page_address(page);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003637 its->cmd_write = its->cmd_base;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01003638 its->fwnode_handle = handle;
3639 its->get_msi_base = its_irq_get_msi_base;
3640 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003641
Robert Richter67510cc2015-09-21 22:58:37 +02003642 its_enable_quirks(its);
3643
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05003644 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003645 if (err)
3646 goto out_free_cmd;
3647
3648 err = its_alloc_collections(its);
3649 if (err)
3650 goto out_free_tables;
3651
3652 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06003653 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003654 GITS_CBASER_InnerShareable |
3655 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
3656 GITS_CBASER_VALID);
3657
Vladimir Murzin0968a612016-11-02 11:54:06 +00003658 gits_write_cbaser(baser, its->base + GITS_CBASER);
3659 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003660
Marc Zyngier4ad3e362015-03-27 14:15:04 +00003661 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00003662 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
3663 /*
3664 * The HW reports non-shareable, we must
3665 * remove the cacheability attributes as
3666 * well.
3667 */
3668 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
3669 GITS_CBASER_CACHEABILITY_MASK);
3670 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00003671 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00003672 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003673 pr_info("ITS: using cache flushing for cmd queue\n");
3674 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
3675 }
3676
Vladimir Murzin0968a612016-11-02 11:54:06 +00003677 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00003678 ctlr = readl_relaxed(its->base + GITS_CTLR);
Marc Zyngierd51c4b42017-06-27 21:24:25 +01003679 ctlr |= GITS_CTLR_ENABLE;
3680 if (its->is_v4)
3681 ctlr |= GITS_CTLR_ImDe;
3682 writel_relaxed(ctlr, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00003683
Derek Basehoredba0bc72018-02-28 21:48:18 -08003684 if (GITS_TYPER_HCC(typer))
3685 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
3686
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003687 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003688 if (err)
3689 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003690
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003691 raw_spin_lock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003692 list_add(&its->entry, &its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003693 raw_spin_unlock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003694
3695 return 0;
3696
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003697out_free_tables:
3698 its_free_tables(its);
3699out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01003700 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003701out_free_its:
3702 kfree(its);
3703out_unmap:
3704 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003705 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003706 return err;
3707}
3708
3709static bool gic_rdists_supports_plpis(void)
3710{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01003711 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003712}
3713
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003714static int redist_disable_lpis(void)
3715{
3716 void __iomem *rbase = gic_data_rdist_rd_base();
3717 u64 timeout = USEC_PER_SEC;
3718 u64 val;
3719
3720 if (!gic_rdists_supports_plpis()) {
3721 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
3722 return -ENXIO;
3723 }
3724
3725 val = readl_relaxed(rbase + GICR_CTLR);
3726 if (!(val & GICR_CTLR_ENABLE_LPIS))
3727 return 0;
3728
Marc Zyngier11e37d32018-07-27 13:38:54 +01003729 /*
3730 * If coming via a CPU hotplug event, we don't need to disable
3731 * LPIs before trying to re-enable them. They are already
3732 * configured and all is well in the world.
Marc Zyngierc440a9d2018-07-27 15:40:13 +01003733 *
3734 * If running with preallocated tables, there is nothing to do.
Marc Zyngier11e37d32018-07-27 13:38:54 +01003735 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01003736 if (gic_data_rdist()->lpi_enabled ||
3737 (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED))
Marc Zyngier11e37d32018-07-27 13:38:54 +01003738 return 0;
3739
3740 /*
3741 * From that point on, we only try to do some damage control.
3742 */
3743 pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003744 smp_processor_id());
3745 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
3746
3747 /* Disable LPIs */
3748 val &= ~GICR_CTLR_ENABLE_LPIS;
3749 writel_relaxed(val, rbase + GICR_CTLR);
3750
3751 /* Make sure any change to GICR_CTLR is observable by the GIC */
3752 dsb(sy);
3753
3754 /*
3755 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
3756 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
3757 * Error out if we time out waiting for RWP to clear.
3758 */
3759 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
3760 if (!timeout) {
3761 pr_err("CPU%d: Timeout while disabling LPIs\n",
3762 smp_processor_id());
3763 return -ETIMEDOUT;
3764 }
3765 udelay(1);
3766 timeout--;
3767 }
3768
3769 /*
3770 * After it has been written to 1, it is IMPLEMENTATION
3771 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
3772 * cleared to 0. Error out if clearing the bit failed.
3773 */
3774 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
3775 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
3776 return -EBUSY;
3777 }
3778
3779 return 0;
3780}
3781
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003782int its_cpu_init(void)
3783{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003784 if (!list_empty(&its_nodes)) {
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05003785 int ret;
3786
3787 ret = redist_disable_lpis();
3788 if (ret)
3789 return ret;
3790
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003791 its_cpu_init_lpis();
Derek Basehore920181c2018-02-28 21:48:20 -08003792 its_cpu_init_collections();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003793 }
3794
3795 return 0;
3796}
3797
Arvind Yadav935bba72017-06-22 16:05:30 +05303798static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003799 { .compatible = "arm,gic-v3-its", },
3800 {},
3801};
3802
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003803static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003804{
3805 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003806 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003807
3808 for (np = of_find_matching_node(node, its_device_id); np;
3809 np = of_find_matching_node(np, its_device_id)) {
Stephen Boyd95a25622018-02-01 09:03:29 -08003810 if (!of_device_is_available(np))
3811 continue;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003812 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003813 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
3814 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02003815 continue;
3816 }
3817
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003818 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05003819 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003820 continue;
3821 }
3822
3823 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003824 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003825 return 0;
3826}
3827
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003828#ifdef CONFIG_ACPI
3829
3830#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
3831
Robert Richterd1ce2632017-07-12 15:25:09 +02003832#ifdef CONFIG_ACPI_NUMA
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303833struct its_srat_map {
3834 /* numa node id */
3835 u32 numa_node;
3836 /* GIC ITS ID */
3837 u32 its_id;
3838};
3839
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003840static struct its_srat_map *its_srat_maps __initdata;
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303841static int its_in_srat __initdata;
3842
3843static int __init acpi_get_its_numa_node(u32 its_id)
3844{
3845 int i;
3846
3847 for (i = 0; i < its_in_srat; i++) {
3848 if (its_id == its_srat_maps[i].its_id)
3849 return its_srat_maps[i].numa_node;
3850 }
3851 return NUMA_NO_NODE;
3852}
3853
Keith Busch60574d12019-03-11 14:55:57 -06003854static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header,
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003855 const unsigned long end)
3856{
3857 return 0;
3858}
3859
Keith Busch60574d12019-03-11 14:55:57 -06003860static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header,
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303861 const unsigned long end)
3862{
3863 int node;
3864 struct acpi_srat_gic_its_affinity *its_affinity;
3865
3866 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
3867 if (!its_affinity)
3868 return -EINVAL;
3869
3870 if (its_affinity->header.length < sizeof(*its_affinity)) {
3871 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
3872 its_affinity->header.length);
3873 return -EINVAL;
3874 }
3875
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303876 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
3877
3878 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
3879 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
3880 return 0;
3881 }
3882
3883 its_srat_maps[its_in_srat].numa_node = node;
3884 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
3885 its_in_srat++;
3886 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
3887 its_affinity->proximity_domain, its_affinity->its_id, node);
3888
3889 return 0;
3890}
3891
3892static void __init acpi_table_parse_srat_its(void)
3893{
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003894 int count;
3895
3896 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
3897 sizeof(struct acpi_table_srat),
3898 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3899 gic_acpi_match_srat_its, 0);
3900 if (count <= 0)
3901 return;
3902
Kees Cook6da2ec52018-06-12 13:55:00 -07003903 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
3904 GFP_KERNEL);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003905 if (!its_srat_maps) {
3906 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
3907 return;
3908 }
3909
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303910 acpi_table_parse_entries(ACPI_SIG_SRAT,
3911 sizeof(struct acpi_table_srat),
3912 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
3913 gic_acpi_parse_srat_its, 0);
3914}
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003915
3916/* free the its_srat_maps after ITS probing */
3917static void __init acpi_its_srat_maps_free(void)
3918{
3919 kfree(its_srat_maps);
3920}
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303921#else
3922static void __init acpi_table_parse_srat_its(void) { }
3923static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003924static void __init acpi_its_srat_maps_free(void) { }
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303925#endif
3926
Keith Busch60574d12019-03-11 14:55:57 -06003927static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header,
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003928 const unsigned long end)
3929{
3930 struct acpi_madt_generic_translator *its_entry;
3931 struct fwnode_handle *dom_handle;
3932 struct resource res;
3933 int err;
3934
3935 its_entry = (struct acpi_madt_generic_translator *)header;
3936 memset(&res, 0, sizeof(res));
3937 res.start = its_entry->base_address;
3938 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
3939 res.flags = IORESOURCE_MEM;
3940
Marc Zyngier5778cc72019-07-31 16:13:42 +01003941 dom_handle = irq_domain_alloc_fwnode(&res.start);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003942 if (!dom_handle) {
3943 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
3944 &res.start);
3945 return -ENOMEM;
3946 }
3947
Shameer Kolothum8b4282e2018-02-13 15:20:50 +00003948 err = iort_register_domain_token(its_entry->translation_id, res.start,
3949 dom_handle);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003950 if (err) {
3951 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
3952 &res.start, its_entry->translation_id);
3953 goto dom_err;
3954 }
3955
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303956 err = its_probe_one(&res, dom_handle,
3957 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003958 if (!err)
3959 return 0;
3960
3961 iort_deregister_domain_token(its_entry->translation_id);
3962dom_err:
3963 irq_domain_free_fwnode(dom_handle);
3964 return err;
3965}
3966
3967static void __init its_acpi_probe(void)
3968{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05303969 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003970 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
3971 gic_acpi_parse_madt_its, 0);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08003972 acpi_its_srat_maps_free();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003973}
3974#else
3975static void __init its_acpi_probe(void) { }
3976#endif
3977
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003978int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
3979 struct irq_domain *parent_domain)
3980{
3981 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003982 struct its_node *its;
3983 bool has_v4 = false;
3984 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02003985
3986 its_parent = parent_domain;
3987 of_node = to_of_node(handle);
3988 if (of_node)
3989 its_of_probe(of_node);
3990 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003991 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003992
3993 if (list_empty(&its_nodes)) {
3994 pr_warn("ITS: No ITS available, not enabling LPIs\n");
3995 return -ENXIO;
3996 }
3997
3998 gic_rdists = rdists;
Marc Zyngier11e37d32018-07-27 13:38:54 +01003999
4000 err = allocate_lpi_tables();
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004001 if (err)
4002 return err;
4003
4004 list_for_each_entry(its, &its_nodes, entry)
4005 has_v4 |= its->is_v4;
4006
4007 if (has_v4 & rdists->has_vlpis) {
Marc Zyngier3d63cb52016-12-20 15:31:54 +00004008 if (its_init_vpe_domain() ||
4009 its_init_v4(parent_domain, &its_vpe_domain_ops)) {
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004010 rdists->has_vlpis = false;
4011 pr_err("ITS: Disabling GICv4 support\n");
4012 }
4013 }
4014
Derek Basehoredba0bc72018-02-28 21:48:18 -08004015 register_syscore_ops(&its_syscore_ops);
4016
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004017 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004018}