]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_prime.c
drm/amd: drop use of drmP.h in amdgpu/amdgpu*
[linux.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_prime.c
1 /*
2  * Copyright 2012 Advanced Micro Devices, Inc.
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 shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  * based on nouveau_prime.c
23  *
24  * Authors: Alex Deucher
25  */
26
27 /**
28  * DOC: PRIME Buffer Sharing
29  *
30  * The following callback implementations are used for :ref:`sharing GEM buffer
31  * objects between different devices via PRIME <prime_buffer_sharing>`.
32  */
33
34 #include "amdgpu.h"
35 #include "amdgpu_display.h"
36 #include "amdgpu_gem.h"
37 #include <drm/amdgpu_drm.h>
38 #include <linux/dma-buf.h>
39 #include <linux/dma-fence-array.h>
40
41 /**
42  * amdgpu_gem_prime_get_sg_table - &drm_driver.gem_prime_get_sg_table
43  * implementation
44  * @obj: GEM buffer object (BO)
45  *
46  * Returns:
47  * A scatter/gather table for the pinned pages of the BO's memory.
48  */
49 struct sg_table *amdgpu_gem_prime_get_sg_table(struct drm_gem_object *obj)
50 {
51         struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
52         int npages = bo->tbo.num_pages;
53
54         return drm_prime_pages_to_sg(bo->tbo.ttm->pages, npages);
55 }
56
57 /**
58  * amdgpu_gem_prime_vmap - &dma_buf_ops.vmap implementation
59  * @obj: GEM BO
60  *
61  * Sets up an in-kernel virtual mapping of the BO's memory.
62  *
63  * Returns:
64  * The virtual address of the mapping or an error pointer.
65  */
66 void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj)
67 {
68         struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
69         int ret;
70
71         ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages,
72                           &bo->dma_buf_vmap);
73         if (ret)
74                 return ERR_PTR(ret);
75
76         return bo->dma_buf_vmap.virtual;
77 }
78
79 /**
80  * amdgpu_gem_prime_vunmap - &dma_buf_ops.vunmap implementation
81  * @obj: GEM BO
82  * @vaddr: Virtual address (unused)
83  *
84  * Tears down the in-kernel virtual mapping of the BO's memory.
85  */
86 void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
87 {
88         struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
89
90         ttm_bo_kunmap(&bo->dma_buf_vmap);
91 }
92
93 /**
94  * amdgpu_gem_prime_mmap - &drm_driver.gem_prime_mmap implementation
95  * @obj: GEM BO
96  * @vma: Virtual memory area
97  *
98  * Sets up a userspace mapping of the BO's memory in the given
99  * virtual memory area.
100  *
101  * Returns:
102  * 0 on success or a negative error code on failure.
103  */
104 int amdgpu_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
105 {
106         struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
107         struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
108         unsigned asize = amdgpu_bo_size(bo);
109         int ret;
110
111         if (!vma->vm_file)
112                 return -ENODEV;
113
114         if (adev == NULL)
115                 return -ENODEV;
116
117         /* Check for valid size. */
118         if (asize < vma->vm_end - vma->vm_start)
119                 return -EINVAL;
120
121         if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
122             (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
123                 return -EPERM;
124         }
125         vma->vm_pgoff += amdgpu_bo_mmap_offset(bo) >> PAGE_SHIFT;
126
127         /* prime mmap does not need to check access, so allow here */
128         ret = drm_vma_node_allow(&obj->vma_node, vma->vm_file->private_data);
129         if (ret)
130                 return ret;
131
132         ret = ttm_bo_mmap(vma->vm_file, vma, &adev->mman.bdev);
133         drm_vma_node_revoke(&obj->vma_node, vma->vm_file->private_data);
134
135         return ret;
136 }
137
138 /**
139  * amdgpu_gem_prime_import_sg_table - &drm_driver.gem_prime_import_sg_table
140  * implementation
141  * @dev: DRM device
142  * @attach: DMA-buf attachment
143  * @sg: Scatter/gather table
144  *
145  * Imports shared DMA buffer memory exported by another device.
146  *
147  * Returns:
148  * A new GEM BO of the given DRM device, representing the memory
149  * described by the given DMA-buf attachment and scatter/gather table.
150  */
151 struct drm_gem_object *
152 amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
153                                  struct dma_buf_attachment *attach,
154                                  struct sg_table *sg)
155 {
156         struct reservation_object *resv = attach->dmabuf->resv;
157         struct amdgpu_device *adev = dev->dev_private;
158         struct amdgpu_bo *bo;
159         struct amdgpu_bo_param bp;
160         int ret;
161
162         memset(&bp, 0, sizeof(bp));
163         bp.size = attach->dmabuf->size;
164         bp.byte_align = PAGE_SIZE;
165         bp.domain = AMDGPU_GEM_DOMAIN_CPU;
166         bp.flags = 0;
167         bp.type = ttm_bo_type_sg;
168         bp.resv = resv;
169         ww_mutex_lock(&resv->lock, NULL);
170         ret = amdgpu_bo_create(adev, &bp, &bo);
171         if (ret)
172                 goto error;
173
174         bo->tbo.sg = sg;
175         bo->tbo.ttm->sg = sg;
176         bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
177         bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
178         if (attach->dmabuf->ops != &amdgpu_dmabuf_ops)
179                 bo->prime_shared_count = 1;
180
181         ww_mutex_unlock(&resv->lock);
182         return &bo->gem_base;
183
184 error:
185         ww_mutex_unlock(&resv->lock);
186         return ERR_PTR(ret);
187 }
188
189 static int
190 __reservation_object_make_exclusive(struct reservation_object *obj)
191 {
192         struct dma_fence **fences;
193         unsigned int count;
194         int r;
195
196         if (!reservation_object_get_list(obj)) /* no shared fences to convert */
197                 return 0;
198
199         r = reservation_object_get_fences_rcu(obj, NULL, &count, &fences);
200         if (r)
201                 return r;
202
203         if (count == 0) {
204                 /* Now that was unexpected. */
205         } else if (count == 1) {
206                 reservation_object_add_excl_fence(obj, fences[0]);
207                 dma_fence_put(fences[0]);
208                 kfree(fences);
209         } else {
210                 struct dma_fence_array *array;
211
212                 array = dma_fence_array_create(count, fences,
213                                                dma_fence_context_alloc(1), 0,
214                                                false);
215                 if (!array)
216                         goto err_fences_put;
217
218                 reservation_object_add_excl_fence(obj, &array->base);
219                 dma_fence_put(&array->base);
220         }
221
222         return 0;
223
224 err_fences_put:
225         while (count--)
226                 dma_fence_put(fences[count]);
227         kfree(fences);
228         return -ENOMEM;
229 }
230
231 /**
232  * amdgpu_gem_map_attach - &dma_buf_ops.attach implementation
233  * @dma_buf: Shared DMA buffer
234  * @attach: DMA-buf attachment
235  *
236  * Makes sure that the shared DMA buffer can be accessed by the target device.
237  * For now, simply pins it to the GTT domain, where it should be accessible by
238  * all DMA devices.
239  *
240  * Returns:
241  * 0 on success or a negative error code on failure.
242  */
243 static int amdgpu_gem_map_attach(struct dma_buf *dma_buf,
244                                  struct dma_buf_attachment *attach)
245 {
246         struct drm_gem_object *obj = dma_buf->priv;
247         struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
248         struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
249         long r;
250
251         r = drm_gem_map_attach(dma_buf, attach);
252         if (r)
253                 return r;
254
255         r = amdgpu_bo_reserve(bo, false);
256         if (unlikely(r != 0))
257                 goto error_detach;
258
259
260         if (attach->dev->driver != adev->dev->driver) {
261                 /*
262                  * We only create shared fences for internal use, but importers
263                  * of the dmabuf rely on exclusive fences for implicitly
264                  * tracking write hazards. As any of the current fences may
265                  * correspond to a write, we need to convert all existing
266                  * fences on the reservation object into a single exclusive
267                  * fence.
268                  */
269                 r = __reservation_object_make_exclusive(bo->tbo.resv);
270                 if (r)
271                         goto error_unreserve;
272         }
273
274         /* pin buffer into GTT */
275         r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT);
276         if (r)
277                 goto error_unreserve;
278
279         if (attach->dev->driver != adev->dev->driver)
280                 bo->prime_shared_count++;
281
282 error_unreserve:
283         amdgpu_bo_unreserve(bo);
284
285 error_detach:
286         if (r)
287                 drm_gem_map_detach(dma_buf, attach);
288         return r;
289 }
290
291 /**
292  * amdgpu_gem_map_detach - &dma_buf_ops.detach implementation
293  * @dma_buf: Shared DMA buffer
294  * @attach: DMA-buf attachment
295  *
296  * This is called when a shared DMA buffer no longer needs to be accessible by
297  * another device. For now, simply unpins the buffer from GTT.
298  */
299 static void amdgpu_gem_map_detach(struct dma_buf *dma_buf,
300                                   struct dma_buf_attachment *attach)
301 {
302         struct drm_gem_object *obj = dma_buf->priv;
303         struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
304         struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
305         int ret = 0;
306
307         ret = amdgpu_bo_reserve(bo, true);
308         if (unlikely(ret != 0))
309                 goto error;
310
311         amdgpu_bo_unpin(bo);
312         if (attach->dev->driver != adev->dev->driver && bo->prime_shared_count)
313                 bo->prime_shared_count--;
314         amdgpu_bo_unreserve(bo);
315
316 error:
317         drm_gem_map_detach(dma_buf, attach);
318 }
319
320 /**
321  * amdgpu_gem_prime_res_obj - &drm_driver.gem_prime_res_obj implementation
322  * @obj: GEM BO
323  *
324  * Returns:
325  * The BO's reservation object.
326  */
327 struct reservation_object *amdgpu_gem_prime_res_obj(struct drm_gem_object *obj)
328 {
329         struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
330
331         return bo->tbo.resv;
332 }
333
334 /**
335  * amdgpu_gem_begin_cpu_access - &dma_buf_ops.begin_cpu_access implementation
336  * @dma_buf: Shared DMA buffer
337  * @direction: Direction of DMA transfer
338  *
339  * This is called before CPU access to the shared DMA buffer's memory. If it's
340  * a read access, the buffer is moved to the GTT domain if possible, for optimal
341  * CPU read performance.
342  *
343  * Returns:
344  * 0 on success or a negative error code on failure.
345  */
346 static int amdgpu_gem_begin_cpu_access(struct dma_buf *dma_buf,
347                                        enum dma_data_direction direction)
348 {
349         struct amdgpu_bo *bo = gem_to_amdgpu_bo(dma_buf->priv);
350         struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
351         struct ttm_operation_ctx ctx = { true, false };
352         u32 domain = amdgpu_display_supported_domains(adev);
353         int ret;
354         bool reads = (direction == DMA_BIDIRECTIONAL ||
355                       direction == DMA_FROM_DEVICE);
356
357         if (!reads || !(domain & AMDGPU_GEM_DOMAIN_GTT))
358                 return 0;
359
360         /* move to gtt */
361         ret = amdgpu_bo_reserve(bo, false);
362         if (unlikely(ret != 0))
363                 return ret;
364
365         if (!bo->pin_count && (bo->allowed_domains & AMDGPU_GEM_DOMAIN_GTT)) {
366                 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
367                 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
368         }
369
370         amdgpu_bo_unreserve(bo);
371         return ret;
372 }
373
374 const struct dma_buf_ops amdgpu_dmabuf_ops = {
375         .attach = amdgpu_gem_map_attach,
376         .detach = amdgpu_gem_map_detach,
377         .map_dma_buf = drm_gem_map_dma_buf,
378         .unmap_dma_buf = drm_gem_unmap_dma_buf,
379         .release = drm_gem_dmabuf_release,
380         .begin_cpu_access = amdgpu_gem_begin_cpu_access,
381         .mmap = drm_gem_dmabuf_mmap,
382         .vmap = drm_gem_dmabuf_vmap,
383         .vunmap = drm_gem_dmabuf_vunmap,
384 };
385
386 /**
387  * amdgpu_gem_prime_export - &drm_driver.gem_prime_export implementation
388  * @dev: DRM device
389  * @gobj: GEM BO
390  * @flags: Flags such as DRM_CLOEXEC and DRM_RDWR.
391  *
392  * The main work is done by the &drm_gem_prime_export helper, which in turn
393  * uses &amdgpu_gem_prime_res_obj.
394  *
395  * Returns:
396  * Shared DMA buffer representing the GEM BO from the given device.
397  */
398 struct dma_buf *amdgpu_gem_prime_export(struct drm_device *dev,
399                                         struct drm_gem_object *gobj,
400                                         int flags)
401 {
402         struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
403         struct dma_buf *buf;
404
405         if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
406             bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
407                 return ERR_PTR(-EPERM);
408
409         buf = drm_gem_prime_export(dev, gobj, flags);
410         if (!IS_ERR(buf)) {
411                 buf->file->f_mapping = dev->anon_inode->i_mapping;
412                 buf->ops = &amdgpu_dmabuf_ops;
413         }
414
415         return buf;
416 }
417
418 /**
419  * amdgpu_gem_prime_import - &drm_driver.gem_prime_import implementation
420  * @dev: DRM device
421  * @dma_buf: Shared DMA buffer
422  *
423  * The main work is done by the &drm_gem_prime_import helper, which in turn
424  * uses &amdgpu_gem_prime_import_sg_table.
425  *
426  * Returns:
427  * GEM BO representing the shared DMA buffer for the given device.
428  */
429 struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
430                                             struct dma_buf *dma_buf)
431 {
432         struct drm_gem_object *obj;
433
434         if (dma_buf->ops == &amdgpu_dmabuf_ops) {
435                 obj = dma_buf->priv;
436                 if (obj->dev == dev) {
437                         /*
438                          * Importing dmabuf exported from out own gem increases
439                          * refcount on gem itself instead of f_count of dmabuf.
440                          */
441                         drm_gem_object_get(obj);
442                         return obj;
443                 }
444         }
445
446         return drm_gem_prime_import(dev, dma_buf);
447 }
This page took 0.061415 seconds and 4 git commands to generate.