1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/* $NetBSD: linux_dma_buf.c,v 1.16 2023/02/21 11:40:13 riastradh Exp $ */
/*-
* Copyright (c) 2018 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Taylor R. Campbell.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: linux_dma_buf.c,v 1.16 2023/02/21 11:40:13 riastradh Exp $");
#include <sys/types.h>
#include <sys/atomic.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/kmem.h>
#include <sys/mutex.h>
#include <linux/dma-buf.h>
#include <linux/err.h>
#include <linux/dma-resv.h>
static int dmabuf_fop_poll(struct file *, int);
static int dmabuf_fop_close(struct file *);
static int dmabuf_fop_kqfilter(struct file *, struct knote *);
static int dmabuf_fop_mmap(struct file *, off_t *, size_t, int, int *,
int *, struct uvm_object **, int *);
static int dmabuf_fop_seek(struct file *fp, off_t delta, int whence,
off_t *newoffp, int flags);
static const struct fileops dmabuf_fileops = {
.fo_name = "dmabuf",
.fo_read = fbadop_read,
.fo_write = fbadop_write,
.fo_ioctl = fbadop_ioctl,
.fo_fcntl = fnullop_fcntl,
.fo_poll = dmabuf_fop_poll,
.fo_stat = fbadop_stat,
.fo_close = dmabuf_fop_close,
.fo_kqfilter = dmabuf_fop_kqfilter,
.fo_restart = fnullop_restart,
.fo_mmap = dmabuf_fop_mmap,
.fo_seek = dmabuf_fop_seek,
};
struct dma_buf *
dma_buf_export(struct dma_buf_export_info *info)
{
struct dma_buf *dmabuf;
if (info->resv == NULL) {
dmabuf = kmem_zalloc(offsetof(struct dma_buf, db_resv_int[1]),
KM_SLEEP);
} else {
dmabuf = kmem_zalloc(sizeof(*dmabuf), KM_SLEEP);
}
dmabuf->priv = info->priv;
dmabuf->ops = info->ops;
dmabuf->size = info->size;
dmabuf->resv = info->resv;
mutex_init(&dmabuf->db_lock, MUTEX_DEFAULT, IPL_NONE);
dmabuf->db_refcnt = 1;
dma_resv_poll_init(&dmabuf->db_resv_poll);
if (dmabuf->resv == NULL) {
dmabuf->resv = &dmabuf->db_resv_int[0];
dma_resv_init(dmabuf->resv);
}
return dmabuf;
}
int
dma_buf_fd(struct dma_buf *dmabuf, int flags)
{
struct file *file;
int fd;
unsigned refcnt __diagused;
int ret;
/* XXX errno NetBSD->Linux */
ret = -fd_allocfile(&file, &fd);
if (ret)
goto out0;
refcnt = atomic_inc_uint_nv(&dmabuf->db_refcnt);
KASSERT(refcnt > 1);
file->f_type = DTYPE_MISC;
file->f_flag = 0; /* XXX DRM code allows only O_CLOEXEC. */
file->f_ops = &dmabuf_fileops;
file->f_data = dmabuf;
fd_set_exclose(curlwp, fd, (flags & O_CLOEXEC) != 0);
fd_affix(curproc, file, fd);
ret = fd;
out0: return ret;
}
struct dma_buf *
dma_buf_get(int fd)
{
struct file *file;
struct dma_buf *dmabuf;
unsigned refcnt __diagused;
int error;
if ((file = fd_getfile(fd)) == NULL) {
error = EBADF;
goto fail0;
}
if (file->f_type != DTYPE_MISC || file->f_ops != &dmabuf_fileops) {
error = EINVAL;
goto fail1;
}
dmabuf = file->f_data;
refcnt = atomic_inc_uint_nv(&dmabuf->db_refcnt);
KASSERT(refcnt > 1);
fd_putfile(fd);
return dmabuf;
fail1: fd_putfile(fd);
fail0: KASSERT(error);
return ERR_PTR(-error);
}
void
get_dma_buf(struct dma_buf *dmabuf)
{
unsigned refcnt __diagused;
refcnt = atomic_inc_uint_nv(&dmabuf->db_refcnt);
KASSERT(refcnt > 1);
}
void
dma_buf_put(struct dma_buf *dmabuf)
{
membar_release();
if (atomic_dec_uint_nv(&dmabuf->db_refcnt) != 0)
return;
membar_acquire();
dma_resv_poll_fini(&dmabuf->db_resv_poll);
mutex_destroy(&dmabuf->db_lock);
if (dmabuf->resv == &dmabuf->db_resv_int[0]) {
dma_resv_fini(dmabuf->resv);
kmem_free(dmabuf, offsetof(struct dma_buf, db_resv_int[1]));
} else {
kmem_free(dmabuf, sizeof(*dmabuf));
}
}
struct dma_buf_attachment *
dma_buf_dynamic_attach(struct dma_buf *dmabuf, bus_dma_tag_t dmat,
bool dynamic_mapping)
{
struct dma_buf_attachment *attach;
int ret = 0;
attach = kmem_zalloc(sizeof(*attach), KM_SLEEP);
attach->dmabuf = dmabuf;
attach->dev = dmat;
attach->dynamic_mapping = dynamic_mapping;
mutex_enter(&dmabuf->db_lock);
if (dmabuf->ops->attach)
ret = dmabuf->ops->attach(dmabuf, attach);
mutex_exit(&dmabuf->db_lock);
if (ret)
goto fail0;
if (attach->dynamic_mapping != dmabuf->ops->dynamic_mapping)
panic("%s: NYI", __func__);
return attach;
fail0: kmem_free(attach, sizeof(*attach));
return ERR_PTR(ret);
}
struct dma_buf_attachment *
dma_buf_attach(struct dma_buf *dmabuf, bus_dma_tag_t dmat)
{
return dma_buf_dynamic_attach(dmabuf, dmat, /*dynamic_mapping*/false);
}
void
dma_buf_detach(struct dma_buf *dmabuf, struct dma_buf_attachment *attach)
{
mutex_enter(&dmabuf->db_lock);
if (dmabuf->ops->detach)
dmabuf->ops->detach(dmabuf, attach);
mutex_exit(&dmabuf->db_lock);
kmem_free(attach, sizeof(*attach));
}
struct sg_table *
dma_buf_map_attachment(struct dma_buf_attachment *attach,
enum dma_data_direction dir)
{
struct sg_table *sg;
if (attach->dmabuf->ops->dynamic_mapping)
dma_resv_lock(attach->dmabuf->resv, NULL);
sg = attach->dmabuf->ops->map_dma_buf(attach, dir);
if (attach->dmabuf->ops->dynamic_mapping)
dma_resv_unlock(attach->dmabuf->resv);
return sg;
}
void
dma_buf_unmap_attachment(struct dma_buf_attachment *attach,
struct sg_table *sg, enum dma_data_direction dir)
{
if (attach->dmabuf->ops->dynamic_mapping)
dma_resv_lock(attach->dmabuf->resv, NULL);
attach->dmabuf->ops->unmap_dma_buf(attach, sg, dir);
if (attach->dmabuf->ops->dynamic_mapping)
dma_resv_unlock(attach->dmabuf->resv);
}
static int
dmabuf_fop_close(struct file *file)
{
struct dma_buf *dmabuf = file->f_data;
dma_buf_put(dmabuf);
return 0;
}
static int
dmabuf_fop_poll(struct file *file, int events)
{
struct dma_buf *dmabuf = file->f_data;
struct dma_resv_poll *rpoll = &dmabuf->db_resv_poll;
return dma_resv_do_poll(dmabuf->resv, events, rpoll);
}
static int
dmabuf_fop_kqfilter(struct file *file, struct knote *kn)
{
struct dma_buf *dmabuf = file->f_data;
struct dma_resv_poll *rpoll = &dmabuf->db_resv_poll;
return dma_resv_kqfilter(dmabuf->resv, kn, rpoll);
}
static int
dmabuf_fop_mmap(struct file *file, off_t *offp, size_t size, int prot,
int *flagsp, int *advicep, struct uvm_object **uobjp, int *maxprotp)
{
struct dma_buf *dmabuf = file->f_data;
if (size > dmabuf->size)
return EINVAL;
return dmabuf->ops->mmap(dmabuf, offp, size, prot, flagsp, advicep,
uobjp, maxprotp);
}
/*
* We don't actually do anything with the file offset; this is just how
* libdrm_amdgpu expects to find the size of the DMA buf. (Why it
* doesn't use fstat is unclear, but it doesn't really matter.)
*/
static int
dmabuf_fop_seek(struct file *fp, off_t delta, int whence, off_t *newoffp,
int flags)
{
const off_t OFF_MAX = __type_max(off_t);
struct dma_buf *dmabuf = fp->f_data;
off_t base, newoff;
int error;
mutex_enter(&fp->f_lock);
switch (whence) {
case SEEK_CUR:
base = fp->f_offset;
break;
case SEEK_END:
base = dmabuf->size;
break;
case SEEK_SET:
base = 0;
break;
default:
error = EINVAL;
goto out;
}
/* Check for arithmetic overflow and reject negative offsets. */
if (base < 0 || delta > OFF_MAX - base || base + delta < 0) {
error = EINVAL;
goto out;
}
/* Compute the new offset. */
newoff = base + delta;
/* Success! */
if (newoffp)
*newoffp = newoff;
if (flags & FOF_UPDATE_OFFSET)
fp->f_offset = newoff;
error = 0;
out: mutex_exit(&fp->f_lock);
return error;
}
|