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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
|
#include <stdio.h>
#include <stdint.h>
#include <assert.h>
#include <ccan/list/list.h>
#include <libfdt/libfdt.h>
#include "bitutils.h"
#include "target.h"
#include "device.h"
#include "operations.h"
#undef PR_DEBUG
#define PR_DEBUG(...)
struct list_head empty_list = LIST_HEAD_INIT(empty_list);
struct list_head target_classes = LIST_HEAD_INIT(target_classes);
/* Work out the address to access based on the current target and
* final class name */
static struct pdbg_target *get_class_target_addr(struct pdbg_target *target, const char *name, uint64_t *addr)
{
/* Check class */
while (strcmp(target->class, name)) {
/* Keep walking the tree translating addresses */
*addr += dt_get_address(target, 0, NULL);
target = target->parent;
/* The should always be a parent. If there isn't it
* means we traversed up the whole device tree and
* didn't find a parent matching the given class. */
assert(target);
}
return target;
}
/* The indirect access code was largely stolen from hw/xscom.c in skiboot */
#define PIB_IND_MAX_RETRIES 10
#define PIB_IND_READ PPC_BIT(0)
#define PIB_IND_ADDR PPC_BITMASK(12, 31)
#define PIB_IND_DATA PPC_BITMASK(48, 63)
#define PIB_DATA_IND_COMPLETE PPC_BIT(32)
#define PIB_DATA_IND_ERR PPC_BITMASK(33, 35)
#define PIB_DATA_IND_DATA PPC_BITMASK(48, 63)
static int pib_indirect_read(struct pib *pib, uint64_t addr, uint64_t *data)
{
uint64_t indirect_addr;
int retries;
if ((addr >> 60) & 1) {
PR_ERROR("Indirect form 1 not supported\n");
return -1;
}
indirect_addr = addr & 0x7fffffff;
*data = PIB_IND_READ | (addr & PIB_IND_ADDR);
CHECK_ERR(pib->write(pib, indirect_addr, *data));
/* Wait for completion */
for (retries = 0; retries < PIB_IND_MAX_RETRIES; retries++) {
CHECK_ERR(pib->read(pib, indirect_addr, data));
if ((*data & PIB_DATA_IND_COMPLETE) &&
((*data & PIB_DATA_IND_ERR) == 0)) {
*data = *data & PIB_DATA_IND_DATA;
break;
}
if ((*data & PIB_DATA_IND_COMPLETE) ||
(retries >= PIB_IND_MAX_RETRIES)) {
PR_ERROR("Error reading indirect register");
return -1;
}
}
return 0;
}
static int pib_indirect_write(struct pib *pib, uint64_t addr, uint64_t data)
{
uint64_t indirect_addr;
int retries;
if ((addr >> 60) & 1) {
PR_ERROR("Indirect form 1 not supported\n");
return -1;
}
indirect_addr = addr & 0x7fffffff;
data &= PIB_IND_DATA;
data |= addr & PIB_IND_ADDR;
CHECK_ERR(pib->write(pib, indirect_addr, data));
/* Wait for completion */
for (retries = 0; retries < PIB_IND_MAX_RETRIES; retries++) {
CHECK_ERR(pib->read(pib, indirect_addr, &data));
if ((data & PIB_DATA_IND_COMPLETE) &&
((data & PIB_DATA_IND_ERR) == 0))
break;
if ((data & PIB_DATA_IND_COMPLETE) ||
(retries >= PIB_IND_MAX_RETRIES)) {
PR_ERROR("Error writing indirect register");
return -1;
}
}
return 0;
}
int pib_read(struct pdbg_target *pib_dt, uint64_t addr, uint64_t *data)
{
struct pib *pib;
int rc;
pib_dt = get_class_target_addr(pib_dt, "pib", &addr);
pib = target_to_pib(pib_dt);
if (addr & PPC_BIT(0))
rc = pib_indirect_read(pib, addr, data);
else
rc = pib->read(pib, addr, data);
return rc;
}
int pib_write(struct pdbg_target *pib_dt, uint64_t addr, uint64_t data)
{
struct pib *pib;
int rc;
pib_dt = get_class_target_addr(pib_dt, "pib", &addr);
pib = target_to_pib(pib_dt);
if (addr & PPC_BIT(0))
rc = pib_indirect_write(pib, addr, data);
else
rc = pib->write(pib, addr, data);
return rc;
}
/* Wait for a SCOM register addr to match value & mask == data */
int pib_wait(struct pdbg_target *pib_dt, uint64_t addr, uint64_t mask, uint64_t data)
{
struct pib *pib;
uint64_t tmp;
int rc;
pib_dt = get_class_target_addr(pib_dt, "pib", &addr);
pib = target_to_pib(pib_dt);
do {
if (addr & PPC_BIT(0))
rc = pib_indirect_read(pib, addr, &tmp);
else
rc = pib->read(pib, addr, &tmp);
if (rc)
return rc;
} while ((tmp & mask) != data);
return 0;
}
int opb_read(struct pdbg_target *opb_dt, uint32_t addr, uint32_t *data)
{
struct opb *opb;
uint64_t addr64 = addr;
opb_dt = get_class_target_addr(opb_dt, "opb", &addr64);
opb = target_to_opb(opb_dt);
return opb->read(opb, addr64, data);
}
int opb_write(struct pdbg_target *opb_dt, uint32_t addr, uint32_t data)
{
struct opb *opb;
uint64_t addr64 = addr;
opb_dt = get_class_target_addr(opb_dt, "opb", &addr64);
opb = target_to_opb(opb_dt);
return opb->write(opb, addr64, data);
}
int fsi_read(struct pdbg_target *fsi_dt, uint32_t addr, uint32_t *data)
{
struct fsi *fsi;
uint64_t addr64 = addr;
fsi_dt = get_class_target_addr(fsi_dt, "fsi", &addr64);
fsi = target_to_fsi(fsi_dt);
return fsi->read(fsi, addr64, data);
}
int fsi_write(struct pdbg_target *fsi_dt, uint32_t addr, uint32_t data)
{
struct fsi *fsi;
uint64_t addr64 = addr;
fsi_dt = get_class_target_addr(fsi_dt, "fsi", &addr64);
fsi = target_to_fsi(fsi_dt);
return fsi->write(fsi, addr64, data);
}
struct pdbg_target *require_target_parent(struct pdbg_target *target)
{
assert(target->parent);
return target->parent;
}
/* Finds the given class. Returns NULL if not found. */
struct pdbg_target_class *find_target_class(const char *name)
{
struct pdbg_target_class *target_class;
list_for_each(&target_classes, target_class, class_head_link)
if (!strcmp(target_class->name, name))
return target_class;
return NULL;
}
/* Same as above but dies with an assert if the target class doesn't
* exist */
struct pdbg_target_class *require_target_class(const char *name)
{
struct pdbg_target_class *target_class;
target_class = find_target_class(name);
if (!target_class) {
PR_ERROR("Couldn't find class %s\n", name);
assert(0);
}
return target_class;
}
/* Returns the existing class or allocates space for a new one */
struct pdbg_target_class *get_target_class(const char *name)
{
struct pdbg_target_class *target_class;
if ((target_class = find_target_class(name)))
return target_class;
/* Need to allocate a new class */
PR_DEBUG("Allocating %s target class\n", name);
target_class = calloc(1, sizeof(*target_class));
assert(target_class);
target_class->name = strdup(name);
list_head_init(&target_class->targets);
list_add(&target_classes, &target_class->class_head_link);
return target_class;
}
extern struct hw_unit_info *__start_hw_units;
extern struct hw_init_info *__stop_hw_units;
struct hw_unit_info *find_compatible_target(const char *compat)
{
struct hw_unit_info **p;
struct pdbg_target *target;
for (p = &__start_hw_units; p < (struct hw_unit_info **) &__stop_hw_units; p++) {
target = (*p)->hw_unit;
if (!strcmp(target->compatible, compat))
return *p;
}
return NULL;
}
void pdbg_targets_init(void *fdt)
{
dt_root = dt_new_root("", NULL, 0);
dt_expand(fdt);
}
/* We walk the tree root down disabling targets which might/should
* exist but don't */
enum pdbg_target_status pdbg_target_probe(struct pdbg_target *target)
{
struct pdbg_target *parent;
enum pdbg_target_status status;
assert(target);
status = pdbg_target_status(target);
assert(status != PDBG_TARGET_RELEASED);
assert(status != PDBG_TARGET_PENDING_RELEASE);
if (status == PDBG_TARGET_DISABLED || status == PDBG_TARGET_NONEXISTENT
|| status == PDBG_TARGET_ENABLED)
/* We've already tried probing this target and by assumption
* it's status won't have changed */
return status;
parent = target->parent;
if (parent) {
/* Recurse up the tree to probe and set parent target status */
pdbg_target_probe(parent);
status = pdbg_target_status(parent);
switch(status) {
case PDBG_TARGET_NONEXISTENT:
/* The parent doesn't exist neither does it's
* children */
target->status = PDBG_TARGET_NONEXISTENT;
return PDBG_TARGET_NONEXISTENT;
case PDBG_TARGET_DISABLED:
/* The parent is disabled, we know nothing of the child
* so leave it in it's current state unless it must
* exist.
* TODO: Must exist error reporting */
assert(pdbg_target_status(target) != PDBG_TARGET_MUSTEXIST);
return pdbg_target_status(target);
case PDBG_TARGET_RELEASED:
case PDBG_TARGET_PENDING_RELEASE:
case PDBG_TARGET_MUSTEXIST:
case PDBG_TARGET_UNKNOWN:
/* We must know by now if the parent exists or not */
assert(0);
break;
case PDBG_TARGET_ENABLED:
break;
}
}
/* At this point any parents must exist and have already been probed */
if (target->probe && target->probe(target)) {
/* Could not find the target */
assert(pdbg_target_status(target) != PDBG_TARGET_MUSTEXIST);
target->status = PDBG_TARGET_NONEXISTENT;
return PDBG_TARGET_NONEXISTENT;
}
target->status = PDBG_TARGET_ENABLED;
return PDBG_TARGET_ENABLED;
}
/*
* Walk back up, releasing.
*/
void pdbg_target_release(struct pdbg_target *target)
{
struct pdbg_target *parent;
struct pdbg_target *child;
assert(target);
/* If it's not enabled, the parent wasn't enabled. */
if ((pdbg_target_status(target) != PDBG_TARGET_ENABLED) &&
(pdbg_target_status(target) != PDBG_TARGET_PENDING_RELEASE))
return;
target->status = PDBG_TARGET_PENDING_RELEASE;
pdbg_for_each_child_target(target, child) {
/* Not all children released yet, stop here. */
if (pdbg_target_status(child) == PDBG_TARGET_ENABLED)
return;
if (pdbg_target_status(child) == PDBG_TARGET_PENDING_RELEASE)
return;
}
/* At this point any parents must exist and have already been probed */
if (target->release)
target->release(target);
target->status = PDBG_TARGET_RELEASED;
parent = target->parent;
if (parent) {
/* Recurse up the tree to release parents */
pdbg_target_release(parent);
}
}
bool pdbg_target_is_class(struct pdbg_target *target, const char *class)
{
if (!target || !target->class || !class)
return false;
return strcmp(target->class, class) == 0;
}
void *pdbg_target_priv(struct pdbg_target *target)
{
return target->priv;
}
void pdbg_target_priv_set(struct pdbg_target *target, void *priv)
{
target->priv = priv;
}
|