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