18c2ecf20Sopenharmony_ci=================== 28c2ecf20Sopenharmony_ciSync File API Guide 38c2ecf20Sopenharmony_ci=================== 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ci:Author: Gustavo Padovan <gustavo at padovan dot org> 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ciThis document serves as a guide for device drivers writers on what the 88c2ecf20Sopenharmony_cisync_file API is, and how drivers can support it. Sync file is the carrier of 98c2ecf20Sopenharmony_cithe fences(struct dma_fence) that are needed to synchronize between drivers or 108c2ecf20Sopenharmony_ciacross process boundaries. 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ciThe sync_file API is meant to be used to send and receive fence information 138c2ecf20Sopenharmony_cito/from userspace. It enables userspace to do explicit fencing, where instead 148c2ecf20Sopenharmony_ciof attaching a fence to the buffer a producer driver (such as a GPU or V4L 158c2ecf20Sopenharmony_cidriver) sends the fence related to the buffer to userspace via a sync_file. 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ciThe sync_file then can be sent to the consumer (DRM driver for example), that 188c2ecf20Sopenharmony_ciwill not use the buffer for anything before the fence(s) signals, i.e., the 198c2ecf20Sopenharmony_cidriver that issued the fence is not using/processing the buffer anymore, so it 208c2ecf20Sopenharmony_cisignals that the buffer is ready to use. And vice-versa for the consumer -> 218c2ecf20Sopenharmony_ciproducer part of the cycle. 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ciSync files allows userspace awareness on buffer sharing synchronization between 248c2ecf20Sopenharmony_cidrivers. 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ciSync file was originally added in the Android kernel but current Linux Desktop 278c2ecf20Sopenharmony_cican benefit a lot from it. 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_ciin-fences and out-fences 308c2ecf20Sopenharmony_ci------------------------ 318c2ecf20Sopenharmony_ci 328c2ecf20Sopenharmony_ciSync files can go either to or from userspace. When a sync_file is sent from 338c2ecf20Sopenharmony_cithe driver to userspace we call the fences it contains 'out-fences'. They are 348c2ecf20Sopenharmony_cirelated to a buffer that the driver is processing or is going to process, so 358c2ecf20Sopenharmony_cithe driver creates an out-fence to be able to notify, through 368c2ecf20Sopenharmony_cidma_fence_signal(), when it has finished using (or processing) that buffer. 378c2ecf20Sopenharmony_ciOut-fences are fences that the driver creates. 388c2ecf20Sopenharmony_ci 398c2ecf20Sopenharmony_ciOn the other hand if the driver receives fence(s) through a sync_file from 408c2ecf20Sopenharmony_ciuserspace we call these fence(s) 'in-fences'. Receiving in-fences means that 418c2ecf20Sopenharmony_ciwe need to wait for the fence(s) to signal before using any buffer related to 428c2ecf20Sopenharmony_cithe in-fences. 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_ciCreating Sync Files 458c2ecf20Sopenharmony_ci------------------- 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ciWhen a driver needs to send an out-fence userspace it creates a sync_file. 488c2ecf20Sopenharmony_ci 498c2ecf20Sopenharmony_ciInterface:: 508c2ecf20Sopenharmony_ci 518c2ecf20Sopenharmony_ci struct sync_file *sync_file_create(struct dma_fence *fence); 528c2ecf20Sopenharmony_ci 538c2ecf20Sopenharmony_ciThe caller pass the out-fence and gets back the sync_file. That is just the 548c2ecf20Sopenharmony_cifirst step, next it needs to install an fd on sync_file->file. So it gets an 558c2ecf20Sopenharmony_cifd:: 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci fd = get_unused_fd_flags(O_CLOEXEC); 588c2ecf20Sopenharmony_ci 598c2ecf20Sopenharmony_ciand installs it on sync_file->file:: 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci fd_install(fd, sync_file->file); 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ciThe sync_file fd now can be sent to userspace. 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ciIf the creation process fail, or the sync_file needs to be released by any 668c2ecf20Sopenharmony_ciother reason fput(sync_file->file) should be used. 678c2ecf20Sopenharmony_ci 688c2ecf20Sopenharmony_ciReceiving Sync Files from Userspace 698c2ecf20Sopenharmony_ci----------------------------------- 708c2ecf20Sopenharmony_ci 718c2ecf20Sopenharmony_ciWhen userspace needs to send an in-fence to the driver it passes file descriptor 728c2ecf20Sopenharmony_ciof the Sync File to the kernel. The kernel can then retrieve the fences 738c2ecf20Sopenharmony_cifrom it. 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ciInterface:: 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci struct dma_fence *sync_file_get_fence(int fd); 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci 808c2ecf20Sopenharmony_ciThe returned reference is owned by the caller and must be disposed of 818c2ecf20Sopenharmony_ciafterwards using dma_fence_put(). In case of error, a NULL is returned instead. 828c2ecf20Sopenharmony_ci 838c2ecf20Sopenharmony_ciReferences: 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci1. struct sync_file in include/linux/sync_file.h 868c2ecf20Sopenharmony_ci2. All interfaces mentioned above defined in include/linux/sync_file.h 87