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