2 * Copyright (C) 2010-2011, 2013-2014, 2016 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.
11 #include "mali_kernel_common.h"
14 #include "ump_kernel_common.h"
15 #include "ump_kernel_types.h"
16 #include "ump_kernel_random_mapping.h"
18 #include <linux/random.h>
19 #include <linux/rbtree.h>
20 #include <linux/sched.h>
21 #include <linux/jiffies.h>
24 static ump_dd_mem *search(struct rb_root *root, int id)
26 struct rb_node *node = root->rb_node;
29 ump_dd_mem *e = container_of(node, ump_dd_mem, node);
31 if (id < e->secure_id) {
33 } else if (id > e->secure_id) {
34 node = node->rb_right;
43 static mali_bool insert(struct rb_root *root, int id, ump_dd_mem *mem)
45 struct rb_node **new = &(root->rb_node);
46 struct rb_node *parent = NULL;
49 ump_dd_mem *this = container_of(*new, ump_dd_mem, node);
52 if (id < this->secure_id) {
53 new = &((*new)->rb_left);
54 } else if (id > this->secure_id) {
55 new = &((*new)->rb_right);
57 printk(KERN_ERR "UMP: ID already used %x\n", id);
62 rb_link_node(&mem->node, parent, new);
63 rb_insert_color(&mem->node, root);
69 ump_random_mapping *ump_random_mapping_create(void)
71 ump_random_mapping *map = _mali_osk_calloc(1, sizeof(ump_random_mapping));
76 map->lock = _mali_osk_mutex_rw_init(_MALI_OSK_LOCKFLAG_ORDERED,
77 _MALI_OSK_LOCK_ORDER_DESCRIPTOR_MAP);
78 if (NULL != map->lock) {
80 #if UMP_RANDOM_MAP_DELAY
81 map->failed.count = 0;
82 map->failed.timestamp = jiffies;
89 void ump_random_mapping_destroy(ump_random_mapping *map)
91 _mali_osk_mutex_rw_term(map->lock);
95 int ump_random_mapping_insert(ump_random_mapping *map, ump_dd_mem *mem)
97 _mali_osk_mutex_rw_wait(map->lock, _MALI_OSK_LOCKMODE_RW);
102 get_random_bytes(&id, sizeof(id));
104 /* Try a new random number if id happened to be the invalid
106 if (unlikely(id == UMP_INVALID_SECURE_ID))
109 /* Insert into the tree. If the id was already in use, get a
110 * new random id and try again. */
111 if (insert(&map->root, id, mem)) {
116 _mali_osk_mutex_rw_signal(map->lock, _MALI_OSK_LOCKMODE_RW);
121 ump_dd_mem *ump_random_mapping_get(ump_random_mapping *map, int id)
123 ump_dd_mem *mem = NULL;
124 #if UMP_RANDOM_MAP_DELAY
130 _mali_osk_mutex_rw_wait(map->lock, _MALI_OSK_LOCKMODE_RO);
131 mem = search(&map->root, id);
133 if (unlikely(NULL == mem)) {
134 #if UMP_RANDOM_MAP_DELAY
137 if (time_is_before_jiffies(map->failed.timestamp +
138 UMP_FAILED_LOOKUP_DELAY * HZ)) {
139 /* If it is a long time since last failure, reset
140 * the counter and skip the delay this time. */
141 map->failed.count = 0;
142 } else if (map->failed.count > UMP_FAILED_LOOKUPS_ALLOWED) {
146 map->failed.timestamp = jiffies;
147 #endif /* UMP_RANDOM_MAP_DELAY */
149 ump_dd_reference_add(mem);
151 _mali_osk_mutex_rw_signal(map->lock, _MALI_OSK_LOCKMODE_RO);
153 #if UMP_RANDOM_MAP_DELAY
156 schedule_timeout_killable(UMP_FAILED_LOOKUP_DELAY);
158 #endif /* UMP_RANDOM_MAP_DELAY */
163 static ump_dd_mem *ump_random_mapping_remove_internal(ump_random_mapping *map, int id)
165 ump_dd_mem *mem = NULL;
167 mem = search(&map->root, id);
170 rb_erase(&mem->node, &map->root);
176 void ump_random_mapping_put(ump_dd_mem *mem)
180 _mali_osk_mutex_rw_wait(device.secure_id_map->lock, _MALI_OSK_LOCKMODE_RW);
182 new_ref = _ump_osk_atomic_dec_and_read(&mem->ref_count);
183 DBG_MSG(5, ("Memory reference decremented. ID: %u, new value: %d\n",
184 mem->secure_id, new_ref));
187 DBG_MSG(3, ("Final release of memory. ID: %u\n", mem->secure_id));
189 #ifdef CONFIG_DMA_SHARED_BUFFER
190 if (mem->import_attach) {
191 struct dma_buf_attachment *attach = mem->import_attach;
192 struct dma_buf *dma_buf;
195 dma_buf_unmap_attachment(attach, mem->sgt,
198 dma_buf = attach->dmabuf;
199 dma_buf_detach(attach->dmabuf, attach);
200 dma_buf_put(dma_buf);
204 ump_random_mapping_remove_internal(device.secure_id_map, mem->secure_id);
206 mem->release_func(mem->ctx, mem);
210 _mali_osk_mutex_rw_signal(device.secure_id_map->lock, _MALI_OSK_LOCKMODE_RW);
213 ump_dd_mem *ump_random_mapping_remove(ump_random_mapping *map, int descriptor)
217 _mali_osk_mutex_rw_wait(map->lock, _MALI_OSK_LOCKMODE_RW);
218 mem = ump_random_mapping_remove_internal(map, descriptor);
219 _mali_osk_mutex_rw_signal(map->lock, _MALI_OSK_LOCKMODE_RW);