summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/freescale/sdk_fman/src/xx/xx_arm_linux.c
blob: dd3e376e96af9ffae833f9385f7dff1bcd881e21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
/*
 * Copyright 2008-2012 Freescale Semiconductor Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of Freescale Semiconductor nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 *
 * ALTERNATIVELY, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") as published by the Free Software
 * Foundation, either version 2 of that License or (at your option) any
 * later version.
 *
 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/**************************************************************************//**
 @File          xx_arm_linux.c

 @Description   XX routines implementation for Linux.
*//***************************************************************************/
#include <linux/version.h>

#if defined(CONFIG_MODVERSIONS) && !defined(MODVERSIONS)
#define MODVERSIONS
#endif
#ifdef MODVERSIONS
#include <config/modversions.h>
#endif /* MODVERSIONS */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/ptrace.h>
#include <linux/errno.h>
#include <linux/ioport.h>
#include <linux/slab.h>
#include <linux/interrupt.h>
#include <linux/fs.h>
#include <linux/vmalloc.h>
#include <linux/init.h>
#include <linux/timer.h>
#include <linux/spinlock.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/smp.h>
#include <linux/of.h>
#include <linux/irqdomain.h>

#include <linux/workqueue.h>

#ifdef BIGPHYSAREA_ENABLE
#include <linux/bigphysarea.h>
#endif /* BIGPHYSAREA_ENABLE */

//#include <sysdev/fsl_soc.h>
#include <asm/pgtable.h>
#include <asm/irq.h>
#include <asm/bitops.h>
#include <asm/uaccess.h>
#include <asm/io.h>
#include <asm/atomic.h>
#include <asm/string.h>
#include <asm/byteorder.h>
#include <asm/page.h>

#include "error_ext.h"
#include "std_ext.h"
#include "list_ext.h"
#include "mm_ext.h"
#include "sys_io_ext.h"
#include "xx.h"


#define __ERR_MODULE__      MODULE_UNKNOWN

#ifdef BIGPHYSAREA_ENABLE
#define MAX_ALLOCATION_SIZE     128 * 1024 /* Maximum size allocated with kmalloc is 128K */


/* TODO: large allocations => use big phys area */
/******************************************************************************
 * routine:     get_nr_pages
 *
 * description:
 *     calculates the number of memory pages for a given size (in bytes)
 *
 * arguments:
 *     size       - the number of bytes
 *
 * return code:
 *     The number of pages
 *
 *****************************************************************************/
static __inline__ uint32_t get_nr_pages (uint32_t size)
{
    return (uint32_t)((size >> PAGE_SHIFT) + (size & PAGE_SHIFT ? 1 : 0));
}

static bool in_big_phys_area (uint32_t addr)
{
    uint32_t base, size;

    bigphysarea_get_details (&base, &size);
    return ((addr >= base) && (addr < base + size));
}
#endif /* BIGPHYSAREA_ENABLE */

void * xx_Malloc(uint32_t n)
{
    void        *a;
    uint32_t    flags;

    flags = XX_DisableAllIntr();
#ifdef BIGPHYSAREA_ENABLE
    if (n >= MAX_ALLOCATION_SIZE)
        a = (void*)bigphysarea_alloc_pages(get_nr_pages(n), 0, GFP_ATOMIC);
    else
#endif /* BIGPHYSAREA_ENABLE */
    a = (void *)kmalloc((uint32_t)n, GFP_ATOMIC);
    if (!a)
        XX_Print("No memory for XX_Malloc\n");
    XX_RestoreAllIntr(flags);

    return a;
}

void xx_Free(void *p)
{
#ifdef BIGPHYSAREA_ENABLE
    if (in_big_phys_area ((uint32_t)p))
        bigphysarea_free_pages(p);
    else
#endif /* BIGPHYSAREA_ENABLE */
    kfree(p);
}

void XX_Exit(int status)
{
    WARN(1, "\n\nFMD: fatal error, driver can't go on!!!\n\n");
}

