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
|
// SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
/*
* Do XSCOMs through linux debugfs interface
*
* Copyright 2014-2017 IBM Corp.
*/
#define _LARGEFILE64_SOURCE
#include <sys/mman.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <stdint.h>
#include <stdbool.h>
#include <dirent.h>
#include <assert.h>
#include <ctype.h>
#include "xscom.h"
#define XSCOM_BASE_PATH "/sys/kernel/debug/powerpc/scom"
struct xscom_chip {
struct xscom_chip *next;
uint32_t chip_id;
int fd;
};
static struct xscom_chip *xscom_chips;
void xscom_for_each_chip(void (*cb)(uint32_t chip_id))
{
struct xscom_chip *c;
for (c = xscom_chips; c; c = c->next)
cb(c->chip_id);
}
static uint32_t xscom_add_chip(const char *base_path, const char *dname)
{
char nbuf[strlen(base_path) + strlen(dname) + 16];
struct xscom_chip *chip;
int fd;
snprintf(nbuf, sizeof(nbuf), "%s/%s/access", base_path, dname);
fd = open(nbuf, O_RDWR);
if (fd < 0) {
perror("Failed to open SCOM access file");
exit(1);
}
chip = malloc(sizeof(*chip));
assert(chip);
memset(chip, 0, sizeof(*chip));
chip->fd = fd;
chip->chip_id = strtoul(dname, NULL, 16);
chip->next = xscom_chips;
xscom_chips = chip;
return chip->chip_id;
}
static bool xscom_check_dirname(const char *n)
{
while(*n) {
char c = toupper(*(n++));
if ((c < 'A' || c > 'Z') &&
(c < '0' || c > '9'))
return false;
}
return true;
}
static uint32_t xscom_scan_chips(const char *base_path)
{
int i, nfiles;
struct dirent **filelist;
uint32_t lower = 0xffffffff;
nfiles = scandir(base_path, &filelist, NULL, alphasort);
if (nfiles < 0) {
perror("Error accessing sysfs scom directory");
exit(1);
}
if (nfiles == 0) {
fprintf(stderr, "No SCOM dir found in sysfs\n");
exit(1);
}
for (i = 0; i < nfiles; i++) {
struct dirent *d = filelist[i];
uint32_t id;
if (d->d_type != DT_DIR)
continue;
if (!xscom_check_dirname(d->d_name))
continue;
id = xscom_add_chip(base_path, d->d_name);
if (id < lower)
lower = id;
free(d);
}
free(filelist);
return lower;
}
static struct xscom_chip *xscom_find_chip(uint32_t chip_id)
{
struct xscom_chip *c;
for (c = xscom_chips; c; c = c->next)
if (c->chip_id == chip_id)
return c;
return NULL;
}
static uint64_t xscom_mangle_addr(uint64_t addr)
{
uint64_t tmp;
/*
* Shift the top 4 bits (indirect mode) down by 4 bits so we
* don't lose going through the debugfs interfaces.
*/
tmp = (addr & 0xf000000000000000) >> 4;
addr &= 0x00ffffffffffffff;
addr |= tmp;
/* Shift up by 3 for debugfs */
return addr << 3;
}
int xscom_read(uint32_t chip_id, uint64_t addr, uint64_t *val)
{
struct xscom_chip *c = xscom_find_chip(chip_id);
int rc;
if (!c)
return -ENODEV;
addr = xscom_mangle_addr(addr);
lseek64(c->fd, addr, SEEK_SET);
rc = read(c->fd, val, 8);
if (rc < 0)
return -errno;
if (rc != 8)
return -EIO;
return 0;
}
int xscom_write(uint32_t chip_id, uint64_t addr, uint64_t val)
{
struct xscom_chip *c = xscom_find_chip(chip_id);
int rc;
if (!c)
return -ENODEV;
addr = xscom_mangle_addr(addr);
lseek64(c->fd, addr, SEEK_SET);
rc = write(c->fd, &val, 8);
if (rc < 0)
return -errno;
if (rc != 8)
return -EIO;
return 0;
}
int xscom_read_ex(uint32_t ex_target_id, uint64_t addr, uint64_t *val)
{
uint32_t chip_id = ex_target_id >> 4;;
addr |= (ex_target_id & 0xf) << 24;
/* XXX TODO: Special wakeup ? */
return xscom_read(chip_id, addr, val);
}
int xscom_write_ex(uint32_t ex_target_id, uint64_t addr, uint64_t val)
{
uint32_t chip_id = ex_target_id >> 4;;
addr |= (ex_target_id & 0xf) << 24;
/* XXX TODO: Special wakeup ? */
return xscom_write(chip_id, addr, val);
}
bool xscom_readable(uint64_t addr)
{
/* Top nibble 9 indicates form 1 indirect, which is write only */
if (((addr >> 60) & 0xf) == 9)
return false;
return true;
}
uint32_t xscom_init(void)
{
return xscom_scan_chips(XSCOM_BASE_PATH);
}
|