/*
* Copyright (c) 2014 - 2016 MediaTek Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files
* (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef __DR_SEC_MEM_API_H__
#define __DR_SEC_MEM_API_H__

#include "tlmem_api.h"

#define SECMEM_64BIT_PHYS_SUPPORT

#define SECMEM_PL_RESERVED_M4U      0
#define SECMEM_PL_RESERVED_CMDQ     1
#define SECMEM_PL_RESERVED_SPI      2

#ifdef SECMEM_DEBUG
#ifdef SECMEM_64BIT_PHYS_SUPPORT

int drSecMemAlloc_Debug(uint64_t * handle, uint64_t size, uint64_t alignment, uint8_t *owner, uint32_t id);
int drSecMemRef_Debug(uint64_t handle, uint32_t *refcount, uint8_t *owner, uint32_t id);
int drSecMemUnref_Debug(uint64_t handle, uint32_t *refcount, uint8_t *owner, uint32_t id);
int drSecMemAllocPA_Debug(uint64_t * phy_addr, uint64_t size, uint64_t alignment, uint8_t *owner, uint32_t id);
int drSecMemAllocZero_Debug(uint64_t * handle, uint64_t size, uint64_t alignment, uint8_t *owner, uint32_t id);
int drSecMemRefPA_Debug(uint64_t phy_addr, uint32_t *refcount, uint8_t *owner, uint32_t id);
int drSecMemUnrefPA_Debug(uint64_t phy_addr, uint32_t *refcount, uint8_t *owner, uint32_t id);
int drSecMemAllocTBL_Debug(uint64_t *handle, uint64_t size, uint64_t alignment, uint8_t *owner, uint32_t id);
int drSecMemUnrefTBL_Debug(uint64_t handle, uint32_t *refcount, uint8_t *owner, uint32_t id);

#define drSecMemAlloc(hndl, size, align)        drSecMemAlloc_Debug(hndl, size, align, __FILE__, __LINE__)
#define drSecMemRef(hndl, refcnt)               drSecMemRef_Debug(hndl, refcnt, __FILE__, __LINE__)
#define drSecMemUnref(hndl, refcnt)             drSecMemUnref_Debug(hndl, refcnt, __FILE__, __LINE__)
#define drSecMemAllocPA(phyaddr, size, align)   drSecMemAllocPA_Debug(phyaddr, size, align, __FILE__, __LINE__)
#define drSecMemAllocZero(hndl, size, align)    drSecMemAllocZero_Debug(hndl, size, align, __FILE__, __LINE__)
#define drSecMemRefPA(phyaddr, refcnt)          drSecMemRefPA_Debug(phyaddr, refcnt, __FILE__, __LINE__)
#define drSecMemUnrefPA(phyaddr, refcnt)        drSecMemUnrefPA_Debug(phyaddr, refcnt, __FILE__, __LINE__)
#define drSecMemAllocTBL(hndl, size, align)     drSecMemAllocTBL_Debug(hndl, size, align, __FILE__, __LINE__)
#define drSecMemUnrefTBL(hndl, refcnt)          drSecMemUnrefTBL_Debug(hndl, refcnt, __FILE__, __LINE__)

#else /* !SECMEM_64BIT_PHYS_SUPPORT */

int drSecMemAlloc_Debug(uint32_t * handle, uint32_t size, uint32_t alignment, uint8_t *owner, uint32_t id);
int drSecMemRef_Debug(uint32_t handle, uint32_t *refcount, uint8_t *owner, uint32_t id);
int drSecMemUnref_Debug(uint32_t handle, uint32_t *refcount, uint8_t *owner, uint32_t id);
int drSecMemAllocPA_Debug(uint32_t * phy_addr, uint32_t size, uint32_t alignment, uint8_t *owner, uint32_t id);
int drSecMemAllocZero_Debug(uint32_t * handle, uint32_t size, uint32_t alignment, uint8_t *owner, uint32_t id);
int drSecMemRefPA_Debug(uint32_t phy_addr, uint32_t *refcount, uint8_t *owner, uint32_t id);
int drSecMemUnrefPA_Debug(uint32_t phy_addr, uint32_t *refcount, uint8_t *owner, uint32_t id);
int drSecMemAllocTBL_Debug(uint32_t *handle, uint32_t size, uint32_t alignment, uint8_t *owner, uint32_t id);
int drSecMemUnrefTBL_Debug(uint32_t handle, uint32_t *refcount, uint8_t *owner, uint32_t id);

