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 #define pr_fmt(fmt) "[TTM] " fmt 32 33 #include <linux/sched.h> 34 #include <linux/highmem.h> 35 #include <linux/pagemap.h> 36 #include <linux/shmem_fs.h> 37 #include <linux/file.h> 38 #include <linux/swap.h> 39 #include <linux/slab.h> 40 #include <linux/export.h> 41 #include <drm/drm_mem_util.h> 42 #include <drm/ttm/ttm_module.h> 43 #include <drm/ttm/ttm_bo_driver.h> 44 #include <drm/ttm/ttm_placement.h> 45 #include <drm/ttm/ttm_page_alloc.h> 46 47 /** 48 * Allocates storage for pointers to the pages that back the ttm. 49 */ 50 static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm) 51 { 52 ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*)); 53 } 54 55 static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm) 56 { 57 ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages, sizeof(void*)); 58 ttm->dma_address = drm_calloc_large(ttm->ttm.num_pages, 59 sizeof(*ttm->dma_address)); 60 } 61 62 #ifdef CONFIG_X86 63 static inline int ttm_tt_set_page_caching(struct page *p, 64 enum ttm_caching_state c_old, 65 enum ttm_caching_state c_new) 66 { 67 int ret = 0; 68 69 #if 0 70 if (PageHighMem(p)) 71 return 0; 72 #endif 73 74 if (c_old != tt_cached) { 75 /* p isn't in the default caching state, set it to 76 * writeback first to free its current memtype. */ 77 78 ret = set_pages_wb(p, 1); 79 if (ret) 80 return ret; 81 } 82 83 if (c_new == tt_wc) 84 pmap_page_set_memattr((struct vm_page *)p, VM_MEMATTR_WRITE_COMBINING); 85 else if (c_new == tt_uncached) 86 pmap_page_set_memattr((struct vm_page *)p, VM_MEMATTR_UNCACHEABLE); 87 88 return (0); 89 } 90 #else /* CONFIG_X86 */ 91 static inline int ttm_tt_set_page_caching(struct page *p, 92 enum ttm_caching_state c_old, 93 enum ttm_caching_state c_new) 94 { 95 return 0; 96 } 97 #endif /* CONFIG_X86 */ 98 99 /* 100 * Change caching policy for the linear kernel map 101 * for range of pages in a ttm. 102 */ 103 104 static int ttm_tt_set_caching(struct ttm_tt *ttm, 105 enum ttm_caching_state c_state) 106 { 107 int i, j; 108 struct page *cur_page; 109 int ret; 110 111 if (ttm->caching_state == c_state) 112 return 0; 113 114 if (ttm->state == tt_unpopulated) { 115 /* Change caching but don't populate */ 116 ttm->caching_state = c_state; 117 return 0; 118 } 119 120 if (ttm->caching_state == tt_cached) 121 drm_clflush_pages(ttm->pages, ttm->num_pages); 122 123 for (i = 0; i < ttm->num_pages; ++i) { 124 cur_page = ttm->pages[i]; 125 if (likely(cur_page != NULL)) { 126 ret = ttm_tt_set_page_caching(cur_page, 127 ttm->caching_state, 128 c_state); 129 if (unlikely(ret != 0)) 130 goto out_err; 131 } 132 } 133 134 ttm->caching_state = c_state; 135 136 return 0; 137 138 out_err: 139 for (j = 0; j < i; ++j) { 140 cur_page = ttm->pages[j]; 141 if (likely(cur_page != NULL)) { 142 (void)ttm_tt_set_page_caching(cur_page, c_state, 143 ttm->caching_state); 144 } 145 } 146 147 return ret; 148 } 149 150 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement) 151 { 152 enum ttm_caching_state state; 153 154 if (placement & TTM_PL_FLAG_WC) 155 state = tt_wc; 156 else if (placement & TTM_PL_FLAG_UNCACHED) 157 state = tt_uncached; 158 else 159 state = tt_cached; 160 161 return ttm_tt_set_caching(ttm, state); 162 } 163 EXPORT_SYMBOL(ttm_tt_set_placement_caching); 164 165 void ttm_tt_destroy(struct ttm_tt *ttm) 166 { 167 if (unlikely(ttm == NULL)) 168 return; 169 170 if (ttm->state == tt_bound) { 171 ttm_tt_unbind(ttm); 172 } 173 174 if (ttm->state == tt_unbound) 175 ttm_tt_unpopulate(ttm); 176 177 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) && 178 ttm->swap_storage) 179 vm_object_deallocate(ttm->swap_storage); 180 181 ttm->swap_storage = NULL; 182 ttm->func->destroy(ttm); 183 } 184 185 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev, 186 unsigned long size, uint32_t page_flags, 187 struct page *dummy_read_page) 188 { 189 ttm->bdev = bdev; 190 ttm->glob = bdev->glob; 191 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 192 ttm->caching_state = tt_cached; 193 ttm->page_flags = page_flags; 194 ttm->dummy_read_page = dummy_read_page; 195 ttm->state = tt_unpopulated; 196 ttm->swap_storage = NULL; 197 198 ttm_tt_alloc_page_directory(ttm); 199 if (!ttm->pages) { 200 ttm_tt_destroy(ttm); 201 pr_err("Failed allocating page table\n"); 202 return -ENOMEM; 203 } 204 return 0; 205 } 206 EXPORT_SYMBOL(ttm_tt_init); 207 208 void ttm_tt_fini(struct ttm_tt *ttm) 209 { 210 drm_free_large(ttm->pages); 211 ttm->pages = NULL; 212 } 213 EXPORT_SYMBOL(ttm_tt_fini); 214 215 int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev, 216 unsigned long size, uint32_t page_flags, 217 struct page *dummy_read_page) 218 { 219 struct ttm_tt *ttm = &ttm_dma->ttm; 220 221 ttm->bdev = bdev; 222 ttm->glob = bdev->glob; 223 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT; 224 ttm->caching_state = tt_cached; 225 ttm->page_flags = page_flags; 226 ttm->dummy_read_page = dummy_read_page; 227 ttm->state = tt_unpopulated; 228 ttm->swap_storage = NULL; 229 230 INIT_LIST_HEAD(&ttm_dma->pages_list); 231 ttm_dma_tt_alloc_page_directory(ttm_dma); 232 if (!ttm->pages || !ttm_dma->dma_address) { 233 ttm_tt_destroy(ttm); 234 pr_err("Failed allocating page table\n"); 235 return -ENOMEM; 236 } 237 return 0; 238 } 239 EXPORT_SYMBOL(ttm_dma_tt_init); 240 241 void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma) 242 { 243 struct ttm_tt *ttm = &ttm_dma->ttm; 244 245 drm_free_large(ttm->pages); 246 ttm->pages = NULL; 247 drm_free_large(ttm_dma->dma_address); 248 ttm_dma->dma_address = NULL; 249 } 250 EXPORT_SYMBOL(ttm_dma_tt_fini); 251 252 void ttm_tt_unbind(struct ttm_tt *ttm) 253 { 254 int ret; 255 256 if (ttm->state == tt_bound) { 257 ret = ttm->func->unbind(ttm); 258 BUG_ON(ret); 259 ttm->state = tt_unbound; 260 } 261 } 262 263 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem) 264 { 265 int ret = 0; 266 267 if (!ttm) 268 return -EINVAL; 269 270 if (ttm->state == tt_bound) 271 return 0; 272 273 ret = ttm->bdev->driver->ttm_tt_populate(ttm); 274 if (ret) 275 return ret; 276 277 ret = ttm->func->bind(ttm, bo_mem); 278 if (unlikely(ret != 0)) 279 return ret; 280 281 ttm->state = tt_bound; 282 283 return 0; 284 } 285 EXPORT_SYMBOL(ttm_tt_bind); 286 287 int ttm_tt_swapin(struct ttm_tt *ttm) 288 { 289 vm_object_t obj; 290 struct page *from_page; 291 struct page *to_page; 292 int i; 293 int ret = -ENOMEM; 294 295 obj = ttm->swap_storage; 296 297 VM_OBJECT_LOCK(obj); 298 vm_object_pip_add(obj, 1); 299 for (i = 0; i < ttm->num_pages; ++i) { 300 from_page = (struct page *)vm_page_grab(obj, i, VM_ALLOC_NORMAL | 301 VM_ALLOC_RETRY); 302 if (((struct vm_page *)from_page)->valid != VM_PAGE_BITS_ALL) { 303 if (vm_pager_has_page(obj, i)) { 304 if (vm_pager_get_page(obj, (struct vm_page **)&from_page, 1) != VM_PAGER_OK) { 305 vm_page_free((struct vm_page *)from_page); 306 ret = -EIO; 307 goto out_err; 308 } 309 } else { 310 vm_page_zero_invalid((struct vm_page *)from_page, TRUE); 311 } 312 } 313 to_page = ttm->pages[i]; 314 if (unlikely(to_page == NULL)) { 315 vm_page_wakeup((struct vm_page *)from_page); 316 goto out_err; 317 } 318 319 pmap_copy_page(VM_PAGE_TO_PHYS((struct vm_page *)from_page), 320 VM_PAGE_TO_PHYS((struct vm_page *)to_page)); 321 vm_page_wakeup((struct vm_page *)from_page); 322 } 323 vm_object_pip_wakeup(obj); 324 VM_OBJECT_UNLOCK(obj); 325 326 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP)) 327 vm_object_deallocate(obj); 328 ttm->swap_storage = NULL; 329 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED; 330 331 return 0; 332 out_err: 333 vm_object_pip_wakeup(obj); 334 VM_OBJECT_UNLOCK(obj); 335 return ret; 336 } 337 338 int ttm_tt_swapout(struct ttm_tt *ttm, vm_object_t persistent_swap_storage) 339 { 340 vm_object_t obj; 341 vm_page_t from_page, to_page; 342 int i; 343 344 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated); 345 BUG_ON(ttm->caching_state != tt_cached); 346 347 if (!persistent_swap_storage) { 348 obj = swap_pager_alloc(NULL, 349 IDX_TO_OFF(ttm->num_pages), VM_PROT_DEFAULT, 0); 350 if (obj == NULL) { 351 pr_err("Failed allocating swap storage\n"); 352 return (-ENOMEM); 353 } 354 } else 355 obj = persistent_swap_storage; 356 357 VM_OBJECT_LOCK(obj); 358 vm_object_pip_add(obj, 1); 359 for (i = 0; i < ttm->num_pages; ++i) { 360 from_page = (struct vm_page *)ttm->pages[i]; 361 if (unlikely(from_page == NULL)) 362 continue; 363 to_page = vm_page_grab(obj, i, VM_ALLOC_NORMAL | 364 VM_ALLOC_RETRY); 365 pmap_copy_page(VM_PAGE_TO_PHYS(from_page), 366 VM_PAGE_TO_PHYS(to_page)); 367 to_page->valid = VM_PAGE_BITS_ALL; 368 vm_page_dirty(to_page); 369 vm_page_wakeup(to_page); 370 } 371 vm_object_pip_wakeup(obj); 372 VM_OBJECT_UNLOCK(obj); 373 374 ttm_tt_unpopulate(ttm); 375 ttm->swap_storage = obj; 376 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED; 377 if (persistent_swap_storage) 378 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP; 379 380 return 0; 381 } 382 383 static void ttm_tt_clear_mapping(struct ttm_tt *ttm) 384 { 385 #if 0 386 pgoff_t i; 387 struct page **page = ttm->pages; 388 389 if (ttm->page_flags & TTM_PAGE_FLAG_SG) 390 return; 391 392 for (i = 0; i < ttm->num_pages; ++i) { 393 (*page)->mapping = NULL; 394 (*page++)->index = 0; 395 } 396 #endif 397 } 398 399 void ttm_tt_unpopulate(struct ttm_tt *ttm) 400 { 401 if (ttm->state == tt_unpopulated) 402 return; 403 404 ttm_tt_clear_mapping(ttm); 405 ttm->bdev->driver->ttm_tt_unpopulate(ttm); 406 } 407