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> |
| 29 | #include <drm/drmP.h> |
| 30 | #include <drm/amdgpu_drm.h> |
| 31 | #include "amdgpu.h" |
| 32 | |
| 33 | void amdgpu_gem_object_free(struct drm_gem_object *gobj) |
| 34 | { |
| 35 | struct amdgpu_bo *robj = gem_to_amdgpu_bo(gobj); |
| 36 | |
| 37 | if (robj) { |
| 38 | if (robj->gem_base.import_attach) |
| 39 | drm_prime_gem_destroy(&robj->gem_base, robj->tbo.sg); |
Christian König | 9298e52 | 2015-06-03 21:31:20 +0200 | [diff] [blame] | 40 | amdgpu_mn_unregister(robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 41 | amdgpu_bo_unref(&robj); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | int amdgpu_gem_object_create(struct amdgpu_device *adev, unsigned long size, |
| 46 | int alignment, u32 initial_domain, |
| 47 | u64 flags, bool kernel, |
| 48 | struct drm_gem_object **obj) |
| 49 | { |
| 50 | struct amdgpu_bo *robj; |
| 51 | unsigned long max_size; |
| 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 | |
| 60 | if (!(initial_domain & (AMDGPU_GEM_DOMAIN_GDS | AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA))) { |
| 61 | /* Maximum bo size is the unpinned gtt size since we use the gtt to |
| 62 | * handle vram to system pool migrations. |
| 63 | */ |
| 64 | max_size = adev->mc.gtt_size - adev->gart_pin_size; |
| 65 | if (size > max_size) { |
| 66 | DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n", |
| 67 | size >> 20, max_size >> 20); |
| 68 | return -ENOMEM; |
| 69 | } |
| 70 | } |
| 71 | retry: |
Christian König | 72d7668 | 2015-09-03 17:34:59 +0200 | [diff] [blame] | 72 | r = amdgpu_bo_create(adev, size, alignment, kernel, initial_domain, |
| 73 | flags, NULL, NULL, &robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 74 | if (r) { |
| 75 | if (r != -ERESTARTSYS) { |
| 76 | if (initial_domain == AMDGPU_GEM_DOMAIN_VRAM) { |
| 77 | initial_domain |= AMDGPU_GEM_DOMAIN_GTT; |
| 78 | goto retry; |
| 79 | } |
| 80 | DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n", |
| 81 | size, initial_domain, alignment, r); |
| 82 | } |
| 83 | return r; |
| 84 | } |
| 85 | *obj = &robj->gem_base; |
| 86 | robj->pid = task_pid_nr(current); |
| 87 | |
| 88 | mutex_lock(&adev->gem.mutex); |
| 89 | list_add_tail(&robj->list, &adev->gem.objects); |
| 90 | mutex_unlock(&adev->gem.mutex); |
| 91 | |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | int amdgpu_gem_init(struct amdgpu_device *adev) |
| 96 | { |
| 97 | INIT_LIST_HEAD(&adev->gem.objects); |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | void amdgpu_gem_fini(struct amdgpu_device *adev) |
| 102 | { |
| 103 | amdgpu_bo_force_delete(adev); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Call from drm_gem_handle_create which appear in both new and open ioctl |
| 108 | * case. |
| 109 | */ |
| 110 | int amdgpu_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv) |
| 111 | { |
| 112 | struct amdgpu_bo *rbo = gem_to_amdgpu_bo(obj); |
| 113 | struct amdgpu_device *adev = rbo->adev; |
| 114 | struct amdgpu_fpriv *fpriv = file_priv->driver_priv; |
| 115 | struct amdgpu_vm *vm = &fpriv->vm; |
| 116 | struct amdgpu_bo_va *bo_va; |
| 117 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 118 | r = amdgpu_bo_reserve(rbo, false); |
Chunming Zhou | e98c1b0 | 2015-11-13 15:22:04 +0800 | [diff] [blame] | 119 | if (r) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 120 | return r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 121 | |
| 122 | bo_va = amdgpu_vm_bo_find(vm, rbo); |
| 123 | if (!bo_va) { |
| 124 | bo_va = amdgpu_vm_bo_add(adev, vm, rbo); |
| 125 | } else { |
| 126 | ++bo_va->ref_count; |
| 127 | } |
| 128 | amdgpu_bo_unreserve(rbo); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | void amdgpu_gem_object_close(struct drm_gem_object *obj, |
| 133 | struct drm_file *file_priv) |
| 134 | { |
| 135 | struct amdgpu_bo *rbo = gem_to_amdgpu_bo(obj); |
| 136 | struct amdgpu_device *adev = rbo->adev; |
| 137 | struct amdgpu_fpriv *fpriv = file_priv->driver_priv; |
| 138 | struct amdgpu_vm *vm = &fpriv->vm; |
| 139 | struct amdgpu_bo_va *bo_va; |
| 140 | int r; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 141 | r = amdgpu_bo_reserve(rbo, true); |
| 142 | if (r) { |
| 143 | dev_err(adev->dev, "leaking bo va because " |
| 144 | "we fail to reserve bo (%d)\n", r); |
| 145 | return; |
| 146 | } |
| 147 | bo_va = amdgpu_vm_bo_find(vm, rbo); |
| 148 | if (bo_va) { |
| 149 | if (--bo_va->ref_count == 0) { |
| 150 | amdgpu_vm_bo_rmv(adev, bo_va); |
| 151 | } |
| 152 | } |
| 153 | amdgpu_bo_unreserve(rbo); |
| 154 | } |
| 155 | |
| 156 | static int amdgpu_gem_handle_lockup(struct amdgpu_device *adev, int r) |
| 157 | { |
| 158 | if (r == -EDEADLK) { |
| 159 | r = amdgpu_gpu_reset(adev); |
| 160 | if (!r) |
| 161 | r = -EAGAIN; |
| 162 | } |
| 163 | return r; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * GEM ioctls. |
| 168 | */ |
| 169 | int amdgpu_gem_create_ioctl(struct drm_device *dev, void *data, |
| 170 | struct drm_file *filp) |
| 171 | { |
| 172 | struct amdgpu_device *adev = dev->dev_private; |
| 173 | union drm_amdgpu_gem_create *args = data; |
| 174 | uint64_t size = args->in.bo_size; |
| 175 | struct drm_gem_object *gobj; |
| 176 | uint32_t handle; |
| 177 | bool kernel = false; |
| 178 | int r; |
| 179 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 180 | /* create a gem object to contain this object in */ |
| 181 | if (args->in.domains & (AMDGPU_GEM_DOMAIN_GDS | |
| 182 | AMDGPU_GEM_DOMAIN_GWS | AMDGPU_GEM_DOMAIN_OA)) { |
| 183 | kernel = true; |
| 184 | if (args->in.domains == AMDGPU_GEM_DOMAIN_GDS) |
| 185 | size = size << AMDGPU_GDS_SHIFT; |
| 186 | else if (args->in.domains == AMDGPU_GEM_DOMAIN_GWS) |
| 187 | size = size << AMDGPU_GWS_SHIFT; |
| 188 | else if (args->in.domains == AMDGPU_GEM_DOMAIN_OA) |
| 189 | size = size << AMDGPU_OA_SHIFT; |
| 190 | else { |
| 191 | r = -EINVAL; |
| 192 | goto error_unlock; |
| 193 | } |
| 194 | } |
| 195 | size = roundup(size, PAGE_SIZE); |
| 196 | |
| 197 | r = amdgpu_gem_object_create(adev, size, args->in.alignment, |
| 198 | (u32)(0xffffffff & args->in.domains), |
| 199 | args->in.domain_flags, |
| 200 | kernel, &gobj); |
| 201 | if (r) |
| 202 | goto error_unlock; |
| 203 | |
| 204 | r = drm_gem_handle_create(filp, gobj, &handle); |
| 205 | /* drop reference from allocate - handle holds it now */ |
| 206 | drm_gem_object_unreference_unlocked(gobj); |
| 207 | if (r) |
| 208 | goto error_unlock; |
| 209 | |
| 210 | memset(args, 0, sizeof(*args)); |
| 211 | args->out.handle = handle; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 212 | return 0; |
| 213 | |
| 214 | error_unlock: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 215 | r = amdgpu_gem_handle_lockup(adev, r); |
| 216 | return r; |
| 217 | } |
| 218 | |
| 219 | int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data, |
| 220 | struct drm_file *filp) |
| 221 | { |
| 222 | struct amdgpu_device *adev = dev->dev_private; |
| 223 | struct drm_amdgpu_gem_userptr *args = data; |
| 224 | struct drm_gem_object *gobj; |
| 225 | struct amdgpu_bo *bo; |
| 226 | uint32_t handle; |
| 227 | int r; |
| 228 | |
| 229 | if (offset_in_page(args->addr | args->size)) |
| 230 | return -EINVAL; |
| 231 | |
| 232 | /* reject unknown flag values */ |
| 233 | if (args->flags & ~(AMDGPU_GEM_USERPTR_READONLY | |
| 234 | AMDGPU_GEM_USERPTR_ANONONLY | AMDGPU_GEM_USERPTR_VALIDATE | |
| 235 | AMDGPU_GEM_USERPTR_REGISTER)) |
| 236 | return -EINVAL; |
| 237 | |
Christian König | 585116c | 2015-11-26 11:06:20 +0100 | [diff] [blame] | 238 | if (!(args->flags & AMDGPU_GEM_USERPTR_READONLY) && ( |
| 239 | !(args->flags & AMDGPU_GEM_USERPTR_ANONONLY) || |
| 240 | !(args->flags & AMDGPU_GEM_USERPTR_REGISTER))) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 241 | |
| 242 | /* if we want to write to it we must require anonymous |
| 243 | memory and install a MMU notifier */ |
| 244 | return -EACCES; |
| 245 | } |
| 246 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 247 | /* create a gem object to contain this object in */ |
| 248 | r = amdgpu_gem_object_create(adev, args->size, 0, |
| 249 | AMDGPU_GEM_DOMAIN_CPU, 0, |
| 250 | 0, &gobj); |
| 251 | if (r) |
| 252 | goto handle_lockup; |
| 253 | |
| 254 | bo = gem_to_amdgpu_bo(gobj); |
| 255 | r = amdgpu_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags); |
| 256 | if (r) |
| 257 | goto release_object; |
| 258 | |
| 259 | if (args->flags & AMDGPU_GEM_USERPTR_REGISTER) { |
| 260 | r = amdgpu_mn_register(bo, args->addr); |
| 261 | if (r) |
| 262 | goto release_object; |
| 263 | } |
| 264 | |
| 265 | if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) { |
| 266 | down_read(¤t->mm->mmap_sem); |
| 267 | r = amdgpu_bo_reserve(bo, true); |
| 268 | if (r) { |
| 269 | up_read(¤t->mm->mmap_sem); |
| 270 | goto release_object; |
| 271 | } |
| 272 | |
| 273 | amdgpu_ttm_placement_from_domain(bo, AMDGPU_GEM_DOMAIN_GTT); |
| 274 | r = ttm_bo_validate(&bo->tbo, &bo->placement, true, false); |
| 275 | amdgpu_bo_unreserve(bo); |
| 276 | up_read(¤t->mm->mmap_sem); |
| 277 | if (r) |
| 278 | goto release_object; |
| 279 | } |
| 280 | |
| 281 | r = drm_gem_handle_create(filp, gobj, &handle); |
| 282 | /* drop reference from allocate - handle holds it now */ |
| 283 | drm_gem_object_unreference_unlocked(gobj); |
| 284 | if (r) |
| 285 | goto handle_lockup; |
| 286 | |
| 287 | args->handle = handle; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 288 | return 0; |
| 289 | |
| 290 | release_object: |
| 291 | drm_gem_object_unreference_unlocked(gobj); |
| 292 | |
| 293 | handle_lockup: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 294 | r = amdgpu_gem_handle_lockup(adev, r); |
| 295 | |
| 296 | return r; |
| 297 | } |
| 298 | |
| 299 | int amdgpu_mode_dumb_mmap(struct drm_file *filp, |
| 300 | struct drm_device *dev, |
| 301 | uint32_t handle, uint64_t *offset_p) |
| 302 | { |
| 303 | struct drm_gem_object *gobj; |
| 304 | struct amdgpu_bo *robj; |
| 305 | |
| 306 | gobj = drm_gem_object_lookup(dev, filp, handle); |
| 307 | if (gobj == NULL) { |
| 308 | return -ENOENT; |
| 309 | } |
| 310 | robj = gem_to_amdgpu_bo(gobj); |
Christian König | 271c812 | 2015-05-13 14:30:53 +0200 | [diff] [blame] | 311 | if (amdgpu_ttm_tt_has_userptr(robj->tbo.ttm) || |
| 312 | (robj->flags & AMDGPU_GEM_CREATE_NO_CPU_ACCESS)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 313 | drm_gem_object_unreference_unlocked(gobj); |
| 314 | return -EPERM; |
| 315 | } |
| 316 | *offset_p = amdgpu_bo_mmap_offset(robj); |
| 317 | drm_gem_object_unreference_unlocked(gobj); |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | int amdgpu_gem_mmap_ioctl(struct drm_device *dev, void *data, |
| 322 | struct drm_file *filp) |
| 323 | { |
| 324 | union drm_amdgpu_gem_mmap *args = data; |
| 325 | uint32_t handle = args->in.handle; |
| 326 | memset(args, 0, sizeof(*args)); |
| 327 | return amdgpu_mode_dumb_mmap(filp, dev, handle, &args->out.addr_ptr); |
| 328 | } |
| 329 | |
| 330 | /** |
| 331 | * amdgpu_gem_timeout - calculate jiffies timeout from absolute value |
| 332 | * |
| 333 | * @timeout_ns: timeout in ns |
| 334 | * |
| 335 | * Calculate the timeout in jiffies from an absolute timeout in ns. |
| 336 | */ |
| 337 | unsigned long amdgpu_gem_timeout(uint64_t timeout_ns) |
| 338 | { |
| 339 | unsigned long timeout_jiffies; |
| 340 | ktime_t timeout; |
| 341 | |
| 342 | /* clamp timeout if it's to large */ |
| 343 | if (((int64_t)timeout_ns) < 0) |
| 344 | return MAX_SCHEDULE_TIMEOUT; |
| 345 | |
Christian König | 0f11770 | 2015-07-08 16:58:48 +0200 | [diff] [blame] | 346 | timeout = ktime_sub(ns_to_ktime(timeout_ns), ktime_get()); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 347 | if (ktime_to_ns(timeout) < 0) |
| 348 | return 0; |
| 349 | |
| 350 | timeout_jiffies = nsecs_to_jiffies(ktime_to_ns(timeout)); |
| 351 | /* clamp timeout to avoid unsigned-> signed overflow */ |
| 352 | if (timeout_jiffies > MAX_SCHEDULE_TIMEOUT ) |
| 353 | return MAX_SCHEDULE_TIMEOUT - 1; |
| 354 | |
| 355 | return timeout_jiffies; |
| 356 | } |
| 357 | |
| 358 | int amdgpu_gem_wait_idle_ioctl(struct drm_device *dev, void *data, |
| 359 | struct drm_file *filp) |
| 360 | { |
| 361 | struct amdgpu_device *adev = dev->dev_private; |
| 362 | union drm_amdgpu_gem_wait_idle *args = data; |
| 363 | struct drm_gem_object *gobj; |
| 364 | struct amdgpu_bo *robj; |
| 365 | uint32_t handle = args->in.handle; |
| 366 | unsigned long timeout = amdgpu_gem_timeout(args->in.timeout); |
| 367 | int r = 0; |
| 368 | long ret; |
| 369 | |
| 370 | gobj = drm_gem_object_lookup(dev, filp, handle); |
| 371 | if (gobj == NULL) { |
| 372 | return -ENOENT; |
| 373 | } |
| 374 | robj = gem_to_amdgpu_bo(gobj); |
| 375 | if (timeout == 0) |
| 376 | ret = reservation_object_test_signaled_rcu(robj->tbo.resv, true); |
| 377 | else |
| 378 | ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, timeout); |
| 379 | |
| 380 | /* ret == 0 means not signaled, |
| 381 | * ret > 0 means signaled |
| 382 | * ret < 0 means interrupted before timeout |
| 383 | */ |
| 384 | if (ret >= 0) { |
| 385 | memset(args, 0, sizeof(*args)); |
| 386 | args->out.status = (ret == 0); |
| 387 | } else |
| 388 | r = ret; |
| 389 | |
| 390 | drm_gem_object_unreference_unlocked(gobj); |
| 391 | r = amdgpu_gem_handle_lockup(adev, r); |
| 392 | return r; |
| 393 | } |
| 394 | |
| 395 | int amdgpu_gem_metadata_ioctl(struct drm_device *dev, void *data, |
| 396 | struct drm_file *filp) |
| 397 | { |
| 398 | struct drm_amdgpu_gem_metadata *args = data; |
| 399 | struct drm_gem_object *gobj; |
| 400 | struct amdgpu_bo *robj; |
| 401 | int r = -1; |
| 402 | |
| 403 | DRM_DEBUG("%d \n", args->handle); |
| 404 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 405 | if (gobj == NULL) |
| 406 | return -ENOENT; |
| 407 | robj = gem_to_amdgpu_bo(gobj); |
| 408 | |
| 409 | r = amdgpu_bo_reserve(robj, false); |
| 410 | if (unlikely(r != 0)) |
| 411 | goto out; |
| 412 | |
| 413 | if (args->op == AMDGPU_GEM_METADATA_OP_GET_METADATA) { |
| 414 | amdgpu_bo_get_tiling_flags(robj, &args->data.tiling_info); |
| 415 | r = amdgpu_bo_get_metadata(robj, args->data.data, |
| 416 | sizeof(args->data.data), |
| 417 | &args->data.data_size_bytes, |
| 418 | &args->data.flags); |
| 419 | } else if (args->op == AMDGPU_GEM_METADATA_OP_SET_METADATA) { |
Dan Carpenter | 0913eab | 2015-09-23 14:00:35 +0300 | [diff] [blame] | 420 | if (args->data.data_size_bytes > sizeof(args->data.data)) { |
| 421 | r = -EINVAL; |
| 422 | goto unreserve; |
| 423 | } |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 424 | r = amdgpu_bo_set_tiling_flags(robj, args->data.tiling_info); |
| 425 | if (!r) |
| 426 | r = amdgpu_bo_set_metadata(robj, args->data.data, |
| 427 | args->data.data_size_bytes, |
| 428 | args->data.flags); |
| 429 | } |
| 430 | |
Dan Carpenter | 0913eab | 2015-09-23 14:00:35 +0300 | [diff] [blame] | 431 | unreserve: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 432 | amdgpu_bo_unreserve(robj); |
| 433 | out: |
| 434 | drm_gem_object_unreference_unlocked(gobj); |
| 435 | return r; |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * amdgpu_gem_va_update_vm -update the bo_va in its VM |
| 440 | * |
| 441 | * @adev: amdgpu_device pointer |
| 442 | * @bo_va: bo_va to update |
| 443 | * |
| 444 | * Update the bo_va directly after setting it's address. Errors are not |
| 445 | * vital here, so they are not reported back to userspace. |
| 446 | */ |
| 447 | static void amdgpu_gem_va_update_vm(struct amdgpu_device *adev, |
monk.liu | 194a336 | 2015-07-22 13:29:28 +0800 | [diff] [blame] | 448 | struct amdgpu_bo_va *bo_va, uint32_t operation) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 449 | { |
| 450 | struct ttm_validate_buffer tv, *entry; |
| 451 | struct amdgpu_bo_list_entry *vm_bos; |
| 452 | struct ww_acquire_ctx ticket; |
Christian König | bf60efd | 2015-09-04 10:47:56 +0200 | [diff] [blame] | 453 | struct list_head list, duplicates; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 454 | unsigned domain; |
| 455 | int r; |
| 456 | |
| 457 | INIT_LIST_HEAD(&list); |
Christian König | bf60efd | 2015-09-04 10:47:56 +0200 | [diff] [blame] | 458 | INIT_LIST_HEAD(&duplicates); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 459 | |
| 460 | tv.bo = &bo_va->bo->tbo; |
| 461 | tv.shared = true; |
| 462 | list_add(&tv.head, &list); |
| 463 | |
| 464 | vm_bos = amdgpu_vm_get_bos(adev, bo_va->vm, &list); |
| 465 | if (!vm_bos) |
| 466 | return; |
| 467 | |
Christian König | bf60efd | 2015-09-04 10:47:56 +0200 | [diff] [blame] | 468 | /* Provide duplicates to avoid -EALREADY */ |
| 469 | r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 470 | if (r) |
| 471 | goto error_free; |
| 472 | |
| 473 | list_for_each_entry(entry, &list, head) { |
| 474 | domain = amdgpu_mem_type_to_domain(entry->bo->mem.mem_type); |
| 475 | /* if anything is swapped out don't swap it in here, |
| 476 | just abort and wait for the next CS */ |
| 477 | if (domain == AMDGPU_GEM_DOMAIN_CPU) |
| 478 | goto error_unreserve; |
| 479 | } |
Chunming Zhou | 43c27fb | 2015-11-12 15:33:09 +0800 | [diff] [blame] | 480 | r = amdgpu_vm_update_page_directory(adev, bo_va->vm); |
| 481 | if (r) |
| 482 | goto error_unreserve; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 483 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 484 | r = amdgpu_vm_clear_freed(adev, bo_va->vm); |
| 485 | if (r) |
Chunming Zhou | f48b265 | 2015-10-16 14:06:19 +0800 | [diff] [blame] | 486 | goto error_unreserve; |
monk.liu | 194a336 | 2015-07-22 13:29:28 +0800 | [diff] [blame] | 487 | |
| 488 | if (operation == AMDGPU_VA_OP_MAP) |
| 489 | r = amdgpu_vm_bo_update(adev, bo_va, &bo_va->bo->tbo.mem); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 490 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 491 | error_unreserve: |
| 492 | ttm_eu_backoff_reservation(&ticket, &list); |
| 493 | |
| 494 | error_free: |
| 495 | drm_free_large(vm_bos); |
| 496 | |
Christian König | 68fdd3d | 2015-06-16 14:50:02 +0200 | [diff] [blame] | 497 | if (r && r != -ERESTARTSYS) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 498 | DRM_ERROR("Couldn't update BO_VA (%d)\n", r); |
| 499 | } |
| 500 | |
| 501 | |
| 502 | |
| 503 | int amdgpu_gem_va_ioctl(struct drm_device *dev, void *data, |
| 504 | struct drm_file *filp) |
| 505 | { |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 506 | struct drm_amdgpu_gem_va *args = data; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 507 | struct drm_gem_object *gobj; |
| 508 | struct amdgpu_device *adev = dev->dev_private; |
| 509 | struct amdgpu_fpriv *fpriv = filp->driver_priv; |
| 510 | struct amdgpu_bo *rbo; |
| 511 | struct amdgpu_bo_va *bo_va; |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 512 | struct ttm_validate_buffer tv, tv_pd; |
| 513 | struct ww_acquire_ctx ticket; |
| 514 | struct list_head list, duplicates; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 515 | uint32_t invalid_flags, va_flags = 0; |
| 516 | int r = 0; |
| 517 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 518 | if (!adev->vm_manager.enabled) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 519 | return -ENOTTY; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 520 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 521 | if (args->va_address < AMDGPU_VA_RESERVED_SIZE) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 522 | dev_err(&dev->pdev->dev, |
| 523 | "va_address 0x%lX is in reserved area 0x%X\n", |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 524 | (unsigned long)args->va_address, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 525 | AMDGPU_VA_RESERVED_SIZE); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 526 | return -EINVAL; |
| 527 | } |
| 528 | |
Christian König | fc220f6 | 2015-06-29 17:12:20 +0200 | [diff] [blame] | 529 | invalid_flags = ~(AMDGPU_VM_DELAY_UPDATE | AMDGPU_VM_PAGE_READABLE | |
| 530 | AMDGPU_VM_PAGE_WRITEABLE | AMDGPU_VM_PAGE_EXECUTABLE); |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 531 | if ((args->flags & invalid_flags)) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 532 | 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] | 533 | args->flags, invalid_flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 534 | return -EINVAL; |
| 535 | } |
| 536 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 537 | switch (args->operation) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 538 | case AMDGPU_VA_OP_MAP: |
| 539 | case AMDGPU_VA_OP_UNMAP: |
| 540 | break; |
| 541 | default: |
| 542 | dev_err(&dev->pdev->dev, "unsupported operation %d\n", |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 543 | args->operation); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 544 | return -EINVAL; |
| 545 | } |
| 546 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 547 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 548 | if (gobj == NULL) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 549 | return -ENOENT; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 550 | rbo = gem_to_amdgpu_bo(gobj); |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 551 | INIT_LIST_HEAD(&list); |
| 552 | INIT_LIST_HEAD(&duplicates); |
| 553 | tv.bo = &rbo->tbo; |
| 554 | tv.shared = true; |
| 555 | list_add(&tv.head, &list); |
| 556 | |
| 557 | if (args->operation == AMDGPU_VA_OP_MAP) { |
| 558 | tv_pd.bo = &fpriv->vm.page_directory->tbo; |
| 559 | tv_pd.shared = true; |
| 560 | list_add(&tv_pd.head, &list); |
| 561 | } |
| 562 | r = ttm_eu_reserve_buffers(&ticket, &list, true, &duplicates); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 563 | if (r) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 564 | drm_gem_object_unreference_unlocked(gobj); |
| 565 | return r; |
| 566 | } |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 567 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 568 | bo_va = amdgpu_vm_bo_find(&fpriv->vm, rbo); |
| 569 | if (!bo_va) { |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 570 | ttm_eu_backoff_reservation(&ticket, &list); |
| 571 | drm_gem_object_unreference_unlocked(gobj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 572 | return -ENOENT; |
| 573 | } |
| 574 | |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 575 | switch (args->operation) { |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 576 | case AMDGPU_VA_OP_MAP: |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 577 | if (args->flags & AMDGPU_VM_PAGE_READABLE) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 578 | va_flags |= AMDGPU_PTE_READABLE; |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 579 | if (args->flags & AMDGPU_VM_PAGE_WRITEABLE) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 580 | va_flags |= AMDGPU_PTE_WRITEABLE; |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 581 | if (args->flags & AMDGPU_VM_PAGE_EXECUTABLE) |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 582 | va_flags |= AMDGPU_PTE_EXECUTABLE; |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 583 | r = amdgpu_vm_bo_map(adev, bo_va, args->va_address, |
| 584 | args->offset_in_bo, args->map_size, |
Christian König | 9f7eb53 | 2015-05-18 16:05:57 +0200 | [diff] [blame] | 585 | va_flags); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 586 | break; |
| 587 | case AMDGPU_VA_OP_UNMAP: |
Christian König | 34b5f6a | 2015-06-08 15:03:00 +0200 | [diff] [blame] | 588 | r = amdgpu_vm_bo_unmap(adev, bo_va, args->va_address); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 589 | break; |
| 590 | default: |
| 591 | break; |
| 592 | } |
Chunming Zhou | 49b02b1 | 2015-11-13 14:18:38 +0800 | [diff] [blame] | 593 | ttm_eu_backoff_reservation(&ticket, &list); |
Christian König | fc220f6 | 2015-06-29 17:12:20 +0200 | [diff] [blame] | 594 | if (!r && !(args->flags & AMDGPU_VM_DELAY_UPDATE)) |
monk.liu | 194a336 | 2015-07-22 13:29:28 +0800 | [diff] [blame] | 595 | amdgpu_gem_va_update_vm(adev, bo_va, args->operation); |
Chunming Zhou | e98c1b0 | 2015-11-13 15:22:04 +0800 | [diff] [blame] | 596 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 597 | drm_gem_object_unreference_unlocked(gobj); |
| 598 | return r; |
| 599 | } |
| 600 | |
| 601 | int amdgpu_gem_op_ioctl(struct drm_device *dev, void *data, |
| 602 | struct drm_file *filp) |
| 603 | { |
| 604 | struct drm_amdgpu_gem_op *args = data; |
| 605 | struct drm_gem_object *gobj; |
| 606 | struct amdgpu_bo *robj; |
| 607 | int r; |
| 608 | |
| 609 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 610 | if (gobj == NULL) { |
| 611 | return -ENOENT; |
| 612 | } |
| 613 | robj = gem_to_amdgpu_bo(gobj); |
| 614 | |
| 615 | r = amdgpu_bo_reserve(robj, false); |
| 616 | if (unlikely(r)) |
| 617 | goto out; |
| 618 | |
| 619 | switch (args->op) { |
| 620 | case AMDGPU_GEM_OP_GET_GEM_CREATE_INFO: { |
| 621 | struct drm_amdgpu_gem_create_in info; |
| 622 | void __user *out = (void __user *)(long)args->value; |
| 623 | |
| 624 | info.bo_size = robj->gem_base.size; |
| 625 | info.alignment = robj->tbo.mem.page_alignment << PAGE_SHIFT; |
| 626 | info.domains = robj->initial_domain; |
| 627 | info.domain_flags = robj->flags; |
Christian König | 4c28fb0 | 2015-08-28 17:27:54 +0200 | [diff] [blame] | 628 | amdgpu_bo_unreserve(robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 629 | if (copy_to_user(out, &info, sizeof(info))) |
| 630 | r = -EFAULT; |
| 631 | break; |
| 632 | } |
Marek Olšák | d8f65a2 | 2015-05-27 14:30:38 +0200 | [diff] [blame] | 633 | case AMDGPU_GEM_OP_SET_PLACEMENT: |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 634 | if (amdgpu_ttm_tt_has_userptr(robj->tbo.ttm)) { |
| 635 | r = -EPERM; |
Christian König | 4c28fb0 | 2015-08-28 17:27:54 +0200 | [diff] [blame] | 636 | amdgpu_bo_unreserve(robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 637 | break; |
| 638 | } |
| 639 | robj->initial_domain = args->value & (AMDGPU_GEM_DOMAIN_VRAM | |
| 640 | AMDGPU_GEM_DOMAIN_GTT | |
| 641 | AMDGPU_GEM_DOMAIN_CPU); |
Christian König | 4c28fb0 | 2015-08-28 17:27:54 +0200 | [diff] [blame] | 642 | amdgpu_bo_unreserve(robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 643 | break; |
| 644 | default: |
Christian König | 4c28fb0 | 2015-08-28 17:27:54 +0200 | [diff] [blame] | 645 | amdgpu_bo_unreserve(robj); |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 646 | r = -EINVAL; |
| 647 | } |
| 648 | |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 649 | out: |
| 650 | drm_gem_object_unreference_unlocked(gobj); |
| 651 | return r; |
| 652 | } |
| 653 | |
| 654 | int amdgpu_mode_dumb_create(struct drm_file *file_priv, |
| 655 | struct drm_device *dev, |
| 656 | struct drm_mode_create_dumb *args) |
| 657 | { |
| 658 | struct amdgpu_device *adev = dev->dev_private; |
| 659 | struct drm_gem_object *gobj; |
| 660 | uint32_t handle; |
| 661 | int r; |
| 662 | |
| 663 | args->pitch = amdgpu_align_pitch(adev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8); |
Dan Carpenter | 54ef0b5 | 2015-09-23 14:00:59 +0300 | [diff] [blame] | 664 | args->size = (u64)args->pitch * args->height; |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 665 | args->size = ALIGN(args->size, PAGE_SIZE); |
| 666 | |
| 667 | r = amdgpu_gem_object_create(adev, args->size, 0, |
| 668 | AMDGPU_GEM_DOMAIN_VRAM, |
Alex Deucher | 857d913 | 2015-08-27 00:14:16 -0400 | [diff] [blame] | 669 | AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED, |
| 670 | ttm_bo_type_device, |
Alex Deucher | d38ceaf | 2015-04-20 16:55:21 -0400 | [diff] [blame] | 671 | &gobj); |
| 672 | if (r) |
| 673 | return -ENOMEM; |
| 674 | |
| 675 | r = drm_gem_handle_create(file_priv, gobj, &handle); |
| 676 | /* drop reference from allocate - handle holds it now */ |
| 677 | drm_gem_object_unreference_unlocked(gobj); |
| 678 | if (r) { |
| 679 | return r; |
| 680 | } |
| 681 | args->handle = handle; |
| 682 | return 0; |
| 683 | } |
| 684 | |
| 685 | #if defined(CONFIG_DEBUG_FS) |
| 686 | static int amdgpu_debugfs_gem_info(struct seq_file *m, void *data) |
| 687 | { |
| 688 | struct drm_info_node *node = (struct drm_info_node *)m->private; |
| 689 | struct drm_device *dev = node->minor->dev; |
| 690 | struct amdgpu_device *adev = dev->dev_private; |
| 691 | struct amdgpu_bo *rbo; |
| 692 | unsigned i = 0; |
| 693 | |
| 694 | mutex_lock(&adev->gem.mutex); |
| 695 | list_for_each_entry(rbo, &adev->gem.objects, list) { |
| 696 | unsigned domain; |
| 697 | const char *placement; |
| 698 | |
| 699 | domain = amdgpu_mem_type_to_domain(rbo->tbo.mem.mem_type); |
| 700 | switch (domain) { |
| 701 | case AMDGPU_GEM_DOMAIN_VRAM: |
| 702 | placement = "VRAM"; |
| 703 | break; |
| 704 | case AMDGPU_GEM_DOMAIN_GTT: |
| 705 | placement = " GTT"; |
| 706 | break; |
| 707 | case AMDGPU_GEM_DOMAIN_CPU: |
| 708 | default: |
| 709 | placement = " CPU"; |
| 710 | break; |
| 711 | } |
| 712 | seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n", |
| 713 | i, amdgpu_bo_size(rbo) >> 10, amdgpu_bo_size(rbo) >> 20, |
| 714 | placement, (unsigned long)rbo->pid); |
| 715 | i++; |
| 716 | } |
| 717 | mutex_unlock(&adev->gem.mutex); |
| 718 | return 0; |
| 719 | } |
| 720 | |
| 721 | static struct drm_info_list amdgpu_debugfs_gem_list[] = { |
| 722 | {"amdgpu_gem_info", &amdgpu_debugfs_gem_info, 0, NULL}, |
| 723 | }; |
| 724 | #endif |
| 725 | |
| 726 | int amdgpu_gem_debugfs_init(struct amdgpu_device *adev) |
| 727 | { |
| 728 | #if defined(CONFIG_DEBUG_FS) |
| 729 | return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_gem_list, 1); |
| 730 | #endif |
| 731 | return 0; |
| 732 | } |