shr_allocator.h (7576B)
1 /* 2 * 3 * This license is set out in https://raw.githubusercontent.com/Broadcom-Network-Switching-Software/OpenBCM/master/Legal/LICENSE file. 4 * 5 * Copyright 2007-2019 Broadcom Inc. All rights reserved. 6 * 7 * File: allocator.h 8 * Purpose: Internal routines to the BCM library for allocating 9 * gu2 resources. 10 */ 11 12 #ifndef _SHR_ALLOCATOR_H_ 13 #define _SHR_ALLOCATOR_H_ 14 15 #include <sal/types.h> 16 #include <sal/core/alloc.h> 17 #include <shared/idxres_afl.h> 18 #include <shared/idxres_fl.h> 19 #include <shared/hash_tbl.h> 20 21 22 #define _SHR_RES_FLAGS_CONTIGOUS 0x1 23 #define _SHR_RES_FLAGS_RESERVE 0x2 24 25 #define SHR_ALLOC_STYLE_NONE 0 26 #define SHR_ALLOC_STYLE_SINGLE 1 27 #define SHR_ALLOC_STYLE_SCALED 2 28 #define SHR_ALLOC_STYLE_VERSATILE 3 29 30 #define SHR_DYNAMIC_BASE_ADDR ~0 31 #define SHR_DYNAMIC_MAX_COUNT ~0 32 #define SHR_DYNAMIC_SIZE ~0 33 34 /* 35 * Resource types visible to the user 36 */ 37 typedef enum { 38 SHR_USR_RES_RES0, 39 SHR_USR_RES_RES1, 40 SHR_USR_RES_RES2, 41 SHR_USR_RES_RES3, 42 SHR_USR_RES_RES4, 43 SHR_USR_RES_RES5, 44 SHR_USR_RES_RES6, 45 SHR_USR_RES_RES7, 46 SHR_USR_RES_RES8, 47 SHR_USR_RES_RES9, 48 SHR_USR_RES_RES10, 49 SHR_USR_RES_RES11, 50 SHR_USR_RES_RES12, 51 SHR_USR_RES_RES13, 52 SHR_USR_RES_RES14, 53 SHR_USR_RES_RES15, 54 SHR_USR_RES_RES16, 55 SHR_USR_RES_RES17, 56 SHR_USR_RES_RES18, 57 SHR_USR_RES_RES19, 58 SHR_USR_RES_RES20, 59 SHR_USR_RES_MAX 60 } _shr_usr_res_types_t; 61 62 typedef enum { 63 SHR_HW_RES_RES0, 64 SHR_HW_RES_RES1, 65 SHR_HW_RES_RES2, 66 SHR_HW_RES_RES3, 67 SHR_HW_RES_RES4, 68 SHR_HW_RES_RES5, 69 SHR_HW_RES_RES6, 70 SHR_HW_RES_RES7, 71 SHR_HW_RES_RES8, 72 SHR_HW_RES_RES9, 73 SHR_HW_RES_RES10, 74 SHR_HW_RES_RES11, 75 SHR_HW_RES_RES12, 76 SHR_HW_RES_RES13, 77 SHR_HW_RES_RES14, 78 SHR_HW_RES_MAX 79 } _shr_hw_res_t; 80 81 typedef union shr_allocator_idxres_list_handle_u { 82 shr_aidxres_list_handle_t handle_aidx; 83 shr_idxres_list_handle_t handle_idx; 84 } shr_allocator_idxres_list_handle_t; 85 86 typedef struct { 87 uint32 alloc_style; 88 shr_allocator_idxres_list_handle_t u; 89 } _shr_res_handle_t; 90 #define aidx_handle u.handle_aidx 91 #define idx_handle u.handle_idx 92 93 94 95 typedef struct { 96 uint32 mask; 97 char *name; 98 } _shr_hw_table_attrs_t; 99 100 /* convert from external representation to internal */ 101 typedef uint32 (*_shr_ext_to_intl_f)(int unit, uint32 ext); 102 /* convert from internal representation to external */ 103 typedef uint32 (*_shr_intl_to_ext_f)(int unit, uint32 intl); 104 105 106 typedef struct { 107 uint32 alloc_style; 108 uint32 base; 109 uint32 max_count; 110 uint32 blocking_factor; /* max possible block in aidx */ 111 uint32 scaling_factor; 112 char *name; 113 uint32 tables; 114 uint32 reservedHigh; 115 uint32 reservedLow; 116 _shr_ext_to_intl_f e2i; 117 _shr_intl_to_ext_f i2e; 118 } _shr_hw_res_attrs_t; 119 120 121 122 /* 123 * Function 124 * _shr_resource_init 125 * Purpose 126 * Initialize the shr resource manager for all HW resources 127 * for the unit 128 * Parameters 129 * (IN) unit : unit number of the device 130 * Returns 131 * BCM_E_NONE all resources are successfully initialized 132 * BCM_E_* as appropriate otherwise 133 * Notes 134 * Returns error is any of the resources cannot be initialized 135 */ 136 extern int 137 _shr_resource_init(int unit); 138 139 140 /* 141 * Function 142 * _shr_resource_alloc 143 * Purpose 144 * Allocate "count" number of resources. 145 * Parameters 146 * unit - (IN) unit number of the device 147 * type - (IN) resource type 148 * (one of _shr_usr_res_types_t) 149 * count - (IN) Number of resources required 150 * elements- (OUT) Resource Index return by the underlying allocator 151 * flags - (IN) _SHR_RES_FLAGS_CONTIGOUS 152 * if the resources need to be contigous 153 * Returns 154 * BCM_E_NONE if element allocated successfully 155 * BCM_E_* as appropriate otherwise 156 */ 157 extern int 158 _shr_resource_alloc(int unit, 159 _shr_usr_res_types_t type, 160 uint32 count, 161 uint32 *elements, 162 uint32 flags); 163 164 /* 165 * Function 166 * _shr_resource_free 167 * Purpose 168 * Free the resources specified in the array 169 * Parameters 170 * unit - (IN) unit number of the device 171 * type - (IN) resource type 172 * (one of _shr_usr_res_types_t) 173 * count - (IN) Number of resources to be freed 174 * elements- (IN) Resource Ids to be freed 175 * flags - (IN) _SHR_RES_FLAGS_CONTIGOUS 176 * Returns 177 * BCM_E_NONE if element freed successfully 178 * BCM_E_* as appropriate otherwise 179 * NOTE: 180 * In case of block allocations, the user must pass the 181 * array back. Just the base element will not do. 182 * i.e. the array must be 'count' elements long 183 */ 184 extern int 185 _shr_resource_free(int unit, 186 _shr_usr_res_types_t type, 187 uint32 count, 188 uint32 *elements, 189 uint32 flags); 190 /* 191 * Function 192 * _shr_resource_test 193 * Purpose 194 * Check to see if the specified resource is available 195 * Parameters 196 * unit - (IN) unit number of the device 197 * type - (IN) resource type 198 * (one of _shr_usr_res_types_t) 199 * element - (IN) Resource Id to be checked 200 * Returns 201 * BCM_E_NOT_FOUND if resource is available 202 * BCM_E_EXIST if resource is in use 203 * BCM_E_* as appropriate otherwise 204 */ 205 extern int 206 _shr_resource_test(int unit, 207 _shr_usr_res_types_t type, 208 uint32 element); 209 /* 210 * Function 211 * _shr_shr_resource_uninit 212 * Purpose 213 * Destroy the all the resource managers for this unit 214 * Parameters 215 * (IN) unit : unit number of the device 216 * Returns 217 * BCM_E_NONE all resources are successfully destroyed 218 * BCM_E_* as appropriate otherwise 219 * Notes 220 * Returns error is any of the resources cannot be destroyed 221 */ 222 extern int 223 _shr_resource_uninit(int unit); 224 225 226 /* 227 * Function 228 * _shr_alloc_range_get 229 * Purpose 230 * Retrieve the range of valid IDs for the given resource 231 * 232 * Parameters 233 * (IN) unit : unit number of the device 234 * (OUT) first : first valid ID 235 * (OUT) last : last valid ID 236 * Returns 237 * BCM_E_NONE - All required resources are allocated 238 * BCM_E_* - failure 239 * Notes 240 */ 241 extern int 242 _shr_alloc_range_get(int unit, _shr_usr_res_types_t type, 243 uint32 *first, uint32 *last); 244 245 /* 246 * Function 247 * _shr_alloc_range_get 248 * Purpose 249 * Retrieve the range of valid IDs for the given resource 250 * 251 * Parameters 252 * (IN) unit : unit number of the device 253 * (OUT) first : first valid ID 254 * (OUT) last : last valid ID 255 * Returns 256 * BCM_E_NONE - All required resources are allocated 257 * BCM_E_* - failure 258 * Notes 259 */ 260 extern int 261 _shr_range_reserve(int unit, _shr_usr_res_types_t type, 262 int highOrLow, uint32 val); 263 264 #endif /* _SHR_ALLOCATOR_H_ */