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 | */ |
| 28 | #include "drmP.h" |
| 29 | #include "drm.h" |
| 30 | #include "radeon_drm.h" |
| 31 | #include "radeon.h" |
| 32 | |
| 33 | int radeon_gem_object_init(struct drm_gem_object *obj) |
| 34 | { |
Daniel Vetter | 441921d | 2011-02-18 17:59:16 +0100 | [diff] [blame] | 35 | BUG(); |
| 36 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 37 | return 0; |
| 38 | } |
| 39 | |
| 40 | void radeon_gem_object_free(struct drm_gem_object *gobj) |
| 41 | { |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 42 | struct radeon_bo *robj = gem_to_radeon_bo(gobj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 43 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 44 | if (robj) { |
Alex Deucher | 40f5cf9 | 2012-05-10 18:33:13 -0400 | [diff] [blame] | 45 | if (robj->gem_base.import_attach) |
| 46 | drm_prime_gem_destroy(&robj->gem_base, robj->tbo.sg); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 47 | radeon_bo_unref(&robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
| 51 | int radeon_gem_object_create(struct radeon_device *rdev, int size, |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 52 | int alignment, int initial_domain, |
| 53 | bool discardable, bool kernel, |
| 54 | struct drm_gem_object **obj) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 55 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 56 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 57 | int r; |
| 58 | |
| 59 | *obj = NULL; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 60 | /* At least align on page size */ |
| 61 | if (alignment < PAGE_SIZE) { |
| 62 | alignment = PAGE_SIZE; |
| 63 | } |
Alex Deucher | 40f5cf9 | 2012-05-10 18:33:13 -0400 | [diff] [blame] | 64 | r = radeon_bo_create(rdev, size, alignment, kernel, initial_domain, NULL, &robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 65 | if (r) { |
Dave Airlie | ecabd32 | 2009-12-15 10:39:48 +1000 | [diff] [blame] | 66 | if (r != -ERESTARTSYS) |
| 67 | DRM_ERROR("Failed to allocate GEM object (%d, %d, %u, %d)\n", |
| 68 | size, initial_domain, alignment, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 69 | return r; |
| 70 | } |
Daniel Vetter | 441921d | 2011-02-18 17:59:16 +0100 | [diff] [blame] | 71 | *obj = &robj->gem_base; |
| 72 | |
| 73 | mutex_lock(&rdev->gem.mutex); |
| 74 | list_add_tail(&robj->list, &rdev->gem.objects); |
| 75 | mutex_unlock(&rdev->gem.mutex); |
| 76 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 77 | return 0; |
| 78 | } |
| 79 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 80 | int radeon_gem_set_domain(struct drm_gem_object *gobj, |
| 81 | uint32_t rdomain, uint32_t wdomain) |
| 82 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 83 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 84 | uint32_t domain; |
| 85 | int r; |
| 86 | |
| 87 | /* FIXME: reeimplement */ |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 88 | robj = gem_to_radeon_bo(gobj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 89 | /* work out where to validate the buffer to */ |
| 90 | domain = wdomain; |
| 91 | if (!domain) { |
| 92 | domain = rdomain; |
| 93 | } |
| 94 | if (!domain) { |
| 95 | /* Do nothings */ |
Masanari Iida | b6cafa2 | 2012-02-27 23:28:38 +0900 | [diff] [blame] | 96 | printk(KERN_WARNING "Set domain without domain !\n"); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | if (domain == RADEON_GEM_DOMAIN_CPU) { |
| 100 | /* Asking for cpu access wait for object idle */ |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 101 | r = radeon_bo_wait(robj, NULL, false); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 102 | if (r) { |
| 103 | printk(KERN_ERR "Failed to wait for object !\n"); |
| 104 | return r; |
| 105 | } |
| 106 | } |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | int radeon_gem_init(struct radeon_device *rdev) |
| 111 | { |
| 112 | INIT_LIST_HEAD(&rdev->gem.objects); |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | void radeon_gem_fini(struct radeon_device *rdev) |
| 117 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 118 | radeon_bo_force_delete(rdev); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 121 | /* |
| 122 | * Call from drm_gem_handle_create which appear in both new and open ioctl |
| 123 | * case. |
| 124 | */ |
| 125 | int radeon_gem_object_open(struct drm_gem_object *obj, struct drm_file *file_priv) |
| 126 | { |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | void radeon_gem_object_close(struct drm_gem_object *obj, |
| 131 | struct drm_file *file_priv) |
| 132 | { |
| 133 | struct radeon_bo *rbo = gem_to_radeon_bo(obj); |
| 134 | struct radeon_device *rdev = rbo->rdev; |
| 135 | struct radeon_fpriv *fpriv = file_priv->driver_priv; |
| 136 | struct radeon_vm *vm = &fpriv->vm; |
Christian König | d59f702 | 2012-09-11 16:10:02 +0200 | [diff] [blame^] | 137 | int r; |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 138 | |
| 139 | if (rdev->family < CHIP_CAYMAN) { |
| 140 | return; |
| 141 | } |
| 142 | |
Christian König | d59f702 | 2012-09-11 16:10:02 +0200 | [diff] [blame^] | 143 | r = radeon_bo_reserve(rbo, true); |
| 144 | if (r) { |
| 145 | dev_err(rdev->dev, "leaking bo va because " |
| 146 | "we fail to reserve bo (%d)\n", r); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 147 | return; |
| 148 | } |
Jerome Glisse | e43b5ec | 2012-08-06 12:32:21 -0400 | [diff] [blame] | 149 | radeon_vm_bo_rmv(rdev, vm, rbo); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 150 | radeon_bo_unreserve(rbo); |
| 151 | } |
| 152 | |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 153 | static int radeon_gem_handle_lockup(struct radeon_device *rdev, int r) |
| 154 | { |
| 155 | if (r == -EDEADLK) { |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 156 | r = radeon_gpu_reset(rdev); |
| 157 | if (!r) |
| 158 | r = -EAGAIN; |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 159 | } |
| 160 | return r; |
| 161 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 162 | |
| 163 | /* |
| 164 | * GEM ioctls. |
| 165 | */ |
| 166 | int radeon_gem_info_ioctl(struct drm_device *dev, void *data, |
| 167 | struct drm_file *filp) |
| 168 | { |
| 169 | struct radeon_device *rdev = dev->dev_private; |
| 170 | struct drm_radeon_gem_info *args = data; |
Dave Airlie | 5359533 | 2011-03-14 09:47:24 +1000 | [diff] [blame] | 171 | struct ttm_mem_type_manager *man; |
Christian König | bf85279 | 2011-10-13 13:19:22 +0200 | [diff] [blame] | 172 | unsigned i; |
Dave Airlie | 5359533 | 2011-03-14 09:47:24 +1000 | [diff] [blame] | 173 | |
| 174 | man = &rdev->mman.bdev.man[TTM_PL_VRAM]; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 175 | |
Dave Airlie | 7a50f01 | 2009-07-21 20:39:30 +1000 | [diff] [blame] | 176 | args->vram_size = rdev->mc.real_vram_size; |
Dave Airlie | 5359533 | 2011-03-14 09:47:24 +1000 | [diff] [blame] | 177 | args->vram_visible = (u64)man->size << PAGE_SHIFT; |
Michel Dänzer | 38e1492 | 2009-08-05 00:19:51 +0200 | [diff] [blame] | 178 | if (rdev->stollen_vga_memory) |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 179 | args->vram_visible -= radeon_bo_size(rdev->stollen_vga_memory); |
Dave Airlie | 3865167 | 2010-03-30 05:34:13 +0000 | [diff] [blame] | 180 | args->vram_visible -= radeon_fbdev_total_size(rdev); |
Christian König | 7b1f248 | 2011-09-23 15:11:23 +0200 | [diff] [blame] | 181 | args->gart_size = rdev->mc.gtt_size - 4096 - RADEON_IB_POOL_SIZE*64*1024; |
Christian König | bf85279 | 2011-10-13 13:19:22 +0200 | [diff] [blame] | 182 | for(i = 0; i < RADEON_NUM_RINGS; ++i) |
Christian König | e32eb50 | 2011-10-23 12:56:27 +0200 | [diff] [blame] | 183 | args->gart_size -= rdev->ring[i].ring_size; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | int radeon_gem_pread_ioctl(struct drm_device *dev, void *data, |
| 188 | struct drm_file *filp) |
| 189 | { |
| 190 | /* TODO: implement */ |
| 191 | DRM_ERROR("unimplemented %s\n", __func__); |
| 192 | return -ENOSYS; |
| 193 | } |
| 194 | |
| 195 | int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
| 196 | struct drm_file *filp) |
| 197 | { |
| 198 | /* TODO: implement */ |
| 199 | DRM_ERROR("unimplemented %s\n", __func__); |
| 200 | return -ENOSYS; |
| 201 | } |
| 202 | |
| 203 | int radeon_gem_create_ioctl(struct drm_device *dev, void *data, |
| 204 | struct drm_file *filp) |
| 205 | { |
| 206 | struct radeon_device *rdev = dev->dev_private; |
| 207 | struct drm_radeon_gem_create *args = data; |
| 208 | struct drm_gem_object *gobj; |
| 209 | uint32_t handle; |
| 210 | int r; |
| 211 | |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 212 | down_read(&rdev->exclusive_lock); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 213 | /* create a gem object to contain this object in */ |
| 214 | args->size = roundup(args->size, PAGE_SIZE); |
| 215 | r = radeon_gem_object_create(rdev, args->size, args->alignment, |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 216 | args->initial_domain, false, |
| 217 | false, &gobj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 218 | if (r) { |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 219 | up_read(&rdev->exclusive_lock); |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 220 | r = radeon_gem_handle_lockup(rdev, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 221 | return r; |
| 222 | } |
| 223 | r = drm_gem_handle_create(filp, gobj, &handle); |
Dave Airlie | 29d08b3 | 2010-09-27 16:17:17 +1000 | [diff] [blame] | 224 | /* drop reference from allocate - handle holds it now */ |
| 225 | drm_gem_object_unreference_unlocked(gobj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 226 | if (r) { |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 227 | up_read(&rdev->exclusive_lock); |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 228 | r = radeon_gem_handle_lockup(rdev, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 229 | return r; |
| 230 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 231 | args->handle = handle; |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 232 | up_read(&rdev->exclusive_lock); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
| 237 | struct drm_file *filp) |
| 238 | { |
| 239 | /* transition the BO to a domain - |
| 240 | * just validate the BO into a certain domain */ |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 241 | struct radeon_device *rdev = dev->dev_private; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 242 | struct drm_radeon_gem_set_domain *args = data; |
| 243 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 244 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 245 | int r; |
| 246 | |
| 247 | /* for now if someone requests domain CPU - |
| 248 | * just make sure the buffer is finished with */ |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 249 | down_read(&rdev->exclusive_lock); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 250 | |
| 251 | /* just do a BO wait for now */ |
| 252 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 253 | if (gobj == NULL) { |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 254 | up_read(&rdev->exclusive_lock); |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 255 | return -ENOENT; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 256 | } |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 257 | robj = gem_to_radeon_bo(gobj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 258 | |
| 259 | r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain); |
| 260 | |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 261 | drm_gem_object_unreference_unlocked(gobj); |
Jerome Glisse | dee53e7 | 2012-07-02 12:45:19 -0400 | [diff] [blame] | 262 | up_read(&rdev->exclusive_lock); |
Christian König | 6c6f478 | 2012-05-02 15:11:19 +0200 | [diff] [blame] | 263 | r = radeon_gem_handle_lockup(robj->rdev, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 264 | return r; |
| 265 | } |
| 266 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 267 | int radeon_mode_dumb_mmap(struct drm_file *filp, |
| 268 | struct drm_device *dev, |
| 269 | uint32_t handle, uint64_t *offset_p) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 270 | { |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 271 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 272 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 273 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 274 | gobj = drm_gem_object_lookup(dev, filp, handle); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 275 | if (gobj == NULL) { |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 276 | return -ENOENT; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 277 | } |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 278 | robj = gem_to_radeon_bo(gobj); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 279 | *offset_p = radeon_bo_mmap_offset(robj); |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 280 | drm_gem_object_unreference_unlocked(gobj); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 281 | return 0; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 282 | } |
| 283 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 284 | int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data, |
| 285 | struct drm_file *filp) |
| 286 | { |
| 287 | struct drm_radeon_gem_mmap *args = data; |
| 288 | |
| 289 | return radeon_mode_dumb_mmap(filp, dev, args->handle, &args->addr_ptr); |
| 290 | } |
| 291 | |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 292 | int radeon_gem_busy_ioctl(struct drm_device *dev, void *data, |
| 293 | struct drm_file *filp) |
| 294 | { |
Jerome Glisse | 1ef5325 | 2012-07-02 12:40:54 -0400 | [diff] [blame] | 295 | struct radeon_device *rdev = dev->dev_private; |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 296 | struct drm_radeon_gem_busy *args = data; |
| 297 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 298 | struct radeon_bo *robj; |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 299 | int r; |
Dave Airlie | 4361e52 | 2009-12-10 15:59:32 +1000 | [diff] [blame] | 300 | uint32_t cur_placement = 0; |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 301 | |
| 302 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 303 | if (gobj == NULL) { |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 304 | return -ENOENT; |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 305 | } |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 306 | robj = gem_to_radeon_bo(gobj); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 307 | r = radeon_bo_wait(robj, &cur_placement, true); |
Michel Dänzer | 9f844e5 | 2009-08-22 17:38:23 +0200 | [diff] [blame] | 308 | switch (cur_placement) { |
| 309 | case TTM_PL_VRAM: |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 310 | args->domain = RADEON_GEM_DOMAIN_VRAM; |
Michel Dänzer | 9f844e5 | 2009-08-22 17:38:23 +0200 | [diff] [blame] | 311 | break; |
| 312 | case TTM_PL_TT: |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 313 | args->domain = RADEON_GEM_DOMAIN_GTT; |
Michel Dänzer | 9f844e5 | 2009-08-22 17:38:23 +0200 | [diff] [blame] | 314 | break; |
| 315 | case TTM_PL_SYSTEM: |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 316 | args->domain = RADEON_GEM_DOMAIN_CPU; |
Michel Dänzer | 9f844e5 | 2009-08-22 17:38:23 +0200 | [diff] [blame] | 317 | default: |
| 318 | break; |
| 319 | } |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 320 | drm_gem_object_unreference_unlocked(gobj); |
Jerome Glisse | 1ef5325 | 2012-07-02 12:40:54 -0400 | [diff] [blame] | 321 | r = radeon_gem_handle_lockup(rdev, r); |
Dave Airlie | e3b2415 | 2009-08-21 09:47:45 +1000 | [diff] [blame] | 322 | return r; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data, |
| 326 | struct drm_file *filp) |
| 327 | { |
Jerome Glisse | 1ef5325 | 2012-07-02 12:40:54 -0400 | [diff] [blame] | 328 | struct radeon_device *rdev = dev->dev_private; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 329 | struct drm_radeon_gem_wait_idle *args = data; |
| 330 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 331 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 332 | int r; |
| 333 | |
| 334 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 335 | if (gobj == NULL) { |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 336 | return -ENOENT; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 337 | } |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 338 | robj = gem_to_radeon_bo(gobj); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 339 | r = radeon_bo_wait(robj, NULL, false); |
Jerome Glisse | 062b389 | 2010-02-04 20:36:39 +0100 | [diff] [blame] | 340 | /* callback hw specific functions if any */ |
Jerome Glisse | 1ef5325 | 2012-07-02 12:40:54 -0400 | [diff] [blame] | 341 | if (rdev->asic->ioctl_wait_idle) |
| 342 | robj->rdev->asic->ioctl_wait_idle(rdev, robj); |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 343 | drm_gem_object_unreference_unlocked(gobj); |
Jerome Glisse | 1ef5325 | 2012-07-02 12:40:54 -0400 | [diff] [blame] | 344 | r = radeon_gem_handle_lockup(rdev, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 345 | return r; |
| 346 | } |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 347 | |
| 348 | int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data, |
| 349 | struct drm_file *filp) |
| 350 | { |
| 351 | struct drm_radeon_gem_set_tiling *args = data; |
| 352 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 353 | struct radeon_bo *robj; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 354 | int r = 0; |
| 355 | |
| 356 | DRM_DEBUG("%d \n", args->handle); |
| 357 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 358 | if (gobj == NULL) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 359 | return -ENOENT; |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 360 | robj = gem_to_radeon_bo(gobj); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 361 | r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch); |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 362 | drm_gem_object_unreference_unlocked(gobj); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 363 | return r; |
| 364 | } |
| 365 | |
| 366 | int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data, |
| 367 | struct drm_file *filp) |
| 368 | { |
| 369 | struct drm_radeon_gem_get_tiling *args = data; |
| 370 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 371 | struct radeon_bo *rbo; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 372 | int r = 0; |
| 373 | |
| 374 | DRM_DEBUG("\n"); |
| 375 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 376 | if (gobj == NULL) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 377 | return -ENOENT; |
Daniel Vetter | 7e4d15d | 2011-02-18 17:59:17 +0100 | [diff] [blame] | 378 | rbo = gem_to_radeon_bo(gobj); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 379 | r = radeon_bo_reserve(rbo, false); |
| 380 | if (unlikely(r != 0)) |
Dave Airlie | 51f07b7 | 2009-12-16 13:10:43 +1000 | [diff] [blame] | 381 | goto out; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 382 | radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch); |
| 383 | radeon_bo_unreserve(rbo); |
Dave Airlie | 51f07b7 | 2009-12-16 13:10:43 +1000 | [diff] [blame] | 384 | out: |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 385 | drm_gem_object_unreference_unlocked(gobj); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 386 | return r; |
| 387 | } |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 388 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 389 | int radeon_gem_va_ioctl(struct drm_device *dev, void *data, |
| 390 | struct drm_file *filp) |
| 391 | { |
| 392 | struct drm_radeon_gem_va *args = data; |
| 393 | struct drm_gem_object *gobj; |
| 394 | struct radeon_device *rdev = dev->dev_private; |
| 395 | struct radeon_fpriv *fpriv = filp->driver_priv; |
| 396 | struct radeon_bo *rbo; |
| 397 | struct radeon_bo_va *bo_va; |
| 398 | u32 invalid_flags; |
| 399 | int r = 0; |
| 400 | |
Alex Deucher | 67e915e | 2012-01-06 09:38:15 -0500 | [diff] [blame] | 401 | if (!rdev->vm_manager.enabled) { |
| 402 | args->operation = RADEON_VA_RESULT_ERROR; |
| 403 | return -ENOTTY; |
| 404 | } |
| 405 | |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 406 | /* !! DONT REMOVE !! |
| 407 | * We don't support vm_id yet, to be sure we don't have have broken |
| 408 | * userspace, reject anyone trying to use non 0 value thus moving |
| 409 | * forward we can use those fields without breaking existant userspace |
| 410 | */ |
| 411 | if (args->vm_id) { |
| 412 | args->operation = RADEON_VA_RESULT_ERROR; |
| 413 | return -EINVAL; |
| 414 | } |
| 415 | |
| 416 | if (args->offset < RADEON_VA_RESERVED_SIZE) { |
| 417 | dev_err(&dev->pdev->dev, |
| 418 | "offset 0x%lX is in reserved area 0x%X\n", |
| 419 | (unsigned long)args->offset, |
| 420 | RADEON_VA_RESERVED_SIZE); |
| 421 | args->operation = RADEON_VA_RESULT_ERROR; |
| 422 | return -EINVAL; |
| 423 | } |
| 424 | |
| 425 | /* don't remove, we need to enforce userspace to set the snooped flag |
| 426 | * otherwise we will endup with broken userspace and we won't be able |
| 427 | * to enable this feature without adding new interface |
| 428 | */ |
| 429 | invalid_flags = RADEON_VM_PAGE_VALID | RADEON_VM_PAGE_SYSTEM; |
| 430 | if ((args->flags & invalid_flags)) { |
| 431 | dev_err(&dev->pdev->dev, "invalid flags 0x%08X vs 0x%08X\n", |
| 432 | args->flags, invalid_flags); |
| 433 | args->operation = RADEON_VA_RESULT_ERROR; |
| 434 | return -EINVAL; |
| 435 | } |
| 436 | if (!(args->flags & RADEON_VM_PAGE_SNOOPED)) { |
| 437 | dev_err(&dev->pdev->dev, "only supported snooped mapping for now\n"); |
| 438 | args->operation = RADEON_VA_RESULT_ERROR; |
| 439 | return -EINVAL; |
| 440 | } |
| 441 | |
| 442 | switch (args->operation) { |
| 443 | case RADEON_VA_MAP: |
| 444 | case RADEON_VA_UNMAP: |
| 445 | break; |
| 446 | default: |
| 447 | dev_err(&dev->pdev->dev, "unsupported operation %d\n", |
| 448 | args->operation); |
| 449 | args->operation = RADEON_VA_RESULT_ERROR; |
| 450 | return -EINVAL; |
| 451 | } |
| 452 | |
| 453 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 454 | if (gobj == NULL) { |
| 455 | args->operation = RADEON_VA_RESULT_ERROR; |
| 456 | return -ENOENT; |
| 457 | } |
| 458 | rbo = gem_to_radeon_bo(gobj); |
| 459 | r = radeon_bo_reserve(rbo, false); |
| 460 | if (r) { |
| 461 | args->operation = RADEON_VA_RESULT_ERROR; |
| 462 | drm_gem_object_unreference_unlocked(gobj); |
| 463 | return r; |
| 464 | } |
| 465 | switch (args->operation) { |
| 466 | case RADEON_VA_MAP: |
Christian König | 421ca7a | 2012-09-11 16:10:00 +0200 | [diff] [blame] | 467 | bo_va = radeon_vm_bo_find(&fpriv->vm, rbo); |
Jerome Glisse | 721604a | 2012-01-05 22:11:05 -0500 | [diff] [blame] | 468 | if (bo_va) { |
| 469 | args->operation = RADEON_VA_RESULT_VA_EXIST; |
| 470 | args->offset = bo_va->soffset; |
| 471 | goto out; |
| 472 | } |
| 473 | r = radeon_vm_bo_add(rdev, &fpriv->vm, rbo, |
| 474 | args->offset, args->flags); |
| 475 | break; |
| 476 | case RADEON_VA_UNMAP: |
| 477 | r = radeon_vm_bo_rmv(rdev, &fpriv->vm, rbo); |
| 478 | break; |
| 479 | default: |
| 480 | break; |
| 481 | } |
| 482 | args->operation = RADEON_VA_RESULT_OK; |
| 483 | if (r) { |
| 484 | args->operation = RADEON_VA_RESULT_ERROR; |
| 485 | } |
| 486 | out: |
| 487 | radeon_bo_unreserve(rbo); |
| 488 | drm_gem_object_unreference_unlocked(gobj); |
| 489 | return r; |
| 490 | } |
| 491 | |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 492 | int radeon_mode_dumb_create(struct drm_file *file_priv, |
| 493 | struct drm_device *dev, |
| 494 | struct drm_mode_create_dumb *args) |
| 495 | { |
| 496 | struct radeon_device *rdev = dev->dev_private; |
| 497 | struct drm_gem_object *gobj; |
Dave Airlie | c87a8d8 | 2011-03-17 13:58:34 +1000 | [diff] [blame] | 498 | uint32_t handle; |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 499 | int r; |
| 500 | |
| 501 | args->pitch = radeon_align_pitch(rdev, args->width, args->bpp, 0) * ((args->bpp + 1) / 8); |
| 502 | args->size = args->pitch * args->height; |
| 503 | args->size = ALIGN(args->size, PAGE_SIZE); |
| 504 | |
| 505 | r = radeon_gem_object_create(rdev, args->size, 0, |
| 506 | RADEON_GEM_DOMAIN_VRAM, |
| 507 | false, ttm_bo_type_device, |
| 508 | &gobj); |
| 509 | if (r) |
| 510 | return -ENOMEM; |
| 511 | |
Dave Airlie | c87a8d8 | 2011-03-17 13:58:34 +1000 | [diff] [blame] | 512 | r = drm_gem_handle_create(file_priv, gobj, &handle); |
| 513 | /* drop reference from allocate - handle holds it now */ |
| 514 | drm_gem_object_unreference_unlocked(gobj); |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 515 | if (r) { |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 516 | return r; |
| 517 | } |
Dave Airlie | c87a8d8 | 2011-03-17 13:58:34 +1000 | [diff] [blame] | 518 | args->handle = handle; |
Dave Airlie | ff72145b | 2011-02-07 12:16:14 +1000 | [diff] [blame] | 519 | return 0; |
| 520 | } |
| 521 | |
| 522 | int radeon_mode_dumb_destroy(struct drm_file *file_priv, |
| 523 | struct drm_device *dev, |
| 524 | uint32_t handle) |
| 525 | { |
| 526 | return drm_gem_handle_delete(file_priv, handle); |
| 527 | } |