//<MStar Software>
//******************************************************************************
// MStar Software
// Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
// All software, firmware and related documentation herein ("MStar Software") are
// intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
// law, including, but not limited to, copyright law and international treaties.
// Any use, modification, reproduction, retransmission, or republication of all
// or part of MStar Software is expressly prohibited, unless prior written
// permission has been granted by MStar.
//
// By accessing, browsing and/or using MStar Software, you acknowledge that you
// have read, understood, and agree, to be bound by below terms ("Terms") and to
// comply with all applicable laws and regulations:
//
// 1. MStar shall retain any and all right, ownership and interest to MStar
//    Software and any modification/derivatives thereof.
//    No right, ownership, or interest to MStar Software and any
//    modification/derivatives thereof is transferred to you under Terms.
//
// 2. You understand that MStar Software might include, incorporate or be
//    supplied together with third party`s software and the use of MStar
//    Software may require additional licenses from third parties.
//    Therefore, you hereby agree it is your sole responsibility to separately
//    obtain any and all third party right and license necessary for your use of
//    such third party`s software.
//
// 3. MStar Software and any modification/derivatives thereof shall be deemed as
//    MStar`s confidential information and you agree to keep MStar`s
//    confidential information in strictest confidence and not disclose to any
//    third party.
//
// 4. MStar Software is provided on an "AS IS" basis without warranties of any
//    kind. Any warranties are hereby expressly disclaimed by MStar, including
//    without limitation, any warranties of merchantability, non-infringement of
//    intellectual property rights, fitness for a particular purpose, error free
//    and in conformity with any international standard.  You agree to waive any
//    claim against MStar for any loss, damage, cost or expense that you may
//    incur related to your use of MStar Software.
//    In no event shall MStar be liable for any direct, indirect, incidental or
//    consequential damages, including without limitation, lost of profit or
//    revenues, lost or damage of data, and unauthorized system use.
//    You agree that this Section 4 shall still apply without being affected
//    even if MStar Software has been modified by MStar in accordance with your
//    request or instruction for your use, except otherwise agreed by both
//    parties in writing.
//
// 5. If requested, MStar may from time to time provide technical supports or
//    services in relation with MStar Software to you for your use of
//    MStar Software in conjunction with your or your customer`s product
//    ("Services").
//    You understand and agree that, except otherwise agreed by both parties in
//    writing, Services are provided on an "AS IS" basis and the warranty
//    disclaimer set forth in Section 4 above shall apply.
//
// 6. Nothing contained herein shall be construed as by implication, estoppels
//    or otherwise:
//    (a) conferring any license or right to use MStar name, trademark, service
//        mark, symbol or any other identification;
//    (b) obligating MStar or any of its affiliates to furnish any person,
//        including without limitation, you and your customers, any assistance
//        of any kind whatsoever, or any information; or
//    (c) conferring any license or right under any intellectual property right.
//
// 7. These terms shall be governed by and construed in accordance with the laws
//    of Taiwan, R.O.C., excluding its conflict of law rules.
//    Any and all dispute arising out hereof or related hereto shall be finally
//    settled by arbitration referred to the Chinese Arbitration Association,
//    Taipei in accordance with the ROC Arbitration Law and the Arbitration
//    Rules of the Association by three (3) arbitrators appointed in accordance
//    with the said Rules.
//    The place of arbitration shall be in Taipei, Taiwan and the language shall
//    be English.
//    The arbitration award shall be final and binding to both parties.
//
//******************************************************************************
//<MStar Software>
////////////////////////////////////////////////////////////////////////////////
//
// Copyright (c) 2008-2009 MStar Semiconductor, Inc.
// All rights reserved.
//
// Unless otherwise stipulated in writing, any and all information contained
// herein regardless in any format shall remain the sole proprietary of
// MStar Semiconductor Inc. and be kept in strict confidence
// ("MStar Confidential Information") by the recipient.
// Any unauthorized act including without limitation unauthorized disclosure,
// copying, use, reproduction, sale, distribution, modification, disassembling,
// reverse engineering and compiling of the contents of MStar Confidential
// Information is unlawful and strictly prohibited. MStar hereby reserves the
// rights to any and all damages, losses, costs and expenses resulting therefrom.
//
////////////////////////////////////////////////////////////////////////////////

#ifndef _MMAP_INFO_H_
#define _MMAP_INFO_H_

