xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/mali400/mali/linux/mali_memory_ump.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2012-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_ukk.h"
12 #include "mali_osk.h"
13 #include "mali_kernel_common.h"
14 #include "mali_session.h"
15 #include "mali_kernel_linux.h"
16 #include "mali_memory.h"
17 #include "ump_kernel_interface.h"
18 
mali_mem_ump_map(mali_mem_backend * mem_backend)19 static int mali_mem_ump_map(mali_mem_backend *mem_backend)
20 {
21 	ump_dd_handle ump_mem;
22 	mali_mem_allocation *alloc;
23 	struct mali_session_data *session;
24 	u32 nr_blocks;
25 	u32 i;
26 	ump_dd_physical_block *ump_blocks;
27 	struct mali_page_directory *pagedir;
28 	u32 offset = 0;
29 	_mali_osk_errcode_t err;
30 
31 	MALI_DEBUG_ASSERT_POINTER(mem_backend);
32 	MALI_DEBUG_ASSERT(MALI_MEM_UMP == mem_backend->type);
33 
34 	alloc = mem_backend->mali_allocation;
35 	MALI_DEBUG_ASSERT_POINTER(alloc);
36 
37 	session = alloc->session;
38 	MALI_DEBUG_ASSERT_POINTER(session);
39 
40 	ump_mem = mem_backend->ump_mem.handle;
41 	MALI_DEBUG_ASSERT(UMP_DD_HANDLE_INVALID != ump_mem);
42 
43 	nr_blocks = ump_dd_phys_block_count_get(ump_mem);
44 	if (nr_blocks == 0) {
45 		MALI_DEBUG_PRINT(1, ("No block count\n"));
46 		return -EINVAL;
47 	}
48 
49 	ump_blocks = _mali_osk_malloc(sizeof(*ump_blocks) * nr_blocks);
50 	if (NULL == ump_blocks) {
51 		return -ENOMEM;
52 	}
53 
54 	if (UMP_DD_INVALID == ump_dd_phys_blocks_get(ump_mem, ump_blocks, nr_blocks)) {
55 		_mali_osk_free(ump_blocks);
56 		return -EFAULT;
57 	}
58 
59 	pagedir = session->page_directory;
60 
61 	mali_session_memory_lock(session);
62 
63 	err = mali_mem_mali_map_prepare(alloc);
64 	if (_MALI_OSK_ERR_OK != err) {
65 		MALI_DEBUG_PRINT(1, ("Mapping of UMP memory failed\n"));
66 
67 		_mali_osk_free(ump_blocks);
68 		mali_session_memory_unlock(session);
69 		return -ENOMEM;
70 	}
71 
72 	for (i = 0; i < nr_blocks; ++i) {
73 		u32 virt = alloc->mali_vma_node.vm_node.start + offset;
74 
75 		MALI_DEBUG_PRINT(7, ("Mapping in 0x%08x size %d\n", ump_blocks[i].addr , ump_blocks[i].size));
76 
77 		mali_mmu_pagedir_update(pagedir, virt, ump_blocks[i].addr,
78 					ump_blocks[i].size, MALI_MMU_FLAGS_DEFAULT);
79 
80 		offset += ump_blocks[i].size;
81 	}
82 
83 	if (alloc->flags & _MALI_MAP_EXTERNAL_MAP_GUARD_PAGE) {
84 		u32 virt = alloc->mali_vma_node.vm_node.start + offset;
85 
86 		/* Map in an extra virtual guard page at the end of the VMA */
87 		MALI_DEBUG_PRINT(6, ("Mapping in extra guard page\n"));
88 
89 		mali_mmu_pagedir_update(pagedir, virt, ump_blocks[0].addr, _MALI_OSK_MALI_PAGE_SIZE, MALI_MMU_FLAGS_DEFAULT);
90 
91 		offset += _MALI_OSK_MALI_PAGE_SIZE;
92 	}
93 	mali_session_memory_unlock(session);
94 	_mali_osk_free(ump_blocks);
95 	return 0;
96 }
97 
mali_mem_ump_unmap(mali_mem_allocation * alloc)98 static void mali_mem_ump_unmap(mali_mem_allocation *alloc)
99 {
100 	struct mali_session_data *session;
101 	MALI_DEBUG_ASSERT_POINTER(alloc);
102 	session = alloc->session;
103 	MALI_DEBUG_ASSERT_POINTER(session);
104 	mali_session_memory_lock(session);
105 	mali_mem_mali_map_free(session, alloc->psize, alloc->mali_vma_node.vm_node.start,
106 			       alloc->flags);
107 	mali_session_memory_unlock(session);
108 }
109 
mali_mem_bind_ump_buf(mali_mem_allocation * alloc,mali_mem_backend * mem_backend,u32 secure_id,u32 flags)110 int mali_mem_bind_ump_buf(mali_mem_allocation *alloc, mali_mem_backend *mem_backend, u32  secure_id, u32 flags)
111 {
112 	ump_dd_handle ump_mem;
113 	int ret;
114 	MALI_DEBUG_ASSERT_POINTER(alloc);
115 	MALI_DEBUG_ASSERT_POINTER(mem_backend);
116 	MALI_DEBUG_ASSERT(MALI_MEM_UMP == mem_backend->type);
117 
118 	MALI_DEBUG_PRINT(3,
119 			 ("Requested to map ump memory with secure id %d into virtual memory 0x%08X, size 0x%08X\n",
120 			  secure_id, alloc->mali_vma_node.vm_node.start, alloc->mali_vma_node.vm_node.size));
121 
122 	ump_mem = ump_dd_handle_create_from_secure_id(secure_id);
123 	if (UMP_DD_HANDLE_INVALID == ump_mem) MALI_ERROR(_MALI_OSK_ERR_FAULT);
124 	alloc->flags |= MALI_MEM_FLAG_DONT_CPU_MAP;
125 	if (flags & _MALI_MAP_EXTERNAL_MAP_GUARD_PAGE) {
126 		alloc->flags |= MALI_MEM_FLAG_MALI_GUARD_PAGE;
127 	}
128 
129 	mem_backend->ump_mem.handle = ump_mem;
130 
131 	ret = mali_mem_ump_map(mem_backend);
132 	if (0 != ret) {
133 		ump_dd_reference_release(ump_mem);
134 		return _MALI_OSK_ERR_FAULT;
135 	}
136 	MALI_DEBUG_PRINT(3, ("Returning from UMP bind\n"));
137 	return _MALI_OSK_ERR_OK;
138 }
139 
mali_mem_unbind_ump_buf(mali_mem_backend * mem_backend)140 void mali_mem_unbind_ump_buf(mali_mem_backend *mem_backend)
141 {
142 	ump_dd_handle ump_mem;
143 	mali_mem_allocation *alloc;
144 	MALI_DEBUG_ASSERT_POINTER(mem_backend);
145 	MALI_DEBUG_ASSERT(MALI_MEM_UMP == mem_backend->type);
146 	ump_mem = mem_backend->ump_mem.handle;
147 	MALI_DEBUG_ASSERT(UMP_DD_HANDLE_INVALID != ump_mem);
148 
149 	alloc = mem_backend->mali_allocation;
150 	MALI_DEBUG_ASSERT_POINTER(alloc);
151 	mali_mem_ump_unmap(alloc);
152 	ump_dd_reference_release(ump_mem);
153 }
154 
155