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