drm/ttm: include linux/seq_file.h for seq_printf
[deliverable/linux.git] / drivers / gpu / drm / ttm / ttm_page_alloc.c
CommitLineData
1403b1a3
PN
1/*
2 * Copyright (c) Red Hat Inc.
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 * Authors: Dave Airlie <airlied@redhat.com>
24 * Jerome Glisse <jglisse@redhat.com>
25 * Pauli Nieminen <suokkos@gmail.com>
26 */
27
28/* simple list based uncached page pool
29 * - Pool collects resently freed pages for reuse
30 * - Use page->lru to keep a free list
31 * - doesn't track currently in use pages
32 */
33#include <linux/list.h>
34#include <linux/spinlock.h>
35#include <linux/highmem.h>
36#include <linux/mm_types.h>
07458661 37#include <linux/module.h>
1403b1a3 38#include <linux/mm.h>
4cdc840a 39#include <linux/seq_file.h> /* for seq_printf */
1403b1a3
PN
40
41#include <asm/atomic.h>
42#include <asm/agp.h>
43
44#include "ttm/ttm_bo_driver.h"
45#include "ttm/ttm_page_alloc.h"
46
47
48#define NUM_PAGES_TO_ALLOC (PAGE_SIZE/sizeof(struct page *))
49#define SMALL_ALLOCATION 16
50#define FREE_ALL_PAGES (~0U)
51/* times are in msecs */
52#define PAGE_FREE_INTERVAL 1000
53
54/**
55 * struct ttm_page_pool - Pool to reuse recently allocated uc/wc pages.
56 *
57 * @lock: Protects the shared pool from concurrnet access. Must be used with
58 * irqsave/irqrestore variants because pool allocator maybe called from
59 * delayed work.
60 * @fill_lock: Prevent concurrent calls to fill.
61 * @list: Pool of free uc/wc pages for fast reuse.
62 * @gfp_flags: Flags to pass for alloc_page.
63 * @npages: Number of pages in pool.
64 */
65struct ttm_page_pool {
66 spinlock_t lock;
67 bool fill_lock;
68 struct list_head list;
69 int gfp_flags;
70 unsigned npages;
07458661
PN
71 char *name;
72 unsigned long nfrees;
73 unsigned long nrefills;
1403b1a3
PN
74};
75
c96af79e
PN
76/**
77 * Limits for the pool. They are handled without locks because only place where
78 * they may change is in sysfs store. They won't have immediate effect anyway
79 * so forcing serialiazation to access them is pointless.
80 */
81
1403b1a3
PN
82struct ttm_pool_opts {
83 unsigned alloc_size;
84 unsigned max_size;
85 unsigned small;
86};
87
88#define NUM_POOLS 4
89
90/**
91 * struct ttm_pool_manager - Holds memory pools for fst allocation
92 *
93 * Manager is read only object for pool code so it doesn't need locking.
94 *
95 * @free_interval: minimum number of jiffies between freeing pages from pool.
96 * @page_alloc_inited: reference counting for pool allocation.
97 * @work: Work that is used to shrink the pool. Work is only run when there is
98 * some pages to free.
99 * @small_allocation: Limit in number of pages what is small allocation.
100 *
101 * @pools: All pool objects in use.
102 **/
103struct ttm_pool_manager {
c96af79e 104 struct kobject kobj;
1403b1a3
PN
105 struct shrinker mm_shrink;
106 atomic_t page_alloc_inited;
107 struct ttm_pool_opts options;
108
109 union {
110 struct ttm_page_pool pools[NUM_POOLS];
111 struct {
112 struct ttm_page_pool wc_pool;
113 struct ttm_page_pool uc_pool;
114 struct ttm_page_pool wc_pool_dma32;
115 struct ttm_page_pool uc_pool_dma32;
116 } ;
117 };
118};
119
c96af79e
PN
120static struct attribute ttm_page_pool_max = {
121 .name = "pool_max_size",
122 .mode = S_IRUGO | S_IWUSR
123};
124static struct attribute ttm_page_pool_small = {
125 .name = "pool_small_allocation",
126 .mode = S_IRUGO | S_IWUSR
127};
128static struct attribute ttm_page_pool_alloc_size = {
129 .name = "pool_allocation_size",
130 .mode = S_IRUGO | S_IWUSR
131};
132
133static struct attribute *ttm_pool_attrs[] = {
134 &ttm_page_pool_max,
135 &ttm_page_pool_small,
136 &ttm_page_pool_alloc_size,
137 NULL
138};
139
140static void ttm_pool_kobj_release(struct kobject *kobj)
141{
142 struct ttm_pool_manager *m =
143 container_of(kobj, struct ttm_pool_manager, kobj);
144 (void)m;
145}
146
147static ssize_t ttm_pool_store(struct kobject *kobj,
148 struct attribute *attr, const char *buffer, size_t size)
149{
150 struct ttm_pool_manager *m =
151 container_of(kobj, struct ttm_pool_manager, kobj);
152 int chars;
153 unsigned val;
154 chars = sscanf(buffer, "%u", &val);
155 if (chars == 0)
156 return size;
157
158 /* Convert kb to number of pages */
159 val = val / (PAGE_SIZE >> 10);
160
161 if (attr == &ttm_page_pool_max)
162 m->options.max_size = val;
163 else if (attr == &ttm_page_pool_small)
164 m->options.small = val;
165 else if (attr == &ttm_page_pool_alloc_size) {
166 if (val > NUM_PAGES_TO_ALLOC*8) {
167 printk(KERN_ERR "[ttm] Setting allocation size to %lu "
168 "is not allowed. Recomended size is "
169 "%lu\n",
170 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
171 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
172 return size;
173 } else if (val > NUM_PAGES_TO_ALLOC) {
174 printk(KERN_WARNING "[ttm] Setting allocation size to "
175 "larger than %lu is not recomended.\n",
176 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
177 }
178 m->options.alloc_size = val;
179 }
180
181 return size;
182}
183
184static ssize_t ttm_pool_show(struct kobject *kobj,
185 struct attribute *attr, char *buffer)
186{
187 struct ttm_pool_manager *m =
188 container_of(kobj, struct ttm_pool_manager, kobj);
189 unsigned val = 0;
190
191 if (attr == &ttm_page_pool_max)
192 val = m->options.max_size;
193 else if (attr == &ttm_page_pool_small)
194 val = m->options.small;
195 else if (attr == &ttm_page_pool_alloc_size)
196 val = m->options.alloc_size;
197
198 val = val * (PAGE_SIZE >> 10);
199
200 return snprintf(buffer, PAGE_SIZE, "%u\n", val);
201}
202
203static const struct sysfs_ops ttm_pool_sysfs_ops = {
204 .show = &ttm_pool_show,
205 .store = &ttm_pool_store,
206};
207
208static struct kobj_type ttm_pool_kobj_type = {
209 .release = &ttm_pool_kobj_release,
210 .sysfs_ops = &ttm_pool_sysfs_ops,
211 .default_attrs = ttm_pool_attrs,
212};
213
1403b1a3
PN
214static struct ttm_pool_manager _manager = {
215 .page_alloc_inited = ATOMIC_INIT(0)
216};
217
975efdb1 218#ifndef CONFIG_X86
1403b1a3
PN
219static int set_pages_array_wb(struct page **pages, int addrinarray)
220{
221#ifdef TTM_HAS_AGP
222 int i;
223
224 for (i = 0; i < addrinarray; i++)
225 unmap_page_from_agp(pages[i]);
226#endif
227 return 0;
228}
229
230static int set_pages_array_wc(struct page **pages, int addrinarray)
231{
232#ifdef TTM_HAS_AGP
233 int i;
234
235 for (i = 0; i < addrinarray; i++)
236 map_page_into_agp(pages[i]);
237#endif
238 return 0;
239}
240
241static int set_pages_array_uc(struct page **pages, int addrinarray)
242{
243#ifdef TTM_HAS_AGP
244 int i;
245
246 for (i = 0; i < addrinarray; i++)
247 map_page_into_agp(pages[i]);
248#endif
249 return 0;
250}
251#endif
252
253/**
254 * Select the right pool or requested caching state and ttm flags. */
255static struct ttm_page_pool *ttm_get_pool(int flags,
256 enum ttm_caching_state cstate)
257{
258 int pool_index;
259
260 if (cstate == tt_cached)
261 return NULL;
262
263 if (cstate == tt_wc)
264 pool_index = 0x0;
265 else
266 pool_index = 0x1;
267
268 if (flags & TTM_PAGE_FLAG_DMA32)
269 pool_index |= 0x2;
270
271 return &_manager.pools[pool_index];
272}
273
274/* set memory back to wb and free the pages. */
275static void ttm_pages_put(struct page *pages[], unsigned npages)
276{
277 unsigned i;
278 if (set_pages_array_wb(pages, npages))
279 printk(KERN_ERR "[ttm] Failed to set %d pages to wb!\n",
280 npages);
281 for (i = 0; i < npages; ++i)
282 __free_page(pages[i]);
283}
284
285static void ttm_pool_update_free_locked(struct ttm_page_pool *pool,
286 unsigned freed_pages)
287{
288 pool->npages -= freed_pages;
07458661 289 pool->nfrees += freed_pages;
1403b1a3
PN
290}
291
292/**
293 * Free pages from pool.
294 *
295 * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
296 * number of pages in one go.
297 *
298 * @pool: to free the pages from
299 * @free_all: If set to true will free all pages in pool
300 **/
301static int ttm_page_pool_free(struct ttm_page_pool *pool, unsigned nr_free)
302{
303 unsigned long irq_flags;
304 struct page *p;
305 struct page **pages_to_free;
306 unsigned freed_pages = 0,
307 npages_to_free = nr_free;
308
309 if (NUM_PAGES_TO_ALLOC < nr_free)
310 npages_to_free = NUM_PAGES_TO_ALLOC;
311
312 pages_to_free = kmalloc(npages_to_free * sizeof(struct page *),
313 GFP_KERNEL);
314 if (!pages_to_free) {
315 printk(KERN_ERR "Failed to allocate memory for pool free operation.\n");
316 return 0;
317 }
318
319restart:
320 spin_lock_irqsave(&pool->lock, irq_flags);
321
322 list_for_each_entry_reverse(p, &pool->list, lru) {
323 if (freed_pages >= npages_to_free)
324 break;
325
326 pages_to_free[freed_pages++] = p;
327 /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
328 if (freed_pages >= NUM_PAGES_TO_ALLOC) {
329 /* remove range of pages from the pool */
330 __list_del(p->lru.prev, &pool->list);
331
332 ttm_pool_update_free_locked(pool, freed_pages);
333 /**
334 * Because changing page caching is costly
335 * we unlock the pool to prevent stalling.
336 */
337 spin_unlock_irqrestore(&pool->lock, irq_flags);
338
339 ttm_pages_put(pages_to_free, freed_pages);
340 if (likely(nr_free != FREE_ALL_PAGES))
341 nr_free -= freed_pages;
342
343 if (NUM_PAGES_TO_ALLOC >= nr_free)
344 npages_to_free = nr_free;
345 else
346 npages_to_free = NUM_PAGES_TO_ALLOC;
347
348 freed_pages = 0;
349
350 /* free all so restart the processing */
351 if (nr_free)
352 goto restart;
353
354 /* Not allowed to fall tough or break because
355 * following context is inside spinlock while we are
356 * outside here.
357 */
358 goto out;
359
360 }
361 }
362
1403b1a3
PN
363 /* remove range of pages from the pool */
364 if (freed_pages) {
365 __list_del(&p->lru, &pool->list);
366
367 ttm_pool_update_free_locked(pool, freed_pages);
368 nr_free -= freed_pages;
369 }
370
371 spin_unlock_irqrestore(&pool->lock, irq_flags);
372
373 if (freed_pages)
374 ttm_pages_put(pages_to_free, freed_pages);
375out:
376 kfree(pages_to_free);
377 return nr_free;
378}
379
380/* Get good estimation how many pages are free in pools */
381static int ttm_pool_get_num_unused_pages(void)
382{
383 unsigned i;
384 int total = 0;
385 for (i = 0; i < NUM_POOLS; ++i)
386 total += _manager.pools[i].npages;
387
388 return total;
389}
390
391/**
392 * Calback for mm to request pool to reduce number of page held.
393 */
394static int ttm_pool_mm_shrink(int shrink_pages, gfp_t gfp_mask)
395{
396 static atomic_t start_pool = ATOMIC_INIT(0);
397 unsigned i;
398 unsigned pool_offset = atomic_add_return(1, &start_pool);
399 struct ttm_page_pool *pool;
400
401 pool_offset = pool_offset % NUM_POOLS;
402 /* select start pool in round robin fashion */
403 for (i = 0; i < NUM_POOLS; ++i) {
404 unsigned nr_free = shrink_pages;
405 if (shrink_pages == 0)
406 break;
407 pool = &_manager.pools[(i + pool_offset)%NUM_POOLS];
408 shrink_pages = ttm_page_pool_free(pool, nr_free);
409 }
410 /* return estimated number of unused pages in pool */
411 return ttm_pool_get_num_unused_pages();
412}
413
414static void ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
415{
416 manager->mm_shrink.shrink = &ttm_pool_mm_shrink;
417 manager->mm_shrink.seeks = 1;
418 register_shrinker(&manager->mm_shrink);
419}
420
421static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
422{
423 unregister_shrinker(&manager->mm_shrink);
424}
425
426static int ttm_set_pages_caching(struct page **pages,
427 enum ttm_caching_state cstate, unsigned cpages)
428{
429 int r = 0;
430 /* Set page caching */
431 switch (cstate) {
432 case tt_uncached:
433 r = set_pages_array_uc(pages, cpages);
434 if (r)
435 printk(KERN_ERR "[ttm] Failed to set %d pages to uc!\n",
436 cpages);
437 break;
438 case tt_wc:
439 r = set_pages_array_wc(pages, cpages);
440 if (r)
441 printk(KERN_ERR "[ttm] Failed to set %d pages to wc!\n",
442 cpages);
443 break;
444 default:
445 break;
446 }
447 return r;
448}
449
450/**
451 * Free pages the pages that failed to change the caching state. If there is
452 * any pages that have changed their caching state already put them to the
453 * pool.
454 */
455static void ttm_handle_caching_state_failure(struct list_head *pages,
456 int ttm_flags, enum ttm_caching_state cstate,
457 struct page **failed_pages, unsigned cpages)
458{
459 unsigned i;
460 /* Failed pages has to be reed */
461 for (i = 0; i < cpages; ++i) {
462 list_del(&failed_pages[i]->lru);
463 __free_page(failed_pages[i]);
464 }
465}
466
467/**
468 * Allocate new pages with correct caching.
469 *
470 * This function is reentrant if caller updates count depending on number of
471 * pages returned in pages array.
472 */
473static int ttm_alloc_new_pages(struct list_head *pages, int gfp_flags,
474 int ttm_flags, enum ttm_caching_state cstate, unsigned count)
475{
476 struct page **caching_array;
477 struct page *p;
478 int r = 0;
479 unsigned i, cpages;
480 unsigned max_cpages = min(count,
481 (unsigned)(PAGE_SIZE/sizeof(struct page *)));
482
483 /* allocate array for page caching change */
484 caching_array = kmalloc(max_cpages*sizeof(struct page *), GFP_KERNEL);
485
486 if (!caching_array) {
487 printk(KERN_ERR "[ttm] unable to allocate table for new pages.");
488 return -ENOMEM;
489 }
490
491 for (i = 0, cpages = 0; i < count; ++i) {
492 p = alloc_page(gfp_flags);
493
494 if (!p) {
495 printk(KERN_ERR "[ttm] unable to get page %u\n", i);
496
497 /* store already allocated pages in the pool after
498 * setting the caching state */
499 if (cpages) {
500 r = ttm_set_pages_caching(caching_array, cstate, cpages);
501 if (r)
502 ttm_handle_caching_state_failure(pages,
503 ttm_flags, cstate,
504 caching_array, cpages);
505 }
506 r = -ENOMEM;
507 goto out;
508 }
509
510#ifdef CONFIG_HIGHMEM
511 /* gfp flags of highmem page should never be dma32 so we
512 * we should be fine in such case
513 */
514 if (!PageHighMem(p))
515#endif
516 {
517 caching_array[cpages++] = p;
518 if (cpages == max_cpages) {
519
520 r = ttm_set_pages_caching(caching_array,
521 cstate, cpages);
522 if (r) {
523 ttm_handle_caching_state_failure(pages,
524 ttm_flags, cstate,
525 caching_array, cpages);
526 goto out;
527 }
528 cpages = 0;
529 }
530 }
531
532 list_add(&p->lru, pages);
533 }
534
535 if (cpages) {
536 r = ttm_set_pages_caching(caching_array, cstate, cpages);
537 if (r)
538 ttm_handle_caching_state_failure(pages,
539 ttm_flags, cstate,
540 caching_array, cpages);
541 }
542out:
543 kfree(caching_array);
544
545 return r;
546}
547
548/**
549 * Fill the given pool if there isn't enough pages and requested number of
550 * pages is small.
551 */
552static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool,
553 int ttm_flags, enum ttm_caching_state cstate, unsigned count,
554 unsigned long *irq_flags)
555{
556 struct page *p;
557 int r;
558 unsigned cpages = 0;
559 /**
560 * Only allow one pool fill operation at a time.
561 * If pool doesn't have enough pages for the allocation new pages are
562 * allocated from outside of pool.
563 */
564 if (pool->fill_lock)
565 return;
566
567 pool->fill_lock = true;
568
569 /* If allocation request is small and there is not enough
570 * pages in pool we fill the pool first */
571 if (count < _manager.options.small
572 && count > pool->npages) {
573 struct list_head new_pages;
574 unsigned alloc_size = _manager.options.alloc_size;
575
576 /**
577 * Can't change page caching if in irqsave context. We have to
578 * drop the pool->lock.
579 */
580 spin_unlock_irqrestore(&pool->lock, *irq_flags);
581
582 INIT_LIST_HEAD(&new_pages);
583 r = ttm_alloc_new_pages(&new_pages, pool->gfp_flags, ttm_flags,
584 cstate, alloc_size);
585 spin_lock_irqsave(&pool->lock, *irq_flags);
586
587 if (!r) {
588 list_splice(&new_pages, &pool->list);
07458661 589 ++pool->nrefills;
1403b1a3
PN
590 pool->npages += alloc_size;
591 } else {
592 printk(KERN_ERR "[ttm] Failed to fill pool (%p).", pool);
593 /* If we have any pages left put them to the pool. */
594 list_for_each_entry(p, &pool->list, lru) {
595 ++cpages;
596 }
597 list_splice(&new_pages, &pool->list);
598 pool->npages += cpages;
599 }
600
601 }
602 pool->fill_lock = false;
603}
604
605/**
606 * Cut count nubmer of pages from the pool and put them to return list
607 *
608 * @return count of pages still to allocate to fill the request.
609 */
610static unsigned ttm_page_pool_get_pages(struct ttm_page_pool *pool,
611 struct list_head *pages, int ttm_flags,
612 enum ttm_caching_state cstate, unsigned count)
613{
614 unsigned long irq_flags;
615 struct list_head *p;
616 unsigned i;
617
618 spin_lock_irqsave(&pool->lock, irq_flags);
619 ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count, &irq_flags);
620
621 if (count >= pool->npages) {
622 /* take all pages from the pool */
623 list_splice_init(&pool->list, pages);
624 count -= pool->npages;
625 pool->npages = 0;
626 goto out;
627 }
628 /* find the last pages to include for requested number of pages. Split
629 * pool to begin and halves to reduce search space. */
630 if (count <= pool->npages/2) {
631 i = 0;
632 list_for_each(p, &pool->list) {
633 if (++i == count)
634 break;
635 }
636 } else {
637 i = pool->npages + 1;
638 list_for_each_prev(p, &pool->list) {
639 if (--i == count)
640 break;
641 }
642 }
643 /* Cut count number of pages from pool */
644 list_cut_position(pages, &pool->list, p);
645 pool->npages -= count;
646 count = 0;
647out:
648 spin_unlock_irqrestore(&pool->lock, irq_flags);
649 return count;
650}
651
652/*
653 * On success pages list will hold count number of correctly
654 * cached pages.
655 */
656int ttm_get_pages(struct list_head *pages, int flags,
657 enum ttm_caching_state cstate, unsigned count)
658{
659 struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
660 struct page *p = NULL;
661 int gfp_flags = 0;
662 int r;
663
664 /* set zero flag for page allocation if required */
665 if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
666 gfp_flags |= __GFP_ZERO;
667
668 /* No pool for cached pages */
669 if (pool == NULL) {
670 if (flags & TTM_PAGE_FLAG_DMA32)
671 gfp_flags |= GFP_DMA32;
672 else
673 gfp_flags |= __GFP_HIGHMEM;
674
675 for (r = 0; r < count; ++r) {
676 p = alloc_page(gfp_flags);
677 if (!p) {
678
679 printk(KERN_ERR "[ttm] unable to allocate page.");
680 return -ENOMEM;
681 }
682
683 list_add(&p->lru, pages);
684 }
685 return 0;
686 }
687
688
689 /* combine zero flag to pool flags */
690 gfp_flags |= pool->gfp_flags;
691
692 /* First we take pages from the pool */
693 count = ttm_page_pool_get_pages(pool, pages, flags, cstate, count);
694
695 /* clear the pages coming from the pool if requested */
696 if (flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
697 list_for_each_entry(p, pages, lru) {
698 clear_page(page_address(p));
699 }
700 }
701
702 /* If pool didn't have enough pages allocate new one. */
703 if (count > 0) {
704 /* ttm_alloc_new_pages doesn't reference pool so we can run
705 * multiple requests in parallel.
706 **/
707 r = ttm_alloc_new_pages(pages, gfp_flags, flags, cstate, count);
708 if (r) {
709 /* If there is any pages in the list put them back to
710 * the pool. */
711 printk(KERN_ERR "[ttm] Failed to allocate extra pages "
712 "for large request.");
713 ttm_put_pages(pages, 0, flags, cstate);
714 return r;
715 }
716 }
717
718
719 return 0;
720}
721
722/* Put all pages in pages list to correct pool to wait for reuse */
723void ttm_put_pages(struct list_head *pages, unsigned page_count, int flags,
724 enum ttm_caching_state cstate)
725{
726 unsigned long irq_flags;
727 struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
728 struct page *p, *tmp;
729
730 if (pool == NULL) {
731 /* No pool for this memory type so free the pages */
732
733 list_for_each_entry_safe(p, tmp, pages, lru) {
734 __free_page(p);
735 }
736 /* Make the pages list empty */
737 INIT_LIST_HEAD(pages);
738 return;
739 }
740 if (page_count == 0) {
741 list_for_each_entry_safe(p, tmp, pages, lru) {
742 ++page_count;
743 }
744 }
745
746 spin_lock_irqsave(&pool->lock, irq_flags);
747 list_splice_init(pages, &pool->list);
748 pool->npages += page_count;
749 /* Check that we don't go over the pool limit */
750 page_count = 0;
751 if (pool->npages > _manager.options.max_size) {
752 page_count = pool->npages - _manager.options.max_size;
753 /* free at least NUM_PAGES_TO_ALLOC number of pages
754 * to reduce calls to set_memory_wb */
755 if (page_count < NUM_PAGES_TO_ALLOC)
756 page_count = NUM_PAGES_TO_ALLOC;
757 }
758 spin_unlock_irqrestore(&pool->lock, irq_flags);
759 if (page_count)
760 ttm_page_pool_free(pool, page_count);
761}
762
07458661
PN
763static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, int flags,
764 char *name)
1403b1a3
PN
765{
766 spin_lock_init(&pool->lock);
767 pool->fill_lock = false;
768 INIT_LIST_HEAD(&pool->list);
07458661 769 pool->npages = pool->nfrees = 0;
1403b1a3 770 pool->gfp_flags = flags;
07458661 771 pool->name = name;
1403b1a3
PN
772}
773
c96af79e 774int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
1403b1a3 775{
c96af79e 776 int ret;
1403b1a3
PN
777 if (atomic_add_return(1, &_manager.page_alloc_inited) > 1)
778 return 0;
779
780 printk(KERN_INFO "[ttm] Initializing pool allocator.\n");
781
07458661 782 ttm_page_pool_init_locked(&_manager.wc_pool, GFP_HIGHUSER, "wc");
1403b1a3 783
07458661 784 ttm_page_pool_init_locked(&_manager.uc_pool, GFP_HIGHUSER, "uc");
1403b1a3 785
07458661
PN
786 ttm_page_pool_init_locked(&_manager.wc_pool_dma32, GFP_USER | GFP_DMA32,
787 "wc dma");
1403b1a3 788
07458661
PN
789 ttm_page_pool_init_locked(&_manager.uc_pool_dma32, GFP_USER | GFP_DMA32,
790 "uc dma");
1403b1a3
PN
791
792 _manager.options.max_size = max_pages;
793 _manager.options.small = SMALL_ALLOCATION;
794 _manager.options.alloc_size = NUM_PAGES_TO_ALLOC;
795
c96af79e
PN
796 kobject_init(&_manager.kobj, &ttm_pool_kobj_type);
797 ret = kobject_add(&_manager.kobj, &glob->kobj, "pool");
798 if (unlikely(ret != 0)) {
799 kobject_put(&_manager.kobj);
800 return ret;
801 }
802
1403b1a3
PN
803 ttm_pool_mm_shrink_init(&_manager);
804
805 return 0;
806}
807
808void ttm_page_alloc_fini()
809{
810 int i;
811
812 if (atomic_sub_return(1, &_manager.page_alloc_inited) > 0)
813 return;
814
815 printk(KERN_INFO "[ttm] Finilizing pool allocator.\n");
816 ttm_pool_mm_shrink_fini(&_manager);
817
818 for (i = 0; i < NUM_POOLS; ++i)
819 ttm_page_pool_free(&_manager.pools[i], FREE_ALL_PAGES);
c96af79e
PN
820
821 kobject_put(&_manager.kobj);
1403b1a3 822}
07458661
PN
823
824int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
825{
826 struct ttm_page_pool *p;
827 unsigned i;
828 char *h[] = {"pool", "refills", "pages freed", "size"};
829 if (atomic_read(&_manager.page_alloc_inited) == 0) {
830 seq_printf(m, "No pool allocator running.\n");
831 return 0;
832 }
833 seq_printf(m, "%6s %12s %13s %8s\n",
834 h[0], h[1], h[2], h[3]);
835 for (i = 0; i < NUM_POOLS; ++i) {
836 p = &_manager.pools[i];
837
838 seq_printf(m, "%6s %12ld %13ld %8d\n",
839 p->name, p->nrefills,
840 p->nfrees, p->npages);
841 }
842 return 0;
843}
844EXPORT_SYMBOL(ttm_page_alloc_debugfs);
This page took 0.058963 seconds and 5 git commands to generate.