2 * Copyright (C) 2013-2015 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>
19 #include <linux/idr.h>
22 #include "mali_executor.h"
24 #include "mali_memory.h"
25 #include "mali_memory_os_alloc.h"
26 #include "mali_memory_block_alloc.h"
27 #include "mali_memory_util.h"
28 #include "mali_memory_virtual.h"
29 #include "mali_memory_manager.h"
30 #include "mali_memory_cow.h"
31 #include "mali_memory_swap_alloc.h"
32 #include "mali_memory_defer_bind.h"
33 #if defined(CONFIG_DMA_SHARED_BUFFER)
34 #include "mali_memory_secure.h"
37 extern unsigned int mali_dedicated_mem_size;
38 extern unsigned int mali_shared_mem_size;
40 #define MALI_VM_NUM_FAULT_PREFETCH (0x8)
42 static void mali_mem_vma_open(struct vm_area_struct *vma)
44 mali_mem_allocation *alloc = (mali_mem_allocation *)vma->vm_private_data;
45 MALI_DEBUG_PRINT(4, ("Open called on vma %p\n", vma));
47 /* If need to share the allocation, add ref_count here */
48 mali_allocation_ref(alloc);
51 static void mali_mem_vma_close(struct vm_area_struct *vma)
53 /* If need to share the allocation, unref ref_count here */
54 mali_mem_allocation *alloc = (mali_mem_allocation *)vma->vm_private_data;
56 mali_allocation_unref(&alloc);
57 vma->vm_private_data = NULL;
60 static int mali_mem_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
62 mali_mem_allocation *alloc = (mali_mem_allocation *)vma->vm_private_data;
63 mali_mem_backend *mem_bkend = NULL;
65 int prefetch_num = MALI_VM_NUM_FAULT_PREFETCH;
67 unsigned long address = (unsigned long)vmf->virtual_address;
68 MALI_DEBUG_ASSERT(alloc->backend_handle);
69 MALI_DEBUG_ASSERT((unsigned long)alloc->cpu_mapping.addr <= address);
71 /* Get backend memory & Map on CPU */
72 mutex_lock(&mali_idr_mutex);
73 if (!(mem_bkend = idr_find(&mali_backend_idr, alloc->backend_handle))) {
74 MALI_DEBUG_PRINT(1, ("Can't find memory backend in mmap!\n"));
75 mutex_unlock(&mali_idr_mutex);
76 return VM_FAULT_SIGBUS;
78 mutex_unlock(&mali_idr_mutex);
79 MALI_DEBUG_ASSERT(mem_bkend->type == alloc->type);
81 if ((mem_bkend->type == MALI_MEM_COW && (MALI_MEM_BACKEND_FLAG_SWAP_COWED !=
82 (mem_bkend->flags & MALI_MEM_BACKEND_FLAG_SWAP_COWED))) &&
83 (mem_bkend->flags & MALI_MEM_BACKEND_FLAG_COW_CPU_NO_WRITE)) {
84 /*check if use page fault to do COW*/
85 MALI_DEBUG_PRINT(4, ("mali_vma_fault: do cow allocate on demand!, address=0x%x\n", address));
86 mutex_lock(&mem_bkend->mutex);
87 ret = mali_mem_cow_allocate_on_demand(mem_bkend,
88 (address - vma->vm_start) / PAGE_SIZE);
89 mutex_unlock(&mem_bkend->mutex);
91 if (ret != _MALI_OSK_ERR_OK) {
96 /* handle COW modified range cpu mapping
97 we zap the mapping in cow_modify_range, it will trigger page fault
98 when CPU access it, so here we map it to CPU*/
99 mutex_lock(&mem_bkend->mutex);
100 ret = mali_mem_cow_cpu_map_pages_locked(mem_bkend, vma, address, prefetch_num);
101 mutex_unlock(&mem_bkend->mutex);
103 if (unlikely(ret != _MALI_OSK_ERR_OK)) {
104 return VM_FAULT_SIGBUS;
106 } else if ((mem_bkend->type == MALI_MEM_SWAP) ||
107 (mem_bkend->type == MALI_MEM_COW && (mem_bkend->flags & MALI_MEM_BACKEND_FLAG_SWAP_COWED))) {
108 u32 offset_in_bkend = (address - vma->vm_start) / PAGE_SIZE;
109 int ret = _MALI_OSK_ERR_OK;
111 mutex_lock(&mem_bkend->mutex);
112 if (mem_bkend->flags & MALI_MEM_BACKEND_FLAG_COW_CPU_NO_WRITE) {
113 ret = mali_mem_swap_cow_page_on_demand(mem_bkend, offset_in_bkend, &vmf->page);
115 ret = mali_mem_swap_allocate_page_on_demand(mem_bkend, offset_in_bkend, &vmf->page);
117 mutex_unlock(&mem_bkend->mutex);
119 if (ret != _MALI_OSK_ERR_OK) {
120 MALI_DEBUG_PRINT(2, ("Mali swap memory page fault process failed, address=0x%x\n", address));
123 return VM_FAULT_LOCKED;
126 MALI_PRINT_ERROR(("Mali vma fault! It never happen, indicating some logic errors in caller.\n"));
127 /*NOT support yet or OOM*/
130 return VM_FAULT_NOPAGE;
133 static struct vm_operations_struct mali_kernel_vm_ops = {
134 .open = mali_mem_vma_open,
135 .close = mali_mem_vma_close,
136 .fault = mali_mem_vma_fault,
140 /** @ map mali allocation to CPU address
142 * Supported backend types:
144 * -- need to add COW?
145 *Not supported backend types:
146 * -_MALI_MEMORY_BIND_BACKEND_UMP
147 * -_MALI_MEMORY_BIND_BACKEND_DMA_BUF
148 * -_MALI_MEMORY_BIND_BACKEND_EXTERNAL_MEMORY
151 int mali_mmap(struct file *filp, struct vm_area_struct *vma)
153 struct mali_session_data *session;
154 mali_mem_allocation *mali_alloc = NULL;
155 u32 mali_addr = vma->vm_pgoff << PAGE_SHIFT;
156 struct mali_vma_node *mali_vma_node = NULL;
157 mali_mem_backend *mem_bkend = NULL;
160 session = (struct mali_session_data *)filp->private_data;
161 if (NULL == session) {
162 MALI_PRINT_ERROR(("mmap called without any session data available\n"));
166 MALI_DEBUG_PRINT(4, ("MMap() handler: start=0x%08X, phys=0x%08X, size=0x%08X vma->flags 0x%08x\n",
167 (unsigned int)vma->vm_start, (unsigned int)(vma->vm_pgoff << PAGE_SHIFT),
168 (unsigned int)(vma->vm_end - vma->vm_start), vma->vm_flags));
170 /* Operations used on any memory system */
171 /* do not need to anything in vm open/close now */
173 /* find mali allocation structure by vaddress*/
174 mali_vma_node = mali_vma_offset_search(&session->allocation_mgr, mali_addr, 0);
175 if (likely(mali_vma_node)) {
176 mali_alloc = container_of(mali_vma_node, struct mali_mem_allocation, mali_vma_node);
177 MALI_DEBUG_ASSERT(mali_addr == mali_vma_node->vm_node.start);
178 if (unlikely(mali_addr != mali_vma_node->vm_node.start)) {
179 /* only allow to use start address for mmap */
180 MALI_DEBUG_PRINT(1, ("mali_addr != mali_vma_node->vm_node.start\n"));
184 MALI_DEBUG_ASSERT(NULL == mali_vma_node);
188 mali_alloc->cpu_mapping.addr = (void __user *)vma->vm_start;
190 if (mali_alloc->flags & _MALI_MEMORY_ALLOCATE_DEFER_BIND) {
191 MALI_DEBUG_PRINT(1, ("ERROR : trying to access varying memory by CPU!\n"));
195 /* Get backend memory & Map on CPU */
196 mutex_lock(&mali_idr_mutex);
197 if (!(mem_bkend = idr_find(&mali_backend_idr, mali_alloc->backend_handle))) {
198 MALI_DEBUG_PRINT(1, ("Can't find memory backend in mmap!\n"));
199 mutex_unlock(&mali_idr_mutex);
202 mutex_unlock(&mali_idr_mutex);
204 if (!(MALI_MEM_SWAP == mali_alloc->type ||
205 (MALI_MEM_COW == mali_alloc->type && (mem_bkend->flags & MALI_MEM_BACKEND_FLAG_SWAP_COWED)))) {
206 /* Set some bits which indicate that, the memory is IO memory, meaning
207 * that no paging is to be performed and the memory should not be
208 * included in crash dumps. And that the memory is reserved, meaning
209 * that it's present and can never be paged out (see also previous
212 vma->vm_flags |= VM_IO;
213 vma->vm_flags |= VM_DONTCOPY;
214 vma->vm_flags |= VM_PFNMAP;
215 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
216 vma->vm_flags |= VM_RESERVED;
218 vma->vm_flags |= VM_DONTDUMP;
219 vma->vm_flags |= VM_DONTEXPAND;
221 } else if (MALI_MEM_SWAP == mali_alloc->type) {
222 vma->vm_pgoff = mem_bkend->start_idx;
225 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
226 vma->vm_ops = &mali_kernel_vm_ops;
228 mali_alloc->cpu_mapping.addr = (void __user *)vma->vm_start;
230 /* If it's a copy-on-write mapping, map to read only */
231 if (!(vma->vm_flags & VM_WRITE)) {
232 MALI_DEBUG_PRINT(4, ("mmap allocation with read only !\n"));
233 /* add VM_WRITE for do_page_fault will check this when a write fault */
234 vma->vm_flags |= VM_WRITE | VM_READ;
235 vma->vm_page_prot = PAGE_READONLY;
236 vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
237 mem_bkend->flags |= MALI_MEM_BACKEND_FLAG_COW_CPU_NO_WRITE;
241 if (mem_bkend->type == MALI_MEM_OS) {
242 ret = mali_mem_os_cpu_map(mem_bkend, vma);
243 } else if (mem_bkend->type == MALI_MEM_COW &&
244 (MALI_MEM_BACKEND_FLAG_SWAP_COWED != (mem_bkend->flags & MALI_MEM_BACKEND_FLAG_SWAP_COWED))) {
245 ret = mali_mem_cow_cpu_map(mem_bkend, vma);
246 } else if (mem_bkend->type == MALI_MEM_BLOCK) {
247 ret = mali_mem_block_cpu_map(mem_bkend, vma);
248 } else if ((mem_bkend->type == MALI_MEM_SWAP) || (mem_bkend->type == MALI_MEM_COW &&
249 (MALI_MEM_BACKEND_FLAG_SWAP_COWED == (mem_bkend->flags & MALI_MEM_BACKEND_FLAG_SWAP_COWED)))) {
250 /*For swappable memory, CPU page table will be created by page fault handler. */
252 } else if (mem_bkend->type == MALI_MEM_SECURE) {
253 #if defined(CONFIG_DMA_SHARED_BUFFER)
254 ret = mali_mem_secure_cpu_map(mem_bkend, vma);
256 MALI_DEBUG_PRINT(1, ("DMA not supported for mali secure memory\n"));
261 MALI_DEBUG_PRINT_ERROR(("Invalid type of backend memory! \n"));
266 MALI_DEBUG_PRINT(1, ("ret != 0\n"));
270 MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == mali_alloc->magic);
272 vma->vm_private_data = (void *)mali_alloc;
273 mali_alloc->cpu_mapping.vma = vma;
275 mali_allocation_ref(mali_alloc);
280 _mali_osk_errcode_t mali_mem_mali_map_prepare(mali_mem_allocation *descriptor)
282 u32 size = descriptor->psize;
283 struct mali_session_data *session = descriptor->session;
285 MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
287 /* Map dma-buf into this session's page tables */
289 if (descriptor->flags & MALI_MEM_FLAG_MALI_GUARD_PAGE) {
290 size += MALI_MMU_PAGE_SIZE;
293 return mali_mmu_pagedir_map(session->page_directory, descriptor->mali_vma_node.vm_node.start, size);
296 _mali_osk_errcode_t mali_mem_mali_map_resize(mali_mem_allocation *descriptor, u32 new_size)
298 u32 old_size = descriptor->psize;
299 struct mali_session_data *session = descriptor->session;
301 MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
303 if (descriptor->flags & MALI_MEM_FLAG_MALI_GUARD_PAGE) {
304 new_size += MALI_MMU_PAGE_SIZE;
307 if (new_size > old_size) {
308 MALI_DEBUG_ASSERT(new_size <= descriptor->mali_vma_node.vm_node.size);
309 return mali_mmu_pagedir_map(session->page_directory, descriptor->mali_vma_node.vm_node.start + old_size, new_size - old_size);
311 return _MALI_OSK_ERR_OK;
314 void mali_mem_mali_map_free(struct mali_session_data *session, u32 size, mali_address_t vaddr, u32 flags)
316 if (flags & MALI_MEM_FLAG_MALI_GUARD_PAGE) {
317 size += MALI_MMU_PAGE_SIZE;
320 /* Umap and flush L2 */
321 mali_mmu_pagedir_unmap(session->page_directory, vaddr, size);
322 mali_executor_zap_all_active(session);
325 u32 _mali_ukk_report_memory_usage(void)
329 if (MALI_TRUE == mali_memory_have_dedicated_memory()) {
330 sum += mali_mem_block_allocator_stat();
333 sum += mali_mem_os_stat();
338 u32 _mali_ukk_report_total_memory_size(void)
340 return mali_dedicated_mem_size + mali_shared_mem_size;
345 * Per-session memory descriptor mapping table sizes
347 #define MALI_MEM_DESCRIPTORS_INIT 64
348 #define MALI_MEM_DESCRIPTORS_MAX 65536
350 _mali_osk_errcode_t mali_memory_session_begin(struct mali_session_data *session_data)
352 MALI_DEBUG_PRINT(5, ("Memory session begin\n"));
354 session_data->memory_lock = _mali_osk_mutex_init(_MALI_OSK_LOCKFLAG_ORDERED,
355 _MALI_OSK_LOCK_ORDER_MEM_SESSION);
357 if (NULL == session_data->memory_lock) {
358 MALI_ERROR(_MALI_OSK_ERR_FAULT);
361 session_data->cow_lock = _mali_osk_mutex_init(_MALI_OSK_LOCKFLAG_UNORDERED, 0);
362 if (NULL == session_data->cow_lock) {
363 _mali_osk_mutex_term(session_data->memory_lock);
364 _mali_osk_free(session_data);
365 MALI_ERROR(_MALI_OSK_ERR_FAULT);
368 mali_memory_manager_init(&session_data->allocation_mgr);
370 MALI_DEBUG_PRINT(5, ("MMU session begin: success\n"));
374 void mali_memory_session_end(struct mali_session_data *session)
376 MALI_DEBUG_PRINT(3, ("MMU session end\n"));
378 if (NULL == session) {
379 MALI_DEBUG_PRINT(1, ("No session data found during session end\n"));
382 /* free allocation */
383 mali_free_session_allocations(session);
384 /* do some check in unint*/
385 mali_memory_manager_uninit(&session->allocation_mgr);
388 _mali_osk_mutex_term(session->memory_lock);
389 _mali_osk_mutex_term(session->cow_lock);
393 _mali_osk_errcode_t mali_memory_initialize(void)
395 _mali_osk_errcode_t err;
397 idr_init(&mali_backend_idr);
398 mutex_init(&mali_idr_mutex);
400 err = mali_mem_swap_init();
401 if (err != _MALI_OSK_ERR_OK) {
404 err = mali_mem_os_init();
405 if (_MALI_OSK_ERR_OK == err) {
406 err = mali_mem_defer_bind_manager_init();
412 void mali_memory_terminate(void)
414 mali_mem_swap_term();
415 mali_mem_defer_bind_manager_destory();
417 if (mali_memory_have_dedicated_memory()) {
418 mali_mem_block_allocator_destroy();
423 struct mali_page_node *_mali_page_node_allocate(mali_page_node_type type)
425 mali_page_node *page_node = NULL;
427 page_node = kzalloc(sizeof(mali_page_node), GFP_KERNEL);
428 MALI_DEBUG_ASSERT(NULL != page_node);
431 page_node->type = type;
432 INIT_LIST_HEAD(&page_node->list);
438 void _mali_page_node_ref(struct mali_page_node *node)
440 if (node->type == MALI_PAGE_NODE_OS) {
441 /* add ref to this page */
442 get_page(node->page);
443 } else if (node->type == MALI_PAGE_NODE_BLOCK) {
444 mali_mem_block_add_ref(node);
445 } else if (node->type == MALI_PAGE_NODE_SWAP) {
446 atomic_inc(&node->swap_it->ref_count);
448 MALI_DEBUG_PRINT_ERROR(("Invalid type of mali page node! \n"));
452 void _mali_page_node_unref(struct mali_page_node *node)
454 if (node->type == MALI_PAGE_NODE_OS) {
455 /* unref to this page */
456 put_page(node->page);
457 } else if (node->type == MALI_PAGE_NODE_BLOCK) {
458 mali_mem_block_dec_ref(node);
460 MALI_DEBUG_PRINT_ERROR(("Invalid type of mali page node! \n"));
465 void _mali_page_node_add_page(struct mali_page_node *node, struct page *page)
467 MALI_DEBUG_ASSERT(MALI_PAGE_NODE_OS == node->type);
472 void _mali_page_node_add_swap_item(struct mali_page_node *node, struct mali_swap_item *item)
474 MALI_DEBUG_ASSERT(MALI_PAGE_NODE_SWAP == node->type);
475 node->swap_it = item;
478 void _mali_page_node_add_block_item(struct mali_page_node *node, mali_block_item *item)
480 MALI_DEBUG_ASSERT(MALI_PAGE_NODE_BLOCK == node->type);
485 int _mali_page_node_get_ref_count(struct mali_page_node *node)
487 if (node->type == MALI_PAGE_NODE_OS) {
488 /* get ref count of this page */
489 return page_count(node->page);
490 } else if (node->type == MALI_PAGE_NODE_BLOCK) {
491 return mali_mem_block_get_ref_count(node);
492 } else if (node->type == MALI_PAGE_NODE_SWAP) {
493 return atomic_read(&node->swap_it->ref_count);
495 MALI_DEBUG_PRINT_ERROR(("Invalid type of mali page node! \n"));
501 dma_addr_t _mali_page_node_get_dma_addr(struct mali_page_node *node)
503 if (node->type == MALI_PAGE_NODE_OS) {
504 return page_private(node->page);
505 } else if (node->type == MALI_PAGE_NODE_BLOCK) {
506 return _mali_blk_item_get_phy_addr(node->blk_it);
507 } else if (node->type == MALI_PAGE_NODE_SWAP) {
508 return node->swap_it->dma_addr;
510 MALI_DEBUG_PRINT_ERROR(("Invalid type of mali page node! \n"));
516 unsigned long _mali_page_node_get_pfn(struct mali_page_node *node)
518 if (node->type == MALI_PAGE_NODE_OS) {
519 return page_to_pfn(node->page);
520 } else if (node->type == MALI_PAGE_NODE_BLOCK) {
521 /* get phy addr for BLOCK page*/
522 return _mali_blk_item_get_pfn(node->blk_it);
523 } else if (node->type == MALI_PAGE_NODE_SWAP) {
524 return page_to_pfn(node->swap_it->page);
526 MALI_DEBUG_PRINT_ERROR(("Invalid type of mali page node! \n"));