xilinx: kconfig: Change Kconfig dependencies for Xilinx drivers
[platform/kernel/u-boot.git] / common / fdt_region.c
1 // SPDX-License-Identifier: GPL-2.0+ OR BSD-2-Clause
2 /*
3  * libfdt - Flat Device Tree manipulation
4  * Copyright (C) 2013 Google, Inc
5  * Written by Simon Glass <sjg@chromium.org>
6  */
7
8 #include <fdt_support.h>
9 #include <linux/libfdt_env.h>
10 #include <fdt_region.h>
11
12 #ifndef USE_HOSTCC
13 #include <fdt.h>
14 #include <linux/libfdt.h>
15 #else
16 #include "fdt_host.h"
17 #endif
18
19 #define FDT_MAX_DEPTH   32
20
21 static int str_in_list(const char *str, char * const list[], int count)
22 {
23         int i;
24
25         for (i = 0; i < count; i++)
26                 if (!strcmp(list[i], str))
27                         return 1;
28
29         return 0;
30 }
31
32 int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
33                      char * const exc_prop[], int exc_prop_count,
34                      struct fdt_region region[], int max_regions,
35                      char *path, int path_len, int add_string_tab)
36 {
37         int stack[FDT_MAX_DEPTH] = { 0 };
38         char *end;
39         int nextoffset = 0;
40         uint32_t tag;
41         int count = 0;
42         int start = -1;
43         int depth = -1;
44         int want = 0;
45         int base = fdt_off_dt_struct(fdt);
46
47         end = path;
48         *end = '\0';
49         do {
50                 const struct fdt_property *prop;
51                 const char *name;
52                 const char *str;
53                 int include = 0;
54                 int stop_at = 0;
55                 int offset;
56                 int len;
57
58                 offset = nextoffset;
59                 tag = fdt_next_tag(fdt, offset, &nextoffset);
60                 stop_at = nextoffset;
61
62                 switch (tag) {
63                 case FDT_PROP:
64                         include = want >= 2;
65                         stop_at = offset;
66                         prop = fdt_get_property_by_offset(fdt, offset, NULL);
67                         str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
68                         if (!str)
69                                 return -FDT_ERR_BADSTRUCTURE;
70                         if (str_in_list(str, exc_prop, exc_prop_count))
71                                 include = 0;
72                         break;
73
74                 case FDT_NOP:
75                         include = want >= 2;
76                         stop_at = offset;
77                         break;
78
79                 case FDT_BEGIN_NODE:
80                         depth++;
81                         if (depth == FDT_MAX_DEPTH)
82                                 return -FDT_ERR_BADSTRUCTURE;
83                         name = fdt_get_name(fdt, offset, &len);
84                         if (end - path + 2 + len >= path_len)
85                                 return -FDT_ERR_NOSPACE;
86                         if (end != path + 1)
87                                 *end++ = '/';
88                         strcpy(end, name);
89                         end += len;
90                         stack[depth] = want;
91                         if (want == 1)
92                                 stop_at = offset;
93                         if (str_in_list(path, inc, inc_count))
94                                 want = 2;
95                         else if (want)
96                                 want--;
97                         else
98                                 stop_at = offset;
99                         include = want;
100                         break;
101
102                 case FDT_END_NODE:
103                         /* Depth must never go below -1 */
104                         if (depth < 0)
105                                 return -FDT_ERR_BADSTRUCTURE;
106                         include = want;
107                         want = stack[depth--];
108                         while (end > path && *--end != '/')
109                                 ;
110                         *end = '\0';
111                         break;
112
113                 case FDT_END:
114                         include = 1;
115                         break;
116                 }
117
118                 if (include && start == -1) {
119                         /* Should we merge with previous? */
120                         if (count && count <= max_regions &&
121                             offset == region[count - 1].offset +
122                                         region[count - 1].size - base)
123                                 start = region[--count].offset - base;
124                         else
125                                 start = offset;
126                 }
127
128                 if (!include && start != -1) {
129                         if (count < max_regions) {
130                                 region[count].offset = base + start;
131                                 region[count].size = stop_at - start;
132                         }
133                         count++;
134                         start = -1;
135                 }
136         } while (tag != FDT_END);
137
138         if (nextoffset != fdt_size_dt_struct(fdt))
139                 return -FDT_ERR_BADLAYOUT;
140
141         /* Add a region for the END tag and the string table */
142         if (count < max_regions) {
143                 region[count].offset = base + start;
144                 region[count].size = nextoffset - start;
145                 if (add_string_tab)
146                         region[count].size += fdt_size_dt_strings(fdt);
147         }
148         count++;
149
150         return count;
151 }
152
153 /**
154  * fdt_add_region() - Add a new region to our list
155  * @info:       State information
156  * @offset:     Start offset of region
157  * @size:       Size of region
158  *
159  * The region is added if there is space, but in any case we increment the
160  * count. If permitted, and the new region overlaps the last one, we merge
161  * them.
162  */
163 static int fdt_add_region(struct fdt_region_state *info, int offset, int size)
164 {
165         struct fdt_region *reg;
166
167         reg = info->region ? &info->region[info->count - 1] : NULL;
168         if (info->can_merge && info->count &&
169             info->count <= info->max_regions &&
170             reg && offset <= reg->offset + reg->size) {
171                 reg->size = offset + size - reg->offset;
172         } else if (info->count++ < info->max_regions) {
173                 if (reg) {
174                         reg++;
175                         reg->offset = offset;
176                         reg->size = size;
177                 }
178         } else {
179                 return -1;
180         }
181
182         return 0;
183 }
184
185 static int region_list_contains_offset(struct fdt_region_state *info,
186                                        const void *fdt, int target)
187 {
188         struct fdt_region *reg;
189         int num;
190
191         target += fdt_off_dt_struct(fdt);
192         for (reg = info->region, num = 0; num < info->count; reg++, num++) {
193                 if (target >= reg->offset && target < reg->offset + reg->size)
194                         return 1;
195         }
196
197         return 0;
198 }
199
200 /**
201  * fdt_add_alias_regions() - Add regions covering the aliases that we want
202  *
203  * The /aliases node is not automatically included by fdtgrep unless the
204  * command-line arguments cause to be included (or not excluded). However
205  * aliases are special in that we generally want to include those which
206  * reference a node that fdtgrep includes.
207  *
208  * In fact we want to include only aliases for those nodes still included in
209  * the fdt, and drop the other aliases since they point to nodes that will not
210  * be present.
211  *
212  * This function scans the aliases and adds regions for those which we want
213  * to keep.
214  *
215  * @fdt: Device tree to scan
216  * @region: List of regions
217  * @count: Number of regions in the list so far (i.e. starting point for this
218  *      function)
219  * @max_regions: Maximum number of regions in @region list
220  * @info: Place to put the region state
221  * @return number of regions after processing, or -FDT_ERR_NOSPACE if we did
222  * not have enough room in the regions table for the regions we wanted to add.
223  */
224 int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
225                           int max_regions, struct fdt_region_state *info)
226 {
227         int base = fdt_off_dt_struct(fdt);
228         int node, node_end, offset;
229         int did_alias_header;
230
231         node = fdt_subnode_offset(fdt, 0, "aliases");
232         if (node < 0)
233                 return -FDT_ERR_NOTFOUND;
234
235         /*
236          * Find the next node so that we know where the /aliases node ends. We
237          * need special handling if /aliases is the last node.
238          */
239         node_end = fdt_next_subnode(fdt, node);
240         if (node_end == -FDT_ERR_NOTFOUND)
241                 /* Move back to the FDT_END_NODE tag of '/' */
242                 node_end = fdt_size_dt_struct(fdt) - sizeof(fdt32_t) * 2;
243         else if (node_end < 0) /* other error */
244                 return node_end;
245         node_end -= sizeof(fdt32_t);  /* Move to FDT_END_NODE tag of /aliases */
246
247         did_alias_header = 0;
248         info->region = region;
249         info->count = count;
250         info->can_merge = 0;
251         info->max_regions = max_regions;
252
253         for (offset = fdt_first_property_offset(fdt, node);
254              offset >= 0;
255              offset = fdt_next_property_offset(fdt, offset)) {
256                 const struct fdt_property *prop;
257                 const char *name;
258                 int target, next;
259
260                 prop = fdt_get_property_by_offset(fdt, offset, NULL);
261                 name = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
262                 target = fdt_path_offset(fdt, name);
263                 if (!region_list_contains_offset(info, fdt, target))
264                         continue;
265                 next = fdt_next_property_offset(fdt, offset);
266                 if (next < 0)
267                         next = node_end;
268
269                 if (!did_alias_header) {
270                         fdt_add_region(info, base + node, 12);
271                         did_alias_header = 1;
272                 }
273                 fdt_add_region(info, base + offset, next - offset);
274         }
275
276         /* Add the FDT_END_NODE tag */
277         if (did_alias_header)
278                 fdt_add_region(info, base + node_end, sizeof(fdt32_t));
279
280         return info->count < max_regions ? info->count : -FDT_ERR_NOSPACE;
281 }
282
283 /**
284  * fdt_include_supernodes() - Include supernodes required by this node
285  * @info:       State information
286  * @depth:      Current stack depth
287  *
288  * When we decided to include a node or property which is not at the top
289  * level, this function forces the inclusion of higher level nodes. For
290  * example, given this tree:
291  *
292  * / {
293  *     testing {
294  *     }
295  * }
296  *
297  * If we decide to include testing then we need the root node to have a valid
298  * tree. This function adds those regions.
299  */
300 static int fdt_include_supernodes(struct fdt_region_state *info, int depth)
301 {
302         int base = fdt_off_dt_struct(info->fdt);
303         int start, stop_at;
304         int i;
305
306         /*
307          * Work down the stack looking for supernodes that we didn't include.
308          * The algortihm here is actually pretty simple, since we know that
309          * no previous subnode had to include these nodes, or if it did, we
310          * marked them as included (on the stack) already.
311          */
312         for (i = 0; i <= depth; i++) {
313                 if (!info->stack[i].included) {
314                         start = info->stack[i].offset;
315
316                         /* Add the FDT_BEGIN_NODE tag of this supernode */
317                         fdt_next_tag(info->fdt, start, &stop_at);
318                         if (fdt_add_region(info, base + start, stop_at - start))
319                                 return -1;
320
321                         /* Remember that this supernode is now included */
322                         info->stack[i].included = 1;
323                         info->can_merge = 1;
324                 }
325
326                 /* Force (later) generation of the FDT_END_NODE tag */
327                 if (!info->stack[i].want)
328                         info->stack[i].want = WANT_NODES_ONLY;
329         }
330
331         return 0;
332 }
333
334 enum {
335         FDT_DONE_NOTHING,
336         FDT_DONE_MEM_RSVMAP,
337         FDT_DONE_STRUCT,
338         FDT_DONE_END,
339         FDT_DONE_STRINGS,
340         FDT_DONE_ALL,
341 };
342
343 int fdt_first_region(const void *fdt,
344                 int (*h_include)(void *priv, const void *fdt, int offset,
345                                  int type, const char *data, int size),
346                 void *priv, struct fdt_region *region,
347                 char *path, int path_len, int flags,
348                 struct fdt_region_state *info)
349 {
350         struct fdt_region_ptrs *p = &info->ptrs;
351
352         /* Set up our state */
353         info->fdt = fdt;
354         info->can_merge = 1;
355         info->max_regions = 1;
356         info->start = -1;
357         p->want = WANT_NOTHING;
358         p->end = path;
359         *p->end = '\0';
360         p->nextoffset = 0;
361         p->depth = -1;
362         p->done = FDT_DONE_NOTHING;
363
364         return fdt_next_region(fdt, h_include, priv, region,
365                                path, path_len, flags, info);
366 }
367
368 /***********************************************************************
369  *
370  *      Theory of operation
371  *
372  * Note: in this description 'included' means that a node (or other part
373  * of the tree) should be included in the region list, i.e. it will have
374  * a region which covers its part of the tree.
375  *
376  * This function maintains some state from the last time it is called.
377  * It checks the next part of the tree that it is supposed to look at
378  * (p.nextoffset) to see if that should be included or not. When it
379  * finds something to include, it sets info->start to its offset. This
380  * marks the start of the region we want to include.
381  *
382  * Once info->start is set to the start (i.e. not -1), we continue
383  * scanning until we find something that we don't want included. This
384  * will be the end of a region. At this point we can close off the
385  * region and add it to the list. So we do so, and reset info->start
386  * to -1.
387  *
388  * One complication here is that we want to merge regions. So when we
389  * come to add another region later, we may in fact merge it with the
390  * previous one if one ends where the other starts.
391  *
392  * The function fdt_add_region() will return -1 if it fails to add the
393  * region, because we already have a region ready to be returned, and
394  * the new one cannot be merged in with it. In this case, we must return
395  * the region we found, and wait for another call to this function.
396  * When it comes, we will repeat the processing of the tag and again
397  * try to add a region. This time it will succeed.
398  *
399  * The current state of the pointers (stack, offset, etc.) is maintained
400  * in a ptrs member. At the start of every loop iteration we make a copy
401  * of it.  The copy is then updated as the tag is processed. Only if we
402  * get to the end of the loop iteration (and successfully call
403  * fdt_add_region() if we need to) can we commit the changes we have
404  * made to these pointers. For example, if we see an FDT_END_NODE tag,
405  * we will decrement the depth value. But if we need to add a region
406  * for this tag (let's say because the previous tag is included and this
407  * FDT_END_NODE tag is not included) then we will only commit the result
408  * if we were able to add the region. That allows us to retry again next
409  * time.
410  *
411  * We keep track of a variable called 'want' which tells us what we want
412  * to include when there is no specific information provided by the
413  * h_include function for a particular property. This basically handles
414  * the inclusion of properties which are pulled in by virtue of the node
415  * they are in. So if you include a node, its properties are also
416  * included.  In this case 'want' will be WANT_NODES_AND_PROPS. The
417  * FDT_REG_DIRECT_SUBNODES feature also makes use of 'want'. While we
418  * are inside the subnode, 'want' will be set to WANT_NODES_ONLY, so
419  * that only the subnode's FDT_BEGIN_NODE and FDT_END_NODE tags will be
420  * included, and properties will be skipped. If WANT_NOTHING is
421  * selected, then we will just rely on what the h_include() function
422  * tells us.
423  *
424  * Using 'want' we work out 'include', which tells us whether this
425  * current tag should be included or not. As you can imagine, if the
426  * value of 'include' changes, that means we are on a boundary between
427  * nodes to include and nodes to exclude. At this point we either close
428  * off a previous region and add it to the list, or mark the start of a
429  * new region.
430  *
431  * Apart from the nodes, we have mem_rsvmap, the FDT_END tag and the
432  * string list. Each of these dealt with as a whole (i.e. we create a
433  * region for each if it is to be included). For mem_rsvmap we don't
434  * allow it to merge with the first struct region. For the stringlist,
435  * we don't allow it to merge with the last struct region (which
436  * contains at minimum the FDT_END tag).
437  *
438  *********************************************************************/
439
440 int fdt_next_region(const void *fdt,
441                 int (*h_include)(void *priv, const void *fdt, int offset,
442                                  int type, const char *data, int size),
443                 void *priv, struct fdt_region *region,
444                 char *path, int path_len, int flags,
445                 struct fdt_region_state *info)
446 {
447         int base = fdt_off_dt_struct(fdt);
448         int last_node = 0;
449         const char *str;
450
451         info->region = region;
452         info->count = 0;
453         if (info->ptrs.done < FDT_DONE_MEM_RSVMAP &&
454             (flags & FDT_REG_ADD_MEM_RSVMAP)) {
455                 /* Add the memory reserve map into its own region */
456                 if (fdt_add_region(info, fdt_off_mem_rsvmap(fdt),
457                                    fdt_off_dt_struct(fdt) -
458                                    fdt_off_mem_rsvmap(fdt)))
459                         return 0;
460                 info->can_merge = 0;    /* Don't allow merging with this */
461                 info->ptrs.done = FDT_DONE_MEM_RSVMAP;
462         }
463
464         /*
465          * Work through the tags one by one, deciding whether each needs to
466          * be included or not. We set the variable 'include' to indicate our
467          * decision. 'want' is used to track what we want to include - it
468          * allows us to pick up all the properties (and/or subnode tags) of
469          * a node.
470          */
471         while (info->ptrs.done < FDT_DONE_STRUCT) {
472                 const struct fdt_property *prop;
473                 struct fdt_region_ptrs p;
474                 const char *name;
475                 int include = 0;
476                 int stop_at = 0;
477                 uint32_t tag;
478                 int offset;
479                 int val;
480                 int len;
481
482                 /*
483                  * Make a copy of our pointers. If we make it to the end of
484                  * this block then we will commit them back to info->ptrs.
485                  * Otherwise we can try again from the same starting state
486                  * next time we are called.
487                  */
488                 p = info->ptrs;
489
490                 /*
491                  * Find the tag, and the offset of the next one. If we need to
492                  * stop including tags, then by default we stop *after*
493                  * including the current tag
494                  */
495                 offset = p.nextoffset;
496                 tag = fdt_next_tag(fdt, offset, &p.nextoffset);
497                 stop_at = p.nextoffset;
498
499                 switch (tag) {
500                 case FDT_PROP:
501                         stop_at = offset;
502                         prop = fdt_get_property_by_offset(fdt, offset, NULL);
503                         str = fdt_string(fdt, fdt32_to_cpu(prop->nameoff));
504                         val = h_include(priv, fdt, last_node, FDT_IS_PROP, str,
505                                             strlen(str) + 1);
506                         if (val == -1) {
507                                 include = p.want >= WANT_NODES_AND_PROPS;
508                         } else {
509                                 include = val;
510                                 /*
511                                  * Make sure we include the } for this block.
512                                  * It might be more correct to have this done
513                                  * by the call to fdt_include_supernodes() in
514                                  * the case where it adds the node we are
515                                  * currently in, but this is equivalent.
516                                  */
517                                 if ((flags & FDT_REG_SUPERNODES) && val &&
518                                     !p.want)
519                                         p.want = WANT_NODES_ONLY;
520                         }
521
522                         /* Value grepping is not yet supported */
523                         break;
524
525                 case FDT_NOP:
526                         include = p.want >= WANT_NODES_AND_PROPS;
527                         stop_at = offset;
528                         break;
529
530                 case FDT_BEGIN_NODE:
531                         last_node = offset;
532                         p.depth++;
533                         if (p.depth == FDT_MAX_DEPTH)
534                                 return -FDT_ERR_BADSTRUCTURE;
535                         name = fdt_get_name(fdt, offset, &len);
536                         if (p.end - path + 2 + len >= path_len)
537                                 return -FDT_ERR_NOSPACE;
538
539                         /* Build the full path of this node */
540                         if (p.end != path + 1)
541                                 *p.end++ = '/';
542                         strcpy(p.end, name);
543                         p.end += len;
544                         info->stack[p.depth].want = p.want;
545                         info->stack[p.depth].offset = offset;
546
547                         /*
548                          * If we are not intending to include this node unless
549                          * it matches, make sure we stop *before* its tag.
550                          */
551                         if (p.want == WANT_NODES_ONLY ||
552                             !(flags & (FDT_REG_DIRECT_SUBNODES |
553                                        FDT_REG_ALL_SUBNODES))) {
554                                 stop_at = offset;
555                                 p.want = WANT_NOTHING;
556                         }
557                         val = h_include(priv, fdt, offset, FDT_IS_NODE, path,
558                                         p.end - path + 1);
559
560                         /* Include this if requested */
561                         if (val) {
562                                 p.want = (flags & FDT_REG_ALL_SUBNODES) ?
563                                         WANT_ALL_NODES_AND_PROPS :
564                                         WANT_NODES_AND_PROPS;
565                         }
566
567                         /* If not requested, decay our 'p.want' value */
568                         else if (p.want) {
569                                 if (p.want != WANT_ALL_NODES_AND_PROPS)
570                                         p.want--;
571
572                         /* Not including this tag, so stop now */
573                         } else {
574                                 stop_at = offset;
575                         }
576
577                         /*
578                          * Decide whether to include this tag, and update our
579                          * stack with the state for this node
580                          */
581                         include = p.want;
582                         info->stack[p.depth].included = include;
583                         break;
584
585                 case FDT_END_NODE:
586                         include = p.want;
587                         if (p.depth < 0)
588                                 return -FDT_ERR_BADSTRUCTURE;
589
590                         /*
591                          * If we don't want this node, stop right away, unless
592                          * we are including subnodes
593                          */
594                         if (!p.want && !(flags & FDT_REG_DIRECT_SUBNODES))
595                                 stop_at = offset;
596                         p.want = info->stack[p.depth].want;
597                         p.depth--;
598                         while (p.end > path && *--p.end != '/')
599                                 ;
600                         *p.end = '\0';
601                         break;
602
603                 case FDT_END:
604                         /* We always include the end tag */
605                         include = 1;
606                         p.done = FDT_DONE_STRUCT;
607                         break;
608                 }
609
610                 /* If this tag is to be included, mark it as region start */
611                 if (include && info->start == -1) {
612                         /* Include any supernodes required by this one */
613                         if (flags & FDT_REG_SUPERNODES) {
614                                 if (fdt_include_supernodes(info, p.depth))
615                                         return 0;
616                         }
617                         info->start = offset;
618                 }
619
620                 /*
621                  * If this tag is not to be included, finish up the current
622                  * region.
623                  */
624                 if (!include && info->start != -1) {
625                         if (fdt_add_region(info, base + info->start,
626                                            stop_at - info->start))
627                                 return 0;
628                         info->start = -1;
629                         info->can_merge = 1;
630                 }
631
632                 /* If we have made it this far, we can commit our pointers */
633                 info->ptrs = p;
634         }
635
636         /* Add a region for the END tag and a separate one for string table */
637         if (info->ptrs.done < FDT_DONE_END) {
638                 if (info->ptrs.nextoffset != fdt_size_dt_struct(fdt))
639                         return -FDT_ERR_BADSTRUCTURE;
640
641                 if (fdt_add_region(info, base + info->start,
642                                    info->ptrs.nextoffset - info->start))
643                         return 0;
644                 info->ptrs.done++;
645         }
646         if (info->ptrs.done < FDT_DONE_STRINGS) {
647                 if (flags & FDT_REG_ADD_STRING_TAB) {
648                         info->can_merge = 0;
649                         if (fdt_off_dt_strings(fdt) <
650                             base + info->ptrs.nextoffset)
651                                 return -FDT_ERR_BADLAYOUT;
652                         if (fdt_add_region(info, fdt_off_dt_strings(fdt),
653                                            fdt_size_dt_strings(fdt)))
654                                 return 0;
655                 }
656                 info->ptrs.done++;
657         }
658
659         return info->count > 0 ? 0 : -FDT_ERR_NOTFOUND;
660 }