#if(REFINE_PARTITION == 0)
#define MMAP_PATH                      "/Customer/mmap.ini"
#define MMAP_PATH_S                    "/Customer/mmaps.ini"
#else
#define MMAP_PATH                      "/config/mmap.ini"
#define MMAP_PATH_S                    "/config/mmaps.ini"
#endif


typedef unsigned char       MMAP_BOOL;
typedef unsigned char       MMAP_U8;    // 1 byte
typedef unsigned short      MMAP_U16;   // 2 bytes
typedef unsigned long       MMAP_U32;   // 4 bytes
typedef unsigned long long  MMAP_U64;   // 8 bytes
/**********************************************************************************/
/* Warning , MMAP static enumerate table only can be added from the end of last element.
 *  Please contact Ted.Chen if you wanna to modify this table
 */

#define MMAP_MAX      160
#define MMAP_ID_COUNT 159

/// MMapp item enum
typedef enum
{
    E_MMAP_ID_INVALID = 0,
    E_MMAP_ID_VDEC_CPU,   //  1
    E_MMAP_ID_VDEC_FRAMEBUFFER, //  2
    E_MMAP_ID_VDEC_BITSTREAM,//  3
    E_MMAP_ID_XC_MAIN_FB,//  4
    E_MMAP_ID_XC_SUB_FB,//  5
    E_MMAP_ID_PM51_VAR_MEM,//  6
    E_MMAP_ID_GOP_REGDMA,//  7
    E_MMAP_ID_DMX_SECBUF,//  8
    E_MMAP_ID_MHEG5_CI_PLUS_BUFFER,//  9
    E_MMAP_ID_VDEC_FRAMEBUFFER_SD,//  10
    E_MMAP_ID_VDEC_BITSTREAM_SD,//  11

    E_DFB_JPD_READ,//  12
    E_DFB_JPD_WRITE,//  13
    E_DFB_JPD_INTERNAL,//  14
    E_MMAP_ID_VE,//  15
    E_MMAP_ID_JPD_WRITE,//  16
    E_MMAP_ID_JPD_READ,//  17
    E_MMAP_ID_PHOTO_INTER,//  18
    E_MMAP_ID_PHOTO_DISPLAY,//  19
    E_MMAP_ID_PHOTO_MPO_INTER,//  20
    E_MMAP_ID_DUMMY16,//  21
    E_MMAP_ID_COPROCESSOR,//  22

    E_MMAP_ID_PHOTO_SHAREMEM,     //  23
    E_MMAP_ID_VDPLAYER_DATA,      //  24
    E_MMAP_ID_VDPLAYER_BITSTREAM, //  25
    E_MMAP_ID_VDPLAYER_BITSTREAM_EXT, //  26
    E_MMAP_ID_DUMMY20,           //  27
    E_MMAP_ID_MAD_DEC,            //  28
    E_MMAP_ID_MAD_SE,             //  29
    E_MMAP_ID_TTX,               //  30
    E_MMAP_ID_TTX_VE,             //  31
    E_MMAP_ID_DUMMY21,           //  32

    E_LX_MEM,//  33
    E_LX_MEM2,//  34
    E_EMAC_MEM,//  35
    E_MST_GEVQ,//  36
    E_MST_GOP_REGDMA,//  37
    E_DFB_FRAMEBUFFER,//  38
    //According to 1 MIU 128MB request, add following ID -- BELOW
    E_MMAP_ID_PVR_DOWNLOAD,//  39
    E_MMAP_ID_PVR_UPLOAD,//  40
    E_MMAP_ID_PVR_BUFFER,//  41
    E_MMAP_ID_PVR_THUMBNAIL,//  42

    E_MMAP_ID_PVR_BROWSER_BUFFER,//  43
    E_MMAP_ID_COMB_3D_BUF,//  44
    E_MMAP_ID_MM_DIVX_FONT_TBL,//  45
    E_MMAP_ID_VDPLAYER_SUBTITLE_BITSTREAM_BUFF,//  46
    E_MMAP_ID_MM_SUBTITLE_BUFFER,//  47
    E_MMAP_ID_MHEG5_BUFFER,//  48
    E_MMAP_ID_DUMMY17,//  49
    E_MMAP_ID_DUMMY18,//  50
    E_MMAP_ID_EVENTDB_SDRAM,//  51
    E_MMAP_ID_EPGEXTDB_SDRAM,//  52

    E_MMAP_ID_DOWNLOAD_BUFFER,//  53
    E_MMAP_ID_DUMMY1,//  54
    E_MMAP_ID_DUMMY2,//  55
    E_MMAP_ID_DUMMY3,//  56
    E_MMAP_ID_DUMMY4,//  57
    E_MMAP_ID_DUMMY5,//  58
    E_MMAP_ID_DUMMY6,//  59
    E_MMAP_ID_DUMMY7,//  60
    E_MMAP_ID_DUMMY8,//  61
    E_MMAP_ID_VDPLAYER,//  62

    E_MMAP_ID_PHOTO,//  63
    E_MMAP_ID_DUMP_BUFFER,//  64
    E_MMAP_ID_DUMP_BUFFER2,//  65
    E_MMAP_ID_SCALER_DNR_SUB_BUF,//  66
    E_MMAP_ID_FLUSH_BUFFER,//  67
    E_MMAP_ID_MHEG5_JPEG_BUFFER,//  68

    E_MMAP_ID_MAD_R2,//  69
    E_MMAP_ID_MFE,//  70
    E_MMAP_ID_DMX_VQUEUE,//  71
    E_MMAP_ID_XC_MLOAD,//  72
    E_MMAP_ID_CC,//  73

    //According to 1 MIU 128MB request, add following ID -- ABOVE
    E_MMAP_ID_DIP,//  74
    E_MMAP_ID_3D_BUF0,//  75
    E_MMAP_ID_3D_BUF1,//  76
    E_MMAP_ID_3D_COLOR,//  77
    E_MMAP_ID_3D_CMD,//  78
    E_MMAP_ID_3D_VTX,//  79
    E_MMAP_ID_3D_DWIN,//  80
    E_MMAP_ID_3D_MPOOL,//  81
    E_MMAP_ID_3D_MPOOL_M1,//  82
    E_MMAP_ID_CAPTURESCREEN,//  83

    E_MMAP_ID_VDEC_SUB_FRAMEBUFFER,//  84
    E_MMAP_ID_VDEC_SUB_BITSTREAM,//  85
    E_MMAP_ID_VDEC_MVC_FRAMEBUFFER,//  86
    E_MMAP_ID_VDEC_MVC_BITSTREAM,//  87
    /**Secure Boot Requirement**/
    E_MMAP_ID_HW_AES_BUF,//  88
    E_MMAP_ID_XC_2DTO3D_DD_BUF,//  89
    E_MMAP_ID_XC_2DTO3D_DR_BUF,//  90

    //FRC
    E_MMAP_ID_FRC_MEMC,//  91
    E_MMAP_ID_FRC_OD,//  92
    E_MMAP_ID_FRC_LD,//  93
    E_MMAP_ID_FRC_ME1,//  94
    E_MMAP_ID_FRC_ME2,//  95
    E_MMAP_ID_FRC_2D3D_Render,//  96
    E_MMAP_ID_FRC_2D3D_Render_Detection,//  97
    E_MMAP_ID_FRC_Halo,//  98
    E_MMAP_ID_FRC_R2,//  99

    E_MMAP_ID_HSL,//  100

    E_MMAP_ID_PM51_USAGE_MEM,//  101
    E_MMAP_ID_PM51_CODE_MEM,//  102
    E_MMAP_ID_PM51_LOADER_MEM,//  103

    E_MMAP_ID_PMEM0,//  104
    E_MMAP_ID_PMEM1,//  105
    E_MMAP_ID_GOP_FB,//  106
    E_DFB_FRAMEBUFFER2,//  107

    E_MMAP_ID_TCON_OD,//  108
    E_MMAP_ID_TCON_OD_LSB,//  109

    E_MMAP_ID_XC_MCDI_ME1,//  110
    E_MMAP_ID_XC_MCDI_ME2,//  111

    E_MMAP_ID_LOCAL_DIMMING, //  112

    E_MMAP_ID_XC_2DTO3D_DS_DD_BUF,//  113
    E_MMAP_ID_XC_2DTO3D_DS_DR_BUF,//  114

    E_MMAP_ID_NET_APP,//  115

    E_MMAP_ID_NUTTX_MEM,//  116
    E_MMAP_ID_VDEC_FW,//  117
    E_MMAP_ID_ADEC_FW,//  118
    E_SECURE_SHM,//  119
    E_SECURE_UPDATE_AREA,//  120
    E_SECURE_TSP,//  121
    E_MMAP_ID_MAD_COMMON,//  122
    E_MMAP_ID_VDEC_SHARE_MEM,//  123

    E_MMAP_ID_GOP_4K2K,//  124
    E_MMAP_ID_XC_DS,//  125

    E_MMAP_ID_DMX_TWINTUNER_BUFFER,//  126
    E_MMAP_ID_GOP_FB2,//  127
    E_MMAP_ID_CCVBI_RINGBUFFER_START_AVAILABLE,//  128
    E_MMAP_ID_DMD_ISDBT_AVAILABLE,//  129

    E_MMAP_ID_XC_MAIN_FB_WEB_AP,//  130
    E_MMAP_ID_WEB_AP_CO_BUFFER_XC,//  131
    E_MMAP_ID_OD,//  132
    E_MMAP_ID_VDEC_FRAMEBUFFER_WEB_AP,//  133
    E_MMAP_ID_VDEC_BITSTREAM_WEB_AP,//  134
    E_MMAP_ID_WEB_AP_CO_BUFFER_VDEC_1,//  135
    E_MMAP_ID_VDPLAYER_SUBTITLE_BS_DUMMY,//  136
    E_MMAP_ID_MM_SUBTITLE_DUMMY,//  137
    E_MMAP_ID_WEB_AP_CO_BUFFER_VDEC_2,//  138
    E_MMAP_ID_DTMB_IL_BUF,//  139

    //for HDMI/YPBPR XC framebuffer use
    E_MMAP_ID_XC_HDMI_FB,//  140

    E_MMAP_ID_PHOTO_MPO_INTER_DUMMY,//  141
    E_MMAP_ID_MIRACAST_BUFFER,//  142

    //for FBL
    E_MMAP_ID_DUMMY22,//  143
    E_MMAP_ID_DUMMY23,//  144
    E_MMAP_ID_DUMMY24,//  145
    E_MMAP_ID_DUMMY25,//  146
    E_MMAP_ID_DUMMY26,//  147
    E_MMAP_ID_DUMMY27,//  148
    E_MMAP_ID_DUMMY28,//  149
    E_MMAP_ID_DUMMY29,//  150
    E_MMAP_ID_VDEC_FRAMEBUFFER_WEB_AP_DUMMY,//  151
    E_MMAP_ID_VDEC_FRAMEBUFFER_WEB_AP_1080,//  152

    E_MMAP_ID_VDPLAYER_DATA_WEB_AP,//  153
    E_MMAP_ID_VDPLAYER_BITSTREAM_EXT_WEB_AP,//  154
    E_MMAP_ID_VDPLAYER_SEAMLESS_WEB_AP,//  155

    E_MMAP_ID_DUMMY30, //156
    E_MMAP_ID_DWIN_CAPTURE, //157

    E_MMAP_ID_MAX   //Must be placed at the end

} EN_MMAP_ID;


