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/module.h>
23 #include <linux/slab.h>
26 struct ion_page_pool_item {
28 struct list_head list;
31 static void *ion_page_pool_alloc_pages(struct ion_page_pool *pool)
33 struct page *page = alloc_pages(pool->gfp_mask, pool->order);
37 ion_pages_sync_for_device(NULL, page, PAGE_SIZE << pool->order,
42 static void ion_page_pool_free_pages(struct ion_page_pool *pool,
45 __free_pages(page, pool->order);
48 static int ion_page_pool_add(struct ion_page_pool *pool, struct page *page)
50 struct ion_page_pool_item *item;
52 item = kmalloc(sizeof(struct ion_page_pool_item), GFP_KERNEL);
56 mutex_lock(&pool->mutex);
58 if (PageHighMem(page)) {
59 list_add_tail(&item->list, &pool->high_items);
62 list_add_tail(&item->list, &pool->low_items);
65 mutex_unlock(&pool->mutex);
69 static struct page *ion_page_pool_remove(struct ion_page_pool *pool, bool high)
71 struct ion_page_pool_item *item;
75 BUG_ON(!pool->high_count);
76 item = list_first_entry(&pool->high_items,
77 struct ion_page_pool_item, list);
80 BUG_ON(!pool->low_count);
81 item = list_first_entry(&pool->low_items,
82 struct ion_page_pool_item, list);
86 list_del(&item->list);
92 void *ion_page_pool_alloc(struct ion_page_pool *pool)
94 struct page *page = NULL;
98 mutex_lock(&pool->mutex);
100 page = ion_page_pool_remove(pool, true);
101 else if (pool->low_count)
102 page = ion_page_pool_remove(pool, false);
103 mutex_unlock(&pool->mutex);
106 page = ion_page_pool_alloc_pages(pool);
111 void ion_page_pool_free(struct ion_page_pool *pool, struct page *page)
115 ret = ion_page_pool_add(pool, page);
117 ion_page_pool_free_pages(pool, page);
120 static int ion_page_pool_total(struct ion_page_pool *pool, bool high)
124 total += high ? (pool->high_count + pool->low_count) *
126 pool->low_count * (1 << pool->order);
130 int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
137 high = !!(gfp_mask & __GFP_HIGHMEM);
140 return ion_page_pool_total(pool, high);
142 for (i = 0; i < nr_to_scan; i++) {
145 mutex_lock(&pool->mutex);
146 if (pool->low_count) {
147 page = ion_page_pool_remove(pool, false);
148 } else if (high && pool->high_count) {
149 page = ion_page_pool_remove(pool, true);
151 mutex_unlock(&pool->mutex);
154 mutex_unlock(&pool->mutex);
155 ion_page_pool_free_pages(pool, page);
156 nr_freed += (1 << pool->order);
162 struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order)
164 struct ion_page_pool *pool = kmalloc(sizeof(struct ion_page_pool),
168 pool->high_count = 0;
170 INIT_LIST_HEAD(&pool->low_items);
171 INIT_LIST_HEAD(&pool->high_items);
172 pool->gfp_mask = gfp_mask;
174 mutex_init(&pool->mutex);
175 plist_node_init(&pool->list, order);
180 void ion_page_pool_destroy(struct ion_page_pool *pool)
185 static int __init ion_page_pool_init(void)
190 static void __exit ion_page_pool_exit(void)
194 module_init(ion_page_pool_init);
195 module_exit(ion_page_pool_exit);