ttm_tt.c revision 1.6.10.2 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 or unbound, and must be marked swapped.
364 * This does not change either state -- the caller is expected to
365 * include it 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 || ttm->state == tt_unbound),
376 "ttm_tt %p must be unpopulated or unbound for wiring,"
377 " but state=%d",
378 ttm, (int)ttm->state);
379 KASSERT(ISSET(ttm->page_flags, TTM_PAGE_FLAG_SWAPPED));
380 KASSERT(uobj != NULL);
381
382 error = uvm_obj_wirepages(uobj, 0, (ttm->num_pages << PAGE_SHIFT),
383 &ttm->pglist);
384 if (error)
385 /* XXX errno NetBSD->Linux */
386 return -error;
387
388 i = 0;
389 TAILQ_FOREACH(page, &ttm->pglist, pageq.queue) {
390 KASSERT(i < ttm->num_pages);
391 KASSERT(ttm->pages[i] == NULL);
392 ttm->pages[i] = container_of(page, struct page, p_vmp);
393 i++;
394 }
395 KASSERT(i == ttm->num_pages);
396
397 /* Success! */
398 return 0;
399 }
400
401 /*
402 * ttm_tt_unwire(ttm)
403 *
404 * Nullify the ttm page array and unwire the uvm pages of ttm.
405 * ttm must be unbound and must be marked swapped. This does not
406 * change either state -- the caller is expected to include it
407 * among other operations for such a state transition.
408 */
409 void
410 ttm_tt_unwire(struct ttm_tt *ttm)
411 {
412 struct uvm_object *uobj = ttm->swap_storage;
413 unsigned i;
414
415 KASSERTMSG((ttm->state == tt_unbound),
416 "ttm_tt %p must be unbound for unwiring, but state=%d",
417 ttm, (int)ttm->state);
418 KASSERT(!ISSET(ttm->page_flags, TTM_PAGE_FLAG_SWAPPED));
419 KASSERT(uobj != NULL);
420
421 uvm_obj_unwirepages(uobj, 0, (ttm->num_pages << PAGE_SHIFT));
422 for (i = 0; i < ttm->num_pages; i++)
423 ttm->pages[i] = NULL;
424 }
425 #endif
426
427 #ifndef __NetBSD__
428 int ttm_tt_swapin(struct ttm_tt *ttm)
429 {
430 struct address_space *swap_space;
431 struct file *swap_storage;
432 struct page *from_page;
433 struct page *to_page;
434 int i;
435 int ret = -ENOMEM;
436
437 swap_storage = ttm->swap_storage;
438 BUG_ON(swap_storage == NULL);
439
440 swap_space = file_inode(swap_storage)->i_mapping;
441
442 for (i = 0; i < ttm->num_pages; ++i) {
443 from_page = shmem_read_mapping_page(swap_space, i);
444 if (IS_ERR(from_page)) {
445 ret = PTR_ERR(from_page);
446 goto out_err;
447 }
448 to_page = ttm->pages[i];
449 if (unlikely(to_page == NULL))
450 goto out_err;
451
452 copy_highpage(to_page, from_page);
453 page_cache_release(from_page);
454 }
455
456 if (!(ttm->page_flags & TTM_PAGE_FLAG_PERSISTENT_SWAP))
457 fput(swap_storage);
458 ttm->swap_storage = NULL;
459 ttm->page_flags &= ~TTM_PAGE_FLAG_SWAPPED;
460
461 return 0;
462 out_err:
463 return ret;
464 }
465 #endif
466
467 int ttm_tt_swapout(struct ttm_tt *ttm, struct file *persistent_swap_storage)
468 {
469 #ifdef __NetBSD__
470
471 KASSERTMSG((ttm->state == tt_unpopulated || ttm->state == tt_unbound),
472 "ttm_tt %p must be unpopulated or unbound for swapout,"
473 " but state=%d",
474 ttm, (int)ttm->state);
475 KASSERTMSG((ttm->caching_state == tt_cached),
476 "ttm_tt %p must be cached for swapout, but caching_state=%d",
477 ttm, (int)ttm->caching_state);
478 KASSERT(persistent_swap_storage == NULL);
479
480 ttm->bdev->driver->ttm_tt_swapout(ttm);
481 return 0;
482 #else
483 struct address_space *swap_space;
484 struct file *swap_storage;
485 struct page *from_page;
486 struct page *to_page;
487 int i;
488 int ret = -ENOMEM;
489
490 BUG_ON(ttm->state != tt_unbound && ttm->state != tt_unpopulated);
491 BUG_ON(ttm->caching_state != tt_cached);
492
493 if (!persistent_swap_storage) {
494 swap_storage = shmem_file_setup("ttm swap",
495 ttm->num_pages << PAGE_SHIFT,
496 0);
497 if (unlikely(IS_ERR(swap_storage))) {
498 pr_err("Failed allocating swap storage\n");
499 return PTR_ERR(swap_storage);
500 }
501 } else
502 swap_storage = persistent_swap_storage;
503
504 swap_space = file_inode(swap_storage)->i_mapping;
505
506 for (i = 0; i < ttm->num_pages; ++i) {
507 from_page = ttm->pages[i];
508 if (unlikely(from_page == NULL))
509 continue;
510 to_page = shmem_read_mapping_page(swap_space, i);
511 if (unlikely(IS_ERR(to_page))) {
512 ret = PTR_ERR(to_page);
513 goto out_err;
514 }
515 copy_highpage(to_page, from_page);
516 set_page_dirty(to_page);
517 mark_page_accessed(to_page);
518 page_cache_release(to_page);
519 }
520
521 ttm_tt_unpopulate(ttm);
522 ttm->swap_storage = swap_storage;
523 ttm->page_flags |= TTM_PAGE_FLAG_SWAPPED;
524 if (persistent_swap_storage)
525 ttm->page_flags |= TTM_PAGE_FLAG_PERSISTENT_SWAP;
526
527 return 0;
528 out_err:
529 if (!persistent_swap_storage)
530 fput(swap_storage);
531
532 return ret;
533 #endif
534 }
535
536 static void ttm_tt_clear_mapping(struct ttm_tt *ttm)
537 {
538 #ifndef __NetBSD__
539 pgoff_t i;
540 struct page **page = ttm->pages;
541
542 if (ttm->page_flags & TTM_PAGE_FLAG_SG)
543 return;
544
545 for (i = 0; i < ttm->num_pages; ++i) {
546 (*page)->mapping = NULL;
547 (*page++)->index = 0;
548 }
549 #endif
550 }
551
552 void ttm_tt_unpopulate(struct ttm_tt *ttm)
553 {
554 if (ttm->state == tt_unpopulated)
555 return;
556
557 ttm_tt_clear_mapping(ttm);
558 ttm->bdev->driver->ttm_tt_unpopulate(ttm);
559 }
560