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
400 /*
401 * GEM ioctls.
402 */
amdgpu_gem_create_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)403 int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data,
404 struct drm_file *filp)
405 {
406 struct amdgpu_device *adev = drm_to_adev(dev);
407 struct amdgpu_fpriv *fpriv = filp->driver_priv;
408 struct amdgpu_vm *vm = &fpriv->vm;
409 union drm_amdgpu_gem_create *args = data;
410 uint64_t flags = args->in.domain_flags;
411 uint64_t size = args->in.bo_size;
412 struct dma_resv *resv = NULL;
413 struct drm_gem_object *gobj;
414 uint32_t handle, initial_domain;
415 int r;
416
417 /* reject invalid gem flags */
418 if (flags & ~AMDGPU_GEM_CREATE_SETTABLE_MASK)
419 return -EINVAL;
420
421 /* reject invalid gem domains */
422 if (args->in.domains & ~AMDGPU_GEM_DOMAIN_MASK)
423 return -EINVAL;
424
425 if (!amdgpu_is_tmz(adev) && (flags & AMDGPU_GEM_CREATE_ENCRYPTED)) {
426 DRM_NOTE_ONCE("Cannot allocate secure buffer since TMZ is disabled\n");
427 return -EINVAL;
428 }
429
430 /* always clear VRAM */
431 flags |= AMDGPU_GEM_CREATE_VRAM_CLEARED;
432
433 /* create a gem object to contain this object in */
434 if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS |
435 AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) {
436 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
437 /* if gds bo is created from user space, it must be
438 * passed to bo list
439 */
440 DRM_ERROR("GDS bo cannot be per-vm-bo\n");
441 return -EINVAL;
442 }
443 flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS;
444 }
445
446 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
447 r = amdgpu_bo_reserve(vm->root.bo, false);
448 if (r)
449 return r;
450
451 resv = vm->root.bo->tbo.base.resv;
452 }
453
454 initial_domain = (u32)(0xffffffff & args->in.domains);
455 retry:
456 r = amdgpu_gem_object_create(adev, size, args->in.alignment,
457 initial_domain,
458 flags, ttm_bo_type_device, resv, &gobj, fpriv->xcp_id + 1);
459 if (r && r != -ERESTARTSYS) {
460 if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) {
461 flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED;
462 goto retry;
463 }
464
465 if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) {
466 initial_domain |= AMDGPU_GEM_DOMAIN_GTT;
467 goto retry;
468 }
469 DRM_DEBUG("Failed to allocate GEM object (%llu, %d, %llu, %d)\n",
470 size, initial_domain, args->in.alignment, r);
471 }
472
473 if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) {
474 if (!r) {
475 struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj);
476
477 abo->parent = amdgpu_bo_ref(vm->root.bo);
478 }
479 amdgpu_bo_unreserve(vm->root.bo);
480 }
481 if (r)
482 return r;
483
484 r = drm_gem_handle_create(filp, gobj, &handle);
485 /* drop reference from allocate - handle holds it now */
486 drm_gem_object_put(gobj);
487 if (r)
488 return r;
489
490 memset(args, 0, sizeof(*args));
491 args->out.handle = handle;
492 return 0;
493 }
494
amdgpu_gem_userptr_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)495 int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
496 struct drm_file *filp)
497 {
498 struct ttm_operation_ctx ctx = { true, false };
499 struct amdgpu_device *adev = drm_to_adev(dev);
500 struct drm_amdgpu_gem_userptr *args = data;
501 struct amdgpu_fpriv *fpriv = filp->driver_priv;
502 struct drm_gem_object *gobj;
503 struct amdgpu_hmm_range *range;
504 struct amdgpu_bo *bo;
505 uint32_t handle;
506 int r;
507
508 args->addr = untagged_addr(args->addr);
509
510 if (offset_in_page(args->addr | args->size))
511 return -EINVAL;
512
513 if (!access_ok((void __user *)(uintptr_t)args->addr, args->size))
514 return -EFAULT;
515
516 /* reject unknown flag values */
517 if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY |
518 AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE |
519 AMDGPU_GEM_USERPTR_REGISTER))
520 return -EINVAL;
521
522 if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) &&
523 !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) {
524
525 /* if we want to write to it we must install a MMU notifier */
526 return -EACCES;
527 }
528
529 /* create a gem object to contain this object in */
530 r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU,
531 0, ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
532 if (r)
533 return r;
534
535 bo = gem_to_amdgpu_bo(gobj);
536 bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT;
537 bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT;
538 r = amdgpu_ttm_tt_set_userptr(&bo->tbo, args->addr, args->flags);
539 if (r)
540 goto release_object;
541
542 r = amdgpu_hmm_register(bo, args->addr);
543 if (r)
544 goto release_object;
545
546 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
547 range = amdgpu_hmm_range_alloc(NULL);
548 if (unlikely(!range))
549 return -ENOMEM;
550 r = amdgpu_ttm_tt_get_user_pages(bo, range);
551 if (r) {
552 amdgpu_hmm_range_free(range);
553 goto release_object;
554 }
555 r = amdgpu_bo_reserve(bo, true);
556 if (r)
557 goto user_pages_done;
558
559 amdgpu_ttm_tt_set_user_pages(bo->tbo.ttm, range);
560
561 amdgpu_bo_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT);
562 r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx);
563 amdgpu_bo_unreserve(bo);
564 if (r)
565 goto user_pages_done;
566 }
567
568 r = drm_gem_handle_create(filp, gobj, &handle);
569 if (r)
570 goto user_pages_done;
571
572 args->handle = handle;
573
574 user_pages_done:
575 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE)
576 amdgpu_hmm_range_free(range);
577 release_object:
578 drm_gem_object_put(gobj);
579
580 return r;
581 }
582
amdgpu_mode_dumb_mmap(struct drm_file * filp,struct drm_device * dev,uint32_t handle,uint64_t * offset_p)583 int amdgpu_mode_dumb_mmap(struct drm_file *filp,
584 struct drm_device *dev,
585 uint32_t handle, uint64_t *offset_p)
586 {
587 struct drm_gem_object *gobj;
588 struct amdgpu_bo *robj;
589
590 gobj = drm_gem_object_lookup(filp, handle);
591 if (!gobj)
592 return -ENOENT;
593
594 robj = gem_to_amdgpu_bo(gobj);
595 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) ||
596 (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) {
597 drm_gem_object_put(gobj);
598 return -EPERM;
599 }
600 *offset_p = amdgpu_bo_mmap_offset(robj);
601 drm_gem_object_put(gobj);
602 return 0;
603 }
604
amdgpu_gem_mmap_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)605 int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data,
606 struct drm_file *filp)
607 {
608 union drm_amdgpu_gem_mmap *args = data;
609 uint32_t handle = args->in.handle;
610
611 memset(args, 0, sizeof(*args));
612 return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr);
613 }
614
615 /**
616 * amdgpu_gem_timeout - calculate jiffies timeout from absolute value
617 *
618 * @timeout_ns: timeout in ns
619 *
620 * Calculate the timeout in jiffies from an absolute timeout in ns.
621 */
amdgpu_gem_timeout(uint64_t timeout_ns)622 unsigned long amdgpu_gem_timeout(uint64_t timeout_ns)
623 {
624 unsigned long timeout_jiffies;
625 ktime_t timeout;
626
627 /* clamp timeout if it's to large */
628 if (((int64_t)timeout_ns) < 0)
629 return MAX_SCHEDULE_TIMEOUT;
630
631 timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get());
632 if (ktime_to_ns(timeout) < 0)
633 return 0;
634
635 timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout));
636 /* clamp timeout to avoid unsigned-> signed overflow */
637 if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT)
638 return MAX_SCHEDULE_TIMEOUT - 1;
639
640 return timeout_jiffies;
641 }
642
amdgpu_gem_wait_idle_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)643 int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data,
644 struct drm_file *filp)
645 {
646 union drm_amdgpu_gem_wait_idle *args = data;
647 struct drm_gem_object *gobj;
648 struct amdgpu_bo *robj;
649 uint32_t handle = args->in.handle;
650 unsigned long timeout = amdgpu_gem_timeout(args->in.timeout);
651 int r = 0;
652 long ret;
653
654 gobj = drm_gem_object_lookup(filp, handle);
655 if (!gobj)
656 return -ENOENT;
657
658 robj = gem_to_amdgpu_bo(gobj);
659 ret = dma_resv_wait_timeout(robj->tbo.base.resv, DMA_RESV_USAGE_READ,
660 true, timeout);
661
662 /* ret == 0 means not signaled,
663 * ret > 0 means signaled
664 * ret < 0 means interrupted before timeout
665 */
666 if (ret >= 0) {
667 memset(args, 0, sizeof(*args));
668 args->out.status = (ret == 0);
669 } else
670 r = ret;
671
672 drm_gem_object_put(gobj);
673 return r;
674 }
675
amdgpu_gem_metadata_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)676 int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data,
677 struct drm_file *filp)
678 {
679 struct drm_amdgpu_gem_metadata *args = data;
680 struct drm_gem_object *gobj;
681 struct amdgpu_bo *robj;
682 int r = -1;
683
684 DRM_DEBUG("%d\n", args->handle);
685 gobj = drm_gem_object_lookup(filp, args->handle);
686 if (gobj == NULL)
687 return -ENOENT;
688 robj = gem_to_amdgpu_bo(gobj);
689
690 r = amdgpu_bo_reserve(robj, false);
691 if (unlikely(r != 0))
692 goto out;
693
694 /* Reject MMIO_REMAP BOs at IOCTL level: metadata/tiling does not apply. */
695 if (robj->tbo.resource &&
696 robj->tbo.resource->mem_type == AMDGPU_PL_MMIO_REMAP) {
697 DRM_WARN("metadata ioctl on MMIO_REMAP BO (handle %d)\n",
698 args->handle);
699 r = -EINVAL;
700 goto unreserve;
701 }
702
703 if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) {
704 amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info);
705 r = amdgpu_bo_get_metadata(robj, args->data.data,
706 sizeof(args->data.data),
707 &args->data.data_size_bytes,
708 &args->data.flags);
709 } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) {
710 if (args->data.data_size_bytes > sizeof(args->data.data)) {
711 r = -EINVAL;
712 goto unreserve;
713 }
714 r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info);
715 if (!r)
716 r = amdgpu_bo_set_metadata(robj, args->data.data,
717 args->data.data_size_bytes,
718 args->data.flags);
719 }
720
721 unreserve:
722 amdgpu_bo_unreserve(robj);
723 out:
724 drm_gem_object_put(gobj);
725 return r;
726 }
727
728 /**
729 * amdgpu_gem_va_update_vm -update the bo_va in its VM
730 *
731 * @adev: amdgpu_device pointer
732 * @vm: vm to update
733 * @bo_va: bo_va to update
734 * @operation: map, unmap or clear
735 *
736 * Update the bo_va directly after setting its address. Errors are not
737 * vital here, so they are not reported back to userspace.
738 *
739 * Returns resulting fence if freed BO(s) got cleared from the PT.
740 * otherwise stub fence in case of error.
741 */
742 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)743 amdgpu_gem_va_update_vm(struct amdgpu_device *adev,
744 struct amdgpu_vm *vm,
745 struct amdgpu_bo_va *bo_va,
746 uint32_t operation)
747 {
748 struct dma_fence *fence;
749 int r = 0;
750
751 /* If the VM is not ready return only a stub. */
752 if (!amdgpu_vm_ready(vm))
753 return dma_fence_get_stub();
754
755
756 /*
757 * First clean up any freed mappings in the VM.
758 *
759 * amdgpu_vm_clear_freed() may replace @fence with a new fence if it
760 * schedules GPU work. If nothing needs clearing, @fence can remain as
761 * the original vm->last_update.
762 */
763 r = amdgpu_vm_clear_freed(adev, vm, &vm->last_update);
764 if (r)
765 goto error;
766
767 /* For MAP/REPLACE we also need to update the BO mappings. */
768 if (operation == AMDGPU_VA_OP_MAP ||
769 operation == AMDGPU_VA_OP_REPLACE) {
770 r = amdgpu_vm_bo_update(adev, bo_va, false);
771 if (r)
772 goto error;
773 }
774
775 /* Always update PDEs after we touched the mappings. */
776 r = amdgpu_vm_update_pdes(adev, vm, false);
777 if (r)
778 goto error;
779
780 if ((operation == AMDGPU_VA_OP_MAP ||
781 operation == AMDGPU_VA_OP_REPLACE) &&
782 !amdgpu_vm_is_bo_always_valid(vm, bo_va->base.bo)) {
783
784 /*
785 * For MAP/REPLACE of non per-VM BOs we need to sync to both the
786 * bo_va->last_pt_update and vm->last_update or otherwise we
787 * potentially miss the PDE updates.
788 */
789 fence = dma_fence_unwrap_merge(vm->last_update,
790 bo_va->last_pt_update);
791 if (!fence) {
792 /* As fallback in OOM situations */
793 dma_fence_wait(vm->last_update, false);
794 dma_fence_wait(bo_va->last_pt_update, false);
795 fence = dma_fence_get_stub();
796 }
797 } else {
798 fence = dma_fence_get(vm->last_update);
799 }
800
801 return fence;
802
803 error:
804 if (r && r != -ERESTARTSYS)
805 DRM_ERROR("Couldn't update BO_VA (%d)\n", r);
806
807 return dma_fence_get(vm->last_update);
808 }
809
amdgpu_gem_va_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)810 int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data,
811 struct drm_file *filp)
812 {
813 const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE |
814 AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE |
815 AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK |
816 AMDGPU_VM_PAGE_NOALLOC;
817 const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE |
818 AMDGPU_VM_PAGE_PRT;
819
820 struct drm_amdgpu_gem_va *args = data;
821 struct drm_gem_object *gobj;
822 struct amdgpu_device *adev = drm_to_adev(dev);
823 struct amdgpu_fpriv *fpriv = filp->driver_priv;
824 struct amdgpu_bo *abo;
825 struct amdgpu_bo_va *bo_va;
826 struct drm_syncobj *timeline_syncobj = NULL;
827 struct dma_fence_chain *timeline_chain = NULL;
828 struct drm_exec exec;
829 uint64_t vm_size, tmp;
830 int r = 0;
831
832 /* Validate virtual address range against reserved regions. */
833 if (args->va_address < AMDGPU_VA_RESERVED_BOTTOM) {
834 dev_dbg(dev->dev,
835 "va_address 0x%llx is in reserved area 0x%llx\n",
836 args->va_address, AMDGPU_VA_RESERVED_BOTTOM);
837 return -EINVAL;
838 }
839
840 if (args->va_address >= AMDGPU_GMC_HOLE_START &&
841 args->va_address < AMDGPU_GMC_HOLE_END) {
842 dev_dbg(dev->dev,
843 "va_address 0x%llx is in VA hole 0x%llx-0x%llx\n",
844 args->va_address, AMDGPU_GMC_HOLE_START,
845 AMDGPU_GMC_HOLE_END);
846 return -EINVAL;
847 }
848
849 args->va_address &= AMDGPU_GMC_HOLE_MASK;
850
851 vm_size = adev->vm_manager.max_pfn * AMDGPU_GPU_PAGE_SIZE;
852 vm_size -= AMDGPU_VA_RESERVED_TOP;
853 if (check_add_overflow(args->va_address, args->map_size, &tmp) || tmp > vm_size) {
854 dev_dbg(dev->dev,
855 "va_address 0x%llx is in top reserved area 0x%llx\n",
856 args->va_address + args->map_size, vm_size);
857 return -EINVAL;
858 }
859
860 if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) {
861 dev_dbg(dev->dev, "invalid flags combination 0x%08X\n",
862 args->flags);
863 return -EINVAL;
864 }
865
866 /* Validate operation type. */
867 switch (args->operation) {
868 case AMDGPU_VA_OP_MAP:
869 case AMDGPU_VA_OP_UNMAP:
870 case AMDGPU_VA_OP_CLEAR:
871 case AMDGPU_VA_OP_REPLACE:
872 break;
873 default:
874 dev_dbg(dev->dev, "unsupported operation %d\n",
875 args->operation);
876 return -EINVAL;
877 }
878
879 if (args->flags & AMDGPU_VM_DELAY_UPDATE &&
880 args->vm_timeline_syncobj_out)
881 return -EINVAL;
882
883 if ((args->operation != AMDGPU_VA_OP_CLEAR) &&
884 !(args->flags & AMDGPU_VM_PAGE_PRT)) {
885 gobj = drm_gem_object_lookup(filp, args->handle);
886 if (gobj == NULL)
887 return -ENOENT;
888 abo = gem_to_amdgpu_bo(gobj);
889 } else {
890 gobj = NULL;
891 abo = NULL;
892 }
893
894 /* Add input syncobj fences (if any) for synchronization. */
895 r = amdgpu_gem_add_input_fence(filp,
896 args->input_fence_syncobj_handles,
897 args->num_syncobj_handles);
898 if (r)
899 goto error_put_gobj;
900
901 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
902 DRM_EXEC_IGNORE_DUPLICATES, 0);
903 drm_exec_until_all_locked(&exec) {
904 if (gobj) {
905 r = drm_exec_lock_obj(&exec, gobj);
906 drm_exec_retry_on_contention(&exec);
907 if (unlikely(r))
908 goto error;
909 }
910
911 r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 2);
912 drm_exec_retry_on_contention(&exec);
913 if (unlikely(r))
914 goto error;
915 }
916
917 /* Resolve the BO-VA mapping for this VM/BO combination. */
918 if (abo) {
919 bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo);
920 if (!bo_va) {
921 r = -ENOENT;
922 goto error;
923 }
924 } else if (args->operation != AMDGPU_VA_OP_CLEAR) {
925 bo_va = fpriv->prt_va;
926 } else {
927 bo_va = NULL;
928 }
929
930 /*
931 * Prepare the timeline syncobj node if the user requested a VM
932 * timeline update. This only allocates/looks up the syncobj and
933 * chain node; the actual fence is attached later.
934 */
935 r = amdgpu_gem_update_timeline_node(filp,
936 args->vm_timeline_syncobj_out,
937 args->vm_timeline_point,
938 &timeline_syncobj,
939 &timeline_chain);
940 if (r)
941 goto error;
942
943 switch (args->operation) {
944 case AMDGPU_VA_OP_MAP:
945 r = amdgpu_vm_bo_map(adev, bo_va, args->va_address,
946 args->offset_in_bo, args->map_size,
947 args->flags);
948 break;
949 case AMDGPU_VA_OP_UNMAP:
950 r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address);
951 break;
952
953 case AMDGPU_VA_OP_CLEAR:
954 r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm,
955 args->va_address,
956 args->map_size);
957 break;
958 case AMDGPU_VA_OP_REPLACE:
959 r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address,
960 args->offset_in_bo, args->map_size,
961 args->flags);
962 break;
963 default:
964 break;
965 }
966
967 /*
968 * Once the VA operation is done, update the VM and obtain the fence
969 * that represents the last relevant update for this mapping. This
970 * fence can then be exported to the user-visible VM timeline.
971 */
972 if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) &&
973 (!adev->debug_vm || timeline_syncobj)) {
974 struct dma_fence *fence;
975
976 fence = amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va,
977 args->operation);
978 if (timeline_syncobj) {
979 if (!args->vm_timeline_point) {
980 /* Replace the existing fence when no point is given. */
981 drm_syncobj_replace_fence(timeline_syncobj,
982 fence);
983 } else {
984 /* Attach the last-update fence at a specific point. */
985 drm_syncobj_add_point(timeline_syncobj,
986 timeline_chain,
987 fence,
988 args->vm_timeline_point);
989 timeline_chain = NULL;
990 }
991 }
992 dma_fence_put(fence);
993
994 }
995
996 error:
997 dma_fence_chain_free(timeline_chain);
998 if (timeline_syncobj)
999 drm_syncobj_put(timeline_syncobj);
1000 drm_exec_fini(&exec);
1001 error_put_gobj:
1002 drm_gem_object_put(gobj);
1003 return r;
1004 }
1005
amdgpu_gem_op_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)1006 int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data,
1007 struct drm_file *filp)
1008 {
1009 struct drm_amdgpu_gem_op *args = data;
1010 struct drm_gem_object *gobj;
1011 struct amdgpu_vm_bo_base *base;
1012 struct amdgpu_bo *robj;
1013 struct drm_exec exec;
1014 struct amdgpu_fpriv *fpriv = filp->driver_priv;
1015 int r;
1016
1017 if (args->padding)
1018 return -EINVAL;
1019
1020 gobj = drm_gem_object_lookup(filp, args->handle);
1021 if (!gobj)
1022 return -ENOENT;
1023
1024 robj = gem_to_amdgpu_bo(gobj);
1025
1026 drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT |
1027 DRM_EXEC_IGNORE_DUPLICATES, 0);
1028 drm_exec_until_all_locked(&exec) {
1029 r = drm_exec_lock_obj(&exec, gobj);
1030 drm_exec_retry_on_contention(&exec);
1031 if (r)
1032 goto out_exec;
1033
1034 if (args->op == AMDGPU_GEM_OP_GET_MAPPING_INFO) {
1035 r = amdgpu_vm_lock_pd(&fpriv->vm, &exec, 0);
1036 drm_exec_retry_on_contention(&exec);
1037 if (r)
1038 goto out_exec;
1039 }
1040 }
1041
1042 switch (args->op) {
1043 case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: {
1044 struct drm_amdgpu_gem_create_in info;
1045 void __user *out = u64_to_user_ptr(args->value);
1046
1047 info.bo_size = robj->tbo.base.size;
1048 info.alignment = robj->tbo.page_alignment << PAGE_SHIFT;
1049 info.domains = robj->preferred_domains;
1050 info.domain_flags = robj->flags;
1051 drm_exec_fini(&exec);
1052 if (copy_to_user(out, &info, sizeof(info)))
1053 r = -EFAULT;
1054 break;
1055 }
1056 case AMDGPU_GEM_OP_SET_PLACEMENT:
1057 if (drm_gem_is_imported(&robj->tbo.base) &&
1058 args->value & AMDGPU_GEM_DOMAIN_VRAM) {
1059 r = -EINVAL;
1060 goto out_exec;
1061 }
1062 if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) {
1063 r = -EPERM;
1064 goto out_exec;
1065 }
1066 for (base = robj->vm_bo; base; base = base->next)
1067 if (amdgpu_xgmi_same_hive(amdgpu_ttm_adev(robj->tbo.bdev),
1068 amdgpu_ttm_adev(base->vm->root.bo->tbo.bdev))) {
1069 r = -EINVAL;
1070 goto out_exec;
1071 }
1072
1073
1074 robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM |
1075 AMDGPU_GEM_DOMAIN_GTT |
1076 AMDGPU_GEM_DOMAIN_CPU);
1077 robj->allowed_domains = robj->preferred_domains;
1078 if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM)
1079 robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT;
1080
1081 if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID)
1082 amdgpu_vm_bo_invalidate(robj, true);
1083 drm_exec_fini(&exec);
1084 break;
1085 case AMDGPU_GEM_OP_GET_MAPPING_INFO: {
1086 struct amdgpu_bo_va *bo_va = amdgpu_vm_bo_find(&fpriv->vm, robj);
1087 struct drm_amdgpu_gem_vm_entry *vm_entries;
1088 struct amdgpu_bo_va_mapping *mapping;
1089 int num_mappings = 0;
1090 /*
1091 * num_entries is set as an input to the size of the user-allocated array of
1092 * drm_amdgpu_gem_vm_entry stored at args->value.
1093 * num_entries is sent back as output as the number of mappings the bo has.
1094 * If that number is larger than the size of the array, the ioctl must
1095 * be retried.
1096 */
1097 if (!bo_va) {
1098 r = -ENOENT;
1099 goto out_exec;
1100 }
1101
1102 if (args->num_entries > INT_MAX / sizeof(*vm_entries)) {
1103 r = -EINVAL;
1104 goto out_exec;
1105 }
1106
1107 vm_entries = kvcalloc(args->num_entries, sizeof(*vm_entries), GFP_KERNEL);
1108 if (!vm_entries) {
1109 r = -ENOMEM;
1110 goto out_exec;
1111 }
1112
1113 amdgpu_vm_bo_va_for_each_valid_mapping(bo_va, mapping) {
1114 if (num_mappings < args->num_entries) {
1115 vm_entries[num_mappings].addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
1116 vm_entries[num_mappings].size = (mapping->last - mapping->start + 1) * AMDGPU_GPU_PAGE_SIZE;
1117 vm_entries[num_mappings].offset = mapping->offset;
1118 vm_entries[num_mappings].flags = mapping->flags;
1119 }
1120 num_mappings += 1;
1121 }
1122
1123 amdgpu_vm_bo_va_for_each_invalid_mapping(bo_va, mapping) {
1124 if (num_mappings < args->num_entries) {
1125 vm_entries[num_mappings].addr = mapping->start * AMDGPU_GPU_PAGE_SIZE;
1126 vm_entries[num_mappings].size = (mapping->last - mapping->start + 1) * AMDGPU_GPU_PAGE_SIZE;
1127 vm_entries[num_mappings].offset = mapping->offset;
1128 vm_entries[num_mappings].flags = mapping->flags;
1129 }
1130 num_mappings += 1;
1131 }
1132
1133 drm_exec_fini(&exec);
1134
1135 if (num_mappings > 0 && num_mappings <= args->num_entries)
1136 if (copy_to_user(u64_to_user_ptr(args->value), vm_entries, num_mappings * sizeof(*vm_entries)))
1137 r = -EFAULT;
1138
1139 args->num_entries = num_mappings;
1140
1141 kvfree(vm_entries);
1142 break;
1143 }
1144 default:
1145 drm_exec_fini(&exec);
1146 r = -EINVAL;
1147 }
1148
1149 drm_gem_object_put(gobj);
1150 return r;
1151 out_exec:
1152 drm_exec_fini(&exec);
1153 drm_gem_object_put(gobj);
1154 return r;
1155 }
1156
1157 /**
1158 * amdgpu_gem_list_handles_ioctl - get information about a process' buffer objects
1159 *
1160 * @dev: drm device pointer
1161 * @data: drm_amdgpu_gem_list_handles
1162 * @filp: drm file pointer
1163 *
1164 * num_entries is set as an input to the size of the entries array.
1165 * num_entries is sent back as output as the number of bos in the process.
1166 * If that number is larger than the size of the array, the ioctl must
1167 * be retried.
1168 *
1169 * Returns:
1170 * 0 for success, -errno for errors.
1171 */
amdgpu_gem_list_handles_ioctl(struct drm_device * dev,void * data,struct drm_file * filp)1172 int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
1173 struct drm_file *filp)
1174 {
1175 struct drm_amdgpu_gem_list_handles *args = data;
1176 struct drm_amdgpu_gem_list_handles_entry *bo_entries;
1177 struct drm_gem_object *gobj;
1178 int id, ret = 0;
1179 int bo_index = 0;
1180 int num_bos = 0;
1181
1182 spin_lock(&filp->table_lock);
1183 idr_for_each_entry(&filp->object_idr, gobj, id)
1184 num_bos += 1;
1185 spin_unlock(&filp->table_lock);
1186
1187 if (args->num_entries < num_bos) {
1188 args->num_entries = num_bos;
1189 return 0;
1190 }
1191
1192 if (num_bos == 0) {
1193 args->num_entries = 0;
1194 return 0;
1195 }
1196
1197 bo_entries = kvzalloc_objs(*bo_entries, num_bos);
1198 if (!bo_entries)
1199 return -ENOMEM;
1200
1201 spin_lock(&filp->table_lock);
1202 idr_for_each_entry(&filp->object_idr, gobj, id) {
1203 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
1204 struct drm_amdgpu_gem_list_handles_entry *bo_entry;
1205
1206 if (bo_index >= num_bos) {
1207 ret = -EAGAIN;
1208 break;
1209 }
1210
1211 bo_entry = &bo_entries[bo_index];
1212
1213 bo_entry->size = amdgpu_bo_size(bo);
1214 bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
1215 bo_entry->preferred_domains = bo->preferred_domains;
1216 bo_entry->gem_handle = id;
1217 bo_entry->alignment = bo->tbo.page_alignment;
1218
1219 if (bo->tbo.base.import_attach)
1220 bo_entry->flags |= AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
1221
1222 bo_index += 1;
1223 }
1224 spin_unlock(&filp->table_lock);
1225
1226 args->num_entries = bo_index;
1227
1228 if (!ret)
1229 if (copy_to_user(u64_to_user_ptr(args->entries), bo_entries, num_bos * sizeof(*bo_entries)))
1230 ret = -EFAULT;
1231
1232 kvfree(bo_entries);
1233
1234 return ret;
1235 }
1236
amdgpu_gem_align_pitch(struct amdgpu_device * adev,unsigned int width,unsigned int cpp,bool tiled)1237 static unsigned int amdgpu_gem_align_pitch(struct amdgpu_device *adev,
1238 unsigned int width,
1239 unsigned int cpp,
1240 bool tiled)
1241 {
1242 unsigned int aligned = width;
1243 unsigned int pitch_mask = 0;
1244 unsigned int pitch;
1245
1246 switch (cpp) {
1247 case 1:
1248 pitch_mask = 255;
1249 break;
1250 case 2:
1251 pitch_mask = 127;
1252 break;
1253 case 3:
1254 case 4:
1255 pitch_mask = 63;
1256 break;
1257 }
1258
1259 if (check_add_overflow(aligned, pitch_mask, &aligned))
1260 return 0;
1261 aligned &= ~pitch_mask;
1262 if (check_mul_overflow(aligned, cpp, &pitch))
1263 return 0;
1264 return pitch;
1265 }
1266
amdgpu_mode_dumb_create(struct drm_file * file_priv,struct drm_device * dev,struct drm_mode_create_dumb * args)1267 int amdgpu_mode_dumb_create(struct drm_file *file_priv,
1268 struct drm_device *dev,
1269 struct drm_mode_create_dumb *args)
1270 {
1271 struct amdgpu_device *adev = drm_to_adev(dev);
1272 struct amdgpu_fpriv *fpriv = file_priv->driver_priv;
1273 struct drm_gem_object *gobj;
1274 uint32_t handle;
1275 u64 flags = AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
1276 AMDGPU_GEM_CREATE_CPU_GTT_USWC |
1277 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS;
1278 u32 domain;
1279 int r;
1280
1281 /*
1282 * The buffer returned from this function should be cleared, but
1283 * it can only be done if the ring is enabled or we'll fail to
1284 * create the buffer.
1285 */
1286 if (adev->mman.buffer_funcs_enabled)
1287 flags |= AMDGPU_GEM_CREATE_VRAM_CLEARED;
1288
1289 args->pitch = amdgpu_gem_align_pitch(adev, args->width,
1290 DIV_ROUND_UP(args->bpp, 8), 0);
1291 if (!args->pitch)
1292 return -EINVAL;
1293 args->size = (u64)args->pitch * args->height;
1294 args->size = ALIGN(args->size, PAGE_SIZE);
1295 if (!args->size)
1296 return -EINVAL;
1297 domain = amdgpu_bo_get_preferred_domain(adev,
1298 amdgpu_display_supported_domains(adev, flags));
1299 r = amdgpu_gem_object_create(adev, args->size, 0, domain, flags,
1300 ttm_bo_type_device, NULL, &gobj, fpriv->xcp_id + 1);
1301 if (r)
1302 return -ENOMEM;
1303
1304 r = drm_gem_handle_create(file_priv, gobj, &handle);
1305 /* drop reference from allocate - handle holds it now */
1306 drm_gem_object_put(gobj);
1307 if (r)
1308 return r;
1309
1310 args->handle = handle;
1311 return 0;
1312 }
1313
1314 #if defined(CONFIG_DEBUG_FS)
amdgpu_debugfs_gem_info_show(struct seq_file * m,void * unused)1315 static int amdgpu_debugfs_gem_info_show(struct seq_file *m, void *unused)
1316 {
1317 struct amdgpu_device *adev = m->private;
1318 struct drm_device *dev = adev_to_drm(adev);
1319 struct drm_file *file;
1320 int r;
1321
1322 r = mutex_lock_interruptible(&dev->filelist_mutex);
1323 if (r)
1324 return r;
1325
1326 list_for_each_entry(file, &dev->filelist, lhead) {
1327 struct task_struct *task;
1328 struct drm_gem_object *gobj;
1329 struct pid *pid;
1330 int id;
1331
1332 /*
1333 * Although we have a valid reference on file->pid, that does
1334 * not guarantee that the task_struct who called get_pid() is
1335 * still alive (e.g. get_pid(current) => fork() => exit()).
1336 * Therefore, we need to protect this ->comm access using RCU.
1337 */
1338 rcu_read_lock();
1339 pid = rcu_dereference(file->pid);
1340 task = pid_task(pid, PIDTYPE_TGID);
1341 seq_printf(m, "pid %8d command %s:\n", pid_nr(pid),
1342 task ? task->comm : "<unknown>");
1343 rcu_read_unlock();
1344
1345 spin_lock(&file->table_lock);
1346 idr_for_each_entry(&file->object_idr, gobj, id) {
1347 struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj);
1348
1349 amdgpu_bo_print_info(id, bo, m);
1350 }
1351 spin_unlock(&file->table_lock);
1352 }
1353
1354 mutex_unlock(&dev->filelist_mutex);
1355 return 0;
1356 }
1357
1358 DEFINE_SHOW_ATTRIBUTE(amdgpu_debugfs_gem_info);
1359
1360 #endif
1361
amdgpu_debugfs_gem_init(struct amdgpu_device * adev)1362 void amdgpu_debugfs_gem_init(struct amdgpu_device *adev)
1363 {
1364 #if defined(CONFIG_DEBUG_FS)
1365 struct drm_minor *minor = adev_to_drm(adev)->primary;
1366 struct dentry *root = minor->debugfs_root;
1367
1368 debugfs_create_file("amdgpu_gem_info", 0444, root, adev,
1369 &amdgpu_debugfs_gem_info_fops);
1370 #endif
1371 }
1372