blob: 1259f7f86a211b63e5e11e63ac701fcdc648d421 [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;
Marc Zyngier5e46a482020-03-04 20:33:14 +000099 void __iomem *sgir_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200100 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000101 struct its_cmd_block *cmd_base;
102 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600103 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000104 struct its_collection *collections;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100105 struct fwnode_handle *fwnode_handle;
106 u64 (*get_msi_base)(struct its_device *its_dev);
Marc Zyngier0dd57fe2019-11-08 16:57:58 +0000107 u64 typer;
Derek Basehoredba0bc72018-02-28 21:48:18 -0800108 u64 cbaser_save;
109 u32 ctlr_save;
Marc Zyngier5e516842019-12-24 11:10:28 +0000110 u32 mpidr;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000111 struct list_head its_device_list;
112 u64 flags;
Marc Zyngierdebf6d02017-10-08 18:44:42 +0100113 unsigned long list_nr;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200114 int numa_node;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100115 unsigned int msi_domain_flags;
116 u32 pre_its_base; /* for Socionext Synquacer */
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100117 int vlpi_redist_offset;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000118};
119
Marc Zyngier0dd57fe2019-11-08 16:57:58 +0000120#define is_v4(its) (!!((its)->typer & GITS_TYPER_VLPIS))
Marc Zyngier5e516842019-12-24 11:10:28 +0000121#define is_v4_1(its) (!!((its)->typer & GITS_TYPER_VMAPP))
Marc Zyngier576a8342019-11-08 16:58:00 +0000122#define device_ids(its) (FIELD_GET(GITS_TYPER_DEVBITS, (its)->typer) + 1)
Marc Zyngier0dd57fe2019-11-08 16:57:58 +0000123
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000124#define ITS_ITT_ALIGN SZ_256
125
Shanker Donthineni32bd44d2017-10-07 15:43:48 -0500126/* The maximum number of VPEID bits supported by VLPI commands */
Marc Zyngierf2d83402019-12-24 11:10:25 +0000127#define ITS_MAX_VPEID_BITS \
128 ({ \
129 int nvpeid = 16; \
130 if (gic_rdists->has_rvpeid && \
131 gic_rdists->gicd_typer2 & GICD_TYPER2_VIL) \
132 nvpeid = 1 + (gic_rdists->gicd_typer2 & \
133 GICD_TYPER2_VID); \
134 \
135 nvpeid; \
136 })
Shanker Donthineni32bd44d2017-10-07 15:43:48 -0500137#define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
138
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600139/* Convert page order to size in bytes */
140#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
141
Marc Zyngier591e5be2015-07-17 10:46:42 +0100142struct event_lpi_map {
143 unsigned long *lpi_map;
144 u16 *col_map;
145 irq_hw_number_t lpi_base;
146 int nr_lpis;
Marc Zyngier11635fa2019-11-08 16:58:05 +0000147 raw_spinlock_t vlpi_lock;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000148 struct its_vm *vm;
149 struct its_vlpi_map *vlpi_maps;
150 int nr_vlpis;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100151};
152
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000153/*
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000154 * The ITS view of a device - belongs to an ITS, owns an interrupt
155 * translation table, and a list of interrupts. If it some of its
156 * LPIs are injected into a guest (GICv4), the event_map.vm field
157 * indicates which one.
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000158 */
159struct its_device {
160 struct list_head entry;
161 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100162 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000163 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000164 u32 nr_ites;
165 u32 device_id;
Marc Zyngier9791ec72019-01-29 10:02:33 +0000166 bool shared;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000167};
168
Marc Zyngier20b3d542016-12-20 15:23:22 +0000169static struct {
170 raw_spinlock_t lock;
171 struct its_device *dev;
172 struct its_vpe **vpes;
173 int next_victim;
174} vpe_proxy;
175
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000176static LIST_HEAD(its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +0200177static DEFINE_RAW_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000178static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200179static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000180
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000181static unsigned long its_list_map;
Marc Zyngier3171a472016-12-20 15:17:28 +0000182static u16 vmovp_seq_num;
183static DEFINE_RAW_SPINLOCK(vmovp_lock);
184
Marc Zyngier7d75bbb2016-12-20 13:55:54 +0000185static DEFINE_IDA(its_vpeid_ida);
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000186
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000187#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
Marc Zyngier11e37d32018-07-27 13:38:54 +0100188#define gic_data_rdist_cpu(cpu) (per_cpu_ptr(gic_rdists->rdist, cpu))
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000189#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngiere643d802016-12-20 15:09:31 +0000190#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000191
Marc Zyngier009384b2020-03-04 20:33:23 +0000192/*
193 * Skip ITSs that have no vLPIs mapped, unless we're on GICv4.1, as we
194 * always have vSGIs mapped.
195 */
196static bool require_its_list_vmovp(struct its_vm *vm, struct its_node *its)
197{
198 return (gic_rdists->has_rvpeid || vm->vlpi_count[its->list_nr]);
199}
200
Zenghui Yu84243122019-10-23 03:46:26 +0000201static u16 get_its_list(struct its_vm *vm)
202{
203 struct its_node *its;
204 unsigned long its_list = 0;
205
206 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +0000207 if (!is_v4(its))
Zenghui Yu84243122019-10-23 03:46:26 +0000208 continue;
209
Marc Zyngier009384b2020-03-04 20:33:23 +0000210 if (require_its_list_vmovp(vm, its))
Zenghui Yu84243122019-10-23 03:46:26 +0000211 __set_bit(its->list_nr, &its_list);
212 }
213
214 return (u16)its_list;
215}
216
Marc Zyngier425c09b2019-11-08 16:57:57 +0000217static inline u32 its_get_event_id(struct irq_data *d)
218{
219 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
220 return d->hwirq - its_dev->event_map.lpi_base;
221}
222
Marc Zyngier591e5be2015-07-17 10:46:42 +0100223static struct its_collection *dev_event_to_col(struct its_device *its_dev,
224 u32 event)
225{
226 struct its_node *its = its_dev->its;
227
228 return its->collections + its_dev->event_map.col_map[event];
229}
230
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +0000231static struct its_vlpi_map *dev_event_to_vlpi_map(struct its_device *its_dev,
232 u32 event)
233{
234 if (WARN_ON_ONCE(event >= its_dev->event_map.nr_lpis))
235 return NULL;
236
237 return &its_dev->event_map.vlpi_maps[event];
238}
239
Marc Zyngierf4a81f52019-12-24 11:10:38 +0000240static struct its_vlpi_map *get_vlpi_map(struct irq_data *d)
241{
242 if (irqd_is_forwarded_to_vcpu(d)) {
243 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
244 u32 event = its_get_event_id(d);
245
246 return dev_event_to_vlpi_map(its_dev, event);
247 }
248
249 return NULL;
250}
251
Marc Zyngierf3a059212020-03-04 20:33:10 +0000252static int vpe_to_cpuid_lock(struct its_vpe *vpe, unsigned long *flags)
Marc Zyngier425c09b2019-11-08 16:57:57 +0000253{
Marc Zyngierf3a059212020-03-04 20:33:10 +0000254 raw_spin_lock_irqsave(&vpe->vpe_lock, *flags);
255 return vpe->col_idx;
256}
257
258static void vpe_to_cpuid_unlock(struct its_vpe *vpe, unsigned long flags)
259{
260 raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags);
261}
262
263static int irq_to_cpuid_lock(struct irq_data *d, unsigned long *flags)
264{
265 struct its_vlpi_map *map = get_vlpi_map(d);
266 int cpu;
267
268 if (map) {
269 cpu = vpe_to_cpuid_lock(map->vpe, flags);
270 } else {
271 /* Physical LPIs are already locked via the irq_desc lock */
272 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
273 cpu = its_dev->event_map.col_map[its_get_event_id(d)];
274 /* Keep GCC quiet... */
275 *flags = 0;
276 }
277
278 return cpu;
279}
280
281static void irq_to_cpuid_unlock(struct irq_data *d, unsigned long flags)
282{
Marc Zyngierf4a81f52019-12-24 11:10:38 +0000283 struct its_vlpi_map *map = get_vlpi_map(d);
Marc Zyngier425c09b2019-11-08 16:57:57 +0000284
Marc Zyngierf4a81f52019-12-24 11:10:38 +0000285 if (map)
Marc Zyngierf3a059212020-03-04 20:33:10 +0000286 vpe_to_cpuid_unlock(map->vpe, flags);
Marc Zyngier425c09b2019-11-08 16:57:57 +0000287}
288
Marc Zyngier83559b42018-06-22 10:52:52 +0100289static struct its_collection *valid_col(struct its_collection *col)
290{
Joe Perches20faba82019-07-09 22:04:18 -0700291 if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(15, 0)))
Marc Zyngier83559b42018-06-22 10:52:52 +0100292 return NULL;
293
294 return col;
295}
296
Marc Zyngier205e0652018-06-22 10:52:53 +0100297static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
298{
299 if (valid_col(its->collections + vpe->col_idx))
300 return vpe;
301
302 return NULL;
303}
304
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000305/*
306 * ITS command descriptors - parameters to be encoded in a command
307 * block.
308 */
309struct its_cmd_desc {
310 union {
311 struct {
312 struct its_device *dev;
313 u32 event_id;
314 } its_inv_cmd;
315
316 struct {
317 struct its_device *dev;
318 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000319 } its_clear_cmd;
320
321 struct {
322 struct its_device *dev;
323 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000324 } its_int_cmd;
325
326 struct {
327 struct its_device *dev;
328 int valid;
329 } its_mapd_cmd;
330
331 struct {
332 struct its_collection *col;
333 int valid;
334 } its_mapc_cmd;
335
336 struct {
337 struct its_device *dev;
338 u32 phys_id;
339 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000340 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000341
342 struct {
343 struct its_device *dev;
344 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100345 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000346 } its_movi_cmd;
347
348 struct {
349 struct its_device *dev;
350 u32 event_id;
351 } its_discard_cmd;
352
353 struct {
354 struct its_collection *col;
355 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000356
357 struct {
358 struct its_vpe *vpe;
Marc Zyngiereb781922016-12-20 14:47:05 +0000359 } its_vinvall_cmd;
360
361 struct {
362 struct its_vpe *vpe;
363 struct its_collection *col;
364 bool valid;
365 } its_vmapp_cmd;
366
367 struct {
368 struct its_vpe *vpe;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000369 struct its_device *dev;
370 u32 virt_id;
371 u32 event_id;
372 bool db_enabled;
373 } its_vmapti_cmd;
374
375 struct {
376 struct its_vpe *vpe;
377 struct its_device *dev;
378 u32 event_id;
379 bool db_enabled;
380 } its_vmovi_cmd;
Marc Zyngier3171a472016-12-20 15:17:28 +0000381
382 struct {
383 struct its_vpe *vpe;
384 struct its_collection *col;
385 u16 seq_num;
386 u16 its_list;
387 } its_vmovp_cmd;
Marc Zyngierd97c97b2019-12-24 11:10:33 +0000388
389 struct {
390 struct its_vpe *vpe;
391 } its_invdb_cmd;
Marc Zyngiere252cf82020-03-04 20:33:16 +0000392
393 struct {
394 struct its_vpe *vpe;
395 u8 sgi;
396 u8 priority;
397 bool enable;
398 bool group;
399 bool clear;
400 } its_vsgi_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000401 };
402};
403
404/*
405 * The ITS command block, which is what the ITS actually parses.
406 */
407struct its_cmd_block {
Ben Dooks (Codethink)2bbdfcc2019-10-17 12:29:55 +0100408 union {
409 u64 raw_cmd[4];
410 __le64 raw_cmd_le[4];
411 };
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000412};
413
414#define ITS_CMD_QUEUE_SZ SZ_64K
415#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
416
Marc Zyngier67047f902017-07-28 21:16:58 +0100417typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
418 struct its_cmd_block *,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000419 struct its_cmd_desc *);
420
Marc Zyngier67047f902017-07-28 21:16:58 +0100421typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
422 struct its_cmd_block *,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000423 struct its_cmd_desc *);
424
Marc Zyngier4d36f132016-12-19 17:11:52 +0000425static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
426{
427 u64 mask = GENMASK_ULL(h, l);
428 *raw_cmd &= ~mask;
429 *raw_cmd |= (val << l) & mask;
430}
431
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000432static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
433{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000434 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000435}
436
437static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
438{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000439 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000440}
441
442static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
443{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000444 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000445}
446
447static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
448{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000449 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000450}
451
452static void its_encode_size(struct its_cmd_block *cmd, u8 size)
453{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000454 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000455}
456
457static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
458{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500459 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000460}
461
462static void its_encode_valid(struct its_cmd_block *cmd, int valid)
463{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000464 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000465}
466
467static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
468{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500469 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000470}
471
472static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
473{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000474 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000475}
476
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000477static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
478{
479 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
480}
481
482static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
483{
484 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
485}
486
487static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
488{
489 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
490}
491
492static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
493{
494 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
495}
496
Marc Zyngier3171a472016-12-20 15:17:28 +0000497static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
498{
499 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
500}
501
502static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
503{
504 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
505}
506
Marc Zyngiereb781922016-12-20 14:47:05 +0000507static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
508{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500509 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
Marc Zyngiereb781922016-12-20 14:47:05 +0000510}
511
512static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
513{
514 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
515}
516
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000517static void its_encode_vconf_addr(struct its_cmd_block *cmd, u64 vconf_pa)
518{
519 its_mask_encode(&cmd->raw_cmd[0], vconf_pa >> 16, 51, 16);
520}
521
522static void its_encode_alloc(struct its_cmd_block *cmd, bool alloc)
523{
524 its_mask_encode(&cmd->raw_cmd[0], alloc, 8, 8);
525}
526
527static void its_encode_ptz(struct its_cmd_block *cmd, bool ptz)
528{
529 its_mask_encode(&cmd->raw_cmd[0], ptz, 9, 9);
530}
531
532static void its_encode_vmapp_default_db(struct its_cmd_block *cmd,
533 u32 vpe_db_lpi)
534{
535 its_mask_encode(&cmd->raw_cmd[1], vpe_db_lpi, 31, 0);
536}
537
Marc Zyngierdd3f0502019-12-24 11:10:31 +0000538static void its_encode_vmovp_default_db(struct its_cmd_block *cmd,
539 u32 vpe_db_lpi)
540{
541 its_mask_encode(&cmd->raw_cmd[3], vpe_db_lpi, 31, 0);
542}
543
544static void its_encode_db(struct its_cmd_block *cmd, bool db)
545{
546 its_mask_encode(&cmd->raw_cmd[2], db, 63, 63);
547}
548
Marc Zyngiere252cf82020-03-04 20:33:16 +0000549static void its_encode_sgi_intid(struct its_cmd_block *cmd, u8 sgi)
550{
551 its_mask_encode(&cmd->raw_cmd[0], sgi, 35, 32);
552}
553
554static void its_encode_sgi_priority(struct its_cmd_block *cmd, u8 prio)
555{
556 its_mask_encode(&cmd->raw_cmd[0], prio >> 4, 23, 20);
557}
558
559static void its_encode_sgi_group(struct its_cmd_block *cmd, bool grp)
560{
561 its_mask_encode(&cmd->raw_cmd[0], grp, 10, 10);
562}
563
564static void its_encode_sgi_clear(struct its_cmd_block *cmd, bool clr)
565{
566 its_mask_encode(&cmd->raw_cmd[0], clr, 9, 9);
567}
568
569static void its_encode_sgi_enable(struct its_cmd_block *cmd, bool en)
570{
571 its_mask_encode(&cmd->raw_cmd[0], en, 8, 8);
572}
573
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000574static inline void its_fixup_cmd(struct its_cmd_block *cmd)
575{
576 /* Let's fixup BE commands */
Ben Dooks (Codethink)2bbdfcc2019-10-17 12:29:55 +0100577 cmd->raw_cmd_le[0] = cpu_to_le64(cmd->raw_cmd[0]);
578 cmd->raw_cmd_le[1] = cpu_to_le64(cmd->raw_cmd[1]);
579 cmd->raw_cmd_le[2] = cpu_to_le64(cmd->raw_cmd[2]);
580 cmd->raw_cmd_le[3] = cpu_to_le64(cmd->raw_cmd[3]);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000581}
582
Marc Zyngier67047f902017-07-28 21:16:58 +0100583static struct its_collection *its_build_mapd_cmd(struct its_node *its,
584 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000585 struct its_cmd_desc *desc)
586{
587 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000588 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000589
590 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
591 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
592
593 its_encode_cmd(cmd, GITS_CMD_MAPD);
594 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
595 its_encode_size(cmd, size - 1);
596 its_encode_itt(cmd, itt_addr);
597 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
598
599 its_fixup_cmd(cmd);
600
Marc Zyngier591e5be2015-07-17 10:46:42 +0100601 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000602}
603
Marc Zyngier67047f902017-07-28 21:16:58 +0100604static struct its_collection *its_build_mapc_cmd(struct its_node *its,
605 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000606 struct its_cmd_desc *desc)
607{
608 its_encode_cmd(cmd, GITS_CMD_MAPC);
609 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
610 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
611 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
612
613 its_fixup_cmd(cmd);
614
615 return desc->its_mapc_cmd.col;
616}
617
Marc Zyngier67047f902017-07-28 21:16:58 +0100618static struct its_collection *its_build_mapti_cmd(struct its_node *its,
619 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000620 struct its_cmd_desc *desc)
621{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100622 struct its_collection *col;
623
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000624 col = dev_event_to_col(desc->its_mapti_cmd.dev,
625 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100626
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000627 its_encode_cmd(cmd, GITS_CMD_MAPTI);
628 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
629 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
630 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100631 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000632
633 its_fixup_cmd(cmd);
634
Marc Zyngier83559b42018-06-22 10:52:52 +0100635 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000636}
637
Marc Zyngier67047f902017-07-28 21:16:58 +0100638static struct its_collection *its_build_movi_cmd(struct its_node *its,
639 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000640 struct its_cmd_desc *desc)
641{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100642 struct its_collection *col;
643
644 col = dev_event_to_col(desc->its_movi_cmd.dev,
645 desc->its_movi_cmd.event_id);
646
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000647 its_encode_cmd(cmd, GITS_CMD_MOVI);
648 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100649 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000650 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
651
652 its_fixup_cmd(cmd);
653
Marc Zyngier83559b42018-06-22 10:52:52 +0100654 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000655}
656
Marc Zyngier67047f902017-07-28 21:16:58 +0100657static struct its_collection *its_build_discard_cmd(struct its_node *its,
658 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000659 struct its_cmd_desc *desc)
660{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100661 struct its_collection *col;
662
663 col = dev_event_to_col(desc->its_discard_cmd.dev,
664 desc->its_discard_cmd.event_id);
665
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000666 its_encode_cmd(cmd, GITS_CMD_DISCARD);
667 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
668 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
669
670 its_fixup_cmd(cmd);
671
Marc Zyngier83559b42018-06-22 10:52:52 +0100672 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000673}
674
Marc Zyngier67047f902017-07-28 21:16:58 +0100675static struct its_collection *its_build_inv_cmd(struct its_node *its,
676 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000677 struct its_cmd_desc *desc)
678{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100679 struct its_collection *col;
680
681 col = dev_event_to_col(desc->its_inv_cmd.dev,
682 desc->its_inv_cmd.event_id);
683
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000684 its_encode_cmd(cmd, GITS_CMD_INV);
685 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
686 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
687
688 its_fixup_cmd(cmd);
689
Marc Zyngier83559b42018-06-22 10:52:52 +0100690 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000691}
692
Marc Zyngier67047f902017-07-28 21:16:58 +0100693static struct its_collection *its_build_int_cmd(struct its_node *its,
694 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000695 struct its_cmd_desc *desc)
696{
697 struct its_collection *col;
698
699 col = dev_event_to_col(desc->its_int_cmd.dev,
700 desc->its_int_cmd.event_id);
701
702 its_encode_cmd(cmd, GITS_CMD_INT);
703 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
704 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
705
706 its_fixup_cmd(cmd);
707
Marc Zyngier83559b42018-06-22 10:52:52 +0100708 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000709}
710
Marc Zyngier67047f902017-07-28 21:16:58 +0100711static struct its_collection *its_build_clear_cmd(struct its_node *its,
712 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000713 struct its_cmd_desc *desc)
714{
715 struct its_collection *col;
716
717 col = dev_event_to_col(desc->its_clear_cmd.dev,
718 desc->its_clear_cmd.event_id);
719
720 its_encode_cmd(cmd, GITS_CMD_CLEAR);
721 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
722 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
723
724 its_fixup_cmd(cmd);
725
Marc Zyngier83559b42018-06-22 10:52:52 +0100726 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000727}
728
Marc Zyngier67047f902017-07-28 21:16:58 +0100729static struct its_collection *its_build_invall_cmd(struct its_node *its,
730 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000731 struct its_cmd_desc *desc)
732{
733 its_encode_cmd(cmd, GITS_CMD_INVALL);
Zenghui Yu10794522019-12-02 15:10:21 +0800734 its_encode_collection(cmd, desc->its_invall_cmd.col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000735
736 its_fixup_cmd(cmd);
737
738 return NULL;
739}
740
Marc Zyngier67047f902017-07-28 21:16:58 +0100741static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
742 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000743 struct its_cmd_desc *desc)
744{
745 its_encode_cmd(cmd, GITS_CMD_VINVALL);
746 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
747
748 its_fixup_cmd(cmd);
749
Marc Zyngier205e0652018-06-22 10:52:53 +0100750 return valid_vpe(its, desc->its_vinvall_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000751}
752
Marc Zyngier67047f902017-07-28 21:16:58 +0100753static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
754 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000755 struct its_cmd_desc *desc)
756{
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000757 unsigned long vpt_addr, vconf_addr;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100758 u64 target;
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000759 bool alloc;
Marc Zyngiereb781922016-12-20 14:47:05 +0000760
761 its_encode_cmd(cmd, GITS_CMD_VMAPP);
762 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
763 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000764
765 if (!desc->its_vmapp_cmd.valid) {
766 if (is_v4_1(its)) {
767 alloc = !atomic_dec_return(&desc->its_vmapp_cmd.vpe->vmapp_count);
768 its_encode_alloc(cmd, alloc);
769 }
770
771 goto out;
772 }
773
774 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
775 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
776
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100777 its_encode_target(cmd, target);
Marc Zyngiereb781922016-12-20 14:47:05 +0000778 its_encode_vpt_addr(cmd, vpt_addr);
779 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
780
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000781 if (!is_v4_1(its))
782 goto out;
783
784 vconf_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->its_vm->vprop_page));
785
786 alloc = !atomic_fetch_inc(&desc->its_vmapp_cmd.vpe->vmapp_count);
787
788 its_encode_alloc(cmd, alloc);
789
790 /* We can only signal PTZ when alloc==1. Why do we have two bits? */
791 its_encode_ptz(cmd, alloc);
792 its_encode_vconf_addr(cmd, vconf_addr);
793 its_encode_vmapp_default_db(cmd, desc->its_vmapp_cmd.vpe->vpe_db_lpi);
794
795out:
Marc Zyngiereb781922016-12-20 14:47:05 +0000796 its_fixup_cmd(cmd);
797
Marc Zyngier205e0652018-06-22 10:52:53 +0100798 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000799}
800
Marc Zyngier67047f902017-07-28 21:16:58 +0100801static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
802 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000803 struct its_cmd_desc *desc)
804{
805 u32 db;
806
Marc Zyngier3858d4d2019-12-24 11:10:37 +0000807 if (!is_v4_1(its) && desc->its_vmapti_cmd.db_enabled)
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000808 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
809 else
810 db = 1023;
811
812 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
813 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
814 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
815 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
816 its_encode_db_phys_id(cmd, db);
817 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
818
819 its_fixup_cmd(cmd);
820
Marc Zyngier205e0652018-06-22 10:52:53 +0100821 return valid_vpe(its, desc->its_vmapti_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000822}
823
Marc Zyngier67047f902017-07-28 21:16:58 +0100824static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
825 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000826 struct its_cmd_desc *desc)
827{
828 u32 db;
829
Marc Zyngier3858d4d2019-12-24 11:10:37 +0000830 if (!is_v4_1(its) && desc->its_vmovi_cmd.db_enabled)
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000831 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
832 else
833 db = 1023;
834
835 its_encode_cmd(cmd, GITS_CMD_VMOVI);
836 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
837 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
838 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
839 its_encode_db_phys_id(cmd, db);
840 its_encode_db_valid(cmd, true);
841
842 its_fixup_cmd(cmd);
843
Marc Zyngier205e0652018-06-22 10:52:53 +0100844 return valid_vpe(its, desc->its_vmovi_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000845}
846
Marc Zyngier67047f902017-07-28 21:16:58 +0100847static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
848 struct its_cmd_block *cmd,
Marc Zyngier3171a472016-12-20 15:17:28 +0000849 struct its_cmd_desc *desc)
850{
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100851 u64 target;
852
853 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngier3171a472016-12-20 15:17:28 +0000854 its_encode_cmd(cmd, GITS_CMD_VMOVP);
855 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
856 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
857 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100858 its_encode_target(cmd, target);
Marc Zyngier3171a472016-12-20 15:17:28 +0000859
Marc Zyngierdd3f0502019-12-24 11:10:31 +0000860 if (is_v4_1(its)) {
861 its_encode_db(cmd, true);
862 its_encode_vmovp_default_db(cmd, desc->its_vmovp_cmd.vpe->vpe_db_lpi);
863 }
864
Marc Zyngier3171a472016-12-20 15:17:28 +0000865 its_fixup_cmd(cmd);
866
Marc Zyngier205e0652018-06-22 10:52:53 +0100867 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
Marc Zyngier3171a472016-12-20 15:17:28 +0000868}
869
Marc Zyngier28614692019-11-08 16:58:02 +0000870static struct its_vpe *its_build_vinv_cmd(struct its_node *its,
871 struct its_cmd_block *cmd,
872 struct its_cmd_desc *desc)
873{
874 struct its_vlpi_map *map;
875
876 map = dev_event_to_vlpi_map(desc->its_inv_cmd.dev,
877 desc->its_inv_cmd.event_id);
878
879 its_encode_cmd(cmd, GITS_CMD_INV);
880 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
881 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
882
883 its_fixup_cmd(cmd);
884
885 return valid_vpe(its, map->vpe);
886}
887
Marc Zyngiered0e4aa2019-11-08 16:58:03 +0000888static struct its_vpe *its_build_vint_cmd(struct its_node *its,
889 struct its_cmd_block *cmd,
890 struct its_cmd_desc *desc)
891{
892 struct its_vlpi_map *map;
893
894 map = dev_event_to_vlpi_map(desc->its_int_cmd.dev,
895 desc->its_int_cmd.event_id);
896
897 its_encode_cmd(cmd, GITS_CMD_INT);
898 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
899 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
900
901 its_fixup_cmd(cmd);
902
903 return valid_vpe(its, map->vpe);
904}
905
906static struct its_vpe *its_build_vclear_cmd(struct its_node *its,
907 struct its_cmd_block *cmd,
908 struct its_cmd_desc *desc)
909{
910 struct its_vlpi_map *map;
911
912 map = dev_event_to_vlpi_map(desc->its_clear_cmd.dev,
913 desc->its_clear_cmd.event_id);
914
915 its_encode_cmd(cmd, GITS_CMD_CLEAR);
916 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
917 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
918
919 its_fixup_cmd(cmd);
920
921 return valid_vpe(its, map->vpe);
922}
923
Marc Zyngierd97c97b2019-12-24 11:10:33 +0000924static struct its_vpe *its_build_invdb_cmd(struct its_node *its,
925 struct its_cmd_block *cmd,
926 struct its_cmd_desc *desc)
927{
928 if (WARN_ON(!is_v4_1(its)))
929 return NULL;
930
931 its_encode_cmd(cmd, GITS_CMD_INVDB);
932 its_encode_vpeid(cmd, desc->its_invdb_cmd.vpe->vpe_id);
933
934 its_fixup_cmd(cmd);
935
936 return valid_vpe(its, desc->its_invdb_cmd.vpe);
937}
938
Marc Zyngiere252cf82020-03-04 20:33:16 +0000939static struct its_vpe *its_build_vsgi_cmd(struct its_node *its,
940 struct its_cmd_block *cmd,
941 struct its_cmd_desc *desc)
942{
943 if (WARN_ON(!is_v4_1(its)))
944 return NULL;
945
946 its_encode_cmd(cmd, GITS_CMD_VSGI);
947 its_encode_vpeid(cmd, desc->its_vsgi_cmd.vpe->vpe_id);
948 its_encode_sgi_intid(cmd, desc->its_vsgi_cmd.sgi);
949 its_encode_sgi_priority(cmd, desc->its_vsgi_cmd.priority);
950 its_encode_sgi_group(cmd, desc->its_vsgi_cmd.group);
951 its_encode_sgi_clear(cmd, desc->its_vsgi_cmd.clear);
952 its_encode_sgi_enable(cmd, desc->its_vsgi_cmd.enable);
953
954 its_fixup_cmd(cmd);
955
956 return valid_vpe(its, desc->its_vsgi_cmd.vpe);
957}
958
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000959static u64 its_cmd_ptr_to_offset(struct its_node *its,
960 struct its_cmd_block *ptr)
961{
962 return (ptr - its->cmd_base) * sizeof(*ptr);
963}
964
965static int its_queue_full(struct its_node *its)
966{
967 int widx;
968 int ridx;
969
970 widx = its->cmd_write - its->cmd_base;
971 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
972
973 /* This is incredibly unlikely to happen, unless the ITS locks up. */
974 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
975 return 1;
976
977 return 0;
978}
979
980static struct its_cmd_block *its_allocate_entry(struct its_node *its)
981{
982 struct its_cmd_block *cmd;
983 u32 count = 1000000; /* 1s! */
984
985 while (its_queue_full(its)) {
986 count--;
987 if (!count) {
988 pr_err_ratelimited("ITS queue not draining\n");
989 return NULL;
990 }
991 cpu_relax();
992 udelay(1);
993 }
994
995 cmd = its->cmd_write++;
996
997 /* Handle queue wrapping */
998 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
999 its->cmd_write = its->cmd_base;
1000
Marc Zyngier34d677a2016-12-19 17:16:45 +00001001 /* Clear command */
1002 cmd->raw_cmd[0] = 0;
1003 cmd->raw_cmd[1] = 0;
1004 cmd->raw_cmd[2] = 0;
1005 cmd->raw_cmd[3] = 0;
1006
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001007 return cmd;
1008}
1009
1010static struct its_cmd_block *its_post_commands(struct its_node *its)
1011{
1012 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
1013
1014 writel_relaxed(wr, its->base + GITS_CWRITER);
1015
1016 return its->cmd_write;
1017}
1018
1019static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
1020{
1021 /*
1022 * Make sure the commands written to memory are observable by
1023 * the ITS.
1024 */
1025 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001026 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001027 else
1028 dsb(ishst);
1029}
1030
Marc Zyngiera19b4622017-08-04 17:45:50 +01001031static int its_wait_for_range_completion(struct its_node *its,
Heyi Guoa050fa52019-05-13 19:42:06 +08001032 u64 prev_idx,
Marc Zyngiera19b4622017-08-04 17:45:50 +01001033 struct its_cmd_block *to)
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001034{
Heyi Guoa050fa52019-05-13 19:42:06 +08001035 u64 rd_idx, to_idx, linear_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001036 u32 count = 1000000; /* 1s! */
1037
Heyi Guoa050fa52019-05-13 19:42:06 +08001038 /* Linearize to_idx if the command set has wrapped around */
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001039 to_idx = its_cmd_ptr_to_offset(its, to);
Heyi Guoa050fa52019-05-13 19:42:06 +08001040 if (to_idx < prev_idx)
1041 to_idx += ITS_CMD_QUEUE_SZ;
1042
1043 linear_idx = prev_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001044
1045 while (1) {
Heyi Guoa050fa52019-05-13 19:42:06 +08001046 s64 delta;
1047
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001048 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +01001049
Heyi Guoa050fa52019-05-13 19:42:06 +08001050 /*
1051 * Compute the read pointer progress, taking the
1052 * potential wrap-around into account.
1053 */
1054 delta = rd_idx - prev_idx;
1055 if (rd_idx < prev_idx)
1056 delta += ITS_CMD_QUEUE_SZ;
Marc Zyngier9bdd8b12017-08-19 10:16:02 +01001057
Heyi Guoa050fa52019-05-13 19:42:06 +08001058 linear_idx += delta;
1059 if (linear_idx >= to_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001060 break;
1061
1062 count--;
1063 if (!count) {
Heyi Guoa050fa52019-05-13 19:42:06 +08001064 pr_err_ratelimited("ITS queue timeout (%llu %llu)\n",
1065 to_idx, linear_idx);
Marc Zyngiera19b4622017-08-04 17:45:50 +01001066 return -1;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001067 }
Heyi Guoa050fa52019-05-13 19:42:06 +08001068 prev_idx = rd_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001069 cpu_relax();
1070 udelay(1);
1071 }
Marc Zyngiera19b4622017-08-04 17:45:50 +01001072
1073 return 0;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001074}
1075
Marc Zyngiere4f90942016-12-19 17:56:32 +00001076/* Warning, macro hell follows */
1077#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
1078void name(struct its_node *its, \
1079 buildtype builder, \
1080 struct its_cmd_desc *desc) \
1081{ \
1082 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
1083 synctype *sync_obj; \
1084 unsigned long flags; \
Heyi Guoa050fa52019-05-13 19:42:06 +08001085 u64 rd_idx; \
Marc Zyngiere4f90942016-12-19 17:56:32 +00001086 \
1087 raw_spin_lock_irqsave(&its->lock, flags); \
1088 \
1089 cmd = its_allocate_entry(its); \
1090 if (!cmd) { /* We're soooooo screewed... */ \
1091 raw_spin_unlock_irqrestore(&its->lock, flags); \
1092 return; \
1093 } \
Marc Zyngier67047f902017-07-28 21:16:58 +01001094 sync_obj = builder(its, cmd, desc); \
Marc Zyngiere4f90942016-12-19 17:56:32 +00001095 its_flush_cmd(its, cmd); \
1096 \
1097 if (sync_obj) { \
1098 sync_cmd = its_allocate_entry(its); \
1099 if (!sync_cmd) \
1100 goto post; \
1101 \
Marc Zyngier67047f902017-07-28 21:16:58 +01001102 buildfn(its, sync_cmd, sync_obj); \
Marc Zyngiere4f90942016-12-19 17:56:32 +00001103 its_flush_cmd(its, sync_cmd); \
1104 } \
1105 \
1106post: \
Heyi Guoa050fa52019-05-13 19:42:06 +08001107 rd_idx = readl_relaxed(its->base + GITS_CREADR); \
Marc Zyngiere4f90942016-12-19 17:56:32 +00001108 next_cmd = its_post_commands(its); \
1109 raw_spin_unlock_irqrestore(&its->lock, flags); \
1110 \
Heyi Guoa050fa52019-05-13 19:42:06 +08001111 if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \
Marc Zyngiera19b4622017-08-04 17:45:50 +01001112 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001113}
1114
Marc Zyngier67047f902017-07-28 21:16:58 +01001115static void its_build_sync_cmd(struct its_node *its,
1116 struct its_cmd_block *sync_cmd,
Marc Zyngiere4f90942016-12-19 17:56:32 +00001117 struct its_collection *sync_col)
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001118{
Marc Zyngiere4f90942016-12-19 17:56:32 +00001119 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
1120 its_encode_target(sync_cmd, sync_col->target_address);
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001121
Marc Zyngiere4f90942016-12-19 17:56:32 +00001122 its_fixup_cmd(sync_cmd);
1123}
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001124
Marc Zyngiere4f90942016-12-19 17:56:32 +00001125static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
1126 struct its_collection, its_build_sync_cmd)
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001127
Marc Zyngier67047f902017-07-28 21:16:58 +01001128static void its_build_vsync_cmd(struct its_node *its,
1129 struct its_cmd_block *sync_cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001130 struct its_vpe *sync_vpe)
1131{
1132 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
1133 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001134
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001135 its_fixup_cmd(sync_cmd);
1136}
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001137
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001138static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
1139 struct its_vpe, its_build_vsync_cmd)
1140
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001141static void its_send_int(struct its_device *dev, u32 event_id)
1142{
1143 struct its_cmd_desc desc;
1144
1145 desc.its_int_cmd.dev = dev;
1146 desc.its_int_cmd.event_id = event_id;
1147
1148 its_send_single_command(dev->its, its_build_int_cmd, &desc);
1149}
1150
1151static void its_send_clear(struct its_device *dev, u32 event_id)
1152{
1153 struct its_cmd_desc desc;
1154
1155 desc.its_clear_cmd.dev = dev;
1156 desc.its_clear_cmd.event_id = event_id;
1157
1158 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001159}
1160
1161static void its_send_inv(struct its_device *dev, u32 event_id)
1162{
1163 struct its_cmd_desc desc;
1164
1165 desc.its_inv_cmd.dev = dev;
1166 desc.its_inv_cmd.event_id = event_id;
1167
1168 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
1169}
1170
1171static void its_send_mapd(struct its_device *dev, int valid)
1172{
1173 struct its_cmd_desc desc;
1174
1175 desc.its_mapd_cmd.dev = dev;
1176 desc.its_mapd_cmd.valid = !!valid;
1177
1178 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
1179}
1180
1181static void its_send_mapc(struct its_node *its, struct its_collection *col,
1182 int valid)
1183{
1184 struct its_cmd_desc desc;
1185
1186 desc.its_mapc_cmd.col = col;
1187 desc.its_mapc_cmd.valid = !!valid;
1188
1189 its_send_single_command(its, its_build_mapc_cmd, &desc);
1190}
1191
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001192static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001193{
1194 struct its_cmd_desc desc;
1195
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001196 desc.its_mapti_cmd.dev = dev;
1197 desc.its_mapti_cmd.phys_id = irq_id;
1198 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001199
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001200 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001201}
1202
1203static void its_send_movi(struct its_device *dev,
1204 struct its_collection *col, u32 id)
1205{
1206 struct its_cmd_desc desc;
1207
1208 desc.its_movi_cmd.dev = dev;
1209 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001210 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001211
1212 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
1213}
1214
1215static void its_send_discard(struct its_device *dev, u32 id)
1216{
1217 struct its_cmd_desc desc;
1218
1219 desc.its_discard_cmd.dev = dev;
1220 desc.its_discard_cmd.event_id = id;
1221
1222 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
1223}
1224
1225static void its_send_invall(struct its_node *its, struct its_collection *col)
1226{
1227 struct its_cmd_desc desc;
1228
1229 desc.its_invall_cmd.col = col;
1230
1231 its_send_single_command(its, its_build_invall_cmd, &desc);
1232}
Marc Zyngierc48ed512014-11-24 14:35:12 +00001233
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001234static void its_send_vmapti(struct its_device *dev, u32 id)
1235{
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001236 struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001237 struct its_cmd_desc desc;
1238
1239 desc.its_vmapti_cmd.vpe = map->vpe;
1240 desc.its_vmapti_cmd.dev = dev;
1241 desc.its_vmapti_cmd.virt_id = map->vintid;
1242 desc.its_vmapti_cmd.event_id = id;
1243 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
1244
1245 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
1246}
1247
1248static void its_send_vmovi(struct its_device *dev, u32 id)
1249{
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001250 struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001251 struct its_cmd_desc desc;
1252
1253 desc.its_vmovi_cmd.vpe = map->vpe;
1254 desc.its_vmovi_cmd.dev = dev;
1255 desc.its_vmovi_cmd.event_id = id;
1256 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
1257
1258 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
1259}
1260
Marc Zyngier75fd9512017-10-08 18:46:39 +01001261static void its_send_vmapp(struct its_node *its,
1262 struct its_vpe *vpe, bool valid)
Marc Zyngiereb781922016-12-20 14:47:05 +00001263{
1264 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +00001265
1266 desc.its_vmapp_cmd.vpe = vpe;
1267 desc.its_vmapp_cmd.valid = valid;
Marc Zyngier75fd9512017-10-08 18:46:39 +01001268 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
Marc Zyngiereb781922016-12-20 14:47:05 +00001269
Marc Zyngier75fd9512017-10-08 18:46:39 +01001270 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +00001271}
1272
Marc Zyngier3171a472016-12-20 15:17:28 +00001273static void its_send_vmovp(struct its_vpe *vpe)
1274{
Zenghui Yu84243122019-10-23 03:46:26 +00001275 struct its_cmd_desc desc = {};
Marc Zyngier3171a472016-12-20 15:17:28 +00001276 struct its_node *its;
1277 unsigned long flags;
1278 int col_id = vpe->col_idx;
1279
1280 desc.its_vmovp_cmd.vpe = vpe;
Marc Zyngier3171a472016-12-20 15:17:28 +00001281
1282 if (!its_list_map) {
1283 its = list_first_entry(&its_nodes, struct its_node, entry);
Marc Zyngier3171a472016-12-20 15:17:28 +00001284 desc.its_vmovp_cmd.col = &its->collections[col_id];
1285 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1286 return;
1287 }
1288
1289 /*
1290 * Yet another marvel of the architecture. If using the
1291 * its_list "feature", we need to make sure that all ITSs
1292 * receive all VMOVP commands in the same order. The only way
1293 * to guarantee this is to make vmovp a serialization point.
1294 *
1295 * Wall <-- Head.
1296 */
1297 raw_spin_lock_irqsave(&vmovp_lock, flags);
1298
1299 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
Zenghui Yu84243122019-10-23 03:46:26 +00001300 desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm);
Marc Zyngier3171a472016-12-20 15:17:28 +00001301
1302 /* Emit VMOVPs */
1303 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00001304 if (!is_v4(its))
Marc Zyngier3171a472016-12-20 15:17:28 +00001305 continue;
1306
Marc Zyngier009384b2020-03-04 20:33:23 +00001307 if (!require_its_list_vmovp(vpe->its_vm, its))
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001308 continue;
1309
Marc Zyngier3171a472016-12-20 15:17:28 +00001310 desc.its_vmovp_cmd.col = &its->collections[col_id];
1311 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1312 }
1313
1314 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1315}
1316
Marc Zyngier40619a22017-10-08 15:16:09 +01001317static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
Marc Zyngiereb781922016-12-20 14:47:05 +00001318{
1319 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +00001320
1321 desc.its_vinvall_cmd.vpe = vpe;
Marc Zyngier40619a22017-10-08 15:16:09 +01001322 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +00001323}
1324
Marc Zyngier28614692019-11-08 16:58:02 +00001325static void its_send_vinv(struct its_device *dev, u32 event_id)
1326{
1327 struct its_cmd_desc desc;
1328
1329 /*
1330 * There is no real VINV command. This is just a normal INV,
1331 * with a VSYNC instead of a SYNC.
1332 */
1333 desc.its_inv_cmd.dev = dev;
1334 desc.its_inv_cmd.event_id = event_id;
1335
1336 its_send_single_vcommand(dev->its, its_build_vinv_cmd, &desc);
1337}
1338
Marc Zyngiered0e4aa2019-11-08 16:58:03 +00001339static void its_send_vint(struct its_device *dev, u32 event_id)
1340{
1341 struct its_cmd_desc desc;
1342
1343 /*
1344 * There is no real VINT command. This is just a normal INT,
1345 * with a VSYNC instead of a SYNC.
1346 */
1347 desc.its_int_cmd.dev = dev;
1348 desc.its_int_cmd.event_id = event_id;
1349
1350 its_send_single_vcommand(dev->its, its_build_vint_cmd, &desc);
1351}
1352
1353static void its_send_vclear(struct its_device *dev, u32 event_id)
1354{
1355 struct its_cmd_desc desc;
1356
1357 /*
1358 * There is no real VCLEAR command. This is just a normal CLEAR,
1359 * with a VSYNC instead of a SYNC.
1360 */
1361 desc.its_clear_cmd.dev = dev;
1362 desc.its_clear_cmd.event_id = event_id;
1363
1364 its_send_single_vcommand(dev->its, its_build_vclear_cmd, &desc);
1365}
1366
Marc Zyngierd97c97b2019-12-24 11:10:33 +00001367static void its_send_invdb(struct its_node *its, struct its_vpe *vpe)
1368{
1369 struct its_cmd_desc desc;
1370
1371 desc.its_invdb_cmd.vpe = vpe;
1372 its_send_single_vcommand(its, its_build_invdb_cmd, &desc);
1373}
1374
Marc Zyngierc48ed512014-11-24 14:35:12 +00001375/*
1376 * irqchip functions - assumes MSI, mostly.
1377 */
Marc Zyngier015ec032016-12-20 09:54:57 +00001378static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +00001379{
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001380 struct its_vlpi_map *map = get_vlpi_map(d);
Marc Zyngier015ec032016-12-20 09:54:57 +00001381 irq_hw_number_t hwirq;
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001382 void *va;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001383 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001384
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001385 if (map) {
1386 va = page_address(map->vm->vprop_page);
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001387 hwirq = map->vintid;
1388
1389 /* Remember the updated property */
1390 map->properties &= ~clr;
1391 map->properties |= set | LPI_PROP_GROUP1;
Marc Zyngier015ec032016-12-20 09:54:57 +00001392 } else {
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001393 va = gic_rdists->prop_table_va;
Marc Zyngier015ec032016-12-20 09:54:57 +00001394 hwirq = d->hwirq;
1395 }
Marc Zyngieradcdb942016-12-19 19:18:13 +00001396
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001397 cfg = va + hwirq - 8192;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001398 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +00001399 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001400
1401 /*
1402 * Make the above write visible to the redistributors.
1403 * And yes, we're flushing exactly: One. Single. Byte.
1404 * Humpf...
1405 */
1406 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001407 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001408 else
1409 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +00001410}
1411
Marc Zyngier2f4f0642019-11-08 16:57:56 +00001412static void wait_for_syncr(void __iomem *rdbase)
1413{
1414 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
1415 cpu_relax();
1416}
1417
Marc Zyngier425c09b2019-11-08 16:57:57 +00001418static void direct_lpi_inv(struct irq_data *d)
1419{
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001420 struct its_vlpi_map *map = get_vlpi_map(d);
Marc Zyngier425c09b2019-11-08 16:57:57 +00001421 void __iomem *rdbase;
Marc Zyngierf3a059212020-03-04 20:33:10 +00001422 unsigned long flags;
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001423 u64 val;
Marc Zyngierf3a059212020-03-04 20:33:10 +00001424 int cpu;
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001425
1426 if (map) {
1427 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1428
1429 WARN_ON(!is_v4_1(its_dev->its));
1430
1431 val = GICR_INVLPIR_V;
1432 val |= FIELD_PREP(GICR_INVLPIR_VPEID, map->vpe->vpe_id);
1433 val |= FIELD_PREP(GICR_INVLPIR_INTID, map->vintid);
1434 } else {
1435 val = d->hwirq;
1436 }
Marc Zyngier425c09b2019-11-08 16:57:57 +00001437
1438 /* Target the redistributor this LPI is currently routed to */
Marc Zyngierf3a059212020-03-04 20:33:10 +00001439 cpu = irq_to_cpuid_lock(d, &flags);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00001440 raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock);
Marc Zyngierf3a059212020-03-04 20:33:10 +00001441 rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base;
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001442 gic_write_lpir(val, rdbase + GICR_INVLPIR);
Marc Zyngier425c09b2019-11-08 16:57:57 +00001443
1444 wait_for_syncr(rdbase);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00001445 raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock);
Marc Zyngierf3a059212020-03-04 20:33:10 +00001446 irq_to_cpuid_unlock(d, flags);
Marc Zyngier425c09b2019-11-08 16:57:57 +00001447}
1448
Marc Zyngier015ec032016-12-20 09:54:57 +00001449static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1450{
1451 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1452
1453 lpi_write_config(d, clr, set);
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001454 if (gic_rdists->has_direct_lpi &&
1455 (is_v4_1(its_dev->its) || !irqd_is_forwarded_to_vcpu(d)))
Marc Zyngier425c09b2019-11-08 16:57:57 +00001456 direct_lpi_inv(d);
Marc Zyngier28614692019-11-08 16:58:02 +00001457 else if (!irqd_is_forwarded_to_vcpu(d))
Marc Zyngier425c09b2019-11-08 16:57:57 +00001458 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngier28614692019-11-08 16:58:02 +00001459 else
1460 its_send_vinv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001461}
1462
Marc Zyngier015ec032016-12-20 09:54:57 +00001463static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1464{
1465 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1466 u32 event = its_get_event_id(d);
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001467 struct its_vlpi_map *map;
Marc Zyngier015ec032016-12-20 09:54:57 +00001468
Marc Zyngier3858d4d2019-12-24 11:10:37 +00001469 /*
1470 * GICv4.1 does away with the per-LPI nonsense, nothing to do
1471 * here.
1472 */
1473 if (is_v4_1(its_dev->its))
1474 return;
1475
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001476 map = dev_event_to_vlpi_map(its_dev, event);
1477
1478 if (map->db_enabled == enable)
Marc Zyngier015ec032016-12-20 09:54:57 +00001479 return;
1480
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001481 map->db_enabled = enable;
Marc Zyngier015ec032016-12-20 09:54:57 +00001482
1483 /*
1484 * More fun with the architecture:
1485 *
1486 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1487 * value or to 1023, depending on the enable bit. But that
1488 * would be issueing a mapping for an /existing/ DevID+EventID
1489 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1490 * to the /same/ vPE, using this opportunity to adjust the
1491 * doorbell. Mouahahahaha. We loves it, Precious.
1492 */
1493 its_send_vmovi(its_dev, event);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001494}
1495
1496static void its_mask_irq(struct irq_data *d)
1497{
Marc Zyngier015ec032016-12-20 09:54:57 +00001498 if (irqd_is_forwarded_to_vcpu(d))
1499 its_vlpi_set_doorbell(d, false);
1500
Marc Zyngieradcdb942016-12-19 19:18:13 +00001501 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001502}
1503
1504static void its_unmask_irq(struct irq_data *d)
1505{
Marc Zyngier015ec032016-12-20 09:54:57 +00001506 if (irqd_is_forwarded_to_vcpu(d))
1507 its_vlpi_set_doorbell(d, true);
1508
Marc Zyngieradcdb942016-12-19 19:18:13 +00001509 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001510}
1511
Marc Zyngierc48ed512014-11-24 14:35:12 +00001512static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1513 bool force)
1514{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001515 unsigned int cpu;
1516 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001517 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1518 struct its_collection *target_col;
1519 u32 id = its_get_event_id(d);
1520
Marc Zyngier015ec032016-12-20 09:54:57 +00001521 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1522 if (irqd_is_forwarded_to_vcpu(d))
1523 return -EINVAL;
1524
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001525 /* lpi cannot be routed to a redistributor that is on a foreign node */
1526 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1527 if (its_dev->its->numa_node >= 0) {
1528 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1529 if (!cpumask_intersects(mask_val, cpu_mask))
1530 return -EINVAL;
1531 }
1532 }
1533
1534 cpu = cpumask_any_and(mask_val, cpu_mask);
1535
Marc Zyngierc48ed512014-11-24 14:35:12 +00001536 if (cpu >= nr_cpu_ids)
1537 return -EINVAL;
1538
MaJun8b8d94a2017-05-18 16:19:13 +08001539 /* don't set the affinity when the target cpu is same as current one */
1540 if (cpu != its_dev->event_map.col_map[id]) {
1541 target_col = &its_dev->its->collections[cpu];
1542 its_send_movi(its_dev, target_col, id);
1543 its_dev->event_map.col_map[id] = cpu;
Marc Zyngier0d224d32017-08-18 09:39:18 +01001544 irq_data_update_effective_affinity(d, cpumask_of(cpu));
MaJun8b8d94a2017-05-18 16:19:13 +08001545 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001546
1547 return IRQ_SET_MASK_OK_DONE;
1548}
1549
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001550static u64 its_irq_get_msi_base(struct its_device *its_dev)
1551{
1552 struct its_node *its = its_dev->its;
1553
1554 return its->phys_base + GITS_TRANSLATER;
1555}
1556
Marc Zyngierb48ac832014-11-24 14:35:16 +00001557static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1558{
1559 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1560 struct its_node *its;
1561 u64 addr;
1562
1563 its = its_dev->its;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001564 addr = its->get_msi_base(its_dev);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001565
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001566 msg->address_lo = lower_32_bits(addr);
1567 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001568 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001569
Julien Grall35ae7df2019-05-01 14:58:21 +01001570 iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001571}
1572
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001573static int its_irq_set_irqchip_state(struct irq_data *d,
1574 enum irqchip_irq_state which,
1575 bool state)
1576{
1577 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1578 u32 event = its_get_event_id(d);
1579
1580 if (which != IRQCHIP_STATE_PENDING)
1581 return -EINVAL;
1582
Marc Zyngiered0e4aa2019-11-08 16:58:03 +00001583 if (irqd_is_forwarded_to_vcpu(d)) {
1584 if (state)
1585 its_send_vint(its_dev, event);
1586 else
1587 its_send_vclear(its_dev, event);
1588 } else {
1589 if (state)
1590 its_send_int(its_dev, event);
1591 else
1592 its_send_clear(its_dev, event);
1593 }
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001594
1595 return 0;
1596}
1597
Marc Zyngier009384b2020-03-04 20:33:23 +00001598/*
1599 * Two favourable cases:
1600 *
1601 * (a) Either we have a GICv4.1, and all vPEs have to be mapped at all times
1602 * for vSGI delivery
1603 *
1604 * (b) Or the ITSs do not use a list map, meaning that VMOVP is cheap enough
1605 * and we're better off mapping all VPEs always
1606 *
1607 * If neither (a) nor (b) is true, then we map vPEs on demand.
1608 *
1609 */
1610static bool gic_requires_eager_mapping(void)
1611{
1612 if (!its_list_map || gic_rdists->has_rvpeid)
1613 return true;
1614
1615 return false;
1616}
1617
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001618static void its_map_vm(struct its_node *its, struct its_vm *vm)
1619{
1620 unsigned long flags;
1621
Marc Zyngier009384b2020-03-04 20:33:23 +00001622 if (gic_requires_eager_mapping())
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001623 return;
1624
1625 raw_spin_lock_irqsave(&vmovp_lock, flags);
1626
1627 /*
1628 * If the VM wasn't mapped yet, iterate over the vpes and get
1629 * them mapped now.
1630 */
1631 vm->vlpi_count[its->list_nr]++;
1632
1633 if (vm->vlpi_count[its->list_nr] == 1) {
1634 int i;
1635
1636 for (i = 0; i < vm->nr_vpes; i++) {
1637 struct its_vpe *vpe = vm->vpes[i];
Marc Zyngier44c4c252017-10-19 10:11:34 +01001638 struct irq_data *d = irq_get_irq_data(vpe->irq);
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001639
1640 /* Map the VPE to the first possible CPU */
1641 vpe->col_idx = cpumask_first(cpu_online_mask);
1642 its_send_vmapp(its, vpe, true);
1643 its_send_vinvall(its, vpe);
Marc Zyngier44c4c252017-10-19 10:11:34 +01001644 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001645 }
1646 }
1647
1648 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1649}
1650
1651static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1652{
1653 unsigned long flags;
1654
1655 /* Not using the ITS list? Everything is always mapped. */
Marc Zyngier009384b2020-03-04 20:33:23 +00001656 if (gic_requires_eager_mapping())
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001657 return;
1658
1659 raw_spin_lock_irqsave(&vmovp_lock, flags);
1660
1661 if (!--vm->vlpi_count[its->list_nr]) {
1662 int i;
1663
1664 for (i = 0; i < vm->nr_vpes; i++)
1665 its_send_vmapp(its, vm->vpes[i], false);
1666 }
1667
1668 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1669}
1670
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001671static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1672{
1673 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1674 u32 event = its_get_event_id(d);
1675 int ret = 0;
1676
1677 if (!info->map)
1678 return -EINVAL;
1679
Marc Zyngier11635fa2019-11-08 16:58:05 +00001680 raw_spin_lock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001681
1682 if (!its_dev->event_map.vm) {
1683 struct its_vlpi_map *maps;
1684
Kees Cook6396bb22018-06-12 14:03:40 -07001685 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
Marc Zyngier11635fa2019-11-08 16:58:05 +00001686 GFP_ATOMIC);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001687 if (!maps) {
1688 ret = -ENOMEM;
1689 goto out;
1690 }
1691
1692 its_dev->event_map.vm = info->map->vm;
1693 its_dev->event_map.vlpi_maps = maps;
1694 } else if (its_dev->event_map.vm != info->map->vm) {
1695 ret = -EINVAL;
1696 goto out;
1697 }
1698
1699 /* Get our private copy of the mapping information */
1700 its_dev->event_map.vlpi_maps[event] = *info->map;
1701
1702 if (irqd_is_forwarded_to_vcpu(d)) {
1703 /* Already mapped, move it around */
1704 its_send_vmovi(its_dev, event);
1705 } else {
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001706 /* Ensure all the VPEs are mapped on this ITS */
1707 its_map_vm(its_dev->its, info->map->vm);
1708
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001709 /*
1710 * Flag the interrupt as forwarded so that we can
1711 * start poking the virtual property table.
1712 */
1713 irqd_set_forwarded_to_vcpu(d);
1714
1715 /* Write out the property to the prop table */
1716 lpi_write_config(d, 0xff, info->map->properties);
1717
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001718 /* Drop the physical mapping */
1719 its_send_discard(its_dev, event);
1720
1721 /* and install the virtual one */
1722 its_send_vmapti(its_dev, event);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001723
1724 /* Increment the number of VLPIs */
1725 its_dev->event_map.nr_vlpis++;
1726 }
1727
1728out:
Marc Zyngier11635fa2019-11-08 16:58:05 +00001729 raw_spin_unlock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001730 return ret;
1731}
1732
1733static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1734{
1735 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier046b5052019-11-08 16:58:04 +00001736 struct its_vlpi_map *map;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001737 int ret = 0;
1738
Marc Zyngier11635fa2019-11-08 16:58:05 +00001739 raw_spin_lock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001740
Marc Zyngier046b5052019-11-08 16:58:04 +00001741 map = get_vlpi_map(d);
1742
1743 if (!its_dev->event_map.vm || !map) {
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001744 ret = -EINVAL;
1745 goto out;
1746 }
1747
1748 /* Copy our mapping information to the incoming request */
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001749 *info->map = *map;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001750
1751out:
Marc Zyngier11635fa2019-11-08 16:58:05 +00001752 raw_spin_unlock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001753 return ret;
1754}
1755
1756static int its_vlpi_unmap(struct irq_data *d)
1757{
1758 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1759 u32 event = its_get_event_id(d);
1760 int ret = 0;
1761
Marc Zyngier11635fa2019-11-08 16:58:05 +00001762 raw_spin_lock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001763
1764 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1765 ret = -EINVAL;
1766 goto out;
1767 }
1768
1769 /* Drop the virtual mapping */
1770 its_send_discard(its_dev, event);
1771
1772 /* and restore the physical one */
1773 irqd_clr_forwarded_to_vcpu(d);
1774 its_send_mapti(its_dev, d->hwirq, event);
1775 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1776 LPI_PROP_ENABLED |
1777 LPI_PROP_GROUP1));
1778
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001779 /* Potentially unmap the VM from this ITS */
1780 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1781
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001782 /*
1783 * Drop the refcount and make the device available again if
1784 * this was the last VLPI.
1785 */
1786 if (!--its_dev->event_map.nr_vlpis) {
1787 its_dev->event_map.vm = NULL;
1788 kfree(its_dev->event_map.vlpi_maps);
1789 }
1790
1791out:
Marc Zyngier11635fa2019-11-08 16:58:05 +00001792 raw_spin_unlock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001793 return ret;
1794}
1795
Marc Zyngier015ec032016-12-20 09:54:57 +00001796static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1797{
1798 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1799
1800 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1801 return -EINVAL;
1802
1803 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1804 lpi_update_config(d, 0xff, info->config);
1805 else
1806 lpi_write_config(d, 0xff, info->config);
1807 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1808
1809 return 0;
1810}
1811
Marc Zyngierc808eea2016-12-20 09:31:20 +00001812static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1813{
1814 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1815 struct its_cmd_info *info = vcpu_info;
1816
1817 /* Need a v4 ITS */
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00001818 if (!is_v4(its_dev->its))
Marc Zyngierc808eea2016-12-20 09:31:20 +00001819 return -EINVAL;
1820
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001821 /* Unmap request? */
1822 if (!info)
1823 return its_vlpi_unmap(d);
1824
Marc Zyngierc808eea2016-12-20 09:31:20 +00001825 switch (info->cmd_type) {
1826 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001827 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001828
1829 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001830 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001831
1832 case PROP_UPDATE_VLPI:
1833 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001834 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001835
1836 default:
1837 return -EINVAL;
1838 }
1839}
1840
Marc Zyngierc48ed512014-11-24 14:35:12 +00001841static struct irq_chip its_irq_chip = {
1842 .name = "ITS",
1843 .irq_mask = its_mask_irq,
1844 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001845 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001846 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001847 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001848 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001849 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001850};
1851
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001852
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001853/*
1854 * How we allocate LPIs:
1855 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001856 * lpi_range_list contains ranges of LPIs that are to available to
1857 * allocate from. To allocate LPIs, just pick the first range that
1858 * fits the required allocation, and reduce it by the required
1859 * amount. Once empty, remove the range from the list.
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001860 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001861 * To free a range of LPIs, add a free range to the list, sort it and
1862 * merge the result if the new range happens to be adjacent to an
1863 * already free block.
1864 *
1865 * The consequence of the above is that allocation is cost is low, but
1866 * freeing is expensive. We assumes that freeing rarely occurs.
1867 */
Jia He4cb205c2018-08-28 12:53:26 +08001868#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001869
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001870static DEFINE_MUTEX(lpi_range_lock);
1871static LIST_HEAD(lpi_range_list);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001872
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001873struct lpi_range {
1874 struct list_head entry;
1875 u32 base_id;
1876 u32 span;
1877};
1878
1879static struct lpi_range *mk_lpi_range(u32 base, u32 span)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001880{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001881 struct lpi_range *range;
1882
Rasmus Villemoes1c73fac2019-03-12 18:33:48 +01001883 range = kmalloc(sizeof(*range), GFP_KERNEL);
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001884 if (range) {
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001885 range->base_id = base;
1886 range->span = span;
1887 }
1888
1889 return range;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001890}
1891
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001892static int alloc_lpi_range(u32 nr_lpis, u32 *base)
1893{
1894 struct lpi_range *range, *tmp;
1895 int err = -ENOSPC;
1896
1897 mutex_lock(&lpi_range_lock);
1898
1899 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1900 if (range->span >= nr_lpis) {
1901 *base = range->base_id;
1902 range->base_id += nr_lpis;
1903 range->span -= nr_lpis;
1904
1905 if (range->span == 0) {
1906 list_del(&range->entry);
1907 kfree(range);
1908 }
1909
1910 err = 0;
1911 break;
1912 }
1913 }
1914
1915 mutex_unlock(&lpi_range_lock);
1916
1917 pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
1918 return err;
1919}
1920
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001921static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b)
1922{
1923 if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list)
1924 return;
1925 if (a->base_id + a->span != b->base_id)
1926 return;
1927 b->base_id = a->base_id;
1928 b->span += a->span;
1929 list_del(&a->entry);
1930 kfree(a);
1931}
1932
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001933static int free_lpi_range(u32 base, u32 nr_lpis)
1934{
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001935 struct lpi_range *new, *old;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001936
1937 new = mk_lpi_range(base, nr_lpis);
Rasmus Villemoesb31a3832019-03-12 18:33:47 +01001938 if (!new)
1939 return -ENOMEM;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001940
1941 mutex_lock(&lpi_range_lock);
1942
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001943 list_for_each_entry_reverse(old, &lpi_range_list, entry) {
1944 if (old->base_id < base)
1945 break;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001946 }
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001947 /*
1948 * old is the last element with ->base_id smaller than base,
1949 * so new goes right after it. If there are no elements with
1950 * ->base_id smaller than base, &old->entry ends up pointing
1951 * at the head of the list, and inserting new it the start of
1952 * the list is the right thing to do in that case as well.
1953 */
1954 list_add(&new->entry, &old->entry);
1955 /*
1956 * Now check if we can merge with the preceding and/or
1957 * following ranges.
1958 */
1959 merge_lpi_ranges(old, new);
1960 merge_lpi_ranges(new, list_next_entry(new, entry));
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001961
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001962 mutex_unlock(&lpi_range_lock);
Rasmus Villemoesb31a3832019-03-12 18:33:47 +01001963 return 0;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001964}
1965
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001966static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001967{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001968 u32 lpis = (1UL << id_bits) - 8192;
Marc Zyngier12b29052018-05-31 09:01:59 +01001969 u32 numlpis;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001970 int err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001971
Marc Zyngier12b29052018-05-31 09:01:59 +01001972 numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
1973
1974 if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
1975 lpis = numlpis;
1976 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
1977 lpis);
1978 }
1979
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001980 /*
1981 * Initializing the allocator is just the same as freeing the
1982 * full range of LPIs.
1983 */
1984 err = free_lpi_range(8192, lpis);
1985 pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
1986 return err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001987}
1988
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001989static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001990{
1991 unsigned long *bitmap = NULL;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001992 int err = 0;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001993
1994 do {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001995 err = alloc_lpi_range(nr_irqs, base);
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001996 if (!err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001997 break;
1998
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001999 nr_irqs /= 2;
2000 } while (nr_irqs > 0);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00002001
Marc Zyngier45725e02019-01-29 15:19:23 +00002002 if (!nr_irqs)
2003 err = -ENOSPC;
2004
Marc Zyngier880cb3c2018-05-27 16:14:15 +01002005 if (err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00002006 goto out;
2007
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002008 bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00002009 if (!bitmap)
2010 goto out;
2011
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002012 *nr_ids = nr_irqs;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00002013
2014out:
Marc Zyngierc8415b92015-10-02 16:44:05 +01002015 if (!bitmap)
2016 *base = *nr_ids = 0;
2017
Marc Zyngierbf9529f2014-11-24 14:35:13 +00002018 return bitmap;
2019}
2020
Marc Zyngier38dd7c42018-05-27 17:03:03 +01002021static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00002022{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01002023 WARN_ON(free_lpi_range(base, nr_ids));
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00002024 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00002025}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002026
Marc Zyngier053be482018-07-27 15:02:27 +01002027static void gic_reset_prop_table(void *va)
2028{
2029 /* Priority 0xa0, Group-1, disabled */
2030 memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ);
2031
2032 /* Make sure the GIC will observe the written configuration */
2033 gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ);
2034}
2035
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00002036static struct page *its_allocate_prop_table(gfp_t gfp_flags)
2037{
2038 struct page *prop_page;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002039
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00002040 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
2041 if (!prop_page)
2042 return NULL;
2043
Marc Zyngier053be482018-07-27 15:02:27 +01002044 gic_reset_prop_table(page_address(prop_page));
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00002045
2046 return prop_page;
2047}
2048
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002049static void its_free_prop_table(struct page *prop_page)
2050{
2051 free_pages((unsigned long)page_address(prop_page),
2052 get_order(LPI_PROPBASE_SZ));
2053}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002054
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002055static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
2056{
2057 phys_addr_t start, end, addr_end;
2058 u64 i;
2059
2060 /*
2061 * We don't bother checking for a kdump kernel as by
2062 * construction, the LPI tables are out of this kernel's
2063 * memory map.
2064 */
2065 if (is_kdump_kernel())
2066 return true;
2067
2068 addr_end = addr + size - 1;
2069
2070 for_each_reserved_mem_region(i, &start, &end) {
2071 if (addr >= start && addr_end <= end)
2072 return true;
2073 }
2074
2075 /* Not found, not a good sign... */
2076 pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n",
2077 &addr, &addr_end);
2078 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
2079 return false;
2080}
2081
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01002082static int gic_reserve_range(phys_addr_t addr, unsigned long size)
2083{
2084 if (efi_enabled(EFI_CONFIG_TABLES))
2085 return efi_mem_reserve_persistent(addr, size);
2086
2087 return 0;
2088}
2089
Marc Zyngier11e37d32018-07-27 13:38:54 +01002090static int __init its_setup_lpi_prop_table(void)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002091{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002092 if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) {
2093 u64 val;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002094
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002095 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2096 lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1;
2097
2098 gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12);
2099 gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa,
2100 LPI_PROPBASE_SZ,
2101 MEMREMAP_WB);
2102 gic_reset_prop_table(gic_rdists->prop_table_va);
2103 } else {
2104 struct page *page;
2105
2106 lpi_id_bits = min_t(u32,
2107 GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
2108 ITS_MAX_LPI_NRBITS);
2109 page = its_allocate_prop_table(GFP_NOWAIT);
2110 if (!page) {
2111 pr_err("Failed to allocate PROPBASE\n");
2112 return -ENOMEM;
2113 }
2114
2115 gic_rdists->prop_table_pa = page_to_phys(page);
2116 gic_rdists->prop_table_va = page_address(page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01002117 WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa,
2118 LPI_PROPBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002119 }
2120
Marc Zyngiere1a2e202018-07-27 14:36:00 +01002121 pr_info("GICv3: using LPI property table @%pa\n",
2122 &gic_rdists->prop_table_pa);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002123
Shanker Donthineni6c31e122017-06-22 18:19:14 -05002124 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002125}
2126
2127static const char *its_base_type_string[] = {
2128 [GITS_BASER_TYPE_DEVICE] = "Devices",
2129 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00002130 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002131 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
2132 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
2133 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
2134 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
2135};
2136
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002137static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
2138{
2139 u32 idx = baser - its->tables;
2140
Vladimir Murzin0968a612016-11-02 11:54:06 +00002141 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002142}
2143
2144static void its_write_baser(struct its_node *its, struct its_baser *baser,
2145 u64 val)
2146{
2147 u32 idx = baser - its->tables;
2148
Vladimir Murzin0968a612016-11-02 11:54:06 +00002149 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002150 baser->val = its_read_baser(its, baser);
2151}
2152
Shanker Donthineni93473592016-06-06 18:17:30 -05002153static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002154 u64 cache, u64 shr, u32 psz, u32 order,
2155 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05002156{
2157 u64 val = its_read_baser(its, baser);
2158 u64 esz = GITS_BASER_ENTRY_SIZE(val);
2159 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05002160 u64 baser_phys, tmp;
Shanker Donthineni93473592016-06-06 18:17:30 -05002161 u32 alloc_pages;
Shanker Donthineni539d3782019-01-14 09:50:19 +00002162 struct page *page;
Shanker Donthineni93473592016-06-06 18:17:30 -05002163 void *base;
Shanker Donthineni93473592016-06-06 18:17:30 -05002164
2165retry_alloc_baser:
2166 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
2167 if (alloc_pages > GITS_BASER_PAGES_MAX) {
2168 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
2169 &its->phys_base, its_base_type_string[type],
2170 alloc_pages, GITS_BASER_PAGES_MAX);
2171 alloc_pages = GITS_BASER_PAGES_MAX;
2172 order = get_order(GITS_BASER_PAGES_MAX * psz);
2173 }
2174
Shanker Donthineni539d3782019-01-14 09:50:19 +00002175 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order);
2176 if (!page)
Shanker Donthineni93473592016-06-06 18:17:30 -05002177 return -ENOMEM;
2178
Shanker Donthineni539d3782019-01-14 09:50:19 +00002179 base = (void *)page_address(page);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05002180 baser_phys = virt_to_phys(base);
2181
2182 /* Check if the physical address of the memory is above 48bits */
2183 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
2184
2185 /* 52bit PA is supported only when PageSize=64K */
2186 if (psz != SZ_64K) {
2187 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
2188 free_pages((unsigned long)base, order);
2189 return -ENXIO;
2190 }
2191
2192 /* Convert 52bit PA to 48bit field */
2193 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
2194 }
2195
Shanker Donthineni93473592016-06-06 18:17:30 -05002196retry_baser:
Shanker Donthineni30ae9612017-10-09 11:46:55 -05002197 val = (baser_phys |
Shanker Donthineni93473592016-06-06 18:17:30 -05002198 (type << GITS_BASER_TYPE_SHIFT) |
2199 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
2200 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
2201 cache |
2202 shr |
2203 GITS_BASER_VALID);
2204
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002205 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
2206
Shanker Donthineni93473592016-06-06 18:17:30 -05002207 switch (psz) {
2208 case SZ_4K:
2209 val |= GITS_BASER_PAGE_SIZE_4K;
2210 break;
2211 case SZ_16K:
2212 val |= GITS_BASER_PAGE_SIZE_16K;
2213 break;
2214 case SZ_64K:
2215 val |= GITS_BASER_PAGE_SIZE_64K;
2216 break;
2217 }
2218
2219 its_write_baser(its, baser, val);
2220 tmp = baser->val;
2221
2222 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
2223 /*
2224 * Shareability didn't stick. Just use
2225 * whatever the read reported, which is likely
2226 * to be the only thing this redistributor
2227 * supports. If that's zero, make it
2228 * non-cacheable as well.
2229 */
2230 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
2231 if (!shr) {
2232 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00002233 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05002234 }
2235 goto retry_baser;
2236 }
2237
2238 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
2239 /*
2240 * Page size didn't stick. Let's try a smaller
2241 * size and retry. If we reach 4K, then
2242 * something is horribly wrong...
2243 */
2244 free_pages((unsigned long)base, order);
2245 baser->base = NULL;
2246
2247 switch (psz) {
2248 case SZ_16K:
2249 psz = SZ_4K;
2250 goto retry_alloc_baser;
2251 case SZ_64K:
2252 psz = SZ_16K;
2253 goto retry_alloc_baser;
2254 }
2255 }
2256
2257 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00002258 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05002259 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00002260 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05002261 free_pages((unsigned long)base, order);
2262 return -ENXIO;
2263 }
2264
2265 baser->order = order;
2266 baser->base = base;
2267 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002268 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05002269
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002270 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00002271 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05002272 its_base_type_string[type],
2273 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002274 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05002275 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
2276
2277 return 0;
2278}
2279
Marc Zyngier4cacac52016-12-19 18:18:34 +00002280static bool its_parse_indirect_baser(struct its_node *its,
2281 struct its_baser *baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002282 u32 psz, u32 *order, u32 ids)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002283{
Marc Zyngier4cacac52016-12-19 18:18:34 +00002284 u64 tmp = its_read_baser(its, baser);
2285 u64 type = GITS_BASER_TYPE(tmp);
2286 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002287 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002288 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002289 bool indirect = false;
2290
2291 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
2292 if ((esz << ids) > (psz * 2)) {
2293 /*
2294 * Find out whether hw supports a single or two-level table by
2295 * table by reading bit at offset '62' after writing '1' to it.
2296 */
2297 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
2298 indirect = !!(baser->val & GITS_BASER_INDIRECT);
2299
2300 if (indirect) {
2301 /*
2302 * The size of the lvl2 table is equal to ITS page size
2303 * which is 'psz'. For computing lvl1 table size,
2304 * subtract ID bits that sparse lvl2 table from 'ids'
2305 * which is reported by ITS hardware times lvl1 table
2306 * entry size.
2307 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00002308 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002309 esz = GITS_LVL1_ENTRY_SIZE;
2310 }
2311 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002312
2313 /*
2314 * Allocate as many entries as required to fit the
2315 * range of device IDs that the ITS can grok... The ID
2316 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002317 * massive waste of memory if two-level device table
2318 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002319 */
2320 new_order = max_t(u32, get_order(esz << ids), new_order);
2321 if (new_order >= MAX_ORDER) {
2322 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00002323 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier576a8342019-11-08 16:58:00 +00002324 pr_warn("ITS@%pa: %s Table too large, reduce ids %llu->%u\n",
Marc Zyngier4cacac52016-12-19 18:18:34 +00002325 &its->phys_base, its_base_type_string[type],
Marc Zyngier576a8342019-11-08 16:58:00 +00002326 device_ids(its), ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002327 }
2328
2329 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002330
2331 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002332}
2333
Marc Zyngier5e516842019-12-24 11:10:28 +00002334static u32 compute_common_aff(u64 val)
2335{
2336 u32 aff, clpiaff;
2337
2338 aff = FIELD_GET(GICR_TYPER_AFFINITY, val);
2339 clpiaff = FIELD_GET(GICR_TYPER_COMMON_LPI_AFF, val);
2340
2341 return aff & ~(GENMASK(31, 0) >> (clpiaff * 8));
2342}
2343
2344static u32 compute_its_aff(struct its_node *its)
2345{
2346 u64 val;
2347 u32 svpet;
2348
2349 /*
2350 * Reencode the ITS SVPET and MPIDR as a GICR_TYPER, and compute
2351 * the resulting affinity. We then use that to see if this match
2352 * our own affinity.
2353 */
2354 svpet = FIELD_GET(GITS_TYPER_SVPET, its->typer);
2355 val = FIELD_PREP(GICR_TYPER_COMMON_LPI_AFF, svpet);
2356 val |= FIELD_PREP(GICR_TYPER_AFFINITY, its->mpidr);
2357 return compute_common_aff(val);
2358}
2359
2360static struct its_node *find_sibling_its(struct its_node *cur_its)
2361{
2362 struct its_node *its;
2363 u32 aff;
2364
2365 if (!FIELD_GET(GITS_TYPER_SVPET, cur_its->typer))
2366 return NULL;
2367
2368 aff = compute_its_aff(cur_its);
2369
2370 list_for_each_entry(its, &its_nodes, entry) {
2371 u64 baser;
2372
2373 if (!is_v4_1(its) || its == cur_its)
2374 continue;
2375
2376 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2377 continue;
2378
2379 if (aff != compute_its_aff(its))
2380 continue;
2381
2382 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2383 baser = its->tables[2].val;
2384 if (!(baser & GITS_BASER_VALID))
2385 continue;
2386
2387 return its;
2388 }
2389
2390 return NULL;
2391}
2392
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002393static void its_free_tables(struct its_node *its)
2394{
2395 int i;
2396
2397 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06002398 if (its->tables[i].base) {
2399 free_pages((unsigned long)its->tables[i].base,
2400 its->tables[i].order);
2401 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002402 }
2403 }
2404}
2405
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05002406static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002407{
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002408 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002409 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05002410 u32 psz = SZ_64K;
2411 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02002412
Ard Biesheuvelfa150012017-10-17 17:55:54 +01002413 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
2414 /* erratum 24313: ignore memory access type */
2415 cache = GITS_BASER_nCnB;
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002416
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002417 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002418 struct its_baser *baser = its->tables + i;
2419 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002420 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05002421 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002422 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002423
Marc Zyngier4cacac52016-12-19 18:18:34 +00002424 switch (type) {
2425 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002426 continue;
2427
Marc Zyngier4cacac52016-12-19 18:18:34 +00002428 case GITS_BASER_TYPE_DEVICE:
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002429 indirect = its_parse_indirect_baser(its, baser,
2430 psz, &order,
Marc Zyngier576a8342019-11-08 16:58:00 +00002431 device_ids(its));
Zenghui Yu8d565742019-02-10 05:24:10 +00002432 break;
2433
Marc Zyngier4cacac52016-12-19 18:18:34 +00002434 case GITS_BASER_TYPE_VCPU:
Marc Zyngier5e516842019-12-24 11:10:28 +00002435 if (is_v4_1(its)) {
2436 struct its_node *sibling;
2437
2438 WARN_ON(i != 2);
2439 if ((sibling = find_sibling_its(its))) {
2440 *baser = sibling->tables[2];
2441 its_write_baser(its, baser, baser->val);
2442 continue;
2443 }
2444 }
2445
Marc Zyngier4cacac52016-12-19 18:18:34 +00002446 indirect = its_parse_indirect_baser(its, baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002447 psz, &order,
2448 ITS_MAX_VPEID_BITS);
Marc Zyngier4cacac52016-12-19 18:18:34 +00002449 break;
2450 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00002451
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002452 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05002453 if (err < 0) {
2454 its_free_tables(its);
2455 return err;
Robert Richter30f21362015-09-21 22:58:34 +02002456 }
2457
Shanker Donthineni93473592016-06-06 18:17:30 -05002458 /* Update settings which will be used for next BASERn */
2459 psz = baser->psz;
2460 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
2461 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002462 }
2463
2464 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002465}
2466
Marc Zyngier5e516842019-12-24 11:10:28 +00002467static u64 inherit_vpe_l1_table_from_its(void)
2468{
2469 struct its_node *its;
2470 u64 val;
2471 u32 aff;
2472
2473 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2474 aff = compute_common_aff(val);
2475
2476 list_for_each_entry(its, &its_nodes, entry) {
2477 u64 baser, addr;
2478
2479 if (!is_v4_1(its))
2480 continue;
2481
2482 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2483 continue;
2484
2485 if (aff != compute_its_aff(its))
2486 continue;
2487
2488 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2489 baser = its->tables[2].val;
2490 if (!(baser & GITS_BASER_VALID))
2491 continue;
2492
2493 /* We have a winner! */
Zenghui Yu8b718d42020-02-06 15:57:07 +08002494 gic_data_rdist()->vpe_l1_base = its->tables[2].base;
2495
Marc Zyngier5e516842019-12-24 11:10:28 +00002496 val = GICR_VPROPBASER_4_1_VALID;
2497 if (baser & GITS_BASER_INDIRECT)
2498 val |= GICR_VPROPBASER_4_1_INDIRECT;
2499 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE,
2500 FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser));
2501 switch (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser)) {
2502 case GIC_PAGE_SIZE_64K:
2503 addr = GITS_BASER_ADDR_48_to_52(baser);
2504 break;
2505 default:
2506 addr = baser & GENMASK_ULL(47, 12);
2507 break;
2508 }
2509 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, addr >> 12);
2510 val |= FIELD_PREP(GICR_VPROPBASER_SHAREABILITY_MASK,
2511 FIELD_GET(GITS_BASER_SHAREABILITY_MASK, baser));
2512 val |= FIELD_PREP(GICR_VPROPBASER_INNER_CACHEABILITY_MASK,
2513 FIELD_GET(GITS_BASER_INNER_CACHEABILITY_MASK, baser));
2514 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, GITS_BASER_NR_PAGES(baser) - 1);
2515
2516 return val;
2517 }
2518
2519 return 0;
2520}
2521
2522static u64 inherit_vpe_l1_table_from_rd(cpumask_t **mask)
2523{
2524 u32 aff;
2525 u64 val;
2526 int cpu;
2527
2528 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2529 aff = compute_common_aff(val);
2530
2531 for_each_possible_cpu(cpu) {
2532 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
Marc Zyngier5e516842019-12-24 11:10:28 +00002533
2534 if (!base || cpu == smp_processor_id())
2535 continue;
2536
2537 val = gic_read_typer(base + GICR_TYPER);
Zenghui Yu4bccf1d2020-02-06 15:57:09 +08002538 if (aff != compute_common_aff(val))
Marc Zyngier5e516842019-12-24 11:10:28 +00002539 continue;
2540
2541 /*
2542 * At this point, we have a victim. This particular CPU
2543 * has already booted, and has an affinity that matches
2544 * ours wrt CommonLPIAff. Let's use its own VPROPBASER.
2545 * Make sure we don't write the Z bit in that case.
2546 */
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002547 val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
Marc Zyngier5e516842019-12-24 11:10:28 +00002548 val &= ~GICR_VPROPBASER_4_1_Z;
2549
Zenghui Yu8b718d42020-02-06 15:57:07 +08002550 gic_data_rdist()->vpe_l1_base = gic_data_rdist_cpu(cpu)->vpe_l1_base;
Marc Zyngier5e516842019-12-24 11:10:28 +00002551 *mask = gic_data_rdist_cpu(cpu)->vpe_table_mask;
2552
2553 return val;
2554 }
2555
2556 return 0;
2557}
2558
Zenghui Yu4e6437f2020-02-06 15:57:08 +08002559static bool allocate_vpe_l2_table(int cpu, u32 id)
2560{
2561 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
Marc Zyngier490d3322020-02-09 22:48:50 +00002562 unsigned int psz, esz, idx, npg, gpsz;
2563 u64 val;
Zenghui Yu4e6437f2020-02-06 15:57:08 +08002564 struct page *page;
2565 __le64 *table;
2566
2567 if (!gic_rdists->has_rvpeid)
2568 return true;
2569
Marc Zyngier28d160d2020-03-04 20:33:09 +00002570 /* Skip non-present CPUs */
2571 if (!base)
2572 return true;
2573
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002574 val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
Zenghui Yu4e6437f2020-02-06 15:57:08 +08002575
2576 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val) + 1;
2577 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2578 npg = FIELD_GET(GICR_VPROPBASER_4_1_SIZE, val) + 1;
2579
2580 switch (gpsz) {
2581 default:
2582 WARN_ON(1);
2583 /* fall through */
2584 case GIC_PAGE_SIZE_4K:
2585 psz = SZ_4K;
2586 break;
2587 case GIC_PAGE_SIZE_16K:
2588 psz = SZ_16K;
2589 break;
2590 case GIC_PAGE_SIZE_64K:
2591 psz = SZ_64K;
2592 break;
2593 }
2594
2595 /* Don't allow vpe_id that exceeds single, flat table limit */
2596 if (!(val & GICR_VPROPBASER_4_1_INDIRECT))
2597 return (id < (npg * psz / (esz * SZ_8)));
2598
2599 /* Compute 1st level table index & check if that exceeds table limit */
2600 idx = id >> ilog2(psz / (esz * SZ_8));
2601 if (idx >= (npg * psz / GITS_LVL1_ENTRY_SIZE))
2602 return false;
2603
2604 table = gic_data_rdist_cpu(cpu)->vpe_l1_base;
2605
2606 /* Allocate memory for 2nd level table */
2607 if (!table[idx]) {
2608 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(psz));
2609 if (!page)
2610 return false;
2611
2612 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2613 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2614 gic_flush_dcache_to_poc(page_address(page), psz);
2615
2616 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2617
2618 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2619 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2620 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
2621
2622 /* Ensure updated table contents are visible to RD hardware */
2623 dsb(sy);
2624 }
2625
2626 return true;
2627}
2628
Marc Zyngier5e516842019-12-24 11:10:28 +00002629static int allocate_vpe_l1_table(void)
2630{
2631 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2632 u64 val, gpsz, npg, pa;
2633 unsigned int psz = SZ_64K;
2634 unsigned int np, epp, esz;
2635 struct page *page;
2636
2637 if (!gic_rdists->has_rvpeid)
2638 return 0;
2639
2640 /*
2641 * if VPENDBASER.Valid is set, disable any previously programmed
2642 * VPE by setting PendingLast while clearing Valid. This has the
2643 * effect of making sure no doorbell will be generated and we can
2644 * then safely clear VPROPBASER.Valid.
2645 */
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002646 if (gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER) & GICR_VPENDBASER_Valid)
2647 gicr_write_vpendbaser(GICR_VPENDBASER_PendingLast,
Marc Zyngier5e516842019-12-24 11:10:28 +00002648 vlpi_base + GICR_VPENDBASER);
2649
2650 /*
2651 * If we can inherit the configuration from another RD, let's do
2652 * so. Otherwise, we have to go through the allocation process. We
2653 * assume that all RDs have the exact same requirements, as
2654 * nothing will work otherwise.
2655 */
2656 val = inherit_vpe_l1_table_from_rd(&gic_data_rdist()->vpe_table_mask);
2657 if (val & GICR_VPROPBASER_4_1_VALID)
2658 goto out;
2659
2660 gic_data_rdist()->vpe_table_mask = kzalloc(sizeof(cpumask_t), GFP_KERNEL);
2661 if (!gic_data_rdist()->vpe_table_mask)
2662 return -ENOMEM;
2663
2664 val = inherit_vpe_l1_table_from_its();
2665 if (val & GICR_VPROPBASER_4_1_VALID)
2666 goto out;
2667
2668 /* First probe the page size */
2669 val = FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, GIC_PAGE_SIZE_64K);
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002670 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2671 val = gicr_read_vpropbaser(vlpi_base + GICR_VPROPBASER);
Marc Zyngier5e516842019-12-24 11:10:28 +00002672 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2673 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val);
2674
2675 switch (gpsz) {
2676 default:
2677 gpsz = GIC_PAGE_SIZE_4K;
2678 /* fall through */
2679 case GIC_PAGE_SIZE_4K:
2680 psz = SZ_4K;
2681 break;
2682 case GIC_PAGE_SIZE_16K:
2683 psz = SZ_16K;
2684 break;
2685 case GIC_PAGE_SIZE_64K:
2686 psz = SZ_64K;
2687 break;
2688 }
2689
2690 /*
2691 * Start populating the register from scratch, including RO fields
2692 * (which we want to print in debug cases...)
2693 */
2694 val = 0;
2695 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, gpsz);
2696 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ENTRY_SIZE, esz);
2697
2698 /* How many entries per GIC page? */
2699 esz++;
2700 epp = psz / (esz * SZ_8);
2701
2702 /*
2703 * If we need more than just a single L1 page, flag the table
2704 * as indirect and compute the number of required L1 pages.
2705 */
2706 if (epp < ITS_MAX_VPEID) {
2707 int nl2;
2708
2709 val |= GICR_VPROPBASER_4_1_INDIRECT;
2710
2711 /* Number of L2 pages required to cover the VPEID space */
2712 nl2 = DIV_ROUND_UP(ITS_MAX_VPEID, epp);
2713
2714 /* Number of L1 pages to point to the L2 pages */
2715 npg = DIV_ROUND_UP(nl2 * SZ_8, psz);
2716 } else {
2717 npg = 1;
2718 }
2719
Zenghui Yue88bd312020-02-06 15:57:06 +08002720 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, npg - 1);
Marc Zyngier5e516842019-12-24 11:10:28 +00002721
2722 /* Right, that's the number of CPU pages we need for L1 */
2723 np = DIV_ROUND_UP(npg * psz, PAGE_SIZE);
2724
2725 pr_debug("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n",
2726 np, npg, psz, epp, esz);
2727 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(np * PAGE_SIZE));
2728 if (!page)
2729 return -ENOMEM;
2730
Zenghui Yu8b718d42020-02-06 15:57:07 +08002731 gic_data_rdist()->vpe_l1_base = page_address(page);
Marc Zyngier5e516842019-12-24 11:10:28 +00002732 pa = virt_to_phys(page_address(page));
2733 WARN_ON(!IS_ALIGNED(pa, psz));
2734
2735 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, pa >> 12);
2736 val |= GICR_VPROPBASER_RaWb;
2737 val |= GICR_VPROPBASER_InnerShareable;
2738 val |= GICR_VPROPBASER_4_1_Z;
2739 val |= GICR_VPROPBASER_4_1_VALID;
2740
2741out:
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002742 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
Marc Zyngier5e516842019-12-24 11:10:28 +00002743 cpumask_set_cpu(smp_processor_id(), gic_data_rdist()->vpe_table_mask);
2744
2745 pr_debug("CPU%d: VPROPBASER = %llx %*pbl\n",
2746 smp_processor_id(), val,
2747 cpumask_pr_args(gic_data_rdist()->vpe_table_mask));
2748
2749 return 0;
2750}
2751
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002752static int its_alloc_collections(struct its_node *its)
2753{
Marc Zyngier83559b42018-06-22 10:52:52 +01002754 int i;
2755
Kees Cook6396bb22018-06-12 14:03:40 -07002756 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002757 GFP_KERNEL);
2758 if (!its->collections)
2759 return -ENOMEM;
2760
Marc Zyngier83559b42018-06-22 10:52:52 +01002761 for (i = 0; i < nr_cpu_ids; i++)
2762 its->collections[i].target_address = ~0ULL;
2763
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002764 return 0;
2765}
2766
Marc Zyngier7c297a22016-12-19 18:34:38 +00002767static struct page *its_allocate_pending_table(gfp_t gfp_flags)
2768{
2769 struct page *pend_page;
Marc Zyngieradaab502018-07-17 18:06:39 +01002770
Marc Zyngier7c297a22016-12-19 18:34:38 +00002771 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
Marc Zyngieradaab502018-07-17 18:06:39 +01002772 get_order(LPI_PENDBASE_SZ));
Marc Zyngier7c297a22016-12-19 18:34:38 +00002773 if (!pend_page)
2774 return NULL;
2775
2776 /* Make sure the GIC will observe the zero-ed page */
2777 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
2778
2779 return pend_page;
2780}
2781
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002782static void its_free_pending_table(struct page *pt)
2783{
Marc Zyngieradaab502018-07-17 18:06:39 +01002784 free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ));
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002785}
2786
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002787/*
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002788 * Booting with kdump and LPIs enabled is generally fine. Any other
2789 * case is wrong in the absence of firmware/EFI support.
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002790 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002791static bool enabled_lpis_allowed(void)
2792{
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002793 phys_addr_t addr;
2794 u64 val;
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002795
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002796 /* Check whether the property table is in a reserved region */
2797 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2798 addr = val & GENMASK_ULL(51, 12);
2799
2800 return gic_check_reserved_range(addr, LPI_PROPBASE_SZ);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002801}
2802
Marc Zyngier11e37d32018-07-27 13:38:54 +01002803static int __init allocate_lpi_tables(void)
2804{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002805 u64 val;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002806 int err, cpu;
2807
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002808 /*
2809 * If LPIs are enabled while we run this from the boot CPU,
2810 * flag the RD tables as pre-allocated if the stars do align.
2811 */
2812 val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR);
2813 if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) {
2814 gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED |
2815 RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING);
2816 pr_info("GICv3: Using preallocated redistributor tables\n");
2817 }
2818
Marc Zyngier11e37d32018-07-27 13:38:54 +01002819 err = its_setup_lpi_prop_table();
2820 if (err)
2821 return err;
2822
2823 /*
2824 * We allocate all the pending tables anyway, as we may have a
2825 * mix of RDs that have had LPIs enabled, and some that
2826 * don't. We'll free the unused ones as each CPU comes online.
2827 */
2828 for_each_possible_cpu(cpu) {
2829 struct page *pend_page;
2830
2831 pend_page = its_allocate_pending_table(GFP_NOWAIT);
2832 if (!pend_page) {
2833 pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu);
2834 return -ENOMEM;
2835 }
2836
2837 gic_data_rdist_cpu(cpu)->pend_page = pend_page;
2838 }
2839
2840 return 0;
2841}
2842
Marc Zyngiere64fab12019-12-24 11:10:35 +00002843static u64 its_clear_vpend_valid(void __iomem *vlpi_base, u64 clr, u64 set)
Heyi Guo64794502019-01-24 21:37:08 +08002844{
2845 u32 count = 1000000; /* 1s! */
2846 bool clean;
2847 u64 val;
2848
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002849 val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002850 val &= ~GICR_VPENDBASER_Valid;
Marc Zyngiere64fab12019-12-24 11:10:35 +00002851 val &= ~clr;
2852 val |= set;
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002853 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002854
2855 do {
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002856 val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002857 clean = !(val & GICR_VPENDBASER_Dirty);
2858 if (!clean) {
2859 count--;
2860 cpu_relax();
2861 udelay(1);
2862 }
2863 } while (!clean && count);
2864
Marc Zyngiere64fab12019-12-24 11:10:35 +00002865 if (unlikely(val & GICR_VPENDBASER_Dirty)) {
2866 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2867 val |= GICR_VPENDBASER_PendingLast;
2868 }
2869
Heyi Guo64794502019-01-24 21:37:08 +08002870 return val;
2871}
2872
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002873static void its_cpu_init_lpis(void)
2874{
2875 void __iomem *rbase = gic_data_rdist_rd_base();
2876 struct page *pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002877 phys_addr_t paddr;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002878 u64 val, tmp;
2879
Marc Zyngier11e37d32018-07-27 13:38:54 +01002880 if (gic_data_rdist()->lpi_enabled)
2881 return;
2882
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002883 val = readl_relaxed(rbase + GICR_CTLR);
2884 if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) &&
2885 (val & GICR_CTLR_ENABLE_LPIS)) {
Marc Zyngierf842ca82018-07-27 16:03:31 +01002886 /*
2887 * Check that we get the same property table on all
2888 * RDs. If we don't, this is hopeless.
2889 */
2890 paddr = gicr_read_propbaser(rbase + GICR_PROPBASER);
2891 paddr &= GENMASK_ULL(51, 12);
2892 if (WARN_ON(gic_rdists->prop_table_pa != paddr))
2893 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
2894
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002895 paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER);
2896 paddr &= GENMASK_ULL(51, 16);
2897
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002898 WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002899 its_free_pending_table(gic_data_rdist()->pend_page);
2900 gic_data_rdist()->pend_page = NULL;
2901
2902 goto out;
2903 }
2904
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002905 pend_page = gic_data_rdist()->pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002906 paddr = page_to_phys(pend_page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01002907 WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002908
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002909 /* set PROPBASE */
Marc Zyngiere1a2e202018-07-27 14:36:00 +01002910 val = (gic_rdists->prop_table_pa |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002911 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002912 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002913 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
2914
Vladimir Murzin0968a612016-11-02 11:54:06 +00002915 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2916 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002917
2918 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002919 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
2920 /*
2921 * The HW reports non-shareable, we must
2922 * remove the cacheability attributes as
2923 * well.
2924 */
2925 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
2926 GICR_PROPBASER_CACHEABILITY_MASK);
2927 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002928 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002929 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002930 pr_info_once("GIC: using cache flushing for LPI property table\n");
2931 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
2932 }
2933
2934 /* set PENDBASE */
2935 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002936 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002937 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002938
Vladimir Murzin0968a612016-11-02 11:54:06 +00002939 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2940 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002941
2942 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
2943 /*
2944 * The HW reports non-shareable, we must remove the
2945 * cacheability attributes as well.
2946 */
2947 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
2948 GICR_PENDBASER_CACHEABILITY_MASK);
2949 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002950 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002951 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002952
2953 /* Enable LPIs */
2954 val = readl_relaxed(rbase + GICR_CTLR);
2955 val |= GICR_CTLR_ENABLE_LPIS;
2956 writel_relaxed(val, rbase + GICR_CTLR);
2957
Marc Zyngier5e516842019-12-24 11:10:28 +00002958 if (gic_rdists->has_vlpis && !gic_rdists->has_rvpeid) {
Heyi Guo64794502019-01-24 21:37:08 +08002959 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2960
2961 /*
2962 * It's possible for CPU to receive VLPIs before it is
2963 * sheduled as a vPE, especially for the first CPU, and the
2964 * VLPI with INTID larger than 2^(IDbits+1) will be considered
2965 * as out of range and dropped by GIC.
2966 * So we initialize IDbits to known value to avoid VLPI drop.
2967 */
2968 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2969 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
2970 smp_processor_id(), val);
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002971 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002972
2973 /*
2974 * Also clear Valid bit of GICR_VPENDBASER, in case some
2975 * ancient programming gets left in and has possibility of
2976 * corrupting memory.
2977 */
Marc Zyngiere64fab12019-12-24 11:10:35 +00002978 val = its_clear_vpend_valid(vlpi_base, 0, 0);
Heyi Guo64794502019-01-24 21:37:08 +08002979 }
2980
Marc Zyngier5e516842019-12-24 11:10:28 +00002981 if (allocate_vpe_l1_table()) {
2982 /*
2983 * If the allocation has failed, we're in massive trouble.
2984 * Disable direct injection, and pray that no VM was
2985 * already running...
2986 */
2987 gic_rdists->has_rvpeid = false;
2988 gic_rdists->has_vlpis = false;
2989 }
2990
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002991 /* Make sure the GIC has seen the above */
2992 dsb(sy);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002993out:
Marc Zyngier11e37d32018-07-27 13:38:54 +01002994 gic_data_rdist()->lpi_enabled = true;
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002995 pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n",
Marc Zyngier11e37d32018-07-27 13:38:54 +01002996 smp_processor_id(),
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002997 gic_data_rdist()->pend_page ? "allocated" : "reserved",
Marc Zyngier11e37d32018-07-27 13:38:54 +01002998 &paddr);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002999}
3000
Derek Basehore920181c2018-02-28 21:48:20 -08003001static void its_cpu_init_collection(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003002{
Derek Basehore920181c2018-02-28 21:48:20 -08003003 int cpu = smp_processor_id();
3004 u64 target;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003005
Derek Basehore920181c2018-02-28 21:48:20 -08003006 /* avoid cross node collections and its mapping */
3007 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
3008 struct device_node *cpu_node;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003009
Derek Basehore920181c2018-02-28 21:48:20 -08003010 cpu_node = of_get_cpu_node(cpu, NULL);
3011 if (its->numa_node != NUMA_NO_NODE &&
3012 its->numa_node != of_node_to_nid(cpu_node))
3013 return;
3014 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003015
Derek Basehore920181c2018-02-28 21:48:20 -08003016 /*
3017 * We now have to bind each collection to its target
3018 * redistributor.
3019 */
3020 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003021 /*
Derek Basehore920181c2018-02-28 21:48:20 -08003022 * This ITS wants the physical address of the
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003023 * redistributor.
3024 */
Derek Basehore920181c2018-02-28 21:48:20 -08003025 target = gic_data_rdist()->phys_base;
3026 } else {
3027 /* This ITS wants a linear CPU number. */
3028 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
3029 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003030 }
3031
Derek Basehore920181c2018-02-28 21:48:20 -08003032 /* Perform collection mapping */
3033 its->collections[cpu].target_address = target;
3034 its->collections[cpu].col_id = cpu;
3035
3036 its_send_mapc(its, &its->collections[cpu], 1);
3037 its_send_invall(its, &its->collections[cpu]);
3038}
3039
3040static void its_cpu_init_collections(void)
3041{
3042 struct its_node *its;
3043
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003044 raw_spin_lock(&its_lock);
Derek Basehore920181c2018-02-28 21:48:20 -08003045
3046 list_for_each_entry(its, &its_nodes, entry)
3047 its_cpu_init_collection(its);
3048
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003049 raw_spin_unlock(&its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003050}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003051
3052static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
3053{
3054 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003055 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003056
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003057 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003058
3059 list_for_each_entry(tmp, &its->its_device_list, entry) {
3060 if (tmp->device_id == dev_id) {
3061 its_dev = tmp;
3062 break;
3063 }
3064 }
3065
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003066 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003067
3068 return its_dev;
3069}
3070
Shanker Donthineni466b7d12016-03-09 22:10:49 -06003071static struct its_baser *its_get_baser(struct its_node *its, u32 type)
3072{
3073 int i;
3074
3075 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
3076 if (GITS_BASER_TYPE(its->tables[i].val) == type)
3077 return &its->tables[i];
3078 }
3079
3080 return NULL;
3081}
3082
Shanker Donthineni539d3782019-01-14 09:50:19 +00003083static bool its_alloc_table_entry(struct its_node *its,
3084 struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003085{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003086 struct page *page;
3087 u32 esz, idx;
3088 __le64 *table;
3089
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003090 /* Don't allow device id that exceeds single, flat table limit */
3091 esz = GITS_BASER_ENTRY_SIZE(baser->val);
3092 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003093 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003094
3095 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003096 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003097 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
3098 return false;
3099
3100 table = baser->base;
3101
3102 /* Allocate memory for 2nd level table */
3103 if (!table[idx]) {
Shanker Donthineni539d3782019-01-14 09:50:19 +00003104 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
3105 get_order(baser->psz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003106 if (!page)
3107 return false;
3108
3109 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
3110 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00003111 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003112
3113 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
3114
3115 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
3116 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00003117 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003118
3119 /* Ensure updated table contents are visible to ITS hardware */
3120 dsb(sy);
3121 }
3122
3123 return true;
3124}
3125
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003126static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
3127{
3128 struct its_baser *baser;
3129
3130 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
3131
3132 /* Don't allow device id that exceeds ITS hardware limit */
3133 if (!baser)
Marc Zyngier576a8342019-11-08 16:58:00 +00003134 return (ilog2(dev_id) < device_ids(its));
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003135
Shanker Donthineni539d3782019-01-14 09:50:19 +00003136 return its_alloc_table_entry(its, baser, dev_id);
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003137}
3138
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003139static bool its_alloc_vpe_table(u32 vpe_id)
3140{
3141 struct its_node *its;
Zenghui Yu4e6437f2020-02-06 15:57:08 +08003142 int cpu;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003143
3144 /*
3145 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
3146 * could try and only do it on ITSs corresponding to devices
3147 * that have interrupts targeted at this VPE, but the
3148 * complexity becomes crazy (and you have tons of memory
3149 * anyway, right?).
3150 */
3151 list_for_each_entry(its, &its_nodes, entry) {
3152 struct its_baser *baser;
3153
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00003154 if (!is_v4(its))
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003155 continue;
3156
3157 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
3158 if (!baser)
3159 return false;
3160
Shanker Donthineni539d3782019-01-14 09:50:19 +00003161 if (!its_alloc_table_entry(its, baser, vpe_id))
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003162 return false;
3163 }
3164
Zenghui Yu4e6437f2020-02-06 15:57:08 +08003165 /* Non v4.1? No need to iterate RDs and go back early. */
3166 if (!gic_rdists->has_rvpeid)
3167 return true;
3168
3169 /*
3170 * Make sure the L2 tables are allocated for all copies of
3171 * the L1 table on *all* v4.1 RDs.
3172 */
3173 for_each_possible_cpu(cpu) {
3174 if (!allocate_vpe_l2_table(cpu, vpe_id))
3175 return false;
3176 }
3177
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003178 return true;
3179}
3180
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003181static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003182 int nvecs, bool alloc_lpis)
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003183{
3184 struct its_device *dev;
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003185 unsigned long *lpi_map = NULL;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003186 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01003187 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003188 void *itt;
3189 int lpi_base;
3190 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00003191 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003192 int sz;
3193
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003194 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06003195 return NULL;
3196
Marc Zyngier147c8f32018-05-27 16:39:55 +01003197 if (WARN_ON(!is_power_of_2(nvecs)))
3198 nvecs = roundup_pow_of_two(nvecs);
3199
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003200 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00003201 /*
Marc Zyngier147c8f32018-05-27 16:39:55 +01003202 * Even if the device wants a single LPI, the ITT must be
3203 * sized as a power of two (and you need at least one bit...).
Marc Zyngierc8481262014-12-12 10:51:24 +00003204 */
Marc Zyngier147c8f32018-05-27 16:39:55 +01003205 nr_ites = max(2, nvecs);
Marc Zyngierffedbf02019-11-08 16:57:59 +00003206 sz = nr_ites * (FIELD_GET(GITS_TYPER_ITT_ENTRY_SIZE, its->typer) + 1);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003207 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Shanker Donthineni539d3782019-01-14 09:50:19 +00003208 itt = kzalloc_node(sz, GFP_KERNEL, its->numa_node);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003209 if (alloc_lpis) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003210 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003211 if (lpi_map)
Kees Cook6396bb22018-06-12 14:03:40 -07003212 col_map = kcalloc(nr_lpis, sizeof(*col_map),
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003213 GFP_KERNEL);
3214 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07003215 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003216 nr_lpis = 0;
3217 lpi_base = 0;
3218 }
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003219
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003220 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003221 kfree(dev);
3222 kfree(itt);
3223 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01003224 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003225 return NULL;
3226 }
3227
Vladimir Murzin328191c2016-11-02 11:54:05 +00003228 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01003229
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003230 dev->its = its;
3231 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00003232 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01003233 dev->event_map.lpi_map = lpi_map;
3234 dev->event_map.col_map = col_map;
3235 dev->event_map.lpi_base = lpi_base;
3236 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngier11635fa2019-11-08 16:58:05 +00003237 raw_spin_lock_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003238 dev->device_id = dev_id;
3239 INIT_LIST_HEAD(&dev->entry);
3240
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003241 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003242 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003243 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003244
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003245 /* Map device to its ITT */
3246 its_send_mapd(dev, 1);
3247
3248 return dev;
3249}
3250
3251static void its_free_device(struct its_device *its_dev)
3252{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003253 unsigned long flags;
3254
3255 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003256 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003257 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier898aa5c2019-11-08 16:57:55 +00003258 kfree(its_dev->event_map.col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003259 kfree(its_dev->itt);
3260 kfree(its_dev);
3261}
Marc Zyngierb48ac832014-11-24 14:35:16 +00003262
Marc Zyngier8208d172019-01-18 14:08:59 +00003263static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
Marc Zyngierb48ac832014-11-24 14:35:16 +00003264{
3265 int idx;
3266
Zenghui Yu342be102019-07-27 06:14:22 +00003267 /* Find a free LPI region in lpi_map and allocate them. */
Marc Zyngier8208d172019-01-18 14:08:59 +00003268 idx = bitmap_find_free_region(dev->event_map.lpi_map,
3269 dev->event_map.nr_lpis,
3270 get_count_order(nvecs));
3271 if (idx < 0)
Marc Zyngierb48ac832014-11-24 14:35:16 +00003272 return -ENOSPC;
3273
Marc Zyngier591e5be2015-07-17 10:46:42 +01003274 *hwirq = dev->event_map.lpi_base + idx;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003275
Marc Zyngierb48ac832014-11-24 14:35:16 +00003276 return 0;
3277}
3278
Marc Zyngier54456db2015-07-28 14:46:21 +01003279static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
3280 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00003281{
Marc Zyngierb48ac832014-11-24 14:35:16 +00003282 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003283 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01003284 struct msi_domain_info *msi_info;
3285 u32 dev_id;
Marc Zyngier9791ec72019-01-29 10:02:33 +00003286 int err = 0;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003287
Marc Zyngier54456db2015-07-28 14:46:21 +01003288 /*
Julien Gralla7c90f52019-04-18 16:58:14 +01003289 * We ignore "dev" entirely, and rely on the dev_id that has
Marc Zyngier54456db2015-07-28 14:46:21 +01003290 * been passed via the scratchpad. This limits this domain's
3291 * usefulness to upper layers that definitely know that they
3292 * are built on top of the ITS.
3293 */
3294 dev_id = info->scratchpad[0].ul;
3295
3296 msi_info = msi_get_domain_info(domain);
3297 its = msi_info->data;
3298
Marc Zyngier20b3d542016-12-20 15:23:22 +00003299 if (!gic_rdists->has_direct_lpi &&
3300 vpe_proxy.dev &&
3301 vpe_proxy.dev->its == its &&
3302 dev_id == vpe_proxy.dev->device_id) {
3303 /* Bad luck. Get yourself a better implementation */
3304 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
3305 dev_id);
3306 return -EINVAL;
3307 }
3308
Marc Zyngier9791ec72019-01-29 10:02:33 +00003309 mutex_lock(&its->dev_alloc_lock);
Marc Zyngierf1304202015-07-28 14:46:18 +01003310 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00003311 if (its_dev) {
3312 /*
3313 * We already have seen this ID, probably through
3314 * another alias (PCI bridge of some sort). No need to
3315 * create the device.
3316 */
Marc Zyngier9791ec72019-01-29 10:02:33 +00003317 its_dev->shared = true;
Marc Zyngierf1304202015-07-28 14:46:18 +01003318 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00003319 goto out;
3320 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00003321
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003322 its_dev = its_create_device(its, dev_id, nvec, true);
Marc Zyngier9791ec72019-01-29 10:02:33 +00003323 if (!its_dev) {
3324 err = -ENOMEM;
3325 goto out;
3326 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00003327
Marc Zyngierf1304202015-07-28 14:46:18 +01003328 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00003329out:
Marc Zyngier9791ec72019-01-29 10:02:33 +00003330 mutex_unlock(&its->dev_alloc_lock);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003331 info->scratchpad[0].ptr = its_dev;
Marc Zyngier9791ec72019-01-29 10:02:33 +00003332 return err;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003333}
3334
Marc Zyngier54456db2015-07-28 14:46:21 +01003335static struct msi_domain_ops its_msi_domain_ops = {
3336 .msi_prepare = its_msi_prepare,
3337};
3338
Marc Zyngierb48ac832014-11-24 14:35:16 +00003339static int its_irq_gic_domain_alloc(struct irq_domain *domain,
3340 unsigned int virq,
3341 irq_hw_number_t hwirq)
3342{
Marc Zyngierf833f572015-10-13 12:51:33 +01003343 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003344
Marc Zyngierf833f572015-10-13 12:51:33 +01003345 if (irq_domain_get_of_node(domain->parent)) {
3346 fwspec.fwnode = domain->parent->fwnode;
3347 fwspec.param_count = 3;
3348 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
3349 fwspec.param[1] = hwirq;
3350 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003351 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
3352 fwspec.fwnode = domain->parent->fwnode;
3353 fwspec.param_count = 2;
3354 fwspec.param[0] = hwirq;
3355 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01003356 } else {
3357 return -EINVAL;
3358 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00003359
Marc Zyngierf833f572015-10-13 12:51:33 +01003360 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003361}
3362
3363static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
3364 unsigned int nr_irqs, void *args)
3365{
3366 msi_alloc_info_t *info = args;
3367 struct its_device *its_dev = info->scratchpad[0].ptr;
Julien Grall35ae7df2019-05-01 14:58:21 +01003368 struct its_node *its = its_dev->its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003369 irq_hw_number_t hwirq;
3370 int err;
3371 int i;
3372
Marc Zyngier8208d172019-01-18 14:08:59 +00003373 err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
3374 if (err)
3375 return err;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003376
Julien Grall35ae7df2019-05-01 14:58:21 +01003377 err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev));
3378 if (err)
3379 return err;
3380
Marc Zyngier8208d172019-01-18 14:08:59 +00003381 for (i = 0; i < nr_irqs; i++) {
3382 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003383 if (err)
3384 return err;
3385
3386 irq_domain_set_hwirq_and_chip(domain, virq + i,
Marc Zyngier8208d172019-01-18 14:08:59 +00003387 hwirq + i, &its_irq_chip, its_dev);
Marc Zyngier0d224d32017-08-18 09:39:18 +01003388 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
Marc Zyngierf1304202015-07-28 14:46:18 +01003389 pr_debug("ID:%d pID:%d vID:%d\n",
Marc Zyngier8208d172019-01-18 14:08:59 +00003390 (int)(hwirq + i - its_dev->event_map.lpi_base),
3391 (int)(hwirq + i), virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003392 }
3393
3394 return 0;
3395}
3396
Thomas Gleixner72491642017-09-13 23:29:10 +02003397static int its_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01003398 struct irq_data *d, bool reserve)
Marc Zyngieraca268d2014-12-12 10:51:23 +00003399{
3400 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3401 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003402 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngier0d224d32017-08-18 09:39:18 +01003403 int cpu;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003404
3405 /* get the cpu_mask of local node */
3406 if (its_dev->its->numa_node >= 0)
3407 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00003408
Marc Zyngier591e5be2015-07-17 10:46:42 +01003409 /* Bind the LPI to the first possible CPU */
Yang Yingliangc1797b12018-06-22 10:52:51 +01003410 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
3411 if (cpu >= nr_cpu_ids) {
3412 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
3413 return -EINVAL;
3414
3415 cpu = cpumask_first(cpu_online_mask);
3416 }
3417
Marc Zyngier0d224d32017-08-18 09:39:18 +01003418 its_dev->event_map.col_map[event] = cpu;
3419 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngier591e5be2015-07-17 10:46:42 +01003420
Marc Zyngieraca268d2014-12-12 10:51:23 +00003421 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00003422 its_send_mapti(its_dev, d->hwirq, event);
Thomas Gleixner72491642017-09-13 23:29:10 +02003423 return 0;
Marc Zyngieraca268d2014-12-12 10:51:23 +00003424}
3425
3426static void its_irq_domain_deactivate(struct irq_domain *domain,
3427 struct irq_data *d)
3428{
3429 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3430 u32 event = its_get_event_id(d);
3431
3432 /* Stop the delivery of interrupts */
3433 its_send_discard(its_dev, event);
3434}
3435
Marc Zyngierb48ac832014-11-24 14:35:16 +00003436static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
3437 unsigned int nr_irqs)
3438{
3439 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
3440 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier9791ec72019-01-29 10:02:33 +00003441 struct its_node *its = its_dev->its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003442 int i;
3443
Marc Zyngierc9c96e32019-09-05 14:56:47 +01003444 bitmap_release_region(its_dev->event_map.lpi_map,
3445 its_get_event_id(irq_domain_get_irq_data(domain, virq)),
3446 get_count_order(nr_irqs));
3447
Marc Zyngierb48ac832014-11-24 14:35:16 +00003448 for (i = 0; i < nr_irqs; i++) {
3449 struct irq_data *data = irq_domain_get_irq_data(domain,
3450 virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003451 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00003452 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003453 }
3454
Marc Zyngier9791ec72019-01-29 10:02:33 +00003455 mutex_lock(&its->dev_alloc_lock);
3456
3457 /*
3458 * If all interrupts have been freed, start mopping the
3459 * floor. This is conditionned on the device not being shared.
3460 */
3461 if (!its_dev->shared &&
3462 bitmap_empty(its_dev->event_map.lpi_map,
Marc Zyngier591e5be2015-07-17 10:46:42 +01003463 its_dev->event_map.nr_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003464 its_lpi_free(its_dev->event_map.lpi_map,
3465 its_dev->event_map.lpi_base,
3466 its_dev->event_map.nr_lpis);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003467
3468 /* Unmap device/itt */
3469 its_send_mapd(its_dev, 0);
3470 its_free_device(its_dev);
3471 }
3472
Marc Zyngier9791ec72019-01-29 10:02:33 +00003473 mutex_unlock(&its->dev_alloc_lock);
3474
Marc Zyngierb48ac832014-11-24 14:35:16 +00003475 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3476}
3477
3478static const struct irq_domain_ops its_domain_ops = {
3479 .alloc = its_irq_domain_alloc,
3480 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00003481 .activate = its_irq_domain_activate,
3482 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00003483};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003484
Marc Zyngier20b3d542016-12-20 15:23:22 +00003485/*
3486 * This is insane.
3487 *
Marc Zyngier0684c702019-12-24 11:10:30 +00003488 * If a GICv4.0 doesn't implement Direct LPIs (which is extremely
Marc Zyngier20b3d542016-12-20 15:23:22 +00003489 * likely), the only way to perform an invalidate is to use a fake
3490 * device to issue an INV command, implying that the LPI has first
3491 * been mapped to some event on that device. Since this is not exactly
3492 * cheap, we try to keep that mapping around as long as possible, and
3493 * only issue an UNMAP if we're short on available slots.
3494 *
3495 * Broken by design(tm).
Marc Zyngier0684c702019-12-24 11:10:30 +00003496 *
3497 * GICv4.1, on the other hand, mandates that we're able to invalidate
3498 * by writing to a MMIO register. It doesn't implement the whole of
3499 * DirectLPI, but that's good enough. And most of the time, we don't
3500 * even have to invalidate anything, as the redistributor can be told
3501 * whether to generate a doorbell or not (we thus leave it enabled,
3502 * always).
Marc Zyngier20b3d542016-12-20 15:23:22 +00003503 */
3504static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
3505{
Marc Zyngier0684c702019-12-24 11:10:30 +00003506 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3507 if (gic_rdists->has_rvpeid)
3508 return;
3509
Marc Zyngier20b3d542016-12-20 15:23:22 +00003510 /* Already unmapped? */
3511 if (vpe->vpe_proxy_event == -1)
3512 return;
3513
3514 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
3515 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
3516
3517 /*
3518 * We don't track empty slots at all, so let's move the
3519 * next_victim pointer if we can quickly reuse that slot
3520 * instead of nuking an existing entry. Not clear that this is
3521 * always a win though, and this might just generate a ripple
3522 * effect... Let's just hope VPEs don't migrate too often.
3523 */
3524 if (vpe_proxy.vpes[vpe_proxy.next_victim])
3525 vpe_proxy.next_victim = vpe->vpe_proxy_event;
3526
3527 vpe->vpe_proxy_event = -1;
3528}
3529
3530static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
3531{
Marc Zyngier0684c702019-12-24 11:10:30 +00003532 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3533 if (gic_rdists->has_rvpeid)
3534 return;
3535
Marc Zyngier20b3d542016-12-20 15:23:22 +00003536 if (!gic_rdists->has_direct_lpi) {
3537 unsigned long flags;
3538
3539 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3540 its_vpe_db_proxy_unmap_locked(vpe);
3541 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3542 }
3543}
3544
3545static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
3546{
Marc Zyngier0684c702019-12-24 11:10:30 +00003547 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3548 if (gic_rdists->has_rvpeid)
3549 return;
3550
Marc Zyngier20b3d542016-12-20 15:23:22 +00003551 /* Already mapped? */
3552 if (vpe->vpe_proxy_event != -1)
3553 return;
3554
3555 /* This slot was already allocated. Kick the other VPE out. */
3556 if (vpe_proxy.vpes[vpe_proxy.next_victim])
3557 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
3558
3559 /* Map the new VPE instead */
3560 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
3561 vpe->vpe_proxy_event = vpe_proxy.next_victim;
3562 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
3563
3564 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
3565 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
3566}
3567
Marc Zyngier958b90d2017-08-18 16:14:17 +01003568static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
3569{
3570 unsigned long flags;
3571 struct its_collection *target_col;
3572
Marc Zyngier0684c702019-12-24 11:10:30 +00003573 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3574 if (gic_rdists->has_rvpeid)
3575 return;
3576
Marc Zyngier958b90d2017-08-18 16:14:17 +01003577 if (gic_rdists->has_direct_lpi) {
3578 void __iomem *rdbase;
3579
3580 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
3581 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
Marc Zyngier2f4f0642019-11-08 16:57:56 +00003582 wait_for_syncr(rdbase);
Marc Zyngier958b90d2017-08-18 16:14:17 +01003583
3584 return;
3585 }
3586
3587 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3588
3589 its_vpe_db_proxy_map_locked(vpe);
3590
3591 target_col = &vpe_proxy.dev->its->collections[to];
3592 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
3593 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
3594
3595 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3596}
3597
Marc Zyngier3171a472016-12-20 15:17:28 +00003598static int its_vpe_set_affinity(struct irq_data *d,
3599 const struct cpumask *mask_val,
3600 bool force)
3601{
3602 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003603 int from, cpu = cpumask_first(mask_val);
Marc Zyngierf3a059212020-03-04 20:33:10 +00003604 unsigned long flags;
Marc Zyngier3171a472016-12-20 15:17:28 +00003605
3606 /*
3607 * Changing affinity is mega expensive, so let's be as lazy as
Marc Zyngier20b3d542016-12-20 15:23:22 +00003608 * we can and only do it if we really have to. Also, if mapped
Marc Zyngier958b90d2017-08-18 16:14:17 +01003609 * into the proxy device, we need to move the doorbell
3610 * interrupt to its new location.
Marc Zyngierf3a059212020-03-04 20:33:10 +00003611 *
3612 * Another thing is that changing the affinity of a vPE affects
3613 * *other interrupts* such as all the vLPIs that are routed to
3614 * this vPE. This means that the irq_desc lock is not enough to
3615 * protect us, and that we must ensure nobody samples vpe->col_idx
3616 * during the update, hence the lock below which must also be
3617 * taken on any vLPI handling path that evaluates vpe->col_idx.
Marc Zyngier3171a472016-12-20 15:17:28 +00003618 */
Marc Zyngierf3a059212020-03-04 20:33:10 +00003619 from = vpe_to_cpuid_lock(vpe, &flags);
3620 if (from == cpu)
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003621 goto out;
Marc Zyngier958b90d2017-08-18 16:14:17 +01003622
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003623 vpe->col_idx = cpu;
Marc Zyngier3171a472016-12-20 15:17:28 +00003624
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003625 /*
3626 * GICv4.1 allows us to skip VMOVP if moving to a cpu whose RD
3627 * is sharing its VPE table with the current one.
3628 */
3629 if (gic_data_rdist_cpu(cpu)->vpe_table_mask &&
3630 cpumask_test_cpu(from, gic_data_rdist_cpu(cpu)->vpe_table_mask))
3631 goto out;
3632
3633 its_send_vmovp(vpe);
3634 its_vpe_db_proxy_move(vpe, from, cpu);
3635
3636out:
Marc Zyngier44c4c252017-10-19 10:11:34 +01003637 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngierf3a059212020-03-04 20:33:10 +00003638 vpe_to_cpuid_unlock(vpe, flags);
Marc Zyngier44c4c252017-10-19 10:11:34 +01003639
Marc Zyngier3171a472016-12-20 15:17:28 +00003640 return IRQ_SET_MASK_OK_DONE;
3641}
3642
Marc Zyngiere643d802016-12-20 15:09:31 +00003643static void its_vpe_schedule(struct its_vpe *vpe)
3644{
Robin Murphy50c33092018-02-16 16:57:56 +00003645 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00003646 u64 val;
3647
3648 /* Schedule the VPE */
3649 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
3650 GENMASK_ULL(51, 12);
3651 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
3652 val |= GICR_VPROPBASER_RaWb;
3653 val |= GICR_VPROPBASER_InnerShareable;
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003654 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
Marc Zyngiere643d802016-12-20 15:09:31 +00003655
3656 val = virt_to_phys(page_address(vpe->vpt_page)) &
3657 GENMASK_ULL(51, 16);
3658 val |= GICR_VPENDBASER_RaWaWb;
3659 val |= GICR_VPENDBASER_NonShareable;
3660 /*
3661 * There is no good way of finding out if the pending table is
3662 * empty as we can race against the doorbell interrupt very
3663 * easily. So in the end, vpe->pending_last is only an
3664 * indication that the vcpu has something pending, not one
3665 * that the pending table is empty. A good implementation
3666 * would be able to read its coarse map pretty quickly anyway,
3667 * making this a tolerable issue.
3668 */
3669 val |= GICR_VPENDBASER_PendingLast;
3670 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
3671 val |= GICR_VPENDBASER_Valid;
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003672 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
Marc Zyngiere643d802016-12-20 15:09:31 +00003673}
3674
3675static void its_vpe_deschedule(struct its_vpe *vpe)
3676{
Robin Murphy50c33092018-02-16 16:57:56 +00003677 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00003678 u64 val;
3679
Marc Zyngiere64fab12019-12-24 11:10:35 +00003680 val = its_clear_vpend_valid(vlpi_base, 0, 0);
Marc Zyngiere643d802016-12-20 15:09:31 +00003681
Marc Zyngiere64fab12019-12-24 11:10:35 +00003682 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
3683 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
Marc Zyngiere643d802016-12-20 15:09:31 +00003684}
3685
Marc Zyngier40619a22017-10-08 15:16:09 +01003686static void its_vpe_invall(struct its_vpe *vpe)
3687{
3688 struct its_node *its;
3689
3690 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00003691 if (!is_v4(its))
Marc Zyngier40619a22017-10-08 15:16:09 +01003692 continue;
3693
Marc Zyngier2247e1b2017-10-08 18:50:36 +01003694 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
3695 continue;
3696
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01003697 /*
3698 * Sending a VINVALL to a single ITS is enough, as all
3699 * we need is to reach the redistributors.
3700 */
Marc Zyngier40619a22017-10-08 15:16:09 +01003701 its_send_vinvall(its, vpe);
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01003702 return;
Marc Zyngier40619a22017-10-08 15:16:09 +01003703 }
3704}
3705
Marc Zyngiere643d802016-12-20 15:09:31 +00003706static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
3707{
3708 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3709 struct its_cmd_info *info = vcpu_info;
3710
3711 switch (info->cmd_type) {
3712 case SCHEDULE_VPE:
3713 its_vpe_schedule(vpe);
3714 return 0;
3715
3716 case DESCHEDULE_VPE:
3717 its_vpe_deschedule(vpe);
3718 return 0;
3719
Marc Zyngier5e2f7642016-12-20 15:10:50 +00003720 case INVALL_VPE:
Marc Zyngier40619a22017-10-08 15:16:09 +01003721 its_vpe_invall(vpe);
Marc Zyngier5e2f7642016-12-20 15:10:50 +00003722 return 0;
3723
Marc Zyngiere643d802016-12-20 15:09:31 +00003724 default:
3725 return -EINVAL;
3726 }
3727}
3728
Marc Zyngier20b3d542016-12-20 15:23:22 +00003729static void its_vpe_send_cmd(struct its_vpe *vpe,
3730 void (*cmd)(struct its_device *, u32))
3731{
3732 unsigned long flags;
3733
3734 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3735
3736 its_vpe_db_proxy_map_locked(vpe);
3737 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
3738
3739 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3740}
3741
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003742static void its_vpe_send_inv(struct irq_data *d)
3743{
3744 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003745
Marc Zyngier20b3d542016-12-20 15:23:22 +00003746 if (gic_rdists->has_direct_lpi) {
3747 void __iomem *rdbase;
3748
Marc Zyngier425c09b2019-11-08 16:57:57 +00003749 /* Target the redistributor this VPE is currently known on */
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003750 raw_spin_lock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003751 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
Marc Zyngier425c09b2019-11-08 16:57:57 +00003752 gic_write_lpir(d->parent_data->hwirq, rdbase + GICR_INVLPIR);
Marc Zyngier2f4f0642019-11-08 16:57:56 +00003753 wait_for_syncr(rdbase);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003754 raw_spin_unlock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003755 } else {
3756 its_vpe_send_cmd(vpe, its_send_inv);
3757 }
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003758}
3759
3760static void its_vpe_mask_irq(struct irq_data *d)
3761{
3762 /*
3763 * We need to unmask the LPI, which is described by the parent
3764 * irq_data. Instead of calling into the parent (which won't
3765 * exactly do the right thing, let's simply use the
3766 * parent_data pointer. Yes, I'm naughty.
3767 */
3768 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
3769 its_vpe_send_inv(d);
3770}
3771
3772static void its_vpe_unmask_irq(struct irq_data *d)
3773{
3774 /* Same hack as above... */
3775 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
3776 its_vpe_send_inv(d);
3777}
3778
Marc Zyngiere57a3e282017-07-31 14:47:24 +01003779static int its_vpe_set_irqchip_state(struct irq_data *d,
3780 enum irqchip_irq_state which,
3781 bool state)
3782{
3783 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3784
3785 if (which != IRQCHIP_STATE_PENDING)
3786 return -EINVAL;
3787
3788 if (gic_rdists->has_direct_lpi) {
3789 void __iomem *rdbase;
3790
3791 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
3792 if (state) {
3793 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
3794 } else {
3795 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
Marc Zyngier2f4f0642019-11-08 16:57:56 +00003796 wait_for_syncr(rdbase);
Marc Zyngiere57a3e282017-07-31 14:47:24 +01003797 }
3798 } else {
3799 if (state)
3800 its_vpe_send_cmd(vpe, its_send_int);
3801 else
3802 its_vpe_send_cmd(vpe, its_send_clear);
3803 }
3804
3805 return 0;
3806}
3807
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003808static struct irq_chip its_vpe_irq_chip = {
3809 .name = "GICv4-vpe",
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003810 .irq_mask = its_vpe_mask_irq,
3811 .irq_unmask = its_vpe_unmask_irq,
3812 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier3171a472016-12-20 15:17:28 +00003813 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngiere57a3e282017-07-31 14:47:24 +01003814 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
Marc Zyngiere643d802016-12-20 15:09:31 +00003815 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003816};
3817
Marc Zyngierd97c97b2019-12-24 11:10:33 +00003818static struct its_node *find_4_1_its(void)
3819{
3820 static struct its_node *its = NULL;
3821
3822 if (!its) {
3823 list_for_each_entry(its, &its_nodes, entry) {
3824 if (is_v4_1(its))
3825 return its;
3826 }
3827
3828 /* Oops? */
3829 its = NULL;
3830 }
3831
3832 return its;
3833}
3834
3835static void its_vpe_4_1_send_inv(struct irq_data *d)
3836{
3837 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3838 struct its_node *its;
3839
3840 /*
3841 * GICv4.1 wants doorbells to be invalidated using the
3842 * INVDB command in order to be broadcast to all RDs. Send
3843 * it to the first valid ITS, and let the HW do its magic.
3844 */
3845 its = find_4_1_its();
3846 if (its)
3847 its_send_invdb(its, vpe);
3848}
3849
3850static void its_vpe_4_1_mask_irq(struct irq_data *d)
3851{
3852 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
3853 its_vpe_4_1_send_inv(d);
3854}
3855
3856static void its_vpe_4_1_unmask_irq(struct irq_data *d)
3857{
3858 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
3859 its_vpe_4_1_send_inv(d);
3860}
3861
Marc Zyngier91bf6392019-12-24 11:10:34 +00003862static void its_vpe_4_1_schedule(struct its_vpe *vpe,
3863 struct its_cmd_info *info)
3864{
3865 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3866 u64 val = 0;
3867
3868 /* Schedule the VPE */
3869 val |= GICR_VPENDBASER_Valid;
3870 val |= info->g0en ? GICR_VPENDBASER_4_1_VGRP0EN : 0;
3871 val |= info->g1en ? GICR_VPENDBASER_4_1_VGRP1EN : 0;
3872 val |= FIELD_PREP(GICR_VPENDBASER_4_1_VPEID, vpe->vpe_id);
3873
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003874 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
Marc Zyngier91bf6392019-12-24 11:10:34 +00003875}
3876
Marc Zyngiere64fab12019-12-24 11:10:35 +00003877static void its_vpe_4_1_deschedule(struct its_vpe *vpe,
3878 struct its_cmd_info *info)
3879{
3880 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3881 u64 val;
3882
3883 if (info->req_db) {
3884 /*
3885 * vPE is going to block: make the vPE non-resident with
3886 * PendingLast clear and DB set. The GIC guarantees that if
3887 * we read-back PendingLast clear, then a doorbell will be
3888 * delivered when an interrupt comes.
3889 */
3890 val = its_clear_vpend_valid(vlpi_base,
3891 GICR_VPENDBASER_PendingLast,
3892 GICR_VPENDBASER_4_1_DB);
3893 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
3894 } else {
3895 /*
3896 * We're not blocking, so just make the vPE non-resident
3897 * with PendingLast set, indicating that we'll be back.
3898 */
3899 val = its_clear_vpend_valid(vlpi_base,
3900 0,
3901 GICR_VPENDBASER_PendingLast);
3902 vpe->pending_last = true;
3903 }
3904}
3905
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003906static void its_vpe_4_1_invall(struct its_vpe *vpe)
3907{
3908 void __iomem *rdbase;
3909 u64 val;
3910
3911 val = GICR_INVALLR_V;
3912 val |= FIELD_PREP(GICR_INVALLR_VPEID, vpe->vpe_id);
3913
3914 /* Target the redistributor this vPE is currently known on */
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003915 raw_spin_lock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003916 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
3917 gic_write_lpir(val, rdbase + GICR_INVALLR);
Zenghui Yub978c252020-03-04 20:33:11 +00003918
3919 wait_for_syncr(rdbase);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003920 raw_spin_unlock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003921}
3922
Marc Zyngier29c647f2019-12-24 11:10:32 +00003923static int its_vpe_4_1_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
3924{
Marc Zyngier91bf6392019-12-24 11:10:34 +00003925 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003926 struct its_cmd_info *info = vcpu_info;
3927
3928 switch (info->cmd_type) {
3929 case SCHEDULE_VPE:
Marc Zyngier91bf6392019-12-24 11:10:34 +00003930 its_vpe_4_1_schedule(vpe, info);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003931 return 0;
3932
3933 case DESCHEDULE_VPE:
Marc Zyngiere64fab12019-12-24 11:10:35 +00003934 its_vpe_4_1_deschedule(vpe, info);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003935 return 0;
3936
3937 case INVALL_VPE:
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003938 its_vpe_4_1_invall(vpe);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003939 return 0;
3940
3941 default:
3942 return -EINVAL;
3943 }
3944}
3945
3946static struct irq_chip its_vpe_4_1_irq_chip = {
3947 .name = "GICv4.1-vpe",
Marc Zyngierd97c97b2019-12-24 11:10:33 +00003948 .irq_mask = its_vpe_4_1_mask_irq,
3949 .irq_unmask = its_vpe_4_1_unmask_irq,
Marc Zyngier29c647f2019-12-24 11:10:32 +00003950 .irq_eoi = irq_chip_eoi_parent,
3951 .irq_set_affinity = its_vpe_set_affinity,
3952 .irq_set_vcpu_affinity = its_vpe_4_1_set_vcpu_affinity,
3953};
3954
Marc Zyngiere252cf82020-03-04 20:33:16 +00003955static void its_configure_sgi(struct irq_data *d, bool clear)
3956{
3957 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3958 struct its_cmd_desc desc;
3959
3960 desc.its_vsgi_cmd.vpe = vpe;
3961 desc.its_vsgi_cmd.sgi = d->hwirq;
3962 desc.its_vsgi_cmd.priority = vpe->sgi_config[d->hwirq].priority;
3963 desc.its_vsgi_cmd.enable = vpe->sgi_config[d->hwirq].enabled;
3964 desc.its_vsgi_cmd.group = vpe->sgi_config[d->hwirq].group;
3965 desc.its_vsgi_cmd.clear = clear;
3966
3967 /*
3968 * GICv4.1 allows us to send VSGI commands to any ITS as long as the
3969 * destination VPE is mapped there. Since we map them eagerly at
3970 * activation time, we're pretty sure the first GICv4.1 ITS will do.
3971 */
3972 its_send_single_vcommand(find_4_1_its(), its_build_vsgi_cmd, &desc);
3973}
3974
Marc Zyngierb4e8d642020-03-04 20:33:17 +00003975static void its_sgi_mask_irq(struct irq_data *d)
3976{
3977 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3978
3979 vpe->sgi_config[d->hwirq].enabled = false;
3980 its_configure_sgi(d, false);
3981}
3982
3983static void its_sgi_unmask_irq(struct irq_data *d)
3984{
3985 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3986
3987 vpe->sgi_config[d->hwirq].enabled = true;
3988 its_configure_sgi(d, false);
3989}
3990
Marc Zyngier166cba72020-03-04 20:33:15 +00003991static int its_sgi_set_affinity(struct irq_data *d,
3992 const struct cpumask *mask_val,
3993 bool force)
3994{
3995 /*
3996 * There is no notion of affinity for virtual SGIs, at least
3997 * not on the host (since they can only be targetting a vPE).
3998 * Tell the kernel we've done whatever it asked for.
3999 */
4000 return IRQ_SET_MASK_OK;
4001}
4002
Marc Zyngier7017ff02020-03-04 20:33:18 +00004003static int its_sgi_set_irqchip_state(struct irq_data *d,
4004 enum irqchip_irq_state which,
4005 bool state)
4006{
4007 if (which != IRQCHIP_STATE_PENDING)
4008 return -EINVAL;
4009
4010 if (state) {
4011 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4012 struct its_node *its = find_4_1_its();
4013 u64 val;
4014
4015 val = FIELD_PREP(GITS_SGIR_VPEID, vpe->vpe_id);
4016 val |= FIELD_PREP(GITS_SGIR_VINTID, d->hwirq);
4017 writeq_relaxed(val, its->sgir_base + GITS_SGIR - SZ_128K);
4018 } else {
4019 its_configure_sgi(d, true);
4020 }
4021
4022 return 0;
4023}
4024
4025static int its_sgi_get_irqchip_state(struct irq_data *d,
4026 enum irqchip_irq_state which, bool *val)
4027{
4028 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4029 void __iomem *base;
4030 unsigned long flags;
4031 u32 count = 1000000; /* 1s! */
4032 u32 status;
4033 int cpu;
4034
4035 if (which != IRQCHIP_STATE_PENDING)
4036 return -EINVAL;
4037
4038 /*
4039 * Locking galore! We can race against two different events:
4040 *
4041 * - Concurent vPE affinity change: we must make sure it cannot
4042 * happen, or we'll talk to the wrong redistributor. This is
4043 * identical to what happens with vLPIs.
4044 *
4045 * - Concurrent VSGIPENDR access: As it involves accessing two
4046 * MMIO registers, this must be made atomic one way or another.
4047 */
4048 cpu = vpe_to_cpuid_lock(vpe, &flags);
4049 raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock);
4050 base = gic_data_rdist_cpu(cpu)->rd_base + SZ_128K;
4051 writel_relaxed(vpe->vpe_id, base + GICR_VSGIR);
4052 do {
4053 status = readl_relaxed(base + GICR_VSGIPENDR);
4054 if (!(status & GICR_VSGIPENDR_BUSY))
4055 goto out;
4056
4057 count--;
4058 if (!count) {
4059 pr_err_ratelimited("Unable to get SGI status\n");
4060 goto out;
4061 }
4062 cpu_relax();
4063 udelay(1);
4064 } while (count);
4065
4066out:
4067 raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock);
4068 vpe_to_cpuid_unlock(vpe, flags);
4069
4070 if (!count)
4071 return -ENXIO;
4072
4073 *val = !!(status & (1 << d->hwirq));
4074
4075 return 0;
4076}
4077
Marc Zyngier05d32df2020-03-04 20:33:19 +00004078static int its_sgi_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
4079{
4080 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4081 struct its_cmd_info *info = vcpu_info;
4082
4083 switch (info->cmd_type) {
4084 case PROP_UPDATE_VSGI:
4085 vpe->sgi_config[d->hwirq].priority = info->priority;
4086 vpe->sgi_config[d->hwirq].group = info->group;
4087 its_configure_sgi(d, false);
4088 return 0;
4089
4090 default:
4091 return -EINVAL;
4092 }
4093}
4094
Marc Zyngier166cba72020-03-04 20:33:15 +00004095static struct irq_chip its_sgi_irq_chip = {
4096 .name = "GICv4.1-sgi",
Marc Zyngierb4e8d642020-03-04 20:33:17 +00004097 .irq_mask = its_sgi_mask_irq,
4098 .irq_unmask = its_sgi_unmask_irq,
Marc Zyngier166cba72020-03-04 20:33:15 +00004099 .irq_set_affinity = its_sgi_set_affinity,
Marc Zyngier7017ff02020-03-04 20:33:18 +00004100 .irq_set_irqchip_state = its_sgi_set_irqchip_state,
4101 .irq_get_irqchip_state = its_sgi_get_irqchip_state,
Marc Zyngier05d32df2020-03-04 20:33:19 +00004102 .irq_set_vcpu_affinity = its_sgi_set_vcpu_affinity,
Marc Zyngier166cba72020-03-04 20:33:15 +00004103};
4104
4105static int its_sgi_irq_domain_alloc(struct irq_domain *domain,
4106 unsigned int virq, unsigned int nr_irqs,
4107 void *args)
4108{
4109 struct its_vpe *vpe = args;
4110 int i;
4111
4112 /* Yes, we do want 16 SGIs */
4113 WARN_ON(nr_irqs != 16);
4114
4115 for (i = 0; i < 16; i++) {
4116 vpe->sgi_config[i].priority = 0;
4117 vpe->sgi_config[i].enabled = false;
4118 vpe->sgi_config[i].group = false;
4119
4120 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
4121 &its_sgi_irq_chip, vpe);
4122 irq_set_status_flags(virq + i, IRQ_DISABLE_UNLAZY);
4123 }
4124
4125 return 0;
4126}
4127
4128static void its_sgi_irq_domain_free(struct irq_domain *domain,
4129 unsigned int virq,
4130 unsigned int nr_irqs)
4131{
4132 /* Nothing to do */
4133}
4134
4135static int its_sgi_irq_domain_activate(struct irq_domain *domain,
4136 struct irq_data *d, bool reserve)
4137{
Marc Zyngiere252cf82020-03-04 20:33:16 +00004138 /* Write out the initial SGI configuration */
4139 its_configure_sgi(d, false);
Marc Zyngier166cba72020-03-04 20:33:15 +00004140 return 0;
4141}
4142
4143static void its_sgi_irq_domain_deactivate(struct irq_domain *domain,
4144 struct irq_data *d)
4145{
Marc Zyngiere252cf82020-03-04 20:33:16 +00004146 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4147
4148 /*
4149 * The VSGI command is awkward:
4150 *
4151 * - To change the configuration, CLEAR must be set to false,
4152 * leaving the pending bit unchanged.
4153 * - To clear the pending bit, CLEAR must be set to true, leaving
4154 * the configuration unchanged.
4155 *
4156 * You just can't do both at once, hence the two commands below.
4157 */
4158 vpe->sgi_config[d->hwirq].enabled = false;
4159 its_configure_sgi(d, false);
4160 its_configure_sgi(d, true);
Marc Zyngier166cba72020-03-04 20:33:15 +00004161}
4162
4163static const struct irq_domain_ops its_sgi_domain_ops = {
4164 .alloc = its_sgi_irq_domain_alloc,
4165 .free = its_sgi_irq_domain_free,
4166 .activate = its_sgi_irq_domain_activate,
4167 .deactivate = its_sgi_irq_domain_deactivate,
4168};
4169
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004170static int its_vpe_id_alloc(void)
4171{
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05004172 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004173}
4174
4175static void its_vpe_id_free(u16 id)
4176{
4177 ida_simple_remove(&its_vpeid_ida, id);
4178}
4179
4180static int its_vpe_init(struct its_vpe *vpe)
4181{
4182 struct page *vpt_page;
4183 int vpe_id;
4184
4185 /* Allocate vpe_id */
4186 vpe_id = its_vpe_id_alloc();
4187 if (vpe_id < 0)
4188 return vpe_id;
4189
4190 /* Allocate VPT */
4191 vpt_page = its_allocate_pending_table(GFP_KERNEL);
4192 if (!vpt_page) {
4193 its_vpe_id_free(vpe_id);
4194 return -ENOMEM;
4195 }
4196
4197 if (!its_alloc_vpe_table(vpe_id)) {
4198 its_vpe_id_free(vpe_id);
Nianyao Tang34f8eb92019-07-26 17:32:57 +08004199 its_free_pending_table(vpt_page);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004200 return -ENOMEM;
4201 }
4202
Marc Zyngierf3a059212020-03-04 20:33:10 +00004203 raw_spin_lock_init(&vpe->vpe_lock);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004204 vpe->vpe_id = vpe_id;
4205 vpe->vpt_page = vpt_page;
Marc Zyngier64edfaa2019-12-24 11:10:29 +00004206 if (gic_rdists->has_rvpeid)
4207 atomic_set(&vpe->vmapp_count, 0);
4208 else
4209 vpe->vpe_proxy_event = -1;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004210
4211 return 0;
4212}
4213
4214static void its_vpe_teardown(struct its_vpe *vpe)
4215{
Marc Zyngier20b3d542016-12-20 15:23:22 +00004216 its_vpe_db_proxy_unmap(vpe);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004217 its_vpe_id_free(vpe->vpe_id);
4218 its_free_pending_table(vpe->vpt_page);
4219}
4220
4221static void its_vpe_irq_domain_free(struct irq_domain *domain,
4222 unsigned int virq,
4223 unsigned int nr_irqs)
4224{
4225 struct its_vm *vm = domain->host_data;
4226 int i;
4227
4228 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
4229
4230 for (i = 0; i < nr_irqs; i++) {
4231 struct irq_data *data = irq_domain_get_irq_data(domain,
4232 virq + i);
4233 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
4234
4235 BUG_ON(vm != vpe->its_vm);
4236
4237 clear_bit(data->hwirq, vm->db_bitmap);
4238 its_vpe_teardown(vpe);
4239 irq_domain_reset_irq_data(data);
4240 }
4241
4242 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01004243 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004244 its_free_prop_table(vm->vprop_page);
4245 }
4246}
4247
4248static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
4249 unsigned int nr_irqs, void *args)
4250{
Marc Zyngier29c647f2019-12-24 11:10:32 +00004251 struct irq_chip *irqchip = &its_vpe_irq_chip;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004252 struct its_vm *vm = args;
4253 unsigned long *bitmap;
4254 struct page *vprop_page;
4255 int base, nr_ids, i, err = 0;
4256
4257 BUG_ON(!vm);
4258
Marc Zyngier38dd7c42018-05-27 17:03:03 +01004259 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004260 if (!bitmap)
4261 return -ENOMEM;
4262
4263 if (nr_ids < nr_irqs) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01004264 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004265 return -ENOMEM;
4266 }
4267
4268 vprop_page = its_allocate_prop_table(GFP_KERNEL);
4269 if (!vprop_page) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01004270 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004271 return -ENOMEM;
4272 }
4273
4274 vm->db_bitmap = bitmap;
4275 vm->db_lpi_base = base;
4276 vm->nr_db_lpis = nr_ids;
4277 vm->vprop_page = vprop_page;
4278
Marc Zyngier29c647f2019-12-24 11:10:32 +00004279 if (gic_rdists->has_rvpeid)
4280 irqchip = &its_vpe_4_1_irq_chip;
4281
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004282 for (i = 0; i < nr_irqs; i++) {
4283 vm->vpes[i]->vpe_db_lpi = base + i;
4284 err = its_vpe_init(vm->vpes[i]);
4285 if (err)
4286 break;
4287 err = its_irq_gic_domain_alloc(domain, virq + i,
4288 vm->vpes[i]->vpe_db_lpi);
4289 if (err)
4290 break;
4291 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
Marc Zyngier29c647f2019-12-24 11:10:32 +00004292 irqchip, vm->vpes[i]);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004293 set_bit(i, bitmap);
4294 }
4295
4296 if (err) {
4297 if (i > 0)
4298 its_vpe_irq_domain_free(domain, virq, i - 1);
4299
Marc Zyngier38dd7c42018-05-27 17:03:03 +01004300 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004301 its_free_prop_table(vprop_page);
4302 }
4303
4304 return err;
4305}
4306
Thomas Gleixner72491642017-09-13 23:29:10 +02004307static int its_vpe_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01004308 struct irq_data *d, bool reserve)
Marc Zyngiereb781922016-12-20 14:47:05 +00004309{
4310 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier40619a22017-10-08 15:16:09 +01004311 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00004312
Marc Zyngier009384b2020-03-04 20:33:23 +00004313 /*
4314 * If we use the list map, we issue VMAPP on demand... Unless
4315 * we're on a GICv4.1 and we eagerly map the VPE on all ITSs
4316 * so that VSGIs can work.
4317 */
4318 if (!gic_requires_eager_mapping())
Marc Zyngier6ef930f2017-11-07 10:04:38 +00004319 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00004320
4321 /* Map the VPE to the first possible CPU */
4322 vpe->col_idx = cpumask_first(cpu_online_mask);
Marc Zyngier40619a22017-10-08 15:16:09 +01004323
4324 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004325 if (!is_v4(its))
Marc Zyngier40619a22017-10-08 15:16:09 +01004326 continue;
4327
Marc Zyngier75fd9512017-10-08 18:46:39 +01004328 its_send_vmapp(its, vpe, true);
Marc Zyngier40619a22017-10-08 15:16:09 +01004329 its_send_vinvall(its, vpe);
4330 }
4331
Marc Zyngier44c4c252017-10-19 10:11:34 +01004332 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
4333
Thomas Gleixner72491642017-09-13 23:29:10 +02004334 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00004335}
4336
4337static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
4338 struct irq_data *d)
4339{
4340 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier75fd9512017-10-08 18:46:39 +01004341 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00004342
Marc Zyngier2247e1b2017-10-08 18:50:36 +01004343 /*
Marc Zyngier009384b2020-03-04 20:33:23 +00004344 * If we use the list map on GICv4.0, we unmap the VPE once no
4345 * VLPIs are associated with the VM.
Marc Zyngier2247e1b2017-10-08 18:50:36 +01004346 */
Marc Zyngier009384b2020-03-04 20:33:23 +00004347 if (!gic_requires_eager_mapping())
Marc Zyngier2247e1b2017-10-08 18:50:36 +01004348 return;
4349
Marc Zyngier75fd9512017-10-08 18:46:39 +01004350 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004351 if (!is_v4(its))
Marc Zyngier75fd9512017-10-08 18:46:39 +01004352 continue;
4353
4354 its_send_vmapp(its, vpe, false);
4355 }
Marc Zyngiereb781922016-12-20 14:47:05 +00004356}
4357
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004358static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004359 .alloc = its_vpe_irq_domain_alloc,
4360 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00004361 .activate = its_vpe_irq_domain_activate,
4362 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004363};
4364
Yun Wu4559fbb2015-03-06 16:37:50 +00004365static int its_force_quiescent(void __iomem *base)
4366{
4367 u32 count = 1000000; /* 1s */
4368 u32 val;
4369
4370 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07004371 /*
4372 * GIC architecture specification requires the ITS to be both
4373 * disabled and quiescent for writes to GITS_BASER<n> or
4374 * GITS_CBASER to not have UNPREDICTABLE results.
4375 */
4376 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00004377 return 0;
4378
4379 /* Disable the generation of all interrupts to this ITS */
Marc Zyngierd51c4b42017-06-27 21:24:25 +01004380 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
Yun Wu4559fbb2015-03-06 16:37:50 +00004381 writel_relaxed(val, base + GITS_CTLR);
4382
4383 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
4384 while (1) {
4385 val = readl_relaxed(base + GITS_CTLR);
4386 if (val & GITS_CTLR_QUIESCENT)
4387 return 0;
4388
4389 count--;
4390 if (!count)
4391 return -EBUSY;
4392
4393 cpu_relax();
4394 udelay(1);
4395 }
4396}
4397
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004398static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
Robert Richter94100972015-09-21 22:58:38 +02004399{
4400 struct its_node *its = data;
4401
Marc Zyngier576a8342019-11-08 16:58:00 +00004402 /* erratum 22375: only alloc 8MB table size (20 bits) */
4403 its->typer &= ~GITS_TYPER_DEVBITS;
4404 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, 20 - 1);
Robert Richter94100972015-09-21 22:58:38 +02004405 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004406
4407 return true;
Robert Richter94100972015-09-21 22:58:38 +02004408}
4409
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004410static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02004411{
4412 struct its_node *its = data;
4413
4414 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004415
4416 return true;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02004417}
4418
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004419static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
Shanker Donthineni90922a22017-03-07 08:20:38 -06004420{
4421 struct its_node *its = data;
4422
4423 /* On QDF2400, the size of the ITE is 16Bytes */
Marc Zyngierffedbf02019-11-08 16:57:59 +00004424 its->typer &= ~GITS_TYPER_ITT_ENTRY_SIZE;
4425 its->typer |= FIELD_PREP(GITS_TYPER_ITT_ENTRY_SIZE, 16 - 1);
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004426
4427 return true;
Shanker Donthineni90922a22017-03-07 08:20:38 -06004428}
4429
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004430static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
4431{
4432 struct its_node *its = its_dev->its;
4433
4434 /*
4435 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
4436 * which maps 32-bit writes targeted at a separate window of
4437 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
4438 * with device ID taken from bits [device_id_bits + 1:2] of
4439 * the window offset.
4440 */
4441 return its->pre_its_base + (its_dev->device_id << 2);
4442}
4443
4444static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
4445{
4446 struct its_node *its = data;
4447 u32 pre_its_window[2];
4448 u32 ids;
4449
4450 if (!fwnode_property_read_u32_array(its->fwnode_handle,
4451 "socionext,synquacer-pre-its",
4452 pre_its_window,
4453 ARRAY_SIZE(pre_its_window))) {
4454
4455 its->pre_its_base = pre_its_window[0];
4456 its->get_msi_base = its_irq_get_msi_base_pre_its;
4457
4458 ids = ilog2(pre_its_window[1]) - 2;
Marc Zyngier576a8342019-11-08 16:58:00 +00004459 if (device_ids(its) > ids) {
4460 its->typer &= ~GITS_TYPER_DEVBITS;
4461 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, ids - 1);
4462 }
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004463
4464 /* the pre-ITS breaks isolation, so disable MSI remapping */
4465 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
4466 return true;
4467 }
4468 return false;
4469}
4470
Marc Zyngier5c9a8822017-07-28 21:20:37 +01004471static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
4472{
4473 struct its_node *its = data;
4474
4475 /*
4476 * Hip07 insists on using the wrong address for the VLPI
4477 * page. Trick it into doing the right thing...
4478 */
4479 its->vlpi_redist_offset = SZ_128K;
4480 return true;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00004481}
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004482
Robert Richter67510cc2015-09-21 22:58:37 +02004483static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02004484#ifdef CONFIG_CAVIUM_ERRATUM_22375
4485 {
4486 .desc = "ITS: Cavium errata 22375, 24313",
4487 .iidr = 0xa100034c, /* ThunderX pass 1.x */
4488 .mask = 0xffff0fff,
4489 .init = its_enable_quirk_cavium_22375,
4490 },
4491#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02004492#ifdef CONFIG_CAVIUM_ERRATUM_23144
4493 {
4494 .desc = "ITS: Cavium erratum 23144",
4495 .iidr = 0xa100034c, /* ThunderX pass 1.x */
4496 .mask = 0xffff0fff,
4497 .init = its_enable_quirk_cavium_23144,
4498 },
4499#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06004500#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
4501 {
4502 .desc = "ITS: QDF2400 erratum 0065",
4503 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
4504 .mask = 0xffffffff,
4505 .init = its_enable_quirk_qdf2400_e0065,
4506 },
4507#endif
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004508#ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
4509 {
4510 /*
4511 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
4512 * implementation, but with a 'pre-ITS' added that requires
4513 * special handling in software.
4514 */
4515 .desc = "ITS: Socionext Synquacer pre-ITS",
4516 .iidr = 0x0001143b,
4517 .mask = 0xffffffff,
4518 .init = its_enable_quirk_socionext_synquacer,
4519 },
4520#endif
Marc Zyngier5c9a8822017-07-28 21:20:37 +01004521#ifdef CONFIG_HISILICON_ERRATUM_161600802
4522 {
4523 .desc = "ITS: Hip07 erratum 161600802",
4524 .iidr = 0x00000004,
4525 .mask = 0xffffffff,
4526 .init = its_enable_quirk_hip07_161600802,
4527 },
4528#endif
Robert Richter67510cc2015-09-21 22:58:37 +02004529 {
4530 }
4531};
4532
4533static void its_enable_quirks(struct its_node *its)
4534{
4535 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
4536
4537 gic_enable_quirks(iidr, its_quirks, its);
4538}
4539
Derek Basehoredba0bc72018-02-28 21:48:18 -08004540static int its_save_disable(void)
4541{
4542 struct its_node *its;
4543 int err = 0;
4544
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004545 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004546 list_for_each_entry(its, &its_nodes, entry) {
4547 void __iomem *base;
4548
4549 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4550 continue;
4551
4552 base = its->base;
4553 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
4554 err = its_force_quiescent(base);
4555 if (err) {
4556 pr_err("ITS@%pa: failed to quiesce: %d\n",
4557 &its->phys_base, err);
4558 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
4559 goto err;
4560 }
4561
4562 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
4563 }
4564
4565err:
4566 if (err) {
4567 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
4568 void __iomem *base;
4569
4570 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4571 continue;
4572
4573 base = its->base;
4574 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
4575 }
4576 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004577 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004578
4579 return err;
4580}
4581
4582static void its_restore_enable(void)
4583{
4584 struct its_node *its;
4585 int ret;
4586
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004587 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004588 list_for_each_entry(its, &its_nodes, entry) {
4589 void __iomem *base;
4590 int i;
4591
4592 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4593 continue;
4594
4595 base = its->base;
4596
4597 /*
4598 * Make sure that the ITS is disabled. If it fails to quiesce,
4599 * don't restore it since writing to CBASER or BASER<n>
4600 * registers is undefined according to the GIC v3 ITS
4601 * Specification.
4602 */
4603 ret = its_force_quiescent(base);
4604 if (ret) {
4605 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
4606 &its->phys_base, ret);
4607 continue;
4608 }
4609
4610 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
4611
4612 /*
4613 * Writing CBASER resets CREADR to 0, so make CWRITER and
4614 * cmd_write line up with it.
4615 */
4616 its->cmd_write = its->cmd_base;
4617 gits_write_cwriter(0, base + GITS_CWRITER);
4618
4619 /* Restore GITS_BASER from the value cache. */
4620 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
4621 struct its_baser *baser = &its->tables[i];
4622
4623 if (!(baser->val & GITS_BASER_VALID))
4624 continue;
4625
4626 its_write_baser(its, baser, baser->val);
4627 }
4628 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
Derek Basehore920181c2018-02-28 21:48:20 -08004629
4630 /*
4631 * Reinit the collection if it's stored in the ITS. This is
4632 * indicated by the col_id being less than the HCC field.
4633 * CID < HCC as specified in the GIC v3 Documentation.
4634 */
4635 if (its->collections[smp_processor_id()].col_id <
4636 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
4637 its_cpu_init_collection(its);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004638 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004639 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004640}
4641
4642static struct syscore_ops its_syscore_ops = {
4643 .suspend = its_save_disable,
4644 .resume = its_restore_enable,
4645};
4646
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004647static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004648{
4649 struct irq_domain *inner_domain;
4650 struct msi_domain_info *info;
4651
4652 info = kzalloc(sizeof(*info), GFP_KERNEL);
4653 if (!info)
4654 return -ENOMEM;
4655
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004656 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004657 if (!inner_domain) {
4658 kfree(info);
4659 return -ENOMEM;
4660 }
4661
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004662 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01004663 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004664 inner_domain->flags |= its->msi_domain_flags;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004665 info->ops = &its_msi_domain_ops;
4666 info->data = its;
4667 inner_domain->host_data = info;
4668
4669 return 0;
4670}
4671
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004672static int its_init_vpe_domain(void)
4673{
Marc Zyngier20b3d542016-12-20 15:23:22 +00004674 struct its_node *its;
4675 u32 devid;
4676 int entries;
4677
4678 if (gic_rdists->has_direct_lpi) {
4679 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
4680 return 0;
4681 }
4682
4683 /* Any ITS will do, even if not v4 */
4684 its = list_first_entry(&its_nodes, struct its_node, entry);
4685
4686 entries = roundup_pow_of_two(nr_cpu_ids);
Kees Cook6396bb22018-06-12 14:03:40 -07004687 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
Marc Zyngier20b3d542016-12-20 15:23:22 +00004688 GFP_KERNEL);
4689 if (!vpe_proxy.vpes) {
4690 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
4691 return -ENOMEM;
4692 }
4693
4694 /* Use the last possible DevID */
Marc Zyngier576a8342019-11-08 16:58:00 +00004695 devid = GENMASK(device_ids(its) - 1, 0);
Marc Zyngier20b3d542016-12-20 15:23:22 +00004696 vpe_proxy.dev = its_create_device(its, devid, entries, false);
4697 if (!vpe_proxy.dev) {
4698 kfree(vpe_proxy.vpes);
4699 pr_err("ITS: Can't allocate GICv4 proxy device\n");
4700 return -ENOMEM;
4701 }
4702
Shanker Donthinenic427a472017-09-23 13:50:19 -05004703 BUG_ON(entries > vpe_proxy.dev->nr_ites);
Marc Zyngier20b3d542016-12-20 15:23:22 +00004704
4705 raw_spin_lock_init(&vpe_proxy.lock);
4706 vpe_proxy.next_victim = 0;
4707 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
4708 devid, vpe_proxy.dev->nr_ites);
4709
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004710 return 0;
4711}
4712
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004713static int __init its_compute_its_list_map(struct resource *res,
4714 void __iomem *its_base)
4715{
4716 int its_number;
4717 u32 ctlr;
4718
4719 /*
4720 * This is assumed to be done early enough that we're
4721 * guaranteed to be single-threaded, hence no
4722 * locking. Should this change, we should address
4723 * this.
4724 */
Marc Zyngierab604912017-10-08 18:48:06 +01004725 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
4726 if (its_number >= GICv4_ITS_LIST_MAX) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004727 pr_err("ITS@%pa: No ITSList entry available!\n",
4728 &res->start);
4729 return -EINVAL;
4730 }
4731
4732 ctlr = readl_relaxed(its_base + GITS_CTLR);
4733 ctlr &= ~GITS_CTLR_ITS_NUMBER;
4734 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
4735 writel_relaxed(ctlr, its_base + GITS_CTLR);
4736 ctlr = readl_relaxed(its_base + GITS_CTLR);
4737 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
4738 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
4739 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
4740 }
4741
4742 if (test_and_set_bit(its_number, &its_list_map)) {
4743 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
4744 &res->start, its_number);
4745 return -EINVAL;
4746 }
4747
4748 return its_number;
4749}
4750
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004751static int __init its_probe_one(struct resource *res,
4752 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004753{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004754 struct its_node *its;
4755 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004756 u32 val, ctlr;
4757 u64 baser, tmp, typer;
Shanker Donthineni539d3782019-01-14 09:50:19 +00004758 struct page *page;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004759 int err;
4760
Marc Zyngier5e46a482020-03-04 20:33:14 +00004761 its_base = ioremap(res->start, SZ_64K);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004762 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004763 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004764 return -ENOMEM;
4765 }
4766
4767 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
4768 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004769 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004770 err = -ENODEV;
4771 goto out_unmap;
4772 }
4773
Yun Wu4559fbb2015-03-06 16:37:50 +00004774 err = its_force_quiescent(its_base);
4775 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004776 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00004777 goto out_unmap;
4778 }
4779
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004780 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004781
4782 its = kzalloc(sizeof(*its), GFP_KERNEL);
4783 if (!its) {
4784 err = -ENOMEM;
4785 goto out_unmap;
4786 }
4787
4788 raw_spin_lock_init(&its->lock);
Marc Zyngier9791ec72019-01-29 10:02:33 +00004789 mutex_init(&its->dev_alloc_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004790 INIT_LIST_HEAD(&its->entry);
4791 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004792 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004793 its->typer = typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004794 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004795 its->phys_base = res->start;
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004796 if (is_v4(its)) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004797 if (!(typer & GITS_TYPER_VMOVP)) {
4798 err = its_compute_its_list_map(res, its_base);
4799 if (err < 0)
4800 goto out_free_its;
4801
Marc Zyngierdebf6d02017-10-08 18:44:42 +01004802 its->list_nr = err;
4803
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004804 pr_info("ITS@%pa: Using ITS number %d\n",
4805 &res->start, err);
4806 } else {
4807 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
4808 }
Marc Zyngier5e516842019-12-24 11:10:28 +00004809
4810 if (is_v4_1(its)) {
4811 u32 svpet = FIELD_GET(GITS_TYPER_SVPET, typer);
Marc Zyngier5e46a482020-03-04 20:33:14 +00004812
4813 its->sgir_base = ioremap(res->start + SZ_128K, SZ_64K);
4814 if (!its->sgir_base) {
4815 err = -ENOMEM;
4816 goto out_free_its;
4817 }
4818
Marc Zyngier5e516842019-12-24 11:10:28 +00004819 its->mpidr = readl_relaxed(its_base + GITS_MPIDR);
4820
4821 pr_info("ITS@%pa: Using GICv4.1 mode %08x %08x\n",
4822 &res->start, its->mpidr, svpet);
4823 }
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004824 }
4825
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004826 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004827
Shanker Donthineni539d3782019-01-14 09:50:19 +00004828 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
4829 get_order(ITS_CMD_QUEUE_SZ));
4830 if (!page) {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004831 err = -ENOMEM;
Marc Zyngier5e46a482020-03-04 20:33:14 +00004832 goto out_unmap_sgir;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004833 }
Shanker Donthineni539d3782019-01-14 09:50:19 +00004834 its->cmd_base = (void *)page_address(page);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004835 its->cmd_write = its->cmd_base;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004836 its->fwnode_handle = handle;
4837 its->get_msi_base = its_irq_get_msi_base;
4838 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004839
Robert Richter67510cc2015-09-21 22:58:37 +02004840 its_enable_quirks(its);
4841
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05004842 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004843 if (err)
4844 goto out_free_cmd;
4845
4846 err = its_alloc_collections(its);
4847 if (err)
4848 goto out_free_tables;
4849
4850 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06004851 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004852 GITS_CBASER_InnerShareable |
4853 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
4854 GITS_CBASER_VALID);
4855
Vladimir Murzin0968a612016-11-02 11:54:06 +00004856 gits_write_cbaser(baser, its->base + GITS_CBASER);
4857 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004858
Marc Zyngier4ad3e362015-03-27 14:15:04 +00004859 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00004860 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
4861 /*
4862 * The HW reports non-shareable, we must
4863 * remove the cacheability attributes as
4864 * well.
4865 */
4866 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
4867 GITS_CBASER_CACHEABILITY_MASK);
4868 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00004869 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00004870 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004871 pr_info("ITS: using cache flushing for cmd queue\n");
4872 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
4873 }
4874
Vladimir Murzin0968a612016-11-02 11:54:06 +00004875 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004876 ctlr = readl_relaxed(its->base + GITS_CTLR);
Marc Zyngierd51c4b42017-06-27 21:24:25 +01004877 ctlr |= GITS_CTLR_ENABLE;
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004878 if (is_v4(its))
Marc Zyngierd51c4b42017-06-27 21:24:25 +01004879 ctlr |= GITS_CTLR_ImDe;
4880 writel_relaxed(ctlr, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00004881
Derek Basehoredba0bc72018-02-28 21:48:18 -08004882 if (GITS_TYPER_HCC(typer))
4883 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
4884
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004885 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004886 if (err)
4887 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004888
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004889 raw_spin_lock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004890 list_add(&its->entry, &its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004891 raw_spin_unlock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004892
4893 return 0;
4894
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004895out_free_tables:
4896 its_free_tables(its);
4897out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01004898 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier5e46a482020-03-04 20:33:14 +00004899out_unmap_sgir:
4900 if (its->sgir_base)
4901 iounmap(its->sgir_base);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004902out_free_its:
4903 kfree(its);
4904out_unmap:
4905 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004906 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004907 return err;
4908}
4909
4910static bool gic_rdists_supports_plpis(void)
4911{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01004912 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004913}
4914
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05004915static int redist_disable_lpis(void)
4916{
4917 void __iomem *rbase = gic_data_rdist_rd_base();
4918 u64 timeout = USEC_PER_SEC;
4919 u64 val;
4920
4921 if (!gic_rdists_supports_plpis()) {
4922 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
4923 return -ENXIO;
4924 }
4925
4926 val = readl_relaxed(rbase + GICR_CTLR);
4927 if (!(val & GICR_CTLR_ENABLE_LPIS))
4928 return 0;
4929
Marc Zyngier11e37d32018-07-27 13:38:54 +01004930 /*
4931 * If coming via a CPU hotplug event, we don't need to disable
4932 * LPIs before trying to re-enable them. They are already
4933 * configured and all is well in the world.
Marc Zyngierc440a9d2018-07-27 15:40:13 +01004934 *
4935 * If running with preallocated tables, there is nothing to do.
Marc Zyngier11e37d32018-07-27 13:38:54 +01004936 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01004937 if (gic_data_rdist()->lpi_enabled ||
4938 (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED))
Marc Zyngier11e37d32018-07-27 13:38:54 +01004939 return 0;
4940
4941 /*
4942 * From that point on, we only try to do some damage control.
4943 */
4944 pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05004945 smp_processor_id());
4946 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
4947
4948 /* Disable LPIs */
4949 val &= ~GICR_CTLR_ENABLE_LPIS;
4950 writel_relaxed(val, rbase + GICR_CTLR);
4951
4952 /* Make sure any change to GICR_CTLR is observable by the GIC */
4953 dsb(sy);
4954
4955 /*
4956 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
4957 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
4958 * Error out if we time out waiting for RWP to clear.
4959 */
4960 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
4961 if (!timeout) {
4962 pr_err("CPU%d: Timeout while disabling LPIs\n",
4963 smp_processor_id());
4964 return -ETIMEDOUT;
4965 }
4966 udelay(1);
4967 timeout--;
4968 }
4969
4970 /*
4971 * After it has been written to 1, it is IMPLEMENTATION
4972 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
4973 * cleared to 0. Error out if clearing the bit failed.
4974 */
4975 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
4976 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
4977 return -EBUSY;
4978 }
4979
4980 return 0;
4981}
4982
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004983int its_cpu_init(void)
4984{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004985 if (!list_empty(&its_nodes)) {
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05004986 int ret;
4987
4988 ret = redist_disable_lpis();
4989 if (ret)
4990 return ret;
4991
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004992 its_cpu_init_lpis();
Derek Basehore920181c2018-02-28 21:48:20 -08004993 its_cpu_init_collections();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004994 }
4995
4996 return 0;
4997}
4998
Arvind Yadav935bba72017-06-22 16:05:30 +05304999static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005000 { .compatible = "arm,gic-v3-its", },
5001 {},
5002};
5003
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005004static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005005{
5006 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005007 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005008
5009 for (np = of_find_matching_node(node, its_device_id); np;
5010 np = of_find_matching_node(np, its_device_id)) {
Stephen Boyd95a25622018-02-01 09:03:29 -08005011 if (!of_device_is_available(np))
5012 continue;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02005013 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05005014 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
5015 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02005016 continue;
5017 }
5018
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005019 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05005020 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005021 continue;
5022 }
5023
5024 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005025 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005026 return 0;
5027}
5028
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005029#ifdef CONFIG_ACPI
5030
5031#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
5032
Robert Richterd1ce2632017-07-12 15:25:09 +02005033#ifdef CONFIG_ACPI_NUMA
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305034struct its_srat_map {
5035 /* numa node id */
5036 u32 numa_node;
5037 /* GIC ITS ID */
5038 u32 its_id;
5039};
5040
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005041static struct its_srat_map *its_srat_maps __initdata;
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305042static int its_in_srat __initdata;
5043
5044static int __init acpi_get_its_numa_node(u32 its_id)
5045{
5046 int i;
5047
5048 for (i = 0; i < its_in_srat; i++) {
5049 if (its_id == its_srat_maps[i].its_id)
5050 return its_srat_maps[i].numa_node;
5051 }
5052 return NUMA_NO_NODE;
5053}
5054
Keith Busch60574d12019-03-11 14:55:57 -06005055static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header,
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005056 const unsigned long end)
5057{
5058 return 0;
5059}
5060
Keith Busch60574d12019-03-11 14:55:57 -06005061static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header,
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305062 const unsigned long end)
5063{
5064 int node;
5065 struct acpi_srat_gic_its_affinity *its_affinity;
5066
5067 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
5068 if (!its_affinity)
5069 return -EINVAL;
5070
5071 if (its_affinity->header.length < sizeof(*its_affinity)) {
5072 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
5073 its_affinity->header.length);
5074 return -EINVAL;
5075 }
5076
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305077 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
5078
5079 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
5080 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
5081 return 0;
5082 }
5083
5084 its_srat_maps[its_in_srat].numa_node = node;
5085 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
5086 its_in_srat++;
5087 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
5088 its_affinity->proximity_domain, its_affinity->its_id, node);
5089
5090 return 0;
5091}
5092
5093static void __init acpi_table_parse_srat_its(void)
5094{
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005095 int count;
5096
5097 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
5098 sizeof(struct acpi_table_srat),
5099 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
5100 gic_acpi_match_srat_its, 0);
5101 if (count <= 0)
5102 return;
5103
Kees Cook6da2ec52018-06-12 13:55:00 -07005104 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
5105 GFP_KERNEL);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005106 if (!its_srat_maps) {
5107 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
5108 return;
5109 }
5110
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305111 acpi_table_parse_entries(ACPI_SIG_SRAT,
5112 sizeof(struct acpi_table_srat),
5113 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
5114 gic_acpi_parse_srat_its, 0);
5115}
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005116
5117/* free the its_srat_maps after ITS probing */
5118static void __init acpi_its_srat_maps_free(void)
5119{
5120 kfree(its_srat_maps);
5121}
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305122#else
5123static void __init acpi_table_parse_srat_its(void) { }
5124static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005125static void __init acpi_its_srat_maps_free(void) { }
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305126#endif
5127
Keith Busch60574d12019-03-11 14:55:57 -06005128static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header,
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005129 const unsigned long end)
5130{
5131 struct acpi_madt_generic_translator *its_entry;
5132 struct fwnode_handle *dom_handle;
5133 struct resource res;
5134 int err;
5135
5136 its_entry = (struct acpi_madt_generic_translator *)header;
5137 memset(&res, 0, sizeof(res));
5138 res.start = its_entry->base_address;
5139 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
5140 res.flags = IORESOURCE_MEM;
5141
Marc Zyngier5778cc72019-07-31 16:13:42 +01005142 dom_handle = irq_domain_alloc_fwnode(&res.start);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005143 if (!dom_handle) {
5144 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
5145 &res.start);
5146 return -ENOMEM;
5147 }
5148
Shameer Kolothum8b4282e2018-02-13 15:20:50 +00005149 err = iort_register_domain_token(its_entry->translation_id, res.start,
5150 dom_handle);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005151 if (err) {
5152 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
5153 &res.start, its_entry->translation_id);
5154 goto dom_err;
5155 }
5156
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305157 err = its_probe_one(&res, dom_handle,
5158 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005159 if (!err)
5160 return 0;
5161
5162 iort_deregister_domain_token(its_entry->translation_id);
5163dom_err:
5164 irq_domain_free_fwnode(dom_handle);
5165 return err;
5166}
5167
5168static void __init its_acpi_probe(void)
5169{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305170 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005171 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
5172 gic_acpi_parse_madt_its, 0);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005173 acpi_its_srat_maps_free();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005174}
5175#else
5176static void __init its_acpi_probe(void) { }
5177#endif
5178
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005179int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
5180 struct irq_domain *parent_domain)
5181{
5182 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00005183 struct its_node *its;
5184 bool has_v4 = false;
Marc Zyngier3c407062020-03-04 20:33:13 +00005185 bool has_v4_1 = false;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00005186 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005187
Marc Zyngier5e516842019-12-24 11:10:28 +00005188 gic_rdists = rdists;
5189
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005190 its_parent = parent_domain;
5191 of_node = to_of_node(handle);
5192 if (of_node)
5193 its_of_probe(of_node);
5194 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005195 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005196
5197 if (list_empty(&its_nodes)) {
5198 pr_warn("ITS: No ITS available, not enabling LPIs\n");
5199 return -ENXIO;
5200 }
5201
Marc Zyngier11e37d32018-07-27 13:38:54 +01005202 err = allocate_lpi_tables();
Marc Zyngier8fff27a2016-12-20 13:41:55 +00005203 if (err)
5204 return err;
5205
Marc Zyngier3c407062020-03-04 20:33:13 +00005206 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00005207 has_v4 |= is_v4(its);
Marc Zyngier3c407062020-03-04 20:33:13 +00005208 has_v4_1 |= is_v4_1(its);
5209 }
5210
5211 /* Don't bother with inconsistent systems */
5212 if (WARN_ON(!has_v4_1 && rdists->has_rvpeid))
5213 rdists->has_rvpeid = false;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00005214
5215 if (has_v4 & rdists->has_vlpis) {
Marc Zyngier166cba72020-03-04 20:33:15 +00005216 const struct irq_domain_ops *sgi_ops;
5217
5218 if (has_v4_1)
5219 sgi_ops = &its_sgi_domain_ops;
5220 else
5221 sgi_ops = NULL;
5222
Marc Zyngier3d63cb52016-12-20 15:31:54 +00005223 if (its_init_vpe_domain() ||
Marc Zyngier166cba72020-03-04 20:33:15 +00005224 its_init_v4(parent_domain, &its_vpe_domain_ops, sgi_ops)) {
Marc Zyngier8fff27a2016-12-20 13:41:55 +00005225 rdists->has_vlpis = false;
5226 pr_err("ITS: Disabling GICv4 support\n");
5227 }
5228 }
5229
Derek Basehoredba0bc72018-02-28 21:48:18 -08005230 register_syscore_ops(&its_syscore_ops);
5231
Marc Zyngier8fff27a2016-12-20 13:41:55 +00005232 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005233}