drm_prime.c revision 1.5 1 1.2 riastrad /* $NetBSD: drm_prime.c,v 1.5 2018/08/27 15:26:50 riastradh Exp $ */
2 1.2 riastrad
3 1.1 riastrad /*
4 1.1 riastrad * Copyright 2012 Red Hat
5 1.1 riastrad *
6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a
7 1.1 riastrad * copy of this software and associated documentation files (the "Software"),
8 1.1 riastrad * to deal in the Software without restriction, including without limitation
9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the
11 1.1 riastrad * Software is furnished to do so, subject to the following conditions:
12 1.1 riastrad *
13 1.1 riastrad * The above copyright notice and this permission notice (including the next
14 1.1 riastrad * paragraph) shall be included in all copies or substantial portions of the
15 1.1 riastrad * Software.
16 1.1 riastrad *
17 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 1.1 riastrad * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 1.1 riastrad * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 1.1 riastrad * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
23 1.1 riastrad * IN THE SOFTWARE.
24 1.1 riastrad *
25 1.1 riastrad * Authors:
26 1.1 riastrad * Dave Airlie <airlied (at) redhat.com>
27 1.1 riastrad * Rob Clark <rob.clark (at) linaro.org>
28 1.1 riastrad *
29 1.1 riastrad */
30 1.1 riastrad
31 1.2 riastrad #include <sys/cdefs.h>
32 1.2 riastrad __KERNEL_RCSID(0, "$NetBSD: drm_prime.c,v 1.5 2018/08/27 15:26:50 riastradh Exp $");
33 1.2 riastrad
34 1.1 riastrad #include <linux/export.h>
35 1.1 riastrad #include <linux/dma-buf.h>
36 1.1 riastrad #include <drm/drmP.h>
37 1.2 riastrad #include <drm/drm_gem.h>
38 1.2 riastrad
39 1.2 riastrad #include "drm_internal.h"
40 1.1 riastrad
41 1.4 riastrad #ifdef __NetBSD__
42 1.4 riastrad
43 1.5 riastrad #include <drm/bus_dma_hacks.h>
44 1.5 riastrad
45 1.4 riastrad /*
46 1.5 riastrad * We use struct sg_table just to pass around an array of pages from
47 1.5 riastrad * one device to another in drm prime. Since this is _not_ a complete
48 1.5 riastrad * implementation of Linux's sg table abstraction (e.g., it does not
49 1.5 riastrad * remember DMA addresses and RAM pages separately, and it doesn't
50 1.5 riastrad * support the nested chained iteration of Linux scatterlists), we
51 1.5 riastrad * isolate it to this file and make all callers go through a few extra
52 1.5 riastrad * subroutines (drm_prime_sg_size, drm_prime_sg_free, &c.) to use it.
53 1.5 riastrad * Don't use this outside drm prime!
54 1.4 riastrad */
55 1.4 riastrad
56 1.3 riastrad struct sg_table {
57 1.5 riastrad struct vm_page **sgt_pgs;
58 1.5 riastrad unsigned sgt_npgs;
59 1.3 riastrad };
60 1.3 riastrad
61 1.3 riastrad static int
62 1.3 riastrad sg_alloc_table_from_pages(struct sg_table *sgt, struct page **pages,
63 1.3 riastrad unsigned npages, bus_size_t offset, bus_size_t size, gfp_t gfp)
64 1.3 riastrad {
65 1.3 riastrad unsigned i;
66 1.3 riastrad
67 1.3 riastrad KASSERT(offset == 0);
68 1.3 riastrad KASSERT(size == npages << PAGE_SHIFT);
69 1.3 riastrad
70 1.5 riastrad sgt->sgt_pgs = kcalloc(npages, sizeof(sgt->sgt_pgs[0]), gfp);
71 1.5 riastrad if (sgt->sgt_pgs == NULL)
72 1.3 riastrad return -ENOMEM;
73 1.5 riastrad sgt->sgt_npgs = npages;
74 1.3 riastrad
75 1.5 riastrad for (i = 0; i < npages; i++)
76 1.5 riastrad sgt->sgt_pgs[i] = &pages[i]->p_vmp;
77 1.3 riastrad
78 1.3 riastrad return 0;
79 1.3 riastrad }
80 1.3 riastrad
81 1.3 riastrad static int
82 1.5 riastrad sg_alloc_table_from_pglist(struct sg_table *sgt, const struct pglist *pglist,
83 1.3 riastrad unsigned npages, bus_size_t offset, bus_size_t size, gfp_t gfp)
84 1.3 riastrad {
85 1.3 riastrad struct vm_page *pg;
86 1.3 riastrad unsigned i;
87 1.3 riastrad
88 1.3 riastrad KASSERT(offset == 0);
89 1.3 riastrad KASSERT(size == npages << PAGE_SHIFT);
90 1.3 riastrad
91 1.5 riastrad sgt->sgt_pgs = kcalloc(npages, sizeof(sgt->sgt_pgs[0]), gfp);
92 1.5 riastrad if (sgt->sgt_pgs == NULL)
93 1.3 riastrad return -ENOMEM;
94 1.5 riastrad sgt->sgt_npgs = npages;
95 1.3 riastrad
96 1.3 riastrad i = 0;
97 1.3 riastrad TAILQ_FOREACH(pg, pglist, pageq.queue) {
98 1.3 riastrad KASSERT(i < npages);
99 1.5 riastrad sgt->sgt_pgs[i] = pg;
100 1.3 riastrad }
101 1.3 riastrad KASSERT(i == npages);
102 1.3 riastrad
103 1.3 riastrad return 0;
104 1.3 riastrad }
105 1.3 riastrad
106 1.4 riastrad static int
107 1.5 riastrad sg_alloc_table_from_bus_dmamem(struct sg_table *sgt, bus_dma_tag_t dmat,
108 1.5 riastrad const bus_dma_segment_t *segs, int nsegs, gfp_t gfp)
109 1.4 riastrad {
110 1.5 riastrad int ret;
111 1.4 riastrad
112 1.4 riastrad KASSERT(nsegs > 0);
113 1.5 riastrad sgt->sgt_pgs = kcalloc(nsegs, sizeof(sgt->sgt_pgs[0]), gfp);
114 1.5 riastrad if (sgt->sgt_pgs == NULL)
115 1.4 riastrad return -ENOMEM;
116 1.5 riastrad sgt->sgt_npgs = nsegs;
117 1.4 riastrad
118 1.5 riastrad /* XXX errno NetBSD->Linux */
119 1.5 riastrad ret = -bus_dmamem_export_pages(dmat, segs, nsegs, sgt->sgt_pgs,
120 1.5 riastrad sgt->sgt_npgs);
121 1.5 riastrad if (ret)
122 1.5 riastrad return ret;
123 1.4 riastrad
124 1.4 riastrad return 0;
125 1.4 riastrad }
126 1.4 riastrad
127 1.3 riastrad static void
128 1.3 riastrad sg_free_table(struct sg_table *sgt)
129 1.3 riastrad {
130 1.3 riastrad
131 1.5 riastrad kfree(sgt->sgt_pgs);
132 1.5 riastrad sgt->sgt_pgs = NULL;
133 1.5 riastrad sgt->sgt_npgs = 0;
134 1.3 riastrad }
135 1.3 riastrad
136 1.4 riastrad #endif /* __NetBSD__ */
137 1.4 riastrad
138 1.1 riastrad /*
139 1.1 riastrad * DMA-BUF/GEM Object references and lifetime overview:
140 1.1 riastrad *
141 1.1 riastrad * On the export the dma_buf holds a reference to the exporting GEM
142 1.1 riastrad * object. It takes this reference in handle_to_fd_ioctl, when it
143 1.1 riastrad * first calls .prime_export and stores the exporting GEM object in
144 1.1 riastrad * the dma_buf priv. This reference is released when the dma_buf
145 1.1 riastrad * object goes away in the driver .release function.
146 1.1 riastrad *
147 1.1 riastrad * On the import the importing GEM object holds a reference to the
148 1.1 riastrad * dma_buf (which in turn holds a ref to the exporting GEM object).
149 1.1 riastrad * It takes that reference in the fd_to_handle ioctl.
150 1.1 riastrad * It calls dma_buf_get, creates an attachment to it and stores the
151 1.1 riastrad * attachment in the GEM object. When this attachment is destroyed
152 1.1 riastrad * when the imported object is destroyed, we remove the attachment
153 1.1 riastrad * and drop the reference to the dma_buf.
154 1.1 riastrad *
155 1.1 riastrad * Thus the chain of references always flows in one direction
156 1.1 riastrad * (avoiding loops): importing_gem -> dmabuf -> exporting_gem
157 1.1 riastrad *
158 1.1 riastrad * Self-importing: if userspace is using PRIME as a replacement for flink
159 1.1 riastrad * then it will get a fd->handle request for a GEM object that it created.
160 1.1 riastrad * Drivers should detect this situation and return back the gem object
161 1.2 riastrad * from the dma-buf private. Prime will do this automatically for drivers that
162 1.2 riastrad * use the drm_gem_prime_{import,export} helpers.
163 1.1 riastrad */
164 1.1 riastrad
165 1.1 riastrad struct drm_prime_member {
166 1.1 riastrad struct list_head entry;
167 1.1 riastrad struct dma_buf *dma_buf;
168 1.1 riastrad uint32_t handle;
169 1.1 riastrad };
170 1.1 riastrad
171 1.2 riastrad struct drm_prime_attachment {
172 1.2 riastrad struct sg_table *sgt;
173 1.2 riastrad enum dma_data_direction dir;
174 1.2 riastrad };
175 1.2 riastrad
176 1.2 riastrad static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv,
177 1.2 riastrad struct dma_buf *dma_buf, uint32_t handle)
178 1.2 riastrad {
179 1.2 riastrad struct drm_prime_member *member;
180 1.2 riastrad
181 1.2 riastrad member = kmalloc(sizeof(*member), GFP_KERNEL);
182 1.2 riastrad if (!member)
183 1.2 riastrad return -ENOMEM;
184 1.2 riastrad
185 1.2 riastrad get_dma_buf(dma_buf);
186 1.2 riastrad member->dma_buf = dma_buf;
187 1.2 riastrad member->handle = handle;
188 1.2 riastrad list_add(&member->entry, &prime_fpriv->head);
189 1.2 riastrad return 0;
190 1.2 riastrad }
191 1.2 riastrad
192 1.2 riastrad static struct dma_buf *drm_prime_lookup_buf_by_handle(struct drm_prime_file_private *prime_fpriv,
193 1.2 riastrad uint32_t handle)
194 1.2 riastrad {
195 1.2 riastrad struct drm_prime_member *member;
196 1.2 riastrad
197 1.2 riastrad list_for_each_entry(member, &prime_fpriv->head, entry) {
198 1.2 riastrad if (member->handle == handle)
199 1.2 riastrad return member->dma_buf;
200 1.2 riastrad }
201 1.2 riastrad
202 1.2 riastrad return NULL;
203 1.2 riastrad }
204 1.2 riastrad
205 1.2 riastrad static int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv,
206 1.2 riastrad struct dma_buf *dma_buf,
207 1.2 riastrad uint32_t *handle)
208 1.2 riastrad {
209 1.2 riastrad struct drm_prime_member *member;
210 1.2 riastrad
211 1.2 riastrad list_for_each_entry(member, &prime_fpriv->head, entry) {
212 1.2 riastrad if (member->dma_buf == dma_buf) {
213 1.2 riastrad *handle = member->handle;
214 1.2 riastrad return 0;
215 1.2 riastrad }
216 1.2 riastrad }
217 1.2 riastrad return -ENOENT;
218 1.2 riastrad }
219 1.2 riastrad
220 1.2 riastrad static int drm_gem_map_attach(struct dma_buf *dma_buf,
221 1.2 riastrad struct device *target_dev,
222 1.2 riastrad struct dma_buf_attachment *attach)
223 1.2 riastrad {
224 1.2 riastrad struct drm_prime_attachment *prime_attach;
225 1.2 riastrad struct drm_gem_object *obj = dma_buf->priv;
226 1.2 riastrad struct drm_device *dev = obj->dev;
227 1.2 riastrad
228 1.2 riastrad prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL);
229 1.2 riastrad if (!prime_attach)
230 1.2 riastrad return -ENOMEM;
231 1.2 riastrad
232 1.2 riastrad prime_attach->dir = DMA_NONE;
233 1.2 riastrad attach->priv = prime_attach;
234 1.2 riastrad
235 1.2 riastrad if (!dev->driver->gem_prime_pin)
236 1.2 riastrad return 0;
237 1.2 riastrad
238 1.2 riastrad return dev->driver->gem_prime_pin(obj);
239 1.2 riastrad }
240 1.2 riastrad
241 1.2 riastrad static void drm_gem_map_detach(struct dma_buf *dma_buf,
242 1.2 riastrad struct dma_buf_attachment *attach)
243 1.2 riastrad {
244 1.2 riastrad struct drm_prime_attachment *prime_attach = attach->priv;
245 1.2 riastrad struct drm_gem_object *obj = dma_buf->priv;
246 1.2 riastrad struct drm_device *dev = obj->dev;
247 1.2 riastrad struct sg_table *sgt;
248 1.2 riastrad
249 1.2 riastrad if (dev->driver->gem_prime_unpin)
250 1.2 riastrad dev->driver->gem_prime_unpin(obj);
251 1.2 riastrad
252 1.2 riastrad if (!prime_attach)
253 1.2 riastrad return;
254 1.2 riastrad
255 1.2 riastrad sgt = prime_attach->sgt;
256 1.2 riastrad if (sgt) {
257 1.3 riastrad #ifndef __NetBSD__ /* We map/unmap elsewhere. */
258 1.2 riastrad if (prime_attach->dir != DMA_NONE)
259 1.2 riastrad dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents,
260 1.2 riastrad prime_attach->dir);
261 1.3 riastrad #endif
262 1.2 riastrad sg_free_table(sgt);
263 1.2 riastrad }
264 1.2 riastrad
265 1.2 riastrad kfree(sgt);
266 1.2 riastrad kfree(prime_attach);
267 1.2 riastrad attach->priv = NULL;
268 1.2 riastrad }
269 1.2 riastrad
270 1.2 riastrad void drm_prime_remove_buf_handle_locked(struct drm_prime_file_private *prime_fpriv,
271 1.2 riastrad struct dma_buf *dma_buf)
272 1.2 riastrad {
273 1.2 riastrad struct drm_prime_member *member, *safe;
274 1.2 riastrad
275 1.2 riastrad list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) {
276 1.2 riastrad if (member->dma_buf == dma_buf) {
277 1.2 riastrad dma_buf_put(dma_buf);
278 1.2 riastrad list_del(&member->entry);
279 1.2 riastrad kfree(member);
280 1.2 riastrad }
281 1.2 riastrad }
282 1.2 riastrad }
283 1.2 riastrad
284 1.2 riastrad static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach,
285 1.3 riastrad enum dma_data_direction dir)
286 1.2 riastrad {
287 1.2 riastrad struct drm_prime_attachment *prime_attach = attach->priv;
288 1.2 riastrad struct drm_gem_object *obj = attach->dmabuf->priv;
289 1.2 riastrad struct sg_table *sgt;
290 1.2 riastrad
291 1.2 riastrad if (WARN_ON(dir == DMA_NONE || !prime_attach))
292 1.2 riastrad return ERR_PTR(-EINVAL);
293 1.2 riastrad
294 1.2 riastrad /* return the cached mapping when possible */
295 1.2 riastrad if (prime_attach->dir == dir)
296 1.2 riastrad return prime_attach->sgt;
297 1.2 riastrad
298 1.2 riastrad /*
299 1.2 riastrad * two mappings with different directions for the same attachment are
300 1.2 riastrad * not allowed
301 1.2 riastrad */
302 1.2 riastrad if (WARN_ON(prime_attach->dir != DMA_NONE))
303 1.2 riastrad return ERR_PTR(-EBUSY);
304 1.2 riastrad
305 1.2 riastrad sgt = obj->dev->driver->gem_prime_get_sg_table(obj);
306 1.2 riastrad if (!IS_ERR(sgt)) {
307 1.3 riastrad #ifdef __NetBSD__ /* We map/unmap elsewhere. */
308 1.3 riastrad prime_attach->sgt = sgt;
309 1.3 riastrad prime_attach->dir = dir;
310 1.3 riastrad #else
311 1.2 riastrad if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) {
312 1.2 riastrad sg_free_table(sgt);
313 1.2 riastrad kfree(sgt);
314 1.2 riastrad sgt = ERR_PTR(-ENOMEM);
315 1.2 riastrad } else {
316 1.2 riastrad prime_attach->sgt = sgt;
317 1.2 riastrad prime_attach->dir = dir;
318 1.2 riastrad }
319 1.3 riastrad #endif
320 1.2 riastrad }
321 1.2 riastrad
322 1.2 riastrad return sgt;
323 1.2 riastrad }
324 1.2 riastrad
325 1.2 riastrad static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach,
326 1.2 riastrad struct sg_table *sgt,
327 1.2 riastrad enum dma_data_direction dir)
328 1.2 riastrad {
329 1.2 riastrad /* nothing to be done here */
330 1.2 riastrad }
331 1.2 riastrad
332 1.2 riastrad /**
333 1.2 riastrad * drm_gem_dmabuf_release - dma_buf release implementation for GEM
334 1.2 riastrad * @dma_buf: buffer to be released
335 1.2 riastrad *
336 1.2 riastrad * Generic release function for dma_bufs exported as PRIME buffers. GEM drivers
337 1.2 riastrad * must use this in their dma_buf ops structure as the release callback.
338 1.2 riastrad */
339 1.2 riastrad void drm_gem_dmabuf_release(struct dma_buf *dma_buf)
340 1.2 riastrad {
341 1.2 riastrad struct drm_gem_object *obj = dma_buf->priv;
342 1.2 riastrad
343 1.2 riastrad /* drop the reference on the export fd holds */
344 1.2 riastrad drm_gem_object_unreference_unlocked(obj);
345 1.2 riastrad }
346 1.2 riastrad EXPORT_SYMBOL(drm_gem_dmabuf_release);
347 1.2 riastrad
348 1.2 riastrad static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf)
349 1.2 riastrad {
350 1.2 riastrad struct drm_gem_object *obj = dma_buf->priv;
351 1.2 riastrad struct drm_device *dev = obj->dev;
352 1.2 riastrad
353 1.2 riastrad return dev->driver->gem_prime_vmap(obj);
354 1.2 riastrad }
355 1.2 riastrad
356 1.2 riastrad static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr)
357 1.2 riastrad {
358 1.2 riastrad struct drm_gem_object *obj = dma_buf->priv;
359 1.2 riastrad struct drm_device *dev = obj->dev;
360 1.2 riastrad
361 1.2 riastrad dev->driver->gem_prime_vunmap(obj, vaddr);
362 1.2 riastrad }
363 1.2 riastrad
364 1.2 riastrad static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf,
365 1.2 riastrad unsigned long page_num)
366 1.2 riastrad {
367 1.2 riastrad return NULL;
368 1.2 riastrad }
369 1.2 riastrad
370 1.2 riastrad static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf,
371 1.2 riastrad unsigned long page_num, void *addr)
372 1.2 riastrad {
373 1.2 riastrad
374 1.2 riastrad }
375 1.2 riastrad static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf,
376 1.2 riastrad unsigned long page_num)
377 1.2 riastrad {
378 1.2 riastrad return NULL;
379 1.2 riastrad }
380 1.2 riastrad
381 1.2 riastrad static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf,
382 1.2 riastrad unsigned long page_num, void *addr)
383 1.2 riastrad {
384 1.2 riastrad
385 1.2 riastrad }
386 1.2 riastrad
387 1.3 riastrad #ifdef __NetBSD__
388 1.3 riastrad static int
389 1.3 riastrad drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, off_t *offp, size_t size,
390 1.3 riastrad int prot, int *flagsp, int *advicep, struct uvm_object **uobjp,
391 1.3 riastrad int *maxprotp)
392 1.3 riastrad #else
393 1.2 riastrad static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf,
394 1.2 riastrad struct vm_area_struct *vma)
395 1.3 riastrad #endif
396 1.2 riastrad {
397 1.2 riastrad struct drm_gem_object *obj = dma_buf->priv;
398 1.2 riastrad struct drm_device *dev = obj->dev;
399 1.2 riastrad
400 1.2 riastrad if (!dev->driver->gem_prime_mmap)
401 1.2 riastrad return -ENOSYS;
402 1.2 riastrad
403 1.3 riastrad #ifdef __NetBSD__
404 1.3 riastrad return dev->driver->gem_prime_mmap(obj, offp, size, prot, flagsp,
405 1.3 riastrad advicep, uobjp, maxprotp);
406 1.3 riastrad #else
407 1.2 riastrad return dev->driver->gem_prime_mmap(obj, vma);
408 1.3 riastrad #endif
409 1.2 riastrad }
410 1.2 riastrad
411 1.2 riastrad static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = {
412 1.2 riastrad .attach = drm_gem_map_attach,
413 1.2 riastrad .detach = drm_gem_map_detach,
414 1.2 riastrad .map_dma_buf = drm_gem_map_dma_buf,
415 1.2 riastrad .unmap_dma_buf = drm_gem_unmap_dma_buf,
416 1.2 riastrad .release = drm_gem_dmabuf_release,
417 1.2 riastrad .kmap = drm_gem_dmabuf_kmap,
418 1.2 riastrad .kmap_atomic = drm_gem_dmabuf_kmap_atomic,
419 1.2 riastrad .kunmap = drm_gem_dmabuf_kunmap,
420 1.2 riastrad .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic,
421 1.2 riastrad .mmap = drm_gem_dmabuf_mmap,
422 1.2 riastrad .vmap = drm_gem_dmabuf_vmap,
423 1.2 riastrad .vunmap = drm_gem_dmabuf_vunmap,
424 1.2 riastrad };
425 1.2 riastrad
426 1.2 riastrad /**
427 1.2 riastrad * DOC: PRIME Helpers
428 1.2 riastrad *
429 1.2 riastrad * Drivers can implement @gem_prime_export and @gem_prime_import in terms of
430 1.2 riastrad * simpler APIs by using the helper functions @drm_gem_prime_export and
431 1.2 riastrad * @drm_gem_prime_import. These functions implement dma-buf support in terms of
432 1.2 riastrad * six lower-level driver callbacks:
433 1.2 riastrad *
434 1.2 riastrad * Export callbacks:
435 1.2 riastrad *
436 1.2 riastrad * - @gem_prime_pin (optional): prepare a GEM object for exporting
437 1.2 riastrad *
438 1.2 riastrad * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages
439 1.2 riastrad *
440 1.2 riastrad * - @gem_prime_vmap: vmap a buffer exported by your driver
441 1.2 riastrad *
442 1.2 riastrad * - @gem_prime_vunmap: vunmap a buffer exported by your driver
443 1.2 riastrad *
444 1.2 riastrad * - @gem_prime_mmap (optional): mmap a buffer exported by your driver
445 1.2 riastrad *
446 1.2 riastrad * Import callback:
447 1.2 riastrad *
448 1.2 riastrad * - @gem_prime_import_sg_table (import): produce a GEM object from another
449 1.2 riastrad * driver's scatter/gather table
450 1.2 riastrad */
451 1.2 riastrad
452 1.2 riastrad /**
453 1.2 riastrad * drm_gem_prime_export - helper library implementation of the export callback
454 1.2 riastrad * @dev: drm_device to export from
455 1.2 riastrad * @obj: GEM object to export
456 1.2 riastrad * @flags: flags like DRM_CLOEXEC
457 1.2 riastrad *
458 1.2 riastrad * This is the implementation of the gem_prime_export functions for GEM drivers
459 1.2 riastrad * using the PRIME helpers.
460 1.2 riastrad */
461 1.2 riastrad struct dma_buf *drm_gem_prime_export(struct drm_device *dev,
462 1.2 riastrad struct drm_gem_object *obj,
463 1.2 riastrad int flags)
464 1.2 riastrad {
465 1.2 riastrad struct dma_buf_export_info exp_info = {
466 1.3 riastrad #ifndef __NetBSD__
467 1.2 riastrad .exp_name = KBUILD_MODNAME, /* white lie for debug */
468 1.2 riastrad .owner = dev->driver->fops->owner,
469 1.3 riastrad #endif
470 1.2 riastrad .ops = &drm_gem_prime_dmabuf_ops,
471 1.2 riastrad .size = obj->size,
472 1.2 riastrad .flags = flags,
473 1.2 riastrad .priv = obj,
474 1.2 riastrad };
475 1.2 riastrad
476 1.2 riastrad if (dev->driver->gem_prime_res_obj)
477 1.2 riastrad exp_info.resv = dev->driver->gem_prime_res_obj(obj);
478 1.2 riastrad
479 1.2 riastrad return dma_buf_export(&exp_info);
480 1.2 riastrad }
481 1.2 riastrad EXPORT_SYMBOL(drm_gem_prime_export);
482 1.2 riastrad
483 1.2 riastrad static struct dma_buf *export_and_register_object(struct drm_device *dev,
484 1.2 riastrad struct drm_gem_object *obj,
485 1.2 riastrad uint32_t flags)
486 1.2 riastrad {
487 1.2 riastrad struct dma_buf *dmabuf;
488 1.2 riastrad
489 1.2 riastrad /* prevent races with concurrent gem_close. */
490 1.2 riastrad if (obj->handle_count == 0) {
491 1.2 riastrad dmabuf = ERR_PTR(-ENOENT);
492 1.2 riastrad return dmabuf;
493 1.2 riastrad }
494 1.2 riastrad
495 1.2 riastrad dmabuf = dev->driver->gem_prime_export(dev, obj, flags);
496 1.2 riastrad if (IS_ERR(dmabuf)) {
497 1.2 riastrad /* normally the created dma-buf takes ownership of the ref,
498 1.2 riastrad * but if that fails then drop the ref
499 1.2 riastrad */
500 1.2 riastrad return dmabuf;
501 1.2 riastrad }
502 1.2 riastrad
503 1.2 riastrad /*
504 1.2 riastrad * Note that callers do not need to clean up the export cache
505 1.2 riastrad * since the check for obj->handle_count guarantees that someone
506 1.2 riastrad * will clean it up.
507 1.2 riastrad */
508 1.2 riastrad obj->dma_buf = dmabuf;
509 1.2 riastrad get_dma_buf(obj->dma_buf);
510 1.2 riastrad /* Grab a new ref since the callers is now used by the dma-buf */
511 1.2 riastrad drm_gem_object_reference(obj);
512 1.2 riastrad
513 1.2 riastrad return dmabuf;
514 1.2 riastrad }
515 1.2 riastrad
516 1.2 riastrad /**
517 1.2 riastrad * drm_gem_prime_handle_to_fd - PRIME export function for GEM drivers
518 1.2 riastrad * @dev: dev to export the buffer from
519 1.2 riastrad * @file_priv: drm file-private structure
520 1.2 riastrad * @handle: buffer handle to export
521 1.2 riastrad * @flags: flags like DRM_CLOEXEC
522 1.2 riastrad * @prime_fd: pointer to storage for the fd id of the create dma-buf
523 1.2 riastrad *
524 1.2 riastrad * This is the PRIME export function which must be used mandatorily by GEM
525 1.2 riastrad * drivers to ensure correct lifetime management of the underlying GEM object.
526 1.2 riastrad * The actual exporting from GEM object to a dma-buf is done through the
527 1.2 riastrad * gem_prime_export driver callback.
528 1.2 riastrad */
529 1.1 riastrad int drm_gem_prime_handle_to_fd(struct drm_device *dev,
530 1.2 riastrad struct drm_file *file_priv, uint32_t handle,
531 1.2 riastrad uint32_t flags,
532 1.2 riastrad int *prime_fd)
533 1.1 riastrad {
534 1.1 riastrad struct drm_gem_object *obj;
535 1.2 riastrad int ret = 0;
536 1.2 riastrad struct dma_buf *dmabuf;
537 1.1 riastrad
538 1.2 riastrad mutex_lock(&file_priv->prime.lock);
539 1.1 riastrad obj = drm_gem_object_lookup(dev, file_priv, handle);
540 1.2 riastrad if (!obj) {
541 1.2 riastrad ret = -ENOENT;
542 1.2 riastrad goto out_unlock;
543 1.2 riastrad }
544 1.2 riastrad
545 1.2 riastrad dmabuf = drm_prime_lookup_buf_by_handle(&file_priv->prime, handle);
546 1.2 riastrad if (dmabuf) {
547 1.2 riastrad get_dma_buf(dmabuf);
548 1.2 riastrad goto out_have_handle;
549 1.2 riastrad }
550 1.1 riastrad
551 1.2 riastrad mutex_lock(&dev->object_name_lock);
552 1.1 riastrad /* re-export the original imported object */
553 1.1 riastrad if (obj->import_attach) {
554 1.2 riastrad dmabuf = obj->import_attach->dmabuf;
555 1.2 riastrad get_dma_buf(dmabuf);
556 1.2 riastrad goto out_have_obj;
557 1.2 riastrad }
558 1.2 riastrad
559 1.2 riastrad if (obj->dma_buf) {
560 1.2 riastrad get_dma_buf(obj->dma_buf);
561 1.2 riastrad dmabuf = obj->dma_buf;
562 1.2 riastrad goto out_have_obj;
563 1.2 riastrad }
564 1.2 riastrad
565 1.2 riastrad dmabuf = export_and_register_object(dev, obj, flags);
566 1.2 riastrad if (IS_ERR(dmabuf)) {
567 1.2 riastrad /* normally the created dma-buf takes ownership of the ref,
568 1.2 riastrad * but if that fails then drop the ref
569 1.2 riastrad */
570 1.2 riastrad ret = PTR_ERR(dmabuf);
571 1.2 riastrad mutex_unlock(&dev->object_name_lock);
572 1.2 riastrad goto out;
573 1.1 riastrad }
574 1.1 riastrad
575 1.2 riastrad out_have_obj:
576 1.2 riastrad /*
577 1.2 riastrad * If we've exported this buffer then cheat and add it to the import list
578 1.2 riastrad * so we get the correct handle back. We must do this under the
579 1.2 riastrad * protection of dev->object_name_lock to ensure that a racing gem close
580 1.2 riastrad * ioctl doesn't miss to remove this buffer handle from the cache.
581 1.2 riastrad */
582 1.2 riastrad ret = drm_prime_add_buf_handle(&file_priv->prime,
583 1.2 riastrad dmabuf, handle);
584 1.2 riastrad mutex_unlock(&dev->object_name_lock);
585 1.2 riastrad if (ret)
586 1.2 riastrad goto fail_put_dmabuf;
587 1.2 riastrad
588 1.2 riastrad out_have_handle:
589 1.2 riastrad ret = dma_buf_fd(dmabuf, flags);
590 1.2 riastrad /*
591 1.2 riastrad * We must _not_ remove the buffer from the handle cache since the newly
592 1.2 riastrad * created dma buf is already linked in the global obj->dma_buf pointer,
593 1.2 riastrad * and that is invariant as long as a userspace gem handle exists.
594 1.2 riastrad * Closing the handle will clean out the cache anyway, so we don't leak.
595 1.2 riastrad */
596 1.2 riastrad if (ret < 0) {
597 1.2 riastrad goto fail_put_dmabuf;
598 1.1 riastrad } else {
599 1.2 riastrad *prime_fd = ret;
600 1.2 riastrad ret = 0;
601 1.2 riastrad }
602 1.2 riastrad
603 1.2 riastrad goto out;
604 1.2 riastrad
605 1.2 riastrad fail_put_dmabuf:
606 1.2 riastrad dma_buf_put(dmabuf);
607 1.2 riastrad out:
608 1.2 riastrad drm_gem_object_unreference_unlocked(obj);
609 1.2 riastrad out_unlock:
610 1.2 riastrad mutex_unlock(&file_priv->prime.lock);
611 1.2 riastrad
612 1.2 riastrad return ret;
613 1.2 riastrad }
614 1.2 riastrad EXPORT_SYMBOL(drm_gem_prime_handle_to_fd);
615 1.2 riastrad
616 1.2 riastrad /**
617 1.2 riastrad * drm_gem_prime_import - helper library implementation of the import callback
618 1.2 riastrad * @dev: drm_device to import into
619 1.2 riastrad * @dma_buf: dma-buf object to import
620 1.2 riastrad *
621 1.2 riastrad * This is the implementation of the gem_prime_import functions for GEM drivers
622 1.2 riastrad * using the PRIME helpers.
623 1.2 riastrad */
624 1.2 riastrad struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev,
625 1.2 riastrad struct dma_buf *dma_buf)
626 1.2 riastrad {
627 1.2 riastrad struct dma_buf_attachment *attach;
628 1.2 riastrad struct sg_table *sgt;
629 1.2 riastrad struct drm_gem_object *obj;
630 1.2 riastrad int ret;
631 1.2 riastrad
632 1.2 riastrad if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) {
633 1.2 riastrad obj = dma_buf->priv;
634 1.2 riastrad if (obj->dev == dev) {
635 1.2 riastrad /*
636 1.2 riastrad * Importing dmabuf exported from out own gem increases
637 1.2 riastrad * refcount on gem itself instead of f_count of dmabuf.
638 1.1 riastrad */
639 1.2 riastrad drm_gem_object_reference(obj);
640 1.2 riastrad return obj;
641 1.1 riastrad }
642 1.1 riastrad }
643 1.2 riastrad
644 1.2 riastrad if (!dev->driver->gem_prime_import_sg_table)
645 1.2 riastrad return ERR_PTR(-EINVAL);
646 1.2 riastrad
647 1.2 riastrad attach = dma_buf_attach(dma_buf, dev->dev);
648 1.2 riastrad if (IS_ERR(attach))
649 1.2 riastrad return ERR_CAST(attach);
650 1.2 riastrad
651 1.2 riastrad get_dma_buf(dma_buf);
652 1.2 riastrad
653 1.2 riastrad sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL);
654 1.2 riastrad if (IS_ERR(sgt)) {
655 1.2 riastrad ret = PTR_ERR(sgt);
656 1.2 riastrad goto fail_detach;
657 1.2 riastrad }
658 1.2 riastrad
659 1.2 riastrad obj = dev->driver->gem_prime_import_sg_table(dev, attach, sgt);
660 1.2 riastrad if (IS_ERR(obj)) {
661 1.2 riastrad ret = PTR_ERR(obj);
662 1.2 riastrad goto fail_unmap;
663 1.1 riastrad }
664 1.1 riastrad
665 1.2 riastrad obj->import_attach = attach;
666 1.2 riastrad
667 1.2 riastrad return obj;
668 1.2 riastrad
669 1.2 riastrad fail_unmap:
670 1.2 riastrad dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL);
671 1.2 riastrad fail_detach:
672 1.2 riastrad dma_buf_detach(dma_buf, attach);
673 1.2 riastrad dma_buf_put(dma_buf);
674 1.2 riastrad
675 1.2 riastrad return ERR_PTR(ret);
676 1.1 riastrad }
677 1.2 riastrad EXPORT_SYMBOL(drm_gem_prime_import);
678 1.1 riastrad
679 1.2 riastrad /**
680 1.2 riastrad * drm_gem_prime_fd_to_handle - PRIME import function for GEM drivers
681 1.2 riastrad * @dev: dev to export the buffer from
682 1.2 riastrad * @file_priv: drm file-private structure
683 1.2 riastrad * @prime_fd: fd id of the dma-buf which should be imported
684 1.2 riastrad * @handle: pointer to storage for the handle of the imported buffer object
685 1.2 riastrad *
686 1.2 riastrad * This is the PRIME import function which must be used mandatorily by GEM
687 1.2 riastrad * drivers to ensure correct lifetime management of the underlying GEM object.
688 1.2 riastrad * The actual importing of GEM object from the dma-buf is done through the
689 1.2 riastrad * gem_import_export driver callback.
690 1.2 riastrad */
691 1.1 riastrad int drm_gem_prime_fd_to_handle(struct drm_device *dev,
692 1.2 riastrad struct drm_file *file_priv, int prime_fd,
693 1.2 riastrad uint32_t *handle)
694 1.1 riastrad {
695 1.1 riastrad struct dma_buf *dma_buf;
696 1.1 riastrad struct drm_gem_object *obj;
697 1.1 riastrad int ret;
698 1.1 riastrad
699 1.1 riastrad dma_buf = dma_buf_get(prime_fd);
700 1.1 riastrad if (IS_ERR(dma_buf))
701 1.1 riastrad return PTR_ERR(dma_buf);
702 1.1 riastrad
703 1.1 riastrad mutex_lock(&file_priv->prime.lock);
704 1.1 riastrad
705 1.2 riastrad ret = drm_prime_lookup_buf_handle(&file_priv->prime,
706 1.1 riastrad dma_buf, handle);
707 1.2 riastrad if (ret == 0)
708 1.1 riastrad goto out_put;
709 1.1 riastrad
710 1.1 riastrad /* never seen this one, need to import */
711 1.2 riastrad mutex_lock(&dev->object_name_lock);
712 1.1 riastrad obj = dev->driver->gem_prime_import(dev, dma_buf);
713 1.1 riastrad if (IS_ERR(obj)) {
714 1.1 riastrad ret = PTR_ERR(obj);
715 1.2 riastrad goto out_unlock;
716 1.2 riastrad }
717 1.2 riastrad
718 1.2 riastrad if (obj->dma_buf) {
719 1.2 riastrad WARN_ON(obj->dma_buf != dma_buf);
720 1.2 riastrad } else {
721 1.2 riastrad obj->dma_buf = dma_buf;
722 1.2 riastrad get_dma_buf(dma_buf);
723 1.1 riastrad }
724 1.1 riastrad
725 1.2 riastrad /* drm_gem_handle_create_tail unlocks dev->object_name_lock. */
726 1.2 riastrad ret = drm_gem_handle_create_tail(file_priv, obj, handle);
727 1.1 riastrad drm_gem_object_unreference_unlocked(obj);
728 1.1 riastrad if (ret)
729 1.1 riastrad goto out_put;
730 1.1 riastrad
731 1.2 riastrad ret = drm_prime_add_buf_handle(&file_priv->prime,
732 1.1 riastrad dma_buf, *handle);
733 1.1 riastrad if (ret)
734 1.1 riastrad goto fail;
735 1.1 riastrad
736 1.1 riastrad mutex_unlock(&file_priv->prime.lock);
737 1.2 riastrad
738 1.2 riastrad dma_buf_put(dma_buf);
739 1.2 riastrad
740 1.1 riastrad return 0;
741 1.1 riastrad
742 1.1 riastrad fail:
743 1.1 riastrad /* hmm, if driver attached, we are relying on the free-object path
744 1.1 riastrad * to detach.. which seems ok..
745 1.1 riastrad */
746 1.2 riastrad drm_gem_handle_delete(file_priv, *handle);
747 1.2 riastrad out_unlock:
748 1.2 riastrad mutex_unlock(&dev->object_name_lock);
749 1.1 riastrad out_put:
750 1.1 riastrad dma_buf_put(dma_buf);
751 1.1 riastrad mutex_unlock(&file_priv->prime.lock);
752 1.1 riastrad return ret;
753 1.1 riastrad }
754 1.1 riastrad EXPORT_SYMBOL(drm_gem_prime_fd_to_handle);
755 1.1 riastrad
756 1.1 riastrad int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data,
757 1.1 riastrad struct drm_file *file_priv)
758 1.1 riastrad {
759 1.1 riastrad struct drm_prime_handle *args = data;
760 1.1 riastrad uint32_t flags;
761 1.1 riastrad
762 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_PRIME))
763 1.1 riastrad return -EINVAL;
764 1.1 riastrad
765 1.1 riastrad if (!dev->driver->prime_handle_to_fd)
766 1.1 riastrad return -ENOSYS;
767 1.1 riastrad
768 1.1 riastrad /* check flags are valid */
769 1.1 riastrad if (args->flags & ~DRM_CLOEXEC)
770 1.1 riastrad return -EINVAL;
771 1.1 riastrad
772 1.1 riastrad /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
773 1.1 riastrad flags = args->flags & DRM_CLOEXEC;
774 1.1 riastrad
775 1.1 riastrad return dev->driver->prime_handle_to_fd(dev, file_priv,
776 1.1 riastrad args->handle, flags, &args->fd);
777 1.1 riastrad }
778 1.1 riastrad
779 1.1 riastrad int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
780 1.1 riastrad struct drm_file *file_priv)
781 1.1 riastrad {
782 1.1 riastrad struct drm_prime_handle *args = data;
783 1.1 riastrad
784 1.1 riastrad if (!drm_core_check_feature(dev, DRIVER_PRIME))
785 1.1 riastrad return -EINVAL;
786 1.1 riastrad
787 1.1 riastrad if (!dev->driver->prime_fd_to_handle)
788 1.1 riastrad return -ENOSYS;
789 1.1 riastrad
790 1.1 riastrad return dev->driver->prime_fd_to_handle(dev, file_priv,
791 1.1 riastrad args->fd, &args->handle);
792 1.1 riastrad }
793 1.1 riastrad
794 1.2 riastrad /**
795 1.2 riastrad * drm_prime_pages_to_sg - converts a page array into an sg list
796 1.2 riastrad * @pages: pointer to the array of page pointers to convert
797 1.2 riastrad * @nr_pages: length of the page vector
798 1.1 riastrad *
799 1.2 riastrad * This helper creates an sg table object from a set of pages
800 1.1 riastrad * the driver is responsible for mapping the pages into the
801 1.2 riastrad * importers address space for use with dma_buf itself.
802 1.1 riastrad */
803 1.2 riastrad struct sg_table *drm_prime_pages_to_sg(struct page **pages, unsigned int nr_pages)
804 1.1 riastrad {
805 1.1 riastrad struct sg_table *sg = NULL;
806 1.1 riastrad int ret;
807 1.1 riastrad
808 1.1 riastrad sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
809 1.2 riastrad if (!sg) {
810 1.2 riastrad ret = -ENOMEM;
811 1.1 riastrad goto out;
812 1.2 riastrad }
813 1.1 riastrad
814 1.2 riastrad ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0,
815 1.2 riastrad nr_pages << PAGE_SHIFT, GFP_KERNEL);
816 1.1 riastrad if (ret)
817 1.1 riastrad goto out;
818 1.1 riastrad
819 1.1 riastrad return sg;
820 1.1 riastrad out:
821 1.1 riastrad kfree(sg);
822 1.2 riastrad return ERR_PTR(ret);
823 1.1 riastrad }
824 1.1 riastrad EXPORT_SYMBOL(drm_prime_pages_to_sg);
825 1.1 riastrad
826 1.3 riastrad #ifdef __NetBSD__
827 1.3 riastrad
828 1.3 riastrad struct sg_table *
829 1.5 riastrad drm_prime_bus_dmamem_to_sg(bus_dma_tag_t dmat, const bus_dma_segment_t *segs,
830 1.5 riastrad int nsegs)
831 1.4 riastrad {
832 1.4 riastrad struct sg_table *sg;
833 1.4 riastrad int ret;
834 1.4 riastrad
835 1.4 riastrad sg = kmalloc(sizeof(*sg), GFP_KERNEL);
836 1.4 riastrad if (sg == NULL) {
837 1.4 riastrad ret = -ENOMEM;
838 1.4 riastrad goto out;
839 1.4 riastrad }
840 1.4 riastrad
841 1.5 riastrad ret = sg_alloc_table_from_bus_dmamem(sg, dmat, segs, nsegs,
842 1.5 riastrad GFP_KERNEL);
843 1.4 riastrad if (ret)
844 1.4 riastrad goto out;
845 1.4 riastrad
846 1.4 riastrad return sg;
847 1.4 riastrad out:
848 1.4 riastrad kfree(sg);
849 1.4 riastrad return ERR_PTR(ret);
850 1.4 riastrad }
851 1.4 riastrad
852 1.4 riastrad struct sg_table *
853 1.3 riastrad drm_prime_pglist_to_sg(struct pglist *pglist, unsigned npages)
854 1.3 riastrad {
855 1.3 riastrad struct sg_table *sg;
856 1.3 riastrad int ret;
857 1.3 riastrad
858 1.3 riastrad sg = kmalloc(sizeof(*sg), GFP_KERNEL);
859 1.3 riastrad if (sg == NULL) {
860 1.3 riastrad ret = -ENOMEM;
861 1.3 riastrad goto out;
862 1.3 riastrad }
863 1.3 riastrad
864 1.3 riastrad ret = sg_alloc_table_from_pglist(sg, pglist, 0, npages << PAGE_SHIFT,
865 1.3 riastrad npages, GFP_KERNEL);
866 1.3 riastrad if (ret)
867 1.3 riastrad goto out;
868 1.3 riastrad
869 1.3 riastrad return sg;
870 1.3 riastrad
871 1.3 riastrad out:
872 1.3 riastrad kfree(sg);
873 1.3 riastrad return ERR_PTR(ret);
874 1.3 riastrad }
875 1.3 riastrad
876 1.4 riastrad bus_size_t
877 1.4 riastrad drm_prime_sg_size(struct sg_table *sg)
878 1.4 riastrad {
879 1.4 riastrad
880 1.5 riastrad return sg->sgt_npgs << PAGE_SHIFT;
881 1.4 riastrad }
882 1.4 riastrad
883 1.3 riastrad void
884 1.3 riastrad drm_prime_sg_free(struct sg_table *sg)
885 1.3 riastrad {
886 1.3 riastrad
887 1.3 riastrad sg_free_table(sg);
888 1.3 riastrad kfree(sg);
889 1.3 riastrad }
890 1.3 riastrad
891 1.3 riastrad int
892 1.5 riastrad drm_prime_sg_to_bus_dmamem(bus_dma_tag_t dmat, bus_dma_segment_t *segs,
893 1.5 riastrad int nsegs, int *rsegs, const struct sg_table *sgt)
894 1.3 riastrad {
895 1.3 riastrad
896 1.3 riastrad /* XXX errno NetBSD->Linux */
897 1.5 riastrad return -bus_dmamem_import_pages(dmat, segs, nsegs, rsegs, sgt->sgt_pgs,
898 1.5 riastrad sgt->sgt_npgs);
899 1.3 riastrad }
900 1.3 riastrad
901 1.4 riastrad int
902 1.5 riastrad drm_prime_bus_dmamap_load_sgt(bus_dma_tag_t dmat, bus_dmamap_t map,
903 1.5 riastrad struct sg_table *sgt)
904 1.4 riastrad {
905 1.5 riastrad bus_dma_segment_t *segs;
906 1.5 riastrad bus_size_t size = drm_prime_sg_size(sgt);
907 1.5 riastrad int nsegs = sgt->sgt_npgs;
908 1.5 riastrad int ret;
909 1.5 riastrad
910 1.5 riastrad segs = kcalloc(sgt->sgt_npgs, sizeof(segs[0]), GFP_KERNEL);
911 1.5 riastrad if (segs == NULL) {
912 1.5 riastrad ret = -ENOMEM;
913 1.5 riastrad goto out0;
914 1.5 riastrad }
915 1.5 riastrad
916 1.5 riastrad ret = drm_prime_sg_to_bus_dmamem(dmat, segs, nsegs, &nsegs, sgt);
917 1.5 riastrad if (ret)
918 1.5 riastrad goto out1;
919 1.5 riastrad KASSERT(nsegs <= sgt->sgt_npgs);
920 1.5 riastrad
921 1.5 riastrad /* XXX errno NetBSD->Linux */
922 1.5 riastrad ret = -bus_dmamap_load_raw(dmat, map, segs, nsegs, size,
923 1.5 riastrad BUS_DMA_NOWAIT);
924 1.5 riastrad if (ret)
925 1.5 riastrad goto out1;
926 1.4 riastrad
927 1.5 riastrad out1: kfree(segs);
928 1.5 riastrad out0: return ret;
929 1.4 riastrad }
930 1.4 riastrad
931 1.3 riastrad #else /* !__NetBSD__ */
932 1.3 riastrad
933 1.2 riastrad /**
934 1.2 riastrad * drm_prime_sg_to_page_addr_arrays - convert an sg table into a page array
935 1.2 riastrad * @sgt: scatter-gather table to convert
936 1.2 riastrad * @pages: array of page pointers to store the page array in
937 1.2 riastrad * @addrs: optional array to store the dma bus address of each page
938 1.2 riastrad * @max_pages: size of both the passed-in arrays
939 1.2 riastrad *
940 1.2 riastrad * Exports an sg table into an array of pages and addresses. This is currently
941 1.2 riastrad * required by the TTM driver in order to do correct fault handling.
942 1.2 riastrad */
943 1.1 riastrad int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages,
944 1.1 riastrad dma_addr_t *addrs, int max_pages)
945 1.1 riastrad {
946 1.1 riastrad unsigned count;
947 1.1 riastrad struct scatterlist *sg;
948 1.1 riastrad struct page *page;
949 1.2 riastrad u32 len;
950 1.1 riastrad int pg_index;
951 1.1 riastrad dma_addr_t addr;
952 1.1 riastrad
953 1.1 riastrad pg_index = 0;
954 1.1 riastrad for_each_sg(sgt->sgl, sg, sgt->nents, count) {
955 1.1 riastrad len = sg->length;
956 1.1 riastrad page = sg_page(sg);
957 1.1 riastrad addr = sg_dma_address(sg);
958 1.1 riastrad
959 1.1 riastrad while (len > 0) {
960 1.1 riastrad if (WARN_ON(pg_index >= max_pages))
961 1.1 riastrad return -1;
962 1.1 riastrad pages[pg_index] = page;
963 1.1 riastrad if (addrs)
964 1.1 riastrad addrs[pg_index] = addr;
965 1.1 riastrad
966 1.1 riastrad page++;
967 1.1 riastrad addr += PAGE_SIZE;
968 1.1 riastrad len -= PAGE_SIZE;
969 1.1 riastrad pg_index++;
970 1.1 riastrad }
971 1.1 riastrad }
972 1.1 riastrad return 0;
973 1.1 riastrad }
974 1.1 riastrad EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays);
975 1.2 riastrad
976 1.3 riastrad #endif /* __NetBSD__ */
977 1.3 riastrad
978 1.2 riastrad /**
979 1.2 riastrad * drm_prime_gem_destroy - helper to clean up a PRIME-imported GEM object
980 1.2 riastrad * @obj: GEM object which was created from a dma-buf
981 1.2 riastrad * @sg: the sg-table which was pinned at import time
982 1.2 riastrad *
983 1.2 riastrad * This is the cleanup functions which GEM drivers need to call when they use
984 1.2 riastrad * @drm_gem_prime_import to import dma-bufs.
985 1.2 riastrad */
986 1.1 riastrad void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg)
987 1.1 riastrad {
988 1.1 riastrad struct dma_buf_attachment *attach;
989 1.1 riastrad struct dma_buf *dma_buf;
990 1.1 riastrad attach = obj->import_attach;
991 1.1 riastrad if (sg)
992 1.1 riastrad dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL);
993 1.1 riastrad dma_buf = attach->dmabuf;
994 1.1 riastrad dma_buf_detach(attach->dmabuf, attach);
995 1.1 riastrad /* remove the reference */
996 1.1 riastrad dma_buf_put(dma_buf);
997 1.1 riastrad }
998 1.1 riastrad EXPORT_SYMBOL(drm_prime_gem_destroy);
999 1.1 riastrad
1000 1.1 riastrad void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv)
1001 1.1 riastrad {
1002 1.1 riastrad INIT_LIST_HEAD(&prime_fpriv->head);
1003 1.3 riastrad #ifdef __NetBSD__
1004 1.3 riastrad linux_mutex_init(&prime_fpriv->lock);
1005 1.3 riastrad #else
1006 1.1 riastrad mutex_init(&prime_fpriv->lock);
1007 1.3 riastrad #endif
1008 1.1 riastrad }
1009 1.1 riastrad
1010 1.1 riastrad void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv)
1011 1.1 riastrad {
1012 1.2 riastrad /* by now drm_gem_release should've made sure the list is empty */
1013 1.2 riastrad WARN_ON(!list_empty(&prime_fpriv->head));
1014 1.3 riastrad #ifdef __NetBSD__
1015 1.3 riastrad linux_mutex_destroy(&prime_fpriv->lock);
1016 1.3 riastrad #else
1017 1.3 riastrad mutex_destroy(&prime_fpriv->lock);
1018 1.3 riastrad #endif
1019 1.1 riastrad }
1020