typedef struct MMInfo_s
{
    MMAP_U32    total_mem_size;
    MMAP_U32    miu0_mem_size;
    MMAP_U32    miu1_mem_size;
    MMAP_U32    miu_boundary;
    MMAP_U32    n_mmap_items;
} __attribute__((packed))MMInfo_t;

typedef struct MMapInfo_s
{
    MMAP_U32    u32gID;
    MMAP_U32    u32Addr;
    MMAP_U32    u32Size;
    MMAP_U8     u8Layer;
    MMAP_U32    u32Align;
    MMAP_U32    u32MemoryType;
    MMAP_U32    b_is_miu0;
} __attribute__((packed))MMapInfo_t;

typedef struct
{
    MMInfo_t m_MMinfo;
    MMapInfo_t *m_MMtable;
} MMAPCfgBlock_t;


class MMAPInfo
{
public:
    // ------------------------------------------------------------
    // public operations
    // ------------------------------------------------------------


    //-------------------------------------------------------------------------------------------------
    /// Destructor of MSrv_Control.
    /// @param  None
    /// @return  None
    //-------------------------------------------------------------------------------------------------
    ~MMAPInfo();


    //-------------------------------------------------------------------------------------------------
    /// Get object of MMAPInfo, if null, new it
    /// @param  None
    /// @return  MMAPInfo*                      \b objcet pointer
    //-------------------------------------------------------------------------------------------------
    static MMAPInfo* GetInstance(void);

