1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
|
/*
* Copyright (C) 2013-2014 ARM Limited. All rights reserved.
*
* This program is free software and is provided to you under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation, and any use by you of this program is subject to the terms of such GNU licence.
*
* A copy of the licence is included with the program, and can also be obtained from Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <linux/list.h>
#include <linux/mm.h>
#include <linux/mm_types.h>
#include <linux/fs.h>
#include <linux/dma-mapping.h>
#include <linux/slab.h>
#include <linux/version.h>
#include <linux/platform_device.h>
#include "mali_osk.h"
#include "mali_osk_mali.h"
#include "mali_kernel_linux.h"
#include "mali_scheduler.h"
#include "mali_kernel_descriptor_mapping.h"
#include "mali_memory.h"
#include "mali_memory_dma_buf.h"
#include "mali_memory_os_alloc.h"
#include "mali_memory_block_alloc.h"
/* session->memory_lock must be held when calling this function */
static void mali_mem_release(mali_mem_allocation *descriptor)
{
MALI_DEBUG_ASSERT_POINTER(descriptor);
MALI_DEBUG_ASSERT_LOCK_HELD(descriptor->session->memory_lock);
MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
switch (descriptor->type) {
case MALI_MEM_OS:
mali_mem_os_release(descriptor);
break;
case MALI_MEM_DMA_BUF:
#if defined(CONFIG_DMA_SHARED_BUFFER)
mali_mem_dma_buf_release(descriptor);
#endif
break;
case MALI_MEM_UMP:
#if defined(CONFIG_MALI400_UMP)
mali_mem_ump_release(descriptor);
#endif
break;
case MALI_MEM_EXTERNAL:
mali_mem_external_release(descriptor);
break;
case MALI_MEM_BLOCK:
mali_mem_block_release(descriptor);
break;
}
}
static void mali_mem_vma_open(struct vm_area_struct *vma)
{
mali_mem_allocation *descriptor = (mali_mem_allocation *)vma->vm_private_data;
MALI_DEBUG_PRINT(4, ("Open called on vma %p\n", vma));
descriptor->cpu_mapping.ref++;
return;
}
static void mali_mem_vma_close(struct vm_area_struct *vma)
{
mali_mem_allocation *descriptor;
struct mali_session_data *session;
mali_mem_virt_cpu_mapping *mapping;
MALI_DEBUG_PRINT(3, ("Close called on vma %p\n", vma));
descriptor = (mali_mem_allocation *)vma->vm_private_data;
BUG_ON(!descriptor);
MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
mapping = &descriptor->cpu_mapping;
BUG_ON(0 == mapping->ref);
mapping->ref--;
if (0 != mapping->ref) {
MALI_DEBUG_PRINT(3, ("Ignoring this close, %d references still exists\n", mapping->ref));
return;
}
session = descriptor->session;
mali_descriptor_mapping_free(session->descriptor_mapping, descriptor->id);
_mali_osk_mutex_wait(session->memory_lock);
mali_mem_release(descriptor);
_mali_osk_mutex_signal(session->memory_lock);
mali_mem_descriptor_destroy(descriptor);
}
static int mali_kernel_memory_cpu_page_fault_handler(struct vm_area_struct *vma, struct vm_fault *vmf)
{
void __user *address;
mali_mem_allocation *descriptor;
address = vmf->virtual_address;
descriptor = (mali_mem_allocation *)vma->vm_private_data;
MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
/*
* We always fail the call since all memory is pre-faulted when assigned to the process.
* Only the Mali cores can use page faults to extend buffers.
*/
MALI_DEBUG_PRINT(1, ("Page-fault in Mali memory region caused by the CPU.\n"));
MALI_DEBUG_PRINT(1, ("Tried to access %p (process local virtual address) which is not currently mapped to any Mali memory.\n", (void *)address));
MALI_IGNORE(address);
MALI_IGNORE(descriptor);
return VM_FAULT_SIGBUS;
}
static struct vm_operations_struct mali_kernel_vm_ops = {
.open = mali_mem_vma_open,
.close = mali_mem_vma_close,
.fault = mali_kernel_memory_cpu_page_fault_handler
};
/** @note munmap handler is done by vma close handler */
int mali_mmap(struct file *filp, struct vm_area_struct *vma)
{
struct mali_session_data *session;
mali_mem_allocation *descriptor;
u32 size = vma->vm_end - vma->vm_start;
u32 mali_addr = vma->vm_pgoff << PAGE_SHIFT;
session = (struct mali_session_data *)filp->private_data;
if (NULL == session) {
MALI_PRINT_ERROR(("mmap called without any session data available\n"));
return -EFAULT;
}
MALI_DEBUG_PRINT(4, ("MMap() handler: start=0x%08X, phys=0x%08X, size=0x%08X vma->flags 0x%08x\n",
(unsigned int)vma->vm_start, (unsigned int)(vma->vm_pgoff << PAGE_SHIFT),
(unsigned int)(vma->vm_end - vma->vm_start), vma->vm_flags));
/* Set some bits which indicate that, the memory is IO memory, meaning
* that no paging is to be performed and the memory should not be
* included in crash dumps. And that the memory is reserved, meaning
* that it's present and can never be paged out (see also previous
* entry)
*/
vma->vm_flags |= VM_IO;
vma->vm_flags |= VM_DONTCOPY;
vma->vm_flags |= VM_PFNMAP;
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 7, 0)
vma->vm_flags |= VM_RESERVED;
#else
vma->vm_flags |= VM_DONTDUMP;
vma->vm_flags |= VM_DONTEXPAND;
#endif
vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot);
vma->vm_ops = &mali_kernel_vm_ops; /* Operations used on any memory system */
descriptor = mali_mem_block_alloc(mali_addr, size, vma, session);
if (NULL == descriptor) {
descriptor = mali_mem_os_alloc(mali_addr, size, vma, session);
if (NULL == descriptor) {
MALI_DEBUG_PRINT(3, ("MMAP failed\n"));
return -ENOMEM;
}
}
MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
vma->vm_private_data = (void *)descriptor;
/* Put on descriptor map */
if (_MALI_OSK_ERR_OK != mali_descriptor_mapping_allocate_mapping(session->descriptor_mapping, descriptor, &descriptor->id)) {
_mali_osk_mutex_wait(session->memory_lock);
mali_mem_os_release(descriptor);
_mali_osk_mutex_signal(session->memory_lock);
return -EFAULT;
}
return 0;
}
/* Prepare memory descriptor */
mali_mem_allocation *mali_mem_descriptor_create(struct mali_session_data *session, mali_mem_type type)
{
mali_mem_allocation *descriptor;
descriptor = (mali_mem_allocation *)kzalloc(sizeof(mali_mem_allocation), GFP_KERNEL);
if (NULL == descriptor) {
MALI_DEBUG_PRINT(3, ("mali_ukk_mem_mmap: descriptor was NULL\n"));
return NULL;
}
MALI_DEBUG_CODE(descriptor->magic = MALI_MEM_ALLOCATION_VALID_MAGIC);
descriptor->flags = 0;
descriptor->type = type;
descriptor->session = session;
return descriptor;
}
void mali_mem_descriptor_destroy(mali_mem_allocation *descriptor)
{
MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
MALI_DEBUG_CODE(descriptor->magic = MALI_MEM_ALLOCATION_FREED_MAGIC);
kfree(descriptor);
}
_mali_osk_errcode_t mali_mem_mali_map_prepare(mali_mem_allocation *descriptor)
{
u32 size = descriptor->size;
struct mali_session_data *session = descriptor->session;
MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
/* Map dma-buf into this session's page tables */
if (descriptor->flags & MALI_MEM_FLAG_MALI_GUARD_PAGE) {
size += MALI_MMU_PAGE_SIZE;
}
return mali_mmu_pagedir_map(session->page_directory, descriptor->mali_mapping.addr, size);
}
void mali_mem_mali_map_free(mali_mem_allocation *descriptor)
{
u32 size = descriptor->size;
struct mali_session_data *session = descriptor->session;
MALI_DEBUG_ASSERT(MALI_MEM_ALLOCATION_VALID_MAGIC == descriptor->magic);
if (descriptor->flags & MALI_MEM_FLAG_MALI_GUARD_PAGE) {
size += MALI_MMU_PAGE_SIZE;
}
/* Umap and flush L2 */
mali_mmu_pagedir_unmap(session->page_directory, descriptor->mali_mapping.addr, descriptor->size);
mali_scheduler_zap_all_active(session);
}
u32 _mali_ukk_report_memory_usage(void)
{
u32 sum = 0;
sum += mali_mem_block_allocator_stat();
sum += mali_mem_os_stat();
return sum;
}
/**
* Per-session memory descriptor mapping table sizes
*/
#define MALI_MEM_DESCRIPTORS_INIT 64
#define MALI_MEM_DESCRIPTORS_MAX 65536
_mali_osk_errcode_t mali_memory_session_begin(struct mali_session_data *session_data)
{
MALI_DEBUG_PRINT(5, ("Memory session begin\n"));
/* Create descriptor mapping table */
session_data->descriptor_mapping = mali_descriptor_mapping_create(MALI_MEM_DESCRIPTORS_INIT, MALI_MEM_DESCRIPTORS_MAX);
if (NULL == session_data->descriptor_mapping) {
MALI_ERROR(_MALI_OSK_ERR_NOMEM);
}
session_data->memory_lock = _mali_osk_mutex_init(_MALI_OSK_LOCKFLAG_ORDERED,
_MALI_OSK_LOCK_ORDER_MEM_SESSION);
if (NULL == session_data->memory_lock) {
mali_descriptor_mapping_destroy(session_data->descriptor_mapping);
_mali_osk_free(session_data);
MALI_ERROR(_MALI_OSK_ERR_FAULT);
}
MALI_DEBUG_PRINT(5, ("MMU session begin: success\n"));
MALI_SUCCESS;
}
/** @brief Callback function that releases memory
*
* session->memory_lock must be held when calling this function.
*/
static void descriptor_table_cleanup_callback(int descriptor_id, void *map_target)
{
mali_mem_allocation *descriptor;
descriptor = (mali_mem_allocation *)map_target;
MALI_DEBUG_ASSERT_LOCK_HELD(descriptor->session->memory_lock);
MALI_DEBUG_PRINT(3, ("Cleanup of descriptor %d mapping to 0x%x in descriptor table\n", descriptor_id, map_target));
MALI_DEBUG_ASSERT(descriptor);
mali_mem_release(descriptor);
mali_mem_descriptor_destroy(descriptor);
}
void mali_memory_session_end(struct mali_session_data *session)
{
MALI_DEBUG_PRINT(3, ("MMU session end\n"));
if (NULL == session) {
MALI_DEBUG_PRINT(1, ("No session data found during session end\n"));
return;
}
/* Lock the session so we can modify the memory list */
_mali_osk_mutex_wait(session->memory_lock);
/* Free all allocations still in the descriptor map, and terminate the map */
if (NULL != session->descriptor_mapping) {
mali_descriptor_mapping_call_for_each(session->descriptor_mapping, descriptor_table_cleanup_callback);
mali_descriptor_mapping_destroy(session->descriptor_mapping);
session->descriptor_mapping = NULL;
}
_mali_osk_mutex_signal(session->memory_lock);
/* Free the lock */
_mali_osk_mutex_term(session->memory_lock);
return;
}
_mali_osk_errcode_t mali_memory_initialize(void)
{
return mali_mem_os_init();
}
void mali_memory_terminate(void)
{
mali_mem_os_term();
mali_mem_block_allocator_destroy(NULL);
}
|