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