1*baa293e9SMauro Carvalho Chehab===================
2*baa293e9SMauro Carvalho ChehabSync File API Guide
3*baa293e9SMauro Carvalho Chehab===================
4*baa293e9SMauro Carvalho Chehab
5*baa293e9SMauro Carvalho Chehab:Author: Gustavo Padovan <gustavo at padovan dot org>
6*baa293e9SMauro Carvalho Chehab
7*baa293e9SMauro Carvalho ChehabThis document serves as a guide for device drivers writers on what the
8*baa293e9SMauro Carvalho Chehabsync_file API is, and how drivers can support it. Sync file is the carrier of
9*baa293e9SMauro Carvalho Chehabthe fences(struct dma_fence) that are needed to synchronize between drivers or
10*baa293e9SMauro Carvalho Chehabacross process boundaries.
11*baa293e9SMauro Carvalho Chehab
12*baa293e9SMauro Carvalho ChehabThe sync_file API is meant to be used to send and receive fence information
13*baa293e9SMauro Carvalho Chehabto/from userspace. It enables userspace to do explicit fencing, where instead
14*baa293e9SMauro Carvalho Chehabof attaching a fence to the buffer a producer driver (such as a GPU or V4L
15*baa293e9SMauro Carvalho Chehabdriver) sends the fence related to the buffer to userspace via a sync_file.
16*baa293e9SMauro Carvalho Chehab
17*baa293e9SMauro Carvalho ChehabThe sync_file then can be sent to the consumer (DRM driver for example), that
18*baa293e9SMauro Carvalho Chehabwill not use the buffer for anything before the fence(s) signals, i.e., the
19*baa293e9SMauro Carvalho Chehabdriver that issued the fence is not using/processing the buffer anymore, so it
20*baa293e9SMauro Carvalho Chehabsignals that the buffer is ready to use. And vice-versa for the consumer ->
21*baa293e9SMauro Carvalho Chehabproducer part of the cycle.
22*baa293e9SMauro Carvalho Chehab
23*baa293e9SMauro Carvalho ChehabSync files allows userspace awareness on buffer sharing synchronization between
24*baa293e9SMauro Carvalho Chehabdrivers.
25*baa293e9SMauro Carvalho Chehab
26*baa293e9SMauro Carvalho ChehabSync file was originally added in the Android kernel but current Linux Desktop
27*baa293e9SMauro Carvalho Chehabcan benefit a lot from it.
28*baa293e9SMauro Carvalho Chehab
29*baa293e9SMauro Carvalho Chehabin-fences and out-fences
30*baa293e9SMauro Carvalho Chehab------------------------
31*baa293e9SMauro Carvalho Chehab
32*baa293e9SMauro Carvalho ChehabSync files can go either to or from userspace. When a sync_file is sent from
33*baa293e9SMauro Carvalho Chehabthe driver to userspace we call the fences it contains 'out-fences'. They are
34*baa293e9SMauro Carvalho Chehabrelated to a buffer that the driver is processing or is going to process, so
35*baa293e9SMauro Carvalho Chehabthe driver creates an out-fence to be able to notify, through
36*baa293e9SMauro Carvalho Chehabdma_fence_signal(), when it has finished using (or processing) that buffer.
37*baa293e9SMauro Carvalho ChehabOut-fences are fences that the driver creates.
38*baa293e9SMauro Carvalho Chehab
39*baa293e9SMauro Carvalho ChehabOn the other hand if the driver receives fence(s) through a sync_file from
40*baa293e9SMauro Carvalho Chehabuserspace we call these fence(s) 'in-fences'. Receiving in-fences means that
41*baa293e9SMauro Carvalho Chehabwe need to wait for the fence(s) to signal before using any buffer related to
42*baa293e9SMauro Carvalho Chehabthe in-fences.
43*baa293e9SMauro Carvalho Chehab
44*baa293e9SMauro Carvalho ChehabCreating Sync Files
45*baa293e9SMauro Carvalho Chehab-------------------
46*baa293e9SMauro Carvalho Chehab
47*baa293e9SMauro Carvalho ChehabWhen a driver needs to send an out-fence userspace it creates a sync_file.
48*baa293e9SMauro Carvalho Chehab
49*baa293e9SMauro Carvalho ChehabInterface::
50*baa293e9SMauro Carvalho Chehab
51*baa293e9SMauro Carvalho Chehab	struct sync_file *sync_file_create(struct dma_fence *fence);
52*baa293e9SMauro Carvalho Chehab
53*baa293e9SMauro Carvalho ChehabThe caller pass the out-fence and gets back the sync_file. That is just the
54*baa293e9SMauro Carvalho Chehabfirst step, next it needs to install an fd on sync_file->file. So it gets an
55*baa293e9SMauro Carvalho Chehabfd::
56*baa293e9SMauro Carvalho Chehab
57*baa293e9SMauro Carvalho Chehab	fd = get_unused_fd_flags(O_CLOEXEC);
58*baa293e9SMauro Carvalho Chehab
59*baa293e9SMauro Carvalho Chehaband installs it on sync_file->file::
60*baa293e9SMauro Carvalho Chehab
61*baa293e9SMauro Carvalho Chehab	fd_install(fd, sync_file->file);
62*baa293e9SMauro Carvalho Chehab
63*baa293e9SMauro Carvalho ChehabThe sync_file fd now can be sent to userspace.
64*baa293e9SMauro Carvalho Chehab
65*baa293e9SMauro Carvalho ChehabIf the creation process fail, or the sync_file needs to be released by any
66*baa293e9SMauro Carvalho Chehabother reason fput(sync_file->file) should be used.
67*baa293e9SMauro Carvalho Chehab
68*baa293e9SMauro Carvalho ChehabReceiving Sync Files from Userspace
69*baa293e9SMauro Carvalho Chehab-----------------------------------
70*baa293e9SMauro Carvalho Chehab
71*baa293e9SMauro Carvalho ChehabWhen userspace needs to send an in-fence to the driver it passes file descriptor
72*baa293e9SMauro Carvalho Chehabof the Sync File to the kernel. The kernel can then retrieve the fences
73*baa293e9SMauro Carvalho Chehabfrom it.
74*baa293e9SMauro Carvalho Chehab
75*baa293e9SMauro Carvalho ChehabInterface::
76*baa293e9SMauro Carvalho Chehab
77*baa293e9SMauro Carvalho Chehab	struct dma_fence *sync_file_get_fence(int fd);
78*baa293e9SMauro Carvalho Chehab
79*baa293e9SMauro Carvalho Chehab
80*baa293e9SMauro Carvalho ChehabThe returned reference is owned by the caller and must be disposed of
81*baa293e9SMauro Carvalho Chehabafterwards using dma_fence_put(). In case of error, a NULL is returned instead.
82*baa293e9SMauro Carvalho Chehab
83*baa293e9SMauro Carvalho ChehabReferences:
84*baa293e9SMauro Carvalho Chehab
85*baa293e9SMauro Carvalho Chehab1. struct sync_file in include/linux/sync_file.h
86*baa293e9SMauro Carvalho Chehab2. All interfaces mentioned above defined in include/linux/sync_file.h
87