forked from adurbin/iotools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scom.c
251 lines (205 loc) · 5.73 KB
/
scom.c
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
/*
Copyright 2013 Google Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#if defined(__powerpc64__) || defined(__powerpc__)
/*
* Quick POWER CPU SCOM register access, requires linux SCOM debugfs support.
*/
#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <inttypes.h>
#include <glob.h>
#include "commands.h"
#include "platform.h"
static int
open_and_seek(int chip, uint64_t scom, int mode, int *fd)
{
char dev[512];
/* Shift scom address to align to 8 byte boundary, mask high bit */
off64_t offset = (scom & ((1ULL << 63) - 1)) << 3;
/* Handle high bit (indirect SCOM) by shifting the bit right one bit.
File offsets are signed, and this is how the kernel expects us to
mangle it.
Note we set bit 62 instead of bit 59 because of a bug in the kernel
scom.c that shifts the whole value right 3 before looking for
bit 59 set.
*/
if (scom & (1ULL << 63)) {
offset |= 1ULL << 62;
}
snprintf(dev, sizeof(dev), "/sys/kernel/debug/powerpc/scom/%08x/access",
chip);
*fd = open(dev, mode);
if (*fd < 0) {
fprintf(stderr, "open(\"%s\"): %s\n", dev, strerror(errno));
return -1;
}
if (lseek64(*fd, offset, SEEK_SET) == (off_t)-1) {
fprintf(stderr, "lseek(%jd): %s\n", offset, strerror(errno));
close(*fd);
return -1;
}
return 0;
}
static int
rd_scom(int argc, const char *argv[], const struct cmd_info *info)
{
uint64_t scom;
int chip;
uint64_t data = 0;
int fd;
chip = strtol(argv[1], NULL, 0);
scom = strtoull(argv[2], NULL, 0);
if (open_and_seek(chip, scom, O_RDONLY, &fd) < 0) {
return -1;
}
if (read(fd, &data, sizeof(data)) != sizeof(data)) {
fprintf(stderr, "read(): %s\n", strerror(errno));
close(fd);
return -1;
}
close(fd);
printf("0x%016" PRIx64 "\n", data);
return 0;
}
static int
wr_scom(int argc, const char *argv[], const struct cmd_info *info)
{
uint64_t scom;
int chip;
uint64_t data;
int fd;
int ret = 0;
chip = strtol(argv[1], NULL, 0);
scom = strtoull(argv[2], NULL, 0);
data = strtoull(argv[3], NULL, 0);
if (open_and_seek(chip, scom, O_WRONLY, &fd) < 0) {
return -1;
}
if (write(fd, &data, sizeof(data)) != sizeof(data)) {
fprintf(stderr, "write(): %s\n", strerror(errno));
ret = -1;
}
close(fd);
return ret;
}
/* Reads the Processor Identification Register (PIR) for a linux CPU number */
static int
cpu_to_pir(int cpu, uint32_t *pir)
{
char pir_file[512], pir_str[64];
int fd;
snprintf(pir_file, sizeof(pir_file),
"/sys/devices/system/cpu/cpu%d/pir", cpu);
fd = open(pir_file, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "open(\"%s\"): %s\n", pir_file, strerror(errno));
return -1;
}
if (read(fd, pir_str, sizeof(pir_str)) < 0) {
fprintf(stderr, "read(): %s\n", strerror(errno));
close(fd);
return -1;
}
close(fd);
*pir = strtoul(pir_str, NULL, 16);
return 0;
}
/* Converts a PIR to a chip ID using the device tree */
static int
pir_to_chipid(uint32_t pir, uint32_t *chipid)
{
char cpu_glob_str[512];
glob_t globbuf;
int glob_err;
int fd;
/* Zero out pir's thread number to get pir of the core */
pir &= ~0x7;
snprintf(cpu_glob_str, sizeof(cpu_glob_str),
"/proc/device-tree/cpus/*@%x/ibm,chip-id", pir);
glob_err = glob(cpu_glob_str, 0, NULL, &globbuf);
if (glob_err != 0) {
fprintf(stderr, "glob(\"%s\"): %d\n", cpu_glob_str, glob_err);
return -1;
}
fd = open(globbuf.gl_pathv[0], O_RDONLY);
if (fd < 0) {
fprintf(stderr, "open(\"%s\"): %s\n", globbuf.gl_pathv[0],
strerror(errno));
globfree(&globbuf);
return -1;
}
globfree(&globbuf);
if (read(fd, chipid, sizeof(*chipid)) < 0) {
fprintf(stderr, "read(): %s\n", strerror(errno));
close(fd);
return -1;
}
close(fd);
return 0;
}
static int
cpu_to_chipid(int argc, const char *argv[], const struct cmd_info *info)
{
int cpu;
uint32_t chipid, pir;
int ret = 0;
cpu = strtoul(argv[1], NULL, 0);
ret = cpu_to_pir(cpu, &pir);
if (ret < 0) {
return ret;
}
ret = pir_to_chipid(pir, &chipid);
if (ret < 0) {
return ret;
}
printf("0x%08x\n", chipid);
return ret;
}
static int
cpu_to_ex(int argc, const char *argv[], const struct cmd_info *info)
{
int cpu;
uint32_t pir, ex;
int ret = 0;
cpu = strtoul(argv[1], NULL, 0);
ret = cpu_to_pir(cpu, &pir);
if (ret < 0) {
return ret;
}
/* EX number is the 4-bit core ID part of PIR */
ex = (pir >> 3) & 0xf;
printf("%d\n", ex);
return ret;
}
MAKE_PREREQ_PARAMS_FIXED_ARGS(rd_params, 3, "<chipid> <scom>", 0);
MAKE_PREREQ_PARAMS_FIXED_ARGS(wr_params, 4, "<chipid> <scom> <data>", 0);
MAKE_PREREQ_PARAMS_FIXED_ARGS(cpu_params, 2, "<cpu>", 0);
static const struct cmd_info scom_cmds[] = {
MAKE_CMD_WITH_PARAMS(getscom, &rd_scom, NULL, &rd_params),
MAKE_CMD_WITH_PARAMS(putscom, &wr_scom, NULL, &wr_params),
MAKE_CMD_WITH_PARAMS(cputochipid, &cpu_to_chipid, NULL, &cpu_params),
MAKE_CMD_WITH_PARAMS(cputoex, &cpu_to_ex, NULL, &cpu_params),
};
MAKE_CMD_GROUP(SCOM, "commands to access SCOM registers",
scom_cmds);
REGISTER_CMD_GROUP(SCOM);
#endif /* defined(__powerpc64__) || defined(__powerpc__) */