blob: 1200c5ba37dad1ab369d542f06479bc1ad0e7ce4 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
28#include <linux/ktime.h>
Stephen Rothwell568d7c72016-03-17 15:30:49 +110029#include <linux/pagemap.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040030#include <drm/drmP.h>
31#include <drm/amdgpu_drm.h>
32#include "amdgpu.h"
33
34void amdgpu_gem_object_free(struct drm_gem_object *gobj)
35{
36 struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj);
37
38 if (robj) {
Christian König9298e522015-06-03 21:31:20 +020039 amdgpu_mn_unregister(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040 amdgpu_bo_unref(&robj);
41 }
42}
43
44int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
Christian Könige1eb899b42017-08-25 09:14:43 +020045 int alignment, u32 initial_domain,
Christian Königeab3de22018-03-14 14:48:17 -050046 u64 flags, enum ttm_bo_type type,
Christian Könige1eb899b42017-08-25 09:14:43 +020047 struct reservation_object *resv,
48 struct drm_gem_object **obj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040049{
Christian Könige1eb899b42017-08-25 09:14:43 +020050 struct amdgpu_bo *bo;
Chunming Zhou3216c6b2018-04-16 18:27:50 +080051 struct amdgpu_bo_param bp;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040052 int r;
53
Chunming Zhou3216c6b2018-04-16 18:27:50 +080054 memset(&bp, 0, sizeof(bp));
Alex Deucherd38ceaf2015-04-20 16:55:21 -040055 *obj = NULL;
56 /* At least align on page size */
57 if (alignment < PAGE_SIZE) {
58 alignment = PAGE_SIZE;
59 }
60
Chunming Zhou3216c6b2018-04-16 18:27:50 +080061 bp.size = size;
62 bp.byte_align = alignment;
63 bp.type = type;
64 bp.resv = resv;
Christian König08082102018-04-10 13:42:38 +020065retry:
Chunming Zhou3216c6b2018-04-16 18:27:50 +080066 bp.flags = flags;
67 bp.domain = initial_domain;
68 r = amdgpu_bo_create(adev, &bp, &bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040069 if (r) {
Christian König08082102018-04-10 13:42:38 +020070 if (r != -ERESTARTSYS) {
71 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
72 flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
73 goto retry;
74 }
75
76 if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
77 initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
78 goto retry;
79 }
80 DRM_DEBUG("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
81 size, initial_domain, alignment, r);
82 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083 return r;
84 }
Christian Könige1eb899b42017-08-25 09:14:43 +020085 *obj = &bo->gem_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086
Alex Deucherd38ceaf2015-04-20 16:55:21 -040087 return 0;
88}
89
Christian König418aa0c2016-02-15 16:59:57 +010090void amdgpu_gem_force_release(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040091{
Christian König418aa0c2016-02-15 16:59:57 +010092 struct drm_device *ddev = adev->ddev;
93 struct drm_file *file;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094
Daniel Vetter1d2ac402016-04-26 19:29:41 +020095 mutex_lock(&ddev->filelist_mutex);
Christian König418aa0c2016-02-15 16:59:57 +010096
97 list_for_each_entry(file, &ddev->filelist, lhead) {
98 struct drm_gem_object *gobj;
99 int handle;
100
101 WARN_ONCE(1, "Still active user space clients!\n");
102 spin_lock(&file->table_lock);
103 idr_for_each_entry(&file->object_idr, gobj, handle) {
104 WARN_ONCE(1, "And also active allocations!\n");
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300105 drm_gem_object_put_unlocked(gobj);
Christian König418aa0c2016-02-15 16:59:57 +0100106 }
107 idr_destroy(&file->object_idr);
108 spin_unlock(&file->table_lock);
109 }
110
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200111 mutex_unlock(&ddev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112}
113
114/*
115 * Call from drm_gem_handle_create which appear in both new and open ioctl
116 * case.
117 */
Christian Königa7d64de2016-09-15 14:58:48 +0200118int amdgpu_gem_object_open(struct drm_gem_object *obj,
119 struct drm_file *file_priv)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120{
Christian König765e7fb2016-09-15 15:06:50 +0200121 struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
Christian Königa7d64de2016-09-15 14:58:48 +0200122 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
124 struct amdgpu_vm *vm = &fpriv->vm;
125 struct amdgpu_bo_va *bo_va;
Christian König4f5839c2017-08-29 16:07:31 +0200126 struct mm_struct *mm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127 int r;
Christian König4f5839c2017-08-29 16:07:31 +0200128
129 mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm);
130 if (mm && mm != current->mm)
131 return -EPERM;
132
Christian Könige1eb899b42017-08-25 09:14:43 +0200133 if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID &&
134 abo->tbo.resv != vm->root.base.bo->tbo.resv)
135 return -EPERM;
136
Christian König765e7fb2016-09-15 15:06:50 +0200137 r = amdgpu_bo_reserve(abo, false);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800138 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400139 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400140
Christian König765e7fb2016-09-15 15:06:50 +0200141 bo_va = amdgpu_vm_bo_find(vm, abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142 if (!bo_va) {
Christian König765e7fb2016-09-15 15:06:50 +0200143 bo_va = amdgpu_vm_bo_add(adev, vm, abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400144 } else {
145 ++bo_va->ref_count;
146 }
Christian König765e7fb2016-09-15 15:06:50 +0200147 amdgpu_bo_unreserve(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400148 return 0;
149}
150
151void amdgpu_gem_object_close(struct drm_gem_object *obj,
152 struct drm_file *file_priv)
153{
Christian Königb5a5ec52016-03-08 17:47:46 +0100154 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
Christian Königa7d64de2016-09-15 14:58:48 +0200155 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
157 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb5a5ec52016-03-08 17:47:46 +0100158
159 struct amdgpu_bo_list_entry vm_pd;
Christian Könige1eb899b42017-08-25 09:14:43 +0200160 struct list_head list, duplicates;
Christian Königb5a5ec52016-03-08 17:47:46 +0100161 struct ttm_validate_buffer tv;
162 struct ww_acquire_ctx ticket;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400163 struct amdgpu_bo_va *bo_va;
164 int r;
Christian Königb5a5ec52016-03-08 17:47:46 +0100165
166 INIT_LIST_HEAD(&list);
Christian Könige1eb899b42017-08-25 09:14:43 +0200167 INIT_LIST_HEAD(&duplicates);
Christian Königb5a5ec52016-03-08 17:47:46 +0100168
169 tv.bo = &bo->tbo;
170 tv.shared = true;
171 list_add(&tv.head, &list);
172
173 amdgpu_vm_get_pd_bo(vm, &list, &vm_pd);
174
Christian Könige1eb899b42017-08-25 09:14:43 +0200175 r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400176 if (r) {
177 dev_err(adev->dev, "leaking bo va because "
178 "we fail to reserve bo (%d)\n", r);
179 return;
180 }
Christian Königb5a5ec52016-03-08 17:47:46 +0100181 bo_va = amdgpu_vm_bo_find(vm, bo);
Christian König5a0f3b52017-04-21 10:05:56 +0200182 if (bo_va && --bo_va->ref_count == 0) {
183 amdgpu_vm_bo_rmv(adev, bo_va);
184
Christian König3f3333f2017-08-03 14:02:13 +0200185 if (amdgpu_vm_ready(vm)) {
Christian König5a0f3b52017-04-21 10:05:56 +0200186 struct dma_fence *fence = NULL;
Nicolai Hähnle23e05632017-03-23 19:34:11 +0100187
188 r = amdgpu_vm_clear_freed(adev, vm, &fence);
189 if (unlikely(r)) {
190 dev_err(adev->dev, "failed to clear page "
191 "tables on GEM object close (%d)\n", r);
192 }
193
194 if (fence) {
195 amdgpu_bo_fence(bo, fence, true);
196 dma_fence_put(fence);
197 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198 }
199 }
Christian Königb5a5ec52016-03-08 17:47:46 +0100200 ttm_eu_backoff_reservation(&ticket, &list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400201}
202
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400203/*
204 * GEM ioctls.
205 */
206int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
207 struct drm_file *filp)
208{
209 struct amdgpu_device *adev = dev->dev_private;
Christian Könige1eb899b42017-08-25 09:14:43 +0200210 struct amdgpu_fpriv *fpriv = filp->driver_priv;
211 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400212 union drm_amdgpu_gem_create *args = data;
Christian König6ac7def2017-08-23 20:11:25 +0200213 uint64_t flags = args->in.domain_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400214 uint64_t size = args->in.bo_size;
Christian Könige1eb899b42017-08-25 09:14:43 +0200215 struct reservation_object *resv = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400216 struct drm_gem_object *gobj;
217 uint32_t handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400218 int r;
219
Alex Deucher834e0f82017-03-08 17:40:17 -0500220 /* reject invalid gem flags */
Christian König6ac7def2017-08-23 20:11:25 +0200221 if (flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
222 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
223 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
Christian Könige1eb899b42017-08-25 09:14:43 +0200224 AMDGPU_GEM_CREATE_VRAM_CLEARED |
Andres Rodriguez177ae092017-09-15 20:44:06 -0400225 AMDGPU_GEM_CREATE_VM_ALWAYS_VALID |
226 AMDGPU_GEM_CREATE_EXPLICIT_SYNC))
227
Christian Königa022c542017-05-08 15:14:54 +0200228 return -EINVAL;
229
Alex Deucher834e0f82017-03-08 17:40:17 -0500230 /* reject invalid gem domains */
231 if (args->in.domains & ~(AMDGPU_GEM_DOMAIN_CPU |
232 AMDGPU_GEM_DOMAIN_GTT |
233 AMDGPU_GEM_DOMAIN_VRAM |
234 AMDGPU_GEM_DOMAIN_GDS |
235 AMDGPU_GEM_DOMAIN_GWS |
Christian Königa022c542017-05-08 15:14:54 +0200236 AMDGPU_GEM_DOMAIN_OA))
237 return -EINVAL;
Alex Deucher834e0f82017-03-08 17:40:17 -0500238
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400239 /* create a gem object to contain this object in */
240 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
241 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
Christian König6ac7def2017-08-23 20:11:25 +0200242 flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400243 if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
244 size = size << AMDGPU_GDS_SHIFT;
245 else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
246 size = size << AMDGPU_GWS_SHIFT;
247 else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
248 size = size << AMDGPU_OA_SHIFT;
Christian Königa022c542017-05-08 15:14:54 +0200249 else
250 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400251 }
252 size = roundup(size, PAGE_SIZE);
253
Christian Könige1eb899b42017-08-25 09:14:43 +0200254 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
255 r = amdgpu_bo_reserve(vm->root.base.bo, false);
256 if (r)
257 return r;
258
259 resv = vm->root.base.bo->tbo.resv;
260 }
261
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400262 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
263 (u32)(0xffffffff & args->in.domains),
Christian Könige1eb899b42017-08-25 09:14:43 +0200264 flags, false, resv, &gobj);
265 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
266 if (!r) {
267 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
268
269 abo->parent = amdgpu_bo_ref(vm->root.base.bo);
270 }
271 amdgpu_bo_unreserve(vm->root.base.bo);
272 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400273 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200274 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400275
276 r = drm_gem_handle_create(filp, gobj, &handle);
277 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300278 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400279 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200280 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400281
282 memset(args, 0, sizeof(*args));
283 args->out.handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400284 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400285}
286
287int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
288 struct drm_file *filp)
289{
Christian König19be5572017-04-12 14:24:39 +0200290 struct ttm_operation_ctx ctx = { true, false };
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400291 struct amdgpu_device *adev = dev->dev_private;
292 struct drm_amdgpu_gem_userptr *args = data;
293 struct drm_gem_object *gobj;
294 struct amdgpu_bo *bo;
295 uint32_t handle;
296 int r;
297
298 if (offset_in_page(args->addr | args->size))
299 return -EINVAL;
300
301 /* reject unknown flag values */
302 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
303 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
304 AMDGPU_GEM_USERPTR_REGISTER))
305 return -EINVAL;
306
Christian König358c2582016-03-11 15:29:27 +0100307 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
308 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400309
Christian König358c2582016-03-11 15:29:27 +0100310 /* if we want to write to it we must install a MMU notifier */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400311 return -EACCES;
312 }
313
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400314 /* create a gem object to contain this object in */
Christian Könige1eb899b42017-08-25 09:14:43 +0200315 r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
316 0, 0, NULL, &gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400317 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200318 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400319
320 bo = gem_to_amdgpu_bo(gobj);
Kent Russell6d7d9c52017-08-08 07:58:01 -0400321 bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
Christian König1ea863f2015-12-18 22:13:12 +0100322 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400323 r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
324 if (r)
325 goto release_object;
326
327 if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
328 r = amdgpu_mn_register(bo, args->addr);
329 if (r)
330 goto release_object;
331 }
332
333 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
Christian König2f568db2016-02-23 12:36:59 +0100334 r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
335 bo->tbo.ttm->pages);
336 if (r)
Xiangliang.Yud5a480b2017-10-20 17:21:40 +0800337 goto release_object;
Christian König2f568db2016-02-23 12:36:59 +0100338
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400339 r = amdgpu_bo_reserve(bo, true);
Christian König2f568db2016-02-23 12:36:59 +0100340 if (r)
341 goto free_pages;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400342
343 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
Christian König19be5572017-04-12 14:24:39 +0200344 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400345 amdgpu_bo_unreserve(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400346 if (r)
Christian König2f568db2016-02-23 12:36:59 +0100347 goto free_pages;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400348 }
349
350 r = drm_gem_handle_create(filp, gobj, &handle);
351 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300352 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400353 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200354 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400355
356 args->handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357 return 0;
358
Christian König2f568db2016-02-23 12:36:59 +0100359free_pages:
Mel Gormanc6f92f92017-11-15 17:37:55 -0800360 release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages);
Christian König2f568db2016-02-23 12:36:59 +0100361
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400362release_object:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300363 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400364
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400365 return r;
366}
367
368int amdgpu_mode_dumb_mmap(struct drm_file *filp,
369 struct drm_device *dev,
370 uint32_t handle, uint64_t *offset_p)
371{
372 struct drm_gem_object *gobj;
373 struct amdgpu_bo *robj;
374
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100375 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400376 if (gobj == NULL) {
377 return -ENOENT;
378 }
379 robj = gem_to_amdgpu_bo(gobj);
Christian Königcc325d12016-02-08 11:08:35 +0100380 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
Christian König271c8122015-05-13 14:30:53 +0200381 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300382 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400383 return -EPERM;
384 }
385 *offset_p = amdgpu_bo_mmap_offset(robj);
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300386 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400387 return 0;
388}
389
390int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
391 struct drm_file *filp)
392{
393 union drm_amdgpu_gem_mmap *args = data;
394 uint32_t handle = args->in.handle;
395 memset(args, 0, sizeof(*args));
396 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
397}
398
399/**
400 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
401 *
402 * @timeout_ns: timeout in ns
403 *
404 * Calculate the timeout in jiffies from an absolute timeout in ns.
405 */
406unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
407{
408 unsigned long timeout_jiffies;
409 ktime_t timeout;
410
411 /* clamp timeout if it's to large */
412 if (((int64_t)timeout_ns) < 0)
413 return MAX_SCHEDULE_TIMEOUT;
414
Christian König0f117702015-07-08 16:58:48 +0200415 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400416 if (ktime_to_ns(timeout) < 0)
417 return 0;
418
419 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
420 /* clamp timeout to avoid unsigned-> signed overflow */
421 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
422 return MAX_SCHEDULE_TIMEOUT - 1;
423
424 return timeout_jiffies;
425}
426
427int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
428 struct drm_file *filp)
429{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400430 union drm_amdgpu_gem_wait_idle *args = data;
431 struct drm_gem_object *gobj;
432 struct amdgpu_bo *robj;
433 uint32_t handle = args->in.handle;
434 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
435 int r = 0;
436 long ret;
437
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100438 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400439 if (gobj == NULL) {
440 return -ENOENT;
441 }
442 robj = gem_to_amdgpu_bo(gobj);
Chris Wilson0fea2ed2016-08-29 08:08:24 +0100443 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
444 timeout);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445
446 /* ret == 0 means not signaled,
447 * ret > 0 means signaled
448 * ret < 0 means interrupted before timeout
449 */
450 if (ret >= 0) {
451 memset(args, 0, sizeof(*args));
452 args->out.status = (ret == 0);
453 } else
454 r = ret;
455
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300456 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400457 return r;
458}
459
460int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
461 struct drm_file *filp)
462{
463 struct drm_amdgpu_gem_metadata *args = data;
464 struct drm_gem_object *gobj;
465 struct amdgpu_bo *robj;
466 int r = -1;
467
468 DRM_DEBUG("%d \n", args->handle);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100469 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400470 if (gobj == NULL)
471 return -ENOENT;
472 robj = gem_to_amdgpu_bo(gobj);
473
474 r = amdgpu_bo_reserve(robj, false);
475 if (unlikely(r != 0))
476 goto out;
477
478 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
479 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
480 r = amdgpu_bo_get_metadata(robj, args->data.data,
481 sizeof(args->data.data),
482 &args->data.data_size_bytes,
483 &args->data.flags);
484 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
Dan Carpenter0913eab2015-09-23 14:00:35 +0300485 if (args->data.data_size_bytes > sizeof(args->data.data)) {
486 r = -EINVAL;
487 goto unreserve;
488 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400489 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
490 if (!r)
491 r = amdgpu_bo_set_metadata(robj, args->data.data,
492 args->data.data_size_bytes,
493 args->data.flags);
494 }
495
Dan Carpenter0913eab2015-09-23 14:00:35 +0300496unreserve:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400497 amdgpu_bo_unreserve(robj);
498out:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300499 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500 return r;
501}
502
503/**
504 * amdgpu_gem_va_update_vm -update the bo_va in its VM
505 *
506 * @adev: amdgpu_device pointer
Christian Königdc54d3d2017-03-13 10:13:38 +0100507 * @vm: vm to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400508 * @bo_va: bo_va to update
Christian König2ffdaaf2017-01-27 15:58:43 +0100509 * @list: validation list
Christian Königdc54d3d2017-03-13 10:13:38 +0100510 * @operation: map, unmap or clear
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511 *
Christian König2ffdaaf2017-01-27 15:58:43 +0100512 * Update the bo_va directly after setting its address. Errors are not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513 * vital here, so they are not reported back to userspace.
514 */
515static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
Christian Königdc54d3d2017-03-13 10:13:38 +0100516 struct amdgpu_vm *vm,
Christian Königf7da30d2016-09-28 12:03:04 +0200517 struct amdgpu_bo_va *bo_va,
Christian König2ffdaaf2017-01-27 15:58:43 +0100518 struct list_head *list,
Christian Königf7da30d2016-09-28 12:03:04 +0200519 uint32_t operation)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400520{
Christian König3f3333f2017-08-03 14:02:13 +0200521 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400522
Christian König3f3333f2017-08-03 14:02:13 +0200523 if (!amdgpu_vm_ready(vm))
524 return;
Chunming Zhoue410b5c2015-12-07 15:02:52 +0800525
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100526 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400527 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100528 goto error;
monk.liu194a3362015-07-22 13:29:28 +0800529
Christian König80f95c52017-03-13 10:13:39 +0100530 if (operation == AMDGPU_VA_OP_MAP ||
Gustavo A. R. Silva93bab702018-02-14 23:20:00 -0600531 operation == AMDGPU_VA_OP_REPLACE) {
Flora Cui05dcb5c2016-09-22 11:34:47 +0800532 r = amdgpu_vm_bo_update(adev, bo_va, false);
Gustavo A. R. Silva93bab702018-02-14 23:20:00 -0600533 if (r)
534 goto error;
535 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536
Christian König0abc6872017-09-01 20:37:57 +0200537 r = amdgpu_vm_update_directories(adev, vm);
Christian König0abc6872017-09-01 20:37:57 +0200538
Christian König2ffdaaf2017-01-27 15:58:43 +0100539error:
Christian König68fdd3d2015-06-16 14:50:02 +0200540 if (r && r != -ERESTARTSYS)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
542}
543
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
545 struct drm_file *filp)
546{
Junwei Zhangb85891b2017-01-16 13:59:01 +0800547 const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
548 AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
Alex Xie66e02bc2017-02-14 12:04:52 -0500549 AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800550 const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
551 AMDGPU_VM_PAGE_PRT;
552
Christian König34b5f6a2015-06-08 15:03:00 +0200553 struct drm_amdgpu_gem_va *args = data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400554 struct drm_gem_object *gobj;
555 struct amdgpu_device *adev = dev->dev_private;
556 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Christian König765e7fb2016-09-15 15:06:50 +0200557 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400558 struct amdgpu_bo_va *bo_va;
Christian Königb88c8792016-09-28 16:33:01 +0200559 struct amdgpu_bo_list_entry vm_pd;
560 struct ttm_validate_buffer tv;
Chunming Zhou49b02b12015-11-13 14:18:38 +0800561 struct ww_acquire_ctx ticket;
Christian Könige1eb899b42017-08-25 09:14:43 +0200562 struct list_head list, duplicates;
Alex Xie54635452017-02-14 12:22:57 -0500563 uint64_t va_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400564 int r = 0;
565
Christian König34b5f6a2015-06-08 15:03:00 +0200566 if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
Christian König4b7f0842017-11-13 13:58:17 +0100567 dev_dbg(&dev->pdev->dev,
Christian Königff4cd382017-11-06 15:25:37 +0100568 "va_address 0x%LX is in reserved area 0x%LX\n",
569 args->va_address, AMDGPU_VA_RESERVED_SIZE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400570 return -EINVAL;
571 }
572
Christian Königbb7939b2017-11-06 15:37:01 +0100573 if (args->va_address >= AMDGPU_VA_HOLE_START &&
574 args->va_address < AMDGPU_VA_HOLE_END) {
575 dev_dbg(&dev->pdev->dev,
576 "va_address 0x%LX is in VA hole 0x%LX-0x%LX\n",
577 args->va_address, AMDGPU_VA_HOLE_START,
578 AMDGPU_VA_HOLE_END);
579 return -EINVAL;
580 }
581
582 args->va_address &= AMDGPU_VA_HOLE_MASK;
583
Junwei Zhangb85891b2017-01-16 13:59:01 +0800584 if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
Christian König4b7f0842017-11-13 13:58:17 +0100585 dev_dbg(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
Junwei Zhangb85891b2017-01-16 13:59:01 +0800586 args->flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587 return -EINVAL;
588 }
589
Christian König34b5f6a2015-06-08 15:03:00 +0200590 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591 case AMDGPU_VA_OP_MAP:
592 case AMDGPU_VA_OP_UNMAP:
Christian Königdc54d3d2017-03-13 10:13:38 +0100593 case AMDGPU_VA_OP_CLEAR:
Christian König80f95c52017-03-13 10:13:39 +0100594 case AMDGPU_VA_OP_REPLACE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400595 break;
596 default:
Christian König4b7f0842017-11-13 13:58:17 +0100597 dev_dbg(&dev->pdev->dev, "unsupported operation %d\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200598 args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400599 return -EINVAL;
600 }
601
Chunming Zhou49b02b12015-11-13 14:18:38 +0800602 INIT_LIST_HEAD(&list);
Christian Könige1eb899b42017-08-25 09:14:43 +0200603 INIT_LIST_HEAD(&duplicates);
Christian Königdc54d3d2017-03-13 10:13:38 +0100604 if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
605 !(args->flags & AMDGPU_VM_PAGE_PRT)) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800606 gobj = drm_gem_object_lookup(filp, args->handle);
607 if (gobj == NULL)
608 return -ENOENT;
609 abo = gem_to_amdgpu_bo(gobj);
610 tv.bo = &abo->tbo;
611 tv.shared = false;
612 list_add(&tv.head, &list);
613 } else {
614 gobj = NULL;
615 abo = NULL;
616 }
Chunming Zhou49b02b12015-11-13 14:18:38 +0800617
Christian Königb88c8792016-09-28 16:33:01 +0200618 amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
Christian Königb5a5ec52016-03-08 17:47:46 +0100619
Christian Könige1eb899b42017-08-25 09:14:43 +0200620 r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800621 if (r)
622 goto error_unref;
Christian König34b5f6a2015-06-08 15:03:00 +0200623
Junwei Zhangb85891b2017-01-16 13:59:01 +0800624 if (abo) {
625 bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
626 if (!bo_va) {
627 r = -ENOENT;
628 goto error_backoff;
629 }
Christian Königdc54d3d2017-03-13 10:13:38 +0100630 } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800631 bo_va = fpriv->prt_va;
Christian Königdc54d3d2017-03-13 10:13:38 +0100632 } else {
633 bo_va = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400634 }
635
Christian König34b5f6a2015-06-08 15:03:00 +0200636 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400637 case AMDGPU_VA_OP_MAP:
Christian Königec681542017-08-01 10:51:43 +0200638 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
Christian König663e4572017-03-13 10:13:37 +0100639 args->map_size);
640 if (r)
641 goto error_backoff;
Alex Xie54635452017-02-14 12:22:57 -0500642
Christian König132f34e2018-01-12 15:26:08 +0100643 va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
Christian König34b5f6a2015-06-08 15:03:00 +0200644 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
645 args->offset_in_bo, args->map_size,
Christian König9f7eb532015-05-18 16:05:57 +0200646 va_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647 break;
648 case AMDGPU_VA_OP_UNMAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200649 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400650 break;
Christian Königdc54d3d2017-03-13 10:13:38 +0100651
652 case AMDGPU_VA_OP_CLEAR:
653 r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
654 args->va_address,
655 args->map_size);
656 break;
Christian König80f95c52017-03-13 10:13:39 +0100657 case AMDGPU_VA_OP_REPLACE:
Christian Königec681542017-08-01 10:51:43 +0200658 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
Christian König80f95c52017-03-13 10:13:39 +0100659 args->map_size);
660 if (r)
661 goto error_backoff;
662
Christian König132f34e2018-01-12 15:26:08 +0100663 va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
Christian König80f95c52017-03-13 10:13:39 +0100664 r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
665 args->offset_in_bo, args->map_size,
666 va_flags);
667 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400668 default:
669 break;
670 }
Junwei Zhangb85891b2017-01-16 13:59:01 +0800671 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
Christian Königdc54d3d2017-03-13 10:13:38 +0100672 amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, &list,
673 args->operation);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800674
675error_backoff:
Christian König2ffdaaf2017-01-27 15:58:43 +0100676 ttm_eu_backoff_reservation(&ticket, &list);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800677
Junwei Zhangb85891b2017-01-16 13:59:01 +0800678error_unref:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300679 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400680 return r;
681}
682
683int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
684 struct drm_file *filp)
685{
Christian Könige1eb899b42017-08-25 09:14:43 +0200686 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400687 struct drm_amdgpu_gem_op *args = data;
688 struct drm_gem_object *gobj;
689 struct amdgpu_bo *robj;
690 int r;
691
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100692 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400693 if (gobj == NULL) {
694 return -ENOENT;
695 }
696 robj = gem_to_amdgpu_bo(gobj);
697
698 r = amdgpu_bo_reserve(robj, false);
699 if (unlikely(r))
700 goto out;
701
702 switch (args->op) {
703 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
704 struct drm_amdgpu_gem_create_in info;
Christian König7ecc2452017-07-26 17:02:52 +0200705 void __user *out = u64_to_user_ptr(args->value);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400706
707 info.bo_size = robj->gem_base.size;
708 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
Kent Russell6d7d9c52017-08-08 07:58:01 -0400709 info.domains = robj->preferred_domains;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400710 info.domain_flags = robj->flags;
Christian König4c28fb02015-08-28 17:27:54 +0200711 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400712 if (copy_to_user(out, &info, sizeof(info)))
713 r = -EFAULT;
714 break;
715 }
Marek Olšákd8f65a22015-05-27 14:30:38 +0200716 case AMDGPU_GEM_OP_SET_PLACEMENT:
Christopher James Halse Rogers803d89a2017-04-03 13:31:22 +1000717 if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
718 r = -EINVAL;
719 amdgpu_bo_unreserve(robj);
720 break;
721 }
Christian Königcc325d12016-02-08 11:08:35 +0100722 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400723 r = -EPERM;
Christian König4c28fb02015-08-28 17:27:54 +0200724 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400725 break;
726 }
Kent Russell6d7d9c52017-08-08 07:58:01 -0400727 robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
Christian König1ea863f2015-12-18 22:13:12 +0100728 AMDGPU_GEM_DOMAIN_GTT |
729 AMDGPU_GEM_DOMAIN_CPU);
Kent Russell6d7d9c52017-08-08 07:58:01 -0400730 robj->allowed_domains = robj->preferred_domains;
Christian König1ea863f2015-12-18 22:13:12 +0100731 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
732 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
733
Christian Könige1eb899b42017-08-25 09:14:43 +0200734 if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
735 amdgpu_vm_bo_invalidate(adev, robj, true);
736
Christian König4c28fb02015-08-28 17:27:54 +0200737 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400738 break;
739 default:
Christian König4c28fb02015-08-28 17:27:54 +0200740 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400741 r = -EINVAL;
742 }
743
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400744out:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300745 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400746 return r;
747}
748
749int amdgpu_mode_dumb_create(struct drm_file *file_priv,
750 struct drm_device *dev,
751 struct drm_mode_create_dumb *args)
752{
753 struct amdgpu_device *adev = dev->dev_private;
754 struct drm_gem_object *gobj;
755 uint32_t handle;
756 int r;
757
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300758 args->pitch = amdgpu_align_pitch(adev, args->width,
759 DIV_ROUND_UP(args->bpp, 8), 0);
Dan Carpenter54ef0b52015-09-23 14:00:59 +0300760 args->size = (u64)args->pitch * args->height;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400761 args->size = ALIGN(args->size, PAGE_SIZE);
762
763 r = amdgpu_gem_object_create(adev, args->size, 0,
764 AMDGPU_GEM_DOMAIN_VRAM,
Alex Deucher857d9132015-08-27 00:14:16 -0400765 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian Könige1eb899b42017-08-25 09:14:43 +0200766 false, NULL, &gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400767 if (r)
768 return -ENOMEM;
769
770 r = drm_gem_handle_create(file_priv, gobj, &handle);
771 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300772 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400773 if (r) {
774 return r;
775 }
776 args->handle = handle;
777 return 0;
778}
779
780#if defined(CONFIG_DEBUG_FS)
Christian König7ea23562016-02-15 15:23:00 +0100781static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
782{
783 struct drm_gem_object *gobj = ptr;
784 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
785 struct seq_file *m = data;
786
787 unsigned domain;
788 const char *placement;
789 unsigned pin_count;
Christian Königb8e0e6e2017-06-26 15:19:30 +0200790 uint64_t offset;
Christian König7ea23562016-02-15 15:23:00 +0100791
792 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
793 switch (domain) {
794 case AMDGPU_GEM_DOMAIN_VRAM:
795 placement = "VRAM";
796 break;
797 case AMDGPU_GEM_DOMAIN_GTT:
798 placement = " GTT";
799 break;
800 case AMDGPU_GEM_DOMAIN_CPU:
801 default:
802 placement = " CPU";
803 break;
804 }
Christian Königb8e0e6e2017-06-26 15:19:30 +0200805 seq_printf(m, "\t0x%08x: %12ld byte %s",
806 id, amdgpu_bo_size(bo), placement);
807
Mark Rutland6aa7de02017-10-23 14:07:29 -0700808 offset = READ_ONCE(bo->tbo.mem.start);
Christian Königb8e0e6e2017-06-26 15:19:30 +0200809 if (offset != AMDGPU_BO_INVALID_OFFSET)
810 seq_printf(m, " @ 0x%010Lx", offset);
Christian König7ea23562016-02-15 15:23:00 +0100811
Mark Rutland6aa7de02017-10-23 14:07:29 -0700812 pin_count = READ_ONCE(bo->pin_count);
Christian König7ea23562016-02-15 15:23:00 +0100813 if (pin_count)
814 seq_printf(m, " pin count %d", pin_count);
815 seq_printf(m, "\n");
816
817 return 0;
818}
819
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400820static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
821{
822 struct drm_info_node *node = (struct drm_info_node *)m->private;
823 struct drm_device *dev = node->minor->dev;
Christian König7ea23562016-02-15 15:23:00 +0100824 struct drm_file *file;
825 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400826
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200827 r = mutex_lock_interruptible(&dev->filelist_mutex);
Christian König7ea23562016-02-15 15:23:00 +0100828 if (r)
829 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400830
Christian König7ea23562016-02-15 15:23:00 +0100831 list_for_each_entry(file, &dev->filelist, lhead) {
832 struct task_struct *task;
Christian Königb22e3ce2016-02-15 12:41:37 +0100833
Christian König7ea23562016-02-15 15:23:00 +0100834 /*
835 * Although we have a valid reference on file->pid, that does
836 * not guarantee that the task_struct who called get_pid() is
837 * still alive (e.g. get_pid(current) => fork() => exit()).
838 * Therefore, we need to protect this ->comm access using RCU.
839 */
840 rcu_read_lock();
841 task = pid_task(file->pid, PIDTYPE_PID);
842 seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
843 task ? task->comm : "<unknown>");
844 rcu_read_unlock();
845
846 spin_lock(&file->table_lock);
847 idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
848 spin_unlock(&file->table_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400849 }
Christian König7ea23562016-02-15 15:23:00 +0100850
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200851 mutex_unlock(&dev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400852 return 0;
853}
854
Nils Wallménius06ab6832016-05-02 12:46:15 -0400855static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400856 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
857};
858#endif
859
Alex Deucher75758252017-12-14 15:23:14 -0500860int amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400861{
862#if defined(CONFIG_DEBUG_FS)
863 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
864#endif
865 return 0;
866}