1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002, 2007 Free Software Foundation, Inc.
5 Contributed by Andrew Cagney and Red Hat.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA. */
43 #include "hw-config.h"
47 const struct hw_descriptor *descriptor;
48 hw_delete_callback *to_delete;
52 generic_hw_unit_decode (struct hw *bus,
56 memset (phys, 0, sizeof (*phys));
62 const int max_nr_cells = hw_unit_nr_address_cells (bus);
67 val = strtoul (unit, &end, 0);
72 if (nr_cells >= max_nr_cells)
75 phys->cells[nr_cells] = val;
79 if (isspace (*unit) || *unit == '\0')
85 if (nr_cells < max_nr_cells) {
86 /* shift everything to correct position */
88 for (i = 1; i <= nr_cells; i++)
89 phys->cells[max_nr_cells - i] = phys->cells[nr_cells - i];
90 for (i = 0; i < (max_nr_cells - nr_cells); i++)
93 phys->nr_cells = max_nr_cells;
99 generic_hw_unit_encode (struct hw *bus,
107 /* skip leading zero's */
108 for (i = 0; i < phys->nr_cells; i++)
110 if (phys->cells[i] != 0)
113 /* don't output anything if empty */
114 if (phys->nr_cells == 0)
119 else if (i == phys->nr_cells)
127 for (; i < phys->nr_cells; i++)
131 pos = strchr(pos, '\0');
133 if (phys->cells[i] < 10)
134 sprintf (pos, "%ld", (unsigned long)phys->cells[i]);
136 sprintf (pos, "0x%lx", (unsigned long)phys->cells[i]);
137 pos = strchr(pos, '\0');
141 if (len >= sizeof_buf)
142 hw_abort (NULL, "generic_unit_encode - buffer overflow\n");
147 generic_hw_unit_address_to_attach_address (struct hw *me,
148 const hw_unit *address,
150 unsigned_word *attach_address,
154 for (i = 0; i < address->nr_cells - 2; i++)
156 if (address->cells[i] != 0)
157 hw_abort (me, "Only 32bit addresses supported");
159 if (address->nr_cells >= 2)
160 *attach_space = address->cells[address->nr_cells - 2];
163 *attach_address = address->cells[address->nr_cells - 1];
168 generic_hw_unit_size_to_attach_size (struct hw *me,
174 for (i = 0; i < size->nr_cells - 1; i++)
176 if (size->cells[i] != 0)
177 hw_abort (me, "Only 32bit sizes supported");
179 *nr_bytes = size->cells[0];
184 /* ignore/passthrough versions of each function */
187 passthrough_hw_attach_address (struct hw *me,
191 address_word nr_bytes,
192 struct hw *client) /*callback/default*/
194 if (hw_parent (me) == NULL)
195 hw_abort (client, "hw_attach_address: no parent attach method");
196 hw_attach_address (hw_parent (me), level,
197 space, addr, nr_bytes,
202 passthrough_hw_detach_address (struct hw *me,
206 address_word nr_bytes,
207 struct hw *client) /*callback/default*/
209 if (hw_parent (me) == NULL)
210 hw_abort (client, "hw_attach_address: no parent attach method");
211 hw_detach_address (hw_parent (me), level,
212 space, addr, nr_bytes,
217 panic_hw_io_read_buffer (struct hw *me,
223 hw_abort (me, "no io-read method");
228 panic_hw_io_write_buffer (struct hw *me,
234 hw_abort (me, "no io-write method");
239 passthrough_hw_dma_read_buffer (struct hw *me,
245 if (hw_parent (me) == NULL)
246 hw_abort (me, "no parent dma-read method");
247 return hw_dma_read_buffer (hw_parent (me), dest,
248 space, addr, nr_bytes);
252 passthrough_hw_dma_write_buffer (struct hw *me,
257 int violate_read_only_section)
259 if (hw_parent (me) == NULL)
260 hw_abort (me, "no parent dma-write method");
261 return hw_dma_write_buffer (hw_parent (me), source,
264 violate_read_only_section);
268 ignore_hw_delete (struct hw *me)
277 full_name_of_hw (struct hw *leaf,
282 char full_name[1024];
286 sizeof_buf = sizeof (full_name);
289 /* use head recursion to construct the path */
291 if (hw_parent (leaf) == NULL)
295 hw_abort (leaf, "buffer overflow");
302 full_name_of_hw (hw_parent (leaf), buf, sizeof_buf);
303 if (hw_unit_encode (hw_parent (leaf),
304 hw_unit_address (leaf),
311 if (strlen (buf) + strlen ("/") + strlen (hw_name (leaf)) + strlen (unit)
313 hw_abort (leaf, "buffer overflow");
315 strcat (buf, hw_name (leaf));
319 /* return it usefully */
320 if (buf == full_name)
321 buf = hw_strdup (leaf, full_name);
326 hw_create (struct sim_state *sd,
333 /* NOTE: HW must be allocated using ZALLOC, others use HW_ZALLOC */
334 struct hw *hw = ZALLOC (struct hw);
337 hw->family_of_hw = hw_strdup (hw, family);
338 hw->name_of_hw = hw_strdup (hw, name);
339 hw->args_of_hw = hw_strdup (hw, args);
341 /* a hook into the system */
343 hw->system_of_hw = sd;
344 else if (parent != NULL)
345 hw->system_of_hw = hw_system (parent);
347 hw_abort (parent, "No system found");
352 struct hw **sibling = &parent->child_of_hw;
353 while ((*sibling) != NULL)
354 sibling = &(*sibling)->sibling_of_hw;
356 hw->parent_of_hw = parent;
362 struct hw *root = parent;
363 while (root->parent_of_hw != NULL)
364 root = root->parent_of_hw;
365 hw->root_of_hw = root;
368 /* a unique identifier for the device on the parents bus */
371 hw_unit_decode (parent, unit, &hw->unit_address_of_hw);
374 /* Determine our path */
376 hw->path_of_hw = full_name_of_hw (hw, NULL, 0);
378 hw->path_of_hw = "/";
380 /* create our base type */
381 hw->base_of_hw = HW_ZALLOC (hw, struct hw_base_data);
382 hw->base_of_hw->finished_p = 0;
385 set_hw_io_read_buffer (hw, panic_hw_io_read_buffer);
386 set_hw_io_write_buffer (hw, panic_hw_io_write_buffer);
387 set_hw_dma_read_buffer (hw, passthrough_hw_dma_read_buffer);
388 set_hw_dma_write_buffer (hw, passthrough_hw_dma_write_buffer);
389 set_hw_unit_decode (hw, generic_hw_unit_decode);
390 set_hw_unit_encode (hw, generic_hw_unit_encode);
391 set_hw_unit_address_to_attach_address (hw, generic_hw_unit_address_to_attach_address);
392 set_hw_unit_size_to_attach_size (hw, generic_hw_unit_size_to_attach_size);
393 set_hw_attach_address (hw, passthrough_hw_attach_address);
394 set_hw_detach_address (hw, passthrough_hw_detach_address);
395 set_hw_delete (hw, ignore_hw_delete);
397 /* locate a descriptor */
399 const struct hw_descriptor **table;
400 for (table = hw_descriptors;
404 const struct hw_descriptor *entry;
406 entry->family != NULL;
409 if (strcmp (family, entry->family) == 0)
411 hw->base_of_hw->descriptor = entry;
416 if (hw->base_of_hw->descriptor == NULL)
418 hw_abort (parent, "Unknown device `%s'", family);
422 /* Attach dummy ports */
423 create_hw_alloc_data (hw);
424 create_hw_property_data (hw);
425 create_hw_port_data (hw);
426 create_hw_event_data (hw);
427 create_hw_handle_data (hw);
428 create_hw_instance_data (hw);
435 hw_finished_p (struct hw *me)
437 return (me->base_of_hw->finished_p);
441 hw_finish (struct hw *me)
443 if (hw_finished_p (me))
444 hw_abort (me, "Attempt to finish finished device");
446 /* Fill in the (hopefully) defined address/size cells values */
447 if (hw_find_property (me, "#address-cells") != NULL)
448 me->nr_address_cells_of_hw_unit =
449 hw_find_integer_property (me, "#address-cells");
451 me->nr_address_cells_of_hw_unit = 2;
452 if (hw_find_property (me, "#size-cells") != NULL)
453 me->nr_size_cells_of_hw_unit =
454 hw_find_integer_property (me, "#size-cells");
456 me->nr_size_cells_of_hw_unit = 1;
458 /* Fill in the (hopefully) defined trace variable */
459 if (hw_find_property (me, "trace?") != NULL)
460 me->trace_of_hw_p = hw_find_boolean_property (me, "trace?");
461 /* allow global variable to define default tracing */
462 else if (! hw_trace_p (me)
463 && hw_find_property (hw_root (me), "global-trace?") != NULL
464 && hw_find_boolean_property (hw_root (me), "global-trace?"))
465 me->trace_of_hw_p = 1;
468 /* Allow the real device to override any methods */
469 me->base_of_hw->descriptor->to_finish (me);
470 me->base_of_hw->finished_p = 1;
475 hw_delete (struct hw *me)
477 /* give the object a chance to tidy up */
478 me->base_of_hw->to_delete (me);
480 delete_hw_instance_data (me);
481 delete_hw_handle_data (me);
482 delete_hw_event_data (me);
483 delete_hw_port_data (me);
484 delete_hw_property_data (me);
486 /* now unlink us from the tree */
489 struct hw **sibling = &hw_parent (me)->child_of_hw;
490 while (*sibling != NULL)
494 *sibling = me->sibling_of_hw;
495 me->sibling_of_hw = NULL;
496 me->parent_of_hw = NULL;
502 /* some sanity checks */
503 if (hw_child (me) != NULL)
505 hw_abort (me, "attempt to delete device with children");
507 if (hw_sibling (me) != NULL)
509 hw_abort (me, "attempt to delete device with siblings");
512 /* blow away all memory belonging to the device */
513 delete_hw_alloc_data (me);
520 set_hw_delete (struct hw *hw, hw_delete_callback method)
522 hw->base_of_hw->to_delete = method;
526 /* Go through the devices various reg properties for those that
527 specify attach addresses */
531 do_hw_attach_regs (struct hw *hw)
533 static const char *(reg_property_names[]) = {
535 "assigned-addresses",
540 const char **reg_property_name;
541 int nr_valid_reg_properties = 0;
542 for (reg_property_name = reg_property_names;
543 *reg_property_name != NULL;
546 if (hw_find_property (hw, *reg_property_name) != NULL)
548 reg_property_spec reg;
551 hw_find_reg_array_property (hw, *reg_property_name, reg_entry,
555 unsigned_word attach_address;
557 unsigned attach_size;
558 if (!hw_unit_address_to_attach_address (hw_parent (hw),
564 if (!hw_unit_size_to_attach_size (hw_parent (hw),
568 hw_attach_address (hw_parent (hw),
570 attach_space, attach_address, attach_size,
572 nr_valid_reg_properties++;
574 /* if first option matches don't try for any others */
575 if (reg_property_name == reg_property_names)