2 * drivers/staging/android/ion/ion_mem_pool.c
4 * Copyright (C) 2011 Google, Inc.
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
17 #include <linux/debugfs.h>
18 #include <linux/dma-mapping.h>
19 #include <linux/err.h>
21 #include <linux/list.h>
22 #include <linux/init.h>
23 #include <linux/slab.h>
24 #include <linux/swap.h>
27 static void *ion_page_pool_alloc_pages(struct ion_page_pool *pool)
29 struct page *page = alloc_pages(pool->gfp_mask, pool->order);
34 ion_pages_sync_for_device(NULL, page, PAGE_SIZE << pool->order,
39 static void ion_page_pool_free_pages(struct ion_page_pool *pool,
42 __free_pages(page, pool->order);
45 static int ion_page_pool_add(struct ion_page_pool *pool, struct page *page)
47 mutex_lock(&pool->mutex);
48 if (PageHighMem(page)) {
49 list_add_tail(&page->lru, &pool->high_items);
52 list_add_tail(&page->lru, &pool->low_items);
55 mutex_unlock(&pool->mutex);
59 static struct page *ion_page_pool_remove(struct ion_page_pool *pool, bool high)
64 BUG_ON(!pool->high_count);
65 page = list_first_entry(&pool->high_items, struct page, lru);
68 BUG_ON(!pool->low_count);
69 page = list_first_entry(&pool->low_items, struct page, lru);
77 struct page *ion_page_pool_alloc(struct ion_page_pool *pool)
79 struct page *page = NULL;
83 mutex_lock(&pool->mutex);
85 page = ion_page_pool_remove(pool, true);
86 else if (pool->low_count)
87 page = ion_page_pool_remove(pool, false);
88 mutex_unlock(&pool->mutex);
91 page = ion_page_pool_alloc_pages(pool);
96 void ion_page_pool_free(struct ion_page_pool *pool, struct page *page)
100 BUG_ON(pool->order != compound_order(page));
102 ret = ion_page_pool_add(pool, page);
104 ion_page_pool_free_pages(pool, page);
107 static int ion_page_pool_total(struct ion_page_pool *pool, bool high)
109 int count = pool->low_count;
112 count += pool->high_count;
114 return count << pool->order;
117 int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
123 if (current_is_kswapd())
126 high = !!(gfp_mask & __GFP_HIGHMEM);
129 return ion_page_pool_total(pool, high);
131 while (freed < nr_to_scan) {
134 mutex_lock(&pool->mutex);
135 if (pool->low_count) {
136 page = ion_page_pool_remove(pool, false);
137 } else if (high && pool->high_count) {
138 page = ion_page_pool_remove(pool, true);
140 mutex_unlock(&pool->mutex);
143 mutex_unlock(&pool->mutex);
144 ion_page_pool_free_pages(pool, page);
145 freed += (1 << pool->order);
151 struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order,
154 struct ion_page_pool *pool = kmalloc(sizeof(*pool), GFP_KERNEL);
158 pool->high_count = 0;
160 INIT_LIST_HEAD(&pool->low_items);
161 INIT_LIST_HEAD(&pool->high_items);
162 pool->gfp_mask = gfp_mask | __GFP_COMP;
164 mutex_init(&pool->mutex);
165 plist_node_init(&pool->list, order);
172 void ion_page_pool_destroy(struct ion_page_pool *pool)
177 static int __init ion_page_pool_init(void)
181 device_initcall(ion_page_pool_init);