dma-buf.h revision 1.10 1 /* $NetBSD: dma-buf.h,v 1.10 2021/12/19 10:38:23 riastradh 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/err.h>
40 #include <linux/dma-resv.h>
41
42 struct device;
43 struct dma_buf;
44 struct dma_buf_attachment;
45 struct dma_buf_export_info;
46 struct dma_buf_ops;
47 struct file;
48 struct module;
49 struct dma_resv;
50 struct sg_table;
51 struct uvm_object;
52
53 enum dma_data_direction {
54 DMA_NONE = 0,
55 DMA_TO_DEVICE = 1,
56 DMA_FROM_DEVICE = 2,
57 DMA_BIDIRECTIONAL = 3,
58 };
59
60 struct dma_buf_ops {
61 bool cache_sgt_mapping;
62 int (*attach)(struct dma_buf *, struct dma_buf_attachment *);
63 void (*detach)(struct dma_buf *, struct dma_buf_attachment *);
64 struct sg_table *
65 (*map_dma_buf)(struct dma_buf_attachment *,
66 enum dma_data_direction);
67 void (*unmap_dma_buf)(struct dma_buf_attachment *,
68 struct sg_table *, enum dma_data_direction);
69 void (*release)(struct dma_buf *);
70 int (*begin_cpu_access)(struct dma_buf *, enum dma_data_direction);
71 int (*end_cpu_access)(struct dma_buf *, enum dma_data_direction);
72 int (*mmap)(struct dma_buf *, off_t *, size_t, int, int *,
73 int *, struct uvm_object **, int *);
74 void * (*vmap)(struct dma_buf *);
75 void (*vunmap)(struct dma_buf *, void *);
76 };
77
78 struct dma_buf {
79 void *priv;
80 const struct dma_buf_ops *ops;
81 size_t size;
82 struct dma_resv *resv;
83
84 kmutex_t db_lock;
85 volatile unsigned db_refcnt;
86 struct dma_resv_poll db_resv_poll;
87 struct dma_resv db_resv_int[];
88 };
89
90 struct dma_buf_attachment {
91 void *priv;
92 struct dma_buf *dmabuf;
93 struct device *dev;
94 };
95
96 struct dma_buf_export_info {
97 #if 0
98 const char *exp_name;
99 struct module *owner;
100 #endif
101 const struct dma_buf_ops *ops;
102 size_t size;
103 int flags;
104 struct dma_resv *resv;
105 void *priv;
106 };
107
108 #define DEFINE_DMA_BUF_EXPORT_INFO(info) \
109 struct dma_buf_export_info info = { .priv = NULL }
110
111 #define dma_buf_attach linux_dma_buf_attach
112 #define dma_buf_detach linux_dma_buf_detach
113 #define dma_buf_export linux_dma_buf_export
114 #define dma_buf_fd linux_dma_buf_fd
115 #define dma_buf_get linux_dma_buf_get
116 #define dma_buf_map_attachment linux_dma_buf_map_attachment
117 #define dma_buf_put linux_dma_buf_put
118 #define dma_buf_unmap_attachment linux_dma_buf_unmap_attachment
119 #define get_dma_buf linux_get_dma_buf
120
121 struct dma_buf *
122 dma_buf_export(struct dma_buf_export_info *);
123
124 int dma_buf_fd(struct dma_buf *, int);
125 struct dma_buf *
126 dma_buf_get(int);
127 void get_dma_buf(struct dma_buf *);
128 void dma_buf_put(struct dma_buf *);
129
130 struct dma_buf_attachment *
131 dma_buf_attach(struct dma_buf *, struct device *);
132 void dma_buf_detach(struct dma_buf *, struct dma_buf_attachment *);
133
134 struct sg_table *
135 dma_buf_map_attachment(struct dma_buf_attachment *,
136 enum dma_data_direction);
137 void dma_buf_unmap_attachment(struct dma_buf_attachment *,
138 struct sg_table *, enum dma_data_direction);
139
140 #endif /* _LINUX_DMA_BUF_H_ */
141