xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/mali400/mali/linux/mali_memory_external.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2013-2017 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_osk.h"
13 #include "mali_ukk.h"
14 #include "mali_memory.h"
15 #include "mali_mem_validation.h"
16 #include "mali_uk_types.h"
17 
mali_mem_unbind_ext_buf(mali_mem_backend * mem_backend)18 void mali_mem_unbind_ext_buf(mali_mem_backend *mem_backend)
19 {
20 	mali_mem_allocation *alloc;
21 	struct mali_session_data *session;
22 	MALI_DEBUG_ASSERT_POINTER(mem_backend);
23 	alloc = mem_backend->mali_allocation;
24 	MALI_DEBUG_ASSERT_POINTER(alloc);
25 	MALI_DEBUG_ASSERT(MALI_MEM_EXTERNAL == mem_backend->type);
26 
27 	session = alloc->session;
28 	MALI_DEBUG_ASSERT_POINTER(session);
29 	mali_session_memory_lock(session);
30 	mali_mem_mali_map_free(session, alloc->psize, alloc->mali_vma_node.vm_node.start,
31 			       alloc->flags);
32 	mali_session_memory_unlock(session);
33 }
34 
mali_mem_bind_ext_buf(mali_mem_allocation * alloc,mali_mem_backend * mem_backend,u32 phys_addr,u32 flag)35 _mali_osk_errcode_t mali_mem_bind_ext_buf(mali_mem_allocation *alloc,
36 		mali_mem_backend *mem_backend,
37 		u32 phys_addr,
38 		u32 flag)
39 {
40 	struct mali_session_data *session;
41 	_mali_osk_errcode_t err;
42 	u32 virt, phys, size;
43 	MALI_DEBUG_ASSERT_POINTER(mem_backend);
44 	MALI_DEBUG_ASSERT_POINTER(alloc);
45 	size = alloc->psize;
46 	session = (struct mali_session_data *)(uintptr_t)alloc->session;
47 	MALI_CHECK_NON_NULL(session, _MALI_OSK_ERR_INVALID_ARGS);
48 
49 	/* check arguments */
50 	/* NULL might be a valid Mali address */
51 	if (!size) MALI_ERROR(_MALI_OSK_ERR_INVALID_ARGS);
52 
53 	/* size must be a multiple of the system page size */
54 	if (size % _MALI_OSK_MALI_PAGE_SIZE) MALI_ERROR(_MALI_OSK_ERR_INVALID_ARGS);
55 
56 	/* Validate the mali physical range */
57 	if (_MALI_OSK_ERR_OK != mali_mem_validation_check(phys_addr, size)) {
58 		return _MALI_OSK_ERR_FAULT;
59 	}
60 
61 	if (flag & _MALI_MAP_EXTERNAL_MAP_GUARD_PAGE) {
62 		alloc->flags |= MALI_MEM_FLAG_MALI_GUARD_PAGE;
63 	}
64 
65 	mali_session_memory_lock(session);
66 
67 	virt = alloc->mali_vma_node.vm_node.start;
68 	phys = phys_addr;
69 
70 	err = mali_mem_mali_map_prepare(alloc);
71 	if (_MALI_OSK_ERR_OK != err) {
72 		mali_session_memory_unlock(session);
73 		return _MALI_OSK_ERR_NOMEM;
74 	}
75 
76 	mali_mmu_pagedir_update(session->page_directory, virt, phys, size, MALI_MMU_FLAGS_DEFAULT);
77 
78 	if (alloc->flags & MALI_MEM_FLAG_MALI_GUARD_PAGE) {
79 		mali_mmu_pagedir_update(session->page_directory, virt + size, phys, _MALI_OSK_MALI_PAGE_SIZE, MALI_MMU_FLAGS_DEFAULT);
80 	}
81 	MALI_DEBUG_PRINT(3,
82 			 ("Requested to map physical memory 0x%x-0x%x into virtual memory 0x%x\n",
83 			  phys_addr, (phys_addr + size - 1),
84 			  virt));
85 	mali_session_memory_unlock(session);
86 
87 	MALI_SUCCESS;
88 }
89 
90