Sridhar Parasuram | bf39132 | 2015-01-23 09:29:07 -0800 | [diff] [blame] | 1 | /* Copyright (c) 2015, The Linux Foundation. All rights reserved. |
| 2 | |
| 3 | Redistribution and use in source and binary forms, with or without |
| 4 | modification, are permitted provided that the following conditions are |
| 5 | met: |
| 6 | * Redistributions of source code must retain the above copyright |
| 7 | notice, this list of conditions and the following disclaimer. |
| 8 | * Redistributions in binary form must reproduce the above |
| 9 | copyright notice, this list of conditions and the following |
| 10 | disclaimer in the documentation and/or other materials provided |
| 11 | with the distribution. |
| 12 | * Neither the name of The Linux Foundation nor the names of its |
| 13 | contributors may be used to endorse or promote products derived |
| 14 | from this software without specific prior written permission. |
| 15 | |
| 16 | THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED |
| 17 | WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT |
| 19 | ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
| 20 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 22 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 24 | WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
| 25 | OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 26 | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
| 29 | |
| 30 | /*=========================================================================== |
| 31 | INCLUDE FILES |
| 32 | ===========================================================================*/ |
| 33 | |
| 34 | #include "glink_os_utils.h" |
| 35 | #include <arch/defines.h> |
| 36 | #include <malloc.h> |
| 37 | #include <kernel/thread.h> |
| 38 | #include <string.h> |
| 39 | #include <kernel/event.h> |
| 40 | #include <platform/irqs.h> |
| 41 | #include <platform/interrupts.h> |
| 42 | |
| 43 | /*=========================================================================== |
| 44 | MACRO DEFINITIONS |
| 45 | ===========================================================================*/ |
| 46 | |
| 47 | #define OS_LOG_BUFFER_SIZE ( 16384 ) |
| 48 | |
| 49 | /*=========================================================================== |
| 50 | TYPE DEFINITIONS |
| 51 | ===========================================================================*/ |
| 52 | typedef void (*glink_os_thread_fn_type)(void* param); |
| 53 | |
| 54 | typedef struct _glink_os_thread_info_type { |
| 55 | glink_os_thread_fn_type thread_fn; |
| 56 | void *param; |
| 57 | }glink_os_thread_info_type; |
| 58 | |
| 59 | /*=========================================================================== |
| 60 | EXTERNAL FUNCTION DEFINITIONS |
| 61 | ===========================================================================*/ |
| 62 | |
| 63 | /*=========================================================================== |
| 64 | FUNCTION glink_os_cs_init |
| 65 | ===========================================================================*/ |
| 66 | /** |
| 67 | Initializes a Critical Section |
| 68 | |
| 69 | @param[in] cs pointer to critical section object allocated by caller. |
| 70 | |
| 71 | @return |
| 72 | TRUE if critical section was initialized, FALSE otherwise |
| 73 | */ |
| 74 | /*=========================================================================*/ |
| 75 | boolean glink_os_cs_init( os_cs_type *cs ) |
| 76 | { |
| 77 | boolean return_value = TRUE; |
| 78 | |
| 79 | /* Create the new critical section */ |
| 80 | |
| 81 | return ( return_value ); |
| 82 | } |
| 83 | |
| 84 | /*=========================================================================== |
| 85 | FUNCTION glink_os_cs_deinit |
| 86 | ===========================================================================*/ |
| 87 | /** |
| 88 | This function de-initializes a critical section. |
| 89 | |
| 90 | @param[in] cs Pointer to the critical section to be de-initialized. |
| 91 | |
| 92 | @return |
| 93 | TRUE if critical section was initialized, FALSE otherwise |
| 94 | |
| 95 | */ |
| 96 | /*=========================================================================*/ |
| 97 | boolean glink_os_cs_deinit( os_cs_type *cs ) |
| 98 | { |
| 99 | /* Deinitialize the critical section */ |
| 100 | |
| 101 | return TRUE; |
| 102 | } |
| 103 | |
| 104 | /*=========================================================================== |
| 105 | FUNCTION glink_os_cs_acquire |
| 106 | ===========================================================================*/ |
| 107 | /** |
| 108 | Lock a critical section |
| 109 | |
| 110 | @param[in] cs Pointer the the critical section |
| 111 | |
| 112 | @return None. |
| 113 | */ |
| 114 | /*=========================================================================*/ |
| 115 | void glink_os_cs_acquire( os_cs_type *cs ) |
| 116 | { |
| 117 | enter_critical_section(); |
| 118 | } |
| 119 | |
| 120 | /*=========================================================================== |
| 121 | FUNCTION glink_os_cs_release |
| 122 | ===========================================================================*/ |
| 123 | /** |
| 124 | Unlock a critical section |
| 125 | |
| 126 | @param[in] cs Pointer the the critical section |
| 127 | |
| 128 | @return None. |
| 129 | */ |
| 130 | /*=========================================================================*/ |
| 131 | void glink_os_cs_release( os_cs_type *cs ) |
| 132 | { |
| 133 | exit_critical_section(); |
| 134 | } |
| 135 | |
| 136 | /*=========================================================================== |
| 137 | FUNCTION glink_os_cs_destroy |
| 138 | ===========================================================================*/ |
| 139 | /** |
| 140 | Destroys a Critical Section |
| 141 | |
| 142 | @return none. |
| 143 | */ |
| 144 | /*=========================================================================*/ |
| 145 | void glink_os_cs_destroy( os_cs_type *cs ) |
| 146 | { |
| 147 | /* Initialize the critical section */ |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | /*=========================================================================== |
| 152 | FUNCTION glink_os_malloc |
| 153 | ===========================================================================*/ |
| 154 | /** |
| 155 | Dynamically allocate a region of memory from the heap. The region should be |
| 156 | freed using \c glink_os_free when no longer used. |
| 157 | |
| 158 | @param[in] size The number of bytes to be allocated. |
| 159 | |
| 160 | @return The pointer to the region of memory that was allocated. |
| 161 | NULL if the allocation failed. |
| 162 | */ |
| 163 | /*=========================================================================*/ |
| 164 | |
| 165 | void *glink_os_malloc( size_t size ) |
| 166 | { |
| 167 | void *pMem; |
| 168 | |
| 169 | pMem = malloc(size); |
| 170 | if (pMem == NULL) |
| 171 | return NULL; |
| 172 | return pMem; |
| 173 | } |
| 174 | |
| 175 | /*=========================================================================== |
| 176 | FUNCTION glink_os_calloc |
| 177 | ===========================================================================*/ |
| 178 | /** |
| 179 | Dynamically allocate a region of memory from the heap and initialize with |
| 180 | the zeroes. The region should be freed using \c glink_os_free |
| 181 | when no longer used. |
| 182 | |
| 183 | @param[in] size The number of bytes to be allocated. |
| 184 | |
| 185 | @return The pointer to the region of memory that was allocated. |
| 186 | NULL if the allocation failed. |
| 187 | */ |
| 188 | /*=========================================================================*/ |
| 189 | void *glink_os_calloc( size_t size ) |
| 190 | { |
| 191 | void *pMem; |
| 192 | pMem = malloc(size); |
| 193 | if( pMem == NULL ) |
| 194 | { |
| 195 | return NULL; |
| 196 | } |
| 197 | else |
| 198 | { |
| 199 | memset( pMem, 0, size ); |
| 200 | return pMem; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /*=========================================================================== |
| 205 | FUNCTION glink_os_free |
| 206 | ===========================================================================*/ |
| 207 | /** |
| 208 | Free a region of memory that was allocated by \c glink_os_malloc. |
| 209 | |
| 210 | @param[in] pMem The reference to the region of memory to be freed. |
| 211 | |
| 212 | @return NA |
| 213 | */ |
| 214 | /*=========================================================================*/ |
| 215 | void glink_os_free( void *pMem ) |
| 216 | { |
| 217 | free( pMem ); |
| 218 | } |
| 219 | |
| 220 | /*=========================================================================== |
| 221 | FUNCTION glink_os_string_copy |
| 222 | ===========================================================================*/ |
| 223 | /** |
| 224 | Copies the source string into the destination buffer until |
| 225 | size is reached, or until a '\0' is encountered. If valid, |
| 226 | the destination string will always be NULL deliminated. |
| 227 | |
| 228 | @param[in] dst The destination string, contents will be updated. |
| 229 | @param[in] src The source string |
| 230 | @param[in] size The maximum copy size (size of dst) |
| 231 | |
| 232 | @return |
| 233 | The destination string pointer, dst. |
| 234 | */ |
| 235 | /*==========================================================================*/ |
| 236 | char *glink_os_string_copy( char *dst, const char *src, uint32 size ) |
| 237 | { |
| 238 | ( void )strlcpy( dst, src, size ); |
| 239 | |
| 240 | return dst; |
| 241 | } |
| 242 | |
| 243 | /*=========================================================================== |
| 244 | FUNCTION glink_os_string_compare |
| 245 | ===========================================================================*/ |
| 246 | /** |
| 247 | Compares two strings delimited by size or NULL character. |
| 248 | |
| 249 | @param[in] s1 String 1 |
| 250 | @param[in] s2 String 2 |
| 251 | @param[in] size The maximum number of characters to compare |
| 252 | |
| 253 | @return |
| 254 | 0 if strings are identical (up to size characters), non-zero otherwise |
| 255 | */ |
| 256 | /*==========================================================================*/ |
| 257 | long glink_os_string_compare( const char *s1, const char *s2, uint32 size ) |
| 258 | { |
| 259 | return strncmp( s1, s2, size ); |
| 260 | } |
| 261 | |
| 262 | /*=========================================================================== |
| 263 | FUNCTION glink_os_copy_mem |
| 264 | ===========================================================================*/ |
| 265 | /** |
| 266 | Copies the source buffer into the destination buffer. |
| 267 | |
| 268 | @param[in] dst The destination, contents will be updated. |
| 269 | @param[in] src The source |
| 270 | @param[in] size The maximum copy size (size of dst) |
| 271 | |
| 272 | @return |
| 273 | The destination string pointer, dst. |
| 274 | */ |
| 275 | /*==========================================================================*/ |
| 276 | void glink_os_copy_mem( void *dst, const void *src, uint32 size ) |
| 277 | { |
| 278 | memcpy( dst, src, size ); |
| 279 | } |
| 280 | |
| 281 | /*=========================================================================== |
| 282 | FUNCTION glink_os_register_isr |
| 283 | ===========================================================================*/ |
| 284 | /** |
| 285 | Registers ISR with the interrupt controller |
| 286 | |
| 287 | @param[in] irq_in Interrupt to register for |
| 288 | @param[in] isr Callback to be invoked when interrupt fires |
| 289 | @param[in] cb_data Data to be provided to the callback |
| 290 | |
| 291 | @return TRUE if registration was successful, FALSE otherwise. |
| 292 | */ |
| 293 | /*=========================================================================*/ |
| 294 | boolean glink_os_register_isr( uint32 irq_in, os_isr_cb_fn isr, void* cb_data ) |
| 295 | { |
| 296 | boolean return_value = TRUE; |
| 297 | |
| 298 | /* Register the interrupt */ |
| 299 | dprintf(SPEW, "Register interrupt: %u\n", irq_in); |
| 300 | register_int_handler(irq_in, (int_handler)(isr), cb_data); |
| 301 | |
| 302 | return ( return_value ); |
| 303 | } |
| 304 | |
| 305 | /*=========================================================================== |
| 306 | FUNCTION glink_os_deregister_isr |
| 307 | ===========================================================================*/ |
| 308 | /** |
| 309 | De-registers ISR with the interrupt controller |
| 310 | |
| 311 | @param[in] irq_in Interrupt to deregister for |
| 312 | |
| 313 | @return TRUE if de-registration was successful, FALSE otherwise. |
| 314 | */ |
| 315 | /*=========================================================================*/ |
| 316 | boolean glink_os_deregister_isr( uint32 irq_in ) |
| 317 | { |
| 318 | boolean return_value = TRUE; |
| 319 | mask_interrupt(irq_in); |
| 320 | return ( return_value ); |
| 321 | } |
| 322 | |
| 323 | /*=========================================================================== |
| 324 | FUNCTION glink_os_enable_interrupt |
| 325 | ===========================================================================*/ |
| 326 | /** |
| 327 | Enables the interrupt in the interrupt controller |
| 328 | |
| 329 | @param[in] irq_in Interrupt to enable |
| 330 | |
| 331 | @return TRUE if operation was successful, FALSE otherwise. |
| 332 | */ |
| 333 | /*=========================================================================*/ |
| 334 | boolean glink_os_enable_interrupt( uint32 irq_in ) |
| 335 | { |
| 336 | boolean return_value = TRUE; |
| 337 | unmask_interrupt(irq_in); |
| 338 | return ( return_value ); |
| 339 | } |
| 340 | |
| 341 | /*=========================================================================== |
| 342 | FUNCTION glink_os_disable_interrupt |
| 343 | ===========================================================================*/ |
| 344 | /** |
| 345 | Disables the interrupt in the interrupt controller |
| 346 | |
| 347 | @param[in] irq_in Interrupt to disable |
| 348 | |
| 349 | @return TRUE if operation was successful, FALSE otherwise. |
| 350 | */ |
| 351 | /*=========================================================================*/ |
| 352 | boolean glink_os_disable_interrupt( uint32 irq_in ) |
| 353 | { |
| 354 | boolean return_value = TRUE; |
| 355 | dprintf(INFO, "Disable IPC Interrupt\n"); |
| 356 | mask_interrupt(irq_in); |
| 357 | return ( return_value ); |
| 358 | } |