1592ffb21SWarner Losh /*
2592ffb21SWarner Losh * Copyright (c) Red Hat Inc.
3592ffb21SWarner Losh
4592ffb21SWarner Losh * Permission is hereby granted, free of charge, to any person obtaining a
5592ffb21SWarner Losh * copy of this software and associated documentation files (the "Software"),
6592ffb21SWarner Losh * to deal in the Software without restriction, including without limitation
7592ffb21SWarner Losh * the rights to use, copy, modify, merge, publish, distribute, sub license,
8592ffb21SWarner Losh * and/or sell copies of the Software, and to permit persons to whom the
9592ffb21SWarner Losh * Software is furnished to do so, subject to the following conditions:
10592ffb21SWarner Losh *
11592ffb21SWarner Losh * The above copyright notice and this permission notice (including the
12592ffb21SWarner Losh * next paragraph) shall be included in all copies or substantial portions
13592ffb21SWarner Losh * of the Software.
14592ffb21SWarner Losh *
15592ffb21SWarner Losh * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16592ffb21SWarner Losh * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17592ffb21SWarner Losh * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18592ffb21SWarner Losh * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19592ffb21SWarner Losh * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20592ffb21SWarner Losh * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21592ffb21SWarner Losh * DEALINGS IN THE SOFTWARE.
22592ffb21SWarner Losh *
23592ffb21SWarner Losh * Authors: Dave Airlie <airlied@redhat.com>
24592ffb21SWarner Losh * Jerome Glisse <jglisse@redhat.com>
25592ffb21SWarner Losh * Pauli Nieminen <suokkos@gmail.com>
26592ffb21SWarner Losh */
27592ffb21SWarner Losh /*
28592ffb21SWarner Losh * Copyright (c) 2013 The FreeBSD Foundation
29592ffb21SWarner Losh * All rights reserved.
30592ffb21SWarner Losh *
31592ffb21SWarner Losh * Portions of this software were developed by Konstantin Belousov
32592ffb21SWarner Losh * <kib@FreeBSD.org> under sponsorship from the FreeBSD Foundation.
33592ffb21SWarner Losh */
34592ffb21SWarner Losh
35592ffb21SWarner Losh /* simple list based uncached page pool
36592ffb21SWarner Losh * - Pool collects resently freed pages for reuse
37592ffb21SWarner Losh * - Use page->lru to keep a free list
38592ffb21SWarner Losh * - doesn't track currently in use pages
39592ffb21SWarner Losh */
40592ffb21SWarner Losh
41592ffb21SWarner Losh #include <sys/cdefs.h>
42592ffb21SWarner Losh #include <dev/drm2/drmP.h>
43592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_bo_driver.h>
44592ffb21SWarner Losh #include <dev/drm2/ttm/ttm_page_alloc.h>
45e12be321SConrad Meyer #include <sys/eventhandler.h>
46592ffb21SWarner Losh #include <vm/vm_pageout.h>
47592ffb21SWarner Losh
48592ffb21SWarner Losh #define NUM_PAGES_TO_ALLOC (PAGE_SIZE/sizeof(vm_page_t))
49592ffb21SWarner Losh #define SMALL_ALLOCATION 16
50592ffb21SWarner Losh #define FREE_ALL_PAGES (~0U)
51592ffb21SWarner Losh /* times are in msecs */
52592ffb21SWarner Losh #define PAGE_FREE_INTERVAL 1000
53592ffb21SWarner Losh
54592ffb21SWarner Losh /**
55592ffb21SWarner Losh * struct ttm_page_pool - Pool to reuse recently allocated uc/wc pages.
56592ffb21SWarner Losh *
57592ffb21SWarner Losh * @lock: Protects the shared pool from concurrnet access. Must be used with
58592ffb21SWarner Losh * irqsave/irqrestore variants because pool allocator maybe called from
59592ffb21SWarner Losh * delayed work.
60592ffb21SWarner Losh * @fill_lock: Prevent concurrent calls to fill.
61592ffb21SWarner Losh * @list: Pool of free uc/wc pages for fast reuse.
62592ffb21SWarner Losh * @gfp_flags: Flags to pass for alloc_page.
63592ffb21SWarner Losh * @npages: Number of pages in pool.
64592ffb21SWarner Losh */
65592ffb21SWarner Losh struct ttm_page_pool {
66592ffb21SWarner Losh struct mtx lock;
67592ffb21SWarner Losh bool fill_lock;
68592ffb21SWarner Losh bool dma32;
69592ffb21SWarner Losh struct pglist list;
70592ffb21SWarner Losh int ttm_page_alloc_flags;
71592ffb21SWarner Losh unsigned npages;
72592ffb21SWarner Losh char *name;
73592ffb21SWarner Losh unsigned long nfrees;
74592ffb21SWarner Losh unsigned long nrefills;
75592ffb21SWarner Losh };
76592ffb21SWarner Losh
77592ffb21SWarner Losh /**
78592ffb21SWarner Losh * Limits for the pool. They are handled without locks because only place where
79592ffb21SWarner Losh * they may change is in sysfs store. They won't have immediate effect anyway
80592ffb21SWarner Losh * so forcing serialization to access them is pointless.
81592ffb21SWarner Losh */
82592ffb21SWarner Losh
83592ffb21SWarner Losh struct ttm_pool_opts {
84592ffb21SWarner Losh unsigned alloc_size;
85592ffb21SWarner Losh unsigned max_size;
86592ffb21SWarner Losh unsigned small;
87592ffb21SWarner Losh };
88592ffb21SWarner Losh
89592ffb21SWarner Losh #define NUM_POOLS 4
90592ffb21SWarner Losh
91592ffb21SWarner Losh /**
92592ffb21SWarner Losh * struct ttm_pool_manager - Holds memory pools for fst allocation
93592ffb21SWarner Losh *
94592ffb21SWarner Losh * Manager is read only object for pool code so it doesn't need locking.
95592ffb21SWarner Losh *
96592ffb21SWarner Losh * @free_interval: minimum number of jiffies between freeing pages from pool.
97592ffb21SWarner Losh * @page_alloc_inited: reference counting for pool allocation.
98592ffb21SWarner Losh * @work: Work that is used to shrink the pool. Work is only run when there is
99592ffb21SWarner Losh * some pages to free.
100592ffb21SWarner Losh * @small_allocation: Limit in number of pages what is small allocation.
101592ffb21SWarner Losh *
102592ffb21SWarner Losh * @pools: All pool objects in use.
103592ffb21SWarner Losh **/
104592ffb21SWarner Losh struct ttm_pool_manager {
105592ffb21SWarner Losh unsigned int kobj_ref;
106592ffb21SWarner Losh eventhandler_tag lowmem_handler;
107592ffb21SWarner Losh struct ttm_pool_opts options;
108592ffb21SWarner Losh
109592ffb21SWarner Losh union {
110592ffb21SWarner Losh struct ttm_page_pool u_pools[NUM_POOLS];
111592ffb21SWarner Losh struct _utag {
112592ffb21SWarner Losh struct ttm_page_pool u_wc_pool;
113592ffb21SWarner Losh struct ttm_page_pool u_uc_pool;
114592ffb21SWarner Losh struct ttm_page_pool u_wc_pool_dma32;
115592ffb21SWarner Losh struct ttm_page_pool u_uc_pool_dma32;
116592ffb21SWarner Losh } _ut;
117592ffb21SWarner Losh } _u;
118592ffb21SWarner Losh };
119592ffb21SWarner Losh
120592ffb21SWarner Losh #define pools _u.u_pools
121592ffb21SWarner Losh #define wc_pool _u._ut.u_wc_pool
122592ffb21SWarner Losh #define uc_pool _u._ut.u_uc_pool
123592ffb21SWarner Losh #define wc_pool_dma32 _u._ut.u_wc_pool_dma32
124592ffb21SWarner Losh #define uc_pool_dma32 _u._ut.u_uc_pool_dma32
125592ffb21SWarner Losh
126592ffb21SWarner Losh MALLOC_DEFINE(M_TTM_POOLMGR, "ttm_poolmgr", "TTM Pool Manager");
127592ffb21SWarner Losh
128592ffb21SWarner Losh static void
ttm_vm_page_free(vm_page_t m)129592ffb21SWarner Losh ttm_vm_page_free(vm_page_t m)
130592ffb21SWarner Losh {
131592ffb21SWarner Losh
132592ffb21SWarner Losh KASSERT(m->object == NULL, ("ttm page %p is owned", m));
133fee2a2faSMark Johnston KASSERT(vm_page_wired(m), ("ttm lost wire %p", m));
134592ffb21SWarner Losh KASSERT((m->flags & PG_FICTITIOUS) != 0, ("ttm lost fictitious %p", m));
135592ffb21SWarner Losh KASSERT((m->oflags & VPO_UNMANAGED) == 0, ("ttm got unmanaged %p", m));
136592ffb21SWarner Losh m->flags &= ~PG_FICTITIOUS;
137592ffb21SWarner Losh m->oflags |= VPO_UNMANAGED;
13888ea538aSMark Johnston vm_page_unwire_noq(m);
139592ffb21SWarner Losh vm_page_free(m);
140592ffb21SWarner Losh }
141592ffb21SWarner Losh
142592ffb21SWarner Losh static vm_memattr_t
ttm_caching_state_to_vm(enum ttm_caching_state cstate)143592ffb21SWarner Losh ttm_caching_state_to_vm(enum ttm_caching_state cstate)
144592ffb21SWarner Losh {
145592ffb21SWarner Losh
146592ffb21SWarner Losh switch (cstate) {
147592ffb21SWarner Losh case tt_uncached:
148592ffb21SWarner Losh return (VM_MEMATTR_UNCACHEABLE);
149592ffb21SWarner Losh case tt_wc:
150592ffb21SWarner Losh return (VM_MEMATTR_WRITE_COMBINING);
151592ffb21SWarner Losh case tt_cached:
152592ffb21SWarner Losh return (VM_MEMATTR_WRITE_BACK);
153592ffb21SWarner Losh }
154592ffb21SWarner Losh panic("caching state %d\n", cstate);
155592ffb21SWarner Losh }
156592ffb21SWarner Losh
157592ffb21SWarner Losh static vm_page_t
ttm_vm_page_alloc_dma32(int req,vm_memattr_t memattr)158592ffb21SWarner Losh ttm_vm_page_alloc_dma32(int req, vm_memattr_t memattr)
159592ffb21SWarner Losh {
160592ffb21SWarner Losh vm_page_t p;
161*2619c5ccSJason A. Harmening int err, tries;
162592ffb21SWarner Losh
163592ffb21SWarner Losh for (tries = 0; ; tries++) {
16484c39222SMark Johnston p = vm_page_alloc_noobj_contig(req, 1, 0, 0xffffffff, PAGE_SIZE,
16584c39222SMark Johnston 0, memattr);
166592ffb21SWarner Losh if (p != NULL || tries > 2)
167592ffb21SWarner Losh return (p);
168*2619c5ccSJason A. Harmening err = vm_page_reclaim_contig(req, 1, 0, 0xffffffff,
169*2619c5ccSJason A. Harmening PAGE_SIZE, 0);
170*2619c5ccSJason A. Harmening if (err == ENOMEM)
171592ffb21SWarner Losh vm_wait(NULL);
172*2619c5ccSJason A. Harmening else if (err != 0)
173*2619c5ccSJason A. Harmening return (NULL);
174592ffb21SWarner Losh }
175592ffb21SWarner Losh }
176592ffb21SWarner Losh
177592ffb21SWarner Losh static vm_page_t
ttm_vm_page_alloc_any(int req,vm_memattr_t memattr)178592ffb21SWarner Losh ttm_vm_page_alloc_any(int req, vm_memattr_t memattr)
179592ffb21SWarner Losh {
180592ffb21SWarner Losh vm_page_t p;
181592ffb21SWarner Losh
182a4667e09SMark Johnston p = vm_page_alloc_noobj(req | VM_ALLOC_WAITOK);
183592ffb21SWarner Losh pmap_page_set_memattr(p, memattr);
184592ffb21SWarner Losh return (p);
185592ffb21SWarner Losh }
186592ffb21SWarner Losh
187592ffb21SWarner Losh static vm_page_t
ttm_vm_page_alloc(int flags,enum ttm_caching_state cstate)188592ffb21SWarner Losh ttm_vm_page_alloc(int flags, enum ttm_caching_state cstate)
189592ffb21SWarner Losh {
190592ffb21SWarner Losh vm_page_t p;
191592ffb21SWarner Losh vm_memattr_t memattr;
192592ffb21SWarner Losh int req;
193592ffb21SWarner Losh
194592ffb21SWarner Losh memattr = ttm_caching_state_to_vm(cstate);
19584c39222SMark Johnston req = VM_ALLOC_WIRED;
196592ffb21SWarner Losh if ((flags & TTM_PAGE_FLAG_ZERO_ALLOC) != 0)
197592ffb21SWarner Losh req |= VM_ALLOC_ZERO;
198592ffb21SWarner Losh
199592ffb21SWarner Losh if ((flags & TTM_PAGE_FLAG_DMA32) != 0)
200592ffb21SWarner Losh p = ttm_vm_page_alloc_dma32(req, memattr);
201592ffb21SWarner Losh else
202592ffb21SWarner Losh p = ttm_vm_page_alloc_any(req, memattr);
203592ffb21SWarner Losh
204592ffb21SWarner Losh if (p != NULL) {
205592ffb21SWarner Losh p->oflags &= ~VPO_UNMANAGED;
206592ffb21SWarner Losh p->flags |= PG_FICTITIOUS;
207592ffb21SWarner Losh }
208592ffb21SWarner Losh return (p);
209592ffb21SWarner Losh }
210592ffb21SWarner Losh
ttm_pool_kobj_release(struct ttm_pool_manager * m)211592ffb21SWarner Losh static void ttm_pool_kobj_release(struct ttm_pool_manager *m)
212592ffb21SWarner Losh {
213592ffb21SWarner Losh
214592ffb21SWarner Losh free(m, M_TTM_POOLMGR);
215592ffb21SWarner Losh }
216592ffb21SWarner Losh
217592ffb21SWarner Losh #if 0
218592ffb21SWarner Losh /* XXXKIB sysctl */
219592ffb21SWarner Losh static ssize_t ttm_pool_store(struct ttm_pool_manager *m,
220592ffb21SWarner Losh struct attribute *attr, const char *buffer, size_t size)
221592ffb21SWarner Losh {
222592ffb21SWarner Losh int chars;
223592ffb21SWarner Losh unsigned val;
224592ffb21SWarner Losh chars = sscanf(buffer, "%u", &val);
225592ffb21SWarner Losh if (chars == 0)
226592ffb21SWarner Losh return size;
227592ffb21SWarner Losh
228592ffb21SWarner Losh /* Convert kb to number of pages */
229592ffb21SWarner Losh val = val / (PAGE_SIZE >> 10);
230592ffb21SWarner Losh
231592ffb21SWarner Losh if (attr == &ttm_page_pool_max)
232592ffb21SWarner Losh m->options.max_size = val;
233592ffb21SWarner Losh else if (attr == &ttm_page_pool_small)
234592ffb21SWarner Losh m->options.small = val;
235592ffb21SWarner Losh else if (attr == &ttm_page_pool_alloc_size) {
236592ffb21SWarner Losh if (val > NUM_PAGES_TO_ALLOC*8) {
237592ffb21SWarner Losh pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
238592ffb21SWarner Losh NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
239592ffb21SWarner Losh NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
240592ffb21SWarner Losh return size;
241592ffb21SWarner Losh } else if (val > NUM_PAGES_TO_ALLOC) {
242592ffb21SWarner Losh pr_warn("Setting allocation size to larger than %lu is not recommended\n",
243592ffb21SWarner Losh NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
244592ffb21SWarner Losh }
245592ffb21SWarner Losh m->options.alloc_size = val;
246592ffb21SWarner Losh }
247592ffb21SWarner Losh
248592ffb21SWarner Losh return size;
249592ffb21SWarner Losh }
250592ffb21SWarner Losh
251592ffb21SWarner Losh static ssize_t ttm_pool_show(struct ttm_pool_manager *m,
252592ffb21SWarner Losh struct attribute *attr, char *buffer)
253592ffb21SWarner Losh {
254592ffb21SWarner Losh unsigned val = 0;
255592ffb21SWarner Losh
256592ffb21SWarner Losh if (attr == &ttm_page_pool_max)
257592ffb21SWarner Losh val = m->options.max_size;
258592ffb21SWarner Losh else if (attr == &ttm_page_pool_small)
259592ffb21SWarner Losh val = m->options.small;
260592ffb21SWarner Losh else if (attr == &ttm_page_pool_alloc_size)
261592ffb21SWarner Losh val = m->options.alloc_size;
262592ffb21SWarner Losh
263592ffb21SWarner Losh val = val * (PAGE_SIZE >> 10);
264592ffb21SWarner Losh
265592ffb21SWarner Losh return snprintf(buffer, PAGE_SIZE, "%u\n", val);
266592ffb21SWarner Losh }
267592ffb21SWarner Losh #endif
268592ffb21SWarner Losh
269592ffb21SWarner Losh static struct ttm_pool_manager *_manager;
270592ffb21SWarner Losh
set_pages_array_wb(vm_page_t * pages,int addrinarray)271592ffb21SWarner Losh static int set_pages_array_wb(vm_page_t *pages, int addrinarray)
272592ffb21SWarner Losh {
273592ffb21SWarner Losh #ifdef TTM_HAS_AGP
274592ffb21SWarner Losh int i;
275592ffb21SWarner Losh
276592ffb21SWarner Losh for (i = 0; i < addrinarray; i++)
277592ffb21SWarner Losh pmap_page_set_memattr(pages[i], VM_MEMATTR_WRITE_BACK);
278592ffb21SWarner Losh #endif
279592ffb21SWarner Losh return 0;
280592ffb21SWarner Losh }
281592ffb21SWarner Losh
set_pages_array_wc(vm_page_t * pages,int addrinarray)282592ffb21SWarner Losh static int set_pages_array_wc(vm_page_t *pages, int addrinarray)
283592ffb21SWarner Losh {
284592ffb21SWarner Losh #ifdef TTM_HAS_AGP
285592ffb21SWarner Losh int i;
286592ffb21SWarner Losh
287592ffb21SWarner Losh for (i = 0; i < addrinarray; i++)
288592ffb21SWarner Losh pmap_page_set_memattr(pages[i], VM_MEMATTR_WRITE_COMBINING);
289592ffb21SWarner Losh #endif
290592ffb21SWarner Losh return 0;
291592ffb21SWarner Losh }
292592ffb21SWarner Losh
set_pages_array_uc(vm_page_t * pages,int addrinarray)293592ffb21SWarner Losh static int set_pages_array_uc(vm_page_t *pages, int addrinarray)
294592ffb21SWarner Losh {
295592ffb21SWarner Losh #ifdef TTM_HAS_AGP
296592ffb21SWarner Losh int i;
297592ffb21SWarner Losh
298592ffb21SWarner Losh for (i = 0; i < addrinarray; i++)
299592ffb21SWarner Losh pmap_page_set_memattr(pages[i], VM_MEMATTR_UNCACHEABLE);
300592ffb21SWarner Losh #endif
301592ffb21SWarner Losh return 0;
302592ffb21SWarner Losh }
303592ffb21SWarner Losh
304592ffb21SWarner Losh /**
305592ffb21SWarner Losh * Select the right pool or requested caching state and ttm flags. */
ttm_get_pool(int flags,enum ttm_caching_state cstate)306592ffb21SWarner Losh static struct ttm_page_pool *ttm_get_pool(int flags,
307592ffb21SWarner Losh enum ttm_caching_state cstate)
308592ffb21SWarner Losh {
309592ffb21SWarner Losh int pool_index;
310592ffb21SWarner Losh
311592ffb21SWarner Losh if (cstate == tt_cached)
312592ffb21SWarner Losh return NULL;
313592ffb21SWarner Losh
314592ffb21SWarner Losh if (cstate == tt_wc)
315592ffb21SWarner Losh pool_index = 0x0;
316592ffb21SWarner Losh else
317592ffb21SWarner Losh pool_index = 0x1;
318592ffb21SWarner Losh
319592ffb21SWarner Losh if (flags & TTM_PAGE_FLAG_DMA32)
320592ffb21SWarner Losh pool_index |= 0x2;
321592ffb21SWarner Losh
322592ffb21SWarner Losh return &_manager->pools[pool_index];
323592ffb21SWarner Losh }
324592ffb21SWarner Losh
325592ffb21SWarner Losh /* set memory back to wb and free the pages. */
ttm_pages_put(vm_page_t * pages,unsigned npages)326592ffb21SWarner Losh static void ttm_pages_put(vm_page_t *pages, unsigned npages)
327592ffb21SWarner Losh {
328592ffb21SWarner Losh unsigned i;
329592ffb21SWarner Losh
330592ffb21SWarner Losh /* Our VM handles vm memattr automatically on the page free. */
331592ffb21SWarner Losh if (set_pages_array_wb(pages, npages))
332592ffb21SWarner Losh printf("[TTM] Failed to set %d pages to wb!\n", npages);
333592ffb21SWarner Losh for (i = 0; i < npages; ++i)
334592ffb21SWarner Losh ttm_vm_page_free(pages[i]);
335592ffb21SWarner Losh }
336592ffb21SWarner Losh
ttm_pool_update_free_locked(struct ttm_page_pool * pool,unsigned freed_pages)337592ffb21SWarner Losh static void ttm_pool_update_free_locked(struct ttm_page_pool *pool,
338592ffb21SWarner Losh unsigned freed_pages)
339592ffb21SWarner Losh {
340592ffb21SWarner Losh pool->npages -= freed_pages;
341592ffb21SWarner Losh pool->nfrees += freed_pages;
342592ffb21SWarner Losh }
343592ffb21SWarner Losh
344592ffb21SWarner Losh /**
345592ffb21SWarner Losh * Free pages from pool.
346592ffb21SWarner Losh *
347592ffb21SWarner Losh * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
348592ffb21SWarner Losh * number of pages in one go.
349592ffb21SWarner Losh *
350592ffb21SWarner Losh * @pool: to free the pages from
351592ffb21SWarner Losh * @free_all: If set to true will free all pages in pool
352592ffb21SWarner Losh **/
ttm_page_pool_free(struct ttm_page_pool * pool,unsigned nr_free)353592ffb21SWarner Losh static int ttm_page_pool_free(struct ttm_page_pool *pool, unsigned nr_free)
354592ffb21SWarner Losh {
355592ffb21SWarner Losh vm_page_t p, p1;
356592ffb21SWarner Losh vm_page_t *pages_to_free;
357592ffb21SWarner Losh unsigned freed_pages = 0,
358592ffb21SWarner Losh npages_to_free = nr_free;
359592ffb21SWarner Losh unsigned i;
360592ffb21SWarner Losh
361592ffb21SWarner Losh if (NUM_PAGES_TO_ALLOC < nr_free)
362592ffb21SWarner Losh npages_to_free = NUM_PAGES_TO_ALLOC;
363592ffb21SWarner Losh
364592ffb21SWarner Losh pages_to_free = malloc(npages_to_free * sizeof(vm_page_t),
365592ffb21SWarner Losh M_TEMP, M_WAITOK | M_ZERO);
366592ffb21SWarner Losh
367592ffb21SWarner Losh restart:
368592ffb21SWarner Losh mtx_lock(&pool->lock);
369592ffb21SWarner Losh
370592ffb21SWarner Losh TAILQ_FOREACH_REVERSE_SAFE(p, &pool->list, pglist, plinks.q, p1) {
371592ffb21SWarner Losh if (freed_pages >= npages_to_free)
372592ffb21SWarner Losh break;
373592ffb21SWarner Losh
374592ffb21SWarner Losh pages_to_free[freed_pages++] = p;
375592ffb21SWarner Losh /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
376592ffb21SWarner Losh if (freed_pages >= NUM_PAGES_TO_ALLOC) {
377592ffb21SWarner Losh /* remove range of pages from the pool */
378592ffb21SWarner Losh for (i = 0; i < freed_pages; i++)
379592ffb21SWarner Losh TAILQ_REMOVE(&pool->list, pages_to_free[i], plinks.q);
380592ffb21SWarner Losh
381592ffb21SWarner Losh ttm_pool_update_free_locked(pool, freed_pages);
382592ffb21SWarner Losh /**
383592ffb21SWarner Losh * Because changing page caching is costly
384592ffb21SWarner Losh * we unlock the pool to prevent stalling.
385592ffb21SWarner Losh */
386592ffb21SWarner Losh mtx_unlock(&pool->lock);
387592ffb21SWarner Losh
388592ffb21SWarner Losh ttm_pages_put(pages_to_free, freed_pages);
389592ffb21SWarner Losh if (likely(nr_free != FREE_ALL_PAGES))
390592ffb21SWarner Losh nr_free -= freed_pages;
391592ffb21SWarner Losh
392592ffb21SWarner Losh if (NUM_PAGES_TO_ALLOC >= nr_free)
393592ffb21SWarner Losh npages_to_free = nr_free;
394592ffb21SWarner Losh else
395592ffb21SWarner Losh npages_to_free = NUM_PAGES_TO_ALLOC;
396592ffb21SWarner Losh
397592ffb21SWarner Losh freed_pages = 0;
398592ffb21SWarner Losh
399592ffb21SWarner Losh /* free all so restart the processing */
400592ffb21SWarner Losh if (nr_free)
401592ffb21SWarner Losh goto restart;
402592ffb21SWarner Losh
403592ffb21SWarner Losh /* Not allowed to fall through or break because
404592ffb21SWarner Losh * following context is inside spinlock while we are
405592ffb21SWarner Losh * outside here.
406592ffb21SWarner Losh */
407592ffb21SWarner Losh goto out;
408592ffb21SWarner Losh
409592ffb21SWarner Losh }
410592ffb21SWarner Losh }
411592ffb21SWarner Losh
412592ffb21SWarner Losh /* remove range of pages from the pool */
413592ffb21SWarner Losh if (freed_pages) {
414592ffb21SWarner Losh for (i = 0; i < freed_pages; i++)
415592ffb21SWarner Losh TAILQ_REMOVE(&pool->list, pages_to_free[i], plinks.q);
416592ffb21SWarner Losh
417592ffb21SWarner Losh ttm_pool_update_free_locked(pool, freed_pages);
418592ffb21SWarner Losh nr_free -= freed_pages;
419592ffb21SWarner Losh }
420592ffb21SWarner Losh
421592ffb21SWarner Losh mtx_unlock(&pool->lock);
422592ffb21SWarner Losh
423592ffb21SWarner Losh if (freed_pages)
424592ffb21SWarner Losh ttm_pages_put(pages_to_free, freed_pages);
425592ffb21SWarner Losh out:
426592ffb21SWarner Losh free(pages_to_free, M_TEMP);
427592ffb21SWarner Losh return nr_free;
428592ffb21SWarner Losh }
429592ffb21SWarner Losh
430592ffb21SWarner Losh /* Get good estimation how many pages are free in pools */
ttm_pool_get_num_unused_pages(void)431592ffb21SWarner Losh static int ttm_pool_get_num_unused_pages(void)
432592ffb21SWarner Losh {
433592ffb21SWarner Losh unsigned i;
434592ffb21SWarner Losh int total = 0;
435592ffb21SWarner Losh for (i = 0; i < NUM_POOLS; ++i)
436592ffb21SWarner Losh total += _manager->pools[i].npages;
437592ffb21SWarner Losh
438592ffb21SWarner Losh return total;
439592ffb21SWarner Losh }
440592ffb21SWarner Losh
441592ffb21SWarner Losh /**
442592ffb21SWarner Losh * Callback for mm to request pool to reduce number of page held.
443592ffb21SWarner Losh */
ttm_pool_mm_shrink(void * arg)444592ffb21SWarner Losh static int ttm_pool_mm_shrink(void *arg)
445592ffb21SWarner Losh {
446592ffb21SWarner Losh static unsigned int start_pool = 0;
447592ffb21SWarner Losh unsigned i;
448592ffb21SWarner Losh unsigned pool_offset = atomic_fetchadd_int(&start_pool, 1);
449592ffb21SWarner Losh struct ttm_page_pool *pool;
450592ffb21SWarner Losh int shrink_pages = 100; /* XXXKIB */
451592ffb21SWarner Losh
452592ffb21SWarner Losh pool_offset = pool_offset % NUM_POOLS;
453592ffb21SWarner Losh /* select start pool in round robin fashion */
454592ffb21SWarner Losh for (i = 0; i < NUM_POOLS; ++i) {
455592ffb21SWarner Losh unsigned nr_free = shrink_pages;
456592ffb21SWarner Losh if (shrink_pages == 0)
457592ffb21SWarner Losh break;
458592ffb21SWarner Losh pool = &_manager->pools[(i + pool_offset)%NUM_POOLS];
459592ffb21SWarner Losh shrink_pages = ttm_page_pool_free(pool, nr_free);
460592ffb21SWarner Losh }
461592ffb21SWarner Losh /* return estimated number of unused pages in pool */
462592ffb21SWarner Losh return ttm_pool_get_num_unused_pages();
463592ffb21SWarner Losh }
464592ffb21SWarner Losh
ttm_pool_mm_shrink_init(struct ttm_pool_manager * manager)465592ffb21SWarner Losh static void ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
466592ffb21SWarner Losh {
467592ffb21SWarner Losh
468592ffb21SWarner Losh manager->lowmem_handler = EVENTHANDLER_REGISTER(vm_lowmem,
469592ffb21SWarner Losh ttm_pool_mm_shrink, manager, EVENTHANDLER_PRI_ANY);
470592ffb21SWarner Losh }
471592ffb21SWarner Losh
ttm_pool_mm_shrink_fini(struct ttm_pool_manager * manager)472592ffb21SWarner Losh static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
473592ffb21SWarner Losh {
474592ffb21SWarner Losh
475592ffb21SWarner Losh EVENTHANDLER_DEREGISTER(vm_lowmem, manager->lowmem_handler);
476592ffb21SWarner Losh }
477592ffb21SWarner Losh
ttm_set_pages_caching(vm_page_t * pages,enum ttm_caching_state cstate,unsigned cpages)478592ffb21SWarner Losh static int ttm_set_pages_caching(vm_page_t *pages,
479592ffb21SWarner Losh enum ttm_caching_state cstate, unsigned cpages)
480592ffb21SWarner Losh {
481592ffb21SWarner Losh int r = 0;
482592ffb21SWarner Losh /* Set page caching */
483592ffb21SWarner Losh switch (cstate) {
484592ffb21SWarner Losh case tt_uncached:
485592ffb21SWarner Losh r = set_pages_array_uc(pages, cpages);
486592ffb21SWarner Losh if (r)
487592ffb21SWarner Losh printf("[TTM] Failed to set %d pages to uc!\n", cpages);
488592ffb21SWarner Losh break;
489592ffb21SWarner Losh case tt_wc:
490592ffb21SWarner Losh r = set_pages_array_wc(pages, cpages);
491592ffb21SWarner Losh if (r)
492592ffb21SWarner Losh printf("[TTM] Failed to set %d pages to wc!\n", cpages);
493592ffb21SWarner Losh break;
494592ffb21SWarner Losh default:
495592ffb21SWarner Losh break;
496592ffb21SWarner Losh }
497592ffb21SWarner Losh return r;
498592ffb21SWarner Losh }
499592ffb21SWarner Losh
500592ffb21SWarner Losh /**
501592ffb21SWarner Losh * Free pages the pages that failed to change the caching state. If there is
502592ffb21SWarner Losh * any pages that have changed their caching state already put them to the
503592ffb21SWarner Losh * pool.
504592ffb21SWarner Losh */
ttm_handle_caching_state_failure(struct pglist * pages,int ttm_flags,enum ttm_caching_state cstate,vm_page_t * failed_pages,unsigned cpages)505592ffb21SWarner Losh static void ttm_handle_caching_state_failure(struct pglist *pages,
506592ffb21SWarner Losh int ttm_flags, enum ttm_caching_state cstate,
507592ffb21SWarner Losh vm_page_t *failed_pages, unsigned cpages)
508592ffb21SWarner Losh {
509592ffb21SWarner Losh unsigned i;
510592ffb21SWarner Losh /* Failed pages have to be freed */
511592ffb21SWarner Losh for (i = 0; i < cpages; ++i) {
512592ffb21SWarner Losh TAILQ_REMOVE(pages, failed_pages[i], plinks.q);
513592ffb21SWarner Losh ttm_vm_page_free(failed_pages[i]);
514592ffb21SWarner Losh }
515592ffb21SWarner Losh }
516592ffb21SWarner Losh
517592ffb21SWarner Losh /**
518592ffb21SWarner Losh * Allocate new pages with correct caching.
519592ffb21SWarner Losh *
520592ffb21SWarner Losh * This function is reentrant if caller updates count depending on number of
521592ffb21SWarner Losh * pages returned in pages array.
522592ffb21SWarner Losh */
ttm_alloc_new_pages(struct pglist * pages,int ttm_alloc_flags,int ttm_flags,enum ttm_caching_state cstate,unsigned count)523592ffb21SWarner Losh static int ttm_alloc_new_pages(struct pglist *pages, int ttm_alloc_flags,
524592ffb21SWarner Losh int ttm_flags, enum ttm_caching_state cstate, unsigned count)
525592ffb21SWarner Losh {
526592ffb21SWarner Losh vm_page_t *caching_array;
527592ffb21SWarner Losh vm_page_t p;
528592ffb21SWarner Losh int r = 0;
529592ffb21SWarner Losh unsigned i, cpages;
530592ffb21SWarner Losh unsigned max_cpages = min(count,
531592ffb21SWarner Losh (unsigned)(PAGE_SIZE/sizeof(vm_page_t)));
532592ffb21SWarner Losh
533592ffb21SWarner Losh /* allocate array for page caching change */
534592ffb21SWarner Losh caching_array = malloc(max_cpages * sizeof(vm_page_t), M_TEMP,
535592ffb21SWarner Losh M_WAITOK | M_ZERO);
536592ffb21SWarner Losh
537592ffb21SWarner Losh for (i = 0, cpages = 0; i < count; ++i) {
538592ffb21SWarner Losh p = ttm_vm_page_alloc(ttm_alloc_flags, cstate);
539592ffb21SWarner Losh if (!p) {
540592ffb21SWarner Losh printf("[TTM] Unable to get page %u\n", i);
541592ffb21SWarner Losh
542592ffb21SWarner Losh /* store already allocated pages in the pool after
543592ffb21SWarner Losh * setting the caching state */
544592ffb21SWarner Losh if (cpages) {
545592ffb21SWarner Losh r = ttm_set_pages_caching(caching_array,
546592ffb21SWarner Losh cstate, cpages);
547592ffb21SWarner Losh if (r)
548592ffb21SWarner Losh ttm_handle_caching_state_failure(pages,
549592ffb21SWarner Losh ttm_flags, cstate,
550592ffb21SWarner Losh caching_array, cpages);
551592ffb21SWarner Losh }
552592ffb21SWarner Losh r = -ENOMEM;
553592ffb21SWarner Losh goto out;
554592ffb21SWarner Losh }
555592ffb21SWarner Losh
556592ffb21SWarner Losh #ifdef CONFIG_HIGHMEM /* KIB: nop */
557592ffb21SWarner Losh /* gfp flags of highmem page should never be dma32 so we
558592ffb21SWarner Losh * we should be fine in such case
559592ffb21SWarner Losh */
560592ffb21SWarner Losh if (!PageHighMem(p))
561592ffb21SWarner Losh #endif
562592ffb21SWarner Losh {
563592ffb21SWarner Losh caching_array[cpages++] = p;
564592ffb21SWarner Losh if (cpages == max_cpages) {
565592ffb21SWarner Losh
566592ffb21SWarner Losh r = ttm_set_pages_caching(caching_array,
567592ffb21SWarner Losh cstate, cpages);
568592ffb21SWarner Losh if (r) {
569592ffb21SWarner Losh ttm_handle_caching_state_failure(pages,
570592ffb21SWarner Losh ttm_flags, cstate,
571592ffb21SWarner Losh caching_array, cpages);
572592ffb21SWarner Losh goto out;
573592ffb21SWarner Losh }
574592ffb21SWarner Losh cpages = 0;
575592ffb21SWarner Losh }
576592ffb21SWarner Losh }
577592ffb21SWarner Losh
578592ffb21SWarner Losh TAILQ_INSERT_HEAD(pages, p, plinks.q);
579592ffb21SWarner Losh }
580592ffb21SWarner Losh
581592ffb21SWarner Losh if (cpages) {
582592ffb21SWarner Losh r = ttm_set_pages_caching(caching_array, cstate, cpages);
583592ffb21SWarner Losh if (r)
584592ffb21SWarner Losh ttm_handle_caching_state_failure(pages,
585592ffb21SWarner Losh ttm_flags, cstate,
586592ffb21SWarner Losh caching_array, cpages);
587592ffb21SWarner Losh }
588592ffb21SWarner Losh out:
589592ffb21SWarner Losh free(caching_array, M_TEMP);
590592ffb21SWarner Losh
591592ffb21SWarner Losh return r;
592592ffb21SWarner Losh }
593592ffb21SWarner Losh
594592ffb21SWarner Losh /**
595592ffb21SWarner Losh * Fill the given pool if there aren't enough pages and the requested number of
596592ffb21SWarner Losh * pages is small.
597592ffb21SWarner Losh */
ttm_page_pool_fill_locked(struct ttm_page_pool * pool,int ttm_flags,enum ttm_caching_state cstate,unsigned count)598592ffb21SWarner Losh static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool,
599592ffb21SWarner Losh int ttm_flags, enum ttm_caching_state cstate, unsigned count)
600592ffb21SWarner Losh {
601592ffb21SWarner Losh vm_page_t p;
602592ffb21SWarner Losh int r;
603592ffb21SWarner Losh unsigned cpages = 0;
604592ffb21SWarner Losh /**
605592ffb21SWarner Losh * Only allow one pool fill operation at a time.
606592ffb21SWarner Losh * If pool doesn't have enough pages for the allocation new pages are
607592ffb21SWarner Losh * allocated from outside of pool.
608592ffb21SWarner Losh */
609592ffb21SWarner Losh if (pool->fill_lock)
610592ffb21SWarner Losh return;
611592ffb21SWarner Losh
612592ffb21SWarner Losh pool->fill_lock = true;
613592ffb21SWarner Losh
614592ffb21SWarner Losh /* If allocation request is small and there are not enough
615592ffb21SWarner Losh * pages in a pool we fill the pool up first. */
616592ffb21SWarner Losh if (count < _manager->options.small
617592ffb21SWarner Losh && count > pool->npages) {
618592ffb21SWarner Losh struct pglist new_pages;
619592ffb21SWarner Losh unsigned alloc_size = _manager->options.alloc_size;
620592ffb21SWarner Losh
621592ffb21SWarner Losh /**
622592ffb21SWarner Losh * Can't change page caching if in irqsave context. We have to
623592ffb21SWarner Losh * drop the pool->lock.
624592ffb21SWarner Losh */
625592ffb21SWarner Losh mtx_unlock(&pool->lock);
626592ffb21SWarner Losh
627592ffb21SWarner Losh TAILQ_INIT(&new_pages);
628592ffb21SWarner Losh r = ttm_alloc_new_pages(&new_pages, pool->ttm_page_alloc_flags,
629592ffb21SWarner Losh ttm_flags, cstate, alloc_size);
630592ffb21SWarner Losh mtx_lock(&pool->lock);
631592ffb21SWarner Losh
632592ffb21SWarner Losh if (!r) {
633592ffb21SWarner Losh TAILQ_CONCAT(&pool->list, &new_pages, plinks.q);
634592ffb21SWarner Losh ++pool->nrefills;
635592ffb21SWarner Losh pool->npages += alloc_size;
636592ffb21SWarner Losh } else {
637592ffb21SWarner Losh printf("[TTM] Failed to fill pool (%p)\n", pool);
638592ffb21SWarner Losh /* If we have any pages left put them to the pool. */
639592ffb21SWarner Losh TAILQ_FOREACH(p, &pool->list, plinks.q) {
640592ffb21SWarner Losh ++cpages;
641592ffb21SWarner Losh }
642592ffb21SWarner Losh TAILQ_CONCAT(&pool->list, &new_pages, plinks.q);
643592ffb21SWarner Losh pool->npages += cpages;
644592ffb21SWarner Losh }
645592ffb21SWarner Losh
646592ffb21SWarner Losh }
647592ffb21SWarner Losh pool->fill_lock = false;
648592ffb21SWarner Losh }
649592ffb21SWarner Losh
650592ffb21SWarner Losh /**
651592ffb21SWarner Losh * Cut 'count' number of pages from the pool and put them on the return list.
652592ffb21SWarner Losh *
653592ffb21SWarner Losh * @return count of pages still required to fulfill the request.
654592ffb21SWarner Losh */
ttm_page_pool_get_pages(struct ttm_page_pool * pool,struct pglist * pages,int ttm_flags,enum ttm_caching_state cstate,unsigned count)655592ffb21SWarner Losh static unsigned ttm_page_pool_get_pages(struct ttm_page_pool *pool,
656592ffb21SWarner Losh struct pglist *pages,
657592ffb21SWarner Losh int ttm_flags,
658592ffb21SWarner Losh enum ttm_caching_state cstate,
659592ffb21SWarner Losh unsigned count)
660592ffb21SWarner Losh {
661592ffb21SWarner Losh vm_page_t p;
662592ffb21SWarner Losh unsigned i;
663592ffb21SWarner Losh
664592ffb21SWarner Losh mtx_lock(&pool->lock);
665592ffb21SWarner Losh ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count);
666592ffb21SWarner Losh
667592ffb21SWarner Losh if (count >= pool->npages) {
668592ffb21SWarner Losh /* take all pages from the pool */
669592ffb21SWarner Losh TAILQ_CONCAT(pages, &pool->list, plinks.q);
670592ffb21SWarner Losh count -= pool->npages;
671592ffb21SWarner Losh pool->npages = 0;
672592ffb21SWarner Losh goto out;
673592ffb21SWarner Losh }
674592ffb21SWarner Losh for (i = 0; i < count; i++) {
675592ffb21SWarner Losh p = TAILQ_FIRST(&pool->list);
676592ffb21SWarner Losh TAILQ_REMOVE(&pool->list, p, plinks.q);
677592ffb21SWarner Losh TAILQ_INSERT_TAIL(pages, p, plinks.q);
678592ffb21SWarner Losh }
679592ffb21SWarner Losh pool->npages -= count;
680592ffb21SWarner Losh count = 0;
681592ffb21SWarner Losh out:
682592ffb21SWarner Losh mtx_unlock(&pool->lock);
683592ffb21SWarner Losh return count;
684592ffb21SWarner Losh }
685592ffb21SWarner Losh
686592ffb21SWarner Losh /* Put all pages in pages list to correct pool to wait for reuse */
ttm_put_pages(vm_page_t * pages,unsigned npages,int flags,enum ttm_caching_state cstate)687592ffb21SWarner Losh static void ttm_put_pages(vm_page_t *pages, unsigned npages, int flags,
688592ffb21SWarner Losh enum ttm_caching_state cstate)
689592ffb21SWarner Losh {
690592ffb21SWarner Losh struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
691592ffb21SWarner Losh unsigned i;
692592ffb21SWarner Losh
693592ffb21SWarner Losh if (pool == NULL) {
694592ffb21SWarner Losh /* No pool for this memory type so free the pages */
695592ffb21SWarner Losh for (i = 0; i < npages; i++) {
696592ffb21SWarner Losh if (pages[i]) {
697592ffb21SWarner Losh ttm_vm_page_free(pages[i]);
698592ffb21SWarner Losh pages[i] = NULL;
699592ffb21SWarner Losh }
700592ffb21SWarner Losh }
701592ffb21SWarner Losh return;
702592ffb21SWarner Losh }
703592ffb21SWarner Losh
704592ffb21SWarner Losh mtx_lock(&pool->lock);
705592ffb21SWarner Losh for (i = 0; i < npages; i++) {
706592ffb21SWarner Losh if (pages[i]) {
707592ffb21SWarner Losh TAILQ_INSERT_TAIL(&pool->list, pages[i], plinks.q);
708592ffb21SWarner Losh pages[i] = NULL;
709592ffb21SWarner Losh pool->npages++;
710592ffb21SWarner Losh }
711592ffb21SWarner Losh }
712592ffb21SWarner Losh /* Check that we don't go over the pool limit */
713592ffb21SWarner Losh npages = 0;
714592ffb21SWarner Losh if (pool->npages > _manager->options.max_size) {
715592ffb21SWarner Losh npages = pool->npages - _manager->options.max_size;
716592ffb21SWarner Losh /* free at least NUM_PAGES_TO_ALLOC number of pages
717592ffb21SWarner Losh * to reduce calls to set_memory_wb */
718592ffb21SWarner Losh if (npages < NUM_PAGES_TO_ALLOC)
719592ffb21SWarner Losh npages = NUM_PAGES_TO_ALLOC;
720592ffb21SWarner Losh }
721592ffb21SWarner Losh mtx_unlock(&pool->lock);
722592ffb21SWarner Losh if (npages)
723592ffb21SWarner Losh ttm_page_pool_free(pool, npages);
724592ffb21SWarner Losh }
725592ffb21SWarner Losh
726592ffb21SWarner Losh /*
727592ffb21SWarner Losh * On success pages list will hold count number of correctly
728592ffb21SWarner Losh * cached pages.
729592ffb21SWarner Losh */
ttm_get_pages(vm_page_t * pages,unsigned npages,int flags,enum ttm_caching_state cstate)730592ffb21SWarner Losh static int ttm_get_pages(vm_page_t *pages, unsigned npages, int flags,
731592ffb21SWarner Losh enum ttm_caching_state cstate)
732592ffb21SWarner Losh {
733592ffb21SWarner Losh struct ttm_page_pool *pool = ttm_get_pool(flags, cstate);
734592ffb21SWarner Losh struct pglist plist;
735592ffb21SWarner Losh vm_page_t p = NULL;
736592ffb21SWarner Losh int gfp_flags;
737592ffb21SWarner Losh unsigned count;
738592ffb21SWarner Losh int r;
739592ffb21SWarner Losh
740592ffb21SWarner Losh /* No pool for cached pages */
741592ffb21SWarner Losh if (pool == NULL) {
742592ffb21SWarner Losh for (r = 0; r < npages; ++r) {
743592ffb21SWarner Losh p = ttm_vm_page_alloc(flags, cstate);
744592ffb21SWarner Losh if (!p) {
745592ffb21SWarner Losh printf("[TTM] Unable to allocate page\n");
746592ffb21SWarner Losh return -ENOMEM;
747592ffb21SWarner Losh }
748592ffb21SWarner Losh pages[r] = p;
749592ffb21SWarner Losh }
750592ffb21SWarner Losh return 0;
751592ffb21SWarner Losh }
752592ffb21SWarner Losh
753592ffb21SWarner Losh /* combine zero flag to pool flags */
754592ffb21SWarner Losh gfp_flags = flags | pool->ttm_page_alloc_flags;
755592ffb21SWarner Losh
756592ffb21SWarner Losh /* First we take pages from the pool */
757592ffb21SWarner Losh TAILQ_INIT(&plist);
758592ffb21SWarner Losh npages = ttm_page_pool_get_pages(pool, &plist, flags, cstate, npages);
759592ffb21SWarner Losh count = 0;
760592ffb21SWarner Losh TAILQ_FOREACH(p, &plist, plinks.q) {
761592ffb21SWarner Losh pages[count++] = p;
762592ffb21SWarner Losh }
763592ffb21SWarner Losh
764592ffb21SWarner Losh /* clear the pages coming from the pool if requested */
765592ffb21SWarner Losh if (flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
766592ffb21SWarner Losh TAILQ_FOREACH(p, &plist, plinks.q) {
767592ffb21SWarner Losh pmap_zero_page(p);
768592ffb21SWarner Losh }
769592ffb21SWarner Losh }
770592ffb21SWarner Losh
771592ffb21SWarner Losh /* If pool didn't have enough pages allocate new one. */
772592ffb21SWarner Losh if (npages > 0) {
773592ffb21SWarner Losh /* ttm_alloc_new_pages doesn't reference pool so we can run
774592ffb21SWarner Losh * multiple requests in parallel.
775592ffb21SWarner Losh **/
776592ffb21SWarner Losh TAILQ_INIT(&plist);
777592ffb21SWarner Losh r = ttm_alloc_new_pages(&plist, gfp_flags, flags, cstate,
778592ffb21SWarner Losh npages);
779592ffb21SWarner Losh TAILQ_FOREACH(p, &plist, plinks.q) {
780592ffb21SWarner Losh pages[count++] = p;
781592ffb21SWarner Losh }
782592ffb21SWarner Losh if (r) {
783592ffb21SWarner Losh /* If there is any pages in the list put them back to
784592ffb21SWarner Losh * the pool. */
785592ffb21SWarner Losh printf("[TTM] Failed to allocate extra pages for large request\n");
786592ffb21SWarner Losh ttm_put_pages(pages, count, flags, cstate);
787592ffb21SWarner Losh return r;
788592ffb21SWarner Losh }
789592ffb21SWarner Losh }
790592ffb21SWarner Losh
791592ffb21SWarner Losh return 0;
792592ffb21SWarner Losh }
793592ffb21SWarner Losh
ttm_page_pool_init_locked(struct ttm_page_pool * pool,int flags,char * name)794592ffb21SWarner Losh static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, int flags,
795592ffb21SWarner Losh char *name)
796592ffb21SWarner Losh {
797592ffb21SWarner Losh mtx_init(&pool->lock, "ttmpool", NULL, MTX_DEF);
798592ffb21SWarner Losh pool->fill_lock = false;
799592ffb21SWarner Losh TAILQ_INIT(&pool->list);
800592ffb21SWarner Losh pool->npages = pool->nfrees = 0;
801592ffb21SWarner Losh pool->ttm_page_alloc_flags = flags;
802592ffb21SWarner Losh pool->name = name;
803592ffb21SWarner Losh }
804592ffb21SWarner Losh
ttm_page_alloc_init(struct ttm_mem_global * glob,unsigned max_pages)805592ffb21SWarner Losh int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
806592ffb21SWarner Losh {
807592ffb21SWarner Losh
808592ffb21SWarner Losh if (_manager != NULL)
809592ffb21SWarner Losh printf("[TTM] manager != NULL\n");
810592ffb21SWarner Losh printf("[TTM] Initializing pool allocator\n");
811592ffb21SWarner Losh
812592ffb21SWarner Losh _manager = malloc(sizeof(*_manager), M_TTM_POOLMGR, M_WAITOK | M_ZERO);
813592ffb21SWarner Losh
814592ffb21SWarner Losh ttm_page_pool_init_locked(&_manager->wc_pool, 0, "wc");
815592ffb21SWarner Losh ttm_page_pool_init_locked(&_manager->uc_pool, 0, "uc");
816592ffb21SWarner Losh ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
817592ffb21SWarner Losh TTM_PAGE_FLAG_DMA32, "wc dma");
818592ffb21SWarner Losh ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
819592ffb21SWarner Losh TTM_PAGE_FLAG_DMA32, "uc dma");
820592ffb21SWarner Losh
821592ffb21SWarner Losh _manager->options.max_size = max_pages;
822592ffb21SWarner Losh _manager->options.small = SMALL_ALLOCATION;
823592ffb21SWarner Losh _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
824592ffb21SWarner Losh
825592ffb21SWarner Losh refcount_init(&_manager->kobj_ref, 1);
826592ffb21SWarner Losh ttm_pool_mm_shrink_init(_manager);
827592ffb21SWarner Losh
828592ffb21SWarner Losh return 0;
829592ffb21SWarner Losh }
830592ffb21SWarner Losh
ttm_page_alloc_fini(void)831592ffb21SWarner Losh void ttm_page_alloc_fini(void)
832592ffb21SWarner Losh {
833592ffb21SWarner Losh int i;
834592ffb21SWarner Losh
835592ffb21SWarner Losh printf("[TTM] Finalizing pool allocator\n");
836592ffb21SWarner Losh ttm_pool_mm_shrink_fini(_manager);
837592ffb21SWarner Losh
838592ffb21SWarner Losh for (i = 0; i < NUM_POOLS; ++i)
839592ffb21SWarner Losh ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES);
840592ffb21SWarner Losh
841592ffb21SWarner Losh if (refcount_release(&_manager->kobj_ref))
842592ffb21SWarner Losh ttm_pool_kobj_release(_manager);
843592ffb21SWarner Losh _manager = NULL;
844592ffb21SWarner Losh }
845592ffb21SWarner Losh
ttm_pool_populate(struct ttm_tt * ttm)846592ffb21SWarner Losh int ttm_pool_populate(struct ttm_tt *ttm)
847592ffb21SWarner Losh {
848592ffb21SWarner Losh struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
849592ffb21SWarner Losh unsigned i;
850592ffb21SWarner Losh int ret;
851592ffb21SWarner Losh
852592ffb21SWarner Losh if (ttm->state != tt_unpopulated)
853592ffb21SWarner Losh return 0;
854592ffb21SWarner Losh
855592ffb21SWarner Losh for (i = 0; i < ttm->num_pages; ++i) {
856592ffb21SWarner Losh ret = ttm_get_pages(&ttm->pages[i], 1,
857592ffb21SWarner Losh ttm->page_flags,
858592ffb21SWarner Losh ttm->caching_state);
859592ffb21SWarner Losh if (ret != 0) {
860592ffb21SWarner Losh ttm_pool_unpopulate(ttm);
861592ffb21SWarner Losh return -ENOMEM;
862592ffb21SWarner Losh }
863592ffb21SWarner Losh
864592ffb21SWarner Losh ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
865592ffb21SWarner Losh false, false);
866592ffb21SWarner Losh if (unlikely(ret != 0)) {
867592ffb21SWarner Losh ttm_pool_unpopulate(ttm);
868592ffb21SWarner Losh return -ENOMEM;
869592ffb21SWarner Losh }
870592ffb21SWarner Losh }
871592ffb21SWarner Losh
872592ffb21SWarner Losh if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
873592ffb21SWarner Losh ret = ttm_tt_swapin(ttm);
874592ffb21SWarner Losh if (unlikely(ret != 0)) {
875592ffb21SWarner Losh ttm_pool_unpopulate(ttm);
876592ffb21SWarner Losh return ret;
877592ffb21SWarner Losh }
878592ffb21SWarner Losh }
879592ffb21SWarner Losh
880592ffb21SWarner Losh ttm->state = tt_unbound;
881592ffb21SWarner Losh return 0;
882592ffb21SWarner Losh }
883592ffb21SWarner Losh
ttm_pool_unpopulate(struct ttm_tt * ttm)884592ffb21SWarner Losh void ttm_pool_unpopulate(struct ttm_tt *ttm)
885592ffb21SWarner Losh {
886592ffb21SWarner Losh unsigned i;
887592ffb21SWarner Losh
888592ffb21SWarner Losh for (i = 0; i < ttm->num_pages; ++i) {
889592ffb21SWarner Losh if (ttm->pages[i]) {
890592ffb21SWarner Losh ttm_mem_global_free_page(ttm->glob->mem_glob,
891592ffb21SWarner Losh ttm->pages[i]);
892592ffb21SWarner Losh ttm_put_pages(&ttm->pages[i], 1,
893592ffb21SWarner Losh ttm->page_flags,
894592ffb21SWarner Losh ttm->caching_state);
895592ffb21SWarner Losh }
896592ffb21SWarner Losh }
897592ffb21SWarner Losh ttm->state = tt_unpopulated;
898592ffb21SWarner Losh }
899592ffb21SWarner Losh
900592ffb21SWarner Losh #if 0
901592ffb21SWarner Losh /* XXXKIB sysctl */
902592ffb21SWarner Losh int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
903592ffb21SWarner Losh {
904592ffb21SWarner Losh struct ttm_page_pool *p;
905592ffb21SWarner Losh unsigned i;
906592ffb21SWarner Losh char *h[] = {"pool", "refills", "pages freed", "size"};
907592ffb21SWarner Losh if (!_manager) {
908592ffb21SWarner Losh seq_printf(m, "No pool allocator running.\n");
909592ffb21SWarner Losh return 0;
910592ffb21SWarner Losh }
911592ffb21SWarner Losh seq_printf(m, "%6s %12s %13s %8s\n",
912592ffb21SWarner Losh h[0], h[1], h[2], h[3]);
913592ffb21SWarner Losh for (i = 0; i < NUM_POOLS; ++i) {
914592ffb21SWarner Losh p = &_manager->pools[i];
915592ffb21SWarner Losh
916592ffb21SWarner Losh seq_printf(m, "%6s %12ld %13ld %8d\n",
917592ffb21SWarner Losh p->name, p->nrefills,
918592ffb21SWarner Losh p->nfrees, p->npages);
919592ffb21SWarner Losh }
920592ffb21SWarner Losh return 0;
921592ffb21SWarner Losh }
922592ffb21SWarner Losh #endif
923