xref: /dragonfly/sys/dev/drm/ttm/ttm_page_alloc.c (revision 5ca0a96d)
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  * Copyright (c) 2013 The FreeBSD Foundation
29  * All rights reserved.
30  *
31  * Portions of this software were developed by Konstantin Belousov
32  * <kib@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
33  */
34 
35 /* simple list based uncached page pool
36  * - Pool collects resently freed pages for reuse
37  * - Use page->lru to keep a free list
38  * - doesn't track currently in use pages
39  */
40 
41 #define pr_fmt(fmt) "[TTM] " fmt
42 
43 #include <linux/list.h>
44 #include <linux/spinlock.h>
45 #include <linux/highmem.h>
46 #include <linux/mm_types.h>
47 #include <linux/module.h>
48 #include <linux/mm.h>
49 #include <linux/seq_file.h> /* for seq_printf */
50 #include <linux/dma-mapping.h>
51 
52 #include <linux/atomic.h>
53 
54 #include <drm/ttm/ttm_bo_driver.h>
55 #include <drm/ttm/ttm_page_alloc.h>
56 
57 #include <sys/eventhandler.h>
58 #include <vm/vm_page2.h>
59 
60 #if IS_ENABLED(CONFIG_AGP)
61 #include <asm/agp.h>
62 #endif
63 #ifdef CONFIG_X86
64 #include <asm/set_memory.h>
65 #endif
66 
67 #define NUM_PAGES_TO_ALLOC		(PAGE_SIZE/sizeof(struct page *))
68 #define SMALL_ALLOCATION		16
69 #define FREE_ALL_PAGES			(~0U)
70 /* times are in msecs */
71 #define PAGE_FREE_INTERVAL		1000
72 
73 /**
74  * struct ttm_page_pool - Pool to reuse recently allocated uc/wc pages.
75  *
76  * @lock: Protects the shared pool from concurrnet access. Must be used with
77  * irqsave/irqrestore variants because pool allocator maybe called from
78  * delayed work.
79  * @fill_lock: Prevent concurrent calls to fill.
80  * @list: Pool of free uc/wc pages for fast reuse.
81  * @gfp_flags: Flags to pass for alloc_page.
82  * @npages: Number of pages in pool.
83  */
84 struct ttm_page_pool {
85 	struct lock		lock;
86 	bool			fill_lock;
87 	struct pglist		list;
88 	gfp_t			gfp_flags;
89 	unsigned		npages;
90 	char			*name;
91 	unsigned long		nfrees;
92 	unsigned long		nrefills;
93 };
94 
95 /**
96  * Limits for the pool. They are handled without locks because only place where
97  * they may change is in sysfs store. They won't have immediate effect anyway
98  * so forcing serialization to access them is pointless.
99  */
100 
101 struct ttm_pool_opts {
102 	unsigned	alloc_size;
103 	unsigned	max_size;
104 	unsigned	small;
105 };
106 
107 #define NUM_POOLS 4
108 
109 /**
110  * struct ttm_pool_manager - Holds memory pools for fst allocation
111  *
112  * Manager is read only object for pool code so it doesn't need locking.
113  *
114  * @free_interval: minimum number of jiffies between freeing pages from pool.
115  * @page_alloc_inited: reference counting for pool allocation.
116  * @work: Work that is used to shrink the pool. Work is only run when there is
117  * some pages to free.
118  * @small_allocation: Limit in number of pages what is small allocation.
119  *
120  * @pools: All pool objects in use.
121  **/
122 struct ttm_pool_manager {
123 	struct kobject		kobj;
124 	struct shrinker		mm_shrink;
125 	eventhandler_tag lowmem_handler;
126 	struct ttm_pool_opts	options;
127 
128 	union {
129 		struct ttm_page_pool	pools[NUM_POOLS];
130 		struct {
131 			struct ttm_page_pool	wc_pool;
132 			struct ttm_page_pool	uc_pool;
133 			struct ttm_page_pool	wc_pool_dma32;
134 			struct ttm_page_pool	uc_pool_dma32;
135 		} ;
136 	};
137 };
138 
139 static struct attribute ttm_page_pool_max = {
140 	.name = "pool_max_size",
141 	.mode = S_IRUGO | S_IWUSR
142 };
143 static struct attribute ttm_page_pool_small = {
144 	.name = "pool_small_allocation",
145 	.mode = S_IRUGO | S_IWUSR
146 };
147 static struct attribute ttm_page_pool_alloc_size = {
148 	.name = "pool_allocation_size",
149 	.mode = S_IRUGO | S_IWUSR
150 };
151 
152 static struct attribute *ttm_pool_attrs[] = {
153 	&ttm_page_pool_max,
154 	&ttm_page_pool_small,
155 	&ttm_page_pool_alloc_size,
156 	NULL
157 };
158 
159 static void ttm_pool_kobj_release(struct kobject *kobj)
160 {
161 	struct ttm_pool_manager *m =
162 		container_of(kobj, struct ttm_pool_manager, kobj);
163 	kfree(m);
164 }
165 
166 static ssize_t ttm_pool_store(struct kobject *kobj,
167 		struct attribute *attr, const char *buffer, size_t size)
168 {
169 	struct ttm_pool_manager *m =
170 		container_of(kobj, struct ttm_pool_manager, kobj);
171 	int chars;
172 	unsigned val;
173 	chars = ksscanf(buffer, "%u", &val);
174 	if (chars == 0)
175 		return size;
176 
177 	/* Convert kb to number of pages */
178 	val = val / (PAGE_SIZE >> 10);
179 
180 	if (attr == &ttm_page_pool_max)
181 		m->options.max_size = val;
182 	else if (attr == &ttm_page_pool_small)
183 		m->options.small = val;
184 	else if (attr == &ttm_page_pool_alloc_size) {
185 		if (val > NUM_PAGES_TO_ALLOC*8) {
186 			pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
187 			       NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
188 			       NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
189 			return size;
190 		} else if (val > NUM_PAGES_TO_ALLOC) {
191 			pr_warn("Setting allocation size to larger than %lu is not recommended\n",
192 				NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
193 		}
194 		m->options.alloc_size = val;
195 	}
196 
197 	return size;
198 }
199 
200 static ssize_t ttm_pool_show(struct kobject *kobj,
201 		struct attribute *attr, char *buffer)
202 {
203 	struct ttm_pool_manager *m =
204 		container_of(kobj, struct ttm_pool_manager, kobj);
205 	unsigned val = 0;
206 
207 	if (attr == &ttm_page_pool_max)
208 		val = m->options.max_size;
209 	else if (attr == &ttm_page_pool_small)
210 		val = m->options.small;
211 	else if (attr == &ttm_page_pool_alloc_size)
212 		val = m->options.alloc_size;
213 
214 	val = val * (PAGE_SIZE >> 10);
215 
216 	return ksnprintf(buffer, PAGE_SIZE, "%u\n", val);
217 }
218 
219 static const struct sysfs_ops ttm_pool_sysfs_ops = {
220 	.show = &ttm_pool_show,
221 	.store = &ttm_pool_store,
222 };
223 
224 static struct kobj_type ttm_pool_kobj_type = {
225 	.release = &ttm_pool_kobj_release,
226 	.sysfs_ops = &ttm_pool_sysfs_ops,
227 	.default_attrs = ttm_pool_attrs,
228 };
229 
230 static struct ttm_pool_manager *_manager;
231 
232 #ifndef CONFIG_X86
233 static int set_pages_wb(struct page *page, int numpages)
234 {
235 #if IS_ENABLED(CONFIG_AGP)
236 	int i;
237 
238 	for (i = 0; i < numpages; i++)
239 		unmap_page_from_agp(page++);
240 #endif
241 	return 0;
242 }
243 
244 static int set_pages_array_wb(struct page **pages, int addrinarray)
245 {
246 #if IS_ENABLED(CONFIG_AGP)
247 	int i;
248 
249 	for (i = 0; i < addrinarray; i++)
250 		unmap_page_from_agp(pages[i]);
251 #endif
252 	return 0;
253 }
254 
255 static int set_pages_array_wc(struct page **pages, int addrinarray)
256 {
257 #if IS_ENABLED(CONFIG_AGP)
258 	int i;
259 
260 	for (i = 0; i < addrinarray; i++)
261 		map_page_into_agp(pages[i]);
262 #endif
263 	return 0;
264 }
265 
266 static int set_pages_array_uc(struct page **pages, int addrinarray)
267 {
268 #if IS_ENABLED(CONFIG_AGP)
269 	int i;
270 
271 	for (i = 0; i < addrinarray; i++)
272 		map_page_into_agp(pages[i]);
273 #endif
274 	return 0;
275 }
276 #endif
277 
278 /**
279  * Select the right pool or requested caching state and ttm flags. */
280 static struct ttm_page_pool *ttm_get_pool(int flags,
281 		enum ttm_caching_state cstate)
282 {
283 	int pool_index;
284 
285 	if (cstate == tt_cached)
286 		return NULL;
287 
288 	if (cstate == tt_wc)
289 		pool_index = 0x0;
290 	else
291 		pool_index = 0x1;
292 
293 	if (flags & TTM_PAGE_FLAG_DMA32)
294 		pool_index |= 0x2;
295 
296 	return &_manager->pools[pool_index];
297 }
298 
299 /* set memory back to wb and free the pages. */
300 static void ttm_pages_put(struct page *pages[], unsigned npages)
301 {
302 	unsigned i;
303 	if (set_pages_array_wb(pages, npages))
304 		pr_err("Failed to set %d pages to wb!\n", npages);
305 	for (i = 0; i < npages; ++i) {
306 		__free_page(pages[i]);
307 	}
308 }
309 
310 static void ttm_pool_update_free_locked(struct ttm_page_pool *pool,
311 		unsigned freed_pages)
312 {
313 	pool->npages -= freed_pages;
314 	pool->nfrees += freed_pages;
315 }
316 
317 /**
318  * Free pages from pool.
319  *
320  * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
321  * number of pages in one go.
322  *
323  * @pool: to free the pages from
324  * @free_all: If set to true will free all pages in pool
325  * @use_static: Safe to use static buffer
326  **/
327 static int ttm_page_pool_free(struct ttm_page_pool *pool, unsigned nr_free,
328 			      bool use_static)
329 {
330 	static struct page *static_buf[NUM_PAGES_TO_ALLOC];
331 	unsigned long irq_flags;
332 	struct vm_page *p, *p1;
333 	struct page **pages_to_free;
334 	unsigned freed_pages = 0,
335 		 npages_to_free = nr_free;
336 	unsigned i;
337 
338 	if (NUM_PAGES_TO_ALLOC < nr_free)
339 		npages_to_free = NUM_PAGES_TO_ALLOC;
340 
341 	if (use_static)
342 		pages_to_free = static_buf;
343 	else
344 		pages_to_free = kmalloc(npages_to_free * sizeof(struct page *),
345 					M_DRM, GFP_KERNEL);
346 	if (!pages_to_free) {
347 		pr_err("Failed to allocate memory for pool free operation\n");
348 		return 0;
349 	}
350 
351 restart:
352 	spin_lock_irqsave(&pool->lock, irq_flags);
353 
354 	TAILQ_FOREACH_REVERSE_MUTABLE(p, &pool->list, pglist, pageq, p1) {
355 		if (freed_pages >= npages_to_free)
356 			break;
357 
358 		pages_to_free[freed_pages++] = (struct page *)p;
359 		/* We can only remove NUM_PAGES_TO_ALLOC at a time. */
360 		if (freed_pages >= NUM_PAGES_TO_ALLOC) {
361 			/* remove range of pages from the pool */
362 			for (i = 0; i < freed_pages; i++)
363 				TAILQ_REMOVE(&pool->list, (struct vm_page *)pages_to_free[i], pageq);
364 
365 			ttm_pool_update_free_locked(pool, freed_pages);
366 			/**
367 			 * Because changing page caching is costly
368 			 * we unlock the pool to prevent stalling.
369 			 */
370 			spin_unlock_irqrestore(&pool->lock, irq_flags);
371 
372 			ttm_pages_put(pages_to_free, freed_pages);
373 			if (likely(nr_free != FREE_ALL_PAGES))
374 				nr_free -= freed_pages;
375 
376 			if (NUM_PAGES_TO_ALLOC >= nr_free)
377 				npages_to_free = nr_free;
378 			else
379 				npages_to_free = NUM_PAGES_TO_ALLOC;
380 
381 			freed_pages = 0;
382 
383 			/* free all so restart the processing */
384 			if (nr_free)
385 				goto restart;
386 
387 			/* Not allowed to fall through or break because
388 			 * following context is inside spinlock while we are
389 			 * outside here.
390 			 */
391 			goto out;
392 
393 		}
394 	}
395 
396 	/* remove range of pages from the pool */
397 	if (freed_pages) {
398 		for (i = 0; i < freed_pages; i++)
399 			TAILQ_REMOVE(&pool->list, (struct vm_page *)pages_to_free[i], pageq);
400 
401 		ttm_pool_update_free_locked(pool, freed_pages);
402 		nr_free -= freed_pages;
403 	}
404 
405 	spin_unlock_irqrestore(&pool->lock, irq_flags);
406 
407 	if (freed_pages)
408 		ttm_pages_put(pages_to_free, freed_pages);
409 out:
410 	if (pages_to_free != static_buf)
411 		kfree(pages_to_free);
412 	return nr_free;
413 }
414 
415 /**
416  * Callback for mm to request pool to reduce number of page held.
417  *
418  * XXX: (dchinner) Deadlock warning!
419  *
420  * This code is crying out for a shrinker per pool....
421  */
422 static unsigned long
423 ttm_pool_shrink_scan(void *arg)
424 {
425 #ifdef __DragonFly__
426 	static struct shrink_control __sc;
427 	struct shrink_control *sc = &__sc;
428 #endif
429 	static DEFINE_MUTEX(lock);
430 	static unsigned start_pool;
431 	unsigned i;
432 	unsigned pool_offset;
433 	struct ttm_page_pool *pool;
434 	int shrink_pages = 100; /* XXXKIB */
435 	unsigned long freed = 0;
436 
437 #ifdef __DragonFly__
438 	sc->gfp_mask = M_WAITOK;
439 #endif
440 
441 	if (!mutex_trylock(&lock))
442 		return SHRINK_STOP;
443 	pool_offset = ++start_pool % NUM_POOLS;
444 	/* select start pool in round robin fashion */
445 	for (i = 0; i < NUM_POOLS; ++i) {
446 		unsigned nr_free = shrink_pages;
447 		if (shrink_pages == 0)
448 			break;
449 		pool = &_manager->pools[(i + pool_offset)%NUM_POOLS];
450 		/* OK to use static buffer since global mutex is held. */
451 		shrink_pages = ttm_page_pool_free(pool, nr_free, true);
452 		freed += nr_free - shrink_pages;
453 	}
454 	mutex_unlock(&lock);
455 	return freed;
456 }
457 
458 
459 static unsigned long
460 ttm_pool_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
461 {
462 	unsigned i;
463 	unsigned long count = 0;
464 
465 	for (i = 0; i < NUM_POOLS; ++i)
466 		count += _manager->pools[i].npages;
467 
468 	return count;
469 }
470 
471 static void ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
472 {
473 	manager->mm_shrink.count_objects = ttm_pool_shrink_count;
474 	manager->lowmem_handler = EVENTHANDLER_REGISTER(vm_lowmem,
475 	    ttm_pool_shrink_scan, manager, EVENTHANDLER_PRI_ANY);
476 }
477 
478 static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
479 {
480 	EVENTHANDLER_DEREGISTER(vm_lowmem, manager->lowmem_handler);
481 }
482 
483 static int ttm_set_pages_caching(struct page **pages,
484 		enum ttm_caching_state cstate, unsigned cpages)
485 {
486 	int r = 0;
487 	/* Set page caching */
488 	switch (cstate) {
489 	case tt_uncached:
490 		r = set_pages_array_uc(pages, cpages);
491 		if (r)
492 			pr_err("Failed to set %d pages to uc!\n", cpages);
493 		break;
494 	case tt_wc:
495 		r = set_pages_array_wc(pages, cpages);
496 		if (r)
497 			pr_err("Failed to set %d pages to wc!\n", cpages);
498 		break;
499 	default:
500 		break;
501 	}
502 	return r;
503 }
504 
505 /**
506  * Free pages the pages that failed to change the caching state. If there is
507  * any pages that have changed their caching state already put them to the
508  * pool.
509  */
510 static void ttm_handle_caching_state_failure(struct pglist *pages,
511 		int ttm_flags, enum ttm_caching_state cstate,
512 		struct page **failed_pages, unsigned cpages)
513 {
514 	unsigned i;
515 	/* Failed pages have to be freed */
516 	for (i = 0; i < cpages; ++i) {
517 		TAILQ_REMOVE(pages, (struct vm_page *)failed_pages[i], pageq);
518 		__free_page(failed_pages[i]);
519 	}
520 }
521 
522 /**
523  * Allocate new pages with correct caching.
524  *
525  * This function is reentrant if caller updates count depending on number of
526  * pages returned in pages array.
527  */
528 static int ttm_alloc_new_pages(struct pglist *pages, gfp_t gfp_flags,
529 		int ttm_flags, enum ttm_caching_state cstate, unsigned count)
530 {
531 	struct page **caching_array;
532 	struct page *p;
533 	int r = 0;
534 	unsigned i, cpages;
535 	unsigned max_cpages = min(count,
536 			(unsigned)(PAGE_SIZE/sizeof(struct page *)));
537 
538 	/* allocate array for page caching change */
539 	caching_array = kmalloc(max_cpages*sizeof(struct page *), M_DRM, M_WAITOK);
540 
541 	if (!caching_array) {
542 		pr_err("Unable to allocate table for new pages\n");
543 		return -ENOMEM;
544 	}
545 
546 	for (i = 0, cpages = 0; i < count; ++i) {
547 		p = alloc_page(gfp_flags);
548 
549 		if (!p) {
550 			pr_err("Unable to get page %u\n", i);
551 
552 			/* store already allocated pages in the pool after
553 			 * setting the caching state */
554 			if (cpages) {
555 				r = ttm_set_pages_caching(caching_array,
556 							  cstate, cpages);
557 				if (r)
558 					ttm_handle_caching_state_failure(pages,
559 						ttm_flags, cstate,
560 						caching_array, cpages);
561 			}
562 			r = -ENOMEM;
563 			goto out;
564 		}
565 
566 #ifdef CONFIG_HIGHMEM
567 		/* gfp flags of highmem page should never be dma32 so we
568 		 * we should be fine in such case
569 		 */
570 		if (!PageHighMem(p))
571 #endif
572 		{
573 			caching_array[cpages++] = p;
574 			if (cpages == max_cpages) {
575 
576 				r = ttm_set_pages_caching(caching_array,
577 						cstate, cpages);
578 				if (r) {
579 					ttm_handle_caching_state_failure(pages,
580 						ttm_flags, cstate,
581 						caching_array, cpages);
582 					goto out;
583 				}
584 				cpages = 0;
585 			}
586 		}
587 
588 		TAILQ_INSERT_HEAD(pages, (struct vm_page *)p, pageq);
589 	}
590 
591 	if (cpages) {
592 		r = ttm_set_pages_caching(caching_array, cstate, cpages);
593 		if (r)
594 			ttm_handle_caching_state_failure(pages,
595 					ttm_flags, cstate,
596 					caching_array, cpages);
597 	}
598 out:
599 	kfree(caching_array);
600 
601 	return r;
602 }
603 
604 /**
605  * Fill the given pool if there aren't enough pages and the requested number of
606  * pages is small.
607  */
608 static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool,
609 		int ttm_flags, enum ttm_caching_state cstate, unsigned count,
610 		unsigned long *irq_flags)
611 {
612 	vm_page_t p;
613 	int r;
614 	unsigned cpages = 0;
615 	/**
616 	 * Only allow one pool fill operation at a time.
617 	 * If pool doesn't have enough pages for the allocation new pages are
618 	 * allocated from outside of pool.
619 	 */
620 	if (pool->fill_lock)
621 		return;
622 
623 	pool->fill_lock = true;
624 
625 	/* If allocation request is small and there are not enough
626 	 * pages in a pool we fill the pool up first. */
627 	if (count < _manager->options.small
628 		&& count > pool->npages) {
629 		struct pglist new_pages;
630 		unsigned alloc_size = _manager->options.alloc_size;
631 
632 		/**
633 		 * Can't change page caching if in irqsave context. We have to
634 		 * drop the pool->lock.
635 		 */
636 		spin_unlock_irqrestore(&pool->lock, *irq_flags);
637 
638 		TAILQ_INIT(&new_pages);
639 		r = ttm_alloc_new_pages(&new_pages, pool->gfp_flags, ttm_flags,
640 				cstate,	alloc_size);
641 		spin_lock_irqsave(&pool->lock, *irq_flags);
642 
643 		if (!r) {
644 			TAILQ_CONCAT(&pool->list, &new_pages, pageq);
645 			++pool->nrefills;
646 			pool->npages += alloc_size;
647 		} else {
648 			pr_err("Failed to fill pool (%p)\n", pool);
649 			/* If we have any pages left put them to the pool. */
650 			TAILQ_FOREACH(p, &new_pages, pageq) {
651 				++cpages;
652 			}
653 			TAILQ_CONCAT(&pool->list, &new_pages, pageq);
654 			pool->npages += cpages;
655 		}
656 
657 	}
658 	pool->fill_lock = false;
659 }
660 
661 /**
662  * Cut 'count' number of pages from the pool and put them on the return list.
663  *
664  * @return count of pages still required to fulfill the request.
665  */
666 static unsigned ttm_page_pool_get_pages(struct ttm_page_pool *pool,
667 					struct pglist *pages,
668 					int ttm_flags,
669 					enum ttm_caching_state cstate,
670 					unsigned count)
671 {
672 	unsigned long irq_flags;
673 	vm_page_t p;
674 	unsigned i;
675 
676 	spin_lock_irqsave(&pool->lock, irq_flags);
677 	ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count, &irq_flags);
678 
679 	if (count >= pool->npages) {
680 		/* take all pages from the pool */
681 		TAILQ_CONCAT(pages, &pool->list, pageq);
682 		count -= pool->npages;
683 		pool->npages = 0;
684 		goto out;
685 	}
686 	for (i = 0; i < count; i++) {
687 		p = TAILQ_FIRST(&pool->list);
688 		TAILQ_REMOVE(&pool->list, p, pageq);
689 		TAILQ_INSERT_TAIL(pages, p, pageq);
690 	}
691 	pool->npages -= count;
692 	count = 0;
693 out:
694 	spin_unlock_irqrestore(&pool->lock, irq_flags);
695 	return count;
696 }
697 
698 /* Put all pages in pages list to correct pool to wait for reuse */
699 static void ttm_put_pages(struct page **pages, unsigned npages, int flags,
700 			  enum ttm_caching_state cstate)
701 {
702 	unsigned long irq_flags;
703 	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
704 	unsigned i;
705 	struct vm_page *page;
706 
707 	if (pool == NULL) {
708 		/* No pool for this memory type so free the pages */
709 		for (i = 0; i < npages; i++) {
710 			if (pages[i]) {
711 #if 0
712 				if (page_count(pages[i]) != 1)
713 					pr_err("Erroneous page count. Leaking pages.\n");
714 #endif
715 				__free_page(pages[i]);
716 				pages[i] = NULL;
717 			}
718 		}
719 		return;
720 	}
721 
722 	spin_lock_irqsave(&pool->lock, irq_flags);
723 	for (i = 0; i < npages; i++) {
724 		if (pages[i]) {
725 			page = (struct vm_page *)pages[i];
726 			TAILQ_INSERT_TAIL(&pool->list, page, pageq);
727 			pages[i] = NULL;
728 			pool->npages++;
729 		}
730 	}
731 	/* Check that we don't go over the pool limit */
732 	npages = 0;
733 	if (pool->npages > _manager->options.max_size) {
734 		npages = pool->npages - _manager->options.max_size;
735 		/* free at least NUM_PAGES_TO_ALLOC number of pages
736 		 * to reduce calls to set_memory_wb */
737 		if (npages < NUM_PAGES_TO_ALLOC)
738 			npages = NUM_PAGES_TO_ALLOC;
739 	}
740 	spin_unlock_irqrestore(&pool->lock, irq_flags);
741 	if (npages)
742 		ttm_page_pool_free(pool, npages, false);
743 }
744 
745 /*
746  * On success pages list will hold count number of correctly
747  * cached pages.
748  */
749 static int ttm_get_pages(struct page **pages, unsigned npages, int flags,
750 			 enum ttm_caching_state cstate)
751 {
752 	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
753 	struct pglist plist;
754 	struct vm_page *p = NULL;
755 	gfp_t gfp_flags = GFP_USER;
756 	unsigned count;
757 	int r;
758 
759 	/* set zero flag for page allocation if required */
760 	if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
761 		gfp_flags |= __GFP_ZERO;
762 
763 	/* No pool for cached pages */
764 	if (pool == NULL) {
765 		if (flags & TTM_PAGE_FLAG_DMA32)
766 			gfp_flags |= GFP_DMA32;
767 		else
768 			gfp_flags |= GFP_HIGHUSER;
769 
770 		for (r = 0; r < npages; ++r) {
771 			p = (struct vm_page *)alloc_page(gfp_flags);
772 			if (!p) {
773 
774 				pr_err("Unable to allocate page\n");
775 				return -ENOMEM;
776 			}
777 			pages[r] = (struct page *)p;
778 		}
779 		return 0;
780 	}
781 
782 	/* combine zero flag to pool flags */
783 	gfp_flags |= pool->gfp_flags;
784 
785 	/* First we take pages from the pool */
786 	TAILQ_INIT(&plist);
787 	npages = ttm_page_pool_get_pages(pool, &plist, flags, cstate, npages);
788 	count = 0;
789 	TAILQ_FOREACH(p, &plist, pageq) {
790 		pages[count++] = (struct page *)p;
791 	}
792 
793 	/* clear the pages coming from the pool if requested */
794 	if (flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
795 		TAILQ_FOREACH(p, &plist, pageq) {
796 			pmap_zero_page(VM_PAGE_TO_PHYS(p));
797 		}
798 	}
799 
800 	/* If pool didn't have enough pages allocate new one. */
801 	if (npages > 0) {
802 		/* ttm_alloc_new_pages doesn't reference pool so we can run
803 		 * multiple requests in parallel.
804 		 **/
805 		TAILQ_INIT(&plist);
806 		r = ttm_alloc_new_pages(&plist, gfp_flags, flags, cstate, npages);
807 		TAILQ_FOREACH(p, &plist, pageq) {
808 			pages[count++] = (struct page *)p;
809 		}
810 		if (r) {
811 			/* If there is any pages in the list put them back to
812 			 * the pool. */
813 			pr_err("Failed to allocate extra pages for large request\n");
814 			ttm_put_pages(pages, count, flags, cstate);
815 			return r;
816 		}
817 	}
818 
819 	return 0;
820 }
821 
822 static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, gfp_t flags,
823 		char *name)
824 {
825 	lockinit(&pool->lock, "ttmpool", 0, LK_CANRECURSE);
826 	pool->fill_lock = false;
827 	TAILQ_INIT(&pool->list);
828 	pool->npages = pool->nfrees = 0;
829 	pool->gfp_flags = flags;
830 	pool->name = name;
831 }
832 
833 int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
834 {
835 	int ret;
836 
837 	WARN_ON(_manager);
838 
839 	pr_info("Initializing pool allocator\n");
840 
841 	_manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
842 	if (!_manager)
843 		return -ENOMEM;
844 
845 	ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc");
846 
847 	ttm_page_pool_init_locked(&_manager->uc_pool, GFP_HIGHUSER, "uc");
848 
849 	ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
850 				  GFP_USER | GFP_DMA32, "wc dma");
851 
852 	ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
853 				  GFP_USER | GFP_DMA32, "uc dma");
854 
855 	_manager->options.max_size = max_pages;
856 	_manager->options.small = SMALL_ALLOCATION;
857 	_manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
858 
859 	ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
860 				   &glob->kobj, "pool");
861 	if (unlikely(ret != 0)) {
862 		kobject_put(&_manager->kobj);
863 		_manager = NULL;
864 		return ret;
865 	}
866 
867 	ttm_pool_mm_shrink_init(_manager);
868 
869 	return 0;
870 }
871 
872 void ttm_page_alloc_fini(void)
873 {
874 	int i;
875 
876 	pr_info("Finalizing pool allocator\n");
877 	ttm_pool_mm_shrink_fini(_manager);
878 
879 	/* OK to use static buffer since global mutex is no longer used. */
880 	for (i = 0; i < NUM_POOLS; ++i)
881 		ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);
882 
883 	kobject_put(&_manager->kobj);
884 	_manager = NULL;
885 }
886 
887 int ttm_pool_populate(struct ttm_tt *ttm)
888 {
889 	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
890 	unsigned i;
891 	int ret;
892 
893 	if (ttm->state != tt_unpopulated)
894 		return 0;
895 
896 	ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
897 			    ttm->caching_state);
898 	if (unlikely(ret != 0)) {
899 		ttm_pool_unpopulate(ttm);
900 		return ret;
901 	}
902 
903 	for (i = 0; i < ttm->num_pages; ++i) {
904 		ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
905 						PAGE_SIZE);
906 		if (unlikely(ret != 0)) {
907 			ttm_pool_unpopulate(ttm);
908 			return -ENOMEM;
909 		}
910 	}
911 
912 	if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
913 		ret = ttm_tt_swapin(ttm);
914 		if (unlikely(ret != 0)) {
915 			ttm_pool_unpopulate(ttm);
916 			return ret;
917 		}
918 	}
919 
920 	ttm->state = tt_unbound;
921 	return 0;
922 }
923 EXPORT_SYMBOL(ttm_pool_populate);
924 
925 void ttm_pool_unpopulate(struct ttm_tt *ttm)
926 {
927 	unsigned i;
928 
929 	for (i = 0; i < ttm->num_pages; ++i) {
930 		if (!ttm->pages[i])
931 			continue;
932 
933 		ttm_mem_global_free_page(ttm->glob->mem_glob, ttm->pages[i],
934 					 PAGE_SIZE);
935 	}
936 	ttm_put_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
937 		      ttm->caching_state);
938 	ttm->state = tt_unpopulated;
939 }
940 EXPORT_SYMBOL(ttm_pool_unpopulate);
941 
942 int ttm_populate_and_map_pages(struct device *dev, struct ttm_dma_tt *tt)
943 {
944 	unsigned i, j;
945 	int r;
946 
947 	r = ttm_pool_populate(&tt->ttm);
948 	if (r)
949 		return r;
950 
951 	for (i = 0; i < tt->ttm.num_pages; ++i) {
952 		struct page *p = tt->ttm.pages[i];
953 		size_t num_pages = 1;
954 
955 		for (j = i + 1; j < tt->ttm.num_pages; ++j) {
956 			if (++p != tt->ttm.pages[j])
957 				break;
958 
959 			++num_pages;
960 		}
961 
962 		tt->dma_address[i] = dma_map_page(dev, tt->ttm.pages[i],
963 						  0, num_pages * PAGE_SIZE,
964 						  DMA_BIDIRECTIONAL);
965 		if (dma_mapping_error(dev, tt->dma_address[i])) {
966 			while (i--) {
967 				dma_unmap_page(dev, tt->dma_address[i],
968 					       PAGE_SIZE, DMA_BIDIRECTIONAL);
969 				tt->dma_address[i] = 0;
970 			}
971 			ttm_pool_unpopulate(&tt->ttm);
972 			return -EFAULT;
973 		}
974 
975 		for (j = 1; j < num_pages; ++j) {
976 			tt->dma_address[i + 1] = tt->dma_address[i] + PAGE_SIZE;
977 			++i;
978 		}
979 	}
980 	return 0;
981 }
982 EXPORT_SYMBOL(ttm_populate_and_map_pages);
983 
984 void ttm_unmap_and_unpopulate_pages(struct device *dev, struct ttm_dma_tt *tt)
985 {
986 	unsigned i, j;
987 
988 	for (i = 0; i < tt->ttm.num_pages;) {
989 		struct page *p = tt->ttm.pages[i];
990 		size_t num_pages = 1;
991 
992 		if (!tt->dma_address[i] || !tt->ttm.pages[i]) {
993 			++i;
994 			continue;
995 		}
996 
997 		for (j = i + 1; j < tt->ttm.num_pages; ++j) {
998 			if (++p != tt->ttm.pages[j])
999 				break;
1000 
1001 			++num_pages;
1002 		}
1003 
1004 		dma_unmap_page(dev, tt->dma_address[i], num_pages * PAGE_SIZE,
1005 			       DMA_BIDIRECTIONAL);
1006 
1007 		i += num_pages;
1008 	}
1009 	ttm_pool_unpopulate(&tt->ttm);
1010 }
1011 EXPORT_SYMBOL(ttm_unmap_and_unpopulate_pages);
1012 
1013 #if 0
1014 int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
1015 {
1016 	struct ttm_page_pool *p;
1017 	unsigned i;
1018 	char *h[] = {"pool", "refills", "pages freed", "size"};
1019 	if (!_manager) {
1020 		seq_printf(m, "No pool allocator running.\n");
1021 		return 0;
1022 	}
1023 	seq_printf(m, "%6s %12s %13s %8s\n",
1024 			h[0], h[1], h[2], h[3]);
1025 	for (i = 0; i < NUM_POOLS; ++i) {
1026 		p = &_manager->pools[i];
1027 
1028 		seq_printf(m, "%6s %12ld %13ld %8d\n",
1029 				p->name, p->nrefills,
1030 				p->nfrees, p->npages);
1031 	}
1032 	return 0;
1033 }
1034 #endif
1035 EXPORT_SYMBOL(ttm_page_alloc_debugfs);
1036