arm: Support pac_key_* register operand for MRS/MSR in Armv8.1-M Mainline
[binutils-gdb.git] / sim / ppc / tree.c
blobc2fe9176e05cd6f41140fa4b8f0dc68642414ad8
1 /* This file is part of the program psim.
3 Copyright (C) 1994-1997, Andrew Cagney <cagney@highland.com.au>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #ifndef _PARSE_C_
22 #define _PARSE_C_
24 #include <stdio.h>
25 #include <stdarg.h>
27 #include "basics.h"
29 #include "device.h"
30 #include "tree.h"
32 #include <stdlib.h>
33 #include <string.h>
34 #include <ctype.h>
36 #include "libiberty.h"
38 /* manipulate/lookup device names */
40 typedef struct _name_specifier {
41 /* components in the full length name */
42 char *path;
43 char *property;
44 char *value;
45 /* current device */
46 char *name;
47 char *base;
48 char *unit;
49 char *args;
50 /* previous device */
51 char *last_name;
52 char *last_base;
53 char *last_unit;
54 char *last_args;
55 /* work area */
56 char buf[1024];
57 } name_specifier;
61 /* Given a device specifier, break it up into its main components:
62 path (and if present) property name and property value. */
64 STATIC_INLINE_TREE\
65 (int)
66 split_device_specifier(device *current,
67 const char *device_specifier,
68 name_specifier *spec)
70 char *chp = NULL;
72 /* expand any leading alias if present */
73 if (current != NULL
74 && *device_specifier != '\0'
75 && *device_specifier != '.'
76 && *device_specifier != '/') {
77 device *aliases = tree_find_device(current, "/aliases");
78 char alias[32];
79 int len = 0;
80 while (device_specifier[len] != '\0'
81 && device_specifier[len] != '/'
82 && device_specifier[len] != ':'
83 && !isspace(device_specifier[len])) {
84 alias[len] = device_specifier[len];
85 len++;
86 if (len >= sizeof(alias))
87 error("split_device_specifier: buffer overflow");
89 alias[len] = '\0';
90 if (aliases != NULL
91 && device_find_property(aliases, alias)) {
92 strcpy(spec->buf, device_find_string_property(aliases, alias));
93 strcat(spec->buf, device_specifier + len);
95 else {
96 strcpy(spec->buf, device_specifier);
99 else {
100 strcpy(spec->buf, device_specifier);
103 /* check no overflow */
104 if (strlen(spec->buf) >= sizeof(spec->buf))
105 error("split_device_specifier: buffer overflow\n");
107 /* strip leading spaces */
108 chp = spec->buf;
109 while (*chp != '\0' && isspace(*chp))
110 chp++;
111 if (*chp == '\0')
112 return 0;
114 /* find the path and terminate it with null */
115 spec->path = chp;
116 while (*chp != '\0' && !isspace(*chp))
117 chp++;
118 if (*chp != '\0') {
119 *chp = '\0';
120 chp++;
123 /* and any value */
124 while (*chp != '\0' && isspace(*chp))
125 chp++;
126 spec->value = chp;
128 /* now go back and chop the property off of the path */
129 if (spec->value[0] == '\0') {
130 spec->property = NULL; /*not a property*/
131 spec->value = NULL;
133 else if (spec->value[0] == '>'
134 || spec->value[0] == '<') {
135 /* an interrupt spec */
136 spec->property = NULL;
138 else {
139 chp = strrchr(spec->path, '/');
140 if (chp == NULL) {
141 spec->property = spec->path;
142 spec->path = strchr(spec->property, '\0');
144 else {
145 *chp = '\0';
146 spec->property = chp+1;
150 /* and mark the rest as invalid */
151 spec->name = NULL;
152 spec->base = NULL;
153 spec->unit = NULL;
154 spec->args = NULL;
155 spec->last_name = NULL;
156 spec->last_base = NULL;
157 spec->last_unit = NULL;
158 spec->last_args = NULL;
160 return 1;
164 /* given a device specifier break it up into its main components -
165 path and property name - assuming that the last `device' is a
166 property name. */
168 STATIC_INLINE_DEVICE\
169 (int)
170 split_property_specifier(device *current,
171 const char *property_specifier,
172 name_specifier *spec)
174 if (split_device_specifier(current, property_specifier, spec)) {
175 if (spec->property == NULL) {
176 /* force the last name to be a property name */
177 char *chp = strrchr(spec->path, '/');
178 if (chp == NULL) {
179 spec->property = spec->path;
180 spec->path = strrchr(spec->property, '\0');;
182 else {
183 *chp = '\0';
184 spec->property = chp+1;
187 return 1;
189 else
190 return 0;
194 /* device the next device name and split it up, return 0 when no more
195 names to device */
197 STATIC_INLINE_TREE\
198 (int)
199 split_device_name(name_specifier *spec)
201 char *chp;
202 /* remember what came before */
203 spec->last_name = spec->name;
204 spec->last_base = spec->base;
205 spec->last_unit = spec->unit;
206 spec->last_args = spec->args;
207 /* finished? */
208 if (spec->path[0] == '\0') {
209 spec->name = NULL;
210 spec->base = NULL;
211 spec->unit = NULL;
212 spec->args = NULL;
213 return 0;
215 /* break the current device spec from the path */
216 spec->name = spec->path;
217 chp = strchr(spec->name, '/');
218 if (chp == NULL)
219 spec->path = strchr(spec->name, '\0');
220 else {
221 spec->path = chp+1;
222 *chp = '\0';
224 /* break out the base */
225 if (spec->name[0] == '(') {
226 chp = strchr(spec->name, ')');
227 if (chp == NULL) {
228 spec->base = spec->name;
230 else {
231 *chp = '\0';
232 spec->base = spec->name + 1;
233 spec->name = chp + 1;
236 else {
237 spec->base = spec->name;
239 /* now break out the unit */
240 chp = strchr(spec->name, '@');
241 if (chp == NULL) {
242 spec->unit = NULL;
243 chp = spec->name;
245 else {
246 *chp = '\0';
247 chp += 1;
248 spec->unit = chp;
250 /* finally any args */
251 chp = strchr(chp, ':');
252 if (chp == NULL)
253 spec->args = NULL;
254 else {
255 *chp = '\0';
256 spec->args = chp+1;
258 return 1;
262 /* device the value, returning the next non-space token */
264 STATIC_INLINE_TREE\
265 (char *)
266 split_value(name_specifier *spec)
268 char *token;
269 if (spec->value == NULL)
270 return NULL;
271 /* skip leading white space */
272 while (isspace(spec->value[0]))
273 spec->value++;
274 if (spec->value[0] == '\0') {
275 spec->value = NULL;
276 return NULL;
278 token = spec->value;
279 /* find trailing space */
280 while (spec->value[0] != '\0' && !isspace(spec->value[0]))
281 spec->value++;
282 /* chop this value out */
283 if (spec->value[0] != '\0') {
284 spec->value[0] = '\0';
285 spec->value++;
287 return token;
292 /* traverse the path specified by spec starting at current */
294 STATIC_INLINE_TREE\
295 (device *)
296 split_find_device(device *current,
297 name_specifier *spec)
299 /* strip off (and process) any leading ., .., ./ and / */
300 while (1) {
301 if (strncmp(spec->path, "/", strlen("/")) == 0) {
302 /* cd /... */
303 while (current != NULL && device_parent(current) != NULL)
304 current = device_parent(current);
305 spec->path += strlen("/");
307 else if (strncmp(spec->path, "./", strlen("./")) == 0) {
308 /* cd ./... */
309 spec->path += strlen("./");
311 else if (strncmp(spec->path, "../", strlen("../")) == 0) {
312 /* cd ../... */
313 if (current != NULL && device_parent(current) != NULL)
314 current = device_parent(current);
315 spec->path += strlen("../");
317 else if (strcmp(spec->path, ".") == 0) {
318 /* cd . */
319 spec->path += strlen(".");
321 else if (strcmp(spec->path, "..") == 0) {
322 /* cd . */
323 if (current != NULL && device_parent(current) != NULL)
324 current = device_parent(current);
325 spec->path += strlen("..");
327 else
328 break;
331 /* now go through the path proper */
333 if (current == NULL) {
334 split_device_name(spec);
335 return NULL;
338 while (split_device_name(spec)) {
339 device *child;
340 for (child = device_child(current);
341 child != NULL; child = device_sibling(child)) {
342 if (strcmp(spec->name, device_name(child)) == 0) {
343 if (spec->unit == NULL)
344 break;
345 else {
346 device_unit phys;
347 device_decode_unit(current, spec->unit, &phys);
348 if (memcmp(&phys, device_unit_address(child),
349 sizeof(device_unit)) == 0)
350 break;
354 if (child == NULL)
355 return current; /* search failed */
356 current = child;
359 return current;
363 STATIC_INLINE_TREE\
364 (device *)
365 split_fill_path(device *current,
366 const char *device_specifier,
367 name_specifier *spec)
369 /* break it up */
370 if (!split_device_specifier(current, device_specifier, spec))
371 device_error(current, "error parsing %s\n", device_specifier);
373 /* fill our tree with its contents */
374 current = split_find_device(current, spec);
376 /* add any additional devices as needed */
377 if (spec->name != NULL) {
378 do {
379 current = device_create(current, spec->base, spec->name,
380 spec->unit, spec->args);
381 } while (split_device_name(spec));
384 return current;
388 INLINE_TREE\
389 (void)
390 tree_init(device *root,
391 psim *system)
393 TRACE(trace_device_tree, ("tree_init(root=%p, system=%p)\n",
394 root,
395 system));
396 /* remove the old, rebuild the new */
397 tree_traverse(root, device_clean, NULL, system);
398 tree_traverse(root, device_init_static_properties, NULL, system);
399 tree_traverse(root, device_init_address, NULL, system);
400 tree_traverse(root, device_init_runtime_properties, NULL, system);
401 tree_traverse(root, device_init_data, NULL, system);
406 /* <non-white-space> */
408 STATIC_INLINE_TREE\
409 (const char *)
410 skip_token(const char *chp)
412 while (!isspace(*chp) && *chp != '\0')
413 chp++;
414 while (isspace(*chp) && *chp != '\0')
415 chp++;
416 return chp;
420 /* count the number of entries */
422 STATIC_INLINE_TREE\
423 (int)
424 count_entries(device *current,
425 const char *property_name,
426 const char *property_value,
427 int modulo)
429 const char *chp = property_value;
430 int nr_entries = 0;
431 while (*chp != '\0') {
432 nr_entries += 1;
433 chp = skip_token(chp);
435 if ((nr_entries % modulo) != 0) {
436 device_error(current, "incorrect number of entries for %s property %s, should be multiple of %d",
437 property_name, property_value, modulo);
439 return nr_entries / modulo;
444 /* parse: <address> ::= <token> ; device dependant */
446 STATIC_INLINE_TREE\
447 (const char *)
448 parse_address(device *current,
449 device *bus,
450 const char *chp,
451 device_unit *address)
453 ASSERT(device_nr_address_cells(bus) > 0);
454 if (device_decode_unit(bus, chp, address) < 0)
455 device_error(current, "invalid unit address in %s", chp);
456 return skip_token(chp);
460 /* parse: <size> ::= <number> { "," <number> } ; */
462 STATIC_INLINE_TREE\
463 (const char *)
464 parse_size(device *current,
465 device *bus,
466 const char *chp,
467 device_unit *size)
469 int i;
470 int nr;
471 const char *curr = chp;
472 memset(size, 0, sizeof(*size));
473 /* parse the numeric list */
474 size->nr_cells = device_nr_size_cells(bus);
475 nr = 0;
476 ASSERT(size->nr_cells > 0);
477 while (1) {
478 char *next;
479 size->cells[nr] = strtoul(curr, &next, 0);
480 if (curr == next)
481 device_error(current, "Problem parsing <size> %s", chp);
482 nr += 1;
483 if (next[0] != ',')
484 break;
485 if (nr == size->nr_cells)
486 device_error(current, "Too many values in <size> %s", chp);
487 curr = next + 1;
489 ASSERT(nr > 0 && nr <= size->nr_cells);
490 /* right align the numbers */
491 for (i = 1; i <= size->nr_cells; i++) {
492 if (i <= nr)
493 size->cells[size->nr_cells - i] = size->cells[nr - i];
494 else
495 size->cells[size->nr_cells - i] = 0;
497 return skip_token(chp);
501 /* parse: <reg> ::= { <address> <size> } ; */
503 STATIC_INLINE_TREE\
504 (void)
505 parse_reg_property(device *current,
506 const char *property_name,
507 const char *property_value)
509 int nr_regs;
510 int reg_nr;
511 reg_property_spec *regs;
512 const char *chp;
513 device *bus = device_parent(current);
515 /* determine the number of reg entries by counting tokens */
516 nr_regs = count_entries(current, property_name, property_value,
517 1 + (device_nr_size_cells(bus) > 0));
519 /* create working space */
520 regs = zalloc(nr_regs * sizeof(*regs));
522 /* fill it in */
523 chp = property_value;
524 for (reg_nr = 0; reg_nr < nr_regs; reg_nr++) {
525 chp = parse_address(current, bus, chp, &regs[reg_nr].address);
526 if (device_nr_size_cells(bus) > 0)
527 chp = parse_size(current, bus, chp, &regs[reg_nr].size);
528 else
529 memset(&regs[reg_nr].size, 0, sizeof (regs[reg_nr].size));
532 /* create it */
533 device_add_reg_array_property(current, property_name,
534 regs, nr_regs);
536 free(regs);
540 /* { <child-address> <parent-address> <child-size> }* */
542 STATIC_INLINE_TREE\
543 (void)
544 parse_ranges_property(device *current,
545 const char *property_name,
546 const char *property_value)
548 int nr_ranges;
549 int range_nr;
550 range_property_spec *ranges;
551 const char *chp;
553 /* determine the number of ranges specified */
554 nr_ranges = count_entries(current, property_name, property_value, 3);
556 /* create a property of that size */
557 ranges = zalloc(nr_ranges * sizeof(*ranges));
559 /* fill it in */
560 chp = property_value;
561 for (range_nr = 0; range_nr < nr_ranges; range_nr++) {
562 chp = parse_address(current, current,
563 chp, &ranges[range_nr].child_address);
564 chp = parse_address(current, device_parent(current),
565 chp, &ranges[range_nr].parent_address);
566 chp = parse_size(current, current,
567 chp, &ranges[range_nr].size);
570 /* create it */
571 device_add_range_array_property(current, property_name, ranges, nr_ranges);
573 free(ranges);
577 /* <integer> ... */
579 STATIC_INLINE_TREE\
580 (void)
581 parse_integer_property(device *current,
582 const char *property_name,
583 const char *property_value)
585 int nr_entries;
586 unsigned_cell words[1024];
587 /* integer or integer array? */
588 nr_entries = 0;
589 while (1) {
590 char *end;
591 words[nr_entries] = strtoul(property_value, &end, 0);
592 if (property_value == end)
593 break;
594 nr_entries += 1;
595 if (nr_entries * sizeof(words[0]) >= sizeof(words))
596 device_error(current, "buffer overflow");
597 property_value = end;
599 if (nr_entries == 0)
600 device_error(current, "error parsing integer property %s (%s)",
601 property_name, property_value);
602 else if (nr_entries == 1)
603 device_add_integer_property(current, property_name, words[0]);
604 else {
605 int i;
606 for (i = 0; i < nr_entries; i++) {
607 H2BE(words[i]);
609 /* perhaps integer array property is better */
610 device_add_array_property(current, property_name, words,
611 sizeof(words[0]) * nr_entries);
615 /* PROPERTY_VALUE is a raw property value. Quote it as required by
616 parse_string_property. It is the caller's responsibility to free
617 the memory returned. */
619 EXTERN_TREE\
620 (char *)
621 tree_quote_property(const char *property_value)
623 char *p;
624 char *ret;
625 const char *chp;
626 int quotees;
628 /* Count characters needing quotes in PROPERTY_VALUE. */
629 quotees = 0;
630 for (chp = property_value; *chp; ++chp)
631 if (*chp == '\\' || *chp == '"')
632 ++quotees;
634 ret = (char *) xmalloc (strlen (property_value)
635 + 2 /* quotes */
636 + quotees
637 + 1 /* terminator */);
639 p = ret;
640 /* Add the opening quote. */
641 *p++ = '"';
642 /* Copy the value. */
643 for (chp = property_value; *chp; ++chp)
644 if (*chp == '\\' || *chp == '"')
646 /* Quote this character. */
647 *p++ = '\\';
648 *p++ = *chp;
650 else
651 *p++ = *chp;
652 /* Add the closing quote. */
653 *p++ = '"';
654 /* Terminate the string. */
655 *p++ = '\0';
657 return ret;
660 /* <string> ... */
662 STATIC_INLINE_TREE\
663 (void)
664 parse_string_property(device *current,
665 const char *property_name,
666 const char *property_value)
668 char **strings;
669 const char *chp;
670 int nr_strings;
671 int approx_nr_strings;
673 /* get an estimate as to the number of strings by counting double
674 quotes */
675 approx_nr_strings = 2;
676 for (chp = property_value; *chp; chp++) {
677 if (*chp == '"')
678 approx_nr_strings++;
680 approx_nr_strings = (approx_nr_strings) / 2;
682 /* create a string buffer for that many (plus a null) */
683 strings = (char**)zalloc((approx_nr_strings + 1) * sizeof(char*));
685 /* now find all the strings */
686 chp = property_value;
687 nr_strings = 0;
688 while (1) {
690 /* skip leading space */
691 while (*chp != '\0' && isspace(*chp))
692 chp += 1;
693 if (*chp == '\0')
694 break;
696 /* copy it in */
697 if (*chp == '"') {
698 /* a quoted string - watch for '\' et.al. */
699 /* estimate the size and allocate space for it */
700 int pos;
701 chp++;
702 pos = 0;
703 while (chp[pos] != '\0' && chp[pos] != '"') {
704 if (chp[pos] == '\\' && chp[pos+1] != '\0')
705 pos += 2;
706 else
707 pos += 1;
709 strings[nr_strings] = zalloc(pos + 1);
710 /* copy the string over */
711 pos = 0;
712 while (*chp != '\0' && *chp != '"') {
713 if (*chp == '\\' && *(chp+1) != '\0') {
714 strings[nr_strings][pos] = *(chp+1);
715 chp += 2;
716 pos++;
718 else {
719 strings[nr_strings][pos] = *chp;
720 chp += 1;
721 pos++;
724 if (*chp != '\0')
725 chp++;
726 strings[nr_strings][pos] = '\0';
728 else {
729 /* copy over a single unquoted token */
730 int len = 0;
731 while (chp[len] != '\0' && !isspace(chp[len]))
732 len++;
733 strings[nr_strings] = zalloc(len + 1);
734 strncpy(strings[nr_strings], chp, len);
735 strings[nr_strings][len] = '\0';
736 chp += len;
738 nr_strings++;
739 if (nr_strings > approx_nr_strings)
740 device_error(current, "String property %s badly formatted",
741 property_name);
743 ASSERT(strings[nr_strings] == NULL); /* from zalloc */
745 /* install it */
746 if (nr_strings == 0)
747 device_add_string_property(current, property_name, "");
748 else if (nr_strings == 1)
749 device_add_string_property(current, property_name, strings[0]);
750 else {
751 const char **specs = (const char**)strings; /* stop a bogus error */
752 device_add_string_array_property(current, property_name,
753 specs, nr_strings);
756 /* flush the created string */
757 while (nr_strings > 0) {
758 nr_strings--;
759 free(strings[nr_strings]);
761 free(strings);
765 /* <path-to-ihandle-device> */
767 STATIC_INLINE_TREE\
768 (void)
769 parse_ihandle_property(device *current,
770 const char *property,
771 const char *value)
773 ihandle_runtime_property_spec ihandle;
775 /* pass the full path */
776 ihandle.full_path = value;
778 /* save this ready for the ihandle create */
779 device_add_ihandle_runtime_property(current, property,
780 &ihandle);
785 EXTERN_TREE\
786 (device *)
787 tree_parse(device *current,
788 const char *fmt,
789 ...)
791 char device_specifier[1024];
792 name_specifier spec;
794 /* format the path */
796 va_list ap;
797 va_start(ap, fmt);
798 vsprintf(device_specifier, fmt, ap);
799 va_end(ap);
800 if (strlen(device_specifier) >= sizeof(device_specifier))
801 error("device_tree_add_deviced: buffer overflow\n");
804 /* construct the tree down to the final device */
805 current = split_fill_path(current, device_specifier, &spec);
807 /* is there an interrupt spec */
808 if (spec.property == NULL
809 && spec.value != NULL) {
810 char *op = split_value(&spec);
811 switch (op[0]) {
812 case '>':
814 char *my_port_name = split_value(&spec);
815 int my_port;
816 char *dest_port_name = split_value(&spec);
817 int dest_port;
818 name_specifier dest_spec;
819 char *dest_device_name = split_value(&spec);
820 device *dest;
821 /* find my name */
822 my_port = device_interrupt_decode(current, my_port_name,
823 output_port);
824 /* find the dest device and port */
825 dest = split_fill_path(current, dest_device_name, &dest_spec);
826 dest_port = device_interrupt_decode(dest, dest_port_name,
827 input_port);
828 /* connect the two */
829 device_interrupt_attach(current,
830 my_port,
831 dest,
832 dest_port,
833 permanent_object);
835 break;
836 default:
837 device_error(current, "unreconised interrupt spec %s\n", spec.value);
838 break;
842 /* is there a property */
843 if (spec.property != NULL) {
844 if (strcmp(spec.value, "true") == 0)
845 device_add_boolean_property(current, spec.property, 1);
846 else if (strcmp(spec.value, "false") == 0)
847 device_add_boolean_property(current, spec.property, 0);
848 else {
849 const device_property *property;
850 switch (spec.value[0]) {
851 case '*':
852 parse_ihandle_property(current, spec.property, spec.value + 1);
853 break;
854 case '[':
856 uint8_t words[1024];
857 char *curr = spec.value + 1;
858 int nr_words = 0;
859 while (1) {
860 char *next;
861 words[nr_words] = H2BE_1(strtoul(curr, &next, 0));
862 if (curr == next)
863 break;
864 curr = next;
865 nr_words += 1;
867 device_add_array_property(current, spec.property,
868 words, sizeof(words[0]) * nr_words);
870 break;
871 case '"':
872 parse_string_property(current, spec.property, spec.value);
873 break;
874 case '!':
875 spec.value++;
876 property = tree_find_property(current, spec.value);
877 if (property == NULL)
878 device_error(current, "property %s not found\n", spec.value);
879 device_add_duplicate_property(current,
880 spec.property,
881 property);
882 break;
883 default:
884 if (strcmp(spec.property, "reg") == 0
885 || strcmp(spec.property, "assigned-addresses") == 0
886 || strcmp(spec.property, "alternate-reg") == 0){
887 parse_reg_property(current, spec.property, spec.value);
889 else if (strcmp(spec.property, "ranges") == 0) {
890 parse_ranges_property(current, spec.property, spec.value);
892 else if (isdigit(spec.value[0])
893 || (spec.value[0] == '-' && isdigit(spec.value[1]))
894 || (spec.value[0] == '+' && isdigit(spec.value[1]))) {
895 parse_integer_property(current, spec.property, spec.value);
897 else
898 parse_string_property(current, spec.property, spec.value);
899 break;
903 return current;
907 INLINE_TREE\
908 (void)
909 tree_traverse(device *root,
910 tree_traverse_function *prefix,
911 tree_traverse_function *postfix,
912 void *data)
914 device *child;
915 if (prefix != NULL)
916 prefix(root, data);
917 for (child = device_child(root);
918 child != NULL;
919 child = device_sibling(child)) {
920 tree_traverse(child, prefix, postfix, data);
922 if (postfix != NULL)
923 postfix(root, data);
927 STATIC_INLINE_TREE\
928 (void)
929 print_address(device *bus,
930 const device_unit *phys)
932 char unit[32];
933 device_encode_unit(bus, phys, unit, sizeof(unit));
934 printf_filtered(" %s", unit);
937 STATIC_INLINE_TREE\
938 (void)
939 print_size(device *bus,
940 const device_unit *size)
942 int i;
943 for (i = 0; i < size->nr_cells; i++)
944 if (size->cells[i] != 0)
945 break;
946 if (i < size->nr_cells) {
947 printf_filtered(" 0x%lx", (unsigned long)size->cells[i]);
948 i++;
949 for (; i < size->nr_cells; i++)
950 printf_filtered(",0x%lx", (unsigned long)size->cells[i]);
952 else
953 printf_filtered(" 0");
956 STATIC_INLINE_TREE\
957 (void)
958 print_reg_property(device *me,
959 const device_property *property)
961 int reg_nr;
962 reg_property_spec reg;
963 for (reg_nr = 0;
964 device_find_reg_array_property(me, property->name, reg_nr, &reg);
965 reg_nr++) {
966 print_address(device_parent(me), &reg.address);
967 print_size(me, &reg.size);
971 STATIC_INLINE_TREE\
972 (void)
973 print_ranges_property(device *me,
974 const device_property *property)
976 int range_nr;
977 range_property_spec range;
978 for (range_nr = 0;
979 device_find_range_array_property(me, property->name, range_nr, &range);
980 range_nr++) {
981 print_address(me, &range.child_address);
982 print_address(device_parent(me), &range.parent_address);
983 print_size(me, &range.size);
987 STATIC_INLINE_TREE\
988 (void)
989 print_string(const char *string)
991 printf_filtered(" \"");
992 while (*string != '\0') {
993 switch (*string) {
994 case '"':
995 printf_filtered("\\\"");
996 break;
997 case '\\':
998 printf_filtered("\\\\");
999 break;
1000 default:
1001 printf_filtered("%c", *string);
1002 break;
1004 string++;
1006 printf_filtered("\"");
1009 STATIC_INLINE_TREE\
1010 (void)
1011 print_string_array_property(device *me,
1012 const device_property *property)
1014 int nr;
1015 string_property_spec string;
1016 for (nr = 0;
1017 device_find_string_array_property(me, property->name, nr, &string);
1018 nr++) {
1019 print_string(string);
1023 STATIC_INLINE_TREE\
1024 (void)
1025 print_properties(device *me)
1027 const device_property *property;
1028 for (property = device_find_property(me, NULL);
1029 property != NULL;
1030 property = device_next_property(property)) {
1031 printf_filtered("%s/%s", device_path(me), property->name);
1032 if (property->original != NULL) {
1033 printf_filtered(" !");
1034 printf_filtered("%s/%s",
1035 device_path(property->original->owner),
1036 property->original->name);
1038 else {
1039 switch (property->type) {
1040 case array_property:
1041 if ((property->sizeof_array % sizeof(signed_cell)) == 0) {
1042 unsigned_cell *w = (unsigned_cell*)property->array;
1043 int cell_nr;
1044 for (cell_nr = 0;
1045 cell_nr < (property->sizeof_array / sizeof(unsigned_cell));
1046 cell_nr++) {
1047 printf_filtered(" 0x%lx", (unsigned long)BE2H_cell(w[cell_nr]));
1050 else {
1051 uint8_t *w = (uint8_t*)property->array;
1052 printf_filtered(" [");
1053 while ((char*)w - (char*)property->array < property->sizeof_array) {
1054 printf_filtered(" 0x%2x", BE2H_1(*w));
1055 w++;
1058 break;
1059 case boolean_property:
1061 int b = device_find_boolean_property(me, property->name);
1062 printf_filtered(" %s", b ? "true" : "false");
1064 break;
1065 case ihandle_property:
1067 if (property->array != NULL) {
1068 device_instance *instance = device_find_ihandle_property(me, property->name);
1069 printf_filtered(" *%s", device_instance_path(instance));
1071 else {
1072 /* not yet initialized, ask the device for the path */
1073 ihandle_runtime_property_spec spec;
1074 device_find_ihandle_runtime_property(me, property->name, &spec);
1075 printf_filtered(" *%s", spec.full_path);
1078 break;
1079 case integer_property:
1081 unsigned_word w = device_find_integer_property(me, property->name);
1082 printf_filtered(" 0x%lx", (unsigned long)w);
1084 break;
1085 case range_array_property:
1086 print_ranges_property(me, property);
1087 break;
1088 case reg_array_property:
1089 print_reg_property(me, property);
1090 break;
1091 case string_property:
1093 const char *s = device_find_string_property(me, property->name);
1094 print_string(s);
1096 break;
1097 case string_array_property:
1098 print_string_array_property(me, property);
1099 break;
1102 printf_filtered("\n");
1106 STATIC_INLINE_TREE\
1107 (void)
1108 print_interrupts(device *me,
1109 int my_port,
1110 device *dest,
1111 int dest_port,
1112 void *ignore_or_null)
1114 char src[32];
1115 char dst[32];
1116 device_interrupt_encode(me, my_port, src, sizeof(src), output_port);
1117 device_interrupt_encode(dest, dest_port, dst, sizeof(dst), input_port);
1118 printf_filtered("%s > %s %s %s\n",
1119 device_path(me),
1120 src, dst,
1121 device_path(dest));
1124 STATIC_INLINE_TREE\
1125 (void)
1126 print_device(device *me,
1127 void *ignore_or_null)
1129 printf_filtered("%s\n", device_path(me));
1130 print_properties(me);
1131 device_interrupt_traverse(me, print_interrupts, NULL);
1134 INLINE_TREE\
1135 (void)
1136 tree_print(device *root)
1138 tree_traverse(root,
1139 print_device, NULL,
1140 NULL);
1144 INLINE_TREE\
1145 (void)
1146 tree_usage(int verbose)
1148 if (verbose == 1) {
1149 printf_filtered("\n");
1150 printf_filtered("A device/property specifier has the form:\n");
1151 printf_filtered("\n");
1152 printf_filtered(" /path/to/a/device [ property-value ]\n");
1153 printf_filtered("\n");
1154 printf_filtered("and a possible device is\n");
1155 printf_filtered("\n");
1157 if (verbose > 1) {
1158 printf_filtered("\n");
1159 printf_filtered("A device/property specifier (<spec>) has the format:\n");
1160 printf_filtered("\n");
1161 printf_filtered(" <spec> ::= <path> [ <value> ] ;\n");
1162 printf_filtered(" <path> ::= { <prefix> } { <node> \"/\" } <node> ;\n");
1163 printf_filtered(" <prefix> ::= ( | \"/\" | \"../\" | \"./\" ) ;\n");
1164 printf_filtered(" <node> ::= <name> [ \"@\" <unit> ] [ \":\" <args> ] ;\n");
1165 printf_filtered(" <unit> ::= <number> { \",\" <number> } ;\n");
1166 printf_filtered("\n");
1167 printf_filtered("Where:\n");
1168 printf_filtered("\n");
1169 printf_filtered(" <name> is the name of a device (list below)\n");
1170 printf_filtered(" <unit> is the unit-address relative to the parent bus\n");
1171 printf_filtered(" <args> additional arguments used when creating the device\n");
1172 printf_filtered(" <value> ::= ( <number> # integer property\n");
1173 printf_filtered(" | \"[\" { <number> } # array property (byte)\n");
1174 printf_filtered(" | \"{\" { <number> } # array property (cell)\n");
1175 printf_filtered(" | [ \"true\" | \"false\" ] # boolean property\n");
1176 printf_filtered(" | \"*\" <path> # ihandle property\n");
1177 printf_filtered(" | \"!\" <path> # copy property\n");
1178 printf_filtered(" | \">\" [ <number> ] <path> # attach interrupt\n");
1179 printf_filtered(" | \"<\" <path> # attach child interrupt\n");
1180 printf_filtered(" | \"\\\"\" <text> # string property\n");
1181 printf_filtered(" | <text> # string property\n");
1182 printf_filtered(" ) ;\n");
1183 printf_filtered("\n");
1184 printf_filtered("And the following are valid device names:\n");
1185 printf_filtered("\n");
1191 INLINE_TREE\
1192 (device_instance *)
1193 tree_instance(device *root,
1194 const char *device_specifier)
1196 /* find the device node */
1197 device *me;
1198 name_specifier spec;
1199 if (!split_device_specifier(root, device_specifier, &spec))
1200 return NULL;
1201 me = split_find_device(root, &spec);
1202 if (spec.name != NULL)
1203 return NULL;
1204 /* create the instance */
1205 return device_create_instance(me, device_specifier, spec.last_args);
1209 INLINE_TREE\
1210 (device *)
1211 tree_find_device(device *root,
1212 const char *path_to_device)
1214 device *node;
1215 name_specifier spec;
1217 /* parse the path */
1218 split_device_specifier(root, path_to_device, &spec);
1219 if (spec.value != NULL)
1220 return NULL; /* something wierd */
1222 /* now find it */
1223 node = split_find_device(root, &spec);
1224 if (spec.name != NULL)
1225 return NULL; /* not a leaf */
1227 return node;
1231 INLINE_TREE\
1232 (const device_property *)
1233 tree_find_property(device *root,
1234 const char *path_to_property)
1236 name_specifier spec;
1237 if (!split_property_specifier(root, path_to_property, &spec))
1238 device_error(root, "Invalid property path %s", path_to_property);
1239 root = split_find_device(root, &spec);
1240 return device_find_property(root, spec.property);
1243 INLINE_TREE\
1244 (int)
1245 tree_find_boolean_property(device *root,
1246 const char *path_to_property)
1248 name_specifier spec;
1249 if (!split_property_specifier(root, path_to_property, &spec))
1250 device_error(root, "Invalid property path %s", path_to_property);
1251 root = split_find_device(root, &spec);
1252 return device_find_boolean_property(root, spec.property);
1255 INLINE_TREE\
1256 (signed_cell)
1257 tree_find_integer_property(device *root,
1258 const char *path_to_property)
1260 name_specifier spec;
1261 if (!split_property_specifier(root, path_to_property, &spec))
1262 device_error(root, "Invalid property path %s", path_to_property);
1263 root = split_find_device(root, &spec);
1264 return device_find_integer_property(root, spec.property);
1267 INLINE_TREE\
1268 (device_instance *)
1269 tree_find_ihandle_property(device *root,
1270 const char *path_to_property)
1272 name_specifier spec;
1273 if (!split_property_specifier(root, path_to_property, &spec))
1274 device_error(root, "Invalid property path %s", path_to_property);
1275 root = split_find_device(root, &spec);
1276 return device_find_ihandle_property(root, spec.property);
1279 INLINE_TREE\
1280 (const char *)
1281 tree_find_string_property(device *root,
1282 const char *path_to_property)
1284 name_specifier spec;
1285 if (!split_property_specifier(root, path_to_property, &spec))
1286 device_error(root, "Invalid property path %s", path_to_property);
1287 root = split_find_device(root, &spec);
1288 return device_find_string_property(root, spec.property);
1292 #endif /* _PARSE_C_ */