2 * Copyright (C) 2010, 2012 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"
12 #include "mali_kernel_descriptor_mapping.h"
14 #include "mali_osk_bitops.h"
16 #define MALI_PAD_INT(x) (((x) + (BITS_PER_LONG - 1)) & ~(BITS_PER_LONG - 1))
19 * Allocate a descriptor table capable of holding 'count' mappings
20 * @param count Number of mappings in the table
21 * @return Pointer to a new table, NULL on error
23 static mali_descriptor_table * descriptor_table_alloc(int count);
26 * Free a descriptor table
27 * @param table The table to free
29 static void descriptor_table_free(mali_descriptor_table * table);
31 mali_descriptor_mapping * mali_descriptor_mapping_create(int init_entries, int max_entries)
33 mali_descriptor_mapping * map = _mali_osk_calloc(1, sizeof(mali_descriptor_mapping));
35 init_entries = MALI_PAD_INT(init_entries);
36 max_entries = MALI_PAD_INT(max_entries);
39 map->table = descriptor_table_alloc(init_entries);
40 if (NULL != map->table) {
41 map->lock = _mali_osk_mutex_rw_init(_MALI_OSK_LOCKFLAG_ORDERED, _MALI_OSK_LOCK_ORDER_DESCRIPTOR_MAP);
42 if (NULL != map->lock) {
43 _mali_osk_set_nonatomic_bit(0, map->table->usage); /* reserve bit 0 to prevent NULL/zero logic to kick in */
44 map->max_nr_mappings_allowed = max_entries;
45 map->current_nr_mappings = init_entries;
48 descriptor_table_free(map->table);
55 void mali_descriptor_mapping_destroy(mali_descriptor_mapping * map)
57 descriptor_table_free(map->table);
58 _mali_osk_mutex_rw_term(map->lock);
62 _mali_osk_errcode_t mali_descriptor_mapping_allocate_mapping(mali_descriptor_mapping * map, void * target, int *odescriptor)
64 _mali_osk_errcode_t err = _MALI_OSK_ERR_FAULT;
67 MALI_DEBUG_ASSERT_POINTER(map);
68 MALI_DEBUG_ASSERT_POINTER(odescriptor);
70 _mali_osk_mutex_rw_wait(map->lock, _MALI_OSK_LOCKMODE_RW);
71 new_descriptor = _mali_osk_find_first_zero_bit(map->table->usage, map->current_nr_mappings);
72 if (new_descriptor == map->current_nr_mappings) {
73 /* no free descriptor, try to expand the table */
74 mali_descriptor_table * new_table, * old_table;
75 if (map->current_nr_mappings >= map->max_nr_mappings_allowed) goto unlock_and_exit;
77 map->current_nr_mappings += BITS_PER_LONG;
78 new_table = descriptor_table_alloc(map->current_nr_mappings);
79 if (NULL == new_table) goto unlock_and_exit;
81 old_table = map->table;
82 _mali_osk_memcpy(new_table->usage, old_table->usage, (sizeof(unsigned long)*map->current_nr_mappings) / BITS_PER_LONG);
83 _mali_osk_memcpy(new_table->mappings, old_table->mappings, map->current_nr_mappings * sizeof(void*));
84 map->table = new_table;
85 descriptor_table_free(old_table);
88 /* we have found a valid descriptor, set the value and usage bit */
89 _mali_osk_set_nonatomic_bit(new_descriptor, map->table->usage);
90 map->table->mappings[new_descriptor] = target;
91 *odescriptor = new_descriptor;
92 err = _MALI_OSK_ERR_OK;
95 _mali_osk_mutex_rw_signal(map->lock, _MALI_OSK_LOCKMODE_RW);
99 void mali_descriptor_mapping_call_for_each(mali_descriptor_mapping * map, void (*callback)(int, void*))
103 MALI_DEBUG_ASSERT_POINTER(map);
104 MALI_DEBUG_ASSERT_POINTER(callback);
106 _mali_osk_mutex_rw_wait(map->lock, _MALI_OSK_LOCKMODE_RO);
107 /* id 0 is skipped as it's an reserved ID not mapping to anything */
108 for (i = 1; i < map->current_nr_mappings; ++i) {
109 if (_mali_osk_test_bit(i, map->table->usage)) {
110 callback(i, map->table->mappings[i]);
113 _mali_osk_mutex_rw_signal(map->lock, _MALI_OSK_LOCKMODE_RO);
116 _mali_osk_errcode_t mali_descriptor_mapping_get(mali_descriptor_mapping * map, int descriptor, void** target)
118 _mali_osk_errcode_t result = _MALI_OSK_ERR_FAULT;
119 MALI_DEBUG_ASSERT_POINTER(map);
120 _mali_osk_mutex_rw_wait(map->lock, _MALI_OSK_LOCKMODE_RO);
121 if ( (descriptor >= 0) && (descriptor < map->current_nr_mappings) && _mali_osk_test_bit(descriptor, map->table->usage) ) {
122 *target = map->table->mappings[descriptor];
123 result = _MALI_OSK_ERR_OK;
124 } else *target = NULL;
125 _mali_osk_mutex_rw_signal(map->lock, _MALI_OSK_LOCKMODE_RO);
129 _mali_osk_errcode_t mali_descriptor_mapping_set(mali_descriptor_mapping * map, int descriptor, void * target)
131 _mali_osk_errcode_t result = _MALI_OSK_ERR_FAULT;
132 _mali_osk_mutex_rw_wait(map->lock, _MALI_OSK_LOCKMODE_RO);
133 if ( (descriptor >= 0) && (descriptor < map->current_nr_mappings) && _mali_osk_test_bit(descriptor, map->table->usage) ) {
134 map->table->mappings[descriptor] = target;
135 result = _MALI_OSK_ERR_OK;
137 _mali_osk_mutex_rw_signal(map->lock, _MALI_OSK_LOCKMODE_RO);
141 void *mali_descriptor_mapping_free(mali_descriptor_mapping * map, int descriptor)
143 void *old_value = NULL;
145 _mali_osk_mutex_rw_wait(map->lock, _MALI_OSK_LOCKMODE_RW);
146 if ( (descriptor >= 0) && (descriptor < map->current_nr_mappings) && _mali_osk_test_bit(descriptor, map->table->usage) ) {
147 old_value = map->table->mappings[descriptor];
148 map->table->mappings[descriptor] = NULL;
149 _mali_osk_clear_nonatomic_bit(descriptor, map->table->usage);
151 _mali_osk_mutex_rw_signal(map->lock, _MALI_OSK_LOCKMODE_RW);
156 static mali_descriptor_table * descriptor_table_alloc(int count)
158 mali_descriptor_table * table;
160 table = _mali_osk_calloc(1, sizeof(mali_descriptor_table) + ((sizeof(unsigned long) * count)/BITS_PER_LONG) + (sizeof(void*) * count));
163 table->usage = (u32*)((u8*)table + sizeof(mali_descriptor_table));
164 table->mappings = (void**)((u8*)table + sizeof(mali_descriptor_table) + ((sizeof(unsigned long) * count)/BITS_PER_LONG));
170 static void descriptor_table_free(mali_descriptor_table * table)
172 _mali_osk_free(table);