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"
50 #define TTM_MAX_ORDER (PMD_SHIFT - PAGE_SHIFT)
51 #define __TTM_DIM_ORDER (TTM_MAX_ORDER + 1)
52 /* Some architectures have a weird PMD_SHIFT */
53 #define TTM_DIM_ORDER (__TTM_DIM_ORDER <= MAX_ORDER ? __TTM_DIM_ORDER : MAX_ORDER)
56 * struct ttm_pool_dma - Helper object for coherent DMA mappings
58 * @addr: original DMA address returned for the mapping
59 * @vaddr: original vaddr return for the mapping and order in the lower bits
66 static unsigned long page_pool_size;
68 MODULE_PARM_DESC(page_pool_size, "Number of pages in the WC/UC/DMA pool");
69 module_param(page_pool_size, ulong, 0644);
71 static atomic_long_t allocated_pages;
73 static struct ttm_pool_type global_write_combined[TTM_DIM_ORDER];
74 static struct ttm_pool_type global_uncached[TTM_DIM_ORDER];
76 static struct ttm_pool_type global_dma32_write_combined[TTM_DIM_ORDER];
77 static struct ttm_pool_type global_dma32_uncached[TTM_DIM_ORDER];
79 static spinlock_t shrinker_lock;
80 static struct list_head shrinker_list;
81 static struct shrinker mm_shrinker;
83 /* Allocate pages of size 1 << order with the given gfp_flags */
84 static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
87 unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
88 struct ttm_pool_dma *dma;
92 /* Don't set the __GFP_COMP flag for higher order allocations.
93 * Mapping pages directly into an userspace process and calling
94 * put_page() on a TTM allocated page is illegal.
97 gfp_flags |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN |
100 if (!pool->use_dma_alloc) {
101 p = alloc_pages(gfp_flags, order);
107 dma = kmalloc(sizeof(*dma), GFP_KERNEL);
112 attr |= DMA_ATTR_NO_WARN;
114 vaddr = dma_alloc_attrs(pool->dev, (1ULL << order) * PAGE_SIZE,
115 &dma->addr, gfp_flags, attr);
119 /* TODO: This is an illegal abuse of the DMA API, but we need to rework
120 * TTM page fault handling and extend the DMA API to clean this up.
122 if (is_vmalloc_addr(vaddr))
123 p = vmalloc_to_page(vaddr);
125 p = virt_to_page(vaddr);
127 dma->vaddr = (unsigned long)vaddr | order;
128 p->private = (unsigned long)dma;
136 /* Reset the caching and pages of size 1 << order */
137 static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
138 unsigned int order, struct page *p)
140 unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
141 struct ttm_pool_dma *dma;
145 /* We don't care that set_pages_wb is inefficient here. This is only
146 * used when we have to shrink and CPU overhead is irrelevant then.
148 if (caching != ttm_cached && !PageHighMem(p))
149 set_pages_wb(p, 1 << order);
152 if (!pool || !pool->use_dma_alloc) {
153 __free_pages(p, order);
158 attr |= DMA_ATTR_NO_WARN;
160 dma = (void *)p->private;
161 vaddr = (void *)(dma->vaddr & PAGE_MASK);
162 dma_free_attrs(pool->dev, (1UL << order) * PAGE_SIZE, vaddr, dma->addr,
167 /* Apply a new caching to an array of pages */
168 static int ttm_pool_apply_caching(struct page **first, struct page **last,
169 enum ttm_caching caching)
172 unsigned int num_pages = last - first;
180 case ttm_write_combined:
181 return set_pages_array_wc(first, num_pages);
183 return set_pages_array_uc(first, num_pages);
189 /* Map pages of 1 << order size and fill the DMA address array */
190 static int ttm_pool_map(struct ttm_pool *pool, unsigned int order,
191 struct page *p, dma_addr_t **dma_addr)
196 if (pool->use_dma_alloc) {
197 struct ttm_pool_dma *dma = (void *)p->private;
201 size_t size = (1ULL << order) * PAGE_SIZE;
203 addr = dma_map_page(pool->dev, p, 0, size, DMA_BIDIRECTIONAL);
204 if (dma_mapping_error(pool->dev, addr))
208 for (i = 1 << order; i ; --i) {
209 *(*dma_addr)++ = addr;
216 /* Unmap pages of 1 << order size */
217 static void ttm_pool_unmap(struct ttm_pool *pool, dma_addr_t dma_addr,
218 unsigned int num_pages)
220 /* Unmapped while freeing the page */
221 if (pool->use_dma_alloc)
224 dma_unmap_page(pool->dev, dma_addr, (long)num_pages << PAGE_SHIFT,
228 /* Give pages into a specific pool_type */
229 static void ttm_pool_type_give(struct ttm_pool_type *pt, struct page *p)
231 unsigned int i, num_pages = 1 << pt->order;
233 for (i = 0; i < num_pages; ++i) {
235 clear_highpage(p + i);
237 clear_page(page_address(p + i));
240 spin_lock(&pt->lock);
241 list_add(&p->lru, &pt->pages);
242 spin_unlock(&pt->lock);
243 atomic_long_add(1 << pt->order, &allocated_pages);
246 /* Take pages from a specific pool_type, return NULL when nothing available */
247 static struct page *ttm_pool_type_take(struct ttm_pool_type *pt)
251 spin_lock(&pt->lock);
252 p = list_first_entry_or_null(&pt->pages, typeof(*p), lru);
254 atomic_long_sub(1 << pt->order, &allocated_pages);
257 spin_unlock(&pt->lock);
262 /* Initialize and add a pool type to the global shrinker list */
263 static void ttm_pool_type_init(struct ttm_pool_type *pt, struct ttm_pool *pool,
264 enum ttm_caching caching, unsigned int order)
267 pt->caching = caching;
269 spin_lock_init(&pt->lock);
270 INIT_LIST_HEAD(&pt->pages);
272 spin_lock(&shrinker_lock);
273 list_add_tail(&pt->shrinker_list, &shrinker_list);
274 spin_unlock(&shrinker_lock);
277 /* Remove a pool_type from the global shrinker list and free all pages */
278 static void ttm_pool_type_fini(struct ttm_pool_type *pt)
282 spin_lock(&shrinker_lock);
283 list_del(&pt->shrinker_list);
284 spin_unlock(&shrinker_lock);
286 while ((p = ttm_pool_type_take(pt)))
287 ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
290 /* Return the pool_type to use for the given caching and order */
291 static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
292 enum ttm_caching caching,
295 if (pool->use_dma_alloc)
296 return &pool->caching[caching].orders[order];
300 case ttm_write_combined:
302 return &global_dma32_write_combined[order];
304 return &global_write_combined[order];
307 return &global_dma32_uncached[order];
309 return &global_uncached[order];
318 /* Free pages using the global shrinker list */
319 static unsigned int ttm_pool_shrink(void)
321 struct ttm_pool_type *pt;
322 unsigned int num_pages;
325 spin_lock(&shrinker_lock);
326 pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list);
327 list_move_tail(&pt->shrinker_list, &shrinker_list);
328 spin_unlock(&shrinker_lock);
330 p = ttm_pool_type_take(pt);
332 ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
333 num_pages = 1 << pt->order;
341 /* Return the allocation order based for a page */
342 static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)
344 if (pool->use_dma_alloc) {
345 struct ttm_pool_dma *dma = (void *)p->private;
347 return dma->vaddr & ~PAGE_MASK;
353 /* Called when we got a page, either from a pool or newly allocated */
354 static int ttm_pool_page_allocated(struct ttm_pool *pool, unsigned int order,
355 struct page *p, dma_addr_t **dma_addr,
356 unsigned long *num_pages,
357 struct page ***pages)
363 r = ttm_pool_map(pool, order, p, dma_addr);
368 *num_pages -= 1 << order;
369 for (i = 1 << order; i; --i, ++(*pages), ++p)
376 * ttm_pool_free_range() - Free a range of TTM pages
377 * @pool: The pool used for allocating.
378 * @tt: The struct ttm_tt holding the page pointers.
379 * @caching: The page caching mode used by the range.
380 * @start_page: index for first page to free.
381 * @end_page: index for last page to free + 1.
383 * During allocation the ttm_tt page-vector may be populated with ranges of
384 * pages with different attributes if allocation hit an error without being
385 * able to completely fulfill the allocation. This function can be used
386 * to free these individual ranges.
388 static void ttm_pool_free_range(struct ttm_pool *pool, struct ttm_tt *tt,
389 enum ttm_caching caching,
390 pgoff_t start_page, pgoff_t end_page)
392 struct page **pages = tt->pages;
396 for (i = start_page; i < end_page; i += nr, pages += nr) {
397 struct ttm_pool_type *pt = NULL;
399 order = ttm_pool_page_order(pool, *pages);
402 ttm_pool_unmap(pool, tt->dma_address[i], nr);
404 pt = ttm_pool_select_type(pool, caching, order);
406 ttm_pool_type_give(pt, *pages);
408 ttm_pool_free_page(pool, caching, order, *pages);
413 * ttm_pool_alloc - Fill a ttm_tt object
415 * @pool: ttm_pool to use
416 * @tt: ttm_tt object to fill
417 * @ctx: operation context
419 * Fill the ttm_tt object with pages and also make sure to DMA map them when
422 * Returns: 0 on successe, negative error code otherwise.
424 int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
425 struct ttm_operation_ctx *ctx)
427 pgoff_t num_pages = tt->num_pages;
428 dma_addr_t *dma_addr = tt->dma_address;
429 struct page **caching = tt->pages;
430 struct page **pages = tt->pages;
431 enum ttm_caching page_caching;
432 gfp_t gfp_flags = GFP_USER;
433 pgoff_t caching_divide;
438 WARN_ON(!num_pages || ttm_tt_is_populated(tt));
439 WARN_ON(dma_addr && !pool->dev);
441 if (tt->page_flags & TTM_TT_FLAG_ZERO_ALLOC)
442 gfp_flags |= __GFP_ZERO;
444 if (ctx->gfp_retry_mayfail)
445 gfp_flags |= __GFP_RETRY_MAYFAIL;
448 gfp_flags |= GFP_DMA32;
450 gfp_flags |= GFP_HIGHUSER;
452 for (order = min_t(unsigned int, TTM_MAX_ORDER, __fls(num_pages));
454 order = min_t(unsigned int, order, __fls(num_pages))) {
455 struct ttm_pool_type *pt;
457 page_caching = tt->caching;
458 pt = ttm_pool_select_type(pool, tt->caching, order);
459 p = pt ? ttm_pool_type_take(pt) : NULL;
461 r = ttm_pool_apply_caching(caching, pages,
464 goto error_free_page;
468 r = ttm_pool_page_allocated(pool, order, p,
473 goto error_free_page;
476 if (num_pages < (1 << order))
479 p = ttm_pool_type_take(pt);
483 page_caching = ttm_cached;
484 while (num_pages >= (1 << order) &&
485 (p = ttm_pool_alloc_page(pool, gfp_flags, order))) {
487 if (PageHighMem(p)) {
488 r = ttm_pool_apply_caching(caching, pages,
491 goto error_free_page;
494 r = ttm_pool_page_allocated(pool, order, p, &dma_addr,
497 goto error_free_page;
512 r = ttm_pool_apply_caching(caching, pages, tt->caching);
519 ttm_pool_free_page(pool, page_caching, order, p);
522 num_pages = tt->num_pages - num_pages;
523 caching_divide = caching - tt->pages;
524 ttm_pool_free_range(pool, tt, tt->caching, 0, caching_divide);
525 ttm_pool_free_range(pool, tt, ttm_cached, caching_divide, num_pages);
529 EXPORT_SYMBOL(ttm_pool_alloc);
532 * ttm_pool_free - Free the backing pages from a ttm_tt object
534 * @pool: Pool to give pages back to.
535 * @tt: ttm_tt object to unpopulate
537 * Give the packing pages back to a pool or free them
539 void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
541 ttm_pool_free_range(pool, tt, tt->caching, 0, tt->num_pages);
543 while (atomic_long_read(&allocated_pages) > page_pool_size)
546 EXPORT_SYMBOL(ttm_pool_free);
549 * ttm_pool_init - Initialize a pool
551 * @pool: the pool to initialize
552 * @dev: device for DMA allocations and mappings
553 * @use_dma_alloc: true if coherent DMA alloc should be used
554 * @use_dma32: true if GFP_DMA32 should be used
556 * Initialize the pool and its pool types.
558 void ttm_pool_init(struct ttm_pool *pool, struct device *dev,
559 bool use_dma_alloc, bool use_dma32)
563 WARN_ON(!dev && use_dma_alloc);
566 pool->use_dma_alloc = use_dma_alloc;
567 pool->use_dma32 = use_dma32;
570 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
571 for (j = 0; j < TTM_DIM_ORDER; ++j)
572 ttm_pool_type_init(&pool->caching[i].orders[j],
578 * ttm_pool_fini - Cleanup a pool
580 * @pool: the pool to clean up
582 * Free all pages in the pool and unregister the types from the global
585 void ttm_pool_fini(struct ttm_pool *pool)
589 if (pool->use_dma_alloc) {
590 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
591 for (j = 0; j < TTM_DIM_ORDER; ++j)
592 ttm_pool_type_fini(&pool->caching[i].orders[j]);
595 /* We removed the pool types from the LRU, but we need to also make sure
596 * that no shrinker is concurrently freeing pages from the pool.
598 synchronize_shrinkers();
601 /* As long as pages are available make sure to release at least one */
602 static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
603 struct shrink_control *sc)
605 unsigned long num_freed = 0;
608 num_freed += ttm_pool_shrink();
609 while (!num_freed && atomic_long_read(&allocated_pages));
614 /* Return the number of pages available or SHRINK_EMPTY if we have none */
615 static unsigned long ttm_pool_shrinker_count(struct shrinker *shrink,
616 struct shrink_control *sc)
618 unsigned long num_pages = atomic_long_read(&allocated_pages);
620 return num_pages ? num_pages : SHRINK_EMPTY;
623 #ifdef CONFIG_DEBUG_FS
624 /* Count the number of pages available in a pool_type */
625 static unsigned int ttm_pool_type_count(struct ttm_pool_type *pt)
627 unsigned int count = 0;
630 spin_lock(&pt->lock);
631 /* Only used for debugfs, the overhead doesn't matter */
632 list_for_each_entry(p, &pt->pages, lru)
634 spin_unlock(&pt->lock);
639 /* Print a nice header for the order */
640 static void ttm_pool_debugfs_header(struct seq_file *m)
645 for (i = 0; i < TTM_DIM_ORDER; ++i)
646 seq_printf(m, " ---%2u---", i);
650 /* Dump information about the different pool types */
651 static void ttm_pool_debugfs_orders(struct ttm_pool_type *pt,
656 for (i = 0; i < TTM_DIM_ORDER; ++i)
657 seq_printf(m, " %8u", ttm_pool_type_count(&pt[i]));
661 /* Dump the total amount of allocated pages */
662 static void ttm_pool_debugfs_footer(struct seq_file *m)
664 seq_printf(m, "\ntotal\t: %8lu of %8lu\n",
665 atomic_long_read(&allocated_pages), page_pool_size);
668 /* Dump the information for the global pools */
669 static int ttm_pool_debugfs_globals_show(struct seq_file *m, void *data)
671 ttm_pool_debugfs_header(m);
673 spin_lock(&shrinker_lock);
674 seq_puts(m, "wc\t:");
675 ttm_pool_debugfs_orders(global_write_combined, m);
676 seq_puts(m, "uc\t:");
677 ttm_pool_debugfs_orders(global_uncached, m);
678 seq_puts(m, "wc 32\t:");
679 ttm_pool_debugfs_orders(global_dma32_write_combined, m);
680 seq_puts(m, "uc 32\t:");
681 ttm_pool_debugfs_orders(global_dma32_uncached, m);
682 spin_unlock(&shrinker_lock);
684 ttm_pool_debugfs_footer(m);
688 DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_globals);
691 * ttm_pool_debugfs - Debugfs dump function for a pool
693 * @pool: the pool to dump the information for
694 * @m: seq_file to dump to
696 * Make a debugfs dump with the per pool and global information.
698 int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
702 if (!pool->use_dma_alloc) {
703 seq_puts(m, "unused\n");
707 ttm_pool_debugfs_header(m);
709 spin_lock(&shrinker_lock);
710 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
716 case ttm_write_combined:
717 seq_puts(m, "wc\t:");
720 seq_puts(m, "uc\t:");
723 ttm_pool_debugfs_orders(pool->caching[i].orders, m);
725 spin_unlock(&shrinker_lock);
727 ttm_pool_debugfs_footer(m);
730 EXPORT_SYMBOL(ttm_pool_debugfs);
732 /* Test the shrinker functions and dump the result */
733 static int ttm_pool_debugfs_shrink_show(struct seq_file *m, void *data)
735 struct shrink_control sc = { .gfp_mask = GFP_NOFS };
737 fs_reclaim_acquire(GFP_KERNEL);
738 seq_printf(m, "%lu/%lu\n", ttm_pool_shrinker_count(&mm_shrinker, &sc),
739 ttm_pool_shrinker_scan(&mm_shrinker, &sc));
740 fs_reclaim_release(GFP_KERNEL);
744 DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_shrink);
749 * ttm_pool_mgr_init - Initialize globals
751 * @num_pages: default number of pages
753 * Initialize the global locks and lists for the MM shrinker.
755 int ttm_pool_mgr_init(unsigned long num_pages)
759 BUILD_BUG_ON(TTM_DIM_ORDER > MAX_ORDER);
760 BUILD_BUG_ON(TTM_DIM_ORDER < 1);
763 page_pool_size = num_pages;
765 spin_lock_init(&shrinker_lock);
766 INIT_LIST_HEAD(&shrinker_list);
768 for (i = 0; i < TTM_DIM_ORDER; ++i) {
769 ttm_pool_type_init(&global_write_combined[i], NULL,
770 ttm_write_combined, i);
771 ttm_pool_type_init(&global_uncached[i], NULL, ttm_uncached, i);
773 ttm_pool_type_init(&global_dma32_write_combined[i], NULL,
774 ttm_write_combined, i);
775 ttm_pool_type_init(&global_dma32_uncached[i], NULL,
779 #ifdef CONFIG_DEBUG_FS
780 debugfs_create_file("page_pool", 0444, ttm_debugfs_root, NULL,
781 &ttm_pool_debugfs_globals_fops);
782 debugfs_create_file("page_pool_shrink", 0400, ttm_debugfs_root, NULL,
783 &ttm_pool_debugfs_shrink_fops);
786 mm_shrinker.count_objects = ttm_pool_shrinker_count;
787 mm_shrinker.scan_objects = ttm_pool_shrinker_scan;
788 mm_shrinker.seeks = 1;
789 return register_shrinker(&mm_shrinker, "drm-ttm_pool");
793 * ttm_pool_mgr_fini - Finalize globals
795 * Cleanup the global pools and unregister the MM shrinker.
797 void ttm_pool_mgr_fini(void)
801 for (i = 0; i < TTM_DIM_ORDER; ++i) {
802 ttm_pool_type_fini(&global_write_combined[i]);
803 ttm_pool_type_fini(&global_uncached[i]);
805 ttm_pool_type_fini(&global_dma32_write_combined[i]);
806 ttm_pool_type_fini(&global_dma32_uncached[i]);
809 unregister_shrinker(&mm_shrinker);
810 WARN_ON(!list_empty(&shrinker_list));