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