/* SPDX-License-Identifier: LGPL-2.1-only OR BSD-3-Clause */
/******************************************************************************
 *
 * This file is provided under a dual license.  When you use or
 * distribute this software, you may choose to be licensed under
 * version 2.1 of the GNU Lesser General Public License
 * ("LGPLv2.1 License") or BSD License.
 *
 * LGPLv2.1 License
 *
 * Copyright(C) 2019 MediaTek Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2.1 of the GNU Lesser General Public
 * License as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html for more
 * details.
 *
 * BSD LICENSE
 *
 * Copyright(C) 2019 MediaTek Inc.
 * All rights reserved.
 *
 * 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 the copyright holder nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "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 THE COPYRIGHT
 * HOLDER OR CONTRIBUTORS 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.
 *
 *****************************************************************************/

/*-----------------------------------------------------------------------------
 *
 * $Author: xiaojun.zhu $
 * $Date: 2015/08/08 $
 * $RCSfile: dmx_if.c,v $
 * $Revision: #3 $
 *
 *---------------------------------------------------------------------------*/

/** @file gcpu_if.c
 *  GCPU driver - public interface
 */

//-----------------------------------------------------------------------------
// Include files
//-----------------------------------------------------------------------------
#include "gcpu.h"

#include "x_lint.h"

LINT_EXT_HEADER_BEGIN

#include "x_assert.h"
#include "x_os.h"

LINT_EXT_HEADER_END


//-----------------------------------------------------------------------------
// Configurations
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Constant definitions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Type definitions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Macro definitions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Static variables
//-----------------------------------------------------------------------------

/// API semaphore
#ifndef CC_MTK_LOADER
static HANDLE_T _hApiSem = NULL_HANDLE;
#endif

//-----------------------------------------------------------------------------
// Static functions
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
/** Enter demux API critical section
 *
 *  @retval void
 */
//-----------------------------------------------------------------------------
static void _ApiLock(void)
{
#ifndef CC_MTK_LOADER
    static BOOL _fgInit = FALSE;

    if (!_fgInit)
    {
        VERIFY(x_sema_create(&_hApiSem, X_SEMA_TYPE_MUTEX, X_SEMA_STATE_UNLOCK) == OSR_OK);
        _fgInit = TRUE;
    }

    VERIFY(x_sema_lock(_hApiSem, X_SEMA_OPTION_WAIT) == OSR_OK);
#endif
}


//-----------------------------------------------------------------------------
/** Leave demux API critical section
 *
 *  @retval void
 */
//-----------------------------------------------------------------------------
static void _ApiUnlock(void)
{
#ifndef CC_MTK_LOADER
    VERIFY(x_sema_unlock(_hApiSem) == OSR_OK);
#endif
}


//-----------------------------------------------------------------------------
// Inter-file functions
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// Public functions
//-----------------------------------------------------------------------------

//
// Initialization
//


//-----------------------------------------------------------------------------
/** DMX_Init
 *  Initialize demux driver
 *
 *  @retval TRUE            Succeed
 *  @retval FALSE           Fail
 */
//-----------------------------------------------------------------------------
BOOL GCPU_Init(UINT32 u4Mode)
{
    BOOL fgRet;

    _ApiLock();
    fgRet = (_GCPU_Init(u4Mode) == S_GCPU_OK)?TRUE:FALSE;
    _ApiUnlock();

    return fgRet;
}


//-----------------------------------------------------------------------------
/** GCPU_Cmd
 */
//-----------------------------------------------------------------------------
INT32 GCPU_Cmd(UPTR u4Handle, UINT32 u4Cmd, void* pvParam)
{
    INT32 i4Ret;

    _ApiLock();
    i4Ret = _GCPU_Cmd(u4Handle, u4Cmd, pvParam);
    _ApiUnlock();

    return i4Ret;
}

//-----------------------------------------------------------------------------
/** GCPU_CmdEx
 */
//-----------------------------------------------------------------------------
INT32 GCPU_CmdEx(UPTR u4Handle, UINT32 u4Cmd, void* pvParam, BOOL fgIOMMU)
{
    INT32 i4Ret;

    _ApiLock();
    i4Ret = _GCPU_CmdEx(u4Handle, u4Cmd, pvParam, fgIOMMU);
    _ApiUnlock();

    return i4Ret;
}

//-----------------------------------------------------------------------------
/** GCPU_Cmd
 */
//-----------------------------------------------------------------------------
INT32 GCPU_CmdSw(UPTR u4Handle, UINT32 u4Cmd, void* pvParam)
{
    INT32 i4Ret;

    _ApiLock();
    i4Ret = _GCPU_CmdSw(u4Handle, u4Cmd, pvParam);
    _ApiUnlock();

    return i4Ret;
}

//-----------------------------------------------------------------------------
/** GCPU_AllocSecureSlot
 */
//-----------------------------------------------------------------------------
UINT32 GCPU_AllocSecureSlot(UINT32 u4Size)
{
    UINT32 u4Ret;

    _ApiLock();
    u4Ret = _GCPU_AllocSecureSlot(u4Size);
    _ApiUnlock();

    return u4Ret;
}


//-----------------------------------------------------------------------------
/** GCPU_FreeSecureSlot
 */
//-----------------------------------------------------------------------------
INT32 GCPU_FreeSecureSlot(UINT32 u4SlotPtr)
{
    INT32 i4Ret;

    _ApiLock();
    i4Ret = _GCPU_FreeSecureSlot(u4SlotPtr);
    _ApiUnlock();

    return i4Ret;
}


