Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * drivers/staging/android/ion/ion_system_heap.c |
| 3 | * |
| 4 | * Copyright (C) 2011 Google, Inc. |
| 5 | * |
| 6 | * This software is licensed under the terms of the GNU General Public |
| 7 | * License version 2, as published by the Free Software Foundation, and |
| 8 | * may be copied, distributed, and modified under those terms. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 17 | #include <asm/page.h> |
| 18 | #include <linux/dma-mapping.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 19 | #include <linux/err.h> |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 20 | #include <linux/highmem.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 21 | #include <linux/mm.h> |
| 22 | #include <linux/scatterlist.h> |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 23 | #include <linux/seq_file.h> |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 24 | #include <linux/slab.h> |
| 25 | #include <linux/vmalloc.h> |
| 26 | #include "ion.h" |
| 27 | #include "ion_priv.h" |
| 28 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 29 | static gfp_t high_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN | |
Mel Gorman | d0164ad | 2015-11-06 16:28:21 -0800 | [diff] [blame^] | 30 | __GFP_NORETRY) & ~__GFP_DIRECT_RECLAIM; |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 31 | static gfp_t low_order_gfp_flags = (GFP_HIGHUSER | __GFP_ZERO | __GFP_NOWARN); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 32 | static const unsigned int orders[] = {8, 4, 0}; |
| 33 | static const int num_orders = ARRAY_SIZE(orders); |
| 34 | static int order_to_index(unsigned int order) |
| 35 | { |
| 36 | int i; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 37 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 38 | for (i = 0; i < num_orders; i++) |
| 39 | if (order == orders[i]) |
| 40 | return i; |
| 41 | BUG(); |
| 42 | return -1; |
| 43 | } |
| 44 | |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 45 | static inline unsigned int order_to_size(int order) |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 46 | { |
| 47 | return PAGE_SIZE << order; |
| 48 | } |
| 49 | |
| 50 | struct ion_system_heap { |
| 51 | struct ion_heap heap; |
Heesub Shin | 6944561 | 2014-05-30 10:26:30 +0900 | [diff] [blame] | 52 | struct ion_page_pool *pools[0]; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 55 | static struct page *alloc_buffer_page(struct ion_system_heap *heap, |
| 56 | struct ion_buffer *buffer, |
| 57 | unsigned long order) |
| 58 | { |
| 59 | bool cached = ion_buffer_cached(buffer); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 60 | struct ion_page_pool *pool = heap->pools[order_to_index(order)]; |
| 61 | struct page *page; |
Rebecca Schultz Zavin | b0599c0 | 2013-12-13 14:24:08 -0800 | [diff] [blame] | 62 | |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 63 | if (!cached) { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 64 | page = ion_page_pool_alloc(pool); |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 65 | } else { |
| 66 | gfp_t gfp_flags = low_order_gfp_flags; |
| 67 | |
| 68 | if (order > 4) |
| 69 | gfp_flags = high_order_gfp_flags; |
Heesub Shin | bdeb9f1 | 2014-05-28 15:52:55 +0900 | [diff] [blame] | 70 | page = alloc_pages(gfp_flags | __GFP_COMP, order); |
Rebecca Schultz Zavin | 8fae831 | 2013-12-13 14:24:18 -0800 | [diff] [blame] | 71 | if (!page) |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 72 | return NULL; |
Colin Cross | e946b20 | 2013-12-13 14:25:01 -0800 | [diff] [blame] | 73 | ion_pages_sync_for_device(NULL, page, PAGE_SIZE << order, |
| 74 | DMA_BIDIRECTIONAL); |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 75 | } |
Rebecca Schultz Zavin | 8fae831 | 2013-12-13 14:24:18 -0800 | [diff] [blame] | 76 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 77 | return page; |
| 78 | } |
| 79 | |
| 80 | static void free_buffer_page(struct ion_system_heap *heap, |
Heesub Shin | 06566f5 | 2014-05-30 10:26:29 +0900 | [diff] [blame] | 81 | struct ion_buffer *buffer, struct page *page) |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 82 | { |
Heesub Shin | 06566f5 | 2014-05-30 10:26:29 +0900 | [diff] [blame] | 83 | unsigned int order = compound_order(page); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 84 | bool cached = ion_buffer_cached(buffer); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 85 | |
Mitchel Humpherys | 53a91c6 | 2014-02-17 13:58:39 -0800 | [diff] [blame] | 86 | if (!cached && !(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE)) { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 87 | struct ion_page_pool *pool = heap->pools[order_to_index(order)]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 88 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 89 | ion_page_pool_free(pool, page); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 90 | } else { |
| 91 | __free_pages(page, order); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
Heesub Shin | 7eb88bf | 2014-05-30 10:26:28 +0900 | [diff] [blame] | 96 | static struct page *alloc_largest_available(struct ion_system_heap *heap, |
| 97 | struct ion_buffer *buffer, |
| 98 | unsigned long size, |
| 99 | unsigned int max_order) |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 100 | { |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 101 | struct page *page; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 102 | int i; |
| 103 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 104 | for (i = 0; i < num_orders; i++) { |
| 105 | if (size < order_to_size(orders[i])) |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 106 | continue; |
Rebecca Schultz Zavin | ba96a2e | 2013-12-13 14:24:07 -0800 | [diff] [blame] | 107 | if (max_order < orders[i]) |
| 108 | continue; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 109 | |
| 110 | page = alloc_buffer_page(heap, buffer, orders[i]); |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 111 | if (!page) |
| 112 | continue; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 113 | |
Heesub Shin | 7eb88bf | 2014-05-30 10:26:28 +0900 | [diff] [blame] | 114 | return page; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 115 | } |
John Stultz | f4ea823 | 2013-12-16 21:07:52 -0800 | [diff] [blame] | 116 | |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 117 | return NULL; |
| 118 | } |
| 119 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 120 | static int ion_system_heap_allocate(struct ion_heap *heap, |
| 121 | struct ion_buffer *buffer, |
| 122 | unsigned long size, unsigned long align, |
| 123 | unsigned long flags) |
| 124 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 125 | struct ion_system_heap *sys_heap = container_of(heap, |
| 126 | struct ion_system_heap, |
| 127 | heap); |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 128 | struct sg_table *table; |
| 129 | struct scatterlist *sg; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 130 | struct list_head pages; |
Heesub Shin | 7eb88bf | 2014-05-30 10:26:28 +0900 | [diff] [blame] | 131 | struct page *page, *tmp_page; |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 132 | int i = 0; |
Colin Cross | c9e8440 | 2014-02-04 16:08:38 -0800 | [diff] [blame] | 133 | unsigned long size_remaining = PAGE_ALIGN(size); |
Rebecca Schultz Zavin | ba96a2e | 2013-12-13 14:24:07 -0800 | [diff] [blame] | 134 | unsigned int max_order = orders[0]; |
| 135 | |
Colin Cross | c13d1df | 2013-12-13 14:25:03 -0800 | [diff] [blame] | 136 | if (align > PAGE_SIZE) |
| 137 | return -EINVAL; |
| 138 | |
Colin Cross | c9e8440 | 2014-02-04 16:08:38 -0800 | [diff] [blame] | 139 | if (size / PAGE_SIZE > totalram_pages / 2) |
| 140 | return -ENOMEM; |
| 141 | |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 142 | INIT_LIST_HEAD(&pages); |
| 143 | while (size_remaining > 0) { |
Heesub Shin | 7eb88bf | 2014-05-30 10:26:28 +0900 | [diff] [blame] | 144 | page = alloc_largest_available(sys_heap, buffer, size_remaining, |
John Stultz | e1d855b | 2013-12-13 19:26:33 -0800 | [diff] [blame] | 145 | max_order); |
Heesub Shin | 7eb88bf | 2014-05-30 10:26:28 +0900 | [diff] [blame] | 146 | if (!page) |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 147 | goto free_pages; |
Heesub Shin | 7eb88bf | 2014-05-30 10:26:28 +0900 | [diff] [blame] | 148 | list_add_tail(&page->lru, &pages); |
| 149 | size_remaining -= PAGE_SIZE << compound_order(page); |
| 150 | max_order = compound_order(page); |
Rebecca Schultz Zavin | 13ba780 | 2013-12-13 14:24:06 -0800 | [diff] [blame] | 151 | i++; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 152 | } |
Gioh Kim | b615201 | 2014-04-25 08:24:24 +0900 | [diff] [blame] | 153 | table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 154 | if (!table) |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 155 | goto free_pages; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 156 | |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 157 | if (sg_alloc_table(table, i, GFP_KERNEL)) |
| 158 | goto free_table; |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 159 | |
| 160 | sg = table->sgl; |
Heesub Shin | 7eb88bf | 2014-05-30 10:26:28 +0900 | [diff] [blame] | 161 | list_for_each_entry_safe(page, tmp_page, &pages, lru) { |
Heesub Shin | d10e4ff | 2014-05-30 10:26:27 +0900 | [diff] [blame] | 162 | sg_set_page(sg, page, PAGE_SIZE << compound_order(page), 0); |
Rebecca Schultz Zavin | c13bd1c | 2013-12-13 14:24:45 -0800 | [diff] [blame] | 163 | sg = sg_next(sg); |
Heesub Shin | 7eb88bf | 2014-05-30 10:26:28 +0900 | [diff] [blame] | 164 | list_del(&page->lru); |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 165 | } |
Rebecca Schultz Zavin | bd5d6bd | 2013-12-13 14:23:51 -0800 | [diff] [blame] | 166 | |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 167 | buffer->priv_virt = table; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 168 | return 0; |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 169 | |
| 170 | free_table: |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 171 | kfree(table); |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 172 | free_pages: |
Heesub Shin | 7eb88bf | 2014-05-30 10:26:28 +0900 | [diff] [blame] | 173 | list_for_each_entry_safe(page, tmp_page, &pages, lru) |
Heesub Shin | 06566f5 | 2014-05-30 10:26:29 +0900 | [diff] [blame] | 174 | free_buffer_page(sys_heap, buffer, page); |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 175 | return -ENOMEM; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 176 | } |
| 177 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 178 | static void ion_system_heap_free(struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 179 | { |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 180 | struct ion_system_heap *sys_heap = container_of(buffer->heap, |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 181 | struct ion_system_heap, |
| 182 | heap); |
Rebecca Schultz Zavin | 8898227 | 2013-12-13 14:24:26 -0800 | [diff] [blame] | 183 | struct sg_table *table = buffer->sg_table; |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 184 | bool cached = ion_buffer_cached(buffer); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 185 | struct scatterlist *sg; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 186 | int i; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 187 | |
Sriram Raghunathan | 7e41617 | 2015-09-22 22:35:51 +0530 | [diff] [blame] | 188 | /* |
| 189 | * uncached pages come from the page pools, zero them before returning |
| 190 | * for security purposes (other allocations are zerod at |
| 191 | * alloc time |
| 192 | */ |
Mitchel Humpherys | 53a91c6 | 2014-02-17 13:58:39 -0800 | [diff] [blame] | 193 | if (!cached && !(buffer->private_flags & ION_PRIV_FLAG_SHRINKER_FREE)) |
Rebecca Schultz Zavin | 0b6b2cd | 2013-12-13 14:24:32 -0800 | [diff] [blame] | 194 | ion_heap_buffer_zero(buffer); |
Rebecca Schultz Zavin | 77cbe82 | 2013-12-13 14:24:31 -0800 | [diff] [blame] | 195 | |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 196 | for_each_sg(table->sgl, sg, table->nents, i) |
Heesub Shin | 06566f5 | 2014-05-30 10:26:29 +0900 | [diff] [blame] | 197 | free_buffer_page(sys_heap, buffer, sg_page(sg)); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 198 | sg_free_table(table); |
| 199 | kfree(table); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 202 | static struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap, |
| 203 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 204 | { |
| 205 | return buffer->priv_virt; |
| 206 | } |
| 207 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 208 | static void ion_system_heap_unmap_dma(struct ion_heap *heap, |
| 209 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 210 | { |
Rebecca Schultz Zavin | b15934b | 2013-12-13 14:23:41 -0800 | [diff] [blame] | 211 | } |
| 212 | |
Colin Cross | b9daf0b | 2014-02-17 13:58:38 -0800 | [diff] [blame] | 213 | static int ion_system_heap_shrink(struct ion_heap *heap, gfp_t gfp_mask, |
| 214 | int nr_to_scan) |
| 215 | { |
| 216 | struct ion_system_heap *sys_heap; |
| 217 | int nr_total = 0; |
Gioh Kim | b44d9ce | 2015-07-06 15:14:40 +0900 | [diff] [blame] | 218 | int i, nr_freed; |
| 219 | int only_scan = 0; |
Colin Cross | b9daf0b | 2014-02-17 13:58:38 -0800 | [diff] [blame] | 220 | |
| 221 | sys_heap = container_of(heap, struct ion_system_heap, heap); |
| 222 | |
Gioh Kim | b44d9ce | 2015-07-06 15:14:40 +0900 | [diff] [blame] | 223 | if (!nr_to_scan) |
| 224 | only_scan = 1; |
| 225 | |
Colin Cross | b9daf0b | 2014-02-17 13:58:38 -0800 | [diff] [blame] | 226 | for (i = 0; i < num_orders; i++) { |
| 227 | struct ion_page_pool *pool = sys_heap->pools[i]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 228 | |
Gioh Kim | b44d9ce | 2015-07-06 15:14:40 +0900 | [diff] [blame] | 229 | nr_freed = ion_page_pool_shrink(pool, gfp_mask, nr_to_scan); |
| 230 | nr_total += nr_freed; |
| 231 | |
| 232 | if (!only_scan) { |
| 233 | nr_to_scan -= nr_freed; |
| 234 | /* shrink completed */ |
| 235 | if (nr_to_scan <= 0) |
| 236 | break; |
| 237 | } |
Colin Cross | b9daf0b | 2014-02-17 13:58:38 -0800 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | return nr_total; |
| 241 | } |
| 242 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 243 | static struct ion_heap_ops system_heap_ops = { |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 244 | .allocate = ion_system_heap_allocate, |
| 245 | .free = ion_system_heap_free, |
| 246 | .map_dma = ion_system_heap_map_dma, |
| 247 | .unmap_dma = ion_system_heap_unmap_dma, |
Rebecca Schultz Zavin | 8898227 | 2013-12-13 14:24:26 -0800 | [diff] [blame] | 248 | .map_kernel = ion_heap_map_kernel, |
| 249 | .unmap_kernel = ion_heap_unmap_kernel, |
| 250 | .map_user = ion_heap_map_user, |
Colin Cross | b9daf0b | 2014-02-17 13:58:38 -0800 | [diff] [blame] | 251 | .shrink = ion_system_heap_shrink, |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 252 | }; |
| 253 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 254 | static int ion_system_heap_debug_show(struct ion_heap *heap, struct seq_file *s, |
| 255 | void *unused) |
| 256 | { |
| 257 | |
| 258 | struct ion_system_heap *sys_heap = container_of(heap, |
| 259 | struct ion_system_heap, |
| 260 | heap); |
| 261 | int i; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 262 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 263 | for (i = 0; i < num_orders; i++) { |
| 264 | struct ion_page_pool *pool = sys_heap->pools[i]; |
Seunghun Lee | 10f6286 | 2014-05-01 01:30:23 +0900 | [diff] [blame] | 265 | |
Rebecca Schultz Zavin | 0fb9b81 | 2013-12-13 14:24:13 -0800 | [diff] [blame] | 266 | seq_printf(s, "%d order %u highmem pages in pool = %lu total\n", |
| 267 | pool->high_count, pool->order, |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 268 | (PAGE_SIZE << pool->order) * pool->high_count); |
Rebecca Schultz Zavin | 0fb9b81 | 2013-12-13 14:24:13 -0800 | [diff] [blame] | 269 | seq_printf(s, "%d order %u lowmem pages in pool = %lu total\n", |
| 270 | pool->low_count, pool->order, |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 271 | (PAGE_SIZE << pool->order) * pool->low_count); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 272 | } |
| 273 | return 0; |
| 274 | } |
| 275 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 276 | struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused) |
| 277 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 278 | struct ion_system_heap *heap; |
| 279 | int i; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 280 | |
Heesub Shin | 6944561 | 2014-05-30 10:26:30 +0900 | [diff] [blame] | 281 | heap = kzalloc(sizeof(struct ion_system_heap) + |
| 282 | sizeof(struct ion_page_pool *) * num_orders, |
| 283 | GFP_KERNEL); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 284 | if (!heap) |
| 285 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 286 | heap->heap.ops = &system_heap_ops; |
| 287 | heap->heap.type = ION_HEAP_TYPE_SYSTEM; |
Rebecca Schultz Zavin | fe2faea | 2013-12-13 14:24:35 -0800 | [diff] [blame] | 288 | heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE; |
Heesub Shin | 6944561 | 2014-05-30 10:26:30 +0900 | [diff] [blame] | 289 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 290 | for (i = 0; i < num_orders; i++) { |
| 291 | struct ion_page_pool *pool; |
Rebecca Schultz Zavin | ee4a498 | 2013-12-13 14:24:12 -0800 | [diff] [blame] | 292 | gfp_t gfp_flags = low_order_gfp_flags; |
| 293 | |
| 294 | if (orders[i] > 4) |
| 295 | gfp_flags = high_order_gfp_flags; |
| 296 | pool = ion_page_pool_create(gfp_flags, orders[i]); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 297 | if (!pool) |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 298 | goto destroy_pools; |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 299 | heap->pools[i] = pool; |
| 300 | } |
Rebecca Schultz Zavin | ea313b5 | 2013-12-13 14:24:39 -0800 | [diff] [blame] | 301 | |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 302 | heap->heap.debug_show = ion_system_heap_debug_show; |
| 303 | return &heap->heap; |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 304 | |
| 305 | destroy_pools: |
| 306 | while (i--) |
| 307 | ion_page_pool_destroy(heap->pools[i]); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 308 | kfree(heap); |
| 309 | return ERR_PTR(-ENOMEM); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void ion_system_heap_destroy(struct ion_heap *heap) |
| 313 | { |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 314 | struct ion_system_heap *sys_heap = container_of(heap, |
| 315 | struct ion_system_heap, |
| 316 | heap); |
| 317 | int i; |
| 318 | |
| 319 | for (i = 0; i < num_orders; i++) |
| 320 | ion_page_pool_destroy(sys_heap->pools[i]); |
Rebecca Schultz Zavin | 45b17a8 | 2013-12-13 14:24:11 -0800 | [diff] [blame] | 321 | kfree(sys_heap); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | static int ion_system_contig_heap_allocate(struct ion_heap *heap, |
| 325 | struct ion_buffer *buffer, |
| 326 | unsigned long len, |
| 327 | unsigned long align, |
| 328 | unsigned long flags) |
| 329 | { |
Colin Cross | c13d1df | 2013-12-13 14:25:03 -0800 | [diff] [blame] | 330 | int order = get_order(len); |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 331 | struct page *page; |
| 332 | struct sg_table *table; |
| 333 | unsigned long i; |
| 334 | int ret; |
Colin Cross | c13d1df | 2013-12-13 14:25:03 -0800 | [diff] [blame] | 335 | |
| 336 | if (align > (PAGE_SIZE << order)) |
| 337 | return -EINVAL; |
| 338 | |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 339 | page = alloc_pages(low_order_gfp_flags, order); |
| 340 | if (!page) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 341 | return -ENOMEM; |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 342 | |
| 343 | split_page(page, order); |
| 344 | |
| 345 | len = PAGE_ALIGN(len); |
| 346 | for (i = len >> PAGE_SHIFT; i < (1 << order); i++) |
| 347 | __free_page(page + i); |
| 348 | |
Gioh Kim | b615201 | 2014-04-25 08:24:24 +0900 | [diff] [blame] | 349 | table = kmalloc(sizeof(struct sg_table), GFP_KERNEL); |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 350 | if (!table) { |
| 351 | ret = -ENOMEM; |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 352 | goto free_pages; |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | ret = sg_alloc_table(table, 1, GFP_KERNEL); |
| 356 | if (ret) |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 357 | goto free_table; |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 358 | |
| 359 | sg_set_page(table->sgl, page, len, 0); |
| 360 | |
| 361 | buffer->priv_virt = table; |
| 362 | |
| 363 | ion_pages_sync_for_device(NULL, page, len, DMA_BIDIRECTIONAL); |
| 364 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 365 | return 0; |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 366 | |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 367 | free_table: |
| 368 | kfree(table); |
| 369 | free_pages: |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 370 | for (i = 0; i < len >> PAGE_SHIFT; i++) |
| 371 | __free_page(page + i); |
Heesub Shin | 7924074 | 2014-05-28 15:52:52 +0900 | [diff] [blame] | 372 | |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 373 | return ret; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 374 | } |
| 375 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 376 | static void ion_system_contig_heap_free(struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 377 | { |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 378 | struct sg_table *table = buffer->priv_virt; |
| 379 | struct page *page = sg_page(table->sgl); |
| 380 | unsigned long pages = PAGE_ALIGN(buffer->size) >> PAGE_SHIFT; |
| 381 | unsigned long i; |
| 382 | |
| 383 | for (i = 0; i < pages; i++) |
| 384 | __free_page(page + i); |
| 385 | sg_free_table(table); |
| 386 | kfree(table); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | static int ion_system_contig_heap_phys(struct ion_heap *heap, |
| 390 | struct ion_buffer *buffer, |
| 391 | ion_phys_addr_t *addr, size_t *len) |
| 392 | { |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 393 | struct sg_table *table = buffer->priv_virt; |
| 394 | struct page *page = sg_page(table->sgl); |
| 395 | *addr = page_to_phys(page); |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 396 | *len = buffer->size; |
| 397 | return 0; |
| 398 | } |
| 399 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 400 | static struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap, |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 401 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 402 | { |
Colin Cross | 5c6a470 | 2013-12-13 19:26:27 -0800 | [diff] [blame] | 403 | return buffer->priv_virt; |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 404 | } |
| 405 | |
Colin Cross | f63958d | 2013-12-13 19:26:28 -0800 | [diff] [blame] | 406 | static void ion_system_contig_heap_unmap_dma(struct ion_heap *heap, |
| 407 | struct ion_buffer *buffer) |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 408 | { |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 409 | } |
| 410 | |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 411 | static struct ion_heap_ops kmalloc_ops = { |
| 412 | .allocate = ion_system_contig_heap_allocate, |
| 413 | .free = ion_system_contig_heap_free, |
| 414 | .phys = ion_system_contig_heap_phys, |
| 415 | .map_dma = ion_system_contig_heap_map_dma, |
Rebecca Schultz Zavin | 56a7c18 | 2013-12-13 14:23:50 -0800 | [diff] [blame] | 416 | .unmap_dma = ion_system_contig_heap_unmap_dma, |
Rebecca Schultz Zavin | 8898227 | 2013-12-13 14:24:26 -0800 | [diff] [blame] | 417 | .map_kernel = ion_heap_map_kernel, |
| 418 | .unmap_kernel = ion_heap_unmap_kernel, |
Colin Cross | a82130f | 2013-12-13 19:26:15 -0800 | [diff] [blame] | 419 | .map_user = ion_heap_map_user, |
Rebecca Schultz Zavin | c30707b | 2013-12-13 19:38:38 -0800 | [diff] [blame] | 420 | }; |
| 421 | |
| 422 | struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *unused) |
| 423 | { |
| 424 | struct ion_heap *heap; |
| 425 | |
| 426 | heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL); |
| 427 | if (!heap) |
| 428 | return ERR_PTR(-ENOMEM); |
| 429 | heap->ops = &kmalloc_ops; |
| 430 | heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG; |
| 431 | return heap; |
| 432 | } |
| 433 | |
| 434 | void ion_system_contig_heap_destroy(struct ion_heap *heap) |
| 435 | { |
| 436 | kfree(heap); |
| 437 | } |