Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * |
| 3 | * Copyright (c) 2006-2009 VMware, Inc., Palo Alto, CA., USA |
| 4 | * All Rights Reserved. |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | * copy of this software and associated documentation files (the |
| 8 | * "Software"), to deal in the Software without restriction, including |
| 9 | * without limitation the rights to use, copy, modify, merge, publish, |
| 10 | * distribute, sub license, and/or sell copies of the Software, and to |
| 11 | * permit persons to whom the Software is furnished to do so, subject to |
| 12 | * the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice (including the |
| 15 | * next paragraph) shall be included in all copies or substantial portions |
| 16 | * of the Software. |
| 17 | * |
| 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL |
| 21 | * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, |
| 22 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 23 | * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE |
| 24 | * USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 25 | * |
| 26 | **************************************************************************/ |
| 27 | /* |
| 28 | * Authors: Thomas Hellstrom <thellstrom-at-vmware-dot-com> |
| 29 | */ |
| 30 | |
| 31 | #include "ttm/ttm_module.h" |
| 32 | #include "ttm/ttm_bo_driver.h" |
| 33 | #include "ttm/ttm_placement.h" |
| 34 | #include <linux/jiffies.h> |
| 35 | #include <linux/slab.h> |
| 36 | #include <linux/sched.h> |
| 37 | #include <linux/mm.h> |
| 38 | #include <linux/file.h> |
| 39 | #include <linux/module.h> |
| 40 | |
| 41 | #define TTM_ASSERT_LOCKED(param) |
| 42 | #define TTM_DEBUG(fmt, arg...) |
| 43 | #define TTM_BO_HASH_ORDER 13 |
| 44 | |
| 45 | static int ttm_bo_setup_vm(struct ttm_buffer_object *bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 46 | static int ttm_bo_swapout(struct ttm_mem_shrink *shrink); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 47 | static void ttm_bo_global_kobj_release(struct kobject *kobj); |
| 48 | |
| 49 | static struct attribute ttm_bo_count = { |
| 50 | .name = "bo_count", |
| 51 | .mode = S_IRUGO |
| 52 | }; |
| 53 | |
| 54 | static ssize_t ttm_bo_global_show(struct kobject *kobj, |
| 55 | struct attribute *attr, |
| 56 | char *buffer) |
| 57 | { |
| 58 | struct ttm_bo_global *glob = |
| 59 | container_of(kobj, struct ttm_bo_global, kobj); |
| 60 | |
| 61 | return snprintf(buffer, PAGE_SIZE, "%lu\n", |
| 62 | (unsigned long) atomic_read(&glob->bo_count)); |
| 63 | } |
| 64 | |
| 65 | static struct attribute *ttm_bo_global_attrs[] = { |
| 66 | &ttm_bo_count, |
| 67 | NULL |
| 68 | }; |
| 69 | |
| 70 | static struct sysfs_ops ttm_bo_global_ops = { |
| 71 | .show = &ttm_bo_global_show |
| 72 | }; |
| 73 | |
| 74 | static struct kobj_type ttm_bo_glob_kobj_type = { |
| 75 | .release = &ttm_bo_global_kobj_release, |
| 76 | .sysfs_ops = &ttm_bo_global_ops, |
| 77 | .default_attrs = ttm_bo_global_attrs |
| 78 | }; |
| 79 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 80 | |
| 81 | static inline uint32_t ttm_bo_type_flags(unsigned type) |
| 82 | { |
| 83 | return 1 << (type); |
| 84 | } |
| 85 | |
| 86 | static void ttm_bo_release_list(struct kref *list_kref) |
| 87 | { |
| 88 | struct ttm_buffer_object *bo = |
| 89 | container_of(list_kref, struct ttm_buffer_object, list_kref); |
| 90 | struct ttm_bo_device *bdev = bo->bdev; |
| 91 | |
| 92 | BUG_ON(atomic_read(&bo->list_kref.refcount)); |
| 93 | BUG_ON(atomic_read(&bo->kref.refcount)); |
| 94 | BUG_ON(atomic_read(&bo->cpu_writers)); |
| 95 | BUG_ON(bo->sync_obj != NULL); |
| 96 | BUG_ON(bo->mem.mm_node != NULL); |
| 97 | BUG_ON(!list_empty(&bo->lru)); |
| 98 | BUG_ON(!list_empty(&bo->ddestroy)); |
| 99 | |
| 100 | if (bo->ttm) |
| 101 | ttm_tt_destroy(bo->ttm); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 102 | atomic_dec(&bo->glob->bo_count); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 103 | if (bo->destroy) |
| 104 | bo->destroy(bo); |
| 105 | else { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 106 | ttm_mem_global_free(bdev->glob->mem_glob, bo->acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 107 | kfree(bo); |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo, bool interruptible) |
| 112 | { |
| 113 | |
| 114 | if (interruptible) { |
| 115 | int ret = 0; |
| 116 | |
| 117 | ret = wait_event_interruptible(bo->event_queue, |
| 118 | atomic_read(&bo->reserved) == 0); |
| 119 | if (unlikely(ret != 0)) |
| 120 | return -ERESTART; |
| 121 | } else { |
| 122 | wait_event(bo->event_queue, atomic_read(&bo->reserved) == 0); |
| 123 | } |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static void ttm_bo_add_to_lru(struct ttm_buffer_object *bo) |
| 128 | { |
| 129 | struct ttm_bo_device *bdev = bo->bdev; |
| 130 | struct ttm_mem_type_manager *man; |
| 131 | |
| 132 | BUG_ON(!atomic_read(&bo->reserved)); |
| 133 | |
| 134 | if (!(bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) { |
| 135 | |
| 136 | BUG_ON(!list_empty(&bo->lru)); |
| 137 | |
| 138 | man = &bdev->man[bo->mem.mem_type]; |
| 139 | list_add_tail(&bo->lru, &man->lru); |
| 140 | kref_get(&bo->list_kref); |
| 141 | |
| 142 | if (bo->ttm != NULL) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 143 | list_add_tail(&bo->swap, &bo->glob->swap_lru); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 144 | kref_get(&bo->list_kref); |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * Call with the lru_lock held. |
| 151 | */ |
| 152 | |
| 153 | static int ttm_bo_del_from_lru(struct ttm_buffer_object *bo) |
| 154 | { |
| 155 | int put_count = 0; |
| 156 | |
| 157 | if (!list_empty(&bo->swap)) { |
| 158 | list_del_init(&bo->swap); |
| 159 | ++put_count; |
| 160 | } |
| 161 | if (!list_empty(&bo->lru)) { |
| 162 | list_del_init(&bo->lru); |
| 163 | ++put_count; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * TODO: Add a driver hook to delete from |
| 168 | * driver-specific LRU's here. |
| 169 | */ |
| 170 | |
| 171 | return put_count; |
| 172 | } |
| 173 | |
| 174 | int ttm_bo_reserve_locked(struct ttm_buffer_object *bo, |
| 175 | bool interruptible, |
| 176 | bool no_wait, bool use_sequence, uint32_t sequence) |
| 177 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 178 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 179 | int ret; |
| 180 | |
| 181 | while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) { |
| 182 | if (use_sequence && bo->seq_valid && |
| 183 | (sequence - bo->val_seq < (1 << 31))) { |
| 184 | return -EAGAIN; |
| 185 | } |
| 186 | |
| 187 | if (no_wait) |
| 188 | return -EBUSY; |
| 189 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 190 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 191 | ret = ttm_bo_wait_unreserved(bo, interruptible); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 192 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 193 | |
| 194 | if (unlikely(ret)) |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | if (use_sequence) { |
| 199 | bo->val_seq = sequence; |
| 200 | bo->seq_valid = true; |
| 201 | } else { |
| 202 | bo->seq_valid = false; |
| 203 | } |
| 204 | |
| 205 | return 0; |
| 206 | } |
| 207 | EXPORT_SYMBOL(ttm_bo_reserve); |
| 208 | |
| 209 | static void ttm_bo_ref_bug(struct kref *list_kref) |
| 210 | { |
| 211 | BUG(); |
| 212 | } |
| 213 | |
| 214 | int ttm_bo_reserve(struct ttm_buffer_object *bo, |
| 215 | bool interruptible, |
| 216 | bool no_wait, bool use_sequence, uint32_t sequence) |
| 217 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 218 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 219 | int put_count = 0; |
| 220 | int ret; |
| 221 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 222 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 223 | ret = ttm_bo_reserve_locked(bo, interruptible, no_wait, use_sequence, |
| 224 | sequence); |
| 225 | if (likely(ret == 0)) |
| 226 | put_count = ttm_bo_del_from_lru(bo); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 227 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 228 | |
| 229 | while (put_count--) |
| 230 | kref_put(&bo->list_kref, ttm_bo_ref_bug); |
| 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | void ttm_bo_unreserve(struct ttm_buffer_object *bo) |
| 236 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 237 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 238 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 239 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 240 | ttm_bo_add_to_lru(bo); |
| 241 | atomic_set(&bo->reserved, 0); |
| 242 | wake_up_all(&bo->event_queue); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 243 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 244 | } |
| 245 | EXPORT_SYMBOL(ttm_bo_unreserve); |
| 246 | |
| 247 | /* |
| 248 | * Call bo->mutex locked. |
| 249 | */ |
| 250 | |
| 251 | static int ttm_bo_add_ttm(struct ttm_buffer_object *bo, bool zero_alloc) |
| 252 | { |
| 253 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 254 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 255 | int ret = 0; |
| 256 | uint32_t page_flags = 0; |
| 257 | |
| 258 | TTM_ASSERT_LOCKED(&bo->mutex); |
| 259 | bo->ttm = NULL; |
| 260 | |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 261 | if (bdev->need_dma32) |
| 262 | page_flags |= TTM_PAGE_FLAG_DMA32; |
| 263 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 264 | switch (bo->type) { |
| 265 | case ttm_bo_type_device: |
| 266 | if (zero_alloc) |
| 267 | page_flags |= TTM_PAGE_FLAG_ZERO_ALLOC; |
| 268 | case ttm_bo_type_kernel: |
| 269 | bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 270 | page_flags, glob->dummy_read_page); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 271 | if (unlikely(bo->ttm == NULL)) |
| 272 | ret = -ENOMEM; |
| 273 | break; |
| 274 | case ttm_bo_type_user: |
| 275 | bo->ttm = ttm_tt_create(bdev, bo->num_pages << PAGE_SHIFT, |
| 276 | page_flags | TTM_PAGE_FLAG_USER, |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 277 | glob->dummy_read_page); |
Dave Airlie | 447aeb9 | 2009-12-08 09:25:45 +1000 | [diff] [blame^] | 278 | if (unlikely(bo->ttm == NULL)) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 279 | ret = -ENOMEM; |
Dave Airlie | 447aeb9 | 2009-12-08 09:25:45 +1000 | [diff] [blame^] | 280 | break; |
| 281 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 282 | |
| 283 | ret = ttm_tt_set_user(bo->ttm, current, |
| 284 | bo->buffer_start, bo->num_pages); |
| 285 | if (unlikely(ret != 0)) |
| 286 | ttm_tt_destroy(bo->ttm); |
| 287 | break; |
| 288 | default: |
| 289 | printk(KERN_ERR TTM_PFX "Illegal buffer object type\n"); |
| 290 | ret = -EINVAL; |
| 291 | break; |
| 292 | } |
| 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
| 297 | static int ttm_bo_handle_move_mem(struct ttm_buffer_object *bo, |
| 298 | struct ttm_mem_reg *mem, |
| 299 | bool evict, bool interruptible, bool no_wait) |
| 300 | { |
| 301 | struct ttm_bo_device *bdev = bo->bdev; |
| 302 | bool old_is_pci = ttm_mem_reg_is_pci(bdev, &bo->mem); |
| 303 | bool new_is_pci = ttm_mem_reg_is_pci(bdev, mem); |
| 304 | struct ttm_mem_type_manager *old_man = &bdev->man[bo->mem.mem_type]; |
| 305 | struct ttm_mem_type_manager *new_man = &bdev->man[mem->mem_type]; |
| 306 | int ret = 0; |
| 307 | |
| 308 | if (old_is_pci || new_is_pci || |
| 309 | ((mem->placement & bo->mem.placement & TTM_PL_MASK_CACHING) == 0)) |
| 310 | ttm_bo_unmap_virtual(bo); |
| 311 | |
| 312 | /* |
| 313 | * Create and bind a ttm if required. |
| 314 | */ |
| 315 | |
| 316 | if (!(new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && (bo->ttm == NULL)) { |
| 317 | ret = ttm_bo_add_ttm(bo, false); |
| 318 | if (ret) |
| 319 | goto out_err; |
| 320 | |
| 321 | ret = ttm_tt_set_placement_caching(bo->ttm, mem->placement); |
| 322 | if (ret) |
Thomas Hellstrom | 87ef920 | 2009-06-17 12:29:57 +0200 | [diff] [blame] | 323 | goto out_err; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 324 | |
| 325 | if (mem->mem_type != TTM_PL_SYSTEM) { |
| 326 | ret = ttm_tt_bind(bo->ttm, mem); |
| 327 | if (ret) |
| 328 | goto out_err; |
| 329 | } |
| 330 | |
| 331 | if (bo->mem.mem_type == TTM_PL_SYSTEM) { |
| 332 | |
| 333 | struct ttm_mem_reg *old_mem = &bo->mem; |
| 334 | uint32_t save_flags = old_mem->placement; |
| 335 | |
| 336 | *old_mem = *mem; |
| 337 | mem->mm_node = NULL; |
| 338 | ttm_flag_masked(&save_flags, mem->placement, |
| 339 | TTM_PL_MASK_MEMTYPE); |
| 340 | goto moved; |
| 341 | } |
| 342 | |
| 343 | } |
| 344 | |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 345 | if (bdev->driver->move_notify) |
| 346 | bdev->driver->move_notify(bo, mem); |
| 347 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 348 | if (!(old_man->flags & TTM_MEMTYPE_FLAG_FIXED) && |
| 349 | !(new_man->flags & TTM_MEMTYPE_FLAG_FIXED)) |
| 350 | ret = ttm_bo_move_ttm(bo, evict, no_wait, mem); |
| 351 | else if (bdev->driver->move) |
| 352 | ret = bdev->driver->move(bo, evict, interruptible, |
| 353 | no_wait, mem); |
| 354 | else |
| 355 | ret = ttm_bo_move_memcpy(bo, evict, no_wait, mem); |
| 356 | |
| 357 | if (ret) |
| 358 | goto out_err; |
| 359 | |
| 360 | moved: |
| 361 | if (bo->evicted) { |
| 362 | ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement); |
| 363 | if (ret) |
| 364 | printk(KERN_ERR TTM_PFX "Can not flush read caches\n"); |
| 365 | bo->evicted = false; |
| 366 | } |
| 367 | |
| 368 | if (bo->mem.mm_node) { |
| 369 | spin_lock(&bo->lock); |
| 370 | bo->offset = (bo->mem.mm_node->start << PAGE_SHIFT) + |
| 371 | bdev->man[bo->mem.mem_type].gpu_offset; |
| 372 | bo->cur_placement = bo->mem.placement; |
| 373 | spin_unlock(&bo->lock); |
| 374 | } |
| 375 | |
| 376 | return 0; |
| 377 | |
| 378 | out_err: |
| 379 | new_man = &bdev->man[bo->mem.mem_type]; |
| 380 | if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) { |
| 381 | ttm_tt_unbind(bo->ttm); |
| 382 | ttm_tt_destroy(bo->ttm); |
| 383 | bo->ttm = NULL; |
| 384 | } |
| 385 | |
| 386 | return ret; |
| 387 | } |
| 388 | |
| 389 | /** |
| 390 | * If bo idle, remove from delayed- and lru lists, and unref. |
| 391 | * If not idle, and already on delayed list, do nothing. |
| 392 | * If not idle, and not on delayed list, put on delayed list, |
| 393 | * up the list_kref and schedule a delayed list check. |
| 394 | */ |
| 395 | |
| 396 | static int ttm_bo_cleanup_refs(struct ttm_buffer_object *bo, bool remove_all) |
| 397 | { |
| 398 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 399 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 400 | struct ttm_bo_driver *driver = bdev->driver; |
| 401 | int ret; |
| 402 | |
| 403 | spin_lock(&bo->lock); |
| 404 | (void) ttm_bo_wait(bo, false, false, !remove_all); |
| 405 | |
| 406 | if (!bo->sync_obj) { |
| 407 | int put_count; |
| 408 | |
| 409 | spin_unlock(&bo->lock); |
| 410 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 411 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 412 | ret = ttm_bo_reserve_locked(bo, false, false, false, 0); |
| 413 | BUG_ON(ret); |
| 414 | if (bo->ttm) |
| 415 | ttm_tt_unbind(bo->ttm); |
| 416 | |
| 417 | if (!list_empty(&bo->ddestroy)) { |
| 418 | list_del_init(&bo->ddestroy); |
| 419 | kref_put(&bo->list_kref, ttm_bo_ref_bug); |
| 420 | } |
| 421 | if (bo->mem.mm_node) { |
| 422 | drm_mm_put_block(bo->mem.mm_node); |
| 423 | bo->mem.mm_node = NULL; |
| 424 | } |
| 425 | put_count = ttm_bo_del_from_lru(bo); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 426 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 427 | |
| 428 | atomic_set(&bo->reserved, 0); |
| 429 | |
| 430 | while (put_count--) |
| 431 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 436 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 437 | if (list_empty(&bo->ddestroy)) { |
| 438 | void *sync_obj = bo->sync_obj; |
| 439 | void *sync_obj_arg = bo->sync_obj_arg; |
| 440 | |
| 441 | kref_get(&bo->list_kref); |
| 442 | list_add_tail(&bo->ddestroy, &bdev->ddestroy); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 443 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 444 | spin_unlock(&bo->lock); |
| 445 | |
| 446 | if (sync_obj) |
| 447 | driver->sync_obj_flush(sync_obj, sync_obj_arg); |
| 448 | schedule_delayed_work(&bdev->wq, |
| 449 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 450 | ret = 0; |
| 451 | |
| 452 | } else { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 453 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 454 | spin_unlock(&bo->lock); |
| 455 | ret = -EBUSY; |
| 456 | } |
| 457 | |
| 458 | return ret; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Traverse the delayed list, and call ttm_bo_cleanup_refs on all |
| 463 | * encountered buffers. |
| 464 | */ |
| 465 | |
| 466 | static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all) |
| 467 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 468 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 469 | struct ttm_buffer_object *entry, *nentry; |
| 470 | struct list_head *list, *next; |
| 471 | int ret; |
| 472 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 473 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 474 | list_for_each_safe(list, next, &bdev->ddestroy) { |
| 475 | entry = list_entry(list, struct ttm_buffer_object, ddestroy); |
| 476 | nentry = NULL; |
| 477 | |
| 478 | /* |
| 479 | * Protect the next list entry from destruction while we |
| 480 | * unlock the lru_lock. |
| 481 | */ |
| 482 | |
| 483 | if (next != &bdev->ddestroy) { |
| 484 | nentry = list_entry(next, struct ttm_buffer_object, |
| 485 | ddestroy); |
| 486 | kref_get(&nentry->list_kref); |
| 487 | } |
| 488 | kref_get(&entry->list_kref); |
| 489 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 490 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 491 | ret = ttm_bo_cleanup_refs(entry, remove_all); |
| 492 | kref_put(&entry->list_kref, ttm_bo_release_list); |
| 493 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 494 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 495 | if (nentry) { |
| 496 | bool next_onlist = !list_empty(next); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 497 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 498 | kref_put(&nentry->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 499 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 500 | /* |
| 501 | * Someone might have raced us and removed the |
| 502 | * next entry from the list. We don't bother restarting |
| 503 | * list traversal. |
| 504 | */ |
| 505 | |
| 506 | if (!next_onlist) |
| 507 | break; |
| 508 | } |
| 509 | if (ret) |
| 510 | break; |
| 511 | } |
| 512 | ret = !list_empty(&bdev->ddestroy); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 513 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 514 | |
| 515 | return ret; |
| 516 | } |
| 517 | |
| 518 | static void ttm_bo_delayed_workqueue(struct work_struct *work) |
| 519 | { |
| 520 | struct ttm_bo_device *bdev = |
| 521 | container_of(work, struct ttm_bo_device, wq.work); |
| 522 | |
| 523 | if (ttm_bo_delayed_delete(bdev, false)) { |
| 524 | schedule_delayed_work(&bdev->wq, |
| 525 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | static void ttm_bo_release(struct kref *kref) |
| 530 | { |
| 531 | struct ttm_buffer_object *bo = |
| 532 | container_of(kref, struct ttm_buffer_object, kref); |
| 533 | struct ttm_bo_device *bdev = bo->bdev; |
| 534 | |
| 535 | if (likely(bo->vm_node != NULL)) { |
| 536 | rb_erase(&bo->vm_rb, &bdev->addr_space_rb); |
| 537 | drm_mm_put_block(bo->vm_node); |
| 538 | bo->vm_node = NULL; |
| 539 | } |
| 540 | write_unlock(&bdev->vm_lock); |
| 541 | ttm_bo_cleanup_refs(bo, false); |
| 542 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 543 | write_lock(&bdev->vm_lock); |
| 544 | } |
| 545 | |
| 546 | void ttm_bo_unref(struct ttm_buffer_object **p_bo) |
| 547 | { |
| 548 | struct ttm_buffer_object *bo = *p_bo; |
| 549 | struct ttm_bo_device *bdev = bo->bdev; |
| 550 | |
| 551 | *p_bo = NULL; |
| 552 | write_lock(&bdev->vm_lock); |
| 553 | kref_put(&bo->kref, ttm_bo_release); |
| 554 | write_unlock(&bdev->vm_lock); |
| 555 | } |
| 556 | EXPORT_SYMBOL(ttm_bo_unref); |
| 557 | |
| 558 | static int ttm_bo_evict(struct ttm_buffer_object *bo, unsigned mem_type, |
| 559 | bool interruptible, bool no_wait) |
| 560 | { |
| 561 | int ret = 0; |
| 562 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 563 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 564 | struct ttm_mem_reg evict_mem; |
| 565 | uint32_t proposed_placement; |
| 566 | |
| 567 | if (bo->mem.mem_type != mem_type) |
| 568 | goto out; |
| 569 | |
| 570 | spin_lock(&bo->lock); |
| 571 | ret = ttm_bo_wait(bo, false, interruptible, no_wait); |
| 572 | spin_unlock(&bo->lock); |
| 573 | |
Thomas Hellstrom | 78ecf09 | 2009-06-17 12:29:55 +0200 | [diff] [blame] | 574 | if (unlikely(ret != 0)) { |
| 575 | if (ret != -ERESTART) { |
| 576 | printk(KERN_ERR TTM_PFX |
| 577 | "Failed to expire sync object before " |
| 578 | "buffer eviction.\n"); |
| 579 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 580 | goto out; |
| 581 | } |
| 582 | |
| 583 | BUG_ON(!atomic_read(&bo->reserved)); |
| 584 | |
| 585 | evict_mem = bo->mem; |
| 586 | evict_mem.mm_node = NULL; |
| 587 | |
| 588 | proposed_placement = bdev->driver->evict_flags(bo); |
| 589 | |
| 590 | ret = ttm_bo_mem_space(bo, proposed_placement, |
| 591 | &evict_mem, interruptible, no_wait); |
| 592 | if (unlikely(ret != 0 && ret != -ERESTART)) |
| 593 | ret = ttm_bo_mem_space(bo, TTM_PL_FLAG_SYSTEM, |
| 594 | &evict_mem, interruptible, no_wait); |
| 595 | |
| 596 | if (ret) { |
| 597 | if (ret != -ERESTART) |
| 598 | printk(KERN_ERR TTM_PFX |
| 599 | "Failed to find memory space for " |
| 600 | "buffer 0x%p eviction.\n", bo); |
| 601 | goto out; |
| 602 | } |
| 603 | |
| 604 | ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, interruptible, |
| 605 | no_wait); |
| 606 | if (ret) { |
| 607 | if (ret != -ERESTART) |
| 608 | printk(KERN_ERR TTM_PFX "Buffer eviction failed\n"); |
| 609 | goto out; |
| 610 | } |
| 611 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 612 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 613 | if (evict_mem.mm_node) { |
| 614 | drm_mm_put_block(evict_mem.mm_node); |
| 615 | evict_mem.mm_node = NULL; |
| 616 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 617 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 618 | bo->evicted = true; |
| 619 | out: |
| 620 | return ret; |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * Repeatedly evict memory from the LRU for @mem_type until we create enough |
| 625 | * space, or we've evicted everything and there isn't enough space. |
| 626 | */ |
| 627 | static int ttm_bo_mem_force_space(struct ttm_bo_device *bdev, |
| 628 | struct ttm_mem_reg *mem, |
| 629 | uint32_t mem_type, |
| 630 | bool interruptible, bool no_wait) |
| 631 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 632 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 633 | struct drm_mm_node *node; |
| 634 | struct ttm_buffer_object *entry; |
| 635 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
| 636 | struct list_head *lru; |
| 637 | unsigned long num_pages = mem->num_pages; |
| 638 | int put_count = 0; |
| 639 | int ret; |
| 640 | |
| 641 | retry_pre_get: |
| 642 | ret = drm_mm_pre_get(&man->manager); |
| 643 | if (unlikely(ret != 0)) |
| 644 | return ret; |
| 645 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 646 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 647 | do { |
| 648 | node = drm_mm_search_free(&man->manager, num_pages, |
| 649 | mem->page_alignment, 1); |
| 650 | if (node) |
| 651 | break; |
| 652 | |
| 653 | lru = &man->lru; |
| 654 | if (list_empty(lru)) |
| 655 | break; |
| 656 | |
| 657 | entry = list_first_entry(lru, struct ttm_buffer_object, lru); |
| 658 | kref_get(&entry->list_kref); |
| 659 | |
| 660 | ret = |
| 661 | ttm_bo_reserve_locked(entry, interruptible, no_wait, |
| 662 | false, 0); |
| 663 | |
| 664 | if (likely(ret == 0)) |
| 665 | put_count = ttm_bo_del_from_lru(entry); |
| 666 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 667 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 668 | |
| 669 | if (unlikely(ret != 0)) |
| 670 | return ret; |
| 671 | |
| 672 | while (put_count--) |
| 673 | kref_put(&entry->list_kref, ttm_bo_ref_bug); |
| 674 | |
| 675 | ret = ttm_bo_evict(entry, mem_type, interruptible, no_wait); |
| 676 | |
| 677 | ttm_bo_unreserve(entry); |
| 678 | |
| 679 | kref_put(&entry->list_kref, ttm_bo_release_list); |
| 680 | if (ret) |
| 681 | return ret; |
| 682 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 683 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 684 | } while (1); |
| 685 | |
| 686 | if (!node) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 687 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 688 | return -ENOMEM; |
| 689 | } |
| 690 | |
| 691 | node = drm_mm_get_block_atomic(node, num_pages, mem->page_alignment); |
| 692 | if (unlikely(!node)) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 693 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 694 | goto retry_pre_get; |
| 695 | } |
| 696 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 697 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 698 | mem->mm_node = node; |
| 699 | mem->mem_type = mem_type; |
| 700 | return 0; |
| 701 | } |
| 702 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 703 | static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man, |
| 704 | uint32_t cur_placement, |
| 705 | uint32_t proposed_placement) |
| 706 | { |
| 707 | uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING; |
| 708 | uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING; |
| 709 | |
| 710 | /** |
| 711 | * Keep current caching if possible. |
| 712 | */ |
| 713 | |
| 714 | if ((cur_placement & caching) != 0) |
| 715 | result |= (cur_placement & caching); |
| 716 | else if ((man->default_caching & caching) != 0) |
| 717 | result |= man->default_caching; |
| 718 | else if ((TTM_PL_FLAG_CACHED & caching) != 0) |
| 719 | result |= TTM_PL_FLAG_CACHED; |
| 720 | else if ((TTM_PL_FLAG_WC & caching) != 0) |
| 721 | result |= TTM_PL_FLAG_WC; |
| 722 | else if ((TTM_PL_FLAG_UNCACHED & caching) != 0) |
| 723 | result |= TTM_PL_FLAG_UNCACHED; |
| 724 | |
| 725 | return result; |
| 726 | } |
| 727 | |
| 728 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 729 | static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man, |
| 730 | bool disallow_fixed, |
| 731 | uint32_t mem_type, |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 732 | uint32_t proposed_placement, |
| 733 | uint32_t *masked_placement) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 734 | { |
| 735 | uint32_t cur_flags = ttm_bo_type_flags(mem_type); |
| 736 | |
| 737 | if ((man->flags & TTM_MEMTYPE_FLAG_FIXED) && disallow_fixed) |
| 738 | return false; |
| 739 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 740 | if ((cur_flags & proposed_placement & TTM_PL_MASK_MEM) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 741 | return false; |
| 742 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 743 | if ((proposed_placement & man->available_caching) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 744 | return false; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 745 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 746 | cur_flags |= (proposed_placement & man->available_caching); |
| 747 | |
| 748 | *masked_placement = cur_flags; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 749 | return true; |
| 750 | } |
| 751 | |
| 752 | /** |
| 753 | * Creates space for memory region @mem according to its type. |
| 754 | * |
| 755 | * This function first searches for free space in compatible memory types in |
| 756 | * the priority order defined by the driver. If free space isn't found, then |
| 757 | * ttm_bo_mem_force_space is attempted in priority order to evict and find |
| 758 | * space. |
| 759 | */ |
| 760 | int ttm_bo_mem_space(struct ttm_buffer_object *bo, |
| 761 | uint32_t proposed_placement, |
| 762 | struct ttm_mem_reg *mem, |
| 763 | bool interruptible, bool no_wait) |
| 764 | { |
| 765 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 766 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 767 | struct ttm_mem_type_manager *man; |
| 768 | |
| 769 | uint32_t num_prios = bdev->driver->num_mem_type_prio; |
| 770 | const uint32_t *prios = bdev->driver->mem_type_prio; |
| 771 | uint32_t i; |
| 772 | uint32_t mem_type = TTM_PL_SYSTEM; |
| 773 | uint32_t cur_flags = 0; |
| 774 | bool type_found = false; |
| 775 | bool type_ok = false; |
| 776 | bool has_eagain = false; |
| 777 | struct drm_mm_node *node = NULL; |
| 778 | int ret; |
| 779 | |
| 780 | mem->mm_node = NULL; |
| 781 | for (i = 0; i < num_prios; ++i) { |
| 782 | mem_type = prios[i]; |
| 783 | man = &bdev->man[mem_type]; |
| 784 | |
| 785 | type_ok = ttm_bo_mt_compatible(man, |
| 786 | bo->type == ttm_bo_type_user, |
| 787 | mem_type, proposed_placement, |
| 788 | &cur_flags); |
| 789 | |
| 790 | if (!type_ok) |
| 791 | continue; |
| 792 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 793 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
| 794 | cur_flags); |
| 795 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 796 | if (mem_type == TTM_PL_SYSTEM) |
| 797 | break; |
| 798 | |
| 799 | if (man->has_type && man->use_type) { |
| 800 | type_found = true; |
| 801 | do { |
| 802 | ret = drm_mm_pre_get(&man->manager); |
| 803 | if (unlikely(ret)) |
| 804 | return ret; |
| 805 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 806 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 807 | node = drm_mm_search_free(&man->manager, |
| 808 | mem->num_pages, |
| 809 | mem->page_alignment, |
| 810 | 1); |
| 811 | if (unlikely(!node)) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 812 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 813 | break; |
| 814 | } |
| 815 | node = drm_mm_get_block_atomic(node, |
| 816 | mem->num_pages, |
| 817 | mem-> |
| 818 | page_alignment); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 819 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 820 | } while (!node); |
| 821 | } |
| 822 | if (node) |
| 823 | break; |
| 824 | } |
| 825 | |
| 826 | if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || node) { |
| 827 | mem->mm_node = node; |
| 828 | mem->mem_type = mem_type; |
| 829 | mem->placement = cur_flags; |
| 830 | return 0; |
| 831 | } |
| 832 | |
| 833 | if (!type_found) |
| 834 | return -EINVAL; |
| 835 | |
| 836 | num_prios = bdev->driver->num_mem_busy_prio; |
| 837 | prios = bdev->driver->mem_busy_prio; |
| 838 | |
| 839 | for (i = 0; i < num_prios; ++i) { |
| 840 | mem_type = prios[i]; |
| 841 | man = &bdev->man[mem_type]; |
| 842 | |
| 843 | if (!man->has_type) |
| 844 | continue; |
| 845 | |
| 846 | if (!ttm_bo_mt_compatible(man, |
| 847 | bo->type == ttm_bo_type_user, |
| 848 | mem_type, |
| 849 | proposed_placement, &cur_flags)) |
| 850 | continue; |
| 851 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 852 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
| 853 | cur_flags); |
| 854 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 855 | ret = ttm_bo_mem_force_space(bdev, mem, mem_type, |
| 856 | interruptible, no_wait); |
| 857 | |
| 858 | if (ret == 0 && mem->mm_node) { |
| 859 | mem->placement = cur_flags; |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | if (ret == -ERESTART) |
| 864 | has_eagain = true; |
| 865 | } |
| 866 | |
| 867 | ret = (has_eagain) ? -ERESTART : -ENOMEM; |
| 868 | return ret; |
| 869 | } |
| 870 | EXPORT_SYMBOL(ttm_bo_mem_space); |
| 871 | |
| 872 | int ttm_bo_wait_cpu(struct ttm_buffer_object *bo, bool no_wait) |
| 873 | { |
| 874 | int ret = 0; |
| 875 | |
| 876 | if ((atomic_read(&bo->cpu_writers) > 0) && no_wait) |
| 877 | return -EBUSY; |
| 878 | |
| 879 | ret = wait_event_interruptible(bo->event_queue, |
| 880 | atomic_read(&bo->cpu_writers) == 0); |
| 881 | |
| 882 | if (ret == -ERESTARTSYS) |
| 883 | ret = -ERESTART; |
| 884 | |
| 885 | return ret; |
| 886 | } |
| 887 | |
| 888 | int ttm_bo_move_buffer(struct ttm_buffer_object *bo, |
| 889 | uint32_t proposed_placement, |
| 890 | bool interruptible, bool no_wait) |
| 891 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 892 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 893 | int ret = 0; |
| 894 | struct ttm_mem_reg mem; |
| 895 | |
| 896 | BUG_ON(!atomic_read(&bo->reserved)); |
| 897 | |
| 898 | /* |
| 899 | * FIXME: It's possible to pipeline buffer moves. |
| 900 | * Have the driver move function wait for idle when necessary, |
| 901 | * instead of doing it here. |
| 902 | */ |
| 903 | |
| 904 | spin_lock(&bo->lock); |
| 905 | ret = ttm_bo_wait(bo, false, interruptible, no_wait); |
| 906 | spin_unlock(&bo->lock); |
| 907 | |
| 908 | if (ret) |
| 909 | return ret; |
| 910 | |
| 911 | mem.num_pages = bo->num_pages; |
| 912 | mem.size = mem.num_pages << PAGE_SHIFT; |
| 913 | mem.page_alignment = bo->mem.page_alignment; |
| 914 | |
| 915 | /* |
| 916 | * Determine where to move the buffer. |
| 917 | */ |
| 918 | |
| 919 | ret = ttm_bo_mem_space(bo, proposed_placement, &mem, |
| 920 | interruptible, no_wait); |
| 921 | if (ret) |
| 922 | goto out_unlock; |
| 923 | |
| 924 | ret = ttm_bo_handle_move_mem(bo, &mem, false, interruptible, no_wait); |
| 925 | |
| 926 | out_unlock: |
| 927 | if (ret && mem.mm_node) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 928 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 929 | drm_mm_put_block(mem.mm_node); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 930 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 931 | } |
| 932 | return ret; |
| 933 | } |
| 934 | |
| 935 | static int ttm_bo_mem_compat(uint32_t proposed_placement, |
| 936 | struct ttm_mem_reg *mem) |
| 937 | { |
| 938 | if ((proposed_placement & mem->placement & TTM_PL_MASK_MEM) == 0) |
| 939 | return 0; |
| 940 | if ((proposed_placement & mem->placement & TTM_PL_MASK_CACHING) == 0) |
| 941 | return 0; |
| 942 | |
| 943 | return 1; |
| 944 | } |
| 945 | |
| 946 | int ttm_buffer_object_validate(struct ttm_buffer_object *bo, |
| 947 | uint32_t proposed_placement, |
| 948 | bool interruptible, bool no_wait) |
| 949 | { |
| 950 | int ret; |
| 951 | |
| 952 | BUG_ON(!atomic_read(&bo->reserved)); |
| 953 | bo->proposed_placement = proposed_placement; |
| 954 | |
| 955 | TTM_DEBUG("Proposed placement 0x%08lx, Old flags 0x%08lx\n", |
| 956 | (unsigned long)proposed_placement, |
| 957 | (unsigned long)bo->mem.placement); |
| 958 | |
| 959 | /* |
| 960 | * Check whether we need to move buffer. |
| 961 | */ |
| 962 | |
| 963 | if (!ttm_bo_mem_compat(bo->proposed_placement, &bo->mem)) { |
| 964 | ret = ttm_bo_move_buffer(bo, bo->proposed_placement, |
| 965 | interruptible, no_wait); |
| 966 | if (ret) { |
| 967 | if (ret != -ERESTART) |
| 968 | printk(KERN_ERR TTM_PFX |
| 969 | "Failed moving buffer. " |
| 970 | "Proposed placement 0x%08x\n", |
| 971 | bo->proposed_placement); |
| 972 | if (ret == -ENOMEM) |
| 973 | printk(KERN_ERR TTM_PFX |
| 974 | "Out of aperture space or " |
| 975 | "DRM memory quota.\n"); |
| 976 | return ret; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | /* |
| 981 | * We might need to add a TTM. |
| 982 | */ |
| 983 | |
| 984 | if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { |
| 985 | ret = ttm_bo_add_ttm(bo, true); |
| 986 | if (ret) |
| 987 | return ret; |
| 988 | } |
| 989 | /* |
| 990 | * Validation has succeeded, move the access and other |
| 991 | * non-mapping-related flag bits from the proposed flags to |
| 992 | * the active flags |
| 993 | */ |
| 994 | |
| 995 | ttm_flag_masked(&bo->mem.placement, bo->proposed_placement, |
| 996 | ~TTM_PL_MASK_MEMTYPE); |
| 997 | |
| 998 | return 0; |
| 999 | } |
| 1000 | EXPORT_SYMBOL(ttm_buffer_object_validate); |
| 1001 | |
| 1002 | int |
| 1003 | ttm_bo_check_placement(struct ttm_buffer_object *bo, |
| 1004 | uint32_t set_flags, uint32_t clr_flags) |
| 1005 | { |
| 1006 | uint32_t new_mask = set_flags | clr_flags; |
| 1007 | |
| 1008 | if ((bo->type == ttm_bo_type_user) && |
| 1009 | (clr_flags & TTM_PL_FLAG_CACHED)) { |
| 1010 | printk(KERN_ERR TTM_PFX |
| 1011 | "User buffers require cache-coherent memory.\n"); |
| 1012 | return -EINVAL; |
| 1013 | } |
| 1014 | |
| 1015 | if (!capable(CAP_SYS_ADMIN)) { |
| 1016 | if (new_mask & TTM_PL_FLAG_NO_EVICT) { |
| 1017 | printk(KERN_ERR TTM_PFX "Need to be root to modify" |
| 1018 | " NO_EVICT status.\n"); |
| 1019 | return -EINVAL; |
| 1020 | } |
| 1021 | |
| 1022 | if ((clr_flags & bo->mem.placement & TTM_PL_MASK_MEMTYPE) && |
| 1023 | (bo->mem.placement & TTM_PL_FLAG_NO_EVICT)) { |
| 1024 | printk(KERN_ERR TTM_PFX |
| 1025 | "Incompatible memory specification" |
| 1026 | " for NO_EVICT buffer.\n"); |
| 1027 | return -EINVAL; |
| 1028 | } |
| 1029 | } |
| 1030 | return 0; |
| 1031 | } |
| 1032 | |
| 1033 | int ttm_buffer_object_init(struct ttm_bo_device *bdev, |
| 1034 | struct ttm_buffer_object *bo, |
| 1035 | unsigned long size, |
| 1036 | enum ttm_bo_type type, |
| 1037 | uint32_t flags, |
| 1038 | uint32_t page_alignment, |
| 1039 | unsigned long buffer_start, |
| 1040 | bool interruptible, |
| 1041 | struct file *persistant_swap_storage, |
| 1042 | size_t acc_size, |
| 1043 | void (*destroy) (struct ttm_buffer_object *)) |
| 1044 | { |
| 1045 | int ret = 0; |
| 1046 | unsigned long num_pages; |
| 1047 | |
| 1048 | size += buffer_start & ~PAGE_MASK; |
| 1049 | num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1050 | if (num_pages == 0) { |
| 1051 | printk(KERN_ERR TTM_PFX "Illegal buffer object size.\n"); |
| 1052 | return -EINVAL; |
| 1053 | } |
| 1054 | bo->destroy = destroy; |
| 1055 | |
| 1056 | spin_lock_init(&bo->lock); |
| 1057 | kref_init(&bo->kref); |
| 1058 | kref_init(&bo->list_kref); |
| 1059 | atomic_set(&bo->cpu_writers, 0); |
| 1060 | atomic_set(&bo->reserved, 1); |
| 1061 | init_waitqueue_head(&bo->event_queue); |
| 1062 | INIT_LIST_HEAD(&bo->lru); |
| 1063 | INIT_LIST_HEAD(&bo->ddestroy); |
| 1064 | INIT_LIST_HEAD(&bo->swap); |
| 1065 | bo->bdev = bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1066 | bo->glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1067 | bo->type = type; |
| 1068 | bo->num_pages = num_pages; |
| 1069 | bo->mem.mem_type = TTM_PL_SYSTEM; |
| 1070 | bo->mem.num_pages = bo->num_pages; |
| 1071 | bo->mem.mm_node = NULL; |
| 1072 | bo->mem.page_alignment = page_alignment; |
| 1073 | bo->buffer_start = buffer_start & PAGE_MASK; |
| 1074 | bo->priv_flags = 0; |
| 1075 | bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED); |
| 1076 | bo->seq_valid = false; |
| 1077 | bo->persistant_swap_storage = persistant_swap_storage; |
| 1078 | bo->acc_size = acc_size; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1079 | atomic_inc(&bo->glob->bo_count); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1080 | |
| 1081 | ret = ttm_bo_check_placement(bo, flags, 0ULL); |
| 1082 | if (unlikely(ret != 0)) |
| 1083 | goto out_err; |
| 1084 | |
| 1085 | /* |
| 1086 | * If no caching attributes are set, accept any form of caching. |
| 1087 | */ |
| 1088 | |
| 1089 | if ((flags & TTM_PL_MASK_CACHING) == 0) |
| 1090 | flags |= TTM_PL_MASK_CACHING; |
| 1091 | |
| 1092 | /* |
| 1093 | * For ttm_bo_type_device buffers, allocate |
| 1094 | * address space from the device. |
| 1095 | */ |
| 1096 | |
| 1097 | if (bo->type == ttm_bo_type_device) { |
| 1098 | ret = ttm_bo_setup_vm(bo); |
| 1099 | if (ret) |
| 1100 | goto out_err; |
| 1101 | } |
| 1102 | |
| 1103 | ret = ttm_buffer_object_validate(bo, flags, interruptible, false); |
| 1104 | if (ret) |
| 1105 | goto out_err; |
| 1106 | |
| 1107 | ttm_bo_unreserve(bo); |
| 1108 | return 0; |
| 1109 | |
| 1110 | out_err: |
| 1111 | ttm_bo_unreserve(bo); |
| 1112 | ttm_bo_unref(&bo); |
| 1113 | |
| 1114 | return ret; |
| 1115 | } |
| 1116 | EXPORT_SYMBOL(ttm_buffer_object_init); |
| 1117 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1118 | static inline size_t ttm_bo_size(struct ttm_bo_global *glob, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1119 | unsigned long num_pages) |
| 1120 | { |
| 1121 | size_t page_array_size = (num_pages * sizeof(void *) + PAGE_SIZE - 1) & |
| 1122 | PAGE_MASK; |
| 1123 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1124 | return glob->ttm_bo_size + 2 * page_array_size; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | int ttm_buffer_object_create(struct ttm_bo_device *bdev, |
| 1128 | unsigned long size, |
| 1129 | enum ttm_bo_type type, |
| 1130 | uint32_t flags, |
| 1131 | uint32_t page_alignment, |
| 1132 | unsigned long buffer_start, |
| 1133 | bool interruptible, |
| 1134 | struct file *persistant_swap_storage, |
| 1135 | struct ttm_buffer_object **p_bo) |
| 1136 | { |
| 1137 | struct ttm_buffer_object *bo; |
| 1138 | int ret; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1139 | struct ttm_mem_global *mem_glob = bdev->glob->mem_glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1140 | |
| 1141 | size_t acc_size = |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1142 | ttm_bo_size(bdev->glob, (size + PAGE_SIZE - 1) >> PAGE_SHIFT); |
Thomas Hellstrom | 5fd9cba | 2009-08-17 16:28:39 +0200 | [diff] [blame] | 1143 | ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1144 | if (unlikely(ret != 0)) |
| 1145 | return ret; |
| 1146 | |
| 1147 | bo = kzalloc(sizeof(*bo), GFP_KERNEL); |
| 1148 | |
| 1149 | if (unlikely(bo == NULL)) { |
Thomas Hellstrom | 5fd9cba | 2009-08-17 16:28:39 +0200 | [diff] [blame] | 1150 | ttm_mem_global_free(mem_glob, acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1151 | return -ENOMEM; |
| 1152 | } |
| 1153 | |
| 1154 | ret = ttm_buffer_object_init(bdev, bo, size, type, flags, |
| 1155 | page_alignment, buffer_start, |
| 1156 | interruptible, |
| 1157 | persistant_swap_storage, acc_size, NULL); |
| 1158 | if (likely(ret == 0)) |
| 1159 | *p_bo = bo; |
| 1160 | |
| 1161 | return ret; |
| 1162 | } |
| 1163 | |
| 1164 | static int ttm_bo_leave_list(struct ttm_buffer_object *bo, |
| 1165 | uint32_t mem_type, bool allow_errors) |
| 1166 | { |
| 1167 | int ret; |
| 1168 | |
| 1169 | spin_lock(&bo->lock); |
| 1170 | ret = ttm_bo_wait(bo, false, false, false); |
| 1171 | spin_unlock(&bo->lock); |
| 1172 | |
| 1173 | if (ret && allow_errors) |
| 1174 | goto out; |
| 1175 | |
| 1176 | if (bo->mem.mem_type == mem_type) |
| 1177 | ret = ttm_bo_evict(bo, mem_type, false, false); |
| 1178 | |
| 1179 | if (ret) { |
| 1180 | if (allow_errors) { |
| 1181 | goto out; |
| 1182 | } else { |
| 1183 | ret = 0; |
| 1184 | printk(KERN_ERR TTM_PFX "Cleanup eviction failed\n"); |
| 1185 | } |
| 1186 | } |
| 1187 | |
| 1188 | out: |
| 1189 | return ret; |
| 1190 | } |
| 1191 | |
| 1192 | static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev, |
| 1193 | struct list_head *head, |
| 1194 | unsigned mem_type, bool allow_errors) |
| 1195 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1196 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1197 | struct ttm_buffer_object *entry; |
| 1198 | int ret; |
| 1199 | int put_count; |
| 1200 | |
| 1201 | /* |
| 1202 | * Can't use standard list traversal since we're unlocking. |
| 1203 | */ |
| 1204 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1205 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1206 | |
| 1207 | while (!list_empty(head)) { |
| 1208 | entry = list_first_entry(head, struct ttm_buffer_object, lru); |
| 1209 | kref_get(&entry->list_kref); |
| 1210 | ret = ttm_bo_reserve_locked(entry, false, false, false, 0); |
| 1211 | put_count = ttm_bo_del_from_lru(entry); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1212 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1213 | while (put_count--) |
| 1214 | kref_put(&entry->list_kref, ttm_bo_ref_bug); |
| 1215 | BUG_ON(ret); |
| 1216 | ret = ttm_bo_leave_list(entry, mem_type, allow_errors); |
| 1217 | ttm_bo_unreserve(entry); |
| 1218 | kref_put(&entry->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1219 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1220 | } |
| 1221 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1222 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1223 | |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type) |
| 1228 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1229 | struct ttm_bo_global *glob = bdev->glob; |
Roel Kluin | c96e7c7 | 2009-08-03 14:22:53 +0200 | [diff] [blame] | 1230 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1231 | int ret = -EINVAL; |
| 1232 | |
| 1233 | if (mem_type >= TTM_NUM_MEM_TYPES) { |
| 1234 | printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", mem_type); |
| 1235 | return ret; |
| 1236 | } |
Roel Kluin | c96e7c7 | 2009-08-03 14:22:53 +0200 | [diff] [blame] | 1237 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1238 | |
| 1239 | if (!man->has_type) { |
| 1240 | printk(KERN_ERR TTM_PFX "Trying to take down uninitialized " |
| 1241 | "memory manager type %u\n", mem_type); |
| 1242 | return ret; |
| 1243 | } |
| 1244 | |
| 1245 | man->use_type = false; |
| 1246 | man->has_type = false; |
| 1247 | |
| 1248 | ret = 0; |
| 1249 | if (mem_type > 0) { |
| 1250 | ttm_bo_force_list_clean(bdev, &man->lru, mem_type, false); |
| 1251 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1252 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1253 | if (drm_mm_clean(&man->manager)) |
| 1254 | drm_mm_takedown(&man->manager); |
| 1255 | else |
| 1256 | ret = -EBUSY; |
| 1257 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1258 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1259 | } |
| 1260 | |
| 1261 | return ret; |
| 1262 | } |
| 1263 | EXPORT_SYMBOL(ttm_bo_clean_mm); |
| 1264 | |
| 1265 | int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type) |
| 1266 | { |
| 1267 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
| 1268 | |
| 1269 | if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) { |
| 1270 | printk(KERN_ERR TTM_PFX |
| 1271 | "Illegal memory manager memory type %u.\n", |
| 1272 | mem_type); |
| 1273 | return -EINVAL; |
| 1274 | } |
| 1275 | |
| 1276 | if (!man->has_type) { |
| 1277 | printk(KERN_ERR TTM_PFX |
| 1278 | "Memory type %u has not been initialized.\n", |
| 1279 | mem_type); |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | return ttm_bo_force_list_clean(bdev, &man->lru, mem_type, true); |
| 1284 | } |
| 1285 | EXPORT_SYMBOL(ttm_bo_evict_mm); |
| 1286 | |
| 1287 | int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, |
| 1288 | unsigned long p_offset, unsigned long p_size) |
| 1289 | { |
| 1290 | int ret = -EINVAL; |
| 1291 | struct ttm_mem_type_manager *man; |
| 1292 | |
| 1293 | if (type >= TTM_NUM_MEM_TYPES) { |
| 1294 | printk(KERN_ERR TTM_PFX "Illegal memory type %d\n", type); |
| 1295 | return ret; |
| 1296 | } |
| 1297 | |
| 1298 | man = &bdev->man[type]; |
| 1299 | if (man->has_type) { |
| 1300 | printk(KERN_ERR TTM_PFX |
| 1301 | "Memory manager already initialized for type %d\n", |
| 1302 | type); |
| 1303 | return ret; |
| 1304 | } |
| 1305 | |
| 1306 | ret = bdev->driver->init_mem_type(bdev, type, man); |
| 1307 | if (ret) |
| 1308 | return ret; |
| 1309 | |
| 1310 | ret = 0; |
| 1311 | if (type != TTM_PL_SYSTEM) { |
| 1312 | if (!p_size) { |
| 1313 | printk(KERN_ERR TTM_PFX |
| 1314 | "Zero size memory manager type %d\n", |
| 1315 | type); |
| 1316 | return ret; |
| 1317 | } |
| 1318 | ret = drm_mm_init(&man->manager, p_offset, p_size); |
| 1319 | if (ret) |
| 1320 | return ret; |
| 1321 | } |
| 1322 | man->has_type = true; |
| 1323 | man->use_type = true; |
| 1324 | man->size = p_size; |
| 1325 | |
| 1326 | INIT_LIST_HEAD(&man->lru); |
| 1327 | |
| 1328 | return 0; |
| 1329 | } |
| 1330 | EXPORT_SYMBOL(ttm_bo_init_mm); |
| 1331 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1332 | static void ttm_bo_global_kobj_release(struct kobject *kobj) |
| 1333 | { |
| 1334 | struct ttm_bo_global *glob = |
| 1335 | container_of(kobj, struct ttm_bo_global, kobj); |
| 1336 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1337 | ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink); |
| 1338 | __free_page(glob->dummy_read_page); |
| 1339 | kfree(glob); |
| 1340 | } |
| 1341 | |
| 1342 | void ttm_bo_global_release(struct ttm_global_reference *ref) |
| 1343 | { |
| 1344 | struct ttm_bo_global *glob = ref->object; |
| 1345 | |
| 1346 | kobject_del(&glob->kobj); |
| 1347 | kobject_put(&glob->kobj); |
| 1348 | } |
| 1349 | EXPORT_SYMBOL(ttm_bo_global_release); |
| 1350 | |
| 1351 | int ttm_bo_global_init(struct ttm_global_reference *ref) |
| 1352 | { |
| 1353 | struct ttm_bo_global_ref *bo_ref = |
| 1354 | container_of(ref, struct ttm_bo_global_ref, ref); |
| 1355 | struct ttm_bo_global *glob = ref->object; |
| 1356 | int ret; |
| 1357 | |
| 1358 | mutex_init(&glob->device_list_mutex); |
| 1359 | spin_lock_init(&glob->lru_lock); |
| 1360 | glob->mem_glob = bo_ref->mem_glob; |
| 1361 | glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32); |
| 1362 | |
| 1363 | if (unlikely(glob->dummy_read_page == NULL)) { |
| 1364 | ret = -ENOMEM; |
| 1365 | goto out_no_drp; |
| 1366 | } |
| 1367 | |
| 1368 | INIT_LIST_HEAD(&glob->swap_lru); |
| 1369 | INIT_LIST_HEAD(&glob->device_list); |
| 1370 | |
| 1371 | ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout); |
| 1372 | ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink); |
| 1373 | if (unlikely(ret != 0)) { |
| 1374 | printk(KERN_ERR TTM_PFX |
| 1375 | "Could not register buffer object swapout.\n"); |
| 1376 | goto out_no_shrink; |
| 1377 | } |
| 1378 | |
| 1379 | glob->ttm_bo_extra_size = |
| 1380 | ttm_round_pot(sizeof(struct ttm_tt)) + |
| 1381 | ttm_round_pot(sizeof(struct ttm_backend)); |
| 1382 | |
| 1383 | glob->ttm_bo_size = glob->ttm_bo_extra_size + |
| 1384 | ttm_round_pot(sizeof(struct ttm_buffer_object)); |
| 1385 | |
| 1386 | atomic_set(&glob->bo_count, 0); |
| 1387 | |
| 1388 | kobject_init(&glob->kobj, &ttm_bo_glob_kobj_type); |
| 1389 | ret = kobject_add(&glob->kobj, ttm_get_kobj(), "buffer_objects"); |
| 1390 | if (unlikely(ret != 0)) |
| 1391 | kobject_put(&glob->kobj); |
| 1392 | return ret; |
| 1393 | out_no_shrink: |
| 1394 | __free_page(glob->dummy_read_page); |
| 1395 | out_no_drp: |
| 1396 | kfree(glob); |
| 1397 | return ret; |
| 1398 | } |
| 1399 | EXPORT_SYMBOL(ttm_bo_global_init); |
| 1400 | |
| 1401 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1402 | int ttm_bo_device_release(struct ttm_bo_device *bdev) |
| 1403 | { |
| 1404 | int ret = 0; |
| 1405 | unsigned i = TTM_NUM_MEM_TYPES; |
| 1406 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1407 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1408 | |
| 1409 | while (i--) { |
| 1410 | man = &bdev->man[i]; |
| 1411 | if (man->has_type) { |
| 1412 | man->use_type = false; |
| 1413 | if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) { |
| 1414 | ret = -EBUSY; |
| 1415 | printk(KERN_ERR TTM_PFX |
| 1416 | "DRM memory manager type %d " |
| 1417 | "is not clean.\n", i); |
| 1418 | } |
| 1419 | man->has_type = false; |
| 1420 | } |
| 1421 | } |
| 1422 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1423 | mutex_lock(&glob->device_list_mutex); |
| 1424 | list_del(&bdev->device_list); |
| 1425 | mutex_unlock(&glob->device_list_mutex); |
| 1426 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1427 | if (!cancel_delayed_work(&bdev->wq)) |
| 1428 | flush_scheduled_work(); |
| 1429 | |
| 1430 | while (ttm_bo_delayed_delete(bdev, true)) |
| 1431 | ; |
| 1432 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1433 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1434 | if (list_empty(&bdev->ddestroy)) |
| 1435 | TTM_DEBUG("Delayed destroy list was clean\n"); |
| 1436 | |
| 1437 | if (list_empty(&bdev->man[0].lru)) |
| 1438 | TTM_DEBUG("Swap list was clean\n"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1439 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1440 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1441 | BUG_ON(!drm_mm_clean(&bdev->addr_space_mm)); |
| 1442 | write_lock(&bdev->vm_lock); |
| 1443 | drm_mm_takedown(&bdev->addr_space_mm); |
| 1444 | write_unlock(&bdev->vm_lock); |
| 1445 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1446 | return ret; |
| 1447 | } |
| 1448 | EXPORT_SYMBOL(ttm_bo_device_release); |
| 1449 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1450 | int ttm_bo_device_init(struct ttm_bo_device *bdev, |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1451 | struct ttm_bo_global *glob, |
| 1452 | struct ttm_bo_driver *driver, |
Dave Airlie | 51c8b40 | 2009-08-20 13:38:04 +1000 | [diff] [blame] | 1453 | uint64_t file_page_offset, |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 1454 | bool need_dma32) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1455 | { |
| 1456 | int ret = -EINVAL; |
| 1457 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1458 | rwlock_init(&bdev->vm_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1459 | bdev->driver = driver; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1460 | |
| 1461 | memset(bdev->man, 0, sizeof(bdev->man)); |
| 1462 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1463 | /* |
| 1464 | * Initialize the system memory buffer type. |
| 1465 | * Other types need to be driver / IOCTL initialized. |
| 1466 | */ |
| 1467 | ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0, 0); |
| 1468 | if (unlikely(ret != 0)) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1469 | goto out_no_sys; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1470 | |
| 1471 | bdev->addr_space_rb = RB_ROOT; |
| 1472 | ret = drm_mm_init(&bdev->addr_space_mm, file_page_offset, 0x10000000); |
| 1473 | if (unlikely(ret != 0)) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1474 | goto out_no_addr_mm; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1475 | |
| 1476 | INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue); |
| 1477 | bdev->nice_mode = true; |
| 1478 | INIT_LIST_HEAD(&bdev->ddestroy); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1479 | bdev->dev_mapping = NULL; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1480 | bdev->glob = glob; |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 1481 | bdev->need_dma32 = need_dma32; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1482 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1483 | mutex_lock(&glob->device_list_mutex); |
| 1484 | list_add_tail(&bdev->device_list, &glob->device_list); |
| 1485 | mutex_unlock(&glob->device_list_mutex); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1486 | |
| 1487 | return 0; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1488 | out_no_addr_mm: |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1489 | ttm_bo_clean_mm(bdev, 0); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1490 | out_no_sys: |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1491 | return ret; |
| 1492 | } |
| 1493 | EXPORT_SYMBOL(ttm_bo_device_init); |
| 1494 | |
| 1495 | /* |
| 1496 | * buffer object vm functions. |
| 1497 | */ |
| 1498 | |
| 1499 | bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 1500 | { |
| 1501 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
| 1502 | |
| 1503 | if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) { |
| 1504 | if (mem->mem_type == TTM_PL_SYSTEM) |
| 1505 | return false; |
| 1506 | |
| 1507 | if (man->flags & TTM_MEMTYPE_FLAG_CMA) |
| 1508 | return false; |
| 1509 | |
| 1510 | if (mem->placement & TTM_PL_FLAG_CACHED) |
| 1511 | return false; |
| 1512 | } |
| 1513 | return true; |
| 1514 | } |
| 1515 | |
| 1516 | int ttm_bo_pci_offset(struct ttm_bo_device *bdev, |
| 1517 | struct ttm_mem_reg *mem, |
| 1518 | unsigned long *bus_base, |
| 1519 | unsigned long *bus_offset, unsigned long *bus_size) |
| 1520 | { |
| 1521 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
| 1522 | |
| 1523 | *bus_size = 0; |
| 1524 | if (!(man->flags & TTM_MEMTYPE_FLAG_MAPPABLE)) |
| 1525 | return -EINVAL; |
| 1526 | |
| 1527 | if (ttm_mem_reg_is_pci(bdev, mem)) { |
| 1528 | *bus_offset = mem->mm_node->start << PAGE_SHIFT; |
| 1529 | *bus_size = mem->num_pages << PAGE_SHIFT; |
| 1530 | *bus_base = man->io_offset; |
| 1531 | } |
| 1532 | |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
| 1536 | void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) |
| 1537 | { |
| 1538 | struct ttm_bo_device *bdev = bo->bdev; |
| 1539 | loff_t offset = (loff_t) bo->addr_space_offset; |
| 1540 | loff_t holelen = ((loff_t) bo->mem.num_pages) << PAGE_SHIFT; |
| 1541 | |
| 1542 | if (!bdev->dev_mapping) |
| 1543 | return; |
| 1544 | |
| 1545 | unmap_mapping_range(bdev->dev_mapping, offset, holelen, 1); |
| 1546 | } |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 1547 | EXPORT_SYMBOL(ttm_bo_unmap_virtual); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1548 | |
| 1549 | static void ttm_bo_vm_insert_rb(struct ttm_buffer_object *bo) |
| 1550 | { |
| 1551 | struct ttm_bo_device *bdev = bo->bdev; |
| 1552 | struct rb_node **cur = &bdev->addr_space_rb.rb_node; |
| 1553 | struct rb_node *parent = NULL; |
| 1554 | struct ttm_buffer_object *cur_bo; |
| 1555 | unsigned long offset = bo->vm_node->start; |
| 1556 | unsigned long cur_offset; |
| 1557 | |
| 1558 | while (*cur) { |
| 1559 | parent = *cur; |
| 1560 | cur_bo = rb_entry(parent, struct ttm_buffer_object, vm_rb); |
| 1561 | cur_offset = cur_bo->vm_node->start; |
| 1562 | if (offset < cur_offset) |
| 1563 | cur = &parent->rb_left; |
| 1564 | else if (offset > cur_offset) |
| 1565 | cur = &parent->rb_right; |
| 1566 | else |
| 1567 | BUG(); |
| 1568 | } |
| 1569 | |
| 1570 | rb_link_node(&bo->vm_rb, parent, cur); |
| 1571 | rb_insert_color(&bo->vm_rb, &bdev->addr_space_rb); |
| 1572 | } |
| 1573 | |
| 1574 | /** |
| 1575 | * ttm_bo_setup_vm: |
| 1576 | * |
| 1577 | * @bo: the buffer to allocate address space for |
| 1578 | * |
| 1579 | * Allocate address space in the drm device so that applications |
| 1580 | * can mmap the buffer and access the contents. This only |
| 1581 | * applies to ttm_bo_type_device objects as others are not |
| 1582 | * placed in the drm device address space. |
| 1583 | */ |
| 1584 | |
| 1585 | static int ttm_bo_setup_vm(struct ttm_buffer_object *bo) |
| 1586 | { |
| 1587 | struct ttm_bo_device *bdev = bo->bdev; |
| 1588 | int ret; |
| 1589 | |
| 1590 | retry_pre_get: |
| 1591 | ret = drm_mm_pre_get(&bdev->addr_space_mm); |
| 1592 | if (unlikely(ret != 0)) |
| 1593 | return ret; |
| 1594 | |
| 1595 | write_lock(&bdev->vm_lock); |
| 1596 | bo->vm_node = drm_mm_search_free(&bdev->addr_space_mm, |
| 1597 | bo->mem.num_pages, 0, 0); |
| 1598 | |
| 1599 | if (unlikely(bo->vm_node == NULL)) { |
| 1600 | ret = -ENOMEM; |
| 1601 | goto out_unlock; |
| 1602 | } |
| 1603 | |
| 1604 | bo->vm_node = drm_mm_get_block_atomic(bo->vm_node, |
| 1605 | bo->mem.num_pages, 0); |
| 1606 | |
| 1607 | if (unlikely(bo->vm_node == NULL)) { |
| 1608 | write_unlock(&bdev->vm_lock); |
| 1609 | goto retry_pre_get; |
| 1610 | } |
| 1611 | |
| 1612 | ttm_bo_vm_insert_rb(bo); |
| 1613 | write_unlock(&bdev->vm_lock); |
| 1614 | bo->addr_space_offset = ((uint64_t) bo->vm_node->start) << PAGE_SHIFT; |
| 1615 | |
| 1616 | return 0; |
| 1617 | out_unlock: |
| 1618 | write_unlock(&bdev->vm_lock); |
| 1619 | return ret; |
| 1620 | } |
| 1621 | |
| 1622 | int ttm_bo_wait(struct ttm_buffer_object *bo, |
| 1623 | bool lazy, bool interruptible, bool no_wait) |
| 1624 | { |
| 1625 | struct ttm_bo_driver *driver = bo->bdev->driver; |
| 1626 | void *sync_obj; |
| 1627 | void *sync_obj_arg; |
| 1628 | int ret = 0; |
| 1629 | |
| 1630 | if (likely(bo->sync_obj == NULL)) |
| 1631 | return 0; |
| 1632 | |
| 1633 | while (bo->sync_obj) { |
| 1634 | |
| 1635 | if (driver->sync_obj_signaled(bo->sync_obj, bo->sync_obj_arg)) { |
| 1636 | void *tmp_obj = bo->sync_obj; |
| 1637 | bo->sync_obj = NULL; |
| 1638 | clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags); |
| 1639 | spin_unlock(&bo->lock); |
| 1640 | driver->sync_obj_unref(&tmp_obj); |
| 1641 | spin_lock(&bo->lock); |
| 1642 | continue; |
| 1643 | } |
| 1644 | |
| 1645 | if (no_wait) |
| 1646 | return -EBUSY; |
| 1647 | |
| 1648 | sync_obj = driver->sync_obj_ref(bo->sync_obj); |
| 1649 | sync_obj_arg = bo->sync_obj_arg; |
| 1650 | spin_unlock(&bo->lock); |
| 1651 | ret = driver->sync_obj_wait(sync_obj, sync_obj_arg, |
| 1652 | lazy, interruptible); |
| 1653 | if (unlikely(ret != 0)) { |
| 1654 | driver->sync_obj_unref(&sync_obj); |
| 1655 | spin_lock(&bo->lock); |
| 1656 | return ret; |
| 1657 | } |
| 1658 | spin_lock(&bo->lock); |
| 1659 | if (likely(bo->sync_obj == sync_obj && |
| 1660 | bo->sync_obj_arg == sync_obj_arg)) { |
| 1661 | void *tmp_obj = bo->sync_obj; |
| 1662 | bo->sync_obj = NULL; |
| 1663 | clear_bit(TTM_BO_PRIV_FLAG_MOVING, |
| 1664 | &bo->priv_flags); |
| 1665 | spin_unlock(&bo->lock); |
| 1666 | driver->sync_obj_unref(&sync_obj); |
| 1667 | driver->sync_obj_unref(&tmp_obj); |
| 1668 | spin_lock(&bo->lock); |
Thomas Hellstrom | fee280d | 2009-08-03 12:39:06 +0200 | [diff] [blame] | 1669 | } else { |
| 1670 | spin_unlock(&bo->lock); |
| 1671 | driver->sync_obj_unref(&sync_obj); |
| 1672 | spin_lock(&bo->lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1673 | } |
| 1674 | } |
| 1675 | return 0; |
| 1676 | } |
| 1677 | EXPORT_SYMBOL(ttm_bo_wait); |
| 1678 | |
| 1679 | void ttm_bo_unblock_reservation(struct ttm_buffer_object *bo) |
| 1680 | { |
| 1681 | atomic_set(&bo->reserved, 0); |
| 1682 | wake_up_all(&bo->event_queue); |
| 1683 | } |
| 1684 | |
| 1685 | int ttm_bo_block_reservation(struct ttm_buffer_object *bo, bool interruptible, |
| 1686 | bool no_wait) |
| 1687 | { |
| 1688 | int ret; |
| 1689 | |
| 1690 | while (unlikely(atomic_cmpxchg(&bo->reserved, 0, 1) != 0)) { |
| 1691 | if (no_wait) |
| 1692 | return -EBUSY; |
| 1693 | else if (interruptible) { |
| 1694 | ret = wait_event_interruptible |
| 1695 | (bo->event_queue, atomic_read(&bo->reserved) == 0); |
| 1696 | if (unlikely(ret != 0)) |
| 1697 | return -ERESTART; |
| 1698 | } else { |
| 1699 | wait_event(bo->event_queue, |
| 1700 | atomic_read(&bo->reserved) == 0); |
| 1701 | } |
| 1702 | } |
| 1703 | return 0; |
| 1704 | } |
| 1705 | |
| 1706 | int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait) |
| 1707 | { |
| 1708 | int ret = 0; |
| 1709 | |
| 1710 | /* |
| 1711 | * Using ttm_bo_reserve instead of ttm_bo_block_reservation |
| 1712 | * makes sure the lru lists are updated. |
| 1713 | */ |
| 1714 | |
| 1715 | ret = ttm_bo_reserve(bo, true, no_wait, false, 0); |
| 1716 | if (unlikely(ret != 0)) |
| 1717 | return ret; |
| 1718 | spin_lock(&bo->lock); |
| 1719 | ret = ttm_bo_wait(bo, false, true, no_wait); |
| 1720 | spin_unlock(&bo->lock); |
| 1721 | if (likely(ret == 0)) |
| 1722 | atomic_inc(&bo->cpu_writers); |
| 1723 | ttm_bo_unreserve(bo); |
| 1724 | return ret; |
| 1725 | } |
| 1726 | |
| 1727 | void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo) |
| 1728 | { |
| 1729 | if (atomic_dec_and_test(&bo->cpu_writers)) |
| 1730 | wake_up_all(&bo->event_queue); |
| 1731 | } |
| 1732 | |
| 1733 | /** |
| 1734 | * A buffer object shrink method that tries to swap out the first |
| 1735 | * buffer object on the bo_global::swap_lru list. |
| 1736 | */ |
| 1737 | |
| 1738 | static int ttm_bo_swapout(struct ttm_mem_shrink *shrink) |
| 1739 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1740 | struct ttm_bo_global *glob = |
| 1741 | container_of(shrink, struct ttm_bo_global, shrink); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1742 | struct ttm_buffer_object *bo; |
| 1743 | int ret = -EBUSY; |
| 1744 | int put_count; |
| 1745 | uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM); |
| 1746 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1747 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1748 | while (ret == -EBUSY) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1749 | if (unlikely(list_empty(&glob->swap_lru))) { |
| 1750 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1751 | return -EBUSY; |
| 1752 | } |
| 1753 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1754 | bo = list_first_entry(&glob->swap_lru, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1755 | struct ttm_buffer_object, swap); |
| 1756 | kref_get(&bo->list_kref); |
| 1757 | |
| 1758 | /** |
| 1759 | * Reserve buffer. Since we unlock while sleeping, we need |
| 1760 | * to re-check that nobody removed us from the swap-list while |
| 1761 | * we slept. |
| 1762 | */ |
| 1763 | |
| 1764 | ret = ttm_bo_reserve_locked(bo, false, true, false, 0); |
| 1765 | if (unlikely(ret == -EBUSY)) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1766 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1767 | ttm_bo_wait_unreserved(bo, false); |
| 1768 | kref_put(&bo->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1769 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1770 | } |
| 1771 | } |
| 1772 | |
| 1773 | BUG_ON(ret != 0); |
| 1774 | put_count = ttm_bo_del_from_lru(bo); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1775 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1776 | |
| 1777 | while (put_count--) |
| 1778 | kref_put(&bo->list_kref, ttm_bo_ref_bug); |
| 1779 | |
| 1780 | /** |
| 1781 | * Wait for GPU, then move to system cached. |
| 1782 | */ |
| 1783 | |
| 1784 | spin_lock(&bo->lock); |
| 1785 | ret = ttm_bo_wait(bo, false, false, false); |
| 1786 | spin_unlock(&bo->lock); |
| 1787 | |
| 1788 | if (unlikely(ret != 0)) |
| 1789 | goto out; |
| 1790 | |
| 1791 | if ((bo->mem.placement & swap_placement) != swap_placement) { |
| 1792 | struct ttm_mem_reg evict_mem; |
| 1793 | |
| 1794 | evict_mem = bo->mem; |
| 1795 | evict_mem.mm_node = NULL; |
| 1796 | evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED; |
| 1797 | evict_mem.mem_type = TTM_PL_SYSTEM; |
| 1798 | |
| 1799 | ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, |
| 1800 | false, false); |
| 1801 | if (unlikely(ret != 0)) |
| 1802 | goto out; |
| 1803 | } |
| 1804 | |
| 1805 | ttm_bo_unmap_virtual(bo); |
| 1806 | |
| 1807 | /** |
| 1808 | * Swap out. Buffer will be swapped in again as soon as |
| 1809 | * anyone tries to access a ttm page. |
| 1810 | */ |
| 1811 | |
| 1812 | ret = ttm_tt_swapout(bo->ttm, bo->persistant_swap_storage); |
| 1813 | out: |
| 1814 | |
| 1815 | /** |
| 1816 | * |
| 1817 | * Unreserve without putting on LRU to avoid swapping out an |
| 1818 | * already swapped buffer. |
| 1819 | */ |
| 1820 | |
| 1821 | atomic_set(&bo->reserved, 0); |
| 1822 | wake_up_all(&bo->event_queue); |
| 1823 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 1824 | return ret; |
| 1825 | } |
| 1826 | |
| 1827 | void ttm_bo_swapout_all(struct ttm_bo_device *bdev) |
| 1828 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1829 | while (ttm_bo_swapout(&bdev->glob->shrink) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1830 | ; |
| 1831 | } |