#define BUF_SIZE    512
void XX_Print(char *str, ...)
{
    va_list args;
#ifdef CONFIG_SMP
    char buf[BUF_SIZE];
#endif /* CONFIG_SMP */

    va_start(args, str);
#ifdef CONFIG_SMP
    if (vsnprintf (buf, BUF_SIZE, str, args) >= BUF_SIZE)
        printk(KERN_WARNING "Illegal string to print!\n    more than %d characters.\n\tString was not printed completelly.\n", BUF_SIZE);
    printk(KERN_CRIT "cpu %d: %s",  raw_smp_processor_id(), buf);
#else
    vprintk(str, args);
#endif /* CONFIG_SMP */
    va_end(args);
}

void XX_Fprint(void *file, char *str, ...)
{
    va_list args;
#ifdef CONFIG_SMP
    char buf[BUF_SIZE];
#endif /* CONFIG_SMP */

    va_start(args, str);
#ifdef CONFIG_SMP
    if (vsnprintf (buf, BUF_SIZE, str, args) >= BUF_SIZE)
        printk(KERN_WARNING "Illegal string to print!\n    more than %d characters.\n\tString was not printed completelly.\n", BUF_SIZE);
    printk (KERN_CRIT "cpu %d: %s", smp_processor_id(), buf);

#else
    vprintk(str, args);
#endif /* CONFIG_SMP */
    va_end(args);
}

#ifdef DEBUG_XX_MALLOC
typedef void (*t_ffn)(void *);
typedef struct {
    t_ffn       f_free;
    void        *mem;
    char        *fname;
    int         fline;
    uint32_t    size;
    t_List      node;
} t_MemDebug;
#define MEMDBG_OBJECT(p_List) LIST_OBJECT(p_List, t_MemDebug, node)

LIST(memDbgLst);


void * XX_MallocDebug(uint32_t size, char *fname, int line)
{
    void       *mem;
    t_MemDebug *p_MemDbg;

    p_MemDbg = (t_MemDebug *)xx_Malloc(sizeof(t_MemDebug));
    if (p_MemDbg == NULL)
        return NULL;

    mem = xx_Malloc(size);
    if (mem == NULL)
    {
        XX_Free(p_MemDbg);
        return NULL;
    }

    INIT_LIST(&p_MemDbg->node);
    p_MemDbg->f_free = xx_Free;
    p_MemDbg->mem    = mem;
    p_MemDbg->fname  = fname;
    p_MemDbg->fline  = line;
    p_MemDbg->size   = size+sizeof(t_MemDebug);
    LIST_AddToTail(&p_MemDbg->node, &memDbgLst);

    return mem;
}

void * XX_MallocSmartDebug(uint32_t size,
                           int      memPartitionId,
                           uint32_t align,
                           char     *fname,
                           int      line)
{
    void       *mem;
    t_MemDebug *p_MemDbg;

    p_MemDbg = (t_MemDebug *)XX_Malloc(sizeof(t_MemDebug));
    if (p_MemDbg == NULL)
        return NULL;

    mem = xx_MallocSmart((uint32_t)size, memPartitionId, align);
    if (mem == NULL)
    {
        XX_Free(p_MemDbg);
        return NULL;
    }

    INIT_LIST(&p_MemDbg->node);
    p_MemDbg->f_free = xx_FreeSmart;
    p_MemDbg->mem    = mem;
    p_MemDbg->fname  = fname;
    p_MemDbg->fline  = line;
    p_MemDbg->size   = size+sizeof(t_MemDebug);
    LIST_AddToTail(&p_MemDbg->node, &memDbgLst);

    return mem;
}

