xref: /freebsd/sys/dev/drm2/ttm/ttm_page_alloc.c (revision a0f704ffc16be42abf4be1573eab25857c2d1fcc)
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 #include <sys/cdefs.h>
42 __FBSDID("$FreeBSD$");
43 
44 #include <dev/drm2/drmP.h>
45 #include <dev/drm2/ttm/ttm_bo_driver.h>
46 #include <dev/drm2/ttm/ttm_page_alloc.h>
47 
48 #define NUM_PAGES_TO_ALLOC		(PAGE_SIZE/sizeof(vm_page_t))
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  */
65 struct ttm_page_pool {
66 	struct mtx		lock;
67 	bool			fill_lock;
68 	bool			dma32;
69 	struct pglist		list;
70 	int			ttm_page_alloc_flags;
71 	unsigned		npages;
72 	char			*name;
73 	unsigned long		nfrees;
74 	unsigned long		nrefills;
75 };
76 
77 /**
78  * Limits for the pool. They are handled without locks because only place where
79  * they may change is in sysfs store. They won't have immediate effect anyway
80  * so forcing serialization to access them is pointless.
81  */
82 
83 struct ttm_pool_opts {
84 	unsigned	alloc_size;
85 	unsigned	max_size;
86 	unsigned	small;
87 };
88 
89 #define NUM_POOLS 4
90 
91 /**
92  * struct ttm_pool_manager - Holds memory pools for fst allocation
93  *
94  * Manager is read only object for pool code so it doesn't need locking.
95  *
96  * @free_interval: minimum number of jiffies between freeing pages from pool.
97  * @page_alloc_inited: reference counting for pool allocation.
98  * @work: Work that is used to shrink the pool. Work is only run when there is
99  * some pages to free.
100  * @small_allocation: Limit in number of pages what is small allocation.
101  *
102  * @pools: All pool objects in use.
103  **/
104 struct ttm_pool_manager {
105 	unsigned int kobj_ref;
106 	eventhandler_tag lowmem_handler;
107 	struct ttm_pool_opts	options;
108 
109 	union {
110 		struct ttm_page_pool	u_pools[NUM_POOLS];
111 		struct _utag {
112 			struct ttm_page_pool	u_wc_pool;
113 			struct ttm_page_pool	u_uc_pool;
114 			struct ttm_page_pool	u_wc_pool_dma32;
115 			struct ttm_page_pool	u_uc_pool_dma32;
116 		} _ut;
117 	} _u;
118 };
119 
120 #define	pools _u.u_pools
121 #define	wc_pool _u._ut.u_wc_pool
122 #define	uc_pool _u._ut.u_uc_pool
123 #define	wc_pool_dma32 _u._ut.u_wc_pool_dma32
124 #define	uc_pool_dma32 _u._ut.u_uc_pool_dma32
125 
126 MALLOC_DEFINE(M_TTM_POOLMGR, "ttm_poolmgr", "TTM Pool Manager");
127 
128 static void
129 ttm_vm_page_free(vm_page_t m)
130 {
131 
132 	KASSERT(m->object == NULL, ("ttm page %p is owned", m));
133 	KASSERT(m->wire_count == 1, ("ttm lost wire %p", m));
134 	KASSERT((m->flags & PG_FICTITIOUS) != 0, ("ttm lost fictitious %p", m));
135 	KASSERT((m->oflags & VPO_UNMANAGED) == 0, ("ttm got unmanaged %p", m));
136 	m->flags &= ~PG_FICTITIOUS;
137 	m->oflags |= VPO_UNMANAGED;
138 	vm_page_unwire(m, PQ_INACTIVE);
139 	vm_page_free(m);
140 }
141 
142 static vm_memattr_t
143 ttm_caching_state_to_vm(enum ttm_caching_state cstate)
144 {
145 
146 	switch (cstate) {
147 	case tt_uncached:
148 		return (VM_MEMATTR_UNCACHEABLE);
149 	case tt_wc:
150 		return (VM_MEMATTR_WRITE_COMBINING);
151 	case tt_cached:
152 		return (VM_MEMATTR_WRITE_BACK);
153 	}
154 	panic("caching state %d\n", cstate);
155 }
156 
157 static void ttm_pool_kobj_release(struct ttm_pool_manager *m)
158 {
159 
160 	free(m, M_TTM_POOLMGR);
161 }
162 
163 #if 0
164 /* XXXKIB sysctl */
165 static ssize_t ttm_pool_store(struct ttm_pool_manager *m,
166 		struct attribute *attr, const char *buffer, size_t size)
167 {
168 	int chars;
169 	unsigned val;
170 	chars = sscanf(buffer, "%u", &val);
171 	if (chars == 0)
172 		return size;
173 
174 	/* Convert kb to number of pages */
175 	val = val / (PAGE_SIZE >> 10);
176 
177 	if (attr == &ttm_page_pool_max)
178 		m->options.max_size = val;
179 	else if (attr == &ttm_page_pool_small)
180 		m->options.small = val;
181 	else if (attr == &ttm_page_pool_alloc_size) {
182 		if (val > NUM_PAGES_TO_ALLOC*8) {
183 			pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
184 			       NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
185 			       NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
186 			return size;
187 		} else if (val > NUM_PAGES_TO_ALLOC) {
188 			pr_warn("Setting allocation size to larger than %lu is not recommended\n",
189 				NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
190 		}
191 		m->options.alloc_size = val;
192 	}
193 
194 	return size;
195 }
196 
197 static ssize_t ttm_pool_show(struct ttm_pool_manager *m,
198 		struct attribute *attr, char *buffer)
199 {
200 	unsigned val = 0;
201 
202 	if (attr == &ttm_page_pool_max)
203 		val = m->options.max_size;
204 	else if (attr == &ttm_page_pool_small)
205 		val = m->options.small;
206 	else if (attr == &ttm_page_pool_alloc_size)
207 		val = m->options.alloc_size;
208 
209 	val = val * (PAGE_SIZE >> 10);
210 
211 	return snprintf(buffer, PAGE_SIZE, "%u\n", val);
212 }
213 #endif
214 
215 static struct ttm_pool_manager *_manager;
216 
217 static int set_pages_array_wb(vm_page_t *pages, int addrinarray)
218 {
219 #ifdef TTM_HAS_AGP
220 	int i;
221 
222 	for (i = 0; i < addrinarray; i++)
223 		pmap_page_set_memattr(pages[i], VM_MEMATTR_WRITE_BACK);
224 #endif
225 	return 0;
226 }
227 
228 static int set_pages_array_wc(vm_page_t *pages, int addrinarray)
229 {
230 #ifdef TTM_HAS_AGP
231 	int i;
232 
233 	for (i = 0; i < addrinarray; i++)
234 		pmap_page_set_memattr(pages[i], VM_MEMATTR_WRITE_COMBINING);
235 #endif
236 	return 0;
237 }
238 
239 static int set_pages_array_uc(vm_page_t *pages, int addrinarray)
240 {
241 #ifdef TTM_HAS_AGP
242 	int i;
243 
244 	for (i = 0; i < addrinarray; i++)
245 		pmap_page_set_memattr(pages[i], VM_MEMATTR_UNCACHEABLE);
246 #endif
247 	return 0;
248 }
249 
250 /**
251  * Select the right pool or requested caching state and ttm flags. */
252 static struct ttm_page_pool *ttm_get_pool(int flags,
253 		enum ttm_caching_state cstate)
254 {
255 	int pool_index;
256 
257 	if (cstate == tt_cached)
258 		return NULL;
259 
260 	if (cstate == tt_wc)
261 		pool_index = 0x0;
262 	else
263 		pool_index = 0x1;
264 
265 	if (flags & TTM_PAGE_FLAG_DMA32)
266 		pool_index |= 0x2;
267 
268 	return &_manager->pools[pool_index];
269 }
270 
271 /* set memory back to wb and free the pages. */
272 static void ttm_pages_put(vm_page_t *pages, unsigned npages)
273 {
274 	unsigned i;
275 
276 	/* Our VM handles vm memattr automatically on the page free. */
277 	if (set_pages_array_wb(pages, npages))
278 		printf("[TTM] Failed to set %d pages to wb!\n", npages);
279 	for (i = 0; i < npages; ++i)
280 		ttm_vm_page_free(pages[i]);
281 }
282 
283 static void ttm_pool_update_free_locked(struct ttm_page_pool *pool,
284 		unsigned freed_pages)
285 {
286 	pool->npages -= freed_pages;
287 	pool->nfrees += freed_pages;
288 }
289 
290 /**
291  * Free pages from pool.
292  *
293  * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
294  * number of pages in one go.
295  *
296  * @pool: to free the pages from
297  * @free_all: If set to true will free all pages in pool
298  **/
299 static int ttm_page_pool_free(struct ttm_page_pool *pool, unsigned nr_free)
300 {
301 	vm_page_t p, p1;
302 	vm_page_t *pages_to_free;
303 	unsigned freed_pages = 0,
304 		 npages_to_free = nr_free;
305 	unsigned i;
306 
307 	if (NUM_PAGES_TO_ALLOC < nr_free)
308 		npages_to_free = NUM_PAGES_TO_ALLOC;
309 
310 	pages_to_free = malloc(npages_to_free * sizeof(vm_page_t),
311 	    M_TEMP, M_WAITOK | M_ZERO);
312 
313 restart:
314 	mtx_lock(&pool->lock);
315 
316 	TAILQ_FOREACH_REVERSE_SAFE(p, &pool->list, pglist, plinks.q, p1) {
317 		if (freed_pages >= npages_to_free)
318 			break;
319 
320 		pages_to_free[freed_pages++] = p;
321 		/* We can only remove NUM_PAGES_TO_ALLOC at a time. */
322 		if (freed_pages >= NUM_PAGES_TO_ALLOC) {
323 			/* remove range of pages from the pool */
324 			for (i = 0; i < freed_pages; i++)
325 				TAILQ_REMOVE(&pool->list, pages_to_free[i], plinks.q);
326 
327 			ttm_pool_update_free_locked(pool, freed_pages);
328 			/**
329 			 * Because changing page caching is costly
330 			 * we unlock the pool to prevent stalling.
331 			 */
332 			mtx_unlock(&pool->lock);
333 
334 			ttm_pages_put(pages_to_free, freed_pages);
335 			if (likely(nr_free != FREE_ALL_PAGES))
336 				nr_free -= freed_pages;
337 
338 			if (NUM_PAGES_TO_ALLOC >= nr_free)
339 				npages_to_free = nr_free;
340 			else
341 				npages_to_free = NUM_PAGES_TO_ALLOC;
342 
343 			freed_pages = 0;
344 
345 			/* free all so restart the processing */
346 			if (nr_free)
347 				goto restart;
348 
349 			/* Not allowed to fall through or break because
350 			 * following context is inside spinlock while we are
351 			 * outside here.
352 			 */
353 			goto out;
354 
355 		}
356 	}
357 
358 	/* remove range of pages from the pool */
359 	if (freed_pages) {
360 		for (i = 0; i < freed_pages; i++)
361 			TAILQ_REMOVE(&pool->list, pages_to_free[i], plinks.q);
362 
363 		ttm_pool_update_free_locked(pool, freed_pages);
364 		nr_free -= freed_pages;
365 	}
366 
367 	mtx_unlock(&pool->lock);
368 
369 	if (freed_pages)
370 		ttm_pages_put(pages_to_free, freed_pages);
371 out:
372 	free(pages_to_free, M_TEMP);
373 	return nr_free;
374 }
375 
376 /* Get good estimation how many pages are free in pools */
377 static int ttm_pool_get_num_unused_pages(void)
378 {
379 	unsigned i;
380 	int total = 0;
381 	for (i = 0; i < NUM_POOLS; ++i)
382 		total += _manager->pools[i].npages;
383 
384 	return total;
385 }
386 
387 /**
388  * Callback for mm to request pool to reduce number of page held.
389  */
390 static int ttm_pool_mm_shrink(void *arg)
391 {
392 	static unsigned int start_pool = 0;
393 	unsigned i;
394 	unsigned pool_offset = atomic_fetchadd_int(&start_pool, 1);
395 	struct ttm_page_pool *pool;
396 	int shrink_pages = 100; /* XXXKIB */
397 
398 	pool_offset = pool_offset % NUM_POOLS;
399 	/* select start pool in round robin fashion */
400 	for (i = 0; i < NUM_POOLS; ++i) {
401 		unsigned nr_free = shrink_pages;
402 		if (shrink_pages == 0)
403 			break;
404 		pool = &_manager->pools[(i + pool_offset)%NUM_POOLS];
405 		shrink_pages = ttm_page_pool_free(pool, nr_free);
406 	}
407 	/* return estimated number of unused pages in pool */
408 	return ttm_pool_get_num_unused_pages();
409 }
410 
411 static void ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
412 {
413 
414 	manager->lowmem_handler = EVENTHANDLER_REGISTER(vm_lowmem,
415 	    ttm_pool_mm_shrink, manager, EVENTHANDLER_PRI_ANY);
416 }
417 
418 static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
419 {
420 
421 	EVENTHANDLER_DEREGISTER(vm_lowmem, manager->lowmem_handler);
422 }
423 
424 static int ttm_set_pages_caching(vm_page_t *pages,
425 		enum ttm_caching_state cstate, unsigned cpages)
426 {
427 	int r = 0;
428 	/* Set page caching */
429 	switch (cstate) {
430 	case tt_uncached:
431 		r = set_pages_array_uc(pages, cpages);
432 		if (r)
433 			printf("[TTM] Failed to set %d pages to uc!\n", cpages);
434 		break;
435 	case tt_wc:
436 		r = set_pages_array_wc(pages, cpages);
437 		if (r)
438 			printf("[TTM] Failed to set %d pages to wc!\n", cpages);
439 		break;
440 	default:
441 		break;
442 	}
443 	return r;
444 }
445 
446 /**
447  * Free pages the pages that failed to change the caching state. If there is
448  * any pages that have changed their caching state already put them to the
449  * pool.
450  */
451 static void ttm_handle_caching_state_failure(struct pglist *pages,
452 		int ttm_flags, enum ttm_caching_state cstate,
453 		vm_page_t *failed_pages, unsigned cpages)
454 {
455 	unsigned i;
456 	/* Failed pages have to be freed */
457 	for (i = 0; i < cpages; ++i) {
458 		TAILQ_REMOVE(pages, failed_pages[i], plinks.q);
459 		ttm_vm_page_free(failed_pages[i]);
460 	}
461 }
462 
463 /**
464  * Allocate new pages with correct caching.
465  *
466  * This function is reentrant if caller updates count depending on number of
467  * pages returned in pages array.
468  */
469 static int ttm_alloc_new_pages(struct pglist *pages, int ttm_alloc_flags,
470 		int ttm_flags, enum ttm_caching_state cstate, unsigned count)
471 {
472 	vm_page_t *caching_array;
473 	vm_page_t p;
474 	int r = 0;
475 	unsigned i, cpages, aflags;
476 	unsigned max_cpages = min(count,
477 			(unsigned)(PAGE_SIZE/sizeof(vm_page_t)));
478 
479 	aflags = VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_NOOBJ |
480 	    ((ttm_alloc_flags & TTM_PAGE_FLAG_ZERO_ALLOC) != 0 ?
481 	    VM_ALLOC_ZERO : 0);
482 
483 	/* allocate array for page caching change */
484 	caching_array = malloc(max_cpages * sizeof(vm_page_t), M_TEMP,
485 	    M_WAITOK | M_ZERO);
486 
487 	for (i = 0, cpages = 0; i < count; ++i) {
488 		p = vm_page_alloc_contig(NULL, 0, aflags, 1, 0,
489 		    (ttm_alloc_flags & TTM_PAGE_FLAG_DMA32) ? 0xffffffff :
490 		    VM_MAX_ADDRESS, PAGE_SIZE, 0,
491 		    ttm_caching_state_to_vm(cstate));
492 		if (!p) {
493 			printf("[TTM] Unable to get page %u\n", i);
494 
495 			/* store already allocated pages in the pool after
496 			 * setting the caching state */
497 			if (cpages) {
498 				r = ttm_set_pages_caching(caching_array,
499 							  cstate, cpages);
500 				if (r)
501 					ttm_handle_caching_state_failure(pages,
502 						ttm_flags, cstate,
503 						caching_array, cpages);
504 			}
505 			r = -ENOMEM;
506 			goto out;
507 		}
508 		p->oflags &= ~VPO_UNMANAGED;
509 		p->flags |= PG_FICTITIOUS;
510 
511 #ifdef CONFIG_HIGHMEM /* KIB: nop */
512 		/* gfp flags of highmem page should never be dma32 so we
513 		 * we should be fine in such case
514 		 */
515 		if (!PageHighMem(p))
516 #endif
517 		{
518 			caching_array[cpages++] = p;
519 			if (cpages == max_cpages) {
520 
521 				r = ttm_set_pages_caching(caching_array,
522 						cstate, cpages);
523 				if (r) {
524 					ttm_handle_caching_state_failure(pages,
525 						ttm_flags, cstate,
526 						caching_array, cpages);
527 					goto out;
528 				}
529 				cpages = 0;
530 			}
531 		}
532 
533 		TAILQ_INSERT_HEAD(pages, p, plinks.q);
534 	}
535 
536 	if (cpages) {
537 		r = ttm_set_pages_caching(caching_array, cstate, cpages);
538 		if (r)
539 			ttm_handle_caching_state_failure(pages,
540 					ttm_flags, cstate,
541 					caching_array, cpages);
542 	}
543 out:
544 	free(caching_array, M_TEMP);
545 
546 	return r;
547 }
548 
549 /**
550  * Fill the given pool if there aren't enough pages and the requested number of
551  * pages is small.
552  */
553 static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool,
554     int ttm_flags, enum ttm_caching_state cstate, unsigned count)
555 {
556 	vm_page_t 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 are not enough
570 	 * pages in a pool we fill the pool up first. */
571 	if (count < _manager->options.small
572 		&& count > pool->npages) {
573 		struct pglist 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 		mtx_unlock(&pool->lock);
581 
582 		TAILQ_INIT(&new_pages);
583 		r = ttm_alloc_new_pages(&new_pages, pool->ttm_page_alloc_flags,
584 		    ttm_flags, cstate, alloc_size);
585 		mtx_lock(&pool->lock);
586 
587 		if (!r) {
588 			TAILQ_CONCAT(&pool->list, &new_pages, plinks.q);
589 			++pool->nrefills;
590 			pool->npages += alloc_size;
591 		} else {
592 			printf("[TTM] Failed to fill pool (%p)\n", pool);
593 			/* If we have any pages left put them to the pool. */
594 			TAILQ_FOREACH(p, &pool->list, plinks.q) {
595 				++cpages;
596 			}
597 			TAILQ_CONCAT(&pool->list, &new_pages, plinks.q);
598 			pool->npages += cpages;
599 		}
600 
601 	}
602 	pool->fill_lock = false;
603 }
604 
605 /**
606  * Cut 'count' number of pages from the pool and put them on the return list.
607  *
608  * @return count of pages still required to fulfill the request.
609  */
610 static unsigned ttm_page_pool_get_pages(struct ttm_page_pool *pool,
611 					struct pglist *pages,
612 					int ttm_flags,
613 					enum ttm_caching_state cstate,
614 					unsigned count)
615 {
616 	vm_page_t p;
617 	unsigned i;
618 
619 	mtx_lock(&pool->lock);
620 	ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count);
621 
622 	if (count >= pool->npages) {
623 		/* take all pages from the pool */
624 		TAILQ_CONCAT(pages, &pool->list, plinks.q);
625 		count -= pool->npages;
626 		pool->npages = 0;
627 		goto out;
628 	}
629 	for (i = 0; i < count; i++) {
630 		p = TAILQ_FIRST(&pool->list);
631 		TAILQ_REMOVE(&pool->list, p, plinks.q);
632 		TAILQ_INSERT_TAIL(pages, p, plinks.q);
633 	}
634 	pool->npages -= count;
635 	count = 0;
636 out:
637 	mtx_unlock(&pool->lock);
638 	return count;
639 }
640 
641 /* Put all pages in pages list to correct pool to wait for reuse */
642 static void ttm_put_pages(vm_page_t *pages, unsigned npages, int flags,
643 			  enum ttm_caching_state cstate)
644 {
645 	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
646 	unsigned i;
647 
648 	if (pool == NULL) {
649 		/* No pool for this memory type so free the pages */
650 		for (i = 0; i < npages; i++) {
651 			if (pages[i]) {
652 				ttm_vm_page_free(pages[i]);
653 				pages[i] = NULL;
654 			}
655 		}
656 		return;
657 	}
658 
659 	mtx_lock(&pool->lock);
660 	for (i = 0; i < npages; i++) {
661 		if (pages[i]) {
662 			TAILQ_INSERT_TAIL(&pool->list, pages[i], plinks.q);
663 			pages[i] = NULL;
664 			pool->npages++;
665 		}
666 	}
667 	/* Check that we don't go over the pool limit */
668 	npages = 0;
669 	if (pool->npages > _manager->options.max_size) {
670 		npages = pool->npages - _manager->options.max_size;
671 		/* free at least NUM_PAGES_TO_ALLOC number of pages
672 		 * to reduce calls to set_memory_wb */
673 		if (npages < NUM_PAGES_TO_ALLOC)
674 			npages = NUM_PAGES_TO_ALLOC;
675 	}
676 	mtx_unlock(&pool->lock);
677 	if (npages)
678 		ttm_page_pool_free(pool, npages);
679 }
680 
681 /*
682  * On success pages list will hold count number of correctly
683  * cached pages.
684  */
685 static int ttm_get_pages(vm_page_t *pages, unsigned npages, int flags,
686 			 enum ttm_caching_state cstate)
687 {
688 	struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
689 	struct pglist plist;
690 	vm_page_t p = NULL;
691 	int gfp_flags, aflags;
692 	unsigned count;
693 	int r;
694 
695 	aflags = VM_ALLOC_NORMAL | VM_ALLOC_NOOBJ | VM_ALLOC_WIRED |
696 	    ((flags & TTM_PAGE_FLAG_ZERO_ALLOC) != 0 ? VM_ALLOC_ZERO : 0);
697 
698 	/* No pool for cached pages */
699 	if (pool == NULL) {
700 		for (r = 0; r < npages; ++r) {
701 			p = vm_page_alloc_contig(NULL, 0, aflags, 1, 0,
702 			    (flags & TTM_PAGE_FLAG_DMA32) ? 0xffffffff :
703 			    VM_MAX_ADDRESS, PAGE_SIZE,
704 			    0, ttm_caching_state_to_vm(cstate));
705 			if (!p) {
706 				printf("[TTM] Unable to allocate page\n");
707 				return -ENOMEM;
708 			}
709 			p->oflags &= ~VPO_UNMANAGED;
710 			p->flags |= PG_FICTITIOUS;
711 			pages[r] = p;
712 		}
713 		return 0;
714 	}
715 
716 	/* combine zero flag to pool flags */
717 	gfp_flags = flags | pool->ttm_page_alloc_flags;
718 
719 	/* First we take pages from the pool */
720 	TAILQ_INIT(&plist);
721 	npages = ttm_page_pool_get_pages(pool, &plist, flags, cstate, npages);
722 	count = 0;
723 	TAILQ_FOREACH(p, &plist, plinks.q) {
724 		pages[count++] = p;
725 	}
726 
727 	/* clear the pages coming from the pool if requested */
728 	if (flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
729 		TAILQ_FOREACH(p, &plist, plinks.q) {
730 			pmap_zero_page(p);
731 		}
732 	}
733 
734 	/* If pool didn't have enough pages allocate new one. */
735 	if (npages > 0) {
736 		/* ttm_alloc_new_pages doesn't reference pool so we can run
737 		 * multiple requests in parallel.
738 		 **/
739 		TAILQ_INIT(&plist);
740 		r = ttm_alloc_new_pages(&plist, gfp_flags, flags, cstate,
741 		    npages);
742 		TAILQ_FOREACH(p, &plist, plinks.q) {
743 			pages[count++] = p;
744 		}
745 		if (r) {
746 			/* If there is any pages in the list put them back to
747 			 * the pool. */
748 			printf("[TTM] Failed to allocate extra pages for large request\n");
749 			ttm_put_pages(pages, count, flags, cstate);
750 			return r;
751 		}
752 	}
753 
754 	return 0;
755 }
756 
757 static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, int flags,
758 				      char *name)
759 {
760 	mtx_init(&pool->lock, "ttmpool", NULL, MTX_DEF);
761 	pool->fill_lock = false;
762 	TAILQ_INIT(&pool->list);
763 	pool->npages = pool->nfrees = 0;
764 	pool->ttm_page_alloc_flags = flags;
765 	pool->name = name;
766 }
767 
768 int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
769 {
770 
771 	if (_manager != NULL)
772 		printf("[TTM] manager != NULL\n");
773 	printf("[TTM] Initializing pool allocator\n");
774 
775 	_manager = malloc(sizeof(*_manager), M_TTM_POOLMGR, M_WAITOK | M_ZERO);
776 
777 	ttm_page_pool_init_locked(&_manager->wc_pool, 0, "wc");
778 	ttm_page_pool_init_locked(&_manager->uc_pool, 0, "uc");
779 	ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
780 	    TTM_PAGE_FLAG_DMA32, "wc dma");
781 	ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
782 	    TTM_PAGE_FLAG_DMA32, "uc dma");
783 
784 	_manager->options.max_size = max_pages;
785 	_manager->options.small = SMALL_ALLOCATION;
786 	_manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
787 
788 	refcount_init(&_manager->kobj_ref, 1);
789 	ttm_pool_mm_shrink_init(_manager);
790 
791 	return 0;
792 }
793 
794 void ttm_page_alloc_fini(void)
795 {
796 	int i;
797 
798 	printf("[TTM] Finalizing pool allocator\n");
799 	ttm_pool_mm_shrink_fini(_manager);
800 
801 	for (i = 0; i < NUM_POOLS; ++i)
802 		ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES);
803 
804 	if (refcount_release(&_manager->kobj_ref))
805 		ttm_pool_kobj_release(_manager);
806 	_manager = NULL;
807 }
808 
809 int ttm_pool_populate(struct ttm_tt *ttm)
810 {
811 	struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
812 	unsigned i;
813 	int ret;
814 
815 	if (ttm->state != tt_unpopulated)
816 		return 0;
817 
818 	for (i = 0; i < ttm->num_pages; ++i) {
819 		ret = ttm_get_pages(&ttm->pages[i], 1,
820 				    ttm->page_flags,
821 				    ttm->caching_state);
822 		if (ret != 0) {
823 			ttm_pool_unpopulate(ttm);
824 			return -ENOMEM;
825 		}
826 
827 		ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
828 						false, false);
829 		if (unlikely(ret != 0)) {
830 			ttm_pool_unpopulate(ttm);
831 			return -ENOMEM;
832 		}
833 	}
834 
835 	if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
836 		ret = ttm_tt_swapin(ttm);
837 		if (unlikely(ret != 0)) {
838 			ttm_pool_unpopulate(ttm);
839 			return ret;
840 		}
841 	}
842 
843 	ttm->state = tt_unbound;
844 	return 0;
845 }
846 
847 void ttm_pool_unpopulate(struct ttm_tt *ttm)
848 {
849 	unsigned i;
850 
851 	for (i = 0; i < ttm->num_pages; ++i) {
852 		if (ttm->pages[i]) {
853 			ttm_mem_global_free_page(ttm->glob->mem_glob,
854 						 ttm->pages[i]);
855 			ttm_put_pages(&ttm->pages[i], 1,
856 				      ttm->page_flags,
857 				      ttm->caching_state);
858 		}
859 	}
860 	ttm->state = tt_unpopulated;
861 }
862 
863 #if 0
864 /* XXXKIB sysctl */
865 int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
866 {
867 	struct ttm_page_pool *p;
868 	unsigned i;
869 	char *h[] = {"pool", "refills", "pages freed", "size"};
870 	if (!_manager) {
871 		seq_printf(m, "No pool allocator running.\n");
872 		return 0;
873 	}
874 	seq_printf(m, "%6s %12s %13s %8s\n",
875 			h[0], h[1], h[2], h[3]);
876 	for (i = 0; i < NUM_POOLS; ++i) {
877 		p = &_manager->pools[i];
878 
879 		seq_printf(m, "%6s %12ld %13ld %8d\n",
880 				p->name, p->nrefills,
881 				p->nfrees, p->npages);
882 	}
883 	return 0;
884 }
885 #endif
886