Commit | Line | Data |
---|---|---|
c30707be RSZ |
1 | /* |
2 | * drivers/staging/android/ion/ion_system_heap.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 | ||
bd5d6bda RSZ |
17 | #include <asm/page.h> |
18 | #include <linux/dma-mapping.h> | |
c30707be | 19 | #include <linux/err.h> |
bd5d6bda | 20 | #include <linux/highmem.h> |
c30707be RSZ |
21 | #include <linux/mm.h> |
22 | #include <linux/scatterlist.h> | |
45b17a80 | 23 | #include <linux/seq_file.h> |
c30707be RSZ |
24 | #include <linux/slab.h> |
25 | #include <linux/vmalloc.h> | |
26 | #include "ion.h" | |
27 | #include "ion_priv.h" | |
28 | ||
f63958d8 | 29 | static gfp_t high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN | |
d0164adc | 30 | __GFP_NORETRY) & ~__GFP_DIRECT_RECLAIM; |
f63958d8 | 31 | static gfp_t low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN); |
45b17a80 RSZ |
32 | static const unsigned int orders[] = {8, 4, 0}; |
33 | static const int num_orders = ARRAY_SIZE(orders); | |
34 | static int order_to_index(unsigned int order) | |
35 | { | |
36 | int i; | |
10f62861 | 37 | |
45b17a80 RSZ |
38 | for (i = 0; i < num_orders; i++) |
39 | if (order == orders[i]) | |
40 | return i; | |
41 | BUG(); | |
42 | return -1; | |
43 | } | |
44 | ||
79240748 | 45 | static inline unsigned int order_to_size(int order) |
45b17a80 RSZ |
46 | { |
47 | return PAGE_SIZE << order; | |
48 | } | |
49 | ||
50 | struct ion_system_heap { | |
51 | struct ion_heap heap; | |
6944561e | 52 | struct ion_page_pool *pools[0]; |
45b17a80 RSZ |
53 | }; |
54 | ||
45b17a80 RSZ |
55 | static struct page *alloc_buffer_page(struct ion_system_heap *heap, |
56 | struct ion_buffer *buffer, | |
57 | unsigned long order) | |
58 | { | |
59 | bool cached = ion_buffer_cached(buffer); | |
45b17a80 RSZ |
60 | struct ion_page_pool *pool = heap->pools[order_to_index(order)]; |
61 | struct page *page; | |
62 | ||
ee4a4986 | 63 | if (!cached) { |
45b17a80 | 64 | page = ion_page_pool_alloc(pool); |
ee4a4986 RSZ |
65 | } else { |
66 | gfp_t gfp_flags = low_order_gfp_flags; | |
67 | ||
68 | if (order > 4) | |
69 | gfp_flags = high_order_gfp_flags; | |
bdeb9f1c | 70 | page = alloc_pages(gfp_flags | __GFP_COMP, order); |
8fae8312 | 71 | if (!page) |
f63958d8 | 72 | return NULL; |
e946b209 CC |
73 | ion_pages_sync_for_device(NULL, page, PAGE_SIZE << order, |
74 | DMA_BIDIRECTIONAL); | |
ee4a4986 | 75 | } |
8fae8312 | 76 | |
45b17a80 RSZ |
77 | return page; |
78 | } | |
79 | ||
80 | static void free_buffer_page(struct ion_system_heap *heap, | |
06566f5d | 81 | struct ion_buffer *buffer, struct page *page) |
45b17a80 | 82 | { |
06566f5d | 83 | unsigned int order = compound_order(page); |
45b17a80 | 84 | bool cached = ion_buffer_cached(buffer); |
45b17a80 | 85 | |
53a91c68 | 86 | if (!cached && !(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE)) { |
45b17a80 | 87 | struct ion_page_pool *pool = heap->pools[order_to_index(order)]; |
10f62861 | 88 | |
45b17a80 | 89 | ion_page_pool_free(pool, page); |
45b17a80 RSZ |
90 | } else { |
91 | __free_pages(page, order); | |
92 | } | |
93 | } | |
94 | ||
b0599c01 | 95 | |
7eb88bff HS |
96 | static struct page *alloc_largest_available(struct ion_system_heap *heap, |
97 | struct ion_buffer *buffer, | |
98 | unsigned long size, | |
99 | unsigned int max_order) | |
bd5d6bda | 100 | { |
bd5d6bda | 101 | struct page *page; |
bd5d6bda RSZ |
102 | int i; |
103 | ||
45b17a80 RSZ |
104 | for (i = 0; i < num_orders; i++) { |
105 | if (size < order_to_size(orders[i])) | |
bd5d6bda | 106 | continue; |
ba96a2ee RSZ |
107 | if (max_order < orders[i]) |
108 | continue; | |
45b17a80 RSZ |
109 | |
110 | page = alloc_buffer_page(heap, buffer, orders[i]); | |
bd5d6bda RSZ |
111 | if (!page) |
112 | continue; | |
45b17a80 | 113 | |
7eb88bff | 114 | return page; |
bd5d6bda | 115 | } |
f4ea823b | 116 | |
bd5d6bda RSZ |
117 | return NULL; |
118 | } | |
119 | ||
c30707be RSZ |
120 | static int ion_system_heap_allocate(struct ion_heap *heap, |
121 | struct ion_buffer *buffer, | |
122 | unsigned long size, unsigned long align, | |
123 | unsigned long flags) | |
c30707be | 124 | { |
45b17a80 RSZ |
125 | struct ion_system_heap *sys_heap = container_of(heap, |
126 | struct ion_system_heap, | |
127 | heap); | |
4d5ca329 RSZ |
128 | struct sg_table *table; |
129 | struct scatterlist *sg; | |
bd5d6bda | 130 | struct list_head pages; |
7eb88bff | 131 | struct page *page, *tmp_page; |
13ba7805 | 132 | int i = 0; |
c9e8440e | 133 | unsigned long size_remaining = PAGE_ALIGN(size); |
ba96a2ee RSZ |
134 | unsigned int max_order = orders[0]; |
135 | ||
c13d1df9 CC |
136 | if (align > PAGE_SIZE) |
137 | return -EINVAL; | |
138 | ||
c9e8440e CC |
139 | if (size / PAGE_SIZE > totalram_pages / 2) |
140 | return -ENOMEM; | |
141 | ||
bd5d6bda RSZ |
142 | INIT_LIST_HEAD(&pages); |
143 | while (size_remaining > 0) { | |
7eb88bff | 144 | page = alloc_largest_available(sys_heap, buffer, size_remaining, |
e1d855b0 | 145 | max_order); |
7eb88bff | 146 | if (!page) |
79240748 | 147 | goto free_pages; |
7eb88bff HS |
148 | list_add_tail(&page->lru, &pages); |
149 | size_remaining -= PAGE_SIZE << compound_order(page); | |
150 | max_order = compound_order(page); | |
13ba7805 | 151 | i++; |
bd5d6bda | 152 | } |
b6152016 | 153 | table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
4d5ca329 | 154 | if (!table) |
79240748 | 155 | goto free_pages; |
bd5d6bda | 156 | |
79240748 HS |
157 | if (sg_alloc_table(table, i, GFP_KERNEL)) |
158 | goto free_table; | |
bd5d6bda RSZ |
159 | |
160 | sg = table->sgl; | |
7eb88bff | 161 | list_for_each_entry_safe(page, tmp_page, &pages, lru) { |
d10e4ffd | 162 | sg_set_page(sg, page, PAGE_SIZE << compound_order(page), 0); |
c13bd1c4 | 163 | sg = sg_next(sg); |
7eb88bff | 164 | list_del(&page->lru); |
c30707be | 165 | } |
bd5d6bda | 166 | |
b15934b6 RSZ |
167 | buffer->priv_virt = table; |
168 | return 0; | |
79240748 HS |
169 | |
170 | free_table: | |
4d5ca329 | 171 | kfree(table); |
79240748 | 172 | free_pages: |
7eb88bff | 173 | list_for_each_entry_safe(page, tmp_page, &pages, lru) |
06566f5d | 174 | free_buffer_page(sys_heap, buffer, page); |
b15934b6 | 175 | return -ENOMEM; |
c30707be RSZ |
176 | } |
177 | ||
f63958d8 | 178 | static void ion_system_heap_free(struct ion_buffer *buffer) |
c30707be | 179 | { |
79240748 | 180 | struct ion_system_heap *sys_heap = container_of(buffer->heap, |
45b17a80 RSZ |
181 | struct ion_system_heap, |
182 | heap); | |
8898227e | 183 | struct sg_table *table = buffer->sg_table; |
0b6b2cde | 184 | bool cached = ion_buffer_cached(buffer); |
45b17a80 | 185 | struct scatterlist *sg; |
45b17a80 | 186 | int i; |
b15934b6 | 187 | |
7e416174 SR |
188 | /* |
189 | * uncached pages come from the page pools, zero them before returning | |
190 | * for security purposes (other allocations are zerod at | |
191 | * alloc time | |
192 | */ | |
53a91c68 | 193 | if (!cached && !(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE)) |
0b6b2cde | 194 | ion_heap_buffer_zero(buffer); |
77cbe828 | 195 | |
b15934b6 | 196 | for_each_sg(table->sgl, sg, table->nents, i) |
06566f5d | 197 | free_buffer_page(sys_heap, buffer, sg_page(sg)); |
45b17a80 RSZ |
198 | sg_free_table(table); |
199 | kfree(table); | |
c30707be RSZ |
200 | } |
201 | ||
f63958d8 CC |
202 | static struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap, |
203 | struct ion_buffer *buffer) | |
b15934b6 RSZ |
204 | { |
205 | return buffer->priv_virt; | |
206 | } | |
207 | ||
f63958d8 CC |
208 | static void ion_system_heap_unmap_dma(struct ion_heap *heap, |
209 | struct ion_buffer *buffer) | |
b15934b6 | 210 | { |
b15934b6 RSZ |
211 | } |
212 | ||
b9daf0b6 CC |
213 | static int ion_system_heap_shrink(struct ion_heap *heap, gfp_t gfp_mask, |
214 | int nr_to_scan) | |
b1aced6f | 215 | { |
b9daf0b6 | 216 | struct ion_system_heap *sys_heap; |
ea313b5f | 217 | int nr_total = 0; |
b44d9ce3 GK |
218 | int i, nr_freed; |
219 | int only_scan = 0; | |
b1aced6f | 220 | |
b9daf0b6 | 221 | sys_heap = container_of(heap, struct ion_system_heap, heap); |
ea313b5f | 222 | |
b44d9ce3 GK |
223 | if (!nr_to_scan) |
224 | only_scan = 1; | |
225 | ||
ea313b5f RSZ |
226 | for (i = 0; i < num_orders; i++) { |
227 | struct ion_page_pool *pool = sys_heap->pools[i]; | |
10f62861 | 228 | |
b44d9ce3 GK |
229 | nr_freed = ion_page_pool_shrink(pool, gfp_mask, nr_to_scan); |
230 | nr_total += nr_freed; | |
231 | ||
232 | if (!only_scan) { | |
233 | nr_to_scan -= nr_freed; | |
234 | /* shrink completed */ | |
235 | if (nr_to_scan <= 0) | |
236 | break; | |
237 | } | |
ea313b5f RSZ |
238 | } |
239 | ||
b9daf0b6 | 240 | return nr_total; |
ea313b5f RSZ |
241 | } |
242 | ||
b9daf0b6 CC |
243 | static struct ion_heap_ops system_heap_ops = { |
244 | .allocate = ion_system_heap_allocate, | |
245 | .free = ion_system_heap_free, | |
246 | .map_dma = ion_system_heap_map_dma, | |
247 | .unmap_dma = ion_system_heap_unmap_dma, | |
248 | .map_kernel = ion_heap_map_kernel, | |
249 | .unmap_kernel = ion_heap_unmap_kernel, | |
250 | .map_user = ion_heap_map_user, | |
251 | .shrink = ion_system_heap_shrink, | |
252 | }; | |
253 | ||
45b17a80 RSZ |
254 | static int ion_system_heap_debug_show(struct ion_heap *heap, struct seq_file *s, |
255 | void *unused) | |
256 | { | |
257 | ||
258 | struct ion_system_heap *sys_heap = container_of(heap, | |
259 | struct ion_system_heap, | |
260 | heap); | |
261 | int i; | |
10f62861 | 262 | |
45b17a80 RSZ |
263 | for (i = 0; i < num_orders; i++) { |
264 | struct ion_page_pool *pool = sys_heap->pools[i]; | |
10f62861 | 265 | |
0fb9b815 RSZ |
266 | seq_printf(s, "%d order %u highmem pages in pool = %lu total\n", |
267 | pool->high_count, pool->order, | |
79240748 | 268 | (PAGE_SIZE << pool->order) * pool->high_count); |
0fb9b815 RSZ |
269 | seq_printf(s, "%d order %u lowmem pages in pool = %lu total\n", |
270 | pool->low_count, pool->order, | |
79240748 | 271 | (PAGE_SIZE << pool->order) * pool->low_count); |
45b17a80 RSZ |
272 | } |
273 | return 0; | |
274 | } | |
275 | ||
c30707be RSZ |
276 | struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused) |
277 | { | |
45b17a80 RSZ |
278 | struct ion_system_heap *heap; |
279 | int i; | |
c30707be | 280 | |
6944561e HS |
281 | heap = kzalloc(sizeof(struct ion_system_heap) + |
282 | sizeof(struct ion_page_pool *) * num_orders, | |
283 | GFP_KERNEL); | |
c30707be RSZ |
284 | if (!heap) |
285 | return ERR_PTR(-ENOMEM); | |
45b17a80 RSZ |
286 | heap->heap.ops = &system_heap_ops; |
287 | heap->heap.type = ION_HEAP_TYPE_SYSTEM; | |
fe2faea7 | 288 | heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE; |
6944561e | 289 | |
45b17a80 RSZ |
290 | for (i = 0; i < num_orders; i++) { |
291 | struct ion_page_pool *pool; | |
ee4a4986 RSZ |
292 | gfp_t gfp_flags = low_order_gfp_flags; |
293 | ||
294 | if (orders[i] > 4) | |
295 | gfp_flags = high_order_gfp_flags; | |
296 | pool = ion_page_pool_create(gfp_flags, orders[i]); | |
45b17a80 | 297 | if (!pool) |
79240748 | 298 | goto destroy_pools; |
45b17a80 RSZ |
299 | heap->pools[i] = pool; |
300 | } | |
ea313b5f | 301 | |
45b17a80 RSZ |
302 | heap->heap.debug_show = ion_system_heap_debug_show; |
303 | return &heap->heap; | |
79240748 HS |
304 | |
305 | destroy_pools: | |
306 | while (i--) | |
307 | ion_page_pool_destroy(heap->pools[i]); | |
45b17a80 RSZ |
308 | kfree(heap); |
309 | return ERR_PTR(-ENOMEM); | |
c30707be RSZ |
310 | } |
311 | ||
312 | void ion_system_heap_destroy(struct ion_heap *heap) | |
313 | { | |
45b17a80 RSZ |
314 | struct ion_system_heap *sys_heap = container_of(heap, |
315 | struct ion_system_heap, | |
316 | heap); | |
317 | int i; | |
318 | ||
319 | for (i = 0; i < num_orders; i++) | |
320 | ion_page_pool_destroy(sys_heap->pools[i]); | |
45b17a80 | 321 | kfree(sys_heap); |
c30707be RSZ |
322 | } |
323 | ||
324 | static int ion_system_contig_heap_allocate(struct ion_heap *heap, | |
325 | struct ion_buffer *buffer, | |
326 | unsigned long len, | |
327 | unsigned long align, | |
328 | unsigned long flags) | |
329 | { | |
c13d1df9 | 330 | int order = get_order(len); |
5c6a4705 CC |
331 | struct page *page; |
332 | struct sg_table *table; | |
333 | unsigned long i; | |
334 | int ret; | |
c13d1df9 CC |
335 | |
336 | if (align > (PAGE_SIZE << order)) | |
337 | return -EINVAL; | |
338 | ||
5c6a4705 CC |
339 | page = alloc_pages(low_order_gfp_flags, order); |
340 | if (!page) | |
c30707be | 341 | return -ENOMEM; |
5c6a4705 CC |
342 | |
343 | split_page(page, order); | |
344 | ||
345 | len = PAGE_ALIGN(len); | |
346 | for (i = len >> PAGE_SHIFT; i < (1 << order); i++) | |
347 | __free_page(page + i); | |
348 | ||
b6152016 | 349 | table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
5c6a4705 CC |
350 | if (!table) { |
351 | ret = -ENOMEM; | |
79240748 | 352 | goto free_pages; |
5c6a4705 CC |
353 | } |
354 | ||
355 | ret = sg_alloc_table(table, 1, GFP_KERNEL); | |
356 | if (ret) | |
79240748 | 357 | goto free_table; |
5c6a4705 CC |
358 | |
359 | sg_set_page(table->sgl, page, len, 0); | |
360 | ||
361 | buffer->priv_virt = table; | |
362 | ||
363 | ion_pages_sync_for_device(NULL, page, len, DMA_BIDIRECTIONAL); | |
364 | ||
c30707be | 365 | return 0; |
5c6a4705 | 366 | |
79240748 HS |
367 | free_table: |
368 | kfree(table); | |
369 | free_pages: | |
5c6a4705 CC |
370 | for (i = 0; i < len >> PAGE_SHIFT; i++) |
371 | __free_page(page + i); | |
79240748 | 372 | |
5c6a4705 | 373 | return ret; |
c30707be RSZ |
374 | } |
375 | ||
f63958d8 | 376 | static void ion_system_contig_heap_free(struct ion_buffer *buffer) |
c30707be | 377 | { |
5c6a4705 CC |
378 | struct sg_table *table = buffer->priv_virt; |
379 | struct page *page = sg_page(table->sgl); | |
380 | unsigned long pages = PAGE_ALIGN(buffer->size) >> PAGE_SHIFT; | |
381 | unsigned long i; | |
382 | ||
383 | for (i = 0; i < pages; i++) | |
384 | __free_page(page + i); | |
385 | sg_free_table(table); | |
386 | kfree(table); | |
c30707be RSZ |
387 | } |
388 | ||
389 | static int ion_system_contig_heap_phys(struct ion_heap *heap, | |
390 | struct ion_buffer *buffer, | |
391 | ion_phys_addr_t *addr, size_t *len) | |
392 | { | |
5c6a4705 CC |
393 | struct sg_table *table = buffer->priv_virt; |
394 | struct page *page = sg_page(table->sgl); | |
395 | *addr = page_to_phys(page); | |
c30707be RSZ |
396 | *len = buffer->size; |
397 | return 0; | |
398 | } | |
399 | ||
f63958d8 | 400 | static struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap, |
56a7c185 | 401 | struct ion_buffer *buffer) |
c30707be | 402 | { |
5c6a4705 | 403 | return buffer->priv_virt; |
c30707be RSZ |
404 | } |
405 | ||
f63958d8 CC |
406 | static void ion_system_contig_heap_unmap_dma(struct ion_heap *heap, |
407 | struct ion_buffer *buffer) | |
56a7c185 | 408 | { |
56a7c185 RSZ |
409 | } |
410 | ||
c30707be RSZ |
411 | static struct ion_heap_ops kmalloc_ops = { |
412 | .allocate = ion_system_contig_heap_allocate, | |
413 | .free = ion_system_contig_heap_free, | |
414 | .phys = ion_system_contig_heap_phys, | |
415 | .map_dma = ion_system_contig_heap_map_dma, | |
56a7c185 | 416 | .unmap_dma = ion_system_contig_heap_unmap_dma, |
8898227e RSZ |
417 | .map_kernel = ion_heap_map_kernel, |
418 | .unmap_kernel = ion_heap_unmap_kernel, | |
a82130f4 | 419 | .map_user = ion_heap_map_user, |
c30707be RSZ |
420 | }; |
421 | ||
422 | struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *unused) | |
423 | { | |
424 | struct ion_heap *heap; | |
425 | ||
426 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); | |
427 | if (!heap) | |
428 | return ERR_PTR(-ENOMEM); | |
429 | heap->ops = &kmalloc_ops; | |
430 | heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG; | |
431 | return heap; | |
432 | } | |
433 | ||
434 | void ion_system_contig_heap_destroy(struct ion_heap *heap) | |
435 | { | |
436 | kfree(heap); | |
437 | } |