From 56b920b8f1b910af693cdf4434c19787b91267c5 Mon Sep 17 00:00:00 2001 From: Karl Koscher Date: Thu, 20 Oct 2022 13:44:34 -0700 Subject: [PATCH 1/5] Add Hexagon Power Manager --- src/runtime/hexagon/hexagon_common.h | 1 + src/runtime/hexagon/hexagon_device_api.h | 7 + src/runtime/hexagon/hexagon_htp.cc | 26 ---- src/runtime/hexagon/hexagon_htp.h | 5 - src/runtime/hexagon/hexagon_power_manager.cc | 128 +++++++++++++++++++ src/runtime/hexagon/hexagon_power_manager.h | 67 ++++++++++ 6 files changed, 203 insertions(+), 31 deletions(-) create mode 100644 src/runtime/hexagon/hexagon_power_manager.cc create mode 100644 src/runtime/hexagon/hexagon_power_manager.h diff --git a/src/runtime/hexagon/hexagon_common.h b/src/runtime/hexagon/hexagon_common.h index 9f304836fcf1..025cc253eee9 100644 --- a/src/runtime/hexagon/hexagon_common.h +++ b/src/runtime/hexagon/hexagon_common.h @@ -41,6 +41,7 @@ int result = api_call; \ if (result != 0) { \ HEXAGON_PRINT(ERROR, "ERROR: " #api_call " failed with error %d.", result); \ + abort(); \ } \ } while (0) diff --git a/src/runtime/hexagon/hexagon_device_api.h b/src/runtime/hexagon/hexagon_device_api.h index e94ae4e87671..61ee9c2d471d 100644 --- a/src/runtime/hexagon/hexagon_device_api.h +++ b/src/runtime/hexagon/hexagon_device_api.h @@ -31,6 +31,7 @@ #include "hexagon_buffer.h" #include "hexagon_buffer_manager.h" +#include "hexagon_power_manager.h" #include "hexagon_thread_manager.h" #include "hexagon_user_dma.h" #include "hexagon_vtcm_pool.h" @@ -55,6 +56,9 @@ class HexagonDeviceAPI final : public DeviceAPI { //! \brief Ensures resource managers are in a good state for the runtime void AcquireResources() { + CHECK_EQ(runtime_power_manager, nullptr); + runtime_power_manager = std::make_unique(); + CHECK_EQ(runtime_vtcm, nullptr); runtime_vtcm = std::make_unique(); @@ -202,6 +206,9 @@ class HexagonDeviceAPI final : public DeviceAPI { //! \brief VTCM memory manager std::unique_ptr runtime_vtcm; + + //! \brief Hexagon power manager + std::unique_ptr runtime_power_manager; }; } // namespace hexagon } // namespace runtime diff --git a/src/runtime/hexagon/hexagon_htp.cc b/src/runtime/hexagon/hexagon_htp.cc index 32084382ed7f..67ef870d0a5b 100644 --- a/src/runtime/hexagon/hexagon_htp.cc +++ b/src/runtime/hexagon/hexagon_htp.cc @@ -36,37 +36,11 @@ namespace runtime { namespace hexagon { HexagonHtp::HexagonHtp() { - PowerOn(); Acquire(); } HexagonHtp::~HexagonHtp() { Release(); - PowerOff(); -} - -void HexagonHtp::PowerOn() { - HAP_power_request_t pwr_req; - int nErr; - - hap_pwr_ctx_ = HAP_utils_create_context(); - pwr_req.type = HAP_power_set_HMX; - pwr_req.hmx.power_up = true; - if ((nErr = HAP_power_set(hap_pwr_ctx_, &pwr_req))) { - LOG(FATAL) << "InternalError: HAP_power_set failed\n"; - } -} - -void HexagonHtp::PowerOff() { - HAP_power_request_t pwr_req; - int nErr; - - pwr_req.type = HAP_power_set_HMX; - pwr_req.hmx.power_up = false; - if ((nErr = HAP_power_set(hap_pwr_ctx_, &pwr_req))) { - LOG(FATAL) << "InternalError: HAP_power_set failed\n"; - } - HAP_utils_destroy_context(hap_pwr_ctx_); } void HexagonHtp::Acquire() { diff --git a/src/runtime/hexagon/hexagon_htp.h b/src/runtime/hexagon/hexagon_htp.h index b52e07e27b46..b3f0c0b5f71f 100644 --- a/src/runtime/hexagon/hexagon_htp.h +++ b/src/runtime/hexagon/hexagon_htp.h @@ -45,14 +45,9 @@ class HexagonHtp { HexagonHtp& operator=(HexagonHtp&&) = delete; private: - //! \brief Power context - void* hap_pwr_ctx_; - //! \brief Acquisition context ID unsigned int context_id_; - void PowerOn(); - void PowerOff(); void Acquire(); void Release(); }; diff --git a/src/runtime/hexagon/hexagon_power_manager.cc b/src/runtime/hexagon/hexagon_power_manager.cc new file mode 100644 index 000000000000..2b04b8c35e17 --- /dev/null +++ b/src/runtime/hexagon/hexagon_power_manager.cc @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include +#include +#include "HAP_power.h" + +#include "hexagon_common.h" +#include "hexagon_power_manager.h" + +namespace tvm { +namespace runtime { +namespace hexagon { + +HexagonPowerManager::HexagonPowerManager() { + hap_pwr_ctx_ = HAP_utils_create_context(); + PowerOnHVX(); + PowerOnHTP(); + SetAppType(); + SetDCVS(); + LogPowerConfig(); +} + +HexagonPowerManager::~HexagonPowerManager() { + PowerOffHTP(); + PowerOffHVX(); + HAP_utils_destroy_context(hap_pwr_ctx_); +} + +void HexagonPowerManager::PowerOnHVX() { + HAP_power_request_t pwr_req; + + pwr_req.type = HAP_power_set_HVX; + pwr_req.hvx.power_up = true; + HEXAGON_SAFE_CALL(HAP_power_set(hap_pwr_ctx_, &pwr_req)); +} + +void HexagonPowerManager::PowerOffHVX() { + HAP_power_request_t pwr_req; + + pwr_req.type = HAP_power_set_HVX; + pwr_req.hvx.power_up = false; + HEXAGON_SAFE_CALL(HAP_power_set(hap_pwr_ctx_, &pwr_req)); +} + +void HexagonPowerManager::PowerOnHTP() { + HAP_power_request_t pwr_req; + + pwr_req.type = HAP_power_set_HMX; + pwr_req.hmx.power_up = true; + HEXAGON_SAFE_CALL(HAP_power_set(hap_pwr_ctx_, &pwr_req)); +} + +void HexagonPowerManager::PowerOffHTP() { + HAP_power_request_t pwr_req; + + pwr_req.type = HAP_power_set_HMX; + pwr_req.hmx.power_up = false; + HEXAGON_SAFE_CALL(HAP_power_set(hap_pwr_ctx_, &pwr_req)); +} + +void HexagonPowerManager::SetAppType() { + HAP_power_request_t pwr_req; + + pwr_req.type = HAP_power_set_apptype; + pwr_req.apptype = HAP_POWER_COMPUTE_CLIENT_CLASS; + HEXAGON_SAFE_CALL(HAP_power_set(hap_pwr_ctx_, &pwr_req)); +} + +void HexagonPowerManager::SetDCVS() { + HAP_power_request_t pwr_req; + + memset(&pwr_req, 0, sizeof(HAP_power_request_t)); + pwr_req.type = HAP_power_set_DCVS_v3; + pwr_req.dcvs_v3.set_dcvs_enable = TRUE; + pwr_req.dcvs_v3.dcvs_enable = FALSE; + pwr_req.dcvs_v3.set_core_params = TRUE; + pwr_req.dcvs_v3.core_params.min_corner = HAP_DCVS_VCORNER_TURBO_PLUS; + pwr_req.dcvs_v3.core_params.max_corner = HAP_DCVS_VCORNER_TURBO_PLUS; + pwr_req.dcvs_v3.core_params.target_corner = HAP_DCVS_VCORNER_TURBO_PLUS; + pwr_req.dcvs_v3.set_bus_params = TRUE; + pwr_req.dcvs_v3.bus_params.min_corner = HAP_DCVS_VCORNER_TURBO_PLUS; + pwr_req.dcvs_v3.bus_params.max_corner = HAP_DCVS_VCORNER_TURBO_PLUS; + pwr_req.dcvs_v3.bus_params.target_corner = HAP_DCVS_VCORNER_TURBO_PLUS; + pwr_req.dcvs_v3.set_sleep_disable = TRUE; + pwr_req.dcvs_v3.sleep_disable = TRUE; + HEXAGON_SAFE_CALL(HAP_power_set(hap_pwr_ctx_, &pwr_req)); +} + +void HexagonPowerManager::LogPowerConfig() { + HAP_power_response_t pwr_resp; + + pwr_resp.type = HAP_power_get_dcvsEnabled; + HAP_power_get(hap_pwr_ctx_, &pwr_resp); + LOG(INFO) << "HexagonPowerManager: DCVS Enabled: " << pwr_resp.dcvsEnabled; + + pwr_resp.type = HAP_power_get_clk_Freq; + HAP_power_get(hap_pwr_ctx_, &pwr_resp); + LOG(INFO) << "HexagonPowerManager: Core running at " << pwr_resp.clkFreqHz << " Hz"; + + pwr_resp.type = HAP_power_get_dma_core_clk_Freq; + HAP_power_get(hap_pwr_ctx_, &pwr_resp); + LOG(INFO) << "HexagonPowerManager: DMA core running at " << pwr_resp.clkFreqHz << " Hz"; + + pwr_resp.type = HAP_power_get_max_bus_bw; + HAP_power_get(hap_pwr_ctx_, &pwr_resp); + LOG(INFO) << "HexagonPowerManager: Max bus bandwidth: " << pwr_resp.max_bus_bw; +} + +} // namespace hexagon +} // namespace runtime +} // namespace tvm \ No newline at end of file diff --git a/src/runtime/hexagon/hexagon_power_manager.h b/src/runtime/hexagon/hexagon_power_manager.h new file mode 100644 index 000000000000..1a85a9801efe --- /dev/null +++ b/src/runtime/hexagon/hexagon_power_manager.h @@ -0,0 +1,67 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef TVM_RUNTIME_HEXAGON_HEXAGON_POWER_MANAGER_H_ +#define TVM_RUNTIME_HEXAGON_HEXAGON_POWER_MANAGER_H_ + +namespace tvm { +namespace runtime { +namespace hexagon { + +class HexagonPowerManager { + public: + //! \brief Constructor. + HexagonPowerManager(); + + //! \brief Destructor. + ~HexagonPowerManager(); + + //! \brief Prevent copy construction of HexagonPowerManager. + HexagonPowerManager(const HexagonPowerManager&) = delete; + + //! \brief Prevent copy assignment with HexagonPowerManager. + HexagonPowerManager& operator=(const HexagonPowerManager&) = delete; + + //! \brief Prevent move construction. + HexagonPowerManager(HexagonPowerManager&&) = delete; + + //! \brief Prevent move assignment. + HexagonPowerManager& operator=(HexagonPowerManager&&) = delete; + + private: + //! \brief Power context + void* hap_pwr_ctx_; + + //! \brief Acquisition context ID + unsigned int context_id_; + + void PowerOnHVX(); + void PowerOffHVX(); + void PowerOnHTP(); + void PowerOffHTP(); + void SetAppType(); + void SetDCVS(); + void LogPowerConfig(); +}; + +} // namespace hexagon +} // namespace runtime +} // namespace tvm + +#endif // TVM_RUNTIME_HEXAGON_HEXAGON_HTP_H_ From ef1fa98444bc29bb9fa21551c181c6dbbbd5c267 Mon Sep 17 00:00:00 2001 From: Karl Koscher Date: Thu, 20 Oct 2022 13:45:50 -0700 Subject: [PATCH 2/5] Remove HexagonPowerManager::LogPowerConfig --- src/runtime/hexagon/hexagon_power_manager.cc | 21 -------------------- src/runtime/hexagon/hexagon_power_manager.h | 1 - 2 files changed, 22 deletions(-) diff --git a/src/runtime/hexagon/hexagon_power_manager.cc b/src/runtime/hexagon/hexagon_power_manager.cc index 2b04b8c35e17..31847ae8f9bc 100644 --- a/src/runtime/hexagon/hexagon_power_manager.cc +++ b/src/runtime/hexagon/hexagon_power_manager.cc @@ -34,7 +34,6 @@ HexagonPowerManager::HexagonPowerManager() { PowerOnHTP(); SetAppType(); SetDCVS(); - LogPowerConfig(); } HexagonPowerManager::~HexagonPowerManager() { @@ -103,26 +102,6 @@ void HexagonPowerManager::SetDCVS() { HEXAGON_SAFE_CALL(HAP_power_set(hap_pwr_ctx_, &pwr_req)); } -void HexagonPowerManager::LogPowerConfig() { - HAP_power_response_t pwr_resp; - - pwr_resp.type = HAP_power_get_dcvsEnabled; - HAP_power_get(hap_pwr_ctx_, &pwr_resp); - LOG(INFO) << "HexagonPowerManager: DCVS Enabled: " << pwr_resp.dcvsEnabled; - - pwr_resp.type = HAP_power_get_clk_Freq; - HAP_power_get(hap_pwr_ctx_, &pwr_resp); - LOG(INFO) << "HexagonPowerManager: Core running at " << pwr_resp.clkFreqHz << " Hz"; - - pwr_resp.type = HAP_power_get_dma_core_clk_Freq; - HAP_power_get(hap_pwr_ctx_, &pwr_resp); - LOG(INFO) << "HexagonPowerManager: DMA core running at " << pwr_resp.clkFreqHz << " Hz"; - - pwr_resp.type = HAP_power_get_max_bus_bw; - HAP_power_get(hap_pwr_ctx_, &pwr_resp); - LOG(INFO) << "HexagonPowerManager: Max bus bandwidth: " << pwr_resp.max_bus_bw; -} - } // namespace hexagon } // namespace runtime } // namespace tvm \ No newline at end of file diff --git a/src/runtime/hexagon/hexagon_power_manager.h b/src/runtime/hexagon/hexagon_power_manager.h index 1a85a9801efe..12b173ae3137 100644 --- a/src/runtime/hexagon/hexagon_power_manager.h +++ b/src/runtime/hexagon/hexagon_power_manager.h @@ -57,7 +57,6 @@ class HexagonPowerManager { void PowerOffHTP(); void SetAppType(); void SetDCVS(); - void LogPowerConfig(); }; } // namespace hexagon From 1013b2e45a906f08c394505d84eaef7ca85eac00 Mon Sep 17 00:00:00 2001 From: Karl Koscher Date: Thu, 20 Oct 2022 14:04:35 -0700 Subject: [PATCH 3/5] lint --- src/runtime/hexagon/hexagon_htp.cc | 8 ++------ src/runtime/hexagon/hexagon_power_manager.cc | 7 ++++--- src/runtime/hexagon/hexagon_power_manager.h | 5 +---- 3 files changed, 7 insertions(+), 13 deletions(-) diff --git a/src/runtime/hexagon/hexagon_htp.cc b/src/runtime/hexagon/hexagon_htp.cc index 67ef870d0a5b..01344ccf4a79 100644 --- a/src/runtime/hexagon/hexagon_htp.cc +++ b/src/runtime/hexagon/hexagon_htp.cc @@ -35,13 +35,9 @@ namespace tvm { namespace runtime { namespace hexagon { -HexagonHtp::HexagonHtp() { - Acquire(); -} +HexagonHtp::HexagonHtp() { Acquire(); } -HexagonHtp::~HexagonHtp() { - Release(); -} +HexagonHtp::~HexagonHtp() { Release(); } void HexagonHtp::Acquire() { compute_res_attr_t compute_res_attr; diff --git a/src/runtime/hexagon/hexagon_power_manager.cc b/src/runtime/hexagon/hexagon_power_manager.cc index 31847ae8f9bc..3d8f621bfcce 100644 --- a/src/runtime/hexagon/hexagon_power_manager.cc +++ b/src/runtime/hexagon/hexagon_power_manager.cc @@ -17,12 +17,13 @@ * under the License. */ +#include "hexagon_power_manager.h" + #include #include -#include "HAP_power.h" +#include "HAP_power.h" #include "hexagon_common.h" -#include "hexagon_power_manager.h" namespace tvm { namespace runtime { @@ -104,4 +105,4 @@ void HexagonPowerManager::SetDCVS() { } // namespace hexagon } // namespace runtime -} // namespace tvm \ No newline at end of file +} // namespace tvm diff --git a/src/runtime/hexagon/hexagon_power_manager.h b/src/runtime/hexagon/hexagon_power_manager.h index 12b173ae3137..6f88d92259c4 100644 --- a/src/runtime/hexagon/hexagon_power_manager.h +++ b/src/runtime/hexagon/hexagon_power_manager.h @@ -48,9 +48,6 @@ class HexagonPowerManager { //! \brief Power context void* hap_pwr_ctx_; - //! \brief Acquisition context ID - unsigned int context_id_; - void PowerOnHVX(); void PowerOffHVX(); void PowerOnHTP(); @@ -63,4 +60,4 @@ class HexagonPowerManager { } // namespace runtime } // namespace tvm -#endif // TVM_RUNTIME_HEXAGON_HEXAGON_HTP_H_ +#endif // TVM_RUNTIME_HEXAGON_HEXAGON_POWER_MANAGER_H_ From 6e5bc440e0a3cf2f1d26d36934a7462e884391fb Mon Sep 17 00:00:00 2001 From: Karl Koscher Date: Thu, 20 Oct 2022 14:21:35 -0700 Subject: [PATCH 4/5] Delete HexagonPowerManager in ReleaseResources --- src/runtime/hexagon/hexagon_device_api.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/runtime/hexagon/hexagon_device_api.h b/src/runtime/hexagon/hexagon_device_api.h index 61ee9c2d471d..aaa8853e5834 100644 --- a/src/runtime/hexagon/hexagon_device_api.h +++ b/src/runtime/hexagon/hexagon_device_api.h @@ -74,6 +74,9 @@ class HexagonDeviceAPI final : public DeviceAPI { //! \brief Ensures all runtime resources are freed void ReleaseResources() { + CHECK(runtime_power_manager) << "runtime_power_manager was not created in AcquireResources"; + runtime_power_manager.reset(); + CHECK(runtime_dma) << "runtime_dma was not created in AcquireResources"; runtime_dma.reset(); From 2869a8dac27a2a24fbd44aefa2160ec20d2ab450 Mon Sep 17 00:00:00 2001 From: Karl Koscher Date: Thu, 20 Oct 2022 14:29:07 -0700 Subject: [PATCH 5/5] Hexagon power manager must be destroyed after the thread manager --- src/runtime/hexagon/hexagon_device_api.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/runtime/hexagon/hexagon_device_api.h b/src/runtime/hexagon/hexagon_device_api.h index aaa8853e5834..5fe4a62e6908 100644 --- a/src/runtime/hexagon/hexagon_device_api.h +++ b/src/runtime/hexagon/hexagon_device_api.h @@ -74,9 +74,6 @@ class HexagonDeviceAPI final : public DeviceAPI { //! \brief Ensures all runtime resources are freed void ReleaseResources() { - CHECK(runtime_power_manager) << "runtime_power_manager was not created in AcquireResources"; - runtime_power_manager.reset(); - CHECK(runtime_dma) << "runtime_dma was not created in AcquireResources"; runtime_dma.reset(); @@ -88,6 +85,9 @@ class HexagonDeviceAPI final : public DeviceAPI { CHECK(runtime_vtcm) << "runtime_vtcm was not created in AcquireResources"; runtime_vtcm.reset(); + + CHECK(runtime_power_manager) << "runtime_power_manager was not created in AcquireResources"; + runtime_power_manager.reset(); } /*! \brief Currently unimplemented interface to specify the active