xref: /openbmc/linux/tools/testing/selftests/sync/sync_fence.c (revision 9095bf25ea08135a5b74875dd0e3eeaddc4218a0)
1*6a5b7d2cSEmilio López /*
2*6a5b7d2cSEmilio López  *  sync fence tests with one timeline
3*6a5b7d2cSEmilio López  *  Copyright 2015-2016 Collabora Ltd.
4*6a5b7d2cSEmilio López  *
5*6a5b7d2cSEmilio López  *  Based on the implementation from the Android Open Source Project,
6*6a5b7d2cSEmilio López  *
7*6a5b7d2cSEmilio López  *  Copyright 2012 Google, Inc
8*6a5b7d2cSEmilio López  *
9*6a5b7d2cSEmilio López  *  Permission is hereby granted, free of charge, to any person obtaining a
10*6a5b7d2cSEmilio López  *  copy of this software and associated documentation files (the "Software"),
11*6a5b7d2cSEmilio López  *  to deal in the Software without restriction, including without limitation
12*6a5b7d2cSEmilio López  *  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13*6a5b7d2cSEmilio López  *  and/or sell copies of the Software, and to permit persons to whom the
14*6a5b7d2cSEmilio López  *  Software is furnished to do so, subject to the following conditions:
15*6a5b7d2cSEmilio López  *
16*6a5b7d2cSEmilio López  *  The above copyright notice and this permission notice shall be included in
17*6a5b7d2cSEmilio López  *  all copies or substantial portions of the Software.
18*6a5b7d2cSEmilio López  *
19*6a5b7d2cSEmilio López  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20*6a5b7d2cSEmilio López  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21*6a5b7d2cSEmilio López  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22*6a5b7d2cSEmilio López  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
23*6a5b7d2cSEmilio López  *  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24*6a5b7d2cSEmilio López  *  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25*6a5b7d2cSEmilio López  *  OTHER DEALINGS IN THE SOFTWARE.
26*6a5b7d2cSEmilio López  */
27*6a5b7d2cSEmilio López 
28*6a5b7d2cSEmilio López #include "sync.h"
29*6a5b7d2cSEmilio López #include "sw_sync.h"
30*6a5b7d2cSEmilio López #include "synctest.h"
31*6a5b7d2cSEmilio López 
test_fence_one_timeline_wait(void)32*6a5b7d2cSEmilio López int test_fence_one_timeline_wait(void)
33*6a5b7d2cSEmilio López {
34*6a5b7d2cSEmilio López 	int fence, valid, ret;
35*6a5b7d2cSEmilio López 	int timeline = sw_sync_timeline_create();
36*6a5b7d2cSEmilio López 
37*6a5b7d2cSEmilio López 	valid = sw_sync_timeline_is_valid(timeline);
38*6a5b7d2cSEmilio López 	ASSERT(valid, "Failure allocating timeline\n");
39*6a5b7d2cSEmilio López 
40*6a5b7d2cSEmilio López 	fence = sw_sync_fence_create(timeline, "allocFence", 5);
41*6a5b7d2cSEmilio López 	valid = sw_sync_fence_is_valid(fence);
42*6a5b7d2cSEmilio López 	ASSERT(valid, "Failure allocating fence\n");
43*6a5b7d2cSEmilio López 
44*6a5b7d2cSEmilio López 	/* Wait on fence until timeout */
45*6a5b7d2cSEmilio López 	ret = sync_wait(fence, 0);
46*6a5b7d2cSEmilio López 	ASSERT(ret == 0, "Failure waiting on fence until timeout\n");
47*6a5b7d2cSEmilio López 
48*6a5b7d2cSEmilio López 	/* Advance timeline from 0 -> 1 */
49*6a5b7d2cSEmilio López 	ret = sw_sync_timeline_inc(timeline, 1);
50*6a5b7d2cSEmilio López 	ASSERT(ret == 0, "Failure advancing timeline\n");
51*6a5b7d2cSEmilio López 
52*6a5b7d2cSEmilio López 	/* Wait on fence until timeout */
53*6a5b7d2cSEmilio López 	ret = sync_wait(fence, 0);
54*6a5b7d2cSEmilio López 	ASSERT(ret == 0, "Failure waiting on fence until timeout\n");
55*6a5b7d2cSEmilio López 
56*6a5b7d2cSEmilio López 	/* Signal the fence */
57*6a5b7d2cSEmilio López 	ret = sw_sync_timeline_inc(timeline, 4);
58*6a5b7d2cSEmilio López 	ASSERT(ret == 0, "Failure signaling the fence\n");
59*6a5b7d2cSEmilio López 
60*6a5b7d2cSEmilio López 	/* Wait successfully */
61*6a5b7d2cSEmilio López 	ret = sync_wait(fence, 0);
62*6a5b7d2cSEmilio López 	ASSERT(ret > 0, "Failure waiting on fence\n");
63*6a5b7d2cSEmilio López 
64*6a5b7d2cSEmilio López 	/* Go even further, and confirm wait still succeeds */
65*6a5b7d2cSEmilio López 	ret = sw_sync_timeline_inc(timeline, 10);
66*6a5b7d2cSEmilio López 	ASSERT(ret == 0, "Failure going further\n");
67*6a5b7d2cSEmilio López 	ret = sync_wait(fence, 0);
68*6a5b7d2cSEmilio López 	ASSERT(ret > 0, "Failure waiting ahead\n");
69*6a5b7d2cSEmilio López 
70*6a5b7d2cSEmilio López 	sw_sync_fence_destroy(fence);
71*6a5b7d2cSEmilio López 	sw_sync_timeline_destroy(timeline);
72*6a5b7d2cSEmilio López 
73*6a5b7d2cSEmilio López 	return 0;
74*6a5b7d2cSEmilio López }
75*6a5b7d2cSEmilio López 
test_fence_one_timeline_merge(void)76*6a5b7d2cSEmilio López int test_fence_one_timeline_merge(void)
77*6a5b7d2cSEmilio López {
78*6a5b7d2cSEmilio López 	int a, b, c, d, valid;
79*6a5b7d2cSEmilio López 	int timeline = sw_sync_timeline_create();
80*6a5b7d2cSEmilio López 
81*6a5b7d2cSEmilio López 	/* create fence a,b,c and then merge them all into fence d */
82*6a5b7d2cSEmilio López 	a = sw_sync_fence_create(timeline, "allocFence", 1);
83*6a5b7d2cSEmilio López 	b = sw_sync_fence_create(timeline, "allocFence", 2);
84*6a5b7d2cSEmilio López 	c = sw_sync_fence_create(timeline, "allocFence", 3);
85*6a5b7d2cSEmilio López 
86*6a5b7d2cSEmilio López 	valid = sw_sync_fence_is_valid(a) &&
87*6a5b7d2cSEmilio López 		sw_sync_fence_is_valid(b) &&
88*6a5b7d2cSEmilio López 		sw_sync_fence_is_valid(c);
89*6a5b7d2cSEmilio López 	ASSERT(valid, "Failure allocating fences\n");
90*6a5b7d2cSEmilio López 
91*6a5b7d2cSEmilio López 	d = sync_merge("mergeFence", b, a);
92*6a5b7d2cSEmilio López 	d = sync_merge("mergeFence", c, d);
93*6a5b7d2cSEmilio López 	valid = sw_sync_fence_is_valid(d);
94*6a5b7d2cSEmilio López 	ASSERT(valid, "Failure merging fences\n");
95*6a5b7d2cSEmilio López 
96*6a5b7d2cSEmilio López 	/* confirm all fences have one active point (even d) */
97*6a5b7d2cSEmilio López 	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_ACTIVE) == 1,
98*6a5b7d2cSEmilio López 	       "a has too many active fences!\n");
99*6a5b7d2cSEmilio López 	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_ACTIVE) == 1,
100*6a5b7d2cSEmilio López 	       "b has too many active fences!\n");
101*6a5b7d2cSEmilio López 	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_ACTIVE) == 1,
102*6a5b7d2cSEmilio López 	       "c has too many active fences!\n");
103*6a5b7d2cSEmilio López 	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_ACTIVE) == 1,
104*6a5b7d2cSEmilio López 	       "d has too many active fences!\n");
105*6a5b7d2cSEmilio López 
106*6a5b7d2cSEmilio López 	/* confirm that d is not signaled until the max of a,b,c */
107*6a5b7d2cSEmilio López 	sw_sync_timeline_inc(timeline, 1);
108*6a5b7d2cSEmilio López 	ASSERT(sync_fence_count_with_status(a, FENCE_STATUS_SIGNALED) == 1,
109*6a5b7d2cSEmilio López 	       "a did not signal!\n");
110*6a5b7d2cSEmilio López 	ASSERT(sync_fence_count_with_status(d, FENCE_STATUS_ACTIVE) == 1,
111*6a5b7d2cSEmilio López 	       "d signaled too early!\n");
112*6a5b7d2cSEmilio López 
113*6a5b7d2cSEmilio López 	sw_sync_timeline_inc(timeline, 1);
114*6a5b7d2cSEmilio López 	ASSERT(sync_fence_count_with_status(b, FENCE_STATUS_SIGNALED) == 1,
115*6a5b7d2cSEmilio López 	       "b did not signal!\n");
116*6a5b7d2cSEmilio López 	ASSERT(sync_fence_count_with_status(d, FENCE_STATUS_ACTIVE) == 1,
117*6a5b7d2cSEmilio López 	       "d signaled too early!\n");
118*6a5b7d2cSEmilio López 
119*6a5b7d2cSEmilio López 	sw_sync_timeline_inc(timeline, 1);
120*6a5b7d2cSEmilio López 	ASSERT(sync_fence_count_with_status(c, FENCE_STATUS_SIGNALED) == 1,
121*6a5b7d2cSEmilio López 	       "c did not signal!\n");
122*6a5b7d2cSEmilio López 	ASSERT(sync_fence_count_with_status(d, FENCE_STATUS_ACTIVE) == 0 &&
123*6a5b7d2cSEmilio López 	       sync_fence_count_with_status(d, FENCE_STATUS_SIGNALED) == 1,
124*6a5b7d2cSEmilio López 	       "d did not signal!\n");
125*6a5b7d2cSEmilio López 
126*6a5b7d2cSEmilio López 	sw_sync_fence_destroy(d);
127*6a5b7d2cSEmilio López 	sw_sync_fence_destroy(c);
128*6a5b7d2cSEmilio López 	sw_sync_fence_destroy(b);
129*6a5b7d2cSEmilio López 	sw_sync_fence_destroy(a);
130*6a5b7d2cSEmilio López 	sw_sync_timeline_destroy(timeline);
131*6a5b7d2cSEmilio López 	return 0;
132*6a5b7d2cSEmilio López }
133