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; |
| 52 | unsigned long max_size; |
| 53 | int r; |
| 54 | |
| 55 | *obj = NULL; |
| 56 | /* At least align on page size */ |
| 57 | if (alignment < PAGE_SIZE) { |
| 58 | alignment = PAGE_SIZE; |
| 59 | } |
| 60 | |
| 61 | if (!(initial_domain & (AMDGPU_GEM_DOMAIN_GDS | AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA))) { |
| 62 | /* Maximum bo size is the unpinned gtt size since we use the gtt to |
| 63 | * handle vram to system pool migrations. |
| 64 | */ |
| 65 | max_size = adev->mc.gtt_size - adev->gart_pin_size; |
| 66 | if (size > max_size) { |
| 67 | DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n", |
| 68 | size >> 20, max_size >> 20); |
| 69 | return -ENOMEM; |
| 70 | } |
| 71 | } |
| 72 | retry: |
Christian König | 72d7668 | 2015-09-03 17:34:59 +0200 | [diff] [blame] | 73 | r = amdgpu_bo_create(adev, size, alignment, kernel, initial_domain, |
| 74 | flags, NULL, NULL, &robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 75 | if (r) { |
| 76 | if (r != -ERESTARTSYS) { |
| 77 | if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) { |
| 78 | initial_domain |= AMDGPU_GEM_DOMAIN_GTT; |
| 79 | goto retry; |
| 80 | } |
| 81 | DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n", |
| 82 | size, initial_domain, alignment, r); |
| 83 | } |
| 84 | return r; |
| 85 | } |
| 86 | *obj = &robj->gem_base; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 87 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Christian König | 418aa0c | 2016-02-15 16:59:57 +0100 | [diff] [blame] | 91 | void amdgpu_gem_force_release(struct amdgpu_device *adev) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 92 | { |
Christian König | 418aa0c | 2016-02-15 16:59:57 +0100 | [diff] [blame] | 93 | struct drm_device *ddev = adev->ddev; |
| 94 | struct drm_file *file; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 95 | |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 96 | mutex_lock(&ddev->filelist_mutex); |
Christian König | 418aa0c | 2016-02-15 16:59:57 +0100 | [diff] [blame] | 97 | |
| 98 | list_for_each_entry(file, &ddev->filelist, lhead) { |
| 99 | struct drm_gem_object *gobj; |
| 100 | int handle; |
| 101 | |
| 102 | WARN_ONCE(1, "Still active user space clients!\n"); |
| 103 | spin_lock(&file->table_lock); |
| 104 | idr_for_each_entry(&file->object_idr, gobj, handle) { |
| 105 | WARN_ONCE(1, "And also active allocations!\n"); |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 106 | drm_gem_object_unreference_unlocked(gobj); |
Christian König | 418aa0c | 2016-02-15 16:59:57 +0100 | [diff] [blame] | 107 | } |
| 108 | idr_destroy(&file->object_idr); |
| 109 | spin_unlock(&file->table_lock); |
| 110 | } |
| 111 | |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 112 | mutex_unlock(&ddev->filelist_mutex); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Call from drm_gem_handle_create which appear in both new and open ioctl |
| 117 | * case. |
| 118 | */ |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 119 | int amdgpu_gem_object_open(struct drm_gem_object *obj, |
| 120 | struct drm_file *file_priv) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 121 | { |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 122 | struct amdgpu_bo *abo = gem_to_amdgpu_bo(obj); |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 123 | struct amdgpu_device *adev = amdgpu_ttm_adev(abo->tbo.bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 124 | struct amdgpu_fpriv *fpriv = file_priv->driver_priv; |
| 125 | struct amdgpu_vm *vm = &fpriv->vm; |
| 126 | struct amdgpu_bo_va *bo_va; |
| 127 | int r; |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 128 | r = amdgpu_bo_reserve(abo, false); |
Chunming Zhou | e98c1b0 | 2015-11-13 15:22:04 +0800 | [diff] [blame] | 129 | if (r) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 130 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 131 | |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 132 | bo_va = amdgpu_vm_bo_find(vm, abo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 133 | if (!bo_va) { |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 134 | bo_va = amdgpu_vm_bo_add(adev, vm, abo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 135 | } else { |
| 136 | ++bo_va->ref_count; |
| 137 | } |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 138 | amdgpu_bo_unreserve(abo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | void amdgpu_gem_object_close(struct drm_gem_object *obj, |
| 143 | struct drm_file *file_priv) |
| 144 | { |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 145 | struct amdgpu_bo *bo = gem_to_amdgpu_bo(obj); |
Christian König | a7d64de | 2016-09-15 14:58:48 +0200 | [diff] [blame] | 146 | struct amdgpu_device *adev = amdgpu_ttm_adev(bo->tbo.bdev); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 147 | struct amdgpu_fpriv *fpriv = file_priv->driver_priv; |
| 148 | struct amdgpu_vm *vm = &fpriv->vm; |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 149 | |
| 150 | struct amdgpu_bo_list_entry vm_pd; |
| 151 | struct list_head list, duplicates; |
| 152 | struct ttm_validate_buffer tv; |
| 153 | struct ww_acquire_ctx ticket; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 154 | struct amdgpu_bo_va *bo_va; |
| 155 | int r; |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 156 | |
| 157 | INIT_LIST_HEAD(&list); |
| 158 | INIT_LIST_HEAD(&duplicates); |
| 159 | |
| 160 | tv.bo = &bo->tbo; |
| 161 | tv.shared = true; |
| 162 | list_add(&tv.head, &list); |
| 163 | |
| 164 | amdgpu_vm_get_pd_bo(vm, &list, &vm_pd); |
| 165 | |
Christian König | 35264f6 | 2016-03-17 17:14:10 +0100 | [diff] [blame] | 166 | r = ttm_eu_reserve_buffers(&ticket, &list, false, &duplicates); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 167 | if (r) { |
| 168 | dev_err(adev->dev, "leaking bo va because " |
| 169 | "we fail to reserve bo (%d)\n", r); |
| 170 | return; |
| 171 | } |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 172 | bo_va = amdgpu_vm_bo_find(vm, bo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 173 | if (bo_va) { |
| 174 | if (--bo_va->ref_count == 0) { |
| 175 | amdgpu_vm_bo_rmv(adev, bo_va); |
| 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 | |
| 181 | static int amdgpu_gem_handle_lockup(struct amdgpu_device *adev, int r) |
| 182 | { |
| 183 | if (r == -EDEADLK) { |
| 184 | r = amdgpu_gpu_reset(adev); |
| 185 | if (!r) |
| 186 | r = -EAGAIN; |
| 187 | } |
| 188 | return r; |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * GEM ioctls. |
| 193 | */ |
| 194 | int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data, |
| 195 | struct drm_file *filp) |
| 196 | { |
| 197 | struct amdgpu_device *adev = dev->dev_private; |
| 198 | union drm_amdgpu_gem_create *args = data; |
| 199 | uint64_t size = args->in.bo_size; |
| 200 | struct drm_gem_object *gobj; |
| 201 | uint32_t handle; |
| 202 | bool kernel = false; |
| 203 | int r; |
| 204 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 205 | /* create a gem object to contain this object in */ |
| 206 | if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS | |
| 207 | AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) { |
| 208 | kernel = true; |
| 209 | if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS) |
| 210 | size = size << AMDGPU_GDS_SHIFT; |
| 211 | else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS) |
| 212 | size = size << AMDGPU_GWS_SHIFT; |
| 213 | else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA) |
| 214 | size = size << AMDGPU_OA_SHIFT; |
| 215 | else { |
| 216 | r = -EINVAL; |
| 217 | goto error_unlock; |
| 218 | } |
| 219 | } |
| 220 | size = roundup(size, PAGE_SIZE); |
| 221 | |
| 222 | r = amdgpu_gem_object_create(adev, size, args->in.alignment, |
| 223 | (u32)(0xffffffff & args->in.domains), |
| 224 | args->in.domain_flags, |
| 225 | kernel, &gobj); |
| 226 | if (r) |
| 227 | goto error_unlock; |
| 228 | |
| 229 | r = drm_gem_handle_create(filp, gobj, &handle); |
| 230 | /* drop reference from allocate - handle holds it now */ |
| 231 | drm_gem_object_unreference_unlocked(gobj); |
| 232 | if (r) |
| 233 | goto error_unlock; |
| 234 | |
| 235 | memset(args, 0, sizeof(*args)); |
| 236 | args->out.handle = handle; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 237 | return 0; |
| 238 | |
| 239 | error_unlock: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 240 | r = amdgpu_gem_handle_lockup(adev, r); |
| 241 | return r; |
| 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) |
| 275 | goto handle_lockup; |
| 276 | |
| 277 | bo = gem_to_amdgpu_bo(gobj); |
Christian König | 1ea863f | 2015-12-18 22:13:12 +0100 | [diff] [blame] | 278 | bo->prefered_domains = AMDGPU_GEM_DOMAIN_GTT; |
| 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 */ |
| 313 | drm_gem_object_unreference_unlocked(gobj); |
| 314 | if (r) |
| 315 | goto handle_lockup; |
| 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: |
| 327 | drm_gem_object_unreference_unlocked(gobj); |
| 328 | |
| 329 | handle_lockup: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 330 | r = amdgpu_gem_handle_lockup(adev, r); |
| 331 | |
| 332 | return r; |
| 333 | } |
| 334 | |
| 335 | int amdgpu_mode_dumb_mmap(struct drm_file *filp, |
| 336 | struct drm_device *dev, |
| 337 | uint32_t handle, uint64_t *offset_p) |
| 338 | { |
| 339 | struct drm_gem_object *gobj; |
| 340 | struct amdgpu_bo *robj; |
| 341 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 342 | gobj = drm_gem_object_lookup(filp, handle); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 343 | if (gobj == NULL) { |
| 344 | return -ENOENT; |
| 345 | } |
| 346 | robj = gem_to_amdgpu_bo(gobj); |
Christian König | cc325d1 | 2016-02-08 11:08:35 +0100 | [diff] [blame] | 347 | if (amdgpu_ttm_tt_get_usermm(robj->tbo.ttm) || |
Christian König | 271c812 | 2015-05-13 14:30:53 +0200 | [diff] [blame] | 348 | (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 349 | drm_gem_object_unreference_unlocked(gobj); |
| 350 | return -EPERM; |
| 351 | } |
| 352 | *offset_p = amdgpu_bo_mmap_offset(robj); |
| 353 | drm_gem_object_unreference_unlocked(gobj); |
| 354 | return 0; |
| 355 | } |
| 356 | |
| 357 | int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data, |
| 358 | struct drm_file *filp) |
| 359 | { |
| 360 | union drm_amdgpu_gem_mmap *args = data; |
| 361 | uint32_t handle = args->in.handle; |
| 362 | memset(args, 0, sizeof(*args)); |
| 363 | return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * amdgpu_gem_timeout - calculate jiffies timeout from absolute value |
| 368 | * |
| 369 | * @timeout_ns: timeout in ns |
| 370 | * |
| 371 | * Calculate the timeout in jiffies from an absolute timeout in ns. |
| 372 | */ |
| 373 | unsigned long amdgpu_gem_timeout(uint64_t timeout_ns) |
| 374 | { |
| 375 | unsigned long timeout_jiffies; |
| 376 | ktime_t timeout; |
| 377 | |
| 378 | /* clamp timeout if it's to large */ |
| 379 | if (((int64_t)timeout_ns) < 0) |
| 380 | return MAX_SCHEDULE_TIMEOUT; |
| 381 | |
Christian König | 0f11770 | 2015-07-08 16:58:48 +0200 | [diff] [blame] | 382 | timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get()); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 383 | if (ktime_to_ns(timeout) < 0) |
| 384 | return 0; |
| 385 | |
| 386 | timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout)); |
| 387 | /* clamp timeout to avoid unsigned-> signed overflow */ |
| 388 | if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT ) |
| 389 | return MAX_SCHEDULE_TIMEOUT - 1; |
| 390 | |
| 391 | return timeout_jiffies; |
| 392 | } |
| 393 | |
| 394 | int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data, |
| 395 | struct drm_file *filp) |
| 396 | { |
| 397 | struct amdgpu_device *adev = dev->dev_private; |
| 398 | union drm_amdgpu_gem_wait_idle *args = data; |
| 399 | struct drm_gem_object *gobj; |
| 400 | struct amdgpu_bo *robj; |
| 401 | uint32_t handle = args->in.handle; |
| 402 | unsigned long timeout = amdgpu_gem_timeout(args->in.timeout); |
| 403 | int r = 0; |
| 404 | long ret; |
| 405 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 406 | gobj = drm_gem_object_lookup(filp, handle); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 407 | if (gobj == NULL) { |
| 408 | return -ENOENT; |
| 409 | } |
| 410 | robj = gem_to_amdgpu_bo(gobj); |
Chris Wilson | 0fea2ed | 2016-08-29 08:08:24 +0100 | [diff] [blame] | 411 | ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, |
| 412 | timeout); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 413 | |
| 414 | /* ret == 0 means not signaled, |
| 415 | * ret > 0 means signaled |
| 416 | * ret < 0 means interrupted before timeout |
| 417 | */ |
| 418 | if (ret >= 0) { |
| 419 | memset(args, 0, sizeof(*args)); |
| 420 | args->out.status = (ret == 0); |
| 421 | } else |
| 422 | r = ret; |
| 423 | |
| 424 | drm_gem_object_unreference_unlocked(gobj); |
| 425 | r = amdgpu_gem_handle_lockup(adev, r); |
| 426 | return r; |
| 427 | } |
| 428 | |
| 429 | int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data, |
| 430 | struct drm_file *filp) |
| 431 | { |
| 432 | struct drm_amdgpu_gem_metadata *args = data; |
| 433 | struct drm_gem_object *gobj; |
| 434 | struct amdgpu_bo *robj; |
| 435 | int r = -1; |
| 436 | |
| 437 | DRM_DEBUG("%d \n", args->handle); |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 438 | gobj = drm_gem_object_lookup(filp, args->handle); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 439 | if (gobj == NULL) |
| 440 | return -ENOENT; |
| 441 | robj = gem_to_amdgpu_bo(gobj); |
| 442 | |
| 443 | r = amdgpu_bo_reserve(robj, false); |
| 444 | if (unlikely(r != 0)) |
| 445 | goto out; |
| 446 | |
| 447 | if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) { |
| 448 | amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info); |
| 449 | r = amdgpu_bo_get_metadata(robj, args->data.data, |
| 450 | sizeof(args->data.data), |
| 451 | &args->data.data_size_bytes, |
| 452 | &args->data.flags); |
| 453 | } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) { |
Dan Carpenter | 0913eab | 2015-09-23 14:00:35 +0300 | [diff] [blame] | 454 | if (args->data.data_size_bytes > sizeof(args->data.data)) { |
| 455 | r = -EINVAL; |
| 456 | goto unreserve; |
| 457 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 458 | r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info); |
| 459 | if (!r) |
| 460 | r = amdgpu_bo_set_metadata(robj, args->data.data, |
| 461 | args->data.data_size_bytes, |
| 462 | args->data.flags); |
| 463 | } |
| 464 | |
Dan Carpenter | 0913eab | 2015-09-23 14:00:35 +0300 | [diff] [blame] | 465 | unreserve: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 466 | amdgpu_bo_unreserve(robj); |
| 467 | out: |
| 468 | drm_gem_object_unreference_unlocked(gobj); |
| 469 | return r; |
| 470 | } |
| 471 | |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame^] | 472 | static int amdgpu_gem_va_check(void *param, struct amdgpu_bo *bo) |
| 473 | { |
| 474 | unsigned domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type); |
| 475 | |
| 476 | /* if anything is swapped out don't swap it in here, |
| 477 | just abort and wait for the next CS */ |
| 478 | |
| 479 | return domain == AMDGPU_GEM_DOMAIN_CPU ? -ERESTARTSYS : 0; |
| 480 | } |
| 481 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 482 | /** |
| 483 | * amdgpu_gem_va_update_vm -update the bo_va in its VM |
| 484 | * |
| 485 | * @adev: amdgpu_device pointer |
| 486 | * @bo_va: bo_va to update |
| 487 | * |
| 488 | * Update the bo_va directly after setting it's address. Errors are not |
| 489 | * vital here, so they are not reported back to userspace. |
| 490 | */ |
| 491 | static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev, |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame^] | 492 | struct amdgpu_bo_va *bo_va, |
| 493 | uint32_t operation) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 494 | { |
| 495 | struct ttm_validate_buffer tv, *entry; |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 496 | struct amdgpu_bo_list_entry vm_pd; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 497 | struct ww_acquire_ctx ticket; |
Christian König | bf60efd | 2015-09-04 10:47:56 +0200 | [diff] [blame] | 498 | struct list_head list, duplicates; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 499 | unsigned domain; |
| 500 | int r; |
| 501 | |
| 502 | INIT_LIST_HEAD(&list); |
Christian König | bf60efd | 2015-09-04 10:47:56 +0200 | [diff] [blame] | 503 | INIT_LIST_HEAD(&duplicates); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 504 | |
| 505 | tv.bo = &bo_va->bo->tbo; |
| 506 | tv.shared = true; |
| 507 | list_add(&tv.head, &list); |
| 508 | |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 509 | amdgpu_vm_get_pd_bo(bo_va->vm, &list, &vm_pd); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 510 | |
Christian König | bf60efd | 2015-09-04 10:47:56 +0200 | [diff] [blame] | 511 | /* Provide duplicates to avoid -EALREADY */ |
| 512 | r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 513 | if (r) |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 514 | goto error_print; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 515 | |
| 516 | list_for_each_entry(entry, &list, head) { |
| 517 | domain = amdgpu_mem_type_to_domain(entry->bo->mem.mem_type); |
| 518 | /* if anything is swapped out don't swap it in here, |
| 519 | just abort and wait for the next CS */ |
| 520 | if (domain == AMDGPU_GEM_DOMAIN_CPU) |
| 521 | goto error_unreserve; |
| 522 | } |
Christian König | f7da30d | 2016-09-28 12:03:04 +0200 | [diff] [blame^] | 523 | r = amdgpu_vm_validate_pt_bos(adev, bo_va->vm, amdgpu_gem_va_check, |
| 524 | NULL); |
| 525 | if (r) |
| 526 | goto error_unreserve; |
Chunming Zhou | e410b5c | 2015-12-07 15:02:52 +0800 | [diff] [blame] | 527 | |
Chunming Zhou | 43c27fb | 2015-11-12 15:33:09 +0800 | [diff] [blame] | 528 | r = amdgpu_vm_update_page_directory(adev, bo_va->vm); |
| 529 | if (r) |
| 530 | goto error_unreserve; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 531 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 532 | r = amdgpu_vm_clear_freed(adev, bo_va->vm); |
| 533 | if (r) |
Chunming Zhou | f48b265 | 2015-10-16 14:06:19 +0800 | [diff] [blame] | 534 | goto error_unreserve; |
monk.liu | 194a336 | 2015-07-22 13:29:28 +0800 | [diff] [blame] | 535 | |
| 536 | if (operation == AMDGPU_VA_OP_MAP) |
Flora Cui | 05dcb5c | 2016-09-22 11:34:47 +0800 | [diff] [blame] | 537 | r = amdgpu_vm_bo_update(adev, bo_va, false); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 538 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 539 | error_unreserve: |
| 540 | ttm_eu_backoff_reservation(&ticket, &list); |
| 541 | |
Christian König | 56467eb | 2015-12-11 15:16:32 +0100 | [diff] [blame] | 542 | error_print: |
Christian König | 68fdd3d | 2015-06-16 14:50:02 +0200 | [diff] [blame] | 543 | if (r && r != -ERESTARTSYS) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 544 | DRM_ERROR("Couldn't update BO_VA (%d)\n", r); |
| 545 | } |
| 546 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 547 | int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, |
| 548 | struct drm_file *filp) |
| 549 | { |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 550 | struct drm_amdgpu_gem_va *args = data; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 551 | struct drm_gem_object *gobj; |
| 552 | struct amdgpu_device *adev = dev->dev_private; |
| 553 | struct amdgpu_fpriv *fpriv = filp->driver_priv; |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 554 | struct amdgpu_bo *abo; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 555 | struct amdgpu_bo_va *bo_va; |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 556 | struct ttm_validate_buffer tv, tv_pd; |
| 557 | struct ww_acquire_ctx ticket; |
| 558 | struct list_head list, duplicates; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 559 | uint32_t invalid_flags, va_flags = 0; |
| 560 | int r = 0; |
| 561 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 562 | if (!adev->vm_manager.enabled) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 563 | return -ENOTTY; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 564 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 565 | if (args->va_address < AMDGPU_VA_RESERVED_SIZE) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 566 | dev_err(&dev->pdev->dev, |
| 567 | "va_address 0x%lX is in reserved area 0x%X\n", |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 568 | (unsigned long)args->va_address, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 569 | AMDGPU_VA_RESERVED_SIZE); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 570 | return -EINVAL; |
| 571 | } |
| 572 | |
Christian König | fc220f6 | 2015-06-29 17:12:20 +0200 | [diff] [blame] | 573 | invalid_flags = ~(AMDGPU_VM_DELAY_UPDATE | AMDGPU_VM_PAGE_READABLE | |
| 574 | AMDGPU_VM_PAGE_WRITEABLE | AMDGPU_VM_PAGE_EXECUTABLE); |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 575 | if ((args->flags & invalid_flags)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 576 | dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n", |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 577 | args->flags, invalid_flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 578 | return -EINVAL; |
| 579 | } |
| 580 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 581 | switch (args->operation) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 582 | case AMDGPU_VA_OP_MAP: |
| 583 | case AMDGPU_VA_OP_UNMAP: |
| 584 | break; |
| 585 | default: |
| 586 | dev_err(&dev->pdev->dev, "unsupported operation %d\n", |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 587 | args->operation); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 588 | return -EINVAL; |
| 589 | } |
| 590 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 591 | gobj = drm_gem_object_lookup(filp, args->handle); |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 592 | if (gobj == NULL) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 593 | return -ENOENT; |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 594 | abo = gem_to_amdgpu_bo(gobj); |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 595 | INIT_LIST_HEAD(&list); |
| 596 | INIT_LIST_HEAD(&duplicates); |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 597 | tv.bo = &abo->tbo; |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 598 | tv.shared = true; |
| 599 | list_add(&tv.head, &list); |
| 600 | |
Christian König | b5a5ec5 | 2016-03-08 17:47:46 +0100 | [diff] [blame] | 601 | tv_pd.bo = &fpriv->vm.page_directory->tbo; |
| 602 | tv_pd.shared = true; |
| 603 | list_add(&tv_pd.head, &list); |
| 604 | |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 605 | r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 606 | if (r) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 607 | drm_gem_object_unreference_unlocked(gobj); |
| 608 | return r; |
| 609 | } |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 610 | |
Christian König | 765e7fb | 2016-09-15 15:06:50 +0200 | [diff] [blame] | 611 | bo_va = amdgpu_vm_bo_find(&fpriv->vm, abo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 612 | if (!bo_va) { |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 613 | ttm_eu_backoff_reservation(&ticket, &list); |
| 614 | drm_gem_object_unreference_unlocked(gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 615 | return -ENOENT; |
| 616 | } |
| 617 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 618 | switch (args->operation) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 619 | case AMDGPU_VA_OP_MAP: |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 620 | if (args->flags & AMDGPU_VM_PAGE_READABLE) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 621 | va_flags |= AMDGPU_PTE_READABLE; |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 622 | if (args->flags & AMDGPU_VM_PAGE_WRITEABLE) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 623 | va_flags |= AMDGPU_PTE_WRITEABLE; |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 624 | if (args->flags & AMDGPU_VM_PAGE_EXECUTABLE) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 625 | va_flags |= AMDGPU_PTE_EXECUTABLE; |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 626 | r = amdgpu_vm_bo_map(adev, bo_va, args->va_address, |
| 627 | args->offset_in_bo, args->map_size, |
Christian König | 9f7eb53 | 2015-05-18 16:05:57 +0200 | [diff] [blame] | 628 | va_flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 629 | break; |
| 630 | case AMDGPU_VA_OP_UNMAP: |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 631 | r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 632 | break; |
| 633 | default: |
| 634 | break; |
| 635 | } |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 636 | ttm_eu_backoff_reservation(&ticket, &list); |
Christian König | 6378076 | 2016-02-19 10:03:03 +0100 | [diff] [blame] | 637 | if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE) && |
| 638 | !amdgpu_vm_debug) |
monk.liu | 194a336 | 2015-07-22 13:29:28 +0800 | [diff] [blame] | 639 | amdgpu_gem_va_update_vm(adev, bo_va, args->operation); |
Chunming Zhou | e98c1b0 | 2015-11-13 15:22:04 +0800 | [diff] [blame] | 640 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 641 | drm_gem_object_unreference_unlocked(gobj); |
| 642 | return r; |
| 643 | } |
| 644 | |
| 645 | int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data, |
| 646 | struct drm_file *filp) |
| 647 | { |
| 648 | struct drm_amdgpu_gem_op *args = data; |
| 649 | struct drm_gem_object *gobj; |
| 650 | struct amdgpu_bo *robj; |
| 651 | int r; |
| 652 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 653 | gobj = drm_gem_object_lookup(filp, args->handle); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 654 | if (gobj == NULL) { |
| 655 | return -ENOENT; |
| 656 | } |
| 657 | robj = gem_to_amdgpu_bo(gobj); |
| 658 | |
| 659 | r = amdgpu_bo_reserve(robj, false); |
| 660 | if (unlikely(r)) |
| 661 | goto out; |
| 662 | |
| 663 | switch (args->op) { |
| 664 | case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: { |
| 665 | struct drm_amdgpu_gem_create_in info; |
| 666 | void __user *out = (void __user *)(long)args->value; |
| 667 | |
| 668 | info.bo_size = robj->gem_base.size; |
| 669 | info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT; |
Christian König | 1ea863f | 2015-12-18 22:13:12 +0100 | [diff] [blame] | 670 | info.domains = robj->prefered_domains; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 671 | info.domain_flags = robj->flags; |
Christian König | 4c28fb0 | 2015-08-28 17:27:54 +0200 | [diff] [blame] | 672 | amdgpu_bo_unreserve(robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 673 | if (copy_to_user(out, &info, sizeof(info))) |
| 674 | r = -EFAULT; |
| 675 | break; |
| 676 | } |
Marek Olšák | d8f65a2 | 2015-05-27 14:30:38 +0200 | [diff] [blame] | 677 | case AMDGPU_GEM_OP_SET_PLACEMENT: |
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 | } |
Christian König | 1ea863f | 2015-12-18 22:13:12 +0100 | [diff] [blame] | 683 | robj->prefered_domains = args->value & (AMDGPU_GEM_DOMAIN_VRAM | |
| 684 | AMDGPU_GEM_DOMAIN_GTT | |
| 685 | AMDGPU_GEM_DOMAIN_CPU); |
| 686 | robj->allowed_domains = robj->prefered_domains; |
| 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: |
| 698 | drm_gem_object_unreference_unlocked(gobj); |
| 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 */ |
| 726 | drm_gem_object_unreference_unlocked(gobj); |
| 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; |
| 744 | |
| 745 | domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type); |
| 746 | switch (domain) { |
| 747 | case AMDGPU_GEM_DOMAIN_VRAM: |
| 748 | placement = "VRAM"; |
| 749 | break; |
| 750 | case AMDGPU_GEM_DOMAIN_GTT: |
| 751 | placement = " GTT"; |
| 752 | break; |
| 753 | case AMDGPU_GEM_DOMAIN_CPU: |
| 754 | default: |
| 755 | placement = " CPU"; |
| 756 | break; |
| 757 | } |
| 758 | seq_printf(m, "\t0x%08x: %12ld byte %s @ 0x%010Lx", |
| 759 | id, amdgpu_bo_size(bo), placement, |
| 760 | amdgpu_bo_gpu_offset(bo)); |
| 761 | |
| 762 | pin_count = ACCESS_ONCE(bo->pin_count); |
| 763 | if (pin_count) |
| 764 | seq_printf(m, " pin count %d", pin_count); |
| 765 | seq_printf(m, "\n"); |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 770 | static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data) |
| 771 | { |
| 772 | struct drm_info_node *node = (struct drm_info_node *)m->private; |
| 773 | struct drm_device *dev = node->minor->dev; |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 774 | struct drm_file *file; |
| 775 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 776 | |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 777 | r = mutex_lock_interruptible(&dev->filelist_mutex); |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 778 | if (r) |
| 779 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 780 | |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 781 | list_for_each_entry(file, &dev->filelist, lhead) { |
| 782 | struct task_struct *task; |
Christian König | b22e3ce | 2016-02-15 12:41:37 +0100 | [diff] [blame] | 783 | |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 784 | /* |
| 785 | * Although we have a valid reference on file->pid, that does |
| 786 | * not guarantee that the task_struct who called get_pid() is |
| 787 | * still alive (e.g. get_pid(current) => fork() => exit()). |
| 788 | * Therefore, we need to protect this ->comm access using RCU. |
| 789 | */ |
| 790 | rcu_read_lock(); |
| 791 | task = pid_task(file->pid, PIDTYPE_PID); |
| 792 | seq_printf(m, "pid %8d command %s:\n", pid_nr(file->pid), |
| 793 | task ? task->comm : "<unknown>"); |
| 794 | rcu_read_unlock(); |
| 795 | |
| 796 | spin_lock(&file->table_lock); |
| 797 | idr_for_each(&file->object_idr, amdgpu_debugfs_gem_bo_info, m); |
| 798 | spin_unlock(&file->table_lock); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 799 | } |
Christian König | 7ea2356 | 2016-02-15 15:23:00 +0100 | [diff] [blame] | 800 | |
Daniel Vetter | 1d2ac40 | 2016-04-26 19:29:41 +0200 | [diff] [blame] | 801 | mutex_unlock(&dev->filelist_mutex); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 802 | return 0; |
| 803 | } |
| 804 | |
Nils Wallménius | 06ab683 | 2016-05-02 12:46:15 -0400 | [diff] [blame] | 805 | static const struct drm_info_list amdgpu_debugfs_gem_list[] = { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 806 | {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL}, |
| 807 | }; |
| 808 | #endif |
| 809 | |
| 810 | int amdgpu_gem_debugfs_init(struct amdgpu_device *adev) |
| 811 | { |
| 812 | #if defined(CONFIG_DEBUG_FS) |
| 813 | return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1); |
| 814 | #endif |
| 815 | return 0; |
| 816 | } |