xref: /dflybsd-src/sys/dev/drm/include/linux/dma-buf.h (revision a85cb24f18e3804e75ab8bcda7692564d0563317)
194a02576SFrançois Tigeot /*
2*a85cb24fSFrançois Tigeot  * Copyright (c) 2018-2020 François Tigeot <ftigeot@wolfpond.org>
394a02576SFrançois Tigeot  * All rights reserved.
494a02576SFrançois Tigeot  *
594a02576SFrançois Tigeot  * Redistribution and use in source and binary forms, with or without
694a02576SFrançois Tigeot  * modification, are permitted provided that the following conditions
794a02576SFrançois Tigeot  * are met:
894a02576SFrançois Tigeot  * 1. Redistributions of source code must retain the above copyright
994a02576SFrançois Tigeot  *    notice unmodified, this list of conditions, and the following
1094a02576SFrançois Tigeot  *    disclaimer.
1194a02576SFrançois Tigeot  * 2. Redistributions in binary form must reproduce the above copyright
1294a02576SFrançois Tigeot  *    notice, this list of conditions and the following disclaimer in the
1394a02576SFrançois Tigeot  *    documentation and/or other materials provided with the distribution.
1494a02576SFrançois Tigeot  *
1594a02576SFrançois Tigeot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1694a02576SFrançois Tigeot  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1794a02576SFrançois Tigeot  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1894a02576SFrançois Tigeot  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1994a02576SFrançois Tigeot  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2094a02576SFrançois Tigeot  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2194a02576SFrançois Tigeot  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2294a02576SFrançois Tigeot  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2394a02576SFrançois Tigeot  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2494a02576SFrançois Tigeot  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2594a02576SFrançois Tigeot  */
2694a02576SFrançois Tigeot 
2794a02576SFrançois Tigeot #ifndef LINUX_DMA_BUF_H
2894a02576SFrançois Tigeot #define LINUX_DMA_BUF_H
2994a02576SFrançois Tigeot 
3094a02576SFrançois Tigeot #include <linux/err.h>
3194a02576SFrançois Tigeot #include <linux/scatterlist.h>
3294a02576SFrançois Tigeot #include <linux/list.h>
3394a02576SFrançois Tigeot #include <linux/dma-mapping.h>
3494a02576SFrançois Tigeot #include <linux/fs.h>
356559babbSFrançois Tigeot #include <linux/dma-fence.h>
3694a02576SFrançois Tigeot #include <linux/wait.h>
3794a02576SFrançois Tigeot 
38565c8854SFrançois Tigeot #include <linux/slab.h>
39565c8854SFrançois Tigeot 
40565c8854SFrançois Tigeot struct dma_buf;
41565c8854SFrançois Tigeot struct dma_buf_attachment;
42565c8854SFrançois Tigeot 
438665870eSFrançois Tigeot struct dma_buf_ops {
448665870eSFrançois Tigeot 	struct sg_table * (*map_dma_buf)(struct dma_buf_attachment *,
458665870eSFrançois Tigeot 						enum dma_data_direction);
468665870eSFrançois Tigeot 	void (*unmap_dma_buf)(struct dma_buf_attachment *,
478665870eSFrançois Tigeot 						struct sg_table *,
488665870eSFrançois Tigeot 						enum dma_data_direction);
498665870eSFrançois Tigeot 	void (*release)(struct dma_buf *);
50*a85cb24fSFrançois Tigeot 	void *(*map)(struct dma_buf *, unsigned long);
51*a85cb24fSFrançois Tigeot 	void *(*map_atomic)(struct dma_buf *, unsigned long);
52*a85cb24fSFrançois Tigeot 	void (*unmap)(struct dma_buf *, unsigned long, void *);
53*a85cb24fSFrançois Tigeot 	void (*unmap_atomic)(struct dma_buf *, unsigned long, void *);
548665870eSFrançois Tigeot 	int (*mmap)(struct dma_buf *, struct vm_area_struct *vma);
558665870eSFrançois Tigeot 	void *(*vmap)(struct dma_buf *);
568665870eSFrançois Tigeot 	void (*vunmap)(struct dma_buf *, void *vaddr);
578665870eSFrançois Tigeot 	int (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction);
588665870eSFrançois Tigeot 	int (*end_cpu_access)(struct dma_buf *, enum dma_data_direction);
59565c8854SFrançois Tigeot 	int (*attach)(struct dma_buf *, struct device *, struct dma_buf_attachment *);
60565c8854SFrançois Tigeot 	void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
618665870eSFrançois Tigeot };
628665870eSFrançois Tigeot 
6394a02576SFrançois Tigeot struct dma_buf {
6494a02576SFrançois Tigeot 	struct reservation_object *resv;
658665870eSFrançois Tigeot 	void *priv;
668665870eSFrançois Tigeot 	const struct dma_buf_ops *ops;
678665870eSFrançois Tigeot 	size_t size;
68269e9b1eSFrançois Tigeot 	struct file *file;
6994a02576SFrançois Tigeot };
7094a02576SFrançois Tigeot 
7194a02576SFrançois Tigeot struct dma_buf_attachment {
7294a02576SFrançois Tigeot 	struct dma_buf *dmabuf;
738665870eSFrançois Tigeot 	struct device *dev;
74565c8854SFrançois Tigeot 	void *priv;
7594a02576SFrançois Tigeot };
7694a02576SFrançois Tigeot 
778665870eSFrançois Tigeot struct dma_buf_export_info {
788665870eSFrançois Tigeot 	const struct dma_buf_ops *ops;
798665870eSFrançois Tigeot 	size_t size;
808665870eSFrançois Tigeot 	int flags;
818665870eSFrançois Tigeot 	void *priv;
82565c8854SFrançois Tigeot 	struct reservation_object *resv;
838665870eSFrançois Tigeot };
848665870eSFrançois Tigeot 
858665870eSFrançois Tigeot struct dma_buf *dma_buf_export(const struct dma_buf_export_info *exp_info);
868665870eSFrançois Tigeot 
878665870eSFrançois Tigeot #define DEFINE_DMA_BUF_EXPORT_INFO(name)	\
888665870eSFrançois Tigeot 	struct dma_buf_export_info name = {	\
898665870eSFrançois Tigeot 	}
908665870eSFrançois Tigeot 
918665870eSFrançois Tigeot struct sg_table * dma_buf_map_attachment(struct dma_buf_attachment *,
928665870eSFrançois Tigeot 						enum dma_data_direction);
938665870eSFrançois Tigeot void dma_buf_unmap_attachment(struct dma_buf_attachment *,
948665870eSFrançois Tigeot 				struct sg_table *, enum dma_data_direction);
958665870eSFrançois Tigeot 
968665870eSFrançois Tigeot static inline struct dma_buf_attachment *
dma_buf_attach(struct dma_buf * dmabuf,struct device * dev)978665870eSFrançois Tigeot dma_buf_attach(struct dma_buf *dmabuf, struct device *dev)
988665870eSFrançois Tigeot {
998665870eSFrançois Tigeot 	/* XXX: this function is a stub */
1008665870eSFrançois Tigeot 	struct dma_buf_attachment *attach;
1018665870eSFrançois Tigeot 
1028665870eSFrançois Tigeot 	attach = kmalloc(sizeof(struct dma_buf_attachment), M_DRM, M_WAITOK | M_ZERO);
1038665870eSFrançois Tigeot 
1048665870eSFrançois Tigeot 	return attach;
1058665870eSFrançois Tigeot }
1068665870eSFrançois Tigeot 
107269e9b1eSFrançois Tigeot static inline void
get_dma_buf(struct dma_buf * dmabuf)108269e9b1eSFrançois Tigeot get_dma_buf(struct dma_buf *dmabuf)
109269e9b1eSFrançois Tigeot {
110269e9b1eSFrançois Tigeot 	fhold(dmabuf->file);
111269e9b1eSFrançois Tigeot }
112269e9b1eSFrançois Tigeot 
113269e9b1eSFrançois Tigeot static inline void
dma_buf_put(struct dma_buf * dmabuf)114269e9b1eSFrançois Tigeot dma_buf_put(struct dma_buf *dmabuf)
115269e9b1eSFrançois Tigeot {
116269e9b1eSFrançois Tigeot 	if (dmabuf == NULL)
117269e9b1eSFrançois Tigeot 		return;
118269e9b1eSFrançois Tigeot 
119269e9b1eSFrançois Tigeot 	if (dmabuf->file == NULL)
120269e9b1eSFrançois Tigeot 		return;
121269e9b1eSFrançois Tigeot 
122269e9b1eSFrançois Tigeot 	fdrop(dmabuf->file);
123269e9b1eSFrançois Tigeot }
124269e9b1eSFrançois Tigeot 
125269e9b1eSFrançois Tigeot int dma_buf_fd(struct dma_buf *dmabuf, int flags);
126269e9b1eSFrançois Tigeot 
127269e9b1eSFrançois Tigeot struct dma_buf *dma_buf_get(int fd);
128269e9b1eSFrançois Tigeot 
129269e9b1eSFrançois Tigeot static inline void
dma_buf_detach(struct dma_buf * dmabuf,struct dma_buf_attachment * dmabuf_attach)130269e9b1eSFrançois Tigeot dma_buf_detach(struct dma_buf *dmabuf,
131269e9b1eSFrançois Tigeot 	       struct dma_buf_attachment *dmabuf_attach)
132269e9b1eSFrançois Tigeot {
133269e9b1eSFrançois Tigeot 	kprintf("dma_buf_detach: Not implemented\n");
134269e9b1eSFrançois Tigeot }
135269e9b1eSFrançois Tigeot 
13694a02576SFrançois Tigeot #endif /* LINUX_DMA_BUF_H */
137