File: target.c

package info (click to toggle)
pdbg 3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,212 kB
  • sloc: ansic: 21,934; cpp: 3,363; sh: 3,343; makefile: 314; asm: 11
file content (640 lines) | stat: -rw-r--r-- 15,427 bytes parent folder | download
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
#include <assert.h>
#include <ccan/list/list.h>
#include <libfdt.h>

#include "bitutils.h"
#include "hwunit.h"
#include "operations.h"
#include "debug.h"

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)
{
	uint64_t old_addr = *addr;

	/* Check class */
	while (strcmp(target->class, name)) {
		if (target->translate) {
			*addr = target->translate(target, *addr);
			target = target_parent(name, target, false);
			assert(target);
			break;
		} else {
			*addr += pdbg_target_address(target, NULL);
		}

		/* Keep walking the tree translating addresses */
		target = get_parent(target, false);

		/* The root node doesn't have an address space so it's
		 * an error in the device tree if we hit this. */
		assert(target != pdbg_target_root());
	}

	pdbg_log(PDBG_DEBUG, "Translating target addr 0x%" PRIx64 " -> 0x%" PRIx64 " on %s\n",
		 old_addr, *addr, pdbg_target_path(target));
	return target;
}

struct pdbg_target *pdbg_address_absolute(struct pdbg_target *target, uint64_t *addr)
{
	return get_class_target_addr(target, "pib", addr);
}

