1 /* 2 * Copyright (C) 2010-2011, 2013-2014, 2016-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 /** 12 * @file ump_kernel_random_mapping.h 13 */ 14 15 #ifndef __UMP_KERNEL_RANDOM_MAPPING_H__ 16 #define __UMP_KERNEL_RANDOM_MAPPING_H__ 17 18 #include "mali_osk.h" 19 #include <linux/rbtree.h> 20 21 #define UMP_RANDOM_MAP_DELAY 1 22 #define UMP_FAILED_LOOKUP_DELAY 10 /* ms */ 23 #define UMP_FAILED_LOOKUPS_ALLOWED 10 /* number of allowed failed lookups */ 24 25 /** 26 * The random mapping object 27 * Provides a separate namespace where we can map an integer to a pointer 28 */ 29 typedef struct ump_random_mapping { 30 _mali_osk_mutex_rw_t *lock; /**< Lock protecting access to the mapping object */ 31 struct rb_root root; 32 #if UMP_RANDOM_MAP_DELAY 33 struct { 34 unsigned long count; 35 unsigned long timestamp; 36 } failed; 37 #endif 38 } ump_random_mapping; 39 40 /** 41 * Create a random mapping object 42 * Create a random mapping capable of holding 2^20 entries 43 * @return Pointer to a random mapping object, NULL on failure 44 */ 45 ump_random_mapping *ump_random_mapping_create(void); 46 47 /** 48 * Destroy a random mapping object 49 * @param map The map to free 50 */ 51 void ump_random_mapping_destroy(ump_random_mapping *map); 52 53 /** 54 * Allocate a new mapping entry (random ID) 55 * Allocates a new entry in the map. 56 * @param map The map to allocate a new entry in 57 * @param target The value to map to 58 * @return The random allocated, a negative value on error 59 */ 60 int ump_random_mapping_insert(ump_random_mapping *map, ump_dd_mem *mem); 61 62 /** 63 * Get the value mapped to by a random ID 64 * 65 * If the lookup fails, punish the calling thread by applying a delay. 66 * 67 * @param map The map to lookup the random id in 68 * @param id The ID to lookup 69 * @param target Pointer to a pointer which will receive the stored value 70 * @return ump_dd_mem pointer on successful lookup, NULL on error 71 */ 72 ump_dd_mem *ump_random_mapping_get(ump_random_mapping *map, int id); 73 74 void ump_random_mapping_put(ump_dd_mem *mem); 75 76 /** 77 * Free the random ID 78 * For the random to be reused it has to be freed 79 * @param map The map to free the random from 80 * @param id The ID to free 81 */ 82 ump_dd_mem *ump_random_mapping_remove(ump_random_mapping *map, int id); 83 84 #endif /* __UMP_KERNEL_RANDOM_MAPPING_H__ */ 85