1 /*
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>
29 #include <linux/module.h>
30 #include <linux/overflow.h>
31 #include <linux/pagemap.h>
32 #include <linux/pci.h>
33 #include <linux/dma-buf.h>
34 #include <linux/dma-fence-unwrap.h>
35 #include <linux/uaccess.h>
36
37 #include <drm/amdgpu_drm.h>
38 #include <drm/drm_drv.h>
39 #include <drm/drm_exec.h>
40 #include <drm/drm_gem_ttm_helper.h>
41 #include <drm/ttm/ttm_tt.h>
42 #include <drm/drm_syncobj.h>
43
44 #include "amdgpu.h"
45 #include "amdgpu_display.h"
46 #include "amdgpu_dma_buf.h"
47 #include "amdgpu_hmm.h"
48 #include "amdgpu_xgmi.h"
49 #include "amdgpu_vm.h"
50
51 static int
amdgpu_gem_add_input_fence(struct drm_file * filp,uint64_t syncobj_handles_array,uint32_t num_syncobj_handles)52 amdgpu_gem_add_input_fence(struct drm_file *filp,
53 uint64_t syncobj_handles_array,
54 uint32_t num_syncobj_handles)
55 {
56 struct dma_fence *fence;
57 uint32_t *syncobj_handles;
58 int ret, i;
59
60 if (!num_syncobj_handles)
61 return 0;
62
63 syncobj_handles = memdup_user(u64_to_user_ptr(syncobj_handles_array),
64 size_mul(sizeof(uint32_t), num_syncobj_handles));
65 if (IS_ERR(syncobj_handles))
66 return PTR_ERR(syncobj_handles);
67
68 for (i = 0; i < num_syncobj_handles; i++) {
69
70 if (!syncobj_handles[i]) {
71 ret = -EINVAL;
72 goto free_memdup;
73 }
74
75 ret = drm_syncobj_find_fence(filp, syncobj_handles[i], 0, 0, &fence);
76 if (ret)
77 goto free_memdup;
78
79 dma_fence_wait(fence, false);
80
81 /* TODO: optimize async handling */
82 dma_fence_put(fence);
83 }
84
85 free_memdup:
86 kfree(syncobj_handles);
87 return ret;
88 }
89
90 static int
amdgpu_gem_update_timeline_node(struct drm_file * filp,uint32_t syncobj_handle,uint64_t point,struct drm_syncobj ** syncobj,struct dma_fence_chain ** chain)91 amdgpu_gem_update_timeline_node(struct drm_file *filp,
92 uint32_t syncobj_handle,
93 uint64_t point,
94 struct drm_syncobj **syncobj,
95 struct dma_fence_chain **chain)
96 {
97 if (!syncobj_handle)
98 return 0;
99
100 /* Find the sync object */
101 *syncobj = drm_syncobj_find(filp, syncobj_handle);
102 if (!*syncobj)
103 return -ENOENT;
104
105 if (!point)
106 return 0;
107
108 /* Allocate the chain node */
109 *chain = dma_fence_chain_alloc();
110 if (!*chain) {
111 drm_syncobj_put(*syncobj);
112 *syncobj = NULL;
113 return -ENOMEM;
114 }
115
116 return 0;
117 }
118
amdgpu_gem_fault(struct vm_fault * vmf)119 static vm_fault_t amdgpu_gem_fault(struct vm_fault *vmf)
120 {
121 struct ttm_buffer_object *bo = vmf->vma->vm_private_data;
122 struct drm_device *ddev = bo->base.dev;
123 vm_fault_t ret;
124 int idx;
125
126 ret = ttm_bo_vm_reserve(bo, vmf);
127 if (ret)
128 return ret;
129
130 if (drm_dev_enter(ddev, &idx)) {
131 ret = amdgpu_bo_fault_reserve_notify(bo);
132 if (ret) {
133 drm_dev_exit(idx);
134 goto unlock;
135 }
136
137 ret = ttm_bo_vm_fault_reserved(vmf, vmf->vma->vm_page_prot,
138 TTM_BO_VM_NUM_PREFAULT);
139
140 drm_dev_exit(idx);
141 } else {
142 ret = ttm_bo_vm_dummy_page(vmf, vmf->vma->vm_page_prot);
143 }
144 if (ret == VM_FAULT_RETRY && !(vmf->flags & FAULT_FLAG_RETRY_NOWAIT))
145 return ret;
146
147 unlock:
148 dma_resv_unlock(bo->base.resv);
149 return ret;
150 }
151
152 static const struct vm_operations_struct amdgpu_gem_vm_ops = {
153 .fault = amdgpu_gem_fault,
154 .open = ttm_bo_vm_open,
155 .close = ttm_bo_vm_close,
156 .access = ttm_bo_vm_access
157 };
158
amdgpu_gem_object_free(struct drm_gem_object * gobj)159 static void amdgpu_gem_object_free(struct drm_gem_object *gobj)
160 {
161 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(gobj);
162
163 amdgpu_hmm_unregister(aobj);
164 ttm_bo_fini(&aobj->tbo);
165 }
166
amdgpu_gem_object_create(struct amdgpu_device * adev,unsigned long size,int alignment,u32 initial_domain,u64 flags,enum ttm_bo_type type,struct dma_resv * resv,struct drm_gem_object ** obj,int8_t xcp_id_plus1)167 int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size,
168 int alignment, u32 initial_domain,
169 u64 flags, enum ttm_bo_type type,
170 struct dma_resv *resv,
171 struct drm_gem_object **obj, int8_t xcp_id_plus1)
172 {
173 struct amdgpu_bo *bo;
174 struct amdgpu_bo_user *ubo;
175 struct amdgpu_bo_param bp;
176 int r;
177
178 memset(&bp, 0, sizeof(bp));
179 *obj = NULL;
180 flags |= AMDGPU_GEM_CREATE_VRAM_WIPE_ON_RELEASE;
181
182 bp.size = size;
183 bp.byte_align = alignment;
184 bp.type = type;
185 bp.resv = resv;
186 bp.preferred_domain = initial_domain;
187 bp.flags = flags;
188 bp.domain = initial_domain;
189 bp.bo_ptr_size = sizeof(struct amdgpu_bo);
190 bp.xcp_id_plus1 = xcp_id_plus1;
191
192 r = amdgpu_bo_create_user(adev, &bp, &ubo);
193 if (r)
194 return r;
195
196 bo = &ubo->bo;
197 *obj = &bo->tbo.base;
198
199 return 0;
200 }
201
amdgpu_gem_force_release(struct amdgpu_device * adev)202 void amdgpu_gem_force_release(struct amdgpu_device *adev)
203 {
204 struct drm_device *ddev = adev_to_drm(adev);
205 struct drm_file *file;
206
207 mutex_lock(&ddev->filelist_mutex);
208
209 list_for_each_entry(file, &ddev->filelist, lhead) {
210 struct drm_gem_object *gobj;
211 int handle;
212
213 WARN_ONCE(1, "Still active user space clients!\n");
214 spin_lock(&file->table_lock);
215 idr_for_each_entry(&file->object_idr, gobj, handle) {
216 WARN_ONCE(1, "And also active allocations!\n");
217 drm_gem_object_put(gobj);
218 }
219 idr_destroy(&file->object_idr);
220 spin_unlock(&file->table_lock);
221 }
222
223 mutex_unlock(&ddev->filelist_mutex);
224 }
225
226 /*
227 * Call from drm_gem_handle_create which appear in both new and open ioctl
228 * case.
229 */
amdgpu_gem_object_open(struct drm_gem_object * obj,struct drm_file * file_priv)230 static int amdgpu_gem_object_open(struct drm_gem_object *obj,
231 struct drm_file *file_priv)
232 {
233 struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj);
234 struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev);
235 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
236 struct amdgpu_vm *vm = &fpriv->vm;
237 struct amdgpu_bo_va *bo_va;
238 struct mm_struct *mm;
239 struct drm_exec exec;
240 int r;
241
242 mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm);
243 if (mm && mm != current->mm)
244 return -EPERM;
245
246 if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID &&
247 !amdgpu_vm_is_bo_always_valid(vm, abo))
248 return -EPERM;
249
250 drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
251 drm_exec_until_all_locked(&exec) {
252 r = drm_exec_prepare_obj(&exec, &abo->tbo.base, TTM_NUM_MOVE_FENCES + 1);
253 drm_exec_retry_on_contention(&exec);
254 if (unlikely(r))
255 goto out_unlock;
256
257 r = amdgpu_vm_lock_pd(vm, &exec, 0);
258 drm_exec_retry_on_contention(&exec);
259 if (unlikely(r))
260 goto out_unlock;
261 }
262
263 amdgpu_vm_bo_update_shared(abo);
264 bo_va = amdgpu_vm_bo_find(vm, abo);
265 if (!bo_va) {
266 bo_va = amdgpu_vm_bo_add(adev, vm, abo);
267 r = amdgpu_evf_mgr_attach_fence(&fpriv->evf_mgr, abo);
268 if (r)
269 goto out_unlock;
270 } else {
271 ++bo_va->ref_count;
272 }
273
274 drm_exec_fini(&exec);
275
276 /* Validate and add eviction fence to DMABuf imports with dynamic
277 * attachment in compute VMs. Re-validation will be done by
278 * amdgpu_vm_validate. Fences are on the reservation shared with the
279 * export, which is currently required to be validated and fenced
280 * already by amdgpu_amdkfd_gpuvm_restore_process_bos.
281 *
282 * Nested locking below for the case that a GEM object is opened in
283 * kfd_mem_export_dmabuf. Since the lock below is only taken for imports,
284 * but not for export, this is a different lock class that cannot lead to
285 * circular lock dependencies.
286 */
287 if (!vm->is_compute_context || !vm->process_info)
288 return 0;
289 if (!drm_gem_is_imported(obj) ||
290 !dma_buf_is_dynamic(obj->import_attach->dmabuf))
291 return 0;
292 mutex_lock_nested(&vm->process_info->lock, 1);
293 if (!WARN_ON(!vm->process_info->eviction_fence)) {
294 r = amdgpu_amdkfd_bo_validate_and_fence(abo, AMDGPU_GEM_DOMAIN_GTT,
295 &vm->process_info->eviction_fence->base);
296 if (r) {
297 struct amdgpu_task_info *ti = amdgpu_vm_get_task_info_vm(vm);
298
299 dev_warn(adev->dev, "validate_and_fence failed: %d\n", r);
300 if (ti) {
301 dev_warn(adev->dev, "pid %d\n", ti->task.pid);
302 amdgpu_vm_put_task_info(ti);
303 }
304 }
305 }
306 mutex_unlock(&vm->process_info->lock);
307 return r;
308
309 out_unlock:
310 drm_exec_fini(&exec);
311 return r;
312 }
313
amdgpu_gem_object_close(struct drm_gem_object * obj,struct drm_file * file_priv)314 static void amdgpu_gem_object_close(struct drm_gem_object *obj,
315 struct drm_file *file_priv)
316 {
317 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
318 struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev);
319 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
320 struct amdgpu_vm *vm = &fpriv->vm;
321
322 struct dma_fence *fence = NULL;
323 struct amdgpu_bo_va *bo_va;
324 struct drm_exec exec;
325 long r;
326
327 drm_exec_init(&exec, DRM_EXEC_IGNORE_DUPLICATES, 0);
328 drm_exec_until_all_locked(&exec) {
329 r = drm_exec_prepare_obj(&exec, &bo->tbo.base, 1);
330 drm_exec_retry_on_contention(&exec);
331 if (unlikely(r))
332 goto out_unlock;
333
334 r = amdgpu_vm_lock_pd(vm, &exec, 0);
335 drm_exec_retry_on_contention(&exec);
336 if (unlikely(r))
337 goto out_unlock;
338 }
339
340 if (!amdgpu_vm_is_bo_always_valid(vm, bo))
341 amdgpu_evf_mgr_detach_fence(&fpriv->evf_mgr, bo);
342
343 bo_va = amdgpu_vm_bo_find(vm, bo);
344 if (!bo_va || --bo_va->ref_count)
345 goto out_unlock;
346
347 amdgpu_vm_bo_del(adev, bo_va);
348 amdgpu_vm_bo_update_shared(bo);
349 if (!amdgpu_vm_ready(vm))
350 goto out_unlock;
351
352 r = amdgpu_vm_clear_freed(adev, vm, &fence);
353 if (unlikely(r < 0) && !drm_dev_is_unplugged(adev_to_drm(adev)))
354 dev_err(adev->dev, "failed to clear page "
355 "tables on GEM object close (%ld)\n", r);
356 if (r || !fence)
357 goto out_unlock;
358
359 amdgpu_bo_fence(bo, fence, true);
360 dma_fence_put(fence);
361
362 out_unlock:
363 if (r && !drm_dev_is_unplugged(adev_to_drm(adev)))
364 dev_err(adev->dev, "leaking bo va (%ld)\n", r);
365 drm_exec_fini(&exec);
366 }
367
amdgpu_gem_object_mmap(struct drm_gem_object * obj,struct vm_area_struct * vma)368 static int amdgpu_gem_object_mmap(struct drm_gem_object *obj, struct vm_area_struct *vma)
369 {
370 struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj);
371
372 if (amdgpu_ttm_tt_get_usermm(bo->tbo.ttm))
373 return -EPERM;
374 if (bo->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)
375 return -EPERM;
376
377 /* Workaround for Thunk bug creating PROT_NONE,MAP_PRIVATE mappings
378 * for debugger access to invisible VRAM. Should have used MAP_SHARED
379 * instead. Clearing VM_MAYWRITE prevents the mapping from ever
380 * becoming writable and makes is_cow_mapping(vm_flags) false.
381 */
382 if (is_cow_mapping(vma->vm_flags) &&
383 !(vma->vm_flags & VM_ACCESS_FLAGS))
384 vm_flags_clear(vma, VM_MAYWRITE);
385
386 return drm_gem_ttm_mmap(obj, vma);
387 }
388
389 const struct drm_gem_object_funcs amdgpu_gem_object_funcs = {
390 .free = amdgpu_gem_object_free,
391 .open = amdgpu_gem_object_open,
392 .close = amdgpu_gem_object_close,
393 .export = amdgpu_gem_prime_export,
394 .vmap = drm_gem_ttm_vmap,
395 .vunmap = drm_gem_ttm_vunmap,
396 .mmap = amdgpu_gem_object_mmap,
397 .vm_ops = &amdgpu_gem_vm_ops,
398 };
399
amdgpu_gem_are_domains_valid(u32 domains)400 static bool amdgpu_gem_are_domains_valid(u32 domains)
401 {
402 u32 normal = AMDGPU_GEM_DOMAIN_CPU |
403 AMDGPU_GEM_DOMAIN_GTT |
404 AMDGPU_GEM_DOMAIN_VRAM;
405 /* Treat all non CPU/GTT/VRAM domains as special domains. */
406 u32 special = AMDGPU_GEM_DOMAIN_MASK & ~normal;
407 u32 normal_mask = domains & normal;
408 u32 special_mask = domains & special;
409
410 if (!special_mask)
411 return true;
412
413 if (normal_mask)
414 return false;
415
416 return !(special_mask & (special_mask - 1));
417 }
418
419 /*
420 * GEM ioctls.
421 */
amdgpu_gem_create_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)422 int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
423 struct drm_file *filp)
424 {
425 struct amdgpu_device *adev = drm_to_adev(dev);
426 struct amdgpu_fpriv *fpriv = filp->driver_priv;
427 struct amdgpu_vm *vm = &fpriv->vm;
428 union drm_amdgpu_gem_create *args = data;
429 uint64_t flags = args->in.domain_flags;
430 uint64_t size = args->in.bo_size;
431 struct dma_resv *resv = NULL;
432 struct drm_gem_object *gobj;
433 uint32_t handle, initial_domain;
434 int r;
435
436 /* reject invalid gem flags */
437 if (flags & ~AMDGPU_GEM_CREATE_SETTABLE_MASK)
438 return -EINVAL;
439
440 /* reject invalid gem domains */
441 if (args->in.domains & ~AMDGPU_GEM_DOMAIN_MASK)
442 return -EINVAL;
443 if (!amdgpu_gem_are_domains_valid(args->in.domains))
444 return -EINVAL;
445
446 if (!amdgpu_is_tmz(adev) && (flags & AMDGPU_GEM_CREATE_ENCRYPTED)) {
447 DRM_NOTE_ONCE("Cannot allocate secure buffer since TMZ is disabled\n");
448 return -EINVAL;
449 }
450
451 /* always clear VRAM */
452 flags |= AMDGPU_GEM_CREATE_VRAM_CLEARED;
453
454 /* create a gem object to contain this object in */
455 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
456 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
457 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
458 /* if gds bo is created from user space, it must be
459 * passed to bo list
460 */
461 DRM_ERROR("GDS bo cannot be per-vm-bo\n");
462 return -EINVAL;
463 }
464 flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
465 }
466
467 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
468 r = amdgpu_bo_reserve(vm->root.bo, false);
469 if (r)
470 return r;
471
472 resv = vm->root.bo->tbo.base.resv;
473 }
474
475 initial_domain = (u32)(0xffffffff & args->in.domains);
476 retry:
477 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
478 initial_domain,
479 flags, ttm_bo_type_device, resv, &gobj, fpriv->xcp_id + 1);
480 if (r && r != -ERESTARTSYS) {
481 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
482 flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
483 goto retry;
484 }
485
486 if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
487 initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
488 goto retry;
489 }
490 DRM_DEBUG("Failed to allocate GEM object (%llu, %d, %llu, %d)\n",
491 size, initial_domain, args->in.alignment, r);
492 }
493
494 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
495 if (!r) {
496 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
497
498 abo->parent = amdgpu_bo_ref(vm->root.bo);
499 }
500 amdgpu_bo_unreserve(vm->root.bo);
501 }
502 if (r)
503 return r;
504
505 r = drm_gem_handle_create(filp, gobj, &handle);
506 /* drop reference from allocate - handle holds it now */
507 drm_gem_object_put(gobj);
508 if (r)
509 return r;
510
511 memset(args, 0, sizeof(*args));
512 args->out.handle = handle;
513 return 0;
514 }
515
amdgpu_gem_userptr_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)516 int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
517 struct drm_file *filp)
518 {
519 struct ttm_operation_ctx ctx = { true, false };
520 struct amdgpu_device *adev = drm_to_adev(dev);
521 struct drm_amdgpu_gem_userptr *args = data;
522 struct amdgpu_fpriv *fpriv = filp->driver_priv;
523 struct drm_gem_object *gobj;
524 struct amdgpu_hmm_range *range;
525 struct amdgpu_bo *bo;
526 uint32_t handle;
527 int r;
528
529 args->addr = untagged_addr(args->addr);
530
531 if (offset_in_page(args->addr | args->size))
532 return -EINVAL;
533
534 if (!access_ok((void __user *)(uintptr_t)args->addr, args->size))
535 return -EFAULT;
536
537 /* reject unknown flag values */
538 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
539 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
540 AMDGPU_GEM_USERPTR_REGISTER))
541 return -EINVAL;
542
543 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
544 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
545
546 /* if we want to write to it we must install a MMU notifier */
547 return -EACCES;
548 }
549
550 /* create a gem object to contain this object in */
551 r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
552 0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
553 if (r)
554 return r;
555
556 bo = gem_to_amdgpu_bo(gobj);
557 bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
558 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
559 bo->parent = amdgpu_bo_ref(fpriv->vm.root.bo);
560 r = amdgpu_ttm_tt_set_userptr(&bo->tbo, args->addr, args->flags);
561 if (r)
562 goto release_object;
563
564 r = amdgpu_hmm_register(bo, args->addr);
565 if (r)
566 goto release_object;
567
568 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
569 range = amdgpu_hmm_range_alloc(NULL);
570 if (unlikely(!range))
571 return -ENOMEM;
572 r = amdgpu_ttm_tt_get_user_pages(bo, range);
573 if (r) {
574 amdgpu_hmm_range_free(range);
575 goto release_object;
576 }
577 r = amdgpu_bo_reserve(bo, true);
578 if (r)
579 goto user_pages_done;
580
581 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range);
582
583 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
584 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
585 amdgpu_bo_unreserve(bo);
586 if (r)
587 goto user_pages_done;
588 }
589
590 r = drm_gem_handle_create(filp, gobj, &handle);
591 if (r)
592 goto user_pages_done;
593
594 args->handle = handle;
595
596 user_pages_done:
597 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE)
598 amdgpu_hmm_range_free(range);
599 release_object:
600 drm_gem_object_put(gobj);
601
602 return r;
603 }
604
amdgpu_mode_dumb_mmap(struct drm_file * filp,struct drm_device * dev,uint32_t handle,uint64_t * offset_p)605 int amdgpu_mode_dumb_mmap(struct drm_file *filp,
606 struct drm_device *dev,
607 uint32_t handle, uint64_t *offset_p)
608 {
609 struct drm_gem_object *gobj;
610 struct amdgpu_bo *robj;
611
612 gobj = drm_gem_object_lookup(filp, handle);
613 if (!gobj)
614 return -ENOENT;
615
616 robj = gem_to_amdgpu_bo(gobj);
617 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
618 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
619 drm_gem_object_put(gobj);
620 return -EPERM;
621 }
622 *offset_p = amdgpu_bo_mmap_offset(robj);
623 drm_gem_object_put(gobj);
624 return 0;
625 }
626
amdgpu_gem_mmap_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)627 int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
628 struct drm_file *filp)
629 {
630 union drm_amdgpu_gem_mmap *args = data;
631 uint32_t handle = args->in.handle;
632
633 memset(args, 0, sizeof(*args));
634 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
635 }
636
637 /**
638 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
639 *
640 * @timeout_ns: timeout in ns
641 *
642 * Calculate the timeout in jiffies from an absolute timeout in ns.
643 */
amdgpu_gem_timeout(uint64_t timeout_ns)644 unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
645 {
646 unsigned long timeout_jiffies;
647 ktime_t timeout;
648
649 /* clamp timeout if it's to large */
650 if (((int64_t)timeout_ns) < 0)
651 return MAX_SCHEDULE_TIMEOUT;
652
653 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
654 if (ktime_to_ns(timeout) < 0)
655 return 0;
656
657 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
658 /* clamp timeout to avoid unsigned-> signed overflow */
659 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT)
660 return MAX_SCHEDULE_TIMEOUT - 1;
661
662 return timeout_jiffies;
663 }
664
amdgpu_gem_wait_idle_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)665 int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
666 struct drm_file *filp)
667 {
668 union drm_amdgpu_gem_wait_idle *args = data;
669 struct drm_gem_object *gobj;
670 struct amdgpu_bo *robj;
671 uint32_t handle = args->in.handle;
672 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
673 int r = 0;
674 long ret;
675
676 gobj = drm_gem_object_lookup(filp, handle);
677 if (!gobj)
678 return -ENOENT;
679
680 robj = gem_to_amdgpu_bo(gobj);
681 ret = dma_resv_wait_timeout(robj->tbo.base.resv, DMA_RESV_USAGE_READ,
682 true, timeout);
683
684 /* ret == 0 means not signaled,
685 * ret > 0 means signaled
686 * ret < 0 means interrupted before timeout
687 */
688 if (ret >= 0) {
689 memset(args, 0, sizeof(*args));
690 args->out.status = (ret == 0);
691 } else
692 r = ret;
693
694 drm_gem_object_put(gobj);
695 return r;
696 }
697
amdgpu_gem_metadata_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)698 int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
699 struct drm_file *filp)
700 {
701 struct drm_amdgpu_gem_metadata *args = data;
702 struct drm_gem_object *gobj;
703 struct amdgpu_bo *robj;
704 int r = -1;
705
706 DRM_DEBUG("%d\n", args->handle);
707 gobj = drm_gem_object_lookup(filp, args->handle);
708 if (gobj == NULL)
709 return -ENOENT;
710 robj = gem_to_amdgpu_bo(gobj);
711
712 r = amdgpu_bo_reserve(robj, false);
713 if (unlikely(r != 0))
714 goto out;
715
716 /* Reject MMIO_REMAP BOs at IOCTL level: metadata/tiling does not apply. */
717 if (robj->tbo.resource &&
718 robj->tbo.resource->mem_type == AMDGPU_PL_MMIO_REMAP) {
719 DRM_WARN("metadata ioctl on MMIO_REMAP BO (handle %d)\n",
720 args->handle);
721 r = -EINVAL;
722 goto unreserve;
723 }
724
725 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
726 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
727 r = amdgpu_bo_get_metadata(robj, args->data.data,
728 sizeof(args->data.data),
729 &args->data.data_size_bytes,
730 &args->data.flags);
731 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
732 if (args->data.data_size_bytes > sizeof(args->data.data)) {
733 r = -EINVAL;
734 goto unreserve;
735 }
736 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
737 if (!r)
738 r = amdgpu_bo_set_metadata(robj, args->data.data,
739 args->data.data_size_bytes,
740 args->data.flags);
741 }
742
743 unreserve:
744 amdgpu_bo_unreserve(robj);
745 out:
746 drm_gem_object_put(gobj);
747 return r;
748 }
749
750 /**
751 * amdgpu_gem_va_update_vm -update the bo_va in its VM
752 *
753 * @adev: amdgpu_device pointer
754 * @vm: vm to update
755 * @bo_va: bo_va to update
756 * @operation: map, unmap or clear
757 *
758 * Update the bo_va directly after setting its address. Errors are not
759 * vital here, so they are not reported back to userspace.
760 *
761 * Returns resulting fence if freed BO(s) got cleared from the PT.
762 * otherwise stub fence in case of error.
763 */
764 static struct dma_fence *
amdgpu_gem_va_update_vm(struct amdgpu_device * adev,struct amdgpu_vm * vm,struct amdgpu_bo_va * bo_va,uint32_t operation)765 amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
766 struct amdgpu_vm *vm,
767 struct amdgpu_bo_va *bo_va,
768 uint32_t operation)
769 {
770 struct dma_fence *fence;
771 int r = 0;
772
773 /* If the VM is not ready return only a stub. */
774 if (!amdgpu_vm_ready(vm))
775 return dma_fence_get_stub();
776
777
778 /*
779 * First clean up any freed mappings in the VM.
780 *
781 * amdgpu_vm_clear_freed() may replace @fence with a new fence if it
782 * schedules GPU work. If nothing needs clearing, @fence can remain as
783 * the original vm->last_update.
784 */
785 r = amdgpu_vm_clear_freed(adev, vm, &vm->last_update);
786 if (r)
787 goto error;
788
789 /* For MAP/REPLACE we also need to update the BO mappings. */
790 if (operation == AMDGPU_VA_OP_MAP ||
791 operation == AMDGPU_VA_OP_REPLACE) {
792 r = amdgpu_vm_bo_update(adev, bo_va, false);
793 if (r)
794 goto error;
795 }
796
797 /* Always update PDEs after we touched the mappings. */
798 r = amdgpu_vm_update_pdes(adev, vm, false);
799 if (r)
800 goto error;
801
802 if ((operation == AMDGPU_VA_OP_MAP ||
803 operation == AMDGPU_VA_OP_REPLACE) &&
804 !amdgpu_vm_is_bo_always_valid(vm, bo_va->base.bo)) {
805
806 /*
807 * For MAP/REPLACE of non per-VM BOs we need to sync to both the
808 * bo_va->last_pt_update and vm->last_update or otherwise we
809 * potentially miss the PDE updates.
810 */
811 fence = dma_fence_unwrap_merge(vm->last_update,
812 bo_va->last_pt_update);
813 if (!fence) {
814 /* As fallback in OOM situations */
815 dma_fence_wait(vm->last_update, false);
816 dma_fence_wait(bo_va->last_pt_update, false);
817 fence = dma_fence_get_stub();
818 }
819 } else {
820 fence = dma_fence_get(vm->last_update);
821 }
822
823 return fence;
824
825 error:
826 if (r && r != -ERESTARTSYS)
827 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
828
829 return dma_fence_get(vm->last_update);
830 }
831
amdgpu_gem_va_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)832 int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
833 struct drm_file *filp)
834 {
835 const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
836 AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
837 AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK |
838 AMDGPU_VM_PAGE_NOALLOC;
839 const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
840 AMDGPU_VM_PAGE_PRT;
841
842 struct drm_amdgpu_gem_va *args = data;
843 struct drm_gem_object *gobj;
844 struct amdgpu_device *adev = drm_to_adev(dev);
845 struct amdgpu_fpriv *fpriv = filp->driver_priv;
846 struct amdgpu_bo *abo;
847 struct amdgpu_bo_va *bo_va;
848 struct drm_syncobj *timeline_syncobj = NULL;
849 struct dma_fence_chain *timeline_chain = NULL;
850 struct drm_exec exec;
851 uint64_t vm_size, tmp;
852 int r = 0;
853
854 /* Validate virtual address range against reserved regions. */
855 if (args->va_address < AMDGPU_VA_RESERVED_BOTTOM) {
856 dev_dbg(dev->dev,
857 "va_address 0x%llx is in reserved area 0x%llx\n",
858 args->va_address, AMDGPU_VA_RESERVED_BOTTOM);
859 return -EINVAL;
860 }
861
862 if (args->va_address >= AMDGPU_GMC_HOLE_START &&
863 args->va_address < AMDGPU_GMC_HOLE_END) {
864 dev_dbg(dev->dev,
865 "va_address 0x%llx is in VA hole 0x%llx-0x%llx\n",
866 args->va_address, AMDGPU_GMC_HOLE_START,
867 AMDGPU_GMC_HOLE_END);
868 return -EINVAL;
869 }
870
871 args->va_address &= AMDGPU_GMC_HOLE_MASK;
872
873 vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
874 vm_size -= AMDGPU_VA_RESERVED_TOP;
875 if (check_add_overflow(args->va_address, args->map_size, &tmp) || tmp > vm_size) {
876 dev_dbg(dev->dev,
877 "va_address 0x%llx is in top reserved area 0x%llx\n",
878 args->va_address + args->map_size, vm_size);
879 return -EINVAL;
880 }
881
882 if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
883 dev_dbg(dev->dev, "invalid flags combination 0x%08X\n",
884 args->flags);
885 return -EINVAL;
886 }
887
888 /* Validate operation type. */
889 switch (args->operation) {
890 case AMDGPU_VA_OP_MAP:
891 case AMDGPU_VA_OP_UNMAP:
892 case AMDGPU_VA_OP_CLEAR:
893 case AMDGPU_VA_OP_REPLACE:
894 break;
895 default:
896 dev_dbg(dev->dev, "unsupported operation %d\n",
897 args->operation);
898 return -EINVAL;
899 }
900
901 if (args->flags & AMDGPU_VM_DELAY_UPDATE &&
902 args->vm_timeline_syncobj_out)
903 return -EINVAL;
904
905 if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
906 !(args->flags & AMDGPU_VM_PAGE_PRT)) {
907 gobj = drm_gem_object_lookup(filp, args->handle);
908 if (gobj == NULL)
909 return -ENOENT;
910 abo = gem_to_amdgpu_bo(gobj);
911 } else {
912 gobj = NULL;
913 abo = NULL;
914 }
915
916 /* Add input syncobj fences (if any) for synchronization. */
917 r = amdgpu_gem_add_input_fence(filp,
918 args->input_fence_syncobj_handles,
919 args->num_syncobj_handles);
920 if (r)
921 goto error_put_gobj;
922
923 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
924 DRM_EXEC_IGNORE_DUPLICATES, 0);
925 drm_exec_until_all_locked(&exec) {
926 if (gobj) {
927 r = drm_exec_lock_obj(&exec, gobj);
928 drm_exec_retry_on_contention(&exec);
929 if (unlikely(r))
930 goto error;
931 }
932
933 r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 2);
934 drm_exec_retry_on_contention(&exec);
935 if (unlikely(r))
936 goto error;
937 }
938
939 /* Resolve the BO-VA mapping for this VM/BO combination. */
940 if (abo) {
941 bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
942 if (!bo_va) {
943 r = -ENOENT;
944 goto error;
945 }
946 } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
947 bo_va = fpriv->prt_va;
948 } else {
949 bo_va = NULL;
950 }
951
952 /*
953 * Prepare the timeline syncobj node if the user requested a VM
954 * timeline update. This only allocates/looks up the syncobj and
955 * chain node; the actual fence is attached later.
956 */
957 r = amdgpu_gem_update_timeline_node(filp,
958 args->vm_timeline_syncobj_out,
959 args->vm_timeline_point,
960 &timeline_syncobj,
961 &timeline_chain);
962 if (r)
963 goto error;
964
965 switch (args->operation) {
966 case AMDGPU_VA_OP_MAP:
967 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
968 args->offset_in_bo, args->map_size,
969 args->flags);
970 break;
971 case AMDGPU_VA_OP_UNMAP:
972 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
973 break;
974
975 case AMDGPU_VA_OP_CLEAR:
976 r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
977 args->va_address,
978 args->map_size);
979 break;
980 case AMDGPU_VA_OP_REPLACE:
981 r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
982 args->offset_in_bo, args->map_size,
983 args->flags);
984 break;
985 default:
986 break;
987 }
988
989 /*
990 * Once the VA operation is done, update the VM and obtain the fence
991 * that represents the last relevant update for this mapping. This
992 * fence can then be exported to the user-visible VM timeline.
993 */
994 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) &&
995 (!adev->debug_vm || timeline_syncobj)) {
996 struct dma_fence *fence;
997
998 fence = amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
999 args->operation);
1000 if (timeline_syncobj) {
1001 if (!args->vm_timeline_point) {
1002 /* Replace the existing fence when no point is given. */
1003 drm_syncobj_replace_fence(timeline_syncobj,
1004 fence);
1005 } else {
1006 /* Attach the last-update fence at a specific point. */
1007 drm_syncobj_add_point(timeline_syncobj,
1008 timeline_chain,
1009 fence,
1010 args->vm_timeline_point);
1011 timeline_chain = NULL;
1012 }
1013 }
1014 dma_fence_put(fence);
1015
1016 }
1017
1018 error:
1019 dma_fence_chain_free(timeline_chain);
1020 if (timeline_syncobj)
1021 drm_syncobj_put(timeline_syncobj);
1022 drm_exec_fini(&exec);
1023 error_put_gobj:
1024 drm_gem_object_put(gobj);
1025 return r;
1026 }
1027
amdgpu_gem_op_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)1028 int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
1029 struct drm_file *filp)
1030 {
1031 struct drm_amdgpu_gem_op *args = data;
1032 struct drm_gem_object *gobj;
1033 struct amdgpu_vm_bo_base *base;
1034 struct amdgpu_bo *robj;
1035 struct drm_exec exec;
1036 struct amdgpu_fpriv *fpriv = filp->driver_priv;
1037 int r;
1038
1039 if (args->padding)
1040 return -EINVAL;
1041
1042 gobj = drm_gem_object_lookup(filp, args->handle);
1043 if (!gobj)
1044 return -ENOENT;
1045
1046 robj = gem_to_amdgpu_bo(gobj);
1047
1048 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
1049 DRM_EXEC_IGNORE_DUPLICATES, 0);
1050 drm_exec_until_all_locked(&exec) {
1051 r = drm_exec_lock_obj(&exec, gobj);
1052 drm_exec_retry_on_contention(&exec);
1053 if (r)
1054 goto out_exec;
1055
1056 if (args->op == AMDGPU_GEM_OP_GET_MAPPING_INFO) {
1057 r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 0);
1058 drm_exec_retry_on_contention(&exec);
1059 if (r)
1060 goto out_exec;
1061 }
1062 }
1063
1064 switch (args->op) {
1065 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
1066 struct drm_amdgpu_gem_create_in info;
1067 void __user *out = u64_to_user_ptr(args->value);
1068
1069 info.bo_size = robj->tbo.base.size;
1070 info.alignment = robj->tbo.page_alignment << PAGE_SHIFT;
1071 info.domains = robj->preferred_domains;
1072 info.domain_flags = robj->flags;
1073 drm_exec_fini(&exec);
1074 if (copy_to_user(out, &info, sizeof(info)))
1075 r = -EFAULT;
1076 break;
1077 }
1078 case AMDGPU_GEM_OP_SET_PLACEMENT:
1079 if (drm_gem_is_imported(&robj->tbo.base) &&
1080 args->value & AMDGPU_GEM_DOMAIN_VRAM) {
1081 r = -EINVAL;
1082 goto out_exec;
1083 }
1084 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
1085 r = -EPERM;
1086 goto out_exec;
1087 }
1088 for (base = robj->vm_bo; base; base = base->next)
1089 if (amdgpu_xgmi_same_hive(amdgpu_ttm_adev(robj->tbo.bdev),
1090 amdgpu_ttm_adev(base->vm->root.bo->tbo.bdev))) {
1091 r = -EINVAL;
1092 goto out_exec;
1093 }
1094
1095
1096 robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
1097 AMDGPU_GEM_DOMAIN_GTT |
1098 AMDGPU_GEM_DOMAIN_CPU);
1099 robj->allowed_domains = robj->preferred_domains;
1100 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
1101 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
1102
1103 if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
1104 amdgpu_vm_bo_invalidate(robj, true);
1105 drm_exec_fini(&exec);
1106 break;
1107 case AMDGPU_GEM_OP_GET_MAPPING_INFO: {
1108 struct amdgpu_bo_va *bo_va = amdgpu_vm_bo_find(&fpriv->vm, robj);
1109 struct drm_amdgpu_gem_vm_entry *vm_entries;
1110 struct amdgpu_bo_va_mapping *mapping;
1111 int num_mappings = 0;
1112 /*
1113 * num_entries is set as an input to the size of the user-allocated array of
1114 * drm_amdgpu_gem_vm_entry stored at args->value.
1115 * num_entries is sent back as output as the number of mappings the bo has.
1116 * If that number is larger than the size of the array, the ioctl must
1117 * be retried.
1118 */
1119 if (!bo_va) {
1120 r = -ENOENT;
1121 goto out_exec;
1122 }
1123
1124 if (args->num_entries > INT_MAX / sizeof(*vm_entries)) {
1125 r = -EINVAL;
1126 goto out_exec;
1127 }
1128
1129 vm_entries = kvcalloc(args->num_entries, sizeof(*vm_entries), GFP_KERNEL);
1130 if (!vm_entries) {
1131 r = -ENOMEM;
1132 goto out_exec;
1133 }
1134
1135 amdgpu_vm_bo_va_for_each_valid_mapping(bo_va, mapping) {
1136 if (num_mappings < args->num_entries) {
1137 vm_entries[num_mappings].addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
1138 vm_entries[num_mappings].size = (mapping->last - mapping->start + 1) * AMDGPU_GPU_PAGE_SIZE;
1139 vm_entries[num_mappings].offset = mapping->offset;
1140 vm_entries[num_mappings].flags = mapping->flags;
1141 }
1142 num_mappings += 1;
1143 }
1144
1145 amdgpu_vm_bo_va_for_each_invalid_mapping(bo_va, mapping) {
1146 if (num_mappings < args->num_entries) {
1147 vm_entries[num_mappings].addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
1148 vm_entries[num_mappings].size = (mapping->last - mapping->start + 1) * AMDGPU_GPU_PAGE_SIZE;
1149 vm_entries[num_mappings].offset = mapping->offset;
1150 vm_entries[num_mappings].flags = mapping->flags;
1151 }
1152 num_mappings += 1;
1153 }
1154
1155 drm_exec_fini(&exec);
1156
1157 if (num_mappings > 0 && num_mappings <= args->num_entries)
1158 if (copy_to_user(u64_to_user_ptr(args->value), vm_entries, num_mappings * sizeof(*vm_entries)))
1159 r = -EFAULT;
1160
1161 args->num_entries = num_mappings;
1162
1163 kvfree(vm_entries);
1164 break;
1165 }
1166 default:
1167 drm_exec_fini(&exec);
1168 r = -EINVAL;
1169 }
1170
1171 drm_gem_object_put(gobj);
1172 return r;
1173 out_exec:
1174 drm_exec_fini(&exec);
1175 drm_gem_object_put(gobj);
1176 return r;
1177 }
1178
1179 /**
1180 * amdgpu_gem_list_handles_ioctl - get information about a process' buffer objects
1181 *
1182 * @dev: drm device pointer
1183 * @data: drm_amdgpu_gem_list_handles
1184 * @filp: drm file pointer
1185 *
1186 * num_entries is set as an input to the size of the entries array.
1187 * num_entries is sent back as output as the number of bos in the process.
1188 * If that number is larger than the size of the array, the ioctl must
1189 * be retried.
1190 *
1191 * Returns:
1192 * 0 for success, -errno for errors.
1193 */
amdgpu_gem_list_handles_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)1194 int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
1195 struct drm_file *filp)
1196 {
1197 struct drm_amdgpu_gem_list_handles *args = data;
1198 struct drm_amdgpu_gem_list_handles_entry *bo_entries;
1199 struct drm_gem_object *gobj;
1200 int id, ret = 0;
1201 int bo_index = 0;
1202 int num_bos = 0;
1203
1204 spin_lock(&filp->table_lock);
1205 idr_for_each_entry(&filp->object_idr, gobj, id)
1206 num_bos += 1;
1207 spin_unlock(&filp->table_lock);
1208
1209 if (args->num_entries < num_bos) {
1210 args->num_entries = num_bos;
1211 return 0;
1212 }
1213
1214 if (num_bos == 0) {
1215 args->num_entries = 0;
1216 return 0;
1217 }
1218
1219 bo_entries = kvzalloc_objs(*bo_entries, num_bos);
1220 if (!bo_entries)
1221 return -ENOMEM;
1222
1223 spin_lock(&filp->table_lock);
1224 idr_for_each_entry(&filp->object_idr, gobj, id) {
1225 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
1226 struct drm_amdgpu_gem_list_handles_entry *bo_entry;
1227
1228 if (bo_index >= num_bos) {
1229 ret = -EAGAIN;
1230 break;
1231 }
1232
1233 bo_entry = &bo_entries[bo_index];
1234
1235 bo_entry->size = amdgpu_bo_size(bo);
1236 bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
1237 bo_entry->preferred_domains = bo->preferred_domains;
1238 bo_entry->gem_handle = id;
1239 bo_entry->alignment = bo->tbo.page_alignment;
1240
1241 if (bo->tbo.base.import_attach)
1242 bo_entry->flags |= AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
1243
1244 bo_index += 1;
1245 }
1246 spin_unlock(&filp->table_lock);
1247
1248 args->num_entries = bo_index;
1249
1250 if (!ret)
1251 if (copy_to_user(u64_to_user_ptr(args->entries), bo_entries, num_bos * sizeof(*bo_entries)))
1252 ret = -EFAULT;
1253
1254 kvfree(bo_entries);
1255
1256 return ret;
1257 }
1258
amdgpu_gem_align_pitch(struct amdgpu_device * adev,unsigned int width,unsigned int cpp,bool tiled)1259 static unsigned int amdgpu_gem_align_pitch(struct amdgpu_device *adev,
1260 unsigned int width,
1261 unsigned int cpp,
1262 bool tiled)
1263 {
1264 unsigned int aligned = width;
1265 unsigned int pitch_mask = 0;
1266 unsigned int pitch;
1267
1268 switch (cpp) {
1269 case 1:
1270 pitch_mask = 255;
1271 break;
1272 case 2:
1273 pitch_mask = 127;
1274 break;
1275 case 3:
1276 case 4:
1277 pitch_mask = 63;
1278 break;
1279 }
1280
1281 if (check_add_overflow(aligned, pitch_mask, &aligned))
1282 return 0;
1283 aligned &= ~pitch_mask;
1284 if (check_mul_overflow(aligned, cpp, &pitch))
1285 return 0;
1286 return pitch;
1287 }
1288
amdgpu_mode_dumb_create(struct drm_file * file_priv,struct drm_device * dev,struct drm_mode_create_dumb * args)1289 int amdgpu_mode_dumb_create(struct drm_file *file_priv,
1290 struct drm_device *dev,
1291 struct drm_mode_create_dumb *args)
1292 {
1293 struct amdgpu_device *adev = drm_to_adev(dev);
1294 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
1295 struct drm_gem_object *gobj;
1296 uint32_t handle;
1297 u64 flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
1298 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
1299 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1300 u32 domain;
1301 int r;
1302
1303 /*
1304 * The buffer returned from this function should be cleared, but
1305 * it can only be done if the ring is enabled or we'll fail to
1306 * create the buffer.
1307 */
1308 if (adev->mman.buffer_funcs_enabled)
1309 flags |= AMDGPU_GEM_CREATE_VRAM_CLEARED;
1310
1311 args->pitch = amdgpu_gem_align_pitch(adev, args->width,
1312 DIV_ROUND_UP(args->bpp, 8), 0);
1313 if (!args->pitch)
1314 return -EINVAL;
1315 args->size = (u64)args->pitch * args->height;
1316 args->size = ALIGN(args->size, PAGE_SIZE);
1317 if (!args->size)
1318 return -EINVAL;
1319 domain = amdgpu_bo_get_preferred_domain(adev,
1320 amdgpu_display_supported_domains(adev, flags));
1321 r = amdgpu_gem_object_create(adev, args->size, 0, domain, flags,
1322 ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
1323 if (r)
1324 return -ENOMEM;
1325
1326 r = drm_gem_handle_create(file_priv, gobj, &handle);
1327 /* drop reference from allocate - handle holds it now */
1328 drm_gem_object_put(gobj);
1329 if (r)
1330 return r;
1331
1332 args->handle = handle;
1333 return 0;
1334 }
1335
1336 #if defined(CONFIG_DEBUG_FS)
amdgpu_debugfs_gem_info_show(struct seq_file * m,void * unused)1337 static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
1338 {
1339 struct amdgpu_device *adev = m->private;
1340 struct drm_device *dev = adev_to_drm(adev);
1341 struct drm_file *file;
1342 int r;
1343
1344 r = mutex_lock_interruptible(&dev->filelist_mutex);
1345 if (r)
1346 return r;
1347
1348 list_for_each_entry(file, &dev->filelist, lhead) {
1349 struct task_struct *task;
1350 struct drm_gem_object *gobj;
1351 struct pid *pid;
1352 int id;
1353
1354 /*
1355 * Although we have a valid reference on file->pid, that does
1356 * not guarantee that the task_struct who called get_pid() is
1357 * still alive (e.g. get_pid(current) => fork() => exit()).
1358 * Therefore, we need to protect this ->comm access using RCU.
1359 */
1360 rcu_read_lock();
1361 pid = rcu_dereference(file->pid);
1362 task = pid_task(pid, PIDTYPE_TGID);
1363 seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
1364 task ? task->comm : "<unknown>");
1365 rcu_read_unlock();
1366
1367 spin_lock(&file->table_lock);
1368 idr_for_each_entry(&file->object_idr, gobj, id) {
1369 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
1370
1371 amdgpu_bo_print_info(id, bo, m);
1372 }
1373 spin_unlock(&file->table_lock);
1374 }
1375
1376 mutex_unlock(&dev->filelist_mutex);
1377 return 0;
1378 }
1379
1380 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_gem_info);
1381
1382 #endif
1383
amdgpu_debugfs_gem_init(struct amdgpu_device * adev)1384 void amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
1385 {
1386 #if defined(CONFIG_DEBUG_FS)
1387 struct drm_minor *minor = adev_to_drm(adev)->primary;
1388 struct dentry *root = minor->debugfs_root;
1389
1390 debugfs_create_file("amdgpu_gem_info", 0444, root, adev,
1391 &amdgpu_debugfs_gem_info_fops);
1392 #endif
1393 }
1394