-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmt.go
103 lines (87 loc) · 1.79 KB
/
pmt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main
import (
"encoding/binary"
"errors"
"fmt"
"io/ioutil"
"math"
"os"
"sync"
)
type PMValue struct {
Address uint
Value float32
}
const pmtFile = "/sys/kernel/ryzen_smu_drv/pm_table"
const pmtVersionFile = "/sys/kernel/ryzen_smu_drv/pm_table_version"
var pmt map[string]PMValue
var pmValues []string
var mut sync.RWMutex
// read pm table into memory
func parsePMT() error {
b, err := ioutil.ReadFile(pmtFile)
if err != nil {
return err
}
mut.Lock()
pmt = map[string]PMValue{}
i := uint(0)
for _, val := range pmValues {
v := getValueFromPMT(&b, i)
// json can't deal with inf
if math.IsInf(float64(v), 0) {
v = 0
}
pmt[val] = PMValue{
Address: i,
Value: v,
}
i += 4
}
mut.Unlock()
return nil
}
// get pm table version
func readPMTVersion() (uint32, error) {
b, err := ioutil.ReadFile(pmtVersionFile)
if err != nil {
if os.IsNotExist(err) {
return 0, errors.New("PM version file not found.\nMake sure the ryzen_smu kernel module is loaded")
}
return 0, err
}
return binary.LittleEndian.Uint32(b), nil
}
// check for
func setPMTLayout() error {
v, err := readPMTVersion()
if err != nil {
return err
}
switch v {
case 0x370002:
case 0x370003:
pmValues = tab370002_3
break
case 0x370004:
pmValues = tab370004
break
case 0x370005:
pmValues = tab370005
break
default:
return fmt.Errorf("Unsupported pm table version detected: 0x%x", v)
}
fmt.Printf("Detected PM table version: 0x%x\n", v)
return nil
}
// get single value from our pm table
func getValueFromPMT(bytes *[]byte, index uint) float32 {
return floatFromBytes((*bytes)[index : index+4])
}
// convert byte slice to float value
func floatFromBytes(bytes []byte) float32 {
bits := binary.LittleEndian.Uint32(bytes)
float := math.Float32frombits(bits)
return float
}