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