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 | { |
| 35 | /* we do nothings here */ |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | void radeon_gem_object_free(struct drm_gem_object *gobj) |
| 40 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 41 | struct radeon_bo *robj = gobj->driver_private; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 42 | |
| 43 | gobj->driver_private = NULL; |
| 44 | if (robj) { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 45 | radeon_bo_unref(&robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | int radeon_gem_object_create(struct radeon_device *rdev, int size, |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 50 | int alignment, int initial_domain, |
| 51 | bool discardable, bool kernel, |
| 52 | struct drm_gem_object **obj) |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 53 | { |
| 54 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 55 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 56 | int r; |
| 57 | |
| 58 | *obj = NULL; |
| 59 | gobj = drm_gem_object_alloc(rdev->ddev, size); |
| 60 | if (!gobj) { |
| 61 | return -ENOMEM; |
| 62 | } |
| 63 | /* At least align on page size */ |
| 64 | if (alignment < PAGE_SIZE) { |
| 65 | alignment = PAGE_SIZE; |
| 66 | } |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 67 | r = radeon_bo_create(rdev, gobj, size, kernel, initial_domain, &robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 68 | if (r) { |
Dave Airlie | ecabd32 | 2009-12-15 10:39:48 +1000 | [diff] [blame] | 69 | if (r != -ERESTARTSYS) |
| 70 | DRM_ERROR("Failed to allocate GEM object (%d, %d, %u, %d)\n", |
| 71 | size, initial_domain, alignment, r); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 72 | mutex_lock(&rdev->ddev->struct_mutex); |
| 73 | drm_gem_object_unreference(gobj); |
| 74 | mutex_unlock(&rdev->ddev->struct_mutex); |
| 75 | return r; |
| 76 | } |
| 77 | gobj->driver_private = robj; |
| 78 | *obj = gobj; |
| 79 | return 0; |
| 80 | } |
| 81 | |
| 82 | int radeon_gem_object_pin(struct drm_gem_object *obj, uint32_t pin_domain, |
| 83 | uint64_t *gpu_addr) |
| 84 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 85 | struct radeon_bo *robj = obj->driver_private; |
| 86 | int r; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 87 | |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 88 | r = radeon_bo_reserve(robj, false); |
| 89 | if (unlikely(r != 0)) |
| 90 | return r; |
| 91 | r = radeon_bo_pin(robj, pin_domain, gpu_addr); |
| 92 | radeon_bo_unreserve(robj); |
| 93 | return r; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void radeon_gem_object_unpin(struct drm_gem_object *obj) |
| 97 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 98 | struct radeon_bo *robj = obj->driver_private; |
| 99 | int r; |
| 100 | |
| 101 | r = radeon_bo_reserve(robj, false); |
| 102 | if (likely(r == 0)) { |
| 103 | radeon_bo_unpin(robj); |
| 104 | radeon_bo_unreserve(robj); |
| 105 | } |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | int radeon_gem_set_domain(struct drm_gem_object *gobj, |
| 109 | uint32_t rdomain, uint32_t wdomain) |
| 110 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 111 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 112 | uint32_t domain; |
| 113 | int r; |
| 114 | |
| 115 | /* FIXME: reeimplement */ |
| 116 | robj = gobj->driver_private; |
| 117 | /* work out where to validate the buffer to */ |
| 118 | domain = wdomain; |
| 119 | if (!domain) { |
| 120 | domain = rdomain; |
| 121 | } |
| 122 | if (!domain) { |
| 123 | /* Do nothings */ |
| 124 | printk(KERN_WARNING "Set domain withou domain !\n"); |
| 125 | return 0; |
| 126 | } |
| 127 | if (domain == RADEON_GEM_DOMAIN_CPU) { |
| 128 | /* Asking for cpu access wait for object idle */ |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 129 | r = radeon_bo_wait(robj, NULL, false); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 130 | if (r) { |
| 131 | printk(KERN_ERR "Failed to wait for object !\n"); |
| 132 | return r; |
| 133 | } |
| 134 | } |
| 135 | return 0; |
| 136 | } |
| 137 | |
| 138 | int radeon_gem_init(struct radeon_device *rdev) |
| 139 | { |
| 140 | INIT_LIST_HEAD(&rdev->gem.objects); |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | void radeon_gem_fini(struct radeon_device *rdev) |
| 145 | { |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 146 | radeon_bo_force_delete(rdev); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | |
| 150 | /* |
| 151 | * GEM ioctls. |
| 152 | */ |
| 153 | int radeon_gem_info_ioctl(struct drm_device *dev, void *data, |
| 154 | struct drm_file *filp) |
| 155 | { |
| 156 | struct radeon_device *rdev = dev->dev_private; |
| 157 | struct drm_radeon_gem_info *args = data; |
| 158 | |
Dave Airlie | 7a50f01 | 2009-07-21 20:39:30 +1000 | [diff] [blame] | 159 | args->vram_size = rdev->mc.real_vram_size; |
Michel Dänzer | 38e1492 | 2009-08-05 00:19:51 +0200 | [diff] [blame] | 160 | args->vram_visible = rdev->mc.real_vram_size; |
| 161 | if (rdev->stollen_vga_memory) |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 162 | args->vram_visible -= radeon_bo_size(rdev->stollen_vga_memory); |
| 163 | if (rdev->fbdev_rbo) |
| 164 | args->vram_visible -= radeon_bo_size(rdev->fbdev_rbo); |
Michel Dänzer | 38e1492 | 2009-08-05 00:19:51 +0200 | [diff] [blame] | 165 | args->gart_size = rdev->mc.gtt_size - rdev->cp.ring_size - 4096 - |
| 166 | RADEON_IB_POOL_SIZE*64*1024; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | int radeon_gem_pread_ioctl(struct drm_device *dev, void *data, |
| 171 | struct drm_file *filp) |
| 172 | { |
| 173 | /* TODO: implement */ |
| 174 | DRM_ERROR("unimplemented %s\n", __func__); |
| 175 | return -ENOSYS; |
| 176 | } |
| 177 | |
| 178 | int radeon_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
| 179 | struct drm_file *filp) |
| 180 | { |
| 181 | /* TODO: implement */ |
| 182 | DRM_ERROR("unimplemented %s\n", __func__); |
| 183 | return -ENOSYS; |
| 184 | } |
| 185 | |
| 186 | int radeon_gem_create_ioctl(struct drm_device *dev, void *data, |
| 187 | struct drm_file *filp) |
| 188 | { |
| 189 | struct radeon_device *rdev = dev->dev_private; |
| 190 | struct drm_radeon_gem_create *args = data; |
| 191 | struct drm_gem_object *gobj; |
| 192 | uint32_t handle; |
| 193 | int r; |
| 194 | |
| 195 | /* create a gem object to contain this object in */ |
| 196 | args->size = roundup(args->size, PAGE_SIZE); |
| 197 | r = radeon_gem_object_create(rdev, args->size, args->alignment, |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 198 | args->initial_domain, false, |
| 199 | false, &gobj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 200 | if (r) { |
| 201 | return r; |
| 202 | } |
| 203 | r = drm_gem_handle_create(filp, gobj, &handle); |
| 204 | if (r) { |
| 205 | mutex_lock(&dev->struct_mutex); |
| 206 | drm_gem_object_unreference(gobj); |
| 207 | mutex_unlock(&dev->struct_mutex); |
| 208 | return r; |
| 209 | } |
| 210 | mutex_lock(&dev->struct_mutex); |
| 211 | drm_gem_object_handle_unreference(gobj); |
| 212 | mutex_unlock(&dev->struct_mutex); |
| 213 | args->handle = handle; |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | int radeon_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
| 218 | struct drm_file *filp) |
| 219 | { |
| 220 | /* transition the BO to a domain - |
| 221 | * just validate the BO into a certain domain */ |
| 222 | struct drm_radeon_gem_set_domain *args = data; |
| 223 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 224 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 225 | int r; |
| 226 | |
| 227 | /* for now if someone requests domain CPU - |
| 228 | * just make sure the buffer is finished with */ |
| 229 | |
| 230 | /* just do a BO wait for now */ |
| 231 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 232 | if (gobj == NULL) { |
| 233 | return -EINVAL; |
| 234 | } |
| 235 | robj = gobj->driver_private; |
| 236 | |
| 237 | r = radeon_gem_set_domain(gobj, args->read_domains, args->write_domain); |
| 238 | |
| 239 | mutex_lock(&dev->struct_mutex); |
| 240 | drm_gem_object_unreference(gobj); |
| 241 | mutex_unlock(&dev->struct_mutex); |
| 242 | return r; |
| 243 | } |
| 244 | |
| 245 | int radeon_gem_mmap_ioctl(struct drm_device *dev, void *data, |
| 246 | struct drm_file *filp) |
| 247 | { |
| 248 | struct drm_radeon_gem_mmap *args = data; |
| 249 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 250 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 251 | |
| 252 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 253 | if (gobj == NULL) { |
| 254 | return -EINVAL; |
| 255 | } |
| 256 | robj = gobj->driver_private; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 257 | args->addr_ptr = radeon_bo_mmap_offset(robj); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 258 | mutex_lock(&dev->struct_mutex); |
| 259 | drm_gem_object_unreference(gobj); |
| 260 | mutex_unlock(&dev->struct_mutex); |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 261 | return 0; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | int radeon_gem_busy_ioctl(struct drm_device *dev, void *data, |
| 265 | struct drm_file *filp) |
| 266 | { |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 267 | struct drm_radeon_gem_busy *args = data; |
| 268 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 269 | struct radeon_bo *robj; |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 270 | int r; |
Dave Airlie | 4361e52 | 2009-12-10 15:59:32 +1000 | [diff] [blame] | 271 | uint32_t cur_placement = 0; |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 272 | |
| 273 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 274 | if (gobj == NULL) { |
| 275 | return -EINVAL; |
| 276 | } |
| 277 | robj = gobj->driver_private; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 278 | r = radeon_bo_wait(robj, &cur_placement, true); |
Michel Dänzer | 9f844e5 | 2009-08-22 17:38:23 +0200 | [diff] [blame] | 279 | switch (cur_placement) { |
| 280 | case TTM_PL_VRAM: |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 281 | args->domain = RADEON_GEM_DOMAIN_VRAM; |
Michel Dänzer | 9f844e5 | 2009-08-22 17:38:23 +0200 | [diff] [blame] | 282 | break; |
| 283 | case TTM_PL_TT: |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 284 | args->domain = RADEON_GEM_DOMAIN_GTT; |
Michel Dänzer | 9f844e5 | 2009-08-22 17:38:23 +0200 | [diff] [blame] | 285 | break; |
| 286 | case TTM_PL_SYSTEM: |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 287 | args->domain = RADEON_GEM_DOMAIN_CPU; |
Michel Dänzer | 9f844e5 | 2009-08-22 17:38:23 +0200 | [diff] [blame] | 288 | default: |
| 289 | break; |
| 290 | } |
Dave Airlie | cefb87e | 2009-08-16 21:05:45 +1000 | [diff] [blame] | 291 | mutex_lock(&dev->struct_mutex); |
| 292 | drm_gem_object_unreference(gobj); |
| 293 | mutex_unlock(&dev->struct_mutex); |
Dave Airlie | e3b2415 | 2009-08-21 09:47:45 +1000 | [diff] [blame] | 294 | return r; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | int radeon_gem_wait_idle_ioctl(struct drm_device *dev, void *data, |
| 298 | struct drm_file *filp) |
| 299 | { |
| 300 | struct drm_radeon_gem_wait_idle *args = data; |
| 301 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 302 | struct radeon_bo *robj; |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 303 | int r; |
| 304 | |
| 305 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 306 | if (gobj == NULL) { |
| 307 | return -EINVAL; |
| 308 | } |
| 309 | robj = gobj->driver_private; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 310 | r = radeon_bo_wait(robj, NULL, false); |
Jerome Glisse | 771fe6b | 2009-06-05 14:42:42 +0200 | [diff] [blame] | 311 | mutex_lock(&dev->struct_mutex); |
| 312 | drm_gem_object_unreference(gobj); |
| 313 | mutex_unlock(&dev->struct_mutex); |
| 314 | return r; |
| 315 | } |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 316 | |
| 317 | int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data, |
| 318 | struct drm_file *filp) |
| 319 | { |
| 320 | struct drm_radeon_gem_set_tiling *args = data; |
| 321 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 322 | struct radeon_bo *robj; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 323 | int r = 0; |
| 324 | |
| 325 | DRM_DEBUG("%d \n", args->handle); |
| 326 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 327 | if (gobj == NULL) |
| 328 | return -EINVAL; |
| 329 | robj = gobj->driver_private; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 330 | r = radeon_bo_set_tiling_flags(robj, args->tiling_flags, args->pitch); |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 331 | mutex_lock(&dev->struct_mutex); |
| 332 | drm_gem_object_unreference(gobj); |
| 333 | mutex_unlock(&dev->struct_mutex); |
| 334 | return r; |
| 335 | } |
| 336 | |
| 337 | int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data, |
| 338 | struct drm_file *filp) |
| 339 | { |
| 340 | struct drm_radeon_gem_get_tiling *args = data; |
| 341 | struct drm_gem_object *gobj; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 342 | struct radeon_bo *rbo; |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 343 | int r = 0; |
| 344 | |
| 345 | DRM_DEBUG("\n"); |
| 346 | gobj = drm_gem_object_lookup(dev, filp, args->handle); |
| 347 | if (gobj == NULL) |
| 348 | return -EINVAL; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 349 | rbo = gobj->driver_private; |
| 350 | r = radeon_bo_reserve(rbo, false); |
| 351 | if (unlikely(r != 0)) |
Dave Airlie | 51f07b7 | 2009-12-16 13:10:43 +1000 | [diff] [blame] | 352 | goto out; |
Jerome Glisse | 4c78867 | 2009-11-20 14:29:23 +0100 | [diff] [blame] | 353 | radeon_bo_get_tiling_flags(rbo, &args->tiling_flags, &args->pitch); |
| 354 | radeon_bo_unreserve(rbo); |
Dave Airlie | 51f07b7 | 2009-12-16 13:10:43 +1000 | [diff] [blame] | 355 | out: |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 356 | mutex_lock(&dev->struct_mutex); |
| 357 | drm_gem_object_unreference(gobj); |
| 358 | mutex_unlock(&dev->struct_mutex); |
| 359 | return r; |
| 360 | } |