static void debug_free(void *mem)
{
    t_List      *p_MemDbgLh = NULL;
    t_MemDebug  *p_MemDbg;
    bool        found = FALSE;

    if (LIST_IsEmpty(&memDbgLst))
    {
        REPORT_ERROR(MAJOR, E_ALREADY_FREE, ("Unbalanced free (0x%08x)", mem));
        return;
    }

    LIST_FOR_EACH(p_MemDbgLh, &memDbgLst)
    {
        p_MemDbg = MEMDBG_OBJECT(p_MemDbgLh);
        if (p_MemDbg->mem == mem)
        {
            found = TRUE;
            break;
        }
    }

    if (!found)
    {
        REPORT_ERROR(MAJOR, E_NOT_FOUND,
                     ("Attempt to free unallocated address (0x%08x)",mem));
        dump_stack();
        return;
    }

    LIST_Del(p_MemDbgLh);
    p_MemDbg->f_free(mem);
    p_MemDbg->f_free(p_MemDbg);
}

void XX_FreeSmart(void *p)
{
    debug_free(p);
}


void XX_Free(void *p)
{
    debug_free(p);
}

#else /* not DEBUG_XX_MALLOC */
void * XX_Malloc(uint32_t size)
{
    return xx_Malloc(size);
}

void * XX_MallocSmart(uint32_t size, int memPartitionId, uint32_t alignment)
{
    return xx_MallocSmart(size,memPartitionId, alignment);
}

void XX_FreeSmart(void *p)
{
    xx_FreeSmart(p);
}


void XX_Free(void *p)
{
    xx_Free(p);
}
#endif /* not DEBUG_XX_MALLOC */


#if (defined(REPORT_EVENTS) && (REPORT_EVENTS > 0))
void XX_EventById(uint32_t event, t_Handle appId, uint16_t flags, char *msg)
{
    e_Event eventCode = (e_Event)event;

    UNUSED(eventCode);
    UNUSED(appId);
    UNUSED(flags);
    UNUSED(msg);
}
#endif /* (defined(REPORT_EVENTS) && ... */


uint32_t XX_DisableAllIntr(void)
{
    unsigned long flags;

#ifdef local_irq_save_nort
    local_irq_save_nort(flags);
#else
    local_irq_save(flags);
#endif

    return (uint32_t)flags;
}

void XX_RestoreAllIntr(uint32_t flags)
{
#ifdef local_irq_restore_nort
    local_irq_restore_nort((unsigned long)flags);
#else
    local_irq_restore((unsigned long)flags);
#endif
}

t_Error XX_Call( uint32_t qid, t_Error (* f)(t_Handle), t_Handle id, t_Handle appId, uint16_t flags )
{
    UNUSED(qid);
    UNUSED(appId);
    UNUSED(flags);

    return f(id);
}

int XX_IsICacheEnable(void)
{
    return TRUE;
}

int XX_IsDCacheEnable(void)
{
    return TRUE;
}


typedef struct {
    t_Isr       *f_Isr;
    t_Handle    handle;
} t_InterruptHandler;


t_Handle interruptHandlers[0x00010000];

static irqreturn_t LinuxInterruptHandler (int irq, void *dev_id)
{
    t_InterruptHandler *p_IntrHndl = (t_InterruptHandler *)dev_id;
    p_IntrHndl->f_Isr(p_IntrHndl->handle);
    return IRQ_HANDLED;
}

t_Error XX_SetIntr(int irq, t_Isr *f_Isr, t_Handle handle)
{
    const char *device;
    t_InterruptHandler *p_IntrHndl;

    device = GetDeviceName(irq);
    if (device == NULL)
        RETURN_ERROR(MAJOR, E_INVALID_VALUE, ("Interrupt source - %d", irq));

    p_IntrHndl = (t_InterruptHandler *)XX_Malloc(sizeof(t_InterruptHandler));
    if (p_IntrHndl == NULL)
        RETURN_ERROR(MAJOR, E_NO_MEMORY, NO_MSG);
    p_IntrHndl->f_Isr = f_Isr;
    p_IntrHndl->handle = handle;
    interruptHandlers[irq] = p_IntrHndl;

    if (request_irq(GetDeviceIrqNum(irq), LinuxInterruptHandler, 0, device, p_IntrHndl) < 0)
        RETURN_ERROR(MAJOR, E_BUSY, ("Can't get IRQ %s\n", device));
    disable_irq(GetDeviceIrqNum(irq));

    return E_OK;
}

