xref: /kvm-unit-tests/arm/gic.c (revision 10e3685f1e4775e7c6ee77863d2dd8684d35da12)
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];
37ca1b7a7bSAndrew Jones static int bad_sender[NR_CPUS], bad_irq[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;
59ca1b7a7bSAndrew Jones 		bad_sender[i] = -1;
60ca1b7a7bSAndrew Jones 		bad_irq[i] = -1;
61ca1b7a7bSAndrew Jones 	}
62ca1b7a7bSAndrew Jones 	smp_wmb();
63ca1b7a7bSAndrew Jones }
64ca1b7a7bSAndrew Jones 
6596edb026SAndre Przywara static void check_acked(const char *testname, cpumask_t *mask)
66ac4a67b6SAndrew Jones {
67ac4a67b6SAndrew Jones 	int missing = 0, extra = 0, unexpected = 0;
68ac4a67b6SAndrew Jones 	int nr_pass, cpu, i;
69ca1b7a7bSAndrew Jones 	bool bad = false;
70ac4a67b6SAndrew Jones 
71ac4a67b6SAndrew Jones 	/* Wait up to 5s for all interrupts to be delivered */
72ac4a67b6SAndrew Jones 	for (i = 0; i < 50; ++i) {
73ac4a67b6SAndrew Jones 		mdelay(100);
74ac4a67b6SAndrew Jones 		nr_pass = 0;
75ac4a67b6SAndrew Jones 		for_each_present_cpu(cpu) {
76ac4a67b6SAndrew Jones 			smp_rmb();
77ac4a67b6SAndrew Jones 			nr_pass += cpumask_test_cpu(cpu, mask) ?
78ac4a67b6SAndrew Jones 				acked[cpu] == 1 : acked[cpu] == 0;
79ca1b7a7bSAndrew Jones 
80ca1b7a7bSAndrew Jones 			if (bad_sender[cpu] != -1) {
81ca1b7a7bSAndrew Jones 				printf("cpu%d received IPI from wrong sender %d\n",
82ca1b7a7bSAndrew Jones 					cpu, bad_sender[cpu]);
83ca1b7a7bSAndrew Jones 				bad = true;
84ca1b7a7bSAndrew Jones 			}
85ca1b7a7bSAndrew Jones 
86ca1b7a7bSAndrew Jones 			if (bad_irq[cpu] != -1) {
87ca1b7a7bSAndrew Jones 				printf("cpu%d received wrong irq %d\n",
88ca1b7a7bSAndrew Jones 					cpu, bad_irq[cpu]);
89ca1b7a7bSAndrew Jones 				bad = true;
90ca1b7a7bSAndrew Jones 			}
91ac4a67b6SAndrew Jones 		}
92ac4a67b6SAndrew Jones 		if (nr_pass == nr_cpus) {
93a299895bSThomas Huth 			report(!bad, "%s", testname);
9496edb026SAndre Przywara 			if (i)
9596edb026SAndre Przywara 				report_info("took more than %d ms", i * 100);
96ac4a67b6SAndrew Jones 			return;
97ac4a67b6SAndrew Jones 		}
98ac4a67b6SAndrew Jones 	}
99ac4a67b6SAndrew Jones 
100ac4a67b6SAndrew Jones 	for_each_present_cpu(cpu) {
101ac4a67b6SAndrew Jones 		if (cpumask_test_cpu(cpu, mask)) {
102ac4a67b6SAndrew Jones 			if (!acked[cpu])
103ac4a67b6SAndrew Jones 				++missing;
104ac4a67b6SAndrew Jones 			else if (acked[cpu] > 1)
105ac4a67b6SAndrew Jones 				++extra;
106ac4a67b6SAndrew Jones 		} else {
107ac4a67b6SAndrew Jones 			if (acked[cpu])
108ac4a67b6SAndrew Jones 				++unexpected;
109ac4a67b6SAndrew Jones 		}
110ac4a67b6SAndrew Jones 	}
111ac4a67b6SAndrew Jones 
112a299895bSThomas Huth 	report(false, "%s", testname);
11396edb026SAndre Przywara 	report_info("Timed-out (5s). ACKS: missing=%d extra=%d unexpected=%d",
11496edb026SAndre Przywara 		    missing, extra, unexpected);
115ac4a67b6SAndrew Jones }
116ac4a67b6SAndrew Jones 
117ac4a67b6SAndrew Jones static void check_spurious(void)
118ac4a67b6SAndrew Jones {
119ac4a67b6SAndrew Jones 	int cpu;
120ac4a67b6SAndrew Jones 
121ac4a67b6SAndrew Jones 	smp_rmb();
122ac4a67b6SAndrew Jones 	for_each_present_cpu(cpu) {
123ac4a67b6SAndrew Jones 		if (spurious[cpu])
124ac4a67b6SAndrew Jones 			report_info("WARN: cpu%d got %d spurious interrupts",
125ac4a67b6SAndrew Jones 				cpu, spurious[cpu]);
126ac4a67b6SAndrew Jones 	}
127ac4a67b6SAndrew Jones }
128ac4a67b6SAndrew Jones 
129ca1b7a7bSAndrew Jones static void check_ipi_sender(u32 irqstat)
130ca1b7a7bSAndrew Jones {
131ca1b7a7bSAndrew Jones 	if (gic_version() == 2) {
132ca1b7a7bSAndrew Jones 		int src = (irqstat >> 10) & 7;
133ca1b7a7bSAndrew Jones 
134ca1b7a7bSAndrew Jones 		if (src != IPI_SENDER)
135ca1b7a7bSAndrew Jones 			bad_sender[smp_processor_id()] = src;
136ca1b7a7bSAndrew Jones 	}
137ca1b7a7bSAndrew Jones }
138ca1b7a7bSAndrew Jones 
139ca1b7a7bSAndrew Jones static void check_irqnr(u32 irqnr)
140ca1b7a7bSAndrew Jones {
141ca1b7a7bSAndrew Jones 	if (irqnr != IPI_IRQ)
142ca1b7a7bSAndrew Jones 		bad_irq[smp_processor_id()] = irqnr;
143ca1b7a7bSAndrew Jones }
144ca1b7a7bSAndrew Jones 
145ac4a67b6SAndrew Jones static void ipi_handler(struct pt_regs *regs __unused)
146ac4a67b6SAndrew Jones {
1472e2d471dSAndrew Jones 	u32 irqstat = gic_read_iar();
1482e2d471dSAndrew Jones 	u32 irqnr = gic_iar_irqnr(irqstat);
149ac4a67b6SAndrew Jones 
150ac4a67b6SAndrew Jones 	if (irqnr != GICC_INT_SPURIOUS) {
1512e2d471dSAndrew Jones 		gic_write_eoir(irqstat);
152ca1b7a7bSAndrew Jones 		smp_rmb(); /* pairs with wmb in stats_reset */
153ac4a67b6SAndrew Jones 		++acked[smp_processor_id()];
154ca1b7a7bSAndrew Jones 		check_ipi_sender(irqstat);
155ca1b7a7bSAndrew Jones 		check_irqnr(irqnr);
156ac4a67b6SAndrew Jones 		smp_wmb(); /* pairs with rmb in check_acked */
157ac4a67b6SAndrew Jones 	} else {
158ac4a67b6SAndrew Jones 		++spurious[smp_processor_id()];
159ac4a67b6SAndrew Jones 		smp_wmb();
160ac4a67b6SAndrew Jones 	}
161ac4a67b6SAndrew Jones }
162ac4a67b6SAndrew Jones 
1630ef02cd6SEric Auger static void setup_irq(irq_handler_fn handler)
1640ef02cd6SEric Auger {
1650ef02cd6SEric Auger 	gic_enable_defaults();
1660ef02cd6SEric Auger #ifdef __arm__
1670ef02cd6SEric Auger 	install_exception_handler(EXCPTN_IRQ, handler);
1680ef02cd6SEric Auger #else
1690ef02cd6SEric Auger 	install_irq_handler(EL1H_IRQ, handler);
1700ef02cd6SEric Auger #endif
1710ef02cd6SEric Auger 	local_irq_enable();
1720ef02cd6SEric Auger }
1730ef02cd6SEric Auger 
1740ef02cd6SEric Auger #if defined(__aarch64__)
1750ef02cd6SEric Auger struct its_event {
1760ef02cd6SEric Auger 	int cpu_id;
1770ef02cd6SEric Auger 	int lpi_id;
1780ef02cd6SEric Auger };
1790ef02cd6SEric Auger 
1800ef02cd6SEric Auger struct its_stats {
1810ef02cd6SEric Auger 	struct its_event expected;
1820ef02cd6SEric Auger 	struct its_event observed;
1830ef02cd6SEric Auger };
1840ef02cd6SEric Auger 
1850ef02cd6SEric Auger static struct its_stats lpi_stats;
1860ef02cd6SEric Auger 
1870ef02cd6SEric Auger static void lpi_handler(struct pt_regs *regs __unused)
1880ef02cd6SEric Auger {
1890ef02cd6SEric Auger 	u32 irqstat = gic_read_iar();
1900ef02cd6SEric Auger 	int irqnr = gic_iar_irqnr(irqstat);
1910ef02cd6SEric Auger 
1920ef02cd6SEric Auger 	gic_write_eoir(irqstat);
1930ef02cd6SEric Auger 	assert(irqnr >= 8192);
1940ef02cd6SEric Auger 	smp_rmb(); /* pairs with wmb in lpi_stats_expect */
1950ef02cd6SEric Auger 	lpi_stats.observed.cpu_id = smp_processor_id();
1960ef02cd6SEric Auger 	lpi_stats.observed.lpi_id = irqnr;
197de582149SEric Auger 	acked[lpi_stats.observed.cpu_id]++;
1980ef02cd6SEric Auger 	smp_wmb(); /* pairs with rmb in check_lpi_stats */
1990ef02cd6SEric Auger }
2000ef02cd6SEric Auger 
2010ef02cd6SEric Auger static void lpi_stats_expect(int exp_cpu_id, int exp_lpi_id)
2020ef02cd6SEric Auger {
2030ef02cd6SEric Auger 	lpi_stats.expected.cpu_id = exp_cpu_id;
2040ef02cd6SEric Auger 	lpi_stats.expected.lpi_id = exp_lpi_id;
2050ef02cd6SEric Auger 	lpi_stats.observed.cpu_id = -1;
2060ef02cd6SEric Auger 	lpi_stats.observed.lpi_id = -1;
2070ef02cd6SEric Auger 	smp_wmb(); /* pairs with rmb in handler */
2080ef02cd6SEric Auger }
2090ef02cd6SEric Auger 
2100ef02cd6SEric Auger static void check_lpi_stats(const char *msg)
2110ef02cd6SEric Auger {
2120ef02cd6SEric Auger 	int i;
2130ef02cd6SEric Auger 
2140ef02cd6SEric Auger 	for (i = 0; i < 50; i++) {
2150ef02cd6SEric Auger 		mdelay(100);
2160ef02cd6SEric Auger 		smp_rmb(); /* pairs with wmb in lpi_handler */
2170ef02cd6SEric Auger 		if (lpi_stats.observed.cpu_id == lpi_stats.expected.cpu_id &&
2180ef02cd6SEric Auger 		    lpi_stats.observed.lpi_id == lpi_stats.expected.lpi_id) {
2190ef02cd6SEric Auger 			report(true, "%s", msg);
2200ef02cd6SEric Auger 			return;
2210ef02cd6SEric Auger 		}
2220ef02cd6SEric Auger 	}
2230ef02cd6SEric Auger 
2240ef02cd6SEric Auger 	if (lpi_stats.observed.cpu_id == -1 && lpi_stats.observed.lpi_id == -1) {
2250ef02cd6SEric Auger 		report_info("No LPI received whereas (cpuid=%d, intid=%d) "
2260ef02cd6SEric Auger 			    "was expected", lpi_stats.expected.cpu_id,
2270ef02cd6SEric Auger 			    lpi_stats.expected.lpi_id);
2280ef02cd6SEric Auger 	} else {
2290ef02cd6SEric Auger 		report_info("Unexpected LPI (cpuid=%d, intid=%d)",
2300ef02cd6SEric Auger 			    lpi_stats.observed.cpu_id,
2310ef02cd6SEric Auger 			    lpi_stats.observed.lpi_id);
2320ef02cd6SEric Auger 	}
2330ef02cd6SEric Auger 	report(false, "%s", msg);
2340ef02cd6SEric Auger }
2350ef02cd6SEric Auger 
2360ef02cd6SEric Auger static void secondary_lpi_test(void)
2370ef02cd6SEric Auger {
2380ef02cd6SEric Auger 	setup_irq(lpi_handler);
2390ef02cd6SEric Auger 	cpumask_set_cpu(smp_processor_id(), &ready);
2400ef02cd6SEric Auger 	while (1)
2410ef02cd6SEric Auger 		wfi();
2420ef02cd6SEric Auger }
243de582149SEric Auger 
244de582149SEric Auger static void check_lpi_hits(int *expected, const char *msg)
245de582149SEric Auger {
246de582149SEric Auger 	bool pass = true;
247de582149SEric Auger 	int i;
248de582149SEric Auger 
249de582149SEric Auger 	for_each_present_cpu(i) {
250de582149SEric Auger 		if (acked[i] != expected[i]) {
251de582149SEric Auger 			report_info("expected %d LPIs on PE #%d, %d observed",
252de582149SEric Auger 				    expected[i], i, acked[i]);
253de582149SEric Auger 			pass = false;
254de582149SEric Auger 			break;
255de582149SEric Auger 		}
256de582149SEric Auger 	}
257de582149SEric Auger 	report(pass, "%s", msg);
258de582149SEric Auger }
2590ef02cd6SEric Auger #endif
2600ef02cd6SEric Auger 
2612e2d471dSAndrew Jones static void gicv2_ipi_send_self(void)
2622e2d471dSAndrew Jones {
263*10e3685fSAlexandru Elisei 	/*
264*10e3685fSAlexandru Elisei 	 * The wmb() in writel and rmb() when acknowledging the interrupt are
265*10e3685fSAlexandru Elisei 	 * sufficient for ensuring that writes that happen in program order
266*10e3685fSAlexandru Elisei 	 * before the interrupt are observed in the interrupt handler after
267*10e3685fSAlexandru Elisei 	 * acknowledging the interrupt.
268*10e3685fSAlexandru Elisei 	 */
269ca1b7a7bSAndrew Jones 	writel(2 << 24 | IPI_IRQ, gicv2_dist_base() + GICD_SGIR);
2702e2d471dSAndrew Jones }
2712e2d471dSAndrew Jones 
2722e2d471dSAndrew Jones static void gicv2_ipi_send_broadcast(void)
2732e2d471dSAndrew Jones {
274*10e3685fSAlexandru Elisei 	/* No barriers are needed, same situation as gicv2_ipi_send_self() */
275ca1b7a7bSAndrew Jones 	writel(1 << 24 | IPI_IRQ, gicv2_dist_base() + GICD_SGIR);
2762e2d471dSAndrew Jones }
2772e2d471dSAndrew Jones 
2782e2d471dSAndrew Jones static void gicv3_ipi_send_self(void)
2792e2d471dSAndrew Jones {
280ca1b7a7bSAndrew Jones 	gic_ipi_send_single(IPI_IRQ, smp_processor_id());
2812e2d471dSAndrew Jones }
2822e2d471dSAndrew Jones 
2832e2d471dSAndrew Jones static void gicv3_ipi_send_broadcast(void)
2842e2d471dSAndrew Jones {
2850c03f4b1SAlexandru Elisei 	/*
2860c03f4b1SAlexandru Elisei 	 * Ensure stores to Normal memory are visible to other CPUs before
2870c03f4b1SAlexandru Elisei 	 * sending the IPI
2880c03f4b1SAlexandru Elisei 	 */
2890c03f4b1SAlexandru Elisei 	wmb();
290ca1b7a7bSAndrew Jones 	gicv3_write_sgi1r(1ULL << 40 | IPI_IRQ << 24);
2912e2d471dSAndrew Jones 	isb();
2922e2d471dSAndrew Jones }
2932e2d471dSAndrew Jones 
294ac4a67b6SAndrew Jones static void ipi_test_self(void)
295ac4a67b6SAndrew Jones {
296ac4a67b6SAndrew Jones 	cpumask_t mask;
297ac4a67b6SAndrew Jones 
298ac4a67b6SAndrew Jones 	report_prefix_push("self");
299ca1b7a7bSAndrew Jones 	stats_reset();
300ac4a67b6SAndrew Jones 	cpumask_clear(&mask);
301ca1b7a7bSAndrew Jones 	cpumask_set_cpu(smp_processor_id(), &mask);
3022e2d471dSAndrew Jones 	gic->ipi.send_self();
30396edb026SAndre Przywara 	check_acked("IPI: self", &mask);
304ac4a67b6SAndrew Jones 	report_prefix_pop();
305ac4a67b6SAndrew Jones }
306ac4a67b6SAndrew Jones 
307ac4a67b6SAndrew Jones static void ipi_test_smp(void)
308ac4a67b6SAndrew Jones {
309ac4a67b6SAndrew Jones 	cpumask_t mask;
3102e2d471dSAndrew Jones 	int i;
311ac4a67b6SAndrew Jones 
312ac4a67b6SAndrew Jones 	report_prefix_push("target-list");
313ca1b7a7bSAndrew Jones 	stats_reset();
3142e2d471dSAndrew Jones 	cpumask_copy(&mask, &cpu_present_mask);
315ca1b7a7bSAndrew Jones 	for (i = smp_processor_id() & 1; i < nr_cpus; i += 2)
3162e2d471dSAndrew Jones 		cpumask_clear_cpu(i, &mask);
317ca1b7a7bSAndrew Jones 	gic_ipi_send_mask(IPI_IRQ, &mask);
31896edb026SAndre Przywara 	check_acked("IPI: directed", &mask);
319ac4a67b6SAndrew Jones 	report_prefix_pop();
320ac4a67b6SAndrew Jones 
321ac4a67b6SAndrew Jones 	report_prefix_push("broadcast");
322ca1b7a7bSAndrew Jones 	stats_reset();
323ac4a67b6SAndrew Jones 	cpumask_copy(&mask, &cpu_present_mask);
324ca1b7a7bSAndrew Jones 	cpumask_clear_cpu(smp_processor_id(), &mask);
3252e2d471dSAndrew Jones 	gic->ipi.send_broadcast();
32696edb026SAndre Przywara 	check_acked("IPI: broadcast", &mask);
327ac4a67b6SAndrew Jones 	report_prefix_pop();
328ac4a67b6SAndrew Jones }
329ac4a67b6SAndrew Jones 
330ca1b7a7bSAndrew Jones static void ipi_send(void)
331ca1b7a7bSAndrew Jones {
33225f66327SEric Auger 	setup_irq(ipi_handler);
333ca1b7a7bSAndrew Jones 	wait_on_ready();
334ca1b7a7bSAndrew Jones 	ipi_test_self();
335ca1b7a7bSAndrew Jones 	ipi_test_smp();
336ca1b7a7bSAndrew Jones 	check_spurious();
337ca1b7a7bSAndrew Jones 	exit(report_summary());
338ca1b7a7bSAndrew Jones }
339ca1b7a7bSAndrew Jones 
340ac4a67b6SAndrew Jones static void ipi_recv(void)
341ac4a67b6SAndrew Jones {
34225f66327SEric Auger 	setup_irq(ipi_handler);
343ac4a67b6SAndrew Jones 	cpumask_set_cpu(smp_processor_id(), &ready);
344ac4a67b6SAndrew Jones 	while (1)
345ac4a67b6SAndrew Jones 		wfi();
346ac4a67b6SAndrew Jones }
347ac4a67b6SAndrew Jones 
34800b34f56SAndrew Jones static void ipi_test(void *data __unused)
349bfd500b4SAndrew Jones {
350bfd500b4SAndrew Jones 	if (smp_processor_id() == IPI_SENDER)
351bfd500b4SAndrew Jones 		ipi_send();
352bfd500b4SAndrew Jones 	else
353bfd500b4SAndrew Jones 		ipi_recv();
354bfd500b4SAndrew Jones }
355bfd500b4SAndrew Jones 
3562e2d471dSAndrew Jones static struct gic gicv2 = {
3572e2d471dSAndrew Jones 	.ipi = {
3582e2d471dSAndrew Jones 		.send_self = gicv2_ipi_send_self,
3592e2d471dSAndrew Jones 		.send_broadcast = gicv2_ipi_send_broadcast,
3602e2d471dSAndrew Jones 	},
3612e2d471dSAndrew Jones };
3622e2d471dSAndrew Jones 
3632e2d471dSAndrew Jones static struct gic gicv3 = {
3642e2d471dSAndrew Jones 	.ipi = {
3652e2d471dSAndrew Jones 		.send_self = gicv3_ipi_send_self,
3662e2d471dSAndrew Jones 		.send_broadcast = gicv3_ipi_send_broadcast,
3672e2d471dSAndrew Jones 	},
3682e2d471dSAndrew Jones };
3692e2d471dSAndrew Jones 
370c152d8bcSChristoffer Dall static void ipi_clear_active_handler(struct pt_regs *regs __unused)
371c152d8bcSChristoffer Dall {
372c152d8bcSChristoffer Dall 	u32 irqstat = gic_read_iar();
373c152d8bcSChristoffer Dall 	u32 irqnr = gic_iar_irqnr(irqstat);
374c152d8bcSChristoffer Dall 
375c152d8bcSChristoffer Dall 	if (irqnr != GICC_INT_SPURIOUS) {
376c152d8bcSChristoffer Dall 		void *base;
377c152d8bcSChristoffer Dall 		u32 val = 1 << IPI_IRQ;
378c152d8bcSChristoffer Dall 
379c152d8bcSChristoffer Dall 		if (gic_version() == 2)
380c152d8bcSChristoffer Dall 			base = gicv2_dist_base();
381c152d8bcSChristoffer Dall 		else
3826d4d7c4bSAndrew Jones 			base = gicv3_sgi_base();
383c152d8bcSChristoffer Dall 
384c152d8bcSChristoffer Dall 		writel(val, base + GICD_ICACTIVER);
385c152d8bcSChristoffer Dall 
386c152d8bcSChristoffer Dall 		smp_rmb(); /* pairs with wmb in stats_reset */
387c152d8bcSChristoffer Dall 		++acked[smp_processor_id()];
388c152d8bcSChristoffer Dall 		check_irqnr(irqnr);
389c152d8bcSChristoffer Dall 		smp_wmb(); /* pairs with rmb in check_acked */
390c152d8bcSChristoffer Dall 	} else {
391c152d8bcSChristoffer Dall 		++spurious[smp_processor_id()];
392c152d8bcSChristoffer Dall 		smp_wmb();
393c152d8bcSChristoffer Dall 	}
394c152d8bcSChristoffer Dall }
395c152d8bcSChristoffer Dall 
396c152d8bcSChristoffer Dall static void run_active_clear_test(void)
397c152d8bcSChristoffer Dall {
398c152d8bcSChristoffer Dall 	report_prefix_push("active");
39925f66327SEric Auger 	setup_irq(ipi_clear_active_handler);
400c152d8bcSChristoffer Dall 	ipi_test_self();
401c152d8bcSChristoffer Dall 	report_prefix_pop();
402c152d8bcSChristoffer Dall }
403c152d8bcSChristoffer Dall 
40478ad7e95SAndre Przywara static bool test_ro_pattern_32(void *address, u32 pattern, u32 orig)
40578ad7e95SAndre Przywara {
40678ad7e95SAndre Przywara 	u32 reg;
40778ad7e95SAndre Przywara 
40878ad7e95SAndre Przywara 	writel(pattern, address);
40978ad7e95SAndre Przywara 	reg = readl(address);
41078ad7e95SAndre Przywara 
41178ad7e95SAndre Przywara 	if (reg != orig)
41278ad7e95SAndre Przywara 		writel(orig, address);
41378ad7e95SAndre Przywara 
41478ad7e95SAndre Przywara 	return reg == orig;
41578ad7e95SAndre Przywara }
41678ad7e95SAndre Przywara 
41778ad7e95SAndre Przywara static bool test_readonly_32(void *address, bool razwi)
41878ad7e95SAndre Przywara {
41978ad7e95SAndre Przywara 	u32 orig, pattern;
42078ad7e95SAndre Przywara 
42178ad7e95SAndre Przywara 	orig = readl(address);
42278ad7e95SAndre Przywara 	if (razwi && orig)
42378ad7e95SAndre Przywara 		return false;
42478ad7e95SAndre Przywara 
42578ad7e95SAndre Przywara 	pattern = 0xffffffff;
42678ad7e95SAndre Przywara 	if (orig != pattern) {
42778ad7e95SAndre Przywara 		if (!test_ro_pattern_32(address, pattern, orig))
42878ad7e95SAndre Przywara 			return false;
42978ad7e95SAndre Przywara 	}
43078ad7e95SAndre Przywara 
43178ad7e95SAndre Przywara 	pattern = 0xa5a55a5a;
43278ad7e95SAndre Przywara 	if (orig != pattern) {
43378ad7e95SAndre Przywara 		if (!test_ro_pattern_32(address, pattern, orig))
43478ad7e95SAndre Przywara 			return false;
43578ad7e95SAndre Przywara 	}
43678ad7e95SAndre Przywara 
43778ad7e95SAndre Przywara 	pattern = 0;
43878ad7e95SAndre Przywara 	if (orig != pattern) {
43978ad7e95SAndre Przywara 		if (!test_ro_pattern_32(address, pattern, orig))
44078ad7e95SAndre Przywara 			return false;
44178ad7e95SAndre Przywara 	}
44278ad7e95SAndre Przywara 
44378ad7e95SAndre Przywara 	return true;
44478ad7e95SAndre Przywara }
44578ad7e95SAndre Przywara 
44678ad7e95SAndre Przywara static void test_typer_v2(uint32_t reg)
44778ad7e95SAndre Przywara {
44878ad7e95SAndre Przywara 	int nr_gic_cpus = ((reg >> 5) & 0x7) + 1;
44978ad7e95SAndre Przywara 
4508e0a4f41SAndre Przywara 	report_info("nr_cpus=%d", nr_cpus);
451a299895bSThomas Huth 	report(nr_cpus == nr_gic_cpus, "all CPUs have interrupts");
45278ad7e95SAndre Przywara }
45378ad7e95SAndre Przywara 
454ff31a1c4SAndre Przywara #define BYTE(reg32, byte) (((reg32) >> ((byte) * 8)) & 0xff)
455ff31a1c4SAndre Przywara #define REPLACE_BYTE(reg32, byte, new) (((reg32) & ~(0xff << ((byte) * 8))) |\
456ff31a1c4SAndre Przywara 					((new) << ((byte) * 8)))
457ff31a1c4SAndre Przywara 
458ff31a1c4SAndre Przywara /*
459ff31a1c4SAndre Przywara  * Some registers are byte accessible, do a byte-wide read and write of known
460ff31a1c4SAndre Przywara  * content to check for this.
461ff31a1c4SAndre Przywara  * Apply a @mask to cater for special register properties.
462ff31a1c4SAndre Przywara  * @pattern contains the value already in the register.
463ff31a1c4SAndre Przywara  */
464ff31a1c4SAndre Przywara static void test_byte_access(void *base_addr, u32 pattern, u32 mask)
465ff31a1c4SAndre Przywara {
466ff31a1c4SAndre Przywara 	u32 reg = readb(base_addr + 1);
4678e0a4f41SAndre Przywara 	bool res;
468ff31a1c4SAndre Przywara 
4698e0a4f41SAndre Przywara 	res = (reg == (BYTE(pattern, 1) & (mask >> 8)));
470a299895bSThomas Huth 	report(res, "byte reads successful");
4718e0a4f41SAndre Przywara 	if (!res)
47246ca10f4SAlexandru Elisei 		report_info("byte 1 of 0x%08"PRIx32" => 0x%02"PRIx32, pattern & mask, reg);
473ff31a1c4SAndre Przywara 
474ff31a1c4SAndre Przywara 	pattern = REPLACE_BYTE(pattern, 2, 0x1f);
475ff31a1c4SAndre Przywara 	writeb(BYTE(pattern, 2), base_addr + 2);
476ff31a1c4SAndre Przywara 	reg = readl(base_addr);
4778e0a4f41SAndre Przywara 	res = (reg == (pattern & mask));
478a299895bSThomas Huth 	report(res, "byte writes successful");
4798e0a4f41SAndre Przywara 	if (!res)
48046ca10f4SAlexandru Elisei 		report_info("writing 0x%02"PRIx32" into bytes 2 => 0x%08"PRIx32,
4818e0a4f41SAndre Przywara 			    BYTE(pattern, 2), reg);
482ff31a1c4SAndre Przywara }
483ff31a1c4SAndre Przywara 
484ff31a1c4SAndre Przywara static void test_priorities(int nr_irqs, void *priptr)
485ff31a1c4SAndre Przywara {
486ff31a1c4SAndre Przywara 	u32 orig_prio, reg, pri_bits;
487ff31a1c4SAndre Przywara 	u32 pri_mask, pattern;
488ff31a1c4SAndre Przywara 	void *first_spi = priptr + GIC_FIRST_SPI;
489ff31a1c4SAndre Przywara 
490ff31a1c4SAndre Przywara 	orig_prio = readl(first_spi);
491ff31a1c4SAndre Przywara 	report_prefix_push("IPRIORITYR");
492ff31a1c4SAndre Przywara 
493ff31a1c4SAndre Przywara 	/*
494ff31a1c4SAndre Przywara 	 * Determine implemented number of priority bits by writing all 1's
495ff31a1c4SAndre Przywara 	 * and checking the number of cleared bits in the value read back.
496ff31a1c4SAndre Przywara 	 */
497ff31a1c4SAndre Przywara 	writel(0xffffffff, first_spi);
498ff31a1c4SAndre Przywara 	pri_mask = readl(first_spi);
499ff31a1c4SAndre Przywara 
500ff31a1c4SAndre Przywara 	reg = ~pri_mask;
501a299895bSThomas Huth 	report((((reg >> 16) == (reg & 0xffff)) &&
502a299895bSThomas Huth 	        ((reg & 0xff) == ((reg >> 8) & 0xff))),
503a299895bSThomas Huth 	       "consistent priority masking");
50446ca10f4SAlexandru Elisei 	report_info("priority mask is 0x%08"PRIx32, pri_mask);
505ff31a1c4SAndre Przywara 
506ff31a1c4SAndre Przywara 	reg = reg & 0xff;
507ff31a1c4SAndre Przywara 	for (pri_bits = 8; reg & 1; reg >>= 1, pri_bits--)
508ff31a1c4SAndre Przywara 		;
509a299895bSThomas Huth 	report(pri_bits >= 4, "implements at least 4 priority bits");
51046ca10f4SAlexandru Elisei 	report_info("%"PRIu32" priority bits implemented", pri_bits);
511ff31a1c4SAndre Przywara 
512ff31a1c4SAndre Przywara 	pattern = 0;
513ff31a1c4SAndre Przywara 	writel(pattern, first_spi);
514a299895bSThomas Huth 	report(readl(first_spi) == pattern, "clearing priorities");
515ff31a1c4SAndre Przywara 
516ff31a1c4SAndre Przywara 	/* setting all priorities to their max valus was tested above */
517ff31a1c4SAndre Przywara 
518a299895bSThomas Huth 	report(test_readonly_32(priptr + nr_irqs, true),
519a299895bSThomas Huth 	       "accesses beyond limit RAZ/WI");
520ff31a1c4SAndre Przywara 
521ff31a1c4SAndre Przywara 	writel(pattern, priptr + nr_irqs - 4);
522a299895bSThomas Huth 	report(readl(priptr + nr_irqs - 4) == (pattern & pri_mask),
523a299895bSThomas Huth 	       "accessing last SPIs");
524ff31a1c4SAndre Przywara 
525ff31a1c4SAndre Przywara 	pattern = 0xff7fbf3f;
526ff31a1c4SAndre Przywara 	writel(pattern, first_spi);
527a299895bSThomas Huth 	report(readl(first_spi) == (pattern & pri_mask),
528a299895bSThomas Huth 	       "priorities are preserved");
529ff31a1c4SAndre Przywara 
530ff31a1c4SAndre Przywara 	/* The PRIORITY registers are byte accessible. */
531ff31a1c4SAndre Przywara 	test_byte_access(first_spi, pattern, pri_mask);
532ff31a1c4SAndre Przywara 
533ff31a1c4SAndre Przywara 	report_prefix_pop();
534ff31a1c4SAndre Przywara 	writel(orig_prio, first_spi);
535ff31a1c4SAndre Przywara }
536ff31a1c4SAndre Przywara 
537fe572a5eSAndre Przywara /* GICD_ITARGETSR is only used by GICv2. */
538fe572a5eSAndre Przywara static void test_targets(int nr_irqs)
539fe572a5eSAndre Przywara {
540fe572a5eSAndre Przywara 	void *targetsptr = gicv2_dist_base() + GICD_ITARGETSR;
541fe572a5eSAndre Przywara 	u32 orig_targets;
542fe572a5eSAndre Przywara 	u32 cpu_mask;
543fe572a5eSAndre Przywara 	u32 pattern, reg;
544fe572a5eSAndre Przywara 
545fe572a5eSAndre Przywara 	orig_targets = readl(targetsptr + GIC_FIRST_SPI);
546fe572a5eSAndre Przywara 	report_prefix_push("ITARGETSR");
547fe572a5eSAndre Przywara 
548fe572a5eSAndre Przywara 	cpu_mask = (1 << nr_cpus) - 1;
549fe572a5eSAndre Przywara 	cpu_mask |= cpu_mask << 8;
550fe572a5eSAndre Przywara 	cpu_mask |= cpu_mask << 16;
551fe572a5eSAndre Przywara 
552fe572a5eSAndre Przywara 	/* Check that bits for non implemented CPUs are RAZ/WI. */
553fe572a5eSAndre Przywara 	if (nr_cpus < 8) {
554fe572a5eSAndre Przywara 		writel(0xffffffff, targetsptr + GIC_FIRST_SPI);
555a299895bSThomas Huth 		report(!(readl(targetsptr + GIC_FIRST_SPI) & ~cpu_mask),
556a299895bSThomas Huth 		       "bits for non-existent CPUs masked");
5578e0a4f41SAndre Przywara 		report_info("%d non-existent CPUs", 8 - nr_cpus);
558fe572a5eSAndre Przywara 	} else {
559fe572a5eSAndre Przywara 		report_skip("CPU masking (all CPUs implemented)");
560fe572a5eSAndre Przywara 	}
561fe572a5eSAndre Przywara 
562a299895bSThomas Huth 	report(test_readonly_32(targetsptr + nr_irqs, true),
563a299895bSThomas Huth 	       "accesses beyond limit RAZ/WI");
564fe572a5eSAndre Przywara 
565fe572a5eSAndre Przywara 	pattern = 0x0103020f;
566fe572a5eSAndre Przywara 	writel(pattern, targetsptr + GIC_FIRST_SPI);
567fe572a5eSAndre Przywara 	reg = readl(targetsptr + GIC_FIRST_SPI);
568a299895bSThomas Huth 	report(reg == (pattern & cpu_mask), "register content preserved");
5698e0a4f41SAndre Przywara 	if (reg != (pattern & cpu_mask))
57046ca10f4SAlexandru Elisei 		report_info("writing %08"PRIx32" reads back as %08"PRIx32,
5718e0a4f41SAndre Przywara 			    pattern & cpu_mask, reg);
572fe572a5eSAndre Przywara 
573fe572a5eSAndre Przywara 	/* The TARGETS registers are byte accessible. */
574fe572a5eSAndre Przywara 	test_byte_access(targetsptr + GIC_FIRST_SPI, pattern, cpu_mask);
575fe572a5eSAndre Przywara 
576fe572a5eSAndre Przywara 	writel(orig_targets, targetsptr + GIC_FIRST_SPI);
577da5b8576SAndre Przywara 
578da5b8576SAndre Przywara 	report_prefix_pop();
579fe572a5eSAndre Przywara }
580fe572a5eSAndre Przywara 
58178ad7e95SAndre Przywara static void gic_test_mmio(void)
58278ad7e95SAndre Przywara {
58378ad7e95SAndre Przywara 	u32 reg;
58478ad7e95SAndre Przywara 	int nr_irqs;
58578ad7e95SAndre Przywara 	void *gic_dist_base, *idreg;
58678ad7e95SAndre Przywara 
58778ad7e95SAndre Przywara 	switch(gic_version()) {
58878ad7e95SAndre Przywara 	case 0x2:
58978ad7e95SAndre Przywara 		gic_dist_base = gicv2_dist_base();
59078ad7e95SAndre Przywara 		idreg = gic_dist_base + GICD_ICPIDR2;
59178ad7e95SAndre Przywara 		break;
59278ad7e95SAndre Przywara 	case 0x3:
59378ad7e95SAndre Przywara 		report_abort("GICv3 MMIO tests NYI");
59478ad7e95SAndre Przywara 	default:
59578ad7e95SAndre Przywara 		report_abort("GIC version %d not supported", gic_version());
59678ad7e95SAndre Przywara 	}
59778ad7e95SAndre Przywara 
59878ad7e95SAndre Przywara 	reg = readl(gic_dist_base + GICD_TYPER);
59978ad7e95SAndre Przywara 	nr_irqs = GICD_TYPER_IRQS(reg);
60078ad7e95SAndre Przywara 	report_info("number of implemented SPIs: %d", nr_irqs - GIC_FIRST_SPI);
60178ad7e95SAndre Przywara 
60278ad7e95SAndre Przywara 	test_typer_v2(reg);
60378ad7e95SAndre Przywara 
60446ca10f4SAlexandru Elisei 	report_info("IIDR: 0x%08"PRIx32, readl(gic_dist_base + GICD_IIDR));
60578ad7e95SAndre Przywara 
606a299895bSThomas Huth 	report(test_readonly_32(gic_dist_base + GICD_TYPER, false),
607a299895bSThomas Huth                "GICD_TYPER is read-only");
608a299895bSThomas Huth 	report(test_readonly_32(gic_dist_base + GICD_IIDR, false),
609a299895bSThomas Huth                "GICD_IIDR is read-only");
61078ad7e95SAndre Przywara 
61178ad7e95SAndre Przywara 	reg = readl(idreg);
612a299895bSThomas Huth 	report(test_readonly_32(idreg, false), "ICPIDR2 is read-only");
61346ca10f4SAlexandru Elisei 	report_info("value of ICPIDR2: 0x%08"PRIx32, reg);
614ff31a1c4SAndre Przywara 
615ff31a1c4SAndre Przywara 	test_priorities(nr_irqs, gic_dist_base + GICD_IPRIORITYR);
616fe572a5eSAndre Przywara 
617fe572a5eSAndre Przywara 	if (gic_version() == 2)
618fe572a5eSAndre Przywara 		test_targets(nr_irqs);
61978ad7e95SAndre Przywara }
62078ad7e95SAndre Przywara 
621ba74b106SEric Auger #if defined(__arm__)
622ba74b106SEric Auger 
623ba74b106SEric Auger static void test_its_introspection(void) {}
6240ef02cd6SEric Auger static void test_its_trigger(void) {}
62564260a5fSEric Auger static void test_its_migration(void) {}
626de582149SEric Auger static void test_its_pending_migration(void) {}
627de582149SEric Auger static void test_migrate_unmapped_collection(void) {}
628ba74b106SEric Auger 
629ba74b106SEric Auger #else /* __aarch64__ */
630ba74b106SEric Auger 
631ba74b106SEric Auger static void test_its_introspection(void)
632ba74b106SEric Auger {
633ba74b106SEric Auger 	struct its_baser *dev_baser = &its_data.device_baser;
634ba74b106SEric Auger 	struct its_baser *coll_baser = &its_data.coll_baser;
635ba74b106SEric Auger 	struct its_typer *typer = &its_data.typer;
636ba74b106SEric Auger 
637ba74b106SEric Auger 	if (!gicv3_its_base()) {
638ba74b106SEric Auger 		report_skip("No ITS, skip ...");
639ba74b106SEric Auger 		return;
640ba74b106SEric Auger 	}
641ba74b106SEric Auger 
642ba74b106SEric Auger 	/* IIDR */
643ba74b106SEric Auger 	report(test_readonly_32(gicv3_its_base() + GITS_IIDR, false),
644ba74b106SEric Auger 	       "GITS_IIDR is read-only"),
645ba74b106SEric Auger 
646ba74b106SEric Auger 	/* TYPER */
647ba74b106SEric Auger 	report(test_readonly_32(gicv3_its_base() + GITS_TYPER, false),
648ba74b106SEric Auger 	       "GITS_TYPER is read-only");
649ba74b106SEric Auger 
650ba74b106SEric Auger 	report(typer->phys_lpi, "ITS supports physical LPIs");
651ba74b106SEric Auger 	report_info("vLPI support: %s", typer->virt_lpi ? "yes" : "no");
652ba74b106SEric Auger 	report_info("ITT entry size = 0x%x", typer->ite_size);
653ba74b106SEric Auger 	report_info("Bit Count: EventID=%d DeviceId=%d CollId=%d",
654ba74b106SEric Auger 		    typer->eventid_bits, typer->deviceid_bits,
655ba74b106SEric Auger 		    typer->collid_bits);
656ba74b106SEric Auger 	report(typer->eventid_bits && typer->deviceid_bits &&
657ba74b106SEric Auger 	       typer->collid_bits, "ID spaces");
658ba74b106SEric Auger 	report_info("Target address format %s",
659ba74b106SEric Auger 			typer->pta ? "Redist base address" : "PE #");
660ba74b106SEric Auger 
661ba74b106SEric Auger 	report(dev_baser && coll_baser, "detect device and collection BASER");
662ba74b106SEric Auger 	report_info("device table entry_size = 0x%x", dev_baser->esz);
663ba74b106SEric Auger 	report_info("collection table entry_size = 0x%x", coll_baser->esz);
664ba74b106SEric Auger }
665ba74b106SEric Auger 
6660ef02cd6SEric Auger static int its_prerequisites(int nb_cpus)
6670ef02cd6SEric Auger {
6680ef02cd6SEric Auger 	int cpu;
6690ef02cd6SEric Auger 
6700ef02cd6SEric Auger 	if (!gicv3_its_base()) {
6710ef02cd6SEric Auger 		report_skip("No ITS, skip ...");
6720ef02cd6SEric Auger 		return -1;
6730ef02cd6SEric Auger 	}
6740ef02cd6SEric Auger 
6750ef02cd6SEric Auger 	if (nr_cpus < nb_cpus) {
6760ef02cd6SEric Auger 		report_skip("Test requires at least %d vcpus", nb_cpus);
6770ef02cd6SEric Auger 		return -1;
6780ef02cd6SEric Auger 	}
6790ef02cd6SEric Auger 
6800ef02cd6SEric Auger 	stats_reset();
6810ef02cd6SEric Auger 
6820ef02cd6SEric Auger 	setup_irq(lpi_handler);
6830ef02cd6SEric Auger 
6840ef02cd6SEric Auger 	for_each_present_cpu(cpu) {
6850ef02cd6SEric Auger 		if (cpu == 0)
6860ef02cd6SEric Auger 			continue;
6870ef02cd6SEric Auger 		smp_boot_secondary(cpu, secondary_lpi_test);
6880ef02cd6SEric Auger 	}
6890ef02cd6SEric Auger 	wait_on_ready();
6900ef02cd6SEric Auger 
6910ef02cd6SEric Auger 	its_enable_defaults();
6920ef02cd6SEric Auger 
6930ef02cd6SEric Auger 	return 0;
6940ef02cd6SEric Auger }
6950ef02cd6SEric Auger 
69664260a5fSEric Auger /*
69764260a5fSEric Auger  * Setup the configuration for those mappings:
69864260a5fSEric Auger  * dev_id=2 event=20 -> vcpu 3, intid=8195
69964260a5fSEric Auger  * dev_id=7 event=255 -> vcpu 2, intid=8196
70064260a5fSEric Auger  * LPIs ready to hit
70164260a5fSEric Auger  */
70264260a5fSEric Auger static int its_setup1(void)
7030ef02cd6SEric Auger {
7040ef02cd6SEric Auger 	struct its_collection *col3, *col2;
7050ef02cd6SEric Auger 	struct its_device *dev2, *dev7;
7060ef02cd6SEric Auger 
7070ef02cd6SEric Auger 	if (its_prerequisites(4))
70864260a5fSEric Auger 		return -1;
7090ef02cd6SEric Auger 
7100ef02cd6SEric Auger 	dev2 = its_create_device(2 /* dev id */, 8 /* nb_ites */);
7110ef02cd6SEric Auger 	dev7 = its_create_device(7 /* dev id */, 8 /* nb_ites */);
7120ef02cd6SEric Auger 
7130ef02cd6SEric Auger 	col3 = its_create_collection(3 /* col id */, 3/* target PE */);
7140ef02cd6SEric Auger 	col2 = its_create_collection(2 /* col id */, 2/* target PE */);
7150ef02cd6SEric Auger 
7160ef02cd6SEric Auger 	gicv3_lpi_set_config(8195, LPI_PROP_DEFAULT);
7170ef02cd6SEric Auger 	gicv3_lpi_set_config(8196, LPI_PROP_DEFAULT);
7180ef02cd6SEric Auger 
7190ef02cd6SEric Auger 	/*
7200ef02cd6SEric Auger 	 * dev=2, eventid=20  -> lpi= 8195, col=3
7210ef02cd6SEric Auger 	 * dev=7, eventid=255 -> lpi= 8196, col=2
7220ef02cd6SEric Auger 	 */
7230ef02cd6SEric Auger 	its_send_mapd(dev2, true);
7240ef02cd6SEric Auger 	its_send_mapd(dev7, true);
7250ef02cd6SEric Auger 
7260ef02cd6SEric Auger 	its_send_mapc(col3, true);
7270ef02cd6SEric Auger 	its_send_mapc(col2, true);
7280ef02cd6SEric Auger 
7290ef02cd6SEric Auger 	its_send_invall(col2);
7300ef02cd6SEric Auger 	its_send_invall(col3);
7310ef02cd6SEric Auger 
7320ef02cd6SEric Auger 	its_send_mapti(dev2, 8195 /* lpi id */, 20 /* event id */, col3);
7330ef02cd6SEric Auger 	its_send_mapti(dev7, 8196 /* lpi id */, 255 /* event id */, col2);
73464260a5fSEric Auger 	return 0;
73564260a5fSEric Auger }
73664260a5fSEric Auger 
73764260a5fSEric Auger static void test_its_trigger(void)
73864260a5fSEric Auger {
73964260a5fSEric Auger 	struct its_collection *col3;
74064260a5fSEric Auger 	struct its_device *dev2, *dev7;
74164260a5fSEric Auger 
74264260a5fSEric Auger 	if (its_setup1())
74364260a5fSEric Auger 		return;
74464260a5fSEric Auger 
74564260a5fSEric Auger 	col3 = its_get_collection(3);
74664260a5fSEric Auger 	dev2 = its_get_device(2);
74764260a5fSEric Auger 	dev7 = its_get_device(7);
74864260a5fSEric Auger 
74964260a5fSEric Auger 	report_prefix_push("int");
7500ef02cd6SEric Auger 
7510ef02cd6SEric Auger 	lpi_stats_expect(3, 8195);
7520ef02cd6SEric Auger 	its_send_int(dev2, 20);
7530ef02cd6SEric Auger 	check_lpi_stats("dev=2, eventid=20  -> lpi= 8195, col=3");
7540ef02cd6SEric Auger 
7550ef02cd6SEric Auger 	lpi_stats_expect(2, 8196);
7560ef02cd6SEric Auger 	its_send_int(dev7, 255);
7570ef02cd6SEric Auger 	check_lpi_stats("dev=7, eventid=255 -> lpi= 8196, col=2");
7580ef02cd6SEric Auger 
7590ef02cd6SEric Auger 	report_prefix_pop();
7600ef02cd6SEric Auger 
7610ef02cd6SEric Auger 	report_prefix_push("inv/invall");
7620ef02cd6SEric Auger 
7630ef02cd6SEric Auger 	/*
7640ef02cd6SEric Auger 	 * disable 8195, check dev2/eventid=20 does not trigger the
7650ef02cd6SEric Auger 	 * corresponding LPI
7660ef02cd6SEric Auger 	 */
7670ef02cd6SEric Auger 	gicv3_lpi_set_config(8195, LPI_PROP_DEFAULT & ~LPI_PROP_ENABLED);
7680ef02cd6SEric Auger 	its_send_inv(dev2, 20);
7690ef02cd6SEric Auger 
7700ef02cd6SEric Auger 	lpi_stats_expect(-1, -1);
7710ef02cd6SEric Auger 	its_send_int(dev2, 20);
7720ef02cd6SEric Auger 	check_lpi_stats("dev2/eventid=20 does not trigger any LPI");
7730ef02cd6SEric Auger 
7740ef02cd6SEric Auger 	/*
7750ef02cd6SEric Auger 	 * re-enable the LPI but willingly do not call invall
7760ef02cd6SEric Auger 	 * so the change in config is not taken into account.
7770ef02cd6SEric Auger 	 * The LPI should not hit
7780ef02cd6SEric Auger 	 */
7790ef02cd6SEric Auger 	gicv3_lpi_set_config(8195, LPI_PROP_DEFAULT);
7800ef02cd6SEric Auger 	lpi_stats_expect(-1, -1);
7810ef02cd6SEric Auger 	its_send_int(dev2, 20);
7820ef02cd6SEric Auger 	check_lpi_stats("dev2/eventid=20 still does not trigger any LPI");
7830ef02cd6SEric Auger 
7840ef02cd6SEric Auger 	/* Now call the invall and check the LPI hits */
7850ef02cd6SEric Auger 	its_send_invall(col3);
7860ef02cd6SEric Auger 	lpi_stats_expect(3, 8195);
7870ef02cd6SEric Auger 	its_send_int(dev2, 20);
7880ef02cd6SEric Auger 	check_lpi_stats("dev2/eventid=20 now triggers an LPI");
7890ef02cd6SEric Auger 
7900ef02cd6SEric Auger 	report_prefix_pop();
7910ef02cd6SEric Auger 
7920ef02cd6SEric Auger 	report_prefix_push("mapd valid=false");
7930ef02cd6SEric Auger 	/*
7940ef02cd6SEric Auger 	 * Unmap device 2 and check the eventid 20 formerly
7950ef02cd6SEric Auger 	 * attached to it does not hit anymore
7960ef02cd6SEric Auger 	 */
7970ef02cd6SEric Auger 
7980ef02cd6SEric Auger 	its_send_mapd(dev2, false);
7990ef02cd6SEric Auger 	lpi_stats_expect(-1, -1);
8000ef02cd6SEric Auger 	its_send_int(dev2, 20);
8010ef02cd6SEric Auger 	check_lpi_stats("no LPI after device unmap");
8020ef02cd6SEric Auger 	report_prefix_pop();
8030ef02cd6SEric Auger }
80464260a5fSEric Auger 
80564260a5fSEric Auger static void test_its_migration(void)
80664260a5fSEric Auger {
80764260a5fSEric Auger 	struct its_device *dev2, *dev7;
80864260a5fSEric Auger 	bool test_skipped = false;
80964260a5fSEric Auger 
81064260a5fSEric Auger 	if (its_setup1()) {
81164260a5fSEric Auger 		test_skipped = true;
81264260a5fSEric Auger 		goto do_migrate;
81364260a5fSEric Auger 	}
81464260a5fSEric Auger 
81564260a5fSEric Auger 	dev2 = its_get_device(2);
81664260a5fSEric Auger 	dev7 = its_get_device(7);
81764260a5fSEric Auger 
81864260a5fSEric Auger do_migrate:
81964260a5fSEric Auger 	puts("Now migrate the VM, then press a key to continue...\n");
82064260a5fSEric Auger 	(void)getchar();
82164260a5fSEric Auger 	report_info("Migration complete");
82264260a5fSEric Auger 	if (test_skipped)
82364260a5fSEric Auger 		return;
82464260a5fSEric Auger 
82564260a5fSEric Auger 	lpi_stats_expect(3, 8195);
82664260a5fSEric Auger 	its_send_int(dev2, 20);
82764260a5fSEric Auger 	check_lpi_stats("dev2/eventid=20 triggers LPI 8195 on PE #3 after migration");
82864260a5fSEric Auger 
82964260a5fSEric Auger 	lpi_stats_expect(2, 8196);
83064260a5fSEric Auger 	its_send_int(dev7, 255);
83164260a5fSEric Auger 	check_lpi_stats("dev7/eventid=255 triggers LPI 8196 on PE #2 after migration");
83264260a5fSEric Auger }
833de582149SEric Auger 
834de582149SEric Auger #define ERRATA_UNMAPPED_COLLECTIONS "ERRATA_8c58be34494b"
835de582149SEric Auger 
836de582149SEric Auger static void test_migrate_unmapped_collection(void)
837de582149SEric Auger {
838de582149SEric Auger 	struct its_collection *col = NULL;
839de582149SEric Auger 	struct its_device *dev2 = NULL, *dev7 = NULL;
840de582149SEric Auger 	bool test_skipped = false;
841de582149SEric Auger 	int pe0 = 0;
842de582149SEric Auger 	u8 config;
843de582149SEric Auger 
844de582149SEric Auger 	if (its_setup1()) {
845de582149SEric Auger 		test_skipped = true;
846de582149SEric Auger 		goto do_migrate;
847de582149SEric Auger 	}
848de582149SEric Auger 
849de582149SEric Auger 	if (!errata(ERRATA_UNMAPPED_COLLECTIONS)) {
850de582149SEric Auger 		report_skip("Skipping test, as this test hangs without the fix. "
851de582149SEric Auger 			    "Set %s=y to enable.", ERRATA_UNMAPPED_COLLECTIONS);
852de582149SEric Auger 		test_skipped = true;
853de582149SEric Auger 		goto do_migrate;
854de582149SEric Auger 	}
855de582149SEric Auger 
856de582149SEric Auger 	col = its_create_collection(pe0, pe0);
857de582149SEric Auger 	dev2 = its_get_device(2);
858de582149SEric Auger 	dev7 = its_get_device(7);
859de582149SEric Auger 
860de582149SEric Auger 	/* MAPTI with the collection unmapped */
861de582149SEric Auger 	its_send_mapti(dev2, 8192, 0, col);
862de582149SEric Auger 	gicv3_lpi_set_config(8192, LPI_PROP_DEFAULT);
863de582149SEric Auger 
864de582149SEric Auger do_migrate:
865de582149SEric Auger 	puts("Now migrate the VM, then press a key to continue...\n");
866de582149SEric Auger 	(void)getchar();
867de582149SEric Auger 	report_info("Migration complete");
868de582149SEric Auger 	if (test_skipped)
869de582149SEric Auger 		return;
870de582149SEric Auger 
871de582149SEric Auger 	/* on the destination, map the collection */
872de582149SEric Auger 	its_send_mapc(col, true);
873de582149SEric Auger 	its_send_invall(col);
874de582149SEric Auger 
875de582149SEric Auger 	lpi_stats_expect(2, 8196);
876de582149SEric Auger 	its_send_int(dev7, 255);
877de582149SEric Auger 	check_lpi_stats("dev7/eventid= 255 triggered LPI 8196 on PE #2");
878de582149SEric Auger 
879de582149SEric Auger 	config = gicv3_lpi_get_config(8192);
880de582149SEric Auger 	report(config == LPI_PROP_DEFAULT,
881de582149SEric Auger 	       "Config of LPI 8192 was properly migrated");
882de582149SEric Auger 
883de582149SEric Auger 	lpi_stats_expect(pe0, 8192);
884de582149SEric Auger 	its_send_int(dev2, 0);
885de582149SEric Auger 	check_lpi_stats("dev2/eventid = 0 triggered LPI 8192 on PE0");
886de582149SEric Auger }
887de582149SEric Auger 
888de582149SEric Auger static void test_its_pending_migration(void)
889de582149SEric Auger {
890de582149SEric Auger 	struct its_device *dev;
891de582149SEric Auger 	struct its_collection *collection[2];
892de582149SEric Auger 	int *expected = calloc(nr_cpus, sizeof(int));
893de582149SEric Auger 	int pe0 = nr_cpus - 1, pe1 = nr_cpus - 2;
894de582149SEric Auger 	bool test_skipped = false;
895de582149SEric Auger 	u64 pendbaser;
896de582149SEric Auger 	void *ptr;
897de582149SEric Auger 	int i;
898de582149SEric Auger 
899de582149SEric Auger 	if (its_prerequisites(4)) {
900de582149SEric Auger 		test_skipped = true;
901de582149SEric Auger 		goto do_migrate;
902de582149SEric Auger 	}
903de582149SEric Auger 
904de582149SEric Auger 	dev = its_create_device(2 /* dev id */, 8 /* nb_ites */);
905de582149SEric Auger 	its_send_mapd(dev, true);
906de582149SEric Auger 
907de582149SEric Auger 	collection[0] = its_create_collection(pe0, pe0);
908de582149SEric Auger 	collection[1] = its_create_collection(pe1, pe1);
909de582149SEric Auger 	its_send_mapc(collection[0], true);
910de582149SEric Auger 	its_send_mapc(collection[1], true);
911de582149SEric Auger 
912de582149SEric Auger 	/* disable lpi at redist level */
913de582149SEric Auger 	gicv3_lpi_rdist_disable(pe0);
914de582149SEric Auger 	gicv3_lpi_rdist_disable(pe1);
915de582149SEric Auger 
916de582149SEric Auger 	/* lpis are interleaved inbetween the 2 PEs */
917de582149SEric Auger 	for (i = 0; i < 256; i++) {
918de582149SEric Auger 		struct its_collection *col = i % 2 ? collection[0] :
919de582149SEric Auger 						     collection[1];
920de582149SEric Auger 		int vcpu = col->target_address >> 16;
921de582149SEric Auger 
922de582149SEric Auger 		its_send_mapti(dev, LPI(i), i, col);
923de582149SEric Auger 		gicv3_lpi_set_config(LPI(i), LPI_PROP_DEFAULT);
924de582149SEric Auger 		gicv3_lpi_set_clr_pending(vcpu, LPI(i), true);
925de582149SEric Auger 	}
926de582149SEric Auger 	its_send_invall(collection[0]);
927de582149SEric Auger 	its_send_invall(collection[1]);
928de582149SEric Auger 
929de582149SEric Auger 	/* Clear the PTZ bit on each pendbaser */
930de582149SEric Auger 
931de582149SEric Auger 	expected[pe0] = 128;
932de582149SEric Auger 	expected[pe1] = 128;
933de582149SEric Auger 
934de582149SEric Auger 	ptr = gicv3_data.redist_base[pe0] + GICR_PENDBASER;
935de582149SEric Auger 	pendbaser = readq(ptr);
936de582149SEric Auger 	writeq(pendbaser & ~GICR_PENDBASER_PTZ, ptr);
937de582149SEric Auger 
938de582149SEric Auger 	ptr = gicv3_data.redist_base[pe1] + GICR_PENDBASER;
939de582149SEric Auger 	pendbaser = readq(ptr);
940de582149SEric Auger 	writeq(pendbaser & ~GICR_PENDBASER_PTZ, ptr);
941de582149SEric Auger 
942de582149SEric Auger 	gicv3_lpi_rdist_enable(pe0);
943de582149SEric Auger 	gicv3_lpi_rdist_enable(pe1);
944de582149SEric Auger 
945de582149SEric Auger do_migrate:
946de582149SEric Auger 	puts("Now migrate the VM, then press a key to continue...\n");
947de582149SEric Auger 	(void)getchar();
948de582149SEric Auger 	report_info("Migration complete");
949de582149SEric Auger 	if (test_skipped)
950de582149SEric Auger 		return;
951de582149SEric Auger 
952de582149SEric Auger 	/* let's wait for the 256 LPIs to be handled */
953de582149SEric Auger 	mdelay(1000);
954de582149SEric Auger 
955de582149SEric Auger 	check_lpi_hits(expected, "128 LPIs on both PE0 and PE1 after migration");
956de582149SEric Auger }
957ba74b106SEric Auger #endif
958ba74b106SEric Auger 
959ac4a67b6SAndrew Jones int main(int argc, char **argv)
960ac4a67b6SAndrew Jones {
9612e2d471dSAndrew Jones 	if (!gic_init()) {
962ac4a67b6SAndrew Jones 		printf("No supported gic present, skipping tests...\n");
963ac4a67b6SAndrew Jones 		return report_summary();
964ac4a67b6SAndrew Jones 	}
965ac4a67b6SAndrew Jones 
9662b19b829SAndrew Jones 	report_prefix_pushf("gicv%d", gic_version());
967ac4a67b6SAndrew Jones 
9682e2d471dSAndrew Jones 	switch (gic_version()) {
9692e2d471dSAndrew Jones 	case 2:
9702e2d471dSAndrew Jones 		gic = &gicv2;
9712e2d471dSAndrew Jones 		break;
9722e2d471dSAndrew Jones 	case 3:
9732e2d471dSAndrew Jones 		gic = &gicv3;
9742e2d471dSAndrew Jones 		break;
9752e2d471dSAndrew Jones 	}
9762e2d471dSAndrew Jones 
977ac4a67b6SAndrew Jones 	if (argc < 2)
978ac4a67b6SAndrew Jones 		report_abort("no test specified");
979ac4a67b6SAndrew Jones 
980ac4a67b6SAndrew Jones 	if (strcmp(argv[1], "ipi") == 0) {
981ac4a67b6SAndrew Jones 		report_prefix_push(argv[1]);
982ac4a67b6SAndrew Jones 		nr_cpu_check(2);
98300b34f56SAndrew Jones 		on_cpus(ipi_test, NULL);
984c152d8bcSChristoffer Dall 	} else if (strcmp(argv[1], "active") == 0) {
985c152d8bcSChristoffer Dall 		run_active_clear_test();
98678ad7e95SAndre Przywara 	} else if (strcmp(argv[1], "mmio") == 0) {
98778ad7e95SAndre Przywara 		report_prefix_push(argv[1]);
98878ad7e95SAndre Przywara 		gic_test_mmio();
98978ad7e95SAndre Przywara 		report_prefix_pop();
9900ef02cd6SEric Auger 	} else if (!strcmp(argv[1], "its-trigger")) {
9910ef02cd6SEric Auger 		report_prefix_push(argv[1]);
9920ef02cd6SEric Auger 		test_its_trigger();
9930ef02cd6SEric Auger 		report_prefix_pop();
99464260a5fSEric Auger 	} else if (!strcmp(argv[1], "its-migration")) {
99564260a5fSEric Auger 		report_prefix_push(argv[1]);
99664260a5fSEric Auger 		test_its_migration();
99764260a5fSEric Auger 		report_prefix_pop();
998de582149SEric Auger 	} else if (!strcmp(argv[1], "its-pending-migration")) {
999de582149SEric Auger 		report_prefix_push(argv[1]);
1000de582149SEric Auger 		test_its_pending_migration();
1001de582149SEric Auger 		report_prefix_pop();
1002de582149SEric Auger 	} else if (!strcmp(argv[1], "its-migrate-unmapped-collection")) {
1003de582149SEric Auger 		report_prefix_push(argv[1]);
1004de582149SEric Auger 		test_migrate_unmapped_collection();
1005de582149SEric Auger 		report_prefix_pop();
1006ba74b106SEric Auger 	} else if (strcmp(argv[1], "its-introspection") == 0) {
1007ba74b106SEric Auger 		report_prefix_push(argv[1]);
1008ba74b106SEric Auger 		test_its_introspection();
1009ba74b106SEric Auger 		report_prefix_pop();
1010ac4a67b6SAndrew Jones 	} else {
1011ac4a67b6SAndrew Jones 		report_abort("Unknown subtest '%s'", argv[1]);
1012ac4a67b6SAndrew Jones 	}
1013ac4a67b6SAndrew Jones 
1014ac4a67b6SAndrew Jones 	return report_summary();
1015ac4a67b6SAndrew Jones }
1016