/* 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;
	uint64_t target_addr = addr;
	int rc;

	pib_dt = get_class_target_addr(pib_dt, "pib", &target_addr);

	if (pdbg_target_status(pib_dt) != PDBG_TARGET_ENABLED)
		return -1;

	pib = target_to_pib(pib_dt);

	if (!pib->read) {
		PR_ERROR("read() not implemented for the target\n");
		return -1;
	}

	if (target_addr & PPC_BIT(0))
		rc = pib_indirect_read(pib, target_addr, data);
	else
		rc = pib->read(pib, target_addr, data);

	PR_DEBUG("rc = %d, addr = 0x%016" PRIx64 ", data = 0x%016" PRIx64 ", target = %s\n",
		 rc, target_addr, *data, pdbg_target_path(&pib->target));

	return rc;
}

int pib_write(struct pdbg_target *pib_dt, uint64_t addr, uint64_t data)
{
	struct pib *pib;
	uint64_t target_addr = addr;
	int rc;

	pib_dt = get_class_target_addr(pib_dt, "pib", &target_addr);

	if (pdbg_target_status(pib_dt) != PDBG_TARGET_ENABLED)
		return -1;

	pib = target_to_pib(pib_dt);

	if (!pib->write) {
		PR_ERROR("write() not implemented for the target\n");
		return -1;
	}

	PR_DEBUG("addr:0x%08" PRIx64 " data:0x%016" PRIx64 "\n",
		 target_addr, data);
	if (target_addr & PPC_BIT(0))
		rc = pib_indirect_write(pib, target_addr, data);
	else
		rc = pib->write(pib, target_addr, data);

	PR_DEBUG("rc = %d, addr = 0x%016" PRIx64 ", data = 0x%016" PRIx64 ", target = %s\n",
		 rc, target_addr, data, pdbg_target_path(&pib->target));

	return rc;
}

int pib_write_mask(struct pdbg_target *pib_dt, uint64_t addr, uint64_t data, uint64_t mask)
{
	uint64_t value;
	int rc;

	rc = pib_read(pib_dt, addr, &value);
	if (rc)
		return rc;

	value = (value & ~mask) | (data & mask);

	return pib_write(pib_dt, addr, value);
}

/* 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);

	if (pdbg_target_status(pib_dt) != PDBG_TARGET_ENABLED)
		return -1;

	pib = target_to_pib(pib_dt);

	if (!pib->read) {
		PR_ERROR("read() not implemented for the target\n");
		return -1;
	}

	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);

	if (pdbg_target_status(opb_dt) != PDBG_TARGET_ENABLED)
		return -1;

	opb = target_to_opb(opb_dt);

	if (!opb->read) {
		PR_ERROR("read() not implemented for the target\n");
		return -1;
	}

	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);

	if (pdbg_target_status(opb_dt) != PDBG_TARGET_ENABLED)
		return -1;

	opb = target_to_opb(opb_dt);

	if (!opb->write) {
		PR_ERROR("write() not implemented for the target\n");
		return -1;
	}
	return opb->write(opb, addr64, data);
}

int fsi_read(struct pdbg_target *fsi_dt, uint32_t addr, uint32_t *data)
{
	struct fsi *fsi;
	int rc;
	uint64_t addr64 = addr;

	fsi_dt = get_class_target_addr(fsi_dt, "fsi", &addr64);
	fsi = target_to_fsi(fsi_dt);

	if (!fsi->read) {
		PR_ERROR("read() not implemented for the target\n");
		return -1;
	}

	rc = fsi->read(fsi, addr64, data);
	PR_DEBUG("rc = %d, addr = 0x%05" PRIx64 ", data = 0x%08" PRIx32 ", target = %s\n",
		 rc, addr64, *data, pdbg_target_path(&fsi->target));
	return rc;
}

int fsi_write(struct pdbg_target *fsi_dt, uint32_t addr, uint32_t data)
{
	struct fsi *fsi;
	int rc;
	uint64_t addr64 = addr;

	fsi_dt = get_class_target_addr(fsi_dt, "fsi", &addr64);
	fsi = target_to_fsi(fsi_dt);

	if (!fsi->write) {
		PR_ERROR("write() not implemented for the target\n");
		return -1;
	}

	rc = fsi->write(fsi, addr64, data);
	PR_DEBUG("rc = %d, addr = 0x%05" PRIx64 ", data = 0x%08" PRIx32 ", target = %s\n",
		 rc, addr64, data, pdbg_target_path(&fsi->target));
	return rc;
}

int fsi_write_mask(struct pdbg_target *fsi_dt, uint32_t addr, uint32_t data, uint32_t mask)
{
	uint32_t value;
	int rc;

	rc = fsi_read(fsi_dt, addr, &value);
	if (rc)
		return rc;

	value = (value & ~mask) | (data & mask);

	return fsi_write(fsi_dt, addr, value);
}

int i2c_read(struct pdbg_target *i2c_dt, uint8_t addr, uint8_t reg, uint16_t size, uint8_t *data)
{
	struct i2cbus *i2cbus;
	uint64_t target_addr = addr;

	i2c_dt = get_class_target_addr(i2c_dt, "i2c_bus", &target_addr);

	if (pdbg_target_status(i2c_dt) != PDBG_TARGET_ENABLED)
		return -1;

	i2cbus = target_to_i2cbus(i2c_dt);

	if (target_addr > 0xFF) {
		PR_ERROR("i2c_read got invalid address 0x%02x --> 0x%" PRIx64 "\n", addr, target_addr);
		return -1;
	}

	addr = target_addr & 0xff;
	return i2cbus->read(i2cbus, addr, reg, size, data);
}

int i2c_write(struct pdbg_target *i2c_dt, uint8_t addr, uint8_t reg, uint16_t size, uint8_t *data)
{
	struct i2cbus *i2cbus;
	uint64_t target_addr = addr;

	i2c_dt = get_class_target_addr(i2c_dt, "i2c_bus", &target_addr);

	if (pdbg_target_status(i2c_dt) != PDBG_TARGET_ENABLED)
		return -1;

	i2cbus = target_to_i2cbus(i2c_dt);

	if (target_addr > 0xFF) {
		PR_ERROR("i2c_write got invalid address 0x%02x --> 0x%" PRIx64 "\n", addr, target_addr);
		return -1;
	}

	addr = target_addr & 0xff;
	return i2cbus->write(i2cbus, addr, reg, size, data);
}

int mem_read(struct pdbg_target *target, uint64_t addr, uint8_t *output, uint64_t size, uint8_t block_size, bool ci)
{
	struct mem *mem;
	int rc = -1;

	assert(pdbg_target_is_class(target, "mem"));

	if (pdbg_target_status(target) != PDBG_TARGET_ENABLED)
		return -1;

	mem = target_to_mem(target);

	if (!mem->read) {
		PR_ERROR("read() not implemented for the target\n");
		return -1;
	}

	rc = mem->read(mem, addr, output, size, block_size, ci);

	return rc;
}

int mem_write(struct pdbg_target *target, uint64_t addr, uint8_t *input, uint64_t size, uint8_t block_size, bool ci)
{
	struct mem *mem;
	int rc = -1;

	assert(pdbg_target_is_class(target, "mem"));

	if (pdbg_target_status(target) != PDBG_TARGET_ENABLED)
		return -1;

	mem = target_to_mem(target);

	if (!mem->write) {
		PR_ERROR("write() not implemented for the target\n");
		return -1;
	}

	rc = mem->write(mem, addr, input, size, block_size, ci);

	return rc;
}

int ocmb_getscom(struct pdbg_target *target, uint64_t addr, uint64_t *val)
{
	struct ocmb *ocmb;

	assert(pdbg_target_is_class(target, "ocmb"));

	if (pdbg_target_status(target) != PDBG_TARGET_ENABLED)
		return -1;

	ocmb = target_to_ocmb(target);

	if (!ocmb->getscom) {
		PR_ERROR("getscom() not implemented for the target\n");
		return -1;
	}

	return ocmb->getscom(ocmb, addr, val);
}

int ocmb_putscom(struct pdbg_target *target, uint64_t addr, uint64_t val)
{
	struct ocmb *ocmb;

	assert(pdbg_target_is_class(target, "ocmb"));

	if (pdbg_target_status(target) != PDBG_TARGET_ENABLED)
		return -1;

	ocmb = target_to_ocmb(target);

	if (!ocmb->putscom) {
		PR_ERROR("putscom() not implemented for the target\n");
		return -1;
	}

	return ocmb->putscom(ocmb, addr, val);
}

/* 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 = NULL;

	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(struct pdbg_target *target)
{
	struct pdbg_target_class *target_class;

	if ((target_class = find_target_class(target->class)))
		return target_class;

	/* Need to allocate a new class */
	PR_DEBUG("Allocating %s target class\n", target->class);
	target_class = calloc(1, sizeof(*target_class));
	assert(target_class);
	target_class->name = strdup(target->class);
	list_head_init(&target_class->targets);
	list_add_tail(&target_classes, &target_class->class_head_link);
	return target_class;
}

