blob: 46b9ea4e61031d13f9e701b03f6c9ca53559751f [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
28#include <linux/ktime.h>
Stephen Rothwell568d7c72016-03-17 15:30:49 +110029#include <linux/pagemap.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040030#include <drm/drmP.h>
31#include <drm/amdgpu_drm.h>
32#include "amdgpu.h"
33
34void amdgpu_gem_object_free(struct drm_gem_object *gobj)
35{
36 struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj);
37
38 if (robj) {
Christian König9298e522015-06-03 21:31:20 +020039 amdgpu_mn_unregister(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040 amdgpu_bo_unref(&robj);
41 }
42}
43
44int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
Christian Könige1eb899b42017-08-25 09:14:43 +020045 int alignment, u32 initial_domain,
Christian Königeab3de22018-03-14 14:48:17 -050046 u64 flags, enum ttm_bo_type type,
Christian Könige1eb899b42017-08-25 09:14:43 +020047 struct reservation_object *resv,
48 struct drm_gem_object **obj)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040049{
Christian Könige1eb899b42017-08-25 09:14:43 +020050 struct amdgpu_bo *bo;
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
Christian König08082102018-04-10 13:42:38 +020059retry:
Christian Königeab3de22018-03-14 14:48:17 -050060 r = amdgpu_bo_create(adev, size, alignment, initial_domain,
61 flags, type, resv, &bo);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040062 if (r) {
Christian König08082102018-04-10 13:42:38 +020063 if (r != -ERESTARTSYS) {
64 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
65 flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
66 goto retry;
67 }
68
69 if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
70 initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
71 goto retry;
72 }
73 DRM_DEBUG("Failed to allocate GEM object (%ld, %d, %u, %d)\n",
74 size, initial_domain, alignment, r);
75 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -040076 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 ||
Gustavo A. R. Silva93bab702018-02-14 23:20:00 -0600524 operation == AMDGPU_VA_OP_REPLACE) {
Flora Cui05dcb5c2016-09-22 11:34:47 +0800525 r = amdgpu_vm_bo_update(adev, bo_va, false);
Gustavo A. R. Silva93bab702018-02-14 23:20:00 -0600526 if (r)
527 goto error;
528 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529
Christian König0abc6872017-09-01 20:37:57 +0200530 r = amdgpu_vm_update_directories(adev, vm);
Christian König0abc6872017-09-01 20:37:57 +0200531
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) {
Christian König4b7f0842017-11-13 13:58:17 +0100560 dev_dbg(&dev->pdev->dev,
Christian Königff4cd382017-11-06 15:25:37 +0100561 "va_address 0x%LX is in reserved area 0x%LX\n",
562 args->va_address, AMDGPU_VA_RESERVED_SIZE);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400563 return -EINVAL;
564 }
565
Christian Königbb7939b2017-11-06 15:37:01 +0100566 if (args->va_address >= AMDGPU_VA_HOLE_START &&
567 args->va_address < AMDGPU_VA_HOLE_END) {
568 dev_dbg(&dev->pdev->dev,
569 "va_address 0x%LX is in VA hole 0x%LX-0x%LX\n",
570 args->va_address, AMDGPU_VA_HOLE_START,
571 AMDGPU_VA_HOLE_END);
572 return -EINVAL;
573 }
574
575 args->va_address &= AMDGPU_VA_HOLE_MASK;
576
Junwei Zhangb85891b2017-01-16 13:59:01 +0800577 if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
Christian König4b7f0842017-11-13 13:58:17 +0100578 dev_dbg(&dev->pdev->dev, "invalid flags combination 0x%08X\n",
Junwei Zhangb85891b2017-01-16 13:59:01 +0800579 args->flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580 return -EINVAL;
581 }
582
Christian König34b5f6a2015-06-08 15:03:00 +0200583 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584 case AMDGPU_VA_OP_MAP:
585 case AMDGPU_VA_OP_UNMAP:
Christian Königdc54d3d2017-03-13 10:13:38 +0100586 case AMDGPU_VA_OP_CLEAR:
Christian König80f95c52017-03-13 10:13:39 +0100587 case AMDGPU_VA_OP_REPLACE:
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400588 break;
589 default:
Christian König4b7f0842017-11-13 13:58:17 +0100590 dev_dbg(&dev->pdev->dev, "unsupported operation %d\n",
Christian König34b5f6a2015-06-08 15:03:00 +0200591 args->operation);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400592 return -EINVAL;
593 }
594
Chunming Zhou49b02b12015-11-13 14:18:38 +0800595 INIT_LIST_HEAD(&list);
Christian Könige1eb899b42017-08-25 09:14:43 +0200596 INIT_LIST_HEAD(&duplicates);
Christian Königdc54d3d2017-03-13 10:13:38 +0100597 if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
598 !(args->flags & AMDGPU_VM_PAGE_PRT)) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800599 gobj = drm_gem_object_lookup(filp, args->handle);
600 if (gobj == NULL)
601 return -ENOENT;
602 abo = gem_to_amdgpu_bo(gobj);
603 tv.bo = &abo->tbo;
604 tv.shared = false;
605 list_add(&tv.head, &list);
606 } else {
607 gobj = NULL;
608 abo = NULL;
609 }
Chunming Zhou49b02b12015-11-13 14:18:38 +0800610
Christian Königb88c8792016-09-28 16:33:01 +0200611 amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd);
Christian Königb5a5ec52016-03-08 17:47:46 +0100612
Christian Könige1eb899b42017-08-25 09:14:43 +0200613 r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800614 if (r)
615 goto error_unref;
Christian König34b5f6a2015-06-08 15:03:00 +0200616
Junwei Zhangb85891b2017-01-16 13:59:01 +0800617 if (abo) {
618 bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
619 if (!bo_va) {
620 r = -ENOENT;
621 goto error_backoff;
622 }
Christian Königdc54d3d2017-03-13 10:13:38 +0100623 } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
Junwei Zhangb85891b2017-01-16 13:59:01 +0800624 bo_va = fpriv->prt_va;
Christian Königdc54d3d2017-03-13 10:13:38 +0100625 } else {
626 bo_va = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400627 }
628
Christian König34b5f6a2015-06-08 15:03:00 +0200629 switch (args->operation) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400630 case AMDGPU_VA_OP_MAP:
Christian Königec681542017-08-01 10:51:43 +0200631 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
Christian König663e4572017-03-13 10:13:37 +0100632 args->map_size);
633 if (r)
634 goto error_backoff;
Alex Xie54635452017-02-14 12:22:57 -0500635
Christian König132f34e2018-01-12 15:26:08 +0100636 va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
Christian König34b5f6a2015-06-08 15:03:00 +0200637 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
638 args->offset_in_bo, args->map_size,
Christian König9f7eb532015-05-18 16:05:57 +0200639 va_flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400640 break;
641 case AMDGPU_VA_OP_UNMAP:
Christian König34b5f6a2015-06-08 15:03:00 +0200642 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643 break;
Christian Königdc54d3d2017-03-13 10:13:38 +0100644
645 case AMDGPU_VA_OP_CLEAR:
646 r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
647 args->va_address,
648 args->map_size);
649 break;
Christian König80f95c52017-03-13 10:13:39 +0100650 case AMDGPU_VA_OP_REPLACE:
Christian Königec681542017-08-01 10:51:43 +0200651 r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address,
Christian König80f95c52017-03-13 10:13:39 +0100652 args->map_size);
653 if (r)
654 goto error_backoff;
655
Christian König132f34e2018-01-12 15:26:08 +0100656 va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags);
Christian König80f95c52017-03-13 10:13:39 +0100657 r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
658 args->offset_in_bo, args->map_size,
659 va_flags);
660 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400661 default:
662 break;
663 }
Junwei Zhangb85891b2017-01-16 13:59:01 +0800664 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug)
Christian Königdc54d3d2017-03-13 10:13:38 +0100665 amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, &list,
666 args->operation);
Junwei Zhangb85891b2017-01-16 13:59:01 +0800667
668error_backoff:
Christian König2ffdaaf2017-01-27 15:58:43 +0100669 ttm_eu_backoff_reservation(&ticket, &list);
Chunming Zhoue98c1b02015-11-13 15:22:04 +0800670
Junwei Zhangb85891b2017-01-16 13:59:01 +0800671error_unref:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300672 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400673 return r;
674}
675
676int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
677 struct drm_file *filp)
678{
Christian Könige1eb899b42017-08-25 09:14:43 +0200679 struct amdgpu_device *adev = dev->dev_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400680 struct drm_amdgpu_gem_op *args = data;
681 struct drm_gem_object *gobj;
682 struct amdgpu_bo *robj;
683 int r;
684
Chris Wilsona8ad0bd2016-05-09 11:04:54 +0100685 gobj = drm_gem_object_lookup(filp, args->handle);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400686 if (gobj == NULL) {
687 return -ENOENT;
688 }
689 robj = gem_to_amdgpu_bo(gobj);
690
691 r = amdgpu_bo_reserve(robj, false);
692 if (unlikely(r))
693 goto out;
694
695 switch (args->op) {
696 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
697 struct drm_amdgpu_gem_create_in info;
Christian König7ecc2452017-07-26 17:02:52 +0200698 void __user *out = u64_to_user_ptr(args->value);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400699
700 info.bo_size = robj->gem_base.size;
701 info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT;
Kent Russell6d7d9c52017-08-08 07:58:01 -0400702 info.domains = robj->preferred_domains;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400703 info.domain_flags = robj->flags;
Christian König4c28fb02015-08-28 17:27:54 +0200704 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400705 if (copy_to_user(out, &info, sizeof(info)))
706 r = -EFAULT;
707 break;
708 }
Marek Olšákd8f65a22015-05-27 14:30:38 +0200709 case AMDGPU_GEM_OP_SET_PLACEMENT:
Christopher James Halse Rogers803d89a2017-04-03 13:31:22 +1000710 if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) {
711 r = -EINVAL;
712 amdgpu_bo_unreserve(robj);
713 break;
714 }
Christian Königcc325d12016-02-08 11:08:35 +0100715 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400716 r = -EPERM;
Christian König4c28fb02015-08-28 17:27:54 +0200717 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400718 break;
719 }
Kent Russell6d7d9c52017-08-08 07:58:01 -0400720 robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
Christian König1ea863f2015-12-18 22:13:12 +0100721 AMDGPU_GEM_DOMAIN_GTT |
722 AMDGPU_GEM_DOMAIN_CPU);
Kent Russell6d7d9c52017-08-08 07:58:01 -0400723 robj->allowed_domains = robj->preferred_domains;
Christian König1ea863f2015-12-18 22:13:12 +0100724 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
725 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
726
Christian Könige1eb899b42017-08-25 09:14:43 +0200727 if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
728 amdgpu_vm_bo_invalidate(adev, robj, true);
729
Christian König4c28fb02015-08-28 17:27:54 +0200730 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400731 break;
732 default:
Christian König4c28fb02015-08-28 17:27:54 +0200733 amdgpu_bo_unreserve(robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734 r = -EINVAL;
735 }
736
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737out:
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300738 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400739 return r;
740}
741
742int amdgpu_mode_dumb_create(struct drm_file *file_priv,
743 struct drm_device *dev,
744 struct drm_mode_create_dumb *args)
745{
746 struct amdgpu_device *adev = dev->dev_private;
747 struct drm_gem_object *gobj;
748 uint32_t handle;
749 int r;
750
Laurent Pinchart8e911ab2016-10-18 01:41:17 +0300751 args->pitch = amdgpu_align_pitch(adev, args->width,
752 DIV_ROUND_UP(args->bpp, 8), 0);
Dan Carpenter54ef0b52015-09-23 14:00:59 +0300753 args->size = (u64)args->pitch * args->height;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400754 args->size = ALIGN(args->size, PAGE_SIZE);
755
756 r = amdgpu_gem_object_create(adev, args->size, 0,
757 AMDGPU_GEM_DOMAIN_VRAM,
Alex Deucher857d9132015-08-27 00:14:16 -0400758 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian Könige1eb899b42017-08-25 09:14:43 +0200759 false, NULL, &gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400760 if (r)
761 return -ENOMEM;
762
763 r = drm_gem_handle_create(file_priv, gobj, &handle);
764 /* drop reference from allocate - handle holds it now */
Cihangir Akturkf62facc2017-08-03 14:58:16 +0300765 drm_gem_object_put_unlocked(gobj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400766 if (r) {
767 return r;
768 }
769 args->handle = handle;
770 return 0;
771}
772
773#if defined(CONFIG_DEBUG_FS)
Christian König7ea23562016-02-15 15:23:00 +0100774static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data)
775{
776 struct drm_gem_object *gobj = ptr;
777 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
778 struct seq_file *m = data;
779
780 unsigned domain;
781 const char *placement;
782 unsigned pin_count;
Christian Königb8e0e6e2017-06-26 15:19:30 +0200783 uint64_t offset;
Christian König7ea23562016-02-15 15:23:00 +0100784
785 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
786 switch (domain) {
787 case AMDGPU_GEM_DOMAIN_VRAM:
788 placement = "VRAM";
789 break;
790 case AMDGPU_GEM_DOMAIN_GTT:
791 placement = " GTT";
792 break;
793 case AMDGPU_GEM_DOMAIN_CPU:
794 default:
795 placement = " CPU";
796 break;
797 }
Christian Königb8e0e6e2017-06-26 15:19:30 +0200798 seq_printf(m, "\t0x%08x: %12ld byte %s",
799 id, amdgpu_bo_size(bo), placement);
800
Mark Rutland6aa7de02017-10-23 14:07:29 -0700801 offset = READ_ONCE(bo->tbo.mem.start);
Christian Königb8e0e6e2017-06-26 15:19:30 +0200802 if (offset != AMDGPU_BO_INVALID_OFFSET)
803 seq_printf(m, " @ 0x%010Lx", offset);
Christian König7ea23562016-02-15 15:23:00 +0100804
Mark Rutland6aa7de02017-10-23 14:07:29 -0700805 pin_count = READ_ONCE(bo->pin_count);
Christian König7ea23562016-02-15 15:23:00 +0100806 if (pin_count)
807 seq_printf(m, " pin count %d", pin_count);
808 seq_printf(m, "\n");
809
810 return 0;
811}
812
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400813static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data)
814{
815 struct drm_info_node *node = (struct drm_info_node *)m->private;
816 struct drm_device *dev = node->minor->dev;
Christian König7ea23562016-02-15 15:23:00 +0100817 struct drm_file *file;
818 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400819
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200820 r = mutex_lock_interruptible(&dev->filelist_mutex);
Christian König7ea23562016-02-15 15:23:00 +0100821 if (r)
822 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400823
Christian König7ea23562016-02-15 15:23:00 +0100824 list_for_each_entry(file, &dev->filelist, lhead) {
825 struct task_struct *task;
Christian Königb22e3ce2016-02-15 12:41:37 +0100826
Christian König7ea23562016-02-15 15:23:00 +0100827 /*
828 * Although we have a valid reference on file->pid, that does
829 * not guarantee that the task_struct who called get_pid() is
830 * still alive (e.g. get_pid(current) => fork() => exit()).
831 * Therefore, we need to protect this ->comm access using RCU.
832 */
833 rcu_read_lock();
834 task = pid_task(file->pid, PIDTYPE_PID);
835 seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid),
836 task ? task->comm : "<unknown>");
837 rcu_read_unlock();
838
839 spin_lock(&file->table_lock);
840 idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m);
841 spin_unlock(&file->table_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400842 }
Christian König7ea23562016-02-15 15:23:00 +0100843
Daniel Vetter1d2ac402016-04-26 19:29:41 +0200844 mutex_unlock(&dev->filelist_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400845 return 0;
846}
847
Nils Wallménius06ab6832016-05-02 12:46:15 -0400848static const struct drm_info_list amdgpu_debugfs_gem_list[] = {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400849 {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL},
850};
851#endif
852
Alex Deucher75758252017-12-14 15:23:14 -0500853int amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400854{
855#if defined(CONFIG_DEBUG_FS)
856 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1);
857#endif
858 return 0;
859}