blob: 54d6fdf7a28e6e73ce009bdef8d71dfc5fc09d2b [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001// SPDX-License-Identifier: GPL-2.0-only
Marc Zyngiercc2d3212014-11-24 14:35:11 +00002/*
Marc Zyngierd7276b82016-12-20 15:11:47 +00003 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
Marc Zyngiercc2d3212014-11-24 14:35:11 +00004 * Author: Marc Zyngier <marc.zyngier@arm.com>
Marc Zyngiercc2d3212014-11-24 14:35:11 +00005 */
6
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02007#include <linux/acpi.h>
Hanjun Guo8d3554b2017-03-07 20:39:59 +08008#include <linux/acpi_iort.h>
Marc Zyngierffedbf02019-11-08 16:57:59 +00009#include <linux/bitfield.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000010#include <linux/bitmap.h>
11#include <linux/cpu.h>
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +010012#include <linux/crash_dump.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000013#include <linux/delay.h>
Robin Murphy44bb7e22016-09-12 17:13:59 +010014#include <linux/dma-iommu.h>
Marc Zyngier3fb68fa2018-07-27 16:21:18 +010015#include <linux/efi.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000016#include <linux/interrupt.h>
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +020017#include <linux/irqdomain.h>
Marc Zyngier880cb3c2018-05-27 16:14:15 +010018#include <linux/list.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000019#include <linux/log2.h>
Marc Zyngier5e2c9f92018-07-27 16:23:18 +010020#include <linux/memblock.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000021#include <linux/mm.h>
22#include <linux/msi.h>
23#include <linux/of.h>
24#include <linux/of_address.h>
25#include <linux/of_irq.h>
26#include <linux/of_pci.h>
27#include <linux/of_platform.h>
28#include <linux/percpu.h>
29#include <linux/slab.h>
Derek Basehoredba0bc72018-02-28 21:48:18 -080030#include <linux/syscore_ops.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000031
Joel Porquet41a83e062015-07-07 17:11:46 -040032#include <linux/irqchip.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000033#include <linux/irqchip/arm-gic-v3.h>
Marc Zyngierc808eea2016-12-20 09:31:20 +000034#include <linux/irqchip/arm-gic-v4.h>
Marc Zyngiercc2d3212014-11-24 14:35:11 +000035
Marc Zyngiercc2d3212014-11-24 14:35:11 +000036#include <asm/cputype.h>
37#include <asm/exception.h>
38
Robert Richter67510cc2015-09-21 22:58:37 +020039#include "irq-gic-common.h"
40
Robert Richter94100972015-09-21 22:58:38 +020041#define ITS_FLAGS_CMDQ_NEEDS_FLUSHING (1ULL << 0)
42#define ITS_FLAGS_WORKAROUND_CAVIUM_22375 (1ULL << 1)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +020043#define ITS_FLAGS_WORKAROUND_CAVIUM_23144 (1ULL << 2)
Derek Basehoredba0bc72018-02-28 21:48:18 -080044#define ITS_FLAGS_SAVE_SUSPEND_STATE (1ULL << 3)
Marc Zyngiercc2d3212014-11-24 14:35:11 +000045
Marc Zyngierc48ed512014-11-24 14:35:12 +000046#define RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING (1 << 0)
Marc Zyngierc440a9d2018-07-27 15:40:13 +010047#define RDIST_FLAGS_RD_TABLES_PREALLOCATED (1 << 1)
Marc Zyngierc48ed512014-11-24 14:35:12 +000048
Marc Zyngiera13b0402016-12-19 17:15:24 +000049static u32 lpi_id_bits;
50
51/*
52 * We allocate memory for PROPBASE to cover 2 ^ lpi_id_bits LPIs to
53 * deal with (one configuration byte per interrupt). PENDBASE has to
54 * be 64kB aligned (one bit per LPI, plus 8192 bits for SPI/PPI/SGI).
55 */
56#define LPI_NRBITS lpi_id_bits
57#define LPI_PROPBASE_SZ ALIGN(BIT(LPI_NRBITS), SZ_64K)
58#define LPI_PENDBASE_SZ ALIGN(BIT(LPI_NRBITS) / 8, SZ_64K)
59
Julien Thierry2130b782018-08-28 16:51:18 +010060#define LPI_PROP_DEFAULT_PRIO GICD_INT_DEF_PRI
Marc Zyngiera13b0402016-12-19 17:15:24 +000061
Marc Zyngiercc2d3212014-11-24 14:35:11 +000062/*
63 * Collection structure - just an ID, and a redistributor address to
64 * ping. We use one per CPU as a bag of interrupts assigned to this
65 * CPU.
66 */
67struct its_collection {
68 u64 target_address;
69 u16 col_id;
70};
71
72/*
Shanker Donthineni93473592016-06-06 18:17:30 -050073 * The ITS_BASER structure - contains memory information, cached
74 * value of BASER register configuration and ITS page size.
Shanker Donthineni466b7d12016-03-09 22:10:49 -060075 */
76struct its_baser {
77 void *base;
78 u64 val;
79 u32 order;
Shanker Donthineni93473592016-06-06 18:17:30 -050080 u32 psz;
Shanker Donthineni466b7d12016-03-09 22:10:49 -060081};
82
Ard Biesheuvel558b0162017-10-17 17:55:56 +010083struct its_device;
84
Shanker Donthineni466b7d12016-03-09 22:10:49 -060085/*
Marc Zyngiercc2d3212014-11-24 14:35:11 +000086 * The ITS structure - contains most of the infrastructure, with the
Marc Zyngier841514a2015-07-28 14:46:20 +010087 * top-level MSI domain, the command queue, the collections, and the
88 * list of devices writing to it.
Marc Zyngier9791ec72019-01-29 10:02:33 +000089 *
90 * dev_alloc_lock has to be taken for device allocations, while the
91 * spinlock must be taken to parse data structures such as the device
92 * list.
Marc Zyngiercc2d3212014-11-24 14:35:11 +000093 */
94struct its_node {
95 raw_spinlock_t lock;
Marc Zyngier9791ec72019-01-29 10:02:33 +000096 struct mutex dev_alloc_lock;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000097 struct list_head entry;
Marc Zyngiercc2d3212014-11-24 14:35:11 +000098 void __iomem *base;
Marc Zyngier5e46a482020-03-04 20:33:14 +000099 void __iomem *sgir_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200100 phys_addr_t phys_base;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000101 struct its_cmd_block *cmd_base;
102 struct its_cmd_block *cmd_write;
Shanker Donthineni466b7d12016-03-09 22:10:49 -0600103 struct its_baser tables[GITS_BASER_NR_REGS];
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000104 struct its_collection *collections;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100105 struct fwnode_handle *fwnode_handle;
106 u64 (*get_msi_base)(struct its_device *its_dev);
Marc Zyngier0dd57fe2019-11-08 16:57:58 +0000107 u64 typer;
Derek Basehoredba0bc72018-02-28 21:48:18 -0800108 u64 cbaser_save;
109 u32 ctlr_save;
Marc Zyngier5e516842019-12-24 11:10:28 +0000110 u32 mpidr;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000111 struct list_head its_device_list;
112 u64 flags;
Marc Zyngierdebf6d02017-10-08 18:44:42 +0100113 unsigned long list_nr;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +0200114 int numa_node;
Ard Biesheuvel558b0162017-10-17 17:55:56 +0100115 unsigned int msi_domain_flags;
116 u32 pre_its_base; /* for Socionext Synquacer */
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100117 int vlpi_redist_offset;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000118};
119
Marc Zyngier0dd57fe2019-11-08 16:57:58 +0000120#define is_v4(its) (!!((its)->typer & GITS_TYPER_VLPIS))
Marc Zyngier5e516842019-12-24 11:10:28 +0000121#define is_v4_1(its) (!!((its)->typer & GITS_TYPER_VMAPP))
Marc Zyngier576a8342019-11-08 16:58:00 +0000122#define device_ids(its) (FIELD_GET(GITS_TYPER_DEVBITS, (its)->typer) + 1)
Marc Zyngier0dd57fe2019-11-08 16:57:58 +0000123
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000124#define ITS_ITT_ALIGN SZ_256
125
Shanker Donthineni32bd44d2017-10-07 15:43:48 -0500126/* The maximum number of VPEID bits supported by VLPI commands */
Marc Zyngierf2d83402019-12-24 11:10:25 +0000127#define ITS_MAX_VPEID_BITS \
128 ({ \
129 int nvpeid = 16; \
130 if (gic_rdists->has_rvpeid && \
131 gic_rdists->gicd_typer2 & GICD_TYPER2_VIL) \
132 nvpeid = 1 + (gic_rdists->gicd_typer2 & \
133 GICD_TYPER2_VID); \
134 \
135 nvpeid; \
136 })
Shanker Donthineni32bd44d2017-10-07 15:43:48 -0500137#define ITS_MAX_VPEID (1 << (ITS_MAX_VPEID_BITS))
138
Shanker Donthineni2eca0d62016-02-16 18:00:36 -0600139/* Convert page order to size in bytes */
140#define PAGE_ORDER_TO_SIZE(o) (PAGE_SIZE << (o))
141
Marc Zyngier591e5be2015-07-17 10:46:42 +0100142struct event_lpi_map {
143 unsigned long *lpi_map;
144 u16 *col_map;
145 irq_hw_number_t lpi_base;
146 int nr_lpis;
Marc Zyngier11635fa2019-11-08 16:58:05 +0000147 raw_spinlock_t vlpi_lock;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000148 struct its_vm *vm;
149 struct its_vlpi_map *vlpi_maps;
150 int nr_vlpis;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100151};
152
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000153/*
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000154 * The ITS view of a device - belongs to an ITS, owns an interrupt
155 * translation table, and a list of interrupts. If it some of its
156 * LPIs are injected into a guest (GICv4), the event_map.vm field
157 * indicates which one.
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000158 */
159struct its_device {
160 struct list_head entry;
161 struct its_node *its;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100162 struct event_lpi_map event_map;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000163 void *itt;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000164 u32 nr_ites;
165 u32 device_id;
Marc Zyngier9791ec72019-01-29 10:02:33 +0000166 bool shared;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000167};
168
Marc Zyngier20b3d542016-12-20 15:23:22 +0000169static struct {
170 raw_spinlock_t lock;
171 struct its_device *dev;
172 struct its_vpe **vpes;
173 int next_victim;
174} vpe_proxy;
175
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000176static LIST_HEAD(its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +0200177static DEFINE_RAW_SPINLOCK(its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000178static struct rdists *gic_rdists;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +0200179static struct irq_domain *its_parent;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000180
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000181static unsigned long its_list_map;
Marc Zyngier3171a472016-12-20 15:17:28 +0000182static u16 vmovp_seq_num;
183static DEFINE_RAW_SPINLOCK(vmovp_lock);
184
Marc Zyngier7d75bbb2016-12-20 13:55:54 +0000185static DEFINE_IDA(its_vpeid_ida);
Marc Zyngier3dfa5762016-12-19 17:25:54 +0000186
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000187#define gic_data_rdist() (raw_cpu_ptr(gic_rdists->rdist))
Marc Zyngier11e37d32018-07-27 13:38:54 +0100188#define gic_data_rdist_cpu(cpu) (per_cpu_ptr(gic_rdists->rdist, cpu))
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000189#define gic_data_rdist_rd_base() (gic_data_rdist()->rd_base)
Marc Zyngiere643d802016-12-20 15:09:31 +0000190#define gic_data_rdist_vlpi_base() (gic_data_rdist_rd_base() + SZ_128K)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +0000191
Zenghui Yu84243122019-10-23 03:46:26 +0000192static u16 get_its_list(struct its_vm *vm)
193{
194 struct its_node *its;
195 unsigned long its_list = 0;
196
197 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +0000198 if (!is_v4(its))
Zenghui Yu84243122019-10-23 03:46:26 +0000199 continue;
200
201 if (vm->vlpi_count[its->list_nr])
202 __set_bit(its->list_nr, &its_list);
203 }
204
205 return (u16)its_list;
206}
207
Marc Zyngier425c09b2019-11-08 16:57:57 +0000208static inline u32 its_get_event_id(struct irq_data *d)
209{
210 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
211 return d->hwirq - its_dev->event_map.lpi_base;
212}
213
Marc Zyngier591e5be2015-07-17 10:46:42 +0100214static struct its_collection *dev_event_to_col(struct its_device *its_dev,
215 u32 event)
216{
217 struct its_node *its = its_dev->its;
218
219 return its->collections + its_dev->event_map.col_map[event];
220}
221
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +0000222static struct its_vlpi_map *dev_event_to_vlpi_map(struct its_device *its_dev,
223 u32 event)
224{
225 if (WARN_ON_ONCE(event >= its_dev->event_map.nr_lpis))
226 return NULL;
227
228 return &its_dev->event_map.vlpi_maps[event];
229}
230
Marc Zyngierf4a81f52019-12-24 11:10:38 +0000231static struct its_vlpi_map *get_vlpi_map(struct irq_data *d)
232{
233 if (irqd_is_forwarded_to_vcpu(d)) {
234 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
235 u32 event = its_get_event_id(d);
236
237 return dev_event_to_vlpi_map(its_dev, event);
238 }
239
240 return NULL;
241}
242
Marc Zyngierf3a059212020-03-04 20:33:10 +0000243static int vpe_to_cpuid_lock(struct its_vpe *vpe, unsigned long *flags)
Marc Zyngier425c09b2019-11-08 16:57:57 +0000244{
Marc Zyngierf3a059212020-03-04 20:33:10 +0000245 raw_spin_lock_irqsave(&vpe->vpe_lock, *flags);
246 return vpe->col_idx;
247}
248
249static void vpe_to_cpuid_unlock(struct its_vpe *vpe, unsigned long flags)
250{
251 raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags);
252}
253
254static int irq_to_cpuid_lock(struct irq_data *d, unsigned long *flags)
255{
256 struct its_vlpi_map *map = get_vlpi_map(d);
257 int cpu;
258
259 if (map) {
260 cpu = vpe_to_cpuid_lock(map->vpe, flags);
261 } else {
262 /* Physical LPIs are already locked via the irq_desc lock */
263 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
264 cpu = its_dev->event_map.col_map[its_get_event_id(d)];
265 /* Keep GCC quiet... */
266 *flags = 0;
267 }
268
269 return cpu;
270}
271
272static void irq_to_cpuid_unlock(struct irq_data *d, unsigned long flags)
273{
Marc Zyngierf4a81f52019-12-24 11:10:38 +0000274 struct its_vlpi_map *map = get_vlpi_map(d);
Marc Zyngier425c09b2019-11-08 16:57:57 +0000275
Marc Zyngierf4a81f52019-12-24 11:10:38 +0000276 if (map)
Marc Zyngierf3a059212020-03-04 20:33:10 +0000277 vpe_to_cpuid_unlock(map->vpe, flags);
Marc Zyngier425c09b2019-11-08 16:57:57 +0000278}
279
Marc Zyngier83559b42018-06-22 10:52:52 +0100280static struct its_collection *valid_col(struct its_collection *col)
281{
Joe Perches20faba82019-07-09 22:04:18 -0700282 if (WARN_ON_ONCE(col->target_address & GENMASK_ULL(15, 0)))
Marc Zyngier83559b42018-06-22 10:52:52 +0100283 return NULL;
284
285 return col;
286}
287
Marc Zyngier205e0652018-06-22 10:52:53 +0100288static struct its_vpe *valid_vpe(struct its_node *its, struct its_vpe *vpe)
289{
290 if (valid_col(its->collections + vpe->col_idx))
291 return vpe;
292
293 return NULL;
294}
295
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000296/*
297 * ITS command descriptors - parameters to be encoded in a command
298 * block.
299 */
300struct its_cmd_desc {
301 union {
302 struct {
303 struct its_device *dev;
304 u32 event_id;
305 } its_inv_cmd;
306
307 struct {
308 struct its_device *dev;
309 u32 event_id;
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000310 } its_clear_cmd;
311
312 struct {
313 struct its_device *dev;
314 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000315 } its_int_cmd;
316
317 struct {
318 struct its_device *dev;
319 int valid;
320 } its_mapd_cmd;
321
322 struct {
323 struct its_collection *col;
324 int valid;
325 } its_mapc_cmd;
326
327 struct {
328 struct its_device *dev;
329 u32 phys_id;
330 u32 event_id;
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000331 } its_mapti_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000332
333 struct {
334 struct its_device *dev;
335 struct its_collection *col;
Marc Zyngier591e5be2015-07-17 10:46:42 +0100336 u32 event_id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000337 } its_movi_cmd;
338
339 struct {
340 struct its_device *dev;
341 u32 event_id;
342 } its_discard_cmd;
343
344 struct {
345 struct its_collection *col;
346 } its_invall_cmd;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000347
348 struct {
349 struct its_vpe *vpe;
Marc Zyngiereb781922016-12-20 14:47:05 +0000350 } its_vinvall_cmd;
351
352 struct {
353 struct its_vpe *vpe;
354 struct its_collection *col;
355 bool valid;
356 } its_vmapp_cmd;
357
358 struct {
359 struct its_vpe *vpe;
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000360 struct its_device *dev;
361 u32 virt_id;
362 u32 event_id;
363 bool db_enabled;
364 } its_vmapti_cmd;
365
366 struct {
367 struct its_vpe *vpe;
368 struct its_device *dev;
369 u32 event_id;
370 bool db_enabled;
371 } its_vmovi_cmd;
Marc Zyngier3171a472016-12-20 15:17:28 +0000372
373 struct {
374 struct its_vpe *vpe;
375 struct its_collection *col;
376 u16 seq_num;
377 u16 its_list;
378 } its_vmovp_cmd;
Marc Zyngierd97c97b2019-12-24 11:10:33 +0000379
380 struct {
381 struct its_vpe *vpe;
382 } its_invdb_cmd;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000383 };
384};
385
386/*
387 * The ITS command block, which is what the ITS actually parses.
388 */
389struct its_cmd_block {
Ben Dooks (Codethink)2bbdfcc2019-10-17 12:29:55 +0100390 union {
391 u64 raw_cmd[4];
392 __le64 raw_cmd_le[4];
393 };
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000394};
395
396#define ITS_CMD_QUEUE_SZ SZ_64K
397#define ITS_CMD_QUEUE_NR_ENTRIES (ITS_CMD_QUEUE_SZ / sizeof(struct its_cmd_block))
398
Marc Zyngier67047f902017-07-28 21:16:58 +0100399typedef struct its_collection *(*its_cmd_builder_t)(struct its_node *,
400 struct its_cmd_block *,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000401 struct its_cmd_desc *);
402
Marc Zyngier67047f902017-07-28 21:16:58 +0100403typedef struct its_vpe *(*its_cmd_vbuilder_t)(struct its_node *,
404 struct its_cmd_block *,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000405 struct its_cmd_desc *);
406
Marc Zyngier4d36f132016-12-19 17:11:52 +0000407static void its_mask_encode(u64 *raw_cmd, u64 val, int h, int l)
408{
409 u64 mask = GENMASK_ULL(h, l);
410 *raw_cmd &= ~mask;
411 *raw_cmd |= (val << l) & mask;
412}
413
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000414static void its_encode_cmd(struct its_cmd_block *cmd, u8 cmd_nr)
415{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000416 its_mask_encode(&cmd->raw_cmd[0], cmd_nr, 7, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000417}
418
419static void its_encode_devid(struct its_cmd_block *cmd, u32 devid)
420{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000421 its_mask_encode(&cmd->raw_cmd[0], devid, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000422}
423
424static void its_encode_event_id(struct its_cmd_block *cmd, u32 id)
425{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000426 its_mask_encode(&cmd->raw_cmd[1], id, 31, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000427}
428
429static void its_encode_phys_id(struct its_cmd_block *cmd, u32 phys_id)
430{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000431 its_mask_encode(&cmd->raw_cmd[1], phys_id, 63, 32);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000432}
433
434static void its_encode_size(struct its_cmd_block *cmd, u8 size)
435{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000436 its_mask_encode(&cmd->raw_cmd[1], size, 4, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000437}
438
439static void its_encode_itt(struct its_cmd_block *cmd, u64 itt_addr)
440{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500441 its_mask_encode(&cmd->raw_cmd[2], itt_addr >> 8, 51, 8);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000442}
443
444static void its_encode_valid(struct its_cmd_block *cmd, int valid)
445{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000446 its_mask_encode(&cmd->raw_cmd[2], !!valid, 63, 63);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000447}
448
449static void its_encode_target(struct its_cmd_block *cmd, u64 target_addr)
450{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500451 its_mask_encode(&cmd->raw_cmd[2], target_addr >> 16, 51, 16);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000452}
453
454static void its_encode_collection(struct its_cmd_block *cmd, u16 col)
455{
Marc Zyngier4d36f132016-12-19 17:11:52 +0000456 its_mask_encode(&cmd->raw_cmd[2], col, 15, 0);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000457}
458
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000459static void its_encode_vpeid(struct its_cmd_block *cmd, u16 vpeid)
460{
461 its_mask_encode(&cmd->raw_cmd[1], vpeid, 47, 32);
462}
463
464static void its_encode_virt_id(struct its_cmd_block *cmd, u32 virt_id)
465{
466 its_mask_encode(&cmd->raw_cmd[2], virt_id, 31, 0);
467}
468
469static void its_encode_db_phys_id(struct its_cmd_block *cmd, u32 db_phys_id)
470{
471 its_mask_encode(&cmd->raw_cmd[2], db_phys_id, 63, 32);
472}
473
474static void its_encode_db_valid(struct its_cmd_block *cmd, bool db_valid)
475{
476 its_mask_encode(&cmd->raw_cmd[2], db_valid, 0, 0);
477}
478
Marc Zyngier3171a472016-12-20 15:17:28 +0000479static void its_encode_seq_num(struct its_cmd_block *cmd, u16 seq_num)
480{
481 its_mask_encode(&cmd->raw_cmd[0], seq_num, 47, 32);
482}
483
484static void its_encode_its_list(struct its_cmd_block *cmd, u16 its_list)
485{
486 its_mask_encode(&cmd->raw_cmd[1], its_list, 15, 0);
487}
488
Marc Zyngiereb781922016-12-20 14:47:05 +0000489static void its_encode_vpt_addr(struct its_cmd_block *cmd, u64 vpt_pa)
490{
Shanker Donthineni30ae9612017-10-09 11:46:55 -0500491 its_mask_encode(&cmd->raw_cmd[3], vpt_pa >> 16, 51, 16);
Marc Zyngiereb781922016-12-20 14:47:05 +0000492}
493
494static void its_encode_vpt_size(struct its_cmd_block *cmd, u8 vpt_size)
495{
496 its_mask_encode(&cmd->raw_cmd[3], vpt_size, 4, 0);
497}
498
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000499static void its_encode_vconf_addr(struct its_cmd_block *cmd, u64 vconf_pa)
500{
501 its_mask_encode(&cmd->raw_cmd[0], vconf_pa >> 16, 51, 16);
502}
503
504static void its_encode_alloc(struct its_cmd_block *cmd, bool alloc)
505{
506 its_mask_encode(&cmd->raw_cmd[0], alloc, 8, 8);
507}
508
509static void its_encode_ptz(struct its_cmd_block *cmd, bool ptz)
510{
511 its_mask_encode(&cmd->raw_cmd[0], ptz, 9, 9);
512}
513
514static void its_encode_vmapp_default_db(struct its_cmd_block *cmd,
515 u32 vpe_db_lpi)
516{
517 its_mask_encode(&cmd->raw_cmd[1], vpe_db_lpi, 31, 0);
518}
519
Marc Zyngierdd3f0502019-12-24 11:10:31 +0000520static void its_encode_vmovp_default_db(struct its_cmd_block *cmd,
521 u32 vpe_db_lpi)
522{
523 its_mask_encode(&cmd->raw_cmd[3], vpe_db_lpi, 31, 0);
524}
525
526static void its_encode_db(struct its_cmd_block *cmd, bool db)
527{
528 its_mask_encode(&cmd->raw_cmd[2], db, 63, 63);
529}
530
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000531static inline void its_fixup_cmd(struct its_cmd_block *cmd)
532{
533 /* Let's fixup BE commands */
Ben Dooks (Codethink)2bbdfcc2019-10-17 12:29:55 +0100534 cmd->raw_cmd_le[0] = cpu_to_le64(cmd->raw_cmd[0]);
535 cmd->raw_cmd_le[1] = cpu_to_le64(cmd->raw_cmd[1]);
536 cmd->raw_cmd_le[2] = cpu_to_le64(cmd->raw_cmd[2]);
537 cmd->raw_cmd_le[3] = cpu_to_le64(cmd->raw_cmd[3]);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000538}
539
Marc Zyngier67047f902017-07-28 21:16:58 +0100540static struct its_collection *its_build_mapd_cmd(struct its_node *its,
541 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000542 struct its_cmd_desc *desc)
543{
544 unsigned long itt_addr;
Marc Zyngierc8481262014-12-12 10:51:24 +0000545 u8 size = ilog2(desc->its_mapd_cmd.dev->nr_ites);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000546
547 itt_addr = virt_to_phys(desc->its_mapd_cmd.dev->itt);
548 itt_addr = ALIGN(itt_addr, ITS_ITT_ALIGN);
549
550 its_encode_cmd(cmd, GITS_CMD_MAPD);
551 its_encode_devid(cmd, desc->its_mapd_cmd.dev->device_id);
552 its_encode_size(cmd, size - 1);
553 its_encode_itt(cmd, itt_addr);
554 its_encode_valid(cmd, desc->its_mapd_cmd.valid);
555
556 its_fixup_cmd(cmd);
557
Marc Zyngier591e5be2015-07-17 10:46:42 +0100558 return NULL;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000559}
560
Marc Zyngier67047f902017-07-28 21:16:58 +0100561static struct its_collection *its_build_mapc_cmd(struct its_node *its,
562 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000563 struct its_cmd_desc *desc)
564{
565 its_encode_cmd(cmd, GITS_CMD_MAPC);
566 its_encode_collection(cmd, desc->its_mapc_cmd.col->col_id);
567 its_encode_target(cmd, desc->its_mapc_cmd.col->target_address);
568 its_encode_valid(cmd, desc->its_mapc_cmd.valid);
569
570 its_fixup_cmd(cmd);
571
572 return desc->its_mapc_cmd.col;
573}
574
Marc Zyngier67047f902017-07-28 21:16:58 +0100575static struct its_collection *its_build_mapti_cmd(struct its_node *its,
576 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000577 struct its_cmd_desc *desc)
578{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100579 struct its_collection *col;
580
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000581 col = dev_event_to_col(desc->its_mapti_cmd.dev,
582 desc->its_mapti_cmd.event_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100583
Marc Zyngier6a25ad32016-12-20 15:52:26 +0000584 its_encode_cmd(cmd, GITS_CMD_MAPTI);
585 its_encode_devid(cmd, desc->its_mapti_cmd.dev->device_id);
586 its_encode_event_id(cmd, desc->its_mapti_cmd.event_id);
587 its_encode_phys_id(cmd, desc->its_mapti_cmd.phys_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100588 its_encode_collection(cmd, col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000589
590 its_fixup_cmd(cmd);
591
Marc Zyngier83559b42018-06-22 10:52:52 +0100592 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000593}
594
Marc Zyngier67047f902017-07-28 21:16:58 +0100595static struct its_collection *its_build_movi_cmd(struct its_node *its,
596 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000597 struct its_cmd_desc *desc)
598{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100599 struct its_collection *col;
600
601 col = dev_event_to_col(desc->its_movi_cmd.dev,
602 desc->its_movi_cmd.event_id);
603
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000604 its_encode_cmd(cmd, GITS_CMD_MOVI);
605 its_encode_devid(cmd, desc->its_movi_cmd.dev->device_id);
Marc Zyngier591e5be2015-07-17 10:46:42 +0100606 its_encode_event_id(cmd, desc->its_movi_cmd.event_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000607 its_encode_collection(cmd, desc->its_movi_cmd.col->col_id);
608
609 its_fixup_cmd(cmd);
610
Marc Zyngier83559b42018-06-22 10:52:52 +0100611 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000612}
613
Marc Zyngier67047f902017-07-28 21:16:58 +0100614static struct its_collection *its_build_discard_cmd(struct its_node *its,
615 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000616 struct its_cmd_desc *desc)
617{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100618 struct its_collection *col;
619
620 col = dev_event_to_col(desc->its_discard_cmd.dev,
621 desc->its_discard_cmd.event_id);
622
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000623 its_encode_cmd(cmd, GITS_CMD_DISCARD);
624 its_encode_devid(cmd, desc->its_discard_cmd.dev->device_id);
625 its_encode_event_id(cmd, desc->its_discard_cmd.event_id);
626
627 its_fixup_cmd(cmd);
628
Marc Zyngier83559b42018-06-22 10:52:52 +0100629 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000630}
631
Marc Zyngier67047f902017-07-28 21:16:58 +0100632static struct its_collection *its_build_inv_cmd(struct its_node *its,
633 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000634 struct its_cmd_desc *desc)
635{
Marc Zyngier591e5be2015-07-17 10:46:42 +0100636 struct its_collection *col;
637
638 col = dev_event_to_col(desc->its_inv_cmd.dev,
639 desc->its_inv_cmd.event_id);
640
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000641 its_encode_cmd(cmd, GITS_CMD_INV);
642 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
643 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
644
645 its_fixup_cmd(cmd);
646
Marc Zyngier83559b42018-06-22 10:52:52 +0100647 return valid_col(col);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000648}
649
Marc Zyngier67047f902017-07-28 21:16:58 +0100650static struct its_collection *its_build_int_cmd(struct its_node *its,
651 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000652 struct its_cmd_desc *desc)
653{
654 struct its_collection *col;
655
656 col = dev_event_to_col(desc->its_int_cmd.dev,
657 desc->its_int_cmd.event_id);
658
659 its_encode_cmd(cmd, GITS_CMD_INT);
660 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
661 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
662
663 its_fixup_cmd(cmd);
664
Marc Zyngier83559b42018-06-22 10:52:52 +0100665 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000666}
667
Marc Zyngier67047f902017-07-28 21:16:58 +0100668static struct its_collection *its_build_clear_cmd(struct its_node *its,
669 struct its_cmd_block *cmd,
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000670 struct its_cmd_desc *desc)
671{
672 struct its_collection *col;
673
674 col = dev_event_to_col(desc->its_clear_cmd.dev,
675 desc->its_clear_cmd.event_id);
676
677 its_encode_cmd(cmd, GITS_CMD_CLEAR);
678 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
679 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
680
681 its_fixup_cmd(cmd);
682
Marc Zyngier83559b42018-06-22 10:52:52 +0100683 return valid_col(col);
Marc Zyngier8d85dce2016-12-19 18:02:13 +0000684}
685
Marc Zyngier67047f902017-07-28 21:16:58 +0100686static struct its_collection *its_build_invall_cmd(struct its_node *its,
687 struct its_cmd_block *cmd,
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000688 struct its_cmd_desc *desc)
689{
690 its_encode_cmd(cmd, GITS_CMD_INVALL);
Zenghui Yu10794522019-12-02 15:10:21 +0800691 its_encode_collection(cmd, desc->its_invall_cmd.col->col_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000692
693 its_fixup_cmd(cmd);
694
695 return NULL;
696}
697
Marc Zyngier67047f902017-07-28 21:16:58 +0100698static struct its_vpe *its_build_vinvall_cmd(struct its_node *its,
699 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000700 struct its_cmd_desc *desc)
701{
702 its_encode_cmd(cmd, GITS_CMD_VINVALL);
703 its_encode_vpeid(cmd, desc->its_vinvall_cmd.vpe->vpe_id);
704
705 its_fixup_cmd(cmd);
706
Marc Zyngier205e0652018-06-22 10:52:53 +0100707 return valid_vpe(its, desc->its_vinvall_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000708}
709
Marc Zyngier67047f902017-07-28 21:16:58 +0100710static struct its_vpe *its_build_vmapp_cmd(struct its_node *its,
711 struct its_cmd_block *cmd,
Marc Zyngiereb781922016-12-20 14:47:05 +0000712 struct its_cmd_desc *desc)
713{
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000714 unsigned long vpt_addr, vconf_addr;
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100715 u64 target;
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000716 bool alloc;
Marc Zyngiereb781922016-12-20 14:47:05 +0000717
718 its_encode_cmd(cmd, GITS_CMD_VMAPP);
719 its_encode_vpeid(cmd, desc->its_vmapp_cmd.vpe->vpe_id);
720 its_encode_valid(cmd, desc->its_vmapp_cmd.valid);
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000721
722 if (!desc->its_vmapp_cmd.valid) {
723 if (is_v4_1(its)) {
724 alloc = !atomic_dec_return(&desc->its_vmapp_cmd.vpe->vmapp_count);
725 its_encode_alloc(cmd, alloc);
726 }
727
728 goto out;
729 }
730
731 vpt_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->vpt_page));
732 target = desc->its_vmapp_cmd.col->target_address + its->vlpi_redist_offset;
733
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100734 its_encode_target(cmd, target);
Marc Zyngiereb781922016-12-20 14:47:05 +0000735 its_encode_vpt_addr(cmd, vpt_addr);
736 its_encode_vpt_size(cmd, LPI_NRBITS - 1);
737
Marc Zyngier64edfaa2019-12-24 11:10:29 +0000738 if (!is_v4_1(its))
739 goto out;
740
741 vconf_addr = virt_to_phys(page_address(desc->its_vmapp_cmd.vpe->its_vm->vprop_page));
742
743 alloc = !atomic_fetch_inc(&desc->its_vmapp_cmd.vpe->vmapp_count);
744
745 its_encode_alloc(cmd, alloc);
746
747 /* We can only signal PTZ when alloc==1. Why do we have two bits? */
748 its_encode_ptz(cmd, alloc);
749 its_encode_vconf_addr(cmd, vconf_addr);
750 its_encode_vmapp_default_db(cmd, desc->its_vmapp_cmd.vpe->vpe_db_lpi);
751
752out:
Marc Zyngiereb781922016-12-20 14:47:05 +0000753 its_fixup_cmd(cmd);
754
Marc Zyngier205e0652018-06-22 10:52:53 +0100755 return valid_vpe(its, desc->its_vmapp_cmd.vpe);
Marc Zyngiereb781922016-12-20 14:47:05 +0000756}
757
Marc Zyngier67047f902017-07-28 21:16:58 +0100758static struct its_vpe *its_build_vmapti_cmd(struct its_node *its,
759 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000760 struct its_cmd_desc *desc)
761{
762 u32 db;
763
Marc Zyngier3858d4d2019-12-24 11:10:37 +0000764 if (!is_v4_1(its) && desc->its_vmapti_cmd.db_enabled)
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000765 db = desc->its_vmapti_cmd.vpe->vpe_db_lpi;
766 else
767 db = 1023;
768
769 its_encode_cmd(cmd, GITS_CMD_VMAPTI);
770 its_encode_devid(cmd, desc->its_vmapti_cmd.dev->device_id);
771 its_encode_vpeid(cmd, desc->its_vmapti_cmd.vpe->vpe_id);
772 its_encode_event_id(cmd, desc->its_vmapti_cmd.event_id);
773 its_encode_db_phys_id(cmd, db);
774 its_encode_virt_id(cmd, desc->its_vmapti_cmd.virt_id);
775
776 its_fixup_cmd(cmd);
777
Marc Zyngier205e0652018-06-22 10:52:53 +0100778 return valid_vpe(its, desc->its_vmapti_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000779}
780
Marc Zyngier67047f902017-07-28 21:16:58 +0100781static struct its_vpe *its_build_vmovi_cmd(struct its_node *its,
782 struct its_cmd_block *cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000783 struct its_cmd_desc *desc)
784{
785 u32 db;
786
Marc Zyngier3858d4d2019-12-24 11:10:37 +0000787 if (!is_v4_1(its) && desc->its_vmovi_cmd.db_enabled)
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000788 db = desc->its_vmovi_cmd.vpe->vpe_db_lpi;
789 else
790 db = 1023;
791
792 its_encode_cmd(cmd, GITS_CMD_VMOVI);
793 its_encode_devid(cmd, desc->its_vmovi_cmd.dev->device_id);
794 its_encode_vpeid(cmd, desc->its_vmovi_cmd.vpe->vpe_id);
795 its_encode_event_id(cmd, desc->its_vmovi_cmd.event_id);
796 its_encode_db_phys_id(cmd, db);
797 its_encode_db_valid(cmd, true);
798
799 its_fixup_cmd(cmd);
800
Marc Zyngier205e0652018-06-22 10:52:53 +0100801 return valid_vpe(its, desc->its_vmovi_cmd.vpe);
Marc Zyngierd011e4e2016-12-20 09:44:41 +0000802}
803
Marc Zyngier67047f902017-07-28 21:16:58 +0100804static struct its_vpe *its_build_vmovp_cmd(struct its_node *its,
805 struct its_cmd_block *cmd,
Marc Zyngier3171a472016-12-20 15:17:28 +0000806 struct its_cmd_desc *desc)
807{
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100808 u64 target;
809
810 target = desc->its_vmovp_cmd.col->target_address + its->vlpi_redist_offset;
Marc Zyngier3171a472016-12-20 15:17:28 +0000811 its_encode_cmd(cmd, GITS_CMD_VMOVP);
812 its_encode_seq_num(cmd, desc->its_vmovp_cmd.seq_num);
813 its_encode_its_list(cmd, desc->its_vmovp_cmd.its_list);
814 its_encode_vpeid(cmd, desc->its_vmovp_cmd.vpe->vpe_id);
Marc Zyngier5c9a8822017-07-28 21:20:37 +0100815 its_encode_target(cmd, target);
Marc Zyngier3171a472016-12-20 15:17:28 +0000816
Marc Zyngierdd3f0502019-12-24 11:10:31 +0000817 if (is_v4_1(its)) {
818 its_encode_db(cmd, true);
819 its_encode_vmovp_default_db(cmd, desc->its_vmovp_cmd.vpe->vpe_db_lpi);
820 }
821
Marc Zyngier3171a472016-12-20 15:17:28 +0000822 its_fixup_cmd(cmd);
823
Marc Zyngier205e0652018-06-22 10:52:53 +0100824 return valid_vpe(its, desc->its_vmovp_cmd.vpe);
Marc Zyngier3171a472016-12-20 15:17:28 +0000825}
826
Marc Zyngier28614692019-11-08 16:58:02 +0000827static struct its_vpe *its_build_vinv_cmd(struct its_node *its,
828 struct its_cmd_block *cmd,
829 struct its_cmd_desc *desc)
830{
831 struct its_vlpi_map *map;
832
833 map = dev_event_to_vlpi_map(desc->its_inv_cmd.dev,
834 desc->its_inv_cmd.event_id);
835
836 its_encode_cmd(cmd, GITS_CMD_INV);
837 its_encode_devid(cmd, desc->its_inv_cmd.dev->device_id);
838 its_encode_event_id(cmd, desc->its_inv_cmd.event_id);
839
840 its_fixup_cmd(cmd);
841
842 return valid_vpe(its, map->vpe);
843}
844
Marc Zyngiered0e4aa2019-11-08 16:58:03 +0000845static struct its_vpe *its_build_vint_cmd(struct its_node *its,
846 struct its_cmd_block *cmd,
847 struct its_cmd_desc *desc)
848{
849 struct its_vlpi_map *map;
850
851 map = dev_event_to_vlpi_map(desc->its_int_cmd.dev,
852 desc->its_int_cmd.event_id);
853
854 its_encode_cmd(cmd, GITS_CMD_INT);
855 its_encode_devid(cmd, desc->its_int_cmd.dev->device_id);
856 its_encode_event_id(cmd, desc->its_int_cmd.event_id);
857
858 its_fixup_cmd(cmd);
859
860 return valid_vpe(its, map->vpe);
861}
862
863static struct its_vpe *its_build_vclear_cmd(struct its_node *its,
864 struct its_cmd_block *cmd,
865 struct its_cmd_desc *desc)
866{
867 struct its_vlpi_map *map;
868
869 map = dev_event_to_vlpi_map(desc->its_clear_cmd.dev,
870 desc->its_clear_cmd.event_id);
871
872 its_encode_cmd(cmd, GITS_CMD_CLEAR);
873 its_encode_devid(cmd, desc->its_clear_cmd.dev->device_id);
874 its_encode_event_id(cmd, desc->its_clear_cmd.event_id);
875
876 its_fixup_cmd(cmd);
877
878 return valid_vpe(its, map->vpe);
879}
880
Marc Zyngierd97c97b2019-12-24 11:10:33 +0000881static struct its_vpe *its_build_invdb_cmd(struct its_node *its,
882 struct its_cmd_block *cmd,
883 struct its_cmd_desc *desc)
884{
885 if (WARN_ON(!is_v4_1(its)))
886 return NULL;
887
888 its_encode_cmd(cmd, GITS_CMD_INVDB);
889 its_encode_vpeid(cmd, desc->its_invdb_cmd.vpe->vpe_id);
890
891 its_fixup_cmd(cmd);
892
893 return valid_vpe(its, desc->its_invdb_cmd.vpe);
894}
895
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000896static u64 its_cmd_ptr_to_offset(struct its_node *its,
897 struct its_cmd_block *ptr)
898{
899 return (ptr - its->cmd_base) * sizeof(*ptr);
900}
901
902static int its_queue_full(struct its_node *its)
903{
904 int widx;
905 int ridx;
906
907 widx = its->cmd_write - its->cmd_base;
908 ridx = readl_relaxed(its->base + GITS_CREADR) / sizeof(struct its_cmd_block);
909
910 /* This is incredibly unlikely to happen, unless the ITS locks up. */
911 if (((widx + 1) % ITS_CMD_QUEUE_NR_ENTRIES) == ridx)
912 return 1;
913
914 return 0;
915}
916
917static struct its_cmd_block *its_allocate_entry(struct its_node *its)
918{
919 struct its_cmd_block *cmd;
920 u32 count = 1000000; /* 1s! */
921
922 while (its_queue_full(its)) {
923 count--;
924 if (!count) {
925 pr_err_ratelimited("ITS queue not draining\n");
926 return NULL;
927 }
928 cpu_relax();
929 udelay(1);
930 }
931
932 cmd = its->cmd_write++;
933
934 /* Handle queue wrapping */
935 if (its->cmd_write == (its->cmd_base + ITS_CMD_QUEUE_NR_ENTRIES))
936 its->cmd_write = its->cmd_base;
937
Marc Zyngier34d677a2016-12-19 17:16:45 +0000938 /* Clear command */
939 cmd->raw_cmd[0] = 0;
940 cmd->raw_cmd[1] = 0;
941 cmd->raw_cmd[2] = 0;
942 cmd->raw_cmd[3] = 0;
943
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000944 return cmd;
945}
946
947static struct its_cmd_block *its_post_commands(struct its_node *its)
948{
949 u64 wr = its_cmd_ptr_to_offset(its, its->cmd_write);
950
951 writel_relaxed(wr, its->base + GITS_CWRITER);
952
953 return its->cmd_write;
954}
955
956static void its_flush_cmd(struct its_node *its, struct its_cmd_block *cmd)
957{
958 /*
959 * Make sure the commands written to memory are observable by
960 * the ITS.
961 */
962 if (its->flags & ITS_FLAGS_CMDQ_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +0000963 gic_flush_dcache_to_poc(cmd, sizeof(*cmd));
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000964 else
965 dsb(ishst);
966}
967
Marc Zyngiera19b4622017-08-04 17:45:50 +0100968static int its_wait_for_range_completion(struct its_node *its,
Heyi Guoa050fa52019-05-13 19:42:06 +0800969 u64 prev_idx,
Marc Zyngiera19b4622017-08-04 17:45:50 +0100970 struct its_cmd_block *to)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000971{
Heyi Guoa050fa52019-05-13 19:42:06 +0800972 u64 rd_idx, to_idx, linear_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000973 u32 count = 1000000; /* 1s! */
974
Heyi Guoa050fa52019-05-13 19:42:06 +0800975 /* Linearize to_idx if the command set has wrapped around */
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000976 to_idx = its_cmd_ptr_to_offset(its, to);
Heyi Guoa050fa52019-05-13 19:42:06 +0800977 if (to_idx < prev_idx)
978 to_idx += ITS_CMD_QUEUE_SZ;
979
980 linear_idx = prev_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000981
982 while (1) {
Heyi Guoa050fa52019-05-13 19:42:06 +0800983 s64 delta;
984
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000985 rd_idx = readl_relaxed(its->base + GITS_CREADR);
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100986
Heyi Guoa050fa52019-05-13 19:42:06 +0800987 /*
988 * Compute the read pointer progress, taking the
989 * potential wrap-around into account.
990 */
991 delta = rd_idx - prev_idx;
992 if (rd_idx < prev_idx)
993 delta += ITS_CMD_QUEUE_SZ;
Marc Zyngier9bdd8b12017-08-19 10:16:02 +0100994
Heyi Guoa050fa52019-05-13 19:42:06 +0800995 linear_idx += delta;
996 if (linear_idx >= to_idx)
Marc Zyngiercc2d3212014-11-24 14:35:11 +0000997 break;
998
999 count--;
1000 if (!count) {
Heyi Guoa050fa52019-05-13 19:42:06 +08001001 pr_err_ratelimited("ITS queue timeout (%llu %llu)\n",
1002 to_idx, linear_idx);
Marc Zyngiera19b4622017-08-04 17:45:50 +01001003 return -1;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001004 }
Heyi Guoa050fa52019-05-13 19:42:06 +08001005 prev_idx = rd_idx;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001006 cpu_relax();
1007 udelay(1);
1008 }
Marc Zyngiera19b4622017-08-04 17:45:50 +01001009
1010 return 0;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001011}
1012
Marc Zyngiere4f90942016-12-19 17:56:32 +00001013/* Warning, macro hell follows */
1014#define BUILD_SINGLE_CMD_FUNC(name, buildtype, synctype, buildfn) \
1015void name(struct its_node *its, \
1016 buildtype builder, \
1017 struct its_cmd_desc *desc) \
1018{ \
1019 struct its_cmd_block *cmd, *sync_cmd, *next_cmd; \
1020 synctype *sync_obj; \
1021 unsigned long flags; \
Heyi Guoa050fa52019-05-13 19:42:06 +08001022 u64 rd_idx; \
Marc Zyngiere4f90942016-12-19 17:56:32 +00001023 \
1024 raw_spin_lock_irqsave(&its->lock, flags); \
1025 \
1026 cmd = its_allocate_entry(its); \
1027 if (!cmd) { /* We're soooooo screewed... */ \
1028 raw_spin_unlock_irqrestore(&its->lock, flags); \
1029 return; \
1030 } \
Marc Zyngier67047f902017-07-28 21:16:58 +01001031 sync_obj = builder(its, cmd, desc); \
Marc Zyngiere4f90942016-12-19 17:56:32 +00001032 its_flush_cmd(its, cmd); \
1033 \
1034 if (sync_obj) { \
1035 sync_cmd = its_allocate_entry(its); \
1036 if (!sync_cmd) \
1037 goto post; \
1038 \
Marc Zyngier67047f902017-07-28 21:16:58 +01001039 buildfn(its, sync_cmd, sync_obj); \
Marc Zyngiere4f90942016-12-19 17:56:32 +00001040 its_flush_cmd(its, sync_cmd); \
1041 } \
1042 \
1043post: \
Heyi Guoa050fa52019-05-13 19:42:06 +08001044 rd_idx = readl_relaxed(its->base + GITS_CREADR); \
Marc Zyngiere4f90942016-12-19 17:56:32 +00001045 next_cmd = its_post_commands(its); \
1046 raw_spin_unlock_irqrestore(&its->lock, flags); \
1047 \
Heyi Guoa050fa52019-05-13 19:42:06 +08001048 if (its_wait_for_range_completion(its, rd_idx, next_cmd)) \
Marc Zyngiera19b4622017-08-04 17:45:50 +01001049 pr_err_ratelimited("ITS cmd %ps failed\n", builder); \
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001050}
1051
Marc Zyngier67047f902017-07-28 21:16:58 +01001052static void its_build_sync_cmd(struct its_node *its,
1053 struct its_cmd_block *sync_cmd,
Marc Zyngiere4f90942016-12-19 17:56:32 +00001054 struct its_collection *sync_col)
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001055{
Marc Zyngiere4f90942016-12-19 17:56:32 +00001056 its_encode_cmd(sync_cmd, GITS_CMD_SYNC);
1057 its_encode_target(sync_cmd, sync_col->target_address);
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001058
Marc Zyngiere4f90942016-12-19 17:56:32 +00001059 its_fixup_cmd(sync_cmd);
1060}
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001061
Marc Zyngiere4f90942016-12-19 17:56:32 +00001062static BUILD_SINGLE_CMD_FUNC(its_send_single_command, its_cmd_builder_t,
1063 struct its_collection, its_build_sync_cmd)
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001064
Marc Zyngier67047f902017-07-28 21:16:58 +01001065static void its_build_vsync_cmd(struct its_node *its,
1066 struct its_cmd_block *sync_cmd,
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001067 struct its_vpe *sync_vpe)
1068{
1069 its_encode_cmd(sync_cmd, GITS_CMD_VSYNC);
1070 its_encode_vpeid(sync_cmd, sync_vpe->vpe_id);
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001071
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001072 its_fixup_cmd(sync_cmd);
1073}
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001074
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001075static BUILD_SINGLE_CMD_FUNC(its_send_single_vcommand, its_cmd_vbuilder_t,
1076 struct its_vpe, its_build_vsync_cmd)
1077
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001078static void its_send_int(struct its_device *dev, u32 event_id)
1079{
1080 struct its_cmd_desc desc;
1081
1082 desc.its_int_cmd.dev = dev;
1083 desc.its_int_cmd.event_id = event_id;
1084
1085 its_send_single_command(dev->its, its_build_int_cmd, &desc);
1086}
1087
1088static void its_send_clear(struct its_device *dev, u32 event_id)
1089{
1090 struct its_cmd_desc desc;
1091
1092 desc.its_clear_cmd.dev = dev;
1093 desc.its_clear_cmd.event_id = event_id;
1094
1095 its_send_single_command(dev->its, its_build_clear_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001096}
1097
1098static void its_send_inv(struct its_device *dev, u32 event_id)
1099{
1100 struct its_cmd_desc desc;
1101
1102 desc.its_inv_cmd.dev = dev;
1103 desc.its_inv_cmd.event_id = event_id;
1104
1105 its_send_single_command(dev->its, its_build_inv_cmd, &desc);
1106}
1107
1108static void its_send_mapd(struct its_device *dev, int valid)
1109{
1110 struct its_cmd_desc desc;
1111
1112 desc.its_mapd_cmd.dev = dev;
1113 desc.its_mapd_cmd.valid = !!valid;
1114
1115 its_send_single_command(dev->its, its_build_mapd_cmd, &desc);
1116}
1117
1118static void its_send_mapc(struct its_node *its, struct its_collection *col,
1119 int valid)
1120{
1121 struct its_cmd_desc desc;
1122
1123 desc.its_mapc_cmd.col = col;
1124 desc.its_mapc_cmd.valid = !!valid;
1125
1126 its_send_single_command(its, its_build_mapc_cmd, &desc);
1127}
1128
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001129static void its_send_mapti(struct its_device *dev, u32 irq_id, u32 id)
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001130{
1131 struct its_cmd_desc desc;
1132
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001133 desc.its_mapti_cmd.dev = dev;
1134 desc.its_mapti_cmd.phys_id = irq_id;
1135 desc.its_mapti_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001136
Marc Zyngier6a25ad32016-12-20 15:52:26 +00001137 its_send_single_command(dev->its, its_build_mapti_cmd, &desc);
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001138}
1139
1140static void its_send_movi(struct its_device *dev,
1141 struct its_collection *col, u32 id)
1142{
1143 struct its_cmd_desc desc;
1144
1145 desc.its_movi_cmd.dev = dev;
1146 desc.its_movi_cmd.col = col;
Marc Zyngier591e5be2015-07-17 10:46:42 +01001147 desc.its_movi_cmd.event_id = id;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00001148
1149 its_send_single_command(dev->its, its_build_movi_cmd, &desc);
1150}
1151
1152static void its_send_discard(struct its_device *dev, u32 id)
1153{
1154 struct its_cmd_desc desc;
1155
1156 desc.its_discard_cmd.dev = dev;
1157 desc.its_discard_cmd.event_id = id;
1158
1159 its_send_single_command(dev->its, its_build_discard_cmd, &desc);
1160}
1161
1162static void its_send_invall(struct its_node *its, struct its_collection *col)
1163{
1164 struct its_cmd_desc desc;
1165
1166 desc.its_invall_cmd.col = col;
1167
1168 its_send_single_command(its, its_build_invall_cmd, &desc);
1169}
Marc Zyngierc48ed512014-11-24 14:35:12 +00001170
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001171static void its_send_vmapti(struct its_device *dev, u32 id)
1172{
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001173 struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001174 struct its_cmd_desc desc;
1175
1176 desc.its_vmapti_cmd.vpe = map->vpe;
1177 desc.its_vmapti_cmd.dev = dev;
1178 desc.its_vmapti_cmd.virt_id = map->vintid;
1179 desc.its_vmapti_cmd.event_id = id;
1180 desc.its_vmapti_cmd.db_enabled = map->db_enabled;
1181
1182 its_send_single_vcommand(dev->its, its_build_vmapti_cmd, &desc);
1183}
1184
1185static void its_send_vmovi(struct its_device *dev, u32 id)
1186{
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001187 struct its_vlpi_map *map = dev_event_to_vlpi_map(dev, id);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001188 struct its_cmd_desc desc;
1189
1190 desc.its_vmovi_cmd.vpe = map->vpe;
1191 desc.its_vmovi_cmd.dev = dev;
1192 desc.its_vmovi_cmd.event_id = id;
1193 desc.its_vmovi_cmd.db_enabled = map->db_enabled;
1194
1195 its_send_single_vcommand(dev->its, its_build_vmovi_cmd, &desc);
1196}
1197
Marc Zyngier75fd9512017-10-08 18:46:39 +01001198static void its_send_vmapp(struct its_node *its,
1199 struct its_vpe *vpe, bool valid)
Marc Zyngiereb781922016-12-20 14:47:05 +00001200{
1201 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +00001202
1203 desc.its_vmapp_cmd.vpe = vpe;
1204 desc.its_vmapp_cmd.valid = valid;
Marc Zyngier75fd9512017-10-08 18:46:39 +01001205 desc.its_vmapp_cmd.col = &its->collections[vpe->col_idx];
Marc Zyngiereb781922016-12-20 14:47:05 +00001206
Marc Zyngier75fd9512017-10-08 18:46:39 +01001207 its_send_single_vcommand(its, its_build_vmapp_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +00001208}
1209
Marc Zyngier3171a472016-12-20 15:17:28 +00001210static void its_send_vmovp(struct its_vpe *vpe)
1211{
Zenghui Yu84243122019-10-23 03:46:26 +00001212 struct its_cmd_desc desc = {};
Marc Zyngier3171a472016-12-20 15:17:28 +00001213 struct its_node *its;
1214 unsigned long flags;
1215 int col_id = vpe->col_idx;
1216
1217 desc.its_vmovp_cmd.vpe = vpe;
Marc Zyngier3171a472016-12-20 15:17:28 +00001218
1219 if (!its_list_map) {
1220 its = list_first_entry(&its_nodes, struct its_node, entry);
Marc Zyngier3171a472016-12-20 15:17:28 +00001221 desc.its_vmovp_cmd.col = &its->collections[col_id];
1222 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1223 return;
1224 }
1225
1226 /*
1227 * Yet another marvel of the architecture. If using the
1228 * its_list "feature", we need to make sure that all ITSs
1229 * receive all VMOVP commands in the same order. The only way
1230 * to guarantee this is to make vmovp a serialization point.
1231 *
1232 * Wall <-- Head.
1233 */
1234 raw_spin_lock_irqsave(&vmovp_lock, flags);
1235
1236 desc.its_vmovp_cmd.seq_num = vmovp_seq_num++;
Zenghui Yu84243122019-10-23 03:46:26 +00001237 desc.its_vmovp_cmd.its_list = get_its_list(vpe->its_vm);
Marc Zyngier3171a472016-12-20 15:17:28 +00001238
1239 /* Emit VMOVPs */
1240 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00001241 if (!is_v4(its))
Marc Zyngier3171a472016-12-20 15:17:28 +00001242 continue;
1243
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001244 if (!vpe->its_vm->vlpi_count[its->list_nr])
1245 continue;
1246
Marc Zyngier3171a472016-12-20 15:17:28 +00001247 desc.its_vmovp_cmd.col = &its->collections[col_id];
1248 its_send_single_vcommand(its, its_build_vmovp_cmd, &desc);
1249 }
1250
1251 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1252}
1253
Marc Zyngier40619a22017-10-08 15:16:09 +01001254static void its_send_vinvall(struct its_node *its, struct its_vpe *vpe)
Marc Zyngiereb781922016-12-20 14:47:05 +00001255{
1256 struct its_cmd_desc desc;
Marc Zyngiereb781922016-12-20 14:47:05 +00001257
1258 desc.its_vinvall_cmd.vpe = vpe;
Marc Zyngier40619a22017-10-08 15:16:09 +01001259 its_send_single_vcommand(its, its_build_vinvall_cmd, &desc);
Marc Zyngiereb781922016-12-20 14:47:05 +00001260}
1261
Marc Zyngier28614692019-11-08 16:58:02 +00001262static void its_send_vinv(struct its_device *dev, u32 event_id)
1263{
1264 struct its_cmd_desc desc;
1265
1266 /*
1267 * There is no real VINV command. This is just a normal INV,
1268 * with a VSYNC instead of a SYNC.
1269 */
1270 desc.its_inv_cmd.dev = dev;
1271 desc.its_inv_cmd.event_id = event_id;
1272
1273 its_send_single_vcommand(dev->its, its_build_vinv_cmd, &desc);
1274}
1275
Marc Zyngiered0e4aa2019-11-08 16:58:03 +00001276static void its_send_vint(struct its_device *dev, u32 event_id)
1277{
1278 struct its_cmd_desc desc;
1279
1280 /*
1281 * There is no real VINT command. This is just a normal INT,
1282 * with a VSYNC instead of a SYNC.
1283 */
1284 desc.its_int_cmd.dev = dev;
1285 desc.its_int_cmd.event_id = event_id;
1286
1287 its_send_single_vcommand(dev->its, its_build_vint_cmd, &desc);
1288}
1289
1290static void its_send_vclear(struct its_device *dev, u32 event_id)
1291{
1292 struct its_cmd_desc desc;
1293
1294 /*
1295 * There is no real VCLEAR command. This is just a normal CLEAR,
1296 * with a VSYNC instead of a SYNC.
1297 */
1298 desc.its_clear_cmd.dev = dev;
1299 desc.its_clear_cmd.event_id = event_id;
1300
1301 its_send_single_vcommand(dev->its, its_build_vclear_cmd, &desc);
1302}
1303
Marc Zyngierd97c97b2019-12-24 11:10:33 +00001304static void its_send_invdb(struct its_node *its, struct its_vpe *vpe)
1305{
1306 struct its_cmd_desc desc;
1307
1308 desc.its_invdb_cmd.vpe = vpe;
1309 its_send_single_vcommand(its, its_build_invdb_cmd, &desc);
1310}
1311
Marc Zyngierc48ed512014-11-24 14:35:12 +00001312/*
1313 * irqchip functions - assumes MSI, mostly.
1314 */
Marc Zyngier015ec032016-12-20 09:54:57 +00001315static void lpi_write_config(struct irq_data *d, u8 clr, u8 set)
Marc Zyngierc48ed512014-11-24 14:35:12 +00001316{
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001317 struct its_vlpi_map *map = get_vlpi_map(d);
Marc Zyngier015ec032016-12-20 09:54:57 +00001318 irq_hw_number_t hwirq;
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001319 void *va;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001320 u8 *cfg;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001321
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001322 if (map) {
1323 va = page_address(map->vm->vprop_page);
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001324 hwirq = map->vintid;
1325
1326 /* Remember the updated property */
1327 map->properties &= ~clr;
1328 map->properties |= set | LPI_PROP_GROUP1;
Marc Zyngier015ec032016-12-20 09:54:57 +00001329 } else {
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001330 va = gic_rdists->prop_table_va;
Marc Zyngier015ec032016-12-20 09:54:57 +00001331 hwirq = d->hwirq;
1332 }
Marc Zyngieradcdb942016-12-19 19:18:13 +00001333
Marc Zyngiere1a2e202018-07-27 14:36:00 +01001334 cfg = va + hwirq - 8192;
Marc Zyngieradcdb942016-12-19 19:18:13 +00001335 *cfg &= ~clr;
Marc Zyngier015ec032016-12-20 09:54:57 +00001336 *cfg |= set | LPI_PROP_GROUP1;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001337
1338 /*
1339 * Make the above write visible to the redistributors.
1340 * And yes, we're flushing exactly: One. Single. Byte.
1341 * Humpf...
1342 */
1343 if (gic_rdists->flags & RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING)
Vladimir Murzin328191c2016-11-02 11:54:05 +00001344 gic_flush_dcache_to_poc(cfg, sizeof(*cfg));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001345 else
1346 dsb(ishst);
Marc Zyngier015ec032016-12-20 09:54:57 +00001347}
1348
Marc Zyngier2f4f0642019-11-08 16:57:56 +00001349static void wait_for_syncr(void __iomem *rdbase)
1350{
1351 while (gic_read_lpir(rdbase + GICR_SYNCR) & 1)
1352 cpu_relax();
1353}
1354
Marc Zyngier425c09b2019-11-08 16:57:57 +00001355static void direct_lpi_inv(struct irq_data *d)
1356{
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001357 struct its_vlpi_map *map = get_vlpi_map(d);
Marc Zyngier425c09b2019-11-08 16:57:57 +00001358 void __iomem *rdbase;
Marc Zyngierf3a059212020-03-04 20:33:10 +00001359 unsigned long flags;
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001360 u64 val;
Marc Zyngierf3a059212020-03-04 20:33:10 +00001361 int cpu;
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001362
1363 if (map) {
1364 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1365
1366 WARN_ON(!is_v4_1(its_dev->its));
1367
1368 val = GICR_INVLPIR_V;
1369 val |= FIELD_PREP(GICR_INVLPIR_VPEID, map->vpe->vpe_id);
1370 val |= FIELD_PREP(GICR_INVLPIR_INTID, map->vintid);
1371 } else {
1372 val = d->hwirq;
1373 }
Marc Zyngier425c09b2019-11-08 16:57:57 +00001374
1375 /* Target the redistributor this LPI is currently routed to */
Marc Zyngierf3a059212020-03-04 20:33:10 +00001376 cpu = irq_to_cpuid_lock(d, &flags);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00001377 raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock);
Marc Zyngierf3a059212020-03-04 20:33:10 +00001378 rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base;
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001379 gic_write_lpir(val, rdbase + GICR_INVLPIR);
Marc Zyngier425c09b2019-11-08 16:57:57 +00001380
1381 wait_for_syncr(rdbase);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00001382 raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock);
Marc Zyngierf3a059212020-03-04 20:33:10 +00001383 irq_to_cpuid_unlock(d, flags);
Marc Zyngier425c09b2019-11-08 16:57:57 +00001384}
1385
Marc Zyngier015ec032016-12-20 09:54:57 +00001386static void lpi_update_config(struct irq_data *d, u8 clr, u8 set)
1387{
1388 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1389
1390 lpi_write_config(d, clr, set);
Marc Zyngierf4a81f52019-12-24 11:10:38 +00001391 if (gic_rdists->has_direct_lpi &&
1392 (is_v4_1(its_dev->its) || !irqd_is_forwarded_to_vcpu(d)))
Marc Zyngier425c09b2019-11-08 16:57:57 +00001393 direct_lpi_inv(d);
Marc Zyngier28614692019-11-08 16:58:02 +00001394 else if (!irqd_is_forwarded_to_vcpu(d))
Marc Zyngier425c09b2019-11-08 16:57:57 +00001395 its_send_inv(its_dev, its_get_event_id(d));
Marc Zyngier28614692019-11-08 16:58:02 +00001396 else
1397 its_send_vinv(its_dev, its_get_event_id(d));
Marc Zyngierc48ed512014-11-24 14:35:12 +00001398}
1399
Marc Zyngier015ec032016-12-20 09:54:57 +00001400static void its_vlpi_set_doorbell(struct irq_data *d, bool enable)
1401{
1402 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1403 u32 event = its_get_event_id(d);
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001404 struct its_vlpi_map *map;
Marc Zyngier015ec032016-12-20 09:54:57 +00001405
Marc Zyngier3858d4d2019-12-24 11:10:37 +00001406 /*
1407 * GICv4.1 does away with the per-LPI nonsense, nothing to do
1408 * here.
1409 */
1410 if (is_v4_1(its_dev->its))
1411 return;
1412
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001413 map = dev_event_to_vlpi_map(its_dev, event);
1414
1415 if (map->db_enabled == enable)
Marc Zyngier015ec032016-12-20 09:54:57 +00001416 return;
1417
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001418 map->db_enabled = enable;
Marc Zyngier015ec032016-12-20 09:54:57 +00001419
1420 /*
1421 * More fun with the architecture:
1422 *
1423 * Ideally, we'd issue a VMAPTI to set the doorbell to its LPI
1424 * value or to 1023, depending on the enable bit. But that
1425 * would be issueing a mapping for an /existing/ DevID+EventID
1426 * pair, which is UNPREDICTABLE. Instead, let's issue a VMOVI
1427 * to the /same/ vPE, using this opportunity to adjust the
1428 * doorbell. Mouahahahaha. We loves it, Precious.
1429 */
1430 its_send_vmovi(its_dev, event);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001431}
1432
1433static void its_mask_irq(struct irq_data *d)
1434{
Marc Zyngier015ec032016-12-20 09:54:57 +00001435 if (irqd_is_forwarded_to_vcpu(d))
1436 its_vlpi_set_doorbell(d, false);
1437
Marc Zyngieradcdb942016-12-19 19:18:13 +00001438 lpi_update_config(d, LPI_PROP_ENABLED, 0);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001439}
1440
1441static void its_unmask_irq(struct irq_data *d)
1442{
Marc Zyngier015ec032016-12-20 09:54:57 +00001443 if (irqd_is_forwarded_to_vcpu(d))
1444 its_vlpi_set_doorbell(d, true);
1445
Marc Zyngieradcdb942016-12-19 19:18:13 +00001446 lpi_update_config(d, 0, LPI_PROP_ENABLED);
Marc Zyngierc48ed512014-11-24 14:35:12 +00001447}
1448
Marc Zyngierc48ed512014-11-24 14:35:12 +00001449static int its_set_affinity(struct irq_data *d, const struct cpumask *mask_val,
1450 bool force)
1451{
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001452 unsigned int cpu;
1453 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngierc48ed512014-11-24 14:35:12 +00001454 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1455 struct its_collection *target_col;
1456 u32 id = its_get_event_id(d);
1457
Marc Zyngier015ec032016-12-20 09:54:57 +00001458 /* A forwarded interrupt should use irq_set_vcpu_affinity */
1459 if (irqd_is_forwarded_to_vcpu(d))
1460 return -EINVAL;
1461
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02001462 /* lpi cannot be routed to a redistributor that is on a foreign node */
1463 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
1464 if (its_dev->its->numa_node >= 0) {
1465 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
1466 if (!cpumask_intersects(mask_val, cpu_mask))
1467 return -EINVAL;
1468 }
1469 }
1470
1471 cpu = cpumask_any_and(mask_val, cpu_mask);
1472
Marc Zyngierc48ed512014-11-24 14:35:12 +00001473 if (cpu >= nr_cpu_ids)
1474 return -EINVAL;
1475
MaJun8b8d94a2017-05-18 16:19:13 +08001476 /* don't set the affinity when the target cpu is same as current one */
1477 if (cpu != its_dev->event_map.col_map[id]) {
1478 target_col = &its_dev->its->collections[cpu];
1479 its_send_movi(its_dev, target_col, id);
1480 its_dev->event_map.col_map[id] = cpu;
Marc Zyngier0d224d32017-08-18 09:39:18 +01001481 irq_data_update_effective_affinity(d, cpumask_of(cpu));
MaJun8b8d94a2017-05-18 16:19:13 +08001482 }
Marc Zyngierc48ed512014-11-24 14:35:12 +00001483
1484 return IRQ_SET_MASK_OK_DONE;
1485}
1486
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001487static u64 its_irq_get_msi_base(struct its_device *its_dev)
1488{
1489 struct its_node *its = its_dev->its;
1490
1491 return its->phys_base + GITS_TRANSLATER;
1492}
1493
Marc Zyngierb48ac832014-11-24 14:35:16 +00001494static void its_irq_compose_msi_msg(struct irq_data *d, struct msi_msg *msg)
1495{
1496 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1497 struct its_node *its;
1498 u64 addr;
1499
1500 its = its_dev->its;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01001501 addr = its->get_msi_base(its_dev);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001502
Vladimir Murzinb11283e2016-11-02 11:54:03 +00001503 msg->address_lo = lower_32_bits(addr);
1504 msg->address_hi = upper_32_bits(addr);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001505 msg->data = its_get_event_id(d);
Robin Murphy44bb7e22016-09-12 17:13:59 +01001506
Julien Grall35ae7df2019-05-01 14:58:21 +01001507 iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg);
Marc Zyngierb48ac832014-11-24 14:35:16 +00001508}
1509
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001510static int its_irq_set_irqchip_state(struct irq_data *d,
1511 enum irqchip_irq_state which,
1512 bool state)
1513{
1514 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1515 u32 event = its_get_event_id(d);
1516
1517 if (which != IRQCHIP_STATE_PENDING)
1518 return -EINVAL;
1519
Marc Zyngiered0e4aa2019-11-08 16:58:03 +00001520 if (irqd_is_forwarded_to_vcpu(d)) {
1521 if (state)
1522 its_send_vint(its_dev, event);
1523 else
1524 its_send_vclear(its_dev, event);
1525 } else {
1526 if (state)
1527 its_send_int(its_dev, event);
1528 else
1529 its_send_clear(its_dev, event);
1530 }
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001531
1532 return 0;
1533}
1534
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001535static void its_map_vm(struct its_node *its, struct its_vm *vm)
1536{
1537 unsigned long flags;
1538
1539 /* Not using the ITS list? Everything is always mapped. */
1540 if (!its_list_map)
1541 return;
1542
1543 raw_spin_lock_irqsave(&vmovp_lock, flags);
1544
1545 /*
1546 * If the VM wasn't mapped yet, iterate over the vpes and get
1547 * them mapped now.
1548 */
1549 vm->vlpi_count[its->list_nr]++;
1550
1551 if (vm->vlpi_count[its->list_nr] == 1) {
1552 int i;
1553
1554 for (i = 0; i < vm->nr_vpes; i++) {
1555 struct its_vpe *vpe = vm->vpes[i];
Marc Zyngier44c4c252017-10-19 10:11:34 +01001556 struct irq_data *d = irq_get_irq_data(vpe->irq);
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001557
1558 /* Map the VPE to the first possible CPU */
1559 vpe->col_idx = cpumask_first(cpu_online_mask);
1560 its_send_vmapp(its, vpe, true);
1561 its_send_vinvall(its, vpe);
Marc Zyngier44c4c252017-10-19 10:11:34 +01001562 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001563 }
1564 }
1565
1566 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1567}
1568
1569static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1570{
1571 unsigned long flags;
1572
1573 /* Not using the ITS list? Everything is always mapped. */
1574 if (!its_list_map)
1575 return;
1576
1577 raw_spin_lock_irqsave(&vmovp_lock, flags);
1578
1579 if (!--vm->vlpi_count[its->list_nr]) {
1580 int i;
1581
1582 for (i = 0; i < vm->nr_vpes; i++)
1583 its_send_vmapp(its, vm->vpes[i], false);
1584 }
1585
1586 raw_spin_unlock_irqrestore(&vmovp_lock, flags);
1587}
1588
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001589static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1590{
1591 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1592 u32 event = its_get_event_id(d);
1593 int ret = 0;
1594
1595 if (!info->map)
1596 return -EINVAL;
1597
Marc Zyngier11635fa2019-11-08 16:58:05 +00001598 raw_spin_lock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001599
1600 if (!its_dev->event_map.vm) {
1601 struct its_vlpi_map *maps;
1602
Kees Cook6396bb22018-06-12 14:03:40 -07001603 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
Marc Zyngier11635fa2019-11-08 16:58:05 +00001604 GFP_ATOMIC);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001605 if (!maps) {
1606 ret = -ENOMEM;
1607 goto out;
1608 }
1609
1610 its_dev->event_map.vm = info->map->vm;
1611 its_dev->event_map.vlpi_maps = maps;
1612 } else if (its_dev->event_map.vm != info->map->vm) {
1613 ret = -EINVAL;
1614 goto out;
1615 }
1616
1617 /* Get our private copy of the mapping information */
1618 its_dev->event_map.vlpi_maps[event] = *info->map;
1619
1620 if (irqd_is_forwarded_to_vcpu(d)) {
1621 /* Already mapped, move it around */
1622 its_send_vmovi(its_dev, event);
1623 } else {
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001624 /* Ensure all the VPEs are mapped on this ITS */
1625 its_map_vm(its_dev->its, info->map->vm);
1626
Marc Zyngierd4d7b4a2017-10-26 10:44:07 +01001627 /*
1628 * Flag the interrupt as forwarded so that we can
1629 * start poking the virtual property table.
1630 */
1631 irqd_set_forwarded_to_vcpu(d);
1632
1633 /* Write out the property to the prop table */
1634 lpi_write_config(d, 0xff, info->map->properties);
1635
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001636 /* Drop the physical mapping */
1637 its_send_discard(its_dev, event);
1638
1639 /* and install the virtual one */
1640 its_send_vmapti(its_dev, event);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001641
1642 /* Increment the number of VLPIs */
1643 its_dev->event_map.nr_vlpis++;
1644 }
1645
1646out:
Marc Zyngier11635fa2019-11-08 16:58:05 +00001647 raw_spin_unlock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001648 return ret;
1649}
1650
1651static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1652{
1653 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier046b5052019-11-08 16:58:04 +00001654 struct its_vlpi_map *map;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001655 int ret = 0;
1656
Marc Zyngier11635fa2019-11-08 16:58:05 +00001657 raw_spin_lock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001658
Marc Zyngier046b5052019-11-08 16:58:04 +00001659 map = get_vlpi_map(d);
1660
1661 if (!its_dev->event_map.vm || !map) {
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001662 ret = -EINVAL;
1663 goto out;
1664 }
1665
1666 /* Copy our mapping information to the incoming request */
Marc Zyngierc1d4d5c2019-11-08 16:58:01 +00001667 *info->map = *map;
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001668
1669out:
Marc Zyngier11635fa2019-11-08 16:58:05 +00001670 raw_spin_unlock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001671 return ret;
1672}
1673
1674static int its_vlpi_unmap(struct irq_data *d)
1675{
1676 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1677 u32 event = its_get_event_id(d);
1678 int ret = 0;
1679
Marc Zyngier11635fa2019-11-08 16:58:05 +00001680 raw_spin_lock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001681
1682 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d)) {
1683 ret = -EINVAL;
1684 goto out;
1685 }
1686
1687 /* Drop the virtual mapping */
1688 its_send_discard(its_dev, event);
1689
1690 /* and restore the physical one */
1691 irqd_clr_forwarded_to_vcpu(d);
1692 its_send_mapti(its_dev, d->hwirq, event);
1693 lpi_update_config(d, 0xff, (LPI_PROP_DEFAULT_PRIO |
1694 LPI_PROP_ENABLED |
1695 LPI_PROP_GROUP1));
1696
Marc Zyngier2247e1b2017-10-08 18:50:36 +01001697 /* Potentially unmap the VM from this ITS */
1698 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
1699
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001700 /*
1701 * Drop the refcount and make the device available again if
1702 * this was the last VLPI.
1703 */
1704 if (!--its_dev->event_map.nr_vlpis) {
1705 its_dev->event_map.vm = NULL;
1706 kfree(its_dev->event_map.vlpi_maps);
1707 }
1708
1709out:
Marc Zyngier11635fa2019-11-08 16:58:05 +00001710 raw_spin_unlock(&its_dev->event_map.vlpi_lock);
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001711 return ret;
1712}
1713
Marc Zyngier015ec032016-12-20 09:54:57 +00001714static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
1715{
1716 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1717
1718 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1719 return -EINVAL;
1720
1721 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
1722 lpi_update_config(d, 0xff, info->config);
1723 else
1724 lpi_write_config(d, 0xff, info->config);
1725 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
1726
1727 return 0;
1728}
1729
Marc Zyngierc808eea2016-12-20 09:31:20 +00001730static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
1731{
1732 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1733 struct its_cmd_info *info = vcpu_info;
1734
1735 /* Need a v4 ITS */
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00001736 if (!is_v4(its_dev->its))
Marc Zyngierc808eea2016-12-20 09:31:20 +00001737 return -EINVAL;
1738
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001739 /* Unmap request? */
1740 if (!info)
1741 return its_vlpi_unmap(d);
1742
Marc Zyngierc808eea2016-12-20 09:31:20 +00001743 switch (info->cmd_type) {
1744 case MAP_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001745 return its_vlpi_map(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001746
1747 case GET_VLPI:
Marc Zyngierd011e4e2016-12-20 09:44:41 +00001748 return its_vlpi_get(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001749
1750 case PROP_UPDATE_VLPI:
1751 case PROP_UPDATE_AND_INV_VLPI:
Marc Zyngier015ec032016-12-20 09:54:57 +00001752 return its_vlpi_prop_update(d, info);
Marc Zyngierc808eea2016-12-20 09:31:20 +00001753
1754 default:
1755 return -EINVAL;
1756 }
1757}
1758
Marc Zyngierc48ed512014-11-24 14:35:12 +00001759static struct irq_chip its_irq_chip = {
1760 .name = "ITS",
1761 .irq_mask = its_mask_irq,
1762 .irq_unmask = its_unmask_irq,
Ashok Kumar004fa082016-02-11 05:38:53 -08001763 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngierc48ed512014-11-24 14:35:12 +00001764 .irq_set_affinity = its_set_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001765 .irq_compose_msi_msg = its_irq_compose_msi_msg,
Marc Zyngier8d85dce2016-12-19 18:02:13 +00001766 .irq_set_irqchip_state = its_irq_set_irqchip_state,
Marc Zyngierc808eea2016-12-20 09:31:20 +00001767 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
Marc Zyngierb48ac832014-11-24 14:35:16 +00001768};
1769
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001770
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001771/*
1772 * How we allocate LPIs:
1773 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001774 * lpi_range_list contains ranges of LPIs that are to available to
1775 * allocate from. To allocate LPIs, just pick the first range that
1776 * fits the required allocation, and reduce it by the required
1777 * amount. Once empty, remove the range from the list.
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001778 *
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001779 * To free a range of LPIs, add a free range to the list, sort it and
1780 * merge the result if the new range happens to be adjacent to an
1781 * already free block.
1782 *
1783 * The consequence of the above is that allocation is cost is low, but
1784 * freeing is expensive. We assumes that freeing rarely occurs.
1785 */
Jia He4cb205c2018-08-28 12:53:26 +08001786#define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001787
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001788static DEFINE_MUTEX(lpi_range_lock);
1789static LIST_HEAD(lpi_range_list);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001790
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001791struct lpi_range {
1792 struct list_head entry;
1793 u32 base_id;
1794 u32 span;
1795};
1796
1797static struct lpi_range *mk_lpi_range(u32 base, u32 span)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001798{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001799 struct lpi_range *range;
1800
Rasmus Villemoes1c73fac2019-03-12 18:33:48 +01001801 range = kmalloc(sizeof(*range), GFP_KERNEL);
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001802 if (range) {
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001803 range->base_id = base;
1804 range->span = span;
1805 }
1806
1807 return range;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001808}
1809
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001810static int alloc_lpi_range(u32 nr_lpis, u32 *base)
1811{
1812 struct lpi_range *range, *tmp;
1813 int err = -ENOSPC;
1814
1815 mutex_lock(&lpi_range_lock);
1816
1817 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
1818 if (range->span >= nr_lpis) {
1819 *base = range->base_id;
1820 range->base_id += nr_lpis;
1821 range->span -= nr_lpis;
1822
1823 if (range->span == 0) {
1824 list_del(&range->entry);
1825 kfree(range);
1826 }
1827
1828 err = 0;
1829 break;
1830 }
1831 }
1832
1833 mutex_unlock(&lpi_range_lock);
1834
1835 pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
1836 return err;
1837}
1838
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001839static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b)
1840{
1841 if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list)
1842 return;
1843 if (a->base_id + a->span != b->base_id)
1844 return;
1845 b->base_id = a->base_id;
1846 b->span += a->span;
1847 list_del(&a->entry);
1848 kfree(a);
1849}
1850
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001851static int free_lpi_range(u32 base, u32 nr_lpis)
1852{
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001853 struct lpi_range *new, *old;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001854
1855 new = mk_lpi_range(base, nr_lpis);
Rasmus Villemoesb31a3832019-03-12 18:33:47 +01001856 if (!new)
1857 return -ENOMEM;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001858
1859 mutex_lock(&lpi_range_lock);
1860
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001861 list_for_each_entry_reverse(old, &lpi_range_list, entry) {
1862 if (old->base_id < base)
1863 break;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001864 }
Rasmus Villemoes12eade12019-03-12 18:33:49 +01001865 /*
1866 * old is the last element with ->base_id smaller than base,
1867 * so new goes right after it. If there are no elements with
1868 * ->base_id smaller than base, &old->entry ends up pointing
1869 * at the head of the list, and inserting new it the start of
1870 * the list is the right thing to do in that case as well.
1871 */
1872 list_add(&new->entry, &old->entry);
1873 /*
1874 * Now check if we can merge with the preceding and/or
1875 * following ranges.
1876 */
1877 merge_lpi_ranges(old, new);
1878 merge_lpi_ranges(new, list_next_entry(new, entry));
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001879
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001880 mutex_unlock(&lpi_range_lock);
Rasmus Villemoesb31a3832019-03-12 18:33:47 +01001881 return 0;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001882}
1883
Tomasz Nowicki04a0e4d2016-01-19 14:11:18 +01001884static int __init its_lpi_init(u32 id_bits)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001885{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001886 u32 lpis = (1UL << id_bits) - 8192;
Marc Zyngier12b29052018-05-31 09:01:59 +01001887 u32 numlpis;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001888 int err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001889
Marc Zyngier12b29052018-05-31 09:01:59 +01001890 numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
1891
1892 if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
1893 lpis = numlpis;
1894 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
1895 lpis);
1896 }
1897
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001898 /*
1899 * Initializing the allocator is just the same as freeing the
1900 * full range of LPIs.
1901 */
1902 err = free_lpi_range(8192, lpis);
1903 pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
1904 return err;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001905}
1906
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001907static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001908{
1909 unsigned long *bitmap = NULL;
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001910 int err = 0;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001911
1912 do {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001913 err = alloc_lpi_range(nr_irqs, base);
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001914 if (!err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001915 break;
1916
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001917 nr_irqs /= 2;
1918 } while (nr_irqs > 0);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001919
Marc Zyngier45725e02019-01-29 15:19:23 +00001920 if (!nr_irqs)
1921 err = -ENOSPC;
1922
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001923 if (err)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001924 goto out;
1925
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001926 bitmap = kcalloc(BITS_TO_LONGS(nr_irqs), sizeof (long), GFP_ATOMIC);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001927 if (!bitmap)
1928 goto out;
1929
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001930 *nr_ids = nr_irqs;
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001931
1932out:
Marc Zyngierc8415b92015-10-02 16:44:05 +01001933 if (!bitmap)
1934 *base = *nr_ids = 0;
1935
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001936 return bitmap;
1937}
1938
Marc Zyngier38dd7c42018-05-27 17:03:03 +01001939static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001940{
Marc Zyngier880cb3c2018-05-27 16:14:15 +01001941 WARN_ON(free_lpi_range(base, nr_ids));
Marc Zyngiercf2be8b2016-12-19 18:49:59 +00001942 kfree(bitmap);
Marc Zyngierbf9529f2014-11-24 14:35:13 +00001943}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001944
Marc Zyngier053be482018-07-27 15:02:27 +01001945static void gic_reset_prop_table(void *va)
1946{
1947 /* Priority 0xa0, Group-1, disabled */
1948 memset(va, LPI_PROP_DEFAULT_PRIO | LPI_PROP_GROUP1, LPI_PROPBASE_SZ);
1949
1950 /* Make sure the GIC will observe the written configuration */
1951 gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ);
1952}
1953
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001954static struct page *its_allocate_prop_table(gfp_t gfp_flags)
1955{
1956 struct page *prop_page;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001957
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001958 prop_page = alloc_pages(gfp_flags, get_order(LPI_PROPBASE_SZ));
1959 if (!prop_page)
1960 return NULL;
1961
Marc Zyngier053be482018-07-27 15:02:27 +01001962 gic_reset_prop_table(page_address(prop_page));
Marc Zyngier0e5ccf92016-12-19 18:15:05 +00001963
1964 return prop_page;
1965}
1966
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00001967static void its_free_prop_table(struct page *prop_page)
1968{
1969 free_pages((unsigned long)page_address(prop_page),
1970 get_order(LPI_PROPBASE_SZ));
1971}
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00001972
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01001973static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
1974{
1975 phys_addr_t start, end, addr_end;
1976 u64 i;
1977
1978 /*
1979 * We don't bother checking for a kdump kernel as by
1980 * construction, the LPI tables are out of this kernel's
1981 * memory map.
1982 */
1983 if (is_kdump_kernel())
1984 return true;
1985
1986 addr_end = addr + size - 1;
1987
1988 for_each_reserved_mem_region(i, &start, &end) {
1989 if (addr >= start && addr_end <= end)
1990 return true;
1991 }
1992
1993 /* Not found, not a good sign... */
1994 pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n",
1995 &addr, &addr_end);
1996 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
1997 return false;
1998}
1999
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01002000static int gic_reserve_range(phys_addr_t addr, unsigned long size)
2001{
2002 if (efi_enabled(EFI_CONFIG_TABLES))
2003 return efi_mem_reserve_persistent(addr, size);
2004
2005 return 0;
2006}
2007
Marc Zyngier11e37d32018-07-27 13:38:54 +01002008static int __init its_setup_lpi_prop_table(void)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002009{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002010 if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) {
2011 u64 val;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002012
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002013 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2014 lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1;
2015
2016 gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12);
2017 gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa,
2018 LPI_PROPBASE_SZ,
2019 MEMREMAP_WB);
2020 gic_reset_prop_table(gic_rdists->prop_table_va);
2021 } else {
2022 struct page *page;
2023
2024 lpi_id_bits = min_t(u32,
2025 GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
2026 ITS_MAX_LPI_NRBITS);
2027 page = its_allocate_prop_table(GFP_NOWAIT);
2028 if (!page) {
2029 pr_err("Failed to allocate PROPBASE\n");
2030 return -ENOMEM;
2031 }
2032
2033 gic_rdists->prop_table_pa = page_to_phys(page);
2034 gic_rdists->prop_table_va = page_address(page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01002035 WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa,
2036 LPI_PROPBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002037 }
2038
Marc Zyngiere1a2e202018-07-27 14:36:00 +01002039 pr_info("GICv3: using LPI property table @%pa\n",
2040 &gic_rdists->prop_table_pa);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002041
Shanker Donthineni6c31e122017-06-22 18:19:14 -05002042 return its_lpi_init(lpi_id_bits);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002043}
2044
2045static const char *its_base_type_string[] = {
2046 [GITS_BASER_TYPE_DEVICE] = "Devices",
2047 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
Marc Zyngier4f46de92016-12-20 15:50:14 +00002048 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002049 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
2050 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
2051 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
2052 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
2053};
2054
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002055static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
2056{
2057 u32 idx = baser - its->tables;
2058
Vladimir Murzin0968a612016-11-02 11:54:06 +00002059 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002060}
2061
2062static void its_write_baser(struct its_node *its, struct its_baser *baser,
2063 u64 val)
2064{
2065 u32 idx = baser - its->tables;
2066
Vladimir Murzin0968a612016-11-02 11:54:06 +00002067 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002068 baser->val = its_read_baser(its, baser);
2069}
2070
Shanker Donthineni93473592016-06-06 18:17:30 -05002071static int its_setup_baser(struct its_node *its, struct its_baser *baser,
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002072 u64 cache, u64 shr, u32 psz, u32 order,
2073 bool indirect)
Shanker Donthineni93473592016-06-06 18:17:30 -05002074{
2075 u64 val = its_read_baser(its, baser);
2076 u64 esz = GITS_BASER_ENTRY_SIZE(val);
2077 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05002078 u64 baser_phys, tmp;
Shanker Donthineni93473592016-06-06 18:17:30 -05002079 u32 alloc_pages;
Shanker Donthineni539d3782019-01-14 09:50:19 +00002080 struct page *page;
Shanker Donthineni93473592016-06-06 18:17:30 -05002081 void *base;
Shanker Donthineni93473592016-06-06 18:17:30 -05002082
2083retry_alloc_baser:
2084 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
2085 if (alloc_pages > GITS_BASER_PAGES_MAX) {
2086 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
2087 &its->phys_base, its_base_type_string[type],
2088 alloc_pages, GITS_BASER_PAGES_MAX);
2089 alloc_pages = GITS_BASER_PAGES_MAX;
2090 order = get_order(GITS_BASER_PAGES_MAX * psz);
2091 }
2092
Shanker Donthineni539d3782019-01-14 09:50:19 +00002093 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order);
2094 if (!page)
Shanker Donthineni93473592016-06-06 18:17:30 -05002095 return -ENOMEM;
2096
Shanker Donthineni539d3782019-01-14 09:50:19 +00002097 base = (void *)page_address(page);
Shanker Donthineni30ae9612017-10-09 11:46:55 -05002098 baser_phys = virt_to_phys(base);
2099
2100 /* Check if the physical address of the memory is above 48bits */
2101 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
2102
2103 /* 52bit PA is supported only when PageSize=64K */
2104 if (psz != SZ_64K) {
2105 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
2106 free_pages((unsigned long)base, order);
2107 return -ENXIO;
2108 }
2109
2110 /* Convert 52bit PA to 48bit field */
2111 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
2112 }
2113
Shanker Donthineni93473592016-06-06 18:17:30 -05002114retry_baser:
Shanker Donthineni30ae9612017-10-09 11:46:55 -05002115 val = (baser_phys |
Shanker Donthineni93473592016-06-06 18:17:30 -05002116 (type << GITS_BASER_TYPE_SHIFT) |
2117 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
2118 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
2119 cache |
2120 shr |
2121 GITS_BASER_VALID);
2122
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002123 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
2124
Shanker Donthineni93473592016-06-06 18:17:30 -05002125 switch (psz) {
2126 case SZ_4K:
2127 val |= GITS_BASER_PAGE_SIZE_4K;
2128 break;
2129 case SZ_16K:
2130 val |= GITS_BASER_PAGE_SIZE_16K;
2131 break;
2132 case SZ_64K:
2133 val |= GITS_BASER_PAGE_SIZE_64K;
2134 break;
2135 }
2136
2137 its_write_baser(its, baser, val);
2138 tmp = baser->val;
2139
2140 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
2141 /*
2142 * Shareability didn't stick. Just use
2143 * whatever the read reported, which is likely
2144 * to be the only thing this redistributor
2145 * supports. If that's zero, make it
2146 * non-cacheable as well.
2147 */
2148 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
2149 if (!shr) {
2150 cache = GITS_BASER_nC;
Vladimir Murzin328191c2016-11-02 11:54:05 +00002151 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
Shanker Donthineni93473592016-06-06 18:17:30 -05002152 }
2153 goto retry_baser;
2154 }
2155
2156 if ((val ^ tmp) & GITS_BASER_PAGE_SIZE_MASK) {
2157 /*
2158 * Page size didn't stick. Let's try a smaller
2159 * size and retry. If we reach 4K, then
2160 * something is horribly wrong...
2161 */
2162 free_pages((unsigned long)base, order);
2163 baser->base = NULL;
2164
2165 switch (psz) {
2166 case SZ_16K:
2167 psz = SZ_4K;
2168 goto retry_alloc_baser;
2169 case SZ_64K:
2170 psz = SZ_16K;
2171 goto retry_alloc_baser;
2172 }
2173 }
2174
2175 if (val != tmp) {
Vladimir Murzinb11283e2016-11-02 11:54:03 +00002176 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
Shanker Donthineni93473592016-06-06 18:17:30 -05002177 &its->phys_base, its_base_type_string[type],
Vladimir Murzinb11283e2016-11-02 11:54:03 +00002178 val, tmp);
Shanker Donthineni93473592016-06-06 18:17:30 -05002179 free_pages((unsigned long)base, order);
2180 return -ENXIO;
2181 }
2182
2183 baser->order = order;
2184 baser->base = base;
2185 baser->psz = psz;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002186 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
Shanker Donthineni93473592016-06-06 18:17:30 -05002187
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002188 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
Vladimir Murzind524eaa2016-11-02 11:54:04 +00002189 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
Shanker Donthineni93473592016-06-06 18:17:30 -05002190 its_base_type_string[type],
2191 (unsigned long)virt_to_phys(base),
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002192 indirect ? "indirect" : "flat", (int)esz,
Shanker Donthineni93473592016-06-06 18:17:30 -05002193 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
2194
2195 return 0;
2196}
2197
Marc Zyngier4cacac52016-12-19 18:18:34 +00002198static bool its_parse_indirect_baser(struct its_node *its,
2199 struct its_baser *baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002200 u32 psz, u32 *order, u32 ids)
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002201{
Marc Zyngier4cacac52016-12-19 18:18:34 +00002202 u64 tmp = its_read_baser(its, baser);
2203 u64 type = GITS_BASER_TYPE(tmp);
2204 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002205 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002206 u32 new_order = *order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002207 bool indirect = false;
2208
2209 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
2210 if ((esz << ids) > (psz * 2)) {
2211 /*
2212 * Find out whether hw supports a single or two-level table by
2213 * table by reading bit at offset '62' after writing '1' to it.
2214 */
2215 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
2216 indirect = !!(baser->val & GITS_BASER_INDIRECT);
2217
2218 if (indirect) {
2219 /*
2220 * The size of the lvl2 table is equal to ITS page size
2221 * which is 'psz'. For computing lvl1 table size,
2222 * subtract ID bits that sparse lvl2 table from 'ids'
2223 * which is reported by ITS hardware times lvl1 table
2224 * entry size.
2225 */
Vladimir Murzind524eaa2016-11-02 11:54:04 +00002226 ids -= ilog2(psz / (int)esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002227 esz = GITS_LVL1_ENTRY_SIZE;
2228 }
2229 }
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002230
2231 /*
2232 * Allocate as many entries as required to fit the
2233 * range of device IDs that the ITS can grok... The ID
2234 * space being incredibly sparse, this results in a
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002235 * massive waste of memory if two-level device table
2236 * feature is not supported by hardware.
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002237 */
2238 new_order = max_t(u32, get_order(esz << ids), new_order);
2239 if (new_order >= MAX_ORDER) {
2240 new_order = MAX_ORDER - 1;
Vladimir Murzind524eaa2016-11-02 11:54:04 +00002241 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
Marc Zyngier576a8342019-11-08 16:58:00 +00002242 pr_warn("ITS@%pa: %s Table too large, reduce ids %llu->%u\n",
Marc Zyngier4cacac52016-12-19 18:18:34 +00002243 &its->phys_base, its_base_type_string[type],
Marc Zyngier576a8342019-11-08 16:58:00 +00002244 device_ids(its), ids);
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002245 }
2246
2247 *order = new_order;
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002248
2249 return indirect;
Shanker Donthineni4b75c452016-06-06 18:17:29 -05002250}
2251
Marc Zyngier5e516842019-12-24 11:10:28 +00002252static u32 compute_common_aff(u64 val)
2253{
2254 u32 aff, clpiaff;
2255
2256 aff = FIELD_GET(GICR_TYPER_AFFINITY, val);
2257 clpiaff = FIELD_GET(GICR_TYPER_COMMON_LPI_AFF, val);
2258
2259 return aff & ~(GENMASK(31, 0) >> (clpiaff * 8));
2260}
2261
2262static u32 compute_its_aff(struct its_node *its)
2263{
2264 u64 val;
2265 u32 svpet;
2266
2267 /*
2268 * Reencode the ITS SVPET and MPIDR as a GICR_TYPER, and compute
2269 * the resulting affinity. We then use that to see if this match
2270 * our own affinity.
2271 */
2272 svpet = FIELD_GET(GITS_TYPER_SVPET, its->typer);
2273 val = FIELD_PREP(GICR_TYPER_COMMON_LPI_AFF, svpet);
2274 val |= FIELD_PREP(GICR_TYPER_AFFINITY, its->mpidr);
2275 return compute_common_aff(val);
2276}
2277
2278static struct its_node *find_sibling_its(struct its_node *cur_its)
2279{
2280 struct its_node *its;
2281 u32 aff;
2282
2283 if (!FIELD_GET(GITS_TYPER_SVPET, cur_its->typer))
2284 return NULL;
2285
2286 aff = compute_its_aff(cur_its);
2287
2288 list_for_each_entry(its, &its_nodes, entry) {
2289 u64 baser;
2290
2291 if (!is_v4_1(its) || its == cur_its)
2292 continue;
2293
2294 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2295 continue;
2296
2297 if (aff != compute_its_aff(its))
2298 continue;
2299
2300 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2301 baser = its->tables[2].val;
2302 if (!(baser & GITS_BASER_VALID))
2303 continue;
2304
2305 return its;
2306 }
2307
2308 return NULL;
2309}
2310
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002311static void its_free_tables(struct its_node *its)
2312{
2313 int i;
2314
2315 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni1a485f42016-02-01 20:19:44 -06002316 if (its->tables[i].base) {
2317 free_pages((unsigned long)its->tables[i].base,
2318 its->tables[i].order);
2319 its->tables[i].base = NULL;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002320 }
2321 }
2322}
2323
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05002324static int its_alloc_tables(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002325{
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002326 u64 shr = GITS_BASER_InnerShareable;
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002327 u64 cache = GITS_BASER_RaWaWb;
Shanker Donthineni93473592016-06-06 18:17:30 -05002328 u32 psz = SZ_64K;
2329 int err, i;
Robert Richter94100972015-09-21 22:58:38 +02002330
Ard Biesheuvelfa150012017-10-17 17:55:54 +01002331 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
2332 /* erratum 24313: ignore memory access type */
2333 cache = GITS_BASER_nCnB;
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002334
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002335 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
Shanker Donthineni2d81d422016-06-06 18:17:28 -05002336 struct its_baser *baser = its->tables + i;
2337 u64 val = its_read_baser(its, baser);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002338 u64 type = GITS_BASER_TYPE(val);
Shanker Donthineni93473592016-06-06 18:17:30 -05002339 u32 order = get_order(psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002340 bool indirect = false;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002341
Marc Zyngier4cacac52016-12-19 18:18:34 +00002342 switch (type) {
2343 case GITS_BASER_TYPE_NONE:
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002344 continue;
2345
Marc Zyngier4cacac52016-12-19 18:18:34 +00002346 case GITS_BASER_TYPE_DEVICE:
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002347 indirect = its_parse_indirect_baser(its, baser,
2348 psz, &order,
Marc Zyngier576a8342019-11-08 16:58:00 +00002349 device_ids(its));
Zenghui Yu8d565742019-02-10 05:24:10 +00002350 break;
2351
Marc Zyngier4cacac52016-12-19 18:18:34 +00002352 case GITS_BASER_TYPE_VCPU:
Marc Zyngier5e516842019-12-24 11:10:28 +00002353 if (is_v4_1(its)) {
2354 struct its_node *sibling;
2355
2356 WARN_ON(i != 2);
2357 if ((sibling = find_sibling_its(its))) {
2358 *baser = sibling->tables[2];
2359 its_write_baser(its, baser, baser->val);
2360 continue;
2361 }
2362 }
2363
Marc Zyngier4cacac52016-12-19 18:18:34 +00002364 indirect = its_parse_indirect_baser(its, baser,
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05002365 psz, &order,
2366 ITS_MAX_VPEID_BITS);
Marc Zyngier4cacac52016-12-19 18:18:34 +00002367 break;
2368 }
Marc Zyngierf54b97e2015-03-06 16:37:41 +00002369
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05002370 err = its_setup_baser(its, baser, cache, shr, psz, order, indirect);
Shanker Donthineni93473592016-06-06 18:17:30 -05002371 if (err < 0) {
2372 its_free_tables(its);
2373 return err;
Robert Richter30f21362015-09-21 22:58:34 +02002374 }
2375
Shanker Donthineni93473592016-06-06 18:17:30 -05002376 /* Update settings which will be used for next BASERn */
2377 psz = baser->psz;
2378 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
2379 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002380 }
2381
2382 return 0;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002383}
2384
Marc Zyngier5e516842019-12-24 11:10:28 +00002385static u64 inherit_vpe_l1_table_from_its(void)
2386{
2387 struct its_node *its;
2388 u64 val;
2389 u32 aff;
2390
2391 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2392 aff = compute_common_aff(val);
2393
2394 list_for_each_entry(its, &its_nodes, entry) {
2395 u64 baser, addr;
2396
2397 if (!is_v4_1(its))
2398 continue;
2399
2400 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2401 continue;
2402
2403 if (aff != compute_its_aff(its))
2404 continue;
2405
2406 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2407 baser = its->tables[2].val;
2408 if (!(baser & GITS_BASER_VALID))
2409 continue;
2410
2411 /* We have a winner! */
Zenghui Yu8b718d42020-02-06 15:57:07 +08002412 gic_data_rdist()->vpe_l1_base = its->tables[2].base;
2413
Marc Zyngier5e516842019-12-24 11:10:28 +00002414 val = GICR_VPROPBASER_4_1_VALID;
2415 if (baser & GITS_BASER_INDIRECT)
2416 val |= GICR_VPROPBASER_4_1_INDIRECT;
2417 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE,
2418 FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser));
2419 switch (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser)) {
2420 case GIC_PAGE_SIZE_64K:
2421 addr = GITS_BASER_ADDR_48_to_52(baser);
2422 break;
2423 default:
2424 addr = baser & GENMASK_ULL(47, 12);
2425 break;
2426 }
2427 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, addr >> 12);
2428 val |= FIELD_PREP(GICR_VPROPBASER_SHAREABILITY_MASK,
2429 FIELD_GET(GITS_BASER_SHAREABILITY_MASK, baser));
2430 val |= FIELD_PREP(GICR_VPROPBASER_INNER_CACHEABILITY_MASK,
2431 FIELD_GET(GITS_BASER_INNER_CACHEABILITY_MASK, baser));
2432 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, GITS_BASER_NR_PAGES(baser) - 1);
2433
2434 return val;
2435 }
2436
2437 return 0;
2438}
2439
2440static u64 inherit_vpe_l1_table_from_rd(cpumask_t **mask)
2441{
2442 u32 aff;
2443 u64 val;
2444 int cpu;
2445
2446 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2447 aff = compute_common_aff(val);
2448
2449 for_each_possible_cpu(cpu) {
2450 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
Marc Zyngier5e516842019-12-24 11:10:28 +00002451
2452 if (!base || cpu == smp_processor_id())
2453 continue;
2454
2455 val = gic_read_typer(base + GICR_TYPER);
Zenghui Yu4bccf1d2020-02-06 15:57:09 +08002456 if (aff != compute_common_aff(val))
Marc Zyngier5e516842019-12-24 11:10:28 +00002457 continue;
2458
2459 /*
2460 * At this point, we have a victim. This particular CPU
2461 * has already booted, and has an affinity that matches
2462 * ours wrt CommonLPIAff. Let's use its own VPROPBASER.
2463 * Make sure we don't write the Z bit in that case.
2464 */
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002465 val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
Marc Zyngier5e516842019-12-24 11:10:28 +00002466 val &= ~GICR_VPROPBASER_4_1_Z;
2467
Zenghui Yu8b718d42020-02-06 15:57:07 +08002468 gic_data_rdist()->vpe_l1_base = gic_data_rdist_cpu(cpu)->vpe_l1_base;
Marc Zyngier5e516842019-12-24 11:10:28 +00002469 *mask = gic_data_rdist_cpu(cpu)->vpe_table_mask;
2470
2471 return val;
2472 }
2473
2474 return 0;
2475}
2476
Zenghui Yu4e6437f2020-02-06 15:57:08 +08002477static bool allocate_vpe_l2_table(int cpu, u32 id)
2478{
2479 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
Marc Zyngier490d3322020-02-09 22:48:50 +00002480 unsigned int psz, esz, idx, npg, gpsz;
2481 u64 val;
Zenghui Yu4e6437f2020-02-06 15:57:08 +08002482 struct page *page;
2483 __le64 *table;
2484
2485 if (!gic_rdists->has_rvpeid)
2486 return true;
2487
Marc Zyngier28d160d2020-03-04 20:33:09 +00002488 /* Skip non-present CPUs */
2489 if (!base)
2490 return true;
2491
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002492 val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
Zenghui Yu4e6437f2020-02-06 15:57:08 +08002493
2494 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val) + 1;
2495 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2496 npg = FIELD_GET(GICR_VPROPBASER_4_1_SIZE, val) + 1;
2497
2498 switch (gpsz) {
2499 default:
2500 WARN_ON(1);
2501 /* fall through */
2502 case GIC_PAGE_SIZE_4K:
2503 psz = SZ_4K;
2504 break;
2505 case GIC_PAGE_SIZE_16K:
2506 psz = SZ_16K;
2507 break;
2508 case GIC_PAGE_SIZE_64K:
2509 psz = SZ_64K;
2510 break;
2511 }
2512
2513 /* Don't allow vpe_id that exceeds single, flat table limit */
2514 if (!(val & GICR_VPROPBASER_4_1_INDIRECT))
2515 return (id < (npg * psz / (esz * SZ_8)));
2516
2517 /* Compute 1st level table index & check if that exceeds table limit */
2518 idx = id >> ilog2(psz / (esz * SZ_8));
2519 if (idx >= (npg * psz / GITS_LVL1_ENTRY_SIZE))
2520 return false;
2521
2522 table = gic_data_rdist_cpu(cpu)->vpe_l1_base;
2523
2524 /* Allocate memory for 2nd level table */
2525 if (!table[idx]) {
2526 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(psz));
2527 if (!page)
2528 return false;
2529
2530 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2531 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2532 gic_flush_dcache_to_poc(page_address(page), psz);
2533
2534 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2535
2536 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2537 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2538 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
2539
2540 /* Ensure updated table contents are visible to RD hardware */
2541 dsb(sy);
2542 }
2543
2544 return true;
2545}
2546
Marc Zyngier5e516842019-12-24 11:10:28 +00002547static int allocate_vpe_l1_table(void)
2548{
2549 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2550 u64 val, gpsz, npg, pa;
2551 unsigned int psz = SZ_64K;
2552 unsigned int np, epp, esz;
2553 struct page *page;
2554
2555 if (!gic_rdists->has_rvpeid)
2556 return 0;
2557
2558 /*
2559 * if VPENDBASER.Valid is set, disable any previously programmed
2560 * VPE by setting PendingLast while clearing Valid. This has the
2561 * effect of making sure no doorbell will be generated and we can
2562 * then safely clear VPROPBASER.Valid.
2563 */
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002564 if (gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER) & GICR_VPENDBASER_Valid)
2565 gicr_write_vpendbaser(GICR_VPENDBASER_PendingLast,
Marc Zyngier5e516842019-12-24 11:10:28 +00002566 vlpi_base + GICR_VPENDBASER);
2567
2568 /*
2569 * If we can inherit the configuration from another RD, let's do
2570 * so. Otherwise, we have to go through the allocation process. We
2571 * assume that all RDs have the exact same requirements, as
2572 * nothing will work otherwise.
2573 */
2574 val = inherit_vpe_l1_table_from_rd(&gic_data_rdist()->vpe_table_mask);
2575 if (val & GICR_VPROPBASER_4_1_VALID)
2576 goto out;
2577
2578 gic_data_rdist()->vpe_table_mask = kzalloc(sizeof(cpumask_t), GFP_KERNEL);
2579 if (!gic_data_rdist()->vpe_table_mask)
2580 return -ENOMEM;
2581
2582 val = inherit_vpe_l1_table_from_its();
2583 if (val & GICR_VPROPBASER_4_1_VALID)
2584 goto out;
2585
2586 /* First probe the page size */
2587 val = FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, GIC_PAGE_SIZE_64K);
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002588 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2589 val = gicr_read_vpropbaser(vlpi_base + GICR_VPROPBASER);
Marc Zyngier5e516842019-12-24 11:10:28 +00002590 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2591 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val);
2592
2593 switch (gpsz) {
2594 default:
2595 gpsz = GIC_PAGE_SIZE_4K;
2596 /* fall through */
2597 case GIC_PAGE_SIZE_4K:
2598 psz = SZ_4K;
2599 break;
2600 case GIC_PAGE_SIZE_16K:
2601 psz = SZ_16K;
2602 break;
2603 case GIC_PAGE_SIZE_64K:
2604 psz = SZ_64K;
2605 break;
2606 }
2607
2608 /*
2609 * Start populating the register from scratch, including RO fields
2610 * (which we want to print in debug cases...)
2611 */
2612 val = 0;
2613 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, gpsz);
2614 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ENTRY_SIZE, esz);
2615
2616 /* How many entries per GIC page? */
2617 esz++;
2618 epp = psz / (esz * SZ_8);
2619
2620 /*
2621 * If we need more than just a single L1 page, flag the table
2622 * as indirect and compute the number of required L1 pages.
2623 */
2624 if (epp < ITS_MAX_VPEID) {
2625 int nl2;
2626
2627 val |= GICR_VPROPBASER_4_1_INDIRECT;
2628
2629 /* Number of L2 pages required to cover the VPEID space */
2630 nl2 = DIV_ROUND_UP(ITS_MAX_VPEID, epp);
2631
2632 /* Number of L1 pages to point to the L2 pages */
2633 npg = DIV_ROUND_UP(nl2 * SZ_8, psz);
2634 } else {
2635 npg = 1;
2636 }
2637
Zenghui Yue88bd312020-02-06 15:57:06 +08002638 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, npg - 1);
Marc Zyngier5e516842019-12-24 11:10:28 +00002639
2640 /* Right, that's the number of CPU pages we need for L1 */
2641 np = DIV_ROUND_UP(npg * psz, PAGE_SIZE);
2642
2643 pr_debug("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n",
2644 np, npg, psz, epp, esz);
2645 page = alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(np * PAGE_SIZE));
2646 if (!page)
2647 return -ENOMEM;
2648
Zenghui Yu8b718d42020-02-06 15:57:07 +08002649 gic_data_rdist()->vpe_l1_base = page_address(page);
Marc Zyngier5e516842019-12-24 11:10:28 +00002650 pa = virt_to_phys(page_address(page));
2651 WARN_ON(!IS_ALIGNED(pa, psz));
2652
2653 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, pa >> 12);
2654 val |= GICR_VPROPBASER_RaWb;
2655 val |= GICR_VPROPBASER_InnerShareable;
2656 val |= GICR_VPROPBASER_4_1_Z;
2657 val |= GICR_VPROPBASER_4_1_VALID;
2658
2659out:
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002660 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
Marc Zyngier5e516842019-12-24 11:10:28 +00002661 cpumask_set_cpu(smp_processor_id(), gic_data_rdist()->vpe_table_mask);
2662
2663 pr_debug("CPU%d: VPROPBASER = %llx %*pbl\n",
2664 smp_processor_id(), val,
2665 cpumask_pr_args(gic_data_rdist()->vpe_table_mask));
2666
2667 return 0;
2668}
2669
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002670static int its_alloc_collections(struct its_node *its)
2671{
Marc Zyngier83559b42018-06-22 10:52:52 +01002672 int i;
2673
Kees Cook6396bb22018-06-12 14:03:40 -07002674 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002675 GFP_KERNEL);
2676 if (!its->collections)
2677 return -ENOMEM;
2678
Marc Zyngier83559b42018-06-22 10:52:52 +01002679 for (i = 0; i < nr_cpu_ids; i++)
2680 its->collections[i].target_address = ~0ULL;
2681
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002682 return 0;
2683}
2684
Marc Zyngier7c297a22016-12-19 18:34:38 +00002685static struct page *its_allocate_pending_table(gfp_t gfp_flags)
2686{
2687 struct page *pend_page;
Marc Zyngieradaab502018-07-17 18:06:39 +01002688
Marc Zyngier7c297a22016-12-19 18:34:38 +00002689 pend_page = alloc_pages(gfp_flags | __GFP_ZERO,
Marc Zyngieradaab502018-07-17 18:06:39 +01002690 get_order(LPI_PENDBASE_SZ));
Marc Zyngier7c297a22016-12-19 18:34:38 +00002691 if (!pend_page)
2692 return NULL;
2693
2694 /* Make sure the GIC will observe the zero-ed page */
2695 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
2696
2697 return pend_page;
2698}
2699
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002700static void its_free_pending_table(struct page *pt)
2701{
Marc Zyngieradaab502018-07-17 18:06:39 +01002702 free_pages((unsigned long)page_address(pt), get_order(LPI_PENDBASE_SZ));
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00002703}
2704
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002705/*
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002706 * Booting with kdump and LPIs enabled is generally fine. Any other
2707 * case is wrong in the absence of firmware/EFI support.
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002708 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002709static bool enabled_lpis_allowed(void)
2710{
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002711 phys_addr_t addr;
2712 u64 val;
Marc Zyngierc6e2ccb2018-06-26 11:21:11 +01002713
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002714 /* Check whether the property table is in a reserved region */
2715 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2716 addr = val & GENMASK_ULL(51, 12);
2717
2718 return gic_check_reserved_range(addr, LPI_PROPBASE_SZ);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002719}
2720
Marc Zyngier11e37d32018-07-27 13:38:54 +01002721static int __init allocate_lpi_tables(void)
2722{
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002723 u64 val;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002724 int err, cpu;
2725
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002726 /*
2727 * If LPIs are enabled while we run this from the boot CPU,
2728 * flag the RD tables as pre-allocated if the stars do align.
2729 */
2730 val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR);
2731 if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) {
2732 gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED |
2733 RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING);
2734 pr_info("GICv3: Using preallocated redistributor tables\n");
2735 }
2736
Marc Zyngier11e37d32018-07-27 13:38:54 +01002737 err = its_setup_lpi_prop_table();
2738 if (err)
2739 return err;
2740
2741 /*
2742 * We allocate all the pending tables anyway, as we may have a
2743 * mix of RDs that have had LPIs enabled, and some that
2744 * don't. We'll free the unused ones as each CPU comes online.
2745 */
2746 for_each_possible_cpu(cpu) {
2747 struct page *pend_page;
2748
2749 pend_page = its_allocate_pending_table(GFP_NOWAIT);
2750 if (!pend_page) {
2751 pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu);
2752 return -ENOMEM;
2753 }
2754
2755 gic_data_rdist_cpu(cpu)->pend_page = pend_page;
2756 }
2757
2758 return 0;
2759}
2760
Marc Zyngiere64fab12019-12-24 11:10:35 +00002761static u64 its_clear_vpend_valid(void __iomem *vlpi_base, u64 clr, u64 set)
Heyi Guo64794502019-01-24 21:37:08 +08002762{
2763 u32 count = 1000000; /* 1s! */
2764 bool clean;
2765 u64 val;
2766
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002767 val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002768 val &= ~GICR_VPENDBASER_Valid;
Marc Zyngiere64fab12019-12-24 11:10:35 +00002769 val &= ~clr;
2770 val |= set;
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002771 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002772
2773 do {
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002774 val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002775 clean = !(val & GICR_VPENDBASER_Dirty);
2776 if (!clean) {
2777 count--;
2778 cpu_relax();
2779 udelay(1);
2780 }
2781 } while (!clean && count);
2782
Marc Zyngiere64fab12019-12-24 11:10:35 +00002783 if (unlikely(val & GICR_VPENDBASER_Dirty)) {
2784 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
2785 val |= GICR_VPENDBASER_PendingLast;
2786 }
2787
Heyi Guo64794502019-01-24 21:37:08 +08002788 return val;
2789}
2790
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002791static void its_cpu_init_lpis(void)
2792{
2793 void __iomem *rbase = gic_data_rdist_rd_base();
2794 struct page *pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002795 phys_addr_t paddr;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002796 u64 val, tmp;
2797
Marc Zyngier11e37d32018-07-27 13:38:54 +01002798 if (gic_data_rdist()->lpi_enabled)
2799 return;
2800
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002801 val = readl_relaxed(rbase + GICR_CTLR);
2802 if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) &&
2803 (val & GICR_CTLR_ENABLE_LPIS)) {
Marc Zyngierf842ca82018-07-27 16:03:31 +01002804 /*
2805 * Check that we get the same property table on all
2806 * RDs. If we don't, this is hopeless.
2807 */
2808 paddr = gicr_read_propbaser(rbase + GICR_PROPBASER);
2809 paddr &= GENMASK_ULL(51, 12);
2810 if (WARN_ON(gic_rdists->prop_table_pa != paddr))
2811 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
2812
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002813 paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER);
2814 paddr &= GENMASK_ULL(51, 16);
2815
Marc Zyngier5e2c9f92018-07-27 16:23:18 +01002816 WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002817 its_free_pending_table(gic_data_rdist()->pend_page);
2818 gic_data_rdist()->pend_page = NULL;
2819
2820 goto out;
2821 }
2822
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002823 pend_page = gic_data_rdist()->pend_page;
Marc Zyngier11e37d32018-07-27 13:38:54 +01002824 paddr = page_to_phys(pend_page);
Marc Zyngier3fb68fa2018-07-27 16:21:18 +01002825 WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ));
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002826
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002827 /* set PROPBASE */
Marc Zyngiere1a2e202018-07-27 14:36:00 +01002828 val = (gic_rdists->prop_table_pa |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002829 GICR_PROPBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002830 GICR_PROPBASER_RaWaWb |
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002831 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
2832
Vladimir Murzin0968a612016-11-02 11:54:06 +00002833 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
2834 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002835
2836 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00002837 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
2838 /*
2839 * The HW reports non-shareable, we must
2840 * remove the cacheability attributes as
2841 * well.
2842 */
2843 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
2844 GICR_PROPBASER_CACHEABILITY_MASK);
2845 val |= GICR_PROPBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002846 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002847 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002848 pr_info_once("GIC: using cache flushing for LPI property table\n");
2849 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
2850 }
2851
2852 /* set PENDBASE */
2853 val = (page_to_phys(pend_page) |
Marc Zyngier4ad3e362015-03-27 14:15:04 +00002854 GICR_PENDBASER_InnerShareable |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06002855 GICR_PENDBASER_RaWaWb);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002856
Vladimir Murzin0968a612016-11-02 11:54:06 +00002857 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
2858 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002859
2860 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
2861 /*
2862 * The HW reports non-shareable, we must remove the
2863 * cacheability attributes as well.
2864 */
2865 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
2866 GICR_PENDBASER_CACHEABILITY_MASK);
2867 val |= GICR_PENDBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00002868 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00002869 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002870
2871 /* Enable LPIs */
2872 val = readl_relaxed(rbase + GICR_CTLR);
2873 val |= GICR_CTLR_ENABLE_LPIS;
2874 writel_relaxed(val, rbase + GICR_CTLR);
2875
Marc Zyngier5e516842019-12-24 11:10:28 +00002876 if (gic_rdists->has_vlpis && !gic_rdists->has_rvpeid) {
Heyi Guo64794502019-01-24 21:37:08 +08002877 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2878
2879 /*
2880 * It's possible for CPU to receive VLPIs before it is
2881 * sheduled as a vPE, especially for the first CPU, and the
2882 * VLPI with INTID larger than 2^(IDbits+1) will be considered
2883 * as out of range and dropped by GIC.
2884 * So we initialize IDbits to known value to avoid VLPI drop.
2885 */
2886 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
2887 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
2888 smp_processor_id(), val);
Zenghui Yu5186a6c2020-02-06 15:57:11 +08002889 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
Heyi Guo64794502019-01-24 21:37:08 +08002890
2891 /*
2892 * Also clear Valid bit of GICR_VPENDBASER, in case some
2893 * ancient programming gets left in and has possibility of
2894 * corrupting memory.
2895 */
Marc Zyngiere64fab12019-12-24 11:10:35 +00002896 val = its_clear_vpend_valid(vlpi_base, 0, 0);
Heyi Guo64794502019-01-24 21:37:08 +08002897 }
2898
Marc Zyngier5e516842019-12-24 11:10:28 +00002899 if (allocate_vpe_l1_table()) {
2900 /*
2901 * If the allocation has failed, we're in massive trouble.
2902 * Disable direct injection, and pray that no VM was
2903 * already running...
2904 */
2905 gic_rdists->has_rvpeid = false;
2906 gic_rdists->has_vlpis = false;
2907 }
2908
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002909 /* Make sure the GIC has seen the above */
2910 dsb(sy);
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002911out:
Marc Zyngier11e37d32018-07-27 13:38:54 +01002912 gic_data_rdist()->lpi_enabled = true;
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002913 pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n",
Marc Zyngier11e37d32018-07-27 13:38:54 +01002914 smp_processor_id(),
Marc Zyngierc440a9d2018-07-27 15:40:13 +01002915 gic_data_rdist()->pend_page ? "allocated" : "reserved",
Marc Zyngier11e37d32018-07-27 13:38:54 +01002916 &paddr);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002917}
2918
Derek Basehore920181c2018-02-28 21:48:20 -08002919static void its_cpu_init_collection(struct its_node *its)
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002920{
Derek Basehore920181c2018-02-28 21:48:20 -08002921 int cpu = smp_processor_id();
2922 u64 target;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002923
Derek Basehore920181c2018-02-28 21:48:20 -08002924 /* avoid cross node collections and its mapping */
2925 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
2926 struct device_node *cpu_node;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002927
Derek Basehore920181c2018-02-28 21:48:20 -08002928 cpu_node = of_get_cpu_node(cpu, NULL);
2929 if (its->numa_node != NUMA_NO_NODE &&
2930 its->numa_node != of_node_to_nid(cpu_node))
2931 return;
2932 }
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002933
Derek Basehore920181c2018-02-28 21:48:20 -08002934 /*
2935 * We now have to bind each collection to its target
2936 * redistributor.
2937 */
2938 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002939 /*
Derek Basehore920181c2018-02-28 21:48:20 -08002940 * This ITS wants the physical address of the
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002941 * redistributor.
2942 */
Derek Basehore920181c2018-02-28 21:48:20 -08002943 target = gic_data_rdist()->phys_base;
2944 } else {
2945 /* This ITS wants a linear CPU number. */
2946 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2947 target = GICR_TYPER_CPU_NUMBER(target) << 16;
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002948 }
2949
Derek Basehore920181c2018-02-28 21:48:20 -08002950 /* Perform collection mapping */
2951 its->collections[cpu].target_address = target;
2952 its->collections[cpu].col_id = cpu;
2953
2954 its_send_mapc(its, &its->collections[cpu], 1);
2955 its_send_invall(its, &its->collections[cpu]);
2956}
2957
2958static void its_cpu_init_collections(void)
2959{
2960 struct its_node *its;
2961
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02002962 raw_spin_lock(&its_lock);
Derek Basehore920181c2018-02-28 21:48:20 -08002963
2964 list_for_each_entry(its, &its_nodes, entry)
2965 its_cpu_init_collection(its);
2966
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02002967 raw_spin_unlock(&its_lock);
Marc Zyngier1ac19ca2014-11-24 14:35:14 +00002968}
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002969
2970static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
2971{
2972 struct its_device *its_dev = NULL, *tmp;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002973 unsigned long flags;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002974
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002975 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002976
2977 list_for_each_entry(tmp, &its->its_device_list, entry) {
2978 if (tmp->device_id == dev_id) {
2979 its_dev = tmp;
2980 break;
2981 }
2982 }
2983
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00002984 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00002985
2986 return its_dev;
2987}
2988
Shanker Donthineni466b7d12016-03-09 22:10:49 -06002989static struct its_baser *its_get_baser(struct its_node *its, u32 type)
2990{
2991 int i;
2992
2993 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2994 if (GITS_BASER_TYPE(its->tables[i].val) == type)
2995 return &its->tables[i];
2996 }
2997
2998 return NULL;
2999}
3000
Shanker Donthineni539d3782019-01-14 09:50:19 +00003001static bool its_alloc_table_entry(struct its_node *its,
3002 struct its_baser *baser, u32 id)
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003003{
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003004 struct page *page;
3005 u32 esz, idx;
3006 __le64 *table;
3007
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003008 /* Don't allow device id that exceeds single, flat table limit */
3009 esz = GITS_BASER_ENTRY_SIZE(baser->val);
3010 if (!(baser->val & GITS_BASER_INDIRECT))
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003011 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003012
3013 /* Compute 1st level table index & check if that exceeds table limit */
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003014 idx = id >> ilog2(baser->psz / esz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003015 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
3016 return false;
3017
3018 table = baser->base;
3019
3020 /* Allocate memory for 2nd level table */
3021 if (!table[idx]) {
Shanker Donthineni539d3782019-01-14 09:50:19 +00003022 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
3023 get_order(baser->psz));
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003024 if (!page)
3025 return false;
3026
3027 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
3028 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00003029 gic_flush_dcache_to_poc(page_address(page), baser->psz);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003030
3031 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
3032
3033 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
3034 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
Vladimir Murzin328191c2016-11-02 11:54:05 +00003035 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003036
3037 /* Ensure updated table contents are visible to ITS hardware */
3038 dsb(sy);
3039 }
3040
3041 return true;
3042}
3043
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003044static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
3045{
3046 struct its_baser *baser;
3047
3048 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
3049
3050 /* Don't allow device id that exceeds ITS hardware limit */
3051 if (!baser)
Marc Zyngier576a8342019-11-08 16:58:00 +00003052 return (ilog2(dev_id) < device_ids(its));
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003053
Shanker Donthineni539d3782019-01-14 09:50:19 +00003054 return its_alloc_table_entry(its, baser, dev_id);
Marc Zyngier70cc81e2016-12-19 18:53:02 +00003055}
3056
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003057static bool its_alloc_vpe_table(u32 vpe_id)
3058{
3059 struct its_node *its;
Zenghui Yu4e6437f2020-02-06 15:57:08 +08003060 int cpu;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003061
3062 /*
3063 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
3064 * could try and only do it on ITSs corresponding to devices
3065 * that have interrupts targeted at this VPE, but the
3066 * complexity becomes crazy (and you have tons of memory
3067 * anyway, right?).
3068 */
3069 list_for_each_entry(its, &its_nodes, entry) {
3070 struct its_baser *baser;
3071
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00003072 if (!is_v4(its))
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003073 continue;
3074
3075 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
3076 if (!baser)
3077 return false;
3078
Shanker Donthineni539d3782019-01-14 09:50:19 +00003079 if (!its_alloc_table_entry(its, baser, vpe_id))
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003080 return false;
3081 }
3082
Zenghui Yu4e6437f2020-02-06 15:57:08 +08003083 /* Non v4.1? No need to iterate RDs and go back early. */
3084 if (!gic_rdists->has_rvpeid)
3085 return true;
3086
3087 /*
3088 * Make sure the L2 tables are allocated for all copies of
3089 * the L1 table on *all* v4.1 RDs.
3090 */
3091 for_each_possible_cpu(cpu) {
3092 if (!allocate_vpe_l2_table(cpu, vpe_id))
3093 return false;
3094 }
3095
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003096 return true;
3097}
3098
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003099static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003100 int nvecs, bool alloc_lpis)
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003101{
3102 struct its_device *dev;
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003103 unsigned long *lpi_map = NULL;
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003104 unsigned long flags;
Marc Zyngier591e5be2015-07-17 10:46:42 +01003105 u16 *col_map = NULL;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003106 void *itt;
3107 int lpi_base;
3108 int nr_lpis;
Marc Zyngierc8481262014-12-12 10:51:24 +00003109 int nr_ites;
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003110 int sz;
3111
Shanker Donthineni3faf24e2016-06-06 18:17:32 -05003112 if (!its_alloc_device_table(its, dev_id))
Shanker Donthineni466b7d12016-03-09 22:10:49 -06003113 return NULL;
3114
Marc Zyngier147c8f32018-05-27 16:39:55 +01003115 if (WARN_ON(!is_power_of_2(nvecs)))
3116 nvecs = roundup_pow_of_two(nvecs);
3117
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003118 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Marc Zyngierc8481262014-12-12 10:51:24 +00003119 /*
Marc Zyngier147c8f32018-05-27 16:39:55 +01003120 * Even if the device wants a single LPI, the ITT must be
3121 * sized as a power of two (and you need at least one bit...).
Marc Zyngierc8481262014-12-12 10:51:24 +00003122 */
Marc Zyngier147c8f32018-05-27 16:39:55 +01003123 nr_ites = max(2, nvecs);
Marc Zyngierffedbf02019-11-08 16:57:59 +00003124 sz = nr_ites * (FIELD_GET(GITS_TYPER_ITT_ENTRY_SIZE, its->typer) + 1);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003125 sz = max(sz, ITS_ITT_ALIGN) + ITS_ITT_ALIGN - 1;
Shanker Donthineni539d3782019-01-14 09:50:19 +00003126 itt = kzalloc_node(sz, GFP_KERNEL, its->numa_node);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003127 if (alloc_lpis) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003128 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003129 if (lpi_map)
Kees Cook6396bb22018-06-12 14:03:40 -07003130 col_map = kcalloc(nr_lpis, sizeof(*col_map),
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003131 GFP_KERNEL);
3132 } else {
Kees Cook6396bb22018-06-12 14:03:40 -07003133 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003134 nr_lpis = 0;
3135 lpi_base = 0;
3136 }
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003137
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003138 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003139 kfree(dev);
3140 kfree(itt);
3141 kfree(lpi_map);
Marc Zyngier591e5be2015-07-17 10:46:42 +01003142 kfree(col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003143 return NULL;
3144 }
3145
Vladimir Murzin328191c2016-11-02 11:54:05 +00003146 gic_flush_dcache_to_poc(itt, sz);
Marc Zyngier5a9a8912015-09-13 12:14:32 +01003147
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003148 dev->its = its;
3149 dev->itt = itt;
Marc Zyngierc8481262014-12-12 10:51:24 +00003150 dev->nr_ites = nr_ites;
Marc Zyngier591e5be2015-07-17 10:46:42 +01003151 dev->event_map.lpi_map = lpi_map;
3152 dev->event_map.col_map = col_map;
3153 dev->event_map.lpi_base = lpi_base;
3154 dev->event_map.nr_lpis = nr_lpis;
Marc Zyngier11635fa2019-11-08 16:58:05 +00003155 raw_spin_lock_init(&dev->event_map.vlpi_lock);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003156 dev->device_id = dev_id;
3157 INIT_LIST_HEAD(&dev->entry);
3158
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003159 raw_spin_lock_irqsave(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003160 list_add(&dev->entry, &its->its_device_list);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003161 raw_spin_unlock_irqrestore(&its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003162
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003163 /* Map device to its ITT */
3164 its_send_mapd(dev, 1);
3165
3166 return dev;
3167}
3168
3169static void its_free_device(struct its_device *its_dev)
3170{
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003171 unsigned long flags;
3172
3173 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003174 list_del(&its_dev->entry);
Marc Zyngier3e39e8f52015-03-06 16:37:43 +00003175 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
Marc Zyngier898aa5c2019-11-08 16:57:55 +00003176 kfree(its_dev->event_map.col_map);
Marc Zyngier84a6a2e2014-11-24 14:35:15 +00003177 kfree(its_dev->itt);
3178 kfree(its_dev);
3179}
Marc Zyngierb48ac832014-11-24 14:35:16 +00003180
Marc Zyngier8208d172019-01-18 14:08:59 +00003181static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
Marc Zyngierb48ac832014-11-24 14:35:16 +00003182{
3183 int idx;
3184
Zenghui Yu342be102019-07-27 06:14:22 +00003185 /* Find a free LPI region in lpi_map and allocate them. */
Marc Zyngier8208d172019-01-18 14:08:59 +00003186 idx = bitmap_find_free_region(dev->event_map.lpi_map,
3187 dev->event_map.nr_lpis,
3188 get_count_order(nvecs));
3189 if (idx < 0)
Marc Zyngierb48ac832014-11-24 14:35:16 +00003190 return -ENOSPC;
3191
Marc Zyngier591e5be2015-07-17 10:46:42 +01003192 *hwirq = dev->event_map.lpi_base + idx;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003193
Marc Zyngierb48ac832014-11-24 14:35:16 +00003194 return 0;
3195}
3196
Marc Zyngier54456db2015-07-28 14:46:21 +01003197static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
3198 int nvec, msi_alloc_info_t *info)
Marc Zyngiere8137f42015-03-06 16:37:42 +00003199{
Marc Zyngierb48ac832014-11-24 14:35:16 +00003200 struct its_node *its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003201 struct its_device *its_dev;
Marc Zyngier54456db2015-07-28 14:46:21 +01003202 struct msi_domain_info *msi_info;
3203 u32 dev_id;
Marc Zyngier9791ec72019-01-29 10:02:33 +00003204 int err = 0;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003205
Marc Zyngier54456db2015-07-28 14:46:21 +01003206 /*
Julien Gralla7c90f52019-04-18 16:58:14 +01003207 * We ignore "dev" entirely, and rely on the dev_id that has
Marc Zyngier54456db2015-07-28 14:46:21 +01003208 * been passed via the scratchpad. This limits this domain's
3209 * usefulness to upper layers that definitely know that they
3210 * are built on top of the ITS.
3211 */
3212 dev_id = info->scratchpad[0].ul;
3213
3214 msi_info = msi_get_domain_info(domain);
3215 its = msi_info->data;
3216
Marc Zyngier20b3d542016-12-20 15:23:22 +00003217 if (!gic_rdists->has_direct_lpi &&
3218 vpe_proxy.dev &&
3219 vpe_proxy.dev->its == its &&
3220 dev_id == vpe_proxy.dev->device_id) {
3221 /* Bad luck. Get yourself a better implementation */
3222 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
3223 dev_id);
3224 return -EINVAL;
3225 }
3226
Marc Zyngier9791ec72019-01-29 10:02:33 +00003227 mutex_lock(&its->dev_alloc_lock);
Marc Zyngierf1304202015-07-28 14:46:18 +01003228 its_dev = its_find_device(its, dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00003229 if (its_dev) {
3230 /*
3231 * We already have seen this ID, probably through
3232 * another alias (PCI bridge of some sort). No need to
3233 * create the device.
3234 */
Marc Zyngier9791ec72019-01-29 10:02:33 +00003235 its_dev->shared = true;
Marc Zyngierf1304202015-07-28 14:46:18 +01003236 pr_debug("Reusing ITT for devID %x\n", dev_id);
Marc Zyngiere8137f42015-03-06 16:37:42 +00003237 goto out;
3238 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00003239
Marc Zyngier93f94ea2017-08-04 18:37:09 +01003240 its_dev = its_create_device(its, dev_id, nvec, true);
Marc Zyngier9791ec72019-01-29 10:02:33 +00003241 if (!its_dev) {
3242 err = -ENOMEM;
3243 goto out;
3244 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00003245
Marc Zyngierf1304202015-07-28 14:46:18 +01003246 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
Marc Zyngiere8137f42015-03-06 16:37:42 +00003247out:
Marc Zyngier9791ec72019-01-29 10:02:33 +00003248 mutex_unlock(&its->dev_alloc_lock);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003249 info->scratchpad[0].ptr = its_dev;
Marc Zyngier9791ec72019-01-29 10:02:33 +00003250 return err;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003251}
3252
Marc Zyngier54456db2015-07-28 14:46:21 +01003253static struct msi_domain_ops its_msi_domain_ops = {
3254 .msi_prepare = its_msi_prepare,
3255};
3256
Marc Zyngierb48ac832014-11-24 14:35:16 +00003257static int its_irq_gic_domain_alloc(struct irq_domain *domain,
3258 unsigned int virq,
3259 irq_hw_number_t hwirq)
3260{
Marc Zyngierf833f572015-10-13 12:51:33 +01003261 struct irq_fwspec fwspec;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003262
Marc Zyngierf833f572015-10-13 12:51:33 +01003263 if (irq_domain_get_of_node(domain->parent)) {
3264 fwspec.fwnode = domain->parent->fwnode;
3265 fwspec.param_count = 3;
3266 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
3267 fwspec.param[1] = hwirq;
3268 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02003269 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
3270 fwspec.fwnode = domain->parent->fwnode;
3271 fwspec.param_count = 2;
3272 fwspec.param[0] = hwirq;
3273 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
Marc Zyngierf833f572015-10-13 12:51:33 +01003274 } else {
3275 return -EINVAL;
3276 }
Marc Zyngierb48ac832014-11-24 14:35:16 +00003277
Marc Zyngierf833f572015-10-13 12:51:33 +01003278 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003279}
3280
3281static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
3282 unsigned int nr_irqs, void *args)
3283{
3284 msi_alloc_info_t *info = args;
3285 struct its_device *its_dev = info->scratchpad[0].ptr;
Julien Grall35ae7df2019-05-01 14:58:21 +01003286 struct its_node *its = its_dev->its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003287 irq_hw_number_t hwirq;
3288 int err;
3289 int i;
3290
Marc Zyngier8208d172019-01-18 14:08:59 +00003291 err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
3292 if (err)
3293 return err;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003294
Julien Grall35ae7df2019-05-01 14:58:21 +01003295 err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev));
3296 if (err)
3297 return err;
3298
Marc Zyngier8208d172019-01-18 14:08:59 +00003299 for (i = 0; i < nr_irqs; i++) {
3300 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003301 if (err)
3302 return err;
3303
3304 irq_domain_set_hwirq_and_chip(domain, virq + i,
Marc Zyngier8208d172019-01-18 14:08:59 +00003305 hwirq + i, &its_irq_chip, its_dev);
Marc Zyngier0d224d32017-08-18 09:39:18 +01003306 irqd_set_single_target(irq_desc_get_irq_data(irq_to_desc(virq + i)));
Marc Zyngierf1304202015-07-28 14:46:18 +01003307 pr_debug("ID:%d pID:%d vID:%d\n",
Marc Zyngier8208d172019-01-18 14:08:59 +00003308 (int)(hwirq + i - its_dev->event_map.lpi_base),
3309 (int)(hwirq + i), virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003310 }
3311
3312 return 0;
3313}
3314
Thomas Gleixner72491642017-09-13 23:29:10 +02003315static int its_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01003316 struct irq_data *d, bool reserve)
Marc Zyngieraca268d2014-12-12 10:51:23 +00003317{
3318 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3319 u32 event = its_get_event_id(d);
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003320 const struct cpumask *cpu_mask = cpu_online_mask;
Marc Zyngier0d224d32017-08-18 09:39:18 +01003321 int cpu;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02003322
3323 /* get the cpu_mask of local node */
3324 if (its_dev->its->numa_node >= 0)
3325 cpu_mask = cpumask_of_node(its_dev->its->numa_node);
Marc Zyngieraca268d2014-12-12 10:51:23 +00003326
Marc Zyngier591e5be2015-07-17 10:46:42 +01003327 /* Bind the LPI to the first possible CPU */
Yang Yingliangc1797b12018-06-22 10:52:51 +01003328 cpu = cpumask_first_and(cpu_mask, cpu_online_mask);
3329 if (cpu >= nr_cpu_ids) {
3330 if (its_dev->its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144)
3331 return -EINVAL;
3332
3333 cpu = cpumask_first(cpu_online_mask);
3334 }
3335
Marc Zyngier0d224d32017-08-18 09:39:18 +01003336 its_dev->event_map.col_map[event] = cpu;
3337 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngier591e5be2015-07-17 10:46:42 +01003338
Marc Zyngieraca268d2014-12-12 10:51:23 +00003339 /* Map the GIC IRQ and event to the device */
Marc Zyngier6a25ad32016-12-20 15:52:26 +00003340 its_send_mapti(its_dev, d->hwirq, event);
Thomas Gleixner72491642017-09-13 23:29:10 +02003341 return 0;
Marc Zyngieraca268d2014-12-12 10:51:23 +00003342}
3343
3344static void its_irq_domain_deactivate(struct irq_domain *domain,
3345 struct irq_data *d)
3346{
3347 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3348 u32 event = its_get_event_id(d);
3349
3350 /* Stop the delivery of interrupts */
3351 its_send_discard(its_dev, event);
3352}
3353
Marc Zyngierb48ac832014-11-24 14:35:16 +00003354static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
3355 unsigned int nr_irqs)
3356{
3357 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
3358 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
Marc Zyngier9791ec72019-01-29 10:02:33 +00003359 struct its_node *its = its_dev->its;
Marc Zyngierb48ac832014-11-24 14:35:16 +00003360 int i;
3361
Marc Zyngierc9c96e32019-09-05 14:56:47 +01003362 bitmap_release_region(its_dev->event_map.lpi_map,
3363 its_get_event_id(irq_domain_get_irq_data(domain, virq)),
3364 get_count_order(nr_irqs));
3365
Marc Zyngierb48ac832014-11-24 14:35:16 +00003366 for (i = 0; i < nr_irqs; i++) {
3367 struct irq_data *data = irq_domain_get_irq_data(domain,
3368 virq + i);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003369 /* Nuke the entry in the domain */
Marc Zyngier2da39942014-12-12 10:51:22 +00003370 irq_domain_reset_irq_data(data);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003371 }
3372
Marc Zyngier9791ec72019-01-29 10:02:33 +00003373 mutex_lock(&its->dev_alloc_lock);
3374
3375 /*
3376 * If all interrupts have been freed, start mopping the
3377 * floor. This is conditionned on the device not being shared.
3378 */
3379 if (!its_dev->shared &&
3380 bitmap_empty(its_dev->event_map.lpi_map,
Marc Zyngier591e5be2015-07-17 10:46:42 +01003381 its_dev->event_map.nr_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003382 its_lpi_free(its_dev->event_map.lpi_map,
3383 its_dev->event_map.lpi_base,
3384 its_dev->event_map.nr_lpis);
Marc Zyngierb48ac832014-11-24 14:35:16 +00003385
3386 /* Unmap device/itt */
3387 its_send_mapd(its_dev, 0);
3388 its_free_device(its_dev);
3389 }
3390
Marc Zyngier9791ec72019-01-29 10:02:33 +00003391 mutex_unlock(&its->dev_alloc_lock);
3392
Marc Zyngierb48ac832014-11-24 14:35:16 +00003393 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3394}
3395
3396static const struct irq_domain_ops its_domain_ops = {
3397 .alloc = its_irq_domain_alloc,
3398 .free = its_irq_domain_free,
Marc Zyngieraca268d2014-12-12 10:51:23 +00003399 .activate = its_irq_domain_activate,
3400 .deactivate = its_irq_domain_deactivate,
Marc Zyngierb48ac832014-11-24 14:35:16 +00003401};
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00003402
Marc Zyngier20b3d542016-12-20 15:23:22 +00003403/*
3404 * This is insane.
3405 *
Marc Zyngier0684c702019-12-24 11:10:30 +00003406 * If a GICv4.0 doesn't implement Direct LPIs (which is extremely
Marc Zyngier20b3d542016-12-20 15:23:22 +00003407 * likely), the only way to perform an invalidate is to use a fake
3408 * device to issue an INV command, implying that the LPI has first
3409 * been mapped to some event on that device. Since this is not exactly
3410 * cheap, we try to keep that mapping around as long as possible, and
3411 * only issue an UNMAP if we're short on available slots.
3412 *
3413 * Broken by design(tm).
Marc Zyngier0684c702019-12-24 11:10:30 +00003414 *
3415 * GICv4.1, on the other hand, mandates that we're able to invalidate
3416 * by writing to a MMIO register. It doesn't implement the whole of
3417 * DirectLPI, but that's good enough. And most of the time, we don't
3418 * even have to invalidate anything, as the redistributor can be told
3419 * whether to generate a doorbell or not (we thus leave it enabled,
3420 * always).
Marc Zyngier20b3d542016-12-20 15:23:22 +00003421 */
3422static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
3423{
Marc Zyngier0684c702019-12-24 11:10:30 +00003424 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3425 if (gic_rdists->has_rvpeid)
3426 return;
3427
Marc Zyngier20b3d542016-12-20 15:23:22 +00003428 /* Already unmapped? */
3429 if (vpe->vpe_proxy_event == -1)
3430 return;
3431
3432 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
3433 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
3434
3435 /*
3436 * We don't track empty slots at all, so let's move the
3437 * next_victim pointer if we can quickly reuse that slot
3438 * instead of nuking an existing entry. Not clear that this is
3439 * always a win though, and this might just generate a ripple
3440 * effect... Let's just hope VPEs don't migrate too often.
3441 */
3442 if (vpe_proxy.vpes[vpe_proxy.next_victim])
3443 vpe_proxy.next_victim = vpe->vpe_proxy_event;
3444
3445 vpe->vpe_proxy_event = -1;
3446}
3447
3448static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
3449{
Marc Zyngier0684c702019-12-24 11:10:30 +00003450 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3451 if (gic_rdists->has_rvpeid)
3452 return;
3453
Marc Zyngier20b3d542016-12-20 15:23:22 +00003454 if (!gic_rdists->has_direct_lpi) {
3455 unsigned long flags;
3456
3457 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3458 its_vpe_db_proxy_unmap_locked(vpe);
3459 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3460 }
3461}
3462
3463static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
3464{
Marc Zyngier0684c702019-12-24 11:10:30 +00003465 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3466 if (gic_rdists->has_rvpeid)
3467 return;
3468
Marc Zyngier20b3d542016-12-20 15:23:22 +00003469 /* Already mapped? */
3470 if (vpe->vpe_proxy_event != -1)
3471 return;
3472
3473 /* This slot was already allocated. Kick the other VPE out. */
3474 if (vpe_proxy.vpes[vpe_proxy.next_victim])
3475 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
3476
3477 /* Map the new VPE instead */
3478 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
3479 vpe->vpe_proxy_event = vpe_proxy.next_victim;
3480 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
3481
3482 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
3483 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
3484}
3485
Marc Zyngier958b90d2017-08-18 16:14:17 +01003486static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
3487{
3488 unsigned long flags;
3489 struct its_collection *target_col;
3490
Marc Zyngier0684c702019-12-24 11:10:30 +00003491 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3492 if (gic_rdists->has_rvpeid)
3493 return;
3494
Marc Zyngier958b90d2017-08-18 16:14:17 +01003495 if (gic_rdists->has_direct_lpi) {
3496 void __iomem *rdbase;
3497
3498 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
3499 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
Marc Zyngier2f4f0642019-11-08 16:57:56 +00003500 wait_for_syncr(rdbase);
Marc Zyngier958b90d2017-08-18 16:14:17 +01003501
3502 return;
3503 }
3504
3505 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3506
3507 its_vpe_db_proxy_map_locked(vpe);
3508
3509 target_col = &vpe_proxy.dev->its->collections[to];
3510 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
3511 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
3512
3513 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3514}
3515
Marc Zyngier3171a472016-12-20 15:17:28 +00003516static int its_vpe_set_affinity(struct irq_data *d,
3517 const struct cpumask *mask_val,
3518 bool force)
3519{
3520 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003521 int from, cpu = cpumask_first(mask_val);
Marc Zyngierf3a059212020-03-04 20:33:10 +00003522 unsigned long flags;
Marc Zyngier3171a472016-12-20 15:17:28 +00003523
3524 /*
3525 * Changing affinity is mega expensive, so let's be as lazy as
Marc Zyngier20b3d542016-12-20 15:23:22 +00003526 * we can and only do it if we really have to. Also, if mapped
Marc Zyngier958b90d2017-08-18 16:14:17 +01003527 * into the proxy device, we need to move the doorbell
3528 * interrupt to its new location.
Marc Zyngierf3a059212020-03-04 20:33:10 +00003529 *
3530 * Another thing is that changing the affinity of a vPE affects
3531 * *other interrupts* such as all the vLPIs that are routed to
3532 * this vPE. This means that the irq_desc lock is not enough to
3533 * protect us, and that we must ensure nobody samples vpe->col_idx
3534 * during the update, hence the lock below which must also be
3535 * taken on any vLPI handling path that evaluates vpe->col_idx.
Marc Zyngier3171a472016-12-20 15:17:28 +00003536 */
Marc Zyngierf3a059212020-03-04 20:33:10 +00003537 from = vpe_to_cpuid_lock(vpe, &flags);
3538 if (from == cpu)
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003539 goto out;
Marc Zyngier958b90d2017-08-18 16:14:17 +01003540
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003541 vpe->col_idx = cpu;
Marc Zyngier3171a472016-12-20 15:17:28 +00003542
Marc Zyngierdd3f0502019-12-24 11:10:31 +00003543 /*
3544 * GICv4.1 allows us to skip VMOVP if moving to a cpu whose RD
3545 * is sharing its VPE table with the current one.
3546 */
3547 if (gic_data_rdist_cpu(cpu)->vpe_table_mask &&
3548 cpumask_test_cpu(from, gic_data_rdist_cpu(cpu)->vpe_table_mask))
3549 goto out;
3550
3551 its_send_vmovp(vpe);
3552 its_vpe_db_proxy_move(vpe, from, cpu);
3553
3554out:
Marc Zyngier44c4c252017-10-19 10:11:34 +01003555 irq_data_update_effective_affinity(d, cpumask_of(cpu));
Marc Zyngierf3a059212020-03-04 20:33:10 +00003556 vpe_to_cpuid_unlock(vpe, flags);
Marc Zyngier44c4c252017-10-19 10:11:34 +01003557
Marc Zyngier3171a472016-12-20 15:17:28 +00003558 return IRQ_SET_MASK_OK_DONE;
3559}
3560
Marc Zyngiere643d802016-12-20 15:09:31 +00003561static void its_vpe_schedule(struct its_vpe *vpe)
3562{
Robin Murphy50c33092018-02-16 16:57:56 +00003563 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00003564 u64 val;
3565
3566 /* Schedule the VPE */
3567 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
3568 GENMASK_ULL(51, 12);
3569 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
3570 val |= GICR_VPROPBASER_RaWb;
3571 val |= GICR_VPROPBASER_InnerShareable;
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003572 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
Marc Zyngiere643d802016-12-20 15:09:31 +00003573
3574 val = virt_to_phys(page_address(vpe->vpt_page)) &
3575 GENMASK_ULL(51, 16);
3576 val |= GICR_VPENDBASER_RaWaWb;
3577 val |= GICR_VPENDBASER_NonShareable;
3578 /*
3579 * There is no good way of finding out if the pending table is
3580 * empty as we can race against the doorbell interrupt very
3581 * easily. So in the end, vpe->pending_last is only an
3582 * indication that the vcpu has something pending, not one
3583 * that the pending table is empty. A good implementation
3584 * would be able to read its coarse map pretty quickly anyway,
3585 * making this a tolerable issue.
3586 */
3587 val |= GICR_VPENDBASER_PendingLast;
3588 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
3589 val |= GICR_VPENDBASER_Valid;
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003590 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
Marc Zyngiere643d802016-12-20 15:09:31 +00003591}
3592
3593static void its_vpe_deschedule(struct its_vpe *vpe)
3594{
Robin Murphy50c33092018-02-16 16:57:56 +00003595 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
Marc Zyngiere643d802016-12-20 15:09:31 +00003596 u64 val;
3597
Marc Zyngiere64fab12019-12-24 11:10:35 +00003598 val = its_clear_vpend_valid(vlpi_base, 0, 0);
Marc Zyngiere643d802016-12-20 15:09:31 +00003599
Marc Zyngiere64fab12019-12-24 11:10:35 +00003600 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
3601 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
Marc Zyngiere643d802016-12-20 15:09:31 +00003602}
3603
Marc Zyngier40619a22017-10-08 15:16:09 +01003604static void its_vpe_invall(struct its_vpe *vpe)
3605{
3606 struct its_node *its;
3607
3608 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00003609 if (!is_v4(its))
Marc Zyngier40619a22017-10-08 15:16:09 +01003610 continue;
3611
Marc Zyngier2247e1b2017-10-08 18:50:36 +01003612 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
3613 continue;
3614
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01003615 /*
3616 * Sending a VINVALL to a single ITS is enough, as all
3617 * we need is to reach the redistributors.
3618 */
Marc Zyngier40619a22017-10-08 15:16:09 +01003619 its_send_vinvall(its, vpe);
Marc Zyngier3c1ccee2017-10-09 13:17:43 +01003620 return;
Marc Zyngier40619a22017-10-08 15:16:09 +01003621 }
3622}
3623
Marc Zyngiere643d802016-12-20 15:09:31 +00003624static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
3625{
3626 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3627 struct its_cmd_info *info = vcpu_info;
3628
3629 switch (info->cmd_type) {
3630 case SCHEDULE_VPE:
3631 its_vpe_schedule(vpe);
3632 return 0;
3633
3634 case DESCHEDULE_VPE:
3635 its_vpe_deschedule(vpe);
3636 return 0;
3637
Marc Zyngier5e2f7642016-12-20 15:10:50 +00003638 case INVALL_VPE:
Marc Zyngier40619a22017-10-08 15:16:09 +01003639 its_vpe_invall(vpe);
Marc Zyngier5e2f7642016-12-20 15:10:50 +00003640 return 0;
3641
Marc Zyngiere643d802016-12-20 15:09:31 +00003642 default:
3643 return -EINVAL;
3644 }
3645}
3646
Marc Zyngier20b3d542016-12-20 15:23:22 +00003647static void its_vpe_send_cmd(struct its_vpe *vpe,
3648 void (*cmd)(struct its_device *, u32))
3649{
3650 unsigned long flags;
3651
3652 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3653
3654 its_vpe_db_proxy_map_locked(vpe);
3655 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
3656
3657 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3658}
3659
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003660static void its_vpe_send_inv(struct irq_data *d)
3661{
3662 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003663
Marc Zyngier20b3d542016-12-20 15:23:22 +00003664 if (gic_rdists->has_direct_lpi) {
3665 void __iomem *rdbase;
3666
Marc Zyngier425c09b2019-11-08 16:57:57 +00003667 /* Target the redistributor this VPE is currently known on */
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003668 raw_spin_lock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003669 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
Marc Zyngier425c09b2019-11-08 16:57:57 +00003670 gic_write_lpir(d->parent_data->hwirq, rdbase + GICR_INVLPIR);
Marc Zyngier2f4f0642019-11-08 16:57:56 +00003671 wait_for_syncr(rdbase);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003672 raw_spin_unlock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngier20b3d542016-12-20 15:23:22 +00003673 } else {
3674 its_vpe_send_cmd(vpe, its_send_inv);
3675 }
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003676}
3677
3678static void its_vpe_mask_irq(struct irq_data *d)
3679{
3680 /*
3681 * We need to unmask the LPI, which is described by the parent
3682 * irq_data. Instead of calling into the parent (which won't
3683 * exactly do the right thing, let's simply use the
3684 * parent_data pointer. Yes, I'm naughty.
3685 */
3686 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
3687 its_vpe_send_inv(d);
3688}
3689
3690static void its_vpe_unmask_irq(struct irq_data *d)
3691{
3692 /* Same hack as above... */
3693 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
3694 its_vpe_send_inv(d);
3695}
3696
Marc Zyngiere57a3e282017-07-31 14:47:24 +01003697static int its_vpe_set_irqchip_state(struct irq_data *d,
3698 enum irqchip_irq_state which,
3699 bool state)
3700{
3701 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3702
3703 if (which != IRQCHIP_STATE_PENDING)
3704 return -EINVAL;
3705
3706 if (gic_rdists->has_direct_lpi) {
3707 void __iomem *rdbase;
3708
3709 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
3710 if (state) {
3711 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
3712 } else {
3713 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
Marc Zyngier2f4f0642019-11-08 16:57:56 +00003714 wait_for_syncr(rdbase);
Marc Zyngiere57a3e282017-07-31 14:47:24 +01003715 }
3716 } else {
3717 if (state)
3718 its_vpe_send_cmd(vpe, its_send_int);
3719 else
3720 its_vpe_send_cmd(vpe, its_send_clear);
3721 }
3722
3723 return 0;
3724}
3725
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003726static struct irq_chip its_vpe_irq_chip = {
3727 .name = "GICv4-vpe",
Marc Zyngierf6a91da2016-12-20 15:20:38 +00003728 .irq_mask = its_vpe_mask_irq,
3729 .irq_unmask = its_vpe_unmask_irq,
3730 .irq_eoi = irq_chip_eoi_parent,
Marc Zyngier3171a472016-12-20 15:17:28 +00003731 .irq_set_affinity = its_vpe_set_affinity,
Marc Zyngiere57a3e282017-07-31 14:47:24 +01003732 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
Marc Zyngiere643d802016-12-20 15:09:31 +00003733 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00003734};
3735
Marc Zyngierd97c97b2019-12-24 11:10:33 +00003736static struct its_node *find_4_1_its(void)
3737{
3738 static struct its_node *its = NULL;
3739
3740 if (!its) {
3741 list_for_each_entry(its, &its_nodes, entry) {
3742 if (is_v4_1(its))
3743 return its;
3744 }
3745
3746 /* Oops? */
3747 its = NULL;
3748 }
3749
3750 return its;
3751}
3752
3753static void its_vpe_4_1_send_inv(struct irq_data *d)
3754{
3755 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3756 struct its_node *its;
3757
3758 /*
3759 * GICv4.1 wants doorbells to be invalidated using the
3760 * INVDB command in order to be broadcast to all RDs. Send
3761 * it to the first valid ITS, and let the HW do its magic.
3762 */
3763 its = find_4_1_its();
3764 if (its)
3765 its_send_invdb(its, vpe);
3766}
3767
3768static void its_vpe_4_1_mask_irq(struct irq_data *d)
3769{
3770 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
3771 its_vpe_4_1_send_inv(d);
3772}
3773
3774static void its_vpe_4_1_unmask_irq(struct irq_data *d)
3775{
3776 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
3777 its_vpe_4_1_send_inv(d);
3778}
3779
Marc Zyngier91bf6392019-12-24 11:10:34 +00003780static void its_vpe_4_1_schedule(struct its_vpe *vpe,
3781 struct its_cmd_info *info)
3782{
3783 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3784 u64 val = 0;
3785
3786 /* Schedule the VPE */
3787 val |= GICR_VPENDBASER_Valid;
3788 val |= info->g0en ? GICR_VPENDBASER_4_1_VGRP0EN : 0;
3789 val |= info->g1en ? GICR_VPENDBASER_4_1_VGRP1EN : 0;
3790 val |= FIELD_PREP(GICR_VPENDBASER_4_1_VPEID, vpe->vpe_id);
3791
Zenghui Yu5186a6c2020-02-06 15:57:11 +08003792 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
Marc Zyngier91bf6392019-12-24 11:10:34 +00003793}
3794
Marc Zyngiere64fab12019-12-24 11:10:35 +00003795static void its_vpe_4_1_deschedule(struct its_vpe *vpe,
3796 struct its_cmd_info *info)
3797{
3798 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3799 u64 val;
3800
3801 if (info->req_db) {
3802 /*
3803 * vPE is going to block: make the vPE non-resident with
3804 * PendingLast clear and DB set. The GIC guarantees that if
3805 * we read-back PendingLast clear, then a doorbell will be
3806 * delivered when an interrupt comes.
3807 */
3808 val = its_clear_vpend_valid(vlpi_base,
3809 GICR_VPENDBASER_PendingLast,
3810 GICR_VPENDBASER_4_1_DB);
3811 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
3812 } else {
3813 /*
3814 * We're not blocking, so just make the vPE non-resident
3815 * with PendingLast set, indicating that we'll be back.
3816 */
3817 val = its_clear_vpend_valid(vlpi_base,
3818 0,
3819 GICR_VPENDBASER_PendingLast);
3820 vpe->pending_last = true;
3821 }
3822}
3823
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003824static void its_vpe_4_1_invall(struct its_vpe *vpe)
3825{
3826 void __iomem *rdbase;
3827 u64 val;
3828
3829 val = GICR_INVALLR_V;
3830 val |= FIELD_PREP(GICR_INVALLR_VPEID, vpe->vpe_id);
3831
3832 /* Target the redistributor this vPE is currently known on */
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003833 raw_spin_lock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003834 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
3835 gic_write_lpir(val, rdbase + GICR_INVALLR);
Zenghui Yub978c252020-03-04 20:33:11 +00003836
3837 wait_for_syncr(rdbase);
Marc Zyngier9058a4e2020-03-04 20:33:12 +00003838 raw_spin_unlock(&gic_data_rdist_cpu(vpe->col_idx)->rd_lock);
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003839}
3840
Marc Zyngier29c647f2019-12-24 11:10:32 +00003841static int its_vpe_4_1_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
3842{
Marc Zyngier91bf6392019-12-24 11:10:34 +00003843 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003844 struct its_cmd_info *info = vcpu_info;
3845
3846 switch (info->cmd_type) {
3847 case SCHEDULE_VPE:
Marc Zyngier91bf6392019-12-24 11:10:34 +00003848 its_vpe_4_1_schedule(vpe, info);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003849 return 0;
3850
3851 case DESCHEDULE_VPE:
Marc Zyngiere64fab12019-12-24 11:10:35 +00003852 its_vpe_4_1_deschedule(vpe, info);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003853 return 0;
3854
3855 case INVALL_VPE:
Marc Zyngierb4a4bd02019-12-24 11:10:36 +00003856 its_vpe_4_1_invall(vpe);
Marc Zyngier29c647f2019-12-24 11:10:32 +00003857 return 0;
3858
3859 default:
3860 return -EINVAL;
3861 }
3862}
3863
3864static struct irq_chip its_vpe_4_1_irq_chip = {
3865 .name = "GICv4.1-vpe",
Marc Zyngierd97c97b2019-12-24 11:10:33 +00003866 .irq_mask = its_vpe_4_1_mask_irq,
3867 .irq_unmask = its_vpe_4_1_unmask_irq,
Marc Zyngier29c647f2019-12-24 11:10:32 +00003868 .irq_eoi = irq_chip_eoi_parent,
3869 .irq_set_affinity = its_vpe_set_affinity,
3870 .irq_set_vcpu_affinity = its_vpe_4_1_set_vcpu_affinity,
3871};
3872
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003873static int its_vpe_id_alloc(void)
3874{
Shanker Donthineni32bd44d2017-10-07 15:43:48 -05003875 return ida_simple_get(&its_vpeid_ida, 0, ITS_MAX_VPEID, GFP_KERNEL);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003876}
3877
3878static void its_vpe_id_free(u16 id)
3879{
3880 ida_simple_remove(&its_vpeid_ida, id);
3881}
3882
3883static int its_vpe_init(struct its_vpe *vpe)
3884{
3885 struct page *vpt_page;
3886 int vpe_id;
3887
3888 /* Allocate vpe_id */
3889 vpe_id = its_vpe_id_alloc();
3890 if (vpe_id < 0)
3891 return vpe_id;
3892
3893 /* Allocate VPT */
3894 vpt_page = its_allocate_pending_table(GFP_KERNEL);
3895 if (!vpt_page) {
3896 its_vpe_id_free(vpe_id);
3897 return -ENOMEM;
3898 }
3899
3900 if (!its_alloc_vpe_table(vpe_id)) {
3901 its_vpe_id_free(vpe_id);
Nianyao Tang34f8eb92019-07-26 17:32:57 +08003902 its_free_pending_table(vpt_page);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003903 return -ENOMEM;
3904 }
3905
Marc Zyngierf3a059212020-03-04 20:33:10 +00003906 raw_spin_lock_init(&vpe->vpe_lock);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003907 vpe->vpe_id = vpe_id;
3908 vpe->vpt_page = vpt_page;
Marc Zyngier64edfaa2019-12-24 11:10:29 +00003909 if (gic_rdists->has_rvpeid)
3910 atomic_set(&vpe->vmapp_count, 0);
3911 else
3912 vpe->vpe_proxy_event = -1;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003913
3914 return 0;
3915}
3916
3917static void its_vpe_teardown(struct its_vpe *vpe)
3918{
Marc Zyngier20b3d542016-12-20 15:23:22 +00003919 its_vpe_db_proxy_unmap(vpe);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003920 its_vpe_id_free(vpe->vpe_id);
3921 its_free_pending_table(vpe->vpt_page);
3922}
3923
3924static void its_vpe_irq_domain_free(struct irq_domain *domain,
3925 unsigned int virq,
3926 unsigned int nr_irqs)
3927{
3928 struct its_vm *vm = domain->host_data;
3929 int i;
3930
3931 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3932
3933 for (i = 0; i < nr_irqs; i++) {
3934 struct irq_data *data = irq_domain_get_irq_data(domain,
3935 virq + i);
3936 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
3937
3938 BUG_ON(vm != vpe->its_vm);
3939
3940 clear_bit(data->hwirq, vm->db_bitmap);
3941 its_vpe_teardown(vpe);
3942 irq_domain_reset_irq_data(data);
3943 }
3944
3945 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003946 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003947 its_free_prop_table(vm->vprop_page);
3948 }
3949}
3950
3951static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
3952 unsigned int nr_irqs, void *args)
3953{
Marc Zyngier29c647f2019-12-24 11:10:32 +00003954 struct irq_chip *irqchip = &its_vpe_irq_chip;
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003955 struct its_vm *vm = args;
3956 unsigned long *bitmap;
3957 struct page *vprop_page;
3958 int base, nr_ids, i, err = 0;
3959
3960 BUG_ON(!vm);
3961
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003962 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003963 if (!bitmap)
3964 return -ENOMEM;
3965
3966 if (nr_ids < nr_irqs) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003967 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003968 return -ENOMEM;
3969 }
3970
3971 vprop_page = its_allocate_prop_table(GFP_KERNEL);
3972 if (!vprop_page) {
Marc Zyngier38dd7c42018-05-27 17:03:03 +01003973 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003974 return -ENOMEM;
3975 }
3976
3977 vm->db_bitmap = bitmap;
3978 vm->db_lpi_base = base;
3979 vm->nr_db_lpis = nr_ids;
3980 vm->vprop_page = vprop_page;
3981
Marc Zyngier29c647f2019-12-24 11:10:32 +00003982 if (gic_rdists->has_rvpeid)
3983 irqchip = &its_vpe_4_1_irq_chip;
3984
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003985 for (i = 0; i < nr_irqs; i++) {
3986 vm->vpes[i]->vpe_db_lpi = base + i;
3987 err = its_vpe_init(vm->vpes[i]);
3988 if (err)
3989 break;
3990 err = its_irq_gic_domain_alloc(domain, virq + i,
3991 vm->vpes[i]->vpe_db_lpi);
3992 if (err)
3993 break;
3994 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
Marc Zyngier29c647f2019-12-24 11:10:32 +00003995 irqchip, vm->vpes[i]);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00003996 set_bit(i, bitmap);
3997 }
3998
3999 if (err) {
4000 if (i > 0)
4001 its_vpe_irq_domain_free(domain, virq, i - 1);
4002
Marc Zyngier38dd7c42018-05-27 17:03:03 +01004003 its_lpi_free(bitmap, base, nr_ids);
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004004 its_free_prop_table(vprop_page);
4005 }
4006
4007 return err;
4008}
4009
Thomas Gleixner72491642017-09-13 23:29:10 +02004010static int its_vpe_irq_domain_activate(struct irq_domain *domain,
Thomas Gleixner702cb0a2017-12-29 16:59:06 +01004011 struct irq_data *d, bool reserve)
Marc Zyngiereb781922016-12-20 14:47:05 +00004012{
4013 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier40619a22017-10-08 15:16:09 +01004014 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00004015
Marc Zyngier2247e1b2017-10-08 18:50:36 +01004016 /* If we use the list map, we issue VMAPP on demand... */
4017 if (its_list_map)
Marc Zyngier6ef930f2017-11-07 10:04:38 +00004018 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00004019
4020 /* Map the VPE to the first possible CPU */
4021 vpe->col_idx = cpumask_first(cpu_online_mask);
Marc Zyngier40619a22017-10-08 15:16:09 +01004022
4023 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004024 if (!is_v4(its))
Marc Zyngier40619a22017-10-08 15:16:09 +01004025 continue;
4026
Marc Zyngier75fd9512017-10-08 18:46:39 +01004027 its_send_vmapp(its, vpe, true);
Marc Zyngier40619a22017-10-08 15:16:09 +01004028 its_send_vinvall(its, vpe);
4029 }
4030
Marc Zyngier44c4c252017-10-19 10:11:34 +01004031 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
4032
Thomas Gleixner72491642017-09-13 23:29:10 +02004033 return 0;
Marc Zyngiereb781922016-12-20 14:47:05 +00004034}
4035
4036static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
4037 struct irq_data *d)
4038{
4039 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
Marc Zyngier75fd9512017-10-08 18:46:39 +01004040 struct its_node *its;
Marc Zyngiereb781922016-12-20 14:47:05 +00004041
Marc Zyngier2247e1b2017-10-08 18:50:36 +01004042 /*
4043 * If we use the list map, we unmap the VPE once no VLPIs are
4044 * associated with the VM.
4045 */
4046 if (its_list_map)
4047 return;
4048
Marc Zyngier75fd9512017-10-08 18:46:39 +01004049 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004050 if (!is_v4(its))
Marc Zyngier75fd9512017-10-08 18:46:39 +01004051 continue;
4052
4053 its_send_vmapp(its, vpe, false);
4054 }
Marc Zyngiereb781922016-12-20 14:47:05 +00004055}
4056
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004057static const struct irq_domain_ops its_vpe_domain_ops = {
Marc Zyngier7d75bbb2016-12-20 13:55:54 +00004058 .alloc = its_vpe_irq_domain_alloc,
4059 .free = its_vpe_irq_domain_free,
Marc Zyngiereb781922016-12-20 14:47:05 +00004060 .activate = its_vpe_irq_domain_activate,
4061 .deactivate = its_vpe_irq_domain_deactivate,
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004062};
4063
Yun Wu4559fbb2015-03-06 16:37:50 +00004064static int its_force_quiescent(void __iomem *base)
4065{
4066 u32 count = 1000000; /* 1s */
4067 u32 val;
4068
4069 val = readl_relaxed(base + GITS_CTLR);
David Daney7611da82016-08-18 15:41:58 -07004070 /*
4071 * GIC architecture specification requires the ITS to be both
4072 * disabled and quiescent for writes to GITS_BASER<n> or
4073 * GITS_CBASER to not have UNPREDICTABLE results.
4074 */
4075 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
Yun Wu4559fbb2015-03-06 16:37:50 +00004076 return 0;
4077
4078 /* Disable the generation of all interrupts to this ITS */
Marc Zyngierd51c4b42017-06-27 21:24:25 +01004079 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
Yun Wu4559fbb2015-03-06 16:37:50 +00004080 writel_relaxed(val, base + GITS_CTLR);
4081
4082 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
4083 while (1) {
4084 val = readl_relaxed(base + GITS_CTLR);
4085 if (val & GITS_CTLR_QUIESCENT)
4086 return 0;
4087
4088 count--;
4089 if (!count)
4090 return -EBUSY;
4091
4092 cpu_relax();
4093 udelay(1);
4094 }
4095}
4096
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004097static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
Robert Richter94100972015-09-21 22:58:38 +02004098{
4099 struct its_node *its = data;
4100
Marc Zyngier576a8342019-11-08 16:58:00 +00004101 /* erratum 22375: only alloc 8MB table size (20 bits) */
4102 its->typer &= ~GITS_TYPER_DEVBITS;
4103 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, 20 - 1);
Robert Richter94100972015-09-21 22:58:38 +02004104 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004105
4106 return true;
Robert Richter94100972015-09-21 22:58:38 +02004107}
4108
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004109static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02004110{
4111 struct its_node *its = data;
4112
4113 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004114
4115 return true;
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02004116}
4117
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004118static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
Shanker Donthineni90922a22017-03-07 08:20:38 -06004119{
4120 struct its_node *its = data;
4121
4122 /* On QDF2400, the size of the ITE is 16Bytes */
Marc Zyngierffedbf02019-11-08 16:57:59 +00004123 its->typer &= ~GITS_TYPER_ITT_ENTRY_SIZE;
4124 its->typer |= FIELD_PREP(GITS_TYPER_ITT_ENTRY_SIZE, 16 - 1);
Ard Biesheuvel9d111d42017-10-17 17:55:55 +01004125
4126 return true;
Shanker Donthineni90922a22017-03-07 08:20:38 -06004127}
4128
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004129static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
4130{
4131 struct its_node *its = its_dev->its;
4132
4133 /*
4134 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
4135 * which maps 32-bit writes targeted at a separate window of
4136 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
4137 * with device ID taken from bits [device_id_bits + 1:2] of
4138 * the window offset.
4139 */
4140 return its->pre_its_base + (its_dev->device_id << 2);
4141}
4142
4143static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
4144{
4145 struct its_node *its = data;
4146 u32 pre_its_window[2];
4147 u32 ids;
4148
4149 if (!fwnode_property_read_u32_array(its->fwnode_handle,
4150 "socionext,synquacer-pre-its",
4151 pre_its_window,
4152 ARRAY_SIZE(pre_its_window))) {
4153
4154 its->pre_its_base = pre_its_window[0];
4155 its->get_msi_base = its_irq_get_msi_base_pre_its;
4156
4157 ids = ilog2(pre_its_window[1]) - 2;
Marc Zyngier576a8342019-11-08 16:58:00 +00004158 if (device_ids(its) > ids) {
4159 its->typer &= ~GITS_TYPER_DEVBITS;
4160 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, ids - 1);
4161 }
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004162
4163 /* the pre-ITS breaks isolation, so disable MSI remapping */
4164 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_MSI_REMAP;
4165 return true;
4166 }
4167 return false;
4168}
4169
Marc Zyngier5c9a8822017-07-28 21:20:37 +01004170static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
4171{
4172 struct its_node *its = data;
4173
4174 /*
4175 * Hip07 insists on using the wrong address for the VLPI
4176 * page. Trick it into doing the right thing...
4177 */
4178 its->vlpi_redist_offset = SZ_128K;
4179 return true;
Marc Zyngiercc2d3212014-11-24 14:35:11 +00004180}
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004181
Robert Richter67510cc2015-09-21 22:58:37 +02004182static const struct gic_quirk its_quirks[] = {
Robert Richter94100972015-09-21 22:58:38 +02004183#ifdef CONFIG_CAVIUM_ERRATUM_22375
4184 {
4185 .desc = "ITS: Cavium errata 22375, 24313",
4186 .iidr = 0xa100034c, /* ThunderX pass 1.x */
4187 .mask = 0xffff0fff,
4188 .init = its_enable_quirk_cavium_22375,
4189 },
4190#endif
Ganapatrao Kulkarnifbf8f402016-05-25 15:29:20 +02004191#ifdef CONFIG_CAVIUM_ERRATUM_23144
4192 {
4193 .desc = "ITS: Cavium erratum 23144",
4194 .iidr = 0xa100034c, /* ThunderX pass 1.x */
4195 .mask = 0xffff0fff,
4196 .init = its_enable_quirk_cavium_23144,
4197 },
4198#endif
Shanker Donthineni90922a22017-03-07 08:20:38 -06004199#ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
4200 {
4201 .desc = "ITS: QDF2400 erratum 0065",
4202 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
4203 .mask = 0xffffffff,
4204 .init = its_enable_quirk_qdf2400_e0065,
4205 },
4206#endif
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004207#ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
4208 {
4209 /*
4210 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
4211 * implementation, but with a 'pre-ITS' added that requires
4212 * special handling in software.
4213 */
4214 .desc = "ITS: Socionext Synquacer pre-ITS",
4215 .iidr = 0x0001143b,
4216 .mask = 0xffffffff,
4217 .init = its_enable_quirk_socionext_synquacer,
4218 },
4219#endif
Marc Zyngier5c9a8822017-07-28 21:20:37 +01004220#ifdef CONFIG_HISILICON_ERRATUM_161600802
4221 {
4222 .desc = "ITS: Hip07 erratum 161600802",
4223 .iidr = 0x00000004,
4224 .mask = 0xffffffff,
4225 .init = its_enable_quirk_hip07_161600802,
4226 },
4227#endif
Robert Richter67510cc2015-09-21 22:58:37 +02004228 {
4229 }
4230};
4231
4232static void its_enable_quirks(struct its_node *its)
4233{
4234 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
4235
4236 gic_enable_quirks(iidr, its_quirks, its);
4237}
4238
Derek Basehoredba0bc72018-02-28 21:48:18 -08004239static int its_save_disable(void)
4240{
4241 struct its_node *its;
4242 int err = 0;
4243
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004244 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004245 list_for_each_entry(its, &its_nodes, entry) {
4246 void __iomem *base;
4247
4248 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4249 continue;
4250
4251 base = its->base;
4252 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
4253 err = its_force_quiescent(base);
4254 if (err) {
4255 pr_err("ITS@%pa: failed to quiesce: %d\n",
4256 &its->phys_base, err);
4257 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
4258 goto err;
4259 }
4260
4261 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
4262 }
4263
4264err:
4265 if (err) {
4266 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
4267 void __iomem *base;
4268
4269 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4270 continue;
4271
4272 base = its->base;
4273 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
4274 }
4275 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004276 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004277
4278 return err;
4279}
4280
4281static void its_restore_enable(void)
4282{
4283 struct its_node *its;
4284 int ret;
4285
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004286 raw_spin_lock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004287 list_for_each_entry(its, &its_nodes, entry) {
4288 void __iomem *base;
4289 int i;
4290
4291 if (!(its->flags & ITS_FLAGS_SAVE_SUSPEND_STATE))
4292 continue;
4293
4294 base = its->base;
4295
4296 /*
4297 * Make sure that the ITS is disabled. If it fails to quiesce,
4298 * don't restore it since writing to CBASER or BASER<n>
4299 * registers is undefined according to the GIC v3 ITS
4300 * Specification.
4301 */
4302 ret = its_force_quiescent(base);
4303 if (ret) {
4304 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
4305 &its->phys_base, ret);
4306 continue;
4307 }
4308
4309 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
4310
4311 /*
4312 * Writing CBASER resets CREADR to 0, so make CWRITER and
4313 * cmd_write line up with it.
4314 */
4315 its->cmd_write = its->cmd_base;
4316 gits_write_cwriter(0, base + GITS_CWRITER);
4317
4318 /* Restore GITS_BASER from the value cache. */
4319 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
4320 struct its_baser *baser = &its->tables[i];
4321
4322 if (!(baser->val & GITS_BASER_VALID))
4323 continue;
4324
4325 its_write_baser(its, baser, baser->val);
4326 }
4327 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
Derek Basehore920181c2018-02-28 21:48:20 -08004328
4329 /*
4330 * Reinit the collection if it's stored in the ITS. This is
4331 * indicated by the col_id being less than the HCC field.
4332 * CID < HCC as specified in the GIC v3 Documentation.
4333 */
4334 if (its->collections[smp_processor_id()].col_id <
4335 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
4336 its_cpu_init_collection(its);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004337 }
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004338 raw_spin_unlock(&its_lock);
Derek Basehoredba0bc72018-02-28 21:48:18 -08004339}
4340
4341static struct syscore_ops its_syscore_ops = {
4342 .suspend = its_save_disable,
4343 .resume = its_restore_enable,
4344};
4345
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004346static int its_init_domain(struct fwnode_handle *handle, struct its_node *its)
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004347{
4348 struct irq_domain *inner_domain;
4349 struct msi_domain_info *info;
4350
4351 info = kzalloc(sizeof(*info), GFP_KERNEL);
4352 if (!info)
4353 return -ENOMEM;
4354
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004355 inner_domain = irq_domain_create_tree(handle, &its_domain_ops, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004356 if (!inner_domain) {
4357 kfree(info);
4358 return -ENOMEM;
4359 }
4360
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004361 inner_domain->parent = its_parent;
Marc Zyngier96f0d932017-06-22 11:42:50 +01004362 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004363 inner_domain->flags |= its->msi_domain_flags;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004364 info->ops = &its_msi_domain_ops;
4365 info->data = its;
4366 inner_domain->host_data = info;
4367
4368 return 0;
4369}
4370
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004371static int its_init_vpe_domain(void)
4372{
Marc Zyngier20b3d542016-12-20 15:23:22 +00004373 struct its_node *its;
4374 u32 devid;
4375 int entries;
4376
4377 if (gic_rdists->has_direct_lpi) {
4378 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
4379 return 0;
4380 }
4381
4382 /* Any ITS will do, even if not v4 */
4383 its = list_first_entry(&its_nodes, struct its_node, entry);
4384
4385 entries = roundup_pow_of_two(nr_cpu_ids);
Kees Cook6396bb22018-06-12 14:03:40 -07004386 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
Marc Zyngier20b3d542016-12-20 15:23:22 +00004387 GFP_KERNEL);
4388 if (!vpe_proxy.vpes) {
4389 pr_err("ITS: Can't allocate GICv4 proxy device array\n");
4390 return -ENOMEM;
4391 }
4392
4393 /* Use the last possible DevID */
Marc Zyngier576a8342019-11-08 16:58:00 +00004394 devid = GENMASK(device_ids(its) - 1, 0);
Marc Zyngier20b3d542016-12-20 15:23:22 +00004395 vpe_proxy.dev = its_create_device(its, devid, entries, false);
4396 if (!vpe_proxy.dev) {
4397 kfree(vpe_proxy.vpes);
4398 pr_err("ITS: Can't allocate GICv4 proxy device\n");
4399 return -ENOMEM;
4400 }
4401
Shanker Donthinenic427a472017-09-23 13:50:19 -05004402 BUG_ON(entries > vpe_proxy.dev->nr_ites);
Marc Zyngier20b3d542016-12-20 15:23:22 +00004403
4404 raw_spin_lock_init(&vpe_proxy.lock);
4405 vpe_proxy.next_victim = 0;
4406 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
4407 devid, vpe_proxy.dev->nr_ites);
4408
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004409 return 0;
4410}
4411
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004412static int __init its_compute_its_list_map(struct resource *res,
4413 void __iomem *its_base)
4414{
4415 int its_number;
4416 u32 ctlr;
4417
4418 /*
4419 * This is assumed to be done early enough that we're
4420 * guaranteed to be single-threaded, hence no
4421 * locking. Should this change, we should address
4422 * this.
4423 */
Marc Zyngierab604912017-10-08 18:48:06 +01004424 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
4425 if (its_number >= GICv4_ITS_LIST_MAX) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004426 pr_err("ITS@%pa: No ITSList entry available!\n",
4427 &res->start);
4428 return -EINVAL;
4429 }
4430
4431 ctlr = readl_relaxed(its_base + GITS_CTLR);
4432 ctlr &= ~GITS_CTLR_ITS_NUMBER;
4433 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
4434 writel_relaxed(ctlr, its_base + GITS_CTLR);
4435 ctlr = readl_relaxed(its_base + GITS_CTLR);
4436 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
4437 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
4438 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
4439 }
4440
4441 if (test_and_set_bit(its_number, &its_list_map)) {
4442 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
4443 &res->start, its_number);
4444 return -EINVAL;
4445 }
4446
4447 return its_number;
4448}
4449
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004450static int __init its_probe_one(struct resource *res,
4451 struct fwnode_handle *handle, int numa_node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004452{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004453 struct its_node *its;
4454 void __iomem *its_base;
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004455 u32 val, ctlr;
4456 u64 baser, tmp, typer;
Shanker Donthineni539d3782019-01-14 09:50:19 +00004457 struct page *page;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004458 int err;
4459
Marc Zyngier5e46a482020-03-04 20:33:14 +00004460 its_base = ioremap(res->start, SZ_64K);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004461 if (!its_base) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004462 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004463 return -ENOMEM;
4464 }
4465
4466 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
4467 if (val != 0x30 && val != 0x40) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004468 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004469 err = -ENODEV;
4470 goto out_unmap;
4471 }
4472
Yun Wu4559fbb2015-03-06 16:37:50 +00004473 err = its_force_quiescent(its_base);
4474 if (err) {
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004475 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
Yun Wu4559fbb2015-03-06 16:37:50 +00004476 goto out_unmap;
4477 }
4478
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004479 pr_info("ITS %pR\n", res);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004480
4481 its = kzalloc(sizeof(*its), GFP_KERNEL);
4482 if (!its) {
4483 err = -ENOMEM;
4484 goto out_unmap;
4485 }
4486
4487 raw_spin_lock_init(&its->lock);
Marc Zyngier9791ec72019-01-29 10:02:33 +00004488 mutex_init(&its->dev_alloc_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004489 INIT_LIST_HEAD(&its->entry);
4490 INIT_LIST_HEAD(&its->its_device_list);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004491 typer = gic_read_typer(its_base + GITS_TYPER);
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004492 its->typer = typer;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004493 its->base = its_base;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004494 its->phys_base = res->start;
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004495 if (is_v4(its)) {
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004496 if (!(typer & GITS_TYPER_VMOVP)) {
4497 err = its_compute_its_list_map(res, its_base);
4498 if (err < 0)
4499 goto out_free_its;
4500
Marc Zyngierdebf6d02017-10-08 18:44:42 +01004501 its->list_nr = err;
4502
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004503 pr_info("ITS@%pa: Using ITS number %d\n",
4504 &res->start, err);
4505 } else {
4506 pr_info("ITS@%pa: Single VMOVP capable\n", &res->start);
4507 }
Marc Zyngier5e516842019-12-24 11:10:28 +00004508
4509 if (is_v4_1(its)) {
4510 u32 svpet = FIELD_GET(GITS_TYPER_SVPET, typer);
Marc Zyngier5e46a482020-03-04 20:33:14 +00004511
4512 its->sgir_base = ioremap(res->start + SZ_128K, SZ_64K);
4513 if (!its->sgir_base) {
4514 err = -ENOMEM;
4515 goto out_free_its;
4516 }
4517
Marc Zyngier5e516842019-12-24 11:10:28 +00004518 its->mpidr = readl_relaxed(its_base + GITS_MPIDR);
4519
4520 pr_info("ITS@%pa: Using GICv4.1 mode %08x %08x\n",
4521 &res->start, its->mpidr, svpet);
4522 }
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004523 }
4524
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004525 its->numa_node = numa_node;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004526
Shanker Donthineni539d3782019-01-14 09:50:19 +00004527 page = alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
4528 get_order(ITS_CMD_QUEUE_SZ));
4529 if (!page) {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004530 err = -ENOMEM;
Marc Zyngier5e46a482020-03-04 20:33:14 +00004531 goto out_unmap_sgir;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004532 }
Shanker Donthineni539d3782019-01-14 09:50:19 +00004533 its->cmd_base = (void *)page_address(page);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004534 its->cmd_write = its->cmd_base;
Ard Biesheuvel558b0162017-10-17 17:55:56 +01004535 its->fwnode_handle = handle;
4536 its->get_msi_base = its_irq_get_msi_base;
4537 its->msi_domain_flags = IRQ_DOMAIN_FLAG_MSI_REMAP;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004538
Robert Richter67510cc2015-09-21 22:58:37 +02004539 its_enable_quirks(its);
4540
Shanker Donthineni0e0b0f62016-06-06 18:17:31 -05004541 err = its_alloc_tables(its);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004542 if (err)
4543 goto out_free_cmd;
4544
4545 err = its_alloc_collections(its);
4546 if (err)
4547 goto out_free_tables;
4548
4549 baser = (virt_to_phys(its->cmd_base) |
Shanker Donthineni2fd632a2017-01-25 21:51:41 -06004550 GITS_CBASER_RaWaWb |
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004551 GITS_CBASER_InnerShareable |
4552 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
4553 GITS_CBASER_VALID);
4554
Vladimir Murzin0968a612016-11-02 11:54:06 +00004555 gits_write_cbaser(baser, its->base + GITS_CBASER);
4556 tmp = gits_read_cbaser(its->base + GITS_CBASER);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004557
Marc Zyngier4ad3e362015-03-27 14:15:04 +00004558 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
Marc Zyngier241a3862015-03-27 14:15:05 +00004559 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
4560 /*
4561 * The HW reports non-shareable, we must
4562 * remove the cacheability attributes as
4563 * well.
4564 */
4565 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
4566 GITS_CBASER_CACHEABILITY_MASK);
4567 baser |= GITS_CBASER_nC;
Vladimir Murzin0968a612016-11-02 11:54:06 +00004568 gits_write_cbaser(baser, its->base + GITS_CBASER);
Marc Zyngier241a3862015-03-27 14:15:05 +00004569 }
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004570 pr_info("ITS: using cache flushing for cmd queue\n");
4571 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
4572 }
4573
Vladimir Murzin0968a612016-11-02 11:54:06 +00004574 gits_write_cwriter(0, its->base + GITS_CWRITER);
Marc Zyngier3dfa5762016-12-19 17:25:54 +00004575 ctlr = readl_relaxed(its->base + GITS_CTLR);
Marc Zyngierd51c4b42017-06-27 21:24:25 +01004576 ctlr |= GITS_CTLR_ENABLE;
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004577 if (is_v4(its))
Marc Zyngierd51c4b42017-06-27 21:24:25 +01004578 ctlr |= GITS_CTLR_ImDe;
4579 writel_relaxed(ctlr, its->base + GITS_CTLR);
Marc Zyngier241a3862015-03-27 14:15:05 +00004580
Derek Basehoredba0bc72018-02-28 21:48:18 -08004581 if (GITS_TYPER_HCC(typer))
4582 its->flags |= ITS_FLAGS_SAVE_SUSPEND_STATE;
4583
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004584 err = its_init_domain(handle, its);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004585 if (err)
4586 goto out_free_tables;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004587
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004588 raw_spin_lock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004589 list_add(&its->entry, &its_nodes);
Sebastian Andrzej Siewiora8db7452018-07-18 17:42:04 +02004590 raw_spin_unlock(&its_lock);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004591
4592 return 0;
4593
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004594out_free_tables:
4595 its_free_tables(its);
4596out_free_cmd:
Robert Richter5bc13c22017-02-01 18:38:25 +01004597 free_pages((unsigned long)its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
Marc Zyngier5e46a482020-03-04 20:33:14 +00004598out_unmap_sgir:
4599 if (its->sgir_base)
4600 iounmap(its->sgir_base);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004601out_free_its:
4602 kfree(its);
4603out_unmap:
4604 iounmap(its_base);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004605 pr_err("ITS@%pa: failed probing (%d)\n", &res->start, err);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004606 return err;
4607}
4608
4609static bool gic_rdists_supports_plpis(void)
4610{
Marc Zyngier589ce5f2016-10-14 15:13:07 +01004611 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004612}
4613
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05004614static int redist_disable_lpis(void)
4615{
4616 void __iomem *rbase = gic_data_rdist_rd_base();
4617 u64 timeout = USEC_PER_SEC;
4618 u64 val;
4619
4620 if (!gic_rdists_supports_plpis()) {
4621 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
4622 return -ENXIO;
4623 }
4624
4625 val = readl_relaxed(rbase + GICR_CTLR);
4626 if (!(val & GICR_CTLR_ENABLE_LPIS))
4627 return 0;
4628
Marc Zyngier11e37d32018-07-27 13:38:54 +01004629 /*
4630 * If coming via a CPU hotplug event, we don't need to disable
4631 * LPIs before trying to re-enable them. They are already
4632 * configured and all is well in the world.
Marc Zyngierc440a9d2018-07-27 15:40:13 +01004633 *
4634 * If running with preallocated tables, there is nothing to do.
Marc Zyngier11e37d32018-07-27 13:38:54 +01004635 */
Marc Zyngierc440a9d2018-07-27 15:40:13 +01004636 if (gic_data_rdist()->lpi_enabled ||
4637 (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED))
Marc Zyngier11e37d32018-07-27 13:38:54 +01004638 return 0;
4639
4640 /*
4641 * From that point on, we only try to do some damage control.
4642 */
4643 pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05004644 smp_processor_id());
4645 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
4646
4647 /* Disable LPIs */
4648 val &= ~GICR_CTLR_ENABLE_LPIS;
4649 writel_relaxed(val, rbase + GICR_CTLR);
4650
4651 /* Make sure any change to GICR_CTLR is observable by the GIC */
4652 dsb(sy);
4653
4654 /*
4655 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
4656 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
4657 * Error out if we time out waiting for RWP to clear.
4658 */
4659 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
4660 if (!timeout) {
4661 pr_err("CPU%d: Timeout while disabling LPIs\n",
4662 smp_processor_id());
4663 return -ETIMEDOUT;
4664 }
4665 udelay(1);
4666 timeout--;
4667 }
4668
4669 /*
4670 * After it has been written to 1, it is IMPLEMENTATION
4671 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
4672 * cleared to 0. Error out if clearing the bit failed.
4673 */
4674 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
4675 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
4676 return -EBUSY;
4677 }
4678
4679 return 0;
4680}
4681
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004682int its_cpu_init(void)
4683{
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004684 if (!list_empty(&its_nodes)) {
Shanker Donthineni6eb486b2018-03-21 20:58:49 -05004685 int ret;
4686
4687 ret = redist_disable_lpis();
4688 if (ret)
4689 return ret;
4690
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004691 its_cpu_init_lpis();
Derek Basehore920181c2018-02-28 21:48:20 -08004692 its_cpu_init_collections();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004693 }
4694
4695 return 0;
4696}
4697
Arvind Yadav935bba72017-06-22 16:05:30 +05304698static const struct of_device_id its_device_id[] = {
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004699 { .compatible = "arm,gic-v3-its", },
4700 {},
4701};
4702
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004703static int __init its_of_probe(struct device_node *node)
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004704{
4705 struct device_node *np;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004706 struct resource res;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004707
4708 for (np = of_find_matching_node(node, its_device_id); np;
4709 np = of_find_matching_node(np, its_device_id)) {
Stephen Boyd95a25622018-02-01 09:03:29 -08004710 if (!of_device_is_available(np))
4711 continue;
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004712 if (!of_property_read_bool(np, "msi-controller")) {
Rob Herringe81f54c2017-07-18 16:43:10 -05004713 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
4714 np);
Tomasz Nowickid14ae5e2016-09-12 20:32:23 +02004715 continue;
4716 }
4717
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004718 if (of_address_to_resource(np, 0, &res)) {
Rob Herringe81f54c2017-07-18 16:43:10 -05004719 pr_warn("%pOF: no regs?\n", np);
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004720 continue;
4721 }
4722
4723 its_probe_one(&res, &np->fwnode, of_node_to_nid(np));
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004724 }
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004725 return 0;
4726}
4727
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004728#ifdef CONFIG_ACPI
4729
4730#define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
4731
Robert Richterd1ce2632017-07-12 15:25:09 +02004732#ifdef CONFIG_ACPI_NUMA
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304733struct its_srat_map {
4734 /* numa node id */
4735 u32 numa_node;
4736 /* GIC ITS ID */
4737 u32 its_id;
4738};
4739
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004740static struct its_srat_map *its_srat_maps __initdata;
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304741static int its_in_srat __initdata;
4742
4743static int __init acpi_get_its_numa_node(u32 its_id)
4744{
4745 int i;
4746
4747 for (i = 0; i < its_in_srat; i++) {
4748 if (its_id == its_srat_maps[i].its_id)
4749 return its_srat_maps[i].numa_node;
4750 }
4751 return NUMA_NO_NODE;
4752}
4753
Keith Busch60574d12019-03-11 14:55:57 -06004754static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header,
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004755 const unsigned long end)
4756{
4757 return 0;
4758}
4759
Keith Busch60574d12019-03-11 14:55:57 -06004760static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header,
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304761 const unsigned long end)
4762{
4763 int node;
4764 struct acpi_srat_gic_its_affinity *its_affinity;
4765
4766 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
4767 if (!its_affinity)
4768 return -EINVAL;
4769
4770 if (its_affinity->header.length < sizeof(*its_affinity)) {
4771 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
4772 its_affinity->header.length);
4773 return -EINVAL;
4774 }
4775
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304776 node = acpi_map_pxm_to_node(its_affinity->proximity_domain);
4777
4778 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
4779 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
4780 return 0;
4781 }
4782
4783 its_srat_maps[its_in_srat].numa_node = node;
4784 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
4785 its_in_srat++;
4786 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
4787 its_affinity->proximity_domain, its_affinity->its_id, node);
4788
4789 return 0;
4790}
4791
4792static void __init acpi_table_parse_srat_its(void)
4793{
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004794 int count;
4795
4796 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
4797 sizeof(struct acpi_table_srat),
4798 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
4799 gic_acpi_match_srat_its, 0);
4800 if (count <= 0)
4801 return;
4802
Kees Cook6da2ec52018-06-12 13:55:00 -07004803 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
4804 GFP_KERNEL);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004805 if (!its_srat_maps) {
4806 pr_warn("SRAT: Failed to allocate memory for its_srat_maps!\n");
4807 return;
4808 }
4809
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304810 acpi_table_parse_entries(ACPI_SIG_SRAT,
4811 sizeof(struct acpi_table_srat),
4812 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
4813 gic_acpi_parse_srat_its, 0);
4814}
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004815
4816/* free the its_srat_maps after ITS probing */
4817static void __init acpi_its_srat_maps_free(void)
4818{
4819 kfree(its_srat_maps);
4820}
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304821#else
4822static void __init acpi_table_parse_srat_its(void) { }
4823static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004824static void __init acpi_its_srat_maps_free(void) { }
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304825#endif
4826
Keith Busch60574d12019-03-11 14:55:57 -06004827static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header,
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004828 const unsigned long end)
4829{
4830 struct acpi_madt_generic_translator *its_entry;
4831 struct fwnode_handle *dom_handle;
4832 struct resource res;
4833 int err;
4834
4835 its_entry = (struct acpi_madt_generic_translator *)header;
4836 memset(&res, 0, sizeof(res));
4837 res.start = its_entry->base_address;
4838 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
4839 res.flags = IORESOURCE_MEM;
4840
Marc Zyngier5778cc72019-07-31 16:13:42 +01004841 dom_handle = irq_domain_alloc_fwnode(&res.start);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004842 if (!dom_handle) {
4843 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
4844 &res.start);
4845 return -ENOMEM;
4846 }
4847
Shameer Kolothum8b4282e2018-02-13 15:20:50 +00004848 err = iort_register_domain_token(its_entry->translation_id, res.start,
4849 dom_handle);
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004850 if (err) {
4851 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
4852 &res.start, its_entry->translation_id);
4853 goto dom_err;
4854 }
4855
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304856 err = its_probe_one(&res, dom_handle,
4857 acpi_get_its_numa_node(its_entry->translation_id));
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004858 if (!err)
4859 return 0;
4860
4861 iort_deregister_domain_token(its_entry->translation_id);
4862dom_err:
4863 irq_domain_free_fwnode(dom_handle);
4864 return err;
4865}
4866
4867static void __init its_acpi_probe(void)
4868{
Ganapatrao Kulkarnidbd2b822017-06-22 11:40:12 +05304869 acpi_table_parse_srat_its();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004870 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
4871 gic_acpi_parse_madt_its, 0);
Hanjun Guofdf6e7a2017-07-26 18:15:49 +08004872 acpi_its_srat_maps_free();
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004873}
4874#else
4875static void __init its_acpi_probe(void) { }
4876#endif
4877
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004878int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
4879 struct irq_domain *parent_domain)
4880{
4881 struct device_node *of_node;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004882 struct its_node *its;
4883 bool has_v4 = false;
Marc Zyngier3c407062020-03-04 20:33:13 +00004884 bool has_v4_1 = false;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004885 int err;
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004886
Marc Zyngier5e516842019-12-24 11:10:28 +00004887 gic_rdists = rdists;
4888
Tomasz Nowickidb40f0a2016-09-12 20:32:24 +02004889 its_parent = parent_domain;
4890 of_node = to_of_node(handle);
4891 if (of_node)
4892 its_of_probe(of_node);
4893 else
Tomasz Nowicki3f010cf2016-09-12 20:32:25 +02004894 its_acpi_probe();
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004895
4896 if (list_empty(&its_nodes)) {
4897 pr_warn("ITS: No ITS available, not enabling LPIs\n");
4898 return -ENXIO;
4899 }
4900
Marc Zyngier11e37d32018-07-27 13:38:54 +01004901 err = allocate_lpi_tables();
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004902 if (err)
4903 return err;
4904
Marc Zyngier3c407062020-03-04 20:33:13 +00004905 list_for_each_entry(its, &its_nodes, entry) {
Marc Zyngier0dd57fe2019-11-08 16:57:58 +00004906 has_v4 |= is_v4(its);
Marc Zyngier3c407062020-03-04 20:33:13 +00004907 has_v4_1 |= is_v4_1(its);
4908 }
4909
4910 /* Don't bother with inconsistent systems */
4911 if (WARN_ON(!has_v4_1 && rdists->has_rvpeid))
4912 rdists->has_rvpeid = false;
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004913
4914 if (has_v4 & rdists->has_vlpis) {
Marc Zyngier3d63cb52016-12-20 15:31:54 +00004915 if (its_init_vpe_domain() ||
4916 its_init_v4(parent_domain, &its_vpe_domain_ops)) {
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004917 rdists->has_vlpis = false;
4918 pr_err("ITS: Disabling GICv4 support\n");
4919 }
4920 }
4921
Derek Basehoredba0bc72018-02-28 21:48:18 -08004922 register_syscore_ops(&its_syscore_ops);
4923
Marc Zyngier8fff27a2016-12-20 13:41:55 +00004924 return 0;
Marc Zyngier4c21f3c2014-11-24 14:35:17 +00004925}