]> Git Repo - linux.git/blob - drivers/gpu/drm/ttm/ttm_page_alloc_dma.c
Merge tag 'drm-intel-next-2017-11-17-1' of git://anongit.freedesktop.org/drm/drm...
[linux.git] / drivers / gpu / drm / ttm / ttm_page_alloc_dma.c
1 /*
2  * Copyright 2011 (c) Oracle Corp.
3
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:
10  *
11  * The above copyright notice and this permission notice (including the
12  * next paragraph) shall be included in all copies or substantial portions
13  * 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 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.
22  *
23  * Author: Konrad Rzeszutek Wilk <[email protected]>
24  */
25
26 /*
27  * A simple DMA pool losely based on dmapool.c. It has certain advantages
28  * over the DMA pools:
29  * - Pool collects resently freed pages for reuse (and hooks up to
30  *   the shrinker).
31  * - Tracks currently in use pages
32  * - Tracks whether the page is UC, WB or cached (and reverts to WB
33  *   when freed).
34  */
35
36 #if defined(CONFIG_SWIOTLB) || defined(CONFIG_INTEL_IOMMU)
37 #define pr_fmt(fmt) "[TTM] " fmt
38
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>
47 #include <linux/mm.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 #if IS_ENABLED(CONFIG_AGP)
54 #include <asm/agp.h>
55 #endif
56 #ifdef CONFIG_X86
57 #include <asm/set_memory.h>
58 #endif
59
60 #define NUM_PAGES_TO_ALLOC              (PAGE_SIZE/sizeof(struct page *))
61 #define SMALL_ALLOCATION                4
62 #define FREE_ALL_PAGES                  (~0U)
63 #define VADDR_FLAG_HUGE_POOL            1UL
64
65 enum pool_type {
66         IS_UNDEFINED    = 0,
67         IS_WC           = 1 << 1,
68         IS_UC           = 1 << 2,
69         IS_CACHED       = 1 << 3,
70         IS_DMA32        = 1 << 4,
71         IS_HUGE         = 1 << 5
72 };
73
74 /*
75  * The pool structure. There are up to nine pools:
76  *  - generic (not restricted to DMA32):
77  *      - write combined, uncached, cached.
78  *  - dma32 (up to 2^32 - so up 4GB):
79  *      - write combined, uncached, cached.
80  *  - huge (not restricted to DMA32):
81  *      - write combined, uncached, cached.
82  * for each 'struct device'. The 'cached' is for pages that are actively used.
83  * The other ones can be shrunk by the shrinker API if neccessary.
84  * @pools: The 'struct device->dma_pools' link.
85  * @type: Type of the pool
86  * @lock: Protects the free_list from concurrnet access. Must be
87  * used with irqsave/irqrestore variants because pool allocator maybe called
88  * from delayed work.
89  * @free_list: Pool of pages that are free to be used. No order requirements.
90  * @dev: The device that is associated with these pools.
91  * @size: Size used during DMA allocation.
92  * @npages_free: Count of available pages for re-use.
93  * @npages_in_use: Count of pages that are in use.
94  * @nfrees: Stats when pool is shrinking.
95  * @nrefills: Stats when the pool is grown.
96  * @gfp_flags: Flags to pass for alloc_page.
97  * @name: Name of the pool.
98  * @dev_name: Name derieved from dev - similar to how dev_info works.
99  *   Used during shutdown as the dev_info during release is unavailable.
100  */
101 struct dma_pool {
102         struct list_head pools; /* The 'struct device->dma_pools link */
103         enum pool_type type;
104         spinlock_t lock;
105         struct list_head free_list;
106         struct device *dev;
107         unsigned size;
108         unsigned npages_free;
109         unsigned npages_in_use;
110         unsigned long nfrees; /* Stats when shrunk. */
111         unsigned long nrefills; /* Stats when grown. */
112         gfp_t gfp_flags;
113         char name[13]; /* "cached dma32" */
114         char dev_name[64]; /* Constructed from dev */
115 };
116
117 /*
118  * The accounting page keeping track of the allocated page along with
119  * the DMA address.
120  * @page_list: The link to the 'page_list' in 'struct dma_pool'.
121  * @vaddr: The virtual address of the page and a flag if the page belongs to a
122  * huge pool
123  * @dma: The bus address of the page. If the page is not allocated
124  *   via the DMA API, it will be -1.
125  */
126 struct dma_page {
127         struct list_head page_list;
128         unsigned long vaddr;
129         struct page *p;
130         dma_addr_t dma;
131 };
132
133 /*
134  * Limits for the pool. They are handled without locks because only place where
135  * they may change is in sysfs store. They won't have immediate effect anyway
136  * so forcing serialization to access them is pointless.
137  */
138
139 struct ttm_pool_opts {
140         unsigned        alloc_size;
141         unsigned        max_size;
142         unsigned        small;
143 };
144
145 /*
146  * Contains the list of all of the 'struct device' and their corresponding
147  * DMA pools. Guarded by _mutex->lock.
148  * @pools: The link to 'struct ttm_pool_manager->pools'
149  * @dev: The 'struct device' associated with the 'pool'
150  * @pool: The 'struct dma_pool' associated with the 'dev'
151  */
152 struct device_pools {
153         struct list_head pools;
154         struct device *dev;
155         struct dma_pool *pool;
156 };
157
158 /*
159  * struct ttm_pool_manager - Holds memory pools for fast allocation
160  *
161  * @lock: Lock used when adding/removing from pools
162  * @pools: List of 'struct device' and 'struct dma_pool' tuples.
163  * @options: Limits for the pool.
164  * @npools: Total amount of pools in existence.
165  * @shrinker: The structure used by [un|]register_shrinker
166  */
167 struct ttm_pool_manager {
168         struct mutex            lock;
169         struct list_head        pools;
170         struct ttm_pool_opts    options;
171         unsigned                npools;
172         struct shrinker         mm_shrink;
173         struct kobject          kobj;
174 };
175
176 static struct ttm_pool_manager *_manager;
177
178 static struct attribute ttm_page_pool_max = {
179         .name = "pool_max_size",
180         .mode = S_IRUGO | S_IWUSR
181 };
182 static struct attribute ttm_page_pool_small = {
183         .name = "pool_small_allocation",
184         .mode = S_IRUGO | S_IWUSR
185 };
186 static struct attribute ttm_page_pool_alloc_size = {
187         .name = "pool_allocation_size",
188         .mode = S_IRUGO | S_IWUSR
189 };
190
191 static struct attribute *ttm_pool_attrs[] = {
192         &ttm_page_pool_max,
193         &ttm_page_pool_small,
194         &ttm_page_pool_alloc_size,
195         NULL
196 };
197
198 static void ttm_pool_kobj_release(struct kobject *kobj)
199 {
200         struct ttm_pool_manager *m =
201                 container_of(kobj, struct ttm_pool_manager, kobj);
202         kfree(m);
203 }
204
205 static ssize_t ttm_pool_store(struct kobject *kobj, struct attribute *attr,
206                               const char *buffer, size_t size)
207 {
208         struct ttm_pool_manager *m =
209                 container_of(kobj, struct ttm_pool_manager, kobj);
210         int chars;
211         unsigned val;
212         chars = sscanf(buffer, "%u", &val);
213         if (chars == 0)
214                 return size;
215
216         /* Convert kb to number of pages */
217         val = val / (PAGE_SIZE >> 10);
218
219         if (attr == &ttm_page_pool_max)
220                 m->options.max_size = val;
221         else if (attr == &ttm_page_pool_small)
222                 m->options.small = val;
223         else if (attr == &ttm_page_pool_alloc_size) {
224                 if (val > NUM_PAGES_TO_ALLOC*8) {
225                         pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
226                                NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
227                                NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
228                         return size;
229                 } else if (val > NUM_PAGES_TO_ALLOC) {
230                         pr_warn("Setting allocation size to larger than %lu is not recommended\n",
231                                 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
232                 }
233                 m->options.alloc_size = val;
234         }
235
236         return size;
237 }
238
239 static ssize_t ttm_pool_show(struct kobject *kobj, struct attribute *attr,
240                              char *buffer)
241 {
242         struct ttm_pool_manager *m =
243                 container_of(kobj, struct ttm_pool_manager, kobj);
244         unsigned val = 0;
245
246         if (attr == &ttm_page_pool_max)
247                 val = m->options.max_size;
248         else if (attr == &ttm_page_pool_small)
249                 val = m->options.small;
250         else if (attr == &ttm_page_pool_alloc_size)
251                 val = m->options.alloc_size;
252
253         val = val * (PAGE_SIZE >> 10);
254
255         return snprintf(buffer, PAGE_SIZE, "%u\n", val);
256 }
257
258 static const struct sysfs_ops ttm_pool_sysfs_ops = {
259         .show = &ttm_pool_show,
260         .store = &ttm_pool_store,
261 };
262
263 static struct kobj_type ttm_pool_kobj_type = {
264         .release = &ttm_pool_kobj_release,
265         .sysfs_ops = &ttm_pool_sysfs_ops,
266         .default_attrs = ttm_pool_attrs,
267 };
268
269 #ifndef CONFIG_X86
270 static int set_pages_array_wb(struct page **pages, int addrinarray)
271 {
272 #if IS_ENABLED(CONFIG_AGP)
273         int i;
274
275         for (i = 0; i < addrinarray; i++)
276                 unmap_page_from_agp(pages[i]);
277 #endif
278         return 0;
279 }
280
281 static int set_pages_array_wc(struct page **pages, int addrinarray)
282 {
283 #if IS_ENABLED(CONFIG_AGP)
284         int i;
285
286         for (i = 0; i < addrinarray; i++)
287                 map_page_into_agp(pages[i]);
288 #endif
289         return 0;
290 }
291
292 static int set_pages_array_uc(struct page **pages, int addrinarray)
293 {
294 #if IS_ENABLED(CONFIG_AGP)
295         int i;
296
297         for (i = 0; i < addrinarray; i++)
298                 map_page_into_agp(pages[i]);
299 #endif
300         return 0;
301 }
302 #endif /* for !CONFIG_X86 */
303
304 static int ttm_set_pages_caching(struct dma_pool *pool,
305                                  struct page **pages, unsigned cpages)
306 {
307         int r = 0;
308         /* Set page caching */
309         if (pool->type & IS_UC) {
310                 r = set_pages_array_uc(pages, cpages);
311                 if (r)
312                         pr_err("%s: Failed to set %d pages to uc!\n",
313                                pool->dev_name, cpages);
314         }
315         if (pool->type & IS_WC) {
316                 r = set_pages_array_wc(pages, cpages);
317                 if (r)
318                         pr_err("%s: Failed to set %d pages to wc!\n",
319                                pool->dev_name, cpages);
320         }
321         return r;
322 }
323
324 static void __ttm_dma_free_page(struct dma_pool *pool, struct dma_page *d_page)
325 {
326         dma_addr_t dma = d_page->dma;
327         d_page->vaddr &= ~VADDR_FLAG_HUGE_POOL;
328         dma_free_coherent(pool->dev, pool->size, (void *)d_page->vaddr, dma);
329
330         kfree(d_page);
331         d_page = NULL;
332 }
333 static struct dma_page *__ttm_dma_alloc_page(struct dma_pool *pool)
334 {
335         struct dma_page *d_page;
336         void *vaddr;
337
338         d_page = kmalloc(sizeof(struct dma_page), GFP_KERNEL);
339         if (!d_page)
340                 return NULL;
341
342         vaddr = dma_alloc_coherent(pool->dev, pool->size, &d_page->dma,
343                                    pool->gfp_flags);
344         if (vaddr) {
345                 if (is_vmalloc_addr(vaddr))
346                         d_page->p = vmalloc_to_page(vaddr);
347                 else
348                         d_page->p = virt_to_page(vaddr);
349                 d_page->vaddr = (unsigned long)vaddr;
350                 if (pool->type & IS_HUGE)
351                         d_page->vaddr |= VADDR_FLAG_HUGE_POOL;
352         } else {
353                 kfree(d_page);
354                 d_page = NULL;
355         }
356         return d_page;
357 }
358 static enum pool_type ttm_to_type(int flags, enum ttm_caching_state cstate)
359 {
360         enum pool_type type = IS_UNDEFINED;
361
362         if (flags & TTM_PAGE_FLAG_DMA32)
363                 type |= IS_DMA32;
364         if (cstate == tt_cached)
365                 type |= IS_CACHED;
366         else if (cstate == tt_uncached)
367                 type |= IS_UC;
368         else
369                 type |= IS_WC;
370
371         return type;
372 }
373
374 static void ttm_pool_update_free_locked(struct dma_pool *pool,
375                                         unsigned freed_pages)
376 {
377         pool->npages_free -= freed_pages;
378         pool->nfrees += freed_pages;
379
380 }
381
382 /* set memory back to wb and free the pages. */
383 static void ttm_dma_page_put(struct dma_pool *pool, struct dma_page *d_page)
384 {
385         struct page *page = d_page->p;
386         unsigned i, num_pages;
387         int ret;
388
389         /* Don't set WB on WB page pool. */
390         if (!(pool->type & IS_CACHED)) {
391                 num_pages = pool->size / PAGE_SIZE;
392                 for (i = 0; i < num_pages; ++i, ++page) {
393                         ret = set_pages_array_wb(&page, 1);
394                         if (ret) {
395                                 pr_err("%s: Failed to set %d pages to wb!\n",
396                                        pool->dev_name, 1);
397                         }
398                 }
399         }
400
401         list_del(&d_page->page_list);
402         __ttm_dma_free_page(pool, d_page);
403 }
404
405 static void ttm_dma_pages_put(struct dma_pool *pool, struct list_head *d_pages,
406                               struct page *pages[], unsigned npages)
407 {
408         struct dma_page *d_page, *tmp;
409
410         if (pool->type & IS_HUGE) {
411                 list_for_each_entry_safe(d_page, tmp, d_pages, page_list)
412                         ttm_dma_page_put(pool, d_page);
413
414                 return;
415         }
416
417         /* Don't set WB on WB page pool. */
418         if (npages && !(pool->type & IS_CACHED) &&
419             set_pages_array_wb(pages, npages))
420                 pr_err("%s: Failed to set %d pages to wb!\n",
421                        pool->dev_name, npages);
422
423         list_for_each_entry_safe(d_page, tmp, d_pages, page_list) {
424                 list_del(&d_page->page_list);
425                 __ttm_dma_free_page(pool, d_page);
426         }
427 }
428
429 /*
430  * Free pages from pool.
431  *
432  * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
433  * number of pages in one go.
434  *
435  * @pool: to free the pages from
436  * @nr_free: If set to true will free all pages in pool
437  * @use_static: Safe to use static buffer
438  **/
439 static unsigned ttm_dma_page_pool_free(struct dma_pool *pool, unsigned nr_free,
440                                        bool use_static)
441 {
442         static struct page *static_buf[NUM_PAGES_TO_ALLOC];
443         unsigned long irq_flags;
444         struct dma_page *dma_p, *tmp;
445         struct page **pages_to_free;
446         struct list_head d_pages;
447         unsigned freed_pages = 0,
448                  npages_to_free = nr_free;
449
450         if (NUM_PAGES_TO_ALLOC < nr_free)
451                 npages_to_free = NUM_PAGES_TO_ALLOC;
452 #if 0
453         if (nr_free > 1) {
454                 pr_debug("%s: (%s:%d) Attempting to free %d (%d) pages\n",
455                          pool->dev_name, pool->name, current->pid,
456                          npages_to_free, nr_free);
457         }
458 #endif
459         if (use_static)
460                 pages_to_free = static_buf;
461         else
462                 pages_to_free = kmalloc(npages_to_free * sizeof(struct page *),
463                                         GFP_KERNEL);
464
465         if (!pages_to_free) {
466                 pr_debug("%s: Failed to allocate memory for pool free operation\n",
467                        pool->dev_name);
468                 return 0;
469         }
470         INIT_LIST_HEAD(&d_pages);
471 restart:
472         spin_lock_irqsave(&pool->lock, irq_flags);
473
474         /* We picking the oldest ones off the list */
475         list_for_each_entry_safe_reverse(dma_p, tmp, &pool->free_list,
476                                          page_list) {
477                 if (freed_pages >= npages_to_free)
478                         break;
479
480                 /* Move the dma_page from one list to another. */
481                 list_move(&dma_p->page_list, &d_pages);
482
483                 pages_to_free[freed_pages++] = dma_p->p;
484                 /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
485                 if (freed_pages >= NUM_PAGES_TO_ALLOC) {
486
487                         ttm_pool_update_free_locked(pool, freed_pages);
488                         /**
489                          * Because changing page caching is costly
490                          * we unlock the pool to prevent stalling.
491                          */
492                         spin_unlock_irqrestore(&pool->lock, irq_flags);
493
494                         ttm_dma_pages_put(pool, &d_pages, pages_to_free,
495                                           freed_pages);
496
497                         INIT_LIST_HEAD(&d_pages);
498
499                         if (likely(nr_free != FREE_ALL_PAGES))
500                                 nr_free -= freed_pages;
501
502                         if (NUM_PAGES_TO_ALLOC >= nr_free)
503                                 npages_to_free = nr_free;
504                         else
505                                 npages_to_free = NUM_PAGES_TO_ALLOC;
506
507                         freed_pages = 0;
508
509                         /* free all so restart the processing */
510                         if (nr_free)
511                                 goto restart;
512
513                         /* Not allowed to fall through or break because
514                          * following context is inside spinlock while we are
515                          * outside here.
516                          */
517                         goto out;
518
519                 }
520         }
521
522         /* remove range of pages from the pool */
523         if (freed_pages) {
524                 ttm_pool_update_free_locked(pool, freed_pages);
525                 nr_free -= freed_pages;
526         }
527
528         spin_unlock_irqrestore(&pool->lock, irq_flags);
529
530         if (freed_pages)
531                 ttm_dma_pages_put(pool, &d_pages, pages_to_free, freed_pages);
532 out:
533         if (pages_to_free != static_buf)
534                 kfree(pages_to_free);
535         return nr_free;
536 }
537
538 static void ttm_dma_free_pool(struct device *dev, enum pool_type type)
539 {
540         struct device_pools *p;
541         struct dma_pool *pool;
542
543         if (!dev)
544                 return;
545
546         mutex_lock(&_manager->lock);
547         list_for_each_entry_reverse(p, &_manager->pools, pools) {
548                 if (p->dev != dev)
549                         continue;
550                 pool = p->pool;
551                 if (pool->type != type)
552                         continue;
553
554                 list_del(&p->pools);
555                 kfree(p);
556                 _manager->npools--;
557                 break;
558         }
559         list_for_each_entry_reverse(pool, &dev->dma_pools, pools) {
560                 if (pool->type != type)
561                         continue;
562                 /* Takes a spinlock.. */
563                 /* OK to use static buffer since global mutex is held. */
564                 ttm_dma_page_pool_free(pool, FREE_ALL_PAGES, true);
565                 WARN_ON(((pool->npages_in_use + pool->npages_free) != 0));
566                 /* This code path is called after _all_ references to the
567                  * struct device has been dropped - so nobody should be
568                  * touching it. In case somebody is trying to _add_ we are
569                  * guarded by the mutex. */
570                 list_del(&pool->pools);
571                 kfree(pool);
572                 break;
573         }
574         mutex_unlock(&_manager->lock);
575 }
576
577 /*
578  * On free-ing of the 'struct device' this deconstructor is run.
579  * Albeit the pool might have already been freed earlier.
580  */
581 static void ttm_dma_pool_release(struct device *dev, void *res)
582 {
583         struct dma_pool *pool = *(struct dma_pool **)res;
584
585         if (pool)
586                 ttm_dma_free_pool(dev, pool->type);
587 }
588
589 static int ttm_dma_pool_match(struct device *dev, void *res, void *match_data)
590 {
591         return *(struct dma_pool **)res == match_data;
592 }
593
594 static struct dma_pool *ttm_dma_pool_init(struct device *dev, gfp_t flags,
595                                           enum pool_type type)
596 {
597         const char *n[] = {"wc", "uc", "cached", " dma32", "huge"};
598         enum pool_type t[] = {IS_WC, IS_UC, IS_CACHED, IS_DMA32, IS_HUGE};
599         struct device_pools *sec_pool = NULL;
600         struct dma_pool *pool = NULL, **ptr;
601         unsigned i;
602         int ret = -ENODEV;
603         char *p;
604
605         if (!dev)
606                 return NULL;
607
608         ptr = devres_alloc(ttm_dma_pool_release, sizeof(*ptr), GFP_KERNEL);
609         if (!ptr)
610                 return NULL;
611
612         ret = -ENOMEM;
613
614         pool = kmalloc_node(sizeof(struct dma_pool), GFP_KERNEL,
615                             dev_to_node(dev));
616         if (!pool)
617                 goto err_mem;
618
619         sec_pool = kmalloc_node(sizeof(struct device_pools), GFP_KERNEL,
620                                 dev_to_node(dev));
621         if (!sec_pool)
622                 goto err_mem;
623
624         INIT_LIST_HEAD(&sec_pool->pools);
625         sec_pool->dev = dev;
626         sec_pool->pool =  pool;
627
628         INIT_LIST_HEAD(&pool->free_list);
629         INIT_LIST_HEAD(&pool->pools);
630         spin_lock_init(&pool->lock);
631         pool->dev = dev;
632         pool->npages_free = pool->npages_in_use = 0;
633         pool->nfrees = 0;
634         pool->gfp_flags = flags;
635         if (type & IS_HUGE)
636 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
637                 pool->size = HPAGE_PMD_SIZE;
638 #else
639                 BUG();
640 #endif
641         else
642                 pool->size = PAGE_SIZE;
643         pool->type = type;
644         pool->nrefills = 0;
645         p = pool->name;
646         for (i = 0; i < ARRAY_SIZE(t); i++) {
647                 if (type & t[i]) {
648                         p += snprintf(p, sizeof(pool->name) - (p - pool->name),
649                                       "%s", n[i]);
650                 }
651         }
652         *p = 0;
653         /* We copy the name for pr_ calls b/c when dma_pool_destroy is called
654          * - the kobj->name has already been deallocated.*/
655         snprintf(pool->dev_name, sizeof(pool->dev_name), "%s %s",
656                  dev_driver_string(dev), dev_name(dev));
657         mutex_lock(&_manager->lock);
658         /* You can get the dma_pool from either the global: */
659         list_add(&sec_pool->pools, &_manager->pools);
660         _manager->npools++;
661         /* or from 'struct device': */
662         list_add(&pool->pools, &dev->dma_pools);
663         mutex_unlock(&_manager->lock);
664
665         *ptr = pool;
666         devres_add(dev, ptr);
667
668         return pool;
669 err_mem:
670         devres_free(ptr);
671         kfree(sec_pool);
672         kfree(pool);
673         return ERR_PTR(ret);
674 }
675
676 static struct dma_pool *ttm_dma_find_pool(struct device *dev,
677                                           enum pool_type type)
678 {
679         struct dma_pool *pool, *tmp, *found = NULL;
680
681         if (type == IS_UNDEFINED)
682                 return found;
683
684         /* NB: We iterate on the 'struct dev' which has no spinlock, but
685          * it does have a kref which we have taken. The kref is taken during
686          * graphic driver loading - in the drm_pci_init it calls either
687          * pci_dev_get or pci_register_driver which both end up taking a kref
688          * on 'struct device'.
689          *
690          * On teardown, the graphic drivers end up quiescing the TTM (put_pages)
691          * and calls the dev_res deconstructors: ttm_dma_pool_release. The nice
692          * thing is at that point of time there are no pages associated with the
693          * driver so this function will not be called.
694          */
695         list_for_each_entry_safe(pool, tmp, &dev->dma_pools, pools) {
696                 if (pool->type != type)
697                         continue;
698                 found = pool;
699                 break;
700         }
701         return found;
702 }
703
704 /*
705  * Free pages the pages that failed to change the caching state. If there
706  * are pages that have changed their caching state already put them to the
707  * pool.
708  */
709 static void ttm_dma_handle_caching_state_failure(struct dma_pool *pool,
710                                                  struct list_head *d_pages,
711                                                  struct page **failed_pages,
712                                                  unsigned cpages)
713 {
714         struct dma_page *d_page, *tmp;
715         struct page *p;
716         unsigned i = 0;
717
718         p = failed_pages[0];
719         if (!p)
720                 return;
721         /* Find the failed page. */
722         list_for_each_entry_safe(d_page, tmp, d_pages, page_list) {
723                 if (d_page->p != p)
724                         continue;
725                 /* .. and then progress over the full list. */
726                 list_del(&d_page->page_list);
727                 __ttm_dma_free_page(pool, d_page);
728                 if (++i < cpages)
729                         p = failed_pages[i];
730                 else
731                         break;
732         }
733
734 }
735
736 /*
737  * Allocate 'count' pages, and put 'need' number of them on the
738  * 'pages' and as well on the 'dma_address' starting at 'dma_offset' offset.
739  * The full list of pages should also be on 'd_pages'.
740  * We return zero for success, and negative numbers as errors.
741  */
742 static int ttm_dma_pool_alloc_new_pages(struct dma_pool *pool,
743                                         struct list_head *d_pages,
744                                         unsigned count)
745 {
746         struct page **caching_array;
747         struct dma_page *dma_p;
748         struct page *p;
749         int r = 0;
750         unsigned i, j, npages, cpages;
751         unsigned max_cpages = min(count,
752                         (unsigned)(PAGE_SIZE/sizeof(struct page *)));
753
754         /* allocate array for page caching change */
755         caching_array = kmalloc(max_cpages*sizeof(struct page *), GFP_KERNEL);
756
757         if (!caching_array) {
758                 pr_debug("%s: Unable to allocate table for new pages\n",
759                        pool->dev_name);
760                 return -ENOMEM;
761         }
762
763         if (count > 1) {
764                 pr_debug("%s: (%s:%d) Getting %d pages\n",
765                          pool->dev_name, pool->name, current->pid, count);
766         }
767
768         for (i = 0, cpages = 0; i < count; ++i) {
769                 dma_p = __ttm_dma_alloc_page(pool);
770                 if (!dma_p) {
771                         pr_debug("%s: Unable to get page %u\n",
772                                  pool->dev_name, i);
773
774                         /* store already allocated pages in the pool after
775                          * setting the caching state */
776                         if (cpages) {
777                                 r = ttm_set_pages_caching(pool, caching_array,
778                                                           cpages);
779                                 if (r)
780                                         ttm_dma_handle_caching_state_failure(
781                                                 pool, d_pages, caching_array,
782                                                 cpages);
783                         }
784                         r = -ENOMEM;
785                         goto out;
786                 }
787                 p = dma_p->p;
788                 list_add(&dma_p->page_list, d_pages);
789
790 #ifdef CONFIG_HIGHMEM
791                 /* gfp flags of highmem page should never be dma32 so we
792                  * we should be fine in such case
793                  */
794                 if (PageHighMem(p))
795                         continue;
796 #endif
797
798                 npages = pool->size / PAGE_SIZE;
799                 for (j = 0; j < npages; ++j) {
800                         caching_array[cpages++] = p + j;
801                         if (cpages == max_cpages) {
802                                 /* Note: Cannot hold the spinlock */
803                                 r = ttm_set_pages_caching(pool, caching_array,
804                                                           cpages);
805                                 if (r) {
806                                         ttm_dma_handle_caching_state_failure(
807                                              pool, d_pages, caching_array,
808                                              cpages);
809                                         goto out;
810                                 }
811                                 cpages = 0;
812                         }
813                 }
814         }
815
816         if (cpages) {
817                 r = ttm_set_pages_caching(pool, caching_array, cpages);
818                 if (r)
819                         ttm_dma_handle_caching_state_failure(pool, d_pages,
820                                         caching_array, cpages);
821         }
822 out:
823         kfree(caching_array);
824         return r;
825 }
826
827 /*
828  * @return count of pages still required to fulfill the request.
829  */
830 static int ttm_dma_page_pool_fill_locked(struct dma_pool *pool,
831                                          unsigned long *irq_flags)
832 {
833         unsigned count = _manager->options.small;
834         int r = pool->npages_free;
835
836         if (count > pool->npages_free) {
837                 struct list_head d_pages;
838
839                 INIT_LIST_HEAD(&d_pages);
840
841                 spin_unlock_irqrestore(&pool->lock, *irq_flags);
842
843                 /* Returns how many more are neccessary to fulfill the
844                  * request. */
845                 r = ttm_dma_pool_alloc_new_pages(pool, &d_pages, count);
846
847                 spin_lock_irqsave(&pool->lock, *irq_flags);
848                 if (!r) {
849                         /* Add the fresh to the end.. */
850                         list_splice(&d_pages, &pool->free_list);
851                         ++pool->nrefills;
852                         pool->npages_free += count;
853                         r = count;
854                 } else {
855                         struct dma_page *d_page;
856                         unsigned cpages = 0;
857
858                         pr_debug("%s: Failed to fill %s pool (r:%d)!\n",
859                                  pool->dev_name, pool->name, r);
860
861                         list_for_each_entry(d_page, &d_pages, page_list) {
862                                 cpages++;
863                         }
864                         list_splice_tail(&d_pages, &pool->free_list);
865                         pool->npages_free += cpages;
866                         r = cpages;
867                 }
868         }
869         return r;
870 }
871
872 /*
873  * @return count of pages still required to fulfill the request.
874  * The populate list is actually a stack (not that is matters as TTM
875  * allocates one page at a time.
876  */
877 static int ttm_dma_pool_get_pages(struct dma_pool *pool,
878                                   struct ttm_dma_tt *ttm_dma,
879                                   unsigned index)
880 {
881         struct dma_page *d_page;
882         struct ttm_tt *ttm = &ttm_dma->ttm;
883         unsigned long irq_flags;
884         int count, r = -ENOMEM;
885
886         spin_lock_irqsave(&pool->lock, irq_flags);
887         count = ttm_dma_page_pool_fill_locked(pool, &irq_flags);
888         if (count) {
889                 d_page = list_first_entry(&pool->free_list, struct dma_page, page_list);
890                 ttm->pages[index] = d_page->p;
891                 ttm_dma->dma_address[index] = d_page->dma;
892                 list_move_tail(&d_page->page_list, &ttm_dma->pages_list);
893                 r = 0;
894                 pool->npages_in_use += 1;
895                 pool->npages_free -= 1;
896         }
897         spin_unlock_irqrestore(&pool->lock, irq_flags);
898         return r;
899 }
900
901 static gfp_t ttm_dma_pool_gfp_flags(struct ttm_dma_tt *ttm_dma, bool huge)
902 {
903         struct ttm_tt *ttm = &ttm_dma->ttm;
904         gfp_t gfp_flags;
905
906         if (ttm->page_flags & TTM_PAGE_FLAG_DMA32)
907                 gfp_flags = GFP_USER | GFP_DMA32;
908         else
909                 gfp_flags = GFP_HIGHUSER;
910         if (ttm->page_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
911                 gfp_flags |= __GFP_ZERO;
912
913         if (huge) {
914                 gfp_flags |= GFP_TRANSHUGE;
915                 gfp_flags &= ~__GFP_MOVABLE;
916                 gfp_flags &= ~__GFP_COMP;
917         }
918
919         return gfp_flags;
920 }
921
922 /*
923  * On success pages list will hold count number of correctly
924  * cached pages. On failure will hold the negative return value (-ENOMEM, etc).
925  */
926 int ttm_dma_populate(struct ttm_dma_tt *ttm_dma, struct device *dev)
927 {
928         struct ttm_tt *ttm = &ttm_dma->ttm;
929         struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
930         unsigned long num_pages = ttm->num_pages;
931         struct dma_pool *pool;
932         enum pool_type type;
933         unsigned i;
934         int ret;
935
936         if (ttm->state != tt_unpopulated)
937                 return 0;
938
939         INIT_LIST_HEAD(&ttm_dma->pages_list);
940         i = 0;
941
942         type = ttm_to_type(ttm->page_flags, ttm->caching_state);
943
944 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
945         if (ttm->page_flags & TTM_PAGE_FLAG_DMA32)
946                 goto skip_huge;
947
948         pool = ttm_dma_find_pool(dev, type | IS_HUGE);
949         if (!pool) {
950                 gfp_t gfp_flags = ttm_dma_pool_gfp_flags(ttm_dma, true);
951
952                 pool = ttm_dma_pool_init(dev, gfp_flags, type | IS_HUGE);
953                 if (IS_ERR_OR_NULL(pool))
954                         goto skip_huge;
955         }
956
957         while (num_pages >= HPAGE_PMD_NR) {
958                 unsigned j;
959
960                 ret = ttm_dma_pool_get_pages(pool, ttm_dma, i);
961                 if (ret != 0)
962                         break;
963
964                 ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
965                                                 pool->size);
966                 if (unlikely(ret != 0)) {
967                         ttm_dma_unpopulate(ttm_dma, dev);
968                         return -ENOMEM;
969                 }
970
971                 for (j = i + 1; j < (i + HPAGE_PMD_NR); ++j) {
972                         ttm->pages[j] = ttm->pages[j - 1] + 1;
973                         ttm_dma->dma_address[j] = ttm_dma->dma_address[j - 1] +
974                                 PAGE_SIZE;
975                 }
976
977                 i += HPAGE_PMD_NR;
978                 num_pages -= HPAGE_PMD_NR;
979         }
980
981 skip_huge:
982 #endif
983
984         pool = ttm_dma_find_pool(dev, type);
985         if (!pool) {
986                 gfp_t gfp_flags = ttm_dma_pool_gfp_flags(ttm_dma, false);
987
988                 pool = ttm_dma_pool_init(dev, gfp_flags, type);
989                 if (IS_ERR_OR_NULL(pool))
990                         return -ENOMEM;
991         }
992
993         while (num_pages) {
994                 ret = ttm_dma_pool_get_pages(pool, ttm_dma, i);
995                 if (ret != 0) {
996                         ttm_dma_unpopulate(ttm_dma, dev);
997                         return -ENOMEM;
998                 }
999
1000                 ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
1001                                                 pool->size);
1002                 if (unlikely(ret != 0)) {
1003                         ttm_dma_unpopulate(ttm_dma, dev);
1004                         return -ENOMEM;
1005                 }
1006
1007                 ++i;
1008                 --num_pages;
1009         }
1010
1011         if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
1012                 ret = ttm_tt_swapin(ttm);
1013                 if (unlikely(ret != 0)) {
1014                         ttm_dma_unpopulate(ttm_dma, dev);
1015                         return ret;
1016                 }
1017         }
1018
1019         ttm->state = tt_unbound;
1020         return 0;
1021 }
1022 EXPORT_SYMBOL_GPL(ttm_dma_populate);
1023
1024 /* Put all pages in pages list to correct pool to wait for reuse */
1025 void ttm_dma_unpopulate(struct ttm_dma_tt *ttm_dma, struct device *dev)
1026 {
1027         struct ttm_tt *ttm = &ttm_dma->ttm;
1028         struct dma_pool *pool;
1029         struct dma_page *d_page, *next;
1030         enum pool_type type;
1031         bool is_cached = false;
1032         unsigned count, i, npages = 0;
1033         unsigned long irq_flags;
1034
1035         type = ttm_to_type(ttm->page_flags, ttm->caching_state);
1036
1037 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
1038         pool = ttm_dma_find_pool(dev, type | IS_HUGE);
1039         if (pool) {
1040                 count = 0;
1041                 list_for_each_entry_safe(d_page, next, &ttm_dma->pages_list,
1042                                          page_list) {
1043                         if (!(d_page->vaddr & VADDR_FLAG_HUGE_POOL))
1044                                 continue;
1045
1046                         count++;
1047                         ttm_mem_global_free_page(ttm->glob->mem_glob,
1048                                                  d_page->p, pool->size);
1049                         ttm_dma_page_put(pool, d_page);
1050                 }
1051
1052                 spin_lock_irqsave(&pool->lock, irq_flags);
1053                 pool->npages_in_use -= count;
1054                 pool->nfrees += count;
1055                 spin_unlock_irqrestore(&pool->lock, irq_flags);
1056         }
1057 #endif
1058
1059         pool = ttm_dma_find_pool(dev, type);
1060         if (!pool)
1061                 return;
1062
1063         is_cached = (ttm_dma_find_pool(pool->dev,
1064                      ttm_to_type(ttm->page_flags, tt_cached)) == pool);
1065
1066         /* make sure pages array match list and count number of pages */
1067         count = 0;
1068         list_for_each_entry(d_page, &ttm_dma->pages_list, page_list) {
1069                 ttm->pages[count] = d_page->p;
1070                 count++;
1071         }
1072
1073         spin_lock_irqsave(&pool->lock, irq_flags);
1074         pool->npages_in_use -= count;
1075         if (is_cached) {
1076                 pool->nfrees += count;
1077         } else {
1078                 pool->npages_free += count;
1079                 list_splice(&ttm_dma->pages_list, &pool->free_list);
1080                 /*
1081                  * Wait to have at at least NUM_PAGES_TO_ALLOC number of pages
1082                  * to free in order to minimize calls to set_memory_wb().
1083                  */
1084                 if (pool->npages_free >= (_manager->options.max_size +
1085                                           NUM_PAGES_TO_ALLOC))
1086                         npages = pool->npages_free - _manager->options.max_size;
1087         }
1088         spin_unlock_irqrestore(&pool->lock, irq_flags);
1089
1090         if (is_cached) {
1091                 list_for_each_entry_safe(d_page, next, &ttm_dma->pages_list, page_list) {
1092                         ttm_mem_global_free_page(ttm->glob->mem_glob,
1093                                                  d_page->p, pool->size);
1094                         ttm_dma_page_put(pool, d_page);
1095                 }
1096         } else {
1097                 for (i = 0; i < count; i++) {
1098                         ttm_mem_global_free_page(ttm->glob->mem_glob,
1099                                                  ttm->pages[i], pool->size);
1100                 }
1101         }
1102
1103         INIT_LIST_HEAD(&ttm_dma->pages_list);
1104         for (i = 0; i < ttm->num_pages; i++) {
1105                 ttm->pages[i] = NULL;
1106                 ttm_dma->dma_address[i] = 0;
1107         }
1108
1109         /* shrink pool if necessary (only on !is_cached pools)*/
1110         if (npages)
1111                 ttm_dma_page_pool_free(pool, npages, false);
1112         ttm->state = tt_unpopulated;
1113 }
1114 EXPORT_SYMBOL_GPL(ttm_dma_unpopulate);
1115
1116 /**
1117  * Callback for mm to request pool to reduce number of page held.
1118  *
1119  * XXX: (dchinner) Deadlock warning!
1120  *
1121  * I'm getting sadder as I hear more pathetical whimpers about needing per-pool
1122  * shrinkers
1123  */
1124 static unsigned long
1125 ttm_dma_pool_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
1126 {
1127         static unsigned start_pool;
1128         unsigned idx = 0;
1129         unsigned pool_offset;
1130         unsigned shrink_pages = sc->nr_to_scan;
1131         struct device_pools *p;
1132         unsigned long freed = 0;
1133
1134         if (list_empty(&_manager->pools))
1135                 return SHRINK_STOP;
1136
1137         if (!mutex_trylock(&_manager->lock))
1138                 return SHRINK_STOP;
1139         if (!_manager->npools)
1140                 goto out;
1141         pool_offset = ++start_pool % _manager->npools;
1142         list_for_each_entry(p, &_manager->pools, pools) {
1143                 unsigned nr_free;
1144
1145                 if (!p->dev)
1146                         continue;
1147                 if (shrink_pages == 0)
1148                         break;
1149                 /* Do it in round-robin fashion. */
1150                 if (++idx < pool_offset)
1151                         continue;
1152                 nr_free = shrink_pages;
1153                 /* OK to use static buffer since global mutex is held. */
1154                 shrink_pages = ttm_dma_page_pool_free(p->pool, nr_free, true);
1155                 freed += nr_free - shrink_pages;
1156
1157                 pr_debug("%s: (%s:%d) Asked to shrink %d, have %d more to go\n",
1158                          p->pool->dev_name, p->pool->name, current->pid,
1159                          nr_free, shrink_pages);
1160         }
1161 out:
1162         mutex_unlock(&_manager->lock);
1163         return freed;
1164 }
1165
1166 static unsigned long
1167 ttm_dma_pool_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
1168 {
1169         struct device_pools *p;
1170         unsigned long count = 0;
1171
1172         if (!mutex_trylock(&_manager->lock))
1173                 return 0;
1174         list_for_each_entry(p, &_manager->pools, pools)
1175                 count += p->pool->npages_free;
1176         mutex_unlock(&_manager->lock);
1177         return count;
1178 }
1179
1180 static void ttm_dma_pool_mm_shrink_init(struct ttm_pool_manager *manager)
1181 {
1182         manager->mm_shrink.count_objects = ttm_dma_pool_shrink_count;
1183         manager->mm_shrink.scan_objects = &ttm_dma_pool_shrink_scan;
1184         manager->mm_shrink.seeks = 1;
1185         register_shrinker(&manager->mm_shrink);
1186 }
1187
1188 static void ttm_dma_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
1189 {
1190         unregister_shrinker(&manager->mm_shrink);
1191 }
1192
1193 int ttm_dma_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
1194 {
1195         int ret = -ENOMEM;
1196
1197         WARN_ON(_manager);
1198
1199         pr_info("Initializing DMA pool allocator\n");
1200
1201         _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
1202         if (!_manager)
1203                 goto err;
1204
1205         mutex_init(&_manager->lock);
1206         INIT_LIST_HEAD(&_manager->pools);
1207
1208         _manager->options.max_size = max_pages;
1209         _manager->options.small = SMALL_ALLOCATION;
1210         _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
1211
1212         /* This takes care of auto-freeing the _manager */
1213         ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
1214                                    &glob->kobj, "dma_pool");
1215         if (unlikely(ret != 0)) {
1216                 kobject_put(&_manager->kobj);
1217                 goto err;
1218         }
1219         ttm_dma_pool_mm_shrink_init(_manager);
1220         return 0;
1221 err:
1222         return ret;
1223 }
1224
1225 void ttm_dma_page_alloc_fini(void)
1226 {
1227         struct device_pools *p, *t;
1228
1229         pr_info("Finalizing DMA pool allocator\n");
1230         ttm_dma_pool_mm_shrink_fini(_manager);
1231
1232         list_for_each_entry_safe_reverse(p, t, &_manager->pools, pools) {
1233                 dev_dbg(p->dev, "(%s:%d) Freeing.\n", p->pool->name,
1234                         current->pid);
1235                 WARN_ON(devres_destroy(p->dev, ttm_dma_pool_release,
1236                         ttm_dma_pool_match, p->pool));
1237                 ttm_dma_free_pool(p->dev, p->pool->type);
1238         }
1239         kobject_put(&_manager->kobj);
1240         _manager = NULL;
1241 }
1242
1243 int ttm_dma_page_alloc_debugfs(struct seq_file *m, void *data)
1244 {
1245         struct device_pools *p;
1246         struct dma_pool *pool = NULL;
1247         char *h[] = {"pool", "refills", "pages freed", "inuse", "available",
1248                      "name", "virt", "busaddr"};
1249
1250         if (!_manager) {
1251                 seq_printf(m, "No pool allocator running.\n");
1252                 return 0;
1253         }
1254         seq_printf(m, "%13s %12s %13s %8s %8s %8s\n",
1255                    h[0], h[1], h[2], h[3], h[4], h[5]);
1256         mutex_lock(&_manager->lock);
1257         list_for_each_entry(p, &_manager->pools, pools) {
1258                 struct device *dev = p->dev;
1259                 if (!dev)
1260                         continue;
1261                 pool = p->pool;
1262                 seq_printf(m, "%13s %12ld %13ld %8d %8d %8s\n",
1263                                 pool->name, pool->nrefills,
1264                                 pool->nfrees, pool->npages_in_use,
1265                                 pool->npages_free,
1266                                 pool->dev_name);
1267         }
1268         mutex_unlock(&_manager->lock);
1269         return 0;
1270 }
1271 EXPORT_SYMBOL_GPL(ttm_dma_page_alloc_debugfs);
1272
1273 #endif
This page took 0.108129 seconds and 4 git commands to generate.