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
|
/* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
*
* Copyright 2010-2016 Freescale Semiconductor Inc.
* Copyright 2017 NXP
*
*/
#include <of.h>
#include <rte_dpaa_logs.h>
static int alive;
static struct dt_dir root_dir;
static const char *base_dir;
static COMPAT_LIST_HEAD(linear);
static int
of_open_dir(const char *relative_path, struct dirent ***d)
{
int ret;
char full_path[PATH_MAX];
snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
ret = scandir(full_path, d, 0, versionsort);
if (ret < 0)
DPAA_BUS_LOG(ERR, "Failed to open directory %s",
full_path);
return ret;
}
static void
of_close_dir(struct dirent **d, int num)
{
while (num--)
free(d[num]);
free(d);
}
static int
of_open_file(const char *relative_path)
{
int ret;
char full_path[PATH_MAX];
snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
ret = open(full_path, O_RDONLY);
if (ret < 0)
DPAA_BUS_LOG(ERR, "Failed to open directory %s",
full_path);
return ret;
}
static void
process_file(struct dirent *dent, struct dt_dir *parent)
{
int fd;
struct dt_file *f = malloc(sizeof(*f));
if (!f) {
DPAA_BUS_LOG(DEBUG, "Unable to allocate memory for file node");
return;
}
f->node.is_file = 1;
snprintf(f->node.node.name, NAME_MAX, "%s", dent->d_name);
snprintf(f->node.node.full_name, PATH_MAX, "%s/%s",
parent->node.node.full_name, dent->d_name);
f->parent = parent;
fd = of_open_file(f->node.node.full_name);
if (fd < 0) {
DPAA_BUS_LOG(DEBUG, "Unable to open file node");
free(f);
return;
}
f->len = read(fd, f->buf, OF_FILE_BUF_MAX);
close(fd);
if (f->len < 0) {
DPAA_BUS_LOG(DEBUG, "Unable to read file node");
free(f);
return;
}
list_add_tail(&f->node.list, &parent->files);
}
static const struct dt_dir *
node2dir(const struct device_node *n)
{
struct dt_node *dn = container_of((struct device_node *)n,
struct dt_node, node);
const struct dt_dir *d = container_of(dn, struct dt_dir, node);
assert(!dn->is_file);
return d;
}
/* process_dir() calls iterate_dir(), but the latter will also call the former
* when recursing into sub-directories, so a predeclaration is needed.
*/
static int process_dir(const char *relative_path, struct dt_dir *dt);
static int
iterate_dir(struct dirent **d, int num, struct dt_dir *dt)
{
int loop;
/* Iterate the directory contents */
for (loop = 0; loop < num; loop++) {
struct dt_dir *subdir;
int ret;
/* Ignore dot files of all types (especially "..") */
if (d[loop]->d_name[0] == '.')
continue;
switch (d[loop]->d_type) {
case DT_REG:
process_file(d[loop], dt);
break;
case DT_DIR:
subdir = malloc(sizeof(*subdir));
if (!subdir) {
perror("malloc");
return -ENOMEM;
}
snprintf(subdir->node.node.name, NAME_MAX, "%s",
d[loop]->d_name);
snprintf(subdir->node.node.full_name, PATH_MAX,
"%s/%s", dt->node.node.full_name,
d[loop]->d_name);
subdir->parent = dt;
ret = process_dir(subdir->node.node.full_name, subdir);
if (ret)
return ret;
list_add_tail(&subdir->node.list, &dt->subdirs);
break;
default:
DPAA_BUS_LOG(DEBUG, "Ignoring invalid dt entry %s/%s",
dt->node.node.full_name, d[loop]->d_name);
}
}
return 0;
}
static int
process_dir(const char *relative_path, struct dt_dir *dt)
{
struct dirent **d;
int ret, num;
dt->node.is_file = 0;
INIT_LIST_HEAD(&dt->subdirs);
INIT_LIST_HEAD(&dt->files);
ret = of_open_dir(relative_path, &d);
if (ret < 0)
return ret;
num = ret;
ret = iterate_dir(d, num, dt);
of_close_dir(d, num);
return (ret < 0) ? ret : 0;
}
static void
linear_dir(struct dt_dir *d)
{
struct dt_file *f;
struct dt_dir *dd;
d->compatible = NULL;
d->status = NULL;
d->lphandle = NULL;
d->a_cells = NULL;
d->s_cells = NULL;
d->reg = NULL;
list_for_each_entry(f, &d->files, node.list) {
if (!strcmp(f->node.node.name, "compatible")) {
if (d->compatible)
DPAA_BUS_LOG(DEBUG, "Duplicate compatible in"
" %s", d->node.node.full_name);
d->compatible = f;
} else if (!strcmp(f->node.node.name, "status")) {
if (d->status)
DPAA_BUS_LOG(DEBUG, "Duplicate status in %s",
d->node.node.full_name);
d->status = f;
} else if (!strcmp(f->node.node.name, "linux,phandle")) {
if (d->lphandle)
DPAA_BUS_LOG(DEBUG, "Duplicate lphandle in %s",
d->node.node.full_name);
d->lphandle = f;
} else if (!strcmp(f->node.node.name, "#address-cells")) {
if (d->a_cells)
DPAA_BUS_LOG(DEBUG, "Duplicate a_cells in %s",
d->node.node.full_name);
d->a_cells = f;
} else if (!strcmp(f->node.node.name, "#size-cells")) {
if (d->s_cells)
DPAA_BUS_LOG(DEBUG, "Duplicate s_cells in %s",
d->node.node.full_name);
d->s_cells = f;
} else if (!strcmp(f->node.node.name, "reg")) {
if (d->reg)
DPAA_BUS_LOG(DEBUG, "Duplicate reg in %s",
d->node.node.full_name);
d->reg = f;
}
}
list_for_each_entry(dd, &d->subdirs, node.list) {
list_add_tail(&dd->linear, &linear);
linear_dir(dd);
}
}
int
of_init_path(const char *dt_path)
{
int ret;
base_dir = dt_path;
/* This needs to be singleton initialization */
DPAA_BUS_HWWARN(alive, "Double-init of device-tree driver!");
/* Prepare root node (the remaining fields are set in process_dir()) */
root_dir.node.node.name[0] = '\0';
root_dir.node.node.full_name[0] = '\0';
INIT_LIST_HEAD(&root_dir.node.list);
root_dir.parent = NULL;
/* Kick things off... */
ret = process_dir("", &root_dir);
if (ret) {
DPAA_BUS_LOG(ERR, "Unable to parse device tree");
return ret;
}
/* Now make a flat, linear list of directories */
linear_dir(&root_dir);
alive = 1;
return 0;
}
static void
destroy_dir(struct dt_dir *d)
{
struct dt_file *f, *tmpf;
struct dt_dir *dd, *tmpd;
list_for_each_entry_safe(f, tmpf, &d->files, node.list) {
list_del(&f->node.list);
free(f);
}
list_for_each_entry_safe(dd, tmpd, &d->subdirs, node.list) {
destroy_dir(dd);
list_del(&dd->node.list);
free(dd);
}
}
void
of_finish(void)
{
DPAA_BUS_HWWARN(!alive, "Double-finish of device-tree driver!");
destroy_dir(&root_dir);
INIT_LIST_HEAD(&linear);
alive = 0;
}
static const struct dt_dir *
next_linear(const struct dt_dir *f)
{
if (f->linear.next == &linear)
return NULL;
return list_entry(f->linear.next, struct dt_dir, linear);
}
static int
check_compatible(const struct dt_file *f, const char *compatible)
{
const char *c = (char *)f->buf;
unsigned int len, remains = f->len;
while (remains) {
len = strlen(c);
if (!strcmp(c, compatible))
return 1;
if (remains < len + 1)
break;
c += (len + 1);
remains -= (len + 1);
}
return 0;
}
const struct device_node *
of_find_compatible_node(const struct device_node *from,
const char *type __always_unused,
const char *compatible)
{
const struct dt_dir *d;
DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
if (list_empty(&linear))
return NULL;
if (!from)
d = list_entry(linear.next, struct dt_dir, linear);
else
d = node2dir(from);
for (d = next_linear(d); d && (!d->compatible ||
!check_compatible(d->compatible,
compatible));
d = next_linear(d))
;
if (d)
return &d->node.node;
return NULL;
}
const void *
of_get_property(const struct device_node *from, const char *name,
size_t *lenp)
{
const struct dt_dir *d;
const struct dt_file *f;
DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
d = node2dir(from);
list_for_each_entry(f, &d->files, node.list)
if (!strcmp(f->node.node.name, name)) {
if (lenp)
*lenp = f->len;
return f->buf;
}
return NULL;
}
bool
of_device_is_available(const struct device_node *dev_node)
{
const struct dt_dir *d;
DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
d = node2dir(dev_node);
if (!d->status)
return true;
if (!strcmp((char *)d->status->buf, "okay"))
return true;
if (!strcmp((char *)d->status->buf, "ok"))
return true;
return false;
}
const struct device_node *
of_find_node_by_phandle(phandle ph)
{
const struct dt_dir *d;
DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
list_for_each_entry(d, &linear, linear)
if (d->lphandle && (d->lphandle->len == 4) &&
!memcmp(d->lphandle->buf, &ph, 4))
return &d->node.node;
return NULL;
}
const struct device_node *
of_get_parent(const struct device_node *dev_node)
{
const struct dt_dir *d;
DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
if (!dev_node)
return NULL;
d = node2dir(dev_node);
if (!d->parent)
return NULL;
return &d->parent->node.node;
}
const struct device_node *
of_get_next_child(const struct device_node *dev_node,
const struct device_node *prev)
{
const struct dt_dir *p, *c;
DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
if (!dev_node)
return NULL;
p = node2dir(dev_node);
if (prev) {
c = node2dir(prev);
DPAA_BUS_HWWARN((c->parent != p), "Parent/child mismatch");
if (c->parent != p)
return NULL;
if (c->node.list.next == &p->subdirs)
/* prev was the last child */
return NULL;
c = list_entry(c->node.list.next, struct dt_dir, node.list);
return &c->node.node;
}
/* Return first child */
if (list_empty(&p->subdirs))
return NULL;
c = list_entry(p->subdirs.next, struct dt_dir, node.list);
return &c->node.node;
}
uint32_t
of_n_addr_cells(const struct device_node *dev_node)
{
const struct dt_dir *d;
DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised");
if (!dev_node)
return OF_DEFAULT_NA;
d = node2dir(dev_node);
while ((d = d->parent))
if (d->a_cells) {
unsigned char *buf =
(unsigned char *)&d->a_cells->buf[0];
assert(d->a_cells->len == 4);
return ((uint32_t)buf[0] << 24) |
((uint32_t)buf[1] << 16) |
((uint32_t)buf[2] << 8) |
(uint32_t)buf[3];
}
return OF_DEFAULT_NA;
}
uint32_t
of_n_size_cells(const struct device_node *dev_node)
{
const struct dt_dir *d;
DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
if (!dev_node)
return OF_DEFAULT_NA;
d = node2dir(dev_node);
while ((d = d->parent))
if (d->s_cells) {
unsigned char *buf =
(unsigned char *)&d->s_cells->buf[0];
assert(d->s_cells->len == 4);
return ((uint32_t)buf[0] << 24) |
((uint32_t)buf[1] << 16) |
((uint32_t)buf[2] << 8) |
(uint32_t)buf[3];
}
return OF_DEFAULT_NS;
}
const uint32_t *
of_get_address(const struct device_node *dev_node, size_t idx,
uint64_t *size, uint32_t *flags __rte_unused)
{
const struct dt_dir *d;
const unsigned char *buf;
uint32_t na = of_n_addr_cells(dev_node);
uint32_t ns = of_n_size_cells(dev_node);
if (!dev_node)
d = &root_dir;
else
d = node2dir(dev_node);
if (!d->reg)
return NULL;
assert(d->reg->len % ((na + ns) * 4) == 0);
assert(d->reg->len / ((na + ns) * 4) > (unsigned int) idx);
buf = (const unsigned char *)&d->reg->buf[0];
buf += (na + ns) * idx * 4;
if (size)
for (*size = 0; ns > 0; ns--, na++)
*size = (*size << 32) +
(((uint32_t)buf[4 * na] << 24) |
((uint32_t)buf[4 * na + 1] << 16) |
((uint32_t)buf[4 * na + 2] << 8) |
(uint32_t)buf[4 * na + 3]);
return (const uint32_t *)buf;
}
uint64_t
of_translate_address(const struct device_node *dev_node,
const uint32_t *addr)
{
uint64_t phys_addr, tmp_addr;
const struct device_node *parent;
const uint32_t *ranges;
size_t rlen;
uint32_t na, pna;
DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
assert(dev_node != NULL);
na = of_n_addr_cells(dev_node);
phys_addr = of_read_number(addr, na);
dev_node = of_get_parent(dev_node);
if (!dev_node)
return 0;
else if (node2dir(dev_node) == &root_dir)
return phys_addr;
do {
pna = of_n_addr_cells(dev_node);
parent = of_get_parent(dev_node);
if (!parent)
return 0;
ranges = of_get_property(dev_node, "ranges", &rlen);
/* "ranges" property is missing. Translation breaks */
if (!ranges)
return 0;
/* "ranges" property is empty. Do 1:1 translation */
else if (rlen == 0)
continue;
else
tmp_addr = of_read_number(ranges + na, pna);
na = pna;
dev_node = parent;
phys_addr += tmp_addr;
} while (node2dir(parent) != &root_dir);
return phys_addr;
}
bool
of_device_is_compatible(const struct device_node *dev_node,
const char *compatible)
{
const struct dt_dir *d;
DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
if (!dev_node)
d = &root_dir;
else
d = node2dir(dev_node);
if (d->compatible && check_compatible(d->compatible, compatible))
return true;
return false;
}
|