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