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 3 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, see <http://www.gnu.org/licenses/>. */
41 #include "hw-config.h"
45 const struct hw_descriptor *descriptor;
46 hw_delete_callback *to_delete;
50 generic_hw_unit_decode (struct hw *bus,
54 memset (phys, 0, sizeof (*phys));
60 const int max_nr_cells = hw_unit_nr_address_cells (bus);
65 val = strtoul (unit, &end, 0);
70 if (nr_cells >= max_nr_cells)
73 phys->cells[nr_cells] = val;
77 if (isspace (*unit) || *unit == '\0')
83 if (nr_cells < max_nr_cells) {
84 /* shift everything to correct position */
86 for (i = 1; i <= nr_cells; i++)
87 phys->cells[max_nr_cells - i] = phys->cells[nr_cells - i];
88 for (i = 0; i < (max_nr_cells - nr_cells); i++)
91 phys->nr_cells = max_nr_cells;
97 generic_hw_unit_encode (struct hw *bus,
105 /* skip leading zero's */
106 for (i = 0; i < phys->nr_cells; i++)
108 if (phys->cells[i] != 0)
111 /* don't output anything if empty */
112 if (phys->nr_cells == 0)
117 else if (i == phys->nr_cells)
125 for (; i < phys->nr_cells; i++)
129 pos = strchr(pos, '\0');
131 if (phys->cells[i] < 10)
132 sprintf (pos, "%ld", (unsigned long)phys->cells[i]);
134 sprintf (pos, "0x%lx", (unsigned long)phys->cells[i]);
135 pos = strchr(pos, '\0');
139 if (len >= sizeof_buf)
140 hw_abort (NULL, "generic_unit_encode - buffer overflow\n");
145 generic_hw_unit_address_to_attach_address (struct hw *me,
146 const hw_unit *address,
148 unsigned_word *attach_address,
152 for (i = 0; i < address->nr_cells - 2; i++)
154 if (address->cells[i] != 0)
155 hw_abort (me, "Only 32bit addresses supported");
157 if (address->nr_cells >= 2)
158 *attach_space = address->cells[address->nr_cells - 2];
161 *attach_address = address->cells[address->nr_cells - 1];
166 generic_hw_unit_size_to_attach_size (struct hw *me,
172 for (i = 0; i < size->nr_cells - 1; i++)
174 if (size->cells[i] != 0)
175 hw_abort (me, "Only 32bit sizes supported");
177 *nr_bytes = size->cells[0];
182 /* ignore/passthrough versions of each function */
185 passthrough_hw_attach_address (struct hw *me,
189 address_word nr_bytes,
190 struct hw *client) /*callback/default*/
192 if (hw_parent (me) == NULL)
193 hw_abort (client, "hw_attach_address: no parent attach method");
194 hw_attach_address (hw_parent (me), level,
195 space, addr, nr_bytes,
200 passthrough_hw_detach_address (struct hw *me,
204 address_word nr_bytes,
205 struct hw *client) /*callback/default*/
207 if (hw_parent (me) == NULL)
208 hw_abort (client, "hw_attach_address: no parent attach method");
209 hw_detach_address (hw_parent (me), level,
210 space, addr, nr_bytes,
215 panic_hw_io_read_buffer (struct hw *me,
221 hw_abort (me, "no io-read method");
226 panic_hw_io_write_buffer (struct hw *me,
232 hw_abort (me, "no io-write method");
237 passthrough_hw_dma_read_buffer (struct hw *me,
243 if (hw_parent (me) == NULL)
244 hw_abort (me, "no parent dma-read method");
245 return hw_dma_read_buffer (hw_parent (me), dest,
246 space, addr, nr_bytes);
250 passthrough_hw_dma_write_buffer (struct hw *me,
255 int violate_read_only_section)
257 if (hw_parent (me) == NULL)
258 hw_abort (me, "no parent dma-write method");
259 return hw_dma_write_buffer (hw_parent (me), source,
262 violate_read_only_section);
266 ignore_hw_delete (struct hw *me)
275 full_name_of_hw (struct hw *leaf,
280 char full_name[1024];
284 sizeof_buf = sizeof (full_name);
287 /* use head recursion to construct the path */
289 if (hw_parent (leaf) == NULL)
293 hw_abort (leaf, "buffer overflow");
300 full_name_of_hw (hw_parent (leaf), buf, sizeof_buf);
301 if (hw_unit_encode (hw_parent (leaf),
302 hw_unit_address (leaf),
309 if (strlen (buf) + strlen ("/") + strlen (hw_name (leaf)) + strlen (unit)
311 hw_abort (leaf, "buffer overflow");
313 strcat (buf, hw_name (leaf));
317 /* return it usefully */
318 if (buf == full_name)
319 buf = hw_strdup (leaf, full_name);
324 hw_create (struct sim_state *sd,
331 /* NOTE: HW must be allocated using ZALLOC, others use HW_ZALLOC */
332 struct hw *hw = ZALLOC (struct hw);
335 hw->family_of_hw = hw_strdup (hw, family);
336 hw->name_of_hw = hw_strdup (hw, name);
337 hw->args_of_hw = hw_strdup (hw, args);
339 /* a hook into the system */
341 hw->system_of_hw = sd;
342 else if (parent != NULL)
343 hw->system_of_hw = hw_system (parent);
345 hw_abort (parent, "No system found");
350 struct hw **sibling = &parent->child_of_hw;
351 while ((*sibling) != NULL)
352 sibling = &(*sibling)->sibling_of_hw;
354 hw->parent_of_hw = parent;
360 struct hw *root = parent;
361 while (root->parent_of_hw != NULL)
362 root = root->parent_of_hw;
363 hw->root_of_hw = root;
366 /* a unique identifier for the device on the parents bus */
369 hw_unit_decode (parent, unit, &hw->unit_address_of_hw);
372 /* Determine our path */
374 hw->path_of_hw = full_name_of_hw (hw, NULL, 0);
376 hw->path_of_hw = "/";
378 /* create our base type */
379 hw->base_of_hw = HW_ZALLOC (hw, struct hw_base_data);
380 hw->base_of_hw->finished_p = 0;
383 set_hw_io_read_buffer (hw, panic_hw_io_read_buffer);
384 set_hw_io_write_buffer (hw, panic_hw_io_write_buffer);
385 set_hw_dma_read_buffer (hw, passthrough_hw_dma_read_buffer);
386 set_hw_dma_write_buffer (hw, passthrough_hw_dma_write_buffer);
387 set_hw_unit_decode (hw, generic_hw_unit_decode);
388 set_hw_unit_encode (hw, generic_hw_unit_encode);
389 set_hw_unit_address_to_attach_address (hw, generic_hw_unit_address_to_attach_address);
390 set_hw_unit_size_to_attach_size (hw, generic_hw_unit_size_to_attach_size);
391 set_hw_attach_address (hw, passthrough_hw_attach_address);
392 set_hw_detach_address (hw, passthrough_hw_detach_address);
393 set_hw_delete (hw, ignore_hw_delete);
395 /* locate a descriptor */
397 const struct hw_descriptor **table;
398 for (table = hw_descriptors;
402 const struct hw_descriptor *entry;
404 entry->family != NULL;
407 if (strcmp (family, entry->family) == 0)
409 hw->base_of_hw->descriptor = entry;
414 if (hw->base_of_hw->descriptor == NULL)
416 hw_abort (parent, "Unknown device `%s'", family);
420 /* Attach dummy ports */
421 create_hw_alloc_data (hw);
422 create_hw_property_data (hw);
423 create_hw_port_data (hw);
424 create_hw_event_data (hw);
425 create_hw_handle_data (hw);
426 create_hw_instance_data (hw);
433 hw_finished_p (struct hw *me)
435 return (me->base_of_hw->finished_p);
439 hw_finish (struct hw *me)
441 if (hw_finished_p (me))
442 hw_abort (me, "Attempt to finish finished device");
444 /* Fill in the (hopefully) defined address/size cells values */
445 if (hw_find_property (me, "#address-cells") != NULL)
446 me->nr_address_cells_of_hw_unit =
447 hw_find_integer_property (me, "#address-cells");
449 me->nr_address_cells_of_hw_unit = 2;
450 if (hw_find_property (me, "#size-cells") != NULL)
451 me->nr_size_cells_of_hw_unit =
452 hw_find_integer_property (me, "#size-cells");
454 me->nr_size_cells_of_hw_unit = 1;
456 /* Fill in the (hopefully) defined trace variable */
457 if (hw_find_property (me, "trace?") != NULL)
458 me->trace_of_hw_p = hw_find_boolean_property (me, "trace?");
459 /* allow global variable to define default tracing */
460 else if (! hw_trace_p (me)
461 && hw_find_property (hw_root (me), "global-trace?") != NULL
462 && hw_find_boolean_property (hw_root (me), "global-trace?"))
463 me->trace_of_hw_p = 1;
466 /* Allow the real device to override any methods */
467 me->base_of_hw->descriptor->to_finish (me);
468 me->base_of_hw->finished_p = 1;
473 hw_delete (struct hw *me)
475 /* give the object a chance to tidy up */
476 me->base_of_hw->to_delete (me);
478 delete_hw_instance_data (me);
479 delete_hw_handle_data (me);
480 delete_hw_event_data (me);
481 delete_hw_port_data (me);
482 delete_hw_property_data (me);
484 /* now unlink us from the tree */
487 struct hw **sibling = &hw_parent (me)->child_of_hw;
488 while (*sibling != NULL)
492 *sibling = me->sibling_of_hw;
493 me->sibling_of_hw = NULL;
494 me->parent_of_hw = NULL;
500 /* some sanity checks */
501 if (hw_child (me) != NULL)
503 hw_abort (me, "attempt to delete device with children");
505 if (hw_sibling (me) != NULL)
507 hw_abort (me, "attempt to delete device with siblings");
510 /* blow away all memory belonging to the device */
511 delete_hw_alloc_data (me);
518 set_hw_delete (struct hw *hw, hw_delete_callback method)
520 hw->base_of_hw->to_delete = method;
524 /* Go through the devices various reg properties for those that
525 specify attach addresses */
529 do_hw_attach_regs (struct hw *hw)
531 static const char *(reg_property_names[]) = {
533 "assigned-addresses",
538 const char **reg_property_name;
539 int nr_valid_reg_properties = 0;
540 for (reg_property_name = reg_property_names;
541 *reg_property_name != NULL;
544 if (hw_find_property (hw, *reg_property_name) != NULL)
546 reg_property_spec reg;
549 hw_find_reg_array_property (hw, *reg_property_name, reg_entry,
553 unsigned_word attach_address;
555 unsigned attach_size;
556 if (!hw_unit_address_to_attach_address (hw_parent (hw),
562 if (!hw_unit_size_to_attach_size (hw_parent (hw),
566 hw_attach_address (hw_parent (hw),
568 attach_space, attach_address, attach_size,
570 nr_valid_reg_properties++;
572 /* if first option matches don't try for any others */
573 if (reg_property_name == reg_property_names)