/* 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, *vnode;
	enum pdbg_target_status status;

	assert(target);

	status = pdbg_target_status(target);
	assert(status != PDBG_TARGET_RELEASED);

	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 = get_parent(target, false);
	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_MUSTEXIST:
		case PDBG_TARGET_UNKNOWN:
			/* We must know by now if the parent exists or not */
			assert(0);
			break;

		case PDBG_TARGET_ENABLED:
			break;
		}
	}

	/* Make sure any virtual nodes are also probed */
	vnode = target_to_virtual(target, true);
	if (vnode)
		pdbg_target_probe(vnode);

	/* 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;
}

/* Releases a target by first recursively releasing all its children */
void pdbg_target_release(struct pdbg_target *target)
{
	struct pdbg_target *child;

	if (pdbg_target_status(target) != PDBG_TARGET_ENABLED)
		return;

	pdbg_for_each_child_target(target, child)
		pdbg_target_release(child);

	/* Release the target */
	if (target->release)
		target->release(target);
	target->status = PDBG_TARGET_RELEASED;
}

/*
 * Probe all targets in the device tree.
 */
void pdbg_target_probe_all(struct pdbg_target *parent)
{
	struct pdbg_target *child;

	if (!parent)
		parent = pdbg_target_root();

	pdbg_for_each_child_target(parent, child) {
		pdbg_target_probe_all(child);
		pdbg_target_probe(child);
	}
}

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;
}

/* For virtual nodes, compatible property is not set */
bool target_is_virtual(struct pdbg_target *target)
{
	return (!target->compatible);
}

/* Map virtual target to real target */
struct pdbg_target *target_to_real(struct pdbg_target *target, bool strict)
{
	if (!target->compatible && target->vnode)
		return target->vnode;

	if (strict)
		return NULL;

	return target;
}

/* Map real target to virtual target */
struct pdbg_target *target_to_virtual(struct pdbg_target *target, bool strict)
{
	if (target->compatible && target->vnode)
		return target->vnode;

	if (strict)
		return NULL;

	return target;
}