| 1 | /*************************************************************************** |
|---|
| 2 | * Copyright (c) 2004-2009, Broadcom Corporation |
|---|
| 3 | * All Rights Reserved |
|---|
| 4 | * Confidential Property of Broadcom Corporation |
|---|
| 5 | * |
|---|
| 6 | * THIS SOFTWARE MAY ONLY BE USED SUBJECT TO AN EXECUTED SOFTWARE LICENSE |
|---|
| 7 | * AGREEMENT BETWEEN THE USER AND BROADCOM. YOU HAVE NO RIGHT TO USE OR |
|---|
| 8 | * EXPLOIT THIS MATERIAL EXCEPT SUBJECT TO THE TERMS OF SUCH AN AGREEMENT. |
|---|
| 9 | * |
|---|
| 10 | * $brcm_Workfile: bsettop_os.h $ |
|---|
| 11 | * $brcm_Revision: 3 $ |
|---|
| 12 | * $brcm_Date: 8/18/09 6:38p $ |
|---|
| 13 | * |
|---|
| 14 | * Module Description: |
|---|
| 15 | * |
|---|
| 16 | * Revision History: |
|---|
| 17 | * |
|---|
| 18 | * $brcm_Log: /BSEAV/api/src/nexus/bsettop_os.h $ |
|---|
| 19 | * |
|---|
| 20 | * 3 8/18/09 6:38p katrep |
|---|
| 21 | * PR56109: Use calllbacks instead of using events for callbacks to the |
|---|
| 22 | * app. |
|---|
| 23 | * |
|---|
| 24 | * 2 6/4/08 11:24a rjlewis |
|---|
| 25 | * PR40352: extra functions needed for VxWorks. |
|---|
| 26 | * |
|---|
| 27 | * 1 3/31/08 6:04p jgarrett |
|---|
| 28 | * PR 40606: Creating bsettop_os for nexus |
|---|
| 29 | * |
|---|
| 30 | ***************************************************************************/ |
|---|
| 31 | #ifndef BSETTOP_OS_H__ |
|---|
| 32 | #define BSETTOP_OS_H__ |
|---|
| 33 | |
|---|
| 34 | /*=********************************************** |
|---|
| 35 | Provides additional operating system functionality |
|---|
| 36 | beyond BKNI. Each OS needs to implement these functions. |
|---|
| 37 | |
|---|
| 38 | These functions run without respect to any thunk layer, therefore |
|---|
| 39 | they can be executed by high or low level settop api code. |
|---|
| 40 | *************************************************/ |
|---|
| 41 | |
|---|
| 42 | #include "bstd.h" |
|---|
| 43 | #include "bsettop_types.h" |
|---|
| 44 | #include "b_os_lib.h" |
|---|
| 45 | |
|---|
| 46 | #ifdef __cplusplus |
|---|
| 47 | extern "C" { |
|---|
| 48 | #endif |
|---|
| 49 | |
|---|
| 50 | /* the following interface is used to set one shot timer callbacks */ |
|---|
| 51 | typedef B_SchedulerTimerId b_timer_t; |
|---|
| 52 | typedef void (*b_timer_callback_t)(void *cntx); |
|---|
| 53 | |
|---|
| 54 | /* Init/uninit os services */ |
|---|
| 55 | bresult b_os_init(void); |
|---|
| 56 | bresult b_os_uninit(void); |
|---|
| 57 | |
|---|
| 58 | /* this function is used to acquire global lock */ |
|---|
| 59 | void b_lock(void); |
|---|
| 60 | /* this function is uses to release global lock (lock must be held by the current task before function is called) */ |
|---|
| 61 | void b_unlock(void); |
|---|
| 62 | /* this function returns trus if current task is holds the lock, the sole purpose of this function is to be used with conjunction with BDBG_ASSERT, e.g. BDBG_ASSERT(b_lock_assert()); */ |
|---|
| 63 | bool b_lock_assert(void); |
|---|
| 64 | #define B_LOCK_ASSERT() BDBG_ASSERT(b_lock_assert()) |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | Request a timer callback to occur no sooner than the specified in milliseconds. |
|---|
| 68 | It may come any amount of time later. If this function called within SettopApi core |
|---|
| 69 | callback would be called with global mutex (b_lock) held. Otherwise no particular |
|---|
| 70 | mutex held and it's user responsibility to call necessary synchronization functions. |
|---|
| 71 | **/ |
|---|
| 72 | b_timer_t b_timer_schedule(unsigned delay /* ms */, b_timer_callback_t callback, void *cntx); |
|---|
| 73 | |
|---|
| 74 | /** |
|---|
| 75 | Cancel a timer callback. |
|---|
| 76 | **/ |
|---|
| 77 | void b_timer_cancel(b_timer_t timer); |
|---|
| 78 | |
|---|
| 79 | |
|---|
| 80 | typedef B_Time b_time_t; |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | Get the current time. |
|---|
| 84 | The value of b_time_t is platform dependent. However, you can obtain a platform-independent |
|---|
| 85 | time diff by using b_time_diff. |
|---|
| 86 | **/ |
|---|
| 87 | void b_time_get(b_time_t *time); /* this functions returns current system time (timestamp) */ |
|---|
| 88 | |
|---|
| 89 | /** |
|---|
| 90 | Returns difference in milliseconds between two timestamps obtained with b_time_get. |
|---|
| 91 | |
|---|
| 92 | It's recomended that code would use this function in a way that it could return |
|---|
| 93 | only positive values (where applicable, it helps in handling wraparound conditions) |
|---|
| 94 | |
|---|
| 95 | TODO: Regarding the above comment, we need to define specific behavior for wraparound. |
|---|
| 96 | This either handles wrap-around and always returns a positive value, or it does not. |
|---|
| 97 | **/ |
|---|
| 98 | long b_time_diff(const b_time_t *future, const b_time_t *past); |
|---|
| 99 | |
|---|
| 100 | /** |
|---|
| 101 | This function flushes and invalidates data cache in a specified memory region. |
|---|
| 102 | |
|---|
| 103 | This function is used before a DMA output (write) operation to be sure that the DMA uses the flushed data. |
|---|
| 104 | This function is also used before a DMA input (read) operation to be sure that when the data is read it |
|---|
| 105 | comes from the newly DMAed data (and not stale data found in the cache). |
|---|
| 106 | |
|---|
| 107 | Note: the implementation ensures that both a flush AND an invailidate operation is performed. |
|---|
| 108 | **/ |
|---|
| 109 | void b_cacheflush(const void *addr, size_t nbytes); |
|---|
| 110 | |
|---|
| 111 | /* |
|---|
| 112 | Summary: |
|---|
| 113 | The following two functions convert from b_lock state to idle state, and vice versa. |
|---|
| 114 | |
|---|
| 115 | Description: |
|---|
| 116 | Both function is place for a race conditione (they don't swap locks atomically) |
|---|
| 117 | so critical state might need to be reexamined after function call. |
|---|
| 118 | */ |
|---|
| 119 | void b_lock2idle(void); |
|---|
| 120 | void b_idle2lock(void); |
|---|
| 121 | |
|---|
| 122 | /* |
|---|
| 123 | Summary: |
|---|
| 124 | The following two functions convert from b_lock state to slow state, and vice versa. |
|---|
| 125 | |
|---|
| 126 | Description: |
|---|
| 127 | Both function is place for a race conditione (they don't swap locks atomically) |
|---|
| 128 | so critical state might need to be reexamined after function call. |
|---|
| 129 | */ |
|---|
| 130 | void b_lock2slow(void); |
|---|
| 131 | void b_slow2lock(void); |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | /* |
|---|
| 135 | The b_event interface provides for receiving a task-context callback function when a KNI event is fired. |
|---|
| 136 | |
|---|
| 137 | The callback function is called with b_lock already held. |
|---|
| 138 | |
|---|
| 139 | The caller should execute the callback with the absolute minimum amount of time. All events |
|---|
| 140 | are serialized. You should never convert from lock2idle or lock2slow in an event callback because it will |
|---|
| 141 | cause all other event processing to block. |
|---|
| 142 | |
|---|
| 143 | It is possible to call b_event_unregister from inside an event. |
|---|
| 144 | |
|---|
| 145 | It is required that all b_event functions are called with b_lock held. |
|---|
| 146 | */ |
|---|
| 147 | |
|---|
| 148 | typedef void (*b_event_callback_t)(void *cntx); |
|---|
| 149 | |
|---|
| 150 | typedef B_SchedulerEventId b_event_id_t; |
|---|
| 151 | |
|---|
| 152 | b_event_id_t b_event_register(B_EventHandle event, b_event_callback_t callback, void *cntx); |
|---|
| 153 | void b_event_unregister(b_event_id_t id); |
|---|
| 154 | |
|---|
| 155 | /* |
|---|
| 156 | The b_callback interface provides for receiving a task-context callback function when a KNI event is fired. |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | The caller should execute the callback with the absolute minimum amount of time. All callbacks |
|---|
| 160 | are serialized by scheduler. |
|---|
| 161 | */ |
|---|
| 162 | typedef void (*b_callback_t)(void *cntx,int param2); |
|---|
| 163 | |
|---|
| 164 | typedef B_SchedulerCallbackHandle b_calllback_id_t; |
|---|
| 165 | |
|---|
| 166 | b_calllback_id_t b_callback_create(void *pInstanceHandle, b_callback_t function,void *cntx,int param); |
|---|
| 167 | void b_callback_destroy(b_calllback_id_t callback); |
|---|
| 168 | void b_callback_fire(b_calllback_id_t callback); |
|---|
| 169 | void b_callback_stop(void *pInstanceHandle); |
|---|
| 170 | |
|---|
| 171 | |
|---|
| 172 | /*********************** |
|---|
| 173 | * |
|---|
| 174 | * b_task api - used for starting tasks in different environments |
|---|
| 175 | * |
|---|
| 176 | */ |
|---|
| 177 | typedef struct b_task *b_task_t; |
|---|
| 178 | typedef void *(*b_task_func)(void *data); |
|---|
| 179 | |
|---|
| 180 | /** |
|---|
| 181 | Summary: |
|---|
| 182 | Params for b_start_task. This is optional. |
|---|
| 183 | **/ |
|---|
| 184 | typedef struct b_task_params |
|---|
| 185 | { |
|---|
| 186 | unsigned priority; /* 0 is highest, 100 is lowest. this is a hint and should |
|---|
| 187 | not be considered a system-critical setting. */ |
|---|
| 188 | size_t stack_size; /* if 0, then default */ |
|---|
| 189 | const char *name; /* optional. used for debug. may be truncated depending on system capabilities. */ |
|---|
| 190 | } b_task_params; |
|---|
| 191 | |
|---|
| 192 | /** |
|---|
| 193 | Summary: |
|---|
| 194 | Set b_task_params defaults. |
|---|
| 195 | **/ |
|---|
| 196 | void b_task_params_init(b_task_params *params); |
|---|
| 197 | |
|---|
| 198 | /** |
|---|
| 199 | Summary: |
|---|
| 200 | Create a new task and call the function with the data parameter. |
|---|
| 201 | **/ |
|---|
| 202 | bresult b_start_task( |
|---|
| 203 | b_task_t *handle, /* [out] new thread handle, passed to b_stop_task when finished */ |
|---|
| 204 | const b_task_params *params, /* param structure containing priority, task name, and stack size. */ |
|---|
| 205 | b_task_func func, /* function to be called by new thread. when this func returns, the |
|---|
| 206 | thread is done processing. */ |
|---|
| 207 | void *data /* data to be passed to func */ |
|---|
| 208 | ); |
|---|
| 209 | |
|---|
| 210 | /** |
|---|
| 211 | Summary: |
|---|
| 212 | Clean up a finished task. |
|---|
| 213 | Description: |
|---|
| 214 | It is very important to realize that this function is not guaranteed to stop the task. |
|---|
| 215 | It assumes that the function has already exited or will exit on its own. |
|---|
| 216 | The application should set whatever conditions are required in order to make the |
|---|
| 217 | task function exit. |
|---|
| 218 | b_stop_task will block until the function is actually stopped and then it |
|---|
| 219 | will clean up whatever resources were allocated for the task. |
|---|
| 220 | **/ |
|---|
| 221 | void b_stop_task(b_task_t handle); |
|---|
| 222 | |
|---|
| 223 | #ifdef __vxworks |
|---|
| 224 | |
|---|
| 225 | /** |
|---|
| 226 | Summary: |
|---|
| 227 | Return the size of a file. |
|---|
| 228 | Description: |
|---|
| 229 | Some OSs don't have support for large files. This provides size for a large file. |
|---|
| 230 | **/ |
|---|
| 231 | off_t b_file_size(int desc); |
|---|
| 232 | |
|---|
| 233 | /** |
|---|
| 234 | Summary: |
|---|
| 235 | Seek to location in long file. |
|---|
| 236 | Description: |
|---|
| 237 | Some OSs don't have support for large files. This provides a seek operation on a long file. |
|---|
| 238 | **/ |
|---|
| 239 | off_t b_file_seek(int desc, off_t offset, int whence); |
|---|
| 240 | |
|---|
| 241 | #endif |
|---|
| 242 | |
|---|
| 243 | #ifdef __cplusplus |
|---|
| 244 | } |
|---|
| 245 | #endif |
|---|
| 246 | |
|---|
| 247 | #endif /* BSETTOP_OS_H__ */ |
|---|