1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2020 Advanced Micro Devices, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 * OTHER DEALINGS IN THE SOFTWARE.
23 * Authors: Christian König
26 /* Pooling of allocated pages is necessary because changing the caching
27 * attributes on x86 of the linear mapping requires a costly cross CPU TLB
28 * invalidate for those addresses.
30 * Additional to that allocations from the DMA coherent API are pooled as well
31 * cause they are rather slow compared to alloc_pages+map.
34 #include <linux/module.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/debugfs.h>
37 #include <linux/highmem.h>
38 #include <linux/sched/mm.h>
41 #include <asm/set_memory.h>
44 #include <drm/ttm/ttm_pool.h>
45 #include <drm/ttm/ttm_tt.h>
46 #include <drm/ttm/ttm_bo.h>
48 #include "ttm_module.h"
51 * struct ttm_pool_dma - Helper object for coherent DMA mappings
53 * @addr: original DMA address returned for the mapping
54 * @vaddr: original vaddr return for the mapping and order in the lower bits
61 static unsigned long page_pool_size;
63 MODULE_PARM_DESC(page_pool_size, "Number of pages in the WC/UC/DMA pool");
64 module_param(page_pool_size, ulong, 0644);
66 static atomic_long_t allocated_pages;
68 static struct ttm_pool_type global_write_combined[MAX_ORDER];
69 static struct ttm_pool_type global_uncached[MAX_ORDER];
71 static struct ttm_pool_type global_dma32_write_combined[MAX_ORDER];
72 static struct ttm_pool_type global_dma32_uncached[MAX_ORDER];
74 static spinlock_t shrinker_lock;
75 static struct list_head shrinker_list;
76 static struct shrinker mm_shrinker;
78 /* Allocate pages of size 1 << order with the given gfp_flags */
79 static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
82 unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
83 struct ttm_pool_dma *dma;
87 /* Don't set the __GFP_COMP flag for higher order allocations.
88 * Mapping pages directly into an userspace process and calling
89 * put_page() on a TTM allocated page is illegal.
92 gfp_flags |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN |
95 if (!pool->use_dma_alloc) {
96 p = alloc_pages(gfp_flags, order);
102 dma = kmalloc(sizeof(*dma), GFP_KERNEL);
107 attr |= DMA_ATTR_NO_WARN;
109 vaddr = dma_alloc_attrs(pool->dev, (1ULL << order) * PAGE_SIZE,
110 &dma->addr, gfp_flags, attr);
114 /* TODO: This is an illegal abuse of the DMA API, but we need to rework
115 * TTM page fault handling and extend the DMA API to clean this up.
117 if (is_vmalloc_addr(vaddr))
118 p = vmalloc_to_page(vaddr);
120 p = virt_to_page(vaddr);
122 dma->vaddr = (unsigned long)vaddr | order;
123 p->private = (unsigned long)dma;
131 /* Reset the caching and pages of size 1 << order */
132 static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
133 unsigned int order, struct page *p)
135 unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
136 struct ttm_pool_dma *dma;
140 /* We don't care that set_pages_wb is inefficient here. This is only
141 * used when we have to shrink and CPU overhead is irrelevant then.
143 if (caching != ttm_cached && !PageHighMem(p))
144 set_pages_wb(p, 1 << order);
147 if (!pool || !pool->use_dma_alloc) {
148 __free_pages(p, order);
153 attr |= DMA_ATTR_NO_WARN;
155 dma = (void *)p->private;
156 vaddr = (void *)(dma->vaddr & PAGE_MASK);
157 dma_free_attrs(pool->dev, (1UL << order) * PAGE_SIZE, vaddr, dma->addr,
162 /* Apply a new caching to an array of pages */
163 static int ttm_pool_apply_caching(struct page **first, struct page **last,
164 enum ttm_caching caching)
167 unsigned int num_pages = last - first;
175 case ttm_write_combined:
176 return set_pages_array_wc(first, num_pages);
178 return set_pages_array_uc(first, num_pages);
184 /* Map pages of 1 << order size and fill the DMA address array */
185 static int ttm_pool_map(struct ttm_pool *pool, unsigned int order,
186 struct page *p, dma_addr_t **dma_addr)
191 if (pool->use_dma_alloc) {
192 struct ttm_pool_dma *dma = (void *)p->private;
196 size_t size = (1ULL << order) * PAGE_SIZE;
198 addr = dma_map_page(pool->dev, p, 0, size, DMA_BIDIRECTIONAL);
199 if (dma_mapping_error(pool->dev, addr))
203 for (i = 1 << order; i ; --i) {
204 *(*dma_addr)++ = addr;
211 /* Unmap pages of 1 << order size */
212 static void ttm_pool_unmap(struct ttm_pool *pool, dma_addr_t dma_addr,
213 unsigned int num_pages)
215 /* Unmapped while freeing the page */
216 if (pool->use_dma_alloc)
219 dma_unmap_page(pool->dev, dma_addr, (long)num_pages << PAGE_SHIFT,
223 /* Give pages into a specific pool_type */
224 static void ttm_pool_type_give(struct ttm_pool_type *pt, struct page *p)
226 unsigned int i, num_pages = 1 << pt->order;
228 for (i = 0; i < num_pages; ++i) {
230 clear_highpage(p + i);
232 clear_page(page_address(p + i));
235 spin_lock(&pt->lock);
236 list_add(&p->lru, &pt->pages);
237 spin_unlock(&pt->lock);
238 atomic_long_add(1 << pt->order, &allocated_pages);
241 /* Take pages from a specific pool_type, return NULL when nothing available */
242 static struct page *ttm_pool_type_take(struct ttm_pool_type *pt)
246 spin_lock(&pt->lock);
247 p = list_first_entry_or_null(&pt->pages, typeof(*p), lru);
249 atomic_long_sub(1 << pt->order, &allocated_pages);
252 spin_unlock(&pt->lock);
257 /* Initialize and add a pool type to the global shrinker list */
258 static void ttm_pool_type_init(struct ttm_pool_type *pt, struct ttm_pool *pool,
259 enum ttm_caching caching, unsigned int order)
262 pt->caching = caching;
264 spin_lock_init(&pt->lock);
265 INIT_LIST_HEAD(&pt->pages);
267 spin_lock(&shrinker_lock);
268 list_add_tail(&pt->shrinker_list, &shrinker_list);
269 spin_unlock(&shrinker_lock);
272 /* Remove a pool_type from the global shrinker list and free all pages */
273 static void ttm_pool_type_fini(struct ttm_pool_type *pt)
277 spin_lock(&shrinker_lock);
278 list_del(&pt->shrinker_list);
279 spin_unlock(&shrinker_lock);
281 while ((p = ttm_pool_type_take(pt)))
282 ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
285 /* Return the pool_type to use for the given caching and order */
286 static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
287 enum ttm_caching caching,
290 if (pool->use_dma_alloc)
291 return &pool->caching[caching].orders[order];
295 case ttm_write_combined:
297 return &global_dma32_write_combined[order];
299 return &global_write_combined[order];
302 return &global_dma32_uncached[order];
304 return &global_uncached[order];
313 /* Free pages using the global shrinker list */
314 static unsigned int ttm_pool_shrink(void)
316 struct ttm_pool_type *pt;
317 unsigned int num_pages;
320 spin_lock(&shrinker_lock);
321 pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list);
322 list_move_tail(&pt->shrinker_list, &shrinker_list);
323 spin_unlock(&shrinker_lock);
325 p = ttm_pool_type_take(pt);
327 ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
328 num_pages = 1 << pt->order;
336 /* Return the allocation order based for a page */
337 static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)
339 if (pool->use_dma_alloc) {
340 struct ttm_pool_dma *dma = (void *)p->private;
342 return dma->vaddr & ~PAGE_MASK;
348 /* Called when we got a page, either from a pool or newly allocated */
349 static int ttm_pool_page_allocated(struct ttm_pool *pool, unsigned int order,
350 struct page *p, dma_addr_t **dma_addr,
351 unsigned long *num_pages,
352 struct page ***pages)
358 r = ttm_pool_map(pool, order, p, dma_addr);
363 *num_pages -= 1 << order;
364 for (i = 1 << order; i; --i, ++(*pages), ++p)
371 * ttm_pool_alloc - Fill a ttm_tt object
373 * @pool: ttm_pool to use
374 * @tt: ttm_tt object to fill
375 * @ctx: operation context
377 * Fill the ttm_tt object with pages and also make sure to DMA map them when
380 * Returns: 0 on successe, negative error code otherwise.
382 int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
383 struct ttm_operation_ctx *ctx)
385 unsigned long num_pages = tt->num_pages;
386 dma_addr_t *dma_addr = tt->dma_address;
387 struct page **caching = tt->pages;
388 struct page **pages = tt->pages;
389 gfp_t gfp_flags = GFP_USER;
390 unsigned int i, order;
394 WARN_ON(!num_pages || ttm_tt_is_populated(tt));
395 WARN_ON(dma_addr && !pool->dev);
397 if (tt->page_flags & TTM_TT_FLAG_ZERO_ALLOC)
398 gfp_flags |= __GFP_ZERO;
400 if (ctx->gfp_retry_mayfail)
401 gfp_flags |= __GFP_RETRY_MAYFAIL;
404 gfp_flags |= GFP_DMA32;
406 gfp_flags |= GFP_HIGHUSER;
408 for (order = min_t(unsigned int, MAX_ORDER - 1, __fls(num_pages));
410 order = min_t(unsigned int, order, __fls(num_pages))) {
411 struct ttm_pool_type *pt;
413 pt = ttm_pool_select_type(pool, tt->caching, order);
414 p = pt ? ttm_pool_type_take(pt) : NULL;
416 r = ttm_pool_apply_caching(caching, pages,
419 goto error_free_page;
422 r = ttm_pool_page_allocated(pool, order, p,
427 goto error_free_page;
429 if (num_pages < (1 << order))
432 p = ttm_pool_type_take(pt);
437 while (num_pages >= (1 << order) &&
438 (p = ttm_pool_alloc_page(pool, gfp_flags, order))) {
440 if (PageHighMem(p)) {
441 r = ttm_pool_apply_caching(caching, pages,
444 goto error_free_page;
446 r = ttm_pool_page_allocated(pool, order, p, &dma_addr,
449 goto error_free_page;
464 r = ttm_pool_apply_caching(caching, pages, tt->caching);
471 ttm_pool_free_page(pool, tt->caching, order, p);
474 num_pages = tt->num_pages - num_pages;
475 for (i = 0; i < num_pages; ) {
476 order = ttm_pool_page_order(pool, tt->pages[i]);
477 ttm_pool_free_page(pool, tt->caching, order, tt->pages[i]);
483 EXPORT_SYMBOL(ttm_pool_alloc);
486 * ttm_pool_free - Free the backing pages from a ttm_tt object
488 * @pool: Pool to give pages back to.
489 * @tt: ttm_tt object to unpopulate
491 * Give the packing pages back to a pool or free them
493 void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
497 for (i = 0; i < tt->num_pages; ) {
498 struct page *p = tt->pages[i];
499 unsigned int order, num_pages;
500 struct ttm_pool_type *pt;
502 order = ttm_pool_page_order(pool, p);
503 num_pages = 1ULL << order;
505 ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
507 pt = ttm_pool_select_type(pool, tt->caching, order);
509 ttm_pool_type_give(pt, tt->pages[i]);
511 ttm_pool_free_page(pool, tt->caching, order,
517 while (atomic_long_read(&allocated_pages) > page_pool_size)
520 EXPORT_SYMBOL(ttm_pool_free);
523 * ttm_pool_init - Initialize a pool
525 * @pool: the pool to initialize
526 * @dev: device for DMA allocations and mappings
527 * @use_dma_alloc: true if coherent DMA alloc should be used
528 * @use_dma32: true if GFP_DMA32 should be used
530 * Initialize the pool and its pool types.
532 void ttm_pool_init(struct ttm_pool *pool, struct device *dev,
533 bool use_dma_alloc, bool use_dma32)
537 WARN_ON(!dev && use_dma_alloc);
540 pool->use_dma_alloc = use_dma_alloc;
541 pool->use_dma32 = use_dma32;
544 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
545 for (j = 0; j < MAX_ORDER; ++j)
546 ttm_pool_type_init(&pool->caching[i].orders[j],
552 * ttm_pool_fini - Cleanup a pool
554 * @pool: the pool to clean up
556 * Free all pages in the pool and unregister the types from the global
559 void ttm_pool_fini(struct ttm_pool *pool)
563 if (pool->use_dma_alloc) {
564 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
565 for (j = 0; j < MAX_ORDER; ++j)
566 ttm_pool_type_fini(&pool->caching[i].orders[j]);
569 /* We removed the pool types from the LRU, but we need to also make sure
570 * that no shrinker is concurrently freeing pages from the pool.
572 synchronize_shrinkers();
575 /* As long as pages are available make sure to release at least one */
576 static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
577 struct shrink_control *sc)
579 unsigned long num_freed = 0;
582 num_freed += ttm_pool_shrink();
583 while (!num_freed && atomic_long_read(&allocated_pages));
588 /* Return the number of pages available or SHRINK_EMPTY if we have none */
589 static unsigned long ttm_pool_shrinker_count(struct shrinker *shrink,
590 struct shrink_control *sc)
592 unsigned long num_pages = atomic_long_read(&allocated_pages);
594 return num_pages ? num_pages : SHRINK_EMPTY;
597 #ifdef CONFIG_DEBUG_FS
598 /* Count the number of pages available in a pool_type */
599 static unsigned int ttm_pool_type_count(struct ttm_pool_type *pt)
601 unsigned int count = 0;
604 spin_lock(&pt->lock);
605 /* Only used for debugfs, the overhead doesn't matter */
606 list_for_each_entry(p, &pt->pages, lru)
608 spin_unlock(&pt->lock);
613 /* Print a nice header for the order */
614 static void ttm_pool_debugfs_header(struct seq_file *m)
619 for (i = 0; i < MAX_ORDER; ++i)
620 seq_printf(m, " ---%2u---", i);
624 /* Dump information about the different pool types */
625 static void ttm_pool_debugfs_orders(struct ttm_pool_type *pt,
630 for (i = 0; i < MAX_ORDER; ++i)
631 seq_printf(m, " %8u", ttm_pool_type_count(&pt[i]));
635 /* Dump the total amount of allocated pages */
636 static void ttm_pool_debugfs_footer(struct seq_file *m)
638 seq_printf(m, "\ntotal\t: %8lu of %8lu\n",
639 atomic_long_read(&allocated_pages), page_pool_size);
642 /* Dump the information for the global pools */
643 static int ttm_pool_debugfs_globals_show(struct seq_file *m, void *data)
645 ttm_pool_debugfs_header(m);
647 spin_lock(&shrinker_lock);
648 seq_puts(m, "wc\t:");
649 ttm_pool_debugfs_orders(global_write_combined, m);
650 seq_puts(m, "uc\t:");
651 ttm_pool_debugfs_orders(global_uncached, m);
652 seq_puts(m, "wc 32\t:");
653 ttm_pool_debugfs_orders(global_dma32_write_combined, m);
654 seq_puts(m, "uc 32\t:");
655 ttm_pool_debugfs_orders(global_dma32_uncached, m);
656 spin_unlock(&shrinker_lock);
658 ttm_pool_debugfs_footer(m);
662 DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_globals);
665 * ttm_pool_debugfs - Debugfs dump function for a pool
667 * @pool: the pool to dump the information for
668 * @m: seq_file to dump to
670 * Make a debugfs dump with the per pool and global information.
672 int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
676 if (!pool->use_dma_alloc) {
677 seq_puts(m, "unused\n");
681 ttm_pool_debugfs_header(m);
683 spin_lock(&shrinker_lock);
684 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
690 case ttm_write_combined:
691 seq_puts(m, "wc\t:");
694 seq_puts(m, "uc\t:");
697 ttm_pool_debugfs_orders(pool->caching[i].orders, m);
699 spin_unlock(&shrinker_lock);
701 ttm_pool_debugfs_footer(m);
704 EXPORT_SYMBOL(ttm_pool_debugfs);
706 /* Test the shrinker functions and dump the result */
707 static int ttm_pool_debugfs_shrink_show(struct seq_file *m, void *data)
709 struct shrink_control sc = { .gfp_mask = GFP_NOFS };
711 fs_reclaim_acquire(GFP_KERNEL);
712 seq_printf(m, "%lu/%lu\n", ttm_pool_shrinker_count(&mm_shrinker, &sc),
713 ttm_pool_shrinker_scan(&mm_shrinker, &sc));
714 fs_reclaim_release(GFP_KERNEL);
718 DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_shrink);
723 * ttm_pool_mgr_init - Initialize globals
725 * @num_pages: default number of pages
727 * Initialize the global locks and lists for the MM shrinker.
729 int ttm_pool_mgr_init(unsigned long num_pages)
734 page_pool_size = num_pages;
736 spin_lock_init(&shrinker_lock);
737 INIT_LIST_HEAD(&shrinker_list);
739 for (i = 0; i < MAX_ORDER; ++i) {
740 ttm_pool_type_init(&global_write_combined[i], NULL,
741 ttm_write_combined, i);
742 ttm_pool_type_init(&global_uncached[i], NULL, ttm_uncached, i);
744 ttm_pool_type_init(&global_dma32_write_combined[i], NULL,
745 ttm_write_combined, i);
746 ttm_pool_type_init(&global_dma32_uncached[i], NULL,
750 #ifdef CONFIG_DEBUG_FS
751 debugfs_create_file("page_pool", 0444, ttm_debugfs_root, NULL,
752 &ttm_pool_debugfs_globals_fops);
753 debugfs_create_file("page_pool_shrink", 0400, ttm_debugfs_root, NULL,
754 &ttm_pool_debugfs_shrink_fops);
757 mm_shrinker.count_objects = ttm_pool_shrinker_count;
758 mm_shrinker.scan_objects = ttm_pool_shrinker_scan;
759 mm_shrinker.seeks = 1;
760 return register_shrinker(&mm_shrinker, "drm-ttm_pool");
764 * ttm_pool_mgr_fini - Finalize globals
766 * Cleanup the global pools and unregister the MM shrinker.
768 void ttm_pool_mgr_fini(void)
772 for (i = 0; i < MAX_ORDER; ++i) {
773 ttm_pool_type_fini(&global_write_combined[i]);
774 ttm_pool_type_fini(&global_uncached[i]);
776 ttm_pool_type_fini(&global_dma32_write_combined[i]);
777 ttm_pool_type_fini(&global_dma32_uncached[i]);
780 unregister_shrinker(&mm_shrinker);
781 WARN_ON(!list_empty(&shrinker_list));