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