2 * Copyright (C) 2013-2014 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 <linux/list.h>
13 #include <linux/mm_types.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/slab.h>
17 #include <linux/version.h>
18 #include <linux/platform_device.h>
21 #include "mali_osk_mali.h"
22 #include "mali_kernel_linux.h"
23 #include "mali_scheduler.h"
24 #include "mali_executor.h"
25 #include "mali_kernel_descriptor_mapping.h"
27 #include "mali_memory.h"
28 #include "mali_memory_dma_buf.h"
29 #include "mali_memory_os_alloc.h"
30 #include "mali_memory_block_alloc.h"
32 extern unsigned int mali_dedicated_mem_size;
33 extern unsigned int mali_shared_mem_size;
35 /* session->memory_lock must be held when calling this function */
36 static void mali_mem_release(mali_mem_allocation *descriptor)
38 MALI_DEBUG_ASSERT_POINTER(descriptor);
39 MALI_DEBUG_ASSERT_LOCK_HELD(descriptor->session->memory_lock);
41 MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
43 switch (descriptor->type) {
45 mali_mem_os_release(descriptor);
47 case MALI_MEM_DMA_BUF:
48 #if defined(CONFIG_DMA_SHARED_BUFFER)
49 mali_mem_dma_buf_release(descriptor);
53 #if defined(CONFIG_MALI400_UMP)
54 mali_mem_ump_release(descriptor);
57 case MALI_MEM_EXTERNAL:
58 mali_mem_external_release(descriptor);
61 mali_mem_block_release(descriptor);
64 MALI_DEBUG_PRINT(1, ("mem type %d is not in the mali_mem_type enum.\n", descriptor->type));
69 static void mali_mem_vma_open(struct vm_area_struct *vma)
71 mali_mem_allocation *descriptor = (mali_mem_allocation *)vma->vm_private_data;
72 MALI_DEBUG_PRINT(4, ("Open called on vma %p\n", vma));
74 descriptor->cpu_mapping.ref++;
79 static void mali_mem_vma_close(struct vm_area_struct *vma)
81 mali_mem_allocation *descriptor;
82 struct mali_session_data *session;
83 mali_mem_virt_cpu_mapping *mapping;
85 MALI_DEBUG_PRINT(3, ("Close called on vma %p\n", vma));
87 descriptor = (mali_mem_allocation *)vma->vm_private_data;
90 MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
92 mapping = &descriptor->cpu_mapping;
93 BUG_ON(0 == mapping->ref);
96 if (0 != mapping->ref) {
97 MALI_DEBUG_PRINT(3, ("Ignoring this close, %d references still exists\n", mapping->ref));
101 session = descriptor->session;
103 mali_descriptor_mapping_free(session->descriptor_mapping, descriptor->id);
105 _mali_osk_mutex_wait(session->memory_lock);
106 mali_mem_release(descriptor);
107 _mali_osk_mutex_signal(session->memory_lock);
109 mali_mem_descriptor_destroy(descriptor);
112 static int mali_kernel_memory_cpu_page_fault_handler(struct vm_area_struct *vma, struct vm_fault *vmf)
114 void __user *address;
115 mali_mem_allocation *descriptor;
117 address = vmf->virtual_address;
118 descriptor = (mali_mem_allocation *)vma->vm_private_data;
120 MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
123 * We always fail the call since all memory is pre-faulted when assigned to the process.
124 * Only the Mali cores can use page faults to extend buffers.
127 MALI_DEBUG_PRINT(1, ("Page-fault in Mali memory region caused by the CPU.\n"));
128 MALI_DEBUG_PRINT(1, ("Tried to access %p (process local virtual address) which is not currently mapped to any Mali memory.\n", (void *)address));
130 MALI_IGNORE(address);
131 MALI_IGNORE(descriptor);
133 return VM_FAULT_SIGBUS;
136 static struct vm_operations_struct mali_kernel_vm_ops = {
137 .open = mali_mem_vma_open,
138 .close = mali_mem_vma_close,
139 .fault = mali_kernel_memory_cpu_page_fault_handler
142 /** @note munmap handler is done by vma close handler */
143 int mali_mmap(struct file *filp, struct vm_area_struct *vma)
145 struct mali_session_data *session;
146 mali_mem_allocation *descriptor;
147 u32 size = vma->vm_end - vma->vm_start;
148 u32 mali_addr = vma->vm_pgoff << PAGE_SHIFT;
150 session = (struct mali_session_data *)filp->private_data;
151 if (NULL == session) {
152 MALI_PRINT_ERROR(("mmap called without any session data available\n"));
156 MALI_DEBUG_PRINT(4, ("MMap() handler: start=0x%08X, phys=0x%08X, size=0x%08X vma->flags 0x%08x\n",
157 (unsigned int)vma->vm_start, (unsigned int)(vma->vm_pgoff << PAGE_SHIFT),
158 (unsigned int)(vma->vm_end - vma->vm_start), vma->vm_flags));
160 /* Set some bits which indicate that, the memory is IO memory, meaning
161 * that no paging is to be performed and the memory should not be
162 * included in crash dumps. And that the memory is reserved, meaning
163 * that it's present and can never be paged out (see also previous
166 vma->vm_flags |= VM_IO;
167 vma->vm_flags |= VM_DONTCOPY;
168 vma->vm_flags |= VM_PFNMAP;
169 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
170 vma->vm_flags |= VM_RESERVED;
172 vma->vm_flags |= VM_DONTDUMP;
173 vma->vm_flags |= VM_DONTEXPAND;
176 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
177 vma->vm_ops = &mali_kernel_vm_ops; /* Operations used on any memory system */
179 descriptor = mali_mem_block_alloc(mali_addr, size, vma, session);
180 if (NULL == descriptor) {
181 descriptor = mali_mem_os_alloc(mali_addr, size, vma, session);
182 if (NULL == descriptor) {
183 MALI_DEBUG_PRINT(3, ("MMAP failed\n"));
188 MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
190 vma->vm_private_data = (void *)descriptor;
192 /* Put on descriptor map */
193 if (_MALI_OSK_ERR_OK != mali_descriptor_mapping_allocate_mapping(session->descriptor_mapping, descriptor, &descriptor->id)) {
194 _mali_osk_mutex_wait(session->memory_lock);
195 if (MALI_MEM_OS == descriptor->type) {
196 mali_mem_os_release(descriptor);
197 } else if (MALI_MEM_BLOCK == descriptor->type) {
198 mali_mem_block_release(descriptor);
200 _mali_osk_mutex_signal(session->memory_lock);
208 /* Prepare memory descriptor */
209 mali_mem_allocation *mali_mem_descriptor_create(struct mali_session_data *session, mali_mem_type type)
211 mali_mem_allocation *descriptor;
213 descriptor = (mali_mem_allocation *)kzalloc(sizeof(mali_mem_allocation), GFP_KERNEL);
214 if (NULL == descriptor) {
215 MALI_DEBUG_PRINT(3, ("mali_ukk_mem_mmap: descriptor was NULL\n"));
219 MALI_DEBUG_CODE(descriptor->magic = MALI_MEM_ALLOCATION_VALID_MAGIC);
221 descriptor->flags = 0;
222 descriptor->type = type;
223 descriptor->session = session;
228 void mali_mem_descriptor_destroy(mali_mem_allocation *descriptor)
230 MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
231 MALI_DEBUG_CODE(descriptor->magic = MALI_MEM_ALLOCATION_FREED_MAGIC);
236 _mali_osk_errcode_t mali_mem_mali_map_prepare(mali_mem_allocation *descriptor)
238 u32 size = descriptor->size;
239 struct mali_session_data *session = descriptor->session;
241 MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
243 /* Map dma-buf into this session's page tables */
245 if (descriptor->flags & MALI_MEM_FLAG_MALI_GUARD_PAGE) {
246 size += MALI_MMU_PAGE_SIZE;
249 return mali_mmu_pagedir_map(session->page_directory, descriptor->mali_mapping.addr, size);
252 void mali_mem_mali_map_free(mali_mem_allocation *descriptor)
254 u32 size = descriptor->size;
255 struct mali_session_data *session = descriptor->session;
257 MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
259 if (descriptor->flags & MALI_MEM_FLAG_MALI_GUARD_PAGE) {
260 size += MALI_MMU_PAGE_SIZE;
263 /* Umap and flush L2 */
264 mali_mmu_pagedir_unmap(session->page_directory, descriptor->mali_mapping.addr, descriptor->size);
266 mali_executor_zap_all_active(session);
269 u32 _mali_ukk_report_memory_usage(void)
273 sum += mali_mem_block_allocator_stat();
274 sum += mali_mem_os_stat();
279 u32 _mali_ukk_report_total_memory_size(void)
281 return mali_dedicated_mem_size + mali_shared_mem_size;
286 * Per-session memory descriptor mapping table sizes
288 #define MALI_MEM_DESCRIPTORS_INIT 64
289 #define MALI_MEM_DESCRIPTORS_MAX 65536
291 _mali_osk_errcode_t mali_memory_session_begin(struct mali_session_data *session_data)
293 MALI_DEBUG_PRINT(5, ("Memory session begin\n"));
295 /* Create descriptor mapping table */
296 session_data->descriptor_mapping = mali_descriptor_mapping_create(MALI_MEM_DESCRIPTORS_INIT, MALI_MEM_DESCRIPTORS_MAX);
298 if (NULL == session_data->descriptor_mapping) {
299 MALI_ERROR(_MALI_OSK_ERR_NOMEM);
302 session_data->memory_lock = _mali_osk_mutex_init(_MALI_OSK_LOCKFLAG_ORDERED,
303 _MALI_OSK_LOCK_ORDER_MEM_SESSION);
305 if (NULL == session_data->memory_lock) {
306 mali_descriptor_mapping_destroy(session_data->descriptor_mapping);
307 _mali_osk_free(session_data);
308 MALI_ERROR(_MALI_OSK_ERR_FAULT);
311 MALI_DEBUG_PRINT(5, ("MMU session begin: success\n"));
315 /** @brief Callback function that releases memory
317 * session->memory_lock must be held when calling this function.
319 static void descriptor_table_cleanup_callback(int descriptor_id, void *map_target)
321 mali_mem_allocation *descriptor;
323 descriptor = (mali_mem_allocation *)map_target;
325 MALI_DEBUG_ASSERT_LOCK_HELD(descriptor->session->memory_lock);
327 MALI_DEBUG_PRINT(3, ("Cleanup of descriptor %d mapping to 0x%x in descriptor table\n", descriptor_id, map_target));
328 MALI_DEBUG_ASSERT(descriptor);
330 mali_mem_release(descriptor);
331 mali_mem_descriptor_destroy(descriptor);
334 void mali_memory_session_end(struct mali_session_data *session)
336 MALI_DEBUG_PRINT(3, ("MMU session end\n"));
338 if (NULL == session) {
339 MALI_DEBUG_PRINT(1, ("No session data found during session end\n"));
343 /* Lock the session so we can modify the memory list */
344 _mali_osk_mutex_wait(session->memory_lock);
346 /* Free all allocations still in the descriptor map, and terminate the map */
347 if (NULL != session->descriptor_mapping) {
348 mali_descriptor_mapping_call_for_each(session->descriptor_mapping, descriptor_table_cleanup_callback);
349 mali_descriptor_mapping_destroy(session->descriptor_mapping);
350 session->descriptor_mapping = NULL;
353 _mali_osk_mutex_signal(session->memory_lock);
356 _mali_osk_mutex_term(session->memory_lock);
361 _mali_osk_errcode_t mali_memory_initialize(void)
363 return mali_mem_os_init();
366 void mali_memory_terminate(void)
369 mali_mem_block_allocator_destroy(NULL);