# \[PATCH\] tools/power/turbostat: Fix turbostat for AMD Zen CPUs

- From: Bas Nieuwenhuizen \<hidden\>
- Date: 2021-01-23 15:50:27 
- List: linux-pm
- Page: https://ratatoskr.run/linux-pm/2021/01/9310823
- Thread: https://ratatoskr.run/linux-pm/2021/01/9310823/t.md

```text
On my Zen+ system turbostat started exiting, which I tracked
down to the MSR_PKG_ENERGY_STAT read failing because
offset_to_idx wasn't returning a non-negative index.

This adds the MSR to the index system as alternative for
MSR_PKG_ENERGY_STATUS.

Signed-off-by: Bas Nieuwenhuizen <redacted>
Fixes: 9972d5d84d76 ("tools/power turbostat: Enable accumulate RAPL display")
---
 tools/power/x86/turbostat/turbostat.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/tools/power/x86/turbostat/turbostat.c b/tools/power/x86/turbostat/turbostat.c
index 389ea5209a83..6b9528d96c48 100644
--- a/tools/power/x86/turbostat/turbostat.c
+++ b/tools/power/x86/turbostat/turbostat.c
@@ -297,7 +297,10 @@ int idx_to_offset(int idx)
 
 	switch (idx) {
 	case IDX_PKG_ENERGY:
-		offset = MSR_PKG_ENERGY_STATUS;
+		if (do_rapl & RAPL_PKG)
+			offset = MSR_PKG_ENERGY_STATUS;
+		else
+			offset = MSR_PKG_ENERGY_STAT;
 		break;
 	case IDX_DRAM_ENERGY:
 		offset = MSR_DRAM_ENERGY_STATUS;
@@ -326,6 +329,7 @@ int offset_to_idx(int offset)
 
 	switch (offset) {
 	case MSR_PKG_ENERGY_STATUS:
+	case MSR_PKG_ENERGY_STAT:
 		idx = IDX_PKG_ENERGY;
 		break;
 	case MSR_DRAM_ENERGY_STATUS:
@@ -353,7 +357,7 @@ int idx_valid(int idx)
 {
 	switch (idx) {
 	case IDX_PKG_ENERGY:
-		return do_rapl & RAPL_PKG;
+		return do_rapl & (RAPL_PKG | RAPL_AMD_F17H);
 	case IDX_DRAM_ENERGY:
 		return do_rapl & RAPL_DRAM;
 	case IDX_PP0_ENERGY:
-- 
2.29.2

```
