2 * Copyright (C) 2012-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.
13 #include "mali_kernel_common.h"
14 #include "mali_session.h"
15 #include "mali_kernel_linux.h"
17 #include "mali_memory.h"
19 #include "ump_kernel_interface.h"
21 static int mali_ump_map(struct mali_session_data *session, mali_mem_allocation *descriptor)
23 ump_dd_handle ump_mem;
26 ump_dd_physical_block *ump_blocks;
27 struct mali_page_directory *pagedir;
30 _mali_osk_errcode_t err;
32 MALI_DEBUG_ASSERT_POINTER(session);
33 MALI_DEBUG_ASSERT_POINTER(descriptor);
34 MALI_DEBUG_ASSERT(MALI_MEM_UMP == descriptor->type);
36 ump_mem = descriptor->ump_mem.handle;
37 MALI_DEBUG_ASSERT(UMP_DD_HANDLE_INVALID != ump_mem);
39 nr_blocks = ump_dd_phys_block_count_get(ump_mem);
41 MALI_DEBUG_PRINT(1, ("No block count\n"));
45 ump_blocks = _mali_osk_malloc(sizeof(*ump_blocks) * nr_blocks);
46 if (NULL == ump_blocks) {
50 if (UMP_DD_INVALID == ump_dd_phys_blocks_get(ump_mem, ump_blocks, nr_blocks)) {
51 _mali_osk_free(ump_blocks);
55 pagedir = session->page_directory;
56 prop = descriptor->mali_mapping.properties;
58 err = mali_mem_mali_map_prepare(descriptor);
59 if (_MALI_OSK_ERR_OK != err) {
60 MALI_DEBUG_PRINT(1, ("Mapping of UMP memory failed\n"));
62 _mali_osk_free(ump_blocks);
66 for (i = 0; i < nr_blocks; ++i) {
67 u32 virt = descriptor->mali_mapping.addr + offset;
69 MALI_DEBUG_PRINT(7, ("Mapping in 0x%08x size %d\n", ump_blocks[i].addr , ump_blocks[i].size));
71 mali_mmu_pagedir_update(pagedir, virt, ump_blocks[i].addr,
72 ump_blocks[i].size, prop);
74 offset += ump_blocks[i].size;
77 if (descriptor->flags & _MALI_MAP_EXTERNAL_MAP_GUARD_PAGE) {
78 u32 virt = descriptor->mali_mapping.addr + offset;
80 /* Map in an extra virtual guard page at the end of the VMA */
81 MALI_DEBUG_PRINT(6, ("Mapping in extra guard page\n"));
83 mali_mmu_pagedir_update(pagedir, virt, ump_blocks[0].addr, _MALI_OSK_MALI_PAGE_SIZE, prop);
85 offset += _MALI_OSK_MALI_PAGE_SIZE;
88 _mali_osk_free(ump_blocks);
93 void mali_ump_unmap(struct mali_session_data *session, mali_mem_allocation *descriptor)
95 ump_dd_handle ump_mem;
96 struct mali_page_directory *pagedir;
98 ump_mem = descriptor->ump_mem.handle;
99 pagedir = session->page_directory;
101 MALI_DEBUG_ASSERT(UMP_DD_HANDLE_INVALID != ump_mem);
103 mali_mem_mali_map_free(descriptor);
105 ump_dd_reference_release(ump_mem);
109 _mali_osk_errcode_t _mali_ukk_attach_ump_mem(_mali_uk_attach_ump_mem_s *args)
111 ump_dd_handle ump_mem;
112 struct mali_session_data *session;
113 mali_mem_allocation *descriptor;
116 MALI_DEBUG_ASSERT_POINTER(args);
117 MALI_DEBUG_ASSERT(NULL != (void *)(uintptr_t)args->ctx);
119 session = (struct mali_session_data *)(uintptr_t)args->ctx;
121 /* check arguments */
122 /* NULL might be a valid Mali address */
123 if (!args->size) MALI_ERROR(_MALI_OSK_ERR_INVALID_ARGS);
125 /* size must be a multiple of the system page size */
126 if (args->size % _MALI_OSK_MALI_PAGE_SIZE) MALI_ERROR(_MALI_OSK_ERR_INVALID_ARGS);
129 ("Requested to map ump memory with secure id %d into virtual memory 0x%08X, size 0x%08X\n",
130 args->secure_id, args->mali_address, args->size));
132 ump_mem = ump_dd_handle_create_from_secure_id((int)args->secure_id);
134 if (UMP_DD_HANDLE_INVALID == ump_mem) MALI_ERROR(_MALI_OSK_ERR_FAULT);
136 descriptor = mali_mem_descriptor_create(session, MALI_MEM_UMP);
137 if (NULL == descriptor) {
138 ump_dd_reference_release(ump_mem);
139 MALI_ERROR(_MALI_OSK_ERR_NOMEM);
142 descriptor->ump_mem.handle = ump_mem;
143 descriptor->mali_mapping.addr = args->mali_address;
144 descriptor->size = args->size;
145 descriptor->mali_mapping.properties = MALI_MMU_FLAGS_DEFAULT;
146 descriptor->flags |= MALI_MEM_FLAG_DONT_CPU_MAP;
148 if (args->flags & _MALI_MAP_EXTERNAL_MAP_GUARD_PAGE) {
149 descriptor->flags = MALI_MEM_FLAG_MALI_GUARD_PAGE;
152 _mali_osk_mutex_wait(session->memory_lock);
154 ret = mali_ump_map(session, descriptor);
156 _mali_osk_mutex_signal(session->memory_lock);
157 ump_dd_reference_release(ump_mem);
158 mali_mem_descriptor_destroy(descriptor);
159 MALI_ERROR(_MALI_OSK_ERR_NOMEM);
162 _mali_osk_mutex_signal(session->memory_lock);
165 if (_MALI_OSK_ERR_OK != mali_descriptor_mapping_allocate_mapping(session->descriptor_mapping, descriptor, &md)) {
166 ump_dd_reference_release(ump_mem);
167 mali_mem_descriptor_destroy(descriptor);
168 MALI_ERROR(_MALI_OSK_ERR_FAULT);
173 MALI_DEBUG_PRINT(5, ("Returning from UMP attach\n"));
178 void mali_mem_ump_release(mali_mem_allocation *descriptor)
180 struct mali_session_data *session = descriptor->session;
182 MALI_DEBUG_ASSERT(MALI_MEM_UMP == descriptor->type);
184 mali_ump_unmap(session, descriptor);
187 _mali_osk_errcode_t _mali_ukk_release_ump_mem(_mali_uk_release_ump_mem_s *args)
189 mali_mem_allocation *descriptor;
190 struct mali_session_data *session;
192 MALI_DEBUG_ASSERT_POINTER(args);
193 MALI_DEBUG_ASSERT(NULL != (void *)(uintptr_t)args->ctx);
195 session = (struct mali_session_data *)(uintptr_t)args->ctx;
197 if (_MALI_OSK_ERR_OK != mali_descriptor_mapping_get(session->descriptor_mapping, args->cookie, (void **)&descriptor)) {
198 MALI_DEBUG_PRINT(1, ("Invalid memory descriptor %d used to release ump memory\n", args->cookie));
199 MALI_ERROR(_MALI_OSK_ERR_FAULT);
202 descriptor = mali_descriptor_mapping_free(session->descriptor_mapping, args->cookie);
204 if (NULL != descriptor) {
205 _mali_osk_mutex_wait(session->memory_lock);
206 mali_mem_ump_release(descriptor);
207 _mali_osk_mutex_signal(session->memory_lock);
209 mali_mem_descriptor_destroy(descriptor);