irqchip/gic-v4.1: Ensure L2 vPE table is allocated at RD level
[linux-block.git] / drivers / irqchip / irq-gic-v3-its.c
CommitLineData
caab277b 1// SPDX-License-Identifier: GPL-2.0-only
cc2d3216 2/*
d7276b80 3 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
cc2d3216 4 * Author: Marc Zyngier <marc.zyngier@arm.com>
cc2d3216
MZ
5 */
6
3f010cf1 7#include <linux/acpi.h>
8d3554b8 8#include <linux/acpi_iort.h>
ffedbf0c 9#include <linux/bitfield.h>
cc2d3216
MZ
10#include <linux/bitmap.h>
11#include <linux/cpu.h>
c6e2ccb6 12#include <linux/crash_dump.h>
cc2d3216 13#include <linux/delay.h>
44bb7e24 14#include <linux/dma-iommu.h>
3fb68fae 15#include <linux/efi.h>
cc2d3216 16#include <linux/interrupt.h>
3f010cf1 17#include <linux/irqdomain.h>
880cb3cd 18#include <linux/list.h>
cc2d3216 19#include <linux/log2.h>
5e2c9f9a 20#include <linux/memblock.h>
cc2d3216
MZ
21#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>
dba0bc7b 30#include <linux/syscore_ops.h>
cc2d3216 31
41a83e06 32#include <linux/irqchip.h>
cc2d3216 33#include <linux/irqchip/arm-gic-v3.h>
c808eea8 34#include <linux/irqchip/arm-gic-v4.h>
cc2d3216 35
cc2d3216
MZ
36#include <asm/cputype.h>
37#include <asm/exception.h>
38
67510cca
RR
39#include "irq-gic-common.h"
40
94100970
RR
41#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
42#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
fbf8f40e 43#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
dba0bc7b 44#define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3)
cc2d3216 45
c48ed51c 46#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
c440a9d9 47#define RDIST_FLAGS_RD_TABLES_PREALLOCATED (1 << 1)
c48ed51c 48
a13b0404
MZ
49static 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
2130b789 60#define LPI_PROP_DEFAULT_PRIO GICD_INT_DEF_PRI
a13b0404 61
cc2d3216
MZ
62/*
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
466b7d16 72/*
9347359a
SD
73 * The ITS_BASER structure - contains memory information, cached
74 * value of BASER register configuration and ITS page size.
466b7d16
SD
75 */
76struct its_baser {
77 void *base;
78 u64 val;
79 u32 order;
9347359a 80 u32 psz;
466b7d16
SD
81};
82
558b0165
AB
83struct its_device;
84
cc2d3216
MZ
85/*
86 * The ITS structure - contains most of the infrastructure, with the
841514ab
MZ
87 * top-level MSI domain, the command queue, the collections, and the
88 * list of devices writing to it.
9791ec7d
MZ
89 *
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.
cc2d3216
MZ
93 */
94struct its_node {
95 raw_spinlock_t lock;
9791ec7d 96 struct mutex dev_alloc_lock;
cc2d3216 97 struct list_head entry;
cc2d3216 98 void __iomem *base;
db40f0a7 99 phys_addr_t phys_base;
cc2d3216
MZ
100 struct its_cmd_block *cmd_base;
101 struct its_cmd_block *cmd_write;
466b7d16 102 struct its_baser tables[GITS_BASER_NR_REGS];
cc2d3216 103 struct its_collection *collections;
558b0165
AB
104 struct fwnode_handle *fwnode_handle;
105 u64 (*get_msi_base)(struct its_device *its_dev);
0dd57fed 106 u64 typer;
dba0bc7b
DB
107 u64 cbaser_save;
108 u32 ctlr_save;
5e516846 109 u32 mpidr;
cc2d3216
MZ
110 struct list_head its_device_list;
111 u64 flags;
debf6d02 112 unsigned long list_nr;
fbf8f40e 113 int numa_node;
558b0165
AB
114 unsigned int msi_domain_flags;
115 u32 pre_its_base; /* for Socionext Synquacer */
5c9a882e 116 int vlpi_redist_offset;
cc2d3216
MZ
117};
118
0dd57fed 119#define is_v4(its) (!!((its)->typer & GITS_TYPER_VLPIS))
5e516846 120#define is_v4_1(its) (!!((its)->typer & GITS_TYPER_VMAPP))
576a8342 121#define device_ids(its) (FIELD_GET(GITS_TYPER_DEVBITS, (its)->typer) + 1)
0dd57fed 122
cc2d3216
MZ
123#define ITS_ITT_ALIGN SZ_256
124
32bd44dc 125/* The maximum number of VPEID bits supported by VLPI commands */
f2d83409
MZ
126#define ITS_MAX_VPEID_BITS \
127 ({ \
128 int nvpeid = 16; \
129 if (gic_rdists->has_rvpeid && \
130 gic_rdists->gicd_typer2 & GICD_TYPER2_VIL) \
131 nvpeid = 1 + (gic_rdists->gicd_typer2 & \
132 GICD_TYPER2_VID); \
133 \
134 nvpeid; \
135 })
32bd44dc
SD
136#define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
137
2eca0d6c
SD
138/* Convert page order to size in bytes */
139#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
140
591e5bec
MZ
141struct event_lpi_map {
142 unsigned long *lpi_map;
143 u16 *col_map;
144 irq_hw_number_t lpi_base;
145 int nr_lpis;
11635fa2 146 raw_spinlock_t vlpi_lock;
d011e4e6
MZ
147 struct its_vm *vm;
148 struct its_vlpi_map *vlpi_maps;
149 int nr_vlpis;
591e5bec
MZ
150};
151
cc2d3216 152/*
d011e4e6
MZ
153 * The ITS view of a device - belongs to an ITS, owns an interrupt
154 * translation table, and a list of interrupts. If it some of its
155 * LPIs are injected into a guest (GICv4), the event_map.vm field
156 * indicates which one.
cc2d3216
MZ
157 */
158struct its_device {
159 struct list_head entry;
160 struct its_node *its;
591e5bec 161 struct event_lpi_map event_map;
cc2d3216 162 void *itt;
cc2d3216
MZ
163 u32 nr_ites;
164 u32 device_id;
9791ec7d 165 bool shared;
cc2d3216
MZ
166};
167
20b3d54e
MZ
168static struct {
169 raw_spinlock_t lock;
170 struct its_device *dev;
171 struct its_vpe **vpes;
172 int next_victim;
173} vpe_proxy;
174
1ac19ca6 175static LIST_HEAD(its_nodes);
a8db7456 176static DEFINE_RAW_SPINLOCK(its_lock);
1ac19ca6 177static struct rdists *gic_rdists;
db40f0a7 178static struct irq_domain *its_parent;
1ac19ca6 179
3dfa576b 180static unsigned long its_list_map;
3171a47a
MZ
181static u16 vmovp_seq_num;
182static DEFINE_RAW_SPINLOCK(vmovp_lock);
183
7d75bbb4 184static DEFINE_IDA(its_vpeid_ida);
3dfa576b 185
1ac19ca6 186#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
11e37d35 187#define gic_data_rdist_cpu(cpu) (per_cpu_ptr(gic_rdists->rdist, cpu))
1ac19ca6 188#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
e643d803 189#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
1ac19ca6 190
84243125
ZY
191static u16 get_its_list(struct its_vm *vm)
192{
193 struct its_node *its;
194 unsigned long its_list = 0;
195
196 list_for_each_entry(its, &its_nodes, entry) {
0dd57fed 197 if (!is_v4(its))
84243125
ZY
198 continue;
199
200 if (vm->vlpi_count[its->list_nr])
201 __set_bit(its->list_nr, &its_list);
202 }
203
204 return (u16)its_list;
205}
206
425c09be
MZ
207static inline u32 its_get_event_id(struct irq_data *d)
208{
209 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
210 return d->hwirq - its_dev->event_map.lpi_base;
211}
212
591e5bec
MZ
213static struct its_collection *dev_event_to_col(struct its_device *its_dev,
214 u32 event)
215{
216 struct its_node *its = its_dev->its;
217
218 return its->collections + its_dev->event_map.col_map[event];
219}
220
c1d4d5cd
MZ
221static struct its_vlpi_map *dev_event_to_vlpi_map(struct its_device *its_dev,
222 u32 event)
223{
224 if (WARN_ON_ONCE(event >= its_dev->event_map.nr_lpis))
225 return NULL;
226
227 return &its_dev->event_map.vlpi_maps[event];
228}
229
f4a81f5a
MZ
230static struct its_vlpi_map *get_vlpi_map(struct irq_data *d)
231{
232 if (irqd_is_forwarded_to_vcpu(d)) {
233 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
234 u32 event = its_get_event_id(d);
235
236 return dev_event_to_vlpi_map(its_dev, event);
237 }
238
239 return NULL;
240}
241
242static int irq_to_cpuid(struct irq_data *d)
425c09be
MZ
243{
244 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
f4a81f5a
MZ
245 struct its_vlpi_map *map = get_vlpi_map(d);
246
247 if (map)
248 return map->vpe->col_idx;
425c09be 249
f4a81f5a 250 return its_dev->event_map.col_map[its_get_event_id(d)];
425c09be
MZ
251}
252
83559b47
MZ
253static struct its_collection *valid_col(struct its_collection *col)
254{
20faba84 255 if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(15, 0)))
83559b47
MZ
256 return NULL;
257
258 return col;
259}
260
205e065d
MZ
261static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
262{
263 if (valid_col(its->collections + vpe->col_idx))
264 return vpe;
265
266 return NULL;
267}
268
cc2d3216
MZ
269/*
270 * ITS command descriptors - parameters to be encoded in a command
271 * block.
272 */
273struct its_cmd_desc {
274 union {
275 struct {
276 struct its_device *dev;
277 u32 event_id;
278 } its_inv_cmd;
279
8d85dced
MZ
280 struct {
281 struct its_device *dev;
282 u32 event_id;
283 } its_clear_cmd;
284
cc2d3216
MZ
285 struct {
286 struct its_device *dev;
287 u32 event_id;
288 } its_int_cmd;
289
290 struct {
291 struct its_device *dev;
292 int valid;
293 } its_mapd_cmd;
294
295 struct {
296 struct its_collection *col;
297 int valid;
298 } its_mapc_cmd;
299
300 struct {
301 struct its_device *dev;
302 u32 phys_id;
303 u32 event_id;
6a25ad3a 304 } its_mapti_cmd;
cc2d3216
MZ
305
306 struct {
307 struct its_device *dev;
308 struct its_collection *col;
591e5bec 309 u32 event_id;
cc2d3216
MZ
310 } its_movi_cmd;
311
312 struct {
313 struct its_device *dev;
314 u32 event_id;
315 } its_discard_cmd;
316
317 struct {
318 struct its_collection *col;
319 } its_invall_cmd;
d011e4e6 320
eb78192b
MZ
321 struct {
322 struct its_vpe *vpe;
323 } its_vinvall_cmd;
324
325 struct {
326 struct its_vpe *vpe;
327 struct its_collection *col;
328 bool valid;
329 } its_vmapp_cmd;
330
d011e4e6
MZ
331 struct {
332 struct its_vpe *vpe;
333 struct its_device *dev;
334 u32 virt_id;
335 u32 event_id;
336 bool db_enabled;
337 } its_vmapti_cmd;
338
339 struct {
340 struct its_vpe *vpe;
341 struct its_device *dev;
342 u32 event_id;
343 bool db_enabled;
344 } its_vmovi_cmd;
3171a47a
MZ
345
346 struct {
347 struct its_vpe *vpe;
348 struct its_collection *col;
349 u16 seq_num;
350 u16 its_list;
351 } its_vmovp_cmd;
d97c97ba
MZ
352
353 struct {
354 struct its_vpe *vpe;
355 } its_invdb_cmd;
cc2d3216
MZ
356 };
357};
358
359/*
360 * The ITS command block, which is what the ITS actually parses.
361 */
362struct its_cmd_block {
2bbdfcc5
BDC
363 union {
364 u64 raw_cmd[4];
365 __le64 raw_cmd_le[4];
366 };
cc2d3216
MZ
367};
368
369#define ITS_CMD_QUEUE_SZ SZ_64K
370#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
371
67047f90
MZ
372typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
373 struct its_cmd_block *,
cc2d3216
MZ
374 struct its_cmd_desc *);
375
67047f90
MZ
376typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
377 struct its_cmd_block *,
d011e4e6
MZ
378 struct its_cmd_desc *);
379
4d36f136
MZ
380static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
381{
382 u64 mask = GENMASK_ULL(h, l);
383 *raw_cmd &= ~mask;
384 *raw_cmd |= (val << l) & mask;
385}
386
cc2d3216
MZ
387static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
388{
4d36f136 389 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
cc2d3216
MZ
390}
391
392static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
393{
4d36f136 394 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
cc2d3216
MZ
395}
396
397static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
398{
4d36f136 399 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
cc2d3216
MZ
400}
401
402static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
403{
4d36f136 404 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
cc2d3216
MZ
405}
406
407static void its_encode_size(struct its_cmd_block *cmd, u8 size)
408{
4d36f136 409 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
cc2d3216
MZ
410}
411
412static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
413{
30ae9610 414 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
cc2d3216
MZ
415}
416
417static void its_encode_valid(struct its_cmd_block *cmd, int valid)
418{
4d36f136 419 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
cc2d3216
MZ
420}
421
422static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
423{
30ae9610 424 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
cc2d3216
MZ
425}
426
427static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
428{
4d36f136 429 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
cc2d3216
MZ
430}
431
d011e4e6
MZ
432static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
433{
434 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
435}
436
437static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
438{
439 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
440}
441
442static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
443{
444 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
445}
446
447static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
448{
449 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
450}
451
3171a47a
MZ
452static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
453{
454 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
455}
456
457static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
458{
459 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
460}
461
eb78192b
MZ
462static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
463{
30ae9610 464 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
eb78192b
MZ
465}
466
467static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
468{
469 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
470}
471
64edfaa9
MZ
472static void its_encode_vconf_addr(struct its_cmd_block *cmd, u64 vconf_pa)
473{
474 its_mask_encode(&cmd->raw_cmd[0], vconf_pa >> 16, 51, 16);
475}
476
477static void its_encode_alloc(struct its_cmd_block *cmd, bool alloc)
478{
479 its_mask_encode(&cmd->raw_cmd[0], alloc, 8, 8);
480}
481
482static void its_encode_ptz(struct its_cmd_block *cmd, bool ptz)
483{
484 its_mask_encode(&cmd->raw_cmd[0], ptz, 9, 9);
485}
486
487static void its_encode_vmapp_default_db(struct its_cmd_block *cmd,
488 u32 vpe_db_lpi)
489{
490 its_mask_encode(&cmd->raw_cmd[1], vpe_db_lpi, 31, 0);
491}
492
dd3f050a
MZ
493static void its_encode_vmovp_default_db(struct its_cmd_block *cmd,
494 u32 vpe_db_lpi)
495{
496 its_mask_encode(&cmd->raw_cmd[3], vpe_db_lpi, 31, 0);
497}
498
499static void its_encode_db(struct its_cmd_block *cmd, bool db)
500{
501 its_mask_encode(&cmd->raw_cmd[2], db, 63, 63);
502}
503
cc2d3216
MZ
504static inline void its_fixup_cmd(struct its_cmd_block *cmd)
505{
506 /* Let's fixup BE commands */
2bbdfcc5
BDC
507 cmd->raw_cmd_le[0] = cpu_to_le64(cmd->raw_cmd[0]);
508 cmd->raw_cmd_le[1] = cpu_to_le64(cmd->raw_cmd[1]);
509 cmd->raw_cmd_le[2] = cpu_to_le64(cmd->raw_cmd[2]);
510 cmd->raw_cmd_le[3] = cpu_to_le64(cmd->raw_cmd[3]);
cc2d3216
MZ
511}
512
67047f90
MZ
513static struct its_collection *its_build_mapd_cmd(struct its_node *its,
514 struct its_cmd_block *cmd,
cc2d3216
MZ
515 struct its_cmd_desc *desc)
516{
517 unsigned long itt_addr;
c8481267 518 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
cc2d3216
MZ
519
520 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
521 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
522
523 its_encode_cmd(cmd, GITS_CMD_MAPD);
524 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
525 its_encode_size(cmd, size - 1);
526 its_encode_itt(cmd, itt_addr);
527 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
528
529 its_fixup_cmd(cmd);
530
591e5bec 531 return NULL;
cc2d3216
MZ
532}
533
67047f90
MZ
534static struct its_collection *its_build_mapc_cmd(struct its_node *its,
535 struct its_cmd_block *cmd,
cc2d3216
MZ
536 struct its_cmd_desc *desc)
537{
538 its_encode_cmd(cmd, GITS_CMD_MAPC);
539 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
540 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
541 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
542
543 its_fixup_cmd(cmd);
544
545 return desc->its_mapc_cmd.col;
546}
547
67047f90
MZ
548static struct its_collection *its_build_mapti_cmd(struct its_node *its,
549 struct its_cmd_block *cmd,
cc2d3216
MZ
550 struct its_cmd_desc *desc)
551{
591e5bec
MZ
552 struct its_collection *col;
553
6a25ad3a
MZ
554 col = dev_event_to_col(desc->its_mapti_cmd.dev,
555 desc->its_mapti_cmd.event_id);
591e5bec 556
6a25ad3a
MZ
557 its_encode_cmd(cmd, GITS_CMD_MAPTI);
558 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
559 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
560 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
591e5bec 561 its_encode_collection(cmd, col->col_id);
cc2d3216
MZ
562
563 its_fixup_cmd(cmd);
564
83559b47 565 return valid_col(col);
cc2d3216
MZ
566}
567
67047f90
MZ
568static struct its_collection *its_build_movi_cmd(struct its_node *its,
569 struct its_cmd_block *cmd,
cc2d3216
MZ
570 struct its_cmd_desc *desc)
571{
591e5bec
MZ
572 struct its_collection *col;
573
574 col = dev_event_to_col(desc->its_movi_cmd.dev,
575 desc->its_movi_cmd.event_id);
576
cc2d3216
MZ
577 its_encode_cmd(cmd, GITS_CMD_MOVI);
578 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
591e5bec 579 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
cc2d3216
MZ
580 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
581
582 its_fixup_cmd(cmd);
583
83559b47 584 return valid_col(col);
cc2d3216
MZ
585}
586
67047f90
MZ
587static struct its_collection *its_build_discard_cmd(struct its_node *its,
588 struct its_cmd_block *cmd,
cc2d3216
MZ
589 struct its_cmd_desc *desc)
590{
591e5bec
MZ
591 struct its_collection *col;
592
593 col = dev_event_to_col(desc->its_discard_cmd.dev,
594 desc->its_discard_cmd.event_id);
595
cc2d3216
MZ
596 its_encode_cmd(cmd, GITS_CMD_DISCARD);
597 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
598 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
599
600 its_fixup_cmd(cmd);
601
83559b47 602 return valid_col(col);
cc2d3216
MZ
603}
604
67047f90
MZ
605static struct its_collection *its_build_inv_cmd(struct its_node *its,
606 struct its_cmd_block *cmd,
cc2d3216
MZ
607 struct its_cmd_desc *desc)
608{
591e5bec
MZ
609 struct its_collection *col;
610
611 col = dev_event_to_col(desc->its_inv_cmd.dev,
612 desc->its_inv_cmd.event_id);
613
cc2d3216
MZ
614 its_encode_cmd(cmd, GITS_CMD_INV);
615 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
616 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
617
618 its_fixup_cmd(cmd);
619
83559b47 620 return valid_col(col);
cc2d3216
MZ
621}
622
67047f90
MZ
623static struct its_collection *its_build_int_cmd(struct its_node *its,
624 struct its_cmd_block *cmd,
8d85dced
MZ
625 struct its_cmd_desc *desc)
626{
627 struct its_collection *col;
628
629 col = dev_event_to_col(desc->its_int_cmd.dev,
630 desc->its_int_cmd.event_id);
631
632 its_encode_cmd(cmd, GITS_CMD_INT);
633 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
634 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
635
636 its_fixup_cmd(cmd);
637
83559b47 638 return valid_col(col);
8d85dced
MZ
639}
640
67047f90
MZ
641static struct its_collection *its_build_clear_cmd(struct its_node *its,
642 struct its_cmd_block *cmd,
8d85dced
MZ
643 struct its_cmd_desc *desc)
644{
645 struct its_collection *col;
646
647 col = dev_event_to_col(desc->its_clear_cmd.dev,
648 desc->its_clear_cmd.event_id);
649
650 its_encode_cmd(cmd, GITS_CMD_CLEAR);
651 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
652 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
653
654 its_fixup_cmd(cmd);
655
83559b47 656 return valid_col(col);
8d85dced
MZ
657}
658
67047f90
MZ
659static struct its_collection *its_build_invall_cmd(struct its_node *its,
660 struct its_cmd_block *cmd,
cc2d3216
MZ
661 struct its_cmd_desc *desc)
662{
663 its_encode_cmd(cmd, GITS_CMD_INVALL);
10794522 664 its_encode_collection(cmd, desc->its_invall_cmd.col->col_id);
cc2d3216
MZ
665
666 its_fixup_cmd(cmd);
667
668 return NULL;
669}
670
67047f90
MZ
671static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
672 struct its_cmd_block *cmd,
eb78192b
MZ
673 struct its_cmd_desc *desc)
674{
675 its_encode_cmd(cmd, GITS_CMD_VINVALL);
676 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
677
678 its_fixup_cmd(cmd);
679
205e065d 680 return valid_vpe(its, desc->its_vinvall_cmd.vpe);
eb78192b
MZ
681}
682
67047f90
MZ
683static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
684 struct its_cmd_block *cmd,
eb78192b
MZ
685 struct its_cmd_desc *desc)
686{
64edfaa9 687 unsigned long vpt_addr, vconf_addr;
5c9a882e 688 u64 target;
64edfaa9 689 bool alloc;
eb78192b
MZ
690
691 its_encode_cmd(cmd, GITS_CMD_VMAPP);
692 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
693 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
64edfaa9
MZ
694
695 if (!desc->its_vmapp_cmd.valid) {
696 if (is_v4_1(its)) {
697 alloc = !atomic_dec_return(&desc->its_vmapp_cmd.vpe->vmapp_count);
698 its_encode_alloc(cmd, alloc);
699 }
700
701 goto out;
702 }
703
704 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
705 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
706
5c9a882e 707 its_encode_target(cmd, target);
eb78192b
MZ
708 its_encode_vpt_addr(cmd, vpt_addr);
709 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
710
64edfaa9
MZ
711 if (!is_v4_1(its))
712 goto out;
713
714 vconf_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->its_vm->vprop_page));
715
716 alloc = !atomic_fetch_inc(&desc->its_vmapp_cmd.vpe->vmapp_count);
717
718 its_encode_alloc(cmd, alloc);
719
720 /* We can only signal PTZ when alloc==1. Why do we have two bits? */
721 its_encode_ptz(cmd, alloc);
722 its_encode_vconf_addr(cmd, vconf_addr);
723 its_encode_vmapp_default_db(cmd, desc->its_vmapp_cmd.vpe->vpe_db_lpi);
724
725out:
eb78192b
MZ
726 its_fixup_cmd(cmd);
727
205e065d 728 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
eb78192b
MZ
729}
730
67047f90
MZ
731static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
732 struct its_cmd_block *cmd,
d011e4e6
MZ
733 struct its_cmd_desc *desc)
734{
735 u32 db;
736
3858d4df 737 if (!is_v4_1(its) && desc->its_vmapti_cmd.db_enabled)
d011e4e6
MZ
738 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
739 else
740 db = 1023;
741
742 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
743 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
744 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
745 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
746 its_encode_db_phys_id(cmd, db);
747 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
748
749 its_fixup_cmd(cmd);
750
205e065d 751 return valid_vpe(its, desc->its_vmapti_cmd.vpe);
d011e4e6
MZ
752}
753
67047f90
MZ
754static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
755 struct its_cmd_block *cmd,
d011e4e6
MZ
756 struct its_cmd_desc *desc)
757{
758 u32 db;
759
3858d4df 760 if (!is_v4_1(its) && desc->its_vmovi_cmd.db_enabled)
d011e4e6
MZ
761 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
762 else
763 db = 1023;
764
765 its_encode_cmd(cmd, GITS_CMD_VMOVI);
766 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
767 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
768 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
769 its_encode_db_phys_id(cmd, db);
770 its_encode_db_valid(cmd, true);
771
772 its_fixup_cmd(cmd);
773
205e065d 774 return valid_vpe(its, desc->its_vmovi_cmd.vpe);
d011e4e6
MZ
775}
776
67047f90
MZ
777static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
778 struct its_cmd_block *cmd,
3171a47a
MZ
779 struct its_cmd_desc *desc)
780{
5c9a882e
MZ
781 u64 target;
782
783 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
3171a47a
MZ
784 its_encode_cmd(cmd, GITS_CMD_VMOVP);
785 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
786 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
787 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
5c9a882e 788 its_encode_target(cmd, target);
3171a47a 789
dd3f050a
MZ
790 if (is_v4_1(its)) {
791 its_encode_db(cmd, true);
792 its_encode_vmovp_default_db(cmd, desc->its_vmovp_cmd.vpe->vpe_db_lpi);
793 }
794
3171a47a
MZ
795 its_fixup_cmd(cmd);
796
205e065d 797 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
3171a47a
MZ
798}
799
28614696
MZ
800static struct its_vpe *its_build_vinv_cmd(struct its_node *its,
801 struct its_cmd_block *cmd,
802 struct its_cmd_desc *desc)
803{
804 struct its_vlpi_map *map;
805
806 map = dev_event_to_vlpi_map(desc->its_inv_cmd.dev,
807 desc->its_inv_cmd.event_id);
808
809 its_encode_cmd(cmd, GITS_CMD_INV);
810 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
811 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
812
813 its_fixup_cmd(cmd);
814
815 return valid_vpe(its, map->vpe);
816}
817
ed0e4aa9
MZ
818static struct its_vpe *its_build_vint_cmd(struct its_node *its,
819 struct its_cmd_block *cmd,
820 struct its_cmd_desc *desc)
821{
822 struct its_vlpi_map *map;
823
824 map = dev_event_to_vlpi_map(desc->its_int_cmd.dev,
825 desc->its_int_cmd.event_id);
826
827 its_encode_cmd(cmd, GITS_CMD_INT);
828 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
829 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
830
831 its_fixup_cmd(cmd);
832
833 return valid_vpe(its, map->vpe);
834}
835
836static struct its_vpe *its_build_vclear_cmd(struct its_node *its,
837 struct its_cmd_block *cmd,
838 struct its_cmd_desc *desc)
839{
840 struct its_vlpi_map *map;
841
842 map = dev_event_to_vlpi_map(desc->its_clear_cmd.dev,
843 desc->its_clear_cmd.event_id);
844
845 its_encode_cmd(cmd, GITS_CMD_CLEAR);
846 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
847 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
848
849 its_fixup_cmd(cmd);
850
851 return valid_vpe(its, map->vpe);
852}
853
d97c97ba
MZ
854static struct its_vpe *its_build_invdb_cmd(struct its_node *its,
855 struct its_cmd_block *cmd,
856 struct its_cmd_desc *desc)
857{
858 if (WARN_ON(!is_v4_1(its)))
859 return NULL;
860
861 its_encode_cmd(cmd, GITS_CMD_INVDB);
862 its_encode_vpeid(cmd, desc->its_invdb_cmd.vpe->vpe_id);
863
864 its_fixup_cmd(cmd);
865
866 return valid_vpe(its, desc->its_invdb_cmd.vpe);
867}
868
cc2d3216
MZ
869static u64 its_cmd_ptr_to_offset(struct its_node *its,
870 struct its_cmd_block *ptr)
871{
872 return (ptr - its->cmd_base) * sizeof(*ptr);
873}
874
875static int its_queue_full(struct its_node *its)
876{
877 int widx;
878 int ridx;
879
880 widx = its->cmd_write - its->cmd_base;
881 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
882
883 /* This is incredibly unlikely to happen, unless the ITS locks up. */
884 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
885 return 1;
886
887 return 0;
888}
889
890static struct its_cmd_block *its_allocate_entry(struct its_node *its)
891{
892 struct its_cmd_block *cmd;
893 u32 count = 1000000; /* 1s! */
894
895 while (its_queue_full(its)) {
896 count--;
897 if (!count) {
898 pr_err_ratelimited("ITS queue not draining\n");
899 return NULL;
900 }
901 cpu_relax();
902 udelay(1);
903 }
904
905 cmd = its->cmd_write++;
906
907 /* Handle queue wrapping */
908 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
909 its->cmd_write = its->cmd_base;
910
34d677a9
MZ
911 /* Clear command */
912 cmd->raw_cmd[0] = 0;
913 cmd->raw_cmd[1] = 0;
914 cmd->raw_cmd[2] = 0;
915 cmd->raw_cmd[3] = 0;
916
cc2d3216
MZ
917 return cmd;
918}
919
920static struct its_cmd_block *its_post_commands(struct its_node *its)
921{
922 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
923
924 writel_relaxed(wr, its->base + GITS_CWRITER);
925
926 return its->cmd_write;
927}
928
929static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
930{
931 /*
932 * Make sure the commands written to memory are observable by
933 * the ITS.
934 */
935 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
328191c0 936 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
cc2d3216
MZ
937 else
938 dsb(ishst);
939}
940
a19b462f 941static int its_wait_for_range_completion(struct its_node *its,
a050fa54 942 u64 prev_idx,
a19b462f 943 struct its_cmd_block *to)
cc2d3216 944{
a050fa54 945 u64 rd_idx, to_idx, linear_idx;
cc2d3216
MZ
946 u32 count = 1000000; /* 1s! */
947
a050fa54 948 /* Linearize to_idx if the command set has wrapped around */
cc2d3216 949 to_idx = its_cmd_ptr_to_offset(its, to);
a050fa54
HG
950 if (to_idx < prev_idx)
951 to_idx += ITS_CMD_QUEUE_SZ;
952
953 linear_idx = prev_idx;
cc2d3216
MZ
954
955 while (1) {
a050fa54
HG
956 s64 delta;
957
cc2d3216 958 rd_idx = readl_relaxed(its->base + GITS_CREADR);
9bdd8b1c 959
a050fa54
HG
960 /*
961 * Compute the read pointer progress, taking the
962 * potential wrap-around into account.
963 */
964 delta = rd_idx - prev_idx;
965 if (rd_idx < prev_idx)
966 delta += ITS_CMD_QUEUE_SZ;
9bdd8b1c 967
a050fa54
HG
968 linear_idx += delta;
969 if (linear_idx >= to_idx)
cc2d3216
MZ
970 break;
971
972 count--;
973 if (!count) {
a050fa54
HG
974 pr_err_ratelimited("ITS queue timeout (%llu %llu)\n",
975 to_idx, linear_idx);
a19b462f 976 return -1;
cc2d3216 977 }
a050fa54 978 prev_idx = rd_idx;
cc2d3216
MZ
979 cpu_relax();
980 udelay(1);
981 }
a19b462f
MZ
982
983 return 0;
cc2d3216
MZ
984}
985
e4f9094b
MZ
986/* Warning, macro hell follows */
987#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
988void name(struct its_node *its, \
989 buildtype builder, \
990 struct its_cmd_desc *desc) \
991{ \
992 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
993 synctype *sync_obj; \
994 unsigned long flags; \
a050fa54 995 u64 rd_idx; \
e4f9094b
MZ
996 \
997 raw_spin_lock_irqsave(&its->lock, flags); \
998 \
999 cmd = its_allocate_entry(its); \
1000 if (!cmd) { /* We're soooooo screewed... */ \
1001 raw_spin_unlock_irqrestore(&its->lock, flags); \
1002 return; \
1003 } \
67047f90 1004 sync_obj = builder(its, cmd, desc); \
e4f9094b
MZ
1005 its_flush_cmd(its, cmd); \
1006 \
1007 if (sync_obj) { \
1008 sync_cmd = its_allocate_entry(its); \
1009 if (!sync_cmd) \
1010 goto post; \
1011 \
67047f90 1012 buildfn(its, sync_cmd, sync_obj); \
e4f9094b
MZ
1013 its_flush_cmd(its, sync_cmd); \
1014 } \
1015 \
1016post: \
a050fa54 1017 rd_idx = readl_relaxed(its->base + GITS_CREADR); \
e4f9094b
MZ
1018 next_cmd = its_post_commands(its); \
1019 raw_spin_unlock_irqrestore(&its->lock, flags); \
1020 \
a050fa54 1021 if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \
a19b462f 1022 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
e4f9094b 1023}
cc2d3216 1024
67047f90
MZ
1025static void its_build_sync_cmd(struct its_node *its,
1026 struct its_cmd_block *sync_cmd,
e4f9094b
MZ
1027 struct its_collection *sync_col)
1028{
1029 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
1030 its_encode_target(sync_cmd, sync_col->target_address);
cc2d3216 1031
e4f9094b 1032 its_fixup_cmd(sync_cmd);
cc2d3216
MZ
1033}
1034
e4f9094b
MZ
1035static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
1036 struct its_collection, its_build_sync_cmd)
1037
67047f90
MZ
1038static void its_build_vsync_cmd(struct its_node *its,
1039 struct its_cmd_block *sync_cmd,
d011e4e6
MZ
1040 struct its_vpe *sync_vpe)
1041{
1042 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
1043 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
1044
1045 its_fixup_cmd(sync_cmd);
1046}
1047
1048static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
1049 struct its_vpe, its_build_vsync_cmd)
1050
8d85dced 1051static void its_send_int(struct its_device *dev, u32 event_id)
cc2d3216 1052{
8d85dced 1053 struct its_cmd_desc desc;
cc2d3216 1054
8d85dced
MZ
1055 desc.its_int_cmd.dev = dev;
1056 desc.its_int_cmd.event_id = event_id;
cc2d3216 1057
8d85dced
MZ
1058 its_send_single_command(dev->its, its_build_int_cmd, &desc);
1059}
cc2d3216 1060
8d85dced
MZ
1061static void its_send_clear(struct its_device *dev, u32 event_id)
1062{
1063 struct its_cmd_desc desc;
cc2d3216 1064
8d85dced
MZ
1065 desc.its_clear_cmd.dev = dev;
1066 desc.its_clear_cmd.event_id = event_id;
cc2d3216 1067
8d85dced 1068 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
cc2d3216
MZ
1069}
1070
1071static void its_send_inv(struct its_device *dev, u32 event_id)
1072{
1073 struct its_cmd_desc desc;
1074
1075 desc.its_inv_cmd.dev = dev;
1076 desc.its_inv_cmd.event_id = event_id;
1077
1078 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
1079}
1080
1081static void its_send_mapd(struct its_device *dev, int valid)
1082{
1083 struct its_cmd_desc desc;
1084
1085 desc.its_mapd_cmd.dev = dev;
1086 desc.its_mapd_cmd.valid = !!valid;
1087
1088 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
1089}
1090
1091static void its_send_mapc(struct its_node *its, struct its_collection *col,
1092 int valid)
1093{
1094 struct its_cmd_desc desc;
1095
1096 desc.its_mapc_cmd.col = col;
1097 desc.its_mapc_cmd.valid = !!valid;
1098
1099 its_send_single_command(its, its_build_mapc_cmd, &desc);
1100}
1101
6a25ad3a 1102static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
cc2d3216
MZ
1103{
1104 struct its_cmd_desc desc;
1105
6a25ad3a
MZ
1106 desc.its_mapti_cmd.dev = dev;
1107 desc.its_mapti_cmd.phys_id = irq_id;
1108 desc.its_mapti_cmd.event_id = id;
cc2d3216 1109
6a25ad3a 1110 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
cc2d3216
MZ
1111}
1112
1113static void its_send_movi(struct its_device *dev,
1114 struct its_collection *col, u32 id)
1115{
1116 struct its_cmd_desc desc;
1117
1118 desc.its_movi_cmd.dev = dev;
1119 desc.its_movi_cmd.col = col;
591e5bec 1120 desc.its_movi_cmd.event_id = id;
cc2d3216
MZ
1121
1122 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
1123}
1124
1125static void its_send_discard(struct its_device *dev, u32 id)
1126{
1127 struct its_cmd_desc desc;
1128
1129 desc.its_discard_cmd.dev = dev;
1130 desc.its_discard_cmd.event_id = id;
1131
1132 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
1133}
1134
1135static void its_send_invall(struct its_node *its, struct its_collection *col)
1136{
1137 struct its_cmd_desc desc;
1138
1139 desc.its_invall_cmd.col = col;
1140
1141 its_send_single_command(its, its_build_invall_cmd, &desc);
1142}
c48ed51c 1143
d011e4e6
MZ
1144static void its_send_vmapti(struct its_device *dev, u32 id)
1145{
c1d4d5cd 1146 struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id);
d011e4e6
MZ
1147 struct its_cmd_desc desc;
1148
1149 desc.its_vmapti_cmd.vpe = map->vpe;
1150 desc.its_vmapti_cmd.dev = dev;
1151 desc.its_vmapti_cmd.virt_id = map->vintid;
1152 desc.its_vmapti_cmd.event_id = id;
1153 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
1154
1155 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
1156}
1157
1158static void its_send_vmovi(struct its_device *dev, u32 id)
1159{
c1d4d5cd 1160 struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id);
d011e4e6
MZ
1161 struct its_cmd_desc desc;
1162
1163 desc.its_vmovi_cmd.vpe = map->vpe;
1164 desc.its_vmovi_cmd.dev = dev;
1165 desc.its_vmovi_cmd.event_id = id;
1166 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
1167
1168 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
1169}
1170
75fd951b
MZ
1171static void its_send_vmapp(struct its_node *its,
1172 struct its_vpe *vpe, bool valid)
eb78192b
MZ
1173{
1174 struct its_cmd_desc desc;
eb78192b
MZ
1175
1176 desc.its_vmapp_cmd.vpe = vpe;
1177 desc.its_vmapp_cmd.valid = valid;
75fd951b 1178 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
eb78192b 1179
75fd951b 1180 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
eb78192b
MZ
1181}
1182
3171a47a
MZ
1183static void its_send_vmovp(struct its_vpe *vpe)
1184{
84243125 1185 struct its_cmd_desc desc = {};
3171a47a
MZ
1186 struct its_node *its;
1187 unsigned long flags;
1188 int col_id = vpe->col_idx;
1189
1190 desc.its_vmovp_cmd.vpe = vpe;
3171a47a
MZ
1191
1192 if (!its_list_map) {
1193 its = list_first_entry(&its_nodes, struct its_node, entry);
3171a47a
MZ
1194 desc.its_vmovp_cmd.col = &its->collections[col_id];
1195 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1196 return;
1197 }
1198
1199 /*
1200 * Yet another marvel of the architecture. If using the
1201 * its_list "feature", we need to make sure that all ITSs
1202 * receive all VMOVP commands in the same order. The only way
1203 * to guarantee this is to make vmovp a serialization point.
1204 *
1205 * Wall <-- Head.
1206 */
1207 raw_spin_lock_irqsave(&vmovp_lock, flags);
1208
1209 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
84243125 1210 desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm);
3171a47a
MZ
1211
1212 /* Emit VMOVPs */
1213 list_for_each_entry(its, &its_nodes, entry) {
0dd57fed 1214 if (!is_v4(its))
3171a47a
MZ
1215 continue;
1216
2247e1bf
MZ
1217 if (!vpe->its_vm->vlpi_count[its->list_nr])
1218 continue;
1219
3171a47a
MZ
1220 desc.its_vmovp_cmd.col = &its->collections[col_id];
1221 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1222 }
1223
1224 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1225}
1226
40619a2e 1227static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
eb78192b
MZ
1228{
1229 struct its_cmd_desc desc;
eb78192b
MZ
1230
1231 desc.its_vinvall_cmd.vpe = vpe;
40619a2e 1232 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
eb78192b
MZ
1233}
1234
28614696
MZ
1235static void its_send_vinv(struct its_device *dev, u32 event_id)
1236{
1237 struct its_cmd_desc desc;
1238
1239 /*
1240 * There is no real VINV command. This is just a normal INV,
1241 * with a VSYNC instead of a SYNC.
1242 */
1243 desc.its_inv_cmd.dev = dev;
1244 desc.its_inv_cmd.event_id = event_id;
1245
1246 its_send_single_vcommand(dev->its, its_build_vinv_cmd, &desc);
1247}
1248
ed0e4aa9
MZ
1249static void its_send_vint(struct its_device *dev, u32 event_id)
1250{
1251 struct its_cmd_desc desc;
1252
1253 /*
1254 * There is no real VINT command. This is just a normal INT,
1255 * with a VSYNC instead of a SYNC.
1256 */
1257 desc.its_int_cmd.dev = dev;
1258 desc.its_int_cmd.event_id = event_id;
1259
1260 its_send_single_vcommand(dev->its, its_build_vint_cmd, &desc);
1261}
1262
1263static void its_send_vclear(struct its_device *dev, u32 event_id)
1264{
1265 struct its_cmd_desc desc;
1266
1267 /*
1268 * There is no real VCLEAR command. This is just a normal CLEAR,
1269 * with a VSYNC instead of a SYNC.
1270 */
1271 desc.its_clear_cmd.dev = dev;
1272 desc.its_clear_cmd.event_id = event_id;
1273
1274 its_send_single_vcommand(dev->its, its_build_vclear_cmd, &desc);
1275}
1276
d97c97ba
MZ
1277static void its_send_invdb(struct its_node *its, struct its_vpe *vpe)
1278{
1279 struct its_cmd_desc desc;
1280
1281 desc.its_invdb_cmd.vpe = vpe;
1282 its_send_single_vcommand(its, its_build_invdb_cmd, &desc);
1283}
1284
c48ed51c
MZ
1285/*
1286 * irqchip functions - assumes MSI, mostly.
1287 */
015ec038 1288static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
c48ed51c 1289{
c1d4d5cd 1290 struct its_vlpi_map *map = get_vlpi_map(d);
015ec038 1291 irq_hw_number_t hwirq;
e1a2e201 1292 void *va;
adcdb94e 1293 u8 *cfg;
c48ed51c 1294
c1d4d5cd
MZ
1295 if (map) {
1296 va = page_address(map->vm->vprop_page);
d4d7b4ad
MZ
1297 hwirq = map->vintid;
1298
1299 /* Remember the updated property */
1300 map->properties &= ~clr;
1301 map->properties |= set | LPI_PROP_GROUP1;
015ec038 1302 } else {
e1a2e201 1303 va = gic_rdists->prop_table_va;
015ec038
MZ
1304 hwirq = d->hwirq;
1305 }
adcdb94e 1306
e1a2e201 1307 cfg = va + hwirq - 8192;
adcdb94e 1308 *cfg &= ~clr;
015ec038 1309 *cfg |= set | LPI_PROP_GROUP1;
c48ed51c
MZ
1310
1311 /*
1312 * Make the above write visible to the redistributors.
1313 * And yes, we're flushing exactly: One. Single. Byte.
1314 * Humpf...
1315 */
1316 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
328191c0 1317 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
c48ed51c
MZ
1318 else
1319 dsb(ishst);
015ec038
MZ
1320}
1321
2f4f064b
MZ
1322static void wait_for_syncr(void __iomem *rdbase)
1323{
1324 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
1325 cpu_relax();
1326}
1327
425c09be
MZ
1328static void direct_lpi_inv(struct irq_data *d)
1329{
f4a81f5a 1330 struct its_vlpi_map *map = get_vlpi_map(d);
425c09be 1331 void __iomem *rdbase;
f4a81f5a
MZ
1332 u64 val;
1333
1334 if (map) {
1335 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1336
1337 WARN_ON(!is_v4_1(its_dev->its));
1338
1339 val = GICR_INVLPIR_V;
1340 val |= FIELD_PREP(GICR_INVLPIR_VPEID, map->vpe->vpe_id);
1341 val |= FIELD_PREP(GICR_INVLPIR_INTID, map->vintid);
1342 } else {
1343 val = d->hwirq;
1344 }
425c09be
MZ
1345
1346 /* Target the redistributor this LPI is currently routed to */
f4a81f5a
MZ
1347 rdbase = per_cpu_ptr(gic_rdists->rdist, irq_to_cpuid(d))->rd_base;
1348 gic_write_lpir(val, rdbase + GICR_INVLPIR);
425c09be
MZ
1349
1350 wait_for_syncr(rdbase);
1351}
1352
015ec038
MZ
1353static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1354{
1355 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1356
1357 lpi_write_config(d, clr, set);
f4a81f5a
MZ
1358 if (gic_rdists->has_direct_lpi &&
1359 (is_v4_1(its_dev->its) || !irqd_is_forwarded_to_vcpu(d)))
425c09be 1360 direct_lpi_inv(d);
28614696 1361 else if (!irqd_is_forwarded_to_vcpu(d))
425c09be 1362 its_send_inv(its_dev, its_get_event_id(d));
28614696
MZ
1363 else
1364 its_send_vinv(its_dev, its_get_event_id(d));
c48ed51c
MZ
1365}
1366
015ec038
MZ
1367static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1368{
1369 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1370 u32 event = its_get_event_id(d);
c1d4d5cd 1371 struct its_vlpi_map *map;
015ec038 1372
3858d4df
MZ
1373 /*
1374 * GICv4.1 does away with the per-LPI nonsense, nothing to do
1375 * here.
1376 */
1377 if (is_v4_1(its_dev->its))
1378 return;
1379
c1d4d5cd
MZ
1380 map = dev_event_to_vlpi_map(its_dev, event);
1381
1382 if (map->db_enabled == enable)
015ec038
MZ
1383 return;
1384
c1d4d5cd 1385 map->db_enabled = enable;
015ec038
MZ
1386
1387 /*
1388 * More fun with the architecture:
1389 *
1390 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1391 * value or to 1023, depending on the enable bit. But that
1392 * would be issueing a mapping for an /existing/ DevID+EventID
1393 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1394 * to the /same/ vPE, using this opportunity to adjust the
1395 * doorbell. Mouahahahaha. We loves it, Precious.
1396 */
1397 its_send_vmovi(its_dev, event);
c48ed51c
MZ
1398}
1399
1400static void its_mask_irq(struct irq_data *d)
1401{
015ec038
MZ
1402 if (irqd_is_forwarded_to_vcpu(d))
1403 its_vlpi_set_doorbell(d, false);
1404
adcdb94e 1405 lpi_update_config(d, LPI_PROP_ENABLED, 0);
c48ed51c
MZ
1406}
1407
1408static void its_unmask_irq(struct irq_data *d)
1409{
015ec038
MZ
1410 if (irqd_is_forwarded_to_vcpu(d))
1411 its_vlpi_set_doorbell(d, true);
1412
adcdb94e 1413 lpi_update_config(d, 0, LPI_PROP_ENABLED);
c48ed51c
MZ
1414}
1415
c48ed51c
MZ
1416static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1417 bool force)
1418{
fbf8f40e
GK
1419 unsigned int cpu;
1420 const struct cpumask *cpu_mask = cpu_online_mask;
c48ed51c
MZ
1421 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1422 struct its_collection *target_col;
1423 u32 id = its_get_event_id(d);
1424
015ec038
MZ
1425 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1426 if (irqd_is_forwarded_to_vcpu(d))
1427 return -EINVAL;
1428
fbf8f40e
GK
1429 /* lpi cannot be routed to a redistributor that is on a foreign node */
1430 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1431 if (its_dev->its->numa_node >= 0) {
1432 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1433 if (!cpumask_intersects(mask_val, cpu_mask))
1434 return -EINVAL;
1435 }
1436 }
1437
1438 cpu = cpumask_any_and(mask_val, cpu_mask);
1439
c48ed51c
MZ
1440 if (cpu >= nr_cpu_ids)
1441 return -EINVAL;
1442
8b8d94a7
M
1443 /* don't set the affinity when the target cpu is same as current one */
1444 if (cpu != its_dev->event_map.col_map[id]) {
1445 target_col = &its_dev->its->collections[cpu];
1446 its_send_movi(its_dev, target_col, id);
1447 its_dev->event_map.col_map[id] = cpu;
0d224d35 1448 irq_data_update_effective_affinity(d, cpumask_of(cpu));
8b8d94a7 1449 }
c48ed51c
MZ
1450
1451 return IRQ_SET_MASK_OK_DONE;
1452}
1453
558b0165
AB
1454static u64 its_irq_get_msi_base(struct its_device *its_dev)
1455{
1456 struct its_node *its = its_dev->its;
1457
1458 return its->phys_base + GITS_TRANSLATER;
1459}
1460
b48ac83d
MZ
1461static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1462{
1463 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1464 struct its_node *its;
1465 u64 addr;
1466
1467 its = its_dev->its;
558b0165 1468 addr = its->get_msi_base(its_dev);
b48ac83d 1469
b11283eb
VM
1470 msg->address_lo = lower_32_bits(addr);
1471 msg->address_hi = upper_32_bits(addr);
b48ac83d 1472 msg->data = its_get_event_id(d);
44bb7e24 1473
35ae7df2 1474 iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg);
b48ac83d
MZ
1475}
1476
8d85dced
MZ
1477static int its_irq_set_irqchip_state(struct irq_data *d,
1478 enum irqchip_irq_state which,
1479 bool state)
1480{
1481 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1482 u32 event = its_get_event_id(d);
1483
1484 if (which != IRQCHIP_STATE_PENDING)
1485 return -EINVAL;
1486
ed0e4aa9
MZ
1487 if (irqd_is_forwarded_to_vcpu(d)) {
1488 if (state)
1489 its_send_vint(its_dev, event);
1490 else
1491 its_send_vclear(its_dev, event);
1492 } else {
1493 if (state)
1494 its_send_int(its_dev, event);
1495 else
1496 its_send_clear(its_dev, event);
1497 }
8d85dced
MZ
1498
1499 return 0;
1500}
1501
2247e1bf
MZ
1502static void its_map_vm(struct its_node *its, struct its_vm *vm)
1503{
1504 unsigned long flags;
1505
1506 /* Not using the ITS list? Everything is always mapped. */
1507 if (!its_list_map)
1508 return;
1509
1510 raw_spin_lock_irqsave(&vmovp_lock, flags);
1511
1512 /*
1513 * If the VM wasn't mapped yet, iterate over the vpes and get
1514 * them mapped now.
1515 */
1516 vm->vlpi_count[its->list_nr]++;
1517
1518 if (vm->vlpi_count[its->list_nr] == 1) {
1519 int i;
1520
1521 for (i = 0; i < vm->nr_vpes; i++) {
1522 struct its_vpe *vpe = vm->vpes[i];
44c4c25e 1523 struct irq_data *d = irq_get_irq_data(vpe->irq);
2247e1bf
MZ
1524
1525 /* Map the VPE to the first possible CPU */
1526 vpe->col_idx = cpumask_first(cpu_online_mask);
1527 its_send_vmapp(its, vpe, true);
1528 its_send_vinvall(its, vpe);
44c4c25e 1529 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
2247e1bf
MZ
1530 }
1531 }
1532
1533 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1534}
1535
1536static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1537{
1538 unsigned long flags;
1539
1540 /* Not using the ITS list? Everything is always mapped. */
1541 if (!its_list_map)
1542 return;
1543
1544 raw_spin_lock_irqsave(&vmovp_lock, flags);
1545
1546 if (!--vm->vlpi_count[its->list_nr]) {
1547 int i;
1548
1549 for (i = 0; i < vm->nr_vpes; i++)
1550 its_send_vmapp(its, vm->vpes[i], false);
1551 }
1552
1553 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1554}
1555
d011e4e6
MZ
1556static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1557{
1558 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1559 u32 event = its_get_event_id(d);
1560 int ret = 0;
1561
1562 if (!info->map)
1563 return -EINVAL;
1564
11635fa2 1565 raw_spin_lock(&its_dev->event_map.vlpi_lock);
d011e4e6
MZ
1566
1567 if (!its_dev->event_map.vm) {
1568 struct its_vlpi_map *maps;
1569
6396bb22 1570 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
11635fa2 1571 GFP_ATOMIC);
d011e4e6
MZ
1572 if (!maps) {
1573 ret = -ENOMEM;
1574 goto out;
1575 }
1576
1577 its_dev->event_map.vm = info->map->vm;
1578 its_dev->event_map.vlpi_maps = maps;
1579 } else if (its_dev->event_map.vm != info->map->vm) {
1580 ret = -EINVAL;
1581 goto out;
1582 }
1583
1584 /* Get our private copy of the mapping information */
1585 its_dev->event_map.vlpi_maps[event] = *info->map;
1586
1587 if (irqd_is_forwarded_to_vcpu(d)) {
1588 /* Already mapped, move it around */
1589 its_send_vmovi(its_dev, event);
1590 } else {
2247e1bf
MZ
1591 /* Ensure all the VPEs are mapped on this ITS */
1592 its_map_vm(its_dev->its, info->map->vm);
1593
d4d7b4ad
MZ
1594 /*
1595 * Flag the interrupt as forwarded so that we can
1596 * start poking the virtual property table.
1597 */
1598 irqd_set_forwarded_to_vcpu(d);
1599
1600 /* Write out the property to the prop table */
1601 lpi_write_config(d, 0xff, info->map->properties);
1602
d011e4e6
MZ
1603 /* Drop the physical mapping */
1604 its_send_discard(its_dev, event);
1605
1606 /* and install the virtual one */
1607 its_send_vmapti(its_dev, event);
d011e4e6
MZ
1608
1609 /* Increment the number of VLPIs */
1610 its_dev->event_map.nr_vlpis++;
1611 }
1612
1613out:
11635fa2 1614 raw_spin_unlock(&its_dev->event_map.vlpi_lock);
d011e4e6
MZ
1615 return ret;
1616}
1617
1618static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1619{
1620 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
046b5054 1621 struct its_vlpi_map *map;
d011e4e6
MZ
1622 int ret = 0;
1623
11635fa2 1624 raw_spin_lock(&its_dev->event_map.vlpi_lock);
d011e4e6 1625
046b5054
MZ
1626 map = get_vlpi_map(d);
1627
1628 if (!its_dev->event_map.vm || !map) {
d011e4e6
MZ
1629 ret = -EINVAL;
1630 goto out;
1631 }
1632
1633 /* Copy our mapping information to the incoming request */
c1d4d5cd 1634 *info->map = *map;
d011e4e6
MZ
1635
1636out:
11635fa2 1637 raw_spin_unlock(&its_dev->event_map.vlpi_lock);
d011e4e6
MZ
1638 return ret;
1639}
1640
1641static int its_vlpi_unmap(struct irq_data *d)
1642{
1643 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1644 u32 event = its_get_event_id(d);
1645 int ret = 0;
1646
11635fa2 1647 raw_spin_lock(&its_dev->event_map.vlpi_lock);
d011e4e6
MZ
1648
1649 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1650 ret = -EINVAL;
1651 goto out;
1652 }
1653
1654 /* Drop the virtual mapping */
1655 its_send_discard(its_dev, event);
1656
1657 /* and restore the physical one */
1658 irqd_clr_forwarded_to_vcpu(d);
1659 its_send_mapti(its_dev, d->hwirq, event);
1660 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1661 LPI_PROP_ENABLED |
1662 LPI_PROP_GROUP1));
1663
2247e1bf
MZ
1664 /* Potentially unmap the VM from this ITS */
1665 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1666
d011e4e6
MZ
1667 /*
1668 * Drop the refcount and make the device available again if
1669 * this was the last VLPI.
1670 */
1671 if (!--its_dev->event_map.nr_vlpis) {
1672 its_dev->event_map.vm = NULL;
1673 kfree(its_dev->event_map.vlpi_maps);
1674 }
1675
1676out:
11635fa2 1677 raw_spin_unlock(&its_dev->event_map.vlpi_lock);
d011e4e6
MZ
1678 return ret;
1679}
1680
015ec038
MZ
1681static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1682{
1683 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1684
1685 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1686 return -EINVAL;
1687
1688 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1689 lpi_update_config(d, 0xff, info->config);
1690 else
1691 lpi_write_config(d, 0xff, info->config);
1692 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1693
1694 return 0;
1695}
1696
c808eea8
MZ
1697static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1698{
1699 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1700 struct its_cmd_info *info = vcpu_info;
1701
1702 /* Need a v4 ITS */
0dd57fed 1703 if (!is_v4(its_dev->its))
c808eea8
MZ
1704 return -EINVAL;
1705
d011e4e6
MZ
1706 /* Unmap request? */
1707 if (!info)
1708 return its_vlpi_unmap(d);
1709
c808eea8
MZ
1710 switch (info->cmd_type) {
1711 case MAP_VLPI:
d011e4e6 1712 return its_vlpi_map(d, info);
c808eea8
MZ
1713
1714 case GET_VLPI:
d011e4e6 1715 return its_vlpi_get(d, info);
c808eea8
MZ
1716
1717 case PROP_UPDATE_VLPI:
1718 case PROP_UPDATE_AND_INV_VLPI:
015ec038 1719 return its_vlpi_prop_update(d, info);
c808eea8
MZ
1720
1721 default:
1722 return -EINVAL;
1723 }
1724}
1725
c48ed51c
MZ
1726static struct irq_chip its_irq_chip = {
1727 .name = "ITS",
1728 .irq_mask = its_mask_irq,
1729 .irq_unmask = its_unmask_irq,
004fa08d 1730 .irq_eoi = irq_chip_eoi_parent,
c48ed51c 1731 .irq_set_affinity = its_set_affinity,
b48ac83d 1732 .irq_compose_msi_msg = its_irq_compose_msi_msg,
8d85dced 1733 .irq_set_irqchip_state = its_irq_set_irqchip_state,
c808eea8 1734 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
b48ac83d
MZ
1735};
1736
880cb3cd 1737
bf9529f8
MZ
1738/*
1739 * How we allocate LPIs:
1740 *
880cb3cd
MZ
1741 * lpi_range_list contains ranges of LPIs that are to available to
1742 * allocate from. To allocate LPIs, just pick the first range that
1743 * fits the required allocation, and reduce it by the required
1744 * amount. Once empty, remove the range from the list.
1745 *
1746 * To free a range of LPIs, add a free range to the list, sort it and
1747 * merge the result if the new range happens to be adjacent to an
1748 * already free block.
bf9529f8 1749 *
880cb3cd
MZ
1750 * The consequence of the above is that allocation is cost is low, but
1751 * freeing is expensive. We assumes that freeing rarely occurs.
1752 */
4cb205c0 1753#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
880cb3cd 1754
880cb3cd
MZ
1755static DEFINE_MUTEX(lpi_range_lock);
1756static LIST_HEAD(lpi_range_list);
1757
1758struct lpi_range {
1759 struct list_head entry;
1760 u32 base_id;
1761 u32 span;
1762};
bf9529f8 1763
880cb3cd 1764static struct lpi_range *mk_lpi_range(u32 base, u32 span)
bf9529f8 1765{
880cb3cd
MZ
1766 struct lpi_range *range;
1767
1c73fac5 1768 range = kmalloc(sizeof(*range), GFP_KERNEL);
880cb3cd 1769 if (range) {
880cb3cd
MZ
1770 range->base_id = base;
1771 range->span = span;
1772 }
1773
1774 return range;
bf9529f8
MZ
1775}
1776
880cb3cd
MZ
1777static int alloc_lpi_range(u32 nr_lpis, u32 *base)
1778{
1779 struct lpi_range *range, *tmp;
1780 int err = -ENOSPC;
1781
1782 mutex_lock(&lpi_range_lock);
1783
1784 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1785 if (range->span >= nr_lpis) {
1786 *base = range->base_id;
1787 range->base_id += nr_lpis;
1788 range->span -= nr_lpis;
1789
1790 if (range->span == 0) {
1791 list_del(&range->entry);
1792 kfree(range);
1793 }
1794
1795 err = 0;
1796 break;
1797 }
1798 }
1799
1800 mutex_unlock(&lpi_range_lock);
1801
1802 pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
1803 return err;
bf9529f8
MZ
1804}
1805
12eade12
RV
1806static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b)
1807{
1808 if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list)
1809 return;
1810 if (a->base_id + a->span != b->base_id)
1811 return;
1812 b->base_id = a->base_id;
1813 b->span += a->span;
1814 list_del(&a->entry);
1815 kfree(a);
1816}
1817
880cb3cd 1818static int free_lpi_range(u32 base, u32 nr_lpis)
bf9529f8 1819{
12eade12 1820 struct lpi_range *new, *old;
880cb3cd
MZ
1821
1822 new = mk_lpi_range(base, nr_lpis);
b31a3838
RV
1823 if (!new)
1824 return -ENOMEM;
880cb3cd
MZ
1825
1826 mutex_lock(&lpi_range_lock);
1827
12eade12
RV
1828 list_for_each_entry_reverse(old, &lpi_range_list, entry) {
1829 if (old->base_id < base)
1830 break;
880cb3cd 1831 }
12eade12
RV
1832 /*
1833 * old is the last element with ->base_id smaller than base,
1834 * so new goes right after it. If there are no elements with
1835 * ->base_id smaller than base, &old->entry ends up pointing
1836 * at the head of the list, and inserting new it the start of
1837 * the list is the right thing to do in that case as well.
1838 */
1839 list_add(&new->entry, &old->entry);
1840 /*
1841 * Now check if we can merge with the preceding and/or
1842 * following ranges.
1843 */
1844 merge_lpi_ranges(old, new);
1845 merge_lpi_ranges(new, list_next_entry(new, entry));
880cb3cd 1846
880cb3cd 1847 mutex_unlock(&lpi_range_lock);
b31a3838 1848 return 0;
880cb3cd
MZ
1849}
1850
1851static int __init its_lpi_init(u32 id_bits)
1852{
1853 u32 lpis = (1UL << id_bits) - 8192;
12b2905a 1854 u32 numlpis;
880cb3cd
MZ
1855 int err;
1856
12b2905a
MZ
1857 numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
1858
1859 if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
1860 lpis = numlpis;
1861 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
1862 lpis);
1863 }
1864
880cb3cd
MZ
1865 /*
1866 * Initializing the allocator is just the same as freeing the
1867 * full range of LPIs.
1868 */
1869 err = free_lpi_range(8192, lpis);
1870 pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
1871 return err;
1872}
bf9529f8 1873
38dd7c49 1874static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
880cb3cd
MZ
1875{
1876 unsigned long *bitmap = NULL;
1877 int err = 0;
bf9529f8
MZ
1878
1879 do {
38dd7c49 1880 err = alloc_lpi_range(nr_irqs, base);
880cb3cd 1881 if (!err)
bf9529f8
MZ
1882 break;
1883
38dd7c49
MZ
1884 nr_irqs /= 2;
1885 } while (nr_irqs > 0);
bf9529f8 1886
45725e0f
MZ
1887 if (!nr_irqs)
1888 err = -ENOSPC;
1889
880cb3cd 1890 if (err)
bf9529f8
MZ
1891 goto out;
1892
38dd7c49 1893 bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC);
bf9529f8
MZ
1894 if (!bitmap)
1895 goto out;
1896
38dd7c49 1897 *nr_ids = nr_irqs;
bf9529f8
MZ
1898
1899out:
c8415b94
MZ
1900 if (!bitmap)
1901 *base = *nr_ids = 0;
1902
bf9529f8
MZ
1903 return bitmap;
1904}
1905
38dd7c49 1906static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
bf9529f8 1907{
880cb3cd 1908 WARN_ON(free_lpi_range(base, nr_ids));
cf2be8ba 1909 kfree(bitmap);
bf9529f8 1910}
1ac19ca6 1911
053be485
MZ
1912static void gic_reset_prop_table(void *va)
1913{
1914 /* Priority 0xa0, Group-1, disabled */
1915 memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ);
1916
1917 /* Make sure the GIC will observe the written configuration */
1918 gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ);
1919}
1920
0e5ccf91
MZ
1921static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1922{
1923 struct page *prop_page;
1ac19ca6 1924
0e5ccf91
MZ
1925 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1926 if (!prop_page)
1927 return NULL;
1928
053be485 1929 gic_reset_prop_table(page_address(prop_page));
0e5ccf91
MZ
1930
1931 return prop_page;
1932}
1933
7d75bbb4
MZ
1934static void its_free_prop_table(struct page *prop_page)
1935{
1936 free_pages((unsigned long)page_address(prop_page),
1937 get_order(LPI_PROPBASE_SZ));
1938}
1ac19ca6 1939
5e2c9f9a
MZ
1940static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
1941{
1942 phys_addr_t start, end, addr_end;
1943 u64 i;
1944
1945 /*
1946 * We don't bother checking for a kdump kernel as by
1947 * construction, the LPI tables are out of this kernel's
1948 * memory map.
1949 */
1950 if (is_kdump_kernel())
1951 return true;
1952
1953 addr_end = addr + size - 1;
1954
1955 for_each_reserved_mem_region(i, &start, &end) {
1956 if (addr >= start && addr_end <= end)
1957 return true;
1958 }
1959
1960 /* Not found, not a good sign... */
1961 pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n",
1962 &addr, &addr_end);
1963 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
1964 return false;
1965}
1966
3fb68fae
MZ
1967static int gic_reserve_range(phys_addr_t addr, unsigned long size)
1968{
1969 if (efi_enabled(EFI_CONFIG_TABLES))
1970 return efi_mem_reserve_persistent(addr, size);
1971
1972 return 0;
1973}
1974
11e37d35 1975static int __init its_setup_lpi_prop_table(void)
1ac19ca6 1976{
c440a9d9
MZ
1977 if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) {
1978 u64 val;
1ac19ca6 1979
c440a9d9
MZ
1980 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
1981 lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1;
1ac19ca6 1982
c440a9d9
MZ
1983 gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12);
1984 gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa,
1985 LPI_PROPBASE_SZ,
1986 MEMREMAP_WB);
1987 gic_reset_prop_table(gic_rdists->prop_table_va);
1988 } else {
1989 struct page *page;
1990
1991 lpi_id_bits = min_t(u32,
1992 GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
1993 ITS_MAX_LPI_NRBITS);
1994 page = its_allocate_prop_table(GFP_NOWAIT);
1995 if (!page) {
1996 pr_err("Failed to allocate PROPBASE\n");
1997 return -ENOMEM;
1998 }
1999
2000 gic_rdists->prop_table_pa = page_to_phys(page);
2001 gic_rdists->prop_table_va = page_address(page);
3fb68fae
MZ
2002 WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa,
2003 LPI_PROPBASE_SZ));
c440a9d9 2004 }
e1a2e201
MZ
2005
2006 pr_info("GICv3: using LPI property table @%pa\n",
2007 &gic_rdists->prop_table_pa);
1ac19ca6 2008
6c31e123 2009 return its_lpi_init(lpi_id_bits);
1ac19ca6
MZ
2010}
2011
2012static const char *its_base_type_string[] = {
2013 [GITS_BASER_TYPE_DEVICE] = "Devices",
2014 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
4f46de9d 2015 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
1ac19ca6
MZ
2016 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
2017 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
2018 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
2019 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
2020};
2021
2d81d425
SD
2022static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
2023{
2024 u32 idx = baser - its->tables;
2025
0968a619 2026 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
2d81d425
SD
2027}
2028
2029static void its_write_baser(struct its_node *its, struct its_baser *baser,
2030 u64 val)
2031{
2032 u32 idx = baser - its->tables;
2033
0968a619 2034 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
2d81d425
SD
2035 baser->val = its_read_baser(its, baser);
2036}
2037
9347359a 2038static int its_setup_baser(struct its_node *its, struct its_baser *baser,
3faf24ea
SD
2039 u64 cache, u64 shr, u32 psz, u32 order,
2040 bool indirect)
9347359a
SD
2041{
2042 u64 val = its_read_baser(its, baser);
2043 u64 esz = GITS_BASER_ENTRY_SIZE(val);
2044 u64 type = GITS_BASER_TYPE(val);
30ae9610 2045 u64 baser_phys, tmp;
9347359a 2046 u32 alloc_pages;
539d3782 2047 struct page *page;
9347359a 2048 void *base;
9347359a
SD
2049
2050retry_alloc_baser:
2051 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
2052 if (alloc_pages > GITS_BASER_PAGES_MAX) {
2053 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
2054 &its->phys_base, its_base_type_string[type],
2055 alloc_pages, GITS_BASER_PAGES_MAX);
2056 alloc_pages = GITS_BASER_PAGES_MAX;
2057 order = get_order(GITS_BASER_PAGES_MAX * psz);
2058 }
2059
539d3782
SD
2060 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order);
2061 if (!page)
9347359a
SD
2062 return -ENOMEM;
2063
539d3782 2064 base = (void *)page_address(page);
30ae9610
SD
2065 baser_phys = virt_to_phys(base);
2066
2067 /* Check if the physical address of the memory is above 48bits */
2068 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
2069
2070 /* 52bit PA is supported only when PageSize=64K */
2071 if (psz != SZ_64K) {
2072 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
2073 free_pages((unsigned long)base, order);
2074 return -ENXIO;
2075 }
2076
2077 /* Convert 52bit PA to 48bit field */
2078 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
2079 }
2080
9347359a 2081retry_baser:
30ae9610 2082 val = (baser_phys |
9347359a
SD
2083 (type << GITS_BASER_TYPE_SHIFT) |
2084 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
2085 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
2086 cache |
2087 shr |
2088 GITS_BASER_VALID);
2089
3faf24ea
SD
2090 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
2091
9347359a
SD
2092 switch (psz) {
2093 case SZ_4K:
2094 val |= GITS_BASER_PAGE_SIZE_4K;
2095 break;
2096 case SZ_16K:
2097 val |= GITS_BASER_PAGE_SIZE_16K;
2098 break;
2099 case SZ_64K:
2100 val |= GITS_BASER_PAGE_SIZE_64K;
2101 break;
2102 }
2103
2104 its_write_baser(its, baser, val);
2105 tmp = baser->val;
2106
2107 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
2108 /*
2109 * Shareability didn't stick. Just use
2110 * whatever the read reported, which is likely
2111 * to be the only thing this redistributor
2112 * supports. If that's zero, make it
2113 * non-cacheable as well.
2114 */
2115 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
2116 if (!shr) {
2117 cache = GITS_BASER_nC;
328191c0 2118 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
9347359a
SD
2119 }
2120 goto retry_baser;
2121 }
2122
2123 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
2124 /*
2125 * Page size didn't stick. Let's try a smaller
2126 * size and retry. If we reach 4K, then
2127 * something is horribly wrong...
2128 */
2129 free_pages((unsigned long)base, order);
2130 baser->base = NULL;
2131
2132 switch (psz) {
2133 case SZ_16K:
2134 psz = SZ_4K;
2135 goto retry_alloc_baser;
2136 case SZ_64K:
2137 psz = SZ_16K;
2138 goto retry_alloc_baser;
2139 }
2140 }
2141
2142 if (val != tmp) {
b11283eb 2143 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
9347359a 2144 &its->phys_base, its_base_type_string[type],
b11283eb 2145 val, tmp);
9347359a
SD
2146 free_pages((unsigned long)base, order);
2147 return -ENXIO;
2148 }
2149
2150 baser->order = order;
2151 baser->base = base;
2152 baser->psz = psz;
3faf24ea 2153 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
9347359a 2154
3faf24ea 2155 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
d524eaa2 2156 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
9347359a
SD
2157 its_base_type_string[type],
2158 (unsigned long)virt_to_phys(base),
3faf24ea 2159 indirect ? "indirect" : "flat", (int)esz,
9347359a
SD
2160 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
2161
2162 return 0;
2163}
2164
4cacac57
MZ
2165static bool its_parse_indirect_baser(struct its_node *its,
2166 struct its_baser *baser,
32bd44dc 2167 u32 psz, u32 *order, u32 ids)
4b75c459 2168{
4cacac57
MZ
2169 u64 tmp = its_read_baser(its, baser);
2170 u64 type = GITS_BASER_TYPE(tmp);
2171 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
2fd632a0 2172 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
4b75c459 2173 u32 new_order = *order;
3faf24ea
SD
2174 bool indirect = false;
2175
2176 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
2177 if ((esz << ids) > (psz * 2)) {
2178 /*
2179 * Find out whether hw supports a single or two-level table by
2180 * table by reading bit at offset '62' after writing '1' to it.
2181 */
2182 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
2183 indirect = !!(baser->val & GITS_BASER_INDIRECT);
2184
2185 if (indirect) {
2186 /*
2187 * The size of the lvl2 table is equal to ITS page size
2188 * which is 'psz'. For computing lvl1 table size,
2189 * subtract ID bits that sparse lvl2 table from 'ids'
2190 * which is reported by ITS hardware times lvl1 table
2191 * entry size.
2192 */
d524eaa2 2193 ids -= ilog2(psz / (int)esz);
3faf24ea
SD
2194 esz = GITS_LVL1_ENTRY_SIZE;
2195 }
2196 }
4b75c459
SD
2197
2198 /*
2199 * Allocate as many entries as required to fit the
2200 * range of device IDs that the ITS can grok... The ID
2201 * space being incredibly sparse, this results in a
3faf24ea
SD
2202 * massive waste of memory if two-level device table
2203 * feature is not supported by hardware.
4b75c459
SD
2204 */
2205 new_order = max_t(u32, get_order(esz << ids), new_order);
2206 if (new_order >= MAX_ORDER) {
2207 new_order = MAX_ORDER - 1;
d524eaa2 2208 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
576a8342 2209 pr_warn("ITS@%pa: %s Table too large, reduce ids %llu->%u\n",
4cacac57 2210 &its->phys_base, its_base_type_string[type],
576a8342 2211 device_ids(its), ids);
4b75c459
SD
2212 }
2213
2214 *order = new_order;
3faf24ea
SD
2215
2216 return indirect;
4b75c459
SD
2217}
2218
5e516846
MZ
2219static u32 compute_common_aff(u64 val)
2220{
2221 u32 aff, clpiaff;
2222
2223 aff = FIELD_GET(GICR_TYPER_AFFINITY, val);
2224 clpiaff = FIELD_GET(GICR_TYPER_COMMON_LPI_AFF, val);
2225
2226 return aff & ~(GENMASK(31, 0) >> (clpiaff * 8));
2227}
2228
2229static u32 compute_its_aff(struct its_node *its)
2230{
2231 u64 val;
2232 u32 svpet;
2233
2234 /*
2235 * Reencode the ITS SVPET and MPIDR as a GICR_TYPER, and compute
2236 * the resulting affinity. We then use that to see if this match
2237 * our own affinity.
2238 */
2239 svpet = FIELD_GET(GITS_TYPER_SVPET, its->typer);
2240 val = FIELD_PREP(GICR_TYPER_COMMON_LPI_AFF, svpet);
2241 val |= FIELD_PREP(GICR_TYPER_AFFINITY, its->mpidr);
2242 return compute_common_aff(val);
2243}
2244
2245static struct its_node *find_sibling_its(struct its_node *cur_its)
2246{
2247 struct its_node *its;
2248 u32 aff;
2249
2250 if (!FIELD_GET(GITS_TYPER_SVPET, cur_its->typer))
2251 return NULL;
2252
2253 aff = compute_its_aff(cur_its);
2254
2255 list_for_each_entry(its, &its_nodes, entry) {
2256 u64 baser;
2257
2258 if (!is_v4_1(its) || its == cur_its)
2259 continue;
2260
2261 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2262 continue;
2263
2264 if (aff != compute_its_aff(its))
2265 continue;
2266
2267 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2268 baser = its->tables[2].val;
2269 if (!(baser & GITS_BASER_VALID))
2270 continue;
2271
2272 return its;
2273 }
2274
2275 return NULL;
2276}
2277
1ac19ca6
MZ
2278static void its_free_tables(struct its_node *its)
2279{
2280 int i;
2281
2282 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
1a485f4d
SD
2283 if (its->tables[i].base) {
2284 free_pages((unsigned long)its->tables[i].base,
2285 its->tables[i].order);
2286 its->tables[i].base = NULL;
1ac19ca6
MZ
2287 }
2288 }
2289}
2290
0e0b0f69 2291static int its_alloc_tables(struct its_node *its)
1ac19ca6 2292{
1ac19ca6 2293 u64 shr = GITS_BASER_InnerShareable;
2fd632a0 2294 u64 cache = GITS_BASER_RaWaWb;
9347359a
SD
2295 u32 psz = SZ_64K;
2296 int err, i;
94100970 2297
fa150019
AB
2298 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
2299 /* erratum 24313: ignore memory access type */
2300 cache = GITS_BASER_nCnB;
466b7d16 2301
1ac19ca6 2302 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2d81d425
SD
2303 struct its_baser *baser = its->tables + i;
2304 u64 val = its_read_baser(its, baser);
1ac19ca6 2305 u64 type = GITS_BASER_TYPE(val);
9347359a 2306 u32 order = get_order(psz);
3faf24ea 2307 bool indirect = false;
1ac19ca6 2308
4cacac57
MZ
2309 switch (type) {
2310 case GITS_BASER_TYPE_NONE:
1ac19ca6
MZ
2311 continue;
2312
4cacac57 2313 case GITS_BASER_TYPE_DEVICE:
32bd44dc
SD
2314 indirect = its_parse_indirect_baser(its, baser,
2315 psz, &order,
576a8342 2316 device_ids(its));
8d565748
ZY
2317 break;
2318
4cacac57 2319 case GITS_BASER_TYPE_VCPU:
5e516846
MZ
2320 if (is_v4_1(its)) {
2321 struct its_node *sibling;
2322
2323 WARN_ON(i != 2);
2324 if ((sibling = find_sibling_its(its))) {
2325 *baser = sibling->tables[2];
2326 its_write_baser(its, baser, baser->val);
2327 continue;
2328 }
2329 }
2330
4cacac57 2331 indirect = its_parse_indirect_baser(its, baser,
32bd44dc
SD
2332 psz, &order,
2333 ITS_MAX_VPEID_BITS);
4cacac57
MZ
2334 break;
2335 }
f54b97ed 2336
3faf24ea 2337 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
9347359a
SD
2338 if (err < 0) {
2339 its_free_tables(its);
2340 return err;
1ac19ca6
MZ
2341 }
2342
9347359a
SD
2343 /* Update settings which will be used for next BASERn */
2344 psz = baser->psz;
2345 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
2346 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
1ac19ca6
MZ
2347 }
2348
2349 return 0;
1ac19ca6
MZ
2350}
2351
5e516846
MZ
2352static u64 inherit_vpe_l1_table_from_its(void)
2353{
2354 struct its_node *its;
2355 u64 val;
2356 u32 aff;
2357
2358 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2359 aff = compute_common_aff(val);
2360
2361 list_for_each_entry(its, &its_nodes, entry) {
2362 u64 baser, addr;
2363
2364 if (!is_v4_1(its))
2365 continue;
2366
2367 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2368 continue;
2369
2370 if (aff != compute_its_aff(its))
2371 continue;
2372
2373 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2374 baser = its->tables[2].val;
2375 if (!(baser & GITS_BASER_VALID))
2376 continue;
2377
2378 /* We have a winner! */
8b718d40
ZY
2379 gic_data_rdist()->vpe_l1_base = its->tables[2].base;
2380
5e516846
MZ
2381 val = GICR_VPROPBASER_4_1_VALID;
2382 if (baser & GITS_BASER_INDIRECT)
2383 val |= GICR_VPROPBASER_4_1_INDIRECT;
2384 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE,
2385 FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser));
2386 switch (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser)) {
2387 case GIC_PAGE_SIZE_64K:
2388 addr = GITS_BASER_ADDR_48_to_52(baser);
2389 break;
2390 default:
2391 addr = baser & GENMASK_ULL(47, 12);
2392 break;
2393 }
2394 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, addr >> 12);
2395 val |= FIELD_PREP(GICR_VPROPBASER_SHAREABILITY_MASK,
2396 FIELD_GET(GITS_BASER_SHAREABILITY_MASK, baser));
2397 val |= FIELD_PREP(GICR_VPROPBASER_INNER_CACHEABILITY_MASK,
2398 FIELD_GET(GITS_BASER_INNER_CACHEABILITY_MASK, baser));
2399 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, GITS_BASER_NR_PAGES(baser) - 1);
2400
2401 return val;
2402 }
2403
2404 return 0;
2405}
2406
2407static u64 inherit_vpe_l1_table_from_rd(cpumask_t **mask)
2408{
2409 u32 aff;
2410 u64 val;
2411 int cpu;
2412
2413 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2414 aff = compute_common_aff(val);
2415
2416 for_each_possible_cpu(cpu) {
2417 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
2418 u32 tmp;
2419
2420 if (!base || cpu == smp_processor_id())
2421 continue;
2422
2423 val = gic_read_typer(base + GICR_TYPER);
2424 tmp = compute_common_aff(val);
2425 if (tmp != aff)
2426 continue;
2427
2428 /*
2429 * At this point, we have a victim. This particular CPU
2430 * has already booted, and has an affinity that matches
2431 * ours wrt CommonLPIAff. Let's use its own VPROPBASER.
2432 * Make sure we don't write the Z bit in that case.
2433 */
2434 val = gits_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
2435 val &= ~GICR_VPROPBASER_4_1_Z;
2436
8b718d40 2437 gic_data_rdist()->vpe_l1_base = gic_data_rdist_cpu(cpu)->vpe_l1_base;
5e516846
MZ
2438 *mask = gic_data_rdist_cpu(cpu)->vpe_table_mask;
2439
2440 return val;
2441 }
2442
2443 return 0;
2444}
2445
4e6437f1
ZY
2446static bool allocate_vpe_l2_table(int cpu, u32 id)
2447{
2448 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
2449 u64 val, gpsz, npg;
2450 unsigned int psz, esz, idx;
2451 struct page *page;
2452 __le64 *table;
2453
2454 if (!gic_rdists->has_rvpeid)
2455 return true;
2456
2457 val = gits_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
2458
2459 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val) + 1;
2460 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2461 npg = FIELD_GET(GICR_VPROPBASER_4_1_SIZE, val) + 1;
2462
2463 switch (gpsz) {
2464 default:
2465 WARN_ON(1);
2466 /* fall through */
2467 case GIC_PAGE_SIZE_4K:
2468 psz = SZ_4K;
2469 break;
2470 case GIC_PAGE_SIZE_16K:
2471 psz = SZ_16K;
2472 break;
2473 case GIC_PAGE_SIZE_64K:
2474 psz = SZ_64K;
2475 break;
2476 }
2477
2478 /* Don't allow vpe_id that exceeds single, flat table limit */
2479 if (!(val & GICR_VPROPBASER_4_1_INDIRECT))
2480 return (id < (npg * psz / (esz * SZ_8)));
2481
2482 /* Compute 1st level table index & check if that exceeds table limit */
2483 idx = id >> ilog2(psz / (esz * SZ_8));
2484 if (idx >= (npg * psz / GITS_LVL1_ENTRY_SIZE))
2485 return false;
2486
2487 table = gic_data_rdist_cpu(cpu)->vpe_l1_base;
2488
2489 /* Allocate memory for 2nd level table */
2490 if (!table[idx]) {
2491 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(psz));
2492 if (!page)
2493 return false;
2494
2495 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2496 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2497 gic_flush_dcache_to_poc(page_address(page), psz);
2498
2499 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2500
2501 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2502 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2503 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
2504
2505 /* Ensure updated table contents are visible to RD hardware */
2506 dsb(sy);
2507 }
2508
2509 return true;
2510}
2511
5e516846
MZ
2512static int allocate_vpe_l1_table(void)
2513{
2514 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2515 u64 val, gpsz, npg, pa;
2516 unsigned int psz = SZ_64K;
2517 unsigned int np, epp, esz;
2518 struct page *page;
2519
2520 if (!gic_rdists->has_rvpeid)
2521 return 0;
2522
2523 /*
2524 * if VPENDBASER.Valid is set, disable any previously programmed
2525 * VPE by setting PendingLast while clearing Valid. This has the
2526 * effect of making sure no doorbell will be generated and we can
2527 * then safely clear VPROPBASER.Valid.
2528 */
2529 if (gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER) & GICR_VPENDBASER_Valid)
2530 gits_write_vpendbaser(GICR_VPENDBASER_PendingLast,
2531 vlpi_base + GICR_VPENDBASER);
2532
2533 /*
2534 * If we can inherit the configuration from another RD, let's do
2535 * so. Otherwise, we have to go through the allocation process. We
2536 * assume that all RDs have the exact same requirements, as
2537 * nothing will work otherwise.
2538 */
2539 val = inherit_vpe_l1_table_from_rd(&gic_data_rdist()->vpe_table_mask);
2540 if (val & GICR_VPROPBASER_4_1_VALID)
2541 goto out;
2542
2543 gic_data_rdist()->vpe_table_mask = kzalloc(sizeof(cpumask_t), GFP_KERNEL);
2544 if (!gic_data_rdist()->vpe_table_mask)
2545 return -ENOMEM;
2546
2547 val = inherit_vpe_l1_table_from_its();
2548 if (val & GICR_VPROPBASER_4_1_VALID)
2549 goto out;
2550
2551 /* First probe the page size */
2552 val = FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, GIC_PAGE_SIZE_64K);
2553 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2554 val = gits_read_vpropbaser(vlpi_base + GICR_VPROPBASER);
2555 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2556 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val);
2557
2558 switch (gpsz) {
2559 default:
2560 gpsz = GIC_PAGE_SIZE_4K;
2561 /* fall through */
2562 case GIC_PAGE_SIZE_4K:
2563 psz = SZ_4K;
2564 break;
2565 case GIC_PAGE_SIZE_16K:
2566 psz = SZ_16K;
2567 break;
2568 case GIC_PAGE_SIZE_64K:
2569 psz = SZ_64K;
2570 break;
2571 }
2572
2573 /*
2574 * Start populating the register from scratch, including RO fields
2575 * (which we want to print in debug cases...)
2576 */
2577 val = 0;
2578 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, gpsz);
2579 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ENTRY_SIZE, esz);
2580
2581 /* How many entries per GIC page? */
2582 esz++;
2583 epp = psz / (esz * SZ_8);
2584
2585 /*
2586 * If we need more than just a single L1 page, flag the table
2587 * as indirect and compute the number of required L1 pages.
2588 */
2589 if (epp < ITS_MAX_VPEID) {
2590 int nl2;
2591
2592 val |= GICR_VPROPBASER_4_1_INDIRECT;
2593
2594 /* Number of L2 pages required to cover the VPEID space */
2595 nl2 = DIV_ROUND_UP(ITS_MAX_VPEID, epp);
2596
2597 /* Number of L1 pages to point to the L2 pages */
2598 npg = DIV_ROUND_UP(nl2 * SZ_8, psz);
2599 } else {
2600 npg = 1;
2601 }
2602
e88bd316 2603 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, npg - 1);
5e516846
MZ
2604
2605 /* Right, that's the number of CPU pages we need for L1 */
2606 np = DIV_ROUND_UP(npg * psz, PAGE_SIZE);
2607
2608 pr_debug("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n",
2609 np, npg, psz, epp, esz);
2610 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(np * PAGE_SIZE));
2611 if (!page)
2612 return -ENOMEM;
2613
8b718d40 2614 gic_data_rdist()->vpe_l1_base = page_address(page);
5e516846
MZ
2615 pa = virt_to_phys(page_address(page));
2616 WARN_ON(!IS_ALIGNED(pa, psz));
2617
2618 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, pa >> 12);
2619 val |= GICR_VPROPBASER_RaWb;
2620 val |= GICR_VPROPBASER_InnerShareable;
2621 val |= GICR_VPROPBASER_4_1_Z;
2622 val |= GICR_VPROPBASER_4_1_VALID;
2623
2624out:
2625 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2626 cpumask_set_cpu(smp_processor_id(), gic_data_rdist()->vpe_table_mask);
2627
2628 pr_debug("CPU%d: VPROPBASER = %llx %*pbl\n",
2629 smp_processor_id(), val,
2630 cpumask_pr_args(gic_data_rdist()->vpe_table_mask));
2631
2632 return 0;
2633}
2634
1ac19ca6
MZ
2635static int its_alloc_collections(struct its_node *its)
2636{
83559b47
MZ
2637 int i;
2638
6396bb22 2639 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
1ac19ca6
MZ
2640 GFP_KERNEL);
2641 if (!its->collections)
2642 return -ENOMEM;
2643
83559b47
MZ
2644 for (i = 0; i < nr_cpu_ids; i++)
2645 its->collections[i].target_address = ~0ULL;
2646
1ac19ca6
MZ
2647 return 0;
2648}
2649
7c297a2d
MZ
2650static struct page *its_allocate_pending_table(gfp_t gfp_flags)
2651{
2652 struct page *pend_page;
adaab500 2653
7c297a2d 2654 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
adaab500 2655 get_order(LPI_PENDBASE_SZ));
7c297a2d
MZ
2656 if (!pend_page)
2657 return NULL;
2658
2659 /* Make sure the GIC will observe the zero-ed page */
2660 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
2661
2662 return pend_page;
2663}
2664
7d75bbb4
MZ
2665static void its_free_pending_table(struct page *pt)
2666{
adaab500 2667 free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ));
7d75bbb4
MZ
2668}
2669
c6e2ccb6 2670/*
5e2c9f9a
MZ
2671 * Booting with kdump and LPIs enabled is generally fine. Any other
2672 * case is wrong in the absence of firmware/EFI support.
c6e2ccb6 2673 */
c440a9d9
MZ
2674static bool enabled_lpis_allowed(void)
2675{
5e2c9f9a
MZ
2676 phys_addr_t addr;
2677 u64 val;
c6e2ccb6 2678
5e2c9f9a
MZ
2679 /* Check whether the property table is in a reserved region */
2680 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2681 addr = val & GENMASK_ULL(51, 12);
2682
2683 return gic_check_reserved_range(addr, LPI_PROPBASE_SZ);
c440a9d9
MZ
2684}
2685
11e37d35 2686static int __init allocate_lpi_tables(void)
1ac19ca6 2687{
c440a9d9 2688 u64 val;
11e37d35 2689 int err, cpu;
1ac19ca6 2690
c440a9d9
MZ
2691 /*
2692 * If LPIs are enabled while we run this from the boot CPU,
2693 * flag the RD tables as pre-allocated if the stars do align.
2694 */
2695 val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR);
2696 if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) {
2697 gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED |
2698 RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING);
2699 pr_info("GICv3: Using preallocated redistributor tables\n");
2700 }
2701
11e37d35
MZ
2702 err = its_setup_lpi_prop_table();
2703 if (err)
2704 return err;
2705
2706 /*
2707 * We allocate all the pending tables anyway, as we may have a
2708 * mix of RDs that have had LPIs enabled, and some that
2709 * don't. We'll free the unused ones as each CPU comes online.
2710 */
2711 for_each_possible_cpu(cpu) {
2712 struct page *pend_page;
7c297a2d
MZ
2713
2714 pend_page = its_allocate_pending_table(GFP_NOWAIT);
1ac19ca6 2715 if (!pend_page) {
11e37d35
MZ
2716 pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu);
2717 return -ENOMEM;
1ac19ca6
MZ
2718 }
2719
11e37d35 2720 gic_data_rdist_cpu(cpu)->pend_page = pend_page;
1ac19ca6
MZ
2721 }
2722
11e37d35
MZ
2723 return 0;
2724}
2725
e64fab1a 2726static u64 its_clear_vpend_valid(void __iomem *vlpi_base, u64 clr, u64 set)
6479450f
HG
2727{
2728 u32 count = 1000000; /* 1s! */
2729 bool clean;
2730 u64 val;
2731
2732 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2733 val &= ~GICR_VPENDBASER_Valid;
e64fab1a
MZ
2734 val &= ~clr;
2735 val |= set;
6479450f
HG
2736 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
2737
2738 do {
2739 val = gits_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
2740 clean = !(val & GICR_VPENDBASER_Dirty);
2741 if (!clean) {
2742 count--;
2743 cpu_relax();
2744 udelay(1);
2745 }
2746 } while (!clean && count);
2747
e64fab1a
MZ
2748 if (unlikely(val & GICR_VPENDBASER_Dirty)) {
2749 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2750 val |= GICR_VPENDBASER_PendingLast;
2751 }
2752
6479450f
HG
2753 return val;
2754}
2755
11e37d35
MZ
2756static void its_cpu_init_lpis(void)
2757{
2758 void __iomem *rbase = gic_data_rdist_rd_base();
2759 struct page *pend_page;
2760 phys_addr_t paddr;
2761 u64 val, tmp;
2762
2763 if (gic_data_rdist()->lpi_enabled)
2764 return;
2765
c440a9d9
MZ
2766 val = readl_relaxed(rbase + GICR_CTLR);
2767 if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) &&
2768 (val & GICR_CTLR_ENABLE_LPIS)) {
f842ca8e
MZ
2769 /*
2770 * Check that we get the same property table on all
2771 * RDs. If we don't, this is hopeless.
2772 */
2773 paddr = gicr_read_propbaser(rbase + GICR_PROPBASER);
2774 paddr &= GENMASK_ULL(51, 12);
2775 if (WARN_ON(gic_rdists->prop_table_pa != paddr))
2776 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
2777
c440a9d9
MZ
2778 paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER);
2779 paddr &= GENMASK_ULL(51, 16);
2780
5e2c9f9a 2781 WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ));
c440a9d9
MZ
2782 its_free_pending_table(gic_data_rdist()->pend_page);
2783 gic_data_rdist()->pend_page = NULL;
2784
2785 goto out;
2786 }
2787
11e37d35
MZ
2788 pend_page = gic_data_rdist()->pend_page;
2789 paddr = page_to_phys(pend_page);
3fb68fae 2790 WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ));
11e37d35 2791
1ac19ca6 2792 /* set PROPBASE */
e1a2e201 2793 val = (gic_rdists->prop_table_pa |
1ac19ca6 2794 GICR_PROPBASER_InnerShareable |
2fd632a0 2795 GICR_PROPBASER_RaWaWb |
1ac19ca6
MZ
2796 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
2797
0968a619
VM
2798 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2799 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
1ac19ca6
MZ
2800
2801 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
241a386c
MZ
2802 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
2803 /*
2804 * The HW reports non-shareable, we must
2805 * remove the cacheability attributes as
2806 * well.
2807 */
2808 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
2809 GICR_PROPBASER_CACHEABILITY_MASK);
2810 val |= GICR_PROPBASER_nC;
0968a619 2811 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
241a386c 2812 }
1ac19ca6
MZ
2813 pr_info_once("GIC: using cache flushing for LPI property table\n");
2814 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
2815 }
2816
2817 /* set PENDBASE */
2818 val = (page_to_phys(pend_page) |
4ad3e363 2819 GICR_PENDBASER_InnerShareable |
2fd632a0 2820 GICR_PENDBASER_RaWaWb);
1ac19ca6 2821
0968a619
VM
2822 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2823 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
241a386c
MZ
2824
2825 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
2826 /*
2827 * The HW reports non-shareable, we must remove the
2828 * cacheability attributes as well.
2829 */
2830 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
2831 GICR_PENDBASER_CACHEABILITY_MASK);
2832 val |= GICR_PENDBASER_nC;
0968a619 2833 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
241a386c 2834 }
1ac19ca6
MZ
2835
2836 /* Enable LPIs */
2837 val = readl_relaxed(rbase + GICR_CTLR);
2838 val |= GICR_CTLR_ENABLE_LPIS;
2839 writel_relaxed(val, rbase + GICR_CTLR);
2840
5e516846 2841 if (gic_rdists->has_vlpis && !gic_rdists->has_rvpeid) {
6479450f
HG
2842 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2843
2844 /*
2845 * It's possible for CPU to receive VLPIs before it is
2846 * sheduled as a vPE, especially for the first CPU, and the
2847 * VLPI with INTID larger than 2^(IDbits+1) will be considered
2848 * as out of range and dropped by GIC.
2849 * So we initialize IDbits to known value to avoid VLPI drop.
2850 */
2851 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2852 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
2853 smp_processor_id(), val);
2854 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2855
2856 /*
2857 * Also clear Valid bit of GICR_VPENDBASER, in case some
2858 * ancient programming gets left in and has possibility of
2859 * corrupting memory.
2860 */
e64fab1a 2861 val = its_clear_vpend_valid(vlpi_base, 0, 0);
6479450f
HG
2862 WARN_ON(val & GICR_VPENDBASER_Dirty);
2863 }
2864
5e516846
MZ
2865 if (allocate_vpe_l1_table()) {
2866 /*
2867 * If the allocation has failed, we're in massive trouble.
2868 * Disable direct injection, and pray that no VM was
2869 * already running...
2870 */
2871 gic_rdists->has_rvpeid = false;
2872 gic_rdists->has_vlpis = false;
2873 }
2874
1ac19ca6
MZ
2875 /* Make sure the GIC has seen the above */
2876 dsb(sy);
c440a9d9 2877out:
11e37d35 2878 gic_data_rdist()->lpi_enabled = true;
c440a9d9 2879 pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n",
11e37d35 2880 smp_processor_id(),
c440a9d9 2881 gic_data_rdist()->pend_page ? "allocated" : "reserved",
11e37d35 2882 &paddr);
1ac19ca6
MZ
2883}
2884
920181ce 2885static void its_cpu_init_collection(struct its_node *its)
1ac19ca6 2886{
920181ce
DB
2887 int cpu = smp_processor_id();
2888 u64 target;
1ac19ca6 2889
920181ce
DB
2890 /* avoid cross node collections and its mapping */
2891 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
2892 struct device_node *cpu_node;
fbf8f40e 2893
920181ce
DB
2894 cpu_node = of_get_cpu_node(cpu, NULL);
2895 if (its->numa_node != NUMA_NO_NODE &&
2896 its->numa_node != of_node_to_nid(cpu_node))
2897 return;
2898 }
fbf8f40e 2899
920181ce
DB
2900 /*
2901 * We now have to bind each collection to its target
2902 * redistributor.
2903 */
2904 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
1ac19ca6 2905 /*
920181ce 2906 * This ITS wants the physical address of the
1ac19ca6
MZ
2907 * redistributor.
2908 */
920181ce
DB
2909 target = gic_data_rdist()->phys_base;
2910 } else {
2911 /* This ITS wants a linear CPU number. */
2912 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2913 target = GICR_TYPER_CPU_NUMBER(target) << 16;
2914 }
1ac19ca6 2915
920181ce
DB
2916 /* Perform collection mapping */
2917 its->collections[cpu].target_address = target;
2918 its->collections[cpu].col_id = cpu;
1ac19ca6 2919
920181ce
DB
2920 its_send_mapc(its, &its->collections[cpu], 1);
2921 its_send_invall(its, &its->collections[cpu]);
2922}
2923
2924static void its_cpu_init_collections(void)
2925{
2926 struct its_node *its;
2927
a8db7456 2928 raw_spin_lock(&its_lock);
920181ce
DB
2929
2930 list_for_each_entry(its, &its_nodes, entry)
2931 its_cpu_init_collection(its);
1ac19ca6 2932
a8db7456 2933 raw_spin_unlock(&its_lock);
1ac19ca6 2934}
84a6a2e7
MZ
2935
2936static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
2937{
2938 struct its_device *its_dev = NULL, *tmp;
3e39e8f5 2939 unsigned long flags;
84a6a2e7 2940
3e39e8f5 2941 raw_spin_lock_irqsave(&its->lock, flags);
84a6a2e7
MZ
2942
2943 list_for_each_entry(tmp, &its->its_device_list, entry) {
2944 if (tmp->device_id == dev_id) {
2945 its_dev = tmp;
2946 break;
2947 }
2948 }
2949
3e39e8f5 2950 raw_spin_unlock_irqrestore(&its->lock, flags);
84a6a2e7
MZ
2951
2952 return its_dev;
2953}
2954
466b7d16
SD
2955static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2956{
2957 int i;
2958
2959 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2960 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2961 return &its->tables[i];
2962 }
2963
2964 return NULL;
2965}
2966
539d3782
SD
2967static bool its_alloc_table_entry(struct its_node *its,
2968 struct its_baser *baser, u32 id)
3faf24ea 2969{
3faf24ea
SD
2970 struct page *page;
2971 u32 esz, idx;
2972 __le64 *table;
2973
3faf24ea
SD
2974 /* Don't allow device id that exceeds single, flat table limit */
2975 esz = GITS_BASER_ENTRY_SIZE(baser->val);
2976 if (!(baser->val & GITS_BASER_INDIRECT))
70cc81ed 2977 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
3faf24ea
SD
2978
2979 /* Compute 1st level table index & check if that exceeds table limit */
70cc81ed 2980 idx = id >> ilog2(baser->psz / esz);
3faf24ea
SD
2981 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
2982 return false;
2983
2984 table = baser->base;
2985
2986 /* Allocate memory for 2nd level table */
2987 if (!table[idx]) {
539d3782
SD
2988 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
2989 get_order(baser->psz));
3faf24ea
SD
2990 if (!page)
2991 return false;
2992
2993 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2994 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
328191c0 2995 gic_flush_dcache_to_poc(page_address(page), baser->psz);
3faf24ea
SD
2996
2997 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2998
2999 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
3000 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
328191c0 3001 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
3faf24ea
SD
3002
3003 /* Ensure updated table contents are visible to ITS hardware */
3004 dsb(sy);
3005 }
3006
3007 return true;
3008}
3009
70cc81ed
MZ
3010static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
3011{
3012 struct its_baser *baser;
3013
3014 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
3015
3016 /* Don't allow device id that exceeds ITS hardware limit */
3017 if (!baser)
576a8342 3018 return (ilog2(dev_id) < device_ids(its));
70cc81ed 3019
539d3782 3020 return its_alloc_table_entry(its, baser, dev_id);
70cc81ed
MZ
3021}
3022
7d75bbb4
MZ
3023static bool its_alloc_vpe_table(u32 vpe_id)
3024{
3025 struct its_node *its;
4e6437f1 3026 int cpu;
7d75bbb4
MZ
3027
3028 /*
3029 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
3030 * could try and only do it on ITSs corresponding to devices
3031 * that have interrupts targeted at this VPE, but the
3032 * complexity becomes crazy (and you have tons of memory
3033 * anyway, right?).
3034 */
3035 list_for_each_entry(its, &its_nodes, entry) {
3036 struct its_baser *baser;
3037
0dd57fed 3038 if (!is_v4(its))
7d75bbb4 3039 continue;
3faf24ea 3040
7d75bbb4
MZ
3041 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
3042 if (!baser)
3043 return false;
3faf24ea 3044
539d3782 3045 if (!its_alloc_table_entry(its, baser, vpe_id))
7d75bbb4 3046 return false;
3faf24ea
SD
3047 }
3048
4e6437f1
ZY
3049 /* Non v4.1? No need to iterate RDs and go back early. */
3050 if (!gic_rdists->has_rvpeid)
3051 return true;
3052
3053 /*
3054 * Make sure the L2 tables are allocated for all copies of
3055 * the L1 table on *all* v4.1 RDs.
3056 */
3057 for_each_possible_cpu(cpu) {
3058 if (!allocate_vpe_l2_table(cpu, vpe_id))
3059 return false;
3060 }
3061
3faf24ea
SD
3062 return true;
3063}
3064
84a6a2e7 3065static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
93f94ea0 3066 int nvecs, bool alloc_lpis)
84a6a2e7
MZ
3067{
3068 struct its_device *dev;
93f94ea0 3069 unsigned long *lpi_map = NULL;
3e39e8f5 3070 unsigned long flags;
591e5bec 3071 u16 *col_map = NULL;
84a6a2e7
MZ
3072 void *itt;
3073 int lpi_base;
3074 int nr_lpis;
c8481267 3075 int nr_ites;
84a6a2e7
MZ
3076 int sz;
3077
3faf24ea 3078 if (!its_alloc_device_table(its, dev_id))
466b7d16
SD
3079 return NULL;
3080
147c8f37
MZ
3081 if (WARN_ON(!is_power_of_2(nvecs)))
3082 nvecs = roundup_pow_of_two(nvecs);
3083
84a6a2e7 3084 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
c8481267 3085 /*
147c8f37
MZ
3086 * Even if the device wants a single LPI, the ITT must be
3087 * sized as a power of two (and you need at least one bit...).
c8481267 3088 */
147c8f37 3089 nr_ites = max(2, nvecs);
ffedbf0c 3090 sz = nr_ites * (FIELD_GET(GITS_TYPER_ITT_ENTRY_SIZE, its->typer) + 1);
84a6a2e7 3091 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
539d3782 3092 itt = kzalloc_node(sz, GFP_KERNEL, its->numa_node);
93f94ea0 3093 if (alloc_lpis) {
38dd7c49 3094 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
93f94ea0 3095 if (lpi_map)
6396bb22 3096 col_map = kcalloc(nr_lpis, sizeof(*col_map),
93f94ea0
MZ
3097 GFP_KERNEL);
3098 } else {
6396bb22 3099 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
93f94ea0
MZ
3100 nr_lpis = 0;
3101 lpi_base = 0;
3102 }
84a6a2e7 3103
93f94ea0 3104 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
84a6a2e7
MZ
3105 kfree(dev);
3106 kfree(itt);
3107 kfree(lpi_map);
591e5bec 3108 kfree(col_map);
84a6a2e7
MZ
3109 return NULL;
3110 }
3111
328191c0 3112 gic_flush_dcache_to_poc(itt, sz);
5a9a8915 3113
84a6a2e7
MZ
3114 dev->its = its;
3115 dev->itt = itt;
c8481267 3116 dev->nr_ites = nr_ites;
591e5bec
MZ
3117 dev->event_map.lpi_map = lpi_map;
3118 dev->event_map.col_map = col_map;
3119 dev->event_map.lpi_base = lpi_base;
3120 dev->event_map.nr_lpis = nr_lpis;
11635fa2 3121 raw_spin_lock_init(&dev->event_map.vlpi_lock);
84a6a2e7
MZ
3122 dev->device_id = dev_id;
3123 INIT_LIST_HEAD(&dev->entry);
3124
3e39e8f5 3125 raw_spin_lock_irqsave(&its->lock, flags);
84a6a2e7 3126 list_add(&dev->entry, &its->its_device_list);
3e39e8f5 3127 raw_spin_unlock_irqrestore(&its->lock, flags);
84a6a2e7 3128
84a6a2e7
MZ
3129 /* Map device to its ITT */
3130 its_send_mapd(dev, 1);
3131
3132 return dev;
3133}
3134
3135static void its_free_device(struct its_device *its_dev)
3136{
3e39e8f5
MZ
3137 unsigned long flags;
3138
3139 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
84a6a2e7 3140 list_del(&its_dev->entry);
3e39e8f5 3141 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
898aa5ce 3142 kfree(its_dev->event_map.col_map);
84a6a2e7
MZ
3143 kfree(its_dev->itt);
3144 kfree(its_dev);
3145}
b48ac83d 3146
8208d170 3147static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
b48ac83d
MZ
3148{
3149 int idx;
3150
342be106 3151 /* Find a free LPI region in lpi_map and allocate them. */
8208d170
MZ
3152 idx = bitmap_find_free_region(dev->event_map.lpi_map,
3153 dev->event_map.nr_lpis,
3154 get_count_order(nvecs));
3155 if (idx < 0)
b48ac83d
MZ
3156 return -ENOSPC;
3157
591e5bec 3158 *hwirq = dev->event_map.lpi_base + idx;
b48ac83d 3159
b48ac83d
MZ
3160 return 0;
3161}
3162
54456db9
MZ
3163static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
3164 int nvec, msi_alloc_info_t *info)
e8137f4f 3165{
b48ac83d 3166 struct its_node *its;
b48ac83d 3167 struct its_device *its_dev;
54456db9
MZ
3168 struct msi_domain_info *msi_info;
3169 u32 dev_id;
9791ec7d 3170 int err = 0;
54456db9
MZ
3171
3172 /*
a7c90f51 3173 * We ignore "dev" entirely, and rely on the dev_id that has
54456db9
MZ
3174 * been passed via the scratchpad. This limits this domain's
3175 * usefulness to upper layers that definitely know that they
3176 * are built on top of the ITS.
3177 */
3178 dev_id = info->scratchpad[0].ul;
3179
3180 msi_info = msi_get_domain_info(domain);
3181 its = msi_info->data;
e8137f4f 3182
20b3d54e
MZ
3183 if (!gic_rdists->has_direct_lpi &&
3184 vpe_proxy.dev &&
3185 vpe_proxy.dev->its == its &&
3186 dev_id == vpe_proxy.dev->device_id) {
3187 /* Bad luck. Get yourself a better implementation */
3188 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
3189 dev_id);
3190 return -EINVAL;
3191 }
3192
9791ec7d 3193 mutex_lock(&its->dev_alloc_lock);
f130420e 3194 its_dev = its_find_device(its, dev_id);
e8137f4f
MZ
3195 if (its_dev) {
3196 /*
3197 * We already have seen this ID, probably through
3198 * another alias (PCI bridge of some sort). No need to
3199 * create the device.
3200 */
9791ec7d 3201 its_dev->shared = true;
f130420e 3202 pr_debug("Reusing ITT for devID %x\n", dev_id);
e8137f4f
MZ
3203 goto out;
3204 }
b48ac83d 3205
93f94ea0 3206 its_dev = its_create_device(its, dev_id, nvec, true);
9791ec7d
MZ
3207 if (!its_dev) {
3208 err = -ENOMEM;
3209 goto out;
3210 }
b48ac83d 3211
f130420e 3212 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
e8137f4f 3213out:
9791ec7d 3214 mutex_unlock(&its->dev_alloc_lock);
b48ac83d 3215 info->scratchpad[0].ptr = its_dev;
9791ec7d 3216 return err;
b48ac83d
MZ
3217}
3218
54456db9
MZ
3219static struct msi_domain_ops its_msi_domain_ops = {
3220 .msi_prepare = its_msi_prepare,
3221};
3222
b48ac83d
MZ
3223static int its_irq_gic_domain_alloc(struct irq_domain *domain,
3224 unsigned int virq,
3225 irq_hw_number_t hwirq)
3226{
f833f57f
MZ
3227 struct irq_fwspec fwspec;
3228
3229 if (irq_domain_get_of_node(domain->parent)) {
3230 fwspec.fwnode = domain->parent->fwnode;
3231 fwspec.param_count = 3;
3232 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
3233 fwspec.param[1] = hwirq;
3234 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
3f010cf1
TN
3235 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
3236 fwspec.fwnode = domain->parent->fwnode;
3237 fwspec.param_count = 2;
3238 fwspec.param[0] = hwirq;
3239 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
f833f57f
MZ
3240 } else {
3241 return -EINVAL;
3242 }
b48ac83d 3243
f833f57f 3244 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
b48ac83d
MZ
3245}
3246
3247static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
3248 unsigned int nr_irqs, void *args)
3249{
3250 msi_alloc_info_t *info = args;
3251 struct its_device *its_dev = info->scratchpad[0].ptr;
35ae7df2 3252 struct its_node *its = its_dev->its;
b48ac83d
MZ
3253 irq_hw_number_t hwirq;
3254 int err;
3255 int i;
3256
8208d170
MZ
3257 err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
3258 if (err)
3259 return err;
b48ac83d 3260
35ae7df2
JG
3261 err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev));
3262 if (err)
3263 return err;
3264
8208d170
MZ
3265 for (i = 0; i < nr_irqs; i++) {
3266 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
b48ac83d
MZ
3267 if (err)
3268 return err;
3269
3270 irq_domain_set_hwirq_and_chip(domain, virq + i,
8208d170 3271 hwirq + i, &its_irq_chip, its_dev);
0d224d35 3272 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
f130420e 3273 pr_debug("ID:%d pID:%d vID:%d\n",
8208d170
MZ
3274 (int)(hwirq + i - its_dev->event_map.lpi_base),
3275 (int)(hwirq + i), virq + i);
b48ac83d
MZ
3276 }
3277
3278 return 0;
3279}
3280
72491643 3281static int its_irq_domain_activate(struct irq_domain *domain,
702cb0a0 3282 struct irq_data *d, bool reserve)
aca268df
MZ
3283{
3284 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3285 u32 event = its_get_event_id(d);
fbf8f40e 3286 const struct cpumask *cpu_mask = cpu_online_mask;
0d224d35 3287 int cpu;
fbf8f40e
GK
3288
3289 /* get the cpu_mask of local node */
3290 if (its_dev->its->numa_node >= 0)
3291 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
aca268df 3292
591e5bec 3293 /* Bind the LPI to the first possible CPU */
c1797b11
YY
3294 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
3295 if (cpu >= nr_cpu_ids) {
3296 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
3297 return -EINVAL;
3298
3299 cpu = cpumask_first(cpu_online_mask);
3300 }
3301
0d224d35
MZ
3302 its_dev->event_map.col_map[event] = cpu;
3303 irq_data_update_effective_affinity(d, cpumask_of(cpu));
591e5bec 3304
aca268df 3305 /* Map the GIC IRQ and event to the device */
6a25ad3a 3306 its_send_mapti(its_dev, d->hwirq, event);
72491643 3307 return 0;
aca268df
MZ
3308}
3309
3310static void its_irq_domain_deactivate(struct irq_domain *domain,
3311 struct irq_data *d)
3312{
3313 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3314 u32 event = its_get_event_id(d);
3315
3316 /* Stop the delivery of interrupts */
3317 its_send_discard(its_dev, event);
3318}
3319
b48ac83d
MZ
3320static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
3321 unsigned int nr_irqs)
3322{
3323 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
3324 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
9791ec7d 3325 struct its_node *its = its_dev->its;
b48ac83d
MZ
3326 int i;
3327
c9c96e30
MZ
3328 bitmap_release_region(its_dev->event_map.lpi_map,
3329 its_get_event_id(irq_domain_get_irq_data(domain, virq)),
3330 get_count_order(nr_irqs));
3331
b48ac83d
MZ
3332 for (i = 0; i < nr_irqs; i++) {
3333 struct irq_data *data = irq_domain_get_irq_data(domain,
3334 virq + i);
b48ac83d 3335 /* Nuke the entry in the domain */
2da39949 3336 irq_domain_reset_irq_data(data);
b48ac83d
MZ
3337 }
3338
9791ec7d
MZ
3339 mutex_lock(&its->dev_alloc_lock);
3340
3341 /*
3342 * If all interrupts have been freed, start mopping the
3343 * floor. This is conditionned on the device not being shared.
3344 */
3345 if (!its_dev->shared &&
3346 bitmap_empty(its_dev->event_map.lpi_map,
591e5bec 3347 its_dev->event_map.nr_lpis)) {
38dd7c49
MZ
3348 its_lpi_free(its_dev->event_map.lpi_map,
3349 its_dev->event_map.lpi_base,
3350 its_dev->event_map.nr_lpis);
b48ac83d
MZ
3351
3352 /* Unmap device/itt */
3353 its_send_mapd(its_dev, 0);
3354 its_free_device(its_dev);
3355 }
3356
9791ec7d
MZ
3357 mutex_unlock(&its->dev_alloc_lock);
3358
b48ac83d
MZ
3359 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3360}
3361
3362static const struct irq_domain_ops its_domain_ops = {
3363 .alloc = its_irq_domain_alloc,
3364 .free = its_irq_domain_free,
aca268df
MZ
3365 .activate = its_irq_domain_activate,
3366 .deactivate = its_irq_domain_deactivate,
b48ac83d 3367};
4c21f3c2 3368
20b3d54e
MZ
3369/*
3370 * This is insane.
3371 *
0684c704 3372 * If a GICv4.0 doesn't implement Direct LPIs (which is extremely
20b3d54e
MZ
3373 * likely), the only way to perform an invalidate is to use a fake
3374 * device to issue an INV command, implying that the LPI has first
3375 * been mapped to some event on that device. Since this is not exactly
3376 * cheap, we try to keep that mapping around as long as possible, and
3377 * only issue an UNMAP if we're short on available slots.
3378 *
3379 * Broken by design(tm).
0684c704
MZ
3380 *
3381 * GICv4.1, on the other hand, mandates that we're able to invalidate
3382 * by writing to a MMIO register. It doesn't implement the whole of
3383 * DirectLPI, but that's good enough. And most of the time, we don't
3384 * even have to invalidate anything, as the redistributor can be told
3385 * whether to generate a doorbell or not (we thus leave it enabled,
3386 * always).
20b3d54e
MZ
3387 */
3388static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
3389{
0684c704
MZ
3390 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3391 if (gic_rdists->has_rvpeid)
3392 return;
3393
20b3d54e
MZ
3394 /* Already unmapped? */
3395 if (vpe->vpe_proxy_event == -1)
3396 return;
3397
3398 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
3399 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
3400
3401 /*
3402 * We don't track empty slots at all, so let's move the
3403 * next_victim pointer if we can quickly reuse that slot
3404 * instead of nuking an existing entry. Not clear that this is
3405 * always a win though, and this might just generate a ripple
3406 * effect... Let's just hope VPEs don't migrate too often.
3407 */
3408 if (vpe_proxy.vpes[vpe_proxy.next_victim])
3409 vpe_proxy.next_victim = vpe->vpe_proxy_event;
3410
3411 vpe->vpe_proxy_event = -1;
3412}
3413
3414static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
3415{
0684c704
MZ
3416 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3417 if (gic_rdists->has_rvpeid)
3418 return;
3419
20b3d54e
MZ
3420 if (!gic_rdists->has_direct_lpi) {
3421 unsigned long flags;
3422
3423 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3424 its_vpe_db_proxy_unmap_locked(vpe);
3425 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3426 }
3427}
3428
3429static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
3430{
0684c704
MZ
3431 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3432 if (gic_rdists->has_rvpeid)
3433 return;
3434
20b3d54e
MZ
3435 /* Already mapped? */
3436 if (vpe->vpe_proxy_event != -1)
3437 return;
3438
3439 /* This slot was already allocated. Kick the other VPE out. */
3440 if (vpe_proxy.vpes[vpe_proxy.next_victim])
3441 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
3442
3443 /* Map the new VPE instead */
3444 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
3445 vpe->vpe_proxy_event = vpe_proxy.next_victim;
3446 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
3447
3448 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
3449 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
3450}
3451
958b90d1
MZ
3452static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
3453{
3454 unsigned long flags;
3455 struct its_collection *target_col;
3456
0684c704
MZ
3457 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3458 if (gic_rdists->has_rvpeid)
3459 return;
3460
958b90d1
MZ
3461 if (gic_rdists->has_direct_lpi) {
3462 void __iomem *rdbase;
3463
3464 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
3465 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2f4f064b 3466 wait_for_syncr(rdbase);
958b90d1
MZ
3467
3468 return;
3469 }
3470
3471 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3472
3473 its_vpe_db_proxy_map_locked(vpe);
3474
3475 target_col = &vpe_proxy.dev->its->collections[to];
3476 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
3477 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
3478
3479 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3480}
3481
3171a47a
MZ
3482static int its_vpe_set_affinity(struct irq_data *d,
3483 const struct cpumask *mask_val,
3484 bool force)
3485{
3486 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
dd3f050a 3487 int from, cpu = cpumask_first(mask_val);
3171a47a
MZ
3488
3489 /*
3490 * Changing affinity is mega expensive, so let's be as lazy as
20b3d54e 3491 * we can and only do it if we really have to. Also, if mapped
958b90d1
MZ
3492 * into the proxy device, we need to move the doorbell
3493 * interrupt to its new location.
3171a47a 3494 */
dd3f050a
MZ
3495 if (vpe->col_idx == cpu)
3496 goto out;
958b90d1 3497
dd3f050a
MZ
3498 from = vpe->col_idx;
3499 vpe->col_idx = cpu;
3500
3501 /*
3502 * GICv4.1 allows us to skip VMOVP if moving to a cpu whose RD
3503 * is sharing its VPE table with the current one.
3504 */
3505 if (gic_data_rdist_cpu(cpu)->vpe_table_mask &&
3506 cpumask_test_cpu(from, gic_data_rdist_cpu(cpu)->vpe_table_mask))
3507 goto out;
3171a47a 3508
dd3f050a
MZ
3509 its_send_vmovp(vpe);
3510 its_vpe_db_proxy_move(vpe, from, cpu);
3511
3512out:
44c4c25e
MZ
3513 irq_data_update_effective_affinity(d, cpumask_of(cpu));
3514
3171a47a
MZ
3515 return IRQ_SET_MASK_OK_DONE;
3516}
3517
e643d803
MZ
3518static void its_vpe_schedule(struct its_vpe *vpe)
3519{
50c33097 3520 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
e643d803
MZ
3521 u64 val;
3522
3523 /* Schedule the VPE */
3524 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
3525 GENMASK_ULL(51, 12);
3526 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
3527 val |= GICR_VPROPBASER_RaWb;
3528 val |= GICR_VPROPBASER_InnerShareable;
3529 gits_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
3530
3531 val = virt_to_phys(page_address(vpe->vpt_page)) &
3532 GENMASK_ULL(51, 16);
3533 val |= GICR_VPENDBASER_RaWaWb;
3534 val |= GICR_VPENDBASER_NonShareable;
3535 /*
3536 * There is no good way of finding out if the pending table is
3537 * empty as we can race against the doorbell interrupt very
3538 * easily. So in the end, vpe->pending_last is only an
3539 * indication that the vcpu has something pending, not one
3540 * that the pending table is empty. A good implementation
3541 * would be able to read its coarse map pretty quickly anyway,
3542 * making this a tolerable issue.
3543 */
3544 val |= GICR_VPENDBASER_PendingLast;
3545 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
3546 val |= GICR_VPENDBASER_Valid;
3547 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
3548}
3549
3550static void its_vpe_deschedule(struct its_vpe *vpe)
3551{
50c33097 3552 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
e643d803
MZ
3553 u64 val;
3554
e64fab1a 3555 val = its_clear_vpend_valid(vlpi_base, 0, 0);
e643d803 3556
e64fab1a
MZ
3557 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
3558 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
e643d803
MZ
3559}
3560
40619a2e
MZ
3561static void its_vpe_invall(struct its_vpe *vpe)
3562{
3563 struct its_node *its;
3564
3565 list_for_each_entry(its, &its_nodes, entry) {
0dd57fed 3566 if (!is_v4(its))
40619a2e
MZ
3567 continue;
3568
2247e1bf
MZ
3569 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
3570 continue;
3571
3c1cceeb
MZ
3572 /*
3573 * Sending a VINVALL to a single ITS is enough, as all
3574 * we need is to reach the redistributors.
3575 */
40619a2e 3576 its_send_vinvall(its, vpe);
3c1cceeb 3577 return;
40619a2e
MZ
3578 }
3579}
3580
e643d803
MZ
3581static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
3582{
3583 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3584 struct its_cmd_info *info = vcpu_info;
3585
3586 switch (info->cmd_type) {
3587 case SCHEDULE_VPE:
3588 its_vpe_schedule(vpe);
3589 return 0;
3590
3591 case DESCHEDULE_VPE:
3592 its_vpe_deschedule(vpe);
3593 return 0;
3594
5e2f7642 3595 case INVALL_VPE:
40619a2e 3596 its_vpe_invall(vpe);
5e2f7642
MZ
3597 return 0;
3598
e643d803
MZ
3599 default:
3600 return -EINVAL;
3601 }
3602}
3603
20b3d54e
MZ
3604static void its_vpe_send_cmd(struct its_vpe *vpe,
3605 void (*cmd)(struct its_device *, u32))
3606{
3607 unsigned long flags;
3608
3609 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3610
3611 its_vpe_db_proxy_map_locked(vpe);
3612 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
3613
3614 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3615}
3616
f6a91da7
MZ
3617static void its_vpe_send_inv(struct irq_data *d)
3618{
3619 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
f6a91da7 3620
20b3d54e
MZ
3621 if (gic_rdists->has_direct_lpi) {
3622 void __iomem *rdbase;
3623
425c09be 3624 /* Target the redistributor this VPE is currently known on */
20b3d54e 3625 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
425c09be 3626 gic_write_lpir(d->parent_data->hwirq, rdbase + GICR_INVLPIR);
2f4f064b 3627 wait_for_syncr(rdbase);
20b3d54e
MZ
3628 } else {
3629 its_vpe_send_cmd(vpe, its_send_inv);
3630 }
f6a91da7
MZ
3631}
3632
3633static void its_vpe_mask_irq(struct irq_data *d)
3634{
3635 /*
3636 * We need to unmask the LPI, which is described by the parent
3637 * irq_data. Instead of calling into the parent (which won't
3638 * exactly do the right thing, let's simply use the
3639 * parent_data pointer. Yes, I'm naughty.
3640 */
3641 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
3642 its_vpe_send_inv(d);
3643}
3644
3645static void its_vpe_unmask_irq(struct irq_data *d)
3646{
3647 /* Same hack as above... */
3648 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
3649 its_vpe_send_inv(d);
3650}
3651
e57a3e28
MZ
3652static int its_vpe_set_irqchip_state(struct irq_data *d,
3653 enum irqchip_irq_state which,
3654 bool state)
3655{
3656 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3657
3658 if (which != IRQCHIP_STATE_PENDING)
3659 return -EINVAL;
3660
3661 if (gic_rdists->has_direct_lpi) {
3662 void __iomem *rdbase;
3663
3664 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
3665 if (state) {
3666 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
3667 } else {
3668 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
2f4f064b 3669 wait_for_syncr(rdbase);
e57a3e28
MZ
3670 }
3671 } else {
3672 if (state)
3673 its_vpe_send_cmd(vpe, its_send_int);
3674 else
3675 its_vpe_send_cmd(vpe, its_send_clear);
3676 }
3677
3678 return 0;
3679}
3680
8fff27ae
MZ
3681static struct irq_chip its_vpe_irq_chip = {
3682 .name = "GICv4-vpe",
f6a91da7
MZ
3683 .irq_mask = its_vpe_mask_irq,
3684 .irq_unmask = its_vpe_unmask_irq,
3685 .irq_eoi = irq_chip_eoi_parent,
3171a47a 3686 .irq_set_affinity = its_vpe_set_affinity,
e57a3e28 3687 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
e643d803 3688 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
8fff27ae
MZ
3689};
3690
d97c97ba
MZ
3691static struct its_node *find_4_1_its(void)
3692{
3693 static struct its_node *its = NULL;
3694
3695 if (!its) {
3696 list_for_each_entry(its, &its_nodes, entry) {
3697 if (is_v4_1(its))
3698 return its;
3699 }
3700
3701 /* Oops? */
3702 its = NULL;
3703 }
3704
3705 return its;
3706}
3707
3708static void its_vpe_4_1_send_inv(struct irq_data *d)
3709{
3710 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3711 struct its_node *its;
3712
3713 /*
3714 * GICv4.1 wants doorbells to be invalidated using the
3715 * INVDB command in order to be broadcast to all RDs. Send
3716 * it to the first valid ITS, and let the HW do its magic.
3717 */
3718 its = find_4_1_its();
3719 if (its)
3720 its_send_invdb(its, vpe);
3721}
3722
3723static void its_vpe_4_1_mask_irq(struct irq_data *d)
3724{
3725 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
3726 its_vpe_4_1_send_inv(d);
3727}
3728
3729static void its_vpe_4_1_unmask_irq(struct irq_data *d)
3730{
3731 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
3732 its_vpe_4_1_send_inv(d);
3733}
3734
91bf6395
MZ
3735static void its_vpe_4_1_schedule(struct its_vpe *vpe,
3736 struct its_cmd_info *info)
3737{
3738 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3739 u64 val = 0;
3740
3741 /* Schedule the VPE */
3742 val |= GICR_VPENDBASER_Valid;
3743 val |= info->g0en ? GICR_VPENDBASER_4_1_VGRP0EN : 0;
3744 val |= info->g1en ? GICR_VPENDBASER_4_1_VGRP1EN : 0;
3745 val |= FIELD_PREP(GICR_VPENDBASER_4_1_VPEID, vpe->vpe_id);
3746
3747 gits_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
3748}
3749
e64fab1a
MZ
3750static void its_vpe_4_1_deschedule(struct its_vpe *vpe,
3751 struct its_cmd_info *info)
3752{
3753 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3754 u64 val;
3755
3756 if (info->req_db) {
3757 /*
3758 * vPE is going to block: make the vPE non-resident with
3759 * PendingLast clear and DB set. The GIC guarantees that if
3760 * we read-back PendingLast clear, then a doorbell will be
3761 * delivered when an interrupt comes.
3762 */
3763 val = its_clear_vpend_valid(vlpi_base,
3764 GICR_VPENDBASER_PendingLast,
3765 GICR_VPENDBASER_4_1_DB);
3766 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
3767 } else {
3768 /*
3769 * We're not blocking, so just make the vPE non-resident
3770 * with PendingLast set, indicating that we'll be back.
3771 */
3772 val = its_clear_vpend_valid(vlpi_base,
3773 0,
3774 GICR_VPENDBASER_PendingLast);
3775 vpe->pending_last = true;
3776 }
3777}
3778
b4a4bd0f
MZ
3779static void its_vpe_4_1_invall(struct its_vpe *vpe)
3780{
3781 void __iomem *rdbase;
3782 u64 val;
3783
3784 val = GICR_INVALLR_V;
3785 val |= FIELD_PREP(GICR_INVALLR_VPEID, vpe->vpe_id);
3786
3787 /* Target the redistributor this vPE is currently known on */
3788 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
3789 gic_write_lpir(val, rdbase + GICR_INVALLR);
3790}
3791
29c647f3
MZ
3792static int its_vpe_4_1_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
3793{
91bf6395 3794 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
29c647f3
MZ
3795 struct its_cmd_info *info = vcpu_info;
3796
3797 switch (info->cmd_type) {
3798 case SCHEDULE_VPE:
91bf6395 3799 its_vpe_4_1_schedule(vpe, info);
29c647f3
MZ
3800 return 0;
3801
3802 case DESCHEDULE_VPE:
e64fab1a 3803 its_vpe_4_1_deschedule(vpe, info);
29c647f3
MZ
3804 return 0;
3805
3806 case INVALL_VPE:
b4a4bd0f 3807 its_vpe_4_1_invall(vpe);
29c647f3
MZ
3808 return 0;
3809
3810 default:
3811 return -EINVAL;
3812 }
3813}
3814
3815static struct irq_chip its_vpe_4_1_irq_chip = {
3816 .name = "GICv4.1-vpe",
d97c97ba
MZ
3817 .irq_mask = its_vpe_4_1_mask_irq,
3818 .irq_unmask = its_vpe_4_1_unmask_irq,
29c647f3
MZ
3819 .irq_eoi = irq_chip_eoi_parent,
3820 .irq_set_affinity = its_vpe_set_affinity,
3821 .irq_set_vcpu_affinity = its_vpe_4_1_set_vcpu_affinity,
3822};
3823
7d75bbb4
MZ
3824static int its_vpe_id_alloc(void)
3825{
32bd44dc 3826 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
7d75bbb4
MZ
3827}
3828
3829static void its_vpe_id_free(u16 id)
3830{
3831 ida_simple_remove(&its_vpeid_ida, id);
3832}
3833
3834static int its_vpe_init(struct its_vpe *vpe)
3835{
3836 struct page *vpt_page;
3837 int vpe_id;
3838
3839 /* Allocate vpe_id */
3840 vpe_id = its_vpe_id_alloc();
3841 if (vpe_id < 0)
3842 return vpe_id;
3843
3844 /* Allocate VPT */
3845 vpt_page = its_allocate_pending_table(GFP_KERNEL);
3846 if (!vpt_page) {
3847 its_vpe_id_free(vpe_id);
3848 return -ENOMEM;
3849 }
3850
3851 if (!its_alloc_vpe_table(vpe_id)) {
3852 its_vpe_id_free(vpe_id);
34f8eb92 3853 its_free_pending_table(vpt_page);
7d75bbb4
MZ
3854 return -ENOMEM;
3855 }
3856
3857 vpe->vpe_id = vpe_id;
3858 vpe->vpt_page = vpt_page;
64edfaa9
MZ
3859 if (gic_rdists->has_rvpeid)
3860 atomic_set(&vpe->vmapp_count, 0);
3861 else
3862 vpe->vpe_proxy_event = -1;
7d75bbb4
MZ
3863
3864 return 0;
3865}
3866
3867static void its_vpe_teardown(struct its_vpe *vpe)
3868{
20b3d54e 3869 its_vpe_db_proxy_unmap(vpe);
7d75bbb4
MZ
3870 its_vpe_id_free(vpe->vpe_id);
3871 its_free_pending_table(vpe->vpt_page);
3872}
3873
3874static void its_vpe_irq_domain_free(struct irq_domain *domain,
3875 unsigned int virq,
3876 unsigned int nr_irqs)
3877{
3878 struct its_vm *vm = domain->host_data;
3879 int i;
3880
3881 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3882
3883 for (i = 0; i < nr_irqs; i++) {
3884 struct irq_data *data = irq_domain_get_irq_data(domain,
3885 virq + i);
3886 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
3887
3888 BUG_ON(vm != vpe->its_vm);
3889
3890 clear_bit(data->hwirq, vm->db_bitmap);
3891 its_vpe_teardown(vpe);
3892 irq_domain_reset_irq_data(data);
3893 }
3894
3895 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
38dd7c49 3896 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
7d75bbb4
MZ
3897 its_free_prop_table(vm->vprop_page);
3898 }
3899}
3900
3901static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
3902 unsigned int nr_irqs, void *args)
3903{
29c647f3 3904 struct irq_chip *irqchip = &its_vpe_irq_chip;
7d75bbb4
MZ
3905 struct its_vm *vm = args;
3906 unsigned long *bitmap;
3907 struct page *vprop_page;
3908 int base, nr_ids, i, err = 0;
3909
3910 BUG_ON(!vm);
3911
38dd7c49 3912 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
7d75bbb4
MZ
3913 if (!bitmap)
3914 return -ENOMEM;
3915
3916 if (nr_ids < nr_irqs) {
38dd7c49 3917 its_lpi_free(bitmap, base, nr_ids);
7d75bbb4
MZ
3918 return -ENOMEM;
3919 }
3920
3921 vprop_page = its_allocate_prop_table(GFP_KERNEL);
3922 if (!vprop_page) {
38dd7c49 3923 its_lpi_free(bitmap, base, nr_ids);
7d75bbb4
MZ
3924 return -ENOMEM;
3925 }
3926
3927 vm->db_bitmap = bitmap;
3928 vm->db_lpi_base = base;
3929 vm->nr_db_lpis = nr_ids;
3930 vm->vprop_page = vprop_page;
3931
29c647f3
MZ
3932 if (gic_rdists->has_rvpeid)
3933 irqchip = &its_vpe_4_1_irq_chip;
3934
7d75bbb4
MZ
3935 for (i = 0; i < nr_irqs; i++) {
3936 vm->vpes[i]->vpe_db_lpi = base + i;
3937 err = its_vpe_init(vm->vpes[i]);
3938 if (err)
3939 break;
3940 err = its_irq_gic_domain_alloc(domain, virq + i,
3941 vm->vpes[i]->vpe_db_lpi);
3942 if (err)
3943 break;
3944 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
29c647f3 3945 irqchip, vm->vpes[i]);
7d75bbb4
MZ
3946 set_bit(i, bitmap);
3947 }
3948
3949 if (err) {
3950 if (i > 0)
3951 its_vpe_irq_domain_free(domain, virq, i - 1);
3952
38dd7c49 3953 its_lpi_free(bitmap, base, nr_ids);
7d75bbb4
MZ
3954 its_free_prop_table(vprop_page);
3955 }
3956
3957 return err;
3958}
3959
72491643 3960static int its_vpe_irq_domain_activate(struct irq_domain *domain,
702cb0a0 3961 struct irq_data *d, bool reserve)
eb78192b
MZ
3962{
3963 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
40619a2e 3964 struct its_node *its;
eb78192b 3965
2247e1bf
MZ
3966 /* If we use the list map, we issue VMAPP on demand... */
3967 if (its_list_map)
6ef930f2 3968 return 0;
eb78192b
MZ
3969
3970 /* Map the VPE to the first possible CPU */
3971 vpe->col_idx = cpumask_first(cpu_online_mask);
40619a2e
MZ
3972
3973 list_for_each_entry(its, &its_nodes, entry) {
0dd57fed 3974 if (!is_v4(its))
40619a2e
MZ
3975 continue;
3976
75fd951b 3977 its_send_vmapp(its, vpe, true);
40619a2e
MZ
3978 its_send_vinvall(its, vpe);
3979 }
3980
44c4c25e
MZ
3981 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
3982
72491643 3983 return 0;
eb78192b
MZ
3984}
3985
3986static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
3987 struct irq_data *d)
3988{
3989 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
75fd951b
MZ
3990 struct its_node *its;
3991
2247e1bf
MZ
3992 /*
3993 * If we use the list map, we unmap the VPE once no VLPIs are
3994 * associated with the VM.
3995 */
3996 if (its_list_map)
3997 return;
eb78192b 3998
75fd951b 3999 list_for_each_entry(its, &its_nodes, entry) {
0dd57fed 4000 if (!is_v4(its))
75fd951b 4001 continue;
eb78192b 4002
75fd951b
MZ
4003 its_send_vmapp(its, vpe, false);
4004 }
eb78192b
MZ
4005}
4006
8fff27ae 4007static const struct irq_domain_ops its_vpe_domain_ops = {
7d75bbb4
MZ
4008 .alloc = its_vpe_irq_domain_alloc,
4009 .free = its_vpe_irq_domain_free,
eb78192b
MZ
4010 .activate = its_vpe_irq_domain_activate,
4011 .deactivate = its_vpe_irq_domain_deactivate,
8fff27ae
MZ
4012};
4013
4559fbb3
YW
4014static int its_force_quiescent(void __iomem *base)
4015{
4016 u32 count = 1000000; /* 1s */
4017 u32 val;
4018
4019 val = readl_relaxed(base + GITS_CTLR);
7611da86
DD
4020 /*
4021 * GIC architecture specification requires the ITS to be both
4022 * disabled and quiescent for writes to GITS_BASER<n> or
4023 * GITS_CBASER to not have UNPREDICTABLE results.
4024 */
4025 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
4559fbb3
YW
4026 return 0;
4027
4028 /* Disable the generation of all interrupts to this ITS */
d51c4b4d 4029 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
4559fbb3
YW
4030 writel_relaxed(val, base + GITS_CTLR);
4031
4032 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
4033 while (1) {
4034 val = readl_relaxed(base + GITS_CTLR);
4035 if (val & GITS_CTLR_QUIESCENT)
4036 return 0;
4037
4038 count--;
4039 if (!count)
4040 return -EBUSY;
4041
4042 cpu_relax();
4043 udelay(1);
4044 }
4045}
4046
9d111d49 4047static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
94100970
RR
4048{
4049 struct its_node *its = data;
4050
576a8342
MZ
4051 /* erratum 22375: only alloc 8MB table size (20 bits) */
4052 its->typer &= ~GITS_TYPER_DEVBITS;
4053 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, 20 - 1);
94100970 4054 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
9d111d49
AB
4055
4056 return true;
94100970
RR
4057}
4058
9d111d49 4059static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
fbf8f40e
GK
4060{
4061 struct its_node *its = data;
4062
4063 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
9d111d49
AB
4064
4065 return true;
fbf8f40e
GK
4066}
4067
9d111d49 4068static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
90922a2d
SD
4069{
4070 struct its_node *its = data;
4071
4072 /* On QDF2400, the size of the ITE is 16Bytes */
ffedbf0c
MZ
4073 its->typer &= ~GITS_TYPER_ITT_ENTRY_SIZE;
4074 its->typer |= FIELD_PREP(GITS_TYPER_ITT_ENTRY_SIZE, 16 - 1);
9d111d49
AB
4075
4076 return true;
90922a2d
SD
4077}
4078
558b0165
AB
4079static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
4080{
4081 struct its_node *its = its_dev->its;
4082
4083 /*
4084 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
4085 * which maps 32-bit writes targeted at a separate window of
4086 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
4087 * with device ID taken from bits [device_id_bits + 1:2] of
4088 * the window offset.
4089 */
4090 return its->pre_its_base + (its_dev->device_id << 2);
4091}
4092
4093static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
4094{
4095 struct its_node *its = data;
4096 u32 pre_its_window[2];
4097 u32 ids;
4098
4099 if (!fwnode_property_read_u32_array(its->fwnode_handle,
4100 "socionext,synquacer-pre-its",
4101 pre_its_window,
4102 ARRAY_SIZE(pre_its_window))) {
4103
4104 its->pre_its_base = pre_its_window[0];
4105 its->get_msi_base = its_irq_get_msi_base_pre_its;
4106
4107 ids = ilog2(pre_its_window[1]) - 2;
576a8342
MZ
4108 if (device_ids(its) > ids) {
4109 its->typer &= ~GITS_TYPER_DEVBITS;
4110 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, ids - 1);
4111 }
558b0165
AB
4112
4113 /* the pre-ITS breaks isolation, so disable MSI remapping */
4114 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
4115 return true;
4116 }
4117 return false;
4118}
4119
5c9a882e
MZ
4120static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
4121{
4122 struct its_node *its = data;
4123
4124 /*
4125 * Hip07 insists on using the wrong address for the VLPI
4126 * page. Trick it into doing the right thing...
4127 */
4128 its->vlpi_redist_offset = SZ_128K;
4129 return true;
90922a2d
SD
4130}
4131
67510cca 4132static const struct gic_quirk its_quirks[] = {
94100970
RR
4133#ifdef CONFIG_CAVIUM_ERRATUM_22375
4134 {
4135 .desc = "ITS: Cavium errata 22375, 24313",
4136 .iidr = 0xa100034c, /* ThunderX pass 1.x */
4137 .mask = 0xffff0fff,
4138 .init = its_enable_quirk_cavium_22375,
4139 },
fbf8f40e
GK
4140#endif
4141#ifdef CONFIG_CAVIUM_ERRATUM_23144
4142 {
4143 .desc = "ITS: Cavium erratum 23144",
4144 .iidr = 0xa100034c, /* ThunderX pass 1.x */
4145 .mask = 0xffff0fff,
4146 .init = its_enable_quirk_cavium_23144,
4147 },
90922a2d
SD
4148#endif
4149#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
4150 {
4151 .desc = "ITS: QDF2400 erratum 0065",
4152 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
4153 .mask = 0xffffffff,
4154 .init = its_enable_quirk_qdf2400_e0065,
4155 },
558b0165
AB
4156#endif
4157#ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
4158 {
4159 /*
4160 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
4161 * implementation, but with a 'pre-ITS' added that requires
4162 * special handling in software.
4163 */
4164 .desc = "ITS: Socionext Synquacer pre-ITS",
4165 .iidr = 0x0001143b,
4166 .mask = 0xffffffff,
4167 .init = its_enable_quirk_socionext_synquacer,
4168 },
5c9a882e
MZ
4169#endif
4170#ifdef CONFIG_HISILICON_ERRATUM_161600802
4171 {
4172 .desc = "ITS: Hip07 erratum 161600802",
4173 .iidr = 0x00000004,
4174 .mask = 0xffffffff,
4175 .init = its_enable_quirk_hip07_161600802,
4176 },
94100970 4177#endif
67510cca
RR
4178 {
4179 }
4180};
4181
4182static void its_enable_quirks(struct its_node *its)
4183{
4184 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
4185
4186 gic_enable_quirks(iidr, its_quirks, its);
4187}
4188
dba0bc7b
DB
4189static int its_save_disable(void)
4190{
4191 struct its_node *its;
4192 int err = 0;
4193
a8db7456 4194 raw_spin_lock(&its_lock);
dba0bc7b
DB
4195 list_for_each_entry(its, &its_nodes, entry) {
4196 void __iomem *base;
4197
4198 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4199 continue;
4200
4201 base = its->base;
4202 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
4203 err = its_force_quiescent(base);
4204 if (err) {
4205 pr_err("ITS@%pa: failed to quiesce: %d\n",
4206 &its->phys_base, err);
4207 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
4208 goto err;
4209 }
4210
4211 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
4212 }
4213
4214err:
4215 if (err) {
4216 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
4217 void __iomem *base;
4218
4219 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4220 continue;
4221
4222 base = its->base;
4223 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
4224 }
4225 }
a8db7456 4226 raw_spin_unlock(&its_lock);
dba0bc7b
DB
4227
4228 return err;
4229}
4230
4231static void its_restore_enable(void)
4232{
4233 struct its_node *its;
4234 int ret;
4235
a8db7456 4236 raw_spin_lock(&its_lock);
dba0bc7b
DB
4237 list_for_each_entry(its, &its_nodes, entry) {
4238 void __iomem *base;
4239 int i;
4240
4241 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4242 continue;
4243
4244 base = its->base;
4245
4246 /*
4247 * Make sure that the ITS is disabled. If it fails to quiesce,
4248 * don't restore it since writing to CBASER or BASER<n>
4249 * registers is undefined according to the GIC v3 ITS
4250 * Specification.
4251 */
4252 ret = its_force_quiescent(base);
4253 if (ret) {
4254 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
4255 &its->phys_base, ret);
4256 continue;
4257 }
4258
4259 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
4260
4261 /*
4262 * Writing CBASER resets CREADR to 0, so make CWRITER and
4263 * cmd_write line up with it.
4264 */
4265 its->cmd_write = its->cmd_base;
4266 gits_write_cwriter(0, base + GITS_CWRITER);
4267
4268 /* Restore GITS_BASER from the value cache. */
4269 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
4270 struct its_baser *baser = &its->tables[i];
4271
4272 if (!(baser->val & GITS_BASER_VALID))
4273 continue;
4274
4275 its_write_baser(its, baser, baser->val);
4276 }
4277 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
920181ce
DB
4278
4279 /*
4280 * Reinit the collection if it's stored in the ITS. This is
4281 * indicated by the col_id being less than the HCC field.
4282 * CID < HCC as specified in the GIC v3 Documentation.
4283 */
4284 if (its->collections[smp_processor_id()].col_id <
4285 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
4286 its_cpu_init_collection(its);
dba0bc7b 4287 }
a8db7456 4288 raw_spin_unlock(&its_lock);
dba0bc7b
DB
4289}
4290
4291static struct syscore_ops its_syscore_ops = {
4292 .suspend = its_save_disable,
4293 .resume = its_restore_enable,
4294};
4295
db40f0a7 4296static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
d14ae5e6
TN
4297{
4298 struct irq_domain *inner_domain;
4299 struct msi_domain_info *info;
4300
4301 info = kzalloc(sizeof(*info), GFP_KERNEL);
4302 if (!info)
4303 return -ENOMEM;
4304
db40f0a7 4305 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
d14ae5e6
TN
4306 if (!inner_domain) {
4307 kfree(info);
4308 return -ENOMEM;
4309 }
4310
db40f0a7 4311 inner_domain->parent = its_parent;
96f0d93a 4312 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
558b0165 4313 inner_domain->flags |= its->msi_domain_flags;
d14ae5e6
TN
4314 info->ops = &its_msi_domain_ops;
4315 info->data = its;
4316 inner_domain->host_data = info;
4317
4318 return 0;
4319}
4320
8fff27ae
MZ
4321static int its_init_vpe_domain(void)
4322{
20b3d54e
MZ
4323 struct its_node *its;
4324 u32 devid;
4325 int entries;
4326
4327 if (gic_rdists->has_direct_lpi) {
4328 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
4329 return 0;
4330 }
4331
4332 /* Any ITS will do, even if not v4 */
4333 its = list_first_entry(&its_nodes, struct its_node, entry);
4334
4335 entries = roundup_pow_of_two(nr_cpu_ids);
6396bb22 4336 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
20b3d54e
MZ
4337 GFP_KERNEL);
4338 if (!vpe_proxy.vpes) {
4339 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
4340 return -ENOMEM;
4341 }
4342
4343 /* Use the last possible DevID */
576a8342 4344 devid = GENMASK(device_ids(its) - 1, 0);
20b3d54e
MZ
4345 vpe_proxy.dev = its_create_device(its, devid, entries, false);
4346 if (!vpe_proxy.dev) {
4347 kfree(vpe_proxy.vpes);
4348 pr_err("ITS: Can't allocate GICv4 proxy device\n");
4349 return -ENOMEM;
4350 }
4351
c427a475 4352 BUG_ON(entries > vpe_proxy.dev->nr_ites);
20b3d54e
MZ
4353
4354 raw_spin_lock_init(&vpe_proxy.lock);
4355 vpe_proxy.next_victim = 0;
4356 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
4357 devid, vpe_proxy.dev->nr_ites);
4358
8fff27ae
MZ
4359 return 0;
4360}
4361
3dfa576b
MZ
4362static int __init its_compute_its_list_map(struct resource *res,
4363 void __iomem *its_base)
4364{
4365 int its_number;
4366 u32 ctlr;
4367
4368 /*
4369 * This is assumed to be done early enough that we're
4370 * guaranteed to be single-threaded, hence no
4371 * locking. Should this change, we should address
4372 * this.
4373 */
ab60491e
MZ
4374 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
4375 if (its_number >= GICv4_ITS_LIST_MAX) {
3dfa576b
MZ
4376 pr_err("ITS@%pa: No ITSList entry available!\n",
4377 &res->start);
4378 return -EINVAL;
4379 }
4380
4381 ctlr = readl_relaxed(its_base + GITS_CTLR);
4382 ctlr &= ~GITS_CTLR_ITS_NUMBER;
4383 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
4384 writel_relaxed(ctlr, its_base + GITS_CTLR);
4385 ctlr = readl_relaxed(its_base + GITS_CTLR);
4386 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
4387 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
4388 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
4389 }
4390
4391 if (test_and_set_bit(its_number, &its_list_map)) {
4392 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
4393 &res->start, its_number);
4394 return -EINVAL;
4395 }
4396
4397 return its_number;
4398}
4399
db40f0a7
TN
4400static int __init its_probe_one(struct resource *res,
4401 struct fwnode_handle *handle, int numa_node)
4c21f3c2 4402{
4c21f3c2
MZ
4403 struct its_node *its;
4404 void __iomem *its_base;
3dfa576b
MZ
4405 u32 val, ctlr;
4406 u64 baser, tmp, typer;
539d3782 4407 struct page *page;
4c21f3c2
MZ
4408 int err;
4409
db40f0a7 4410 its_base = ioremap(res->start, resource_size(res));
4c21f3c2 4411 if (!its_base) {
db40f0a7 4412 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
4c21f3c2
MZ
4413 return -ENOMEM;
4414 }
4415
4416 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
4417 if (val != 0x30 && val != 0x40) {
db40f0a7 4418 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
4c21f3c2
MZ
4419 err = -ENODEV;
4420 goto out_unmap;
4421 }
4422
4559fbb3
YW
4423 err = its_force_quiescent(its_base);
4424 if (err) {
db40f0a7 4425 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
4559fbb3
YW
4426 goto out_unmap;
4427 }
4428
db40f0a7 4429 pr_info("ITS %pR\n", res);
4c21f3c2
MZ
4430
4431 its = kzalloc(sizeof(*its), GFP_KERNEL);
4432 if (!its) {
4433 err = -ENOMEM;
4434 goto out_unmap;
4435 }
4436
4437 raw_spin_lock_init(&its->lock);
9791ec7d 4438 mutex_init(&its->dev_alloc_lock);
4c21f3c2
MZ
4439 INIT_LIST_HEAD(&its->entry);
4440 INIT_LIST_HEAD(&its->its_device_list);
3dfa576b 4441 typer = gic_read_typer(its_base + GITS_TYPER);
0dd57fed 4442 its->typer = typer;
4c21f3c2 4443 its->base = its_base;
db40f0a7 4444 its->phys_base = res->start;
0dd57fed 4445 if (is_v4(its)) {
3dfa576b
MZ
4446 if (!(typer & GITS_TYPER_VMOVP)) {
4447 err = its_compute_its_list_map(res, its_base);
4448 if (err < 0)
4449 goto out_free_its;
4450
debf6d02
MZ
4451 its->list_nr = err;
4452
3dfa576b
MZ
4453 pr_info("ITS@%pa: Using ITS number %d\n",
4454 &res->start, err);
4455 } else {
4456 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
4457 }
5e516846
MZ
4458
4459 if (is_v4_1(its)) {
4460 u32 svpet = FIELD_GET(GITS_TYPER_SVPET, typer);
4461 its->mpidr = readl_relaxed(its_base + GITS_MPIDR);
4462
4463 pr_info("ITS@%pa: Using GICv4.1 mode %08x %08x\n",
4464 &res->start, its->mpidr, svpet);
4465 }
3dfa576b
MZ
4466 }
4467
db40f0a7 4468 its->numa_node = numa_node;
4c21f3c2 4469
539d3782
SD
4470 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
4471 get_order(ITS_CMD_QUEUE_SZ));
4472 if (!page) {
4c21f3c2
MZ
4473 err = -ENOMEM;
4474 goto out_free_its;
4475 }
539d3782 4476 its->cmd_base = (void *)page_address(page);
4c21f3c2 4477 its->cmd_write = its->cmd_base;
558b0165
AB
4478 its->fwnode_handle = handle;
4479 its->get_msi_base = its_irq_get_msi_base;
4480 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
4c21f3c2 4481
67510cca
RR
4482 its_enable_quirks(its);
4483
0e0b0f69 4484 err = its_alloc_tables(its);
4c21f3c2
MZ
4485 if (err)
4486 goto out_free_cmd;
4487
4488 err = its_alloc_collections(its);
4489 if (err)
4490 goto out_free_tables;
4491
4492 baser = (virt_to_phys(its->cmd_base) |
2fd632a0 4493 GITS_CBASER_RaWaWb |
4c21f3c2
MZ
4494 GITS_CBASER_InnerShareable |
4495 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
4496 GITS_CBASER_VALID);
4497
0968a619
VM
4498 gits_write_cbaser(baser, its->base + GITS_CBASER);
4499 tmp = gits_read_cbaser(its->base + GITS_CBASER);
4c21f3c2 4500
4ad3e363 4501 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
241a386c
MZ
4502 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
4503 /*
4504 * The HW reports non-shareable, we must
4505 * remove the cacheability attributes as
4506 * well.
4507 */
4508 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
4509 GITS_CBASER_CACHEABILITY_MASK);
4510 baser |= GITS_CBASER_nC;
0968a619 4511 gits_write_cbaser(baser, its->base + GITS_CBASER);
241a386c 4512 }
4c21f3c2
MZ
4513 pr_info("ITS: using cache flushing for cmd queue\n");
4514 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
4515 }
4516
0968a619 4517 gits_write_cwriter(0, its->base + GITS_CWRITER);
3dfa576b 4518 ctlr = readl_relaxed(its->base + GITS_CTLR);
d51c4b4d 4519 ctlr |= GITS_CTLR_ENABLE;
0dd57fed 4520 if (is_v4(its))
d51c4b4d
MZ
4521 ctlr |= GITS_CTLR_ImDe;
4522 writel_relaxed(ctlr, its->base + GITS_CTLR);
241a386c 4523
dba0bc7b
DB
4524 if (GITS_TYPER_HCC(typer))
4525 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
4526
db40f0a7 4527 err = its_init_domain(handle, its);
d14ae5e6
TN
4528 if (err)
4529 goto out_free_tables;
4c21f3c2 4530
a8db7456 4531 raw_spin_lock(&its_lock);
4c21f3c2 4532 list_add(&its->entry, &its_nodes);
a8db7456 4533 raw_spin_unlock(&its_lock);
4c21f3c2
MZ
4534
4535 return 0;
4536
4c21f3c2
MZ
4537out_free_tables:
4538 its_free_tables(its);
4539out_free_cmd:
5bc13c2c 4540 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
4c21f3c2
MZ
4541out_free_its:
4542 kfree(its);
4543out_unmap:
4544 iounmap(its_base);
db40f0a7 4545 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
4c21f3c2
MZ
4546 return err;
4547}
4548
4549static bool gic_rdists_supports_plpis(void)
4550{
589ce5f4 4551 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
4c21f3c2
MZ
4552}
4553
6eb486b6
SD
4554static int redist_disable_lpis(void)
4555{
4556 void __iomem *rbase = gic_data_rdist_rd_base();
4557 u64 timeout = USEC_PER_SEC;
4558 u64 val;
4559
4560 if (!gic_rdists_supports_plpis()) {
4561 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
4562 return -ENXIO;
4563 }
4564
4565 val = readl_relaxed(rbase + GICR_CTLR);
4566 if (!(val & GICR_CTLR_ENABLE_LPIS))
4567 return 0;
4568
11e37d35
MZ
4569 /*
4570 * If coming via a CPU hotplug event, we don't need to disable
4571 * LPIs before trying to re-enable them. They are already
4572 * configured and all is well in the world.
c440a9d9
MZ
4573 *
4574 * If running with preallocated tables, there is nothing to do.
11e37d35 4575 */
c440a9d9
MZ
4576 if (gic_data_rdist()->lpi_enabled ||
4577 (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED))
11e37d35
MZ
4578 return 0;
4579
4580 /*
4581 * From that point on, we only try to do some damage control.
4582 */
4583 pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
6eb486b6
SD
4584 smp_processor_id());
4585 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
4586
4587 /* Disable LPIs */
4588 val &= ~GICR_CTLR_ENABLE_LPIS;
4589 writel_relaxed(val, rbase + GICR_CTLR);
4590
4591 /* Make sure any change to GICR_CTLR is observable by the GIC */
4592 dsb(sy);
4593
4594 /*
4595 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
4596 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
4597 * Error out if we time out waiting for RWP to clear.
4598 */
4599 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
4600 if (!timeout) {
4601 pr_err("CPU%d: Timeout while disabling LPIs\n",
4602 smp_processor_id());
4603 return -ETIMEDOUT;
4604 }
4605 udelay(1);
4606 timeout--;
4607 }
4608
4609 /*
4610 * After it has been written to 1, it is IMPLEMENTATION
4611 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
4612 * cleared to 0. Error out if clearing the bit failed.
4613 */
4614 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
4615 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
4616 return -EBUSY;
4617 }
4618
4619 return 0;
4620}
4621
4c21f3c2
MZ
4622int its_cpu_init(void)
4623{
4c21f3c2 4624 if (!list_empty(&its_nodes)) {
6eb486b6
SD
4625 int ret;
4626
4627 ret = redist_disable_lpis();
4628 if (ret)
4629 return ret;
4630
4c21f3c2 4631 its_cpu_init_lpis();
920181ce 4632 its_cpu_init_collections();
4c21f3c2
MZ
4633 }
4634
4635 return 0;
4636}
4637
935bba7c 4638static const struct of_device_id its_device_id[] = {
4c21f3c2
MZ
4639 { .compatible = "arm,gic-v3-its", },
4640 {},
4641};
4642
db40f0a7 4643static int __init its_of_probe(struct device_node *node)
4c21f3c2
MZ
4644{
4645 struct device_node *np;
db40f0a7 4646 struct resource res;
4c21f3c2
MZ
4647
4648 for (np = of_find_matching_node(node, its_device_id); np;
4649 np = of_find_matching_node(np, its_device_id)) {
95a25625
SB
4650 if (!of_device_is_available(np))
4651 continue;
d14ae5e6 4652 if (!of_property_read_bool(np, "msi-controller")) {
e81f54c6
RH
4653 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
4654 np);
d14ae5e6
TN
4655 continue;
4656 }
4657
db40f0a7 4658 if (of_address_to_resource(np, 0, &res)) {
e81f54c6 4659 pr_warn("%pOF: no regs?\n", np);
db40f0a7
TN
4660 continue;
4661 }
4662
4663 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
4c21f3c2 4664 }
db40f0a7
TN
4665 return 0;
4666}
4667
3f010cf1
TN
4668#ifdef CONFIG_ACPI
4669
4670#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
4671
d1ce263f 4672#ifdef CONFIG_ACPI_NUMA
dbd2b826
GK
4673struct its_srat_map {
4674 /* numa node id */
4675 u32 numa_node;
4676 /* GIC ITS ID */
4677 u32 its_id;
4678};
4679
fdf6e7a8 4680static struct its_srat_map *its_srat_maps __initdata;
dbd2b826
GK
4681static int its_in_srat __initdata;
4682
4683static int __init acpi_get_its_numa_node(u32 its_id)
4684{
4685 int i;
4686
4687 for (i = 0; i < its_in_srat; i++) {
4688 if (its_id == its_srat_maps[i].its_id)
4689 return its_srat_maps[i].numa_node;
4690 }
4691 return NUMA_NO_NODE;
4692}
4693
60574d1e 4694static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header,
fdf6e7a8
HG
4695 const unsigned long end)
4696{
4697 return 0;
4698}
4699
60574d1e 4700static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header,
dbd2b826
GK
4701 const unsigned long end)
4702{
4703 int node;
4704 struct acpi_srat_gic_its_affinity *its_affinity;
4705
4706 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
4707 if (!its_affinity)
4708 return -EINVAL;
4709
4710 if (its_affinity->header.length < sizeof(*its_affinity)) {
4711 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
4712 its_affinity->header.length);
4713 return -EINVAL;
4714 }
4715
dbd2b826
GK
4716 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
4717
4718 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
4719 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
4720 return 0;
4721 }
4722
4723 its_srat_maps[its_in_srat].numa_node = node;
4724 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
4725 its_in_srat++;
4726 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
4727 its_affinity->proximity_domain, its_affinity->its_id, node);
4728
4729 return 0;
4730}
4731
4732static void __init acpi_table_parse_srat_its(void)
4733{
fdf6e7a8
HG
4734 int count;
4735
4736 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
4737 sizeof(struct acpi_table_srat),
4738 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
4739 gic_acpi_match_srat_its, 0);
4740 if (count <= 0)
4741 return;
4742
6da2ec56
KC
4743 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
4744 GFP_KERNEL);
fdf6e7a8
HG
4745 if (!its_srat_maps) {
4746 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
4747 return;
4748 }
4749
dbd2b826
GK
4750 acpi_table_parse_entries(ACPI_SIG_SRAT,
4751 sizeof(struct acpi_table_srat),
4752 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
4753 gic_acpi_parse_srat_its, 0);
4754}
fdf6e7a8
HG
4755
4756/* free the its_srat_maps after ITS probing */
4757static void __init acpi_its_srat_maps_free(void)
4758{
4759 kfree(its_srat_maps);
4760}
dbd2b826
GK
4761#else
4762static void __init acpi_table_parse_srat_its(void) { }
4763static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
fdf6e7a8 4764static void __init acpi_its_srat_maps_free(void) { }
dbd2b826
GK
4765#endif
4766
60574d1e 4767static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header,
3f010cf1
TN
4768 const unsigned long end)
4769{
4770 struct acpi_madt_generic_translator *its_entry;
4771 struct fwnode_handle *dom_handle;
4772 struct resource res;
4773 int err;
4774
4775 its_entry = (struct acpi_madt_generic_translator *)header;
4776 memset(&res, 0, sizeof(res));
4777 res.start = its_entry->base_address;
4778 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
4779 res.flags = IORESOURCE_MEM;
4780
5778cc77 4781 dom_handle = irq_domain_alloc_fwnode(&res.start);
3f010cf1
TN
4782 if (!dom_handle) {
4783 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
4784 &res.start);
4785 return -ENOMEM;
4786 }
4787
8b4282e6
SK
4788 err = iort_register_domain_token(its_entry->translation_id, res.start,
4789 dom_handle);
3f010cf1
TN
4790 if (err) {
4791 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
4792 &res.start, its_entry->translation_id);
4793 goto dom_err;
4794 }
4795
dbd2b826
GK
4796 err = its_probe_one(&res, dom_handle,
4797 acpi_get_its_numa_node(its_entry->translation_id));
3f010cf1
TN
4798 if (!err)
4799 return 0;
4800
4801 iort_deregister_domain_token(its_entry->translation_id);
4802dom_err:
4803 irq_domain_free_fwnode(dom_handle);
4804 return err;
4805}
4806
4807static void __init its_acpi_probe(void)
4808{
dbd2b826 4809 acpi_table_parse_srat_its();
3f010cf1
TN
4810 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
4811 gic_acpi_parse_madt_its, 0);
fdf6e7a8 4812 acpi_its_srat_maps_free();
3f010cf1
TN
4813}
4814#else
4815static void __init its_acpi_probe(void) { }
4816#endif
4817
db40f0a7
TN
4818int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
4819 struct irq_domain *parent_domain)
4820{
4821 struct device_node *of_node;
8fff27ae
MZ
4822 struct its_node *its;
4823 bool has_v4 = false;
4824 int err;
db40f0a7 4825
5e516846
MZ
4826 gic_rdists = rdists;
4827
db40f0a7
TN
4828 its_parent = parent_domain;
4829 of_node = to_of_node(handle);
4830 if (of_node)
4831 its_of_probe(of_node);
4832 else
3f010cf1 4833 its_acpi_probe();
4c21f3c2
MZ
4834
4835 if (list_empty(&its_nodes)) {
4836 pr_warn("ITS: No ITS available, not enabling LPIs\n");
4837 return -ENXIO;
4838 }
4839
11e37d35 4840 err = allocate_lpi_tables();
8fff27ae
MZ
4841 if (err)
4842 return err;
4843
4844 list_for_each_entry(its, &its_nodes, entry)
0dd57fed 4845 has_v4 |= is_v4(its);
8fff27ae
MZ
4846
4847 if (has_v4 & rdists->has_vlpis) {
3d63cb53
MZ
4848 if (its_init_vpe_domain() ||
4849 its_init_v4(parent_domain, &its_vpe_domain_ops)) {
8fff27ae
MZ
4850 rdists->has_vlpis = false;
4851 pr_err("ITS: Disabling GICv4 support\n");
4852 }
4853 }
4854
dba0bc7b
DB
4855 register_syscore_ops(&its_syscore_ops);
4856
8fff27ae 4857 return 0;
4c21f3c2 4858}