drm/ttm: add debugfs entry to test pool shrinker v2
[linux-2.6-microblaze.git] / drivers / gpu / drm / ttm / ttm_pool.c
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2020 Advanced Micro Devices, Inc.
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors: Christian König
24  */
25
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.
29  *
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.
32  */
33
34 #include <linux/module.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/sched/mm.h>
37
38 #ifdef CONFIG_X86
39 #include <asm/set_memory.h>
40 #endif
41
42 #include <drm/ttm/ttm_pool.h>
43 #include <drm/ttm/ttm_bo_driver.h>
44 #include <drm/ttm/ttm_tt.h>
45
46 #include "ttm_module.h"
47
48 /**
49  * struct ttm_pool_dma - Helper object for coherent DMA mappings
50  *
51  * @addr: original DMA address returned for the mapping
52  * @vaddr: original vaddr return for the mapping and order in the lower bits
53  */
54 struct ttm_pool_dma {
55         dma_addr_t addr;
56         unsigned long vaddr;
57 };
58
59 static unsigned long page_pool_size;
60
61 MODULE_PARM_DESC(page_pool_size, "Number of pages in the WC/UC/DMA pool");
62 module_param(page_pool_size, ulong, 0644);
63
64 static atomic_long_t allocated_pages;
65
66 static struct ttm_pool_type global_write_combined[MAX_ORDER];
67 static struct ttm_pool_type global_uncached[MAX_ORDER];
68
69 static struct ttm_pool_type global_dma32_write_combined[MAX_ORDER];
70 static struct ttm_pool_type global_dma32_uncached[MAX_ORDER];
71
72 static spinlock_t shrinker_lock;
73 static struct list_head shrinker_list;
74 static struct shrinker mm_shrinker;
75
76 /* Allocate pages of size 1 << order with the given gfp_flags */
77 static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,
78                                         unsigned int order)
79 {
80         unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
81         struct ttm_pool_dma *dma;
82         struct page *p;
83         void *vaddr;
84
85         if (order) {
86                 gfp_flags |= GFP_TRANSHUGE_LIGHT | __GFP_NORETRY |
87                         __GFP_KSWAPD_RECLAIM;
88                 gfp_flags &= ~__GFP_MOVABLE;
89                 gfp_flags &= ~__GFP_COMP;
90         }
91
92         if (!pool->use_dma_alloc) {
93                 p = alloc_pages(gfp_flags, order);
94                 if (p)
95                         p->private = order;
96                 return p;
97         }
98
99         dma = kmalloc(sizeof(*dma), GFP_KERNEL);
100         if (!dma)
101                 return NULL;
102
103         if (order)
104                 attr |= DMA_ATTR_NO_WARN;
105
106         vaddr = dma_alloc_attrs(pool->dev, (1ULL << order) * PAGE_SIZE,
107                                 &dma->addr, gfp_flags, attr);
108         if (!vaddr)
109                 goto error_free;
110
111         /* TODO: This is an illegal abuse of the DMA API, but we need to rework
112          * TTM page fault handling and extend the DMA API to clean this up.
113          */
114         if (is_vmalloc_addr(vaddr))
115                 p = vmalloc_to_page(vaddr);
116         else
117                 p = virt_to_page(vaddr);
118
119         dma->vaddr = (unsigned long)vaddr | order;
120         p->private = (unsigned long)dma;
121         return p;
122
123 error_free:
124         kfree(dma);
125         return NULL;
126 }
127
128 /* Reset the caching and pages of size 1 << order */
129 static void ttm_pool_free_page(struct ttm_pool *pool, enum ttm_caching caching,
130                                unsigned int order, struct page *p)
131 {
132         unsigned long attr = DMA_ATTR_FORCE_CONTIGUOUS;
133         struct ttm_pool_dma *dma;
134         void *vaddr;
135
136 #ifdef CONFIG_X86
137         /* We don't care that set_pages_wb is inefficient here. This is only
138          * used when we have to shrink and CPU overhead is irrelevant then.
139          */
140         if (caching != ttm_cached && !PageHighMem(p))
141                 set_pages_wb(p, 1 << order);
142 #endif
143
144         if (!pool || !pool->use_dma_alloc) {
145                 __free_pages(p, order);
146                 return;
147         }
148
149         if (order)
150                 attr |= DMA_ATTR_NO_WARN;
151
152         dma = (void *)p->private;
153         vaddr = (void *)(dma->vaddr & PAGE_MASK);
154         dma_free_attrs(pool->dev, (1UL << order) * PAGE_SIZE, vaddr, dma->addr,
155                        attr);
156         kfree(dma);
157 }
158
159 /* Apply a new caching to an array of pages */
160 static int ttm_pool_apply_caching(struct page **first, struct page **last,
161                                   enum ttm_caching caching)
162 {
163 #ifdef CONFIG_X86
164         unsigned int num_pages = last - first;
165
166         if (!num_pages)
167                 return 0;
168
169         switch (caching) {
170         case ttm_cached:
171                 break;
172         case ttm_write_combined:
173                 return set_pages_array_wc(first, num_pages);
174         case ttm_uncached:
175                 return set_pages_array_uc(first, num_pages);
176         }
177 #endif
178         return 0;
179 }
180
181 /* Map pages of 1 << order size and fill the DMA address array  */
182 static int ttm_pool_map(struct ttm_pool *pool, unsigned int order,
183                         struct page *p, dma_addr_t **dma_addr)
184 {
185         dma_addr_t addr;
186         unsigned int i;
187
188         if (pool->use_dma_alloc) {
189                 struct ttm_pool_dma *dma = (void *)p->private;
190
191                 addr = dma->addr;
192         } else {
193                 size_t size = (1ULL << order) * PAGE_SIZE;
194
195                 addr = dma_map_page(pool->dev, p, 0, size, DMA_BIDIRECTIONAL);
196                 if (dma_mapping_error(pool->dev, **dma_addr))
197                         return -EFAULT;
198         }
199
200         for (i = 1 << order; i ; --i) {
201                 *(*dma_addr)++ = addr;
202                 addr += PAGE_SIZE;
203         }
204
205         return 0;
206 }
207
208 /* Unmap pages of 1 << order size */
209 static void ttm_pool_unmap(struct ttm_pool *pool, dma_addr_t dma_addr,
210                            unsigned int num_pages)
211 {
212         /* Unmapped while freeing the page */
213         if (pool->use_dma_alloc)
214                 return;
215
216         dma_unmap_page(pool->dev, dma_addr, (long)num_pages << PAGE_SHIFT,
217                        DMA_BIDIRECTIONAL);
218 }
219
220 /* Give pages into a specific pool_type */
221 static void ttm_pool_type_give(struct ttm_pool_type *pt, struct page *p)
222 {
223         spin_lock(&pt->lock);
224         list_add(&p->lru, &pt->pages);
225         spin_unlock(&pt->lock);
226         atomic_long_add(1 << pt->order, &allocated_pages);
227 }
228
229 /* Take pages from a specific pool_type, return NULL when nothing available */
230 static struct page *ttm_pool_type_take(struct ttm_pool_type *pt)
231 {
232         struct page *p;
233
234         spin_lock(&pt->lock);
235         p = list_first_entry_or_null(&pt->pages, typeof(*p), lru);
236         if (p) {
237                 atomic_long_sub(1 << pt->order, &allocated_pages);
238                 list_del(&p->lru);
239         }
240         spin_unlock(&pt->lock);
241
242         return p;
243 }
244
245 /* Initialize and add a pool type to the global shrinker list */
246 static void ttm_pool_type_init(struct ttm_pool_type *pt, struct ttm_pool *pool,
247                                enum ttm_caching caching, unsigned int order)
248 {
249         pt->pool = pool;
250         pt->caching = caching;
251         pt->order = order;
252         spin_lock_init(&pt->lock);
253         INIT_LIST_HEAD(&pt->pages);
254
255         spin_lock(&shrinker_lock);
256         list_add_tail(&pt->shrinker_list, &shrinker_list);
257         spin_unlock(&shrinker_lock);
258 }
259
260 /* Remove a pool_type from the global shrinker list and free all pages */
261 static void ttm_pool_type_fini(struct ttm_pool_type *pt)
262 {
263         struct page *p, *tmp;
264
265         spin_lock(&shrinker_lock);
266         list_del(&pt->shrinker_list);
267         spin_unlock(&shrinker_lock);
268
269         list_for_each_entry_safe(p, tmp, &pt->pages, lru)
270                 ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
271 }
272
273 /* Return the pool_type to use for the given caching and order */
274 static struct ttm_pool_type *ttm_pool_select_type(struct ttm_pool *pool,
275                                                   enum ttm_caching caching,
276                                                   unsigned int order)
277 {
278         if (pool->use_dma_alloc)
279                 return &pool->caching[caching].orders[order];
280
281 #ifdef CONFIG_X86
282         switch (caching) {
283         case ttm_write_combined:
284                 if (pool->use_dma32)
285                         return &global_dma32_write_combined[order];
286
287                 return &global_write_combined[order];
288         case ttm_uncached:
289                 if (pool->use_dma32)
290                         return &global_dma32_uncached[order];
291
292                 return &global_uncached[order];
293         default:
294                 break;
295         }
296 #endif
297
298         return NULL;
299 }
300
301 /* Free pages using the global shrinker list */
302 static unsigned int ttm_pool_shrink(void)
303 {
304         struct ttm_pool_type *pt;
305         unsigned int num_freed;
306         struct page *p;
307
308         spin_lock(&shrinker_lock);
309         pt = list_first_entry(&shrinker_list, typeof(*pt), shrinker_list);
310
311         p = ttm_pool_type_take(pt);
312         if (p) {
313                 ttm_pool_free_page(pt->pool, pt->caching, pt->order, p);
314                 num_freed = 1 << pt->order;
315         } else {
316                 num_freed = 0;
317         }
318
319         list_move_tail(&pt->shrinker_list, &shrinker_list);
320         spin_unlock(&shrinker_lock);
321
322         return num_freed;
323 }
324
325 /* Return the allocation order based for a page */
326 static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)
327 {
328         if (pool->use_dma_alloc) {
329                 struct ttm_pool_dma *dma = (void *)p->private;
330
331                 return dma->vaddr & ~PAGE_MASK;
332         }
333
334         return p->private;
335 }
336
337 /**
338  * ttm_pool_alloc - Fill a ttm_tt object
339  *
340  * @pool: ttm_pool to use
341  * @tt: ttm_tt object to fill
342  * @ctx: operation context
343  *
344  * Fill the ttm_tt object with pages and also make sure to DMA map them when
345  * necessary.
346  *
347  * Returns: 0 on successe, negative error code otherwise.
348  */
349 int ttm_pool_alloc(struct ttm_pool *pool, struct ttm_tt *tt,
350                    struct ttm_operation_ctx *ctx)
351 {
352         unsigned long num_pages = tt->num_pages;
353         dma_addr_t *dma_addr = tt->dma_address;
354         struct page **caching = tt->pages;
355         struct page **pages = tt->pages;
356         gfp_t gfp_flags = GFP_USER;
357         unsigned int i, order;
358         struct page *p;
359         int r;
360
361         WARN_ON(!num_pages || ttm_tt_is_populated(tt));
362         WARN_ON(dma_addr && !pool->dev);
363
364         if (tt->page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
365                 gfp_flags |= __GFP_ZERO;
366
367         if (ctx->gfp_retry_mayfail)
368                 gfp_flags |= __GFP_RETRY_MAYFAIL;
369
370         if (pool->use_dma32)
371                 gfp_flags |= GFP_DMA32;
372         else
373                 gfp_flags |= GFP_HIGHUSER;
374
375         for (order = min(MAX_ORDER - 1UL, __fls(num_pages)); num_pages;
376              order = min_t(unsigned int, order, __fls(num_pages))) {
377                 bool apply_caching = false;
378                 struct ttm_pool_type *pt;
379
380                 pt = ttm_pool_select_type(pool, tt->caching, order);
381                 p = pt ? ttm_pool_type_take(pt) : NULL;
382                 if (p) {
383                         apply_caching = true;
384                 } else {
385                         p = ttm_pool_alloc_page(pool, gfp_flags, order);
386                         if (p && PageHighMem(p))
387                                 apply_caching = true;
388                 }
389
390                 if (!p) {
391                         if (order) {
392                                 --order;
393                                 continue;
394                         }
395                         r = -ENOMEM;
396                         goto error_free_all;
397                 }
398
399                 if (apply_caching) {
400                         r = ttm_pool_apply_caching(caching, pages,
401                                                    tt->caching);
402                         if (r)
403                                 goto error_free_page;
404                         caching = pages + (1 << order);
405                 }
406
407                 r = ttm_mem_global_alloc_page(&ttm_mem_glob, p,
408                                               (1 << order) * PAGE_SIZE,
409                                               ctx);
410                 if (r)
411                         goto error_free_page;
412
413                 if (dma_addr) {
414                         r = ttm_pool_map(pool, order, p, &dma_addr);
415                         if (r)
416                                 goto error_global_free;
417                 }
418
419                 num_pages -= 1 << order;
420                 for (i = 1 << order; i; --i)
421                         *(pages++) = p++;
422         }
423
424         r = ttm_pool_apply_caching(caching, pages, tt->caching);
425         if (r)
426                 goto error_free_all;
427
428         return 0;
429
430 error_global_free:
431         ttm_mem_global_free_page(&ttm_mem_glob, p, (1 << order) * PAGE_SIZE);
432
433 error_free_page:
434         ttm_pool_free_page(pool, tt->caching, order, p);
435
436 error_free_all:
437         num_pages = tt->num_pages - num_pages;
438         for (i = 0; i < num_pages; ) {
439                 order = ttm_pool_page_order(pool, tt->pages[i]);
440                 ttm_pool_free_page(pool, tt->caching, order, tt->pages[i]);
441                 i += 1 << order;
442         }
443
444         return r;
445 }
446 EXPORT_SYMBOL(ttm_pool_alloc);
447
448 /**
449  * ttm_pool_free - Free the backing pages from a ttm_tt object
450  *
451  * @pool: Pool to give pages back to.
452  * @tt: ttm_tt object to unpopulate
453  *
454  * Give the packing pages back to a pool or free them
455  */
456 void ttm_pool_free(struct ttm_pool *pool, struct ttm_tt *tt)
457 {
458         unsigned int i;
459
460         for (i = 0; i < tt->num_pages; ) {
461                 struct page *p = tt->pages[i];
462                 unsigned int order, num_pages;
463                 struct ttm_pool_type *pt;
464
465                 order = ttm_pool_page_order(pool, p);
466                 num_pages = 1ULL << order;
467                 ttm_mem_global_free_page(&ttm_mem_glob, p,
468                                          num_pages * PAGE_SIZE);
469                 if (tt->dma_address)
470                         ttm_pool_unmap(pool, tt->dma_address[i], num_pages);
471
472                 pt = ttm_pool_select_type(pool, tt->caching, order);
473                 if (pt)
474                         ttm_pool_type_give(pt, tt->pages[i]);
475                 else
476                         ttm_pool_free_page(pool, tt->caching, order,
477                                            tt->pages[i]);
478
479                 i += num_pages;
480         }
481
482         while (atomic_long_read(&allocated_pages) > page_pool_size)
483                 ttm_pool_shrink();
484 }
485 EXPORT_SYMBOL(ttm_pool_free);
486
487 /**
488  * ttm_pool_init - Initialize a pool
489  *
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
494  *
495  * Initialize the pool and its pool types.
496  */
497 void ttm_pool_init(struct ttm_pool *pool, struct device *dev,
498                    bool use_dma_alloc, bool use_dma32)
499 {
500         unsigned int i, j;
501
502         WARN_ON(!dev && use_dma_alloc);
503
504         pool->dev = dev;
505         pool->use_dma_alloc = use_dma_alloc;
506         pool->use_dma32 = use_dma32;
507
508         for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
509                 for (j = 0; j < MAX_ORDER; ++j)
510                         ttm_pool_type_init(&pool->caching[i].orders[j],
511                                            pool, i, j);
512 }
513 EXPORT_SYMBOL(ttm_pool_init);
514
515 /**
516  * ttm_pool_fini - Cleanup a pool
517  *
518  * @pool: the pool to clean up
519  *
520  * Free all pages in the pool and unregister the types from the global
521  * shrinker.
522  */
523 void ttm_pool_fini(struct ttm_pool *pool)
524 {
525         unsigned int i, j;
526
527         for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i)
528                 for (j = 0; j < MAX_ORDER; ++j)
529                         ttm_pool_type_fini(&pool->caching[i].orders[j]);
530 }
531 EXPORT_SYMBOL(ttm_pool_fini);
532
533 /* As long as pages are available make sure to release at least one */
534 static unsigned long ttm_pool_shrinker_scan(struct shrinker *shrink,
535                                             struct shrink_control *sc)
536 {
537         unsigned long num_freed = 0;
538
539         do
540                 num_freed += ttm_pool_shrink();
541         while (!num_freed && atomic_long_read(&allocated_pages));
542
543         return num_freed;
544 }
545
546 /* Return the number of pages available or SHRINK_EMPTY if we have none */
547 static unsigned long ttm_pool_shrinker_count(struct shrinker *shrink,
548                                              struct shrink_control *sc)
549 {
550         unsigned long num_pages = atomic_long_read(&allocated_pages);
551
552         return num_pages ? num_pages : SHRINK_EMPTY;
553 }
554
555 #ifdef CONFIG_DEBUG_FS
556 /* Count the number of pages available in a pool_type */
557 static unsigned int ttm_pool_type_count(struct ttm_pool_type *pt)
558 {
559         unsigned int count = 0;
560         struct page *p;
561
562         spin_lock(&pt->lock);
563         /* Only used for debugfs, the overhead doesn't matter */
564         list_for_each_entry(p, &pt->pages, lru)
565                 ++count;
566         spin_unlock(&pt->lock);
567
568         return count;
569 }
570
571 /* Print a nice header for the order */
572 static void ttm_pool_debugfs_header(struct seq_file *m)
573 {
574         unsigned int i;
575
576         seq_puts(m, "\t ");
577         for (i = 0; i < MAX_ORDER; ++i)
578                 seq_printf(m, " ---%2u---", i);
579         seq_puts(m, "\n");
580 }
581
582 /* Dump information about the different pool types */
583 static void ttm_pool_debugfs_orders(struct ttm_pool_type *pt,
584                                     struct seq_file *m)
585 {
586         unsigned int i;
587
588         for (i = 0; i < MAX_ORDER; ++i)
589                 seq_printf(m, " %8u", ttm_pool_type_count(&pt[i]));
590         seq_puts(m, "\n");
591 }
592
593 /* Dump the total amount of allocated pages */
594 static void ttm_pool_debugfs_footer(struct seq_file *m)
595 {
596         seq_printf(m, "\ntotal\t: %8lu of %8lu\n",
597                    atomic_long_read(&allocated_pages), page_pool_size);
598 }
599
600 /* Dump the information for the global pools */
601 static int ttm_pool_debugfs_globals_show(struct seq_file *m, void *data)
602 {
603         ttm_pool_debugfs_header(m);
604
605         spin_lock(&shrinker_lock);
606         seq_puts(m, "wc\t:");
607         ttm_pool_debugfs_orders(global_write_combined, m);
608         seq_puts(m, "uc\t:");
609         ttm_pool_debugfs_orders(global_uncached, m);
610         seq_puts(m, "wc 32\t:");
611         ttm_pool_debugfs_orders(global_dma32_write_combined, m);
612         seq_puts(m, "uc 32\t:");
613         ttm_pool_debugfs_orders(global_dma32_uncached, m);
614         spin_unlock(&shrinker_lock);
615
616         ttm_pool_debugfs_footer(m);
617
618         return 0;
619 }
620 DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_globals);
621
622 /**
623  * ttm_pool_debugfs - Debugfs dump function for a pool
624  *
625  * @pool: the pool to dump the information for
626  * @m: seq_file to dump to
627  *
628  * Make a debugfs dump with the per pool and global information.
629  */
630 int ttm_pool_debugfs(struct ttm_pool *pool, struct seq_file *m)
631 {
632         unsigned int i;
633
634         ttm_pool_debugfs_header(m);
635
636         spin_lock(&shrinker_lock);
637         for (i = 0; i < TTM_NUM_CACHING_TYPES; ++i) {
638                 seq_puts(m, "DMA ");
639                 switch (i) {
640                 case ttm_cached:
641                         seq_puts(m, "\t:");
642                         break;
643                 case ttm_write_combined:
644                         seq_puts(m, "wc\t:");
645                         break;
646                 case ttm_uncached:
647                         seq_puts(m, "uc\t:");
648                         break;
649                 }
650                 ttm_pool_debugfs_orders(pool->caching[i].orders, m);
651         }
652         spin_unlock(&shrinker_lock);
653
654         ttm_pool_debugfs_footer(m);
655         return 0;
656 }
657 EXPORT_SYMBOL(ttm_pool_debugfs);
658
659 /* Test the shrinker functions and dump the result */
660 static int ttm_pool_debugfs_shrink_show(struct seq_file *m, void *data)
661 {
662         struct shrink_control sc = { .gfp_mask = GFP_NOFS };
663
664         fs_reclaim_acquire(GFP_KERNEL);
665         seq_printf(m, "%lu/%lu\n", ttm_pool_shrinker_count(&mm_shrinker, &sc),
666                    ttm_pool_shrinker_scan(&mm_shrinker, &sc));
667         fs_reclaim_release(GFP_KERNEL);
668
669         return 0;
670 }
671 DEFINE_SHOW_ATTRIBUTE(ttm_pool_debugfs_shrink);
672
673 #endif
674
675 /**
676  * ttm_pool_mgr_init - Initialize globals
677  *
678  * @num_pages: default number of pages
679  *
680  * Initialize the global locks and lists for the MM shrinker.
681  */
682 int ttm_pool_mgr_init(unsigned long num_pages)
683 {
684         unsigned int i;
685
686         if (!page_pool_size)
687                 page_pool_size = num_pages;
688
689         spin_lock_init(&shrinker_lock);
690         INIT_LIST_HEAD(&shrinker_list);
691
692         for (i = 0; i < MAX_ORDER; ++i) {
693                 ttm_pool_type_init(&global_write_combined[i], NULL,
694                                    ttm_write_combined, i);
695                 ttm_pool_type_init(&global_uncached[i], NULL, ttm_uncached, i);
696
697                 ttm_pool_type_init(&global_dma32_write_combined[i], NULL,
698                                    ttm_write_combined, i);
699                 ttm_pool_type_init(&global_dma32_uncached[i], NULL,
700                                    ttm_uncached, i);
701         }
702
703 #ifdef CONFIG_DEBUG_FS
704         debugfs_create_file("page_pool", 0444, ttm_debugfs_root, NULL,
705                             &ttm_pool_debugfs_globals_fops);
706         debugfs_create_file("page_pool_shrink", 0400, ttm_debugfs_root, NULL,
707                             &ttm_pool_debugfs_shrink_fops);
708 #endif
709
710         mm_shrinker.count_objects = ttm_pool_shrinker_count;
711         mm_shrinker.scan_objects = ttm_pool_shrinker_scan;
712         mm_shrinker.seeks = 1;
713         return register_shrinker(&mm_shrinker);
714 }
715
716 /**
717  * ttm_pool_mgr_fini - Finalize globals
718  *
719  * Cleanup the global pools and unregister the MM shrinker.
720  */
721 void ttm_pool_mgr_fini(void)
722 {
723         unsigned int i;
724
725         for (i = 0; i < MAX_ORDER; ++i) {
726                 ttm_pool_type_fini(&global_write_combined[i]);
727                 ttm_pool_type_fini(&global_uncached[i]);
728
729                 ttm_pool_type_fini(&global_dma32_write_combined[i]);
730                 ttm_pool_type_fini(&global_dma32_uncached[i]);
731         }
732
733         unregister_shrinker(&mm_shrinker);
734         WARN_ON(!list_empty(&shrinker_list));
735 }