1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2022 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/>. */
22 /* This must come before any other includes. */
31 #include "hw-config.h"
36 const struct hw_descriptor *descriptor;
37 hw_delete_callback *to_delete;
41 generic_hw_unit_decode (struct hw *bus,
45 memset (phys, 0, sizeof (*phys));
51 const int max_nr_cells = hw_unit_nr_address_cells (bus);
56 val = strtoul (unit, &end, 0);
61 if (nr_cells >= max_nr_cells)
64 phys->cells[nr_cells] = val;
68 if (isspace (*unit) || *unit == '\0')
74 if (nr_cells < max_nr_cells)
76 /* shift everything to correct position */
79 for (i = 1; i <= nr_cells; i++)
80 phys->cells[max_nr_cells - i] = phys->cells[nr_cells - i];
81 for (i = 0; i < (max_nr_cells - nr_cells); i++)
84 phys->nr_cells = max_nr_cells;
90 generic_hw_unit_encode (struct hw *bus,
98 /* skip leading zero's */
99 for (i = 0; i < phys->nr_cells; i++)
101 if (phys->cells[i] != 0)
104 /* don't output anything if empty */
105 if (phys->nr_cells == 0)
110 else if (i == phys->nr_cells)
118 for (; i < phys->nr_cells; i++)
123 pos = strchr (pos, '\0');
125 if (phys->cells[i] < 10)
126 sprintf (pos, "%ld", (unsigned long)phys->cells[i]);
128 sprintf (pos, "0x%lx", (unsigned long)phys->cells[i]);
129 pos = strchr (pos, '\0');
133 if (len >= sizeof_buf)
134 hw_abort (NULL, "generic_unit_encode - buffer overflow\n");
139 generic_hw_unit_address_to_attach_address (struct hw *me,
140 const hw_unit *address,
142 unsigned_word *attach_address,
146 for (i = 0; i < address->nr_cells - 2; i++)
148 if (address->cells[i] != 0)
149 hw_abort (me, "Only 32bit addresses supported");
151 if (address->nr_cells >= 2)
152 *attach_space = address->cells[address->nr_cells - 2];
155 *attach_address = address->cells[address->nr_cells - 1];
160 generic_hw_unit_size_to_attach_size (struct hw *me,
166 for (i = 0; i < size->nr_cells - 1; i++)
168 if (size->cells[i] != 0)
169 hw_abort (me, "Only 32bit sizes supported");
171 *nr_bytes = size->cells[0];
176 /* ignore/passthrough versions of each function */
179 passthrough_hw_attach_address (struct hw *me,
183 address_word nr_bytes,
184 struct hw *client) /*callback/default*/
186 if (hw_parent (me) == NULL)
187 hw_abort (client, "hw_attach_address: no parent attach method");
188 hw_attach_address (hw_parent (me), level,
189 space, addr, nr_bytes,
194 passthrough_hw_detach_address (struct hw *me,
198 address_word nr_bytes,
199 struct hw *client) /*callback/default*/
201 if (hw_parent (me) == NULL)
202 hw_abort (client, "hw_attach_address: no parent attach method");
203 hw_detach_address (hw_parent (me), level,
204 space, addr, nr_bytes,
209 panic_hw_io_read_buffer (struct hw *me,
215 hw_abort (me, "no io-read method");
220 panic_hw_io_write_buffer (struct hw *me,
226 hw_abort (me, "no io-write method");
231 passthrough_hw_dma_read_buffer (struct hw *me,
237 if (hw_parent (me) == NULL)
238 hw_abort (me, "no parent dma-read method");
239 return hw_dma_read_buffer (hw_parent (me), dest,
240 space, addr, nr_bytes);
244 passthrough_hw_dma_write_buffer (struct hw *me,
249 int violate_read_only_section)
251 if (hw_parent (me) == NULL)
252 hw_abort (me, "no parent dma-write method");
253 return hw_dma_write_buffer (hw_parent (me), source,
256 violate_read_only_section);
260 ignore_hw_delete (struct hw *me)
269 full_name_of_hw (struct hw *leaf,
277 buf = hw_malloc (leaf, sizeof_buf);
280 /* use head recursion to construct the path */
282 if (hw_parent (leaf) == NULL)
286 hw_abort (leaf, "buffer overflow");
293 full_name_of_hw (hw_parent (leaf), buf, sizeof_buf);
294 if (hw_unit_encode (hw_parent (leaf),
295 hw_unit_address (leaf),
302 if (strlen (buf) + strlen ("/") + strlen (hw_name (leaf)) + strlen (unit)
304 hw_abort (leaf, "buffer overflow");
306 strcat (buf, hw_name (leaf));
314 hw_create (struct sim_state *sd,
321 /* NOTE: HW must be allocated using ZALLOC, others use HW_ZALLOC */
322 struct hw *hw = ZALLOC (struct hw);
325 hw->family_of_hw = hw_strdup (hw, family);
326 hw->name_of_hw = hw_strdup (hw, name);
327 hw->args_of_hw = hw_strdup (hw, args);
329 /* a hook into the system */
331 hw->system_of_hw = sd;
332 else if (parent != NULL)
333 hw->system_of_hw = hw_system (parent);
335 hw_abort (parent, "No system found");
340 struct hw **sibling = &parent->child_of_hw;
341 while ((*sibling) != NULL)
342 sibling = &(*sibling)->sibling_of_hw;
344 hw->parent_of_hw = parent;
350 struct hw *root = parent;
351 while (root->parent_of_hw != NULL)
352 root = root->parent_of_hw;
353 hw->root_of_hw = root;
356 /* a unique identifier for the device on the parents bus */
359 hw_unit_decode (parent, unit, &hw->unit_address_of_hw);
362 /* Determine our path */
364 hw->path_of_hw = full_name_of_hw (hw, NULL, 0);
366 hw->path_of_hw = "/";
368 /* create our base type */
369 hw->base_of_hw = HW_ZALLOC (hw, struct hw_base_data);
370 hw->base_of_hw->finished_p = 0;
373 set_hw_io_read_buffer (hw, panic_hw_io_read_buffer);
374 set_hw_io_write_buffer (hw, panic_hw_io_write_buffer);
375 set_hw_dma_read_buffer (hw, passthrough_hw_dma_read_buffer);
376 set_hw_dma_write_buffer (hw, passthrough_hw_dma_write_buffer);
377 set_hw_unit_decode (hw, generic_hw_unit_decode);
378 set_hw_unit_encode (hw, generic_hw_unit_encode);
379 set_hw_unit_address_to_attach_address (hw, generic_hw_unit_address_to_attach_address);
380 set_hw_unit_size_to_attach_size (hw, generic_hw_unit_size_to_attach_size);
381 set_hw_attach_address (hw, passthrough_hw_attach_address);
382 set_hw_detach_address (hw, passthrough_hw_detach_address);
383 set_hw_delete (hw, ignore_hw_delete);
385 /* locate a descriptor */
387 const struct hw_descriptor * const *table;
388 for (table = hw_descriptors;
392 const struct hw_descriptor *entry;
394 entry->family != NULL;
397 if (strcmp (family, entry->family) == 0)
399 hw->base_of_hw->descriptor = entry;
404 if (hw->base_of_hw->descriptor == NULL)
406 hw_abort (parent, "Unknown device `%s'", family);
410 /* Attach dummy ports */
411 create_hw_alloc_data (hw);
412 create_hw_property_data (hw);
413 create_hw_port_data (hw);
414 create_hw_event_data (hw);
415 create_hw_handle_data (hw);
416 create_hw_instance_data (hw);
423 hw_finished_p (struct hw *me)
425 return (me->base_of_hw->finished_p);
429 hw_finish (struct hw *me)
431 if (hw_finished_p (me))
432 hw_abort (me, "Attempt to finish finished device");
434 /* Fill in the (hopefully) defined address/size cells values */
435 if (hw_find_property (me, "#address-cells") != NULL)
436 me->nr_address_cells_of_hw_unit =
437 hw_find_integer_property (me, "#address-cells");
439 me->nr_address_cells_of_hw_unit = 2;
440 if (hw_find_property (me, "#size-cells") != NULL)
441 me->nr_size_cells_of_hw_unit =
442 hw_find_integer_property (me, "#size-cells");
444 me->nr_size_cells_of_hw_unit = 1;
446 /* Fill in the (hopefully) defined trace variable */
447 if (hw_find_property (me, "trace?") != NULL)
448 me->trace_of_hw_p = hw_find_boolean_property (me, "trace?");
449 /* allow global variable to define default tracing */
450 else if (! hw_trace_p (me)
451 && hw_find_property (hw_root (me), "global-trace?") != NULL
452 && hw_find_boolean_property (hw_root (me), "global-trace?"))
453 me->trace_of_hw_p = 1;
456 /* Allow the real device to override any methods */
457 me->base_of_hw->descriptor->to_finish (me);
458 me->base_of_hw->finished_p = 1;
463 hw_delete (struct hw *me)
465 /* give the object a chance to tidy up */
466 me->base_of_hw->to_delete (me);
468 delete_hw_instance_data (me);
469 delete_hw_handle_data (me);
470 delete_hw_event_data (me);
471 delete_hw_port_data (me);
472 delete_hw_property_data (me);
474 /* now unlink us from the tree */
477 struct hw **sibling = &hw_parent (me)->child_of_hw;
478 while (*sibling != NULL)
482 *sibling = me->sibling_of_hw;
483 me->sibling_of_hw = NULL;
484 me->parent_of_hw = NULL;
490 /* some sanity checks */
491 if (hw_child (me) != NULL)
493 hw_abort (me, "attempt to delete device with children");
495 if (hw_sibling (me) != NULL)
497 hw_abort (me, "attempt to delete device with siblings");
500 /* blow away all memory belonging to the device */
501 delete_hw_alloc_data (me);
508 set_hw_delete (struct hw *hw, hw_delete_callback method)
510 hw->base_of_hw->to_delete = method;
514 /* Go through the devices various reg properties for those that
515 specify attach addresses */
519 do_hw_attach_regs (struct hw *hw)
521 static const char *(reg_property_names[]) = {
523 "assigned-addresses",
528 const char **reg_property_name;
529 int nr_valid_reg_properties = 0;
530 for (reg_property_name = reg_property_names;
531 *reg_property_name != NULL;
534 if (hw_find_property (hw, *reg_property_name) != NULL)
536 reg_property_spec reg;
539 hw_find_reg_array_property (hw, *reg_property_name, reg_entry,
543 unsigned_word attach_address;
545 unsigned attach_size;
546 if (!hw_unit_address_to_attach_address (hw_parent (hw),
552 if (!hw_unit_size_to_attach_size (hw_parent (hw),
556 hw_attach_address (hw_parent (hw),
558 attach_space, attach_address, attach_size,
560 nr_valid_reg_properties++;
562 /* if first option matches don't try for any others */
563 if (reg_property_name == reg_property_names)