blob: 116897a205141156af7498b8c0708431ea49d489 [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;
Roger He750a2502017-11-21 09:37:52 +080084 unsigned int order;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000085};
86
Pauli Nieminenc96af792010-04-01 12:45:03 +000087/**
88 * Limits for the pool. They are handled without locks because only place where
89 * they may change is in sysfs store. They won't have immediate effect anyway
Thomas Hellstrom4abe4382010-05-26 16:21:04 +020090 * so forcing serialization to access them is pointless.
Pauli Nieminenc96af792010-04-01 12:45:03 +000091 */
92
Pauli Nieminen1403b1a2010-04-01 12:44:57 +000093struct ttm_pool_opts {
94 unsigned alloc_size;
95 unsigned max_size;
96 unsigned small;
97};
98
Christian König6ed4e2e2017-10-05 14:27:34 +020099#define NUM_POOLS 6
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000100
101/**
102 * struct ttm_pool_manager - Holds memory pools for fst allocation
103 *
104 * Manager is read only object for pool code so it doesn't need locking.
105 *
106 * @free_interval: minimum number of jiffies between freeing pages from pool.
107 * @page_alloc_inited: reference counting for pool allocation.
108 * @work: Work that is used to shrink the pool. Work is only run when there is
109 * some pages to free.
110 * @small_allocation: Limit in number of pages what is small allocation.
111 *
112 * @pools: All pool objects in use.
113 **/
114struct ttm_pool_manager {
Pauli Nieminenc96af792010-04-01 12:45:03 +0000115 struct kobject kobj;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000116 struct shrinker mm_shrink;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000117 struct ttm_pool_opts options;
118
119 union {
120 struct ttm_page_pool pools[NUM_POOLS];
121 struct {
122 struct ttm_page_pool wc_pool;
123 struct ttm_page_pool uc_pool;
124 struct ttm_page_pool wc_pool_dma32;
125 struct ttm_page_pool uc_pool_dma32;
Christian König6ed4e2e2017-10-05 14:27:34 +0200126 struct ttm_page_pool wc_pool_huge;
127 struct ttm_page_pool uc_pool_huge;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000128 } ;
129 };
130};
131
Pauli Nieminenc96af792010-04-01 12:45:03 +0000132static struct attribute ttm_page_pool_max = {
133 .name = "pool_max_size",
134 .mode = S_IRUGO | S_IWUSR
135};
136static struct attribute ttm_page_pool_small = {
137 .name = "pool_small_allocation",
138 .mode = S_IRUGO | S_IWUSR
139};
140static struct attribute ttm_page_pool_alloc_size = {
141 .name = "pool_allocation_size",
142 .mode = S_IRUGO | S_IWUSR
143};
144
145static struct attribute *ttm_pool_attrs[] = {
146 &ttm_page_pool_max,
147 &ttm_page_pool_small,
148 &ttm_page_pool_alloc_size,
149 NULL
150};
151
152static void ttm_pool_kobj_release(struct kobject *kobj)
153{
154 struct ttm_pool_manager *m =
155 container_of(kobj, struct ttm_pool_manager, kobj);
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200156 kfree(m);
Pauli Nieminenc96af792010-04-01 12:45:03 +0000157}
158
159static ssize_t ttm_pool_store(struct kobject *kobj,
160 struct attribute *attr, const char *buffer, size_t size)
161{
162 struct ttm_pool_manager *m =
163 container_of(kobj, struct ttm_pool_manager, kobj);
164 int chars;
165 unsigned val;
166 chars = sscanf(buffer, "%u", &val);
167 if (chars == 0)
168 return size;
169
170 /* Convert kb to number of pages */
171 val = val / (PAGE_SIZE >> 10);
172
173 if (attr == &ttm_page_pool_max)
174 m->options.max_size = val;
175 else if (attr == &ttm_page_pool_small)
176 m->options.small = val;
177 else if (attr == &ttm_page_pool_alloc_size) {
178 if (val > NUM_PAGES_TO_ALLOC*8) {
Joe Perches25d04792012-03-16 21:43:50 -0700179 pr_err("Setting allocation size to %lu is not allowed. Recommended size is %lu\n",
Thomas Hellstrom4abe4382010-05-26 16:21:04 +0200180 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 7),
181 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
Pauli Nieminenc96af792010-04-01 12:45:03 +0000182 return size;
183 } else if (val > NUM_PAGES_TO_ALLOC) {
Joe Perches25d04792012-03-16 21:43:50 -0700184 pr_warn("Setting allocation size to larger than %lu is not recommended\n",
185 NUM_PAGES_TO_ALLOC*(PAGE_SIZE >> 10));
Pauli Nieminenc96af792010-04-01 12:45:03 +0000186 }
187 m->options.alloc_size = val;
188 }
189
190 return size;
191}
192
193static ssize_t ttm_pool_show(struct kobject *kobj,
194 struct attribute *attr, char *buffer)
195{
196 struct ttm_pool_manager *m =
197 container_of(kobj, struct ttm_pool_manager, kobj);
198 unsigned val = 0;
199
200 if (attr == &ttm_page_pool_max)
201 val = m->options.max_size;
202 else if (attr == &ttm_page_pool_small)
203 val = m->options.small;
204 else if (attr == &ttm_page_pool_alloc_size)
205 val = m->options.alloc_size;
206
207 val = val * (PAGE_SIZE >> 10);
208
209 return snprintf(buffer, PAGE_SIZE, "%u\n", val);
210}
211
212static const struct sysfs_ops ttm_pool_sysfs_ops = {
213 .show = &ttm_pool_show,
214 .store = &ttm_pool_store,
215};
216
217static struct kobj_type ttm_pool_kobj_type = {
218 .release = &ttm_pool_kobj_release,
219 .sysfs_ops = &ttm_pool_sysfs_ops,
220 .default_attrs = ttm_pool_attrs,
221};
222
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200223static struct ttm_pool_manager *_manager;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000224
Pauli Nieminen975efdb2010-04-01 12:45:02 +0000225#ifndef CONFIG_X86
Roger He154683d2017-11-22 15:09:33 +0800226static int set_pages_wb(struct page *page, int numpages)
227{
228#if IS_ENABLED(CONFIG_AGP)
229 int i;
230
231 for (i = 0; i < numpages; i++)
232 unmap_page_from_agp(page++);
233#endif
234 return 0;
235}
236
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000237static int set_pages_array_wb(struct page **pages, int addrinarray)
238{
Daniel Vettere6bf6e52016-03-30 13:24:06 +0200239#if IS_ENABLED(CONFIG_AGP)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000240 int i;
241
242 for (i = 0; i < addrinarray; i++)
243 unmap_page_from_agp(pages[i]);
244#endif
245 return 0;
246}
247
248static int set_pages_array_wc(struct page **pages, int addrinarray)
249{
Daniel Vettere6bf6e52016-03-30 13:24:06 +0200250#if IS_ENABLED(CONFIG_AGP)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000251 int i;
252
253 for (i = 0; i < addrinarray; i++)
254 map_page_into_agp(pages[i]);
255#endif
256 return 0;
257}
258
259static int set_pages_array_uc(struct page **pages, int addrinarray)
260{
Daniel Vettere6bf6e52016-03-30 13:24:06 +0200261#if IS_ENABLED(CONFIG_AGP)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000262 int i;
263
264 for (i = 0; i < addrinarray; i++)
265 map_page_into_agp(pages[i]);
266#endif
267 return 0;
268}
269#endif
270
271/**
272 * Select the right pool or requested caching state and ttm flags. */
Christian König6ed4e2e2017-10-05 14:27:34 +0200273static struct ttm_page_pool *ttm_get_pool(int flags, bool huge,
274 enum ttm_caching_state cstate)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000275{
276 int pool_index;
277
278 if (cstate == tt_cached)
279 return NULL;
280
281 if (cstate == tt_wc)
282 pool_index = 0x0;
283 else
284 pool_index = 0x1;
285
Christian König6ed4e2e2017-10-05 14:27:34 +0200286 if (flags & TTM_PAGE_FLAG_DMA32) {
287 if (huge)
288 return NULL;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000289 pool_index |= 0x2;
290
Christian König6ed4e2e2017-10-05 14:27:34 +0200291 } else if (huge) {
292 pool_index |= 0x4;
293 }
294
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200295 return &_manager->pools[pool_index];
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000296}
297
298/* set memory back to wb and free the pages. */
Roger He444f8ef2017-11-21 14:24:48 +0800299static void ttm_pages_put(struct page *pages[], unsigned npages,
300 unsigned int order)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000301{
Roger He444f8ef2017-11-21 14:24:48 +0800302 unsigned int i, pages_nr = (1 << order);
303
304 if (order == 0) {
305 if (set_pages_array_wb(pages, npages))
306 pr_err("Failed to set %d pages to wb!\n", npages);
307 }
308
309 for (i = 0; i < npages; ++i) {
310 if (order > 0) {
311 if (set_pages_wb(pages[i], pages_nr))
312 pr_err("Failed to set %d pages to wb!\n", pages_nr);
313 }
314 __free_pages(pages[i], order);
315 }
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000316}
317
318static void ttm_pool_update_free_locked(struct ttm_page_pool *pool,
319 unsigned freed_pages)
320{
321 pool->npages -= freed_pages;
Pauli Nieminen07458662010-04-01 12:44:58 +0000322 pool->nfrees += freed_pages;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000323}
324
325/**
326 * Free pages from pool.
327 *
328 * To prevent hogging the ttm_swap process we only free NUM_PAGES_TO_ALLOC
329 * number of pages in one go.
330 *
331 * @pool: to free the pages from
332 * @free_all: If set to true will free all pages in pool
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900333 * @use_static: Safe to use static buffer
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000334 **/
Tetsuo Handaa91576d2014-08-03 20:02:31 +0900335static int ttm_page_pool_free(struct ttm_page_pool *pool, unsigned nr_free,
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900336 bool use_static)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000337{
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900338 static struct page *static_buf[NUM_PAGES_TO_ALLOC];
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000339 unsigned long irq_flags;
340 struct page *p;
341 struct page **pages_to_free;
342 unsigned freed_pages = 0,
343 npages_to_free = nr_free;
344
345 if (NUM_PAGES_TO_ALLOC < nr_free)
346 npages_to_free = NUM_PAGES_TO_ALLOC;
347
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900348 if (use_static)
349 pages_to_free = static_buf;
350 else
351 pages_to_free = kmalloc(npages_to_free * sizeof(struct page *),
352 GFP_KERNEL);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000353 if (!pages_to_free) {
Michel Dänzer767601d2017-11-03 16:00:35 +0100354 pr_debug("Failed to allocate memory for pool free operation\n");
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000355 return 0;
356 }
357
358restart:
359 spin_lock_irqsave(&pool->lock, irq_flags);
360
361 list_for_each_entry_reverse(p, &pool->list, lru) {
362 if (freed_pages >= npages_to_free)
363 break;
364
365 pages_to_free[freed_pages++] = p;
366 /* We can only remove NUM_PAGES_TO_ALLOC at a time. */
367 if (freed_pages >= NUM_PAGES_TO_ALLOC) {
368 /* remove range of pages from the pool */
369 __list_del(p->lru.prev, &pool->list);
370
371 ttm_pool_update_free_locked(pool, freed_pages);
372 /**
373 * Because changing page caching is costly
374 * we unlock the pool to prevent stalling.
375 */
376 spin_unlock_irqrestore(&pool->lock, irq_flags);
377
Roger He444f8ef2017-11-21 14:24:48 +0800378 ttm_pages_put(pages_to_free, freed_pages, pool->order);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000379 if (likely(nr_free != FREE_ALL_PAGES))
380 nr_free -= freed_pages;
381
382 if (NUM_PAGES_TO_ALLOC >= nr_free)
383 npages_to_free = nr_free;
384 else
385 npages_to_free = NUM_PAGES_TO_ALLOC;
386
387 freed_pages = 0;
388
389 /* free all so restart the processing */
390 if (nr_free)
391 goto restart;
392
Konrad Rzeszutek Wilk0d74f862011-06-08 17:06:15 +0000393 /* Not allowed to fall through or break because
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000394 * following context is inside spinlock while we are
395 * outside here.
396 */
397 goto out;
398
399 }
400 }
401
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000402 /* remove range of pages from the pool */
403 if (freed_pages) {
404 __list_del(&p->lru, &pool->list);
405
406 ttm_pool_update_free_locked(pool, freed_pages);
407 nr_free -= freed_pages;
408 }
409
410 spin_unlock_irqrestore(&pool->lock, irq_flags);
411
412 if (freed_pages)
Roger He444f8ef2017-11-21 14:24:48 +0800413 ttm_pages_put(pages_to_free, freed_pages, pool->order);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000414out:
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900415 if (pages_to_free != static_buf)
416 kfree(pages_to_free);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000417 return nr_free;
418}
419
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000420/**
Thomas Hellstrom4abe4382010-05-26 16:21:04 +0200421 * Callback for mm to request pool to reduce number of page held.
Dave Chinner7dc19d52013-08-28 10:18:11 +1000422 *
423 * XXX: (dchinner) Deadlock warning!
424 *
Dave Chinner7dc19d52013-08-28 10:18:11 +1000425 * This code is crying out for a shrinker per pool....
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000426 */
Dave Chinner7dc19d52013-08-28 10:18:11 +1000427static unsigned long
428ttm_pool_shrink_scan(struct shrinker *shrink, struct shrink_control *sc)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000429{
Tetsuo Handa71336e012014-08-03 20:02:03 +0900430 static DEFINE_MUTEX(lock);
431 static unsigned start_pool;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000432 unsigned i;
Tetsuo Handa71336e012014-08-03 20:02:03 +0900433 unsigned pool_offset;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000434 struct ttm_page_pool *pool;
Ying Han1495f232011-05-24 17:12:27 -0700435 int shrink_pages = sc->nr_to_scan;
Dave Chinner7dc19d52013-08-28 10:18:11 +1000436 unsigned long freed = 0;
Roger He750a2502017-11-21 09:37:52 +0800437 unsigned int nr_free_pool;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000438
Tetsuo Handa71336e012014-08-03 20:02:03 +0900439 if (!mutex_trylock(&lock))
440 return SHRINK_STOP;
441 pool_offset = ++start_pool % NUM_POOLS;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000442 /* select start pool in round robin fashion */
443 for (i = 0; i < NUM_POOLS; ++i) {
444 unsigned nr_free = shrink_pages;
Roger He1bfcbad2017-11-21 16:47:16 +0800445 unsigned page_nr;
446
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000447 if (shrink_pages == 0)
448 break;
Roger He750a2502017-11-21 09:37:52 +0800449
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200450 pool = &_manager->pools[(i + pool_offset)%NUM_POOLS];
Roger He1bfcbad2017-11-21 16:47:16 +0800451 page_nr = (1 << pool->order);
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900452 /* OK to use static buffer since global mutex is held. */
Roger He1bfcbad2017-11-21 16:47:16 +0800453 nr_free_pool = roundup(nr_free, page_nr) >> pool->order;
Roger He750a2502017-11-21 09:37:52 +0800454 shrink_pages = ttm_page_pool_free(pool, nr_free_pool, true);
Roger He1bfcbad2017-11-21 16:47:16 +0800455 freed += (nr_free_pool - shrink_pages) << pool->order;
456 if (freed >= sc->nr_to_scan)
457 break;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000458 }
Tetsuo Handa71336e012014-08-03 20:02:03 +0900459 mutex_unlock(&lock);
Dave Chinner7dc19d52013-08-28 10:18:11 +1000460 return freed;
461}
462
463
464static unsigned long
465ttm_pool_shrink_count(struct shrinker *shrink, struct shrink_control *sc)
466{
467 unsigned i;
468 unsigned long count = 0;
Roger He750a2502017-11-21 09:37:52 +0800469 struct ttm_page_pool *pool;
Dave Chinner7dc19d52013-08-28 10:18:11 +1000470
Roger He750a2502017-11-21 09:37:52 +0800471 for (i = 0; i < NUM_POOLS; ++i) {
472 pool = &_manager->pools[i];
473 count += (pool->npages << pool->order);
474 }
Dave Chinner7dc19d52013-08-28 10:18:11 +1000475
476 return count;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000477}
478
479static void ttm_pool_mm_shrink_init(struct ttm_pool_manager *manager)
480{
Dave Chinner7dc19d52013-08-28 10:18:11 +1000481 manager->mm_shrink.count_objects = ttm_pool_shrink_count;
482 manager->mm_shrink.scan_objects = ttm_pool_shrink_scan;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000483 manager->mm_shrink.seeks = 1;
484 register_shrinker(&manager->mm_shrink);
485}
486
487static void ttm_pool_mm_shrink_fini(struct ttm_pool_manager *manager)
488{
489 unregister_shrinker(&manager->mm_shrink);
490}
491
492static int ttm_set_pages_caching(struct page **pages,
493 enum ttm_caching_state cstate, unsigned cpages)
494{
495 int r = 0;
496 /* Set page caching */
497 switch (cstate) {
498 case tt_uncached:
499 r = set_pages_array_uc(pages, cpages);
500 if (r)
Joe Perches25d04792012-03-16 21:43:50 -0700501 pr_err("Failed to set %d pages to uc!\n", cpages);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000502 break;
503 case tt_wc:
504 r = set_pages_array_wc(pages, cpages);
505 if (r)
Joe Perches25d04792012-03-16 21:43:50 -0700506 pr_err("Failed to set %d pages to wc!\n", cpages);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000507 break;
508 default:
509 break;
510 }
511 return r;
512}
513
514/**
515 * Free pages the pages that failed to change the caching state. If there is
516 * any pages that have changed their caching state already put them to the
517 * pool.
518 */
519static void ttm_handle_caching_state_failure(struct list_head *pages,
520 int ttm_flags, enum ttm_caching_state cstate,
521 struct page **failed_pages, unsigned cpages)
522{
523 unsigned i;
Thomas Hellstrom4abe4382010-05-26 16:21:04 +0200524 /* Failed pages have to be freed */
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000525 for (i = 0; i < cpages; ++i) {
526 list_del(&failed_pages[i]->lru);
527 __free_page(failed_pages[i]);
528 }
529}
530
531/**
532 * Allocate new pages with correct caching.
533 *
534 * This function is reentrant if caller updates count depending on number of
535 * pages returned in pages array.
536 */
Daniel J Blueman0e57a3c2010-09-22 17:45:45 +0100537static int ttm_alloc_new_pages(struct list_head *pages, gfp_t gfp_flags,
Christian König6ed4e2e2017-10-05 14:27:34 +0200538 int ttm_flags, enum ttm_caching_state cstate,
539 unsigned count, unsigned order)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000540{
541 struct page **caching_array;
542 struct page *p;
543 int r = 0;
Christian König6ed4e2e2017-10-05 14:27:34 +0200544 unsigned i, j, cpages;
545 unsigned npages = 1 << order;
Roger Hef991c0e2017-11-21 09:58:26 +0800546 unsigned max_cpages = min(count, (unsigned)NUM_PAGES_TO_ALLOC);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000547
548 /* allocate array for page caching change */
549 caching_array = kmalloc(max_cpages*sizeof(struct page *), GFP_KERNEL);
550
551 if (!caching_array) {
Michel Dänzer767601d2017-11-03 16:00:35 +0100552 pr_debug("Unable to allocate table for new pages\n");
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000553 return -ENOMEM;
554 }
555
556 for (i = 0, cpages = 0; i < count; ++i) {
Christian König6ed4e2e2017-10-05 14:27:34 +0200557 p = alloc_pages(gfp_flags, order);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000558
559 if (!p) {
Michel Dänzer767601d2017-11-03 16:00:35 +0100560 pr_debug("Unable to get page %u\n", i);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000561
562 /* store already allocated pages in the pool after
563 * setting the caching state */
564 if (cpages) {
Thomas Hellstrom4abe4382010-05-26 16:21:04 +0200565 r = ttm_set_pages_caching(caching_array,
566 cstate, cpages);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000567 if (r)
568 ttm_handle_caching_state_failure(pages,
569 ttm_flags, cstate,
570 caching_array, cpages);
571 }
572 r = -ENOMEM;
573 goto out;
574 }
575
Christian König6ed4e2e2017-10-05 14:27:34 +0200576 list_add(&p->lru, pages);
577
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000578#ifdef CONFIG_HIGHMEM
579 /* gfp flags of highmem page should never be dma32 so we
580 * we should be fine in such case
581 */
Christian König6ed4e2e2017-10-05 14:27:34 +0200582 if (PageHighMem(p))
583 continue;
584
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000585#endif
Christian König6ed4e2e2017-10-05 14:27:34 +0200586 for (j = 0; j < npages; ++j) {
587 caching_array[cpages++] = p++;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000588 if (cpages == max_cpages) {
589
590 r = ttm_set_pages_caching(caching_array,
591 cstate, cpages);
592 if (r) {
593 ttm_handle_caching_state_failure(pages,
594 ttm_flags, cstate,
595 caching_array, cpages);
596 goto out;
597 }
598 cpages = 0;
599 }
600 }
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000601 }
602
603 if (cpages) {
604 r = ttm_set_pages_caching(caching_array, cstate, cpages);
605 if (r)
606 ttm_handle_caching_state_failure(pages,
607 ttm_flags, cstate,
608 caching_array, cpages);
609 }
610out:
611 kfree(caching_array);
612
613 return r;
614}
615
616/**
Konrad Rzeszutek Wilk0d74f862011-06-08 17:06:15 +0000617 * Fill the given pool if there aren't enough pages and the requested number of
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000618 * pages is small.
619 */
Christian König6ed4e2e2017-10-05 14:27:34 +0200620static void ttm_page_pool_fill_locked(struct ttm_page_pool *pool, int ttm_flags,
621 enum ttm_caching_state cstate,
622 unsigned count, unsigned long *irq_flags)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000623{
624 struct page *p;
625 int r;
626 unsigned cpages = 0;
627 /**
628 * Only allow one pool fill operation at a time.
629 * If pool doesn't have enough pages for the allocation new pages are
630 * allocated from outside of pool.
631 */
632 if (pool->fill_lock)
633 return;
634
635 pool->fill_lock = true;
636
Konrad Rzeszutek Wilk0d74f862011-06-08 17:06:15 +0000637 /* If allocation request is small and there are not enough
638 * pages in a pool we fill the pool up first. */
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200639 if (count < _manager->options.small
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000640 && count > pool->npages) {
641 struct list_head new_pages;
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200642 unsigned alloc_size = _manager->options.alloc_size;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000643
644 /**
645 * Can't change page caching if in irqsave context. We have to
646 * drop the pool->lock.
647 */
648 spin_unlock_irqrestore(&pool->lock, *irq_flags);
649
650 INIT_LIST_HEAD(&new_pages);
651 r = ttm_alloc_new_pages(&new_pages, pool->gfp_flags, ttm_flags,
Christian König6ed4e2e2017-10-05 14:27:34 +0200652 cstate, alloc_size, 0);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000653 spin_lock_irqsave(&pool->lock, *irq_flags);
654
655 if (!r) {
656 list_splice(&new_pages, &pool->list);
Pauli Nieminen07458662010-04-01 12:44:58 +0000657 ++pool->nrefills;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000658 pool->npages += alloc_size;
659 } else {
Michel Dänzer767601d2017-11-03 16:00:35 +0100660 pr_debug("Failed to fill pool (%p)\n", pool);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000661 /* If we have any pages left put them to the pool. */
Xiangliang.Yu9afae272017-08-16 14:25:51 +0800662 list_for_each_entry(p, &new_pages, lru) {
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000663 ++cpages;
664 }
665 list_splice(&new_pages, &pool->list);
666 pool->npages += cpages;
667 }
668
669 }
670 pool->fill_lock = false;
671}
672
673/**
Christian König8593e9b2017-09-21 11:28:25 +0200674 * Allocate pages from the pool and put them on the return list.
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000675 *
Christian König8593e9b2017-09-21 11:28:25 +0200676 * @return zero for success or negative error code.
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000677 */
Christian König8593e9b2017-09-21 11:28:25 +0200678static int ttm_page_pool_get_pages(struct ttm_page_pool *pool,
679 struct list_head *pages,
680 int ttm_flags,
681 enum ttm_caching_state cstate,
Christian König6ed4e2e2017-10-05 14:27:34 +0200682 unsigned count, unsigned order)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000683{
684 unsigned long irq_flags;
685 struct list_head *p;
686 unsigned i;
Christian König8593e9b2017-09-21 11:28:25 +0200687 int r = 0;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000688
689 spin_lock_irqsave(&pool->lock, irq_flags);
Christian König6ed4e2e2017-10-05 14:27:34 +0200690 if (!order)
691 ttm_page_pool_fill_locked(pool, ttm_flags, cstate, count,
692 &irq_flags);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000693
694 if (count >= pool->npages) {
695 /* take all pages from the pool */
696 list_splice_init(&pool->list, pages);
697 count -= pool->npages;
698 pool->npages = 0;
699 goto out;
700 }
701 /* find the last pages to include for requested number of pages. Split
Konrad Rzeszutek Wilk0d74f862011-06-08 17:06:15 +0000702 * pool to begin and halve it to reduce search space. */
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000703 if (count <= pool->npages/2) {
704 i = 0;
705 list_for_each(p, &pool->list) {
706 if (++i == count)
707 break;
708 }
709 } else {
710 i = pool->npages + 1;
711 list_for_each_prev(p, &pool->list) {
712 if (--i == count)
713 break;
714 }
715 }
Konrad Rzeszutek Wilk0d74f862011-06-08 17:06:15 +0000716 /* Cut 'count' number of pages from the pool */
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000717 list_cut_position(pages, &pool->list, p);
718 pool->npages -= count;
719 count = 0;
720out:
721 spin_unlock_irqrestore(&pool->lock, irq_flags);
Christian König8593e9b2017-09-21 11:28:25 +0200722
723 /* clear the pages coming from the pool if requested */
724 if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC) {
725 struct page *page;
726
727 list_for_each_entry(page, pages, lru) {
728 if (PageHighMem(page))
729 clear_highpage(page);
730 else
731 clear_page(page_address(page));
732 }
733 }
734
735 /* If pool didn't have enough pages allocate new one. */
736 if (count) {
737 gfp_t gfp_flags = pool->gfp_flags;
738
739 /* set zero flag for page allocation if required */
740 if (ttm_flags & TTM_PAGE_FLAG_ZERO_ALLOC)
741 gfp_flags |= __GFP_ZERO;
742
743 /* ttm_alloc_new_pages doesn't reference pool so we can run
744 * multiple requests in parallel.
745 **/
746 r = ttm_alloc_new_pages(pages, gfp_flags, ttm_flags, cstate,
Christian König6ed4e2e2017-10-05 14:27:34 +0200747 count, order);
Christian König8593e9b2017-09-21 11:28:25 +0200748 }
749
750 return r;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000751}
752
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500753/* Put all pages in pages list to correct pool to wait for reuse */
754static void ttm_put_pages(struct page **pages, unsigned npages, int flags,
755 enum ttm_caching_state cstate)
756{
Christian König6ed4e2e2017-10-05 14:27:34 +0200757 struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
Tom St Denis7d0a4282017-10-12 07:25:08 -0400758#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Christian König6ed4e2e2017-10-05 14:27:34 +0200759 struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
Tom St Denis7d0a4282017-10-12 07:25:08 -0400760#endif
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500761 unsigned long irq_flags;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500762 unsigned i;
763
764 if (pool == NULL) {
765 /* No pool for this memory type so free the pages */
Christian König0284f1e2017-09-20 15:06:12 +0200766 i = 0;
767 while (i < npages) {
Christian König5c42c642017-10-12 19:28:42 +0200768#ifdef CONFIG_TRANSPARENT_HUGEPAGE
769 struct page *p = pages[i];
770#endif
771 unsigned order = 0, j;
Christian König0284f1e2017-09-20 15:06:12 +0200772
773 if (!pages[i]) {
774 ++i;
775 continue;
776 }
777
Christian König5c42c642017-10-12 19:28:42 +0200778#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Dave Airlie33d22c22017-11-23 12:12:17 +1000779 if (!(flags & TTM_PAGE_FLAG_DMA32)) {
780 for (j = 0; j < HPAGE_PMD_NR; ++j)
781 if (p++ != pages[i + j])
782 break;
Christian König5c42c642017-10-12 19:28:42 +0200783
Dave Airlie33d22c22017-11-23 12:12:17 +1000784 if (j == HPAGE_PMD_NR)
785 order = HPAGE_PMD_ORDER;
786 }
Christian König5c42c642017-10-12 19:28:42 +0200787#endif
788
Christian König0284f1e2017-09-20 15:06:12 +0200789 if (page_count(pages[i]) != 1)
790 pr_err("Erroneous page count. Leaking pages.\n");
Christian König0284f1e2017-09-20 15:06:12 +0200791 __free_pages(pages[i], order);
792
Christian König5c42c642017-10-12 19:28:42 +0200793 j = 1 << order;
794 while (j) {
Christian König0284f1e2017-09-20 15:06:12 +0200795 pages[i++] = NULL;
Christian König5c42c642017-10-12 19:28:42 +0200796 --j;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500797 }
798 }
799 return;
800 }
801
Christian König6ed4e2e2017-10-05 14:27:34 +0200802 i = 0;
803#ifdef CONFIG_TRANSPARENT_HUGEPAGE
804 if (huge) {
805 unsigned max_size, n2free;
806
807 spin_lock_irqsave(&huge->lock, irq_flags);
808 while (i < npages) {
809 struct page *p = pages[i];
810 unsigned j;
811
812 if (!p)
813 break;
814
815 for (j = 0; j < HPAGE_PMD_NR; ++j)
816 if (p++ != pages[i + j])
817 break;
818
819 if (j != HPAGE_PMD_NR)
820 break;
821
822 list_add_tail(&pages[i]->lru, &huge->list);
823
824 for (j = 0; j < HPAGE_PMD_NR; ++j)
825 pages[i++] = NULL;
826 huge->npages++;
827 }
828
829 /* Check that we don't go over the pool limit */
830 max_size = _manager->options.max_size;
831 max_size /= HPAGE_PMD_NR;
832 if (huge->npages > max_size)
833 n2free = huge->npages - max_size;
834 else
835 n2free = 0;
836 spin_unlock_irqrestore(&huge->lock, irq_flags);
837 if (n2free)
838 ttm_page_pool_free(huge, n2free, false);
839 }
840#endif
841
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500842 spin_lock_irqsave(&pool->lock, irq_flags);
Christian König6ed4e2e2017-10-05 14:27:34 +0200843 while (i < npages) {
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500844 if (pages[i]) {
845 if (page_count(pages[i]) != 1)
Joe Perches25d04792012-03-16 21:43:50 -0700846 pr_err("Erroneous page count. Leaking pages.\n");
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500847 list_add_tail(&pages[i]->lru, &pool->list);
848 pages[i] = NULL;
849 pool->npages++;
850 }
Christian König6ed4e2e2017-10-05 14:27:34 +0200851 ++i;
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500852 }
853 /* Check that we don't go over the pool limit */
854 npages = 0;
855 if (pool->npages > _manager->options.max_size) {
856 npages = pool->npages - _manager->options.max_size;
857 /* free at least NUM_PAGES_TO_ALLOC number of pages
858 * to reduce calls to set_memory_wb */
859 if (npages < NUM_PAGES_TO_ALLOC)
860 npages = NUM_PAGES_TO_ALLOC;
861 }
862 spin_unlock_irqrestore(&pool->lock, irq_flags);
863 if (npages)
Tetsuo Handa881fdaa2014-11-13 22:43:23 +0900864 ttm_page_pool_free(pool, npages, false);
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500865}
866
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000867/*
868 * On success pages list will hold count number of correctly
869 * cached pages.
870 */
Jerome Glisse8e7e7052011-11-09 17:15:26 -0500871static int ttm_get_pages(struct page **pages, unsigned npages, int flags,
872 enum ttm_caching_state cstate)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000873{
Christian König6ed4e2e2017-10-05 14:27:34 +0200874 struct ttm_page_pool *pool = ttm_get_pool(flags, false, cstate);
Tom St Denis7d0a4282017-10-12 07:25:08 -0400875#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Christian König6ed4e2e2017-10-05 14:27:34 +0200876 struct ttm_page_pool *huge = ttm_get_pool(flags, true, cstate);
Tom St Denis7d0a4282017-10-12 07:25:08 -0400877#endif
Jerome Glisse822c4d92011-11-10 18:24:09 -0500878 struct list_head plist;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000879 struct page *p = NULL;
Jerome Glisse822c4d92011-11-10 18:24:09 -0500880 unsigned count;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000881 int r;
882
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000883 /* No pool for cached pages */
884 if (pool == NULL) {
Christian König8593e9b2017-09-21 11:28:25 +0200885 gfp_t gfp_flags = GFP_USER;
Tom St Denis7d0a4282017-10-12 07:25:08 -0400886 unsigned i;
887#ifdef CONFIG_TRANSPARENT_HUGEPAGE
888 unsigned j;
889#endif
Christian König0284f1e2017-09-20 15:06:12 +0200890
Christian König8593e9b2017-09-21 11:28:25 +0200891 /* set zero flag for page allocation if required */
892 if (flags & TTM_PAGE_FLAG_ZERO_ALLOC)
893 gfp_flags |= __GFP_ZERO;
894
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000895 if (flags & TTM_PAGE_FLAG_DMA32)
896 gfp_flags |= GFP_DMA32;
897 else
Thomas Hellstrome8613c02010-05-26 16:21:03 +0200898 gfp_flags |= GFP_HIGHUSER;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000899
Christian König0284f1e2017-09-20 15:06:12 +0200900 i = 0;
901#ifdef CONFIG_TRANSPARENT_HUGEPAGE
Dave Airlie33d22c22017-11-23 12:12:17 +1000902 if (!(gfp_flags & GFP_DMA32)) {
903 while (npages >= HPAGE_PMD_NR) {
904 gfp_t huge_flags = gfp_flags;
Christian König0284f1e2017-09-20 15:06:12 +0200905
Dave Airlie33d22c22017-11-23 12:12:17 +1000906 huge_flags |= GFP_TRANSHUGE;
907 huge_flags &= ~__GFP_MOVABLE;
908 huge_flags &= ~__GFP_COMP;
909 p = alloc_pages(huge_flags, HPAGE_PMD_ORDER);
910 if (!p)
911 break;
Christian König0284f1e2017-09-20 15:06:12 +0200912
Dave Airlie33d22c22017-11-23 12:12:17 +1000913 for (j = 0; j < HPAGE_PMD_NR; ++j)
914 pages[i++] = p++;
Christian König0284f1e2017-09-20 15:06:12 +0200915
Dave Airlie33d22c22017-11-23 12:12:17 +1000916 npages -= HPAGE_PMD_NR;
917 }
Christian König0284f1e2017-09-20 15:06:12 +0200918 }
919#endif
920
921 while (npages) {
Dave Airlied87dfdb2011-04-13 09:15:09 +1000922 p = alloc_page(gfp_flags);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000923 if (!p) {
Michel Dänzer767601d2017-11-03 16:00:35 +0100924 pr_debug("Unable to allocate page\n");
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000925 return -ENOMEM;
926 }
Dave Airlied87dfdb2011-04-13 09:15:09 +1000927
Christian König0284f1e2017-09-20 15:06:12 +0200928 pages[i++] = p;
929 --npages;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000930 }
931 return 0;
932 }
933
Jerome Glisse822c4d92011-11-10 18:24:09 -0500934 count = 0;
Christian König6ed4e2e2017-10-05 14:27:34 +0200935
936#ifdef CONFIG_TRANSPARENT_HUGEPAGE
937 if (huge && npages >= HPAGE_PMD_NR) {
938 INIT_LIST_HEAD(&plist);
939 ttm_page_pool_get_pages(huge, &plist, flags, cstate,
940 npages / HPAGE_PMD_NR,
941 HPAGE_PMD_ORDER);
942
943 list_for_each_entry(p, &plist, lru) {
944 unsigned j;
945
946 for (j = 0; j < HPAGE_PMD_NR; ++j)
947 pages[count++] = &p[j];
948 }
949 }
950#endif
951
952 INIT_LIST_HEAD(&plist);
953 r = ttm_page_pool_get_pages(pool, &plist, flags, cstate,
954 npages - count, 0);
955
Christian König8593e9b2017-09-21 11:28:25 +0200956 list_for_each_entry(p, &plist, lru)
Jerome Glisse822c4d92011-11-10 18:24:09 -0500957 pages[count++] = p;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000958
Christian König8593e9b2017-09-21 11:28:25 +0200959 if (r) {
960 /* If there is any pages in the list put them back to
961 * the pool.
962 */
Michel Dänzer767601d2017-11-03 16:00:35 +0100963 pr_debug("Failed to allocate extra pages for large request\n");
Christian König8593e9b2017-09-21 11:28:25 +0200964 ttm_put_pages(pages, count, flags, cstate);
965 return r;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000966 }
967
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000968 return 0;
969}
970
Dave Airlie3b9c2142014-07-22 12:55:39 +1000971static void ttm_page_pool_init_locked(struct ttm_page_pool *pool, gfp_t flags,
Roger He750a2502017-11-21 09:37:52 +0800972 char *name, unsigned int order)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000973{
974 spin_lock_init(&pool->lock);
975 pool->fill_lock = false;
976 INIT_LIST_HEAD(&pool->list);
Pauli Nieminen07458662010-04-01 12:44:58 +0000977 pool->npages = pool->nfrees = 0;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000978 pool->gfp_flags = flags;
Pauli Nieminen07458662010-04-01 12:44:58 +0000979 pool->name = name;
Roger He750a2502017-11-21 09:37:52 +0800980 pool->order = order;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000981}
982
Pauli Nieminenc96af792010-04-01 12:45:03 +0000983int ttm_page_alloc_init(struct ttm_mem_global *glob, unsigned max_pages)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000984{
Pauli Nieminenc96af792010-04-01 12:45:03 +0000985 int ret;
Roger He750a2502017-11-21 09:37:52 +0800986#ifdef CONFIG_TRANSPARENT_HUGEPAGE
987 unsigned order = HPAGE_PMD_ORDER;
988#else
989 unsigned order = 0;
990#endif
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200991
992 WARN_ON(_manager);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000993
Joe Perches25d04792012-03-16 21:43:50 -0700994 pr_info("Initializing pool allocator\n");
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000995
Francisco Jerez5870a4d2010-07-04 04:03:07 +0200996 _manager = kzalloc(sizeof(*_manager), GFP_KERNEL);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000997
Roger He750a2502017-11-21 09:37:52 +0800998 ttm_page_pool_init_locked(&_manager->wc_pool, GFP_HIGHUSER, "wc", 0);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +0000999
Roger He750a2502017-11-21 09:37:52 +08001000 ttm_page_pool_init_locked(&_manager->uc_pool, GFP_HIGHUSER, "uc", 0);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +00001001
Francisco Jerez5870a4d2010-07-04 04:03:07 +02001002 ttm_page_pool_init_locked(&_manager->wc_pool_dma32,
Roger He750a2502017-11-21 09:37:52 +08001003 GFP_USER | GFP_DMA32, "wc dma", 0);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +00001004
Francisco Jerez5870a4d2010-07-04 04:03:07 +02001005 ttm_page_pool_init_locked(&_manager->uc_pool_dma32,
Roger He750a2502017-11-21 09:37:52 +08001006 GFP_USER | GFP_DMA32, "uc dma", 0);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +00001007
Christian König6ed4e2e2017-10-05 14:27:34 +02001008 ttm_page_pool_init_locked(&_manager->wc_pool_huge,
1009 GFP_TRANSHUGE & ~(__GFP_MOVABLE | __GFP_COMP),
Roger He750a2502017-11-21 09:37:52 +08001010 "wc huge", order);
Christian König6ed4e2e2017-10-05 14:27:34 +02001011
1012 ttm_page_pool_init_locked(&_manager->uc_pool_huge,
1013 GFP_TRANSHUGE & ~(__GFP_MOVABLE | __GFP_COMP)
Roger He750a2502017-11-21 09:37:52 +08001014 , "uc huge", order);
Christian König6ed4e2e2017-10-05 14:27:34 +02001015
Francisco Jerez5870a4d2010-07-04 04:03:07 +02001016 _manager->options.max_size = max_pages;
1017 _manager->options.small = SMALL_ALLOCATION;
1018 _manager->options.alloc_size = NUM_PAGES_TO_ALLOC;
1019
1020 ret = kobject_init_and_add(&_manager->kobj, &ttm_pool_kobj_type,
1021 &glob->kobj, "pool");
Pauli Nieminenc96af792010-04-01 12:45:03 +00001022 if (unlikely(ret != 0)) {
Francisco Jerez5870a4d2010-07-04 04:03:07 +02001023 kobject_put(&_manager->kobj);
1024 _manager = NULL;
Pauli Nieminenc96af792010-04-01 12:45:03 +00001025 return ret;
1026 }
1027
Francisco Jerez5870a4d2010-07-04 04:03:07 +02001028 ttm_pool_mm_shrink_init(_manager);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +00001029
1030 return 0;
1031}
1032
Daniel J Blueman0e57a3c2010-09-22 17:45:45 +01001033void ttm_page_alloc_fini(void)
Pauli Nieminen1403b1a2010-04-01 12:44:57 +00001034{
1035 int i;
1036
Joe Perches25d04792012-03-16 21:43:50 -07001037 pr_info("Finalizing pool allocator\n");
Francisco Jerez5870a4d2010-07-04 04:03:07 +02001038 ttm_pool_mm_shrink_fini(_manager);
Pauli Nieminen1403b1a2010-04-01 12:44:57 +00001039
Tetsuo Handa881fdaa2014-11-13 22:43:23 +09001040 /* OK to use static buffer since global mutex is no longer used. */
Pauli Nieminen1403b1a2010-04-01 12:44:57 +00001041 for (i = 0; i < NUM_POOLS; ++i)
Tetsuo Handa881fdaa2014-11-13 22:43:23 +09001042 ttm_page_pool_free(&_manager->pools[i], FREE_ALL_PAGES, true);
Pauli Nieminenc96af792010-04-01 12:45:03 +00001043
Francisco Jerez5870a4d2010-07-04 04:03:07 +02001044 kobject_put(&_manager->kobj);
1045 _manager = NULL;
Pauli Nieminen1403b1a2010-04-01 12:44:57 +00001046}
Pauli Nieminen07458662010-04-01 12:44:58 +00001047
Jerome Glisseb1e5f172011-11-02 23:59:28 -04001048int ttm_pool_populate(struct ttm_tt *ttm)
1049{
1050 struct ttm_mem_global *mem_glob = ttm->glob->mem_glob;
1051 unsigned i;
1052 int ret;
1053
1054 if (ttm->state != tt_unpopulated)
1055 return 0;
1056
Christian Königc6e839a2017-09-19 15:20:42 +02001057 ret = ttm_get_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
1058 ttm->caching_state);
1059 if (unlikely(ret != 0)) {
1060 ttm_pool_unpopulate(ttm);
1061 return ret;
1062 }
Jerome Glisseb1e5f172011-11-02 23:59:28 -04001063
Christian Königc6e839a2017-09-19 15:20:42 +02001064 for (i = 0; i < ttm->num_pages; ++i) {
Christian Königd188bfa2017-07-04 16:56:24 +02001065 ret = ttm_mem_global_alloc_page(mem_glob, ttm->pages[i],
1066 PAGE_SIZE);
Jerome Glisseb1e5f172011-11-02 23:59:28 -04001067 if (unlikely(ret != 0)) {
1068 ttm_pool_unpopulate(ttm);
1069 return -ENOMEM;
1070 }
1071 }
1072
1073 if (unlikely(ttm->page_flags & TTM_PAGE_FLAG_SWAPPED)) {
1074 ret = ttm_tt_swapin(ttm);
1075 if (unlikely(ret != 0)) {
1076 ttm_pool_unpopulate(ttm);
1077 return ret;
1078 }
1079 }
1080
1081 ttm->state = tt_unbound;
1082 return 0;
1083}
1084EXPORT_SYMBOL(ttm_pool_populate);
1085
1086void ttm_pool_unpopulate(struct ttm_tt *ttm)
1087{
1088 unsigned i;
1089
1090 for (i = 0; i < ttm->num_pages; ++i) {
Christian Königc6e839a2017-09-19 15:20:42 +02001091 if (!ttm->pages[i])
1092 continue;
1093
1094 ttm_mem_global_free_page(ttm->glob->mem_glob, ttm->pages[i],
1095 PAGE_SIZE);
Jerome Glisseb1e5f172011-11-02 23:59:28 -04001096 }
Christian Königc6e839a2017-09-19 15:20:42 +02001097 ttm_put_pages(ttm->pages, ttm->num_pages, ttm->page_flags,
1098 ttm->caching_state);
Jerome Glisseb1e5f172011-11-02 23:59:28 -04001099 ttm->state = tt_unpopulated;
1100}
1101EXPORT_SYMBOL(ttm_pool_unpopulate);
1102
Tom St Denisa4dec812017-08-18 10:04:57 -04001103int ttm_populate_and_map_pages(struct device *dev, struct ttm_dma_tt *tt)
1104{
Christian König6056a1a2017-09-20 14:07:02 +02001105 unsigned i, j;
Tom St Denisa4dec812017-08-18 10:04:57 -04001106 int r;
1107
1108 r = ttm_pool_populate(&tt->ttm);
1109 if (r)
1110 return r;
1111
Christian König6056a1a2017-09-20 14:07:02 +02001112 for (i = 0; i < tt->ttm.num_pages; ++i) {
1113 struct page *p = tt->ttm.pages[i];
1114 size_t num_pages = 1;
1115
1116 for (j = i + 1; j < tt->ttm.num_pages; ++j) {
1117 if (++p != tt->ttm.pages[j])
1118 break;
1119
1120 ++num_pages;
1121 }
1122
Tom St Denisa4dec812017-08-18 10:04:57 -04001123 tt->dma_address[i] = dma_map_page(dev, tt->ttm.pages[i],
Christian König6056a1a2017-09-20 14:07:02 +02001124 0, num_pages * PAGE_SIZE,
Tom St Denisa4dec812017-08-18 10:04:57 -04001125 DMA_BIDIRECTIONAL);
1126 if (dma_mapping_error(dev, tt->dma_address[i])) {
1127 while (i--) {
1128 dma_unmap_page(dev, tt->dma_address[i],
1129 PAGE_SIZE, DMA_BIDIRECTIONAL);
1130 tt->dma_address[i] = 0;
1131 }
1132 ttm_pool_unpopulate(&tt->ttm);
1133 return -EFAULT;
1134 }
Christian König6056a1a2017-09-20 14:07:02 +02001135
1136 for (j = 1; j < num_pages; ++j) {
1137 tt->dma_address[i + 1] = tt->dma_address[i] + PAGE_SIZE;
1138 ++i;
1139 }
Tom St Denisa4dec812017-08-18 10:04:57 -04001140 }
1141 return 0;
1142}
1143EXPORT_SYMBOL(ttm_populate_and_map_pages);
1144
1145void ttm_unmap_and_unpopulate_pages(struct device *dev, struct ttm_dma_tt *tt)
1146{
Christian König6056a1a2017-09-20 14:07:02 +02001147 unsigned i, j;
1148
1149 for (i = 0; i < tt->ttm.num_pages;) {
1150 struct page *p = tt->ttm.pages[i];
1151 size_t num_pages = 1;
1152
1153 if (!tt->dma_address[i] || !tt->ttm.pages[i]) {
1154 ++i;
1155 continue;
Tom St Denisa4dec812017-08-18 10:04:57 -04001156 }
Christian König6056a1a2017-09-20 14:07:02 +02001157
1158 for (j = i + 1; j < tt->ttm.num_pages; ++j) {
1159 if (++p != tt->ttm.pages[j])
1160 break;
1161
1162 ++num_pages;
1163 }
1164
1165 dma_unmap_page(dev, tt->dma_address[i], num_pages * PAGE_SIZE,
1166 DMA_BIDIRECTIONAL);
1167
1168 i += num_pages;
Tom St Denisa4dec812017-08-18 10:04:57 -04001169 }
1170 ttm_pool_unpopulate(&tt->ttm);
1171}
1172EXPORT_SYMBOL(ttm_unmap_and_unpopulate_pages);
1173
Pauli Nieminen07458662010-04-01 12:44:58 +00001174int ttm_page_alloc_debugfs(struct seq_file *m, void *data)
1175{
1176 struct ttm_page_pool *p;
1177 unsigned i;
1178 char *h[] = {"pool", "refills", "pages freed", "size"};
Francisco Jerez5870a4d2010-07-04 04:03:07 +02001179 if (!_manager) {
Pauli Nieminen07458662010-04-01 12:44:58 +00001180 seq_printf(m, "No pool allocator running.\n");
1181 return 0;
1182 }
Christian König6ed4e2e2017-10-05 14:27:34 +02001183 seq_printf(m, "%7s %12s %13s %8s\n",
Pauli Nieminen07458662010-04-01 12:44:58 +00001184 h[0], h[1], h[2], h[3]);
1185 for (i = 0; i < NUM_POOLS; ++i) {
Francisco Jerez5870a4d2010-07-04 04:03:07 +02001186 p = &_manager->pools[i];
Pauli Nieminen07458662010-04-01 12:44:58 +00001187
Christian König6ed4e2e2017-10-05 14:27:34 +02001188 seq_printf(m, "%7s %12ld %13ld %8d\n",
Pauli Nieminen07458662010-04-01 12:44:58 +00001189 p->name, p->nrefills,
1190 p->nfrees, p->npages);
1191 }
1192 return 0;
1193}
1194EXPORT_SYMBOL(ttm_page_alloc_debugfs);