blob: f1e61b3df64088906b9506ba795303b6a7c40667 [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) {
39 if (robj->gem_base.import_attach)
40 drm_prime_gem_destroy(&robj->gem_base, robj->tbo.sg);
Christian König9298e522015-06-03 21:31:20 +020041 amdgpu_mn_unregister(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040042 amdgpu_bo_unref(&robj);
43 }
44}
45
46int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
Christian Könige1eb899b42017-08-25 09:14:43 +020047 int alignment, u32 initial_domain,
48 u64 flags, bool kernel,
49 struct reservation_object *resv,
50 struct drm_gem_object **obj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040051{
Christian Könige1eb899b42017-08-25 09:14:43 +020052 struct amdgpu_bo *bo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053 int r;
54
55 *obj = NULL;
56 /* At least align on page size */
57 if (alignment < PAGE_SIZE) {
58 alignment = PAGE_SIZE;
59 }
60
Alex Deucherd38ceaf2015-04-20 16:55:21 -040061retry:
Christian König72d76682015-09-03 17:34:59 +020062 r = amdgpu_bo_create(adev, size, alignment, kernel, initial_domain,
Christian Könige1eb899b42017-08-25 09:14:43 +020063 flags, NULL, resv, 0, &bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040064 if (r) {
65 if (r != -ERESTARTSYS) {
66 if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
67 initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
68 goto retry;
69 }
70 DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
71 size, initial_domain, alignment, r);
72 }
73 return r;
74 }
Christian Könige1eb899b42017-08-25 09:14:43 +020075 *obj = &bo->gem_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040076
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077 return 0;
78}
79
Christian König418aa0c2016-02-15 16:59:57 +010080void amdgpu_gem_force_release(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040081{
Christian König418aa0c2016-02-15 16:59:57 +010082 struct drm_device *ddev = adev->ddev;
83 struct drm_file *file;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040084
Daniel Vetter1d2ac402016-04-26 19:29:41 +020085 mutex_lock(&ddev->filelist_mutex);
Christian König418aa0c2016-02-15 16:59:57 +010086
87 list_for_each_entry(file, &ddev->filelist, lhead) {
88 struct drm_gem_object *gobj;
89 int handle;
90
91 WARN_ONCE(1, "Still active user space clients!\n");
92 spin_lock(&file->table_lock);
93 idr_for_each_entry(&file->object_idr, gobj, handle) {
94 WARN_ONCE(1, "And also active allocations!\n");
Cihangir Akturkf62facc2017-08-03 14:58:16 +030095 drm_gem_object_put_unlocked(gobj);
Christian König418aa0c2016-02-15 16:59:57 +010096 }
97 idr_destroy(&file->object_idr);
98 spin_unlock(&file->table_lock);
99 }
100
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200101 mutex_unlock(&ddev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400102}
103
104/*
105 * Call from drm_gem_handle_create which appear in both new and open ioctl
106 * case.
107 */
Christian Königa7d64de2016-09-15 14:58:48 +0200108int amdgpu_gem_object_open(struct drm_gem_object *obj,
109 struct drm_file *file_priv)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110{
Christian König765e7fb2016-09-15 15:06:50 +0200111 struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
Christian Königa7d64de2016-09-15 14:58:48 +0200112 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
114 struct amdgpu_vm *vm = &fpriv->vm;
115 struct amdgpu_bo_va *bo_va;
Christian König4f5839c2017-08-29 16:07:31 +0200116 struct mm_struct *mm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117 int r;
Christian König4f5839c2017-08-29 16:07:31 +0200118
119 mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm);
120 if (mm && mm != current->mm)
121 return -EPERM;
122
Christian Könige1eb899b42017-08-25 09:14:43 +0200123 if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID &&
124 abo->tbo.resv != vm->root.base.bo->tbo.resv)
125 return -EPERM;
126
Christian König765e7fb2016-09-15 15:06:50 +0200127 r = amdgpu_bo_reserve(abo, false);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800128 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400129 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400130
Christian König765e7fb2016-09-15 15:06:50 +0200131 bo_va = amdgpu_vm_bo_find(vm, abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400132 if (!bo_va) {
Christian König765e7fb2016-09-15 15:06:50 +0200133 bo_va = amdgpu_vm_bo_add(adev, vm, abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134 } else {
135 ++bo_va->ref_count;
136 }
Christian König765e7fb2016-09-15 15:06:50 +0200137 amdgpu_bo_unreserve(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138 return 0;
139}
140
141void amdgpu_gem_object_close(struct drm_gem_object *obj,
142 struct drm_file *file_priv)
143{
Christian Königb5a5ec52016-03-08 17:47:46 +0100144 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
Christian Königa7d64de2016-09-15 14:58:48 +0200145 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400146 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
147 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb5a5ec52016-03-08 17:47:46 +0100148
149 struct amdgpu_bo_list_entry vm_pd;
Christian Könige1eb899b42017-08-25 09:14:43 +0200150 struct list_head list, duplicates;
Christian Königb5a5ec52016-03-08 17:47:46 +0100151 struct ttm_validate_buffer tv;
152 struct ww_acquire_ctx ticket;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400153 struct amdgpu_bo_va *bo_va;
154 int r;
Christian Königb5a5ec52016-03-08 17:47:46 +0100155
156 INIT_LIST_HEAD(&list);
Christian Könige1eb899b42017-08-25 09:14:43 +0200157 INIT_LIST_HEAD(&duplicates);
Christian Königb5a5ec52016-03-08 17:47:46 +0100158
159 tv.bo = &bo->tbo;
160 tv.shared = true;
161 list_add(&tv.head, &list);
162
163 amdgpu_vm_get_pd_bo(vm, &list, &vm_pd);
164
Christian Könige1eb899b42017-08-25 09:14:43 +0200165 r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400166 if (r) {
167 dev_err(adev->dev, "leaking bo va because "
168 "we fail to reserve bo (%d)\n", r);
169 return;
170 }
Christian Königb5a5ec52016-03-08 17:47:46 +0100171 bo_va = amdgpu_vm_bo_find(vm, bo);
Christian König5a0f3b52017-04-21 10:05:56 +0200172 if (bo_va && --bo_va->ref_count == 0) {
173 amdgpu_vm_bo_rmv(adev, bo_va);
174
Christian König3f3333f2017-08-03 14:02:13 +0200175 if (amdgpu_vm_ready(vm)) {
Christian König5a0f3b52017-04-21 10:05:56 +0200176 struct dma_fence *fence = NULL;
Nicolai Hähnle23e05632017-03-23 19:34:11 +0100177
178 r = amdgpu_vm_clear_freed(adev, vm, &fence);
179 if (unlikely(r)) {
180 dev_err(adev->dev, "failed to clear page "
181 "tables on GEM object close (%d)\n", r);
182 }
183
184 if (fence) {
185 amdgpu_bo_fence(bo, fence, true);
186 dma_fence_put(fence);
187 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400188 }
189 }
Christian Königb5a5ec52016-03-08 17:47:46 +0100190 ttm_eu_backoff_reservation(&ticket, &list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400191}
192
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400193/*
194 * GEM ioctls.
195 */
196int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
197 struct drm_file *filp)
198{
199 struct amdgpu_device *adev = dev->dev_private;
Christian Könige1eb899b42017-08-25 09:14:43 +0200200 struct amdgpu_fpriv *fpriv = filp->driver_priv;
201 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400202 union drm_amdgpu_gem_create *args = data;
Christian König6ac7def2017-08-23 20:11:25 +0200203 uint64_t flags = args->in.domain_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400204 uint64_t size = args->in.bo_size;
Christian Könige1eb899b42017-08-25 09:14:43 +0200205 struct reservation_object *resv = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400206 struct drm_gem_object *gobj;
207 uint32_t handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400208 int r;
209
Alex Deucher834e0f82017-03-08 17:40:17 -0500210 /* reject invalid gem flags */
Christian König6ac7def2017-08-23 20:11:25 +0200211 if (flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
212 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
213 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
Christian Könige1eb899b42017-08-25 09:14:43 +0200214 AMDGPU_GEM_CREATE_VRAM_CLEARED |
215 AMDGPU_GEM_CREATE_VM_ALWAYS_VALID))
Christian Königa022c542017-05-08 15:14:54 +0200216 return -EINVAL;
217
Alex Deucher834e0f82017-03-08 17:40:17 -0500218 /* reject invalid gem domains */
219 if (args->in.domains & ~(AMDGPU_GEM_DOMAIN_CPU |
220 AMDGPU_GEM_DOMAIN_GTT |
221 AMDGPU_GEM_DOMAIN_VRAM |
222 AMDGPU_GEM_DOMAIN_GDS |
223 AMDGPU_GEM_DOMAIN_GWS |
Christian Königa022c542017-05-08 15:14:54 +0200224 AMDGPU_GEM_DOMAIN_OA))
225 return -EINVAL;
Alex Deucher834e0f82017-03-08 17:40:17 -0500226
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400227 /* create a gem object to contain this object in */
228 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
229 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
Christian König6ac7def2017-08-23 20:11:25 +0200230 flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400231 if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
232 size = size << AMDGPU_GDS_SHIFT;
233 else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
234 size = size << AMDGPU_GWS_SHIFT;
235 else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
236 size = size << AMDGPU_OA_SHIFT;
Christian Königa022c542017-05-08 15:14:54 +0200237 else
238 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400239 }
240 size = roundup(size, PAGE_SIZE);
241
Christian Könige1eb899b42017-08-25 09:14:43 +0200242 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
243 r = amdgpu_bo_reserve(vm->root.base.bo, false);
244 if (r)
245 return r;
246
247 resv = vm->root.base.bo->tbo.resv;
248 }
249
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400250 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
251 (u32)(0xffffffff & args->in.domains),
Christian Könige1eb899b42017-08-25 09:14:43 +0200252 flags, false, resv, &gobj);
253 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
254 if (!r) {
255 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
256
257 abo->parent = amdgpu_bo_ref(vm->root.base.bo);
258 }
259 amdgpu_bo_unreserve(vm->root.base.bo);
260 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400261 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200262 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400263
264 r = drm_gem_handle_create(filp, gobj, &handle);
265 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300266 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400267 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200268 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400269
270 memset(args, 0, sizeof(*args));
271 args->out.handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400272 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400273}
274
275int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
276 struct drm_file *filp)
277{
278 struct amdgpu_device *adev = dev->dev_private;
279 struct drm_amdgpu_gem_userptr *args = data;
280 struct drm_gem_object *gobj;
281 struct amdgpu_bo *bo;
282 uint32_t handle;
283 int r;
284
285 if (offset_in_page(args->addr | args->size))
286 return -EINVAL;
287
288 /* reject unknown flag values */
289 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
290 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
291 AMDGPU_GEM_USERPTR_REGISTER))
292 return -EINVAL;
293
Christian König358c2582016-03-11 15:29:27 +0100294 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
295 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400296
Christian König358c2582016-03-11 15:29:27 +0100297 /* if we want to write to it we must install a MMU notifier */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400298 return -EACCES;
299 }
300
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400301 /* create a gem object to contain this object in */
Christian Könige1eb899b42017-08-25 09:14:43 +0200302 r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
303 0, 0, NULL, &gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400304 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200305 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400306
307 bo = gem_to_amdgpu_bo(gobj);
Kent Russell6d7d9c52017-08-08 07:58:01 -0400308 bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
Christian König1ea863f2015-12-18 22:13:12 +0100309 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400310 r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
311 if (r)
312 goto release_object;
313
314 if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
315 r = amdgpu_mn_register(bo, args->addr);
316 if (r)
317 goto release_object;
318 }
319
320 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
321 down_read(&current->mm->mmap_sem);
Christian König2f568db2016-02-23 12:36:59 +0100322
323 r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
324 bo->tbo.ttm->pages);
325 if (r)
326 goto unlock_mmap_sem;
327
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400328 r = amdgpu_bo_reserve(bo, true);
Christian König2f568db2016-02-23 12:36:59 +0100329 if (r)
330 goto free_pages;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400331
332 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
333 r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false);
334 amdgpu_bo_unreserve(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400335 if (r)
Christian König2f568db2016-02-23 12:36:59 +0100336 goto free_pages;
337
338 up_read(&current->mm->mmap_sem);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400339 }
340
341 r = drm_gem_handle_create(filp, gobj, &handle);
342 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300343 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400344 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200345 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400346
347 args->handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400348 return 0;
349
Christian König2f568db2016-02-23 12:36:59 +0100350free_pages:
351 release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages, false);
352
353unlock_mmap_sem:
354 up_read(&current->mm->mmap_sem);
355
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400356release_object:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300357 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400358
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400359 return r;
360}
361
362int amdgpu_mode_dumb_mmap(struct drm_file *filp,
363 struct drm_device *dev,
364 uint32_t handle, uint64_t *offset_p)
365{
366 struct drm_gem_object *gobj;
367 struct amdgpu_bo *robj;
368
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100369 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400370 if (gobj == NULL) {
371 return -ENOENT;
372 }
373 robj = gem_to_amdgpu_bo(gobj);
Christian Königcc325d12016-02-08 11:08:35 +0100374 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
Christian König271c8122015-05-13 14:30:53 +0200375 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300376 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400377 return -EPERM;
378 }
379 *offset_p = amdgpu_bo_mmap_offset(robj);
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300380 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400381 return 0;
382}
383
384int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
385 struct drm_file *filp)
386{
387 union drm_amdgpu_gem_mmap *args = data;
388 uint32_t handle = args->in.handle;
389 memset(args, 0, sizeof(*args));
390 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
391}
392
393/**
394 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
395 *
396 * @timeout_ns: timeout in ns
397 *
398 * Calculate the timeout in jiffies from an absolute timeout in ns.
399 */
400unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
401{
402 unsigned long timeout_jiffies;
403 ktime_t timeout;
404
405 /* clamp timeout if it's to large */
406 if (((int64_t)timeout_ns) < 0)
407 return MAX_SCHEDULE_TIMEOUT;
408
Christian König0f117702015-07-08 16:58:48 +0200409 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400410 if (ktime_to_ns(timeout) < 0)
411 return 0;
412
413 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
414 /* clamp timeout to avoid unsigned-> signed overflow */
415 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
416 return MAX_SCHEDULE_TIMEOUT - 1;
417
418 return timeout_jiffies;
419}
420
421int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
422 struct drm_file *filp)
423{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400424 union drm_amdgpu_gem_wait_idle *args = data;
425 struct drm_gem_object *gobj;
426 struct amdgpu_bo *robj;
427 uint32_t handle = args->in.handle;
428 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
429 int r = 0;
430 long ret;
431
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100432 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400433 if (gobj == NULL) {
434 return -ENOENT;
435 }
436 robj = gem_to_amdgpu_bo(gobj);
Chris Wilson0fea2ed2016-08-29 08:08:24 +0100437 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
438 timeout);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400439
440 /* ret == 0 means not signaled,
441 * ret > 0 means signaled
442 * ret < 0 means interrupted before timeout
443 */
444 if (ret >= 0) {
445 memset(args, 0, sizeof(*args));
446 args->out.status = (ret == 0);
447 } else
448 r = ret;
449
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300450 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400451 return r;
452}
453
454int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
455 struct drm_file *filp)
456{
457 struct drm_amdgpu_gem_metadata *args = data;
458 struct drm_gem_object *gobj;
459 struct amdgpu_bo *robj;
460 int r = -1;
461
462 DRM_DEBUG("%d \n", args->handle);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100463 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464 if (gobj == NULL)
465 return -ENOENT;
466 robj = gem_to_amdgpu_bo(gobj);
467
468 r = amdgpu_bo_reserve(robj, false);
469 if (unlikely(r != 0))
470 goto out;
471
472 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
473 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
474 r = amdgpu_bo_get_metadata(robj, args->data.data,
475 sizeof(args->data.data),
476 &args->data.data_size_bytes,
477 &args->data.flags);
478 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
Dan Carpenter0913eab2015-09-23 14:00:35 +0300479 if (args->data.data_size_bytes > sizeof(args->data.data)) {
480 r = -EINVAL;
481 goto unreserve;
482 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400483 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
484 if (!r)
485 r = amdgpu_bo_set_metadata(robj, args->data.data,
486 args->data.data_size_bytes,
487 args->data.flags);
488 }
489
Dan Carpenter0913eab2015-09-23 14:00:35 +0300490unreserve:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400491 amdgpu_bo_unreserve(robj);
492out:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300493 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400494 return r;
495}
496
497/**
498 * amdgpu_gem_va_update_vm -update the bo_va in its VM
499 *
500 * @adev: amdgpu_device pointer
Christian Königdc54d3d2017-03-13 10:13:38 +0100501 * @vm: vm to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400502 * @bo_va: bo_va to update
Christian König2ffdaaf2017-01-27 15:58:43 +0100503 * @list: validation list
Christian Königdc54d3d2017-03-13 10:13:38 +0100504 * @operation: map, unmap or clear
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400505 *
Christian König2ffdaaf2017-01-27 15:58:43 +0100506 * Update the bo_va directly after setting its address. Errors are not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507 * vital here, so they are not reported back to userspace.
508 */
509static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
Christian Königdc54d3d2017-03-13 10:13:38 +0100510 struct amdgpu_vm *vm,
Christian Königf7da30d2016-09-28 12:03:04 +0200511 struct amdgpu_bo_va *bo_va,
Christian König2ffdaaf2017-01-27 15:58:43 +0100512 struct list_head *list,
Christian Königf7da30d2016-09-28 12:03:04 +0200513 uint32_t operation)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400514{
Christian König3f3333f2017-08-03 14:02:13 +0200515 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400516
Christian König3f3333f2017-08-03 14:02:13 +0200517 if (!amdgpu_vm_ready(vm))
518 return;
Chunming Zhoue410b5c2015-12-07 15:02:52 +0800519
Christian König194d2162016-10-12 15:13:52 +0200520 r = amdgpu_vm_update_directories(adev, vm);
Chunming Zhou43c27fb2015-11-12 15:33:09 +0800521 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100522 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100524 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400525 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100526 goto error;
monk.liu194a3362015-07-22 13:29:28 +0800527
Christian König80f95c52017-03-13 10:13:39 +0100528 if (operation == AMDGPU_VA_OP_MAP ||
529 operation == AMDGPU_VA_OP_REPLACE)
Flora Cui05dcb5c2016-09-22 11:34:47 +0800530 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400531
Christian König2ffdaaf2017-01-27 15:58:43 +0100532error:
Christian König68fdd3d2015-06-16 14:50:02 +0200533 if (r && r != -ERESTARTSYS)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
535}
536
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400537int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
538 struct drm_file *filp)
539{
Junwei Zhangb85891b2017-01-16 13:59:01 +0800540 const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
541 AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
Alex Xie66e02bc2017-02-14 12:04:52 -0500542 AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800543 const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
544 AMDGPU_VM_PAGE_PRT;
545
Christian König34b5f6a2015-06-08 15:03:00 +0200546 struct drm_amdgpu_gem_va *args = data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400547 struct drm_gem_object *gobj;
548 struct amdgpu_device *adev = dev->dev_private;
549 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Christian König765e7fb2016-09-15 15:06:50 +0200550 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400551 struct amdgpu_bo_va *bo_va;
Christian Königb88c8792016-09-28 16:33:01 +0200552 struct amdgpu_bo_list_entry vm_pd;
553 struct ttm_validate_buffer tv;
Chunming Zhou49b02b12015-11-13 14:18:38 +0800554 struct ww_acquire_ctx ticket;
Christian Könige1eb899b42017-08-25 09:14:43 +0200555 struct list_head list, duplicates;
Alex Xie54635452017-02-14 12:22:57 -0500556 uint64_t va_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400557 int r = 0;
558
Christian König34b5f6a2015-06-08 15:03:00 +0200559 if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560 dev_err(&dev->pdev->dev,
561 "va_address 0x%lX is in reserved area 0x%X\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200562 (unsigned long)args->va_address,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400563 AMDGPU_VA_RESERVED_SIZE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400564 return -EINVAL;
565 }
566
Junwei Zhangb85891b2017-01-16 13:59:01 +0800567 if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
568 dev_err(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
569 args->flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400570 return -EINVAL;
571 }
572
Christian König34b5f6a2015-06-08 15:03:00 +0200573 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400574 case AMDGPU_VA_OP_MAP:
575 case AMDGPU_VA_OP_UNMAP:
Christian Königdc54d3d2017-03-13 10:13:38 +0100576 case AMDGPU_VA_OP_CLEAR:
Christian König80f95c52017-03-13 10:13:39 +0100577 case AMDGPU_VA_OP_REPLACE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400578 break;
579 default:
580 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200581 args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400582 return -EINVAL;
583 }
Chunming Zhouf1892132017-05-15 16:48:27 +0800584 if ((args->operation == AMDGPU_VA_OP_MAP) ||
585 (args->operation == AMDGPU_VA_OP_REPLACE)) {
586 if (amdgpu_kms_vram_lost(adev, fpriv))
587 return -ENODEV;
588 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400589
Chunming Zhou49b02b12015-11-13 14:18:38 +0800590 INIT_LIST_HEAD(&list);
Christian Könige1eb899b42017-08-25 09:14:43 +0200591 INIT_LIST_HEAD(&duplicates);
Christian Königdc54d3d2017-03-13 10:13:38 +0100592 if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
593 !(args->flags & AMDGPU_VM_PAGE_PRT)) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800594 gobj = drm_gem_object_lookup(filp, args->handle);
595 if (gobj == NULL)
596 return -ENOENT;
597 abo = gem_to_amdgpu_bo(gobj);
598 tv.bo = &abo->tbo;
599 tv.shared = false;
600 list_add(&tv.head, &list);
601 } else {
602 gobj = NULL;
603 abo = NULL;
604 }
Chunming Zhou49b02b12015-11-13 14:18:38 +0800605
Christian Königb88c8792016-09-28 16:33:01 +0200606 amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
Christian Königb5a5ec52016-03-08 17:47:46 +0100607
Christian Könige1eb899b42017-08-25 09:14:43 +0200608 r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800609 if (r)
610 goto error_unref;
Christian König34b5f6a2015-06-08 15:03:00 +0200611
Junwei Zhangb85891b2017-01-16 13:59:01 +0800612 if (abo) {
613 bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
614 if (!bo_va) {
615 r = -ENOENT;
616 goto error_backoff;
617 }
Christian Königdc54d3d2017-03-13 10:13:38 +0100618 } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800619 bo_va = fpriv->prt_va;
Christian Königdc54d3d2017-03-13 10:13:38 +0100620 } else {
621 bo_va = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400622 }
623
Christian König34b5f6a2015-06-08 15:03:00 +0200624 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625 case AMDGPU_VA_OP_MAP:
Christian Königec681542017-08-01 10:51:43 +0200626 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
Christian König663e4572017-03-13 10:13:37 +0100627 args->map_size);
628 if (r)
629 goto error_backoff;
Alex Xie54635452017-02-14 12:22:57 -0500630
Christian König663e4572017-03-13 10:13:37 +0100631 va_flags = amdgpu_vm_get_pte_flags(adev, args->flags);
Christian König34b5f6a2015-06-08 15:03:00 +0200632 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
633 args->offset_in_bo, args->map_size,
Christian König9f7eb532015-05-18 16:05:57 +0200634 va_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400635 break;
636 case AMDGPU_VA_OP_UNMAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200637 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400638 break;
Christian Königdc54d3d2017-03-13 10:13:38 +0100639
640 case AMDGPU_VA_OP_CLEAR:
641 r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
642 args->va_address,
643 args->map_size);
644 break;
Christian König80f95c52017-03-13 10:13:39 +0100645 case AMDGPU_VA_OP_REPLACE:
Christian Königec681542017-08-01 10:51:43 +0200646 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
Christian König80f95c52017-03-13 10:13:39 +0100647 args->map_size);
648 if (r)
649 goto error_backoff;
650
651 va_flags = amdgpu_vm_get_pte_flags(adev, args->flags);
652 r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
653 args->offset_in_bo, args->map_size,
654 va_flags);
655 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400656 default:
657 break;
658 }
Junwei Zhangb85891b2017-01-16 13:59:01 +0800659 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
Christian Königdc54d3d2017-03-13 10:13:38 +0100660 amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, &list,
661 args->operation);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800662
663error_backoff:
Christian König2ffdaaf2017-01-27 15:58:43 +0100664 ttm_eu_backoff_reservation(&ticket, &list);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800665
Junwei Zhangb85891b2017-01-16 13:59:01 +0800666error_unref:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300667 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400668 return r;
669}
670
671int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
672 struct drm_file *filp)
673{
Christian Könige1eb899b42017-08-25 09:14:43 +0200674 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400675 struct drm_amdgpu_gem_op *args = data;
676 struct drm_gem_object *gobj;
677 struct amdgpu_bo *robj;
678 int r;
679
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100680 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400681 if (gobj == NULL) {
682 return -ENOENT;
683 }
684 robj = gem_to_amdgpu_bo(gobj);
685
686 r = amdgpu_bo_reserve(robj, false);
687 if (unlikely(r))
688 goto out;
689
690 switch (args->op) {
691 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
692 struct drm_amdgpu_gem_create_in info;
Christian König7ecc2452017-07-26 17:02:52 +0200693 void __user *out = u64_to_user_ptr(args->value);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694
695 info.bo_size = robj->gem_base.size;
696 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
Kent Russell6d7d9c52017-08-08 07:58:01 -0400697 info.domains = robj->preferred_domains;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698 info.domain_flags = robj->flags;
Christian König4c28fb02015-08-28 17:27:54 +0200699 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700 if (copy_to_user(out, &info, sizeof(info)))
701 r = -EFAULT;
702 break;
703 }
Marek Olšákd8f65a22015-05-27 14:30:38 +0200704 case AMDGPU_GEM_OP_SET_PLACEMENT:
Christopher James Halse Rogers803d89a2017-04-03 13:31:22 +1000705 if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
706 r = -EINVAL;
707 amdgpu_bo_unreserve(robj);
708 break;
709 }
Christian Königcc325d12016-02-08 11:08:35 +0100710 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711 r = -EPERM;
Christian König4c28fb02015-08-28 17:27:54 +0200712 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713 break;
714 }
Kent Russell6d7d9c52017-08-08 07:58:01 -0400715 robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
Christian König1ea863f2015-12-18 22:13:12 +0100716 AMDGPU_GEM_DOMAIN_GTT |
717 AMDGPU_GEM_DOMAIN_CPU);
Kent Russell6d7d9c52017-08-08 07:58:01 -0400718 robj->allowed_domains = robj->preferred_domains;
Christian König1ea863f2015-12-18 22:13:12 +0100719 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
720 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
721
Christian Könige1eb899b42017-08-25 09:14:43 +0200722 if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
723 amdgpu_vm_bo_invalidate(adev, robj, true);
724
Christian König4c28fb02015-08-28 17:27:54 +0200725 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400726 break;
727 default:
Christian König4c28fb02015-08-28 17:27:54 +0200728 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729 r = -EINVAL;
730 }
731
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732out:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300733 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734 return r;
735}
736
737int amdgpu_mode_dumb_create(struct drm_file *file_priv,
738 struct drm_device *dev,
739 struct drm_mode_create_dumb *args)
740{
741 struct amdgpu_device *adev = dev->dev_private;
742 struct drm_gem_object *gobj;
743 uint32_t handle;
744 int r;
745
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300746 args->pitch = amdgpu_align_pitch(adev, args->width,
747 DIV_ROUND_UP(args->bpp, 8), 0);
Dan Carpenter54ef0b52015-09-23 14:00:59 +0300748 args->size = (u64)args->pitch * args->height;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400749 args->size = ALIGN(args->size, PAGE_SIZE);
750
751 r = amdgpu_gem_object_create(adev, args->size, 0,
752 AMDGPU_GEM_DOMAIN_VRAM,
Alex Deucher857d9132015-08-27 00:14:16 -0400753 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian Könige1eb899b42017-08-25 09:14:43 +0200754 false, NULL, &gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400755 if (r)
756 return -ENOMEM;
757
758 r = drm_gem_handle_create(file_priv, gobj, &handle);
759 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300760 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400761 if (r) {
762 return r;
763 }
764 args->handle = handle;
765 return 0;
766}
767
768#if defined(CONFIG_DEBUG_FS)
Christian König7ea23562016-02-15 15:23:00 +0100769static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
770{
771 struct drm_gem_object *gobj = ptr;
772 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
773 struct seq_file *m = data;
774
775 unsigned domain;
776 const char *placement;
777 unsigned pin_count;
Christian Königb8e0e6e2017-06-26 15:19:30 +0200778 uint64_t offset;
Christian König7ea23562016-02-15 15:23:00 +0100779
780 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
781 switch (domain) {
782 case AMDGPU_GEM_DOMAIN_VRAM:
783 placement = "VRAM";
784 break;
785 case AMDGPU_GEM_DOMAIN_GTT:
786 placement = " GTT";
787 break;
788 case AMDGPU_GEM_DOMAIN_CPU:
789 default:
790 placement = " CPU";
791 break;
792 }
Christian Königb8e0e6e2017-06-26 15:19:30 +0200793 seq_printf(m, "\t0x%08x: %12ld byte %s",
794 id, amdgpu_bo_size(bo), placement);
795
796 offset = ACCESS_ONCE(bo->tbo.mem.start);
797 if (offset != AMDGPU_BO_INVALID_OFFSET)
798 seq_printf(m, " @ 0x%010Lx", offset);
Christian König7ea23562016-02-15 15:23:00 +0100799
800 pin_count = ACCESS_ONCE(bo->pin_count);
801 if (pin_count)
802 seq_printf(m, " pin count %d", pin_count);
803 seq_printf(m, "\n");
804
805 return 0;
806}
807
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400808static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
809{
810 struct drm_info_node *node = (struct drm_info_node *)m->private;
811 struct drm_device *dev = node->minor->dev;
Christian König7ea23562016-02-15 15:23:00 +0100812 struct drm_file *file;
813 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400814
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200815 r = mutex_lock_interruptible(&dev->filelist_mutex);
Christian König7ea23562016-02-15 15:23:00 +0100816 if (r)
817 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400818
Christian König7ea23562016-02-15 15:23:00 +0100819 list_for_each_entry(file, &dev->filelist, lhead) {
820 struct task_struct *task;
Christian Königb22e3ce2016-02-15 12:41:37 +0100821
Christian König7ea23562016-02-15 15:23:00 +0100822 /*
823 * Although we have a valid reference on file->pid, that does
824 * not guarantee that the task_struct who called get_pid() is
825 * still alive (e.g. get_pid(current) => fork() => exit()).
826 * Therefore, we need to protect this ->comm access using RCU.
827 */
828 rcu_read_lock();
829 task = pid_task(file->pid, PIDTYPE_PID);
830 seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
831 task ? task->comm : "<unknown>");
832 rcu_read_unlock();
833
834 spin_lock(&file->table_lock);
835 idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
836 spin_unlock(&file->table_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400837 }
Christian König7ea23562016-02-15 15:23:00 +0100838
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200839 mutex_unlock(&dev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400840 return 0;
841}
842
Nils Wallménius06ab6832016-05-02 12:46:15 -0400843static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
845};
846#endif
847
848int amdgpu_gem_debugfs_init(struct amdgpu_device *adev)
849{
850#if defined(CONFIG_DEBUG_FS)
851 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
852#endif
853 return 0;
854}