New upstream version 18.02
[deb_dpdk.git] / drivers / bus / dpaa / base / fman / of.c
1 /* SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0)
2  *
3  * Copyright 2010-2016 Freescale Semiconductor Inc.
4  * Copyright 2017 NXP
5  *
6  */
7
8 #include <of.h>
9 #include <rte_dpaa_logs.h>
10
11 static int alive;
12 static struct dt_dir root_dir;
13 static const char *base_dir;
14 static COMPAT_LIST_HEAD(linear);
15
16 static int
17 of_open_dir(const char *relative_path, struct dirent ***d)
18 {
19         int ret;
20         char full_path[PATH_MAX];
21
22         snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
23         ret = scandir(full_path, d, 0, versionsort);
24         if (ret < 0)
25                 DPAA_BUS_LOG(ERR, "Failed to open directory %s",
26                              full_path);
27         return ret;
28 }
29
30 static void
31 of_close_dir(struct dirent **d, int num)
32 {
33         while (num--)
34                 free(d[num]);
35         free(d);
36 }
37
38 static int
39 of_open_file(const char *relative_path)
40 {
41         int ret;
42         char full_path[PATH_MAX];
43
44         snprintf(full_path, PATH_MAX, "%s/%s", base_dir, relative_path);
45         ret = open(full_path, O_RDONLY);
46         if (ret < 0)
47                 DPAA_BUS_LOG(ERR, "Failed to open directory %s",
48                              full_path);
49         return ret;
50 }
51
52 static void
53 process_file(struct dirent *dent, struct dt_dir *parent)
54 {
55         int fd;
56         struct dt_file *f = malloc(sizeof(*f));
57
58         if (!f) {
59                 DPAA_BUS_LOG(DEBUG, "Unable to allocate memory for file node");
60                 return;
61         }
62         f->node.is_file = 1;
63         snprintf(f->node.node.name, NAME_MAX, "%s", dent->d_name);
64         snprintf(f->node.node.full_name, PATH_MAX, "%s/%s",
65                  parent->node.node.full_name, dent->d_name);
66         f->parent = parent;
67         fd = of_open_file(f->node.node.full_name);
68         if (fd < 0) {
69                 DPAA_BUS_LOG(DEBUG, "Unable to open file node");
70                 free(f);
71                 return;
72         }
73         f->len = read(fd, f->buf, OF_FILE_BUF_MAX);
74         close(fd);
75         if (f->len < 0) {
76                 DPAA_BUS_LOG(DEBUG, "Unable to read file node");
77                 free(f);
78                 return;
79         }
80         list_add_tail(&f->node.list, &parent->files);
81 }
82
83 static const struct dt_dir *
84 node2dir(const struct device_node *n)
85 {
86         struct dt_node *dn = container_of((struct device_node *)n,
87                                           struct dt_node, node);
88         const struct dt_dir *d = container_of(dn, struct dt_dir, node);
89
90         assert(!dn->is_file);
91         return d;
92 }
93
94 /* process_dir() calls iterate_dir(), but the latter will also call the former
95  * when recursing into sub-directories, so a predeclaration is needed.
96  */
97 static int process_dir(const char *relative_path, struct dt_dir *dt);
98
99 static int
100 iterate_dir(struct dirent **d, int num, struct dt_dir *dt)
101 {
102         int loop;
103         /* Iterate the directory contents */
104         for (loop = 0; loop < num; loop++) {
105                 struct dt_dir *subdir;
106                 int ret;
107                 /* Ignore dot files of all types (especially "..") */
108                 if (d[loop]->d_name[0] == '.')
109                         continue;
110                 switch (d[loop]->d_type) {
111                 case DT_REG:
112                         process_file(d[loop], dt);
113                         break;
114                 case DT_DIR:
115                         subdir = malloc(sizeof(*subdir));
116                         if (!subdir) {
117                                 perror("malloc");
118                                 return -ENOMEM;
119                         }
120                         snprintf(subdir->node.node.name, NAME_MAX, "%s",
121                                  d[loop]->d_name);
122                         snprintf(subdir->node.node.full_name, PATH_MAX,
123                                  "%s/%s", dt->node.node.full_name,
124                                  d[loop]->d_name);
125                         subdir->parent = dt;
126                         ret = process_dir(subdir->node.node.full_name, subdir);
127                         if (ret)
128                                 return ret;
129                         list_add_tail(&subdir->node.list, &dt->subdirs);
130                         break;
131                 default:
132                         DPAA_BUS_LOG(DEBUG, "Ignoring invalid dt entry %s/%s",
133                                      dt->node.node.full_name, d[loop]->d_name);
134                 }
135         }
136         return 0;
137 }
138
139 static int
140 process_dir(const char *relative_path, struct dt_dir *dt)
141 {
142         struct dirent **d;
143         int ret, num;
144
145         dt->node.is_file = 0;
146         INIT_LIST_HEAD(&dt->subdirs);
147         INIT_LIST_HEAD(&dt->files);
148         ret = of_open_dir(relative_path, &d);
149         if (ret < 0)
150                 return ret;
151         num = ret;
152         ret = iterate_dir(d, num, dt);
153         of_close_dir(d, num);
154         return (ret < 0) ? ret : 0;
155 }
156
157 static void
158 linear_dir(struct dt_dir *d)
159 {
160         struct dt_file *f;
161         struct dt_dir *dd;
162
163         d->compatible = NULL;
164         d->status = NULL;
165         d->lphandle = NULL;
166         d->a_cells = NULL;
167         d->s_cells = NULL;
168         d->reg = NULL;
169         list_for_each_entry(f, &d->files, node.list) {
170                 if (!strcmp(f->node.node.name, "compatible")) {
171                         if (d->compatible)
172                                 DPAA_BUS_LOG(DEBUG, "Duplicate compatible in"
173                                              " %s", d->node.node.full_name);
174                         d->compatible = f;
175                 } else if (!strcmp(f->node.node.name, "status")) {
176                         if (d->status)
177                                 DPAA_BUS_LOG(DEBUG, "Duplicate status in %s",
178                                              d->node.node.full_name);
179                         d->status = f;
180                 } else if (!strcmp(f->node.node.name, "linux,phandle")) {
181                         if (d->lphandle)
182                                 DPAA_BUS_LOG(DEBUG, "Duplicate lphandle in %s",
183                                              d->node.node.full_name);
184                         d->lphandle = f;
185                 } else if (!strcmp(f->node.node.name, "#address-cells")) {
186                         if (d->a_cells)
187                                 DPAA_BUS_LOG(DEBUG, "Duplicate a_cells in %s",
188                                              d->node.node.full_name);
189                         d->a_cells = f;
190                 } else if (!strcmp(f->node.node.name, "#size-cells")) {
191                         if (d->s_cells)
192                                 DPAA_BUS_LOG(DEBUG, "Duplicate s_cells in %s",
193                                              d->node.node.full_name);
194                         d->s_cells = f;
195                 } else if (!strcmp(f->node.node.name, "reg")) {
196                         if (d->reg)
197                                 DPAA_BUS_LOG(DEBUG, "Duplicate reg in %s",
198                                              d->node.node.full_name);
199                         d->reg = f;
200                 }
201         }
202
203         list_for_each_entry(dd, &d->subdirs, node.list) {
204                 list_add_tail(&dd->linear, &linear);
205                 linear_dir(dd);
206         }
207 }
208
209 int
210 of_init_path(const char *dt_path)
211 {
212         int ret;
213
214         base_dir = dt_path;
215
216         /* This needs to be singleton initialization */
217         DPAA_BUS_HWWARN(alive, "Double-init of device-tree driver!");
218
219         /* Prepare root node (the remaining fields are set in process_dir()) */
220         root_dir.node.node.name[0] = '\0';
221         root_dir.node.node.full_name[0] = '\0';
222         INIT_LIST_HEAD(&root_dir.node.list);
223         root_dir.parent = NULL;
224
225         /* Kick things off... */
226         ret = process_dir("", &root_dir);
227         if (ret) {
228                 DPAA_BUS_LOG(ERR, "Unable to parse device tree");
229                 return ret;
230         }
231
232         /* Now make a flat, linear list of directories */
233         linear_dir(&root_dir);
234         alive = 1;
235         return 0;
236 }
237
238 static void
239 destroy_dir(struct dt_dir *d)
240 {
241         struct dt_file *f, *tmpf;
242         struct dt_dir *dd, *tmpd;
243
244         list_for_each_entry_safe(f, tmpf, &d->files, node.list) {
245                 list_del(&f->node.list);
246                 free(f);
247         }
248         list_for_each_entry_safe(dd, tmpd, &d->subdirs, node.list) {
249                 destroy_dir(dd);
250                 list_del(&dd->node.list);
251                 free(dd);
252         }
253 }
254
255 void
256 of_finish(void)
257 {
258         DPAA_BUS_HWWARN(!alive, "Double-finish of device-tree driver!");
259
260         destroy_dir(&root_dir);
261         INIT_LIST_HEAD(&linear);
262         alive = 0;
263 }
264
265 static const struct dt_dir *
266 next_linear(const struct dt_dir *f)
267 {
268         if (f->linear.next == &linear)
269                 return NULL;
270         return list_entry(f->linear.next, struct dt_dir, linear);
271 }
272
273 static int
274 check_compatible(const struct dt_file *f, const char *compatible)
275 {
276         const char *c = (char *)f->buf;
277         unsigned int len, remains = f->len;
278
279         while (remains) {
280                 len = strlen(c);
281                 if (!strcmp(c, compatible))
282                         return 1;
283
284                 if (remains < len + 1)
285                         break;
286
287                 c += (len + 1);
288                 remains -= (len + 1);
289         }
290         return 0;
291 }
292
293 const struct device_node *
294 of_find_compatible_node(const struct device_node *from,
295                         const char *type __always_unused,
296                         const char *compatible)
297 {
298         const struct dt_dir *d;
299
300         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
301
302         if (list_empty(&linear))
303                 return NULL;
304         if (!from)
305                 d = list_entry(linear.next, struct dt_dir, linear);
306         else
307                 d = node2dir(from);
308         for (d = next_linear(d); d && (!d->compatible ||
309                                        !check_compatible(d->compatible,
310                                        compatible));
311                         d = next_linear(d))
312                 ;
313         if (d)
314                 return &d->node.node;
315         return NULL;
316 }
317
318 const void *
319 of_get_property(const struct device_node *from, const char *name,
320                 size_t *lenp)
321 {
322         const struct dt_dir *d;
323         const struct dt_file *f;
324
325         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
326
327         d = node2dir(from);
328         list_for_each_entry(f, &d->files, node.list)
329                 if (!strcmp(f->node.node.name, name)) {
330                         if (lenp)
331                                 *lenp = f->len;
332                         return f->buf;
333                 }
334         return NULL;
335 }
336
337 bool
338 of_device_is_available(const struct device_node *dev_node)
339 {
340         const struct dt_dir *d;
341
342         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
343         d = node2dir(dev_node);
344         if (!d->status)
345                 return true;
346         if (!strcmp((char *)d->status->buf, "okay"))
347                 return true;
348         if (!strcmp((char *)d->status->buf, "ok"))
349                 return true;
350         return false;
351 }
352
353 const struct device_node *
354 of_find_node_by_phandle(phandle ph)
355 {
356         const struct dt_dir *d;
357
358         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
359         list_for_each_entry(d, &linear, linear)
360                 if (d->lphandle && (d->lphandle->len == 4) &&
361                     !memcmp(d->lphandle->buf, &ph, 4))
362                         return &d->node.node;
363         return NULL;
364 }
365
366 const struct device_node *
367 of_get_parent(const struct device_node *dev_node)
368 {
369         const struct dt_dir *d;
370
371         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
372
373         if (!dev_node)
374                 return NULL;
375         d = node2dir(dev_node);
376         if (!d->parent)
377                 return NULL;
378         return &d->parent->node.node;
379 }
380
381 const struct device_node *
382 of_get_next_child(const struct device_node *dev_node,
383                   const struct device_node *prev)
384 {
385         const struct dt_dir *p, *c;
386
387         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
388
389         if (!dev_node)
390                 return NULL;
391         p = node2dir(dev_node);
392         if (prev) {
393                 c = node2dir(prev);
394                 DPAA_BUS_HWWARN((c->parent != p), "Parent/child mismatch");
395                 if (c->parent != p)
396                         return NULL;
397                 if (c->node.list.next == &p->subdirs)
398                         /* prev was the last child */
399                         return NULL;
400                 c = list_entry(c->node.list.next, struct dt_dir, node.list);
401                 return &c->node.node;
402         }
403         /* Return first child */
404         if (list_empty(&p->subdirs))
405                 return NULL;
406         c = list_entry(p->subdirs.next, struct dt_dir, node.list);
407         return &c->node.node;
408 }
409
410 uint32_t
411 of_n_addr_cells(const struct device_node *dev_node)
412 {
413         const struct dt_dir *d;
414
415         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised");
416         if (!dev_node)
417                 return OF_DEFAULT_NA;
418         d = node2dir(dev_node);
419         while ((d = d->parent))
420                 if (d->a_cells) {
421                         unsigned char *buf =
422                                 (unsigned char *)&d->a_cells->buf[0];
423                         assert(d->a_cells->len == 4);
424                         return ((uint32_t)buf[0] << 24) |
425                                 ((uint32_t)buf[1] << 16) |
426                                 ((uint32_t)buf[2] << 8) |
427                                 (uint32_t)buf[3];
428                 }
429         return OF_DEFAULT_NA;
430 }
431
432 uint32_t
433 of_n_size_cells(const struct device_node *dev_node)
434 {
435         const struct dt_dir *d;
436
437         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
438         if (!dev_node)
439                 return OF_DEFAULT_NA;
440         d = node2dir(dev_node);
441         while ((d = d->parent))
442                 if (d->s_cells) {
443                         unsigned char *buf =
444                                 (unsigned char *)&d->s_cells->buf[0];
445                         assert(d->s_cells->len == 4);
446                         return ((uint32_t)buf[0] << 24) |
447                                 ((uint32_t)buf[1] << 16) |
448                                 ((uint32_t)buf[2] << 8) |
449                                 (uint32_t)buf[3];
450                 }
451         return OF_DEFAULT_NS;
452 }
453
454 const uint32_t *
455 of_get_address(const struct device_node *dev_node, size_t idx,
456                uint64_t *size, uint32_t *flags __rte_unused)
457 {
458         const struct dt_dir *d;
459         const unsigned char *buf;
460         uint32_t na = of_n_addr_cells(dev_node);
461         uint32_t ns = of_n_size_cells(dev_node);
462
463         if (!dev_node)
464                 d = &root_dir;
465         else
466                 d = node2dir(dev_node);
467         if (!d->reg)
468                 return NULL;
469         assert(d->reg->len % ((na + ns) * 4) == 0);
470         assert(d->reg->len / ((na + ns) * 4) > (unsigned int) idx);
471         buf = (const unsigned char *)&d->reg->buf[0];
472         buf += (na + ns) * idx * 4;
473         if (size)
474                 for (*size = 0; ns > 0; ns--, na++)
475                         *size = (*size << 32) +
476                                 (((uint32_t)buf[4 * na] << 24) |
477                                 ((uint32_t)buf[4 * na + 1] << 16) |
478                                 ((uint32_t)buf[4 * na + 2] << 8) |
479                                 (uint32_t)buf[4 * na + 3]);
480         return (const uint32_t *)buf;
481 }
482
483 uint64_t
484 of_translate_address(const struct device_node *dev_node,
485                      const uint32_t *addr)
486 {
487         uint64_t phys_addr, tmp_addr;
488         const struct device_node *parent;
489         const uint32_t *ranges;
490         size_t rlen;
491         uint32_t na, pna;
492
493         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
494         assert(dev_node != NULL);
495
496         na = of_n_addr_cells(dev_node);
497         phys_addr = of_read_number(addr, na);
498
499         dev_node = of_get_parent(dev_node);
500         if (!dev_node)
501                 return 0;
502         else if (node2dir(dev_node) == &root_dir)
503                 return phys_addr;
504
505         do {
506                 pna = of_n_addr_cells(dev_node);
507                 parent = of_get_parent(dev_node);
508                 if (!parent)
509                         return 0;
510
511                 ranges = of_get_property(dev_node, "ranges", &rlen);
512                 /* "ranges" property is missing. Translation breaks */
513                 if (!ranges)
514                         return 0;
515                 /* "ranges" property is empty. Do 1:1 translation */
516                 else if (rlen == 0)
517                         continue;
518                 else
519                         tmp_addr = of_read_number(ranges + na, pna);
520
521                 na = pna;
522                 dev_node = parent;
523                 phys_addr += tmp_addr;
524         } while (node2dir(parent) != &root_dir);
525
526         return phys_addr;
527 }
528
529 bool
530 of_device_is_compatible(const struct device_node *dev_node,
531                         const char *compatible)
532 {
533         const struct dt_dir *d;
534
535         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
536         if (!dev_node)
537                 d = &root_dir;
538         else
539                 d = node2dir(dev_node);
540         if (d->compatible && check_compatible(d->compatible, compatible))
541                 return true;
542         return false;
543 }