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.
23 * Author: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
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 #if defined(CONFIG_SWIOTLB) || defined(CONFIG_INTEL_IOMMU)
37 #define pr_fmt(fmt) "[TTM] " fmt
39 #include <linux/dma-mapping.h>
40 #include <linux/list.h>
41 #include <linux/seq_file.h> /* for seq_printf */
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/highmem.h>
45 #include <linux/mm_types.h>
46 #include <linux/module.h>
48 #include <linux/atomic.h>
49 #include <linux/device.h>
50 #include <linux/kthread.h>
51 #include <drm/ttm/ttm_bo_driver.h>
52 #include <drm/ttm/ttm_page_alloc.h>
53 #include <drm/ttm/ttm_set_memory.h>
55 #define NUM_PAGES_TO_ALLOC (PAGE_SIZE/sizeof(struct page *))
56 #define SMALL_ALLOCATION 4
57 #define FREE_ALL_PAGES (~0U)
58 #define VADDR_FLAG_HUGE_POOL 1UL
59 #define VADDR_FLAG_UPDATED_COUNT 2UL
71 * The pool structure. There are up to nine pools:
72 * - generic (not restricted to DMA32):
73 * - write combined, uncached, cached.
74 * - dma32 (up to 2^32 - so up 4GB):
75 * - write combined, uncached, cached.
76 * - huge (not restricted to DMA32):
77 * - write combined, uncached, cached.
78 * for each 'struct device'. The 'cached' is for pages that are actively used.
79 * The other ones can be shrunk by the shrinker API if neccessary.
80 * @pools: The 'struct device->dma_pools' link.
81 * @type: Type of the pool
82 * @lock: Protects the free_list from concurrnet access. Must be
83 * used with irqsave/irqrestore variants because pool allocator maybe called
85 * @free_list: Pool of pages that are free to be used. No order requirements.
86 * @dev: The device that is associated with these pools.
87 * @size: Size used during DMA allocation.
88 * @npages_free: Count of available pages for re-use.
89 * @npages_in_use: Count of pages that are in use.
90 * @nfrees: Stats when pool is shrinking.
91 * @nrefills: Stats when the pool is grown.
92 * @gfp_flags: Flags to pass for alloc_page.
93 * @name: Name of the pool.
94 * @dev_name: Name derieved from dev - similar to how dev_info works.
95 * Used during shutdown as the dev_info during release is unavailable.
98 struct list_head pools
; /* The 'struct device->dma_pools link */
101 struct list_head free_list
;
104 unsigned npages_free
;
105 unsigned npages_in_use
;
106 unsigned long nfrees
; /* Stats when shrunk. */
107 unsigned long nrefills
; /* Stats when grown. */
109 char name
[13]; /* "cached dma32" */
110 char dev_name
[64]; /* Constructed from dev */
114 * The accounting page keeping track of the allocated page along with
116 * @page_list: The link to the 'page_list' in 'struct dma_pool'.
117 * @vaddr: The virtual address of the page and a flag if the page belongs to a
119 * @dma: The bus address of the page. If the page is not allocated
120 * via the DMA API, it will be -1.
123 struct list_head page_list
;
130 * Limits for the pool. They are handled without locks because only place where
131 * they may change is in sysfs store. They won't have immediate effect anyway
132 * so forcing serialization to access them is pointless.
135 struct ttm_pool_opts
{
142 * Contains the list of all of the 'struct device' and their corresponding
143 * DMA pools. Guarded by _mutex->lock.
144 * @pools: The link to 'struct ttm_pool_manager->pools'
145 * @dev: The 'struct device' associated with the 'pool'
146 * @pool: The 'struct dma_pool' associated with the 'dev'
148 struct device_pools
{
149 struct list_head pools
;
151 struct dma_pool
*pool
;
155 * struct ttm_pool_manager - Holds memory pools for fast allocation
157 * @lock: Lock used when adding/removing from pools
158 * @pools: List of 'struct device' and 'struct dma_pool' tuples.
159 * @options: Limits for the pool.
160 * @npools: Total amount of pools in existence.
161 * @shrinker: The structure used by [un|]register_shrinker
163 struct ttm_pool_manager
{
165 struct list_head pools
;
166 struct ttm_pool_opts options
;
168 struct shrinker mm_shrink
;
172 static struct ttm_pool_manager
*_manager
;
174 static struct attribute ttm_page_pool_max
= {
175 .name
= "pool_max_size",
176 .mode
= S_IRUGO
| S_IWUSR
178 static struct attribute ttm_page_pool_small
= {
179 .name
= "pool_small_allocation",
180 .mode
= S_IRUGO
| S_IWUSR
182 static struct attribute ttm_page_pool_alloc_size
= {
183 .name
= "pool_allocation_size",
184 .mode
= S_IRUGO
| S_IWUSR
187 static struct attribute
*ttm_pool_attrs
[] = {
189 &ttm_page_pool_small
,
190 &ttm_page_pool_alloc_size
,
194 static void ttm_pool_kobj_release(struct kobject
*kobj
)
196 struct ttm_pool_manager
*m
=
197 container_of(kobj
, struct ttm_pool_manager
, kobj
);
201 static ssize_t
ttm_pool_store(struct kobject
*kobj
, struct attribute
*attr
,
202 const char *buffer
, size_t size
)
204 struct ttm_pool_manager
*m
=
205 container_of(kobj
, struct ttm_pool_manager
, kobj
);
209 chars
= sscanf(buffer
, "%u", &val
);
213 /* Convert kb to number of pages */
214 val
= val
/ (PAGE_SIZE
>> 10);
216 if (attr
== &ttm_page_pool_max
) {
217 m
->options
.max_size
= val
;
218 } else if (attr
== &ttm_page_pool_small
) {
219 m
->options
.small
= val
;
220 } else if (attr
== &ttm_page_pool_alloc_size
) {
221 if (val
> NUM_PAGES_TO_ALLOC
*8) {
222 pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
223 NUM_PAGES_TO_ALLOC
*(PAGE_SIZE
>> 7),
224 NUM_PAGES_TO_ALLOC
*(PAGE_SIZE
>> 10));
226 } else if (val
> NUM_PAGES_TO_ALLOC
) {
227 pr_warn("Setting allocation size to larger than %lu is not recommended\n",
228 NUM_PAGES_TO_ALLOC
*(PAGE_SIZE
>> 10));
230 m
->options
.alloc_size
= val
;
236 static ssize_t
ttm_pool_show(struct kobject
*kobj
, struct attribute
*attr
,
239 struct ttm_pool_manager
*m
=
240 container_of(kobj
, struct ttm_pool_manager
, kobj
);
243 if (attr
== &ttm_page_pool_max
)
244 val
= m
->options
.max_size
;
245 else if (attr
== &ttm_page_pool_small
)
246 val
= m
->options
.small
;
247 else if (attr
== &ttm_page_pool_alloc_size
)
248 val
= m
->options
.alloc_size
;
250 val
= val
* (PAGE_SIZE
>> 10);
252 return snprintf(buffer
, PAGE_SIZE
, "%u\n", val
);
255 static const struct sysfs_ops ttm_pool_sysfs_ops
= {
256 .show
= &ttm_pool_show
,
257 .store
= &ttm_pool_store
,
260 static struct kobj_type ttm_pool_kobj_type
= {
261 .release
= &ttm_pool_kobj_release
,
262 .sysfs_ops
= &ttm_pool_sysfs_ops
,
263 .default_attrs
= ttm_pool_attrs
,
266 static int ttm_set_pages_caching(struct dma_pool
*pool
,
267 struct page
**pages
, unsigned cpages
)
270 /* Set page caching */
271 if (pool
->type
& IS_UC
) {
272 r
= ttm_set_pages_array_uc(pages
, cpages
);
274 pr_err("%s: Failed to set %d pages to uc!\n",
275 pool
->dev_name
, cpages
);
277 if (pool
->type
& IS_WC
) {
278 r
= ttm_set_pages_array_wc(pages
, cpages
);
280 pr_err("%s: Failed to set %d pages to wc!\n",
281 pool
->dev_name
, cpages
);
286 static void __ttm_dma_free_page(struct dma_pool
*pool
, struct dma_page
*d_page
)
288 dma_addr_t dma
= d_page
->dma
;
289 d_page
->vaddr
&= ~VADDR_FLAG_HUGE_POOL
;
290 dma_free_coherent(pool
->dev
, pool
->size
, (void *)d_page
->vaddr
, dma
);
295 static struct dma_page
*__ttm_dma_alloc_page(struct dma_pool
*pool
)
297 struct dma_page
*d_page
;
298 unsigned long attrs
= 0;
301 d_page
= kmalloc(sizeof(struct dma_page
), GFP_KERNEL
);
305 if (pool
->type
& IS_HUGE
)
306 attrs
= DMA_ATTR_NO_WARN
;
308 vaddr
= dma_alloc_attrs(pool
->dev
, pool
->size
, &d_page
->dma
,
309 pool
->gfp_flags
, attrs
);
311 if (is_vmalloc_addr(vaddr
))
312 d_page
->p
= vmalloc_to_page(vaddr
);
314 d_page
->p
= virt_to_page(vaddr
);
315 d_page
->vaddr
= (unsigned long)vaddr
;
316 if (pool
->type
& IS_HUGE
)
317 d_page
->vaddr
|= VADDR_FLAG_HUGE_POOL
;
324 static enum pool_type
ttm_to_type(int flags
, enum ttm_caching_state cstate
)
326 enum pool_type type
= IS_UNDEFINED
;
328 if (flags
& TTM_PAGE_FLAG_DMA32
)
330 if (cstate
== tt_cached
)
332 else if (cstate
== tt_uncached
)
340 static void ttm_pool_update_free_locked(struct dma_pool
*pool
,
341 unsigned freed_pages
)
343 pool
->npages_free
-= freed_pages
;
344 pool
->nfrees
+= freed_pages
;
348 /* set memory back to wb and free the pages. */
349 static void ttm_dma_page_put(struct dma_pool
*pool
, struct dma_page
*d_page
)
351 struct page
*page
= d_page
->p
;
354 /* Don't set WB on WB page pool. */
355 if (!(pool
->type
& IS_CACHED
)) {
356 num_pages
= pool
->size
/ PAGE_SIZE
;
357 if (ttm_set_pages_wb(page
, num_pages
))
358 pr_err("%s: Failed to set %d pages to wb!\n",
359 pool
->dev_name
, num_pages
);
362 list_del(&d_page
->page_list
);
363 __ttm_dma_free_page(pool
, d_page
);
366 static void ttm_dma_pages_put(struct dma_pool
*pool
, struct list_head
*d_pages
,
367 struct page
*pages
[], unsigned npages
)
369 struct dma_page
*d_page
, *tmp
;
371 if (pool
->type
& IS_HUGE
) {
372 list_for_each_entry_safe(d_page
, tmp
, d_pages
, page_list
)
373 ttm_dma_page_put(pool
, d_page
);
378 /* Don't set WB on WB page pool. */
379 if (npages
&& !(pool
->type
& IS_CACHED
) &&
380 ttm_set_pages_array_wb(pages
, npages
))
381 pr_err("%s: Failed to set %d pages to wb!\n",
382 pool
->dev_name
, npages
);
384 list_for_each_entry_safe(d_page
, tmp
, d_pages
, page_list
) {
385 list_del(&d_page
->page_list
);
386 __ttm_dma_free_page(pool
, d_page
);
391 * Free pages from pool.
393 * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
394 * number of pages in one go.
396 * @pool: to free the pages from
397 * @nr_free: If set to true will free all pages in pool
398 * @use_static: Safe to use static buffer
400 static unsigned ttm_dma_page_pool_free(struct dma_pool
*pool
, unsigned nr_free
,
403 static struct page
*static_buf
[NUM_PAGES_TO_ALLOC
];
404 unsigned long irq_flags
;
405 struct dma_page
*dma_p
, *tmp
;
406 struct page
**pages_to_free
;
407 struct list_head d_pages
;
408 unsigned freed_pages
= 0,
409 npages_to_free
= nr_free
;
411 if (NUM_PAGES_TO_ALLOC
< nr_free
)
412 npages_to_free
= NUM_PAGES_TO_ALLOC
;
415 pr_debug("%s: (%s:%d) Attempting to free %d (%d) pages\n",
416 pool
->dev_name
, pool
->name
, current
->pid
,
417 npages_to_free
, nr_free
);
421 pages_to_free
= static_buf
;
423 pages_to_free
= kmalloc_array(npages_to_free
,
424 sizeof(struct page
*),
427 if (!pages_to_free
) {
428 pr_debug("%s: Failed to allocate memory for pool free operation\n",
432 INIT_LIST_HEAD(&d_pages
);
434 spin_lock_irqsave(&pool
->lock
, irq_flags
);
436 /* We picking the oldest ones off the list */
437 list_for_each_entry_safe_reverse(dma_p
, tmp
, &pool
->free_list
,
439 if (freed_pages
>= npages_to_free
)
442 /* Move the dma_page from one list to another. */
443 list_move(&dma_p
->page_list
, &d_pages
);
445 pages_to_free
[freed_pages
++] = dma_p
->p
;
446 /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
447 if (freed_pages
>= NUM_PAGES_TO_ALLOC
) {
449 ttm_pool_update_free_locked(pool
, freed_pages
);
451 * Because changing page caching is costly
452 * we unlock the pool to prevent stalling.
454 spin_unlock_irqrestore(&pool
->lock
, irq_flags
);
456 ttm_dma_pages_put(pool
, &d_pages
, pages_to_free
,
459 INIT_LIST_HEAD(&d_pages
);
461 if (likely(nr_free
!= FREE_ALL_PAGES
))
462 nr_free
-= freed_pages
;
464 if (NUM_PAGES_TO_ALLOC
>= nr_free
)
465 npages_to_free
= nr_free
;
467 npages_to_free
= NUM_PAGES_TO_ALLOC
;
471 /* free all so restart the processing */
475 /* Not allowed to fall through or break because
476 * following context is inside spinlock while we are
484 /* remove range of pages from the pool */
486 ttm_pool_update_free_locked(pool
, freed_pages
);
487 nr_free
-= freed_pages
;
490 spin_unlock_irqrestore(&pool
->lock
, irq_flags
);
493 ttm_dma_pages_put(pool
, &d_pages
, pages_to_free
, freed_pages
);
495 if (pages_to_free
!= static_buf
)
496 kfree(pages_to_free
);
500 static void ttm_dma_free_pool(struct device
*dev
, enum pool_type type
)
502 struct device_pools
*p
;
503 struct dma_pool
*pool
;
508 mutex_lock(&_manager
->lock
);
509 list_for_each_entry_reverse(p
, &_manager
->pools
, pools
) {
513 if (pool
->type
!= type
)
521 list_for_each_entry_reverse(pool
, &dev
->dma_pools
, pools
) {
522 if (pool
->type
!= type
)
524 /* Takes a spinlock.. */
525 /* OK to use static buffer since global mutex is held. */
526 ttm_dma_page_pool_free(pool
, FREE_ALL_PAGES
, true);
527 WARN_ON(((pool
->npages_in_use
+ pool
->npages_free
) != 0));
528 /* This code path is called after _all_ references to the
529 * struct device has been dropped - so nobody should be
530 * touching it. In case somebody is trying to _add_ we are
531 * guarded by the mutex. */
532 list_del(&pool
->pools
);
536 mutex_unlock(&_manager
->lock
);
540 * On free-ing of the 'struct device' this deconstructor is run.
541 * Albeit the pool might have already been freed earlier.
543 static void ttm_dma_pool_release(struct device
*dev
, void *res
)
545 struct dma_pool
*pool
= *(struct dma_pool
**)res
;
548 ttm_dma_free_pool(dev
, pool
->type
);
551 static int ttm_dma_pool_match(struct device
*dev
, void *res
, void *match_data
)
553 return *(struct dma_pool
**)res
== match_data
;
556 static struct dma_pool
*ttm_dma_pool_init(struct device
*dev
, gfp_t flags
,
559 const char *n
[] = {"wc", "uc", "cached", " dma32", "huge"};
560 enum pool_type t
[] = {IS_WC
, IS_UC
, IS_CACHED
, IS_DMA32
, IS_HUGE
};
561 struct device_pools
*sec_pool
= NULL
;
562 struct dma_pool
*pool
= NULL
, **ptr
;
570 ptr
= devres_alloc(ttm_dma_pool_release
, sizeof(*ptr
), GFP_KERNEL
);
576 pool
= kmalloc_node(sizeof(struct dma_pool
), GFP_KERNEL
,
581 sec_pool
= kmalloc_node(sizeof(struct device_pools
), GFP_KERNEL
,
586 INIT_LIST_HEAD(&sec_pool
->pools
);
588 sec_pool
->pool
= pool
;
590 INIT_LIST_HEAD(&pool
->free_list
);
591 INIT_LIST_HEAD(&pool
->pools
);
592 spin_lock_init(&pool
->lock
);
594 pool
->npages_free
= pool
->npages_in_use
= 0;
596 pool
->gfp_flags
= flags
;
598 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
599 pool
->size
= HPAGE_PMD_SIZE
;
604 pool
->size
= PAGE_SIZE
;
608 for (i
= 0; i
< ARRAY_SIZE(t
); 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
;
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
)
664 * Free pages the pages that failed to change the caching state. If there
665 * are pages that have changed their caching state already put them to the
668 static void ttm_dma_handle_caching_state_failure(struct dma_pool
*pool
,
669 struct list_head
*d_pages
,
670 struct page
**failed_pages
,
673 struct dma_page
*d_page
, *tmp
;
680 /* Find the failed page. */
681 list_for_each_entry_safe(d_page
, tmp
, d_pages
, page_list
) {
684 /* .. and then progress over the full list. */
685 list_del(&d_page
->page_list
);
686 __ttm_dma_free_page(pool
, d_page
);
696 * Allocate 'count' pages, and put 'need' number of them on the
697 * 'pages' and as well on the 'dma_address' starting at 'dma_offset' offset.
698 * The full list of pages should also be on 'd_pages'.
699 * We return zero for success, and negative numbers as errors.
701 static int ttm_dma_pool_alloc_new_pages(struct dma_pool
*pool
,
702 struct list_head
*d_pages
,
705 struct page
**caching_array
;
706 struct dma_page
*dma_p
;
709 unsigned i
, j
, npages
, cpages
;
710 unsigned max_cpages
= min(count
,
711 (unsigned)(PAGE_SIZE
/sizeof(struct page
*)));
713 /* allocate array for page caching change */
714 caching_array
= kmalloc_array(max_cpages
, sizeof(struct page
*),
717 if (!caching_array
) {
718 pr_debug("%s: Unable to allocate table for new pages\n",
724 pr_debug("%s: (%s:%d) Getting %d pages\n",
725 pool
->dev_name
, pool
->name
, current
->pid
, count
);
727 for (i
= 0, cpages
= 0; i
< count
; ++i
) {
728 dma_p
= __ttm_dma_alloc_page(pool
);
730 pr_debug("%s: Unable to get page %u\n",
733 /* store already allocated pages in the pool after
734 * setting the caching state */
736 r
= ttm_set_pages_caching(pool
, caching_array
,
739 ttm_dma_handle_caching_state_failure(
740 pool
, d_pages
, caching_array
,
747 list_add(&dma_p
->page_list
, d_pages
);
749 #ifdef CONFIG_HIGHMEM
750 /* gfp flags of highmem page should never be dma32 so we
751 * we should be fine in such case
757 npages
= pool
->size
/ PAGE_SIZE
;
758 for (j
= 0; j
< npages
; ++j
) {
759 caching_array
[cpages
++] = p
+ j
;
760 if (cpages
== max_cpages
) {
761 /* Note: Cannot hold the spinlock */
762 r
= ttm_set_pages_caching(pool
, caching_array
,
765 ttm_dma_handle_caching_state_failure(
766 pool
, d_pages
, caching_array
,
776 r
= ttm_set_pages_caching(pool
, caching_array
, cpages
);
778 ttm_dma_handle_caching_state_failure(pool
, d_pages
,
779 caching_array
, cpages
);
782 kfree(caching_array
);
787 * @return count of pages still required to fulfill the request.
789 static int ttm_dma_page_pool_fill_locked(struct dma_pool
*pool
,
790 unsigned long *irq_flags
)
792 unsigned count
= _manager
->options
.small
;
793 int r
= pool
->npages_free
;
795 if (count
> pool
->npages_free
) {
796 struct list_head d_pages
;
798 INIT_LIST_HEAD(&d_pages
);
800 spin_unlock_irqrestore(&pool
->lock
, *irq_flags
);
802 /* Returns how many more are neccessary to fulfill the
804 r
= ttm_dma_pool_alloc_new_pages(pool
, &d_pages
, count
);
806 spin_lock_irqsave(&pool
->lock
, *irq_flags
);
808 /* Add the fresh to the end.. */
809 list_splice(&d_pages
, &pool
->free_list
);
811 pool
->npages_free
+= count
;
814 struct dma_page
*d_page
;
817 pr_debug("%s: Failed to fill %s pool (r:%d)!\n",
818 pool
->dev_name
, pool
->name
, r
);
820 list_for_each_entry(d_page
, &d_pages
, page_list
) {
823 list_splice_tail(&d_pages
, &pool
->free_list
);
824 pool
->npages_free
+= cpages
;
832 * The populate list is actually a stack (not that is matters as TTM
833 * allocates one page at a time.
834 * return dma_page pointer if success, otherwise NULL.
836 static struct dma_page
*ttm_dma_pool_get_pages(struct dma_pool
*pool
,
837 struct ttm_dma_tt
*ttm_dma
,
840 struct dma_page
*d_page
= NULL
;
841 struct ttm_tt
*ttm
= &ttm_dma
->ttm
;
842 unsigned long irq_flags
;
845 spin_lock_irqsave(&pool
->lock
, irq_flags
);
846 count
= ttm_dma_page_pool_fill_locked(pool
, &irq_flags
);
848 d_page
= list_first_entry(&pool
->free_list
, struct dma_page
, page_list
);
849 ttm
->pages
[index
] = d_page
->p
;
850 ttm_dma
->dma_address
[index
] = d_page
->dma
;
851 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
);
859 static gfp_t
ttm_dma_pool_gfp_flags(struct ttm_dma_tt
*ttm_dma
, bool huge
)
861 struct ttm_tt
*ttm
= &ttm_dma
->ttm
;
864 if (ttm
->page_flags
& TTM_PAGE_FLAG_DMA32
)
865 gfp_flags
= GFP_USER
| GFP_DMA32
;
867 gfp_flags
= GFP_HIGHUSER
;
868 if (ttm
->page_flags
& TTM_PAGE_FLAG_ZERO_ALLOC
)
869 gfp_flags
|= __GFP_ZERO
;
872 gfp_flags
|= GFP_TRANSHUGE_LIGHT
| __GFP_NORETRY
|
873 __GFP_KSWAPD_RECLAIM
;
874 gfp_flags
&= ~__GFP_MOVABLE
;
875 gfp_flags
&= ~__GFP_COMP
;
878 if (ttm
->page_flags
& TTM_PAGE_FLAG_NO_RETRY
)
879 gfp_flags
|= __GFP_RETRY_MAYFAIL
;
885 * On success pages list will hold count number of correctly
886 * cached pages. On failure will hold the negative return value (-ENOMEM, etc).
888 int ttm_dma_populate(struct ttm_dma_tt
*ttm_dma
, struct device
*dev
,
889 struct ttm_operation_ctx
*ctx
)
891 struct ttm_tt
*ttm
= &ttm_dma
->ttm
;
892 struct ttm_mem_global
*mem_glob
= ttm
->bdev
->glob
->mem_glob
;
893 unsigned long num_pages
= ttm
->num_pages
;
894 struct dma_pool
*pool
;
895 struct dma_page
*d_page
;
900 if (ttm
->state
!= tt_unpopulated
)
903 if (ttm_check_under_lowerlimit(mem_glob
, num_pages
, ctx
))
906 INIT_LIST_HEAD(&ttm_dma
->pages_list
);
909 type
= ttm_to_type(ttm
->page_flags
, ttm
->caching_state
);
911 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
912 if (ttm
->page_flags
& TTM_PAGE_FLAG_DMA32
)
915 pool
= ttm_dma_find_pool(dev
, type
| IS_HUGE
);
917 gfp_t gfp_flags
= ttm_dma_pool_gfp_flags(ttm_dma
, true);
919 pool
= ttm_dma_pool_init(dev
, gfp_flags
, type
| IS_HUGE
);
920 if (IS_ERR_OR_NULL(pool
))
924 while (num_pages
>= HPAGE_PMD_NR
) {
927 d_page
= ttm_dma_pool_get_pages(pool
, ttm_dma
, i
);
931 ret
= ttm_mem_global_alloc_page(mem_glob
, ttm
->pages
[i
],
933 if (unlikely(ret
!= 0)) {
934 ttm_dma_unpopulate(ttm_dma
, dev
);
938 d_page
->vaddr
|= VADDR_FLAG_UPDATED_COUNT
;
939 for (j
= i
+ 1; j
< (i
+ HPAGE_PMD_NR
); ++j
) {
940 ttm
->pages
[j
] = ttm
->pages
[j
- 1] + 1;
941 ttm_dma
->dma_address
[j
] = ttm_dma
->dma_address
[j
- 1] +
946 num_pages
-= HPAGE_PMD_NR
;
952 pool
= ttm_dma_find_pool(dev
, type
);
954 gfp_t gfp_flags
= ttm_dma_pool_gfp_flags(ttm_dma
, false);
956 pool
= ttm_dma_pool_init(dev
, gfp_flags
, type
);
957 if (IS_ERR_OR_NULL(pool
))
962 d_page
= ttm_dma_pool_get_pages(pool
, ttm_dma
, i
);
964 ttm_dma_unpopulate(ttm_dma
, dev
);
968 ret
= ttm_mem_global_alloc_page(mem_glob
, ttm
->pages
[i
],
970 if (unlikely(ret
!= 0)) {
971 ttm_dma_unpopulate(ttm_dma
, dev
);
975 d_page
->vaddr
|= VADDR_FLAG_UPDATED_COUNT
;
980 if (unlikely(ttm
->page_flags
& TTM_PAGE_FLAG_SWAPPED
)) {
981 ret
= ttm_tt_swapin(ttm
);
982 if (unlikely(ret
!= 0)) {
983 ttm_dma_unpopulate(ttm_dma
, dev
);
988 ttm
->state
= tt_unbound
;
991 EXPORT_SYMBOL_GPL(ttm_dma_populate
);
993 /* Put all pages in pages list to correct pool to wait for reuse */
994 void ttm_dma_unpopulate(struct ttm_dma_tt
*ttm_dma
, struct device
*dev
)
996 struct ttm_tt
*ttm
= &ttm_dma
->ttm
;
997 struct ttm_mem_global
*mem_glob
= ttm
->bdev
->glob
->mem_glob
;
998 struct dma_pool
*pool
;
999 struct dma_page
*d_page
, *next
;
1000 enum pool_type type
;
1001 bool is_cached
= false;
1002 unsigned count
, i
, npages
= 0;
1003 unsigned long irq_flags
;
1005 type
= ttm_to_type(ttm
->page_flags
, ttm
->caching_state
);
1007 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1008 pool
= ttm_dma_find_pool(dev
, type
| IS_HUGE
);
1011 list_for_each_entry_safe(d_page
, next
, &ttm_dma
->pages_list
,
1013 if (!(d_page
->vaddr
& VADDR_FLAG_HUGE_POOL
))
1017 if (d_page
->vaddr
& VADDR_FLAG_UPDATED_COUNT
) {
1018 ttm_mem_global_free_page(mem_glob
, d_page
->p
,
1020 d_page
->vaddr
&= ~VADDR_FLAG_UPDATED_COUNT
;
1022 ttm_dma_page_put(pool
, d_page
);
1025 spin_lock_irqsave(&pool
->lock
, irq_flags
);
1026 pool
->npages_in_use
-= count
;
1027 pool
->nfrees
+= count
;
1028 spin_unlock_irqrestore(&pool
->lock
, irq_flags
);
1032 pool
= ttm_dma_find_pool(dev
, type
);
1036 is_cached
= (ttm_dma_find_pool(pool
->dev
,
1037 ttm_to_type(ttm
->page_flags
, tt_cached
)) == pool
);
1039 /* make sure pages array match list and count number of pages */
1041 list_for_each_entry_safe(d_page
, next
, &ttm_dma
->pages_list
,
1043 ttm
->pages
[count
] = d_page
->p
;
1046 if (d_page
->vaddr
& VADDR_FLAG_UPDATED_COUNT
) {
1047 ttm_mem_global_free_page(mem_glob
, d_page
->p
,
1049 d_page
->vaddr
&= ~VADDR_FLAG_UPDATED_COUNT
;
1053 ttm_dma_page_put(pool
, d_page
);
1056 spin_lock_irqsave(&pool
->lock
, irq_flags
);
1057 pool
->npages_in_use
-= count
;
1059 pool
->nfrees
+= count
;
1061 pool
->npages_free
+= count
;
1062 list_splice(&ttm_dma
->pages_list
, &pool
->free_list
);
1064 * Wait to have at at least NUM_PAGES_TO_ALLOC number of pages
1065 * to free in order to minimize calls to set_memory_wb().
1067 if (pool
->npages_free
>= (_manager
->options
.max_size
+
1068 NUM_PAGES_TO_ALLOC
))
1069 npages
= pool
->npages_free
- _manager
->options
.max_size
;
1071 spin_unlock_irqrestore(&pool
->lock
, irq_flags
);
1073 INIT_LIST_HEAD(&ttm_dma
->pages_list
);
1074 for (i
= 0; i
< ttm
->num_pages
; i
++) {
1075 ttm
->pages
[i
] = NULL
;
1076 ttm_dma
->dma_address
[i
] = 0;
1079 /* shrink pool if necessary (only on !is_cached pools)*/
1081 ttm_dma_page_pool_free(pool
, npages
, false);
1082 ttm
->state
= tt_unpopulated
;
1084 EXPORT_SYMBOL_GPL(ttm_dma_unpopulate
);
1087 * Callback for mm to request pool to reduce number of page held.
1089 * XXX: (dchinner) Deadlock warning!
1091 * I'm getting sadder as I hear more pathetical whimpers about needing per-pool
1094 static unsigned long
1095 ttm_dma_pool_shrink_scan(struct shrinker
*shrink
, struct shrink_control
*sc
)
1097 static unsigned start_pool
;
1099 unsigned pool_offset
;
1100 unsigned shrink_pages
= sc
->nr_to_scan
;
1101 struct device_pools
*p
;
1102 unsigned long freed
= 0;
1104 if (list_empty(&_manager
->pools
))
1107 if (!mutex_trylock(&_manager
->lock
))
1109 if (!_manager
->npools
)
1111 pool_offset
= ++start_pool
% _manager
->npools
;
1112 list_for_each_entry(p
, &_manager
->pools
, pools
) {
1117 if (shrink_pages
== 0)
1119 /* Do it in round-robin fashion. */
1120 if (++idx
< pool_offset
)
1122 nr_free
= shrink_pages
;
1123 /* OK to use static buffer since global mutex is held. */
1124 shrink_pages
= ttm_dma_page_pool_free(p
->pool
, nr_free
, true);
1125 freed
+= nr_free
- shrink_pages
;
1127 pr_debug("%s: (%s:%d) Asked to shrink %d, have %d more to go\n",
1128 p
->pool
->dev_name
, p
->pool
->name
, current
->pid
,
1129 nr_free
, shrink_pages
);
1132 mutex_unlock(&_manager
->lock
);
1136 static unsigned long
1137 ttm_dma_pool_shrink_count(struct shrinker
*shrink
, struct shrink_control
*sc
)
1139 struct device_pools
*p
;
1140 unsigned long count
= 0;
1142 if (!mutex_trylock(&_manager
->lock
))
1144 list_for_each_entry(p
, &_manager
->pools
, pools
)
1145 count
+= p
->pool
->npages_free
;
1146 mutex_unlock(&_manager
->lock
);
1150 static int ttm_dma_pool_mm_shrink_init(struct ttm_pool_manager
*manager
)
1152 manager
->mm_shrink
.count_objects
= ttm_dma_pool_shrink_count
;
1153 manager
->mm_shrink
.scan_objects
= &ttm_dma_pool_shrink_scan
;
1154 manager
->mm_shrink
.seeks
= 1;
1155 return register_shrinker(&manager
->mm_shrink
);
1158 static void ttm_dma_pool_mm_shrink_fini(struct ttm_pool_manager
*manager
)
1160 unregister_shrinker(&manager
->mm_shrink
);
1163 int ttm_dma_page_alloc_init(struct ttm_mem_global
*glob
, unsigned max_pages
)
1169 pr_info("Initializing DMA pool allocator\n");
1171 _manager
= kzalloc(sizeof(*_manager
), GFP_KERNEL
);
1175 mutex_init(&_manager
->lock
);
1176 INIT_LIST_HEAD(&_manager
->pools
);
1178 _manager
->options
.max_size
= max_pages
;
1179 _manager
->options
.small
= SMALL_ALLOCATION
;
1180 _manager
->options
.alloc_size
= NUM_PAGES_TO_ALLOC
;
1182 /* This takes care of auto-freeing the _manager */
1183 ret
= kobject_init_and_add(&_manager
->kobj
, &ttm_pool_kobj_type
,
1184 &glob
->kobj
, "dma_pool");
1185 if (unlikely(ret
!= 0))
1188 ret
= ttm_dma_pool_mm_shrink_init(_manager
);
1189 if (unlikely(ret
!= 0))
1194 kobject_put(&_manager
->kobj
);
1199 void ttm_dma_page_alloc_fini(void)
1201 struct device_pools
*p
, *t
;
1203 pr_info("Finalizing DMA pool allocator\n");
1204 ttm_dma_pool_mm_shrink_fini(_manager
);
1206 list_for_each_entry_safe_reverse(p
, t
, &_manager
->pools
, pools
) {
1207 dev_dbg(p
->dev
, "(%s:%d) Freeing.\n", p
->pool
->name
,
1209 WARN_ON(devres_destroy(p
->dev
, ttm_dma_pool_release
,
1210 ttm_dma_pool_match
, p
->pool
));
1211 ttm_dma_free_pool(p
->dev
, p
->pool
->type
);
1213 kobject_put(&_manager
->kobj
);
1217 int ttm_dma_page_alloc_debugfs(struct seq_file
*m
, void *data
)
1219 struct device_pools
*p
;
1220 struct dma_pool
*pool
= NULL
;
1223 seq_printf(m
, "No pool allocator running.\n");
1226 seq_printf(m
, " pool refills pages freed inuse available name\n");
1227 mutex_lock(&_manager
->lock
);
1228 list_for_each_entry(p
, &_manager
->pools
, pools
) {
1229 struct device
*dev
= p
->dev
;
1233 seq_printf(m
, "%13s %12ld %13ld %8d %8d %8s\n",
1234 pool
->name
, pool
->nrefills
,
1235 pool
->nfrees
, pool
->npages_in_use
,
1239 mutex_unlock(&_manager
->lock
);
1242 EXPORT_SYMBOL_GPL(ttm_dma_page_alloc_debugfs
);