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/highmem.h>
37 #include <linux/sched/mm.h>
40 #include <asm/set_memory.h>
43 #include <drm/ttm/ttm_pool.h>
44 #include <drm/ttm/ttm_bo_driver.h>
45 #include <drm/ttm/ttm_tt.h>
47 #include "ttm_module.h"
50 * struct ttm_pool_dma - Helper object for coherent DMA mappings
52 * @addr: original DMA address returned for the mapping
53 * @vaddr: original vaddr return for the mapping and order in the lower bits
60 static unsigned long page_pool_size;
62 MODULE_PARM_DESC(page_pool_size, "Number of pages in the WC/UC/DMA pool");
63 module_param(page_pool_size, ulong, 0644);
65 static atomic_long_t allocated_pages;
67 static struct ttm_pool_type global_write_combined[MAX_ORDER];
68 static struct ttm_pool_type global_uncached[MAX_ORDER];
70 static struct ttm_pool_type global_dma32_write_combined[MAX_ORDER];
71 static struct ttm_pool_type global_dma32_uncached[MAX_ORDER];
73 static spinlock_t shrinker_lock;
74 static struct list_head shrinker_list;
75 static struct shrinker mm_shrinker;
77 /* Allocate pages of size 1 << order with the given gfp_flags */
78 static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
81 unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
82 struct ttm_pool_dma *dma;
86 /* Don't set the __GFP_COMP flag for higher order allocations.
87 * Mapping pages directly into an userspace process and calling
88 * put_page() on a TTM allocated page is illegal.
91 gfp_flags |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN |
94 if (!pool->use_dma_alloc) {
95 p = alloc_pages(gfp_flags, order);
101 dma = kmalloc(sizeof(*dma), GFP_KERNEL);
106 attr |= DMA_ATTR_NO_WARN;
108 vaddr = dma_alloc_attrs(pool->dev, (1ULL << order) * PAGE_SIZE,
109 &dma->addr, gfp_flags, attr);
113 /* TODO: This is an illegal abuse of the DMA API, but we need to rework
114 * TTM page fault handling and extend the DMA API to clean this up.
116 if (is_vmalloc_addr(vaddr))
117 p = vmalloc_to_page(vaddr);
119 p = virt_to_page(vaddr);
121 dma->vaddr = (unsigned long)vaddr | order;
122 p->private = (unsigned long)dma;
130 /* Reset the caching and pages of size 1 << order */
131 static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
132 unsigned int order, struct page *p)
134 unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
135 struct ttm_pool_dma *dma;
139 /* We don't care that set_pages_wb is inefficient here. This is only
140 * used when we have to shrink and CPU overhead is irrelevant then.
142 if (caching != ttm_cached && !PageHighMem(p))
143 set_pages_wb(p, 1 << order);
146 if (!pool || !pool->use_dma_alloc) {
147 __free_pages(p, order);
152 attr |= DMA_ATTR_NO_WARN;
154 dma = (void *)p->private;
155 vaddr = (void *)(dma->vaddr & PAGE_MASK);
156 dma_free_attrs(pool->dev, (1UL << order) * PAGE_SIZE, vaddr, dma->addr,
161 /* Apply a new caching to an array of pages */
162 static int ttm_pool_apply_caching(struct page **first, struct page **last,
163 enum ttm_caching caching)
166 unsigned int num_pages = last - first;
174 case ttm_write_combined:
175 return set_pages_array_wc(first, num_pages);
177 return set_pages_array_uc(first, num_pages);
183 /* Map pages of 1 << order size and fill the DMA address array */
184 static int ttm_pool_map(struct ttm_pool *pool, unsigned int order,
185 struct page *p, dma_addr_t **dma_addr)
190 if (pool->use_dma_alloc) {
191 struct ttm_pool_dma *dma = (void *)p->private;
195 size_t size = (1ULL << order) * PAGE_SIZE;
197 addr = dma_map_page(pool->dev, p, 0, size, DMA_BIDIRECTIONAL);
198 if (dma_mapping_error(pool->dev, addr))
202 for (i = 1 << order; i ; --i) {
203 *(*dma_addr)++ = addr;
210 /* Unmap pages of 1 << order size */
211 static void ttm_pool_unmap(struct ttm_pool *pool, dma_addr_t dma_addr,
212 unsigned int num_pages)
214 /* Unmapped while freeing the page */
215 if (pool->use_dma_alloc)
218 dma_unmap_page(pool->dev, dma_addr, (long)num_pages << PAGE_SHIFT,
222 /* Give pages into a specific pool_type */
223 static void ttm_pool_type_give(struct ttm_pool_type *pt, struct page *p)
225 unsigned int i, num_pages = 1 << pt->order;
227 for (i = 0; i < num_pages; ++i) {
229 clear_highpage(p + i);
231 clear_page(page_address(p + i));
234 spin_lock(&pt->lock);
235 list_add(&p->lru, &pt->pages);
236 spin_unlock(&pt->lock);
237 atomic_long_add(1 << pt->order, &allocated_pages);
240 /* Take pages from a specific pool_type, return NULL when nothing available */
241 static struct page *ttm_pool_type_take(struct ttm_pool_type *pt)
245 spin_lock(&pt->lock);
246 p = list_first_entry_or_null(&pt->pages, typeof(*p), lru);
248 atomic_long_sub(1 << pt->order, &allocated_pages);
251 spin_unlock(&pt->lock);
256 /* Initialize and add a pool type to the global shrinker list */
257 static void ttm_pool_type_init(struct ttm_pool_type *pt, struct ttm_pool *pool,
258 enum ttm_caching caching, unsigned int order)
261 pt->caching = caching;
263 spin_lock_init(&pt->lock);
264 INIT_LIST_HEAD(&pt->pages);
266 spin_lock(&shrinker_lock);
267 list_add_tail(&pt->shrinker_list, &shrinker_list);
268 spin_unlock(&shrinker_lock);
271 /* Remove a pool_type from the global shrinker list and free all pages */
272 static void ttm_pool_type_fini(struct ttm_pool_type *pt)
276 spin_lock(&shrinker_lock);
277 list_del(&pt->shrinker_list);
278 spin_unlock(&shrinker_lock);
280 while ((p = ttm_pool_type_take(pt)))
281 ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
284 /* Return the pool_type to use for the given caching and order */
285 static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
286 enum ttm_caching caching,
289 if (pool->use_dma_alloc)
290 return &pool->caching[caching].orders[order];
294 case ttm_write_combined:
296 return &global_dma32_write_combined[order];
298 return &global_write_combined[order];
301 return &global_dma32_uncached[order];
303 return &global_uncached[order];
312 /* Free pages using the global shrinker list */
313 static unsigned int ttm_pool_shrink(void)
315 struct ttm_pool_type *pt;
316 unsigned int num_pages;
319 spin_lock(&shrinker_lock);
320 pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list);
321 list_move_tail(&pt->shrinker_list, &shrinker_list);
322 spin_unlock(&shrinker_lock);
324 p = ttm_pool_type_take(pt);
326 ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
327 num_pages = 1 << pt->order;
335 /* Return the allocation order based for a page */
336 static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)
338 if (pool->use_dma_alloc) {
339 struct ttm_pool_dma *dma = (void *)p->private;
341 return dma->vaddr & ~PAGE_MASK;
348 * ttm_pool_alloc - Fill a ttm_tt object
350 * @pool: ttm_pool to use
351 * @tt: ttm_tt object to fill
352 * @ctx: operation context
354 * Fill the ttm_tt object with pages and also make sure to DMA map them when
357 * Returns: 0 on successe, negative error code otherwise.
359 int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
360 struct ttm_operation_ctx *ctx)
362 unsigned long num_pages = tt->num_pages;
363 dma_addr_t *dma_addr = tt->dma_address;
364 struct page **caching = tt->pages;
365 struct page **pages = tt->pages;
366 gfp_t gfp_flags = GFP_USER;
367 unsigned int i, order;
371 WARN_ON(!num_pages || ttm_tt_is_populated(tt));
372 WARN_ON(dma_addr && !pool->dev);
374 if (tt->page_flags & TTM_TT_FLAG_ZERO_ALLOC)
375 gfp_flags |= __GFP_ZERO;
377 if (ctx->gfp_retry_mayfail)
378 gfp_flags |= __GFP_RETRY_MAYFAIL;
381 gfp_flags |= GFP_DMA32;
383 gfp_flags |= GFP_HIGHUSER;
385 for (order = min_t(unsigned int, MAX_ORDER - 1, __fls(num_pages));
387 order = min_t(unsigned int, order, __fls(num_pages))) {
388 bool apply_caching = false;
389 struct ttm_pool_type *pt;
391 pt = ttm_pool_select_type(pool, tt->caching, order);
392 p = pt ? ttm_pool_type_take(pt) : NULL;
394 apply_caching = true;
396 p = ttm_pool_alloc_page(pool, gfp_flags, order);
397 if (p && PageHighMem(p))
398 apply_caching = true;
411 r = ttm_pool_apply_caching(caching, pages,
414 goto error_free_page;
415 caching = pages + (1 << order);
419 r = ttm_pool_map(pool, order, p, &dma_addr);
421 goto error_free_page;
424 num_pages -= 1 << order;
425 for (i = 1 << order; i; --i)
429 r = ttm_pool_apply_caching(caching, pages, tt->caching);
436 ttm_pool_free_page(pool, tt->caching, order, p);
439 num_pages = tt->num_pages - num_pages;
440 for (i = 0; i < num_pages; ) {
441 order = ttm_pool_page_order(pool, tt->pages[i]);
442 ttm_pool_free_page(pool, tt->caching, order, tt->pages[i]);
448 EXPORT_SYMBOL(ttm_pool_alloc);
451 * ttm_pool_free - Free the backing pages from a ttm_tt object
453 * @pool: Pool to give pages back to.
454 * @tt: ttm_tt object to unpopulate
456 * Give the packing pages back to a pool or free them
458 void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
462 for (i = 0; i < tt->num_pages; ) {
463 struct page *p = tt->pages[i];
464 unsigned int order, num_pages;
465 struct ttm_pool_type *pt;
467 order = ttm_pool_page_order(pool, p);
468 num_pages = 1ULL << order;
470 ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
472 pt = ttm_pool_select_type(pool, tt->caching, order);
474 ttm_pool_type_give(pt, tt->pages[i]);
476 ttm_pool_free_page(pool, tt->caching, order,
482 while (atomic_long_read(&allocated_pages) > page_pool_size)
485 EXPORT_SYMBOL(ttm_pool_free);
488 * ttm_pool_init - Initialize a pool
490 * @pool: the pool to initialize
491 * @dev: device for DMA allocations and mappings
492 * @use_dma_alloc: true if coherent DMA alloc should be used
493 * @use_dma32: true if GFP_DMA32 should be used
495 * Initialize the pool and its pool types.
497 void ttm_pool_init(struct ttm_pool *pool, struct device *dev,
498 bool use_dma_alloc, bool use_dma32)
502 WARN_ON(!dev && use_dma_alloc);
505 pool->use_dma_alloc = use_dma_alloc;
506 pool->use_dma32 = use_dma32;
509 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
510 for (j = 0; j < MAX_ORDER; ++j)
511 ttm_pool_type_init(&pool->caching[i].orders[j],
517 * ttm_pool_fini - Cleanup a pool
519 * @pool: the pool to clean up
521 * Free all pages in the pool and unregister the types from the global
524 void ttm_pool_fini(struct ttm_pool *pool)
528 if (pool->use_dma_alloc) {
529 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
530 for (j = 0; j < MAX_ORDER; ++j)
531 ttm_pool_type_fini(&pool->caching[i].orders[j]);
534 /* We removed the pool types from the LRU, but we need to also make sure
535 * that no shrinker is concurrently freeing pages from the pool.
537 synchronize_shrinkers();
540 /* As long as pages are available make sure to release at least one */
541 static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
542 struct shrink_control *sc)
544 unsigned long num_freed = 0;
547 num_freed += ttm_pool_shrink();
548 while (!num_freed && atomic_long_read(&allocated_pages));
553 /* Return the number of pages available or SHRINK_EMPTY if we have none */
554 static unsigned long ttm_pool_shrinker_count(struct shrinker *shrink,
555 struct shrink_control *sc)
557 unsigned long num_pages = atomic_long_read(&allocated_pages);
559 return num_pages ? num_pages : SHRINK_EMPTY;
562 #ifdef CONFIG_DEBUG_FS
563 /* Count the number of pages available in a pool_type */
564 static unsigned int ttm_pool_type_count(struct ttm_pool_type *pt)
566 unsigned int count = 0;
569 spin_lock(&pt->lock);
570 /* Only used for debugfs, the overhead doesn't matter */
571 list_for_each_entry(p, &pt->pages, lru)
573 spin_unlock(&pt->lock);
578 /* Print a nice header for the order */
579 static void ttm_pool_debugfs_header(struct seq_file *m)
584 for (i = 0; i < MAX_ORDER; ++i)
585 seq_printf(m, " ---%2u---", i);
589 /* Dump information about the different pool types */
590 static void ttm_pool_debugfs_orders(struct ttm_pool_type *pt,
595 for (i = 0; i < MAX_ORDER; ++i)
596 seq_printf(m, " %8u", ttm_pool_type_count(&pt[i]));
600 /* Dump the total amount of allocated pages */
601 static void ttm_pool_debugfs_footer(struct seq_file *m)
603 seq_printf(m, "\ntotal\t: %8lu of %8lu\n",
604 atomic_long_read(&allocated_pages), page_pool_size);
607 /* Dump the information for the global pools */
608 static int ttm_pool_debugfs_globals_show(struct seq_file *m, void *data)
610 ttm_pool_debugfs_header(m);
612 spin_lock(&shrinker_lock);
613 seq_puts(m, "wc\t:");
614 ttm_pool_debugfs_orders(global_write_combined, m);
615 seq_puts(m, "uc\t:");
616 ttm_pool_debugfs_orders(global_uncached, m);
617 seq_puts(m, "wc 32\t:");
618 ttm_pool_debugfs_orders(global_dma32_write_combined, m);
619 seq_puts(m, "uc 32\t:");
620 ttm_pool_debugfs_orders(global_dma32_uncached, m);
621 spin_unlock(&shrinker_lock);
623 ttm_pool_debugfs_footer(m);
627 DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_globals);
630 * ttm_pool_debugfs - Debugfs dump function for a pool
632 * @pool: the pool to dump the information for
633 * @m: seq_file to dump to
635 * Make a debugfs dump with the per pool and global information.
637 int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
641 if (!pool->use_dma_alloc) {
642 seq_puts(m, "unused\n");
646 ttm_pool_debugfs_header(m);
648 spin_lock(&shrinker_lock);
649 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
655 case ttm_write_combined:
656 seq_puts(m, "wc\t:");
659 seq_puts(m, "uc\t:");
662 ttm_pool_debugfs_orders(pool->caching[i].orders, m);
664 spin_unlock(&shrinker_lock);
666 ttm_pool_debugfs_footer(m);
669 EXPORT_SYMBOL(ttm_pool_debugfs);
671 /* Test the shrinker functions and dump the result */
672 static int ttm_pool_debugfs_shrink_show(struct seq_file *m, void *data)
674 struct shrink_control sc = { .gfp_mask = GFP_NOFS };
676 fs_reclaim_acquire(GFP_KERNEL);
677 seq_printf(m, "%lu/%lu\n", ttm_pool_shrinker_count(&mm_shrinker, &sc),
678 ttm_pool_shrinker_scan(&mm_shrinker, &sc));
679 fs_reclaim_release(GFP_KERNEL);
683 DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_shrink);
688 * ttm_pool_mgr_init - Initialize globals
690 * @num_pages: default number of pages
692 * Initialize the global locks and lists for the MM shrinker.
694 int ttm_pool_mgr_init(unsigned long num_pages)
699 page_pool_size = num_pages;
701 spin_lock_init(&shrinker_lock);
702 INIT_LIST_HEAD(&shrinker_list);
704 for (i = 0; i < MAX_ORDER; ++i) {
705 ttm_pool_type_init(&global_write_combined[i], NULL,
706 ttm_write_combined, i);
707 ttm_pool_type_init(&global_uncached[i], NULL, ttm_uncached, i);
709 ttm_pool_type_init(&global_dma32_write_combined[i], NULL,
710 ttm_write_combined, i);
711 ttm_pool_type_init(&global_dma32_uncached[i], NULL,
715 #ifdef CONFIG_DEBUG_FS
716 debugfs_create_file("page_pool", 0444, ttm_debugfs_root, NULL,
717 &ttm_pool_debugfs_globals_fops);
718 debugfs_create_file("page_pool_shrink", 0400, ttm_debugfs_root, NULL,
719 &ttm_pool_debugfs_shrink_fops);
722 mm_shrinker.count_objects = ttm_pool_shrinker_count;
723 mm_shrinker.scan_objects = ttm_pool_shrinker_scan;
724 mm_shrinker.seeks = 1;
725 return register_shrinker(&mm_shrinker, "drm-ttm_pool");
729 * ttm_pool_mgr_fini - Finalize globals
731 * Cleanup the global pools and unregister the MM shrinker.
733 void ttm_pool_mgr_fini(void)
737 for (i = 0; i < MAX_ORDER; ++i) {
738 ttm_pool_type_fini(&global_write_combined[i]);
739 ttm_pool_type_fini(&global_uncached[i]);
741 ttm_pool_type_fini(&global_dma32_write_combined[i]);
742 ttm_pool_type_fini(&global_dma32_uncached[i]);
745 unregister_shrinker(&mm_shrinker);
746 WARN_ON(!list_empty(&shrinker_list));