1 /*************************************************************************/ /*!
3 @Copyright Copyright (c) Imagination Technologies Ltd. All Rights Reserved
4 @License Dual MIT/GPLv2
6 The contents of this file are subject to the MIT license as set out below.
8 Permission is hereby granted, free of charge, to any person obtaining a copy
9 of this software and associated documentation files (the "Software"), to deal
10 in the Software without restriction, including without limitation the rights
11 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 copies of the Software, and to permit persons to whom the Software is
13 furnished to do so, subject to the following conditions:
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
18 Alternatively, the contents of this file may be used under the terms of
19 the GNU General Public License Version 2 ("GPL") in which case the provisions
20 of GPL are applicable instead of those above.
22 If you wish to allow use of your version of this file only under the terms of
23 GPL, and not to allow others to use your version of this file under the terms
24 of the MIT license, indicate your decision by deleting the provisions above
25 and replace them with the notice and other provisions required by GPL as set
26 out in the file called "GPL-COPYING" included in this distribution. If you do
27 not delete the provisions above, a recipient may use your version of this file
28 under the terms of either the MIT license or GPL.
30 This License is also included in this distribution in the file called
33 EXCEPT AS OTHERWISE STATED IN A NEGOTIATED AGREEMENT: (A) THE SOFTWARE IS
34 PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
35 BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
36 PURPOSE AND NONINFRINGEMENT; AND (B) IN NO EVENT SHALL THE AUTHORS OR
37 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
38 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
39 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40 */ /**************************************************************************/
43 #include "ion_lma_heap.h"
46 #include <linux/slab.h>
47 #include <linux/genalloc.h>
48 #include <linux/scatterlist.h>
50 /* Ion heap for LMA allocations. This heap is identical to CARVEOUT except
51 * that it does not do any CPU cache maintenance nor does it zero the memory
52 * using the CPU (this is handled with PVR_ANDROID_DEFER_CLEAR in userspace).
57 struct gen_pool *pool;
61 static ion_phys_addr_t ion_lma_allocate(struct ion_heap *heap,
65 struct ion_lma_heap *lma_heap =
66 container_of(heap, struct ion_lma_heap, heap);
67 unsigned long offset = gen_pool_alloc(lma_heap->pool, size);
70 return ION_CARVEOUT_ALLOCATE_FAIL;
75 static void ion_lma_free(struct ion_heap *heap, ion_phys_addr_t addr,
78 struct ion_lma_heap *lma_heap =
79 container_of(heap, struct ion_lma_heap, heap);
81 if (addr == ION_CARVEOUT_ALLOCATE_FAIL)
84 gen_pool_free(lma_heap->pool, addr, size);
87 static int ion_lma_heap_phys(struct ion_heap *heap,
88 struct ion_buffer *buffer,
89 ion_phys_addr_t *addr, size_t *len)
91 struct sg_table *table = buffer->priv_virt;
92 struct page *page = sg_page(table->sgl);
93 ion_phys_addr_t paddr = PFN_PHYS(page_to_pfn(page));
100 static int ion_lma_heap_allocate(struct ion_heap *heap,
101 struct ion_buffer *buffer,
102 unsigned long size, unsigned long align,
105 struct sg_table *table;
106 ion_phys_addr_t paddr;
109 if (align > PAGE_SIZE)
112 table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
116 ret = sg_alloc_table(table, 1, GFP_KERNEL);
120 paddr = ion_lma_allocate(heap, size, align);
121 if (paddr == ION_CARVEOUT_ALLOCATE_FAIL) {
126 sg_set_page(table->sgl, pfn_to_page(PFN_DOWN(paddr)), size, 0);
127 buffer->priv_virt = table;
131 sg_free_table(table);
137 static void ion_lma_heap_free(struct ion_buffer *buffer)
139 struct ion_heap *heap = buffer->heap;
140 struct sg_table *table = buffer->priv_virt;
141 struct page *page = sg_page(table->sgl);
142 ion_phys_addr_t paddr = PFN_PHYS(page_to_pfn(page));
144 /* Do not zero the LMA heap from the CPU. This is very slow with
145 * the current TCF (w/ no DMA engine). We will use the TLA to clear
146 * the memory with Rogue in another place.
148 * We also skip the CPU cache maintenance for the heap space, as we
149 * statically know that the TCF PCI memory bar has UC/WC set by the
150 * MTRR/PAT subsystem.
153 ion_lma_free(heap, paddr, buffer->size);
154 sg_free_table(table);
158 static struct sg_table *ion_lma_heap_map_dma(struct ion_heap *heap,
159 struct ion_buffer *buffer)
161 return buffer->priv_virt;
164 static void ion_lma_heap_unmap_dma(struct ion_heap *heap,
165 struct ion_buffer *buffer)
170 static int ion_lma_heap_map_user(struct ion_heap *mapper,
171 struct ion_buffer *buffer,
172 struct vm_area_struct *vma)
174 struct sg_table *table = buffer->priv_virt;
175 struct page *page = sg_page(table->sgl);
176 ion_phys_addr_t paddr = PFN_PHYS(page_to_pfn(page));
178 return remap_pfn_range(vma, vma->vm_start,
179 PFN_DOWN(paddr) + vma->vm_pgoff,
180 vma->vm_end - vma->vm_start,
181 pgprot_writecombine(vma->vm_page_prot));
184 static void *ion_lma_heap_map_kernel(struct ion_heap *heap,
185 struct ion_buffer *buffer)
187 struct sg_table *table = buffer->priv_virt;
188 struct page *page = sg_page(table->sgl);
189 ion_phys_addr_t paddr = PFN_PHYS(page_to_pfn(page));
191 return ioremap_wc(paddr, buffer->size);
194 static void ion_lma_heap_unmap_kernel(struct ion_heap *heap,
195 struct ion_buffer *buffer)
197 iounmap(buffer->vaddr);
200 static struct ion_heap_ops lma_heap_ops = {
201 .allocate = ion_lma_heap_allocate,
202 .free = ion_lma_heap_free,
203 .phys = ion_lma_heap_phys,
204 .map_dma = ion_lma_heap_map_dma,
205 .unmap_dma = ion_lma_heap_unmap_dma,
206 .map_user = ion_lma_heap_map_user,
207 .map_kernel = ion_lma_heap_map_kernel,
208 .unmap_kernel = ion_lma_heap_unmap_kernel,
211 struct ion_heap *ion_lma_heap_create(struct ion_platform_heap *heap_data)
213 struct ion_lma_heap *lma_heap;
214 size_t size = heap_data->size;
217 page = pfn_to_page(PFN_DOWN(heap_data->base));
219 /* Do not zero the LMA heap from the CPU. This is very slow with
220 * the current TCF (w/ no DMA engine). We will use the TLA to clear
221 * the memory with Rogue in another place.
223 * We also skip the CPU cache maintenance for the heap space, as we
224 * statically know that the TCF PCI memory bar has UC/WC set by the
225 * MTRR/PAT subsystem.
228 lma_heap = kzalloc(sizeof(struct ion_lma_heap), GFP_KERNEL);
230 return ERR_PTR(-ENOMEM);
232 lma_heap->pool = gen_pool_create(12, -1);
233 if (!lma_heap->pool) {
235 return ERR_PTR(-ENOMEM);
238 lma_heap->base = heap_data->base;
239 gen_pool_add(lma_heap->pool, lma_heap->base, size, -1);
241 lma_heap->heap.id = heap_data->id;
242 lma_heap->heap.ops = &lma_heap_ops;
243 lma_heap->heap.name = heap_data->name;
244 lma_heap->heap.type = ION_HEAP_TYPE_CUSTOM;
245 lma_heap->heap.flags = ION_HEAP_FLAG_DEFER_FREE;
247 return &lma_heap->heap;
250 void ion_lma_heap_destroy(struct ion_heap *heap)
252 struct ion_lma_heap *lma_heap =
253 container_of(heap, struct ion_lma_heap, heap);
254 gen_pool_destroy(lma_heap->pool);