1 /* This file is part of the program psim.
3 Copyright (C) 1994-1998, 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 2 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, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 #include "sim-assert.h"
41 /* manipulate/lookup device names */
43 typedef struct _name_specifier {
45 /* components in the full length name */
69 /* Given a device specifier, break it up into its main components:
70 path (and if present) property name and property value. */
73 split_device_specifier (struct hw *current,
74 const char *device_specifier,
79 /* expand any leading alias if present */
81 && *device_specifier != '\0'
82 && *device_specifier != '.'
83 && *device_specifier != '/')
85 struct hw *aliases = hw_tree_find_device (current, "/aliases");
88 while (device_specifier[len] != '\0'
89 && device_specifier[len] != '/'
90 && device_specifier[len] != ':'
91 && !isspace (device_specifier[len]))
93 alias[len] = device_specifier[len];
95 if (len >= sizeof(alias))
96 hw_abort (NULL, "split_device_specifier: buffer overflow");
100 && hw_find_property (aliases, alias))
102 strcpy (spec->buf, hw_find_string_property(aliases, alias));
103 strcat (spec->buf, device_specifier + len);
107 strcpy (spec->buf, device_specifier);
112 strcpy(spec->buf, device_specifier);
115 /* check no overflow */
116 if (strlen(spec->buf) >= sizeof(spec->buf))
117 hw_abort (NULL, "split_device_specifier: buffer overflow\n");
119 /* strip leading spaces */
121 while (*chp != '\0' && isspace(*chp))
126 /* find the path and terminate it with null */
128 while (*chp != '\0' && !isspace(*chp))
137 while (*chp != '\0' && isspace(*chp))
141 /* now go back and chop the property off of the path */
142 if (spec->value[0] == '\0')
144 spec->property = NULL; /*not a property*/
147 else if (spec->value[0] == '>'
148 || spec->value[0] == '<')
150 /* an interrupt spec */
151 spec->property = NULL;
154 chp = strrchr(spec->path, '/');
157 spec->property = spec->path;
158 spec->path = strchr(spec->property, '\0');
162 spec->property = chp+1;
166 /* and mark the rest as invalid */
171 spec->last_name = NULL;
172 spec->last_family = NULL;
173 spec->last_unit = NULL;
174 spec->last_args = NULL;
180 /* given a device specifier break it up into its main components -
181 path and property name - assuming that the last `device' is a
185 split_property_specifier (struct hw *current,
186 const char *property_specifier,
187 name_specifier *spec)
189 if (split_device_specifier (current, property_specifier, spec))
191 if (spec->property == NULL)
193 /* force the last name to be a property name */
194 char *chp = strrchr (spec->path, '/');
197 spec->property = spec->path;
198 spec->path = strrchr (spec->property, '\0');;
203 spec->property = chp + 1;
213 /* device the next device name and split it up, return 0 when no more
214 names to struct hw */
217 split_device_name (name_specifier *spec)
220 /* remember what came before */
221 spec->last_name = spec->name;
222 spec->last_family = spec->family;
223 spec->last_unit = spec->unit;
224 spec->last_args = spec->args;
226 if (spec->path[0] == '\0')
234 /* break the current device spec from the path */
235 spec->name = spec->path;
236 chp = strchr (spec->name, '/');
238 spec->path = strchr (spec->name, '\0');
244 /* break out the base */
245 if (spec->name[0] == '(')
247 chp = strchr(spec->name, ')');
250 spec->family = spec->name;
255 spec->family = spec->name + 1;
256 spec->name = chp + 1;
261 spec->family = spec->name;
263 /* now break out the unit */
264 chp = strchr(spec->name, '@');
276 /* finally any args */
277 chp = strchr(chp, ':');
289 /* device the value, returning the next non-space token */
292 split_value (name_specifier *spec)
295 if (spec->value == NULL)
297 /* skip leading white space */
298 while (isspace (spec->value[0]))
300 if (spec->value[0] == '\0')
306 /* find trailing space */
307 while (spec->value[0] != '\0' && !isspace (spec->value[0]))
309 /* chop this value out */
310 if (spec->value[0] != '\0')
312 spec->value[0] = '\0';
320 /* traverse the path specified by spec starting at current */
323 split_find_device (struct hw *current,
324 name_specifier *spec)
326 /* strip off (and process) any leading ., .., ./ and / */
329 if (strncmp (spec->path, "/", strlen ("/")) == 0)
332 while (current != NULL && hw_parent (current) != NULL)
333 current = hw_parent (current);
334 spec->path += strlen ("/");
336 else if (strncmp (spec->path, "./", strlen ("./")) == 0)
340 spec->path += strlen ("./");
342 else if (strncmp (spec->path, "../", strlen ("../")) == 0)
345 if (current != NULL && hw_parent (current) != NULL)
346 current = hw_parent (current);
347 spec->path += strlen ("../");
349 else if (strcmp (spec->path, ".") == 0)
353 spec->path += strlen (".");
355 else if (strcmp (spec->path, "..") == 0)
358 if (current != NULL && hw_parent (current) != NULL)
359 current = hw_parent (current);
360 spec->path += strlen ("..");
366 /* now go through the path proper */
370 split_device_name (spec);
374 while (split_device_name (spec))
377 for (child = hw_child (current);
378 child != NULL; child = hw_sibling (child))
380 if (strcmp (spec->name, hw_name (child)) == 0)
382 if (spec->unit == NULL)
387 hw_unit_decode (current, spec->unit, &phys);
388 if (memcmp (&phys, hw_unit_address (child),
389 sizeof (hw_unit)) == 0)
395 return current; /* search failed */
404 split_fill_path (struct hw *current,
405 const char *device_specifier,
406 name_specifier *spec)
409 if (!split_device_specifier (current, device_specifier, spec))
410 hw_abort (current, "error parsing %s\n", device_specifier);
412 /* fill our tree with its contents */
413 current = split_find_device (current, spec);
415 /* add any additional devices as needed */
416 if (spec->name != NULL)
420 if (current != NULL && !hw_finished_p (current))
422 current = hw_create (NULL,
429 while (split_device_name (spec));
436 /* <non-white-space> */
439 skip_token(const char *chp)
441 while (!isspace(*chp) && *chp != '\0')
443 while (isspace(*chp) && *chp != '\0')
449 /* count the number of entries */
452 count_entries (struct hw *current,
453 const char *property_name,
454 const char *property_value,
457 const char *chp = property_value;
462 chp = skip_token (chp);
464 if ((nr_entries % modulo) != 0)
466 hw_abort (current, "incorrect number of entries for %s property %s, should be multiple of %d",
467 property_name, property_value, modulo);
469 return nr_entries / modulo;
474 /* parse: <address> ::= <token> ; device dependant */
477 parse_address (struct hw *current,
482 if (hw_unit_decode (bus, chp, address) < 0)
483 hw_abort (current, "invalid unit address in %s", chp);
484 return skip_token (chp);
488 /* parse: <size> ::= <number> { "," <number> } ; */
491 parse_size (struct hw *current,
498 const char *curr = chp;
499 memset(size, 0, sizeof(*size));
500 /* parse the numeric list */
501 size->nr_cells = hw_unit_nr_size_cells (bus);
506 size->cells[nr] = strtoul (curr, &next, 0);
508 hw_abort (current, "Problem parsing <size> %s", chp);
512 if (nr == size->nr_cells)
513 hw_abort (current, "Too many values in <size> %s", chp);
516 ASSERT (nr > 0 && nr <= size->nr_cells);
517 /* right align the numbers */
518 for (i = 1; i <= size->nr_cells; i++)
521 size->cells[size->nr_cells - i] = size->cells[nr - i];
523 size->cells[size->nr_cells - i] = 0;
525 return skip_token (chp);
529 /* parse: <reg> ::= { <address> <size> } ; */
532 parse_reg_property (struct hw *current,
533 const char *property_name,
534 const char *property_value)
538 reg_property_spec *regs;
541 /* determine the number of reg entries by counting tokens */
542 nr_regs = count_entries (current, property_name, property_value, 2);
544 /* create working space */
545 regs = zalloc (nr_regs * sizeof (*regs));
548 chp = property_value;
549 for (reg_nr = 0; reg_nr < nr_regs; reg_nr++)
551 chp = parse_address (current, hw_parent(current),
552 chp, ®s[reg_nr].address);
553 chp = parse_size (current, hw_parent(current),
554 chp, ®s[reg_nr].size);
558 hw_add_reg_array_property (current, property_name,
565 /* { <child-address> <parent-address> <child-size> }* */
568 parse_ranges_property (struct hw *current,
569 const char *property_name,
570 const char *property_value)
574 range_property_spec *ranges;
577 /* determine the number of ranges specified */
578 nr_ranges = count_entries (current, property_name, property_value, 3);
580 /* create a property of that size */
581 ranges = zalloc (nr_ranges * sizeof(*ranges));
584 chp = property_value;
585 for (range_nr = 0; range_nr < nr_ranges; range_nr++)
587 chp = parse_address (current, current,
588 chp, &ranges[range_nr].child_address);
589 chp = parse_address (current, hw_parent(current),
590 chp, &ranges[range_nr].parent_address);
591 chp = parse_size (current, current,
592 chp, &ranges[range_nr].size);
596 hw_add_range_array_property (current, property_name, ranges, nr_ranges);
605 parse_integer_property (struct hw *current,
606 const char *property_name,
607 const char *property_value)
610 unsigned_cell words[1024];
611 /* integer or integer array? */
616 words[nr_entries] = strtoul (property_value, &end, 0);
617 if (property_value == end)
620 if (nr_entries * sizeof (words[0]) >= sizeof (words))
621 hw_abort (current, "buffer overflow");
622 property_value = end;
625 hw_abort (current, "error parsing integer property %s (%s)",
626 property_name, property_value);
627 else if (nr_entries == 1)
628 hw_add_integer_property (current, property_name, words[0]);
632 for (i = 0; i < nr_entries; i++)
636 /* perhaphs integer array property is better */
637 hw_add_array_property (current, property_name, words,
638 sizeof(words[0]) * nr_entries);
646 parse_string_property (struct hw *current,
647 const char *property_name,
648 const char *property_value)
653 int approx_nr_strings;
655 /* get an estimate as to the number of strings by counting double
657 approx_nr_strings = 2;
658 for (chp = property_value; *chp; chp++)
663 approx_nr_strings = (approx_nr_strings) / 2;
665 /* create a string buffer for that many (plus a null) */
666 strings = (char**) zalloc ((approx_nr_strings + 1) * sizeof(char*));
668 /* now find all the strings */
669 chp = property_value;
674 /* skip leading space */
675 while (*chp != '\0' && isspace (*chp))
683 /* a quoted string - watch for '\' et.al. */
684 /* estimate the size and allocate space for it */
688 while (chp[pos] != '\0' && chp[pos] != '"')
690 if (chp[pos] == '\\' && chp[pos+1] != '\0')
695 strings[nr_strings] = zalloc (pos + 1);
696 /* copy the string over */
698 while (*chp != '\0' && *chp != '"')
700 if (*chp == '\\' && *(chp+1) != '\0') {
701 strings[nr_strings][pos] = *(chp+1);
707 strings[nr_strings][pos] = *chp;
714 strings[nr_strings][pos] = '\0';
718 /* copy over a single unquoted token */
720 while (chp[len] != '\0' && !isspace(chp[len]))
722 strings[nr_strings] = zalloc(len + 1);
723 strncpy(strings[nr_strings], chp, len);
724 strings[nr_strings][len] = '\0';
728 if (nr_strings > approx_nr_strings)
729 hw_abort (current, "String property %s badly formatted",
732 ASSERT (strings[nr_strings] == NULL); /* from zalloc */
736 hw_add_string_property (current, property_name, "");
737 else if (nr_strings == 1)
738 hw_add_string_property (current, property_name, strings[0]);
741 const char **specs = (const char**) strings; /* stop a bogus error */
742 hw_add_string_array_property (current, property_name,
746 /* flush the created string */
747 while (nr_strings > 0)
750 zfree (strings[nr_strings]);
756 /* <path-to-ihandle-device> */
760 parse_ihandle_property (struct hw *current,
761 const char *property,
764 ihandle_runtime_property_spec ihandle;
766 /* pass the full path */
767 ihandle.full_path = value;
769 /* save this ready for the ihandle create */
770 hw_add_ihandle_runtime_property (current, property,
777 hw_tree_create (SIM_DESC sd,
780 return hw_create (sd, NULL, family, family, NULL, NULL);
784 hw_tree_delete (struct hw *me)
786 /* Need to allow devices to disapear under our feet */
787 while (hw_child (me) != NULL)
789 hw_tree_delete (hw_child (me));
796 hw_tree_parse (struct hw *current,
802 current = hw_tree_vparse (current, fmt, ap);
808 hw_tree_vparse (struct hw *current,
812 char device_specifier[1024];
815 /* format the path */
816 vsprintf (device_specifier, fmt, ap);
817 if (strlen (device_specifier) >= sizeof (device_specifier))
818 hw_abort (NULL, "device_tree_add_deviced: buffer overflow\n");
820 /* construct the tree down to the final struct hw */
821 current = split_fill_path (current, device_specifier, &spec);
823 /* is there an interrupt spec */
824 if (spec.property == NULL
825 && spec.value != NULL)
827 char *op = split_value (&spec);
832 char *my_port_name = split_value (&spec);
834 char *dest_port_name = split_value (&spec);
836 name_specifier dest_spec;
837 char *dest_hw_name = split_value (&spec);
840 if (!hw_finished_p (current))
842 my_port = hw_port_decode (current, my_port_name, output_port);
843 /* find the dest device and port */
844 dest = split_fill_path (current, dest_hw_name, &dest_spec);
845 if (!hw_finished_p (dest))
847 dest_port = hw_port_decode (dest, dest_port_name,
849 /* connect the two */
850 hw_port_attach (current,
858 hw_abort (current, "unreconised interrupt spec %s\n", spec.value);
863 /* is there a property */
864 if (spec.property != NULL)
866 if (strcmp (spec.value, "true") == 0)
867 hw_add_boolean_property (current, spec.property, 1);
868 else if (strcmp (spec.value, "false") == 0)
869 hw_add_boolean_property (current, spec.property, 0);
872 const struct hw_property *property;
873 switch (spec.value[0])
878 parse_ihandle_property (current, spec.property, spec.value + 1);
884 unsigned8 words[1024];
885 char *curr = spec.value + 1;
890 words[nr_words] = H2BE_1 (strtoul (curr, &next, 0));
896 hw_add_array_property (current, spec.property,
897 words, sizeof(words[0]) * nr_words);
902 parse_string_property (current, spec.property, spec.value);
908 property = hw_tree_find_property (current, spec.value);
909 if (property == NULL)
910 hw_abort (current, "property %s not found\n", spec.value);
911 hw_add_duplicate_property (current,
918 if (strcmp (spec.property, "reg") == 0
919 || strcmp (spec.property, "assigned-addresses") == 0
920 || strcmp (spec.property, "alternate-reg") == 0)
922 parse_reg_property (current, spec.property, spec.value);
924 else if (strcmp (spec.property, "ranges") == 0)
926 parse_ranges_property (current, spec.property, spec.value);
928 else if (isdigit(spec.value[0])
929 || (spec.value[0] == '-' && isdigit(spec.value[1]))
930 || (spec.value[0] == '+' && isdigit(spec.value[1])))
932 parse_integer_property(current, spec.property, spec.value);
935 parse_string_property(current, spec.property, spec.value);
946 finish_hw_tree (struct hw *me,
949 if (!hw_finished_p (me))
954 hw_tree_finish (struct hw *root)
956 hw_tree_traverse (root, finish_hw_tree, NULL, NULL);
962 hw_tree_traverse (struct hw *root,
963 hw_tree_traverse_function *prefix,
964 hw_tree_traverse_function *postfix,
970 for (child = hw_child (root);
972 child = hw_sibling (child))
974 hw_tree_traverse (child, prefix, postfix, data);
977 postfix (root, data);
983 hw_tree_print_callback *print;
988 print_address (struct hw *bus,
993 hw_unit_encode (bus, phys, unit, sizeof(unit));
994 p->print (p->file, " %s", unit);
998 print_size (struct hw *bus,
1003 for (i = 0; i < size->nr_cells; i++)
1004 if (size->cells[i] != 0)
1006 if (i < size->nr_cells) {
1007 p->print (p->file, " 0x%lx", (unsigned long) size->cells[i]);
1009 for (; i < size->nr_cells; i++)
1010 p->print (p->file, ",0x%lx", (unsigned long) size->cells[i]);
1013 p->print (p->file, " 0");
1017 print_reg_property (struct hw *me,
1018 const struct hw_property *property,
1022 reg_property_spec reg;
1024 hw_find_reg_array_property (me, property->name, reg_nr, ®);
1026 print_address (hw_parent (me), ®.address, p);
1027 print_size (me, ®.size, p);
1032 print_ranges_property (struct hw *me,
1033 const struct hw_property *property,
1037 range_property_spec range;
1039 hw_find_range_array_property (me, property->name, range_nr, &range);
1042 print_address (me, &range.child_address, p);
1043 print_address (hw_parent (me), &range.parent_address, p);
1044 print_size (me, &range.size, p);
1049 print_string (struct hw *me,
1053 p->print (p->file, " \"");
1054 while (*string != '\0') {
1057 p->print (p->file, "\\\"");
1060 p->print (p->file, "\\\\");
1063 p->print (p->file, "%c", *string);
1068 p->print (p->file, "\"");
1072 print_string_array_property (struct hw *me,
1073 const struct hw_property *property,
1077 string_property_spec string;
1079 hw_find_string_array_property (me, property->name, nr, &string);
1082 print_string (me, string, p);
1087 print_properties (struct hw *me,
1090 const struct hw_property *property;
1091 for (property = hw_find_property (me, NULL);
1093 property = hw_next_property (property))
1095 if (hw_parent (me) == NULL)
1096 p->print (p->file, "/%s", property->name);
1098 p->print (p->file, "%s/%s", hw_path (me), property->name);
1099 if (property->original != NULL)
1101 p->print (p->file, " !");
1102 p->print (p->file, "%s/%s",
1103 hw_path (property->original->owner),
1104 property->original->name);
1108 switch (property->type)
1110 case array_property:
1112 if ((property->sizeof_array % sizeof (signed_cell)) == 0)
1114 unsigned_cell *w = (unsigned_cell*) property->array;
1117 cell_nr < (property->sizeof_array / sizeof (unsigned_cell));
1120 p->print (p->file, " 0x%lx", (unsigned long) BE2H_cell (w[cell_nr]));
1125 unsigned8 *w = (unsigned8*)property->array;
1126 p->print (p->file, " [");
1127 while ((char*)w - (char*)property->array < property->sizeof_array) {
1128 p->print (p->file, " 0x%2x", BE2H_1 (*w));
1134 case boolean_property:
1136 int b = hw_find_boolean_property(me, property->name);
1137 p->print (p->file, " %s", b ? "true" : "false");
1141 case ihandle_property:
1143 if (property->array != NULL)
1145 device_instance *instance = hw_find_ihandle_property (me, property->name);
1146 p->print (p->file, " *%s", device_instance_path(instance));
1150 /* not yet initialized, ask the device for the path */
1151 ihandle_runtime_property_spec spec;
1152 hw_find_ihandle_runtime_property (me, property->name, &spec);
1153 p->print (p->file, " *%s", spec.full_path);
1158 case integer_property:
1160 unsigned_word w = hw_find_integer_property (me, property->name);
1161 p->print (p->file, " 0x%lx", (unsigned long)w);
1164 case range_array_property:
1166 print_ranges_property (me, property, p);
1169 case reg_array_property:
1171 print_reg_property (me, property, p);
1174 case string_property:
1176 const char *s = hw_find_string_property (me, property->name);
1177 print_string (me, s, p);
1180 case string_array_property:
1182 print_string_array_property (me, property, p);
1187 p->print (p->file, "\n");
1192 print_interrupts (struct hw *me,
1198 struct printer *p = data;
1201 hw_port_encode (me, my_port, src, sizeof(src), output_port);
1202 hw_port_encode (dest, dest_port, dst, sizeof(dst), input_port);
1211 print_device (struct hw *me,
1214 struct printer *p = data;
1215 p->print (p->file, "%s\n", hw_path (me));
1216 print_properties (me, p);
1217 hw_port_traverse (me, print_interrupts, data);
1221 hw_tree_print (struct hw *root,
1222 hw_tree_print_callback *print,
1228 hw_tree_traverse (root,
1237 tree_instance(struct hw *root,
1238 const char *device_specifier)
1240 /* find the device node */
1242 name_specifier spec;
1243 if (!split_device_specifier(root, device_specifier, &spec))
1245 me = split_find_device(root, &spec);
1246 if (spec.name != NULL)
1248 /* create the instance */
1249 return device_create_instance(me, device_specifier, spec.last_args);
1254 hw_tree_find_device (struct hw *root,
1255 const char *path_to_device)
1258 name_specifier spec;
1260 /* parse the path */
1261 split_device_specifier (root, path_to_device, &spec);
1262 if (spec.value != NULL)
1263 return NULL; /* something wierd */
1266 node = split_find_device (root, &spec);
1267 if (spec.name != NULL)
1268 return NULL; /* not a leaf */
1274 const struct hw_property *
1275 hw_tree_find_property (struct hw *root,
1276 const char *path_to_property)
1278 name_specifier spec;
1279 if (!split_property_specifier (root, path_to_property, &spec))
1280 hw_abort (root, "Invalid property path %s", path_to_property);
1281 root = split_find_device (root, &spec);
1282 if (spec.name != NULL)
1283 return NULL; /* not a leaf */
1284 return hw_find_property (root, spec.property);
1288 hw_tree_find_boolean_property (struct hw *root,
1289 const char *path_to_property)
1291 name_specifier spec;
1292 if (!split_property_specifier (root, path_to_property, &spec))
1293 hw_abort (root, "Invalid property path %s", path_to_property);
1294 root = split_find_device (root, &spec);
1295 if (spec.name != NULL)
1296 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1297 spec.name, path_to_property);
1298 return hw_find_boolean_property (root, spec.property);
1302 hw_tree_find_integer_property (struct hw *root,
1303 const char *path_to_property)
1305 name_specifier spec;
1306 if (!split_property_specifier (root, path_to_property, &spec))
1307 hw_abort (root, "Invalid property path %s", path_to_property);
1308 root = split_find_device (root, &spec);
1309 if (spec.name != NULL)
1310 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1311 spec.name, path_to_property);
1312 return hw_find_integer_property (root, spec.property);
1317 hw_tree_find_ihandle_property (struct hw *root,
1318 const char *path_to_property)
1321 name_specifier spec;
1322 if (!split_property_specifier (root, path_to_property, &spec))
1323 hw_abort (root, "Invalid property path %s", path_to_property);
1324 root = split_find_device (root, &spec);
1325 if (spec.name != NULL)
1326 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1327 spec.name, path_to_property);
1328 return hw_find_ihandle_property (root, spec.property);
1333 hw_tree_find_string_property (struct hw *root,
1334 const char *path_to_property)
1336 name_specifier spec;
1337 if (!split_property_specifier (root, path_to_property, &spec))
1338 hw_abort (root, "Invalid property path %s", path_to_property);
1339 root = split_find_device (root, &spec);
1340 if (spec.name != NULL)
1341 hw_abort (root, "device \"%s\" not found (property \"%s\")",
1342 spec.name, path_to_property);
1343 return hw_find_string_property (root, spec.property);