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