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 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 31 | #include <linux/vmalloc.h> |
| 32 | #include <linux/sched.h> |
| 33 | #include <linux/highmem.h> |
| 34 | #include <linux/pagemap.h> |
| 35 | #include <linux/file.h> |
| 36 | #include <linux/swap.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame^] | 37 | #include <linux/slab.h> |
Dave Airlie | c9c97b8 | 2009-08-27 09:53:47 +1000 | [diff] [blame] | 38 | #include "drm_cache.h" |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 39 | #include "ttm/ttm_module.h" |
| 40 | #include "ttm/ttm_bo_driver.h" |
| 41 | #include "ttm/ttm_placement.h" |
| 42 | |
| 43 | static int ttm_tt_swapin(struct ttm_tt *ttm); |
| 44 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 45 | /** |
| 46 | * Allocates storage for pointers to the pages that back the ttm. |
| 47 | * |
| 48 | * Uses kmalloc if possible. Otherwise falls back to vmalloc. |
| 49 | */ |
| 50 | static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm) |
| 51 | { |
| 52 | unsigned long size = ttm->num_pages * sizeof(*ttm->pages); |
| 53 | ttm->pages = NULL; |
| 54 | |
| 55 | if (size <= PAGE_SIZE) |
| 56 | ttm->pages = kzalloc(size, GFP_KERNEL); |
| 57 | |
| 58 | if (!ttm->pages) { |
| 59 | ttm->pages = vmalloc_user(size); |
| 60 | if (ttm->pages) |
| 61 | ttm->page_flags |= TTM_PAGE_FLAG_VMALLOC; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | static void ttm_tt_free_page_directory(struct ttm_tt *ttm) |
| 66 | { |
| 67 | if (ttm->page_flags & TTM_PAGE_FLAG_VMALLOC) { |
| 68 | vfree(ttm->pages); |
| 69 | ttm->page_flags &= ~TTM_PAGE_FLAG_VMALLOC; |
| 70 | } else { |
| 71 | kfree(ttm->pages); |
| 72 | } |
| 73 | ttm->pages = NULL; |
| 74 | } |
| 75 | |
| 76 | static struct page *ttm_tt_alloc_page(unsigned page_flags) |
| 77 | { |
Dave Airlie | b42db2b | 2009-07-29 16:56:52 +1000 | [diff] [blame] | 78 | gfp_t gfp_flags = GFP_USER; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 79 | |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 80 | if (page_flags & TTM_PAGE_FLAG_ZERO_ALLOC) |
| 81 | gfp_flags |= __GFP_ZERO; |
| 82 | |
| 83 | if (page_flags & TTM_PAGE_FLAG_DMA32) |
| 84 | gfp_flags |= __GFP_DMA32; |
Dave Airlie | b42db2b | 2009-07-29 16:56:52 +1000 | [diff] [blame] | 85 | else |
| 86 | gfp_flags |= __GFP_HIGHMEM; |
Dave Airlie | ad49f50 | 2009-07-10 22:36:26 +1000 | [diff] [blame] | 87 | |
| 88 | return alloc_page(gfp_flags); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | static void ttm_tt_free_user_pages(struct ttm_tt *ttm) |
| 92 | { |
| 93 | int write; |
| 94 | int dirty; |
| 95 | struct page *page; |
| 96 | int i; |
| 97 | struct ttm_backend *be = ttm->be; |
| 98 | |
| 99 | BUG_ON(!(ttm->page_flags & TTM_PAGE_FLAG_USER)); |
| 100 | write = ((ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0); |
| 101 | dirty = ((ttm->page_flags & TTM_PAGE_FLAG_USER_DIRTY) != 0); |
| 102 | |
| 103 | if (be) |
| 104 | be->func->clear(be); |
| 105 | |
| 106 | for (i = 0; i < ttm->num_pages; ++i) { |
| 107 | page = ttm->pages[i]; |
| 108 | if (page == NULL) |
| 109 | continue; |
| 110 | |
| 111 | if (page == ttm->dummy_read_page) { |
| 112 | BUG_ON(write); |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | if (write && dirty && !PageReserved(page)) |
| 117 | set_page_dirty_lock(page); |
| 118 | |
| 119 | ttm->pages[i] = NULL; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 120 | ttm_mem_global_free(ttm->glob->mem_glob, PAGE_SIZE); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 121 | put_page(page); |
| 122 | } |
| 123 | ttm->state = tt_unpopulated; |
| 124 | ttm->first_himem_page = ttm->num_pages; |
| 125 | ttm->last_lomem_page = -1; |
| 126 | } |
| 127 | |
| 128 | static struct page *__ttm_tt_get_page(struct ttm_tt *ttm, int index) |
| 129 | { |
| 130 | struct page *p; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 131 | struct ttm_mem_global *mem_glob = ttm->glob->mem_glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 132 | int ret; |
| 133 | |
| 134 | while (NULL == (p = ttm->pages[index])) { |
| 135 | p = ttm_tt_alloc_page(ttm->page_flags); |
| 136 | |
| 137 | if (!p) |
| 138 | return NULL; |
| 139 | |
Thomas Hellstrom | 5fd9cba | 2009-08-17 16:28:39 +0200 | [diff] [blame] | 140 | ret = ttm_mem_global_alloc_page(mem_glob, p, false, false); |
| 141 | if (unlikely(ret != 0)) |
| 142 | goto out_err; |
| 143 | |
| 144 | if (PageHighMem(p)) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 145 | ttm->pages[--ttm->first_himem_page] = p; |
Thomas Hellstrom | 5fd9cba | 2009-08-17 16:28:39 +0200 | [diff] [blame] | 146 | else |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 147 | ttm->pages[++ttm->last_lomem_page] = p; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 148 | } |
| 149 | return p; |
| 150 | out_err: |
| 151 | put_page(p); |
| 152 | return NULL; |
| 153 | } |
| 154 | |
| 155 | struct page *ttm_tt_get_page(struct ttm_tt *ttm, int index) |
| 156 | { |
| 157 | int ret; |
| 158 | |
| 159 | if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) { |
| 160 | ret = ttm_tt_swapin(ttm); |
| 161 | if (unlikely(ret != 0)) |
| 162 | return NULL; |
| 163 | } |
| 164 | return __ttm_tt_get_page(ttm, index); |
| 165 | } |
| 166 | |
| 167 | int ttm_tt_populate(struct ttm_tt *ttm) |
| 168 | { |
| 169 | struct page *page; |
| 170 | unsigned long i; |
| 171 | struct ttm_backend *be; |
| 172 | int ret; |
| 173 | |
| 174 | if (ttm->state != tt_unpopulated) |
| 175 | return 0; |
| 176 | |
| 177 | if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) { |
| 178 | ret = ttm_tt_swapin(ttm); |
| 179 | if (unlikely(ret != 0)) |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | be = ttm->be; |
| 184 | |
| 185 | for (i = 0; i < ttm->num_pages; ++i) { |
| 186 | page = __ttm_tt_get_page(ttm, i); |
| 187 | if (!page) |
| 188 | return -ENOMEM; |
| 189 | } |
| 190 | |
| 191 | be->func->populate(be, ttm->num_pages, ttm->pages, |
| 192 | ttm->dummy_read_page); |
| 193 | ttm->state = tt_unbound; |
| 194 | return 0; |
| 195 | } |
Thomas Hellstrom | 4bfd75c | 2009-12-06 21:46:27 +0100 | [diff] [blame] | 196 | EXPORT_SYMBOL(ttm_tt_populate); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 197 | |
| 198 | #ifdef CONFIG_X86 |
| 199 | static inline int ttm_tt_set_page_caching(struct page *p, |
Francisco Jerez | f0e2f38 | 2010-02-20 07:30:15 +1000 | [diff] [blame] | 200 | enum ttm_caching_state c_old, |
| 201 | enum ttm_caching_state c_new) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 202 | { |
Francisco Jerez | db78e27 | 2010-01-12 18:49:43 +0100 | [diff] [blame] | 203 | int ret = 0; |
| 204 | |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 205 | if (PageHighMem(p)) |
| 206 | return 0; |
| 207 | |
Francisco Jerez | f0e2f38 | 2010-02-20 07:30:15 +1000 | [diff] [blame] | 208 | if (c_old != tt_cached) { |
Francisco Jerez | db78e27 | 2010-01-12 18:49:43 +0100 | [diff] [blame] | 209 | /* p isn't in the default caching state, set it to |
| 210 | * writeback first to free its current memtype. */ |
| 211 | |
| 212 | ret = set_pages_wb(p, 1); |
| 213 | if (ret) |
| 214 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 215 | } |
Francisco Jerez | db78e27 | 2010-01-12 18:49:43 +0100 | [diff] [blame] | 216 | |
Francisco Jerez | f0e2f38 | 2010-02-20 07:30:15 +1000 | [diff] [blame] | 217 | if (c_new == tt_wc) |
Francisco Jerez | db78e27 | 2010-01-12 18:49:43 +0100 | [diff] [blame] | 218 | ret = set_memory_wc((unsigned long) page_address(p), 1); |
Francisco Jerez | f0e2f38 | 2010-02-20 07:30:15 +1000 | [diff] [blame] | 219 | else if (c_new == tt_uncached) |
Francisco Jerez | db78e27 | 2010-01-12 18:49:43 +0100 | [diff] [blame] | 220 | ret = set_pages_uc(p, 1); |
| 221 | |
| 222 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 223 | } |
| 224 | #else /* CONFIG_X86 */ |
| 225 | static inline int ttm_tt_set_page_caching(struct page *p, |
Francisco Jerez | f0e2f38 | 2010-02-20 07:30:15 +1000 | [diff] [blame] | 226 | enum ttm_caching_state c_old, |
| 227 | enum ttm_caching_state c_new) |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 228 | { |
| 229 | return 0; |
| 230 | } |
| 231 | #endif /* CONFIG_X86 */ |
| 232 | |
| 233 | /* |
| 234 | * Change caching policy for the linear kernel map |
| 235 | * for range of pages in a ttm. |
| 236 | */ |
| 237 | |
| 238 | static int ttm_tt_set_caching(struct ttm_tt *ttm, |
| 239 | enum ttm_caching_state c_state) |
| 240 | { |
| 241 | int i, j; |
| 242 | struct page *cur_page; |
| 243 | int ret; |
| 244 | |
| 245 | if (ttm->caching_state == c_state) |
| 246 | return 0; |
| 247 | |
| 248 | if (c_state != tt_cached) { |
| 249 | ret = ttm_tt_populate(ttm); |
| 250 | if (unlikely(ret != 0)) |
| 251 | return ret; |
| 252 | } |
| 253 | |
| 254 | if (ttm->caching_state == tt_cached) |
Dave Airlie | c9c97b8 | 2009-08-27 09:53:47 +1000 | [diff] [blame] | 255 | drm_clflush_pages(ttm->pages, ttm->num_pages); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 256 | |
| 257 | for (i = 0; i < ttm->num_pages; ++i) { |
| 258 | cur_page = ttm->pages[i]; |
| 259 | if (likely(cur_page != NULL)) { |
Francisco Jerez | f0e2f38 | 2010-02-20 07:30:15 +1000 | [diff] [blame] | 260 | ret = ttm_tt_set_page_caching(cur_page, |
| 261 | ttm->caching_state, |
| 262 | c_state); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 263 | if (unlikely(ret != 0)) |
| 264 | goto out_err; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | ttm->caching_state = c_state; |
| 269 | |
| 270 | return 0; |
| 271 | |
| 272 | out_err: |
| 273 | for (j = 0; j < i; ++j) { |
| 274 | cur_page = ttm->pages[j]; |
| 275 | if (likely(cur_page != NULL)) { |
Francisco Jerez | f0e2f38 | 2010-02-20 07:30:15 +1000 | [diff] [blame] | 276 | (void)ttm_tt_set_page_caching(cur_page, c_state, |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 277 | ttm->caching_state); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement) |
| 285 | { |
| 286 | enum ttm_caching_state state; |
| 287 | |
| 288 | if (placement & TTM_PL_FLAG_WC) |
| 289 | state = tt_wc; |
| 290 | else if (placement & TTM_PL_FLAG_UNCACHED) |
| 291 | state = tt_uncached; |
| 292 | else |
| 293 | state = tt_cached; |
| 294 | |
| 295 | return ttm_tt_set_caching(ttm, state); |
| 296 | } |
Dave Airlie | df67bed | 2009-10-30 13:31:26 +1000 | [diff] [blame] | 297 | EXPORT_SYMBOL(ttm_tt_set_placement_caching); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 298 | |
| 299 | static void ttm_tt_free_alloced_pages(struct ttm_tt *ttm) |
| 300 | { |
| 301 | int i; |
| 302 | struct page *cur_page; |
| 303 | struct ttm_backend *be = ttm->be; |
| 304 | |
| 305 | if (be) |
| 306 | be->func->clear(be); |
| 307 | (void)ttm_tt_set_caching(ttm, tt_cached); |
| 308 | for (i = 0; i < ttm->num_pages; ++i) { |
| 309 | cur_page = ttm->pages[i]; |
| 310 | ttm->pages[i] = NULL; |
| 311 | if (cur_page) { |
| 312 | if (page_count(cur_page) != 1) |
| 313 | printk(KERN_ERR TTM_PFX |
| 314 | "Erroneous page count. " |
| 315 | "Leaking pages.\n"); |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 316 | ttm_mem_global_free_page(ttm->glob->mem_glob, |
Thomas Hellstrom | 5fd9cba | 2009-08-17 16:28:39 +0200 | [diff] [blame] | 317 | cur_page); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 318 | __free_page(cur_page); |
| 319 | } |
| 320 | } |
| 321 | ttm->state = tt_unpopulated; |
| 322 | ttm->first_himem_page = ttm->num_pages; |
| 323 | ttm->last_lomem_page = -1; |
| 324 | } |
| 325 | |
| 326 | void ttm_tt_destroy(struct ttm_tt *ttm) |
| 327 | { |
| 328 | struct ttm_backend *be; |
| 329 | |
| 330 | if (unlikely(ttm == NULL)) |
| 331 | return; |
| 332 | |
| 333 | be = ttm->be; |
| 334 | if (likely(be != NULL)) { |
| 335 | be->func->destroy(be); |
| 336 | ttm->be = NULL; |
| 337 | } |
| 338 | |
| 339 | if (likely(ttm->pages != NULL)) { |
| 340 | if (ttm->page_flags & TTM_PAGE_FLAG_USER) |
| 341 | ttm_tt_free_user_pages(ttm); |
| 342 | else |
| 343 | ttm_tt_free_alloced_pages(ttm); |
| 344 | |
| 345 | ttm_tt_free_page_directory(ttm); |
| 346 | } |
| 347 | |
| 348 | if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP) && |
| 349 | ttm->swap_storage) |
| 350 | fput(ttm->swap_storage); |
| 351 | |
| 352 | kfree(ttm); |
| 353 | } |
| 354 | |
| 355 | int ttm_tt_set_user(struct ttm_tt *ttm, |
| 356 | struct task_struct *tsk, |
| 357 | unsigned long start, unsigned long num_pages) |
| 358 | { |
| 359 | struct mm_struct *mm = tsk->mm; |
| 360 | int ret; |
| 361 | int write = (ttm->page_flags & TTM_PAGE_FLAG_WRITE) != 0; |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 362 | struct ttm_mem_global *mem_glob = ttm->glob->mem_glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 363 | |
| 364 | BUG_ON(num_pages != ttm->num_pages); |
| 365 | BUG_ON((ttm->page_flags & TTM_PAGE_FLAG_USER) == 0); |
| 366 | |
| 367 | /** |
| 368 | * Account user pages as lowmem pages for now. |
| 369 | */ |
| 370 | |
| 371 | ret = ttm_mem_global_alloc(mem_glob, num_pages * PAGE_SIZE, |
Thomas Hellstrom | 5fd9cba | 2009-08-17 16:28:39 +0200 | [diff] [blame] | 372 | false, false); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 373 | if (unlikely(ret != 0)) |
| 374 | return ret; |
| 375 | |
| 376 | down_read(&mm->mmap_sem); |
| 377 | ret = get_user_pages(tsk, mm, start, num_pages, |
| 378 | write, 0, ttm->pages, NULL); |
| 379 | up_read(&mm->mmap_sem); |
| 380 | |
| 381 | if (ret != num_pages && write) { |
| 382 | ttm_tt_free_user_pages(ttm); |
Thomas Hellstrom | 5fd9cba | 2009-08-17 16:28:39 +0200 | [diff] [blame] | 383 | ttm_mem_global_free(mem_glob, num_pages * PAGE_SIZE); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 384 | return -ENOMEM; |
| 385 | } |
| 386 | |
| 387 | ttm->tsk = tsk; |
| 388 | ttm->start = start; |
| 389 | ttm->state = tt_unbound; |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | struct ttm_tt *ttm_tt_create(struct ttm_bo_device *bdev, unsigned long size, |
| 395 | uint32_t page_flags, struct page *dummy_read_page) |
| 396 | { |
| 397 | struct ttm_bo_driver *bo_driver = bdev->driver; |
| 398 | struct ttm_tt *ttm; |
| 399 | |
| 400 | if (!bo_driver) |
| 401 | return NULL; |
| 402 | |
| 403 | ttm = kzalloc(sizeof(*ttm), GFP_KERNEL); |
| 404 | if (!ttm) |
| 405 | return NULL; |
| 406 | |
Thomas Hellstrom | a987fca | 2009-08-18 16:51:56 +0200 | [diff] [blame] | 407 | ttm->glob = bdev->glob; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 408 | ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; |
| 409 | ttm->first_himem_page = ttm->num_pages; |
| 410 | ttm->last_lomem_page = -1; |
| 411 | ttm->caching_state = tt_cached; |
| 412 | ttm->page_flags = page_flags; |
| 413 | |
| 414 | ttm->dummy_read_page = dummy_read_page; |
| 415 | |
| 416 | ttm_tt_alloc_page_directory(ttm); |
| 417 | if (!ttm->pages) { |
| 418 | ttm_tt_destroy(ttm); |
| 419 | printk(KERN_ERR TTM_PFX "Failed allocating page table\n"); |
| 420 | return NULL; |
| 421 | } |
| 422 | ttm->be = bo_driver->create_ttm_backend_entry(bdev); |
| 423 | if (!ttm->be) { |
| 424 | ttm_tt_destroy(ttm); |
| 425 | printk(KERN_ERR TTM_PFX "Failed creating ttm backend entry\n"); |
| 426 | return NULL; |
| 427 | } |
| 428 | ttm->state = tt_unpopulated; |
| 429 | return ttm; |
| 430 | } |
| 431 | |
| 432 | void ttm_tt_unbind(struct ttm_tt *ttm) |
| 433 | { |
| 434 | int ret; |
| 435 | struct ttm_backend *be = ttm->be; |
| 436 | |
| 437 | if (ttm->state == tt_bound) { |
| 438 | ret = be->func->unbind(be); |
| 439 | BUG_ON(ret); |
| 440 | ttm->state = tt_unbound; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem) |
| 445 | { |
| 446 | int ret = 0; |
| 447 | struct ttm_backend *be; |
| 448 | |
| 449 | if (!ttm) |
| 450 | return -EINVAL; |
| 451 | |
| 452 | if (ttm->state == tt_bound) |
| 453 | return 0; |
| 454 | |
| 455 | be = ttm->be; |
| 456 | |
| 457 | ret = ttm_tt_populate(ttm); |
| 458 | if (ret) |
| 459 | return ret; |
| 460 | |
| 461 | ret = be->func->bind(be, bo_mem); |
| 462 | if (ret) { |
| 463 | printk(KERN_ERR TTM_PFX "Couldn't bind backend.\n"); |
| 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | ttm->state = tt_bound; |
| 468 | |
| 469 | if (ttm->page_flags & TTM_PAGE_FLAG_USER) |
| 470 | ttm->page_flags |= TTM_PAGE_FLAG_USER_DIRTY; |
| 471 | return 0; |
| 472 | } |
| 473 | EXPORT_SYMBOL(ttm_tt_bind); |
| 474 | |
| 475 | static int ttm_tt_swapin(struct ttm_tt *ttm) |
| 476 | { |
| 477 | struct address_space *swap_space; |
| 478 | struct file *swap_storage; |
| 479 | struct page *from_page; |
| 480 | struct page *to_page; |
| 481 | void *from_virtual; |
| 482 | void *to_virtual; |
| 483 | int i; |
Maarten Maathuis | 290e5505 | 2010-02-20 03:22:21 +0100 | [diff] [blame] | 484 | int ret = -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 485 | |
| 486 | if (ttm->page_flags & TTM_PAGE_FLAG_USER) { |
| 487 | ret = ttm_tt_set_user(ttm, ttm->tsk, ttm->start, |
| 488 | ttm->num_pages); |
| 489 | if (unlikely(ret != 0)) |
| 490 | return ret; |
| 491 | |
| 492 | ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED; |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | swap_storage = ttm->swap_storage; |
| 497 | BUG_ON(swap_storage == NULL); |
| 498 | |
| 499 | swap_space = swap_storage->f_path.dentry->d_inode->i_mapping; |
| 500 | |
| 501 | for (i = 0; i < ttm->num_pages; ++i) { |
| 502 | from_page = read_mapping_page(swap_space, i, NULL); |
Maarten Maathuis | 290e5505 | 2010-02-20 03:22:21 +0100 | [diff] [blame] | 503 | if (IS_ERR(from_page)) { |
| 504 | ret = PTR_ERR(from_page); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 505 | goto out_err; |
Maarten Maathuis | 290e5505 | 2010-02-20 03:22:21 +0100 | [diff] [blame] | 506 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 507 | to_page = __ttm_tt_get_page(ttm, i); |
| 508 | if (unlikely(to_page == NULL)) |
| 509 | goto out_err; |
| 510 | |
| 511 | preempt_disable(); |
| 512 | from_virtual = kmap_atomic(from_page, KM_USER0); |
| 513 | to_virtual = kmap_atomic(to_page, KM_USER1); |
| 514 | memcpy(to_virtual, from_virtual, PAGE_SIZE); |
| 515 | kunmap_atomic(to_virtual, KM_USER1); |
| 516 | kunmap_atomic(from_virtual, KM_USER0); |
| 517 | preempt_enable(); |
| 518 | page_cache_release(from_page); |
| 519 | } |
| 520 | |
| 521 | if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTANT_SWAP)) |
| 522 | fput(swap_storage); |
| 523 | ttm->swap_storage = NULL; |
| 524 | ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED; |
| 525 | |
| 526 | return 0; |
| 527 | out_err: |
| 528 | ttm_tt_free_alloced_pages(ttm); |
Maarten Maathuis | 290e5505 | 2010-02-20 03:22:21 +0100 | [diff] [blame] | 529 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistant_swap_storage) |
| 533 | { |
| 534 | struct address_space *swap_space; |
| 535 | struct file *swap_storage; |
| 536 | struct page *from_page; |
| 537 | struct page *to_page; |
| 538 | void *from_virtual; |
| 539 | void *to_virtual; |
| 540 | int i; |
Maarten Maathuis | 290e5505 | 2010-02-20 03:22:21 +0100 | [diff] [blame] | 541 | int ret = -ENOMEM; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 542 | |
| 543 | BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated); |
| 544 | BUG_ON(ttm->caching_state != tt_cached); |
| 545 | |
| 546 | /* |
| 547 | * For user buffers, just unpin the pages, as there should be |
| 548 | * vma references. |
| 549 | */ |
| 550 | |
| 551 | if (ttm->page_flags & TTM_PAGE_FLAG_USER) { |
| 552 | ttm_tt_free_user_pages(ttm); |
| 553 | ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED; |
| 554 | ttm->swap_storage = NULL; |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | if (!persistant_swap_storage) { |
| 559 | swap_storage = shmem_file_setup("ttm swap", |
| 560 | ttm->num_pages << PAGE_SHIFT, |
| 561 | 0); |
| 562 | if (unlikely(IS_ERR(swap_storage))) { |
| 563 | printk(KERN_ERR "Failed allocating swap storage.\n"); |
Maarten Maathuis | 290e5505 | 2010-02-20 03:22:21 +0100 | [diff] [blame] | 564 | return PTR_ERR(swap_storage); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 565 | } |
| 566 | } else |
| 567 | swap_storage = persistant_swap_storage; |
| 568 | |
| 569 | swap_space = swap_storage->f_path.dentry->d_inode->i_mapping; |
| 570 | |
| 571 | for (i = 0; i < ttm->num_pages; ++i) { |
| 572 | from_page = ttm->pages[i]; |
| 573 | if (unlikely(from_page == NULL)) |
| 574 | continue; |
| 575 | to_page = read_mapping_page(swap_space, i, NULL); |
Maarten Maathuis | 290e5505 | 2010-02-20 03:22:21 +0100 | [diff] [blame] | 576 | if (unlikely(IS_ERR(to_page))) { |
| 577 | ret = PTR_ERR(to_page); |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 578 | goto out_err; |
Maarten Maathuis | 290e5505 | 2010-02-20 03:22:21 +0100 | [diff] [blame] | 579 | } |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 580 | preempt_disable(); |
| 581 | from_virtual = kmap_atomic(from_page, KM_USER0); |
| 582 | to_virtual = kmap_atomic(to_page, KM_USER1); |
| 583 | memcpy(to_virtual, from_virtual, PAGE_SIZE); |
| 584 | kunmap_atomic(to_virtual, KM_USER1); |
| 585 | kunmap_atomic(from_virtual, KM_USER0); |
| 586 | preempt_enable(); |
| 587 | set_page_dirty(to_page); |
| 588 | mark_page_accessed(to_page); |
| 589 | page_cache_release(to_page); |
| 590 | } |
| 591 | |
| 592 | ttm_tt_free_alloced_pages(ttm); |
| 593 | ttm->swap_storage = swap_storage; |
| 594 | ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED; |
| 595 | if (persistant_swap_storage) |
| 596 | ttm->page_flags |= TTM_PAGE_FLAG_PERSISTANT_SWAP; |
| 597 | |
| 598 | return 0; |
| 599 | out_err: |
| 600 | if (!persistant_swap_storage) |
| 601 | fput(swap_storage); |
| 602 | |
Maarten Maathuis | 290e5505 | 2010-02-20 03:22:21 +0100 | [diff] [blame] | 603 | return ret; |
Thomas Hellstrom | ba4e7d9 | 2009-06-10 15:20:19 +0200 | [diff] [blame] | 604 | } |