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