Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [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 | */ |
David Howells | 760285e | 2012-10-02 18:01:07 +0100 | [diff] [blame] | 28 | #include <drm/drmP.h> |
| 29 | #include <drm/radeon_drm.h> |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 30 | #include "radeon.h" |
| 31 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 32 | void radeon_gem_object_free(struct drm_gem_object *gobj) |
| 33 | { |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 34 | struct radeon_bo *robj = gem_to_radeon_bo(gobj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 35 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 36 | if (robj) { |
Christian König | 12f1384 | 2015-07-14 15:58:30 +0200 | [diff] [blame] | 37 | radeon_mn_unregister(robj); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 38 | radeon_bo_unref(&robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | |
Alex Deucher | 391bfec | 2014-07-17 12:26:29 -0400 | [diff] [blame] | 42 | int radeon_gem_object_create(struct radeon_device *rdev, unsigned long size, |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 43 | int alignment, int initial_domain, |
Christian König | ed5cb43 | 2014-07-21 13:27:27 +0200 | [diff] [blame] | 44 | u32 flags, bool kernel, |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 45 | struct drm_gem_object **obj) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 46 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 47 | struct radeon_bo *robj; |
Christian König | 6c0d112 | 2012-10-23 15:53:18 +0200 | [diff] [blame] | 48 | unsigned long max_size; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 49 | int r; |
| 50 | |
| 51 | *obj = NULL; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 52 | /* At least align on page size */ |
| 53 | if (alignment < PAGE_SIZE) { |
| 54 | alignment = PAGE_SIZE; |
| 55 | } |
Christian König | 6c0d112 | 2012-10-23 15:53:18 +0200 | [diff] [blame] | 56 | |
Alex Deucher | 391bfec | 2014-07-17 12:26:29 -0400 | [diff] [blame] | 57 | /* Maximum bo size is the unpinned gtt size since we use the gtt to |
| 58 | * handle vram to system pool migrations. |
| 59 | */ |
| 60 | max_size = rdev->mc.gtt_size - rdev->gart_pin_size; |
Christian König | 6c0d112 | 2012-10-23 15:53:18 +0200 | [diff] [blame] | 61 | if (size > max_size) { |
Alex Deucher | 391bfec | 2014-07-17 12:26:29 -0400 | [diff] [blame] | 62 | DRM_DEBUG("Allocation size %ldMb bigger than %ldMb limit\n", |
Michel Dänzer | 380670a | 2014-07-16 18:40:32 +0900 | [diff] [blame] | 63 | size >> 20, max_size >> 20); |
Christian König | 6c0d112 | 2012-10-23 15:53:18 +0200 | [diff] [blame] | 64 | return -ENOMEM; |
| 65 | } |
| 66 | |
Christian König | 0fe7158 | 2012-10-23 15:53:19 +0200 | [diff] [blame] | 67 | retry: |
Michel Dänzer | 02376d8 | 2014-07-17 19:01:08 +0900 | [diff] [blame] | 68 | r = radeon_bo_create(rdev, size, alignment, kernel, initial_domain, |
Maarten Lankhorst | 831b696 | 2014-09-18 14:11:56 +0200 | [diff] [blame] | 69 | flags, NULL, NULL, &robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 70 | if (r) { |
Christian König | 0fe7158 | 2012-10-23 15:53:19 +0200 | [diff] [blame] | 71 | if (r != -ERESTARTSYS) { |
| 72 | if (initial_domain == RADEON_GEM_DOMAIN_VRAM) { |
| 73 | initial_domain |= RADEON_GEM_DOMAIN_GTT; |
| 74 | goto retry; |
| 75 | } |
Alex Deucher | 391bfec | 2014-07-17 12:26:29 -0400 | [diff] [blame] | 76 | DRM_ERROR("Failed to allocate GEM object (%ld, %d, %u, %d)\n", |
Dave Airlie | ecabd32 | 2009-12-15 10:39:48 +1000 | [diff] [blame] | 77 | size, initial_domain, alignment, r); |
Christian König | 0fe7158 | 2012-10-23 15:53:19 +0200 | [diff] [blame] | 78 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 79 | return r; |
| 80 | } |
Daniel Vetter | 441921d | 2011-02-18 17:59:16 +0100 | [diff] [blame] | 81 | *obj = &robj->gem_base; |
Jerome Glisse | 409851f | 2013-04-25 22:29:27 -0400 | [diff] [blame] | 82 | robj->pid = task_pid_nr(current); |
Daniel Vetter | 441921d | 2011-02-18 17:59:16 +0100 | [diff] [blame] | 83 | |
| 84 | mutex_lock(&rdev->gem.mutex); |
| 85 | list_add_tail(&robj->list, &rdev->gem.objects); |
| 86 | mutex_unlock(&rdev->gem.mutex); |
| 87 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 88 | return 0; |
| 89 | } |
| 90 | |
Rashika Kheria | 248a6c4 | 2014-01-06 20:58:45 +0530 | [diff] [blame] | 91 | static int radeon_gem_set_domain(struct drm_gem_object *gobj, |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 92 | uint32_t rdomain, uint32_t wdomain) |
| 93 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 94 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 95 | uint32_t domain; |
Maarten Lankhorst | 39e7f6f | 2014-05-14 15:40:49 +0200 | [diff] [blame] | 96 | long r; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 97 | |
| 98 | /* FIXME: reeimplement */ |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 99 | robj = gem_to_radeon_bo(gobj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 100 | /* work out where to validate the buffer to */ |
| 101 | domain = wdomain; |
| 102 | if (!domain) { |
| 103 | domain = rdomain; |
| 104 | } |
| 105 | if (!domain) { |
| 106 | /* Do nothings */ |
Joe Perches | 7ca8529 | 2017-02-28 04:55:52 -0800 | [diff] [blame] | 107 | pr_warn("Set domain without domain !\n"); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 108 | return 0; |
| 109 | } |
| 110 | if (domain == RADEON_GEM_DOMAIN_CPU) { |
| 111 | /* Asking for cpu access wait for object idle */ |
Maarten Lankhorst | 39e7f6f | 2014-05-14 15:40:49 +0200 | [diff] [blame] | 112 | r = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ); |
| 113 | if (!r) |
| 114 | r = -EBUSY; |
| 115 | |
| 116 | if (r < 0 && r != -EINTR) { |
Joe Perches | 7ca8529 | 2017-02-28 04:55:52 -0800 | [diff] [blame] | 117 | pr_err("Failed to wait for object: %li\n", r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 118 | return r; |
| 119 | } |
| 120 | } |
Christopher James Halse Rogers | ede2e01 | 2017-04-03 13:35:23 +1000 | [diff] [blame] | 121 | if (domain == RADEON_GEM_DOMAIN_VRAM && robj->prime_shared_count) { |
| 122 | /* A BO that is associated with a dma-buf cannot be sensibly migrated to VRAM */ |
| 123 | return -EINVAL; |
| 124 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int radeon_gem_init(struct radeon_device *rdev) |
| 129 | { |
| 130 | INIT_LIST_HEAD(&rdev->gem.objects); |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | void radeon_gem_fini(struct radeon_device *rdev) |
| 135 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 136 | radeon_bo_force_delete(rdev); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 137 | } |
| 138 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 139 | /* |
| 140 | * Call from drm_gem_handle_create which appear in both new and open ioctl |
| 141 | * case. |
| 142 | */ |
| 143 | int radeon_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv) |
| 144 | { |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame] | 145 | struct radeon_bo *rbo = gem_to_radeon_bo(obj); |
| 146 | struct radeon_device *rdev = rbo->rdev; |
| 147 | struct radeon_fpriv *fpriv = file_priv->driver_priv; |
| 148 | struct radeon_vm *vm = &fpriv->vm; |
| 149 | struct radeon_bo_va *bo_va; |
| 150 | int r; |
| 151 | |
Alex Deucher | 544143f | 2015-01-28 14:36:26 -0500 | [diff] [blame] | 152 | if ((rdev->family < CHIP_CAYMAN) || |
| 153 | (!rdev->accel_working)) { |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame] | 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | r = radeon_bo_reserve(rbo, false); |
| 158 | if (r) { |
| 159 | return r; |
| 160 | } |
| 161 | |
| 162 | bo_va = radeon_vm_bo_find(vm, rbo); |
| 163 | if (!bo_va) { |
| 164 | bo_va = radeon_vm_bo_add(rdev, vm, rbo); |
| 165 | } else { |
| 166 | ++bo_va->ref_count; |
| 167 | } |
| 168 | radeon_bo_unreserve(rbo); |
| 169 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | void radeon_gem_object_close(struct drm_gem_object *obj, |
| 174 | struct drm_file *file_priv) |
| 175 | { |
| 176 | struct radeon_bo *rbo = gem_to_radeon_bo(obj); |
| 177 | struct radeon_device *rdev = rbo->rdev; |
| 178 | struct radeon_fpriv *fpriv = file_priv->driver_priv; |
| 179 | struct radeon_vm *vm = &fpriv->vm; |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame] | 180 | struct radeon_bo_va *bo_va; |
Christian König | d59f702 | 2012-09-11 16:10:02 +0200 | [diff] [blame] | 181 | int r; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 182 | |
Alex Deucher | 544143f | 2015-01-28 14:36:26 -0500 | [diff] [blame] | 183 | if ((rdev->family < CHIP_CAYMAN) || |
| 184 | (!rdev->accel_working)) { |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
Christian König | d59f702 | 2012-09-11 16:10:02 +0200 | [diff] [blame] | 188 | r = radeon_bo_reserve(rbo, true); |
| 189 | if (r) { |
| 190 | dev_err(rdev->dev, "leaking bo va because " |
| 191 | "we fail to reserve bo (%d)\n", r); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 192 | return; |
| 193 | } |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame] | 194 | bo_va = radeon_vm_bo_find(vm, rbo); |
| 195 | if (bo_va) { |
| 196 | if (--bo_va->ref_count == 0) { |
| 197 | radeon_vm_bo_rmv(rdev, bo_va); |
| 198 | } |
| 199 | } |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 200 | radeon_bo_unreserve(rbo); |
| 201 | } |
| 202 | |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 203 | static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r) |
| 204 | { |
| 205 | if (r == -EDEADLK) { |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 206 | r = radeon_gpu_reset(rdev); |
| 207 | if (!r) |
| 208 | r = -EAGAIN; |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 209 | } |
| 210 | return r; |
| 211 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 212 | |
| 213 | /* |
| 214 | * GEM ioctls. |
| 215 | */ |
| 216 | int radeon_gem_info_ioctl(struct drm_device *dev, void *data, |
| 217 | struct drm_file *filp) |
| 218 | { |
| 219 | struct radeon_device *rdev = dev->dev_private; |
| 220 | struct drm_radeon_gem_info *args = data; |
Dave Airlie | 5359533 | 2011-03-14 09:47:24 +1000 | [diff] [blame] | 221 | struct ttm_mem_type_manager *man; |
| 222 | |
| 223 | man = &rdev->mman.bdev.man[TTM_PL_VRAM]; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 224 | |
Michel Dänzer | 51964e9 | 2017-01-30 12:06:35 +0900 | [diff] [blame] | 225 | args->vram_size = (u64)man->size << PAGE_SHIFT; |
| 226 | args->vram_visible = rdev->mc.visible_vram_size; |
Alex Deucher | ccbe006 | 2014-07-17 12:16:20 -0400 | [diff] [blame] | 227 | args->vram_visible -= rdev->vram_pin_size; |
| 228 | args->gart_size = rdev->mc.gtt_size; |
| 229 | args->gart_size -= rdev->gart_pin_size; |
| 230 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | int radeon_gem_pread_ioctl(struct drm_device *dev, void *data, |
| 235 | struct drm_file *filp) |
| 236 | { |
| 237 | /* TODO: implement */ |
| 238 | DRM_ERROR("unimplemented %s\n", __func__); |
| 239 | return -ENOSYS; |
| 240 | } |
| 241 | |
| 242 | int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
| 243 | struct drm_file *filp) |
| 244 | { |
| 245 | /* TODO: implement */ |
| 246 | DRM_ERROR("unimplemented %s\n", __func__); |
| 247 | return -ENOSYS; |
| 248 | } |
| 249 | |
| 250 | int radeon_gem_create_ioctl(struct drm_device *dev, void *data, |
| 251 | struct drm_file *filp) |
| 252 | { |
| 253 | struct radeon_device *rdev = dev->dev_private; |
| 254 | struct drm_radeon_gem_create *args = data; |
| 255 | struct drm_gem_object *gobj; |
| 256 | uint32_t handle; |
| 257 | int r; |
| 258 | |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 259 | down_read(&rdev->exclusive_lock); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 260 | /* create a gem object to contain this object in */ |
| 261 | args->size = roundup(args->size, PAGE_SIZE); |
| 262 | r = radeon_gem_object_create(rdev, args->size, args->alignment, |
Michel Dänzer | 02376d8 | 2014-07-17 19:01:08 +0900 | [diff] [blame] | 263 | args->initial_domain, args->flags, |
Christian König | ed5cb43 | 2014-07-21 13:27:27 +0200 | [diff] [blame] | 264 | false, &gobj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 265 | if (r) { |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 266 | up_read(&rdev->exclusive_lock); |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 267 | r = radeon_gem_handle_lockup(rdev, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 268 | return r; |
| 269 | } |
| 270 | r = drm_gem_handle_create(filp, gobj, &handle); |
Dave Airlie | 29d08b3 | 2010-09-27 16:17:17 +1000 | [diff] [blame] | 271 | /* drop reference from allocate - handle holds it now */ |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 272 | drm_gem_object_put_unlocked(gobj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 273 | if (r) { |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 274 | up_read(&rdev->exclusive_lock); |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 275 | r = radeon_gem_handle_lockup(rdev, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 276 | return r; |
| 277 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 278 | args->handle = handle; |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 279 | up_read(&rdev->exclusive_lock); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 280 | return 0; |
| 281 | } |
| 282 | |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 283 | int radeon_gem_userptr_ioctl(struct drm_device *dev, void *data, |
| 284 | struct drm_file *filp) |
| 285 | { |
Christian König | 19be557 | 2017-04-12 14:24:39 +0200 | [diff] [blame] | 286 | struct ttm_operation_ctx ctx = { true, false }; |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 287 | struct radeon_device *rdev = dev->dev_private; |
| 288 | struct drm_radeon_gem_userptr *args = data; |
| 289 | struct drm_gem_object *gobj; |
| 290 | struct radeon_bo *bo; |
| 291 | uint32_t handle; |
| 292 | int r; |
| 293 | |
| 294 | if (offset_in_page(args->addr | args->size)) |
| 295 | return -EINVAL; |
| 296 | |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 297 | /* reject unknown flag values */ |
Christian König | ddd00e3 | 2014-08-07 09:36:01 +0200 | [diff] [blame] | 298 | if (args->flags & ~(RADEON_GEM_USERPTR_READONLY | |
Christian König | 341cb9e | 2014-08-07 09:36:03 +0200 | [diff] [blame] | 299 | RADEON_GEM_USERPTR_ANONONLY | RADEON_GEM_USERPTR_VALIDATE | |
| 300 | RADEON_GEM_USERPTR_REGISTER)) |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 301 | return -EINVAL; |
| 302 | |
Christian König | bd645e4 | 2014-08-07 09:36:04 +0200 | [diff] [blame] | 303 | if (args->flags & RADEON_GEM_USERPTR_READONLY) { |
| 304 | /* readonly pages not tested on older hardware */ |
| 305 | if (rdev->family < CHIP_R600) |
| 306 | return -EINVAL; |
| 307 | |
| 308 | } else if (!(args->flags & RADEON_GEM_USERPTR_ANONONLY) || |
| 309 | !(args->flags & RADEON_GEM_USERPTR_REGISTER)) { |
| 310 | |
| 311 | /* if we want to write to it we must require anonymous |
| 312 | memory and install a MMU notifier */ |
| 313 | return -EACCES; |
| 314 | } |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 315 | |
| 316 | down_read(&rdev->exclusive_lock); |
| 317 | |
| 318 | /* create a gem object to contain this object in */ |
| 319 | r = radeon_gem_object_create(rdev, args->size, 0, |
| 320 | RADEON_GEM_DOMAIN_CPU, 0, |
| 321 | false, &gobj); |
| 322 | if (r) |
| 323 | goto handle_lockup; |
| 324 | |
| 325 | bo = gem_to_radeon_bo(gobj); |
| 326 | r = radeon_ttm_tt_set_userptr(bo->tbo.ttm, args->addr, args->flags); |
| 327 | if (r) |
| 328 | goto release_object; |
| 329 | |
Christian König | 341cb9e | 2014-08-07 09:36:03 +0200 | [diff] [blame] | 330 | if (args->flags & RADEON_GEM_USERPTR_REGISTER) { |
| 331 | r = radeon_mn_register(bo, args->addr); |
| 332 | if (r) |
| 333 | goto release_object; |
| 334 | } |
| 335 | |
Christian König | 2a84a44 | 2014-08-07 09:36:02 +0200 | [diff] [blame] | 336 | if (args->flags & RADEON_GEM_USERPTR_VALIDATE) { |
| 337 | down_read(¤t->mm->mmap_sem); |
| 338 | r = radeon_bo_reserve(bo, true); |
| 339 | if (r) { |
| 340 | up_read(¤t->mm->mmap_sem); |
| 341 | goto release_object; |
| 342 | } |
| 343 | |
| 344 | radeon_ttm_placement_from_domain(bo, RADEON_GEM_DOMAIN_GTT); |
Christian König | 19be557 | 2017-04-12 14:24:39 +0200 | [diff] [blame] | 345 | r = ttm_bo_validate(&bo->tbo, &bo->placement, &ctx); |
Christian König | 2a84a44 | 2014-08-07 09:36:02 +0200 | [diff] [blame] | 346 | radeon_bo_unreserve(bo); |
| 347 | up_read(¤t->mm->mmap_sem); |
| 348 | if (r) |
| 349 | goto release_object; |
| 350 | } |
| 351 | |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 352 | r = drm_gem_handle_create(filp, gobj, &handle); |
| 353 | /* drop reference from allocate - handle holds it now */ |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 354 | drm_gem_object_put_unlocked(gobj); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 355 | if (r) |
| 356 | goto handle_lockup; |
| 357 | |
| 358 | args->handle = handle; |
| 359 | up_read(&rdev->exclusive_lock); |
| 360 | return 0; |
| 361 | |
| 362 | release_object: |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 363 | drm_gem_object_put_unlocked(gobj); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 364 | |
| 365 | handle_lockup: |
| 366 | up_read(&rdev->exclusive_lock); |
| 367 | r = radeon_gem_handle_lockup(rdev, r); |
| 368 | |
| 369 | return r; |
| 370 | } |
| 371 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 372 | int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
| 373 | struct drm_file *filp) |
| 374 | { |
| 375 | /* transition the BO to a domain - |
| 376 | * just validate the BO into a certain domain */ |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 377 | struct radeon_device *rdev = dev->dev_private; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 378 | struct drm_radeon_gem_set_domain *args = data; |
| 379 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 380 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 381 | int r; |
| 382 | |
| 383 | /* for now if someone requests domain CPU - |
| 384 | * just make sure the buffer is finished with */ |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 385 | down_read(&rdev->exclusive_lock); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 386 | |
| 387 | /* just do a BO wait for now */ |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 388 | gobj = drm_gem_object_lookup(filp, args->handle); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 389 | if (gobj == NULL) { |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 390 | up_read(&rdev->exclusive_lock); |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 391 | return -ENOENT; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 392 | } |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 393 | robj = gem_to_radeon_bo(gobj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 394 | |
| 395 | r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain); |
| 396 | |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 397 | drm_gem_object_put_unlocked(gobj); |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 398 | up_read(&rdev->exclusive_lock); |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 399 | r = radeon_gem_handle_lockup(robj->rdev, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 400 | return r; |
| 401 | } |
| 402 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 403 | int radeon_mode_dumb_mmap(struct drm_file *filp, |
| 404 | struct drm_device *dev, |
| 405 | uint32_t handle, uint64_t *offset_p) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 406 | { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 407 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 408 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 409 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 410 | gobj = drm_gem_object_lookup(filp, handle); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 411 | if (gobj == NULL) { |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 412 | return -ENOENT; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 413 | } |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 414 | robj = gem_to_radeon_bo(gobj); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 415 | if (radeon_ttm_tt_has_userptr(robj->tbo.ttm)) { |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 416 | drm_gem_object_put_unlocked(gobj); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 417 | return -EPERM; |
| 418 | } |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 419 | *offset_p = radeon_bo_mmap_offset(robj); |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 420 | drm_gem_object_put_unlocked(gobj); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 421 | return 0; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 422 | } |
| 423 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 424 | int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data, |
| 425 | struct drm_file *filp) |
| 426 | { |
| 427 | struct drm_radeon_gem_mmap *args = data; |
| 428 | |
Dave Airlie | da6b51d | 2014-12-24 13:11:17 +1000 | [diff] [blame] | 429 | return radeon_mode_dumb_mmap(filp, dev, args->handle, &args->addr_ptr); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 430 | } |
| 431 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 432 | int radeon_gem_busy_ioctl(struct drm_device *dev, void *data, |
| 433 | struct drm_file *filp) |
| 434 | { |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 435 | struct drm_radeon_gem_busy *args = data; |
| 436 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 437 | struct radeon_bo *robj; |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 438 | int r; |
Dave Airlie | 4361e52 | 2009-12-10 15:59:32 +1000 | [diff] [blame] | 439 | uint32_t cur_placement = 0; |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 440 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 441 | gobj = drm_gem_object_lookup(filp, args->handle); |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 442 | if (gobj == NULL) { |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 443 | return -ENOENT; |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 444 | } |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 445 | robj = gem_to_radeon_bo(gobj); |
Grigori Goronzy | 828202a | 2015-07-03 01:54:10 +0200 | [diff] [blame] | 446 | |
| 447 | r = reservation_object_test_signaled_rcu(robj->tbo.resv, true); |
| 448 | if (r == 0) |
| 449 | r = -EBUSY; |
| 450 | else |
| 451 | r = 0; |
| 452 | |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 453 | cur_placement = READ_ONCE(robj->tbo.mem.mem_type); |
Marek Olšák | 0bc490a | 2014-03-02 00:56:19 +0100 | [diff] [blame] | 454 | args->domain = radeon_mem_type_to_domain(cur_placement); |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 455 | drm_gem_object_put_unlocked(gobj); |
Dave Airlie | e3b2415 | 2009-08-21 09:47:45 +1000 | [diff] [blame] | 456 | return r; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data, |
| 460 | struct drm_file *filp) |
| 461 | { |
Jerome Glisse | 1ef5325 | 2012-07-02 12:40:54 -0400 | [diff] [blame] | 462 | struct radeon_device *rdev = dev->dev_private; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 463 | struct drm_radeon_gem_wait_idle *args = data; |
| 464 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 465 | struct radeon_bo *robj; |
Maarten Lankhorst | 39e7f6f | 2014-05-14 15:40:49 +0200 | [diff] [blame] | 466 | int r = 0; |
Michel Dänzer | 404a6a5 | 2014-08-01 17:22:09 +0900 | [diff] [blame] | 467 | uint32_t cur_placement = 0; |
Maarten Lankhorst | 39e7f6f | 2014-05-14 15:40:49 +0200 | [diff] [blame] | 468 | long ret; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 469 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 470 | gobj = drm_gem_object_lookup(filp, args->handle); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 471 | if (gobj == NULL) { |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 472 | return -ENOENT; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 473 | } |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 474 | robj = gem_to_radeon_bo(gobj); |
Maarten Lankhorst | 39e7f6f | 2014-05-14 15:40:49 +0200 | [diff] [blame] | 475 | |
| 476 | ret = reservation_object_wait_timeout_rcu(robj->tbo.resv, true, true, 30 * HZ); |
| 477 | if (ret == 0) |
| 478 | r = -EBUSY; |
| 479 | else if (ret < 0) |
| 480 | r = ret; |
| 481 | |
Michel Dänzer | 124764f | 2014-07-31 18:43:48 +0900 | [diff] [blame] | 482 | /* Flush HDP cache via MMIO if necessary */ |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 483 | cur_placement = READ_ONCE(robj->tbo.mem.mem_type); |
Michel Dänzer | 404a6a5 | 2014-08-01 17:22:09 +0900 | [diff] [blame] | 484 | if (rdev->asic->mmio_hdp_flush && |
| 485 | radeon_mem_type_to_domain(cur_placement) == RADEON_GEM_DOMAIN_VRAM) |
Michel Dänzer | 124764f | 2014-07-31 18:43:48 +0900 | [diff] [blame] | 486 | robj->rdev->asic->mmio_hdp_flush(rdev); |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 487 | drm_gem_object_put_unlocked(gobj); |
Jerome Glisse | 1ef5325 | 2012-07-02 12:40:54 -0400 | [diff] [blame] | 488 | r = radeon_gem_handle_lockup(rdev, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 489 | return r; |
| 490 | } |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 491 | |
| 492 | int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data, |
| 493 | struct drm_file *filp) |
| 494 | { |
| 495 | struct drm_radeon_gem_set_tiling *args = data; |
| 496 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 497 | struct radeon_bo *robj; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 498 | int r = 0; |
| 499 | |
| 500 | DRM_DEBUG("%d \n", args->handle); |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 501 | gobj = drm_gem_object_lookup(filp, args->handle); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 502 | if (gobj == NULL) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 503 | return -ENOENT; |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 504 | robj = gem_to_radeon_bo(gobj); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 505 | r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch); |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 506 | drm_gem_object_put_unlocked(gobj); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 507 | return r; |
| 508 | } |
| 509 | |
| 510 | int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data, |
| 511 | struct drm_file *filp) |
| 512 | { |
| 513 | struct drm_radeon_gem_get_tiling *args = data; |
| 514 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 515 | struct radeon_bo *rbo; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 516 | int r = 0; |
| 517 | |
| 518 | DRM_DEBUG("\n"); |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 519 | gobj = drm_gem_object_lookup(filp, args->handle); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 520 | if (gobj == NULL) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 521 | return -ENOENT; |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 522 | rbo = gem_to_radeon_bo(gobj); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 523 | r = radeon_bo_reserve(rbo, false); |
| 524 | if (unlikely(r != 0)) |
Dave Airlie | 51f07b7 | 2009-12-16 13:10:43 +1000 | [diff] [blame] | 525 | goto out; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 526 | radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch); |
| 527 | radeon_bo_unreserve(rbo); |
Dave Airlie | 51f07b7 | 2009-12-16 13:10:43 +1000 | [diff] [blame] | 528 | out: |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 529 | drm_gem_object_put_unlocked(gobj); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 530 | return r; |
| 531 | } |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 532 | |
Christian König | 2f2624c | 2014-09-12 12:25:45 +0200 | [diff] [blame] | 533 | /** |
| 534 | * radeon_gem_va_update_vm -update the bo_va in its VM |
| 535 | * |
| 536 | * @rdev: radeon_device pointer |
| 537 | * @bo_va: bo_va to update |
| 538 | * |
| 539 | * Update the bo_va directly after setting it's address. Errors are not |
| 540 | * vital here, so they are not reported back to userspace. |
| 541 | */ |
| 542 | static void radeon_gem_va_update_vm(struct radeon_device *rdev, |
| 543 | struct radeon_bo_va *bo_va) |
| 544 | { |
| 545 | struct ttm_validate_buffer tv, *entry; |
Christian König | 1d0c094 | 2014-11-27 14:48:42 +0100 | [diff] [blame] | 546 | struct radeon_bo_list *vm_bos; |
Christian König | 2f2624c | 2014-09-12 12:25:45 +0200 | [diff] [blame] | 547 | struct ww_acquire_ctx ticket; |
| 548 | struct list_head list; |
| 549 | unsigned domain; |
| 550 | int r; |
| 551 | |
| 552 | INIT_LIST_HEAD(&list); |
| 553 | |
| 554 | tv.bo = &bo_va->bo->tbo; |
Christian König | a9f34c70 | 2018-09-19 16:25:08 +0200 | [diff] [blame^] | 555 | tv.num_shared = 1; |
Christian König | 2f2624c | 2014-09-12 12:25:45 +0200 | [diff] [blame] | 556 | list_add(&tv.head, &list); |
| 557 | |
| 558 | vm_bos = radeon_vm_get_bos(rdev, bo_va->vm, &list); |
| 559 | if (!vm_bos) |
| 560 | return; |
| 561 | |
Christian König | aa35071 | 2014-12-03 15:46:48 +0100 | [diff] [blame] | 562 | r = ttm_eu_reserve_buffers(&ticket, &list, true, NULL); |
Christian König | 2f2624c | 2014-09-12 12:25:45 +0200 | [diff] [blame] | 563 | if (r) |
| 564 | goto error_free; |
| 565 | |
| 566 | list_for_each_entry(entry, &list, head) { |
| 567 | domain = radeon_mem_type_to_domain(entry->bo->mem.mem_type); |
| 568 | /* if anything is swapped out don't swap it in here, |
| 569 | just abort and wait for the next CS */ |
| 570 | if (domain == RADEON_GEM_DOMAIN_CPU) |
| 571 | goto error_unreserve; |
| 572 | } |
| 573 | |
| 574 | mutex_lock(&bo_va->vm->mutex); |
| 575 | r = radeon_vm_clear_freed(rdev, bo_va->vm); |
| 576 | if (r) |
| 577 | goto error_unlock; |
| 578 | |
| 579 | if (bo_va->it.start) |
| 580 | r = radeon_vm_bo_update(rdev, bo_va, &bo_va->bo->tbo.mem); |
| 581 | |
| 582 | error_unlock: |
| 583 | mutex_unlock(&bo_va->vm->mutex); |
| 584 | |
| 585 | error_unreserve: |
| 586 | ttm_eu_backoff_reservation(&ticket, &list); |
| 587 | |
| 588 | error_free: |
Michal Hocko | 2098105 | 2017-05-17 14:23:12 +0200 | [diff] [blame] | 589 | kvfree(vm_bos); |
Christian König | 2f2624c | 2014-09-12 12:25:45 +0200 | [diff] [blame] | 590 | |
Christian König | ad1a622 | 2015-01-09 11:07:49 +0100 | [diff] [blame] | 591 | if (r && r != -ERESTARTSYS) |
Christian König | 2f2624c | 2014-09-12 12:25:45 +0200 | [diff] [blame] | 592 | DRM_ERROR("Couldn't update BO_VA (%d)\n", r); |
| 593 | } |
| 594 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 595 | int radeon_gem_va_ioctl(struct drm_device *dev, void *data, |
| 596 | struct drm_file *filp) |
| 597 | { |
| 598 | struct drm_radeon_gem_va *args = data; |
| 599 | struct drm_gem_object *gobj; |
| 600 | struct radeon_device *rdev = dev->dev_private; |
| 601 | struct radeon_fpriv *fpriv = filp->driver_priv; |
| 602 | struct radeon_bo *rbo; |
| 603 | struct radeon_bo_va *bo_va; |
| 604 | u32 invalid_flags; |
| 605 | int r = 0; |
| 606 | |
Alex Deucher | 67e915e | 2012-01-06 09:38:15 -0500 | [diff] [blame] | 607 | if (!rdev->vm_manager.enabled) { |
| 608 | args->operation = RADEON_VA_RESULT_ERROR; |
| 609 | return -ENOTTY; |
| 610 | } |
| 611 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 612 | /* !! DONT REMOVE !! |
| 613 | * We don't support vm_id yet, to be sure we don't have have broken |
| 614 | * userspace, reject anyone trying to use non 0 value thus moving |
| 615 | * forward we can use those fields without breaking existant userspace |
| 616 | */ |
| 617 | if (args->vm_id) { |
| 618 | args->operation = RADEON_VA_RESULT_ERROR; |
| 619 | return -EINVAL; |
| 620 | } |
| 621 | |
| 622 | if (args->offset < RADEON_VA_RESERVED_SIZE) { |
| 623 | dev_err(&dev->pdev->dev, |
| 624 | "offset 0x%lX is in reserved area 0x%X\n", |
| 625 | (unsigned long)args->offset, |
| 626 | RADEON_VA_RESERVED_SIZE); |
| 627 | args->operation = RADEON_VA_RESULT_ERROR; |
| 628 | return -EINVAL; |
| 629 | } |
| 630 | |
| 631 | /* don't remove, we need to enforce userspace to set the snooped flag |
| 632 | * otherwise we will endup with broken userspace and we won't be able |
| 633 | * to enable this feature without adding new interface |
| 634 | */ |
| 635 | invalid_flags = RADEON_VM_PAGE_VALID | RADEON_VM_PAGE_SYSTEM; |
| 636 | if ((args->flags & invalid_flags)) { |
| 637 | dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n", |
| 638 | args->flags, invalid_flags); |
| 639 | args->operation = RADEON_VA_RESULT_ERROR; |
| 640 | return -EINVAL; |
| 641 | } |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 642 | |
| 643 | switch (args->operation) { |
| 644 | case RADEON_VA_MAP: |
| 645 | case RADEON_VA_UNMAP: |
| 646 | break; |
| 647 | default: |
| 648 | dev_err(&dev->pdev->dev, "unsupported operation %d\n", |
| 649 | args->operation); |
| 650 | args->operation = RADEON_VA_RESULT_ERROR; |
| 651 | return -EINVAL; |
| 652 | } |
| 653 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 654 | gobj = drm_gem_object_lookup(filp, args->handle); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 655 | if (gobj == NULL) { |
| 656 | args->operation = RADEON_VA_RESULT_ERROR; |
| 657 | return -ENOENT; |
| 658 | } |
| 659 | rbo = gem_to_radeon_bo(gobj); |
| 660 | r = radeon_bo_reserve(rbo, false); |
| 661 | if (r) { |
| 662 | args->operation = RADEON_VA_RESULT_ERROR; |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 663 | drm_gem_object_put_unlocked(gobj); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 664 | return r; |
| 665 | } |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame] | 666 | bo_va = radeon_vm_bo_find(&fpriv->vm, rbo); |
| 667 | if (!bo_va) { |
| 668 | args->operation = RADEON_VA_RESULT_ERROR; |
Matthew Dawson | 186bac8 | 2016-01-25 10:34:12 -0500 | [diff] [blame] | 669 | radeon_bo_unreserve(rbo); |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 670 | drm_gem_object_put_unlocked(gobj); |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame] | 671 | return -ENOENT; |
| 672 | } |
| 673 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 674 | switch (args->operation) { |
| 675 | case RADEON_VA_MAP: |
Alex Deucher | 0aea5e4 | 2014-07-30 11:49:56 -0400 | [diff] [blame] | 676 | if (bo_va->it.start) { |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 677 | args->operation = RADEON_VA_RESULT_VA_EXIST; |
Alex Deucher | 0aea5e4 | 2014-07-30 11:49:56 -0400 | [diff] [blame] | 678 | args->offset = bo_va->it.start * RADEON_GPU_PAGE_SIZE; |
Christian König | 85761f6 | 2014-11-19 14:01:20 +0100 | [diff] [blame] | 679 | radeon_bo_unreserve(rbo); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 680 | goto out; |
| 681 | } |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame] | 682 | r = radeon_vm_bo_set_addr(rdev, bo_va, args->offset, args->flags); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 683 | break; |
| 684 | case RADEON_VA_UNMAP: |
Christian König | e971bd5 | 2012-09-11 16:10:04 +0200 | [diff] [blame] | 685 | r = radeon_vm_bo_set_addr(rdev, bo_va, 0, 0); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 686 | break; |
| 687 | default: |
| 688 | break; |
| 689 | } |
Christian König | 2f2624c | 2014-09-12 12:25:45 +0200 | [diff] [blame] | 690 | if (!r) |
| 691 | radeon_gem_va_update_vm(rdev, bo_va); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 692 | args->operation = RADEON_VA_RESULT_OK; |
| 693 | if (r) { |
| 694 | args->operation = RADEON_VA_RESULT_ERROR; |
| 695 | } |
| 696 | out: |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 697 | drm_gem_object_put_unlocked(gobj); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 698 | return r; |
| 699 | } |
| 700 | |
Marek Olšák | bda72d5 | 2014-03-02 00:56:17 +0100 | [diff] [blame] | 701 | int radeon_gem_op_ioctl(struct drm_device *dev, void *data, |
| 702 | struct drm_file *filp) |
| 703 | { |
| 704 | struct drm_radeon_gem_op *args = data; |
| 705 | struct drm_gem_object *gobj; |
| 706 | struct radeon_bo *robj; |
| 707 | int r; |
| 708 | |
Chris Wilson | a8ad0bd | 2016-05-09 11:04:54 +0100 | [diff] [blame] | 709 | gobj = drm_gem_object_lookup(filp, args->handle); |
Marek Olšák | bda72d5 | 2014-03-02 00:56:17 +0100 | [diff] [blame] | 710 | if (gobj == NULL) { |
| 711 | return -ENOENT; |
| 712 | } |
| 713 | robj = gem_to_radeon_bo(gobj); |
Christian König | f72a113a | 2014-08-07 09:36:00 +0200 | [diff] [blame] | 714 | |
| 715 | r = -EPERM; |
| 716 | if (radeon_ttm_tt_has_userptr(robj->tbo.ttm)) |
| 717 | goto out; |
| 718 | |
Marek Olšák | bda72d5 | 2014-03-02 00:56:17 +0100 | [diff] [blame] | 719 | r = radeon_bo_reserve(robj, false); |
| 720 | if (unlikely(r)) |
| 721 | goto out; |
| 722 | |
| 723 | switch (args->op) { |
| 724 | case RADEON_GEM_OP_GET_INITIAL_DOMAIN: |
| 725 | args->value = robj->initial_domain; |
| 726 | break; |
| 727 | case RADEON_GEM_OP_SET_INITIAL_DOMAIN: |
| 728 | robj->initial_domain = args->value & (RADEON_GEM_DOMAIN_VRAM | |
| 729 | RADEON_GEM_DOMAIN_GTT | |
| 730 | RADEON_GEM_DOMAIN_CPU); |
| 731 | break; |
| 732 | default: |
| 733 | r = -EINVAL; |
| 734 | } |
| 735 | |
| 736 | radeon_bo_unreserve(robj); |
| 737 | out: |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 738 | drm_gem_object_put_unlocked(gobj); |
Marek Olšák | bda72d5 | 2014-03-02 00:56:17 +0100 | [diff] [blame] | 739 | return r; |
| 740 | } |
| 741 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 742 | int radeon_mode_dumb_create(struct drm_file *file_priv, |
| 743 | struct drm_device *dev, |
| 744 | struct drm_mode_create_dumb *args) |
| 745 | { |
| 746 | struct radeon_device *rdev = dev->dev_private; |
| 747 | struct drm_gem_object *gobj; |
Dave Airlie | c87a8d8 | 2011-03-17 13:58:34 +1000 | [diff] [blame] | 748 | uint32_t handle; |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 749 | int r; |
| 750 | |
Laurent Pinchart | 802aaf7 | 2016-10-18 01:41:18 +0300 | [diff] [blame] | 751 | args->pitch = radeon_align_pitch(rdev, args->width, |
| 752 | DIV_ROUND_UP(args->bpp, 8), 0); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 753 | args->size = args->pitch * args->height; |
| 754 | args->size = ALIGN(args->size, PAGE_SIZE); |
| 755 | |
| 756 | r = radeon_gem_object_create(rdev, args->size, 0, |
Michel Dänzer | 02376d8 | 2014-07-17 19:01:08 +0900 | [diff] [blame] | 757 | RADEON_GEM_DOMAIN_VRAM, 0, |
Christian König | ed5cb43 | 2014-07-21 13:27:27 +0200 | [diff] [blame] | 758 | false, &gobj); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 759 | if (r) |
| 760 | return -ENOMEM; |
| 761 | |
Dave Airlie | c87a8d8 | 2011-03-17 13:58:34 +1000 | [diff] [blame] | 762 | r = drm_gem_handle_create(file_priv, gobj, &handle); |
| 763 | /* drop reference from allocate - handle holds it now */ |
Cihangir Akturk | 07f65bb | 2017-08-03 14:58:35 +0300 | [diff] [blame] | 764 | drm_gem_object_put_unlocked(gobj); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 765 | if (r) { |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 766 | return r; |
| 767 | } |
Dave Airlie | c87a8d8 | 2011-03-17 13:58:34 +1000 | [diff] [blame] | 768 | args->handle = handle; |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 769 | return 0; |
| 770 | } |
| 771 | |
Jerome Glisse | 409851f | 2013-04-25 22:29:27 -0400 | [diff] [blame] | 772 | #if defined(CONFIG_DEBUG_FS) |
| 773 | static int radeon_debugfs_gem_info(struct seq_file *m, void *data) |
| 774 | { |
| 775 | struct drm_info_node *node = (struct drm_info_node *)m->private; |
| 776 | struct drm_device *dev = node->minor->dev; |
| 777 | struct radeon_device *rdev = dev->dev_private; |
| 778 | struct radeon_bo *rbo; |
| 779 | unsigned i = 0; |
| 780 | |
| 781 | mutex_lock(&rdev->gem.mutex); |
| 782 | list_for_each_entry(rbo, &rdev->gem.objects, list) { |
| 783 | unsigned domain; |
| 784 | const char *placement; |
| 785 | |
| 786 | domain = radeon_mem_type_to_domain(rbo->tbo.mem.mem_type); |
| 787 | switch (domain) { |
| 788 | case RADEON_GEM_DOMAIN_VRAM: |
| 789 | placement = "VRAM"; |
| 790 | break; |
| 791 | case RADEON_GEM_DOMAIN_GTT: |
| 792 | placement = " GTT"; |
| 793 | break; |
| 794 | case RADEON_GEM_DOMAIN_CPU: |
| 795 | default: |
| 796 | placement = " CPU"; |
| 797 | break; |
| 798 | } |
| 799 | seq_printf(m, "bo[0x%08x] %8ldkB %8ldMB %s pid %8ld\n", |
| 800 | i, radeon_bo_size(rbo) >> 10, radeon_bo_size(rbo) >> 20, |
| 801 | placement, (unsigned long)rbo->pid); |
| 802 | i++; |
| 803 | } |
| 804 | mutex_unlock(&rdev->gem.mutex); |
| 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | static struct drm_info_list radeon_debugfs_gem_list[] = { |
| 809 | {"radeon_gem_info", &radeon_debugfs_gem_info, 0, NULL}, |
| 810 | }; |
| 811 | #endif |
| 812 | |
| 813 | int radeon_gem_debugfs_init(struct radeon_device *rdev) |
| 814 | { |
| 815 | #if defined(CONFIG_DEBUG_FS) |
| 816 | return radeon_debugfs_add_files(rdev, radeon_debugfs_gem_list, 1); |
| 817 | #endif |
| 818 | return 0; |
| 819 | } |