LCOV - code coverage report
Current view: top level - include/linux - dma-buf.h (source / functions) Hit Total Coverage
Test: combined.info Lines: 0 7 0.0 %
Date: 2022-03-28 13:20:08 Functions: 0 0 -
Branches: 0 18 0.0 %

           Branch data     Line data    Source code
       1                 :            : /* SPDX-License-Identifier: GPL-2.0-only */
       2                 :            : /*
       3                 :            :  * Header file for dma buffer sharing framework.
       4                 :            :  *
       5                 :            :  * Copyright(C) 2011 Linaro Limited. All rights reserved.
       6                 :            :  * Author: Sumit Semwal <sumit.semwal@ti.com>
       7                 :            :  *
       8                 :            :  * Many thanks to linaro-mm-sig list, and specially
       9                 :            :  * Arnd Bergmann <arnd@arndb.de>, Rob Clark <rob@ti.com> and
      10                 :            :  * Daniel Vetter <daniel@ffwll.ch> for their support in creation and
      11                 :            :  * refining of this idea.
      12                 :            :  */
      13                 :            : #ifndef __DMA_BUF_H__
      14                 :            : #define __DMA_BUF_H__
      15                 :            : 
      16                 :            : #include <linux/file.h>
      17                 :            : #include <linux/err.h>
      18                 :            : #include <linux/scatterlist.h>
      19                 :            : #include <linux/list.h>
      20                 :            : #include <linux/dma-mapping.h>
      21                 :            : #include <linux/fs.h>
      22                 :            : #include <linux/dma-fence.h>
      23                 :            : #include <linux/wait.h>
      24                 :            : 
      25                 :            : struct device;
      26                 :            : struct dma_buf;
      27                 :            : struct dma_buf_attachment;
      28                 :            : 
      29                 :            : /**
      30                 :            :  * struct dma_buf_ops - operations possible on struct dma_buf
      31                 :            :  * @vmap: [optional] creates a virtual mapping for the buffer into kernel
      32                 :            :  *        address space. Same restrictions as for vmap and friends apply.
      33                 :            :  * @vunmap: [optional] unmaps a vmap from the buffer
      34                 :            :  */
      35                 :            : struct dma_buf_ops {
      36                 :            :         /**
      37                 :            :           * @cache_sgt_mapping:
      38                 :            :           *
      39                 :            :           * If true the framework will cache the first mapping made for each
      40                 :            :           * attachment. This avoids creating mappings for attachments multiple
      41                 :            :           * times.
      42                 :            :           */
      43                 :            :         bool cache_sgt_mapping;
      44                 :            : 
      45                 :            :         /**
      46                 :            :          * @dynamic_mapping:
      47                 :            :          *
      48                 :            :          * If true the framework makes sure that the map/unmap_dma_buf
      49                 :            :          * callbacks are always called with the dma_resv object locked.
      50                 :            :          *
      51                 :            :          * If false the framework makes sure that the map/unmap_dma_buf
      52                 :            :          * callbacks are always called without the dma_resv object locked.
      53                 :            :          * Mutual exclusive with @cache_sgt_mapping.
      54                 :            :          */
      55                 :            :         bool dynamic_mapping;
      56                 :            : 
      57                 :            :         /**
      58                 :            :          * @attach:
      59                 :            :          *
      60                 :            :          * This is called from dma_buf_attach() to make sure that a given
      61                 :            :          * &dma_buf_attachment.dev can access the provided &dma_buf. Exporters
      62                 :            :          * which support buffer objects in special locations like VRAM or
      63                 :            :          * device-specific carveout areas should check whether the buffer could
      64                 :            :          * be move to system memory (or directly accessed by the provided
      65                 :            :          * device), and otherwise need to fail the attach operation.
      66                 :            :          *
      67                 :            :          * The exporter should also in general check whether the current
      68                 :            :          * allocation fullfills the DMA constraints of the new device. If this
      69                 :            :          * is not the case, and the allocation cannot be moved, it should also
      70                 :            :          * fail the attach operation.
      71                 :            :          *
      72                 :            :          * Any exporter-private housekeeping data can be stored in the
      73                 :            :          * &dma_buf_attachment.priv pointer.
      74                 :            :          *
      75                 :            :          * This callback is optional.
      76                 :            :          *
      77                 :            :          * Returns:
      78                 :            :          *
      79                 :            :          * 0 on success, negative error code on failure. It might return -EBUSY
      80                 :            :          * to signal that backing storage is already allocated and incompatible
      81                 :            :          * with the requirements of requesting device.
      82                 :            :          */
      83                 :            :         int (*attach)(struct dma_buf *, struct dma_buf_attachment *);
      84                 :            : 
      85                 :            :         /**
      86                 :            :          * @detach:
      87                 :            :          *
      88                 :            :          * This is called by dma_buf_detach() to release a &dma_buf_attachment.
      89                 :            :          * Provided so that exporters can clean up any housekeeping for an
      90                 :            :          * &dma_buf_attachment.
      91                 :            :          *
      92                 :            :          * This callback is optional.
      93                 :            :          */
      94                 :            :         void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
      95                 :            : 
      96                 :            :         /**
      97                 :            :          * @map_dma_buf:
      98                 :            :          *
      99                 :            :          * This is called by dma_buf_map_attachment() and is used to map a
     100                 :            :          * shared &dma_buf into device address space, and it is mandatory. It
     101                 :            :          * can only be called if @attach has been called successfully. This
     102                 :            :          * essentially pins the DMA buffer into place, and it cannot be moved
     103                 :            :          * any more
     104                 :            :          *
     105                 :            :          * This call may sleep, e.g. when the backing storage first needs to be
     106                 :            :          * allocated, or moved to a location suitable for all currently attached
     107                 :            :          * devices.
     108                 :            :          *
     109                 :            :          * Note that any specific buffer attributes required for this function
     110                 :            :          * should get added to device_dma_parameters accessible via
     111                 :            :          * &device.dma_params from the &dma_buf_attachment. The @attach callback
     112                 :            :          * should also check these constraints.
     113                 :            :          *
     114                 :            :          * If this is being called for the first time, the exporter can now
     115                 :            :          * choose to scan through the list of attachments for this buffer,
     116                 :            :          * collate the requirements of the attached devices, and choose an
     117                 :            :          * appropriate backing storage for the buffer.
     118                 :            :          *
     119                 :            :          * Based on enum dma_data_direction, it might be possible to have
     120                 :            :          * multiple users accessing at the same time (for reading, maybe), or
     121                 :            :          * any other kind of sharing that the exporter might wish to make
     122                 :            :          * available to buffer-users.
     123                 :            :          *
     124                 :            :          * This is always called with the dmabuf->resv object locked when
     125                 :            :          * the dynamic_mapping flag is true.
     126                 :            :          *
     127                 :            :          * Returns:
     128                 :            :          *
     129                 :            :          * A &sg_table scatter list of or the backing storage of the DMA buffer,
     130                 :            :          * already mapped into the device address space of the &device attached
     131                 :            :          * with the provided &dma_buf_attachment.
     132                 :            :          *
     133                 :            :          * On failure, returns a negative error value wrapped into a pointer.
     134                 :            :          * May also return -EINTR when a signal was received while being
     135                 :            :          * blocked.
     136                 :            :          */
     137                 :            :         struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *,
     138                 :            :                                          enum dma_data_direction);
     139                 :            :         /**
     140                 :            :          * @unmap_dma_buf:
     141                 :            :          *
     142                 :            :          * This is called by dma_buf_unmap_attachment() and should unmap and
     143                 :            :          * release the &sg_table allocated in @map_dma_buf, and it is mandatory.
     144                 :            :          * It should also unpin the backing storage if this is the last mapping
     145                 :            :          * of the DMA buffer, it the exporter supports backing storage
     146                 :            :          * migration.
     147                 :            :          */
     148                 :            :         void (*unmap_dma_buf)(struct dma_buf_attachment *,
     149                 :            :                               struct sg_table *,
     150                 :            :                               enum dma_data_direction);
     151                 :            : 
     152                 :            :         /* TODO: Add try_map_dma_buf version, to return immed with -EBUSY
     153                 :            :          * if the call would block.
     154                 :            :          */
     155                 :            : 
     156                 :            :         /**
     157                 :            :          * @release:
     158                 :            :          *
     159                 :            :          * Called after the last dma_buf_put to release the &dma_buf, and
     160                 :            :          * mandatory.
     161                 :            :          */
     162                 :            :         void (*release)(struct dma_buf *);
     163                 :            : 
     164                 :            :         /**
     165                 :            :          * @begin_cpu_access:
     166                 :            :          *
     167                 :            :          * This is called from dma_buf_begin_cpu_access() and allows the
     168                 :            :          * exporter to ensure that the memory is actually available for cpu
     169                 :            :          * access - the exporter might need to allocate or swap-in and pin the
     170                 :            :          * backing storage. The exporter also needs to ensure that cpu access is
     171                 :            :          * coherent for the access direction. The direction can be used by the
     172                 :            :          * exporter to optimize the cache flushing, i.e. access with a different
     173                 :            :          * direction (read instead of write) might return stale or even bogus
     174                 :            :          * data (e.g. when the exporter needs to copy the data to temporary
     175                 :            :          * storage).
     176                 :            :          *
     177                 :            :          * This callback is optional.
     178                 :            :          *
     179                 :            :          * FIXME: This is both called through the DMA_BUF_IOCTL_SYNC command
     180                 :            :          * from userspace (where storage shouldn't be pinned to avoid handing
     181                 :            :          * de-factor mlock rights to userspace) and for the kernel-internal
     182                 :            :          * users of the various kmap interfaces, where the backing storage must
     183                 :            :          * be pinned to guarantee that the atomic kmap calls can succeed. Since
     184                 :            :          * there's no in-kernel users of the kmap interfaces yet this isn't a
     185                 :            :          * real problem.
     186                 :            :          *
     187                 :            :          * Returns:
     188                 :            :          *
     189                 :            :          * 0 on success or a negative error code on failure. This can for
     190                 :            :          * example fail when the backing storage can't be allocated. Can also
     191                 :            :          * return -ERESTARTSYS or -EINTR when the call has been interrupted and
     192                 :            :          * needs to be restarted.
     193                 :            :          */
     194                 :            :         int (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction);
     195                 :            : 
     196                 :            :         /**
     197                 :            :          * @end_cpu_access:
     198                 :            :          *
     199                 :            :          * This is called from dma_buf_end_cpu_access() when the importer is
     200                 :            :          * done accessing the CPU. The exporter can use this to flush caches and
     201                 :            :          * unpin any resources pinned in @begin_cpu_access.
     202                 :            :          * The result of any dma_buf kmap calls after end_cpu_access is
     203                 :            :          * undefined.
     204                 :            :          *
     205                 :            :          * This callback is optional.
     206                 :            :          *
     207                 :            :          * Returns:
     208                 :            :          *
     209                 :            :          * 0 on success or a negative error code on failure. Can return
     210                 :            :          * -ERESTARTSYS or -EINTR when the call has been interrupted and needs
     211                 :            :          * to be restarted.
     212                 :            :          */
     213                 :            :         int (*end_cpu_access)(struct dma_buf *, enum dma_data_direction);
     214                 :            : 
     215                 :            :         /**
     216                 :            :          * @mmap:
     217                 :            :          *
     218                 :            :          * This callback is used by the dma_buf_mmap() function
     219                 :            :          *
     220                 :            :          * Note that the mapping needs to be incoherent, userspace is expected
     221                 :            :          * to braket CPU access using the DMA_BUF_IOCTL_SYNC interface.
     222                 :            :          *
     223                 :            :          * Because dma-buf buffers have invariant size over their lifetime, the
     224                 :            :          * dma-buf core checks whether a vma is too large and rejects such
     225                 :            :          * mappings. The exporter hence does not need to duplicate this check.
     226                 :            :          * Drivers do not need to check this themselves.
     227                 :            :          *
     228                 :            :          * If an exporter needs to manually flush caches and hence needs to fake
     229                 :            :          * coherency for mmap support, it needs to be able to zap all the ptes
     230                 :            :          * pointing at the backing storage. Now linux mm needs a struct
     231                 :            :          * address_space associated with the struct file stored in vma->vm_file
     232                 :            :          * to do that with the function unmap_mapping_range. But the dma_buf
     233                 :            :          * framework only backs every dma_buf fd with the anon_file struct file,
     234                 :            :          * i.e. all dma_bufs share the same file.
     235                 :            :          *
     236                 :            :          * Hence exporters need to setup their own file (and address_space)
     237                 :            :          * association by setting vma->vm_file and adjusting vma->vm_pgoff in
     238                 :            :          * the dma_buf mmap callback. In the specific case of a gem driver the
     239                 :            :          * exporter could use the shmem file already provided by gem (and set
     240                 :            :          * vm_pgoff = 0). Exporters can then zap ptes by unmapping the
     241                 :            :          * corresponding range of the struct address_space associated with their
     242                 :            :          * own file.
     243                 :            :          *
     244                 :            :          * This callback is optional.
     245                 :            :          *
     246                 :            :          * Returns:
     247                 :            :          *
     248                 :            :          * 0 on success or a negative error code on failure.
     249                 :            :          */
     250                 :            :         int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
     251                 :            : 
     252                 :            :         void *(*vmap)(struct dma_buf *);
     253                 :            :         void (*vunmap)(struct dma_buf *, void *vaddr);
     254                 :            : };
     255                 :            : 
     256                 :            : /**
     257                 :            :  * struct dma_buf - shared buffer object
     258                 :            :  * @size: size of the buffer
     259                 :            :  * @file: file pointer used for sharing buffers across, and for refcounting.
     260                 :            :  * @attachments: list of dma_buf_attachment that denotes all devices attached,
     261                 :            :  *               protected by dma_resv lock.
     262                 :            :  * @ops: dma_buf_ops associated with this buffer object.
     263                 :            :  * @lock: used internally to serialize list manipulation, attach/detach and
     264                 :            :  *        vmap/unmap
     265                 :            :  * @vmapping_counter: used internally to refcnt the vmaps
     266                 :            :  * @vmap_ptr: the current vmap ptr if vmapping_counter > 0
     267                 :            :  * @exp_name: name of the exporter; useful for debugging.
     268                 :            :  * @name: userspace-provided name; useful for accounting and debugging,
     269                 :            :  *        protected by @resv.
     270                 :            :  * @owner: pointer to exporter module; used for refcounting when exporter is a
     271                 :            :  *         kernel module.
     272                 :            :  * @list_node: node for dma_buf accounting and debugging.
     273                 :            :  * @priv: exporter specific private data for this buffer object.
     274                 :            :  * @resv: reservation object linked to this dma-buf
     275                 :            :  * @poll: for userspace poll support
     276                 :            :  * @cb_excl: for userspace poll support
     277                 :            :  * @cb_shared: for userspace poll support
     278                 :            :  *
     279                 :            :  * This represents a shared buffer, created by calling dma_buf_export(). The
     280                 :            :  * userspace representation is a normal file descriptor, which can be created by
     281                 :            :  * calling dma_buf_fd().
     282                 :            :  *
     283                 :            :  * Shared dma buffers are reference counted using dma_buf_put() and
     284                 :            :  * get_dma_buf().
     285                 :            :  *
     286                 :            :  * Device DMA access is handled by the separate &struct dma_buf_attachment.
     287                 :            :  */
     288                 :            : struct dma_buf {
     289                 :            :         size_t size;
     290                 :            :         struct file *file;
     291                 :            :         struct list_head attachments;
     292                 :            :         const struct dma_buf_ops *ops;
     293                 :            :         struct mutex lock;
     294                 :            :         unsigned vmapping_counter;
     295                 :            :         void *vmap_ptr;
     296                 :            :         const char *exp_name;
     297                 :            :         const char *name;
     298                 :            :         struct module *owner;
     299                 :            :         struct list_head list_node;
     300                 :            :         void *priv;
     301                 :            :         struct dma_resv *resv;
     302                 :            : 
     303                 :            :         /* poll support */
     304                 :            :         wait_queue_head_t poll;
     305                 :            : 
     306                 :            :         struct dma_buf_poll_cb_t {
     307                 :            :                 struct dma_fence_cb cb;
     308                 :            :                 wait_queue_head_t *poll;
     309                 :            : 
     310                 :            :                 __poll_t active;
     311                 :            :         } cb_excl, cb_shared;
     312                 :            : };
     313                 :            : 
     314                 :            : /**
     315                 :            :  * struct dma_buf_attachment - holds device-buffer attachment data
     316                 :            :  * @dmabuf: buffer for this attachment.
     317                 :            :  * @dev: device attached to the buffer.
     318                 :            :  * @node: list of dma_buf_attachment, protected by dma_resv lock of the dmabuf.
     319                 :            :  * @sgt: cached mapping.
     320                 :            :  * @dir: direction of cached mapping.
     321                 :            :  * @priv: exporter specific attachment data.
     322                 :            :  * @dynamic_mapping: true if dma_buf_map/unmap_attachment() is called with the
     323                 :            :  * dma_resv lock held.
     324                 :            :  *
     325                 :            :  * This structure holds the attachment information between the dma_buf buffer
     326                 :            :  * and its user device(s). The list contains one attachment struct per device
     327                 :            :  * attached to the buffer.
     328                 :            :  *
     329                 :            :  * An attachment is created by calling dma_buf_attach(), and released again by
     330                 :            :  * calling dma_buf_detach(). The DMA mapping itself needed to initiate a
     331                 :            :  * transfer is created by dma_buf_map_attachment() and freed again by calling
     332                 :            :  * dma_buf_unmap_attachment().
     333                 :            :  */
     334                 :            : struct dma_buf_attachment {
     335                 :            :         struct dma_buf *dmabuf;
     336                 :            :         struct device *dev;
     337                 :            :         struct list_head node;
     338                 :            :         struct sg_table *sgt;
     339                 :            :         enum dma_data_direction dir;
     340                 :            :         bool dynamic_mapping;
     341                 :            :         void *priv;
     342                 :            : };
     343                 :            : 
     344                 :            : /**
     345                 :            :  * struct dma_buf_export_info - holds information needed to export a dma_buf
     346                 :            :  * @exp_name:   name of the exporter - useful for debugging.
     347                 :            :  * @owner:      pointer to exporter module - used for refcounting kernel module
     348                 :            :  * @ops:        Attach allocator-defined dma buf ops to the new buffer
     349                 :            :  * @size:       Size of the buffer
     350                 :            :  * @flags:      mode flags for the file
     351                 :            :  * @resv:       reservation-object, NULL to allocate default one
     352                 :            :  * @priv:       Attach private data of allocator to this buffer
     353                 :            :  *
     354                 :            :  * This structure holds the information required to export the buffer. Used
     355                 :            :  * with dma_buf_export() only.
     356                 :            :  */
     357                 :            : struct dma_buf_export_info {
     358                 :            :         const char *exp_name;
     359                 :            :         struct module *owner;
     360                 :            :         const struct dma_buf_ops *ops;
     361                 :            :         size_t size;
     362                 :            :         int flags;
     363                 :            :         struct dma_resv *resv;
     364                 :            :         void *priv;
     365                 :            : };
     366                 :            : 
     367                 :            : /**
     368                 :            :  * DEFINE_DMA_BUF_EXPORT_INFO - helper macro for exporters
     369                 :            :  * @name: export-info name
     370                 :            :  *
     371                 :            :  * DEFINE_DMA_BUF_EXPORT_INFO macro defines the &struct dma_buf_export_info,
     372                 :            :  * zeroes it out and pre-populates exp_name in it.
     373                 :            :  */
     374                 :            : #define DEFINE_DMA_BUF_EXPORT_INFO(name)        \
     375                 :            :         struct dma_buf_export_info name = { .exp_name = KBUILD_MODNAME, \
     376                 :            :                                          .owner = THIS_MODULE }
     377                 :            : 
     378                 :            : /**
     379                 :            :  * get_dma_buf - convenience wrapper for get_file.
     380                 :            :  * @dmabuf:     [in]    pointer to dma_buf
     381                 :            :  *
     382                 :            :  * Increments the reference count on the dma-buf, needed in case of drivers
     383                 :            :  * that either need to create additional references to the dmabuf on the
     384                 :            :  * kernel side.  For example, an exporter that needs to keep a dmabuf ptr
     385                 :            :  * so that subsequent exports don't create a new dmabuf.
     386                 :            :  */
     387                 :          0 : static inline void get_dma_buf(struct dma_buf *dmabuf)
     388                 :            : {
     389                 :          0 :         get_file(dmabuf->file);
     390                 :          0 : }
     391                 :            : 
     392                 :            : /**
     393                 :            :  * dma_buf_is_dynamic - check if a DMA-buf uses dynamic mappings.
     394                 :            :  * @dmabuf: the DMA-buf to check
     395                 :            :  *
     396                 :            :  * Returns true if a DMA-buf exporter wants to be called with the dma_resv
     397                 :            :  * locked for the map/unmap callbacks, false if it doesn't wants to be called
     398                 :            :  * with the lock held.
     399                 :            :  */
     400                 :          0 : static inline bool dma_buf_is_dynamic(struct dma_buf *dmabuf)
     401                 :            : {
     402   [ #  #  #  #  :          0 :         return dmabuf->ops->dynamic_mapping;
          #  #  #  #  #  
                #  #  # ]
     403                 :            : }
     404                 :            : 
     405                 :            : /**
     406                 :            :  * dma_buf_attachment_is_dynamic - check if a DMA-buf attachment uses dynamic
     407                 :            :  * mappinsg
     408                 :            :  * @attach: the DMA-buf attachment to check
     409                 :            :  *
     410                 :            :  * Returns true if a DMA-buf importer wants to call the map/unmap functions with
     411                 :            :  * the dma_resv lock held.
     412                 :            :  */
     413                 :            : static inline bool
     414                 :          0 : dma_buf_attachment_is_dynamic(struct dma_buf_attachment *attach)
     415                 :            : {
     416   [ #  #  #  #  :          0 :         return attach->dynamic_mapping;
                   #  # ]
     417                 :            : }
     418                 :            : 
     419                 :            : struct dma_buf_attachment *dma_buf_attach(struct dma_buf *dmabuf,
     420                 :            :                                           struct device *dev);
     421                 :            : struct dma_buf_attachment *
     422                 :            : dma_buf_dynamic_attach(struct dma_buf *dmabuf, struct device *dev,
     423                 :            :                        bool dynamic_mapping);
     424                 :            : void dma_buf_detach(struct dma_buf *dmabuf,
     425                 :            :                     struct dma_buf_attachment *attach);
     426                 :            : 
     427                 :            : struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
     428                 :            : 
     429                 :            : int dma_buf_fd(struct dma_buf *dmabuf, int flags);
     430                 :            : struct dma_buf *dma_buf_get(int fd);
     431                 :            : void dma_buf_put(struct dma_buf *dmabuf);
     432                 :            : 
     433                 :            : struct sg_table *dma_buf_map_attachment(struct dma_buf_attachment *,
     434                 :            :                                         enum dma_data_direction);
     435                 :            : void dma_buf_unmap_attachment(struct dma_buf_attachment *, struct sg_table *,
     436                 :            :                                 enum dma_data_direction);
     437                 :            : void dma_buf_move_notify(struct dma_buf *dma_buf);
     438                 :            : int dma_buf_begin_cpu_access(struct dma_buf *dma_buf,
     439                 :            :                              enum dma_data_direction dir);
     440                 :            : int dma_buf_end_cpu_access(struct dma_buf *dma_buf,
     441                 :            :                            enum dma_data_direction dir);
     442                 :            : 
     443                 :            : int dma_buf_mmap(struct dma_buf *, struct vm_area_struct *,
     444                 :            :                  unsigned long);
     445                 :            : void *dma_buf_vmap(struct dma_buf *);
     446                 :            : void dma_buf_vunmap(struct dma_buf *, void *vaddr);
     447                 :            : #endif /* __DMA_BUF_H__ */

Generated by: LCOV version 1.14