1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2013-2017 ARM Limited, All Rights Reserved.
4 * Author: Marc Zyngier <[email protected]>
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/[email protected]
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 struct its_node *its;
1815 u64 addr;
1816
1817 its = its_dev->its;
1818 addr = its->get_msi_base(its_dev);
1819
1820 msg->address_lo = lower_32_bits(addr);
1821 msg->address_hi = upper_32_bits(addr);
1822 msg->data = its_get_event_id(d);
1823
1824 iommu_dma_compose_msi_msg(irq_data_get_msi_desc(d), msg);
1825 }
1826
its_irq_set_irqchip_state(struct irq_data * d,enum irqchip_irq_state which,bool state)1827 static int its_irq_set_irqchip_state(struct irq_data *d,
1828 enum irqchip_irq_state which,
1829 bool state)
1830 {
1831 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1832 u32 event = its_get_event_id(d);
1833
1834 if (which != IRQCHIP_STATE_PENDING)
1835 return -EINVAL;
1836
1837 if (irqd_is_forwarded_to_vcpu(d)) {
1838 if (state)
1839 its_send_vint(its_dev, event);
1840 else
1841 its_send_vclear(its_dev, event);
1842 } else {
1843 if (state)
1844 its_send_int(its_dev, event);
1845 else
1846 its_send_clear(its_dev, event);
1847 }
1848
1849 return 0;
1850 }
1851
its_irq_retrigger(struct irq_data * d)1852 static int its_irq_retrigger(struct irq_data *d)
1853 {
1854 return !its_irq_set_irqchip_state(d, IRQCHIP_STATE_PENDING, true);
1855 }
1856
1857 /*
1858 * Two favourable cases:
1859 *
1860 * (a) Either we have a GICv4.1, and all vPEs have to be mapped at all times
1861 * for vSGI delivery
1862 *
1863 * (b) Or the ITSs do not use a list map, meaning that VMOVP is cheap enough
1864 * and we're better off mapping all VPEs always
1865 *
1866 * If neither (a) nor (b) is true, then we map vPEs on demand.
1867 *
1868 */
gic_requires_eager_mapping(void)1869 static bool gic_requires_eager_mapping(void)
1870 {
1871 if (!its_list_map || gic_rdists->has_rvpeid)
1872 return true;
1873
1874 return false;
1875 }
1876
its_map_vm(struct its_node * its,struct its_vm * vm)1877 static void its_map_vm(struct its_node *its, struct its_vm *vm)
1878 {
1879 if (gic_requires_eager_mapping())
1880 return;
1881
1882 guard(raw_spinlock_irqsave)(&vm->vmapp_lock);
1883
1884 /*
1885 * If the VM wasn't mapped yet, iterate over the vpes and get
1886 * them mapped now.
1887 */
1888 vm->vlpi_count[its->list_nr]++;
1889
1890 if (vm->vlpi_count[its->list_nr] == 1) {
1891 int i;
1892
1893 for (i = 0; i < vm->nr_vpes; i++) {
1894 struct its_vpe *vpe = vm->vpes[i];
1895
1896 scoped_guard(raw_spinlock, &vpe->vpe_lock)
1897 its_send_vmapp(its, vpe, true);
1898
1899 its_send_vinvall(its, vpe);
1900 }
1901 }
1902 }
1903
its_unmap_vm(struct its_node * its,struct its_vm * vm)1904 static void its_unmap_vm(struct its_node *its, struct its_vm *vm)
1905 {
1906 /* Not using the ITS list? Everything is always mapped. */
1907 if (gic_requires_eager_mapping())
1908 return;
1909
1910 guard(raw_spinlock_irqsave)(&vm->vmapp_lock);
1911
1912 if (!--vm->vlpi_count[its->list_nr]) {
1913 int i;
1914
1915 for (i = 0; i < vm->nr_vpes; i++) {
1916 guard(raw_spinlock)(&vm->vpes[i]->vpe_lock);
1917 its_send_vmapp(its, vm->vpes[i], false);
1918 }
1919 }
1920 }
1921
its_vlpi_map(struct irq_data * d,struct its_cmd_info * info)1922 static int its_vlpi_map(struct irq_data *d, struct its_cmd_info *info)
1923 {
1924 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1925 u32 event = its_get_event_id(d);
1926
1927 if (!info->map)
1928 return -EINVAL;
1929
1930 if (!its_dev->event_map.vm) {
1931 struct its_vlpi_map *maps;
1932
1933 maps = kcalloc(its_dev->event_map.nr_lpis, sizeof(*maps),
1934 GFP_ATOMIC);
1935 if (!maps)
1936 return -ENOMEM;
1937
1938 its_dev->event_map.vm = info->map->vm;
1939 its_dev->event_map.vlpi_maps = maps;
1940 } else if (its_dev->event_map.vm != info->map->vm) {
1941 return -EINVAL;
1942 }
1943
1944 /* Get our private copy of the mapping information */
1945 its_dev->event_map.vlpi_maps[event] = *info->map;
1946
1947 if (irqd_is_forwarded_to_vcpu(d)) {
1948 /* Already mapped, move it around */
1949 its_send_vmovi(its_dev, event);
1950 } else {
1951 /* Ensure all the VPEs are mapped on this ITS */
1952 its_map_vm(its_dev->its, info->map->vm);
1953
1954 /*
1955 * Flag the interrupt as forwarded so that we can
1956 * start poking the virtual property table.
1957 */
1958 irqd_set_forwarded_to_vcpu(d);
1959
1960 /* Write out the property to the prop table */
1961 lpi_write_config(d, 0xff, info->map->properties);
1962
1963 /* Drop the physical mapping */
1964 its_send_discard(its_dev, event);
1965
1966 /* and install the virtual one */
1967 its_send_vmapti(its_dev, event);
1968
1969 /* Increment the number of VLPIs */
1970 its_dev->event_map.nr_vlpis++;
1971 }
1972
1973 return 0;
1974 }
1975
its_vlpi_get(struct irq_data * d,struct its_cmd_info * info)1976 static int its_vlpi_get(struct irq_data *d, struct its_cmd_info *info)
1977 {
1978 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1979 struct its_vlpi_map *map;
1980
1981 map = get_vlpi_map(d);
1982
1983 if (!its_dev->event_map.vm || !map)
1984 return -EINVAL;
1985
1986 /* Copy our mapping information to the incoming request */
1987 *info->map = *map;
1988
1989 return 0;
1990 }
1991
its_vlpi_unmap(struct irq_data * d)1992 static int its_vlpi_unmap(struct irq_data *d)
1993 {
1994 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
1995 u32 event = its_get_event_id(d);
1996
1997 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
1998 return -EINVAL;
1999
2000 /* Drop the virtual mapping */
2001 its_send_discard(its_dev, event);
2002
2003 /* and restore the physical one */
2004 irqd_clr_forwarded_to_vcpu(d);
2005 its_send_mapti(its_dev, d->hwirq, event);
2006 lpi_update_config(d, 0xff, (lpi_prop_prio |
2007 LPI_PROP_ENABLED |
2008 LPI_PROP_GROUP1));
2009
2010 /* Potentially unmap the VM from this ITS */
2011 its_unmap_vm(its_dev->its, its_dev->event_map.vm);
2012
2013 /*
2014 * Drop the refcount and make the device available again if
2015 * this was the last VLPI.
2016 */
2017 if (!--its_dev->event_map.nr_vlpis) {
2018 its_dev->event_map.vm = NULL;
2019 kfree(its_dev->event_map.vlpi_maps);
2020 }
2021
2022 return 0;
2023 }
2024
its_vlpi_prop_update(struct irq_data * d,struct its_cmd_info * info)2025 static int its_vlpi_prop_update(struct irq_data *d, struct its_cmd_info *info)
2026 {
2027 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2028
2029 if (!its_dev->event_map.vm || !irqd_is_forwarded_to_vcpu(d))
2030 return -EINVAL;
2031
2032 if (info->cmd_type == PROP_UPDATE_AND_INV_VLPI)
2033 lpi_update_config(d, 0xff, info->config);
2034 else
2035 lpi_write_config(d, 0xff, info->config);
2036 its_vlpi_set_doorbell(d, !!(info->config & LPI_PROP_ENABLED));
2037
2038 return 0;
2039 }
2040
its_irq_set_vcpu_affinity(struct irq_data * d,void * vcpu_info)2041 static int its_irq_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
2042 {
2043 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
2044 struct its_cmd_info *info = vcpu_info;
2045
2046 /* Need a v4 ITS */
2047 if (!is_v4(its_dev->its))
2048 return -EINVAL;
2049
2050 guard(raw_spinlock)(&its_dev->event_map.vlpi_lock);
2051
2052 /* Unmap request? */
2053 if (!info)
2054 return its_vlpi_unmap(d);
2055
2056 switch (info->cmd_type) {
2057 case MAP_VLPI:
2058 return its_vlpi_map(d, info);
2059
2060 case GET_VLPI:
2061 return its_vlpi_get(d, info);
2062
2063 case PROP_UPDATE_VLPI:
2064 case PROP_UPDATE_AND_INV_VLPI:
2065 return its_vlpi_prop_update(d, info);
2066
2067 default:
2068 return -EINVAL;
2069 }
2070 }
2071
2072 static struct irq_chip its_irq_chip = {
2073 .name = "ITS",
2074 .irq_mask = its_mask_irq,
2075 .irq_unmask = its_unmask_irq,
2076 .irq_eoi = irq_chip_eoi_parent,
2077 .irq_set_affinity = its_set_affinity,
2078 .irq_compose_msi_msg = its_irq_compose_msi_msg,
2079 .irq_set_irqchip_state = its_irq_set_irqchip_state,
2080 .irq_retrigger = its_irq_retrigger,
2081 .irq_set_vcpu_affinity = its_irq_set_vcpu_affinity,
2082 };
2083
2084
2085 /*
2086 * How we allocate LPIs:
2087 *
2088 * lpi_range_list contains ranges of LPIs that are to available to
2089 * allocate from. To allocate LPIs, just pick the first range that
2090 * fits the required allocation, and reduce it by the required
2091 * amount. Once empty, remove the range from the list.
2092 *
2093 * To free a range of LPIs, add a free range to the list, sort it and
2094 * merge the result if the new range happens to be adjacent to an
2095 * already free block.
2096 *
2097 * The consequence of the above is that allocation is cost is low, but
2098 * freeing is expensive. We assumes that freeing rarely occurs.
2099 */
2100 #define ITS_MAX_LPI_NRBITS 16 /* 64K LPIs */
2101
2102 static DEFINE_MUTEX(lpi_range_lock);
2103 static LIST_HEAD(lpi_range_list);
2104
2105 struct lpi_range {
2106 struct list_head entry;
2107 u32 base_id;
2108 u32 span;
2109 };
2110
mk_lpi_range(u32 base,u32 span)2111 static struct lpi_range *mk_lpi_range(u32 base, u32 span)
2112 {
2113 struct lpi_range *range;
2114
2115 range = kmalloc(sizeof(*range), GFP_KERNEL);
2116 if (range) {
2117 range->base_id = base;
2118 range->span = span;
2119 }
2120
2121 return range;
2122 }
2123
alloc_lpi_range(u32 nr_lpis,u32 * base)2124 static int alloc_lpi_range(u32 nr_lpis, u32 *base)
2125 {
2126 struct lpi_range *range, *tmp;
2127 int err = -ENOSPC;
2128
2129 mutex_lock(&lpi_range_lock);
2130
2131 list_for_each_entry_safe(range, tmp, &lpi_range_list, entry) {
2132 if (range->span >= nr_lpis) {
2133 *base = range->base_id;
2134 range->base_id += nr_lpis;
2135 range->span -= nr_lpis;
2136
2137 if (range->span == 0) {
2138 list_del(&range->entry);
2139 kfree(range);
2140 }
2141
2142 err = 0;
2143 break;
2144 }
2145 }
2146
2147 mutex_unlock(&lpi_range_lock);
2148
2149 pr_debug("ITS: alloc %u:%u\n", *base, nr_lpis);
2150 return err;
2151 }
2152
merge_lpi_ranges(struct lpi_range * a,struct lpi_range * b)2153 static void merge_lpi_ranges(struct lpi_range *a, struct lpi_range *b)
2154 {
2155 if (&a->entry == &lpi_range_list || &b->entry == &lpi_range_list)
2156 return;
2157 if (a->base_id + a->span != b->base_id)
2158 return;
2159 b->base_id = a->base_id;
2160 b->span += a->span;
2161 list_del(&a->entry);
2162 kfree(a);
2163 }
2164
free_lpi_range(u32 base,u32 nr_lpis)2165 static int free_lpi_range(u32 base, u32 nr_lpis)
2166 {
2167 struct lpi_range *new, *old;
2168
2169 new = mk_lpi_range(base, nr_lpis);
2170 if (!new)
2171 return -ENOMEM;
2172
2173 mutex_lock(&lpi_range_lock);
2174
2175 list_for_each_entry_reverse(old, &lpi_range_list, entry) {
2176 if (old->base_id < base)
2177 break;
2178 }
2179 /*
2180 * old is the last element with ->base_id smaller than base,
2181 * so new goes right after it. If there are no elements with
2182 * ->base_id smaller than base, &old->entry ends up pointing
2183 * at the head of the list, and inserting new it the start of
2184 * the list is the right thing to do in that case as well.
2185 */
2186 list_add(&new->entry, &old->entry);
2187 /*
2188 * Now check if we can merge with the preceding and/or
2189 * following ranges.
2190 */
2191 merge_lpi_ranges(old, new);
2192 merge_lpi_ranges(new, list_next_entry(new, entry));
2193
2194 mutex_unlock(&lpi_range_lock);
2195 return 0;
2196 }
2197
its_lpi_init(u32 id_bits)2198 static int __init its_lpi_init(u32 id_bits)
2199 {
2200 u32 lpis = (1UL << id_bits) - 8192;
2201 u32 numlpis;
2202 int err;
2203
2204 numlpis = 1UL << GICD_TYPER_NUM_LPIS(gic_rdists->gicd_typer);
2205
2206 if (numlpis > 2 && !WARN_ON(numlpis > lpis)) {
2207 lpis = numlpis;
2208 pr_info("ITS: Using hypervisor restricted LPI range [%u]\n",
2209 lpis);
2210 }
2211
2212 /*
2213 * Initializing the allocator is just the same as freeing the
2214 * full range of LPIs.
2215 */
2216 err = free_lpi_range(8192, lpis);
2217 pr_debug("ITS: Allocator initialized for %u LPIs\n", lpis);
2218 return err;
2219 }
2220
its_lpi_alloc(int nr_irqs,u32 * base,int * nr_ids)2221 static unsigned long *its_lpi_alloc(int nr_irqs, u32 *base, int *nr_ids)
2222 {
2223 unsigned long *bitmap = NULL;
2224 int err = 0;
2225
2226 do {
2227 err = alloc_lpi_range(nr_irqs, base);
2228 if (!err)
2229 break;
2230
2231 nr_irqs /= 2;
2232 } while (nr_irqs > 0);
2233
2234 if (!nr_irqs)
2235 err = -ENOSPC;
2236
2237 if (err)
2238 goto out;
2239
2240 bitmap = bitmap_zalloc(nr_irqs, GFP_ATOMIC);
2241 if (!bitmap)
2242 goto out;
2243
2244 *nr_ids = nr_irqs;
2245
2246 out:
2247 if (!bitmap)
2248 *base = *nr_ids = 0;
2249
2250 return bitmap;
2251 }
2252
its_lpi_free(unsigned long * bitmap,u32 base,u32 nr_ids)2253 static void its_lpi_free(unsigned long *bitmap, u32 base, u32 nr_ids)
2254 {
2255 WARN_ON(free_lpi_range(base, nr_ids));
2256 bitmap_free(bitmap);
2257 }
2258
gic_reset_prop_table(void * va)2259 static void gic_reset_prop_table(void *va)
2260 {
2261 /* Regular IRQ priority, Group-1, disabled */
2262 memset(va, lpi_prop_prio | LPI_PROP_GROUP1, LPI_PROPBASE_SZ);
2263
2264 /* Make sure the GIC will observe the written configuration */
2265 gic_flush_dcache_to_poc(va, LPI_PROPBASE_SZ);
2266 }
2267
its_allocate_prop_table(gfp_t gfp_flags)2268 static struct page *its_allocate_prop_table(gfp_t gfp_flags)
2269 {
2270 struct page *prop_page;
2271
2272 prop_page = its_alloc_pages(gfp_flags,
2273 get_order(LPI_PROPBASE_SZ));
2274 if (!prop_page)
2275 return NULL;
2276
2277 gic_reset_prop_table(page_address(prop_page));
2278
2279 return prop_page;
2280 }
2281
its_free_prop_table(struct page * prop_page)2282 static void its_free_prop_table(struct page *prop_page)
2283 {
2284 its_free_pages(page_address(prop_page), get_order(LPI_PROPBASE_SZ));
2285 }
2286
gic_check_reserved_range(phys_addr_t addr,unsigned long size)2287 static bool gic_check_reserved_range(phys_addr_t addr, unsigned long size)
2288 {
2289 phys_addr_t start, end, addr_end;
2290 u64 i;
2291
2292 /*
2293 * We don't bother checking for a kdump kernel as by
2294 * construction, the LPI tables are out of this kernel's
2295 * memory map.
2296 */
2297 if (is_kdump_kernel())
2298 return true;
2299
2300 addr_end = addr + size - 1;
2301
2302 for_each_reserved_mem_range(i, &start, &end) {
2303 if (addr >= start && addr_end <= end)
2304 return true;
2305 }
2306
2307 /* Not found, not a good sign... */
2308 pr_warn("GICv3: Expected reserved range [%pa:%pa], not found\n",
2309 &addr, &addr_end);
2310 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
2311 return false;
2312 }
2313
gic_reserve_range(phys_addr_t addr,unsigned long size)2314 static int gic_reserve_range(phys_addr_t addr, unsigned long size)
2315 {
2316 if (efi_enabled(EFI_CONFIG_TABLES))
2317 return efi_mem_reserve_persistent(addr, size);
2318
2319 return 0;
2320 }
2321
its_setup_lpi_prop_table(void)2322 static int __init its_setup_lpi_prop_table(void)
2323 {
2324 if (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) {
2325 u64 val;
2326
2327 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
2328 lpi_id_bits = (val & GICR_PROPBASER_IDBITS_MASK) + 1;
2329
2330 gic_rdists->prop_table_pa = val & GENMASK_ULL(51, 12);
2331 gic_rdists->prop_table_va = memremap(gic_rdists->prop_table_pa,
2332 LPI_PROPBASE_SZ,
2333 MEMREMAP_WB);
2334 gic_reset_prop_table(gic_rdists->prop_table_va);
2335 } else {
2336 struct page *page;
2337
2338 lpi_id_bits = min_t(u32,
2339 GICD_TYPER_ID_BITS(gic_rdists->gicd_typer),
2340 ITS_MAX_LPI_NRBITS);
2341 page = its_allocate_prop_table(GFP_NOWAIT);
2342 if (!page) {
2343 pr_err("Failed to allocate PROPBASE\n");
2344 return -ENOMEM;
2345 }
2346
2347 gic_rdists->prop_table_pa = page_to_phys(page);
2348 gic_rdists->prop_table_va = page_address(page);
2349 WARN_ON(gic_reserve_range(gic_rdists->prop_table_pa,
2350 LPI_PROPBASE_SZ));
2351 }
2352
2353 pr_info("GICv3: using LPI property table @%pa\n",
2354 &gic_rdists->prop_table_pa);
2355
2356 return its_lpi_init(lpi_id_bits);
2357 }
2358
2359 static const char *its_base_type_string[] = {
2360 [GITS_BASER_TYPE_DEVICE] = "Devices",
2361 [GITS_BASER_TYPE_VCPU] = "Virtual CPUs",
2362 [GITS_BASER_TYPE_RESERVED3] = "Reserved (3)",
2363 [GITS_BASER_TYPE_COLLECTION] = "Interrupt Collections",
2364 [GITS_BASER_TYPE_RESERVED5] = "Reserved (5)",
2365 [GITS_BASER_TYPE_RESERVED6] = "Reserved (6)",
2366 [GITS_BASER_TYPE_RESERVED7] = "Reserved (7)",
2367 };
2368
its_read_baser(struct its_node * its,struct its_baser * baser)2369 static u64 its_read_baser(struct its_node *its, struct its_baser *baser)
2370 {
2371 u32 idx = baser - its->tables;
2372
2373 return gits_read_baser(its->base + GITS_BASER + (idx << 3));
2374 }
2375
its_write_baser(struct its_node * its,struct its_baser * baser,u64 val)2376 static void its_write_baser(struct its_node *its, struct its_baser *baser,
2377 u64 val)
2378 {
2379 u32 idx = baser - its->tables;
2380
2381 gits_write_baser(val, its->base + GITS_BASER + (idx << 3));
2382 baser->val = its_read_baser(its, baser);
2383 }
2384
its_setup_baser(struct its_node * its,struct its_baser * baser,u64 cache,u64 shr,u32 order,bool indirect)2385 static int its_setup_baser(struct its_node *its, struct its_baser *baser,
2386 u64 cache, u64 shr, u32 order, bool indirect)
2387 {
2388 u64 val = its_read_baser(its, baser);
2389 u64 esz = GITS_BASER_ENTRY_SIZE(val);
2390 u64 type = GITS_BASER_TYPE(val);
2391 u64 baser_phys, tmp;
2392 u32 alloc_pages, psz;
2393 struct page *page;
2394 void *base;
2395
2396 psz = baser->psz;
2397 alloc_pages = (PAGE_ORDER_TO_SIZE(order) / psz);
2398 if (alloc_pages > GITS_BASER_PAGES_MAX) {
2399 pr_warn("ITS@%pa: %s too large, reduce ITS pages %u->%u\n",
2400 &its->phys_base, its_base_type_string[type],
2401 alloc_pages, GITS_BASER_PAGES_MAX);
2402 alloc_pages = GITS_BASER_PAGES_MAX;
2403 order = get_order(GITS_BASER_PAGES_MAX * psz);
2404 }
2405
2406 page = its_alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO, order);
2407 if (!page)
2408 return -ENOMEM;
2409
2410 base = (void *)page_address(page);
2411 baser_phys = virt_to_phys(base);
2412
2413 /* Check if the physical address of the memory is above 48bits */
2414 if (IS_ENABLED(CONFIG_ARM64_64K_PAGES) && (baser_phys >> 48)) {
2415
2416 /* 52bit PA is supported only when PageSize=64K */
2417 if (psz != SZ_64K) {
2418 pr_err("ITS: no 52bit PA support when psz=%d\n", psz);
2419 its_free_pages(base, order);
2420 return -ENXIO;
2421 }
2422
2423 /* Convert 52bit PA to 48bit field */
2424 baser_phys = GITS_BASER_PHYS_52_to_48(baser_phys);
2425 }
2426
2427 retry_baser:
2428 val = (baser_phys |
2429 (type << GITS_BASER_TYPE_SHIFT) |
2430 ((esz - 1) << GITS_BASER_ENTRY_SIZE_SHIFT) |
2431 ((alloc_pages - 1) << GITS_BASER_PAGES_SHIFT) |
2432 cache |
2433 shr |
2434 GITS_BASER_VALID);
2435
2436 val |= indirect ? GITS_BASER_INDIRECT : 0x0;
2437
2438 switch (psz) {
2439 case SZ_4K:
2440 val |= GITS_BASER_PAGE_SIZE_4K;
2441 break;
2442 case SZ_16K:
2443 val |= GITS_BASER_PAGE_SIZE_16K;
2444 break;
2445 case SZ_64K:
2446 val |= GITS_BASER_PAGE_SIZE_64K;
2447 break;
2448 }
2449
2450 if (!shr)
2451 gic_flush_dcache_to_poc(base, PAGE_ORDER_TO_SIZE(order));
2452
2453 its_write_baser(its, baser, val);
2454 tmp = baser->val;
2455
2456 if ((val ^ tmp) & GITS_BASER_SHAREABILITY_MASK) {
2457 /*
2458 * Shareability didn't stick. Just use
2459 * whatever the read reported, which is likely
2460 * to be the only thing this redistributor
2461 * supports. If that's zero, make it
2462 * non-cacheable as well.
2463 */
2464 shr = tmp & GITS_BASER_SHAREABILITY_MASK;
2465 if (!shr)
2466 cache = GITS_BASER_nC;
2467
2468 goto retry_baser;
2469 }
2470
2471 if (val != tmp) {
2472 pr_err("ITS@%pa: %s doesn't stick: %llx %llx\n",
2473 &its->phys_base, its_base_type_string[type],
2474 val, tmp);
2475 its_free_pages(base, order);
2476 return -ENXIO;
2477 }
2478
2479 baser->order = order;
2480 baser->base = base;
2481 baser->psz = psz;
2482 tmp = indirect ? GITS_LVL1_ENTRY_SIZE : esz;
2483
2484 pr_info("ITS@%pa: allocated %d %s @%lx (%s, esz %d, psz %dK, shr %d)\n",
2485 &its->phys_base, (int)(PAGE_ORDER_TO_SIZE(order) / (int)tmp),
2486 its_base_type_string[type],
2487 (unsigned long)virt_to_phys(base),
2488 indirect ? "indirect" : "flat", (int)esz,
2489 psz / SZ_1K, (int)shr >> GITS_BASER_SHAREABILITY_SHIFT);
2490
2491 return 0;
2492 }
2493
its_parse_indirect_baser(struct its_node * its,struct its_baser * baser,u32 * order,u32 ids)2494 static bool its_parse_indirect_baser(struct its_node *its,
2495 struct its_baser *baser,
2496 u32 *order, u32 ids)
2497 {
2498 u64 tmp = its_read_baser(its, baser);
2499 u64 type = GITS_BASER_TYPE(tmp);
2500 u64 esz = GITS_BASER_ENTRY_SIZE(tmp);
2501 u64 val = GITS_BASER_InnerShareable | GITS_BASER_RaWaWb;
2502 u32 new_order = *order;
2503 u32 psz = baser->psz;
2504 bool indirect = false;
2505
2506 /* No need to enable Indirection if memory requirement < (psz*2)bytes */
2507 if ((esz << ids) > (psz * 2)) {
2508 /*
2509 * Find out whether hw supports a single or two-level table by
2510 * table by reading bit at offset '62' after writing '1' to it.
2511 */
2512 its_write_baser(its, baser, val | GITS_BASER_INDIRECT);
2513 indirect = !!(baser->val & GITS_BASER_INDIRECT);
2514
2515 if (indirect) {
2516 /*
2517 * The size of the lvl2 table is equal to ITS page size
2518 * which is 'psz'. For computing lvl1 table size,
2519 * subtract ID bits that sparse lvl2 table from 'ids'
2520 * which is reported by ITS hardware times lvl1 table
2521 * entry size.
2522 */
2523 ids -= ilog2(psz / (int)esz);
2524 esz = GITS_LVL1_ENTRY_SIZE;
2525 }
2526 }
2527
2528 /*
2529 * Allocate as many entries as required to fit the
2530 * range of device IDs that the ITS can grok... The ID
2531 * space being incredibly sparse, this results in a
2532 * massive waste of memory if two-level device table
2533 * feature is not supported by hardware.
2534 */
2535 new_order = max_t(u32, get_order(esz << ids), new_order);
2536 if (new_order > MAX_PAGE_ORDER) {
2537 new_order = MAX_PAGE_ORDER;
2538 ids = ilog2(PAGE_ORDER_TO_SIZE(new_order) / (int)esz);
2539 pr_warn("ITS@%pa: %s Table too large, reduce ids %llu->%u\n",
2540 &its->phys_base, its_base_type_string[type],
2541 device_ids(its), ids);
2542 }
2543
2544 *order = new_order;
2545
2546 return indirect;
2547 }
2548
compute_common_aff(u64 val)2549 static u32 compute_common_aff(u64 val)
2550 {
2551 u32 aff, clpiaff;
2552
2553 aff = FIELD_GET(GICR_TYPER_AFFINITY, val);
2554 clpiaff = FIELD_GET(GICR_TYPER_COMMON_LPI_AFF, val);
2555
2556 return aff & ~(GENMASK(31, 0) >> (clpiaff * 8));
2557 }
2558
compute_its_aff(struct its_node * its)2559 static u32 compute_its_aff(struct its_node *its)
2560 {
2561 u64 val;
2562 u32 svpet;
2563
2564 /*
2565 * Reencode the ITS SVPET and MPIDR as a GICR_TYPER, and compute
2566 * the resulting affinity. We then use that to see if this match
2567 * our own affinity.
2568 */
2569 svpet = FIELD_GET(GITS_TYPER_SVPET, its->typer);
2570 val = FIELD_PREP(GICR_TYPER_COMMON_LPI_AFF, svpet);
2571 val |= FIELD_PREP(GICR_TYPER_AFFINITY, its->mpidr);
2572 return compute_common_aff(val);
2573 }
2574
find_sibling_its(struct its_node * cur_its)2575 static struct its_node *find_sibling_its(struct its_node *cur_its)
2576 {
2577 struct its_node *its;
2578 u32 aff;
2579
2580 if (!FIELD_GET(GITS_TYPER_SVPET, cur_its->typer))
2581 return NULL;
2582
2583 aff = compute_its_aff(cur_its);
2584
2585 list_for_each_entry(its, &its_nodes, entry) {
2586 u64 baser;
2587
2588 if (!is_v4_1(its) || its == cur_its)
2589 continue;
2590
2591 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2592 continue;
2593
2594 if (aff != compute_its_aff(its))
2595 continue;
2596
2597 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2598 baser = its->tables[2].val;
2599 if (!(baser & GITS_BASER_VALID))
2600 continue;
2601
2602 return its;
2603 }
2604
2605 return NULL;
2606 }
2607
its_free_tables(struct its_node * its)2608 static void its_free_tables(struct its_node *its)
2609 {
2610 int i;
2611
2612 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2613 if (its->tables[i].base) {
2614 its_free_pages(its->tables[i].base, its->tables[i].order);
2615 its->tables[i].base = NULL;
2616 }
2617 }
2618 }
2619
its_probe_baser_psz(struct its_node * its,struct its_baser * baser)2620 static int its_probe_baser_psz(struct its_node *its, struct its_baser *baser)
2621 {
2622 u64 psz = SZ_64K;
2623
2624 while (psz) {
2625 u64 val, gpsz;
2626
2627 val = its_read_baser(its, baser);
2628 val &= ~GITS_BASER_PAGE_SIZE_MASK;
2629
2630 switch (psz) {
2631 case SZ_64K:
2632 gpsz = GITS_BASER_PAGE_SIZE_64K;
2633 break;
2634 case SZ_16K:
2635 gpsz = GITS_BASER_PAGE_SIZE_16K;
2636 break;
2637 case SZ_4K:
2638 default:
2639 gpsz = GITS_BASER_PAGE_SIZE_4K;
2640 break;
2641 }
2642
2643 gpsz >>= GITS_BASER_PAGE_SIZE_SHIFT;
2644
2645 val |= FIELD_PREP(GITS_BASER_PAGE_SIZE_MASK, gpsz);
2646 its_write_baser(its, baser, val);
2647
2648 if (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser->val) == gpsz)
2649 break;
2650
2651 switch (psz) {
2652 case SZ_64K:
2653 psz = SZ_16K;
2654 break;
2655 case SZ_16K:
2656 psz = SZ_4K;
2657 break;
2658 case SZ_4K:
2659 default:
2660 return -1;
2661 }
2662 }
2663
2664 baser->psz = psz;
2665 return 0;
2666 }
2667
its_alloc_tables(struct its_node * its)2668 static int its_alloc_tables(struct its_node *its)
2669 {
2670 u64 shr = GITS_BASER_InnerShareable;
2671 u64 cache = GITS_BASER_RaWaWb;
2672 int err, i;
2673
2674 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_22375)
2675 /* erratum 24313: ignore memory access type */
2676 cache = GITS_BASER_nCnB;
2677
2678 if (its->flags & ITS_FLAGS_FORCE_NON_SHAREABLE) {
2679 cache = GITS_BASER_nC;
2680 shr = 0;
2681 }
2682
2683 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
2684 struct its_baser *baser = its->tables + i;
2685 u64 val = its_read_baser(its, baser);
2686 u64 type = GITS_BASER_TYPE(val);
2687 bool indirect = false;
2688 u32 order;
2689
2690 if (type == GITS_BASER_TYPE_NONE)
2691 continue;
2692
2693 if (its_probe_baser_psz(its, baser)) {
2694 its_free_tables(its);
2695 return -ENXIO;
2696 }
2697
2698 order = get_order(baser->psz);
2699
2700 switch (type) {
2701 case GITS_BASER_TYPE_DEVICE:
2702 indirect = its_parse_indirect_baser(its, baser, &order,
2703 device_ids(its));
2704 break;
2705
2706 case GITS_BASER_TYPE_VCPU:
2707 if (is_v4_1(its)) {
2708 struct its_node *sibling;
2709
2710 WARN_ON(i != 2);
2711 if ((sibling = find_sibling_its(its))) {
2712 *baser = sibling->tables[2];
2713 its_write_baser(its, baser, baser->val);
2714 continue;
2715 }
2716 }
2717
2718 indirect = its_parse_indirect_baser(its, baser, &order,
2719 ITS_MAX_VPEID_BITS);
2720 break;
2721 }
2722
2723 err = its_setup_baser(its, baser, cache, shr, order, indirect);
2724 if (err < 0) {
2725 its_free_tables(its);
2726 return err;
2727 }
2728
2729 /* Update settings which will be used for next BASERn */
2730 cache = baser->val & GITS_BASER_CACHEABILITY_MASK;
2731 shr = baser->val & GITS_BASER_SHAREABILITY_MASK;
2732 }
2733
2734 return 0;
2735 }
2736
inherit_vpe_l1_table_from_its(void)2737 static u64 inherit_vpe_l1_table_from_its(void)
2738 {
2739 struct its_node *its;
2740 u64 val;
2741 u32 aff;
2742
2743 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2744 aff = compute_common_aff(val);
2745
2746 list_for_each_entry(its, &its_nodes, entry) {
2747 u64 baser, addr;
2748
2749 if (!is_v4_1(its))
2750 continue;
2751
2752 if (!FIELD_GET(GITS_TYPER_SVPET, its->typer))
2753 continue;
2754
2755 if (aff != compute_its_aff(its))
2756 continue;
2757
2758 /* GICv4.1 guarantees that the vPE table is GITS_BASER2 */
2759 baser = its->tables[2].val;
2760 if (!(baser & GITS_BASER_VALID))
2761 continue;
2762
2763 /* We have a winner! */
2764 gic_data_rdist()->vpe_l1_base = its->tables[2].base;
2765
2766 val = GICR_VPROPBASER_4_1_VALID;
2767 if (baser & GITS_BASER_INDIRECT)
2768 val |= GICR_VPROPBASER_4_1_INDIRECT;
2769 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE,
2770 FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser));
2771 switch (FIELD_GET(GITS_BASER_PAGE_SIZE_MASK, baser)) {
2772 case GIC_PAGE_SIZE_64K:
2773 addr = GITS_BASER_ADDR_48_to_52(baser);
2774 break;
2775 default:
2776 addr = baser & GENMASK_ULL(47, 12);
2777 break;
2778 }
2779 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, addr >> 12);
2780 if (rdists_support_shareable()) {
2781 val |= FIELD_PREP(GICR_VPROPBASER_SHAREABILITY_MASK,
2782 FIELD_GET(GITS_BASER_SHAREABILITY_MASK, baser));
2783 val |= FIELD_PREP(GICR_VPROPBASER_INNER_CACHEABILITY_MASK,
2784 FIELD_GET(GITS_BASER_INNER_CACHEABILITY_MASK, baser));
2785 }
2786 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, GITS_BASER_NR_PAGES(baser) - 1);
2787
2788 return val;
2789 }
2790
2791 return 0;
2792 }
2793
inherit_vpe_l1_table_from_rd(cpumask_t ** mask)2794 static u64 inherit_vpe_l1_table_from_rd(cpumask_t **mask)
2795 {
2796 u32 aff;
2797 u64 val;
2798 int cpu;
2799
2800 val = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
2801 aff = compute_common_aff(val);
2802
2803 for_each_possible_cpu(cpu) {
2804 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
2805
2806 if (!base || cpu == smp_processor_id())
2807 continue;
2808
2809 val = gic_read_typer(base + GICR_TYPER);
2810 if (aff != compute_common_aff(val))
2811 continue;
2812
2813 /*
2814 * At this point, we have a victim. This particular CPU
2815 * has already booted, and has an affinity that matches
2816 * ours wrt CommonLPIAff. Let's use its own VPROPBASER.
2817 * Make sure we don't write the Z bit in that case.
2818 */
2819 val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
2820 val &= ~GICR_VPROPBASER_4_1_Z;
2821
2822 gic_data_rdist()->vpe_l1_base = gic_data_rdist_cpu(cpu)->vpe_l1_base;
2823 *mask = gic_data_rdist_cpu(cpu)->vpe_table_mask;
2824
2825 return val;
2826 }
2827
2828 return 0;
2829 }
2830
allocate_vpe_l2_table(int cpu,u32 id)2831 static bool allocate_vpe_l2_table(int cpu, u32 id)
2832 {
2833 void __iomem *base = gic_data_rdist_cpu(cpu)->rd_base;
2834 unsigned int psz, esz, idx, npg, gpsz;
2835 u64 val;
2836 struct page *page;
2837 __le64 *table;
2838
2839 if (!gic_rdists->has_rvpeid)
2840 return true;
2841
2842 /* Skip non-present CPUs */
2843 if (!base)
2844 return true;
2845
2846 val = gicr_read_vpropbaser(base + SZ_128K + GICR_VPROPBASER);
2847
2848 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val) + 1;
2849 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2850 npg = FIELD_GET(GICR_VPROPBASER_4_1_SIZE, val) + 1;
2851
2852 switch (gpsz) {
2853 default:
2854 WARN_ON(1);
2855 fallthrough;
2856 case GIC_PAGE_SIZE_4K:
2857 psz = SZ_4K;
2858 break;
2859 case GIC_PAGE_SIZE_16K:
2860 psz = SZ_16K;
2861 break;
2862 case GIC_PAGE_SIZE_64K:
2863 psz = SZ_64K;
2864 break;
2865 }
2866
2867 /* Don't allow vpe_id that exceeds single, flat table limit */
2868 if (!(val & GICR_VPROPBASER_4_1_INDIRECT))
2869 return (id < (npg * psz / (esz * SZ_8)));
2870
2871 /* Compute 1st level table index & check if that exceeds table limit */
2872 idx = id >> ilog2(psz / (esz * SZ_8));
2873 if (idx >= (npg * psz / GITS_LVL1_ENTRY_SIZE))
2874 return false;
2875
2876 table = gic_data_rdist_cpu(cpu)->vpe_l1_base;
2877
2878 /* Allocate memory for 2nd level table */
2879 if (!table[idx]) {
2880 page = its_alloc_pages(GFP_KERNEL | __GFP_ZERO, get_order(psz));
2881 if (!page)
2882 return false;
2883
2884 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
2885 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2886 gic_flush_dcache_to_poc(page_address(page), psz);
2887
2888 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
2889
2890 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
2891 if (!(val & GICR_VPROPBASER_SHAREABILITY_MASK))
2892 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
2893
2894 /* Ensure updated table contents are visible to RD hardware */
2895 dsb(sy);
2896 }
2897
2898 return true;
2899 }
2900
allocate_vpe_l1_table(void)2901 static int allocate_vpe_l1_table(void)
2902 {
2903 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
2904 u64 val, gpsz, npg, pa;
2905 unsigned int psz = SZ_64K;
2906 unsigned int np, epp, esz;
2907 struct page *page;
2908
2909 if (!gic_rdists->has_rvpeid)
2910 return 0;
2911
2912 /*
2913 * if VPENDBASER.Valid is set, disable any previously programmed
2914 * VPE by setting PendingLast while clearing Valid. This has the
2915 * effect of making sure no doorbell will be generated and we can
2916 * then safely clear VPROPBASER.Valid.
2917 */
2918 if (gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER) & GICR_VPENDBASER_Valid)
2919 gicr_write_vpendbaser(GICR_VPENDBASER_PendingLast,
2920 vlpi_base + GICR_VPENDBASER);
2921
2922 /*
2923 * If we can inherit the configuration from another RD, let's do
2924 * so. Otherwise, we have to go through the allocation process. We
2925 * assume that all RDs have the exact same requirements, as
2926 * nothing will work otherwise.
2927 */
2928 val = inherit_vpe_l1_table_from_rd(&gic_data_rdist()->vpe_table_mask);
2929 if (val & GICR_VPROPBASER_4_1_VALID)
2930 goto out;
2931
2932 gic_data_rdist()->vpe_table_mask = kzalloc(sizeof(cpumask_t), GFP_ATOMIC);
2933 if (!gic_data_rdist()->vpe_table_mask)
2934 return -ENOMEM;
2935
2936 val = inherit_vpe_l1_table_from_its();
2937 if (val & GICR_VPROPBASER_4_1_VALID)
2938 goto out;
2939
2940 /* First probe the page size */
2941 val = FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, GIC_PAGE_SIZE_64K);
2942 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
2943 val = gicr_read_vpropbaser(vlpi_base + GICR_VPROPBASER);
2944 gpsz = FIELD_GET(GICR_VPROPBASER_4_1_PAGE_SIZE, val);
2945 esz = FIELD_GET(GICR_VPROPBASER_4_1_ENTRY_SIZE, val);
2946
2947 switch (gpsz) {
2948 default:
2949 gpsz = GIC_PAGE_SIZE_4K;
2950 fallthrough;
2951 case GIC_PAGE_SIZE_4K:
2952 psz = SZ_4K;
2953 break;
2954 case GIC_PAGE_SIZE_16K:
2955 psz = SZ_16K;
2956 break;
2957 case GIC_PAGE_SIZE_64K:
2958 psz = SZ_64K;
2959 break;
2960 }
2961
2962 /*
2963 * Start populating the register from scratch, including RO fields
2964 * (which we want to print in debug cases...)
2965 */
2966 val = 0;
2967 val |= FIELD_PREP(GICR_VPROPBASER_4_1_PAGE_SIZE, gpsz);
2968 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ENTRY_SIZE, esz);
2969
2970 /* How many entries per GIC page? */
2971 esz++;
2972 epp = psz / (esz * SZ_8);
2973
2974 /*
2975 * If we need more than just a single L1 page, flag the table
2976 * as indirect and compute the number of required L1 pages.
2977 */
2978 if (epp < ITS_MAX_VPEID) {
2979 int nl2;
2980
2981 val |= GICR_VPROPBASER_4_1_INDIRECT;
2982
2983 /* Number of L2 pages required to cover the VPEID space */
2984 nl2 = DIV_ROUND_UP(ITS_MAX_VPEID, epp);
2985
2986 /* Number of L1 pages to point to the L2 pages */
2987 npg = DIV_ROUND_UP(nl2 * SZ_8, psz);
2988 } else {
2989 npg = 1;
2990 }
2991
2992 val |= FIELD_PREP(GICR_VPROPBASER_4_1_SIZE, npg - 1);
2993
2994 /* Right, that's the number of CPU pages we need for L1 */
2995 np = DIV_ROUND_UP(npg * psz, PAGE_SIZE);
2996
2997 pr_debug("np = %d, npg = %lld, psz = %d, epp = %d, esz = %d\n",
2998 np, npg, psz, epp, esz);
2999 page = its_alloc_pages(GFP_ATOMIC | __GFP_ZERO, get_order(np * PAGE_SIZE));
3000 if (!page)
3001 return -ENOMEM;
3002
3003 gic_data_rdist()->vpe_l1_base = page_address(page);
3004 pa = virt_to_phys(page_address(page));
3005 WARN_ON(!IS_ALIGNED(pa, psz));
3006
3007 val |= FIELD_PREP(GICR_VPROPBASER_4_1_ADDR, pa >> 12);
3008 if (rdists_support_shareable()) {
3009 val |= GICR_VPROPBASER_RaWb;
3010 val |= GICR_VPROPBASER_InnerShareable;
3011 }
3012 val |= GICR_VPROPBASER_4_1_Z;
3013 val |= GICR_VPROPBASER_4_1_VALID;
3014
3015 out:
3016 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
3017 cpumask_set_cpu(smp_processor_id(), gic_data_rdist()->vpe_table_mask);
3018
3019 pr_debug("CPU%d: VPROPBASER = %llx %*pbl\n",
3020 smp_processor_id(), val,
3021 cpumask_pr_args(gic_data_rdist()->vpe_table_mask));
3022
3023 return 0;
3024 }
3025
its_alloc_collections(struct its_node * its)3026 static int its_alloc_collections(struct its_node *its)
3027 {
3028 int i;
3029
3030 its->collections = kcalloc(nr_cpu_ids, sizeof(*its->collections),
3031 GFP_KERNEL);
3032 if (!its->collections)
3033 return -ENOMEM;
3034
3035 for (i = 0; i < nr_cpu_ids; i++)
3036 its->collections[i].target_address = ~0ULL;
3037
3038 return 0;
3039 }
3040
its_allocate_pending_table(gfp_t gfp_flags)3041 static struct page *its_allocate_pending_table(gfp_t gfp_flags)
3042 {
3043 struct page *pend_page;
3044
3045 pend_page = its_alloc_pages(gfp_flags | __GFP_ZERO, get_order(LPI_PENDBASE_SZ));
3046 if (!pend_page)
3047 return NULL;
3048
3049 /* Make sure the GIC will observe the zero-ed page */
3050 gic_flush_dcache_to_poc(page_address(pend_page), LPI_PENDBASE_SZ);
3051
3052 return pend_page;
3053 }
3054
its_free_pending_table(struct page * pt)3055 static void its_free_pending_table(struct page *pt)
3056 {
3057 its_free_pages(page_address(pt), get_order(LPI_PENDBASE_SZ));
3058 }
3059
3060 /*
3061 * Booting with kdump and LPIs enabled is generally fine. Any other
3062 * case is wrong in the absence of firmware/EFI support.
3063 */
enabled_lpis_allowed(void)3064 static bool enabled_lpis_allowed(void)
3065 {
3066 phys_addr_t addr;
3067 u64 val;
3068
3069 /* Check whether the property table is in a reserved region */
3070 val = gicr_read_propbaser(gic_data_rdist_rd_base() + GICR_PROPBASER);
3071 addr = val & GENMASK_ULL(51, 12);
3072
3073 return gic_check_reserved_range(addr, LPI_PROPBASE_SZ);
3074 }
3075
allocate_lpi_tables(void)3076 static int __init allocate_lpi_tables(void)
3077 {
3078 u64 val;
3079 int err, cpu;
3080
3081 /*
3082 * If LPIs are enabled while we run this from the boot CPU,
3083 * flag the RD tables as pre-allocated if the stars do align.
3084 */
3085 val = readl_relaxed(gic_data_rdist_rd_base() + GICR_CTLR);
3086 if ((val & GICR_CTLR_ENABLE_LPIS) && enabled_lpis_allowed()) {
3087 gic_rdists->flags |= (RDIST_FLAGS_RD_TABLES_PREALLOCATED |
3088 RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING);
3089 pr_info("GICv3: Using preallocated redistributor tables\n");
3090 }
3091
3092 err = its_setup_lpi_prop_table();
3093 if (err)
3094 return err;
3095
3096 /*
3097 * We allocate all the pending tables anyway, as we may have a
3098 * mix of RDs that have had LPIs enabled, and some that
3099 * don't. We'll free the unused ones as each CPU comes online.
3100 */
3101 for_each_possible_cpu(cpu) {
3102 struct page *pend_page;
3103
3104 pend_page = its_allocate_pending_table(GFP_NOWAIT);
3105 if (!pend_page) {
3106 pr_err("Failed to allocate PENDBASE for CPU%d\n", cpu);
3107 return -ENOMEM;
3108 }
3109
3110 gic_data_rdist_cpu(cpu)->pend_page = pend_page;
3111 }
3112
3113 return 0;
3114 }
3115
read_vpend_dirty_clear(void __iomem * vlpi_base)3116 static u64 read_vpend_dirty_clear(void __iomem *vlpi_base)
3117 {
3118 u32 count = 1000000; /* 1s! */
3119 bool clean;
3120 u64 val;
3121
3122 do {
3123 val = gicr_read_vpendbaser(vlpi_base + GICR_VPENDBASER);
3124 clean = !(val & GICR_VPENDBASER_Dirty);
3125 if (!clean) {
3126 count--;
3127 cpu_relax();
3128 udelay(1);
3129 }
3130 } while (!clean && count);
3131
3132 if (unlikely(!clean))
3133 pr_err_ratelimited("ITS virtual pending table not cleaning\n");
3134
3135 return val;
3136 }
3137
its_clear_vpend_valid(void __iomem * vlpi_base,u64 clr,u64 set)3138 static u64 its_clear_vpend_valid(void __iomem *vlpi_base, u64 clr, u64 set)
3139 {
3140 u64 val;
3141
3142 /* Make sure we wait until the RD is done with the initial scan */
3143 val = read_vpend_dirty_clear(vlpi_base);
3144 val &= ~GICR_VPENDBASER_Valid;
3145 val &= ~clr;
3146 val |= set;
3147 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
3148
3149 val = read_vpend_dirty_clear(vlpi_base);
3150 if (unlikely(val & GICR_VPENDBASER_Dirty))
3151 val |= GICR_VPENDBASER_PendingLast;
3152
3153 return val;
3154 }
3155
its_cpu_init_lpis(void)3156 static void its_cpu_init_lpis(void)
3157 {
3158 void __iomem *rbase = gic_data_rdist_rd_base();
3159 struct page *pend_page;
3160 phys_addr_t paddr;
3161 u64 val, tmp;
3162
3163 if (gic_data_rdist()->flags & RD_LOCAL_LPI_ENABLED)
3164 return;
3165
3166 val = readl_relaxed(rbase + GICR_CTLR);
3167 if ((gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED) &&
3168 (val & GICR_CTLR_ENABLE_LPIS)) {
3169 /*
3170 * Check that we get the same property table on all
3171 * RDs. If we don't, this is hopeless.
3172 */
3173 paddr = gicr_read_propbaser(rbase + GICR_PROPBASER);
3174 paddr &= GENMASK_ULL(51, 12);
3175 if (WARN_ON(gic_rdists->prop_table_pa != paddr))
3176 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
3177
3178 paddr = gicr_read_pendbaser(rbase + GICR_PENDBASER);
3179 paddr &= GENMASK_ULL(51, 16);
3180
3181 WARN_ON(!gic_check_reserved_range(paddr, LPI_PENDBASE_SZ));
3182 gic_data_rdist()->flags |= RD_LOCAL_PENDTABLE_PREALLOCATED;
3183
3184 goto out;
3185 }
3186
3187 pend_page = gic_data_rdist()->pend_page;
3188 paddr = page_to_phys(pend_page);
3189
3190 /* set PROPBASE */
3191 val = (gic_rdists->prop_table_pa |
3192 GICR_PROPBASER_InnerShareable |
3193 GICR_PROPBASER_RaWaWb |
3194 ((LPI_NRBITS - 1) & GICR_PROPBASER_IDBITS_MASK));
3195
3196 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
3197 tmp = gicr_read_propbaser(rbase + GICR_PROPBASER);
3198
3199 if (!rdists_support_shareable())
3200 tmp &= ~GICR_PROPBASER_SHAREABILITY_MASK;
3201
3202 if ((tmp ^ val) & GICR_PROPBASER_SHAREABILITY_MASK) {
3203 if (!(tmp & GICR_PROPBASER_SHAREABILITY_MASK)) {
3204 /*
3205 * The HW reports non-shareable, we must
3206 * remove the cacheability attributes as
3207 * well.
3208 */
3209 val &= ~(GICR_PROPBASER_SHAREABILITY_MASK |
3210 GICR_PROPBASER_CACHEABILITY_MASK);
3211 val |= GICR_PROPBASER_nC;
3212 gicr_write_propbaser(val, rbase + GICR_PROPBASER);
3213 }
3214 pr_info_once("GIC: using cache flushing for LPI property table\n");
3215 gic_rdists->flags |= RDIST_FLAGS_PROPBASE_NEEDS_FLUSHING;
3216 }
3217
3218 /* set PENDBASE */
3219 val = (page_to_phys(pend_page) |
3220 GICR_PENDBASER_InnerShareable |
3221 GICR_PENDBASER_RaWaWb);
3222
3223 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
3224 tmp = gicr_read_pendbaser(rbase + GICR_PENDBASER);
3225
3226 if (!rdists_support_shareable())
3227 tmp &= ~GICR_PENDBASER_SHAREABILITY_MASK;
3228
3229 if (!(tmp & GICR_PENDBASER_SHAREABILITY_MASK)) {
3230 /*
3231 * The HW reports non-shareable, we must remove the
3232 * cacheability attributes as well.
3233 */
3234 val &= ~(GICR_PENDBASER_SHAREABILITY_MASK |
3235 GICR_PENDBASER_CACHEABILITY_MASK);
3236 val |= GICR_PENDBASER_nC;
3237 gicr_write_pendbaser(val, rbase + GICR_PENDBASER);
3238 }
3239
3240 /* Enable LPIs */
3241 val = readl_relaxed(rbase + GICR_CTLR);
3242 val |= GICR_CTLR_ENABLE_LPIS;
3243 writel_relaxed(val, rbase + GICR_CTLR);
3244
3245 out:
3246 if (gic_rdists->has_vlpis && !gic_rdists->has_rvpeid) {
3247 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3248
3249 /*
3250 * It's possible for CPU to receive VLPIs before it is
3251 * scheduled as a vPE, especially for the first CPU, and the
3252 * VLPI with INTID larger than 2^(IDbits+1) will be considered
3253 * as out of range and dropped by GIC.
3254 * So we initialize IDbits to known value to avoid VLPI drop.
3255 */
3256 val = (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
3257 pr_debug("GICv4: CPU%d: Init IDbits to 0x%llx for GICR_VPROPBASER\n",
3258 smp_processor_id(), val);
3259 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
3260
3261 /*
3262 * Also clear Valid bit of GICR_VPENDBASER, in case some
3263 * ancient programming gets left in and has possibility of
3264 * corrupting memory.
3265 */
3266 val = its_clear_vpend_valid(vlpi_base, 0, 0);
3267 }
3268
3269 if (allocate_vpe_l1_table()) {
3270 /*
3271 * If the allocation has failed, we're in massive trouble.
3272 * Disable direct injection, and pray that no VM was
3273 * already running...
3274 */
3275 gic_rdists->has_rvpeid = false;
3276 gic_rdists->has_vlpis = false;
3277 }
3278
3279 /* Make sure the GIC has seen the above */
3280 dsb(sy);
3281 gic_data_rdist()->flags |= RD_LOCAL_LPI_ENABLED;
3282 pr_info("GICv3: CPU%d: using %s LPI pending table @%pa\n",
3283 smp_processor_id(),
3284 gic_data_rdist()->flags & RD_LOCAL_PENDTABLE_PREALLOCATED ?
3285 "reserved" : "allocated",
3286 &paddr);
3287 }
3288
its_cpu_init_collection(struct its_node * its)3289 static void its_cpu_init_collection(struct its_node *its)
3290 {
3291 int cpu = smp_processor_id();
3292 u64 target;
3293
3294 /* avoid cross node collections and its mapping */
3295 if (its->flags & ITS_FLAGS_WORKAROUND_CAVIUM_23144) {
3296 struct device_node *cpu_node;
3297
3298 cpu_node = of_get_cpu_node(cpu, NULL);
3299 if (its->numa_node != NUMA_NO_NODE &&
3300 its->numa_node != of_node_to_nid(cpu_node))
3301 return;
3302 }
3303
3304 /*
3305 * We now have to bind each collection to its target
3306 * redistributor.
3307 */
3308 if (gic_read_typer(its->base + GITS_TYPER) & GITS_TYPER_PTA) {
3309 /*
3310 * This ITS wants the physical address of the
3311 * redistributor.
3312 */
3313 target = gic_data_rdist()->phys_base;
3314 } else {
3315 /* This ITS wants a linear CPU number. */
3316 target = gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER);
3317 target = GICR_TYPER_CPU_NUMBER(target) << 16;
3318 }
3319
3320 /* Perform collection mapping */
3321 its->collections[cpu].target_address = target;
3322 its->collections[cpu].col_id = cpu;
3323
3324 its_send_mapc(its, &its->collections[cpu], 1);
3325 its_send_invall(its, &its->collections[cpu]);
3326 }
3327
its_cpu_init_collections(void)3328 static void its_cpu_init_collections(void)
3329 {
3330 struct its_node *its;
3331
3332 raw_spin_lock(&its_lock);
3333
3334 list_for_each_entry(its, &its_nodes, entry)
3335 its_cpu_init_collection(its);
3336
3337 raw_spin_unlock(&its_lock);
3338 }
3339
its_find_device(struct its_node * its,u32 dev_id)3340 static struct its_device *its_find_device(struct its_node *its, u32 dev_id)
3341 {
3342 struct its_device *its_dev = NULL, *tmp;
3343 unsigned long flags;
3344
3345 raw_spin_lock_irqsave(&its->lock, flags);
3346
3347 list_for_each_entry(tmp, &its->its_device_list, entry) {
3348 if (tmp->device_id == dev_id) {
3349 its_dev = tmp;
3350 break;
3351 }
3352 }
3353
3354 raw_spin_unlock_irqrestore(&its->lock, flags);
3355
3356 return its_dev;
3357 }
3358
its_get_baser(struct its_node * its,u32 type)3359 static struct its_baser *its_get_baser(struct its_node *its, u32 type)
3360 {
3361 int i;
3362
3363 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
3364 if (GITS_BASER_TYPE(its->tables[i].val) == type)
3365 return &its->tables[i];
3366 }
3367
3368 return NULL;
3369 }
3370
its_alloc_table_entry(struct its_node * its,struct its_baser * baser,u32 id)3371 static bool its_alloc_table_entry(struct its_node *its,
3372 struct its_baser *baser, u32 id)
3373 {
3374 struct page *page;
3375 u32 esz, idx;
3376 __le64 *table;
3377
3378 /* Don't allow device id that exceeds single, flat table limit */
3379 esz = GITS_BASER_ENTRY_SIZE(baser->val);
3380 if (!(baser->val & GITS_BASER_INDIRECT))
3381 return (id < (PAGE_ORDER_TO_SIZE(baser->order) / esz));
3382
3383 /* Compute 1st level table index & check if that exceeds table limit */
3384 idx = id >> ilog2(baser->psz / esz);
3385 if (idx >= (PAGE_ORDER_TO_SIZE(baser->order) / GITS_LVL1_ENTRY_SIZE))
3386 return false;
3387
3388 table = baser->base;
3389
3390 /* Allocate memory for 2nd level table */
3391 if (!table[idx]) {
3392 page = its_alloc_pages_node(its->numa_node, GFP_KERNEL | __GFP_ZERO,
3393 get_order(baser->psz));
3394 if (!page)
3395 return false;
3396
3397 /* Flush Lvl2 table to PoC if hw doesn't support coherency */
3398 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
3399 gic_flush_dcache_to_poc(page_address(page), baser->psz);
3400
3401 table[idx] = cpu_to_le64(page_to_phys(page) | GITS_BASER_VALID);
3402
3403 /* Flush Lvl1 entry to PoC if hw doesn't support coherency */
3404 if (!(baser->val & GITS_BASER_SHAREABILITY_MASK))
3405 gic_flush_dcache_to_poc(table + idx, GITS_LVL1_ENTRY_SIZE);
3406
3407 /* Ensure updated table contents are visible to ITS hardware */
3408 dsb(sy);
3409 }
3410
3411 return true;
3412 }
3413
its_alloc_device_table(struct its_node * its,u32 dev_id)3414 static bool its_alloc_device_table(struct its_node *its, u32 dev_id)
3415 {
3416 struct its_baser *baser;
3417
3418 baser = its_get_baser(its, GITS_BASER_TYPE_DEVICE);
3419
3420 /* Don't allow device id that exceeds ITS hardware limit */
3421 if (!baser)
3422 return (ilog2(dev_id) < device_ids(its));
3423
3424 return its_alloc_table_entry(its, baser, dev_id);
3425 }
3426
its_alloc_vpe_table(u32 vpe_id)3427 static bool its_alloc_vpe_table(u32 vpe_id)
3428 {
3429 struct its_node *its;
3430 int cpu;
3431
3432 /*
3433 * Make sure the L2 tables are allocated on *all* v4 ITSs. We
3434 * could try and only do it on ITSs corresponding to devices
3435 * that have interrupts targeted at this VPE, but the
3436 * complexity becomes crazy (and you have tons of memory
3437 * anyway, right?).
3438 */
3439 list_for_each_entry(its, &its_nodes, entry) {
3440 struct its_baser *baser;
3441
3442 if (!is_v4(its))
3443 continue;
3444
3445 baser = its_get_baser(its, GITS_BASER_TYPE_VCPU);
3446 if (!baser)
3447 return false;
3448
3449 if (!its_alloc_table_entry(its, baser, vpe_id))
3450 return false;
3451 }
3452
3453 /* Non v4.1? No need to iterate RDs and go back early. */
3454 if (!gic_rdists->has_rvpeid)
3455 return true;
3456
3457 /*
3458 * Make sure the L2 tables are allocated for all copies of
3459 * the L1 table on *all* v4.1 RDs.
3460 */
3461 for_each_possible_cpu(cpu) {
3462 if (!allocate_vpe_l2_table(cpu, vpe_id))
3463 return false;
3464 }
3465
3466 return true;
3467 }
3468
its_create_device(struct its_node * its,u32 dev_id,int nvecs,bool alloc_lpis)3469 static struct its_device *its_create_device(struct its_node *its, u32 dev_id,
3470 int nvecs, bool alloc_lpis)
3471 {
3472 struct its_device *dev;
3473 unsigned long *lpi_map = NULL;
3474 unsigned long flags;
3475 u16 *col_map = NULL;
3476 void *itt;
3477 int lpi_base;
3478 int nr_lpis;
3479 int nr_ites;
3480 int sz;
3481
3482 if (!its_alloc_device_table(its, dev_id))
3483 return NULL;
3484
3485 if (WARN_ON(!is_power_of_2(nvecs)))
3486 nvecs = roundup_pow_of_two(nvecs);
3487
3488 /*
3489 * Even if the device wants a single LPI, the ITT must be
3490 * sized as a power of two (and you need at least one bit...).
3491 */
3492 nr_ites = max(2, nvecs);
3493 sz = nr_ites * (FIELD_GET(GITS_TYPER_ITT_ENTRY_SIZE, its->typer) + 1);
3494 sz = max(sz, ITS_ITT_ALIGN);
3495
3496 itt = itt_alloc_pool(its->numa_node, sz);
3497
3498 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
3499
3500 if (alloc_lpis) {
3501 lpi_map = its_lpi_alloc(nvecs, &lpi_base, &nr_lpis);
3502 if (lpi_map)
3503 col_map = kcalloc(nr_lpis, sizeof(*col_map),
3504 GFP_KERNEL);
3505 } else {
3506 col_map = kcalloc(nr_ites, sizeof(*col_map), GFP_KERNEL);
3507 nr_lpis = 0;
3508 lpi_base = 0;
3509 }
3510
3511 if (!dev || !itt || !col_map || (!lpi_map && alloc_lpis)) {
3512 kfree(dev);
3513 itt_free_pool(itt, sz);
3514 bitmap_free(lpi_map);
3515 kfree(col_map);
3516 return NULL;
3517 }
3518
3519 gic_flush_dcache_to_poc(itt, sz);
3520
3521 dev->its = its;
3522 dev->itt = itt;
3523 dev->itt_sz = sz;
3524 dev->nr_ites = nr_ites;
3525 dev->event_map.lpi_map = lpi_map;
3526 dev->event_map.col_map = col_map;
3527 dev->event_map.lpi_base = lpi_base;
3528 dev->event_map.nr_lpis = nr_lpis;
3529 raw_spin_lock_init(&dev->event_map.vlpi_lock);
3530 dev->device_id = dev_id;
3531 INIT_LIST_HEAD(&dev->entry);
3532
3533 raw_spin_lock_irqsave(&its->lock, flags);
3534 list_add(&dev->entry, &its->its_device_list);
3535 raw_spin_unlock_irqrestore(&its->lock, flags);
3536
3537 /* Map device to its ITT */
3538 its_send_mapd(dev, 1);
3539
3540 return dev;
3541 }
3542
its_free_device(struct its_device * its_dev)3543 static void its_free_device(struct its_device *its_dev)
3544 {
3545 unsigned long flags;
3546
3547 raw_spin_lock_irqsave(&its_dev->its->lock, flags);
3548 list_del(&its_dev->entry);
3549 raw_spin_unlock_irqrestore(&its_dev->its->lock, flags);
3550 kfree(its_dev->event_map.col_map);
3551 itt_free_pool(its_dev->itt, its_dev->itt_sz);
3552 kfree(its_dev);
3553 }
3554
its_alloc_device_irq(struct its_device * dev,int nvecs,irq_hw_number_t * hwirq)3555 static int its_alloc_device_irq(struct its_device *dev, int nvecs, irq_hw_number_t *hwirq)
3556 {
3557 int idx;
3558
3559 /* Find a free LPI region in lpi_map and allocate them. */
3560 idx = bitmap_find_free_region(dev->event_map.lpi_map,
3561 dev->event_map.nr_lpis,
3562 get_count_order(nvecs));
3563 if (idx < 0)
3564 return -ENOSPC;
3565
3566 *hwirq = dev->event_map.lpi_base + idx;
3567
3568 return 0;
3569 }
3570
its_msi_prepare(struct irq_domain * domain,struct device * dev,int nvec,msi_alloc_info_t * info)3571 static int its_msi_prepare(struct irq_domain *domain, struct device *dev,
3572 int nvec, msi_alloc_info_t *info)
3573 {
3574 struct its_node *its;
3575 struct its_device *its_dev;
3576 struct msi_domain_info *msi_info;
3577 u32 dev_id;
3578 int err = 0;
3579
3580 /*
3581 * We ignore "dev" entirely, and rely on the dev_id that has
3582 * been passed via the scratchpad. This limits this domain's
3583 * usefulness to upper layers that definitely know that they
3584 * are built on top of the ITS.
3585 */
3586 dev_id = info->scratchpad[0].ul;
3587
3588 msi_info = msi_get_domain_info(domain);
3589 its = msi_info->data;
3590
3591 if (!gic_rdists->has_direct_lpi &&
3592 vpe_proxy.dev &&
3593 vpe_proxy.dev->its == its &&
3594 dev_id == vpe_proxy.dev->device_id) {
3595 /* Bad luck. Get yourself a better implementation */
3596 WARN_ONCE(1, "DevId %x clashes with GICv4 VPE proxy device\n",
3597 dev_id);
3598 return -EINVAL;
3599 }
3600
3601 mutex_lock(&its->dev_alloc_lock);
3602 its_dev = its_find_device(its, dev_id);
3603 if (its_dev) {
3604 /*
3605 * We already have seen this ID, probably through
3606 * another alias (PCI bridge of some sort). No need to
3607 * create the device.
3608 */
3609 its_dev->shared = true;
3610 pr_debug("Reusing ITT for devID %x\n", dev_id);
3611 goto out;
3612 }
3613
3614 its_dev = its_create_device(its, dev_id, nvec, true);
3615 if (!its_dev) {
3616 err = -ENOMEM;
3617 goto out;
3618 }
3619
3620 if (info->flags & MSI_ALLOC_FLAGS_PROXY_DEVICE)
3621 its_dev->shared = true;
3622
3623 pr_debug("ITT %d entries, %d bits\n", nvec, ilog2(nvec));
3624 out:
3625 mutex_unlock(&its->dev_alloc_lock);
3626 info->scratchpad[0].ptr = its_dev;
3627 return err;
3628 }
3629
3630 static struct msi_domain_ops its_msi_domain_ops = {
3631 .msi_prepare = its_msi_prepare,
3632 };
3633
its_irq_gic_domain_alloc(struct irq_domain * domain,unsigned int virq,irq_hw_number_t hwirq)3634 static int its_irq_gic_domain_alloc(struct irq_domain *domain,
3635 unsigned int virq,
3636 irq_hw_number_t hwirq)
3637 {
3638 struct irq_fwspec fwspec;
3639
3640 if (irq_domain_get_of_node(domain->parent)) {
3641 fwspec.fwnode = domain->parent->fwnode;
3642 fwspec.param_count = 3;
3643 fwspec.param[0] = GIC_IRQ_TYPE_LPI;
3644 fwspec.param[1] = hwirq;
3645 fwspec.param[2] = IRQ_TYPE_EDGE_RISING;
3646 } else if (is_fwnode_irqchip(domain->parent->fwnode)) {
3647 fwspec.fwnode = domain->parent->fwnode;
3648 fwspec.param_count = 2;
3649 fwspec.param[0] = hwirq;
3650 fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
3651 } else {
3652 return -EINVAL;
3653 }
3654
3655 return irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
3656 }
3657
its_irq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * args)3658 static int its_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
3659 unsigned int nr_irqs, void *args)
3660 {
3661 msi_alloc_info_t *info = args;
3662 struct its_device *its_dev = info->scratchpad[0].ptr;
3663 struct its_node *its = its_dev->its;
3664 struct irq_data *irqd;
3665 irq_hw_number_t hwirq;
3666 int err;
3667 int i;
3668
3669 err = its_alloc_device_irq(its_dev, nr_irqs, &hwirq);
3670 if (err)
3671 return err;
3672
3673 err = iommu_dma_prepare_msi(info->desc, its->get_msi_base(its_dev));
3674 if (err)
3675 return err;
3676
3677 for (i = 0; i < nr_irqs; i++) {
3678 err = its_irq_gic_domain_alloc(domain, virq + i, hwirq + i);
3679 if (err)
3680 return err;
3681
3682 irq_domain_set_hwirq_and_chip(domain, virq + i,
3683 hwirq + i, &its_irq_chip, its_dev);
3684 irqd = irq_get_irq_data(virq + i);
3685 irqd_set_single_target(irqd);
3686 irqd_set_affinity_on_activate(irqd);
3687 irqd_set_resend_when_in_progress(irqd);
3688 pr_debug("ID:%d pID:%d vID:%d\n",
3689 (int)(hwirq + i - its_dev->event_map.lpi_base),
3690 (int)(hwirq + i), virq + i);
3691 }
3692
3693 return 0;
3694 }
3695
its_irq_domain_activate(struct irq_domain * domain,struct irq_data * d,bool reserve)3696 static int its_irq_domain_activate(struct irq_domain *domain,
3697 struct irq_data *d, bool reserve)
3698 {
3699 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3700 u32 event = its_get_event_id(d);
3701 int cpu;
3702
3703 cpu = its_select_cpu(d, cpu_online_mask);
3704 if (cpu < 0 || cpu >= nr_cpu_ids)
3705 return -EINVAL;
3706
3707 its_inc_lpi_count(d, cpu);
3708 its_dev->event_map.col_map[event] = cpu;
3709 irq_data_update_effective_affinity(d, cpumask_of(cpu));
3710
3711 /* Map the GIC IRQ and event to the device */
3712 its_send_mapti(its_dev, d->hwirq, event);
3713 return 0;
3714 }
3715
its_irq_domain_deactivate(struct irq_domain * domain,struct irq_data * d)3716 static void its_irq_domain_deactivate(struct irq_domain *domain,
3717 struct irq_data *d)
3718 {
3719 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3720 u32 event = its_get_event_id(d);
3721
3722 its_dec_lpi_count(d, its_dev->event_map.col_map[event]);
3723 /* Stop the delivery of interrupts */
3724 its_send_discard(its_dev, event);
3725 }
3726
its_irq_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)3727 static void its_irq_domain_free(struct irq_domain *domain, unsigned int virq,
3728 unsigned int nr_irqs)
3729 {
3730 struct irq_data *d = irq_domain_get_irq_data(domain, virq);
3731 struct its_device *its_dev = irq_data_get_irq_chip_data(d);
3732 struct its_node *its = its_dev->its;
3733 int i;
3734
3735 bitmap_release_region(its_dev->event_map.lpi_map,
3736 its_get_event_id(irq_domain_get_irq_data(domain, virq)),
3737 get_count_order(nr_irqs));
3738
3739 for (i = 0; i < nr_irqs; i++) {
3740 struct irq_data *data = irq_domain_get_irq_data(domain,
3741 virq + i);
3742 /* Nuke the entry in the domain */
3743 irq_domain_reset_irq_data(data);
3744 }
3745
3746 mutex_lock(&its->dev_alloc_lock);
3747
3748 /*
3749 * If all interrupts have been freed, start mopping the
3750 * floor. This is conditioned on the device not being shared.
3751 */
3752 if (!its_dev->shared &&
3753 bitmap_empty(its_dev->event_map.lpi_map,
3754 its_dev->event_map.nr_lpis)) {
3755 its_lpi_free(its_dev->event_map.lpi_map,
3756 its_dev->event_map.lpi_base,
3757 its_dev->event_map.nr_lpis);
3758
3759 /* Unmap device/itt */
3760 its_send_mapd(its_dev, 0);
3761 its_free_device(its_dev);
3762 }
3763
3764 mutex_unlock(&its->dev_alloc_lock);
3765
3766 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
3767 }
3768
3769 static const struct irq_domain_ops its_domain_ops = {
3770 .select = msi_lib_irq_domain_select,
3771 .alloc = its_irq_domain_alloc,
3772 .free = its_irq_domain_free,
3773 .activate = its_irq_domain_activate,
3774 .deactivate = its_irq_domain_deactivate,
3775 };
3776
3777 /*
3778 * This is insane.
3779 *
3780 * If a GICv4.0 doesn't implement Direct LPIs (which is extremely
3781 * likely), the only way to perform an invalidate is to use a fake
3782 * device to issue an INV command, implying that the LPI has first
3783 * been mapped to some event on that device. Since this is not exactly
3784 * cheap, we try to keep that mapping around as long as possible, and
3785 * only issue an UNMAP if we're short on available slots.
3786 *
3787 * Broken by design(tm).
3788 *
3789 * GICv4.1, on the other hand, mandates that we're able to invalidate
3790 * by writing to a MMIO register. It doesn't implement the whole of
3791 * DirectLPI, but that's good enough. And most of the time, we don't
3792 * even have to invalidate anything, as the redistributor can be told
3793 * whether to generate a doorbell or not (we thus leave it enabled,
3794 * always).
3795 */
its_vpe_db_proxy_unmap_locked(struct its_vpe * vpe)3796 static void its_vpe_db_proxy_unmap_locked(struct its_vpe *vpe)
3797 {
3798 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3799 if (gic_rdists->has_rvpeid)
3800 return;
3801
3802 /* Already unmapped? */
3803 if (vpe->vpe_proxy_event == -1)
3804 return;
3805
3806 its_send_discard(vpe_proxy.dev, vpe->vpe_proxy_event);
3807 vpe_proxy.vpes[vpe->vpe_proxy_event] = NULL;
3808
3809 /*
3810 * We don't track empty slots at all, so let's move the
3811 * next_victim pointer if we can quickly reuse that slot
3812 * instead of nuking an existing entry. Not clear that this is
3813 * always a win though, and this might just generate a ripple
3814 * effect... Let's just hope VPEs don't migrate too often.
3815 */
3816 if (vpe_proxy.vpes[vpe_proxy.next_victim])
3817 vpe_proxy.next_victim = vpe->vpe_proxy_event;
3818
3819 vpe->vpe_proxy_event = -1;
3820 }
3821
its_vpe_db_proxy_unmap(struct its_vpe * vpe)3822 static void its_vpe_db_proxy_unmap(struct its_vpe *vpe)
3823 {
3824 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3825 if (gic_rdists->has_rvpeid)
3826 return;
3827
3828 if (!gic_rdists->has_direct_lpi) {
3829 unsigned long flags;
3830
3831 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3832 its_vpe_db_proxy_unmap_locked(vpe);
3833 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3834 }
3835 }
3836
its_vpe_db_proxy_map_locked(struct its_vpe * vpe)3837 static void its_vpe_db_proxy_map_locked(struct its_vpe *vpe)
3838 {
3839 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3840 if (gic_rdists->has_rvpeid)
3841 return;
3842
3843 /* Already mapped? */
3844 if (vpe->vpe_proxy_event != -1)
3845 return;
3846
3847 /* This slot was already allocated. Kick the other VPE out. */
3848 if (vpe_proxy.vpes[vpe_proxy.next_victim])
3849 its_vpe_db_proxy_unmap_locked(vpe_proxy.vpes[vpe_proxy.next_victim]);
3850
3851 /* Map the new VPE instead */
3852 vpe_proxy.vpes[vpe_proxy.next_victim] = vpe;
3853 vpe->vpe_proxy_event = vpe_proxy.next_victim;
3854 vpe_proxy.next_victim = (vpe_proxy.next_victim + 1) % vpe_proxy.dev->nr_ites;
3855
3856 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = vpe->col_idx;
3857 its_send_mapti(vpe_proxy.dev, vpe->vpe_db_lpi, vpe->vpe_proxy_event);
3858 }
3859
its_vpe_db_proxy_move(struct its_vpe * vpe,int from,int to)3860 static void its_vpe_db_proxy_move(struct its_vpe *vpe, int from, int to)
3861 {
3862 unsigned long flags;
3863 struct its_collection *target_col;
3864
3865 /* GICv4.1 doesn't use a proxy, so nothing to do here */
3866 if (gic_rdists->has_rvpeid)
3867 return;
3868
3869 if (gic_rdists->has_direct_lpi) {
3870 void __iomem *rdbase;
3871
3872 rdbase = per_cpu_ptr(gic_rdists->rdist, from)->rd_base;
3873 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
3874 wait_for_syncr(rdbase);
3875
3876 return;
3877 }
3878
3879 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
3880
3881 its_vpe_db_proxy_map_locked(vpe);
3882
3883 target_col = &vpe_proxy.dev->its->collections[to];
3884 its_send_movi(vpe_proxy.dev, target_col, vpe->vpe_proxy_event);
3885 vpe_proxy.dev->event_map.col_map[vpe->vpe_proxy_event] = to;
3886
3887 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
3888 }
3889
its_vpe_4_1_invall_locked(int cpu,struct its_vpe * vpe)3890 static void its_vpe_4_1_invall_locked(int cpu, struct its_vpe *vpe)
3891 {
3892 void __iomem *rdbase;
3893 u64 val;
3894
3895 val = GICR_INVALLR_V;
3896 val |= FIELD_PREP(GICR_INVALLR_VPEID, vpe->vpe_id);
3897
3898 guard(raw_spinlock)(&gic_data_rdist_cpu(cpu)->rd_lock);
3899 rdbase = per_cpu_ptr(gic_rdists->rdist, cpu)->rd_base;
3900 gic_write_lpir(val, rdbase + GICR_INVALLR);
3901 wait_for_syncr(rdbase);
3902 }
3903
its_vpe_set_affinity(struct irq_data * d,const struct cpumask * mask_val,bool force)3904 static int its_vpe_set_affinity(struct irq_data *d,
3905 const struct cpumask *mask_val,
3906 bool force)
3907 {
3908 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
3909 unsigned int from, cpu = nr_cpu_ids;
3910 struct cpumask *table_mask;
3911 struct its_node *its;
3912 unsigned long flags;
3913
3914 /*
3915 * Check if we're racing against a VPE being destroyed, for
3916 * which we don't want to allow a VMOVP.
3917 */
3918 if (!atomic_read(&vpe->vmapp_count)) {
3919 if (gic_requires_eager_mapping())
3920 return -EINVAL;
3921
3922 /*
3923 * If we lazily map the VPEs, this isn't an error and
3924 * we can exit cleanly.
3925 */
3926 cpu = cpumask_first(mask_val);
3927 irq_data_update_effective_affinity(d, cpumask_of(cpu));
3928 return IRQ_SET_MASK_OK_DONE;
3929 }
3930
3931 /*
3932 * Changing affinity is mega expensive, so let's be as lazy as
3933 * we can and only do it if we really have to. Also, if mapped
3934 * into the proxy device, we need to move the doorbell
3935 * interrupt to its new location.
3936 *
3937 * Another thing is that changing the affinity of a vPE affects
3938 * *other interrupts* such as all the vLPIs that are routed to
3939 * this vPE. This means that the irq_desc lock is not enough to
3940 * protect us, and that we must ensure nobody samples vpe->col_idx
3941 * during the update, hence the lock below which must also be
3942 * taken on any vLPI handling path that evaluates vpe->col_idx.
3943 *
3944 * Finally, we must protect ourselves against concurrent updates of
3945 * the mapping state on this VM should the ITS list be in use (see
3946 * the shortcut in its_send_vmovp() otherewise).
3947 */
3948 if (its_list_map)
3949 raw_spin_lock(&vpe->its_vm->vmapp_lock);
3950
3951 from = vpe_to_cpuid_lock(vpe, &flags);
3952 table_mask = gic_data_rdist_cpu(from)->vpe_table_mask;
3953
3954 /*
3955 * If we are offered another CPU in the same GICv4.1 ITS
3956 * affinity, pick this one. Otherwise, any CPU will do.
3957 */
3958 if (table_mask)
3959 cpu = cpumask_any_and(mask_val, table_mask);
3960 if (cpu < nr_cpu_ids) {
3961 if (cpumask_test_cpu(from, mask_val) &&
3962 cpumask_test_cpu(from, table_mask))
3963 cpu = from;
3964 } else {
3965 cpu = cpumask_first(mask_val);
3966 }
3967
3968 if (from == cpu)
3969 goto out;
3970
3971 vpe->col_idx = cpu;
3972
3973 its_send_vmovp(vpe);
3974
3975 its = find_4_1_its();
3976 if (its && its->flags & ITS_FLAGS_WORKAROUND_HISILICON_162100801)
3977 its_vpe_4_1_invall_locked(cpu, vpe);
3978
3979 its_vpe_db_proxy_move(vpe, from, cpu);
3980
3981 out:
3982 irq_data_update_effective_affinity(d, cpumask_of(cpu));
3983 vpe_to_cpuid_unlock(vpe, flags);
3984
3985 if (its_list_map)
3986 raw_spin_unlock(&vpe->its_vm->vmapp_lock);
3987
3988 return IRQ_SET_MASK_OK_DONE;
3989 }
3990
its_wait_vpt_parse_complete(void)3991 static void its_wait_vpt_parse_complete(void)
3992 {
3993 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
3994 u64 val;
3995
3996 if (!gic_rdists->has_vpend_valid_dirty)
3997 return;
3998
3999 WARN_ON_ONCE(readq_relaxed_poll_timeout_atomic(vlpi_base + GICR_VPENDBASER,
4000 val,
4001 !(val & GICR_VPENDBASER_Dirty),
4002 1, 500));
4003 }
4004
its_vpe_schedule(struct its_vpe * vpe)4005 static void its_vpe_schedule(struct its_vpe *vpe)
4006 {
4007 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
4008 u64 val;
4009
4010 /* Schedule the VPE */
4011 val = virt_to_phys(page_address(vpe->its_vm->vprop_page)) &
4012 GENMASK_ULL(51, 12);
4013 val |= (LPI_NRBITS - 1) & GICR_VPROPBASER_IDBITS_MASK;
4014 if (rdists_support_shareable()) {
4015 val |= GICR_VPROPBASER_RaWb;
4016 val |= GICR_VPROPBASER_InnerShareable;
4017 }
4018 gicr_write_vpropbaser(val, vlpi_base + GICR_VPROPBASER);
4019
4020 val = virt_to_phys(page_address(vpe->vpt_page)) &
4021 GENMASK_ULL(51, 16);
4022 if (rdists_support_shareable()) {
4023 val |= GICR_VPENDBASER_RaWaWb;
4024 val |= GICR_VPENDBASER_InnerShareable;
4025 }
4026 /*
4027 * There is no good way of finding out if the pending table is
4028 * empty as we can race against the doorbell interrupt very
4029 * easily. So in the end, vpe->pending_last is only an
4030 * indication that the vcpu has something pending, not one
4031 * that the pending table is empty. A good implementation
4032 * would be able to read its coarse map pretty quickly anyway,
4033 * making this a tolerable issue.
4034 */
4035 val |= GICR_VPENDBASER_PendingLast;
4036 val |= vpe->idai ? GICR_VPENDBASER_IDAI : 0;
4037 val |= GICR_VPENDBASER_Valid;
4038 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
4039 }
4040
its_vpe_deschedule(struct its_vpe * vpe)4041 static void its_vpe_deschedule(struct its_vpe *vpe)
4042 {
4043 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
4044 u64 val;
4045
4046 val = its_clear_vpend_valid(vlpi_base, 0, 0);
4047
4048 vpe->idai = !!(val & GICR_VPENDBASER_IDAI);
4049 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
4050 }
4051
its_vpe_invall(struct its_vpe * vpe)4052 static void its_vpe_invall(struct its_vpe *vpe)
4053 {
4054 struct its_node *its;
4055
4056 guard(raw_spinlock_irqsave)(&vpe->its_vm->vmapp_lock);
4057
4058 list_for_each_entry(its, &its_nodes, entry) {
4059 if (!is_v4(its))
4060 continue;
4061
4062 if (its_list_map && !vpe->its_vm->vlpi_count[its->list_nr])
4063 continue;
4064
4065 /*
4066 * Sending a VINVALL to a single ITS is enough, as all
4067 * we need is to reach the redistributors.
4068 */
4069 its_send_vinvall(its, vpe);
4070 return;
4071 }
4072 }
4073
its_vpe_set_vcpu_affinity(struct irq_data * d,void * vcpu_info)4074 static int its_vpe_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
4075 {
4076 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4077 struct its_cmd_info *info = vcpu_info;
4078
4079 switch (info->cmd_type) {
4080 case SCHEDULE_VPE:
4081 its_vpe_schedule(vpe);
4082 return 0;
4083
4084 case DESCHEDULE_VPE:
4085 its_vpe_deschedule(vpe);
4086 return 0;
4087
4088 case COMMIT_VPE:
4089 its_wait_vpt_parse_complete();
4090 return 0;
4091
4092 case INVALL_VPE:
4093 its_vpe_invall(vpe);
4094 return 0;
4095
4096 default:
4097 return -EINVAL;
4098 }
4099 }
4100
its_vpe_send_cmd(struct its_vpe * vpe,void (* cmd)(struct its_device *,u32))4101 static void its_vpe_send_cmd(struct its_vpe *vpe,
4102 void (*cmd)(struct its_device *, u32))
4103 {
4104 unsigned long flags;
4105
4106 raw_spin_lock_irqsave(&vpe_proxy.lock, flags);
4107
4108 its_vpe_db_proxy_map_locked(vpe);
4109 cmd(vpe_proxy.dev, vpe->vpe_proxy_event);
4110
4111 raw_spin_unlock_irqrestore(&vpe_proxy.lock, flags);
4112 }
4113
its_vpe_send_inv(struct irq_data * d)4114 static void its_vpe_send_inv(struct irq_data *d)
4115 {
4116 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4117
4118 if (gic_rdists->has_direct_lpi)
4119 __direct_lpi_inv(d, d->parent_data->hwirq);
4120 else
4121 its_vpe_send_cmd(vpe, its_send_inv);
4122 }
4123
its_vpe_mask_irq(struct irq_data * d)4124 static void its_vpe_mask_irq(struct irq_data *d)
4125 {
4126 /*
4127 * We need to unmask the LPI, which is described by the parent
4128 * irq_data. Instead of calling into the parent (which won't
4129 * exactly do the right thing, let's simply use the
4130 * parent_data pointer. Yes, I'm naughty.
4131 */
4132 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
4133 its_vpe_send_inv(d);
4134 }
4135
its_vpe_unmask_irq(struct irq_data * d)4136 static void its_vpe_unmask_irq(struct irq_data *d)
4137 {
4138 /* Same hack as above... */
4139 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
4140 its_vpe_send_inv(d);
4141 }
4142
its_vpe_set_irqchip_state(struct irq_data * d,enum irqchip_irq_state which,bool state)4143 static int its_vpe_set_irqchip_state(struct irq_data *d,
4144 enum irqchip_irq_state which,
4145 bool state)
4146 {
4147 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4148
4149 if (which != IRQCHIP_STATE_PENDING)
4150 return -EINVAL;
4151
4152 if (gic_rdists->has_direct_lpi) {
4153 void __iomem *rdbase;
4154
4155 rdbase = per_cpu_ptr(gic_rdists->rdist, vpe->col_idx)->rd_base;
4156 if (state) {
4157 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_SETLPIR);
4158 } else {
4159 gic_write_lpir(vpe->vpe_db_lpi, rdbase + GICR_CLRLPIR);
4160 wait_for_syncr(rdbase);
4161 }
4162 } else {
4163 if (state)
4164 its_vpe_send_cmd(vpe, its_send_int);
4165 else
4166 its_vpe_send_cmd(vpe, its_send_clear);
4167 }
4168
4169 return 0;
4170 }
4171
its_vpe_retrigger(struct irq_data * d)4172 static int its_vpe_retrigger(struct irq_data *d)
4173 {
4174 return !its_vpe_set_irqchip_state(d, IRQCHIP_STATE_PENDING, true);
4175 }
4176
4177 static struct irq_chip its_vpe_irq_chip = {
4178 .name = "GICv4-vpe",
4179 .irq_mask = its_vpe_mask_irq,
4180 .irq_unmask = its_vpe_unmask_irq,
4181 .irq_eoi = irq_chip_eoi_parent,
4182 .irq_set_affinity = its_vpe_set_affinity,
4183 .irq_retrigger = its_vpe_retrigger,
4184 .irq_set_irqchip_state = its_vpe_set_irqchip_state,
4185 .irq_set_vcpu_affinity = its_vpe_set_vcpu_affinity,
4186 };
4187
find_4_1_its(void)4188 static struct its_node *find_4_1_its(void)
4189 {
4190 static struct its_node *its = NULL;
4191
4192 if (!its) {
4193 list_for_each_entry(its, &its_nodes, entry) {
4194 if (is_v4_1(its))
4195 return its;
4196 }
4197
4198 /* Oops? */
4199 its = NULL;
4200 }
4201
4202 return its;
4203 }
4204
its_vpe_4_1_send_inv(struct irq_data * d)4205 static void its_vpe_4_1_send_inv(struct irq_data *d)
4206 {
4207 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4208 struct its_node *its;
4209
4210 /*
4211 * GICv4.1 wants doorbells to be invalidated using the
4212 * INVDB command in order to be broadcast to all RDs. Send
4213 * it to the first valid ITS, and let the HW do its magic.
4214 */
4215 its = find_4_1_its();
4216 if (its)
4217 its_send_invdb(its, vpe);
4218 }
4219
its_vpe_4_1_mask_irq(struct irq_data * d)4220 static void its_vpe_4_1_mask_irq(struct irq_data *d)
4221 {
4222 lpi_write_config(d->parent_data, LPI_PROP_ENABLED, 0);
4223 its_vpe_4_1_send_inv(d);
4224 }
4225
its_vpe_4_1_unmask_irq(struct irq_data * d)4226 static void its_vpe_4_1_unmask_irq(struct irq_data *d)
4227 {
4228 lpi_write_config(d->parent_data, 0, LPI_PROP_ENABLED);
4229 its_vpe_4_1_send_inv(d);
4230 }
4231
its_vpe_4_1_schedule(struct its_vpe * vpe,struct its_cmd_info * info)4232 static void its_vpe_4_1_schedule(struct its_vpe *vpe,
4233 struct its_cmd_info *info)
4234 {
4235 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
4236 u64 val = 0;
4237
4238 /* Schedule the VPE */
4239 val |= GICR_VPENDBASER_Valid;
4240 val |= info->g0en ? GICR_VPENDBASER_4_1_VGRP0EN : 0;
4241 val |= info->g1en ? GICR_VPENDBASER_4_1_VGRP1EN : 0;
4242 val |= FIELD_PREP(GICR_VPENDBASER_4_1_VPEID, vpe->vpe_id);
4243
4244 gicr_write_vpendbaser(val, vlpi_base + GICR_VPENDBASER);
4245 }
4246
its_vpe_4_1_deschedule(struct its_vpe * vpe,struct its_cmd_info * info)4247 static void its_vpe_4_1_deschedule(struct its_vpe *vpe,
4248 struct its_cmd_info *info)
4249 {
4250 void __iomem *vlpi_base = gic_data_rdist_vlpi_base();
4251 u64 val;
4252
4253 if (info->req_db) {
4254 unsigned long flags;
4255
4256 /*
4257 * vPE is going to block: make the vPE non-resident with
4258 * PendingLast clear and DB set. The GIC guarantees that if
4259 * we read-back PendingLast clear, then a doorbell will be
4260 * delivered when an interrupt comes.
4261 *
4262 * Note the locking to deal with the concurrent update of
4263 * pending_last from the doorbell interrupt handler that can
4264 * run concurrently.
4265 */
4266 raw_spin_lock_irqsave(&vpe->vpe_lock, flags);
4267 val = its_clear_vpend_valid(vlpi_base,
4268 GICR_VPENDBASER_PendingLast,
4269 GICR_VPENDBASER_4_1_DB);
4270 vpe->pending_last = !!(val & GICR_VPENDBASER_PendingLast);
4271 raw_spin_unlock_irqrestore(&vpe->vpe_lock, flags);
4272 } else {
4273 /*
4274 * We're not blocking, so just make the vPE non-resident
4275 * with PendingLast set, indicating that we'll be back.
4276 */
4277 val = its_clear_vpend_valid(vlpi_base,
4278 0,
4279 GICR_VPENDBASER_PendingLast);
4280 vpe->pending_last = true;
4281 }
4282 }
4283
its_vpe_4_1_invall(struct its_vpe * vpe)4284 static void its_vpe_4_1_invall(struct its_vpe *vpe)
4285 {
4286 unsigned long flags;
4287 int cpu;
4288
4289 /* Target the redistributor this vPE is currently known on */
4290 cpu = vpe_to_cpuid_lock(vpe, &flags);
4291 its_vpe_4_1_invall_locked(cpu, vpe);
4292 vpe_to_cpuid_unlock(vpe, flags);
4293 }
4294
its_vpe_4_1_set_vcpu_affinity(struct irq_data * d,void * vcpu_info)4295 static int its_vpe_4_1_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
4296 {
4297 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4298 struct its_cmd_info *info = vcpu_info;
4299
4300 switch (info->cmd_type) {
4301 case SCHEDULE_VPE:
4302 its_vpe_4_1_schedule(vpe, info);
4303 return 0;
4304
4305 case DESCHEDULE_VPE:
4306 its_vpe_4_1_deschedule(vpe, info);
4307 return 0;
4308
4309 case COMMIT_VPE:
4310 its_wait_vpt_parse_complete();
4311 return 0;
4312
4313 case INVALL_VPE:
4314 its_vpe_4_1_invall(vpe);
4315 return 0;
4316
4317 default:
4318 return -EINVAL;
4319 }
4320 }
4321
4322 static struct irq_chip its_vpe_4_1_irq_chip = {
4323 .name = "GICv4.1-vpe",
4324 .irq_mask = its_vpe_4_1_mask_irq,
4325 .irq_unmask = its_vpe_4_1_unmask_irq,
4326 .irq_eoi = irq_chip_eoi_parent,
4327 .irq_set_affinity = its_vpe_set_affinity,
4328 .irq_set_vcpu_affinity = its_vpe_4_1_set_vcpu_affinity,
4329 };
4330
its_configure_sgi(struct irq_data * d,bool clear)4331 static void its_configure_sgi(struct irq_data *d, bool clear)
4332 {
4333 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4334 struct its_cmd_desc desc;
4335
4336 desc.its_vsgi_cmd.vpe = vpe;
4337 desc.its_vsgi_cmd.sgi = d->hwirq;
4338 desc.its_vsgi_cmd.priority = vpe->sgi_config[d->hwirq].priority;
4339 desc.its_vsgi_cmd.enable = vpe->sgi_config[d->hwirq].enabled;
4340 desc.its_vsgi_cmd.group = vpe->sgi_config[d->hwirq].group;
4341 desc.its_vsgi_cmd.clear = clear;
4342
4343 /*
4344 * GICv4.1 allows us to send VSGI commands to any ITS as long as the
4345 * destination VPE is mapped there. Since we map them eagerly at
4346 * activation time, we're pretty sure the first GICv4.1 ITS will do.
4347 */
4348 its_send_single_vcommand(find_4_1_its(), its_build_vsgi_cmd, &desc);
4349 }
4350
its_sgi_mask_irq(struct irq_data * d)4351 static void its_sgi_mask_irq(struct irq_data *d)
4352 {
4353 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4354
4355 vpe->sgi_config[d->hwirq].enabled = false;
4356 its_configure_sgi(d, false);
4357 }
4358
its_sgi_unmask_irq(struct irq_data * d)4359 static void its_sgi_unmask_irq(struct irq_data *d)
4360 {
4361 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4362
4363 vpe->sgi_config[d->hwirq].enabled = true;
4364 its_configure_sgi(d, false);
4365 }
4366
its_sgi_set_affinity(struct irq_data * d,const struct cpumask * mask_val,bool force)4367 static int its_sgi_set_affinity(struct irq_data *d,
4368 const struct cpumask *mask_val,
4369 bool force)
4370 {
4371 /*
4372 * There is no notion of affinity for virtual SGIs, at least
4373 * not on the host (since they can only be targeting a vPE).
4374 * Tell the kernel we've done whatever it asked for.
4375 */
4376 irq_data_update_effective_affinity(d, mask_val);
4377 return IRQ_SET_MASK_OK;
4378 }
4379
its_sgi_set_irqchip_state(struct irq_data * d,enum irqchip_irq_state which,bool state)4380 static int its_sgi_set_irqchip_state(struct irq_data *d,
4381 enum irqchip_irq_state which,
4382 bool state)
4383 {
4384 if (which != IRQCHIP_STATE_PENDING)
4385 return -EINVAL;
4386
4387 if (state) {
4388 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4389 struct its_node *its = find_4_1_its();
4390 u64 val;
4391
4392 val = FIELD_PREP(GITS_SGIR_VPEID, vpe->vpe_id);
4393 val |= FIELD_PREP(GITS_SGIR_VINTID, d->hwirq);
4394 writeq_relaxed(val, its->sgir_base + GITS_SGIR - SZ_128K);
4395 } else {
4396 its_configure_sgi(d, true);
4397 }
4398
4399 return 0;
4400 }
4401
its_sgi_get_irqchip_state(struct irq_data * d,enum irqchip_irq_state which,bool * val)4402 static int its_sgi_get_irqchip_state(struct irq_data *d,
4403 enum irqchip_irq_state which, bool *val)
4404 {
4405 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4406 void __iomem *base;
4407 unsigned long flags;
4408 u32 count = 1000000; /* 1s! */
4409 u32 status;
4410 int cpu;
4411
4412 if (which != IRQCHIP_STATE_PENDING)
4413 return -EINVAL;
4414
4415 /*
4416 * Locking galore! We can race against two different events:
4417 *
4418 * - Concurrent vPE affinity change: we must make sure it cannot
4419 * happen, or we'll talk to the wrong redistributor. This is
4420 * identical to what happens with vLPIs.
4421 *
4422 * - Concurrent VSGIPENDR access: As it involves accessing two
4423 * MMIO registers, this must be made atomic one way or another.
4424 */
4425 cpu = vpe_to_cpuid_lock(vpe, &flags);
4426 raw_spin_lock(&gic_data_rdist_cpu(cpu)->rd_lock);
4427 base = gic_data_rdist_cpu(cpu)->rd_base + SZ_128K;
4428 writel_relaxed(vpe->vpe_id, base + GICR_VSGIR);
4429 do {
4430 status = readl_relaxed(base + GICR_VSGIPENDR);
4431 if (!(status & GICR_VSGIPENDR_BUSY))
4432 goto out;
4433
4434 count--;
4435 if (!count) {
4436 pr_err_ratelimited("Unable to get SGI status\n");
4437 goto out;
4438 }
4439 cpu_relax();
4440 udelay(1);
4441 } while (count);
4442
4443 out:
4444 raw_spin_unlock(&gic_data_rdist_cpu(cpu)->rd_lock);
4445 vpe_to_cpuid_unlock(vpe, flags);
4446
4447 if (!count)
4448 return -ENXIO;
4449
4450 *val = !!(status & (1 << d->hwirq));
4451
4452 return 0;
4453 }
4454
its_sgi_set_vcpu_affinity(struct irq_data * d,void * vcpu_info)4455 static int its_sgi_set_vcpu_affinity(struct irq_data *d, void *vcpu_info)
4456 {
4457 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4458 struct its_cmd_info *info = vcpu_info;
4459
4460 switch (info->cmd_type) {
4461 case PROP_UPDATE_VSGI:
4462 vpe->sgi_config[d->hwirq].priority = info->priority;
4463 vpe->sgi_config[d->hwirq].group = info->group;
4464 its_configure_sgi(d, false);
4465 return 0;
4466
4467 default:
4468 return -EINVAL;
4469 }
4470 }
4471
4472 static struct irq_chip its_sgi_irq_chip = {
4473 .name = "GICv4.1-sgi",
4474 .irq_mask = its_sgi_mask_irq,
4475 .irq_unmask = its_sgi_unmask_irq,
4476 .irq_set_affinity = its_sgi_set_affinity,
4477 .irq_set_irqchip_state = its_sgi_set_irqchip_state,
4478 .irq_get_irqchip_state = its_sgi_get_irqchip_state,
4479 .irq_set_vcpu_affinity = its_sgi_set_vcpu_affinity,
4480 };
4481
its_sgi_irq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * args)4482 static int its_sgi_irq_domain_alloc(struct irq_domain *domain,
4483 unsigned int virq, unsigned int nr_irqs,
4484 void *args)
4485 {
4486 struct its_vpe *vpe = args;
4487 int i;
4488
4489 /* Yes, we do want 16 SGIs */
4490 WARN_ON(nr_irqs != 16);
4491
4492 for (i = 0; i < 16; i++) {
4493 vpe->sgi_config[i].priority = 0;
4494 vpe->sgi_config[i].enabled = false;
4495 vpe->sgi_config[i].group = false;
4496
4497 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
4498 &its_sgi_irq_chip, vpe);
4499 irq_set_status_flags(virq + i, IRQ_DISABLE_UNLAZY);
4500 }
4501
4502 return 0;
4503 }
4504
its_sgi_irq_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)4505 static void its_sgi_irq_domain_free(struct irq_domain *domain,
4506 unsigned int virq,
4507 unsigned int nr_irqs)
4508 {
4509 /* Nothing to do */
4510 }
4511
its_sgi_irq_domain_activate(struct irq_domain * domain,struct irq_data * d,bool reserve)4512 static int its_sgi_irq_domain_activate(struct irq_domain *domain,
4513 struct irq_data *d, bool reserve)
4514 {
4515 /* Write out the initial SGI configuration */
4516 its_configure_sgi(d, false);
4517 return 0;
4518 }
4519
its_sgi_irq_domain_deactivate(struct irq_domain * domain,struct irq_data * d)4520 static void its_sgi_irq_domain_deactivate(struct irq_domain *domain,
4521 struct irq_data *d)
4522 {
4523 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4524
4525 /*
4526 * The VSGI command is awkward:
4527 *
4528 * - To change the configuration, CLEAR must be set to false,
4529 * leaving the pending bit unchanged.
4530 * - To clear the pending bit, CLEAR must be set to true, leaving
4531 * the configuration unchanged.
4532 *
4533 * You just can't do both at once, hence the two commands below.
4534 */
4535 vpe->sgi_config[d->hwirq].enabled = false;
4536 its_configure_sgi(d, false);
4537 its_configure_sgi(d, true);
4538 }
4539
4540 static const struct irq_domain_ops its_sgi_domain_ops = {
4541 .alloc = its_sgi_irq_domain_alloc,
4542 .free = its_sgi_irq_domain_free,
4543 .activate = its_sgi_irq_domain_activate,
4544 .deactivate = its_sgi_irq_domain_deactivate,
4545 };
4546
its_vpe_id_alloc(void)4547 static int its_vpe_id_alloc(void)
4548 {
4549 return ida_alloc_max(&its_vpeid_ida, ITS_MAX_VPEID - 1, GFP_KERNEL);
4550 }
4551
its_vpe_id_free(u16 id)4552 static void its_vpe_id_free(u16 id)
4553 {
4554 ida_free(&its_vpeid_ida, id);
4555 }
4556
its_vpe_init(struct its_vpe * vpe)4557 static int its_vpe_init(struct its_vpe *vpe)
4558 {
4559 struct page *vpt_page;
4560 int vpe_id;
4561
4562 /* Allocate vpe_id */
4563 vpe_id = its_vpe_id_alloc();
4564 if (vpe_id < 0)
4565 return vpe_id;
4566
4567 /* Allocate VPT */
4568 vpt_page = its_allocate_pending_table(GFP_KERNEL);
4569 if (!vpt_page) {
4570 its_vpe_id_free(vpe_id);
4571 return -ENOMEM;
4572 }
4573
4574 if (!its_alloc_vpe_table(vpe_id)) {
4575 its_vpe_id_free(vpe_id);
4576 its_free_pending_table(vpt_page);
4577 return -ENOMEM;
4578 }
4579
4580 raw_spin_lock_init(&vpe->vpe_lock);
4581 vpe->vpe_id = vpe_id;
4582 vpe->vpt_page = vpt_page;
4583 atomic_set(&vpe->vmapp_count, 0);
4584 if (!gic_rdists->has_rvpeid)
4585 vpe->vpe_proxy_event = -1;
4586
4587 return 0;
4588 }
4589
its_vpe_teardown(struct its_vpe * vpe)4590 static void its_vpe_teardown(struct its_vpe *vpe)
4591 {
4592 its_vpe_db_proxy_unmap(vpe);
4593 its_vpe_id_free(vpe->vpe_id);
4594 its_free_pending_table(vpe->vpt_page);
4595 }
4596
its_vpe_irq_domain_free(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs)4597 static void its_vpe_irq_domain_free(struct irq_domain *domain,
4598 unsigned int virq,
4599 unsigned int nr_irqs)
4600 {
4601 struct its_vm *vm = domain->host_data;
4602 int i;
4603
4604 irq_domain_free_irqs_parent(domain, virq, nr_irqs);
4605
4606 for (i = 0; i < nr_irqs; i++) {
4607 struct irq_data *data = irq_domain_get_irq_data(domain,
4608 virq + i);
4609 struct its_vpe *vpe = irq_data_get_irq_chip_data(data);
4610
4611 BUG_ON(vm != vpe->its_vm);
4612
4613 clear_bit(data->hwirq, vm->db_bitmap);
4614 its_vpe_teardown(vpe);
4615 irq_domain_reset_irq_data(data);
4616 }
4617
4618 if (bitmap_empty(vm->db_bitmap, vm->nr_db_lpis)) {
4619 its_lpi_free(vm->db_bitmap, vm->db_lpi_base, vm->nr_db_lpis);
4620 its_free_prop_table(vm->vprop_page);
4621 }
4622 }
4623
its_vpe_irq_domain_alloc(struct irq_domain * domain,unsigned int virq,unsigned int nr_irqs,void * args)4624 static int its_vpe_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
4625 unsigned int nr_irqs, void *args)
4626 {
4627 struct irq_chip *irqchip = &its_vpe_irq_chip;
4628 struct its_vm *vm = args;
4629 unsigned long *bitmap;
4630 struct page *vprop_page;
4631 int base, nr_ids, i, err = 0;
4632
4633 bitmap = its_lpi_alloc(roundup_pow_of_two(nr_irqs), &base, &nr_ids);
4634 if (!bitmap)
4635 return -ENOMEM;
4636
4637 if (nr_ids < nr_irqs) {
4638 its_lpi_free(bitmap, base, nr_ids);
4639 return -ENOMEM;
4640 }
4641
4642 vprop_page = its_allocate_prop_table(GFP_KERNEL);
4643 if (!vprop_page) {
4644 its_lpi_free(bitmap, base, nr_ids);
4645 return -ENOMEM;
4646 }
4647
4648 vm->db_bitmap = bitmap;
4649 vm->db_lpi_base = base;
4650 vm->nr_db_lpis = nr_ids;
4651 vm->vprop_page = vprop_page;
4652 raw_spin_lock_init(&vm->vmapp_lock);
4653
4654 if (gic_rdists->has_rvpeid)
4655 irqchip = &its_vpe_4_1_irq_chip;
4656
4657 for (i = 0; i < nr_irqs; i++) {
4658 vm->vpes[i]->vpe_db_lpi = base + i;
4659 err = its_vpe_init(vm->vpes[i]);
4660 if (err)
4661 break;
4662 err = its_irq_gic_domain_alloc(domain, virq + i,
4663 vm->vpes[i]->vpe_db_lpi);
4664 if (err)
4665 break;
4666 irq_domain_set_hwirq_and_chip(domain, virq + i, i,
4667 irqchip, vm->vpes[i]);
4668 set_bit(i, bitmap);
4669 irqd_set_resend_when_in_progress(irq_get_irq_data(virq + i));
4670 }
4671
4672 if (err)
4673 its_vpe_irq_domain_free(domain, virq, i);
4674
4675 return err;
4676 }
4677
its_vpe_irq_domain_activate(struct irq_domain * domain,struct irq_data * d,bool reserve)4678 static int its_vpe_irq_domain_activate(struct irq_domain *domain,
4679 struct irq_data *d, bool reserve)
4680 {
4681 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4682 struct its_node *its;
4683
4684 /* Map the VPE to the first possible CPU */
4685 vpe->col_idx = cpumask_first(cpu_online_mask);
4686 irq_data_update_effective_affinity(d, cpumask_of(vpe->col_idx));
4687
4688 /*
4689 * If we use the list map, we issue VMAPP on demand... Unless
4690 * we're on a GICv4.1 and we eagerly map the VPE on all ITSs
4691 * so that VSGIs can work.
4692 */
4693 if (!gic_requires_eager_mapping())
4694 return 0;
4695
4696 list_for_each_entry(its, &its_nodes, entry) {
4697 if (!is_v4(its))
4698 continue;
4699
4700 its_send_vmapp(its, vpe, true);
4701 its_send_vinvall(its, vpe);
4702 }
4703
4704 return 0;
4705 }
4706
its_vpe_irq_domain_deactivate(struct irq_domain * domain,struct irq_data * d)4707 static void its_vpe_irq_domain_deactivate(struct irq_domain *domain,
4708 struct irq_data *d)
4709 {
4710 struct its_vpe *vpe = irq_data_get_irq_chip_data(d);
4711 struct its_node *its;
4712
4713 /*
4714 * If we use the list map on GICv4.0, we unmap the VPE once no
4715 * VLPIs are associated with the VM.
4716 */
4717 if (!gic_requires_eager_mapping())
4718 return;
4719
4720 list_for_each_entry(its, &its_nodes, entry) {
4721 if (!is_v4(its))
4722 continue;
4723
4724 its_send_vmapp(its, vpe, false);
4725 }
4726
4727 /*
4728 * There may be a direct read to the VPT after unmapping the
4729 * vPE, to guarantee the validity of this, we make the VPT
4730 * memory coherent with the CPU caches here.
4731 */
4732 if (find_4_1_its() && !atomic_read(&vpe->vmapp_count))
4733 gic_flush_dcache_to_poc(page_address(vpe->vpt_page),
4734 LPI_PENDBASE_SZ);
4735 }
4736
4737 static const struct irq_domain_ops its_vpe_domain_ops = {
4738 .alloc = its_vpe_irq_domain_alloc,
4739 .free = its_vpe_irq_domain_free,
4740 .activate = its_vpe_irq_domain_activate,
4741 .deactivate = its_vpe_irq_domain_deactivate,
4742 };
4743
its_force_quiescent(void __iomem * base)4744 static int its_force_quiescent(void __iomem *base)
4745 {
4746 u32 count = 1000000; /* 1s */
4747 u32 val;
4748
4749 val = readl_relaxed(base + GITS_CTLR);
4750 /*
4751 * GIC architecture specification requires the ITS to be both
4752 * disabled and quiescent for writes to GITS_BASER<n> or
4753 * GITS_CBASER to not have UNPREDICTABLE results.
4754 */
4755 if ((val & GITS_CTLR_QUIESCENT) && !(val & GITS_CTLR_ENABLE))
4756 return 0;
4757
4758 /* Disable the generation of all interrupts to this ITS */
4759 val &= ~(GITS_CTLR_ENABLE | GITS_CTLR_ImDe);
4760 writel_relaxed(val, base + GITS_CTLR);
4761
4762 /* Poll GITS_CTLR and wait until ITS becomes quiescent */
4763 while (1) {
4764 val = readl_relaxed(base + GITS_CTLR);
4765 if (val & GITS_CTLR_QUIESCENT)
4766 return 0;
4767
4768 count--;
4769 if (!count)
4770 return -EBUSY;
4771
4772 cpu_relax();
4773 udelay(1);
4774 }
4775 }
4776
its_enable_quirk_cavium_22375(void * data)4777 static bool __maybe_unused its_enable_quirk_cavium_22375(void *data)
4778 {
4779 struct its_node *its = data;
4780
4781 /* erratum 22375: only alloc 8MB table size (20 bits) */
4782 its->typer &= ~GITS_TYPER_DEVBITS;
4783 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, 20 - 1);
4784 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_22375;
4785
4786 return true;
4787 }
4788
its_enable_quirk_cavium_23144(void * data)4789 static bool __maybe_unused its_enable_quirk_cavium_23144(void *data)
4790 {
4791 struct its_node *its = data;
4792
4793 its->flags |= ITS_FLAGS_WORKAROUND_CAVIUM_23144;
4794
4795 return true;
4796 }
4797
its_enable_quirk_qdf2400_e0065(void * data)4798 static bool __maybe_unused its_enable_quirk_qdf2400_e0065(void *data)
4799 {
4800 struct its_node *its = data;
4801
4802 /* On QDF2400, the size of the ITE is 16Bytes */
4803 its->typer &= ~GITS_TYPER_ITT_ENTRY_SIZE;
4804 its->typer |= FIELD_PREP(GITS_TYPER_ITT_ENTRY_SIZE, 16 - 1);
4805
4806 return true;
4807 }
4808
its_irq_get_msi_base_pre_its(struct its_device * its_dev)4809 static u64 its_irq_get_msi_base_pre_its(struct its_device *its_dev)
4810 {
4811 struct its_node *its = its_dev->its;
4812
4813 /*
4814 * The Socionext Synquacer SoC has a so-called 'pre-ITS',
4815 * which maps 32-bit writes targeted at a separate window of
4816 * size '4 << device_id_bits' onto writes to GITS_TRANSLATER
4817 * with device ID taken from bits [device_id_bits + 1:2] of
4818 * the window offset.
4819 */
4820 return its->pre_its_base + (its_dev->device_id << 2);
4821 }
4822
its_enable_quirk_socionext_synquacer(void * data)4823 static bool __maybe_unused its_enable_quirk_socionext_synquacer(void *data)
4824 {
4825 struct its_node *its = data;
4826 u32 pre_its_window[2];
4827 u32 ids;
4828
4829 if (!fwnode_property_read_u32_array(its->fwnode_handle,
4830 "socionext,synquacer-pre-its",
4831 pre_its_window,
4832 ARRAY_SIZE(pre_its_window))) {
4833
4834 its->pre_its_base = pre_its_window[0];
4835 its->get_msi_base = its_irq_get_msi_base_pre_its;
4836
4837 ids = ilog2(pre_its_window[1]) - 2;
4838 if (device_ids(its) > ids) {
4839 its->typer &= ~GITS_TYPER_DEVBITS;
4840 its->typer |= FIELD_PREP(GITS_TYPER_DEVBITS, ids - 1);
4841 }
4842
4843 /* the pre-ITS breaks isolation, so disable MSI remapping */
4844 its->msi_domain_flags &= ~IRQ_DOMAIN_FLAG_ISOLATED_MSI;
4845 return true;
4846 }
4847 return false;
4848 }
4849
its_enable_quirk_hip07_161600802(void * data)4850 static bool __maybe_unused its_enable_quirk_hip07_161600802(void *data)
4851 {
4852 struct its_node *its = data;
4853
4854 /*
4855 * Hip07 insists on using the wrong address for the VLPI
4856 * page. Trick it into doing the right thing...
4857 */
4858 its->vlpi_redist_offset = SZ_128K;
4859 return true;
4860 }
4861
its_enable_rk3588001(void * data)4862 static bool __maybe_unused its_enable_rk3588001(void *data)
4863 {
4864 struct its_node *its = data;
4865
4866 if (!of_machine_is_compatible("rockchip,rk3588") &&
4867 !of_machine_is_compatible("rockchip,rk3588s"))
4868 return false;
4869
4870 its->flags |= ITS_FLAGS_FORCE_NON_SHAREABLE;
4871 gic_rdists->flags |= RDIST_FLAGS_FORCE_NON_SHAREABLE;
4872
4873 return true;
4874 }
4875
its_set_non_coherent(void * data)4876 static bool its_set_non_coherent(void *data)
4877 {
4878 struct its_node *its = data;
4879
4880 its->flags |= ITS_FLAGS_FORCE_NON_SHAREABLE;
4881 return true;
4882 }
4883
its_enable_quirk_hip09_162100801(void * data)4884 static bool __maybe_unused its_enable_quirk_hip09_162100801(void *data)
4885 {
4886 struct its_node *its = data;
4887
4888 its->flags |= ITS_FLAGS_WORKAROUND_HISILICON_162100801;
4889 return true;
4890 }
4891
its_enable_rk3568002(void * data)4892 static bool __maybe_unused its_enable_rk3568002(void *data)
4893 {
4894 if (!of_machine_is_compatible("rockchip,rk3566") &&
4895 !of_machine_is_compatible("rockchip,rk3568"))
4896 return false;
4897
4898 gfp_flags_quirk |= GFP_DMA32;
4899
4900 return true;
4901 }
4902
4903 static const struct gic_quirk its_quirks[] = {
4904 #ifdef CONFIG_CAVIUM_ERRATUM_22375
4905 {
4906 .desc = "ITS: Cavium errata 22375, 24313",
4907 .iidr = 0xa100034c, /* ThunderX pass 1.x */
4908 .mask = 0xffff0fff,
4909 .init = its_enable_quirk_cavium_22375,
4910 },
4911 #endif
4912 #ifdef CONFIG_CAVIUM_ERRATUM_23144
4913 {
4914 .desc = "ITS: Cavium erratum 23144",
4915 .iidr = 0xa100034c, /* ThunderX pass 1.x */
4916 .mask = 0xffff0fff,
4917 .init = its_enable_quirk_cavium_23144,
4918 },
4919 #endif
4920 #ifdef CONFIG_QCOM_QDF2400_ERRATUM_0065
4921 {
4922 .desc = "ITS: QDF2400 erratum 0065",
4923 .iidr = 0x00001070, /* QDF2400 ITS rev 1.x */
4924 .mask = 0xffffffff,
4925 .init = its_enable_quirk_qdf2400_e0065,
4926 },
4927 #endif
4928 #ifdef CONFIG_SOCIONEXT_SYNQUACER_PREITS
4929 {
4930 /*
4931 * The Socionext Synquacer SoC incorporates ARM's own GIC-500
4932 * implementation, but with a 'pre-ITS' added that requires
4933 * special handling in software.
4934 */
4935 .desc = "ITS: Socionext Synquacer pre-ITS",
4936 .iidr = 0x0001143b,
4937 .mask = 0xffffffff,
4938 .init = its_enable_quirk_socionext_synquacer,
4939 },
4940 #endif
4941 #ifdef CONFIG_HISILICON_ERRATUM_161600802
4942 {
4943 .desc = "ITS: Hip07 erratum 161600802",
4944 .iidr = 0x00000004,
4945 .mask = 0xffffffff,
4946 .init = its_enable_quirk_hip07_161600802,
4947 },
4948 #endif
4949 #ifdef CONFIG_HISILICON_ERRATUM_162100801
4950 {
4951 .desc = "ITS: Hip09 erratum 162100801",
4952 .iidr = 0x00051736,
4953 .mask = 0xffffffff,
4954 .init = its_enable_quirk_hip09_162100801,
4955 },
4956 #endif
4957 #ifdef CONFIG_ROCKCHIP_ERRATUM_3588001
4958 {
4959 .desc = "ITS: Rockchip erratum RK3588001",
4960 .iidr = 0x0201743b,
4961 .mask = 0xffffffff,
4962 .init = its_enable_rk3588001,
4963 },
4964 #endif
4965 {
4966 .desc = "ITS: non-coherent attribute",
4967 .property = "dma-noncoherent",
4968 .init = its_set_non_coherent,
4969 },
4970 #ifdef CONFIG_ROCKCHIP_ERRATUM_3568002
4971 {
4972 .desc = "ITS: Rockchip erratum RK3568002",
4973 .iidr = 0x0201743b,
4974 .mask = 0xffffffff,
4975 .init = its_enable_rk3568002,
4976 },
4977 #endif
4978 {
4979 }
4980 };
4981
its_enable_quirks(struct its_node * its)4982 static void its_enable_quirks(struct its_node *its)
4983 {
4984 u32 iidr = readl_relaxed(its->base + GITS_IIDR);
4985
4986 gic_enable_quirks(iidr, its_quirks, its);
4987
4988 if (is_of_node(its->fwnode_handle))
4989 gic_enable_of_quirks(to_of_node(its->fwnode_handle),
4990 its_quirks, its);
4991 }
4992
its_save_disable(void)4993 static int its_save_disable(void)
4994 {
4995 struct its_node *its;
4996 int err = 0;
4997
4998 raw_spin_lock(&its_lock);
4999 list_for_each_entry(its, &its_nodes, entry) {
5000 void __iomem *base;
5001
5002 base = its->base;
5003 its->ctlr_save = readl_relaxed(base + GITS_CTLR);
5004 err = its_force_quiescent(base);
5005 if (err) {
5006 pr_err("ITS@%pa: failed to quiesce: %d\n",
5007 &its->phys_base, err);
5008 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
5009 goto err;
5010 }
5011
5012 its->cbaser_save = gits_read_cbaser(base + GITS_CBASER);
5013 }
5014
5015 err:
5016 if (err) {
5017 list_for_each_entry_continue_reverse(its, &its_nodes, entry) {
5018 void __iomem *base;
5019
5020 base = its->base;
5021 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
5022 }
5023 }
5024 raw_spin_unlock(&its_lock);
5025
5026 return err;
5027 }
5028
its_restore_enable(void)5029 static void its_restore_enable(void)
5030 {
5031 struct its_node *its;
5032 int ret;
5033
5034 raw_spin_lock(&its_lock);
5035 list_for_each_entry(its, &its_nodes, entry) {
5036 void __iomem *base;
5037 int i;
5038
5039 base = its->base;
5040
5041 /*
5042 * Make sure that the ITS is disabled. If it fails to quiesce,
5043 * don't restore it since writing to CBASER or BASER<n>
5044 * registers is undefined according to the GIC v3 ITS
5045 * Specification.
5046 *
5047 * Firmware resuming with the ITS enabled is terminally broken.
5048 */
5049 WARN_ON(readl_relaxed(base + GITS_CTLR) & GITS_CTLR_ENABLE);
5050 ret = its_force_quiescent(base);
5051 if (ret) {
5052 pr_err("ITS@%pa: failed to quiesce on resume: %d\n",
5053 &its->phys_base, ret);
5054 continue;
5055 }
5056
5057 gits_write_cbaser(its->cbaser_save, base + GITS_CBASER);
5058
5059 /*
5060 * Writing CBASER resets CREADR to 0, so make CWRITER and
5061 * cmd_write line up with it.
5062 */
5063 its->cmd_write = its->cmd_base;
5064 gits_write_cwriter(0, base + GITS_CWRITER);
5065
5066 /* Restore GITS_BASER from the value cache. */
5067 for (i = 0; i < GITS_BASER_NR_REGS; i++) {
5068 struct its_baser *baser = &its->tables[i];
5069
5070 if (!(baser->val & GITS_BASER_VALID))
5071 continue;
5072
5073 its_write_baser(its, baser, baser->val);
5074 }
5075 writel_relaxed(its->ctlr_save, base + GITS_CTLR);
5076
5077 /*
5078 * Reinit the collection if it's stored in the ITS. This is
5079 * indicated by the col_id being less than the HCC field.
5080 * CID < HCC as specified in the GIC v3 Documentation.
5081 */
5082 if (its->collections[smp_processor_id()].col_id <
5083 GITS_TYPER_HCC(gic_read_typer(base + GITS_TYPER)))
5084 its_cpu_init_collection(its);
5085 }
5086 raw_spin_unlock(&its_lock);
5087 }
5088
5089 static struct syscore_ops its_syscore_ops = {
5090 .suspend = its_save_disable,
5091 .resume = its_restore_enable,
5092 };
5093
its_map_one(struct resource * res,int * err)5094 static void __init __iomem *its_map_one(struct resource *res, int *err)
5095 {
5096 void __iomem *its_base;
5097 u32 val;
5098
5099 its_base = ioremap(res->start, SZ_64K);
5100 if (!its_base) {
5101 pr_warn("ITS@%pa: Unable to map ITS registers\n", &res->start);
5102 *err = -ENOMEM;
5103 return NULL;
5104 }
5105
5106 val = readl_relaxed(its_base + GITS_PIDR2) & GIC_PIDR2_ARCH_MASK;
5107 if (val != 0x30 && val != 0x40) {
5108 pr_warn("ITS@%pa: No ITS detected, giving up\n", &res->start);
5109 *err = -ENODEV;
5110 goto out_unmap;
5111 }
5112
5113 *err = its_force_quiescent(its_base);
5114 if (*err) {
5115 pr_warn("ITS@%pa: Failed to quiesce, giving up\n", &res->start);
5116 goto out_unmap;
5117 }
5118
5119 return its_base;
5120
5121 out_unmap:
5122 iounmap(its_base);
5123 return NULL;
5124 }
5125
its_init_domain(struct its_node * its)5126 static int its_init_domain(struct its_node *its)
5127 {
5128 struct irq_domain *inner_domain;
5129 struct msi_domain_info *info;
5130
5131 info = kzalloc(sizeof(*info), GFP_KERNEL);
5132 if (!info)
5133 return -ENOMEM;
5134
5135 info->ops = &its_msi_domain_ops;
5136 info->data = its;
5137
5138 inner_domain = irq_domain_create_hierarchy(its_parent,
5139 its->msi_domain_flags, 0,
5140 its->fwnode_handle, &its_domain_ops,
5141 info);
5142 if (!inner_domain) {
5143 kfree(info);
5144 return -ENOMEM;
5145 }
5146
5147 irq_domain_update_bus_token(inner_domain, DOMAIN_BUS_NEXUS);
5148
5149 inner_domain->msi_parent_ops = &gic_v3_its_msi_parent_ops;
5150 inner_domain->flags |= IRQ_DOMAIN_FLAG_MSI_PARENT;
5151
5152 return 0;
5153 }
5154
its_init_vpe_domain(void)5155 static int its_init_vpe_domain(void)
5156 {
5157 struct its_node *its;
5158 u32 devid;
5159 int entries;
5160
5161 if (gic_rdists->has_direct_lpi) {
5162 pr_info("ITS: Using DirectLPI for VPE invalidation\n");
5163 return 0;
5164 }
5165
5166 /* Any ITS will do, even if not v4 */
5167 its = list_first_entry(&its_nodes, struct its_node, entry);
5168
5169 entries = roundup_pow_of_two(nr_cpu_ids);
5170 vpe_proxy.vpes = kcalloc(entries, sizeof(*vpe_proxy.vpes),
5171 GFP_KERNEL);
5172 if (!vpe_proxy.vpes)
5173 return -ENOMEM;
5174
5175 /* Use the last possible DevID */
5176 devid = GENMASK(device_ids(its) - 1, 0);
5177 vpe_proxy.dev = its_create_device(its, devid, entries, false);
5178 if (!vpe_proxy.dev) {
5179 kfree(vpe_proxy.vpes);
5180 pr_err("ITS: Can't allocate GICv4 proxy device\n");
5181 return -ENOMEM;
5182 }
5183
5184 BUG_ON(entries > vpe_proxy.dev->nr_ites);
5185
5186 raw_spin_lock_init(&vpe_proxy.lock);
5187 vpe_proxy.next_victim = 0;
5188 pr_info("ITS: Allocated DevID %x as GICv4 proxy device (%d slots)\n",
5189 devid, vpe_proxy.dev->nr_ites);
5190
5191 return 0;
5192 }
5193
its_compute_its_list_map(struct its_node * its)5194 static int __init its_compute_its_list_map(struct its_node *its)
5195 {
5196 int its_number;
5197 u32 ctlr;
5198
5199 /*
5200 * This is assumed to be done early enough that we're
5201 * guaranteed to be single-threaded, hence no
5202 * locking. Should this change, we should address
5203 * this.
5204 */
5205 its_number = find_first_zero_bit(&its_list_map, GICv4_ITS_LIST_MAX);
5206 if (its_number >= GICv4_ITS_LIST_MAX) {
5207 pr_err("ITS@%pa: No ITSList entry available!\n",
5208 &its->phys_base);
5209 return -EINVAL;
5210 }
5211
5212 ctlr = readl_relaxed(its->base + GITS_CTLR);
5213 ctlr &= ~GITS_CTLR_ITS_NUMBER;
5214 ctlr |= its_number << GITS_CTLR_ITS_NUMBER_SHIFT;
5215 writel_relaxed(ctlr, its->base + GITS_CTLR);
5216 ctlr = readl_relaxed(its->base + GITS_CTLR);
5217 if ((ctlr & GITS_CTLR_ITS_NUMBER) != (its_number << GITS_CTLR_ITS_NUMBER_SHIFT)) {
5218 its_number = ctlr & GITS_CTLR_ITS_NUMBER;
5219 its_number >>= GITS_CTLR_ITS_NUMBER_SHIFT;
5220 }
5221
5222 if (test_and_set_bit(its_number, &its_list_map)) {
5223 pr_err("ITS@%pa: Duplicate ITSList entry %d\n",
5224 &its->phys_base, its_number);
5225 return -EINVAL;
5226 }
5227
5228 return its_number;
5229 }
5230
its_probe_one(struct its_node * its)5231 static int __init its_probe_one(struct its_node *its)
5232 {
5233 u64 baser, tmp;
5234 struct page *page;
5235 u32 ctlr;
5236 int err;
5237
5238 its_enable_quirks(its);
5239
5240 if (is_v4(its)) {
5241 if (!(its->typer & GITS_TYPER_VMOVP)) {
5242 err = its_compute_its_list_map(its);
5243 if (err < 0)
5244 goto out;
5245
5246 its->list_nr = err;
5247
5248 pr_info("ITS@%pa: Using ITS number %d\n",
5249 &its->phys_base, err);
5250 } else {
5251 pr_info("ITS@%pa: Single VMOVP capable\n", &its->phys_base);
5252 }
5253
5254 if (is_v4_1(its)) {
5255 u32 svpet = FIELD_GET(GITS_TYPER_SVPET, its->typer);
5256
5257 its->sgir_base = ioremap(its->phys_base + SZ_128K, SZ_64K);
5258 if (!its->sgir_base) {
5259 err = -ENOMEM;
5260 goto out;
5261 }
5262
5263 its->mpidr = readl_relaxed(its->base + GITS_MPIDR);
5264
5265 pr_info("ITS@%pa: Using GICv4.1 mode %08x %08x\n",
5266 &its->phys_base, its->mpidr, svpet);
5267 }
5268 }
5269
5270 page = its_alloc_pages_node(its->numa_node,
5271 GFP_KERNEL | __GFP_ZERO,
5272 get_order(ITS_CMD_QUEUE_SZ));
5273 if (!page) {
5274 err = -ENOMEM;
5275 goto out_unmap_sgir;
5276 }
5277 its->cmd_base = (void *)page_address(page);
5278 its->cmd_write = its->cmd_base;
5279
5280 err = its_alloc_tables(its);
5281 if (err)
5282 goto out_free_cmd;
5283
5284 err = its_alloc_collections(its);
5285 if (err)
5286 goto out_free_tables;
5287
5288 baser = (virt_to_phys(its->cmd_base) |
5289 GITS_CBASER_RaWaWb |
5290 GITS_CBASER_InnerShareable |
5291 (ITS_CMD_QUEUE_SZ / SZ_4K - 1) |
5292 GITS_CBASER_VALID);
5293
5294 gits_write_cbaser(baser, its->base + GITS_CBASER);
5295 tmp = gits_read_cbaser(its->base + GITS_CBASER);
5296
5297 if (its->flags & ITS_FLAGS_FORCE_NON_SHAREABLE)
5298 tmp &= ~GITS_CBASER_SHAREABILITY_MASK;
5299
5300 if ((tmp ^ baser) & GITS_CBASER_SHAREABILITY_MASK) {
5301 if (!(tmp & GITS_CBASER_SHAREABILITY_MASK)) {
5302 /*
5303 * The HW reports non-shareable, we must
5304 * remove the cacheability attributes as
5305 * well.
5306 */
5307 baser &= ~(GITS_CBASER_SHAREABILITY_MASK |
5308 GITS_CBASER_CACHEABILITY_MASK);
5309 baser |= GITS_CBASER_nC;
5310 gits_write_cbaser(baser, its->base + GITS_CBASER);
5311 }
5312 pr_info("ITS: using cache flushing for cmd queue\n");
5313 its->flags |= ITS_FLAGS_CMDQ_NEEDS_FLUSHING;
5314 }
5315
5316 gits_write_cwriter(0, its->base + GITS_CWRITER);
5317 ctlr = readl_relaxed(its->base + GITS_CTLR);
5318 ctlr |= GITS_CTLR_ENABLE;
5319 if (is_v4(its))
5320 ctlr |= GITS_CTLR_ImDe;
5321 writel_relaxed(ctlr, its->base + GITS_CTLR);
5322
5323 err = its_init_domain(its);
5324 if (err)
5325 goto out_free_tables;
5326
5327 raw_spin_lock(&its_lock);
5328 list_add(&its->entry, &its_nodes);
5329 raw_spin_unlock(&its_lock);
5330
5331 return 0;
5332
5333 out_free_tables:
5334 its_free_tables(its);
5335 out_free_cmd:
5336 its_free_pages(its->cmd_base, get_order(ITS_CMD_QUEUE_SZ));
5337 out_unmap_sgir:
5338 if (its->sgir_base)
5339 iounmap(its->sgir_base);
5340 out:
5341 pr_err("ITS@%pa: failed probing (%d)\n", &its->phys_base, err);
5342 return err;
5343 }
5344
gic_rdists_supports_plpis(void)5345 static bool gic_rdists_supports_plpis(void)
5346 {
5347 return !!(gic_read_typer(gic_data_rdist_rd_base() + GICR_TYPER) & GICR_TYPER_PLPIS);
5348 }
5349
redist_disable_lpis(void)5350 static int redist_disable_lpis(void)
5351 {
5352 void __iomem *rbase = gic_data_rdist_rd_base();
5353 u64 timeout = USEC_PER_SEC;
5354 u64 val;
5355
5356 if (!gic_rdists_supports_plpis()) {
5357 pr_info("CPU%d: LPIs not supported\n", smp_processor_id());
5358 return -ENXIO;
5359 }
5360
5361 val = readl_relaxed(rbase + GICR_CTLR);
5362 if (!(val & GICR_CTLR_ENABLE_LPIS))
5363 return 0;
5364
5365 /*
5366 * If coming via a CPU hotplug event, we don't need to disable
5367 * LPIs before trying to re-enable them. They are already
5368 * configured and all is well in the world.
5369 *
5370 * If running with preallocated tables, there is nothing to do.
5371 */
5372 if ((gic_data_rdist()->flags & RD_LOCAL_LPI_ENABLED) ||
5373 (gic_rdists->flags & RDIST_FLAGS_RD_TABLES_PREALLOCATED))
5374 return 0;
5375
5376 /*
5377 * From that point on, we only try to do some damage control.
5378 */
5379 pr_warn("GICv3: CPU%d: Booted with LPIs enabled, memory probably corrupted\n",
5380 smp_processor_id());
5381 add_taint(TAINT_CRAP, LOCKDEP_STILL_OK);
5382
5383 /* Disable LPIs */
5384 val &= ~GICR_CTLR_ENABLE_LPIS;
5385 writel_relaxed(val, rbase + GICR_CTLR);
5386
5387 /* Make sure any change to GICR_CTLR is observable by the GIC */
5388 dsb(sy);
5389
5390 /*
5391 * Software must observe RWP==0 after clearing GICR_CTLR.EnableLPIs
5392 * from 1 to 0 before programming GICR_PEND{PROP}BASER registers.
5393 * Error out if we time out waiting for RWP to clear.
5394 */
5395 while (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_RWP) {
5396 if (!timeout) {
5397 pr_err("CPU%d: Timeout while disabling LPIs\n",
5398 smp_processor_id());
5399 return -ETIMEDOUT;
5400 }
5401 udelay(1);
5402 timeout--;
5403 }
5404
5405 /*
5406 * After it has been written to 1, it is IMPLEMENTATION
5407 * DEFINED whether GICR_CTLR.EnableLPI becomes RES1 or can be
5408 * cleared to 0. Error out if clearing the bit failed.
5409 */
5410 if (readl_relaxed(rbase + GICR_CTLR) & GICR_CTLR_ENABLE_LPIS) {
5411 pr_err("CPU%d: Failed to disable LPIs\n", smp_processor_id());
5412 return -EBUSY;
5413 }
5414
5415 return 0;
5416 }
5417
its_cpu_init(void)5418 int its_cpu_init(void)
5419 {
5420 if (!list_empty(&its_nodes)) {
5421 int ret;
5422
5423 ret = redist_disable_lpis();
5424 if (ret)
5425 return ret;
5426
5427 its_cpu_init_lpis();
5428 its_cpu_init_collections();
5429 }
5430
5431 return 0;
5432 }
5433
rdist_memreserve_cpuhp_cleanup_workfn(struct work_struct * work)5434 static void rdist_memreserve_cpuhp_cleanup_workfn(struct work_struct *work)
5435 {
5436 cpuhp_remove_state_nocalls(gic_rdists->cpuhp_memreserve_state);
5437 gic_rdists->cpuhp_memreserve_state = CPUHP_INVALID;
5438 }
5439
5440 static DECLARE_WORK(rdist_memreserve_cpuhp_cleanup_work,
5441 rdist_memreserve_cpuhp_cleanup_workfn);
5442
its_cpu_memreserve_lpi(unsigned int cpu)5443 static int its_cpu_memreserve_lpi(unsigned int cpu)
5444 {
5445 struct page *pend_page;
5446 int ret = 0;
5447
5448 /* This gets to run exactly once per CPU */
5449 if (gic_data_rdist()->flags & RD_LOCAL_MEMRESERVE_DONE)
5450 return 0;
5451
5452 pend_page = gic_data_rdist()->pend_page;
5453 if (WARN_ON(!pend_page)) {
5454 ret = -ENOMEM;
5455 goto out;
5456 }
5457 /*
5458 * If the pending table was pre-programmed, free the memory we
5459 * preemptively allocated. Otherwise, reserve that memory for
5460 * later kexecs.
5461 */
5462 if (gic_data_rdist()->flags & RD_LOCAL_PENDTABLE_PREALLOCATED) {
5463 its_free_pending_table(pend_page);
5464 gic_data_rdist()->pend_page = NULL;
5465 } else {
5466 phys_addr_t paddr = page_to_phys(pend_page);
5467 WARN_ON(gic_reserve_range(paddr, LPI_PENDBASE_SZ));
5468 }
5469
5470 out:
5471 /* Last CPU being brought up gets to issue the cleanup */
5472 if (!IS_ENABLED(CONFIG_SMP) ||
5473 cpumask_equal(&cpus_booted_once_mask, cpu_possible_mask))
5474 schedule_work(&rdist_memreserve_cpuhp_cleanup_work);
5475
5476 gic_data_rdist()->flags |= RD_LOCAL_MEMRESERVE_DONE;
5477 return ret;
5478 }
5479
5480 /* Mark all the BASER registers as invalid before they get reprogrammed */
its_reset_one(struct resource * res)5481 static int __init its_reset_one(struct resource *res)
5482 {
5483 void __iomem *its_base;
5484 int err, i;
5485
5486 its_base = its_map_one(res, &err);
5487 if (!its_base)
5488 return err;
5489
5490 for (i = 0; i < GITS_BASER_NR_REGS; i++)
5491 gits_write_baser(0, its_base + GITS_BASER + (i << 3));
5492
5493 iounmap(its_base);
5494 return 0;
5495 }
5496
5497 static const struct of_device_id its_device_id[] = {
5498 { .compatible = "arm,gic-v3-its", },
5499 {},
5500 };
5501
its_node_init(struct resource * res,struct fwnode_handle * handle,int numa_node)5502 static struct its_node __init *its_node_init(struct resource *res,
5503 struct fwnode_handle *handle, int numa_node)
5504 {
5505 void __iomem *its_base;
5506 struct its_node *its;
5507 int err;
5508
5509 its_base = its_map_one(res, &err);
5510 if (!its_base)
5511 return NULL;
5512
5513 pr_info("ITS %pR\n", res);
5514
5515 its = kzalloc(sizeof(*its), GFP_KERNEL);
5516 if (!its)
5517 goto out_unmap;
5518
5519 raw_spin_lock_init(&its->lock);
5520 mutex_init(&its->dev_alloc_lock);
5521 INIT_LIST_HEAD(&its->entry);
5522 INIT_LIST_HEAD(&its->its_device_list);
5523
5524 its->typer = gic_read_typer(its_base + GITS_TYPER);
5525 its->base = its_base;
5526 its->phys_base = res->start;
5527 its->get_msi_base = its_irq_get_msi_base;
5528 its->msi_domain_flags = IRQ_DOMAIN_FLAG_ISOLATED_MSI;
5529
5530 its->numa_node = numa_node;
5531 its->fwnode_handle = handle;
5532
5533 return its;
5534
5535 out_unmap:
5536 iounmap(its_base);
5537 return NULL;
5538 }
5539
its_node_destroy(struct its_node * its)5540 static void its_node_destroy(struct its_node *its)
5541 {
5542 iounmap(its->base);
5543 kfree(its);
5544 }
5545
its_of_probe(struct device_node * node)5546 static int __init its_of_probe(struct device_node *node)
5547 {
5548 struct device_node *np;
5549 struct resource res;
5550 int err;
5551
5552 /*
5553 * Make sure *all* the ITS are reset before we probe any, as
5554 * they may be sharing memory. If any of the ITS fails to
5555 * reset, don't even try to go any further, as this could
5556 * result in something even worse.
5557 */
5558 for (np = of_find_matching_node(node, its_device_id); np;
5559 np = of_find_matching_node(np, its_device_id)) {
5560 if (!of_device_is_available(np) ||
5561 !of_property_read_bool(np, "msi-controller") ||
5562 of_address_to_resource(np, 0, &res))
5563 continue;
5564
5565 err = its_reset_one(&res);
5566 if (err)
5567 return err;
5568 }
5569
5570 for (np = of_find_matching_node(node, its_device_id); np;
5571 np = of_find_matching_node(np, its_device_id)) {
5572 struct its_node *its;
5573
5574 if (!of_device_is_available(np))
5575 continue;
5576 if (!of_property_read_bool(np, "msi-controller")) {
5577 pr_warn("%pOF: no msi-controller property, ITS ignored\n",
5578 np);
5579 continue;
5580 }
5581
5582 if (of_address_to_resource(np, 0, &res)) {
5583 pr_warn("%pOF: no regs?\n", np);
5584 continue;
5585 }
5586
5587
5588 its = its_node_init(&res, &np->fwnode, of_node_to_nid(np));
5589 if (!its)
5590 return -ENOMEM;
5591
5592 err = its_probe_one(its);
5593 if (err) {
5594 its_node_destroy(its);
5595 return err;
5596 }
5597 }
5598 return 0;
5599 }
5600
5601 #ifdef CONFIG_ACPI
5602
5603 #define ACPI_GICV3_ITS_MEM_SIZE (SZ_128K)
5604
5605 #ifdef CONFIG_ACPI_NUMA
5606 struct its_srat_map {
5607 /* numa node id */
5608 u32 numa_node;
5609 /* GIC ITS ID */
5610 u32 its_id;
5611 };
5612
5613 static struct its_srat_map *its_srat_maps __initdata;
5614 static int its_in_srat __initdata;
5615
acpi_get_its_numa_node(u32 its_id)5616 static int __init acpi_get_its_numa_node(u32 its_id)
5617 {
5618 int i;
5619
5620 for (i = 0; i < its_in_srat; i++) {
5621 if (its_id == its_srat_maps[i].its_id)
5622 return its_srat_maps[i].numa_node;
5623 }
5624 return NUMA_NO_NODE;
5625 }
5626
gic_acpi_match_srat_its(union acpi_subtable_headers * header,const unsigned long end)5627 static int __init gic_acpi_match_srat_its(union acpi_subtable_headers *header,
5628 const unsigned long end)
5629 {
5630 return 0;
5631 }
5632
gic_acpi_parse_srat_its(union acpi_subtable_headers * header,const unsigned long end)5633 static int __init gic_acpi_parse_srat_its(union acpi_subtable_headers *header,
5634 const unsigned long end)
5635 {
5636 int node;
5637 struct acpi_srat_gic_its_affinity *its_affinity;
5638
5639 its_affinity = (struct acpi_srat_gic_its_affinity *)header;
5640 if (!its_affinity)
5641 return -EINVAL;
5642
5643 if (its_affinity->header.length < sizeof(*its_affinity)) {
5644 pr_err("SRAT: Invalid header length %d in ITS affinity\n",
5645 its_affinity->header.length);
5646 return -EINVAL;
5647 }
5648
5649 /*
5650 * Note that in theory a new proximity node could be created by this
5651 * entry as it is an SRAT resource allocation structure.
5652 * We do not currently support doing so.
5653 */
5654 node = pxm_to_node(its_affinity->proximity_domain);
5655
5656 if (node == NUMA_NO_NODE || node >= MAX_NUMNODES) {
5657 pr_err("SRAT: Invalid NUMA node %d in ITS affinity\n", node);
5658 return 0;
5659 }
5660
5661 its_srat_maps[its_in_srat].numa_node = node;
5662 its_srat_maps[its_in_srat].its_id = its_affinity->its_id;
5663 its_in_srat++;
5664 pr_info("SRAT: PXM %d -> ITS %d -> Node %d\n",
5665 its_affinity->proximity_domain, its_affinity->its_id, node);
5666
5667 return 0;
5668 }
5669
acpi_table_parse_srat_its(void)5670 static void __init acpi_table_parse_srat_its(void)
5671 {
5672 int count;
5673
5674 count = acpi_table_parse_entries(ACPI_SIG_SRAT,
5675 sizeof(struct acpi_table_srat),
5676 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
5677 gic_acpi_match_srat_its, 0);
5678 if (count <= 0)
5679 return;
5680
5681 its_srat_maps = kmalloc_array(count, sizeof(struct its_srat_map),
5682 GFP_KERNEL);
5683 if (!its_srat_maps)
5684 return;
5685
5686 acpi_table_parse_entries(ACPI_SIG_SRAT,
5687 sizeof(struct acpi_table_srat),
5688 ACPI_SRAT_TYPE_GIC_ITS_AFFINITY,
5689 gic_acpi_parse_srat_its, 0);
5690 }
5691
5692 /* free the its_srat_maps after ITS probing */
acpi_its_srat_maps_free(void)5693 static void __init acpi_its_srat_maps_free(void)
5694 {
5695 kfree(its_srat_maps);
5696 }
5697 #else
acpi_table_parse_srat_its(void)5698 static void __init acpi_table_parse_srat_its(void) { }
acpi_get_its_numa_node(u32 its_id)5699 static int __init acpi_get_its_numa_node(u32 its_id) { return NUMA_NO_NODE; }
acpi_its_srat_maps_free(void)5700 static void __init acpi_its_srat_maps_free(void) { }
5701 #endif
5702
gic_acpi_parse_madt_its(union acpi_subtable_headers * header,const unsigned long end)5703 static int __init gic_acpi_parse_madt_its(union acpi_subtable_headers *header,
5704 const unsigned long end)
5705 {
5706 struct acpi_madt_generic_translator *its_entry;
5707 struct fwnode_handle *dom_handle;
5708 struct its_node *its;
5709 struct resource res;
5710 int err;
5711
5712 its_entry = (struct acpi_madt_generic_translator *)header;
5713 memset(&res, 0, sizeof(res));
5714 res.start = its_entry->base_address;
5715 res.end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1;
5716 res.flags = IORESOURCE_MEM;
5717
5718 dom_handle = irq_domain_alloc_fwnode(&res.start);
5719 if (!dom_handle) {
5720 pr_err("ITS@%pa: Unable to allocate GICv3 ITS domain token\n",
5721 &res.start);
5722 return -ENOMEM;
5723 }
5724
5725 err = iort_register_domain_token(its_entry->translation_id, res.start,
5726 dom_handle);
5727 if (err) {
5728 pr_err("ITS@%pa: Unable to register GICv3 ITS domain token (ITS ID %d) to IORT\n",
5729 &res.start, its_entry->translation_id);
5730 goto dom_err;
5731 }
5732
5733 its = its_node_init(&res, dom_handle,
5734 acpi_get_its_numa_node(its_entry->translation_id));
5735 if (!its) {
5736 err = -ENOMEM;
5737 goto node_err;
5738 }
5739
5740 if (acpi_get_madt_revision() >= 7 &&
5741 (its_entry->flags & ACPI_MADT_ITS_NON_COHERENT))
5742 its->flags |= ITS_FLAGS_FORCE_NON_SHAREABLE;
5743
5744 err = its_probe_one(its);
5745 if (!err)
5746 return 0;
5747
5748 node_err:
5749 iort_deregister_domain_token(its_entry->translation_id);
5750 dom_err:
5751 irq_domain_free_fwnode(dom_handle);
5752 return err;
5753 }
5754
its_acpi_reset(union acpi_subtable_headers * header,const unsigned long end)5755 static int __init its_acpi_reset(union acpi_subtable_headers *header,
5756 const unsigned long end)
5757 {
5758 struct acpi_madt_generic_translator *its_entry;
5759 struct resource res;
5760
5761 its_entry = (struct acpi_madt_generic_translator *)header;
5762 res = (struct resource) {
5763 .start = its_entry->base_address,
5764 .end = its_entry->base_address + ACPI_GICV3_ITS_MEM_SIZE - 1,
5765 .flags = IORESOURCE_MEM,
5766 };
5767
5768 return its_reset_one(&res);
5769 }
5770
its_acpi_probe(void)5771 static void __init its_acpi_probe(void)
5772 {
5773 acpi_table_parse_srat_its();
5774 /*
5775 * Make sure *all* the ITS are reset before we probe any, as
5776 * they may be sharing memory. If any of the ITS fails to
5777 * reset, don't even try to go any further, as this could
5778 * result in something even worse.
5779 */
5780 if (acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
5781 its_acpi_reset, 0) > 0)
5782 acpi_table_parse_madt(ACPI_MADT_TYPE_GENERIC_TRANSLATOR,
5783 gic_acpi_parse_madt_its, 0);
5784 acpi_its_srat_maps_free();
5785 }
5786 #else
its_acpi_probe(void)5787 static void __init its_acpi_probe(void) { }
5788 #endif
5789
its_lpi_memreserve_init(void)5790 int __init its_lpi_memreserve_init(void)
5791 {
5792 int state;
5793
5794 if (!efi_enabled(EFI_CONFIG_TABLES))
5795 return 0;
5796
5797 if (list_empty(&its_nodes))
5798 return 0;
5799
5800 gic_rdists->cpuhp_memreserve_state = CPUHP_INVALID;
5801 state = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN,
5802 "irqchip/arm/gicv3/memreserve:online",
5803 its_cpu_memreserve_lpi,
5804 NULL);
5805 if (state < 0)
5806 return state;
5807
5808 gic_rdists->cpuhp_memreserve_state = state;
5809
5810 return 0;
5811 }
5812
its_init(struct fwnode_handle * handle,struct rdists * rdists,struct irq_domain * parent_domain,u8 irq_prio)5813 int __init its_init(struct fwnode_handle *handle, struct rdists *rdists,
5814 struct irq_domain *parent_domain, u8 irq_prio)
5815 {
5816 struct device_node *of_node;
5817 struct its_node *its;
5818 bool has_v4 = false;
5819 bool has_v4_1 = false;
5820 int err;
5821
5822 itt_pool = gen_pool_create(get_order(ITS_ITT_ALIGN), -1);
5823 if (!itt_pool)
5824 return -ENOMEM;
5825
5826 gic_rdists = rdists;
5827
5828 lpi_prop_prio = irq_prio;
5829 its_parent = parent_domain;
5830 of_node = to_of_node(handle);
5831 if (of_node)
5832 its_of_probe(of_node);
5833 else
5834 its_acpi_probe();
5835
5836 if (list_empty(&its_nodes)) {
5837 pr_warn("ITS: No ITS available, not enabling LPIs\n");
5838 return -ENXIO;
5839 }
5840
5841 err = allocate_lpi_tables();
5842 if (err)
5843 return err;
5844
5845 list_for_each_entry(its, &its_nodes, entry) {
5846 has_v4 |= is_v4(its);
5847 has_v4_1 |= is_v4_1(its);
5848 }
5849
5850 /* Don't bother with inconsistent systems */
5851 if (WARN_ON(!has_v4_1 && rdists->has_rvpeid))
5852 rdists->has_rvpeid = false;
5853
5854 if (has_v4 & rdists->has_vlpis) {
5855 const struct irq_domain_ops *sgi_ops;
5856
5857 if (has_v4_1)
5858 sgi_ops = &its_sgi_domain_ops;
5859 else
5860 sgi_ops = NULL;
5861
5862 if (its_init_vpe_domain() ||
5863 its_init_v4(parent_domain, &its_vpe_domain_ops, sgi_ops)) {
5864 rdists->has_vlpis = false;
5865 pr_err("ITS: Disabling GICv4 support\n");
5866 }
5867 }
5868
5869 register_syscore_ops(&its_syscore_ops);
5870
5871 return 0;
5872 }
5873