summaryrefslogtreecommitdiff
path: root/drivers/gpu/arm/utgard/linux/mali_memory_virtual.c
blob: 35f8f90a444dee715a62b5bea7a3f81ac143f55a (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
/*
 * Copyright (C) 2013-2015 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/platform_device.h>

#include "mali_osk.h"
#include "mali_osk_mali.h"
#include "mali_kernel_linux.h"
#include "mali_scheduler.h"
#include "mali_memory_os_alloc.h"
#include "mali_memory_manager.h"
#include "mali_memory_virtual.h"


/**
*internal helper to link node into the rb-tree
*/
static inline void _mali_vma_offset_add_rb(struct mali_allocation_manager *mgr,
		struct mali_vma_node *node)
{
	struct rb_node **iter = &mgr->allocation_mgr_rb.rb_node;
	struct rb_node *parent = NULL;
	struct mali_vma_node *iter_node;

	while (likely(*iter)) {
		parent = *iter;
		iter_node = rb_entry(*iter, struct mali_vma_node, vm_rb);

		if (node->vm_node.start < iter_node->vm_node.start)
			iter = &(*iter)->rb_left;
		else if (node->vm_node.start > iter_node->vm_node.start)
			iter = &(*iter)->rb_right;
		else
			MALI_DEBUG_ASSERT(0);
	}

	rb_link_node(&node->vm_rb, parent, iter);
	rb_insert_color(&node->vm_rb, &mgr->allocation_mgr_rb);
}

/**
 * mali_vma_offset_add() - Add offset node to RB Tree
 */
int mali_vma_offset_add(struct mali_allocation_manager *mgr,
			struct mali_vma_node *node)
{
	int ret = 0;
	write_lock(&mgr->vm_lock);

	if (node->vm_node.allocated) {
		goto out;
	}

	_mali_vma_offset_add_rb(mgr, node);
	/* set to allocated */
	node->vm_node.allocated = 1;

out:
	write_unlock(&mgr->vm_lock);
	return ret;
}

/**
 * mali_vma_offset_remove() - Remove offset node from RB tree
 */
void mali_vma_offset_remove(struct mali_allocation_manager *mgr,
			    struct mali_vma_node *node)
{
	write_lock(&mgr->vm_lock);

	if (node->vm_node.allocated) {
		rb_erase(&node->vm_rb, &mgr->allocation_mgr_rb);
		memset(&node->vm_node, 0, sizeof(node->vm_node));
	}
	write_unlock(&mgr->vm_lock);
}

/**
* mali_vma_offset_search - Search the node in RB tree
*/
struct mali_vma_node *mali_vma_offset_search(struct mali_allocation_manager *mgr,
		unsigned long start, unsigned long pages)
{
	struct mali_vma_node *node, *best;
	struct rb_node *iter;
	unsigned long offset;
	read_lock(&mgr->vm_lock);

	iter = mgr->allocation_mgr_rb.rb_node;
	best = NULL;

	while (likely(iter)) {
		node = rb_entry(iter, struct mali_vma_node, vm_rb);
		offset = node->vm_node.start;
		if (start >= offset) {
			iter = iter->rb_right;
			best = node;
			if (start == offset)
				break;
		} else {
			iter = iter->rb_left;
		}
	}

	if (best) {
		offset = best->vm_node.start + best->vm_node.size;
		if (offset <= start + pages)
			best = NULL;
	}
	read_unlock(&mgr->vm_lock);

	return best;
}