diff --git a/build.sh b/build.sh old mode 100644 new mode 100755 diff --git a/clean.sh b/clean.sh old mode 100644 new mode 100755 diff --git a/common/inc/pwrdata.h b/common/inc/pwrdata.h index e8bcb6efd46795620c32df81df13a6c400b07bd8..9f95c30b4a383560dd024cb4ee77b646f70a5b11 100644 --- a/common/inc/pwrdata.h +++ b/common/inc/pwrdata.h @@ -298,4 +298,12 @@ typedef struct PWR_PROC_SmartGridGov { char sgLevel1Gov[PWR_MAX_ELEMENT_NAME_LEN]; } PWR_PROC_SmartGridGov; +// HBM +typedef enum PWR_HBM_SysState { + PWR_HBM_NOT_SUPPORT = 0, + PWR_HBM_FLAT_MOD, + PWR_HBM_CACHE_MOD, + PWR_HBM_HYBRID_MOD, +} PWR_HBM_SysState; + #endif \ No newline at end of file diff --git a/common/inc/pwrerr.h b/common/inc/pwrerr.h index c4431e632ed6ac25aa2f517d04264dae0d9363b5..4de333e457f4d53b33affebe924c8ec58b3edf2c 100644 --- a/common/inc/pwrerr.h +++ b/common/inc/pwrerr.h @@ -55,5 +55,6 @@ enum PWR_RtnCode { PWR_ERR_FILE_FFLUSH_FAILED, PWR_ERR_FILE_OPEN_FAILED, PWR_ERR_FILE_SPRINTF_FAILED, + PWR_ERR_HBM_NOT_SUPPORTED = 600, }; #endif \ No newline at end of file diff --git a/common/inc/pwrmsg.h b/common/inc/pwrmsg.h index dc27c0842c8af31516e7e85c64a0b808d3d28f6f..19a2e37c994804375b11460bcddd0382ff58c417 100644 --- a/common/inc/pwrmsg.h +++ b/common/inc/pwrmsg.h @@ -103,6 +103,8 @@ enum OperationType { PROC_SET_SMART_GRID_PROCS_LEVEL, PROC_GET_SMART_GRID_GOV, PROC_SET_SMART_GRID_GOV, + HBM_GET_SYS_STATE = 700, + HBM_SET_ALL_POWER_STATE, }; enum DataFormat { FMT_BIN = 0, diff --git a/pwrapic/inc/powerapi.h b/pwrapic/inc/powerapi.h index bef75f4c718e657a55e13a65cbd61f2a37fe181b..6eacb409198d4e0e290e79571f2b0b8a698f3aac 100644 --- a/pwrapic/inc/powerapi.h +++ b/pwrapic/inc/powerapi.h @@ -103,6 +103,10 @@ PWR_API int PWR_PROC_SetSmartGridLevel(const PWR_PROC_SmartGridProcs *sgProcs); PWR_API int PWR_PROC_GetSmartGridGov(PWR_PROC_SmartGridGov *sgGov); PWR_API int PWR_PROC_SetSmartGridGov(const PWR_PROC_SmartGridGov *sgGov); +// HBM +PWR_API int PWR_HBM_GetSysState(PWR_HBM_SysState *hbmState); +PWR_API int PWR_HBM_SetAllPwrState(int state); + #endif // #ifndef RELEASE_MODE #ifdef __cplusplus diff --git a/pwrapic/inc/pwrhbm.h b/pwrapic/inc/pwrhbm.h new file mode 100644 index 0000000000000000000000000000000000000000..c757bebe543e5460768543c5a02b0830d6e6225c --- /dev/null +++ b/pwrapic/inc/pwrhbm.h @@ -0,0 +1,24 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024 All rights reserved. + * PowerAPI licensed under the Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v2 for more details. + * Author: heppen + * Create: 2024-04-30 + * Description: provide hbm operation interface + * **************************************************************************** */ + +#ifndef POWERAPI_HBM__H__ +#define POWERAPI_HBM__H__ + +#include "pwrdata.h" + +int GetHbmSysState(PWR_HBM_SysState *hmbState); +int SetAllHbmPowerState(int state); + +#endif //!POWERAPI_HBM__H__ \ No newline at end of file diff --git a/pwrapic/src/powerapi.c b/pwrapic/src/powerapi.c index 372417860228cab1706fc60a6d163139e9e86f42..e5b2d13a5b130a0215b1de294c10b65f147b7e67 100644 --- a/pwrapic/src/powerapi.c +++ b/pwrapic/src/powerapi.c @@ -27,6 +27,7 @@ #include "pwrnet.h" #include "pwrusb.h" #include "pwrproc.h" +#include "pwrhbm.h" #define CHECK_STATUS(s) \ { \ @@ -290,6 +291,22 @@ int PWR_CPU_DmaSetLatency(int latency) return SetCpuDmaLatency(latency); } +// HBM +int PWR_HBM_GetSysState(PWR_HBM_SysState *hbmState) +{ + CHECK_STATUS(STATUS_REGISTERTED); + + return GetHbmSysState(hbmState); +} + +int PWR_HBM_SetAllPwrState(int state) +{ + CHECK_STATUS(STATUS_AUTHED); + if (state < 0 || state > 1) { + return PWR_ERR_INVALIDE_PARAM; + } + return SetAllHbmPowerState(state); +} #ifndef RELEASE_MODE int PWR_SetMetaDataCallback(void(MetaDataCallback)(const PWR_COM_CallbackData *)) diff --git a/pwrapic/src/pwrhbm.c b/pwrapic/src/pwrhbm.c new file mode 100644 index 0000000000000000000000000000000000000000..90668086aee9388cb0558d2a37071be145ee1ea1 --- /dev/null +++ b/pwrapic/src/pwrhbm.c @@ -0,0 +1,60 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024 All rights reserved. + * PowerAPI licensed under the Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v2 for more details. + * Author: heppen + * Create: 2024-04-30 + * Description: provide hbm operation interface + * **************************************************************************** */ + +#include "pwrhbm.h" +#include "pwrlog.h" +#include "pwrerr.h" +#include "sockclient.h" + +int GetHbmSysState(PWR_HBM_SysState *hmbState) +{ + ReqInputParam input; + input.optType = HBM_GET_SYS_STATE; + input.dataLen = 0; + input.data = NULL; + + uint32_t size = sizeof(PWR_HBM_SysState); + RspOutputParam output; + output.rspBuffSize = &size; + output.rspData = (void *)hmbState; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "GetHbmSysState failed. ret: %d", ret); + } else { + PwrLog(DEBUG, "GetHbmSysState succeed."); + } + return ret; +} + +int SetAllHbmPowerState(int state) +{ + ReqInputParam input; + input.optType = HBM_SET_ALL_POWER_STATE; + input.dataLen = sizeof(int); + input.data = (char *)&state; + + RspOutputParam output; + output.rspBuffSize = NULL; + output.rspData = NULL; + + int ret = SendReqAndWaitForRsp(input, output); + if (ret != PWR_SUCCESS) { + PwrLog(ERROR, "SetAllHbmPowerState failed. ret: %d", ret); + } else { + PwrLog(DEBUG, "SetAllHbmPowerState succeed."); + } + return ret; +} \ No newline at end of file diff --git a/pwrapic/test/demo_main.c b/pwrapic/test/demo_main.c index 663e23092d03dc56af8a1eaecdfe1645c597ecf8..b711cca97537321f28be431d77f6cf5516295fb7 100644 --- a/pwrapic/test/demo_main.c +++ b/pwrapic/test/demo_main.c @@ -581,6 +581,32 @@ static void TEST_PWR_DISK_GetList(void) } /*************************** DISK END ************************/ +/***************************** HBM **************************/ +static void TEST_PWR_HBM_GetSysState(void) +{ + int ret = -1; + PWR_HBM_SysState state = PWR_HBM_HYBRID_MOD; + ret = PWR_HBM_GetSysState(&state); + PrintResult("TEST_PWR_HBM_GetSysState", ret); + printf("hbm state is %d.\n", (int)state); +} + +static void TEST_PWR_HBM_SetAllPwrState(void) +{ + int ret = -1; + ret = PWR_HBM_SetAllPwrState(0); + PrintResult("TEST_PWR_HBM_SetAllPwrState", ret); + + ret = PWR_HBM_SetAllPwrState(1); + PrintResult("TEST_PWR_HBM_SetAllPwrState", ret); + + ret = PWR_HBM_SetAllPwrState(2); + PrintResult("TEST_PWR_HBM_SetAllPwrState", ret); +} + + +/*************************** HBM END ************************/ + int main(int argc, const char *args[]) { /********** Common **********/ @@ -619,6 +645,11 @@ int main(int argc, const char *args[]) // TEST_PWR_COM_DcTaskMgr(); /************ PROC ***********/ TEST_PROC_AllFunc(); + + /************ HBM ***********/ + TEST_PWR_HBM_GetSysState(); + TEST_PWR_HBM_SetAllPwrState(); + // todo: 其他接口测试 while (g_run) { sleep(MAIN_LOOP_INTERVAL); diff --git a/pwrapis/inc/common.h b/pwrapis/inc/common.h index 8530d7e6098a34678346c3f59d8076f7c9c3d68c..f97c3ba38dc530a67e174072684087e0925c1948 100644 --- a/pwrapis/inc/common.h +++ b/pwrapis/inc/common.h @@ -58,6 +58,7 @@ #define MD_NM_SVR_TASK "TASK_SERVICE" #define MD_NM_SVR_PROC "PROC_SERVICE" #define MD_NM_SVR_COM "COM_SERVICE" +#define MD_NM_SVR_HBM "HBM_SERVICE" #define MD_NM_CRED "CREDENTIALS" #define MD_NM_OTHS "OTHERS" diff --git a/pwrapis/inc/hbmservice.h b/pwrapis/inc/hbmservice.h new file mode 100644 index 0000000000000000000000000000000000000000..915752035484824be9488caf92d24496ae0d1e7d --- /dev/null +++ b/pwrapis/inc/hbmservice.h @@ -0,0 +1,25 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024 All rights reserved. + * PowerAPI licensed under the Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v2 for more details. + * Author: heppen + * Create: 2024-04-30 + * Description: provide hbm get and set interface + * **************************************************************************** */ + +#ifndef PAPIS_HBM_SERVICE_H__ +#define PAPIS_HBM_SERVICE_H__ + +#include "pwrmsg.h" +#include "pwrdata.h" + +void GetHbmSysState(PwrMsg *req); +void SetHbmAllPowerState(PwrMsg *req); + +#endif //!PAPIS_HBM_SERVICE_H__ \ No newline at end of file diff --git a/pwrapis/src/cpuservice.c b/pwrapis/src/cpuservice.c index 9e4dd2ddff2e77c197921443d508e336eadffac5..1e220c5d5b713c00eda1819734247e4306a61afb 100644 --- a/pwrapis/src/cpuservice.c +++ b/pwrapis/src/cpuservice.c @@ -294,6 +294,7 @@ int PerfDataRead(PWR_CPU_PerfData *perfData) perfData->llcMiss = (double)cacheMiss / ins; perfData->ipc = (double)ins / cycles; pclose(fp); + remove("perf.txt"); return PWR_SUCCESS; } diff --git a/pwrapis/src/hbmservice.c b/pwrapis/src/hbmservice.c new file mode 100644 index 0000000000000000000000000000000000000000..443e1598cbe04733942a35bb47e66aa62247a4db --- /dev/null +++ b/pwrapis/src/hbmservice.c @@ -0,0 +1,133 @@ +/****************************************************************************** + * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024 All rights reserved. + * PowerAPI licensed under the Mulan PSL v2. + * You can use this software according to the terms and conditions of the Mulan PSL v2. + * You may obtain a copy of Mulan PSL v2 at: + * http://license.coscl.org.cn/MulanPSL2 + * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR + * PURPOSE. + * See the Mulan PSL v2 for more details. + * Author: heppen + * Create: 2024-04-30 + * Description: provide hbm service + * **************************************************************************** */ + +#include "string.h" +#include "pwrerr.h" +#include "server.h" +#include "log.h" +#include "unistd.h" +#include "utils.h" +#include "hbmservice.h" + +#define EXEC_COMMAND(cmd) \ + do { \ + FILE *fp = popen(cmd, "r"); \ + if (fp == NULL) { \ + return PWR_ERR_COMMON; \ + } \ + pclose(fp); \ + } while (0) + +static int GetHbmMode(PWR_HBM_SysState *state) +{ + char cache_mod_cmd[] = "find /sys/devices/LNXSYSTM* -name 'HISI04A1*'"; + char flat_mod_cmd[] = "find /sys/devices/LNXSYSTM* -name 'PNP0C80*'"; + *state = PWR_HBM_NOT_SUPPORT; + + FILE *cache_mod_fp = popen(cache_mod_cmd, "r"); + if (cache_mod_fp == NULL) { + return PWR_ERR_COMMON; + } + char cache_buf[PWR_MAX_STRING_LEN] = {0}; + if (fgets(cache_buf, PWR_MAX_STRING_LEN, cache_mod_fp) != NULL) { + *state |= PWR_HBM_FLAT_MOD; + } + + FILE *flat_mod_fp = popen(flat_mod_cmd, "r"); + if (flat_mod_fp == NULL) { + pclose(cache_mod_fp); + return PWR_ERR_COMMON; + } + char flat_buf[PWR_MAX_STRING_LEN] = {0}; + if (fgets(cache_buf, PWR_MAX_STRING_LEN, cache_mod_fp) != NULL) { + *state |= PWR_HBM_CACHE_MOD; + } + pclose(cache_mod_fp); + pclose(flat_mod_fp); + return PWR_SUCCESS; +} + +void GetHbmSysState(PwrMsg *req) +{ + PWR_HBM_SysState *state = (PWR_HBM_SysState *)malloc(sizeof(PWR_HBM_SysState)); + if (!state) { + SendRspToClient(req, PWR_ERR_SYS_EXCEPTION, NULL, 0); + return; + } + *state = PWR_HBM_NOT_SUPPORT; + int rspCode = GetHbmMode(state); + if (rspCode != PWR_SUCCESS) { + free(state); + SendRspToClient(req, rspCode, NULL, 0); + } else { + SendRspToClient(req, rspCode, (char *)state, sizeof(PWR_HBM_SysState)); + } +} + +static int SetPowerState(int powerState) +{ + PWR_HBM_SysState hbmState = PWR_HBM_NOT_SUPPORT; + if (GetHbmMode(&hbmState) != PWR_SUCCESS) { + Logger(ERROR, MD_NM_SVR_HBM, "GetHbmMode failed"); + return PWR_ERR_COMMON; + } + if (hbmState == PWR_HBM_NOT_SUPPORT) { + Logger(ERROR, MD_NM_SVR_HBM, "SetHbmAllPowerState: HBM is not support"); + return PWR_ERR_HBM_NOT_SUPPORTED; + } + + const char *state_str = (powerState == 0) ? "offline" : "online"; + if (hbmState == PWR_HBM_CACHE_MOD || hbmState == PWR_HBM_HYBRID_MOD) { + char cmd[PWR_MAX_STRING_LEN] = {0}; + snprintf( + cmd, sizeof(cmd), + "find /sys/kernel/hbm_cache/*/state -type f | xargs -I {} sh -c 'echo \"%s\" > {}'", + state_str); + EXEC_COMMAND(cmd); + } + + if (hbmState == PWR_HBM_FLAT_MOD || hbmState == PWR_HBM_HYBRID_MOD) { + char cmd[PWR_MAX_STRING_LEN] = {0}; + snprintf(cmd, sizeof(cmd), + "find /sys/devices/system/container/PNP0A06*/state -type f | xargs -I {} sh -c 'echo " + "\"%s\" > {}'", + state_str); + EXEC_COMMAND(cmd); + } +} + +void SetHbmAllPowerState(PwrMsg *req) +{ + int rspCode = PWR_SUCCESS; + do { + if (!req || req->head.dataLen != sizeof(int)) { + Logger(ERROR, MD_NM_SVR_HBM, "SetHbmAllPowerState: wrong req msg.dataLen:%d", + req->head.dataLen); + rspCode = PWR_ERR_INVALIDE_PARAM; + break; + } + + int state = *(int *)req->data; + if (state != PWR_ENABLE && state != PWR_DISABLE) { + Logger(ERROR, MD_NM_SVR_HBM, "SetHbmAllPowerState: wrong state:%d", state); + rspCode = PWR_ERR_INVALIDE_PARAM; + break; + } + + rspCode = SetPowerState(state); + } while (PWR_FALSE); + + SendRspToClient(req, rspCode, NULL, 0); +} \ No newline at end of file diff --git a/pwrapis/src/server.c b/pwrapis/src/server.c index 1e1ce0bbcc4643250d3caa48c50f10325422d605..95decc1e0d030ff6a56a802ad524594f09ae0e6d 100644 --- a/pwrapis/src/server.c +++ b/pwrapis/src/server.c @@ -36,6 +36,7 @@ #include "procservice.h" #include "pwrerr.h" #include "utils.h" +#include "hbmservice.h" #define COUNT_MAX 5 #define THREAD_LOOP_INTERVAL 2000 // us @@ -520,6 +521,8 @@ static OptToFunct g_optToFunct[] = { {PROC_SET_SMART_GRID_PROCS_LEVEL, ProcSetSmartGridProcsLevel}, {PROC_GET_SMART_GRID_GOV, ProcGetSmartGridGov}, {PROC_SET_SMART_GRID_GOV, ProcSetSmartGridGov}, + {HBM_GET_SYS_STATE, GetHbmSysState}, + {HBM_SET_ALL_POWER_STATE, SetHbmAllPowerState}, }; static void ProcessReqMsg(PwrMsg *req)