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