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