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