blob: fcfbb661f2df200fc7ae6c009143bb212a025e9a [file] [log] [blame]
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -08001/*
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 Zavinbd5d6bd2013-12-13 14:23:51 -080017#include <asm/page.h>
18#include <linux/dma-mapping.h>
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080019#include <linux/err.h>
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080020#include <linux/highmem.h>
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080021#include <linux/mm.h>
22#include <linux/scatterlist.h>
23#include <linux/slab.h>
24#include <linux/vmalloc.h>
25#include "ion.h"
26#include "ion_priv.h"
27
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080028struct page_info {
29 struct page *page;
30 unsigned long order;
31 struct list_head list;
32};
33
Rebecca Schultz Zavinb0599c02013-12-13 14:24:08 -080034static unsigned int orders[] = {8, 4, 0};
35
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -080036static struct page_info *alloc_largest_available(unsigned long size,
Rebecca Schultz Zavinba96a2e2013-12-13 14:24:07 -080037 bool split_pages,
38 unsigned int max_order)
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080039{
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080040 struct page *page;
41 struct page_info *info;
42 int i;
43
44 for (i = 0; i < ARRAY_SIZE(orders); i++) {
45 if (size < (1 << orders[i]) * PAGE_SIZE)
46 continue;
Rebecca Schultz Zavinba96a2e2013-12-13 14:24:07 -080047 if (max_order < orders[i])
48 continue;
Dima Zavinfe65ec52013-12-13 14:23:53 -080049 page = alloc_pages(GFP_HIGHUSER | __GFP_ZERO |
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080050 __GFP_NOWARN | __GFP_NORETRY, orders[i]);
51 if (!page)
52 continue;
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -080053 if (split_pages)
54 split_page(page, orders[i]);
Rebecca Schultz Zavin708f0ca2013-12-13 14:23:59 -080055 info = kmalloc(sizeof(struct page_info *), GFP_KERNEL);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080056 info->page = page;
57 info->order = orders[i];
58 return info;
59 }
60 return NULL;
61}
62
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -080063static int ion_system_heap_allocate(struct ion_heap *heap,
64 struct ion_buffer *buffer,
65 unsigned long size, unsigned long align,
66 unsigned long flags)
67{
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -080068 struct sg_table *table;
69 struct scatterlist *sg;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080070 int ret;
71 struct list_head pages;
72 struct page_info *info, *tmp_info;
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -080073 int i = 0;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080074 long size_remaining = PAGE_ALIGN(size);
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -080075 bool split_pages = ion_buffer_fault_user_mappings(buffer);
76
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080077
Rebecca Schultz Zavinba96a2e2013-12-13 14:24:07 -080078 unsigned int max_order = orders[0];
79
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080080 INIT_LIST_HEAD(&pages);
81 while (size_remaining > 0) {
Rebecca Schultz Zavinba96a2e2013-12-13 14:24:07 -080082 info = alloc_largest_available(size_remaining, split_pages,
83 max_order);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080084 if (!info)
85 goto err;
86 list_add_tail(&info->list, &pages);
87 size_remaining -= (1 << info->order) * PAGE_SIZE;
Rebecca Schultz Zavinba96a2e2013-12-13 14:24:07 -080088 max_order = info->order;
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -080089 i++;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080090 }
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -080091
92 table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
93 if (!table)
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -080094 goto err;
95
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -080096 if (split_pages)
97 ret = sg_alloc_table(table, PAGE_ALIGN(size) / PAGE_SIZE,
98 GFP_KERNEL);
99 else
100 ret = sg_alloc_table(table, i, GFP_KERNEL);
101
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800102 if (ret)
103 goto err1;
104
105 sg = table->sgl;
106 list_for_each_entry_safe(info, tmp_info, &pages, list) {
107 struct page *page = info->page;
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -0800108
109 if (split_pages) {
110 for (i = 0; i < (1 << info->order); i++) {
111 sg_set_page(sg, page + i, PAGE_SIZE, 0);
112 sg = sg_next(sg);
113 }
114 } else {
115 sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE,
116 0);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800117 sg = sg_next(sg);
118 }
119 list_del(&info->list);
Rebecca Schultz Zavin708f0ca2013-12-13 14:23:59 -0800120 kfree(info);
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800121 }
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800122
123 dma_sync_sg_for_device(NULL, table->sgl, table->nents,
124 DMA_BIDIRECTIONAL);
125
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800126 buffer->priv_virt = table;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800127 return 0;
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800128err1:
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800129 kfree(table);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800130err:
131 list_for_each_entry(info, &pages, list) {
Rebecca Schultz Zavin13ba7802013-12-13 14:24:06 -0800132 if (split_pages)
133 for (i = 0; i < (1 << info->order); i++)
134 __free_page(info->page + i);
135 else
136 __free_pages(info->page, info->order);
137
Rebecca Schultz Zavin708f0ca2013-12-13 14:23:59 -0800138 kfree(info);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800139 }
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800140 return -ENOMEM;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800141}
142
143void ion_system_heap_free(struct ion_buffer *buffer)
144{
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800145 int i;
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800146 struct scatterlist *sg;
147 struct sg_table *table = buffer->priv_virt;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800148
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800149 for_each_sg(table->sgl, sg, table->nents, i)
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800150 __free_pages(sg_page(sg), get_order(sg_dma_len(sg)));
Rebecca Schultz Zavin4d5ca322013-12-13 14:23:37 -0800151 if (buffer->sg_table)
152 sg_free_table(buffer->sg_table);
153 kfree(buffer->sg_table);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800154}
155
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800156struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap,
157 struct ion_buffer *buffer)
158{
159 return buffer->priv_virt;
160}
161
162void ion_system_heap_unmap_dma(struct ion_heap *heap,
163 struct ion_buffer *buffer)
164{
165 return;
166}
167
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800168void *ion_system_heap_map_kernel(struct ion_heap *heap,
169 struct ion_buffer *buffer)
170{
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800171 struct scatterlist *sg;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800172 int i, j;
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800173 void *vaddr;
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800174 pgprot_t pgprot;
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800175 struct sg_table *table = buffer->priv_virt;
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800176 int npages = PAGE_ALIGN(buffer->size) / PAGE_SIZE;
Rebecca Schultz Zavin98d5d5f2013-12-13 14:24:09 -0800177 struct page **pages = vmalloc(sizeof(struct page *) * npages);
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800178 struct page **tmp = pages;
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800179
Rebecca Schultz Zavin98d5d5f2013-12-13 14:24:09 -0800180 if (!pages)
181 return 0;
182
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800183 if (buffer->flags & ION_FLAG_CACHED)
184 pgprot = PAGE_KERNEL;
185 else
186 pgprot = pgprot_writecombine(PAGE_KERNEL);
187
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800188 for_each_sg(table->sgl, sg, table->nents, i) {
189 int npages_this_entry = PAGE_ALIGN(sg_dma_len(sg)) / PAGE_SIZE;
190 struct page *page = sg_page(sg);
191 BUG_ON(i >= npages);
192 for (j = 0; j < npages_this_entry; j++) {
193 *(tmp++) = page++;
194 }
195 }
196 vaddr = vmap(pages, npages, VM_MAP, pgprot);
Rebecca Schultz Zavin98d5d5f2013-12-13 14:24:09 -0800197 vfree(pages);
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800198
199 return vaddr;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800200}
201
202void ion_system_heap_unmap_kernel(struct ion_heap *heap,
203 struct ion_buffer *buffer)
204{
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800205 vunmap(buffer->vaddr);
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800206}
207
208int ion_system_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer,
209 struct vm_area_struct *vma)
210{
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800211 struct sg_table *table = buffer->priv_virt;
212 unsigned long addr = vma->vm_start;
213 unsigned long offset = vma->vm_pgoff;
214 struct scatterlist *sg;
215 int i;
216
217 for_each_sg(table->sgl, sg, table->nents, i) {
218 if (offset) {
219 offset--;
220 continue;
221 }
Rebecca Schultz Zavinbd5d6bd2013-12-13 14:23:51 -0800222 remap_pfn_range(vma, addr, page_to_pfn(sg_page(sg)),
223 sg_dma_len(sg), vma->vm_page_prot);
224 addr += sg_dma_len(sg);
Rebecca Schultz Zavinb8230242013-12-13 14:23:58 -0800225 if (addr >= vma->vm_end)
226 return 0;
Rebecca Schultz Zavinb15934b2013-12-13 14:23:41 -0800227 }
228 return 0;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800229}
230
231static struct ion_heap_ops vmalloc_ops = {
232 .allocate = ion_system_heap_allocate,
233 .free = ion_system_heap_free,
234 .map_dma = ion_system_heap_map_dma,
235 .unmap_dma = ion_system_heap_unmap_dma,
236 .map_kernel = ion_system_heap_map_kernel,
237 .unmap_kernel = ion_system_heap_unmap_kernel,
238 .map_user = ion_system_heap_map_user,
239};
240
241struct ion_heap *ion_system_heap_create(struct ion_platform_heap *unused)
242{
243 struct ion_heap *heap;
244
245 heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
246 if (!heap)
247 return ERR_PTR(-ENOMEM);
248 heap->ops = &vmalloc_ops;
249 heap->type = ION_HEAP_TYPE_SYSTEM;
250 return heap;
251}
252
253void ion_system_heap_destroy(struct ion_heap *heap)
254{
255 kfree(heap);
256}
257
258static int ion_system_contig_heap_allocate(struct ion_heap *heap,
259 struct ion_buffer *buffer,
260 unsigned long len,
261 unsigned long align,
262 unsigned long flags)
263{
264 buffer->priv_virt = kzalloc(len, GFP_KERNEL);
265 if (!buffer->priv_virt)
266 return -ENOMEM;
267 return 0;
268}
269
270void ion_system_contig_heap_free(struct ion_buffer *buffer)
271{
272 kfree(buffer->priv_virt);
273}
274
275static int ion_system_contig_heap_phys(struct ion_heap *heap,
276 struct ion_buffer *buffer,
277 ion_phys_addr_t *addr, size_t *len)
278{
279 *addr = virt_to_phys(buffer->priv_virt);
280 *len = buffer->size;
281 return 0;
282}
283
Rebecca Schultz Zavin4d5ca322013-12-13 14:23:37 -0800284struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap,
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800285 struct ion_buffer *buffer)
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800286{
Rebecca Schultz Zavin4d5ca322013-12-13 14:23:37 -0800287 struct sg_table *table;
288 int ret;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800289
Rebecca Schultz Zavin4d5ca322013-12-13 14:23:37 -0800290 table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
291 if (!table)
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800292 return ERR_PTR(-ENOMEM);
Rebecca Schultz Zavin4d5ca322013-12-13 14:23:37 -0800293 ret = sg_alloc_table(table, 1, GFP_KERNEL);
294 if (ret) {
295 kfree(table);
296 return ERR_PTR(ret);
297 }
298 sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size,
299 0);
300 return table;
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800301}
302
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800303void ion_system_contig_heap_unmap_dma(struct ion_heap *heap,
304 struct ion_buffer *buffer)
305{
306 sg_free_table(buffer->sg_table);
307 kfree(buffer->sg_table);
308}
309
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800310int ion_system_contig_heap_map_user(struct ion_heap *heap,
311 struct ion_buffer *buffer,
312 struct vm_area_struct *vma)
313{
314 unsigned long pfn = __phys_to_pfn(virt_to_phys(buffer->priv_virt));
315 return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
316 vma->vm_end - vma->vm_start,
317 vma->vm_page_prot);
318
319}
320
321static struct ion_heap_ops kmalloc_ops = {
322 .allocate = ion_system_contig_heap_allocate,
323 .free = ion_system_contig_heap_free,
324 .phys = ion_system_contig_heap_phys,
325 .map_dma = ion_system_contig_heap_map_dma,
Rebecca Schultz Zavin56a7c182013-12-13 14:23:50 -0800326 .unmap_dma = ion_system_contig_heap_unmap_dma,
Rebecca Schultz Zavinc30707b2013-12-13 19:38:38 -0800327 .map_kernel = ion_system_heap_map_kernel,
328 .unmap_kernel = ion_system_heap_unmap_kernel,
329 .map_user = ion_system_contig_heap_map_user,
330};
331
332struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *unused)
333{
334 struct ion_heap *heap;
335
336 heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
337 if (!heap)
338 return ERR_PTR(-ENOMEM);
339 heap->ops = &kmalloc_ops;
340 heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG;
341 return heap;
342}
343
344void ion_system_contig_heap_destroy(struct ion_heap *heap)
345{
346 kfree(heap);
347}
348