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