//-----------------------------------------------------------------------------
/** GCPU_SetSecureSlot
 */
//-----------------------------------------------------------------------------
INT32 GCPU_SetSecureSlot(UINT32 u4SlotPtr, UINT8* pu1Data, UINT32 u4Size)
{
    INT32 i4Ret;

    _ApiLock();
    i4Ret = _GCPU_SetSecureSlot(u4SlotPtr, pu1Data, u4Size);
    _ApiUnlock();

    return i4Ret;
}


#if defined(CC_SUPPORT_STR) && !defined(CC_UBOOT) && !defined(CC_MTK_LOADER)
//-----------------------------------------------------------------------------
/** GCPU_pm_str_suspend
 */
//-----------------------------------------------------------------------------
void GCPU_pm_str_suspend(void)
{
    _ApiLock();
    _GCPU_pm_str_suspend();
    //_ApiUnlock();
}


//-----------------------------------------------------------------------------
/** GCPU_pm_str_resume
 */
//-----------------------------------------------------------------------------
void GCPU_pm_str_resume(void)
{
    //_ApiLock();
    _GCPU_pm_str_resume();
    _ApiUnlock();
}
#endif // CC_SUPPORT_STR

#ifdef CC_MTK_LOADER
BOOL GCPU_LoaderInit(UINT32 u4Mode)
{
    BOOL fgRet;

    _ApiLock();
    fgRet = (_GCPU_LoaderInit(u4Mode) == S_GCPU_OK)?TRUE:FALSE;
    _ApiUnlock();

    return fgRet;
}


BOOL GCPU_SetAesKey(UINT8 *pu1Key, KEY_BIT_LEN eKeyBitLen, UINT8 *pu1Iv)
{
    BOOL fgRet;

    _ApiLock();
    fgRet = _GCPU_SetAesKey(pu1Key, eKeyBitLen, pu1Iv);
    _ApiUnlock();

    return fgRet;
}


BOOL GCPU_AesDecrypt(UINT32 u4InBufStart, UINT32 u4OutBufStart, UINT32 u4BufSize)
{
    BOOL fgRet;

    _ApiLock();
    fgRet = _GCPU_AesDecrypt(u4InBufStart, u4OutBufStart, u4BufSize);
    _ApiUnlock();

    return fgRet;
}


BOOL GCPU_AesEncrypt(UINT32 u4InBufStart, UINT32 u4OutBufStart, UINT32 u4BufSize)
{
    BOOL fgRet;

    _ApiLock();
    fgRet = _GCPU_AesEncrypt(u4InBufStart, u4OutBufStart, u4BufSize);
    _ApiUnlock();

    return fgRet;
}


BOOL GCPU_SHA256(UINT8 *pu1Buf, UINT32 u4Size, UINT8 Message_Digest[32])
{
    BOOL fgRet;

    _ApiLock();
    fgRet = _GCPU_SHA256(pu1Buf, u4Size, Message_Digest);
    _ApiUnlock();

    return fgRet;
}
#endif // CC_MTK_LOADER

BOOL GCPU_SHA256_Init(UPTR u4Handle, BOOL fgIOMMU)
{
    BOOL fgRet;

    _ApiLock();
    fgRet = _GCPU_SHA256_Init(u4Handle, fgIOMMU);
    _ApiUnlock();

    return fgRet;
}

BOOL GCPU_SHA256_Update(UPTR u4Handle, UINT8 *pu1Buf, UINT32 u4Size, BOOL fgLastPacket)
{
    BOOL fgRet;

    _ApiLock();
    fgRet = _GCPU_SHA256_Update(u4Handle, pu1Buf, u4Size, fgLastPacket, 0);
    _ApiUnlock();

    return fgRet;
}

BOOL GCPU_SHA256_UpdateEx(UPTR u4Handle, UINT8 *pu1Buf, UINT32 u4Size, BOOL fgLastPacket, UINT32 u4MmuTblAddr)
{
    BOOL fgRet;

    _ApiLock();
    fgRet = _GCPU_SHA256_Update(u4Handle, pu1Buf, u4Size, fgLastPacket, u4MmuTblAddr);
    _ApiUnlock();

    return fgRet;
}

BOOL GCPU_SHA256_Final(UPTR u4Handle, UINT8 au1Digest[32])
{
    BOOL fgRet;

    _ApiLock();
    fgRet = _GCPU_SHA256_Final(u4Handle, au1Digest);
    _ApiUnlock();

    return fgRet;
}

BOOL GCPU_RSA_decrypt(UINT8 *pu1Signature, UINT8 *pu1PublicKey, UINT8 *pu1CheckSum, UINT32 e, UINT32 u4Len)
{
    BOOL fgRet;

    _ApiLock();
    fgRet = _GCPU_RSA_decrypt(pu1Signature, pu1PublicKey, pu1CheckSum, e, u4Len);
    _ApiUnlock();

    return fgRet;
}

INT32 GCPU_AESGCM(UINT32 u4Handle,  void* prParam, BOOL fgIOMMU)
{
	INT32 i4Ret;
	
    _ApiLock();
 	i4Ret = _GCPU_AESGCM(u4Handle, prParam, fgIOMMU);
    _ApiUnlock();

	return i4Ret;
}

INT32 GCPU_AESXTS(UINT32 u4Handle, AES_XTS_PARAM_T *prParam, BOOL fgIOMMU)
{
    INT32 i4Ret;

    _ApiLock();
    i4Ret = _GCPU_AESXTS(u4Handle, prParam, fgIOMMU);
    _ApiUnlock();

    return i4Ret;
}