    //-------------------------------------------------------------------------------------------------
    /// Interface for modifying system configuration block via MSrv
    /// @param  MMAPCfgBlock_t *                      \b IN: mmap config block
    /// @return  None
    //-------------------------------------------------------------------------------------------------
    void SaveMMAPCfgBlock(const MMAPCfgBlock_t* const);

    //-------------------------------------------------------------------------------------------------
    /// Get mmap
    /// @param  EN_MMAP_ID                         \b IN: id
    /// @return  MMapInfo_t *                          \b mmap info pointer
    //-------------------------------------------------------------------------------------------------
    MMapInfo_t * get_mmap(EN_MMAP_ID id);

    //-------------------------------------------------------------------------------------------------
    /// get mmap info
    /// @param  None
    /// @return  MMInfo_t &                         \b mm info reference
    //-------------------------------------------------------------------------------------------------
    MMInfo_t & get_mminfo();

    //-------------------------------------------------------------------------------------------------
    /// get mmap infos
    /// @param  None
    /// @return  MMInfo_t &                         \b mm info reference
    //-------------------------------------------------------------------------------------------------

    MMInfo_t & get_mminfos();

    //-------------------------------------------------------------------------------------------------
    /// get mmap info
    /// @param  None
    /// @return  MMapInfo_t *                         \b mm info reference
    //-------------------------------------------------------------------------------------------------

