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>
38 #include <asm/set_memory.h>
41 #include <drm/ttm/ttm_pool.h>
42 #include <drm/ttm/ttm_bo_driver.h>
43 #include <drm/ttm/ttm_tt.h>
46 * struct ttm_pool_dma - Helper object for coherent DMA mappings
48 * @addr: original DMA address returned for the mapping
49 * @vaddr: original vaddr return for the mapping and order in the lower bits
56 static unsigned long page_pool_size;
58 MODULE_PARM_DESC(page_pool_size, "Number of pages in the WC/UC/DMA pool");
59 module_param(page_pool_size, ulong, 0644);
61 static atomic_long_t allocated_pages;
63 static struct ttm_pool_type global_write_combined[MAX_ORDER];
64 static struct ttm_pool_type global_uncached[MAX_ORDER];
66 static struct ttm_pool_type global_dma32_write_combined[MAX_ORDER];
67 static struct ttm_pool_type global_dma32_uncached[MAX_ORDER];
69 static spinlock_t shrinker_lock;
70 static struct list_head shrinker_list;
71 static struct shrinker mm_shrinker;
73 /* Allocate pages of size 1 << order with the given gfp_flags */
74 static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
77 unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
78 struct ttm_pool_dma *dma;
83 gfp_flags |= GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
85 gfp_flags &= ~__GFP_MOVABLE;
86 gfp_flags &= ~__GFP_COMP;
89 if (!pool->use_dma_alloc) {
90 p = alloc_pages(gfp_flags, order);
96 dma = kmalloc(sizeof(*dma), GFP_KERNEL);
101 attr |= DMA_ATTR_NO_WARN;
103 vaddr = dma_alloc_attrs(pool->dev, (1ULL << order) * PAGE_SIZE,
104 &dma->addr, gfp_flags, attr);
108 /* TODO: This is an illegal abuse of the DMA API, but we need to rework
109 * TTM page fault handling and extend the DMA API to clean this up.
111 if (is_vmalloc_addr(vaddr))
112 p = vmalloc_to_page(vaddr);
114 p = virt_to_page(vaddr);
116 dma->vaddr = (unsigned long)vaddr | order;
117 p->private = (unsigned long)dma;
125 /* Reset the caching and pages of size 1 << order */
126 static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
127 unsigned int order, struct page *p)
129 unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
130 struct ttm_pool_dma *dma;
134 /* We don't care that set_pages_wb is inefficient here. This is only
135 * used when we have to shrink and CPU overhead is irrelevant then.
137 if (caching != ttm_cached && !PageHighMem(p))
138 set_pages_wb(p, 1 << order);
141 if (!pool || !pool->use_dma_alloc) {
142 __free_pages(p, order);
147 attr |= DMA_ATTR_NO_WARN;
149 dma = (void *)p->private;
150 vaddr = (void *)(dma->vaddr & PAGE_MASK);
151 dma_free_attrs(pool->dev, (1UL << order) * PAGE_SIZE, vaddr, dma->addr,
156 /* Apply a new caching to an array of pages */
157 static int ttm_pool_apply_caching(struct page **first, struct page **last,
158 enum ttm_caching caching)
161 unsigned int num_pages = last - first;
169 case ttm_write_combined:
170 return set_pages_array_wc(first, num_pages);
172 return set_pages_array_uc(first, num_pages);
178 /* Map pages of 1 << order size and fill the DMA address array */
179 static int ttm_pool_map(struct ttm_pool *pool, unsigned int order,
180 struct page *p, dma_addr_t **dma_addr)
185 if (pool->use_dma_alloc) {
186 struct ttm_pool_dma *dma = (void *)p->private;
190 size_t size = (1ULL << order) * PAGE_SIZE;
192 addr = dma_map_page(pool->dev, p, 0, size, DMA_BIDIRECTIONAL);
193 if (dma_mapping_error(pool->dev, **dma_addr))
197 for (i = 1 << order; i ; --i) {
198 *(*dma_addr)++ = addr;
205 /* Unmap pages of 1 << order size */
206 static void ttm_pool_unmap(struct ttm_pool *pool, dma_addr_t dma_addr,
207 unsigned int num_pages)
209 /* Unmapped while freeing the page */
210 if (pool->use_dma_alloc)
213 dma_unmap_page(pool->dev, dma_addr, (long)num_pages << PAGE_SHIFT,
217 /* Give pages into a specific pool_type */
218 static void ttm_pool_type_give(struct ttm_pool_type *pt, struct page *p)
220 spin_lock(&pt->lock);
221 list_add(&p->lru, &pt->pages);
222 spin_unlock(&pt->lock);
223 atomic_long_add(1 << pt->order, &allocated_pages);
226 /* Take pages from a specific pool_type, return NULL when nothing available */
227 static struct page *ttm_pool_type_take(struct ttm_pool_type *pt)
231 spin_lock(&pt->lock);
232 p = list_first_entry_or_null(&pt->pages, typeof(*p), lru);
234 atomic_long_sub(1 << pt->order, &allocated_pages);
237 spin_unlock(&pt->lock);
242 /* Count the number of pages available in a pool_type */
243 static unsigned int ttm_pool_type_count(struct ttm_pool_type *pt)
245 unsigned int count = 0;
248 spin_lock(&pt->lock);
249 /* Only used for debugfs, the overhead doesn't matter */
250 list_for_each_entry(p, &pt->pages, lru)
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)
275 struct page *p, *tmp;
277 spin_lock(&shrinker_lock);
278 list_del(&pt->shrinker_list);
279 spin_unlock(&shrinker_lock);
281 list_for_each_entry_safe(p, tmp, &pt->pages, lru)
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_freed;
320 spin_lock(&shrinker_lock);
321 pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list);
323 p = ttm_pool_type_take(pt);
325 ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
326 num_freed = 1 << pt->order;
331 list_move_tail(&pt->shrinker_list, &shrinker_list);
332 spin_unlock(&shrinker_lock);
337 /* Return the allocation order based for a page */
338 static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)
340 if (pool->use_dma_alloc) {
341 struct ttm_pool_dma *dma = (void *)p->private;
343 return dma->vaddr & ~PAGE_MASK;
350 * ttm_pool_alloc - Fill a ttm_tt object
352 * @pool: ttm_pool to use
353 * @tt: ttm_tt object to fill
354 * @ctx: operation context
356 * Fill the ttm_tt object with pages and also make sure to DMA map them when
359 * Returns: 0 on successe, negative error code otherwise.
361 int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
362 struct ttm_operation_ctx *ctx)
364 unsigned long num_pages = tt->num_pages;
365 dma_addr_t *dma_addr = tt->dma_address;
366 struct page **caching = tt->pages;
367 struct page **pages = tt->pages;
368 gfp_t gfp_flags = GFP_USER;
369 unsigned int i, order;
373 WARN_ON(!num_pages || ttm_tt_is_populated(tt));
374 WARN_ON(dma_addr && !pool->dev);
376 if (tt->page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
377 gfp_flags |= __GFP_ZERO;
379 if (ctx->gfp_retry_mayfail)
380 gfp_flags |= __GFP_RETRY_MAYFAIL;
383 gfp_flags |= GFP_DMA32;
385 gfp_flags |= GFP_HIGHUSER;
387 for (order = min(MAX_ORDER - 1UL, __fls(num_pages)); num_pages;
388 order = min_t(unsigned int, order, __fls(num_pages))) {
389 bool apply_caching = false;
390 struct ttm_pool_type *pt;
392 pt = ttm_pool_select_type(pool, tt->caching, order);
393 p = pt ? ttm_pool_type_take(pt) : NULL;
395 apply_caching = true;
397 p = ttm_pool_alloc_page(pool, gfp_flags, order);
398 if (p && PageHighMem(p))
399 apply_caching = true;
412 r = ttm_pool_apply_caching(caching, pages,
415 goto error_free_page;
416 caching = pages + (1 << order);
419 r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
420 (1 << order) * PAGE_SIZE,
423 goto error_free_page;
426 r = ttm_pool_map(pool, order, p, &dma_addr);
428 goto error_global_free;
431 num_pages -= 1 << order;
432 for (i = 1 << order; i; --i)
436 r = ttm_pool_apply_caching(caching, pages, tt->caching);
443 ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * PAGE_SIZE);
446 ttm_pool_free_page(pool, tt->caching, order, p);
449 num_pages = tt->num_pages - num_pages;
450 for (i = 0; i < num_pages; ) {
451 order = ttm_pool_page_order(pool, tt->pages[i]);
452 ttm_pool_free_page(pool, tt->caching, order, tt->pages[i]);
458 EXPORT_SYMBOL(ttm_pool_alloc);
461 * ttm_pool_free - Free the backing pages from a ttm_tt object
463 * @pool: Pool to give pages back to.
464 * @tt: ttm_tt object to unpopulate
466 * Give the packing pages back to a pool or free them
468 void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
472 for (i = 0; i < tt->num_pages; ) {
473 struct page *p = tt->pages[i];
474 unsigned int order, num_pages;
475 struct ttm_pool_type *pt;
477 order = ttm_pool_page_order(pool, p);
478 num_pages = 1ULL << order;
479 ttm_mem_global_free_page(&ttm_mem_glob, p,
480 num_pages * PAGE_SIZE);
482 ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
484 pt = ttm_pool_select_type(pool, tt->caching, order);
486 ttm_pool_type_give(pt, tt->pages[i]);
488 ttm_pool_free_page(pool, tt->caching, order,
494 while (atomic_long_read(&allocated_pages) > page_pool_size)
497 EXPORT_SYMBOL(ttm_pool_free);
500 * ttm_pool_init - Initialize a pool
502 * @pool: the pool to initialize
503 * @dev: device for DMA allocations and mappings
504 * @use_dma_alloc: true if coherent DMA alloc should be used
505 * @use_dma32: true if GFP_DMA32 should be used
507 * Initialize the pool and its pool types.
509 void ttm_pool_init(struct ttm_pool *pool, struct device *dev,
510 bool use_dma_alloc, bool use_dma32)
514 WARN_ON(!dev && use_dma_alloc);
517 pool->use_dma_alloc = use_dma_alloc;
518 pool->use_dma32 = use_dma32;
520 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
521 for (j = 0; j < MAX_ORDER; ++j)
522 ttm_pool_type_init(&pool->caching[i].orders[j],
525 EXPORT_SYMBOL(ttm_pool_init);
528 * ttm_pool_fini - Cleanup a pool
530 * @pool: the pool to clean up
532 * Free all pages in the pool and unregister the types from the global
535 void ttm_pool_fini(struct ttm_pool *pool)
539 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
540 for (j = 0; j < MAX_ORDER; ++j)
541 ttm_pool_type_fini(&pool->caching[i].orders[j]);
543 EXPORT_SYMBOL(ttm_pool_fini);
545 #ifdef CONFIG_DEBUG_FS
547 /* Dump information about the different pool types */
548 static void ttm_pool_debugfs_orders(struct ttm_pool_type *pt,
553 for (i = 0; i < MAX_ORDER; ++i)
554 seq_printf(m, " %8u", ttm_pool_type_count(&pt[i]));
559 * ttm_pool_debugfs - Debugfs dump function for a pool
561 * @pool: the pool to dump the information for
562 * @m: seq_file to dump to
564 * Make a debugfs dump with the per pool and global information.
566 int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
570 spin_lock(&shrinker_lock);
573 for (i = 0; i < MAX_ORDER; ++i)
574 seq_printf(m, " ---%2u---", i);
577 seq_puts(m, "wc\t:");
578 ttm_pool_debugfs_orders(global_write_combined, m);
579 seq_puts(m, "uc\t:");
580 ttm_pool_debugfs_orders(global_uncached, m);
582 seq_puts(m, "wc 32\t:");
583 ttm_pool_debugfs_orders(global_dma32_write_combined, m);
584 seq_puts(m, "uc 32\t:");
585 ttm_pool_debugfs_orders(global_dma32_uncached, m);
587 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
593 case ttm_write_combined:
594 seq_puts(m, "wc\t:");
597 seq_puts(m, "uc\t:");
600 ttm_pool_debugfs_orders(pool->caching[i].orders, m);
603 seq_printf(m, "\ntotal\t: %8lu of %8lu\n",
604 atomic_long_read(&allocated_pages), page_pool_size);
606 spin_unlock(&shrinker_lock);
610 EXPORT_SYMBOL(ttm_pool_debugfs);
614 /* As long as pages are available make sure to release at least one */
615 static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
616 struct shrink_control *sc)
618 unsigned long num_freed = 0;
621 num_freed += ttm_pool_shrink();
622 while (!num_freed && atomic_long_read(&allocated_pages));
627 /* Return the number of pages available or SHRINK_EMPTY if we have none */
628 static unsigned long ttm_pool_shrinker_count(struct shrinker *shrink,
629 struct shrink_control *sc)
631 unsigned long num_pages = atomic_long_read(&allocated_pages);
633 return num_pages ? num_pages : SHRINK_EMPTY;
637 * ttm_pool_mgr_init - Initialize globals
639 * @num_pages: default number of pages
641 * Initialize the global locks and lists for the MM shrinker.
643 int ttm_pool_mgr_init(unsigned long num_pages)
648 page_pool_size = num_pages;
650 spin_lock_init(&shrinker_lock);
651 INIT_LIST_HEAD(&shrinker_list);
653 for (i = 0; i < MAX_ORDER; ++i) {
654 ttm_pool_type_init(&global_write_combined[i], NULL,
655 ttm_write_combined, i);
656 ttm_pool_type_init(&global_uncached[i], NULL, ttm_uncached, i);
658 ttm_pool_type_init(&global_dma32_write_combined[i], NULL,
659 ttm_write_combined, i);
660 ttm_pool_type_init(&global_dma32_uncached[i], NULL,
664 mm_shrinker.count_objects = ttm_pool_shrinker_count;
665 mm_shrinker.scan_objects = ttm_pool_shrinker_scan;
666 mm_shrinker.seeks = 1;
667 return register_shrinker(&mm_shrinker);
671 * ttm_pool_mgr_fini - Finalize globals
673 * Cleanup the global pools and unregister the MM shrinker.
675 void ttm_pool_mgr_fini(void)
679 for (i = 0; i < MAX_ORDER; ++i) {
680 ttm_pool_type_fini(&global_write_combined[i]);
681 ttm_pool_type_fini(&global_uncached[i]);
683 ttm_pool_type_fini(&global_dma32_write_combined[i]);
684 ttm_pool_type_fini(&global_dma32_uncached[i]);
687 unregister_shrinker(&mm_shrinker);
688 WARN_ON(!list_empty(&shrinker_list));