RK3368 GPU version Rogue M 1.28
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / rogue_m / kernel / drivers / staging / imgtec / ion_lma_heap.c
1 /*************************************************************************/ /*!
2 @File           ion_lma_heap.c
3 @Copyright      Copyright (c) Imagination Technologies Ltd. All Rights Reserved
4 @License        Dual MIT/GPLv2
5
6 The contents of this file are subject to the MIT license as set out below.
7
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:
14
15 The above copyright notice and this permission notice shall be included in
16 all copies or substantial portions of the Software.
17
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.
21
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.
29
30 This License is also included in this distribution in the file called
31 "MIT-COPYING".
32
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 */ /**************************************************************************/
41 /* vi: set ts=8: */
42
43 #include "ion_lma_heap.h"
44
45 #include <linux/mm.h>
46 #include <linux/slab.h>
47 #include <linux/genalloc.h>
48 #include <linux/scatterlist.h>
49
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).
53  */
54
55 struct ion_lma_heap {
56         struct ion_heap heap;
57         struct gen_pool *pool;
58         ion_phys_addr_t base;
59 };
60
61 static ion_phys_addr_t ion_lma_allocate(struct ion_heap *heap,
62                                         unsigned long size,
63                                         unsigned long align)
64 {
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);
68
69         if (!offset)
70                 return ION_CARVEOUT_ALLOCATE_FAIL;
71
72         return offset;
73 }
74
75 static void ion_lma_free(struct ion_heap *heap, ion_phys_addr_t addr,
76                          unsigned long size)
77 {
78         struct ion_lma_heap *lma_heap =
79                 container_of(heap, struct ion_lma_heap, heap);
80
81         if (addr == ION_CARVEOUT_ALLOCATE_FAIL)
82                 return;
83
84         gen_pool_free(lma_heap->pool, addr, size);
85 }
86
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)
90 {
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));
94
95         *addr = paddr;
96         *len = buffer->size;
97         return 0;
98 }
99
100 static int ion_lma_heap_allocate(struct ion_heap *heap,
101                                  struct ion_buffer *buffer,
102                                  unsigned long size, unsigned long align,
103                                  unsigned long flags)
104 {
105         struct sg_table *table;
106         ion_phys_addr_t paddr;
107         int ret;
108
109         if (align > PAGE_SIZE)
110                 return -EINVAL;
111
112         table = kzalloc(sizeof(struct sg_table), GFP_KERNEL);
113         if (!table)
114                 return -ENOMEM;
115
116         ret = sg_alloc_table(table, 1, GFP_KERNEL);
117         if (ret)
118                 goto err_free;
119
120         paddr = ion_lma_allocate(heap, size, align);
121         if (paddr == ION_CARVEOUT_ALLOCATE_FAIL) {
122                 ret = -ENOMEM;
123                 goto err_free_table;
124         }
125
126         sg_set_page(table->sgl, pfn_to_page(PFN_DOWN(paddr)), size, 0);
127         buffer->priv_virt = table;
128         return 0;
129
130 err_free_table:
131         sg_free_table(table);
132 err_free:
133         kfree(table);
134         return ret;
135 }
136
137 static void ion_lma_heap_free(struct ion_buffer *buffer)
138 {
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));
143
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.
147          *
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.
151          */
152
153         ion_lma_free(heap, paddr, buffer->size);
154         sg_free_table(table);
155         kfree(table);
156 }
157
158 static struct sg_table *ion_lma_heap_map_dma(struct ion_heap *heap,
159                                              struct ion_buffer *buffer)
160 {
161         return buffer->priv_virt;
162 }
163
164 static void ion_lma_heap_unmap_dma(struct ion_heap *heap,
165                                    struct ion_buffer *buffer)
166 {
167         return;
168 }
169
170 static int ion_lma_heap_map_user(struct ion_heap *mapper,
171                                  struct ion_buffer *buffer,
172                                  struct vm_area_struct *vma)
173 {
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));
177
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));
182 }
183
184 static void *ion_lma_heap_map_kernel(struct ion_heap *heap,
185                                      struct ion_buffer *buffer)
186 {
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));
190
191         return ioremap_wc(paddr, buffer->size);
192 }
193
194 static void ion_lma_heap_unmap_kernel(struct ion_heap *heap,
195                                       struct ion_buffer *buffer)
196 {
197         iounmap(buffer->vaddr);
198 }
199
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,
209 };
210
211 struct ion_heap *ion_lma_heap_create(struct ion_platform_heap *heap_data)
212 {
213         struct ion_lma_heap *lma_heap;
214         size_t size = heap_data->size;
215         struct page *page;
216
217         page = pfn_to_page(PFN_DOWN(heap_data->base));
218
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.
222          *
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.
226          */
227
228         lma_heap = kzalloc(sizeof(struct ion_lma_heap), GFP_KERNEL);
229         if (!lma_heap)
230                 return ERR_PTR(-ENOMEM);
231
232         lma_heap->pool = gen_pool_create(12, -1);
233         if (!lma_heap->pool) {
234                 kfree(lma_heap);
235                 return ERR_PTR(-ENOMEM);
236         }
237
238         lma_heap->base = heap_data->base;
239         gen_pool_add(lma_heap->pool, lma_heap->base, size, -1);
240
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;
246
247         return &lma_heap->heap;
248 }
249
250 void ion_lma_heap_destroy(struct ion_heap *heap)
251 {
252         struct ion_lma_heap *lma_heap =
253                 container_of(heap, struct ion_lma_heap, heap);
254         gen_pool_destroy(lma_heap->pool);
255         kfree(lma_heap);
256         lma_heap = NULL;
257 }