Currently, the uncore power library requires users to manually set
environment variables and call rte_power_uncore_init() one by one
in their applications. Since these steps are essentially part of
the driver initialization, they can be encapsulated in the uncore
core. So introduce two global API to initialize and deinitialize
uncore driver for application.

Signed-off-by: Huisong Li <[email protected]>
---
 lib/power/rte_power_uncore.c | 88 ++++++++++++++++++++++++++++++++++++
 lib/power/rte_power_uncore.h | 20 ++++++++
 2 files changed, 108 insertions(+)

diff --git a/lib/power/rte_power_uncore.c b/lib/power/rte_power_uncore.c
index a5d080e09e..0611a60d33 100644
--- a/lib/power/rte_power_uncore.c
+++ b/lib/power/rte_power_uncore.c
@@ -79,6 +79,12 @@ static int rte_power_probe_uncore_driver(void)
        return global_uncore_ops ? 0 : -ENODEV;
 }
 
+static void rte_power_remove_uncore_driver(void)
+{
+       global_uncore_ops = NULL;
+       global_uncore_env = RTE_UNCORE_PM_ENV_NOT_SET;
+}
+
 RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_power_set_uncore_env, 23.11)
 int
 rte_power_set_uncore_env(enum rte_uncore_power_mgmt_env env)
@@ -135,6 +141,88 @@ rte_power_get_uncore_env(void)
        return global_uncore_env;
 }
 
+RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_power_uncore_driver_init, 26.07)
+int
+rte_power_uncore_driver_init(void)
+{
+       int die, pkg, max_pkg, max_die;
+       int ret;
+
+       rte_spinlock_lock(&global_env_cfg_lock);
+       ret = rte_power_probe_uncore_driver();
+       if (ret) {
+               POWER_LOG(ERR, "Probe uncore driver failed, ret = %d.", ret);
+               goto out;
+       }
+
+       max_pkg = rte_power_uncore_get_num_pkgs();
+       if (max_pkg == 0) {
+               ret = -EINVAL;
+               goto remove_uncore_drv;
+       }
+
+       for (pkg = 0; pkg < max_pkg; pkg++) {
+               max_die = rte_power_uncore_get_num_dies(pkg);
+               if (max_die == 0) {
+                       ret = -EINVAL;
+                       goto remove_uncore_drv;
+               }
+
+               for (die = 0; die < max_die; die++) {
+                       ret = rte_power_uncore_init(pkg, die);
+                       if (ret) {
+                               POWER_LOG(ERR, "Unable to initialize uncore for 
pkg-%d die-%d",
+                                         pkg, die);
+                               goto uncore_exit;
+                       }
+               }
+       }
+       rte_spinlock_unlock(&global_env_cfg_lock);
+       return 0;
+
+uncore_exit:
+       for (; pkg >= 0; pkg--) {
+               max_die = rte_power_uncore_get_num_dies(pkg);
+               for (die = 0; die < max_die; die++) {
+                       ret = rte_power_uncore_exit(pkg, die);
+                       if (ret)
+                               POWER_LOG(ERR, "Failed to deinitialize uncore 
for pkg-%d die-%d",
+                                         pkg, die);
+               }
+       }
+
+remove_uncore_drv:
+       rte_power_remove_uncore_driver();
+out:
+       rte_spinlock_unlock(&global_env_cfg_lock);
+       return ret;
+}
+
+RTE_EXPORT_EXPERIMENTAL_SYMBOL(rte_power_uncore_driver_deinit, 26.07)
+void
+rte_power_uncore_driver_deinit(void)
+{
+       unsigned int die, pkg, max_pkg, max_die;
+
+       rte_spinlock_lock(&global_env_cfg_lock);
+       if (global_uncore_ops == NULL)
+               goto out;
+
+       max_pkg = rte_power_uncore_get_num_pkgs();
+       for (pkg = 0; pkg < max_pkg; pkg++) {
+               max_die = rte_power_uncore_get_num_dies(pkg);
+               for (die = 0; die < max_die; die++) {
+                       if (rte_power_uncore_exit(pkg, die) != 0)
+                               POWER_LOG(ERR, "Unable to deinitialize uncore 
for pkg-%02u die-%02u",
+                                         pkg, die);
+               }
+       }
+
+       rte_power_remove_uncore_driver();
+out:
+       rte_spinlock_unlock(&global_env_cfg_lock);
+}
+
 RTE_EXPORT_SYMBOL(rte_power_uncore_init)
 int
 rte_power_uncore_init(unsigned int pkg, unsigned int die)
diff --git a/lib/power/rte_power_uncore.h b/lib/power/rte_power_uncore.h
index 66aea1b37f..cac9127894 100644
--- a/lib/power/rte_power_uncore.h
+++ b/lib/power/rte_power_uncore.h
@@ -26,6 +26,26 @@ enum rte_uncore_power_mgmt_env {
        RTE_UNCORE_PM_ENV_AMD_HSMP
 };
 
+/**
+ * Probing and Initializing the uncore driver on platform.
+ *
+ * @return
+ *  - 0 on success.
+ *  - Negative on error.
+ */
+__rte_experimental
+int rte_power_uncore_driver_init(void);
+
+/**
+ * Deinitializing the uncore driver on platform.
+ *
+ * @return
+ *  - 0 on success.
+ *  - Negative on error.
+ */
+__rte_experimental
+void rte_power_uncore_driver_deinit(void);
+
 /**
  * Set the default uncore power management implementation.
  * This has to be called prior to calling any other rte_power_uncore_*() API.
-- 
2.33.0

Reply via email to