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> |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 43 | #include <linux/reservation.h> |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 44 | |
| 45 | #define TTM_ASSERT_LOCKED(param) |
| 46 | #define TTM_DEBUG(fmt, arg...) |
| 47 | #define TTM_BO_HASH_ORDER 13 |
| 48 | |
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 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 57 | static inline int ttm_mem_type_from_place(const struct ttm_place *place, |
| 58 | uint32_t *mem_type) |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 59 | { |
| 60 | int i; |
| 61 | |
| 62 | for (i = 0; i <= TTM_PL_PRIV5; i++) |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 63 | if (place->flags & (1 << i)) { |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 64 | *mem_type = i; |
| 65 | return 0; |
| 66 | } |
| 67 | return -EINVAL; |
| 68 | } |
| 69 | |
Jerome Glisse | 5012f50 | 2009-12-10 18:07:26 +0100 | [diff] [blame] | 70 | 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] | 71 | { |
Jerome Glisse | 5012f50 | 2009-12-10 18:07:26 +0100 | [diff] [blame] | 72 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
| 73 | |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 74 | pr_err(" has_type: %d\n", man->has_type); |
| 75 | pr_err(" use_type: %d\n", man->use_type); |
| 76 | pr_err(" flags: 0x%08X\n", man->flags); |
Alex Deucher | 54c4cd6 | 2015-03-04 00:18:38 -0500 | [diff] [blame] | 77 | pr_err(" gpu_offset: 0x%08llX\n", man->gpu_offset); |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 78 | pr_err(" size: %llu\n", man->size); |
| 79 | pr_err(" available_caching: 0x%08X\n", man->available_caching); |
| 80 | pr_err(" default_caching: 0x%08X\n", man->default_caching); |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 81 | if (mem_type != TTM_PL_SYSTEM) |
| 82 | (*man->func->debug)(man, TTM_PFX); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | static void ttm_bo_mem_space_debug(struct ttm_buffer_object *bo, |
| 86 | struct ttm_placement *placement) |
| 87 | { |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 88 | int i, ret, mem_type; |
| 89 | |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 90 | pr_err("No space for %p (%lu pages, %luK, %luM)\n", |
| 91 | bo, bo->mem.num_pages, bo->mem.size >> 10, |
| 92 | bo->mem.size >> 20); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 93 | for (i = 0; i < placement->num_placement; i++) { |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 94 | ret = ttm_mem_type_from_place(&placement->placement[i], |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 95 | &mem_type); |
| 96 | if (ret) |
| 97 | return; |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 98 | pr_err(" placement[%d]=0x%08X (%d)\n", |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 99 | i, placement->placement[i].flags, mem_type); |
Jerome Glisse | 5012f50 | 2009-12-10 18:07:26 +0100 | [diff] [blame] | 100 | ttm_mem_type_debug(bo->bdev, mem_type); |
Jerome Glisse | fb53f86 | 2009-12-09 21:55:10 +0100 | [diff] [blame] | 101 | } |
| 102 | } |
| 103 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 104 | static ssize_t ttm_bo_global_show(struct kobject *kobj, |
| 105 | struct attribute *attr, |
| 106 | char *buffer) |
| 107 | { |
| 108 | struct ttm_bo_global *glob = |
| 109 | container_of(kobj, struct ttm_bo_global, kobj); |
| 110 | |
| 111 | return snprintf(buffer, PAGE_SIZE, "%lu\n", |
| 112 | (unsigned long) atomic_read(&glob->bo_count)); |
| 113 | } |
| 114 | |
| 115 | static struct attribute *ttm_bo_global_attrs[] = { |
| 116 | &ttm_bo_count, |
| 117 | NULL |
| 118 | }; |
| 119 | |
Emese Revfy | 52cf25d | 2010-01-19 02:58:23 +0100 | [diff] [blame] | 120 | static const struct sysfs_ops ttm_bo_global_ops = { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 121 | .show = &ttm_bo_global_show |
| 122 | }; |
| 123 | |
| 124 | static struct kobj_type ttm_bo_glob_kobj_type = { |
| 125 | .release = &ttm_bo_global_kobj_release, |
| 126 | .sysfs_ops = &ttm_bo_global_ops, |
| 127 | .default_attrs = ttm_bo_global_attrs |
| 128 | }; |
| 129 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 130 | |
| 131 | static inline uint32_t ttm_bo_type_flags(unsigned type) |
| 132 | { |
| 133 | return 1 << (type); |
| 134 | } |
| 135 | |
| 136 | static void ttm_bo_release_list(struct kref *list_kref) |
| 137 | { |
| 138 | struct ttm_buffer_object *bo = |
| 139 | container_of(list_kref, struct ttm_buffer_object, list_kref); |
| 140 | struct ttm_bo_device *bdev = bo->bdev; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 141 | size_t acc_size = bo->acc_size; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 142 | |
| 143 | BUG_ON(atomic_read(&bo->list_kref.refcount)); |
| 144 | BUG_ON(atomic_read(&bo->kref.refcount)); |
| 145 | BUG_ON(atomic_read(&bo->cpu_writers)); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 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); |
Thomas Hellstrom | c58f009 | 2013-11-14 10:49:05 -0800 | [diff] [blame] | 155 | mutex_destroy(&bo->wu_mutex); |
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 | 009a9da | 2013-06-27 13:48:25 +0200 | [diff] [blame] | 169 | lockdep_assert_held(&bo->resv->lock.base); |
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) { |
Rob Clark | 9ef7506 | 2014-03-12 10:59:37 -0400 | [diff] [blame] | 355 | if (bdev->driver->invalidate_caches) { |
| 356 | ret = bdev->driver->invalidate_caches(bdev, bo->mem.placement); |
| 357 | if (ret) |
| 358 | pr_err("Can not flush read caches\n"); |
| 359 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 360 | bo->evicted = false; |
| 361 | } |
| 362 | |
| 363 | if (bo->mem.mm_node) { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 364 | bo->offset = (bo->mem.start << PAGE_SHIFT) + |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 365 | bdev->man[bo->mem.mem_type].gpu_offset; |
| 366 | bo->cur_placement = bo->mem.placement; |
Thomas Hellstrom | 354fb52 | 2010-01-13 22:28:45 +0100 | [diff] [blame] | 367 | } else |
| 368 | bo->offset = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 369 | |
| 370 | return 0; |
| 371 | |
| 372 | out_err: |
| 373 | new_man = &bdev->man[bo->mem.mem_type]; |
| 374 | if ((new_man->flags & TTM_MEMTYPE_FLAG_FIXED) && bo->ttm) { |
| 375 | ttm_tt_unbind(bo->ttm); |
| 376 | ttm_tt_destroy(bo->ttm); |
| 377 | bo->ttm = NULL; |
| 378 | } |
| 379 | |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | /** |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 384 | * Call bo::reserved. |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 385 | * Will release GPU memory type usage on destruction. |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 386 | * This is the place to put in driver specific hooks to release |
| 387 | * driver private resources. |
| 388 | * Will release the bo::reserved lock. |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 389 | */ |
| 390 | |
| 391 | static void ttm_bo_cleanup_memtype_use(struct ttm_buffer_object *bo) |
| 392 | { |
Jerome Glisse | dc97b34 | 2011-11-18 11:47:03 -0500 | [diff] [blame] | 393 | if (bo->bdev->driver->move_notify) |
| 394 | bo->bdev->driver->move_notify(bo, NULL); |
| 395 | |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 396 | if (bo->ttm) { |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 397 | ttm_tt_unbind(bo->ttm); |
| 398 | ttm_tt_destroy(bo->ttm); |
| 399 | bo->ttm = NULL; |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 400 | } |
Thomas Hellstrom | 40d857b | 2010-10-19 09:01:00 +0200 | [diff] [blame] | 401 | ttm_bo_mem_put(bo, &bo->mem); |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 402 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame] | 403 | ww_mutex_unlock (&bo->resv->lock); |
Thomas Hellstrom | 1df6a2e | 2010-09-30 12:36:45 +0200 | [diff] [blame] | 404 | } |
| 405 | |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 406 | static void ttm_bo_flush_all_fences(struct ttm_buffer_object *bo) |
| 407 | { |
| 408 | struct reservation_object_list *fobj; |
| 409 | struct fence *fence; |
| 410 | int i; |
| 411 | |
| 412 | fobj = reservation_object_get_list(bo->resv); |
| 413 | fence = reservation_object_get_excl(bo->resv); |
| 414 | if (fence && !fence->ops->signaled) |
| 415 | fence_enable_sw_signaling(fence); |
| 416 | |
| 417 | for (i = 0; fobj && i < fobj->shared_count; ++i) { |
| 418 | fence = rcu_dereference_protected(fobj->shared[i], |
| 419 | reservation_object_held(bo->resv)); |
| 420 | |
| 421 | if (!fence->ops->signaled) |
| 422 | fence_enable_sw_signaling(fence); |
| 423 | } |
| 424 | } |
| 425 | |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 426 | 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] | 427 | { |
| 428 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 429 | struct ttm_bo_global *glob = bo->glob; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 430 | int put_count; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 431 | int ret; |
| 432 | |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 433 | spin_lock(&glob->lru_lock); |
Martin Kepplinger | 0eff2a2 | 2014-06-15 02:10:39 +0200 | [diff] [blame] | 434 | ret = __ttm_bo_reserve(bo, false, true, false, NULL); |
Maarten Lankhorst | 4154f05 | 2012-11-28 12:25:39 +0100 | [diff] [blame] | 435 | |
Thomas Hellstrom | 15205fb | 2013-10-10 11:09:03 -0700 | [diff] [blame] | 436 | if (!ret) { |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 437 | if (!ttm_bo_wait(bo, false, false, true)) { |
Maarten Lankhorst | dd7cfd6 | 2014-01-21 13:07:31 +0100 | [diff] [blame] | 438 | put_count = ttm_bo_del_from_lru(bo); |
| 439 | |
| 440 | spin_unlock(&glob->lru_lock); |
| 441 | ttm_bo_cleanup_memtype_use(bo); |
| 442 | |
| 443 | ttm_bo_list_ref_sub(bo, put_count, true); |
| 444 | |
| 445 | return; |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 446 | } else |
| 447 | ttm_bo_flush_all_fences(bo); |
Thomas Hellstrom | 15205fb | 2013-10-10 11:09:03 -0700 | [diff] [blame] | 448 | |
| 449 | /* |
| 450 | * Make NO_EVICT bos immediately available to |
| 451 | * shrinkers, now that they are queued for |
| 452 | * destruction. |
| 453 | */ |
| 454 | if (bo->mem.placement & TTM_PL_FLAG_NO_EVICT) { |
| 455 | bo->mem.placement &= ~TTM_PL_FLAG_NO_EVICT; |
| 456 | ttm_bo_add_to_lru(bo); |
| 457 | } |
| 458 | |
Thomas Hellstrom | c752308 | 2014-02-20 11:36:25 +0100 | [diff] [blame] | 459 | __ttm_bo_unreserve(bo); |
Thomas Hellstrom | 15205fb | 2013-10-10 11:09:03 -0700 | [diff] [blame] | 460 | } |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 461 | |
| 462 | kref_get(&bo->list_kref); |
| 463 | list_add_tail(&bo->ddestroy, &bdev->ddestroy); |
| 464 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 465 | |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 466 | schedule_delayed_work(&bdev->wq, |
| 467 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 468 | } |
| 469 | |
| 470 | /** |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 471 | * function ttm_bo_cleanup_refs_and_unlock |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 472 | * If bo idle, remove from delayed- and lru lists, and unref. |
| 473 | * If not idle, do nothing. |
| 474 | * |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 475 | * Must be called with lru_lock and reservation held, this function |
| 476 | * will drop both before returning. |
| 477 | * |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 478 | * @interruptible Any sleeps should occur interruptibly. |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 479 | * @no_wait_gpu Never wait for gpu. Return -EBUSY instead. |
| 480 | */ |
| 481 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 482 | static int ttm_bo_cleanup_refs_and_unlock(struct ttm_buffer_object *bo, |
| 483 | bool interruptible, |
| 484 | bool no_wait_gpu) |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 485 | { |
| 486 | struct ttm_bo_global *glob = bo->glob; |
| 487 | int put_count; |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 488 | int ret; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 489 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 490 | ret = ttm_bo_wait(bo, false, false, true); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 491 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 492 | if (ret && !no_wait_gpu) { |
Maarten Lankhorst | 472db7a | 2014-05-14 15:42:29 +0200 | [diff] [blame] | 493 | long lret; |
| 494 | ww_mutex_unlock(&bo->resv->lock); |
| 495 | spin_unlock(&glob->lru_lock); |
| 496 | |
| 497 | lret = reservation_object_wait_timeout_rcu(bo->resv, |
| 498 | true, |
| 499 | interruptible, |
| 500 | 30 * HZ); |
| 501 | |
| 502 | if (lret < 0) |
| 503 | return lret; |
| 504 | else if (lret == 0) |
| 505 | return -EBUSY; |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 506 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 507 | spin_lock(&glob->lru_lock); |
Martin Kepplinger | 0eff2a2 | 2014-06-15 02:10:39 +0200 | [diff] [blame] | 508 | ret = __ttm_bo_reserve(bo, false, true, false, NULL); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 509 | |
| 510 | /* |
| 511 | * We raced, and lost, someone else holds the reservation now, |
| 512 | * and is probably busy in ttm_bo_cleanup_memtype_use. |
| 513 | * |
| 514 | * Even if it's not the case, because we finished waiting any |
| 515 | * delayed destruction would succeed, so just return success |
| 516 | * here. |
| 517 | */ |
| 518 | if (ret) { |
| 519 | spin_unlock(&glob->lru_lock); |
| 520 | return 0; |
| 521 | } |
Maarten Lankhorst | 7040138 | 2014-01-21 13:07:01 +0100 | [diff] [blame] | 522 | |
| 523 | /* |
| 524 | * remove sync_obj with ttm_bo_wait, the wait should be |
| 525 | * finished, and no new wait object should have been added. |
| 526 | */ |
Maarten Lankhorst | 7040138 | 2014-01-21 13:07:01 +0100 | [diff] [blame] | 527 | ret = ttm_bo_wait(bo, false, false, true); |
| 528 | WARN_ON(ret); |
| 529 | } |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 530 | |
| 531 | if (ret || unlikely(list_empty(&bo->ddestroy))) { |
Thomas Hellstrom | c752308 | 2014-02-20 11:36:25 +0100 | [diff] [blame] | 532 | __ttm_bo_unreserve(bo); |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 533 | spin_unlock(&glob->lru_lock); |
| 534 | return ret; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 535 | } |
| 536 | |
| 537 | put_count = ttm_bo_del_from_lru(bo); |
| 538 | list_del_init(&bo->ddestroy); |
| 539 | ++put_count; |
| 540 | |
| 541 | spin_unlock(&glob->lru_lock); |
| 542 | ttm_bo_cleanup_memtype_use(bo); |
| 543 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 544 | ttm_bo_list_ref_sub(bo, put_count, true); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 545 | |
| 546 | return 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /** |
| 550 | * Traverse the delayed list, and call ttm_bo_cleanup_refs on all |
| 551 | * encountered buffers. |
| 552 | */ |
| 553 | |
| 554 | static int ttm_bo_delayed_delete(struct ttm_bo_device *bdev, bool remove_all) |
| 555 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 556 | struct ttm_bo_global *glob = bdev->glob; |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 557 | struct ttm_buffer_object *entry = NULL; |
| 558 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 559 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 560 | spin_lock(&glob->lru_lock); |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 561 | if (list_empty(&bdev->ddestroy)) |
| 562 | goto out_unlock; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 563 | |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 564 | entry = list_first_entry(&bdev->ddestroy, |
| 565 | struct ttm_buffer_object, ddestroy); |
| 566 | kref_get(&entry->list_kref); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 567 | |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 568 | for (;;) { |
| 569 | struct ttm_buffer_object *nentry = NULL; |
| 570 | |
| 571 | if (entry->ddestroy.next != &bdev->ddestroy) { |
| 572 | nentry = list_first_entry(&entry->ddestroy, |
| 573 | struct ttm_buffer_object, ddestroy); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 574 | kref_get(&nentry->list_kref); |
| 575 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 576 | |
Martin Kepplinger | 0eff2a2 | 2014-06-15 02:10:39 +0200 | [diff] [blame] | 577 | ret = __ttm_bo_reserve(entry, false, true, false, NULL); |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 578 | if (remove_all && ret) { |
| 579 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | c752308 | 2014-02-20 11:36:25 +0100 | [diff] [blame] | 580 | ret = __ttm_bo_reserve(entry, false, false, |
Martin Kepplinger | 0eff2a2 | 2014-06-15 02:10:39 +0200 | [diff] [blame] | 581 | false, NULL); |
Maarten Lankhorst | 63d0a41 | 2013-01-15 14:56:37 +0100 | [diff] [blame] | 582 | spin_lock(&glob->lru_lock); |
| 583 | } |
| 584 | |
Maarten Lankhorst | 85b144f | 2012-11-29 11:36:54 +0000 | [diff] [blame] | 585 | if (!ret) |
| 586 | ret = ttm_bo_cleanup_refs_and_unlock(entry, false, |
| 587 | !remove_all); |
| 588 | else |
| 589 | spin_unlock(&glob->lru_lock); |
| 590 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 591 | kref_put(&entry->list_kref, ttm_bo_release_list); |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 592 | entry = nentry; |
| 593 | |
| 594 | if (ret || !entry) |
| 595 | goto out; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 596 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 597 | spin_lock(&glob->lru_lock); |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 598 | if (list_empty(&entry->ddestroy)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 599 | break; |
| 600 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 601 | |
Luca Barbieri | 1a961ce | 2010-01-20 20:01:30 +0100 | [diff] [blame] | 602 | out_unlock: |
| 603 | spin_unlock(&glob->lru_lock); |
| 604 | out: |
| 605 | if (entry) |
| 606 | kref_put(&entry->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 607 | return ret; |
| 608 | } |
| 609 | |
| 610 | static void ttm_bo_delayed_workqueue(struct work_struct *work) |
| 611 | { |
| 612 | struct ttm_bo_device *bdev = |
| 613 | container_of(work, struct ttm_bo_device, wq.work); |
| 614 | |
| 615 | if (ttm_bo_delayed_delete(bdev, false)) { |
| 616 | schedule_delayed_work(&bdev->wq, |
| 617 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | static void ttm_bo_release(struct kref *kref) |
| 622 | { |
| 623 | struct ttm_buffer_object *bo = |
| 624 | container_of(kref, struct ttm_buffer_object, kref); |
| 625 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 626 | struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 627 | |
David Herrmann | 72525b3 | 2013-07-24 21:08:53 +0200 | [diff] [blame] | 628 | drm_vma_offset_remove(&bdev->vma_manager, &bo->vma_node); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 629 | ttm_mem_io_lock(man, false); |
| 630 | ttm_mem_io_free_vm(bo); |
| 631 | ttm_mem_io_unlock(man); |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 632 | ttm_bo_cleanup_refs_or_queue(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 633 | kref_put(&bo->list_kref, ttm_bo_release_list); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | void ttm_bo_unref(struct ttm_buffer_object **p_bo) |
| 637 | { |
| 638 | struct ttm_buffer_object *bo = *p_bo; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 639 | |
| 640 | *p_bo = NULL; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 641 | kref_put(&bo->kref, ttm_bo_release); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 642 | } |
| 643 | EXPORT_SYMBOL(ttm_bo_unref); |
| 644 | |
Matthew Garrett | 7c5ee53 | 2010-04-26 16:00:09 -0400 | [diff] [blame] | 645 | int ttm_bo_lock_delayed_workqueue(struct ttm_bo_device *bdev) |
| 646 | { |
| 647 | return cancel_delayed_work_sync(&bdev->wq); |
| 648 | } |
| 649 | EXPORT_SYMBOL(ttm_bo_lock_delayed_workqueue); |
| 650 | |
| 651 | void ttm_bo_unlock_delayed_workqueue(struct ttm_bo_device *bdev, int resched) |
| 652 | { |
| 653 | if (resched) |
| 654 | schedule_delayed_work(&bdev->wq, |
| 655 | ((HZ / 100) < 1) ? 1 : HZ / 100); |
| 656 | } |
| 657 | EXPORT_SYMBOL(ttm_bo_unlock_delayed_workqueue); |
| 658 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 659 | static int ttm_bo_evict(struct ttm_buffer_object *bo, bool interruptible, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 660 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 661 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 662 | struct ttm_bo_device *bdev = bo->bdev; |
| 663 | struct ttm_mem_reg evict_mem; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 664 | struct ttm_placement placement; |
| 665 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 666 | |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 667 | ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 668 | |
Thomas Hellstrom | 78ecf09 | 2009-06-17 12:29:55 +0200 | [diff] [blame] | 669 | if (unlikely(ret != 0)) { |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 670 | if (ret != -ERESTARTSYS) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 671 | pr_err("Failed to expire sync object before buffer eviction\n"); |
Thomas Hellstrom | 78ecf09 | 2009-06-17 12:29:55 +0200 | [diff] [blame] | 672 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 673 | goto out; |
| 674 | } |
| 675 | |
Maarten Lankhorst | 009a9da | 2013-06-27 13:48:25 +0200 | [diff] [blame] | 676 | lockdep_assert_held(&bo->resv->lock.base); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 677 | |
| 678 | evict_mem = bo->mem; |
| 679 | evict_mem.mm_node = NULL; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 680 | evict_mem.bus.io_reserved_vm = false; |
| 681 | evict_mem.bus.io_reserved_count = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 682 | |
Jerome Glisse | 7cb7d1d | 2009-12-09 22:14:27 +0100 | [diff] [blame] | 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, |
Michel Dänzer | e300180 | 2014-10-09 15:02:59 +0900 | [diff] [blame] | 712 | const struct ttm_place *place, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 713 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 714 | bool no_wait_gpu) |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 715 | { |
| 716 | struct ttm_bo_global *glob = bdev->glob; |
| 717 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
| 718 | struct ttm_buffer_object *bo; |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 719 | int ret = -EBUSY, put_count; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 720 | |
| 721 | spin_lock(&glob->lru_lock); |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 722 | list_for_each_entry(bo, &man->lru, lru) { |
Martin Kepplinger | 0eff2a2 | 2014-06-15 02:10:39 +0200 | [diff] [blame] | 723 | ret = __ttm_bo_reserve(bo, false, true, false, NULL); |
Michel Dänzer | e300180 | 2014-10-09 15:02:59 +0900 | [diff] [blame] | 724 | if (!ret) { |
| 725 | if (place && (place->fpfn || place->lpfn)) { |
| 726 | /* Don't evict this BO if it's outside of the |
| 727 | * requested placement range |
| 728 | */ |
| 729 | if (place->fpfn >= (bo->mem.start + bo->mem.size) || |
| 730 | (place->lpfn && place->lpfn <= bo->mem.start)) { |
| 731 | __ttm_bo_unreserve(bo); |
| 732 | ret = -EBUSY; |
| 733 | continue; |
| 734 | } |
| 735 | } |
| 736 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 737 | break; |
Michel Dänzer | e300180 | 2014-10-09 15:02:59 +0900 | [diff] [blame] | 738 | } |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 739 | } |
| 740 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 741 | if (ret) { |
| 742 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | b8e902f | 2012-10-22 12:51:26 +0000 | [diff] [blame] | 743 | return ret; |
Thomas Hellstrom | e1efc9b | 2010-10-19 09:01:01 +0200 | [diff] [blame] | 744 | } |
| 745 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 746 | kref_get(&bo->list_kref); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 747 | |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 748 | if (!list_empty(&bo->ddestroy)) { |
| 749 | ret = ttm_bo_cleanup_refs_and_unlock(bo, interruptible, |
| 750 | no_wait_gpu); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 751 | kref_put(&bo->list_kref, ttm_bo_release_list); |
Maarten Lankhorst | e7ab201 | 2012-11-28 11:25:43 +0000 | [diff] [blame] | 752 | return ret; |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | put_count = ttm_bo_del_from_lru(bo); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 756 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 757 | |
| 758 | BUG_ON(ret != 0); |
| 759 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 760 | ttm_bo_list_ref_sub(bo, put_count, true); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 761 | |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 762 | ret = ttm_bo_evict(bo, interruptible, no_wait_gpu); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 763 | ttm_bo_unreserve(bo); |
Thomas Hellstrom | 9c51ba1 | 2009-12-02 18:33:46 +0100 | [diff] [blame] | 764 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 765 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 766 | return ret; |
| 767 | } |
| 768 | |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 769 | void ttm_bo_mem_put(struct ttm_buffer_object *bo, struct ttm_mem_reg *mem) |
| 770 | { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 771 | struct ttm_mem_type_manager *man = &bo->bdev->man[mem->mem_type]; |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 772 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 773 | if (mem->mm_node) |
| 774 | (*man->func->put_node)(man, mem); |
Ben Skeggs | 42311ff | 2010-08-04 12:07:08 +1000 | [diff] [blame] | 775 | } |
| 776 | EXPORT_SYMBOL(ttm_bo_mem_put); |
| 777 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 778 | /** |
| 779 | * Repeatedly evict memory from the LRU for @mem_type until we create enough |
| 780 | * space, or we've evicted everything and there isn't enough space. |
| 781 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 782 | static int ttm_bo_mem_force_space(struct ttm_buffer_object *bo, |
| 783 | uint32_t mem_type, |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 784 | const struct ttm_place *place, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 785 | struct ttm_mem_reg *mem, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 786 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 787 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 788 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 789 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 790 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 791 | int ret; |
| 792 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 793 | do { |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 794 | ret = (*man->func->get_node)(man, bo, place, mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 795 | if (unlikely(ret != 0)) |
| 796 | return ret; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 797 | if (mem->mm_node) |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 798 | break; |
Michel Dänzer | e300180 | 2014-10-09 15:02:59 +0900 | [diff] [blame] | 799 | ret = ttm_mem_evict_first(bdev, mem_type, place, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 800 | interruptible, no_wait_gpu); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 801 | if (unlikely(ret != 0)) |
| 802 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 803 | } while (1); |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 804 | if (mem->mm_node == NULL) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 805 | return -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 806 | mem->mem_type = mem_type; |
| 807 | return 0; |
| 808 | } |
| 809 | |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 810 | static uint32_t ttm_bo_select_caching(struct ttm_mem_type_manager *man, |
| 811 | uint32_t cur_placement, |
| 812 | uint32_t proposed_placement) |
| 813 | { |
| 814 | uint32_t caching = proposed_placement & TTM_PL_MASK_CACHING; |
| 815 | uint32_t result = proposed_placement & ~TTM_PL_MASK_CACHING; |
| 816 | |
| 817 | /** |
| 818 | * Keep current caching if possible. |
| 819 | */ |
| 820 | |
| 821 | if ((cur_placement & caching) != 0) |
| 822 | result |= (cur_placement & caching); |
| 823 | else if ((man->default_caching & caching) != 0) |
| 824 | result |= man->default_caching; |
| 825 | else if ((TTM_PL_FLAG_CACHED & caching) != 0) |
| 826 | result |= TTM_PL_FLAG_CACHED; |
| 827 | else if ((TTM_PL_FLAG_WC & caching) != 0) |
| 828 | result |= TTM_PL_FLAG_WC; |
| 829 | else if ((TTM_PL_FLAG_UNCACHED & caching) != 0) |
| 830 | result |= TTM_PL_FLAG_UNCACHED; |
| 831 | |
| 832 | return result; |
| 833 | } |
| 834 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 835 | static bool ttm_bo_mt_compatible(struct ttm_mem_type_manager *man, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 836 | uint32_t mem_type, |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 837 | const struct ttm_place *place, |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 838 | uint32_t *masked_placement) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 839 | { |
| 840 | uint32_t cur_flags = ttm_bo_type_flags(mem_type); |
| 841 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 842 | if ((cur_flags & place->flags & TTM_PL_MASK_MEM) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 843 | return false; |
| 844 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 845 | if ((place->flags & man->available_caching) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 846 | return false; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 847 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 848 | cur_flags |= (place->flags & man->available_caching); |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 849 | |
| 850 | *masked_placement = cur_flags; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 851 | return true; |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * Creates space for memory region @mem according to its type. |
| 856 | * |
| 857 | * This function first searches for free space in compatible memory types in |
| 858 | * the priority order defined by the driver. If free space isn't found, then |
| 859 | * ttm_bo_mem_force_space is attempted in priority order to evict and find |
| 860 | * space. |
| 861 | */ |
| 862 | int ttm_bo_mem_space(struct ttm_buffer_object *bo, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 863 | struct ttm_placement *placement, |
| 864 | struct ttm_mem_reg *mem, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 865 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 866 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 867 | { |
| 868 | struct ttm_bo_device *bdev = bo->bdev; |
| 869 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 870 | uint32_t mem_type = TTM_PL_SYSTEM; |
| 871 | uint32_t cur_flags = 0; |
| 872 | bool type_found = false; |
| 873 | bool type_ok = false; |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 874 | bool has_erestartsys = false; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 875 | int i, ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 876 | |
| 877 | mem->mm_node = NULL; |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 878 | for (i = 0; i < placement->num_placement; ++i) { |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 879 | const struct ttm_place *place = &placement->placement[i]; |
| 880 | |
| 881 | ret = ttm_mem_type_from_place(place, &mem_type); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 882 | if (ret) |
| 883 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 884 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame^] | 885 | if (!man->has_type || !man->use_type) |
| 886 | continue; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 887 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 888 | type_ok = ttm_bo_mt_compatible(man, mem_type, place, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 889 | &cur_flags); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 890 | |
| 891 | if (!type_ok) |
| 892 | continue; |
| 893 | |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame^] | 894 | type_found = true; |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 895 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
| 896 | cur_flags); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 897 | /* |
| 898 | * Use the access and other non-mapping-related flag bits from |
| 899 | * the memory placement flags to the current flags |
| 900 | */ |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 901 | ttm_flag_masked(&cur_flags, place->flags, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 902 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 903 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 904 | if (mem_type == TTM_PL_SYSTEM) |
| 905 | break; |
| 906 | |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame^] | 907 | ret = (*man->func->get_node)(man, bo, place, mem); |
| 908 | if (unlikely(ret)) |
| 909 | return ret; |
| 910 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 911 | if (mem->mm_node) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 912 | break; |
| 913 | } |
| 914 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 915 | if ((type_ok && (mem_type == TTM_PL_SYSTEM)) || mem->mm_node) { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 916 | mem->mem_type = mem_type; |
| 917 | mem->placement = cur_flags; |
| 918 | return 0; |
| 919 | } |
| 920 | |
Dave Airlie | b663752 | 2009-12-14 14:51:35 +1000 | [diff] [blame] | 921 | for (i = 0; i < placement->num_busy_placement; ++i) { |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 922 | const struct ttm_place *place = &placement->busy_placement[i]; |
| 923 | |
| 924 | ret = ttm_mem_type_from_place(place, &mem_type); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 925 | if (ret) |
| 926 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 927 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame^] | 928 | if (!man->has_type || !man->use_type) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 929 | continue; |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 930 | if (!ttm_bo_mt_compatible(man, mem_type, place, &cur_flags)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 931 | continue; |
| 932 | |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame^] | 933 | type_found = true; |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 934 | cur_flags = ttm_bo_select_caching(man, bo->mem.placement, |
| 935 | cur_flags); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 936 | /* |
| 937 | * Use the access and other non-mapping-related flag bits from |
| 938 | * the memory placement flags to the current flags |
| 939 | */ |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 940 | ttm_flag_masked(&cur_flags, place->flags, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 941 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ae3e812 | 2009-06-24 19:57:34 +0200 | [diff] [blame] | 942 | |
Thomas Hellstrom | 0eaddb2 | 2010-01-16 16:05:04 +0100 | [diff] [blame] | 943 | if (mem_type == TTM_PL_SYSTEM) { |
| 944 | mem->mem_type = mem_type; |
| 945 | mem->placement = cur_flags; |
| 946 | mem->mm_node = NULL; |
| 947 | return 0; |
| 948 | } |
| 949 | |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 950 | ret = ttm_bo_mem_force_space(bo, mem_type, place, mem, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 951 | interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 952 | if (ret == 0 && mem->mm_node) { |
| 953 | mem->placement = cur_flags; |
| 954 | return 0; |
| 955 | } |
Thomas Hellstrom | 98ffc415 | 2009-12-07 18:36:18 +0100 | [diff] [blame] | 956 | if (ret == -ERESTARTSYS) |
| 957 | has_erestartsys = true; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 958 | } |
Thomas Hellstrom | e30f396 | 2015-09-14 01:24:41 -0700 | [diff] [blame^] | 959 | |
| 960 | if (!type_found) { |
| 961 | printk(KERN_ERR TTM_PFX "No compatible memory type found.\n"); |
| 962 | return -EINVAL; |
| 963 | } |
| 964 | |
| 965 | return (has_erestartsys) ? -ERESTARTSYS : -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 966 | } |
| 967 | EXPORT_SYMBOL(ttm_bo_mem_space); |
| 968 | |
Rashika Kheria | 6e87fa4 | 2014-01-06 22:12:58 +0530 | [diff] [blame] | 969 | static int ttm_bo_move_buffer(struct ttm_buffer_object *bo, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 970 | struct ttm_placement *placement, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 971 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 972 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 973 | { |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 974 | int ret = 0; |
| 975 | struct ttm_mem_reg mem; |
| 976 | |
Maarten Lankhorst | 009a9da | 2013-06-27 13:48:25 +0200 | [diff] [blame] | 977 | lockdep_assert_held(&bo->resv->lock.base); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 978 | |
| 979 | /* |
| 980 | * FIXME: It's possible to pipeline buffer moves. |
| 981 | * Have the driver move function wait for idle when necessary, |
| 982 | * instead of doing it here. |
| 983 | */ |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 984 | ret = ttm_bo_wait(bo, false, interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 985 | if (ret) |
| 986 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 987 | mem.num_pages = bo->num_pages; |
| 988 | mem.size = mem.num_pages << PAGE_SHIFT; |
| 989 | mem.page_alignment = bo->mem.page_alignment; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 990 | mem.bus.io_reserved_vm = false; |
| 991 | mem.bus.io_reserved_count = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 992 | /* |
| 993 | * Determine where to move the buffer. |
| 994 | */ |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 995 | ret = ttm_bo_mem_space(bo, placement, &mem, |
| 996 | interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 997 | if (ret) |
| 998 | goto out_unlock; |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 999 | ret = ttm_bo_handle_move_mem(bo, &mem, false, |
| 1000 | interruptible, no_wait_gpu); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1001 | out_unlock: |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1002 | if (ret && mem.mm_node) |
| 1003 | ttm_bo_mem_put(bo, &mem); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1004 | return ret; |
| 1005 | } |
| 1006 | |
Thomas Hellstrom | 59c8e66 | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1007 | static bool ttm_bo_mem_compat(struct ttm_placement *placement, |
| 1008 | struct ttm_mem_reg *mem, |
| 1009 | uint32_t *new_flags) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1010 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1011 | int i; |
Thomas Hellstrom | e22238e | 2010-02-12 00:18:00 +0100 | [diff] [blame] | 1012 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1013 | for (i = 0; i < placement->num_placement; i++) { |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 1014 | const struct ttm_place *heap = &placement->placement[i]; |
Michel Dänzer | 9ace2ef | 2014-10-09 15:03:03 +0900 | [diff] [blame] | 1015 | if (mem->mm_node && |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 1016 | (mem->start < heap->fpfn || |
Michel Dänzer | 9ace2ef | 2014-10-09 15:03:03 +0900 | [diff] [blame] | 1017 | (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn))) |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 1018 | continue; |
| 1019 | |
| 1020 | *new_flags = heap->flags; |
Thomas Hellstrom | 59c8e66 | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1021 | if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && |
| 1022 | (*new_flags & mem->placement & TTM_PL_MASK_MEM)) |
| 1023 | return true; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1024 | } |
Thomas Hellstrom | 59c8e66 | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1025 | |
| 1026 | for (i = 0; i < placement->num_busy_placement; i++) { |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 1027 | const struct ttm_place *heap = &placement->busy_placement[i]; |
Michel Dänzer | 9ace2ef | 2014-10-09 15:03:03 +0900 | [diff] [blame] | 1028 | if (mem->mm_node && |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 1029 | (mem->start < heap->fpfn || |
Michel Dänzer | 9ace2ef | 2014-10-09 15:03:03 +0900 | [diff] [blame] | 1030 | (heap->lpfn != 0 && (mem->start + mem->num_pages) > heap->lpfn))) |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 1031 | continue; |
| 1032 | |
| 1033 | *new_flags = heap->flags; |
Thomas Hellstrom | 59c8e66 | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1034 | if ((*new_flags & mem->placement & TTM_PL_MASK_CACHING) && |
| 1035 | (*new_flags & mem->placement & TTM_PL_MASK_MEM)) |
| 1036 | return true; |
| 1037 | } |
| 1038 | |
| 1039 | return false; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1040 | } |
| 1041 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1042 | int ttm_bo_validate(struct ttm_buffer_object *bo, |
| 1043 | struct ttm_placement *placement, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1044 | bool interruptible, |
Jerome Glisse | 9d87fa2 | 2010-04-07 10:21:19 +0000 | [diff] [blame] | 1045 | bool no_wait_gpu) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1046 | { |
| 1047 | int ret; |
Thomas Hellstrom | 59c8e66 | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1048 | uint32_t new_flags; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1049 | |
Maarten Lankhorst | 009a9da | 2013-06-27 13:48:25 +0200 | [diff] [blame] | 1050 | lockdep_assert_held(&bo->resv->lock.base); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1051 | /* |
| 1052 | * Check whether we need to move buffer. |
| 1053 | */ |
Thomas Hellstrom | 59c8e66 | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1054 | if (!ttm_bo_mem_compat(placement, &bo->mem, &new_flags)) { |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1055 | ret = ttm_bo_move_buffer(bo, placement, interruptible, |
| 1056 | no_wait_gpu); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1057 | if (ret) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1058 | return ret; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1059 | } else { |
| 1060 | /* |
| 1061 | * Use the access and other non-mapping-related flag bits from |
| 1062 | * the compatible memory placement flags to the active flags |
| 1063 | */ |
Thomas Hellstrom | 59c8e66 | 2013-10-28 02:02:19 -0700 | [diff] [blame] | 1064 | ttm_flag_masked(&bo->mem.placement, new_flags, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1065 | ~TTM_PL_MASK_MEMTYPE); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1066 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1067 | /* |
| 1068 | * We might need to add a TTM. |
| 1069 | */ |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1070 | if (bo->mem.mem_type == TTM_PL_SYSTEM && bo->ttm == NULL) { |
| 1071 | ret = ttm_bo_add_ttm(bo, true); |
| 1072 | if (ret) |
| 1073 | return ret; |
| 1074 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1075 | return 0; |
| 1076 | } |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1077 | EXPORT_SYMBOL(ttm_bo_validate); |
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 ttm_bo_init(struct ttm_bo_device *bdev, |
| 1080 | struct ttm_buffer_object *bo, |
| 1081 | unsigned long size, |
| 1082 | enum ttm_bo_type type, |
| 1083 | struct ttm_placement *placement, |
| 1084 | uint32_t page_alignment, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1085 | bool interruptible, |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1086 | struct file *persistent_swap_storage, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1087 | size_t acc_size, |
Dave Airlie | 129b78b | 2012-04-02 11:46:06 +0100 | [diff] [blame] | 1088 | struct sg_table *sg, |
Maarten Lankhorst | f4f4e3e | 2014-01-09 11:03:15 +0100 | [diff] [blame] | 1089 | struct reservation_object *resv, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1090 | void (*destroy) (struct ttm_buffer_object *)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1091 | { |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1092 | int ret = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1093 | unsigned long num_pages; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1094 | struct ttm_mem_global *mem_glob = bdev->glob->mem_glob; |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame] | 1095 | bool locked; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1096 | |
| 1097 | ret = ttm_mem_global_alloc(mem_glob, acc_size, false, false); |
| 1098 | if (ret) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1099 | pr_err("Out of kernel memory\n"); |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1100 | if (destroy) |
| 1101 | (*destroy)(bo); |
| 1102 | else |
| 1103 | kfree(bo); |
| 1104 | return -ENOMEM; |
| 1105 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1106 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1107 | num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 1108 | if (num_pages == 0) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1109 | pr_err("Illegal buffer object size\n"); |
Thomas Hellstrom | 7dfbbdc | 2010-11-09 21:31:44 +0100 | [diff] [blame] | 1110 | if (destroy) |
| 1111 | (*destroy)(bo); |
| 1112 | else |
| 1113 | kfree(bo); |
Thomas Hellstrom | a393c73 | 2012-06-12 13:28:42 +0200 | [diff] [blame] | 1114 | ttm_mem_global_free(mem_glob, acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1115 | return -EINVAL; |
| 1116 | } |
| 1117 | bo->destroy = destroy; |
| 1118 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1119 | kref_init(&bo->kref); |
| 1120 | kref_init(&bo->list_kref); |
| 1121 | atomic_set(&bo->cpu_writers, 0); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1122 | INIT_LIST_HEAD(&bo->lru); |
| 1123 | INIT_LIST_HEAD(&bo->ddestroy); |
| 1124 | INIT_LIST_HEAD(&bo->swap); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1125 | INIT_LIST_HEAD(&bo->io_reserve_lru); |
Thomas Hellstrom | c58f009 | 2013-11-14 10:49:05 -0800 | [diff] [blame] | 1126 | mutex_init(&bo->wu_mutex); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1127 | bo->bdev = bdev; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1128 | bo->glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1129 | bo->type = type; |
| 1130 | bo->num_pages = num_pages; |
Jerome Glisse | eb6d2c3 | 2009-12-10 16:15:52 +0100 | [diff] [blame] | 1131 | bo->mem.size = num_pages << PAGE_SHIFT; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1132 | bo->mem.mem_type = TTM_PL_SYSTEM; |
| 1133 | bo->mem.num_pages = bo->num_pages; |
| 1134 | bo->mem.mm_node = NULL; |
| 1135 | bo->mem.page_alignment = page_alignment; |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1136 | bo->mem.bus.io_reserved_vm = false; |
| 1137 | bo->mem.bus.io_reserved_count = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1138 | bo->priv_flags = 0; |
| 1139 | bo->mem.placement = (TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED); |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1140 | bo->persistent_swap_storage = persistent_swap_storage; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1141 | bo->acc_size = acc_size; |
Dave Airlie | 129b78b | 2012-04-02 11:46:06 +0100 | [diff] [blame] | 1142 | bo->sg = sg; |
Maarten Lankhorst | f4f4e3e | 2014-01-09 11:03:15 +0100 | [diff] [blame] | 1143 | if (resv) { |
| 1144 | bo->resv = resv; |
| 1145 | lockdep_assert_held(&bo->resv->lock.base); |
| 1146 | } else { |
| 1147 | bo->resv = &bo->ttm_resv; |
| 1148 | reservation_object_init(&bo->ttm_resv); |
| 1149 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1150 | atomic_inc(&bo->glob->bo_count); |
David Herrmann | 72525b3 | 2013-07-24 21:08:53 +0200 | [diff] [blame] | 1151 | drm_vma_node_reset(&bo->vma_node); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1152 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1153 | /* |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1154 | * For ttm_bo_type_device buffers, allocate |
| 1155 | * address space from the device. |
| 1156 | */ |
Christian König | f1217ed | 2014-08-27 13:16:04 +0200 | [diff] [blame] | 1157 | if (bo->type == ttm_bo_type_device || |
| 1158 | bo->type == ttm_bo_type_sg) |
David Herrmann | abf1903 | 2013-07-25 14:08:51 +0200 | [diff] [blame] | 1159 | ret = drm_vma_offset_add(&bdev->vma_manager, &bo->vma_node, |
| 1160 | bo->mem.num_pages); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1161 | |
Maarten Lankhorst | f4f4e3e | 2014-01-09 11:03:15 +0100 | [diff] [blame] | 1162 | /* passed reservation objects should already be locked, |
| 1163 | * since otherwise lockdep will be angered in radeon. |
| 1164 | */ |
| 1165 | if (!resv) { |
| 1166 | locked = ww_mutex_trylock(&bo->resv->lock); |
| 1167 | WARN_ON(!locked); |
| 1168 | } |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame] | 1169 | |
| 1170 | if (likely(!ret)) |
| 1171 | ret = ttm_bo_validate(bo, placement, interruptible, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1172 | |
Maarten Lankhorst | f4f4e3e | 2014-01-09 11:03:15 +0100 | [diff] [blame] | 1173 | if (!resv) |
| 1174 | ttm_bo_unreserve(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1175 | |
Maarten Lankhorst | 5e33840 | 2013-06-27 13:48:19 +0200 | [diff] [blame] | 1176 | if (unlikely(ret)) |
| 1177 | ttm_bo_unref(&bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1178 | |
| 1179 | return ret; |
| 1180 | } |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1181 | EXPORT_SYMBOL(ttm_bo_init); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1182 | |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1183 | size_t ttm_bo_acc_size(struct ttm_bo_device *bdev, |
| 1184 | unsigned long bo_size, |
| 1185 | unsigned struct_size) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1186 | { |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1187 | unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; |
| 1188 | size_t size = 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1189 | |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1190 | size += ttm_round_pot(struct_size); |
| 1191 | size += PAGE_ALIGN(npages * sizeof(void *)); |
| 1192 | size += ttm_round_pot(sizeof(struct ttm_tt)); |
| 1193 | return size; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1194 | } |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1195 | EXPORT_SYMBOL(ttm_bo_acc_size); |
| 1196 | |
| 1197 | size_t ttm_bo_dma_acc_size(struct ttm_bo_device *bdev, |
| 1198 | unsigned long bo_size, |
| 1199 | unsigned struct_size) |
| 1200 | { |
| 1201 | unsigned npages = (PAGE_ALIGN(bo_size)) >> PAGE_SHIFT; |
| 1202 | size_t size = 0; |
| 1203 | |
| 1204 | size += ttm_round_pot(struct_size); |
| 1205 | size += PAGE_ALIGN(npages * sizeof(void *)); |
| 1206 | size += PAGE_ALIGN(npages * sizeof(dma_addr_t)); |
| 1207 | size += ttm_round_pot(sizeof(struct ttm_dma_tt)); |
| 1208 | return size; |
| 1209 | } |
| 1210 | EXPORT_SYMBOL(ttm_bo_dma_acc_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1211 | |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1212 | int ttm_bo_create(struct ttm_bo_device *bdev, |
| 1213 | unsigned long size, |
| 1214 | enum ttm_bo_type type, |
| 1215 | struct ttm_placement *placement, |
| 1216 | uint32_t page_alignment, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1217 | bool interruptible, |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1218 | struct file *persistent_swap_storage, |
Jerome Glisse | 09855ac | 2009-12-10 17:16:27 +0100 | [diff] [blame] | 1219 | struct ttm_buffer_object **p_bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1220 | { |
| 1221 | struct ttm_buffer_object *bo; |
Jerome Glisse | 57de4ba | 2011-11-11 15:42:57 -0500 | [diff] [blame] | 1222 | size_t acc_size; |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1223 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1224 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1225 | bo = kzalloc(sizeof(*bo), GFP_KERNEL); |
Thomas Hellstrom | a393c73 | 2012-06-12 13:28:42 +0200 | [diff] [blame] | 1226 | if (unlikely(bo == NULL)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1227 | return -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1228 | |
Thomas Hellstrom | a393c73 | 2012-06-12 13:28:42 +0200 | [diff] [blame] | 1229 | 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] | 1230 | ret = ttm_bo_init(bdev, bo, size, type, placement, page_alignment, |
Marcin Slusarz | 0b91c4a | 2012-11-06 21:49:51 +0000 | [diff] [blame] | 1231 | interruptible, persistent_swap_storage, acc_size, |
Maarten Lankhorst | f4f4e3e | 2014-01-09 11:03:15 +0100 | [diff] [blame] | 1232 | NULL, NULL, NULL); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1233 | if (likely(ret == 0)) |
| 1234 | *p_bo = bo; |
| 1235 | |
| 1236 | return ret; |
| 1237 | } |
Thomas Hellstrom | 4d79893 | 2011-10-04 20:13:11 +0200 | [diff] [blame] | 1238 | EXPORT_SYMBOL(ttm_bo_create); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1239 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1240 | static int ttm_bo_force_list_clean(struct ttm_bo_device *bdev, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1241 | unsigned mem_type, bool allow_errors) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1242 | { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1243 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1244 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1245 | int ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1246 | |
| 1247 | /* |
| 1248 | * Can't use standard list traversal since we're unlocking. |
| 1249 | */ |
| 1250 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1251 | spin_lock(&glob->lru_lock); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1252 | while (!list_empty(&man->lru)) { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1253 | spin_unlock(&glob->lru_lock); |
Michel Dänzer | e300180 | 2014-10-09 15:02:59 +0900 | [diff] [blame] | 1254 | ret = ttm_mem_evict_first(bdev, mem_type, NULL, false, false); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1255 | if (ret) { |
| 1256 | if (allow_errors) { |
| 1257 | return ret; |
| 1258 | } else { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1259 | pr_err("Cleanup eviction failed\n"); |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1260 | } |
| 1261 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1262 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1263 | } |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1264 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1265 | return 0; |
| 1266 | } |
| 1267 | |
| 1268 | int ttm_bo_clean_mm(struct ttm_bo_device *bdev, unsigned mem_type) |
| 1269 | { |
Roel Kluin | c96e7c7 | 2009-08-03 14:22:53 +0200 | [diff] [blame] | 1270 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1271 | int ret = -EINVAL; |
| 1272 | |
| 1273 | if (mem_type >= TTM_NUM_MEM_TYPES) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1274 | pr_err("Illegal memory type %d\n", mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1275 | return ret; |
| 1276 | } |
Roel Kluin | c96e7c7 | 2009-08-03 14:22:53 +0200 | [diff] [blame] | 1277 | man = &bdev->man[mem_type]; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1278 | |
| 1279 | if (!man->has_type) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1280 | pr_err("Trying to take down uninitialized memory manager type %u\n", |
| 1281 | mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1282 | return ret; |
| 1283 | } |
| 1284 | |
| 1285 | man->use_type = false; |
| 1286 | man->has_type = false; |
| 1287 | |
| 1288 | ret = 0; |
| 1289 | if (mem_type > 0) { |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1290 | ttm_bo_force_list_clean(bdev, mem_type, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1291 | |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1292 | ret = (*man->func->takedown)(man); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | return ret; |
| 1296 | } |
| 1297 | EXPORT_SYMBOL(ttm_bo_clean_mm); |
| 1298 | |
| 1299 | int ttm_bo_evict_mm(struct ttm_bo_device *bdev, unsigned mem_type) |
| 1300 | { |
| 1301 | struct ttm_mem_type_manager *man = &bdev->man[mem_type]; |
| 1302 | |
| 1303 | if (mem_type == 0 || mem_type >= TTM_NUM_MEM_TYPES) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1304 | pr_err("Illegal memory manager memory type %u\n", mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1305 | return -EINVAL; |
| 1306 | } |
| 1307 | |
| 1308 | if (!man->has_type) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1309 | pr_err("Memory type %u has not been initialized\n", mem_type); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1310 | return 0; |
| 1311 | } |
| 1312 | |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1313 | return ttm_bo_force_list_clean(bdev, mem_type, true); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1314 | } |
| 1315 | EXPORT_SYMBOL(ttm_bo_evict_mm); |
| 1316 | |
| 1317 | int ttm_bo_init_mm(struct ttm_bo_device *bdev, unsigned type, |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1318 | unsigned long p_size) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1319 | { |
| 1320 | int ret = -EINVAL; |
| 1321 | struct ttm_mem_type_manager *man; |
| 1322 | |
Thomas Hellstrom | dbc4a5b | 2010-10-29 10:46:47 +0200 | [diff] [blame] | 1323 | BUG_ON(type >= TTM_NUM_MEM_TYPES); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1324 | man = &bdev->man[type]; |
Thomas Hellstrom | dbc4a5b | 2010-10-29 10:46:47 +0200 | [diff] [blame] | 1325 | BUG_ON(man->has_type); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1326 | man->io_reserve_fastpath = true; |
| 1327 | man->use_io_reserve_lru = false; |
| 1328 | mutex_init(&man->io_reserve_mutex); |
| 1329 | INIT_LIST_HEAD(&man->io_reserve_lru); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1330 | |
| 1331 | ret = bdev->driver->init_mem_type(bdev, type, man); |
| 1332 | if (ret) |
| 1333 | return ret; |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1334 | man->bdev = bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1335 | |
| 1336 | ret = 0; |
| 1337 | if (type != TTM_PL_SYSTEM) { |
Ben Skeggs | d961db7 | 2010-08-05 10:48:18 +1000 | [diff] [blame] | 1338 | ret = (*man->func->init)(man, p_size); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1339 | if (ret) |
| 1340 | return ret; |
| 1341 | } |
| 1342 | man->has_type = true; |
| 1343 | man->use_type = true; |
| 1344 | man->size = p_size; |
| 1345 | |
| 1346 | INIT_LIST_HEAD(&man->lru); |
| 1347 | |
| 1348 | return 0; |
| 1349 | } |
| 1350 | EXPORT_SYMBOL(ttm_bo_init_mm); |
| 1351 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1352 | static void ttm_bo_global_kobj_release(struct kobject *kobj) |
| 1353 | { |
| 1354 | struct ttm_bo_global *glob = |
| 1355 | container_of(kobj, struct ttm_bo_global, kobj); |
| 1356 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1357 | ttm_mem_unregister_shrink(glob->mem_glob, &glob->shrink); |
| 1358 | __free_page(glob->dummy_read_page); |
| 1359 | kfree(glob); |
| 1360 | } |
| 1361 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 1362 | void ttm_bo_global_release(struct drm_global_reference *ref) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1363 | { |
| 1364 | struct ttm_bo_global *glob = ref->object; |
| 1365 | |
| 1366 | kobject_del(&glob->kobj); |
| 1367 | kobject_put(&glob->kobj); |
| 1368 | } |
| 1369 | EXPORT_SYMBOL(ttm_bo_global_release); |
| 1370 | |
Dave Airlie | ba4420c | 2010-03-09 10:56:52 +1000 | [diff] [blame] | 1371 | int ttm_bo_global_init(struct drm_global_reference *ref) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1372 | { |
| 1373 | struct ttm_bo_global_ref *bo_ref = |
| 1374 | container_of(ref, struct ttm_bo_global_ref, ref); |
| 1375 | struct ttm_bo_global *glob = ref->object; |
| 1376 | int ret; |
| 1377 | |
| 1378 | mutex_init(&glob->device_list_mutex); |
| 1379 | spin_lock_init(&glob->lru_lock); |
| 1380 | glob->mem_glob = bo_ref->mem_glob; |
| 1381 | glob->dummy_read_page = alloc_page(__GFP_ZERO | GFP_DMA32); |
| 1382 | |
| 1383 | if (unlikely(glob->dummy_read_page == NULL)) { |
| 1384 | ret = -ENOMEM; |
| 1385 | goto out_no_drp; |
| 1386 | } |
| 1387 | |
| 1388 | INIT_LIST_HEAD(&glob->swap_lru); |
| 1389 | INIT_LIST_HEAD(&glob->device_list); |
| 1390 | |
| 1391 | ttm_mem_init_shrink(&glob->shrink, ttm_bo_swapout); |
| 1392 | ret = ttm_mem_register_shrink(glob->mem_glob, &glob->shrink); |
| 1393 | if (unlikely(ret != 0)) { |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1394 | pr_err("Could not register buffer object swapout\n"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1395 | goto out_no_shrink; |
| 1396 | } |
| 1397 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1398 | atomic_set(&glob->bo_count, 0); |
| 1399 | |
Robert P. J. Day | b642ed0 | 2010-03-13 10:36:32 +0000 | [diff] [blame] | 1400 | ret = kobject_init_and_add( |
| 1401 | &glob->kobj, &ttm_bo_glob_kobj_type, ttm_get_kobj(), "buffer_objects"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1402 | if (unlikely(ret != 0)) |
| 1403 | kobject_put(&glob->kobj); |
| 1404 | return ret; |
| 1405 | out_no_shrink: |
| 1406 | __free_page(glob->dummy_read_page); |
| 1407 | out_no_drp: |
| 1408 | kfree(glob); |
| 1409 | return ret; |
| 1410 | } |
| 1411 | EXPORT_SYMBOL(ttm_bo_global_init); |
| 1412 | |
| 1413 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1414 | int ttm_bo_device_release(struct ttm_bo_device *bdev) |
| 1415 | { |
| 1416 | int ret = 0; |
| 1417 | unsigned i = TTM_NUM_MEM_TYPES; |
| 1418 | struct ttm_mem_type_manager *man; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1419 | struct ttm_bo_global *glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1420 | |
| 1421 | while (i--) { |
| 1422 | man = &bdev->man[i]; |
| 1423 | if (man->has_type) { |
| 1424 | man->use_type = false; |
| 1425 | if ((i != TTM_PL_SYSTEM) && ttm_bo_clean_mm(bdev, i)) { |
| 1426 | ret = -EBUSY; |
Joe Perches | 25d0479 | 2012-03-16 21:43:50 -0700 | [diff] [blame] | 1427 | pr_err("DRM memory manager type %d is not clean\n", |
| 1428 | i); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1429 | } |
| 1430 | man->has_type = false; |
| 1431 | } |
| 1432 | } |
| 1433 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1434 | mutex_lock(&glob->device_list_mutex); |
| 1435 | list_del(&bdev->device_list); |
| 1436 | mutex_unlock(&glob->device_list_mutex); |
| 1437 | |
Tejun Heo | f094cfc | 2010-12-24 15:59:06 +0100 | [diff] [blame] | 1438 | cancel_delayed_work_sync(&bdev->wq); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1439 | |
| 1440 | while (ttm_bo_delayed_delete(bdev, true)) |
| 1441 | ; |
| 1442 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1443 | spin_lock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1444 | if (list_empty(&bdev->ddestroy)) |
| 1445 | TTM_DEBUG("Delayed destroy list was clean\n"); |
| 1446 | |
| 1447 | if (list_empty(&bdev->man[0].lru)) |
| 1448 | TTM_DEBUG("Swap list was clean\n"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1449 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1450 | |
David Herrmann | 72525b3 | 2013-07-24 21:08:53 +0200 | [diff] [blame] | 1451 | drm_vma_offset_manager_destroy(&bdev->vma_manager); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1452 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1453 | return ret; |
| 1454 | } |
| 1455 | EXPORT_SYMBOL(ttm_bo_device_release); |
| 1456 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1457 | int ttm_bo_device_init(struct ttm_bo_device *bdev, |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1458 | struct ttm_bo_global *glob, |
| 1459 | struct ttm_bo_driver *driver, |
David Herrmann | 44d847b | 2013-08-13 19:10:30 +0200 | [diff] [blame] | 1460 | struct address_space *mapping, |
Dave Airlie | 51c8b40 | 2009-08-20 13:38:04 +1000 | [diff] [blame] | 1461 | uint64_t file_page_offset, |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 1462 | bool need_dma32) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1463 | { |
| 1464 | int ret = -EINVAL; |
| 1465 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1466 | bdev->driver = driver; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1467 | |
| 1468 | memset(bdev->man, 0, sizeof(bdev->man)); |
| 1469 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1470 | /* |
| 1471 | * Initialize the system memory buffer type. |
| 1472 | * Other types need to be driver / IOCTL initialized. |
| 1473 | */ |
Jerome Glisse | ca262a999 | 2009-12-08 15:33:32 +0100 | [diff] [blame] | 1474 | ret = ttm_bo_init_mm(bdev, TTM_PL_SYSTEM, 0); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1475 | if (unlikely(ret != 0)) |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1476 | goto out_no_sys; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1477 | |
David Herrmann | 72525b3 | 2013-07-24 21:08:53 +0200 | [diff] [blame] | 1478 | drm_vma_offset_manager_init(&bdev->vma_manager, file_page_offset, |
| 1479 | 0x10000000); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1480 | INIT_DELAYED_WORK(&bdev->wq, ttm_bo_delayed_workqueue); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1481 | INIT_LIST_HEAD(&bdev->ddestroy); |
David Herrmann | 44d847b | 2013-08-13 19:10:30 +0200 | [diff] [blame] | 1482 | bdev->dev_mapping = mapping; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1483 | bdev->glob = glob; |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 1484 | bdev->need_dma32 = need_dma32; |
Thomas Hellstrom | 6570596 | 2010-11-17 12:28:31 +0000 | [diff] [blame] | 1485 | bdev->val_seq = 0; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1486 | mutex_lock(&glob->device_list_mutex); |
| 1487 | list_add_tail(&bdev->device_list, &glob->device_list); |
| 1488 | mutex_unlock(&glob->device_list_mutex); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1489 | |
| 1490 | return 0; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1491 | out_no_sys: |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1492 | return ret; |
| 1493 | } |
| 1494 | EXPORT_SYMBOL(ttm_bo_device_init); |
| 1495 | |
| 1496 | /* |
| 1497 | * buffer object vm functions. |
| 1498 | */ |
| 1499 | |
| 1500 | bool ttm_mem_reg_is_pci(struct ttm_bo_device *bdev, struct ttm_mem_reg *mem) |
| 1501 | { |
| 1502 | struct ttm_mem_type_manager *man = &bdev->man[mem->mem_type]; |
| 1503 | |
| 1504 | if (!(man->flags & TTM_MEMTYPE_FLAG_FIXED)) { |
| 1505 | if (mem->mem_type == TTM_PL_SYSTEM) |
| 1506 | return false; |
| 1507 | |
| 1508 | if (man->flags & TTM_MEMTYPE_FLAG_CMA) |
| 1509 | return false; |
| 1510 | |
| 1511 | if (mem->placement & TTM_PL_FLAG_CACHED) |
| 1512 | return false; |
| 1513 | } |
| 1514 | return true; |
| 1515 | } |
| 1516 | |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1517 | void ttm_bo_unmap_virtual_locked(struct ttm_buffer_object *bo) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1518 | { |
| 1519 | struct ttm_bo_device *bdev = bo->bdev; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1520 | |
David Herrmann | 51335df | 2013-07-24 21:10:03 +0200 | [diff] [blame] | 1521 | drm_vma_node_unmap(&bo->vma_node, bdev->dev_mapping); |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1522 | ttm_mem_io_free_vm(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1523 | } |
Thomas Hellstrom | eba6709 | 2010-11-11 09:41:57 +0100 | [diff] [blame] | 1524 | |
| 1525 | void ttm_bo_unmap_virtual(struct ttm_buffer_object *bo) |
| 1526 | { |
| 1527 | struct ttm_bo_device *bdev = bo->bdev; |
| 1528 | struct ttm_mem_type_manager *man = &bdev->man[bo->mem.mem_type]; |
| 1529 | |
| 1530 | ttm_mem_io_lock(man, false); |
| 1531 | ttm_bo_unmap_virtual_locked(bo); |
| 1532 | ttm_mem_io_unlock(man); |
| 1533 | } |
| 1534 | |
| 1535 | |
Dave Airlie | e024e11 | 2009-06-24 09:48:08 +1000 | [diff] [blame] | 1536 | EXPORT_SYMBOL(ttm_bo_unmap_virtual); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1537 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1538 | int ttm_bo_wait(struct ttm_buffer_object *bo, |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1539 | bool lazy, bool interruptible, bool no_wait) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1540 | { |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 1541 | struct reservation_object_list *fobj; |
| 1542 | struct reservation_object *resv; |
| 1543 | struct fence *excl; |
| 1544 | long timeout = 15 * HZ; |
| 1545 | int i; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1546 | |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 1547 | resv = bo->resv; |
| 1548 | fobj = reservation_object_get_list(resv); |
| 1549 | excl = reservation_object_get_excl(resv); |
| 1550 | if (excl) { |
| 1551 | if (!fence_is_signaled(excl)) { |
| 1552 | if (no_wait) |
| 1553 | return -EBUSY; |
Maarten Lankhorst | 7040138 | 2014-01-21 13:07:01 +0100 | [diff] [blame] | 1554 | |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 1555 | timeout = fence_wait_timeout(excl, |
| 1556 | interruptible, timeout); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1557 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1558 | } |
Maarten Lankhorst | f2c24b8 | 2014-04-02 17:14:48 +0200 | [diff] [blame] | 1559 | |
| 1560 | for (i = 0; fobj && timeout > 0 && i < fobj->shared_count; ++i) { |
| 1561 | struct fence *fence; |
| 1562 | fence = rcu_dereference_protected(fobj->shared[i], |
| 1563 | reservation_object_held(resv)); |
| 1564 | |
| 1565 | if (!fence_is_signaled(fence)) { |
| 1566 | if (no_wait) |
| 1567 | return -EBUSY; |
| 1568 | |
| 1569 | timeout = fence_wait_timeout(fence, |
| 1570 | interruptible, timeout); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | if (timeout < 0) |
| 1575 | return timeout; |
| 1576 | |
| 1577 | if (timeout == 0) |
| 1578 | return -EBUSY; |
| 1579 | |
| 1580 | reservation_object_add_excl_fence(resv, NULL); |
| 1581 | clear_bit(TTM_BO_PRIV_FLAG_MOVING, &bo->priv_flags); |
| 1582 | return 0; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1583 | } |
| 1584 | EXPORT_SYMBOL(ttm_bo_wait); |
| 1585 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1586 | int ttm_bo_synccpu_write_grab(struct ttm_buffer_object *bo, bool no_wait) |
| 1587 | { |
| 1588 | int ret = 0; |
| 1589 | |
| 1590 | /* |
Thomas Hellstrom | 8cfe92d | 2010-04-28 11:33:25 +0200 | [diff] [blame] | 1591 | * Using ttm_bo_reserve makes sure the lru lists are updated. |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1592 | */ |
| 1593 | |
Martin Kepplinger | 0eff2a2 | 2014-06-15 02:10:39 +0200 | [diff] [blame] | 1594 | ret = ttm_bo_reserve(bo, true, no_wait, false, NULL); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1595 | if (unlikely(ret != 0)) |
| 1596 | return ret; |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1597 | ret = ttm_bo_wait(bo, false, true, no_wait); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1598 | if (likely(ret == 0)) |
| 1599 | atomic_inc(&bo->cpu_writers); |
| 1600 | ttm_bo_unreserve(bo); |
| 1601 | return ret; |
| 1602 | } |
Ben Skeggs | d1ede14 | 2009-12-11 15:13:00 +1000 | [diff] [blame] | 1603 | EXPORT_SYMBOL(ttm_bo_synccpu_write_grab); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1604 | |
| 1605 | void ttm_bo_synccpu_write_release(struct ttm_buffer_object *bo) |
| 1606 | { |
Maarten Lankhorst | 654aa792 | 2012-11-06 14:39:43 +0100 | [diff] [blame] | 1607 | atomic_dec(&bo->cpu_writers); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1608 | } |
Ben Skeggs | d1ede14 | 2009-12-11 15:13:00 +1000 | [diff] [blame] | 1609 | EXPORT_SYMBOL(ttm_bo_synccpu_write_release); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1610 | |
| 1611 | /** |
| 1612 | * A buffer object shrink method that tries to swap out the first |
| 1613 | * buffer object on the bo_global::swap_lru list. |
| 1614 | */ |
| 1615 | |
| 1616 | static int ttm_bo_swapout(struct ttm_mem_shrink *shrink) |
| 1617 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1618 | struct ttm_bo_global *glob = |
| 1619 | container_of(shrink, struct ttm_bo_global, shrink); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1620 | struct ttm_buffer_object *bo; |
| 1621 | int ret = -EBUSY; |
| 1622 | int put_count; |
| 1623 | uint32_t swap_placement = (TTM_PL_FLAG_CACHED | TTM_PL_FLAG_SYSTEM); |
| 1624 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1625 | spin_lock(&glob->lru_lock); |
Maarten Lankhorst | 2b7b3ad | 2012-11-28 11:25:42 +0000 | [diff] [blame] | 1626 | list_for_each_entry(bo, &glob->swap_lru, swap) { |
Martin Kepplinger | 0eff2a2 | 2014-06-15 02:10:39 +0200 | [diff] [blame] | 1627 | ret = __ttm_bo_reserve(bo, false, true, false, NULL); |
Maarten Lankhorst | 2b7b3ad | 2012-11-28 11:25:42 +0000 | [diff] [blame] | 1628 | if (!ret) |
| 1629 | break; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1630 | } |
| 1631 | |
Maarten Lankhorst | 2b7b3ad | 2012-11-28 11:25:42 +0000 | [diff] [blame] | 1632 | if (ret) { |
| 1633 | spin_unlock(&glob->lru_lock); |
| 1634 | return ret; |
| 1635 | } |
| 1636 | |
| 1637 | kref_get(&bo->list_kref); |
| 1638 | |
| 1639 | if (!list_empty(&bo->ddestroy)) { |
| 1640 | ret = ttm_bo_cleanup_refs_and_unlock(bo, false, false); |
| 1641 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 1642 | return ret; |
| 1643 | } |
| 1644 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1645 | put_count = ttm_bo_del_from_lru(bo); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1646 | spin_unlock(&glob->lru_lock); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1647 | |
Dave Airlie | d6ea888 | 2010-11-22 13:24:40 +1000 | [diff] [blame] | 1648 | ttm_bo_list_ref_sub(bo, put_count, true); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1649 | |
| 1650 | /** |
| 1651 | * Wait for GPU, then move to system cached. |
| 1652 | */ |
| 1653 | |
Dave Airlie | 1717c0e | 2011-10-27 18:28:37 +0200 | [diff] [blame] | 1654 | ret = ttm_bo_wait(bo, false, false, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1655 | |
| 1656 | if (unlikely(ret != 0)) |
| 1657 | goto out; |
| 1658 | |
| 1659 | if ((bo->mem.placement & swap_placement) != swap_placement) { |
| 1660 | struct ttm_mem_reg evict_mem; |
| 1661 | |
| 1662 | evict_mem = bo->mem; |
| 1663 | evict_mem.mm_node = NULL; |
| 1664 | evict_mem.placement = TTM_PL_FLAG_SYSTEM | TTM_PL_FLAG_CACHED; |
| 1665 | evict_mem.mem_type = TTM_PL_SYSTEM; |
| 1666 | |
| 1667 | ret = ttm_bo_handle_move_mem(bo, &evict_mem, true, |
Maarten Lankhorst | 97a875c | 2012-11-28 11:25:44 +0000 | [diff] [blame] | 1668 | false, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1669 | if (unlikely(ret != 0)) |
| 1670 | goto out; |
| 1671 | } |
| 1672 | |
| 1673 | ttm_bo_unmap_virtual(bo); |
| 1674 | |
| 1675 | /** |
| 1676 | * Swap out. Buffer will be swapped in again as soon as |
| 1677 | * anyone tries to access a ttm page. |
| 1678 | */ |
| 1679 | |
Thomas Hellstrom | 3f09ea4 | 2010-01-13 22:28:40 +0100 | [diff] [blame] | 1680 | if (bo->bdev->driver->swap_notify) |
| 1681 | bo->bdev->driver->swap_notify(bo); |
| 1682 | |
Jan Engelhardt | 5df2397 | 2011-04-04 01:25:18 +0200 | [diff] [blame] | 1683 | ret = ttm_tt_swapout(bo->ttm, bo->persistent_swap_storage); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1684 | out: |
| 1685 | |
| 1686 | /** |
| 1687 | * |
| 1688 | * Unreserve without putting on LRU to avoid swapping out an |
| 1689 | * already swapped buffer. |
| 1690 | */ |
| 1691 | |
Thomas Hellstrom | c752308 | 2014-02-20 11:36:25 +0100 | [diff] [blame] | 1692 | __ttm_bo_unreserve(bo); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1693 | kref_put(&bo->list_kref, ttm_bo_release_list); |
| 1694 | return ret; |
| 1695 | } |
| 1696 | |
| 1697 | void ttm_bo_swapout_all(struct ttm_bo_device *bdev) |
| 1698 | { |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 1699 | while (ttm_bo_swapout(&bdev->glob->shrink) == 0) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 1700 | ; |
| 1701 | } |
Thomas Hellstrom | e99e1e7 | 2010-01-13 22:28:42 +0100 | [diff] [blame] | 1702 | EXPORT_SYMBOL(ttm_bo_swapout_all); |
Thomas Hellstrom | c58f009 | 2013-11-14 10:49:05 -0800 | [diff] [blame] | 1703 | |
| 1704 | /** |
| 1705 | * ttm_bo_wait_unreserved - interruptible wait for a buffer object to become |
| 1706 | * unreserved |
| 1707 | * |
| 1708 | * @bo: Pointer to buffer |
| 1709 | */ |
| 1710 | int ttm_bo_wait_unreserved(struct ttm_buffer_object *bo) |
| 1711 | { |
| 1712 | int ret; |
| 1713 | |
| 1714 | /* |
| 1715 | * In the absense of a wait_unlocked API, |
| 1716 | * Use the bo::wu_mutex to avoid triggering livelocks due to |
| 1717 | * concurrent use of this function. Note that this use of |
| 1718 | * bo::wu_mutex can go away if we change locking order to |
| 1719 | * mmap_sem -> bo::reserve. |
| 1720 | */ |
| 1721 | ret = mutex_lock_interruptible(&bo->wu_mutex); |
| 1722 | if (unlikely(ret != 0)) |
| 1723 | return -ERESTARTSYS; |
| 1724 | if (!ww_mutex_is_locked(&bo->resv->lock)) |
| 1725 | goto out_unlock; |
Thomas Hellstrom | c752308 | 2014-02-20 11:36:25 +0100 | [diff] [blame] | 1726 | ret = __ttm_bo_reserve(bo, true, false, false, NULL); |
Thomas Hellstrom | c58f009 | 2013-11-14 10:49:05 -0800 | [diff] [blame] | 1727 | if (unlikely(ret != 0)) |
| 1728 | goto out_unlock; |
Thomas Hellstrom | c752308 | 2014-02-20 11:36:25 +0100 | [diff] [blame] | 1729 | __ttm_bo_unreserve(bo); |
Thomas Hellstrom | c58f009 | 2013-11-14 10:49:05 -0800 | [diff] [blame] | 1730 | |
| 1731 | out_unlock: |
| 1732 | mutex_unlock(&bo->wu_mutex); |
| 1733 | return ret; |
| 1734 | } |