1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2018 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"
46 const struct hw_descriptor *descriptor;
47 hw_delete_callback *to_delete;
51 generic_hw_unit_decode (struct hw *bus,
55 memset (phys, 0, sizeof (*phys));
61 const int max_nr_cells = hw_unit_nr_address_cells (bus);
66 val = strtoul (unit, &end, 0);
71 if (nr_cells >= max_nr_cells)
74 phys->cells[nr_cells] = val;
78 if (isspace (*unit) || *unit == '\0')
84 if (nr_cells < max_nr_cells)
86 /* shift everything to correct position */
89 for (i = 1; i <= nr_cells; i++)
90 phys->cells[max_nr_cells - i] = phys->cells[nr_cells - i];
91 for (i = 0; i < (max_nr_cells - nr_cells); i++)
94 phys->nr_cells = max_nr_cells;
100 generic_hw_unit_encode (struct hw *bus,
108 /* skip leading zero's */
109 for (i = 0; i < phys->nr_cells; i++)
111 if (phys->cells[i] != 0)
114 /* don't output anything if empty */
115 if (phys->nr_cells == 0)
120 else if (i == phys->nr_cells)
128 for (; i < phys->nr_cells; i++)
133 pos = strchr (pos, '\0');
135 if (phys->cells[i] < 10)
136 sprintf (pos, "%ld", (unsigned long)phys->cells[i]);
138 sprintf (pos, "0x%lx", (unsigned long)phys->cells[i]);
139 pos = strchr (pos, '\0');
143 if (len >= sizeof_buf)
144 hw_abort (NULL, "generic_unit_encode - buffer overflow\n");
149 generic_hw_unit_address_to_attach_address (struct hw *me,
150 const hw_unit *address,
152 unsigned_word *attach_address,
156 for (i = 0; i < address->nr_cells - 2; i++)
158 if (address->cells[i] != 0)
159 hw_abort (me, "Only 32bit addresses supported");
161 if (address->nr_cells >= 2)
162 *attach_space = address->cells[address->nr_cells - 2];
165 *attach_address = address->cells[address->nr_cells - 1];
170 generic_hw_unit_size_to_attach_size (struct hw *me,
176 for (i = 0; i < size->nr_cells - 1; i++)
178 if (size->cells[i] != 0)
179 hw_abort (me, "Only 32bit sizes supported");
181 *nr_bytes = size->cells[0];
186 /* ignore/passthrough versions of each function */
189 passthrough_hw_attach_address (struct hw *me,
193 address_word nr_bytes,
194 struct hw *client) /*callback/default*/
196 if (hw_parent (me) == NULL)
197 hw_abort (client, "hw_attach_address: no parent attach method");
198 hw_attach_address (hw_parent (me), level,
199 space, addr, nr_bytes,
204 passthrough_hw_detach_address (struct hw *me,
208 address_word nr_bytes,
209 struct hw *client) /*callback/default*/
211 if (hw_parent (me) == NULL)
212 hw_abort (client, "hw_attach_address: no parent attach method");
213 hw_detach_address (hw_parent (me), level,
214 space, addr, nr_bytes,
219 panic_hw_io_read_buffer (struct hw *me,
225 hw_abort (me, "no io-read method");
230 panic_hw_io_write_buffer (struct hw *me,
236 hw_abort (me, "no io-write method");
241 passthrough_hw_dma_read_buffer (struct hw *me,
247 if (hw_parent (me) == NULL)
248 hw_abort (me, "no parent dma-read method");
249 return hw_dma_read_buffer (hw_parent (me), dest,
250 space, addr, nr_bytes);
254 passthrough_hw_dma_write_buffer (struct hw *me,
259 int violate_read_only_section)
261 if (hw_parent (me) == NULL)
262 hw_abort (me, "no parent dma-write method");
263 return hw_dma_write_buffer (hw_parent (me), source,
266 violate_read_only_section);
270 ignore_hw_delete (struct hw *me)
279 full_name_of_hw (struct hw *leaf,
284 char full_name[1024];
288 sizeof_buf = sizeof (full_name);
291 /* use head recursion to construct the path */
293 if (hw_parent (leaf) == NULL)
297 hw_abort (leaf, "buffer overflow");
304 full_name_of_hw (hw_parent (leaf), buf, sizeof_buf);
305 if (hw_unit_encode (hw_parent (leaf),
306 hw_unit_address (leaf),
313 if (strlen (buf) + strlen ("/") + strlen (hw_name (leaf)) + strlen (unit)
315 hw_abort (leaf, "buffer overflow");
317 strcat (buf, hw_name (leaf));
321 /* return it usefully */
322 if (buf == full_name)
323 buf = hw_strdup (leaf, full_name);
328 hw_create (struct sim_state *sd,
335 /* NOTE: HW must be allocated using ZALLOC, others use HW_ZALLOC */
336 struct hw *hw = ZALLOC (struct hw);
339 hw->family_of_hw = hw_strdup (hw, family);
340 hw->name_of_hw = hw_strdup (hw, name);
341 hw->args_of_hw = hw_strdup (hw, args);
343 /* a hook into the system */
345 hw->system_of_hw = sd;
346 else if (parent != NULL)
347 hw->system_of_hw = hw_system (parent);
349 hw_abort (parent, "No system found");
354 struct hw **sibling = &parent->child_of_hw;
355 while ((*sibling) != NULL)
356 sibling = &(*sibling)->sibling_of_hw;
358 hw->parent_of_hw = parent;
364 struct hw *root = parent;
365 while (root->parent_of_hw != NULL)
366 root = root->parent_of_hw;
367 hw->root_of_hw = root;
370 /* a unique identifier for the device on the parents bus */
373 hw_unit_decode (parent, unit, &hw->unit_address_of_hw);
376 /* Determine our path */
378 hw->path_of_hw = full_name_of_hw (hw, NULL, 0);
380 hw->path_of_hw = "/";
382 /* create our base type */
383 hw->base_of_hw = HW_ZALLOC (hw, struct hw_base_data);
384 hw->base_of_hw->finished_p = 0;
387 set_hw_io_read_buffer (hw, panic_hw_io_read_buffer);
388 set_hw_io_write_buffer (hw, panic_hw_io_write_buffer);
389 set_hw_dma_read_buffer (hw, passthrough_hw_dma_read_buffer);
390 set_hw_dma_write_buffer (hw, passthrough_hw_dma_write_buffer);
391 set_hw_unit_decode (hw, generic_hw_unit_decode);
392 set_hw_unit_encode (hw, generic_hw_unit_encode);
393 set_hw_unit_address_to_attach_address (hw, generic_hw_unit_address_to_attach_address);
394 set_hw_unit_size_to_attach_size (hw, generic_hw_unit_size_to_attach_size);
395 set_hw_attach_address (hw, passthrough_hw_attach_address);
396 set_hw_detach_address (hw, passthrough_hw_detach_address);
397 set_hw_delete (hw, ignore_hw_delete);
399 /* locate a descriptor */
401 const struct hw_descriptor **table;
402 for (table = hw_descriptors;
406 const struct hw_descriptor *entry;
408 entry->family != NULL;
411 if (strcmp (family, entry->family) == 0)
413 hw->base_of_hw->descriptor = entry;
418 if (hw->base_of_hw->descriptor == NULL)
420 hw_abort (parent, "Unknown device `%s'", family);
424 /* Attach dummy ports */
425 create_hw_alloc_data (hw);
426 create_hw_property_data (hw);
427 create_hw_port_data (hw);
428 create_hw_event_data (hw);
429 create_hw_handle_data (hw);
430 create_hw_instance_data (hw);
437 hw_finished_p (struct hw *me)
439 return (me->base_of_hw->finished_p);
443 hw_finish (struct hw *me)
445 if (hw_finished_p (me))
446 hw_abort (me, "Attempt to finish finished device");
448 /* Fill in the (hopefully) defined address/size cells values */
449 if (hw_find_property (me, "#address-cells") != NULL)
450 me->nr_address_cells_of_hw_unit =
451 hw_find_integer_property (me, "#address-cells");
453 me->nr_address_cells_of_hw_unit = 2;
454 if (hw_find_property (me, "#size-cells") != NULL)
455 me->nr_size_cells_of_hw_unit =
456 hw_find_integer_property (me, "#size-cells");
458 me->nr_size_cells_of_hw_unit = 1;
460 /* Fill in the (hopefully) defined trace variable */
461 if (hw_find_property (me, "trace?") != NULL)
462 me->trace_of_hw_p = hw_find_boolean_property (me, "trace?");
463 /* allow global variable to define default tracing */
464 else if (! hw_trace_p (me)
465 && hw_find_property (hw_root (me), "global-trace?") != NULL
466 && hw_find_boolean_property (hw_root (me), "global-trace?"))
467 me->trace_of_hw_p = 1;
470 /* Allow the real device to override any methods */
471 me->base_of_hw->descriptor->to_finish (me);
472 me->base_of_hw->finished_p = 1;
477 hw_delete (struct hw *me)
479 /* give the object a chance to tidy up */
480 me->base_of_hw->to_delete (me);
482 delete_hw_instance_data (me);
483 delete_hw_handle_data (me);
484 delete_hw_event_data (me);
485 delete_hw_port_data (me);
486 delete_hw_property_data (me);
488 /* now unlink us from the tree */
491 struct hw **sibling = &hw_parent (me)->child_of_hw;
492 while (*sibling != NULL)
496 *sibling = me->sibling_of_hw;
497 me->sibling_of_hw = NULL;
498 me->parent_of_hw = NULL;
504 /* some sanity checks */
505 if (hw_child (me) != NULL)
507 hw_abort (me, "attempt to delete device with children");
509 if (hw_sibling (me) != NULL)
511 hw_abort (me, "attempt to delete device with siblings");
514 /* blow away all memory belonging to the device */
515 delete_hw_alloc_data (me);
522 set_hw_delete (struct hw *hw, hw_delete_callback method)
524 hw->base_of_hw->to_delete = method;
528 /* Go through the devices various reg properties for those that
529 specify attach addresses */
533 do_hw_attach_regs (struct hw *hw)
535 static const char *(reg_property_names[]) = {
537 "assigned-addresses",
542 const char **reg_property_name;
543 int nr_valid_reg_properties = 0;
544 for (reg_property_name = reg_property_names;
545 *reg_property_name != NULL;
548 if (hw_find_property (hw, *reg_property_name) != NULL)
550 reg_property_spec reg;
553 hw_find_reg_array_property (hw, *reg_property_name, reg_entry,
557 unsigned_word attach_address;
559 unsigned attach_size;
560 if (!hw_unit_address_to_attach_address (hw_parent (hw),
566 if (!hw_unit_size_to_attach_size (hw_parent (hw),
570 hw_attach_address (hw_parent (hw),
572 attach_space, attach_address, attach_size,
574 nr_valid_reg_properties++;
576 /* if first option matches don't try for any others */
577 if (reg_property_name == reg_property_names)