| 1 |
/* |
| 2 |
* Copyright (c) 2023, Intel Corporation |
| 3 |
* |
| 4 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 5 |
* copy of this software and associated documentation files (the "Software"), |
| 6 |
* to deal in the Software without restriction, including without limitation |
| 7 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 9 |
* Software is furnished to do so, subject to the following conditions: |
| 10 |
* |
| 11 |
* The above copyright notice and this permission notice shall be included |
| 12 |
* in all copies or substantial portions of the Software. |
| 13 |
* |
| 14 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 |
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 17 |
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 18 |
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 19 |
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 20 |
* OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
*/ |
| 22 |
//! |
| 23 |
//! \file oca_rtlog_section_mgr.cpp |
| 24 |
//! \brief OCA rtlog section manager |
| 25 |
//! |
| 26 |
|
| 27 |
#include "oca_rtlog_section_mgr.h" |
| 28 |
#include "mos_utilities.h" |
| 29 |
|
| 30 |
#define ADDRESS_PAGE_ALIGNMENT_MASK 0xFFFFFFFFFFFFF000ULL |
| 31 |
|
| 32 |
OcaRtLogSectionMgr OcaRtLogSectionMgr::s_rtLogSectionMgr[MOS_OCA_RTLOG_COMPONENT_MAX] = {}; |
| 33 |
uint8_t OcaRtLogSectionMgr::s_localSysMem[MAX_OCA_RT_POOL_SIZE] = {}; |
| 34 |
|
| 35 |
OcaRtLogSectionMgr::OcaRtLogSectionMgr() |
| 36 |
{ |
| 37 |
} |
| 38 |
|
| 39 |
OcaRtLogSectionMgr::~OcaRtLogSectionMgr() |
| 40 |
{ |
| 41 |
} |
| 42 |
|
| 43 |
uint8_t *OcaRtLogSectionMgr::GetMemAddress() |
| 44 |
{ |
| 45 |
static uint8_t *heapAddr = InitSectionMgrAndGetAddress(); |
| 46 |
|
| 47 |
return heapAddr; |
| 48 |
} |
| 49 |
|
| 50 |
MOS_STATUS OcaRtLogSectionMgr::InsertRTLog( |
| 51 |
MOS_OCA_RTLOG_COMPONENT_TPYE componentType, |
| 52 |
bool isErr, |
| 53 |
int32_t id, |
| 54 |
uint32_t paramCount, |
| 55 |
const void *param) |
| 56 |
{ |
| 57 |
//Try to init by calling GetMemAddress. |
| 58 |
GetMemAddress(); |
| 59 |
|
| 60 |
if (paramCount != MOS_OCA_RTLOG_MAX_PARAM_COUNT) |
| 61 |
{ |
| 62 |
return MOS_STATUS_INVALID_PARAMETER; |
| 63 |
} |
| 64 |
if (componentType < MOS_OCA_RTLOG_COMPONENT_MAX) |
| 65 |
{ |
| 66 |
OcaRtLogSectionMgr *insMgr = &s_rtLogSectionMgr[componentType]; |
| 67 |
|
| 68 |
if (insMgr->IsInitialized()) |
| 69 |
{ |
| 70 |
uint64_t index = 0; |
| 71 |
MosUtilities::MosQueryPerformanceCounter(&index); |
| 72 |
MOS_OCA_RTLOG_HEADER header = {}; |
| 73 |
header.globalId = index; |
| 74 |
header.id = id; |
| 75 |
header.paramCount = paramCount; |
| 76 |
MOS_OS_CHK_STATUS_RETURN(insMgr->InsertData(header, (uint8_t *)param)); |
| 77 |
} |
| 78 |
} |
| 79 |
return MOS_STATUS_SUCCESS; |
| 80 |
} |
| 81 |
|
| 82 |
uint8_t *OcaRtLogSectionMgr::InitSectionMgrAndGetAddress() |
| 83 |
{ |
| 84 |
uint64_t linearAddress = (uint64_t)s_localSysMem; |
| 85 |
|
| 86 |
uint8_t *heapAddr = (uint8_t *)((linearAddress + MOS_PAGE_SIZE - 1) & ADDRESS_PAGE_ALIGNMENT_MASK); |
| 87 |
|
| 88 |
uint32_t offset = 0; |
| 89 |
uint32_t initeSize = MAX_OCA_RT_SUB_SIZE; |
| 90 |
for (int i = 0; i < MOS_OCA_RTLOG_COMPONENT_MAX; ++i) |
| 91 |
{ |
| 92 |
initeSize = (MOS_OCA_RTLOG_COMPONENT_TPYE)i == MOS_OCA_RTLOG_COMPONENT_COMMON ? MAX_OCA_RT_COMMON_SUB_SIZE : MAX_OCA_RT_SUB_SIZE; |
| 93 |
s_rtLogSectionMgr[i].Init(heapAddr, MAX_OCA_RT_SIZE, initeSize, offset); |
| 94 |
MOS_OCA_RTLOG_SECTION_HEADER sectionHeader = {}; |
| 95 |
sectionHeader.magicNum = MOS_OCA_RTLOG_MAGIC_NUM; |
| 96 |
sectionHeader.componentType = (MOS_OCA_RTLOG_COMPONENT_TPYE)i; |
| 97 |
sectionHeader.freq = 0; |
| 98 |
MosUtilities::MosQueryPerformanceFrequency(§ionHeader.freq); |
| 99 |
s_rtLogSectionMgr[i].InsertUid(sectionHeader); |
| 100 |
offset += ((MOS_OCA_RTLOG_COMPONENT_TPYE)i == MOS_OCA_RTLOG_COMPONENT_COMMON ? MAX_OCA_RT_COMMON_SUB_SIZE : MAX_OCA_RT_SUB_SIZE); |
| 101 |
} |
| 102 |
return heapAddr; |
| 103 |
} |
| 104 |
|
| 105 |
void OcaRtLogSectionMgr::Init(uint8_t* logSysMem, uint32_t size, uint32_t componentSize, uint32_t offset) |
| 106 |
{ |
| 107 |
if (logSysMem && size && componentSize) |
| 108 |
{ |
| 109 |
m_LockedHeap = logSysMem; |
| 110 |
m_HeapSize = size; |
| 111 |
m_Offset = offset; |
| 112 |
m_HeapHandle = 0; |
| 113 |
m_EntryCount = (componentSize - sizeof(MOS_OCA_RTLOG_SECTION_HEADER))/ MOS_OCA_RTLOG_ENTRY_SIZE; |
| 114 |
m_IsInitialized = true; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
uint32_t OcaRtLogSectionMgr::AllocHeapHandle() |
| 119 |
{ |
| 120 |
return (uint32_t)MosUtilities::MosAtomicIncrement((int32_t *)&m_HeapHandle); |
| 121 |
} |
| 122 |
|
| 123 |
MOS_STATUS OcaRtLogSectionMgr::InsertUid(MOS_OCA_RTLOG_SECTION_HEADER sectionHeader) |
| 124 |
{ |
| 125 |
if (0 == sectionHeader.magicNum) |
| 126 |
{ |
| 127 |
return MOS_STATUS_INVALID_PARAMETER; |
| 128 |
} |
| 129 |
MOS_OS_CHK_STATUS_RETURN(MOS_SecureMemcpy((uint8_t *)m_LockedHeap + m_Offset, sizeof(MOS_OCA_RTLOG_SECTION_HEADER), §ionHeader, sizeof(MOS_OCA_RTLOG_SECTION_HEADER))); |
| 130 |
m_Offset += sizeof(MOS_OCA_RTLOG_SECTION_HEADER); |
| 131 |
return MOS_STATUS_SUCCESS; |
| 132 |
} |
| 133 |
|
| 134 |
MOS_STATUS OcaRtLogSectionMgr::InsertData(MOS_OCA_RTLOG_HEADER header, const void *param) |
| 135 |
{ |
| 136 |
if (param) |
| 137 |
{ |
| 138 |
if (header.paramCount * (sizeof(int32_t) + sizeof(int64_t)) > MOS_OCA_RTLOG_ENTRY_SIZE) |
| 139 |
{ |
| 140 |
return MOS_STATUS_NO_SPACE; |
| 141 |
} |
| 142 |
if (0 == m_EntryCount) |
| 143 |
{ |
| 144 |
MOS_OS_CHK_STATUS_RETURN(MOS_STATUS_INVALID_PARAMETER); |
| 145 |
} |
| 146 |
uint32_t heapHandle = AllocHeapHandle() % m_EntryCount; |
| 147 |
if (heapHandle < m_EntryCount) |
| 148 |
{ |
| 149 |
uint8_t *copyAddr = (uint8_t *)m_LockedHeap + m_Offset + heapHandle * MOS_OCA_RTLOG_ENTRY_SIZE; |
| 150 |
MOS_OS_CHK_NULL_RETURN(copyAddr); |
| 151 |
MOS_OS_CHK_STATUS_RETURN(MOS_SecureMemcpy(copyAddr, sizeof(MOS_OCA_RTLOG_HEADER), &header, sizeof(MOS_OCA_RTLOG_HEADER))); |
| 152 |
uint32_t copySize = header.paramCount * (sizeof(int32_t) + sizeof(int64_t)); |
| 153 |
MOS_OS_CHK_STATUS_RETURN(MOS_SecureMemcpy(copyAddr + sizeof(MOS_OCA_RTLOG_HEADER), copySize, param, copySize)); |
| 154 |
} |
| 155 |
} |
| 156 |
return MOS_STATUS_SUCCESS; |
| 157 |
} |