1*4882a593Smuzhiyun=================== 2*4882a593SmuzhiyunSync File API Guide 3*4882a593Smuzhiyun=================== 4*4882a593Smuzhiyun 5*4882a593Smuzhiyun:Author: Gustavo Padovan <gustavo at padovan dot org> 6*4882a593Smuzhiyun 7*4882a593SmuzhiyunThis document serves as a guide for device drivers writers on what the 8*4882a593Smuzhiyunsync_file API is, and how drivers can support it. Sync file is the carrier of 9*4882a593Smuzhiyunthe fences(struct dma_fence) that are needed to synchronize between drivers or 10*4882a593Smuzhiyunacross process boundaries. 11*4882a593Smuzhiyun 12*4882a593SmuzhiyunThe sync_file API is meant to be used to send and receive fence information 13*4882a593Smuzhiyunto/from userspace. It enables userspace to do explicit fencing, where instead 14*4882a593Smuzhiyunof attaching a fence to the buffer a producer driver (such as a GPU or V4L 15*4882a593Smuzhiyundriver) sends the fence related to the buffer to userspace via a sync_file. 16*4882a593Smuzhiyun 17*4882a593SmuzhiyunThe sync_file then can be sent to the consumer (DRM driver for example), that 18*4882a593Smuzhiyunwill not use the buffer for anything before the fence(s) signals, i.e., the 19*4882a593Smuzhiyundriver that issued the fence is not using/processing the buffer anymore, so it 20*4882a593Smuzhiyunsignals that the buffer is ready to use. And vice-versa for the consumer -> 21*4882a593Smuzhiyunproducer part of the cycle. 22*4882a593Smuzhiyun 23*4882a593SmuzhiyunSync files allows userspace awareness on buffer sharing synchronization between 24*4882a593Smuzhiyundrivers. 25*4882a593Smuzhiyun 26*4882a593SmuzhiyunSync file was originally added in the Android kernel but current Linux Desktop 27*4882a593Smuzhiyuncan benefit a lot from it. 28*4882a593Smuzhiyun 29*4882a593Smuzhiyunin-fences and out-fences 30*4882a593Smuzhiyun------------------------ 31*4882a593Smuzhiyun 32*4882a593SmuzhiyunSync files can go either to or from userspace. When a sync_file is sent from 33*4882a593Smuzhiyunthe driver to userspace we call the fences it contains 'out-fences'. They are 34*4882a593Smuzhiyunrelated to a buffer that the driver is processing or is going to process, so 35*4882a593Smuzhiyunthe driver creates an out-fence to be able to notify, through 36*4882a593Smuzhiyundma_fence_signal(), when it has finished using (or processing) that buffer. 37*4882a593SmuzhiyunOut-fences are fences that the driver creates. 38*4882a593Smuzhiyun 39*4882a593SmuzhiyunOn the other hand if the driver receives fence(s) through a sync_file from 40*4882a593Smuzhiyunuserspace we call these fence(s) 'in-fences'. Receiving in-fences means that 41*4882a593Smuzhiyunwe need to wait for the fence(s) to signal before using any buffer related to 42*4882a593Smuzhiyunthe in-fences. 43*4882a593Smuzhiyun 44*4882a593SmuzhiyunCreating Sync Files 45*4882a593Smuzhiyun------------------- 46*4882a593Smuzhiyun 47*4882a593SmuzhiyunWhen a driver needs to send an out-fence userspace it creates a sync_file. 48*4882a593Smuzhiyun 49*4882a593SmuzhiyunInterface:: 50*4882a593Smuzhiyun 51*4882a593Smuzhiyun struct sync_file *sync_file_create(struct dma_fence *fence); 52*4882a593Smuzhiyun 53*4882a593SmuzhiyunThe caller pass the out-fence and gets back the sync_file. That is just the 54*4882a593Smuzhiyunfirst step, next it needs to install an fd on sync_file->file. So it gets an 55*4882a593Smuzhiyunfd:: 56*4882a593Smuzhiyun 57*4882a593Smuzhiyun fd = get_unused_fd_flags(O_CLOEXEC); 58*4882a593Smuzhiyun 59*4882a593Smuzhiyunand installs it on sync_file->file:: 60*4882a593Smuzhiyun 61*4882a593Smuzhiyun fd_install(fd, sync_file->file); 62*4882a593Smuzhiyun 63*4882a593SmuzhiyunThe sync_file fd now can be sent to userspace. 64*4882a593Smuzhiyun 65*4882a593SmuzhiyunIf the creation process fail, or the sync_file needs to be released by any 66*4882a593Smuzhiyunother reason fput(sync_file->file) should be used. 67*4882a593Smuzhiyun 68*4882a593SmuzhiyunReceiving Sync Files from Userspace 69*4882a593Smuzhiyun----------------------------------- 70*4882a593Smuzhiyun 71*4882a593SmuzhiyunWhen userspace needs to send an in-fence to the driver it passes file descriptor 72*4882a593Smuzhiyunof the Sync File to the kernel. The kernel can then retrieve the fences 73*4882a593Smuzhiyunfrom it. 74*4882a593Smuzhiyun 75*4882a593SmuzhiyunInterface:: 76*4882a593Smuzhiyun 77*4882a593Smuzhiyun struct dma_fence *sync_file_get_fence(int fd); 78*4882a593Smuzhiyun 79*4882a593Smuzhiyun 80*4882a593SmuzhiyunThe returned reference is owned by the caller and must be disposed of 81*4882a593Smuzhiyunafterwards using dma_fence_put(). In case of error, a NULL is returned instead. 82*4882a593Smuzhiyun 83*4882a593SmuzhiyunReferences: 84*4882a593Smuzhiyun 85*4882a593Smuzhiyun1. struct sync_file in include/linux/sync_file.h 86*4882a593Smuzhiyun2. All interfaces mentioned above defined in include/linux/sync_file.h 87