2 * Copyright (C) 2010-2012 ARM Limited. All rights reserved.
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.
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.
11 #include "mali_kernel_common.h"
12 #include "mali_memory.h"
13 #include "mali_memory_block_alloc.h"
15 #include <linux/mutex.h>
16 #define MALI_BLOCK_SIZE (256UL * 1024UL) /* 256 kB, remember to keep the ()s */
19 struct block_info *next;
22 typedef struct block_info block_info;
25 typedef struct block_allocator {
27 block_info *all_blocks;
28 block_info *first_free;
35 static block_allocator *mali_mem_block_gobal_allocator = NULL;
37 MALI_STATIC_INLINE u32 get_phys(block_allocator *info, block_info *block)
39 return info->base + ((block - info->all_blocks) * MALI_BLOCK_SIZE);
42 mali_mem_allocator *mali_mem_block_allocator_create(u32 base_address, u32 cpu_usage_adjust, u32 size)
44 block_allocator *info;
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));
54 if (usable_size == 0) {
55 MALI_DEBUG_PRINT(1, ("Memory block of size %d is unusable\n", size));
59 info = _mali_osk_malloc(sizeof(block_allocator));
61 mutex_init(&info->mutex);
62 info->all_blocks = _mali_osk_malloc(sizeof(block_info) * num_blocks);
63 if (NULL != info->all_blocks) {
65 info->first_free = NULL;
66 info->num_blocks = num_blocks;
67 info->free_blocks = num_blocks;
69 info->base = base_address;
70 info->cpu_usage_adjust = cpu_usage_adjust;
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];
77 return (mali_mem_allocator *)info;
85 void mali_mem_block_allocator_destroy(mali_mem_allocator *allocator)
87 block_allocator *info = (block_allocator*)allocator;
89 info = mali_mem_block_gobal_allocator;
90 if (NULL == info) return;
92 MALI_DEBUG_ASSERT_POINTER(info);
94 _mali_osk_free(info->all_blocks);
98 static void mali_mem_block_mali_map(mali_mem_allocation *descriptor, u32 phys, u32 virt, u32 size)
100 struct mali_page_directory *pagedir = descriptor->session->page_directory;
101 u32 prop = descriptor->mali_mapping.properties;
105 mali_mmu_pagedir_update(pagedir, virt + offset, phys + offset, MALI_MMU_PAGE_SIZE, prop);
107 size -= MALI_MMU_PAGE_SIZE;
108 offset += MALI_MMU_PAGE_SIZE;
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)
114 u32 virt = vma->vm_start + mapping_offset;
115 u32 cpu_phys = mali_phys + cpu_usage_adjust;
120 ret = vm_insert_pfn(vma, virt + offset, __phys_to_pfn(cpu_phys + offset));
123 MALI_DEBUG_PRINT(1, ("Block allocator: Failed to insert pfn into vma\n"));
127 size -= MALI_MMU_PAGE_SIZE;
128 offset += MALI_MMU_PAGE_SIZE;
134 mali_mem_allocation *mali_mem_block_alloc(u32 mali_addr, u32 size, struct vm_area_struct *vma, struct mali_session_data *session)
136 _mali_osk_errcode_t err;
137 mali_mem_allocation *descriptor;
138 block_allocator *info;
140 block_info *last_allocated = NULL;
141 block_allocator_allocation *ret_allocation;
144 size = ALIGN(size, MALI_BLOCK_SIZE);
146 info = mali_mem_block_gobal_allocator;
147 if (NULL == info) return NULL;
150 MALI_DEBUG_ASSERT(0 != left);
152 descriptor = mali_mem_descriptor_create(session, MALI_MEM_BLOCK);
153 if (NULL == descriptor) {
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;
162 if (VM_SHARED == (VM_SHARED & vma->vm_flags)) {
163 descriptor->mali_mapping.properties = MALI_MMU_FLAGS_DEFAULT;
165 /* Cached Mali memory mapping */
166 descriptor->mali_mapping.properties = MALI_MMU_FLAGS_FORCE_GP_READ_ALLOCATE;
167 vma->vm_flags |= VM_SHARED;
170 ret_allocation = &descriptor->block_mem.mem;
172 ret_allocation->mapping_length = 0;
174 _mali_osk_mutex_wait(session->memory_lock);
175 mutex_lock(&info->mutex);
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);
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);
193 while ((left > 0) && (info->first_free)) {
196 u32 current_mapping_size;
198 block = info->first_free;
199 info->first_free = info->first_free->next;
200 block->next = last_allocated;
201 last_allocated = block;
203 phys_addr = get_phys(info, block);
205 if (MALI_BLOCK_SIZE < left) {
206 current_mapping_size = MALI_BLOCK_SIZE;
208 current_mapping_size = left;
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;
222 mutex_unlock(&info->mutex);
223 _mali_osk_mutex_signal(session->memory_lock);
225 mali_mem_mali_map_free(descriptor);
226 mali_mem_descriptor_destroy(descriptor);
231 left -= current_mapping_size;
232 offset += current_mapping_size;
233 ret_allocation->mapping_length += current_mapping_size;
238 mutex_unlock(&info->mutex);
239 _mali_osk_mutex_signal(session->memory_lock);
241 MALI_DEBUG_ASSERT(0 == left);
243 /* Record all the information about this allocation */
244 ret_allocation->last_allocated = last_allocated;
245 ret_allocation->info = info;
250 void mali_mem_block_release(mali_mem_allocation *descriptor)
252 block_allocator *info = descriptor->block_mem.mem.info;
253 block_info *block, *next;
254 block_allocator_allocation *allocation = &descriptor->block_mem.mem;
256 MALI_DEBUG_ASSERT(MALI_MEM_BLOCK == descriptor->type);
258 block = allocation->last_allocated;
260 MALI_DEBUG_ASSERT_POINTER(block);
263 mali_mem_mali_map_free(descriptor);
265 mutex_lock(&info->mutex);
268 MALI_DEBUG_ASSERT(!((block < info->all_blocks) || (block > (info->all_blocks + info->num_blocks))));
272 /* relink into free-list */
273 block->next = info->first_free;
274 info->first_free = block;
276 /* advance the loop */
282 mutex_unlock(&info->mutex);
285 u32 mali_mem_block_allocator_stat(void)
287 block_allocator *info = (block_allocator *)mali_mem_block_gobal_allocator;
289 if (NULL == info) return 0;
291 MALI_DEBUG_ASSERT_POINTER(info);
293 return (info->num_blocks - info->free_blocks) * MALI_BLOCK_SIZE;
296 _mali_osk_errcode_t mali_memory_core_resource_dedicated_memory(u32 start, u32 size)
298 mali_mem_allocator *allocator;
300 /* Do the low level linux operation first */
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;
308 /* Create generic block allocator object to handle it */
309 allocator = mali_mem_block_allocator_create(start, 0 /* cpu_usage_adjust */, size);
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);
317 mali_mem_block_gobal_allocator = (block_allocator*)allocator;
319 return _MALI_OSK_ERR_OK;