blob: 316f831ad5f044d99be8bcfc40db59da8e3fdc7a [file] [log] [blame]
Pauli Nieminen1403b1a2010-04-01 12:44:57 +00001/*
2 * Copyright (c) Red Hat Inc.
3
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sub license,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors: Dave Airlie <airlied@redhat.com>
24 * Jerome Glisse <jglisse@redhat.com>
25 * Pauli Nieminen <suokkos@gmail.com>
26 */
27
28/* simple list based uncached page pool
29 * - Pool collects resently freed pages for reuse
30 * - Use page->lru to keep a free list
31 * - doesn't track currently in use pages
32 */
Joe Perches25d04792012-03-16 21:43:50 -070033
34#define pr_fmt(fmt) "[TTM] " fmt
35
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000036#include <linux/list.h>
37#include <linux/spinlock.h>
38#include <linux/highmem.h>
39#include <linux/mm_types.h>
Pauli Nieminen07458662010-04-01 12:44:58 +000040#include <linux/module.h>
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000041#include <linux/mm.h>
Matt Turner4cdc8402010-04-07 22:42:04 +000042#include <linux/seq_file.h> /* for seq_printf */
Stephen Rothwell2125b8a2010-04-08 13:42:03 +100043#include <linux/slab.h>
Konrad Rzeszutek Wilkf9820a42010-11-29 13:52:18 -050044#include <linux/dma-mapping.h>
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000045
Arun Sharma600634972011-07-26 16:09:06 -070046#include <linux/atomic.h>
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000047
David Howells760285e2012-10-02 18:01:07 +010048#include <drm/ttm/ttm_bo_driver.h>
49#include <drm/ttm/ttm_page_alloc.h>
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000050
Daniel Vettere6bf6e52016-03-30 13:24:06 +020051#if IS_ENABLED(CONFIG_AGP)
Luck, Tonyd6678652010-07-21 10:15:39 -070052#include <asm/agp.h>
53#endif
Laura Abbotted3ba072017-05-08 15:58:17 -070054#ifdef CONFIG_X86
55#include <asm/set_memory.h>
56#endif
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000057
58#define NUM_PAGES_TO_ALLOC (PAGE_SIZE/sizeof(struct page *))
59#define SMALL_ALLOCATION 16
60#define FREE_ALL_PAGES (~0U)
61/* times are in msecs */
62#define PAGE_FREE_INTERVAL 1000
63
64/**
65 * struct ttm_page_pool - Pool to reuse recently allocated uc/wc pages.
66 *
67 * @lock: Protects the shared pool from concurrnet access. Must be used with
68 * irqsave/irqrestore variants because pool allocator maybe called from
69 * delayed work.
70 * @fill_lock: Prevent concurrent calls to fill.
71 * @list: Pool of free uc/wc pages for fast reuse.
72 * @gfp_flags: Flags to pass for alloc_page.
73 * @npages: Number of pages in pool.
74 */
75struct ttm_page_pool {
76 spinlock_t lock;
77 bool fill_lock;
78 struct list_head list;
Daniel J Blueman0e57a3c2010-09-22 17:45:45 +010079 gfp_t gfp_flags;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000080 unsigned npages;
Pauli Nieminen07458662010-04-01 12:44:58 +000081 char *name;
82 unsigned long nfrees;
83 unsigned long nrefills;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000084};
85
Pauli Nieminenc96af792010-04-01 12:45:03 +000086/**
87 * Limits for the pool. They are handled without locks because only place where
88 * they may change is in sysfs store. They won't have immediate effect anyway
Thomas Hellstrom4abe4382010-05-26 16:21:04 +020089 * so forcing serialization to access them is pointless.
Pauli Nieminenc96af792010-04-01 12:45:03 +000090 */
91
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000092struct ttm_pool_opts {
93 unsigned alloc_size;
94 unsigned max_size;
95 unsigned small;
96};
97
Christian König6ed4e2e2017-10-05 14:27:34 +020098#define NUM_POOLS 6
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000099
100/**
101 * struct ttm_pool_manager - Holds memory pools for fst allocation
102 *
103 * Manager is read only object for pool code so it doesn't need locking.
104 *
105 * @free_interval: minimum number of jiffies between freeing pages from pool.
106 * @page_alloc_inited: reference counting for pool allocation.
107 * @work: Work that is used to shrink the pool. Work is only run when there is
108 * some pages to free.
109 * @small_allocation: Limit in number of pages what is small allocation.
110 *
111 * @pools: All pool objects in use.
112 **/
113struct ttm_pool_manager {
Pauli Nieminenc96af792010-04-01 12:45:03 +0000114 struct kobject kobj;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000115 struct shrinker mm_shrink;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000116 struct ttm_pool_opts options;
117
118 union {
119 struct ttm_page_pool pools[NUM_POOLS];
120 struct {
121 struct ttm_page_pool wc_pool;
122 struct ttm_page_pool uc_pool;
123 struct ttm_page_pool wc_pool_dma32;
124 struct ttm_page_pool uc_pool_dma32;
Christian König6ed4e2e2017-10-05 14:27:34 +0200125 struct ttm_page_pool wc_pool_huge;
126 struct ttm_page_pool uc_pool_huge;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000127 } ;
128 };
129};
130
Pauli Nieminenc96af792010-04-01 12:45:03 +0000131static struct attribute ttm_page_pool_max = {
132 .name = "pool_max_size",
133 .mode = S_IRUGO | S_IWUSR
134};
135static struct attribute ttm_page_pool_small = {
136 .name = "pool_small_allocation",
137 .mode = S_IRUGO | S_IWUSR
138};
139static struct attribute ttm_page_pool_alloc_size = {
140 .name = "pool_allocation_size",
141 .mode = S_IRUGO | S_IWUSR
142};
143
144static struct attribute *ttm_pool_attrs[] = {
145 &ttm_page_pool_max,
146 &ttm_page_pool_small,
147 &ttm_page_pool_alloc_size,
148 NULL
149};
150
151static void ttm_pool_kobj_release(struct kobject *kobj)
152{
153 struct ttm_pool_manager *m =
154 container_of(kobj, struct ttm_pool_manager, kobj);
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200155 kfree(m);
Pauli Nieminenc96af792010-04-01 12:45:03 +0000156}
157
158static ssize_t ttm_pool_store(struct kobject *kobj,
159 struct attribute *attr, const char *buffer, size_t size)
160{
161 struct ttm_pool_manager *m =
162 container_of(kobj, struct ttm_pool_manager, kobj);
163 int chars;
164 unsigned val;
165 chars = sscanf(buffer, "%u", &val);
166 if (chars == 0)
167 return size;
168
169 /* Convert kb to number of pages */
170 val = val / (PAGE_SIZE >> 10);
171
172 if (attr == &ttm_page_pool_max)
173 m->options.max_size = val;
174 else if (attr == &ttm_page_pool_small)
175 m->options.small = val;
176 else if (attr == &ttm_page_pool_alloc_size) {
177 if (val > NUM_PAGES_TO_ALLOC*8) {
Joe Perches25d04792012-03-16 21:43:50 -0700178 pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
Thomas Hellstrom4abe4382010-05-26 16:21:04 +0200179 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
180 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
Pauli Nieminenc96af792010-04-01 12:45:03 +0000181 return size;
182 } else if (val > NUM_PAGES_TO_ALLOC) {
Joe Perches25d04792012-03-16 21:43:50 -0700183 pr_warn("Setting allocation size to larger than %lu is not recommended\n",
184 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
Pauli Nieminenc96af792010-04-01 12:45:03 +0000185 }
186 m->options.alloc_size = val;
187 }
188
189 return size;
190}
191
192static ssize_t ttm_pool_show(struct kobject *kobj,
193 struct attribute *attr, char *buffer)
194{
195 struct ttm_pool_manager *m =
196 container_of(kobj, struct ttm_pool_manager, kobj);
197 unsigned val = 0;
198
199 if (attr == &ttm_page_pool_max)
200 val = m->options.max_size;
201 else if (attr == &ttm_page_pool_small)
202 val = m->options.small;
203 else if (attr == &ttm_page_pool_alloc_size)
204 val = m->options.alloc_size;
205
206 val = val * (PAGE_SIZE >> 10);
207
208 return snprintf(buffer, PAGE_SIZE, "%u\n", val);
209}
210
211static const struct sysfs_ops ttm_pool_sysfs_ops = {
212 .show = &ttm_pool_show,
213 .store = &ttm_pool_store,
214};
215
216static struct kobj_type ttm_pool_kobj_type = {
217 .release = &ttm_pool_kobj_release,
218 .sysfs_ops = &ttm_pool_sysfs_ops,
219 .default_attrs = ttm_pool_attrs,
220};
221
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200222static struct ttm_pool_manager *_manager;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000223
Pauli Nieminen975efdb2010-04-01 12:45:02 +0000224#ifndef CONFIG_X86
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000225static int set_pages_array_wb(struct page **pages, int addrinarray)
226{
Daniel Vettere6bf6e52016-03-30 13:24:06 +0200227#if IS_ENABLED(CONFIG_AGP)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000228 int i;
229
230 for (i = 0; i < addrinarray; i++)
231 unmap_page_from_agp(pages[i]);
232#endif
233 return 0;
234}
235
236static int set_pages_array_wc(struct page **pages, int addrinarray)
237{
Daniel Vettere6bf6e52016-03-30 13:24:06 +0200238#if IS_ENABLED(CONFIG_AGP)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000239 int i;
240
241 for (i = 0; i < addrinarray; i++)
242 map_page_into_agp(pages[i]);
243#endif
244 return 0;
245}
246
247static int set_pages_array_uc(struct page **pages, int addrinarray)
248{
Daniel Vettere6bf6e52016-03-30 13:24:06 +0200249#if IS_ENABLED(CONFIG_AGP)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000250 int i;
251
252 for (i = 0; i < addrinarray; i++)
253 map_page_into_agp(pages[i]);
254#endif
255 return 0;
256}
257#endif
258
259/**
260 * Select the right pool or requested caching state and ttm flags. */
Christian König6ed4e2e2017-10-05 14:27:34 +0200261static struct ttm_page_pool *ttm_get_pool(int flags, bool huge,
262 enum ttm_caching_state cstate)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000263{
264 int pool_index;
265
266 if (cstate == tt_cached)
267 return NULL;
268
269 if (cstate == tt_wc)
270 pool_index = 0x0;
271 else
272 pool_index = 0x1;
273
Christian König6ed4e2e2017-10-05 14:27:34 +0200274 if (flags & TTM_PAGE_FLAG_DMA32) {
275 if (huge)
276 return NULL;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000277 pool_index |= 0x2;
278
Christian König6ed4e2e2017-10-05 14:27:34 +0200279 } else if (huge) {
280 pool_index |= 0x4;
281 }
282
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200283 return &_manager->pools[pool_index];
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000284}
285
286/* set memory back to wb and free the pages. */
287static void ttm_pages_put(struct page *pages[], unsigned npages)
288{
289 unsigned i;
290 if (set_pages_array_wb(pages, npages))
Joe Perches25d04792012-03-16 21:43:50 -0700291 pr_err("Failed to set %d pages to wb!\n", npages);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000292 for (i = 0; i < npages; ++i)
293 __free_page(pages[i]);
294}
295
296static void ttm_pool_update_free_locked(struct ttm_page_pool *pool,
297 unsigned freed_pages)
298{
299 pool->npages -= freed_pages;
Pauli Nieminen07458662010-04-01 12:44:58 +0000300 pool->nfrees += freed_pages;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000301}
302
303/**
304 * Free pages from pool.
305 *
306 * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
307 * number of pages in one go.
308 *
309 * @pool: to free the pages from
310 * @free_all: If set to true will free all pages in pool
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900311 * @use_static: Safe to use static buffer
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000312 **/
Tetsuo Handaa91576d2014-08-03 20:02:31 +0900313static int ttm_page_pool_free(struct ttm_page_pool *pool, unsigned nr_free,
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900314 bool use_static)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000315{
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900316 static struct page *static_buf[NUM_PAGES_TO_ALLOC];
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000317 unsigned long irq_flags;
318 struct page *p;
319 struct page **pages_to_free;
320 unsigned freed_pages = 0,
321 npages_to_free = nr_free;
322
323 if (NUM_PAGES_TO_ALLOC < nr_free)
324 npages_to_free = NUM_PAGES_TO_ALLOC;
325
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900326 if (use_static)
327 pages_to_free = static_buf;
328 else
329 pages_to_free = kmalloc(npages_to_free * sizeof(struct page *),
330 GFP_KERNEL);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000331 if (!pages_to_free) {
Michel Dänzer767601d2017-11-03 16:00:35 +0100332 pr_debug("Failed to allocate memory for pool free operation\n");
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000333 return 0;
334 }
335
336restart:
337 spin_lock_irqsave(&pool->lock, irq_flags);
338
339 list_for_each_entry_reverse(p, &pool->list, lru) {
340 if (freed_pages >= npages_to_free)
341 break;
342
343 pages_to_free[freed_pages++] = p;
344 /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
345 if (freed_pages >= NUM_PAGES_TO_ALLOC) {
346 /* remove range of pages from the pool */
347 __list_del(p->lru.prev, &pool->list);
348
349 ttm_pool_update_free_locked(pool, freed_pages);
350 /**
351 * Because changing page caching is costly
352 * we unlock the pool to prevent stalling.
353 */
354 spin_unlock_irqrestore(&pool->lock, irq_flags);
355
356 ttm_pages_put(pages_to_free, freed_pages);
357 if (likely(nr_free != FREE_ALL_PAGES))
358 nr_free -= freed_pages;
359
360 if (NUM_PAGES_TO_ALLOC >= nr_free)
361 npages_to_free = nr_free;
362 else
363 npages_to_free = NUM_PAGES_TO_ALLOC;
364
365 freed_pages = 0;
366
367 /* free all so restart the processing */
368 if (nr_free)
369 goto restart;
370
Konrad Rzeszutek Wilk0d74f862011-06-08 17:06:15 +0000371 /* Not allowed to fall through or break because
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000372 * following context is inside spinlock while we are
373 * outside here.
374 */
375 goto out;
376
377 }
378 }
379
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000380 /* remove range of pages from the pool */
381 if (freed_pages) {
382 __list_del(&p->lru, &pool->list);
383
384 ttm_pool_update_free_locked(pool, freed_pages);
385 nr_free -= freed_pages;
386 }
387
388 spin_unlock_irqrestore(&pool->lock, irq_flags);
389
390 if (freed_pages)
391 ttm_pages_put(pages_to_free, freed_pages);
392out:
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900393 if (pages_to_free != static_buf)
394 kfree(pages_to_free);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000395 return nr_free;
396}
397
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000398/**
Thomas Hellstrom4abe4382010-05-26 16:21:04 +0200399 * Callback for mm to request pool to reduce number of page held.
Dave Chinner7dc19d52013-08-28 10:18:11 +1000400 *
401 * XXX: (dchinner) Deadlock warning!
402 *
Dave Chinner7dc19d52013-08-28 10:18:11 +1000403 * This code is crying out for a shrinker per pool....
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000404 */
Dave Chinner7dc19d52013-08-28 10:18:11 +1000405static unsigned long
406ttm_pool_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000407{
Tetsuo Handa71336e012014-08-03 20:02:03 +0900408 static DEFINE_MUTEX(lock);
409 static unsigned start_pool;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000410 unsigned i;
Tetsuo Handa71336e012014-08-03 20:02:03 +0900411 unsigned pool_offset;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000412 struct ttm_page_pool *pool;
Ying Han1495f232011-05-24 17:12:27 -0700413 int shrink_pages = sc->nr_to_scan;
Dave Chinner7dc19d52013-08-28 10:18:11 +1000414 unsigned long freed = 0;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000415
Tetsuo Handa71336e012014-08-03 20:02:03 +0900416 if (!mutex_trylock(&lock))
417 return SHRINK_STOP;
418 pool_offset = ++start_pool % NUM_POOLS;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000419 /* select start pool in round robin fashion */
420 for (i = 0; i < NUM_POOLS; ++i) {
421 unsigned nr_free = shrink_pages;
422 if (shrink_pages == 0)
423 break;
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200424 pool = &_manager->pools[(i + pool_offset)%NUM_POOLS];
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900425 /* OK to use static buffer since global mutex is held. */
426 shrink_pages = ttm_page_pool_free(pool, nr_free, true);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000427 freed += nr_free - shrink_pages;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000428 }
Tetsuo Handa71336e012014-08-03 20:02:03 +0900429 mutex_unlock(&lock);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000430 return freed;
431}
432
433
434static unsigned long
435ttm_pool_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
436{
437 unsigned i;
438 unsigned long count = 0;
439
440 for (i = 0; i < NUM_POOLS; ++i)
441 count += _manager->pools[i].npages;
442
443 return count;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000444}
445
446static void ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
447{
Dave Chinner7dc19d52013-08-28 10:18:11 +1000448 manager->mm_shrink.count_objects = ttm_pool_shrink_count;
449 manager->mm_shrink.scan_objects = ttm_pool_shrink_scan;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000450 manager->mm_shrink.seeks = 1;
451 register_shrinker(&manager->mm_shrink);
452}
453
454static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
455{
456 unregister_shrinker(&manager->mm_shrink);
457}
458
459static int ttm_set_pages_caching(struct page **pages,
460 enum ttm_caching_state cstate, unsigned cpages)
461{
462 int r = 0;
463 /* Set page caching */
464 switch (cstate) {
465 case tt_uncached:
466 r = set_pages_array_uc(pages, cpages);
467 if (r)
Joe Perches25d04792012-03-16 21:43:50 -0700468 pr_err("Failed to set %d pages to uc!\n", cpages);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000469 break;
470 case tt_wc:
471 r = set_pages_array_wc(pages, cpages);
472 if (r)
Joe Perches25d04792012-03-16 21:43:50 -0700473 pr_err("Failed to set %d pages to wc!\n", cpages);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000474 break;
475 default:
476 break;
477 }
478 return r;
479}
480
481/**
482 * Free pages the pages that failed to change the caching state. If there is
483 * any pages that have changed their caching state already put them to the
484 * pool.
485 */
486static void ttm_handle_caching_state_failure(struct list_head *pages,
487 int ttm_flags, enum ttm_caching_state cstate,
488 struct page **failed_pages, unsigned cpages)
489{
490 unsigned i;
Thomas Hellstrom4abe4382010-05-26 16:21:04 +0200491 /* Failed pages have to be freed */
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000492 for (i = 0; i < cpages; ++i) {
493 list_del(&failed_pages[i]->lru);
494 __free_page(failed_pages[i]);
495 }
496}
497
498/**
499 * Allocate new pages with correct caching.
500 *
501 * This function is reentrant if caller updates count depending on number of
502 * pages returned in pages array.
503 */
Daniel J Blueman0e57a3c2010-09-22 17:45:45 +0100504static int ttm_alloc_new_pages(struct list_head *pages, gfp_t gfp_flags,
Christian König6ed4e2e2017-10-05 14:27:34 +0200505 int ttm_flags, enum ttm_caching_state cstate,
506 unsigned count, unsigned order)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000507{
508 struct page **caching_array;
509 struct page *p;
510 int r = 0;
Christian König6ed4e2e2017-10-05 14:27:34 +0200511 unsigned i, j, cpages;
512 unsigned npages = 1 << order;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000513 unsigned max_cpages = min(count,
514 (unsigned)(PAGE_SIZE/sizeof(struct page *)));
515
516 /* allocate array for page caching change */
517 caching_array = kmalloc(max_cpages*sizeof(struct page *), GFP_KERNEL);
518
519 if (!caching_array) {
Michel Dänzer767601d2017-11-03 16:00:35 +0100520 pr_debug("Unable to allocate table for new pages\n");
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000521 return -ENOMEM;
522 }
523
524 for (i = 0, cpages = 0; i < count; ++i) {
Christian König6ed4e2e2017-10-05 14:27:34 +0200525 p = alloc_pages(gfp_flags, order);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000526
527 if (!p) {
Michel Dänzer767601d2017-11-03 16:00:35 +0100528 pr_debug("Unable to get page %u\n", i);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000529
530 /* store already allocated pages in the pool after
531 * setting the caching state */
532 if (cpages) {
Thomas Hellstrom4abe4382010-05-26 16:21:04 +0200533 r = ttm_set_pages_caching(caching_array,
534 cstate, cpages);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000535 if (r)
536 ttm_handle_caching_state_failure(pages,
537 ttm_flags, cstate,
538 caching_array, cpages);
539 }
540 r = -ENOMEM;
541 goto out;
542 }
543
Christian König6ed4e2e2017-10-05 14:27:34 +0200544 list_add(&p->lru, pages);
545
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000546#ifdef CONFIG_HIGHMEM
547 /* gfp flags of highmem page should never be dma32 so we
548 * we should be fine in such case
549 */
Christian König6ed4e2e2017-10-05 14:27:34 +0200550 if (PageHighMem(p))
551 continue;
552
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000553#endif
Christian König6ed4e2e2017-10-05 14:27:34 +0200554 for (j = 0; j < npages; ++j) {
555 caching_array[cpages++] = p++;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000556 if (cpages == max_cpages) {
557
558 r = ttm_set_pages_caching(caching_array,
559 cstate, cpages);
560 if (r) {
561 ttm_handle_caching_state_failure(pages,
562 ttm_flags, cstate,
563 caching_array, cpages);
564 goto out;
565 }
566 cpages = 0;
567 }
568 }
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000569 }
570
571 if (cpages) {
572 r = ttm_set_pages_caching(caching_array, cstate, cpages);
573 if (r)
574 ttm_handle_caching_state_failure(pages,
575 ttm_flags, cstate,
576 caching_array, cpages);
577 }
578out:
579 kfree(caching_array);
580
581 return r;
582}
583
584/**
Konrad Rzeszutek Wilk0d74f862011-06-08 17:06:15 +0000585 * Fill the given pool if there aren't enough pages and the requested number of
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000586 * pages is small.
587 */
Christian König6ed4e2e2017-10-05 14:27:34 +0200588static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool, int ttm_flags,
589 enum ttm_caching_state cstate,
590 unsigned count, unsigned long *irq_flags)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000591{
592 struct page *p;
593 int r;
594 unsigned cpages = 0;
595 /**
596 * Only allow one pool fill operation at a time.
597 * If pool doesn't have enough pages for the allocation new pages are
598 * allocated from outside of pool.
599 */
600 if (pool->fill_lock)
601 return;
602
603 pool->fill_lock = true;
604
Konrad Rzeszutek Wilk0d74f862011-06-08 17:06:15 +0000605 /* If allocation request is small and there are not enough
606 * pages in a pool we fill the pool up first. */
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200607 if (count < _manager->options.small
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000608 && count > pool->npages) {
609 struct list_head new_pages;
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200610 unsigned alloc_size = _manager->options.alloc_size;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000611
612 /**
613 * Can't change page caching if in irqsave context. We have to
614 * drop the pool->lock.
615 */
616 spin_unlock_irqrestore(&pool->lock, *irq_flags);
617
618 INIT_LIST_HEAD(&new_pages);
619 r = ttm_alloc_new_pages(&new_pages, pool->gfp_flags, ttm_flags,
Christian König6ed4e2e2017-10-05 14:27:34 +0200620 cstate, alloc_size, 0);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000621 spin_lock_irqsave(&pool->lock, *irq_flags);
622
623 if (!r) {
624 list_splice(&new_pages, &pool->list);
Pauli Nieminen07458662010-04-01 12:44:58 +0000625 ++pool->nrefills;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000626 pool->npages += alloc_size;
627 } else {
Michel Dänzer767601d2017-11-03 16:00:35 +0100628 pr_debug("Failed to fill pool (%p)\n", pool);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000629 /* If we have any pages left put them to the pool. */
Xiangliang.Yu9afae272017-08-16 14:25:51 +0800630 list_for_each_entry(p, &new_pages, lru) {
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000631 ++cpages;
632 }
633 list_splice(&new_pages, &pool->list);
634 pool->npages += cpages;
635 }
636
637 }
638 pool->fill_lock = false;
639}
640
641/**
Christian König8593e9b2017-09-21 11:28:25 +0200642 * Allocate pages from the pool and put them on the return list.
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000643 *
Christian König8593e9b2017-09-21 11:28:25 +0200644 * @return zero for success or negative error code.
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000645 */
Christian König8593e9b2017-09-21 11:28:25 +0200646static int ttm_page_pool_get_pages(struct ttm_page_pool *pool,
647 struct list_head *pages,
648 int ttm_flags,
649 enum ttm_caching_state cstate,
Christian König6ed4e2e2017-10-05 14:27:34 +0200650 unsigned count, unsigned order)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000651{
652 unsigned long irq_flags;
653 struct list_head *p;
654 unsigned i;
Christian König8593e9b2017-09-21 11:28:25 +0200655 int r = 0;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000656
657 spin_lock_irqsave(&pool->lock, irq_flags);
Christian König6ed4e2e2017-10-05 14:27:34 +0200658 if (!order)
659 ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count,
660 &irq_flags);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000661
662 if (count >= pool->npages) {
663 /* take all pages from the pool */
664 list_splice_init(&pool->list, pages);
665 count -= pool->npages;
666 pool->npages = 0;
667 goto out;
668 }
669 /* find the last pages to include for requested number of pages. Split
Konrad Rzeszutek Wilk0d74f862011-06-08 17:06:15 +0000670 * pool to begin and halve it to reduce search space. */
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000671 if (count <= pool->npages/2) {
672 i = 0;
673 list_for_each(p, &pool->list) {
674 if (++i == count)
675 break;
676 }
677 } else {
678 i = pool->npages + 1;
679 list_for_each_prev(p, &pool->list) {
680 if (--i == count)
681 break;
682 }
683 }
Konrad Rzeszutek Wilk0d74f862011-06-08 17:06:15 +0000684 /* Cut 'count' number of pages from the pool */
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000685 list_cut_position(pages, &pool->list, p);
686 pool->npages -= count;
687 count = 0;
688out:
689 spin_unlock_irqrestore(&pool->lock, irq_flags);
Christian König8593e9b2017-09-21 11:28:25 +0200690
691 /* clear the pages coming from the pool if requested */
692 if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
693 struct page *page;
694
695 list_for_each_entry(page, pages, lru) {
696 if (PageHighMem(page))
697 clear_highpage(page);
698 else
699 clear_page(page_address(page));
700 }
701 }
702
703 /* If pool didn't have enough pages allocate new one. */
704 if (count) {
705 gfp_t gfp_flags = pool->gfp_flags;
706
707 /* set zero flag for page allocation if required */
708 if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
709 gfp_flags |= __GFP_ZERO;
710
711 /* ttm_alloc_new_pages doesn't reference pool so we can run
712 * multiple requests in parallel.
713 **/
714 r = ttm_alloc_new_pages(pages, gfp_flags, ttm_flags, cstate,
Christian König6ed4e2e2017-10-05 14:27:34 +0200715 count, order);
Christian König8593e9b2017-09-21 11:28:25 +0200716 }
717
718 return r;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000719}
720
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500721/* Put all pages in pages list to correct pool to wait for reuse */
722static void ttm_put_pages(struct page **pages, unsigned npages, int flags,
723 enum ttm_caching_state cstate)
724{
Christian König6ed4e2e2017-10-05 14:27:34 +0200725 struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
Tom St Denis7d0a4282017-10-12 07:25:08 -0400726#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Christian König6ed4e2e2017-10-05 14:27:34 +0200727 struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
Tom St Denis7d0a4282017-10-12 07:25:08 -0400728#endif
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500729 unsigned long irq_flags;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500730 unsigned i;
731
732 if (pool == NULL) {
733 /* No pool for this memory type so free the pages */
Christian König0284f1e2017-09-20 15:06:12 +0200734 i = 0;
735 while (i < npages) {
Christian König5c42c642017-10-12 19:28:42 +0200736#ifdef CONFIG_TRANSPARENT_HUGEPAGE
737 struct page *p = pages[i];
738#endif
739 unsigned order = 0, j;
Christian König0284f1e2017-09-20 15:06:12 +0200740
741 if (!pages[i]) {
742 ++i;
743 continue;
744 }
745
Christian König5c42c642017-10-12 19:28:42 +0200746#ifdef CONFIG_TRANSPARENT_HUGEPAGE
747 for (j = 0; j < HPAGE_PMD_NR; ++j)
748 if (p++ != pages[i + j])
749 break;
750
751 if (j == HPAGE_PMD_NR)
752 order = HPAGE_PMD_ORDER;
753#endif
754
Christian König0284f1e2017-09-20 15:06:12 +0200755 if (page_count(pages[i]) != 1)
756 pr_err("Erroneous page count. Leaking pages.\n");
Christian König0284f1e2017-09-20 15:06:12 +0200757 __free_pages(pages[i], order);
758
Christian König5c42c642017-10-12 19:28:42 +0200759 j = 1 << order;
760 while (j) {
Christian König0284f1e2017-09-20 15:06:12 +0200761 pages[i++] = NULL;
Christian König5c42c642017-10-12 19:28:42 +0200762 --j;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500763 }
764 }
765 return;
766 }
767
Christian König6ed4e2e2017-10-05 14:27:34 +0200768 i = 0;
769#ifdef CONFIG_TRANSPARENT_HUGEPAGE
770 if (huge) {
771 unsigned max_size, n2free;
772
773 spin_lock_irqsave(&huge->lock, irq_flags);
774 while (i < npages) {
775 struct page *p = pages[i];
776 unsigned j;
777
778 if (!p)
779 break;
780
781 for (j = 0; j < HPAGE_PMD_NR; ++j)
782 if (p++ != pages[i + j])
783 break;
784
785 if (j != HPAGE_PMD_NR)
786 break;
787
788 list_add_tail(&pages[i]->lru, &huge->list);
789
790 for (j = 0; j < HPAGE_PMD_NR; ++j)
791 pages[i++] = NULL;
792 huge->npages++;
793 }
794
795 /* Check that we don't go over the pool limit */
796 max_size = _manager->options.max_size;
797 max_size /= HPAGE_PMD_NR;
798 if (huge->npages > max_size)
799 n2free = huge->npages - max_size;
800 else
801 n2free = 0;
802 spin_unlock_irqrestore(&huge->lock, irq_flags);
803 if (n2free)
804 ttm_page_pool_free(huge, n2free, false);
805 }
806#endif
807
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500808 spin_lock_irqsave(&pool->lock, irq_flags);
Christian König6ed4e2e2017-10-05 14:27:34 +0200809 while (i < npages) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500810 if (pages[i]) {
811 if (page_count(pages[i]) != 1)
Joe Perches25d04792012-03-16 21:43:50 -0700812 pr_err("Erroneous page count. Leaking pages.\n");
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500813 list_add_tail(&pages[i]->lru, &pool->list);
814 pages[i] = NULL;
815 pool->npages++;
816 }
Christian König6ed4e2e2017-10-05 14:27:34 +0200817 ++i;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500818 }
819 /* Check that we don't go over the pool limit */
820 npages = 0;
821 if (pool->npages > _manager->options.max_size) {
822 npages = pool->npages - _manager->options.max_size;
823 /* free at least NUM_PAGES_TO_ALLOC number of pages
824 * to reduce calls to set_memory_wb */
825 if (npages < NUM_PAGES_TO_ALLOC)
826 npages = NUM_PAGES_TO_ALLOC;
827 }
828 spin_unlock_irqrestore(&pool->lock, irq_flags);
829 if (npages)
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900830 ttm_page_pool_free(pool, npages, false);
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500831}
832
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000833/*
834 * On success pages list will hold count number of correctly
835 * cached pages.
836 */
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500837static int ttm_get_pages(struct page **pages, unsigned npages, int flags,
838 enum ttm_caching_state cstate)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000839{
Christian König6ed4e2e2017-10-05 14:27:34 +0200840 struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
Tom St Denis7d0a4282017-10-12 07:25:08 -0400841#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Christian König6ed4e2e2017-10-05 14:27:34 +0200842 struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
Tom St Denis7d0a4282017-10-12 07:25:08 -0400843#endif
Jerome Glisse822c4d92011-11-10 18:24:09 -0500844 struct list_head plist;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000845 struct page *p = NULL;
Jerome Glisse822c4d92011-11-10 18:24:09 -0500846 unsigned count;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000847 int r;
848
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000849 /* No pool for cached pages */
850 if (pool == NULL) {
Christian König8593e9b2017-09-21 11:28:25 +0200851 gfp_t gfp_flags = GFP_USER;
Tom St Denis7d0a4282017-10-12 07:25:08 -0400852 unsigned i;
853#ifdef CONFIG_TRANSPARENT_HUGEPAGE
854 unsigned j;
855#endif
Christian König0284f1e2017-09-20 15:06:12 +0200856
Christian König8593e9b2017-09-21 11:28:25 +0200857 /* set zero flag for page allocation if required */
858 if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
859 gfp_flags |= __GFP_ZERO;
860
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000861 if (flags & TTM_PAGE_FLAG_DMA32)
862 gfp_flags |= GFP_DMA32;
863 else
Thomas Hellstrome8613c02010-05-26 16:21:03 +0200864 gfp_flags |= GFP_HIGHUSER;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000865
Christian König0284f1e2017-09-20 15:06:12 +0200866 i = 0;
867#ifdef CONFIG_TRANSPARENT_HUGEPAGE
868 while (npages >= HPAGE_PMD_NR) {
869 gfp_t huge_flags = gfp_flags;
870
871 huge_flags |= GFP_TRANSHUGE;
872 huge_flags &= ~__GFP_MOVABLE;
873 huge_flags &= ~__GFP_COMP;
874 p = alloc_pages(huge_flags, HPAGE_PMD_ORDER);
875 if (!p)
876 break;
877
878 for (j = 0; j < HPAGE_PMD_NR; ++j)
879 pages[i++] = p++;
880
881 npages -= HPAGE_PMD_NR;
882 }
883#endif
884
885 while (npages) {
Dave Airlied87dfdb2011-04-13 09:15:09 +1000886 p = alloc_page(gfp_flags);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000887 if (!p) {
Michel Dänzer767601d2017-11-03 16:00:35 +0100888 pr_debug("Unable to allocate page\n");
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000889 return -ENOMEM;
890 }
Dave Airlied87dfdb2011-04-13 09:15:09 +1000891
Christian König0284f1e2017-09-20 15:06:12 +0200892 pages[i++] = p;
893 --npages;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000894 }
895 return 0;
896 }
897
Jerome Glisse822c4d92011-11-10 18:24:09 -0500898 count = 0;
Christian König6ed4e2e2017-10-05 14:27:34 +0200899
900#ifdef CONFIG_TRANSPARENT_HUGEPAGE
901 if (huge && npages >= HPAGE_PMD_NR) {
902 INIT_LIST_HEAD(&plist);
903 ttm_page_pool_get_pages(huge, &plist, flags, cstate,
904 npages / HPAGE_PMD_NR,
905 HPAGE_PMD_ORDER);
906
907 list_for_each_entry(p, &plist, lru) {
908 unsigned j;
909
910 for (j = 0; j < HPAGE_PMD_NR; ++j)
911 pages[count++] = &p[j];
912 }
913 }
914#endif
915
916 INIT_LIST_HEAD(&plist);
917 r = ttm_page_pool_get_pages(pool, &plist, flags, cstate,
918 npages - count, 0);
919
Christian König8593e9b2017-09-21 11:28:25 +0200920 list_for_each_entry(p, &plist, lru)
Jerome Glisse822c4d92011-11-10 18:24:09 -0500921 pages[count++] = p;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000922
Christian König8593e9b2017-09-21 11:28:25 +0200923 if (r) {
924 /* If there is any pages in the list put them back to
925 * the pool.
926 */
Michel Dänzer767601d2017-11-03 16:00:35 +0100927 pr_debug("Failed to allocate extra pages for large request\n");
Christian König8593e9b2017-09-21 11:28:25 +0200928 ttm_put_pages(pages, count, flags, cstate);
929 return r;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000930 }
931
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000932 return 0;
933}
934
Dave Airlie3b9c2142014-07-22 12:55:39 +1000935static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, gfp_t flags,
Pauli Nieminen07458662010-04-01 12:44:58 +0000936 char *name)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000937{
938 spin_lock_init(&pool->lock);
939 pool->fill_lock = false;
940 INIT_LIST_HEAD(&pool->list);
Pauli Nieminen07458662010-04-01 12:44:58 +0000941 pool->npages = pool->nfrees = 0;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000942 pool->gfp_flags = flags;
Pauli Nieminen07458662010-04-01 12:44:58 +0000943 pool->name = name;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000944}
945
Pauli Nieminenc96af792010-04-01 12:45:03 +0000946int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000947{
Pauli Nieminenc96af792010-04-01 12:45:03 +0000948 int ret;
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200949
950 WARN_ON(_manager);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000951
Joe Perches25d04792012-03-16 21:43:50 -0700952 pr_info("Initializing pool allocator\n");
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000953
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200954 _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000955
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200956 ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc");
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000957
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200958 ttm_page_pool_init_locked(&_manager->uc_pool, GFP_HIGHUSER, "uc");
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000959
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200960 ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
961 GFP_USER | GFP_DMA32, "wc dma");
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000962
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200963 ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
964 GFP_USER | GFP_DMA32, "uc dma");
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000965
Christian König6ed4e2e2017-10-05 14:27:34 +0200966 ttm_page_pool_init_locked(&_manager->wc_pool_huge,
967 GFP_TRANSHUGE & ~(__GFP_MOVABLE | __GFP_COMP),
968 "wc huge");
969
970 ttm_page_pool_init_locked(&_manager->uc_pool_huge,
971 GFP_TRANSHUGE & ~(__GFP_MOVABLE | __GFP_COMP)
972 , "uc huge");
973
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200974 _manager->options.max_size = max_pages;
975 _manager->options.small = SMALL_ALLOCATION;
976 _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
977
978 ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
979 &glob->kobj, "pool");
Pauli Nieminenc96af792010-04-01 12:45:03 +0000980 if (unlikely(ret != 0)) {
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200981 kobject_put(&_manager->kobj);
982 _manager = NULL;
Pauli Nieminenc96af792010-04-01 12:45:03 +0000983 return ret;
984 }
985
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200986 ttm_pool_mm_shrink_init(_manager);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000987
988 return 0;
989}
990
Daniel J Blueman0e57a3c2010-09-22 17:45:45 +0100991void ttm_page_alloc_fini(void)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000992{
993 int i;
994
Joe Perches25d04792012-03-16 21:43:50 -0700995 pr_info("Finalizing pool allocator\n");
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200996 ttm_pool_mm_shrink_fini(_manager);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000997
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900998 /* OK to use static buffer since global mutex is no longer used. */
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000999 for (i = 0; i < NUM_POOLS; ++i)
Tetsuo Handa881fdaa2014-11-13 22:43:23 +09001000 ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);
Pauli Nieminenc96af792010-04-01 12:45:03 +00001001
Francisco Jerez5870a4d2010-07-04 04:03:07 +02001002 kobject_put(&_manager->kobj);
1003 _manager = NULL;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +00001004}
Pauli Nieminen07458662010-04-01 12:44:58 +00001005
Jerome Glisseb1e5f172011-11-02 23:59:28 -04001006int ttm_pool_populate(struct ttm_tt *ttm)
1007{
1008 struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
1009 unsigned i;
1010 int ret;
1011
1012 if (ttm->state != tt_unpopulated)
1013 return 0;
1014
Christian Königc6e839a2017-09-19 15:20:42 +02001015 ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
1016 ttm->caching_state);
1017 if (unlikely(ret != 0)) {
1018 ttm_pool_unpopulate(ttm);
1019 return ret;
1020 }
Jerome Glisseb1e5f172011-11-02 23:59:28 -04001021
Christian Königc6e839a2017-09-19 15:20:42 +02001022 for (i = 0; i < ttm->num_pages; ++i) {
Christian Königd188bfa2017-07-04 16:56:24 +02001023 ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
1024 PAGE_SIZE);
Jerome Glisseb1e5f172011-11-02 23:59:28 -04001025 if (unlikely(ret != 0)) {
1026 ttm_pool_unpopulate(ttm);
1027 return -ENOMEM;
1028 }
1029 }
1030
1031 if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
1032 ret = ttm_tt_swapin(ttm);
1033 if (unlikely(ret != 0)) {
1034 ttm_pool_unpopulate(ttm);
1035 return ret;
1036 }
1037 }
1038
1039 ttm->state = tt_unbound;
1040 return 0;
1041}
1042EXPORT_SYMBOL(ttm_pool_populate);
1043
1044void ttm_pool_unpopulate(struct ttm_tt *ttm)
1045{
1046 unsigned i;
1047
1048 for (i = 0; i < ttm->num_pages; ++i) {
Christian Königc6e839a2017-09-19 15:20:42 +02001049 if (!ttm->pages[i])
1050 continue;
1051
1052 ttm_mem_global_free_page(ttm->glob->mem_glob, ttm->pages[i],
1053 PAGE_SIZE);
Jerome Glisseb1e5f172011-11-02 23:59:28 -04001054 }
Christian Königc6e839a2017-09-19 15:20:42 +02001055 ttm_put_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
1056 ttm->caching_state);
Jerome Glisseb1e5f172011-11-02 23:59:28 -04001057 ttm->state = tt_unpopulated;
1058}
1059EXPORT_SYMBOL(ttm_pool_unpopulate);
1060
Tom St Denis7a9667a2017-09-05 07:30:59 -04001061#if defined(CONFIG_SWIOTLB) || defined(CONFIG_INTEL_IOMMU)
Tom St Denisa4dec812017-08-18 10:04:57 -04001062int ttm_populate_and_map_pages(struct device *dev, struct ttm_dma_tt *tt)
1063{
Christian König6056a1a2017-09-20 14:07:02 +02001064 unsigned i, j;
Tom St Denisa4dec812017-08-18 10:04:57 -04001065 int r;
1066
1067 r = ttm_pool_populate(&tt->ttm);
1068 if (r)
1069 return r;
1070
Christian König6056a1a2017-09-20 14:07:02 +02001071 for (i = 0; i < tt->ttm.num_pages; ++i) {
1072 struct page *p = tt->ttm.pages[i];
1073 size_t num_pages = 1;
1074
1075 for (j = i + 1; j < tt->ttm.num_pages; ++j) {
1076 if (++p != tt->ttm.pages[j])
1077 break;
1078
1079 ++num_pages;
1080 }
1081
Tom St Denisa4dec812017-08-18 10:04:57 -04001082 tt->dma_address[i] = dma_map_page(dev, tt->ttm.pages[i],
Christian König6056a1a2017-09-20 14:07:02 +02001083 0, num_pages * PAGE_SIZE,
Tom St Denisa4dec812017-08-18 10:04:57 -04001084 DMA_BIDIRECTIONAL);
1085 if (dma_mapping_error(dev, tt->dma_address[i])) {
1086 while (i--) {
1087 dma_unmap_page(dev, tt->dma_address[i],
1088 PAGE_SIZE, DMA_BIDIRECTIONAL);
1089 tt->dma_address[i] = 0;
1090 }
1091 ttm_pool_unpopulate(&tt->ttm);
1092 return -EFAULT;
1093 }
Christian König6056a1a2017-09-20 14:07:02 +02001094
1095 for (j = 1; j < num_pages; ++j) {
1096 tt->dma_address[i + 1] = tt->dma_address[i] + PAGE_SIZE;
1097 ++i;
1098 }
Tom St Denisa4dec812017-08-18 10:04:57 -04001099 }
1100 return 0;
1101}
1102EXPORT_SYMBOL(ttm_populate_and_map_pages);
1103
1104void ttm_unmap_and_unpopulate_pages(struct device *dev, struct ttm_dma_tt *tt)
1105{
Christian König6056a1a2017-09-20 14:07:02 +02001106 unsigned i, j;
1107
1108 for (i = 0; i < tt->ttm.num_pages;) {
1109 struct page *p = tt->ttm.pages[i];
1110 size_t num_pages = 1;
1111
1112 if (!tt->dma_address[i] || !tt->ttm.pages[i]) {
1113 ++i;
1114 continue;
Tom St Denisa4dec812017-08-18 10:04:57 -04001115 }
Christian König6056a1a2017-09-20 14:07:02 +02001116
1117 for (j = i + 1; j < tt->ttm.num_pages; ++j) {
1118 if (++p != tt->ttm.pages[j])
1119 break;
1120
1121 ++num_pages;
1122 }
1123
1124 dma_unmap_page(dev, tt->dma_address[i], num_pages * PAGE_SIZE,
1125 DMA_BIDIRECTIONAL);
1126
1127 i += num_pages;
Tom St Denisa4dec812017-08-18 10:04:57 -04001128 }
1129 ttm_pool_unpopulate(&tt->ttm);
1130}
1131EXPORT_SYMBOL(ttm_unmap_and_unpopulate_pages);
Tom St Denis7a9667a2017-09-05 07:30:59 -04001132#endif
Tom St Denisa4dec812017-08-18 10:04:57 -04001133
Pauli Nieminen07458662010-04-01 12:44:58 +00001134int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
1135{
1136 struct ttm_page_pool *p;
1137 unsigned i;
1138 char *h[] = {"pool", "refills", "pages freed", "size"};
Francisco Jerez5870a4d2010-07-04 04:03:07 +02001139 if (!_manager) {
Pauli Nieminen07458662010-04-01 12:44:58 +00001140 seq_printf(m, "No pool allocator running.\n");
1141 return 0;
1142 }
Christian König6ed4e2e2017-10-05 14:27:34 +02001143 seq_printf(m, "%7s %12s %13s %8s\n",
Pauli Nieminen07458662010-04-01 12:44:58 +00001144 h[0], h[1], h[2], h[3]);
1145 for (i = 0; i < NUM_POOLS; ++i) {
Francisco Jerez5870a4d2010-07-04 04:03:07 +02001146 p = &_manager->pools[i];
Pauli Nieminen07458662010-04-01 12:44:58 +00001147
Christian König6ed4e2e2017-10-05 14:27:34 +02001148 seq_printf(m, "%7s %12ld %13ld %8d\n",
Pauli Nieminen07458662010-04-01 12:44:58 +00001149 p->name, p->nrefills,
1150 p->nfrees, p->npages);
1151 }
1152 return 0;
1153}
1154EXPORT_SYMBOL(ttm_page_alloc_debugfs);