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