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 spinlock_t shrinker_lock;
67 static struct list_head shrinker_list;
68 static struct shrinker mm_shrinker;
70 /* Allocate pages of size 1 << order with the given gfp_flags */
71 static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
74 unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
75 struct ttm_pool_dma *dma;
80 gfp_flags |= GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
82 gfp_flags &= ~__GFP_MOVABLE;
83 gfp_flags &= ~__GFP_COMP;
86 if (!pool->use_dma_alloc) {
87 p = alloc_pages(gfp_flags, order);
93 dma = kmalloc(sizeof(*dma), GFP_KERNEL);
98 attr |= DMA_ATTR_NO_WARN;
100 vaddr = dma_alloc_attrs(pool->dev, (1ULL << order) * PAGE_SIZE,
101 &dma->addr, gfp_flags, attr);
105 /* TODO: This is an illegal abuse of the DMA API, but we need to rework
106 * TTM page fault handling and extend the DMA API to clean this up.
108 if (is_vmalloc_addr(vaddr))
109 p = vmalloc_to_page(vaddr);
111 p = virt_to_page(vaddr);
113 dma->vaddr = (unsigned long)vaddr | order;
114 p->private = (unsigned long)dma;
122 /* Reset the caching and pages of size 1 << order */
123 static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
124 unsigned int order, struct page *p)
126 unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
127 struct ttm_pool_dma *dma;
131 /* We don't care that set_pages_wb is inefficient here. This is only
132 * used when we have to shrink and CPU overhead is irrelevant then.
134 if (caching != ttm_cached && !PageHighMem(p))
135 set_pages_wb(p, 1 << order);
138 if (!pool || !pool->use_dma_alloc) {
139 __free_pages(p, order);
144 attr |= DMA_ATTR_NO_WARN;
146 dma = (void *)p->private;
147 vaddr = (void *)(dma->vaddr & PAGE_MASK);
148 dma_free_attrs(pool->dev, (1UL << order) * PAGE_SIZE, vaddr, dma->addr,
153 /* Apply a new caching to an array of pages */
154 static int ttm_pool_apply_caching(struct page **first, struct page **last,
155 enum ttm_caching caching)
158 unsigned int num_pages = last - first;
166 case ttm_write_combined:
167 return set_pages_array_wc(first, num_pages);
169 return set_pages_array_uc(first, num_pages);
175 /* Map pages of 1 << order size and fill the DMA address array */
176 static int ttm_pool_map(struct ttm_pool *pool, unsigned int order,
177 struct page *p, dma_addr_t **dma_addr)
182 if (pool->use_dma_alloc) {
183 struct ttm_pool_dma *dma = (void *)p->private;
187 size_t size = (1ULL << order) * PAGE_SIZE;
189 addr = dma_map_page(pool->dev, p, 0, size, DMA_BIDIRECTIONAL);
190 if (dma_mapping_error(pool->dev, **dma_addr))
194 for (i = 1 << order; i ; --i) {
195 *(*dma_addr)++ = addr;
202 /* Unmap pages of 1 << order size */
203 static void ttm_pool_unmap(struct ttm_pool *pool, dma_addr_t dma_addr,
204 unsigned int num_pages)
206 /* Unmapped while freeing the page */
207 if (pool->use_dma_alloc)
210 dma_unmap_page(pool->dev, dma_addr, (long)num_pages << PAGE_SHIFT,
214 /* Give pages into a specific pool_type */
215 static void ttm_pool_type_give(struct ttm_pool_type *pt, struct page *p)
217 spin_lock(&pt->lock);
218 list_add(&p->lru, &pt->pages);
219 spin_unlock(&pt->lock);
220 atomic_long_add(1 << pt->order, &allocated_pages);
223 /* Take pages from a specific pool_type, return NULL when nothing available */
224 static struct page *ttm_pool_type_take(struct ttm_pool_type *pt)
228 spin_lock(&pt->lock);
229 p = list_first_entry_or_null(&pt->pages, typeof(*p), lru);
231 atomic_long_sub(1 << pt->order, &allocated_pages);
234 spin_unlock(&pt->lock);
239 /* Count the number of pages available in a pool_type */
240 static unsigned int ttm_pool_type_count(struct ttm_pool_type *pt)
242 unsigned int count = 0;
245 spin_lock(&pt->lock);
246 /* Only used for debugfs, the overhead doesn't matter */
247 list_for_each_entry(p, &pt->pages, lru)
249 spin_unlock(&pt->lock);
254 /* Initialize and add a pool type to the global shrinker list */
255 static void ttm_pool_type_init(struct ttm_pool_type *pt, struct ttm_pool *pool,
256 enum ttm_caching caching, unsigned int order)
259 pt->caching = caching;
261 spin_lock_init(&pt->lock);
262 INIT_LIST_HEAD(&pt->pages);
264 spin_lock(&shrinker_lock);
265 list_add_tail(&pt->shrinker_list, &shrinker_list);
266 spin_unlock(&shrinker_lock);
269 /* Remove a pool_type from the global shrinker list and free all pages */
270 static void ttm_pool_type_fini(struct ttm_pool_type *pt)
272 struct page *p, *tmp;
274 spin_lock(&shrinker_lock);
275 list_del(&pt->shrinker_list);
276 spin_unlock(&shrinker_lock);
278 list_for_each_entry_safe(p, tmp, &pt->pages, lru)
279 ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
282 /* Return the pool_type to use for the given caching and order */
283 static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
284 enum ttm_caching caching,
287 if (pool->use_dma_alloc)
288 return &pool->caching[caching].orders[order];
292 case ttm_write_combined:
293 return &global_write_combined[order];
295 return &global_uncached[order];
304 /* Free pages using the global shrinker list */
305 static unsigned int ttm_pool_shrink(void)
307 struct ttm_pool_type *pt;
308 unsigned int num_freed;
311 spin_lock(&shrinker_lock);
312 pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list);
314 p = ttm_pool_type_take(pt);
316 ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
317 num_freed = 1 << pt->order;
322 list_move_tail(&pt->shrinker_list, &shrinker_list);
323 spin_unlock(&shrinker_lock);
328 /* Return the allocation order based for a page */
329 static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)
331 if (pool->use_dma_alloc) {
332 struct ttm_pool_dma *dma = (void *)p->private;
334 return dma->vaddr & ~PAGE_MASK;
341 * ttm_pool_alloc - Fill a ttm_tt object
343 * @pool: ttm_pool to use
344 * @tt: ttm_tt object to fill
345 * @ctx: operation context
347 * Fill the ttm_tt object with pages and also make sure to DMA map them when
350 * Returns: 0 on successe, negative error code otherwise.
352 int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
353 struct ttm_operation_ctx *ctx)
355 unsigned long num_pages = tt->num_pages;
356 dma_addr_t *dma_addr = tt->dma_address;
357 struct page **caching = tt->pages;
358 struct page **pages = tt->pages;
359 gfp_t gfp_flags = GFP_USER;
360 unsigned int i, order;
364 WARN_ON(!num_pages || ttm_tt_is_populated(tt));
365 WARN_ON(dma_addr && !pool->dev);
367 if (tt->page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
368 gfp_flags |= __GFP_ZERO;
370 if (ctx->gfp_retry_mayfail)
371 gfp_flags |= __GFP_RETRY_MAYFAIL;
374 gfp_flags |= GFP_DMA32;
376 gfp_flags |= GFP_HIGHUSER;
378 for (order = min(MAX_ORDER - 1UL, __fls(num_pages)); num_pages;
379 order = min_t(unsigned int, order, __fls(num_pages))) {
380 bool apply_caching = false;
381 struct ttm_pool_type *pt;
383 pt = ttm_pool_select_type(pool, tt->caching, order);
384 p = pt ? ttm_pool_type_take(pt) : NULL;
386 apply_caching = true;
388 p = ttm_pool_alloc_page(pool, gfp_flags, order);
389 if (p && PageHighMem(p))
390 apply_caching = true;
403 r = ttm_pool_apply_caching(caching, pages,
406 goto error_free_page;
407 caching = pages + (1 << order);
410 r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
411 (1 << order) * PAGE_SIZE,
414 goto error_free_page;
417 r = ttm_pool_map(pool, order, p, &dma_addr);
419 goto error_global_free;
422 num_pages -= 1 << order;
423 for (i = 1 << order; i; --i)
427 r = ttm_pool_apply_caching(caching, pages, tt->caching);
434 ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * PAGE_SIZE);
437 ttm_pool_free_page(pool, tt->caching, order, p);
440 num_pages = tt->num_pages - num_pages;
441 for (i = 0; i < num_pages; ) {
442 order = ttm_pool_page_order(pool, tt->pages[i]);
443 ttm_pool_free_page(pool, tt->caching, order, tt->pages[i]);
449 EXPORT_SYMBOL(ttm_pool_alloc);
452 * ttm_pool_free - Free the backing pages from a ttm_tt object
454 * @pool: Pool to give pages back to.
455 * @tt: ttm_tt object to unpopulate
457 * Give the packing pages back to a pool or free them
459 void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
463 for (i = 0; i < tt->num_pages; ) {
464 struct page *p = tt->pages[i];
465 unsigned int order, num_pages;
466 struct ttm_pool_type *pt;
468 order = ttm_pool_page_order(pool, p);
469 num_pages = 1ULL << order;
470 ttm_mem_global_free_page(&ttm_mem_glob, p,
471 num_pages * PAGE_SIZE);
473 ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
475 pt = ttm_pool_select_type(pool, tt->caching, order);
477 ttm_pool_type_give(pt, tt->pages[i]);
479 ttm_pool_free_page(pool, tt->caching, order,
485 while (atomic_long_read(&allocated_pages) > page_pool_size)
488 EXPORT_SYMBOL(ttm_pool_free);
491 * ttm_pool_init - Initialize a pool
493 * @pool: the pool to initialize
494 * @dev: device for DMA allocations and mappings
495 * @use_dma_alloc: true if coherent DMA alloc should be used
496 * @use_dma32: true if GFP_DMA32 should be used
498 * Initialize the pool and its pool types.
500 void ttm_pool_init(struct ttm_pool *pool, struct device *dev,
501 bool use_dma_alloc, bool use_dma32)
505 WARN_ON(!dev && use_dma_alloc);
508 pool->use_dma_alloc = use_dma_alloc;
509 pool->use_dma32 = use_dma32;
511 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
512 for (j = 0; j < MAX_ORDER; ++j)
513 ttm_pool_type_init(&pool->caching[i].orders[j],
516 EXPORT_SYMBOL(ttm_pool_init);
519 * ttm_pool_fini - Cleanup a pool
521 * @pool: the pool to clean up
523 * Free all pages in the pool and unregister the types from the global
526 void ttm_pool_fini(struct ttm_pool *pool)
530 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
531 for (j = 0; j < MAX_ORDER; ++j)
532 ttm_pool_type_fini(&pool->caching[i].orders[j]);
534 EXPORT_SYMBOL(ttm_pool_fini);
536 #ifdef CONFIG_DEBUG_FS
538 /* Dump information about the different pool types */
539 static void ttm_pool_debugfs_orders(struct ttm_pool_type *pt,
544 for (i = 0; i < MAX_ORDER; ++i)
545 seq_printf(m, " %8u", ttm_pool_type_count(&pt[i]));
550 * ttm_pool_debugfs - Debugfs dump function for a pool
552 * @pool: the pool to dump the information for
553 * @m: seq_file to dump to
555 * Make a debugfs dump with the per pool and global information.
557 int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
561 spin_lock(&shrinker_lock);
564 for (i = 0; i < MAX_ORDER; ++i)
565 seq_printf(m, " ---%2u---", i);
568 seq_puts(m, "wc\t:");
569 ttm_pool_debugfs_orders(global_write_combined, m);
570 seq_puts(m, "uc\t:");
571 ttm_pool_debugfs_orders(global_uncached, m);
573 for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
579 case ttm_write_combined:
580 seq_puts(m, "wc\t:");
583 seq_puts(m, "uc\t:");
586 ttm_pool_debugfs_orders(pool->caching[i].orders, m);
589 seq_printf(m, "\ntotal\t: %8lu of %8lu\n",
590 atomic_long_read(&allocated_pages), page_pool_size);
592 spin_unlock(&shrinker_lock);
596 EXPORT_SYMBOL(ttm_pool_debugfs);
600 /* As long as pages are available make sure to release at least one */
601 static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
602 struct shrink_control *sc)
604 unsigned long num_freed = 0;
607 num_freed += ttm_pool_shrink();
608 while (!num_freed && atomic_long_read(&allocated_pages));
613 /* Return the number of pages available or SHRINK_EMPTY if we have none */
614 static unsigned long ttm_pool_shrinker_count(struct shrinker *shrink,
615 struct shrink_control *sc)
617 unsigned long num_pages = atomic_long_read(&allocated_pages);
619 return num_pages ? num_pages : SHRINK_EMPTY;
623 * ttm_pool_mgr_init - Initialize globals
625 * @num_pages: default number of pages
627 * Initialize the global locks and lists for the MM shrinker.
629 int ttm_pool_mgr_init(unsigned long num_pages)
634 page_pool_size = num_pages;
636 spin_lock_init(&shrinker_lock);
637 INIT_LIST_HEAD(&shrinker_list);
639 for (i = 0; i < MAX_ORDER; ++i) {
640 ttm_pool_type_init(&global_write_combined[i], NULL,
641 ttm_write_combined, i);
642 ttm_pool_type_init(&global_uncached[i], NULL, ttm_uncached, i);
645 mm_shrinker.count_objects = ttm_pool_shrinker_count;
646 mm_shrinker.scan_objects = ttm_pool_shrinker_scan;
647 mm_shrinker.seeks = 1;
648 return register_shrinker(&mm_shrinker);
652 * ttm_pool_mgr_fini - Finalize globals
654 * Cleanup the global pools and unregister the MM shrinker.
656 void ttm_pool_mgr_fini(void)
660 for (i = 0; i < MAX_ORDER; ++i) {
661 ttm_pool_type_fini(&global_write_combined[i]);
662 ttm_pool_type_fini(&global_uncached[i]);
665 unregister_shrinker(&mm_shrinker);
666 WARN_ON(!list_empty(&shrinker_list));