xref: /kvm-unit-tests/arm/gic.c (revision ae7dac4ee8f6205461295f601b9c71459e7745c7)
1ac4a67b6SAndrew Jones /*
2ac4a67b6SAndrew Jones  * GIC tests
3ac4a67b6SAndrew Jones  *
4ac4a67b6SAndrew Jones  * GICv2
5ac4a67b6SAndrew Jones  *   + test sending/receiving IPIs
678ad7e95SAndre Przywara  *   + MMIO access tests
72e2d471dSAndrew Jones  * GICv3
82e2d471dSAndrew Jones  *   + test sending/receiving IPIs
9ac4a67b6SAndrew Jones  *
10ac4a67b6SAndrew Jones  * Copyright (C) 2016, Red Hat Inc, Andrew Jones <drjones@redhat.com>
11ac4a67b6SAndrew Jones  *
12ac4a67b6SAndrew Jones  * This work is licensed under the terms of the GNU LGPL, version 2.
13ac4a67b6SAndrew Jones  */
14ac4a67b6SAndrew Jones #include <libcflat.h>
15de582149SEric Auger #include <errata.h>
16ac4a67b6SAndrew Jones #include <asm/setup.h>
17ac4a67b6SAndrew Jones #include <asm/processor.h>
18ac4a67b6SAndrew Jones #include <asm/delay.h>
19ac4a67b6SAndrew Jones #include <asm/gic.h>
20ba74b106SEric Auger #include <asm/gic-v3-its.h>
21ac4a67b6SAndrew Jones #include <asm/smp.h>
22ac4a67b6SAndrew Jones #include <asm/barrier.h>
23ac4a67b6SAndrew Jones #include <asm/io.h>
24ac4a67b6SAndrew Jones 
25ca1b7a7bSAndrew Jones #define IPI_SENDER	1
26ca1b7a7bSAndrew Jones #define IPI_IRQ		1
27ca1b7a7bSAndrew Jones 
282e2d471dSAndrew Jones struct gic {
292e2d471dSAndrew Jones 	struct {
302e2d471dSAndrew Jones 		void (*send_self)(void);
312e2d471dSAndrew Jones 		void (*send_broadcast)(void);
322e2d471dSAndrew Jones 	} ipi;
332e2d471dSAndrew Jones };
342e2d471dSAndrew Jones 
352e2d471dSAndrew Jones static struct gic *gic;
36ac4a67b6SAndrew Jones static int acked[NR_CPUS], spurious[NR_CPUS];
370e0a39dfSAlexandru Elisei static int irq_sender[NR_CPUS], irq_number[NR_CPUS];
38ac4a67b6SAndrew Jones static cpumask_t ready;
39ac4a67b6SAndrew Jones 
40ac4a67b6SAndrew Jones static void nr_cpu_check(int nr)
41ac4a67b6SAndrew Jones {
42ac4a67b6SAndrew Jones 	if (nr_cpus < nr)
43ac4a67b6SAndrew Jones 		report_abort("At least %d cpus required", nr);
44ac4a67b6SAndrew Jones }
45ac4a67b6SAndrew Jones 
46ac4a67b6SAndrew Jones static void wait_on_ready(void)
47ac4a67b6SAndrew Jones {
48ac4a67b6SAndrew Jones 	cpumask_set_cpu(smp_processor_id(), &ready);
49ac4a67b6SAndrew Jones 	while (!cpumask_full(&ready))
50ac4a67b6SAndrew Jones 		cpu_relax();
51ac4a67b6SAndrew Jones }
52ac4a67b6SAndrew Jones 
53ca1b7a7bSAndrew Jones static void stats_reset(void)
54ca1b7a7bSAndrew Jones {
55ca1b7a7bSAndrew Jones 	int i;
56ca1b7a7bSAndrew Jones 
57ca1b7a7bSAndrew Jones 	for (i = 0; i < nr_cpus; ++i) {
58ca1b7a7bSAndrew Jones 		acked[i] = 0;
590e0a39dfSAlexandru Elisei 		irq_sender[i] = -1;
600e0a39dfSAlexandru Elisei 		irq_number[i] = -1;
61ca1b7a7bSAndrew Jones 	}
62ca1b7a7bSAndrew Jones }
63ca1b7a7bSAndrew Jones 
647af008b1SAlexandru Elisei static void wait_for_interrupts(cpumask_t *mask)
65ac4a67b6SAndrew Jones {
66ac4a67b6SAndrew Jones 	int nr_pass, cpu, i;
67ac4a67b6SAndrew Jones 
68ac4a67b6SAndrew Jones 	/* Wait up to 5s for all interrupts to be delivered */
697af008b1SAlexandru Elisei 	for (i = 0; i < 50; i++) {
70ac4a67b6SAndrew Jones 		mdelay(100);
71ac4a67b6SAndrew Jones 		nr_pass = 0;
72ac4a67b6SAndrew Jones 		for_each_present_cpu(cpu) {
737af008b1SAlexandru Elisei 			/*
747af008b1SAlexandru Elisei 			 * A CPU having received more than one interrupts will
757af008b1SAlexandru Elisei 			 * show up in check_acked(), and no matter how long we
767af008b1SAlexandru Elisei 			 * wait it cannot un-receive it. Consider at least one
777af008b1SAlexandru Elisei 			 * interrupt as a pass.
787af008b1SAlexandru Elisei 			 */
79ac4a67b6SAndrew Jones 			nr_pass += cpumask_test_cpu(cpu, mask) ?
807af008b1SAlexandru Elisei 				acked[cpu] >= 1 : acked[cpu] == 0;
81ca1b7a7bSAndrew Jones 		}
82ca1b7a7bSAndrew Jones 
83ac4a67b6SAndrew Jones 		if (nr_pass == nr_cpus) {
8496edb026SAndre Przywara 			if (i)
857af008b1SAlexandru Elisei 				report_info("interrupts took more than %d ms", i * 100);
867af008b1SAlexandru Elisei 			/* Wait for unexpected interrupts to fire */
877af008b1SAlexandru Elisei 			mdelay(100);
88ac4a67b6SAndrew Jones 			return;
89ac4a67b6SAndrew Jones 		}
90ac4a67b6SAndrew Jones 	}
91ac4a67b6SAndrew Jones 
927af008b1SAlexandru Elisei 	report_info("interrupts timed-out (5s)");
937af008b1SAlexandru Elisei }
947af008b1SAlexandru Elisei 
950e0a39dfSAlexandru Elisei static bool check_acked(cpumask_t *mask, int sender, int irqnum)
967af008b1SAlexandru Elisei {
977af008b1SAlexandru Elisei 	int missing = 0, extra = 0, unexpected = 0;
980e0a39dfSAlexandru Elisei 	bool has_gicv2 = (gic_version() == 2);
997af008b1SAlexandru Elisei 	bool pass = true;
1007af008b1SAlexandru Elisei 	int cpu;
1017af008b1SAlexandru Elisei 
102ac4a67b6SAndrew Jones 	for_each_present_cpu(cpu) {
103ac4a67b6SAndrew Jones 		if (cpumask_test_cpu(cpu, mask)) {
104ac4a67b6SAndrew Jones 			if (!acked[cpu])
105ac4a67b6SAndrew Jones 				++missing;
106ac4a67b6SAndrew Jones 			else if (acked[cpu] > 1)
107ac4a67b6SAndrew Jones 				++extra;
108ac4a67b6SAndrew Jones 		} else {
109ac4a67b6SAndrew Jones 			if (acked[cpu])
110ac4a67b6SAndrew Jones 				++unexpected;
111ac4a67b6SAndrew Jones 		}
1120e0a39dfSAlexandru Elisei 		if (!acked[cpu])
1130e0a39dfSAlexandru Elisei 			continue;
1147af008b1SAlexandru Elisei 		smp_rmb(); /* pairs with smp_wmb in ipi_handler */
1157af008b1SAlexandru Elisei 
1160e0a39dfSAlexandru Elisei 		if (has_gicv2 && irq_sender[cpu] != sender) {
1177af008b1SAlexandru Elisei 			report_info("cpu%d received IPI from wrong sender %d",
1180e0a39dfSAlexandru Elisei 					cpu, irq_sender[cpu]);
1197af008b1SAlexandru Elisei 			pass = false;
120ac4a67b6SAndrew Jones 		}
121ac4a67b6SAndrew Jones 
1220e0a39dfSAlexandru Elisei 		if (irq_number[cpu] != irqnum) {
1237af008b1SAlexandru Elisei 			report_info("cpu%d received wrong irq %d",
1240e0a39dfSAlexandru Elisei 					cpu, irq_number[cpu]);
1257af008b1SAlexandru Elisei 			pass = false;
1267af008b1SAlexandru Elisei 		}
1277af008b1SAlexandru Elisei 	}
1287af008b1SAlexandru Elisei 
1297af008b1SAlexandru Elisei 	if (missing || extra || unexpected) {
1307af008b1SAlexandru Elisei 		report_info("ACKS: missing=%d extra=%d unexpected=%d",
13196edb026SAndre Przywara 				missing, extra, unexpected);
1327af008b1SAlexandru Elisei 		pass = false;
1337af008b1SAlexandru Elisei 	}
1347af008b1SAlexandru Elisei 
1357af008b1SAlexandru Elisei 	return pass;
136ac4a67b6SAndrew Jones }
137ac4a67b6SAndrew Jones 
138ac4a67b6SAndrew Jones static void check_spurious(void)
139ac4a67b6SAndrew Jones {
140ac4a67b6SAndrew Jones 	int cpu;
141ac4a67b6SAndrew Jones 
142ac4a67b6SAndrew Jones 	for_each_present_cpu(cpu) {
143ac4a67b6SAndrew Jones 		if (spurious[cpu])
144ac4a67b6SAndrew Jones 			report_info("WARN: cpu%d got %d spurious interrupts",
145ac4a67b6SAndrew Jones 				cpu, spurious[cpu]);
146ac4a67b6SAndrew Jones 	}
147ac4a67b6SAndrew Jones }
148ac4a67b6SAndrew Jones 
1490e0a39dfSAlexandru Elisei static int gic_get_sender(int irqstat)
150ca1b7a7bSAndrew Jones {
1510e0a39dfSAlexandru Elisei 	if (gic_version() == 2)
1520e0a39dfSAlexandru Elisei 		return (irqstat >> 10) & 7;
1530e0a39dfSAlexandru Elisei 	return -1;
154ca1b7a7bSAndrew Jones }
155ca1b7a7bSAndrew Jones 
156ac4a67b6SAndrew Jones static void ipi_handler(struct pt_regs *regs __unused)
157ac4a67b6SAndrew Jones {
1582e2d471dSAndrew Jones 	u32 irqstat = gic_read_iar();
1592e2d471dSAndrew Jones 	u32 irqnr = gic_iar_irqnr(irqstat);
1600e0a39dfSAlexandru Elisei 	int this_cpu = smp_processor_id();
161ac4a67b6SAndrew Jones 
162ac4a67b6SAndrew Jones 	if (irqnr != GICC_INT_SPURIOUS) {
1632e2d471dSAndrew Jones 		gic_write_eoir(irqstat);
1640e0a39dfSAlexandru Elisei 		irq_sender[this_cpu] = gic_get_sender(irqstat);
1650e0a39dfSAlexandru Elisei 		irq_number[this_cpu] = irqnr;
166718d77f1SAlexandru Elisei 		smp_wmb(); /* pairs with smp_rmb in check_acked */
1670e0a39dfSAlexandru Elisei 		++acked[this_cpu];
168ac4a67b6SAndrew Jones 	} else {
1690e0a39dfSAlexandru Elisei 		++spurious[this_cpu];
170ac4a67b6SAndrew Jones 	}
171b3029e53SAlexandru Elisei 
172b3029e53SAlexandru Elisei 	/* Wait for writes to acked/spurious to complete */
173b3029e53SAlexandru Elisei 	dsb(ishst);
174ac4a67b6SAndrew Jones }
175ac4a67b6SAndrew Jones 
1760ef02cd6SEric Auger static void setup_irq(irq_handler_fn handler)
1770ef02cd6SEric Auger {
1780ef02cd6SEric Auger 	gic_enable_defaults();
1790ef02cd6SEric Auger #ifdef __arm__
1800ef02cd6SEric Auger 	install_exception_handler(EXCPTN_IRQ, handler);
1810ef02cd6SEric Auger #else
1820ef02cd6SEric Auger 	install_irq_handler(EL1H_IRQ, handler);
1830ef02cd6SEric Auger #endif
1840ef02cd6SEric Auger 	local_irq_enable();
1850ef02cd6SEric Auger }
1860ef02cd6SEric Auger 
1870ef02cd6SEric Auger #if defined(__aarch64__)
1880ef02cd6SEric Auger struct its_event {
1890ef02cd6SEric Auger 	int cpu_id;
1900ef02cd6SEric Auger 	int lpi_id;
1910ef02cd6SEric Auger };
1920ef02cd6SEric Auger 
1930ef02cd6SEric Auger struct its_stats {
1940ef02cd6SEric Auger 	struct its_event expected;
1950ef02cd6SEric Auger 	struct its_event observed;
1960ef02cd6SEric Auger };
1970ef02cd6SEric Auger 
1980ef02cd6SEric Auger static struct its_stats lpi_stats;
1990ef02cd6SEric Auger 
2000ef02cd6SEric Auger static void lpi_handler(struct pt_regs *regs __unused)
2010ef02cd6SEric Auger {
2020ef02cd6SEric Auger 	u32 irqstat = gic_read_iar();
2030ef02cd6SEric Auger 	int irqnr = gic_iar_irqnr(irqstat);
2040ef02cd6SEric Auger 
2050ef02cd6SEric Auger 	gic_write_eoir(irqstat);
2060ef02cd6SEric Auger 	assert(irqnr >= 8192);
2070ef02cd6SEric Auger 	smp_rmb(); /* pairs with wmb in lpi_stats_expect */
2080ef02cd6SEric Auger 	lpi_stats.observed.cpu_id = smp_processor_id();
2090ef02cd6SEric Auger 	lpi_stats.observed.lpi_id = irqnr;
210de582149SEric Auger 	acked[lpi_stats.observed.cpu_id]++;
2110ef02cd6SEric Auger 	smp_wmb(); /* pairs with rmb in check_lpi_stats */
2120ef02cd6SEric Auger }
2130ef02cd6SEric Auger 
2140ef02cd6SEric Auger static void lpi_stats_expect(int exp_cpu_id, int exp_lpi_id)
2150ef02cd6SEric Auger {
2160ef02cd6SEric Auger 	lpi_stats.expected.cpu_id = exp_cpu_id;
2170ef02cd6SEric Auger 	lpi_stats.expected.lpi_id = exp_lpi_id;
2180ef02cd6SEric Auger 	lpi_stats.observed.cpu_id = -1;
2190ef02cd6SEric Auger 	lpi_stats.observed.lpi_id = -1;
2200ef02cd6SEric Auger 	smp_wmb(); /* pairs with rmb in handler */
2210ef02cd6SEric Auger }
2220ef02cd6SEric Auger 
2230ef02cd6SEric Auger static void check_lpi_stats(const char *msg)
2240ef02cd6SEric Auger {
2250ef02cd6SEric Auger 	int i;
2260ef02cd6SEric Auger 
2270ef02cd6SEric Auger 	for (i = 0; i < 50; i++) {
2280ef02cd6SEric Auger 		mdelay(100);
2290ef02cd6SEric Auger 		smp_rmb(); /* pairs with wmb in lpi_handler */
2300ef02cd6SEric Auger 		if (lpi_stats.observed.cpu_id == lpi_stats.expected.cpu_id &&
2310ef02cd6SEric Auger 		    lpi_stats.observed.lpi_id == lpi_stats.expected.lpi_id) {
2320ef02cd6SEric Auger 			report(true, "%s", msg);
2330ef02cd6SEric Auger 			return;
2340ef02cd6SEric Auger 		}
2350ef02cd6SEric Auger 	}
2360ef02cd6SEric Auger 
2370ef02cd6SEric Auger 	if (lpi_stats.observed.cpu_id == -1 && lpi_stats.observed.lpi_id == -1) {
2380ef02cd6SEric Auger 		report_info("No LPI received whereas (cpuid=%d, intid=%d) "
2390ef02cd6SEric Auger 			    "was expected", lpi_stats.expected.cpu_id,
2400ef02cd6SEric Auger 			    lpi_stats.expected.lpi_id);
2410ef02cd6SEric Auger 	} else {
2420ef02cd6SEric Auger 		report_info("Unexpected LPI (cpuid=%d, intid=%d)",
2430ef02cd6SEric Auger 			    lpi_stats.observed.cpu_id,
2440ef02cd6SEric Auger 			    lpi_stats.observed.lpi_id);
2450ef02cd6SEric Auger 	}
2460ef02cd6SEric Auger 	report(false, "%s", msg);
2470ef02cd6SEric Auger }
2480ef02cd6SEric Auger 
2490ef02cd6SEric Auger static void secondary_lpi_test(void)
2500ef02cd6SEric Auger {
2510ef02cd6SEric Auger 	setup_irq(lpi_handler);
2520ef02cd6SEric Auger 	cpumask_set_cpu(smp_processor_id(), &ready);
2530ef02cd6SEric Auger 	while (1)
2540ef02cd6SEric Auger 		wfi();
2550ef02cd6SEric Auger }
256de582149SEric Auger 
257de582149SEric Auger static void check_lpi_hits(int *expected, const char *msg)
258de582149SEric Auger {
259de582149SEric Auger 	bool pass = true;
260de582149SEric Auger 	int i;
261de582149SEric Auger 
262de582149SEric Auger 	for_each_present_cpu(i) {
263de582149SEric Auger 		if (acked[i] != expected[i]) {
264de582149SEric Auger 			report_info("expected %d LPIs on PE #%d, %d observed",
265de582149SEric Auger 				    expected[i], i, acked[i]);
266de582149SEric Auger 			pass = false;
267de582149SEric Auger 			break;
268de582149SEric Auger 		}
269de582149SEric Auger 	}
270de582149SEric Auger 	report(pass, "%s", msg);
271de582149SEric Auger }
2720ef02cd6SEric Auger #endif
2730ef02cd6SEric Auger 
2742e2d471dSAndrew Jones static void gicv2_ipi_send_self(void)
2752e2d471dSAndrew Jones {
27610e3685fSAlexandru Elisei 	/*
27710e3685fSAlexandru Elisei 	 * The wmb() in writel and rmb() when acknowledging the interrupt are
27810e3685fSAlexandru Elisei 	 * sufficient for ensuring that writes that happen in program order
27910e3685fSAlexandru Elisei 	 * before the interrupt are observed in the interrupt handler after
28010e3685fSAlexandru Elisei 	 * acknowledging the interrupt.
28110e3685fSAlexandru Elisei 	 */
282ca1b7a7bSAndrew Jones 	writel(2 << 24 | IPI_IRQ, gicv2_dist_base() + GICD_SGIR);
2832e2d471dSAndrew Jones }
2842e2d471dSAndrew Jones 
2852e2d471dSAndrew Jones static void gicv2_ipi_send_broadcast(void)
2862e2d471dSAndrew Jones {
28710e3685fSAlexandru Elisei 	/* No barriers are needed, same situation as gicv2_ipi_send_self() */
288ca1b7a7bSAndrew Jones 	writel(1 << 24 | IPI_IRQ, gicv2_dist_base() + GICD_SGIR);
2892e2d471dSAndrew Jones }
2902e2d471dSAndrew Jones 
2912e2d471dSAndrew Jones static void gicv3_ipi_send_self(void)
2922e2d471dSAndrew Jones {
293ca1b7a7bSAndrew Jones 	gic_ipi_send_single(IPI_IRQ, smp_processor_id());
2942e2d471dSAndrew Jones }
2952e2d471dSAndrew Jones 
2962e2d471dSAndrew Jones static void gicv3_ipi_send_broadcast(void)
2972e2d471dSAndrew Jones {
2980c03f4b1SAlexandru Elisei 	/*
2990c03f4b1SAlexandru Elisei 	 * Ensure stores to Normal memory are visible to other CPUs before
3000c03f4b1SAlexandru Elisei 	 * sending the IPI
3010c03f4b1SAlexandru Elisei 	 */
3020c03f4b1SAlexandru Elisei 	wmb();
303ca1b7a7bSAndrew Jones 	gicv3_write_sgi1r(1ULL << 40 | IPI_IRQ << 24);
3042e2d471dSAndrew Jones 	isb();
3052e2d471dSAndrew Jones }
3062e2d471dSAndrew Jones 
307ac4a67b6SAndrew Jones static void ipi_test_self(void)
308ac4a67b6SAndrew Jones {
3090e0a39dfSAlexandru Elisei 	int this_cpu = smp_processor_id();
310ac4a67b6SAndrew Jones 	cpumask_t mask;
311ac4a67b6SAndrew Jones 
312ac4a67b6SAndrew Jones 	report_prefix_push("self");
313ca1b7a7bSAndrew Jones 	stats_reset();
314ac4a67b6SAndrew Jones 	cpumask_clear(&mask);
3150e0a39dfSAlexandru Elisei 	cpumask_set_cpu(this_cpu, &mask);
3162e2d471dSAndrew Jones 	gic->ipi.send_self();
3177af008b1SAlexandru Elisei 	wait_for_interrupts(&mask);
3180e0a39dfSAlexandru Elisei 	report(check_acked(&mask, this_cpu, IPI_IRQ), "Interrupts received");
319ac4a67b6SAndrew Jones 	report_prefix_pop();
320ac4a67b6SAndrew Jones }
321ac4a67b6SAndrew Jones 
322ac4a67b6SAndrew Jones static void ipi_test_smp(void)
323ac4a67b6SAndrew Jones {
3240e0a39dfSAlexandru Elisei 	int this_cpu = smp_processor_id();
325ac4a67b6SAndrew Jones 	cpumask_t mask;
3262e2d471dSAndrew Jones 	int i;
327ac4a67b6SAndrew Jones 
328ac4a67b6SAndrew Jones 	report_prefix_push("target-list");
329ca1b7a7bSAndrew Jones 	stats_reset();
3302e2d471dSAndrew Jones 	cpumask_copy(&mask, &cpu_present_mask);
3310e0a39dfSAlexandru Elisei 	for (i = this_cpu & 1; i < nr_cpus; i += 2)
3322e2d471dSAndrew Jones 		cpumask_clear_cpu(i, &mask);
333ca1b7a7bSAndrew Jones 	gic_ipi_send_mask(IPI_IRQ, &mask);
3347af008b1SAlexandru Elisei 	wait_for_interrupts(&mask);
3350e0a39dfSAlexandru Elisei 	report(check_acked(&mask, this_cpu, IPI_IRQ), "Interrupts received");
336ac4a67b6SAndrew Jones 	report_prefix_pop();
337ac4a67b6SAndrew Jones 
338ac4a67b6SAndrew Jones 	report_prefix_push("broadcast");
339ca1b7a7bSAndrew Jones 	stats_reset();
340ac4a67b6SAndrew Jones 	cpumask_copy(&mask, &cpu_present_mask);
3410e0a39dfSAlexandru Elisei 	cpumask_clear_cpu(this_cpu, &mask);
3422e2d471dSAndrew Jones 	gic->ipi.send_broadcast();
3437af008b1SAlexandru Elisei 	wait_for_interrupts(&mask);
3440e0a39dfSAlexandru Elisei 	report(check_acked(&mask, this_cpu, IPI_IRQ), "Interrupts received");
345ac4a67b6SAndrew Jones 	report_prefix_pop();
346ac4a67b6SAndrew Jones }
347ac4a67b6SAndrew Jones 
348ca1b7a7bSAndrew Jones static void ipi_send(void)
349ca1b7a7bSAndrew Jones {
35025f66327SEric Auger 	setup_irq(ipi_handler);
351ca1b7a7bSAndrew Jones 	wait_on_ready();
352ca1b7a7bSAndrew Jones 	ipi_test_self();
353ca1b7a7bSAndrew Jones 	ipi_test_smp();
354ca1b7a7bSAndrew Jones 	check_spurious();
355ca1b7a7bSAndrew Jones 	exit(report_summary());
356ca1b7a7bSAndrew Jones }
357ca1b7a7bSAndrew Jones 
358ac4a67b6SAndrew Jones static void ipi_recv(void)
359ac4a67b6SAndrew Jones {
36025f66327SEric Auger 	setup_irq(ipi_handler);
361ac4a67b6SAndrew Jones 	cpumask_set_cpu(smp_processor_id(), &ready);
362ac4a67b6SAndrew Jones 	while (1)
363ac4a67b6SAndrew Jones 		wfi();
364ac4a67b6SAndrew Jones }
365ac4a67b6SAndrew Jones 
36600b34f56SAndrew Jones static void ipi_test(void *data __unused)
367bfd500b4SAndrew Jones {
368bfd500b4SAndrew Jones 	if (smp_processor_id() == IPI_SENDER)
369bfd500b4SAndrew Jones 		ipi_send();
370bfd500b4SAndrew Jones 	else
371bfd500b4SAndrew Jones 		ipi_recv();
372bfd500b4SAndrew Jones }
373bfd500b4SAndrew Jones 
3742e2d471dSAndrew Jones static struct gic gicv2 = {
3752e2d471dSAndrew Jones 	.ipi = {
3762e2d471dSAndrew Jones 		.send_self = gicv2_ipi_send_self,
3772e2d471dSAndrew Jones 		.send_broadcast = gicv2_ipi_send_broadcast,
3782e2d471dSAndrew Jones 	},
3792e2d471dSAndrew Jones };
3802e2d471dSAndrew Jones 
3812e2d471dSAndrew Jones static struct gic gicv3 = {
3822e2d471dSAndrew Jones 	.ipi = {
3832e2d471dSAndrew Jones 		.send_self = gicv3_ipi_send_self,
3842e2d471dSAndrew Jones 		.send_broadcast = gicv3_ipi_send_broadcast,
3852e2d471dSAndrew Jones 	},
3862e2d471dSAndrew Jones };
3872e2d471dSAndrew Jones 
388680beae9SAlexandru Elisei /* Runs on the same CPU as the sender, no need for memory synchronization */
389c152d8bcSChristoffer Dall static void ipi_clear_active_handler(struct pt_regs *regs __unused)
390c152d8bcSChristoffer Dall {
391c152d8bcSChristoffer Dall 	u32 irqstat = gic_read_iar();
392c152d8bcSChristoffer Dall 	u32 irqnr = gic_iar_irqnr(irqstat);
3930e0a39dfSAlexandru Elisei 	int this_cpu = smp_processor_id();
394c152d8bcSChristoffer Dall 
395c152d8bcSChristoffer Dall 	if (irqnr != GICC_INT_SPURIOUS) {
396c152d8bcSChristoffer Dall 		void *base;
397c152d8bcSChristoffer Dall 		u32 val = 1 << IPI_IRQ;
398c152d8bcSChristoffer Dall 
399c152d8bcSChristoffer Dall 		if (gic_version() == 2)
400c152d8bcSChristoffer Dall 			base = gicv2_dist_base();
401c152d8bcSChristoffer Dall 		else
4026d4d7c4bSAndrew Jones 			base = gicv3_sgi_base();
403c152d8bcSChristoffer Dall 
404c152d8bcSChristoffer Dall 		writel(val, base + GICD_ICACTIVER);
405c152d8bcSChristoffer Dall 
4060e0a39dfSAlexandru Elisei 		irq_sender[this_cpu] = gic_get_sender(irqstat);
4070e0a39dfSAlexandru Elisei 		irq_number[this_cpu] = irqnr;
4080e0a39dfSAlexandru Elisei 		++acked[this_cpu];
409c152d8bcSChristoffer Dall 	} else {
4100e0a39dfSAlexandru Elisei 		++spurious[this_cpu];
411c152d8bcSChristoffer Dall 	}
412c152d8bcSChristoffer Dall }
413c152d8bcSChristoffer Dall 
414c152d8bcSChristoffer Dall static void run_active_clear_test(void)
415c152d8bcSChristoffer Dall {
416c152d8bcSChristoffer Dall 	report_prefix_push("active");
41725f66327SEric Auger 	setup_irq(ipi_clear_active_handler);
418c152d8bcSChristoffer Dall 	ipi_test_self();
41964366016SAlexandru Elisei 	check_spurious();
420c152d8bcSChristoffer Dall 	report_prefix_pop();
421c152d8bcSChristoffer Dall }
422c152d8bcSChristoffer Dall 
42378ad7e95SAndre Przywara static bool test_ro_pattern_32(void *address, u32 pattern, u32 orig)
42478ad7e95SAndre Przywara {
42578ad7e95SAndre Przywara 	u32 reg;
42678ad7e95SAndre Przywara 
42778ad7e95SAndre Przywara 	writel(pattern, address);
42878ad7e95SAndre Przywara 	reg = readl(address);
42978ad7e95SAndre Przywara 
43078ad7e95SAndre Przywara 	if (reg != orig)
43178ad7e95SAndre Przywara 		writel(orig, address);
43278ad7e95SAndre Przywara 
43378ad7e95SAndre Przywara 	return reg == orig;
43478ad7e95SAndre Przywara }
43578ad7e95SAndre Przywara 
43678ad7e95SAndre Przywara static bool test_readonly_32(void *address, bool razwi)
43778ad7e95SAndre Przywara {
43878ad7e95SAndre Przywara 	u32 orig, pattern;
43978ad7e95SAndre Przywara 
44078ad7e95SAndre Przywara 	orig = readl(address);
44178ad7e95SAndre Przywara 	if (razwi && orig)
44278ad7e95SAndre Przywara 		return false;
44378ad7e95SAndre Przywara 
44478ad7e95SAndre Przywara 	pattern = 0xffffffff;
44578ad7e95SAndre Przywara 	if (orig != pattern) {
44678ad7e95SAndre Przywara 		if (!test_ro_pattern_32(address, pattern, orig))
44778ad7e95SAndre Przywara 			return false;
44878ad7e95SAndre Przywara 	}
44978ad7e95SAndre Przywara 
45078ad7e95SAndre Przywara 	pattern = 0xa5a55a5a;
45178ad7e95SAndre Przywara 	if (orig != pattern) {
45278ad7e95SAndre Przywara 		if (!test_ro_pattern_32(address, pattern, orig))
45378ad7e95SAndre Przywara 			return false;
45478ad7e95SAndre Przywara 	}
45578ad7e95SAndre Przywara 
45678ad7e95SAndre Przywara 	pattern = 0;
45778ad7e95SAndre Przywara 	if (orig != pattern) {
45878ad7e95SAndre Przywara 		if (!test_ro_pattern_32(address, pattern, orig))
45978ad7e95SAndre Przywara 			return false;
46078ad7e95SAndre Przywara 	}
46178ad7e95SAndre Przywara 
46278ad7e95SAndre Przywara 	return true;
46378ad7e95SAndre Przywara }
46478ad7e95SAndre Przywara 
46578ad7e95SAndre Przywara static void test_typer_v2(uint32_t reg)
46678ad7e95SAndre Przywara {
46778ad7e95SAndre Przywara 	int nr_gic_cpus = ((reg >> 5) & 0x7) + 1;
46878ad7e95SAndre Przywara 
4698e0a4f41SAndre Przywara 	report_info("nr_cpus=%d", nr_cpus);
470a299895bSThomas Huth 	report(nr_cpus == nr_gic_cpus, "all CPUs have interrupts");
47178ad7e95SAndre Przywara }
47278ad7e95SAndre Przywara 
473ff31a1c4SAndre Przywara #define BYTE(reg32, byte) (((reg32) >> ((byte) * 8)) & 0xff)
474ff31a1c4SAndre Przywara #define REPLACE_BYTE(reg32, byte, new) (((reg32) & ~(0xff << ((byte) * 8))) |\
475ff31a1c4SAndre Przywara 					((new) << ((byte) * 8)))
476ff31a1c4SAndre Przywara 
477ff31a1c4SAndre Przywara /*
478ff31a1c4SAndre Przywara  * Some registers are byte accessible, do a byte-wide read and write of known
479ff31a1c4SAndre Przywara  * content to check for this.
480ff31a1c4SAndre Przywara  * Apply a @mask to cater for special register properties.
481ff31a1c4SAndre Przywara  * @pattern contains the value already in the register.
482ff31a1c4SAndre Przywara  */
483ff31a1c4SAndre Przywara static void test_byte_access(void *base_addr, u32 pattern, u32 mask)
484ff31a1c4SAndre Przywara {
485ff31a1c4SAndre Przywara 	u32 reg = readb(base_addr + 1);
4868e0a4f41SAndre Przywara 	bool res;
487ff31a1c4SAndre Przywara 
4888e0a4f41SAndre Przywara 	res = (reg == (BYTE(pattern, 1) & (mask >> 8)));
489a299895bSThomas Huth 	report(res, "byte reads successful");
4908e0a4f41SAndre Przywara 	if (!res)
49146ca10f4SAlexandru Elisei 		report_info("byte 1 of 0x%08"PRIx32" => 0x%02"PRIx32, pattern & mask, reg);
492ff31a1c4SAndre Przywara 
493ff31a1c4SAndre Przywara 	pattern = REPLACE_BYTE(pattern, 2, 0x1f);
494ff31a1c4SAndre Przywara 	writeb(BYTE(pattern, 2), base_addr + 2);
495ff31a1c4SAndre Przywara 	reg = readl(base_addr);
4968e0a4f41SAndre Przywara 	res = (reg == (pattern & mask));
497a299895bSThomas Huth 	report(res, "byte writes successful");
4988e0a4f41SAndre Przywara 	if (!res)
49946ca10f4SAlexandru Elisei 		report_info("writing 0x%02"PRIx32" into bytes 2 => 0x%08"PRIx32,
5008e0a4f41SAndre Przywara 			    BYTE(pattern, 2), reg);
501ff31a1c4SAndre Przywara }
502ff31a1c4SAndre Przywara 
503ff31a1c4SAndre Przywara static void test_priorities(int nr_irqs, void *priptr)
504ff31a1c4SAndre Przywara {
505ff31a1c4SAndre Przywara 	u32 orig_prio, reg, pri_bits;
506ff31a1c4SAndre Przywara 	u32 pri_mask, pattern;
507ff31a1c4SAndre Przywara 	void *first_spi = priptr + GIC_FIRST_SPI;
508ff31a1c4SAndre Przywara 
509ff31a1c4SAndre Przywara 	orig_prio = readl(first_spi);
510ff31a1c4SAndre Przywara 	report_prefix_push("IPRIORITYR");
511ff31a1c4SAndre Przywara 
512ff31a1c4SAndre Przywara 	/*
513ff31a1c4SAndre Przywara 	 * Determine implemented number of priority bits by writing all 1's
514ff31a1c4SAndre Przywara 	 * and checking the number of cleared bits in the value read back.
515ff31a1c4SAndre Przywara 	 */
516ff31a1c4SAndre Przywara 	writel(0xffffffff, first_spi);
517ff31a1c4SAndre Przywara 	pri_mask = readl(first_spi);
518ff31a1c4SAndre Przywara 
519ff31a1c4SAndre Przywara 	reg = ~pri_mask;
520a299895bSThomas Huth 	report((((reg >> 16) == (reg & 0xffff)) &&
521a299895bSThomas Huth 	        ((reg & 0xff) == ((reg >> 8) & 0xff))),
522a299895bSThomas Huth 	       "consistent priority masking");
52346ca10f4SAlexandru Elisei 	report_info("priority mask is 0x%08"PRIx32, pri_mask);
524ff31a1c4SAndre Przywara 
525ff31a1c4SAndre Przywara 	reg = reg & 0xff;
526ff31a1c4SAndre Przywara 	for (pri_bits = 8; reg & 1; reg >>= 1, pri_bits--)
527ff31a1c4SAndre Przywara 		;
528a299895bSThomas Huth 	report(pri_bits >= 4, "implements at least 4 priority bits");
52946ca10f4SAlexandru Elisei 	report_info("%"PRIu32" priority bits implemented", pri_bits);
530ff31a1c4SAndre Przywara 
531ff31a1c4SAndre Przywara 	pattern = 0;
532ff31a1c4SAndre Przywara 	writel(pattern, first_spi);
533a299895bSThomas Huth 	report(readl(first_spi) == pattern, "clearing priorities");
534ff31a1c4SAndre Przywara 
535ff31a1c4SAndre Przywara 	/* setting all priorities to their max valus was tested above */
536ff31a1c4SAndre Przywara 
537a299895bSThomas Huth 	report(test_readonly_32(priptr + nr_irqs, true),
538a299895bSThomas Huth 	       "accesses beyond limit RAZ/WI");
539ff31a1c4SAndre Przywara 
540ff31a1c4SAndre Przywara 	writel(pattern, priptr + nr_irqs - 4);
541a299895bSThomas Huth 	report(readl(priptr + nr_irqs - 4) == (pattern & pri_mask),
542a299895bSThomas Huth 	       "accessing last SPIs");
543ff31a1c4SAndre Przywara 
544ff31a1c4SAndre Przywara 	pattern = 0xff7fbf3f;
545ff31a1c4SAndre Przywara 	writel(pattern, first_spi);
546a299895bSThomas Huth 	report(readl(first_spi) == (pattern & pri_mask),
547a299895bSThomas Huth 	       "priorities are preserved");
548ff31a1c4SAndre Przywara 
549ff31a1c4SAndre Przywara 	/* The PRIORITY registers are byte accessible. */
550ff31a1c4SAndre Przywara 	test_byte_access(first_spi, pattern, pri_mask);
551ff31a1c4SAndre Przywara 
552ff31a1c4SAndre Przywara 	report_prefix_pop();
553ff31a1c4SAndre Przywara 	writel(orig_prio, first_spi);
554ff31a1c4SAndre Przywara }
555ff31a1c4SAndre Przywara 
556fe572a5eSAndre Przywara /* GICD_ITARGETSR is only used by GICv2. */
557fe572a5eSAndre Przywara static void test_targets(int nr_irqs)
558fe572a5eSAndre Przywara {
559fe572a5eSAndre Przywara 	void *targetsptr = gicv2_dist_base() + GICD_ITARGETSR;
560fe572a5eSAndre Przywara 	u32 orig_targets;
561fe572a5eSAndre Przywara 	u32 cpu_mask;
562fe572a5eSAndre Przywara 	u32 pattern, reg;
563fe572a5eSAndre Przywara 
564fe572a5eSAndre Przywara 	orig_targets = readl(targetsptr + GIC_FIRST_SPI);
565fe572a5eSAndre Przywara 	report_prefix_push("ITARGETSR");
566fe572a5eSAndre Przywara 
567fe572a5eSAndre Przywara 	cpu_mask = (1 << nr_cpus) - 1;
568fe572a5eSAndre Przywara 	cpu_mask |= cpu_mask << 8;
569fe572a5eSAndre Przywara 	cpu_mask |= cpu_mask << 16;
570fe572a5eSAndre Przywara 
571fe572a5eSAndre Przywara 	/* Check that bits for non implemented CPUs are RAZ/WI. */
572fe572a5eSAndre Przywara 	if (nr_cpus < 8) {
573fe572a5eSAndre Przywara 		writel(0xffffffff, targetsptr + GIC_FIRST_SPI);
574a299895bSThomas Huth 		report(!(readl(targetsptr + GIC_FIRST_SPI) & ~cpu_mask),
575a299895bSThomas Huth 		       "bits for non-existent CPUs masked");
5768e0a4f41SAndre Przywara 		report_info("%d non-existent CPUs", 8 - nr_cpus);
577fe572a5eSAndre Przywara 	} else {
578fe572a5eSAndre Przywara 		report_skip("CPU masking (all CPUs implemented)");
579fe572a5eSAndre Przywara 	}
580fe572a5eSAndre Przywara 
581a299895bSThomas Huth 	report(test_readonly_32(targetsptr + nr_irqs, true),
582a299895bSThomas Huth 	       "accesses beyond limit RAZ/WI");
583fe572a5eSAndre Przywara 
584fe572a5eSAndre Przywara 	pattern = 0x0103020f;
585fe572a5eSAndre Przywara 	writel(pattern, targetsptr + GIC_FIRST_SPI);
586fe572a5eSAndre Przywara 	reg = readl(targetsptr + GIC_FIRST_SPI);
587a299895bSThomas Huth 	report(reg == (pattern & cpu_mask), "register content preserved");
5888e0a4f41SAndre Przywara 	if (reg != (pattern & cpu_mask))
58946ca10f4SAlexandru Elisei 		report_info("writing %08"PRIx32" reads back as %08"PRIx32,
5908e0a4f41SAndre Przywara 			    pattern & cpu_mask, reg);
591fe572a5eSAndre Przywara 
592fe572a5eSAndre Przywara 	/* The TARGETS registers are byte accessible. */
593fe572a5eSAndre Przywara 	test_byte_access(targetsptr + GIC_FIRST_SPI, pattern, cpu_mask);
594fe572a5eSAndre Przywara 
595fe572a5eSAndre Przywara 	writel(orig_targets, targetsptr + GIC_FIRST_SPI);
596da5b8576SAndre Przywara 
597da5b8576SAndre Przywara 	report_prefix_pop();
598fe572a5eSAndre Przywara }
599fe572a5eSAndre Przywara 
60078ad7e95SAndre Przywara static void gic_test_mmio(void)
60178ad7e95SAndre Przywara {
60278ad7e95SAndre Przywara 	u32 reg;
60378ad7e95SAndre Przywara 	int nr_irqs;
60478ad7e95SAndre Przywara 	void *gic_dist_base, *idreg;
60578ad7e95SAndre Przywara 
60678ad7e95SAndre Przywara 	switch(gic_version()) {
60778ad7e95SAndre Przywara 	case 0x2:
60878ad7e95SAndre Przywara 		gic_dist_base = gicv2_dist_base();
60978ad7e95SAndre Przywara 		idreg = gic_dist_base + GICD_ICPIDR2;
61078ad7e95SAndre Przywara 		break;
61178ad7e95SAndre Przywara 	case 0x3:
61278ad7e95SAndre Przywara 		report_abort("GICv3 MMIO tests NYI");
61378ad7e95SAndre Przywara 	default:
61478ad7e95SAndre Przywara 		report_abort("GIC version %d not supported", gic_version());
61578ad7e95SAndre Przywara 	}
61678ad7e95SAndre Przywara 
61778ad7e95SAndre Przywara 	reg = readl(gic_dist_base + GICD_TYPER);
61878ad7e95SAndre Przywara 	nr_irqs = GICD_TYPER_IRQS(reg);
61978ad7e95SAndre Przywara 	report_info("number of implemented SPIs: %d", nr_irqs - GIC_FIRST_SPI);
62078ad7e95SAndre Przywara 
62178ad7e95SAndre Przywara 	test_typer_v2(reg);
62278ad7e95SAndre Przywara 
62346ca10f4SAlexandru Elisei 	report_info("IIDR: 0x%08"PRIx32, readl(gic_dist_base + GICD_IIDR));
62478ad7e95SAndre Przywara 
625a299895bSThomas Huth 	report(test_readonly_32(gic_dist_base + GICD_TYPER, false),
626a299895bSThomas Huth                "GICD_TYPER is read-only");
627a299895bSThomas Huth 	report(test_readonly_32(gic_dist_base + GICD_IIDR, false),
628a299895bSThomas Huth                "GICD_IIDR is read-only");
62978ad7e95SAndre Przywara 
63078ad7e95SAndre Przywara 	reg = readl(idreg);
631a299895bSThomas Huth 	report(test_readonly_32(idreg, false), "ICPIDR2 is read-only");
63246ca10f4SAlexandru Elisei 	report_info("value of ICPIDR2: 0x%08"PRIx32, reg);
633ff31a1c4SAndre Przywara 
634ff31a1c4SAndre Przywara 	test_priorities(nr_irqs, gic_dist_base + GICD_IPRIORITYR);
635fe572a5eSAndre Przywara 
636fe572a5eSAndre Przywara 	if (gic_version() == 2)
637fe572a5eSAndre Przywara 		test_targets(nr_irqs);
63878ad7e95SAndre Przywara }
63978ad7e95SAndre Przywara 
640ba74b106SEric Auger #if defined(__arm__)
641ba74b106SEric Auger 
642ba74b106SEric Auger static void test_its_introspection(void) {}
6430ef02cd6SEric Auger static void test_its_trigger(void) {}
64464260a5fSEric Auger static void test_its_migration(void) {}
645de582149SEric Auger static void test_its_pending_migration(void) {}
646de582149SEric Auger static void test_migrate_unmapped_collection(void) {}
647ba74b106SEric Auger 
648ba74b106SEric Auger #else /* __aarch64__ */
649ba74b106SEric Auger 
650ba74b106SEric Auger static void test_its_introspection(void)
651ba74b106SEric Auger {
652ba74b106SEric Auger 	struct its_baser *dev_baser = &its_data.device_baser;
653ba74b106SEric Auger 	struct its_baser *coll_baser = &its_data.coll_baser;
654ba74b106SEric Auger 	struct its_typer *typer = &its_data.typer;
655ba74b106SEric Auger 
656ba74b106SEric Auger 	if (!gicv3_its_base()) {
657ba74b106SEric Auger 		report_skip("No ITS, skip ...");
658ba74b106SEric Auger 		return;
659ba74b106SEric Auger 	}
660ba74b106SEric Auger 
661ba74b106SEric Auger 	/* IIDR */
662ba74b106SEric Auger 	report(test_readonly_32(gicv3_its_base() + GITS_IIDR, false),
663ba74b106SEric Auger 	       "GITS_IIDR is read-only"),
664ba74b106SEric Auger 
665ba74b106SEric Auger 	/* TYPER */
666ba74b106SEric Auger 	report(test_readonly_32(gicv3_its_base() + GITS_TYPER, false),
667ba74b106SEric Auger 	       "GITS_TYPER is read-only");
668ba74b106SEric Auger 
669ba74b106SEric Auger 	report(typer->phys_lpi, "ITS supports physical LPIs");
670ba74b106SEric Auger 	report_info("vLPI support: %s", typer->virt_lpi ? "yes" : "no");
671ba74b106SEric Auger 	report_info("ITT entry size = 0x%x", typer->ite_size);
672ba74b106SEric Auger 	report_info("Bit Count: EventID=%d DeviceId=%d CollId=%d",
673ba74b106SEric Auger 		    typer->eventid_bits, typer->deviceid_bits,
674ba74b106SEric Auger 		    typer->collid_bits);
675ba74b106SEric Auger 	report(typer->eventid_bits && typer->deviceid_bits &&
676ba74b106SEric Auger 	       typer->collid_bits, "ID spaces");
677ba74b106SEric Auger 	report_info("Target address format %s",
678ba74b106SEric Auger 			typer->pta ? "Redist base address" : "PE #");
679ba74b106SEric Auger 
680ba74b106SEric Auger 	report(dev_baser && coll_baser, "detect device and collection BASER");
681ba74b106SEric Auger 	report_info("device table entry_size = 0x%x", dev_baser->esz);
682ba74b106SEric Auger 	report_info("collection table entry_size = 0x%x", coll_baser->esz);
683ba74b106SEric Auger }
684ba74b106SEric Auger 
6850ef02cd6SEric Auger static int its_prerequisites(int nb_cpus)
6860ef02cd6SEric Auger {
6870ef02cd6SEric Auger 	int cpu;
6880ef02cd6SEric Auger 
6890ef02cd6SEric Auger 	if (!gicv3_its_base()) {
6900ef02cd6SEric Auger 		report_skip("No ITS, skip ...");
6910ef02cd6SEric Auger 		return -1;
6920ef02cd6SEric Auger 	}
6930ef02cd6SEric Auger 
6940ef02cd6SEric Auger 	if (nr_cpus < nb_cpus) {
6950ef02cd6SEric Auger 		report_skip("Test requires at least %d vcpus", nb_cpus);
6960ef02cd6SEric Auger 		return -1;
6970ef02cd6SEric Auger 	}
6980ef02cd6SEric Auger 
6990ef02cd6SEric Auger 	stats_reset();
7000ef02cd6SEric Auger 
7010ef02cd6SEric Auger 	setup_irq(lpi_handler);
7020ef02cd6SEric Auger 
7030ef02cd6SEric Auger 	for_each_present_cpu(cpu) {
7040ef02cd6SEric Auger 		if (cpu == 0)
7050ef02cd6SEric Auger 			continue;
7060ef02cd6SEric Auger 		smp_boot_secondary(cpu, secondary_lpi_test);
7070ef02cd6SEric Auger 	}
7080ef02cd6SEric Auger 	wait_on_ready();
7090ef02cd6SEric Auger 
7100ef02cd6SEric Auger 	its_enable_defaults();
7110ef02cd6SEric Auger 
7120ef02cd6SEric Auger 	return 0;
7130ef02cd6SEric Auger }
7140ef02cd6SEric Auger 
71564260a5fSEric Auger /*
71664260a5fSEric Auger  * Setup the configuration for those mappings:
71764260a5fSEric Auger  * dev_id=2 event=20 -> vcpu 3, intid=8195
71864260a5fSEric Auger  * dev_id=7 event=255 -> vcpu 2, intid=8196
71964260a5fSEric Auger  * LPIs ready to hit
72064260a5fSEric Auger  */
72164260a5fSEric Auger static int its_setup1(void)
7220ef02cd6SEric Auger {
7230ef02cd6SEric Auger 	struct its_collection *col3, *col2;
7240ef02cd6SEric Auger 	struct its_device *dev2, *dev7;
7250ef02cd6SEric Auger 
7260ef02cd6SEric Auger 	if (its_prerequisites(4))
72764260a5fSEric Auger 		return -1;
7280ef02cd6SEric Auger 
7290ef02cd6SEric Auger 	dev2 = its_create_device(2 /* dev id */, 8 /* nb_ites */);
7300ef02cd6SEric Auger 	dev7 = its_create_device(7 /* dev id */, 8 /* nb_ites */);
7310ef02cd6SEric Auger 
7320ef02cd6SEric Auger 	col3 = its_create_collection(3 /* col id */, 3/* target PE */);
7330ef02cd6SEric Auger 	col2 = its_create_collection(2 /* col id */, 2/* target PE */);
7340ef02cd6SEric Auger 
7350ef02cd6SEric Auger 	gicv3_lpi_set_config(8195, LPI_PROP_DEFAULT);
7360ef02cd6SEric Auger 	gicv3_lpi_set_config(8196, LPI_PROP_DEFAULT);
7370ef02cd6SEric Auger 
7380ef02cd6SEric Auger 	/*
7390ef02cd6SEric Auger 	 * dev=2, eventid=20  -> lpi= 8195, col=3
7400ef02cd6SEric Auger 	 * dev=7, eventid=255 -> lpi= 8196, col=2
7410ef02cd6SEric Auger 	 */
7420ef02cd6SEric Auger 	its_send_mapd(dev2, true);
7430ef02cd6SEric Auger 	its_send_mapd(dev7, true);
7440ef02cd6SEric Auger 
7450ef02cd6SEric Auger 	its_send_mapc(col3, true);
7460ef02cd6SEric Auger 	its_send_mapc(col2, true);
7470ef02cd6SEric Auger 
7480ef02cd6SEric Auger 	its_send_invall(col2);
7490ef02cd6SEric Auger 	its_send_invall(col3);
7500ef02cd6SEric Auger 
7510ef02cd6SEric Auger 	its_send_mapti(dev2, 8195 /* lpi id */, 20 /* event id */, col3);
7520ef02cd6SEric Auger 	its_send_mapti(dev7, 8196 /* lpi id */, 255 /* event id */, col2);
75364260a5fSEric Auger 	return 0;
75464260a5fSEric Auger }
75564260a5fSEric Auger 
75664260a5fSEric Auger static void test_its_trigger(void)
75764260a5fSEric Auger {
75864260a5fSEric Auger 	struct its_collection *col3;
75964260a5fSEric Auger 	struct its_device *dev2, *dev7;
76064260a5fSEric Auger 
76164260a5fSEric Auger 	if (its_setup1())
76264260a5fSEric Auger 		return;
76364260a5fSEric Auger 
76464260a5fSEric Auger 	col3 = its_get_collection(3);
76564260a5fSEric Auger 	dev2 = its_get_device(2);
76664260a5fSEric Auger 	dev7 = its_get_device(7);
76764260a5fSEric Auger 
76864260a5fSEric Auger 	report_prefix_push("int");
7690ef02cd6SEric Auger 
7700ef02cd6SEric Auger 	lpi_stats_expect(3, 8195);
7710ef02cd6SEric Auger 	its_send_int(dev2, 20);
7720ef02cd6SEric Auger 	check_lpi_stats("dev=2, eventid=20  -> lpi= 8195, col=3");
7730ef02cd6SEric Auger 
7740ef02cd6SEric Auger 	lpi_stats_expect(2, 8196);
7750ef02cd6SEric Auger 	its_send_int(dev7, 255);
7760ef02cd6SEric Auger 	check_lpi_stats("dev=7, eventid=255 -> lpi= 8196, col=2");
7770ef02cd6SEric Auger 
7780ef02cd6SEric Auger 	report_prefix_pop();
7790ef02cd6SEric Auger 
7800ef02cd6SEric Auger 	report_prefix_push("inv/invall");
7810ef02cd6SEric Auger 
7820ef02cd6SEric Auger 	/*
7830ef02cd6SEric Auger 	 * disable 8195, check dev2/eventid=20 does not trigger the
7840ef02cd6SEric Auger 	 * corresponding LPI
7850ef02cd6SEric Auger 	 */
7860ef02cd6SEric Auger 	gicv3_lpi_set_config(8195, LPI_PROP_DEFAULT & ~LPI_PROP_ENABLED);
7870ef02cd6SEric Auger 	its_send_inv(dev2, 20);
7880ef02cd6SEric Auger 
7890ef02cd6SEric Auger 	lpi_stats_expect(-1, -1);
7900ef02cd6SEric Auger 	its_send_int(dev2, 20);
7910ef02cd6SEric Auger 	check_lpi_stats("dev2/eventid=20 does not trigger any LPI");
7920ef02cd6SEric Auger 
7930ef02cd6SEric Auger 	/*
7940ef02cd6SEric Auger 	 * re-enable the LPI but willingly do not call invall
7950ef02cd6SEric Auger 	 * so the change in config is not taken into account.
7960ef02cd6SEric Auger 	 * The LPI should not hit
7970ef02cd6SEric Auger 	 */
7980ef02cd6SEric Auger 	gicv3_lpi_set_config(8195, LPI_PROP_DEFAULT);
7990ef02cd6SEric Auger 	lpi_stats_expect(-1, -1);
8000ef02cd6SEric Auger 	its_send_int(dev2, 20);
8010ef02cd6SEric Auger 	check_lpi_stats("dev2/eventid=20 still does not trigger any LPI");
8020ef02cd6SEric Auger 
8030ef02cd6SEric Auger 	/* Now call the invall and check the LPI hits */
8040ef02cd6SEric Auger 	its_send_invall(col3);
8050ef02cd6SEric Auger 	lpi_stats_expect(3, 8195);
806*ae7dac4eSAlexandru Elisei 	check_lpi_stats("dev2/eventid=20 pending LPI is received");
807*ae7dac4eSAlexandru Elisei 
808*ae7dac4eSAlexandru Elisei 	lpi_stats_expect(3, 8195);
8090ef02cd6SEric Auger 	its_send_int(dev2, 20);
8100ef02cd6SEric Auger 	check_lpi_stats("dev2/eventid=20 now triggers an LPI");
8110ef02cd6SEric Auger 
8120ef02cd6SEric Auger 	report_prefix_pop();
8130ef02cd6SEric Auger 
8140ef02cd6SEric Auger 	report_prefix_push("mapd valid=false");
8150ef02cd6SEric Auger 	/*
8160ef02cd6SEric Auger 	 * Unmap device 2 and check the eventid 20 formerly
8170ef02cd6SEric Auger 	 * attached to it does not hit anymore
8180ef02cd6SEric Auger 	 */
8190ef02cd6SEric Auger 
8200ef02cd6SEric Auger 	its_send_mapd(dev2, false);
8210ef02cd6SEric Auger 	lpi_stats_expect(-1, -1);
8220ef02cd6SEric Auger 	its_send_int(dev2, 20);
8230ef02cd6SEric Auger 	check_lpi_stats("no LPI after device unmap");
8240ef02cd6SEric Auger 	report_prefix_pop();
8250ef02cd6SEric Auger }
82664260a5fSEric Auger 
82764260a5fSEric Auger static void test_its_migration(void)
82864260a5fSEric Auger {
82964260a5fSEric Auger 	struct its_device *dev2, *dev7;
83064260a5fSEric Auger 	bool test_skipped = false;
83164260a5fSEric Auger 
83264260a5fSEric Auger 	if (its_setup1()) {
83364260a5fSEric Auger 		test_skipped = true;
83464260a5fSEric Auger 		goto do_migrate;
83564260a5fSEric Auger 	}
83664260a5fSEric Auger 
83764260a5fSEric Auger 	dev2 = its_get_device(2);
83864260a5fSEric Auger 	dev7 = its_get_device(7);
83964260a5fSEric Auger 
84064260a5fSEric Auger do_migrate:
84164260a5fSEric Auger 	puts("Now migrate the VM, then press a key to continue...\n");
84264260a5fSEric Auger 	(void)getchar();
84364260a5fSEric Auger 	report_info("Migration complete");
84464260a5fSEric Auger 	if (test_skipped)
84564260a5fSEric Auger 		return;
84664260a5fSEric Auger 
84764260a5fSEric Auger 	lpi_stats_expect(3, 8195);
84864260a5fSEric Auger 	its_send_int(dev2, 20);
84964260a5fSEric Auger 	check_lpi_stats("dev2/eventid=20 triggers LPI 8195 on PE #3 after migration");
85064260a5fSEric Auger 
85164260a5fSEric Auger 	lpi_stats_expect(2, 8196);
85264260a5fSEric Auger 	its_send_int(dev7, 255);
85364260a5fSEric Auger 	check_lpi_stats("dev7/eventid=255 triggers LPI 8196 on PE #2 after migration");
85464260a5fSEric Auger }
855de582149SEric Auger 
856de582149SEric Auger #define ERRATA_UNMAPPED_COLLECTIONS "ERRATA_8c58be34494b"
857de582149SEric Auger 
858de582149SEric Auger static void test_migrate_unmapped_collection(void)
859de582149SEric Auger {
860de582149SEric Auger 	struct its_collection *col = NULL;
861de582149SEric Auger 	struct its_device *dev2 = NULL, *dev7 = NULL;
862de582149SEric Auger 	bool test_skipped = false;
863de582149SEric Auger 	int pe0 = 0;
864de582149SEric Auger 	u8 config;
865de582149SEric Auger 
866de582149SEric Auger 	if (its_setup1()) {
867de582149SEric Auger 		test_skipped = true;
868de582149SEric Auger 		goto do_migrate;
869de582149SEric Auger 	}
870de582149SEric Auger 
871de582149SEric Auger 	if (!errata(ERRATA_UNMAPPED_COLLECTIONS)) {
872de582149SEric Auger 		report_skip("Skipping test, as this test hangs without the fix. "
873de582149SEric Auger 			    "Set %s=y to enable.", ERRATA_UNMAPPED_COLLECTIONS);
874de582149SEric Auger 		test_skipped = true;
875de582149SEric Auger 		goto do_migrate;
876de582149SEric Auger 	}
877de582149SEric Auger 
878de582149SEric Auger 	col = its_create_collection(pe0, pe0);
879de582149SEric Auger 	dev2 = its_get_device(2);
880de582149SEric Auger 	dev7 = its_get_device(7);
881de582149SEric Auger 
882de582149SEric Auger 	/* MAPTI with the collection unmapped */
883de582149SEric Auger 	its_send_mapti(dev2, 8192, 0, col);
884de582149SEric Auger 	gicv3_lpi_set_config(8192, LPI_PROP_DEFAULT);
885de582149SEric Auger 
886de582149SEric Auger do_migrate:
887de582149SEric Auger 	puts("Now migrate the VM, then press a key to continue...\n");
888de582149SEric Auger 	(void)getchar();
889de582149SEric Auger 	report_info("Migration complete");
890de582149SEric Auger 	if (test_skipped)
891de582149SEric Auger 		return;
892de582149SEric Auger 
893de582149SEric Auger 	/* on the destination, map the collection */
894de582149SEric Auger 	its_send_mapc(col, true);
895de582149SEric Auger 	its_send_invall(col);
896de582149SEric Auger 
897de582149SEric Auger 	lpi_stats_expect(2, 8196);
898de582149SEric Auger 	its_send_int(dev7, 255);
899de582149SEric Auger 	check_lpi_stats("dev7/eventid= 255 triggered LPI 8196 on PE #2");
900de582149SEric Auger 
901de582149SEric Auger 	config = gicv3_lpi_get_config(8192);
902de582149SEric Auger 	report(config == LPI_PROP_DEFAULT,
903de582149SEric Auger 	       "Config of LPI 8192 was properly migrated");
904de582149SEric Auger 
905de582149SEric Auger 	lpi_stats_expect(pe0, 8192);
906de582149SEric Auger 	its_send_int(dev2, 0);
907de582149SEric Auger 	check_lpi_stats("dev2/eventid = 0 triggered LPI 8192 on PE0");
908de582149SEric Auger }
909de582149SEric Auger 
910de582149SEric Auger static void test_its_pending_migration(void)
911de582149SEric Auger {
912de582149SEric Auger 	struct its_device *dev;
913de582149SEric Auger 	struct its_collection *collection[2];
914de582149SEric Auger 	int *expected = calloc(nr_cpus, sizeof(int));
915de582149SEric Auger 	int pe0 = nr_cpus - 1, pe1 = nr_cpus - 2;
916de582149SEric Auger 	bool test_skipped = false;
917de582149SEric Auger 	u64 pendbaser;
918de582149SEric Auger 	void *ptr;
919de582149SEric Auger 	int i;
920de582149SEric Auger 
921de582149SEric Auger 	if (its_prerequisites(4)) {
922de582149SEric Auger 		test_skipped = true;
923de582149SEric Auger 		goto do_migrate;
924de582149SEric Auger 	}
925de582149SEric Auger 
926de582149SEric Auger 	dev = its_create_device(2 /* dev id */, 8 /* nb_ites */);
927de582149SEric Auger 	its_send_mapd(dev, true);
928de582149SEric Auger 
929de582149SEric Auger 	collection[0] = its_create_collection(pe0, pe0);
930de582149SEric Auger 	collection[1] = its_create_collection(pe1, pe1);
931de582149SEric Auger 	its_send_mapc(collection[0], true);
932de582149SEric Auger 	its_send_mapc(collection[1], true);
933de582149SEric Auger 
934de582149SEric Auger 	/* disable lpi at redist level */
935de582149SEric Auger 	gicv3_lpi_rdist_disable(pe0);
936de582149SEric Auger 	gicv3_lpi_rdist_disable(pe1);
937de582149SEric Auger 
938de582149SEric Auger 	/* lpis are interleaved inbetween the 2 PEs */
939de582149SEric Auger 	for (i = 0; i < 256; i++) {
940de582149SEric Auger 		struct its_collection *col = i % 2 ? collection[0] :
941de582149SEric Auger 						     collection[1];
942de582149SEric Auger 		int vcpu = col->target_address >> 16;
943de582149SEric Auger 
944de582149SEric Auger 		its_send_mapti(dev, LPI(i), i, col);
945de582149SEric Auger 		gicv3_lpi_set_config(LPI(i), LPI_PROP_DEFAULT);
946de582149SEric Auger 		gicv3_lpi_set_clr_pending(vcpu, LPI(i), true);
947de582149SEric Auger 	}
948de582149SEric Auger 	its_send_invall(collection[0]);
949de582149SEric Auger 	its_send_invall(collection[1]);
950de582149SEric Auger 
951de582149SEric Auger 	/* Clear the PTZ bit on each pendbaser */
952de582149SEric Auger 
953de582149SEric Auger 	expected[pe0] = 128;
954de582149SEric Auger 	expected[pe1] = 128;
955de582149SEric Auger 
956de582149SEric Auger 	ptr = gicv3_data.redist_base[pe0] + GICR_PENDBASER;
957de582149SEric Auger 	pendbaser = readq(ptr);
958de582149SEric Auger 	writeq(pendbaser & ~GICR_PENDBASER_PTZ, ptr);
959de582149SEric Auger 
960de582149SEric Auger 	ptr = gicv3_data.redist_base[pe1] + GICR_PENDBASER;
961de582149SEric Auger 	pendbaser = readq(ptr);
962de582149SEric Auger 	writeq(pendbaser & ~GICR_PENDBASER_PTZ, ptr);
963de582149SEric Auger 
964de582149SEric Auger 	gicv3_lpi_rdist_enable(pe0);
965de582149SEric Auger 	gicv3_lpi_rdist_enable(pe1);
966de582149SEric Auger 
967de582149SEric Auger do_migrate:
968de582149SEric Auger 	puts("Now migrate the VM, then press a key to continue...\n");
969de582149SEric Auger 	(void)getchar();
970de582149SEric Auger 	report_info("Migration complete");
971de582149SEric Auger 	if (test_skipped)
972de582149SEric Auger 		return;
973de582149SEric Auger 
974de582149SEric Auger 	/* let's wait for the 256 LPIs to be handled */
975de582149SEric Auger 	mdelay(1000);
976de582149SEric Auger 
977de582149SEric Auger 	check_lpi_hits(expected, "128 LPIs on both PE0 and PE1 after migration");
978de582149SEric Auger }
979ba74b106SEric Auger #endif
980ba74b106SEric Auger 
981ac4a67b6SAndrew Jones int main(int argc, char **argv)
982ac4a67b6SAndrew Jones {
9832e2d471dSAndrew Jones 	if (!gic_init()) {
984ac4a67b6SAndrew Jones 		printf("No supported gic present, skipping tests...\n");
985ac4a67b6SAndrew Jones 		return report_summary();
986ac4a67b6SAndrew Jones 	}
987ac4a67b6SAndrew Jones 
9882b19b829SAndrew Jones 	report_prefix_pushf("gicv%d", gic_version());
989ac4a67b6SAndrew Jones 
9902e2d471dSAndrew Jones 	switch (gic_version()) {
9912e2d471dSAndrew Jones 	case 2:
9922e2d471dSAndrew Jones 		gic = &gicv2;
9932e2d471dSAndrew Jones 		break;
9942e2d471dSAndrew Jones 	case 3:
9952e2d471dSAndrew Jones 		gic = &gicv3;
9962e2d471dSAndrew Jones 		break;
9972e2d471dSAndrew Jones 	}
9982e2d471dSAndrew Jones 
999ac4a67b6SAndrew Jones 	if (argc < 2)
1000ac4a67b6SAndrew Jones 		report_abort("no test specified");
1001ac4a67b6SAndrew Jones 
1002ac4a67b6SAndrew Jones 	if (strcmp(argv[1], "ipi") == 0) {
1003ac4a67b6SAndrew Jones 		report_prefix_push(argv[1]);
1004ac4a67b6SAndrew Jones 		nr_cpu_check(2);
100500b34f56SAndrew Jones 		on_cpus(ipi_test, NULL);
1006c152d8bcSChristoffer Dall 	} else if (strcmp(argv[1], "active") == 0) {
1007c152d8bcSChristoffer Dall 		run_active_clear_test();
100878ad7e95SAndre Przywara 	} else if (strcmp(argv[1], "mmio") == 0) {
100978ad7e95SAndre Przywara 		report_prefix_push(argv[1]);
101078ad7e95SAndre Przywara 		gic_test_mmio();
101178ad7e95SAndre Przywara 		report_prefix_pop();
10120ef02cd6SEric Auger 	} else if (!strcmp(argv[1], "its-trigger")) {
10130ef02cd6SEric Auger 		report_prefix_push(argv[1]);
10140ef02cd6SEric Auger 		test_its_trigger();
10150ef02cd6SEric Auger 		report_prefix_pop();
101664260a5fSEric Auger 	} else if (!strcmp(argv[1], "its-migration")) {
101764260a5fSEric Auger 		report_prefix_push(argv[1]);
101864260a5fSEric Auger 		test_its_migration();
101964260a5fSEric Auger 		report_prefix_pop();
1020de582149SEric Auger 	} else if (!strcmp(argv[1], "its-pending-migration")) {
1021de582149SEric Auger 		report_prefix_push(argv[1]);
1022de582149SEric Auger 		test_its_pending_migration();
1023de582149SEric Auger 		report_prefix_pop();
1024de582149SEric Auger 	} else if (!strcmp(argv[1], "its-migrate-unmapped-collection")) {
1025de582149SEric Auger 		report_prefix_push(argv[1]);
1026de582149SEric Auger 		test_migrate_unmapped_collection();
1027de582149SEric Auger 		report_prefix_pop();
1028ba74b106SEric Auger 	} else if (strcmp(argv[1], "its-introspection") == 0) {
1029ba74b106SEric Auger 		report_prefix_push(argv[1]);
1030ba74b106SEric Auger 		test_its_introspection();
1031ba74b106SEric Auger 		report_prefix_pop();
1032ac4a67b6SAndrew Jones 	} else {
1033ac4a67b6SAndrew Jones 		report_abort("Unknown subtest '%s'", argv[1]);
1034ac4a67b6SAndrew Jones 	}
1035ac4a67b6SAndrew Jones 
1036ac4a67b6SAndrew Jones 	return report_summary();
1037ac4a67b6SAndrew Jones }
1038