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