1 /****************************************************************************** 2 * 3 * Copyright (C) 1999-2012 Broadcom Corporation 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 #ifndef GKI_COMMON_H 19 #define GKI_COMMON_H 20 21 #include "gki.h" 22 #include "dyn_mem.h" 23 24 /* Task States: (For OSRdyTbl) */ 25 #define TASK_DEAD 0 /* b0000 */ 26 #define TASK_READY 1 /* b0001 */ 27 #define TASK_WAIT 2 /* b0010 */ 28 #define TASK_DELAY 4 /* b0100 */ 29 #define TASK_SUSPEND 8 /* b1000 */ 30 31 32 /******************************************************************** 33 ** Internal Error codes 34 *********************************************************************/ 35 #define GKI_ERROR_BUF_CORRUPTED 0xFFFF 36 #define GKI_ERROR_NOT_BUF_OWNER 0xFFFE 37 #define GKI_ERROR_FREEBUF_BAD_QID 0xFFFD 38 #define GKI_ERROR_FREEBUF_BUF_LINKED 0xFFFC 39 #define GKI_ERROR_SEND_MSG_BAD_DEST 0xFFFB 40 #define GKI_ERROR_SEND_MSG_BUF_LINKED 0xFFFA 41 #define GKI_ERROR_ENQUEUE_BUF_LINKED 0xFFF9 42 #define GKI_ERROR_DELETE_POOL_BAD_QID 0xFFF8 43 #define GKI_ERROR_BUF_SIZE_TOOBIG 0xFFF7 44 #define GKI_ERROR_BUF_SIZE_ZERO 0xFFF6 45 #define GKI_ERROR_ADDR_NOT_IN_BUF 0xFFF5 46 #define GKI_ERROR_OUT_OF_BUFFERS 0xFFF4 47 #define GKI_ERROR_GETPOOLBUF_BAD_QID 0xFFF3 48 49 50 /******************************************************************** 51 ** Misc constants 52 *********************************************************************/ 53 54 #define GKI_MAX_INT32 (0x7fffffffL) 55 #define GKI_MAX_TIMESTAMP (0xffffffffL) 56 57 /******************************************************************** 58 ** Buffer Management Data Structures 59 *********************************************************************/ 60 61 typedef struct _buffer_hdr 62 { 63 struct _buffer_hdr *p_next; /* next buffer in the queue */ 64 UINT8 q_id; /* id of the queue */ 65 UINT8 task_id; /* task which allocated the buffer*/ 66 UINT8 status; /* FREE, UNLINKED or QUEUED */ 67 UINT8 Type; 68 } BUFFER_HDR_T; 69 70 typedef struct _free_queue 71 { 72 BUFFER_HDR_T *p_first; /* first buffer in the queue */ 73 BUFFER_HDR_T *p_last; /* last buffer in the queue */ 74 UINT16 size; /* size of the buffers in the pool */ 75 UINT16 total; /* toatal number of buffers */ 76 UINT16 cur_cnt; /* number of buffers currently allocated */ 77 UINT16 max_cnt; /* maximum number of buffers allocated at any time */ 78 } FREE_QUEUE_T; 79 80 81 /* Buffer related defines 82 */ 83 #define ALIGN_POOL(pl_size) ( (((pl_size) + 3) / sizeof(UINT32)) * sizeof(UINT32)) 84 #define BUFFER_HDR_SIZE (sizeof(BUFFER_HDR_T)) /* Offset past header */ 85 #define BUFFER_PADDING_SIZE (sizeof(BUFFER_HDR_T) + sizeof(UINT32)) /* Header + Magic Number */ 86 #define MAX_USER_BUF_SIZE ((UINT16)0xffff - BUFFER_PADDING_SIZE) /* pool size must allow for header */ 87 #define MAGIC_NO 0xDDBADDBA 88 89 #define BUF_STATUS_FREE 0 90 #define BUF_STATUS_UNLINKED 1 91 #define BUF_STATUS_QUEUED 2 92 93 // btla-specific ++ 94 #define GKI_USE_DEFERED_ALLOC_BUF_POOLS 95 // btla-specific -- 96 97 /* Exception related structures (Used in debug mode only) 98 */ 99 #if (GKI_DEBUG == TRUE) 100 typedef struct 101 { 102 UINT16 type; 103 UINT8 taskid; 104 UINT8 msg[GKI_MAX_EXCEPTION_MSGLEN]; 105 } EXCEPTION_T; 106 #endif 107 108 109 /* Put all GKI variables into one control block 110 */ 111 typedef struct 112 { 113 /* Task management variables 114 */ 115 /* The stack and stack size are not used on Windows 116 */ 117 // btla-specific ++ 118 #if (!defined GKI_USE_DEFERED_ALLOC_BUF_POOLS && (GKI_USE_DYNAMIC_BUFFERS == FALSE)) 119 // btla-specific -- 120 121 #if (GKI_NUM_FIXED_BUF_POOLS > 0) 122 UINT8 bufpool0[(ALIGN_POOL(GKI_BUF0_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF0_MAX]; 123 #endif 124 125 #if (GKI_NUM_FIXED_BUF_POOLS > 1) 126 UINT8 bufpool1[(ALIGN_POOL(GKI_BUF1_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF1_MAX]; 127 #endif 128 129 #if (GKI_NUM_FIXED_BUF_POOLS > 2) 130 UINT8 bufpool2[(ALIGN_POOL(GKI_BUF2_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF2_MAX]; 131 #endif 132 133 #if (GKI_NUM_FIXED_BUF_POOLS > 3) 134 UINT8 bufpool3[(ALIGN_POOL(GKI_BUF3_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF3_MAX]; 135 #endif 136 137 #if (GKI_NUM_FIXED_BUF_POOLS > 4) 138 UINT8 bufpool4[(ALIGN_POOL(GKI_BUF4_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF4_MAX]; 139 #endif 140 141 #if (GKI_NUM_FIXED_BUF_POOLS > 5) 142 UINT8 bufpool5[(ALIGN_POOL(GKI_BUF5_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF5_MAX]; 143 #endif 144 145 #if (GKI_NUM_FIXED_BUF_POOLS > 6) 146 UINT8 bufpool6[(ALIGN_POOL(GKI_BUF6_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF6_MAX]; 147 #endif 148 149 #if (GKI_NUM_FIXED_BUF_POOLS > 7) 150 UINT8 bufpool7[(ALIGN_POOL(GKI_BUF7_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF7_MAX]; 151 #endif 152 153 #if (GKI_NUM_FIXED_BUF_POOLS > 8) 154 UINT8 bufpool8[(ALIGN_POOL(GKI_BUF8_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF8_MAX]; 155 #endif 156 157 #if (GKI_NUM_FIXED_BUF_POOLS > 9) 158 UINT8 bufpool9[(ALIGN_POOL(GKI_BUF9_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF9_MAX]; 159 #endif 160 161 #if (GKI_NUM_FIXED_BUF_POOLS > 10) 162 UINT8 bufpool10[(ALIGN_POOL(GKI_BUF10_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF10_MAX]; 163 #endif 164 165 #if (GKI_NUM_FIXED_BUF_POOLS > 11) 166 UINT8 bufpool11[(ALIGN_POOL(GKI_BUF11_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF11_MAX]; 167 #endif 168 169 #if (GKI_NUM_FIXED_BUF_POOLS > 12) 170 UINT8 bufpool12[(ALIGN_POOL(GKI_BUF12_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF12_MAX]; 171 #endif 172 173 #if (GKI_NUM_FIXED_BUF_POOLS > 13) 174 UINT8 bufpool13[(ALIGN_POOL(GKI_BUF13_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF13_MAX]; 175 #endif 176 177 #if (GKI_NUM_FIXED_BUF_POOLS > 14) 178 UINT8 bufpool14[(ALIGN_POOL(GKI_BUF14_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF14_MAX]; 179 #endif 180 181 #if (GKI_NUM_FIXED_BUF_POOLS > 15) 182 UINT8 bufpool15[(ALIGN_POOL(GKI_BUF15_SIZE) + BUFFER_PADDING_SIZE) * GKI_BUF15_MAX]; 183 #endif 184 185 #else 186 /* Definitions for dynamic buffer use */ 187 #if (GKI_NUM_FIXED_BUF_POOLS > 0) 188 UINT8 *bufpool0; 189 #endif 190 191 #if (GKI_NUM_FIXED_BUF_POOLS > 1) 192 UINT8 *bufpool1; 193 #endif 194 195 #if (GKI_NUM_FIXED_BUF_POOLS > 2) 196 UINT8 *bufpool2; 197 #endif 198 199 #if (GKI_NUM_FIXED_BUF_POOLS > 3) 200 UINT8 *bufpool3; 201 #endif 202 203 #if (GKI_NUM_FIXED_BUF_POOLS > 4) 204 UINT8 *bufpool4; 205 #endif 206 207 #if (GKI_NUM_FIXED_BUF_POOLS > 5) 208 UINT8 *bufpool5; 209 #endif 210 211 #if (GKI_NUM_FIXED_BUF_POOLS > 6) 212 UINT8 *bufpool6; 213 #endif 214 215 #if (GKI_NUM_FIXED_BUF_POOLS > 7) 216 UINT8 *bufpool7; 217 #endif 218 219 #if (GKI_NUM_FIXED_BUF_POOLS > 8) 220 UINT8 *bufpool8; 221 #endif 222 223 #if (GKI_NUM_FIXED_BUF_POOLS > 9) 224 UINT8 *bufpool9; 225 #endif 226 227 #if (GKI_NUM_FIXED_BUF_POOLS > 10) 228 UINT8 *bufpool10; 229 #endif 230 231 #if (GKI_NUM_FIXED_BUF_POOLS > 11) 232 UINT8 *bufpool11; 233 #endif 234 235 #if (GKI_NUM_FIXED_BUF_POOLS > 12) 236 UINT8 *bufpool12; 237 #endif 238 239 #if (GKI_NUM_FIXED_BUF_POOLS > 13) 240 UINT8 *bufpool13; 241 #endif 242 243 #if (GKI_NUM_FIXED_BUF_POOLS > 14) 244 UINT8 *bufpool14; 245 #endif 246 247 #if (GKI_NUM_FIXED_BUF_POOLS > 15) 248 UINT8 *bufpool15; 249 #endif 250 251 #endif 252 253 UINT8 *OSStack[GKI_MAX_TASKS]; /* pointer to beginning of stack */ 254 UINT16 OSStackSize[GKI_MAX_TASKS]; /* stack size available to each task */ 255 256 257 INT8 *OSTName[GKI_MAX_TASKS]; /* name of the task */ 258 259 UINT8 OSRdyTbl[GKI_MAX_TASKS]; /* current state of the task */ 260 UINT16 OSWaitEvt[GKI_MAX_TASKS]; /* events that have to be processed by the task */ 261 UINT16 OSWaitForEvt[GKI_MAX_TASKS]; /* events the task is waiting for*/ 262 263 UINT32 OSTicks; /* system ticks from start */ 264 UINT32 OSIdleCnt; /* idle counter */ 265 INT16 OSDisableNesting; /* counter to keep track of interrupt disable nesting */ 266 INT16 OSLockNesting; /* counter to keep track of sched lock nesting */ 267 INT16 OSIntNesting; /* counter to keep track of interrupt nesting */ 268 269 /* Timer related variables 270 */ 271 INT32 OSTicksTilExp; /* Number of ticks till next timer expires */ 272 #if (defined(GKI_DELAY_STOP_SYS_TICK) && (GKI_DELAY_STOP_SYS_TICK > 0)) 273 UINT32 OSTicksTilStop; /* inactivity delay timer; OS Ticks till stopping system tick */ 274 #endif 275 INT32 OSNumOrigTicks; /* Number of ticks between last timer expiration to the next one */ 276 277 INT32 OSWaitTmr [GKI_MAX_TASKS]; /* ticks the task has to wait, for specific events */ 278 279 /* Only take up space timers used in the system (GKI_NUM_TIMERS defined in target.h) */ 280 #if (GKI_NUM_TIMERS > 0) 281 INT32 OSTaskTmr0 [GKI_MAX_TASKS]; 282 INT32 OSTaskTmr0R [GKI_MAX_TASKS]; 283 #endif 284 285 #if (GKI_NUM_TIMERS > 1) 286 INT32 OSTaskTmr1 [GKI_MAX_TASKS]; 287 INT32 OSTaskTmr1R [GKI_MAX_TASKS]; 288 #endif 289 290 #if (GKI_NUM_TIMERS > 2) 291 INT32 OSTaskTmr2 [GKI_MAX_TASKS]; 292 INT32 OSTaskTmr2R [GKI_MAX_TASKS]; 293 #endif 294 295 #if (GKI_NUM_TIMERS > 3) 296 INT32 OSTaskTmr3 [GKI_MAX_TASKS]; 297 INT32 OSTaskTmr3R [GKI_MAX_TASKS]; 298 #endif 299 300 301 302 /* Buffer related variables 303 */ 304 BUFFER_HDR_T *OSTaskQFirst[GKI_MAX_TASKS][NUM_TASK_MBOX]; /* array of pointers to the first event in the task mailbox */ 305 BUFFER_HDR_T *OSTaskQLast [GKI_MAX_TASKS][NUM_TASK_MBOX]; /* array of pointers to the last event in the task mailbox */ 306 307 /* Define the buffer pool management variables 308 */ 309 FREE_QUEUE_T freeq[GKI_NUM_TOTAL_BUF_POOLS]; 310 311 UINT16 pool_buf_size[GKI_NUM_TOTAL_BUF_POOLS]; 312 UINT16 pool_max_count[GKI_NUM_TOTAL_BUF_POOLS]; 313 UINT16 pool_additions[GKI_NUM_TOTAL_BUF_POOLS]; 314 315 /* Define the buffer pool start addresses 316 */ 317 UINT8 *pool_start[GKI_NUM_TOTAL_BUF_POOLS]; /* array of pointers to the start of each buffer pool */ 318 UINT8 *pool_end[GKI_NUM_TOTAL_BUF_POOLS]; /* array of pointers to the end of each buffer pool */ 319 UINT16 pool_size[GKI_NUM_TOTAL_BUF_POOLS]; /* actual size of the buffers in a pool */ 320 321 /* Define the buffer pool access control variables */ 322 void *p_user_mempool; /* User O/S memory pool */ 323 UINT16 pool_access_mask; /* Bits are set if the corresponding buffer pool is a restricted pool */ 324 UINT8 pool_list[GKI_NUM_TOTAL_BUF_POOLS]; /* buffer pools arranged in the order of size */ 325 UINT8 curr_total_no_of_pools; /* number of fixed buf pools + current number of dynamic pools */ 326 327 BOOLEAN timer_nesting; /* flag to prevent timer interrupt nesting */ 328 329 /* Time queue arrays */ 330 TIMER_LIST_Q *timer_queues[GKI_MAX_TIMER_QUEUES]; 331 /* System tick callback */ 332 SYSTEM_TICK_CBACK *p_tick_cb; 333 BOOLEAN system_tick_running; /* TRUE if system tick is running. Valid only if p_tick_cb is not NULL */ 334 335 #if (GKI_DEBUG == TRUE) 336 UINT16 ExceptionCnt; /* number of GKI exceptions that have happened */ 337 EXCEPTION_T Exception[GKI_MAX_EXCEPTION]; 338 #endif 339 340 } tGKI_COM_CB; 341 342 343 #ifdef __cplusplus 344 extern "C" { 345 #endif 346 347 /* Internal GKI function prototypes 348 */ 349 GKI_API extern BOOLEAN gki_chk_buf_damage(void *); 350 extern BOOLEAN gki_chk_buf_owner(void *); 351 extern void gki_buffer_init (void); 352 extern void gki_timers_init(void); 353 extern void gki_adjust_timer_count (INT32); 354 355 #ifdef GKI_USE_DEFERED_ALLOC_BUF_POOLS 356 extern void gki_dealloc_free_queue(void); 357 #endif 358 359 extern void OSStartRdy(void); 360 extern void OSCtxSw(void); 361 extern void OSIntCtxSw(void); 362 extern void OSSched(void); 363 extern void OSIntEnter(void); 364 extern void OSIntExit(void); 365 366 367 /* Debug aids 368 */ 369 typedef void (*FP_PRINT)(char *, ...); 370 371 #if (GKI_DEBUG == TRUE) 372 373 typedef void (*PKT_PRINT)(UINT8 *, UINT16); 374 375 extern void gki_print_task(FP_PRINT); 376 extern void gki_print_exception(FP_PRINT); 377 extern void gki_print_timer(FP_PRINT); 378 extern void gki_print_stack(FP_PRINT); 379 extern void gki_print_buffer(FP_PRINT); 380 extern void gki_print_buffer_statistics(FP_PRINT, INT16); 381 GKI_API extern void gki_print_used_bufs (FP_PRINT, UINT8); 382 extern void gki_dump(UINT8 *, UINT16, FP_PRINT); 383 extern void gki_dump2(UINT16 *, UINT16, FP_PRINT); 384 extern void gki_dump4(UINT32 *, UINT16, FP_PRINT); 385 386 #endif 387 388 #ifdef __cplusplus 389 } 390 #endif 391 392 #endif 393