blob: f6d7f6404a07d19d27257156761d598c9ea0b6e8 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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#include <drm/drmP.h>
27
28#include "amdgpu.h"
Samuel Li09052fc2017-12-08 16:18:59 -050029#include "amdgpu_display.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040030#include <drm/amdgpu_drm.h>
31#include <linux/dma-buf.h>
32
33struct sg_table *amdgpu_gem_prime_get_sg_table(struct drm_gem_object *obj)
34{
35 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
36 int npages = bo->tbo.num_pages;
37
38 return drm_prime_pages_to_sg(bo->tbo.ttm->pages, npages);
39}
40
41void *amdgpu_gem_prime_vmap(struct drm_gem_object *obj)
42{
43 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
44 int ret;
45
46 ret = ttm_bo_kmap(&bo->tbo, 0, bo->tbo.num_pages,
47 &bo->dma_buf_vmap);
48 if (ret)
49 return ERR_PTR(ret);
50
51 return bo->dma_buf_vmap.virtual;
52}
53
54void amdgpu_gem_prime_vunmap(struct drm_gem_object *obj, void *vaddr)
55{
56 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
57
58 ttm_bo_kunmap(&bo->dma_buf_vmap);
59}
60
Samuel Lidfced2e2017-08-22 15:25:33 -040061int amdgpu_gem_prime_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
62{
63 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
64 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
65 unsigned asize = amdgpu_bo_size(bo);
66 int ret;
67
68 if (!vma->vm_file)
69 return -ENODEV;
70
71 if (adev == NULL)
72 return -ENODEV;
73
74 /* Check for valid size. */
75 if (asize < vma->vm_end - vma->vm_start)
76 return -EINVAL;
77
78 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
79 (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
80 return -EPERM;
81 }
82 vma->vm_pgoff += amdgpu_bo_mmap_offset(bo) >> PAGE_SHIFT;
83
84 /* prime mmap does not need to check access, so allow here */
85 ret = drm_vma_node_allow(&obj->vma_node, vma->vm_file->private_data);
86 if (ret)
87 return ret;
88
89 ret = ttm_bo_mmap(vma->vm_file, vma, &adev->mman.bdev);
90 drm_vma_node_revoke(&obj->vma_node, vma->vm_file->private_data);
91
92 return ret;
93}
94
Christian König4d9c5142016-05-03 18:46:19 +020095struct drm_gem_object *
96amdgpu_gem_prime_import_sg_table(struct drm_device *dev,
97 struct dma_buf_attachment *attach,
98 struct sg_table *sg)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040099{
Christian König72d76682015-09-03 17:34:59 +0200100 struct reservation_object *resv = attach->dmabuf->resv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101 struct amdgpu_device *adev = dev->dev_private;
102 struct amdgpu_bo *bo;
103 int ret;
104
Christian König72d76682015-09-03 17:34:59 +0200105 ww_mutex_lock(&resv->lock, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 ret = amdgpu_bo_create(adev, attach->dmabuf->size, PAGE_SIZE, false,
Christian König8febe612018-01-24 19:55:32 +0100107 AMDGPU_GEM_DOMAIN_GTT, 0, sg, resv, &bo);
Christian König72d76682015-09-03 17:34:59 +0200108 ww_mutex_unlock(&resv->lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109 if (ret)
110 return ERR_PTR(ret);
111
Mario Kleiner8e94a462016-11-09 02:25:15 +0100112 bo->prime_shared_count = 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113 return &bo->gem_base;
114}
115
Christian König5a137612018-02-16 13:16:11 +0100116static int amdgpu_gem_map_attach(struct dma_buf *dma_buf,
117 struct device *target_dev,
118 struct dma_buf_attachment *attach)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119{
Christian König5a137612018-02-16 13:16:11 +0100120 struct drm_gem_object *obj = dma_buf->priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400121 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
Christian König5a137612018-02-16 13:16:11 +0100122 long r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123
Christian König5a137612018-02-16 13:16:11 +0100124 r = drm_gem_map_attach(dma_buf, target_dev, attach);
125 if (r)
126 return r;
127
128 r = amdgpu_bo_reserve(bo, false);
129 if (unlikely(r != 0))
130 goto error_detach;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131
Mario Kleiner8e94a462016-11-09 02:25:15 +0100132 /*
133 * Wait for all shared fences to complete before we switch to future
134 * use of exclusive fence on this prime shared bo.
135 */
Christian König5a137612018-02-16 13:16:11 +0100136 r = reservation_object_wait_timeout_rcu(bo->tbo.resv, true, false,
137 MAX_SCHEDULE_TIMEOUT);
138 if (unlikely(r < 0)) {
139 DRM_DEBUG_PRIME("Fence wait failed: %li\n", r);
140 goto error_unreserve;
Mario Kleiner8e94a462016-11-09 02:25:15 +0100141 }
142
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400143 /* pin buffer into GTT */
Christian König5a137612018-02-16 13:16:11 +0100144 r = amdgpu_bo_pin(bo, AMDGPU_GEM_DOMAIN_GTT, NULL);
145 if (likely(r == 0))
Mario Kleiner8e94a462016-11-09 02:25:15 +0100146 bo->prime_shared_count++;
147
Christian König5a137612018-02-16 13:16:11 +0100148error_unreserve:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400149 amdgpu_bo_unreserve(bo);
Christian König5a137612018-02-16 13:16:11 +0100150
151error_detach:
152 if (r)
153 drm_gem_map_detach(dma_buf, attach);
154 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400155}
156
Christian König5a137612018-02-16 13:16:11 +0100157static void amdgpu_gem_map_detach(struct dma_buf *dma_buf,
158 struct dma_buf_attachment *attach)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400159{
Christian König5a137612018-02-16 13:16:11 +0100160 struct drm_gem_object *obj = dma_buf->priv;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400161 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
162 int ret = 0;
163
Michel Dänzerc81a1a72017-04-28 17:28:14 +0900164 ret = amdgpu_bo_reserve(bo, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400165 if (unlikely(ret != 0))
Christian König5a137612018-02-16 13:16:11 +0100166 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400167
168 amdgpu_bo_unpin(bo);
Mario Kleiner8e94a462016-11-09 02:25:15 +0100169 if (bo->prime_shared_count)
170 bo->prime_shared_count--;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400171 amdgpu_bo_unreserve(bo);
Christian König5a137612018-02-16 13:16:11 +0100172
173error:
174 drm_gem_map_detach(dma_buf, attach);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400175}
176
177struct reservation_object *amdgpu_gem_prime_res_obj(struct drm_gem_object *obj)
178{
179 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
180
181 return bo->tbo.resv;
182}
183
Samuel Li09052fc2017-12-08 16:18:59 -0500184static int amdgpu_gem_begin_cpu_access(struct dma_buf *dma_buf,
185 enum dma_data_direction direction)
186{
187 struct amdgpu_bo *bo = gem_to_amdgpu_bo(dma_buf->priv);
188 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
189 struct ttm_operation_ctx ctx = { true, false };
190 u32 domain = amdgpu_display_framebuffer_domains(adev);
191 int ret;
192 bool reads = (direction == DMA_BIDIRECTIONAL ||
193 direction == DMA_FROM_DEVICE);
194
195 if (!reads || !(domain & AMDGPU_GEM_DOMAIN_GTT))
196 return 0;
197
198 /* move to gtt */
199 ret = amdgpu_bo_reserve(bo, false);
200 if (unlikely(ret != 0))
201 return ret;
202
203 if (!bo->pin_count && (bo->allowed_domains & AMDGPU_GEM_DOMAIN_GTT)) {
204 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
205 ret = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
206 }
207
208 amdgpu_bo_unreserve(bo);
209 return ret;
210}
211
212static const struct dma_buf_ops amdgpu_dmabuf_ops = {
Christian König5a137612018-02-16 13:16:11 +0100213 .attach = amdgpu_gem_map_attach,
214 .detach = amdgpu_gem_map_detach,
Samuel Li09052fc2017-12-08 16:18:59 -0500215 .map_dma_buf = drm_gem_map_dma_buf,
216 .unmap_dma_buf = drm_gem_unmap_dma_buf,
217 .release = drm_gem_dmabuf_release,
218 .begin_cpu_access = amdgpu_gem_begin_cpu_access,
219 .map = drm_gem_dmabuf_kmap,
220 .map_atomic = drm_gem_dmabuf_kmap_atomic,
221 .unmap = drm_gem_dmabuf_kunmap,
222 .unmap_atomic = drm_gem_dmabuf_kunmap_atomic,
223 .mmap = drm_gem_dmabuf_mmap,
224 .vmap = drm_gem_dmabuf_vmap,
225 .vunmap = drm_gem_dmabuf_vunmap,
226};
227
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400228struct dma_buf *amdgpu_gem_prime_export(struct drm_device *dev,
229 struct drm_gem_object *gobj,
230 int flags)
231{
232 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
Christian König4b277242017-11-13 17:20:50 +0100233 struct dma_buf *buf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400234
Christian Könige1eb899b42017-08-25 09:14:43 +0200235 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm) ||
236 bo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400237 return ERR_PTR(-EPERM);
238
Christian König4b277242017-11-13 17:20:50 +0100239 buf = drm_gem_prime_export(dev, gobj, flags);
Samuel Li09052fc2017-12-08 16:18:59 -0500240 if (!IS_ERR(buf)) {
Christian König4b277242017-11-13 17:20:50 +0100241 buf->file->f_mapping = dev->anon_inode->i_mapping;
Samuel Li09052fc2017-12-08 16:18:59 -0500242 buf->ops = &amdgpu_dmabuf_ops;
243 }
244
Christian König4b277242017-11-13 17:20:50 +0100245 return buf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400246}
Samuel Li09052fc2017-12-08 16:18:59 -0500247
248struct drm_gem_object *amdgpu_gem_prime_import(struct drm_device *dev,
249 struct dma_buf *dma_buf)
250{
251 struct drm_gem_object *obj;
252
253 if (dma_buf->ops == &amdgpu_dmabuf_ops) {
254 obj = dma_buf->priv;
255 if (obj->dev == dev) {
256 /*
257 * Importing dmabuf exported from out own gem increases
258 * refcount on gem itself instead of f_count of dmabuf.
259 */
260 drm_gem_object_get(obj);
261 return obj;
262 }
263 }
264
265 return drm_gem_prime_import(dev, dma_buf);
266}