-
Notifications
You must be signed in to change notification settings - Fork 17
/
sst_if.go
144 lines (118 loc) · 3.79 KB
/
sst_if.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
/*
Copyright 2021 Intel Corporation
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 sst
//go:generate ./gen_sst_types.sh
import (
"fmt"
"math"
"os"
"syscall"
"unsafe"
"github.com/intel/goresctrl/pkg/utils"
)
// cpuMap holds the logical to punit cpu mapping table
var cpuMap = make(map[utils.ID]utils.ID)
// punitCPU returns the PUNIT CPU id corresponding a given Linux logical CPU
func punitCPU(cpu utils.ID) (utils.ID, error) {
if id, ok := cpuMap[cpu]; ok {
return id, nil
}
id, err := getCPUMapping(cpu)
if err == nil {
cpuMap[cpu] = id
}
return id, err
}
// isstIoctl is a helper for executing ioctls on the linux isst_if device driver
func isstIoctl(ioctl uintptr, req uintptr) error {
devPath := isstDevPath()
f, err := os.Open(devPath)
if err != nil {
return fmt.Errorf("failed to open isst device %q: %v", devPath, err)
}
defer f.Close()
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, uintptr(f.Fd()), ioctl, req); errno != 0 {
return errno
}
return nil
}
// getCPUMapping gets mapping of Linux logical CPU numbers to (package-specific)
// PUNIT CPU number for one cpu. This is needed because the PUNIT CPU/core
// numbering differs from the Linux kernel numbering (exposed via sysfs) which
// is based on APIC.
func getCPUMapping(cpu utils.ID) (utils.ID, error) {
if cpu < 0 || cpu > math.MaxUint32 {
return utils.Unknown, fmt.Errorf("Invalid CPU number %d", cpu)
}
req := isstIfCPUMaps{
Cmd_count: 1,
Cpu_map: [1]isstIfCPUMap{
{Logical_cpu: uint32(cpu)},
},
}
if err := isstIoctl(ISST_IF_GET_PHY_ID, uintptr(unsafe.Pointer(&req))); err != nil {
return -1, fmt.Errorf("failed to get CPU mapping for cpu %d: %v", cpu, err)
}
return utils.ID(req.Cpu_map[0].Physical_cpu), nil
}
// sendMboxCmd sends one mailbox command to PUNIT
func sendMboxCmd(cpu utils.ID, cmd uint16, subCmd uint16, parameter uint32, reqData uint32) (uint32, error) {
if cpu < 0 || cpu > math.MaxUint32 {
return 0, fmt.Errorf("Invalid CPU number %d", cpu)
}
req := isstIfMboxCmds{
Cmd_count: 1,
Mbox_cmd: [1]isstIfMboxCmd{
{
Logical_cpu: uint32(cpu),
Command: cmd,
Sub_command: subCmd,
Parameter: parameter,
Req_data: reqData,
},
},
}
sstlog.Debugf("MBOX SEND cpu: %d cmd: %#02x sub: %#02x data: %#x", cpu, cmd, subCmd, reqData)
if err := isstIoctl(ISST_IF_MBOX_COMMAND, uintptr(unsafe.Pointer(&req))); err != nil {
return 0, fmt.Errorf("Mbox command failed with %v", err)
}
sstlog.Debugf("MBOX RECV data: %#x", req.Mbox_cmd[0].Resp_data)
return req.Mbox_cmd[0].Resp_data, nil
}
// sendMMIOCmd sends one MMIO command to PUNIT
func sendMMIOCmd(cpu utils.ID, reg uint32, value uint32, doWrite bool) (uint32, error) {
if cpu < 0 || cpu > math.MaxUint32 {
return 0, fmt.Errorf("Invalid CPU number %d", cpu)
}
var ReadWrite uint32
if doWrite {
ReadWrite = 1
}
req := isstIfIoRegs{
Req_count: 1,
Io_reg: [1]isstIfIoReg{
{
Logical_cpu: uint32(cpu),
Reg: reg,
Value: value,
Read_write: ReadWrite,
},
},
}
sstlog.Debugf("MMIO SEND cpu: %d reg: %#x value: %#x write: %t", cpu, reg, value, doWrite)
if err := isstIoctl(ISST_IF_IO_CMD, uintptr(unsafe.Pointer(&req))); err != nil {
return 0, fmt.Errorf("MMIO command failed with %v", err)
}
sstlog.Debugf("MMIO RECV data: %#x", req.Io_reg[0].Value)
return req.Io_reg[0].Value, nil
}