Skip to contentSkip to conversation

power: refactor power management library

RFC PATCH In review Sivaprasad Tummala 7 participants 139 messages Feb 20, 2024 – Nov 16, 2024 Arm/Amlogic Meson SoC support Page 1 of 3

lore
Share & tools
lore
#0 lore raw

cover power: refactor power management library

This patchset refactors the power management library, addressing both core and uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/core/*' and 'drivers/power/uncore/*'.

This refactor significantly improves code organization, enhances clarity, and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

Furthermore, this effort aims to streamline code maintenance by consolidating common functions for cpufreq and cppc across various core drivers, thus reducing code duplication.

Please note that this RFC patch is currently in its initial phase and is primarily intended for soliciting feedback and comments. As of now, it has not undergone testing for build or functional issues.

---
power: refactor core power management library
power: refactor uncore power management library

 drivers/meson.build                           |   1 +
 drivers/power/core/acpi/meson.build           |   8 +
 .../power/core/acpi}/power_acpi_cpufreq.c     |  19 ++
 .../power/core/acpi}/power_acpi_cpufreq.h     |   0
 drivers/power/core/amd-pstate/meson.build     |   8 +
 .../amd-pstate}/power_amd_pstate_cpufreq.c    |  19 ++
 .../amd-pstate}/power_amd_pstate_cpufreq.h    |   0
 drivers/power/core/cppc/meson.build           |   8 +
 .../power/core/cppc}/power_cppc_cpufreq.c     |  19 ++
 .../power/core/cppc}/power_cppc_cpufreq.h     |   0
 .../power/core/kvm-vm}/guest_channel.c        |   0
 .../power/core/kvm-vm}/guest_channel.h        |   0
 drivers/power/core/kvm-vm/meson.build         |  20 ++
 .../power/core/kvm-vm}/power_kvm_vm.c         |  19 ++
 .../power/core/kvm-vm}/power_kvm_vm.h         |   0
 drivers/power/core/meson.build                |  12 +
 drivers/power/core/pstate/meson.build         |   8 +
 .../power/core/pstate}/power_pstate_cpufreq.c |  19 ++
 .../power/core/pstate}/power_pstate_cpufreq.h |   0
 drivers/power/meson.build                     |   9 +
 drivers/power/uncore/intel/meson.build        |   9 +
 .../power/uncore/intel}/power_intel_uncore.c  |  15 +
 .../power/uncore/intel}/power_intel_uncore.h  |   0
 drivers/power/uncore/meson.build              |   8 +
 lib/power/meson.build                         |   7 -
 lib/power/power_common.h                      |  11 +
 lib/power/rte_power.c                         | 305 ++++++++----------
 lib/power/rte_power.h                         | 207 ++++++++++--
 lib/power/rte_power_uncore.c                  | 163 ++++------
 lib/power/rte_power_uncore.h                  | 150 ++++++++-
 lib/power/version.map                         |  13 +
 31 files changed, 742 insertions(+), 315 deletions(-)
 create mode 100644 drivers/power/core/acpi/meson.build
 rename {lib/power => drivers/power/core/acpi}/power_acpi_cpufreq.c (95%)
 rename {lib/power => drivers/power/core/acpi}/power_acpi_cpufreq.h (100%)
 create mode 100644 drivers/power/core/amd-pstate/meson.build
 rename {lib/power => drivers/power/core/amd-pstate}/power_amd_pstate_cpufreq.c (95%)
 rename {lib/power => drivers/power/core/amd-pstate}/power_amd_pstate_cpufreq.h (100%)
 create mode 100644 drivers/power/core/cppc/meson.build
 rename {lib/power => drivers/power/core/cppc}/power_cppc_cpufreq.c (96%)
 rename {lib/power => drivers/power/core/cppc}/power_cppc_cpufreq.h (100%)
 rename {lib/power => drivers/power/core/kvm-vm}/guest_channel.c (100%)
 rename {lib/power => drivers/power/core/kvm-vm}/guest_channel.h (100%)
 create mode 100644 drivers/power/core/kvm-vm/meson.build
 rename {lib/power => drivers/power/core/kvm-vm}/power_kvm_vm.c (83%)
 rename {lib/power => drivers/power/core/kvm-vm}/power_kvm_vm.h (100%)
 create mode 100644 drivers/power/core/meson.build
 create mode 100644 drivers/power/core/pstate/meson.build
 rename {lib/power => drivers/power/core/pstate}/power_pstate_cpufreq.c (96%)
 rename {lib/power => drivers/power/core/pstate}/power_pstate_cpufreq.h (100%)
 create mode 100644 drivers/power/meson.build
 create mode 100644 drivers/power/uncore/intel/meson.build
 rename {lib/power => drivers/power/uncore/intel}/power_intel_uncore.c (95%)
 rename {lib/power => drivers/power/uncore/intel}/power_intel_uncore.h (100%)
 create mode 100644 drivers/power/uncore/meson.build
Signature · 1 line
2.25.1
#1 lore raw

RFC PATCH 1/2 power: refactor core power management library

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

---
 drivers/meson.build                           |   1 +
 drivers/power/core/acpi/meson.build           |   8 +
 .../power/core/acpi}/power_acpi_cpufreq.c     |  19 ++
 .../power/core/acpi}/power_acpi_cpufreq.h     |   0
 drivers/power/core/amd-pstate/meson.build     |   8 +
 .../amd-pstate}/power_amd_pstate_cpufreq.c    |  19 ++
 .../amd-pstate}/power_amd_pstate_cpufreq.h    |   0
 drivers/power/core/cppc/meson.build           |   8 +
 .../power/core/cppc}/power_cppc_cpufreq.c     |  19 ++
 .../power/core/cppc}/power_cppc_cpufreq.h     |   0
 .../power/core/kvm-vm}/guest_channel.c        |   0
 .../power/core/kvm-vm}/guest_channel.h        |   0
 drivers/power/core/kvm-vm/meson.build         |  20 ++
 .../power/core/kvm-vm}/power_kvm_vm.c         |  19 ++
 .../power/core/kvm-vm}/power_kvm_vm.h         |   0
 drivers/power/core/meson.build                |  12 +
 drivers/power/core/pstate/meson.build         |   8 +
 .../power/core/pstate}/power_pstate_cpufreq.c |  19 ++
 .../power/core/pstate}/power_pstate_cpufreq.h |   0
 drivers/power/meson.build                     |   8 +
 lib/power/meson.build                         |   6 -
 lib/power/power_common.h                      |  11 +
 lib/power/rte_power.c                         | 305 ++++++++----------
 lib/power/rte_power.h                         | 207 ++++++++++--
 lib/power/version.map                         |  12 +
 25 files changed, 506 insertions(+), 203 deletions(-)
 create mode 100644 drivers/power/core/acpi/meson.build
 rename {lib/power => drivers/power/core/acpi}/power_acpi_cpufreq.c (95%)
 rename {lib/power => drivers/power/core/acpi}/power_acpi_cpufreq.h (100%)
 create mode 100644 drivers/power/core/amd-pstate/meson.build
 rename {lib/power => drivers/power/core/amd-pstate}/power_amd_pstate_cpufreq.c (95%)
 rename {lib/power => drivers/power/core/amd-pstate}/power_amd_pstate_cpufreq.h (100%)
 create mode 100644 drivers/power/core/cppc/meson.build
 rename {lib/power => drivers/power/core/cppc}/power_cppc_cpufreq.c (96%)
 rename {lib/power => drivers/power/core/cppc}/power_cppc_cpufreq.h (100%)
 rename {lib/power => drivers/power/core/kvm-vm}/guest_channel.c (100%)
 rename {lib/power => drivers/power/core/kvm-vm}/guest_channel.h (100%)
 create mode 100644 drivers/power/core/kvm-vm/meson.build
 rename {lib/power => drivers/power/core/kvm-vm}/power_kvm_vm.c (83%)
 rename {lib/power => drivers/power/core/kvm-vm}/power_kvm_vm.h (100%)
 create mode 100644 drivers/power/core/meson.build
 create mode 100644 drivers/power/core/pstate/meson.build
 rename {lib/power => drivers/power/core/pstate}/power_pstate_cpufreq.c (96%)
 rename {lib/power => drivers/power/core/pstate}/power_pstate_cpufreq.h (100%)
 create mode 100644 drivers/power/meson.build
drivers/meson.build +1 -0
index f2be71bc05..e293c3945f 100644
@@ -28,6 +28,7 @@ subdirs = [
         'event',          # depends on common, bus, mempool and net.
         'baseband',       # depends on common and bus.
         'gpu',            # depends on common and bus.
+        'power',          # depends on common (in future).
 ]
 
 if meson.is_cross_build()
drivers/power/core/acpi/meson.build new file +8 -0
new file mode 100644
index 0000000000..d10ec8ee94
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_acpi_cpufreq.c')
+
+headers = files('power_acpi_cpufreq.h')
+
+deps += ['power']
drivers/power/core/acpi/power_acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +19 -0
similarity index 95%
rename from lib/power/power_acpi_cpufreq.c
rename to drivers/power/core/acpi/power_acpi_cpufreq.c
index f8d978d03d..69d80ad2ae 100644
@@ -577,3 +577,22 @@ int power_acpi_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_ops acpi_ops = {
+	.init = power_acpi_cpufreq_init,
+	.exit = power_acpi_cpufreq_exit,
+	.check_env_support = power_acpi_cpufreq_check_supported,
+	.get_avail_freqs = power_acpi_cpufreq_freqs,
+	.get_freq = power_acpi_cpufreq_get_freq,
+	.set_freq = power_acpi_cpufreq_set_freq,
+	.freq_down = power_acpi_cpufreq_freq_down,
+	.freq_up = power_acpi_cpufreq_freq_up,
+	.freq_max = power_acpi_cpufreq_freq_max,
+	.freq_min = power_acpi_cpufreq_freq_min,
+	.turbo_status = power_acpi_turbo_status,
+	.enable_turbo = power_acpi_enable_turbo,
+	.disable_turbo = power_acpi_disable_turbo,
+	.get_caps = power_acpi_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(acpi_ops);
drivers/power/core/acpi/power_acpi_cpufreq.h renamed from lib/power/power_acpi_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_acpi_cpufreq.h
rename to drivers/power/core/acpi/power_acpi_cpufreq.h
drivers/power/core/amd-pstate/meson.build new file +8 -0
new file mode 100644
index 0000000000..8ec4c960f5
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_amd_pstate_cpufreq.c')
+
+headers = files('power_amd_pstate_cpufreq.h')
+
+deps += ['power']
drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.c renamed from lib/power/power_amd_pstate_cpufreq.c +19 -0
similarity index 95%
rename from lib/power/power_amd_pstate_cpufreq.c
rename to drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.c
index 028f84416b..9938de72a6 100644
@@ -700,3 +700,22 @@ power_amd_pstate_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_ops amd_pstate_ops = {
+	.init = power_amd_pstate_cpufreq_init,
+	.exit = power_amd_pstate_cpufreq_exit,
+	.check_env_support = power_amd_pstate_cpufreq_check_supported,
+	.get_avail_freqs = power_amd_pstate_cpufreq_freqs,
+	.get_freq = power_amd_pstate_cpufreq_get_freq,
+	.set_freq = power_amd_pstate_cpufreq_set_freq,
+	.freq_down = power_amd_pstate_cpufreq_freq_down,
+	.freq_up = power_amd_pstate_cpufreq_freq_up,
+	.freq_max = power_amd_pstate_cpufreq_freq_max,
+	.freq_min = power_amd_pstate_cpufreq_freq_min,
+	.turbo_status = power_amd_pstate_turbo_status,
+	.enable_turbo = power_amd_pstate_enable_turbo,
+	.disable_turbo = power_amd_pstate_disable_turbo,
+	.get_caps = power_amd_pstate_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(amd_pstate_ops);
drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.h renamed from lib/power/power_amd_pstate_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_amd_pstate_cpufreq.h
rename to drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.h
drivers/power/core/cppc/meson.build new file +8 -0
new file mode 100644
index 0000000000..06f3b99bb8
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_cppc_cpufreq.c')
+
+headers = files('power_cppc_cpufreq.h')
+
+deps += ['power']
drivers/power/core/cppc/power_cppc_cpufreq.c renamed from lib/power/power_cppc_cpufreq.c +19 -0
similarity index 96%
rename from lib/power/power_cppc_cpufreq.c
rename to drivers/power/core/cppc/power_cppc_cpufreq.c
index 3ddf39bd76..605f633309 100644
@@ -685,3 +685,22 @@ power_cppc_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_ops cppc_ops = {
+	.init = power_cppc_cpufreq_init,
+	.exit = power_cppc_cpufreq_exit,
+	.check_env_support = power_cppc_cpufreq_check_supported,
+	.get_avail_freqs = power_cppc_cpufreq_freqs,
+	.get_freq = power_cppc_cpufreq_get_freq,
+	.set_freq = power_cppc_cpufreq_set_freq,
+	.freq_down = power_cppc_cpufreq_freq_down,
+	.freq_up = power_cppc_cpufreq_freq_up,
+	.freq_max = power_cppc_cpufreq_freq_max,
+	.freq_min = power_cppc_cpufreq_freq_min,
+	.turbo_status = power_cppc_turbo_status,
+	.enable_turbo = power_cppc_enable_turbo,
+	.disable_turbo = power_cppc_disable_turbo,
+	.get_caps = power_cppc_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(cppc_ops);
drivers/power/core/cppc/power_cppc_cpufreq.h renamed from lib/power/power_cppc_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_cppc_cpufreq.h
rename to drivers/power/core/cppc/power_cppc_cpufreq.h
drivers/power/core/kvm-vm/guest_channel.c renamed from lib/power/guest_channel.c +0 -0
similarity index 100%
rename from lib/power/guest_channel.c
rename to drivers/power/core/kvm-vm/guest_channel.c
drivers/power/core/kvm-vm/guest_channel.h renamed from lib/power/guest_channel.h +0 -0
similarity index 100%
rename from lib/power/guest_channel.h
rename to drivers/power/core/kvm-vm/guest_channel.h
drivers/power/core/kvm-vm/meson.build new file +20 -0
new file mode 100644
index 0000000000..3150c6674b
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 AMD Limited.
+#
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+sources = files(
+        'guest_channel.c',
+        'power_kvm_vm.c',
+)
+
+headers = files(
+        'guest_channel.h',
+        'power_kvm_vm.h',
+)
+deps += ['power']
drivers/power/core/kvm-vm/power_kvm_vm.c renamed from lib/power/power_kvm_vm.c +19 -0
similarity index 83%
rename from lib/power/power_kvm_vm.c
rename to drivers/power/core/kvm-vm/power_kvm_vm.c
index f15be8fac5..a5d6984d26 100644
@@ -137,3 +137,22 @@ int power_kvm_vm_get_capabilities(__rte_unused unsigned int lcore_id,
 	POWER_LOG(ERR, "rte_power_get_capabilities is not implemented for Virtual Machine Power Management");
 	return -ENOTSUP;
 }
+
+static struct rte_power_ops kvm_vm_ops = {
+	.init = power_kvm_vm_init,
+	.exit = power_kvm_vm_exit,
+	.check_env_support = power_kvm_vm_check_supported,
+	.get_avail_freqs = power_kvm_vm_freqs,
+	.get_freq = power_kvm_vm_get_freq,
+	.set_freq = power_kvm_vm_set_freq,
+	.freq_down = power_kvm_vm_freq_down,
+	.freq_up = power_kvm_vm_freq_up,
+	.freq_max = power_kvm_vm_freq_max,
+	.freq_min = power_kvm_vm_freq_min,
+	.turbo_status = power_kvm_vm_turbo_status,
+	.enable_turbo = power_kvm_vm_enable_turbo,
+	.disable_turbo = power_kvm_vm_disable_turbo,
+	.get_caps = power_kvm_vm_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(kvm_vm_ops);
drivers/power/core/kvm-vm/power_kvm_vm.h renamed from lib/power/power_kvm_vm.h +0 -0
similarity index 100%
rename from lib/power/power_kvm_vm.h
rename to drivers/power/core/kvm-vm/power_kvm_vm.h
drivers/power/core/meson.build new file +12 -0
new file mode 100644
index 0000000000..4081dafaa0
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+drivers = [
+        'acpi',
+        'amd-pstate',
+        'cppc',
+        'kvm-vm',
+        'pstate'
+]
+
+std_deps = ['power']
drivers/power/core/pstate/meson.build new file +8 -0
new file mode 100644
index 0000000000..1025c64e48
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_pstate_cpufreq.c')
+
+headers = files('power_pstate_cpufreq.h')
+
+deps += ['power']
drivers/power/core/pstate/power_pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c +19 -0
similarity index 96%
rename from lib/power/power_pstate_cpufreq.c
rename to drivers/power/core/pstate/power_pstate_cpufreq.c
index 73138dc4e4..d4c3645ff8 100644
@@ -888,3 +888,22 @@ int power_pstate_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_ops pstate_ops = {
+	.init = power_pstate_cpufreq_init,
+	.exit = power_pstate_cpufreq_exit,
+	.check_env_support = power_pstate_cpufreq_check_supported,
+	.get_avail_freqs = power_pstate_cpufreq_freqs,
+	.get_freq = power_pstate_cpufreq_get_freq,
+	.set_freq = power_pstate_cpufreq_set_freq,
+	.freq_down = power_pstate_cpufreq_freq_down,
+	.freq_up = power_pstate_cpufreq_freq_up,
+	.freq_max = power_pstate_cpufreq_freq_max,
+	.freq_min = power_pstate_cpufreq_freq_min,
+	.turbo_status = power_pstate_turbo_status,
+	.enable_turbo = power_pstate_enable_turbo,
+	.disable_turbo = power_pstate_disable_turbo,
+	.get_caps = power_pstate_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(pstate_ops);
drivers/power/core/pstate/power_pstate_cpufreq.h renamed from lib/power/power_pstate_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_pstate_cpufreq.h
rename to drivers/power/core/pstate/power_pstate_cpufreq.h
drivers/power/meson.build new file +8 -0
new file mode 100644
index 0000000000..7d9034c7ac
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+drivers = [
+        'core',
+]
+
+std_deps = ['power']
lib/power/meson.build +0 -6
index b8426589b2..207d96d877 100644
@@ -12,14 +12,8 @@ if not is_linux
     reason = 'only supported on Linux'
 endif
 sources = files(
-        'guest_channel.c',
-        'power_acpi_cpufreq.c',
-        'power_amd_pstate_cpufreq.c',
         'power_common.c',
-        'power_cppc_cpufreq.c',
-        'power_kvm_vm.c',
         'power_intel_uncore.c',
-        'power_pstate_cpufreq.c',
         'rte_power.c',
         'rte_power_uncore.c',
         'rte_power_pmd_mgmt.c',
lib/power/power_common.h +11 -0
index 30966400ba..c90b611f4f 100644
@@ -23,13 +23,24 @@ extern int power_logtype;
 #endif
 
 /* check if scaling driver matches one we want */
+__rte_internal
 int cpufreq_check_scaling_driver(const char *driver);
+
+__rte_internal
 int power_set_governor(unsigned int lcore_id, const char *new_governor,
 		char *orig_governor, size_t orig_governor_len);
+
+__rte_internal
 int open_core_sysfs_file(FILE **f, const char *mode, const char *format, ...)
 		__rte_format_printf(3, 4);
+
+__rte_internal
 int read_core_sysfs_u32(FILE *f, uint32_t *val);
+
+__rte_internal
 int read_core_sysfs_s(FILE *f, char *buf, unsigned int len);
+
+__rte_internal
 int write_core_sysfs_s(FILE *f, const char *str);
 
 #endif /* _POWER_COMMON_H_ */
lib/power/rte_power.c +138 -167

369 lines not on this page · raw message

lib/power/rte_power.h +177 -30

292 lines not on this page · raw message

lib/power/version.map +12 -0
index ad92a65f91..2f89645ec2 100644
@@ -52,3 +52,15 @@ EXPERIMENTAL {
 	rte_power_uncore_freqs;
 	rte_power_unset_uncore_env;
 };
+
+INTERNAL {
+       global:
+
+       rte_power_register_ops;
+       cpufreq_check_scaling_driver;
+       power_set_governor;
+       open_core_sysfs_file;
+       read_core_sysfs_u32;
+       read_core_sysfs_s;
+       write_core_sysfs_s;
+};
Signature · 1 line
2.25.1
#2 lore raw

Re: [RFC PATCH 1/2] power: refactor core power management library

Quoted 7 lines · Sivaprasad Tummala

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

Quoted 9 lines

These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

+1 to refactor, thanks for the work.

There are multiple power implementations but all are managed withing the power library, it is good idea to extract different implementations as drivers.

<...>

drivers/power/core/acpi/meson.build line 2

Quoted 9 lines
drivers/power/core/acpi/meson.build new file +2 -0
new file mode 100644
index 0000000000..d10ec8ee94
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited

It should be as following, same for all:

Copyright (C) 2024, Advanced Micro Devices, Inc.

drivers/power/core/acpi/meson.build line 6

Quoted 5 lines
drivers/power/core/acpi/meson.build new file +4 -0
+
+sources = files('power_acpi_cpufreq.c')
+
+headers = files('power_acpi_cpufreq.h')

In meson, 'headers' variable is used to install the header, this is required for the case user needs to include the header but I guess that is not the case for power libraries.
Can you please check if the 'header' variable in meson is required?

<...>

Quoted 12 lines
drivers/power/core/acpi/meson.build new file +8 -0
@@ -577,3 +577,22 @@ int power_acpi_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_ops acpi_ops = {
+	.init = power_acpi_cpufreq_init,
+	.exit = power_acpi_cpufreq_exit,
+	.check_env_support = power_acpi_cpufreq_check_supported,
+	.get_avail_freqs = power_acpi_cpufreq_freqs,
+	.get_freq = power_acpi_cpufreq_get_freq,
+	.set_freq = power_acpi_cpufreq_set_freq,

drivers/power/core/acpi/meson.build int power_acpi_get_capabilities(unsigned int lcore_id, · line 597

Quoted 10 lines
drivers/power/core/acpi/meson.build new file +10 -0
+	.freq_down = power_acpi_cpufreq_freq_down,
+	.freq_up = power_acpi_cpufreq_freq_up,
+	.freq_max = power_acpi_cpufreq_freq_max,
+	.freq_min = power_acpi_cpufreq_freq_min,
+	.turbo_status = power_acpi_turbo_status,
+	.enable_turbo = power_acpi_enable_turbo,
+	.disable_turbo = power_acpi_disable_turbo,
+	.get_caps = power_acpi_get_capabilities
+};
+

With current usage of the ops struct, I guess all can be "static const".

<...>

Quoted 7 lines
drivers/power/core/kvm-vm/meson.build new file +0 -0
new file mode 100644
index 0000000000..3150c6674b

drivers/power/core/kvm-vm/meson.build line 9

Quoted 9 lines
drivers/power/core/kvm-vm/meson.build new file +9 -0
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 AMD Limited.
+#
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif

Before refactoring, in lib/power was supported only for Linux, I assume this means all existing power libraries supported only for Linux. If so above check can be added to all drivers.

<...>

Quoted 18 lines
drivers/power/core/kvm-vm/meson.build new file +17 -0
+/* register the ops struct in rte_power_ops, return 0 on success. */
+int
+rte_power_register_ops(const struct rte_power_ops *op)
+{
+	struct rte_power_ops *ops;
+
+	if (op->env >= PM_ENV_MAX) {
+		POWER_LOG(ERR, "Unsupported power management environment\n");
+		return -EINVAL;
+	}
+
+	if (op->status != 0) {
+		POWER_LOG(ERR, "Power management env[%d] ops registered already\n",
+			op->env);
+		return -EINVAL;
+	}
+

drivers/power/core/kvm-vm/meson.build

Quoted 10 lines
drivers/power/core/kvm-vm/meson.build new file +10 -0
+	if (!op->init || !op->exit || !op->check_env_support ||
+		!op->get_avail_freqs || !op->get_freq || !op->set_freq ||
+		!op->freq_up || !op->freq_down || !op->freq_max ||
+		!op->freq_min || !op->turbo_status || !op->enable_turbo ||
+		!op->disable_turbo || !op->get_caps) {
+		POWER_LOG(ERR, "Missing callbacks while registering power ops\n");
+		return -EINVAL;
+	}
+
+	ops = &rte_power_ops[op->env];

I don't see all drivers set 'op->env',

This 'rte_power_register_ops()' function copies ops from driver proved struct to library global 'rte_power_ops[]' array,

it can be possible to store ops pointer provided by driver, instead of copying it.

And it can be possible to link the ops in this function, instead of putting them to specific index, as only one ops can be active in a given time, it can be possible to store active ops pointer in a global variable which removes the need to have index accessible array for ops.

<...>

Quoted 29 lines
drivers/power/core/kvm-vm/meson.build new file +10 -13
@@ -177,59 +138,76 @@ int
 rte_power_init(unsigned int lcore_id)
 {
 	int ret = -1;
+	struct rte_power_ops *ops;
 
-	switch (global_default_env) {
-	case PM_ENV_ACPI_CPUFREQ:
-		return power_acpi_cpufreq_init(lcore_id);
-	case PM_ENV_KVM_VM:
-		return power_kvm_vm_init(lcore_id);
-	case PM_ENV_PSTATE_CPUFREQ:
-		return power_pstate_cpufreq_init(lcore_id);
-	case PM_ENV_CPPC_CPUFREQ:
-		return power_cppc_cpufreq_init(lcore_id);
-	case PM_ENV_AMD_PSTATE_CPUFREQ:
-		return power_amd_pstate_cpufreq_init(lcore_id);
-	default:
-		POWER_LOG(INFO, "Env isn't set yet!");
+	if (global_default_env != PM_ENV_NOT_SET) {
+		ops = &rte_power_ops[global_default_env];
+		if (!ops->status) {
+			POWER_LOG(ERR, "Power management env[%d] not"
+				" supported\n", global_default_env);
+			goto out;
+		}
+		return ops->init(lcore_id);
 	}
+	POWER_LOG(INFO, POWER, "Env isn't set yet!\n");

drivers/power/core/kvm-vm/meson.build int · line 157

Quoted 10 lines
drivers/power/core/kvm-vm/meson.build new file +2 -5
 
 	/* Auto detect Environment */
-	POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq power management...");
-	ret = power_acpi_cpufreq_init(lcore_id);
-	if (ret == 0) {
-		rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
-		goto out;
+	POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq"
+			" power management...\n");
 

Shouldn't break the log, can break the line by keeping message whole:
POWER_LOG(INFO,

	"Attempting to initialise ACPI cpufreq power management...");

<...>

drivers/power/core/kvm-vm/meson.build extern "C" { · line 26

Quoted 7 lines
drivers/power/core/kvm-vm/meson.build new file +1 -1
@@ -21,7 +22,7 @@ extern "C" {
 /* Power Management Environment State */
 enum power_management_env {PM_ENV_NOT_SET, PM_ENV_ACPI_CPUFREQ, PM_ENV_KVM_VM,
 		PM_ENV_PSTATE_CPUFREQ, PM_ENV_CPPC_CPUFREQ,
-		PM_ENV_AMD_PSTATE_CPUFREQ};
+		PM_ENV_AMD_PSTATE_CPUFREQ, PM_ENV_MAX};
 

Syntax. Can we have enum item per line?

Quoted 7 lines
drivers/power/core/kvm-vm/meson.build new file +1 -0
 /**
  * Check if a specific power management environment type is supported on a
@@ -66,6 +67,97 @@ void rte_power_unset_env(void);
  */
 enum power_management_env rte_power_get_env(void);
 
+typedef int (*rte_power_cpufreq_init_t)(unsigned int lcore_id);

drivers/power/core/kvm-vm/meson.build void rte_power_unset_env(void); · line 80

Quoted 10 lines
drivers/power/core/kvm-vm/meson.build new file +9 -0
+typedef int (*rte_power_cpufreq_exit_t)(unsigned int lcore_id);
+typedef int (*rte_power_check_env_support_t)(void);
+
+typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
+					uint32_t num);
+typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);
+typedef int (*rte_power_set_freq_t)(unsigned int lcore_id, uint32_t index);
+typedef int (*rte_power_freq_change_t)(unsigned int lcore_id);
+
 

I guess above is not required for users, what do you think to create a driver header file and move these to driver header file?

<...>

drivers/power/core/kvm-vm/meson.build void rte_power_unset_env(void); · line 90

Quoted 10 lines
drivers/power/core/kvm-vm/meson.build new file +9 -0
+
+/**
+ * Macro to statically register the ops of a cpufreq driver.
+ */
+#define RTE_POWER_REGISTER_OPS(ops)		\
+	(RTE_INIT(power_hdlr_init_##ops)	\
+	{					\
+		rte_power_register_ops(&ops);	\
+	})
 

is () required around RTE_INIT()

Quoted 21 lines
drivers/power/core/kvm-vm/meson.build new file +12 -2
+
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @param ops_index
+ *   The index of the ops struct in the ops struct table.
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_ops *
+rte_power_get_ops(int ops_index);
+
 /**
  * Initialize power management for a specific lcore. If rte_power_set_env() has
  * not been called then an auto-detect of the environment will start and
@@ -108,10 +200,14 @@ int rte_power_exit(unsigned int lcore_id);
  * @return
  *  The number of available frequencies.
  */
-typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
-		uint32_t num);

drivers/power/core/kvm-vm/meson.build int rte_power_exit(unsigned int lcore_id); · line 211

Quoted 10 lines
drivers/power/core/kvm-vm/meson.build new file +7 -1
+static inline uint32_t
+rte_power_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t n)
+{
+	struct rte_power_ops *ops;
 
-extern rte_power_freqs_t rte_power_freqs;
+	ops = rte_power_get_ops(rte_power_get_env());
+	return ops->get_avail_freqs(lcore_id, freqs, n);
+}
 

Why not proper functions but "static inline functions"?

Quoted 6 lines
drivers/power/core/kvm-vm/meson.build new file +0 -0
 /**
  * Return the current index of available frequencies of a specific lcore.
@@ -124,9 +220,14 @@ extern rte_power_freqs_t rte_power_freqs;
  * @return
  *  The current index of available frequencies.

drivers/power/core/kvm-vm/meson.build extern rte_power_freqs_t rte_power_freqs; · line 229

Quoted 10 lines
drivers/power/core/kvm-vm/meson.build new file +5 -2
  */
-typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);
+static inline uint32_t
+rte_power_get_freq(unsigned int lcore_id)
+{
+	struct rte_power_ops *ops;
 
-extern rte_power_get_freq_t rte_power_get_freq;
+	ops = rte_power_get_ops(rte_power_get_env());
 

As 'rte_power_get_env()' already returns a global variable, why not set a global ops pointer and directly access to them, is above abstraction providing any benefit?

#3 lore raw

RE: [RFC PATCH 1/2] power: refactor core power management library

[AMD Official Use Only - General]

Hi Ferruh,

Quoted 48 lines

-----Original Message-----
From: Yigit, Ferruh <[email protected]>
Sent: Tuesday, February 27, 2024 9:48 PM
To: Tummala, Sivaprasad <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]
Cc: [email protected]
Subject: Re: [RFC PATCH 1/2] power: refactor core power management library

Quoted 7 lines · Sivaprasad Tummala

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

Quoted 9 lines

These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

+1 to refactor, thanks for the work.

There are multiple power implementations but all are managed withing the power library, it is good idea to extract different implementations as drivers.

<...>

drivers/power/core/acpi/meson.build line 2

Quoted 10 lines
drivers/power/core/acpi/meson.build new file +2 -0
new file mode 100644
index 0000000000..d10ec8ee94
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2024 AMD
+Limited

It should be as following, same for all:

Quoted 2 lines

Copyright (C) 2024, Advanced Micro Devices, Inc.

ACK

Quoted 29 lines

drivers/power/core/acpi/meson.build line 6

Quoted 5 lines
drivers/power/core/acpi/meson.build new file +4 -0
+
+sources = files('power_acpi_cpufreq.c')
+
+headers = files('power_acpi_cpufreq.h')

In meson, 'headers' variable is used to install the header, this is required for the case user needs to include the header but I guess that is not the case for power libraries.
Can you please check if the 'header' variable in meson is required?

<...>

Quoted 16 lines
drivers/power/core/acpi/meson.build new file +11 -0
@@ -577,3 +577,22 @@ int power_acpi_get_capabilities(unsigned int
 lcore_id,
 
    return 0;
 }
+
+static struct rte_power_ops acpi_ops = {
+   .init = power_acpi_cpufreq_init,
+   .exit = power_acpi_cpufreq_exit,
+   .check_env_support = power_acpi_cpufreq_check_supported,
+   .get_avail_freqs = power_acpi_cpufreq_freqs,
+   .get_freq = power_acpi_cpufreq_get_freq,
+   .set_freq = power_acpi_cpufreq_set_freq,
+   .freq_down = power_acpi_cpufreq_freq_down,
+   .freq_up = power_acpi_cpufreq_freq_up,
+   .freq_max = power_acpi_cpufreq_freq_max,
Quoted 8 lines

drivers/power/core/acpi/meson.build

Quoted 6 lines
drivers/power/core/acpi/meson.build new file +6 -0
+   .freq_min = power_acpi_cpufreq_freq_min,
+   .turbo_status = power_acpi_turbo_status,
+   .enable_turbo = power_acpi_enable_turbo,
+   .disable_turbo = power_acpi_disable_turbo,
+   .get_caps = power_acpi_get_capabilities };
+

With current usage of the ops struct, I guess all can be "static const".

ACK

Quoted 16 lines

<...>

Quoted 13 lines
drivers/power/core/kvm-vm/meson.build new file +6 -0
new file mode 100644
index 0000000000..3150c6674b
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(C) 2024 AMD
+Limited.
+#
+
+if not is_linux
+    build = false
Quoted 8 lines

drivers/power/core/kvm-vm/meson.build line 9

Quoted 4 lines
drivers/power/core/kvm-vm/meson.build new file +3 -0
+    reason = 'only supported on Linux'
+    subdir_done()
+endif

Before refactoring, in lib/power was supported only for Linux, I assume this means all existing power libraries supported only for Linux.
If so above check can be added to all drivers.

ACK

Quoted 38 lines

<...>

drivers/power/core/kvm-vm/meson.build

Quoted 6 lines
drivers/power/core/kvm-vm/meson.build new file +6 -0
+/* register the ops struct in rte_power_ops, return 0 on success. */
+int rte_power_register_ops(const struct rte_power_ops *op) {
+   struct rte_power_ops *ops;
+
+   if (op->env >= PM_ENV_MAX) {
+           POWER_LOG(ERR, "Unsupported power management

environment\n");

drivers/power/core/kvm-vm/meson.build

Quoted 5 lines
drivers/power/core/kvm-vm/meson.build new file +5 -0
+           return -EINVAL;
+   }
+
+   if (op->status != 0) {
+           POWER_LOG(ERR, "Power management env[%d] ops registered

already\n",

drivers/power/core/kvm-vm/meson.build

Quoted 10 lines
drivers/power/core/kvm-vm/meson.build new file +10 -0
+                   op->env);
+           return -EINVAL;
+   }
+
+   if (!op->init || !op->exit || !op->check_env_support ||
+           !op->get_avail_freqs || !op->get_freq || !op->set_freq ||
+           !op->freq_up || !op->freq_down || !op->freq_max ||
+           !op->freq_min || !op->turbo_status || !op->enable_turbo ||
+           !op->disable_turbo || !op->get_caps) {
+           POWER_LOG(ERR, "Missing callbacks while registering power

ops\n");

drivers/power/core/kvm-vm/meson.build

Quoted 5 lines
drivers/power/core/kvm-vm/meson.build new file +4 -0
+           return -EINVAL;
+   }
+
+   ops = &rte_power_ops[op->env];

I don't see all drivers set 'op->env',

This 'rte_power_register_ops()' function copies ops from driver proved struct to library global 'rte_power_ops[]' array,

Quoted 6 lines

it can be possible to store ops pointer provided by driver, instead of copying it.

And it can be possible to link the ops in this function, instead of putting them to specific index, as only one ops can be active in a given time, it can be possible to store active ops pointer in a global variable which removes the need to have index accessible array for ops.

Agreed. I will rework this to a new struct which can hold a reference to the respective ops struct.

Quoted 43 lines

<...>

Quoted 21 lines
drivers/power/core/kvm-vm/meson.build new file +4 -13
@@ -177,59 +138,76 @@ int
 rte_power_init(unsigned int lcore_id)  {
    int ret = -1;
+   struct rte_power_ops *ops;
 
-   switch (global_default_env) {
-   case PM_ENV_ACPI_CPUFREQ:
-           return power_acpi_cpufreq_init(lcore_id);
-   case PM_ENV_KVM_VM:
-           return power_kvm_vm_init(lcore_id);
-   case PM_ENV_PSTATE_CPUFREQ:
-           return power_pstate_cpufreq_init(lcore_id);
-   case PM_ENV_CPPC_CPUFREQ:
-           return power_cppc_cpufreq_init(lcore_id);
-   case PM_ENV_AMD_PSTATE_CPUFREQ:
-           return power_amd_pstate_cpufreq_init(lcore_id);
-   default:
-           POWER_LOG(INFO, "Env isn't set yet!");
+   if (global_default_env != PM_ENV_NOT_SET) {
+           ops = &rte_power_ops[global_default_env];
+           if (!ops->status) {

drivers/power/core/kvm-vm/meson.build int · line 196

Quoted 10 lines
drivers/power/core/kvm-vm/meson.build new file +6 -1
+                   POWER_LOG(ERR, "Power management env[%d] not"
+                           " supported\n", global_default_env);
+                   goto out;
+           }
+           return ops->init(lcore_id);
    }
+   POWER_LOG(INFO, POWER, "Env isn't set yet!\n");
 
    /* Auto detect Environment */
-   POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq power

management...");

drivers/power/core/kvm-vm/meson.build int · line 156

Quoted 7 lines
drivers/power/core/kvm-vm/meson.build new file +2 -4
-   ret = power_acpi_cpufreq_init(lcore_id);
-   if (ret == 0) {
-           rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
-           goto out;
+   POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq"
+                   " power management...\n");
 
Quoted 5 lines

Shouldn't break the log, can break the line by keeping message whole:
POWER_LOG(INFO,

      "Attempting to initialise ACPI cpufreq power management...");

<...>

ACK

Quoted 10 lines

drivers/power/core/kvm-vm/meson.build extern "C" { · line 26

Quoted 8 lines
drivers/power/core/kvm-vm/meson.build new file +1 -1
@@ -21,7 +22,7 @@ extern "C" {
 /* Power Management Environment State */  enum power_management_env
 {PM_ENV_NOT_SET, PM_ENV_ACPI_CPUFREQ, PM_ENV_KVM_VM,
            PM_ENV_PSTATE_CPUFREQ, PM_ENV_CPPC_CPUFREQ,
-           PM_ENV_AMD_PSTATE_CPUFREQ};
+           PM_ENV_AMD_PSTATE_CPUFREQ, PM_ENV_MAX};
 

Syntax. Can we have enum item per line?

ACK

Quoted 26 lines
Quoted 9 lines
drivers/power/core/kvm-vm/meson.build new file +0 -0
 /**
  * Check if a specific power management environment type is supported

on a @@ -66,6 +67,97 @@ void rte_power_unset_env(void);
*/
enum power_management_env rte_power_get_env(void);

drivers/power/core/kvm-vm/meson.build new file +1 -0
+typedef int (*rte_power_cpufreq_init_t)(unsigned int lcore_id);

drivers/power/core/kvm-vm/meson.build

Quoted 10 lines
drivers/power/core/kvm-vm/meson.build new file +10 -0
+typedef int (*rte_power_cpufreq_exit_t)(unsigned int lcore_id);
+typedef int (*rte_power_check_env_support_t)(void);
+
+typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
+                                   uint32_t num);
+typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);
+typedef int (*rte_power_set_freq_t)(unsigned int lcore_id, uint32_t
+index); typedef int (*rte_power_freq_change_t)(unsigned int
+lcore_id);
+

I guess above is not required for users, what do you think to create a driver header file and move these to driver header file?

<...>

Quoted 12 lines

drivers/power/core/kvm-vm/meson.build

Quoted 10 lines
drivers/power/core/kvm-vm/meson.build new file +9 -0
+
+/**
+ * Macro to statically register the ops of a cpufreq driver.
+ */
+#define RTE_POWER_REGISTER_OPS(ops)                \
+   (RTE_INIT(power_hdlr_init_##ops)        \
+   {                                       \
+           rte_power_register_ops(&ops);   \
+   })

is () required around RTE_INIT()

This was added to address the checkpatch errors.

Quoted 26 lines
Quoted 26 lines
drivers/power/core/kvm-vm/meson.build new file +12 -0
+
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @param ops_index
+ *   The index of the ops struct in the ops struct table.
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_ops *
+rte_power_get_ops(int ops_index);
+
 /**
  * Initialize power management for a specific lcore. If rte_power_set_env() has
  * not been called then an auto-detect of the environment will start

and @@ -108,10 +200,14 @@ int rte_power_exit(unsigned int lcore_id);
* @return
* The number of available frequencies.
*/

drivers/power/core/kvm-vm/meson.build new file +3 -2
-typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
-           uint32_t num);
+static inline uint32_t
+rte_power_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t n) {
+   struct rte_power_ops *ops;
Quoted 6 lines

drivers/power/core/kvm-vm/meson.build

Quoted 4 lines
drivers/power/core/kvm-vm/meson.build new file +2 -1
-extern rte_power_freqs_t rte_power_freqs;
+   ops = rte_power_get_ops(rte_power_get_env());
+   return ops->get_avail_freqs(lcore_id, freqs, n); }

Why not proper functions but "static inline functions"?

These inline functions are expected to be called from datapath and to avoid additional cycles with the refactor.

Quoted 13 lines
Quoted 13 lines

/**
* Return the current index of available frequencies of a specific lcore.

drivers/power/core/kvm-vm/meson.build new file +3 -1
@@ -124,9 +220,14 @@ extern rte_power_freqs_t rte_power_freqs;
  * @return
  *  The current index of available frequencies.
  */
-typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);
+static inline uint32_t
+rte_power_get_freq(unsigned int lcore_id) {
+   struct rte_power_ops *ops;
 
Quoted 6 lines

drivers/power/core/kvm-vm/meson.build extern rte_power_freqs_t rte_power_freqs; · line 228

Quoted 3 lines
drivers/power/core/kvm-vm/meson.build new file +1 -1
-extern rte_power_get_freq_t rte_power_get_freq;
+   ops = rte_power_get_ops(rte_power_get_env());
 

As 'rte_power_get_env()' already returns a global variable, why not set a global ops pointer and directly access to them, is above abstraction providing any benefit?

rte_power_get_ops() internally will check if the respective ops struct is registered or not. I will rework it and keep global ops to get populated in rte_power_set_env().

Quoted 2 lines
#4 lore raw

Re: [RFC PATCH 1/2] power: refactor core power management library

In reply to Sivaprasad Tummala

lib/power/rte_power.c rte_power_set_env(enum power_management_env env) · line 111

Quoted 5 lines · Sivaprasad Tummala
lib/power/rte_power.c +5 -0
+	ops = rte_power_get_ops(env);
+	if (ops->status == 0) {
+		POWER_LOG(ERR, WER,
+			"Power Management Environment(%d) not"
+			" registered\n", env);

'WER' seems typo, causing build error.
Also CI reports a few other build errors, fyi.

#5 lore raw

Re: [RFC PATCH 1/2] power: refactor core power management library

In reply to Sivaprasad Tummala
Quoted 7 lines · Sivaprasad Tummala

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

Quoted 6 lines

These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

Good job. +1 to refacotor.

<...>

drivers/power/core/acpi/power_acpi_cpufreq.c

Quoted 30 lines
drivers/meson.build +1 -0
index f2be71bc05..e293c3945f 100644
@@ -28,6 +28,7 @@ subdirs = [
          'event',          # depends on common, bus, mempool and net.
          'baseband',       # depends on common and bus.
          'gpu',            # depends on common and bus.
+        'power',          # depends on common (in future).
  ]
 
  if meson.is_cross_build()
drivers/power/core/acpi/meson.build new file +8 -0
new file mode 100644
index 0000000000..d10ec8ee94
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_acpi_cpufreq.c')
+
+headers = files('power_acpi_cpufreq.h')
+
+deps += ['power']
drivers/power/core/acpi/power_acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +0 -0
similarity index 95%
rename from lib/power/power_acpi_cpufreq.c
rename to drivers/power/core/acpi/power_acpi_cpufreq.c

This file is in power lib.
How about remove the 'power' prefix of this file name? like acpi_cpufreq.c, cppc_cpufreq.c.

drivers/power/core/acpi/power_acpi_cpufreq.c int power_acpi_get_capabilities(unsigned int lcore_id, · line 581

Quoted 9 lines

index f8d978d03d..69d80ad2ae 100644

drivers/power/core/acpi/power_acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +2 -0
@@ -577,3 +577,22 @@ int power_acpi_get_capabilities(unsigned int lcore_id,
 
  	return 0;
  }
+
+static struct rte_power_ops acpi_ops = {

How about use the following structure name?
"struct rte_power_cpufreq_ops" or "struct rte_power_core_ops" After all, we also have other power ops, like uncore, right?

Quoted 319 lines
drivers/power/core/acpi/power_acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +17 -0
+	.init = power_acpi_cpufreq_init,
+	.exit = power_acpi_cpufreq_exit,
+	.check_env_support = power_acpi_cpufreq_check_supported,
+	.get_avail_freqs = power_acpi_cpufreq_freqs,
+	.get_freq = power_acpi_cpufreq_get_freq,
+	.set_freq = power_acpi_cpufreq_set_freq,
+	.freq_down = power_acpi_cpufreq_freq_down,
+	.freq_up = power_acpi_cpufreq_freq_up,
+	.freq_max = power_acpi_cpufreq_freq_max,
+	.freq_min = power_acpi_cpufreq_freq_min,
+	.turbo_status = power_acpi_turbo_status,
+	.enable_turbo = power_acpi_enable_turbo,
+	.disable_turbo = power_acpi_disable_turbo,
+	.get_caps = power_acpi_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(acpi_ops);
drivers/power/core/acpi/power_acpi_cpufreq.h renamed from lib/power/power_acpi_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_acpi_cpufreq.h
rename to drivers/power/core/acpi/power_acpi_cpufreq.h
drivers/power/core/amd-pstate/meson.build new file +8 -0
new file mode 100644
index 0000000000..8ec4c960f5
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_amd_pstate_cpufreq.c')
+
+headers = files('power_amd_pstate_cpufreq.h')
+
+deps += ['power']
drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.c renamed from lib/power/power_amd_pstate_cpufreq.c +19 -0
similarity index 95%
rename from lib/power/power_amd_pstate_cpufreq.c
rename to drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.c
index 028f84416b..9938de72a6 100644
@@ -700,3 +700,22 @@ power_amd_pstate_get_capabilities(unsigned int lcore_id,
 
  	return 0;
  }
+
+static struct rte_power_ops amd_pstate_ops = {
+	.init = power_amd_pstate_cpufreq_init,
+	.exit = power_amd_pstate_cpufreq_exit,
+	.check_env_support = power_amd_pstate_cpufreq_check_supported,
+	.get_avail_freqs = power_amd_pstate_cpufreq_freqs,
+	.get_freq = power_amd_pstate_cpufreq_get_freq,
+	.set_freq = power_amd_pstate_cpufreq_set_freq,
+	.freq_down = power_amd_pstate_cpufreq_freq_down,
+	.freq_up = power_amd_pstate_cpufreq_freq_up,
+	.freq_max = power_amd_pstate_cpufreq_freq_max,
+	.freq_min = power_amd_pstate_cpufreq_freq_min,
+	.turbo_status = power_amd_pstate_turbo_status,
+	.enable_turbo = power_amd_pstate_enable_turbo,
+	.disable_turbo = power_amd_pstate_disable_turbo,
+	.get_caps = power_amd_pstate_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(amd_pstate_ops);
drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.h renamed from lib/power/power_amd_pstate_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_amd_pstate_cpufreq.h
rename to drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.h
drivers/power/core/cppc/meson.build new file +8 -0
new file mode 100644
index 0000000000..06f3b99bb8
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_cppc_cpufreq.c')
+
+headers = files('power_cppc_cpufreq.h')
+
+deps += ['power']
drivers/power/core/cppc/power_cppc_cpufreq.c renamed from lib/power/power_cppc_cpufreq.c +19 -0
similarity index 96%
rename from lib/power/power_cppc_cpufreq.c
rename to drivers/power/core/cppc/power_cppc_cpufreq.c
index 3ddf39bd76..605f633309 100644
@@ -685,3 +685,22 @@ power_cppc_get_capabilities(unsigned int lcore_id,
 
  	return 0;
  }
+
+static struct rte_power_ops cppc_ops = {
+	.init = power_cppc_cpufreq_init,
+	.exit = power_cppc_cpufreq_exit,
+	.check_env_support = power_cppc_cpufreq_check_supported,
+	.get_avail_freqs = power_cppc_cpufreq_freqs,
+	.get_freq = power_cppc_cpufreq_get_freq,
+	.set_freq = power_cppc_cpufreq_set_freq,
+	.freq_down = power_cppc_cpufreq_freq_down,
+	.freq_up = power_cppc_cpufreq_freq_up,
+	.freq_max = power_cppc_cpufreq_freq_max,
+	.freq_min = power_cppc_cpufreq_freq_min,
+	.turbo_status = power_cppc_turbo_status,
+	.enable_turbo = power_cppc_enable_turbo,
+	.disable_turbo = power_cppc_disable_turbo,
+	.get_caps = power_cppc_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(cppc_ops);
drivers/power/core/cppc/power_cppc_cpufreq.h renamed from lib/power/power_cppc_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_cppc_cpufreq.h
rename to drivers/power/core/cppc/power_cppc_cpufreq.h
drivers/power/core/kvm-vm/guest_channel.c renamed from lib/power/guest_channel.c +0 -0
similarity index 100%
rename from lib/power/guest_channel.c
rename to drivers/power/core/kvm-vm/guest_channel.c
drivers/power/core/kvm-vm/guest_channel.h renamed from lib/power/guest_channel.h +0 -0
similarity index 100%
rename from lib/power/guest_channel.h
rename to drivers/power/core/kvm-vm/guest_channel.h
drivers/power/core/kvm-vm/meson.build new file +20 -0
new file mode 100644
index 0000000000..3150c6674b
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 AMD Limited.
+#
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+sources = files(
+        'guest_channel.c',
+        'power_kvm_vm.c',
+)
+
+headers = files(
+        'guest_channel.h',
+        'power_kvm_vm.h',
+)
+deps += ['power']
drivers/power/core/kvm-vm/power_kvm_vm.c renamed from lib/power/power_kvm_vm.c +19 -0
similarity index 83%
rename from lib/power/power_kvm_vm.c
rename to drivers/power/core/kvm-vm/power_kvm_vm.c
index f15be8fac5..a5d6984d26 100644
@@ -137,3 +137,22 @@ int power_kvm_vm_get_capabilities(__rte_unused unsigned int lcore_id,
  	POWER_LOG(ERR, "rte_power_get_capabilities is not implemented for Virtual Machine Power Management");
  	return -ENOTSUP;
  }
+
+static struct rte_power_ops kvm_vm_ops = {
+	.init = power_kvm_vm_init,
+	.exit = power_kvm_vm_exit,
+	.check_env_support = power_kvm_vm_check_supported,
+	.get_avail_freqs = power_kvm_vm_freqs,
+	.get_freq = power_kvm_vm_get_freq,
+	.set_freq = power_kvm_vm_set_freq,
+	.freq_down = power_kvm_vm_freq_down,
+	.freq_up = power_kvm_vm_freq_up,
+	.freq_max = power_kvm_vm_freq_max,
+	.freq_min = power_kvm_vm_freq_min,
+	.turbo_status = power_kvm_vm_turbo_status,
+	.enable_turbo = power_kvm_vm_enable_turbo,
+	.disable_turbo = power_kvm_vm_disable_turbo,
+	.get_caps = power_kvm_vm_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(kvm_vm_ops);
drivers/power/core/kvm-vm/power_kvm_vm.h renamed from lib/power/power_kvm_vm.h +0 -0
similarity index 100%
rename from lib/power/power_kvm_vm.h
rename to drivers/power/core/kvm-vm/power_kvm_vm.h
drivers/power/core/meson.build new file +12 -0
new file mode 100644
index 0000000000..4081dafaa0
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+drivers = [
+        'acpi',
+        'amd-pstate',
+        'cppc',
+        'kvm-vm',
+        'pstate'
+]
+
+std_deps = ['power']
drivers/power/core/pstate/meson.build new file +8 -0
new file mode 100644
index 0000000000..1025c64e48
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_pstate_cpufreq.c')
+
+headers = files('power_pstate_cpufreq.h')
+
+deps += ['power']
drivers/power/core/pstate/power_pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c +19 -0
similarity index 96%
rename from lib/power/power_pstate_cpufreq.c
rename to drivers/power/core/pstate/power_pstate_cpufreq.c
index 73138dc4e4..d4c3645ff8 100644
@@ -888,3 +888,22 @@ int power_pstate_get_capabilities(unsigned int lcore_id,
 
  	return 0;
  }
+
+static struct rte_power_ops pstate_ops = {
+	.init = power_pstate_cpufreq_init,
+	.exit = power_pstate_cpufreq_exit,
+	.check_env_support = power_pstate_cpufreq_check_supported,
+	.get_avail_freqs = power_pstate_cpufreq_freqs,
+	.get_freq = power_pstate_cpufreq_get_freq,
+	.set_freq = power_pstate_cpufreq_set_freq,
+	.freq_down = power_pstate_cpufreq_freq_down,
+	.freq_up = power_pstate_cpufreq_freq_up,
+	.freq_max = power_pstate_cpufreq_freq_max,
+	.freq_min = power_pstate_cpufreq_freq_min,
+	.turbo_status = power_pstate_turbo_status,
+	.enable_turbo = power_pstate_enable_turbo,
+	.disable_turbo = power_pstate_disable_turbo,
+	.get_caps = power_pstate_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(pstate_ops);
drivers/power/core/pstate/power_pstate_cpufreq.h renamed from lib/power/power_pstate_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_pstate_cpufreq.h
rename to drivers/power/core/pstate/power_pstate_cpufreq.h
drivers/power/meson.build new file +8 -0
new file mode 100644
index 0000000000..7d9034c7ac
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+drivers = [
+        'core',
+]
+
+std_deps = ['power']
lib/power/meson.build +0 -6
index b8426589b2..207d96d877 100644
@@ -12,14 +12,8 @@ if not is_linux
      reason = 'only supported on Linux'
  endif
  sources = files(
-        'guest_channel.c',
-        'power_acpi_cpufreq.c',
-        'power_amd_pstate_cpufreq.c',
          'power_common.c',
-        'power_cppc_cpufreq.c',
-        'power_kvm_vm.c',
          'power_intel_uncore.c',
-        'power_pstate_cpufreq.c',
          'rte_power.c',
          'rte_power_uncore.c',
          'rte_power_pmd_mgmt.c',
lib/power/power_common.h +11 -0
index 30966400ba..c90b611f4f 100644
@@ -23,13 +23,24 @@ extern int power_logtype;
  #endif
 
  /* check if scaling driver matches one we want */
+__rte_internal
  int cpufreq_check_scaling_driver(const char *driver);
+
+__rte_internal
  int power_set_governor(unsigned int lcore_id, const char *new_governor,
  		char *orig_governor, size_t orig_governor_len);
+
+__rte_internal
  int open_core_sysfs_file(FILE **f, const char *mode, const char *format, ...)
  		__rte_format_printf(3, 4);
+
+__rte_internal
  int read_core_sysfs_u32(FILE *f, uint32_t *val);
+
+__rte_internal
  int read_core_sysfs_s(FILE *f, char *buf, unsigned int len);
+
+__rte_internal
  int write_core_sysfs_s(FILE *f, const char *str);
 
  #endif /* _POWER_COMMON_H_ */
lib/power/rte_power.c +0 -0
index 36c3f3da98..70176807f4 100644
@@ -8,64 +8,80 @@
  #include <rte_spinlock.h>

lib/power/rte_power.c line 13

Quoted 10 lines
lib/power/rte_power.c +0 -5
 
  #include "rte_power.h"
-#include "power_acpi_cpufreq.h"
-#include "power_cppc_cpufreq.h"
  #include "power_common.h"
-#include "power_kvm_vm.h"
-#include "power_pstate_cpufreq.h"
-#include "power_amd_pstate_cpufreq.h"
 
  enum power_management_env global_default_env = PM_ENV_NOT_SET;

use a pointer to save the current power cpufreq ops?

Quoted 36 lines
lib/power/rte_power.c +18 -15
  static rte_spinlock_t global_env_cfg_lock = RTE_SPINLOCK_INITIALIZER;
+static struct rte_power_ops rte_power_ops[PM_ENV_MAX];
 
-/* function pointers */
-rte_power_freqs_t rte_power_freqs  = NULL;
-rte_power_get_freq_t rte_power_get_freq = NULL;
-rte_power_set_freq_t rte_power_set_freq = NULL;
-rte_power_freq_change_t rte_power_freq_up = NULL;
-rte_power_freq_change_t rte_power_freq_down = NULL;
-rte_power_freq_change_t rte_power_freq_max = NULL;
-rte_power_freq_change_t rte_power_freq_min = NULL;
-rte_power_freq_change_t rte_power_turbo_status;
-rte_power_freq_change_t rte_power_freq_enable_turbo;
-rte_power_freq_change_t rte_power_freq_disable_turbo;
-rte_power_get_capabilities_t rte_power_get_capabilities;
-
-static void
-reset_power_function_ptrs(void)
+/* register the ops struct in rte_power_ops, return 0 on success. */
+int
+rte_power_register_ops(const struct rte_power_ops *op)
+{
+	struct rte_power_ops *ops;
+
+	if (op->env >= PM_ENV_MAX) {
+		POWER_LOG(ERR, "Unsupported power management environment\n");
+		return -EINVAL;
+	}
+
+	if (op->status != 0) {
+		POWER_LOG(ERR, "Power management env[%d] ops registered already\n",
+			op->env);
+		return -EINVAL;
+	}
+

lib/power/rte_power.c line 44

Quoted 10 lines
lib/power/rte_power.c +10 -0
+	if (!op->init || !op->exit || !op->check_env_support ||
+		!op->get_avail_freqs || !op->get_freq || !op->set_freq ||
+		!op->freq_up || !op->freq_down || !op->freq_max ||
+		!op->freq_min || !op->turbo_status || !op->enable_turbo ||
+		!op->disable_turbo || !op->get_caps) {
+		POWER_LOG(ERR, "Missing callbacks while registering power ops\n");
+		return -EINVAL;
+	}
+
+	ops = &rte_power_ops[op->env];

It is better to use a global linked list instead of an array.
And we should extract a list structure including this ops structure and this ops's owner.

Quoted 4 lines
lib/power/rte_power.c +4 -0
+	ops->env = op->env;
+	ops->init = op->init;
+	ops->exit = op->exit;
+	ops->check_env_support = op->check_env_support;

lib/power/rte_power.c line 58

Quoted 10 lines
lib/power/rte_power.c +10 -0
+	ops->get_avail_freqs = op->get_avail_freqs;
+	ops->get_freq = op->get_freq;
+	ops->set_freq = op->set_freq;
+	ops->freq_up = op->freq_up;
+	ops->freq_down = op->freq_down;
+	ops->freq_max = op->freq_max;
+	ops->freq_min = op->freq_min;
+	ops->turbo_status = op->turbo_status;
+	ops->enable_turbo = op->enable_turbo;
+	ops->disable_turbo = op->disable_turbo;

*ops = *op?

lib/power/rte_power.c line 59

Quoted 1 line
lib/power/rte_power.c +1 -0
+	ops->status = 1; /* registered */

status --> registered?
But if use ops linked list, this flag also can be removed.

lib/power/rte_power.c line 65

Quoted 6 lines
lib/power/rte_power.c +6 -0
+
+	return 0;
+}
+
+struct rte_power_ops *
+rte_power_get_ops(int ops_index)

AFAICS, there is only one cpufreq driver on one platform and just have one power_cpufreq_ops to use for user. We don't need user to get other power ops, and user just want to know the power ops using currently, right? So using 'index' toget this ops is not good.

Quoted 59 lines
lib/power/rte_power.c +19 -25
  {
-	rte_power_freqs  = NULL;
-	rte_power_get_freq = NULL;
-	rte_power_set_freq = NULL;
-	rte_power_freq_up = NULL;
-	rte_power_freq_down = NULL;
-	rte_power_freq_max = NULL;
-	rte_power_freq_min = NULL;
-	rte_power_turbo_status = NULL;
-	rte_power_freq_enable_turbo = NULL;
-	rte_power_freq_disable_turbo = NULL;
-	rte_power_get_capabilities = NULL;
+	RTE_VERIFY((ops_index >= PM_ENV_NOT_SET) && (ops_index < PM_ENV_MAX));
+	RTE_VERIFY(rte_power_ops[ops_index].status != 0);
+
+	return &rte_power_ops[ops_index];
  }
 
  int
  rte_power_check_env_supported(enum power_management_env env)
  {
-	switch (env) {
-	case PM_ENV_ACPI_CPUFREQ:
-		return power_acpi_cpufreq_check_supported();
-	case PM_ENV_PSTATE_CPUFREQ:
-		return power_pstate_cpufreq_check_supported();
-	case PM_ENV_KVM_VM:
-		return power_kvm_vm_check_supported();
-	case PM_ENV_CPPC_CPUFREQ:
-		return power_cppc_cpufreq_check_supported();
-	case PM_ENV_AMD_PSTATE_CPUFREQ:
-		return power_amd_pstate_cpufreq_check_supported();
-	default:
-		rte_errno = EINVAL;
-		return -1;
+	struct rte_power_ops *ops;
+
+	if ((env > PM_ENV_NOT_SET) && (env < PM_ENV_MAX)) {
+		ops = rte_power_get_ops(env);
+		return ops->check_env_support();
  	}
+
+	rte_errno = EINVAL;
+	return -1;
  }
 
  int
@@ -80,80 +96,26 @@ rte_power_set_env(enum power_management_env env)
  	}
 
  	int ret = 0;
+	struct rte_power_ops *ops;
+
+	if ((env == PM_ENV_NOT_SET) || (env >= PM_ENV_MAX)) {
+		POWER_LOG(ERR, "Invalid Power Management Environment(%d)"
+				" set\n", env);
+		ret = -1;
+	}
 

<...>

lib/power/rte_power.c

Quoted 1 line
lib/power/rte_power.c +1 -0
+	ops = rte_power_get_ops(env);

To find the target ops from the global list according to the env?

lib/power/rte_power.c rte_power_set_env(enum power_management_env env) · line 116

Quoted 9 lines
lib/power/rte_power.c +4 -0
+	if (ops->status == 0) {
+		POWER_LOG(ERR, WER,
+			"Power Management Environment(%d) not"
+			" registered\n", env);
  		ret = -1;
  	}
 
  	if (ret == 0)
  		global_default_env = env;

It is more convenient to use a global variable to point to the default power_cpufreq ops or its list node.

Quoted 116 lines
lib/power/rte_power.c +47 -42
-	else {
+	else
  		global_default_env = PM_ENV_NOT_SET;
-		reset_power_function_ptrs();
-	}
 
  	rte_spinlock_unlock(&global_env_cfg_lock);
  	return ret;
@@ -164,7 +126,6 @@ rte_power_unset_env(void)
  {
  	rte_spinlock_lock(&global_env_cfg_lock);
  	global_default_env = PM_ENV_NOT_SET;
-	reset_power_function_ptrs();
  	rte_spinlock_unlock(&global_env_cfg_lock);
  }
 
@@ -177,59 +138,76 @@ int
  rte_power_init(unsigned int lcore_id)
  {
  	int ret = -1;
+	struct rte_power_ops *ops;
 
-	switch (global_default_env) {
-	case PM_ENV_ACPI_CPUFREQ:
-		return power_acpi_cpufreq_init(lcore_id);
-	case PM_ENV_KVM_VM:
-		return power_kvm_vm_init(lcore_id);
-	case PM_ENV_PSTATE_CPUFREQ:
-		return power_pstate_cpufreq_init(lcore_id);
-	case PM_ENV_CPPC_CPUFREQ:
-		return power_cppc_cpufreq_init(lcore_id);
-	case PM_ENV_AMD_PSTATE_CPUFREQ:
-		return power_amd_pstate_cpufreq_init(lcore_id);
-	default:
-		POWER_LOG(INFO, "Env isn't set yet!");
+	if (global_default_env != PM_ENV_NOT_SET) {
+		ops = &rte_power_ops[global_default_env];
+		if (!ops->status) {
+			POWER_LOG(ERR, "Power management env[%d] not"
+				" supported\n", global_default_env);
+			goto out;
+		}
+		return ops->init(lcore_id);
  	}
+	POWER_LOG(INFO, POWER, "Env isn't set yet!\n");
 
  	/* Auto detect Environment */
-	POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq power management...");
-	ret = power_acpi_cpufreq_init(lcore_id);
-	if (ret == 0) {
-		rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
-		goto out;
+	POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq"
+			" power management...\n");
+	ops = &rte_power_ops[PM_ENV_ACPI_CPUFREQ];
+	if (ops->status) {
+		ret = ops->init(lcore_id);
+		if (ret == 0) {
+			rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
+			goto out;
+		}
  	}
 
-	POWER_LOG(INFO, "Attempting to initialise PSTAT power management...");
-	ret = power_pstate_cpufreq_init(lcore_id);
-	if (ret == 0) {
-		rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
-		goto out;
+	POWER_LOG(INFO, "Attempting to initialise PSTAT"
+			" power management...\n");
+	ops = &rte_power_ops[PM_ENV_PSTATE_CPUFREQ];
+	if (ops->status) {
+		ret = ops->init(lcore_id);
+		if (ret == 0) {
+			rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
+			goto out;
+		}
  	}
 
-	POWER_LOG(INFO, "Attempting to initialise AMD PSTATE power management...");
-	ret = power_amd_pstate_cpufreq_init(lcore_id);
-	if (ret == 0) {
-		rte_power_set_env(PM_ENV_AMD_PSTATE_CPUFREQ);
-		goto out;
+	POWER_LOG(INFO,	"Attempting to initialise AMD PSTATE"
+			" power management...\n");
+	ops = &rte_power_ops[PM_ENV_AMD_PSTATE_CPUFREQ];
+	if (ops->status) {
+		ret = ops->init(lcore_id);
+		if (ret == 0) {
+			rte_power_set_env(PM_ENV_AMD_PSTATE_CPUFREQ);
+			goto out;
+		}
  	}
 
-	POWER_LOG(INFO, "Attempting to initialise CPPC power management...");
-	ret = power_cppc_cpufreq_init(lcore_id);
-	if (ret == 0) {
-		rte_power_set_env(PM_ENV_CPPC_CPUFREQ);
-		goto out;
+	POWER_LOG(INFO, "Attempting to initialise CPPC power"
+			" management...\n");
+	ops = &rte_power_ops[PM_ENV_CPPC_CPUFREQ];
+	if (ops->status) {
+		ret = ops->init(lcore_id);
+		if (ret == 0) {
+			rte_power_set_env(PM_ENV_CPPC_CPUFREQ);
+			goto out;
+		}
  	}
 
-	POWER_LOG(INFO, "Attempting to initialise VM power management...");
-	ret = power_kvm_vm_init(lcore_id);
-	if (ret == 0) {
-		rte_power_set_env(PM_ENV_KVM_VM);
-		goto out;

lib/power/rte_power.c int · line 208

Quoted 10 lines
lib/power/rte_power.c +9 -0
+	POWER_LOG(INFO, "Attempting to initialise VM power"
+			" management...\n");
+	ops = &rte_power_ops[PM_ENV_KVM_VM];
+	if (ops->status) {
+		ret = ops->init(lcore_id);
+		if (ret == 0) {
+			rte_power_set_env(PM_ENV_KVM_VM);
+			goto out;
+		}
  	}

If we use a linked list, above code can be simpled like this:
->
for_each_power_cpufreq_ops(ops, ...) {
    ret = ops->init()
    if (ret) {
        ....
    }
}

Quoted 48 lines
lib/power/rte_power.c +9 -16
-	POWER_LOG(ERR, "Unable to set Power Management Environment for lcore "
-			"%u", lcore_id);
+	POWER_LOG(ERR, "Unable to set Power Management Environment"
+			" for lcore %u\n", lcore_id);
  out:
  	return ret;
  }
@@ -237,21 +215,14 @@ rte_power_init(unsigned int lcore_id)
  int
  rte_power_exit(unsigned int lcore_id)
  {
-	switch (global_default_env) {
-	case PM_ENV_ACPI_CPUFREQ:
-		return power_acpi_cpufreq_exit(lcore_id);
-	case PM_ENV_KVM_VM:
-		return power_kvm_vm_exit(lcore_id);
-	case PM_ENV_PSTATE_CPUFREQ:
-		return power_pstate_cpufreq_exit(lcore_id);
-	case PM_ENV_CPPC_CPUFREQ:
-		return power_cppc_cpufreq_exit(lcore_id);
-	case PM_ENV_AMD_PSTATE_CPUFREQ:
-		return power_amd_pstate_cpufreq_exit(lcore_id);
-	default:
-		POWER_LOG(ERR, "Environment has not been set, unable to exit gracefully");
+	struct rte_power_ops *ops;
 
+	if (global_default_env != PM_ENV_NOT_SET) {
+		ops = &rte_power_ops[global_default_env];
+		return ops->exit(lcore_id);
  	}
-	return -1;
+	POWER_LOG(ERR, "Environment has not been set, unable "
+			"to exit gracefully\n");
 
+	return -1;
  }
lib/power/rte_power.h +1 -0
index 4fa4afe399..749bb823ab 100644
@@ -1,5 +1,6 @@
  /* SPDX-License-Identifier: BSD-3-Clause
   * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 AMD Limited
   */
 
  #ifndef _RTE_POWER_H

lib/power/rte_power.h extern "C" { · line 25

Quoted 5 lines
lib/power/rte_power.h +1 -1
@@ -21,7 +22,7 @@ extern "C" {
  /* Power Management Environment State */
  enum power_management_env {PM_ENV_NOT_SET, PM_ENV_ACPI_CPUFREQ, PM_ENV_KVM_VM,
  		PM_ENV_PSTATE_CPUFREQ, PM_ENV_CPPC_CPUFREQ,
-		PM_ENV_AMD_PSTATE_CPUFREQ};
+		PM_ENV_AMD_PSTATE_CPUFREQ, PM_ENV_MAX};

"enum power_management_env" is not good. may be like "enum power_cpufreq_driver_type"?
In previous linked list structure to be defined, may be directly use a string name instead of a fixed enum is better. Becuase the new "PM_ENV_MAX" will  lead to break ABI when add a new cpufreq driver.

Quoted 55 lines
lib/power/rte_power.h +48 -0
  /**
   * Check if a specific power management environment type is supported on a
@@ -66,6 +67,97 @@ void rte_power_unset_env(void);
   */
  enum power_management_env rte_power_get_env(void);
 
+typedef int (*rte_power_cpufreq_init_t)(unsigned int lcore_id);
+typedef int (*rte_power_cpufreq_exit_t)(unsigned int lcore_id);
+typedef int (*rte_power_check_env_support_t)(void);
+
+typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
+					uint32_t num);
+typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);
+typedef int (*rte_power_set_freq_t)(unsigned int lcore_id, uint32_t index);
+typedef int (*rte_power_freq_change_t)(unsigned int lcore_id);
+
+/**
+ * Function pointer definition for generic frequency change functions. Review
+ * each environments specific documentation for usage.
+ *
+ * @param lcore_id
+ *  lcore id.
+ *
+ * @return
+ *  - 1 on success with frequency changed.
+ *  - 0 on success without frequency changed.
+ *  - Negative on error.
+ */
+
+/**
+ * Power capabilities summary.
+ */
+struct rte_power_core_capabilities {
+	union {
+		uint64_t capabilities;
+		struct {
+			uint64_t turbo:1;       /**< Turbo can be enabled. */
+			uint64_t priority:1;    /**< SST-BF high freq core */
+		};
+	};
+};
+
+typedef int (*rte_power_get_capabilities_t)(unsigned int lcore_id,
+				struct rte_power_core_capabilities *caps);
+
+/** Structure defining core power operations structure */
+struct rte_power_ops {
+uint8_t status;                         /**< ops register status. */
+	enum power_management_env env;          /**< power mgmt env. */
+	rte_power_cpufreq_init_t init;    /**< Initialize power management. */
+	rte_power_cpufreq_exit_t exit;    /**< Exit power management. */
+	rte_power_check_env_support_t check_env_support; /**< verify env is supported. */
+	rte_power_freqs_t get_avail_freqs; /**< Get the available frequencies. */
+	rte_power_get_freq_t get_freq; /**< Get frequency index. */

lib/power/rte_power.h void rte_power_unset_env(void); · line 127

Quoted 10 lines
lib/power/rte_power.h +10 -0
+	rte_power_set_freq_t set_freq; /**< Set frequency index. */
+	rte_power_freq_change_t freq_up;   /**< Scale up frequency. */
+	rte_power_freq_change_t freq_down; /**< Scale down frequency. */
+	rte_power_freq_change_t freq_max;  /**< Scale up frequency to highest. */
+	rte_power_freq_change_t freq_min;  /**< Scale up frequency to lowest. */
+	rte_power_freq_change_t turbo_status; /**< Get Turbo status. */
+	rte_power_freq_change_t enable_turbo; /**< Enable Turbo. */
+	rte_power_freq_change_t disable_turbo; /**< Disable Turbo. */
+	rte_power_get_capabilities_t get_caps; /**< power capabilities. */
+} __rte_cache_aligned;

Suggest that fix this sturcture, like:
struct rte_power_cpufreq_list {
    char name[];   // like "cppc_cpufreq", "pstate_cpufreq" struct rte_power_cpufreq *ops;
    struct rte_power_cpufreq_list *node;
}

Quoted 41 lines
lib/power/rte_power.h +33 -1
+
+/**
+ * Register power cpu frequency operations.
+ *
+ * @param ops
+ *   Pointer to an ops structure to register.
+ * @return
+ *   - >=0: Success; return the index of the ops struct in the table.
+ *   - -EINVAL - error while registering ops struct.
+ */
+__rte_internal
+int rte_power_register_ops(const struct rte_power_ops *ops);
+
+/**
+ * Macro to statically register the ops of a cpufreq driver.
+ */
+#define RTE_POWER_REGISTER_OPS(ops)		\
+	(RTE_INIT(power_hdlr_init_##ops)	\
+	{					\
+		rte_power_register_ops(&ops);	\
+	})
+
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @param ops_index
+ *   The index of the ops struct in the ops struct table.
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_ops *
+rte_power_get_ops(int ops_index);
+
  /**
   * Initialize power management for a specific lcore. If rte_power_set_env() has
   * not been called then an auto-detect of the environment will start and
@@ -108,10 +200,14 @@ int rte_power_exit(unsigned int lcore_id);
   * @return
   *  The number of available frequencies.
   */
-typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,

lib/power/rte_power.h int rte_power_exit(unsigned int lcore_id); · line 210

Quoted 10 lines
lib/power/rte_power.h +7 -2
-		uint32_t num);
+static inline uint32_t
+rte_power_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t n)
+{
+	struct rte_power_ops *ops;
 
-extern rte_power_freqs_t rte_power_freqs;
+	ops = rte_power_get_ops(rte_power_get_env());
+	return ops->get_avail_freqs(lcore_id, freqs, n);
+}

nice.
<...>

#6 lore raw

Re: [RFC PATCH 1/2] power: refactor core power management library

Quoted 16 lines · lihuisong (C)
Quoted 7 lines · Sivaprasad Tummala

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

Quoted 6 lines

These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

Quoted 4 lines

Good job. +1 to refacotor.

<...>

Also a +1 from me, looks like a sensible re-organisation of the power code.

Regards,
Dave.

Quoted 825 lines

drivers/power/core/acpi/power_acpi_cpufreq.c

Quoted 29 lines
drivers/meson.build +0 -0
index f2be71bc05..e293c3945f 100644
@@ -28,6 +28,7 @@ subdirs = [

          'event',          # depends on common, bus, mempool and net. 'baseband',       # depends on common and bus. 'gpu',            # depends on common and bus.
+        'power',          # depends on common (in future). ]
    if meson.is_cross_build()

drivers/power/core/acpi/meson.build new file +8 -0
new file mode 100644
index 0000000000..d10ec8ee94
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_acpi_cpufreq.c')
+
+headers = files('power_acpi_cpufreq.h')
+
+deps += ['power']
drivers/power/core/acpi/power_acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +0 -0
similarity index 95%
rename from lib/power/power_acpi_cpufreq.c
rename to drivers/power/core/acpi/power_acpi_cpufreq.c

This file is in power lib.
How about remove the 'power' prefix of this file name? like acpi_cpufreq.c, cppc_cpufreq.c.

drivers/power/core/acpi/power_acpi_cpufreq.c int power_acpi_get_capabilities(unsigned int · line 581

Quoted 9 lines

index f8d978d03d..69d80ad2ae 100644

drivers/power/core/acpi/power_acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +2 -0
@@ -577,3 +577,22 @@ int power_acpi_get_capabilities(unsigned int 
 lcore_id,
         return 0;
   }
+
+static struct rte_power_ops acpi_ops = {

How about use the following structure name?
"struct rte_power_cpufreq_ops" or "struct rte_power_core_ops" After all, we also have other power ops, like uncore, right?

Quoted 316 lines
drivers/power/core/acpi/power_acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +17 -0
+    .init = power_acpi_cpufreq_init,
+    .exit = power_acpi_cpufreq_exit,
+    .check_env_support = power_acpi_cpufreq_check_supported,
+    .get_avail_freqs = power_acpi_cpufreq_freqs,
+    .get_freq = power_acpi_cpufreq_get_freq,
+    .set_freq = power_acpi_cpufreq_set_freq,
+    .freq_down = power_acpi_cpufreq_freq_down,
+    .freq_up = power_acpi_cpufreq_freq_up,
+    .freq_max = power_acpi_cpufreq_freq_max,
+    .freq_min = power_acpi_cpufreq_freq_min,
+    .turbo_status = power_acpi_turbo_status,
+    .enable_turbo = power_acpi_enable_turbo,
+    .disable_turbo = power_acpi_disable_turbo,
+    .get_caps = power_acpi_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(acpi_ops);
drivers/power/core/acpi/power_acpi_cpufreq.h renamed from lib/power/power_acpi_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_acpi_cpufreq.h
rename to drivers/power/core/acpi/power_acpi_cpufreq.h
drivers/power/core/amd-pstate/meson.build new file +8 -0
new file mode 100644
index 0000000000..8ec4c960f5
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_amd_pstate_cpufreq.c')
+
+headers = files('power_amd_pstate_cpufreq.h')
+
+deps += ['power']
drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.c renamed from lib/power/power_amd_pstate_cpufreq.c +19 -0
similarity index 95%
rename from lib/power/power_amd_pstate_cpufreq.c
rename to drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.c
index 028f84416b..9938de72a6 100644
@@ -700,3 +700,22 @@ power_amd_pstate_get_capabilities(unsigned int 
 lcore_id,
         return 0;
   }
+
+static struct rte_power_ops amd_pstate_ops = {
+    .init = power_amd_pstate_cpufreq_init,
+    .exit = power_amd_pstate_cpufreq_exit,
+    .check_env_support = power_amd_pstate_cpufreq_check_supported,
+    .get_avail_freqs = power_amd_pstate_cpufreq_freqs,
+    .get_freq = power_amd_pstate_cpufreq_get_freq,
+    .set_freq = power_amd_pstate_cpufreq_set_freq,
+    .freq_down = power_amd_pstate_cpufreq_freq_down,
+    .freq_up = power_amd_pstate_cpufreq_freq_up,
+    .freq_max = power_amd_pstate_cpufreq_freq_max,
+    .freq_min = power_amd_pstate_cpufreq_freq_min,
+    .turbo_status = power_amd_pstate_turbo_status,
+    .enable_turbo = power_amd_pstate_enable_turbo,
+    .disable_turbo = power_amd_pstate_disable_turbo,
+    .get_caps = power_amd_pstate_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(amd_pstate_ops);
drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.h renamed from lib/power/power_amd_pstate_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_amd_pstate_cpufreq.h
rename to drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.h
drivers/power/core/cppc/meson.build new file +8 -0
new file mode 100644
index 0000000000..06f3b99bb8
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_cppc_cpufreq.c')
+
+headers = files('power_cppc_cpufreq.h')
+
+deps += ['power']
drivers/power/core/cppc/power_cppc_cpufreq.c renamed from lib/power/power_cppc_cpufreq.c +19 -0
similarity index 96%
rename from lib/power/power_cppc_cpufreq.c
rename to drivers/power/core/cppc/power_cppc_cpufreq.c
index 3ddf39bd76..605f633309 100644
@@ -685,3 +685,22 @@ power_cppc_get_capabilities(unsigned int lcore_id,
         return 0;
   }
+
+static struct rte_power_ops cppc_ops = {
+    .init = power_cppc_cpufreq_init,
+    .exit = power_cppc_cpufreq_exit,
+    .check_env_support = power_cppc_cpufreq_check_supported,
+    .get_avail_freqs = power_cppc_cpufreq_freqs,
+    .get_freq = power_cppc_cpufreq_get_freq,
+    .set_freq = power_cppc_cpufreq_set_freq,
+    .freq_down = power_cppc_cpufreq_freq_down,
+    .freq_up = power_cppc_cpufreq_freq_up,
+    .freq_max = power_cppc_cpufreq_freq_max,
+    .freq_min = power_cppc_cpufreq_freq_min,
+    .turbo_status = power_cppc_turbo_status,
+    .enable_turbo = power_cppc_enable_turbo,
+    .disable_turbo = power_cppc_disable_turbo,
+    .get_caps = power_cppc_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(cppc_ops);
drivers/power/core/cppc/power_cppc_cpufreq.h renamed from lib/power/power_cppc_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_cppc_cpufreq.h
rename to drivers/power/core/cppc/power_cppc_cpufreq.h
drivers/power/core/kvm-vm/guest_channel.c renamed from lib/power/guest_channel.c +0 -0
similarity index 100%
rename from lib/power/guest_channel.c
rename to drivers/power/core/kvm-vm/guest_channel.c
drivers/power/core/kvm-vm/guest_channel.h renamed from lib/power/guest_channel.h +0 -0
similarity index 100%
rename from lib/power/guest_channel.h
rename to drivers/power/core/kvm-vm/guest_channel.h
drivers/power/core/kvm-vm/meson.build new file +20 -0
new file mode 100644
index 0000000000..3150c6674b
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 AMD Limited.
+#
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+sources = files(
+        'guest_channel.c',
+        'power_kvm_vm.c',
+)
+
+headers = files(
+        'guest_channel.h',
+        'power_kvm_vm.h',
+)
+deps += ['power']
drivers/power/core/kvm-vm/power_kvm_vm.c renamed from lib/power/power_kvm_vm.c +0 -0
similarity index 83%
rename from lib/power/power_kvm_vm.c
rename to drivers/power/core/kvm-vm/power_kvm_vm.c
index f15be8fac5..a5d6984d26 100644
@@ -137,3 +137,22 @@ int power_kvm_vm_get_capabilities(__rte_unused 
 unsigned int lcore_id,
       POWER_LOG(ERR, "rte_power_get_capabilities is not implemented for Virtual Machine Power Management");
       return -ENOTSUP;

  }

drivers/power/core/kvm-vm/power_kvm_vm.c renamed from lib/power/power_kvm_vm.c +19 -0
+
+static struct rte_power_ops kvm_vm_ops = {
+    .init = power_kvm_vm_init,
+    .exit = power_kvm_vm_exit,
+    .check_env_support = power_kvm_vm_check_supported,
+    .get_avail_freqs = power_kvm_vm_freqs,
+    .get_freq = power_kvm_vm_get_freq,
+    .set_freq = power_kvm_vm_set_freq,
+    .freq_down = power_kvm_vm_freq_down,
+    .freq_up = power_kvm_vm_freq_up,
+    .freq_max = power_kvm_vm_freq_max,
+    .freq_min = power_kvm_vm_freq_min,
+    .turbo_status = power_kvm_vm_turbo_status,
+    .enable_turbo = power_kvm_vm_enable_turbo,
+    .disable_turbo = power_kvm_vm_disable_turbo,
+    .get_caps = power_kvm_vm_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(kvm_vm_ops);
drivers/power/core/kvm-vm/power_kvm_vm.h renamed from lib/power/power_kvm_vm.h +0 -0
similarity index 100%
rename from lib/power/power_kvm_vm.h
rename to drivers/power/core/kvm-vm/power_kvm_vm.h
drivers/power/core/meson.build new file +12 -0
new file mode 100644
index 0000000000..4081dafaa0
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+drivers = [
+        'acpi',
+        'amd-pstate',
+        'cppc',
+        'kvm-vm',
+        'pstate'
+]
+
+std_deps = ['power']
drivers/power/core/pstate/meson.build new file +8 -0
new file mode 100644
index 0000000000..1025c64e48
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_pstate_cpufreq.c')
+
+headers = files('power_pstate_cpufreq.h')
+
+deps += ['power']
drivers/power/core/pstate/power_pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c +19 -0
similarity index 96%
rename from lib/power/power_pstate_cpufreq.c
rename to drivers/power/core/pstate/power_pstate_cpufreq.c
index 73138dc4e4..d4c3645ff8 100644
@@ -888,3 +888,22 @@ int power_pstate_get_capabilities(unsigned int 
 lcore_id,
         return 0;
   }
+
+static struct rte_power_ops pstate_ops = {
+    .init = power_pstate_cpufreq_init,
+    .exit = power_pstate_cpufreq_exit,
+    .check_env_support = power_pstate_cpufreq_check_supported,
+    .get_avail_freqs = power_pstate_cpufreq_freqs,
+    .get_freq = power_pstate_cpufreq_get_freq,
+    .set_freq = power_pstate_cpufreq_set_freq,
+    .freq_down = power_pstate_cpufreq_freq_down,
+    .freq_up = power_pstate_cpufreq_freq_up,
+    .freq_max = power_pstate_cpufreq_freq_max,
+    .freq_min = power_pstate_cpufreq_freq_min,
+    .turbo_status = power_pstate_turbo_status,
+    .enable_turbo = power_pstate_enable_turbo,
+    .disable_turbo = power_pstate_disable_turbo,
+    .get_caps = power_pstate_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(pstate_ops);
drivers/power/core/pstate/power_pstate_cpufreq.h renamed from lib/power/power_pstate_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_pstate_cpufreq.h
rename to drivers/power/core/pstate/power_pstate_cpufreq.h
drivers/power/meson.build new file +8 -0
new file mode 100644
index 0000000000..7d9034c7ac
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+drivers = [
+        'core',
+]
+
+std_deps = ['power']
lib/power/meson.build +0 -0
index b8426589b2..207d96d877 100644
@@ -12,14 +12,8 @@ if not is_linux

      reason = 'only supported on Linux'
  endif
  sources = files(
-        'guest_channel.c',
-        'power_acpi_cpufreq.c',
-        'power_amd_pstate_cpufreq.c',
          'power_common.c',
-        'power_cppc_cpufreq.c',
-        'power_kvm_vm.c',
          'power_intel_uncore.c',
-        'power_pstate_cpufreq.c',
          'rte_power.c',
          'rte_power_uncore.c',
          'rte_power_pmd_mgmt.c',

lib/power/power_common.h +11 -0
index 30966400ba..c90b611f4f 100644
@@ -23,13 +23,24 @@ extern int power_logtype;
   #endif
     /* check if scaling driver matches one we want */
+__rte_internal
   int cpufreq_check_scaling_driver(const char *driver);
+
+__rte_internal
   int power_set_governor(unsigned int lcore_id, const char *new_governor,
           char *orig_governor, size_t orig_governor_len);
+
+__rte_internal
   int open_core_sysfs_file(FILE **f, const char *mode, const char *format, ...)
           __rte_format_printf(3, 4);
+
+__rte_internal
   int read_core_sysfs_u32(FILE *f, uint32_t *val);
+
+__rte_internal
   int read_core_sysfs_s(FILE *f, char *buf, unsigned int len);
+
+__rte_internal
   int write_core_sysfs_s(FILE *f, const char *str);
     #endif /* _POWER_COMMON_H_ */
lib/power/rte_power.c +0 -0
index 36c3f3da98..70176807f4 100644

lib/power/rte_power.c line 11

Quoted 9 lines
lib/power/rte_power.c +0 -5
@@ -8,64 +8,80 @@
   #include <rte_spinlock.h>
     #include "rte_power.h"
-#include "power_acpi_cpufreq.h"
-#include "power_cppc_cpufreq.h"
   #include "power_common.h"
-#include "power_kvm_vm.h"
-#include "power_pstate_cpufreq.h"
-#include "power_amd_pstate_cpufreq.h"
     enum power_management_env global_default_env = PM_ENV_NOT_SET;

use a pointer to save the current power cpufreq ops?

Quoted 38 lines
lib/power/rte_power.c +1 -0
     static rte_spinlock_t global_env_cfg_lock = RTE_SPINLOCK_INITIALIZER;
+static struct rte_power_ops rte_power_ops[PM_ENV_MAX];

  -/* function pointers */

lib/power/rte_power.c +13 -14
-rte_power_freqs_t rte_power_freqs  = NULL;
-rte_power_get_freq_t rte_power_get_freq = NULL;
-rte_power_set_freq_t rte_power_set_freq = NULL;
-rte_power_freq_change_t rte_power_freq_up = NULL;
-rte_power_freq_change_t rte_power_freq_down = NULL;
-rte_power_freq_change_t rte_power_freq_max = NULL;
-rte_power_freq_change_t rte_power_freq_min = NULL;
-rte_power_freq_change_t rte_power_turbo_status;
-rte_power_freq_change_t rte_power_freq_enable_turbo;
-rte_power_freq_change_t rte_power_freq_disable_turbo;
-rte_power_get_capabilities_t rte_power_get_capabilities;
-
-static void
-reset_power_function_ptrs(void)
+/* register the ops struct in rte_power_ops, return 0 on success. */
+int
+rte_power_register_ops(const struct rte_power_ops *op)
+{
+    struct rte_power_ops *ops;
+
+    if (op->env >= PM_ENV_MAX) {
+        POWER_LOG(ERR, "Unsupported power management environment\n");
+        return -EINVAL;
+    }
+
+    if (op->status != 0) {
+        POWER_LOG(ERR, "Power management env[%d] ops registered 

already\n",
+            op->env);
+        return -EINVAL;
+    }
+
+    if (!op->init || !op->exit || !op->check_env_support || +        !op->get_avail_freqs || !op->get_freq || !op->set_freq || +        !op->freq_up || !op->freq_down || !op->freq_max ||

Quoted 8 lines

+        !op->freq_min || !op->turbo_status || !op->enable_turbo || +        !op->disable_turbo || !op->get_caps) {
+        POWER_LOG(ERR, "Missing callbacks while registering power ops\n");
+        return -EINVAL;
+    }
+
+    ops = &rte_power_ops[op->env];

It is better to use a global linked list instead of an array.
And we should extract a list structure including this ops structure and this ops's owner.

Quoted 6 lines

+    ops->env = op->env;
+    ops->init = op->init;
+    ops->exit = op->exit;
+    ops->check_env_support = op->check_env_support; +    ops->get_avail_freqs = op->get_avail_freqs; +    ops->get_freq = op->get_freq;

Quoted 8 lines

+    ops->set_freq = op->set_freq;
+    ops->freq_up = op->freq_up;
+    ops->freq_down = op->freq_down;
+    ops->freq_max = op->freq_max;
+    ops->freq_min = op->freq_min;
+    ops->turbo_status = op->turbo_status;
+    ops->enable_turbo = op->enable_turbo;
+    ops->disable_turbo = op->disable_turbo;

*ops = *op?

Quoted 1 line

+    ops->status = 1; /* registered */

status --> registered?
But if use ops linked list, this flag also can be removed.

lib/power/rte_power.c line 65

Quoted 6 lines
lib/power/rte_power.c +6 -0
+
+    return 0;
+}
+
+struct rte_power_ops *
+rte_power_get_ops(int ops_index)

AFAICS, there is only one cpufreq driver on one platform and just have one power_cpufreq_ops to use for user. We don't need user to get other power ops, and user just want to know the power ops using currently, right? So using 'index' toget this ops is not good.

Quoted 56 lines

  {
-    rte_power_freqs  = NULL;
-    rte_power_get_freq = NULL;
-    rte_power_set_freq = NULL;
-    rte_power_freq_up = NULL;
-    rte_power_freq_down = NULL;
-    rte_power_freq_max = NULL;
-    rte_power_freq_min = NULL;
-    rte_power_turbo_status = NULL;
-    rte_power_freq_enable_turbo = NULL;
-    rte_power_freq_disable_turbo = NULL;
-    rte_power_get_capabilities = NULL;
+    RTE_VERIFY((ops_index >= PM_ENV_NOT_SET) && (ops_index < PM_ENV_MAX));
+    RTE_VERIFY(rte_power_ops[ops_index].status != 0); +
+    return &rte_power_ops[ops_index];
  }
    int
  rte_power_check_env_supported(enum power_management_env env) {
-    switch (env) {
-    case PM_ENV_ACPI_CPUFREQ:
-        return power_acpi_cpufreq_check_supported(); -    case PM_ENV_PSTATE_CPUFREQ:
-        return power_pstate_cpufreq_check_supported(); -    case PM_ENV_KVM_VM:
-        return power_kvm_vm_check_supported();
-    case PM_ENV_CPPC_CPUFREQ:
-        return power_cppc_cpufreq_check_supported(); -    case PM_ENV_AMD_PSTATE_CPUFREQ:
-        return power_amd_pstate_cpufreq_check_supported(); -    default:
-        rte_errno = EINVAL;
-        return -1;
+    struct rte_power_ops *ops;
+
+    if ((env > PM_ENV_NOT_SET) && (env < PM_ENV_MAX)) { +        ops = rte_power_get_ops(env);
+        return ops->check_env_support();
      }
+
+    rte_errno = EINVAL;
+    return -1;
  }
    int

lib/power/rte_power.c +7 -0
@@ -80,80 +96,26 @@ rte_power_set_env(enum power_management_env env)
       }
         int ret = 0;
+    struct rte_power_ops *ops;
+
+    if ((env == PM_ENV_NOT_SET) || (env >= PM_ENV_MAX)) {
+        POWER_LOG(ERR, "Invalid Power Management Environment(%d)"
+                " set\n", env);
+        ret = -1;
+    }

<...>

Quoted 1 line

+    ops = rte_power_get_ops(env);

To find the target ops from the global list according to the env?

Quoted 8 lines

+    if (ops->status == 0) {
+        POWER_LOG(ERR, WER,
+            "Power Management Environment(%d) not" +            " registered\n", env);
          ret = -1;
      }
        if (ret == 0)
          global_default_env = env;

It is more convenient to use a global variable to point to the default power_cpufreq ops or its list node.

Quoted 111 lines

-    else {
+    else
          global_default_env = PM_ENV_NOT_SET; -        reset_power_function_ptrs();
-    } rte_spinlock_unlock(&global_env_cfg_lock); return ret;

lib/power/rte_power.c +0 -1
@@ -164,7 +126,6 @@ rte_power_unset_env(void)
   {
       rte_spinlock_lock(&global_env_cfg_lock);
       global_default_env = PM_ENV_NOT_SET;
-    reset_power_function_ptrs();
       rte_spinlock_unlock(&global_env_cfg_lock);
   }

  @@ -177,59 +138,76 @@ int
  rte_power_init(unsigned int lcore_id)
  {
      int ret = -1;
+    struct rte_power_ops *ops;
  -    switch (global_default_env) {
-    case PM_ENV_ACPI_CPUFREQ:
-        return power_acpi_cpufreq_init(lcore_id);
-    case PM_ENV_KVM_VM:
-        return power_kvm_vm_init(lcore_id);
-    case PM_ENV_PSTATE_CPUFREQ:
-        return power_pstate_cpufreq_init(lcore_id);
-    case PM_ENV_CPPC_CPUFREQ:
-        return power_cppc_cpufreq_init(lcore_id);
-    case PM_ENV_AMD_PSTATE_CPUFREQ:
-        return power_amd_pstate_cpufreq_init(lcore_id);
-    default:
-        POWER_LOG(INFO, "Env isn't set yet!");
+    if (global_default_env != PM_ENV_NOT_SET) {
+        ops = &rte_power_ops[global_default_env];
+        if (!ops->status) {
+            POWER_LOG(ERR, "Power management env[%d] not"
+                " supported\n", global_default_env);
+            goto out;
+        }
+        return ops->init(lcore_id);
      }
+    POWER_LOG(INFO, POWER, "Env isn't set yet!\n");
        /* Auto detect Environment */
-    POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq power management...");
-    ret = power_acpi_cpufreq_init(lcore_id);
-    if (ret == 0) {
-        rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
-        goto out;
+    POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq"
+            " power management...\n");
+    ops = &rte_power_ops[PM_ENV_ACPI_CPUFREQ];
+    if (ops->status) {
+        ret = ops->init(lcore_id);
+        if (ret == 0) {
+            rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
+            goto out;
+        }
      }
  -    POWER_LOG(INFO, "Attempting to initialise PSTAT power management..."); -    ret = power_pstate_cpufreq_init(lcore_id);
-    if (ret == 0) {
-        rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
-        goto out;
+    POWER_LOG(INFO, "Attempting to initialise PSTAT"
+            " power management...\n");
+    ops = &rte_power_ops[PM_ENV_PSTATE_CPUFREQ];
+    if (ops->status) {
+        ret = ops->init(lcore_id);
+        if (ret == 0) {
+            rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
+            goto out;
+        }
      }
  -    POWER_LOG(INFO, "Attempting to initialise AMD PSTATE power management..."); -    ret = power_amd_pstate_cpufreq_init(lcore_id);
-    if (ret == 0) {
-        rte_power_set_env(PM_ENV_AMD_PSTATE_CPUFREQ);
-        goto out;
+    POWER_LOG(INFO,    "Attempting to initialise AMD PSTATE"
+            " power management...\n");
+    ops = &rte_power_ops[PM_ENV_AMD_PSTATE_CPUFREQ];
+    if (ops->status) {
+        ret = ops->init(lcore_id);
+        if (ret == 0) {
+            rte_power_set_env(PM_ENV_AMD_PSTATE_CPUFREQ);
+            goto out;
+        }
      }
  -    POWER_LOG(INFO, "Attempting to initialise CPPC power management..."); -    ret = power_cppc_cpufreq_init(lcore_id);
-    if (ret == 0) {
-        rte_power_set_env(PM_ENV_CPPC_CPUFREQ);
-        goto out;
+    POWER_LOG(INFO, "Attempting to initialise CPPC power"
+            " management...\n");
+    ops = &rte_power_ops[PM_ENV_CPPC_CPUFREQ];
+    if (ops->status) {
+        ret = ops->init(lcore_id);
+        if (ret == 0) {
+            rte_power_set_env(PM_ENV_CPPC_CPUFREQ);
+            goto out;
+        }
      }
  -    POWER_LOG(INFO, "Attempting to initialise VM power management..."); -    ret = power_kvm_vm_init(lcore_id);
-    if (ret == 0) {
-        rte_power_set_env(PM_ENV_KVM_VM);
-        goto out;
+    POWER_LOG(INFO, "Attempting to initialise VM power"
+            " management...\n");

Quoted 8 lines

+    ops = &rte_power_ops[PM_ENV_KVM_VM];
+    if (ops->status) {
+        ret = ops->init(lcore_id);
+        if (ret == 0) {
+            rte_power_set_env(PM_ENV_KVM_VM);
+            goto out;
+        }
      }

If we use a linked list, above code can be simpled like this:
->
for_each_power_cpufreq_ops(ops, ...) {
    ret = ops->init()
    if (ret) {
        ....
    }
}

Quoted 47 lines

-    POWER_LOG(ERR, "Unable to set Power Management Environment for lcore "
-            "%u", lcore_id);
+    POWER_LOG(ERR, "Unable to set Power Management Environment" +            " for lcore %u\n", lcore_id);
  out:
      return ret;
  }

lib/power/rte_power.c +0 -0
@@ -237,21 +215,14 @@ rte_power_init(unsigned int lcore_id)

  int
  rte_power_exit(unsigned int lcore_id)
  {
-    switch (global_default_env) {
-    case PM_ENV_ACPI_CPUFREQ:
-        return power_acpi_cpufreq_exit(lcore_id);
-    case PM_ENV_KVM_VM:
-        return power_kvm_vm_exit(lcore_id);
-    case PM_ENV_PSTATE_CPUFREQ:
-        return power_pstate_cpufreq_exit(lcore_id);
-    case PM_ENV_CPPC_CPUFREQ:
-        return power_cppc_cpufreq_exit(lcore_id);
-    case PM_ENV_AMD_PSTATE_CPUFREQ:
-        return power_amd_pstate_cpufreq_exit(lcore_id);
-    default:
-        POWER_LOG(ERR, "Environment has not been set, unable to exit gracefully");
+    struct rte_power_ops *ops;
  +    if (global_default_env != PM_ENV_NOT_SET) {
+        ops = &rte_power_ops[global_default_env];
+        return ops->exit(lcore_id);
      }
-    return -1;
+    POWER_LOG(ERR, "Environment has not been set, unable " +            "to exit gracefully\n");
  +    return -1;
  }

lib/power/rte_power.h +1 -0
index 4fa4afe399..749bb823ab 100644
@@ -1,5 +1,6 @@
   /* SPDX-License-Identifier: BSD-3-Clause
    * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 AMD Limited
    */
     #ifndef _RTE_POWER_H

lib/power/rte_power.h extern "C" { · line 25

Quoted 5 lines
lib/power/rte_power.h +1 -1
@@ -21,7 +22,7 @@ extern "C" {
   /* Power Management Environment State */
   enum power_management_env {PM_ENV_NOT_SET, PM_ENV_ACPI_CPUFREQ, PM_ENV_KVM_VM,
           PM_ENV_PSTATE_CPUFREQ, PM_ENV_CPPC_CPUFREQ,
-        PM_ENV_AMD_PSTATE_CPUFREQ};
+        PM_ENV_AMD_PSTATE_CPUFREQ, PM_ENV_MAX};

"enum power_management_env" is not good. may be like "enum power_cpufreq_driver_type"?
In previous linked list structure to be defined, may be directly use a string name instead of a fixed enum is better. Becuase the new "PM_ENV_MAX" will  lead to break ABI when add a new cpufreq driver.

Quoted 63 lines
lib/power/rte_power.h +11 -0
     /**
    * Check if a specific power management environment type is supported on a
@@ -66,6 +67,97 @@ void rte_power_unset_env(void);
    */
   enum power_management_env rte_power_get_env(void);
   +typedef int (*rte_power_cpufreq_init_t)(unsigned int lcore_id);
+typedef int (*rte_power_cpufreq_exit_t)(unsigned int lcore_id);
+typedef int (*rte_power_check_env_support_t)(void);
+
+typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, 
 uint32_t *freqs,
+                    uint32_t num);
+typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);
+typedef int (*rte_power_set_freq_t)(unsigned int lcore_id, uint32_t 
 index);
+typedef int (*rte_power_freq_change_t)(unsigned int lcore_id);
+
+/**
+ * Function pointer definition for generic frequency change 

functions. Review
+ * each environments specific documentation for usage.
+ *
+ * @param lcore_id
+ *  lcore id.
+ *
+ * @return
+ *  - 1 on success with frequency changed.
+ *  - 0 on success without frequency changed.
+ *  - Negative on error.
+ */

lib/power/rte_power.h +22 -0
+
+/**
+ * Power capabilities summary.
+ */
+struct rte_power_core_capabilities {
+    union {
+        uint64_t capabilities;
+        struct {
+            uint64_t turbo:1;       /**< Turbo can be enabled. */
+            uint64_t priority:1;    /**< SST-BF high freq core */
+        };
+    };
+};
+
+typedef int (*rte_power_get_capabilities_t)(unsigned int lcore_id,
+                struct rte_power_core_capabilities *caps);
+
+/** Structure defining core power operations structure */
+struct rte_power_ops {
+uint8_t status;                         /**< ops register status. */
+    enum power_management_env env;          /**< power mgmt env. */
+    rte_power_cpufreq_init_t init;    /**< Initialize power 

management. */
+    rte_power_cpufreq_exit_t exit;    /**< Exit power management. */ +    rte_power_check_env_support_t check_env_support; /**< verify env is supported. */
+    rte_power_freqs_t get_avail_freqs; /**< Get the available frequencies. */
+    rte_power_get_freq_t get_freq; /**< Get frequency index. */ +    rte_power_set_freq_t set_freq; /**< Set frequency index. */ +    rte_power_freq_change_t freq_up;   /**< Scale up frequency. */ +    rte_power_freq_change_t freq_down; /**< Scale down frequency. */ +    rte_power_freq_change_t freq_max;  /**< Scale up frequency to

Quoted 8 lines

highest. */
+    rte_power_freq_change_t freq_min;  /**< Scale up frequency to lowest. */
+    rte_power_freq_change_t turbo_status; /**< Get Turbo status. */ +    rte_power_freq_change_t enable_turbo; /**< Enable Turbo. */ +    rte_power_freq_change_t disable_turbo; /**< Disable Turbo. */ +    rte_power_get_capabilities_t get_caps; /**< power capabilities. */ +} __rte_cache_aligned;

Suggest that fix this sturcture, like:
struct rte_power_cpufreq_list {
    char name[];   // like "cppc_cpufreq", "pstate_cpufreq" struct rte_power_cpufreq *ops;
    struct rte_power_cpufreq_list *node;
}

Quoted 41 lines
lib/power/rte_power.h +33 -0
+
+/**
+ * Register power cpu frequency operations.
+ *
+ * @param ops
+ *   Pointer to an ops structure to register.
+ * @return
+ *   - >=0: Success; return the index of the ops struct in the table.
+ *   - -EINVAL - error while registering ops struct.
+ */
+__rte_internal
+int rte_power_register_ops(const struct rte_power_ops *ops);
+
+/**
+ * Macro to statically register the ops of a cpufreq driver.
+ */
+#define RTE_POWER_REGISTER_OPS(ops)        \
+    (RTE_INIT(power_hdlr_init_##ops)    \
+    {                    \
+        rte_power_register_ops(&ops);    \
+    })
+
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @param ops_index
+ *   The index of the ops struct in the ops struct table.
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_ops *
+rte_power_get_ops(int ops_index);
+

  /**
   * Initialize power management for a specific lcore. If rte_power_set_env() has
   * not been called then an auto-detect of the environment will start and

lib/power/rte_power.h +0 -1
@@ -108,10 +200,14 @@ int rte_power_exit(unsigned int lcore_id);
    * @return
    *  The number of available frequencies.
    */
-typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, 

lib/power/rte_power.h int rte_power_exit(unsigned int lcore_id); · line 211

Quoted 10 lines
lib/power/rte_power.h +7 -1
 uint32_t *freqs,
-        uint32_t num);
+static inline uint32_t
+rte_power_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t n)
+{
+    struct rte_power_ops *ops;
   -extern rte_power_freqs_t rte_power_freqs;
+    ops = rte_power_get_ops(rte_power_get_env());
+    return ops->get_avail_freqs(lcore_id, freqs, n);
+}

nice.
<...>

#7 lore raw

RE: [RFC PATCH 1/2] power: refactor core power management library

In reply to lihuisong (C)

[AMD Official Use Only - General]

Hi Lihuisong,

Quoted 60 lines

-----Original Message-----
From: lihuisong (C) <[email protected]>
Sent: Friday, March 1, 2024 8:27 AM
To: Tummala, Sivaprasad <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Cc: [email protected]
Subject: Re: [RFC PATCH 1/2] power: refactor core power management library

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 7 lines · Sivaprasad Tummala

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

Quoted 6 lines

These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

Good job. +1 to refacotor.

<...>

Quoted 27 lines
drivers/meson.build index renamed from drivers/meson.build +0 -0

f2be71bc05..e293c3945f 100644

drivers/meson.build +1 -0
@@ -28,6 +28,7 @@ subdirs = [
          'event',          # depends on common, bus, mempool and net.
          'baseband',       # depends on common and bus.
          'gpu',            # depends on common and bus.
+        'power',          # depends on common (in future).
  ]
 
  if meson.is_cross_build()
drivers/power/core/acpi/meson.build new file +8 -0
new file mode 100644
index 0000000000..d10ec8ee94
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2024 AMD
+Limited
+
+sources = files('power_acpi_cpufreq.c')
+
+headers = files('power_acpi_cpufreq.h')
+
+deps += ['power']
Quoted 8 lines
Quoted 5 lines
drivers/power/core/acpi/power_acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c rename to +0 -0
similarity index 95%
rename from lib/power/power_acpi_cpufreq.c rename to

drivers/power/core/acpi/power_acpi_cpufreq.c

This file is in power lib.
How about remove the 'power' prefix of this file name? like acpi_cpufreq.c, cppc_cpufreq.c.

ACK

Quoted 6 lines
Quoted 6 lines

index f8d978d03d..69d80ad2ae 100644

drivers/power/core/acpi/power_acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +0 -0
@@ -577,3 +577,22 @@ int power_acpi_get_capabilities(unsigned int
 lcore_id,
 
Quoted 7 lines

drivers/power/core/acpi/power_acpi_cpufreq.c int power_acpi_get_capabilities(unsigned int lcore_id, · line 581

Quoted 4 lines
drivers/power/core/acpi/power_acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +2 -0
      return 0;
  }
+
+static struct rte_power_ops acpi_ops = {

How about use the following structure name?
"struct rte_power_cpufreq_ops" or "struct rte_power_core_ops" After all, we also have other power ops, like uncore, right?

Agreed.

Quoted 342 lines

drivers/power/core/kvm-vm/power_kvm_vm.c int power_kvm_vm_get_capabilities(__rte_unused

Quoted 169 lines
drivers/power/core/acpi/power_acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +16 -0
+     .init = power_acpi_cpufreq_init,
+     .exit = power_acpi_cpufreq_exit,
+     .check_env_support = power_acpi_cpufreq_check_supported,
+     .get_avail_freqs = power_acpi_cpufreq_freqs,
+     .get_freq = power_acpi_cpufreq_get_freq,
+     .set_freq = power_acpi_cpufreq_set_freq,
+     .freq_down = power_acpi_cpufreq_freq_down,
+     .freq_up = power_acpi_cpufreq_freq_up,
+     .freq_max = power_acpi_cpufreq_freq_max,
+     .freq_min = power_acpi_cpufreq_freq_min,
+     .turbo_status = power_acpi_turbo_status,
+     .enable_turbo = power_acpi_enable_turbo,
+     .disable_turbo = power_acpi_disable_turbo,
+     .get_caps = power_acpi_get_capabilities };
+
+RTE_POWER_REGISTER_OPS(acpi_ops);
drivers/power/core/acpi/power_acpi_cpufreq.h renamed from lib/power/power_acpi_cpufreq.h rename to +0 -0
similarity index 100%
rename from lib/power/power_acpi_cpufreq.h rename to

drivers/power/core/acpi/power_acpi_cpufreq.h

drivers/power/core/amd-pstate/meson.build new file +8 -0
new file mode 100644
index 0000000000..8ec4c960f5
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2024 AMD
+Limited
+
+sources = files('power_amd_pstate_cpufreq.c')
+
+headers = files('power_amd_pstate_cpufreq.h')
+
+deps += ['power']
drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.c renamed from lib/power/power_amd_pstate_cpufreq.c +18 -0
similarity index 95%
rename from lib/power/power_amd_pstate_cpufreq.c
rename to drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.c
index 028f84416b..9938de72a6 100644
@@ -700,3 +700,22 @@ power_amd_pstate_get_capabilities(unsigned int
 lcore_id,
 
      return 0;
  }
+
+static struct rte_power_ops amd_pstate_ops = {
+     .init = power_amd_pstate_cpufreq_init,
+     .exit = power_amd_pstate_cpufreq_exit,
+     .check_env_support = power_amd_pstate_cpufreq_check_supported,
+     .get_avail_freqs = power_amd_pstate_cpufreq_freqs,
+     .get_freq = power_amd_pstate_cpufreq_get_freq,
+     .set_freq = power_amd_pstate_cpufreq_set_freq,
+     .freq_down = power_amd_pstate_cpufreq_freq_down,
+     .freq_up = power_amd_pstate_cpufreq_freq_up,
+     .freq_max = power_amd_pstate_cpufreq_freq_max,
+     .freq_min = power_amd_pstate_cpufreq_freq_min,
+     .turbo_status = power_amd_pstate_turbo_status,
+     .enable_turbo = power_amd_pstate_enable_turbo,
+     .disable_turbo = power_amd_pstate_disable_turbo,
+     .get_caps = power_amd_pstate_get_capabilities };
+
+RTE_POWER_REGISTER_OPS(amd_pstate_ops);
drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.h renamed from lib/power/power_amd_pstate_cpufreq.h +0 -0
similarity index 100%
rename from lib/power/power_amd_pstate_cpufreq.h
rename to drivers/power/core/amd-pstate/power_amd_pstate_cpufreq.h
drivers/power/core/cppc/meson.build new file +8 -0
new file mode 100644
index 0000000000..06f3b99bb8
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2024 AMD
+Limited
+
+sources = files('power_cppc_cpufreq.c')
+
+headers = files('power_cppc_cpufreq.h')
+
+deps += ['power']
drivers/power/core/cppc/power_cppc_cpufreq.c renamed from lib/power/power_cppc_cpufreq.c rename to +0 -0
similarity index 96%
rename from lib/power/power_cppc_cpufreq.c rename to

drivers/power/core/cppc/power_cppc_cpufreq.c
index 3ddf39bd76..605f633309 100644

drivers/power/core/cppc/power_cppc_cpufreq.c renamed from lib/power/power_cppc_cpufreq.c +18 -0
@@ -685,3 +685,22 @@ power_cppc_get_capabilities(unsigned int
 lcore_id,
 
      return 0;
  }
+
+static struct rte_power_ops cppc_ops = {
+     .init = power_cppc_cpufreq_init,
+     .exit = power_cppc_cpufreq_exit,
+     .check_env_support = power_cppc_cpufreq_check_supported,
+     .get_avail_freqs = power_cppc_cpufreq_freqs,
+     .get_freq = power_cppc_cpufreq_get_freq,
+     .set_freq = power_cppc_cpufreq_set_freq,
+     .freq_down = power_cppc_cpufreq_freq_down,
+     .freq_up = power_cppc_cpufreq_freq_up,
+     .freq_max = power_cppc_cpufreq_freq_max,
+     .freq_min = power_cppc_cpufreq_freq_min,
+     .turbo_status = power_cppc_turbo_status,
+     .enable_turbo = power_cppc_enable_turbo,
+     .disable_turbo = power_cppc_disable_turbo,
+     .get_caps = power_cppc_get_capabilities };
+
+RTE_POWER_REGISTER_OPS(cppc_ops);
drivers/power/core/cppc/power_cppc_cpufreq.h renamed from lib/power/power_cppc_cpufreq.h rename to +0 -0
similarity index 100%
rename from lib/power/power_cppc_cpufreq.h rename to

drivers/power/core/cppc/power_cppc_cpufreq.h

drivers/power/core/kvm-vm/guest_channel.c renamed from lib/power/guest_channel.c +0 -0
similarity index 100%
rename from lib/power/guest_channel.c
rename to drivers/power/core/kvm-vm/guest_channel.c
drivers/power/core/kvm-vm/guest_channel.h renamed from lib/power/guest_channel.h +0 -0
similarity index 100%
rename from lib/power/guest_channel.h
rename to drivers/power/core/kvm-vm/guest_channel.h
drivers/power/core/kvm-vm/meson.build new file +20 -0
new file mode 100644
index 0000000000..3150c6674b
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(C) 2024 AMD
+Limited.
+#
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+sources = files(
+        'guest_channel.c',
+        'power_kvm_vm.c',
+)
+
+headers = files(
+        'guest_channel.h',
+        'power_kvm_vm.h',
+)
+deps += ['power']
drivers/power/core/kvm-vm/power_kvm_vm.c renamed from lib/power/power_kvm_vm.c +0 -0
similarity index 83%
rename from lib/power/power_kvm_vm.c
rename to drivers/power/core/kvm-vm/power_kvm_vm.c
index f15be8fac5..a5d6984d26 100644
@@ -137,3 +137,22 @@ int power_kvm_vm_get_capabilities(__rte_unused

unsigned int lcore_id,

drivers/power/core/kvm-vm/power_kvm_vm.c int power_kvm_vm_get_capabilities(__rte_unused · line 137

Quoted 1 line
drivers/power/core/kvm-vm/power_kvm_vm.c renamed from lib/power/power_kvm_vm.c +0 -0
      POWER_LOG(ERR, "rte_power_get_capabilities is not implemented for Virtual

Machine Power Management");

Quoted 170 lines
drivers/power/core/kvm-vm/power_kvm_vm.c renamed from lib/power/power_kvm_vm.c +18 -0
      return -ENOTSUP;
  }
+
+static struct rte_power_ops kvm_vm_ops = {
+     .init = power_kvm_vm_init,
+     .exit = power_kvm_vm_exit,
+     .check_env_support = power_kvm_vm_check_supported,
+     .get_avail_freqs = power_kvm_vm_freqs,
+     .get_freq = power_kvm_vm_get_freq,
+     .set_freq = power_kvm_vm_set_freq,
+     .freq_down = power_kvm_vm_freq_down,
+     .freq_up = power_kvm_vm_freq_up,
+     .freq_max = power_kvm_vm_freq_max,
+     .freq_min = power_kvm_vm_freq_min,
+     .turbo_status = power_kvm_vm_turbo_status,
+     .enable_turbo = power_kvm_vm_enable_turbo,
+     .disable_turbo = power_kvm_vm_disable_turbo,
+     .get_caps = power_kvm_vm_get_capabilities };
+
+RTE_POWER_REGISTER_OPS(kvm_vm_ops);
drivers/power/core/kvm-vm/power_kvm_vm.h renamed from lib/power/power_kvm_vm.h +0 -0
similarity index 100%
rename from lib/power/power_kvm_vm.h
rename to drivers/power/core/kvm-vm/power_kvm_vm.h
(quoted diff) +0 -0

b/drivers/power/core/meson.build new file mode 100644 index 0000000000..4081dafaa0

drivers/power/core/meson.build new file +12 -0
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2024 AMD
+Limited
+
+drivers = [
+        'acpi',
+        'amd-pstate',
+        'cppc',
+        'kvm-vm',
+        'pstate'
+]
+
+std_deps = ['power']
drivers/power/core/pstate/meson.build new file +8 -0
new file mode 100644
index 0000000000..1025c64e48
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2024 AMD
+Limited
+
+sources = files('power_pstate_cpufreq.c')
+
+headers = files('power_pstate_cpufreq.h')
+
+deps += ['power']
drivers/power/core/pstate/power_pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c rename to +0 -0
similarity index 96%
rename from lib/power/power_pstate_cpufreq.c rename to

drivers/power/core/pstate/power_pstate_cpufreq.c
index 73138dc4e4..d4c3645ff8 100644

drivers/power/core/pstate/power_pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c +18 -0
@@ -888,3 +888,22 @@ int power_pstate_get_capabilities(unsigned int
 lcore_id,
 
      return 0;
  }
+
+static struct rte_power_ops pstate_ops = {
+     .init = power_pstate_cpufreq_init,
+     .exit = power_pstate_cpufreq_exit,
+     .check_env_support = power_pstate_cpufreq_check_supported,
+     .get_avail_freqs = power_pstate_cpufreq_freqs,
+     .get_freq = power_pstate_cpufreq_get_freq,
+     .set_freq = power_pstate_cpufreq_set_freq,
+     .freq_down = power_pstate_cpufreq_freq_down,
+     .freq_up = power_pstate_cpufreq_freq_up,
+     .freq_max = power_pstate_cpufreq_freq_max,
+     .freq_min = power_pstate_cpufreq_freq_min,
+     .turbo_status = power_pstate_turbo_status,
+     .enable_turbo = power_pstate_enable_turbo,
+     .disable_turbo = power_pstate_disable_turbo,
+     .get_caps = power_pstate_get_capabilities };
+
+RTE_POWER_REGISTER_OPS(pstate_ops);
drivers/power/core/pstate/power_pstate_cpufreq.h renamed from lib/power/power_pstate_cpufreq.h rename to +0 -0
similarity index 100%
rename from lib/power/power_pstate_cpufreq.h rename to

drivers/power/core/pstate/power_pstate_cpufreq.h

drivers/power/meson.build new renamed from drivers/power/meson.build +0 -0

file mode 100644 index 0000000000..7d9034c7ac

drivers/power/meson.build new file +8 -0
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2024 AMD
+Limited
+
+drivers = [
+        'core',
+]
+
+std_deps = ['power']
lib/power/meson.build index renamed from lib/power/meson.build +0 -0

b8426589b2..207d96d877 100644

lib/power/meson.build +0 -6
@@ -12,14 +12,8 @@ if not is_linux
      reason = 'only supported on Linux'
  endif
  sources = files(
-        'guest_channel.c',
-        'power_acpi_cpufreq.c',
-        'power_amd_pstate_cpufreq.c',
          'power_common.c',
-        'power_cppc_cpufreq.c',
-        'power_kvm_vm.c',
          'power_intel_uncore.c',
-        'power_pstate_cpufreq.c',
          'rte_power.c',
          'rte_power_uncore.c',
          'rte_power_pmd_mgmt.c',
lib/power/power_common.h index renamed from lib/power/power_common.h +0 -0

30966400ba..c90b611f4f 100644

lib/power/power_common.h +11 -0
@@ -23,13 +23,24 @@ extern int power_logtype;
  #endif
 
  /* check if scaling driver matches one we want */
+__rte_internal
  int cpufreq_check_scaling_driver(const char *driver);
+
+__rte_internal
  int power_set_governor(unsigned int lcore_id, const char *new_governor,
              char *orig_governor, size_t orig_governor_len);
+
+__rte_internal
  int open_core_sysfs_file(FILE **f, const char *mode, const char *format, ...)
              __rte_format_printf(3, 4);
+
+__rte_internal
  int read_core_sysfs_u32(FILE *f, uint32_t *val);
+
+__rte_internal
  int read_core_sysfs_s(FILE *f, char *buf, unsigned int len);
+
+__rte_internal
  int write_core_sysfs_s(FILE *f, const char *str);
 
  #endif /* _POWER_COMMON_H_ */
lib/power/rte_power.c index renamed from lib/power/rte_power.c +0 -0

36c3f3da98..70176807f4 100644

lib/power/rte_power.c +0 -5
@@ -8,64 +8,80 @@
  #include <rte_spinlock.h>
 
  #include "rte_power.h"
-#include "power_acpi_cpufreq.h"
-#include "power_cppc_cpufreq.h"
  #include "power_common.h"
-#include "power_kvm_vm.h"
-#include "power_pstate_cpufreq.h"
-#include "power_amd_pstate_cpufreq.h"
 
Quoted 2 lines

lib/power/rte_power.c line 13

Quoted 1 line
lib/power/rte_power.c +0 -0
  enum power_management_env global_default_env = PM_ENV_NOT_SET;

use a pointer to save the current power cpufreq ops?

ACK

Quoted 66 lines
Quoted 20 lines
lib/power/rte_power.c +1 -2
  static rte_spinlock_t global_env_cfg_lock =
 RTE_SPINLOCK_INITIALIZER;
+static struct rte_power_ops rte_power_ops[PM_ENV_MAX];
 
-/* function pointers */
-rte_power_freqs_t rte_power_freqs  = NULL; -rte_power_get_freq_t

rte_power_get_freq = NULL; -rte_power_set_freq_t rte_power_set_freq = NULL; -rte_power_freq_change_t rte_power_freq_up = NULL;

lib/power/rte_power.c +1 -10
-rte_power_freq_change_t rte_power_freq_down = NULL;
-rte_power_freq_change_t rte_power_freq_max = NULL;
-rte_power_freq_change_t rte_power_freq_min = NULL;
-rte_power_freq_change_t rte_power_turbo_status;
-rte_power_freq_change_t rte_power_freq_enable_turbo;
-rte_power_freq_change_t rte_power_freq_disable_turbo;
-rte_power_get_capabilities_t rte_power_get_capabilities;
-
-static void
-reset_power_function_ptrs(void)
+/* register the ops struct in rte_power_ops, return 0 on success. */

lib/power/rte_power.c

Quoted 10 lines
lib/power/rte_power.c +10 -0
+int rte_power_register_ops(const struct rte_power_ops *op) {
+     struct rte_power_ops *ops;
+
+     if (op->env >= PM_ENV_MAX) {
+             POWER_LOG(ERR, "Unsupported power management environment\n");
+             return -EINVAL;
+     }
+
+     if (op->status != 0) {
+             POWER_LOG(ERR, "Power management env[%d] ops registered

already\n",

Quoted 4 lines
lib/power/rte_power.c +4 -0
+                     op->env);
+             return -EINVAL;
+     }
+

lib/power/rte_power.c line 44

Quoted 10 lines
lib/power/rte_power.c +10 -0
+     if (!op->init || !op->exit || !op->check_env_support ||
+             !op->get_avail_freqs || !op->get_freq || !op->set_freq ||
+             !op->freq_up || !op->freq_down || !op->freq_max ||
+             !op->freq_min || !op->turbo_status || !op->enable_turbo ||
+             !op->disable_turbo || !op->get_caps) {
+             POWER_LOG(ERR, "Missing callbacks while registering power ops\n");
+             return -EINVAL;
+     }
+
+     ops = &rte_power_ops[op->env];

It is better to use a global linked list instead of an array.
And we should extract a list structure including this ops structure and this ops's owner.

Quoted 4 lines
lib/power/rte_power.c +4 -0
+     ops->env = op->env;
+     ops->init = op->init;
+     ops->exit = op->exit;
+     ops->check_env_support = op->check_env_support;

lib/power/rte_power.c line 58

Quoted 10 lines
lib/power/rte_power.c +10 -0
+     ops->get_avail_freqs = op->get_avail_freqs;
+     ops->get_freq = op->get_freq;
+     ops->set_freq = op->set_freq;
+     ops->freq_up = op->freq_up;
+     ops->freq_down = op->freq_down;
+     ops->freq_max = op->freq_max;
+     ops->freq_min = op->freq_min;
+     ops->turbo_status = op->turbo_status;
+     ops->enable_turbo = op->enable_turbo;
+     ops->disable_turbo = op->disable_turbo;

*ops = *op?

Quoted 1 line

+ ops->status = 1; /* registered */

status --> registered?
But if use ops linked list, this flag also can be removed.

Quoted 11 lines

lib/power/rte_power.c line 65

Quoted 6 lines
lib/power/rte_power.c +6 -0
+
+     return 0;
+}
+
+struct rte_power_ops *
+rte_power_get_ops(int ops_index)

AFAICS, there is only one cpufreq driver on one platform and just have one power_cpufreq_ops to use for user.
We don't need user to get other power ops, and user just want to know the power ops using currently, right?
So using 'index' toget this ops is not good.

Agreed! I will rework this to make it global.

Quoted 71 lines

lib/power/rte_power.c

Quoted 13 lines
  {
lib/power/rte_power.c +1 -11
-     rte_power_freqs  = NULL;
-     rte_power_get_freq = NULL;
-     rte_power_set_freq = NULL;
-     rte_power_freq_up = NULL;
-     rte_power_freq_down = NULL;
-     rte_power_freq_max = NULL;
-     rte_power_freq_min = NULL;
-     rte_power_turbo_status = NULL;
-     rte_power_freq_enable_turbo = NULL;
-     rte_power_freq_disable_turbo = NULL;
-     rte_power_get_capabilities = NULL;
+     RTE_VERIFY((ops_index >= PM_ENV_NOT_SET) && (ops_index <

PM_ENV_MAX));

lib/power/rte_power.c rte_power_set_env(enum power_management_env

Quoted 35 lines
lib/power/rte_power.c +11 -14
+     RTE_VERIFY(rte_power_ops[ops_index].status != 0);
+
+     return &rte_power_ops[ops_index];
  }
 
  int
  rte_power_check_env_supported(enum power_management_env env)
  {
-     switch (env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_check_supported();
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_check_supported();
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_check_supported();
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_check_supported();
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_check_supported();
-     default:
-             rte_errno = EINVAL;
-             return -1;
+     struct rte_power_ops *ops;
+
+     if ((env > PM_ENV_NOT_SET) && (env < PM_ENV_MAX)) {
+             ops = rte_power_get_ops(env);
+             return ops->check_env_support();
      }
+
+     rte_errno = EINVAL;
+     return -1;
  }
 
  int
@@ -80,80 +96,26 @@ rte_power_set_env(enum power_management_env

env)

Quoted 11 lines
lib/power/rte_power.c +7 -0
      }
 
      int ret = 0;
+     struct rte_power_ops *ops;
+
+     if ((env == PM_ENV_NOT_SET) || (env >= PM_ENV_MAX)) {
+             POWER_LOG(ERR, "Invalid Power Management Environment(%d)"
+                             " set\n", env);
+             ret = -1;
+     }
 

<...>

Quoted 1 line

+ ops = rte_power_get_ops(env);

To find the target ops from the global list according to the env?

Quoted 7 lines
lib/power/rte_power.c +4 -0
+     if (ops->status == 0) {
+             POWER_LOG(ERR, WER,
+                     "Power Management Environment(%d) not"
+                     " registered\n", env);
              ret = -1;
      }
Quoted 4 lines
Quoted 2 lines
      if (ret == 0)
              global_default_env = env;

It is more convenient to use a global variable to point to the default power_cpufreq ops or its list node.

Agreed

Quoted 128 lines
Quoted 38 lines
lib/power/rte_power.c +5 -17
-     else {
+     else
              global_default_env = PM_ENV_NOT_SET;
-             reset_power_function_ptrs();
-     }
 
      rte_spinlock_unlock(&global_env_cfg_lock);
      return ret;
@@ -164,7 +126,6 @@ rte_power_unset_env(void)
  {
      rte_spinlock_lock(&global_env_cfg_lock);
      global_default_env = PM_ENV_NOT_SET;
-     reset_power_function_ptrs();
      rte_spinlock_unlock(&global_env_cfg_lock);
  }
 
@@ -177,59 +138,76 @@ int
  rte_power_init(unsigned int lcore_id)
  {
      int ret = -1;
+     struct rte_power_ops *ops;
 
-     switch (global_default_env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_init(lcore_id);
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_init(lcore_id);
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_init(lcore_id);
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_init(lcore_id);
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_init(lcore_id);
-     default:
-             POWER_LOG(INFO, "Env isn't set yet!");
+     if (global_default_env != PM_ENV_NOT_SET) {
+             ops = &rte_power_ops[global_default_env];
+             if (!ops->status) {

lib/power/rte_power.c int · line 197

Quoted 10 lines
lib/power/rte_power.c +6 -1
+                     POWER_LOG(ERR, "Power management env[%d] not"
+                             " supported\n", global_default_env);
+                     goto out;
+             }
+             return ops->init(lcore_id);
      }
+     POWER_LOG(INFO, POWER, "Env isn't set yet!\n");
 
      /* Auto detect Environment */
-     POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq power

management...");

Quoted 22 lines
lib/power/rte_power.c +11 -9
-     ret = power_acpi_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
-             goto out;
+     POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq"
+                     " power management...\n");
+     ops = &rte_power_ops[PM_ENV_ACPI_CPUFREQ];
+     if (ops->status) {
+             ret = ops->init(lcore_id);
+             if (ret == 0) {
+                     rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
+                     goto out;
+             }
      }
 
-     POWER_LOG(INFO, "Attempting to initialise PSTAT power management...");
-     ret = power_pstate_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
-             goto out;
+     POWER_LOG(INFO, "Attempting to initialise PSTAT"
+                     " power management...\n");

lib/power/rte_power.c int

Quoted 10 lines
lib/power/rte_power.c +7 -1
+     ops = &rte_power_ops[PM_ENV_PSTATE_CPUFREQ];
+     if (ops->status) {
+             ret = ops->init(lcore_id);
+             if (ret == 0) {
+                     rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
+                     goto out;
+             }
      }
 
-     POWER_LOG(INFO, "Attempting to initialise AMD PSTATE power

management...");

Quoted 36 lines
lib/power/rte_power.c +18 -14
-     ret = power_amd_pstate_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_AMD_PSTATE_CPUFREQ);
-             goto out;
+     POWER_LOG(INFO, "Attempting to initialise AMD PSTATE"
+                     " power management...\n");
+     ops = &rte_power_ops[PM_ENV_AMD_PSTATE_CPUFREQ];
+     if (ops->status) {
+             ret = ops->init(lcore_id);
+             if (ret == 0) {
+                     rte_power_set_env(PM_ENV_AMD_PSTATE_CPUFREQ);
+                     goto out;
+             }
      }
 
-     POWER_LOG(INFO, "Attempting to initialise CPPC power management...");
-     ret = power_cppc_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_CPPC_CPUFREQ);
-             goto out;
+     POWER_LOG(INFO, "Attempting to initialise CPPC power"
+                     " management...\n");
+     ops = &rte_power_ops[PM_ENV_CPPC_CPUFREQ];
+     if (ops->status) {
+             ret = ops->init(lcore_id);
+             if (ret == 0) {
+                     rte_power_set_env(PM_ENV_CPPC_CPUFREQ);
+                     goto out;
+             }
      }
 
-     POWER_LOG(INFO, "Attempting to initialise VM power management...");
-     ret = power_kvm_vm_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_KVM_VM);
-             goto out;

lib/power/rte_power.c int · line 208

Quoted 10 lines
lib/power/rte_power.c +9 -0
+     POWER_LOG(INFO, "Attempting to initialise VM power"
+                     " management...\n");
+     ops = &rte_power_ops[PM_ENV_KVM_VM];
+     if (ops->status) {
+             ret = ops->init(lcore_id);
+             if (ret == 0) {
+                     rte_power_set_env(PM_ENV_KVM_VM);
+                     goto out;
+             }
      }
Quoted 8 lines

If we use a linked list, above code can be simpled like this:
->

for_each_power_cpufreq_ops(ops, ...) {
     ret = ops->init()
     if (ret) {
         ....
     }
}

ACK

Quoted 55 lines
Quoted 14 lines
lib/power/rte_power.c +2 -5
-     POWER_LOG(ERR, "Unable to set Power Management Environment for lcore "
-                     "%u", lcore_id);
+     POWER_LOG(ERR, "Unable to set Power Management Environment"
+                     " for lcore %u\n", lcore_id);
  out:
      return ret;
  }
@@ -237,21 +215,14 @@ rte_power_init(unsigned int lcore_id)
  int
  rte_power_exit(unsigned int lcore_id)
  {
-     switch (global_default_env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_exit(lcore_id);

lib/power/rte_power.c rte_power_init(unsigned int lcore_id) · line 252

Quoted 10 lines
lib/power/rte_power.c +0 -10
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_exit(lcore_id);
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_exit(lcore_id);
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_exit(lcore_id);
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_exit(lcore_id);
-     default:
-             POWER_LOG(ERR, "Environment has not been set, unable to exit

gracefully");

Quoted 24 lines
lib/power/rte_power.c +7 -1
+     struct rte_power_ops *ops;
 
+     if (global_default_env != PM_ENV_NOT_SET) {
+             ops = &rte_power_ops[global_default_env];
+             return ops->exit(lcore_id);
      }
-     return -1;
+     POWER_LOG(ERR, "Environment has not been set, unable "
+                     "to exit gracefully\n");
 
+     return -1;
  }
lib/power/rte_power.h index renamed from lib/power/rte_power.h +0 -0

4fa4afe399..749bb823ab 100644

lib/power/rte_power.h +1 -0
@@ -1,5 +1,6 @@
  /* SPDX-License-Identifier: BSD-3-Clause
   * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 AMD Limited
   */
 
  #ifndef _RTE_POWER_H

lib/power/rte_power.h extern "C" { · line 23

Quoted 2 lines
lib/power/rte_power.h +0 -0
@@ -21,7 +22,7 @@ extern "C" {
  /* Power Management Environment State */
  enum power_management_env {PM_ENV_NOT_SET, PM_ENV_ACPI_CPUFREQ,

PM_ENV_KVM_VM,

lib/power/rte_power.h extern "C" { · line 25

Quoted 3 lines
lib/power/rte_power.h +1 -1
              PM_ENV_PSTATE_CPUFREQ, PM_ENV_CPPC_CPUFREQ,
-             PM_ENV_AMD_PSTATE_CPUFREQ};
+             PM_ENV_AMD_PSTATE_CPUFREQ, PM_ENV_MAX};
Quoted 6 lines

"enum power_management_env" is not good. may be like "enum power_cpufreq_driver_type"?
In previous linked list structure to be defined, may be directly use a string name instead of a fixed enum is better.
Becuase the new "PM_ENV_MAX" will lead to break ABI when add a new cpufreq driver.

I will rework this to remove the max macro.
How changing the enum power_management_env requires ABI versioning. Will consider this change in future.

Quoted 69 lines
Quoted 45 lines
lib/power/rte_power.h +0 -0
  /**
   * Check if a specific power management environment type is
 supported on a @@ -66,6 +67,97 @@ void rte_power_unset_env(void);
   */
  enum power_management_env rte_power_get_env(void);
lib/power/rte_power.h +38 -0
+typedef int (*rte_power_cpufreq_init_t)(unsigned int lcore_id);
+typedef int (*rte_power_cpufreq_exit_t)(unsigned int lcore_id);
+typedef int (*rte_power_check_env_support_t)(void);
+
+typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
+                                     uint32_t num); typedef uint32_t
+(*rte_power_get_freq_t)(unsigned int lcore_id); typedef int
+(*rte_power_set_freq_t)(unsigned int lcore_id, uint32_t index);
+typedef int (*rte_power_freq_change_t)(unsigned int lcore_id);
+
+/**
+ * Function pointer definition for generic frequency change
+functions. Review
+ * each environments specific documentation for usage.
+ *
+ * @param lcore_id
+ *  lcore id.
+ *
+ * @return
+ *  - 1 on success with frequency changed.
+ *  - 0 on success without frequency changed.
+ *  - Negative on error.
+ */
+
+/**
+ * Power capabilities summary.
+ */
+struct rte_power_core_capabilities {
+     union {
+             uint64_t capabilities;
+             struct {
+                     uint64_t turbo:1;       /**< Turbo can be enabled. */
+                     uint64_t priority:1;    /**< SST-BF high freq core */
+             };
+     };
+};
+
+typedef int (*rte_power_get_capabilities_t)(unsigned int lcore_id,

lib/power/rte_power.h void rte_power_unset_env(void);

Quoted 10 lines
lib/power/rte_power.h +10 -0
+                             struct rte_power_core_capabilities
+*caps);
+
+/** Structure defining core power operations structure */ struct
+rte_power_ops {
+uint8_t status;                         /**< ops register status. */
+     enum power_management_env env;          /**< power mgmt env. */
+     rte_power_cpufreq_init_t init;    /**< Initialize power management. */
+     rte_power_cpufreq_exit_t exit;    /**< Exit power management. */
+     rte_power_check_env_support_t check_env_support; /**< verify env is

supported. */

lib/power/rte_power.h void rte_power_unset_env(void);

Quoted 6 lines
lib/power/rte_power.h +6 -0
+     rte_power_freqs_t get_avail_freqs; /**< Get the available frequencies. */
+     rte_power_get_freq_t get_freq; /**< Get frequency index. */
+     rte_power_set_freq_t set_freq; /**< Set frequency index. */
+     rte_power_freq_change_t freq_up;   /**< Scale up frequency. */
+     rte_power_freq_change_t freq_down; /**< Scale down frequency. */
+     rte_power_freq_change_t freq_max;  /**< Scale up frequency to highest.

*/

lib/power/rte_power.h void rte_power_unset_env(void);

Quoted 6 lines
lib/power/rte_power.h +6 -0
+     rte_power_freq_change_t freq_min;  /**< Scale up frequency to lowest. */
+     rte_power_freq_change_t turbo_status; /**< Get Turbo status. */
+     rte_power_freq_change_t enable_turbo; /**< Enable Turbo. */
+     rte_power_freq_change_t disable_turbo; /**< Disable Turbo. */
+     rte_power_get_capabilities_t get_caps; /**< power capabilities.
+*/ } __rte_cache_aligned;
Quoted 5 lines

Suggest that fix this sturcture, like:

struct rte_power_cpufreq_list {
     char name[];   // like "cppc_cpufreq", "pstate_cpufreq"
     struct rte_power_cpufreq *ops;
     struct rte_power_cpufreq_list *node; }

ACK

Quoted 51 lines
Quoted 40 lines
lib/power/rte_power.h +33 -0
+
+/**
+ * Register power cpu frequency operations.
+ *
+ * @param ops
+ *   Pointer to an ops structure to register.
+ * @return
+ *   - >=0: Success; return the index of the ops struct in the table.
+ *   - -EINVAL - error while registering ops struct.
+ */
+__rte_internal
+int rte_power_register_ops(const struct rte_power_ops *ops);
+
+/**
+ * Macro to statically register the ops of a cpufreq driver.
+ */
+#define RTE_POWER_REGISTER_OPS(ops)          \
+     (RTE_INIT(power_hdlr_init_##ops)        \
+     {                                       \
+             rte_power_register_ops(&ops);   \
+     })
+
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @param ops_index
+ *   The index of the ops struct in the ops struct table.
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_ops *
+rte_power_get_ops(int ops_index);
+
  /**
   * Initialize power management for a specific lcore. If rte_power_set_env() has
   * not been called then an auto-detect of the environment will start

and @@ -108,10 +200,14 @@ int rte_power_exit(unsigned int lcore_id);
* @return
* The number of available frequencies.
*/

lib/power/rte_power.h int rte_power_exit(unsigned int lcore_id);

Quoted 9 lines
lib/power/rte_power.h +5 -3
-typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
-             uint32_t num);
+static inline uint32_t
+rte_power_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t n) {
+     struct rte_power_ops *ops;
 
-extern rte_power_freqs_t rte_power_freqs;
+     ops = rte_power_get_ops(rte_power_get_env());
+     return ops->get_avail_freqs(lcore_id, freqs, n); }

nice.
<...>

#8 lore raw

cover power: refactor power management library

In reply to Sivaprasad Tummala

This patchset refactors the power management library, addressing both core and uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/core/*' and 'drivers/power/uncore/*'.

This refactor significantly improves code organization, enhances clarity, and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

Furthermore, this effort aims to streamline code maintenance by consolidating common functions for cpufreq and cppc across various core drivers, thus reducing code duplication.

Please note that this RFC patch is currently in its initial phase and is primarily intended for soliciting feedback and comments. As of now, it has not undergone testing for build or functional issues.

---
power: refactor core power management library
power: refactor uncore power management library

 drivers/meson.build                           |   1 +
 drivers/power/core/acpi/meson.build           |   8 +
 .../power/core/acpi}/power_acpi_cpufreq.c     |  19 ++
 .../power/core/acpi}/power_acpi_cpufreq.h     |   0
 drivers/power/core/amd-pstate/meson.build     |   8 +
 .../amd-pstate}/power_amd_pstate_cpufreq.c    |  19 ++
 .../amd-pstate}/power_amd_pstate_cpufreq.h    |   0
 drivers/power/core/cppc/meson.build           |   8 +
 .../power/core/cppc}/power_cppc_cpufreq.c     |  19 ++
 .../power/core/cppc}/power_cppc_cpufreq.h     |   0
 .../power/core/kvm-vm}/guest_channel.c        |   0
 .../power/core/kvm-vm}/guest_channel.h        |   0
 drivers/power/core/kvm-vm/meson.build         |  20 ++
 .../power/core/kvm-vm}/power_kvm_vm.c         |  19 ++
 .../power/core/kvm-vm}/power_kvm_vm.h         |   0
 drivers/power/core/meson.build                |  12 +
 drivers/power/core/pstate/meson.build         |   8 +
 .../power/core/pstate}/power_pstate_cpufreq.c |  19 ++
 .../power/core/pstate}/power_pstate_cpufreq.h |   0
 drivers/power/meson.build                     |   9 +
 drivers/power/uncore/intel/meson.build        |   9 +
 .../power/uncore/intel}/power_intel_uncore.c  |  15 +
 .../power/uncore/intel}/power_intel_uncore.h  |   0
 drivers/power/uncore/meson.build              |   8 +
 lib/power/meson.build                         |   7 -
 lib/power/power_common.h                      |  11 +
 lib/power/rte_power.c                         | 305 ++++++++----------
 lib/power/rte_power.h                         | 207 ++++++++++--
 lib/power/rte_power_uncore.c                  | 163 ++++------
 lib/power/rte_power_uncore.h                  | 150 ++++++++-
 lib/power/version.map                         |  13 +
 31 files changed, 742 insertions(+), 315 deletions(-)
 create mode 100644 drivers/power/core/acpi/meson.build
 rename {lib/power => drivers/power/core/acpi}/power_acpi_cpufreq.c (95%)
 rename {lib/power => drivers/power/core/acpi}/power_acpi_cpufreq.h (100%)
 create mode 100644 drivers/power/core/amd-pstate/meson.build
 rename {lib/power => drivers/power/core/amd-pstate}/power_amd_pstate_cpufreq.c (95%)
 rename {lib/power => drivers/power/core/amd-pstate}/power_amd_pstate_cpufreq.h (100%)
 create mode 100644 drivers/power/core/cppc/meson.build
 rename {lib/power => drivers/power/core/cppc}/power_cppc_cpufreq.c (96%)
 rename {lib/power => drivers/power/core/cppc}/power_cppc_cpufreq.h (100%)
 rename {lib/power => drivers/power/core/kvm-vm}/guest_channel.c (100%)
 rename {lib/power => drivers/power/core/kvm-vm}/guest_channel.h (100%)
 create mode 100644 drivers/power/core/kvm-vm/meson.build
 rename {lib/power => drivers/power/core/kvm-vm}/power_kvm_vm.c (83%)
 rename {lib/power => drivers/power/core/kvm-vm}/power_kvm_vm.h (100%)
 create mode 100644 drivers/power/core/meson.build
 create mode 100644 drivers/power/core/pstate/meson.build
 rename {lib/power => drivers/power/core/pstate}/power_pstate_cpufreq.c (96%)
 rename {lib/power => drivers/power/core/pstate}/power_pstate_cpufreq.h (100%)
 create mode 100644 drivers/power/meson.build
 create mode 100644 drivers/power/uncore/intel/meson.build
 rename {lib/power => drivers/power/uncore/intel}/power_intel_uncore.c (95%)
 rename {lib/power => drivers/power/uncore/intel}/power_intel_uncore.h (100%)
 create mode 100644 drivers/power/uncore/meson.build
Signature · 1 line
2.25.1
#9 lore raw

RFC PATCH 2/2 power: refactor uncore power management library

In reply to Sivaprasad Tummala

This patch refactors the power management library, addressing uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/uncore/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

This refactor significantly improves code organization, enhances clarity and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

---
 drivers/power/meson.build                     |   1 +
 drivers/power/uncore/intel/meson.build        |   9 +
 .../power/uncore/intel}/power_intel_uncore.c  |  15 ++
 .../power/uncore/intel}/power_intel_uncore.h  |   0
 drivers/power/uncore/meson.build              |   8 +
 lib/power/meson.build                         |   1 -
 lib/power/rte_power_uncore.c                  | 163 +++++++-----------
 lib/power/rte_power_uncore.h                  | 150 ++++++++++++++--
 lib/power/version.map                         |   1 +
 9 files changed, 236 insertions(+), 112 deletions(-)
 create mode 100644 drivers/power/uncore/intel/meson.build
 rename {lib/power => drivers/power/uncore/intel}/power_intel_uncore.c (95%)
 rename {lib/power => drivers/power/uncore/intel}/power_intel_uncore.h (100%)
 create mode 100644 drivers/power/uncore/meson.build
drivers/power/meson.build +1 -0
index 7d9034c7ac..0803e99027 100644
@@ -3,6 +3,7 @@
 
 drivers = [
         'core',
+        'uncore',
 ]
 
 std_deps = ['power']
drivers/power/uncore/intel/meson.build new file +9 -0
new file mode 100644
index 0000000000..187ab15aec
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2017 Intel Corporation
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_intel_uncore.c')
+
+headers = files('power_intel_uncore.h')
+
+deps += ['power']
drivers/power/uncore/intel/power_intel_uncore.c renamed from lib/power/power_intel_uncore.c +15 -0
similarity index 95%
rename from lib/power/power_intel_uncore.c
rename to drivers/power/uncore/intel/power_intel_uncore.c
index 3ce8fccec2..3af4cc3bc7 100644
@@ -476,3 +476,18 @@ power_intel_uncore_get_num_dies(unsigned int pkg)
 
 	return count;
 }
+
+static struct rte_power_uncore_ops intel_uncore_ops = {
+	.init = power_intel_uncore_init,
+	.exit = power_intel_uncore_exit,
+	.get_avail_freqs = power_intel_uncore_freqs,
+	.get_num_pkgs = power_intel_uncore_get_num_pkgs,
+	.get_num_dies = power_intel_uncore_get_num_dies,
+	.get_num_freqs = power_intel_uncore_get_num_freqs,
+	.get_freq = power_get_intel_uncore_freq,
+	.set_freq = power_set_intel_uncore_freq,
+	.freq_max = power_intel_uncore_freq_max,
+	.freq_min = power_intel_uncore_freq_min,
+};
+
+RTE_POWER_REGISTER_UNCORE_OPS(intel_uncore_ops);
drivers/power/uncore/intel/power_intel_uncore.h renamed from lib/power/power_intel_uncore.h +0 -0
similarity index 100%
rename from lib/power/power_intel_uncore.h
rename to drivers/power/uncore/intel/power_intel_uncore.h
drivers/power/uncore/meson.build new file +8 -0
new file mode 100644
index 0000000000..005c0dc622
@@ -0,0 +1,8 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 AMD Limited
+
+drivers = [
+        'intel',
+]
+
+std_deps = ['power']
lib/power/meson.build +0 -1
index 207d96d877..459e9b6e9b 100644
@@ -13,7 +13,6 @@ if not is_linux
 endif
 sources = files(
         'power_common.c',
-        'power_intel_uncore.c',
         'rte_power.c',
         'rte_power_uncore.c',
         'rte_power_pmd_mgmt.c',
lib/power/rte_power_uncore.c +67 -96

216 lines not on this page · raw message

lib/power/rte_power_uncore.h +135 -15

210 lines not on this page · raw message

lib/power/version.map +1 -0
index 2f89645ec2..d8a6f9436c 100644
@@ -57,6 +57,7 @@ INTERNAL {
        global:
 
        rte_power_register_ops;
+       rte_power_register_uncore_ops;
        cpufreq_check_scaling_driver;
        power_set_governor;
        open_core_sysfs_file;
Signature · 1 line
2.25.1
#10 lore raw

Re: [RFC PATCH 2/2] power: refactor uncore power management library

Hi,

Quoted 5 lines · Sivaprasad Tummala

This patch refactors the power management library, addressing uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/uncore/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

+1 to discriminate core and uncore.

Quoted 18 lines

This refactor significantly improves code organization, enhances clarity and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

---
  drivers/power/meson.build                     |   1 +
  drivers/power/uncore/intel/meson.build        |   9 +
  .../power/uncore/intel}/power_intel_uncore.c  |  15 ++
  .../power/uncore/intel}/power_intel_uncore.h  |   0
  drivers/power/uncore/meson.build              |   8 +
  lib/power/meson.build                         |   1 -
  lib/power/rte_power_uncore.c                  | 163 +++++++-----------
  lib/power/rte_power_uncore.h                  | 150 ++++++++++++++--
  lib/power/version.map                         |   1 +
  9 files changed, 236 insertions(+), 112 deletions(-)
Quoted 3 lines

create mode 100644 drivers/power/uncore/intel/meson.build rename {lib/power => drivers/power/uncore/intel}/power_intel_uncore.c (95%) rename {lib/power => drivers/power/uncore/intel}/power_intel_uncore.h (100%)

How about remove 'power' in "power_intel_uncore.c"

Quoted 55 lines

create mode 100644 drivers/power/uncore/meson.build

drivers/power/meson.build +1 -0
index 7d9034c7ac..0803e99027 100644
@@ -3,6 +3,7 @@
 
  drivers = [
          'core',
+        'uncore',
  ]
 
  std_deps = ['power']
drivers/power/uncore/intel/meson.build new file +9 -0
new file mode 100644
index 0000000000..187ab15aec
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2017 Intel Corporation
+# Copyright(c) 2024 AMD Limited
+
+sources = files('power_intel_uncore.c')
+
+headers = files('power_intel_uncore.h')
+
+deps += ['power']
drivers/power/uncore/intel/power_intel_uncore.c renamed from lib/power/power_intel_uncore.c +15 -0
similarity index 95%
rename from lib/power/power_intel_uncore.c
rename to drivers/power/uncore/intel/power_intel_uncore.c
index 3ce8fccec2..3af4cc3bc7 100644
@@ -476,3 +476,18 @@ power_intel_uncore_get_num_dies(unsigned int pkg)
 
  	return count;
  }
+
+static struct rte_power_uncore_ops intel_uncore_ops = {
+	.init = power_intel_uncore_init,
+	.exit = power_intel_uncore_exit,
+	.get_avail_freqs = power_intel_uncore_freqs,
+	.get_num_pkgs = power_intel_uncore_get_num_pkgs,
+	.get_num_dies = power_intel_uncore_get_num_dies,
+	.get_num_freqs = power_intel_uncore_get_num_freqs,
+	.get_freq = power_get_intel_uncore_freq,
+	.set_freq = power_set_intel_uncore_freq,
+	.freq_max = power_intel_uncore_freq_max,
+	.freq_min = power_intel_uncore_freq_min,
+};
+
+RTE_POWER_REGISTER_UNCORE_OPS(intel_uncore_ops);

<...>

Quoted 6 lines
drivers/power/uncore/intel/power_intel_uncore.c renamed from lib/power/power_intel_uncore.c +6 -0
+
+/** Structure defining uncore power operations structure */
+struct rte_power_uncore_ops {
+	uint8_t status;                         /**< ops register status. */
+	enum rte_uncore_power_mgmt_env env;          /**< power mgmt env. */
+	rte_power_uncore_init_t init;    /**< Initialize power management. */

drivers/power/uncore/intel/power_intel_uncore.c

Quoted 10 lines
drivers/power/uncore/intel/power_intel_uncore.c renamed from lib/power/power_intel_uncore.c +10 -0
+	rte_power_uncore_exit_t exit;    /**< Exit power management. */
+	rte_power_uncore_get_num_pkgs_t get_num_pkgs;
+	rte_power_uncore_get_num_dies_t get_num_dies;
+	rte_power_uncore_get_num_freqs_t get_num_freqs; /**< Number of available frequencies. */
+	rte_power_uncore_freqs_t get_avail_freqs; /**< Get the available frequencies. */
+	rte_power_get_uncore_freq_t get_freq; /**< Get frequency index. */
+	rte_power_set_uncore_freq_t set_freq; /**< Set frequency index. */
+	rte_power_uncore_freq_change_t freq_max;  /**< Scale up frequency to highest. */
+	rte_power_uncore_freq_change_t freq_min;  /**< Scale up frequency to lowest. */
+} __rte_cache_aligned;

For all core drivers (cpufreq),  they all basically follow the ACPI specification.
So libray can extract a common ops for all core DVFS driver.
AFAIS, there is only one uncore driver in kernel, namely intel uncore driver.
But there is not an unify specification to control uncore frequency scaling(UFS) in kernel.
That is to say, every chip manufacturers can implement their uncore driver as themselves request. As a result, there is different system interface for userspace between manufacturer.
So I am not sure if this new extracted rte_power_uncore_ops sturcture is very common for all uncore drivers in future.

Quoted 21 lines
drivers/power/uncore/intel/power_intel_uncore.c renamed from lib/power/power_intel_uncore.c +21 -0
+
+/**
+ * Register power uncore frequency operations.
+ * @param ops
+ *   Pointer to an ops structure to register.
+ * @return
+ *   - >=0: Success; return the index of the ops struct in the table.
+ *   - -EINVAL - error while registering ops struct.
+ */
+__rte_internal
+int rte_power_register_uncore_ops(const struct rte_power_uncore_ops *ops);
+
+/**
+ * Macro to statically register the ops of an uncore driver.
+ */
+#define RTE_POWER_REGISTER_UNCORE_OPS(ops)		\
+	(RTE_INIT(power_hdlr_init_uncore_##ops)         \
+	{                                               \
+		rte_power_register_uncore_ops(&ops);    \
+	})
+

<...>

#11 lore raw

RE: [RFC PATCH 2/2] power: refactor uncore power management library

[AMD Official Use Only - General]

Hi Lihuisong,

Quoted 43 lines

-----Original Message-----
From: lihuisong (C) <[email protected]>
Sent: Friday, March 1, 2024 9:04 AM
To: Tummala, Sivaprasad <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Subject: Re: [RFC PATCH 2/2] power: refactor uncore power management library

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Hi,

Quoted 5 lines · Sivaprasad Tummala

This patch refactors the power management library, addressing uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/uncore/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

+1 to discriminate core and uncore.

Quoted 18 lines

This refactor significantly improves code organization, enhances clarity and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

---
  drivers/power/meson.build                     |   1 +
  drivers/power/uncore/intel/meson.build        |   9 +
  .../power/uncore/intel}/power_intel_uncore.c  |  15 ++
  .../power/uncore/intel}/power_intel_uncore.h  |   0
  drivers/power/uncore/meson.build              |   8 +
  lib/power/meson.build                         |   1 -
  lib/power/rte_power_uncore.c                  | 163 +++++++-----------
  lib/power/rte_power_uncore.h                  | 150 ++++++++++++++--
  lib/power/version.map                         |   1 +
  9 files changed, 236 insertions(+), 112 deletions(-)
Quoted 2 lines

create mode 100644 drivers/power/uncore/intel/meson.build rename {lib/power => drivers/power/uncore/intel}/power_intel_uncore.c

Quoted 4 lines

(95%)

Quoted 2 lines

rename {lib/power =>
drivers/power/uncore/intel}/power_intel_uncore.h (100%)

How about remove 'power' in "power_intel_uncore.c"

ACK!

Quoted 75 lines
Quoted 55 lines

create mode 100644 drivers/power/uncore/meson.build

drivers/power/meson.build +1 -0
index 7d9034c7ac..0803e99027 100644
@@ -3,6 +3,7 @@
 
  drivers = [
          'core',
+        'uncore',
  ]
 
  std_deps = ['power']
drivers/power/uncore/intel/meson.build new file +8 -0
new file mode 100644
index 0000000000..187ab15aec
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2017 Intel
+Corporation # Copyright(c) 2024 AMD Limited
+
+sources = files('power_intel_uncore.c')
+
+headers = files('power_intel_uncore.h')
+
+deps += ['power']
drivers/power/uncore/intel/power_intel_uncore.c renamed from lib/power/power_intel_uncore.c rename to +0 -0
similarity index 95%
rename from lib/power/power_intel_uncore.c rename to

drivers/power/uncore/intel/power_intel_uncore.c
index 3ce8fccec2..3af4cc3bc7 100644

drivers/power/uncore/intel/power_intel_uncore.c renamed from lib/power/power_intel_uncore.c +14 -0
@@ -476,3 +476,18 @@ power_intel_uncore_get_num_dies(unsigned int pkg)
 
      return count;
  }
+
+static struct rte_power_uncore_ops intel_uncore_ops = {
+     .init = power_intel_uncore_init,
+     .exit = power_intel_uncore_exit,
+     .get_avail_freqs = power_intel_uncore_freqs,
+     .get_num_pkgs = power_intel_uncore_get_num_pkgs,
+     .get_num_dies = power_intel_uncore_get_num_dies,
+     .get_num_freqs = power_intel_uncore_get_num_freqs,
+     .get_freq = power_get_intel_uncore_freq,
+     .set_freq = power_set_intel_uncore_freq,
+     .freq_max = power_intel_uncore_freq_max,
+     .freq_min = power_intel_uncore_freq_min, };
+
+RTE_POWER_REGISTER_UNCORE_OPS(intel_uncore_ops);

<...>

drivers/power/uncore/intel/power_intel_uncore.c

Quoted 10 lines
drivers/power/uncore/intel/power_intel_uncore.c renamed from lib/power/power_intel_uncore.c +10 -0
+
+/** Structure defining uncore power operations structure */ struct
+rte_power_uncore_ops {
+     uint8_t status;                         /**< ops register status. */
+     enum rte_uncore_power_mgmt_env env;          /**< power mgmt env. */
+     rte_power_uncore_init_t init;    /**< Initialize power management. */
+     rte_power_uncore_exit_t exit;    /**< Exit power management. */
+     rte_power_uncore_get_num_pkgs_t get_num_pkgs;
+     rte_power_uncore_get_num_dies_t get_num_dies;
+     rte_power_uncore_get_num_freqs_t get_num_freqs; /**< Number of

available frequencies. */

Quoted 1 line

+ rte_power_uncore_freqs_t get_avail_freqs; /**< Get the available

frequencies. */

drivers/power/uncore/intel/power_intel_uncore.c

Quoted 3 lines
drivers/power/uncore/intel/power_intel_uncore.c renamed from lib/power/power_intel_uncore.c +3 -0
+     rte_power_get_uncore_freq_t get_freq; /**< Get frequency index. */
+     rte_power_set_uncore_freq_t set_freq; /**< Set frequency index. */
+     rte_power_uncore_freq_change_t freq_max;  /**< Scale up frequency to

highest. */

drivers/power/uncore/intel/power_intel_uncore.c

Quoted 2 lines
drivers/power/uncore/intel/power_intel_uncore.c renamed from lib/power/power_intel_uncore.c +2 -0
+     rte_power_uncore_freq_change_t freq_min;  /**< Scale up
+frequency to lowest. */ } __rte_cache_aligned;
Quoted 11 lines

For all core drivers (cpufreq), they all basically follow the ACPI specification. So libray can extract a common ops for all core DVFS driver.
AFAIS, there is only one uncore driver in kernel, namely intel uncore driver. But there is not an unify specification to control uncore frequency scaling(UFS) in kernel.
That is to say, every chip manufacturers can implement their uncore driver as themselves request.
As a result, there is different system interface for userspace between manufacturer.
So I am not sure if this new extracted rte_power_uncore_ops sturcture is very common for all uncore drivers in future.

Agreed! The uncore implementation (vendor specific) are expected to be abstracted At driver level. One possible approach I think is to provide different performance levels (instead of num_freqs) by the uncore library and each driver implementation can interpret/implement the perf level independently (uncore/crosssocket/pcie/umc frequencies). Application can query the no. of performance levels (highest to lowest) and can select a Performance level as needed for power savings.

Quoted 15 lines
Quoted 15 lines
drivers/power/uncore/intel/power_intel_uncore.c renamed from lib/power/power_intel_uncore.c +15 -0
+
+/**
+ * Register power uncore frequency operations.
+ * @param ops
+ *   Pointer to an ops structure to register.
+ * @return
+ *   - >=0: Success; return the index of the ops struct in the table.
+ *   - -EINVAL - error while registering ops struct.
+ */
+__rte_internal
+int rte_power_register_uncore_ops(const struct rte_power_uncore_ops
+*ops);
+
+/**
+ * Macro to statically register the ops of an uncore driver.
Quoted 8 lines
Quoted 7 lines

+ */

drivers/power/uncore/intel/power_intel_uncore.c renamed from lib/power/power_intel_uncore.c +6 -0
+#define RTE_POWER_REGISTER_UNCORE_OPS(ops)           \
+     (RTE_INIT(power_hdlr_init_uncore_##ops)         \
+     {                                               \
+             rte_power_register_uncore_ops(&ops);    \
+     })
+

<...>

Thanks & Regards,
Sivaprasad

#12 lore raw

cover power: refactor power management library

In reply to Sivaprasad Tummala

This patchset refactors the power management library, addressing both core and uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/core/*' and 'drivers/power/uncore/*'.

This refactor significantly improves code organization, enhances clarity, and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

Furthermore, this effort aims to streamline code maintenance by consolidating common functions for cpufreq and cppc across various core drivers, thus reducing code duplication.

Sivaprasad Tummala (4):
power: refactor core power management library
power: refactor uncore power management library test/power: removed function pointer validations power/amd_uncore: uncore power management support for AMD EPYC
processors

 app/test/test_power.c                         |  95 ------
 app/test/test_power_cpufreq.c                 |  52 ---
 app/test/test_power_kvm_vm.c                  |  36 --
 drivers/meson.build                           |   1 +
 .../power/acpi/acpi_cpufreq.c                 |  22 +-
 .../power/acpi/acpi_cpufreq.h                 |   6 +-
 drivers/power/acpi/meson.build                |  10 +
 .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
 .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
 drivers/power/amd_pstate/meson.build          |  10 +
 drivers/power/amd_uncore/amd_uncore.c         | 321 ++++++++++++++++++
 drivers/power/amd_uncore/amd_uncore.h         | 226 ++++++++++++
 drivers/power/amd_uncore/meson.build          |  20 ++
 .../power/cppc/cppc_cpufreq.c                 |  22 +-
 .../power/cppc/cppc_cpufreq.h                 |   8 +-
 drivers/power/cppc/meson.build                |  10 +
 .../power/intel_uncore/intel_uncore.c         |  18 +-
 .../power/intel_uncore/intel_uncore.h         |   8 +-
 drivers/power/intel_uncore/meson.build        |   7 +
 .../power/kvm_vm}/guest_channel.c             |   0
 .../power/kvm_vm}/guest_channel.h             |   0
 .../power/kvm_vm/kvm_vm.c                     |  22 +-
 .../power/kvm_vm/kvm_vm.h                     |   6 +-
 drivers/power/kvm_vm/meson.build              |  16 +
 drivers/power/meson.build                     |  14 +
 drivers/power/pstate/meson.build              |  10 +
 .../power/pstate/pstate_cpufreq.c             |  22 +-
 .../power/pstate/pstate_cpufreq.h             |   6 +-
 lib/power/meson.build                         |   9 +-
 lib/power/power_common.c                      |   2 +-
 lib/power/power_common.h                      |  16 +-
 lib/power/rte_power.c                         | 287 ++++++----------
 lib/power/rte_power.h                         | 139 +++++---
 lib/power/rte_power_core_ops.h                | 208 ++++++++++++
 lib/power/rte_power_uncore.c                  | 206 +++++------
 lib/power/rte_power_uncore.h                  |  91 ++---
 lib/power/rte_power_uncore_ops.h              | 239 +++++++++++++
 lib/power/version.map                         |  15 +
 38 files changed, 1591 insertions(+), 621 deletions(-)
 rename lib/power/power_acpi_cpufreq.c => drivers/power/acpi/acpi_cpufreq.c (95%)
 rename lib/power/power_acpi_cpufreq.h => drivers/power/acpi/acpi_cpufreq.h (98%)
 create mode 100644 drivers/power/acpi/meson.build
 rename lib/power/power_amd_pstate_cpufreq.c => drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%)
 rename lib/power/power_amd_pstate_cpufreq.h => drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%)
 create mode 100644 drivers/power/amd_pstate/meson.build
 create mode 100644 drivers/power/amd_uncore/amd_uncore.c
 create mode 100644 drivers/power/amd_uncore/amd_uncore.h
 create mode 100644 drivers/power/amd_uncore/meson.build
 rename lib/power/power_cppc_cpufreq.c => drivers/power/cppc/cppc_cpufreq.c (95%)
 rename lib/power/power_cppc_cpufreq.h => drivers/power/cppc/cppc_cpufreq.h (97%)
 create mode 100644 drivers/power/cppc/meson.build
 rename lib/power/power_intel_uncore.c => drivers/power/intel_uncore/intel_uncore.c (95%)
 rename lib/power/power_intel_uncore.h => drivers/power/intel_uncore/intel_uncore.h (97%)
 create mode 100644 drivers/power/intel_uncore/meson.build
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%)
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%)
 rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c (82%)
 rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h (98%)
 create mode 100644 drivers/power/kvm_vm/meson.build
 create mode 100644 drivers/power/meson.build
 create mode 100644 drivers/power/pstate/meson.build
 rename lib/power/power_pstate_cpufreq.c => drivers/power/pstate/pstate_cpufreq.c (96%)
 rename lib/power/power_pstate_cpufreq.h => drivers/power/pstate/pstate_cpufreq.h (98%)
 create mode 100644 lib/power/rte_power_core_ops.h
 create mode 100644 lib/power/rte_power_uncore_ops.h
Signature · 1 line
2.34.1
#13 lore raw

PATCH 1/4 power: refactor core power management library

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

---
 drivers/meson.build                           |   1 +
 .../power/acpi/acpi_cpufreq.c                 |  22 +-
 .../power/acpi/acpi_cpufreq.h                 |   6 +-
 drivers/power/acpi/meson.build                |  10 +
 .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
 .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
 drivers/power/amd_pstate/meson.build          |  10 +
 .../power/cppc/cppc_cpufreq.c                 |  22 +-
 .../power/cppc/cppc_cpufreq.h                 |   8 +-
 drivers/power/cppc/meson.build                |  10 +
 .../power/kvm_vm}/guest_channel.c             |   0
 .../power/kvm_vm}/guest_channel.h             |   0
 .../power/kvm_vm/kvm_vm.c                     |  22 +-
 .../power/kvm_vm/kvm_vm.h                     |   6 +-
 drivers/power/kvm_vm/meson.build              |  16 +
 drivers/power/meson.build                     |  12 +
 drivers/power/pstate/meson.build              |  10 +
 .../power/pstate/pstate_cpufreq.c             |  22 +-
 .../power/pstate/pstate_cpufreq.h             |   6 +-
 lib/power/meson.build                         |   7 +-
 lib/power/power_common.c                      |   2 +-
 lib/power/power_common.h                      |  16 +-
 lib/power/rte_power.c                         | 287 ++++++------------
 lib/power/rte_power.h                         | 139 ++++++---
 lib/power/rte_power_core_ops.h                | 208 +++++++++++++
 lib/power/version.map                         |  14 +
 26 files changed, 618 insertions(+), 270 deletions(-)
 rename lib/power/power_acpi_cpufreq.c => drivers/power/acpi/acpi_cpufreq.c (95%)
 rename lib/power/power_acpi_cpufreq.h => drivers/power/acpi/acpi_cpufreq.h (98%)
 create mode 100644 drivers/power/acpi/meson.build
 rename lib/power/power_amd_pstate_cpufreq.c => drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%)
 rename lib/power/power_amd_pstate_cpufreq.h => drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%)
 create mode 100644 drivers/power/amd_pstate/meson.build
 rename lib/power/power_cppc_cpufreq.c => drivers/power/cppc/cppc_cpufreq.c (95%)
 rename lib/power/power_cppc_cpufreq.h => drivers/power/cppc/cppc_cpufreq.h (97%)
 create mode 100644 drivers/power/cppc/meson.build
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%)
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%)
 rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c (82%)
 rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h (98%)
 create mode 100644 drivers/power/kvm_vm/meson.build
 create mode 100644 drivers/power/meson.build
 create mode 100644 drivers/power/pstate/meson.build
 rename lib/power/power_pstate_cpufreq.c => drivers/power/pstate/pstate_cpufreq.c (96%)
 rename lib/power/power_pstate_cpufreq.h => drivers/power/pstate/pstate_cpufreq.h (98%)
 create mode 100644 lib/power/rte_power_core_ops.h
drivers/meson.build +1 -0
index 66931d4241..9d77e0deab 100644
@@ -29,6 +29,7 @@ subdirs = [
         'event',          # depends on common, bus, mempool and net.
         'baseband',       # depends on common and bus.
         'gpu',            # depends on common and bus.
+        'power',          # depends on common (in future).
 ]
 
 if meson.is_cross_build()
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +21 -1
similarity index 95%
rename from lib/power/power_acpi_cpufreq.c
rename to drivers/power/acpi/acpi_cpufreq.c
index 81996e1c13..8637c69703 100644
@@ -10,7 +10,7 @@
 #include <rte_stdatomic.h>
 #include <rte_string_fns.h>
 
-#include "power_acpi_cpufreq.h"
+#include "acpi_cpufreq.h"
 #include "power_common.h"
 
 #define STR_SIZE     1024
@@ -577,3 +577,23 @@ int power_acpi_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_core_ops acpi_ops = {
+	.name = "acpi",
+	.init = power_acpi_cpufreq_init,
+	.exit = power_acpi_cpufreq_exit,
+	.check_env_support = power_acpi_cpufreq_check_supported,
+	.get_avail_freqs = power_acpi_cpufreq_freqs,
+	.get_freq = power_acpi_cpufreq_get_freq,
+	.set_freq = power_acpi_cpufreq_set_freq,
+	.freq_down = power_acpi_cpufreq_freq_down,
+	.freq_up = power_acpi_cpufreq_freq_up,
+	.freq_max = power_acpi_cpufreq_freq_max,
+	.freq_min = power_acpi_cpufreq_freq_min,
+	.turbo_status = power_acpi_turbo_status,
+	.enable_turbo = power_acpi_enable_turbo,
+	.disable_turbo = power_acpi_disable_turbo,
+	.get_caps = power_acpi_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(acpi_ops);
drivers/power/acpi/acpi_cpufreq.h renamed from lib/power/power_acpi_cpufreq.h +3 -3
similarity index 98%
rename from lib/power/power_acpi_cpufreq.h
rename to drivers/power/acpi/acpi_cpufreq.h
index 682fd9278c..1194a7e2a5 100644
@@ -2,15 +2,15 @@
  * Copyright(c) 2010-2014 Intel Corporation
  */
 
-#ifndef _POWER_ACPI_CPUFREQ_H
-#define _POWER_ACPI_CPUFREQ_H
+#ifndef _ACPI_CPUFREQ_H
+#define _ACPI_CPUFREQ_H
 
 /**
  * @file
  * RTE Power Management via userspace ACPI cpufreq
  */
 
-#include "rte_power.h"
+#include "rte_power_core_ops.h"
 
 /**
  * Check if ACPI power management is supported.
drivers/power/acpi/meson.build new file +10 -0
new file mode 100644
index 0000000000..f5afc893ce
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('acpi_cpufreq.c')
+
+deps += ['power']
drivers/power/amd_pstate/amd_pstate_cpufreq.c renamed from lib/power/power_amd_pstate_cpufreq.c +22 -2
similarity index 95%
rename from lib/power/power_amd_pstate_cpufreq.c
rename to drivers/power/amd_pstate/amd_pstate_cpufreq.c
index 090a0d96cb..f571f4184a 100644
@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: BSD-3-Clause
  * Copyright(c) 2010-2021 Intel Corporation
  * Copyright(c) 2021 Arm Limited
- * Copyright(c) 2023 Amd Limited
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
  */
 
 #include <stdlib.h>
@@ -9,7 +9,7 @@
 #include <rte_memcpy.h>
 #include <rte_stdatomic.h>
 
-#include "power_amd_pstate_cpufreq.h"
+#include "amd_pstate_cpufreq.h"
 #include "power_common.h"
 
 /* macros used for rounding frequency to nearest 1000 */
@@ -700,3 +700,23 @@ power_amd_pstate_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_core_ops amd_pstate_ops = {
+	.name = "amd-pstate",
+	.init = power_amd_pstate_cpufreq_init,
+	.exit = power_amd_pstate_cpufreq_exit,
+	.check_env_support = power_amd_pstate_cpufreq_check_supported,
+	.get_avail_freqs = power_amd_pstate_cpufreq_freqs,
+	.get_freq = power_amd_pstate_cpufreq_get_freq,
+	.set_freq = power_amd_pstate_cpufreq_set_freq,
+	.freq_down = power_amd_pstate_cpufreq_freq_down,
+	.freq_up = power_amd_pstate_cpufreq_freq_up,
+	.freq_max = power_amd_pstate_cpufreq_freq_max,
+	.freq_min = power_amd_pstate_cpufreq_freq_min,
+	.turbo_status = power_amd_pstate_turbo_status,
+	.enable_turbo = power_amd_pstate_enable_turbo,
+	.disable_turbo = power_amd_pstate_disable_turbo,
+	.get_caps = power_amd_pstate_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(amd_pstate_ops);
drivers/power/amd_pstate/amd_pstate_cpufreq.h renamed from lib/power/power_amd_pstate_cpufreq.h +4 -4
similarity index 97%
rename from lib/power/power_amd_pstate_cpufreq.h
rename to drivers/power/amd_pstate/amd_pstate_cpufreq.h
index b02f9f98e4..b04b2f28c0 100644
@@ -1,18 +1,18 @@
 /* SPDX-License-Identifier: BSD-3-Clause
  * Copyright(c) 2010-2021 Intel Corporation
  * Copyright(c) 2021 Arm Limited
- * Copyright(c) 2023 Amd Limited
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
  */
 
-#ifndef _POWER_AMD_PSTATE_CPUFREQ_H
-#define _POWER_AMD_PSTATE_CPUFREQ_H
+#ifndef _AMD_PSTATE_CPUFREQ_H
+#define _AMD_PSTATE_CPUFREQ_H
 
 /**
  * @file
  * RTE Power Management via userspace AMD pstate cpufreq
  */
 
-#include "rte_power.h"
+#include "rte_power_core_ops.h"
 
 /**
  * Check if amd p-state power management is supported.
drivers/power/amd_pstate/meson.build new file +10 -0
new file mode 100644
index 0000000000..acaf20b388
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('amd_pstate_cpufreq.c')
+
+deps += ['power']
drivers/power/cppc/cppc_cpufreq.c renamed from lib/power/power_cppc_cpufreq.c +21 -1
similarity index 95%
rename from lib/power/power_cppc_cpufreq.c
rename to drivers/power/cppc/cppc_cpufreq.c
index 32aaacb948..775b8f4434 100644
@@ -8,7 +8,7 @@
 #include <rte_memcpy.h>
 #include <rte_stdatomic.h>
 
-#include "power_cppc_cpufreq.h"
+#include "cppc_cpufreq.h"
 #include "power_common.h"
 
 /* macros used for rounding frequency to nearest 100000 */
@@ -685,3 +685,23 @@ power_cppc_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_core_ops cppc_ops = {
+	.name = "cppc",
+	.init = power_cppc_cpufreq_init,
+	.exit = power_cppc_cpufreq_exit,
+	.check_env_support = power_cppc_cpufreq_check_supported,
+	.get_avail_freqs = power_cppc_cpufreq_freqs,
+	.get_freq = power_cppc_cpufreq_get_freq,
+	.set_freq = power_cppc_cpufreq_set_freq,
+	.freq_down = power_cppc_cpufreq_freq_down,
+	.freq_up = power_cppc_cpufreq_freq_up,
+	.freq_max = power_cppc_cpufreq_freq_max,
+	.freq_min = power_cppc_cpufreq_freq_min,
+	.turbo_status = power_cppc_turbo_status,
+	.enable_turbo = power_cppc_enable_turbo,
+	.disable_turbo = power_cppc_disable_turbo,
+	.get_caps = power_cppc_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(cppc_ops);
drivers/power/cppc/cppc_cpufreq.h renamed from lib/power/power_cppc_cpufreq.h +4 -4
similarity index 97%
rename from lib/power/power_cppc_cpufreq.h
rename to drivers/power/cppc/cppc_cpufreq.h
index f4121b237e..d6e32fdd47 100644
@@ -3,15 +3,15 @@
  * Copyright(c) 2021 Arm Limited
  */
 
-#ifndef _POWER_CPPC_CPUFREQ_H
-#define _POWER_CPPC_CPUFREQ_H
+#ifndef _CPPC_CPUFREQ_H
+#define _CPPC_CPUFREQ_H
 
 /**
  * @file
  * RTE Power Management via userspace CPPC cpufreq
  */
 
-#include "rte_power.h"
+#include "rte_power_core_ops.h"
 
 /**
  * Check if CPPC power management is supported.
@@ -215,4 +215,4 @@ int power_cppc_disable_turbo(unsigned int lcore_id);
 int power_cppc_get_capabilities(unsigned int lcore_id,
 		struct rte_power_core_capabilities *caps);
 
-#endif /* _POWER_CPPC_CPUFREQ_H */
+#endif /* _CPPC_CPUFREQ_H */
drivers/power/cppc/meson.build new file +10 -0
new file mode 100644
index 0000000000..f1948cd424
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('cppc_cpufreq.c')
+
+deps += ['power']
drivers/power/kvm_vm/guest_channel.c renamed from lib/power/guest_channel.c +0 -0
similarity index 100%
rename from lib/power/guest_channel.c
rename to drivers/power/kvm_vm/guest_channel.c
drivers/power/kvm_vm/guest_channel.h renamed from lib/power/guest_channel.h +0 -0
similarity index 100%
rename from lib/power/guest_channel.h
rename to drivers/power/kvm_vm/guest_channel.h
drivers/power/kvm_vm/kvm_vm.c renamed from lib/power/power_kvm_vm.c +21 -1
similarity index 82%
rename from lib/power/power_kvm_vm.c
rename to drivers/power/kvm_vm/kvm_vm.c
index f15be8fac5..a1342dcd8b 100644
@@ -9,7 +9,7 @@
 #include "rte_power_guest_channel.h"
 #include "guest_channel.h"
 #include "power_common.h"
-#include "power_kvm_vm.h"
+#include "kvm_vm.h"
 
 #define FD_PATH "/dev/virtio-ports/virtio.serial.port.poweragent"
 
@@ -137,3 +137,23 @@ int power_kvm_vm_get_capabilities(__rte_unused unsigned int lcore_id,
 	POWER_LOG(ERR, "rte_power_get_capabilities is not implemented for Virtual Machine Power Management");
 	return -ENOTSUP;
 }
+
+static struct rte_power_core_ops kvm_vm_ops = {
+	.name = "kvm-vm",
+	.init = power_kvm_vm_init,
+	.exit = power_kvm_vm_exit,
+	.check_env_support = power_kvm_vm_check_supported,
+	.get_avail_freqs = power_kvm_vm_freqs,
+	.get_freq = power_kvm_vm_get_freq,
+	.set_freq = power_kvm_vm_set_freq,
+	.freq_down = power_kvm_vm_freq_down,
+	.freq_up = power_kvm_vm_freq_up,
+	.freq_max = power_kvm_vm_freq_max,
+	.freq_min = power_kvm_vm_freq_min,
+	.turbo_status = power_kvm_vm_turbo_status,
+	.enable_turbo = power_kvm_vm_enable_turbo,
+	.disable_turbo = power_kvm_vm_disable_turbo,
+	.get_caps = power_kvm_vm_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(kvm_vm_ops);
drivers/power/kvm_vm/kvm_vm.h renamed from lib/power/power_kvm_vm.h +3 -3
similarity index 98%
rename from lib/power/power_kvm_vm.h
rename to drivers/power/kvm_vm/kvm_vm.h
index 303fcc041b..64086a67e7 100644
@@ -2,15 +2,15 @@
  * Copyright(c) 2010-2014 Intel Corporation
  */
 
-#ifndef _POWER_KVM_VM_H
-#define _POWER_KVM_VM_H
+#ifndef _KVM_VM_H
+#define _KVM_VM_H
 
 /**
  * @file
  * RTE Power Management KVM VM
  */
 
-#include "rte_power.h"
+#include "rte_power_core_ops.h"
 
 /**
  * Check if KVM power management is supported.
drivers/power/kvm_vm/meson.build new file +16 -0
new file mode 100644
index 0000000000..405524ce7c
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 Advanced Micro Devices, Inc.
+#
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+sources = files(
+        'guest_channel.c',
+        'kvm_vm.c',
+)
+
+deps += ['power']
drivers/power/meson.build new file +12 -0
new file mode 100644
index 0000000000..8c7215c639
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+drivers = [
+        'acpi',
+        'amd_pstate',
+        'cppc',
+        'kvm_vm',
+        'pstate'
+]
+
+std_deps = ['power']
drivers/power/pstate/meson.build new file +10 -0
new file mode 100644
index 0000000000..9cd47833fb
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('pstate_cpufreq.c')
+
+deps += ['power']
drivers/power/pstate/pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c +21 -1
similarity index 96%
rename from lib/power/power_pstate_cpufreq.c
rename to drivers/power/pstate/pstate_cpufreq.c
index 2343121621..c32b1adabc 100644
@@ -15,7 +15,7 @@
 #include <rte_stdatomic.h>
 
 #include "rte_power_pmd_mgmt.h"
-#include "power_pstate_cpufreq.h"
+#include "pstate_cpufreq.h"
 #include "power_common.h"
 
 /* macros used for rounding frequency to nearest 100000 */
@@ -888,3 +888,23 @@ int power_pstate_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_core_ops pstate_ops = {
+	.name = "pstate",
+	.init = power_pstate_cpufreq_init,
+	.exit = power_pstate_cpufreq_exit,
+	.check_env_support = power_pstate_cpufreq_check_supported,
+	.get_avail_freqs = power_pstate_cpufreq_freqs,
+	.get_freq = power_pstate_cpufreq_get_freq,
+	.set_freq = power_pstate_cpufreq_set_freq,
+	.freq_down = power_pstate_cpufreq_freq_down,
+	.freq_up = power_pstate_cpufreq_freq_up,
+	.freq_max = power_pstate_cpufreq_freq_max,
+	.freq_min = power_pstate_cpufreq_freq_min,
+	.turbo_status = power_pstate_turbo_status,
+	.enable_turbo = power_pstate_enable_turbo,
+	.disable_turbo = power_pstate_disable_turbo,
+	.get_caps = power_pstate_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(pstate_ops);
drivers/power/pstate/pstate_cpufreq.h renamed from lib/power/power_pstate_cpufreq.h +3 -3
similarity index 98%
rename from lib/power/power_pstate_cpufreq.h
rename to drivers/power/pstate/pstate_cpufreq.h
index 7bf64a518c..8b67b2da21 100644
@@ -2,15 +2,15 @@
  * Copyright(c) 2018 Intel Corporation
  */
 
-#ifndef _POWER_PSTATE_CPUFREQ_H
-#define _POWER_PSTATE_CPUFREQ_H
+#ifndef _PSTATE_CPUFREQ_H
+#define _PSTATE_CPUFREQ_H
 
 /**
  * @file
  * RTE Power Management via Intel Pstate driver
  */
 
-#include "rte_power.h"
+#include "rte_power_core_ops.h"
 
 /**
  * Check if pstate power management is supported.
lib/power/meson.build +1 -6
index b8426589b2..f3e3451cdc 100644
@@ -12,20 +12,15 @@ if not is_linux
     reason = 'only supported on Linux'
 endif
 sources = files(
-        'guest_channel.c',
-        'power_acpi_cpufreq.c',
-        'power_amd_pstate_cpufreq.c',
         'power_common.c',
-        'power_cppc_cpufreq.c',
-        'power_kvm_vm.c',
         'power_intel_uncore.c',
-        'power_pstate_cpufreq.c',
         'rte_power.c',
         'rte_power_uncore.c',
         'rte_power_pmd_mgmt.c',
 )
 headers = files(
         'rte_power.h',
+        'rte_power_core_ops.h',
         'rte_power_guest_channel.h',
         'rte_power_pmd_mgmt.h',
         'rte_power_uncore.h',
lib/power/power_common.c +1 -1
index 590986d5ef..6c06411e8b 100644
@@ -12,7 +12,7 @@
 
 #include "power_common.h"
 
-RTE_LOG_REGISTER_DEFAULT(power_logtype, INFO);
+RTE_LOG_REGISTER_DEFAULT(rte_power_logtype, INFO);
 
 #define POWER_SYSFILE_SCALING_DRIVER   \
 		"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_driver"
lib/power/power_common.h +14 -2
index 83f742f42a..767686ee12 100644
@@ -6,12 +6,13 @@
 #define _POWER_COMMON_H_
 
 #include <rte_common.h>
+#include <rte_compat.h>
 #include <rte_log.h>
 
 #define RTE_POWER_INVALID_FREQ_INDEX (~0)
 
-extern int power_logtype;
-#define RTE_LOGTYPE_POWER power_logtype
+extern int rte_power_logtype;
+#define RTE_LOGTYPE_POWER rte_power_logtype
 #define POWER_LOG(level, ...) \
 	RTE_LOG_LINE(level, POWER, "" __VA_ARGS__)
 
@@ -23,13 +24,24 @@ extern int power_logtype;
 #endif
 
 /* check if scaling driver matches one we want */
+__rte_internal
 int cpufreq_check_scaling_driver(const char *driver);
+
+__rte_internal
 int power_set_governor(unsigned int lcore_id, const char *new_governor,
 		char *orig_governor, size_t orig_governor_len);
+
+__rte_internal
 int open_core_sysfs_file(FILE **f, const char *mode, const char *format, ...)
 		__rte_format_printf(3, 4);
+
+__rte_internal
 int read_core_sysfs_u32(FILE *f, uint32_t *val);
+
+__rte_internal
 int read_core_sysfs_s(FILE *f, char *buf, unsigned int len);
+
+__rte_internal
 int write_core_sysfs_s(FILE *f, const char *str);
 
 #endif /* _POWER_COMMON_H_ */
lib/power/rte_power.c +95 -192

342 lines not on this page · raw message

lib/power/rte_power.h +93 -46

229 lines not on this page · raw message

lib/power/rte_power_core_ops.h new file +208 -0

208 lines not on this page · raw message

lib/power/version.map +14 -0
index ad92a65f91..c2098fd667 100644
@@ -51,4 +51,18 @@ EXPERIMENTAL {
 	rte_power_set_uncore_env;
 	rte_power_uncore_freqs;
 	rte_power_unset_uncore_env;
+	# added in 24.07
+	rte_power_logtype;
+};
+
+INTERNAL {
+	global:
+
+	rte_power_register_ops;
+	cpufreq_check_scaling_driver;
+	power_set_governor;
+	open_core_sysfs_file;
+	read_core_sysfs_u32;
+	read_core_sysfs_s;
+	write_core_sysfs_s;
 };
Signature · 1 line
2.34.1
#14 lore raw

Re: [PATCH v1 1/4] power: refactor core power management library

Hi Sivaprasad,

A couple of comments below:

Quoted 54 lines · Sivaprasad Tummala

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

---
  drivers/meson.build                           |   1 +
  .../power/acpi/acpi_cpufreq.c                 |  22 +-
  .../power/acpi/acpi_cpufreq.h                 |   6 +-
  drivers/power/acpi/meson.build                |  10 +
  .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
  .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
  drivers/power/amd_pstate/meson.build          |  10 +
  .../power/cppc/cppc_cpufreq.c                 |  22 +-
  .../power/cppc/cppc_cpufreq.h                 |   8 +-
  drivers/power/cppc/meson.build                |  10 +
  .../power/kvm_vm}/guest_channel.c             |   0
  .../power/kvm_vm}/guest_channel.h             |   0
  .../power/kvm_vm/kvm_vm.c                     |  22 +-
  .../power/kvm_vm/kvm_vm.h                     |   6 +-
  drivers/power/kvm_vm/meson.build              |  16 +
  drivers/power/meson.build                     |  12 +
  drivers/power/pstate/meson.build              |  10 +
  .../power/pstate/pstate_cpufreq.c             |  22 +-
  .../power/pstate/pstate_cpufreq.h             |   6 +-
  lib/power/meson.build                         |   7 +-
  lib/power/power_common.c                      |   2 +-
  lib/power/power_common.h                      |  16 +-
  lib/power/rte_power.c                         | 287 ++++++------------
  lib/power/rte_power.h                         | 139 ++++++---
  lib/power/rte_power_core_ops.h                | 208 +++++++++++++
  lib/power/version.map                         |  14 +
  26 files changed, 618 insertions(+), 270 deletions(-)

rename lib/power/power_acpi_cpufreq.c => drivers/power/acpi/acpi_cpufreq.c (95%) rename lib/power/power_acpi_cpufreq.h => drivers/power/acpi/acpi_cpufreq.h (98%) create mode 100644 drivers/power/acpi/meson.build
rename lib/power/power_amd_pstate_cpufreq.c => drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%) rename lib/power/power_amd_pstate_cpufreq.h => drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%) create mode 100644 drivers/power/amd_pstate/meson.build
rename lib/power/power_cppc_cpufreq.c => drivers/power/cppc/cppc_cpufreq.c (95%) rename lib/power/power_cppc_cpufreq.h => drivers/power/cppc/cppc_cpufreq.h (97%) create mode 100644 drivers/power/cppc/meson.build
rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%)
rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%)

Quoted 8 lines

rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c (82%) rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h (98%) create mode 100644 drivers/power/kvm_vm/meson.build
create mode 100644 drivers/power/meson.build
create mode 100644 drivers/power/pstate/meson.build
rename lib/power/power_pstate_cpufreq.c => drivers/power/pstate/pstate_cpufreq.c (96%) rename lib/power/power_pstate_cpufreq.h => drivers/power/pstate/pstate_cpufreq.h (98%) create mode 100644 lib/power/rte_power_core_ops.h

--snip--

Quoted 8 lines
lib/power/rte_power.c +0 -0
index 36c3f3da98..8afb5949b9 100644
@@ -8,153 +8,86 @@
  #include <rte_spinlock.h>
 
  #include "rte_power.h"

lib/power/rte_power.c line 14

Quoted 10 lines
lib/power/rte_power.c +2 -6
-#include "power_acpi_cpufreq.h"
-#include "power_cppc_cpufreq.h"
  #include "power_common.h"
-#include "power_kvm_vm.h"
-#include "power_pstate_cpufreq.h"
-#include "power_amd_pstate_cpufreq.h"
 
-enum power_management_env global_default_env = PM_ENV_NOT_SET;
+static enum power_management_env global_default_env = PM_ENV_NOT_SET;
+static struct rte_power_core_ops *global_power_core_ops;

Suggest initialising this to NULL so we can check in rte_power_get_core_ops if it's null and throw an error.

--snip--

lib/power/rte_power.c rte_power_unset_env(void) · line 112

Quoted 3 lines
lib/power/rte_power.c +3 -0
+struct rte_power_core_ops *
+rte_power_get_core_ops(void)
+{

Need a check here to see if rte_power_get_core_ops is NULL. If it is, then the developer has probably called a frequency change API before the relevant init function, so throw an error.

Also, all the functions that call this need to check if it returns NULL so as to avoid a segfault when they attempts to call the op function.

lib/power/rte_power.c rte_power_unset_env(void) · line 115

Quoted 3 lines
lib/power/rte_power.c +3 -0
+	return global_power_core_ops;
+}
+

--snip--

Quoted 55 lines
lib/power/rte_power.h +20 -3
index 4fa4afe399..5e4aacf08b 100644
@@ -1,5 +1,6 @@
  /* SPDX-License-Identifier: BSD-3-Clause
   * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
   */
 
  #ifndef _RTE_POWER_H
@@ -14,14 +15,21 @@
  #include <rte_log.h>
  #include <rte_power_guest_channel.h>
 
+#include "rte_power_core_ops.h"
+
  #ifdef __cplusplus
  extern "C" {
  #endif
 
  /* Power Management Environment State */
-enum power_management_env {PM_ENV_NOT_SET, PM_ENV_ACPI_CPUFREQ, PM_ENV_KVM_VM,
-		PM_ENV_PSTATE_CPUFREQ, PM_ENV_CPPC_CPUFREQ,
-		PM_ENV_AMD_PSTATE_CPUFREQ};
+enum power_management_env {
+	PM_ENV_NOT_SET = 0,
+	PM_ENV_ACPI_CPUFREQ,
+	PM_ENV_KVM_VM,
+	PM_ENV_PSTATE_CPUFREQ,
+	PM_ENV_CPPC_CPUFREQ,
+	PM_ENV_AMD_PSTATE_CPUFREQ
+};
 
  /**
   * Check if a specific power management environment type is supported on a
@@ -66,6 +74,15 @@ void rte_power_unset_env(void);
   */
  enum power_management_env rte_power_get_env(void);
 
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_core_ops *
+rte_power_get_core_ops(void);
+
  /**
   * Initialize power management for a specific lcore. If rte_power_set_env() has
   * not been called then an auto-detect of the environment will start and
@@ -108,10 +125,13 @@ int rte_power_exit(unsigned int lcore_id);
   * @return
   *  The number of available frequencies.

lib/power/rte_power.h int rte_power_exit(unsigned int lcore_id); · line 133

Quoted 10 lines
lib/power/rte_power.h +5 -3
   */
-typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
-		uint32_t num);
+static inline uint32_t
+rte_power_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t n)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_freqs_t rte_power_freqs;
+	return ops->get_avail_freqs(lcore_id, freqs, n);

This function will segfault if is called before the appropriate init is performed. See comments above on global_power_core_ops.

Same for all the functions below that call global_power_core_ops().

Quoted 165 lines
lib/power/rte_power.h +63 -39
+}
 
  /**
   * Return the current index of available frequencies of a specific lcore.
@@ -124,9 +144,13 @@ extern rte_power_freqs_t rte_power_freqs;
   * @return
   *  The current index of available frequencies.
   */
-typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);
+static inline uint32_t
+rte_power_get_freq(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_get_freq_t rte_power_get_freq;
+	return ops->get_freq(lcore_id);
+}
 
  /**
   * Set the new frequency for a specific lcore by indicating the index of
@@ -144,82 +168,101 @@ extern rte_power_get_freq_t rte_power_get_freq;
   *  - 0 on success without frequency changed.
   *  - Negative on error.
   */
-typedef int (*rte_power_set_freq_t)(unsigned int lcore_id, uint32_t index);
-
-extern rte_power_set_freq_t rte_power_set_freq;
+static inline uint32_t
+rte_power_set_freq(unsigned int lcore_id, uint32_t index)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-/**
- * Function pointer definition for generic frequency change functions. Review
- * each environments specific documentation for usage.
- *
- * @param lcore_id
- *  lcore id.
- *
- * @return
- *  - 1 on success with frequency changed.
- *  - 0 on success without frequency changed.
- *  - Negative on error.
- */
-typedef int (*rte_power_freq_change_t)(unsigned int lcore_id);
+	return ops->set_freq(lcore_id, index);
+}
 
  /**
   * Scale up the frequency of a specific lcore according to the available
   * frequencies.
   * Review each environments specific documentation for usage.
   */
-extern rte_power_freq_change_t rte_power_freq_up;
+static inline int
+rte_power_freq_up(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+	return ops->freq_up(lcore_id);
+}
 
  /**
   * Scale down the frequency of a specific lcore according to the available
   * frequencies.
   * Review each environments specific documentation for usage.
   */
-extern rte_power_freq_change_t rte_power_freq_down;
+static inline int
+rte_power_freq_down(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+	return ops->freq_down(lcore_id);
+}
 
  /**
   * Scale up the frequency of a specific lcore to the highest according to the
   * available frequencies.
   * Review each environments specific documentation for usage.
   */
-extern rte_power_freq_change_t rte_power_freq_max;
+static inline int
+rte_power_freq_max(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+	return ops->freq_max(lcore_id);
+}
 
  /**
   * Scale down the frequency of a specific lcore to the lowest according to the
   * available frequencies.
   * Review each environments specific documentation for usage..
   */
-extern rte_power_freq_change_t rte_power_freq_min;
+static inline int
+rte_power_freq_min(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+	return ops->freq_min(lcore_id);
+}
 
  /**
   * Query the Turbo Boost status of a specific lcore.
   * Review each environments specific documentation for usage..
   */
-extern rte_power_freq_change_t rte_power_turbo_status;
+static inline int
+rte_power_turbo_status(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+	return ops->turbo_status(lcore_id);
+}
 
  /**
   * Enable Turbo Boost for this lcore.
   * Review each environments specific documentation for usage..
   */
-extern rte_power_freq_change_t rte_power_freq_enable_turbo;
+static inline int
+rte_power_freq_enable_turbo(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+	return ops->enable_turbo(lcore_id);
+}
 
  /**
   * Disable Turbo Boost for this lcore.
   * Review each environments specific documentation for usage..
   */
-extern rte_power_freq_change_t rte_power_freq_disable_turbo;
+static inline int
+rte_power_freq_disable_turbo(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-/**
- * Power capabilities summary.
- */
-struct rte_power_core_capabilities {
-	union {
-		uint64_t capabilities;
-		struct {
-			uint64_t turbo:1;	/**< Turbo can be enabled. */
-			uint64_t priority:1;	/**< SST-BF high freq core */
-		};
-	};
-};
+	return ops->disable_turbo(lcore_id);
+}
 
  /**
   * Returns power capabilities for a specific lcore.
@@ -235,10 +278,14 @@ struct rte_power_core_capabilities {
   *  - 0 on success.
   *  - Negative on error.
   */
-typedef int (*rte_power_get_capabilities_t)(unsigned int lcore_id,
-		struct rte_power_core_capabilities *caps);
+static inline int
+rte_power_get_capabilities(unsigned int lcore_id,

lib/power/rte_power.h struct rte_power_core_capabilities { · line 291

Quoted 10 lines
lib/power/rte_power.h +5 -1
+		struct rte_power_core_capabilities *caps)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_get_capabilities_t rte_power_get_capabilities;
+	return ops->get_caps(lcore_id, caps);
+}
 
  #ifdef __cplusplus
  }

--snip--

#15 lore raw

RE: [PATCH v1 1/4] power: refactor core power management library

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Dave,

From: Hunt, David <[email protected]>
Sent: Tuesday, July 23, 2024 3:34 PM
To: Tummala, Sivaprasad <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v1 1/4] power: refactor core power management library

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Hi Sivaprasad,

A couple of comments below:
On 20/07/2024 17:50, Sivaprasad Tummala wrote:

This patch introduces a comprehensive refactor to the core power

management library. The primary focus is on improving modularity

and organization by relocating specific driver implementations

from the 'lib/power' directory to dedicated directories within

'drivers/power/core/*'. The adjustment of meson.build files

enables the selective activation of individual drivers.

These changes contribute to a significant enhancement in code

organization, providing a clearer structure for driver implementations.

The refactor aims to improve overall code clarity and boost

maintainability. Additionally, it establishes a foundation for

future development, allowing for more focused work on individual

drivers and seamless integration of forthcoming enhancements.

---

 drivers/meson.build                           |   1 +
 .../power/acpi/acpi_cpufreq.c                 |  22 +-
 .../power/acpi/acpi_cpufreq.h                 |   6 +-
 drivers/power/acpi/meson.build                |  10 +
 .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
 .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
 drivers/power/amd_pstate/meson.build          |  10 +
 .../power/cppc/cppc_cpufreq.c                 |  22 +-
 .../power/cppc/cppc_cpufreq.h                 |   8 +-
 drivers/power/cppc/meson.build                |  10 +
 .../power/kvm_vm}/guest_channel.c             |   0
 .../power/kvm_vm}/guest_channel.h             |   0
 .../power/kvm_vm/kvm_vm.c                     |  22 +-
 .../power/kvm_vm/kvm_vm.h                     |   6 +-
 drivers/power/kvm_vm/meson.build              |  16 +
 drivers/power/meson.build                     |  12 +
 drivers/power/pstate/meson.build              |  10 +
 .../power/pstate/pstate_cpufreq.c             |  22 +-
 .../power/pstate/pstate_cpufreq.h             |   6 +-
 lib/power/meson.build                         |   7 +-
 lib/power/power_common.c                      |   2 +-
 lib/power/power_common.h                      |  16 +-
 lib/power/rte_power.c                         | 287 ++++++------------
 lib/power/rte_power.h                         | 139 ++++++---
 lib/power/rte_power_core_ops.h                | 208 +++++++++++++
 lib/power/version.map                         |  14 +
 26 files changed, 618 insertions(+), 270 deletions(-)
 rename lib/power/power_acpi_cpufreq.c => drivers/power/acpi/acpi_cpufreq.c (95%)
 rename lib/power/power_acpi_cpufreq.h => drivers/power/acpi/acpi_cpufreq.h (98%)
 create mode 100644 drivers/power/acpi/meson.build
 rename lib/power/power_amd_pstate_cpufreq.c => drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%)
 rename lib/power/power_amd_pstate_cpufreq.h => drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%)
 create mode 100644 drivers/power/amd_pstate/meson.build
 rename lib/power/power_cppc_cpufreq.c => drivers/power/cppc/cppc_cpufreq.c (95%)
 rename lib/power/power_cppc_cpufreq.h => drivers/power/cppc/cppc_cpufreq.h (97%)
 create mode 100644 drivers/power/cppc/meson.build
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%)
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%)
 rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c (82%)
 rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h (98%)
 create mode 100644 drivers/power/kvm_vm/meson.build
 create mode 100644 drivers/power/meson.build
 create mode 100644 drivers/power/pstate/meson.build
 rename lib/power/power_pstate_cpufreq.c => drivers/power/pstate/pstate_cpufreq.c (96%)
 rename lib/power/power_pstate_cpufreq.h => drivers/power/pstate/pstate_cpufreq.h (98%)
 create mode 100644 lib/power/rte_power_core_ops.h

--snip--

lib/power/rte_power.c +0 -0

index 36c3f3da98..8afb5949b9 100644

--- a/lib/power/rte_power.c

+++ b/lib/power/rte_power.c

@@ -8,153 +8,86 @@

#include <rte_spinlock.h>

#include "rte_power.h"

-#include "power_acpi_cpufreq.h"

-#include "power_cppc_cpufreq.h"

#include "power_common.h"

-#include "power_kvm_vm.h"

-#include "power_pstate_cpufreq.h"

-#include "power_amd_pstate_cpufreq.h"

-enum power_management_env global_default_env = PM_ENV_NOT_SET;

+static enum power_management_env global_default_env = PM_ENV_NOT_SET;

+static struct rte_power_core_ops *global_power_core_ops;

Suggest initialising this to NULL so we can check in rte_power_get_core_ops if it's null and throw an error.

[Siva] rte_power_core_ops as static global is initialized to NULL at runtime. Not sure, if it’s still required to initialize to NULL.

--snip--

+struct rte_power_core_ops *

+rte_power_get_core_ops(void)

+{

Need a check here to see if rte_power_get_core_ops is NULL. If it is, then the developer has probably called a frequency change API before the relevant init function, so throw an error.

Also, all the functions that call this need to check if it returns NULL so as to avoid a segfault when they attempts to call the op function.

[Siva] ACK. Will fix this in next version.

+ return global_power_core_ops;

+}

+

--snip--

lib/power/rte_power.h +0 -0

index 4fa4afe399..5e4aacf08b 100644

--- a/lib/power/rte_power.h

+++ b/lib/power/rte_power.h

@@ -1,5 +1,6 @@

/* SPDX-License-Identifier: BSD-3-Clause

* Copyright(c) 2010-2014 Intel Corporation

+ * Copyright(c) 2024 Advanced Micro Devices, Inc.

*/

#ifndef _RTE_POWER_H

@@ -14,14 +15,21 @@

#include <rte_log.h>

#include <rte_power_guest_channel.h>

+#include "rte_power_core_ops.h"

+

#ifdef __cplusplus

extern "C" {

#endif

/* Power Management Environment State */

-enum power_management_env {PM_ENV_NOT_SET, PM_ENV_ACPI_CPUFREQ, PM_ENV_KVM_VM,

- PM_ENV_PSTATE_CPUFREQ, PM_ENV_CPPC_CPUFREQ,

- PM_ENV_AMD_PSTATE_CPUFREQ};

+enum power_management_env {

+ PM_ENV_NOT_SET = 0,

+ PM_ENV_ACPI_CPUFREQ,

+ PM_ENV_KVM_VM,

+ PM_ENV_PSTATE_CPUFREQ,

+ PM_ENV_CPPC_CPUFREQ,

+ PM_ENV_AMD_PSTATE_CPUFREQ

+};

/**

* Check if a specific power management environment type is supported on a

@@ -66,6 +74,15 @@ void rte_power_unset_env(void);

*/

enum power_management_env rte_power_get_env(void);

+/**

+ * @internal Get the power ops struct from its index.

+ *

+ * @return

+ * The pointer to the ops struct in the table if registered.

+ */

+struct rte_power_core_ops *

+rte_power_get_core_ops(void);

+

/**

* Initialize power management for a specific lcore. If rte_power_set_env() has

* not been called then an auto-detect of the environment will start and

@@ -108,10 +125,13 @@ int rte_power_exit(unsigned int lcore_id);

* @return

* The number of available frequencies.

*/

-typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,

- uint32_t num);

+static inline uint32_t

+rte_power_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t n)

+{

+ struct rte_power_core_ops *ops = rte_power_get_core_ops();

-extern rte_power_freqs_t rte_power_freqs;

+ return ops->get_avail_freqs(lcore_id, freqs, n);

This function will segfault if is called before the appropriate init is performed. See comments above on global_power_core_ops.

Same for all the functions below that call global_power_core_ops().

+}

/**

* Return the current index of available frequencies of a specific lcore.

@@ -124,9 +144,13 @@ extern rte_power_freqs_t rte_power_freqs;

* @return

* The current index of available frequencies.

*/

-typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);

+static inline uint32_t

+rte_power_get_freq(unsigned int lcore_id)

+{

+ struct rte_power_core_ops *ops = rte_power_get_core_ops();

-extern rte_power_get_freq_t rte_power_get_freq;

+ return ops->get_freq(lcore_id);

+}

/**

* Set the new frequency for a specific lcore by indicating the index of

@@ -144,82 +168,101 @@ extern rte_power_get_freq_t rte_power_get_freq;

* - 0 on success without frequency changed.

* - Negative on error.

*/

-typedef int (*rte_power_set_freq_t)(unsigned int lcore_id, uint32_t index);

-

-extern rte_power_set_freq_t rte_power_set_freq;

+static inline uint32_t

+rte_power_set_freq(unsigned int lcore_id, uint32_t index)

+{

+ struct rte_power_core_ops *ops = rte_power_get_core_ops();

-/**

- * Function pointer definition for generic frequency change functions. Review

- * each environments specific documentation for usage.

- *

- * @param lcore_id

- * lcore id.

- *

- * @return

- * - 1 on success with frequency changed.

- * - 0 on success without frequency changed.

- * - Negative on error.

- */

-typedef int (*rte_power_freq_change_t)(unsigned int lcore_id);

+ return ops->set_freq(lcore_id, index);

+}

/**

* Scale up the frequency of a specific lcore according to the available

* frequencies.

* Review each environments specific documentation for usage.

*/

-extern rte_power_freq_change_t rte_power_freq_up;

+static inline int

+rte_power_freq_up(unsigned int lcore_id)

+{

+ struct rte_power_core_ops *ops = rte_power_get_core_ops();

+

+ return ops->freq_up(lcore_id);

+}

/**

* Scale down the frequency of a specific lcore according to the available

* frequencies.

* Review each environments specific documentation for usage.

*/

-extern rte_power_freq_change_t rte_power_freq_down;

+static inline int

+rte_power_freq_down(unsigned int lcore_id)

+{

+ struct rte_power_core_ops *ops = rte_power_get_core_ops();

+

+ return ops->freq_down(lcore_id);

+}

/**

* Scale up the frequency of a specific lcore to the highest according to the

* available frequencies.

* Review each environments specific documentation for usage.

*/

-extern rte_power_freq_change_t rte_power_freq_max;

+static inline int

+rte_power_freq_max(unsigned int lcore_id)

+{

+ struct rte_power_core_ops *ops = rte_power_get_core_ops();

+

+ return ops->freq_max(lcore_id);

+}

/**

* Scale down the frequency of a specific lcore to the lowest according to the

* available frequencies.

* Review each environments specific documentation for usage..

*/

-extern rte_power_freq_change_t rte_power_freq_min;

+static inline int

+rte_power_freq_min(unsigned int lcore_id)

+{

+ struct rte_power_core_ops *ops = rte_power_get_core_ops();

+

+ return ops->freq_min(lcore_id);

+}

/**

* Query the Turbo Boost status of a specific lcore.

* Review each environments specific documentation for usage..

*/

-extern rte_power_freq_change_t rte_power_turbo_status;

+static inline int

+rte_power_turbo_status(unsigned int lcore_id)

+{

+ struct rte_power_core_ops *ops = rte_power_get_core_ops();

+

+ return ops->turbo_status(lcore_id);

+}

/**

* Enable Turbo Boost for this lcore.

* Review each environments specific documentation for usage..

*/

-extern rte_power_freq_change_t rte_power_freq_enable_turbo;

+static inline int

+rte_power_freq_enable_turbo(unsigned int lcore_id)

+{

+ struct rte_power_core_ops *ops = rte_power_get_core_ops();

+

+ return ops->enable_turbo(lcore_id);

+}

/**

* Disable Turbo Boost for this lcore.

* Review each environments specific documentation for usage..

*/

-extern rte_power_freq_change_t rte_power_freq_disable_turbo;

+static inline int

+rte_power_freq_disable_turbo(unsigned int lcore_id)

+{

+ struct rte_power_core_ops *ops = rte_power_get_core_ops();

-/**

- * Power capabilities summary.

- */

-struct rte_power_core_capabilities {

- union {

- uint64_t capabilities;

- struct {

- uint64_t turbo:1; /**< Turbo can be enabled. */

- uint64_t priority:1; /**< SST-BF high freq core */

- };

- };

-};

+ return ops->disable_turbo(lcore_id);

+}

/**

* Returns power capabilities for a specific lcore.

@@ -235,10 +278,14 @@ struct rte_power_core_capabilities {

* - 0 on success.

* - Negative on error.

*/

-typedef int (*rte_power_get_capabilities_t)(unsigned int lcore_id,

- struct rte_power_core_capabilities *caps);

+static inline int

+rte_power_get_capabilities(unsigned int lcore_id,

+ struct rte_power_core_capabilities *caps)

+{

+ struct rte_power_core_ops *ops = rte_power_get_core_ops();

-extern rte_power_get_capabilities_t rte_power_get_capabilities;

+ return ops->get_caps(lcore_id, caps);

+}

#ifdef __cplusplus

}

--snip--

#16 lore raw

PATCH 2/4 power: refactor uncore power management library

In reply to Sivaprasad Tummala

This patch refactors the power management library, addressing uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/uncore/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

This refactor significantly improves code organization, enhances clarity and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

---
 .../power/intel_uncore/intel_uncore.c         |  18 +-
 .../power/intel_uncore/intel_uncore.h         |   8 +-
 drivers/power/intel_uncore/meson.build        |   7 +
 drivers/power/meson.build                     |   3 +-
 lib/power/meson.build                         |   2 +-
 lib/power/rte_power_uncore.c                  | 206 ++++++---------
 lib/power/rte_power_uncore.h                  |  91 ++++---
 lib/power/rte_power_uncore_ops.h              | 239 ++++++++++++++++++
 lib/power/version.map                         |   1 +
 9 files changed, 406 insertions(+), 169 deletions(-)
 rename lib/power/power_intel_uncore.c => drivers/power/intel_uncore/intel_uncore.c (95%)
 rename lib/power/power_intel_uncore.h => drivers/power/intel_uncore/intel_uncore.h (97%)
 create mode 100644 drivers/power/intel_uncore/meson.build
 create mode 100644 lib/power/rte_power_uncore_ops.h
drivers/power/intel_uncore/intel_uncore.c renamed from lib/power/power_intel_uncore.c +17 -1
similarity index 95%
rename from lib/power/power_intel_uncore.c
rename to drivers/power/intel_uncore/intel_uncore.c
index 9c152e4ed2..6f3b347a8d 100644
@@ -8,7 +8,7 @@
 
 #include <rte_memcpy.h>
 
-#include "power_intel_uncore.h"
+#include "intel_uncore.h"
 #include "power_common.h"
 
 #define MAX_UNCORE_FREQS 32
@@ -476,3 +476,19 @@ power_intel_uncore_get_num_dies(unsigned int pkg)
 
 	return count;
 }
+
+static struct rte_power_uncore_ops intel_uncore_ops = {
+	.name = "intel-uncore",
+	.init = power_intel_uncore_init,
+	.exit = power_intel_uncore_exit,
+	.get_avail_freqs = power_intel_uncore_freqs,
+	.get_num_pkgs = power_intel_uncore_get_num_pkgs,
+	.get_num_dies = power_intel_uncore_get_num_dies,
+	.get_num_freqs = power_intel_uncore_get_num_freqs,
+	.get_freq = power_get_intel_uncore_freq,
+	.set_freq = power_set_intel_uncore_freq,
+	.freq_max = power_intel_uncore_freq_max,
+	.freq_min = power_intel_uncore_freq_min,
+};
+
+RTE_POWER_REGISTER_UNCORE_OPS(intel_uncore_ops);
drivers/power/intel_uncore/intel_uncore.h renamed from lib/power/power_intel_uncore.h +4 -4
similarity index 97%
rename from lib/power/power_intel_uncore.h
rename to drivers/power/intel_uncore/intel_uncore.h
index 20a3ba8ebe..f2ce2f0c66 100644
@@ -2,8 +2,8 @@
  * Copyright(c) 2022 Intel Corporation
  */
 
-#ifndef POWER_INTEL_UNCORE_H
-#define POWER_INTEL_UNCORE_H
+#ifndef INTEL_UNCORE_H
+#define INTEL_UNCORE_H
 
 /**
  * @file
@@ -11,7 +11,7 @@
  */
 
 #include "rte_power.h"
-#include "rte_power_uncore.h"
+#include "rte_power_uncore_ops.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -223,4 +223,4 @@ power_intel_uncore_get_num_dies(unsigned int pkg);
 }
 #endif
 
-#endif /* POWER_INTEL_UNCORE_H */
+#endif /* INTEL_UNCORE_H */
drivers/power/intel_uncore/meson.build new file +7 -0
new file mode 100644
index 0000000000..c46202fd6a
@@ -0,0 +1,7 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2017 Intel Corporation
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+sources = files('intel_uncore.c')
+
+deps += ['power']
drivers/power/meson.build +2 -1
index 8c7215c639..c83047af94 100644
@@ -6,7 +6,8 @@ drivers = [
         'amd_pstate',
         'cppc',
         'kvm_vm',
-        'pstate'
+        'pstate',
+        'intel_uncore'
 ]
 
 std_deps = ['power']
lib/power/meson.build +1 -1
index f3e3451cdc..9b13d98810 100644
@@ -13,7 +13,6 @@ if not is_linux
 endif
 sources = files(
         'power_common.c',
-        'power_intel_uncore.c',
         'rte_power.c',
         'rte_power_uncore.c',
         'rte_power_pmd_mgmt.c',
@@ -24,6 +23,7 @@ headers = files(
         'rte_power_guest_channel.h',
         'rte_power_pmd_mgmt.h',
         'rte_power_uncore.h',
+        'rte_power_uncore_ops.h',
 )
 if cc.has_argument('-Wno-cast-qual')
     cflags += '-Wno-cast-qual'
lib/power/rte_power_uncore.c +82 -124

267 lines not on this page · raw message

lib/power/rte_power_uncore.h +53 -38

156 lines not on this page · raw message

lib/power/rte_power_uncore_ops.h new file +239 -0

239 lines not on this page · raw message

lib/power/version.map +1 -0
index c2098fd667..112790df73 100644
@@ -59,6 +59,7 @@ INTERNAL {
 	global:
 
 	rte_power_register_ops;
+	rte_power_register_uncore_ops;
 	cpufreq_check_scaling_driver;
 	power_set_governor;
 	open_core_sysfs_file;
Signature · 1 line
2.34.1
#17 lore raw

Re: [PATCH v1 2/4] power: refactor uncore power management library

Quoted 23 lines · Sivaprasad Tummala

This patch refactors the power management library, addressing uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/uncore/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

This refactor significantly improves code organization, enhances clarity and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

---
  .../power/intel_uncore/intel_uncore.c         |  18 +-
  .../power/intel_uncore/intel_uncore.h         |   8 +-
  drivers/power/intel_uncore/meson.build        |   7 +
  drivers/power/meson.build                     |   3 +-
  lib/power/meson.build                         |   2 +-
  lib/power/rte_power_uncore.c                  | 206 ++++++---------
  lib/power/rte_power_uncore.h                  |  91 ++++---
  lib/power/rte_power_uncore_ops.h              | 239 ++++++++++++++++++
  lib/power/version.map                         |   1 +
  9 files changed, 406 insertions(+), 169 deletions(-)
Quoted 5 lines

rename lib/power/power_intel_uncore.c => drivers/power/intel_uncore/intel_uncore.c (95%) rename lib/power/power_intel_uncore.h => drivers/power/intel_uncore/intel_uncore.h (97%) create mode 100644 drivers/power/intel_uncore/meson.build
create mode 100644 lib/power/rte_power_uncore_ops.h

--snip--

lib/power/rte_power_uncore.c line 5

Quoted 12 lines
lib/power/rte_power_uncore.c +1 -1
index 48c75a5da0..127f6ed212 100644
@@ -1,6 +1,6 @@
  /* SPDX-License-Identifier: BSD-3-Clause
   * Copyright(c) 2010-2014 Intel Corporation
- * Copyright(c) 2023 AMD Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
   */
 

--snip--

lib/power/rte_power_uncore.c void · line 105

Quoted 4 lines
lib/power/rte_power_uncore.c +4 -0
+struct rte_power_uncore_ops *
+rte_power_get_uncore_ops(void)
+{
+	RTE_ASSERT(global_uncore_ops != NULL);

I'm only seeing this now after sending the email for the first patch. This would be a good solution for the global_core_ops check in rte_power_get_core_ops() in rte_power.c,  and would be the smaller change, rather than checking everywhere rte_power_get_env() is called.

lib/power/rte_power_uncore.c void · line 108

Quoted 4 lines
lib/power/rte_power_uncore.c +2 -0
+
+	return global_uncore_ops;
  }

--snip--

#18 lore raw

PATCH 3/4 test/power: removed function pointer validations

In reply to Sivaprasad Tummala

After refactoring the power library, power management operations are now consistently supported regardless of the operating environment, making function pointer checks unnecessary and thus removed from applications.

---
 app/test/test_power.c         | 95 -----------------------------------
 app/test/test_power_cpufreq.c | 52 -------------------
 app/test/test_power_kvm_vm.c  | 36 -------------
 3 files changed, 183 deletions(-)
app/test/test_power.c +0 -95
index 403adc22d6..5df5848c70 100644
@@ -24,86 +24,6 @@ test_power(void)
 
 #include <rte_power.h>
 
-static int
-check_function_ptrs(void)
-{
-	enum power_management_env env = rte_power_get_env();
-
-	const bool not_null_expected = !(env == PM_ENV_NOT_SET);
-
-	const char *inject_not_string1 = not_null_expected ? " not" : "";
-	const char *inject_not_string2 = not_null_expected ? "" : " not";
-
-	if ((rte_power_freqs == NULL) == not_null_expected) {
-		printf("rte_power_freqs should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-					inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_get_freq == NULL) == not_null_expected) {
-		printf("rte_power_get_freq should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-					inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_set_freq == NULL) == not_null_expected) {
-		printf("rte_power_set_freq should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_freq_up == NULL) == not_null_expected) {
-		printf("rte_power_freq_up should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_freq_down == NULL) == not_null_expected) {
-		printf("rte_power_freq_down should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_freq_max == NULL) == not_null_expected) {
-		printf("rte_power_freq_max should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_freq_min == NULL) == not_null_expected) {
-		printf("rte_power_freq_min should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_turbo_status == NULL) == not_null_expected) {
-		printf("rte_power_turbo_status should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_freq_enable_turbo == NULL) == not_null_expected) {
-		printf("rte_power_freq_enable_turbo should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_freq_disable_turbo == NULL) == not_null_expected) {
-		printf("rte_power_freq_disable_turbo should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_get_capabilities == NULL) == not_null_expected) {
-		printf("rte_power_get_capabilities should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-
-	return 0;
-}
-
 static int
 test_power(void)
 {
@@ -124,10 +44,6 @@ test_power(void)
 		return -1;
 	}
 
-	/* Verify that function pointers are NULL */
-	if (check_function_ptrs() < 0)
-		goto fail_all;
-
 	rte_power_unset_env();
 
 	/* Perform tests for valid environments.*/
@@ -154,22 +70,11 @@ test_power(void)
 			return -1;
 		}
 
-		/* Verify that function pointers are NOT NULL */
-		if (check_function_ptrs() < 0)
-			goto fail_all;
-
 		rte_power_unset_env();
 
-		/* Verify that function pointers are NULL */
-		if (check_function_ptrs() < 0)
-			goto fail_all;
-
 	}
 
 	return 0;
-fail_all:
-	rte_power_unset_env();
-	return -1;
 }
 #endif
 
app/test/test_power_cpufreq.c +0 -52

58 lines not on this page · raw message

app/test/test_power_kvm_vm.c +0 -36
index 464e06002e..a7d104e973 100644
@@ -47,42 +47,6 @@ test_power_kvm_vm(void)
 		return -1;
 	}
 
-	/* verify that function pointers are not NULL */
-	if (rte_power_freqs == NULL) {
-		printf("rte_power_freqs should not be NULL, environment has not been "
-				"initialised\n");
-		return -1;
-	}
-	if (rte_power_get_freq == NULL) {
-		printf("rte_power_get_freq should not be NULL, environment has not "
-				"been initialised\n");
-		return -1;
-	}
-	if (rte_power_set_freq == NULL) {
-		printf("rte_power_set_freq should not be NULL, environment has not "
-				"been initialised\n");
-		return -1;
-	}
-	if (rte_power_freq_up == NULL) {
-		printf("rte_power_freq_up should not be NULL, environment has not "
-				"been initialised\n");
-		return -1;
-	}
-	if (rte_power_freq_down == NULL) {
-		printf("rte_power_freq_down should not be NULL, environment has not "
-				"been initialised\n");
-		return -1;
-	}
-	if (rte_power_freq_max == NULL) {
-		printf("rte_power_freq_max should not be NULL, environment has not "
-				"been initialised\n");
-		return -1;
-	}
-	if (rte_power_freq_min == NULL) {
-		printf("rte_power_freq_min should not be NULL, environment has not "
-				"been initialised\n");
-		return -1;
-	}
 	/* Test initialisation of an out of bounds lcore */
 	ret = rte_power_init(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
 	if (ret != -1) {
Signature · 1 line
2.34.1
#19 lore raw

Re: [PATCH v1 3/4] test/power: removed function pointer validations

Quoted 11 lines · Sivaprasad Tummala

After refactoring the power library, power management operations are now consistently supported regardless of the operating environment, making function pointer checks unnecessary and thus removed from applications.

---
  app/test/test_power.c         | 95 -----------------------------------
  app/test/test_power_cpufreq.c | 52 -------------------
  app/test/test_power_kvm_vm.c  | 36 -------------
  3 files changed, 183 deletions(-)

Hi Sivaprasad,

    Nice work on the patch-set.

There's just four function pointer checks remaining that my compiler is complaining about. They are in examples/l3fwd-power/main.c (lines 443, 452, 1350, 1353). It would be nice to have these removed as well, seeing as the functions are now inlines and don't need these checks.

I'm running the patch set through some tests here, will keep you posted on progress.

Rgds,
Dave.

---snip---

#20 lore raw

RE: [PATCH v1 3/4] test/power: removed function pointer validations

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Dave,

Inline..

From: Hunt, David <[email protected]>
Sent: Monday, July 22, 2024 4:20 PM
To: Tummala, Sivaprasad <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v1 3/4] test/power: removed function pointer validations

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

On 20/07/2024 17:50, Sivaprasad Tummala wrote:

After refactoring the power library, power management operations are now

consistently supported regardless of the operating environment, making

function pointer checks unnecessary and thus removed from applications.

---

 app/test/test_power.c         | 95 -----------------------------------
 app/test/test_power_cpufreq.c | 52 -------------------
 app/test/test_power_kvm_vm.c  | 36 -------------
 3 files changed, 183 deletions(-)

Hi Sivaprasad,

Nice work on the patch-set.

There's just four function pointer checks remaining that my compiler is complaining about. They are in examples/l3fwd-power/main.c (lines 443, 452, 1350, 1353). It would be nice to have these removed as well, seeing as the functions are now inlines and don't need these checks.

[Siva] ACK. Will fix this in next version.

I'm running the patch set through some tests here, will keep you posted on progress.

Rgds,
Dave.

---snip---

#21 lore raw

PATCH 4/4 power/amd_uncore: uncore power management support for AMD EPYC processors

In reply to Sivaprasad Tummala

This patch introduces driver support for power management of uncore components in AMD EPYC processors.

---
 drivers/power/amd_uncore/amd_uncore.c | 321 ++++++++++++++++++++++++++
 drivers/power/amd_uncore/amd_uncore.h | 226 ++++++++++++++++++
 drivers/power/amd_uncore/meson.build  |  20 ++
 drivers/power/meson.build             |   1 +
 4 files changed, 568 insertions(+)
 create mode 100644 drivers/power/amd_uncore/amd_uncore.c
 create mode 100644 drivers/power/amd_uncore/amd_uncore.h
 create mode 100644 drivers/power/amd_uncore/meson.build
drivers/power/amd_uncore/amd_uncore.c new file +321 -0
new file mode 100644
index 0000000000..f15eaaa307
@@ -0,0 +1,321 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
+ */
+
+#include <errno.h>
+#include <dirent.h>
+#include <fnmatch.h>
+
+#include <rte_memcpy.h>
+
+#include "amd_uncore.h"
+#include "power_common.h"
+#include "e_smi/e_smi.h"
+
+#define MAX_UNCORE_FREQS 8
+#define MAX_NUMA_DIE 8
+
+#define BUS_FREQ 1000
+
+struct  __rte_cache_aligned uncore_power_info {
+	unsigned int die;                  /* Core die id */
+	unsigned int pkg;                  /* Package id */
+	uint32_t freqs[MAX_UNCORE_FREQS];  /* Frequency array */
+	uint32_t nb_freqs;                 /* Number of available freqs */
+	uint32_t curr_idx;                 /* Freq index in freqs array */
+	uint32_t max_freq;            /* System max uncore freq */
+	uint32_t min_freq;            /* System min uncore freq */
+};
+
+static struct uncore_power_info uncore_info[RTE_MAX_NUMA_NODES][MAX_NUMA_DIE];
+static int esmi_initialized;
+
+static int
+set_uncore_freq_internal(struct uncore_power_info *ui, uint32_t idx)
+{
+	int ret;
+
+	if (idx >= MAX_UNCORE_FREQS || idx >= ui->nb_freqs) {
+		POWER_LOG(DEBUG, "Invalid uncore frequency index %u, which "
+				"should be less than %u", idx, ui->nb_freqs);
+		return -1;
+	}
+
+	ret = esmi_apb_disable(ui->pkg, idx);
+	if (ret != ESMI_SUCCESS) {
+		POWER_LOG(ERR, "DF P-state '%u' set failed for pkg %02u",
+				idx, ui->pkg);
+		return -1;
+	}
+
+	POWER_DEBUG_LOG("DF P-state '%u' to be set for pkg %02u die %02u",
+				idx, ui->pkg, ui->die);
+
+	/* write the minimum value first if the target freq is less than current max */
+	ui->curr_idx = idx;
+
+	return 0;
+}
+
+/*
+ * Fopen the sys file for the future setting of the uncore die frequency.
+ */
+static int
+power_init_for_setting_uncore_freq(struct uncore_power_info *ui)
+{
+	/* open and read all uncore sys files */
+	/* Base max */
+	ui->max_freq = 1800000;
+	ui->min_freq = 1200000;
+
+	return 0;
+}
+
+/*
+ * Get the available uncore frequencies of the specific die by reading the
+ * sys file.
+ */
+static int
+power_get_available_uncore_freqs(struct uncore_power_info *ui)
+{
+	int ret = -1;
+	uint32_t i, num_uncore_freqs = 3;
+	uint32_t fabric_freqs[] = {
+	/* to be extended for probing support in future */
+		1800,
+		1444,
+		1200
+	};
+
+	if (num_uncore_freqs >= MAX_UNCORE_FREQS) {
+		POWER_LOG(ERR, "Too many available uncore frequencies: %d",
+				num_uncore_freqs);
+		goto out;
+	}
+
+	/* Generate the uncore freq bucket array. */
+	for (i = 0; i < num_uncore_freqs; i++)
+		ui->freqs[i] = fabric_freqs[i] * BUS_FREQ;
+
+	ui->nb_freqs = num_uncore_freqs;
+
+	ret = 0;
+
+	POWER_DEBUG_LOG("%d frequency(s) of pkg %02u die %02u are available",
+			num_uncore_freqs, ui->pkg, ui->die);
+
+out:
+	return ret;
+}
+
+static int
+check_pkg_die_values(unsigned int pkg, unsigned int die)
+{
+	unsigned int max_pkgs, max_dies;
+	max_pkgs = power_amd_uncore_get_num_pkgs();
+	if (max_pkgs == 0)
+		return -1;
+	if (pkg >= max_pkgs) {
+		POWER_LOG(DEBUG, "Package number %02u can not exceed %u",
+				pkg, max_pkgs);
+		return -1;
+	}
+
+	max_dies = power_amd_uncore_get_num_dies(pkg);
+	if (max_dies == 0)
+		return -1;
+	if (die >= max_dies) {
+		POWER_LOG(DEBUG, "Die number %02u can not exceed %u",
+				die, max_dies);
+		return -1;
+	}
+
+	return 0;
+}
+
+static void
+power_amd_uncore_esmi_init(void)
+{
+	if (esmi_init() == ESMI_SUCCESS)
+		esmi_initialized = 1;
+}
+
+int
+power_amd_uncore_init(unsigned int pkg, unsigned int die)
+{
+	struct uncore_power_info *ui;
+	int ret;
+
+	if (!esmi_initialized) {
+		ret = esmi_init();
+		if (ret != ESMI_SUCCESS) {
+			POWER_LOG(DEBUG, "ESMI Not initialized, drivers not found");
+			return -1;
+		}
+	}
+
+	ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	ui = &uncore_info[pkg][die];
+	ui->die = die;
+	ui->pkg = pkg;
+
+	/* Init for setting uncore die frequency */
+	if (power_init_for_setting_uncore_freq(ui) < 0) {
+		POWER_LOG(DEBUG, "Cannot init for setting uncore frequency for "
+				"pkg %02u die %02u", pkg, die);
+		return -1;
+	}
+
+	/* Get the available frequencies */
+	if (power_get_available_uncore_freqs(ui) < 0) {
+		POWER_LOG(DEBUG, "Cannot get available uncore frequencies of "
+				"pkg %02u die %02u", pkg, die);
+		return -1;
+	}
+
+	return 0;
+}
+
+int
+power_amd_uncore_exit(unsigned int pkg, unsigned int die)
+{
+	struct uncore_power_info *ui;
+
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	ui = &uncore_info[pkg][die];
+	ui->nb_freqs = 0;
+
+	if (esmi_initialized) {
+		esmi_exit();
+		esmi_initialized = 0;
+	}
+
+	return 0;
+}
+
+uint32_t
+power_get_amd_uncore_freq(unsigned int pkg, unsigned int die)
+{
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	return uncore_info[pkg][die].curr_idx;
+}
+
+int
+power_set_amd_uncore_freq(unsigned int pkg, unsigned int die, uint32_t index)
+{
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	return set_uncore_freq_internal(&(uncore_info[pkg][die]), index);
+}
+
+int
+power_amd_uncore_freq_max(unsigned int pkg, unsigned int die)
+{
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	return set_uncore_freq_internal(&(uncore_info[pkg][die]), 0);
+}
+
+
+int
+power_amd_uncore_freq_min(unsigned int pkg, unsigned int die)
+{
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	struct uncore_power_info *ui = &uncore_info[pkg][die];
+
+	return set_uncore_freq_internal(&(uncore_info[pkg][die]), ui->nb_freqs - 1);
+}
+
+int
+power_amd_uncore_freqs(unsigned int pkg, unsigned int die, uint32_t *freqs, uint32_t num)
+{
+	struct uncore_power_info *ui;
+
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	if (freqs == NULL) {
+		POWER_LOG(ERR, "NULL buffer supplied");
+		return 0;
+	}
+
+	ui = &uncore_info[pkg][die];
+	if (num < ui->nb_freqs) {
+		POWER_LOG(ERR, "Buffer size is not enough");
+		return 0;
+	}
+	rte_memcpy(freqs, ui->freqs, ui->nb_freqs * sizeof(uint32_t));
+
+	return ui->nb_freqs;
+}
+
+int
+power_amd_uncore_get_num_freqs(unsigned int pkg, unsigned int die)
+{
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	return uncore_info[pkg][die].nb_freqs;
+}
+
+unsigned int
+power_amd_uncore_get_num_pkgs(void)
+{
+	uint32_t num_pkgs = 0;
+	int ret;
+
+	if (esmi_initialized) {
+		ret = esmi_number_of_sockets_get(&num_pkgs);
+		if (ret != ESMI_SUCCESS) {
+			POWER_LOG(ERR, "Failed to get number of sockets");
+			num_pkgs = 0;
+		}
+	}
+	return num_pkgs;
+}
+
+unsigned int
+power_amd_uncore_get_num_dies(unsigned int pkg)
+{
+	if (pkg >= power_amd_uncore_get_num_pkgs()) {
+		POWER_LOG(ERR, "Invalid package ID");
+		return 0;
+	}
+
+	return 1;
+}
+
+static struct rte_power_uncore_ops amd_uncore_ops = {
+	.name = "amd-hsmp",
+	.cb = power_amd_uncore_esmi_init,
+	.init = power_amd_uncore_init,
+	.exit = power_amd_uncore_exit,
+	.get_avail_freqs = power_amd_uncore_freqs,
+	.get_num_pkgs = power_amd_uncore_get_num_pkgs,
+	.get_num_dies = power_amd_uncore_get_num_dies,
+	.get_num_freqs = power_amd_uncore_get_num_freqs,
+	.get_freq = power_get_amd_uncore_freq,
+	.set_freq = power_set_amd_uncore_freq,
+	.freq_max = power_amd_uncore_freq_max,
+	.freq_min = power_amd_uncore_freq_min,
+};
+
+RTE_POWER_REGISTER_UNCORE_OPS(amd_uncore_ops);
drivers/power/amd_uncore/amd_uncore.h new file +226 -0

226 lines not on this page · raw message

drivers/power/amd_uncore/meson.build new file +20 -0
new file mode 100644
index 0000000000..ec1b741c3a
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+IMB_header = '#include<e_smi/e_smi.h>'
+lib = cc.find_library('e_smi64', required: false)
+if not lib.found()
+    build = false
+    reason = 'missing dependency, "libe_smi"'
+else
+    ext_deps += lib
+endif
+
+sources = files('amd_uncore.c')
+deps += ['power']
drivers/power/meson.build +1 -0
index c83047af94..4ba5954e13 100644
@@ -7,6 +7,7 @@ drivers = [
         'cppc',
         'kvm_vm',
         'pstate',
+        'amd_uncore',
         'intel_uncore'
 ]
 
Signature · 1 line
2.34.1
#22 lore raw

Re: [PATCH v1 4/4] power/amd_uncore: uncore power management support for AMD EPYC processors

Quoted 72 lines · Sivaprasad Tummala

This patch introduces driver support for power management of uncore components in AMD EPYC processors.

---
  drivers/power/amd_uncore/amd_uncore.c | 321 ++++++++++++++++++++++++++
  drivers/power/amd_uncore/amd_uncore.h | 226 ++++++++++++++++++
  drivers/power/amd_uncore/meson.build  |  20 ++
  drivers/power/meson.build             |   1 +
  4 files changed, 568 insertions(+)

create mode 100644 drivers/power/amd_uncore/amd_uncore.c create mode 100644 drivers/power/amd_uncore/amd_uncore.h create mode 100644 drivers/power/amd_uncore/meson.build

drivers/power/amd_uncore/amd_uncore.c new file +52 -0
new file mode 100644
index 0000000000..f15eaaa307
@@ -0,0 +1,321 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
+ */
+
+#include <errno.h>
+#include <dirent.h>
+#include <fnmatch.h>
+
+#include <rte_memcpy.h>
+
+#include "amd_uncore.h"
+#include "power_common.h"
+#include "e_smi/e_smi.h"
+
+#define MAX_UNCORE_FREQS 8
+#define MAX_NUMA_DIE 8
+
+#define BUS_FREQ 1000
+
+struct  __rte_cache_aligned uncore_power_info {
+	unsigned int die;                  /* Core die id */
+	unsigned int pkg;                  /* Package id */
+	uint32_t freqs[MAX_UNCORE_FREQS];  /* Frequency array */
+	uint32_t nb_freqs;                 /* Number of available freqs */
+	uint32_t curr_idx;                 /* Freq index in freqs array */
+	uint32_t max_freq;            /* System max uncore freq */
+	uint32_t min_freq;            /* System min uncore freq */
+};
+
+static struct uncore_power_info uncore_info[RTE_MAX_NUMA_NODES][MAX_NUMA_DIE];
+static int esmi_initialized;
+
+static int
+set_uncore_freq_internal(struct uncore_power_info *ui, uint32_t idx)
+{
+	int ret;
+
+	if (idx >= MAX_UNCORE_FREQS || idx >= ui->nb_freqs) {
+		POWER_LOG(DEBUG, "Invalid uncore frequency index %u, which "
+				"should be less than %u", idx, ui->nb_freqs);
+		return -1;
+	}
+
+	ret = esmi_apb_disable(ui->pkg, idx);
+	if (ret != ESMI_SUCCESS) {
+		POWER_LOG(ERR, "DF P-state '%u' set failed for pkg %02u",
+				idx, ui->pkg);
+		return -1;
+	}
+
+	POWER_DEBUG_LOG("DF P-state '%u' to be set for pkg %02u die %02u",
+				idx, ui->pkg, ui->die);

drivers/power/amd_uncore/amd_uncore.c line 62

Quoted 10 lines
drivers/power/amd_uncore/amd_uncore.c new file +10 -0
+
+	/* write the minimum value first if the target freq is less than current max */
+	ui->curr_idx = idx;
+
+	return 0;
+}
+
+/*
+ * Fopen the sys file for the future setting of the uncore die frequency.
+ */

Comment may need updating, as function is not reading any sysfs files (for the moment, at least).

drivers/power/amd_uncore/amd_uncore.c line 66

Quoted 4 lines
drivers/power/amd_uncore/amd_uncore.c new file +4 -0
+static int
+power_init_for_setting_uncore_freq(struct uncore_power_info *ui)
+{
+	/* open and read all uncore sys files */

Comment may need updating, as function is not reading any sysfs files (for the moment, at least).

drivers/power/amd_uncore/amd_uncore.c line 77

Quoted 11 lines
drivers/power/amd_uncore/amd_uncore.c new file +11 -0
+	/* Base max */
+	ui->max_freq = 1800000;
+	ui->min_freq = 1200000;
+
+	return 0;
+}
+
+/*
+ * Get the available uncore frequencies of the specific die by reading the
+ * sys file.
+ */

Comment may need updating, as function is not reading any sysfs files. 3 uncore frequencies hard-coded for the moment, may get via esmi or sysfs in the future.

Quoted 23 lines
drivers/power/amd_uncore/amd_uncore.c new file +23 -0
+static int
+power_get_available_uncore_freqs(struct uncore_power_info *ui)
+{
+	int ret = -1;
+	uint32_t i, num_uncore_freqs = 3;
+	uint32_t fabric_freqs[] = {
+	/* to be extended for probing support in future */
+		1800,
+		1444,
+		1200
+	};
+
+	if (num_uncore_freqs >= MAX_UNCORE_FREQS) {
+		POWER_LOG(ERR, "Too many available uncore frequencies: %d",
+				num_uncore_freqs);
+		goto out;
+	}
+
+	/* Generate the uncore freq bucket array. */
+	for (i = 0; i < num_uncore_freqs; i++)
+		ui->freqs[i] = fabric_freqs[i] * BUS_FREQ;
+
+	ui->nb_freqs = num_uncore_freqs;

drivers/power/amd_uncore/amd_uncore.c line 110

Quoted 10 lines
drivers/power/amd_uncore/amd_uncore.c new file +10 -0
+
+	ret = 0;
+
+	POWER_DEBUG_LOG("%d frequency(s) of pkg %02u die %02u are available",
+			num_uncore_freqs, ui->pkg, ui->die);
+
+out:
+	return ret;
+}
+

--snip--

Quoted 1 line
#23 lore raw

RE: [PATCH v1 4/4] power/amd_uncore: uncore power management support for AMD EPYC processors

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Dave,

Quoted 94 lines

-----Original Message-----
From: Hunt, David <[email protected]>
Sent: Tuesday, July 23, 2024 4:03 PM
To: Tummala, Sivaprasad <[email protected]>; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Cc: [email protected]
Subject: Re: [PATCH v1 4/4] power/amd_uncore: uncore power management support for AMD EPYC processors

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 6 lines · Sivaprasad Tummala

This patch introduces driver support for power management of uncore components in AMD EPYC processors.

---
  drivers/power/amd_uncore/amd_uncore.c | 321

++++++++++++++++++++++++++

Quoted 60 lines
  drivers/power/amd_uncore/amd_uncore.h | 226 ++++++++++++++++++
  drivers/power/amd_uncore/meson.build  |  20 ++
  drivers/power/meson.build             |   1 +
  4 files changed, 568 insertions(+)

create mode 100644 drivers/power/amd_uncore/amd_uncore.c create mode 100644 drivers/power/amd_uncore/amd_uncore.h create mode 100644 drivers/power/amd_uncore/meson.build

drivers/power/amd_uncore/amd_uncore.c new file +45 -0
new file mode 100644
index 0000000000..f15eaaa307
@@ -0,0 +1,321 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
+ */
+
+#include <errno.h>
+#include <dirent.h>
+#include <fnmatch.h>
+
+#include <rte_memcpy.h>
+
+#include "amd_uncore.h"
+#include "power_common.h"
+#include "e_smi/e_smi.h"
+
+#define MAX_UNCORE_FREQS 8
+#define MAX_NUMA_DIE 8
+
+#define BUS_FREQ 1000
+
+struct  __rte_cache_aligned uncore_power_info {
+     unsigned int die;                  /* Core die id */
+     unsigned int pkg;                  /* Package id */
+     uint32_t freqs[MAX_UNCORE_FREQS];  /* Frequency array */
+     uint32_t nb_freqs;                 /* Number of available freqs */
+     uint32_t curr_idx;                 /* Freq index in freqs array */
+     uint32_t max_freq;            /* System max uncore freq */
+     uint32_t min_freq;            /* System min uncore freq */
+};
+
+static struct uncore_power_info
+uncore_info[RTE_MAX_NUMA_NODES][MAX_NUMA_DIE];
+static int esmi_initialized;
+
+static int
+set_uncore_freq_internal(struct uncore_power_info *ui, uint32_t idx)
+{
+     int ret;
+
+     if (idx >= MAX_UNCORE_FREQS || idx >= ui->nb_freqs) {
+             POWER_LOG(DEBUG, "Invalid uncore frequency index %u, which "
+                             "should be less than %u", idx, ui->nb_freqs);
+             return -1;
+     }
+
+     ret = esmi_apb_disable(ui->pkg, idx);

drivers/power/amd_uncore/amd_uncore.c line 55

Quoted 10 lines
drivers/power/amd_uncore/amd_uncore.c new file +10 -0
+     if (ret != ESMI_SUCCESS) {
+             POWER_LOG(ERR, "DF P-state '%u' set failed for pkg %02u",
+                             idx, ui->pkg);
+             return -1;
+     }
+
+     POWER_DEBUG_LOG("DF P-state '%u' to be set for pkg %02u die %02u",
+                             idx, ui->pkg, ui->die);
+
+     /* write the minimum value first if the target freq is less than current max

*/

Quoted 12 lines

drivers/power/amd_uncore/amd_uncore.c line 62

Quoted 8 lines
drivers/power/amd_uncore/amd_uncore.c new file +8 -0
+     ui->curr_idx = idx;
+
+     return 0;
+}
+
+/*
+ * Fopen the sys file for the future setting of the uncore die frequency.
+ */

Comment may need updating, as function is not reading any sysfs files (for the moment, at least).

ACK! Will address this in next version.

Quoted 9 lines

drivers/power/amd_uncore/amd_uncore.c line 66

Quoted 5 lines
drivers/power/amd_uncore/amd_uncore.c new file +3 -0
+static int
+power_init_for_setting_uncore_freq(struct uncore_power_info *ui) {
+     /* open and read all uncore sys files */

Comment may need updating, as function is not reading any sysfs files (for the moment, at least).

ACK! Will address this in next version.

Quoted 12 lines
Quoted 12 lines
drivers/power/amd_uncore/amd_uncore.c new file +9 -0
+     /* Base max */
+     ui->max_freq = 1800000;
+     ui->min_freq = 1200000;
+
+     return 0;
+}
+
+/*
+ * Get the available uncore frequencies of the specific die by
Quoted 8 lines

drivers/power/amd_uncore/amd_uncore.c line 77

Quoted 3 lines
drivers/power/amd_uncore/amd_uncore.c new file +3 -0
+reading the
+ * sys file.
+ */

Comment may need updating, as function is not reading any sysfs files. 3 uncore frequencies hard-coded for the moment, may get via esmi or sysfs in the future.

ACK! Will address this in next version.

Quoted 40 lines
Quoted 18 lines
drivers/power/amd_uncore/amd_uncore.c new file +16 -0
+static int
+power_get_available_uncore_freqs(struct uncore_power_info *ui) {
+     int ret = -1;
+     uint32_t i, num_uncore_freqs = 3;
+     uint32_t fabric_freqs[] = {
+     /* to be extended for probing support in future */
+             1800,
+             1444,
+             1200
+     };
+
+     if (num_uncore_freqs >= MAX_UNCORE_FREQS) {
+             POWER_LOG(ERR, "Too many available uncore frequencies: %d",
+                             num_uncore_freqs);
+             goto out;
+     }

drivers/power/amd_uncore/amd_uncore.c

Quoted 10 lines
drivers/power/amd_uncore/amd_uncore.c new file +10 -0
+
+     /* Generate the uncore freq bucket array. */
+     for (i = 0; i < num_uncore_freqs; i++)
+             ui->freqs[i] = fabric_freqs[i] * BUS_FREQ;
+
+     ui->nb_freqs = num_uncore_freqs;
+
+     ret = 0;
+
+     POWER_DEBUG_LOG("%d frequency(s) of pkg %02u die %02u are

available",

drivers/power/amd_uncore/amd_uncore.c line 110

Quoted 6 lines
drivers/power/amd_uncore/amd_uncore.c new file +6 -0
+                     num_uncore_freqs, ui->pkg, ui->die);
+
+out:
+     return ret;
+}
+

--snip--

Quoted 1 line
#24 lore raw

cover power: refactor power management library

In reply to Sivaprasad Tummala

This patchset refactors the power management library, addressing both core and uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/core/*' and 'drivers/power/uncore/*'.

This refactor significantly improves code organization, enhances clarity, and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

Furthermore, this effort aims to streamline code maintenance by consolidating common functions for cpufreq and cppc across various core drivers, thus reducing code duplication.

Sivaprasad Tummala (4):
power: refactor core power management library
power: refactor uncore power management library test/power: removed function pointer validations power/amd_uncore: uncore power management support for AMD EPYC
processors

 app/test/test_power.c                         |  95 ------
 app/test/test_power_cpufreq.c                 |  52 ---
 app/test/test_power_kvm_vm.c                  |  36 --
 drivers/meson.build                           |   1 +
 .../power/acpi/acpi_cpufreq.c                 |  22 +-
 .../power/acpi/acpi_cpufreq.h                 |   6 +-
 drivers/power/acpi/meson.build                |  10 +
 .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
 .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
 drivers/power/amd_pstate/meson.build          |  10 +
 drivers/power/amd_uncore/amd_uncore.c         | 321 ++++++++++++++++++
 drivers/power/amd_uncore/amd_uncore.h         | 226 ++++++++++++
 drivers/power/amd_uncore/meson.build          |  20 ++
 .../power/cppc/cppc_cpufreq.c                 |  22 +-
 .../power/cppc/cppc_cpufreq.h                 |   8 +-
 drivers/power/cppc/meson.build                |  10 +
 .../power/intel_uncore/intel_uncore.c         |  18 +-
 .../power/intel_uncore/intel_uncore.h         |   8 +-
 drivers/power/intel_uncore/meson.build        |   7 +
 .../power/kvm_vm}/guest_channel.c             |   0
 .../power/kvm_vm}/guest_channel.h             |   0
 .../power/kvm_vm/kvm_vm.c                     |  22 +-
 .../power/kvm_vm/kvm_vm.h                     |   6 +-
 drivers/power/kvm_vm/meson.build              |  16 +
 drivers/power/meson.build                     |  14 +
 drivers/power/pstate/meson.build              |  10 +
 .../power/pstate/pstate_cpufreq.c             |  22 +-
 .../power/pstate/pstate_cpufreq.h             |   6 +-
 lib/power/meson.build                         |   9 +-
 lib/power/power_common.c                      |   2 +-
 lib/power/power_common.h                      |  16 +-
 lib/power/rte_power.c                         | 287 ++++++----------
 lib/power/rte_power.h                         | 139 +++++---
 lib/power/rte_power_core_ops.h                | 208 ++++++++++++
 lib/power/rte_power_uncore.c                  | 206 +++++------
 lib/power/rte_power_uncore.h                  |  91 ++---
 lib/power/rte_power_uncore_ops.h              | 239 +++++++++++++
 lib/power/version.map                         |  15 +
 38 files changed, 1591 insertions(+), 621 deletions(-)
 rename lib/power/power_acpi_cpufreq.c => drivers/power/acpi/acpi_cpufreq.c (95%)
 rename lib/power/power_acpi_cpufreq.h => drivers/power/acpi/acpi_cpufreq.h (98%)
 create mode 100644 drivers/power/acpi/meson.build
 rename lib/power/power_amd_pstate_cpufreq.c => drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%)
 rename lib/power/power_amd_pstate_cpufreq.h => drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%)
 create mode 100644 drivers/power/amd_pstate/meson.build
 create mode 100644 drivers/power/amd_uncore/amd_uncore.c
 create mode 100644 drivers/power/amd_uncore/amd_uncore.h
 create mode 100644 drivers/power/amd_uncore/meson.build
 rename lib/power/power_cppc_cpufreq.c => drivers/power/cppc/cppc_cpufreq.c (95%)
 rename lib/power/power_cppc_cpufreq.h => drivers/power/cppc/cppc_cpufreq.h (97%)
 create mode 100644 drivers/power/cppc/meson.build
 rename lib/power/power_intel_uncore.c => drivers/power/intel_uncore/intel_uncore.c (95%)
 rename lib/power/power_intel_uncore.h => drivers/power/intel_uncore/intel_uncore.h (97%)
 create mode 100644 drivers/power/intel_uncore/meson.build
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%)
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%)
 rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c (82%)
 rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h (98%)
 create mode 100644 drivers/power/kvm_vm/meson.build
 create mode 100644 drivers/power/meson.build
 create mode 100644 drivers/power/pstate/meson.build
 rename lib/power/power_pstate_cpufreq.c => drivers/power/pstate/pstate_cpufreq.c (96%)
 rename lib/power/power_pstate_cpufreq.h => drivers/power/pstate/pstate_cpufreq.h (98%)
 create mode 100644 lib/power/rte_power_core_ops.h
 create mode 100644 lib/power/rte_power_uncore_ops.h
Signature · 1 line
2.34.1
#25 lore raw

v2 cover power: refactor power management library

In reply to Sivaprasad Tummala

This patchset refactors the power management library, addressing both core and uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/core/*' and 'drivers/power/uncore/*'.

This refactor significantly improves code organization, enhances clarity, and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

Furthermore, this effort aims to streamline code maintenance by consolidating common functions for cpufreq and cppc across various core drivers, thus reducing code duplication.

Sivaprasad Tummala (4):
power: refactor core power management library
power: refactor uncore power management library test/power: removed function pointer validations power/amd_uncore: uncore power management support for AMD EPYC
processors

 app/test/test_power.c                         |  95 -----
 app/test/test_power_cpufreq.c                 |  52 ---
 app/test/test_power_kvm_vm.c                  |  36 --
 drivers/meson.build                           |   1 +
 .../power/acpi/acpi_cpufreq.c                 |  22 +-
 .../power/acpi/acpi_cpufreq.h                 |   6 +-
 drivers/power/acpi/meson.build                |  10 +
 .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
 .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
 drivers/power/amd_pstate/meson.build          |  10 +
 drivers/power/amd_uncore/amd_uncore.c         | 328 ++++++++++++++++++
 drivers/power/amd_uncore/amd_uncore.h         | 226 ++++++++++++
 drivers/power/amd_uncore/meson.build          |  20 ++
 .../power/cppc/cppc_cpufreq.c                 |  22 +-
 .../power/cppc/cppc_cpufreq.h                 |   8 +-
 drivers/power/cppc/meson.build                |  10 +
 .../power/intel_uncore/intel_uncore.c         |  18 +-
 .../power/intel_uncore/intel_uncore.h         |   8 +-
 drivers/power/intel_uncore/meson.build        |   6 +
 .../power/kvm_vm}/guest_channel.c             |   0
 .../power/kvm_vm}/guest_channel.h             |   0
 .../power/kvm_vm/kvm_vm.c                     |  22 +-
 .../power/kvm_vm/kvm_vm.h                     |   6 +-
 drivers/power/kvm_vm/meson.build              |  16 +
 drivers/power/meson.build                     |  14 +
 drivers/power/pstate/meson.build              |  10 +
 .../power/pstate/pstate_cpufreq.c             |  22 +-
 .../power/pstate/pstate_cpufreq.h             |   6 +-
 examples/l3fwd-power/main.c                   |  12 +-
 lib/power/meson.build                         |   9 +-
 lib/power/power_common.c                      |   2 +-
 lib/power/power_common.h                      |  16 +-
 lib/power/rte_power.c                         | 291 ++++++----------
 lib/power/rte_power.h                         | 139 +++++---
 lib/power/rte_power_core_ops.h                | 208 +++++++++++
 lib/power/rte_power_uncore.c                  | 205 +++++------
 lib/power/rte_power_uncore.h                  |  87 +++--
 lib/power/rte_power_uncore_ops.h              | 239 +++++++++++++
 lib/power/version.map                         |  15 +
 39 files changed, 1604 insertions(+), 625 deletions(-)
 rename lib/power/power_acpi_cpufreq.c => drivers/power/acpi/acpi_cpufreq.c (95%)
 rename lib/power/power_acpi_cpufreq.h => drivers/power/acpi/acpi_cpufreq.h (98%)
 create mode 100644 drivers/power/acpi/meson.build
 rename lib/power/power_amd_pstate_cpufreq.c => drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%)
 rename lib/power/power_amd_pstate_cpufreq.h => drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%)
 create mode 100644 drivers/power/amd_pstate/meson.build
 create mode 100644 drivers/power/amd_uncore/amd_uncore.c
 create mode 100644 drivers/power/amd_uncore/amd_uncore.h
 create mode 100644 drivers/power/amd_uncore/meson.build
 rename lib/power/power_cppc_cpufreq.c => drivers/power/cppc/cppc_cpufreq.c (95%)
 rename lib/power/power_cppc_cpufreq.h => drivers/power/cppc/cppc_cpufreq.h (97%)
 create mode 100644 drivers/power/cppc/meson.build
 rename lib/power/power_intel_uncore.c => drivers/power/intel_uncore/intel_uncore.c (95%)
 rename lib/power/power_intel_uncore.h => drivers/power/intel_uncore/intel_uncore.h (97%)
 create mode 100644 drivers/power/intel_uncore/meson.build
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%)
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%)
 rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c (82%)
 rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h (98%)
 create mode 100644 drivers/power/kvm_vm/meson.build
 create mode 100644 drivers/power/meson.build
 create mode 100644 drivers/power/pstate/meson.build
 rename lib/power/power_pstate_cpufreq.c => drivers/power/pstate/pstate_cpufreq.c (96%)
 rename lib/power/power_pstate_cpufreq.h => drivers/power/pstate/pstate_cpufreq.h (98%)
 create mode 100644 lib/power/rte_power_core_ops.h
 create mode 100644 lib/power/rte_power_uncore_ops.h
Signature · 1 line
2.34.1
#26 lore raw

PATCH v2 1/4 power: refactor core power management library

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

v2:
- added NULL check for global_core_ops in rte_power_get_core_ops

---
 drivers/meson.build                           |   1 +
 .../power/acpi/acpi_cpufreq.c                 |  22 +-
 .../power/acpi/acpi_cpufreq.h                 |   6 +-
 drivers/power/acpi/meson.build                |  10 +
 .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
 .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
 drivers/power/amd_pstate/meson.build          |  10 +
 .../power/cppc/cppc_cpufreq.c                 |  22 +-
 .../power/cppc/cppc_cpufreq.h                 |   8 +-
 drivers/power/cppc/meson.build                |  10 +
 .../power/kvm_vm}/guest_channel.c             |   0
 .../power/kvm_vm}/guest_channel.h             |   0
 .../power/kvm_vm/kvm_vm.c                     |  22 +-
 .../power/kvm_vm/kvm_vm.h                     |   6 +-
 drivers/power/kvm_vm/meson.build              |  16 +
 drivers/power/meson.build                     |  12 +
 drivers/power/pstate/meson.build              |  10 +
 .../power/pstate/pstate_cpufreq.c             |  22 +-
 .../power/pstate/pstate_cpufreq.h             |   6 +-
 lib/power/meson.build                         |   7 +-
 lib/power/power_common.c                      |   2 +-
 lib/power/power_common.h                      |  16 +-
 lib/power/rte_power.c                         | 291 ++++++------------
 lib/power/rte_power.h                         | 139 ++++++---
 lib/power/rte_power_core_ops.h                | 208 +++++++++++++
 lib/power/version.map                         |  14 +
 26 files changed, 621 insertions(+), 271 deletions(-)
 rename lib/power/power_acpi_cpufreq.c => drivers/power/acpi/acpi_cpufreq.c (95%)
 rename lib/power/power_acpi_cpufreq.h => drivers/power/acpi/acpi_cpufreq.h (98%)
 create mode 100644 drivers/power/acpi/meson.build
 rename lib/power/power_amd_pstate_cpufreq.c => drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%)
 rename lib/power/power_amd_pstate_cpufreq.h => drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%)
 create mode 100644 drivers/power/amd_pstate/meson.build
 rename lib/power/power_cppc_cpufreq.c => drivers/power/cppc/cppc_cpufreq.c (95%)
 rename lib/power/power_cppc_cpufreq.h => drivers/power/cppc/cppc_cpufreq.h (97%)
 create mode 100644 drivers/power/cppc/meson.build
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%)
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%)
 rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c (82%)
 rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h (98%)
 create mode 100644 drivers/power/kvm_vm/meson.build
 create mode 100644 drivers/power/meson.build
 create mode 100644 drivers/power/pstate/meson.build
 rename lib/power/power_pstate_cpufreq.c => drivers/power/pstate/pstate_cpufreq.c (96%)
 rename lib/power/power_pstate_cpufreq.h => drivers/power/pstate/pstate_cpufreq.h (98%)
 create mode 100644 lib/power/rte_power_core_ops.h
drivers/meson.build +1 -0
index 66931d4241..9d77e0deab 100644
@@ -29,6 +29,7 @@ subdirs = [
         'event',          # depends on common, bus, mempool and net.
         'baseband',       # depends on common and bus.
         'gpu',            # depends on common and bus.
+        'power',          # depends on common (in future).
 ]
 
 if meson.is_cross_build()
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +21 -1
similarity index 95%
rename from lib/power/power_acpi_cpufreq.c
rename to drivers/power/acpi/acpi_cpufreq.c
index 81996e1c13..8637c69703 100644
@@ -10,7 +10,7 @@
 #include <rte_stdatomic.h>
 #include <rte_string_fns.h>
 
-#include "power_acpi_cpufreq.h"
+#include "acpi_cpufreq.h"
 #include "power_common.h"
 
 #define STR_SIZE     1024
@@ -577,3 +577,23 @@ int power_acpi_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_core_ops acpi_ops = {
+	.name = "acpi",
+	.init = power_acpi_cpufreq_init,
+	.exit = power_acpi_cpufreq_exit,
+	.check_env_support = power_acpi_cpufreq_check_supported,
+	.get_avail_freqs = power_acpi_cpufreq_freqs,
+	.get_freq = power_acpi_cpufreq_get_freq,
+	.set_freq = power_acpi_cpufreq_set_freq,
+	.freq_down = power_acpi_cpufreq_freq_down,
+	.freq_up = power_acpi_cpufreq_freq_up,
+	.freq_max = power_acpi_cpufreq_freq_max,
+	.freq_min = power_acpi_cpufreq_freq_min,
+	.turbo_status = power_acpi_turbo_status,
+	.enable_turbo = power_acpi_enable_turbo,
+	.disable_turbo = power_acpi_disable_turbo,
+	.get_caps = power_acpi_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(acpi_ops);
drivers/power/acpi/acpi_cpufreq.h renamed from lib/power/power_acpi_cpufreq.h +3 -3
similarity index 98%
rename from lib/power/power_acpi_cpufreq.h
rename to drivers/power/acpi/acpi_cpufreq.h
index 682fd9278c..1194a7e2a5 100644
@@ -2,15 +2,15 @@
  * Copyright(c) 2010-2014 Intel Corporation
  */
 
-#ifndef _POWER_ACPI_CPUFREQ_H
-#define _POWER_ACPI_CPUFREQ_H
+#ifndef _ACPI_CPUFREQ_H
+#define _ACPI_CPUFREQ_H
 
 /**
  * @file
  * RTE Power Management via userspace ACPI cpufreq
  */
 
-#include "rte_power.h"
+#include "rte_power_core_ops.h"
 
 /**
  * Check if ACPI power management is supported.
drivers/power/acpi/meson.build new file +10 -0
new file mode 100644
index 0000000000..f5afc893ce
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('acpi_cpufreq.c')
+
+deps += ['power']
drivers/power/amd_pstate/amd_pstate_cpufreq.c renamed from lib/power/power_amd_pstate_cpufreq.c +22 -2
similarity index 95%
rename from lib/power/power_amd_pstate_cpufreq.c
rename to drivers/power/amd_pstate/amd_pstate_cpufreq.c
index 090a0d96cb..f571f4184a 100644
@@ -1,7 +1,7 @@
 /* SPDX-License-Identifier: BSD-3-Clause
  * Copyright(c) 2010-2021 Intel Corporation
  * Copyright(c) 2021 Arm Limited
- * Copyright(c) 2023 Amd Limited
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
  */
 
 #include <stdlib.h>
@@ -9,7 +9,7 @@
 #include <rte_memcpy.h>
 #include <rte_stdatomic.h>
 
-#include "power_amd_pstate_cpufreq.h"
+#include "amd_pstate_cpufreq.h"
 #include "power_common.h"
 
 /* macros used for rounding frequency to nearest 1000 */
@@ -700,3 +700,23 @@ power_amd_pstate_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_core_ops amd_pstate_ops = {
+	.name = "amd-pstate",
+	.init = power_amd_pstate_cpufreq_init,
+	.exit = power_amd_pstate_cpufreq_exit,
+	.check_env_support = power_amd_pstate_cpufreq_check_supported,
+	.get_avail_freqs = power_amd_pstate_cpufreq_freqs,
+	.get_freq = power_amd_pstate_cpufreq_get_freq,
+	.set_freq = power_amd_pstate_cpufreq_set_freq,
+	.freq_down = power_amd_pstate_cpufreq_freq_down,
+	.freq_up = power_amd_pstate_cpufreq_freq_up,
+	.freq_max = power_amd_pstate_cpufreq_freq_max,
+	.freq_min = power_amd_pstate_cpufreq_freq_min,
+	.turbo_status = power_amd_pstate_turbo_status,
+	.enable_turbo = power_amd_pstate_enable_turbo,
+	.disable_turbo = power_amd_pstate_disable_turbo,
+	.get_caps = power_amd_pstate_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(amd_pstate_ops);
drivers/power/amd_pstate/amd_pstate_cpufreq.h renamed from lib/power/power_amd_pstate_cpufreq.h +4 -4
similarity index 97%
rename from lib/power/power_amd_pstate_cpufreq.h
rename to drivers/power/amd_pstate/amd_pstate_cpufreq.h
index b02f9f98e4..b04b2f28c0 100644
@@ -1,18 +1,18 @@
 /* SPDX-License-Identifier: BSD-3-Clause
  * Copyright(c) 2010-2021 Intel Corporation
  * Copyright(c) 2021 Arm Limited
- * Copyright(c) 2023 Amd Limited
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
  */
 
-#ifndef _POWER_AMD_PSTATE_CPUFREQ_H
-#define _POWER_AMD_PSTATE_CPUFREQ_H
+#ifndef _AMD_PSTATE_CPUFREQ_H
+#define _AMD_PSTATE_CPUFREQ_H
 
 /**
  * @file
  * RTE Power Management via userspace AMD pstate cpufreq
  */
 
-#include "rte_power.h"
+#include "rte_power_core_ops.h"
 
 /**
  * Check if amd p-state power management is supported.
drivers/power/amd_pstate/meson.build new file +10 -0
new file mode 100644
index 0000000000..acaf20b388
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('amd_pstate_cpufreq.c')
+
+deps += ['power']
drivers/power/cppc/cppc_cpufreq.c renamed from lib/power/power_cppc_cpufreq.c +21 -1
similarity index 95%
rename from lib/power/power_cppc_cpufreq.c
rename to drivers/power/cppc/cppc_cpufreq.c
index 32aaacb948..775b8f4434 100644
@@ -8,7 +8,7 @@
 #include <rte_memcpy.h>
 #include <rte_stdatomic.h>
 
-#include "power_cppc_cpufreq.h"
+#include "cppc_cpufreq.h"
 #include "power_common.h"
 
 /* macros used for rounding frequency to nearest 100000 */
@@ -685,3 +685,23 @@ power_cppc_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_core_ops cppc_ops = {
+	.name = "cppc",
+	.init = power_cppc_cpufreq_init,
+	.exit = power_cppc_cpufreq_exit,
+	.check_env_support = power_cppc_cpufreq_check_supported,
+	.get_avail_freqs = power_cppc_cpufreq_freqs,
+	.get_freq = power_cppc_cpufreq_get_freq,
+	.set_freq = power_cppc_cpufreq_set_freq,
+	.freq_down = power_cppc_cpufreq_freq_down,
+	.freq_up = power_cppc_cpufreq_freq_up,
+	.freq_max = power_cppc_cpufreq_freq_max,
+	.freq_min = power_cppc_cpufreq_freq_min,
+	.turbo_status = power_cppc_turbo_status,
+	.enable_turbo = power_cppc_enable_turbo,
+	.disable_turbo = power_cppc_disable_turbo,
+	.get_caps = power_cppc_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(cppc_ops);
drivers/power/cppc/cppc_cpufreq.h renamed from lib/power/power_cppc_cpufreq.h +4 -4
similarity index 97%
rename from lib/power/power_cppc_cpufreq.h
rename to drivers/power/cppc/cppc_cpufreq.h
index f4121b237e..d6e32fdd47 100644
@@ -3,15 +3,15 @@
  * Copyright(c) 2021 Arm Limited
  */
 
-#ifndef _POWER_CPPC_CPUFREQ_H
-#define _POWER_CPPC_CPUFREQ_H
+#ifndef _CPPC_CPUFREQ_H
+#define _CPPC_CPUFREQ_H
 
 /**
  * @file
  * RTE Power Management via userspace CPPC cpufreq
  */
 
-#include "rte_power.h"
+#include "rte_power_core_ops.h"
 
 /**
  * Check if CPPC power management is supported.
@@ -215,4 +215,4 @@ int power_cppc_disable_turbo(unsigned int lcore_id);
 int power_cppc_get_capabilities(unsigned int lcore_id,
 		struct rte_power_core_capabilities *caps);
 
-#endif /* _POWER_CPPC_CPUFREQ_H */
+#endif /* _CPPC_CPUFREQ_H */
drivers/power/cppc/meson.build new file +10 -0
new file mode 100644
index 0000000000..f1948cd424
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('cppc_cpufreq.c')
+
+deps += ['power']
drivers/power/kvm_vm/guest_channel.c renamed from lib/power/guest_channel.c +0 -0
similarity index 100%
rename from lib/power/guest_channel.c
rename to drivers/power/kvm_vm/guest_channel.c
drivers/power/kvm_vm/guest_channel.h renamed from lib/power/guest_channel.h +0 -0
similarity index 100%
rename from lib/power/guest_channel.h
rename to drivers/power/kvm_vm/guest_channel.h
drivers/power/kvm_vm/kvm_vm.c renamed from lib/power/power_kvm_vm.c +21 -1
similarity index 82%
rename from lib/power/power_kvm_vm.c
rename to drivers/power/kvm_vm/kvm_vm.c
index f15be8fac5..a1342dcd8b 100644
@@ -9,7 +9,7 @@
 #include "rte_power_guest_channel.h"
 #include "guest_channel.h"
 #include "power_common.h"
-#include "power_kvm_vm.h"
+#include "kvm_vm.h"
 
 #define FD_PATH "/dev/virtio-ports/virtio.serial.port.poweragent"
 
@@ -137,3 +137,23 @@ int power_kvm_vm_get_capabilities(__rte_unused unsigned int lcore_id,
 	POWER_LOG(ERR, "rte_power_get_capabilities is not implemented for Virtual Machine Power Management");
 	return -ENOTSUP;
 }
+
+static struct rte_power_core_ops kvm_vm_ops = {
+	.name = "kvm-vm",
+	.init = power_kvm_vm_init,
+	.exit = power_kvm_vm_exit,
+	.check_env_support = power_kvm_vm_check_supported,
+	.get_avail_freqs = power_kvm_vm_freqs,
+	.get_freq = power_kvm_vm_get_freq,
+	.set_freq = power_kvm_vm_set_freq,
+	.freq_down = power_kvm_vm_freq_down,
+	.freq_up = power_kvm_vm_freq_up,
+	.freq_max = power_kvm_vm_freq_max,
+	.freq_min = power_kvm_vm_freq_min,
+	.turbo_status = power_kvm_vm_turbo_status,
+	.enable_turbo = power_kvm_vm_enable_turbo,
+	.disable_turbo = power_kvm_vm_disable_turbo,
+	.get_caps = power_kvm_vm_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(kvm_vm_ops);
drivers/power/kvm_vm/kvm_vm.h renamed from lib/power/power_kvm_vm.h +3 -3
similarity index 98%
rename from lib/power/power_kvm_vm.h
rename to drivers/power/kvm_vm/kvm_vm.h
index 303fcc041b..64086a67e7 100644
@@ -2,15 +2,15 @@
  * Copyright(c) 2010-2014 Intel Corporation
  */
 
-#ifndef _POWER_KVM_VM_H
-#define _POWER_KVM_VM_H
+#ifndef _KVM_VM_H
+#define _KVM_VM_H
 
 /**
  * @file
  * RTE Power Management KVM VM
  */
 
-#include "rte_power.h"
+#include "rte_power_core_ops.h"
 
 /**
  * Check if KVM power management is supported.
drivers/power/kvm_vm/meson.build new file +16 -0
new file mode 100644
index 0000000000..405524ce7c
@@ -0,0 +1,16 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(C) 2024 Advanced Micro Devices, Inc.
+#
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+sources = files(
+        'guest_channel.c',
+        'kvm_vm.c',
+)
+
+deps += ['power']
drivers/power/meson.build new file +12 -0
new file mode 100644
index 0000000000..8c7215c639
@@ -0,0 +1,12 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+drivers = [
+        'acpi',
+        'amd_pstate',
+        'cppc',
+        'kvm_vm',
+        'pstate'
+]
+
+std_deps = ['power']
drivers/power/pstate/meson.build new file +10 -0
new file mode 100644
index 0000000000..9cd47833fb
@@ -0,0 +1,10 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('pstate_cpufreq.c')
+
+deps += ['power']
drivers/power/pstate/pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c +21 -1
similarity index 96%
rename from lib/power/power_pstate_cpufreq.c
rename to drivers/power/pstate/pstate_cpufreq.c
index 2343121621..c32b1adabc 100644
@@ -15,7 +15,7 @@
 #include <rte_stdatomic.h>
 
 #include "rte_power_pmd_mgmt.h"
-#include "power_pstate_cpufreq.h"
+#include "pstate_cpufreq.h"
 #include "power_common.h"
 
 /* macros used for rounding frequency to nearest 100000 */
@@ -888,3 +888,23 @@ int power_pstate_get_capabilities(unsigned int lcore_id,
 
 	return 0;
 }
+
+static struct rte_power_core_ops pstate_ops = {
+	.name = "pstate",
+	.init = power_pstate_cpufreq_init,
+	.exit = power_pstate_cpufreq_exit,
+	.check_env_support = power_pstate_cpufreq_check_supported,
+	.get_avail_freqs = power_pstate_cpufreq_freqs,
+	.get_freq = power_pstate_cpufreq_get_freq,
+	.set_freq = power_pstate_cpufreq_set_freq,
+	.freq_down = power_pstate_cpufreq_freq_down,
+	.freq_up = power_pstate_cpufreq_freq_up,
+	.freq_max = power_pstate_cpufreq_freq_max,
+	.freq_min = power_pstate_cpufreq_freq_min,
+	.turbo_status = power_pstate_turbo_status,
+	.enable_turbo = power_pstate_enable_turbo,
+	.disable_turbo = power_pstate_disable_turbo,
+	.get_caps = power_pstate_get_capabilities
+};
+
+RTE_POWER_REGISTER_OPS(pstate_ops);
drivers/power/pstate/pstate_cpufreq.h renamed from lib/power/power_pstate_cpufreq.h +3 -3
similarity index 98%
rename from lib/power/power_pstate_cpufreq.h
rename to drivers/power/pstate/pstate_cpufreq.h
index 7bf64a518c..8b67b2da21 100644
@@ -2,15 +2,15 @@
  * Copyright(c) 2018 Intel Corporation
  */
 
-#ifndef _POWER_PSTATE_CPUFREQ_H
-#define _POWER_PSTATE_CPUFREQ_H
+#ifndef _PSTATE_CPUFREQ_H
+#define _PSTATE_CPUFREQ_H
 
 /**
  * @file
  * RTE Power Management via Intel Pstate driver
  */
 
-#include "rte_power.h"
+#include "rte_power_core_ops.h"
 
 /**
  * Check if pstate power management is supported.
lib/power/meson.build +1 -6
index b8426589b2..f3e3451cdc 100644
@@ -12,20 +12,15 @@ if not is_linux
     reason = 'only supported on Linux'
 endif
 sources = files(
-        'guest_channel.c',
-        'power_acpi_cpufreq.c',
-        'power_amd_pstate_cpufreq.c',
         'power_common.c',
-        'power_cppc_cpufreq.c',
-        'power_kvm_vm.c',
         'power_intel_uncore.c',
-        'power_pstate_cpufreq.c',
         'rte_power.c',
         'rte_power_uncore.c',
         'rte_power_pmd_mgmt.c',
 )
 headers = files(
         'rte_power.h',
+        'rte_power_core_ops.h',
         'rte_power_guest_channel.h',
         'rte_power_pmd_mgmt.h',
         'rte_power_uncore.h',
lib/power/power_common.c +1 -1
index 590986d5ef..6c06411e8b 100644
@@ -12,7 +12,7 @@
 
 #include "power_common.h"
 
-RTE_LOG_REGISTER_DEFAULT(power_logtype, INFO);
+RTE_LOG_REGISTER_DEFAULT(rte_power_logtype, INFO);
 
 #define POWER_SYSFILE_SCALING_DRIVER   \
 		"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_driver"
lib/power/power_common.h +14 -2
index 83f742f42a..767686ee12 100644
@@ -6,12 +6,13 @@
 #define _POWER_COMMON_H_
 
 #include <rte_common.h>
+#include <rte_compat.h>
 #include <rte_log.h>
 
 #define RTE_POWER_INVALID_FREQ_INDEX (~0)
 
-extern int power_logtype;
-#define RTE_LOGTYPE_POWER power_logtype
+extern int rte_power_logtype;
+#define RTE_LOGTYPE_POWER rte_power_logtype
 #define POWER_LOG(level, ...) \
 	RTE_LOG_LINE(level, POWER, "" __VA_ARGS__)
 
@@ -23,13 +24,24 @@ extern int power_logtype;
 #endif
 
 /* check if scaling driver matches one we want */
+__rte_internal
 int cpufreq_check_scaling_driver(const char *driver);
+
+__rte_internal
 int power_set_governor(unsigned int lcore_id, const char *new_governor,
 		char *orig_governor, size_t orig_governor_len);
+
+__rte_internal
 int open_core_sysfs_file(FILE **f, const char *mode, const char *format, ...)
 		__rte_format_printf(3, 4);
+
+__rte_internal
 int read_core_sysfs_u32(FILE *f, uint32_t *val);
+
+__rte_internal
 int read_core_sysfs_s(FILE *f, char *buf, unsigned int len);
+
+__rte_internal
 int write_core_sysfs_s(FILE *f, const char *str);
 
 #endif /* _POWER_COMMON_H_ */
lib/power/rte_power.c +98 -193

345 lines not on this page · raw message

lib/power/rte_power.h +93 -46

229 lines not on this page · raw message

lib/power/rte_power_core_ops.h new file +208 -0

208 lines not on this page · raw message

lib/power/version.map +14 -0
index c9a226614e..bd64e0828f 100644
@@ -51,4 +51,18 @@ EXPERIMENTAL {
 	rte_power_set_uncore_env;
 	rte_power_uncore_freqs;
 	rte_power_unset_uncore_env;
+	# added in 24.07
+	rte_power_logtype;
+};
+
+INTERNAL {
+	global:
+
+	rte_power_register_ops;
+	cpufreq_check_scaling_driver;
+	power_set_governor;
+	open_core_sysfs_file;
+	read_core_sysfs_u32;
+	read_core_sysfs_s;
+	write_core_sysfs_s;
 };
Signature · 1 line
2.34.1
#27 lore raw

Re: [PATCH v2 1/4] power: refactor core power management library

Quoted 8 lines · On Mon, 26 Aug 2024 13:06:46 +0000 Sivaprasad Tummala…
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +8 -0
+static struct rte_power_core_ops acpi_ops = {
+	.name = "acpi",
+	.init = power_acpi_cpufreq_init,
+	.exit = power_acpi_cpufreq_exit,
+	.check_env_support = power_acpi_cpufreq_check_supported,
+	.get_avail_freqs = power_acpi_cpufreq_freqs,
+	.get_freq = power_acpi_cpufreq_get_freq,
+	.set_freq = power_acpi_cpufreq_set_freq,

drivers/power/acpi/acpi_cpufreq.c int power_acpi_get_capabilities(unsigned int lcore_id, · line 598

Quoted 10 lines
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +10 -0
+	.freq_down = power_acpi_cpufreq_freq_down,
+	.freq_up = power_acpi_cpufreq_freq_up,
+	.freq_max = power_acpi_cpufreq_freq_max,
+	.freq_min = power_acpi_cpufreq_freq_min,
+	.turbo_status = power_acpi_turbo_status,
+	.enable_turbo = power_acpi_enable_turbo,
+	.disable_turbo = power_acpi_disable_turbo,
+	.get_caps = power_acpi_get_capabilities
+};
+

Can this be made const?
It is good for security and overall safety to have structures with function pointers marked const.

#28 lore raw

RE: [PATCH v2 1/4] power: refactor core power management library

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Stephen,

Quoted 30 lines

-----Original Message-----
From: Stephen Hemminger <[email protected]>
Sent: Monday, August 26, 2024 8:56 PM
To: Tummala, Sivaprasad <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]; [email protected]
Subject: Re: [PATCH v2 1/4] power: refactor core power management library

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 13 lines · On Mon, 26 Aug 2024 13:06:46 +0000 Sivaprasad Tummala…
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +13 -0
+static struct rte_power_core_ops acpi_ops = {
+     .name = "acpi",
+     .init = power_acpi_cpufreq_init,
+     .exit = power_acpi_cpufreq_exit,
+     .check_env_support = power_acpi_cpufreq_check_supported,
+     .get_avail_freqs = power_acpi_cpufreq_freqs,
+     .get_freq = power_acpi_cpufreq_get_freq,
+     .set_freq = power_acpi_cpufreq_set_freq,
+     .freq_down = power_acpi_cpufreq_freq_down,
+     .freq_up = power_acpi_cpufreq_freq_up,
+     .freq_max = power_acpi_cpufreq_freq_max,
+     .freq_min = power_acpi_cpufreq_freq_min,
+     .turbo_status = power_acpi_turbo_status,
Quoted 8 lines

drivers/power/acpi/acpi_cpufreq.c int power_acpi_get_capabilities(unsigned int lcore_id, · line 598

Quoted 4 lines
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +4 -0
+     .enable_turbo = power_acpi_enable_turbo,
+     .disable_turbo = power_acpi_disable_turbo,
+     .get_caps = power_acpi_get_capabilities };
+

Can this be made const?
It is good for security and overall safety to have structures with function pointers marked const.

The struct relies on dynamic list operations, it makes sense to keep it mutable. This will ensure we can effectively manage the operations as needed without running into issues with read-only restrictions.

#29 lore raw

Re: [PATCH v2 1/4] power: refactor core power management library

In reply to Sivaprasad Tummala

Hi Sivaprasa,

Some comments inline.

/Huisong

Quoted 56 lines · Sivaprasad Tummala

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers. These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

v2:
- added NULL check for global_core_ops in rte_power_get_core_ops

---
  drivers/meson.build                           |   1 +
  .../power/acpi/acpi_cpufreq.c                 |  22 +-
  .../power/acpi/acpi_cpufreq.h                 |   6 +-
  drivers/power/acpi/meson.build                |  10 +
  .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
  .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
  drivers/power/amd_pstate/meson.build          |  10 +
  .../power/cppc/cppc_cpufreq.c                 |  22 +-
  .../power/cppc/cppc_cpufreq.h                 |   8 +-
  drivers/power/cppc/meson.build                |  10 +
  .../power/kvm_vm}/guest_channel.c             |   0
  .../power/kvm_vm}/guest_channel.h             |   0
  .../power/kvm_vm/kvm_vm.c                     |  22 +-
  .../power/kvm_vm/kvm_vm.h                     |   6 +-
  drivers/power/kvm_vm/meson.build              |  16 +
  drivers/power/meson.build                     |  12 +
  drivers/power/pstate/meson.build              |  10 +
  .../power/pstate/pstate_cpufreq.c             |  22 +-
  .../power/pstate/pstate_cpufreq.h             |   6 +-
  lib/power/meson.build                         |   7 +-
  lib/power/power_common.c                      |   2 +-
  lib/power/power_common.h                      |  16 +-
  lib/power/rte_power.c                         | 291 ++++++------------
  lib/power/rte_power.h                         | 139 ++++++---
  lib/power/rte_power_core_ops.h                | 208 +++++++++++++
  lib/power/version.map                         |  14 +
  26 files changed, 621 insertions(+), 271 deletions(-)

rename lib/power/power_acpi_cpufreq.c => drivers/power/acpi/acpi_cpufreq.c (95%) rename lib/power/power_acpi_cpufreq.h => drivers/power/acpi/acpi_cpufreq.h (98%) create mode 100644 drivers/power/acpi/meson.build
rename lib/power/power_amd_pstate_cpufreq.c => drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%) rename lib/power/power_amd_pstate_cpufreq.h => drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%) create mode 100644 drivers/power/amd_pstate/meson.build
rename lib/power/power_cppc_cpufreq.c => drivers/power/cppc/cppc_cpufreq.c (95%) rename lib/power/power_cppc_cpufreq.h => drivers/power/cppc/cppc_cpufreq.h (97%) create mode 100644 drivers/power/cppc/meson.build
rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%)
rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%)

Quoted 8 lines

rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c (82%) rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h (98%) create mode 100644 drivers/power/kvm_vm/meson.build
create mode 100644 drivers/power/meson.build
create mode 100644 drivers/power/pstate/meson.build
rename lib/power/power_pstate_cpufreq.c => drivers/power/pstate/pstate_cpufreq.c (96%) rename lib/power/power_pstate_cpufreq.h => drivers/power/pstate/pstate_cpufreq.h (98%) create mode 100644 lib/power/rte_power_core_ops.h

How about use the following directory structure?
*For power libs*
lib/power/power_common.*
lib/power/rte_power_pmd_mgmt.*
lib/power/rte_power_cpufreq_api.* (replacing rte_power.c file maybe simple for us. but I'm not sure if we can put the init of core, uncore and pmd mgmt to rte_power_init.c in rte_power.c.) lib/power/rte_power_uncore_freq_api.*

*And has directories under drivers/power:*
1> For core dvfs driver:
drivers/power/cpufreq/acpi_cpufreq.c
drivers/power/cpufreq/cppc_cpufreq.c
drivers/power/cpufreq/amd_pstate_cpufreq.c
drivers/power/cpufreq/intel_pstate_cpufreq.c
drivers/power/cpufreq/kvm_cpufreq.c
The code of each cpufreq driver is not too much and doesn't probably increase. So don't need to use a directory for it.

2> For uncore dvfs driver:
drivers/power/uncorefreq/intel_uncore.*

drivers/power/acpi/acpi_cpufreq.c

Quoted 16 lines
drivers/meson.build +1 -0
index 66931d4241..9d77e0deab 100644
@@ -29,6 +29,7 @@ subdirs = [
          'event',          # depends on common, bus, mempool and net.
          'baseband',       # depends on common and bus.
          'gpu',            # depends on common and bus.
+        'power',          # depends on common (in future).
  ]
 
  if meson.is_cross_build()
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +0 -0
similarity index 95%
rename from lib/power/power_acpi_cpufreq.c
rename to drivers/power/acpi/acpi_cpufreq.c

do not suggest to create one directory for each cpufreq driver. Because pstate drivers also comply with ACPI spec, right? In addition, the code of each cpufreq drivers are not too much. There is just one file under one directory which is not good.

Quoted 11 lines

index 81996e1c13..8637c69703 100644

drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +1 -1
@@ -10,7 +10,7 @@
  #include <rte_stdatomic.h>
  #include <rte_string_fns.h>
 
-#include "power_acpi_cpufreq.h"
+#include "acpi_cpufreq.h"
  #include "power_common.h"
 

<...>

drivers/power/pstate/pstate_cpufreq.c

Quoted 11 lines
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +7 -0
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('pstate_cpufreq.c')
+
+deps += ['power']
drivers/power/pstate/pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c +0 -0
similarity index 96%
rename from lib/power/power_pstate_cpufreq.c
rename to drivers/power/pstate/pstate_cpufreq.c

pstate_cpufreq.c is actually intel_pstate cpufreq driver, right? So how about modify this file name to intel_pstate_cpufreq.c?

Quoted 17 lines

index 2343121621..c32b1adabc 100644

drivers/power/pstate/pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c +2 -1
@@ -15,7 +15,7 @@
  #include <rte_stdatomic.h>
 
  #include "rte_power_pmd_mgmt.h"
-#include "power_pstate_cpufreq.h"
+#include "pstate_cpufreq.h"
  #include "power_common.h"
 
  /* macros used for rounding frequency to nearest 100000 */
@@ -888,3 +888,23 @@ int power_pstate_get_capabilities(unsigned int lcore_id,
 
  	return 0;
  }
+

<...>

Quoted 34 lines
lib/power/power_common.c +1 -1
index 590986d5ef..6c06411e8b 100644
@@ -12,7 +12,7 @@
 
  #include "power_common.h"
 
-RTE_LOG_REGISTER_DEFAULT(power_logtype, INFO);
+RTE_LOG_REGISTER_DEFAULT(rte_power_logtype, INFO);
 
  #define POWER_SYSFILE_SCALING_DRIVER   \
  		"/sys/devices/system/cpu/cpu%u/cpufreq/scaling_driver"
lib/power/power_common.h +3 -2
index 83f742f42a..767686ee12 100644
@@ -6,12 +6,13 @@
  #define _POWER_COMMON_H_
 
  #include <rte_common.h>
+#include <rte_compat.h>
  #include <rte_log.h>
 
  #define RTE_POWER_INVALID_FREQ_INDEX (~0)
 
-extern int power_logtype;
-#define RTE_LOGTYPE_POWER power_logtype
+extern int rte_power_logtype;
+#define RTE_LOGTYPE_POWER rte_power_logtype
  #define POWER_LOG(level, ...) \
  	RTE_LOG_LINE(level, POWER, "" __VA_ARGS__)
 

lib/power/power_common.h extern int power_logtype; · line 32

Quoted 9 lines
lib/power/power_common.h +3 -0
@@ -23,13 +24,24 @@ extern int power_logtype;
  #endif
 
  /* check if scaling driver matches one we want */
+__rte_internal
  int cpufreq_check_scaling_driver(const char *driver);
+
+__rte_internal
  int power_set_governor(unsigned int lcore_id, const char *new_governor,
  		char *orig_governor, size_t orig_governor_len);

suggest that move cpufreq interfaces like this to the rte_power_cpufreq_api.* I proposed above. The interfaces in power_comm.* can be used by all power modules, like core/uncore/pmd mgmt.

lib/power/rte_power.c

Quoted 16 lines
lib/power/power_common.h +8 -0
+
+__rte_internal
  int open_core_sysfs_file(FILE **f, const char *mode, const char *format, ...)
  		__rte_format_printf(3, 4);
+
+__rte_internal
  int read_core_sysfs_u32(FILE *f, uint32_t *val);
+
+__rte_internal
  int read_core_sysfs_s(FILE *f, char *buf, unsigned int len);
+
+__rte_internal
  int write_core_sysfs_s(FILE *f, const char *str);
 
  #endif /* _POWER_COMMON_H_ */
lib/power/rte_power.c +0 -0

The name of the rte_power.c file is impropriate now. The context in this file is just for cpufreq, right? So I suggest that we need to rename this file as the rte_power_cpufreq_api.c

Quoted 60 lines

index 36c3f3da98..2bf6d40517 100644

lib/power/rte_power.c +17 -30
@@ -8,153 +8,86 @@
  #include <rte_spinlock.h>
 
  #include "rte_power.h"
-#include "power_acpi_cpufreq.h"
-#include "power_cppc_cpufreq.h"
  #include "power_common.h"
-#include "power_kvm_vm.h"
-#include "power_pstate_cpufreq.h"
-#include "power_amd_pstate_cpufreq.h"
 
-enum power_management_env global_default_env = PM_ENV_NOT_SET;
+static enum power_management_env global_default_env = PM_ENV_NOT_SET;
+static struct rte_power_core_ops *global_power_core_ops;
 
  static rte_spinlock_t global_env_cfg_lock = RTE_SPINLOCK_INITIALIZER;
+static RTE_TAILQ_HEAD(, rte_power_core_ops) core_ops_list =
+			TAILQ_HEAD_INITIALIZER(core_ops_list);
 
-/* function pointers */
-rte_power_freqs_t rte_power_freqs  = NULL;
-rte_power_get_freq_t rte_power_get_freq = NULL;
-rte_power_set_freq_t rte_power_set_freq = NULL;
-rte_power_freq_change_t rte_power_freq_up = NULL;
-rte_power_freq_change_t rte_power_freq_down = NULL;
-rte_power_freq_change_t rte_power_freq_max = NULL;
-rte_power_freq_change_t rte_power_freq_min = NULL;
-rte_power_freq_change_t rte_power_turbo_status;
-rte_power_freq_change_t rte_power_freq_enable_turbo;
-rte_power_freq_change_t rte_power_freq_disable_turbo;
-rte_power_get_capabilities_t rte_power_get_capabilities;
-
-static void
-reset_power_function_ptrs(void)
+
+const char *power_env_str[] = {
+	"not set",
+	"acpi",
+	"kvm-vm",
+	"pstate",
+	"cppc",
+	"amd-pstate"
+};
+
+/* register the ops struct in rte_power_core_ops, return 0 on success. */
+int
+rte_power_register_ops(struct rte_power_core_ops *driver_ops)
  {
-	rte_power_freqs  = NULL;
-	rte_power_get_freq = NULL;
-	rte_power_set_freq = NULL;
-	rte_power_freq_up = NULL;
-	rte_power_freq_down = NULL;
-	rte_power_freq_max = NULL;
-	rte_power_freq_min = NULL;
-	rte_power_turbo_status = NULL;
-	rte_power_freq_enable_turbo = NULL;

lib/power/rte_power.c line 41

Quoted 10 lines
lib/power/rte_power.c +8 -2
-	rte_power_freq_disable_turbo = NULL;
-	rte_power_get_capabilities = NULL;
+	if (!driver_ops->init || !driver_ops->exit ||
+		!driver_ops->check_env_support || !driver_ops->get_avail_freqs ||
+		!driver_ops->get_freq || !driver_ops->set_freq ||
+		!driver_ops->freq_up || !driver_ops->freq_down ||
+		!driver_ops->freq_max || !driver_ops->freq_min ||
+		!driver_ops->turbo_status || !driver_ops->enable_turbo ||
+		!driver_ops->disable_turbo || !driver_ops->get_caps) {
+		POWER_LOG(ERR, "Missing callbacks while registering power ops");

turbo_status(), enable_turbo() and disable turbo() are not necessary, right? These depand on the capabilities from get_caps().

Quoted 53 lines
lib/power/rte_power.c +20 -19
+		return -EINVAL;
+	}
+
+	TAILQ_INSERT_TAIL(&core_ops_list, driver_ops, next);
+
+	return 0;
  }
 
  int
  rte_power_check_env_supported(enum power_management_env env)
  {
-	switch (env) {
-	case PM_ENV_ACPI_CPUFREQ:
-		return power_acpi_cpufreq_check_supported();
-	case PM_ENV_PSTATE_CPUFREQ:
-		return power_pstate_cpufreq_check_supported();
-	case PM_ENV_KVM_VM:
-		return power_kvm_vm_check_supported();
-	case PM_ENV_CPPC_CPUFREQ:
-		return power_cppc_cpufreq_check_supported();
-	case PM_ENV_AMD_PSTATE_CPUFREQ:
-		return power_amd_pstate_cpufreq_check_supported();
-	default:
-		rte_errno = EINVAL;
-		return -1;
-	}
+	struct rte_power_core_ops *ops;
+
+	if (env >= RTE_DIM(power_env_str))
+		return 0;
+
+	RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+		if (strncmp(ops->name, power_env_str[env],
+				RTE_POWER_DRIVER_NAMESZ) == 0)
+			return ops->check_env_support();
+
+	return 0;
  }
 
  int
  rte_power_set_env(enum power_management_env env)
  {
+	struct rte_power_core_ops *ops;
+	int ret = -1;
+
  	rte_spinlock_lock(&global_env_cfg_lock);
 
  	if (global_default_env != PM_ENV_NOT_SET) {
  		POWER_LOG(ERR, "Power Management Environment already set.");
-		rte_spinlock_unlock(&global_env_cfg_lock);
-		return -1;
-	}
-

<...>

Quoted 81 lines
lib/power/rte_power.c +24 -38
-	if (ret == 0)
-		global_default_env = env;
-	else {
-		global_default_env = PM_ENV_NOT_SET;
-		reset_power_function_ptrs();
-	}
+	RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+		if (strncmp(ops->name, power_env_str[env],
+				RTE_POWER_DRIVER_NAMESZ) == 0) {
+			global_power_core_ops = ops;
+			global_default_env = env;
+			ret = 0;
+			goto out;
+		}
+	POWER_LOG(ERR, "Invalid Power Management Environment(%d) set",
+			env);
 
+out:
  	rte_spinlock_unlock(&global_env_cfg_lock);
  	return ret;
  }
@@ -164,94 +97,66 @@ rte_power_unset_env(void)
  {
  	rte_spinlock_lock(&global_env_cfg_lock);
  	global_default_env = PM_ENV_NOT_SET;
-	reset_power_function_ptrs();
+	global_power_core_ops = NULL;
  	rte_spinlock_unlock(&global_env_cfg_lock);
  }
 
  enum power_management_env
-rte_power_get_env(void) {
+rte_power_get_env(void)
+{
  	return global_default_env;
  }
 
-int
-rte_power_init(unsigned int lcore_id)
+struct rte_power_core_ops *
+rte_power_get_core_ops(void)
  {
-	int ret = -1;
+	RTE_ASSERT(global_power_core_ops != NULL);
 
-	switch (global_default_env) {
-	case PM_ENV_ACPI_CPUFREQ:
-		return power_acpi_cpufreq_init(lcore_id);
-	case PM_ENV_KVM_VM:
-		return power_kvm_vm_init(lcore_id);
-	case PM_ENV_PSTATE_CPUFREQ:
-		return power_pstate_cpufreq_init(lcore_id);
-	case PM_ENV_CPPC_CPUFREQ:
-		return power_cppc_cpufreq_init(lcore_id);
-	case PM_ENV_AMD_PSTATE_CPUFREQ:
-		return power_amd_pstate_cpufreq_init(lcore_id);
-	default:
-		POWER_LOG(INFO, "Env isn't set yet!");
-	}
+	return global_power_core_ops;
+}
 
-	/* Auto detect Environment */
-	POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq power management...");
-	ret = power_acpi_cpufreq_init(lcore_id);
-	if (ret == 0) {
-		rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
-		goto out;
-	}
+int
+rte_power_init(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops;
+	uint8_t env;
 
-	POWER_LOG(INFO, "Attempting to initialise PSTAT power management...");
-	ret = power_pstate_cpufreq_init(lcore_id);
-	if (ret == 0) {
-		rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
-		goto out;
-	}

lib/power/rte_power.c rte_power_unset_env(void) · line 127

Quoted 10 lines
lib/power/rte_power.c +3 -6
+	if (global_default_env != PM_ENV_NOT_SET)
+		return global_power_core_ops->init(lcore_id);
 
-	POWER_LOG(INFO, "Attempting to initialise AMD PSTATE power management...");
-	ret = power_amd_pstate_cpufreq_init(lcore_id);
-	if (ret == 0) {
-		rte_power_set_env(PM_ENV_AMD_PSTATE_CPUFREQ);
-		goto out;
-	}
+	POWER_LOG(INFO, "Env isn't set yet!");

remove this log?

Quoted 12 lines
lib/power/rte_power.c +5 -6
-	POWER_LOG(INFO, "Attempting to initialise CPPC power management...");
-	ret = power_cppc_cpufreq_init(lcore_id);
-	if (ret == 0) {
-		rte_power_set_env(PM_ENV_CPPC_CPUFREQ);
-		goto out;
-	}
+	/* Auto detect Environment */
+	RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+		if (ops) {
+			POWER_LOG(INFO,
+				"Attempting to initialise %s cpufreq power management...",

lib/power/rte_power.c rte_power_unset_env(void) · line 143

Quoted 10 lines
lib/power/rte_power.c +10 -0
+				ops->name);
+			if (ops->init(lcore_id) == 0) {
+				for (env = 0; env < RTE_DIM(power_env_str); env++)
+					if (strncmp(ops->name, power_env_str[env],
+							RTE_POWER_DRIVER_NAMESZ) == 0) {
+						rte_power_set_env(env);
+						return 0;
+					}
+			}
+		}

Can we change the logic of rte_power_set_env()? like:
    RTE_TAILQ_FOREACH(ops, &core_ops_list, next) {
        for (env = 0; env < RTE_DIM(power_env_str); env++) {
                if (strncmp(ops->name, power_env_str[env], RTE_POWER_DRIVER_NAMESZ) == 0 && ops->init(lcore_id) == 0) {
                    global_power_core_ops = ops;
                    global_default_env = env;
                }
        }
    }
That is easier to follow code.

Quoted 81 lines
lib/power/rte_power.c +10 -25
+
+	POWER_LOG(ERR,
+		"Unable to set Power Management Environment for lcore %u",
+		lcore_id);
 
-	POWER_LOG(INFO, "Attempting to initialise VM power management...");
-	ret = power_kvm_vm_init(lcore_id);
-	if (ret == 0) {
-		rte_power_set_env(PM_ENV_KVM_VM);
-		goto out;
-	}
-	POWER_LOG(ERR, "Unable to set Power Management Environment for lcore "
-			"%u", lcore_id);
-out:
-	return ret;
+	return -1;
  }
 
  int
  rte_power_exit(unsigned int lcore_id)
  {
-	switch (global_default_env) {
-	case PM_ENV_ACPI_CPUFREQ:
-		return power_acpi_cpufreq_exit(lcore_id);
-	case PM_ENV_KVM_VM:
-		return power_kvm_vm_exit(lcore_id);
-	case PM_ENV_PSTATE_CPUFREQ:
-		return power_pstate_cpufreq_exit(lcore_id);
-	case PM_ENV_CPPC_CPUFREQ:
-		return power_cppc_cpufreq_exit(lcore_id);
-	case PM_ENV_AMD_PSTATE_CPUFREQ:
-		return power_amd_pstate_cpufreq_exit(lcore_id);
-	default:
-		POWER_LOG(ERR, "Environment has not been set, unable to exit gracefully");
+	if (global_default_env != PM_ENV_NOT_SET)
+		return global_power_core_ops->exit(lcore_id);
 
-	}
-	return -1;
+	POWER_LOG(ERR,
+		"Environment has not been set, unable to exit gracefully");
 
+	return -1;
  }
lib/power/rte_power.h +11 -3
index 4fa4afe399..5e4aacf08b 100644
@@ -1,5 +1,6 @@
  /* SPDX-License-Identifier: BSD-3-Clause
   * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
   */
 
  #ifndef _RTE_POWER_H
@@ -14,14 +15,21 @@
  #include <rte_log.h>
  #include <rte_power_guest_channel.h>
 
+#include "rte_power_core_ops.h"
+
  #ifdef __cplusplus
  extern "C" {
  #endif
 
  /* Power Management Environment State */
-enum power_management_env {PM_ENV_NOT_SET, PM_ENV_ACPI_CPUFREQ, PM_ENV_KVM_VM,
-		PM_ENV_PSTATE_CPUFREQ, PM_ENV_CPPC_CPUFREQ,
-		PM_ENV_AMD_PSTATE_CPUFREQ};
+enum power_management_env {
+	PM_ENV_NOT_SET = 0,
+	PM_ENV_ACPI_CPUFREQ,
+	PM_ENV_KVM_VM,
+	PM_ENV_PSTATE_CPUFREQ,
+	PM_ENV_CPPC_CPUFREQ,
+	PM_ENV_AMD_PSTATE_CPUFREQ
+};
 
  /**
   * Check if a specific power management environment type is supported on a

lib/power/rte_power.h void rte_power_unset_env(void); · line 75

Quoted 2 lines
lib/power/rte_power.h +0 -0
@@ -66,6 +74,15 @@ void rte_power_unset_env(void);
   */
  enum power_management_env rte_power_get_env(void);

I'd like to let user not know used which cpufreq driver, which is friendly to user.

So we can rethink if this API is necessary.

Quoted 207 lines
lib/power/rte_power.h +82 -43
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_core_ops *
+rte_power_get_core_ops(void);
+
  /**
   * Initialize power management for a specific lcore. If rte_power_set_env() has
   * not been called then an auto-detect of the environment will start and
@@ -108,10 +125,13 @@ int rte_power_exit(unsigned int lcore_id);
   * @return
   *  The number of available frequencies.
   */
-typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
-		uint32_t num);
+static inline uint32_t
+rte_power_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t n)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_freqs_t rte_power_freqs;
+	return ops->get_avail_freqs(lcore_id, freqs, n);
+}
 
  /**
   * Return the current index of available frequencies of a specific lcore.
@@ -124,9 +144,13 @@ extern rte_power_freqs_t rte_power_freqs;
   * @return
   *  The current index of available frequencies.
   */
-typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);
+static inline uint32_t
+rte_power_get_freq(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_get_freq_t rte_power_get_freq;
+	return ops->get_freq(lcore_id);
+}
 
  /**
   * Set the new frequency for a specific lcore by indicating the index of
@@ -144,82 +168,101 @@ extern rte_power_get_freq_t rte_power_get_freq;
   *  - 0 on success without frequency changed.
   *  - Negative on error.
   */
-typedef int (*rte_power_set_freq_t)(unsigned int lcore_id, uint32_t index);
-
-extern rte_power_set_freq_t rte_power_set_freq;
+static inline uint32_t
+rte_power_set_freq(unsigned int lcore_id, uint32_t index)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-/**
- * Function pointer definition for generic frequency change functions. Review
- * each environments specific documentation for usage.
- *
- * @param lcore_id
- *  lcore id.
- *
- * @return
- *  - 1 on success with frequency changed.
- *  - 0 on success without frequency changed.
- *  - Negative on error.
- */
-typedef int (*rte_power_freq_change_t)(unsigned int lcore_id);
+	return ops->set_freq(lcore_id, index);
+}
 
  /**
   * Scale up the frequency of a specific lcore according to the available
   * frequencies.
   * Review each environments specific documentation for usage.
   */
-extern rte_power_freq_change_t rte_power_freq_up;
+static inline int
+rte_power_freq_up(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+	return ops->freq_up(lcore_id);
+}
 
  /**
   * Scale down the frequency of a specific lcore according to the available
   * frequencies.
   * Review each environments specific documentation for usage.
   */
-extern rte_power_freq_change_t rte_power_freq_down;
+static inline int
+rte_power_freq_down(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+	return ops->freq_down(lcore_id);
+}
 
  /**
   * Scale up the frequency of a specific lcore to the highest according to the
   * available frequencies.
   * Review each environments specific documentation for usage.
   */
-extern rte_power_freq_change_t rte_power_freq_max;
+static inline int
+rte_power_freq_max(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+	return ops->freq_max(lcore_id);
+}
 
  /**
   * Scale down the frequency of a specific lcore to the lowest according to the
   * available frequencies.
   * Review each environments specific documentation for usage..
   */
-extern rte_power_freq_change_t rte_power_freq_min;
+static inline int
+rte_power_freq_min(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+	return ops->freq_min(lcore_id);
+}
 
  /**
   * Query the Turbo Boost status of a specific lcore.
   * Review each environments specific documentation for usage..
   */
-extern rte_power_freq_change_t rte_power_turbo_status;
+static inline int
+rte_power_turbo_status(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+	return ops->turbo_status(lcore_id);
+}
 
  /**
   * Enable Turbo Boost for this lcore.
   * Review each environments specific documentation for usage..
   */
-extern rte_power_freq_change_t rte_power_freq_enable_turbo;
+static inline int
+rte_power_freq_enable_turbo(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+	return ops->enable_turbo(lcore_id);
+}
 
  /**
   * Disable Turbo Boost for this lcore.
   * Review each environments specific documentation for usage..
   */
-extern rte_power_freq_change_t rte_power_freq_disable_turbo;
+static inline int
+rte_power_freq_disable_turbo(unsigned int lcore_id)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-/**
- * Power capabilities summary.
- */
-struct rte_power_core_capabilities {
-	union {
-		uint64_t capabilities;
-		struct {
-			uint64_t turbo:1;	/**< Turbo can be enabled. */
-			uint64_t priority:1;	/**< SST-BF high freq core */
-		};
-	};
-};
+	return ops->disable_turbo(lcore_id);
+}
 
  /**
   * Returns power capabilities for a specific lcore.
@@ -235,10 +278,14 @@ struct rte_power_core_capabilities {
   *  - 0 on success.
   *  - Negative on error.
   */
-typedef int (*rte_power_get_capabilities_t)(unsigned int lcore_id,
-		struct rte_power_core_capabilities *caps);
+static inline int
+rte_power_get_capabilities(unsigned int lcore_id,
+		struct rte_power_core_capabilities *caps)
+{
+	struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_get_capabilities_t rte_power_get_capabilities;
+	return ops->get_caps(lcore_id, caps);
+}
 
  #ifdef __cplusplus
  }
lib/power/rte_power_core_ops.h new file +0 -0
new file mode 100644
index 0000000000..356a64df79

lib/power/rte_power_core_ops.h line 8

Quoted 8 lines
lib/power/rte_power_core_ops.h new file +8 -0
@@ -0,0 +1,208 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _RTE_POWER_CORE_OPS_H
+#define _RTE_POWER_CORE_OPS_H
+

suggest rename the file as rte_power_cpufreq_api.h. If so, the role of this file is more clearly.

Quoted 31 lines
lib/power/rte_power_core_ops.h new file +26 -0
+__rte_internal
+int rte_power_register_ops(struct rte_power_core_ops *ops);
+
+/**
+ * Macro to statically register the ops of a cpufreq driver.
+ */
+#define RTE_POWER_REGISTER_OPS(ops)		\
+	RTE_INIT(power_hdlr_init_##ops)		\
+	{					\
+		rte_power_register_ops(&ops);	\
+	}
+
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_core_ops *
+rte_power_get_core_ops(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
lib/power/version.map +0 -0
index c9a226614e..bd64e0828f 100644

lib/power/version.map EXPERIMENTAL { · line 54

Quoted 4 lines
lib/power/version.map +1 -0
@@ -51,4 +51,18 @@ EXPERIMENTAL {
  	rte_power_set_uncore_env;
  	rte_power_uncore_freqs;
  	rte_power_unset_uncore_env;
+	# added in 24.07

24.07-->24.11?

Quoted 14 lines
lib/power/version.map +13 -0
+	rte_power_logtype;
+};
+
+INTERNAL {
+	global:
+
+	rte_power_register_ops;
+	cpufreq_check_scaling_driver;
+	power_set_governor;
+	open_core_sysfs_file;
+	read_core_sysfs_u32;
+	read_core_sysfs_s;
+	write_core_sysfs_s;
  };
#30 lore raw

RE: [PATCH v2 1/4] power: refactor core power management library

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Huisong,

Please find my response inline.

Quoted 93 lines

-----Original Message-----
From: lihuisong (C) <[email protected]>
Sent: Tuesday, August 27, 2024 1:51 PM
To: Tummala, Sivaprasad <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Subject: Re: [PATCH v2 1/4] power: refactor core power management library

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Hi Sivaprasa,

Some comments inline.

/Huisong

Quoted 45 lines · Sivaprasad Tummala

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.
These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

v2:
- added NULL check for global_core_ops in rte_power_get_core_ops

---
  drivers/meson.build                           |   1 +
  .../power/acpi/acpi_cpufreq.c                 |  22 +-
  .../power/acpi/acpi_cpufreq.h                 |   6 +-
  drivers/power/acpi/meson.build                |  10 +
  .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
  .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
  drivers/power/amd_pstate/meson.build          |  10 +
  .../power/cppc/cppc_cpufreq.c                 |  22 +-
  .../power/cppc/cppc_cpufreq.h                 |   8 +-
  drivers/power/cppc/meson.build                |  10 +
  .../power/kvm_vm}/guest_channel.c             |   0
  .../power/kvm_vm}/guest_channel.h             |   0
  .../power/kvm_vm/kvm_vm.c                     |  22 +-
  .../power/kvm_vm/kvm_vm.h                     |   6 +-
  drivers/power/kvm_vm/meson.build              |  16 +
  drivers/power/meson.build                     |  12 +
  drivers/power/pstate/meson.build              |  10 +
  .../power/pstate/pstate_cpufreq.c             |  22 +-
  .../power/pstate/pstate_cpufreq.h             |   6 +-
  lib/power/meson.build                         |   7 +-
  lib/power/power_common.c                      |   2 +-
  lib/power/power_common.h                      |  16 +-
  lib/power/rte_power.c                         | 291 ++++++------------
  lib/power/rte_power.h                         | 139 ++++++---
  lib/power/rte_power_core_ops.h                | 208 +++++++++++++
  lib/power/version.map                         |  14 +
  26 files changed, 621 insertions(+), 271 deletions(-)
Quoted 1 line

rename lib/power/power_acpi_cpufreq.c => drivers/power/acpi/acpi_cpufreq.c

(95%)

Quoted 1 line

rename lib/power/power_acpi_cpufreq.h => drivers/power/acpi/acpi_cpufreq.h

(98%)

Quoted 2 lines

create mode 100644 drivers/power/acpi/meson.build rename lib/power/power_amd_pstate_cpufreq.c =>

drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%)

Quoted 1 line

rename lib/power/power_amd_pstate_cpufreq.h =>

drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%)

Quoted 2 lines

create mode 100644 drivers/power/amd_pstate/meson.build
rename lib/power/power_cppc_cpufreq.c => drivers/power/cppc/cppc_cpufreq.c

(95%)

Quoted 1 line

rename lib/power/power_cppc_cpufreq.h => drivers/power/cppc/cppc_cpufreq.h

(97%)

Quoted 9 lines

create mode 100644 drivers/power/cppc/meson.build
rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%) rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%) rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c (82%) rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h (98%) create mode 100644 drivers/power/kvm_vm/meson.build
create mode 100644 drivers/power/meson.build
create mode 100644 drivers/power/pstate/meson.build
rename lib/power/power_pstate_cpufreq.c =>

drivers/power/pstate/pstate_cpufreq.c (96%)

Quoted 1 line

rename lib/power/power_pstate_cpufreq.h =>

drivers/power/pstate/pstate_cpufreq.h (98%)

Quoted 1 line

create mode 100644 lib/power/rte_power_core_ops.h

Quoted 8 lines

How about use the following directory structure?
*For power libs*
lib/power/power_common.*
lib/power/rte_power_pmd_mgmt.*
lib/power/rte_power_cpufreq_api.* (replacing rte_power.c file maybe simple for us. but I'm not sure if we can put the init of core, uncore and pmd mgmt to rte_power_init.c in rte_power.c.) lib/power/rte_power_uncore_freq_api.*

Yes, renaming rte_power.c is definitely a possible incremental change that could be considered later. However, for the time being, our focus will be on refactoring the cpufreq drivers only.

Quoted 24 lines

*And has directories under drivers/power:*
1> For core dvfs driver:
drivers/power/cpufreq/acpi_cpufreq.c drivers/power/cpufreq/cppc_cpufreq.c drivers/power/cpufreq/amd_pstate_cpufreq.c drivers/power/cpufreq/intel_pstate_cpufreq.c drivers/power/cpufreq/kvm_cpufreq.c
The code of each cpufreq driver is not too much and doesn't probably increase. So don't need to use a directory for it.

2> For uncore dvfs driver:
drivers/power/uncorefreq/intel_uncore.*

Quoted 11 lines
drivers/meson.build index renamed from drivers/meson.build +0 -0

66931d4241..9d77e0deab 100644

drivers/meson.build +1 -0
@@ -29,6 +29,7 @@ subdirs = [
          'event',          # depends on common, bus, mempool and net.
          'baseband',       # depends on common and bus.
          'gpu',            # depends on common and bus.
+        'power',          # depends on common (in future).
  ]
 
Quoted 10 lines
Quoted 6 lines
drivers/meson.build +0 -0
  if meson.is_cross_build()
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c rename to +0 -0
similarity index 95%
rename from lib/power/power_acpi_cpufreq.c rename to

drivers/power/acpi/acpi_cpufreq.c

do not suggest to create one directory for each cpufreq driver. Because pstate drivers also comply with ACPI spec, right? In addition, the code of each cpufreq drivers are not too much. There is just one file under one directory which is not good.

One of our objectives for the refactoring is to selectively disable non-essential drivers using Meson build options. However, by rearranging the driver structure, we risk disrupting this capability.

Quoted 12 lines
Quoted 11 lines

index 81996e1c13..8637c69703 100644

drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +1 -1
@@ -10,7 +10,7 @@
  #include <rte_stdatomic.h>
  #include <rte_string_fns.h>
 
-#include "power_acpi_cpufreq.h"
+#include "acpi_cpufreq.h"
  #include "power_common.h"
 

<...>

Quoted 14 lines
Quoted 11 lines
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +7 -0
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('pstate_cpufreq.c')
+
+deps += ['power']
drivers/power/pstate/pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c rename to +0 -0
similarity index 96%
rename from lib/power/power_pstate_cpufreq.c rename to
Quoted 1 line

drivers/power/pstate/pstate_cpufreq.c

pstate_cpufreq.c is actually intel_pstate cpufreq driver, right? So how about modify this file name to intel_pstate_cpufreq.c?

Yes, will fix this in next version.

Quoted 54 lines
Quoted 17 lines

index 2343121621..c32b1adabc 100644

drivers/power/pstate/pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c +3 -1
@@ -15,7 +15,7 @@
  #include <rte_stdatomic.h>
 
  #include "rte_power_pmd_mgmt.h"
-#include "power_pstate_cpufreq.h"
+#include "pstate_cpufreq.h"
  #include "power_common.h"
 
  /* macros used for rounding frequency to nearest 100000 */ @@ -888,3
+888,23 @@ int power_pstate_get_capabilities(unsigned int lcore_id,
 
      return 0;
  }
+

<...>

Quoted 36 lines
lib/power/power_common.c index renamed from lib/power/power_common.c +0 -0

590986d5ef..6c06411e8b 100644

lib/power/power_common.c +1 -1
@@ -12,7 +12,7 @@
 
  #include "power_common.h"
 
-RTE_LOG_REGISTER_DEFAULT(power_logtype, INFO);
+RTE_LOG_REGISTER_DEFAULT(rte_power_logtype, INFO);
 
  #define POWER_SYSFILE_SCALING_DRIVER   \
              "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_driver"
lib/power/power_common.h index renamed from lib/power/power_common.h +0 -0

83f742f42a..767686ee12 100644

lib/power/power_common.h +3 -2
@@ -6,12 +6,13 @@
  #define _POWER_COMMON_H_
 
  #include <rte_common.h>
+#include <rte_compat.h>
  #include <rte_log.h>
 
  #define RTE_POWER_INVALID_FREQ_INDEX (~0)
 
-extern int power_logtype;
-#define RTE_LOGTYPE_POWER power_logtype
+extern int rte_power_logtype;
+#define RTE_LOGTYPE_POWER rte_power_logtype
  #define POWER_LOG(level, ...) \
      RTE_LOG_LINE(level, POWER, "" __VA_ARGS__)
 
@@ -23,13 +24,24 @@ extern int power_logtype;
  #endif
 
Quoted 9 lines

lib/power/power_common.h extern int power_logtype; · line 32

Quoted 7 lines
lib/power/power_common.h +3 -0
  /* check if scaling driver matches one we want */
+__rte_internal
  int cpufreq_check_scaling_driver(const char *driver);
+
+__rte_internal
  int power_set_governor(unsigned int lcore_id, const char *new_governor,
              char *orig_governor, size_t orig_governor_len);

suggest that move cpufreq interfaces like this to the rte_power_cpufreq_api.* I proposed above.

This is an internal API and isn’t intended for direct use by applications. By moving it to rte_power_*, we risk exposing it inadvertently.

Quoted 16 lines

The interfaces in power_comm.* can be used by all power modules, like core/uncore/pmd mgmt.

Quoted 14 lines
lib/power/power_common.h +8 -0
+
+__rte_internal
  int open_core_sysfs_file(FILE **f, const char *mode, const char *format, ...)
              __rte_format_printf(3, 4);
+
+__rte_internal
  int read_core_sysfs_u32(FILE *f, uint32_t *val);
+
+__rte_internal
  int read_core_sysfs_s(FILE *f, char *buf, unsigned int len);
+
+__rte_internal
  int write_core_sysfs_s(FILE *f, const char *str);
 
Quoted 5 lines

lib/power/rte_power.c

Quoted 2 lines
lib/power/power_common.h +0 -0
  #endif /* _POWER_COMMON_H_ */
lib/power/rte_power.c +0 -0

The name of the rte_power.c file is impropriate now. The context in this file is just for cpufreq, right?
So I suggest that we need to rename this file as the rte_power_cpufreq_api.c

Yes, renaming rte_power.c to rte_power_cpufreq.c is definitely a possible incremental change and will fix this as a separate patch.
.

Quoted 65 lines
Quoted 6 lines

index 36c3f3da98..2bf6d40517 100644

lib/power/rte_power.c +0 -0
@@ -8,153 +8,86 @@
  #include <rte_spinlock.h>
 

lib/power/rte_power.c line 13

Quoted 10 lines
lib/power/rte_power.c +1 -6
  #include "rte_power.h"
-#include "power_acpi_cpufreq.h"
-#include "power_cppc_cpufreq.h"
  #include "power_common.h"
-#include "power_kvm_vm.h"
-#include "power_pstate_cpufreq.h"
-#include "power_amd_pstate_cpufreq.h"
 
-enum power_management_env global_default_env = PM_ENV_NOT_SET;
+static enum power_management_env global_default_env =

PM_ENV_NOT_SET;

Quoted 48 lines
lib/power/rte_power.c +3 -2
+static struct rte_power_core_ops *global_power_core_ops;
 
  static rte_spinlock_t global_env_cfg_lock =
 RTE_SPINLOCK_INITIALIZER;
+static RTE_TAILQ_HEAD(, rte_power_core_ops) core_ops_list =
+                     TAILQ_HEAD_INITIALIZER(core_ops_list);
 
-/* function pointers */
-rte_power_freqs_t rte_power_freqs  = NULL; -rte_power_get_freq_t

rte_power_get_freq = NULL; -rte_power_set_freq_t rte_power_set_freq = NULL; -rte_power_freq_change_t rte_power_freq_up = NULL;

lib/power/rte_power.c +15 -21
-rte_power_freq_change_t rte_power_freq_down = NULL;
-rte_power_freq_change_t rte_power_freq_max = NULL;
-rte_power_freq_change_t rte_power_freq_min = NULL;
-rte_power_freq_change_t rte_power_turbo_status;
-rte_power_freq_change_t rte_power_freq_enable_turbo;
-rte_power_freq_change_t rte_power_freq_disable_turbo;
-rte_power_get_capabilities_t rte_power_get_capabilities;
-
-static void
-reset_power_function_ptrs(void)
+
+const char *power_env_str[] = {
+     "not set",
+     "acpi",
+     "kvm-vm",
+     "pstate",
+     "cppc",
+     "amd-pstate"
+};
+
+/* register the ops struct in rte_power_core_ops, return 0 on
+success. */ int rte_power_register_ops(struct rte_power_core_ops
+*driver_ops)
  {
-     rte_power_freqs  = NULL;
-     rte_power_get_freq = NULL;
-     rte_power_set_freq = NULL;
-     rte_power_freq_up = NULL;
-     rte_power_freq_down = NULL;
-     rte_power_freq_max = NULL;
-     rte_power_freq_min = NULL;
-     rte_power_turbo_status = NULL;
-     rte_power_freq_enable_turbo = NULL;
-     rte_power_freq_disable_turbo = NULL;
-     rte_power_get_capabilities = NULL;
+     if (!driver_ops->init || !driver_ops->exit ||
+             !driver_ops->check_env_support || !driver_ops->get_avail_freqs ||
Quoted 8 lines

lib/power/rte_power.c

Quoted 7 lines
lib/power/rte_power.c +7 -0
+             !driver_ops->get_freq || !driver_ops->set_freq ||
+             !driver_ops->freq_up || !driver_ops->freq_down ||
+             !driver_ops->freq_max || !driver_ops->freq_min ||
+             !driver_ops->turbo_status || !driver_ops->enable_turbo ||
+             !driver_ops->disable_turbo || !driver_ops->get_caps) {
+             POWER_LOG(ERR, "Missing callbacks while registering
+ power ops");

turbo_status(), enable_turbo() and disable turbo() are not necessary, right?

Nope, this is required to get the current status unlike the capability API (get_caps()).

Quoted 181 lines

These depand on the capabilities from get_caps().

Quoted 53 lines
lib/power/rte_power.c +20 -19
+             return -EINVAL;
+     }
+
+     TAILQ_INSERT_TAIL(&core_ops_list, driver_ops, next);
+
+     return 0;
  }
 
  int
  rte_power_check_env_supported(enum power_management_env env)
  {
-     switch (env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_check_supported();
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_check_supported();
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_check_supported();
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_check_supported();
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_check_supported();
-     default:
-             rte_errno = EINVAL;
-             return -1;
-     }
+     struct rte_power_core_ops *ops;
+
+     if (env >= RTE_DIM(power_env_str))
+             return 0;
+
+     RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+             if (strncmp(ops->name, power_env_str[env],
+                             RTE_POWER_DRIVER_NAMESZ) == 0)
+                     return ops->check_env_support();
+
+     return 0;
  }
 
  int
  rte_power_set_env(enum power_management_env env)
  {
+     struct rte_power_core_ops *ops;
+     int ret = -1;
+
      rte_spinlock_lock(&global_env_cfg_lock);
 
      if (global_default_env != PM_ENV_NOT_SET) {
              POWER_LOG(ERR, "Power Management Environment already set.");
-             rte_spinlock_unlock(&global_env_cfg_lock);
-             return -1;
-     }
-

<...>

Quoted 54 lines
lib/power/rte_power.c +17 -20
-     if (ret == 0)
-             global_default_env = env;
-     else {
-             global_default_env = PM_ENV_NOT_SET;
-             reset_power_function_ptrs();
-     }
+     RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+             if (strncmp(ops->name, power_env_str[env],
+                             RTE_POWER_DRIVER_NAMESZ) == 0) {
+                     global_power_core_ops = ops;
+                     global_default_env = env;
+                     ret = 0;
+                     goto out;
+             }
+     POWER_LOG(ERR, "Invalid Power Management Environment(%d) set",
+                     env);
 
+out:
      rte_spinlock_unlock(&global_env_cfg_lock);
      return ret;
  }
@@ -164,94 +97,66 @@ rte_power_unset_env(void)
  {
      rte_spinlock_lock(&global_env_cfg_lock);
      global_default_env = PM_ENV_NOT_SET;
-     reset_power_function_ptrs();
+     global_power_core_ops = NULL;
      rte_spinlock_unlock(&global_env_cfg_lock);
  }
 
  enum power_management_env
-rte_power_get_env(void) {
+rte_power_get_env(void)
+{
      return global_default_env;
  }
 
-int
-rte_power_init(unsigned int lcore_id)
+struct rte_power_core_ops *
+rte_power_get_core_ops(void)
  {
-     int ret = -1;
+     RTE_ASSERT(global_power_core_ops != NULL);
 
-     switch (global_default_env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_init(lcore_id);
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_init(lcore_id);
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_init(lcore_id);
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_init(lcore_id);

lib/power/rte_power.c rte_power_unset_env(void) · line 197

Quoted 10 lines
lib/power/rte_power.c +2 -7
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_init(lcore_id);
-     default:
-             POWER_LOG(INFO, "Env isn't set yet!");
-     }
+     return global_power_core_ops;
+}
 
-     /* Auto detect Environment */
-     POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq power

management...");

Quoted 10 lines
lib/power/rte_power.c +4 -5
-     ret = power_acpi_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
-             goto out;
-     }
+int
+rte_power_init(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops;
+     uint8_t env;
 

lib/power/rte_power.c rte_power_unset_env(void)

Quoted 10 lines
lib/power/rte_power.c +2 -7
-     POWER_LOG(INFO, "Attempting to initialise PSTAT power management...");
-     ret = power_pstate_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
-             goto out;
-     }
+     if (global_default_env != PM_ENV_NOT_SET)
+             return global_power_core_ops->init(lcore_id);
 
-     POWER_LOG(INFO, "Attempting to initialise AMD PSTATE power

management...");

lib/power/rte_power.c rte_power_unset_env(void) · line 127

Quoted 6 lines
lib/power/rte_power.c +1 -5
-     ret = power_amd_pstate_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_AMD_PSTATE_CPUFREQ);
-             goto out;
-     }
+     POWER_LOG(INFO, "Env isn't set yet!");

remove this log?

Quoted 12 lines
lib/power/rte_power.c +5 -6
-     POWER_LOG(INFO, "Attempting to initialise CPPC power management...");
-     ret = power_cppc_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_CPPC_CPUFREQ);
-             goto out;
-     }
+     /* Auto detect Environment */
+     RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+             if (ops) {
+                     POWER_LOG(INFO,
+                             "Attempting to initialise %s cpufreq power management...",

lib/power/rte_power.c rte_power_unset_env(void) · line 143

Quoted 10 lines
lib/power/rte_power.c +10 -0
+                             ops->name);
+                     if (ops->init(lcore_id) == 0) {
+                             for (env = 0; env < RTE_DIM(power_env_str); env++)
+                                     if (strncmp(ops->name, power_env_str[env],
+                                                     RTE_POWER_DRIVER_NAMESZ) == 0) {
+                                             rte_power_set_env(env);
+                                             return 0;
+                                     }
+                     }
+             }

Can we change the logic of rte_power_set_env()? like:

     RTE_TAILQ_FOREACH(ops, &core_ops_list, next) {
         for (env = 0; env < RTE_DIM(power_env_str); env++) {
                 if (strncmp(ops->name, power_env_str[env],
RTE_POWER_DRIVER_NAMESZ) == 0 &&
                     ops->init(lcore_id) == 0) {
                     global_power_core_ops = ops;
                     global_default_env = env;
                 }
         }
     }
Quoted 1 line

That is easier to follow code.

Yes, will fix in next version.

Quoted 89 lines

lib/power/rte_power.c rte_power_unset_env(void)

Quoted 12 lines
lib/power/rte_power.c +4 -7
+
+     POWER_LOG(ERR,
+             "Unable to set Power Management Environment for lcore %u",
+             lcore_id);
 
-     POWER_LOG(INFO, "Attempting to initialise VM power management...");
-     ret = power_kvm_vm_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_KVM_VM);
-             goto out;
-     }
-     POWER_LOG(ERR, "Unable to set Power Management Environment for lcore

"

Quoted 12 lines
lib/power/rte_power.c +1 -6
-                     "%u", lcore_id);
-out:
-     return ret;
+     return -1;
  }
 
  int
  rte_power_exit(unsigned int lcore_id)
  {
-     switch (global_default_env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_exit(lcore_id);

lib/power/rte_power.c rte_power_unset_env(void)

Quoted 10 lines
lib/power/rte_power.c +0 -10
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_exit(lcore_id);
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_exit(lcore_id);
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_exit(lcore_id);
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_exit(lcore_id);
-     default:
-             POWER_LOG(ERR, "Environment has not been set, unable to exit

gracefully");

Quoted 49 lines
lib/power/rte_power.c +6 -2
+     if (global_default_env != PM_ENV_NOT_SET)
+             return global_power_core_ops->exit(lcore_id);
 
-     }
-     return -1;
+     POWER_LOG(ERR,
+             "Environment has not been set, unable to exit
+ gracefully");
 
+     return -1;
  }
lib/power/rte_power.h index renamed from lib/power/rte_power.h +0 -0

4fa4afe399..5e4aacf08b 100644

lib/power/rte_power.h +11 -2
@@ -1,5 +1,6 @@
  /* SPDX-License-Identifier: BSD-3-Clause
   * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
   */
 
  #ifndef _RTE_POWER_H
@@ -14,14 +15,21 @@
  #include <rte_log.h>
  #include <rte_power_guest_channel.h>
 
+#include "rte_power_core_ops.h"
+
  #ifdef __cplusplus
  extern "C" {
  #endif
 
  /* Power Management Environment State */ -enum power_management_env
 {PM_ENV_NOT_SET, PM_ENV_ACPI_CPUFREQ, PM_ENV_KVM_VM,
-             PM_ENV_PSTATE_CPUFREQ, PM_ENV_CPPC_CPUFREQ,
-             PM_ENV_AMD_PSTATE_CPUFREQ};
+enum power_management_env {
+     PM_ENV_NOT_SET = 0,
+     PM_ENV_ACPI_CPUFREQ,
+     PM_ENV_KVM_VM,
+     PM_ENV_PSTATE_CPUFREQ,
+     PM_ENV_CPPC_CPUFREQ,
+     PM_ENV_AMD_PSTATE_CPUFREQ
+};
 
  /**
   * Check if a specific power management environment type is
supported on a @@ -66,6 +74,15 @@ void rte_power_unset_env(void);
   */
Quoted 1 line

enum power_management_env rte_power_get_env(void);

I'd like to let user not know used which cpufreq driver, which is friendly to user.

Quoted 1 line

So we can rethink if this API is necessary.

For any API changes, could we handle this as a separate RFC for discussion? It’s important that these changes are not included within the scope of this patch.

Quoted 253 lines
Quoted 44 lines
lib/power/rte_power.h +9 -0
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_core_ops *
+rte_power_get_core_ops(void);
+
  /**
   * Initialize power management for a specific lcore. If rte_power_set_env() has
   * not been called then an auto-detect of the environment will start

and @@ -108,10 +125,13 @@ int rte_power_exit(unsigned int lcore_id);
* @return
* The number of available frequencies.
*/

lib/power/rte_power.h +9 -5
-typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
-             uint32_t num);
+static inline uint32_t
+rte_power_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t n) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_freqs_t rte_power_freqs;
+     return ops->get_avail_freqs(lcore_id, freqs, n); }
 
  /**
   * Return the current index of available frequencies of a specific lcore.
@@ -124,9 +144,13 @@ extern rte_power_freqs_t rte_power_freqs;
   * @return
   *  The current index of available frequencies.
   */
-typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);
+static inline uint32_t
+rte_power_get_freq(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_get_freq_t rte_power_get_freq;
+     return ops->get_freq(lcore_id);
+}
 
  /**
   * Set the new frequency for a specific lcore by indicating the
Quoted 1 line

index of @@ -144,82 +168,101 @@ extern rte_power_get_freq_t

rte_power_get_freq;

Quoted 147 lines

* - 0 on success without frequency changed.
* - Negative on error.
*/
-typedef int (*rte_power_set_freq_t)(unsigned int lcore_id, uint32_t index);

lib/power/rte_power.h +3 -4
-
-extern rte_power_set_freq_t rte_power_set_freq;
+static inline uint32_t
+rte_power_set_freq(unsigned int lcore_id, uint32_t index) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-/**
- * Function pointer definition for generic frequency change

functions. Review
- * each environments specific documentation for usage.
- *
- * @param lcore_id
- * lcore id.
- *
- * @return

- *  - 1 on success with frequency changed.
- *  - 0 on success without frequency changed.
- *  - Negative on error.

- */

lib/power/rte_power.h +43 -23
-typedef int (*rte_power_freq_change_t)(unsigned int lcore_id);
+     return ops->set_freq(lcore_id, index); }
 
  /**
   * Scale up the frequency of a specific lcore according to the available
   * frequencies.
   * Review each environments specific documentation for usage.
   */
-extern rte_power_freq_change_t rte_power_freq_up;
+static inline int
+rte_power_freq_up(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_up(lcore_id);
+}
 
  /**
   * Scale down the frequency of a specific lcore according to the available
   * frequencies.
   * Review each environments specific documentation for usage.
   */
-extern rte_power_freq_change_t rte_power_freq_down;
+static inline int
+rte_power_freq_down(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_down(lcore_id); }
 
  /**
   * Scale up the frequency of a specific lcore to the highest according to the
   * available frequencies.
   * Review each environments specific documentation for usage.
   */
-extern rte_power_freq_change_t rte_power_freq_max;
+static inline int
+rte_power_freq_max(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_max(lcore_id);
+}
 
  /**
   * Scale down the frequency of a specific lcore to the lowest according to the
   * available frequencies.
   * Review each environments specific documentation for usage..
   */
-extern rte_power_freq_change_t rte_power_freq_min;
+static inline int
+rte_power_freq_min(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_min(lcore_id);
+}
 
  /**
   * Query the Turbo Boost status of a specific lcore.
   * Review each environments specific documentation for usage..
   */
-extern rte_power_freq_change_t rte_power_turbo_status;
+static inline int
+rte_power_turbo_status(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->turbo_status(lcore_id); }
 
  /**
   * Enable Turbo Boost for this lcore.
   * Review each environments specific documentation for usage..
   */
-extern rte_power_freq_change_t rte_power_freq_enable_turbo;
+static inline int
+rte_power_freq_enable_turbo(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->enable_turbo(lcore_id); }
 
  /**
   * Disable Turbo Boost for this lcore.
   * Review each environments specific documentation for usage..
   */
-extern rte_power_freq_change_t rte_power_freq_disable_turbo;
+static inline int
+rte_power_freq_disable_turbo(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-/**
- * Power capabilities summary.
- */
-struct rte_power_core_capabilities {
-     union {
-             uint64_t capabilities;
-             struct {
-                     uint64_t turbo:1;       /**< Turbo can be enabled. */
-                     uint64_t priority:1;    /**< SST-BF high freq core */
-             };
-     };
-};
+     return ops->disable_turbo(lcore_id); }
 
  /**
   * Returns power capabilities for a specific lcore.
@@ -235,10 +278,14 @@ struct rte_power_core_capabilities {
   *  - 0 on success.
   *  - Negative on error.
   */
-typedef int (*rte_power_get_capabilities_t)(unsigned int lcore_id,
-             struct rte_power_core_capabilities *caps);
+static inline int
+rte_power_get_capabilities(unsigned int lcore_id,
+             struct rte_power_core_capabilities *caps) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_get_capabilities_t rte_power_get_capabilities;
+     return ops->get_caps(lcore_id, caps); }
 
  #ifdef __cplusplus
  }
(quoted diff) +0 -0

b/lib/power/rte_power_core_ops.h new file mode 100644 index 0000000000..356a64df79

lib/power/rte_power_core_ops.h line 8

Quoted 8 lines
lib/power/rte_power_core_ops.h new file +8 -0
@@ -0,0 +1,208 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _RTE_POWER_CORE_OPS_H
+#define _RTE_POWER_CORE_OPS_H
+

suggest rename the file as rte_power_cpufreq_api.h. If so, the role of this file is more clearly.

Quoted 31 lines
lib/power/rte_power_core_ops.h new file +26 -0
+__rte_internal
+int rte_power_register_ops(struct rte_power_core_ops *ops);
+
+/**
+ * Macro to statically register the ops of a cpufreq driver.
+ */
+#define RTE_POWER_REGISTER_OPS(ops)          \
+     RTE_INIT(power_hdlr_init_##ops)         \
+     {                                       \
+             rte_power_register_ops(&ops);   \
+     }
+
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_core_ops *
+rte_power_get_core_ops(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
lib/power/version.map index renamed from lib/power/version.map +0 -0

c9a226614e..bd64e0828f 100644

lib/power/version.map EXPERIMENTAL { · line 54

Quoted 4 lines
lib/power/version.map +1 -0
@@ -51,4 +51,18 @@ EXPERIMENTAL {
      rte_power_set_uncore_env;
      rte_power_uncore_freqs;
      rte_power_unset_uncore_env;
+     # added in 24.07

24.07-->24.11?

Quoted 14 lines
lib/power/version.map +13 -0
+     rte_power_logtype;
+};
+
+INTERNAL {
+     global:
+
+     rte_power_register_ops;
+     cpufreq_check_scaling_driver;
+     power_set_governor;
+     open_core_sysfs_file;
+     read_core_sysfs_u32;
+     read_core_sysfs_s;
+     write_core_sysfs_s;
  };
#31 lore raw

Re: [PATCH v2 1/4] power: refactor core power management library

Quoted 107 lines · Sivaprasad

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Huisong,

Please find my response inline.

Quoted 93 lines

-----Original Message-----
From: lihuisong (C) <[email protected]>
Sent: Tuesday, August 27, 2024 1:51 PM
To: Tummala, Sivaprasad <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Subject: Re: [PATCH v2 1/4] power: refactor core power management library

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Hi Sivaprasa,

Some comments inline.

/Huisong

Quoted 45 lines · Sivaprasad Tummala

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.
These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

v2:
- added NULL check for global_core_ops in rte_power_get_core_ops

---
   drivers/meson.build                           |   1 +
   .../power/acpi/acpi_cpufreq.c                 |  22 +-
   .../power/acpi/acpi_cpufreq.h                 |   6 +-
   drivers/power/acpi/meson.build                |  10 +
   .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
   .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
   drivers/power/amd_pstate/meson.build          |  10 +
   .../power/cppc/cppc_cpufreq.c                 |  22 +-
   .../power/cppc/cppc_cpufreq.h                 |   8 +-
   drivers/power/cppc/meson.build                |  10 +
   .../power/kvm_vm}/guest_channel.c             |   0
   .../power/kvm_vm}/guest_channel.h             |   0
   .../power/kvm_vm/kvm_vm.c                     |  22 +-
   .../power/kvm_vm/kvm_vm.h                     |   6 +-
   drivers/power/kvm_vm/meson.build              |  16 +
   drivers/power/meson.build                     |  12 +
   drivers/power/pstate/meson.build              |  10 +
   .../power/pstate/pstate_cpufreq.c             |  22 +-
   .../power/pstate/pstate_cpufreq.h             |   6 +-
   lib/power/meson.build                         |   7 +-
   lib/power/power_common.c                      |   2 +-
   lib/power/power_common.h                      |  16 +-
   lib/power/rte_power.c                         | 291 ++++++------------
   lib/power/rte_power.h                         | 139 ++++++---
   lib/power/rte_power_core_ops.h                | 208 +++++++++++++
   lib/power/version.map                         |  14 +
   26 files changed, 621 insertions(+), 271 deletions(-)
Quoted 1 line

rename lib/power/power_acpi_cpufreq.c => drivers/power/acpi/acpi_cpufreq.c

(95%)

Quoted 1 line

rename lib/power/power_acpi_cpufreq.h => drivers/power/acpi/acpi_cpufreq.h

(98%)

Quoted 2 lines

create mode 100644 drivers/power/acpi/meson.build rename lib/power/power_amd_pstate_cpufreq.c =>

drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%)

Quoted 1 line

rename lib/power/power_amd_pstate_cpufreq.h =>

drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%)

Quoted 2 lines

create mode 100644 drivers/power/amd_pstate/meson.build
rename lib/power/power_cppc_cpufreq.c => drivers/power/cppc/cppc_cpufreq.c

(95%)

Quoted 1 line

rename lib/power/power_cppc_cpufreq.h => drivers/power/cppc/cppc_cpufreq.h

(97%)

Quoted 9 lines

create mode 100644 drivers/power/cppc/meson.build
rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%) rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%) rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c (82%) rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h (98%) create mode 100644 drivers/power/kvm_vm/meson.build
create mode 100644 drivers/power/meson.build
create mode 100644 drivers/power/pstate/meson.build
rename lib/power/power_pstate_cpufreq.c =>

drivers/power/pstate/pstate_cpufreq.c (96%)

Quoted 1 line

rename lib/power/power_pstate_cpufreq.h =>

drivers/power/pstate/pstate_cpufreq.h (98%)

Quoted 1 line

create mode 100644 lib/power/rte_power_core_ops.h

Quoted 8 lines

How about use the following directory structure?
*For power libs*
lib/power/power_common.*
lib/power/rte_power_pmd_mgmt.*
lib/power/rte_power_cpufreq_api.* (replacing rte_power.c file maybe simple for us. but I'm not sure if we can put the init of core, uncore and pmd mgmt to rte_power_init.c in rte_power.c.) lib/power/rte_power_uncore_freq_api.*

Quoted 2 lines

Yes, renaming rte_power.c is definitely a possible incremental change that could be considered later. However, for the time being, our focus will be on refactoring the cpufreq drivers only.

The rte_power.c just works for the initialization of cpufreq driver. Now that you are reworking core and uncore power library and rearrange the directory under power. I think renaming this file name should be more appropriate in this series.

Quoted 33 lines
Quoted 23 lines

*And has directories under drivers/power:*
1> For core dvfs driver:
drivers/power/cpufreq/acpi_cpufreq.c drivers/power/cpufreq/cppc_cpufreq.c drivers/power/cpufreq/amd_pstate_cpufreq.c drivers/power/cpufreq/intel_pstate_cpufreq.c drivers/power/cpufreq/kvm_cpufreq.c
The code of each cpufreq driver is not too much and doesn't probably increase. So don't need to use a directory for it.

2> For uncore dvfs driver:
drivers/power/uncorefreq/intel_uncore.*

Quoted 11 lines
drivers/meson.build index renamed from drivers/meson.build +0 -0

66931d4241..9d77e0deab 100644

drivers/meson.build +1 -0
@@ -29,6 +29,7 @@ subdirs = [
           'event',          # depends on common, bus, mempool and net.
           'baseband',       # depends on common and bus.
           'gpu',            # depends on common and bus.
+        'power',          # depends on common (in future).
   ]
 
Quoted 10 lines
Quoted 6 lines
drivers/meson.build +0 -0
   if meson.is_cross_build()
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c rename to +0 -0
similarity index 95%
rename from lib/power/power_acpi_cpufreq.c rename to

drivers/power/acpi/acpi_cpufreq.c

do not suggest to create one directory for each cpufreq driver. Because pstate drivers also comply with ACPI spec, right? In addition, the code of each cpufreq drivers are not too much. There is just one file under one directory which is not good.

Quoted 2 lines

One of our objectives for the refactoring is to selectively disable non-essential drivers using Meson build options. However, by rearranging the driver structure, we risk disrupting this capability.

I get your purpose.
The cpufreq library has the feature and interface to detect which driver to use, right?
So it is not necessary for cpufreq library to introduce the Meson build options, which probably makes it complicate.

Quoted 90 lines
Quoted 12 lines
Quoted 11 lines

index 81996e1c13..8637c69703 100644

drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +1 -1
@@ -10,7 +10,7 @@
   #include <rte_stdatomic.h>
   #include <rte_string_fns.h>
 
-#include "power_acpi_cpufreq.h"
+#include "acpi_cpufreq.h"
   #include "power_common.h"
 

<...>

Quoted 14 lines
Quoted 11 lines
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +7 -0
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('pstate_cpufreq.c')
+
+deps += ['power']
drivers/power/pstate/pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c rename to +0 -0
similarity index 96%
rename from lib/power/power_pstate_cpufreq.c rename to
Quoted 1 line

drivers/power/pstate/pstate_cpufreq.c

pstate_cpufreq.c is actually intel_pstate cpufreq driver, right? So how about modify this file name to intel_pstate_cpufreq.c?

Yes, will fix this in next version.

Quoted 54 lines
Quoted 17 lines

index 2343121621..c32b1adabc 100644

drivers/power/pstate/pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c +3 -1
@@ -15,7 +15,7 @@
   #include <rte_stdatomic.h>
 
   #include "rte_power_pmd_mgmt.h"
-#include "power_pstate_cpufreq.h"
+#include "pstate_cpufreq.h"
   #include "power_common.h"
 
   /* macros used for rounding frequency to nearest 100000 */ @@ -888,3
+888,23 @@ int power_pstate_get_capabilities(unsigned int lcore_id,
 
       return 0;
   }
+

<...>

Quoted 36 lines
lib/power/power_common.c index renamed from lib/power/power_common.c +0 -0

590986d5ef..6c06411e8b 100644

lib/power/power_common.c +1 -1
@@ -12,7 +12,7 @@
 
   #include "power_common.h"
 
-RTE_LOG_REGISTER_DEFAULT(power_logtype, INFO);
+RTE_LOG_REGISTER_DEFAULT(rte_power_logtype, INFO);
 
   #define POWER_SYSFILE_SCALING_DRIVER   \
               "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_driver"
lib/power/power_common.h index renamed from lib/power/power_common.h +0 -0

83f742f42a..767686ee12 100644

lib/power/power_common.h +3 -2
@@ -6,12 +6,13 @@
   #define _POWER_COMMON_H_
 
   #include <rte_common.h>
+#include <rte_compat.h>
   #include <rte_log.h>
 
   #define RTE_POWER_INVALID_FREQ_INDEX (~0)
 
-extern int power_logtype;
-#define RTE_LOGTYPE_POWER power_logtype
+extern int rte_power_logtype;
+#define RTE_LOGTYPE_POWER rte_power_logtype
   #define POWER_LOG(level, ...) \
       RTE_LOG_LINE(level, POWER, "" __VA_ARGS__)
 
@@ -23,13 +24,24 @@ extern int power_logtype;
   #endif
 
Quoted 9 lines

lib/power/power_common.h extern int power_logtype; · line 32

Quoted 7 lines
lib/power/power_common.h +3 -0
   /* check if scaling driver matches one we want */
+__rte_internal
   int cpufreq_check_scaling_driver(const char *driver);
+
+__rte_internal
   int power_set_governor(unsigned int lcore_id, const char *new_governor,
               char *orig_governor, size_t orig_governor_len);

suggest that move cpufreq interfaces like this to the rte_power_cpufreq_api.* I proposed above.

Quoted 2 lines

This is an internal API and isn’t intended for direct use by applications. By moving it to rte_power_*, we risk exposing it inadvertently.

we don't expose these to applications. application do not include this header file.
power_set_governor() and cpufreq_check_scaling_driver() is just used by cpufreq driver. So they just can be seen by cpufreq lib or module, right? But if these interface are in power_common.h, pmd_mgmt and uncore driver also include this header file and can see them. This is not good. AFAIS, the power_common.h should just contain the kind of interfaces that are used by all power libs or sub-modules, like cpufreq, uncore, pmd_mgmt and so on.

Quoted 99 lines
Quoted 17 lines

The interfaces in power_comm.* can be used by all power modules, like core/uncore/pmd mgmt.

Quoted 14 lines
lib/power/power_common.h +8 -0
+
+__rte_internal
   int open_core_sysfs_file(FILE **f, const char *mode, const char *format, ...)
               __rte_format_printf(3, 4);
+
+__rte_internal
   int read_core_sysfs_u32(FILE *f, uint32_t *val);
+
+__rte_internal
   int read_core_sysfs_s(FILE *f, char *buf, unsigned int len);
+
+__rte_internal
   int write_core_sysfs_s(FILE *f, const char *str);
 
Quoted 5 lines

lib/power/rte_power.c

Quoted 2 lines
lib/power/power_common.h +0 -0
   #endif /* _POWER_COMMON_H_ */
lib/power/rte_power.c +0 -0

The name of the rte_power.c file is impropriate now. The context in this file is just for cpufreq, right?
So I suggest that we need to rename this file as the rte_power_cpufreq_api.c

Yes, renaming rte_power.c to rte_power_cpufreq.c is definitely a possible incremental change and will fix this as a separate patch.
.

Quoted 65 lines
Quoted 6 lines

index 36c3f3da98..2bf6d40517 100644

lib/power/rte_power.c +0 -0
@@ -8,153 +8,86 @@
   #include <rte_spinlock.h>
 

lib/power/rte_power.c line 13

Quoted 10 lines
lib/power/rte_power.c +1 -6
   #include "rte_power.h"
-#include "power_acpi_cpufreq.h"
-#include "power_cppc_cpufreq.h"
   #include "power_common.h"
-#include "power_kvm_vm.h"
-#include "power_pstate_cpufreq.h"
-#include "power_amd_pstate_cpufreq.h"
 
-enum power_management_env global_default_env = PM_ENV_NOT_SET;
+static enum power_management_env global_default_env =

PM_ENV_NOT_SET;

Quoted 48 lines
lib/power/rte_power.c +3 -2
+static struct rte_power_core_ops *global_power_core_ops;
 
   static rte_spinlock_t global_env_cfg_lock =
 RTE_SPINLOCK_INITIALIZER;
+static RTE_TAILQ_HEAD(, rte_power_core_ops) core_ops_list =
+                     TAILQ_HEAD_INITIALIZER(core_ops_list);
 
-/* function pointers */
-rte_power_freqs_t rte_power_freqs  = NULL; -rte_power_get_freq_t

rte_power_get_freq = NULL; -rte_power_set_freq_t rte_power_set_freq = NULL; -rte_power_freq_change_t rte_power_freq_up = NULL;

lib/power/rte_power.c +15 -21
-rte_power_freq_change_t rte_power_freq_down = NULL;
-rte_power_freq_change_t rte_power_freq_max = NULL;
-rte_power_freq_change_t rte_power_freq_min = NULL;
-rte_power_freq_change_t rte_power_turbo_status;
-rte_power_freq_change_t rte_power_freq_enable_turbo;
-rte_power_freq_change_t rte_power_freq_disable_turbo;
-rte_power_get_capabilities_t rte_power_get_capabilities;
-
-static void
-reset_power_function_ptrs(void)
+
+const char *power_env_str[] = {
+     "not set",
+     "acpi",
+     "kvm-vm",
+     "pstate",
+     "cppc",
+     "amd-pstate"
+};
+
+/* register the ops struct in rte_power_core_ops, return 0 on
+success. */ int rte_power_register_ops(struct rte_power_core_ops
+*driver_ops)
   {
-     rte_power_freqs  = NULL;
-     rte_power_get_freq = NULL;
-     rte_power_set_freq = NULL;
-     rte_power_freq_up = NULL;
-     rte_power_freq_down = NULL;
-     rte_power_freq_max = NULL;
-     rte_power_freq_min = NULL;
-     rte_power_turbo_status = NULL;
-     rte_power_freq_enable_turbo = NULL;
-     rte_power_freq_disable_turbo = NULL;
-     rte_power_get_capabilities = NULL;
+     if (!driver_ops->init || !driver_ops->exit ||
+             !driver_ops->check_env_support || !driver_ops->get_avail_freqs ||
Quoted 8 lines

lib/power/rte_power.c

Quoted 7 lines
lib/power/rte_power.c +7 -0
+             !driver_ops->get_freq || !driver_ops->set_freq ||
+             !driver_ops->freq_up || !driver_ops->freq_down ||
+             !driver_ops->freq_max || !driver_ops->freq_min ||
+             !driver_ops->turbo_status || !driver_ops->enable_turbo ||
+             !driver_ops->disable_turbo || !driver_ops->get_caps) {
+             POWER_LOG(ERR, "Missing callbacks while registering
+ power ops");

turbo_status(), enable_turbo() and disable turbo() are not necessary, right?

Quoted 1 line

Nope, this is required to get the current status unlike the capability API (get_caps()).

ok

Quoted 271 lines
Quoted 180 lines

These depand on the capabilities from get_caps().

Quoted 53 lines
lib/power/rte_power.c +20 -19
+             return -EINVAL;
+     }
+
+     TAILQ_INSERT_TAIL(&core_ops_list, driver_ops, next);
+
+     return 0;
   }
 
   int
   rte_power_check_env_supported(enum power_management_env env)
   {
-     switch (env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_check_supported();
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_check_supported();
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_check_supported();
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_check_supported();
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_check_supported();
-     default:
-             rte_errno = EINVAL;
-             return -1;
-     }
+     struct rte_power_core_ops *ops;
+
+     if (env >= RTE_DIM(power_env_str))
+             return 0;
+
+     RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+             if (strncmp(ops->name, power_env_str[env],
+                             RTE_POWER_DRIVER_NAMESZ) == 0)
+                     return ops->check_env_support();
+
+     return 0;
   }
 
   int
   rte_power_set_env(enum power_management_env env)
   {
+     struct rte_power_core_ops *ops;
+     int ret = -1;
+
       rte_spinlock_lock(&global_env_cfg_lock);
 
       if (global_default_env != PM_ENV_NOT_SET) {
               POWER_LOG(ERR, "Power Management Environment already set.");
-             rte_spinlock_unlock(&global_env_cfg_lock);
-             return -1;
-     }
-

<...>

Quoted 54 lines
lib/power/rte_power.c +17 -20
-     if (ret == 0)
-             global_default_env = env;
-     else {
-             global_default_env = PM_ENV_NOT_SET;
-             reset_power_function_ptrs();
-     }
+     RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+             if (strncmp(ops->name, power_env_str[env],
+                             RTE_POWER_DRIVER_NAMESZ) == 0) {
+                     global_power_core_ops = ops;
+                     global_default_env = env;
+                     ret = 0;
+                     goto out;
+             }
+     POWER_LOG(ERR, "Invalid Power Management Environment(%d) set",
+                     env);
 
+out:
       rte_spinlock_unlock(&global_env_cfg_lock);
       return ret;
   }
@@ -164,94 +97,66 @@ rte_power_unset_env(void)
   {
       rte_spinlock_lock(&global_env_cfg_lock);
       global_default_env = PM_ENV_NOT_SET;
-     reset_power_function_ptrs();
+     global_power_core_ops = NULL;
       rte_spinlock_unlock(&global_env_cfg_lock);
   }
 
   enum power_management_env
-rte_power_get_env(void) {
+rte_power_get_env(void)
+{
       return global_default_env;
   }
 
-int
-rte_power_init(unsigned int lcore_id)
+struct rte_power_core_ops *
+rte_power_get_core_ops(void)
   {
-     int ret = -1;
+     RTE_ASSERT(global_power_core_ops != NULL);
 
-     switch (global_default_env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_init(lcore_id);
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_init(lcore_id);
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_init(lcore_id);
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_init(lcore_id);

lib/power/rte_power.c rte_power_unset_env(void) · line 197

Quoted 10 lines
lib/power/rte_power.c +2 -7
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_init(lcore_id);
-     default:
-             POWER_LOG(INFO, "Env isn't set yet!");
-     }
+     return global_power_core_ops;
+}
 
-     /* Auto detect Environment */
-     POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq power

management...");

Quoted 10 lines
lib/power/rte_power.c +4 -5
-     ret = power_acpi_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
-             goto out;
-     }
+int
+rte_power_init(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops;
+     uint8_t env;
 

lib/power/rte_power.c rte_power_unset_env(void)

Quoted 10 lines
lib/power/rte_power.c +2 -7
-     POWER_LOG(INFO, "Attempting to initialise PSTAT power management...");
-     ret = power_pstate_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
-             goto out;
-     }
+     if (global_default_env != PM_ENV_NOT_SET)
+             return global_power_core_ops->init(lcore_id);
 
-     POWER_LOG(INFO, "Attempting to initialise AMD PSTATE power

management...");

lib/power/rte_power.c rte_power_unset_env(void) · line 127

Quoted 6 lines
lib/power/rte_power.c +1 -5
-     ret = power_amd_pstate_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_AMD_PSTATE_CPUFREQ);
-             goto out;
-     }
+     POWER_LOG(INFO, "Env isn't set yet!");

remove this log?

Quoted 11 lines
lib/power/rte_power.c +5 -6
-     POWER_LOG(INFO, "Attempting to initialise CPPC power management...");
-     ret = power_cppc_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_CPPC_CPUFREQ);
-             goto out;
-     }
+     /* Auto detect Environment */
+     RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+             if (ops) {
+                     POWER_LOG(INFO,
+                             "Attempting to initialise %s cpufreq power management...",

lib/power/rte_power.c rte_power_unset_env(void) · line 143

Quoted 10 lines
lib/power/rte_power.c +10 -0
+                             ops->name);
+                     if (ops->init(lcore_id) == 0) {
+                             for (env = 0; env < RTE_DIM(power_env_str); env++)
+                                     if (strncmp(ops->name, power_env_str[env],
+                                                     RTE_POWER_DRIVER_NAMESZ) == 0) {
+                                             rte_power_set_env(env);
+                                             return 0;
+                                     }
+                     }
+             }

Can we change the logic of rte_power_set_env()? like:

      RTE_TAILQ_FOREACH(ops, &core_ops_list, next) {
          for (env = 0; env < RTE_DIM(power_env_str); env++) {
                  if (strncmp(ops->name, power_env_str[env],
RTE_POWER_DRIVER_NAMESZ) == 0 &&
                      ops->init(lcore_id) == 0) {
                      global_power_core_ops = ops;
                      global_default_env = env;
                  }
          }
      }
Quoted 1 line

That is easier to follow code.

Yes, will fix in next version.

Quoted 88 lines

lib/power/rte_power.c rte_power_unset_env(void)

Quoted 12 lines
lib/power/rte_power.c +4 -7
+
+     POWER_LOG(ERR,
+             "Unable to set Power Management Environment for lcore %u",
+             lcore_id);
 
-     POWER_LOG(INFO, "Attempting to initialise VM power management...");
-     ret = power_kvm_vm_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_KVM_VM);
-             goto out;
-     }
-     POWER_LOG(ERR, "Unable to set Power Management Environment for lcore

"

Quoted 12 lines
lib/power/rte_power.c +1 -6
-                     "%u", lcore_id);
-out:
-     return ret;
+     return -1;
   }
 
   int
   rte_power_exit(unsigned int lcore_id)
   {
-     switch (global_default_env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_exit(lcore_id);

lib/power/rte_power.c rte_power_unset_env(void)

Quoted 10 lines
lib/power/rte_power.c +0 -10
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_exit(lcore_id);
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_exit(lcore_id);
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_exit(lcore_id);
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_exit(lcore_id);
-     default:
-             POWER_LOG(ERR, "Environment has not been set, unable to exit

gracefully");

Quoted 49 lines
lib/power/rte_power.c +6 -2
+     if (global_default_env != PM_ENV_NOT_SET)
+             return global_power_core_ops->exit(lcore_id);
 
-     }
-     return -1;
+     POWER_LOG(ERR,
+             "Environment has not been set, unable to exit
+ gracefully");
 
+     return -1;
   }
lib/power/rte_power.h index renamed from lib/power/rte_power.h +0 -0

4fa4afe399..5e4aacf08b 100644

lib/power/rte_power.h +11 -2
@@ -1,5 +1,6 @@
   /* SPDX-License-Identifier: BSD-3-Clause
    * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
    */
 
   #ifndef _RTE_POWER_H
@@ -14,14 +15,21 @@
   #include <rte_log.h>
   #include <rte_power_guest_channel.h>
 
+#include "rte_power_core_ops.h"
+
   #ifdef __cplusplus
   extern "C" {
   #endif
 
   /* Power Management Environment State */ -enum power_management_env
 {PM_ENV_NOT_SET, PM_ENV_ACPI_CPUFREQ, PM_ENV_KVM_VM,
-             PM_ENV_PSTATE_CPUFREQ, PM_ENV_CPPC_CPUFREQ,
-             PM_ENV_AMD_PSTATE_CPUFREQ};
+enum power_management_env {
+     PM_ENV_NOT_SET = 0,
+     PM_ENV_ACPI_CPUFREQ,
+     PM_ENV_KVM_VM,
+     PM_ENV_PSTATE_CPUFREQ,
+     PM_ENV_CPPC_CPUFREQ,
+     PM_ENV_AMD_PSTATE_CPUFREQ
+};
 
   /**
    * Check if a specific power management environment type is
supported on a @@ -66,6 +74,15 @@ void rte_power_unset_env(void);
    */
Quoted 1 line

enum power_management_env rte_power_get_env(void);

I'd like to let user not know used which cpufreq driver, which is friendly to user.

Quoted 3 lines
Quoted 1 line

So we can rethink if this API is necessary.

For any API changes, could we handle this as a separate RFC for discussion? It’s important that these changes are not included within the scope of this patch.

Agreed.
Can you post a separate RFC to disscuss this improvement later?

Quoted 251 lines
Quoted 251 lines
Quoted 42 lines
lib/power/rte_power.h +9 -0
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_core_ops *
+rte_power_get_core_ops(void);
+
   /**
    * Initialize power management for a specific lcore. If rte_power_set_env() has
    * not been called then an auto-detect of the environment will start

and @@ -108,10 +125,13 @@ int rte_power_exit(unsigned int lcore_id);
* @return
* The number of available frequencies.
*/

lib/power/rte_power.h +9 -5
-typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
-             uint32_t num);
+static inline uint32_t
+rte_power_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t n) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_freqs_t rte_power_freqs;
+     return ops->get_avail_freqs(lcore_id, freqs, n); }
 
   /**
    * Return the current index of available frequencies of a specific lcore.
@@ -124,9 +144,13 @@ extern rte_power_freqs_t rte_power_freqs;
    * @return
    *  The current index of available frequencies.
    */
-typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);
+static inline uint32_t
+rte_power_get_freq(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_get_freq_t rte_power_get_freq;
+     return ops->get_freq(lcore_id);
+}
 
   /**
    * Set the new frequency for a specific lcore by indicating the
Quoted 1 line

index of @@ -144,82 +168,101 @@ extern rte_power_get_freq_t

rte_power_get_freq;

Quoted 147 lines

* - 0 on success without frequency changed.
* - Negative on error.
*/
-typedef int (*rte_power_set_freq_t)(unsigned int lcore_id, uint32_t index);

lib/power/rte_power.h +3 -4
-
-extern rte_power_set_freq_t rte_power_set_freq;
+static inline uint32_t
+rte_power_set_freq(unsigned int lcore_id, uint32_t index) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-/**
- * Function pointer definition for generic frequency change

functions. Review
- * each environments specific documentation for usage.
- *
- * @param lcore_id
- * lcore id.
- *
- * @return

- *  - 1 on success with frequency changed.
- *  - 0 on success without frequency changed.
- *  - Negative on error.

- */

lib/power/rte_power.h +43 -23
-typedef int (*rte_power_freq_change_t)(unsigned int lcore_id);
+     return ops->set_freq(lcore_id, index); }
 
   /**
    * Scale up the frequency of a specific lcore according to the available
    * frequencies.
    * Review each environments specific documentation for usage.
    */
-extern rte_power_freq_change_t rte_power_freq_up;
+static inline int
+rte_power_freq_up(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_up(lcore_id);
+}
 
   /**
    * Scale down the frequency of a specific lcore according to the available
    * frequencies.
    * Review each environments specific documentation for usage.
    */
-extern rte_power_freq_change_t rte_power_freq_down;
+static inline int
+rte_power_freq_down(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_down(lcore_id); }
 
   /**
    * Scale up the frequency of a specific lcore to the highest according to the
    * available frequencies.
    * Review each environments specific documentation for usage.
    */
-extern rte_power_freq_change_t rte_power_freq_max;
+static inline int
+rte_power_freq_max(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_max(lcore_id);
+}
 
   /**
    * Scale down the frequency of a specific lcore to the lowest according to the
    * available frequencies.
    * Review each environments specific documentation for usage..
    */
-extern rte_power_freq_change_t rte_power_freq_min;
+static inline int
+rte_power_freq_min(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_min(lcore_id);
+}
 
   /**
    * Query the Turbo Boost status of a specific lcore.
    * Review each environments specific documentation for usage..
    */
-extern rte_power_freq_change_t rte_power_turbo_status;
+static inline int
+rte_power_turbo_status(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->turbo_status(lcore_id); }
 
   /**
    * Enable Turbo Boost for this lcore.
    * Review each environments specific documentation for usage..
    */
-extern rte_power_freq_change_t rte_power_freq_enable_turbo;
+static inline int
+rte_power_freq_enable_turbo(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->enable_turbo(lcore_id); }
 
   /**
    * Disable Turbo Boost for this lcore.
    * Review each environments specific documentation for usage..
    */
-extern rte_power_freq_change_t rte_power_freq_disable_turbo;
+static inline int
+rte_power_freq_disable_turbo(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-/**
- * Power capabilities summary.
- */
-struct rte_power_core_capabilities {
-     union {
-             uint64_t capabilities;
-             struct {
-                     uint64_t turbo:1;       /**< Turbo can be enabled. */
-                     uint64_t priority:1;    /**< SST-BF high freq core */
-             };
-     };
-};
+     return ops->disable_turbo(lcore_id); }
 
   /**
    * Returns power capabilities for a specific lcore.
@@ -235,10 +278,14 @@ struct rte_power_core_capabilities {
    *  - 0 on success.
    *  - Negative on error.
    */
-typedef int (*rte_power_get_capabilities_t)(unsigned int lcore_id,
-             struct rte_power_core_capabilities *caps);
+static inline int
+rte_power_get_capabilities(unsigned int lcore_id,
+             struct rte_power_core_capabilities *caps) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_get_capabilities_t rte_power_get_capabilities;
+     return ops->get_caps(lcore_id, caps); }
 
   #ifdef __cplusplus
   }
(quoted diff) +0 -0

b/lib/power/rte_power_core_ops.h new file mode 100644 index 0000000000..356a64df79

lib/power/rte_power_core_ops.h line 8

Quoted 8 lines
lib/power/rte_power_core_ops.h new file +8 -0
@@ -0,0 +1,208 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _RTE_POWER_CORE_OPS_H
+#define _RTE_POWER_CORE_OPS_H
+

suggest rename the file as rte_power_cpufreq_api.h. If so, the role of this file is more clearly.

Quoted 31 lines
lib/power/rte_power_core_ops.h new file +26 -0
+__rte_internal
+int rte_power_register_ops(struct rte_power_core_ops *ops);
+
+/**
+ * Macro to statically register the ops of a cpufreq driver.
+ */
+#define RTE_POWER_REGISTER_OPS(ops)          \
+     RTE_INIT(power_hdlr_init_##ops)         \
+     {                                       \
+             rte_power_register_ops(&ops);   \
+     }
+
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_core_ops *
+rte_power_get_core_ops(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
lib/power/version.map index renamed from lib/power/version.map +0 -0

c9a226614e..bd64e0828f 100644

lib/power/version.map EXPERIMENTAL { · line 54

Quoted 4 lines
lib/power/version.map +1 -0
@@ -51,4 +51,18 @@ EXPERIMENTAL {
       rte_power_set_uncore_env;
       rte_power_uncore_freqs;
       rte_power_unset_uncore_env;
+     # added in 24.07

24.07-->24.11?

Quoted 14 lines
lib/power/version.map +13 -0
+     rte_power_logtype;
+};
+
+INTERNAL {
+     global:
+
+     rte_power_register_ops;
+     cpufreq_check_scaling_driver;
+     power_set_governor;
+     open_core_sysfs_file;
+     read_core_sysfs_u32;
+     read_core_sysfs_s;
+     write_core_sysfs_s;
   };
#32 lore raw

RE: [PATCH v2 1/4] power: refactor core power management library

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 170 lines

-----Original Message-----
From: lihuisong (C) <[email protected]>
Sent: Friday, September 13, 2024 1:05 PM
To: Tummala, Sivaprasad <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Subject: Re: [PATCH v2 1/4] power: refactor core power management library

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 95 lines · Sivaprasad

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Huisong,

Please find my response inline.

Quoted 89 lines

-----Original Message-----
From: lihuisong (C) <[email protected]>
Sent: Tuesday, August 27, 2024 1:51 PM
To: Tummala, Sivaprasad <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Subject: Re: [PATCH v2 1/4] power: refactor core power management library

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Hi Sivaprasa,

Some comments inline.

/Huisong

Quoted 45 lines · Sivaprasad Tummala

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.
These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

v2:
- added NULL check for global_core_ops in rte_power_get_core_ops

---
   drivers/meson.build                           |   1 +
   .../power/acpi/acpi_cpufreq.c                 |  22 +-
   .../power/acpi/acpi_cpufreq.h                 |   6 +-
   drivers/power/acpi/meson.build                |  10 +
   .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
   .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
   drivers/power/amd_pstate/meson.build          |  10 +
   .../power/cppc/cppc_cpufreq.c                 |  22 +-
   .../power/cppc/cppc_cpufreq.h                 |   8 +-
   drivers/power/cppc/meson.build                |  10 +
   .../power/kvm_vm}/guest_channel.c             |   0
   .../power/kvm_vm}/guest_channel.h             |   0
   .../power/kvm_vm/kvm_vm.c                     |  22 +-
   .../power/kvm_vm/kvm_vm.h                     |   6 +-
   drivers/power/kvm_vm/meson.build              |  16 +
   drivers/power/meson.build                     |  12 +
   drivers/power/pstate/meson.build              |  10 +
   .../power/pstate/pstate_cpufreq.c             |  22 +-
   .../power/pstate/pstate_cpufreq.h             |   6 +-
   lib/power/meson.build                         |   7 +-
   lib/power/power_common.c                      |   2 +-
   lib/power/power_common.h                      |  16 +-
   lib/power/rte_power.c                         | 291 ++++++------------
   lib/power/rte_power.h                         | 139 ++++++---
   lib/power/rte_power_core_ops.h                | 208 +++++++++++++
   lib/power/version.map                         |  14 +
   26 files changed, 621 insertions(+), 271 deletions(-)
Quoted 2 lines

rename lib/power/power_acpi_cpufreq.c =>
drivers/power/acpi/acpi_cpufreq.c

(95%)

Quoted 2 lines

rename lib/power/power_acpi_cpufreq.h =>
drivers/power/acpi/acpi_cpufreq.h

(98%)

Quoted 2 lines

create mode 100644 drivers/power/acpi/meson.build rename lib/power/power_amd_pstate_cpufreq.c =>

drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%)

Quoted 1 line

rename lib/power/power_amd_pstate_cpufreq.h =>

drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%)

Quoted 3 lines

create mode 100644 drivers/power/amd_pstate/meson.build rename lib/power/power_cppc_cpufreq.c =>
drivers/power/cppc/cppc_cpufreq.c

(95%)

Quoted 2 lines

rename lib/power/power_cppc_cpufreq.h =>
drivers/power/cppc/cppc_cpufreq.h

(97%)

Quoted 4 lines

create mode 100644 drivers/power/cppc/meson.build rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%) rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%) rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c

(82%)

Quoted 1 line
Quoted 1 line
Quoted 1 line

rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h

(98%)

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

create mode 100644 drivers/power/kvm_vm/meson.build create mode 100644 drivers/power/meson.build create mode 100644 drivers/power/pstate/meson.build rename lib/power/power_pstate_cpufreq.c =>

drivers/power/pstate/pstate_cpufreq.c (96%)

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

rename lib/power/power_pstate_cpufreq.h =>

drivers/power/pstate/pstate_cpufreq.h (98%)

Quoted 1 line

create mode 100644 lib/power/rte_power_core_ops.h

How about use the following directory structure?
*For power libs*
lib/power/power_common.*
lib/power/rte_power_pmd_mgmt.*
lib/power/rte_power_cpufreq_api.* (replacing rte_power.c file maybe simple for

us.

Quoted 4 lines
Quoted 3 lines

but I'm not sure if we can put the init of core, uncore and pmd mgmt to rte_power_init.c in rte_power.c.) lib/power/rte_power_uncore_freq_api.*

Yes, renaming rte_power.c is definitely a possible incremental change that could

be considered later.

Quoted 1 line

However, for the time being, our focus will be on refactoring the cpufreq drivers

only.
The rte_power.c just works for the initialization of cpufreq driver. Now that you are reworking core and uncore power library and rearrange the directory under power. I think renaming this file name should be more appropriate in this series.

Quoted 33 lines
Quoted 33 lines

*And has directories under drivers/power:*
1> For core dvfs driver:
drivers/power/cpufreq/acpi_cpufreq.c drivers/power/cpufreq/cppc_cpufreq.c drivers/power/cpufreq/amd_pstate_cpufreq.c drivers/power/cpufreq/intel_pstate_cpufreq.c drivers/power/cpufreq/kvm_cpufreq.c
The code of each cpufreq driver is not too much and doesn't probably increase. So don't need to use a directory for it.

2> For uncore dvfs driver:
drivers/power/uncorefreq/intel_uncore.*

Quoted 16 lines
drivers/meson.build index renamed from drivers/meson.build +0 -0

66931d4241..9d77e0deab 100644

drivers/meson.build +1 -0
@@ -29,6 +29,7 @@ subdirs = [
           'event',          # depends on common, bus, mempool and net.
           'baseband',       # depends on common and bus.
           'gpu',            # depends on common and bus.
+        'power',          # depends on common (in future).
   ]
 
   if meson.is_cross_build()
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c rename to +0 -0
similarity index 95%
rename from lib/power/power_acpi_cpufreq.c rename to
Quoted 1 line

drivers/power/acpi/acpi_cpufreq.c

do not suggest to create one directory for each cpufreq driver. Because pstate drivers also comply with ACPI spec, right? In addition, the code of each cpufreq drivers are not too much. There is just one file under one directory which is not good.

Quoted 7 lines
Quoted 1 line

One of our objectives for the refactoring is to selectively disable non-essential

drivers using Meson build options.

Quoted 1 line

However, by rearranging the driver structure, we risk disrupting this capability.

I get your purpose.
The cpufreq library has the feature and interface to detect which driver to use, right? So it is not necessary for cpufreq library to introduce the Meson build options, which probably makes it complicate.

In Meson, you can reduce code size by disabling specific drivers or components through build options, allowing you to exclude unnecessary features. At runtime, the library will automatically detect the available driver, and if it's not present in the build, initialization will fail.
We're not introducing any new complexities; rather, we aim to ensure that the drivers in drivers/power/* are consistent with the other drivers.

Quoted 94 lines
Quoted 92 lines
Quoted 12 lines
Quoted 11 lines

index 81996e1c13..8637c69703 100644

drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +1 -1
@@ -10,7 +10,7 @@
   #include <rte_stdatomic.h>
   #include <rte_string_fns.h>
 
-#include "power_acpi_cpufreq.h"
+#include "acpi_cpufreq.h"
   #include "power_common.h"
 

<...>

Quoted 14 lines
Quoted 11 lines
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +7 -0
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('pstate_cpufreq.c')
+
+deps += ['power']
drivers/power/pstate/pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c rename to +0 -0
similarity index 96%
rename from lib/power/power_pstate_cpufreq.c rename to
Quoted 1 line

drivers/power/pstate/pstate_cpufreq.c

pstate_cpufreq.c is actually intel_pstate cpufreq driver, right? So how about modify this file name to intel_pstate_cpufreq.c?

Yes, will fix this in next version.

Quoted 56 lines
Quoted 18 lines

index 2343121621..c32b1adabc 100644

drivers/power/pstate/pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c +3 -2
@@ -15,7 +15,7 @@
   #include <rte_stdatomic.h>
 
   #include "rte_power_pmd_mgmt.h"
-#include "power_pstate_cpufreq.h"
+#include "pstate_cpufreq.h"
   #include "power_common.h"
 
   /* macros used for rounding frequency to nearest 100000 */ @@
-888,3
+888,23 @@ int power_pstate_get_capabilities(unsigned int lcore_id,
 
       return 0;
   }
+

<...>

Quoted 37 lines
lib/power/power_common.c +1 -1
index 590986d5ef..6c06411e8b 100644
@@ -12,7 +12,7 @@
 
   #include "power_common.h"
 
-RTE_LOG_REGISTER_DEFAULT(power_logtype, INFO);
+RTE_LOG_REGISTER_DEFAULT(rte_power_logtype, INFO);
 
   #define POWER_SYSFILE_SCALING_DRIVER   \
               "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_driver"
lib/power/power_common.h +0 -0

index
83f742f42a..767686ee12 100644

lib/power/power_common.h +3 -2
@@ -6,12 +6,13 @@
   #define _POWER_COMMON_H_
 
   #include <rte_common.h>
+#include <rte_compat.h>
   #include <rte_log.h>
 
   #define RTE_POWER_INVALID_FREQ_INDEX (~0)
 
-extern int power_logtype;
-#define RTE_LOGTYPE_POWER power_logtype
+extern int rte_power_logtype;
+#define RTE_LOGTYPE_POWER rte_power_logtype
   #define POWER_LOG(level, ...) \
       RTE_LOG_LINE(level, POWER, "" __VA_ARGS__)
 
@@ -23,13 +24,24 @@ extern int power_logtype;
   #endif
 
Quoted 9 lines

lib/power/power_common.h extern int power_logtype; · line 32

Quoted 7 lines
lib/power/power_common.h +3 -0
   /* check if scaling driver matches one we want */
+__rte_internal
   int cpufreq_check_scaling_driver(const char *driver);
+
+__rte_internal
   int power_set_governor(unsigned int lcore_id, const char *new_governor,
               char *orig_governor, size_t orig_governor_len);

suggest that move cpufreq interfaces like this to the rte_power_cpufreq_api.* I proposed above.

Quoted 2 lines

This is an internal API and isn’t intended for direct use by applications. By moving it to rte_power_*, we risk exposing it inadvertently.

Quoted 7 lines

we don't expose these to applications. application do not include this header file. power_set_governor() and cpufreq_check_scaling_driver() is just used by cpufreq driver. So they just can be seen by cpufreq lib or module, right?
But if these interface are in power_common.h, pmd_mgmt and uncore driver also include this header file and can see them. This is not good.
AFAIS, the power_common.h should just contain the kind of interfaces that are used by all power libs or sub-modules, like cpufreq, uncore, pmd_mgmt and so on.

OK., Will move this internal APIs from power_common.h to a separate header file.

Quoted 632 lines
Quoted 100 lines
Quoted 17 lines

The interfaces in power_comm.* can be used by all power modules, like core/uncore/pmd mgmt.

Quoted 14 lines
lib/power/power_common.h +8 -0
+
+__rte_internal
   int open_core_sysfs_file(FILE **f, const char *mode, const char *format, ...)
               __rte_format_printf(3, 4);
+
+__rte_internal
   int read_core_sysfs_u32(FILE *f, uint32_t *val);
+
+__rte_internal
   int read_core_sysfs_s(FILE *f, char *buf, unsigned int len);
+
+__rte_internal
   int write_core_sysfs_s(FILE *f, const char *str);
 
Quoted 6 lines

lib/power/rte_power.c

Quoted 2 lines
lib/power/power_common.h +0 -0
   #endif /* _POWER_COMMON_H_ */
lib/power/rte_power.c +0 -0

The name of the rte_power.c file is impropriate now. The context in this file is just for cpufreq, right?
So I suggest that we need to rename this file as the rte_power_cpufreq_api.c

Yes, renaming rte_power.c to rte_power_cpufreq.c is definitely a possible incremental change and will fix this as a separate patch. .

Quoted 65 lines
Quoted 6 lines

index 36c3f3da98..2bf6d40517 100644

lib/power/rte_power.c +0 -0
@@ -8,153 +8,86 @@
   #include <rte_spinlock.h>
 

lib/power/rte_power.c line 13

Quoted 10 lines
lib/power/rte_power.c +1 -6
   #include "rte_power.h"
-#include "power_acpi_cpufreq.h"
-#include "power_cppc_cpufreq.h"
   #include "power_common.h"
-#include "power_kvm_vm.h"
-#include "power_pstate_cpufreq.h"
-#include "power_amd_pstate_cpufreq.h"
 
-enum power_management_env global_default_env = PM_ENV_NOT_SET;
+static enum power_management_env global_default_env =

PM_ENV_NOT_SET;

Quoted 48 lines
lib/power/rte_power.c +3 -2
+static struct rte_power_core_ops *global_power_core_ops;
 
   static rte_spinlock_t global_env_cfg_lock =
 RTE_SPINLOCK_INITIALIZER;
+static RTE_TAILQ_HEAD(, rte_power_core_ops) core_ops_list =
+                     TAILQ_HEAD_INITIALIZER(core_ops_list);
 
-/* function pointers */
-rte_power_freqs_t rte_power_freqs  = NULL; -rte_power_get_freq_t

rte_power_get_freq = NULL; -rte_power_set_freq_t rte_power_set_freq = NULL; -rte_power_freq_change_t rte_power_freq_up = NULL;

lib/power/rte_power.c +15 -21
-rte_power_freq_change_t rte_power_freq_down = NULL;
-rte_power_freq_change_t rte_power_freq_max = NULL;
-rte_power_freq_change_t rte_power_freq_min = NULL;
-rte_power_freq_change_t rte_power_turbo_status;
-rte_power_freq_change_t rte_power_freq_enable_turbo;
-rte_power_freq_change_t rte_power_freq_disable_turbo;
-rte_power_get_capabilities_t rte_power_get_capabilities;
-
-static void
-reset_power_function_ptrs(void)
+
+const char *power_env_str[] = {
+     "not set",
+     "acpi",
+     "kvm-vm",
+     "pstate",
+     "cppc",
+     "amd-pstate"
+};
+
+/* register the ops struct in rte_power_core_ops, return 0 on
+success. */ int rte_power_register_ops(struct rte_power_core_ops
+*driver_ops)
   {
-     rte_power_freqs  = NULL;
-     rte_power_get_freq = NULL;
-     rte_power_set_freq = NULL;
-     rte_power_freq_up = NULL;
-     rte_power_freq_down = NULL;
-     rte_power_freq_max = NULL;
-     rte_power_freq_min = NULL;
-     rte_power_turbo_status = NULL;
-     rte_power_freq_enable_turbo = NULL;
-     rte_power_freq_disable_turbo = NULL;
-     rte_power_get_capabilities = NULL;
+     if (!driver_ops->init || !driver_ops->exit ||
+             !driver_ops->check_env_support || !driver_ops->get_avail_freqs ||
Quoted 8 lines

lib/power/rte_power.c

Quoted 7 lines
lib/power/rte_power.c +7 -0
+             !driver_ops->get_freq || !driver_ops->set_freq ||
+             !driver_ops->freq_up || !driver_ops->freq_down ||
+             !driver_ops->freq_max || !driver_ops->freq_min ||
+             !driver_ops->turbo_status || !driver_ops->enable_turbo ||
+             !driver_ops->disable_turbo || !driver_ops->get_caps) {
+             POWER_LOG(ERR, "Missing callbacks while registering
+ power ops");

turbo_status(), enable_turbo() and disable turbo() are not necessary, right?

Quoted 1 line

Nope, this is required to get the current status unlike the capability API

(get_caps()).
ok

Quoted 48 lines
Quoted 48 lines

These depand on the capabilities from get_caps().

Quoted 47 lines
lib/power/rte_power.c +20 -15
+             return -EINVAL;
+     }
+
+     TAILQ_INSERT_TAIL(&core_ops_list, driver_ops, next);
+
+     return 0;
   }
 
   int
   rte_power_check_env_supported(enum power_management_env env)
   {
-     switch (env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_check_supported();
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_check_supported();
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_check_supported();
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_check_supported();
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_check_supported();
-     default:
-             rte_errno = EINVAL;
-             return -1;
-     }
+     struct rte_power_core_ops *ops;
+
+     if (env >= RTE_DIM(power_env_str))
+             return 0;
+
+     RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+             if (strncmp(ops->name, power_env_str[env],
+                             RTE_POWER_DRIVER_NAMESZ) == 0)
+                     return ops->check_env_support();
+
+     return 0;
   }
 
   int
   rte_power_set_env(enum power_management_env env)
   {
+     struct rte_power_core_ops *ops;
+     int ret = -1;
+
       rte_spinlock_lock(&global_env_cfg_lock);
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

if (global_default_env != PM_ENV_NOT_SET) {
POWER_LOG(ERR, "Power Management Environment already

set.");

Quoted 80 lines
Quoted 80 lines
Quoted 4 lines
lib/power/rte_power.c +0 -4
-             rte_spinlock_unlock(&global_env_cfg_lock);
-             return -1;
-     }
-

<...>

Quoted 54 lines
lib/power/rte_power.c +17 -20
-     if (ret == 0)
-             global_default_env = env;
-     else {
-             global_default_env = PM_ENV_NOT_SET;
-             reset_power_function_ptrs();
-     }
+     RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+             if (strncmp(ops->name, power_env_str[env],
+                             RTE_POWER_DRIVER_NAMESZ) == 0) {
+                     global_power_core_ops = ops;
+                     global_default_env = env;
+                     ret = 0;
+                     goto out;
+             }
+     POWER_LOG(ERR, "Invalid Power Management Environment(%d) set",
+                     env);
 
+out:
       rte_spinlock_unlock(&global_env_cfg_lock);
       return ret;
   }
@@ -164,94 +97,66 @@ rte_power_unset_env(void)
   {
       rte_spinlock_lock(&global_env_cfg_lock);
       global_default_env = PM_ENV_NOT_SET;
-     reset_power_function_ptrs();
+     global_power_core_ops = NULL;
       rte_spinlock_unlock(&global_env_cfg_lock);
   }
 
   enum power_management_env
-rte_power_get_env(void) {
+rte_power_get_env(void)
+{
       return global_default_env;
   }
 
-int
-rte_power_init(unsigned int lcore_id)
+struct rte_power_core_ops *
+rte_power_get_core_ops(void)
   {
-     int ret = -1;
+     RTE_ASSERT(global_power_core_ops != NULL);
 
-     switch (global_default_env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_init(lcore_id);
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_init(lcore_id);
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_init(lcore_id);
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_init(lcore_id);

lib/power/rte_power.c rte_power_unset_env(void) · line 197

Quoted 10 lines
lib/power/rte_power.c +2 -7
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_init(lcore_id);
-     default:
-             POWER_LOG(INFO, "Env isn't set yet!");
-     }
+     return global_power_core_ops;
+}
 
-     /* Auto detect Environment */
-     POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq power

management...");

Quoted 10 lines
lib/power/rte_power.c +4 -5
-     ret = power_acpi_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
-             goto out;
-     }
+int
+rte_power_init(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops;
+     uint8_t env;
 
Quoted 1 line
Quoted 1 line

lib/power/rte_power.c rte_power_unset_env(void) · line 204

Quoted 1 line
lib/power/rte_power.c +0 -1
-     POWER_LOG(INFO, "Attempting to initialise PSTAT power

management...");

Quoted 10 lines
Quoted 10 lines

lib/power/rte_power.c rte_power_unset_env(void)

Quoted 9 lines
lib/power/rte_power.c +2 -6
-     ret = power_pstate_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
-             goto out;
-     }
+     if (global_default_env != PM_ENV_NOT_SET)
+             return global_power_core_ops->init(lcore_id);
 
-     POWER_LOG(INFO, "Attempting to initialise AMD PSTATE power

management...");

Quoted 8 lines
Quoted 8 lines

lib/power/rte_power.c rte_power_unset_env(void) · line 127

Quoted 6 lines
lib/power/rte_power.c +1 -5
-     ret = power_amd_pstate_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_AMD_PSTATE_CPUFREQ);
-             goto out;
-     }
+     POWER_LOG(INFO, "Env isn't set yet!");

remove this log?

Quoted 1 line
lib/power/rte_power.c +0 -1
-     POWER_LOG(INFO, "Attempting to initialise CPPC power

management...");

Quoted 32 lines
Quoted 31 lines
Quoted 10 lines
lib/power/rte_power.c +5 -5
-     ret = power_cppc_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_CPPC_CPUFREQ);
-             goto out;
-     }
+     /* Auto detect Environment */
+     RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+             if (ops) {
+                     POWER_LOG(INFO,
+                             "Attempting to initialise %s cpufreq power management...",

lib/power/rte_power.c rte_power_unset_env(void) · line 143

Quoted 10 lines
lib/power/rte_power.c +10 -0
+                             ops->name);
+                     if (ops->init(lcore_id) == 0) {
+                             for (env = 0; env < RTE_DIM(power_env_str); env++)
+                                     if (strncmp(ops->name, power_env_str[env],
+                                                     RTE_POWER_DRIVER_NAMESZ) == 0) {
+                                             rte_power_set_env(env);
+                                             return 0;
+                                     }
+                     }
+             }

Can we change the logic of rte_power_set_env()? like:

      RTE_TAILQ_FOREACH(ops, &core_ops_list, next) {
          for (env = 0; env < RTE_DIM(power_env_str); env++) {
                  if (strncmp(ops->name, power_env_str[env],
RTE_POWER_DRIVER_NAMESZ) == 0 &&
                      ops->init(lcore_id) == 0) {
                      global_power_core_ops = ops;
                      global_default_env = env;
                  }
          }
      }
Quoted 1 line

That is easier to follow code.

Quoted 14 lines

Yes, will fix in next version.

Quoted 12 lines
Quoted 12 lines
lib/power/rte_power.c +4 -7
+
+     POWER_LOG(ERR,
+             "Unable to set Power Management Environment for lcore %u",
+             lcore_id);
 
-     POWER_LOG(INFO, "Attempting to initialise VM power management...");
-     ret = power_kvm_vm_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_KVM_VM);
-             goto out;
-     }
-     POWER_LOG(ERR, "Unable to set Power Management Environment for

lcore

Quoted 50 lines
Quoted 50 lines

"

Quoted 12 lines
lib/power/rte_power.c +1 -6
-                     "%u", lcore_id);
-out:
-     return ret;
+     return -1;
   }
 
   int
   rte_power_exit(unsigned int lcore_id)
   {
-     switch (global_default_env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_exit(lcore_id);

lib/power/rte_power.c rte_power_unset_env(void)

Quoted 10 lines
lib/power/rte_power.c +0 -10
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_exit(lcore_id);
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_exit(lcore_id);
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_exit(lcore_id);
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_exit(lcore_id);
-     default:
-             POWER_LOG(ERR, "Environment has not been set, unable to exit

gracefully");

Quoted 26 lines
lib/power/rte_power.c +6 -2
+     if (global_default_env != PM_ENV_NOT_SET)
+             return global_power_core_ops->exit(lcore_id);
 
-     }
-     return -1;
+     POWER_LOG(ERR,
+             "Environment has not been set, unable to exit
+ gracefully");
 
+     return -1;
   }
lib/power/rte_power.h index renamed from lib/power/rte_power.h +0 -0

4fa4afe399..5e4aacf08b 100644

lib/power/rte_power.h +1 -0
@@ -1,5 +1,6 @@
   /* SPDX-License-Identifier: BSD-3-Clause
    * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
    */
 
   #ifndef _RTE_POWER_H
@@ -14,14 +15,21 @@
   #include <rte_log.h>
   #include <rte_power_guest_channel.h>
 
Quoted 8 lines
Quoted 8 lines

lib/power/rte_power.h

Quoted 8 lines
lib/power/rte_power.h +2 -0
+#include "rte_power_core_ops.h"
+
   #ifdef __cplusplus
   extern "C" {
   #endif
 
   /* Power Management Environment State */ -enum
 power_management_env {PM_ENV_NOT_SET, PM_ENV_ACPI_CPUFREQ,

PM_ENV_KVM_VM,

Quoted 18 lines
Quoted 18 lines
Quoted 15 lines
lib/power/rte_power.h +8 -2
-             PM_ENV_PSTATE_CPUFREQ, PM_ENV_CPPC_CPUFREQ,
-             PM_ENV_AMD_PSTATE_CPUFREQ};
+enum power_management_env {
+     PM_ENV_NOT_SET = 0,
+     PM_ENV_ACPI_CPUFREQ,
+     PM_ENV_KVM_VM,
+     PM_ENV_PSTATE_CPUFREQ,
+     PM_ENV_CPPC_CPUFREQ,
+     PM_ENV_AMD_PSTATE_CPUFREQ
+};
 
   /**
    * Check if a specific power management environment type is
supported on a @@ -66,6 +74,15 @@ void rte_power_unset_env(void);
    */
Quoted 1 line

enum power_management_env rte_power_get_env(void);

I'd like to let user not know used which cpufreq driver, which is friendly to user.

Quoted 3 lines
Quoted 1 line

So we can rethink if this API is necessary.

For any API changes, could we handle this as a separate RFC for discussion? It’s important that these changes are not included within the scope of this patch.

Agreed.
Can you post a separate RFC to disscuss this improvement later?

Quoted 11 lines
Quoted 11 lines

lib/power/rte_power.h void rte_power_unset_env(void);

Quoted 11 lines
lib/power/rte_power.h +9 -0
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_core_ops *
+rte_power_get_core_ops(void);
+
   /**
    * Initialize power management for a specific lcore. If rte_power_set_env()

has

Quoted 236 lines
Quoted 236 lines
Quoted 30 lines

* not been called then an auto-detect of the environment will
start and @@ -108,10 +125,13 @@ int rte_power_exit(unsigned int lcore_id);
* @return
* The number of available frequencies.
*/

lib/power/rte_power.h +8 -5
-typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
-             uint32_t num);
+static inline uint32_t
+rte_power_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t n) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_freqs_t rte_power_freqs;
+     return ops->get_avail_freqs(lcore_id, freqs, n); }
 
   /**
    * Return the current index of available frequencies of a specific lcore.
@@ -124,9 +144,13 @@ extern rte_power_freqs_t rte_power_freqs;
    * @return
    *  The current index of available frequencies.
    */
-typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);
+static inline uint32_t
+rte_power_get_freq(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_get_freq_t rte_power_get_freq;
+     return ops->get_freq(lcore_id); }
 
   /**
    * Set the new frequency for a specific lcore by indicating the
Quoted 1 line

index of @@ -144,82 +168,101 @@ extern rte_power_get_freq_t

rte_power_get_freq;

Quoted 144 lines

* - 0 on success without frequency changed.
* - Negative on error.
*/
-typedef int (*rte_power_set_freq_t)(unsigned int lcore_id, uint32_t index);

lib/power/rte_power.h +3 -4
-
-extern rte_power_set_freq_t rte_power_set_freq;
+static inline uint32_t
+rte_power_set_freq(unsigned int lcore_id, uint32_t index) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-/**
- * Function pointer definition for generic frequency change

functions. Review
- * each environments specific documentation for usage.
- *
- * @param lcore_id
- * lcore id.
- *
- * @return

- *  - 1 on success with frequency changed.
- *  - 0 on success without frequency changed.
- *  - Negative on error.

- */

lib/power/rte_power.h +40 -23
-typedef int (*rte_power_freq_change_t)(unsigned int lcore_id);
+     return ops->set_freq(lcore_id, index); }
 
   /**
    * Scale up the frequency of a specific lcore according to the available
    * frequencies.
    * Review each environments specific documentation for usage.
    */
-extern rte_power_freq_change_t rte_power_freq_up;
+static inline int
+rte_power_freq_up(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_up(lcore_id); }
 
   /**
    * Scale down the frequency of a specific lcore according to the available
    * frequencies.
    * Review each environments specific documentation for usage.
    */
-extern rte_power_freq_change_t rte_power_freq_down;
+static inline int
+rte_power_freq_down(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_down(lcore_id); }
 
   /**
    * Scale up the frequency of a specific lcore to the highest according to the
    * available frequencies.
    * Review each environments specific documentation for usage.
    */
-extern rte_power_freq_change_t rte_power_freq_max;
+static inline int
+rte_power_freq_max(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_max(lcore_id); }
 
   /**
    * Scale down the frequency of a specific lcore to the lowest according to the
    * available frequencies.
    * Review each environments specific documentation for usage..
    */
-extern rte_power_freq_change_t rte_power_freq_min;
+static inline int
+rte_power_freq_min(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_min(lcore_id); }
 
   /**
    * Query the Turbo Boost status of a specific lcore.
    * Review each environments specific documentation for usage..
    */
-extern rte_power_freq_change_t rte_power_turbo_status;
+static inline int
+rte_power_turbo_status(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->turbo_status(lcore_id); }
 
   /**
    * Enable Turbo Boost for this lcore.
    * Review each environments specific documentation for usage..
    */
-extern rte_power_freq_change_t rte_power_freq_enable_turbo;
+static inline int
+rte_power_freq_enable_turbo(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->enable_turbo(lcore_id); }
 
   /**
    * Disable Turbo Boost for this lcore.
    * Review each environments specific documentation for usage..
    */
-extern rte_power_freq_change_t rte_power_freq_disable_turbo;
+static inline int
+rte_power_freq_disable_turbo(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-/**
- * Power capabilities summary.
- */
-struct rte_power_core_capabilities {
-     union {
-             uint64_t capabilities;
-             struct {
-                     uint64_t turbo:1;       /**< Turbo can be enabled. */
-                     uint64_t priority:1;    /**< SST-BF high freq core */
-             };
-     };
-};
+     return ops->disable_turbo(lcore_id); }
 
   /**
    * Returns power capabilities for a specific lcore.
@@ -235,10 +278,14 @@ struct rte_power_core_capabilities {
    *  - 0 on success.
    *  - Negative on error.
    */
-typedef int (*rte_power_get_capabilities_t)(unsigned int lcore_id,
-             struct rte_power_core_capabilities *caps);
+static inline int
+rte_power_get_capabilities(unsigned int lcore_id,
+             struct rte_power_core_capabilities *caps) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_get_capabilities_t rte_power_get_capabilities;
+     return ops->get_caps(lcore_id, caps); }
 
   #ifdef __cplusplus
   }
(quoted diff) +0 -0

b/lib/power/rte_power_core_ops.h new file mode 100644 index 0000000000..356a64df79

lib/power/rte_power_core_ops.h line 8

Quoted 8 lines
lib/power/rte_power_core_ops.h new file +8 -0
@@ -0,0 +1,208 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _RTE_POWER_CORE_OPS_H
+#define _RTE_POWER_CORE_OPS_H
+

suggest rename the file as rte_power_cpufreq_api.h. If so, the role of this file is more clearly.

Quoted 31 lines
lib/power/rte_power_core_ops.h new file +26 -0
+__rte_internal
+int rte_power_register_ops(struct rte_power_core_ops *ops);
+
+/**
+ * Macro to statically register the ops of a cpufreq driver.
+ */
+#define RTE_POWER_REGISTER_OPS(ops)          \
+     RTE_INIT(power_hdlr_init_##ops)         \
+     {                                       \
+             rte_power_register_ops(&ops);   \
+     }
+
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_core_ops *
+rte_power_get_core_ops(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
lib/power/version.map index renamed from lib/power/version.map +0 -0

c9a226614e..bd64e0828f 100644

lib/power/version.map EXPERIMENTAL { · line 54

Quoted 4 lines
lib/power/version.map +1 -0
@@ -51,4 +51,18 @@ EXPERIMENTAL {
       rte_power_set_uncore_env;
       rte_power_uncore_freqs;
       rte_power_unset_uncore_env;
+     # added in 24.07

24.07-->24.11?

Quoted 14 lines
lib/power/version.map +13 -0
+     rte_power_logtype;
+};
+
+INTERNAL {
+     global:
+
+     rte_power_register_ops;
+     cpufreq_check_scaling_driver;
+     power_set_governor;
+     open_core_sysfs_file;
+     read_core_sysfs_u32;
+     read_core_sysfs_s;
+     write_core_sysfs_s;
   };
#33 lore raw

Re: [PATCH v2 1/4] power: refactor core power management library

Quoted 174 lines · Sivaprasad

[AMD Official Use Only - AMD Internal Distribution Only]

Quoted 172 lines

-----Original Message-----
From: lihuisong (C) <[email protected]>
Sent: Friday, September 13, 2024 1:05 PM
To: Tummala, Sivaprasad <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Subject: Re: [PATCH v2 1/4] power: refactor core power management library

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Quoted 95 lines · Sivaprasad

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Huisong,

Please find my response inline.

Quoted 89 lines

-----Original Message-----
From: lihuisong (C) <[email protected]>
Sent: Tuesday, August 27, 2024 1:51 PM
To: Tummala, Sivaprasad <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Subject: Re: [PATCH v2 1/4] power: refactor core power management library

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Hi Sivaprasa,

Some comments inline.

/Huisong

Quoted 45 lines · Sivaprasad Tummala

This patch introduces a comprehensive refactor to the core power management library. The primary focus is on improving modularity and organization by relocating specific driver implementations from the 'lib/power' directory to dedicated directories within 'drivers/power/core/*'. The adjustment of meson.build files enables the selective activation of individual drivers.
These changes contribute to a significant enhancement in code organization, providing a clearer structure for driver implementations. The refactor aims to improve overall code clarity and boost maintainability. Additionally, it establishes a foundation for future development, allowing for more focused work on individual drivers and seamless integration of forthcoming enhancements.

v2:
- added NULL check for global_core_ops in rte_power_get_core_ops

---
    drivers/meson.build                           |   1 +
    .../power/acpi/acpi_cpufreq.c                 |  22 +-
    .../power/acpi/acpi_cpufreq.h                 |   6 +-
    drivers/power/acpi/meson.build                |  10 +
    .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
    .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
    drivers/power/amd_pstate/meson.build          |  10 +
    .../power/cppc/cppc_cpufreq.c                 |  22 +-
    .../power/cppc/cppc_cpufreq.h                 |   8 +-
    drivers/power/cppc/meson.build                |  10 +
    .../power/kvm_vm}/guest_channel.c             |   0
    .../power/kvm_vm}/guest_channel.h             |   0
    .../power/kvm_vm/kvm_vm.c                     |  22 +-
    .../power/kvm_vm/kvm_vm.h                     |   6 +-
    drivers/power/kvm_vm/meson.build              |  16 +
    drivers/power/meson.build                     |  12 +
    drivers/power/pstate/meson.build              |  10 +
    .../power/pstate/pstate_cpufreq.c             |  22 +-
    .../power/pstate/pstate_cpufreq.h             |   6 +-
    lib/power/meson.build                         |   7 +-
    lib/power/power_common.c                      |   2 +-
    lib/power/power_common.h                      |  16 +-
    lib/power/rte_power.c                         | 291 ++++++------------
    lib/power/rte_power.h                         | 139 ++++++---
    lib/power/rte_power_core_ops.h                | 208 +++++++++++++
    lib/power/version.map                         |  14 +
    26 files changed, 621 insertions(+), 271 deletions(-)
Quoted 2 lines

rename lib/power/power_acpi_cpufreq.c =>
drivers/power/acpi/acpi_cpufreq.c

(95%)

Quoted 2 lines

rename lib/power/power_acpi_cpufreq.h =>
drivers/power/acpi/acpi_cpufreq.h

(98%)

Quoted 2 lines

create mode 100644 drivers/power/acpi/meson.build rename lib/power/power_amd_pstate_cpufreq.c =>

drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%)

Quoted 1 line

rename lib/power/power_amd_pstate_cpufreq.h =>

drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%)

Quoted 3 lines

create mode 100644 drivers/power/amd_pstate/meson.build rename lib/power/power_cppc_cpufreq.c =>
drivers/power/cppc/cppc_cpufreq.c

(95%)

Quoted 2 lines

rename lib/power/power_cppc_cpufreq.h =>
drivers/power/cppc/cppc_cpufreq.h

(97%)

Quoted 4 lines

create mode 100644 drivers/power/cppc/meson.build rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%) rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%) rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c

(82%)

Quoted 1 line
Quoted 1 line
Quoted 1 line

rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h

(98%)

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines

create mode 100644 drivers/power/kvm_vm/meson.build create mode 100644 drivers/power/meson.build create mode 100644 drivers/power/pstate/meson.build rename lib/power/power_pstate_cpufreq.c =>

drivers/power/pstate/pstate_cpufreq.c (96%)

Quoted 8 lines
Quoted 8 lines
Quoted 1 line

rename lib/power/power_pstate_cpufreq.h =>

drivers/power/pstate/pstate_cpufreq.h (98%)

Quoted 1 line

create mode 100644 lib/power/rte_power_core_ops.h

How about use the following directory structure?
*For power libs*
lib/power/power_common.*
lib/power/rte_power_pmd_mgmt.*
lib/power/rte_power_cpufreq_api.* (replacing rte_power.c file maybe simple for

us.

Quoted 4 lines
Quoted 3 lines

but I'm not sure if we can put the init of core, uncore and pmd mgmt to rte_power_init.c in rte_power.c.) lib/power/rte_power_uncore_freq_api.*

Yes, renaming rte_power.c is definitely a possible incremental change that could

be considered later.

Quoted 1 line

However, for the time being, our focus will be on refactoring the cpufreq drivers

only.
The rte_power.c just works for the initialization of cpufreq driver. Now that you are reworking core and uncore power library and rearrange the directory under power. I think renaming this file name should be more appropriate in this series.

Quoted 33 lines
Quoted 23 lines

*And has directories under drivers/power:*
1> For core dvfs driver:
drivers/power/cpufreq/acpi_cpufreq.c drivers/power/cpufreq/cppc_cpufreq.c drivers/power/cpufreq/amd_pstate_cpufreq.c drivers/power/cpufreq/intel_pstate_cpufreq.c drivers/power/cpufreq/kvm_cpufreq.c
The code of each cpufreq driver is not too much and doesn't probably increase. So don't need to use a directory for it.

2> For uncore dvfs driver:
drivers/power/uncorefreq/intel_uncore.*

Quoted 11 lines
drivers/meson.build index renamed from drivers/meson.build +0 -0

66931d4241..9d77e0deab 100644

drivers/meson.build +1 -0
@@ -29,6 +29,7 @@ subdirs = [
            'event',          # depends on common, bus, mempool and net.
            'baseband',       # depends on common and bus.
            'gpu',            # depends on common and bus.
+        'power',          # depends on common (in future).
    ]
 
Quoted 10 lines
Quoted 6 lines
drivers/meson.build +0 -0
    if meson.is_cross_build()
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c rename to +0 -0
similarity index 95%
rename from lib/power/power_acpi_cpufreq.c rename to

drivers/power/acpi/acpi_cpufreq.c

do not suggest to create one directory for each cpufreq driver. Because pstate drivers also comply with ACPI spec, right? In addition, the code of each cpufreq drivers are not too much. There is just one file under one directory which is not good.

Quoted 1 line

One of our objectives for the refactoring is to selectively disable non-essential

drivers using Meson build options.

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

However, by rearranging the driver structure, we risk disrupting this capability.

I get your purpose.
The cpufreq library has the feature and interface to detect which driver to use, right? So it is not necessary for cpufreq library to introduce the Meson build options, which probably makes it complicate.

In Meson, you can reduce code size by disabling specific drivers or components through build options, allowing you to exclude unnecessary features. At runtime, the library will automatically detect the available driver, and if it's not present in the build, initialization will fail.

I still cannot understand why you want to do this.
The reducing code size is not a good reason. If all libraries or drivers want to do it like this, need to add more meson options.
Unless there is a situation where multiple drivers on a platform can be used, and the automatic detection mechanism is not enough to determine which driver to use. Of course, you also can see the opinion of other reviewers.

Quoted 2 lines

We're not introducing any new complexities; rather, we aim to ensure that the drivers in drivers/power/* are consistent with the other drivers.

What do the other drivers stand for?
Anyway, we need to make the directory hierarchy under drivers/power/ and lib/power/ clear.

Quoted 733 lines
Quoted 94 lines
Quoted 92 lines
Quoted 12 lines
Quoted 11 lines

index 81996e1c13..8637c69703 100644

drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +1 -1
@@ -10,7 +10,7 @@
    #include <rte_stdatomic.h>
    #include <rte_string_fns.h>
 
-#include "power_acpi_cpufreq.h"
+#include "acpi_cpufreq.h"
    #include "power_common.h"
 

<...>

Quoted 14 lines
Quoted 11 lines
drivers/power/acpi/acpi_cpufreq.c renamed from lib/power/power_acpi_cpufreq.c +7 -0
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+endif
+sources = files('pstate_cpufreq.c')
+
+deps += ['power']
drivers/power/pstate/pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c rename to +0 -0
similarity index 96%
rename from lib/power/power_pstate_cpufreq.c rename to
Quoted 1 line

drivers/power/pstate/pstate_cpufreq.c

pstate_cpufreq.c is actually intel_pstate cpufreq driver, right? So how about modify this file name to intel_pstate_cpufreq.c?

Yes, will fix this in next version.

Quoted 56 lines
Quoted 18 lines

index 2343121621..c32b1adabc 100644

drivers/power/pstate/pstate_cpufreq.c renamed from lib/power/power_pstate_cpufreq.c +3 -2
@@ -15,7 +15,7 @@
    #include <rte_stdatomic.h>
 
    #include "rte_power_pmd_mgmt.h"
-#include "power_pstate_cpufreq.h"
+#include "pstate_cpufreq.h"
    #include "power_common.h"
 
    /* macros used for rounding frequency to nearest 100000 */ @@
-888,3
+888,23 @@ int power_pstate_get_capabilities(unsigned int lcore_id,
 
        return 0;
    }
+

<...>

Quoted 37 lines
lib/power/power_common.c +1 -1
index 590986d5ef..6c06411e8b 100644
@@ -12,7 +12,7 @@
 
    #include "power_common.h"
 
-RTE_LOG_REGISTER_DEFAULT(power_logtype, INFO);
+RTE_LOG_REGISTER_DEFAULT(rte_power_logtype, INFO);
 
    #define POWER_SYSFILE_SCALING_DRIVER   \
                "/sys/devices/system/cpu/cpu%u/cpufreq/scaling_driver"
lib/power/power_common.h +0 -0

index
83f742f42a..767686ee12 100644

lib/power/power_common.h +3 -2
@@ -6,12 +6,13 @@
    #define _POWER_COMMON_H_
 
    #include <rte_common.h>
+#include <rte_compat.h>
    #include <rte_log.h>
 
    #define RTE_POWER_INVALID_FREQ_INDEX (~0)
 
-extern int power_logtype;
-#define RTE_LOGTYPE_POWER power_logtype
+extern int rte_power_logtype;
+#define RTE_LOGTYPE_POWER rte_power_logtype
    #define POWER_LOG(level, ...) \
        RTE_LOG_LINE(level, POWER, "" __VA_ARGS__)
 
@@ -23,13 +24,24 @@ extern int power_logtype;
    #endif
 
Quoted 9 lines

lib/power/power_common.h extern int power_logtype; · line 32

Quoted 7 lines
lib/power/power_common.h +3 -0
    /* check if scaling driver matches one we want */
+__rte_internal
    int cpufreq_check_scaling_driver(const char *driver);
+
+__rte_internal
    int power_set_governor(unsigned int lcore_id, const char *new_governor,
                char *orig_governor, size_t orig_governor_len);

suggest that move cpufreq interfaces like this to the rte_power_cpufreq_api.* I proposed above.

Quoted 2 lines

This is an internal API and isn’t intended for direct use by applications. By moving it to rte_power_*, we risk exposing it inadvertently.

Quoted 7 lines

we don't expose these to applications. application do not include this header file. power_set_governor() and cpufreq_check_scaling_driver() is just used by cpufreq driver. So they just can be seen by cpufreq lib or module, right?
But if these interface are in power_common.h, pmd_mgmt and uncore driver also include this header file and can see them. This is not good.
AFAIS, the power_common.h should just contain the kind of interfaces that are used by all power libs or sub-modules, like cpufreq, uncore, pmd_mgmt and so on.

OK., Will move this internal APIs from power_common.h to a separate header file.

Quoted 631 lines
Quoted 99 lines
Quoted 16 lines

The interfaces in power_comm.* can be used by all power modules, like core/uncore/pmd mgmt.

Quoted 14 lines
lib/power/power_common.h +8 -0
+
+__rte_internal
    int open_core_sysfs_file(FILE **f, const char *mode, const char *format, ...)
                __rte_format_printf(3, 4);
+
+__rte_internal
    int read_core_sysfs_u32(FILE *f, uint32_t *val);
+
+__rte_internal
    int read_core_sysfs_s(FILE *f, char *buf, unsigned int len);
+
+__rte_internal
    int write_core_sysfs_s(FILE *f, const char *str);
 
Quoted 6 lines

lib/power/rte_power.c

Quoted 2 lines
lib/power/power_common.h +0 -0
    #endif /* _POWER_COMMON_H_ */
lib/power/rte_power.c +0 -0

The name of the rte_power.c file is impropriate now. The context in this file is just for cpufreq, right?
So I suggest that we need to rename this file as the rte_power_cpufreq_api.c

Yes, renaming rte_power.c to rte_power_cpufreq.c is definitely a possible incremental change and will fix this as a separate patch. .

Quoted 65 lines
Quoted 6 lines

index 36c3f3da98..2bf6d40517 100644

lib/power/rte_power.c +0 -0
@@ -8,153 +8,86 @@
    #include <rte_spinlock.h>
 

lib/power/rte_power.c line 13

Quoted 10 lines
lib/power/rte_power.c +1 -6
    #include "rte_power.h"
-#include "power_acpi_cpufreq.h"
-#include "power_cppc_cpufreq.h"
    #include "power_common.h"
-#include "power_kvm_vm.h"
-#include "power_pstate_cpufreq.h"
-#include "power_amd_pstate_cpufreq.h"
 
-enum power_management_env global_default_env = PM_ENV_NOT_SET;
+static enum power_management_env global_default_env =

PM_ENV_NOT_SET;

Quoted 48 lines
lib/power/rte_power.c +3 -2
+static struct rte_power_core_ops *global_power_core_ops;
 
    static rte_spinlock_t global_env_cfg_lock =
 RTE_SPINLOCK_INITIALIZER;
+static RTE_TAILQ_HEAD(, rte_power_core_ops) core_ops_list =
+                     TAILQ_HEAD_INITIALIZER(core_ops_list);
 
-/* function pointers */
-rte_power_freqs_t rte_power_freqs  = NULL; -rte_power_get_freq_t

rte_power_get_freq = NULL; -rte_power_set_freq_t rte_power_set_freq = NULL; -rte_power_freq_change_t rte_power_freq_up = NULL;

lib/power/rte_power.c +15 -21
-rte_power_freq_change_t rte_power_freq_down = NULL;
-rte_power_freq_change_t rte_power_freq_max = NULL;
-rte_power_freq_change_t rte_power_freq_min = NULL;
-rte_power_freq_change_t rte_power_turbo_status;
-rte_power_freq_change_t rte_power_freq_enable_turbo;
-rte_power_freq_change_t rte_power_freq_disable_turbo;
-rte_power_get_capabilities_t rte_power_get_capabilities;
-
-static void
-reset_power_function_ptrs(void)
+
+const char *power_env_str[] = {
+     "not set",
+     "acpi",
+     "kvm-vm",
+     "pstate",
+     "cppc",
+     "amd-pstate"
+};
+
+/* register the ops struct in rte_power_core_ops, return 0 on
+success. */ int rte_power_register_ops(struct rte_power_core_ops
+*driver_ops)
    {
-     rte_power_freqs  = NULL;
-     rte_power_get_freq = NULL;
-     rte_power_set_freq = NULL;
-     rte_power_freq_up = NULL;
-     rte_power_freq_down = NULL;
-     rte_power_freq_max = NULL;
-     rte_power_freq_min = NULL;
-     rte_power_turbo_status = NULL;
-     rte_power_freq_enable_turbo = NULL;
-     rte_power_freq_disable_turbo = NULL;
-     rte_power_get_capabilities = NULL;
+     if (!driver_ops->init || !driver_ops->exit ||
+             !driver_ops->check_env_support || !driver_ops->get_avail_freqs ||
Quoted 8 lines

lib/power/rte_power.c

Quoted 7 lines
lib/power/rte_power.c +7 -0
+             !driver_ops->get_freq || !driver_ops->set_freq ||
+             !driver_ops->freq_up || !driver_ops->freq_down ||
+             !driver_ops->freq_max || !driver_ops->freq_min ||
+             !driver_ops->turbo_status || !driver_ops->enable_turbo ||
+             !driver_ops->disable_turbo || !driver_ops->get_caps) {
+             POWER_LOG(ERR, "Missing callbacks while registering
+ power ops");

turbo_status(), enable_turbo() and disable turbo() are not necessary, right?

Quoted 1 line

Nope, this is required to get the current status unlike the capability API

(get_caps()).
ok

Quoted 48 lines
Quoted 48 lines

These depand on the capabilities from get_caps().

Quoted 47 lines
lib/power/rte_power.c +20 -15
+             return -EINVAL;
+     }
+
+     TAILQ_INSERT_TAIL(&core_ops_list, driver_ops, next);
+
+     return 0;
    }
 
    int
    rte_power_check_env_supported(enum power_management_env env)
    {
-     switch (env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_check_supported();
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_check_supported();
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_check_supported();
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_check_supported();
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_check_supported();
-     default:
-             rte_errno = EINVAL;
-             return -1;
-     }
+     struct rte_power_core_ops *ops;
+
+     if (env >= RTE_DIM(power_env_str))
+             return 0;
+
+     RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+             if (strncmp(ops->name, power_env_str[env],
+                             RTE_POWER_DRIVER_NAMESZ) == 0)
+                     return ops->check_env_support();
+
+     return 0;
    }
 
    int
    rte_power_set_env(enum power_management_env env)
    {
+     struct rte_power_core_ops *ops;
+     int ret = -1;
+
        rte_spinlock_lock(&global_env_cfg_lock);
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

if (global_default_env != PM_ENV_NOT_SET) {
POWER_LOG(ERR, "Power Management Environment already

set.");

Quoted 80 lines
Quoted 80 lines
Quoted 4 lines
lib/power/rte_power.c +0 -4
-             rte_spinlock_unlock(&global_env_cfg_lock);
-             return -1;
-     }
-

<...>

Quoted 54 lines
lib/power/rte_power.c +17 -20
-     if (ret == 0)
-             global_default_env = env;
-     else {
-             global_default_env = PM_ENV_NOT_SET;
-             reset_power_function_ptrs();
-     }
+     RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+             if (strncmp(ops->name, power_env_str[env],
+                             RTE_POWER_DRIVER_NAMESZ) == 0) {
+                     global_power_core_ops = ops;
+                     global_default_env = env;
+                     ret = 0;
+                     goto out;
+             }
+     POWER_LOG(ERR, "Invalid Power Management Environment(%d) set",
+                     env);
 
+out:
        rte_spinlock_unlock(&global_env_cfg_lock);
        return ret;
    }
@@ -164,94 +97,66 @@ rte_power_unset_env(void)
    {
        rte_spinlock_lock(&global_env_cfg_lock);
        global_default_env = PM_ENV_NOT_SET;
-     reset_power_function_ptrs();
+     global_power_core_ops = NULL;
        rte_spinlock_unlock(&global_env_cfg_lock);
    }
 
    enum power_management_env
-rte_power_get_env(void) {
+rte_power_get_env(void)
+{
        return global_default_env;
    }
 
-int
-rte_power_init(unsigned int lcore_id)
+struct rte_power_core_ops *
+rte_power_get_core_ops(void)
    {
-     int ret = -1;
+     RTE_ASSERT(global_power_core_ops != NULL);
 
-     switch (global_default_env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_init(lcore_id);
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_init(lcore_id);
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_init(lcore_id);
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_init(lcore_id);

lib/power/rte_power.c rte_power_unset_env(void) · line 197

Quoted 10 lines
lib/power/rte_power.c +2 -7
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_init(lcore_id);
-     default:
-             POWER_LOG(INFO, "Env isn't set yet!");
-     }
+     return global_power_core_ops;
+}
 
-     /* Auto detect Environment */
-     POWER_LOG(INFO, "Attempting to initialise ACPI cpufreq power

management...");

Quoted 10 lines
lib/power/rte_power.c +4 -5
-     ret = power_acpi_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_ACPI_CPUFREQ);
-             goto out;
-     }
+int
+rte_power_init(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops;
+     uint8_t env;
 
Quoted 1 line
Quoted 1 line

lib/power/rte_power.c rte_power_unset_env(void) · line 204

Quoted 1 line
lib/power/rte_power.c +0 -1
-     POWER_LOG(INFO, "Attempting to initialise PSTAT power

management...");

Quoted 10 lines
Quoted 10 lines

lib/power/rte_power.c rte_power_unset_env(void)

Quoted 9 lines
lib/power/rte_power.c +2 -6
-     ret = power_pstate_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_PSTATE_CPUFREQ);
-             goto out;
-     }
+     if (global_default_env != PM_ENV_NOT_SET)
+             return global_power_core_ops->init(lcore_id);
 
-     POWER_LOG(INFO, "Attempting to initialise AMD PSTATE power

management...");

Quoted 8 lines
Quoted 8 lines

lib/power/rte_power.c rte_power_unset_env(void) · line 127

Quoted 6 lines
lib/power/rte_power.c +1 -5
-     ret = power_amd_pstate_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_AMD_PSTATE_CPUFREQ);
-             goto out;
-     }
+     POWER_LOG(INFO, "Env isn't set yet!");

remove this log?

Quoted 1 line
lib/power/rte_power.c +0 -1
-     POWER_LOG(INFO, "Attempting to initialise CPPC power

management...");

Quoted 32 lines
Quoted 31 lines
Quoted 10 lines
lib/power/rte_power.c +5 -5
-     ret = power_cppc_cpufreq_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_CPPC_CPUFREQ);
-             goto out;
-     }
+     /* Auto detect Environment */
+     RTE_TAILQ_FOREACH(ops, &core_ops_list, next)
+             if (ops) {
+                     POWER_LOG(INFO,
+                             "Attempting to initialise %s cpufreq power management...",

lib/power/rte_power.c rte_power_unset_env(void) · line 143

Quoted 10 lines
lib/power/rte_power.c +10 -0
+                             ops->name);
+                     if (ops->init(lcore_id) == 0) {
+                             for (env = 0; env < RTE_DIM(power_env_str); env++)
+                                     if (strncmp(ops->name, power_env_str[env],
+                                                     RTE_POWER_DRIVER_NAMESZ) == 0) {
+                                             rte_power_set_env(env);
+                                             return 0;
+                                     }
+                     }
+             }

Can we change the logic of rte_power_set_env()? like:

       RTE_TAILQ_FOREACH(ops, &core_ops_list, next) {
           for (env = 0; env < RTE_DIM(power_env_str); env++) {
                   if (strncmp(ops->name, power_env_str[env],
RTE_POWER_DRIVER_NAMESZ) == 0 &&
                       ops->init(lcore_id) == 0) {
                       global_power_core_ops = ops;
                       global_default_env = env;
                   }
           }
       }
Quoted 1 line

That is easier to follow code.

Quoted 14 lines

Yes, will fix in next version.

Quoted 12 lines
Quoted 12 lines
lib/power/rte_power.c +4 -7
+
+     POWER_LOG(ERR,
+             "Unable to set Power Management Environment for lcore %u",
+             lcore_id);
 
-     POWER_LOG(INFO, "Attempting to initialise VM power management...");
-     ret = power_kvm_vm_init(lcore_id);
-     if (ret == 0) {
-             rte_power_set_env(PM_ENV_KVM_VM);
-             goto out;
-     }
-     POWER_LOG(ERR, "Unable to set Power Management Environment for

lcore

Quoted 50 lines
Quoted 50 lines

"

Quoted 12 lines
lib/power/rte_power.c +1 -6
-                     "%u", lcore_id);
-out:
-     return ret;
+     return -1;
    }
 
    int
    rte_power_exit(unsigned int lcore_id)
    {
-     switch (global_default_env) {
-     case PM_ENV_ACPI_CPUFREQ:
-             return power_acpi_cpufreq_exit(lcore_id);

lib/power/rte_power.c rte_power_unset_env(void)

Quoted 10 lines
lib/power/rte_power.c +0 -10
-     case PM_ENV_KVM_VM:
-             return power_kvm_vm_exit(lcore_id);
-     case PM_ENV_PSTATE_CPUFREQ:
-             return power_pstate_cpufreq_exit(lcore_id);
-     case PM_ENV_CPPC_CPUFREQ:
-             return power_cppc_cpufreq_exit(lcore_id);
-     case PM_ENV_AMD_PSTATE_CPUFREQ:
-             return power_amd_pstate_cpufreq_exit(lcore_id);
-     default:
-             POWER_LOG(ERR, "Environment has not been set, unable to exit

gracefully");

Quoted 26 lines
lib/power/rte_power.c +6 -2
+     if (global_default_env != PM_ENV_NOT_SET)
+             return global_power_core_ops->exit(lcore_id);
 
-     }
-     return -1;
+     POWER_LOG(ERR,
+             "Environment has not been set, unable to exit
+ gracefully");
 
+     return -1;
    }
lib/power/rte_power.h index renamed from lib/power/rte_power.h +0 -0

4fa4afe399..5e4aacf08b 100644

lib/power/rte_power.h +1 -0
@@ -1,5 +1,6 @@
    /* SPDX-License-Identifier: BSD-3-Clause
     * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
     */
 
    #ifndef _RTE_POWER_H
@@ -14,14 +15,21 @@
    #include <rte_log.h>
    #include <rte_power_guest_channel.h>
 
Quoted 8 lines
Quoted 8 lines

lib/power/rte_power.h

Quoted 8 lines
lib/power/rte_power.h +2 -0
+#include "rte_power_core_ops.h"
+
    #ifdef __cplusplus
    extern "C" {
    #endif
 
    /* Power Management Environment State */ -enum
 power_management_env {PM_ENV_NOT_SET, PM_ENV_ACPI_CPUFREQ,

PM_ENV_KVM_VM,

Quoted 18 lines
Quoted 18 lines
Quoted 13 lines
lib/power/rte_power.h +8 -2
-             PM_ENV_PSTATE_CPUFREQ, PM_ENV_CPPC_CPUFREQ,
-             PM_ENV_AMD_PSTATE_CPUFREQ};
+enum power_management_env {
+     PM_ENV_NOT_SET = 0,
+     PM_ENV_ACPI_CPUFREQ,
+     PM_ENV_KVM_VM,
+     PM_ENV_PSTATE_CPUFREQ,
+     PM_ENV_CPPC_CPUFREQ,
+     PM_ENV_AMD_PSTATE_CPUFREQ
+};
 
    /**
     * Check if a specific power management environment type is
Quoted 3 lines
supported on a @@ -66,6 +74,15 @@ void rte_power_unset_env(void);
     */
    enum power_management_env rte_power_get_env(void);

I'd like to let user not know used which cpufreq driver, which is friendly to user.

Quoted 3 lines
Quoted 1 line

So we can rethink if this API is necessary.

For any API changes, could we handle this as a separate RFC for discussion? It’s important that these changes are not included within the scope of this patch.

Agreed.
Can you post a separate RFC to disscuss this improvement later?

Quoted 11 lines
Quoted 11 lines

lib/power/rte_power.h void rte_power_unset_env(void);

Quoted 11 lines
lib/power/rte_power.h +9 -0
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_core_ops *
+rte_power_get_core_ops(void);
+
    /**
     * Initialize power management for a specific lcore. If rte_power_set_env()

has

Quoted 236 lines
Quoted 236 lines
Quoted 30 lines

* not been called then an auto-detect of the environment will
start and @@ -108,10 +125,13 @@ int rte_power_exit(unsigned int lcore_id);
* @return
* The number of available frequencies.
*/

lib/power/rte_power.h +8 -5
-typedef uint32_t (*rte_power_freqs_t)(unsigned int lcore_id, uint32_t *freqs,
-             uint32_t num);
+static inline uint32_t
+rte_power_freqs(unsigned int lcore_id, uint32_t *freqs, uint32_t n) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_freqs_t rte_power_freqs;
+     return ops->get_avail_freqs(lcore_id, freqs, n); }
 
    /**
     * Return the current index of available frequencies of a specific lcore.
@@ -124,9 +144,13 @@ extern rte_power_freqs_t rte_power_freqs;
     * @return
     *  The current index of available frequencies.
     */
-typedef uint32_t (*rte_power_get_freq_t)(unsigned int lcore_id);
+static inline uint32_t
+rte_power_get_freq(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_get_freq_t rte_power_get_freq;
+     return ops->get_freq(lcore_id); }
 
    /**
     * Set the new frequency for a specific lcore by indicating the
Quoted 1 line

index of @@ -144,82 +168,101 @@ extern rte_power_get_freq_t

rte_power_get_freq;

Quoted 144 lines

* - 0 on success without frequency changed.
* - Negative on error.
*/
-typedef int (*rte_power_set_freq_t)(unsigned int lcore_id, uint32_t index);

lib/power/rte_power.h +3 -4
-
-extern rte_power_set_freq_t rte_power_set_freq;
+static inline uint32_t
+rte_power_set_freq(unsigned int lcore_id, uint32_t index) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-/**
- * Function pointer definition for generic frequency change

functions. Review
- * each environments specific documentation for usage.
- *
- * @param lcore_id
- * lcore id.
- *
- * @return

- *  - 1 on success with frequency changed.
- *  - 0 on success without frequency changed.
- *  - Negative on error.

- */

lib/power/rte_power.h +40 -23
-typedef int (*rte_power_freq_change_t)(unsigned int lcore_id);
+     return ops->set_freq(lcore_id, index); }
 
    /**
     * Scale up the frequency of a specific lcore according to the available
     * frequencies.
     * Review each environments specific documentation for usage.
     */
-extern rte_power_freq_change_t rte_power_freq_up;
+static inline int
+rte_power_freq_up(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_up(lcore_id); }
 
    /**
     * Scale down the frequency of a specific lcore according to the available
     * frequencies.
     * Review each environments specific documentation for usage.
     */
-extern rte_power_freq_change_t rte_power_freq_down;
+static inline int
+rte_power_freq_down(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_down(lcore_id); }
 
    /**
     * Scale up the frequency of a specific lcore to the highest according to the
     * available frequencies.
     * Review each environments specific documentation for usage.
     */
-extern rte_power_freq_change_t rte_power_freq_max;
+static inline int
+rte_power_freq_max(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_max(lcore_id); }
 
    /**
     * Scale down the frequency of a specific lcore to the lowest according to the
     * available frequencies.
     * Review each environments specific documentation for usage..
     */
-extern rte_power_freq_change_t rte_power_freq_min;
+static inline int
+rte_power_freq_min(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->freq_min(lcore_id); }
 
    /**
     * Query the Turbo Boost status of a specific lcore.
     * Review each environments specific documentation for usage..
     */
-extern rte_power_freq_change_t rte_power_turbo_status;
+static inline int
+rte_power_turbo_status(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->turbo_status(lcore_id); }
 
    /**
     * Enable Turbo Boost for this lcore.
     * Review each environments specific documentation for usage..
     */
-extern rte_power_freq_change_t rte_power_freq_enable_turbo;
+static inline int
+rte_power_freq_enable_turbo(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
+
+     return ops->enable_turbo(lcore_id); }
 
    /**
     * Disable Turbo Boost for this lcore.
     * Review each environments specific documentation for usage..
     */
-extern rte_power_freq_change_t rte_power_freq_disable_turbo;
+static inline int
+rte_power_freq_disable_turbo(unsigned int lcore_id) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-/**
- * Power capabilities summary.
- */
-struct rte_power_core_capabilities {
-     union {
-             uint64_t capabilities;
-             struct {
-                     uint64_t turbo:1;       /**< Turbo can be enabled. */
-                     uint64_t priority:1;    /**< SST-BF high freq core */
-             };
-     };
-};
+     return ops->disable_turbo(lcore_id); }
 
    /**
     * Returns power capabilities for a specific lcore.
@@ -235,10 +278,14 @@ struct rte_power_core_capabilities {
     *  - 0 on success.
     *  - Negative on error.
     */
-typedef int (*rte_power_get_capabilities_t)(unsigned int lcore_id,
-             struct rte_power_core_capabilities *caps);
+static inline int
+rte_power_get_capabilities(unsigned int lcore_id,
+             struct rte_power_core_capabilities *caps) {
+     struct rte_power_core_ops *ops = rte_power_get_core_ops();
 
-extern rte_power_get_capabilities_t rte_power_get_capabilities;
+     return ops->get_caps(lcore_id, caps); }
 
    #ifdef __cplusplus
    }
(quoted diff) +0 -0

b/lib/power/rte_power_core_ops.h new file mode 100644 index 0000000000..356a64df79

lib/power/rte_power_core_ops.h line 8

Quoted 8 lines
lib/power/rte_power_core_ops.h new file +8 -0
@@ -0,0 +1,208 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2010-2014 Intel Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
+ */
+
+#ifndef _RTE_POWER_CORE_OPS_H
+#define _RTE_POWER_CORE_OPS_H
+

suggest rename the file as rte_power_cpufreq_api.h. If so, the role of this file is more clearly.

Quoted 31 lines
lib/power/rte_power_core_ops.h new file +26 -0
+__rte_internal
+int rte_power_register_ops(struct rte_power_core_ops *ops);
+
+/**
+ * Macro to statically register the ops of a cpufreq driver.
+ */
+#define RTE_POWER_REGISTER_OPS(ops)          \
+     RTE_INIT(power_hdlr_init_##ops)         \
+     {                                       \
+             rte_power_register_ops(&ops);   \
+     }
+
+/**
+ * @internal Get the power ops struct from its index.
+ *
+ * @return
+ *   The pointer to the ops struct in the table if registered.
+ */
+struct rte_power_core_ops *
+rte_power_get_core_ops(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
lib/power/version.map index renamed from lib/power/version.map +0 -0

c9a226614e..bd64e0828f 100644

lib/power/version.map EXPERIMENTAL { · line 54

Quoted 4 lines
lib/power/version.map +1 -0
@@ -51,4 +51,18 @@ EXPERIMENTAL {
        rte_power_set_uncore_env;
        rte_power_uncore_freqs;
        rte_power_unset_uncore_env;
+     # added in 24.07

24.07-->24.11?

Quoted 14 lines
lib/power/version.map +13 -0
+     rte_power_logtype;
+};
+
+INTERNAL {
+     global:
+
+     rte_power_register_ops;
+     cpufreq_check_scaling_driver;
+     power_set_governor;
+     open_core_sysfs_file;
+     read_core_sysfs_u32;
+     read_core_sysfs_s;
+     write_core_sysfs_s;
    };
#34 lore raw

PATCH v2 2/4 power: refactor uncore power management library

In reply to Sivaprasad Tummala

This patch refactors the power management library, addressing uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/uncore/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

This refactor significantly improves code organization, enhances clarity and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

---
 .../power/intel_uncore/intel_uncore.c         |  18 +-
 .../power/intel_uncore/intel_uncore.h         |   8 +-
 drivers/power/intel_uncore/meson.build        |   6 +
 drivers/power/meson.build                     |   3 +-
 lib/power/meson.build                         |   2 +-
 lib/power/rte_power_uncore.c                  | 205 ++++++---------
 lib/power/rte_power_uncore.h                  |  87 ++++---
 lib/power/rte_power_uncore_ops.h              | 239 ++++++++++++++++++
 lib/power/version.map                         |   1 +
 9 files changed, 405 insertions(+), 164 deletions(-)
 rename lib/power/power_intel_uncore.c => drivers/power/intel_uncore/intel_uncore.c (95%)
 rename lib/power/power_intel_uncore.h => drivers/power/intel_uncore/intel_uncore.h (97%)
 create mode 100644 drivers/power/intel_uncore/meson.build
 create mode 100644 lib/power/rte_power_uncore_ops.h
drivers/power/intel_uncore/intel_uncore.c renamed from lib/power/power_intel_uncore.c +17 -1
similarity index 95%
rename from lib/power/power_intel_uncore.c
rename to drivers/power/intel_uncore/intel_uncore.c
index 4eb9c5900a..804ad5d755 100644
@@ -8,7 +8,7 @@
 
 #include <rte_memcpy.h>
 
-#include "power_intel_uncore.h"
+#include "intel_uncore.h"
 #include "power_common.h"
 
 #define MAX_NUMA_DIE 8
@@ -475,3 +475,19 @@ power_intel_uncore_get_num_dies(unsigned int pkg)
 
 	return count;
 }
+
+static struct rte_power_uncore_ops intel_uncore_ops = {
+	.name = "intel-uncore",
+	.init = power_intel_uncore_init,
+	.exit = power_intel_uncore_exit,
+	.get_avail_freqs = power_intel_uncore_freqs,
+	.get_num_pkgs = power_intel_uncore_get_num_pkgs,
+	.get_num_dies = power_intel_uncore_get_num_dies,
+	.get_num_freqs = power_intel_uncore_get_num_freqs,
+	.get_freq = power_get_intel_uncore_freq,
+	.set_freq = power_set_intel_uncore_freq,
+	.freq_max = power_intel_uncore_freq_max,
+	.freq_min = power_intel_uncore_freq_min,
+};
+
+RTE_POWER_REGISTER_UNCORE_OPS(intel_uncore_ops);
drivers/power/intel_uncore/intel_uncore.h renamed from lib/power/power_intel_uncore.h +4 -4
similarity index 97%
rename from lib/power/power_intel_uncore.h
rename to drivers/power/intel_uncore/intel_uncore.h
index 20a3ba8ebe..f2ce2f0c66 100644
@@ -2,8 +2,8 @@
  * Copyright(c) 2022 Intel Corporation
  */
 
-#ifndef POWER_INTEL_UNCORE_H
-#define POWER_INTEL_UNCORE_H
+#ifndef INTEL_UNCORE_H
+#define INTEL_UNCORE_H
 
 /**
  * @file
@@ -11,7 +11,7 @@
  */
 
 #include "rte_power.h"
-#include "rte_power_uncore.h"
+#include "rte_power_uncore_ops.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -223,4 +223,4 @@ power_intel_uncore_get_num_dies(unsigned int pkg);
 }
 #endif
 
-#endif /* POWER_INTEL_UNCORE_H */
+#endif /* INTEL_UNCORE_H */
drivers/power/intel_uncore/meson.build new file +6 -0
new file mode 100644
index 0000000000..876df8ad14
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2017 Intel Corporation
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+sources = files('intel_uncore.c')
+deps += ['power']
drivers/power/meson.build +2 -1
index 8c7215c639..c83047af94 100644
@@ -6,7 +6,8 @@ drivers = [
         'amd_pstate',
         'cppc',
         'kvm_vm',
-        'pstate'
+        'pstate',
+        'intel_uncore'
 ]
 
 std_deps = ['power']
lib/power/meson.build +1 -1
index f3e3451cdc..9b13d98810 100644
@@ -13,7 +13,6 @@ if not is_linux
 endif
 sources = files(
         'power_common.c',
-        'power_intel_uncore.c',
         'rte_power.c',
         'rte_power_uncore.c',
         'rte_power_pmd_mgmt.c',
@@ -24,6 +23,7 @@ headers = files(
         'rte_power_guest_channel.h',
         'rte_power_pmd_mgmt.h',
         'rte_power_uncore.h',
+        'rte_power_uncore_ops.h',
 )
 if cc.has_argument('-Wno-cast-qual')
     cflags += '-Wno-cast-qual'
lib/power/rte_power_uncore.c +82 -123

263 lines not on this page · raw message

lib/power/rte_power_uncore.h +53 -34

153 lines not on this page · raw message

lib/power/rte_power_uncore_ops.h new file +239 -0

239 lines not on this page · raw message

lib/power/version.map +1 -0
index bd64e0828f..f1eabd7c9a 100644
@@ -59,6 +59,7 @@ INTERNAL {
 	global:
 
 	rte_power_register_ops;
+	rte_power_register_uncore_ops;
 	cpufreq_check_scaling_driver;
 	power_set_governor;
 	open_core_sysfs_file;
Signature · 1 line
2.34.1
#35 lore raw

Re: [PATCH v2 2/4] power: refactor uncore power management library

Hi Sivaprasad,

Suggest to split this patch into two patches for easiler to review:
patch-1: abstract a file for uncore dvfs core level, namely, the rte_power_uncore_ops.c you did.
patch-2: move and rename, lib/power/power_intel_uncore.c => drivers/power/intel_uncore/intel_uncore.c

patch[1/4] is also too big and not good to review.

In addition, I have some question and am not sure if we can adjust uncore init process.

/Huisong

Quoted 48 lines · Sivaprasad Tummala

This patch refactors the power management library, addressing uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/uncore/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

This refactor significantly improves code organization, enhances clarity and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

---
  .../power/intel_uncore/intel_uncore.c         |  18 +-
  .../power/intel_uncore/intel_uncore.h         |   8 +-
  drivers/power/intel_uncore/meson.build        |   6 +
  drivers/power/meson.build                     |   3 +-
  lib/power/meson.build                         |   2 +-
  lib/power/rte_power_uncore.c                  | 205 ++++++---------
  lib/power/rte_power_uncore.h                  |  87 ++++---
  lib/power/rte_power_uncore_ops.h              | 239 ++++++++++++++++++
  lib/power/version.map                         |   1 +
  9 files changed, 405 insertions(+), 164 deletions(-)

rename lib/power/power_intel_uncore.c => drivers/power/intel_uncore/intel_uncore.c (95%) rename lib/power/power_intel_uncore.h => drivers/power/intel_uncore/intel_uncore.h (97%) create mode 100644 drivers/power/intel_uncore/meson.build
create mode 100644 lib/power/rte_power_uncore_ops.h

drivers/power/intel_uncore/intel_uncore.c renamed from lib/power/power_intel_uncore.c +1 -1
similarity index 95%
rename from lib/power/power_intel_uncore.c
rename to drivers/power/intel_uncore/intel_uncore.c
index 4eb9c5900a..804ad5d755 100644
@@ -8,7 +8,7 @@
 
  #include <rte_memcpy.h>
 
-#include "power_intel_uncore.h"
+#include "intel_uncore.h"
  #include "power_common.h"
 
  #define MAX_NUMA_DIE 8
@@ -475,3 +475,19 @@ power_intel_uncore_get_num_dies(unsigned int pkg)
 
  	return count;
  }

<...>

Quoted 20 lines
drivers/power/intel_uncore/intel_uncore.c renamed from lib/power/power_intel_uncore.c +1 -1
-#endif /* POWER_INTEL_UNCORE_H */
+#endif /* INTEL_UNCORE_H */
drivers/power/intel_uncore/meson.build new file +6 -0
new file mode 100644
index 0000000000..876df8ad14
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2017 Intel Corporation
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+sources = files('intel_uncore.c')
+deps += ['power']
drivers/power/meson.build +0 -0
index 8c7215c639..c83047af94 100644

drivers/power/meson.build drivers = [ · line 10

Quoted 6 lines
drivers/power/meson.build +2 -1
@@ -6,7 +6,8 @@ drivers = [
          'amd_pstate',
          'cppc',
          'kvm_vm',
-        'pstate'
+        'pstate',
+        'intel_uncore'

The cppc, amd_pstate and so on belong to cpufreq scope.
And intel_uncore belongs to uncore dvfs scope.
They are not the same level. So I proposes that we need to create one directory called like cpufreq or core. This 'intel_uncore' name don't seems appropriate. what do you think the following directory structure:
drivers/power/uncore/intel_uncore.c
drivers/power/uncore/amd_uncore.c (according to the patch[4/4]).

Quoted 55 lines
drivers/power/meson.build +0 -0
  ]
  std_deps = ['power']
lib/power/meson.build +1 -1
index f3e3451cdc..9b13d98810 100644
@@ -13,7 +13,6 @@ if not is_linux
  endif
  sources = files(
          'power_common.c',
-        'power_intel_uncore.c',
          'rte_power.c',
          'rte_power_uncore.c',
          'rte_power_pmd_mgmt.c',
@@ -24,6 +23,7 @@ headers = files(
          'rte_power_guest_channel.h',
          'rte_power_pmd_mgmt.h',
          'rte_power_uncore.h',
+        'rte_power_uncore_ops.h',
  )
  if cc.has_argument('-Wno-cast-qual')
      cflags += '-Wno-cast-qual'
lib/power/rte_power_uncore.c +5 -10
index 48c75a5da0..9f8771224f 100644
@@ -1,6 +1,7 @@
  /* SPDX-License-Identifier: BSD-3-Clause
   * Copyright(c) 2010-2014 Intel Corporation
   * Copyright(c) 2023 AMD Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
   */
 
  #include <errno.h>
@@ -12,98 +13,50 @@
  #include "rte_power_uncore.h"
  #include "power_intel_uncore.h"
 
-enum rte_uncore_power_mgmt_env default_uncore_env = RTE_UNCORE_PM_ENV_NOT_SET;
+static enum rte_uncore_power_mgmt_env global_uncore_env = RTE_UNCORE_PM_ENV_NOT_SET;
+static struct rte_power_uncore_ops *global_uncore_ops;
 
  static rte_spinlock_t global_env_cfg_lock = RTE_SPINLOCK_INITIALIZER;
+static RTE_TAILQ_HEAD(, rte_power_uncore_ops) uncore_ops_list =
+			TAILQ_HEAD_INITIALIZER(uncore_ops_list);
 
-static uint32_t
-power_get_dummy_uncore_freq(unsigned int pkg __rte_unused,
-	       unsigned int die __rte_unused)
-{
-	return 0;
-}
-
-static int
-power_set_dummy_uncore_freq(unsigned int pkg __rte_unused,

lib/power/rte_power_uncore.c line 28

Quoted 10 lines
lib/power/rte_power_uncore.c +6 -4
-	       unsigned int die __rte_unused, uint32_t index __rte_unused)
-{
-	return 0;
-}
+const char *uncore_env_str[] = {
+	"not set",
+	"auto-detect",
+	"intel-uncore",
+	"amd-hsmp"
+};

Why open the "auto-detect" mode to user?
Why not set this automatically at framework initialization? After all, the uncore driver is fixed for one platform.

Quoted 8 lines
lib/power/rte_power_uncore.c +0 -7
-static int
-power_dummy_uncore_freq_max(unsigned int pkg __rte_unused,
-	       unsigned int die __rte_unused)
-{
-	return 0;
-}
-

<...>

Quoted 73 lines
lib/power/rte_power_uncore.c +18 -41
-static int
-power_dummy_uncore_get_num_freqs(unsigned int pkg __rte_unused,
-	       unsigned int die __rte_unused)
+/* register the ops struct in rte_power_uncore_ops, return 0 on success. */
+int
+rte_power_register_uncore_ops(struct rte_power_uncore_ops *driver_ops)
  {
-	return 0;
-}
+	if (!driver_ops->init || !driver_ops->exit || !driver_ops->get_num_pkgs ||
+		!driver_ops->get_num_dies || !driver_ops->get_num_freqs ||
+		!driver_ops->get_avail_freqs || !driver_ops->get_freq ||
+		!driver_ops->set_freq || !driver_ops->freq_max ||
+		!driver_ops->freq_min) {
+		POWER_LOG(ERR, "Missing callbacks while registering power ops");
+		return -1;
+	}
+	if (driver_ops->cb)
+		driver_ops->cb();
 
-static unsigned int
-power_dummy_uncore_get_num_pkgs(void)
-{
-	return 0;
-}
+	TAILQ_INSERT_TAIL(&uncore_ops_list, driver_ops, next);
 
-static unsigned int
-power_dummy_uncore_get_num_dies(unsigned int pkg __rte_unused)
-{
  	return 0;
  }
-
-/* function pointers */
-rte_power_get_uncore_freq_t rte_power_get_uncore_freq = power_get_dummy_uncore_freq;
-rte_power_set_uncore_freq_t rte_power_set_uncore_freq = power_set_dummy_uncore_freq;
-rte_power_uncore_freq_change_t rte_power_uncore_freq_max = power_dummy_uncore_freq_max;
-rte_power_uncore_freq_change_t rte_power_uncore_freq_min = power_dummy_uncore_freq_min;
-rte_power_uncore_freqs_t rte_power_uncore_freqs = power_dummy_uncore_freqs;
-rte_power_uncore_get_num_freqs_t rte_power_uncore_get_num_freqs = power_dummy_uncore_get_num_freqs;
-rte_power_uncore_get_num_pkgs_t rte_power_uncore_get_num_pkgs = power_dummy_uncore_get_num_pkgs;
-rte_power_uncore_get_num_dies_t rte_power_uncore_get_num_dies = power_dummy_uncore_get_num_dies;
-
-static void
-reset_power_uncore_function_ptrs(void)
-{
-	rte_power_get_uncore_freq = power_get_dummy_uncore_freq;
-	rte_power_set_uncore_freq = power_set_dummy_uncore_freq;
-	rte_power_uncore_freq_max = power_dummy_uncore_freq_max;
-	rte_power_uncore_freq_min = power_dummy_uncore_freq_min;
-	rte_power_uncore_freqs  = power_dummy_uncore_freqs;
-	rte_power_uncore_get_num_freqs = power_dummy_uncore_get_num_freqs;
-	rte_power_uncore_get_num_pkgs = power_dummy_uncore_get_num_pkgs;
-	rte_power_uncore_get_num_dies = power_dummy_uncore_get_num_dies;
-}
-
  int
  rte_power_set_uncore_env(enum rte_uncore_power_mgmt_env env)
  {
-	int ret;
+	int ret = -1;
+	struct rte_power_uncore_ops *ops;
 
  	rte_spinlock_lock(&global_env_cfg_lock);
 
-	if (default_uncore_env != RTE_UNCORE_PM_ENV_NOT_SET) {
+	if (global_uncore_env != RTE_UNCORE_PM_ENV_NOT_SET) {
  		POWER_LOG(ERR, "Uncore Power Management Env already set.");
-		rte_spinlock_unlock(&global_env_cfg_lock);
-		return -1;
+		goto out;
  	}

<...>

Quoted 19 lines
lib/power/rte_power_uncore.c +13 -1
+	if (env <= RTE_DIM(uncore_env_str)) {
+		RTE_TAILQ_FOREACH(ops, &uncore_ops_list, next)
+			if (strncmp(ops->name, uncore_env_str[env],
+				RTE_POWER_UNCORE_DRIVER_NAMESZ) == 0) {
+				global_uncore_env = env;
+				global_uncore_ops = ops;
+				ret = 0;
+				goto out;
+			}
+		POWER_LOG(ERR, "Power Management (%s) not supported",
+				uncore_env_str[env]);
+	} else
+		POWER_LOG(ERR, "Invalid Power Management Environment");
 
-	default_uncore_env = env;
  out:
  	rte_spinlock_unlock(&global_env_cfg_lock);
  	return ret;

lib/power/rte_power_uncore.c void · line 95

Quoted 9 lines
lib/power/rte_power_uncore.c +1 -2
@@ -139,15 +89,22 @@ void
  rte_power_unset_uncore_env(void)
  {
  	rte_spinlock_lock(&global_env_cfg_lock);
-	default_uncore_env = RTE_UNCORE_PM_ENV_NOT_SET;
-	reset_power_uncore_function_ptrs();
+	global_uncore_env = RTE_UNCORE_PM_ENV_NOT_SET;
  	rte_spinlock_unlock(&global_env_cfg_lock);
  }
 

How about abstract an ABI interface to intialize or set the uncore driver on platform by automatical.

And later do power_intel_uncore_init_on_die() for each die on different package.

lib/power/rte_power_uncore.c rte_power_uncore_init(unsigned int pkg, unsigned int die)

Quoted 17 lines
lib/power/rte_power_uncore.c +9 -1
  enum rte_uncore_power_mgmt_env
  rte_power_get_uncore_env(void)
  {
-	return default_uncore_env;
+	return global_uncore_env;
+}
+
+struct rte_power_uncore_ops *
+rte_power_get_uncore_ops(void)
+{
+	RTE_ASSERT(global_uncore_ops != NULL);
+
+	return global_uncore_ops;
  }
 
  int
@@ -155,27 +112,29 @@ rte_power_uncore_init(unsigned int pkg, unsigned int die)

This pkg means the socket id on the platform, right?
If so, I am not sure that the uncore_info[RTE_MAX_NUMA_NODES][MAX_NUMA_DIE] used in uncore lib is universal for all uncore driver. For example, uncore driver just support do uncore dvfs based on the socket unit.
What shoud we do for this? we may need to think twice.

Quoted 3 lines
lib/power/rte_power_uncore.c +0 -0
  {
  	int ret = -1;
 

<...>

#36 lore raw

RE: [PATCH v2 2/4] power: refactor uncore power management library

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lihuisong,

Quoted 109 lines

-----Original Message-----
From: lihuisong (C) <[email protected]>
Sent: Tuesday, August 27, 2024 6:33 PM
To: Tummala, Sivaprasad <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Subject: Re: [PATCH v2 2/4] power: refactor uncore power management library

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Hi Sivaprasad,

Suggest to split this patch into two patches for easiler to review:
patch-1: abstract a file for uncore dvfs core level, namely, the rte_power_uncore_ops.c you did.
patch-2: move and rename, lib/power/power_intel_uncore.c => drivers/power/intel_uncore/intel_uncore.c

patch[1/4] is also too big and not good to review.

In addition, I have some question and am not sure if we can adjust uncore init process.

/Huisong

Quoted 23 lines · Sivaprasad Tummala

This patch refactors the power management library, addressing uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/uncore/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

This refactor significantly improves code organization, enhances clarity and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

---
  .../power/intel_uncore/intel_uncore.c         |  18 +-
  .../power/intel_uncore/intel_uncore.h         |   8 +-
  drivers/power/intel_uncore/meson.build        |   6 +
  drivers/power/meson.build                     |   3 +-
  lib/power/meson.build                         |   2 +-
  lib/power/rte_power_uncore.c                  | 205 ++++++---------
  lib/power/rte_power_uncore.h                  |  87 ++++---
  lib/power/rte_power_uncore_ops.h              | 239 ++++++++++++++++++
  lib/power/version.map                         |   1 +
  9 files changed, 405 insertions(+), 164 deletions(-)
Quoted 1 line

rename lib/power/power_intel_uncore.c =>

drivers/power/intel_uncore/intel_uncore.c (95%)

Quoted 1 line

rename lib/power/power_intel_uncore.h =>

drivers/power/intel_uncore/intel_uncore.h (97%)

Quoted 24 lines

create mode 100644 drivers/power/intel_uncore/meson.build create mode 100644 lib/power/rte_power_uncore_ops.h

drivers/power/intel_uncore/intel_uncore.c renamed from lib/power/power_intel_uncore.c rename to +0 -0
similarity index 95%
rename from lib/power/power_intel_uncore.c rename to

drivers/power/intel_uncore/intel_uncore.c
index 4eb9c5900a..804ad5d755 100644

drivers/power/intel_uncore/intel_uncore.c renamed from lib/power/power_intel_uncore.c +1 -1
@@ -8,7 +8,7 @@
 
  #include <rte_memcpy.h>
 
-#include "power_intel_uncore.h"
+#include "intel_uncore.h"
  #include "power_common.h"
 
  #define MAX_NUMA_DIE 8
@@ -475,3 +475,19 @@ power_intel_uncore_get_num_dies(unsigned int pkg)
 
      return count;
  }

<...>

Quoted 20 lines
drivers/power/intel_uncore/intel_uncore.c renamed from lib/power/power_intel_uncore.c +1 -1
-#endif /* POWER_INTEL_UNCORE_H */
+#endif /* INTEL_UNCORE_H */
drivers/power/intel_uncore/meson.build new file +5 -0
new file mode 100644
index 0000000000..876df8ad14
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2017 Intel
+Corporation # Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+sources = files('intel_uncore.c')
+deps += ['power']
drivers/power/meson.build +0 -0
index 8c7215c639..c83047af94 100644

drivers/power/meson.build drivers = [ · line 10

Quoted 6 lines
drivers/power/meson.build +2 -1
@@ -6,7 +6,8 @@ drivers = [
          'amd_pstate',
          'cppc',
          'kvm_vm',
-        'pstate'
+        'pstate',
+        'intel_uncore'
Quoted 8 lines

The cppc, amd_pstate and so on belong to cpufreq scope.
And intel_uncore belongs to uncore dvfs scope.
They are not the same level. So I proposes that we need to create one directory called like cpufreq or core.
This 'intel_uncore' name don't seems appropriate. what do you think the following directory structure:
drivers/power/uncore/intel_uncore.c drivers/power/uncore/amd_uncore.c (according to the patch[4/4]).

At present, Meson does not support detecting an additional level of subdirectories within drivers/*. All the drivers maintain a consistent subdirectory structure.

Quoted 63 lines
Quoted 63 lines
drivers/power/meson.build +0 -0
  ]
  std_deps = ['power']
lib/power/meson.build index renamed from lib/power/meson.build +0 -0

f3e3451cdc..9b13d98810 100644

lib/power/meson.build +1 -1
@@ -13,7 +13,6 @@ if not is_linux
  endif
  sources = files(
          'power_common.c',
-        'power_intel_uncore.c',
          'rte_power.c',
          'rte_power_uncore.c',
          'rte_power_pmd_mgmt.c',
@@ -24,6 +23,7 @@ headers = files(
          'rte_power_guest_channel.h',
          'rte_power_pmd_mgmt.h',
          'rte_power_uncore.h',
+        'rte_power_uncore_ops.h',
  )
  if cc.has_argument('-Wno-cast-qual')
      cflags += '-Wno-cast-qual'
(quoted diff) +0 -0

b/lib/power/rte_power_uncore.c index 48c75a5da0..9f8771224f 100644

lib/power/rte_power_uncore.c +7 -14
@@ -1,6 +1,7 @@
  /* SPDX-License-Identifier: BSD-3-Clause
   * Copyright(c) 2010-2014 Intel Corporation
   * Copyright(c) 2023 AMD Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
   */
 
  #include <errno.h>
@@ -12,98 +13,50 @@
  #include "rte_power_uncore.h"
  #include "power_intel_uncore.h"
 
-enum rte_uncore_power_mgmt_env default_uncore_env =
 RTE_UNCORE_PM_ENV_NOT_SET;
+static enum rte_uncore_power_mgmt_env global_uncore_env =
+RTE_UNCORE_PM_ENV_NOT_SET; static struct rte_power_uncore_ops
+*global_uncore_ops;
 
  static rte_spinlock_t global_env_cfg_lock =
 RTE_SPINLOCK_INITIALIZER;
+static RTE_TAILQ_HEAD(, rte_power_uncore_ops) uncore_ops_list =
+                     TAILQ_HEAD_INITIALIZER(uncore_ops_list);
 
-static uint32_t
-power_get_dummy_uncore_freq(unsigned int pkg __rte_unused,
-            unsigned int die __rte_unused)
-{
-     return 0;
-}
-
-static int
-power_set_dummy_uncore_freq(unsigned int pkg __rte_unused,
-            unsigned int die __rte_unused, uint32_t index __rte_unused)
-{
-     return 0;
-}
+const char *uncore_env_str[] = {
Quoted 8 lines

lib/power/rte_power_uncore.c line 28

Quoted 5 lines
lib/power/rte_power_uncore.c +5 -0
+     "not set",
+     "auto-detect",
+     "intel-uncore",
+     "amd-hsmp"
+};

Why open the "auto-detect" mode to user?
Why not set this automatically at framework initialization? After all, the uncore driver is fixed for one platform.

The auto-detection feature has been implemented to enable seamless migration across platforms without requiring any changes to the application

Quoted 136 lines
Quoted 8 lines
lib/power/rte_power_uncore.c +0 -7
-static int
-power_dummy_uncore_freq_max(unsigned int pkg __rte_unused,
-            unsigned int die __rte_unused)
-{
-     return 0;
-}
-

<...>

Quoted 76 lines
lib/power/rte_power_uncore.c +14 -15
-static int
-power_dummy_uncore_get_num_freqs(unsigned int pkg __rte_unused,
-            unsigned int die __rte_unused)
+/* register the ops struct in rte_power_uncore_ops, return 0 on
+success. */ int rte_power_register_uncore_ops(struct
+rte_power_uncore_ops *driver_ops)
  {
-     return 0;
-}
+     if (!driver_ops->init || !driver_ops->exit || !driver_ops->get_num_pkgs ||
+             !driver_ops->get_num_dies || !driver_ops->get_num_freqs ||
+             !driver_ops->get_avail_freqs || !driver_ops->get_freq ||
+             !driver_ops->set_freq || !driver_ops->freq_max ||
+             !driver_ops->freq_min) {
+             POWER_LOG(ERR, "Missing callbacks while registering power ops");
+             return -1;
+     }
+     if (driver_ops->cb)
+             driver_ops->cb();
 
-static unsigned int
-power_dummy_uncore_get_num_pkgs(void)
-{
-     return 0;
-}
+     TAILQ_INSERT_TAIL(&uncore_ops_list, driver_ops, next);
 
-static unsigned int
-power_dummy_uncore_get_num_dies(unsigned int pkg __rte_unused) -{
      return 0;
  }
-
-/* function pointers */
-rte_power_get_uncore_freq_t rte_power_get_uncore_freq =

power_get_dummy_uncore_freq; -rte_power_set_uncore_freq_t rte_power_set_uncore_freq = power_set_dummy_uncore_freq; -rte_power_uncore_freq_change_t rte_power_uncore_freq_max = power_dummy_uncore_freq_max; -rte_power_uncore_freq_change_t rte_power_uncore_freq_min = power_dummy_uncore_freq_min; -rte_power_uncore_freqs_t rte_power_uncore_freqs = power_dummy_uncore_freqs; -rte_power_uncore_get_num_freqs_t rte_power_uncore_get_num_freqs = power_dummy_uncore_get_num_freqs; -rte_power_uncore_get_num_pkgs_t rte_power_uncore_get_num_pkgs = power_dummy_uncore_get_num_pkgs; -rte_power_uncore_get_num_dies_t rte_power_uncore_get_num_dies = power_dummy_uncore_get_num_dies;

lib/power/rte_power_uncore.c +4 -18
-
-static void
-reset_power_uncore_function_ptrs(void)
-{
-     rte_power_get_uncore_freq = power_get_dummy_uncore_freq;
-     rte_power_set_uncore_freq = power_set_dummy_uncore_freq;
-     rte_power_uncore_freq_max = power_dummy_uncore_freq_max;
-     rte_power_uncore_freq_min = power_dummy_uncore_freq_min;
-     rte_power_uncore_freqs  = power_dummy_uncore_freqs;
-     rte_power_uncore_get_num_freqs = power_dummy_uncore_get_num_freqs;
-     rte_power_uncore_get_num_pkgs = power_dummy_uncore_get_num_pkgs;
-     rte_power_uncore_get_num_dies = power_dummy_uncore_get_num_dies;
-}
-
  int
  rte_power_set_uncore_env(enum rte_uncore_power_mgmt_env env)
  {
-     int ret;
+     int ret = -1;
+     struct rte_power_uncore_ops *ops;
 
      rte_spinlock_lock(&global_env_cfg_lock);
 
-     if (default_uncore_env != RTE_UNCORE_PM_ENV_NOT_SET) {
+     if (global_uncore_env != RTE_UNCORE_PM_ENV_NOT_SET) {
              POWER_LOG(ERR, "Uncore Power Management Env already set.");
-             rte_spinlock_unlock(&global_env_cfg_lock);
-             return -1;
+             goto out;
      }

<...>

Quoted 19 lines
lib/power/rte_power_uncore.c +13 -1
+     if (env <= RTE_DIM(uncore_env_str)) {
+             RTE_TAILQ_FOREACH(ops, &uncore_ops_list, next)
+                     if (strncmp(ops->name, uncore_env_str[env],
+                             RTE_POWER_UNCORE_DRIVER_NAMESZ) == 0) {
+                             global_uncore_env = env;
+                             global_uncore_ops = ops;
+                             ret = 0;
+                             goto out;
+                     }
+             POWER_LOG(ERR, "Power Management (%s) not supported",
+                             uncore_env_str[env]);
+     } else
+             POWER_LOG(ERR, "Invalid Power Management Environment");
 
-     default_uncore_env = env;
  out:
      rte_spinlock_unlock(&global_env_cfg_lock);
      return ret;

lib/power/rte_power_uncore.c void · line 95

Quoted 9 lines
lib/power/rte_power_uncore.c +1 -2
@@ -139,15 +89,22 @@ void
  rte_power_unset_uncore_env(void)
  {
      rte_spinlock_lock(&global_env_cfg_lock);
-     default_uncore_env = RTE_UNCORE_PM_ENV_NOT_SET;
-     reset_power_uncore_function_ptrs();
+     global_uncore_env = RTE_UNCORE_PM_ENV_NOT_SET;
      rte_spinlock_unlock(&global_env_cfg_lock);
  }
 

How about abstract an ABI interface to intialize or set the uncore driver on platform by automatical.

And later do power_intel_uncore_init_on_die() for each die on different package.

Quoted 15 lines
lib/power/rte_power_uncore.c +9 -1
  enum rte_uncore_power_mgmt_env
  rte_power_get_uncore_env(void)
  {
-     return default_uncore_env;
+     return global_uncore_env;
+}
+
+struct rte_power_uncore_ops *
+rte_power_get_uncore_ops(void)
+{
+     RTE_ASSERT(global_uncore_ops != NULL);
+
+     return global_uncore_ops;
  }
 
Quoted 9 lines

lib/power/rte_power_uncore.c rte_power_uncore_init(unsigned int pkg, unsigned · line 112

Quoted 3 lines
lib/power/rte_power_uncore.c +0 -0
  int
@@ -155,27 +112,29 @@ rte_power_uncore_init(unsigned int pkg, unsigned
 int die)

This pkg means the socket id on the platform, right?
If so, I am not sure that the uncore_info[RTE_MAX_NUMA_NODES][MAX_NUMA_DIE] used in uncore lib is universal for all uncore driver.
For example, uncore driver just support do uncore dvfs based on the socket unit. What shoud we do for this? we may need to think twice.

Yes, pkg represents a socket id. In platforms with a single uncore controller per socket, the die ID should be set to '0' for the corresponding socket ID (pkg).
.

Quoted 4 lines
Quoted 3 lines
lib/power/rte_power_uncore.c +0 -0
  {
      int ret = -1;
 

<...>

#37 lore raw

Re: [PATCH v2 2/4] power: refactor uncore power management library

Hi Sivaprasa,

I have a inline question, please take a look.

Quoted 336 lines · Sivaprasad

[AMD Official Use Only - AMD Internal Distribution Only]

Hi Lihuisong,

Quoted 108 lines

-----Original Message-----
From: lihuisong (C) <[email protected]>
Sent: Tuesday, August 27, 2024 6:33 PM
To: Tummala, Sivaprasad <[email protected]>
Cc: [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; [email protected]; Yigit, Ferruh <[email protected]>; [email protected]
Subject: Re: [PATCH v2 2/4] power: refactor uncore power management library

Caution: This message originated from an External Source. Use proper caution when opening attachments, clicking links, or responding.

Hi Sivaprasad,

Suggest to split this patch into two patches for easiler to review:
patch-1: abstract a file for uncore dvfs core level, namely, the rte_power_uncore_ops.c you did.
patch-2: move and rename, lib/power/power_intel_uncore.c => drivers/power/intel_uncore/intel_uncore.c

patch[1/4] is also too big and not good to review.

In addition, I have some question and am not sure if we can adjust uncore init process.

/Huisong

Quoted 23 lines · Sivaprasad Tummala

This patch refactors the power management library, addressing uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/uncore/*'. The adjustment of meson.build files enables the selective activation of individual drivers.

This refactor significantly improves code organization, enhances clarity and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

---
   .../power/intel_uncore/intel_uncore.c         |  18 +-
   .../power/intel_uncore/intel_uncore.h         |   8 +-
   drivers/power/intel_uncore/meson.build        |   6 +
   drivers/power/meson.build                     |   3 +-
   lib/power/meson.build                         |   2 +-
   lib/power/rte_power_uncore.c                  | 205 ++++++---------
   lib/power/rte_power_uncore.h                  |  87 ++++---
   lib/power/rte_power_uncore_ops.h              | 239 ++++++++++++++++++
   lib/power/version.map                         |   1 +
   9 files changed, 405 insertions(+), 164 deletions(-)
Quoted 1 line

rename lib/power/power_intel_uncore.c =>

drivers/power/intel_uncore/intel_uncore.c (95%)

Quoted 1 line

rename lib/power/power_intel_uncore.h =>

drivers/power/intel_uncore/intel_uncore.h (97%)

Quoted 24 lines

create mode 100644 drivers/power/intel_uncore/meson.build create mode 100644 lib/power/rte_power_uncore_ops.h

drivers/power/intel_uncore/intel_uncore.c renamed from lib/power/power_intel_uncore.c rename to +0 -0
similarity index 95%
rename from lib/power/power_intel_uncore.c rename to

drivers/power/intel_uncore/intel_uncore.c
index 4eb9c5900a..804ad5d755 100644

drivers/power/intel_uncore/intel_uncore.c renamed from lib/power/power_intel_uncore.c +1 -1
@@ -8,7 +8,7 @@
 
   #include <rte_memcpy.h>
 
-#include "power_intel_uncore.h"
+#include "intel_uncore.h"
   #include "power_common.h"
 
   #define MAX_NUMA_DIE 8
@@ -475,3 +475,19 @@ power_intel_uncore_get_num_dies(unsigned int pkg)
 
       return count;
   }

<...>

Quoted 19 lines
drivers/power/intel_uncore/intel_uncore.c renamed from lib/power/power_intel_uncore.c +1 -1
-#endif /* POWER_INTEL_UNCORE_H */
+#endif /* INTEL_UNCORE_H */
drivers/power/intel_uncore/meson.build new file +5 -0
new file mode 100644
index 0000000000..876df8ad14
@@ -0,0 +1,6 @@
+# SPDX-License-Identifier: BSD-3-Clause # Copyright(c) 2017 Intel
+Corporation # Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+sources = files('intel_uncore.c')
+deps += ['power']
drivers/power/meson.build +0 -0
index 8c7215c639..c83047af94 100644

drivers/power/meson.build drivers = [ · line 10

Quoted 6 lines
drivers/power/meson.build +2 -1
@@ -6,7 +6,8 @@ drivers = [
           'amd_pstate',
           'cppc',
           'kvm_vm',
-        'pstate'
+        'pstate',
+        'intel_uncore'
Quoted 8 lines

The cppc, amd_pstate and so on belong to cpufreq scope.
And intel_uncore belongs to uncore dvfs scope.
They are not the same level. So I proposes that we need to create one directory called like cpufreq or core.
This 'intel_uncore' name don't seems appropriate. what do you think the following directory structure:
drivers/power/uncore/intel_uncore.c drivers/power/uncore/amd_uncore.c (according to the patch[4/4]).

At present, Meson does not support detecting an additional level of subdirectories within drivers/*. All the drivers maintain a consistent subdirectory structure.

Quoted 63 lines
Quoted 63 lines
drivers/power/meson.build +0 -0
   ]
   std_deps = ['power']
lib/power/meson.build index renamed from lib/power/meson.build +0 -0

f3e3451cdc..9b13d98810 100644

lib/power/meson.build +1 -1
@@ -13,7 +13,6 @@ if not is_linux
   endif
   sources = files(
           'power_common.c',
-        'power_intel_uncore.c',
           'rte_power.c',
           'rte_power_uncore.c',
           'rte_power_pmd_mgmt.c',
@@ -24,6 +23,7 @@ headers = files(
           'rte_power_guest_channel.h',
           'rte_power_pmd_mgmt.h',
           'rte_power_uncore.h',
+        'rte_power_uncore_ops.h',
   )
   if cc.has_argument('-Wno-cast-qual')
       cflags += '-Wno-cast-qual'
(quoted diff) +0 -0

b/lib/power/rte_power_uncore.c index 48c75a5da0..9f8771224f 100644

lib/power/rte_power_uncore.c +7 -14
@@ -1,6 +1,7 @@
   /* SPDX-License-Identifier: BSD-3-Clause
    * Copyright(c) 2010-2014 Intel Corporation
    * Copyright(c) 2023 AMD Corporation
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
    */
 
   #include <errno.h>
@@ -12,98 +13,50 @@
   #include "rte_power_uncore.h"
   #include "power_intel_uncore.h"
 
-enum rte_uncore_power_mgmt_env default_uncore_env =
 RTE_UNCORE_PM_ENV_NOT_SET;
+static enum rte_uncore_power_mgmt_env global_uncore_env =
+RTE_UNCORE_PM_ENV_NOT_SET; static struct rte_power_uncore_ops
+*global_uncore_ops;
 
   static rte_spinlock_t global_env_cfg_lock =
 RTE_SPINLOCK_INITIALIZER;
+static RTE_TAILQ_HEAD(, rte_power_uncore_ops) uncore_ops_list =
+                     TAILQ_HEAD_INITIALIZER(uncore_ops_list);
 
-static uint32_t
-power_get_dummy_uncore_freq(unsigned int pkg __rte_unused,
-            unsigned int die __rte_unused)
-{
-     return 0;
-}
-
-static int
-power_set_dummy_uncore_freq(unsigned int pkg __rte_unused,
-            unsigned int die __rte_unused, uint32_t index __rte_unused)
-{
-     return 0;
-}
+const char *uncore_env_str[] = {
Quoted 8 lines

lib/power/rte_power_uncore.c line 28

Quoted 5 lines
lib/power/rte_power_uncore.c +5 -0
+     "not set",
+     "auto-detect",
+     "intel-uncore",
+     "amd-hsmp"
+};

Why open the "auto-detect" mode to user?
Why not set this automatically at framework initialization? After all, the uncore driver is fixed for one platform.

The auto-detection feature has been implemented to enable seamless migration across platforms without requiring any changes to the application

Quoted 132 lines
Quoted 7 lines
lib/power/rte_power_uncore.c +0 -7
-static int
-power_dummy_uncore_freq_max(unsigned int pkg __rte_unused,
-            unsigned int die __rte_unused)
-{
-     return 0;
-}
-

<...>

Quoted 76 lines
lib/power/rte_power_uncore.c +14 -15
-static int
-power_dummy_uncore_get_num_freqs(unsigned int pkg __rte_unused,
-            unsigned int die __rte_unused)
+/* register the ops struct in rte_power_uncore_ops, return 0 on
+success. */ int rte_power_register_uncore_ops(struct
+rte_power_uncore_ops *driver_ops)
   {
-     return 0;
-}
+     if (!driver_ops->init || !driver_ops->exit || !driver_ops->get_num_pkgs ||
+             !driver_ops->get_num_dies || !driver_ops->get_num_freqs ||
+             !driver_ops->get_avail_freqs || !driver_ops->get_freq ||
+             !driver_ops->set_freq || !driver_ops->freq_max ||
+             !driver_ops->freq_min) {
+             POWER_LOG(ERR, "Missing callbacks while registering power ops");
+             return -1;
+     }
+     if (driver_ops->cb)
+             driver_ops->cb();
 
-static unsigned int
-power_dummy_uncore_get_num_pkgs(void)
-{
-     return 0;
-}
+     TAILQ_INSERT_TAIL(&uncore_ops_list, driver_ops, next);
 
-static unsigned int
-power_dummy_uncore_get_num_dies(unsigned int pkg __rte_unused) -{
       return 0;
   }
-
-/* function pointers */
-rte_power_get_uncore_freq_t rte_power_get_uncore_freq =

power_get_dummy_uncore_freq; -rte_power_set_uncore_freq_t rte_power_set_uncore_freq = power_set_dummy_uncore_freq; -rte_power_uncore_freq_change_t rte_power_uncore_freq_max = power_dummy_uncore_freq_max; -rte_power_uncore_freq_change_t rte_power_uncore_freq_min = power_dummy_uncore_freq_min; -rte_power_uncore_freqs_t rte_power_uncore_freqs = power_dummy_uncore_freqs; -rte_power_uncore_get_num_freqs_t rte_power_uncore_get_num_freqs = power_dummy_uncore_get_num_freqs; -rte_power_uncore_get_num_pkgs_t rte_power_uncore_get_num_pkgs = power_dummy_uncore_get_num_pkgs; -rte_power_uncore_get_num_dies_t rte_power_uncore_get_num_dies = power_dummy_uncore_get_num_dies;

lib/power/rte_power_uncore.c +4 -18
-
-static void
-reset_power_uncore_function_ptrs(void)
-{
-     rte_power_get_uncore_freq = power_get_dummy_uncore_freq;
-     rte_power_set_uncore_freq = power_set_dummy_uncore_freq;
-     rte_power_uncore_freq_max = power_dummy_uncore_freq_max;
-     rte_power_uncore_freq_min = power_dummy_uncore_freq_min;
-     rte_power_uncore_freqs  = power_dummy_uncore_freqs;
-     rte_power_uncore_get_num_freqs = power_dummy_uncore_get_num_freqs;
-     rte_power_uncore_get_num_pkgs = power_dummy_uncore_get_num_pkgs;
-     rte_power_uncore_get_num_dies = power_dummy_uncore_get_num_dies;
-}
-
   int
   rte_power_set_uncore_env(enum rte_uncore_power_mgmt_env env)
   {
-     int ret;
+     int ret = -1;
+     struct rte_power_uncore_ops *ops;
 
       rte_spinlock_lock(&global_env_cfg_lock);
 
-     if (default_uncore_env != RTE_UNCORE_PM_ENV_NOT_SET) {
+     if (global_uncore_env != RTE_UNCORE_PM_ENV_NOT_SET) {
               POWER_LOG(ERR, "Uncore Power Management Env already set.");
-             rte_spinlock_unlock(&global_env_cfg_lock);
-             return -1;
+             goto out;
       }

<...>

Quoted 19 lines
lib/power/rte_power_uncore.c +13 -1
+     if (env <= RTE_DIM(uncore_env_str)) {
+             RTE_TAILQ_FOREACH(ops, &uncore_ops_list, next)
+                     if (strncmp(ops->name, uncore_env_str[env],
+                             RTE_POWER_UNCORE_DRIVER_NAMESZ) == 0) {
+                             global_uncore_env = env;
+                             global_uncore_ops = ops;
+                             ret = 0;
+                             goto out;
+                     }
+             POWER_LOG(ERR, "Power Management (%s) not supported",
+                             uncore_env_str[env]);
+     } else
+             POWER_LOG(ERR, "Invalid Power Management Environment");
 
-     default_uncore_env = env;
   out:
       rte_spinlock_unlock(&global_env_cfg_lock);
       return ret;

lib/power/rte_power_uncore.c void · line 95

Quoted 9 lines
lib/power/rte_power_uncore.c +1 -2
@@ -139,15 +89,22 @@ void
   rte_power_unset_uncore_env(void)
   {
       rte_spinlock_lock(&global_env_cfg_lock);
-     default_uncore_env = RTE_UNCORE_PM_ENV_NOT_SET;
-     reset_power_uncore_function_ptrs();
+     global_uncore_env = RTE_UNCORE_PM_ENV_NOT_SET;
       rte_spinlock_unlock(&global_env_cfg_lock);
   }
 

How about abstract an ABI interface to intialize or set the uncore driver on platform by automatical.

And later do power_intel_uncore_init_on_die() for each die on different package.

Quoted 15 lines
lib/power/rte_power_uncore.c +9 -1
   enum rte_uncore_power_mgmt_env
   rte_power_get_uncore_env(void)
   {
-     return default_uncore_env;
+     return global_uncore_env;
+}
+
+struct rte_power_uncore_ops *
+rte_power_get_uncore_ops(void)
+{
+     RTE_ASSERT(global_uncore_ops != NULL);
+
+     return global_uncore_ops;
   }
 
Quoted 9 lines

lib/power/rte_power_uncore.c rte_power_uncore_init(unsigned int pkg, unsigned · line 112

Quoted 3 lines
lib/power/rte_power_uncore.c +0 -0
   int
@@ -155,27 +112,29 @@ rte_power_uncore_init(unsigned int pkg, unsigned
 int die)

This pkg means the socket id on the platform, right?
If so, I am not sure that the uncore_info[RTE_MAX_NUMA_NODES][MAX_NUMA_DIE] used in uncore lib is universal for all uncore driver.
For example, uncore driver just support do uncore dvfs based on the socket unit. What shoud we do for this? we may need to think twice.

Quoted 3 lines

Yes, pkg represents a socket id. In platforms with a single uncore controller per socket, the die ID should be set to '0' for the corresponding socket ID (pkg).
.

So just use the die ID 0 on one socket ID(namely, uncore_info[0][0], uncore_info[1][0]) to initialize the uncore power info on sockets, right? From the implement in l3fwd-power, it set all die ID and all sockets.
For the platform with a single uncore controller per socket, their uncore driver in DPDK have to ignore other die IDs except die-0 on one socket. right?

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
lib/power/rte_power_uncore.c +0 -0
   {
       int ret = -1;
 

<...>

#38 lore raw

PATCH v2 3/4 test/power: removed function pointer validations

In reply to Sivaprasad Tummala

After refactoring the power library, power management operations are now consistently supported regardless of the operating environment, making function pointer checks unnecessary and thus removed from applications.

v2:
- removed function pointer validation in l3fwd-power app.

---
 app/test/test_power.c         | 95 -----------------------------------
 app/test/test_power_cpufreq.c | 52 -------------------
 app/test/test_power_kvm_vm.c  | 36 -------------
 examples/l3fwd-power/main.c   | 12 ++---
 4 files changed, 4 insertions(+), 191 deletions(-)
app/test/test_power.c +0 -95
index 403adc22d6..5df5848c70 100644
@@ -24,86 +24,6 @@ test_power(void)
 
 #include <rte_power.h>
 
-static int
-check_function_ptrs(void)
-{
-	enum power_management_env env = rte_power_get_env();
-
-	const bool not_null_expected = !(env == PM_ENV_NOT_SET);
-
-	const char *inject_not_string1 = not_null_expected ? " not" : "";
-	const char *inject_not_string2 = not_null_expected ? "" : " not";
-
-	if ((rte_power_freqs == NULL) == not_null_expected) {
-		printf("rte_power_freqs should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-					inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_get_freq == NULL) == not_null_expected) {
-		printf("rte_power_get_freq should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-					inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_set_freq == NULL) == not_null_expected) {
-		printf("rte_power_set_freq should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_freq_up == NULL) == not_null_expected) {
-		printf("rte_power_freq_up should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_freq_down == NULL) == not_null_expected) {
-		printf("rte_power_freq_down should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_freq_max == NULL) == not_null_expected) {
-		printf("rte_power_freq_max should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_freq_min == NULL) == not_null_expected) {
-		printf("rte_power_freq_min should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_turbo_status == NULL) == not_null_expected) {
-		printf("rte_power_turbo_status should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_freq_enable_turbo == NULL) == not_null_expected) {
-		printf("rte_power_freq_enable_turbo should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_freq_disable_turbo == NULL) == not_null_expected) {
-		printf("rte_power_freq_disable_turbo should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-	if ((rte_power_get_capabilities == NULL) == not_null_expected) {
-		printf("rte_power_get_capabilities should%s be NULL, environment has%s been "
-				"initialised\n", inject_not_string1,
-				inject_not_string2);
-		return -1;
-	}
-
-	return 0;
-}
-
 static int
 test_power(void)
 {
@@ -124,10 +44,6 @@ test_power(void)
 		return -1;
 	}
 
-	/* Verify that function pointers are NULL */
-	if (check_function_ptrs() < 0)
-		goto fail_all;
-
 	rte_power_unset_env();
 
 	/* Perform tests for valid environments.*/
@@ -154,22 +70,11 @@ test_power(void)
 			return -1;
 		}
 
-		/* Verify that function pointers are NOT NULL */
-		if (check_function_ptrs() < 0)
-			goto fail_all;
-
 		rte_power_unset_env();
 
-		/* Verify that function pointers are NULL */
-		if (check_function_ptrs() < 0)
-			goto fail_all;
-
 	}
 
 	return 0;
-fail_all:
-	rte_power_unset_env();
-	return -1;
 }
 #endif
 
app/test/test_power_cpufreq.c +0 -52

58 lines not on this page · raw message

app/test/test_power_kvm_vm.c +0 -36
index 464e06002e..a7d104e973 100644
@@ -47,42 +47,6 @@ test_power_kvm_vm(void)
 		return -1;
 	}
 
-	/* verify that function pointers are not NULL */
-	if (rte_power_freqs == NULL) {
-		printf("rte_power_freqs should not be NULL, environment has not been "
-				"initialised\n");
-		return -1;
-	}
-	if (rte_power_get_freq == NULL) {
-		printf("rte_power_get_freq should not be NULL, environment has not "
-				"been initialised\n");
-		return -1;
-	}
-	if (rte_power_set_freq == NULL) {
-		printf("rte_power_set_freq should not be NULL, environment has not "
-				"been initialised\n");
-		return -1;
-	}
-	if (rte_power_freq_up == NULL) {
-		printf("rte_power_freq_up should not be NULL, environment has not "
-				"been initialised\n");
-		return -1;
-	}
-	if (rte_power_freq_down == NULL) {
-		printf("rte_power_freq_down should not be NULL, environment has not "
-				"been initialised\n");
-		return -1;
-	}
-	if (rte_power_freq_max == NULL) {
-		printf("rte_power_freq_max should not be NULL, environment has not "
-				"been initialised\n");
-		return -1;
-	}
-	if (rte_power_freq_min == NULL) {
-		printf("rte_power_freq_min should not be NULL, environment has not "
-				"been initialised\n");
-		return -1;
-	}
 	/* Test initialisation of an out of bounds lcore */
 	ret = rte_power_init(TEST_POWER_VM_LCORE_OUT_OF_BOUNDS);
 	if (ret != -1) {
examples/l3fwd-power/main.c +4 -8
index 2bb6b092c3..6bd76515e6 100644
@@ -440,8 +440,7 @@ power_timer_cb(__rte_unused struct rte_timer *tim,
 	 * check whether need to scale down frequency a step if it sleep a lot.
 	 */
 	if (sleep_time_ratio >= SCALING_DOWN_TIME_RATIO_THRESHOLD) {
-		if (rte_power_freq_down)
-			rte_power_freq_down(lcore_id);
+		rte_power_freq_down(lcore_id);
 	}
 	else if ( (unsigned)(stats[lcore_id].nb_rx_processed /
 		stats[lcore_id].nb_iteration_looped) < MAX_PKT_BURST) {
@@ -449,8 +448,7 @@ power_timer_cb(__rte_unused struct rte_timer *tim,
 		 * scale down a step if average packet per iteration less
 		 * than expectation.
 		 */
-		if (rte_power_freq_down)
-			rte_power_freq_down(lcore_id);
+		rte_power_freq_down(lcore_id);
 	}
 
 	/**
@@ -1344,11 +1342,9 @@ main_legacy_loop(__rte_unused void *dummy)
 			}
 
 			if (lcore_scaleup_hint == FREQ_HIGHEST) {
-				if (rte_power_freq_max)
-					rte_power_freq_max(lcore_id);
+				rte_power_freq_max(lcore_id);
 			} else if (lcore_scaleup_hint == FREQ_HIGHER) {
-				if (rte_power_freq_up)
-					rte_power_freq_up(lcore_id);
+				rte_power_freq_up(lcore_id);
 			}
 		} else {
 			/**
Signature · 1 line
2.34.1
#39 lore raw

PATCH v2 4/4 power/amd_uncore: uncore power management support for AMD EPYC processors

In reply to Sivaprasad Tummala

This patch introduces driver support for power management of uncore components in AMD EPYC processors.

v2:
- fixed typo in comments section.
- added fabric frequency get support for legacy platforms.

---
 drivers/power/amd_uncore/amd_uncore.c | 328 ++++++++++++++++++++++++++
 drivers/power/amd_uncore/amd_uncore.h | 226 ++++++++++++++++++
 drivers/power/amd_uncore/meson.build  |  20 ++
 drivers/power/meson.build             |   1 +
 4 files changed, 575 insertions(+)
 create mode 100644 drivers/power/amd_uncore/amd_uncore.c
 create mode 100644 drivers/power/amd_uncore/amd_uncore.h
 create mode 100644 drivers/power/amd_uncore/meson.build
drivers/power/amd_uncore/amd_uncore.c new file +328 -0
new file mode 100644
index 0000000000..e667a783cd
@@ -0,0 +1,328 @@
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright(c) 2024 Advanced Micro Devices, Inc.
+ */
+
+#include <errno.h>
+#include <dirent.h>
+#include <fnmatch.h>
+
+#include <rte_memcpy.h>
+
+#include "amd_uncore.h"
+#include "power_common.h"
+#include "e_smi/e_smi.h"
+
+#define MAX_NUMA_DIE 8
+
+struct  __rte_cache_aligned uncore_power_info {
+	unsigned int die;                  /* Core die id */
+	unsigned int pkg;                  /* Package id */
+	uint32_t freqs[RTE_MAX_UNCORE_FREQS];  /* Frequency array */
+	uint32_t nb_freqs;                 /* Number of available freqs */
+	uint32_t curr_idx;                 /* Freq index in freqs array */
+	uint32_t max_freq;            /* System max uncore freq */
+	uint32_t min_freq;            /* System min uncore freq */
+};
+
+static struct uncore_power_info uncore_info[RTE_MAX_NUMA_NODES][MAX_NUMA_DIE];
+static int esmi_initialized;
+static int hsmp_proto_ver;
+
+static int
+set_uncore_freq_internal(struct uncore_power_info *ui, uint32_t idx)
+{
+	int ret;
+
+	if (idx >= RTE_MAX_UNCORE_FREQS || idx >= ui->nb_freqs) {
+		POWER_LOG(DEBUG, "Invalid uncore frequency index %u, which "
+				"should be less than %u", idx, ui->nb_freqs);
+		return -1;
+	}
+
+	ret = esmi_apb_disable(ui->pkg, idx);
+	if (ret != ESMI_SUCCESS) {
+		POWER_LOG(ERR, "DF P-state '%u' set failed for pkg %02u",
+				idx, ui->pkg);
+		return -1;
+	}
+
+	POWER_DEBUG_LOG("DF P-state '%u' to be set for pkg %02u die %02u",
+				idx, ui->pkg, ui->die);
+
+	/* write the minimum value first if the target freq is less than current max */
+	ui->curr_idx = idx;
+
+	return 0;
+}
+
+static int
+power_init_for_setting_uncore_freq(struct uncore_power_info *ui)
+{
+	switch (hsmp_proto_ver) {
+	case HSMP_PROTO_VER5:
+		ui->max_freq = 1800000; /* Hz */
+		ui->min_freq = 1200000; /* Hz */
+		break;
+	case HSMP_PROTO_VER2:
+	default:
+		ui->max_freq = 1600000; /* Hz */
+		ui->min_freq = 1200000; /* Hz */
+	}
+
+	return 0;
+}
+
+/*
+ * Get the available uncore frequencies of the specific die.
+ */
+static int
+power_get_available_uncore_freqs(struct uncore_power_info *ui)
+{
+	ui->nb_freqs = 3;
+	if (ui->nb_freqs >= RTE_MAX_UNCORE_FREQS) {
+		POWER_LOG(ERR, "Too many available uncore frequencies: %d",
+				num_uncore_freqs);
+		return -1;
+	}
+
+	/* Generate the uncore freq bucket array. */
+	switch (hsmp_proto_ver) {
+	case HSMP_PROTO_VER5:
+		ui->freqs[0] = 1800000;
+		ui->freqs[1] = 1440000;
+		ui->freqs[2] = 1200000;
+	case HSMP_PROTO_VER2:
+	default:
+		ui->freqs[0] = 1600000;
+		ui->freqs[1] = 1333000;
+		ui->freqs[2] = 1200000;
+	}
+
+	POWER_DEBUG_LOG("%d frequency(s) of pkg %02u die %02u are available",
+			ui->num_uncore_freqs, ui->pkg, ui->die);
+
+	return 0;
+}
+
+static int
+check_pkg_die_values(unsigned int pkg, unsigned int die)
+{
+	unsigned int max_pkgs, max_dies;
+	max_pkgs = power_amd_uncore_get_num_pkgs();
+	if (max_pkgs == 0)
+		return -1;
+	if (pkg >= max_pkgs) {
+		POWER_LOG(DEBUG, "Package number %02u can not exceed %u",
+				pkg, max_pkgs);
+		return -1;
+	}
+
+	max_dies = power_amd_uncore_get_num_dies(pkg);
+	if (max_dies == 0)
+		return -1;
+	if (die >= max_dies) {
+		POWER_LOG(DEBUG, "Die number %02u can not exceed %u",
+				die, max_dies);
+		return -1;
+	}
+
+	return 0;
+}
+
+static void
+power_amd_uncore_esmi_init(void)
+{
+	if (esmi_init() == ESMI_SUCCESS) {
+		if (esmi_hsmp_proto_ver_get(&hsmp_proto_ver) ==
+				ESMI_SUCCESS)
+			esmi_initialized = 1;
+	}
+}
+
+int
+power_amd_uncore_init(unsigned int pkg, unsigned int die)
+{
+	struct uncore_power_info *ui;
+	int ret;
+
+	if (!esmi_initialized) {
+		ret = esmi_init();
+		if (ret != ESMI_SUCCESS) {
+			POWER_LOG(DEBUG, "ESMI Not initialized, drivers not found");
+			return -1;
+		}
+		ret = esmi_hsmp_proto_ver_get(&hsmp_proto_ver);
+		if (ret != ESMI_SUCCESS) {
+			POWER_LOG(DEBUG, "HSMP Proto Version Get failed with "
+					"error %s", esmi_get_err_msg(ret));
+			esmi_exit();
+			return -1;
+		}
+		esmi_initialized = 1;
+	}
+
+	ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	ui = &uncore_info[pkg][die];
+	ui->die = die;
+	ui->pkg = pkg;
+
+	/* Init for setting uncore die frequency */
+	if (power_init_for_setting_uncore_freq(ui) < 0) {
+		POWER_LOG(DEBUG, "Cannot init for setting uncore frequency for "
+				"pkg %02u die %02u", pkg, die);
+		return -1;
+	}
+
+	/* Get the available frequencies */
+	if (power_get_available_uncore_freqs(ui) < 0) {
+		POWER_LOG(DEBUG, "Cannot get available uncore frequencies of "
+				"pkg %02u die %02u", pkg, die);
+		return -1;
+	}
+
+	return 0;
+}
+
+int
+power_amd_uncore_exit(unsigned int pkg, unsigned int die)
+{
+	struct uncore_power_info *ui;
+
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	ui = &uncore_info[pkg][die];
+	ui->nb_freqs = 0;
+
+	if (esmi_initialized) {
+		esmi_exit();
+		esmi_initialized = 0;
+	}
+
+	return 0;
+}
+
+uint32_t
+power_get_amd_uncore_freq(unsigned int pkg, unsigned int die)
+{
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	return uncore_info[pkg][die].curr_idx;
+}
+
+int
+power_set_amd_uncore_freq(unsigned int pkg, unsigned int die, uint32_t index)
+{
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	return set_uncore_freq_internal(&(uncore_info[pkg][die]), index);
+}
+
+int
+power_amd_uncore_freq_max(unsigned int pkg, unsigned int die)
+{
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	return set_uncore_freq_internal(&(uncore_info[pkg][die]), 0);
+}
+
+
+int
+power_amd_uncore_freq_min(unsigned int pkg, unsigned int die)
+{
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	struct uncore_power_info *ui = &uncore_info[pkg][die];
+
+	return set_uncore_freq_internal(&(uncore_info[pkg][die]), ui->nb_freqs - 1);
+}
+
+int
+power_amd_uncore_freqs(unsigned int pkg, unsigned int die, uint32_t *freqs, uint32_t num)
+{
+	struct uncore_power_info *ui;
+
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	if (freqs == NULL) {
+		POWER_LOG(ERR, "NULL buffer supplied");
+		return 0;
+	}
+
+	ui = &uncore_info[pkg][die];
+	if (num < ui->nb_freqs) {
+		POWER_LOG(ERR, "Buffer size is not enough");
+		return 0;
+	}
+	rte_memcpy(freqs, ui->freqs, ui->nb_freqs * sizeof(uint32_t));
+
+	return ui->nb_freqs;
+}
+
+int
+power_amd_uncore_get_num_freqs(unsigned int pkg, unsigned int die)
+{
+	int ret = check_pkg_die_values(pkg, die);
+	if (ret < 0)
+		return -1;
+
+	return uncore_info[pkg][die].nb_freqs;
+}
+
+unsigned int
+power_amd_uncore_get_num_pkgs(void)
+{
+	uint32_t num_pkgs = 0;
+	int ret;
+
+	if (esmi_initialized) {
+		ret = esmi_number_of_sockets_get(&num_pkgs);
+		if (ret != ESMI_SUCCESS) {
+			POWER_LOG(ERR, "Failed to get number of sockets");
+			num_pkgs = 0;
+		}
+	}
+	return num_pkgs;
+}
+
+unsigned int
+power_amd_uncore_get_num_dies(unsigned int pkg)
+{
+	if (pkg >= power_amd_uncore_get_num_pkgs()) {
+		POWER_LOG(ERR, "Invalid package ID");
+		return 0;
+	}
+
+	return 1;
+}
+
+static struct rte_power_uncore_ops amd_uncore_ops = {
+	.name = "amd-hsmp",
+	.cb = power_amd_uncore_esmi_init,
+	.init = power_amd_uncore_init,
+	.exit = power_amd_uncore_exit,
+	.get_avail_freqs = power_amd_uncore_freqs,
+	.get_num_pkgs = power_amd_uncore_get_num_pkgs,
+	.get_num_dies = power_amd_uncore_get_num_dies,
+	.get_num_freqs = power_amd_uncore_get_num_freqs,
+	.get_freq = power_get_amd_uncore_freq,
+	.set_freq = power_set_amd_uncore_freq,
+	.freq_max = power_amd_uncore_freq_max,
+	.freq_min = power_amd_uncore_freq_min,
+};
+
+RTE_POWER_REGISTER_UNCORE_OPS(amd_uncore_ops);
drivers/power/amd_uncore/amd_uncore.h new file +226 -0

226 lines not on this page · raw message

drivers/power/amd_uncore/meson.build new file +20 -0
new file mode 100644
index 0000000000..8cbab47b01
@@ -0,0 +1,20 @@
+# SPDX-License-Identifier: BSD-3-Clause
+# Copyright(c) 2024 Advanced Micro Devices, Inc.
+
+if not is_linux
+    build = false
+    reason = 'only supported on Linux'
+    subdir_done()
+endif
+
+ESMI_header = '#include<e_smi/e_smi.h>'
+lib = cc.find_library('e_smi64', required: false)
+if not lib.found()
+    build = false
+    reason = 'missing dependency, "libe_smi"'
+else
+    ext_deps += lib
+endif
+
+sources = files('amd_uncore.c')
+deps += ['power']
drivers/power/meson.build +1 -0
index c83047af94..4ba5954e13 100644
@@ -7,6 +7,7 @@ drivers = [
         'cppc',
         'kvm_vm',
         'pstate',
+        'amd_uncore',
         'intel_uncore'
 ]
 
Signature · 1 line
2.34.1
#40 lore raw

v2 cover power: refactor power management library

In reply to Sivaprasad Tummala

This patchset refactors the power management library, addressing both core and uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/core/*' and 'drivers/power/uncore/*'.

This refactor significantly improves code organization, enhances clarity, and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

Furthermore, this effort aims to streamline code maintenance by consolidating common functions for cpufreq and cppc across various core drivers, thus reducing code duplication.

Sivaprasad Tummala (4):
power: refactor core power management library
power: refactor uncore power management library test/power: removed function pointer validations power/amd_uncore: uncore power management support for AMD EPYC
processors

 app/test/test_power.c                         |  95 -----
 app/test/test_power_cpufreq.c                 |  52 ---
 app/test/test_power_kvm_vm.c                  |  36 --
 drivers/meson.build                           |   1 +
 .../power/acpi/acpi_cpufreq.c                 |  22 +-
 .../power/acpi/acpi_cpufreq.h                 |   6 +-
 drivers/power/acpi/meson.build                |  10 +
 .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
 .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
 drivers/power/amd_pstate/meson.build          |  10 +
 drivers/power/amd_uncore/amd_uncore.c         | 328 ++++++++++++++++++
 drivers/power/amd_uncore/amd_uncore.h         | 226 ++++++++++++
 drivers/power/amd_uncore/meson.build          |  20 ++
 .../power/cppc/cppc_cpufreq.c                 |  22 +-
 .../power/cppc/cppc_cpufreq.h                 |   8 +-
 drivers/power/cppc/meson.build                |  10 +
 .../power/intel_uncore/intel_uncore.c         |  18 +-
 .../power/intel_uncore/intel_uncore.h         |   8 +-
 drivers/power/intel_uncore/meson.build        |   6 +
 .../power/kvm_vm}/guest_channel.c             |   0
 .../power/kvm_vm}/guest_channel.h             |   0
 .../power/kvm_vm/kvm_vm.c                     |  22 +-
 .../power/kvm_vm/kvm_vm.h                     |   6 +-
 drivers/power/kvm_vm/meson.build              |  16 +
 drivers/power/meson.build                     |  14 +
 drivers/power/pstate/meson.build              |  10 +
 .../power/pstate/pstate_cpufreq.c             |  22 +-
 .../power/pstate/pstate_cpufreq.h             |   6 +-
 examples/l3fwd-power/main.c                   |  12 +-
 lib/power/meson.build                         |   9 +-
 lib/power/power_common.c                      |   2 +-
 lib/power/power_common.h                      |  16 +-
 lib/power/rte_power.c                         | 291 ++++++----------
 lib/power/rte_power.h                         | 139 +++++---
 lib/power/rte_power_core_ops.h                | 208 +++++++++++
 lib/power/rte_power_uncore.c                  | 205 +++++------
 lib/power/rte_power_uncore.h                  |  87 +++--
 lib/power/rte_power_uncore_ops.h              | 239 +++++++++++++
 lib/power/version.map                         |  15 +
 39 files changed, 1604 insertions(+), 625 deletions(-)
 rename lib/power/power_acpi_cpufreq.c => drivers/power/acpi/acpi_cpufreq.c (95%)
 rename lib/power/power_acpi_cpufreq.h => drivers/power/acpi/acpi_cpufreq.h (98%)
 create mode 100644 drivers/power/acpi/meson.build
 rename lib/power/power_amd_pstate_cpufreq.c => drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%)
 rename lib/power/power_amd_pstate_cpufreq.h => drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%)
 create mode 100644 drivers/power/amd_pstate/meson.build
 create mode 100644 drivers/power/amd_uncore/amd_uncore.c
 create mode 100644 drivers/power/amd_uncore/amd_uncore.h
 create mode 100644 drivers/power/amd_uncore/meson.build
 rename lib/power/power_cppc_cpufreq.c => drivers/power/cppc/cppc_cpufreq.c (95%)
 rename lib/power/power_cppc_cpufreq.h => drivers/power/cppc/cppc_cpufreq.h (97%)
 create mode 100644 drivers/power/cppc/meson.build
 rename lib/power/power_intel_uncore.c => drivers/power/intel_uncore/intel_uncore.c (95%)
 rename lib/power/power_intel_uncore.h => drivers/power/intel_uncore/intel_uncore.h (97%)
 create mode 100644 drivers/power/intel_uncore/meson.build
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%)
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%)
 rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c (82%)
 rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h (98%)
 create mode 100644 drivers/power/kvm_vm/meson.build
 create mode 100644 drivers/power/meson.build
 create mode 100644 drivers/power/pstate/meson.build
 rename lib/power/power_pstate_cpufreq.c => drivers/power/pstate/pstate_cpufreq.c (96%)
 rename lib/power/power_pstate_cpufreq.h => drivers/power/pstate/pstate_cpufreq.h (98%)
 create mode 100644 lib/power/rte_power_core_ops.h
 create mode 100644 lib/power/rte_power_uncore_ops.h
Signature · 1 line
2.34.1
#41 lore raw
In reply to Sivaprasad Tummala
Quoted 14 lines · On Mon, 26 Aug 2024 13:06:45 +0000 Sivaprasad Tummala…

This patchset refactors the power management library, addressing both core and uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/core/*' and 'drivers/power/uncore/*'.

This refactor significantly improves code organization, enhances clarity, and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

Furthermore, this effort aims to streamline code maintenance by consolidating common functions for cpufreq and cppc across various core drivers, thus reducing code duplication.

Quoted 6 lines

Sivaprasad Tummala (4):
power: refactor core power management library
power: refactor uncore power management library test/power: removed function pointer validations power/amd_uncore: uncore power management support for AMD EPYC
processors

Build fails and lots of comments. Please redo.

#42 lore raw

v3 cover power: refactor power management library

In reply to Sivaprasad Tummala

This patchset refactors the power management library, addressing both core and uncore power management. The primary changes involve the creation of dedicated directories for each driver within 'drivers/power/core/*' and 'drivers/power/uncore/*'.

This refactor significantly improves code organization, enhances clarity, and boosts maintainability. It lays the foundation for more focused development on individual drivers and facilitates seamless integration of future enhancements, particularly the AMD uncore driver.

Furthermore, this effort aims to streamline code maintenance by consolidating common functions for cpufreq and cppc across various core drivers, thus reducing code duplication.

Sivaprasad Tummala (5):
power: refactor core power management library
power: refactor uncore power management library test/power: removed function pointer validations power/amd_uncore: uncore support for AMD EPYC processors
maintainers: update for drivers/power

 MAINTAINERS                                   |   1 +
 app/test/test_power.c                         |  95 -----
 app/test/test_power_cpufreq.c                 |  52 ---
 app/test/test_power_kvm_vm.c                  |  36 --
 drivers/meson.build                           |   1 +
 .../power/acpi/acpi_cpufreq.c                 |  22 +-
 .../power/acpi/acpi_cpufreq.h                 |   6 +-
 drivers/power/acpi/meson.build                |  10 +
 .../power/amd_pstate/amd_pstate_cpufreq.c     |  24 +-
 .../power/amd_pstate/amd_pstate_cpufreq.h     |   8 +-
 drivers/power/amd_pstate/meson.build          |  10 +
 drivers/power/amd_uncore/amd_uncore.c         | 328 ++++++++++++++++++
 drivers/power/amd_uncore/amd_uncore.h         | 226 ++++++++++++
 drivers/power/amd_uncore/meson.build          |  20 ++
 .../power/cppc/cppc_cpufreq.c                 |  22 +-
 .../power/cppc/cppc_cpufreq.h                 |   8 +-
 drivers/power/cppc/meson.build                |  10 +
 .../power/intel_uncore/intel_uncore.c         |  18 +-
 .../power/intel_uncore/intel_uncore.h         |   8 +-
 drivers/power/intel_uncore/meson.build        |   6 +
 .../power/kvm_vm}/guest_channel.c             |   0
 .../power/kvm_vm}/guest_channel.h             |   0
 .../power/kvm_vm/kvm_vm.c                     |  22 +-
 .../power/kvm_vm/kvm_vm.h                     |   6 +-
 drivers/power/kvm_vm/meson.build              |  16 +
 drivers/power/meson.build                     |  14 +
 drivers/power/pstate/meson.build              |  10 +
 .../power/pstate/pstate_cpufreq.c             |  22 +-
 .../power/pstate/pstate_cpufreq.h             |   6 +-
 examples/l3fwd-power/main.c                   |  12 +-
 lib/power/meson.build                         |   9 +-
 lib/power/power_common.c                      |   2 +-
 lib/power/power_common.h                      |  16 +-
 lib/power/rte_power.c                         | 286 +++++----------
 lib/power/rte_power.h                         | 139 +++++---
 lib/power/rte_power_cpufreq_api.h             | 208 +++++++++++
 lib/power/rte_power_uncore.c                  | 206 +++++------
 lib/power/rte_power_uncore.h                  |  87 +++--
 lib/power/rte_power_uncore_ops.h              | 239 +++++++++++++
 lib/power/version.map                         |  15 +
 40 files changed, 1602 insertions(+), 624 deletions(-)
 rename lib/power/power_acpi_cpufreq.c => drivers/power/acpi/acpi_cpufreq.c (95%)
 rename lib/power/power_acpi_cpufreq.h => drivers/power/acpi/acpi_cpufreq.h (98%)
 create mode 100644 drivers/power/acpi/meson.build
 rename lib/power/power_amd_pstate_cpufreq.c => drivers/power/amd_pstate/amd_pstate_cpufreq.c (95%)
 rename lib/power/power_amd_pstate_cpufreq.h => drivers/power/amd_pstate/amd_pstate_cpufreq.h (97%)
 create mode 100644 drivers/power/amd_pstate/meson.build
 create mode 100644 drivers/power/amd_uncore/amd_uncore.c
 create mode 100644 drivers/power/amd_uncore/amd_uncore.h
 create mode 100644 drivers/power/amd_uncore/meson.build
 rename lib/power/power_cppc_cpufreq.c => drivers/power/cppc/cppc_cpufreq.c (95%)
 rename lib/power/power_cppc_cpufreq.h => drivers/power/cppc/cppc_cpufreq.h (97%)
 create mode 100644 drivers/power/cppc/meson.build
 rename lib/power/power_intel_uncore.c => drivers/power/intel_uncore/intel_uncore.c (95%)
 rename lib/power/power_intel_uncore.h => drivers/power/intel_uncore/intel_uncore.h (97%)
 create mode 100644 drivers/power/intel_uncore/meson.build
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.c (100%)
 rename {lib/power => drivers/power/kvm_vm}/guest_channel.h (100%)
 rename lib/power/power_kvm_vm.c => drivers/power/kvm_vm/kvm_vm.c (82%)
 rename lib/power/power_kvm_vm.h => drivers/power/kvm_vm/kvm_vm.h (98%)
 create mode 100644 drivers/power/kvm_vm/meson.build
 create mode 100644 drivers/power/meson.build
 create mode 100644 drivers/power/pstate/meson.build
 rename lib/power/power_pstate_cpufreq.c => drivers/power/pstate/pstate_cpufreq.c (96%)
 rename lib/power/power_pstate_cpufreq.h => drivers/power/pstate/pstate_cpufreq.h (98%)
 create mode 100644 lib/power/rte_power_cpufreq_api.h
 create mode 100644 lib/power/rte_power_uncore_ops.h
Signature · 1 line
2.34.1