ttm_tt.c revision 1.8.2.1 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 <linux/printk.h>
42 #include <drm/drm_cache.h>
43 #include <drm/drm_mem_util.h>
44 #include <drm/ttm/ttm_module.h>
45 #include <drm/ttm/ttm_bo_driver.h>
46 #include <drm/ttm/ttm_placement.h>
47 #include <drm/ttm/ttm_page_alloc.h>
48 #include <drm/bus_dma_hacks.h>
49
50 /**
51 * Allocates storage for pointers to the pages that back the ttm.
52 */
53 static void ttm_tt_alloc_page_directory(struct ttm_tt *ttm)
54 {
55 ttm->pages = drm_calloc_large(ttm->num_pages, sizeof(void*));
56 }
57
58 static void ttm_dma_tt_alloc_page_directory(struct ttm_dma_tt *ttm)
59 {
60 ttm->ttm.pages = drm_calloc_large(ttm->ttm.num_pages, sizeof(void*));
61 #ifndef __NetBSD__
62 ttm->dma_address = drm_calloc_large(ttm->ttm.num_pages,
63 sizeof(*ttm->dma_address));
64 #endif
65 }
66
67 #ifdef CONFIG_X86
68 static inline int ttm_tt_set_page_caching(struct page *p,
69 enum ttm_caching_state c_old,
70 enum ttm_caching_state c_new)
71 {
72 #ifdef __NetBSD__
73 return 0;
74 #else
75 int ret = 0;
76
77 if (PageHighMem(p))
78 return 0;
79
80 if (c_old != tt_cached) {
81 /* p isn't in the default caching state, set it to
82 * writeback first to free its current memtype. */
83
84 ret = set_pages_wb(p, 1);
85 if (ret)
86 return ret;
87 }
88
89 if (c_new == tt_wc)
90 ret = set_memory_wc((unsigned long) page_address(p), 1);
91 else if (c_new == tt_uncached)
92 ret = set_pages_uc(p, 1);
93
94 return ret;
95 #endif
96 }
97 #else /* CONFIG_X86 */
98 static inline int ttm_tt_set_page_caching(struct page *p,
99 enum ttm_caching_state c_old,
100 enum ttm_caching_state c_new)
101 {
102 return 0;
103 }
104 #endif /* CONFIG_X86 */
105
106 /*
107 * Change caching policy for the linear kernel map
108 * for range of pages in a ttm.
109 */
110
111 static int ttm_tt_set_caching(struct ttm_tt *ttm,
112 enum ttm_caching_state c_state)
113 {
114 int i, j;
115 struct page *cur_page;
116 int ret;
117
118 if (ttm->caching_state == c_state)
119 return 0;
120
121 if (ttm->state == tt_unpopulated) {
122 /* Change caching but don't populate */
123 ttm->caching_state = c_state;
124 return 0;
125 }
126
127 if (ttm->caching_state == tt_cached)
128 drm_clflush_pages(ttm->pages, ttm->num_pages);
129
130 for (i = 0; i < ttm->num_pages; ++i) {
131 cur_page = ttm->pages[i];
132 if (likely(cur_page != NULL)) {
133 ret = ttm_tt_set_page_caching(cur_page,
134 ttm->caching_state,
135 c_state);
136 if (unlikely(ret != 0))
137 goto out_err;
138 }
139 }
140
141 ttm->caching_state = c_state;
142
143 return 0;
144
145 out_err:
146 for (j = 0; j < i; ++j) {
147 cur_page = ttm->pages[j];
148 if (likely(cur_page != NULL)) {
149 (void)ttm_tt_set_page_caching(cur_page, c_state,
150 ttm->caching_state);
151 }
152 }
153
154 return ret;
155 }
156
157 int ttm_tt_set_placement_caching(struct ttm_tt *ttm, uint32_t placement)
158 {
159 enum ttm_caching_state state;
160
161 if (placement & TTM_PL_FLAG_WC)
162 state = tt_wc;
163 else if (placement & TTM_PL_FLAG_UNCACHED)
164 state = tt_uncached;
165 else
166 state = tt_cached;
167
168 return ttm_tt_set_caching(ttm, state);
169 }
170 EXPORT_SYMBOL(ttm_tt_set_placement_caching);
171
172 void ttm_tt_destroy(struct ttm_tt *ttm)
173 {
174 if (unlikely(ttm == NULL))
175 return;
176
177 if (ttm->state == tt_bound) {
178 ttm_tt_unbind(ttm);
179 }
180
181 if (ttm->state == tt_unbound)
182 ttm_tt_unpopulate(ttm);
183
184 #ifndef __NetBSD__
185 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP) &&
186 ttm->swap_storage)
187 fput(ttm->swap_storage);
188
189 ttm->swap_storage = NULL;
190 #endif
191 ttm->func->destroy(ttm);
192 }
193
194 int ttm_tt_init(struct ttm_tt *ttm, struct ttm_bo_device *bdev,
195 unsigned long size, uint32_t page_flags,
196 struct page *dummy_read_page)
197 {
198 ttm->bdev = bdev;
199 ttm->glob = bdev->glob;
200 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
201 ttm->caching_state = tt_cached;
202 ttm->page_flags = page_flags;
203 ttm->dummy_read_page = dummy_read_page;
204 ttm->state = tt_unpopulated;
205 #ifdef __NetBSD__
206 WARN(size == 0, "zero-size allocation in %s, please file a NetBSD PR",
207 __func__); /* paranoia -- can't prove in five minutes */
208 size = MAX(size, 1);
209 ttm->swap_storage = uao_create(roundup2(size, PAGE_SIZE), 0);
210 uao_set_pgfl(ttm->swap_storage, bus_dmamem_pgfl(bdev->dmat));
211 #else
212 ttm->swap_storage = NULL;
213 #endif
214 TAILQ_INIT(&ttm->pglist);
215
216 ttm_tt_alloc_page_directory(ttm);
217 if (!ttm->pages) {
218 ttm_tt_destroy(ttm);
219 pr_err("Failed allocating page table\n");
220 return -ENOMEM;
221 }
222 return 0;
223 }
224 EXPORT_SYMBOL(ttm_tt_init);
225
226 void ttm_tt_fini(struct ttm_tt *ttm)
227 {
228 #ifdef __NetBSD__
229 uao_detach(ttm->swap_storage);
230 ttm->swap_storage = NULL;
231 #endif
232 drm_free_large(ttm->pages);
233 ttm->pages = NULL;
234 }
235 EXPORT_SYMBOL(ttm_tt_fini);
236
237 int ttm_dma_tt_init(struct ttm_dma_tt *ttm_dma, struct ttm_bo_device *bdev,
238 unsigned long size, uint32_t page_flags,
239 struct page *dummy_read_page)
240 {
241 struct ttm_tt *ttm = &ttm_dma->ttm;
242
243 ttm->bdev = bdev;
244 ttm->glob = bdev->glob;
245 ttm->num_pages = (size + PAGE_SIZE - 1) >> PAGE_SHIFT;
246 ttm->caching_state = tt_cached;
247 ttm->page_flags = page_flags;
248 ttm->dummy_read_page = dummy_read_page;
249 ttm->state = tt_unpopulated;
250 #ifdef __NetBSD__
251 WARN(size == 0, "zero-size allocation in %s, please file a NetBSD PR",
252 __func__); /* paranoia -- can't prove in five minutes */
253 size = MAX(size, 1);
254 ttm->swap_storage = uao_create(roundup2(size, PAGE_SIZE), 0);
255 uao_set_pgfl(ttm->swap_storage, bus_dmamem_pgfl(bdev->dmat));
256 #else
257 ttm->swap_storage = NULL;
258 #endif
259 TAILQ_INIT(&ttm->pglist);
260
261 INIT_LIST_HEAD(&ttm_dma->pages_list);
262 ttm_dma_tt_alloc_page_directory(ttm_dma);
263 #ifdef __NetBSD__
264 {
265 int error;
266
267 if (ttm->num_pages > (SIZE_MAX /
268 MIN(sizeof(ttm_dma->dma_segs[0]), PAGE_SIZE))) {
269 error = ENOMEM;
270 goto fail0;
271 }
272 ttm_dma->dma_segs = kmem_alloc((ttm->num_pages *
273 sizeof(ttm_dma->dma_segs[0])), KM_SLEEP);
274 error = bus_dmamap_create(ttm->bdev->dmat,
275 (ttm->num_pages * PAGE_SIZE), ttm->num_pages, PAGE_SIZE, 0,
276 BUS_DMA_WAITOK, &ttm_dma->dma_address);
277 if (error)
278 goto fail1;
279
280 return 0;
281
282 fail2: __unused
283 bus_dmamap_destroy(ttm->bdev->dmat, ttm_dma->dma_address);
284 fail1: kmem_free(ttm_dma->dma_segs, (ttm->num_pages *
285 sizeof(ttm_dma->dma_segs[0])));
286 fail0: KASSERT(error);
287 ttm_tt_destroy(ttm);
288 /* XXX errno NetBSD->Linux */
289 return -error;
290 }
291 #else
292 if (!ttm->pages || !ttm_dma->dma_address) {
293 ttm_tt_destroy(ttm);
294 pr_err("Failed allocating page table\n");
295 return -ENOMEM;
296 }
297 return 0;
298 #endif
299 }
300 EXPORT_SYMBOL(ttm_dma_tt_init);
301
302 void ttm_dma_tt_fini(struct ttm_dma_tt *ttm_dma)
303 {
304 struct ttm_tt *ttm = &ttm_dma->ttm;
305
306 #ifdef __NetBSD__
307 uao_detach(ttm->swap_storage);
308 ttm->swap_storage = NULL;
309 #endif
310 drm_free_large(ttm->pages);
311 ttm->pages = NULL;
312 #ifdef __NetBSD__
313 bus_dmamap_destroy(ttm->bdev->dmat, ttm_dma->dma_address);
314 kmem_free(ttm_dma->dma_segs, (ttm->num_pages *
315 sizeof(ttm_dma->dma_segs[0])));
316 #else
317 drm_free_large(ttm_dma->dma_address);
318 ttm_dma->dma_address = NULL;
319 #endif
320 }
321 EXPORT_SYMBOL(ttm_dma_tt_fini);
322
323 void ttm_tt_unbind(struct ttm_tt *ttm)
324 {
325 int ret __diagused;
326
327 if (ttm->state == tt_bound) {
328 ret = ttm->func->unbind(ttm);
329 BUG_ON(ret);
330 ttm->state = tt_unbound;
331 }
332 }
333
334 int ttm_tt_bind(struct ttm_tt *ttm, struct ttm_mem_reg *bo_mem)
335 {
336 int ret = 0;
337
338 if (!ttm)
339 return -EINVAL;
340
341 if (ttm->state == tt_bound)
342 return 0;
343
344 ret = ttm->bdev->driver->ttm_tt_populate(ttm);
345 if (ret)
346 return ret;
347
348 ret = ttm->func->bind(ttm, bo_mem);
349 if (unlikely(ret != 0))
350 return ret;
351
352 ttm->state = tt_bound;
353
354 return 0;
355 }
356 EXPORT_SYMBOL(ttm_tt_bind);
357
358 #ifdef __NetBSD__
359 /*
360 * ttm_tt_wire(ttm)
361 *
362 * Wire the uvm pages of ttm and fill the ttm page array. ttm
363 * must be unpopulated, and must be marked swapped. This does not
364 * change either state -- the caller is expected to include it
365 * among other operations for such a state transition.
366 */
367 int
368 ttm_tt_wire(struct ttm_tt *ttm)
369 {
370 struct uvm_object *uobj = ttm->swap_storage;
371 struct vm_page *page;
372 unsigned i;
373 int error;
374
375 KASSERTMSG((ttm->state == tt_unpopulated),
376 "ttm_tt %p must be unpopulated for wiring, but state=%d",
377 ttm, (int)ttm->state);
378 KASSERT(ISSET(ttm->page_flags, TTM_PAGE_FLAG_SWAPPED));
379 KASSERT(uobj != NULL);
380
381 error = uvm_obj_wirepages(uobj, 0, (ttm->num_pages << PAGE_SHIFT),
382 &ttm->pglist);
383 if (error)
384 /* XXX errno NetBSD->Linux */
385 return -error;
386
387 i = 0;
388 TAILQ_FOREACH(page, &ttm->pglist, pageq.queue) {
389 KASSERT(i < ttm->num_pages);
390 KASSERT(ttm->pages[i] == NULL);
391 ttm->pages[i] = container_of(page, struct page, p_vmp);
392 i++;
393 }
394 KASSERT(i == ttm->num_pages);
395
396 /* Success! */
397 return 0;
398 }
399
400 /*
401 * ttm_tt_unwire(ttm)
402 *
403 * Nullify the ttm page array and unwire the uvm pages of ttm.
404 * ttm must be unbound and must be marked swapped. This does not
405 * change either state -- the caller is expected to include it
406 * among other operations for such a state transition.
407 */
408 void
409 ttm_tt_unwire(struct ttm_tt *ttm)
410 {
411 struct uvm_object *uobj = ttm->swap_storage;
412 unsigned i;
413
414 KASSERTMSG((ttm->state == tt_unbound),
415 "ttm_tt %p must be unbound for unwiring, but state=%d",
416 ttm, (int)ttm->state);
417 KASSERT(!ISSET(ttm->page_flags, TTM_PAGE_FLAG_SWAPPED));
418 KASSERT(uobj != NULL);
419
420 uvm_obj_unwirepages(uobj, 0, (ttm->num_pages << PAGE_SHIFT));
421 for (i = 0; i < ttm->num_pages; i++)
422 ttm->pages[i] = NULL;
423 }
424 #endif
425
426 #ifndef __NetBSD__
427 int ttm_tt_swapin(struct ttm_tt *ttm)
428 {
429 struct address_space *swap_space;
430 struct file *swap_storage;
431 struct page *from_page;
432 struct page *to_page;
433 int i;
434 int ret = -ENOMEM;
435
436 swap_storage = ttm->swap_storage;
437 BUG_ON(swap_storage == NULL);
438
439 swap_space = file_inode(swap_storage)->i_mapping;
440
441 for (i = 0; i < ttm->num_pages; ++i) {
442 from_page = shmem_read_mapping_page(swap_space, i);
443 if (IS_ERR(from_page)) {
444 ret = PTR_ERR(from_page);
445 goto out_err;
446 }
447 to_page = ttm->pages[i];
448 if (unlikely(to_page == NULL))
449 goto out_err;
450
451 copy_highpage(to_page, from_page);
452 page_cache_release(from_page);
453 }
454
455 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
456 fput(swap_storage);
457 ttm->swap_storage = NULL;
458 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
459
460 return 0;
461 out_err:
462 return ret;
463 }
464 #endif
465
466 int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
467 {
468 #ifdef __NetBSD__
469
470 KASSERTMSG((ttm->state == tt_unpopulated || ttm->state == tt_unbound),
471 "ttm_tt %p must be unpopulated or unbound for swapout,"
472 " but state=%d",
473 ttm, (int)ttm->state);
474 KASSERTMSG((ttm->caching_state == tt_cached),
475 "ttm_tt %p must be cached for swapout, but caching_state=%d",
476 ttm, (int)ttm->caching_state);
477 KASSERT(persistent_swap_storage == NULL);
478
479 ttm->bdev->driver->ttm_tt_swapout(ttm);
480 return 0;
481 #else
482 struct address_space *swap_space;
483 struct file *swap_storage;
484 struct page *from_page;
485 struct page *to_page;
486 int i;
487 int ret = -ENOMEM;
488
489 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
490 BUG_ON(ttm->caching_state != tt_cached);
491
492 if (!persistent_swap_storage) {
493 swap_storage = shmem_file_setup("ttm swap",
494 ttm->num_pages << PAGE_SHIFT,
495 0);
496 if (unlikely(IS_ERR(swap_storage))) {
497 pr_err("Failed allocating swap storage\n");
498 return PTR_ERR(swap_storage);
499 }
500 } else
501 swap_storage = persistent_swap_storage;
502
503 swap_space = file_inode(swap_storage)->i_mapping;
504
505 for (i = 0; i < ttm->num_pages; ++i) {
506 from_page = ttm->pages[i];
507 if (unlikely(from_page == NULL))
508 continue;
509 to_page = shmem_read_mapping_page(swap_space, i);
510 if (unlikely(IS_ERR(to_page))) {
511 ret = PTR_ERR(to_page);
512 goto out_err;
513 }
514 copy_highpage(to_page, from_page);
515 set_page_dirty(to_page);
516 mark_page_accessed(to_page);
517 page_cache_release(to_page);
518 }
519
520 ttm_tt_unpopulate(ttm);
521 ttm->swap_storage = swap_storage;
522 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
523 if (persistent_swap_storage)
524 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
525
526 return 0;
527 out_err:
528 if (!persistent_swap_storage)
529 fput(swap_storage);
530
531 return ret;
532 #endif
533 }
534
535 static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
536 {
537 #ifndef __NetBSD__
538 pgoff_t i;
539 struct page **page = ttm->pages;
540
541 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
542 return;
543
544 for (i = 0; i < ttm->num_pages; ++i) {
545 (*page)->mapping = NULL;
546 (*page++)->index = 0;
547 }
548 #endif
549 }
550
551 void ttm_tt_unpopulate(struct ttm_tt *ttm)
552 {
553 if (ttm->state == tt_unpopulated)
554 return;
555
556 ttm_tt_clear_mapping(ttm);
557 ttm->bdev->driver->ttm_tt_unpopulate(ttm);
558 }
559