-
Notifications
You must be signed in to change notification settings - Fork 44
/
amdgpu.go
276 lines (236 loc) · 7.88 KB
/
amdgpu.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* Copyright 2018 Advanced Micro Devices, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
// Package amdgpu is a collection of utility functions to access various properties
// of AMD GPU via Linux kernel interfaces like sysfs and ioctl (using libdrm.)
package amdgpu
// #cgo pkg-config: libdrm libdrm_amdgpu
// #include <stdint.h>
// #include <xf86drm.h>
// #include <drm.h>
// #include <amdgpu.h>
// #include <amdgpu_drm.h>
import "C"
import (
"bufio"
"errors"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/golang/glog"
)
// FamilyID to String convert AMDGPU_FAMILY_* into string
// AMDGPU_FAMILY_* as defined in https://github.com/torvalds/linux/blob/master/include/uapi/drm/amdgpu_drm.h#L986
func FamilyIDtoString(familyId uint32) (string, error) {
switch familyId {
case C.AMDGPU_FAMILY_SI:
return "SI", nil
case C.AMDGPU_FAMILY_CI:
return "CI", nil
case C.AMDGPU_FAMILY_KV:
return "KV", nil
case C.AMDGPU_FAMILY_VI:
return "VI", nil
case C.AMDGPU_FAMILY_CZ:
return "CZ", nil
case C.AMDGPU_FAMILY_AI:
return "AI", nil
case C.AMDGPU_FAMILY_RV:
return "RV", nil
case C.AMDGPU_FAMILY_NV:
return "NV", nil
default:
ret := ""
err := fmt.Errorf("Unknown Family ID: %d", familyId)
return ret, err
}
}
func GetCardFamilyName(cardName string) (string, error) {
devHandle, err := openAMDGPU(cardName)
if err != nil {
return "", err
}
defer C.amdgpu_device_deinitialize(devHandle)
var info C.struct_amdgpu_gpu_info
rc := C.amdgpu_query_gpu_info(devHandle, &info)
if rc < 0 {
return "", fmt.Errorf("Fail to get FamilyID %s: %d", cardName, rc)
}
return FamilyIDtoString(uint32(info.family_id))
}
// GetAMDGPUs return a map of AMD GPU on a node identified by the part of the pci address
func GetAMDGPUs() map[string]map[string]int {
if _, err := os.Stat("/sys/module/amdgpu/drivers/"); err != nil {
glog.Warningf("amdgpu driver unavailable: %s", err)
return make(map[string]map[string]int)
}
//ex: /sys/module/amdgpu/drivers/pci:amdgpu/0000:19:00.0
matches, _ := filepath.Glob("/sys/module/amdgpu/drivers/pci:amdgpu/[0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F]:*")
devices := make(map[string]map[string]int)
for _, path := range matches {
glog.Info(path)
devPaths, _ := filepath.Glob(path + "/drm/*")
devices[filepath.Base(path)] = make(map[string]int)
for _, devPath := range devPaths {
switch name := filepath.Base(devPath); {
case name[0:4] == "card":
devices[filepath.Base(path)][name[0:4]], _ = strconv.Atoi(name[4:])
case name[0:7] == "renderD":
devices[filepath.Base(path)][name[0:7]], _ = strconv.Atoi(name[7:])
}
}
}
return devices
}
// AMDGPU check if a particular card is an AMD GPU by checking the device's vendor ID
func AMDGPU(cardName string) bool {
sysfsVendorPath := "/sys/class/drm/" + cardName + "/device/vendor"
b, err := ioutil.ReadFile(sysfsVendorPath)
if err == nil {
vid := strings.TrimSpace(string(b))
// AMD vendor ID is 0x1002
if "0x1002" == vid {
return true
}
} else {
glog.Errorf("Error opening %s: %s", sysfsVendorPath, err)
}
return false
}
func openAMDGPU(cardName string) (C.amdgpu_device_handle, error) {
if !AMDGPU(cardName) {
return nil, fmt.Errorf("%s is not an AMD GPU", cardName)
}
devPath := "/dev/dri/" + cardName
dev, err := os.Open(devPath)
if err != nil {
return nil, fmt.Errorf("Fail to open %s: %s", devPath, err)
}
defer dev.Close()
devFd := C.int(dev.Fd())
var devHandle C.amdgpu_device_handle
var major C.uint32_t
var minor C.uint32_t
rc := C.amdgpu_device_initialize(devFd, &major, &minor, &devHandle)
if rc < 0 {
return nil, fmt.Errorf("Fail to initialize %s: %d", devPath, err)
}
glog.Infof("Initialized AMD GPU version: major %d, minor %d", major, minor)
return devHandle, nil
}
// DevFunctional does a simple check on whether a particular GPU is working
// by attempting to open the device
func DevFunctional(cardName string) bool {
devHandle, err := openAMDGPU(cardName)
if err != nil {
glog.Errorf("%s", err)
return false
}
defer C.amdgpu_device_deinitialize(devHandle)
return true
}
// GetFirmwareVersions obtain a subset of firmware and feature version via libdrm
// amdgpu_query_firmware_version
func GetFirmwareVersions(cardName string) (map[string]uint32, map[string]uint32, error) {
devHandle, err := openAMDGPU(cardName)
if err != nil {
return map[string]uint32{}, map[string]uint32{}, err
}
defer C.amdgpu_device_deinitialize(devHandle)
var ver C.uint32_t
var feat C.uint32_t
featVersions := map[string]uint32{}
fwVersions := map[string]uint32{}
C.amdgpu_query_firmware_version(devHandle, C.AMDGPU_INFO_FW_VCE, 0, 0, &ver, &feat)
featVersions["VCE"] = uint32(feat)
fwVersions["VCE"] = uint32(ver)
C.amdgpu_query_firmware_version(devHandle, C.AMDGPU_INFO_FW_UVD, 0, 0, &ver, &feat)
featVersions["UVD"] = uint32(feat)
fwVersions["UVD"] = uint32(ver)
C.amdgpu_query_firmware_version(devHandle, C.AMDGPU_INFO_FW_GMC, 0, 0, &ver, &feat)
featVersions["MC"] = uint32(feat)
fwVersions["MC"] = uint32(ver)
C.amdgpu_query_firmware_version(devHandle, C.AMDGPU_INFO_FW_GFX_ME, 0, 0, &ver, &feat)
featVersions["ME"] = uint32(feat)
fwVersions["ME"] = uint32(ver)
C.amdgpu_query_firmware_version(devHandle, C.AMDGPU_INFO_FW_GFX_PFP, 0, 0, &ver, &feat)
featVersions["PFP"] = uint32(feat)
fwVersions["PFP"] = uint32(ver)
C.amdgpu_query_firmware_version(devHandle, C.AMDGPU_INFO_FW_GFX_CE, 0, 0, &ver, &feat)
featVersions["CE"] = uint32(feat)
fwVersions["CE"] = uint32(ver)
C.amdgpu_query_firmware_version(devHandle, C.AMDGPU_INFO_FW_GFX_RLC, 0, 0, &ver, &feat)
featVersions["RLC"] = uint32(feat)
fwVersions["RLC"] = uint32(ver)
C.amdgpu_query_firmware_version(devHandle, C.AMDGPU_INFO_FW_GFX_MEC, 0, 0, &ver, &feat)
featVersions["MEC"] = uint32(feat)
fwVersions["MEC"] = uint32(ver)
C.amdgpu_query_firmware_version(devHandle, C.AMDGPU_INFO_FW_SMC, 0, 0, &ver, &feat)
featVersions["SMC"] = uint32(feat)
fwVersions["SMC"] = uint32(ver)
C.amdgpu_query_firmware_version(devHandle, C.AMDGPU_INFO_FW_SDMA, 0, 0, &ver, &feat)
featVersions["SDMA0"] = uint32(feat)
fwVersions["SDMA0"] = uint32(ver)
return featVersions, fwVersions, nil
}
// ParseTopologyProperties parse for a property value in kfd topology file
// The format is usually one entry per line <name> <value>. Examples in
// testdata/topology-parsing/.
func ParseTopologyProperties(path string, re *regexp.Regexp) (int64, error) {
f, e := os.Open(path)
if e != nil {
return 0, e
}
e = errors.New("Topology property not found. Regex: " + re.String())
v := int64(0)
scanner := bufio.NewScanner(f)
for scanner.Scan() {
m := re.FindStringSubmatch(scanner.Text())
if m == nil {
continue
}
v, e = strconv.ParseInt(m[1], 0, 64)
break
}
f.Close()
return v, e
}
var fwVersionRe = regexp.MustCompile(`(\w+) feature version: (\d+), firmware version: (0x[0-9a-fA-F]+)`)
func parseDebugFSFirmwareInfo(path string) (map[string]uint32, map[string]uint32) {
feat := make(map[string]uint32)
fw := make(map[string]uint32)
glog.Info("Parsing " + path)
f, e := os.Open(path)
if e == nil {
scanner := bufio.NewScanner(f)
var v int64
for scanner.Scan() {
m := fwVersionRe.FindStringSubmatch(scanner.Text())
if m != nil {
v, _ = strconv.ParseInt(m[2], 0, 32)
feat[m[1]] = uint32(v)
v, _ = strconv.ParseInt(m[3], 0, 32)
fw[m[1]] = uint32(v)
}
}
} else {
glog.Error("Fail to open " + path)
}
return feat, fw
}