xref: /linux/tools/testing/selftests/kvm/arm64/vgic_lpi_stress.c (revision 1260ed77798502de9c98020040d2995008de10cc)
1*67730e6cSSean Christopherson // SPDX-License-Identifier: GPL-2.0
2*67730e6cSSean Christopherson /*
3*67730e6cSSean Christopherson  * vgic_lpi_stress - Stress test for KVM's ITS emulation
4*67730e6cSSean Christopherson  *
5*67730e6cSSean Christopherson  * Copyright (c) 2024 Google LLC
6*67730e6cSSean Christopherson  */
7*67730e6cSSean Christopherson 
8*67730e6cSSean Christopherson #include <linux/sizes.h>
9*67730e6cSSean Christopherson #include <pthread.h>
10*67730e6cSSean Christopherson #include <stdatomic.h>
11*67730e6cSSean Christopherson #include <sys/sysinfo.h>
12*67730e6cSSean Christopherson 
13*67730e6cSSean Christopherson #include "kvm_util.h"
14*67730e6cSSean Christopherson #include "gic.h"
15*67730e6cSSean Christopherson #include "gic_v3.h"
16*67730e6cSSean Christopherson #include "gic_v3_its.h"
17*67730e6cSSean Christopherson #include "processor.h"
18*67730e6cSSean Christopherson #include "ucall.h"
19*67730e6cSSean Christopherson #include "vgic.h"
20*67730e6cSSean Christopherson 
21*67730e6cSSean Christopherson #define TEST_MEMSLOT_INDEX	1
22*67730e6cSSean Christopherson 
23*67730e6cSSean Christopherson #define GIC_LPI_OFFSET	8192
24*67730e6cSSean Christopherson 
25*67730e6cSSean Christopherson static size_t nr_iterations = 1000;
26*67730e6cSSean Christopherson static vm_paddr_t gpa_base;
27*67730e6cSSean Christopherson 
28*67730e6cSSean Christopherson static struct kvm_vm *vm;
29*67730e6cSSean Christopherson static struct kvm_vcpu **vcpus;
30*67730e6cSSean Christopherson static int gic_fd, its_fd;
31*67730e6cSSean Christopherson 
32*67730e6cSSean Christopherson static struct test_data {
33*67730e6cSSean Christopherson 	bool		request_vcpus_stop;
34*67730e6cSSean Christopherson 	u32		nr_cpus;
35*67730e6cSSean Christopherson 	u32		nr_devices;
36*67730e6cSSean Christopherson 	u32		nr_event_ids;
37*67730e6cSSean Christopherson 
38*67730e6cSSean Christopherson 	vm_paddr_t	device_table;
39*67730e6cSSean Christopherson 	vm_paddr_t	collection_table;
40*67730e6cSSean Christopherson 	vm_paddr_t	cmdq_base;
41*67730e6cSSean Christopherson 	void		*cmdq_base_va;
42*67730e6cSSean Christopherson 	vm_paddr_t	itt_tables;
43*67730e6cSSean Christopherson 
44*67730e6cSSean Christopherson 	vm_paddr_t	lpi_prop_table;
45*67730e6cSSean Christopherson 	vm_paddr_t	lpi_pend_tables;
46*67730e6cSSean Christopherson } test_data =  {
47*67730e6cSSean Christopherson 	.nr_cpus	= 1,
48*67730e6cSSean Christopherson 	.nr_devices	= 1,
49*67730e6cSSean Christopherson 	.nr_event_ids	= 16,
50*67730e6cSSean Christopherson };
51*67730e6cSSean Christopherson 
52*67730e6cSSean Christopherson static void guest_irq_handler(struct ex_regs *regs)
53*67730e6cSSean Christopherson {
54*67730e6cSSean Christopherson 	u32 intid = gic_get_and_ack_irq();
55*67730e6cSSean Christopherson 
56*67730e6cSSean Christopherson 	if (intid == IAR_SPURIOUS)
57*67730e6cSSean Christopherson 		return;
58*67730e6cSSean Christopherson 
59*67730e6cSSean Christopherson 	GUEST_ASSERT(intid >= GIC_LPI_OFFSET);
60*67730e6cSSean Christopherson 	gic_set_eoi(intid);
61*67730e6cSSean Christopherson }
62*67730e6cSSean Christopherson 
63*67730e6cSSean Christopherson static void guest_setup_its_mappings(void)
64*67730e6cSSean Christopherson {
65*67730e6cSSean Christopherson 	u32 coll_id, device_id, event_id, intid = GIC_LPI_OFFSET;
66*67730e6cSSean Christopherson 	u32 nr_events = test_data.nr_event_ids;
67*67730e6cSSean Christopherson 	u32 nr_devices = test_data.nr_devices;
68*67730e6cSSean Christopherson 	u32 nr_cpus = test_data.nr_cpus;
69*67730e6cSSean Christopherson 
70*67730e6cSSean Christopherson 	for (coll_id = 0; coll_id < nr_cpus; coll_id++)
71*67730e6cSSean Christopherson 		its_send_mapc_cmd(test_data.cmdq_base_va, coll_id, coll_id, true);
72*67730e6cSSean Christopherson 
73*67730e6cSSean Christopherson 	/* Round-robin the LPIs to all of the vCPUs in the VM */
74*67730e6cSSean Christopherson 	coll_id = 0;
75*67730e6cSSean Christopherson 	for (device_id = 0; device_id < nr_devices; device_id++) {
76*67730e6cSSean Christopherson 		vm_paddr_t itt_base = test_data.itt_tables + (device_id * SZ_64K);
77*67730e6cSSean Christopherson 
78*67730e6cSSean Christopherson 		its_send_mapd_cmd(test_data.cmdq_base_va, device_id,
79*67730e6cSSean Christopherson 				  itt_base, SZ_64K, true);
80*67730e6cSSean Christopherson 
81*67730e6cSSean Christopherson 		for (event_id = 0; event_id < nr_events; event_id++) {
82*67730e6cSSean Christopherson 			its_send_mapti_cmd(test_data.cmdq_base_va, device_id,
83*67730e6cSSean Christopherson 					   event_id, coll_id, intid++);
84*67730e6cSSean Christopherson 
85*67730e6cSSean Christopherson 			coll_id = (coll_id + 1) % test_data.nr_cpus;
86*67730e6cSSean Christopherson 		}
87*67730e6cSSean Christopherson 	}
88*67730e6cSSean Christopherson }
89*67730e6cSSean Christopherson 
90*67730e6cSSean Christopherson static void guest_invalidate_all_rdists(void)
91*67730e6cSSean Christopherson {
92*67730e6cSSean Christopherson 	int i;
93*67730e6cSSean Christopherson 
94*67730e6cSSean Christopherson 	for (i = 0; i < test_data.nr_cpus; i++)
95*67730e6cSSean Christopherson 		its_send_invall_cmd(test_data.cmdq_base_va, i);
96*67730e6cSSean Christopherson }
97*67730e6cSSean Christopherson 
98*67730e6cSSean Christopherson static void guest_setup_gic(void)
99*67730e6cSSean Christopherson {
100*67730e6cSSean Christopherson 	static atomic_int nr_cpus_ready = 0;
101*67730e6cSSean Christopherson 	u32 cpuid = guest_get_vcpuid();
102*67730e6cSSean Christopherson 
103*67730e6cSSean Christopherson 	gic_init(GIC_V3, test_data.nr_cpus);
104*67730e6cSSean Christopherson 	gic_rdist_enable_lpis(test_data.lpi_prop_table, SZ_64K,
105*67730e6cSSean Christopherson 			      test_data.lpi_pend_tables + (cpuid * SZ_64K));
106*67730e6cSSean Christopherson 
107*67730e6cSSean Christopherson 	atomic_fetch_add(&nr_cpus_ready, 1);
108*67730e6cSSean Christopherson 
109*67730e6cSSean Christopherson 	if (cpuid > 0)
110*67730e6cSSean Christopherson 		return;
111*67730e6cSSean Christopherson 
112*67730e6cSSean Christopherson 	while (atomic_load(&nr_cpus_ready) < test_data.nr_cpus)
113*67730e6cSSean Christopherson 		cpu_relax();
114*67730e6cSSean Christopherson 
115*67730e6cSSean Christopherson 	its_init(test_data.collection_table, SZ_64K,
116*67730e6cSSean Christopherson 		 test_data.device_table, SZ_64K,
117*67730e6cSSean Christopherson 		 test_data.cmdq_base, SZ_64K);
118*67730e6cSSean Christopherson 
119*67730e6cSSean Christopherson 	guest_setup_its_mappings();
120*67730e6cSSean Christopherson 	guest_invalidate_all_rdists();
121*67730e6cSSean Christopherson }
122*67730e6cSSean Christopherson 
123*67730e6cSSean Christopherson static void guest_code(size_t nr_lpis)
124*67730e6cSSean Christopherson {
125*67730e6cSSean Christopherson 	guest_setup_gic();
126*67730e6cSSean Christopherson 
127*67730e6cSSean Christopherson 	GUEST_SYNC(0);
128*67730e6cSSean Christopherson 
129*67730e6cSSean Christopherson 	/*
130*67730e6cSSean Christopherson 	 * Don't use WFI here to avoid blocking the vCPU thread indefinitely and
131*67730e6cSSean Christopherson 	 * never getting the stop signal.
132*67730e6cSSean Christopherson 	 */
133*67730e6cSSean Christopherson 	while (!READ_ONCE(test_data.request_vcpus_stop))
134*67730e6cSSean Christopherson 		cpu_relax();
135*67730e6cSSean Christopherson 
136*67730e6cSSean Christopherson 	GUEST_DONE();
137*67730e6cSSean Christopherson }
138*67730e6cSSean Christopherson 
139*67730e6cSSean Christopherson static void setup_memslot(void)
140*67730e6cSSean Christopherson {
141*67730e6cSSean Christopherson 	size_t pages;
142*67730e6cSSean Christopherson 	size_t sz;
143*67730e6cSSean Christopherson 
144*67730e6cSSean Christopherson 	/*
145*67730e6cSSean Christopherson 	 * For the ITS:
146*67730e6cSSean Christopherson 	 *  - A single level device table
147*67730e6cSSean Christopherson 	 *  - A single level collection table
148*67730e6cSSean Christopherson 	 *  - The command queue
149*67730e6cSSean Christopherson 	 *  - An ITT for each device
150*67730e6cSSean Christopherson 	 */
151*67730e6cSSean Christopherson 	sz = (3 + test_data.nr_devices) * SZ_64K;
152*67730e6cSSean Christopherson 
153*67730e6cSSean Christopherson 	/*
154*67730e6cSSean Christopherson 	 * For the redistributors:
155*67730e6cSSean Christopherson 	 *  - A shared LPI configuration table
156*67730e6cSSean Christopherson 	 *  - An LPI pending table for each vCPU
157*67730e6cSSean Christopherson 	 */
158*67730e6cSSean Christopherson 	sz += (1 + test_data.nr_cpus) * SZ_64K;
159*67730e6cSSean Christopherson 
160*67730e6cSSean Christopherson 	pages = sz / vm->page_size;
161*67730e6cSSean Christopherson 	gpa_base = ((vm_compute_max_gfn(vm) + 1) * vm->page_size) - sz;
162*67730e6cSSean Christopherson 	vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, gpa_base,
163*67730e6cSSean Christopherson 				    TEST_MEMSLOT_INDEX, pages, 0);
164*67730e6cSSean Christopherson }
165*67730e6cSSean Christopherson 
166*67730e6cSSean Christopherson #define LPI_PROP_DEFAULT_PRIO	0xa0
167*67730e6cSSean Christopherson 
168*67730e6cSSean Christopherson static void configure_lpis(void)
169*67730e6cSSean Christopherson {
170*67730e6cSSean Christopherson 	size_t nr_lpis = test_data.nr_devices * test_data.nr_event_ids;
171*67730e6cSSean Christopherson 	u8 *tbl = addr_gpa2hva(vm, test_data.lpi_prop_table);
172*67730e6cSSean Christopherson 	size_t i;
173*67730e6cSSean Christopherson 
174*67730e6cSSean Christopherson 	for (i = 0; i < nr_lpis; i++) {
175*67730e6cSSean Christopherson 		tbl[i] = LPI_PROP_DEFAULT_PRIO |
176*67730e6cSSean Christopherson 			 LPI_PROP_GROUP1 |
177*67730e6cSSean Christopherson 			 LPI_PROP_ENABLED;
178*67730e6cSSean Christopherson 	}
179*67730e6cSSean Christopherson }
180*67730e6cSSean Christopherson 
181*67730e6cSSean Christopherson static void setup_test_data(void)
182*67730e6cSSean Christopherson {
183*67730e6cSSean Christopherson 	size_t pages_per_64k = vm_calc_num_guest_pages(vm->mode, SZ_64K);
184*67730e6cSSean Christopherson 	u32 nr_devices = test_data.nr_devices;
185*67730e6cSSean Christopherson 	u32 nr_cpus = test_data.nr_cpus;
186*67730e6cSSean Christopherson 	vm_paddr_t cmdq_base;
187*67730e6cSSean Christopherson 
188*67730e6cSSean Christopherson 	test_data.device_table = vm_phy_pages_alloc(vm, pages_per_64k,
189*67730e6cSSean Christopherson 						    gpa_base,
190*67730e6cSSean Christopherson 						    TEST_MEMSLOT_INDEX);
191*67730e6cSSean Christopherson 
192*67730e6cSSean Christopherson 	test_data.collection_table = vm_phy_pages_alloc(vm, pages_per_64k,
193*67730e6cSSean Christopherson 							gpa_base,
194*67730e6cSSean Christopherson 							TEST_MEMSLOT_INDEX);
195*67730e6cSSean Christopherson 
196*67730e6cSSean Christopherson 	cmdq_base = vm_phy_pages_alloc(vm, pages_per_64k, gpa_base,
197*67730e6cSSean Christopherson 				       TEST_MEMSLOT_INDEX);
198*67730e6cSSean Christopherson 	virt_map(vm, cmdq_base, cmdq_base, pages_per_64k);
199*67730e6cSSean Christopherson 	test_data.cmdq_base = cmdq_base;
200*67730e6cSSean Christopherson 	test_data.cmdq_base_va = (void *)cmdq_base;
201*67730e6cSSean Christopherson 
202*67730e6cSSean Christopherson 	test_data.itt_tables = vm_phy_pages_alloc(vm, pages_per_64k * nr_devices,
203*67730e6cSSean Christopherson 						  gpa_base, TEST_MEMSLOT_INDEX);
204*67730e6cSSean Christopherson 
205*67730e6cSSean Christopherson 	test_data.lpi_prop_table = vm_phy_pages_alloc(vm, pages_per_64k,
206*67730e6cSSean Christopherson 						      gpa_base, TEST_MEMSLOT_INDEX);
207*67730e6cSSean Christopherson 	configure_lpis();
208*67730e6cSSean Christopherson 
209*67730e6cSSean Christopherson 	test_data.lpi_pend_tables = vm_phy_pages_alloc(vm, pages_per_64k * nr_cpus,
210*67730e6cSSean Christopherson 						       gpa_base, TEST_MEMSLOT_INDEX);
211*67730e6cSSean Christopherson 
212*67730e6cSSean Christopherson 	sync_global_to_guest(vm, test_data);
213*67730e6cSSean Christopherson }
214*67730e6cSSean Christopherson 
215*67730e6cSSean Christopherson static void setup_gic(void)
216*67730e6cSSean Christopherson {
217*67730e6cSSean Christopherson 	gic_fd = vgic_v3_setup(vm, test_data.nr_cpus, 64);
218*67730e6cSSean Christopherson 	__TEST_REQUIRE(gic_fd >= 0, "Failed to create GICv3");
219*67730e6cSSean Christopherson 
220*67730e6cSSean Christopherson 	its_fd = vgic_its_setup(vm);
221*67730e6cSSean Christopherson }
222*67730e6cSSean Christopherson 
223*67730e6cSSean Christopherson static void signal_lpi(u32 device_id, u32 event_id)
224*67730e6cSSean Christopherson {
225*67730e6cSSean Christopherson 	vm_paddr_t db_addr = GITS_BASE_GPA + GITS_TRANSLATER;
226*67730e6cSSean Christopherson 
227*67730e6cSSean Christopherson 	struct kvm_msi msi = {
228*67730e6cSSean Christopherson 		.address_lo	= db_addr,
229*67730e6cSSean Christopherson 		.address_hi	= db_addr >> 32,
230*67730e6cSSean Christopherson 		.data		= event_id,
231*67730e6cSSean Christopherson 		.devid		= device_id,
232*67730e6cSSean Christopherson 		.flags		= KVM_MSI_VALID_DEVID,
233*67730e6cSSean Christopherson 	};
234*67730e6cSSean Christopherson 
235*67730e6cSSean Christopherson 	/*
236*67730e6cSSean Christopherson 	 * KVM_SIGNAL_MSI returns 1 if the MSI wasn't 'blocked' by the VM,
237*67730e6cSSean Christopherson 	 * which for arm64 implies having a valid translation in the ITS.
238*67730e6cSSean Christopherson 	 */
239*67730e6cSSean Christopherson 	TEST_ASSERT(__vm_ioctl(vm, KVM_SIGNAL_MSI, &msi) == 1,
240*67730e6cSSean Christopherson 		    "KVM_SIGNAL_MSI ioctl failed");
241*67730e6cSSean Christopherson }
242*67730e6cSSean Christopherson 
243*67730e6cSSean Christopherson static pthread_barrier_t test_setup_barrier;
244*67730e6cSSean Christopherson 
245*67730e6cSSean Christopherson static void *lpi_worker_thread(void *data)
246*67730e6cSSean Christopherson {
247*67730e6cSSean Christopherson 	u32 device_id = (size_t)data;
248*67730e6cSSean Christopherson 	u32 event_id;
249*67730e6cSSean Christopherson 	size_t i;
250*67730e6cSSean Christopherson 
251*67730e6cSSean Christopherson 	pthread_barrier_wait(&test_setup_barrier);
252*67730e6cSSean Christopherson 
253*67730e6cSSean Christopherson 	for (i = 0; i < nr_iterations; i++)
254*67730e6cSSean Christopherson 		for (event_id = 0; event_id < test_data.nr_event_ids; event_id++)
255*67730e6cSSean Christopherson 			signal_lpi(device_id, event_id);
256*67730e6cSSean Christopherson 
257*67730e6cSSean Christopherson 	return NULL;
258*67730e6cSSean Christopherson }
259*67730e6cSSean Christopherson 
260*67730e6cSSean Christopherson static void *vcpu_worker_thread(void *data)
261*67730e6cSSean Christopherson {
262*67730e6cSSean Christopherson 	struct kvm_vcpu *vcpu = data;
263*67730e6cSSean Christopherson 	struct ucall uc;
264*67730e6cSSean Christopherson 
265*67730e6cSSean Christopherson 	while (true) {
266*67730e6cSSean Christopherson 		vcpu_run(vcpu);
267*67730e6cSSean Christopherson 
268*67730e6cSSean Christopherson 		switch (get_ucall(vcpu, &uc)) {
269*67730e6cSSean Christopherson 		case UCALL_SYNC:
270*67730e6cSSean Christopherson 			pthread_barrier_wait(&test_setup_barrier);
271*67730e6cSSean Christopherson 			continue;
272*67730e6cSSean Christopherson 		case UCALL_DONE:
273*67730e6cSSean Christopherson 			return NULL;
274*67730e6cSSean Christopherson 		case UCALL_ABORT:
275*67730e6cSSean Christopherson 			REPORT_GUEST_ASSERT(uc);
276*67730e6cSSean Christopherson 			break;
277*67730e6cSSean Christopherson 		default:
278*67730e6cSSean Christopherson 			TEST_FAIL("Unknown ucall: %lu", uc.cmd);
279*67730e6cSSean Christopherson 		}
280*67730e6cSSean Christopherson 	}
281*67730e6cSSean Christopherson 
282*67730e6cSSean Christopherson 	return NULL;
283*67730e6cSSean Christopherson }
284*67730e6cSSean Christopherson 
285*67730e6cSSean Christopherson static void report_stats(struct timespec delta)
286*67730e6cSSean Christopherson {
287*67730e6cSSean Christopherson 	double nr_lpis;
288*67730e6cSSean Christopherson 	double time;
289*67730e6cSSean Christopherson 
290*67730e6cSSean Christopherson 	nr_lpis = test_data.nr_devices * test_data.nr_event_ids * nr_iterations;
291*67730e6cSSean Christopherson 
292*67730e6cSSean Christopherson 	time = delta.tv_sec;
293*67730e6cSSean Christopherson 	time += ((double)delta.tv_nsec) / NSEC_PER_SEC;
294*67730e6cSSean Christopherson 
295*67730e6cSSean Christopherson 	pr_info("Rate: %.2f LPIs/sec\n", nr_lpis / time);
296*67730e6cSSean Christopherson }
297*67730e6cSSean Christopherson 
298*67730e6cSSean Christopherson static void run_test(void)
299*67730e6cSSean Christopherson {
300*67730e6cSSean Christopherson 	u32 nr_devices = test_data.nr_devices;
301*67730e6cSSean Christopherson 	u32 nr_vcpus = test_data.nr_cpus;
302*67730e6cSSean Christopherson 	pthread_t *lpi_threads = malloc(nr_devices * sizeof(pthread_t));
303*67730e6cSSean Christopherson 	pthread_t *vcpu_threads = malloc(nr_vcpus * sizeof(pthread_t));
304*67730e6cSSean Christopherson 	struct timespec start, delta;
305*67730e6cSSean Christopherson 	size_t i;
306*67730e6cSSean Christopherson 
307*67730e6cSSean Christopherson 	TEST_ASSERT(lpi_threads && vcpu_threads, "Failed to allocate pthread arrays");
308*67730e6cSSean Christopherson 
309*67730e6cSSean Christopherson 	pthread_barrier_init(&test_setup_barrier, NULL, nr_vcpus + nr_devices + 1);
310*67730e6cSSean Christopherson 
311*67730e6cSSean Christopherson 	for (i = 0; i < nr_vcpus; i++)
312*67730e6cSSean Christopherson 		pthread_create(&vcpu_threads[i], NULL, vcpu_worker_thread, vcpus[i]);
313*67730e6cSSean Christopherson 
314*67730e6cSSean Christopherson 	for (i = 0; i < nr_devices; i++)
315*67730e6cSSean Christopherson 		pthread_create(&lpi_threads[i], NULL, lpi_worker_thread, (void *)i);
316*67730e6cSSean Christopherson 
317*67730e6cSSean Christopherson 	pthread_barrier_wait(&test_setup_barrier);
318*67730e6cSSean Christopherson 
319*67730e6cSSean Christopherson 	clock_gettime(CLOCK_MONOTONIC, &start);
320*67730e6cSSean Christopherson 
321*67730e6cSSean Christopherson 	for (i = 0; i < nr_devices; i++)
322*67730e6cSSean Christopherson 		pthread_join(lpi_threads[i], NULL);
323*67730e6cSSean Christopherson 
324*67730e6cSSean Christopherson 	delta = timespec_elapsed(start);
325*67730e6cSSean Christopherson 	write_guest_global(vm, test_data.request_vcpus_stop, true);
326*67730e6cSSean Christopherson 
327*67730e6cSSean Christopherson 	for (i = 0; i < nr_vcpus; i++)
328*67730e6cSSean Christopherson 		pthread_join(vcpu_threads[i], NULL);
329*67730e6cSSean Christopherson 
330*67730e6cSSean Christopherson 	report_stats(delta);
331*67730e6cSSean Christopherson }
332*67730e6cSSean Christopherson 
333*67730e6cSSean Christopherson static void setup_vm(void)
334*67730e6cSSean Christopherson {
335*67730e6cSSean Christopherson 	int i;
336*67730e6cSSean Christopherson 
337*67730e6cSSean Christopherson 	vcpus = malloc(test_data.nr_cpus * sizeof(struct kvm_vcpu));
338*67730e6cSSean Christopherson 	TEST_ASSERT(vcpus, "Failed to allocate vCPU array");
339*67730e6cSSean Christopherson 
340*67730e6cSSean Christopherson 	vm = vm_create_with_vcpus(test_data.nr_cpus, guest_code, vcpus);
341*67730e6cSSean Christopherson 
342*67730e6cSSean Christopherson 	vm_init_descriptor_tables(vm);
343*67730e6cSSean Christopherson 	for (i = 0; i < test_data.nr_cpus; i++)
344*67730e6cSSean Christopherson 		vcpu_init_descriptor_tables(vcpus[i]);
345*67730e6cSSean Christopherson 
346*67730e6cSSean Christopherson 	vm_install_exception_handler(vm, VECTOR_IRQ_CURRENT, guest_irq_handler);
347*67730e6cSSean Christopherson 
348*67730e6cSSean Christopherson 	setup_memslot();
349*67730e6cSSean Christopherson 
350*67730e6cSSean Christopherson 	setup_gic();
351*67730e6cSSean Christopherson 
352*67730e6cSSean Christopherson 	setup_test_data();
353*67730e6cSSean Christopherson }
354*67730e6cSSean Christopherson 
355*67730e6cSSean Christopherson static void destroy_vm(void)
356*67730e6cSSean Christopherson {
357*67730e6cSSean Christopherson 	close(its_fd);
358*67730e6cSSean Christopherson 	close(gic_fd);
359*67730e6cSSean Christopherson 	kvm_vm_free(vm);
360*67730e6cSSean Christopherson 	free(vcpus);
361*67730e6cSSean Christopherson }
362*67730e6cSSean Christopherson 
363*67730e6cSSean Christopherson static void pr_usage(const char *name)
364*67730e6cSSean Christopherson {
365*67730e6cSSean Christopherson 	pr_info("%s [-v NR_VCPUS] [-d NR_DEVICES] [-e NR_EVENTS] [-i ITERS] -h\n", name);
366*67730e6cSSean Christopherson 	pr_info("  -v:\tnumber of vCPUs (default: %u)\n", test_data.nr_cpus);
367*67730e6cSSean Christopherson 	pr_info("  -d:\tnumber of devices (default: %u)\n", test_data.nr_devices);
368*67730e6cSSean Christopherson 	pr_info("  -e:\tnumber of event IDs per device (default: %u)\n", test_data.nr_event_ids);
369*67730e6cSSean Christopherson 	pr_info("  -i:\tnumber of iterations (default: %lu)\n", nr_iterations);
370*67730e6cSSean Christopherson }
371*67730e6cSSean Christopherson 
372*67730e6cSSean Christopherson int main(int argc, char **argv)
373*67730e6cSSean Christopherson {
374*67730e6cSSean Christopherson 	u32 nr_threads;
375*67730e6cSSean Christopherson 	int c;
376*67730e6cSSean Christopherson 
377*67730e6cSSean Christopherson 	while ((c = getopt(argc, argv, "hv:d:e:i:")) != -1) {
378*67730e6cSSean Christopherson 		switch (c) {
379*67730e6cSSean Christopherson 		case 'v':
380*67730e6cSSean Christopherson 			test_data.nr_cpus = atoi(optarg);
381*67730e6cSSean Christopherson 			break;
382*67730e6cSSean Christopherson 		case 'd':
383*67730e6cSSean Christopherson 			test_data.nr_devices = atoi(optarg);
384*67730e6cSSean Christopherson 			break;
385*67730e6cSSean Christopherson 		case 'e':
386*67730e6cSSean Christopherson 			test_data.nr_event_ids = atoi(optarg);
387*67730e6cSSean Christopherson 			break;
388*67730e6cSSean Christopherson 		case 'i':
389*67730e6cSSean Christopherson 			nr_iterations = strtoul(optarg, NULL, 0);
390*67730e6cSSean Christopherson 			break;
391*67730e6cSSean Christopherson 		case 'h':
392*67730e6cSSean Christopherson 		default:
393*67730e6cSSean Christopherson 			pr_usage(argv[0]);
394*67730e6cSSean Christopherson 			return 1;
395*67730e6cSSean Christopherson 		}
396*67730e6cSSean Christopherson 	}
397*67730e6cSSean Christopherson 
398*67730e6cSSean Christopherson 	nr_threads = test_data.nr_cpus + test_data.nr_devices;
399*67730e6cSSean Christopherson 	if (nr_threads > get_nprocs())
400*67730e6cSSean Christopherson 		pr_info("WARNING: running %u threads on %d CPUs; performance is degraded.\n",
401*67730e6cSSean Christopherson 			 nr_threads, get_nprocs());
402*67730e6cSSean Christopherson 
403*67730e6cSSean Christopherson 	setup_vm();
404*67730e6cSSean Christopherson 
405*67730e6cSSean Christopherson 	run_test();
406*67730e6cSSean Christopherson 
407*67730e6cSSean Christopherson 	destroy_vm();
408*67730e6cSSean Christopherson 
409*67730e6cSSean Christopherson 	return 0;
410*67730e6cSSean Christopherson }
411