    MMapInfo_t * get_pmmapinfo();

    //-------------------------------------------------------------------------------------------------
    /// get mmap info
    /// @param  None
    /// @return  MMapInfo_t *                         \b mm info reference
    //-------------------------------------------------------------------------------------------------

    MMapInfo_t * get_pmmapinfos();

    MMAP_U32 Get_MIU_DRAM_LEN();
    MMAP_U32 Get_MIU_DRAM_LEN0();
    MMAP_U32 Get_MIU_DRAM_LEN1();
    MMAP_U32 Get_MIU_INTERVAL();
    MMAP_U32 Get_MIU0();
    MMAP_U32 Get_MIU1();
    MMAP_U32 Get_TYPE_NONE();
    MMAP_U32 Get_UNCACHE();
    MMAP_U32 Get_REMAPPING_TO_USER_SPACE();
    MMAP_U32 Get_MIU0_END_ADR();
    MMAP_U32 Get_MIU1_END_ADR();
    MMAP_U32 Get_MMAP_COUNT();
    bool Get_ENABLE_MIU_1();
    #if (QJY_ENABLE == 1)
    bool SetBrowserFlag(bool bEnable);
    bool GetBrowserFlag();
    static bool m_bBrowser;
    #endif
    #if (ENABLE_LITE_SN == 1)
    bool SetHDMIFlag(bool bEnable);
    static bool m_bChangeToHDMI;
    #endif
private:
    //MMAP info
    static MMInfo_t m_mminfo;
    static MMInfo_t m_mminfos;
    /**
     * ------------------------------------------------------------ protected
     * attributes ------------------------------------------------------------
     */
    static MMapInfo_t * m_pMMap;
    static MMapInfo_t * m_pMMapsec;
    static MMAPInfo *m_pInstance;

    static const char *m_pMmap_id[MMAP_ID_COUNT];
    MMapInfo_t m_pMmapInfos[MMAP_MAX];
    MMAP_U32 m_u32Miu_dram_len;
    MMAP_U32 m_u32Miu_dram_len0;
    MMAP_U32 m_u32Miu_dram_len1;
    MMAP_U32 m_u32Miu_interval;
    bool m_bEnable_miu_1;
    MMAP_U32 m_u32Miu0;
    MMAP_U32 m_u32Miu1;
    MMAP_U32 m_u32Type_none;
    MMAP_U32 m_u32Uncache;
    MMAP_U32 m_u32Remapping_to_user_space;
    MMAP_U32 m_u32Miu0_end_adr;
    MMAP_U32 m_u32Miu1_end_adr;
    MMAP_U32 m_u32Mmap_count;
    MMAP_U32 m_u32MmapInfoCount;

    //-------------------------------------------------------------------------------------------------
    /// Instructor of MSrv_Control.
    /// @param  None
    /// @return  None
    //-------------------------------------------------------------------------------------------------
    MMAPInfo();

    //-------------------------------------------------------------------------------------------------
    /// Init, Set MMAP config
    /// @param  pmminfo, pMMap                \b IN: mminfo & mmap
    /// @return  None
    //-------------------------------------------------------------------------------------------------
    void Init();

    //-------------------------------------------------------------------------------------------------
    /// Init, Set security MMAP config
    /// @param  pmminfo, pMMap                \b IN: mminfo & mmap
    /// @return  None
    //-------------------------------------------------------------------------------------------------
    void Inits();

    MMapInfo_t* SaveInfo(char*, int);
    MMAP_U8 StrToMMAPID(char*);

    MMAP_BOOL CheckSize(EN_MMAP_ID id, MMAP_U32 u32LimitedSize);
};
#endif // _MMAP_INFO_H_

