at commit 3c516a56

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