Skip to content

Commit e0c0bd6

Browse files
authored
Merge pull request #1518 from uniemimu/fake
gpu_fakedev: better dra support
2 parents dfe75b1 + 7ed30b2 commit e0c0bd6

File tree

1 file changed

+56
-20
lines changed

1 file changed

+56
-20
lines changed

cmd/gpu_fakedev/gpu_fakedev.go

Lines changed: 56 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import (
4242
"io/fs"
4343
"log"
4444
"os"
45+
"path/filepath"
46+
"strconv"
4547

4648
"golang.org/x/sys/unix"
4749
)
@@ -78,36 +80,36 @@ type genOptions struct {
7880
}
7981

8082
func addSysfsDriTree(root string, opts *genOptions, i int) error {
81-
card := cardBase + i
82-
base := fmt.Sprintf("%s/class/drm/card%d", root, card)
83+
card := fmt.Sprintf("card%d", cardBase+i)
84+
base := filepath.Join(root, "class", "drm", card)
8385

8486
if err := os.MkdirAll(base, dirMode); err != nil {
8587
return err
8688
}
8789
opts.dirs++
8890

89-
data := []byte(fmt.Sprintf("%d", opts.DevMemSize))
90-
file := fmt.Sprintf("%s/lmem_total_bytes", base)
91+
data := []byte(strconv.Itoa(opts.DevMemSize))
92+
file := filepath.Join(base, "lmem_total_bytes")
9193

9294
if err := os.WriteFile(file, data, fileMode); err != nil {
9395
return err
9496
}
9597
opts.files++
9698

97-
path := fmt.Sprintf("%s/device/drm/card%d", base, card)
99+
path := filepath.Join(base, "device", "drm", card)
98100
if err := os.MkdirAll(path, dirMode); err != nil {
99101
return err
100102
}
101103
opts.dirs++
102104

103-
path = fmt.Sprintf("%s/device/drm/renderD%d", base, renderBase+i)
105+
path = filepath.Join(base, "device", "drm", fmt.Sprintf("renderD%d", renderBase+i))
104106
if err := os.Mkdir(path, dirMode); err != nil {
105107
return err
106108
}
107109
opts.dirs++
108110

109111
data = []byte("0x8086")
110-
file = fmt.Sprintf("%s/device/vendor", base)
112+
file = filepath.Join(base, "device", "vendor")
111113

112114
if err := os.WriteFile(file, data, fileMode); err != nil {
113115
return err
@@ -119,17 +121,17 @@ func addSysfsDriTree(root string, opts *genOptions, i int) error {
119121
node = i / opts.DevsPerNode
120122
}
121123

122-
data = []byte(fmt.Sprintf("%d", node))
123-
file = fmt.Sprintf("%s/device/numa_node", base)
124+
data = []byte(strconv.Itoa(node))
125+
file = filepath.Join(base, "device", "numa_node")
124126

125127
if err := os.WriteFile(file, data, fileMode); err != nil {
126128
return err
127129
}
128130
opts.files++
129131

130132
if opts.VfsPerPf > 0 && i%(opts.VfsPerPf+1) == 0 {
131-
data = []byte(fmt.Sprintf("%d", opts.VfsPerPf))
132-
file = fmt.Sprintf("%s/device/sriov_numvfs", base)
133+
data = []byte(strconv.Itoa(opts.VfsPerPf))
134+
file = filepath.Join(base, "device", "sriov_numvfs")
133135

134136
if err := os.WriteFile(file, data, fileMode); err != nil {
135137
return err
@@ -138,7 +140,7 @@ func addSysfsDriTree(root string, opts *genOptions, i int) error {
138140
}
139141

140142
for tile := 0; tile < opts.TilesPerDev; tile++ {
141-
path := fmt.Sprintf("%s/gt/gt%d", base, tile)
143+
path := filepath.Join(base, "gt", fmt.Sprintf("gt%d", tile))
142144
if err := os.MkdirAll(path, dirMode); err != nil {
143145
return err
144146
}
@@ -148,24 +150,44 @@ func addSysfsDriTree(root string, opts *genOptions, i int) error {
148150
return nil
149151
}
150152

151-
func addDevfsDriTree(root string, opts *genOptions, i int) error {
152-
base := fmt.Sprintf("%s/dri", root)
153+
func addSysfsBusTree(root string, opts *genOptions, i int) error {
154+
pciName := fmt.Sprintf("0000:00:0%d.0", i)
155+
base := filepath.Join(root, "bus", "pci", "drivers", "i915", pciName)
156+
153157
if err := os.MkdirAll(base, dirMode); err != nil {
154158
return err
155159
}
156160
opts.dirs++
157161

162+
data := []byte("0x4905")
163+
file := filepath.Join(base, "device")
164+
165+
if err := os.WriteFile(file, data, fileMode); err != nil {
166+
return err
167+
}
168+
opts.files++
169+
170+
drm := filepath.Join(base, "drm")
171+
if err := os.MkdirAll(drm, dirMode); err != nil {
172+
return err
173+
}
174+
opts.dirs++
175+
176+
return addDeviceNodes(drm, opts, i)
177+
}
178+
179+
func addDeviceNodes(base string, opts *genOptions, i int) error {
158180
mode := uint32(fileMode | devNullType)
159181
devid := int(unix.Mkdev(uint32(devNullMajor), uint32(devNullMinor)))
160182

161-
file := fmt.Sprintf("%s/card%d", base, cardBase+i)
183+
file := filepath.Join(base, fmt.Sprintf("card%d", cardBase+i))
162184
if err := unix.Mknod(file, mode, devid); err != nil {
163185
return fmt.Errorf("NULL device (%d:%d) node creation failed for '%s': %w",
164186
devNullMajor, devNullMinor, file, err)
165187
}
166188
opts.devs++
167189

168-
file = fmt.Sprintf("%s/renderD%d", base, renderBase+i)
190+
file = filepath.Join(base, fmt.Sprintf("renderD%d", renderBase+i))
169191
if err := unix.Mknod(file, mode, devid); err != nil {
170192
return fmt.Errorf("NULL device (%d:%d) node creation failed for '%s': %w",
171193
devNullMajor, devNullMinor, file, err)
@@ -175,14 +197,24 @@ func addDevfsDriTree(root string, opts *genOptions, i int) error {
175197
return nil
176198
}
177199

200+
func addDevfsDriTree(root string, opts *genOptions, i int) error {
201+
base := filepath.Join(root, "dri")
202+
if err := os.MkdirAll(base, dirMode); err != nil {
203+
return err
204+
}
205+
opts.dirs++
206+
207+
return addDeviceNodes(base, opts, i)
208+
}
209+
178210
func addDebugfsDriTree(root string, opts *genOptions, i int) error {
179-
base := fmt.Sprintf("%s/kernel/debug/dri/%d", root, i)
211+
base := filepath.Join(root, "kernel", "debug", "dri", strconv.Itoa(i))
180212
if err := os.MkdirAll(base, dirMode); err != nil {
181213
return err
182214
}
183215
opts.dirs++
184216

185-
path := fmt.Sprintf("%s/i915_capabilities", base)
217+
path := filepath.Join(base, "i915_capabilities")
186218
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_EXCL, fileMode)
187219

188220
if err != nil {
@@ -212,8 +244,8 @@ func removeExistingDir(path, name string) {
212244
return
213245
}
214246

215-
if name == "sysfs" && len(entries) > 2 {
216-
log.Fatalf("ERROR: >2 entries in '%s' - real sysfs?", path)
247+
if name == "sysfs" && len(entries) > 3 {
248+
log.Fatalf("ERROR: >3 entries in '%s' - real sysfs?", path)
217249
}
218250

219251
if name == "devfs" && (entries[0].Name() != "dri" || len(entries) > 1) {
@@ -251,6 +283,10 @@ func generateDriFiles(opts genOptions) {
251283
if err := addDevfsDriTree(devfsPath, &opts, i); err != nil {
252284
log.Fatalf("ERROR: dev-%d devfs tree generation failed: %v", i, err)
253285
}
286+
287+
if err := addSysfsBusTree(sysfsPath, &opts, i); err != nil {
288+
log.Fatalf("ERROR: dev-%d sysfs bus tree generation failed: %v", i, err)
289+
}
254290
}
255291
log.Printf("Done, created %d dirs, %d devices and %d files.", opts.dirs, opts.devs, opts.files)
256292
}

0 commit comments

Comments
 (0)