Skip to content

Commit

Permalink
Core: add get i/d cache information APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Huaqi Fang <[email protected]>
  • Loading branch information
fanghuaqi committed Aug 10, 2021
1 parent 7f17abe commit 2f79e9b
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions NMSIS/Core/Include/core_feature_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ typedef enum CCM_CMD {
CCM_IC_INVAL_ALL = 0xd /*!< Unlock and invalidate all the I-Cache lines */
} CCM_CMD_Type;

/**
* \brief Cache Information Type
*/
typedef struct CacheInfo {
uint32_t linesize; /*!< Cache Line size in bytes */
uint32_t ways; /*!< Cache ways */
uint32_t setperway; /*!< Cache set per way */
uint32_t size; /*!< Cache total size in bytes */
} CacheInfo_Type;

#define CCM_SUEN_SUEN_Pos 0U /*!< CSR CCM_SUEN: SUEN bit Position */
#define CCM_SUEN_SUEN_Msk (1UL << CCM_SUEN_SUEN_Pos) /*!< CSR CCM_SUEN: SUEN Mask */

Expand Down Expand Up @@ -178,6 +188,60 @@ __STATIC_FORCEINLINE void DisableICache(void)
__RV_CSR_CLEAR(CSR_MCACHE_CTL, CSR_MCACHE_CTL_IE);
}

/**
* \brief Get I-Cache Information
* \details
* This function get I-Cache Information
* \remarks
* - This function can be called in M-Mode only.
* - You can use this function in combination with cache lines operations
* \sa
* - \ref GetDCacheInfo
*/
__STATIC_FORCEINLINE int32_t GetICacheInfo(CacheInfo_Type *info)
{
if (info == NULL) {
return -1;
}
CSR_MICFGINFO_Type csr_ccfg = (CSR_MICFGINFO_Type)__RV_CSR_READ(CSR_MICFG_INFO);
uint32_t info->setperway = (1 << csr_ccfg.b.set) << 3;
uint32_t info->ways = (1 + csr_ccfg.b.way);
if (csr_ccfg.b.lsize == 0) {
info->linesize = 0;
} else {
info->linesize = (1 << (csr_ccfg.b.lsize - 1)) << 3;
}
info->size = info->setperway * info->ways * info->linesize;
return 0;
}

/**
* \brief Get D-Cache Information
* \details
* This function get D-Cache Information
* \remarks
* - This function can be called in M-Mode only.
* - You can use this function in combination with cache lines operations
* \sa
* - \ref GetICacheInfo
*/
__STATIC_FORCEINLINE int32_t GetDCacheInfo(CacheInfo_Type *info)
{
if (info == NULL) {
return -1;
}
CSR_MDCFGINFO_Type csr_ccfg = (CSR_MDCFGINFO_Type)__RV_CSR_READ(CSR_MDCFG_INFO);
uint32_t info->setperway = (1 << csr_ccfg.b.set) << 3;
uint32_t info->ways = (1 + csr_ccfg.b.way);
if (csr_ccfg.b.lsize == 0) {
info->linesize = 0;
} else {
info->linesize = (1 << (csr_ccfg.b.lsize - 1)) << 3;
}
info->size = info->setperway * info->ways * info->linesize;
return 0;
}

/**
* \brief Invalidate one I-Cache line specified by address in M-Mode
* \details
Expand Down

0 comments on commit 2f79e9b

Please sign in to comment.