blob: 21e99366cab3de933968edf8298b8f69c2eb4efd [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 |
Andres Rodriguez177ae092017-09-15 20:44:06 -0400215 AMDGPU_GEM_CREATE_VM_ALWAYS_VALID |
216 AMDGPU_GEM_CREATE_EXPLICIT_SYNC))
217
Christian Königa022c542017-05-08 15:14:54 +0200218 return -EINVAL;
219
Alex Deucher834e0f82017-03-08 17:40:17 -0500220 /* reject invalid gem domains */
221 if (args->in.domains & ~(AMDGPU_GEM_DOMAIN_CPU |
222 AMDGPU_GEM_DOMAIN_GTT |
223 AMDGPU_GEM_DOMAIN_VRAM |
224 AMDGPU_GEM_DOMAIN_GDS |
225 AMDGPU_GEM_DOMAIN_GWS |
Christian Königa022c542017-05-08 15:14:54 +0200226 AMDGPU_GEM_DOMAIN_OA))
227 return -EINVAL;
Alex Deucher834e0f82017-03-08 17:40:17 -0500228
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400229 /* create a gem object to contain this object in */
230 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
231 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
Christian König6ac7def2017-08-23 20:11:25 +0200232 flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400233 if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
234 size = size << AMDGPU_GDS_SHIFT;
235 else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
236 size = size << AMDGPU_GWS_SHIFT;
237 else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
238 size = size << AMDGPU_OA_SHIFT;
Christian Königa022c542017-05-08 15:14:54 +0200239 else
240 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400241 }
242 size = roundup(size, PAGE_SIZE);
243
Christian Könige1eb899b42017-08-25 09:14:43 +0200244 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
245 r = amdgpu_bo_reserve(vm->root.base.bo, false);
246 if (r)
247 return r;
248
249 resv = vm->root.base.bo->tbo.resv;
250 }
251
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400252 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
253 (u32)(0xffffffff & args->in.domains),
Christian Könige1eb899b42017-08-25 09:14:43 +0200254 flags, false, resv, &gobj);
255 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
256 if (!r) {
257 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
258
259 abo->parent = amdgpu_bo_ref(vm->root.base.bo);
260 }
261 amdgpu_bo_unreserve(vm->root.base.bo);
262 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400263 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200264 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400265
266 r = drm_gem_handle_create(filp, gobj, &handle);
267 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300268 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400269 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200270 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400271
272 memset(args, 0, sizeof(*args));
273 args->out.handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400274 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400275}
276
277int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
278 struct drm_file *filp)
279{
280 struct amdgpu_device *adev = dev->dev_private;
281 struct drm_amdgpu_gem_userptr *args = data;
282 struct drm_gem_object *gobj;
283 struct amdgpu_bo *bo;
284 uint32_t handle;
285 int r;
286
287 if (offset_in_page(args->addr | args->size))
288 return -EINVAL;
289
290 /* reject unknown flag values */
291 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
292 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
293 AMDGPU_GEM_USERPTR_REGISTER))
294 return -EINVAL;
295
Christian König358c2582016-03-11 15:29:27 +0100296 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
297 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400298
Christian König358c2582016-03-11 15:29:27 +0100299 /* if we want to write to it we must install a MMU notifier */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400300 return -EACCES;
301 }
302
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400303 /* create a gem object to contain this object in */
Christian Könige1eb899b42017-08-25 09:14:43 +0200304 r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
305 0, 0, NULL, &gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400306 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200307 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400308
309 bo = gem_to_amdgpu_bo(gobj);
Kent Russell6d7d9c52017-08-08 07:58:01 -0400310 bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
Christian König1ea863f2015-12-18 22:13:12 +0100311 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400312 r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
313 if (r)
314 goto release_object;
315
316 if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
317 r = amdgpu_mn_register(bo, args->addr);
318 if (r)
319 goto release_object;
320 }
321
322 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
Christian König2f568db2016-02-23 12:36:59 +0100323 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;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400337 }
338
339 r = drm_gem_handle_create(filp, gobj, &handle);
340 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300341 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400342 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200343 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400344
345 args->handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400346 return 0;
347
Christian König2f568db2016-02-23 12:36:59 +0100348free_pages:
349 release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages, false);
350
351unlock_mmap_sem:
352 up_read(&current->mm->mmap_sem);
353
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400354release_object:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300355 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400356
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357 return r;
358}
359
360int amdgpu_mode_dumb_mmap(struct drm_file *filp,
361 struct drm_device *dev,
362 uint32_t handle, uint64_t *offset_p)
363{
364 struct drm_gem_object *gobj;
365 struct amdgpu_bo *robj;
366
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100367 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400368 if (gobj == NULL) {
369 return -ENOENT;
370 }
371 robj = gem_to_amdgpu_bo(gobj);
Christian Königcc325d12016-02-08 11:08:35 +0100372 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
Christian König271c8122015-05-13 14:30:53 +0200373 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300374 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400375 return -EPERM;
376 }
377 *offset_p = amdgpu_bo_mmap_offset(robj);
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300378 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400379 return 0;
380}
381
382int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
383 struct drm_file *filp)
384{
385 union drm_amdgpu_gem_mmap *args = data;
386 uint32_t handle = args->in.handle;
387 memset(args, 0, sizeof(*args));
388 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
389}
390
391/**
392 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
393 *
394 * @timeout_ns: timeout in ns
395 *
396 * Calculate the timeout in jiffies from an absolute timeout in ns.
397 */
398unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
399{
400 unsigned long timeout_jiffies;
401 ktime_t timeout;
402
403 /* clamp timeout if it's to large */
404 if (((int64_t)timeout_ns) < 0)
405 return MAX_SCHEDULE_TIMEOUT;
406
Christian König0f117702015-07-08 16:58:48 +0200407 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400408 if (ktime_to_ns(timeout) < 0)
409 return 0;
410
411 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
412 /* clamp timeout to avoid unsigned-> signed overflow */
413 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
414 return MAX_SCHEDULE_TIMEOUT - 1;
415
416 return timeout_jiffies;
417}
418
419int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
420 struct drm_file *filp)
421{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400422 union drm_amdgpu_gem_wait_idle *args = data;
423 struct drm_gem_object *gobj;
424 struct amdgpu_bo *robj;
425 uint32_t handle = args->in.handle;
426 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
427 int r = 0;
428 long ret;
429
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100430 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400431 if (gobj == NULL) {
432 return -ENOENT;
433 }
434 robj = gem_to_amdgpu_bo(gobj);
Chris Wilson0fea2ed2016-08-29 08:08:24 +0100435 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
436 timeout);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400437
438 /* ret == 0 means not signaled,
439 * ret > 0 means signaled
440 * ret < 0 means interrupted before timeout
441 */
442 if (ret >= 0) {
443 memset(args, 0, sizeof(*args));
444 args->out.status = (ret == 0);
445 } else
446 r = ret;
447
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300448 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400449 return r;
450}
451
452int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
453 struct drm_file *filp)
454{
455 struct drm_amdgpu_gem_metadata *args = data;
456 struct drm_gem_object *gobj;
457 struct amdgpu_bo *robj;
458 int r = -1;
459
460 DRM_DEBUG("%d \n", args->handle);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100461 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400462 if (gobj == NULL)
463 return -ENOENT;
464 robj = gem_to_amdgpu_bo(gobj);
465
466 r = amdgpu_bo_reserve(robj, false);
467 if (unlikely(r != 0))
468 goto out;
469
470 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
471 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
472 r = amdgpu_bo_get_metadata(robj, args->data.data,
473 sizeof(args->data.data),
474 &args->data.data_size_bytes,
475 &args->data.flags);
476 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
Dan Carpenter0913eab2015-09-23 14:00:35 +0300477 if (args->data.data_size_bytes > sizeof(args->data.data)) {
478 r = -EINVAL;
479 goto unreserve;
480 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400481 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
482 if (!r)
483 r = amdgpu_bo_set_metadata(robj, args->data.data,
484 args->data.data_size_bytes,
485 args->data.flags);
486 }
487
Dan Carpenter0913eab2015-09-23 14:00:35 +0300488unreserve:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400489 amdgpu_bo_unreserve(robj);
490out:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300491 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400492 return r;
493}
494
495/**
496 * amdgpu_gem_va_update_vm -update the bo_va in its VM
497 *
498 * @adev: amdgpu_device pointer
Christian Königdc54d3d2017-03-13 10:13:38 +0100499 * @vm: vm to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500 * @bo_va: bo_va to update
Christian König2ffdaaf2017-01-27 15:58:43 +0100501 * @list: validation list
Christian Königdc54d3d2017-03-13 10:13:38 +0100502 * @operation: map, unmap or clear
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400503 *
Christian König2ffdaaf2017-01-27 15:58:43 +0100504 * Update the bo_va directly after setting its address. Errors are not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400505 * vital here, so they are not reported back to userspace.
506 */
507static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
Christian Königdc54d3d2017-03-13 10:13:38 +0100508 struct amdgpu_vm *vm,
Christian Königf7da30d2016-09-28 12:03:04 +0200509 struct amdgpu_bo_va *bo_va,
Christian König2ffdaaf2017-01-27 15:58:43 +0100510 struct list_head *list,
Christian Königf7da30d2016-09-28 12:03:04 +0200511 uint32_t operation)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400512{
Christian König3f3333f2017-08-03 14:02:13 +0200513 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400514
Christian König3f3333f2017-08-03 14:02:13 +0200515 if (!amdgpu_vm_ready(vm))
516 return;
Chunming Zhoue410b5c2015-12-07 15:02:52 +0800517
Christian König194d2162016-10-12 15:13:52 +0200518 r = amdgpu_vm_update_directories(adev, vm);
Chunming Zhou43c27fb2015-11-12 15:33:09 +0800519 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100520 goto error;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400521
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100522 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100524 goto error;
monk.liu194a3362015-07-22 13:29:28 +0800525
Christian König80f95c52017-03-13 10:13:39 +0100526 if (operation == AMDGPU_VA_OP_MAP ||
527 operation == AMDGPU_VA_OP_REPLACE)
Flora Cui05dcb5c2016-09-22 11:34:47 +0800528 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529
Christian König2ffdaaf2017-01-27 15:58:43 +0100530error:
Christian König68fdd3d2015-06-16 14:50:02 +0200531 if (r && r != -ERESTARTSYS)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400532 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
533}
534
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400535int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
536 struct drm_file *filp)
537{
Junwei Zhangb85891b2017-01-16 13:59:01 +0800538 const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
539 AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
Alex Xie66e02bc2017-02-14 12:04:52 -0500540 AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800541 const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
542 AMDGPU_VM_PAGE_PRT;
543
Christian König34b5f6a2015-06-08 15:03:00 +0200544 struct drm_amdgpu_gem_va *args = data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400545 struct drm_gem_object *gobj;
546 struct amdgpu_device *adev = dev->dev_private;
547 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Christian König765e7fb2016-09-15 15:06:50 +0200548 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400549 struct amdgpu_bo_va *bo_va;
Christian Königb88c8792016-09-28 16:33:01 +0200550 struct amdgpu_bo_list_entry vm_pd;
551 struct ttm_validate_buffer tv;
Chunming Zhou49b02b12015-11-13 14:18:38 +0800552 struct ww_acquire_ctx ticket;
Christian Könige1eb899b42017-08-25 09:14:43 +0200553 struct list_head list, duplicates;
Alex Xie54635452017-02-14 12:22:57 -0500554 uint64_t va_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400555 int r = 0;
556
Christian König34b5f6a2015-06-08 15:03:00 +0200557 if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400558 dev_err(&dev->pdev->dev,
559 "va_address 0x%lX is in reserved area 0x%X\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200560 (unsigned long)args->va_address,
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400561 AMDGPU_VA_RESERVED_SIZE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400562 return -EINVAL;
563 }
564
Junwei Zhangb85891b2017-01-16 13:59:01 +0800565 if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
566 dev_err(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
567 args->flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400568 return -EINVAL;
569 }
570
Christian König34b5f6a2015-06-08 15:03:00 +0200571 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400572 case AMDGPU_VA_OP_MAP:
573 case AMDGPU_VA_OP_UNMAP:
Christian Königdc54d3d2017-03-13 10:13:38 +0100574 case AMDGPU_VA_OP_CLEAR:
Christian König80f95c52017-03-13 10:13:39 +0100575 case AMDGPU_VA_OP_REPLACE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400576 break;
577 default:
578 dev_err(&dev->pdev->dev, "unsupported operation %d\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200579 args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580 return -EINVAL;
581 }
Chunming Zhouf1892132017-05-15 16:48:27 +0800582 if ((args->operation == AMDGPU_VA_OP_MAP) ||
583 (args->operation == AMDGPU_VA_OP_REPLACE)) {
584 if (amdgpu_kms_vram_lost(adev, fpriv))
585 return -ENODEV;
586 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587
Chunming Zhou49b02b12015-11-13 14:18:38 +0800588 INIT_LIST_HEAD(&list);
Christian Könige1eb899b42017-08-25 09:14:43 +0200589 INIT_LIST_HEAD(&duplicates);
Christian Königdc54d3d2017-03-13 10:13:38 +0100590 if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
591 !(args->flags & AMDGPU_VM_PAGE_PRT)) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800592 gobj = drm_gem_object_lookup(filp, args->handle);
593 if (gobj == NULL)
594 return -ENOENT;
595 abo = gem_to_amdgpu_bo(gobj);
596 tv.bo = &abo->tbo;
597 tv.shared = false;
598 list_add(&tv.head, &list);
599 } else {
600 gobj = NULL;
601 abo = NULL;
602 }
Chunming Zhou49b02b12015-11-13 14:18:38 +0800603
Christian Königb88c8792016-09-28 16:33:01 +0200604 amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
Christian Königb5a5ec52016-03-08 17:47:46 +0100605
Christian Könige1eb899b42017-08-25 09:14:43 +0200606 r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800607 if (r)
608 goto error_unref;
Christian König34b5f6a2015-06-08 15:03:00 +0200609
Junwei Zhangb85891b2017-01-16 13:59:01 +0800610 if (abo) {
611 bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
612 if (!bo_va) {
613 r = -ENOENT;
614 goto error_backoff;
615 }
Christian Königdc54d3d2017-03-13 10:13:38 +0100616 } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800617 bo_va = fpriv->prt_va;
Christian Königdc54d3d2017-03-13 10:13:38 +0100618 } else {
619 bo_va = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400620 }
621
Christian König34b5f6a2015-06-08 15:03:00 +0200622 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623 case AMDGPU_VA_OP_MAP:
Christian Königec681542017-08-01 10:51:43 +0200624 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
Christian König663e4572017-03-13 10:13:37 +0100625 args->map_size);
626 if (r)
627 goto error_backoff;
Alex Xie54635452017-02-14 12:22:57 -0500628
Christian König663e4572017-03-13 10:13:37 +0100629 va_flags = amdgpu_vm_get_pte_flags(adev, args->flags);
Christian König34b5f6a2015-06-08 15:03:00 +0200630 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
631 args->offset_in_bo, args->map_size,
Christian König9f7eb532015-05-18 16:05:57 +0200632 va_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400633 break;
634 case AMDGPU_VA_OP_UNMAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200635 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400636 break;
Christian Königdc54d3d2017-03-13 10:13:38 +0100637
638 case AMDGPU_VA_OP_CLEAR:
639 r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
640 args->va_address,
641 args->map_size);
642 break;
Christian König80f95c52017-03-13 10:13:39 +0100643 case AMDGPU_VA_OP_REPLACE:
Christian Königec681542017-08-01 10:51:43 +0200644 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
Christian König80f95c52017-03-13 10:13:39 +0100645 args->map_size);
646 if (r)
647 goto error_backoff;
648
649 va_flags = amdgpu_vm_get_pte_flags(adev, args->flags);
650 r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
651 args->offset_in_bo, args->map_size,
652 va_flags);
653 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400654 default:
655 break;
656 }
Junwei Zhangb85891b2017-01-16 13:59:01 +0800657 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
Christian Königdc54d3d2017-03-13 10:13:38 +0100658 amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, &list,
659 args->operation);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800660
661error_backoff:
Christian König2ffdaaf2017-01-27 15:58:43 +0100662 ttm_eu_backoff_reservation(&ticket, &list);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800663
Junwei Zhangb85891b2017-01-16 13:59:01 +0800664error_unref:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300665 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400666 return r;
667}
668
669int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
670 struct drm_file *filp)
671{
Christian Könige1eb899b42017-08-25 09:14:43 +0200672 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400673 struct drm_amdgpu_gem_op *args = data;
674 struct drm_gem_object *gobj;
675 struct amdgpu_bo *robj;
676 int r;
677
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100678 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400679 if (gobj == NULL) {
680 return -ENOENT;
681 }
682 robj = gem_to_amdgpu_bo(gobj);
683
684 r = amdgpu_bo_reserve(robj, false);
685 if (unlikely(r))
686 goto out;
687
688 switch (args->op) {
689 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
690 struct drm_amdgpu_gem_create_in info;
Christian König7ecc2452017-07-26 17:02:52 +0200691 void __user *out = u64_to_user_ptr(args->value);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400692
693 info.bo_size = robj->gem_base.size;
694 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
Kent Russell6d7d9c52017-08-08 07:58:01 -0400695 info.domains = robj->preferred_domains;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696 info.domain_flags = robj->flags;
Christian König4c28fb02015-08-28 17:27:54 +0200697 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698 if (copy_to_user(out, &info, sizeof(info)))
699 r = -EFAULT;
700 break;
701 }
Marek Olšákd8f65a22015-05-27 14:30:38 +0200702 case AMDGPU_GEM_OP_SET_PLACEMENT:
Christopher James Halse Rogers803d89a2017-04-03 13:31:22 +1000703 if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
704 r = -EINVAL;
705 amdgpu_bo_unreserve(robj);
706 break;
707 }
Christian Königcc325d12016-02-08 11:08:35 +0100708 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709 r = -EPERM;
Christian König4c28fb02015-08-28 17:27:54 +0200710 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711 break;
712 }
Kent Russell6d7d9c52017-08-08 07:58:01 -0400713 robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
Christian König1ea863f2015-12-18 22:13:12 +0100714 AMDGPU_GEM_DOMAIN_GTT |
715 AMDGPU_GEM_DOMAIN_CPU);
Kent Russell6d7d9c52017-08-08 07:58:01 -0400716 robj->allowed_domains = robj->preferred_domains;
Christian König1ea863f2015-12-18 22:13:12 +0100717 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
718 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
719
Christian Könige1eb899b42017-08-25 09:14:43 +0200720 if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
721 amdgpu_vm_bo_invalidate(adev, robj, true);
722
Christian König4c28fb02015-08-28 17:27:54 +0200723 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400724 break;
725 default:
Christian König4c28fb02015-08-28 17:27:54 +0200726 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400727 r = -EINVAL;
728 }
729
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730out:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300731 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732 return r;
733}
734
735int amdgpu_mode_dumb_create(struct drm_file *file_priv,
736 struct drm_device *dev,
737 struct drm_mode_create_dumb *args)
738{
739 struct amdgpu_device *adev = dev->dev_private;
740 struct drm_gem_object *gobj;
741 uint32_t handle;
742 int r;
743
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300744 args->pitch = amdgpu_align_pitch(adev, args->width,
745 DIV_ROUND_UP(args->bpp, 8), 0);
Dan Carpenter54ef0b52015-09-23 14:00:59 +0300746 args->size = (u64)args->pitch * args->height;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400747 args->size = ALIGN(args->size, PAGE_SIZE);
748
749 r = amdgpu_gem_object_create(adev, args->size, 0,
750 AMDGPU_GEM_DOMAIN_VRAM,
Alex Deucher857d9132015-08-27 00:14:16 -0400751 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian Könige1eb899b42017-08-25 09:14:43 +0200752 false, NULL, &gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400753 if (r)
754 return -ENOMEM;
755
756 r = drm_gem_handle_create(file_priv, gobj, &handle);
757 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300758 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400759 if (r) {
760 return r;
761 }
762 args->handle = handle;
763 return 0;
764}
765
766#if defined(CONFIG_DEBUG_FS)
Christian König7ea23562016-02-15 15:23:00 +0100767static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
768{
769 struct drm_gem_object *gobj = ptr;
770 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
771 struct seq_file *m = data;
772
773 unsigned domain;
774 const char *placement;
775 unsigned pin_count;
Christian Königb8e0e6e2017-06-26 15:19:30 +0200776 uint64_t offset;
Christian König7ea23562016-02-15 15:23:00 +0100777
778 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
779 switch (domain) {
780 case AMDGPU_GEM_DOMAIN_VRAM:
781 placement = "VRAM";
782 break;
783 case AMDGPU_GEM_DOMAIN_GTT:
784 placement = " GTT";
785 break;
786 case AMDGPU_GEM_DOMAIN_CPU:
787 default:
788 placement = " CPU";
789 break;
790 }
Christian Königb8e0e6e2017-06-26 15:19:30 +0200791 seq_printf(m, "\t0x%08x: %12ld byte %s",
792 id, amdgpu_bo_size(bo), placement);
793
794 offset = ACCESS_ONCE(bo->tbo.mem.start);
795 if (offset != AMDGPU_BO_INVALID_OFFSET)
796 seq_printf(m, " @ 0x%010Lx", offset);
Christian König7ea23562016-02-15 15:23:00 +0100797
798 pin_count = ACCESS_ONCE(bo->pin_count);
799 if (pin_count)
800 seq_printf(m, " pin count %d", pin_count);
801 seq_printf(m, "\n");
802
803 return 0;
804}
805
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400806static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
807{
808 struct drm_info_node *node = (struct drm_info_node *)m->private;
809 struct drm_device *dev = node->minor->dev;
Christian König7ea23562016-02-15 15:23:00 +0100810 struct drm_file *file;
811 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400812
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200813 r = mutex_lock_interruptible(&dev->filelist_mutex);
Christian König7ea23562016-02-15 15:23:00 +0100814 if (r)
815 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400816
Christian König7ea23562016-02-15 15:23:00 +0100817 list_for_each_entry(file, &dev->filelist, lhead) {
818 struct task_struct *task;
Christian Königb22e3ce2016-02-15 12:41:37 +0100819
Christian König7ea23562016-02-15 15:23:00 +0100820 /*
821 * Although we have a valid reference on file->pid, that does
822 * not guarantee that the task_struct who called get_pid() is
823 * still alive (e.g. get_pid(current) => fork() => exit()).
824 * Therefore, we need to protect this ->comm access using RCU.
825 */
826 rcu_read_lock();
827 task = pid_task(file->pid, PIDTYPE_PID);
828 seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
829 task ? task->comm : "<unknown>");
830 rcu_read_unlock();
831
832 spin_lock(&file->table_lock);
833 idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
834 spin_unlock(&file->table_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400835 }
Christian König7ea23562016-02-15 15:23:00 +0100836
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200837 mutex_unlock(&dev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400838 return 0;
839}
840
Nils Wallménius06ab6832016-05-02 12:46:15 -0400841static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400842 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
843};
844#endif
845
846int amdgpu_gem_debugfs_init(struct amdgpu_device *adev)
847{
848#if defined(CONFIG_DEBUG_FS)
849 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
850#endif
851 return 0;
852}