1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2 /* 3 * 4 * (C) COPYRIGHT 2012-2016, 2018-2022 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_FILE) 34 #include "mali_kbase_fence_defs.h" 35 #include <linux/sync_file.h> 36 #endif 37 38 #include "mali_kbase.h" 39 40 /** 41 * struct kbase_sync_fence_info - Information about a fence 42 * @fence: Pointer to fence (type is void*, as underlaying struct can differ) 43 * @name: The name given to this fence when it was created 44 * @status: < 0 means error, 0 means active, 1 means signaled 45 * 46 * Use kbase_sync_fence_in_info_get() or kbase_sync_fence_out_info_get() 47 * to get the information. 48 */ 49 struct kbase_sync_fence_info { 50 void *fence; 51 char name[32]; 52 int status; 53 }; 54 55 /** 56 * kbase_sync_fence_stream_create() - Create a stream object 57 * @name: Name of stream (only used to ease debugging/visualization) 58 * @out_fd: A file descriptor representing the created stream object 59 * 60 * Can map down to a timeline implementation in some implementations. 61 * Exposed as a file descriptor. 62 * Life-time controlled via the file descriptor: 63 * - dup to add a ref 64 * - close to remove a ref 65 * 66 * Return: 0 on success, < 0 on error 67 */ 68 int kbase_sync_fence_stream_create(const char *name, int *const out_fd); 69 70 #if !MALI_USE_CSF 71 /** 72 * kbase_sync_fence_out_create - Create an explicit output fence to specified atom 73 * 74 * @katom: Atom to assign the new explicit fence to 75 * @stream_fd: File descriptor for stream object to create fence on 76 * 77 * Return: Valid file descriptor to fence or < 0 on error 78 */ 79 int kbase_sync_fence_out_create(struct kbase_jd_atom *katom, int stream_fd); 80 81 /** 82 * kbase_sync_fence_in_from_fd() - Assigns an existing fence to specified atom 83 * @katom: Atom to assign the existing explicit fence to 84 * @fd: File descriptor to an existing fence 85 * 86 * Assigns an explicit input fence to atom. 87 * This can later be waited for by calling @kbase_sync_fence_in_wait 88 * 89 * Return: 0 on success, < 0 on error 90 */ 91 int kbase_sync_fence_in_from_fd(struct kbase_jd_atom *katom, int fd); 92 #endif /* !MALI_USE_CSF */ 93 94 /** 95 * kbase_sync_fence_validate() - Validate a fd to be a valid fence 96 * 97 * @fd: File descriptor to check 98 * 99 * This function is only usable to catch unintentional user errors early, 100 * it does not stop malicious code changing the fd after this function returns. 101 * 102 * Return: 0 if fd is for a valid fence, < 0 if invalid 103 */ 104 int kbase_sync_fence_validate(int fd); 105 106 #if !MALI_USE_CSF 107 /** 108 * kbase_sync_fence_out_trigger - Signal explicit output fence attached on katom 109 * @katom: Atom with an explicit fence to signal 110 * @result: < 0 means signal with error, 0 >= indicates success 111 * 112 * Signal output fence attached on katom and remove the fence from the atom. 113 * 114 * Return: The "next" event code for atom, typically JOB_CANCELLED or EVENT_DONE 115 */ 116 enum base_jd_event_code 117 kbase_sync_fence_out_trigger(struct kbase_jd_atom *katom, int result); 118 119 /** 120 * kbase_sync_fence_in_wait() - Wait for explicit input fence to be signaled 121 * @katom: Atom with explicit fence to wait for 122 * 123 * If the fence is already signaled, then 0 is returned, and the caller must 124 * continue processing of the katom. 125 * 126 * If the fence isn't already signaled, then this kbase_sync framework will 127 * take responsibility to continue the processing once the fence is signaled. 128 * 129 * Return: 0 if already signaled, otherwise 1 130 */ 131 int kbase_sync_fence_in_wait(struct kbase_jd_atom *katom); 132 133 /** 134 * kbase_sync_fence_in_cancel_wait() - Cancel explicit input fence waits 135 * @katom: Atom to cancel wait for 136 * 137 * This function is fully responsible for continuing processing of this atom 138 * (remove_waiting_soft_job + finish_soft_job + jd_done + js_sched_all) 139 */ 140 void kbase_sync_fence_in_cancel_wait(struct kbase_jd_atom *katom); 141 142 /** 143 * kbase_sync_fence_in_remove() - Remove the input fence from the katom 144 * @katom: Atom to remove explicit input fence for 145 * 146 * This will also release the corresponding reference. 147 */ 148 void kbase_sync_fence_in_remove(struct kbase_jd_atom *katom); 149 150 /** 151 * kbase_sync_fence_out_remove() - Remove the output fence from the katom 152 * @katom: Atom to remove explicit output fence for 153 * 154 * This will also release the corresponding reference. 155 */ 156 void kbase_sync_fence_out_remove(struct kbase_jd_atom *katom); 157 #endif /* !MALI_USE_CSF */ 158 159 #if !MALI_USE_CSF 160 /** 161 * kbase_sync_fence_in_info_get() - Retrieves information about input fence 162 * @katom: Atom to get fence information from 163 * @info: Struct to be filled with fence information 164 * 165 * Return: 0 on success, < 0 on error 166 */ 167 int kbase_sync_fence_in_info_get(struct kbase_jd_atom *katom, 168 struct kbase_sync_fence_info *info); 169 170 /** 171 * kbase_sync_fence_out_info_get() - Retrieves information about output fence 172 * @katom: Atom to get fence information from 173 * @info: Struct to be filled with fence information 174 * 175 * Return: 0 on success, < 0 on error 176 */ 177 int kbase_sync_fence_out_info_get(struct kbase_jd_atom *katom, 178 struct kbase_sync_fence_info *info); 179 #endif /* !MALI_USE_CSF */ 180 181 #if IS_ENABLED(CONFIG_SYNC_FILE) 182 #if (KERNEL_VERSION(4, 10, 0) > LINUX_VERSION_CODE) 183 void kbase_sync_fence_info_get(struct fence *fence, 184 struct kbase_sync_fence_info *info); 185 #else 186 void kbase_sync_fence_info_get(struct dma_fence *fence, 187 struct kbase_sync_fence_info *info); 188 #endif 189 #endif 190 191 /** 192 * kbase_sync_status_string() - Get string matching @status 193 * @status: Value of fence status. 194 * 195 * Return: Pointer to string describing @status. 196 */ 197 const char *kbase_sync_status_string(int status); 198 199 200 #if !MALI_USE_CSF 201 /* 202 * Internal worker used to continue processing of atom. 203 */ 204 void kbase_sync_fence_wait_worker(struct work_struct *data); 205 206 #ifdef CONFIG_MALI_BIFROST_FENCE_DEBUG 207 /** 208 * kbase_sync_fence_in_dump() - Trigger a debug dump of atoms input fence state 209 * 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