t_Error XX_FreeIntr(int irq)
{
    t_InterruptHandler *p_IntrHndl = interruptHandlers[irq];
    free_irq(GetDeviceIrqNum(irq), p_IntrHndl);
    XX_Free(p_IntrHndl);
    interruptHandlers[irq] = 0;
    return E_OK;
}

t_Error XX_EnableIntr(int irq)
{
    enable_irq(GetDeviceIrqNum(irq));
    return E_OK;
}

t_Error XX_DisableIntr(int irq)
{
    disable_irq(GetDeviceIrqNum(irq));
    return E_OK;
}


/*****************************************************************************/
/*                       Tasklet Service Routines                            */
/*****************************************************************************/
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
typedef struct
{
    t_Handle            h_Data;
    void                (*f_Callback) (void *);
    struct delayed_work dwork;
} t_Tasklet;

static void GenericTaskletCallback(struct work_struct *p_Work)
{
    t_Tasklet *p_Task = container_of(p_Work, t_Tasklet, dwork.work);

    p_Task->f_Callback(p_Task->h_Data);
}
#endif    /* LINUX_VERSION_CODE */


t_TaskletHandle XX_InitTasklet (void (*routine)(void *), void *data)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
    struct work_struct *p_Task;
    p_Task = (struct work_struct *)XX_Malloc(sizeof(struct work_struct));
    INIT_WORK(p_Task, routine, data);
#else
    t_Tasklet *p_Task = (t_Tasklet *)XX_Malloc(sizeof(t_Tasklet));
    p_Task->h_Data = data;
    p_Task->f_Callback = routine;
    INIT_DELAYED_WORK(&p_Task->dwork, GenericTaskletCallback);
#endif    /* LINUX_VERSION_CODE */

    return (t_TaskletHandle)p_Task;
}


void XX_FreeTasklet (t_TaskletHandle h_Tasklet)
{
    if (h_Tasklet)
        XX_Free(h_Tasklet);
}

int XX_ScheduleTask(t_TaskletHandle h_Tasklet, int immediate)
{
    int ans;

#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
    if (immediate)
        ans = schedule_work(h_Tasklet);
    else
        ans = schedule_delayed_work(h_Tasklet, 1);
#else
    if (immediate)
        ans = schedule_delayed_work(&((t_Tasklet *)h_Tasklet)->dwork, 0);
    else
        ans = schedule_delayed_work(&((t_Tasklet *)h_Tasklet)->dwork, HZ);
#endif /* LINUX_VERSION_CODE */

    return ans;
}

void XX_FlushScheduledTasks(void)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
    flush_scheduled_tasks();
#else
    flush_scheduled_work();
#endif    /* LINUX_VERSION_CODE */
}

int XX_TaskletIsQueued(t_TaskletHandle h_Tasklet)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
    return (int)(((struct work_struct *)h_Tasklet)->pending);
#else
    return (int)delayed_work_pending(&((t_Tasklet *)h_Tasklet)->dwork);
#endif    /* LINUX_VERSION_CODE */
}

void XX_SetTaskletData(t_TaskletHandle h_Tasklet, t_Handle data)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0)
    ((struct tq_struct *)h_Tasklet)->data = data;
#elif LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
    ((struct work_struct *)h_Tasklet)->data = data;
#else
    ((t_Tasklet *)h_Tasklet)->h_Data = data;
#endif    /* LINUX_VERSION_CODE */
}

t_Handle XX_GetTaskletData(t_TaskletHandle h_Tasklet)
{
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
    return (t_Handle)(((struct work_struct *)h_Tasklet)->data);
#else
    return ((t_Tasklet *)h_Tasklet)->h_Data;
#endif    /* LINUX_VERSION_CODE */
}


/*****************************************************************************/
/*                         Spinlock Service Routines                         */
/*****************************************************************************/

t_Handle XX_InitSpinlock(void)
{
    spinlock_t *p_Spinlock = (spinlock_t *)XX_Malloc(sizeof(spinlock_t));
    if (!p_Spinlock)
        return NULL;

    spin_lock_init(p_Spinlock);

    return (t_Handle)p_Spinlock;
}

