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