ion_page_pool.c revision ce3d10933a3e2e1d2cb3b9ae91b6b945f189bb90
1/*
2 * drivers/staging/android/ion/ion_mem_pool.c
3 *
4 * Copyright (C) 2011 Google, Inc.
5 *
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.
9 *
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.
14 *
15 */
16
17#include <linux/debugfs.h>
18#include <linux/dma-mapping.h>
19#include <linux/err.h>
20#include <linux/fs.h>
21#include <linux/list.h>
22#include <linux/module.h>
23#include <linux/slab.h>
24#include "ion_priv.h"
25
26struct ion_page_pool_item {
27	struct page *page;
28	struct list_head list;
29};
30
31static void *ion_page_pool_alloc_pages(struct ion_page_pool *pool)
32{
33	struct page *page = alloc_pages(pool->gfp_mask, pool->order);
34
35	if (!page)
36		return NULL;
37	ion_pages_sync_for_device(NULL, page, PAGE_SIZE << pool->order,
38						DMA_BIDIRECTIONAL);
39	return page;
40}
41
42static void ion_page_pool_free_pages(struct ion_page_pool *pool,
43				     struct page *page)
44{
45	__free_pages(page, pool->order);
46}
47
48static int ion_page_pool_add(struct ion_page_pool *pool, struct page *page)
49{
50	struct ion_page_pool_item *item;
51
52	item = kmalloc(sizeof(struct ion_page_pool_item), GFP_KERNEL);
53	if (!item)
54		return -ENOMEM;
55
56	mutex_lock(&pool->mutex);
57	item->page = page;
58	if (PageHighMem(page)) {
59		list_add_tail(&item->list, &pool->high_items);
60		pool->high_count++;
61	} else {
62		list_add_tail(&item->list, &pool->low_items);
63		pool->low_count++;
64	}
65	mutex_unlock(&pool->mutex);
66	return 0;
67}
68
69static struct page *ion_page_pool_remove(struct ion_page_pool *pool, bool high)
70{
71	struct ion_page_pool_item *item;
72	struct page *page;
73
74	if (high) {
75		BUG_ON(!pool->high_count);
76		item = list_first_entry(&pool->high_items,
77					struct ion_page_pool_item, list);
78		pool->high_count--;
79	} else {
80		BUG_ON(!pool->low_count);
81		item = list_first_entry(&pool->low_items,
82					struct ion_page_pool_item, list);
83		pool->low_count--;
84	}
85
86	list_del(&item->list);
87	page = item->page;
88	kfree(item);
89	return page;
90}
91
92void *ion_page_pool_alloc(struct ion_page_pool *pool)
93{
94	struct page *page = NULL;
95
96	BUG_ON(!pool);
97
98	mutex_lock(&pool->mutex);
99	if (pool->high_count)
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);
104
105	if (!page)
106		page = ion_page_pool_alloc_pages(pool);
107
108	return page;
109}
110
111void ion_page_pool_free(struct ion_page_pool *pool, struct page* page)
112{
113	int ret;
114
115	ret = ion_page_pool_add(pool, page);
116	if (ret)
117		ion_page_pool_free_pages(pool, page);
118}
119
120static int ion_page_pool_total(struct ion_page_pool *pool, bool high)
121{
122	int total = 0;
123
124	total += high ? (pool->high_count + pool->low_count) *
125		(1 << pool->order) :
126			pool->low_count * (1 << pool->order);
127	return total;
128}
129
130int ion_page_pool_shrink(struct ion_page_pool *pool, gfp_t gfp_mask,
131				int nr_to_scan)
132{
133	int nr_freed = 0;
134	int i;
135	bool high;
136
137	high = gfp_mask & __GFP_HIGHMEM;
138
139	if (nr_to_scan == 0)
140		return ion_page_pool_total(pool, high);
141
142	for (i = 0; i < nr_to_scan; i++) {
143		struct page *page;
144
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);
150		} else {
151			mutex_unlock(&pool->mutex);
152			break;
153		}
154		mutex_unlock(&pool->mutex);
155		ion_page_pool_free_pages(pool, page);
156		nr_freed += (1 << pool->order);
157	}
158
159	return nr_freed;
160}
161
162struct ion_page_pool *ion_page_pool_create(gfp_t gfp_mask, unsigned int order)
163{
164	struct ion_page_pool *pool = kmalloc(sizeof(struct ion_page_pool),
165					     GFP_KERNEL);
166	if (!pool)
167		return NULL;
168	pool->high_count = 0;
169	pool->low_count = 0;
170	INIT_LIST_HEAD(&pool->low_items);
171	INIT_LIST_HEAD(&pool->high_items);
172	pool->gfp_mask = gfp_mask;
173	pool->order = order;
174	mutex_init(&pool->mutex);
175	plist_node_init(&pool->list, order);
176
177	return pool;
178}
179
180void ion_page_pool_destroy(struct ion_page_pool *pool)
181{
182	kfree(pool);
183}
184
185static int __init ion_page_pool_init(void)
186{
187	return 0;
188}
189
190static void __exit ion_page_pool_exit(void)
191{
192}
193
194module_init(ion_page_pool_init);
195module_exit(ion_page_pool_exit);
196