Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/gpu/ion/ion_system_heap.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Google, Inc. |
Mitchel Humpherys | af3b522 | 2013-01-15 15:38:52 -0800 | [diff] [blame] | 5 | * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved. |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 6 | * |
| 7 | * This software is licensed under the terms of the GNU General Public |
| 8 | * License version 2, as published by the Free Software Foundation, and |
| 9 | * may be copied, distributed, and modified under those terms. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | */ |
| 17 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 18 | #include <asm/page.h> |
| 19 | #include <linux/dma-mapping.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 20 | #include <linux/err.h> |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 21 | #include <linux/highmem.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 22 | #include <linux/ion.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/scatterlist.h> |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 25 | #include <linux/seq_file.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | #include <linux/vmalloc.h> |
| 28 | #include "ion_priv.h" |
Laura Abbott | abcb6f7 | 2011-10-04 16:26:49 -0700 | [diff] [blame] | 29 | #include <mach/memory.h> |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 30 | #include <asm/cacheflush.h> |
Mitchel Humpherys | af2e5c5 | 2012-09-06 12:16:36 -0700 | [diff] [blame] | 31 | #include <linux/msm_ion.h> |
Neeti Desai | 3f3c282 | 2013-03-08 17:29:53 -0800 | [diff] [blame] | 32 | #include <linux/dma-mapping.h> |
Adrian Alexei | 21f62bd | 2013-04-22 12:57:41 -0700 | [diff] [blame] | 33 | #include <trace/events/kmem.h> |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 34 | |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 35 | static unsigned int high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | |
| 36 | __GFP_NOWARN | __GFP_NORETRY | |
Rebecca Schultz Zavin | 1797e59a | 2012-10-18 21:51:53 -0700 | [diff] [blame] | 37 | __GFP_NO_KSWAPD) & ~__GFP_WAIT; |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 38 | static unsigned int low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | |
| 39 | __GFP_NOWARN); |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 40 | static const unsigned int orders[] = {8, 4, 0}; |
| 41 | static const int num_orders = ARRAY_SIZE(orders); |
| 42 | static int order_to_index(unsigned int order) |
| 43 | { |
| 44 | int i; |
| 45 | for (i = 0; i < num_orders; i++) |
| 46 | if (order == orders[i]) |
| 47 | return i; |
| 48 | BUG(); |
| 49 | return -1; |
| 50 | } |
| 51 | |
| 52 | static unsigned int order_to_size(int order) |
| 53 | { |
| 54 | return PAGE_SIZE << order; |
| 55 | } |
| 56 | |
| 57 | struct ion_system_heap { |
| 58 | struct ion_heap heap; |
| 59 | struct ion_page_pool **pools; |
| 60 | }; |
| 61 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 62 | struct page_info { |
| 63 | struct page *page; |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 64 | unsigned int order; |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 65 | struct list_head list; |
| 66 | }; |
| 67 | |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 68 | static struct page *alloc_buffer_page(struct ion_system_heap *heap, |
| 69 | struct ion_buffer *buffer, |
| 70 | unsigned long order) |
| 71 | { |
| 72 | bool cached = ion_buffer_cached(buffer); |
| 73 | bool split_pages = ion_buffer_fault_user_mappings(buffer); |
| 74 | struct ion_page_pool *pool = heap->pools[order_to_index(order)]; |
| 75 | struct page *page; |
Rebecca Schultz Zavin | 96dd58d | 2012-09-26 10:58:30 -0700 | [diff] [blame] | 76 | |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 77 | if (!cached) { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 78 | page = ion_page_pool_alloc(pool); |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 79 | } else { |
Rebecca Schultz Zavin | 220a652 | 2012-10-18 21:54:01 -0700 | [diff] [blame] | 80 | struct scatterlist sg; |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 81 | gfp_t gfp_flags = low_order_gfp_flags; |
| 82 | |
| 83 | if (order > 4) |
| 84 | gfp_flags = high_order_gfp_flags; |
Adrian Alexei | 21f62bd | 2013-04-22 12:57:41 -0700 | [diff] [blame] | 85 | trace_alloc_pages_sys_start(gfp_flags, order); |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 86 | page = alloc_pages(gfp_flags, order); |
Adrian Alexei | 21f62bd | 2013-04-22 12:57:41 -0700 | [diff] [blame] | 87 | trace_alloc_pages_sys_end(gfp_flags, order); |
| 88 | if (!page) { |
| 89 | trace_alloc_pages_sys_fail(gfp_flags, order); |
Rebecca Schultz Zavin | 220a652 | 2012-10-18 21:54:01 -0700 | [diff] [blame] | 90 | return 0; |
Adrian Alexei | 21f62bd | 2013-04-22 12:57:41 -0700 | [diff] [blame] | 91 | } |
Rebecca Schultz Zavin | 220a652 | 2012-10-18 21:54:01 -0700 | [diff] [blame] | 92 | sg_init_table(&sg, 1); |
| 93 | sg_set_page(&sg, page, PAGE_SIZE << order, 0); |
| 94 | dma_sync_sg_for_device(NULL, &sg, 1, DMA_BIDIRECTIONAL); |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 95 | } |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 96 | if (!page) |
| 97 | return 0; |
Rebecca Schultz Zavin | 220a652 | 2012-10-18 21:54:01 -0700 | [diff] [blame] | 98 | |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 99 | if (split_pages) |
| 100 | split_page(page, order); |
| 101 | return page; |
| 102 | } |
| 103 | |
| 104 | static void free_buffer_page(struct ion_system_heap *heap, |
| 105 | struct ion_buffer *buffer, struct page *page, |
Rebecca Schultz Zavin | ca12f5d | 2013-01-09 11:26:37 -0800 | [diff] [blame] | 106 | unsigned int order) |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 107 | { |
| 108 | bool cached = ion_buffer_cached(buffer); |
| 109 | bool split_pages = ion_buffer_fault_user_mappings(buffer); |
| 110 | int i; |
| 111 | |
| 112 | if (!cached) { |
| 113 | struct ion_page_pool *pool = heap->pools[order_to_index(order)]; |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 114 | ion_page_pool_free(pool, page); |
| 115 | } else if (split_pages) { |
| 116 | for (i = 0; i < (1 << order); i++) |
| 117 | __free_page(page + i); |
| 118 | } else { |
| 119 | __free_pages(page, order); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static struct page_info *alloc_largest_available(struct ion_system_heap *heap, |
| 125 | struct ion_buffer *buffer, |
| 126 | unsigned long size, |
Rebecca Schultz Zavin | 158316f | 2012-09-25 20:55:27 -0700 | [diff] [blame] | 127 | unsigned int max_order) |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 128 | { |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 129 | struct page *page; |
| 130 | struct page_info *info; |
| 131 | int i; |
| 132 | |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 133 | for (i = 0; i < num_orders; i++) { |
| 134 | if (size < order_to_size(orders[i])) |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 135 | continue; |
Rebecca Schultz Zavin | 158316f | 2012-09-25 20:55:27 -0700 | [diff] [blame] | 136 | if (max_order < orders[i]) |
| 137 | continue; |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 138 | |
| 139 | page = alloc_buffer_page(heap, buffer, orders[i]); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 140 | if (!page) |
| 141 | continue; |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 142 | |
| 143 | info = kmalloc(sizeof(struct page_info), GFP_KERNEL); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 144 | info->page = page; |
| 145 | info->order = orders[i]; |
| 146 | return info; |
| 147 | } |
| 148 | return NULL; |
| 149 | } |
| 150 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 151 | static int ion_system_heap_allocate(struct ion_heap *heap, |
| 152 | struct ion_buffer *buffer, |
| 153 | unsigned long size, unsigned long align, |
| 154 | unsigned long flags) |
| 155 | { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 156 | struct ion_system_heap *sys_heap = container_of(heap, |
| 157 | struct ion_system_heap, |
| 158 | heap); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 159 | struct sg_table *table; |
| 160 | struct scatterlist *sg; |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 161 | int ret; |
| 162 | struct list_head pages; |
| 163 | struct page_info *info, *tmp_info; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 164 | int i = 0; |
Olav Haugan | d877069 | 2013-04-17 16:11:31 -0700 | [diff] [blame] | 165 | unsigned long size_remaining = PAGE_ALIGN(size); |
Rebecca Schultz Zavin | 158316f | 2012-09-25 20:55:27 -0700 | [diff] [blame] | 166 | unsigned int max_order = orders[0]; |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 167 | bool split_pages = ion_buffer_fault_user_mappings(buffer); |
Rebecca Schultz Zavin | 158316f | 2012-09-25 20:55:27 -0700 | [diff] [blame] | 168 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 169 | INIT_LIST_HEAD(&pages); |
| 170 | while (size_remaining > 0) { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 171 | info = alloc_largest_available(sys_heap, buffer, size_remaining, max_order); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 172 | if (!info) |
| 173 | goto err; |
| 174 | list_add_tail(&info->list, &pages); |
| 175 | size_remaining -= (1 << info->order) * PAGE_SIZE; |
Rebecca Schultz Zavin | 158316f | 2012-09-25 20:55:27 -0700 | [diff] [blame] | 176 | max_order = info->order; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 177 | i++; |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 178 | } |
Laura Abbott | 68c8064 | 2011-10-21 17:32:27 -0700 | [diff] [blame] | 179 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 180 | table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 181 | if (!table) |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 182 | goto err; |
| 183 | |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 184 | if (split_pages) |
| 185 | ret = sg_alloc_table(table, PAGE_ALIGN(size) / PAGE_SIZE, |
| 186 | GFP_KERNEL); |
| 187 | else |
| 188 | ret = sg_alloc_table(table, i, GFP_KERNEL); |
| 189 | |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 190 | if (ret) |
| 191 | goto err1; |
| 192 | |
| 193 | sg = table->sgl; |
| 194 | list_for_each_entry_safe(info, tmp_info, &pages, list) { |
| 195 | struct page *page = info->page; |
Rebecca Schultz Zavin | f858ba4 | 2012-09-21 11:46:06 -0700 | [diff] [blame] | 196 | if (split_pages) { |
| 197 | for (i = 0; i < (1 << info->order); i++) { |
| 198 | sg_set_page(sg, page + i, PAGE_SIZE, 0); |
| 199 | sg = sg_next(sg); |
| 200 | } |
| 201 | } else { |
| 202 | sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE, |
| 203 | 0); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 204 | sg = sg_next(sg); |
| 205 | } |
| 206 | list_del(&info->list); |
Rebecca Schultz Zavin | 6a93a29 | 2012-08-21 21:35:20 -0700 | [diff] [blame] | 207 | kfree(info); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 208 | } |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 209 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 210 | buffer->priv_virt = table; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 211 | return 0; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 212 | err1: |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 213 | kfree(table); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 214 | err: |
| 215 | list_for_each_entry(info, &pages, list) { |
Rebecca Schultz Zavin | ca12f5d | 2013-01-09 11:26:37 -0800 | [diff] [blame] | 216 | free_buffer_page(sys_heap, buffer, info->page, info->order); |
Rebecca Schultz Zavin | 6a93a29 | 2012-08-21 21:35:20 -0700 | [diff] [blame] | 217 | kfree(info); |
Rebecca Schultz Zavin | b831c8c | 2012-06-14 13:30:01 -0700 | [diff] [blame] | 218 | } |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 219 | return -ENOMEM; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | void ion_system_heap_free(struct ion_buffer *buffer) |
| 223 | { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 224 | struct ion_heap *heap = buffer->heap; |
| 225 | struct ion_system_heap *sys_heap = container_of(heap, |
| 226 | struct ion_system_heap, |
| 227 | heap); |
Rebecca Schultz Zavin | 3df181c | 2012-11-15 10:43:46 -0800 | [diff] [blame] | 228 | struct sg_table *table = buffer->sg_table; |
Rebecca Schultz Zavin | ca12f5d | 2013-01-09 11:26:37 -0800 | [diff] [blame] | 229 | bool cached = ion_buffer_cached(buffer); |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 230 | struct scatterlist *sg; |
| 231 | LIST_HEAD(pages); |
| 232 | int i; |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 233 | |
Rebecca Schultz Zavin | ca12f5d | 2013-01-09 11:26:37 -0800 | [diff] [blame] | 234 | /* uncached pages come from the page pools, zero them before returning |
| 235 | for security purposes (other allocations are zerod at alloc time */ |
| 236 | if (!cached) |
| 237 | ion_heap_buffer_zero(buffer); |
Rebecca Schultz Zavin | f910122 | 2012-12-18 22:46:57 -0800 | [diff] [blame] | 238 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 239 | for_each_sg(table->sgl, sg, table->nents, i) |
Rebecca Schultz Zavin | f910122 | 2012-12-18 22:46:57 -0800 | [diff] [blame] | 240 | free_buffer_page(sys_heap, buffer, sg_page(sg), |
Rebecca Schultz Zavin | ca12f5d | 2013-01-09 11:26:37 -0800 | [diff] [blame] | 241 | get_order(sg_dma_len(sg))); |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 242 | sg_free_table(table); |
| 243 | kfree(table); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 246 | struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap, |
| 247 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 248 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 249 | return buffer->priv_virt; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void ion_system_heap_unmap_dma(struct ion_heap *heap, |
| 253 | struct ion_buffer *buffer) |
| 254 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 255 | return; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 258 | static struct ion_heap_ops system_heap_ops = { |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 259 | .allocate = ion_system_heap_allocate, |
| 260 | .free = ion_system_heap_free, |
| 261 | .map_dma = ion_system_heap_map_dma, |
| 262 | .unmap_dma = ion_system_heap_unmap_dma, |
Rebecca Schultz Zavin | 3df181c | 2012-11-15 10:43:46 -0800 | [diff] [blame] | 263 | .map_kernel = ion_heap_map_kernel, |
| 264 | .unmap_kernel = ion_heap_unmap_kernel, |
| 265 | .map_user = ion_heap_map_user, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 266 | }; |
| 267 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 268 | struct ion_heap *ion_system_heap_create(struct ion_platform_heap *pheap) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 269 | { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 270 | struct ion_system_heap *heap; |
| 271 | int i; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 272 | |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 273 | heap = kzalloc(sizeof(struct ion_system_heap), GFP_KERNEL); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 274 | if (!heap) |
| 275 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 276 | heap->heap.ops = &system_heap_ops; |
| 277 | heap->heap.type = ION_HEAP_TYPE_SYSTEM; |
Rebecca Schultz Zavin | 618d6be | 2013-02-13 14:48:11 -0800 | [diff] [blame] | 278 | heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE; |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 279 | heap->pools = kzalloc(sizeof(struct ion_page_pool *) * num_orders, |
| 280 | GFP_KERNEL); |
| 281 | if (!heap->pools) |
| 282 | goto err_alloc_pools; |
| 283 | for (i = 0; i < num_orders; i++) { |
| 284 | struct ion_page_pool *pool; |
Rebecca Schultz Zavin | bff299e | 2012-10-02 22:43:41 -0700 | [diff] [blame] | 285 | gfp_t gfp_flags = low_order_gfp_flags; |
| 286 | |
| 287 | if (orders[i] > 4) |
| 288 | gfp_flags = high_order_gfp_flags; |
| 289 | pool = ion_page_pool_create(gfp_flags, orders[i]); |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 290 | if (!pool) |
| 291 | goto err_create_pool; |
| 292 | heap->pools[i] = pool; |
| 293 | } |
| 294 | return &heap->heap; |
| 295 | err_create_pool: |
| 296 | for (i = 0; i < num_orders; i++) |
| 297 | if (heap->pools[i]) |
| 298 | ion_page_pool_destroy(heap->pools[i]); |
| 299 | kfree(heap->pools); |
| 300 | err_alloc_pools: |
| 301 | kfree(heap); |
| 302 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | void ion_system_heap_destroy(struct ion_heap *heap) |
| 306 | { |
Rebecca Schultz Zavin | 943facc | 2012-08-06 21:37:23 -0700 | [diff] [blame] | 307 | struct ion_system_heap *sys_heap = container_of(heap, |
| 308 | struct ion_system_heap, |
| 309 | heap); |
| 310 | int i; |
| 311 | |
| 312 | for (i = 0; i < num_orders; i++) |
| 313 | ion_page_pool_destroy(sys_heap->pools[i]); |
| 314 | kfree(sys_heap->pools); |
| 315 | kfree(sys_heap); |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static int ion_system_contig_heap_allocate(struct ion_heap *heap, |
| 319 | struct ion_buffer *buffer, |
| 320 | unsigned long len, |
| 321 | unsigned long align, |
| 322 | unsigned long flags) |
| 323 | { |
| 324 | buffer->priv_virt = kzalloc(len, GFP_KERNEL); |
| 325 | if (!buffer->priv_virt) |
| 326 | return -ENOMEM; |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | void ion_system_contig_heap_free(struct ion_buffer *buffer) |
| 331 | { |
| 332 | kfree(buffer->priv_virt); |
| 333 | } |
| 334 | |
| 335 | static int ion_system_contig_heap_phys(struct ion_heap *heap, |
| 336 | struct ion_buffer *buffer, |
| 337 | ion_phys_addr_t *addr, size_t *len) |
| 338 | { |
| 339 | *addr = virt_to_phys(buffer->priv_virt); |
| 340 | *len = buffer->size; |
| 341 | return 0; |
| 342 | } |
| 343 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 344 | struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap, |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 345 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 346 | { |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 347 | struct sg_table *table; |
| 348 | int ret; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 349 | |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 350 | table = kzalloc(sizeof(struct sg_table), GFP_KERNEL); |
| 351 | if (!table) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 352 | return ERR_PTR(-ENOMEM); |
Laura Abbott | b14ed96 | 2012-01-30 14:18:08 -0800 | [diff] [blame] | 353 | ret = sg_alloc_table(table, 1, GFP_KERNEL); |
| 354 | if (ret) { |
| 355 | kfree(table); |
| 356 | return ERR_PTR(ret); |
| 357 | } |
| 358 | sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size, |
| 359 | 0); |
| 360 | return table; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 363 | void ion_system_contig_heap_unmap_dma(struct ion_heap *heap, |
| 364 | struct ion_buffer *buffer) |
| 365 | { |
| 366 | sg_free_table(buffer->sg_table); |
| 367 | kfree(buffer->sg_table); |
| 368 | } |
| 369 | |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 370 | static struct ion_heap_ops kmalloc_ops = { |
| 371 | .allocate = ion_system_contig_heap_allocate, |
| 372 | .free = ion_system_contig_heap_free, |
| 373 | .phys = ion_system_contig_heap_phys, |
| 374 | .map_dma = ion_system_contig_heap_map_dma, |
Rebecca Schultz Zavin | b179067 | 2012-06-14 15:08:53 -0700 | [diff] [blame] | 375 | .unmap_dma = ion_system_contig_heap_unmap_dma, |
Rebecca Schultz Zavin | 3df181c | 2012-11-15 10:43:46 -0800 | [diff] [blame] | 376 | .map_kernel = ion_heap_map_kernel, |
| 377 | .unmap_kernel = ion_heap_unmap_kernel, |
Laura Abbott | d59c223 | 2013-04-17 17:19:40 -0700 | [diff] [blame] | 378 | .map_user = ion_heap_map_user, |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 379 | }; |
| 380 | |
Olav Haugan | 85c9540 | 2012-05-30 17:32:37 -0700 | [diff] [blame] | 381 | struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *pheap) |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 382 | { |
| 383 | struct ion_heap *heap; |
| 384 | |
| 385 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 386 | if (!heap) |
| 387 | return ERR_PTR(-ENOMEM); |
| 388 | heap->ops = &kmalloc_ops; |
| 389 | heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG; |
Rebecca Schultz Zavin | c80005a | 2011-06-29 19:44:29 -0700 | [diff] [blame] | 390 | return heap; |
| 391 | } |
| 392 | |
| 393 | void ion_system_contig_heap_destroy(struct ion_heap *heap) |
| 394 | { |
| 395 | kfree(heap); |
| 396 | } |
| 397 | |