Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 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> |
Stephen Rothwell | 568d7c7 | 2016-03-17 15:30:49 +1100 | [diff] [blame] | 29 | #include <linux/pagemap.h> |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 30 | #include <drm/drmP.h> |
| 31 | #include <drm/amdgpu_drm.h> |
| 32 | #include "amdgpu.h" |
Deepak Sharma | bda31a2 | 2018-05-22 15:31:23 -0700 | [diff] [blame] | 33 | #include "amdgpu_display.h" |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 34 | |
| 35 | void amdgpu_gem_object_free(struct drm_gem_object *gobj) |
| 36 | { |
| 37 | struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj); |
| 38 | |
| 39 | if (robj) { |
Christian König | 9298e52 | 2015-06-03 21:31:20 +0200 | [diff] [blame] | 40 | amdgpu_mn_unregister(robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 41 | amdgpu_bo_unref(&robj); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size, |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 46 | int alignment, u32 initial_domain, |
Christian König | eab3de2 | 2018-03-14 14:48:17 -0500 | [diff] [blame] | 47 | u64 flags, enum ttm_bo_type type, |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 48 | struct reservation_object *resv, |
| 49 | struct drm_gem_object **obj) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 50 | { |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 51 | struct amdgpu_bo *bo; |
Chunming Zhou | 3216c6b | 2018-04-16 18:27:50 +0800 | [diff] [blame] | 52 | struct amdgpu_bo_param bp; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 53 | int r; |
| 54 | |
Chunming Zhou | 3216c6b | 2018-04-16 18:27:50 +0800 | [diff] [blame] | 55 | memset(&bp, 0, sizeof(bp)); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 56 | *obj = NULL; |
| 57 | /* At least align on page size */ |
| 58 | if (alignment < PAGE_SIZE) { |
| 59 | alignment = PAGE_SIZE; |
| 60 | } |
| 61 | |
Chunming Zhou | 3216c6b | 2018-04-16 18:27:50 +0800 | [diff] [blame] | 62 | bp.size = size; |
| 63 | bp.byte_align = alignment; |
| 64 | bp.type = type; |
| 65 | bp.resv = resv; |
Chunming Zhou | aa2b2e2 | 2018-04-17 11:52:53 +0800 | [diff] [blame] | 66 | bp.preferred_domain = initial_domain; |
Christian König | 0808210 | 2018-04-10 13:42:38 +0200 | [diff] [blame] | 67 | retry: |
Chunming Zhou | 3216c6b | 2018-04-16 18:27:50 +0800 | [diff] [blame] | 68 | bp.flags = flags; |
| 69 | bp.domain = initial_domain; |
| 70 | r = amdgpu_bo_create(adev, &bp, &bo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 71 | if (r) { |
Christian König | 0808210 | 2018-04-10 13:42:38 +0200 | [diff] [blame] | 72 | if (r != -ERESTARTSYS) { |
| 73 | if (flags & AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED) { |
| 74 | flags &= ~AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED; |
| 75 | goto retry; |
| 76 | } |
| 77 | |
| 78 | if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) { |
| 79 | initial_domain |= AMDGPU_GEM_DOMAIN_GTT; |
| 80 | goto retry; |
| 81 | } |
| 82 | DRM_DEBUG("Failed to allocate GEM object (%ld, %d, %u, %d)\n", |
| 83 | size, initial_domain, alignment, r); |
| 84 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 85 | return r; |
| 86 | } |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 87 | *obj = &bo->gem_base; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 88 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
Christian König | 418aa0c | 2016-02-15 16:59:57 +0100 | [diff] [blame] | 92 | void amdgpu_gem_force_release(struct amdgpu_device *adev) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 93 | { |
Christian König | 418aa0c | 2016-02-15 16:59:57 +0100 | [diff] [blame] | 94 | struct drm_device *ddev = adev->ddev; |
| 95 | struct drm_file *file; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 96 | |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 97 | mutex_lock(&ddev->filelist_mutex); |
Christian König | 418aa0c | 2016-02-15 16:59:57 +0100 | [diff] [blame] | 98 | |
| 99 | list_for_each_entry(file, &ddev->filelist, lhead) { |
| 100 | struct drm_gem_object *gobj; |
| 101 | int handle; |
| 102 | |
| 103 | WARN_ONCE(1, "Still active user space clients!\n"); |
| 104 | spin_lock(&file->table_lock); |
| 105 | idr_for_each_entry(&file->object_idr, gobj, handle) { |
| 106 | WARN_ONCE(1, "And also active allocations!\n"); |
Cihangir Akturk | f62facc | 2017-08-03 14:58:16 +0300 | [diff] [blame] | 107 | drm_gem_object_put_unlocked(gobj); |
Christian König | 418aa0c | 2016-02-15 16:59:57 +0100 | [diff] [blame] | 108 | } |
| 109 | idr_destroy(&file->object_idr); |
| 110 | spin_unlock(&file->table_lock); |
| 111 | } |
| 112 | |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 113 | mutex_unlock(&ddev->filelist_mutex); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Call from drm_gem_handle_create which appear in both new and open ioctl |
| 118 | * case. |
| 119 | */ |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 120 | int amdgpu_gem_object_open(struct drm_gem_object *obj, |
| 121 | struct drm_file *file_priv) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 122 | { |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 123 | struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj); |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 124 | struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 125 | struct amdgpu_fpriv *fpriv = file_priv->driver_priv; |
| 126 | struct amdgpu_vm *vm = &fpriv->vm; |
| 127 | struct amdgpu_bo_va *bo_va; |
Christian König | 4f5839c | 2017-08-29 16:07:31 +0200 | [diff] [blame] | 128 | struct mm_struct *mm; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 129 | int r; |
Christian König | 4f5839c | 2017-08-29 16:07:31 +0200 | [diff] [blame] | 130 | |
| 131 | mm = amdgpu_ttm_tt_get_usermm(abo->tbo.ttm); |
| 132 | if (mm && mm != current->mm) |
| 133 | return -EPERM; |
| 134 | |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 135 | if (abo->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID && |
| 136 | abo->tbo.resv != vm->root.base.bo->tbo.resv) |
| 137 | return -EPERM; |
| 138 | |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 139 | r = amdgpu_bo_reserve(abo, false); |
Chunming Zhou | e98c1b0 | 2015-11-13 15:22:04 +0800 | [diff] [blame] | 140 | if (r) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 141 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 142 | |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 143 | bo_va = amdgpu_vm_bo_find(vm, abo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 144 | if (!bo_va) { |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 145 | bo_va = amdgpu_vm_bo_add(adev, vm, abo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 146 | } else { |
| 147 | ++bo_va->ref_count; |
| 148 | } |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 149 | amdgpu_bo_unreserve(abo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | void amdgpu_gem_object_close(struct drm_gem_object *obj, |
| 154 | struct drm_file *file_priv) |
| 155 | { |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 156 | struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 157 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 158 | struct amdgpu_fpriv *fpriv = file_priv->driver_priv; |
| 159 | struct amdgpu_vm *vm = &fpriv->vm; |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 160 | |
| 161 | struct amdgpu_bo_list_entry vm_pd; |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 162 | struct list_head list, duplicates; |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 163 | struct ttm_validate_buffer tv; |
| 164 | struct ww_acquire_ctx ticket; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 165 | struct amdgpu_bo_va *bo_va; |
| 166 | int r; |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 167 | |
| 168 | INIT_LIST_HEAD(&list); |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 169 | INIT_LIST_HEAD(&duplicates); |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 170 | |
| 171 | tv.bo = &bo->tbo; |
| 172 | tv.shared = true; |
| 173 | list_add(&tv.head, &list); |
| 174 | |
| 175 | amdgpu_vm_get_pd_bo(vm, &list, &vm_pd); |
| 176 | |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 177 | r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 178 | if (r) { |
| 179 | dev_err(adev->dev, "leaking bo va because " |
| 180 | "we fail to reserve bo (%d)\n", r); |
| 181 | return; |
| 182 | } |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 183 | bo_va = amdgpu_vm_bo_find(vm, bo); |
Christian König | 5a0f3b5 | 2017-04-21 10:05:56 +0200 | [diff] [blame] | 184 | if (bo_va && --bo_va->ref_count == 0) { |
| 185 | amdgpu_vm_bo_rmv(adev, bo_va); |
| 186 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 187 | if (amdgpu_vm_ready(vm)) { |
Christian König | 5a0f3b5 | 2017-04-21 10:05:56 +0200 | [diff] [blame] | 188 | struct dma_fence *fence = NULL; |
Nicolai Hähnle | 23e0563 | 2017-03-23 19:34:11 +0100 | [diff] [blame] | 189 | |
| 190 | r = amdgpu_vm_clear_freed(adev, vm, &fence); |
| 191 | if (unlikely(r)) { |
| 192 | dev_err(adev->dev, "failed to clear page " |
| 193 | "tables on GEM object close (%d)\n", r); |
| 194 | } |
| 195 | |
| 196 | if (fence) { |
| 197 | amdgpu_bo_fence(bo, fence, true); |
| 198 | dma_fence_put(fence); |
| 199 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 200 | } |
| 201 | } |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 202 | ttm_eu_backoff_reservation(&ticket, &list); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 203 | } |
| 204 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 205 | /* |
| 206 | * GEM ioctls. |
| 207 | */ |
| 208 | int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data, |
| 209 | struct drm_file *filp) |
| 210 | { |
| 211 | struct amdgpu_device *adev = dev->dev_private; |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 212 | struct amdgpu_fpriv *fpriv = filp->driver_priv; |
| 213 | struct amdgpu_vm *vm = &fpriv->vm; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 214 | union drm_amdgpu_gem_create *args = data; |
Christian König | 6ac7def | 2017-08-23 20:11:25 +0200 | [diff] [blame] | 215 | uint64_t flags = args->in.domain_flags; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 216 | uint64_t size = args->in.bo_size; |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 217 | struct reservation_object *resv = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 218 | struct drm_gem_object *gobj; |
| 219 | uint32_t handle; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 220 | int r; |
| 221 | |
Alex Deucher | 834e0f8 | 2017-03-08 17:40:17 -0500 | [diff] [blame] | 222 | /* reject invalid gem flags */ |
Christian König | 6ac7def | 2017-08-23 20:11:25 +0200 | [diff] [blame] | 223 | if (flags & ~(AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED | |
| 224 | AMDGPU_GEM_CREATE_NO_CPU_ACCESS | |
| 225 | AMDGPU_GEM_CREATE_CPU_GTT_USWC | |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 226 | AMDGPU_GEM_CREATE_VRAM_CLEARED | |
Andres Rodriguez | 177ae09 | 2017-09-15 20:44:06 -0400 | [diff] [blame] | 227 | AMDGPU_GEM_CREATE_VM_ALWAYS_VALID | |
| 228 | AMDGPU_GEM_CREATE_EXPLICIT_SYNC)) |
| 229 | |
Christian König | a022c54 | 2017-05-08 15:14:54 +0200 | [diff] [blame] | 230 | return -EINVAL; |
| 231 | |
Alex Deucher | 834e0f8 | 2017-03-08 17:40:17 -0500 | [diff] [blame] | 232 | /* reject invalid gem domains */ |
Chunming Zhou | 3f18845 | 2018-04-17 18:34:40 +0800 | [diff] [blame] | 233 | if (args->in.domains & ~AMDGPU_GEM_DOMAIN_MASK) |
Christian König | a022c54 | 2017-05-08 15:14:54 +0200 | [diff] [blame] | 234 | return -EINVAL; |
Alex Deucher | 834e0f8 | 2017-03-08 17:40:17 -0500 | [diff] [blame] | 235 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 236 | /* create a gem object to contain this object in */ |
| 237 | if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS | |
| 238 | AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) { |
Chunming Zhou | ee5309d | 2018-05-30 11:12:08 +0800 | [diff] [blame] | 239 | if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) { |
| 240 | /* if gds bo is created from user space, it must be |
| 241 | * passed to bo list |
| 242 | */ |
| 243 | DRM_ERROR("GDS bo cannot be per-vm-bo\n"); |
| 244 | return -EINVAL; |
| 245 | } |
Christian König | 6ac7def | 2017-08-23 20:11:25 +0200 | [diff] [blame] | 246 | flags |= AMDGPU_GEM_CREATE_NO_CPU_ACCESS; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 247 | if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS) |
| 248 | size = size << AMDGPU_GDS_SHIFT; |
| 249 | else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS) |
| 250 | size = size << AMDGPU_GWS_SHIFT; |
| 251 | else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA) |
| 252 | size = size << AMDGPU_OA_SHIFT; |
Christian König | a022c54 | 2017-05-08 15:14:54 +0200 | [diff] [blame] | 253 | else |
| 254 | return -EINVAL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 255 | } |
| 256 | size = roundup(size, PAGE_SIZE); |
| 257 | |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 258 | if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) { |
| 259 | r = amdgpu_bo_reserve(vm->root.base.bo, false); |
| 260 | if (r) |
| 261 | return r; |
| 262 | |
| 263 | resv = vm->root.base.bo->tbo.resv; |
| 264 | } |
| 265 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 266 | r = amdgpu_gem_object_create(adev, size, args->in.alignment, |
| 267 | (u32)(0xffffffff & args->in.domains), |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 268 | flags, false, resv, &gobj); |
| 269 | if (flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) { |
| 270 | if (!r) { |
| 271 | struct amdgpu_bo *abo = gem_to_amdgpu_bo(gobj); |
| 272 | |
| 273 | abo->parent = amdgpu_bo_ref(vm->root.base.bo); |
| 274 | } |
| 275 | amdgpu_bo_unreserve(vm->root.base.bo); |
| 276 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 277 | if (r) |
Christian König | a022c54 | 2017-05-08 15:14:54 +0200 | [diff] [blame] | 278 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 279 | |
| 280 | r = drm_gem_handle_create(filp, gobj, &handle); |
| 281 | /* drop reference from allocate - handle holds it now */ |
Cihangir Akturk | f62facc | 2017-08-03 14:58:16 +0300 | [diff] [blame] | 282 | drm_gem_object_put_unlocked(gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 283 | if (r) |
Christian König | a022c54 | 2017-05-08 15:14:54 +0200 | [diff] [blame] | 284 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 285 | |
| 286 | memset(args, 0, sizeof(*args)); |
| 287 | args->out.handle = handle; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 288 | return 0; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data, |
| 292 | struct drm_file *filp) |
| 293 | { |
Christian König | 19be557 | 2017-04-12 14:24:39 +0200 | [diff] [blame] | 294 | struct ttm_operation_ctx ctx = { true, false }; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 295 | struct amdgpu_device *adev = dev->dev_private; |
| 296 | struct drm_amdgpu_gem_userptr *args = data; |
| 297 | struct drm_gem_object *gobj; |
| 298 | struct amdgpu_bo *bo; |
| 299 | uint32_t handle; |
| 300 | int r; |
| 301 | |
| 302 | if (offset_in_page(args->addr | args->size)) |
| 303 | return -EINVAL; |
| 304 | |
| 305 | /* reject unknown flag values */ |
| 306 | if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY | |
| 307 | AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE | |
| 308 | AMDGPU_GEM_USERPTR_REGISTER)) |
| 309 | return -EINVAL; |
| 310 | |
Christian König | 358c258 | 2016-03-11 15:29:27 +0100 | [diff] [blame] | 311 | if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) && |
| 312 | !(args->flags & AMDGPU_GEM_USERPTR_REGISTER)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 313 | |
Christian König | 358c258 | 2016-03-11 15:29:27 +0100 | [diff] [blame] | 314 | /* if we want to write to it we must install a MMU notifier */ |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 315 | return -EACCES; |
| 316 | } |
| 317 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 318 | /* create a gem object to contain this object in */ |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 319 | r = amdgpu_gem_object_create(adev, args->size, 0, AMDGPU_GEM_DOMAIN_CPU, |
| 320 | 0, 0, NULL, &gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 321 | if (r) |
Christian König | a022c54 | 2017-05-08 15:14:54 +0200 | [diff] [blame] | 322 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 323 | |
| 324 | bo = gem_to_amdgpu_bo(gobj); |
Kent Russell | 6d7d9c5 | 2017-08-08 07:58:01 -0400 | [diff] [blame] | 325 | bo->preferred_domains = AMDGPU_GEM_DOMAIN_GTT; |
Christian König | 1ea863f | 2015-12-18 22:13:12 +0100 | [diff] [blame] | 326 | bo->allowed_domains = AMDGPU_GEM_DOMAIN_GTT; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 327 | r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags); |
| 328 | if (r) |
| 329 | goto release_object; |
| 330 | |
| 331 | if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) { |
| 332 | r = amdgpu_mn_register(bo, args->addr); |
| 333 | if (r) |
| 334 | goto release_object; |
| 335 | } |
| 336 | |
| 337 | if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) { |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 338 | r = amdgpu_ttm_tt_get_user_pages(bo->tbo.ttm, |
| 339 | bo->tbo.ttm->pages); |
| 340 | if (r) |
Xiangliang.Yu | d5a480b | 2017-10-20 17:21:40 +0800 | [diff] [blame] | 341 | goto release_object; |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 342 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 343 | r = amdgpu_bo_reserve(bo, true); |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 344 | if (r) |
| 345 | goto free_pages; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 346 | |
| 347 | amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT); |
Christian König | 19be557 | 2017-04-12 14:24:39 +0200 | [diff] [blame] | 348 | r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 349 | amdgpu_bo_unreserve(bo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 350 | if (r) |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 351 | goto free_pages; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | r = drm_gem_handle_create(filp, gobj, &handle); |
| 355 | /* drop reference from allocate - handle holds it now */ |
Cihangir Akturk | f62facc | 2017-08-03 14:58:16 +0300 | [diff] [blame] | 356 | drm_gem_object_put_unlocked(gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 357 | if (r) |
Christian König | a022c54 | 2017-05-08 15:14:54 +0200 | [diff] [blame] | 358 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 359 | |
| 360 | args->handle = handle; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 361 | return 0; |
| 362 | |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 363 | free_pages: |
Mel Gorman | c6f92f9 | 2017-11-15 17:37:55 -0800 | [diff] [blame] | 364 | release_pages(bo->tbo.ttm->pages, bo->tbo.ttm->num_pages); |
Christian König | 2f568db | 2016-02-23 12:36:59 +0100 | [diff] [blame] | 365 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 366 | release_object: |
Cihangir Akturk | f62facc | 2017-08-03 14:58:16 +0300 | [diff] [blame] | 367 | drm_gem_object_put_unlocked(gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 368 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 369 | return r; |
| 370 | } |
| 371 | |
| 372 | int amdgpu_mode_dumb_mmap(struct drm_file *filp, |
| 373 | struct drm_device *dev, |
| 374 | uint32_t handle, uint64_t *offset_p) |
| 375 | { |
| 376 | struct drm_gem_object *gobj; |
| 377 | struct amdgpu_bo *robj; |
| 378 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 379 | gobj = drm_gem_object_lookup(filp, handle); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 380 | if (gobj == NULL) { |
| 381 | return -ENOENT; |
| 382 | } |
| 383 | robj = gem_to_amdgpu_bo(gobj); |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 384 | if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) || |
Christian König | 271c812 | 2015-05-13 14:30:53 +0200 | [diff] [blame] | 385 | (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) { |
Cihangir Akturk | f62facc | 2017-08-03 14:58:16 +0300 | [diff] [blame] | 386 | drm_gem_object_put_unlocked(gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 387 | return -EPERM; |
| 388 | } |
| 389 | *offset_p = amdgpu_bo_mmap_offset(robj); |
Cihangir Akturk | f62facc | 2017-08-03 14:58:16 +0300 | [diff] [blame] | 390 | drm_gem_object_put_unlocked(gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data, |
| 395 | struct drm_file *filp) |
| 396 | { |
| 397 | union drm_amdgpu_gem_mmap *args = data; |
| 398 | uint32_t handle = args->in.handle; |
| 399 | memset(args, 0, sizeof(*args)); |
| 400 | return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr); |
| 401 | } |
| 402 | |
| 403 | /** |
| 404 | * amdgpu_gem_timeout - calculate jiffies timeout from absolute value |
| 405 | * |
| 406 | * @timeout_ns: timeout in ns |
| 407 | * |
| 408 | * Calculate the timeout in jiffies from an absolute timeout in ns. |
| 409 | */ |
| 410 | unsigned long amdgpu_gem_timeout(uint64_t timeout_ns) |
| 411 | { |
| 412 | unsigned long timeout_jiffies; |
| 413 | ktime_t timeout; |
| 414 | |
| 415 | /* clamp timeout if it's to large */ |
| 416 | if (((int64_t)timeout_ns) < 0) |
| 417 | return MAX_SCHEDULE_TIMEOUT; |
| 418 | |
Christian König | 0f11770 | 2015-07-08 16:58:48 +0200 | [diff] [blame] | 419 | timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get()); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 420 | if (ktime_to_ns(timeout) < 0) |
| 421 | return 0; |
| 422 | |
| 423 | timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout)); |
| 424 | /* clamp timeout to avoid unsigned-> signed overflow */ |
| 425 | if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT ) |
| 426 | return MAX_SCHEDULE_TIMEOUT - 1; |
| 427 | |
| 428 | return timeout_jiffies; |
| 429 | } |
| 430 | |
| 431 | int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data, |
| 432 | struct drm_file *filp) |
| 433 | { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 434 | union drm_amdgpu_gem_wait_idle *args = data; |
| 435 | struct drm_gem_object *gobj; |
| 436 | struct amdgpu_bo *robj; |
| 437 | uint32_t handle = args->in.handle; |
| 438 | unsigned long timeout = amdgpu_gem_timeout(args->in.timeout); |
| 439 | int r = 0; |
| 440 | long ret; |
| 441 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 442 | gobj = drm_gem_object_lookup(filp, handle); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 443 | if (gobj == NULL) { |
| 444 | return -ENOENT; |
| 445 | } |
| 446 | robj = gem_to_amdgpu_bo(gobj); |
Chris Wilson | 0fea2ed | 2016-08-29 08:08:24 +0100 | [diff] [blame] | 447 | ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, |
| 448 | timeout); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 449 | |
| 450 | /* ret == 0 means not signaled, |
| 451 | * ret > 0 means signaled |
| 452 | * ret < 0 means interrupted before timeout |
| 453 | */ |
| 454 | if (ret >= 0) { |
| 455 | memset(args, 0, sizeof(*args)); |
| 456 | args->out.status = (ret == 0); |
| 457 | } else |
| 458 | r = ret; |
| 459 | |
Cihangir Akturk | f62facc | 2017-08-03 14:58:16 +0300 | [diff] [blame] | 460 | drm_gem_object_put_unlocked(gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 461 | return r; |
| 462 | } |
| 463 | |
| 464 | int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data, |
| 465 | struct drm_file *filp) |
| 466 | { |
| 467 | struct drm_amdgpu_gem_metadata *args = data; |
| 468 | struct drm_gem_object *gobj; |
| 469 | struct amdgpu_bo *robj; |
| 470 | int r = -1; |
| 471 | |
| 472 | DRM_DEBUG("%d \n", args->handle); |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 473 | gobj = drm_gem_object_lookup(filp, args->handle); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 474 | if (gobj == NULL) |
| 475 | return -ENOENT; |
| 476 | robj = gem_to_amdgpu_bo(gobj); |
| 477 | |
| 478 | r = amdgpu_bo_reserve(robj, false); |
| 479 | if (unlikely(r != 0)) |
| 480 | goto out; |
| 481 | |
| 482 | if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) { |
| 483 | amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info); |
| 484 | r = amdgpu_bo_get_metadata(robj, args->data.data, |
| 485 | sizeof(args->data.data), |
| 486 | &args->data.data_size_bytes, |
| 487 | &args->data.flags); |
| 488 | } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) { |
Dan Carpenter | 0913eab | 2015-09-23 14:00:35 +0300 | [diff] [blame] | 489 | if (args->data.data_size_bytes > sizeof(args->data.data)) { |
| 490 | r = -EINVAL; |
| 491 | goto unreserve; |
| 492 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 493 | r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info); |
| 494 | if (!r) |
| 495 | r = amdgpu_bo_set_metadata(robj, args->data.data, |
| 496 | args->data.data_size_bytes, |
| 497 | args->data.flags); |
| 498 | } |
| 499 | |
Dan Carpenter | 0913eab | 2015-09-23 14:00:35 +0300 | [diff] [blame] | 500 | unreserve: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 501 | amdgpu_bo_unreserve(robj); |
| 502 | out: |
Cihangir Akturk | f62facc | 2017-08-03 14:58:16 +0300 | [diff] [blame] | 503 | drm_gem_object_put_unlocked(gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 504 | return r; |
| 505 | } |
| 506 | |
| 507 | /** |
| 508 | * amdgpu_gem_va_update_vm -update the bo_va in its VM |
| 509 | * |
| 510 | * @adev: amdgpu_device pointer |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 511 | * @vm: vm to update |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 512 | * @bo_va: bo_va to update |
Christian König | 2ffdaaf | 2017-01-27 15:58:43 +0100 | [diff] [blame] | 513 | * @list: validation list |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 514 | * @operation: map, unmap or clear |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 515 | * |
Christian König | 2ffdaaf | 2017-01-27 15:58:43 +0100 | [diff] [blame] | 516 | * Update the bo_va directly after setting its address. Errors are not |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 517 | * vital here, so they are not reported back to userspace. |
| 518 | */ |
| 519 | static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev, |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 520 | struct amdgpu_vm *vm, |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 521 | struct amdgpu_bo_va *bo_va, |
Christian König | 2ffdaaf | 2017-01-27 15:58:43 +0100 | [diff] [blame] | 522 | struct list_head *list, |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame] | 523 | uint32_t operation) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 524 | { |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 525 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 526 | |
Christian König | 3f3333f | 2017-08-03 14:02:13 +0200 | [diff] [blame] | 527 | if (!amdgpu_vm_ready(vm)) |
| 528 | return; |
Chunming Zhou | e410b5c | 2015-12-07 15:02:52 +0800 | [diff] [blame] | 529 | |
Nicolai Hähnle | f346781 | 2017-03-23 19:36:31 +0100 | [diff] [blame] | 530 | r = amdgpu_vm_clear_freed(adev, vm, NULL); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 531 | if (r) |
Christian König | 2ffdaaf | 2017-01-27 15:58:43 +0100 | [diff] [blame] | 532 | goto error; |
monk.liu | 194a336 | 2015-07-22 13:29:28 +0800 | [diff] [blame] | 533 | |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 534 | if (operation == AMDGPU_VA_OP_MAP || |
Gustavo A. R. Silva | 93bab70 | 2018-02-14 23:20:00 -0600 | [diff] [blame] | 535 | operation == AMDGPU_VA_OP_REPLACE) { |
Flora Cui | 05dcb5c | 2016-09-22 11:34:47 +0800 | [diff] [blame] | 536 | r = amdgpu_vm_bo_update(adev, bo_va, false); |
Gustavo A. R. Silva | 93bab70 | 2018-02-14 23:20:00 -0600 | [diff] [blame] | 537 | if (r) |
| 538 | goto error; |
| 539 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 540 | |
Christian König | 0abc687 | 2017-09-01 20:37:57 +0200 | [diff] [blame] | 541 | r = amdgpu_vm_update_directories(adev, vm); |
Christian König | 0abc687 | 2017-09-01 20:37:57 +0200 | [diff] [blame] | 542 | |
Christian König | 2ffdaaf | 2017-01-27 15:58:43 +0100 | [diff] [blame] | 543 | error: |
Christian König | 68fdd3d | 2015-06-16 14:50:02 +0200 | [diff] [blame] | 544 | if (r && r != -ERESTARTSYS) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 545 | DRM_ERROR("Couldn't update BO_VA (%d)\n", r); |
| 546 | } |
| 547 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 548 | int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, |
| 549 | struct drm_file *filp) |
| 550 | { |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 551 | const uint32_t valid_flags = AMDGPU_VM_DELAY_UPDATE | |
| 552 | AMDGPU_VM_PAGE_READABLE | AMDGPU_VM_PAGE_WRITEABLE | |
Alex Xie | 66e02bc | 2017-02-14 12:04:52 -0500 | [diff] [blame] | 553 | AMDGPU_VM_PAGE_EXECUTABLE | AMDGPU_VM_MTYPE_MASK; |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 554 | const uint32_t prt_flags = AMDGPU_VM_DELAY_UPDATE | |
| 555 | AMDGPU_VM_PAGE_PRT; |
| 556 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 557 | struct drm_amdgpu_gem_va *args = data; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 558 | struct drm_gem_object *gobj; |
| 559 | struct amdgpu_device *adev = dev->dev_private; |
| 560 | struct amdgpu_fpriv *fpriv = filp->driver_priv; |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 561 | struct amdgpu_bo *abo; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 562 | struct amdgpu_bo_va *bo_va; |
Christian König | b88c879 | 2016-09-28 16:33:01 +0200 | [diff] [blame] | 563 | struct amdgpu_bo_list_entry vm_pd; |
| 564 | struct ttm_validate_buffer tv; |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 565 | struct ww_acquire_ctx ticket; |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 566 | struct list_head list, duplicates; |
Alex Xie | 5463545 | 2017-02-14 12:22:57 -0500 | [diff] [blame] | 567 | uint64_t va_flags; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 568 | int r = 0; |
| 569 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 570 | if (args->va_address < AMDGPU_VA_RESERVED_SIZE) { |
Christian König | 4b7f084 | 2017-11-13 13:58:17 +0100 | [diff] [blame] | 571 | dev_dbg(&dev->pdev->dev, |
Christian König | ff4cd38 | 2017-11-06 15:25:37 +0100 | [diff] [blame] | 572 | "va_address 0x%LX is in reserved area 0x%LX\n", |
| 573 | args->va_address, AMDGPU_VA_RESERVED_SIZE); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 574 | return -EINVAL; |
| 575 | } |
| 576 | |
Christian König | bb7939b | 2017-11-06 15:37:01 +0100 | [diff] [blame] | 577 | if (args->va_address >= AMDGPU_VA_HOLE_START && |
| 578 | args->va_address < AMDGPU_VA_HOLE_END) { |
| 579 | dev_dbg(&dev->pdev->dev, |
| 580 | "va_address 0x%LX is in VA hole 0x%LX-0x%LX\n", |
| 581 | args->va_address, AMDGPU_VA_HOLE_START, |
| 582 | AMDGPU_VA_HOLE_END); |
| 583 | return -EINVAL; |
| 584 | } |
| 585 | |
| 586 | args->va_address &= AMDGPU_VA_HOLE_MASK; |
| 587 | |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 588 | if ((args->flags & ~valid_flags) && (args->flags & ~prt_flags)) { |
Christian König | 4b7f084 | 2017-11-13 13:58:17 +0100 | [diff] [blame] | 589 | dev_dbg(&dev->pdev->dev, "invalid flags combination 0x%08X\n", |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 590 | args->flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 591 | return -EINVAL; |
| 592 | } |
| 593 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 594 | switch (args->operation) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 595 | case AMDGPU_VA_OP_MAP: |
| 596 | case AMDGPU_VA_OP_UNMAP: |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 597 | case AMDGPU_VA_OP_CLEAR: |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 598 | case AMDGPU_VA_OP_REPLACE: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 599 | break; |
| 600 | default: |
Christian König | 4b7f084 | 2017-11-13 13:58:17 +0100 | [diff] [blame] | 601 | dev_dbg(&dev->pdev->dev, "unsupported operation %d\n", |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 602 | args->operation); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 603 | return -EINVAL; |
| 604 | } |
| 605 | |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 606 | INIT_LIST_HEAD(&list); |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 607 | INIT_LIST_HEAD(&duplicates); |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 608 | if ((args->operation != AMDGPU_VA_OP_CLEAR) && |
| 609 | !(args->flags & AMDGPU_VM_PAGE_PRT)) { |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 610 | gobj = drm_gem_object_lookup(filp, args->handle); |
| 611 | if (gobj == NULL) |
| 612 | return -ENOENT; |
| 613 | abo = gem_to_amdgpu_bo(gobj); |
| 614 | tv.bo = &abo->tbo; |
| 615 | tv.shared = false; |
| 616 | list_add(&tv.head, &list); |
| 617 | } else { |
| 618 | gobj = NULL; |
| 619 | abo = NULL; |
| 620 | } |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 621 | |
Christian König | b88c879 | 2016-09-28 16:33:01 +0200 | [diff] [blame] | 622 | amdgpu_vm_get_pd_bo(&fpriv->vm, &list, &vm_pd); |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 623 | |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 624 | r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates); |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 625 | if (r) |
| 626 | goto error_unref; |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 627 | |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 628 | if (abo) { |
| 629 | bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo); |
| 630 | if (!bo_va) { |
| 631 | r = -ENOENT; |
| 632 | goto error_backoff; |
| 633 | } |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 634 | } else if (args->operation != AMDGPU_VA_OP_CLEAR) { |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 635 | bo_va = fpriv->prt_va; |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 636 | } else { |
| 637 | bo_va = NULL; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 638 | } |
| 639 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 640 | switch (args->operation) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 641 | case AMDGPU_VA_OP_MAP: |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 642 | r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address, |
Christian König | 663e457 | 2017-03-13 10:13:37 +0100 | [diff] [blame] | 643 | args->map_size); |
| 644 | if (r) |
| 645 | goto error_backoff; |
Alex Xie | 5463545 | 2017-02-14 12:22:57 -0500 | [diff] [blame] | 646 | |
Christian König | 132f34e | 2018-01-12 15:26:08 +0100 | [diff] [blame] | 647 | va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags); |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 648 | r = amdgpu_vm_bo_map(adev, bo_va, args->va_address, |
| 649 | args->offset_in_bo, args->map_size, |
Christian König | 9f7eb53 | 2015-05-18 16:05:57 +0200 | [diff] [blame] | 650 | va_flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 651 | break; |
| 652 | case AMDGPU_VA_OP_UNMAP: |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 653 | r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 654 | break; |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 655 | |
| 656 | case AMDGPU_VA_OP_CLEAR: |
| 657 | r = amdgpu_vm_bo_clear_mappings(adev, &fpriv->vm, |
| 658 | args->va_address, |
| 659 | args->map_size); |
| 660 | break; |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 661 | case AMDGPU_VA_OP_REPLACE: |
Christian König | ec68154 | 2017-08-01 10:51:43 +0200 | [diff] [blame] | 662 | r = amdgpu_vm_alloc_pts(adev, bo_va->base.vm, args->va_address, |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 663 | args->map_size); |
| 664 | if (r) |
| 665 | goto error_backoff; |
| 666 | |
Christian König | 132f34e | 2018-01-12 15:26:08 +0100 | [diff] [blame] | 667 | va_flags = amdgpu_gmc_get_pte_flags(adev, args->flags); |
Christian König | 80f95c5 | 2017-03-13 10:13:39 +0100 | [diff] [blame] | 668 | r = amdgpu_vm_bo_replace_map(adev, bo_va, args->va_address, |
| 669 | args->offset_in_bo, args->map_size, |
| 670 | va_flags); |
| 671 | break; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 672 | default: |
| 673 | break; |
| 674 | } |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 675 | if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && !amdgpu_vm_debug) |
Christian König | dc54d3d | 2017-03-13 10:13:38 +0100 | [diff] [blame] | 676 | amdgpu_gem_va_update_vm(adev, &fpriv->vm, bo_va, &list, |
| 677 | args->operation); |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 678 | |
| 679 | error_backoff: |
Christian König | 2ffdaaf | 2017-01-27 15:58:43 +0100 | [diff] [blame] | 680 | ttm_eu_backoff_reservation(&ticket, &list); |
Chunming Zhou | e98c1b0 | 2015-11-13 15:22:04 +0800 | [diff] [blame] | 681 | |
Junwei Zhang | b85891b | 2017-01-16 13:59:01 +0800 | [diff] [blame] | 682 | error_unref: |
Cihangir Akturk | f62facc | 2017-08-03 14:58:16 +0300 | [diff] [blame] | 683 | drm_gem_object_put_unlocked(gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 684 | return r; |
| 685 | } |
| 686 | |
| 687 | int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data, |
| 688 | struct drm_file *filp) |
| 689 | { |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 690 | struct amdgpu_device *adev = dev->dev_private; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 691 | struct drm_amdgpu_gem_op *args = data; |
| 692 | struct drm_gem_object *gobj; |
| 693 | struct amdgpu_bo *robj; |
| 694 | int r; |
| 695 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 696 | gobj = drm_gem_object_lookup(filp, args->handle); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 697 | if (gobj == NULL) { |
| 698 | return -ENOENT; |
| 699 | } |
| 700 | robj = gem_to_amdgpu_bo(gobj); |
| 701 | |
| 702 | r = amdgpu_bo_reserve(robj, false); |
| 703 | if (unlikely(r)) |
| 704 | goto out; |
| 705 | |
| 706 | switch (args->op) { |
| 707 | case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: { |
| 708 | struct drm_amdgpu_gem_create_in info; |
Christian König | 7ecc245 | 2017-07-26 17:02:52 +0200 | [diff] [blame] | 709 | void __user *out = u64_to_user_ptr(args->value); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 710 | |
| 711 | info.bo_size = robj->gem_base.size; |
| 712 | info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT; |
Kent Russell | 6d7d9c5 | 2017-08-08 07:58:01 -0400 | [diff] [blame] | 713 | info.domains = robj->preferred_domains; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 714 | info.domain_flags = robj->flags; |
Christian König | 4c28fb0 | 2015-08-28 17:27:54 +0200 | [diff] [blame] | 715 | amdgpu_bo_unreserve(robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 716 | if (copy_to_user(out, &info, sizeof(info))) |
| 717 | r = -EFAULT; |
| 718 | break; |
| 719 | } |
Marek Olšák | d8f65a2 | 2015-05-27 14:30:38 +0200 | [diff] [blame] | 720 | case AMDGPU_GEM_OP_SET_PLACEMENT: |
Christopher James Halse Rogers | 803d89a | 2017-04-03 13:31:22 +1000 | [diff] [blame] | 721 | if (robj->prime_shared_count && (args->value & AMDGPU_GEM_DOMAIN_VRAM)) { |
| 722 | r = -EINVAL; |
| 723 | amdgpu_bo_unreserve(robj); |
| 724 | break; |
| 725 | } |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 726 | if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 727 | r = -EPERM; |
Christian König | 4c28fb0 | 2015-08-28 17:27:54 +0200 | [diff] [blame] | 728 | amdgpu_bo_unreserve(robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 729 | break; |
| 730 | } |
Kent Russell | 6d7d9c5 | 2017-08-08 07:58:01 -0400 | [diff] [blame] | 731 | robj->preferred_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM | |
Christian König | 1ea863f | 2015-12-18 22:13:12 +0100 | [diff] [blame] | 732 | AMDGPU_GEM_DOMAIN_GTT | |
| 733 | AMDGPU_GEM_DOMAIN_CPU); |
Kent Russell | 6d7d9c5 | 2017-08-08 07:58:01 -0400 | [diff] [blame] | 734 | robj->allowed_domains = robj->preferred_domains; |
Christian König | 1ea863f | 2015-12-18 22:13:12 +0100 | [diff] [blame] | 735 | if (robj->allowed_domains == AMDGPU_GEM_DOMAIN_VRAM) |
| 736 | robj->allowed_domains |= AMDGPU_GEM_DOMAIN_GTT; |
| 737 | |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 738 | if (robj->flags & AMDGPU_GEM_CREATE_VM_ALWAYS_VALID) |
| 739 | amdgpu_vm_bo_invalidate(adev, robj, true); |
| 740 | |
Christian König | 4c28fb0 | 2015-08-28 17:27:54 +0200 | [diff] [blame] | 741 | amdgpu_bo_unreserve(robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 742 | break; |
| 743 | default: |
Christian König | 4c28fb0 | 2015-08-28 17:27:54 +0200 | [diff] [blame] | 744 | amdgpu_bo_unreserve(robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 745 | r = -EINVAL; |
| 746 | } |
| 747 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 748 | out: |
Cihangir Akturk | f62facc | 2017-08-03 14:58:16 +0300 | [diff] [blame] | 749 | drm_gem_object_put_unlocked(gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 750 | return r; |
| 751 | } |
| 752 | |
| 753 | int amdgpu_mode_dumb_create(struct drm_file *file_priv, |
| 754 | struct drm_device *dev, |
| 755 | struct drm_mode_create_dumb *args) |
| 756 | { |
| 757 | struct amdgpu_device *adev = dev->dev_private; |
| 758 | struct drm_gem_object *gobj; |
| 759 | uint32_t handle; |
Deepak Sharma | 84b7460 | 2018-05-25 17:12:29 -0700 | [diff] [blame] | 760 | u32 domain; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 761 | int r; |
| 762 | |
Laurent Pinchart | 8e911ab | 2016-10-18 01:41:17 +0300 | [diff] [blame] | 763 | args->pitch = amdgpu_align_pitch(adev, args->width, |
| 764 | DIV_ROUND_UP(args->bpp, 8), 0); |
Dan Carpenter | 54ef0b5 | 2015-09-23 14:00:59 +0300 | [diff] [blame] | 765 | args->size = (u64)args->pitch * args->height; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 766 | args->size = ALIGN(args->size, PAGE_SIZE); |
Deepak Sharma | 84b7460 | 2018-05-25 17:12:29 -0700 | [diff] [blame] | 767 | domain = amdgpu_bo_get_preferred_pin_domain(adev, |
| 768 | amdgpu_display_supported_domains(adev)); |
Deepak Sharma | bda31a2 | 2018-05-22 15:31:23 -0700 | [diff] [blame] | 769 | r = amdgpu_gem_object_create(adev, args->size, 0, domain, |
Alex Deucher | 857d913 | 2015-08-27 00:14:16 -0400 | [diff] [blame] | 770 | AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED, |
Christian König | e1eb899b4 | 2017-08-25 09:14:43 +0200 | [diff] [blame] | 771 | false, NULL, &gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 772 | if (r) |
| 773 | return -ENOMEM; |
| 774 | |
| 775 | r = drm_gem_handle_create(file_priv, gobj, &handle); |
| 776 | /* drop reference from allocate - handle holds it now */ |
Cihangir Akturk | f62facc | 2017-08-03 14:58:16 +0300 | [diff] [blame] | 777 | drm_gem_object_put_unlocked(gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 778 | if (r) { |
| 779 | return r; |
| 780 | } |
| 781 | args->handle = handle; |
| 782 | return 0; |
| 783 | } |
| 784 | |
| 785 | #if defined(CONFIG_DEBUG_FS) |
Christian König | 6b155d6 | 2018-05-11 23:14:29 +0800 | [diff] [blame] | 786 | |
| 787 | #define amdgpu_debugfs_gem_bo_print_flag(m, bo, flag) \ |
| 788 | if (bo->flags & (AMDGPU_GEM_CREATE_ ## flag)) { \ |
| 789 | seq_printf((m), " " #flag); \ |
| 790 | } |
| 791 | |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 792 | static int amdgpu_debugfs_gem_bo_info(int id, void *ptr, void *data) |
| 793 | { |
| 794 | struct drm_gem_object *gobj = ptr; |
| 795 | struct amdgpu_bo *bo = gem_to_amdgpu_bo(gobj); |
| 796 | struct seq_file *m = data; |
| 797 | |
Christian König | b1f223c | 2018-03-25 10:10:25 +0200 | [diff] [blame] | 798 | struct dma_buf_attachment *attachment; |
| 799 | struct dma_buf *dma_buf; |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 800 | unsigned domain; |
| 801 | const char *placement; |
| 802 | unsigned pin_count; |
| 803 | |
| 804 | domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type); |
| 805 | switch (domain) { |
| 806 | case AMDGPU_GEM_DOMAIN_VRAM: |
| 807 | placement = "VRAM"; |
| 808 | break; |
| 809 | case AMDGPU_GEM_DOMAIN_GTT: |
| 810 | placement = " GTT"; |
| 811 | break; |
| 812 | case AMDGPU_GEM_DOMAIN_CPU: |
| 813 | default: |
| 814 | placement = " CPU"; |
| 815 | break; |
| 816 | } |
Christian König | b8e0e6e | 2017-06-26 15:19:30 +0200 | [diff] [blame] | 817 | seq_printf(m, "\t0x%08x: %12ld byte %s", |
| 818 | id, amdgpu_bo_size(bo), placement); |
| 819 | |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 820 | pin_count = READ_ONCE(bo->pin_count); |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 821 | if (pin_count) |
| 822 | seq_printf(m, " pin count %d", pin_count); |
Christian König | b1f223c | 2018-03-25 10:10:25 +0200 | [diff] [blame] | 823 | |
| 824 | dma_buf = READ_ONCE(bo->gem_base.dma_buf); |
| 825 | attachment = READ_ONCE(bo->gem_base.import_attach); |
| 826 | |
| 827 | if (attachment) |
| 828 | seq_printf(m, " imported from %p", dma_buf); |
| 829 | else if (dma_buf) |
| 830 | seq_printf(m, " exported as %p", dma_buf); |
| 831 | |
Christian König | 6b155d6 | 2018-05-11 23:14:29 +0800 | [diff] [blame] | 832 | amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_ACCESS_REQUIRED); |
| 833 | amdgpu_debugfs_gem_bo_print_flag(m, bo, NO_CPU_ACCESS); |
| 834 | amdgpu_debugfs_gem_bo_print_flag(m, bo, CPU_GTT_USWC); |
| 835 | amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CLEARED); |
| 836 | amdgpu_debugfs_gem_bo_print_flag(m, bo, SHADOW); |
| 837 | amdgpu_debugfs_gem_bo_print_flag(m, bo, VRAM_CONTIGUOUS); |
| 838 | amdgpu_debugfs_gem_bo_print_flag(m, bo, VM_ALWAYS_VALID); |
| 839 | amdgpu_debugfs_gem_bo_print_flag(m, bo, EXPLICIT_SYNC); |
| 840 | |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 841 | seq_printf(m, "\n"); |
| 842 | |
| 843 | return 0; |
| 844 | } |
| 845 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 846 | static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data) |
| 847 | { |
| 848 | struct drm_info_node *node = (struct drm_info_node *)m->private; |
| 849 | struct drm_device *dev = node->minor->dev; |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 850 | struct drm_file *file; |
| 851 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 852 | |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 853 | r = mutex_lock_interruptible(&dev->filelist_mutex); |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 854 | if (r) |
| 855 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 856 | |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 857 | list_for_each_entry(file, &dev->filelist, lhead) { |
| 858 | struct task_struct *task; |
Christian König | b22e3ce | 2016-02-15 12:41:37 +0100 | [diff] [blame] | 859 | |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 860 | /* |
| 861 | * Although we have a valid reference on file->pid, that does |
| 862 | * not guarantee that the task_struct who called get_pid() is |
| 863 | * still alive (e.g. get_pid(current) => fork() => exit()). |
| 864 | * Therefore, we need to protect this ->comm access using RCU. |
| 865 | */ |
| 866 | rcu_read_lock(); |
| 867 | task = pid_task(file->pid, PIDTYPE_PID); |
| 868 | seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid), |
| 869 | task ? task->comm : "<unknown>"); |
| 870 | rcu_read_unlock(); |
| 871 | |
| 872 | spin_lock(&file->table_lock); |
| 873 | idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m); |
| 874 | spin_unlock(&file->table_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 875 | } |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 876 | |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 877 | mutex_unlock(&dev->filelist_mutex); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 878 | return 0; |
| 879 | } |
| 880 | |
Nils Wallménius | 06ab683 | 2016-05-02 12:46:15 -0400 | [diff] [blame] | 881 | static const struct drm_info_list amdgpu_debugfs_gem_list[] = { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 882 | {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL}, |
| 883 | }; |
| 884 | #endif |
| 885 | |
Alex Deucher | 7575825 | 2017-12-14 15:23:14 -0500 | [diff] [blame] | 886 | int amdgpu_debugfs_gem_init(struct amdgpu_device *adev) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 887 | { |
| 888 | #if defined(CONFIG_DEBUG_FS) |
| 889 | return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1); |
| 890 | #endif |
| 891 | return 0; |
| 892 | } |