New upstream version 17.11-rc3
[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, "#address-cells")) {
219                         if (d->a_cells)
220                                 DPAA_BUS_LOG(DEBUG, "Duplicate a_cells in %s",
221                                              d->node.node.full_name);
222                         d->a_cells = f;
223                 } else if (!strcmp(f->node.node.name, "#size-cells")) {
224                         if (d->s_cells)
225                                 DPAA_BUS_LOG(DEBUG, "Duplicate s_cells in %s",
226                                              d->node.node.full_name);
227                         d->s_cells = f;
228                 } else if (!strcmp(f->node.node.name, "reg")) {
229                         if (d->reg)
230                                 DPAA_BUS_LOG(DEBUG, "Duplicate reg in %s",
231                                              d->node.node.full_name);
232                         d->reg = f;
233                 }
234         }
235
236         list_for_each_entry(dd, &d->subdirs, node.list) {
237                 list_add_tail(&dd->linear, &linear);
238                 linear_dir(dd);
239         }
240 }
241
242 int
243 of_init_path(const char *dt_path)
244 {
245         int ret;
246
247         base_dir = dt_path;
248
249         /* This needs to be singleton initialization */
250         DPAA_BUS_HWWARN(alive, "Double-init of device-tree driver!");
251
252         /* Prepare root node (the remaining fields are set in process_dir()) */
253         root_dir.node.node.name[0] = '\0';
254         root_dir.node.node.full_name[0] = '\0';
255         INIT_LIST_HEAD(&root_dir.node.list);
256         root_dir.parent = NULL;
257
258         /* Kick things off... */
259         ret = process_dir("", &root_dir);
260         if (ret) {
261                 DPAA_BUS_LOG(ERR, "Unable to parse device tree");
262                 return ret;
263         }
264
265         /* Now make a flat, linear list of directories */
266         linear_dir(&root_dir);
267         alive = 1;
268         return 0;
269 }
270
271 static void
272 destroy_dir(struct dt_dir *d)
273 {
274         struct dt_file *f, *tmpf;
275         struct dt_dir *dd, *tmpd;
276
277         list_for_each_entry_safe(f, tmpf, &d->files, node.list) {
278                 list_del(&f->node.list);
279                 free(f);
280         }
281         list_for_each_entry_safe(dd, tmpd, &d->subdirs, node.list) {
282                 destroy_dir(dd);
283                 list_del(&dd->node.list);
284                 free(dd);
285         }
286 }
287
288 void
289 of_finish(void)
290 {
291         DPAA_BUS_HWWARN(!alive, "Double-finish of device-tree driver!");
292
293         destroy_dir(&root_dir);
294         INIT_LIST_HEAD(&linear);
295         alive = 0;
296 }
297
298 static const struct dt_dir *
299 next_linear(const struct dt_dir *f)
300 {
301         if (f->linear.next == &linear)
302                 return NULL;
303         return list_entry(f->linear.next, struct dt_dir, linear);
304 }
305
306 static int
307 check_compatible(const struct dt_file *f, const char *compatible)
308 {
309         const char *c = (char *)f->buf;
310         unsigned int len, remains = f->len;
311
312         while (remains) {
313                 len = strlen(c);
314                 if (!strcmp(c, compatible))
315                         return 1;
316
317                 if (remains < len + 1)
318                         break;
319
320                 c += (len + 1);
321                 remains -= (len + 1);
322         }
323         return 0;
324 }
325
326 const struct device_node *
327 of_find_compatible_node(const struct device_node *from,
328                         const char *type __always_unused,
329                         const char *compatible)
330 {
331         const struct dt_dir *d;
332
333         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
334
335         if (list_empty(&linear))
336                 return NULL;
337         if (!from)
338                 d = list_entry(linear.next, struct dt_dir, linear);
339         else
340                 d = node2dir(from);
341         for (d = next_linear(d); d && (!d->compatible ||
342                                        !check_compatible(d->compatible,
343                                        compatible));
344                         d = next_linear(d))
345                 ;
346         if (d)
347                 return &d->node.node;
348         return NULL;
349 }
350
351 const void *
352 of_get_property(const struct device_node *from, const char *name,
353                 size_t *lenp)
354 {
355         const struct dt_dir *d;
356         const struct dt_file *f;
357
358         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
359
360         d = node2dir(from);
361         list_for_each_entry(f, &d->files, node.list)
362                 if (!strcmp(f->node.node.name, name)) {
363                         if (lenp)
364                                 *lenp = f->len;
365                         return f->buf;
366                 }
367         return NULL;
368 }
369
370 bool
371 of_device_is_available(const struct device_node *dev_node)
372 {
373         const struct dt_dir *d;
374
375         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
376         d = node2dir(dev_node);
377         if (!d->status)
378                 return true;
379         if (!strcmp((char *)d->status->buf, "okay"))
380                 return true;
381         if (!strcmp((char *)d->status->buf, "ok"))
382                 return true;
383         return false;
384 }
385
386 const struct device_node *
387 of_find_node_by_phandle(phandle ph)
388 {
389         const struct dt_dir *d;
390
391         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
392         list_for_each_entry(d, &linear, linear)
393                 if (d->lphandle && (d->lphandle->len == 4) &&
394                     !memcmp(d->lphandle->buf, &ph, 4))
395                         return &d->node.node;
396         return NULL;
397 }
398
399 const struct device_node *
400 of_get_parent(const struct device_node *dev_node)
401 {
402         const struct dt_dir *d;
403
404         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
405
406         if (!dev_node)
407                 return NULL;
408         d = node2dir(dev_node);
409         if (!d->parent)
410                 return NULL;
411         return &d->parent->node.node;
412 }
413
414 const struct device_node *
415 of_get_next_child(const struct device_node *dev_node,
416                   const struct device_node *prev)
417 {
418         const struct dt_dir *p, *c;
419
420         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
421
422         if (!dev_node)
423                 return NULL;
424         p = node2dir(dev_node);
425         if (prev) {
426                 c = node2dir(prev);
427                 DPAA_BUS_HWWARN((c->parent != p), "Parent/child mismatch");
428                 if (c->parent != p)
429                         return NULL;
430                 if (c->node.list.next == &p->subdirs)
431                         /* prev was the last child */
432                         return NULL;
433                 c = list_entry(c->node.list.next, struct dt_dir, node.list);
434                 return &c->node.node;
435         }
436         /* Return first child */
437         if (list_empty(&p->subdirs))
438                 return NULL;
439         c = list_entry(p->subdirs.next, struct dt_dir, node.list);
440         return &c->node.node;
441 }
442
443 uint32_t
444 of_n_addr_cells(const struct device_node *dev_node)
445 {
446         const struct dt_dir *d;
447
448         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised");
449         if (!dev_node)
450                 return OF_DEFAULT_NA;
451         d = node2dir(dev_node);
452         while ((d = d->parent))
453                 if (d->a_cells) {
454                         unsigned char *buf =
455                                 (unsigned char *)&d->a_cells->buf[0];
456                         assert(d->a_cells->len == 4);
457                         return ((uint32_t)buf[0] << 24) |
458                                 ((uint32_t)buf[1] << 16) |
459                                 ((uint32_t)buf[2] << 8) |
460                                 (uint32_t)buf[3];
461                 }
462         return OF_DEFAULT_NA;
463 }
464
465 uint32_t
466 of_n_size_cells(const struct device_node *dev_node)
467 {
468         const struct dt_dir *d;
469
470         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
471         if (!dev_node)
472                 return OF_DEFAULT_NA;
473         d = node2dir(dev_node);
474         while ((d = d->parent))
475                 if (d->s_cells) {
476                         unsigned char *buf =
477                                 (unsigned char *)&d->s_cells->buf[0];
478                         assert(d->s_cells->len == 4);
479                         return ((uint32_t)buf[0] << 24) |
480                                 ((uint32_t)buf[1] << 16) |
481                                 ((uint32_t)buf[2] << 8) |
482                                 (uint32_t)buf[3];
483                 }
484         return OF_DEFAULT_NS;
485 }
486
487 const uint32_t *
488 of_get_address(const struct device_node *dev_node, size_t idx,
489                uint64_t *size, uint32_t *flags __rte_unused)
490 {
491         const struct dt_dir *d;
492         const unsigned char *buf;
493         uint32_t na = of_n_addr_cells(dev_node);
494         uint32_t ns = of_n_size_cells(dev_node);
495
496         if (!dev_node)
497                 d = &root_dir;
498         else
499                 d = node2dir(dev_node);
500         if (!d->reg)
501                 return NULL;
502         assert(d->reg->len % ((na + ns) * 4) == 0);
503         assert(d->reg->len / ((na + ns) * 4) > (unsigned int) idx);
504         buf = (const unsigned char *)&d->reg->buf[0];
505         buf += (na + ns) * idx * 4;
506         if (size)
507                 for (*size = 0; ns > 0; ns--, na++)
508                         *size = (*size << 32) +
509                                 (((uint32_t)buf[4 * na] << 24) |
510                                 ((uint32_t)buf[4 * na + 1] << 16) |
511                                 ((uint32_t)buf[4 * na + 2] << 8) |
512                                 (uint32_t)buf[4 * na + 3]);
513         return (const uint32_t *)buf;
514 }
515
516 uint64_t
517 of_translate_address(const struct device_node *dev_node,
518                      const uint32_t *addr)
519 {
520         uint64_t phys_addr, tmp_addr;
521         const struct device_node *parent;
522         const uint32_t *ranges;
523         size_t rlen;
524         uint32_t na, pna;
525
526         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
527         assert(dev_node != NULL);
528
529         na = of_n_addr_cells(dev_node);
530         phys_addr = of_read_number(addr, na);
531
532         dev_node = of_get_parent(dev_node);
533         if (!dev_node)
534                 return 0;
535         else if (node2dir(dev_node) == &root_dir)
536                 return phys_addr;
537
538         do {
539                 pna = of_n_addr_cells(dev_node);
540                 parent = of_get_parent(dev_node);
541                 if (!parent)
542                         return 0;
543
544                 ranges = of_get_property(dev_node, "ranges", &rlen);
545                 /* "ranges" property is missing. Translation breaks */
546                 if (!ranges)
547                         return 0;
548                 /* "ranges" property is empty. Do 1:1 translation */
549                 else if (rlen == 0)
550                         continue;
551                 else
552                         tmp_addr = of_read_number(ranges + na, pna);
553
554                 na = pna;
555                 dev_node = parent;
556                 phys_addr += tmp_addr;
557         } while (node2dir(parent) != &root_dir);
558
559         return phys_addr;
560 }
561
562 bool
563 of_device_is_compatible(const struct device_node *dev_node,
564                         const char *compatible)
565 {
566         const struct dt_dir *d;
567
568         DPAA_BUS_HWWARN(!alive, "Device-tree driver not initialised!");
569         if (!dev_node)
570                 d = &root_dir;
571         else
572                 d = node2dir(dev_node);
573         if (d->compatible && check_compatible(d->compatible, compatible))
574                 return true;
575         return false;
576 }