void XX_FreeSpinlock(t_Handle h_Spinlock)
{
    if (h_Spinlock)
        XX_Free(h_Spinlock);
}

void XX_LockSpinlock(t_Handle h_Spinlock)
{
    spin_lock((spinlock_t *)h_Spinlock);
}

void XX_UnlockSpinlock(t_Handle h_Spinlock)
{
    spin_unlock((spinlock_t *)h_Spinlock);
}

uint32_t XX_LockIntrSpinlock(t_Handle h_Spinlock)
{
    unsigned long intrFlags;
    spin_lock_irqsave((spinlock_t *)h_Spinlock, intrFlags);
    return intrFlags;
}

void XX_UnlockIntrSpinlock(t_Handle h_Spinlock, uint32_t intrFlags)
{
     spin_unlock_irqrestore((spinlock_t *)h_Spinlock, (unsigned long)intrFlags);
}


/*****************************************************************************/
/*                        Timers Service Routines                            */
/*****************************************************************************/
/* The time now is in mili sec. resolution */
uint32_t XX_CurrentTime(void)
{
    return (jiffies*1000)/HZ;
}


t_Handle XX_CreateTimer(void)
{
    struct timer_list *p_Timer = (struct timer_list *)XX_Malloc(sizeof(struct timer_list));
    if (p_Timer)
    {
        memset(p_Timer, 0, sizeof(struct timer_list));
        init_timer(p_Timer);
    }
    return (t_Handle)p_Timer;
}

void XX_FreeTimer(t_Handle h_Timer)
{
    if (h_Timer)
        XX_Free(h_Timer);
}

void XX_StartTimer(t_Handle h_Timer,
                   uint32_t msecs,
                   bool     periodic,
                   void     (*f_TimerExpired)(t_Handle),
                   t_Handle h_Arg)
{
    int                 tmp_jiffies = (msecs*HZ)/1000;
    struct timer_list   *p_Timer = (struct timer_list *)h_Timer;

    SANITY_CHECK_RETURN((periodic == FALSE), E_NOT_SUPPORTED);

    p_Timer->function = (void (*)(unsigned long))f_TimerExpired;
    p_Timer->data = (unsigned long)h_Arg;
    if ((msecs*HZ)%1000)
        tmp_jiffies++;
    p_Timer->expires = (jiffies + tmp_jiffies);

    add_timer((struct timer_list *)h_Timer);
}

void XX_SetTimerData(t_Handle h_Timer, t_Handle data)
{
    struct timer_list   *p_Timer = (struct timer_list *)h_Timer;

    p_Timer->data = (unsigned long)data;
}

t_Handle XX_GetTimerData(t_Handle h_Timer)
{
    struct timer_list   *p_Timer = (struct timer_list *)h_Timer;

    return (t_Handle)p_Timer->data;
}

uint32_t   XX_GetExpirationTime(t_Handle h_Timer)
{
    struct timer_list   *p_Timer = (struct timer_list *)h_Timer;

    return (uint32_t)p_Timer->expires;
}

void XX_StopTimer(t_Handle h_Timer)
{
    del_timer((struct timer_list *)h_Timer);
}

void XX_ModTimer(t_Handle h_Timer, uint32_t msecs)
{
    int tmp_jiffies = (msecs*HZ)/1000;

    if ((msecs*HZ)%1000)
        tmp_jiffies++;
    mod_timer((struct timer_list *)h_Timer, jiffies + tmp_jiffies);
}

int XX_TimerIsActive(t_Handle h_Timer)
{
  return timer_pending((struct timer_list *)h_Timer);
}

uint32_t XX_Sleep(uint32_t msecs)
{
    int tmp_jiffies = (msecs*HZ)/1000;

    if ((msecs*HZ)%1000)
        tmp_jiffies++;
    return schedule_timeout(tmp_jiffies);
}

/*BEWARE!!!!! UDelay routine is BUSY WAITTING!!!!!*/
void XX_UDelay(uint32_t usecs)
{
    udelay(usecs);
}

