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