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