2 * Copyright 2011 (c) Oracle Corp.
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
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 NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
27 * A simple DMA pool losely based on dmapool.c. It has certain advantages
29 * - Pool collects resently freed pages for reuse (and hooks up to
31 * - Tracks currently in use pages
32 * - Tracks whether the page is UC, WB or cached (and reverts to WB
36 #define pr_fmt(fmt) "[TTM] " fmt
38 #include <linux/dma-mapping.h>
39 #include <linux/list.h>
40 #include <linux/seq_file.h> /* for seq_printf */
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
43 #include <linux/highmem.h>
44 #include <linux/mm_types.h>
45 #include <linux/module.h>
47 #include <linux/atomic.h>
48 #include <linux/device.h>
49 #include <linux/kthread.h>
50 #include <drm/ttm/ttm_bo_driver.h>
51 #include <drm/ttm/ttm_page_alloc.h>
56 #define NUM_PAGES_TO_ALLOC (PAGE_SIZE/sizeof(struct page *))
57 #define SMALL_ALLOCATION 4
58 #define FREE_ALL_PAGES (~0U)
59 /* times are in msecs */
60 #define IS_UNDEFINED (0)
63 #define IS_CACHED (1<<3)
64 #define IS_DMA32 (1<<4)
70 POOL_IS_CACHED = IS_CACHED,
71 POOL_IS_WC_DMA32 = IS_WC | IS_DMA32,
72 POOL_IS_UC_DMA32 = IS_UC | IS_DMA32,
73 POOL_IS_CACHED_DMA32 = IS_CACHED | IS_DMA32,
76 * The pool structure. There are usually six pools:
77 * - generic (not restricted to DMA32):
78 * - write combined, uncached, cached.
79 * - dma32 (up to 2^32 - so up 4GB):
80 * - write combined, uncached, cached.
81 * for each 'struct device'. The 'cached' is for pages that are actively used.
82 * The other ones can be shrunk by the shrinker API if neccessary.
83 * @pools: The 'struct device->dma_pools' link.
84 * @type: Type of the pool
85 * @lock: Protects the inuse_list and free_list from concurrnet access. Must be
86 * used with irqsave/irqrestore variants because pool allocator maybe called
88 * @inuse_list: Pool of pages that are in use. The order is very important and
89 * it is in the order that the TTM pages that are put back are in.
90 * @free_list: Pool of pages that are free to be used. No order requirements.
91 * @dev: The device that is associated with these pools.
92 * @size: Size used during DMA allocation.
93 * @npages_free: Count of available pages for re-use.
94 * @npages_in_use: Count of pages that are in use.
95 * @nfrees: Stats when pool is shrinking.
96 * @nrefills: Stats when the pool is grown.
97 * @gfp_flags: Flags to pass for alloc_page.
98 * @name: Name of the pool.
99 * @dev_name: Name derieved from dev - similar to how dev_info works.
100 * Used during shutdown as the dev_info during release is unavailable.
103 struct list_head pools; /* The 'struct device->dma_pools link */
106 struct list_head inuse_list;
107 struct list_head free_list;
110 unsigned npages_free;
111 unsigned npages_in_use;
112 unsigned long nfrees; /* Stats when shrunk. */
113 unsigned long nrefills; /* Stats when grown. */
115 char name[13]; /* "cached dma32" */
116 char dev_name[64]; /* Constructed from dev */
120 * The accounting page keeping track of the allocated page along with
122 * @page_list: The link to the 'page_list' in 'struct dma_pool'.
123 * @vaddr: The virtual address of the page
124 * @dma: The bus address of the page. If the page is not allocated
125 * via the DMA API, it will be -1.
128 struct list_head page_list;
135 * Limits for the pool. They are handled without locks because only place where
136 * they may change is in sysfs store. They won't have immediate effect anyway
137 * so forcing serialization to access them is pointless.
140 struct ttm_pool_opts {
147 * Contains the list of all of the 'struct device' and their corresponding
148 * DMA pools. Guarded by _mutex->lock.
149 * @pools: The link to 'struct ttm_pool_manager->pools'
150 * @dev: The 'struct device' associated with the 'pool'
151 * @pool: The 'struct dma_pool' associated with the 'dev'
153 struct device_pools {
154 struct list_head pools;
156 struct dma_pool *pool;
160 * struct ttm_pool_manager - Holds memory pools for fast allocation
162 * @lock: Lock used when adding/removing from pools
163 * @pools: List of 'struct device' and 'struct dma_pool' tuples.
164 * @options: Limits for the pool.
165 * @npools: Total amount of pools in existence.
166 * @shrinker: The structure used by [un|]register_shrinker
168 struct ttm_pool_manager {
170 struct list_head pools;
171 struct ttm_pool_opts options;
173 struct shrinker mm_shrink;
177 static struct ttm_pool_manager *_manager;
179 static struct attribute ttm_page_pool_max = {
180 .name = "pool_max_size",
181 .mode = S_IRUGO | S_IWUSR
183 static struct attribute ttm_page_pool_small = {
184 .name = "pool_small_allocation",
185 .mode = S_IRUGO | S_IWUSR
187 static struct attribute ttm_page_pool_alloc_size = {
188 .name = "pool_allocation_size",
189 .mode = S_IRUGO | S_IWUSR
192 static struct attribute *ttm_pool_attrs[] = {
194 &ttm_page_pool_small,
195 &ttm_page_pool_alloc_size,
199 static void ttm_pool_kobj_release(struct kobject *kobj)
201 struct ttm_pool_manager *m =
202 container_of(kobj, struct ttm_pool_manager, kobj);
206 static ssize_t ttm_pool_store(struct kobject *kobj, struct attribute *attr,
207 const char *buffer, size_t size)
209 struct ttm_pool_manager *m =
210 container_of(kobj, struct ttm_pool_manager, kobj);
213 chars = sscanf(buffer, "%u", &val);
217 /* Convert kb to number of pages */
218 val = val / (PAGE_SIZE >> 10);
220 if (attr == &ttm_page_pool_max)
221 m->options.max_size = val;
222 else if (attr == &ttm_page_pool_small)
223 m->options.small = val;
224 else if (attr == &ttm_page_pool_alloc_size) {
225 if (val > NUM_PAGES_TO_ALLOC*8) {
226 pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
227 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
228 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
230 } else if (val > NUM_PAGES_TO_ALLOC) {
231 pr_warn("Setting allocation size to larger than %lu is not recommended\n",
232 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
234 m->options.alloc_size = val;
240 static ssize_t ttm_pool_show(struct kobject *kobj, struct attribute *attr,
243 struct ttm_pool_manager *m =
244 container_of(kobj, struct ttm_pool_manager, kobj);
247 if (attr == &ttm_page_pool_max)
248 val = m->options.max_size;
249 else if (attr == &ttm_page_pool_small)
250 val = m->options.small;
251 else if (attr == &ttm_page_pool_alloc_size)
252 val = m->options.alloc_size;
254 val = val * (PAGE_SIZE >> 10);
256 return snprintf(buffer, PAGE_SIZE, "%u\n", val);
259 static const struct sysfs_ops ttm_pool_sysfs_ops = {
260 .show = &ttm_pool_show,
261 .store = &ttm_pool_store,
264 static struct kobj_type ttm_pool_kobj_type = {
265 .release = &ttm_pool_kobj_release,
266 .sysfs_ops = &ttm_pool_sysfs_ops,
267 .default_attrs = ttm_pool_attrs,
271 static int set_pages_array_wb(struct page **pages, int addrinarray)
276 for (i = 0; i < addrinarray; i++)
277 unmap_page_from_agp(pages[i]);
282 static int set_pages_array_wc(struct page **pages, int addrinarray)
287 for (i = 0; i < addrinarray; i++)
288 map_page_into_agp(pages[i]);
293 static int set_pages_array_uc(struct page **pages, int addrinarray)
298 for (i = 0; i < addrinarray; i++)
299 map_page_into_agp(pages[i]);
303 #endif /* for !CONFIG_X86 */
305 static int ttm_set_pages_caching(struct dma_pool *pool,
306 struct page **pages, unsigned cpages)
309 /* Set page caching */
310 if (pool->type & IS_UC) {
311 r = set_pages_array_uc(pages, cpages);
313 pr_err("%s: Failed to set %d pages to uc!\n",
314 pool->dev_name, cpages);
316 if (pool->type & IS_WC) {
317 r = set_pages_array_wc(pages, cpages);
319 pr_err("%s: Failed to set %d pages to wc!\n",
320 pool->dev_name, cpages);
325 static void __ttm_dma_free_page(struct dma_pool *pool, struct dma_page *d_page)
327 dma_addr_t dma = d_page->dma;
328 dma_free_coherent(pool->dev, pool->size, d_page->vaddr, dma);
333 static struct dma_page *__ttm_dma_alloc_page(struct dma_pool *pool)
335 struct dma_page *d_page;
337 d_page = kmalloc(sizeof(struct dma_page), GFP_KERNEL);
341 d_page->vaddr = dma_alloc_coherent(pool->dev, pool->size,
345 d_page->p = virt_to_page(d_page->vaddr);
352 static enum pool_type ttm_to_type(int flags, enum ttm_caching_state cstate)
354 enum pool_type type = IS_UNDEFINED;
356 if (flags & TTM_PAGE_FLAG_DMA32)
358 if (cstate == tt_cached)
360 else if (cstate == tt_uncached)
368 static void ttm_pool_update_free_locked(struct dma_pool *pool,
369 unsigned freed_pages)
371 pool->npages_free -= freed_pages;
372 pool->nfrees += freed_pages;
376 /* set memory back to wb and free the pages. */
377 static void ttm_dma_pages_put(struct dma_pool *pool, struct list_head *d_pages,
378 struct page *pages[], unsigned npages)
380 struct dma_page *d_page, *tmp;
382 /* Don't set WB on WB page pool. */
383 if (npages && !(pool->type & IS_CACHED) &&
384 set_pages_array_wb(pages, npages))
385 pr_err("%s: Failed to set %d pages to wb!\n",
386 pool->dev_name, npages);
388 list_for_each_entry_safe(d_page, tmp, d_pages, page_list) {
389 list_del(&d_page->page_list);
390 __ttm_dma_free_page(pool, d_page);
394 static void ttm_dma_page_put(struct dma_pool *pool, struct dma_page *d_page)
396 /* Don't set WB on WB page pool. */
397 if (!(pool->type & IS_CACHED) && set_pages_array_wb(&d_page->p, 1))
398 pr_err("%s: Failed to set %d pages to wb!\n",
401 list_del(&d_page->page_list);
402 __ttm_dma_free_page(pool, d_page);
406 * Free pages from pool.
408 * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
409 * number of pages in one go.
411 * @pool: to free the pages from
412 * @nr_free: If set to true will free all pages in pool
414 static unsigned ttm_dma_page_pool_free(struct dma_pool *pool, unsigned nr_free)
416 unsigned long irq_flags;
417 struct dma_page *dma_p, *tmp;
418 struct page **pages_to_free;
419 struct list_head d_pages;
420 unsigned freed_pages = 0,
421 npages_to_free = nr_free;
423 if (NUM_PAGES_TO_ALLOC < nr_free)
424 npages_to_free = NUM_PAGES_TO_ALLOC;
427 pr_debug("%s: (%s:%d) Attempting to free %d (%d) pages\n",
428 pool->dev_name, pool->name, current->pid,
429 npages_to_free, nr_free);
432 pages_to_free = kmalloc(npages_to_free * sizeof(struct page *),
435 if (!pages_to_free) {
436 pr_err("%s: Failed to allocate memory for pool free operation\n",
440 INIT_LIST_HEAD(&d_pages);
442 spin_lock_irqsave(&pool->lock, irq_flags);
444 /* We picking the oldest ones off the list */
445 list_for_each_entry_safe_reverse(dma_p, tmp, &pool->free_list,
447 if (freed_pages >= npages_to_free)
450 /* Move the dma_page from one list to another. */
451 list_move(&dma_p->page_list, &d_pages);
453 pages_to_free[freed_pages++] = dma_p->p;
454 /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
455 if (freed_pages >= NUM_PAGES_TO_ALLOC) {
457 ttm_pool_update_free_locked(pool, freed_pages);
459 * Because changing page caching is costly
460 * we unlock the pool to prevent stalling.
462 spin_unlock_irqrestore(&pool->lock, irq_flags);
464 ttm_dma_pages_put(pool, &d_pages, pages_to_free,
467 INIT_LIST_HEAD(&d_pages);
469 if (likely(nr_free != FREE_ALL_PAGES))
470 nr_free -= freed_pages;
472 if (NUM_PAGES_TO_ALLOC >= nr_free)
473 npages_to_free = nr_free;
475 npages_to_free = NUM_PAGES_TO_ALLOC;
479 /* free all so restart the processing */
483 /* Not allowed to fall through or break because
484 * following context is inside spinlock while we are
492 /* remove range of pages from the pool */
494 ttm_pool_update_free_locked(pool, freed_pages);
495 nr_free -= freed_pages;
498 spin_unlock_irqrestore(&pool->lock, irq_flags);
501 ttm_dma_pages_put(pool, &d_pages, pages_to_free, freed_pages);
503 kfree(pages_to_free);
507 static void ttm_dma_free_pool(struct device *dev, enum pool_type type)
509 struct device_pools *p;
510 struct dma_pool *pool;
515 mutex_lock(&_manager->lock);
516 list_for_each_entry_reverse(p, &_manager->pools, pools) {
520 if (pool->type != type)
528 list_for_each_entry_reverse(pool, &dev->dma_pools, pools) {
529 if (pool->type != type)
531 /* Takes a spinlock.. */
532 ttm_dma_page_pool_free(pool, FREE_ALL_PAGES);
533 WARN_ON(((pool->npages_in_use + pool->npages_free) != 0));
534 /* This code path is called after _all_ references to the
535 * struct device has been dropped - so nobody should be
536 * touching it. In case somebody is trying to _add_ we are
537 * guarded by the mutex. */
538 list_del(&pool->pools);
542 mutex_unlock(&_manager->lock);
546 * On free-ing of the 'struct device' this deconstructor is run.
547 * Albeit the pool might have already been freed earlier.
549 static void ttm_dma_pool_release(struct device *dev, void *res)
551 struct dma_pool *pool = *(struct dma_pool **)res;
554 ttm_dma_free_pool(dev, pool->type);
557 static int ttm_dma_pool_match(struct device *dev, void *res, void *match_data)
559 return *(struct dma_pool **)res == match_data;
562 static struct dma_pool *ttm_dma_pool_init(struct device *dev, gfp_t flags,
565 char *n[] = {"wc", "uc", "cached", " dma32", "unknown",};
566 enum pool_type t[] = {IS_WC, IS_UC, IS_CACHED, IS_DMA32, IS_UNDEFINED};
567 struct device_pools *sec_pool = NULL;
568 struct dma_pool *pool = NULL, **ptr;
576 ptr = devres_alloc(ttm_dma_pool_release, sizeof(*ptr), GFP_KERNEL);
582 pool = kmalloc_node(sizeof(struct dma_pool), GFP_KERNEL,
587 sec_pool = kmalloc_node(sizeof(struct device_pools), GFP_KERNEL,
592 INIT_LIST_HEAD(&sec_pool->pools);
594 sec_pool->pool = pool;
596 INIT_LIST_HEAD(&pool->free_list);
597 INIT_LIST_HEAD(&pool->inuse_list);
598 INIT_LIST_HEAD(&pool->pools);
599 spin_lock_init(&pool->lock);
601 pool->npages_free = pool->npages_in_use = 0;
603 pool->gfp_flags = flags;
604 pool->size = PAGE_SIZE;
608 for (i = 0; i < 5; i++) {
610 p += snprintf(p, sizeof(pool->name) - (p - pool->name),
615 /* We copy the name for pr_ calls b/c when dma_pool_destroy is called
616 * - the kobj->name has already been deallocated.*/
617 snprintf(pool->dev_name, sizeof(pool->dev_name), "%s %s",
618 dev_driver_string(dev), dev_name(dev));
619 mutex_lock(&_manager->lock);
620 /* You can get the dma_pool from either the global: */
621 list_add(&sec_pool->pools, &_manager->pools);
623 /* or from 'struct device': */
624 list_add(&pool->pools, &dev->dma_pools);
625 mutex_unlock(&_manager->lock);
628 devres_add(dev, ptr);
638 static struct dma_pool *ttm_dma_find_pool(struct device *dev,
641 struct dma_pool *pool, *tmp, *found = NULL;
643 if (type == IS_UNDEFINED)
646 /* NB: We iterate on the 'struct dev' which has no spinlock, but
647 * it does have a kref which we have taken. The kref is taken during
648 * graphic driver loading - in the drm_pci_init it calls either
649 * pci_dev_get or pci_register_driver which both end up taking a kref
650 * on 'struct device'.
652 * On teardown, the graphic drivers end up quiescing the TTM (put_pages)
653 * and calls the dev_res deconstructors: ttm_dma_pool_release. The nice
654 * thing is at that point of time there are no pages associated with the
655 * driver so this function will not be called.
657 list_for_each_entry_safe(pool, tmp, &dev->dma_pools, pools) {
658 if (pool->type != type)
667 * Free pages the pages that failed to change the caching state. If there
668 * are pages that have changed their caching state already put them to the
671 static void ttm_dma_handle_caching_state_failure(struct dma_pool *pool,
672 struct list_head *d_pages,
673 struct page **failed_pages,
676 struct dma_page *d_page, *tmp;
683 /* Find the failed page. */
684 list_for_each_entry_safe(d_page, tmp, d_pages, page_list) {
687 /* .. and then progress over the full list. */
688 list_del(&d_page->page_list);
689 __ttm_dma_free_page(pool, d_page);
699 * Allocate 'count' pages, and put 'need' number of them on the
700 * 'pages' and as well on the 'dma_address' starting at 'dma_offset' offset.
701 * The full list of pages should also be on 'd_pages'.
702 * We return zero for success, and negative numbers as errors.
704 static int ttm_dma_pool_alloc_new_pages(struct dma_pool *pool,
705 struct list_head *d_pages,
708 struct page **caching_array;
709 struct dma_page *dma_p;
713 unsigned max_cpages = min(count,
714 (unsigned)(PAGE_SIZE/sizeof(struct page *)));
716 /* allocate array for page caching change */
717 caching_array = kmalloc(max_cpages*sizeof(struct page *), GFP_KERNEL);
719 if (!caching_array) {
720 pr_err("%s: Unable to allocate table for new pages\n",
726 pr_debug("%s: (%s:%d) Getting %d pages\n",
727 pool->dev_name, pool->name, current->pid, count);
730 for (i = 0, cpages = 0; i < count; ++i) {
731 dma_p = __ttm_dma_alloc_page(pool);
733 pr_err("%s: Unable to get page %u\n",
736 /* store already allocated pages in the pool after
737 * setting the caching state */
739 r = ttm_set_pages_caching(pool, caching_array,
742 ttm_dma_handle_caching_state_failure(
743 pool, d_pages, caching_array,
750 #ifdef CONFIG_HIGHMEM
751 /* gfp flags of highmem page should never be dma32 so we
752 * we should be fine in such case
757 caching_array[cpages++] = p;
758 if (cpages == max_cpages) {
759 /* Note: Cannot hold the spinlock */
760 r = ttm_set_pages_caching(pool, caching_array,
763 ttm_dma_handle_caching_state_failure(
764 pool, d_pages, caching_array,
771 list_add(&dma_p->page_list, d_pages);
775 r = ttm_set_pages_caching(pool, caching_array, cpages);
777 ttm_dma_handle_caching_state_failure(pool, d_pages,
778 caching_array, cpages);
781 kfree(caching_array);
786 * @return count of pages still required to fulfill the request.
788 static int ttm_dma_page_pool_fill_locked(struct dma_pool *pool,
789 unsigned long *irq_flags)
791 unsigned count = _manager->options.small;
792 int r = pool->npages_free;
794 if (count > pool->npages_free) {
795 struct list_head d_pages;
797 INIT_LIST_HEAD(&d_pages);
799 spin_unlock_irqrestore(&pool->lock, *irq_flags);
801 /* Returns how many more are neccessary to fulfill the
803 r = ttm_dma_pool_alloc_new_pages(pool, &d_pages, count);
805 spin_lock_irqsave(&pool->lock, *irq_flags);
807 /* Add the fresh to the end.. */
808 list_splice(&d_pages, &pool->free_list);
810 pool->npages_free += count;
813 struct dma_page *d_page;
816 pr_err("%s: Failed to fill %s pool (r:%d)!\n",
817 pool->dev_name, pool->name, r);
819 list_for_each_entry(d_page, &d_pages, page_list) {
822 list_splice_tail(&d_pages, &pool->free_list);
823 pool->npages_free += cpages;
831 * @return count of pages still required to fulfill the request.
832 * The populate list is actually a stack (not that is matters as TTM
833 * allocates one page at a time.
835 static int ttm_dma_pool_get_pages(struct dma_pool *pool,
836 struct ttm_dma_tt *ttm_dma,
839 struct dma_page *d_page;
840 struct ttm_tt *ttm = &ttm_dma->ttm;
841 unsigned long irq_flags;
842 int count, r = -ENOMEM;
844 spin_lock_irqsave(&pool->lock, irq_flags);
845 count = ttm_dma_page_pool_fill_locked(pool, &irq_flags);
847 d_page = list_first_entry(&pool->free_list, struct dma_page, page_list);
848 ttm->pages[index] = d_page->p;
849 ttm_dma->dma_address[index] = d_page->dma;
850 list_move_tail(&d_page->page_list, &ttm_dma->pages_list);
852 pool->npages_in_use += 1;
853 pool->npages_free -= 1;
855 spin_unlock_irqrestore(&pool->lock, irq_flags);
860 * On success pages list will hold count number of correctly
861 * cached pages. On failure will hold the negative return value (-ENOMEM, etc).
863 int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev)
865 struct ttm_tt *ttm = &ttm_dma->ttm;
866 struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
867 struct dma_pool *pool;
873 if (ttm->state != tt_unpopulated)
876 type = ttm_to_type(ttm->page_flags, ttm->caching_state);
877 if (ttm->page_flags & TTM_PAGE_FLAG_DMA32)
878 gfp_flags = GFP_USER | GFP_DMA32;
880 gfp_flags = GFP_HIGHUSER;
881 if (ttm->page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
882 gfp_flags |= __GFP_ZERO;
884 pool = ttm_dma_find_pool(dev, type);
886 pool = ttm_dma_pool_init(dev, gfp_flags, type);
887 if (IS_ERR_OR_NULL(pool)) {
892 INIT_LIST_HEAD(&ttm_dma->pages_list);
893 for (i = 0; i < ttm->num_pages; ++i) {
894 ret = ttm_dma_pool_get_pages(pool, ttm_dma, i);
896 ttm_dma_unpopulate(ttm_dma, dev);
900 ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
902 if (unlikely(ret != 0)) {
903 ttm_dma_unpopulate(ttm_dma, dev);
908 if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
909 ret = ttm_tt_swapin(ttm);
910 if (unlikely(ret != 0)) {
911 ttm_dma_unpopulate(ttm_dma, dev);
916 ttm->state = tt_unbound;
919 EXPORT_SYMBOL_GPL(ttm_dma_populate);
921 /* Get good estimation how many pages are free in pools */
922 static int ttm_dma_pool_get_num_unused_pages(void)
924 struct device_pools *p;
927 mutex_lock(&_manager->lock);
928 list_for_each_entry(p, &_manager->pools, pools)
929 total += p->pool->npages_free;
930 mutex_unlock(&_manager->lock);
934 /* Put all pages in pages list to correct pool to wait for reuse */
935 void ttm_dma_unpopulate(struct ttm_dma_tt *ttm_dma, struct device *dev)
937 struct ttm_tt *ttm = &ttm_dma->ttm;
938 struct dma_pool *pool;
939 struct dma_page *d_page, *next;
941 bool is_cached = false;
942 unsigned count = 0, i, npages = 0;
943 unsigned long irq_flags;
945 type = ttm_to_type(ttm->page_flags, ttm->caching_state);
946 pool = ttm_dma_find_pool(dev, type);
950 is_cached = (ttm_dma_find_pool(pool->dev,
951 ttm_to_type(ttm->page_flags, tt_cached)) == pool);
953 /* make sure pages array match list and count number of pages */
954 list_for_each_entry(d_page, &ttm_dma->pages_list, page_list) {
955 ttm->pages[count] = d_page->p;
959 spin_lock_irqsave(&pool->lock, irq_flags);
960 pool->npages_in_use -= count;
962 pool->nfrees += count;
964 pool->npages_free += count;
965 list_splice(&ttm_dma->pages_list, &pool->free_list);
967 if (pool->npages_free > _manager->options.max_size) {
968 npages = pool->npages_free - _manager->options.max_size;
969 /* free at least NUM_PAGES_TO_ALLOC number of pages
970 * to reduce calls to set_memory_wb */
971 if (npages < NUM_PAGES_TO_ALLOC)
972 npages = NUM_PAGES_TO_ALLOC;
975 spin_unlock_irqrestore(&pool->lock, irq_flags);
978 list_for_each_entry_safe(d_page, next, &ttm_dma->pages_list, page_list) {
979 ttm_mem_global_free_page(ttm->glob->mem_glob,
981 ttm_dma_page_put(pool, d_page);
984 for (i = 0; i < count; i++) {
985 ttm_mem_global_free_page(ttm->glob->mem_glob,
990 INIT_LIST_HEAD(&ttm_dma->pages_list);
991 for (i = 0; i < ttm->num_pages; i++) {
992 ttm->pages[i] = NULL;
993 ttm_dma->dma_address[i] = 0;
996 /* shrink pool if necessary (only on !is_cached pools)*/
998 ttm_dma_page_pool_free(pool, npages);
999 ttm->state = tt_unpopulated;
1001 EXPORT_SYMBOL_GPL(ttm_dma_unpopulate);
1004 * Callback for mm to request pool to reduce number of page held.
1006 static int ttm_dma_pool_mm_shrink(struct shrinker *shrink,
1007 struct shrink_control *sc)
1009 static atomic_t start_pool = ATOMIC_INIT(0);
1011 unsigned pool_offset = atomic_add_return(1, &start_pool);
1012 unsigned shrink_pages = sc->nr_to_scan;
1013 struct device_pools *p;
1015 if (list_empty(&_manager->pools))
1018 mutex_lock(&_manager->lock);
1019 pool_offset = pool_offset % _manager->npools;
1020 list_for_each_entry(p, &_manager->pools, pools) {
1025 if (shrink_pages == 0)
1027 /* Do it in round-robin fashion. */
1028 if (++idx < pool_offset)
1030 nr_free = shrink_pages;
1031 shrink_pages = ttm_dma_page_pool_free(p->pool, nr_free);
1032 pr_debug("%s: (%s:%d) Asked to shrink %d, have %d more to go\n",
1033 p->pool->dev_name, p->pool->name, current->pid,
1034 nr_free, shrink_pages);
1036 mutex_unlock(&_manager->lock);
1037 /* return estimated number of unused pages in pool */
1038 return ttm_dma_pool_get_num_unused_pages();
1041 static void ttm_dma_pool_mm_shrink_init(struct ttm_pool_manager *manager)
1043 manager->mm_shrink.shrink = &ttm_dma_pool_mm_shrink;
1044 manager->mm_shrink.seeks = 1;
1045 register_shrinker(&manager->mm_shrink);
1048 static void ttm_dma_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
1050 unregister_shrinker(&manager->mm_shrink);
1053 int ttm_dma_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
1059 pr_info("Initializing DMA pool allocator\n");
1061 _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
1065 mutex_init(&_manager->lock);
1066 INIT_LIST_HEAD(&_manager->pools);
1068 _manager->options.max_size = max_pages;
1069 _manager->options.small = SMALL_ALLOCATION;
1070 _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
1072 /* This takes care of auto-freeing the _manager */
1073 ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
1074 &glob->kobj, "dma_pool");
1075 if (unlikely(ret != 0)) {
1076 kobject_put(&_manager->kobj);
1079 ttm_dma_pool_mm_shrink_init(_manager);
1085 void ttm_dma_page_alloc_fini(void)
1087 struct device_pools *p, *t;
1089 pr_info("Finalizing DMA pool allocator\n");
1090 ttm_dma_pool_mm_shrink_fini(_manager);
1092 list_for_each_entry_safe_reverse(p, t, &_manager->pools, pools) {
1093 dev_dbg(p->dev, "(%s:%d) Freeing.\n", p->pool->name,
1095 WARN_ON(devres_destroy(p->dev, ttm_dma_pool_release,
1096 ttm_dma_pool_match, p->pool));
1097 ttm_dma_free_pool(p->dev, p->pool->type);
1099 kobject_put(&_manager->kobj);
1103 int ttm_dma_page_alloc_debugfs(struct seq_file *m, void *data)
1105 struct device_pools *p;
1106 struct dma_pool *pool = NULL;
1107 char *h[] = {"pool", "refills", "pages freed", "inuse", "available",
1108 "name", "virt", "busaddr"};
1111 seq_printf(m, "No pool allocator running.\n");
1114 seq_printf(m, "%13s %12s %13s %8s %8s %8s\n",
1115 h[0], h[1], h[2], h[3], h[4], h[5]);
1116 mutex_lock(&_manager->lock);
1117 list_for_each_entry(p, &_manager->pools, pools) {
1118 struct device *dev = p->dev;
1122 seq_printf(m, "%13s %12ld %13ld %8d %8d %8s\n",
1123 pool->name, pool->nrefills,
1124 pool->nfrees, pool->npages_in_use,
1128 mutex_unlock(&_manager->lock);
1131 EXPORT_SYMBOL_GPL(ttm_dma_page_alloc_debugfs);