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