tizen 2.4 release
[kernel/linux-3.0.git] / drivers / gpu / arm / mali400 / r4p0_rel0 / linux / mali_memory_block_alloc.c
1 /*
2  * Copyright (C) 2010-2012 ARM Limited. All rights reserved.
3  * 
4  * This program is free software and is provided to you under the terms of the GNU General Public License version 2
5  * as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
6  * 
7  * A copy of the licence is included with the program, and can also be obtained from Free Software
8  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
9  */
10
11 #include "mali_kernel_common.h"
12 #include "mali_memory.h"
13 #include "mali_memory_block_alloc.h"
14 #include "mali_osk.h"
15 #include <linux/mutex.h>
16 #define MALI_BLOCK_SIZE (256UL * 1024UL)  /* 256 kB, remember to keep the ()s */
17
18 struct block_info {
19         struct block_info *next;
20 };
21
22 typedef struct block_info block_info;
23
24
25 typedef struct block_allocator {
26         struct mutex mutex;
27         block_info *all_blocks;
28         block_info *first_free;
29         u32 base;
30         u32 cpu_usage_adjust;
31         u32 num_blocks;
32         u32 free_blocks;
33 } block_allocator;
34
35 static block_allocator *mali_mem_block_gobal_allocator = NULL;
36
37 MALI_STATIC_INLINE u32 get_phys(block_allocator *info, block_info *block)
38 {
39         return info->base + ((block - info->all_blocks) * MALI_BLOCK_SIZE);
40 }
41
42 mali_mem_allocator *mali_mem_block_allocator_create(u32 base_address, u32 cpu_usage_adjust, u32 size)
43 {
44         block_allocator *info;
45         u32 usable_size;
46         u32 num_blocks;
47
48         usable_size = size & ~(MALI_BLOCK_SIZE - 1);
49         MALI_DEBUG_PRINT(3, ("Mali block allocator create for region starting at 0x%08X length 0x%08X\n", base_address, size));
50         MALI_DEBUG_PRINT(4, ("%d usable bytes\n", usable_size));
51         num_blocks = usable_size / MALI_BLOCK_SIZE;
52         MALI_DEBUG_PRINT(4, ("which becomes %d blocks\n", num_blocks));
53
54         if (usable_size == 0) {
55                 MALI_DEBUG_PRINT(1, ("Memory block of size %d is unusable\n", size));
56                 return NULL;
57         }
58
59         info = _mali_osk_malloc(sizeof(block_allocator));
60         if (NULL != info) {
61                 mutex_init(&info->mutex);
62                 info->all_blocks = _mali_osk_malloc(sizeof(block_info) * num_blocks);
63                 if (NULL != info->all_blocks) {
64                         u32 i;
65                         info->first_free = NULL;
66                         info->num_blocks = num_blocks;
67                         info->free_blocks = num_blocks;
68
69                         info->base = base_address;
70                         info->cpu_usage_adjust = cpu_usage_adjust;
71
72                         for ( i = 0; i < num_blocks; i++) {
73                                 info->all_blocks[i].next = info->first_free;
74                                 info->first_free = &info->all_blocks[i];
75                         }
76
77                         return (mali_mem_allocator *)info;
78                 }
79                 _mali_osk_free(info);
80         }
81
82         return NULL;
83 }
84
85 void mali_mem_block_allocator_destroy(mali_mem_allocator *allocator)
86 {
87         block_allocator *info = (block_allocator*)allocator;
88
89         info = mali_mem_block_gobal_allocator;
90         if (NULL == info) return;
91
92         MALI_DEBUG_ASSERT_POINTER(info);
93
94         _mali_osk_free(info->all_blocks);
95         _mali_osk_free(info);
96 }
97
98 static void mali_mem_block_mali_map(mali_mem_allocation *descriptor, u32 phys, u32 virt, u32 size)
99 {
100         struct mali_page_directory *pagedir = descriptor->session->page_directory;
101         u32 prop = descriptor->mali_mapping.properties;
102         u32 offset = 0;
103
104         while (size) {
105                 mali_mmu_pagedir_update(pagedir, virt + offset, phys + offset, MALI_MMU_PAGE_SIZE, prop);
106
107                 size -= MALI_MMU_PAGE_SIZE;
108                 offset += MALI_MMU_PAGE_SIZE;
109         }
110 }
111
112 static int mali_mem_block_cpu_map(mali_mem_allocation *descriptor, struct vm_area_struct *vma, u32 mali_phys, u32 mapping_offset, u32 size, u32 cpu_usage_adjust)
113 {
114         u32 virt = vma->vm_start + mapping_offset;
115         u32 cpu_phys = mali_phys + cpu_usage_adjust;
116         u32 offset = 0;
117         int ret;
118
119         while (size) {
120                 ret = vm_insert_pfn(vma, virt + offset, __phys_to_pfn(cpu_phys + offset));
121
122                 if (unlikely(ret)) {
123                         MALI_DEBUG_PRINT(1, ("Block allocator: Failed to insert pfn into vma\n"));
124                         return 1;
125                 }
126
127                 size -= MALI_MMU_PAGE_SIZE;
128                 offset += MALI_MMU_PAGE_SIZE;
129         }
130
131         return 0;
132 }
133
134 mali_mem_allocation *mali_mem_block_alloc(u32 mali_addr, u32 size, struct vm_area_struct *vma, struct mali_session_data *session)
135 {
136         _mali_osk_errcode_t err;
137         mali_mem_allocation *descriptor;
138         block_allocator *info;
139         u32 left;
140         block_info *last_allocated = NULL;
141         block_allocator_allocation *ret_allocation;
142         u32 offset = 0;
143
144         size = ALIGN(size, MALI_BLOCK_SIZE);
145
146         info = mali_mem_block_gobal_allocator;
147         if (NULL == info) return NULL;
148
149         left = size;
150         MALI_DEBUG_ASSERT(0 != left);
151
152         descriptor = mali_mem_descriptor_create(session, MALI_MEM_BLOCK);
153         if (NULL == descriptor) {
154                 return NULL;
155         }
156
157         descriptor->mali_mapping.addr = mali_addr;
158         descriptor->size = size;
159         descriptor->cpu_mapping.addr = (void __user*)vma->vm_start;
160         descriptor->cpu_mapping.ref = 1;
161
162         if (VM_SHARED == (VM_SHARED & vma->vm_flags)) {
163                 descriptor->mali_mapping.properties = MALI_MMU_FLAGS_DEFAULT;
164         } else {
165                 /* Cached Mali memory mapping */
166                 descriptor->mali_mapping.properties = MALI_MMU_FLAGS_FORCE_GP_READ_ALLOCATE;
167                 vma->vm_flags |= VM_SHARED;
168         }
169
170         ret_allocation = &descriptor->block_mem.mem;
171
172         ret_allocation->mapping_length = 0;
173
174         _mali_osk_mutex_wait(session->memory_lock);
175         mutex_lock(&info->mutex);
176
177         if (left > (info->free_blocks * MALI_BLOCK_SIZE)) {
178                 MALI_DEBUG_PRINT(2, ("Mali block allocator: not enough free blocks to service allocation (%u)\n", left));
179                 mutex_unlock(&info->mutex);
180                 _mali_osk_mutex_signal(session->memory_lock);
181                 mali_mem_descriptor_destroy(descriptor);
182                 return NULL;
183         }
184
185         err = mali_mem_mali_map_prepare(descriptor);
186         if (_MALI_OSK_ERR_OK != err) {
187                 mutex_unlock(&info->mutex);
188                 _mali_osk_mutex_signal(session->memory_lock);
189                 mali_mem_descriptor_destroy(descriptor);
190                 return NULL;
191         }
192
193         while ((left > 0) && (info->first_free)) {
194                 block_info *block;
195                 u32 phys_addr;
196                 u32 current_mapping_size;
197
198                 block = info->first_free;
199                 info->first_free = info->first_free->next;
200                 block->next = last_allocated;
201                 last_allocated = block;
202
203                 phys_addr = get_phys(info, block);
204
205                 if (MALI_BLOCK_SIZE < left) {
206                         current_mapping_size = MALI_BLOCK_SIZE;
207                 } else {
208                         current_mapping_size = left;
209                 }
210
211                 mali_mem_block_mali_map(descriptor, phys_addr, mali_addr + offset, current_mapping_size);
212                 if (mali_mem_block_cpu_map(descriptor, vma, phys_addr, offset, current_mapping_size, info->cpu_usage_adjust)) {
213                         /* release all memory back to the pool */
214                         while (last_allocated) {
215                                 /* This relinks every block we've just allocated back into the free-list */
216                                 block = last_allocated->next;
217                                 last_allocated->next = info->first_free;
218                                 info->first_free = last_allocated;
219                                 last_allocated = block;
220                         }
221
222                         mutex_unlock(&info->mutex);
223                         _mali_osk_mutex_signal(session->memory_lock);
224
225                         mali_mem_mali_map_free(descriptor);
226                         mali_mem_descriptor_destroy(descriptor);
227
228                         return NULL;
229                 }
230
231                 left -= current_mapping_size;
232                 offset += current_mapping_size;
233                 ret_allocation->mapping_length += current_mapping_size;
234
235                 --info->free_blocks;
236         }
237
238         mutex_unlock(&info->mutex);
239         _mali_osk_mutex_signal(session->memory_lock);
240
241         MALI_DEBUG_ASSERT(0 == left);
242
243         /* Record all the information about this allocation */
244         ret_allocation->last_allocated = last_allocated;
245         ret_allocation->info = info;
246
247         return descriptor;
248 }
249
250 void mali_mem_block_release(mali_mem_allocation *descriptor)
251 {
252         block_allocator *info = descriptor->block_mem.mem.info;
253         block_info *block, *next;
254         block_allocator_allocation *allocation = &descriptor->block_mem.mem;
255
256         MALI_DEBUG_ASSERT(MALI_MEM_BLOCK == descriptor->type);
257
258         block = allocation->last_allocated;
259
260         MALI_DEBUG_ASSERT_POINTER(block);
261
262         /* unmap */
263         mali_mem_mali_map_free(descriptor);
264
265         mutex_lock(&info->mutex);
266
267         while (block) {
268                 MALI_DEBUG_ASSERT(!((block < info->all_blocks) || (block > (info->all_blocks + info->num_blocks))));
269
270                 next = block->next;
271
272                 /* relink into free-list */
273                 block->next = info->first_free;
274                 info->first_free = block;
275
276                 /* advance the loop */
277                 block = next;
278
279                 ++info->free_blocks;
280         }
281
282         mutex_unlock(&info->mutex);
283 }
284
285 u32 mali_mem_block_allocator_stat(void)
286 {
287         block_allocator *info = (block_allocator *)mali_mem_block_gobal_allocator;
288
289         if (NULL == info) return 0;
290
291         MALI_DEBUG_ASSERT_POINTER(info);
292
293         return (info->num_blocks - info->free_blocks) * MALI_BLOCK_SIZE;
294 }
295
296 _mali_osk_errcode_t mali_memory_core_resource_dedicated_memory(u32 start, u32 size)
297 {
298         mali_mem_allocator *allocator;
299
300         /* Do the low level linux operation first */
301
302         /* Request ownership of the memory */
303         if (_MALI_OSK_ERR_OK != _mali_osk_mem_reqregion(start, size, "Dedicated Mali GPU memory")) {
304                 MALI_DEBUG_PRINT(1, ("Failed to request memory region for frame buffer (0x%08X - 0x%08X)\n", start, start + size - 1));
305                 return _MALI_OSK_ERR_FAULT;
306         }
307
308         /* Create generic block allocator object to handle it */
309         allocator = mali_mem_block_allocator_create(start, 0 /* cpu_usage_adjust */, size);
310
311         if (NULL == allocator) {
312                 MALI_DEBUG_PRINT(1, ("Memory bank registration failed\n"));
313                 _mali_osk_mem_unreqregion(start, size);
314                 MALI_ERROR(_MALI_OSK_ERR_FAULT);
315         }
316
317         mali_mem_block_gobal_allocator = (block_allocator*)allocator;
318
319         return _MALI_OSK_ERR_OK;
320 }