aboutsummaryrefslogtreecommitdiff
path: root/drivers/gpu/arm/utgard/linux/mali_memory_block_alloc.c
blob: d4264ecf0f9a5552af4d2f4e8ecee484087fc001 (plain)
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
/*
 * Copyright (C) 2010-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 "mali_kernel_common.h"
#include "mali_memory.h"
#include "mali_memory_block_alloc.h"
#include "mali_osk.h"
#include <linux/mutex.h>
#define MALI_BLOCK_SIZE (256UL * 1024UL)  /* 256 kB, remember to keep the ()s */

struct block_info {
	struct block_info *next;
};

typedef struct block_info block_info;


typedef struct block_allocator {
	struct mutex mutex;
	block_info *all_blocks;
	block_info *first_free;
	u32 base;
	u32 cpu_usage_adjust;
	u32 num_blocks;
	u32 free_blocks;
} block_allocator;

static block_allocator *mali_mem_block_gobal_allocator = NULL;

MALI_STATIC_INLINE u32 get_phys(block_allocator *info, block_info *block)
{
	return info->base + ((block - info->all_blocks) * MALI_BLOCK_SIZE);
}

static mali_mem_allocator *mali_mem_block_allocator_create(u32 base_address, u32 cpu_usage_adjust, u32 size)
{
	block_allocator *info;
	u32 usable_size;
	u32 num_blocks;

	usable_size = size & ~(MALI_BLOCK_SIZE - 1);
	MALI_DEBUG_PRINT(3, ("Mali block allocator create for region starting at 0x%08X length 0x%08X\n", base_address, size));
	MALI_DEBUG_PRINT(4, ("%d usable bytes\n", usable_size));
	num_blocks = usable_size / MALI_BLOCK_SIZE;
	MALI_DEBUG_PRINT(4, ("which becomes %d blocks\n", num_blocks));

	if (usable_size == 0) {
		MALI_DEBUG_PRINT(1, ("Memory block of size %d is unusable\n", size));
		return NULL;
	}

	info = _mali_osk_malloc(sizeof(block_allocator));
	if (NULL != info) {
		mutex_init(&info->mutex);
		info->all_blocks = _mali_osk_malloc(sizeof(block_info) * num_blocks);
		if (NULL != info->all_blocks) {
			u32 i;
			info->first_free = NULL;
			info->num_blocks = num_blocks;
			info->free_blocks = num_blocks;

			info->base = base_address;
			info->cpu_usage_adjust = cpu_usage_adjust;

			for (i = 0; i < num_blocks; i++) {
				info->all_blocks[i].next = info->first_free;
				info->first_free = &info->all_blocks[i];
			}

			return (mali_mem_allocator *)info;
		}
		_mali_osk_free(info);
	}

	return NULL;
}

void mali_mem_block_allocator_destroy(mali_mem_allocator *allocator)
{
	block_allocator *info = (block_allocator *)allocator;

	info = mali_mem_block_gobal_allocator;
	if (NULL == info) return;

	MALI_DEBUG_ASSERT_POINTER(info);

	_mali_osk_free(info->all_blocks);
	_mali_osk_free(info);
}

static void mali_mem_block_mali_map(mali_mem_allocation *descriptor, u32 phys, u32 virt, u32 size)
{
	struct mali_page_directory *pagedir = descriptor->session->page_directory;
	u32 prop = descriptor->mali_mapping.properties;
	u32 offset = 0;

	while (size) {
		mali_mmu_pagedir_update(pagedir, virt + offset, phys + offset, MALI_MMU_PAGE_SIZE, prop);

		size -= MALI_MMU_PAGE_SIZE;
		offset += MALI_MMU_PAGE_SIZE;
	}
}

static int mali_mem_block_cpu_map(mali_mem_allocation *descriptor, struct vm_area_struct *vma, u32 mali_phys, u32 mapping_offset, u32 size, u32 cpu_usage_adjust)
{
	u32 virt = vma->vm_start + mapping_offset;
	u32 cpu_phys = mali_phys + cpu_usage_adjust;
	u32 offset = 0;
	int ret;

	while (size) {
		ret = vm_insert_pfn(vma, virt + offset, __phys_to_pfn(cpu_phys + offset));

		if (unlikely(ret)) {
			MALI_DEBUG_PRINT(1, ("Block allocator: Failed to insert pfn into vma\n"));
			return 1;
		}

		size -= MALI_MMU_PAGE_SIZE;
		offset += MALI_MMU_PAGE_SIZE;
	}

	return 0;
}

