blob: bcc1a0957cda099ccf3533d9546e1e1207c4a673 [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 Zyngierffedbf02019-11-08 16:57:59 +00009#include <linux/bitfield.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000010#include <linux/bitmap.h>
11#include <linux/cpu.h>
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +010012#include <linux/crash_dump.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000013#include <linux/delay.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010014#include <linux/dma-iommu.h>
Marc Zyngier3fb68fa2018-07-27 16:21:18 +010015#include <linux/efi.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000016#include <linux/interrupt.h>
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +020017#include <linux/irqdomain.h>
Marc Zyngier880cb3c2018-05-27 16:14:15 +010018#include <linux/list.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000019#include <linux/log2.h>
Marc Zyngier5e2c9f92018-07-27 16:23:18 +010020#include <linux/memblock.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000021#include <linux/mm.h>
22#include <linux/msi.h>
23#include <linux/of.h>
24#include <linux/of_address.h>
25#include <linux/of_irq.h>
26#include <linux/of_pci.h>
27#include <linux/of_platform.h>
28#include <linux/percpu.h>
29#include <linux/slab.h>
Derek Basehoredba0bc72018-02-28 21:48:18 -080030#include <linux/syscore_ops.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000031
Joel Porquet41a83e062015-07-07 17:11:46 -040032#include <linux/irqchip.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000033#include <linux/irqchip/arm-gic-v3.h>
Marc Zyngierc808eea2016-12-20 09:31:20 +000034#include <linux/irqchip/arm-gic-v4.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000035
Marc Zyngiercc2d3212014-11-24 14:35:11 +000036#include <asm/cputype.h>
37#include <asm/exception.h>
38
Robert Richter67510cc2015-09-21 22:58:37 +020039#include "irq-gic-common.h"
40
Robert Richter94100972015-09-21 22:58:38 +020041#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
42#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020043#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
Derek Basehoredba0bc72018-02-28 21:48:18 -080044#define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3)
Marc Zyngiercc2d3212014-11-24 14:35:11 +000045
Marc Zyngierc48ed512014-11-24 14:35:12 +000046#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
Marc Zyngierc440a9d2018-07-27 15:40:13 +010047#define RDIST_FLAGS_RD_TABLES_PREALLOCATED (1 << 1)
Marc Zyngierc48ed512014-11-24 14:35:12 +000048
Marc Zyngiera13b0402016-12-19 17:15:24 +000049static u32 lpi_id_bits;
50
51/*
52 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
53 * deal with (one configuration byte per interrupt). PENDBASE has to
54 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
55 */
56#define LPI_NRBITS lpi_id_bits
57#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
58#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
59
Julien Thierry2130b782018-08-28 16:51:18 +010060#define LPI_PROP_DEFAULT_PRIO GICD_INT_DEF_PRI
Marc Zyngiera13b0402016-12-19 17:15:24 +000061
Marc Zyngiercc2d3212014-11-24 14:35:11 +000062/*
63 * Collection structure - just an ID, and a redistributor address to
64 * ping. We use one per CPU as a bag of interrupts assigned to this
65 * CPU.
66 */
67struct its_collection {
68 u64 target_address;
69 u16 col_id;
70};
71
72/*
Shanker Donthineni93473592016-06-06 18:17:30 -050073 * The ITS_BASER structure - contains memory information, cached
74 * value of BASER register configuration and ITS page size.
Shanker Donthineni466b7d12016-03-09 22:10:49 -060075 */
76struct its_baser {
77 void *base;
78 u64 val;
79 u32 order;
Shanker Donthineni93473592016-06-06 18:17:30 -050080 u32 psz;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060081};
82
Ard Biesheuvel558b0162017-10-17 17:55:56 +010083struct its_device;
84
Shanker Donthineni466b7d12016-03-09 22:10:49 -060085/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000086 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010087 * top-level MSI domain, the command queue, the collections, and the
88 * list of devices writing to it.
Marc Zyngier9791ec72019-01-29 10:02:33 +000089 *
90 * dev_alloc_lock has to be taken for device allocations, while the
91 * spinlock must be taken to parse data structures such as the device
92 * list.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000093 */
94struct its_node {
95 raw_spinlock_t lock;
Marc Zyngier9791ec72019-01-29 10:02:33 +000096 struct mutex dev_alloc_lock;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000097 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000098 void __iomem *base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +020099 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000100 struct its_cmd_block *cmd_base;
101 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600102 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000103 struct its_collection *collections;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100104 struct fwnode_handle *fwnode_handle;
105 u64 (*get_msi_base)(struct its_device *its_dev);
Marc Zyngier0dd57fe2019-11-08 16:57:58 +0000106 u64 typer;
Derek Basehoredba0bc72018-02-28 21:48:18 -0800107 u64 cbaser_save;
108 u32 ctlr_save;
Marc Zyngier5e516842019-12-24 11:10:28 +0000109 u32 mpidr;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000110 struct list_head its_device_list;
111 u64 flags;
Marc Zyngierdebf6d02017-10-08 18:44:42 +0100112 unsigned long list_nr;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200113 int numa_node;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100114 unsigned int msi_domain_flags;
115 u32 pre_its_base; /* for Socionext Synquacer */
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100116 int vlpi_redist_offset;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000117};
118
Marc Zyngier0dd57fe2019-11-08 16:57:58 +0000119#define is_v4(its) (!!((its)->typer & GITS_TYPER_VLPIS))
Marc Zyngier5e516842019-12-24 11:10:28 +0000120#define is_v4_1(its) (!!((its)->typer & GITS_TYPER_VMAPP))
Marc Zyngier576a8342019-11-08 16:58:00 +0000121#define device_ids(its) (FIELD_GET(GITS_TYPER_DEVBITS, (its)->typer) + 1)
Marc Zyngier0dd57fe2019-11-08 16:57:58 +0000122
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000123#define ITS_ITT_ALIGN SZ_256
124
Shanker Donthineni32bd44d2017-10-07 15:43:48 -0500125/* The maximum number of VPEID bits supported by VLPI commands */
Marc Zyngierf2d83402019-12-24 11:10:25 +0000126#define ITS_MAX_VPEID_BITS \
127 ({ \
128 int nvpeid = 16; \
129 if (gic_rdists->has_rvpeid && \
130 gic_rdists->gicd_typer2 & GICD_TYPER2_VIL) \
131 nvpeid = 1 + (gic_rdists->gicd_typer2 & \
132 GICD_TYPER2_VID); \
133 \
134 nvpeid; \
135 })
Shanker Donthineni32bd44d2017-10-07 15:43:48 -0500136#define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
137
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600138/* Convert page order to size in bytes */
139#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
140
Marc Zyngier591e5be2015-07-17 10:46:42 +0100141struct event_lpi_map {
142 unsigned long *lpi_map;
143 u16 *col_map;
144 irq_hw_number_t lpi_base;
145 int nr_lpis;
Marc Zyngier11635fa2019-11-08 16:58:05 +0000146 raw_spinlock_t vlpi_lock;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000147 struct its_vm *vm;
148 struct its_vlpi_map *vlpi_maps;
149 int nr_vlpis;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100150};
151
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000152/*
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000153 * The ITS view of a device - belongs to an ITS, owns an interrupt
154 * translation table, and a list of interrupts. If it some of its
155 * LPIs are injected into a guest (GICv4), the event_map.vm field
156 * indicates which one.
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000157 */
158struct its_device {
159 struct list_head entry;
160 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100161 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000162 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000163 u32 nr_ites;
164 u32 device_id;
Marc Zyngier9791ec72019-01-29 10:02:33 +0000165 bool shared;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000166};
167
Marc Zyngier20b3d542016-12-20 15:23:22 +0000168static struct {
169 raw_spinlock_t lock;
170 struct its_device *dev;
171 struct its_vpe **vpes;
172 int next_victim;
173} vpe_proxy;
174
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000175static LIST_HEAD(its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +0200176static DEFINE_RAW_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000177static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200178static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000179
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000180static unsigned long its_list_map;
Marc Zyngier3171a472016-12-20 15:17:28 +0000181static u16 vmovp_seq_num;
182static DEFINE_RAW_SPINLOCK(vmovp_lock);
183
Marc Zyngier7d75bbb2016-12-20 13:55:54 +0000184static DEFINE_IDA(its_vpeid_ida);
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000185
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000186#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
Marc Zyngier11e37d32018-07-27 13:38:54 +0100187#define gic_data_rdist_cpu(cpu) (per_cpu_ptr(gic_rdists->rdist, cpu))
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000188#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngiere643d802016-12-20 15:09:31 +0000189#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000190
Zenghui Yu84243122019-10-23 03:46:26 +0000191static u16 get_its_list(struct its_vm *vm)
192{
193 struct its_node *its;
194 unsigned long its_list = 0;
195
196 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +0000197 if (!is_v4(its))
Zenghui Yu84243122019-10-23 03:46:26 +0000198 continue;
199
200 if (vm->vlpi_count[its->list_nr])
201 __set_bit(its->list_nr, &its_list);
202 }
203
204 return (u16)its_list;
205}
206
Marc Zyngier425c09b2019-11-08 16:57:57 +0000207static inline u32 its_get_event_id(struct irq_data *d)
208{
209 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
210 return d->hwirq - its_dev->event_map.lpi_base;
211}
212
Marc Zyngier591e5be2015-07-17 10:46:42 +0100213static struct its_collection *dev_event_to_col(struct its_device *its_dev,
214 u32 event)
215{
216 struct its_node *its = its_dev->its;
217
218 return its->collections + its_dev->event_map.col_map[event];
219}
220
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +0000221static struct its_vlpi_map *dev_event_to_vlpi_map(struct its_device *its_dev,
222 u32 event)
223{
224 if (WARN_ON_ONCE(event >= its_dev->event_map.nr_lpis))
225 return NULL;
226
227 return &its_dev->event_map.vlpi_maps[event];
228}
229
Marc Zyngierf4a81f52019-12-24 11:10:38 +0000230static struct its_vlpi_map *get_vlpi_map(struct irq_data *d)
231{
232 if (irqd_is_forwarded_to_vcpu(d)) {
233 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
234 u32 event = its_get_event_id(d);
235
236 return dev_event_to_vlpi_map(its_dev, event);
237 }
238
239 return NULL;
240}
241
Marc Zyngierf3a059212020-03-04 20:33:10 +0000242static int vpe_to_cpuid_lock(struct its_vpe *vpe, unsigned long *flags)
Marc Zyngier425c09b2019-11-08 16:57:57 +0000243{
Marc Zyngierf3a059212020-03-04 20:33:10 +0000244 raw_spin_lock_irqsave(&vpe->vpe_lock, *flags);
245 return vpe->col_idx;
246}
247
248static void vpe_to_cpuid_unlock(struct its_vpe *vpe, unsigned long flags)
249{
250 raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags);
251}
252
253static int irq_to_cpuid_lock(struct irq_data *d, unsigned long *flags)
254{
255 struct its_vlpi_map *map = get_vlpi_map(d);
256 int cpu;
257
258 if (map) {
259 cpu = vpe_to_cpuid_lock(map->vpe, flags);
260 } else {
261 /* Physical LPIs are already locked via the irq_desc lock */
262 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
263 cpu = its_dev->event_map.col_map[its_get_event_id(d)];
264 /* Keep GCC quiet... */
265 *flags = 0;
266 }
267
268 return cpu;
269}
270
271static void irq_to_cpuid_unlock(struct irq_data *d, unsigned long flags)
272{
Marc Zyngierf4a81f52019-12-24 11:10:38 +0000273 struct its_vlpi_map *map = get_vlpi_map(d);
Marc Zyngier425c09b2019-11-08 16:57:57 +0000274
Marc Zyngierf4a81f52019-12-24 11:10:38 +0000275 if (map)
Marc Zyngierf3a059212020-03-04 20:33:10 +0000276 vpe_to_cpuid_unlock(map->vpe, flags);
Marc Zyngier425c09b2019-11-08 16:57:57 +0000277}
278
Marc Zyngier83559b42018-06-22 10:52:52 +0100279static struct its_collection *valid_col(struct its_collection *col)
280{
Joe Perches20faba82019-07-09 22:04:18 -0700281 if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(15, 0)))
Marc Zyngier83559b42018-06-22 10:52:52 +0100282 return NULL;
283
284 return col;
285}
286
Marc Zyngier205e0652018-06-22 10:52:53 +0100287static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
288{
289 if (valid_col(its->collections + vpe->col_idx))
290 return vpe;
291
292 return NULL;
293}
294
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000295/*
296 * ITS command descriptors - parameters to be encoded in a command
297 * block.
298 */
299struct its_cmd_desc {
300 union {
301 struct {
302 struct its_device *dev;
303 u32 event_id;
304 } its_inv_cmd;
305
306 struct {
307 struct its_device *dev;
308 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000309 } its_clear_cmd;
310
311 struct {
312 struct its_device *dev;
313 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000314 } its_int_cmd;
315
316 struct {
317 struct its_device *dev;
318 int valid;
319 } its_mapd_cmd;
320
321 struct {
322 struct its_collection *col;
323 int valid;
324 } its_mapc_cmd;
325
326 struct {
327 struct its_device *dev;
328 u32 phys_id;
329 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000330 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000331
332 struct {
333 struct its_device *dev;
334 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100335 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000336 } its_movi_cmd;
337
338 struct {
339 struct its_device *dev;
340 u32 event_id;
341 } its_discard_cmd;
342
343 struct {
344 struct its_collection *col;
345 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000346
347 struct {
348 struct its_vpe *vpe;
Marc Zyngiereb781922016-12-20 14:47:05 +0000349 } its_vinvall_cmd;
350
351 struct {
352 struct its_vpe *vpe;
353 struct its_collection *col;
354 bool valid;
355 } its_vmapp_cmd;
356
357 struct {
358 struct its_vpe *vpe;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000359 struct its_device *dev;
360 u32 virt_id;
361 u32 event_id;
362 bool db_enabled;
363 } its_vmapti_cmd;
364
365 struct {
366 struct its_vpe *vpe;
367 struct its_device *dev;
368 u32 event_id;
369 bool db_enabled;
370 } its_vmovi_cmd;
Marc Zyngier3171a472016-12-20 15:17:28 +0000371
372 struct {
373 struct its_vpe *vpe;
374 struct its_collection *col;
375 u16 seq_num;
376 u16 its_list;
377 } its_vmovp_cmd;
Marc Zyngierd97c97b2019-12-24 11:10:33 +0000378
379 struct {
380 struct its_vpe *vpe;
381 } its_invdb_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000382 };
383};
384
385/*
386 * The ITS command block, which is what the ITS actually parses.
387 */
388struct its_cmd_block {
Ben Dooks (Codethink)2bbdfcc2019-10-17 12:29:55 +0100389 union {
390 u64 raw_cmd[4];
391 __le64 raw_cmd_le[4];
392 };
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000393};
394
395#define ITS_CMD_QUEUE_SZ SZ_64K
396#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
397
Marc Zyngier67047f902017-07-28 21:16:58 +0100398typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
399 struct its_cmd_block *,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000400 struct its_cmd_desc *);
401
Marc Zyngier67047f902017-07-28 21:16:58 +0100402typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
403 struct its_cmd_block *,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000404 struct its_cmd_desc *);
405
Marc Zyngier4d36f132016-12-19 17:11:52 +0000406static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
407{
408 u64 mask = GENMASK_ULL(h, l);
409 *raw_cmd &= ~mask;
410 *raw_cmd |= (val << l) & mask;
411}
412
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000413static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
414{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000415 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000416}
417
418static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
419{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000420 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000421}
422
423static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
424{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000425 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000426}
427
428static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
429{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000430 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000431}
432
433static void its_encode_size(struct its_cmd_block *cmd, u8 size)
434{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000435 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000436}
437
438static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
439{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500440 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000441}
442
443static void its_encode_valid(struct its_cmd_block *cmd, int valid)
444{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000445 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000446}
447
448static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
449{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500450 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000451}
452
453static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
454{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000455 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000456}
457
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000458static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
459{
460 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
461}
462
463static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
464{
465 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
466}
467
468static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
469{
470 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
471}
472
473static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
474{
475 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
476}
477
Marc Zyngier3171a472016-12-20 15:17:28 +0000478static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
479{
480 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
481}
482
483static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
484{
485 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
486}
487
Marc Zyngiereb781922016-12-20 14:47:05 +0000488static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
489{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500490 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
Marc Zyngiereb781922016-12-20 14:47:05 +0000491}
492
493static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
494{
495 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
496}
497
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000498static void its_encode_vconf_addr(struct its_cmd_block *cmd, u64 vconf_pa)
499{
500 its_mask_encode(&cmd->raw_cmd[0], vconf_pa >> 16, 51, 16);
501}
502
503static void its_encode_alloc(struct its_cmd_block *cmd, bool alloc)
504{
505 its_mask_encode(&cmd->raw_cmd[0], alloc, 8, 8);
506}
507
508static void its_encode_ptz(struct its_cmd_block *cmd, bool ptz)
509{
510 its_mask_encode(&cmd->raw_cmd[0], ptz, 9, 9);
511}
512
513static void its_encode_vmapp_default_db(struct its_cmd_block *cmd,
514 u32 vpe_db_lpi)
515{
516 its_mask_encode(&cmd->raw_cmd[1], vpe_db_lpi, 31, 0);
517}
518
Marc Zyngierdd3f0502019-12-24 11:10:31 +0000519static void its_encode_vmovp_default_db(struct its_cmd_block *cmd,
520 u32 vpe_db_lpi)
521{
522 its_mask_encode(&cmd->raw_cmd[3], vpe_db_lpi, 31, 0);
523}
524
525static void its_encode_db(struct its_cmd_block *cmd, bool db)
526{
527 its_mask_encode(&cmd->raw_cmd[2], db, 63, 63);
528}
529
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000530static inline void its_fixup_cmd(struct its_cmd_block *cmd)
531{
532 /* Let's fixup BE commands */
Ben Dooks (Codethink)2bbdfcc2019-10-17 12:29:55 +0100533 cmd->raw_cmd_le[0] = cpu_to_le64(cmd->raw_cmd[0]);
534 cmd->raw_cmd_le[1] = cpu_to_le64(cmd->raw_cmd[1]);
535 cmd->raw_cmd_le[2] = cpu_to_le64(cmd->raw_cmd[2]);
536 cmd->raw_cmd_le[3] = cpu_to_le64(cmd->raw_cmd[3]);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000537}
538
Marc Zyngier67047f902017-07-28 21:16:58 +0100539static struct its_collection *its_build_mapd_cmd(struct its_node *its,
540 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000541 struct its_cmd_desc *desc)
542{
543 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000544 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000545
546 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
547 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
548
549 its_encode_cmd(cmd, GITS_CMD_MAPD);
550 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
551 its_encode_size(cmd, size - 1);
552 its_encode_itt(cmd, itt_addr);
553 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
554
555 its_fixup_cmd(cmd);
556
Marc Zyngier591e5be2015-07-17 10:46:42 +0100557 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000558}
559
Marc Zyngier67047f902017-07-28 21:16:58 +0100560static struct its_collection *its_build_mapc_cmd(struct its_node *its,
561 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000562 struct its_cmd_desc *desc)
563{
564 its_encode_cmd(cmd, GITS_CMD_MAPC);
565 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
566 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
567 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
568
569 its_fixup_cmd(cmd);
570
571 return desc->its_mapc_cmd.col;
572}
573
Marc Zyngier67047f902017-07-28 21:16:58 +0100574static struct its_collection *its_build_mapti_cmd(struct its_node *its,
575 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000576 struct its_cmd_desc *desc)
577{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100578 struct its_collection *col;
579
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000580 col = dev_event_to_col(desc->its_mapti_cmd.dev,
581 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100582
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000583 its_encode_cmd(cmd, GITS_CMD_MAPTI);
584 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
585 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
586 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100587 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000588
589 its_fixup_cmd(cmd);
590
Marc Zyngier83559b42018-06-22 10:52:52 +0100591 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000592}
593
Marc Zyngier67047f902017-07-28 21:16:58 +0100594static struct its_collection *its_build_movi_cmd(struct its_node *its,
595 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000596 struct its_cmd_desc *desc)
597{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100598 struct its_collection *col;
599
600 col = dev_event_to_col(desc->its_movi_cmd.dev,
601 desc->its_movi_cmd.event_id);
602
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000603 its_encode_cmd(cmd, GITS_CMD_MOVI);
604 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100605 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000606 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
607
608 its_fixup_cmd(cmd);
609
Marc Zyngier83559b42018-06-22 10:52:52 +0100610 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000611}
612
Marc Zyngier67047f902017-07-28 21:16:58 +0100613static struct its_collection *its_build_discard_cmd(struct its_node *its,
614 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000615 struct its_cmd_desc *desc)
616{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100617 struct its_collection *col;
618
619 col = dev_event_to_col(desc->its_discard_cmd.dev,
620 desc->its_discard_cmd.event_id);
621
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000622 its_encode_cmd(cmd, GITS_CMD_DISCARD);
623 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
624 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
625
626 its_fixup_cmd(cmd);
627
Marc Zyngier83559b42018-06-22 10:52:52 +0100628 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000629}
630
Marc Zyngier67047f902017-07-28 21:16:58 +0100631static struct its_collection *its_build_inv_cmd(struct its_node *its,
632 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000633 struct its_cmd_desc *desc)
634{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100635 struct its_collection *col;
636
637 col = dev_event_to_col(desc->its_inv_cmd.dev,
638 desc->its_inv_cmd.event_id);
639
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000640 its_encode_cmd(cmd, GITS_CMD_INV);
641 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
642 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
643
644 its_fixup_cmd(cmd);
645
Marc Zyngier83559b42018-06-22 10:52:52 +0100646 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000647}
648
Marc Zyngier67047f902017-07-28 21:16:58 +0100649static struct its_collection *its_build_int_cmd(struct its_node *its,
650 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000651 struct its_cmd_desc *desc)
652{
653 struct its_collection *col;
654
655 col = dev_event_to_col(desc->its_int_cmd.dev,
656 desc->its_int_cmd.event_id);
657
658 its_encode_cmd(cmd, GITS_CMD_INT);
659 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
660 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
661
662 its_fixup_cmd(cmd);
663
Marc Zyngier83559b42018-06-22 10:52:52 +0100664 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000665}
666
Marc Zyngier67047f902017-07-28 21:16:58 +0100667static struct its_collection *its_build_clear_cmd(struct its_node *its,
668 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000669 struct its_cmd_desc *desc)
670{
671 struct its_collection *col;
672
673 col = dev_event_to_col(desc->its_clear_cmd.dev,
674 desc->its_clear_cmd.event_id);
675
676 its_encode_cmd(cmd, GITS_CMD_CLEAR);
677 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
678 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
679
680 its_fixup_cmd(cmd);
681
Marc Zyngier83559b42018-06-22 10:52:52 +0100682 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000683}
684
Marc Zyngier67047f902017-07-28 21:16:58 +0100685static struct its_collection *its_build_invall_cmd(struct its_node *its,
686 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000687 struct its_cmd_desc *desc)
688{
689 its_encode_cmd(cmd, GITS_CMD_INVALL);
Zenghui Yu10794522019-12-02 15:10:21 +0800690 its_encode_collection(cmd, desc->its_invall_cmd.col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000691
692 its_fixup_cmd(cmd);
693
694 return NULL;
695}
696
Marc Zyngier67047f902017-07-28 21:16:58 +0100697static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
698 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000699 struct its_cmd_desc *desc)
700{
701 its_encode_cmd(cmd, GITS_CMD_VINVALL);
702 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
703
704 its_fixup_cmd(cmd);
705
Marc Zyngier205e0652018-06-22 10:52:53 +0100706 return valid_vpe(its, desc->its_vinvall_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000707}
708
Marc Zyngier67047f902017-07-28 21:16:58 +0100709static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
710 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000711 struct its_cmd_desc *desc)
712{
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000713 unsigned long vpt_addr, vconf_addr;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100714 u64 target;
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000715 bool alloc;
Marc Zyngiereb781922016-12-20 14:47:05 +0000716
717 its_encode_cmd(cmd, GITS_CMD_VMAPP);
718 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
719 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000720
721 if (!desc->its_vmapp_cmd.valid) {
722 if (is_v4_1(its)) {
723 alloc = !atomic_dec_return(&desc->its_vmapp_cmd.vpe->vmapp_count);
724 its_encode_alloc(cmd, alloc);
725 }
726
727 goto out;
728 }
729
730 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
731 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
732
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100733 its_encode_target(cmd, target);
Marc Zyngiereb781922016-12-20 14:47:05 +0000734 its_encode_vpt_addr(cmd, vpt_addr);
735 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
736
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000737 if (!is_v4_1(its))
738 goto out;
739
740 vconf_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->its_vm->vprop_page));
741
742 alloc = !atomic_fetch_inc(&desc->its_vmapp_cmd.vpe->vmapp_count);
743
744 its_encode_alloc(cmd, alloc);
745
746 /* We can only signal PTZ when alloc==1. Why do we have two bits? */
747 its_encode_ptz(cmd, alloc);
748 its_encode_vconf_addr(cmd, vconf_addr);
749 its_encode_vmapp_default_db(cmd, desc->its_vmapp_cmd.vpe->vpe_db_lpi);
750
751out:
Marc Zyngiereb781922016-12-20 14:47:05 +0000752 its_fixup_cmd(cmd);
753
Marc Zyngier205e0652018-06-22 10:52:53 +0100754 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000755}
756
Marc Zyngier67047f902017-07-28 21:16:58 +0100757static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
758 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000759 struct its_cmd_desc *desc)
760{
761 u32 db;
762
Marc Zyngier3858d4d2019-12-24 11:10:37 +0000763 if (!is_v4_1(its) && desc->its_vmapti_cmd.db_enabled)
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000764 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
765 else
766 db = 1023;
767
768 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
769 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
770 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
771 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
772 its_encode_db_phys_id(cmd, db);
773 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
774
775 its_fixup_cmd(cmd);
776
Marc Zyngier205e0652018-06-22 10:52:53 +0100777 return valid_vpe(its, desc->its_vmapti_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000778}
779
Marc Zyngier67047f902017-07-28 21:16:58 +0100780static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
781 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000782 struct its_cmd_desc *desc)
783{
784 u32 db;
785
Marc Zyngier3858d4d2019-12-24 11:10:37 +0000786 if (!is_v4_1(its) && desc->its_vmovi_cmd.db_enabled)
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000787 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
788 else
789 db = 1023;
790
791 its_encode_cmd(cmd, GITS_CMD_VMOVI);
792 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
793 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
794 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
795 its_encode_db_phys_id(cmd, db);
796 its_encode_db_valid(cmd, true);
797
798 its_fixup_cmd(cmd);
799
Marc Zyngier205e0652018-06-22 10:52:53 +0100800 return valid_vpe(its, desc->its_vmovi_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000801}
802
Marc Zyngier67047f902017-07-28 21:16:58 +0100803static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
804 struct its_cmd_block *cmd,
Marc Zyngier3171a472016-12-20 15:17:28 +0000805 struct its_cmd_desc *desc)
806{
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100807 u64 target;
808
809 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngier3171a472016-12-20 15:17:28 +0000810 its_encode_cmd(cmd, GITS_CMD_VMOVP);
811 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
812 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
813 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100814 its_encode_target(cmd, target);
Marc Zyngier3171a472016-12-20 15:17:28 +0000815
Marc Zyngierdd3f0502019-12-24 11:10:31 +0000816 if (is_v4_1(its)) {
817 its_encode_db(cmd, true);
818 its_encode_vmovp_default_db(cmd, desc->its_vmovp_cmd.vpe->vpe_db_lpi);
819 }
820
Marc Zyngier3171a472016-12-20 15:17:28 +0000821 its_fixup_cmd(cmd);
822
Marc Zyngier205e0652018-06-22 10:52:53 +0100823 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
Marc Zyngier3171a472016-12-20 15:17:28 +0000824}
825
Marc Zyngier28614692019-11-08 16:58:02 +0000826static struct its_vpe *its_build_vinv_cmd(struct its_node *its,
827 struct its_cmd_block *cmd,
828 struct its_cmd_desc *desc)
829{
830 struct its_vlpi_map *map;
831
832 map = dev_event_to_vlpi_map(desc->its_inv_cmd.dev,
833 desc->its_inv_cmd.event_id);
834
835 its_encode_cmd(cmd, GITS_CMD_INV);
836 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
837 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
838
839 its_fixup_cmd(cmd);
840
841 return valid_vpe(its, map->vpe);
842}
843
Marc Zyngiered0e4aa2019-11-08 16:58:03 +0000844static struct its_vpe *its_build_vint_cmd(struct its_node *its,
845 struct its_cmd_block *cmd,
846 struct its_cmd_desc *desc)
847{
848 struct its_vlpi_map *map;
849
850 map = dev_event_to_vlpi_map(desc->its_int_cmd.dev,
851 desc->its_int_cmd.event_id);
852
853 its_encode_cmd(cmd, GITS_CMD_INT);
854 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
855 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
856
857 its_fixup_cmd(cmd);
858
859 return valid_vpe(its, map->vpe);
860}
861
862static struct its_vpe *its_build_vclear_cmd(struct its_node *its,
863 struct its_cmd_block *cmd,
864 struct its_cmd_desc *desc)
865{
866 struct its_vlpi_map *map;
867
868 map = dev_event_to_vlpi_map(desc->its_clear_cmd.dev,
869 desc->its_clear_cmd.event_id);
870
871 its_encode_cmd(cmd, GITS_CMD_CLEAR);
872 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
873 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
874
875 its_fixup_cmd(cmd);
876
877 return valid_vpe(its, map->vpe);
878}
879
Marc Zyngierd97c97b2019-12-24 11:10:33 +0000880static struct its_vpe *its_build_invdb_cmd(struct its_node *its,
881 struct its_cmd_block *cmd,
882 struct its_cmd_desc *desc)
883{
884 if (WARN_ON(!is_v4_1(its)))
885 return NULL;
886
887 its_encode_cmd(cmd, GITS_CMD_INVDB);
888 its_encode_vpeid(cmd, desc->its_invdb_cmd.vpe->vpe_id);
889
890 its_fixup_cmd(cmd);
891
892 return valid_vpe(its, desc->its_invdb_cmd.vpe);
893}
894
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000895static u64 its_cmd_ptr_to_offset(struct its_node *its,
896 struct its_cmd_block *ptr)
897{
898 return (ptr - its->cmd_base) * sizeof(*ptr);
899}
900
901static int its_queue_full(struct its_node *its)
902{
903 int widx;
904 int ridx;
905
906 widx = its->cmd_write - its->cmd_base;
907 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
908
909 /* This is incredibly unlikely to happen, unless the ITS locks up. */
910 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
911 return 1;
912
913 return 0;
914}
915
916static struct its_cmd_block *its_allocate_entry(struct its_node *its)
917{
918 struct its_cmd_block *cmd;
919 u32 count = 1000000; /* 1s! */
920
921 while (its_queue_full(its)) {
922 count--;
923 if (!count) {
924 pr_err_ratelimited("ITS queue not draining\n");
925 return NULL;
926 }
927 cpu_relax();
928 udelay(1);
929 }
930
931 cmd = its->cmd_write++;
932
933 /* Handle queue wrapping */
934 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
935 its->cmd_write = its->cmd_base;
936
Marc Zyngier34d677a2016-12-19 17:16:45 +0000937 /* Clear command */
938 cmd->raw_cmd[0] = 0;
939 cmd->raw_cmd[1] = 0;
940 cmd->raw_cmd[2] = 0;
941 cmd->raw_cmd[3] = 0;
942
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000943 return cmd;
944}
945
946static struct its_cmd_block *its_post_commands(struct its_node *its)
947{
948 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
949
950 writel_relaxed(wr, its->base + GITS_CWRITER);
951
952 return its->cmd_write;
953}
954
955static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
956{
957 /*
958 * Make sure the commands written to memory are observable by
959 * the ITS.
960 */
961 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000962 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000963 else
964 dsb(ishst);
965}
966
Marc Zyngiera19b4622017-08-04 17:45:50 +0100967static int its_wait_for_range_completion(struct its_node *its,
Heyi Guoa050fa52019-05-13 19:42:06 +0800968 u64 prev_idx,
Marc Zyngiera19b4622017-08-04 17:45:50 +0100969 struct its_cmd_block *to)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000970{
Heyi Guoa050fa52019-05-13 19:42:06 +0800971 u64 rd_idx, to_idx, linear_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000972 u32 count = 1000000; /* 1s! */
973
Heyi Guoa050fa52019-05-13 19:42:06 +0800974 /* Linearize to_idx if the command set has wrapped around */
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000975 to_idx = its_cmd_ptr_to_offset(its, to);
Heyi Guoa050fa52019-05-13 19:42:06 +0800976 if (to_idx < prev_idx)
977 to_idx += ITS_CMD_QUEUE_SZ;
978
979 linear_idx = prev_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000980
981 while (1) {
Heyi Guoa050fa52019-05-13 19:42:06 +0800982 s64 delta;
983
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000984 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100985
Heyi Guoa050fa52019-05-13 19:42:06 +0800986 /*
987 * Compute the read pointer progress, taking the
988 * potential wrap-around into account.
989 */
990 delta = rd_idx - prev_idx;
991 if (rd_idx < prev_idx)
992 delta += ITS_CMD_QUEUE_SZ;
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100993
Heyi Guoa050fa52019-05-13 19:42:06 +0800994 linear_idx += delta;
995 if (linear_idx >= to_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000996 break;
997
998 count--;
999 if (!count) {
Heyi Guoa050fa52019-05-13 19:42:06 +08001000 pr_err_ratelimited("ITS queue timeout (%llu %llu)\n",
1001 to_idx, linear_idx);
Marc Zyngiera19b4622017-08-04 17:45:50 +01001002 return -1;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001003 }
Heyi Guoa050fa52019-05-13 19:42:06 +08001004 prev_idx = rd_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001005 cpu_relax();
1006 udelay(1);
1007 }
Marc Zyngiera19b4622017-08-04 17:45:50 +01001008
1009 return 0;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001010}
1011
Marc Zyngiere4f90942016-12-19 17:56:32 +00001012/* Warning, macro hell follows */
1013#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
1014void name(struct its_node *its, \
1015 buildtype builder, \
1016 struct its_cmd_desc *desc) \
1017{ \
1018 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
1019 synctype *sync_obj; \
1020 unsigned long flags; \
Heyi Guoa050fa52019-05-13 19:42:06 +08001021 u64 rd_idx; \
Marc Zyngiere4f90942016-12-19 17:56:32 +00001022 \
1023 raw_spin_lock_irqsave(&its->lock, flags); \
1024 \
1025 cmd = its_allocate_entry(its); \
1026 if (!cmd) { /* We're soooooo screewed... */ \
1027 raw_spin_unlock_irqrestore(&its->lock, flags); \
1028 return; \
1029 } \
Marc Zyngier67047f902017-07-28 21:16:58 +01001030 sync_obj = builder(its, cmd, desc); \
Marc Zyngiere4f90942016-12-19 17:56:32 +00001031 its_flush_cmd(its, cmd); \
1032 \
1033 if (sync_obj) { \
1034 sync_cmd = its_allocate_entry(its); \
1035 if (!sync_cmd) \
1036 goto post; \
1037 \
Marc Zyngier67047f902017-07-28 21:16:58 +01001038 buildfn(its, sync_cmd, sync_obj); \
Marc Zyngiere4f90942016-12-19 17:56:32 +00001039 its_flush_cmd(its, sync_cmd); \
1040 } \
1041 \
1042post: \
Heyi Guoa050fa52019-05-13 19:42:06 +08001043 rd_idx = readl_relaxed(its->base + GITS_CREADR); \
Marc Zyngiere4f90942016-12-19 17:56:32 +00001044 next_cmd = its_post_commands(its); \
1045 raw_spin_unlock_irqrestore(&its->lock, flags); \
1046 \
Heyi Guoa050fa52019-05-13 19:42:06 +08001047 if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \
Marc Zyngiera19b4622017-08-04 17:45:50 +01001048 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001049}
1050
Marc Zyngier67047f902017-07-28 21:16:58 +01001051static void its_build_sync_cmd(struct its_node *its,
1052 struct its_cmd_block *sync_cmd,
Marc Zyngiere4f90942016-12-19 17:56:32 +00001053 struct its_collection *sync_col)
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001054{
Marc Zyngiere4f90942016-12-19 17:56:32 +00001055 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
1056 its_encode_target(sync_cmd, sync_col->target_address);
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001057
Marc Zyngiere4f90942016-12-19 17:56:32 +00001058 its_fixup_cmd(sync_cmd);
1059}
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001060
Marc Zyngiere4f90942016-12-19 17:56:32 +00001061static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
1062 struct its_collection, its_build_sync_cmd)
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001063
Marc Zyngier67047f902017-07-28 21:16:58 +01001064static void its_build_vsync_cmd(struct its_node *its,
1065 struct its_cmd_block *sync_cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001066 struct its_vpe *sync_vpe)
1067{
1068 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
1069 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001070
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001071 its_fixup_cmd(sync_cmd);
1072}
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001073
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001074static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
1075 struct its_vpe, its_build_vsync_cmd)
1076
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001077static void its_send_int(struct its_device *dev, u32 event_id)
1078{
1079 struct its_cmd_desc desc;
1080
1081 desc.its_int_cmd.dev = dev;
1082 desc.its_int_cmd.event_id = event_id;
1083
1084 its_send_single_command(dev->its, its_build_int_cmd, &desc);
1085}
1086
1087static void its_send_clear(struct its_device *dev, u32 event_id)
1088{
1089 struct its_cmd_desc desc;
1090
1091 desc.its_clear_cmd.dev = dev;
1092 desc.its_clear_cmd.event_id = event_id;
1093
1094 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001095}
1096
1097static void its_send_inv(struct its_device *dev, u32 event_id)
1098{
1099 struct its_cmd_desc desc;
1100
1101 desc.its_inv_cmd.dev = dev;
1102 desc.its_inv_cmd.event_id = event_id;
1103
1104 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
1105}
1106
1107static void its_send_mapd(struct its_device *dev, int valid)
1108{
1109 struct its_cmd_desc desc;
1110
1111 desc.its_mapd_cmd.dev = dev;
1112 desc.its_mapd_cmd.valid = !!valid;
1113
1114 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
1115}
1116
1117static void its_send_mapc(struct its_node *its, struct its_collection *col,
1118 int valid)
1119{
1120 struct its_cmd_desc desc;
1121
1122 desc.its_mapc_cmd.col = col;
1123 desc.its_mapc_cmd.valid = !!valid;
1124
1125 its_send_single_command(its, its_build_mapc_cmd, &desc);
1126}
1127
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001128static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001129{
1130 struct its_cmd_desc desc;
1131
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001132 desc.its_mapti_cmd.dev = dev;
1133 desc.its_mapti_cmd.phys_id = irq_id;
1134 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001135
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001136 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001137}
1138
1139static void its_send_movi(struct its_device *dev,
1140 struct its_collection *col, u32 id)
1141{
1142 struct its_cmd_desc desc;
1143
1144 desc.its_movi_cmd.dev = dev;
1145 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001146 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001147
1148 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
1149}
1150
1151static void its_send_discard(struct its_device *dev, u32 id)
1152{
1153 struct its_cmd_desc desc;
1154
1155 desc.its_discard_cmd.dev = dev;
1156 desc.its_discard_cmd.event_id = id;
1157
1158 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
1159}
1160
1161static void its_send_invall(struct its_node *its, struct its_collection *col)
1162{
1163 struct its_cmd_desc desc;
1164
1165 desc.its_invall_cmd.col = col;
1166
1167 its_send_single_command(its, its_build_invall_cmd, &desc);
1168}
Marc Zyngierc48ed512014-11-24 14:35:12 +00001169
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001170static void its_send_vmapti(struct its_device *dev, u32 id)
1171{
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001172 struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001173 struct its_cmd_desc desc;
1174
1175 desc.its_vmapti_cmd.vpe = map->vpe;
1176 desc.its_vmapti_cmd.dev = dev;
1177 desc.its_vmapti_cmd.virt_id = map->vintid;
1178 desc.its_vmapti_cmd.event_id = id;
1179 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
1180
1181 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
1182}
1183
1184static void its_send_vmovi(struct its_device *dev, u32 id)
1185{
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001186 struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001187 struct its_cmd_desc desc;
1188
1189 desc.its_vmovi_cmd.vpe = map->vpe;
1190 desc.its_vmovi_cmd.dev = dev;
1191 desc.its_vmovi_cmd.event_id = id;
1192 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
1193
1194 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
1195}
1196
Marc Zyngier75fd9512017-10-08 18:46:39 +01001197static void its_send_vmapp(struct its_node *its,
1198 struct its_vpe *vpe, bool valid)
Marc Zyngiereb781922016-12-20 14:47:05 +00001199{
1200 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +00001201
1202 desc.its_vmapp_cmd.vpe = vpe;
1203 desc.its_vmapp_cmd.valid = valid;
Marc Zyngier75fd9512017-10-08 18:46:39 +01001204 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
Marc Zyngiereb781922016-12-20 14:47:05 +00001205
Marc Zyngier75fd9512017-10-08 18:46:39 +01001206 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +00001207}
1208
Marc Zyngier3171a472016-12-20 15:17:28 +00001209static void its_send_vmovp(struct its_vpe *vpe)
1210{
Zenghui Yu84243122019-10-23 03:46:26 +00001211 struct its_cmd_desc desc = {};
Marc Zyngier3171a472016-12-20 15:17:28 +00001212 struct its_node *its;
1213 unsigned long flags;
1214 int col_id = vpe->col_idx;
1215
1216 desc.its_vmovp_cmd.vpe = vpe;
Marc Zyngier3171a472016-12-20 15:17:28 +00001217
1218 if (!its_list_map) {
1219 its = list_first_entry(&its_nodes, struct its_node, entry);
Marc Zyngier3171a472016-12-20 15:17:28 +00001220 desc.its_vmovp_cmd.col = &its->collections[col_id];
1221 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1222 return;
1223 }
1224
1225 /*
1226 * Yet another marvel of the architecture. If using the
1227 * its_list "feature", we need to make sure that all ITSs
1228 * receive all VMOVP commands in the same order. The only way
1229 * to guarantee this is to make vmovp a serialization point.
1230 *
1231 * Wall <-- Head.
1232 */
1233 raw_spin_lock_irqsave(&vmovp_lock, flags);
1234
1235 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
Zenghui Yu84243122019-10-23 03:46:26 +00001236 desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm);
Marc Zyngier3171a472016-12-20 15:17:28 +00001237
1238 /* Emit VMOVPs */
1239 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00001240 if (!is_v4(its))
Marc Zyngier3171a472016-12-20 15:17:28 +00001241 continue;
1242
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001243 if (!vpe->its_vm->vlpi_count[its->list_nr])
1244 continue;
1245
Marc Zyngier3171a472016-12-20 15:17:28 +00001246 desc.its_vmovp_cmd.col = &its->collections[col_id];
1247 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1248 }
1249
1250 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1251}
1252
Marc Zyngier40619a22017-10-08 15:16:09 +01001253static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
Marc Zyngiereb781922016-12-20 14:47:05 +00001254{
1255 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +00001256
1257 desc.its_vinvall_cmd.vpe = vpe;
Marc Zyngier40619a22017-10-08 15:16:09 +01001258 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +00001259}
1260
Marc Zyngier28614692019-11-08 16:58:02 +00001261static void its_send_vinv(struct its_device *dev, u32 event_id)
1262{
1263 struct its_cmd_desc desc;
1264
1265 /*
1266 * There is no real VINV command. This is just a normal INV,
1267 * with a VSYNC instead of a SYNC.
1268 */
1269 desc.its_inv_cmd.dev = dev;
1270 desc.its_inv_cmd.event_id = event_id;
1271
1272 its_send_single_vcommand(dev->its, its_build_vinv_cmd, &desc);
1273}
1274
Marc Zyngiered0e4aa2019-11-08 16:58:03 +00001275static void its_send_vint(struct its_device *dev, u32 event_id)
1276{
1277 struct its_cmd_desc desc;
1278
1279 /*
1280 * There is no real VINT command. This is just a normal INT,
1281 * with a VSYNC instead of a SYNC.
1282 */
1283 desc.its_int_cmd.dev = dev;
1284 desc.its_int_cmd.event_id = event_id;
1285
1286 its_send_single_vcommand(dev->its, its_build_vint_cmd, &desc);
1287}
1288
1289static void its_send_vclear(struct its_device *dev, u32 event_id)
1290{
1291 struct its_cmd_desc desc;
1292
1293 /*
1294 * There is no real VCLEAR command. This is just a normal CLEAR,
1295 * with a VSYNC instead of a SYNC.
1296 */
1297 desc.its_clear_cmd.dev = dev;
1298 desc.its_clear_cmd.event_id = event_id;
1299
1300 its_send_single_vcommand(dev->its, its_build_vclear_cmd, &desc);
1301}
1302
Marc Zyngierd97c97b2019-12-24 11:10:33 +00001303static void its_send_invdb(struct its_node *its, struct its_vpe *vpe)
1304{
1305 struct its_cmd_desc desc;
1306
1307 desc.its_invdb_cmd.vpe = vpe;
1308 its_send_single_vcommand(its, its_build_invdb_cmd, &desc);
1309}
1310
Marc Zyngierc48ed512014-11-24 14:35:12 +00001311/*
1312 * irqchip functions - assumes MSI, mostly.
1313 */
Marc Zyngier015ec032016-12-20 09:54:57 +00001314static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +00001315{
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001316 struct its_vlpi_map *map = get_vlpi_map(d);
Marc Zyngier015ec032016-12-20 09:54:57 +00001317 irq_hw_number_t hwirq;
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001318 void *va;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001319 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001320
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001321 if (map) {
1322 va = page_address(map->vm->vprop_page);
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001323 hwirq = map->vintid;
1324
1325 /* Remember the updated property */
1326 map->properties &= ~clr;
1327 map->properties |= set | LPI_PROP_GROUP1;
Marc Zyngier015ec032016-12-20 09:54:57 +00001328 } else {
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001329 va = gic_rdists->prop_table_va;
Marc Zyngier015ec032016-12-20 09:54:57 +00001330 hwirq = d->hwirq;
1331 }
Marc Zyngieradcdb942016-12-19 19:18:13 +00001332
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001333 cfg = va + hwirq - 8192;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001334 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +00001335 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001336
1337 /*
1338 * Make the above write visible to the redistributors.
1339 * And yes, we're flushing exactly: One. Single. Byte.
1340 * Humpf...
1341 */
1342 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001343 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001344 else
1345 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +00001346}
1347
Marc Zyngier2f4f0642019-11-08 16:57:56 +00001348static void wait_for_syncr(void __iomem *rdbase)
1349{
1350 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
1351 cpu_relax();
1352}
1353
Marc Zyngier425c09b2019-11-08 16:57:57 +00001354static void direct_lpi_inv(struct irq_data *d)
1355{
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001356 struct its_vlpi_map *map = get_vlpi_map(d);
Marc Zyngier425c09b2019-11-08 16:57:57 +00001357 void __iomem *rdbase;
Marc Zyngierf3a059212020-03-04 20:33:10 +00001358 unsigned long flags;
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001359 u64 val;
Marc Zyngierf3a059212020-03-04 20:33:10 +00001360 int cpu;
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001361
1362 if (map) {
1363 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1364
1365 WARN_ON(!is_v4_1(its_dev->its));
1366
1367 val = GICR_INVLPIR_V;
1368 val |= FIELD_PREP(GICR_INVLPIR_VPEID, map->vpe->vpe_id);
1369 val |= FIELD_PREP(GICR_INVLPIR_INTID, map->vintid);
1370 } else {
1371 val = d->hwirq;
1372 }
Marc Zyngier425c09b2019-11-08 16:57:57 +00001373
1374 /* Target the redistributor this LPI is currently routed to */
Marc Zyngierf3a059212020-03-04 20:33:10 +00001375 cpu = irq_to_cpuid_lock(d, &flags);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00001376 raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock);
Marc Zyngierf3a059212020-03-04 20:33:10 +00001377 rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base;
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001378 gic_write_lpir(val, rdbase + GICR_INVLPIR);
Marc Zyngier425c09b2019-11-08 16:57:57 +00001379
1380 wait_for_syncr(rdbase);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00001381 raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock);
Marc Zyngierf3a059212020-03-04 20:33:10 +00001382 irq_to_cpuid_unlock(d, flags);
Marc Zyngier425c09b2019-11-08 16:57:57 +00001383}
1384
Marc Zyngier015ec032016-12-20 09:54:57 +00001385static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1386{
1387 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1388
1389 lpi_write_config(d, clr, set);
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001390 if (gic_rdists->has_direct_lpi &&
1391 (is_v4_1(its_dev->its) || !irqd_is_forwarded_to_vcpu(d)))
Marc Zyngier425c09b2019-11-08 16:57:57 +00001392 direct_lpi_inv(d);
Marc Zyngier28614692019-11-08 16:58:02 +00001393 else if (!irqd_is_forwarded_to_vcpu(d))
Marc Zyngier425c09b2019-11-08 16:57:57 +00001394 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngier28614692019-11-08 16:58:02 +00001395 else
1396 its_send_vinv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001397}
1398
Marc Zyngier015ec032016-12-20 09:54:57 +00001399static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1400{
1401 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1402 u32 event = its_get_event_id(d);
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001403 struct its_vlpi_map *map;
Marc Zyngier015ec032016-12-20 09:54:57 +00001404
Marc Zyngier3858d4d2019-12-24 11:10:37 +00001405 /*
1406 * GICv4.1 does away with the per-LPI nonsense, nothing to do
1407 * here.
1408 */
1409 if (is_v4_1(its_dev->its))
1410 return;
1411
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001412 map = dev_event_to_vlpi_map(its_dev, event);
1413
1414 if (map->db_enabled == enable)
Marc Zyngier015ec032016-12-20 09:54:57 +00001415 return;
1416
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001417 map->db_enabled = enable;
Marc Zyngier015ec032016-12-20 09:54:57 +00001418
1419 /*
1420 * More fun with the architecture:
1421 *
1422 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1423 * value or to 1023, depending on the enable bit. But that
1424 * would be issueing a mapping for an /existing/ DevID+EventID
1425 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1426 * to the /same/ vPE, using this opportunity to adjust the
1427 * doorbell. Mouahahahaha. We loves it, Precious.
1428 */
1429 its_send_vmovi(its_dev, event);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001430}
1431
1432static void its_mask_irq(struct irq_data *d)
1433{
Marc Zyngier015ec032016-12-20 09:54:57 +00001434 if (irqd_is_forwarded_to_vcpu(d))
1435 its_vlpi_set_doorbell(d, false);
1436
Marc Zyngieradcdb942016-12-19 19:18:13 +00001437 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001438}
1439
1440static void its_unmask_irq(struct irq_data *d)
1441{
Marc Zyngier015ec032016-12-20 09:54:57 +00001442 if (irqd_is_forwarded_to_vcpu(d))
1443 its_vlpi_set_doorbell(d, true);
1444
Marc Zyngieradcdb942016-12-19 19:18:13 +00001445 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001446}
1447
Marc Zyngierc48ed512014-11-24 14:35:12 +00001448static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1449 bool force)
1450{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001451 unsigned int cpu;
1452 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001453 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1454 struct its_collection *target_col;
1455 u32 id = its_get_event_id(d);
1456
Marc Zyngier015ec032016-12-20 09:54:57 +00001457 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1458 if (irqd_is_forwarded_to_vcpu(d))
1459 return -EINVAL;
1460
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001461 /* lpi cannot be routed to a redistributor that is on a foreign node */
1462 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1463 if (its_dev->its->numa_node >= 0) {
1464 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1465 if (!cpumask_intersects(mask_val, cpu_mask))
1466 return -EINVAL;
1467 }
1468 }
1469
1470 cpu = cpumask_any_and(mask_val, cpu_mask);
1471
Marc Zyngierc48ed512014-11-24 14:35:12 +00001472 if (cpu >= nr_cpu_ids)
1473 return -EINVAL;
1474
MaJun8b8d94a2017-05-18 16:19:13 +08001475 /* don't set the affinity when the target cpu is same as current one */
1476 if (cpu != its_dev->event_map.col_map[id]) {
1477 target_col = &its_dev->its->collections[cpu];
1478 its_send_movi(its_dev, target_col, id);
1479 its_dev->event_map.col_map[id] = cpu;
Marc Zyngier0d224d32017-08-18 09:39:18 +01001480 irq_data_update_effective_affinity(d, cpumask_of(cpu));
MaJun8b8d94a2017-05-18 16:19:13 +08001481 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001482
1483 return IRQ_SET_MASK_OK_DONE;
1484}
1485
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001486static u64 its_irq_get_msi_base(struct its_device *its_dev)
1487{
1488 struct its_node *its = its_dev->its;
1489
1490 return its->phys_base + GITS_TRANSLATER;
1491}
1492
Marc Zyngierb48ac832014-11-24 14:35:16 +00001493static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1494{
1495 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1496 struct its_node *its;
1497 u64 addr;
1498
1499 its = its_dev->its;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001500 addr = its->get_msi_base(its_dev);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001501
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001502 msg->address_lo = lower_32_bits(addr);
1503 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001504 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001505
Julien Grall35ae7df2019-05-01 14:58:21 +01001506 iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001507}
1508
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001509static int its_irq_set_irqchip_state(struct irq_data *d,
1510 enum irqchip_irq_state which,
1511 bool state)
1512{
1513 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1514 u32 event = its_get_event_id(d);
1515
1516 if (which != IRQCHIP_STATE_PENDING)
1517 return -EINVAL;
1518
Marc Zyngiered0e4aa2019-11-08 16:58:03 +00001519 if (irqd_is_forwarded_to_vcpu(d)) {
1520 if (state)
1521 its_send_vint(its_dev, event);
1522 else
1523 its_send_vclear(its_dev, event);
1524 } else {
1525 if (state)
1526 its_send_int(its_dev, event);
1527 else
1528 its_send_clear(its_dev, event);
1529 }
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001530
1531 return 0;
1532}
1533
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001534static void its_map_vm(struct its_node *its, struct its_vm *vm)
1535{
1536 unsigned long flags;
1537
1538 /* Not using the ITS list? Everything is always mapped. */
1539 if (!its_list_map)
1540 return;
1541
1542 raw_spin_lock_irqsave(&vmovp_lock, flags);
1543
1544 /*
1545 * If the VM wasn't mapped yet, iterate over the vpes and get
1546 * them mapped now.
1547 */
1548 vm->vlpi_count[its->list_nr]++;
1549
1550 if (vm->vlpi_count[its->list_nr] == 1) {
1551 int i;
1552
1553 for (i = 0; i < vm->nr_vpes; i++) {
1554 struct its_vpe *vpe = vm->vpes[i];
Marc Zyngier44c4c252017-10-19 10:11:34 +01001555 struct irq_data *d = irq_get_irq_data(vpe->irq);
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001556
1557 /* Map the VPE to the first possible CPU */
1558 vpe->col_idx = cpumask_first(cpu_online_mask);
1559 its_send_vmapp(its, vpe, true);
1560 its_send_vinvall(its, vpe);
Marc Zyngier44c4c252017-10-19 10:11:34 +01001561 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001562 }
1563 }
1564
1565 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1566}
1567
1568static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1569{
1570 unsigned long flags;
1571
1572 /* Not using the ITS list? Everything is always mapped. */
1573 if (!its_list_map)
1574 return;
1575
1576 raw_spin_lock_irqsave(&vmovp_lock, flags);
1577
1578 if (!--vm->vlpi_count[its->list_nr]) {
1579 int i;
1580
1581 for (i = 0; i < vm->nr_vpes; i++)
1582 its_send_vmapp(its, vm->vpes[i], false);
1583 }
1584
1585 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1586}
1587
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001588static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1589{
1590 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1591 u32 event = its_get_event_id(d);
1592 int ret = 0;
1593
1594 if (!info->map)
1595 return -EINVAL;
1596
Marc Zyngier11635fa2019-11-08 16:58:05 +00001597 raw_spin_lock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001598
1599 if (!its_dev->event_map.vm) {
1600 struct its_vlpi_map *maps;
1601
Kees Cook6396bb22018-06-12 14:03:40 -07001602 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
Marc Zyngier11635fa2019-11-08 16:58:05 +00001603 GFP_ATOMIC);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001604 if (!maps) {
1605 ret = -ENOMEM;
1606 goto out;
1607 }
1608
1609 its_dev->event_map.vm = info->map->vm;
1610 its_dev->event_map.vlpi_maps = maps;
1611 } else if (its_dev->event_map.vm != info->map->vm) {
1612 ret = -EINVAL;
1613 goto out;
1614 }
1615
1616 /* Get our private copy of the mapping information */
1617 its_dev->event_map.vlpi_maps[event] = *info->map;
1618
1619 if (irqd_is_forwarded_to_vcpu(d)) {
1620 /* Already mapped, move it around */
1621 its_send_vmovi(its_dev, event);
1622 } else {
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001623 /* Ensure all the VPEs are mapped on this ITS */
1624 its_map_vm(its_dev->its, info->map->vm);
1625
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001626 /*
1627 * Flag the interrupt as forwarded so that we can
1628 * start poking the virtual property table.
1629 */
1630 irqd_set_forwarded_to_vcpu(d);
1631
1632 /* Write out the property to the prop table */
1633 lpi_write_config(d, 0xff, info->map->properties);
1634
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001635 /* Drop the physical mapping */
1636 its_send_discard(its_dev, event);
1637
1638 /* and install the virtual one */
1639 its_send_vmapti(its_dev, event);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001640
1641 /* Increment the number of VLPIs */
1642 its_dev->event_map.nr_vlpis++;
1643 }
1644
1645out:
Marc Zyngier11635fa2019-11-08 16:58:05 +00001646 raw_spin_unlock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001647 return ret;
1648}
1649
1650static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1651{
1652 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier046b5052019-11-08 16:58:04 +00001653 struct its_vlpi_map *map;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001654 int ret = 0;
1655
Marc Zyngier11635fa2019-11-08 16:58:05 +00001656 raw_spin_lock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001657
Marc Zyngier046b5052019-11-08 16:58:04 +00001658 map = get_vlpi_map(d);
1659
1660 if (!its_dev->event_map.vm || !map) {
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001661 ret = -EINVAL;
1662 goto out;
1663 }
1664
1665 /* Copy our mapping information to the incoming request */
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001666 *info->map = *map;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001667
1668out:
Marc Zyngier11635fa2019-11-08 16:58:05 +00001669 raw_spin_unlock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001670 return ret;
1671}
1672
1673static int its_vlpi_unmap(struct irq_data *d)
1674{
1675 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1676 u32 event = its_get_event_id(d);
1677 int ret = 0;
1678
Marc Zyngier11635fa2019-11-08 16:58:05 +00001679 raw_spin_lock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001680
1681 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1682 ret = -EINVAL;
1683 goto out;
1684 }
1685
1686 /* Drop the virtual mapping */
1687 its_send_discard(its_dev, event);
1688
1689 /* and restore the physical one */
1690 irqd_clr_forwarded_to_vcpu(d);
1691 its_send_mapti(its_dev, d->hwirq, event);
1692 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1693 LPI_PROP_ENABLED |
1694 LPI_PROP_GROUP1));
1695
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001696 /* Potentially unmap the VM from this ITS */
1697 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1698
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001699 /*
1700 * Drop the refcount and make the device available again if
1701 * this was the last VLPI.
1702 */
1703 if (!--its_dev->event_map.nr_vlpis) {
1704 its_dev->event_map.vm = NULL;
1705 kfree(its_dev->event_map.vlpi_maps);
1706 }
1707
1708out:
Marc Zyngier11635fa2019-11-08 16:58:05 +00001709 raw_spin_unlock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001710 return ret;
1711}
1712
Marc Zyngier015ec032016-12-20 09:54:57 +00001713static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1714{
1715 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1716
1717 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1718 return -EINVAL;
1719
1720 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1721 lpi_update_config(d, 0xff, info->config);
1722 else
1723 lpi_write_config(d, 0xff, info->config);
1724 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1725
1726 return 0;
1727}
1728
Marc Zyngierc808eea2016-12-20 09:31:20 +00001729static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1730{
1731 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1732 struct its_cmd_info *info = vcpu_info;
1733
1734 /* Need a v4 ITS */
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00001735 if (!is_v4(its_dev->its))
Marc Zyngierc808eea2016-12-20 09:31:20 +00001736 return -EINVAL;
1737
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001738 /* Unmap request? */
1739 if (!info)
1740 return its_vlpi_unmap(d);
1741
Marc Zyngierc808eea2016-12-20 09:31:20 +00001742 switch (info->cmd_type) {
1743 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001744 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001745
1746 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001747 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001748
1749 case PROP_UPDATE_VLPI:
1750 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001751 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001752
1753 default:
1754 return -EINVAL;
1755 }
1756}
1757
Marc Zyngierc48ed512014-11-24 14:35:12 +00001758static struct irq_chip its_irq_chip = {
1759 .name = "ITS",
1760 .irq_mask = its_mask_irq,
1761 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001762 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001763 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001764 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001765 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001766 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001767};
1768
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001769
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001770/*
1771 * How we allocate LPIs:
1772 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001773 * lpi_range_list contains ranges of LPIs that are to available to
1774 * allocate from. To allocate LPIs, just pick the first range that
1775 * fits the required allocation, and reduce it by the required
1776 * amount. Once empty, remove the range from the list.
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001777 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001778 * To free a range of LPIs, add a free range to the list, sort it and
1779 * merge the result if the new range happens to be adjacent to an
1780 * already free block.
1781 *
1782 * The consequence of the above is that allocation is cost is low, but
1783 * freeing is expensive. We assumes that freeing rarely occurs.
1784 */
Jia He4cb205c2018-08-28 12:53:26 +08001785#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001786
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001787static DEFINE_MUTEX(lpi_range_lock);
1788static LIST_HEAD(lpi_range_list);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001789
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001790struct lpi_range {
1791 struct list_head entry;
1792 u32 base_id;
1793 u32 span;
1794};
1795
1796static struct lpi_range *mk_lpi_range(u32 base, u32 span)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001797{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001798 struct lpi_range *range;
1799
Rasmus Villemoes1c73fac2019-03-12 18:33:48 +01001800 range = kmalloc(sizeof(*range), GFP_KERNEL);
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001801 if (range) {
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001802 range->base_id = base;
1803 range->span = span;
1804 }
1805
1806 return range;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001807}
1808
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001809static int alloc_lpi_range(u32 nr_lpis, u32 *base)
1810{
1811 struct lpi_range *range, *tmp;
1812 int err = -ENOSPC;
1813
1814 mutex_lock(&lpi_range_lock);
1815
1816 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1817 if (range->span >= nr_lpis) {
1818 *base = range->base_id;
1819 range->base_id += nr_lpis;
1820 range->span -= nr_lpis;
1821
1822 if (range->span == 0) {
1823 list_del(&range->entry);
1824 kfree(range);
1825 }
1826
1827 err = 0;
1828 break;
1829 }
1830 }
1831
1832 mutex_unlock(&lpi_range_lock);
1833
1834 pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
1835 return err;
1836}
1837
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001838static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b)
1839{
1840 if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list)
1841 return;
1842 if (a->base_id + a->span != b->base_id)
1843 return;
1844 b->base_id = a->base_id;
1845 b->span += a->span;
1846 list_del(&a->entry);
1847 kfree(a);
1848}
1849
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001850static int free_lpi_range(u32 base, u32 nr_lpis)
1851{
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001852 struct lpi_range *new, *old;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001853
1854 new = mk_lpi_range(base, nr_lpis);
Rasmus Villemoesb31a3832019-03-12 18:33:47 +01001855 if (!new)
1856 return -ENOMEM;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001857
1858 mutex_lock(&lpi_range_lock);
1859
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001860 list_for_each_entry_reverse(old, &lpi_range_list, entry) {
1861 if (old->base_id < base)
1862 break;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001863 }
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001864 /*
1865 * old is the last element with ->base_id smaller than base,
1866 * so new goes right after it. If there are no elements with
1867 * ->base_id smaller than base, &old->entry ends up pointing
1868 * at the head of the list, and inserting new it the start of
1869 * the list is the right thing to do in that case as well.
1870 */
1871 list_add(&new->entry, &old->entry);
1872 /*
1873 * Now check if we can merge with the preceding and/or
1874 * following ranges.
1875 */
1876 merge_lpi_ranges(old, new);
1877 merge_lpi_ranges(new, list_next_entry(new, entry));
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001878
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001879 mutex_unlock(&lpi_range_lock);
Rasmus Villemoesb31a3832019-03-12 18:33:47 +01001880 return 0;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001881}
1882
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001883static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001884{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001885 u32 lpis = (1UL << id_bits) - 8192;
Marc Zyngier12b29052018-05-31 09:01:59 +01001886 u32 numlpis;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001887 int err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001888
Marc Zyngier12b29052018-05-31 09:01:59 +01001889 numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
1890
1891 if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
1892 lpis = numlpis;
1893 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
1894 lpis);
1895 }
1896
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001897 /*
1898 * Initializing the allocator is just the same as freeing the
1899 * full range of LPIs.
1900 */
1901 err = free_lpi_range(8192, lpis);
1902 pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
1903 return err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001904}
1905
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001906static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001907{
1908 unsigned long *bitmap = NULL;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001909 int err = 0;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001910
1911 do {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001912 err = alloc_lpi_range(nr_irqs, base);
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001913 if (!err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001914 break;
1915
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001916 nr_irqs /= 2;
1917 } while (nr_irqs > 0);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001918
Marc Zyngier45725e02019-01-29 15:19:23 +00001919 if (!nr_irqs)
1920 err = -ENOSPC;
1921
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001922 if (err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001923 goto out;
1924
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001925 bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001926 if (!bitmap)
1927 goto out;
1928
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001929 *nr_ids = nr_irqs;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001930
1931out:
Marc Zyngierc8415b92015-10-02 16:44:05 +01001932 if (!bitmap)
1933 *base = *nr_ids = 0;
1934
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001935 return bitmap;
1936}
1937
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001938static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001939{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001940 WARN_ON(free_lpi_range(base, nr_ids));
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001941 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001942}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001943
Marc Zyngier053be482018-07-27 15:02:27 +01001944static void gic_reset_prop_table(void *va)
1945{
1946 /* Priority 0xa0, Group-1, disabled */
1947 memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ);
1948
1949 /* Make sure the GIC will observe the written configuration */
1950 gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ);
1951}
1952
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001953static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1954{
1955 struct page *prop_page;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001956
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001957 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1958 if (!prop_page)
1959 return NULL;
1960
Marc Zyngier053be482018-07-27 15:02:27 +01001961 gic_reset_prop_table(page_address(prop_page));
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001962
1963 return prop_page;
1964}
1965
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001966static void its_free_prop_table(struct page *prop_page)
1967{
1968 free_pages((unsigned long)page_address(prop_page),
1969 get_order(LPI_PROPBASE_SZ));
1970}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001971
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01001972static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
1973{
1974 phys_addr_t start, end, addr_end;
1975 u64 i;
1976
1977 /*
1978 * We don't bother checking for a kdump kernel as by
1979 * construction, the LPI tables are out of this kernel's
1980 * memory map.
1981 */
1982 if (is_kdump_kernel())
1983 return true;
1984
1985 addr_end = addr + size - 1;
1986
1987 for_each_reserved_mem_region(i, &start, &end) {
1988 if (addr >= start && addr_end <= end)
1989 return true;
1990 }
1991
1992 /* Not found, not a good sign... */
1993 pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n",
1994 &addr, &addr_end);
1995 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
1996 return false;
1997}
1998
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01001999static int gic_reserve_range(phys_addr_t addr, unsigned long size)
2000{
2001 if (efi_enabled(EFI_CONFIG_TABLES))
2002 return efi_mem_reserve_persistent(addr, size);
2003
2004 return 0;
2005}
2006
Marc Zyngier11e37d32018-07-27 13:38:54 +01002007static int __init its_setup_lpi_prop_table(void)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002008{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002009 if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) {
2010 u64 val;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002011
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002012 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2013 lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1;
2014
2015 gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12);
2016 gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa,
2017 LPI_PROPBASE_SZ,
2018 MEMREMAP_WB);
2019 gic_reset_prop_table(gic_rdists->prop_table_va);
2020 } else {
2021 struct page *page;
2022
2023 lpi_id_bits = min_t(u32,
2024 GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
2025 ITS_MAX_LPI_NRBITS);
2026 page = its_allocate_prop_table(GFP_NOWAIT);
2027 if (!page) {
2028 pr_err("Failed to allocate PROPBASE\n");
2029 return -ENOMEM;
2030 }
2031
2032 gic_rdists->prop_table_pa = page_to_phys(page);
2033 gic_rdists->prop_table_va = page_address(page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01002034 WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa,
2035 LPI_PROPBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002036 }
2037
Marc Zyngiere1a2e202018-07-27 14:36:00 +01002038 pr_info("GICv3: using LPI property table @%pa\n",
2039 &gic_rdists->prop_table_pa);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002040
Shanker Donthineni6c31e122017-06-22 18:19:14 -05002041 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002042}
2043
2044static const char *its_base_type_string[] = {
2045 [GITS_BASER_TYPE_DEVICE] = "Devices",
2046 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00002047 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002048 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
2049 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
2050 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
2051 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
2052};
2053
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002054static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
2055{
2056 u32 idx = baser - its->tables;
2057
Vladimir Murzin0968a612016-11-02 11:54:06 +00002058 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002059}
2060
2061static void its_write_baser(struct its_node *its, struct its_baser *baser,
2062 u64 val)
2063{
2064 u32 idx = baser - its->tables;
2065
Vladimir Murzin0968a612016-11-02 11:54:06 +00002066 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002067 baser->val = its_read_baser(its, baser);
2068}
2069
Shanker Donthineni93473592016-06-06 18:17:30 -05002070static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002071 u64 cache, u64 shr, u32 psz, u32 order,
2072 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05002073{
2074 u64 val = its_read_baser(its, baser);
2075 u64 esz = GITS_BASER_ENTRY_SIZE(val);
2076 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05002077 u64 baser_phys, tmp;
Shanker Donthineni93473592016-06-06 18:17:30 -05002078 u32 alloc_pages;
Shanker Donthineni539d3782019-01-14 09:50:19 +00002079 struct page *page;
Shanker Donthineni93473592016-06-06 18:17:30 -05002080 void *base;
Shanker Donthineni93473592016-06-06 18:17:30 -05002081
2082retry_alloc_baser:
2083 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
2084 if (alloc_pages > GITS_BASER_PAGES_MAX) {
2085 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
2086 &its->phys_base, its_base_type_string[type],
2087 alloc_pages, GITS_BASER_PAGES_MAX);
2088 alloc_pages = GITS_BASER_PAGES_MAX;
2089 order = get_order(GITS_BASER_PAGES_MAX * psz);
2090 }
2091
Shanker Donthineni539d3782019-01-14 09:50:19 +00002092 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order);
2093 if (!page)
Shanker Donthineni93473592016-06-06 18:17:30 -05002094 return -ENOMEM;
2095
Shanker Donthineni539d3782019-01-14 09:50:19 +00002096 base = (void *)page_address(page);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05002097 baser_phys = virt_to_phys(base);
2098
2099 /* Check if the physical address of the memory is above 48bits */
2100 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
2101
2102 /* 52bit PA is supported only when PageSize=64K */
2103 if (psz != SZ_64K) {
2104 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
2105 free_pages((unsigned long)base, order);
2106 return -ENXIO;
2107 }
2108
2109 /* Convert 52bit PA to 48bit field */
2110 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
2111 }
2112
Shanker Donthineni93473592016-06-06 18:17:30 -05002113retry_baser:
Shanker Donthineni30ae9612017-10-09 11:46:55 -05002114 val = (baser_phys |
Shanker Donthineni93473592016-06-06 18:17:30 -05002115 (type << GITS_BASER_TYPE_SHIFT) |
2116 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
2117 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
2118 cache |
2119 shr |
2120 GITS_BASER_VALID);
2121
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002122 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
2123
Shanker Donthineni93473592016-06-06 18:17:30 -05002124 switch (psz) {
2125 case SZ_4K:
2126 val |= GITS_BASER_PAGE_SIZE_4K;
2127 break;
2128 case SZ_16K:
2129 val |= GITS_BASER_PAGE_SIZE_16K;
2130 break;
2131 case SZ_64K:
2132 val |= GITS_BASER_PAGE_SIZE_64K;
2133 break;
2134 }
2135
2136 its_write_baser(its, baser, val);
2137 tmp = baser->val;
2138
2139 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
2140 /*
2141 * Shareability didn't stick. Just use
2142 * whatever the read reported, which is likely
2143 * to be the only thing this redistributor
2144 * supports. If that's zero, make it
2145 * non-cacheable as well.
2146 */
2147 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
2148 if (!shr) {
2149 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00002150 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05002151 }
2152 goto retry_baser;
2153 }
2154
2155 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
2156 /*
2157 * Page size didn't stick. Let's try a smaller
2158 * size and retry. If we reach 4K, then
2159 * something is horribly wrong...
2160 */
2161 free_pages((unsigned long)base, order);
2162 baser->base = NULL;
2163
2164 switch (psz) {
2165 case SZ_16K:
2166 psz = SZ_4K;
2167 goto retry_alloc_baser;
2168 case SZ_64K:
2169 psz = SZ_16K;
2170 goto retry_alloc_baser;
2171 }
2172 }
2173
2174 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00002175 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05002176 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00002177 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05002178 free_pages((unsigned long)base, order);
2179 return -ENXIO;
2180 }
2181
2182 baser->order = order;
2183 baser->base = base;
2184 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002185 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05002186
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002187 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00002188 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05002189 its_base_type_string[type],
2190 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002191 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05002192 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
2193
2194 return 0;
2195}
2196
Marc Zyngier4cacac52016-12-19 18:18:34 +00002197static bool its_parse_indirect_baser(struct its_node *its,
2198 struct its_baser *baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002199 u32 psz, u32 *order, u32 ids)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002200{
Marc Zyngier4cacac52016-12-19 18:18:34 +00002201 u64 tmp = its_read_baser(its, baser);
2202 u64 type = GITS_BASER_TYPE(tmp);
2203 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002204 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002205 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002206 bool indirect = false;
2207
2208 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
2209 if ((esz << ids) > (psz * 2)) {
2210 /*
2211 * Find out whether hw supports a single or two-level table by
2212 * table by reading bit at offset '62' after writing '1' to it.
2213 */
2214 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
2215 indirect = !!(baser->val & GITS_BASER_INDIRECT);
2216
2217 if (indirect) {
2218 /*
2219 * The size of the lvl2 table is equal to ITS page size
2220 * which is 'psz'. For computing lvl1 table size,
2221 * subtract ID bits that sparse lvl2 table from 'ids'
2222 * which is reported by ITS hardware times lvl1 table
2223 * entry size.
2224 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00002225 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002226 esz = GITS_LVL1_ENTRY_SIZE;
2227 }
2228 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002229
2230 /*
2231 * Allocate as many entries as required to fit the
2232 * range of device IDs that the ITS can grok... The ID
2233 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002234 * massive waste of memory if two-level device table
2235 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002236 */
2237 new_order = max_t(u32, get_order(esz << ids), new_order);
2238 if (new_order >= MAX_ORDER) {
2239 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00002240 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier576a8342019-11-08 16:58:00 +00002241 pr_warn("ITS@%pa: %s Table too large, reduce ids %llu->%u\n",
Marc Zyngier4cacac52016-12-19 18:18:34 +00002242 &its->phys_base, its_base_type_string[type],
Marc Zyngier576a8342019-11-08 16:58:00 +00002243 device_ids(its), ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002244 }
2245
2246 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002247
2248 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002249}
2250
Marc Zyngier5e516842019-12-24 11:10:28 +00002251static u32 compute_common_aff(u64 val)
2252{
2253 u32 aff, clpiaff;
2254
2255 aff = FIELD_GET(GICR_TYPER_AFFINITY, val);
2256 clpiaff = FIELD_GET(GICR_TYPER_COMMON_LPI_AFF, val);
2257
2258 return aff & ~(GENMASK(31, 0) >> (clpiaff * 8));
2259}
2260
2261static u32 compute_its_aff(struct its_node *its)
2262{
2263 u64 val;
2264 u32 svpet;
2265
2266 /*
2267 * Reencode the ITS SVPET and MPIDR as a GICR_TYPER, and compute
2268 * the resulting affinity. We then use that to see if this match
2269 * our own affinity.
2270 */
2271 svpet = FIELD_GET(GITS_TYPER_SVPET, its->typer);
2272 val = FIELD_PREP(GICR_TYPER_COMMON_LPI_AFF, svpet);
2273 val |= FIELD_PREP(GICR_TYPER_AFFINITY, its->mpidr);
2274 return compute_common_aff(val);
2275}
2276
2277static struct its_node *find_sibling_its(struct its_node *cur_its)
2278{
2279 struct its_node *its;
2280 u32 aff;
2281
2282 if (!FIELD_GET(GITS_TYPER_SVPET, cur_its->typer))
2283 return NULL;
2284
2285 aff = compute_its_aff(cur_its);
2286
2287 list_for_each_entry(its, &its_nodes, entry) {
2288 u64 baser;
2289
2290 if (!is_v4_1(its) || its == cur_its)
2291 continue;
2292
2293 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2294 continue;
2295
2296 if (aff != compute_its_aff(its))
2297 continue;
2298
2299 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2300 baser = its->tables[2].val;
2301 if (!(baser & GITS_BASER_VALID))
2302 continue;
2303
2304 return its;
2305 }
2306
2307 return NULL;
2308}
2309
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002310static void its_free_tables(struct its_node *its)
2311{
2312 int i;
2313
2314 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06002315 if (its->tables[i].base) {
2316 free_pages((unsigned long)its->tables[i].base,
2317 its->tables[i].order);
2318 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002319 }
2320 }
2321}
2322
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05002323static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002324{
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002325 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002326 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05002327 u32 psz = SZ_64K;
2328 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02002329
Ard Biesheuvelfa150012017-10-17 17:55:54 +01002330 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
2331 /* erratum 24313: ignore memory access type */
2332 cache = GITS_BASER_nCnB;
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002333
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002334 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002335 struct its_baser *baser = its->tables + i;
2336 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002337 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05002338 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002339 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002340
Marc Zyngier4cacac52016-12-19 18:18:34 +00002341 switch (type) {
2342 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002343 continue;
2344
Marc Zyngier4cacac52016-12-19 18:18:34 +00002345 case GITS_BASER_TYPE_DEVICE:
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002346 indirect = its_parse_indirect_baser(its, baser,
2347 psz, &order,
Marc Zyngier576a8342019-11-08 16:58:00 +00002348 device_ids(its));
Zenghui Yu8d565742019-02-10 05:24:10 +00002349 break;
2350
Marc Zyngier4cacac52016-12-19 18:18:34 +00002351 case GITS_BASER_TYPE_VCPU:
Marc Zyngier5e516842019-12-24 11:10:28 +00002352 if (is_v4_1(its)) {
2353 struct its_node *sibling;
2354
2355 WARN_ON(i != 2);
2356 if ((sibling = find_sibling_its(its))) {
2357 *baser = sibling->tables[2];
2358 its_write_baser(its, baser, baser->val);
2359 continue;
2360 }
2361 }
2362
Marc Zyngier4cacac52016-12-19 18:18:34 +00002363 indirect = its_parse_indirect_baser(its, baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002364 psz, &order,
2365 ITS_MAX_VPEID_BITS);
Marc Zyngier4cacac52016-12-19 18:18:34 +00002366 break;
2367 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00002368
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002369 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05002370 if (err < 0) {
2371 its_free_tables(its);
2372 return err;
Robert Richter30f21362015-09-21 22:58:34 +02002373 }
2374
Shanker Donthineni93473592016-06-06 18:17:30 -05002375 /* Update settings which will be used for next BASERn */
2376 psz = baser->psz;
2377 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
2378 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002379 }
2380
2381 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002382}
2383
Marc Zyngier5e516842019-12-24 11:10:28 +00002384static u64 inherit_vpe_l1_table_from_its(void)
2385{
2386 struct its_node *its;
2387 u64 val;
2388 u32 aff;
2389
2390 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2391 aff = compute_common_aff(val);
2392
2393 list_for_each_entry(its, &its_nodes, entry) {
2394 u64 baser, addr;
2395
2396 if (!is_v4_1(its))
2397 continue;
2398
2399 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2400 continue;
2401
2402 if (aff != compute_its_aff(its))
2403 continue;
2404
2405 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2406 baser = its->tables[2].val;
2407 if (!(baser & GITS_BASER_VALID))
2408 continue;
2409
2410 /* We have a winner! */
Zenghui Yu8b718d42020-02-06 15:57:07 +08002411 gic_data_rdist()->vpe_l1_base = its->tables[2].base;
2412
Marc Zyngier5e516842019-12-24 11:10:28 +00002413 val = GICR_VPROPBASER_4_1_VALID;
2414 if (baser & GITS_BASER_INDIRECT)
2415 val |= GICR_VPROPBASER_4_1_INDIRECT;
2416 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE,
2417 FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser));
2418 switch (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser)) {
2419 case GIC_PAGE_SIZE_64K:
2420 addr = GITS_BASER_ADDR_48_to_52(baser);
2421 break;
2422 default:
2423 addr = baser & GENMASK_ULL(47, 12);
2424 break;
2425 }
2426 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, addr >> 12);
2427 val |= FIELD_PREP(GICR_VPROPBASER_SHAREABILITY_MASK,
2428 FIELD_GET(GITS_BASER_SHAREABILITY_MASK, baser));
2429 val |= FIELD_PREP(GICR_VPROPBASER_INNER_CACHEABILITY_MASK,
2430 FIELD_GET(GITS_BASER_INNER_CACHEABILITY_MASK, baser));
2431 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, GITS_BASER_NR_PAGES(baser) - 1);
2432
2433 return val;
2434 }
2435
2436 return 0;
2437}
2438
2439static u64 inherit_vpe_l1_table_from_rd(cpumask_t **mask)
2440{
2441 u32 aff;
2442 u64 val;
2443 int cpu;
2444
2445 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2446 aff = compute_common_aff(val);
2447
2448 for_each_possible_cpu(cpu) {
2449 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
Marc Zyngier5e516842019-12-24 11:10:28 +00002450
2451 if (!base || cpu == smp_processor_id())
2452 continue;
2453
2454 val = gic_read_typer(base + GICR_TYPER);
Zenghui Yu4bccf1d2020-02-06 15:57:09 +08002455 if (aff != compute_common_aff(val))
Marc Zyngier5e516842019-12-24 11:10:28 +00002456 continue;
2457
2458 /*
2459 * At this point, we have a victim. This particular CPU
2460 * has already booted, and has an affinity that matches
2461 * ours wrt CommonLPIAff. Let's use its own VPROPBASER.
2462 * Make sure we don't write the Z bit in that case.
2463 */
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002464 val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
Marc Zyngier5e516842019-12-24 11:10:28 +00002465 val &= ~GICR_VPROPBASER_4_1_Z;
2466
Zenghui Yu8b718d42020-02-06 15:57:07 +08002467 gic_data_rdist()->vpe_l1_base = gic_data_rdist_cpu(cpu)->vpe_l1_base;
Marc Zyngier5e516842019-12-24 11:10:28 +00002468 *mask = gic_data_rdist_cpu(cpu)->vpe_table_mask;
2469
2470 return val;
2471 }
2472
2473 return 0;
2474}
2475
Zenghui Yu4e6437f2020-02-06 15:57:08 +08002476static bool allocate_vpe_l2_table(int cpu, u32 id)
2477{
2478 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
Marc Zyngier490d3322020-02-09 22:48:50 +00002479 unsigned int psz, esz, idx, npg, gpsz;
2480 u64 val;
Zenghui Yu4e6437f2020-02-06 15:57:08 +08002481 struct page *page;
2482 __le64 *table;
2483
2484 if (!gic_rdists->has_rvpeid)
2485 return true;
2486
Marc Zyngier28d160d2020-03-04 20:33:09 +00002487 /* Skip non-present CPUs */
2488 if (!base)
2489 return true;
2490
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002491 val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
Zenghui Yu4e6437f2020-02-06 15:57:08 +08002492
2493 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val) + 1;
2494 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2495 npg = FIELD_GET(GICR_VPROPBASER_4_1_SIZE, val) + 1;
2496
2497 switch (gpsz) {
2498 default:
2499 WARN_ON(1);
2500 /* fall through */
2501 case GIC_PAGE_SIZE_4K:
2502 psz = SZ_4K;
2503 break;
2504 case GIC_PAGE_SIZE_16K:
2505 psz = SZ_16K;
2506 break;
2507 case GIC_PAGE_SIZE_64K:
2508 psz = SZ_64K;
2509 break;
2510 }
2511
2512 /* Don't allow vpe_id that exceeds single, flat table limit */
2513 if (!(val & GICR_VPROPBASER_4_1_INDIRECT))
2514 return (id < (npg * psz / (esz * SZ_8)));
2515
2516 /* Compute 1st level table index & check if that exceeds table limit */
2517 idx = id >> ilog2(psz / (esz * SZ_8));
2518 if (idx >= (npg * psz / GITS_LVL1_ENTRY_SIZE))
2519 return false;
2520
2521 table = gic_data_rdist_cpu(cpu)->vpe_l1_base;
2522
2523 /* Allocate memory for 2nd level table */
2524 if (!table[idx]) {
2525 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(psz));
2526 if (!page)
2527 return false;
2528
2529 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2530 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2531 gic_flush_dcache_to_poc(page_address(page), psz);
2532
2533 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2534
2535 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2536 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2537 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
2538
2539 /* Ensure updated table contents are visible to RD hardware */
2540 dsb(sy);
2541 }
2542
2543 return true;
2544}
2545
Marc Zyngier5e516842019-12-24 11:10:28 +00002546static int allocate_vpe_l1_table(void)
2547{
2548 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2549 u64 val, gpsz, npg, pa;
2550 unsigned int psz = SZ_64K;
2551 unsigned int np, epp, esz;
2552 struct page *page;
2553
2554 if (!gic_rdists->has_rvpeid)
2555 return 0;
2556
2557 /*
2558 * if VPENDBASER.Valid is set, disable any previously programmed
2559 * VPE by setting PendingLast while clearing Valid. This has the
2560 * effect of making sure no doorbell will be generated and we can
2561 * then safely clear VPROPBASER.Valid.
2562 */
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002563 if (gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER) & GICR_VPENDBASER_Valid)
2564 gicr_write_vpendbaser(GICR_VPENDBASER_PendingLast,
Marc Zyngier5e516842019-12-24 11:10:28 +00002565 vlpi_base + GICR_VPENDBASER);
2566
2567 /*
2568 * If we can inherit the configuration from another RD, let's do
2569 * so. Otherwise, we have to go through the allocation process. We
2570 * assume that all RDs have the exact same requirements, as
2571 * nothing will work otherwise.
2572 */
2573 val = inherit_vpe_l1_table_from_rd(&gic_data_rdist()->vpe_table_mask);
2574 if (val & GICR_VPROPBASER_4_1_VALID)
2575 goto out;
2576
2577 gic_data_rdist()->vpe_table_mask = kzalloc(sizeof(cpumask_t), GFP_KERNEL);
2578 if (!gic_data_rdist()->vpe_table_mask)
2579 return -ENOMEM;
2580
2581 val = inherit_vpe_l1_table_from_its();
2582 if (val & GICR_VPROPBASER_4_1_VALID)
2583 goto out;
2584
2585 /* First probe the page size */
2586 val = FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, GIC_PAGE_SIZE_64K);
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002587 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2588 val = gicr_read_vpropbaser(vlpi_base + GICR_VPROPBASER);
Marc Zyngier5e516842019-12-24 11:10:28 +00002589 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2590 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val);
2591
2592 switch (gpsz) {
2593 default:
2594 gpsz = GIC_PAGE_SIZE_4K;
2595 /* fall through */
2596 case GIC_PAGE_SIZE_4K:
2597 psz = SZ_4K;
2598 break;
2599 case GIC_PAGE_SIZE_16K:
2600 psz = SZ_16K;
2601 break;
2602 case GIC_PAGE_SIZE_64K:
2603 psz = SZ_64K;
2604 break;
2605 }
2606
2607 /*
2608 * Start populating the register from scratch, including RO fields
2609 * (which we want to print in debug cases...)
2610 */
2611 val = 0;
2612 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, gpsz);
2613 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ENTRY_SIZE, esz);
2614
2615 /* How many entries per GIC page? */
2616 esz++;
2617 epp = psz / (esz * SZ_8);
2618
2619 /*
2620 * If we need more than just a single L1 page, flag the table
2621 * as indirect and compute the number of required L1 pages.
2622 */
2623 if (epp < ITS_MAX_VPEID) {
2624 int nl2;
2625
2626 val |= GICR_VPROPBASER_4_1_INDIRECT;
2627
2628 /* Number of L2 pages required to cover the VPEID space */
2629 nl2 = DIV_ROUND_UP(ITS_MAX_VPEID, epp);
2630
2631 /* Number of L1 pages to point to the L2 pages */
2632 npg = DIV_ROUND_UP(nl2 * SZ_8, psz);
2633 } else {
2634 npg = 1;
2635 }
2636
Zenghui Yue88bd312020-02-06 15:57:06 +08002637 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, npg - 1);
Marc Zyngier5e516842019-12-24 11:10:28 +00002638
2639 /* Right, that's the number of CPU pages we need for L1 */
2640 np = DIV_ROUND_UP(npg * psz, PAGE_SIZE);
2641
2642 pr_debug("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n",
2643 np, npg, psz, epp, esz);
2644 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(np * PAGE_SIZE));
2645 if (!page)
2646 return -ENOMEM;
2647
Zenghui Yu8b718d42020-02-06 15:57:07 +08002648 gic_data_rdist()->vpe_l1_base = page_address(page);
Marc Zyngier5e516842019-12-24 11:10:28 +00002649 pa = virt_to_phys(page_address(page));
2650 WARN_ON(!IS_ALIGNED(pa, psz));
2651
2652 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, pa >> 12);
2653 val |= GICR_VPROPBASER_RaWb;
2654 val |= GICR_VPROPBASER_InnerShareable;
2655 val |= GICR_VPROPBASER_4_1_Z;
2656 val |= GICR_VPROPBASER_4_1_VALID;
2657
2658out:
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002659 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
Marc Zyngier5e516842019-12-24 11:10:28 +00002660 cpumask_set_cpu(smp_processor_id(), gic_data_rdist()->vpe_table_mask);
2661
2662 pr_debug("CPU%d: VPROPBASER = %llx %*pbl\n",
2663 smp_processor_id(), val,
2664 cpumask_pr_args(gic_data_rdist()->vpe_table_mask));
2665
2666 return 0;
2667}
2668
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002669static int its_alloc_collections(struct its_node *its)
2670{
Marc Zyngier83559b42018-06-22 10:52:52 +01002671 int i;
2672
Kees Cook6396bb22018-06-12 14:03:40 -07002673 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002674 GFP_KERNEL);
2675 if (!its->collections)
2676 return -ENOMEM;
2677
Marc Zyngier83559b42018-06-22 10:52:52 +01002678 for (i = 0; i < nr_cpu_ids; i++)
2679 its->collections[i].target_address = ~0ULL;
2680
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002681 return 0;
2682}
2683
Marc Zyngier7c297a22016-12-19 18:34:38 +00002684static struct page *its_allocate_pending_table(gfp_t gfp_flags)
2685{
2686 struct page *pend_page;
Marc Zyngieradaab502018-07-17 18:06:39 +01002687
Marc Zyngier7c297a22016-12-19 18:34:38 +00002688 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
Marc Zyngieradaab502018-07-17 18:06:39 +01002689 get_order(LPI_PENDBASE_SZ));
Marc Zyngier7c297a22016-12-19 18:34:38 +00002690 if (!pend_page)
2691 return NULL;
2692
2693 /* Make sure the GIC will observe the zero-ed page */
2694 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
2695
2696 return pend_page;
2697}
2698
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002699static void its_free_pending_table(struct page *pt)
2700{
Marc Zyngieradaab502018-07-17 18:06:39 +01002701 free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ));
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002702}
2703
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002704/*
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002705 * Booting with kdump and LPIs enabled is generally fine. Any other
2706 * case is wrong in the absence of firmware/EFI support.
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002707 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002708static bool enabled_lpis_allowed(void)
2709{
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002710 phys_addr_t addr;
2711 u64 val;
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002712
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002713 /* Check whether the property table is in a reserved region */
2714 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2715 addr = val & GENMASK_ULL(51, 12);
2716
2717 return gic_check_reserved_range(addr, LPI_PROPBASE_SZ);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002718}
2719
Marc Zyngier11e37d32018-07-27 13:38:54 +01002720static int __init allocate_lpi_tables(void)
2721{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002722 u64 val;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002723 int err, cpu;
2724
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002725 /*
2726 * If LPIs are enabled while we run this from the boot CPU,
2727 * flag the RD tables as pre-allocated if the stars do align.
2728 */
2729 val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR);
2730 if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) {
2731 gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED |
2732 RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING);
2733 pr_info("GICv3: Using preallocated redistributor tables\n");
2734 }
2735
Marc Zyngier11e37d32018-07-27 13:38:54 +01002736 err = its_setup_lpi_prop_table();
2737 if (err)
2738 return err;
2739
2740 /*
2741 * We allocate all the pending tables anyway, as we may have a
2742 * mix of RDs that have had LPIs enabled, and some that
2743 * don't. We'll free the unused ones as each CPU comes online.
2744 */
2745 for_each_possible_cpu(cpu) {
2746 struct page *pend_page;
2747
2748 pend_page = its_allocate_pending_table(GFP_NOWAIT);
2749 if (!pend_page) {
2750 pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu);
2751 return -ENOMEM;
2752 }
2753
2754 gic_data_rdist_cpu(cpu)->pend_page = pend_page;
2755 }
2756
2757 return 0;
2758}
2759
Marc Zyngiere64fab12019-12-24 11:10:35 +00002760static u64 its_clear_vpend_valid(void __iomem *vlpi_base, u64 clr, u64 set)
Heyi Guo64794502019-01-24 21:37:08 +08002761{
2762 u32 count = 1000000; /* 1s! */
2763 bool clean;
2764 u64 val;
2765
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002766 val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002767 val &= ~GICR_VPENDBASER_Valid;
Marc Zyngiere64fab12019-12-24 11:10:35 +00002768 val &= ~clr;
2769 val |= set;
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002770 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002771
2772 do {
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002773 val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002774 clean = !(val & GICR_VPENDBASER_Dirty);
2775 if (!clean) {
2776 count--;
2777 cpu_relax();
2778 udelay(1);
2779 }
2780 } while (!clean && count);
2781
Marc Zyngiere64fab12019-12-24 11:10:35 +00002782 if (unlikely(val & GICR_VPENDBASER_Dirty)) {
2783 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2784 val |= GICR_VPENDBASER_PendingLast;
2785 }
2786
Heyi Guo64794502019-01-24 21:37:08 +08002787 return val;
2788}
2789
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002790static void its_cpu_init_lpis(void)
2791{
2792 void __iomem *rbase = gic_data_rdist_rd_base();
2793 struct page *pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002794 phys_addr_t paddr;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002795 u64 val, tmp;
2796
Marc Zyngier11e37d32018-07-27 13:38:54 +01002797 if (gic_data_rdist()->lpi_enabled)
2798 return;
2799
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002800 val = readl_relaxed(rbase + GICR_CTLR);
2801 if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) &&
2802 (val & GICR_CTLR_ENABLE_LPIS)) {
Marc Zyngierf842ca82018-07-27 16:03:31 +01002803 /*
2804 * Check that we get the same property table on all
2805 * RDs. If we don't, this is hopeless.
2806 */
2807 paddr = gicr_read_propbaser(rbase + GICR_PROPBASER);
2808 paddr &= GENMASK_ULL(51, 12);
2809 if (WARN_ON(gic_rdists->prop_table_pa != paddr))
2810 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
2811
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002812 paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER);
2813 paddr &= GENMASK_ULL(51, 16);
2814
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002815 WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002816 its_free_pending_table(gic_data_rdist()->pend_page);
2817 gic_data_rdist()->pend_page = NULL;
2818
2819 goto out;
2820 }
2821
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002822 pend_page = gic_data_rdist()->pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002823 paddr = page_to_phys(pend_page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01002824 WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002825
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002826 /* set PROPBASE */
Marc Zyngiere1a2e202018-07-27 14:36:00 +01002827 val = (gic_rdists->prop_table_pa |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002828 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002829 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002830 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
2831
Vladimir Murzin0968a612016-11-02 11:54:06 +00002832 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2833 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002834
2835 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002836 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
2837 /*
2838 * The HW reports non-shareable, we must
2839 * remove the cacheability attributes as
2840 * well.
2841 */
2842 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
2843 GICR_PROPBASER_CACHEABILITY_MASK);
2844 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002845 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002846 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002847 pr_info_once("GIC: using cache flushing for LPI property table\n");
2848 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
2849 }
2850
2851 /* set PENDBASE */
2852 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002853 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002854 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002855
Vladimir Murzin0968a612016-11-02 11:54:06 +00002856 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2857 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002858
2859 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
2860 /*
2861 * The HW reports non-shareable, we must remove the
2862 * cacheability attributes as well.
2863 */
2864 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
2865 GICR_PENDBASER_CACHEABILITY_MASK);
2866 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002867 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002868 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002869
2870 /* Enable LPIs */
2871 val = readl_relaxed(rbase + GICR_CTLR);
2872 val |= GICR_CTLR_ENABLE_LPIS;
2873 writel_relaxed(val, rbase + GICR_CTLR);
2874
Marc Zyngier5e516842019-12-24 11:10:28 +00002875 if (gic_rdists->has_vlpis && !gic_rdists->has_rvpeid) {
Heyi Guo64794502019-01-24 21:37:08 +08002876 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2877
2878 /*
2879 * It's possible for CPU to receive VLPIs before it is
2880 * sheduled as a vPE, especially for the first CPU, and the
2881 * VLPI with INTID larger than 2^(IDbits+1) will be considered
2882 * as out of range and dropped by GIC.
2883 * So we initialize IDbits to known value to avoid VLPI drop.
2884 */
2885 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2886 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
2887 smp_processor_id(), val);
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002888 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002889
2890 /*
2891 * Also clear Valid bit of GICR_VPENDBASER, in case some
2892 * ancient programming gets left in and has possibility of
2893 * corrupting memory.
2894 */
Marc Zyngiere64fab12019-12-24 11:10:35 +00002895 val = its_clear_vpend_valid(vlpi_base, 0, 0);
Heyi Guo64794502019-01-24 21:37:08 +08002896 }
2897
Marc Zyngier5e516842019-12-24 11:10:28 +00002898 if (allocate_vpe_l1_table()) {
2899 /*
2900 * If the allocation has failed, we're in massive trouble.
2901 * Disable direct injection, and pray that no VM was
2902 * already running...
2903 */
2904 gic_rdists->has_rvpeid = false;
2905 gic_rdists->has_vlpis = false;
2906 }
2907
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002908 /* Make sure the GIC has seen the above */
2909 dsb(sy);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002910out:
Marc Zyngier11e37d32018-07-27 13:38:54 +01002911 gic_data_rdist()->lpi_enabled = true;
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002912 pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n",
Marc Zyngier11e37d32018-07-27 13:38:54 +01002913 smp_processor_id(),
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002914 gic_data_rdist()->pend_page ? "allocated" : "reserved",
Marc Zyngier11e37d32018-07-27 13:38:54 +01002915 &paddr);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002916}
2917
Derek Basehore920181c2018-02-28 21:48:20 -08002918static void its_cpu_init_collection(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002919{
Derek Basehore920181c2018-02-28 21:48:20 -08002920 int cpu = smp_processor_id();
2921 u64 target;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002922
Derek Basehore920181c2018-02-28 21:48:20 -08002923 /* avoid cross node collections and its mapping */
2924 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
2925 struct device_node *cpu_node;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002926
Derek Basehore920181c2018-02-28 21:48:20 -08002927 cpu_node = of_get_cpu_node(cpu, NULL);
2928 if (its->numa_node != NUMA_NO_NODE &&
2929 its->numa_node != of_node_to_nid(cpu_node))
2930 return;
2931 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002932
Derek Basehore920181c2018-02-28 21:48:20 -08002933 /*
2934 * We now have to bind each collection to its target
2935 * redistributor.
2936 */
2937 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002938 /*
Derek Basehore920181c2018-02-28 21:48:20 -08002939 * This ITS wants the physical address of the
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002940 * redistributor.
2941 */
Derek Basehore920181c2018-02-28 21:48:20 -08002942 target = gic_data_rdist()->phys_base;
2943 } else {
2944 /* This ITS wants a linear CPU number. */
2945 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2946 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002947 }
2948
Derek Basehore920181c2018-02-28 21:48:20 -08002949 /* Perform collection mapping */
2950 its->collections[cpu].target_address = target;
2951 its->collections[cpu].col_id = cpu;
2952
2953 its_send_mapc(its, &its->collections[cpu], 1);
2954 its_send_invall(its, &its->collections[cpu]);
2955}
2956
2957static void its_cpu_init_collections(void)
2958{
2959 struct its_node *its;
2960
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02002961 raw_spin_lock(&its_lock);
Derek Basehore920181c2018-02-28 21:48:20 -08002962
2963 list_for_each_entry(its, &its_nodes, entry)
2964 its_cpu_init_collection(its);
2965
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02002966 raw_spin_unlock(&its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002967}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002968
2969static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
2970{
2971 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002972 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002973
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002974 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002975
2976 list_for_each_entry(tmp, &its->its_device_list, entry) {
2977 if (tmp->device_id == dev_id) {
2978 its_dev = tmp;
2979 break;
2980 }
2981 }
2982
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002983 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002984
2985 return its_dev;
2986}
2987
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002988static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2989{
2990 int i;
2991
2992 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2993 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2994 return &its->tables[i];
2995 }
2996
2997 return NULL;
2998}
2999
Shanker Donthineni539d3782019-01-14 09:50:19 +00003000static bool its_alloc_table_entry(struct its_node *its,
3001 struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003002{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003003 struct page *page;
3004 u32 esz, idx;
3005 __le64 *table;
3006
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003007 /* Don't allow device id that exceeds single, flat table limit */
3008 esz = GITS_BASER_ENTRY_SIZE(baser->val);
3009 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003010 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003011
3012 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003013 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003014 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
3015 return false;
3016
3017 table = baser->base;
3018
3019 /* Allocate memory for 2nd level table */
3020 if (!table[idx]) {
Shanker Donthineni539d3782019-01-14 09:50:19 +00003021 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
3022 get_order(baser->psz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003023 if (!page)
3024 return false;
3025
3026 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
3027 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00003028 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003029
3030 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
3031
3032 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
3033 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00003034 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003035
3036 /* Ensure updated table contents are visible to ITS hardware */
3037 dsb(sy);
3038 }
3039
3040 return true;
3041}
3042
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003043static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
3044{
3045 struct its_baser *baser;
3046
3047 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
3048
3049 /* Don't allow device id that exceeds ITS hardware limit */
3050 if (!baser)
Marc Zyngier576a8342019-11-08 16:58:00 +00003051 return (ilog2(dev_id) < device_ids(its));
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003052
Shanker Donthineni539d3782019-01-14 09:50:19 +00003053 return its_alloc_table_entry(its, baser, dev_id);
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003054}
3055
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003056static bool its_alloc_vpe_table(u32 vpe_id)
3057{
3058 struct its_node *its;
Zenghui Yu4e6437f2020-02-06 15:57:08 +08003059 int cpu;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003060
3061 /*
3062 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
3063 * could try and only do it on ITSs corresponding to devices
3064 * that have interrupts targeted at this VPE, but the
3065 * complexity becomes crazy (and you have tons of memory
3066 * anyway, right?).
3067 */
3068 list_for_each_entry(its, &its_nodes, entry) {
3069 struct its_baser *baser;
3070
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00003071 if (!is_v4(its))
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003072 continue;
3073
3074 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
3075 if (!baser)
3076 return false;
3077
Shanker Donthineni539d3782019-01-14 09:50:19 +00003078 if (!its_alloc_table_entry(its, baser, vpe_id))
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003079 return false;
3080 }
3081
Zenghui Yu4e6437f2020-02-06 15:57:08 +08003082 /* Non v4.1? No need to iterate RDs and go back early. */
3083 if (!gic_rdists->has_rvpeid)
3084 return true;
3085
3086 /*
3087 * Make sure the L2 tables are allocated for all copies of
3088 * the L1 table on *all* v4.1 RDs.
3089 */
3090 for_each_possible_cpu(cpu) {
3091 if (!allocate_vpe_l2_table(cpu, vpe_id))
3092 return false;
3093 }
3094
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003095 return true;
3096}
3097
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003098static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003099 int nvecs, bool alloc_lpis)
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003100{
3101 struct its_device *dev;
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003102 unsigned long *lpi_map = NULL;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003103 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01003104 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003105 void *itt;
3106 int lpi_base;
3107 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00003108 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003109 int sz;
3110
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003111 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06003112 return NULL;
3113
Marc Zyngier147c8f32018-05-27 16:39:55 +01003114 if (WARN_ON(!is_power_of_2(nvecs)))
3115 nvecs = roundup_pow_of_two(nvecs);
3116
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003117 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00003118 /*
Marc Zyngier147c8f32018-05-27 16:39:55 +01003119 * Even if the device wants a single LPI, the ITT must be
3120 * sized as a power of two (and you need at least one bit...).
Marc Zyngierc8481262014-12-12 10:51:24 +00003121 */
Marc Zyngier147c8f32018-05-27 16:39:55 +01003122 nr_ites = max(2, nvecs);
Marc Zyngierffedbf02019-11-08 16:57:59 +00003123 sz = nr_ites * (FIELD_GET(GITS_TYPER_ITT_ENTRY_SIZE, its->typer) + 1);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003124 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Shanker Donthineni539d3782019-01-14 09:50:19 +00003125 itt = kzalloc_node(sz, GFP_KERNEL, its->numa_node);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003126 if (alloc_lpis) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003127 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003128 if (lpi_map)
Kees Cook6396bb22018-06-12 14:03:40 -07003129 col_map = kcalloc(nr_lpis, sizeof(*col_map),
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003130 GFP_KERNEL);
3131 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07003132 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003133 nr_lpis = 0;
3134 lpi_base = 0;
3135 }
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003136
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003137 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003138 kfree(dev);
3139 kfree(itt);
3140 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01003141 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003142 return NULL;
3143 }
3144
Vladimir Murzin328191c2016-11-02 11:54:05 +00003145 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01003146
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003147 dev->its = its;
3148 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00003149 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01003150 dev->event_map.lpi_map = lpi_map;
3151 dev->event_map.col_map = col_map;
3152 dev->event_map.lpi_base = lpi_base;
3153 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngier11635fa2019-11-08 16:58:05 +00003154 raw_spin_lock_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003155 dev->device_id = dev_id;
3156 INIT_LIST_HEAD(&dev->entry);
3157
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003158 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003159 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003160 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003161
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003162 /* Map device to its ITT */
3163 its_send_mapd(dev, 1);
3164
3165 return dev;
3166}
3167
3168static void its_free_device(struct its_device *its_dev)
3169{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003170 unsigned long flags;
3171
3172 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003173 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003174 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier898aa5c2019-11-08 16:57:55 +00003175 kfree(its_dev->event_map.col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003176 kfree(its_dev->itt);
3177 kfree(its_dev);
3178}
Marc Zyngierb48ac832014-11-24 14:35:16 +00003179
Marc Zyngier8208d172019-01-18 14:08:59 +00003180static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
Marc Zyngierb48ac832014-11-24 14:35:16 +00003181{
3182 int idx;
3183
Zenghui Yu342be102019-07-27 06:14:22 +00003184 /* Find a free LPI region in lpi_map and allocate them. */
Marc Zyngier8208d172019-01-18 14:08:59 +00003185 idx = bitmap_find_free_region(dev->event_map.lpi_map,
3186 dev->event_map.nr_lpis,
3187 get_count_order(nvecs));
3188 if (idx < 0)
Marc Zyngierb48ac832014-11-24 14:35:16 +00003189 return -ENOSPC;
3190
Marc Zyngier591e5be2015-07-17 10:46:42 +01003191 *hwirq = dev->event_map.lpi_base + idx;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003192
Marc Zyngierb48ac832014-11-24 14:35:16 +00003193 return 0;
3194}
3195
Marc Zyngier54456db2015-07-28 14:46:21 +01003196static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
3197 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00003198{
Marc Zyngierb48ac832014-11-24 14:35:16 +00003199 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003200 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01003201 struct msi_domain_info *msi_info;
3202 u32 dev_id;
Marc Zyngier9791ec72019-01-29 10:02:33 +00003203 int err = 0;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003204
Marc Zyngier54456db2015-07-28 14:46:21 +01003205 /*
Julien Gralla7c90f52019-04-18 16:58:14 +01003206 * We ignore "dev" entirely, and rely on the dev_id that has
Marc Zyngier54456db2015-07-28 14:46:21 +01003207 * been passed via the scratchpad. This limits this domain's
3208 * usefulness to upper layers that definitely know that they
3209 * are built on top of the ITS.
3210 */
3211 dev_id = info->scratchpad[0].ul;
3212
3213 msi_info = msi_get_domain_info(domain);
3214 its = msi_info->data;
3215
Marc Zyngier20b3d542016-12-20 15:23:22 +00003216 if (!gic_rdists->has_direct_lpi &&
3217 vpe_proxy.dev &&
3218 vpe_proxy.dev->its == its &&
3219 dev_id == vpe_proxy.dev->device_id) {
3220 /* Bad luck. Get yourself a better implementation */
3221 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
3222 dev_id);
3223 return -EINVAL;
3224 }
3225
Marc Zyngier9791ec72019-01-29 10:02:33 +00003226 mutex_lock(&its->dev_alloc_lock);
Marc Zyngierf1304202015-07-28 14:46:18 +01003227 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00003228 if (its_dev) {
3229 /*
3230 * We already have seen this ID, probably through
3231 * another alias (PCI bridge of some sort). No need to
3232 * create the device.
3233 */
Marc Zyngier9791ec72019-01-29 10:02:33 +00003234 its_dev->shared = true;
Marc Zyngierf1304202015-07-28 14:46:18 +01003235 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00003236 goto out;
3237 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00003238
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003239 its_dev = its_create_device(its, dev_id, nvec, true);
Marc Zyngier9791ec72019-01-29 10:02:33 +00003240 if (!its_dev) {
3241 err = -ENOMEM;
3242 goto out;
3243 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00003244
Marc Zyngierf1304202015-07-28 14:46:18 +01003245 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00003246out:
Marc Zyngier9791ec72019-01-29 10:02:33 +00003247 mutex_unlock(&its->dev_alloc_lock);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003248 info->scratchpad[0].ptr = its_dev;
Marc Zyngier9791ec72019-01-29 10:02:33 +00003249 return err;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003250}
3251
Marc Zyngier54456db2015-07-28 14:46:21 +01003252static struct msi_domain_ops its_msi_domain_ops = {
3253 .msi_prepare = its_msi_prepare,
3254};
3255
Marc Zyngierb48ac832014-11-24 14:35:16 +00003256static int its_irq_gic_domain_alloc(struct irq_domain *domain,
3257 unsigned int virq,
3258 irq_hw_number_t hwirq)
3259{
Marc Zyngierf833f572015-10-13 12:51:33 +01003260 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003261
Marc Zyngierf833f572015-10-13 12:51:33 +01003262 if (irq_domain_get_of_node(domain->parent)) {
3263 fwspec.fwnode = domain->parent->fwnode;
3264 fwspec.param_count = 3;
3265 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
3266 fwspec.param[1] = hwirq;
3267 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003268 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
3269 fwspec.fwnode = domain->parent->fwnode;
3270 fwspec.param_count = 2;
3271 fwspec.param[0] = hwirq;
3272 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01003273 } else {
3274 return -EINVAL;
3275 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00003276
Marc Zyngierf833f572015-10-13 12:51:33 +01003277 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003278}
3279
3280static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
3281 unsigned int nr_irqs, void *args)
3282{
3283 msi_alloc_info_t *info = args;
3284 struct its_device *its_dev = info->scratchpad[0].ptr;
Julien Grall35ae7df2019-05-01 14:58:21 +01003285 struct its_node *its = its_dev->its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003286 irq_hw_number_t hwirq;
3287 int err;
3288 int i;
3289
Marc Zyngier8208d172019-01-18 14:08:59 +00003290 err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
3291 if (err)
3292 return err;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003293
Julien Grall35ae7df2019-05-01 14:58:21 +01003294 err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev));
3295 if (err)
3296 return err;
3297
Marc Zyngier8208d172019-01-18 14:08:59 +00003298 for (i = 0; i < nr_irqs; i++) {
3299 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003300 if (err)
3301 return err;
3302
3303 irq_domain_set_hwirq_and_chip(domain, virq + i,
Marc Zyngier8208d172019-01-18 14:08:59 +00003304 hwirq + i, &its_irq_chip, its_dev);
Marc Zyngier0d224d32017-08-18 09:39:18 +01003305 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
Marc Zyngierf1304202015-07-28 14:46:18 +01003306 pr_debug("ID:%d pID:%d vID:%d\n",
Marc Zyngier8208d172019-01-18 14:08:59 +00003307 (int)(hwirq + i - its_dev->event_map.lpi_base),
3308 (int)(hwirq + i), virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003309 }
3310
3311 return 0;
3312}
3313
Thomas Gleixner72491642017-09-13 23:29:10 +02003314static int its_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01003315 struct irq_data *d, bool reserve)
Marc Zyngieraca268d2014-12-12 10:51:23 +00003316{
3317 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3318 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003319 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngier0d224d32017-08-18 09:39:18 +01003320 int cpu;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003321
3322 /* get the cpu_mask of local node */
3323 if (its_dev->its->numa_node >= 0)
3324 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00003325
Marc Zyngier591e5be2015-07-17 10:46:42 +01003326 /* Bind the LPI to the first possible CPU */
Yang Yingliangc1797b12018-06-22 10:52:51 +01003327 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
3328 if (cpu >= nr_cpu_ids) {
3329 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
3330 return -EINVAL;
3331
3332 cpu = cpumask_first(cpu_online_mask);
3333 }
3334
Marc Zyngier0d224d32017-08-18 09:39:18 +01003335 its_dev->event_map.col_map[event] = cpu;
3336 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngier591e5be2015-07-17 10:46:42 +01003337
Marc Zyngieraca268d2014-12-12 10:51:23 +00003338 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00003339 its_send_mapti(its_dev, d->hwirq, event);
Thomas Gleixner72491642017-09-13 23:29:10 +02003340 return 0;
Marc Zyngieraca268d2014-12-12 10:51:23 +00003341}
3342
3343static void its_irq_domain_deactivate(struct irq_domain *domain,
3344 struct irq_data *d)
3345{
3346 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3347 u32 event = its_get_event_id(d);
3348
3349 /* Stop the delivery of interrupts */
3350 its_send_discard(its_dev, event);
3351}
3352
Marc Zyngierb48ac832014-11-24 14:35:16 +00003353static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
3354 unsigned int nr_irqs)
3355{
3356 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
3357 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier9791ec72019-01-29 10:02:33 +00003358 struct its_node *its = its_dev->its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003359 int i;
3360
Marc Zyngierc9c96e32019-09-05 14:56:47 +01003361 bitmap_release_region(its_dev->event_map.lpi_map,
3362 its_get_event_id(irq_domain_get_irq_data(domain, virq)),
3363 get_count_order(nr_irqs));
3364
Marc Zyngierb48ac832014-11-24 14:35:16 +00003365 for (i = 0; i < nr_irqs; i++) {
3366 struct irq_data *data = irq_domain_get_irq_data(domain,
3367 virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003368 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00003369 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003370 }
3371
Marc Zyngier9791ec72019-01-29 10:02:33 +00003372 mutex_lock(&its->dev_alloc_lock);
3373
3374 /*
3375 * If all interrupts have been freed, start mopping the
3376 * floor. This is conditionned on the device not being shared.
3377 */
3378 if (!its_dev->shared &&
3379 bitmap_empty(its_dev->event_map.lpi_map,
Marc Zyngier591e5be2015-07-17 10:46:42 +01003380 its_dev->event_map.nr_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003381 its_lpi_free(its_dev->event_map.lpi_map,
3382 its_dev->event_map.lpi_base,
3383 its_dev->event_map.nr_lpis);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003384
3385 /* Unmap device/itt */
3386 its_send_mapd(its_dev, 0);
3387 its_free_device(its_dev);
3388 }
3389
Marc Zyngier9791ec72019-01-29 10:02:33 +00003390 mutex_unlock(&its->dev_alloc_lock);
3391
Marc Zyngierb48ac832014-11-24 14:35:16 +00003392 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3393}
3394
3395static const struct irq_domain_ops its_domain_ops = {
3396 .alloc = its_irq_domain_alloc,
3397 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00003398 .activate = its_irq_domain_activate,
3399 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00003400};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003401
Marc Zyngier20b3d542016-12-20 15:23:22 +00003402/*
3403 * This is insane.
3404 *
Marc Zyngier0684c702019-12-24 11:10:30 +00003405 * If a GICv4.0 doesn't implement Direct LPIs (which is extremely
Marc Zyngier20b3d542016-12-20 15:23:22 +00003406 * likely), the only way to perform an invalidate is to use a fake
3407 * device to issue an INV command, implying that the LPI has first
3408 * been mapped to some event on that device. Since this is not exactly
3409 * cheap, we try to keep that mapping around as long as possible, and
3410 * only issue an UNMAP if we're short on available slots.
3411 *
3412 * Broken by design(tm).
Marc Zyngier0684c702019-12-24 11:10:30 +00003413 *
3414 * GICv4.1, on the other hand, mandates that we're able to invalidate
3415 * by writing to a MMIO register. It doesn't implement the whole of
3416 * DirectLPI, but that's good enough. And most of the time, we don't
3417 * even have to invalidate anything, as the redistributor can be told
3418 * whether to generate a doorbell or not (we thus leave it enabled,
3419 * always).
Marc Zyngier20b3d542016-12-20 15:23:22 +00003420 */
3421static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
3422{
Marc Zyngier0684c702019-12-24 11:10:30 +00003423 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3424 if (gic_rdists->has_rvpeid)
3425 return;
3426
Marc Zyngier20b3d542016-12-20 15:23:22 +00003427 /* Already unmapped? */
3428 if (vpe->vpe_proxy_event == -1)
3429 return;
3430
3431 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
3432 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
3433
3434 /*
3435 * We don't track empty slots at all, so let's move the
3436 * next_victim pointer if we can quickly reuse that slot
3437 * instead of nuking an existing entry. Not clear that this is
3438 * always a win though, and this might just generate a ripple
3439 * effect... Let's just hope VPEs don't migrate too often.
3440 */
3441 if (vpe_proxy.vpes[vpe_proxy.next_victim])
3442 vpe_proxy.next_victim = vpe->vpe_proxy_event;
3443
3444 vpe->vpe_proxy_event = -1;
3445}
3446
3447static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
3448{
Marc Zyngier0684c702019-12-24 11:10:30 +00003449 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3450 if (gic_rdists->has_rvpeid)
3451 return;
3452
Marc Zyngier20b3d542016-12-20 15:23:22 +00003453 if (!gic_rdists->has_direct_lpi) {
3454 unsigned long flags;
3455
3456 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3457 its_vpe_db_proxy_unmap_locked(vpe);
3458 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3459 }
3460}
3461
3462static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
3463{
Marc Zyngier0684c702019-12-24 11:10:30 +00003464 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3465 if (gic_rdists->has_rvpeid)
3466 return;
3467
Marc Zyngier20b3d542016-12-20 15:23:22 +00003468 /* Already mapped? */
3469 if (vpe->vpe_proxy_event != -1)
3470 return;
3471
3472 /* This slot was already allocated. Kick the other VPE out. */
3473 if (vpe_proxy.vpes[vpe_proxy.next_victim])
3474 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
3475
3476 /* Map the new VPE instead */
3477 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
3478 vpe->vpe_proxy_event = vpe_proxy.next_victim;
3479 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
3480
3481 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
3482 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
3483}
3484
Marc Zyngier958b90d2017-08-18 16:14:17 +01003485static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
3486{
3487 unsigned long flags;
3488 struct its_collection *target_col;
3489
Marc Zyngier0684c702019-12-24 11:10:30 +00003490 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3491 if (gic_rdists->has_rvpeid)
3492 return;
3493
Marc Zyngier958b90d2017-08-18 16:14:17 +01003494 if (gic_rdists->has_direct_lpi) {
3495 void __iomem *rdbase;
3496
3497 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
3498 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
Marc Zyngier2f4f0642019-11-08 16:57:56 +00003499 wait_for_syncr(rdbase);
Marc Zyngier958b90d2017-08-18 16:14:17 +01003500
3501 return;
3502 }
3503
3504 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3505
3506 its_vpe_db_proxy_map_locked(vpe);
3507
3508 target_col = &vpe_proxy.dev->its->collections[to];
3509 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
3510 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
3511
3512 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3513}
3514
Marc Zyngier3171a472016-12-20 15:17:28 +00003515static int its_vpe_set_affinity(struct irq_data *d,
3516 const struct cpumask *mask_val,
3517 bool force)
3518{
3519 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003520 int from, cpu = cpumask_first(mask_val);
Marc Zyngierf3a059212020-03-04 20:33:10 +00003521 unsigned long flags;
Marc Zyngier3171a472016-12-20 15:17:28 +00003522
3523 /*
3524 * Changing affinity is mega expensive, so let's be as lazy as
Marc Zyngier20b3d542016-12-20 15:23:22 +00003525 * we can and only do it if we really have to. Also, if mapped
Marc Zyngier958b90d2017-08-18 16:14:17 +01003526 * into the proxy device, we need to move the doorbell
3527 * interrupt to its new location.
Marc Zyngierf3a059212020-03-04 20:33:10 +00003528 *
3529 * Another thing is that changing the affinity of a vPE affects
3530 * *other interrupts* such as all the vLPIs that are routed to
3531 * this vPE. This means that the irq_desc lock is not enough to
3532 * protect us, and that we must ensure nobody samples vpe->col_idx
3533 * during the update, hence the lock below which must also be
3534 * taken on any vLPI handling path that evaluates vpe->col_idx.
Marc Zyngier3171a472016-12-20 15:17:28 +00003535 */
Marc Zyngierf3a059212020-03-04 20:33:10 +00003536 from = vpe_to_cpuid_lock(vpe, &flags);
3537 if (from == cpu)
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003538 goto out;
Marc Zyngier958b90d2017-08-18 16:14:17 +01003539
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003540 vpe->col_idx = cpu;
Marc Zyngier3171a472016-12-20 15:17:28 +00003541
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003542 /*
3543 * GICv4.1 allows us to skip VMOVP if moving to a cpu whose RD
3544 * is sharing its VPE table with the current one.
3545 */
3546 if (gic_data_rdist_cpu(cpu)->vpe_table_mask &&
3547 cpumask_test_cpu(from, gic_data_rdist_cpu(cpu)->vpe_table_mask))
3548 goto out;
3549
3550 its_send_vmovp(vpe);
3551 its_vpe_db_proxy_move(vpe, from, cpu);
3552
3553out:
Marc Zyngier44c4c252017-10-19 10:11:34 +01003554 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngierf3a059212020-03-04 20:33:10 +00003555 vpe_to_cpuid_unlock(vpe, flags);
Marc Zyngier44c4c252017-10-19 10:11:34 +01003556
Marc Zyngier3171a472016-12-20 15:17:28 +00003557 return IRQ_SET_MASK_OK_DONE;
3558}
3559
Marc Zyngiere643d802016-12-20 15:09:31 +00003560static void its_vpe_schedule(struct its_vpe *vpe)
3561{
Robin Murphy50c33092018-02-16 16:57:56 +00003562 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00003563 u64 val;
3564
3565 /* Schedule the VPE */
3566 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
3567 GENMASK_ULL(51, 12);
3568 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
3569 val |= GICR_VPROPBASER_RaWb;
3570 val |= GICR_VPROPBASER_InnerShareable;
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003571 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
Marc Zyngiere643d802016-12-20 15:09:31 +00003572
3573 val = virt_to_phys(page_address(vpe->vpt_page)) &
3574 GENMASK_ULL(51, 16);
3575 val |= GICR_VPENDBASER_RaWaWb;
3576 val |= GICR_VPENDBASER_NonShareable;
3577 /*
3578 * There is no good way of finding out if the pending table is
3579 * empty as we can race against the doorbell interrupt very
3580 * easily. So in the end, vpe->pending_last is only an
3581 * indication that the vcpu has something pending, not one
3582 * that the pending table is empty. A good implementation
3583 * would be able to read its coarse map pretty quickly anyway,
3584 * making this a tolerable issue.
3585 */
3586 val |= GICR_VPENDBASER_PendingLast;
3587 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
3588 val |= GICR_VPENDBASER_Valid;
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003589 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
Marc Zyngiere643d802016-12-20 15:09:31 +00003590}
3591
3592static void its_vpe_deschedule(struct its_vpe *vpe)
3593{
Robin Murphy50c33092018-02-16 16:57:56 +00003594 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00003595 u64 val;
3596
Marc Zyngiere64fab12019-12-24 11:10:35 +00003597 val = its_clear_vpend_valid(vlpi_base, 0, 0);
Marc Zyngiere643d802016-12-20 15:09:31 +00003598
Marc Zyngiere64fab12019-12-24 11:10:35 +00003599 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
3600 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
Marc Zyngiere643d802016-12-20 15:09:31 +00003601}
3602
Marc Zyngier40619a22017-10-08 15:16:09 +01003603static void its_vpe_invall(struct its_vpe *vpe)
3604{
3605 struct its_node *its;
3606
3607 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00003608 if (!is_v4(its))
Marc Zyngier40619a22017-10-08 15:16:09 +01003609 continue;
3610
Marc Zyngier2247e1b2017-10-08 18:50:36 +01003611 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
3612 continue;
3613
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01003614 /*
3615 * Sending a VINVALL to a single ITS is enough, as all
3616 * we need is to reach the redistributors.
3617 */
Marc Zyngier40619a22017-10-08 15:16:09 +01003618 its_send_vinvall(its, vpe);
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01003619 return;
Marc Zyngier40619a22017-10-08 15:16:09 +01003620 }
3621}
3622
Marc Zyngiere643d802016-12-20 15:09:31 +00003623static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
3624{
3625 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3626 struct its_cmd_info *info = vcpu_info;
3627
3628 switch (info->cmd_type) {
3629 case SCHEDULE_VPE:
3630 its_vpe_schedule(vpe);
3631 return 0;
3632
3633 case DESCHEDULE_VPE:
3634 its_vpe_deschedule(vpe);
3635 return 0;
3636
Marc Zyngier5e2f7642016-12-20 15:10:50 +00003637 case INVALL_VPE:
Marc Zyngier40619a22017-10-08 15:16:09 +01003638 its_vpe_invall(vpe);
Marc Zyngier5e2f7642016-12-20 15:10:50 +00003639 return 0;
3640
Marc Zyngiere643d802016-12-20 15:09:31 +00003641 default:
3642 return -EINVAL;
3643 }
3644}
3645
Marc Zyngier20b3d542016-12-20 15:23:22 +00003646static void its_vpe_send_cmd(struct its_vpe *vpe,
3647 void (*cmd)(struct its_device *, u32))
3648{
3649 unsigned long flags;
3650
3651 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3652
3653 its_vpe_db_proxy_map_locked(vpe);
3654 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
3655
3656 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3657}
3658
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003659static void its_vpe_send_inv(struct irq_data *d)
3660{
3661 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003662
Marc Zyngier20b3d542016-12-20 15:23:22 +00003663 if (gic_rdists->has_direct_lpi) {
3664 void __iomem *rdbase;
3665
Marc Zyngier425c09b2019-11-08 16:57:57 +00003666 /* Target the redistributor this VPE is currently known on */
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003667 raw_spin_lock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003668 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
Marc Zyngier425c09b2019-11-08 16:57:57 +00003669 gic_write_lpir(d->parent_data->hwirq, rdbase + GICR_INVLPIR);
Marc Zyngier2f4f0642019-11-08 16:57:56 +00003670 wait_for_syncr(rdbase);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003671 raw_spin_unlock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003672 } else {
3673 its_vpe_send_cmd(vpe, its_send_inv);
3674 }
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003675}
3676
3677static void its_vpe_mask_irq(struct irq_data *d)
3678{
3679 /*
3680 * We need to unmask the LPI, which is described by the parent
3681 * irq_data. Instead of calling into the parent (which won't
3682 * exactly do the right thing, let's simply use the
3683 * parent_data pointer. Yes, I'm naughty.
3684 */
3685 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
3686 its_vpe_send_inv(d);
3687}
3688
3689static void its_vpe_unmask_irq(struct irq_data *d)
3690{
3691 /* Same hack as above... */
3692 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
3693 its_vpe_send_inv(d);
3694}
3695
Marc Zyngiere57a3e282017-07-31 14:47:24 +01003696static int its_vpe_set_irqchip_state(struct irq_data *d,
3697 enum irqchip_irq_state which,
3698 bool state)
3699{
3700 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3701
3702 if (which != IRQCHIP_STATE_PENDING)
3703 return -EINVAL;
3704
3705 if (gic_rdists->has_direct_lpi) {
3706 void __iomem *rdbase;
3707
3708 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
3709 if (state) {
3710 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
3711 } else {
3712 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
Marc Zyngier2f4f0642019-11-08 16:57:56 +00003713 wait_for_syncr(rdbase);
Marc Zyngiere57a3e282017-07-31 14:47:24 +01003714 }
3715 } else {
3716 if (state)
3717 its_vpe_send_cmd(vpe, its_send_int);
3718 else
3719 its_vpe_send_cmd(vpe, its_send_clear);
3720 }
3721
3722 return 0;
3723}
3724
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003725static struct irq_chip its_vpe_irq_chip = {
3726 .name = "GICv4-vpe",
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003727 .irq_mask = its_vpe_mask_irq,
3728 .irq_unmask = its_vpe_unmask_irq,
3729 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier3171a472016-12-20 15:17:28 +00003730 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngiere57a3e282017-07-31 14:47:24 +01003731 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
Marc Zyngiere643d802016-12-20 15:09:31 +00003732 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003733};
3734
Marc Zyngierd97c97b2019-12-24 11:10:33 +00003735static struct its_node *find_4_1_its(void)
3736{
3737 static struct its_node *its = NULL;
3738
3739 if (!its) {
3740 list_for_each_entry(its, &its_nodes, entry) {
3741 if (is_v4_1(its))
3742 return its;
3743 }
3744
3745 /* Oops? */
3746 its = NULL;
3747 }
3748
3749 return its;
3750}
3751
3752static void its_vpe_4_1_send_inv(struct irq_data *d)
3753{
3754 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3755 struct its_node *its;
3756
3757 /*
3758 * GICv4.1 wants doorbells to be invalidated using the
3759 * INVDB command in order to be broadcast to all RDs. Send
3760 * it to the first valid ITS, and let the HW do its magic.
3761 */
3762 its = find_4_1_its();
3763 if (its)
3764 its_send_invdb(its, vpe);
3765}
3766
3767static void its_vpe_4_1_mask_irq(struct irq_data *d)
3768{
3769 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
3770 its_vpe_4_1_send_inv(d);
3771}
3772
3773static void its_vpe_4_1_unmask_irq(struct irq_data *d)
3774{
3775 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
3776 its_vpe_4_1_send_inv(d);
3777}
3778
Marc Zyngier91bf6392019-12-24 11:10:34 +00003779static void its_vpe_4_1_schedule(struct its_vpe *vpe,
3780 struct its_cmd_info *info)
3781{
3782 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3783 u64 val = 0;
3784
3785 /* Schedule the VPE */
3786 val |= GICR_VPENDBASER_Valid;
3787 val |= info->g0en ? GICR_VPENDBASER_4_1_VGRP0EN : 0;
3788 val |= info->g1en ? GICR_VPENDBASER_4_1_VGRP1EN : 0;
3789 val |= FIELD_PREP(GICR_VPENDBASER_4_1_VPEID, vpe->vpe_id);
3790
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003791 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
Marc Zyngier91bf6392019-12-24 11:10:34 +00003792}
3793
Marc Zyngiere64fab12019-12-24 11:10:35 +00003794static void its_vpe_4_1_deschedule(struct its_vpe *vpe,
3795 struct its_cmd_info *info)
3796{
3797 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3798 u64 val;
3799
3800 if (info->req_db) {
3801 /*
3802 * vPE is going to block: make the vPE non-resident with
3803 * PendingLast clear and DB set. The GIC guarantees that if
3804 * we read-back PendingLast clear, then a doorbell will be
3805 * delivered when an interrupt comes.
3806 */
3807 val = its_clear_vpend_valid(vlpi_base,
3808 GICR_VPENDBASER_PendingLast,
3809 GICR_VPENDBASER_4_1_DB);
3810 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
3811 } else {
3812 /*
3813 * We're not blocking, so just make the vPE non-resident
3814 * with PendingLast set, indicating that we'll be back.
3815 */
3816 val = its_clear_vpend_valid(vlpi_base,
3817 0,
3818 GICR_VPENDBASER_PendingLast);
3819 vpe->pending_last = true;
3820 }
3821}
3822
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003823static void its_vpe_4_1_invall(struct its_vpe *vpe)
3824{
3825 void __iomem *rdbase;
3826 u64 val;
3827
3828 val = GICR_INVALLR_V;
3829 val |= FIELD_PREP(GICR_INVALLR_VPEID, vpe->vpe_id);
3830
3831 /* Target the redistributor this vPE is currently known on */
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003832 raw_spin_lock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003833 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
3834 gic_write_lpir(val, rdbase + GICR_INVALLR);
Zenghui Yub978c252020-03-04 20:33:11 +00003835
3836 wait_for_syncr(rdbase);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003837 raw_spin_unlock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003838}
3839
Marc Zyngier29c647f2019-12-24 11:10:32 +00003840static int its_vpe_4_1_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
3841{
Marc Zyngier91bf6392019-12-24 11:10:34 +00003842 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003843 struct its_cmd_info *info = vcpu_info;
3844
3845 switch (info->cmd_type) {
3846 case SCHEDULE_VPE:
Marc Zyngier91bf6392019-12-24 11:10:34 +00003847 its_vpe_4_1_schedule(vpe, info);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003848 return 0;
3849
3850 case DESCHEDULE_VPE:
Marc Zyngiere64fab12019-12-24 11:10:35 +00003851 its_vpe_4_1_deschedule(vpe, info);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003852 return 0;
3853
3854 case INVALL_VPE:
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003855 its_vpe_4_1_invall(vpe);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003856 return 0;
3857
3858 default:
3859 return -EINVAL;
3860 }
3861}
3862
3863static struct irq_chip its_vpe_4_1_irq_chip = {
3864 .name = "GICv4.1-vpe",
Marc Zyngierd97c97b2019-12-24 11:10:33 +00003865 .irq_mask = its_vpe_4_1_mask_irq,
3866 .irq_unmask = its_vpe_4_1_unmask_irq,
Marc Zyngier29c647f2019-12-24 11:10:32 +00003867 .irq_eoi = irq_chip_eoi_parent,
3868 .irq_set_affinity = its_vpe_set_affinity,
3869 .irq_set_vcpu_affinity = its_vpe_4_1_set_vcpu_affinity,
3870};
3871
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003872static int its_vpe_id_alloc(void)
3873{
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05003874 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003875}
3876
3877static void its_vpe_id_free(u16 id)
3878{
3879 ida_simple_remove(&its_vpeid_ida, id);
3880}
3881
3882static int its_vpe_init(struct its_vpe *vpe)
3883{
3884 struct page *vpt_page;
3885 int vpe_id;
3886
3887 /* Allocate vpe_id */
3888 vpe_id = its_vpe_id_alloc();
3889 if (vpe_id < 0)
3890 return vpe_id;
3891
3892 /* Allocate VPT */
3893 vpt_page = its_allocate_pending_table(GFP_KERNEL);
3894 if (!vpt_page) {
3895 its_vpe_id_free(vpe_id);
3896 return -ENOMEM;
3897 }
3898
3899 if (!its_alloc_vpe_table(vpe_id)) {
3900 its_vpe_id_free(vpe_id);
Nianyao Tang34f8eb92019-07-26 17:32:57 +08003901 its_free_pending_table(vpt_page);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003902 return -ENOMEM;
3903 }
3904
Marc Zyngierf3a059212020-03-04 20:33:10 +00003905 raw_spin_lock_init(&vpe->vpe_lock);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003906 vpe->vpe_id = vpe_id;
3907 vpe->vpt_page = vpt_page;
Marc Zyngier64edfaa2019-12-24 11:10:29 +00003908 if (gic_rdists->has_rvpeid)
3909 atomic_set(&vpe->vmapp_count, 0);
3910 else
3911 vpe->vpe_proxy_event = -1;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003912
3913 return 0;
3914}
3915
3916static void its_vpe_teardown(struct its_vpe *vpe)
3917{
Marc Zyngier20b3d542016-12-20 15:23:22 +00003918 its_vpe_db_proxy_unmap(vpe);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003919 its_vpe_id_free(vpe->vpe_id);
3920 its_free_pending_table(vpe->vpt_page);
3921}
3922
3923static void its_vpe_irq_domain_free(struct irq_domain *domain,
3924 unsigned int virq,
3925 unsigned int nr_irqs)
3926{
3927 struct its_vm *vm = domain->host_data;
3928 int i;
3929
3930 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3931
3932 for (i = 0; i < nr_irqs; i++) {
3933 struct irq_data *data = irq_domain_get_irq_data(domain,
3934 virq + i);
3935 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
3936
3937 BUG_ON(vm != vpe->its_vm);
3938
3939 clear_bit(data->hwirq, vm->db_bitmap);
3940 its_vpe_teardown(vpe);
3941 irq_domain_reset_irq_data(data);
3942 }
3943
3944 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003945 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003946 its_free_prop_table(vm->vprop_page);
3947 }
3948}
3949
3950static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
3951 unsigned int nr_irqs, void *args)
3952{
Marc Zyngier29c647f2019-12-24 11:10:32 +00003953 struct irq_chip *irqchip = &its_vpe_irq_chip;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003954 struct its_vm *vm = args;
3955 unsigned long *bitmap;
3956 struct page *vprop_page;
3957 int base, nr_ids, i, err = 0;
3958
3959 BUG_ON(!vm);
3960
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003961 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003962 if (!bitmap)
3963 return -ENOMEM;
3964
3965 if (nr_ids < nr_irqs) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003966 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003967 return -ENOMEM;
3968 }
3969
3970 vprop_page = its_allocate_prop_table(GFP_KERNEL);
3971 if (!vprop_page) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003972 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003973 return -ENOMEM;
3974 }
3975
3976 vm->db_bitmap = bitmap;
3977 vm->db_lpi_base = base;
3978 vm->nr_db_lpis = nr_ids;
3979 vm->vprop_page = vprop_page;
3980
Marc Zyngier29c647f2019-12-24 11:10:32 +00003981 if (gic_rdists->has_rvpeid)
3982 irqchip = &its_vpe_4_1_irq_chip;
3983
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003984 for (i = 0; i < nr_irqs; i++) {
3985 vm->vpes[i]->vpe_db_lpi = base + i;
3986 err = its_vpe_init(vm->vpes[i]);
3987 if (err)
3988 break;
3989 err = its_irq_gic_domain_alloc(domain, virq + i,
3990 vm->vpes[i]->vpe_db_lpi);
3991 if (err)
3992 break;
3993 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
Marc Zyngier29c647f2019-12-24 11:10:32 +00003994 irqchip, vm->vpes[i]);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003995 set_bit(i, bitmap);
3996 }
3997
3998 if (err) {
3999 if (i > 0)
4000 its_vpe_irq_domain_free(domain, virq, i - 1);
4001
Marc Zyngier38dd7c42018-05-27 17:03:03 +01004002 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004003 its_free_prop_table(vprop_page);
4004 }
4005
4006 return err;
4007}
4008
Thomas Gleixner72491642017-09-13 23:29:10 +02004009static int its_vpe_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01004010 struct irq_data *d, bool reserve)
Marc Zyngiereb781922016-12-20 14:47:05 +00004011{
4012 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier40619a22017-10-08 15:16:09 +01004013 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00004014
Marc Zyngier2247e1b2017-10-08 18:50:36 +01004015 /* If we use the list map, we issue VMAPP on demand... */
4016 if (its_list_map)
Marc Zyngier6ef930f2017-11-07 10:04:38 +00004017 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00004018
4019 /* Map the VPE to the first possible CPU */
4020 vpe->col_idx = cpumask_first(cpu_online_mask);
Marc Zyngier40619a22017-10-08 15:16:09 +01004021
4022 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004023 if (!is_v4(its))
Marc Zyngier40619a22017-10-08 15:16:09 +01004024 continue;
4025
Marc Zyngier75fd9512017-10-08 18:46:39 +01004026 its_send_vmapp(its, vpe, true);
Marc Zyngier40619a22017-10-08 15:16:09 +01004027 its_send_vinvall(its, vpe);
4028 }
4029
Marc Zyngier44c4c252017-10-19 10:11:34 +01004030 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
4031
Thomas Gleixner72491642017-09-13 23:29:10 +02004032 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00004033}
4034
4035static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
4036 struct irq_data *d)
4037{
4038 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier75fd9512017-10-08 18:46:39 +01004039 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00004040
Marc Zyngier2247e1b2017-10-08 18:50:36 +01004041 /*
4042 * If we use the list map, we unmap the VPE once no VLPIs are
4043 * associated with the VM.
4044 */
4045 if (its_list_map)
4046 return;
4047
Marc Zyngier75fd9512017-10-08 18:46:39 +01004048 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004049 if (!is_v4(its))
Marc Zyngier75fd9512017-10-08 18:46:39 +01004050 continue;
4051
4052 its_send_vmapp(its, vpe, false);
4053 }
Marc Zyngiereb781922016-12-20 14:47:05 +00004054}
4055
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004056static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004057 .alloc = its_vpe_irq_domain_alloc,
4058 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00004059 .activate = its_vpe_irq_domain_activate,
4060 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004061};
4062
Yun Wu4559fbb2015-03-06 16:37:50 +00004063static int its_force_quiescent(void __iomem *base)
4064{
4065 u32 count = 1000000; /* 1s */
4066 u32 val;
4067
4068 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07004069 /*
4070 * GIC architecture specification requires the ITS to be both
4071 * disabled and quiescent for writes to GITS_BASER<n> or
4072 * GITS_CBASER to not have UNPREDICTABLE results.
4073 */
4074 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00004075 return 0;
4076
4077 /* Disable the generation of all interrupts to this ITS */
Marc Zyngierd51c4b42017-06-27 21:24:25 +01004078 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
Yun Wu4559fbb2015-03-06 16:37:50 +00004079 writel_relaxed(val, base + GITS_CTLR);
4080
4081 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
4082 while (1) {
4083 val = readl_relaxed(base + GITS_CTLR);
4084 if (val & GITS_CTLR_QUIESCENT)
4085 return 0;
4086
4087 count--;
4088 if (!count)
4089 return -EBUSY;
4090
4091 cpu_relax();
4092 udelay(1);
4093 }
4094}
4095
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004096static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
Robert Richter94100972015-09-21 22:58:38 +02004097{
4098 struct its_node *its = data;
4099
Marc Zyngier576a8342019-11-08 16:58:00 +00004100 /* erratum 22375: only alloc 8MB table size (20 bits) */
4101 its->typer &= ~GITS_TYPER_DEVBITS;
4102 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, 20 - 1);
Robert Richter94100972015-09-21 22:58:38 +02004103 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004104
4105 return true;
Robert Richter94100972015-09-21 22:58:38 +02004106}
4107
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004108static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02004109{
4110 struct its_node *its = data;
4111
4112 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004113
4114 return true;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02004115}
4116
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004117static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
Shanker Donthineni90922a22017-03-07 08:20:38 -06004118{
4119 struct its_node *its = data;
4120
4121 /* On QDF2400, the size of the ITE is 16Bytes */
Marc Zyngierffedbf02019-11-08 16:57:59 +00004122 its->typer &= ~GITS_TYPER_ITT_ENTRY_SIZE;
4123 its->typer |= FIELD_PREP(GITS_TYPER_ITT_ENTRY_SIZE, 16 - 1);
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004124
4125 return true;
Shanker Donthineni90922a22017-03-07 08:20:38 -06004126}
4127
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004128static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
4129{
4130 struct its_node *its = its_dev->its;
4131
4132 /*
4133 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
4134 * which maps 32-bit writes targeted at a separate window of
4135 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
4136 * with device ID taken from bits [device_id_bits + 1:2] of
4137 * the window offset.
4138 */
4139 return its->pre_its_base + (its_dev->device_id << 2);
4140}
4141
4142static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
4143{
4144 struct its_node *its = data;
4145 u32 pre_its_window[2];
4146 u32 ids;
4147
4148 if (!fwnode_property_read_u32_array(its->fwnode_handle,
4149 "socionext,synquacer-pre-its",
4150 pre_its_window,
4151 ARRAY_SIZE(pre_its_window))) {
4152
4153 its->pre_its_base = pre_its_window[0];
4154 its->get_msi_base = its_irq_get_msi_base_pre_its;
4155
4156 ids = ilog2(pre_its_window[1]) - 2;
Marc Zyngier576a8342019-11-08 16:58:00 +00004157 if (device_ids(its) > ids) {
4158 its->typer &= ~GITS_TYPER_DEVBITS;
4159 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, ids - 1);
4160 }
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004161
4162 /* the pre-ITS breaks isolation, so disable MSI remapping */
4163 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
4164 return true;
4165 }
4166 return false;
4167}
4168
Marc Zyngier5c9a8822017-07-28 21:20:37 +01004169static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
4170{
4171 struct its_node *its = data;
4172
4173 /*
4174 * Hip07 insists on using the wrong address for the VLPI
4175 * page. Trick it into doing the right thing...
4176 */
4177 its->vlpi_redist_offset = SZ_128K;
4178 return true;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00004179}
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004180
Robert Richter67510cc2015-09-21 22:58:37 +02004181static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02004182#ifdef CONFIG_CAVIUM_ERRATUM_22375
4183 {
4184 .desc = "ITS: Cavium errata 22375, 24313",
4185 .iidr = 0xa100034c, /* ThunderX pass 1.x */
4186 .mask = 0xffff0fff,
4187 .init = its_enable_quirk_cavium_22375,
4188 },
4189#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02004190#ifdef CONFIG_CAVIUM_ERRATUM_23144
4191 {
4192 .desc = "ITS: Cavium erratum 23144",
4193 .iidr = 0xa100034c, /* ThunderX pass 1.x */
4194 .mask = 0xffff0fff,
4195 .init = its_enable_quirk_cavium_23144,
4196 },
4197#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06004198#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
4199 {
4200 .desc = "ITS: QDF2400 erratum 0065",
4201 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
4202 .mask = 0xffffffff,
4203 .init = its_enable_quirk_qdf2400_e0065,
4204 },
4205#endif
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004206#ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
4207 {
4208 /*
4209 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
4210 * implementation, but with a 'pre-ITS' added that requires
4211 * special handling in software.
4212 */
4213 .desc = "ITS: Socionext Synquacer pre-ITS",
4214 .iidr = 0x0001143b,
4215 .mask = 0xffffffff,
4216 .init = its_enable_quirk_socionext_synquacer,
4217 },
4218#endif
Marc Zyngier5c9a8822017-07-28 21:20:37 +01004219#ifdef CONFIG_HISILICON_ERRATUM_161600802
4220 {
4221 .desc = "ITS: Hip07 erratum 161600802",
4222 .iidr = 0x00000004,
4223 .mask = 0xffffffff,
4224 .init = its_enable_quirk_hip07_161600802,
4225 },
4226#endif
Robert Richter67510cc2015-09-21 22:58:37 +02004227 {
4228 }
4229};
4230
4231static void its_enable_quirks(struct its_node *its)
4232{
4233 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
4234
4235 gic_enable_quirks(iidr, its_quirks, its);
4236}
4237
Derek Basehoredba0bc72018-02-28 21:48:18 -08004238static int its_save_disable(void)
4239{
4240 struct its_node *its;
4241 int err = 0;
4242
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004243 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004244 list_for_each_entry(its, &its_nodes, entry) {
4245 void __iomem *base;
4246
4247 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4248 continue;
4249
4250 base = its->base;
4251 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
4252 err = its_force_quiescent(base);
4253 if (err) {
4254 pr_err("ITS@%pa: failed to quiesce: %d\n",
4255 &its->phys_base, err);
4256 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
4257 goto err;
4258 }
4259
4260 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
4261 }
4262
4263err:
4264 if (err) {
4265 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
4266 void __iomem *base;
4267
4268 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4269 continue;
4270
4271 base = its->base;
4272 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
4273 }
4274 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004275 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004276
4277 return err;
4278}
4279
4280static void its_restore_enable(void)
4281{
4282 struct its_node *its;
4283 int ret;
4284
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004285 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004286 list_for_each_entry(its, &its_nodes, entry) {
4287 void __iomem *base;
4288 int i;
4289
4290 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4291 continue;
4292
4293 base = its->base;
4294
4295 /*
4296 * Make sure that the ITS is disabled. If it fails to quiesce,
4297 * don't restore it since writing to CBASER or BASER<n>
4298 * registers is undefined according to the GIC v3 ITS
4299 * Specification.
4300 */
4301 ret = its_force_quiescent(base);
4302 if (ret) {
4303 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
4304 &its->phys_base, ret);
4305 continue;
4306 }
4307
4308 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
4309
4310 /*
4311 * Writing CBASER resets CREADR to 0, so make CWRITER and
4312 * cmd_write line up with it.
4313 */
4314 its->cmd_write = its->cmd_base;
4315 gits_write_cwriter(0, base + GITS_CWRITER);
4316
4317 /* Restore GITS_BASER from the value cache. */
4318 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
4319 struct its_baser *baser = &its->tables[i];
4320
4321 if (!(baser->val & GITS_BASER_VALID))
4322 continue;
4323
4324 its_write_baser(its, baser, baser->val);
4325 }
4326 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
Derek Basehore920181c2018-02-28 21:48:20 -08004327
4328 /*
4329 * Reinit the collection if it's stored in the ITS. This is
4330 * indicated by the col_id being less than the HCC field.
4331 * CID < HCC as specified in the GIC v3 Documentation.
4332 */
4333 if (its->collections[smp_processor_id()].col_id <
4334 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
4335 its_cpu_init_collection(its);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004336 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004337 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004338}
4339
4340static struct syscore_ops its_syscore_ops = {
4341 .suspend = its_save_disable,
4342 .resume = its_restore_enable,
4343};
4344
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004345static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004346{
4347 struct irq_domain *inner_domain;
4348 struct msi_domain_info *info;
4349
4350 info = kzalloc(sizeof(*info), GFP_KERNEL);
4351 if (!info)
4352 return -ENOMEM;
4353
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004354 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004355 if (!inner_domain) {
4356 kfree(info);
4357 return -ENOMEM;
4358 }
4359
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004360 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01004361 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004362 inner_domain->flags |= its->msi_domain_flags;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004363 info->ops = &its_msi_domain_ops;
4364 info->data = its;
4365 inner_domain->host_data = info;
4366
4367 return 0;
4368}
4369
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004370static int its_init_vpe_domain(void)
4371{
Marc Zyngier20b3d542016-12-20 15:23:22 +00004372 struct its_node *its;
4373 u32 devid;
4374 int entries;
4375
4376 if (gic_rdists->has_direct_lpi) {
4377 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
4378 return 0;
4379 }
4380
4381 /* Any ITS will do, even if not v4 */
4382 its = list_first_entry(&its_nodes, struct its_node, entry);
4383
4384 entries = roundup_pow_of_two(nr_cpu_ids);
Kees Cook6396bb22018-06-12 14:03:40 -07004385 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
Marc Zyngier20b3d542016-12-20 15:23:22 +00004386 GFP_KERNEL);
4387 if (!vpe_proxy.vpes) {
4388 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
4389 return -ENOMEM;
4390 }
4391
4392 /* Use the last possible DevID */
Marc Zyngier576a8342019-11-08 16:58:00 +00004393 devid = GENMASK(device_ids(its) - 1, 0);
Marc Zyngier20b3d542016-12-20 15:23:22 +00004394 vpe_proxy.dev = its_create_device(its, devid, entries, false);
4395 if (!vpe_proxy.dev) {
4396 kfree(vpe_proxy.vpes);
4397 pr_err("ITS: Can't allocate GICv4 proxy device\n");
4398 return -ENOMEM;
4399 }
4400
Shanker Donthinenic427a472017-09-23 13:50:19 -05004401 BUG_ON(entries > vpe_proxy.dev->nr_ites);
Marc Zyngier20b3d542016-12-20 15:23:22 +00004402
4403 raw_spin_lock_init(&vpe_proxy.lock);
4404 vpe_proxy.next_victim = 0;
4405 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
4406 devid, vpe_proxy.dev->nr_ites);
4407
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004408 return 0;
4409}
4410
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004411static int __init its_compute_its_list_map(struct resource *res,
4412 void __iomem *its_base)
4413{
4414 int its_number;
4415 u32 ctlr;
4416
4417 /*
4418 * This is assumed to be done early enough that we're
4419 * guaranteed to be single-threaded, hence no
4420 * locking. Should this change, we should address
4421 * this.
4422 */
Marc Zyngierab604912017-10-08 18:48:06 +01004423 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
4424 if (its_number >= GICv4_ITS_LIST_MAX) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004425 pr_err("ITS@%pa: No ITSList entry available!\n",
4426 &res->start);
4427 return -EINVAL;
4428 }
4429
4430 ctlr = readl_relaxed(its_base + GITS_CTLR);
4431 ctlr &= ~GITS_CTLR_ITS_NUMBER;
4432 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
4433 writel_relaxed(ctlr, its_base + GITS_CTLR);
4434 ctlr = readl_relaxed(its_base + GITS_CTLR);
4435 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
4436 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
4437 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
4438 }
4439
4440 if (test_and_set_bit(its_number, &its_list_map)) {
4441 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
4442 &res->start, its_number);
4443 return -EINVAL;
4444 }
4445
4446 return its_number;
4447}
4448
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004449static int __init its_probe_one(struct resource *res,
4450 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004451{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004452 struct its_node *its;
4453 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004454 u32 val, ctlr;
4455 u64 baser, tmp, typer;
Shanker Donthineni539d3782019-01-14 09:50:19 +00004456 struct page *page;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004457 int err;
4458
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004459 its_base = ioremap(res->start, resource_size(res));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004460 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004461 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004462 return -ENOMEM;
4463 }
4464
4465 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
4466 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004467 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004468 err = -ENODEV;
4469 goto out_unmap;
4470 }
4471
Yun Wu4559fbb2015-03-06 16:37:50 +00004472 err = its_force_quiescent(its_base);
4473 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004474 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00004475 goto out_unmap;
4476 }
4477
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004478 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004479
4480 its = kzalloc(sizeof(*its), GFP_KERNEL);
4481 if (!its) {
4482 err = -ENOMEM;
4483 goto out_unmap;
4484 }
4485
4486 raw_spin_lock_init(&its->lock);
Marc Zyngier9791ec72019-01-29 10:02:33 +00004487 mutex_init(&its->dev_alloc_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004488 INIT_LIST_HEAD(&its->entry);
4489 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004490 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004491 its->typer = typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004492 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004493 its->phys_base = res->start;
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004494 if (is_v4(its)) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004495 if (!(typer & GITS_TYPER_VMOVP)) {
4496 err = its_compute_its_list_map(res, its_base);
4497 if (err < 0)
4498 goto out_free_its;
4499
Marc Zyngierdebf6d02017-10-08 18:44:42 +01004500 its->list_nr = err;
4501
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004502 pr_info("ITS@%pa: Using ITS number %d\n",
4503 &res->start, err);
4504 } else {
4505 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
4506 }
Marc Zyngier5e516842019-12-24 11:10:28 +00004507
4508 if (is_v4_1(its)) {
4509 u32 svpet = FIELD_GET(GITS_TYPER_SVPET, typer);
4510 its->mpidr = readl_relaxed(its_base + GITS_MPIDR);
4511
4512 pr_info("ITS@%pa: Using GICv4.1 mode %08x %08x\n",
4513 &res->start, its->mpidr, svpet);
4514 }
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004515 }
4516
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004517 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004518
Shanker Donthineni539d3782019-01-14 09:50:19 +00004519 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
4520 get_order(ITS_CMD_QUEUE_SZ));
4521 if (!page) {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004522 err = -ENOMEM;
4523 goto out_free_its;
4524 }
Shanker Donthineni539d3782019-01-14 09:50:19 +00004525 its->cmd_base = (void *)page_address(page);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004526 its->cmd_write = its->cmd_base;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004527 its->fwnode_handle = handle;
4528 its->get_msi_base = its_irq_get_msi_base;
4529 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004530
Robert Richter67510cc2015-09-21 22:58:37 +02004531 its_enable_quirks(its);
4532
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05004533 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004534 if (err)
4535 goto out_free_cmd;
4536
4537 err = its_alloc_collections(its);
4538 if (err)
4539 goto out_free_tables;
4540
4541 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06004542 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004543 GITS_CBASER_InnerShareable |
4544 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
4545 GITS_CBASER_VALID);
4546
Vladimir Murzin0968a612016-11-02 11:54:06 +00004547 gits_write_cbaser(baser, its->base + GITS_CBASER);
4548 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004549
Marc Zyngier4ad3e362015-03-27 14:15:04 +00004550 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00004551 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
4552 /*
4553 * The HW reports non-shareable, we must
4554 * remove the cacheability attributes as
4555 * well.
4556 */
4557 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
4558 GITS_CBASER_CACHEABILITY_MASK);
4559 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00004560 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00004561 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004562 pr_info("ITS: using cache flushing for cmd queue\n");
4563 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
4564 }
4565
Vladimir Murzin0968a612016-11-02 11:54:06 +00004566 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004567 ctlr = readl_relaxed(its->base + GITS_CTLR);
Marc Zyngierd51c4b42017-06-27 21:24:25 +01004568 ctlr |= GITS_CTLR_ENABLE;
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004569 if (is_v4(its))
Marc Zyngierd51c4b42017-06-27 21:24:25 +01004570 ctlr |= GITS_CTLR_ImDe;
4571 writel_relaxed(ctlr, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00004572
Derek Basehoredba0bc72018-02-28 21:48:18 -08004573 if (GITS_TYPER_HCC(typer))
4574 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
4575
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004576 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004577 if (err)
4578 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004579
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004580 raw_spin_lock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004581 list_add(&its->entry, &its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004582 raw_spin_unlock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004583
4584 return 0;
4585
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004586out_free_tables:
4587 its_free_tables(its);
4588out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01004589 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004590out_free_its:
4591 kfree(its);
4592out_unmap:
4593 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004594 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004595 return err;
4596}
4597
4598static bool gic_rdists_supports_plpis(void)
4599{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01004600 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004601}
4602
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05004603static int redist_disable_lpis(void)
4604{
4605 void __iomem *rbase = gic_data_rdist_rd_base();
4606 u64 timeout = USEC_PER_SEC;
4607 u64 val;
4608
4609 if (!gic_rdists_supports_plpis()) {
4610 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
4611 return -ENXIO;
4612 }
4613
4614 val = readl_relaxed(rbase + GICR_CTLR);
4615 if (!(val & GICR_CTLR_ENABLE_LPIS))
4616 return 0;
4617
Marc Zyngier11e37d32018-07-27 13:38:54 +01004618 /*
4619 * If coming via a CPU hotplug event, we don't need to disable
4620 * LPIs before trying to re-enable them. They are already
4621 * configured and all is well in the world.
Marc Zyngierc440a9d2018-07-27 15:40:13 +01004622 *
4623 * If running with preallocated tables, there is nothing to do.
Marc Zyngier11e37d32018-07-27 13:38:54 +01004624 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01004625 if (gic_data_rdist()->lpi_enabled ||
4626 (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED))
Marc Zyngier11e37d32018-07-27 13:38:54 +01004627 return 0;
4628
4629 /*
4630 * From that point on, we only try to do some damage control.
4631 */
4632 pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05004633 smp_processor_id());
4634 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
4635
4636 /* Disable LPIs */
4637 val &= ~GICR_CTLR_ENABLE_LPIS;
4638 writel_relaxed(val, rbase + GICR_CTLR);
4639
4640 /* Make sure any change to GICR_CTLR is observable by the GIC */
4641 dsb(sy);
4642
4643 /*
4644 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
4645 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
4646 * Error out if we time out waiting for RWP to clear.
4647 */
4648 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
4649 if (!timeout) {
4650 pr_err("CPU%d: Timeout while disabling LPIs\n",
4651 smp_processor_id());
4652 return -ETIMEDOUT;
4653 }
4654 udelay(1);
4655 timeout--;
4656 }
4657
4658 /*
4659 * After it has been written to 1, it is IMPLEMENTATION
4660 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
4661 * cleared to 0. Error out if clearing the bit failed.
4662 */
4663 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
4664 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
4665 return -EBUSY;
4666 }
4667
4668 return 0;
4669}
4670
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004671int its_cpu_init(void)
4672{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004673 if (!list_empty(&its_nodes)) {
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05004674 int ret;
4675
4676 ret = redist_disable_lpis();
4677 if (ret)
4678 return ret;
4679
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004680 its_cpu_init_lpis();
Derek Basehore920181c2018-02-28 21:48:20 -08004681 its_cpu_init_collections();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004682 }
4683
4684 return 0;
4685}
4686
Arvind Yadav935bba72017-06-22 16:05:30 +05304687static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004688 { .compatible = "arm,gic-v3-its", },
4689 {},
4690};
4691
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004692static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004693{
4694 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004695 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004696
4697 for (np = of_find_matching_node(node, its_device_id); np;
4698 np = of_find_matching_node(np, its_device_id)) {
Stephen Boyd95a25622018-02-01 09:03:29 -08004699 if (!of_device_is_available(np))
4700 continue;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004701 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05004702 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
4703 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004704 continue;
4705 }
4706
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004707 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05004708 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004709 continue;
4710 }
4711
4712 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004713 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004714 return 0;
4715}
4716
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004717#ifdef CONFIG_ACPI
4718
4719#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
4720
Robert Richterd1ce2632017-07-12 15:25:09 +02004721#ifdef CONFIG_ACPI_NUMA
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304722struct its_srat_map {
4723 /* numa node id */
4724 u32 numa_node;
4725 /* GIC ITS ID */
4726 u32 its_id;
4727};
4728
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004729static struct its_srat_map *its_srat_maps __initdata;
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304730static int its_in_srat __initdata;
4731
4732static int __init acpi_get_its_numa_node(u32 its_id)
4733{
4734 int i;
4735
4736 for (i = 0; i < its_in_srat; i++) {
4737 if (its_id == its_srat_maps[i].its_id)
4738 return its_srat_maps[i].numa_node;
4739 }
4740 return NUMA_NO_NODE;
4741}
4742
Keith Busch60574d12019-03-11 14:55:57 -06004743static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header,
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004744 const unsigned long end)
4745{
4746 return 0;
4747}
4748
Keith Busch60574d12019-03-11 14:55:57 -06004749static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header,
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304750 const unsigned long end)
4751{
4752 int node;
4753 struct acpi_srat_gic_its_affinity *its_affinity;
4754
4755 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
4756 if (!its_affinity)
4757 return -EINVAL;
4758
4759 if (its_affinity->header.length < sizeof(*its_affinity)) {
4760 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
4761 its_affinity->header.length);
4762 return -EINVAL;
4763 }
4764
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304765 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
4766
4767 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
4768 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
4769 return 0;
4770 }
4771
4772 its_srat_maps[its_in_srat].numa_node = node;
4773 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
4774 its_in_srat++;
4775 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
4776 its_affinity->proximity_domain, its_affinity->its_id, node);
4777
4778 return 0;
4779}
4780
4781static void __init acpi_table_parse_srat_its(void)
4782{
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004783 int count;
4784
4785 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
4786 sizeof(struct acpi_table_srat),
4787 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
4788 gic_acpi_match_srat_its, 0);
4789 if (count <= 0)
4790 return;
4791
Kees Cook6da2ec52018-06-12 13:55:00 -07004792 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
4793 GFP_KERNEL);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004794 if (!its_srat_maps) {
4795 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
4796 return;
4797 }
4798
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304799 acpi_table_parse_entries(ACPI_SIG_SRAT,
4800 sizeof(struct acpi_table_srat),
4801 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
4802 gic_acpi_parse_srat_its, 0);
4803}
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004804
4805/* free the its_srat_maps after ITS probing */
4806static void __init acpi_its_srat_maps_free(void)
4807{
4808 kfree(its_srat_maps);
4809}
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304810#else
4811static void __init acpi_table_parse_srat_its(void) { }
4812static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004813static void __init acpi_its_srat_maps_free(void) { }
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304814#endif
4815
Keith Busch60574d12019-03-11 14:55:57 -06004816static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header,
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004817 const unsigned long end)
4818{
4819 struct acpi_madt_generic_translator *its_entry;
4820 struct fwnode_handle *dom_handle;
4821 struct resource res;
4822 int err;
4823
4824 its_entry = (struct acpi_madt_generic_translator *)header;
4825 memset(&res, 0, sizeof(res));
4826 res.start = its_entry->base_address;
4827 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
4828 res.flags = IORESOURCE_MEM;
4829
Marc Zyngier5778cc72019-07-31 16:13:42 +01004830 dom_handle = irq_domain_alloc_fwnode(&res.start);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004831 if (!dom_handle) {
4832 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
4833 &res.start);
4834 return -ENOMEM;
4835 }
4836
Shameer Kolothum8b4282e2018-02-13 15:20:50 +00004837 err = iort_register_domain_token(its_entry->translation_id, res.start,
4838 dom_handle);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004839 if (err) {
4840 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
4841 &res.start, its_entry->translation_id);
4842 goto dom_err;
4843 }
4844
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304845 err = its_probe_one(&res, dom_handle,
4846 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004847 if (!err)
4848 return 0;
4849
4850 iort_deregister_domain_token(its_entry->translation_id);
4851dom_err:
4852 irq_domain_free_fwnode(dom_handle);
4853 return err;
4854}
4855
4856static void __init its_acpi_probe(void)
4857{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304858 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004859 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
4860 gic_acpi_parse_madt_its, 0);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004861 acpi_its_srat_maps_free();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004862}
4863#else
4864static void __init its_acpi_probe(void) { }
4865#endif
4866
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004867int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
4868 struct irq_domain *parent_domain)
4869{
4870 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004871 struct its_node *its;
4872 bool has_v4 = false;
Marc Zyngier3c407062020-03-04 20:33:13 +00004873 bool has_v4_1 = false;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004874 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004875
Marc Zyngier5e516842019-12-24 11:10:28 +00004876 gic_rdists = rdists;
4877
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004878 its_parent = parent_domain;
4879 of_node = to_of_node(handle);
4880 if (of_node)
4881 its_of_probe(of_node);
4882 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004883 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004884
4885 if (list_empty(&its_nodes)) {
4886 pr_warn("ITS: No ITS available, not enabling LPIs\n");
4887 return -ENXIO;
4888 }
4889
Marc Zyngier11e37d32018-07-27 13:38:54 +01004890 err = allocate_lpi_tables();
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004891 if (err)
4892 return err;
4893
Marc Zyngier3c407062020-03-04 20:33:13 +00004894 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004895 has_v4 |= is_v4(its);
Marc Zyngier3c407062020-03-04 20:33:13 +00004896 has_v4_1 |= is_v4_1(its);
4897 }
4898
4899 /* Don't bother with inconsistent systems */
4900 if (WARN_ON(!has_v4_1 && rdists->has_rvpeid))
4901 rdists->has_rvpeid = false;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004902
4903 if (has_v4 & rdists->has_vlpis) {
Marc Zyngier3d63cb52016-12-20 15:31:54 +00004904 if (its_init_vpe_domain() ||
4905 its_init_v4(parent_domain, &its_vpe_domain_ops)) {
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004906 rdists->has_vlpis = false;
4907 pr_err("ITS: Disabling GICv4 support\n");
4908 }
4909 }
4910
Derek Basehoredba0bc72018-02-28 21:48:18 -08004911 register_syscore_ops(&its_syscore_ops);
4912
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004913 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004914}