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