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 
23 /* Task States: (For OSRdyTbl) */
24 #define TASK_DEAD 0    /* b0000 */
25 #define TASK_READY 1   /* b0001 */
26 
27 /********************************************************************
28 **  Internal Error codes
29 *********************************************************************/
30 #define GKI_ERROR_BUF_CORRUPTED 0xFFFF
31 #define GKI_ERROR_NOT_BUF_OWNER 0xFFFE
32 #define GKI_ERROR_FREEBUF_BAD_QID 0xFFFD
33 #define GKI_ERROR_FREEBUF_BUF_LINKED 0xFFFC
34 #define GKI_ERROR_SEND_MSG_BAD_DEST 0xFFFB
35 #define GKI_ERROR_SEND_MSG_BUF_LINKED 0xFFFA
36 #define GKI_ERROR_ENQUEUE_BUF_LINKED 0xFFF9
37 #define GKI_ERROR_DELETE_POOL_BAD_QID 0xFFF8
38 #define GKI_ERROR_BUF_SIZE_TOOBIG 0xFFF7
39 #define GKI_ERROR_BUF_SIZE_ZERO 0xFFF6
40 
41 /********************************************************************
42 **  Misc constants
43 *********************************************************************/
44 
45 #define GKI_MAX_INT32 (0x7fffffffL)
46 
47 /********************************************************************
48 **  Buffer Management Data Structures
49 *********************************************************************/
50 
51 typedef struct _buffer_hdr {
52   struct _buffer_hdr* p_next; /* next buffer in the queue */
53   uint8_t q_id;               /* id of the queue */
54   uint8_t task_id;            /* task which allocated the buffer*/
55   uint8_t status;             /* FREE, UNLINKED or QUEUED */
56   uint8_t Type;
57 #if defined(DYN_ALLOC) || defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
58   uint16_t size;
59 #endif
60 } BUFFER_HDR_T;
61 
62 typedef struct _free_queue {
63   BUFFER_HDR_T* p_first; /* first buffer in the queue */
64   BUFFER_HDR_T* p_last;  /* last buffer in the queue */
65   uint16_t size;         /* size of the buffers in the pool */
66   uint16_t total;        /* toatal number of buffers */
67   uint16_t cur_cnt;      /* number of  buffers currently allocated */
68   uint16_t max_cnt;      /* maximum number of buffers allocated at any time */
69 } FREE_QUEUE_T;
70 
71 /* Buffer related defines
72 */
73 #define ALIGN_POOL(pl_size) \
74   ((((pl_size) + 3) / sizeof(uint32_t)) * sizeof(uint32_t))
75 /* Offset past header */
76 #define BUFFER_HDR_SIZE (sizeof(BUFFER_HDR_T))
77 /* Header + Magic Number */
78 #define BUFFER_PADDING_SIZE (sizeof(BUFFER_HDR_T) + sizeof(uint32_t))
79 /* pool size must allow for header */
80 #define MAX_USER_BUF_SIZE ((uint16_t)0xffff - BUFFER_PADDING_SIZE)
81 #define MAGIC_NO 0xDDBADDBA
82 
83 #define BUF_STATUS_FREE 0
84 #define BUF_STATUS_UNLINKED 1
85 #define BUF_STATUS_QUEUED 2
86 
87 /* Put all GKI variables into one control block
88 */
89 typedef struct {
90 /* Task management variables
91 */
92 /* The stack and stack size are not used on Windows
93 */
94 
95 #if (GKI_NUM_FIXED_BUF_POOLS > 0)
96   uint8_t bufpool0[(ALIGN_POOL(GKI_BUF0_SIZE) + BUFFER_PADDING_SIZE) *
97                    GKI_BUF0_MAX];
98 #endif
99 
100 #if (GKI_NUM_FIXED_BUF_POOLS > 1)
101   uint8_t bufpool1[(ALIGN_POOL(GKI_BUF1_SIZE) + BUFFER_PADDING_SIZE) *
102                    GKI_BUF1_MAX];
103 #endif
104 
105 #if (GKI_NUM_FIXED_BUF_POOLS > 2)
106   uint8_t bufpool2[(ALIGN_POOL(GKI_BUF2_SIZE) + BUFFER_PADDING_SIZE) *
107                    GKI_BUF2_MAX];
108 #endif
109 
110 #if (GKI_NUM_FIXED_BUF_POOLS > 3)
111   uint8_t bufpool3[(ALIGN_POOL(GKI_BUF3_SIZE) + BUFFER_PADDING_SIZE) *
112                    GKI_BUF3_MAX];
113 #endif
114 
115 #if (GKI_NUM_FIXED_BUF_POOLS > 4)
116   uint8_t bufpool4[(ALIGN_POOL(GKI_BUF4_SIZE) + BUFFER_PADDING_SIZE) *
117                    GKI_BUF4_MAX];
118 #endif
119 
120 #if (GKI_NUM_FIXED_BUF_POOLS > 5)
121   uint8_t bufpool5[(ALIGN_POOL(GKI_BUF5_SIZE) + BUFFER_PADDING_SIZE) *
122                    GKI_BUF5_MAX];
123 #endif
124 
125 #if (GKI_NUM_FIXED_BUF_POOLS > 6)
126   uint8_t bufpool6[(ALIGN_POOL(GKI_BUF6_SIZE) + BUFFER_PADDING_SIZE) *
127                    GKI_BUF6_MAX];
128 #endif
129 
130 #if (GKI_NUM_FIXED_BUF_POOLS > 7)
131   uint8_t bufpool7[(ALIGN_POOL(GKI_BUF7_SIZE) + BUFFER_PADDING_SIZE) *
132                    GKI_BUF7_MAX];
133 #endif
134 
135 #if (GKI_NUM_FIXED_BUF_POOLS > 8)
136   uint8_t bufpool8[(ALIGN_POOL(GKI_BUF8_SIZE) + BUFFER_PADDING_SIZE) *
137                    GKI_BUF8_MAX];
138 #endif
139 
140 #if (GKI_NUM_FIXED_BUF_POOLS > 9)
141   uint8_t bufpool9[(ALIGN_POOL(GKI_BUF9_SIZE) + BUFFER_PADDING_SIZE) *
142                    GKI_BUF9_MAX];
143 #endif
144 
145 #if (GKI_NUM_FIXED_BUF_POOLS > 10)
146   uint8_t bufpool10[(ALIGN_POOL(GKI_BUF10_SIZE) + BUFFER_PADDING_SIZE) *
147                     GKI_BUF10_MAX];
148 #endif
149 
150 #if (GKI_NUM_FIXED_BUF_POOLS > 11)
151   uint8_t bufpool11[(ALIGN_POOL(GKI_BUF11_SIZE) + BUFFER_PADDING_SIZE) *
152                     GKI_BUF11_MAX];
153 #endif
154 
155 #if (GKI_NUM_FIXED_BUF_POOLS > 12)
156   uint8_t bufpool12[(ALIGN_POOL(GKI_BUF12_SIZE) + BUFFER_PADDING_SIZE) *
157                     GKI_BUF12_MAX];
158 #endif
159 
160 #if (GKI_NUM_FIXED_BUF_POOLS > 13)
161   uint8_t bufpool13[(ALIGN_POOL(GKI_BUF13_SIZE) + BUFFER_PADDING_SIZE) *
162                     GKI_BUF13_MAX];
163 #endif
164 
165 #if (GKI_NUM_FIXED_BUF_POOLS > 14)
166   uint8_t bufpool14[(ALIGN_POOL(GKI_BUF14_SIZE) + BUFFER_PADDING_SIZE) *
167                     GKI_BUF14_MAX];
168 #endif
169 
170 #if (GKI_NUM_FIXED_BUF_POOLS > 15)
171   uint8_t bufpool15[(ALIGN_POOL(GKI_BUF15_SIZE) + BUFFER_PADDING_SIZE) *
172                     GKI_BUF15_MAX];
173 #endif
174 
175   uint8_t* OSStack[GKI_MAX_TASKS];     /* pointer to beginning of stack */
176   uint16_t OSStackSize[GKI_MAX_TASKS]; /* stack size available to each task */
177 
178   int8_t* OSTName[GKI_MAX_TASKS]; /* name of the task */
179 
180   uint8_t OSRdyTbl[GKI_MAX_TASKS]; /* current state of the task */
181   uint16_t OSWaitEvt
182       [GKI_MAX_TASKS]; /* events that have to be processed by the task */
183   uint16_t OSWaitForEvt[GKI_MAX_TASKS]; /* events the task is waiting for*/
184 
185   uint32_t OSTicks;   /* system ticks from start */
186   uint32_t OSIdleCnt; /* idle counter */
187   int16_t
188       OSDisableNesting; /* counter to keep track of interrupt disable nesting */
189   int16_t OSLockNesting; /* counter to keep track of sched lock nesting */
190   int16_t OSIntNesting;  /* counter to keep track of interrupt nesting */
191 
192   /* Timer related variables
193   */
194   int32_t OSTicksTilExp; /* Number of ticks till next timer expires */
195 #if (GKI_DELAY_STOP_SYS_TICK > 0)
196   uint32_t OSTicksTilStop; /* inactivity delay timer; OS Ticks till stopping
197                               system tick */
198 #endif
199   int32_t OSNumOrigTicks; /* Number of ticks between last timer expiration to
200                              the next one */
201 
202   int32_t OSWaitTmr
203       [GKI_MAX_TASKS]; /* ticks the task has to wait, for specific events */
204 
205 /* Only take up space timers used in the system (GKI_NUM_TIMERS defined in
206  * target.h) */
207 #if (GKI_NUM_TIMERS > 0)
208   int32_t OSTaskTmr0[GKI_MAX_TASKS];
209   int32_t OSTaskTmr0R[GKI_MAX_TASKS];
210 #endif
211 
212 #if (GKI_NUM_TIMERS > 1)
213   int32_t OSTaskTmr1[GKI_MAX_TASKS];
214   int32_t OSTaskTmr1R[GKI_MAX_TASKS];
215 #endif
216 
217 #if (GKI_NUM_TIMERS > 2)
218   int32_t OSTaskTmr2[GKI_MAX_TASKS];
219   int32_t OSTaskTmr2R[GKI_MAX_TASKS];
220 #endif
221 
222 #if (GKI_NUM_TIMERS > 3)
223   int32_t OSTaskTmr3[GKI_MAX_TASKS];
224   int32_t OSTaskTmr3R[GKI_MAX_TASKS];
225 #endif
226 
227   /* Buffer related variables
228   */
229   BUFFER_HDR_T* OSTaskQFirst[GKI_MAX_TASKS]
230                             [NUM_TASK_MBOX]; /* array of pointers to the first
231                                                 event in the task mailbox */
232   BUFFER_HDR_T* OSTaskQLast[GKI_MAX_TASKS]
233                            [NUM_TASK_MBOX]; /* array of pointers to the last
234                                                event in the task mailbox */
235 
236   /* Define the buffer pool management variables
237   */
238   FREE_QUEUE_T freeq[GKI_NUM_TOTAL_BUF_POOLS];
239 
240   uint16_t pool_buf_size[GKI_NUM_TOTAL_BUF_POOLS];
241   uint16_t pool_max_count[GKI_NUM_TOTAL_BUF_POOLS];
242   uint16_t pool_additions[GKI_NUM_TOTAL_BUF_POOLS];
243 
244   /* Define the buffer pool start addresses
245   */
246   uint8_t* pool_start[GKI_NUM_TOTAL_BUF_POOLS]; /* array of pointers to the
247                                                    start of each buffer pool */
248   uint8_t*
249       pool_end[GKI_NUM_TOTAL_BUF_POOLS]; /* array of pointers to the end of each
250                                             buffer pool */
251   uint16_t pool_size
252       [GKI_NUM_TOTAL_BUF_POOLS]; /* actual size of the buffers in a pool */
253 
254   /* Define the buffer pool access control variables */
255   void* p_user_mempool; /* User O/S memory pool */
256   uint16_t
257       pool_access_mask; /* Bits are set if the corresponding buffer pool is a
258                            restricted pool */
259   uint8_t pool_list[GKI_NUM_TOTAL_BUF_POOLS]; /* buffer pools arranged in the
260                                                  order of size */
261   uint8_t
262       curr_total_no_of_pools; /* number of fixed buf pools + current number of
263                                  dynamic pools */
264 
265   bool timer_nesting; /* flag to prevent timer interrupt nesting */
266 
267   /* Time queue arrays */
268   TIMER_LIST_Q* timer_queues[GKI_MAX_TIMER_QUEUES];
269   /* System tick callback */
270   SYSTEM_TICK_CBACK* p_tick_cb;
271   bool system_tick_running; /* TRUE if system tick is running. Valid only if
272                                p_tick_cb is not NULL */
273 
274 } tGKI_COM_CB;
275 
276 /* Internal GKI function prototypes
277 */
278 extern bool gki_chk_buf_damage(void*);
279 extern bool gki_chk_buf_owner(void*);
280 extern void gki_buffer_init(void);
281 extern void gki_timers_init(void);
282 extern void gki_adjust_timer_count(int32_t);
283 
284 #endif
285