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