Home | History | Annotate | Line # | Download | only in linux
dma-buf.h revision 1.2.36.1
      1 /*	$NetBSD: dma-buf.h,v 1.2.36.1 2019/06/10 22:08:31 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2018 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Taylor R. Campbell.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 #ifndef _LINUX_DMA_BUF_H_
     33 #define _LINUX_DMA_BUF_H_
     34 
     35 #include <sys/types.h>
     36 #include <sys/bus.h>
     37 #include <sys/mutex.h>
     38 
     39 #include <linux/reservation.h>
     40 
     41 struct device;
     42 struct dma_buf;
     43 struct dma_buf_attachment;
     44 struct dma_buf_export_info;
     45 struct dma_buf_ops;
     46 struct file;
     47 struct module;
     48 struct reservation_object;
     49 struct sg_table;
     50 struct uvm_object;
     51 
     52 enum dma_data_direction {
     53 	DMA_NONE		= 0,
     54 	DMA_TO_DEVICE		= 1,
     55 	DMA_FROM_DEVICE		= 2,
     56 	DMA_BIDIRECTIONAL	= 3,
     57 };
     58 
     59 struct dma_buf_ops {
     60 	int	(*attach)(struct dma_buf *, struct device *,
     61 		    struct dma_buf_attachment *);
     62 	void	(*detach)(struct dma_buf *, struct dma_buf_attachment *);
     63 	struct sg_table *
     64 		(*map_dma_buf)(struct dma_buf_attachment *,
     65 		    enum dma_data_direction);
     66 	void	(*unmap_dma_buf)(struct dma_buf_attachment *,
     67 		    struct sg_table *, enum dma_data_direction);
     68 	void	(*release)(struct dma_buf *);
     69 	int	(*begin_cpu_access)(struct dma_buf *, size_t, size_t,
     70 		    enum dma_data_direction);
     71 	int	(*end_cpu_access)(struct dma_buf *, size_t, size_t,
     72 		    enum dma_data_direction);
     73 	void *	(*kmap_atomic)(struct dma_buf *, unsigned long);
     74 	void	(*kunmap_atomic)(struct dma_buf *, unsigned long, void *);
     75 	void *	(*kmap)(struct dma_buf *, unsigned long);
     76 	void	(*kunmap)(struct dma_buf *, unsigned long, void *);
     77 	int	(*mmap)(struct dma_buf *, off_t *, size_t, int, int *,
     78 		    int *, struct uvm_object **, int *);
     79 	void *	(*vmap)(struct dma_buf *);
     80 	void	(*vunmap)(struct dma_buf *, void *);
     81 };
     82 
     83 struct dma_buf {
     84 	void				*priv;
     85 	const struct dma_buf_ops	*ops;
     86 	size_t				size;
     87 	struct reservation_object	*resv;
     88 
     89 	kmutex_t			db_lock;
     90 	volatile unsigned		db_refcnt;
     91 	struct reservation_poll		db_resv_poll;
     92 	struct reservation_object	db_resv_int[];
     93 };
     94 
     95 struct dma_buf_attachment {
     96 	void				*priv;
     97 	struct dma_buf			*dmabuf;
     98 };
     99 
    100 struct dma_buf_export_info {
    101 #if 0
    102 	const char			*exp_name;
    103 	struct module			*owner;
    104 #endif
    105 	const struct dma_buf_ops	*ops;
    106 	size_t				size;
    107 	int				flags;
    108 	struct reservation_object	*resv;
    109 	void				*priv;
    110 };
    111 
    112 #define	DEFINE_DMA_BUF_EXPORT_INFO(info)				      \
    113 	struct dma_buf_export_info info = { .priv = NULL }
    114 
    115 #define	dma_buf_attach		linux_dma_buf_attach
    116 #define	dma_buf_detach		linux_dma_buf_detach
    117 #define	dma_buf_export		linux_dma_buf_export
    118 #define	dma_buf_fd		linux_dma_buf_fd
    119 #define	dma_buf_get		linux_dma_buf_get
    120 #define	dma_buf_map_attachment	linux_dma_buf_map_attachment
    121 #define	dma_buf_put		linux_dma_buf_put
    122 #define	dma_buf_unmap_attachment linux_dma_buf_unmap_attachment
    123 #define	get_dma_buf		linux_get_dma_buf
    124 
    125 struct dma_buf *
    126 	dma_buf_export(struct dma_buf_export_info *);
    127 
    128 int	dma_buf_fd(struct dma_buf *, int);
    129 struct dma_buf *
    130 	dma_buf_get(int);
    131 void	get_dma_buf(struct dma_buf *);
    132 void	dma_buf_put(struct dma_buf *);
    133 
    134 struct dma_buf_attachment *
    135 	dma_buf_attach(struct dma_buf *, struct device *);
    136 void	dma_buf_detach(struct dma_buf *, struct dma_buf_attachment *);
    137 
    138 struct sg_table *
    139 	dma_buf_map_attachment(struct dma_buf_attachment *,
    140 	    enum dma_data_direction);
    141 void	dma_buf_unmap_attachment(struct dma_buf_attachment *,
    142 	    struct sg_table *, enum dma_data_direction);
    143 
    144 #endif  /* _LINUX_DMA_BUF_H_ */
    145