/******************************************************************************/
/* Component Name   Customer-implement component                              */
/******************************************************************************/
/* Product      : ICU-M Firmware                                              */
/******************************************************************************/
/* Copyright(C) 2021-2023 Renesas Electronics Corporation.                    */
/******************************************************************************/

#ifndef USER_ICUMIF_API_H_
#define USER_ICUMIF_API_H_

/* Bit pattern of current program status register */
#define FIQ_BIT                        (0x00000040U)
#define IRQ_BIT                        (0x00000080U)
#define FIQ_DISABLE                    (0x00000040U)
#define IRQ_DISABLE                    (0x00000080U)

#if defined(TARGET_DEVICE_S4X)
#define CR52_CPU_NUM (1u)
#define CA_CPU_NUM (8u)
#else /* TARGET_DEVICE_V4H */
#define CR52_CPU_NUM (3u)
#define CA_CPU_NUM (4u)
#endif

void USER_ICUMIF_FlushDCache(uint32_t addr, uint32_t size);
void USER_ICUMIF_InvalidateDCache(uint32_t addr, uint32_t size);
uint32_t USER_ICUMIF_GetMyPEID(void);
void USER_ICUMIF_GetLock(uintptr_t *p_saved_psw);
void USER_ICUMIF_ReleaseLock(uintptr_t saved_psw);
void USER_ICUMIF_Sync(void);

#if defined(TARGET_CORTEX_A) && defined(AARCH64)
#define USER_ICUMIF_GET_LOCK(X) \
    do\
    {\
        __asm ("mrs %0, DAIF" : "=r" (X));\
        if ((IRQ_DISABLE != ((X) & IRQ_BIT)) || (FIQ_DISABLE != ((X) & FIQ_BIT)))\
        {\
            __asm ("msr DAIFSet, #0x3");\
        }\
    } while (0)
#else
#define USER_ICUMIF_GET_LOCK(X) \
    do\
    {\
        __asm volatile ("mrs %0, cpsr" : "=r" (X));\
        if ((IRQ_DISABLE != ((X) & IRQ_BIT)) || (FIQ_DISABLE != ((X) & FIQ_BIT)))\
        {\
            __asm ("cpsid if");\
        }\
    } while (0)
#endif

#if defined(TARGET_CORTEX_A) && defined(AARCH64)
#define USER_ICUMIF_RELEASE_LOCK(X) \
    do\
    {\
        if (FIQ_DISABLE != ((X) & FIQ_BIT))\
        {\
            __asm ("msr DAIFClr, #0x1");\
        }\
        else\
        {\
        }\
        if (IRQ_DISABLE != ((X) & IRQ_BIT))\
        {\
            __asm ("msr DAIFClr, #0x2");\
        }\
        else\
        {\
        }\
    } while (0)
#else
#define USER_ICUMIF_RELEASE_LOCK(X) \
    do\
    {\
        if (FIQ_DISABLE != ((X) & FIQ_BIT))\
        {\
            __asm ("cpsie f");\
        }\
        else\
        {\
        }\
        if (IRQ_DISABLE != ((X) & IRQ_BIT))\
        {\
            __asm ("cpsie i");\
        }\
        else\
        {\
        }\
    } while (0)
#endif


#endif /* USER_ICUMIF_API_H_ */