#define drSecMemAlloc(hndl, size, align)        drSecMemAlloc_Debug(hndl, size, align, __FILE__, __LINE__)
#define drSecMemRef(hndl, refcnt)               drSecMemRef_Debug(hndl, refcnt, __FILE__, __LINE__)
#define drSecMemUnref(hndl, refcnt)             drSecMemUnref_Debug(hndl, refcnt, __FILE__, __LINE__)
#define drSecMemAllocPA(phyaddr, size, align)   drSecMemAllocPA_Debug(phyaddr, size, align, __FILE__, __LINE__)
#define drSecMemAllocZero(hndl, size, align)    drSecMemAllocZero_Debug(hndl, size, align, __FILE__, __LINE__)
#define drSecMemRefPA(phyaddr, refcnt)          drSecMemRefPA_Debug(phyaddr, refcnt, __FILE__, __LINE__)
#define drSecMemUnrefPA(phyaddr, refcnt)        drSecMemUnrefPA_Debug(phyaddr, refcnt, __FILE__, __LINE__)
#define drSecMemAllocTBL(hndl, size, align)     drSecMemAllocTBL_Debug(hndl, size, align, __FILE__, __LINE__)
#define drSecMemUnrefTBL(hndl, refcnt)          drSecMemUnrefTBL_Debug(hndl, refcnt, __FILE__, __LINE__)

#endif /* !SECMEM_64BIT_PHYS_SUPPORT */
#else /* SECMEM_DEBUG */
#ifdef SECMEM_64BIT_PHYS_SUPPORT

int drSecMemAlloc(uint64_t * handle, uint64_t size, uint64_t alignment);
int drSecMemRef(uint64_t handle, uint32_t *refcount);
int drSecMemUnref(uint64_t handle, uint32_t *refcount);
int drSecMemAllocPA(uint64_t * phy_addr, uint64_t size, uint64_t alignment);
int drSecMemAllocZero(uint64_t *handle, uint64_t size, uint64_t alignment);
int drSecMemRefPA(uint64_t phy_addr, uint32_t *refcount);
int drSecMemUnrefPA(uint64_t phy_addr, uint32_t *refcount);
int drSecMemAllocTBL(uint64_t *handle, uint64_t size, uint64_t alignment);
int drSecMemUnrefTBL(uint64_t handle, uint32_t *refcount);

#else /* !SECMEM_64BIT_PHYS_SUPPORT */

int drSecMemAlloc(uint32_t * handle, uint32_t size, uint32_t alignment);
int drSecMemRef(uint32_t handle, uint32_t *refcount);
int drSecMemUnref(uint32_t handle, uint32_t *refcount);
int drSecMemAllocPA(uint32_t * phy_addr, uint32_t size, uint32_t alignment);
int drSecMemAllocZero(uint32_t *handle, uint32_t size, uint32_t alignment);
int drSecMemRefPA(uint32_t phy_addr, uint32_t *refcount);
int drSecMemUnrefPA(uint32_t phy_addr, uint32_t *refcount);
int drSecMemAllocTBL(uint32_t *handle, uint32_t size, uint32_t alignment);
int drSecMemUnrefTBL(uint32_t handle, uint32_t *refcount);

#endif /* !SECMEM_64BIT_PHYS_SUPPORT */
#endif /* SECMEM_DEBUG */

int drSecMemQuery(tlApimem_ptr mem_result_ptr);
int drSecMemQueryArray(tlApimem_ptr mem_result_ptr, uint32_t len);
int drSecMemQueryPA(tlApimem_ptr mem_result_ptr);
int drSecMemQueryArrayPA(tlApimem_ptr mem_result_ptr, uint32_t len);
int drSecMemDumpInfo(void);
int drSecMemQuerySharedMem(uint32_t *status);
int drSecMemQueryPlReservedMem(uint32_t index, uint32_t *addr, uint32_t *size);

#ifdef SECMEM_64BIT_PHYS_SUPPORT
int drSecMemQueryPool(uint64_t *start_addr, uint64_t *size);
int drSecMemEnable(uint64_t start_addr, uint64_t size);
int drSecMemDisable(uint64_t *size);
int drSecMemAllocated(uint64_t *size);
#else
int drSecMemQueryPool(uint32_t *start_addr, uint32_t *size);
int drSecMemEnable(uint32_t start_addr, uint32_t size);
int drSecMemDisable(uint32_t *size);
int drSecMemAllocated(uint32_t *size);
#endif /* !SECMEM_64BIT_PHYS_SUPPORT */

#define drSecMemFree(fmt, args...) drSecMemUnref(fmt, ##args)
#define drSecMemFreePA(fmt, args...) drSecMemUnrefPA(fmt, ##args)

#endif // __DR_SEC_MEM_API_H__