/* TODO: verify that these are correct */
#define MSG_BODY_SIZE       512
typedef t_Error (t_MsgHandler) (t_Handle h_Mod, uint32_t msgId, uint8_t msgBody[MSG_BODY_SIZE]);
typedef void (t_MsgCompletionCB) (t_Handle h_Arg, uint8_t msgBody[MSG_BODY_SIZE]);
t_Error XX_SendMessage(char                 *p_DestAddr,
                       uint32_t             msgId,
                       uint8_t              msgBody[MSG_BODY_SIZE],
                       t_MsgCompletionCB    *f_CompletionCB,
                       t_Handle             h_CBArg);

typedef struct {
    char            *p_Addr;
    t_MsgHandler    *f_MsgHandlerCB;
    t_Handle        h_Mod;
    t_List          node;
} t_MsgHndlr;
#define MSG_HNDLR_OBJECT(ptr)  LIST_OBJECT(ptr, t_MsgHndlr, node)

LIST(msgHndlrList);

static void EnqueueMsgHndlr(t_MsgHndlr *p_MsgHndlr)
{
    uint32_t   intFlags;

    intFlags = XX_DisableAllIntr();
    LIST_AddToTail(&p_MsgHndlr->node, &msgHndlrList);
    XX_RestoreAllIntr(intFlags);
}
/* TODO: add this for multi-platform support
static t_MsgHndlr * DequeueMsgHndlr(void)
{
    t_MsgHndlr *p_MsgHndlr = NULL;
    uint32_t   intFlags;

    intFlags = XX_DisableAllIntr();
    if (!LIST_IsEmpty(&msgHndlrList))
    {
        p_MsgHndlr = MSG_HNDLR_OBJECT(msgHndlrList.p_Next);
        LIST_DelAndInit(&p_MsgHndlr->node);
    }
    XX_RestoreAllIntr(intFlags);

    return p_MsgHndlr;
}
*/
static t_MsgHndlr * FindMsgHndlr(char *p_Addr)
{
    t_MsgHndlr  *p_MsgHndlr;
    t_List      *p_Pos;

    LIST_FOR_EACH(p_Pos, &msgHndlrList)
    {
        p_MsgHndlr = MSG_HNDLR_OBJECT(p_Pos);
        if (strstr(p_MsgHndlr->p_Addr, p_Addr))
            return p_MsgHndlr;
    }

    return NULL;
}

t_Error XX_RegisterMessageHandler   (char *p_Addr, t_MsgHandler *f_MsgHandlerCB, t_Handle h_Mod)
{
    t_MsgHndlr  *p_MsgHndlr;
    uint32_t    len;

    p_MsgHndlr = (t_MsgHndlr*)XX_Malloc(sizeof(t_MsgHndlr));
    if (!p_MsgHndlr)
        RETURN_ERROR(MINOR, E_NO_MEMORY, ("message handler object!!!"));
    memset(p_MsgHndlr, 0, sizeof(t_MsgHndlr));

    len = strlen(p_Addr);
    p_MsgHndlr->p_Addr = (char*)XX_Malloc(len+1);
    strncpy(p_MsgHndlr->p_Addr,p_Addr, (uint32_t)(len+1));

    p_MsgHndlr->f_MsgHandlerCB = f_MsgHandlerCB;
    p_MsgHndlr->h_Mod = h_Mod;
    INIT_LIST(&p_MsgHndlr->node);
    EnqueueMsgHndlr(p_MsgHndlr);

    return E_OK;
}

t_Error XX_UnregisterMessageHandler (char *p_Addr)
{
    t_MsgHndlr *p_MsgHndlr = FindMsgHndlr(p_Addr);
    if (!p_MsgHndlr)
        RETURN_ERROR(MINOR, E_NO_DEVICE, ("message handler not found in list!!!"));

    LIST_Del(&p_MsgHndlr->node);
    XX_Free(p_MsgHndlr->p_Addr);
    XX_Free(p_MsgHndlr);

    return E_OK;
}

