blob: ca6c931dabfab9c3248dc5a0460b8bdd15f1011b [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,
46 u64 flags, bool kernel,
47 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;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040051 int r;
52
53 *obj = NULL;
54 /* At least align on page size */
55 if (alignment < PAGE_SIZE) {
56 alignment = PAGE_SIZE;
57 }
58
Alex Deucherd38ceaf2015-04-20 16:55:21 -040059retry:
Christian König72d76682015-09-03 17:34:59 +020060 r = amdgpu_bo_create(adev, size, alignment, kernel, initial_domain,
Christian Könige1eb899b42017-08-25 09:14:43 +020061 flags, NULL, resv, 0, &bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040062 if (r) {
63 if (r != -ERESTARTSYS) {
Roger He8e96e372017-11-10 20:00:30 +080064 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
65 flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
66 goto retry;
67 }
68
Alex Deucherd38ceaf2015-04-20 16:55:21 -040069 if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
70 initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
71 goto retry;
72 }
Michel Dänzer299c7762017-11-15 11:37:23 +010073 DRM_DEBUG("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040074 size, initial_domain, alignment, r);
75 }
76 return r;
77 }
Christian Könige1eb899b42017-08-25 09:14:43 +020078 *obj = &bo->gem_base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040079
Alex Deucherd38ceaf2015-04-20 16:55:21 -040080 return 0;
81}
82
Christian König418aa0c2016-02-15 16:59:57 +010083void amdgpu_gem_force_release(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040084{
Christian König418aa0c2016-02-15 16:59:57 +010085 struct drm_device *ddev = adev->ddev;
86 struct drm_file *file;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040087
Daniel Vetter1d2ac402016-04-26 19:29:41 +020088 mutex_lock(&ddev->filelist_mutex);
Christian König418aa0c2016-02-15 16:59:57 +010089
90 list_for_each_entry(file, &ddev->filelist, lhead) {
91 struct drm_gem_object *gobj;
92 int handle;
93
94 WARN_ONCE(1, "Still active user space clients!\n");
95 spin_lock(&file->table_lock);
96 idr_for_each_entry(&file->object_idr, gobj, handle) {
97 WARN_ONCE(1, "And also active allocations!\n");
Cihangir Akturkf62facc2017-08-03 14:58:16 +030098 drm_gem_object_put_unlocked(gobj);
Christian König418aa0c2016-02-15 16:59:57 +010099 }
100 idr_destroy(&file->object_idr);
101 spin_unlock(&file->table_lock);
102 }
103
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200104 mutex_unlock(&ddev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105}
106
107/*
108 * Call from drm_gem_handle_create which appear in both new and open ioctl
109 * case.
110 */
Christian Königa7d64de2016-09-15 14:58:48 +0200111int amdgpu_gem_object_open(struct drm_gem_object *obj,
112 struct drm_file *file_priv)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113{
Christian König765e7fb2016-09-15 15:06:50 +0200114 struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
Christian Königa7d64de2016-09-15 14:58:48 +0200115 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
117 struct amdgpu_vm *vm = &fpriv->vm;
118 struct amdgpu_bo_va *bo_va;
Christian König4f5839c2017-08-29 16:07:31 +0200119 struct mm_struct *mm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120 int r;
Christian König4f5839c2017-08-29 16:07:31 +0200121
122 mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm);
123 if (mm && mm != current->mm)
124 return -EPERM;
125
Christian Könige1eb899b42017-08-25 09:14:43 +0200126 if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID &&
127 abo->tbo.resv != vm->root.base.bo->tbo.resv)
128 return -EPERM;
129
Christian König765e7fb2016-09-15 15:06:50 +0200130 r = amdgpu_bo_reserve(abo, false);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800131 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400132 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133
Christian König765e7fb2016-09-15 15:06:50 +0200134 bo_va = amdgpu_vm_bo_find(vm, abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135 if (!bo_va) {
Christian König765e7fb2016-09-15 15:06:50 +0200136 bo_va = amdgpu_vm_bo_add(adev, vm, abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400137 } else {
138 ++bo_va->ref_count;
139 }
Christian König765e7fb2016-09-15 15:06:50 +0200140 amdgpu_bo_unreserve(abo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141 return 0;
142}
143
144void amdgpu_gem_object_close(struct drm_gem_object *obj,
145 struct drm_file *file_priv)
146{
Christian Königb5a5ec52016-03-08 17:47:46 +0100147 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
Christian Königa7d64de2016-09-15 14:58:48 +0200148 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400149 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
150 struct amdgpu_vm *vm = &fpriv->vm;
Christian Königb5a5ec52016-03-08 17:47:46 +0100151
152 struct amdgpu_bo_list_entry vm_pd;
Christian Könige1eb899b42017-08-25 09:14:43 +0200153 struct list_head list, duplicates;
Christian Königb5a5ec52016-03-08 17:47:46 +0100154 struct ttm_validate_buffer tv;
155 struct ww_acquire_ctx ticket;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156 struct amdgpu_bo_va *bo_va;
157 int r;
Christian Königb5a5ec52016-03-08 17:47:46 +0100158
159 INIT_LIST_HEAD(&list);
Christian Könige1eb899b42017-08-25 09:14:43 +0200160 INIT_LIST_HEAD(&duplicates);
Christian Königb5a5ec52016-03-08 17:47:46 +0100161
162 tv.bo = &bo->tbo;
163 tv.shared = true;
164 list_add(&tv.head, &list);
165
166 amdgpu_vm_get_pd_bo(vm, &list, &vm_pd);
167
Christian Könige1eb899b42017-08-25 09:14:43 +0200168 r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400169 if (r) {
170 dev_err(adev->dev, "leaking bo va because "
171 "we fail to reserve bo (%d)\n", r);
172 return;
173 }
Christian Königb5a5ec52016-03-08 17:47:46 +0100174 bo_va = amdgpu_vm_bo_find(vm, bo);
Christian König5a0f3b52017-04-21 10:05:56 +0200175 if (bo_va && --bo_va->ref_count == 0) {
176 amdgpu_vm_bo_rmv(adev, bo_va);
177
Christian König3f3333f2017-08-03 14:02:13 +0200178 if (amdgpu_vm_ready(vm)) {
Christian König5a0f3b52017-04-21 10:05:56 +0200179 struct dma_fence *fence = NULL;
Nicolai Hähnle23e05632017-03-23 19:34:11 +0100180
181 r = amdgpu_vm_clear_freed(adev, vm, &fence);
182 if (unlikely(r)) {
183 dev_err(adev->dev, "failed to clear page "
184 "tables on GEM object close (%d)\n", r);
185 }
186
187 if (fence) {
188 amdgpu_bo_fence(bo, fence, true);
189 dma_fence_put(fence);
190 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400191 }
192 }
Christian Königb5a5ec52016-03-08 17:47:46 +0100193 ttm_eu_backoff_reservation(&ticket, &list);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400194}
195
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400196/*
197 * GEM ioctls.
198 */
199int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
200 struct drm_file *filp)
201{
202 struct amdgpu_device *adev = dev->dev_private;
Christian Könige1eb899b42017-08-25 09:14:43 +0200203 struct amdgpu_fpriv *fpriv = filp->driver_priv;
204 struct amdgpu_vm *vm = &fpriv->vm;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400205 union drm_amdgpu_gem_create *args = data;
Christian König6ac7def2017-08-23 20:11:25 +0200206 uint64_t flags = args->in.domain_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400207 uint64_t size = args->in.bo_size;
Christian Könige1eb899b42017-08-25 09:14:43 +0200208 struct reservation_object *resv = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400209 struct drm_gem_object *gobj;
210 uint32_t handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400211 int r;
212
Alex Deucher834e0f82017-03-08 17:40:17 -0500213 /* reject invalid gem flags */
Christian König6ac7def2017-08-23 20:11:25 +0200214 if (flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
215 AMDGPU_GEM_CREATE_NO_CPU_ACCESS |
216 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
Christian Könige1eb899b42017-08-25 09:14:43 +0200217 AMDGPU_GEM_CREATE_VRAM_CLEARED |
Andres Rodriguez177ae092017-09-15 20:44:06 -0400218 AMDGPU_GEM_CREATE_VM_ALWAYS_VALID |
219 AMDGPU_GEM_CREATE_EXPLICIT_SYNC))
220
Christian Königa022c542017-05-08 15:14:54 +0200221 return -EINVAL;
222
Alex Deucher834e0f82017-03-08 17:40:17 -0500223 /* reject invalid gem domains */
224 if (args->in.domains & ~(AMDGPU_GEM_DOMAIN_CPU |
225 AMDGPU_GEM_DOMAIN_GTT |
226 AMDGPU_GEM_DOMAIN_VRAM |
227 AMDGPU_GEM_DOMAIN_GDS |
228 AMDGPU_GEM_DOMAIN_GWS |
Christian Königa022c542017-05-08 15:14:54 +0200229 AMDGPU_GEM_DOMAIN_OA))
230 return -EINVAL;
Alex Deucher834e0f82017-03-08 17:40:17 -0500231
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400232 /* create a gem object to contain this object in */
233 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
234 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
Christian König6ac7def2017-08-23 20:11:25 +0200235 flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400236 if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS)
237 size = size << AMDGPU_GDS_SHIFT;
238 else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS)
239 size = size << AMDGPU_GWS_SHIFT;
240 else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA)
241 size = size << AMDGPU_OA_SHIFT;
Christian Königa022c542017-05-08 15:14:54 +0200242 else
243 return -EINVAL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400244 }
245 size = roundup(size, PAGE_SIZE);
246
Christian Könige1eb899b42017-08-25 09:14:43 +0200247 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
248 r = amdgpu_bo_reserve(vm->root.base.bo, false);
249 if (r)
250 return r;
251
252 resv = vm->root.base.bo->tbo.resv;
253 }
254
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400255 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
256 (u32)(0xffffffff & args->in.domains),
Christian Könige1eb899b42017-08-25 09:14:43 +0200257 flags, false, resv, &gobj);
258 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
259 if (!r) {
260 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
261
262 abo->parent = amdgpu_bo_ref(vm->root.base.bo);
263 }
264 amdgpu_bo_unreserve(vm->root.base.bo);
265 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200267 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400268
269 r = drm_gem_handle_create(filp, gobj, &handle);
270 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300271 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400272 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200273 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400274
275 memset(args, 0, sizeof(*args));
276 args->out.handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400277 return 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400278}
279
280int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
281 struct drm_file *filp)
282{
Christian König19be5572017-04-12 14:24:39 +0200283 struct ttm_operation_ctx ctx = { true, false };
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400284 struct amdgpu_device *adev = dev->dev_private;
285 struct drm_amdgpu_gem_userptr *args = data;
286 struct drm_gem_object *gobj;
287 struct amdgpu_bo *bo;
288 uint32_t handle;
289 int r;
290
291 if (offset_in_page(args->addr | args->size))
292 return -EINVAL;
293
294 /* reject unknown flag values */
295 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
296 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
297 AMDGPU_GEM_USERPTR_REGISTER))
298 return -EINVAL;
299
Christian König358c2582016-03-11 15:29:27 +0100300 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
301 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400302
Christian König358c2582016-03-11 15:29:27 +0100303 /* if we want to write to it we must install a MMU notifier */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400304 return -EACCES;
305 }
306
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400307 /* create a gem object to contain this object in */
Christian Könige1eb899b42017-08-25 09:14:43 +0200308 r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
309 0, 0, NULL, &gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400310 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200311 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400312
313 bo = gem_to_amdgpu_bo(gobj);
Kent Russell6d7d9c52017-08-08 07:58:01 -0400314 bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
Christian König1ea863f2015-12-18 22:13:12 +0100315 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400316 r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags);
317 if (r)
318 goto release_object;
319
320 if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) {
321 r = amdgpu_mn_register(bo, args->addr);
322 if (r)
323 goto release_object;
324 }
325
326 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
Christian König2f568db2016-02-23 12:36:59 +0100327 r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm,
328 bo->tbo.ttm->pages);
329 if (r)
Xiangliang.Yud5a480b2017-10-20 17:21:40 +0800330 goto release_object;
Christian König2f568db2016-02-23 12:36:59 +0100331
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400332 r = amdgpu_bo_reserve(bo, true);
Christian König2f568db2016-02-23 12:36:59 +0100333 if (r)
334 goto free_pages;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400335
336 amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
Christian König19be5572017-04-12 14:24:39 +0200337 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400338 amdgpu_bo_unreserve(bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400339 if (r)
Christian König2f568db2016-02-23 12:36:59 +0100340 goto free_pages;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400341 }
342
343 r = drm_gem_handle_create(filp, gobj, &handle);
344 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300345 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400346 if (r)
Christian Königa022c542017-05-08 15:14:54 +0200347 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400348
349 args->handle = handle;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400350 return 0;
351
Christian König2f568db2016-02-23 12:36:59 +0100352free_pages:
Mel Gormanc6f92f92017-11-15 17:37:55 -0800353 release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages);
Christian König2f568db2016-02-23 12:36:59 +0100354
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400355release_object:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300356 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400358 return r;
359}
360
361int amdgpu_mode_dumb_mmap(struct drm_file *filp,
362 struct drm_device *dev,
363 uint32_t handle, uint64_t *offset_p)
364{
365 struct drm_gem_object *gobj;
366 struct amdgpu_bo *robj;
367
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100368 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400369 if (gobj == NULL) {
370 return -ENOENT;
371 }
372 robj = gem_to_amdgpu_bo(gobj);
Christian Königcc325d12016-02-08 11:08:35 +0100373 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
Christian König271c8122015-05-13 14:30:53 +0200374 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300375 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400376 return -EPERM;
377 }
378 *offset_p = amdgpu_bo_mmap_offset(robj);
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300379 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400380 return 0;
381}
382
383int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
384 struct drm_file *filp)
385{
386 union drm_amdgpu_gem_mmap *args = data;
387 uint32_t handle = args->in.handle;
388 memset(args, 0, sizeof(*args));
389 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
390}
391
392/**
393 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
394 *
395 * @timeout_ns: timeout in ns
396 *
397 * Calculate the timeout in jiffies from an absolute timeout in ns.
398 */
399unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
400{
401 unsigned long timeout_jiffies;
402 ktime_t timeout;
403
404 /* clamp timeout if it's to large */
405 if (((int64_t)timeout_ns) < 0)
406 return MAX_SCHEDULE_TIMEOUT;
407
Christian König0f117702015-07-08 16:58:48 +0200408 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400409 if (ktime_to_ns(timeout) < 0)
410 return 0;
411
412 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
413 /* clamp timeout to avoid unsigned-> signed overflow */
414 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT )
415 return MAX_SCHEDULE_TIMEOUT - 1;
416
417 return timeout_jiffies;
418}
419
420int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
421 struct drm_file *filp)
422{
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400423 union drm_amdgpu_gem_wait_idle *args = data;
424 struct drm_gem_object *gobj;
425 struct amdgpu_bo *robj;
426 uint32_t handle = args->in.handle;
427 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
428 int r = 0;
429 long ret;
430
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100431 gobj = drm_gem_object_lookup(filp, handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400432 if (gobj == NULL) {
433 return -ENOENT;
434 }
435 robj = gem_to_amdgpu_bo(gobj);
Chris Wilson0fea2ed2016-08-29 08:08:24 +0100436 ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true,
437 timeout);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400438
439 /* ret == 0 means not signaled,
440 * ret > 0 means signaled
441 * ret < 0 means interrupted before timeout
442 */
443 if (ret >= 0) {
444 memset(args, 0, sizeof(*args));
445 args->out.status = (ret == 0);
446 } else
447 r = ret;
448
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300449 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400450 return r;
451}
452
453int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
454 struct drm_file *filp)
455{
456 struct drm_amdgpu_gem_metadata *args = data;
457 struct drm_gem_object *gobj;
458 struct amdgpu_bo *robj;
459 int r = -1;
460
461 DRM_DEBUG("%d \n", args->handle);
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100462 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463 if (gobj == NULL)
464 return -ENOENT;
465 robj = gem_to_amdgpu_bo(gobj);
466
467 r = amdgpu_bo_reserve(robj, false);
468 if (unlikely(r != 0))
469 goto out;
470
471 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
472 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
473 r = amdgpu_bo_get_metadata(robj, args->data.data,
474 sizeof(args->data.data),
475 &args->data.data_size_bytes,
476 &args->data.flags);
477 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
Dan Carpenter0913eab2015-09-23 14:00:35 +0300478 if (args->data.data_size_bytes > sizeof(args->data.data)) {
479 r = -EINVAL;
480 goto unreserve;
481 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400482 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
483 if (!r)
484 r = amdgpu_bo_set_metadata(robj, args->data.data,
485 args->data.data_size_bytes,
486 args->data.flags);
487 }
488
Dan Carpenter0913eab2015-09-23 14:00:35 +0300489unreserve:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400490 amdgpu_bo_unreserve(robj);
491out:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300492 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400493 return r;
494}
495
496/**
497 * amdgpu_gem_va_update_vm -update the bo_va in its VM
498 *
499 * @adev: amdgpu_device pointer
Christian Königdc54d3d2017-03-13 10:13:38 +0100500 * @vm: vm to update
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400501 * @bo_va: bo_va to update
Christian König2ffdaaf2017-01-27 15:58:43 +0100502 * @list: validation list
Christian Königdc54d3d2017-03-13 10:13:38 +0100503 * @operation: map, unmap or clear
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400504 *
Christian König2ffdaaf2017-01-27 15:58:43 +0100505 * Update the bo_va directly after setting its address. Errors are not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400506 * vital here, so they are not reported back to userspace.
507 */
508static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
Christian Königdc54d3d2017-03-13 10:13:38 +0100509 struct amdgpu_vm *vm,
Christian Königf7da30d2016-09-28 12:03:04 +0200510 struct amdgpu_bo_va *bo_va,
Christian König2ffdaaf2017-01-27 15:58:43 +0100511 struct list_head *list,
Christian Königf7da30d2016-09-28 12:03:04 +0200512 uint32_t operation)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513{
Christian König3f3333f2017-08-03 14:02:13 +0200514 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400515
Christian König3f3333f2017-08-03 14:02:13 +0200516 if (!amdgpu_vm_ready(vm))
517 return;
Chunming Zhoue410b5c2015-12-07 15:02:52 +0800518
Nicolai Hähnlef3467812017-03-23 19:36:31 +0100519 r = amdgpu_vm_clear_freed(adev, vm, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400520 if (r)
Christian König2ffdaaf2017-01-27 15:58:43 +0100521 goto error;
monk.liu194a3362015-07-22 13:29:28 +0800522
Christian König80f95c52017-03-13 10:13:39 +0100523 if (operation == AMDGPU_VA_OP_MAP ||
524 operation == AMDGPU_VA_OP_REPLACE)
Flora Cui05dcb5c2016-09-22 11:34:47 +0800525 r = amdgpu_vm_bo_update(adev, bo_va, false);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400526
Christian König0abc6872017-09-01 20:37:57 +0200527 r = amdgpu_vm_update_directories(adev, vm);
528 if (r)
529 goto error;
530
Christian König2ffdaaf2017-01-27 15:58:43 +0100531error:
Christian König68fdd3d2015-06-16 14:50:02 +0200532 if (r && r != -ERESTARTSYS)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400533 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
534}
535
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
537 struct drm_file *filp)
538{
Junwei Zhangb85891b2017-01-16 13:59:01 +0800539 const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
540 AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
Alex Xie66e02bc2017-02-14 12:04:52 -0500541 AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK;
Junwei Zhangb85891b2017-01-16 13:59:01 +0800542 const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
543 AMDGPU_VM_PAGE_PRT;
544
Christian König34b5f6a2015-06-08 15:03:00 +0200545 struct drm_amdgpu_gem_va *args = data;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400546 struct drm_gem_object *gobj;
547 struct amdgpu_device *adev = dev->dev_private;
548 struct amdgpu_fpriv *fpriv = filp->driver_priv;
Christian König765e7fb2016-09-15 15:06:50 +0200549 struct amdgpu_bo *abo;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400550 struct amdgpu_bo_va *bo_va;
Christian Königb88c8792016-09-28 16:33:01 +0200551 struct amdgpu_bo_list_entry vm_pd;
552 struct ttm_validate_buffer tv;
Chunming Zhou49b02b12015-11-13 14:18:38 +0800553 struct ww_acquire_ctx ticket;
Christian Könige1eb899b42017-08-25 09:14:43 +0200554 struct list_head list, duplicates;
Alex Xie54635452017-02-14 12:22:57 -0500555 uint64_t va_flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556 int r = 0;
557
Christian König34b5f6a2015-06-08 15:03:00 +0200558 if (args->va_address < AMDGPU_VA_RESERVED_SIZE) {
Christian König4b7f0842017-11-13 13:58:17 +0100559 dev_dbg(&dev->pdev->dev,
Christian Königff4cd382017-11-06 15:25:37 +0100560 "va_address 0x%LX is in reserved area 0x%LX\n",
561 args->va_address, AMDGPU_VA_RESERVED_SIZE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400562 return -EINVAL;
563 }
564
Christian Königbb7939b2017-11-06 15:37:01 +0100565 if (args->va_address >= AMDGPU_VA_HOLE_START &&
566 args->va_address < AMDGPU_VA_HOLE_END) {
567 dev_dbg(&dev->pdev->dev,
568 "va_address 0x%LX is in VA hole 0x%LX-0x%LX\n",
569 args->va_address, AMDGPU_VA_HOLE_START,
570 AMDGPU_VA_HOLE_END);
571 return -EINVAL;
572 }
573
574 args->va_address &= AMDGPU_VA_HOLE_MASK;
575
Junwei Zhangb85891b2017-01-16 13:59:01 +0800576 if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
Christian König4b7f0842017-11-13 13:58:17 +0100577 dev_dbg(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
Junwei Zhangb85891b2017-01-16 13:59:01 +0800578 args->flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400579 return -EINVAL;
580 }
581
Christian König34b5f6a2015-06-08 15:03:00 +0200582 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400583 case AMDGPU_VA_OP_MAP:
584 case AMDGPU_VA_OP_UNMAP:
Christian Königdc54d3d2017-03-13 10:13:38 +0100585 case AMDGPU_VA_OP_CLEAR:
Christian König80f95c52017-03-13 10:13:39 +0100586 case AMDGPU_VA_OP_REPLACE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587 break;
588 default:
Christian König4b7f0842017-11-13 13:58:17 +0100589 dev_dbg(&dev->pdev->dev, "unsupported operation %d\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200590 args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400591 return -EINVAL;
592 }
593
Chunming Zhou49b02b12015-11-13 14:18:38 +0800594 INIT_LIST_HEAD(&list);
Christian Könige1eb899b42017-08-25 09:14:43 +0200595 INIT_LIST_HEAD(&duplicates);
Christian Königdc54d3d2017-03-13 10:13:38 +0100596 if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
597 !(args->flags & AMDGPU_VM_PAGE_PRT)) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800598 gobj = drm_gem_object_lookup(filp, args->handle);
599 if (gobj == NULL)
600 return -ENOENT;
601 abo = gem_to_amdgpu_bo(gobj);
602 tv.bo = &abo->tbo;
603 tv.shared = false;
604 list_add(&tv.head, &list);
605 } else {
606 gobj = NULL;
607 abo = NULL;
608 }
Chunming Zhou49b02b12015-11-13 14:18:38 +0800609
Christian Königb88c8792016-09-28 16:33:01 +0200610 amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
Christian Königb5a5ec52016-03-08 17:47:46 +0100611
Christian Könige1eb899b42017-08-25 09:14:43 +0200612 r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800613 if (r)
614 goto error_unref;
Christian König34b5f6a2015-06-08 15:03:00 +0200615
Junwei Zhangb85891b2017-01-16 13:59:01 +0800616 if (abo) {
617 bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
618 if (!bo_va) {
619 r = -ENOENT;
620 goto error_backoff;
621 }
Christian Königdc54d3d2017-03-13 10:13:38 +0100622 } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800623 bo_va = fpriv->prt_va;
Christian Königdc54d3d2017-03-13 10:13:38 +0100624 } else {
625 bo_va = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400626 }
627
Christian König34b5f6a2015-06-08 15:03:00 +0200628 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400629 case AMDGPU_VA_OP_MAP:
Christian Königec681542017-08-01 10:51:43 +0200630 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
Christian König663e4572017-03-13 10:13:37 +0100631 args->map_size);
632 if (r)
633 goto error_backoff;
Alex Xie54635452017-02-14 12:22:57 -0500634
Christian König663e4572017-03-13 10:13:37 +0100635 va_flags = amdgpu_vm_get_pte_flags(adev, args->flags);
Christian König34b5f6a2015-06-08 15:03:00 +0200636 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
637 args->offset_in_bo, args->map_size,
Christian König9f7eb532015-05-18 16:05:57 +0200638 va_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400639 break;
640 case AMDGPU_VA_OP_UNMAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200641 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642 break;
Christian Königdc54d3d2017-03-13 10:13:38 +0100643
644 case AMDGPU_VA_OP_CLEAR:
645 r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
646 args->va_address,
647 args->map_size);
648 break;
Christian König80f95c52017-03-13 10:13:39 +0100649 case AMDGPU_VA_OP_REPLACE:
Christian Königec681542017-08-01 10:51:43 +0200650 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
Christian König80f95c52017-03-13 10:13:39 +0100651 args->map_size);
652 if (r)
653 goto error_backoff;
654
655 va_flags = amdgpu_vm_get_pte_flags(adev, args->flags);
656 r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
657 args->offset_in_bo, args->map_size,
658 va_flags);
659 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400660 default:
661 break;
662 }
Junwei Zhangb85891b2017-01-16 13:59:01 +0800663 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
Christian Königdc54d3d2017-03-13 10:13:38 +0100664 amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, &list,
665 args->operation);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800666
667error_backoff:
Christian König2ffdaaf2017-01-27 15:58:43 +0100668 ttm_eu_backoff_reservation(&ticket, &list);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800669
Junwei Zhangb85891b2017-01-16 13:59:01 +0800670error_unref:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300671 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672 return r;
673}
674
675int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
676 struct drm_file *filp)
677{
Christian Könige1eb899b42017-08-25 09:14:43 +0200678 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400679 struct drm_amdgpu_gem_op *args = data;
680 struct drm_gem_object *gobj;
681 struct amdgpu_bo *robj;
682 int r;
683
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100684 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400685 if (gobj == NULL) {
686 return -ENOENT;
687 }
688 robj = gem_to_amdgpu_bo(gobj);
689
690 r = amdgpu_bo_reserve(robj, false);
691 if (unlikely(r))
692 goto out;
693
694 switch (args->op) {
695 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
696 struct drm_amdgpu_gem_create_in info;
Christian König7ecc2452017-07-26 17:02:52 +0200697 void __user *out = u64_to_user_ptr(args->value);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698
699 info.bo_size = robj->gem_base.size;
700 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
Kent Russell6d7d9c52017-08-08 07:58:01 -0400701 info.domains = robj->preferred_domains;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702 info.domain_flags = robj->flags;
Christian König4c28fb02015-08-28 17:27:54 +0200703 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704 if (copy_to_user(out, &info, sizeof(info)))
705 r = -EFAULT;
706 break;
707 }
Marek Olšákd8f65a22015-05-27 14:30:38 +0200708 case AMDGPU_GEM_OP_SET_PLACEMENT:
Christopher James Halse Rogers803d89a2017-04-03 13:31:22 +1000709 if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
710 r = -EINVAL;
711 amdgpu_bo_unreserve(robj);
712 break;
713 }
Christian Königcc325d12016-02-08 11:08:35 +0100714 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715 r = -EPERM;
Christian König4c28fb02015-08-28 17:27:54 +0200716 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400717 break;
718 }
Kent Russell6d7d9c52017-08-08 07:58:01 -0400719 robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
Christian König1ea863f2015-12-18 22:13:12 +0100720 AMDGPU_GEM_DOMAIN_GTT |
721 AMDGPU_GEM_DOMAIN_CPU);
Kent Russell6d7d9c52017-08-08 07:58:01 -0400722 robj->allowed_domains = robj->preferred_domains;
Christian König1ea863f2015-12-18 22:13:12 +0100723 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
724 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
725
Christian Könige1eb899b42017-08-25 09:14:43 +0200726 if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
727 amdgpu_vm_bo_invalidate(adev, robj, true);
728
Christian König4c28fb02015-08-28 17:27:54 +0200729 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730 break;
731 default:
Christian König4c28fb02015-08-28 17:27:54 +0200732 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400733 r = -EINVAL;
734 }
735
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400736out:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300737 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400738 return r;
739}
740
741int amdgpu_mode_dumb_create(struct drm_file *file_priv,
742 struct drm_device *dev,
743 struct drm_mode_create_dumb *args)
744{
745 struct amdgpu_device *adev = dev->dev_private;
746 struct drm_gem_object *gobj;
747 uint32_t handle;
748 int r;
749
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300750 args->pitch = amdgpu_align_pitch(adev, args->width,
751 DIV_ROUND_UP(args->bpp, 8), 0);
Dan Carpenter54ef0b52015-09-23 14:00:59 +0300752 args->size = (u64)args->pitch * args->height;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400753 args->size = ALIGN(args->size, PAGE_SIZE);
754
755 r = amdgpu_gem_object_create(adev, args->size, 0,
756 AMDGPU_GEM_DOMAIN_VRAM,
Alex Deucher857d9132015-08-27 00:14:16 -0400757 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian Könige1eb899b42017-08-25 09:14:43 +0200758 false, NULL, &gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400759 if (r)
760 return -ENOMEM;
761
762 r = drm_gem_handle_create(file_priv, gobj, &handle);
763 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300764 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400765 if (r) {
766 return r;
767 }
768 args->handle = handle;
769 return 0;
770}
771
772#if defined(CONFIG_DEBUG_FS)
Christian König7ea23562016-02-15 15:23:00 +0100773static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
774{
775 struct drm_gem_object *gobj = ptr;
776 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
777 struct seq_file *m = data;
778
779 unsigned domain;
780 const char *placement;
781 unsigned pin_count;
Christian Königb8e0e6e2017-06-26 15:19:30 +0200782 uint64_t offset;
Christian König7ea23562016-02-15 15:23:00 +0100783
784 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
785 switch (domain) {
786 case AMDGPU_GEM_DOMAIN_VRAM:
787 placement = "VRAM";
788 break;
789 case AMDGPU_GEM_DOMAIN_GTT:
790 placement = " GTT";
791 break;
792 case AMDGPU_GEM_DOMAIN_CPU:
793 default:
794 placement = " CPU";
795 break;
796 }
Christian Königb8e0e6e2017-06-26 15:19:30 +0200797 seq_printf(m, "\t0x%08x: %12ld byte %s",
798 id, amdgpu_bo_size(bo), placement);
799
Mark Rutland6aa7de02017-10-23 14:07:29 -0700800 offset = READ_ONCE(bo->tbo.mem.start);
Christian Königb8e0e6e2017-06-26 15:19:30 +0200801 if (offset != AMDGPU_BO_INVALID_OFFSET)
802 seq_printf(m, " @ 0x%010Lx", offset);
Christian König7ea23562016-02-15 15:23:00 +0100803
Mark Rutland6aa7de02017-10-23 14:07:29 -0700804 pin_count = READ_ONCE(bo->pin_count);
Christian König7ea23562016-02-15 15:23:00 +0100805 if (pin_count)
806 seq_printf(m, " pin count %d", pin_count);
807 seq_printf(m, "\n");
808
809 return 0;
810}
811
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400812static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
813{
814 struct drm_info_node *node = (struct drm_info_node *)m->private;
815 struct drm_device *dev = node->minor->dev;
Christian König7ea23562016-02-15 15:23:00 +0100816 struct drm_file *file;
817 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400818
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200819 r = mutex_lock_interruptible(&dev->filelist_mutex);
Christian König7ea23562016-02-15 15:23:00 +0100820 if (r)
821 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400822
Christian König7ea23562016-02-15 15:23:00 +0100823 list_for_each_entry(file, &dev->filelist, lhead) {
824 struct task_struct *task;
Christian Königb22e3ce2016-02-15 12:41:37 +0100825
Christian König7ea23562016-02-15 15:23:00 +0100826 /*
827 * Although we have a valid reference on file->pid, that does
828 * not guarantee that the task_struct who called get_pid() is
829 * still alive (e.g. get_pid(current) => fork() => exit()).
830 * Therefore, we need to protect this ->comm access using RCU.
831 */
832 rcu_read_lock();
833 task = pid_task(file->pid, PIDTYPE_PID);
834 seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
835 task ? task->comm : "<unknown>");
836 rcu_read_unlock();
837
838 spin_lock(&file->table_lock);
839 idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
840 spin_unlock(&file->table_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400841 }
Christian König7ea23562016-02-15 15:23:00 +0100842
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200843 mutex_unlock(&dev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844 return 0;
845}
846
Nils Wallménius06ab6832016-05-02 12:46:15 -0400847static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400848 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
849};
850#endif
851
Alex Deucher75758252017-12-14 15:23:14 -0500852int amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400853{
854#if defined(CONFIG_DEBUG_FS)
855 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
856#endif
857 return 0;
858}