1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun *
3*4882a593Smuzhiyun * (C) COPYRIGHT 2012-2017 ARM Limited. All rights reserved.
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * This program is free software and is provided to you under the terms of the
6*4882a593Smuzhiyun * GNU General Public License version 2 as published by the Free Software
7*4882a593Smuzhiyun * Foundation, and any use by you of this program is subject to the terms
8*4882a593Smuzhiyun * of such GNU licence.
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * A copy of the licence is included with the program, and can also be obtained
11*4882a593Smuzhiyun * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12*4882a593Smuzhiyun * Boston, MA 02110-1301, USA.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun */
15*4882a593Smuzhiyun
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /**
19*4882a593Smuzhiyun * @file mali_kbase_sync.h
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * This file contains our internal "API" for explicit fences.
22*4882a593Smuzhiyun * It hides the implementation details of the actual explicit fence mechanism
23*4882a593Smuzhiyun * used (Android fences or sync file with DMA fences).
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #ifndef MALI_KBASE_SYNC_H
27*4882a593Smuzhiyun #define MALI_KBASE_SYNC_H
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include <linux/syscalls.h>
30*4882a593Smuzhiyun #ifdef CONFIG_SYNC
31*4882a593Smuzhiyun #include <sync.h>
32*4882a593Smuzhiyun #endif
33*4882a593Smuzhiyun #ifdef CONFIG_SYNC_FILE
34*4882a593Smuzhiyun #include "mali_kbase_fence_defs.h"
35*4882a593Smuzhiyun #include <linux/sync_file.h>
36*4882a593Smuzhiyun #endif
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun #include "mali_kbase.h"
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun /**
41*4882a593Smuzhiyun * struct kbase_sync_fence_info - Information about a fence
42*4882a593Smuzhiyun * @fence: Pointer to fence (type is void*, as underlaying struct can differ)
43*4882a593Smuzhiyun * @name: The name given to this fence when it was created
44*4882a593Smuzhiyun * @status: < 0 means error, 0 means active, 1 means signaled
45*4882a593Smuzhiyun *
46*4882a593Smuzhiyun * Use kbase_sync_fence_in_info_get() or kbase_sync_fence_out_info_get()
47*4882a593Smuzhiyun * to get the information.
48*4882a593Smuzhiyun */
49*4882a593Smuzhiyun struct kbase_sync_fence_info {
50*4882a593Smuzhiyun void *fence;
51*4882a593Smuzhiyun char name[32];
52*4882a593Smuzhiyun int status;
53*4882a593Smuzhiyun };
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /**
56*4882a593Smuzhiyun * kbase_sync_fence_stream_create() - Create a stream object
57*4882a593Smuzhiyun * @name: Name of stream (only used to ease debugging/visualization)
58*4882a593Smuzhiyun * @out_fd: A file descriptor representing the created stream object
59*4882a593Smuzhiyun *
60*4882a593Smuzhiyun * Can map down to a timeline implementation in some implementations.
61*4882a593Smuzhiyun * Exposed as a file descriptor.
62*4882a593Smuzhiyun * Life-time controlled via the file descriptor:
63*4882a593Smuzhiyun * - dup to add a ref
64*4882a593Smuzhiyun * - close to remove a ref
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * return: 0 on success, < 0 on error
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun int kbase_sync_fence_stream_create(const char *name, int *const out_fd);
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /**
71*4882a593Smuzhiyun * kbase_sync_fence_out_create Create an explicit output fence to specified atom
72*4882a593Smuzhiyun * @katom: Atom to assign the new explicit fence to
73*4882a593Smuzhiyun * @stream_fd: File descriptor for stream object to create fence on
74*4882a593Smuzhiyun *
75*4882a593Smuzhiyun * return: Valid file descriptor to fence or < 0 on error
76*4882a593Smuzhiyun */
77*4882a593Smuzhiyun int kbase_sync_fence_out_create(struct kbase_jd_atom *katom, int stream_fd);
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /**
80*4882a593Smuzhiyun * kbase_sync_fence_in_from_fd() Assigns an existing fence to specified atom
81*4882a593Smuzhiyun * @katom: Atom to assign the existing explicit fence to
82*4882a593Smuzhiyun * @fd: File descriptor to an existing fence
83*4882a593Smuzhiyun *
84*4882a593Smuzhiyun * Assigns an explicit input fence to atom.
85*4882a593Smuzhiyun * This can later be waited for by calling @kbase_sync_fence_in_wait
86*4882a593Smuzhiyun *
87*4882a593Smuzhiyun * return: 0 on success, < 0 on error
88*4882a593Smuzhiyun */
89*4882a593Smuzhiyun int kbase_sync_fence_in_from_fd(struct kbase_jd_atom *katom, int fd);
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /**
92*4882a593Smuzhiyun * kbase_sync_fence_validate() - Validate a fd to be a valid fence
93*4882a593Smuzhiyun * @fd: File descriptor to check
94*4882a593Smuzhiyun *
95*4882a593Smuzhiyun * This function is only usable to catch unintentional user errors early,
96*4882a593Smuzhiyun * it does not stop malicious code changing the fd after this function returns.
97*4882a593Smuzhiyun *
98*4882a593Smuzhiyun * return 0: if fd is for a valid fence, < 0 if invalid
99*4882a593Smuzhiyun */
100*4882a593Smuzhiyun int kbase_sync_fence_validate(int fd);
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /**
103*4882a593Smuzhiyun * kbase_sync_fence_out_trigger - Signal explicit output fence attached on katom
104*4882a593Smuzhiyun * @katom: Atom with an explicit fence to signal
105*4882a593Smuzhiyun * @result: < 0 means signal with error, 0 >= indicates success
106*4882a593Smuzhiyun *
107*4882a593Smuzhiyun * Signal output fence attached on katom and remove the fence from the atom.
108*4882a593Smuzhiyun *
109*4882a593Smuzhiyun * return: The "next" event code for atom, typically JOB_CANCELLED or EVENT_DONE
110*4882a593Smuzhiyun */
111*4882a593Smuzhiyun enum base_jd_event_code
112*4882a593Smuzhiyun kbase_sync_fence_out_trigger(struct kbase_jd_atom *katom, int result);
113*4882a593Smuzhiyun
114*4882a593Smuzhiyun /**
115*4882a593Smuzhiyun * kbase_sync_fence_in_wait() - Wait for explicit input fence to be signaled
116*4882a593Smuzhiyun * @katom: Atom with explicit fence to wait for
117*4882a593Smuzhiyun *
118*4882a593Smuzhiyun * If the fence is already signaled, then 0 is returned, and the caller must
119*4882a593Smuzhiyun * continue processing of the katom.
120*4882a593Smuzhiyun *
121*4882a593Smuzhiyun * If the fence isn't already signaled, then this kbase_sync framework will
122*4882a593Smuzhiyun * take responsibility to continue the processing once the fence is signaled.
123*4882a593Smuzhiyun *
124*4882a593Smuzhiyun * return: 0 if already signaled, otherwise 1
125*4882a593Smuzhiyun */
126*4882a593Smuzhiyun int kbase_sync_fence_in_wait(struct kbase_jd_atom *katom);
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun /**
129*4882a593Smuzhiyun * kbase_sync_fence_in_cancel_wait() - Cancel explicit input fence waits
130*4882a593Smuzhiyun * @katom: Atom to cancel wait for
131*4882a593Smuzhiyun *
132*4882a593Smuzhiyun * This function is fully responsible for continuing processing of this atom
133*4882a593Smuzhiyun * (remove_waiting_soft_job + finish_soft_job + jd_done + js_sched_all)
134*4882a593Smuzhiyun */
135*4882a593Smuzhiyun void kbase_sync_fence_in_cancel_wait(struct kbase_jd_atom *katom);
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun /**
138*4882a593Smuzhiyun * kbase_sync_fence_in_remove() - Remove the input fence from the katom
139*4882a593Smuzhiyun * @katom: Atom to remove explicit input fence for
140*4882a593Smuzhiyun *
141*4882a593Smuzhiyun * This will also release the corresponding reference.
142*4882a593Smuzhiyun */
143*4882a593Smuzhiyun void kbase_sync_fence_in_remove(struct kbase_jd_atom *katom);
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun /**
146*4882a593Smuzhiyun * kbase_sync_fence_out_remove() - Remove the output fence from the katom
147*4882a593Smuzhiyun * @katom: Atom to remove explicit output fence for
148*4882a593Smuzhiyun *
149*4882a593Smuzhiyun * This will also release the corresponding reference.
150*4882a593Smuzhiyun */
151*4882a593Smuzhiyun void kbase_sync_fence_out_remove(struct kbase_jd_atom *katom);
152*4882a593Smuzhiyun
153*4882a593Smuzhiyun /**
154*4882a593Smuzhiyun * kbase_sync_fence_close_fd() - Close a file descriptor representing a fence
155*4882a593Smuzhiyun * @fd: File descriptor to close
156*4882a593Smuzhiyun */
kbase_sync_fence_close_fd(int fd)157*4882a593Smuzhiyun static inline void kbase_sync_fence_close_fd(int fd)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun ksys_close(fd);
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun
162*4882a593Smuzhiyun /**
163*4882a593Smuzhiyun * kbase_sync_fence_in_info_get() - Retrieves information about input fence
164*4882a593Smuzhiyun * @katom: Atom to get fence information from
165*4882a593Smuzhiyun * @info: Struct to be filled with fence information
166*4882a593Smuzhiyun *
167*4882a593Smuzhiyun * return: 0 on success, < 0 on error
168*4882a593Smuzhiyun */
169*4882a593Smuzhiyun int kbase_sync_fence_in_info_get(struct kbase_jd_atom *katom,
170*4882a593Smuzhiyun struct kbase_sync_fence_info *info);
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /**
173*4882a593Smuzhiyun * kbase_sync_fence_out_info_get() - Retrieves information about output fence
174*4882a593Smuzhiyun * @katom: Atom to get fence information from
175*4882a593Smuzhiyun * @info: Struct to be filled with fence information
176*4882a593Smuzhiyun *
177*4882a593Smuzhiyun * return: 0 on success, < 0 on error
178*4882a593Smuzhiyun */
179*4882a593Smuzhiyun int kbase_sync_fence_out_info_get(struct kbase_jd_atom *katom,
180*4882a593Smuzhiyun struct kbase_sync_fence_info *info);
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun /**
183*4882a593Smuzhiyun * kbase_sync_status_string() - Get string matching @status
184*4882a593Smuzhiyun * @status: Value of fence status.
185*4882a593Smuzhiyun *
186*4882a593Smuzhiyun * return: Pointer to string describing @status.
187*4882a593Smuzhiyun */
188*4882a593Smuzhiyun const char *kbase_sync_status_string(int status);
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun /*
191*4882a593Smuzhiyun * Internal worker used to continue processing of atom.
192*4882a593Smuzhiyun */
193*4882a593Smuzhiyun void kbase_sync_fence_wait_worker(struct work_struct *data);
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun #ifdef CONFIG_MALI_FENCE_DEBUG
196*4882a593Smuzhiyun /**
197*4882a593Smuzhiyun * kbase_sync_fence_in_dump() Trigger a debug dump of atoms input fence state
198*4882a593Smuzhiyun * @katom: Atom to trigger fence debug dump for
199*4882a593Smuzhiyun */
200*4882a593Smuzhiyun void kbase_sync_fence_in_dump(struct kbase_jd_atom *katom);
201*4882a593Smuzhiyun #endif
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun #endif /* MALI_KBASE_SYNC_H */
204