]>
Commit | Line | Data |
---|---|---|
3248877e DA |
1 | /* |
2 | * Copyright © 2012 Red Hat | |
3 | * | |
4 | * Permission is hereby granted, free of charge, to any person obtaining a | |
5 | * copy of this software and associated documentation files (the "Software"), | |
6 | * to deal in the Software without restriction, including without limitation | |
7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, | |
8 | * and/or sell copies of the Software, and to permit persons to whom the | |
9 | * Software is furnished to do so, subject to the following conditions: | |
10 | * | |
11 | * The above copyright notice and this permission notice (including the next | |
12 | * paragraph) shall be included in all copies or substantial portions of the | |
13 | * Software. | |
14 | * | |
15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
21 | * IN THE SOFTWARE. | |
22 | * | |
23 | * Authors: | |
24 | * Dave Airlie <airlied@redhat.com> | |
25 | * Rob Clark <rob.clark@linaro.org> | |
26 | * | |
27 | */ | |
28 | ||
29 | #include <linux/export.h> | |
30 | #include <linux/dma-buf.h> | |
760285e7 | 31 | #include <drm/drmP.h> |
3248877e DA |
32 | |
33 | /* | |
34 | * DMA-BUF/GEM Object references and lifetime overview: | |
35 | * | |
36 | * On the export the dma_buf holds a reference to the exporting GEM | |
37 | * object. It takes this reference in handle_to_fd_ioctl, when it | |
38 | * first calls .prime_export and stores the exporting GEM object in | |
39 | * the dma_buf priv. This reference is released when the dma_buf | |
40 | * object goes away in the driver .release function. | |
41 | * | |
42 | * On the import the importing GEM object holds a reference to the | |
43 | * dma_buf (which in turn holds a ref to the exporting GEM object). | |
44 | * It takes that reference in the fd_to_handle ioctl. | |
45 | * It calls dma_buf_get, creates an attachment to it and stores the | |
46 | * attachment in the GEM object. When this attachment is destroyed | |
47 | * when the imported object is destroyed, we remove the attachment | |
48 | * and drop the reference to the dma_buf. | |
49 | * | |
50 | * Thus the chain of references always flows in one direction | |
51 | * (avoiding loops): importing_gem -> dmabuf -> exporting_gem | |
52 | * | |
53 | * Self-importing: if userspace is using PRIME as a replacement for flink | |
54 | * then it will get a fd->handle request for a GEM object that it created. | |
55 | * Drivers should detect this situation and return back the gem object | |
89177644 AP |
56 | * from the dma-buf private. Prime will do this automatically for drivers that |
57 | * use the drm_gem_prime_{import,export} helpers. | |
3248877e DA |
58 | */ |
59 | ||
60 | struct drm_prime_member { | |
61 | struct list_head entry; | |
62 | struct dma_buf *dma_buf; | |
63 | uint32_t handle; | |
64 | }; | |
538d6661 JS |
65 | |
66 | struct drm_prime_attachment { | |
67 | struct sg_table *sgt; | |
68 | enum dma_data_direction dir; | |
69 | }; | |
70 | ||
ce92e3c9 SWK |
71 | static int drm_prime_add_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t handle) |
72 | { | |
73 | struct drm_prime_member *member; | |
74 | ||
75 | member = kmalloc(sizeof(*member), GFP_KERNEL); | |
76 | if (!member) | |
77 | return -ENOMEM; | |
78 | ||
79 | get_dma_buf(dma_buf); | |
80 | member->dma_buf = dma_buf; | |
81 | member->handle = handle; | |
82 | list_add(&member->entry, &prime_fpriv->head); | |
83 | return 0; | |
84 | } | |
3248877e | 85 | |
ca793f75 ML |
86 | static int drm_gem_map_attach(struct dma_buf *dma_buf, |
87 | struct device *target_dev, | |
88 | struct dma_buf_attachment *attach) | |
89 | { | |
538d6661 | 90 | struct drm_prime_attachment *prime_attach; |
ca793f75 ML |
91 | struct drm_gem_object *obj = dma_buf->priv; |
92 | struct drm_device *dev = obj->dev; | |
93 | ||
538d6661 JS |
94 | prime_attach = kzalloc(sizeof(*prime_attach), GFP_KERNEL); |
95 | if (!prime_attach) | |
96 | return -ENOMEM; | |
97 | ||
98 | prime_attach->dir = DMA_NONE; | |
99 | attach->priv = prime_attach; | |
100 | ||
ca793f75 ML |
101 | if (!dev->driver->gem_prime_pin) |
102 | return 0; | |
103 | ||
104 | return dev->driver->gem_prime_pin(obj); | |
105 | } | |
106 | ||
107 | static void drm_gem_map_detach(struct dma_buf *dma_buf, | |
108 | struct dma_buf_attachment *attach) | |
109 | { | |
538d6661 | 110 | struct drm_prime_attachment *prime_attach = attach->priv; |
ca793f75 ML |
111 | struct drm_gem_object *obj = dma_buf->priv; |
112 | struct drm_device *dev = obj->dev; | |
538d6661 | 113 | struct sg_table *sgt; |
ca793f75 ML |
114 | |
115 | if (dev->driver->gem_prime_unpin) | |
116 | dev->driver->gem_prime_unpin(obj); | |
538d6661 JS |
117 | |
118 | if (!prime_attach) | |
119 | return; | |
120 | ||
121 | sgt = prime_attach->sgt; | |
f9d8a129 JS |
122 | if (sgt) { |
123 | if (prime_attach->dir != DMA_NONE) | |
124 | dma_unmap_sg(attach->dev, sgt->sgl, sgt->nents, | |
125 | prime_attach->dir); | |
126 | sg_free_table(sgt); | |
127 | } | |
538d6661 | 128 | |
538d6661 JS |
129 | kfree(sgt); |
130 | kfree(prime_attach); | |
131 | attach->priv = NULL; | |
ca793f75 ML |
132 | } |
133 | ||
da34242e YC |
134 | static void drm_prime_remove_buf_handle_locked( |
135 | struct drm_prime_file_private *prime_fpriv, | |
136 | struct dma_buf *dma_buf) | |
137 | { | |
138 | struct drm_prime_member *member, *safe; | |
139 | ||
140 | list_for_each_entry_safe(member, safe, &prime_fpriv->head, entry) { | |
141 | if (member->dma_buf == dma_buf) { | |
142 | dma_buf_put(dma_buf); | |
143 | list_del(&member->entry); | |
144 | kfree(member); | |
145 | } | |
146 | } | |
147 | } | |
148 | ||
89177644 AP |
149 | static struct sg_table *drm_gem_map_dma_buf(struct dma_buf_attachment *attach, |
150 | enum dma_data_direction dir) | |
151 | { | |
538d6661 | 152 | struct drm_prime_attachment *prime_attach = attach->priv; |
89177644 AP |
153 | struct drm_gem_object *obj = attach->dmabuf->priv; |
154 | struct sg_table *sgt; | |
155 | ||
538d6661 JS |
156 | if (WARN_ON(dir == DMA_NONE || !prime_attach)) |
157 | return ERR_PTR(-EINVAL); | |
158 | ||
159 | /* return the cached mapping when possible */ | |
160 | if (prime_attach->dir == dir) | |
161 | return prime_attach->sgt; | |
162 | ||
163 | /* | |
164 | * two mappings with different directions for the same attachment are | |
165 | * not allowed | |
166 | */ | |
167 | if (WARN_ON(prime_attach->dir != DMA_NONE)) | |
168 | return ERR_PTR(-EBUSY); | |
169 | ||
89177644 AP |
170 | sgt = obj->dev->driver->gem_prime_get_sg_table(obj); |
171 | ||
7e3d88f9 | 172 | if (!IS_ERR(sgt)) { |
b720d54a YC |
173 | if (!dma_map_sg(attach->dev, sgt->sgl, sgt->nents, dir)) { |
174 | sg_free_table(sgt); | |
175 | kfree(sgt); | |
176 | sgt = ERR_PTR(-ENOMEM); | |
538d6661 JS |
177 | } else { |
178 | prime_attach->sgt = sgt; | |
179 | prime_attach->dir = dir; | |
b720d54a YC |
180 | } |
181 | } | |
89177644 | 182 | |
89177644 AP |
183 | return sgt; |
184 | } | |
185 | ||
186 | static void drm_gem_unmap_dma_buf(struct dma_buf_attachment *attach, | |
187 | struct sg_table *sgt, enum dma_data_direction dir) | |
188 | { | |
538d6661 | 189 | /* nothing to be done here */ |
89177644 AP |
190 | } |
191 | ||
c1d6798d | 192 | void drm_gem_dmabuf_release(struct dma_buf *dma_buf) |
89177644 AP |
193 | { |
194 | struct drm_gem_object *obj = dma_buf->priv; | |
195 | ||
196 | if (obj->export_dma_buf == dma_buf) { | |
197 | /* drop the reference on the export fd holds */ | |
198 | obj->export_dma_buf = NULL; | |
199 | drm_gem_object_unreference_unlocked(obj); | |
200 | } | |
201 | } | |
c1d6798d | 202 | EXPORT_SYMBOL(drm_gem_dmabuf_release); |
89177644 AP |
203 | |
204 | static void *drm_gem_dmabuf_vmap(struct dma_buf *dma_buf) | |
205 | { | |
206 | struct drm_gem_object *obj = dma_buf->priv; | |
207 | struct drm_device *dev = obj->dev; | |
208 | ||
209 | return dev->driver->gem_prime_vmap(obj); | |
210 | } | |
211 | ||
212 | static void drm_gem_dmabuf_vunmap(struct dma_buf *dma_buf, void *vaddr) | |
213 | { | |
214 | struct drm_gem_object *obj = dma_buf->priv; | |
215 | struct drm_device *dev = obj->dev; | |
216 | ||
217 | dev->driver->gem_prime_vunmap(obj, vaddr); | |
218 | } | |
219 | ||
220 | static void *drm_gem_dmabuf_kmap_atomic(struct dma_buf *dma_buf, | |
221 | unsigned long page_num) | |
222 | { | |
223 | return NULL; | |
224 | } | |
225 | ||
226 | static void drm_gem_dmabuf_kunmap_atomic(struct dma_buf *dma_buf, | |
227 | unsigned long page_num, void *addr) | |
228 | { | |
229 | ||
230 | } | |
231 | static void *drm_gem_dmabuf_kmap(struct dma_buf *dma_buf, | |
232 | unsigned long page_num) | |
233 | { | |
234 | return NULL; | |
235 | } | |
236 | ||
237 | static void drm_gem_dmabuf_kunmap(struct dma_buf *dma_buf, | |
238 | unsigned long page_num, void *addr) | |
239 | { | |
240 | ||
241 | } | |
242 | ||
243 | static int drm_gem_dmabuf_mmap(struct dma_buf *dma_buf, | |
244 | struct vm_area_struct *vma) | |
245 | { | |
7c397cd9 JS |
246 | struct drm_gem_object *obj = dma_buf->priv; |
247 | struct drm_device *dev = obj->dev; | |
248 | ||
249 | if (!dev->driver->gem_prime_mmap) | |
250 | return -ENOSYS; | |
251 | ||
252 | return dev->driver->gem_prime_mmap(obj, vma); | |
89177644 AP |
253 | } |
254 | ||
255 | static const struct dma_buf_ops drm_gem_prime_dmabuf_ops = { | |
ca793f75 ML |
256 | .attach = drm_gem_map_attach, |
257 | .detach = drm_gem_map_detach, | |
89177644 AP |
258 | .map_dma_buf = drm_gem_map_dma_buf, |
259 | .unmap_dma_buf = drm_gem_unmap_dma_buf, | |
260 | .release = drm_gem_dmabuf_release, | |
261 | .kmap = drm_gem_dmabuf_kmap, | |
262 | .kmap_atomic = drm_gem_dmabuf_kmap_atomic, | |
263 | .kunmap = drm_gem_dmabuf_kunmap, | |
264 | .kunmap_atomic = drm_gem_dmabuf_kunmap_atomic, | |
265 | .mmap = drm_gem_dmabuf_mmap, | |
266 | .vmap = drm_gem_dmabuf_vmap, | |
267 | .vunmap = drm_gem_dmabuf_vunmap, | |
268 | }; | |
269 | ||
270 | /** | |
271 | * DOC: PRIME Helpers | |
272 | * | |
273 | * Drivers can implement @gem_prime_export and @gem_prime_import in terms of | |
274 | * simpler APIs by using the helper functions @drm_gem_prime_export and | |
275 | * @drm_gem_prime_import. These functions implement dma-buf support in terms of | |
276 | * five lower-level driver callbacks: | |
277 | * | |
278 | * Export callbacks: | |
279 | * | |
280 | * - @gem_prime_pin (optional): prepare a GEM object for exporting | |
281 | * | |
282 | * - @gem_prime_get_sg_table: provide a scatter/gather table of pinned pages | |
283 | * | |
284 | * - @gem_prime_vmap: vmap a buffer exported by your driver | |
285 | * | |
286 | * - @gem_prime_vunmap: vunmap a buffer exported by your driver | |
287 | * | |
288 | * Import callback: | |
289 | * | |
290 | * - @gem_prime_import_sg_table (import): produce a GEM object from another | |
291 | * driver's scatter/gather table | |
292 | */ | |
293 | ||
294 | struct dma_buf *drm_gem_prime_export(struct drm_device *dev, | |
295 | struct drm_gem_object *obj, int flags) | |
296 | { | |
ebc0bad4 | 297 | return dma_buf_export(obj, &drm_gem_prime_dmabuf_ops, obj->size, flags); |
89177644 AP |
298 | } |
299 | EXPORT_SYMBOL(drm_gem_prime_export); | |
300 | ||
3248877e DA |
301 | int drm_gem_prime_handle_to_fd(struct drm_device *dev, |
302 | struct drm_file *file_priv, uint32_t handle, uint32_t flags, | |
303 | int *prime_fd) | |
304 | { | |
305 | struct drm_gem_object *obj; | |
219b4733 DA |
306 | int ret = 0; |
307 | struct dma_buf *dmabuf; | |
3248877e DA |
308 | |
309 | obj = drm_gem_object_lookup(dev, file_priv, handle); | |
310 | if (!obj) | |
311 | return -ENOENT; | |
312 | ||
3248877e DA |
313 | /* re-export the original imported object */ |
314 | if (obj->import_attach) { | |
219b4733 DA |
315 | dmabuf = obj->import_attach->dmabuf; |
316 | goto out_have_obj; | |
3248877e DA |
317 | } |
318 | ||
319 | if (obj->export_dma_buf) { | |
219b4733 DA |
320 | dmabuf = obj->export_dma_buf; |
321 | goto out_have_obj; | |
3248877e | 322 | } |
219b4733 | 323 | |
4332bf43 DV |
324 | dmabuf = dev->driver->gem_prime_export(dev, obj, flags); |
325 | if (IS_ERR(dmabuf)) { | |
219b4733 DA |
326 | /* normally the created dma-buf takes ownership of the ref, |
327 | * but if that fails then drop the ref | |
328 | */ | |
4332bf43 | 329 | ret = PTR_ERR(dmabuf); |
219b4733 DA |
330 | goto out; |
331 | } | |
4332bf43 | 332 | obj->export_dma_buf = dmabuf; |
219b4733 | 333 | |
bdf655de | 334 | mutex_lock(&file_priv->prime.lock); |
0ff926c7 DA |
335 | /* if we've exported this buffer the cheat and add it to the import list |
336 | * so we get the correct handle back | |
337 | */ | |
219b4733 DA |
338 | ret = drm_prime_add_buf_handle(&file_priv->prime, |
339 | obj->export_dma_buf, handle); | |
340 | if (ret) | |
7d8f06ac | 341 | goto fail_put_dmabuf; |
0ff926c7 | 342 | |
4332bf43 | 343 | ret = dma_buf_fd(dmabuf, flags); |
da34242e YC |
344 | if (ret < 0) |
345 | goto fail_rm_handle; | |
346 | ||
347 | *prime_fd = ret; | |
3248877e DA |
348 | mutex_unlock(&file_priv->prime.lock); |
349 | return 0; | |
219b4733 DA |
350 | |
351 | out_have_obj: | |
352 | get_dma_buf(dmabuf); | |
da34242e | 353 | ret = dma_buf_fd(dmabuf, flags); |
4a88f73f | 354 | if (ret < 0) { |
da34242e | 355 | dma_buf_put(dmabuf); |
4a88f73f | 356 | } else { |
da34242e | 357 | *prime_fd = ret; |
4a88f73f DV |
358 | ret = 0; |
359 | } | |
360 | ||
7d8f06ac YC |
361 | goto out; |
362 | ||
da34242e | 363 | fail_rm_handle: |
4332bf43 DV |
364 | drm_prime_remove_buf_handle_locked(&file_priv->prime, |
365 | dmabuf); | |
bdf655de | 366 | mutex_unlock(&file_priv->prime.lock); |
7d8f06ac YC |
367 | fail_put_dmabuf: |
368 | /* clear NOT to be checked when releasing dma_buf */ | |
369 | obj->export_dma_buf = NULL; | |
4332bf43 | 370 | dma_buf_put(dmabuf); |
219b4733 DA |
371 | out: |
372 | drm_gem_object_unreference_unlocked(obj); | |
219b4733 | 373 | return ret; |
3248877e DA |
374 | } |
375 | EXPORT_SYMBOL(drm_gem_prime_handle_to_fd); | |
376 | ||
89177644 AP |
377 | struct drm_gem_object *drm_gem_prime_import(struct drm_device *dev, |
378 | struct dma_buf *dma_buf) | |
379 | { | |
380 | struct dma_buf_attachment *attach; | |
381 | struct sg_table *sgt; | |
382 | struct drm_gem_object *obj; | |
383 | int ret; | |
384 | ||
385 | if (!dev->driver->gem_prime_import_sg_table) | |
386 | return ERR_PTR(-EINVAL); | |
387 | ||
388 | if (dma_buf->ops == &drm_gem_prime_dmabuf_ops) { | |
389 | obj = dma_buf->priv; | |
390 | if (obj->dev == dev) { | |
391 | /* | |
392 | * Importing dmabuf exported from out own gem increases | |
393 | * refcount on gem itself instead of f_count of dmabuf. | |
394 | */ | |
395 | drm_gem_object_reference(obj); | |
89177644 AP |
396 | return obj; |
397 | } | |
398 | } | |
399 | ||
400 | attach = dma_buf_attach(dma_buf, dev->dev); | |
401 | if (IS_ERR(attach)) | |
f2a5da4f | 402 | return ERR_CAST(attach); |
89177644 | 403 | |
011c2282 ID |
404 | get_dma_buf(dma_buf); |
405 | ||
89177644 AP |
406 | sgt = dma_buf_map_attachment(attach, DMA_BIDIRECTIONAL); |
407 | if (IS_ERR_OR_NULL(sgt)) { | |
408 | ret = PTR_ERR(sgt); | |
409 | goto fail_detach; | |
410 | } | |
411 | ||
412 | obj = dev->driver->gem_prime_import_sg_table(dev, dma_buf->size, sgt); | |
413 | if (IS_ERR(obj)) { | |
414 | ret = PTR_ERR(obj); | |
415 | goto fail_unmap; | |
416 | } | |
417 | ||
418 | obj->import_attach = attach; | |
419 | ||
420 | return obj; | |
421 | ||
422 | fail_unmap: | |
423 | dma_buf_unmap_attachment(attach, sgt, DMA_BIDIRECTIONAL); | |
424 | fail_detach: | |
425 | dma_buf_detach(dma_buf, attach); | |
011c2282 ID |
426 | dma_buf_put(dma_buf); |
427 | ||
89177644 AP |
428 | return ERR_PTR(ret); |
429 | } | |
430 | EXPORT_SYMBOL(drm_gem_prime_import); | |
431 | ||
3248877e DA |
432 | int drm_gem_prime_fd_to_handle(struct drm_device *dev, |
433 | struct drm_file *file_priv, int prime_fd, uint32_t *handle) | |
434 | { | |
435 | struct dma_buf *dma_buf; | |
436 | struct drm_gem_object *obj; | |
437 | int ret; | |
438 | ||
439 | dma_buf = dma_buf_get(prime_fd); | |
440 | if (IS_ERR(dma_buf)) | |
441 | return PTR_ERR(dma_buf); | |
442 | ||
443 | mutex_lock(&file_priv->prime.lock); | |
444 | ||
219b4733 | 445 | ret = drm_prime_lookup_buf_handle(&file_priv->prime, |
3248877e | 446 | dma_buf, handle); |
84341c28 | 447 | if (ret == 0) |
3248877e | 448 | goto out_put; |
3248877e DA |
449 | |
450 | /* never seen this one, need to import */ | |
451 | obj = dev->driver->gem_prime_import(dev, dma_buf); | |
452 | if (IS_ERR(obj)) { | |
453 | ret = PTR_ERR(obj); | |
454 | goto out_put; | |
455 | } | |
456 | ||
457 | ret = drm_gem_handle_create(file_priv, obj, handle); | |
458 | drm_gem_object_unreference_unlocked(obj); | |
459 | if (ret) | |
460 | goto out_put; | |
461 | ||
219b4733 | 462 | ret = drm_prime_add_buf_handle(&file_priv->prime, |
3248877e DA |
463 | dma_buf, *handle); |
464 | if (ret) | |
465 | goto fail; | |
466 | ||
467 | mutex_unlock(&file_priv->prime.lock); | |
011c2282 ID |
468 | |
469 | dma_buf_put(dma_buf); | |
470 | ||
3248877e DA |
471 | return 0; |
472 | ||
473 | fail: | |
474 | /* hmm, if driver attached, we are relying on the free-object path | |
475 | * to detach.. which seems ok.. | |
476 | */ | |
730c4ff9 | 477 | drm_gem_handle_delete(file_priv, *handle); |
3248877e DA |
478 | out_put: |
479 | dma_buf_put(dma_buf); | |
480 | mutex_unlock(&file_priv->prime.lock); | |
481 | return ret; | |
482 | } | |
483 | EXPORT_SYMBOL(drm_gem_prime_fd_to_handle); | |
484 | ||
485 | int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, void *data, | |
486 | struct drm_file *file_priv) | |
487 | { | |
488 | struct drm_prime_handle *args = data; | |
489 | uint32_t flags; | |
490 | ||
491 | if (!drm_core_check_feature(dev, DRIVER_PRIME)) | |
492 | return -EINVAL; | |
493 | ||
494 | if (!dev->driver->prime_handle_to_fd) | |
495 | return -ENOSYS; | |
496 | ||
497 | /* check flags are valid */ | |
498 | if (args->flags & ~DRM_CLOEXEC) | |
499 | return -EINVAL; | |
500 | ||
501 | /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */ | |
502 | flags = args->flags & DRM_CLOEXEC; | |
503 | ||
504 | return dev->driver->prime_handle_to_fd(dev, file_priv, | |
505 | args->handle, flags, &args->fd); | |
506 | } | |
507 | ||
508 | int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data, | |
509 | struct drm_file *file_priv) | |
510 | { | |
511 | struct drm_prime_handle *args = data; | |
512 | ||
513 | if (!drm_core_check_feature(dev, DRIVER_PRIME)) | |
514 | return -EINVAL; | |
515 | ||
516 | if (!dev->driver->prime_fd_to_handle) | |
517 | return -ENOSYS; | |
518 | ||
519 | return dev->driver->prime_fd_to_handle(dev, file_priv, | |
520 | args->fd, &args->handle); | |
521 | } | |
522 | ||
523 | /* | |
524 | * drm_prime_pages_to_sg | |
525 | * | |
526 | * this helper creates an sg table object from a set of pages | |
527 | * the driver is responsible for mapping the pages into the | |
528 | * importers address space | |
529 | */ | |
530 | struct sg_table *drm_prime_pages_to_sg(struct page **pages, int nr_pages) | |
531 | { | |
532 | struct sg_table *sg = NULL; | |
3248877e DA |
533 | int ret; |
534 | ||
535 | sg = kmalloc(sizeof(struct sg_table), GFP_KERNEL); | |
7e3d88f9 YC |
536 | if (!sg) { |
537 | ret = -ENOMEM; | |
3248877e | 538 | goto out; |
7e3d88f9 | 539 | } |
3248877e | 540 | |
dca25cb8 RS |
541 | ret = sg_alloc_table_from_pages(sg, pages, nr_pages, 0, |
542 | nr_pages << PAGE_SHIFT, GFP_KERNEL); | |
3248877e DA |
543 | if (ret) |
544 | goto out; | |
545 | ||
3248877e DA |
546 | return sg; |
547 | out: | |
548 | kfree(sg); | |
7e3d88f9 | 549 | return ERR_PTR(ret); |
3248877e DA |
550 | } |
551 | EXPORT_SYMBOL(drm_prime_pages_to_sg); | |
552 | ||
51ab7ba2 DA |
553 | /* export an sg table into an array of pages and addresses |
554 | this is currently required by the TTM driver in order to do correct fault | |
555 | handling */ | |
556 | int drm_prime_sg_to_page_addr_arrays(struct sg_table *sgt, struct page **pages, | |
557 | dma_addr_t *addrs, int max_pages) | |
558 | { | |
559 | unsigned count; | |
560 | struct scatterlist *sg; | |
561 | struct page *page; | |
562 | u32 len, offset; | |
563 | int pg_index; | |
564 | dma_addr_t addr; | |
565 | ||
566 | pg_index = 0; | |
567 | for_each_sg(sgt->sgl, sg, sgt->nents, count) { | |
568 | len = sg->length; | |
569 | offset = sg->offset; | |
570 | page = sg_page(sg); | |
571 | addr = sg_dma_address(sg); | |
572 | ||
573 | while (len > 0) { | |
574 | if (WARN_ON(pg_index >= max_pages)) | |
575 | return -1; | |
576 | pages[pg_index] = page; | |
577 | if (addrs) | |
578 | addrs[pg_index] = addr; | |
579 | ||
580 | page++; | |
581 | addr += PAGE_SIZE; | |
582 | len -= PAGE_SIZE; | |
583 | pg_index++; | |
584 | } | |
585 | } | |
586 | return 0; | |
587 | } | |
588 | EXPORT_SYMBOL(drm_prime_sg_to_page_addr_arrays); | |
3248877e DA |
589 | /* helper function to cleanup a GEM/prime object */ |
590 | void drm_prime_gem_destroy(struct drm_gem_object *obj, struct sg_table *sg) | |
591 | { | |
592 | struct dma_buf_attachment *attach; | |
593 | struct dma_buf *dma_buf; | |
594 | attach = obj->import_attach; | |
595 | if (sg) | |
596 | dma_buf_unmap_attachment(attach, sg, DMA_BIDIRECTIONAL); | |
597 | dma_buf = attach->dmabuf; | |
598 | dma_buf_detach(attach->dmabuf, attach); | |
599 | /* remove the reference */ | |
600 | dma_buf_put(dma_buf); | |
601 | } | |
602 | EXPORT_SYMBOL(drm_prime_gem_destroy); | |
603 | ||
604 | void drm_prime_init_file_private(struct drm_prime_file_private *prime_fpriv) | |
605 | { | |
606 | INIT_LIST_HEAD(&prime_fpriv->head); | |
607 | mutex_init(&prime_fpriv->lock); | |
608 | } | |
609 | EXPORT_SYMBOL(drm_prime_init_file_private); | |
610 | ||
611 | void drm_prime_destroy_file_private(struct drm_prime_file_private *prime_fpriv) | |
612 | { | |
98b76231 ID |
613 | /* by now drm_gem_release should've made sure the list is empty */ |
614 | WARN_ON(!list_empty(&prime_fpriv->head)); | |
3248877e DA |
615 | } |
616 | EXPORT_SYMBOL(drm_prime_destroy_file_private); | |
617 | ||
219b4733 | 618 | int drm_prime_lookup_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf, uint32_t *handle) |
3248877e DA |
619 | { |
620 | struct drm_prime_member *member; | |
621 | ||
622 | list_for_each_entry(member, &prime_fpriv->head, entry) { | |
623 | if (member->dma_buf == dma_buf) { | |
624 | *handle = member->handle; | |
625 | return 0; | |
626 | } | |
627 | } | |
628 | return -ENOENT; | |
629 | } | |
219b4733 | 630 | EXPORT_SYMBOL(drm_prime_lookup_buf_handle); |
3248877e | 631 | |
219b4733 | 632 | void drm_prime_remove_buf_handle(struct drm_prime_file_private *prime_fpriv, struct dma_buf *dma_buf) |
3248877e | 633 | { |
3248877e | 634 | mutex_lock(&prime_fpriv->lock); |
da34242e | 635 | drm_prime_remove_buf_handle_locked(prime_fpriv, dma_buf); |
3248877e DA |
636 | mutex_unlock(&prime_fpriv->lock); |
637 | } | |
219b4733 | 638 | EXPORT_SYMBOL(drm_prime_remove_buf_handle); |