xref: /OK3568_Linux_fs/kernel/drivers/gpu/arm/mali400/mali/linux/mali_memory_virtual.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright (C) 2013-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 <linux/list.h>
12 #include <linux/mm.h>
13 #include <linux/mm_types.h>
14 #include <linux/fs.h>
15 #include <linux/dma-mapping.h>
16 #include <linux/slab.h>
17 #include <linux/platform_device.h>
18 
19 #include "mali_osk.h"
20 #include "mali_osk_mali.h"
21 #include "mali_kernel_linux.h"
22 #include "mali_scheduler.h"
23 #include "mali_memory_os_alloc.h"
24 #include "mali_memory_manager.h"
25 #include "mali_memory_virtual.h"
26 
27 
28 /**
29 *internal helper to link node into the rb-tree
30 */
_mali_vma_offset_add_rb(struct mali_allocation_manager * mgr,struct mali_vma_node * node)31 static inline void _mali_vma_offset_add_rb(struct mali_allocation_manager *mgr,
32 		struct mali_vma_node *node)
33 {
34 	struct rb_node **iter = &mgr->allocation_mgr_rb.rb_node;
35 	struct rb_node *parent = NULL;
36 	struct mali_vma_node *iter_node;
37 
38 	while (likely(*iter)) {
39 		parent = *iter;
40 		iter_node = rb_entry(*iter, struct mali_vma_node, vm_rb);
41 
42 		if (node->vm_node.start < iter_node->vm_node.start)
43 			iter = &(*iter)->rb_left;
44 		else if (node->vm_node.start > iter_node->vm_node.start)
45 			iter = &(*iter)->rb_right;
46 		else
47 			MALI_DEBUG_ASSERT(0);
48 	}
49 
50 	rb_link_node(&node->vm_rb, parent, iter);
51 	rb_insert_color(&node->vm_rb, &mgr->allocation_mgr_rb);
52 }
53 
54 /**
55  * mali_vma_offset_add() - Add offset node to RB Tree
56  */
mali_vma_offset_add(struct mali_allocation_manager * mgr,struct mali_vma_node * node)57 int mali_vma_offset_add(struct mali_allocation_manager *mgr,
58 			struct mali_vma_node *node)
59 {
60 	int ret = 0;
61 	write_lock(&mgr->vm_lock);
62 
63 	if (node->vm_node.allocated) {
64 		goto out;
65 	}
66 
67 	_mali_vma_offset_add_rb(mgr, node);
68 	/* set to allocated */
69 	node->vm_node.allocated = 1;
70 
71 out:
72 	write_unlock(&mgr->vm_lock);
73 	return ret;
74 }
75 
76 /**
77  * mali_vma_offset_remove() - Remove offset node from RB tree
78  */
mali_vma_offset_remove(struct mali_allocation_manager * mgr,struct mali_vma_node * node)79 void mali_vma_offset_remove(struct mali_allocation_manager *mgr,
80 			    struct mali_vma_node *node)
81 {
82 	write_lock(&mgr->vm_lock);
83 
84 	if (node->vm_node.allocated) {
85 		rb_erase(&node->vm_rb, &mgr->allocation_mgr_rb);
86 		memset(&node->vm_node, 0, sizeof(node->vm_node));
87 	}
88 	write_unlock(&mgr->vm_lock);
89 }
90 
91 /**
92 * mali_vma_offset_search - Search the node in RB tree
93 */
mali_vma_offset_search(struct mali_allocation_manager * mgr,unsigned long start,unsigned long pages)94 struct mali_vma_node *mali_vma_offset_search(struct mali_allocation_manager *mgr,
95 		unsigned long start, unsigned long pages)
96 {
97 	struct mali_vma_node *node, *best;
98 	struct rb_node *iter;
99 	unsigned long offset;
100 	read_lock(&mgr->vm_lock);
101 
102 	iter = mgr->allocation_mgr_rb.rb_node;
103 	best = NULL;
104 
105 	while (likely(iter)) {
106 		node = rb_entry(iter, struct mali_vma_node, vm_rb);
107 		offset = node->vm_node.start;
108 		if (start >= offset) {
109 			iter = iter->rb_right;
110 			best = node;
111 			if (start == offset)
112 				break;
113 		} else {
114 			iter = iter->rb_left;
115 		}
116 	}
117 
118 	if (best) {
119 		offset = best->vm_node.start + best->vm_node.size;
120 		if (offset <= start + pages)
121 			best = NULL;
122 	}
123 	read_unlock(&mgr->vm_lock);
124 
125 	return best;
126 }
127 
128