mali_mem_allocation *mali_mem_block_alloc(u32 mali_addr, u32 size, struct vm_area_struct *vma, struct mali_session_data *session)
{
	_mali_osk_errcode_t err;
	mali_mem_allocation *descriptor;
	block_allocator *info;
	u32 left;
	block_info *last_allocated = NULL;
	block_allocator_allocation *ret_allocation;
	u32 offset = 0;

	size = ALIGN(size, MALI_BLOCK_SIZE);

	info = mali_mem_block_gobal_allocator;
	if (NULL == info) return NULL;

	left = size;
	MALI_DEBUG_ASSERT(0 != left);

	descriptor = mali_mem_descriptor_create(session, MALI_MEM_BLOCK);
	if (NULL == descriptor) {
		return NULL;
	}

	descriptor->mali_mapping.addr = mali_addr;
	descriptor->size = size;
	descriptor->cpu_mapping.addr = (void __user *)vma->vm_start;
	descriptor->cpu_mapping.ref = 1;

	if (VM_SHARED == (VM_SHARED & vma->vm_flags)) {
		descriptor->mali_mapping.properties = MALI_MMU_FLAGS_DEFAULT;
	} else {
		/* Cached Mali memory mapping */
		descriptor->mali_mapping.properties = MALI_MMU_FLAGS_FORCE_GP_READ_ALLOCATE;
		vma->vm_flags |= VM_SHARED;
	}

	ret_allocation = &descriptor->block_mem.mem;

	ret_allocation->mapping_length = 0;

	_mali_osk_mutex_wait(session->memory_lock);
	mutex_lock(&info->mutex);

	if (left > (info->free_blocks * MALI_BLOCK_SIZE)) {
		MALI_DEBUG_PRINT(2, ("Mali block allocator: not enough free blocks to service allocation (%u)\n", left));
		mutex_unlock(&info->mutex);
		_mali_osk_mutex_signal(session->memory_lock);
		mali_mem_descriptor_destroy(descriptor);
		return NULL;
	}

	err = mali_mem_mali_map_prepare(descriptor);
	if (_MALI_OSK_ERR_OK != err) {
		mutex_unlock(&info->mutex);
		_mali_osk_mutex_signal(session->memory_lock);
		mali_mem_descriptor_destroy(descriptor);
		return NULL;
	}

	while ((left > 0) && (info->first_free)) {
		block_info *block;
		u32 phys_addr;
		u32 current_mapping_size;

		block = info->first_free;
		info->first_free = info->first_free->next;
		block->next = last_allocated;
		last_allocated = block;

		phys_addr = get_phys(info, block);

		if (MALI_BLOCK_SIZE < left) {
			current_mapping_size = MALI_BLOCK_SIZE;
		} else {
			current_mapping_size = left;
		}

		mali_mem_block_mali_map(descriptor, phys_addr, mali_addr + offset, current_mapping_size);
		if (mali_mem_block_cpu_map(descriptor, vma, phys_addr, offset, current_mapping_size, info->cpu_usage_adjust)) {
			/* release all memory back to the pool */
			while (last_allocated) {
				/* This relinks every block we've just allocated back into the free-list */
				block = last_allocated->next;
				last_allocated->next = info->first_free;
				info->first_free = last_allocated;
				last_allocated = block;
			}

			mutex_unlock(&info->mutex);
			_mali_osk_mutex_signal(session->memory_lock);

			mali_mem_mali_map_free(descriptor);
			mali_mem_descriptor_destroy(descriptor);

			return NULL;
		}

		left -= current_mapping_size;
		offset += current_mapping_size;
		ret_allocation->mapping_length += current_mapping_size;

		--info->free_blocks;
	}

	mutex_unlock(&info->mutex);
	_mali_osk_mutex_signal(session->memory_lock);

	MALI_DEBUG_ASSERT(0 == left);

	/* Record all the information about this allocation */
	ret_allocation->last_allocated = last_allocated;
	ret_allocation->info = info;

	return descriptor;
}

void mali_mem_block_release(mali_mem_allocation *descriptor)
{
	block_allocator *info = descriptor->block_mem.mem.info;
	block_info *block, *next;
	block_allocator_allocation *allocation = &descriptor->block_mem.mem;

	MALI_DEBUG_ASSERT(MALI_MEM_BLOCK == descriptor->type);

	block = allocation->last_allocated;

	MALI_DEBUG_ASSERT_POINTER(block);

	/* unmap */
	mali_mem_mali_map_free(descriptor);

	mutex_lock(&info->mutex);

	while (block) {
		MALI_DEBUG_ASSERT(!((block < info->all_blocks) || (block > (info->all_blocks + info->num_blocks))));

		next = block->next;

		/* relink into free-list */
		block->next = info->first_free;
		info->first_free = block;

		/* advance the loop */
		block = next;

		++info->free_blocks;
	}

	mutex_unlock(&info->mutex);
}

u32 mali_mem_block_allocator_stat(void)
{
	block_allocator *info = (block_allocator *)mali_mem_block_gobal_allocator;

	if (NULL == info) return 0;

	MALI_DEBUG_ASSERT_POINTER(info);

	return (info->num_blocks - info->free_blocks) * MALI_BLOCK_SIZE;
}

_mali_osk_errcode_t mali_memory_core_resource_dedicated_memory(u32 start, u32 size)
{
	mali_mem_allocator *allocator;

	/* Do the low level linux operation first */

	/* Request ownership of the memory */
	if (_MALI_OSK_ERR_OK != _mali_osk_mem_reqregion(start, size, "Dedicated Mali GPU memory")) {
		MALI_DEBUG_PRINT(1, ("Failed to request memory region for frame buffer (0x%08X - 0x%08X)\n", start, start + size - 1));
		return _MALI_OSK_ERR_FAULT;
	}

	/* Create generic block allocator object to handle it */
	allocator = mali_mem_block_allocator_create(start, 0 /* cpu_usage_adjust */, size);

	if (NULL == allocator) {
		MALI_DEBUG_PRINT(1, ("Memory bank registration failed\n"));
		_mali_osk_mem_unreqregion(start, size);
		MALI_ERROR(_MALI_OSK_ERR_FAULT);
	}

	mali_mem_block_gobal_allocator = (block_allocator *)allocator;

	return _MALI_OSK_ERR_OK;
}