blob: 54d142ccc63a3fab9bc41419b4d89bd3862331d5 [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{
Heyi Guo04d80db2020-02-25 17:00:23 +08001414 while (readl_relaxed(rdbase + GICR_SYNCR) & 1)
Marc Zyngier2f4f0642019-11-08 16:57:56 +00001415 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,
Marc Zyngierd5df9dc2020-03-13 11:01:15 +00002154 u64 cache, u64 shr, u32 order, bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05002155{
2156 u64 val = its_read_baser(its, baser);
2157 u64 esz = GITS_BASER_ENTRY_SIZE(val);
2158 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05002159 u64 baser_phys, tmp;
Marc Zyngierd5df9dc2020-03-13 11:01:15 +00002160 u32 alloc_pages, psz;
Shanker Donthineni539d3782019-01-14 09:50:19 +00002161 struct page *page;
Shanker Donthineni93473592016-06-06 18:17:30 -05002162 void *base;
Shanker Donthineni93473592016-06-06 18:17:30 -05002163
Marc Zyngierd5df9dc2020-03-13 11:01:15 +00002164 psz = baser->psz;
Shanker Donthineni93473592016-06-06 18:17:30 -05002165 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
2166 if (alloc_pages > GITS_BASER_PAGES_MAX) {
2167 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
2168 &its->phys_base, its_base_type_string[type],
2169 alloc_pages, GITS_BASER_PAGES_MAX);
2170 alloc_pages = GITS_BASER_PAGES_MAX;
2171 order = get_order(GITS_BASER_PAGES_MAX * psz);
2172 }
2173
Shanker Donthineni539d3782019-01-14 09:50:19 +00002174 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order);
2175 if (!page)
Shanker Donthineni93473592016-06-06 18:17:30 -05002176 return -ENOMEM;
2177
Shanker Donthineni539d3782019-01-14 09:50:19 +00002178 base = (void *)page_address(page);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05002179 baser_phys = virt_to_phys(base);
2180
2181 /* Check if the physical address of the memory is above 48bits */
2182 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
2183
2184 /* 52bit PA is supported only when PageSize=64K */
2185 if (psz != SZ_64K) {
2186 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
2187 free_pages((unsigned long)base, order);
2188 return -ENXIO;
2189 }
2190
2191 /* Convert 52bit PA to 48bit field */
2192 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
2193 }
2194
Shanker Donthineni93473592016-06-06 18:17:30 -05002195retry_baser:
Shanker Donthineni30ae9612017-10-09 11:46:55 -05002196 val = (baser_phys |
Shanker Donthineni93473592016-06-06 18:17:30 -05002197 (type << GITS_BASER_TYPE_SHIFT) |
2198 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
2199 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
2200 cache |
2201 shr |
2202 GITS_BASER_VALID);
2203
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002204 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
2205
Shanker Donthineni93473592016-06-06 18:17:30 -05002206 switch (psz) {
2207 case SZ_4K:
2208 val |= GITS_BASER_PAGE_SIZE_4K;
2209 break;
2210 case SZ_16K:
2211 val |= GITS_BASER_PAGE_SIZE_16K;
2212 break;
2213 case SZ_64K:
2214 val |= GITS_BASER_PAGE_SIZE_64K;
2215 break;
2216 }
2217
2218 its_write_baser(its, baser, val);
2219 tmp = baser->val;
2220
2221 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
2222 /*
2223 * Shareability didn't stick. Just use
2224 * whatever the read reported, which is likely
2225 * to be the only thing this redistributor
2226 * supports. If that's zero, make it
2227 * non-cacheable as well.
2228 */
2229 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
2230 if (!shr) {
2231 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00002232 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05002233 }
2234 goto retry_baser;
2235 }
2236
Shanker Donthineni93473592016-06-06 18:17:30 -05002237 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00002238 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05002239 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00002240 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05002241 free_pages((unsigned long)base, order);
2242 return -ENXIO;
2243 }
2244
2245 baser->order = order;
2246 baser->base = base;
2247 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002248 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05002249
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002250 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00002251 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05002252 its_base_type_string[type],
2253 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002254 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05002255 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
2256
2257 return 0;
2258}
2259
Marc Zyngier4cacac52016-12-19 18:18:34 +00002260static bool its_parse_indirect_baser(struct its_node *its,
2261 struct its_baser *baser,
Marc Zyngierd5df9dc2020-03-13 11:01:15 +00002262 u32 *order, u32 ids)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002263{
Marc Zyngier4cacac52016-12-19 18:18:34 +00002264 u64 tmp = its_read_baser(its, baser);
2265 u64 type = GITS_BASER_TYPE(tmp);
2266 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002267 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002268 u32 new_order = *order;
Marc Zyngierd5df9dc2020-03-13 11:01:15 +00002269 u32 psz = baser->psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002270 bool indirect = false;
2271
2272 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
2273 if ((esz << ids) > (psz * 2)) {
2274 /*
2275 * Find out whether hw supports a single or two-level table by
2276 * table by reading bit at offset '62' after writing '1' to it.
2277 */
2278 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
2279 indirect = !!(baser->val & GITS_BASER_INDIRECT);
2280
2281 if (indirect) {
2282 /*
2283 * The size of the lvl2 table is equal to ITS page size
2284 * which is 'psz'. For computing lvl1 table size,
2285 * subtract ID bits that sparse lvl2 table from 'ids'
2286 * which is reported by ITS hardware times lvl1 table
2287 * entry size.
2288 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00002289 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002290 esz = GITS_LVL1_ENTRY_SIZE;
2291 }
2292 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002293
2294 /*
2295 * Allocate as many entries as required to fit the
2296 * range of device IDs that the ITS can grok... The ID
2297 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002298 * massive waste of memory if two-level device table
2299 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002300 */
2301 new_order = max_t(u32, get_order(esz << ids), new_order);
2302 if (new_order >= MAX_ORDER) {
2303 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00002304 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier576a8342019-11-08 16:58:00 +00002305 pr_warn("ITS@%pa: %s Table too large, reduce ids %llu->%u\n",
Marc Zyngier4cacac52016-12-19 18:18:34 +00002306 &its->phys_base, its_base_type_string[type],
Marc Zyngier576a8342019-11-08 16:58:00 +00002307 device_ids(its), ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002308 }
2309
2310 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002311
2312 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002313}
2314
Marc Zyngier5e516842019-12-24 11:10:28 +00002315static u32 compute_common_aff(u64 val)
2316{
2317 u32 aff, clpiaff;
2318
2319 aff = FIELD_GET(GICR_TYPER_AFFINITY, val);
2320 clpiaff = FIELD_GET(GICR_TYPER_COMMON_LPI_AFF, val);
2321
2322 return aff & ~(GENMASK(31, 0) >> (clpiaff * 8));
2323}
2324
2325static u32 compute_its_aff(struct its_node *its)
2326{
2327 u64 val;
2328 u32 svpet;
2329
2330 /*
2331 * Reencode the ITS SVPET and MPIDR as a GICR_TYPER, and compute
2332 * the resulting affinity. We then use that to see if this match
2333 * our own affinity.
2334 */
2335 svpet = FIELD_GET(GITS_TYPER_SVPET, its->typer);
2336 val = FIELD_PREP(GICR_TYPER_COMMON_LPI_AFF, svpet);
2337 val |= FIELD_PREP(GICR_TYPER_AFFINITY, its->mpidr);
2338 return compute_common_aff(val);
2339}
2340
2341static struct its_node *find_sibling_its(struct its_node *cur_its)
2342{
2343 struct its_node *its;
2344 u32 aff;
2345
2346 if (!FIELD_GET(GITS_TYPER_SVPET, cur_its->typer))
2347 return NULL;
2348
2349 aff = compute_its_aff(cur_its);
2350
2351 list_for_each_entry(its, &its_nodes, entry) {
2352 u64 baser;
2353
2354 if (!is_v4_1(its) || its == cur_its)
2355 continue;
2356
2357 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2358 continue;
2359
2360 if (aff != compute_its_aff(its))
2361 continue;
2362
2363 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2364 baser = its->tables[2].val;
2365 if (!(baser & GITS_BASER_VALID))
2366 continue;
2367
2368 return its;
2369 }
2370
2371 return NULL;
2372}
2373
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002374static void its_free_tables(struct its_node *its)
2375{
2376 int i;
2377
2378 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06002379 if (its->tables[i].base) {
2380 free_pages((unsigned long)its->tables[i].base,
2381 its->tables[i].order);
2382 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002383 }
2384 }
2385}
2386
Marc Zyngierd5df9dc2020-03-13 11:01:15 +00002387static int its_probe_baser_psz(struct its_node *its, struct its_baser *baser)
2388{
2389 u64 psz = SZ_64K;
2390
2391 while (psz) {
2392 u64 val, gpsz;
2393
2394 val = its_read_baser(its, baser);
2395 val &= ~GITS_BASER_PAGE_SIZE_MASK;
2396
2397 switch (psz) {
2398 case SZ_64K:
2399 gpsz = GITS_BASER_PAGE_SIZE_64K;
2400 break;
2401 case SZ_16K:
2402 gpsz = GITS_BASER_PAGE_SIZE_16K;
2403 break;
2404 case SZ_4K:
2405 default:
2406 gpsz = GITS_BASER_PAGE_SIZE_4K;
2407 break;
2408 }
2409
2410 gpsz >>= GITS_BASER_PAGE_SIZE_SHIFT;
2411
2412 val |= FIELD_PREP(GITS_BASER_PAGE_SIZE_MASK, gpsz);
2413 its_write_baser(its, baser, val);
2414
2415 if (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser->val) == gpsz)
2416 break;
2417
2418 switch (psz) {
2419 case SZ_64K:
2420 psz = SZ_16K;
2421 break;
2422 case SZ_16K:
2423 psz = SZ_4K;
2424 break;
2425 case SZ_4K:
2426 default:
2427 return -1;
2428 }
2429 }
2430
2431 baser->psz = psz;
2432 return 0;
2433}
2434
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05002435static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002436{
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002437 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002438 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05002439 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02002440
Ard Biesheuvelfa150012017-10-17 17:55:54 +01002441 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
2442 /* erratum 24313: ignore memory access type */
2443 cache = GITS_BASER_nCnB;
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002444
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002445 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002446 struct its_baser *baser = its->tables + i;
2447 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002448 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002449 bool indirect = false;
Marc Zyngierd5df9dc2020-03-13 11:01:15 +00002450 u32 order;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002451
Marc Zyngierd5df9dc2020-03-13 11:01:15 +00002452 if (type == GITS_BASER_TYPE_NONE)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002453 continue;
2454
Marc Zyngierd5df9dc2020-03-13 11:01:15 +00002455 if (its_probe_baser_psz(its, baser)) {
2456 its_free_tables(its);
2457 return -ENXIO;
2458 }
2459
2460 order = get_order(baser->psz);
2461
2462 switch (type) {
Marc Zyngier4cacac52016-12-19 18:18:34 +00002463 case GITS_BASER_TYPE_DEVICE:
Marc Zyngierd5df9dc2020-03-13 11:01:15 +00002464 indirect = its_parse_indirect_baser(its, baser, &order,
Marc Zyngier576a8342019-11-08 16:58:00 +00002465 device_ids(its));
Zenghui Yu8d565742019-02-10 05:24:10 +00002466 break;
2467
Marc Zyngier4cacac52016-12-19 18:18:34 +00002468 case GITS_BASER_TYPE_VCPU:
Marc Zyngier5e516842019-12-24 11:10:28 +00002469 if (is_v4_1(its)) {
2470 struct its_node *sibling;
2471
2472 WARN_ON(i != 2);
2473 if ((sibling = find_sibling_its(its))) {
2474 *baser = sibling->tables[2];
2475 its_write_baser(its, baser, baser->val);
2476 continue;
2477 }
2478 }
2479
Marc Zyngierd5df9dc2020-03-13 11:01:15 +00002480 indirect = its_parse_indirect_baser(its, baser, &order,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002481 ITS_MAX_VPEID_BITS);
Marc Zyngier4cacac52016-12-19 18:18:34 +00002482 break;
2483 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00002484
Marc Zyngierd5df9dc2020-03-13 11:01:15 +00002485 err = its_setup_baser(its, baser, cache, shr, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05002486 if (err < 0) {
2487 its_free_tables(its);
2488 return err;
Robert Richter30f21362015-09-21 22:58:34 +02002489 }
2490
Shanker Donthineni93473592016-06-06 18:17:30 -05002491 /* Update settings which will be used for next BASERn */
Shanker Donthineni93473592016-06-06 18:17:30 -05002492 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
2493 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002494 }
2495
2496 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002497}
2498
Marc Zyngier5e516842019-12-24 11:10:28 +00002499static u64 inherit_vpe_l1_table_from_its(void)
2500{
2501 struct its_node *its;
2502 u64 val;
2503 u32 aff;
2504
2505 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2506 aff = compute_common_aff(val);
2507
2508 list_for_each_entry(its, &its_nodes, entry) {
2509 u64 baser, addr;
2510
2511 if (!is_v4_1(its))
2512 continue;
2513
2514 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2515 continue;
2516
2517 if (aff != compute_its_aff(its))
2518 continue;
2519
2520 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2521 baser = its->tables[2].val;
2522 if (!(baser & GITS_BASER_VALID))
2523 continue;
2524
2525 /* We have a winner! */
Zenghui Yu8b718d42020-02-06 15:57:07 +08002526 gic_data_rdist()->vpe_l1_base = its->tables[2].base;
2527
Marc Zyngier5e516842019-12-24 11:10:28 +00002528 val = GICR_VPROPBASER_4_1_VALID;
2529 if (baser & GITS_BASER_INDIRECT)
2530 val |= GICR_VPROPBASER_4_1_INDIRECT;
2531 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE,
2532 FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser));
2533 switch (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser)) {
2534 case GIC_PAGE_SIZE_64K:
2535 addr = GITS_BASER_ADDR_48_to_52(baser);
2536 break;
2537 default:
2538 addr = baser & GENMASK_ULL(47, 12);
2539 break;
2540 }
2541 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, addr >> 12);
2542 val |= FIELD_PREP(GICR_VPROPBASER_SHAREABILITY_MASK,
2543 FIELD_GET(GITS_BASER_SHAREABILITY_MASK, baser));
2544 val |= FIELD_PREP(GICR_VPROPBASER_INNER_CACHEABILITY_MASK,
2545 FIELD_GET(GITS_BASER_INNER_CACHEABILITY_MASK, baser));
2546 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, GITS_BASER_NR_PAGES(baser) - 1);
2547
2548 return val;
2549 }
2550
2551 return 0;
2552}
2553
2554static u64 inherit_vpe_l1_table_from_rd(cpumask_t **mask)
2555{
2556 u32 aff;
2557 u64 val;
2558 int cpu;
2559
2560 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2561 aff = compute_common_aff(val);
2562
2563 for_each_possible_cpu(cpu) {
2564 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
Marc Zyngier5e516842019-12-24 11:10:28 +00002565
2566 if (!base || cpu == smp_processor_id())
2567 continue;
2568
2569 val = gic_read_typer(base + GICR_TYPER);
Zenghui Yu4bccf1d2020-02-06 15:57:09 +08002570 if (aff != compute_common_aff(val))
Marc Zyngier5e516842019-12-24 11:10:28 +00002571 continue;
2572
2573 /*
2574 * At this point, we have a victim. This particular CPU
2575 * has already booted, and has an affinity that matches
2576 * ours wrt CommonLPIAff. Let's use its own VPROPBASER.
2577 * Make sure we don't write the Z bit in that case.
2578 */
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002579 val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
Marc Zyngier5e516842019-12-24 11:10:28 +00002580 val &= ~GICR_VPROPBASER_4_1_Z;
2581
Zenghui Yu8b718d42020-02-06 15:57:07 +08002582 gic_data_rdist()->vpe_l1_base = gic_data_rdist_cpu(cpu)->vpe_l1_base;
Marc Zyngier5e516842019-12-24 11:10:28 +00002583 *mask = gic_data_rdist_cpu(cpu)->vpe_table_mask;
2584
2585 return val;
2586 }
2587
2588 return 0;
2589}
2590
Zenghui Yu4e6437f2020-02-06 15:57:08 +08002591static bool allocate_vpe_l2_table(int cpu, u32 id)
2592{
2593 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
Marc Zyngier490d3322020-02-09 22:48:50 +00002594 unsigned int psz, esz, idx, npg, gpsz;
2595 u64 val;
Zenghui Yu4e6437f2020-02-06 15:57:08 +08002596 struct page *page;
2597 __le64 *table;
2598
2599 if (!gic_rdists->has_rvpeid)
2600 return true;
2601
Marc Zyngier28d160d2020-03-04 20:33:09 +00002602 /* Skip non-present CPUs */
2603 if (!base)
2604 return true;
2605
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002606 val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
Zenghui Yu4e6437f2020-02-06 15:57:08 +08002607
2608 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val) + 1;
2609 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2610 npg = FIELD_GET(GICR_VPROPBASER_4_1_SIZE, val) + 1;
2611
2612 switch (gpsz) {
2613 default:
2614 WARN_ON(1);
2615 /* fall through */
2616 case GIC_PAGE_SIZE_4K:
2617 psz = SZ_4K;
2618 break;
2619 case GIC_PAGE_SIZE_16K:
2620 psz = SZ_16K;
2621 break;
2622 case GIC_PAGE_SIZE_64K:
2623 psz = SZ_64K;
2624 break;
2625 }
2626
2627 /* Don't allow vpe_id that exceeds single, flat table limit */
2628 if (!(val & GICR_VPROPBASER_4_1_INDIRECT))
2629 return (id < (npg * psz / (esz * SZ_8)));
2630
2631 /* Compute 1st level table index & check if that exceeds table limit */
2632 idx = id >> ilog2(psz / (esz * SZ_8));
2633 if (idx >= (npg * psz / GITS_LVL1_ENTRY_SIZE))
2634 return false;
2635
2636 table = gic_data_rdist_cpu(cpu)->vpe_l1_base;
2637
2638 /* Allocate memory for 2nd level table */
2639 if (!table[idx]) {
2640 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(psz));
2641 if (!page)
2642 return false;
2643
2644 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2645 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2646 gic_flush_dcache_to_poc(page_address(page), psz);
2647
2648 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2649
2650 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2651 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2652 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
2653
2654 /* Ensure updated table contents are visible to RD hardware */
2655 dsb(sy);
2656 }
2657
2658 return true;
2659}
2660
Marc Zyngier5e516842019-12-24 11:10:28 +00002661static int allocate_vpe_l1_table(void)
2662{
2663 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2664 u64 val, gpsz, npg, pa;
2665 unsigned int psz = SZ_64K;
2666 unsigned int np, epp, esz;
2667 struct page *page;
2668
2669 if (!gic_rdists->has_rvpeid)
2670 return 0;
2671
2672 /*
2673 * if VPENDBASER.Valid is set, disable any previously programmed
2674 * VPE by setting PendingLast while clearing Valid. This has the
2675 * effect of making sure no doorbell will be generated and we can
2676 * then safely clear VPROPBASER.Valid.
2677 */
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002678 if (gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER) & GICR_VPENDBASER_Valid)
2679 gicr_write_vpendbaser(GICR_VPENDBASER_PendingLast,
Marc Zyngier5e516842019-12-24 11:10:28 +00002680 vlpi_base + GICR_VPENDBASER);
2681
2682 /*
2683 * If we can inherit the configuration from another RD, let's do
2684 * so. Otherwise, we have to go through the allocation process. We
2685 * assume that all RDs have the exact same requirements, as
2686 * nothing will work otherwise.
2687 */
2688 val = inherit_vpe_l1_table_from_rd(&gic_data_rdist()->vpe_table_mask);
2689 if (val & GICR_VPROPBASER_4_1_VALID)
2690 goto out;
2691
2692 gic_data_rdist()->vpe_table_mask = kzalloc(sizeof(cpumask_t), GFP_KERNEL);
2693 if (!gic_data_rdist()->vpe_table_mask)
2694 return -ENOMEM;
2695
2696 val = inherit_vpe_l1_table_from_its();
2697 if (val & GICR_VPROPBASER_4_1_VALID)
2698 goto out;
2699
2700 /* First probe the page size */
2701 val = FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, GIC_PAGE_SIZE_64K);
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002702 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2703 val = gicr_read_vpropbaser(vlpi_base + GICR_VPROPBASER);
Marc Zyngier5e516842019-12-24 11:10:28 +00002704 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2705 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val);
2706
2707 switch (gpsz) {
2708 default:
2709 gpsz = GIC_PAGE_SIZE_4K;
2710 /* fall through */
2711 case GIC_PAGE_SIZE_4K:
2712 psz = SZ_4K;
2713 break;
2714 case GIC_PAGE_SIZE_16K:
2715 psz = SZ_16K;
2716 break;
2717 case GIC_PAGE_SIZE_64K:
2718 psz = SZ_64K;
2719 break;
2720 }
2721
2722 /*
2723 * Start populating the register from scratch, including RO fields
2724 * (which we want to print in debug cases...)
2725 */
2726 val = 0;
2727 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, gpsz);
2728 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ENTRY_SIZE, esz);
2729
2730 /* How many entries per GIC page? */
2731 esz++;
2732 epp = psz / (esz * SZ_8);
2733
2734 /*
2735 * If we need more than just a single L1 page, flag the table
2736 * as indirect and compute the number of required L1 pages.
2737 */
2738 if (epp < ITS_MAX_VPEID) {
2739 int nl2;
2740
2741 val |= GICR_VPROPBASER_4_1_INDIRECT;
2742
2743 /* Number of L2 pages required to cover the VPEID space */
2744 nl2 = DIV_ROUND_UP(ITS_MAX_VPEID, epp);
2745
2746 /* Number of L1 pages to point to the L2 pages */
2747 npg = DIV_ROUND_UP(nl2 * SZ_8, psz);
2748 } else {
2749 npg = 1;
2750 }
2751
Zenghui Yue88bd312020-02-06 15:57:06 +08002752 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, npg - 1);
Marc Zyngier5e516842019-12-24 11:10:28 +00002753
2754 /* Right, that's the number of CPU pages we need for L1 */
2755 np = DIV_ROUND_UP(npg * psz, PAGE_SIZE);
2756
2757 pr_debug("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n",
2758 np, npg, psz, epp, esz);
2759 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(np * PAGE_SIZE));
2760 if (!page)
2761 return -ENOMEM;
2762
Zenghui Yu8b718d42020-02-06 15:57:07 +08002763 gic_data_rdist()->vpe_l1_base = page_address(page);
Marc Zyngier5e516842019-12-24 11:10:28 +00002764 pa = virt_to_phys(page_address(page));
2765 WARN_ON(!IS_ALIGNED(pa, psz));
2766
2767 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, pa >> 12);
2768 val |= GICR_VPROPBASER_RaWb;
2769 val |= GICR_VPROPBASER_InnerShareable;
2770 val |= GICR_VPROPBASER_4_1_Z;
2771 val |= GICR_VPROPBASER_4_1_VALID;
2772
2773out:
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002774 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
Marc Zyngier5e516842019-12-24 11:10:28 +00002775 cpumask_set_cpu(smp_processor_id(), gic_data_rdist()->vpe_table_mask);
2776
2777 pr_debug("CPU%d: VPROPBASER = %llx %*pbl\n",
2778 smp_processor_id(), val,
2779 cpumask_pr_args(gic_data_rdist()->vpe_table_mask));
2780
2781 return 0;
2782}
2783
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002784static int its_alloc_collections(struct its_node *its)
2785{
Marc Zyngier83559b42018-06-22 10:52:52 +01002786 int i;
2787
Kees Cook6396bb22018-06-12 14:03:40 -07002788 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002789 GFP_KERNEL);
2790 if (!its->collections)
2791 return -ENOMEM;
2792
Marc Zyngier83559b42018-06-22 10:52:52 +01002793 for (i = 0; i < nr_cpu_ids; i++)
2794 its->collections[i].target_address = ~0ULL;
2795
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002796 return 0;
2797}
2798
Marc Zyngier7c297a22016-12-19 18:34:38 +00002799static struct page *its_allocate_pending_table(gfp_t gfp_flags)
2800{
2801 struct page *pend_page;
Marc Zyngieradaab502018-07-17 18:06:39 +01002802
Marc Zyngier7c297a22016-12-19 18:34:38 +00002803 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
Marc Zyngieradaab502018-07-17 18:06:39 +01002804 get_order(LPI_PENDBASE_SZ));
Marc Zyngier7c297a22016-12-19 18:34:38 +00002805 if (!pend_page)
2806 return NULL;
2807
2808 /* Make sure the GIC will observe the zero-ed page */
2809 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
2810
2811 return pend_page;
2812}
2813
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002814static void its_free_pending_table(struct page *pt)
2815{
Marc Zyngieradaab502018-07-17 18:06:39 +01002816 free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ));
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002817}
2818
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002819/*
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002820 * Booting with kdump and LPIs enabled is generally fine. Any other
2821 * case is wrong in the absence of firmware/EFI support.
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002822 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002823static bool enabled_lpis_allowed(void)
2824{
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002825 phys_addr_t addr;
2826 u64 val;
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002827
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002828 /* Check whether the property table is in a reserved region */
2829 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2830 addr = val & GENMASK_ULL(51, 12);
2831
2832 return gic_check_reserved_range(addr, LPI_PROPBASE_SZ);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002833}
2834
Marc Zyngier11e37d32018-07-27 13:38:54 +01002835static int __init allocate_lpi_tables(void)
2836{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002837 u64 val;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002838 int err, cpu;
2839
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002840 /*
2841 * If LPIs are enabled while we run this from the boot CPU,
2842 * flag the RD tables as pre-allocated if the stars do align.
2843 */
2844 val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR);
2845 if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) {
2846 gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED |
2847 RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING);
2848 pr_info("GICv3: Using preallocated redistributor tables\n");
2849 }
2850
Marc Zyngier11e37d32018-07-27 13:38:54 +01002851 err = its_setup_lpi_prop_table();
2852 if (err)
2853 return err;
2854
2855 /*
2856 * We allocate all the pending tables anyway, as we may have a
2857 * mix of RDs that have had LPIs enabled, and some that
2858 * don't. We'll free the unused ones as each CPU comes online.
2859 */
2860 for_each_possible_cpu(cpu) {
2861 struct page *pend_page;
2862
2863 pend_page = its_allocate_pending_table(GFP_NOWAIT);
2864 if (!pend_page) {
2865 pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu);
2866 return -ENOMEM;
2867 }
2868
2869 gic_data_rdist_cpu(cpu)->pend_page = pend_page;
2870 }
2871
2872 return 0;
2873}
2874
Marc Zyngiere64fab12019-12-24 11:10:35 +00002875static u64 its_clear_vpend_valid(void __iomem *vlpi_base, u64 clr, u64 set)
Heyi Guo64794502019-01-24 21:37:08 +08002876{
2877 u32 count = 1000000; /* 1s! */
2878 bool clean;
2879 u64 val;
2880
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002881 val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002882 val &= ~GICR_VPENDBASER_Valid;
Marc Zyngiere64fab12019-12-24 11:10:35 +00002883 val &= ~clr;
2884 val |= set;
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002885 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002886
2887 do {
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002888 val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002889 clean = !(val & GICR_VPENDBASER_Dirty);
2890 if (!clean) {
2891 count--;
2892 cpu_relax();
2893 udelay(1);
2894 }
2895 } while (!clean && count);
2896
Marc Zyngiere64fab12019-12-24 11:10:35 +00002897 if (unlikely(val & GICR_VPENDBASER_Dirty)) {
2898 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2899 val |= GICR_VPENDBASER_PendingLast;
2900 }
2901
Heyi Guo64794502019-01-24 21:37:08 +08002902 return val;
2903}
2904
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002905static void its_cpu_init_lpis(void)
2906{
2907 void __iomem *rbase = gic_data_rdist_rd_base();
2908 struct page *pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002909 phys_addr_t paddr;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002910 u64 val, tmp;
2911
Marc Zyngier11e37d32018-07-27 13:38:54 +01002912 if (gic_data_rdist()->lpi_enabled)
2913 return;
2914
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002915 val = readl_relaxed(rbase + GICR_CTLR);
2916 if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) &&
2917 (val & GICR_CTLR_ENABLE_LPIS)) {
Marc Zyngierf842ca82018-07-27 16:03:31 +01002918 /*
2919 * Check that we get the same property table on all
2920 * RDs. If we don't, this is hopeless.
2921 */
2922 paddr = gicr_read_propbaser(rbase + GICR_PROPBASER);
2923 paddr &= GENMASK_ULL(51, 12);
2924 if (WARN_ON(gic_rdists->prop_table_pa != paddr))
2925 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
2926
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002927 paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER);
2928 paddr &= GENMASK_ULL(51, 16);
2929
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002930 WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002931 its_free_pending_table(gic_data_rdist()->pend_page);
2932 gic_data_rdist()->pend_page = NULL;
2933
2934 goto out;
2935 }
2936
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002937 pend_page = gic_data_rdist()->pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002938 paddr = page_to_phys(pend_page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01002939 WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002940
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002941 /* set PROPBASE */
Marc Zyngiere1a2e202018-07-27 14:36:00 +01002942 val = (gic_rdists->prop_table_pa |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002943 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002944 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002945 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
2946
Vladimir Murzin0968a612016-11-02 11:54:06 +00002947 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2948 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002949
2950 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002951 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
2952 /*
2953 * The HW reports non-shareable, we must
2954 * remove the cacheability attributes as
2955 * well.
2956 */
2957 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
2958 GICR_PROPBASER_CACHEABILITY_MASK);
2959 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002960 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002961 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002962 pr_info_once("GIC: using cache flushing for LPI property table\n");
2963 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
2964 }
2965
2966 /* set PENDBASE */
2967 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002968 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002969 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002970
Vladimir Murzin0968a612016-11-02 11:54:06 +00002971 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2972 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002973
2974 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
2975 /*
2976 * The HW reports non-shareable, we must remove the
2977 * cacheability attributes as well.
2978 */
2979 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
2980 GICR_PENDBASER_CACHEABILITY_MASK);
2981 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002982 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002983 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002984
2985 /* Enable LPIs */
2986 val = readl_relaxed(rbase + GICR_CTLR);
2987 val |= GICR_CTLR_ENABLE_LPIS;
2988 writel_relaxed(val, rbase + GICR_CTLR);
2989
Marc Zyngier5e516842019-12-24 11:10:28 +00002990 if (gic_rdists->has_vlpis && !gic_rdists->has_rvpeid) {
Heyi Guo64794502019-01-24 21:37:08 +08002991 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2992
2993 /*
2994 * It's possible for CPU to receive VLPIs before it is
2995 * sheduled as a vPE, especially for the first CPU, and the
2996 * VLPI with INTID larger than 2^(IDbits+1) will be considered
2997 * as out of range and dropped by GIC.
2998 * So we initialize IDbits to known value to avoid VLPI drop.
2999 */
3000 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
3001 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
3002 smp_processor_id(), val);
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003003 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
Heyi Guo64794502019-01-24 21:37:08 +08003004
3005 /*
3006 * Also clear Valid bit of GICR_VPENDBASER, in case some
3007 * ancient programming gets left in and has possibility of
3008 * corrupting memory.
3009 */
Marc Zyngiere64fab12019-12-24 11:10:35 +00003010 val = its_clear_vpend_valid(vlpi_base, 0, 0);
Heyi Guo64794502019-01-24 21:37:08 +08003011 }
3012
Marc Zyngier5e516842019-12-24 11:10:28 +00003013 if (allocate_vpe_l1_table()) {
3014 /*
3015 * If the allocation has failed, we're in massive trouble.
3016 * Disable direct injection, and pray that no VM was
3017 * already running...
3018 */
3019 gic_rdists->has_rvpeid = false;
3020 gic_rdists->has_vlpis = false;
3021 }
3022
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003023 /* Make sure the GIC has seen the above */
3024 dsb(sy);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01003025out:
Marc Zyngier11e37d32018-07-27 13:38:54 +01003026 gic_data_rdist()->lpi_enabled = true;
Marc Zyngierc440a9d2018-07-27 15:40:13 +01003027 pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n",
Marc Zyngier11e37d32018-07-27 13:38:54 +01003028 smp_processor_id(),
Marc Zyngierc440a9d2018-07-27 15:40:13 +01003029 gic_data_rdist()->pend_page ? "allocated" : "reserved",
Marc Zyngier11e37d32018-07-27 13:38:54 +01003030 &paddr);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003031}
3032
Derek Basehore920181c2018-02-28 21:48:20 -08003033static void its_cpu_init_collection(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003034{
Derek Basehore920181c2018-02-28 21:48:20 -08003035 int cpu = smp_processor_id();
3036 u64 target;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003037
Derek Basehore920181c2018-02-28 21:48:20 -08003038 /* avoid cross node collections and its mapping */
3039 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
3040 struct device_node *cpu_node;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003041
Derek Basehore920181c2018-02-28 21:48:20 -08003042 cpu_node = of_get_cpu_node(cpu, NULL);
3043 if (its->numa_node != NUMA_NO_NODE &&
3044 its->numa_node != of_node_to_nid(cpu_node))
3045 return;
3046 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003047
Derek Basehore920181c2018-02-28 21:48:20 -08003048 /*
3049 * We now have to bind each collection to its target
3050 * redistributor.
3051 */
3052 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003053 /*
Derek Basehore920181c2018-02-28 21:48:20 -08003054 * This ITS wants the physical address of the
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003055 * redistributor.
3056 */
Derek Basehore920181c2018-02-28 21:48:20 -08003057 target = gic_data_rdist()->phys_base;
3058 } else {
3059 /* This ITS wants a linear CPU number. */
3060 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
3061 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003062 }
3063
Derek Basehore920181c2018-02-28 21:48:20 -08003064 /* Perform collection mapping */
3065 its->collections[cpu].target_address = target;
3066 its->collections[cpu].col_id = cpu;
3067
3068 its_send_mapc(its, &its->collections[cpu], 1);
3069 its_send_invall(its, &its->collections[cpu]);
3070}
3071
3072static void its_cpu_init_collections(void)
3073{
3074 struct its_node *its;
3075
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003076 raw_spin_lock(&its_lock);
Derek Basehore920181c2018-02-28 21:48:20 -08003077
3078 list_for_each_entry(its, &its_nodes, entry)
3079 its_cpu_init_collection(its);
3080
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02003081 raw_spin_unlock(&its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00003082}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003083
3084static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
3085{
3086 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003087 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003088
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003089 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003090
3091 list_for_each_entry(tmp, &its->its_device_list, entry) {
3092 if (tmp->device_id == dev_id) {
3093 its_dev = tmp;
3094 break;
3095 }
3096 }
3097
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003098 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003099
3100 return its_dev;
3101}
3102
Shanker Donthineni466b7d12016-03-09 22:10:49 -06003103static struct its_baser *its_get_baser(struct its_node *its, u32 type)
3104{
3105 int i;
3106
3107 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
3108 if (GITS_BASER_TYPE(its->tables[i].val) == type)
3109 return &its->tables[i];
3110 }
3111
3112 return NULL;
3113}
3114
Shanker Donthineni539d3782019-01-14 09:50:19 +00003115static bool its_alloc_table_entry(struct its_node *its,
3116 struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003117{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003118 struct page *page;
3119 u32 esz, idx;
3120 __le64 *table;
3121
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003122 /* Don't allow device id that exceeds single, flat table limit */
3123 esz = GITS_BASER_ENTRY_SIZE(baser->val);
3124 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003125 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003126
3127 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003128 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003129 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
3130 return false;
3131
3132 table = baser->base;
3133
3134 /* Allocate memory for 2nd level table */
3135 if (!table[idx]) {
Shanker Donthineni539d3782019-01-14 09:50:19 +00003136 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
3137 get_order(baser->psz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003138 if (!page)
3139 return false;
3140
3141 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
3142 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00003143 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003144
3145 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
3146
3147 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
3148 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00003149 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003150
3151 /* Ensure updated table contents are visible to ITS hardware */
3152 dsb(sy);
3153 }
3154
3155 return true;
3156}
3157
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003158static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
3159{
3160 struct its_baser *baser;
3161
3162 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
3163
3164 /* Don't allow device id that exceeds ITS hardware limit */
3165 if (!baser)
Marc Zyngier576a8342019-11-08 16:58:00 +00003166 return (ilog2(dev_id) < device_ids(its));
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003167
Shanker Donthineni539d3782019-01-14 09:50:19 +00003168 return its_alloc_table_entry(its, baser, dev_id);
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003169}
3170
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003171static bool its_alloc_vpe_table(u32 vpe_id)
3172{
3173 struct its_node *its;
Zenghui Yu4e6437f2020-02-06 15:57:08 +08003174 int cpu;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003175
3176 /*
3177 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
3178 * could try and only do it on ITSs corresponding to devices
3179 * that have interrupts targeted at this VPE, but the
3180 * complexity becomes crazy (and you have tons of memory
3181 * anyway, right?).
3182 */
3183 list_for_each_entry(its, &its_nodes, entry) {
3184 struct its_baser *baser;
3185
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00003186 if (!is_v4(its))
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003187 continue;
3188
3189 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
3190 if (!baser)
3191 return false;
3192
Shanker Donthineni539d3782019-01-14 09:50:19 +00003193 if (!its_alloc_table_entry(its, baser, vpe_id))
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003194 return false;
3195 }
3196
Zenghui Yu4e6437f2020-02-06 15:57:08 +08003197 /* Non v4.1? No need to iterate RDs and go back early. */
3198 if (!gic_rdists->has_rvpeid)
3199 return true;
3200
3201 /*
3202 * Make sure the L2 tables are allocated for all copies of
3203 * the L1 table on *all* v4.1 RDs.
3204 */
3205 for_each_possible_cpu(cpu) {
3206 if (!allocate_vpe_l2_table(cpu, vpe_id))
3207 return false;
3208 }
3209
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003210 return true;
3211}
3212
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003213static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003214 int nvecs, bool alloc_lpis)
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003215{
3216 struct its_device *dev;
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003217 unsigned long *lpi_map = NULL;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003218 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01003219 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003220 void *itt;
3221 int lpi_base;
3222 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00003223 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003224 int sz;
3225
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003226 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06003227 return NULL;
3228
Marc Zyngier147c8f32018-05-27 16:39:55 +01003229 if (WARN_ON(!is_power_of_2(nvecs)))
3230 nvecs = roundup_pow_of_two(nvecs);
3231
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003232 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00003233 /*
Marc Zyngier147c8f32018-05-27 16:39:55 +01003234 * Even if the device wants a single LPI, the ITT must be
3235 * sized as a power of two (and you need at least one bit...).
Marc Zyngierc8481262014-12-12 10:51:24 +00003236 */
Marc Zyngier147c8f32018-05-27 16:39:55 +01003237 nr_ites = max(2, nvecs);
Marc Zyngierffedbf02019-11-08 16:57:59 +00003238 sz = nr_ites * (FIELD_GET(GITS_TYPER_ITT_ENTRY_SIZE, its->typer) + 1);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003239 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Shanker Donthineni539d3782019-01-14 09:50:19 +00003240 itt = kzalloc_node(sz, GFP_KERNEL, its->numa_node);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003241 if (alloc_lpis) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003242 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003243 if (lpi_map)
Kees Cook6396bb22018-06-12 14:03:40 -07003244 col_map = kcalloc(nr_lpis, sizeof(*col_map),
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003245 GFP_KERNEL);
3246 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07003247 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003248 nr_lpis = 0;
3249 lpi_base = 0;
3250 }
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003251
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003252 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003253 kfree(dev);
3254 kfree(itt);
3255 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01003256 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003257 return NULL;
3258 }
3259
Vladimir Murzin328191c2016-11-02 11:54:05 +00003260 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01003261
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003262 dev->its = its;
3263 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00003264 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01003265 dev->event_map.lpi_map = lpi_map;
3266 dev->event_map.col_map = col_map;
3267 dev->event_map.lpi_base = lpi_base;
3268 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngier11635fa2019-11-08 16:58:05 +00003269 raw_spin_lock_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003270 dev->device_id = dev_id;
3271 INIT_LIST_HEAD(&dev->entry);
3272
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003273 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003274 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003275 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003276
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003277 /* Map device to its ITT */
3278 its_send_mapd(dev, 1);
3279
3280 return dev;
3281}
3282
3283static void its_free_device(struct its_device *its_dev)
3284{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003285 unsigned long flags;
3286
3287 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003288 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003289 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier898aa5c2019-11-08 16:57:55 +00003290 kfree(its_dev->event_map.col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003291 kfree(its_dev->itt);
3292 kfree(its_dev);
3293}
Marc Zyngierb48ac832014-11-24 14:35:16 +00003294
Marc Zyngier8208d172019-01-18 14:08:59 +00003295static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
Marc Zyngierb48ac832014-11-24 14:35:16 +00003296{
3297 int idx;
3298
Zenghui Yu342be102019-07-27 06:14:22 +00003299 /* Find a free LPI region in lpi_map and allocate them. */
Marc Zyngier8208d172019-01-18 14:08:59 +00003300 idx = bitmap_find_free_region(dev->event_map.lpi_map,
3301 dev->event_map.nr_lpis,
3302 get_count_order(nvecs));
3303 if (idx < 0)
Marc Zyngierb48ac832014-11-24 14:35:16 +00003304 return -ENOSPC;
3305
Marc Zyngier591e5be2015-07-17 10:46:42 +01003306 *hwirq = dev->event_map.lpi_base + idx;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003307
Marc Zyngierb48ac832014-11-24 14:35:16 +00003308 return 0;
3309}
3310
Marc Zyngier54456db2015-07-28 14:46:21 +01003311static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
3312 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00003313{
Marc Zyngierb48ac832014-11-24 14:35:16 +00003314 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003315 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01003316 struct msi_domain_info *msi_info;
3317 u32 dev_id;
Marc Zyngier9791ec72019-01-29 10:02:33 +00003318 int err = 0;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003319
Marc Zyngier54456db2015-07-28 14:46:21 +01003320 /*
Julien Gralla7c90f52019-04-18 16:58:14 +01003321 * We ignore "dev" entirely, and rely on the dev_id that has
Marc Zyngier54456db2015-07-28 14:46:21 +01003322 * been passed via the scratchpad. This limits this domain's
3323 * usefulness to upper layers that definitely know that they
3324 * are built on top of the ITS.
3325 */
3326 dev_id = info->scratchpad[0].ul;
3327
3328 msi_info = msi_get_domain_info(domain);
3329 its = msi_info->data;
3330
Marc Zyngier20b3d542016-12-20 15:23:22 +00003331 if (!gic_rdists->has_direct_lpi &&
3332 vpe_proxy.dev &&
3333 vpe_proxy.dev->its == its &&
3334 dev_id == vpe_proxy.dev->device_id) {
3335 /* Bad luck. Get yourself a better implementation */
3336 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
3337 dev_id);
3338 return -EINVAL;
3339 }
3340
Marc Zyngier9791ec72019-01-29 10:02:33 +00003341 mutex_lock(&its->dev_alloc_lock);
Marc Zyngierf1304202015-07-28 14:46:18 +01003342 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00003343 if (its_dev) {
3344 /*
3345 * We already have seen this ID, probably through
3346 * another alias (PCI bridge of some sort). No need to
3347 * create the device.
3348 */
Marc Zyngier9791ec72019-01-29 10:02:33 +00003349 its_dev->shared = true;
Marc Zyngierf1304202015-07-28 14:46:18 +01003350 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00003351 goto out;
3352 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00003353
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003354 its_dev = its_create_device(its, dev_id, nvec, true);
Marc Zyngier9791ec72019-01-29 10:02:33 +00003355 if (!its_dev) {
3356 err = -ENOMEM;
3357 goto out;
3358 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00003359
Marc Zyngierf1304202015-07-28 14:46:18 +01003360 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00003361out:
Marc Zyngier9791ec72019-01-29 10:02:33 +00003362 mutex_unlock(&its->dev_alloc_lock);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003363 info->scratchpad[0].ptr = its_dev;
Marc Zyngier9791ec72019-01-29 10:02:33 +00003364 return err;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003365}
3366
Marc Zyngier54456db2015-07-28 14:46:21 +01003367static struct msi_domain_ops its_msi_domain_ops = {
3368 .msi_prepare = its_msi_prepare,
3369};
3370
Marc Zyngierb48ac832014-11-24 14:35:16 +00003371static int its_irq_gic_domain_alloc(struct irq_domain *domain,
3372 unsigned int virq,
3373 irq_hw_number_t hwirq)
3374{
Marc Zyngierf833f572015-10-13 12:51:33 +01003375 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003376
Marc Zyngierf833f572015-10-13 12:51:33 +01003377 if (irq_domain_get_of_node(domain->parent)) {
3378 fwspec.fwnode = domain->parent->fwnode;
3379 fwspec.param_count = 3;
3380 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
3381 fwspec.param[1] = hwirq;
3382 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003383 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
3384 fwspec.fwnode = domain->parent->fwnode;
3385 fwspec.param_count = 2;
3386 fwspec.param[0] = hwirq;
3387 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01003388 } else {
3389 return -EINVAL;
3390 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00003391
Marc Zyngierf833f572015-10-13 12:51:33 +01003392 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003393}
3394
3395static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
3396 unsigned int nr_irqs, void *args)
3397{
3398 msi_alloc_info_t *info = args;
3399 struct its_device *its_dev = info->scratchpad[0].ptr;
Julien Grall35ae7df2019-05-01 14:58:21 +01003400 struct its_node *its = its_dev->its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003401 irq_hw_number_t hwirq;
3402 int err;
3403 int i;
3404
Marc Zyngier8208d172019-01-18 14:08:59 +00003405 err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
3406 if (err)
3407 return err;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003408
Julien Grall35ae7df2019-05-01 14:58:21 +01003409 err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev));
3410 if (err)
3411 return err;
3412
Marc Zyngier8208d172019-01-18 14:08:59 +00003413 for (i = 0; i < nr_irqs; i++) {
3414 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003415 if (err)
3416 return err;
3417
3418 irq_domain_set_hwirq_and_chip(domain, virq + i,
Marc Zyngier8208d172019-01-18 14:08:59 +00003419 hwirq + i, &its_irq_chip, its_dev);
Marc Zyngier0d224d32017-08-18 09:39:18 +01003420 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
Marc Zyngierf1304202015-07-28 14:46:18 +01003421 pr_debug("ID:%d pID:%d vID:%d\n",
Marc Zyngier8208d172019-01-18 14:08:59 +00003422 (int)(hwirq + i - its_dev->event_map.lpi_base),
3423 (int)(hwirq + i), virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003424 }
3425
3426 return 0;
3427}
3428
Thomas Gleixner72491642017-09-13 23:29:10 +02003429static int its_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01003430 struct irq_data *d, bool reserve)
Marc Zyngieraca268d2014-12-12 10:51:23 +00003431{
3432 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3433 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003434 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngier0d224d32017-08-18 09:39:18 +01003435 int cpu;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003436
3437 /* get the cpu_mask of local node */
3438 if (its_dev->its->numa_node >= 0)
3439 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00003440
Marc Zyngier591e5be2015-07-17 10:46:42 +01003441 /* Bind the LPI to the first possible CPU */
Yang Yingliangc1797b12018-06-22 10:52:51 +01003442 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
3443 if (cpu >= nr_cpu_ids) {
3444 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
3445 return -EINVAL;
3446
3447 cpu = cpumask_first(cpu_online_mask);
3448 }
3449
Marc Zyngier0d224d32017-08-18 09:39:18 +01003450 its_dev->event_map.col_map[event] = cpu;
3451 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngier591e5be2015-07-17 10:46:42 +01003452
Marc Zyngieraca268d2014-12-12 10:51:23 +00003453 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00003454 its_send_mapti(its_dev, d->hwirq, event);
Thomas Gleixner72491642017-09-13 23:29:10 +02003455 return 0;
Marc Zyngieraca268d2014-12-12 10:51:23 +00003456}
3457
3458static void its_irq_domain_deactivate(struct irq_domain *domain,
3459 struct irq_data *d)
3460{
3461 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3462 u32 event = its_get_event_id(d);
3463
3464 /* Stop the delivery of interrupts */
3465 its_send_discard(its_dev, event);
3466}
3467
Marc Zyngierb48ac832014-11-24 14:35:16 +00003468static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
3469 unsigned int nr_irqs)
3470{
3471 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
3472 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier9791ec72019-01-29 10:02:33 +00003473 struct its_node *its = its_dev->its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003474 int i;
3475
Marc Zyngierc9c96e32019-09-05 14:56:47 +01003476 bitmap_release_region(its_dev->event_map.lpi_map,
3477 its_get_event_id(irq_domain_get_irq_data(domain, virq)),
3478 get_count_order(nr_irqs));
3479
Marc Zyngierb48ac832014-11-24 14:35:16 +00003480 for (i = 0; i < nr_irqs; i++) {
3481 struct irq_data *data = irq_domain_get_irq_data(domain,
3482 virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003483 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00003484 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003485 }
3486
Marc Zyngier9791ec72019-01-29 10:02:33 +00003487 mutex_lock(&its->dev_alloc_lock);
3488
3489 /*
3490 * If all interrupts have been freed, start mopping the
3491 * floor. This is conditionned on the device not being shared.
3492 */
3493 if (!its_dev->shared &&
3494 bitmap_empty(its_dev->event_map.lpi_map,
Marc Zyngier591e5be2015-07-17 10:46:42 +01003495 its_dev->event_map.nr_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003496 its_lpi_free(its_dev->event_map.lpi_map,
3497 its_dev->event_map.lpi_base,
3498 its_dev->event_map.nr_lpis);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003499
3500 /* Unmap device/itt */
3501 its_send_mapd(its_dev, 0);
3502 its_free_device(its_dev);
3503 }
3504
Marc Zyngier9791ec72019-01-29 10:02:33 +00003505 mutex_unlock(&its->dev_alloc_lock);
3506
Marc Zyngierb48ac832014-11-24 14:35:16 +00003507 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3508}
3509
3510static const struct irq_domain_ops its_domain_ops = {
3511 .alloc = its_irq_domain_alloc,
3512 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00003513 .activate = its_irq_domain_activate,
3514 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00003515};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003516
Marc Zyngier20b3d542016-12-20 15:23:22 +00003517/*
3518 * This is insane.
3519 *
Marc Zyngier0684c702019-12-24 11:10:30 +00003520 * If a GICv4.0 doesn't implement Direct LPIs (which is extremely
Marc Zyngier20b3d542016-12-20 15:23:22 +00003521 * likely), the only way to perform an invalidate is to use a fake
3522 * device to issue an INV command, implying that the LPI has first
3523 * been mapped to some event on that device. Since this is not exactly
3524 * cheap, we try to keep that mapping around as long as possible, and
3525 * only issue an UNMAP if we're short on available slots.
3526 *
3527 * Broken by design(tm).
Marc Zyngier0684c702019-12-24 11:10:30 +00003528 *
3529 * GICv4.1, on the other hand, mandates that we're able to invalidate
3530 * by writing to a MMIO register. It doesn't implement the whole of
3531 * DirectLPI, but that's good enough. And most of the time, we don't
3532 * even have to invalidate anything, as the redistributor can be told
3533 * whether to generate a doorbell or not (we thus leave it enabled,
3534 * always).
Marc Zyngier20b3d542016-12-20 15:23:22 +00003535 */
3536static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
3537{
Marc Zyngier0684c702019-12-24 11:10:30 +00003538 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3539 if (gic_rdists->has_rvpeid)
3540 return;
3541
Marc Zyngier20b3d542016-12-20 15:23:22 +00003542 /* Already unmapped? */
3543 if (vpe->vpe_proxy_event == -1)
3544 return;
3545
3546 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
3547 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
3548
3549 /*
3550 * We don't track empty slots at all, so let's move the
3551 * next_victim pointer if we can quickly reuse that slot
3552 * instead of nuking an existing entry. Not clear that this is
3553 * always a win though, and this might just generate a ripple
3554 * effect... Let's just hope VPEs don't migrate too often.
3555 */
3556 if (vpe_proxy.vpes[vpe_proxy.next_victim])
3557 vpe_proxy.next_victim = vpe->vpe_proxy_event;
3558
3559 vpe->vpe_proxy_event = -1;
3560}
3561
3562static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
3563{
Marc Zyngier0684c702019-12-24 11:10:30 +00003564 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3565 if (gic_rdists->has_rvpeid)
3566 return;
3567
Marc Zyngier20b3d542016-12-20 15:23:22 +00003568 if (!gic_rdists->has_direct_lpi) {
3569 unsigned long flags;
3570
3571 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3572 its_vpe_db_proxy_unmap_locked(vpe);
3573 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3574 }
3575}
3576
3577static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
3578{
Marc Zyngier0684c702019-12-24 11:10:30 +00003579 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3580 if (gic_rdists->has_rvpeid)
3581 return;
3582
Marc Zyngier20b3d542016-12-20 15:23:22 +00003583 /* Already mapped? */
3584 if (vpe->vpe_proxy_event != -1)
3585 return;
3586
3587 /* This slot was already allocated. Kick the other VPE out. */
3588 if (vpe_proxy.vpes[vpe_proxy.next_victim])
3589 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
3590
3591 /* Map the new VPE instead */
3592 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
3593 vpe->vpe_proxy_event = vpe_proxy.next_victim;
3594 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
3595
3596 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
3597 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
3598}
3599
Marc Zyngier958b90d2017-08-18 16:14:17 +01003600static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
3601{
3602 unsigned long flags;
3603 struct its_collection *target_col;
3604
Marc Zyngier0684c702019-12-24 11:10:30 +00003605 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3606 if (gic_rdists->has_rvpeid)
3607 return;
3608
Marc Zyngier958b90d2017-08-18 16:14:17 +01003609 if (gic_rdists->has_direct_lpi) {
3610 void __iomem *rdbase;
3611
3612 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
3613 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
Marc Zyngier2f4f0642019-11-08 16:57:56 +00003614 wait_for_syncr(rdbase);
Marc Zyngier958b90d2017-08-18 16:14:17 +01003615
3616 return;
3617 }
3618
3619 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3620
3621 its_vpe_db_proxy_map_locked(vpe);
3622
3623 target_col = &vpe_proxy.dev->its->collections[to];
3624 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
3625 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
3626
3627 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3628}
3629
Marc Zyngier3171a472016-12-20 15:17:28 +00003630static int its_vpe_set_affinity(struct irq_data *d,
3631 const struct cpumask *mask_val,
3632 bool force)
3633{
3634 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003635 int from, cpu = cpumask_first(mask_val);
Marc Zyngierf3a059212020-03-04 20:33:10 +00003636 unsigned long flags;
Marc Zyngier3171a472016-12-20 15:17:28 +00003637
3638 /*
3639 * Changing affinity is mega expensive, so let's be as lazy as
Marc Zyngier20b3d542016-12-20 15:23:22 +00003640 * we can and only do it if we really have to. Also, if mapped
Marc Zyngier958b90d2017-08-18 16:14:17 +01003641 * into the proxy device, we need to move the doorbell
3642 * interrupt to its new location.
Marc Zyngierf3a059212020-03-04 20:33:10 +00003643 *
3644 * Another thing is that changing the affinity of a vPE affects
3645 * *other interrupts* such as all the vLPIs that are routed to
3646 * this vPE. This means that the irq_desc lock is not enough to
3647 * protect us, and that we must ensure nobody samples vpe->col_idx
3648 * during the update, hence the lock below which must also be
3649 * taken on any vLPI handling path that evaluates vpe->col_idx.
Marc Zyngier3171a472016-12-20 15:17:28 +00003650 */
Marc Zyngierf3a059212020-03-04 20:33:10 +00003651 from = vpe_to_cpuid_lock(vpe, &flags);
3652 if (from == cpu)
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003653 goto out;
Marc Zyngier958b90d2017-08-18 16:14:17 +01003654
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003655 vpe->col_idx = cpu;
Marc Zyngier3171a472016-12-20 15:17:28 +00003656
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003657 /*
3658 * GICv4.1 allows us to skip VMOVP if moving to a cpu whose RD
3659 * is sharing its VPE table with the current one.
3660 */
3661 if (gic_data_rdist_cpu(cpu)->vpe_table_mask &&
3662 cpumask_test_cpu(from, gic_data_rdist_cpu(cpu)->vpe_table_mask))
3663 goto out;
3664
3665 its_send_vmovp(vpe);
3666 its_vpe_db_proxy_move(vpe, from, cpu);
3667
3668out:
Marc Zyngier44c4c252017-10-19 10:11:34 +01003669 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngierf3a059212020-03-04 20:33:10 +00003670 vpe_to_cpuid_unlock(vpe, flags);
Marc Zyngier44c4c252017-10-19 10:11:34 +01003671
Marc Zyngier3171a472016-12-20 15:17:28 +00003672 return IRQ_SET_MASK_OK_DONE;
3673}
3674
Marc Zyngiere643d802016-12-20 15:09:31 +00003675static void its_vpe_schedule(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
3680 /* Schedule the VPE */
3681 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
3682 GENMASK_ULL(51, 12);
3683 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
3684 val |= GICR_VPROPBASER_RaWb;
3685 val |= GICR_VPROPBASER_InnerShareable;
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003686 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
Marc Zyngiere643d802016-12-20 15:09:31 +00003687
3688 val = virt_to_phys(page_address(vpe->vpt_page)) &
3689 GENMASK_ULL(51, 16);
3690 val |= GICR_VPENDBASER_RaWaWb;
Heyi Guob2cb11f2019-11-30 15:38:49 +08003691 val |= GICR_VPENDBASER_InnerShareable;
Marc Zyngiere643d802016-12-20 15:09:31 +00003692 /*
3693 * There is no good way of finding out if the pending table is
3694 * empty as we can race against the doorbell interrupt very
3695 * easily. So in the end, vpe->pending_last is only an
3696 * indication that the vcpu has something pending, not one
3697 * that the pending table is empty. A good implementation
3698 * would be able to read its coarse map pretty quickly anyway,
3699 * making this a tolerable issue.
3700 */
3701 val |= GICR_VPENDBASER_PendingLast;
3702 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
3703 val |= GICR_VPENDBASER_Valid;
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003704 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
Marc Zyngiere643d802016-12-20 15:09:31 +00003705}
3706
3707static void its_vpe_deschedule(struct its_vpe *vpe)
3708{
Robin Murphy50c33092018-02-16 16:57:56 +00003709 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00003710 u64 val;
3711
Marc Zyngiere64fab12019-12-24 11:10:35 +00003712 val = its_clear_vpend_valid(vlpi_base, 0, 0);
Marc Zyngiere643d802016-12-20 15:09:31 +00003713
Marc Zyngiere64fab12019-12-24 11:10:35 +00003714 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
3715 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
Marc Zyngiere643d802016-12-20 15:09:31 +00003716}
3717
Marc Zyngier40619a22017-10-08 15:16:09 +01003718static void its_vpe_invall(struct its_vpe *vpe)
3719{
3720 struct its_node *its;
3721
3722 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00003723 if (!is_v4(its))
Marc Zyngier40619a22017-10-08 15:16:09 +01003724 continue;
3725
Marc Zyngier2247e1b2017-10-08 18:50:36 +01003726 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
3727 continue;
3728
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01003729 /*
3730 * Sending a VINVALL to a single ITS is enough, as all
3731 * we need is to reach the redistributors.
3732 */
Marc Zyngier40619a22017-10-08 15:16:09 +01003733 its_send_vinvall(its, vpe);
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01003734 return;
Marc Zyngier40619a22017-10-08 15:16:09 +01003735 }
3736}
3737
Marc Zyngiere643d802016-12-20 15:09:31 +00003738static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
3739{
3740 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3741 struct its_cmd_info *info = vcpu_info;
3742
3743 switch (info->cmd_type) {
3744 case SCHEDULE_VPE:
3745 its_vpe_schedule(vpe);
3746 return 0;
3747
3748 case DESCHEDULE_VPE:
3749 its_vpe_deschedule(vpe);
3750 return 0;
3751
Marc Zyngier5e2f7642016-12-20 15:10:50 +00003752 case INVALL_VPE:
Marc Zyngier40619a22017-10-08 15:16:09 +01003753 its_vpe_invall(vpe);
Marc Zyngier5e2f7642016-12-20 15:10:50 +00003754 return 0;
3755
Marc Zyngiere643d802016-12-20 15:09:31 +00003756 default:
3757 return -EINVAL;
3758 }
3759}
3760
Marc Zyngier20b3d542016-12-20 15:23:22 +00003761static void its_vpe_send_cmd(struct its_vpe *vpe,
3762 void (*cmd)(struct its_device *, u32))
3763{
3764 unsigned long flags;
3765
3766 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3767
3768 its_vpe_db_proxy_map_locked(vpe);
3769 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
3770
3771 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3772}
3773
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003774static void its_vpe_send_inv(struct irq_data *d)
3775{
3776 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003777
Marc Zyngier20b3d542016-12-20 15:23:22 +00003778 if (gic_rdists->has_direct_lpi) {
3779 void __iomem *rdbase;
3780
Marc Zyngier425c09b2019-11-08 16:57:57 +00003781 /* Target the redistributor this VPE is currently known on */
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003782 raw_spin_lock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003783 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
Marc Zyngier425c09b2019-11-08 16:57:57 +00003784 gic_write_lpir(d->parent_data->hwirq, rdbase + GICR_INVLPIR);
Marc Zyngier2f4f0642019-11-08 16:57:56 +00003785 wait_for_syncr(rdbase);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003786 raw_spin_unlock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003787 } else {
3788 its_vpe_send_cmd(vpe, its_send_inv);
3789 }
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003790}
3791
3792static void its_vpe_mask_irq(struct irq_data *d)
3793{
3794 /*
3795 * We need to unmask the LPI, which is described by the parent
3796 * irq_data. Instead of calling into the parent (which won't
3797 * exactly do the right thing, let's simply use the
3798 * parent_data pointer. Yes, I'm naughty.
3799 */
3800 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
3801 its_vpe_send_inv(d);
3802}
3803
3804static void its_vpe_unmask_irq(struct irq_data *d)
3805{
3806 /* Same hack as above... */
3807 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
3808 its_vpe_send_inv(d);
3809}
3810
Marc Zyngiere57a3e282017-07-31 14:47:24 +01003811static int its_vpe_set_irqchip_state(struct irq_data *d,
3812 enum irqchip_irq_state which,
3813 bool state)
3814{
3815 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3816
3817 if (which != IRQCHIP_STATE_PENDING)
3818 return -EINVAL;
3819
3820 if (gic_rdists->has_direct_lpi) {
3821 void __iomem *rdbase;
3822
3823 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
3824 if (state) {
3825 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
3826 } else {
3827 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
Marc Zyngier2f4f0642019-11-08 16:57:56 +00003828 wait_for_syncr(rdbase);
Marc Zyngiere57a3e282017-07-31 14:47:24 +01003829 }
3830 } else {
3831 if (state)
3832 its_vpe_send_cmd(vpe, its_send_int);
3833 else
3834 its_vpe_send_cmd(vpe, its_send_clear);
3835 }
3836
3837 return 0;
3838}
3839
Marc Zyngier7809f702020-03-10 18:49:21 +00003840static int its_vpe_retrigger(struct irq_data *d)
3841{
3842 return !its_vpe_set_irqchip_state(d, IRQCHIP_STATE_PENDING, true);
3843}
3844
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003845static struct irq_chip its_vpe_irq_chip = {
3846 .name = "GICv4-vpe",
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003847 .irq_mask = its_vpe_mask_irq,
3848 .irq_unmask = its_vpe_unmask_irq,
3849 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier3171a472016-12-20 15:17:28 +00003850 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngier7809f702020-03-10 18:49:21 +00003851 .irq_retrigger = its_vpe_retrigger,
Marc Zyngiere57a3e282017-07-31 14:47:24 +01003852 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
Marc Zyngiere643d802016-12-20 15:09:31 +00003853 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003854};
3855
Marc Zyngierd97c97b2019-12-24 11:10:33 +00003856static struct its_node *find_4_1_its(void)
3857{
3858 static struct its_node *its = NULL;
3859
3860 if (!its) {
3861 list_for_each_entry(its, &its_nodes, entry) {
3862 if (is_v4_1(its))
3863 return its;
3864 }
3865
3866 /* Oops? */
3867 its = NULL;
3868 }
3869
3870 return its;
3871}
3872
3873static void its_vpe_4_1_send_inv(struct irq_data *d)
3874{
3875 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3876 struct its_node *its;
3877
3878 /*
3879 * GICv4.1 wants doorbells to be invalidated using the
3880 * INVDB command in order to be broadcast to all RDs. Send
3881 * it to the first valid ITS, and let the HW do its magic.
3882 */
3883 its = find_4_1_its();
3884 if (its)
3885 its_send_invdb(its, vpe);
3886}
3887
3888static void its_vpe_4_1_mask_irq(struct irq_data *d)
3889{
3890 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
3891 its_vpe_4_1_send_inv(d);
3892}
3893
3894static void its_vpe_4_1_unmask_irq(struct irq_data *d)
3895{
3896 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
3897 its_vpe_4_1_send_inv(d);
3898}
3899
Marc Zyngier91bf6392019-12-24 11:10:34 +00003900static void its_vpe_4_1_schedule(struct its_vpe *vpe,
3901 struct its_cmd_info *info)
3902{
3903 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3904 u64 val = 0;
3905
3906 /* Schedule the VPE */
3907 val |= GICR_VPENDBASER_Valid;
3908 val |= info->g0en ? GICR_VPENDBASER_4_1_VGRP0EN : 0;
3909 val |= info->g1en ? GICR_VPENDBASER_4_1_VGRP1EN : 0;
3910 val |= FIELD_PREP(GICR_VPENDBASER_4_1_VPEID, vpe->vpe_id);
3911
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003912 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
Marc Zyngier91bf6392019-12-24 11:10:34 +00003913}
3914
Marc Zyngiere64fab12019-12-24 11:10:35 +00003915static void its_vpe_4_1_deschedule(struct its_vpe *vpe,
3916 struct its_cmd_info *info)
3917{
3918 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3919 u64 val;
3920
3921 if (info->req_db) {
3922 /*
3923 * vPE is going to block: make the vPE non-resident with
3924 * PendingLast clear and DB set. The GIC guarantees that if
3925 * we read-back PendingLast clear, then a doorbell will be
3926 * delivered when an interrupt comes.
3927 */
3928 val = its_clear_vpend_valid(vlpi_base,
3929 GICR_VPENDBASER_PendingLast,
3930 GICR_VPENDBASER_4_1_DB);
3931 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
3932 } else {
3933 /*
3934 * We're not blocking, so just make the vPE non-resident
3935 * with PendingLast set, indicating that we'll be back.
3936 */
3937 val = its_clear_vpend_valid(vlpi_base,
3938 0,
3939 GICR_VPENDBASER_PendingLast);
3940 vpe->pending_last = true;
3941 }
3942}
3943
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003944static void its_vpe_4_1_invall(struct its_vpe *vpe)
3945{
3946 void __iomem *rdbase;
3947 u64 val;
3948
3949 val = GICR_INVALLR_V;
3950 val |= FIELD_PREP(GICR_INVALLR_VPEID, vpe->vpe_id);
3951
3952 /* Target the redistributor this vPE is currently known on */
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003953 raw_spin_lock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003954 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
3955 gic_write_lpir(val, rdbase + GICR_INVALLR);
Zenghui Yub978c252020-03-04 20:33:11 +00003956
3957 wait_for_syncr(rdbase);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003958 raw_spin_unlock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003959}
3960
Marc Zyngier29c647f2019-12-24 11:10:32 +00003961static int its_vpe_4_1_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
3962{
Marc Zyngier91bf6392019-12-24 11:10:34 +00003963 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003964 struct its_cmd_info *info = vcpu_info;
3965
3966 switch (info->cmd_type) {
3967 case SCHEDULE_VPE:
Marc Zyngier91bf6392019-12-24 11:10:34 +00003968 its_vpe_4_1_schedule(vpe, info);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003969 return 0;
3970
3971 case DESCHEDULE_VPE:
Marc Zyngiere64fab12019-12-24 11:10:35 +00003972 its_vpe_4_1_deschedule(vpe, info);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003973 return 0;
3974
3975 case INVALL_VPE:
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003976 its_vpe_4_1_invall(vpe);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003977 return 0;
3978
3979 default:
3980 return -EINVAL;
3981 }
3982}
3983
3984static struct irq_chip its_vpe_4_1_irq_chip = {
3985 .name = "GICv4.1-vpe",
Marc Zyngierd97c97b2019-12-24 11:10:33 +00003986 .irq_mask = its_vpe_4_1_mask_irq,
3987 .irq_unmask = its_vpe_4_1_unmask_irq,
Marc Zyngier29c647f2019-12-24 11:10:32 +00003988 .irq_eoi = irq_chip_eoi_parent,
3989 .irq_set_affinity = its_vpe_set_affinity,
3990 .irq_set_vcpu_affinity = its_vpe_4_1_set_vcpu_affinity,
3991};
3992
Marc Zyngiere252cf82020-03-04 20:33:16 +00003993static void its_configure_sgi(struct irq_data *d, bool clear)
3994{
3995 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3996 struct its_cmd_desc desc;
3997
3998 desc.its_vsgi_cmd.vpe = vpe;
3999 desc.its_vsgi_cmd.sgi = d->hwirq;
4000 desc.its_vsgi_cmd.priority = vpe->sgi_config[d->hwirq].priority;
4001 desc.its_vsgi_cmd.enable = vpe->sgi_config[d->hwirq].enabled;
4002 desc.its_vsgi_cmd.group = vpe->sgi_config[d->hwirq].group;
4003 desc.its_vsgi_cmd.clear = clear;
4004
4005 /*
4006 * GICv4.1 allows us to send VSGI commands to any ITS as long as the
4007 * destination VPE is mapped there. Since we map them eagerly at
4008 * activation time, we're pretty sure the first GICv4.1 ITS will do.
4009 */
4010 its_send_single_vcommand(find_4_1_its(), its_build_vsgi_cmd, &desc);
4011}
4012
Marc Zyngierb4e8d642020-03-04 20:33:17 +00004013static void its_sgi_mask_irq(struct irq_data *d)
4014{
4015 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4016
4017 vpe->sgi_config[d->hwirq].enabled = false;
4018 its_configure_sgi(d, false);
4019}
4020
4021static void its_sgi_unmask_irq(struct irq_data *d)
4022{
4023 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4024
4025 vpe->sgi_config[d->hwirq].enabled = true;
4026 its_configure_sgi(d, false);
4027}
4028
Marc Zyngier166cba72020-03-04 20:33:15 +00004029static int its_sgi_set_affinity(struct irq_data *d,
4030 const struct cpumask *mask_val,
4031 bool force)
4032{
4033 /*
4034 * There is no notion of affinity for virtual SGIs, at least
4035 * not on the host (since they can only be targetting a vPE).
4036 * Tell the kernel we've done whatever it asked for.
4037 */
4038 return IRQ_SET_MASK_OK;
4039}
4040
Marc Zyngier7017ff02020-03-04 20:33:18 +00004041static int its_sgi_set_irqchip_state(struct irq_data *d,
4042 enum irqchip_irq_state which,
4043 bool state)
4044{
4045 if (which != IRQCHIP_STATE_PENDING)
4046 return -EINVAL;
4047
4048 if (state) {
4049 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4050 struct its_node *its = find_4_1_its();
4051 u64 val;
4052
4053 val = FIELD_PREP(GITS_SGIR_VPEID, vpe->vpe_id);
4054 val |= FIELD_PREP(GITS_SGIR_VINTID, d->hwirq);
4055 writeq_relaxed(val, its->sgir_base + GITS_SGIR - SZ_128K);
4056 } else {
4057 its_configure_sgi(d, true);
4058 }
4059
4060 return 0;
4061}
4062
4063static int its_sgi_get_irqchip_state(struct irq_data *d,
4064 enum irqchip_irq_state which, bool *val)
4065{
4066 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4067 void __iomem *base;
4068 unsigned long flags;
4069 u32 count = 1000000; /* 1s! */
4070 u32 status;
4071 int cpu;
4072
4073 if (which != IRQCHIP_STATE_PENDING)
4074 return -EINVAL;
4075
4076 /*
4077 * Locking galore! We can race against two different events:
4078 *
4079 * - Concurent vPE affinity change: we must make sure it cannot
4080 * happen, or we'll talk to the wrong redistributor. This is
4081 * identical to what happens with vLPIs.
4082 *
4083 * - Concurrent VSGIPENDR access: As it involves accessing two
4084 * MMIO registers, this must be made atomic one way or another.
4085 */
4086 cpu = vpe_to_cpuid_lock(vpe, &flags);
4087 raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock);
4088 base = gic_data_rdist_cpu(cpu)->rd_base + SZ_128K;
4089 writel_relaxed(vpe->vpe_id, base + GICR_VSGIR);
4090 do {
4091 status = readl_relaxed(base + GICR_VSGIPENDR);
4092 if (!(status & GICR_VSGIPENDR_BUSY))
4093 goto out;
4094
4095 count--;
4096 if (!count) {
4097 pr_err_ratelimited("Unable to get SGI status\n");
4098 goto out;
4099 }
4100 cpu_relax();
4101 udelay(1);
4102 } while (count);
4103
4104out:
4105 raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock);
4106 vpe_to_cpuid_unlock(vpe, flags);
4107
4108 if (!count)
4109 return -ENXIO;
4110
4111 *val = !!(status & (1 << d->hwirq));
4112
4113 return 0;
4114}
4115
Marc Zyngier05d32df2020-03-04 20:33:19 +00004116static int its_sgi_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
4117{
4118 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4119 struct its_cmd_info *info = vcpu_info;
4120
4121 switch (info->cmd_type) {
4122 case PROP_UPDATE_VSGI:
4123 vpe->sgi_config[d->hwirq].priority = info->priority;
4124 vpe->sgi_config[d->hwirq].group = info->group;
4125 its_configure_sgi(d, false);
4126 return 0;
4127
4128 default:
4129 return -EINVAL;
4130 }
4131}
4132
Marc Zyngier166cba72020-03-04 20:33:15 +00004133static struct irq_chip its_sgi_irq_chip = {
4134 .name = "GICv4.1-sgi",
Marc Zyngierb4e8d642020-03-04 20:33:17 +00004135 .irq_mask = its_sgi_mask_irq,
4136 .irq_unmask = its_sgi_unmask_irq,
Marc Zyngier166cba72020-03-04 20:33:15 +00004137 .irq_set_affinity = its_sgi_set_affinity,
Marc Zyngier7017ff02020-03-04 20:33:18 +00004138 .irq_set_irqchip_state = its_sgi_set_irqchip_state,
4139 .irq_get_irqchip_state = its_sgi_get_irqchip_state,
Marc Zyngier05d32df2020-03-04 20:33:19 +00004140 .irq_set_vcpu_affinity = its_sgi_set_vcpu_affinity,
Marc Zyngier166cba72020-03-04 20:33:15 +00004141};
4142
4143static int its_sgi_irq_domain_alloc(struct irq_domain *domain,
4144 unsigned int virq, unsigned int nr_irqs,
4145 void *args)
4146{
4147 struct its_vpe *vpe = args;
4148 int i;
4149
4150 /* Yes, we do want 16 SGIs */
4151 WARN_ON(nr_irqs != 16);
4152
4153 for (i = 0; i < 16; i++) {
4154 vpe->sgi_config[i].priority = 0;
4155 vpe->sgi_config[i].enabled = false;
4156 vpe->sgi_config[i].group = false;
4157
4158 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
4159 &its_sgi_irq_chip, vpe);
4160 irq_set_status_flags(virq + i, IRQ_DISABLE_UNLAZY);
4161 }
4162
4163 return 0;
4164}
4165
4166static void its_sgi_irq_domain_free(struct irq_domain *domain,
4167 unsigned int virq,
4168 unsigned int nr_irqs)
4169{
4170 /* Nothing to do */
4171}
4172
4173static int its_sgi_irq_domain_activate(struct irq_domain *domain,
4174 struct irq_data *d, bool reserve)
4175{
Marc Zyngiere252cf82020-03-04 20:33:16 +00004176 /* Write out the initial SGI configuration */
4177 its_configure_sgi(d, false);
Marc Zyngier166cba72020-03-04 20:33:15 +00004178 return 0;
4179}
4180
4181static void its_sgi_irq_domain_deactivate(struct irq_domain *domain,
4182 struct irq_data *d)
4183{
Marc Zyngiere252cf82020-03-04 20:33:16 +00004184 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4185
4186 /*
4187 * The VSGI command is awkward:
4188 *
4189 * - To change the configuration, CLEAR must be set to false,
4190 * leaving the pending bit unchanged.
4191 * - To clear the pending bit, CLEAR must be set to true, leaving
4192 * the configuration unchanged.
4193 *
4194 * You just can't do both at once, hence the two commands below.
4195 */
4196 vpe->sgi_config[d->hwirq].enabled = false;
4197 its_configure_sgi(d, false);
4198 its_configure_sgi(d, true);
Marc Zyngier166cba72020-03-04 20:33:15 +00004199}
4200
4201static const struct irq_domain_ops its_sgi_domain_ops = {
4202 .alloc = its_sgi_irq_domain_alloc,
4203 .free = its_sgi_irq_domain_free,
4204 .activate = its_sgi_irq_domain_activate,
4205 .deactivate = its_sgi_irq_domain_deactivate,
4206};
4207
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004208static int its_vpe_id_alloc(void)
4209{
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05004210 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004211}
4212
4213static void its_vpe_id_free(u16 id)
4214{
4215 ida_simple_remove(&its_vpeid_ida, id);
4216}
4217
4218static int its_vpe_init(struct its_vpe *vpe)
4219{
4220 struct page *vpt_page;
4221 int vpe_id;
4222
4223 /* Allocate vpe_id */
4224 vpe_id = its_vpe_id_alloc();
4225 if (vpe_id < 0)
4226 return vpe_id;
4227
4228 /* Allocate VPT */
4229 vpt_page = its_allocate_pending_table(GFP_KERNEL);
4230 if (!vpt_page) {
4231 its_vpe_id_free(vpe_id);
4232 return -ENOMEM;
4233 }
4234
4235 if (!its_alloc_vpe_table(vpe_id)) {
4236 its_vpe_id_free(vpe_id);
Nianyao Tang34f8eb92019-07-26 17:32:57 +08004237 its_free_pending_table(vpt_page);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004238 return -ENOMEM;
4239 }
4240
Marc Zyngierf3a059212020-03-04 20:33:10 +00004241 raw_spin_lock_init(&vpe->vpe_lock);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004242 vpe->vpe_id = vpe_id;
4243 vpe->vpt_page = vpt_page;
Marc Zyngier64edfaa2019-12-24 11:10:29 +00004244 if (gic_rdists->has_rvpeid)
4245 atomic_set(&vpe->vmapp_count, 0);
4246 else
4247 vpe->vpe_proxy_event = -1;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004248
4249 return 0;
4250}
4251
4252static void its_vpe_teardown(struct its_vpe *vpe)
4253{
Marc Zyngier20b3d542016-12-20 15:23:22 +00004254 its_vpe_db_proxy_unmap(vpe);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004255 its_vpe_id_free(vpe->vpe_id);
4256 its_free_pending_table(vpe->vpt_page);
4257}
4258
4259static void its_vpe_irq_domain_free(struct irq_domain *domain,
4260 unsigned int virq,
4261 unsigned int nr_irqs)
4262{
4263 struct its_vm *vm = domain->host_data;
4264 int i;
4265
4266 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
4267
4268 for (i = 0; i < nr_irqs; i++) {
4269 struct irq_data *data = irq_domain_get_irq_data(domain,
4270 virq + i);
4271 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
4272
4273 BUG_ON(vm != vpe->its_vm);
4274
4275 clear_bit(data->hwirq, vm->db_bitmap);
4276 its_vpe_teardown(vpe);
4277 irq_domain_reset_irq_data(data);
4278 }
4279
4280 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01004281 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004282 its_free_prop_table(vm->vprop_page);
4283 }
4284}
4285
4286static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
4287 unsigned int nr_irqs, void *args)
4288{
Marc Zyngier29c647f2019-12-24 11:10:32 +00004289 struct irq_chip *irqchip = &its_vpe_irq_chip;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004290 struct its_vm *vm = args;
4291 unsigned long *bitmap;
4292 struct page *vprop_page;
4293 int base, nr_ids, i, err = 0;
4294
4295 BUG_ON(!vm);
4296
Marc Zyngier38dd7c42018-05-27 17:03:03 +01004297 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004298 if (!bitmap)
4299 return -ENOMEM;
4300
4301 if (nr_ids < nr_irqs) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01004302 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004303 return -ENOMEM;
4304 }
4305
4306 vprop_page = its_allocate_prop_table(GFP_KERNEL);
4307 if (!vprop_page) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01004308 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004309 return -ENOMEM;
4310 }
4311
4312 vm->db_bitmap = bitmap;
4313 vm->db_lpi_base = base;
4314 vm->nr_db_lpis = nr_ids;
4315 vm->vprop_page = vprop_page;
4316
Marc Zyngier29c647f2019-12-24 11:10:32 +00004317 if (gic_rdists->has_rvpeid)
4318 irqchip = &its_vpe_4_1_irq_chip;
4319
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004320 for (i = 0; i < nr_irqs; i++) {
4321 vm->vpes[i]->vpe_db_lpi = base + i;
4322 err = its_vpe_init(vm->vpes[i]);
4323 if (err)
4324 break;
4325 err = its_irq_gic_domain_alloc(domain, virq + i,
4326 vm->vpes[i]->vpe_db_lpi);
4327 if (err)
4328 break;
4329 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
Marc Zyngier29c647f2019-12-24 11:10:32 +00004330 irqchip, vm->vpes[i]);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004331 set_bit(i, bitmap);
4332 }
4333
4334 if (err) {
4335 if (i > 0)
4336 its_vpe_irq_domain_free(domain, virq, i - 1);
4337
Marc Zyngier38dd7c42018-05-27 17:03:03 +01004338 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004339 its_free_prop_table(vprop_page);
4340 }
4341
4342 return err;
4343}
4344
Thomas Gleixner72491642017-09-13 23:29:10 +02004345static int its_vpe_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01004346 struct irq_data *d, bool reserve)
Marc Zyngiereb781922016-12-20 14:47:05 +00004347{
4348 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier40619a22017-10-08 15:16:09 +01004349 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00004350
Marc Zyngier009384b2020-03-04 20:33:23 +00004351 /*
4352 * If we use the list map, we issue VMAPP on demand... Unless
4353 * we're on a GICv4.1 and we eagerly map the VPE on all ITSs
4354 * so that VSGIs can work.
4355 */
4356 if (!gic_requires_eager_mapping())
Marc Zyngier6ef930f2017-11-07 10:04:38 +00004357 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00004358
4359 /* Map the VPE to the first possible CPU */
4360 vpe->col_idx = cpumask_first(cpu_online_mask);
Marc Zyngier40619a22017-10-08 15:16:09 +01004361
4362 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004363 if (!is_v4(its))
Marc Zyngier40619a22017-10-08 15:16:09 +01004364 continue;
4365
Marc Zyngier75fd9512017-10-08 18:46:39 +01004366 its_send_vmapp(its, vpe, true);
Marc Zyngier40619a22017-10-08 15:16:09 +01004367 its_send_vinvall(its, vpe);
4368 }
4369
Marc Zyngier44c4c252017-10-19 10:11:34 +01004370 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
4371
Thomas Gleixner72491642017-09-13 23:29:10 +02004372 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00004373}
4374
4375static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
4376 struct irq_data *d)
4377{
4378 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier75fd9512017-10-08 18:46:39 +01004379 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00004380
Marc Zyngier2247e1b2017-10-08 18:50:36 +01004381 /*
Marc Zyngier009384b2020-03-04 20:33:23 +00004382 * If we use the list map on GICv4.0, we unmap the VPE once no
4383 * VLPIs are associated with the VM.
Marc Zyngier2247e1b2017-10-08 18:50:36 +01004384 */
Marc Zyngier009384b2020-03-04 20:33:23 +00004385 if (!gic_requires_eager_mapping())
Marc Zyngier2247e1b2017-10-08 18:50:36 +01004386 return;
4387
Marc Zyngier75fd9512017-10-08 18:46:39 +01004388 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004389 if (!is_v4(its))
Marc Zyngier75fd9512017-10-08 18:46:39 +01004390 continue;
4391
4392 its_send_vmapp(its, vpe, false);
4393 }
Marc Zyngiereb781922016-12-20 14:47:05 +00004394}
4395
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004396static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004397 .alloc = its_vpe_irq_domain_alloc,
4398 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00004399 .activate = its_vpe_irq_domain_activate,
4400 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004401};
4402
Yun Wu4559fbb2015-03-06 16:37:50 +00004403static int its_force_quiescent(void __iomem *base)
4404{
4405 u32 count = 1000000; /* 1s */
4406 u32 val;
4407
4408 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07004409 /*
4410 * GIC architecture specification requires the ITS to be both
4411 * disabled and quiescent for writes to GITS_BASER<n> or
4412 * GITS_CBASER to not have UNPREDICTABLE results.
4413 */
4414 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00004415 return 0;
4416
4417 /* Disable the generation of all interrupts to this ITS */
Marc Zyngierd51c4b42017-06-27 21:24:25 +01004418 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
Yun Wu4559fbb2015-03-06 16:37:50 +00004419 writel_relaxed(val, base + GITS_CTLR);
4420
4421 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
4422 while (1) {
4423 val = readl_relaxed(base + GITS_CTLR);
4424 if (val & GITS_CTLR_QUIESCENT)
4425 return 0;
4426
4427 count--;
4428 if (!count)
4429 return -EBUSY;
4430
4431 cpu_relax();
4432 udelay(1);
4433 }
4434}
4435
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004436static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
Robert Richter94100972015-09-21 22:58:38 +02004437{
4438 struct its_node *its = data;
4439
Marc Zyngier576a8342019-11-08 16:58:00 +00004440 /* erratum 22375: only alloc 8MB table size (20 bits) */
4441 its->typer &= ~GITS_TYPER_DEVBITS;
4442 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, 20 - 1);
Robert Richter94100972015-09-21 22:58:38 +02004443 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004444
4445 return true;
Robert Richter94100972015-09-21 22:58:38 +02004446}
4447
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004448static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02004449{
4450 struct its_node *its = data;
4451
4452 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004453
4454 return true;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02004455}
4456
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004457static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
Shanker Donthineni90922a22017-03-07 08:20:38 -06004458{
4459 struct its_node *its = data;
4460
4461 /* On QDF2400, the size of the ITE is 16Bytes */
Marc Zyngierffedbf02019-11-08 16:57:59 +00004462 its->typer &= ~GITS_TYPER_ITT_ENTRY_SIZE;
4463 its->typer |= FIELD_PREP(GITS_TYPER_ITT_ENTRY_SIZE, 16 - 1);
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004464
4465 return true;
Shanker Donthineni90922a22017-03-07 08:20:38 -06004466}
4467
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004468static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
4469{
4470 struct its_node *its = its_dev->its;
4471
4472 /*
4473 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
4474 * which maps 32-bit writes targeted at a separate window of
4475 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
4476 * with device ID taken from bits [device_id_bits + 1:2] of
4477 * the window offset.
4478 */
4479 return its->pre_its_base + (its_dev->device_id << 2);
4480}
4481
4482static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
4483{
4484 struct its_node *its = data;
4485 u32 pre_its_window[2];
4486 u32 ids;
4487
4488 if (!fwnode_property_read_u32_array(its->fwnode_handle,
4489 "socionext,synquacer-pre-its",
4490 pre_its_window,
4491 ARRAY_SIZE(pre_its_window))) {
4492
4493 its->pre_its_base = pre_its_window[0];
4494 its->get_msi_base = its_irq_get_msi_base_pre_its;
4495
4496 ids = ilog2(pre_its_window[1]) - 2;
Marc Zyngier576a8342019-11-08 16:58:00 +00004497 if (device_ids(its) > ids) {
4498 its->typer &= ~GITS_TYPER_DEVBITS;
4499 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, ids - 1);
4500 }
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004501
4502 /* the pre-ITS breaks isolation, so disable MSI remapping */
4503 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
4504 return true;
4505 }
4506 return false;
4507}
4508
Marc Zyngier5c9a8822017-07-28 21:20:37 +01004509static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
4510{
4511 struct its_node *its = data;
4512
4513 /*
4514 * Hip07 insists on using the wrong address for the VLPI
4515 * page. Trick it into doing the right thing...
4516 */
4517 its->vlpi_redist_offset = SZ_128K;
4518 return true;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00004519}
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004520
Robert Richter67510cc2015-09-21 22:58:37 +02004521static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02004522#ifdef CONFIG_CAVIUM_ERRATUM_22375
4523 {
4524 .desc = "ITS: Cavium errata 22375, 24313",
4525 .iidr = 0xa100034c, /* ThunderX pass 1.x */
4526 .mask = 0xffff0fff,
4527 .init = its_enable_quirk_cavium_22375,
4528 },
4529#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02004530#ifdef CONFIG_CAVIUM_ERRATUM_23144
4531 {
4532 .desc = "ITS: Cavium erratum 23144",
4533 .iidr = 0xa100034c, /* ThunderX pass 1.x */
4534 .mask = 0xffff0fff,
4535 .init = its_enable_quirk_cavium_23144,
4536 },
4537#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06004538#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
4539 {
4540 .desc = "ITS: QDF2400 erratum 0065",
4541 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
4542 .mask = 0xffffffff,
4543 .init = its_enable_quirk_qdf2400_e0065,
4544 },
4545#endif
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004546#ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
4547 {
4548 /*
4549 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
4550 * implementation, but with a 'pre-ITS' added that requires
4551 * special handling in software.
4552 */
4553 .desc = "ITS: Socionext Synquacer pre-ITS",
4554 .iidr = 0x0001143b,
4555 .mask = 0xffffffff,
4556 .init = its_enable_quirk_socionext_synquacer,
4557 },
4558#endif
Marc Zyngier5c9a8822017-07-28 21:20:37 +01004559#ifdef CONFIG_HISILICON_ERRATUM_161600802
4560 {
4561 .desc = "ITS: Hip07 erratum 161600802",
4562 .iidr = 0x00000004,
4563 .mask = 0xffffffff,
4564 .init = its_enable_quirk_hip07_161600802,
4565 },
4566#endif
Robert Richter67510cc2015-09-21 22:58:37 +02004567 {
4568 }
4569};
4570
4571static void its_enable_quirks(struct its_node *its)
4572{
4573 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
4574
4575 gic_enable_quirks(iidr, its_quirks, its);
4576}
4577
Derek Basehoredba0bc72018-02-28 21:48:18 -08004578static int its_save_disable(void)
4579{
4580 struct its_node *its;
4581 int err = 0;
4582
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004583 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004584 list_for_each_entry(its, &its_nodes, entry) {
4585 void __iomem *base;
4586
4587 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4588 continue;
4589
4590 base = its->base;
4591 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
4592 err = its_force_quiescent(base);
4593 if (err) {
4594 pr_err("ITS@%pa: failed to quiesce: %d\n",
4595 &its->phys_base, err);
4596 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
4597 goto err;
4598 }
4599
4600 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
4601 }
4602
4603err:
4604 if (err) {
4605 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
4606 void __iomem *base;
4607
4608 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4609 continue;
4610
4611 base = its->base;
4612 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
4613 }
4614 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004615 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004616
4617 return err;
4618}
4619
4620static void its_restore_enable(void)
4621{
4622 struct its_node *its;
4623 int ret;
4624
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004625 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004626 list_for_each_entry(its, &its_nodes, entry) {
4627 void __iomem *base;
4628 int i;
4629
4630 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4631 continue;
4632
4633 base = its->base;
4634
4635 /*
4636 * Make sure that the ITS is disabled. If it fails to quiesce,
4637 * don't restore it since writing to CBASER or BASER<n>
4638 * registers is undefined according to the GIC v3 ITS
4639 * Specification.
4640 */
4641 ret = its_force_quiescent(base);
4642 if (ret) {
4643 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
4644 &its->phys_base, ret);
4645 continue;
4646 }
4647
4648 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
4649
4650 /*
4651 * Writing CBASER resets CREADR to 0, so make CWRITER and
4652 * cmd_write line up with it.
4653 */
4654 its->cmd_write = its->cmd_base;
4655 gits_write_cwriter(0, base + GITS_CWRITER);
4656
4657 /* Restore GITS_BASER from the value cache. */
4658 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
4659 struct its_baser *baser = &its->tables[i];
4660
4661 if (!(baser->val & GITS_BASER_VALID))
4662 continue;
4663
4664 its_write_baser(its, baser, baser->val);
4665 }
4666 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
Derek Basehore920181c2018-02-28 21:48:20 -08004667
4668 /*
4669 * Reinit the collection if it's stored in the ITS. This is
4670 * indicated by the col_id being less than the HCC field.
4671 * CID < HCC as specified in the GIC v3 Documentation.
4672 */
4673 if (its->collections[smp_processor_id()].col_id <
4674 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
4675 its_cpu_init_collection(its);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004676 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004677 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004678}
4679
4680static struct syscore_ops its_syscore_ops = {
4681 .suspend = its_save_disable,
4682 .resume = its_restore_enable,
4683};
4684
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004685static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004686{
4687 struct irq_domain *inner_domain;
4688 struct msi_domain_info *info;
4689
4690 info = kzalloc(sizeof(*info), GFP_KERNEL);
4691 if (!info)
4692 return -ENOMEM;
4693
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004694 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004695 if (!inner_domain) {
4696 kfree(info);
4697 return -ENOMEM;
4698 }
4699
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004700 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01004701 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004702 inner_domain->flags |= its->msi_domain_flags;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004703 info->ops = &its_msi_domain_ops;
4704 info->data = its;
4705 inner_domain->host_data = info;
4706
4707 return 0;
4708}
4709
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004710static int its_init_vpe_domain(void)
4711{
Marc Zyngier20b3d542016-12-20 15:23:22 +00004712 struct its_node *its;
4713 u32 devid;
4714 int entries;
4715
4716 if (gic_rdists->has_direct_lpi) {
4717 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
4718 return 0;
4719 }
4720
4721 /* Any ITS will do, even if not v4 */
4722 its = list_first_entry(&its_nodes, struct its_node, entry);
4723
4724 entries = roundup_pow_of_two(nr_cpu_ids);
Kees Cook6396bb22018-06-12 14:03:40 -07004725 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
Marc Zyngier20b3d542016-12-20 15:23:22 +00004726 GFP_KERNEL);
4727 if (!vpe_proxy.vpes) {
4728 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
4729 return -ENOMEM;
4730 }
4731
4732 /* Use the last possible DevID */
Marc Zyngier576a8342019-11-08 16:58:00 +00004733 devid = GENMASK(device_ids(its) - 1, 0);
Marc Zyngier20b3d542016-12-20 15:23:22 +00004734 vpe_proxy.dev = its_create_device(its, devid, entries, false);
4735 if (!vpe_proxy.dev) {
4736 kfree(vpe_proxy.vpes);
4737 pr_err("ITS: Can't allocate GICv4 proxy device\n");
4738 return -ENOMEM;
4739 }
4740
Shanker Donthinenic427a472017-09-23 13:50:19 -05004741 BUG_ON(entries > vpe_proxy.dev->nr_ites);
Marc Zyngier20b3d542016-12-20 15:23:22 +00004742
4743 raw_spin_lock_init(&vpe_proxy.lock);
4744 vpe_proxy.next_victim = 0;
4745 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
4746 devid, vpe_proxy.dev->nr_ites);
4747
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004748 return 0;
4749}
4750
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004751static int __init its_compute_its_list_map(struct resource *res,
4752 void __iomem *its_base)
4753{
4754 int its_number;
4755 u32 ctlr;
4756
4757 /*
4758 * This is assumed to be done early enough that we're
4759 * guaranteed to be single-threaded, hence no
4760 * locking. Should this change, we should address
4761 * this.
4762 */
Marc Zyngierab604912017-10-08 18:48:06 +01004763 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
4764 if (its_number >= GICv4_ITS_LIST_MAX) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004765 pr_err("ITS@%pa: No ITSList entry available!\n",
4766 &res->start);
4767 return -EINVAL;
4768 }
4769
4770 ctlr = readl_relaxed(its_base + GITS_CTLR);
4771 ctlr &= ~GITS_CTLR_ITS_NUMBER;
4772 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
4773 writel_relaxed(ctlr, its_base + GITS_CTLR);
4774 ctlr = readl_relaxed(its_base + GITS_CTLR);
4775 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
4776 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
4777 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
4778 }
4779
4780 if (test_and_set_bit(its_number, &its_list_map)) {
4781 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
4782 &res->start, its_number);
4783 return -EINVAL;
4784 }
4785
4786 return its_number;
4787}
4788
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004789static int __init its_probe_one(struct resource *res,
4790 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004791{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004792 struct its_node *its;
4793 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004794 u32 val, ctlr;
4795 u64 baser, tmp, typer;
Shanker Donthineni539d3782019-01-14 09:50:19 +00004796 struct page *page;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004797 int err;
4798
Marc Zyngier5e46a482020-03-04 20:33:14 +00004799 its_base = ioremap(res->start, SZ_64K);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004800 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004801 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004802 return -ENOMEM;
4803 }
4804
4805 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
4806 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004807 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004808 err = -ENODEV;
4809 goto out_unmap;
4810 }
4811
Yun Wu4559fbb2015-03-06 16:37:50 +00004812 err = its_force_quiescent(its_base);
4813 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004814 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00004815 goto out_unmap;
4816 }
4817
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004818 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004819
4820 its = kzalloc(sizeof(*its), GFP_KERNEL);
4821 if (!its) {
4822 err = -ENOMEM;
4823 goto out_unmap;
4824 }
4825
4826 raw_spin_lock_init(&its->lock);
Marc Zyngier9791ec72019-01-29 10:02:33 +00004827 mutex_init(&its->dev_alloc_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004828 INIT_LIST_HEAD(&its->entry);
4829 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004830 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004831 its->typer = typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004832 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004833 its->phys_base = res->start;
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004834 if (is_v4(its)) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004835 if (!(typer & GITS_TYPER_VMOVP)) {
4836 err = its_compute_its_list_map(res, its_base);
4837 if (err < 0)
4838 goto out_free_its;
4839
Marc Zyngierdebf6d02017-10-08 18:44:42 +01004840 its->list_nr = err;
4841
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004842 pr_info("ITS@%pa: Using ITS number %d\n",
4843 &res->start, err);
4844 } else {
4845 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
4846 }
Marc Zyngier5e516842019-12-24 11:10:28 +00004847
4848 if (is_v4_1(its)) {
4849 u32 svpet = FIELD_GET(GITS_TYPER_SVPET, typer);
Marc Zyngier5e46a482020-03-04 20:33:14 +00004850
4851 its->sgir_base = ioremap(res->start + SZ_128K, SZ_64K);
4852 if (!its->sgir_base) {
4853 err = -ENOMEM;
4854 goto out_free_its;
4855 }
4856
Marc Zyngier5e516842019-12-24 11:10:28 +00004857 its->mpidr = readl_relaxed(its_base + GITS_MPIDR);
4858
4859 pr_info("ITS@%pa: Using GICv4.1 mode %08x %08x\n",
4860 &res->start, its->mpidr, svpet);
4861 }
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004862 }
4863
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004864 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004865
Shanker Donthineni539d3782019-01-14 09:50:19 +00004866 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
4867 get_order(ITS_CMD_QUEUE_SZ));
4868 if (!page) {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004869 err = -ENOMEM;
Marc Zyngier5e46a482020-03-04 20:33:14 +00004870 goto out_unmap_sgir;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004871 }
Shanker Donthineni539d3782019-01-14 09:50:19 +00004872 its->cmd_base = (void *)page_address(page);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004873 its->cmd_write = its->cmd_base;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004874 its->fwnode_handle = handle;
4875 its->get_msi_base = its_irq_get_msi_base;
4876 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004877
Robert Richter67510cc2015-09-21 22:58:37 +02004878 its_enable_quirks(its);
4879
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05004880 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004881 if (err)
4882 goto out_free_cmd;
4883
4884 err = its_alloc_collections(its);
4885 if (err)
4886 goto out_free_tables;
4887
4888 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06004889 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004890 GITS_CBASER_InnerShareable |
4891 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
4892 GITS_CBASER_VALID);
4893
Vladimir Murzin0968a612016-11-02 11:54:06 +00004894 gits_write_cbaser(baser, its->base + GITS_CBASER);
4895 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004896
Marc Zyngier4ad3e362015-03-27 14:15:04 +00004897 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00004898 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
4899 /*
4900 * The HW reports non-shareable, we must
4901 * remove the cacheability attributes as
4902 * well.
4903 */
4904 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
4905 GITS_CBASER_CACHEABILITY_MASK);
4906 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00004907 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00004908 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004909 pr_info("ITS: using cache flushing for cmd queue\n");
4910 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
4911 }
4912
Vladimir Murzin0968a612016-11-02 11:54:06 +00004913 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004914 ctlr = readl_relaxed(its->base + GITS_CTLR);
Marc Zyngierd51c4b42017-06-27 21:24:25 +01004915 ctlr |= GITS_CTLR_ENABLE;
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004916 if (is_v4(its))
Marc Zyngierd51c4b42017-06-27 21:24:25 +01004917 ctlr |= GITS_CTLR_ImDe;
4918 writel_relaxed(ctlr, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00004919
Derek Basehoredba0bc72018-02-28 21:48:18 -08004920 if (GITS_TYPER_HCC(typer))
4921 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
4922
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004923 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004924 if (err)
4925 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004926
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004927 raw_spin_lock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004928 list_add(&its->entry, &its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004929 raw_spin_unlock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004930
4931 return 0;
4932
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004933out_free_tables:
4934 its_free_tables(its);
4935out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01004936 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier5e46a482020-03-04 20:33:14 +00004937out_unmap_sgir:
4938 if (its->sgir_base)
4939 iounmap(its->sgir_base);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004940out_free_its:
4941 kfree(its);
4942out_unmap:
4943 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004944 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004945 return err;
4946}
4947
4948static bool gic_rdists_supports_plpis(void)
4949{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01004950 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004951}
4952
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05004953static int redist_disable_lpis(void)
4954{
4955 void __iomem *rbase = gic_data_rdist_rd_base();
4956 u64 timeout = USEC_PER_SEC;
4957 u64 val;
4958
4959 if (!gic_rdists_supports_plpis()) {
4960 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
4961 return -ENXIO;
4962 }
4963
4964 val = readl_relaxed(rbase + GICR_CTLR);
4965 if (!(val & GICR_CTLR_ENABLE_LPIS))
4966 return 0;
4967
Marc Zyngier11e37d32018-07-27 13:38:54 +01004968 /*
4969 * If coming via a CPU hotplug event, we don't need to disable
4970 * LPIs before trying to re-enable them. They are already
4971 * configured and all is well in the world.
Marc Zyngierc440a9d2018-07-27 15:40:13 +01004972 *
4973 * If running with preallocated tables, there is nothing to do.
Marc Zyngier11e37d32018-07-27 13:38:54 +01004974 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01004975 if (gic_data_rdist()->lpi_enabled ||
4976 (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED))
Marc Zyngier11e37d32018-07-27 13:38:54 +01004977 return 0;
4978
4979 /*
4980 * From that point on, we only try to do some damage control.
4981 */
4982 pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05004983 smp_processor_id());
4984 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
4985
4986 /* Disable LPIs */
4987 val &= ~GICR_CTLR_ENABLE_LPIS;
4988 writel_relaxed(val, rbase + GICR_CTLR);
4989
4990 /* Make sure any change to GICR_CTLR is observable by the GIC */
4991 dsb(sy);
4992
4993 /*
4994 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
4995 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
4996 * Error out if we time out waiting for RWP to clear.
4997 */
4998 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
4999 if (!timeout) {
5000 pr_err("CPU%d: Timeout while disabling LPIs\n",
5001 smp_processor_id());
5002 return -ETIMEDOUT;
5003 }
5004 udelay(1);
5005 timeout--;
5006 }
5007
5008 /*
5009 * After it has been written to 1, it is IMPLEMENTATION
5010 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
5011 * cleared to 0. Error out if clearing the bit failed.
5012 */
5013 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
5014 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
5015 return -EBUSY;
5016 }
5017
5018 return 0;
5019}
5020
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005021int its_cpu_init(void)
5022{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005023 if (!list_empty(&its_nodes)) {
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05005024 int ret;
5025
5026 ret = redist_disable_lpis();
5027 if (ret)
5028 return ret;
5029
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005030 its_cpu_init_lpis();
Derek Basehore920181c2018-02-28 21:48:20 -08005031 its_cpu_init_collections();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005032 }
5033
5034 return 0;
5035}
5036
Arvind Yadav935bba72017-06-22 16:05:30 +05305037static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005038 { .compatible = "arm,gic-v3-its", },
5039 {},
5040};
5041
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005042static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005043{
5044 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005045 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005046
5047 for (np = of_find_matching_node(node, its_device_id); np;
5048 np = of_find_matching_node(np, its_device_id)) {
Stephen Boyd95a25622018-02-01 09:03:29 -08005049 if (!of_device_is_available(np))
5050 continue;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02005051 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05005052 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
5053 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02005054 continue;
5055 }
5056
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005057 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05005058 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005059 continue;
5060 }
5061
5062 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005063 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005064 return 0;
5065}
5066
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005067#ifdef CONFIG_ACPI
5068
5069#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
5070
Robert Richterd1ce2632017-07-12 15:25:09 +02005071#ifdef CONFIG_ACPI_NUMA
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305072struct its_srat_map {
5073 /* numa node id */
5074 u32 numa_node;
5075 /* GIC ITS ID */
5076 u32 its_id;
5077};
5078
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005079static struct its_srat_map *its_srat_maps __initdata;
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305080static int its_in_srat __initdata;
5081
5082static int __init acpi_get_its_numa_node(u32 its_id)
5083{
5084 int i;
5085
5086 for (i = 0; i < its_in_srat; i++) {
5087 if (its_id == its_srat_maps[i].its_id)
5088 return its_srat_maps[i].numa_node;
5089 }
5090 return NUMA_NO_NODE;
5091}
5092
Keith Busch60574d12019-03-11 14:55:57 -06005093static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header,
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005094 const unsigned long end)
5095{
5096 return 0;
5097}
5098
Keith Busch60574d12019-03-11 14:55:57 -06005099static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header,
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305100 const unsigned long end)
5101{
5102 int node;
5103 struct acpi_srat_gic_its_affinity *its_affinity;
5104
5105 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
5106 if (!its_affinity)
5107 return -EINVAL;
5108
5109 if (its_affinity->header.length < sizeof(*its_affinity)) {
5110 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
5111 its_affinity->header.length);
5112 return -EINVAL;
5113 }
5114
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305115 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
5116
5117 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
5118 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
5119 return 0;
5120 }
5121
5122 its_srat_maps[its_in_srat].numa_node = node;
5123 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
5124 its_in_srat++;
5125 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
5126 its_affinity->proximity_domain, its_affinity->its_id, node);
5127
5128 return 0;
5129}
5130
5131static void __init acpi_table_parse_srat_its(void)
5132{
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005133 int count;
5134
5135 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
5136 sizeof(struct acpi_table_srat),
5137 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
5138 gic_acpi_match_srat_its, 0);
5139 if (count <= 0)
5140 return;
5141
Kees Cook6da2ec52018-06-12 13:55:00 -07005142 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
5143 GFP_KERNEL);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005144 if (!its_srat_maps) {
5145 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
5146 return;
5147 }
5148
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305149 acpi_table_parse_entries(ACPI_SIG_SRAT,
5150 sizeof(struct acpi_table_srat),
5151 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
5152 gic_acpi_parse_srat_its, 0);
5153}
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005154
5155/* free the its_srat_maps after ITS probing */
5156static void __init acpi_its_srat_maps_free(void)
5157{
5158 kfree(its_srat_maps);
5159}
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305160#else
5161static void __init acpi_table_parse_srat_its(void) { }
5162static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005163static void __init acpi_its_srat_maps_free(void) { }
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305164#endif
5165
Keith Busch60574d12019-03-11 14:55:57 -06005166static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header,
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005167 const unsigned long end)
5168{
5169 struct acpi_madt_generic_translator *its_entry;
5170 struct fwnode_handle *dom_handle;
5171 struct resource res;
5172 int err;
5173
5174 its_entry = (struct acpi_madt_generic_translator *)header;
5175 memset(&res, 0, sizeof(res));
5176 res.start = its_entry->base_address;
5177 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
5178 res.flags = IORESOURCE_MEM;
5179
Marc Zyngier5778cc72019-07-31 16:13:42 +01005180 dom_handle = irq_domain_alloc_fwnode(&res.start);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005181 if (!dom_handle) {
5182 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
5183 &res.start);
5184 return -ENOMEM;
5185 }
5186
Shameer Kolothum8b4282e2018-02-13 15:20:50 +00005187 err = iort_register_domain_token(its_entry->translation_id, res.start,
5188 dom_handle);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005189 if (err) {
5190 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
5191 &res.start, its_entry->translation_id);
5192 goto dom_err;
5193 }
5194
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305195 err = its_probe_one(&res, dom_handle,
5196 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005197 if (!err)
5198 return 0;
5199
5200 iort_deregister_domain_token(its_entry->translation_id);
5201dom_err:
5202 irq_domain_free_fwnode(dom_handle);
5203 return err;
5204}
5205
5206static void __init its_acpi_probe(void)
5207{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05305208 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005209 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
5210 gic_acpi_parse_madt_its, 0);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08005211 acpi_its_srat_maps_free();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005212}
5213#else
5214static void __init its_acpi_probe(void) { }
5215#endif
5216
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005217int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
5218 struct irq_domain *parent_domain)
5219{
5220 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00005221 struct its_node *its;
5222 bool has_v4 = false;
Marc Zyngier3c407062020-03-04 20:33:13 +00005223 bool has_v4_1 = false;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00005224 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005225
Marc Zyngier5e516842019-12-24 11:10:28 +00005226 gic_rdists = rdists;
5227
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02005228 its_parent = parent_domain;
5229 of_node = to_of_node(handle);
5230 if (of_node)
5231 its_of_probe(of_node);
5232 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02005233 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005234
5235 if (list_empty(&its_nodes)) {
5236 pr_warn("ITS: No ITS available, not enabling LPIs\n");
5237 return -ENXIO;
5238 }
5239
Marc Zyngier11e37d32018-07-27 13:38:54 +01005240 err = allocate_lpi_tables();
Marc Zyngier8fff27a2016-12-20 13:41:55 +00005241 if (err)
5242 return err;
5243
Marc Zyngier3c407062020-03-04 20:33:13 +00005244 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00005245 has_v4 |= is_v4(its);
Marc Zyngier3c407062020-03-04 20:33:13 +00005246 has_v4_1 |= is_v4_1(its);
5247 }
5248
5249 /* Don't bother with inconsistent systems */
5250 if (WARN_ON(!has_v4_1 && rdists->has_rvpeid))
5251 rdists->has_rvpeid = false;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00005252
5253 if (has_v4 & rdists->has_vlpis) {
Marc Zyngier166cba72020-03-04 20:33:15 +00005254 const struct irq_domain_ops *sgi_ops;
5255
5256 if (has_v4_1)
5257 sgi_ops = &its_sgi_domain_ops;
5258 else
5259 sgi_ops = NULL;
5260
Marc Zyngier3d63cb52016-12-20 15:31:54 +00005261 if (its_init_vpe_domain() ||
Marc Zyngier166cba72020-03-04 20:33:15 +00005262 its_init_v4(parent_domain, &its_vpe_domain_ops, sgi_ops)) {
Marc Zyngier8fff27a2016-12-20 13:41:55 +00005263 rdists->has_vlpis = false;
5264 pr_err("ITS: Disabling GICv4 support\n");
5265 }
5266 }
5267
Derek Basehoredba0bc72018-02-28 21:48:18 -08005268 register_syscore_ops(&its_syscore_ops);
5269
Marc Zyngier8fff27a2016-12-20 13:41:55 +00005270 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00005271}