t_Error XX_SendMessage(char                 *p_DestAddr,
                       uint32_t             msgId,
                       uint8_t              msgBody[MSG_BODY_SIZE],
                       t_MsgCompletionCB    *f_CompletionCB,
                       t_Handle             h_CBArg)
{
    t_Error     ans;
    t_MsgHndlr  *p_MsgHndlr = FindMsgHndlr(p_DestAddr);
    if (!p_MsgHndlr)
        RETURN_ERROR(MINOR, E_NO_DEVICE, ("message handler not found in list!!!"));

    ans = p_MsgHndlr->f_MsgHandlerCB(p_MsgHndlr->h_Mod, msgId, msgBody);

    if (f_CompletionCB)
        f_CompletionCB(h_CBArg, msgBody);

    return ans;
}

t_Error XX_IpcRegisterMsgHandler(char                   addr[XX_IPC_MAX_ADDR_NAME_LENGTH],
                                 t_IpcMsgHandler        *f_MsgHandler,
                                 t_Handle               h_Module,
                                 uint32_t               replyLength)
{
    UNUSED(addr);UNUSED(f_MsgHandler);UNUSED(h_Module);UNUSED(replyLength);
    return E_OK;
}

t_Error XX_IpcUnregisterMsgHandler(char addr[XX_IPC_MAX_ADDR_NAME_LENGTH])
{
    UNUSED(addr);
    return E_OK;
}


t_Error XX_IpcSendMessage(t_Handle           h_Session,
                          uint8_t            *p_Msg,
                          uint32_t           msgLength,
                          uint8_t            *p_Reply,
                          uint32_t           *p_ReplyLength,
                          t_IpcMsgCompletion *f_Completion,
                          t_Handle           h_Arg)
{
    UNUSED(h_Session); UNUSED(p_Msg); UNUSED(msgLength); UNUSED(p_Reply);
    UNUSED(p_ReplyLength); UNUSED(f_Completion); UNUSED(h_Arg);
    return E_OK;
}

t_Handle XX_IpcInitSession(char destAddr[XX_IPC_MAX_ADDR_NAME_LENGTH],
                           char srcAddr[XX_IPC_MAX_ADDR_NAME_LENGTH])
{
    UNUSED(destAddr); UNUSED(srcAddr);
    return E_OK;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22)
int GetDeviceIrqNum(int irq)
{
    struct device_node  *iPar;
    struct irq_domain   *irqHost;
    uint32_t            hwIrq;

    /* Get the interrupt controller */
    iPar = of_find_node_by_name(NULL, "mpic");
    hwIrq = 0;

    ASSERT_COND(iPar != NULL);
    /* Get the irq host */
    irqHost = irq_find_host(iPar);
    of_node_put(iPar);

    /* Create irq mapping */
    return irq_create_mapping(irqHost, hwIrq);
}
#else
#error "kernel not supported!!!"
#endif    /* LINUX_VERSION_CODE */

void * XX_PhysToVirt(physAddress_t addr)
{
    return UINT_TO_PTR(SYS_PhysToVirt((uint64_t)addr));
}

physAddress_t XX_VirtToPhys(void * addr)
{
    return (physAddress_t)SYS_VirtToPhys(PTR_TO_UINT(addr));
}

void * xx_MallocSmart(uint32_t size, int memPartitionId, uint32_t alignment)
{
    uintptr_t   *returnCode, tmp;

    if (alignment < sizeof(uintptr_t))
        alignment = sizeof(uintptr_t);
    size += alignment + sizeof(returnCode);
    tmp = (uintptr_t)xx_Malloc(size);
    if (tmp == 0)
        return NULL;
    returnCode = (uintptr_t*)((tmp + alignment + sizeof(returnCode)) & ~((uintptr_t)alignment - 1));
    *(returnCode - 1) = tmp;

    return (void*)returnCode;
}

void xx_FreeSmart(void *p)
{
    xx_Free((void*)(*((uintptr_t *)(p) - 1)));
}