blob: 4094631a2ed1bb4133b7e93665e63e494f06b07f [file] [log] [blame]
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07001/*
2 * drivers/gpu/ion/ion_system_heap.c
3 *
4 * Copyright (C) 2011 Google, Inc.
Mitchel Humpherysaf3b5222013-01-15 15:38:52 -08005 * Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -07006 *
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 Zavinb831c8c2012-06-14 13:30:01 -070018#include <asm/page.h>
19#include <linux/dma-mapping.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070020#include <linux/err.h>
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070021#include <linux/highmem.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070022#include <linux/ion.h>
23#include <linux/mm.h>
24#include <linux/scatterlist.h>
25#include <linux/slab.h>
26#include <linux/vmalloc.h>
Olav Haugan3d4fe1a2012-01-13 11:42:15 -080027#include <linux/seq_file.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070028#include "ion_priv.h"
Laura Abbottabcb6f72011-10-04 16:26:49 -070029#include <mach/memory.h>
Olav Haugan85c95402012-05-30 17:32:37 -070030#include <asm/cacheflush.h>
Mitchel Humpherysaf2e5c52012-09-06 12:16:36 -070031#include <linux/msm_ion.h>
Neeti Desai3f3c2822013-03-08 17:29:53 -080032#include <linux/dma-mapping.h>
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070033
Laura Abbott68c80642011-10-21 17:32:27 -070034static atomic_t system_heap_allocated;
35static atomic_t system_contig_heap_allocated;
36
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070037struct page_info {
38 struct page *page;
39 unsigned long order;
40 struct list_head list;
41};
42
Rebecca Schultz Zavin96dd58d2012-09-26 10:58:30 -070043static unsigned int orders[] = {8, 4, 0};
44
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -070045static struct page_info *alloc_largest_available(unsigned long size,
Rebecca Schultz Zavin158316f2012-09-25 20:55:27 -070046 bool split_pages,
47 unsigned int max_order)
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070048{
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070049 struct page *page;
50 struct page_info *info;
51 int i;
52
53 for (i = 0; i < ARRAY_SIZE(orders); i++) {
54 if (size < (1 << orders[i]) * PAGE_SIZE)
55 continue;
Rebecca Schultz Zavin158316f2012-09-25 20:55:27 -070056 if (max_order < orders[i])
57 continue;
Dima Zavindcd71bc2012-07-27 15:29:55 -070058 page = alloc_pages(GFP_HIGHUSER | __GFP_ZERO |
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070059 __GFP_NOWARN | __GFP_NORETRY, orders[i]);
60 if (!page)
61 continue;
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -070062 if (split_pages)
63 split_page(page, orders[i]);
Rebecca Schultz Zavin6a93a292012-08-21 21:35:20 -070064 info = kmalloc(sizeof(struct page_info *), GFP_KERNEL);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070065 info->page = page;
66 info->order = orders[i];
67 return info;
68 }
69 return NULL;
70}
71
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -070072static int ion_system_heap_allocate(struct ion_heap *heap,
73 struct ion_buffer *buffer,
74 unsigned long size, unsigned long align,
75 unsigned long flags)
76{
Laura Abbottb14ed962012-01-30 14:18:08 -080077 struct sg_table *table;
78 struct scatterlist *sg;
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070079 int ret;
80 struct list_head pages;
81 struct page_info *info, *tmp_info;
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -070082 int i = 0;
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070083 long size_remaining = PAGE_ALIGN(size);
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -070084 bool split_pages = ion_buffer_fault_user_mappings(buffer);
85
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070086
Rebecca Schultz Zavin158316f2012-09-25 20:55:27 -070087 unsigned int max_order = orders[0];
88
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070089 INIT_LIST_HEAD(&pages);
90 while (size_remaining > 0) {
Rebecca Schultz Zavin158316f2012-09-25 20:55:27 -070091 info = alloc_largest_available(size_remaining, split_pages,
92 max_order);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070093 if (!info)
94 goto err;
95 list_add_tail(&info->list, &pages);
96 size_remaining -= (1 << info->order) * PAGE_SIZE;
Rebecca Schultz Zavin158316f2012-09-25 20:55:27 -070097 max_order = info->order;
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -070098 i++;
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -070099 }
Laura Abbott68c80642011-10-21 17:32:27 -0700100
Laura Abbottb14ed962012-01-30 14:18:08 -0800101 table = kmalloc(sizeof(struct sg_table), GFP_KERNEL);
102 if (!table)
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700103 goto err;
104
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -0700105 if (split_pages)
106 ret = sg_alloc_table(table, PAGE_ALIGN(size) / PAGE_SIZE,
107 GFP_KERNEL);
108 else
109 ret = sg_alloc_table(table, i, GFP_KERNEL);
110
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700111 if (ret)
112 goto err1;
113
114 sg = table->sgl;
115 list_for_each_entry_safe(info, tmp_info, &pages, list) {
116 struct page *page = info->page;
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -0700117
118 if (split_pages) {
119 for (i = 0; i < (1 << info->order); i++) {
120 sg_set_page(sg, page + i, PAGE_SIZE, 0);
121 sg = sg_next(sg);
122 }
123 } else {
124 sg_set_page(sg, page, (1 << info->order) * PAGE_SIZE,
125 0);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700126 sg = sg_next(sg);
127 }
128 list_del(&info->list);
Rebecca Schultz Zavin6a93a292012-08-21 21:35:20 -0700129 kfree(info);
Laura Abbottb14ed962012-01-30 14:18:08 -0800130 }
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700131
132 dma_sync_sg_for_device(NULL, table->sgl, table->nents,
133 DMA_BIDIRECTIONAL);
134
Laura Abbottb14ed962012-01-30 14:18:08 -0800135 buffer->priv_virt = table;
Laura Abbott68c80642011-10-21 17:32:27 -0700136 atomic_add(size, &system_heap_allocated);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700137 return 0;
Laura Abbottb14ed962012-01-30 14:18:08 -0800138err1:
Laura Abbottb14ed962012-01-30 14:18:08 -0800139 kfree(table);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700140err:
141 list_for_each_entry(info, &pages, list) {
Rebecca Schultz Zavinf858ba42012-09-21 11:46:06 -0700142 if (split_pages)
143 for (i = 0; i < (1 << info->order); i++)
144 __free_page(info->page + i);
145 else
146 __free_pages(info->page, info->order);
147
Rebecca Schultz Zavin6a93a292012-08-21 21:35:20 -0700148 kfree(info);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700149 }
Laura Abbottb14ed962012-01-30 14:18:08 -0800150 return -ENOMEM;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700151}
152
153void ion_system_heap_free(struct ion_buffer *buffer)
154{
Laura Abbottb14ed962012-01-30 14:18:08 -0800155 int i;
156 struct scatterlist *sg;
157 struct sg_table *table = buffer->priv_virt;
158
159 for_each_sg(table->sgl, sg, table->nents, i)
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700160 __free_pages(sg_page(sg), get_order(sg_dma_len(sg)));
Laura Abbottb14ed962012-01-30 14:18:08 -0800161 if (buffer->sg_table)
162 sg_free_table(buffer->sg_table);
163 kfree(buffer->sg_table);
Laura Abbott68c80642011-10-21 17:32:27 -0700164 atomic_sub(buffer->size, &system_heap_allocated);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700165}
166
Laura Abbottb14ed962012-01-30 14:18:08 -0800167struct sg_table *ion_system_heap_map_dma(struct ion_heap *heap,
168 struct ion_buffer *buffer)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700169{
Laura Abbottb14ed962012-01-30 14:18:08 -0800170 return buffer->priv_virt;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700171}
172
173void ion_system_heap_unmap_dma(struct ion_heap *heap,
174 struct ion_buffer *buffer)
175{
Laura Abbottb14ed962012-01-30 14:18:08 -0800176 return;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700177}
178
179void *ion_system_heap_map_kernel(struct ion_heap *heap,
Laura Abbottb14ed962012-01-30 14:18:08 -0800180 struct ion_buffer *buffer)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700181{
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700182 struct scatterlist *sg;
183 int i, j;
184 void *vaddr;
185 pgprot_t pgprot;
186 struct sg_table *table = buffer->priv_virt;
187 int npages = PAGE_ALIGN(buffer->size) / PAGE_SIZE;
Rebecca Schultz Zavin21413b62012-09-30 14:53:27 -0700188 struct page **pages = vmalloc(sizeof(struct page *) * npages);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700189 struct page **tmp = pages;
Laura Abbottb14ed962012-01-30 14:18:08 -0800190
Rebecca Schultz Zavin21413b62012-09-30 14:53:27 -0700191 if (!pages)
192 return 0;
193
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700194 if (buffer->flags & ION_FLAG_CACHED)
195 pgprot = PAGE_KERNEL;
196 else
197 pgprot = pgprot_writecombine(PAGE_KERNEL);
Laura Abbottb14ed962012-01-30 14:18:08 -0800198
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700199 for_each_sg(table->sgl, sg, table->nents, i) {
200 int npages_this_entry = PAGE_ALIGN(sg_dma_len(sg)) / PAGE_SIZE;
201 struct page *page = sg_page(sg);
202 BUG_ON(i >= npages);
203 for (j = 0; j < npages_this_entry; j++) {
204 *(tmp++) = page++;
205 }
Laura Abbott894fd582011-08-19 13:33:56 -0700206 }
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700207 vaddr = vmap(pages, npages, VM_MAP, pgprot);
Rebecca Schultz Zavin21413b62012-09-30 14:53:27 -0700208 vfree(pages);
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700209
210 return vaddr;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700211}
212
213void ion_system_heap_unmap_kernel(struct ion_heap *heap,
214 struct ion_buffer *buffer)
215{
Laura Abbottb14ed962012-01-30 14:18:08 -0800216 vunmap(buffer->vaddr);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700217}
218
219int ion_system_heap_map_user(struct ion_heap *heap, struct ion_buffer *buffer,
Laura Abbottb14ed962012-01-30 14:18:08 -0800220 struct vm_area_struct *vma)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700221{
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700222 struct sg_table *table = buffer->priv_virt;
223 unsigned long addr = vma->vm_start;
224 unsigned long offset = vma->vm_pgoff;
225 struct scatterlist *sg;
226 int i;
227
Laura Abbottb14ed962012-01-30 14:18:08 -0800228 if (!ION_IS_CACHED(buffer->flags)) {
Laura Abbott894fd582011-08-19 13:33:56 -0700229 pr_err("%s: cannot map system heap uncached\n", __func__);
230 return -EINVAL;
231 }
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700232
233 for_each_sg(table->sgl, sg, table->nents, i) {
234 if (offset) {
235 offset--;
236 continue;
237 }
238 remap_pfn_range(vma, addr, page_to_pfn(sg_page(sg)),
239 sg_dma_len(sg), vma->vm_page_prot);
240 addr += sg_dma_len(sg);
Rebecca Schultz Zavin805f1302012-08-09 21:29:52 -0700241 if (addr >= vma->vm_end)
242 return 0;
Rebecca Schultz Zavinb831c8c2012-06-14 13:30:01 -0700243 }
244 return 0;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700245}
246
Olav Haugan0671b9a2012-05-25 11:58:56 -0700247static int ion_system_print_debug(struct ion_heap *heap, struct seq_file *s,
248 const struct rb_root *unused)
Laura Abbott68c80642011-10-21 17:32:27 -0700249{
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800250 seq_printf(s, "total bytes currently allocated: %lx\n",
251 (unsigned long) atomic_read(&system_heap_allocated));
252
253 return 0;
Laura Abbott68c80642011-10-21 17:32:27 -0700254}
255
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700256static struct ion_heap_ops vmalloc_ops = {
257 .allocate = ion_system_heap_allocate,
258 .free = ion_system_heap_free,
259 .map_dma = ion_system_heap_map_dma,
260 .unmap_dma = ion_system_heap_unmap_dma,
261 .map_kernel = ion_system_heap_map_kernel,
262 .unmap_kernel = ion_system_heap_unmap_kernel,
263 .map_user = ion_system_heap_map_user,
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800264 .print_debug = ion_system_print_debug,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700265};
266
Olav Haugan85c95402012-05-30 17:32:37 -0700267struct ion_heap *ion_system_heap_create(struct ion_platform_heap *pheap)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700268{
269 struct ion_heap *heap;
270
271 heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
272 if (!heap)
273 return ERR_PTR(-ENOMEM);
274 heap->ops = &vmalloc_ops;
275 heap->type = ION_HEAP_TYPE_SYSTEM;
276 return heap;
277}
278
279void ion_system_heap_destroy(struct ion_heap *heap)
280{
281 kfree(heap);
282}
283
284static int ion_system_contig_heap_allocate(struct ion_heap *heap,
285 struct ion_buffer *buffer,
286 unsigned long len,
287 unsigned long align,
288 unsigned long flags)
289{
290 buffer->priv_virt = kzalloc(len, GFP_KERNEL);
291 if (!buffer->priv_virt)
292 return -ENOMEM;
Laura Abbott68c80642011-10-21 17:32:27 -0700293 atomic_add(len, &system_contig_heap_allocated);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700294 return 0;
295}
296
297void ion_system_contig_heap_free(struct ion_buffer *buffer)
298{
299 kfree(buffer->priv_virt);
Laura Abbott68c80642011-10-21 17:32:27 -0700300 atomic_sub(buffer->size, &system_contig_heap_allocated);
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700301}
302
303static int ion_system_contig_heap_phys(struct ion_heap *heap,
304 struct ion_buffer *buffer,
305 ion_phys_addr_t *addr, size_t *len)
306{
307 *addr = virt_to_phys(buffer->priv_virt);
308 *len = buffer->size;
309 return 0;
310}
311
Laura Abbottb14ed962012-01-30 14:18:08 -0800312struct sg_table *ion_system_contig_heap_map_dma(struct ion_heap *heap,
Rebecca Schultz Zavinb1790672012-06-14 15:08:53 -0700313 struct ion_buffer *buffer)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700314{
Laura Abbottb14ed962012-01-30 14:18:08 -0800315 struct sg_table *table;
316 int ret;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700317
Laura Abbottb14ed962012-01-30 14:18:08 -0800318 table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
319 if (!table)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700320 return ERR_PTR(-ENOMEM);
Laura Abbottb14ed962012-01-30 14:18:08 -0800321 ret = sg_alloc_table(table, 1, GFP_KERNEL);
322 if (ret) {
323 kfree(table);
324 return ERR_PTR(ret);
325 }
326 sg_set_page(table->sgl, virt_to_page(buffer->priv_virt), buffer->size,
327 0);
328 return table;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700329}
330
Rebecca Schultz Zavinb1790672012-06-14 15:08:53 -0700331void ion_system_contig_heap_unmap_dma(struct ion_heap *heap,
332 struct ion_buffer *buffer)
333{
334 sg_free_table(buffer->sg_table);
335 kfree(buffer->sg_table);
336}
337
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700338int ion_system_contig_heap_map_user(struct ion_heap *heap,
339 struct ion_buffer *buffer,
Laura Abbottb14ed962012-01-30 14:18:08 -0800340 struct vm_area_struct *vma)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700341{
342 unsigned long pfn = __phys_to_pfn(virt_to_phys(buffer->priv_virt));
Laura Abbott894fd582011-08-19 13:33:56 -0700343
Laura Abbottb14ed962012-01-30 14:18:08 -0800344 if (ION_IS_CACHED(buffer->flags))
Laura Abbott894fd582011-08-19 13:33:56 -0700345 return remap_pfn_range(vma, vma->vm_start, pfn + vma->vm_pgoff,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700346 vma->vm_end - vma->vm_start,
347 vma->vm_page_prot);
Laura Abbott894fd582011-08-19 13:33:56 -0700348 else {
349 pr_err("%s: cannot map system heap uncached\n", __func__);
350 return -EINVAL;
351 }
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700352}
353
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800354static int ion_system_contig_print_debug(struct ion_heap *heap,
Olav Haugan0671b9a2012-05-25 11:58:56 -0700355 struct seq_file *s,
356 const struct rb_root *unused)
Laura Abbott68c80642011-10-21 17:32:27 -0700357{
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800358 seq_printf(s, "total bytes currently allocated: %lx\n",
359 (unsigned long) atomic_read(&system_contig_heap_allocated));
360
361 return 0;
Laura Abbott68c80642011-10-21 17:32:27 -0700362}
363
Rohit Vaswani35edc882012-11-20 10:20:47 -0800364void *ion_system_contig_heap_map_kernel(struct ion_heap *heap,
365 struct ion_buffer *buffer)
366{
367 return buffer->priv_virt;
368}
369
370void ion_system_contig_heap_unmap_kernel(struct ion_heap *heap,
371 struct ion_buffer *buffer)
372{
373 return;
374}
375
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700376static struct ion_heap_ops kmalloc_ops = {
377 .allocate = ion_system_contig_heap_allocate,
378 .free = ion_system_contig_heap_free,
379 .phys = ion_system_contig_heap_phys,
380 .map_dma = ion_system_contig_heap_map_dma,
Rebecca Schultz Zavinb1790672012-06-14 15:08:53 -0700381 .unmap_dma = ion_system_contig_heap_unmap_dma,
Rohit Vaswani35edc882012-11-20 10:20:47 -0800382 .map_kernel = ion_system_contig_heap_map_kernel,
383 .unmap_kernel = ion_system_contig_heap_unmap_kernel,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700384 .map_user = ion_system_contig_heap_map_user,
Olav Haugan3d4fe1a2012-01-13 11:42:15 -0800385 .print_debug = ion_system_contig_print_debug,
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700386};
387
Olav Haugan85c95402012-05-30 17:32:37 -0700388struct ion_heap *ion_system_contig_heap_create(struct ion_platform_heap *pheap)
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700389{
390 struct ion_heap *heap;
391
392 heap = kzalloc(sizeof(struct ion_heap), GFP_KERNEL);
393 if (!heap)
394 return ERR_PTR(-ENOMEM);
395 heap->ops = &kmalloc_ops;
396 heap->type = ION_HEAP_TYPE_SYSTEM_CONTIG;
Rebecca Schultz Zavinc80005a2011-06-29 19:44:29 -0700397 return heap;
398}
399
400void ion_system_contig_heap_destroy(struct ion_heap *heap)
401{
402 kfree(heap);
403}
404