xref: /openbmc/linux/tools/testing/selftests/powerpc/math/vmx_preempt.c (revision 1188f7f111c61394ec56beb8e30322305a8220b6)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e5ab8be6SCyril Bur /*
3e5ab8be6SCyril Bur  * Copyright 2015, Cyril Bur, IBM Corp.
4e5ab8be6SCyril Bur  *
5e5ab8be6SCyril Bur  * This test attempts to see if the VMX registers change across preemption.
6e5ab8be6SCyril Bur  * Two things should be noted here a) The check_vmx function in asm only checks
7e5ab8be6SCyril Bur  * the non volatile registers as it is reused from the syscall test b) There is
8e5ab8be6SCyril Bur  * no way to be sure preemption happened so this test just uses many threads
9e5ab8be6SCyril Bur  * and a long wait. As such, a successful test doesn't mean much but a failure
10e5ab8be6SCyril Bur  * is bad.
11e5ab8be6SCyril Bur  */
12e5ab8be6SCyril Bur 
13e5ab8be6SCyril Bur #include <stdio.h>
14e5ab8be6SCyril Bur #include <unistd.h>
15e5ab8be6SCyril Bur #include <sys/syscall.h>
16e5ab8be6SCyril Bur #include <sys/time.h>
17e5ab8be6SCyril Bur #include <sys/types.h>
18e5ab8be6SCyril Bur #include <sys/wait.h>
19e5ab8be6SCyril Bur #include <stdlib.h>
20e5ab8be6SCyril Bur #include <pthread.h>
21e5ab8be6SCyril Bur 
22e5ab8be6SCyril Bur #include "utils.h"
23e5ab8be6SCyril Bur 
24e5ab8be6SCyril Bur /* Time to wait for workers to get preempted (seconds) */
25e5ab8be6SCyril Bur #define PREEMPT_TIME 20
26e5ab8be6SCyril Bur /*
27e5ab8be6SCyril Bur  * Factor by which to multiply number of online CPUs for total number of
28e5ab8be6SCyril Bur  * worker threads
29e5ab8be6SCyril Bur  */
30e5ab8be6SCyril Bur #define THREAD_FACTOR 8
31e5ab8be6SCyril Bur 
32e5ab8be6SCyril Bur __thread vector int varray[] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10,11,12},
33e5ab8be6SCyril Bur 	{13,14,15,16},{17,18,19,20},{21,22,23,24},
34e5ab8be6SCyril Bur 	{25,26,27,28},{29,30,31,32},{33,34,35,36},
35e5ab8be6SCyril Bur 	{37,38,39,40},{41,42,43,44},{45,46,47,48}};
36e5ab8be6SCyril Bur 
37e5ab8be6SCyril Bur int threads_starting;
38e5ab8be6SCyril Bur int running;
39e5ab8be6SCyril Bur 
40*0a16df5bSMichael Ellerman extern int preempt_vmx(vector int *varray, int *threads_starting, int *running);
41e5ab8be6SCyril Bur 
preempt_vmx_c(void * p)42e5ab8be6SCyril Bur void *preempt_vmx_c(void *p)
43e5ab8be6SCyril Bur {
44e5ab8be6SCyril Bur 	int i, j;
45*0a16df5bSMichael Ellerman 	long rc;
46*0a16df5bSMichael Ellerman 
47e5ab8be6SCyril Bur 	srand(pthread_self());
48e5ab8be6SCyril Bur 	for (i = 0; i < 12; i++)
49e5ab8be6SCyril Bur 		for (j = 0; j < 4; j++)
50e5ab8be6SCyril Bur 			varray[i][j] = rand();
51e5ab8be6SCyril Bur 
52*0a16df5bSMichael Ellerman 	rc = preempt_vmx(varray, &threads_starting, &running);
53*0a16df5bSMichael Ellerman 
54*0a16df5bSMichael Ellerman 	return (void *)rc;
55e5ab8be6SCyril Bur }
56e5ab8be6SCyril Bur 
test_preempt_vmx(void)57e5ab8be6SCyril Bur int test_preempt_vmx(void)
58e5ab8be6SCyril Bur {
59e5ab8be6SCyril Bur 	int i, rc, threads;
60e5ab8be6SCyril Bur 	pthread_t *tids;
61e5ab8be6SCyril Bur 
62872d11bcSMichael Ellerman 	// vcmpequd used in vmx_asm.S is v2.07
63872d11bcSMichael Ellerman 	SKIP_IF(!have_hwcap2(PPC_FEATURE2_ARCH_2_07));
64872d11bcSMichael Ellerman 
65e5ab8be6SCyril Bur 	threads = sysconf(_SC_NPROCESSORS_ONLN) * THREAD_FACTOR;
66e5ab8be6SCyril Bur 	tids = malloc(threads * sizeof(pthread_t));
67e5ab8be6SCyril Bur 	FAIL_IF(!tids);
68e5ab8be6SCyril Bur 
69e5ab8be6SCyril Bur 	running = true;
70e5ab8be6SCyril Bur 	threads_starting = threads;
71e5ab8be6SCyril Bur 	for (i = 0; i < threads; i++) {
72e5ab8be6SCyril Bur 		rc = pthread_create(&tids[i], NULL, preempt_vmx_c, NULL);
73e5ab8be6SCyril Bur 		FAIL_IF(rc);
74e5ab8be6SCyril Bur 	}
75e5ab8be6SCyril Bur 
76e5ab8be6SCyril Bur 	setbuf(stdout, NULL);
77e5ab8be6SCyril Bur 	/* Not really nessesary but nice to wait for every thread to start */
78e5ab8be6SCyril Bur 	printf("\tWaiting for all workers to start...");
79e5ab8be6SCyril Bur 	while(threads_starting)
80e5ab8be6SCyril Bur 		asm volatile("": : :"memory");
81e5ab8be6SCyril Bur 	printf("done\n");
82e5ab8be6SCyril Bur 
83e5ab8be6SCyril Bur 	printf("\tWaiting for %d seconds to let some workers get preempted...", PREEMPT_TIME);
84e5ab8be6SCyril Bur 	sleep(PREEMPT_TIME);
85e5ab8be6SCyril Bur 	printf("done\n");
86e5ab8be6SCyril Bur 
87e5ab8be6SCyril Bur 	printf("\tStopping workers...");
88e5ab8be6SCyril Bur 	/*
89e5ab8be6SCyril Bur 	 * Working are checking this value every loop. In preempt_vmx 'cmpwi r5,0; bne 2b'.
90e5ab8be6SCyril Bur 	 * r5 will have loaded the value of running.
91e5ab8be6SCyril Bur 	 */
92e5ab8be6SCyril Bur 	running = 0;
93e5ab8be6SCyril Bur 	for (i = 0; i < threads; i++) {
94e5ab8be6SCyril Bur 		void *rc_p;
95e5ab8be6SCyril Bur 		pthread_join(tids[i], &rc_p);
96e5ab8be6SCyril Bur 
97e5ab8be6SCyril Bur 		/*
98e5ab8be6SCyril Bur 		 * Harness will say the fail was here, look at why preempt_vmx
99e5ab8be6SCyril Bur 		 * returned
100e5ab8be6SCyril Bur 		 */
101e5ab8be6SCyril Bur 		if ((long) rc_p)
102e5ab8be6SCyril Bur 			printf("oops\n");
103e5ab8be6SCyril Bur 		FAIL_IF((long) rc_p);
104e5ab8be6SCyril Bur 	}
105e5ab8be6SCyril Bur 	printf("done\n");
106e5ab8be6SCyril Bur 
107e5ab8be6SCyril Bur 	return 0;
108e5ab8be6SCyril Bur }
109e5ab8be6SCyril Bur 
main(int argc,char * argv[])110e5ab8be6SCyril Bur int main(int argc, char *argv[])
111e5ab8be6SCyril Bur {
112e5ab8be6SCyril Bur 	return test_harness(test_preempt_vmx, "vmx_preempt");
113e5ab8be6SCyril Bur }
114