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