1 /* The common simulator framework for GDB, the GNU Debugger.
3 Copyright 2002-2021 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. */
32 #include "hw-config.h"
37 const struct hw_descriptor
*descriptor
;
38 hw_delete_callback
*to_delete
;
42 generic_hw_unit_decode (struct hw
*bus
,
46 memset (phys
, 0, sizeof (*phys
));
52 const int max_nr_cells
= hw_unit_nr_address_cells (bus
);
57 val
= strtoul (unit
, &end
, 0);
62 if (nr_cells
>= max_nr_cells
)
65 phys
->cells
[nr_cells
] = val
;
69 if (isspace (*unit
) || *unit
== '\0')
75 if (nr_cells
< max_nr_cells
)
77 /* shift everything to correct position */
80 for (i
= 1; i
<= nr_cells
; i
++)
81 phys
->cells
[max_nr_cells
- i
] = phys
->cells
[nr_cells
- i
];
82 for (i
= 0; i
< (max_nr_cells
- nr_cells
); i
++)
85 phys
->nr_cells
= max_nr_cells
;
91 generic_hw_unit_encode (struct hw
*bus
,
99 /* skip leading zero's */
100 for (i
= 0; i
< phys
->nr_cells
; i
++)
102 if (phys
->cells
[i
] != 0)
105 /* don't output anything if empty */
106 if (phys
->nr_cells
== 0)
111 else if (i
== phys
->nr_cells
)
119 for (; i
< phys
->nr_cells
; i
++)
124 pos
= strchr (pos
, '\0');
126 if (phys
->cells
[i
] < 10)
127 sprintf (pos
, "%ld", (unsigned long)phys
->cells
[i
]);
129 sprintf (pos
, "0x%lx", (unsigned long)phys
->cells
[i
]);
130 pos
= strchr (pos
, '\0');
134 if (len
>= sizeof_buf
)
135 hw_abort (NULL
, "generic_unit_encode - buffer overflow\n");
140 generic_hw_unit_address_to_attach_address (struct hw
*me
,
141 const hw_unit
*address
,
143 unsigned_word
*attach_address
,
147 for (i
= 0; i
< address
->nr_cells
- 2; i
++)
149 if (address
->cells
[i
] != 0)
150 hw_abort (me
, "Only 32bit addresses supported");
152 if (address
->nr_cells
>= 2)
153 *attach_space
= address
->cells
[address
->nr_cells
- 2];
156 *attach_address
= address
->cells
[address
->nr_cells
- 1];
161 generic_hw_unit_size_to_attach_size (struct hw
*me
,
167 for (i
= 0; i
< size
->nr_cells
- 1; i
++)
169 if (size
->cells
[i
] != 0)
170 hw_abort (me
, "Only 32bit sizes supported");
172 *nr_bytes
= size
->cells
[0];
177 /* ignore/passthrough versions of each function */
180 passthrough_hw_attach_address (struct hw
*me
,
184 address_word nr_bytes
,
185 struct hw
*client
) /*callback/default*/
187 if (hw_parent (me
) == NULL
)
188 hw_abort (client
, "hw_attach_address: no parent attach method");
189 hw_attach_address (hw_parent (me
), level
,
190 space
, addr
, nr_bytes
,
195 passthrough_hw_detach_address (struct hw
*me
,
199 address_word nr_bytes
,
200 struct hw
*client
) /*callback/default*/
202 if (hw_parent (me
) == NULL
)
203 hw_abort (client
, "hw_attach_address: no parent attach method");
204 hw_detach_address (hw_parent (me
), level
,
205 space
, addr
, nr_bytes
,
210 panic_hw_io_read_buffer (struct hw
*me
,
216 hw_abort (me
, "no io-read method");
221 panic_hw_io_write_buffer (struct hw
*me
,
227 hw_abort (me
, "no io-write method");
232 passthrough_hw_dma_read_buffer (struct hw
*me
,
238 if (hw_parent (me
) == NULL
)
239 hw_abort (me
, "no parent dma-read method");
240 return hw_dma_read_buffer (hw_parent (me
), dest
,
241 space
, addr
, nr_bytes
);
245 passthrough_hw_dma_write_buffer (struct hw
*me
,
250 int violate_read_only_section
)
252 if (hw_parent (me
) == NULL
)
253 hw_abort (me
, "no parent dma-write method");
254 return hw_dma_write_buffer (hw_parent (me
), source
,
257 violate_read_only_section
);
261 ignore_hw_delete (struct hw
*me
)
270 full_name_of_hw (struct hw
*leaf
,
278 buf
= hw_malloc (leaf
, sizeof_buf
);
281 /* use head recursion to construct the path */
283 if (hw_parent (leaf
) == NULL
)
287 hw_abort (leaf
, "buffer overflow");
294 full_name_of_hw (hw_parent (leaf
), buf
, sizeof_buf
);
295 if (hw_unit_encode (hw_parent (leaf
),
296 hw_unit_address (leaf
),
303 if (strlen (buf
) + strlen ("/") + strlen (hw_name (leaf
)) + strlen (unit
)
305 hw_abort (leaf
, "buffer overflow");
307 strcat (buf
, hw_name (leaf
));
315 hw_create (struct sim_state
*sd
,
322 /* NOTE: HW must be allocated using ZALLOC, others use HW_ZALLOC */
323 struct hw
*hw
= ZALLOC (struct hw
);
326 hw
->family_of_hw
= hw_strdup (hw
, family
);
327 hw
->name_of_hw
= hw_strdup (hw
, name
);
328 hw
->args_of_hw
= hw_strdup (hw
, args
);
330 /* a hook into the system */
332 hw
->system_of_hw
= sd
;
333 else if (parent
!= NULL
)
334 hw
->system_of_hw
= hw_system (parent
);
336 hw_abort (parent
, "No system found");
341 struct hw
**sibling
= &parent
->child_of_hw
;
342 while ((*sibling
) != NULL
)
343 sibling
= &(*sibling
)->sibling_of_hw
;
345 hw
->parent_of_hw
= parent
;
351 struct hw
*root
= parent
;
352 while (root
->parent_of_hw
!= NULL
)
353 root
= root
->parent_of_hw
;
354 hw
->root_of_hw
= root
;
357 /* a unique identifier for the device on the parents bus */
360 hw_unit_decode (parent
, unit
, &hw
->unit_address_of_hw
);
363 /* Determine our path */
365 hw
->path_of_hw
= full_name_of_hw (hw
, NULL
, 0);
367 hw
->path_of_hw
= "/";
369 /* create our base type */
370 hw
->base_of_hw
= HW_ZALLOC (hw
, struct hw_base_data
);
371 hw
->base_of_hw
->finished_p
= 0;
374 set_hw_io_read_buffer (hw
, panic_hw_io_read_buffer
);
375 set_hw_io_write_buffer (hw
, panic_hw_io_write_buffer
);
376 set_hw_dma_read_buffer (hw
, passthrough_hw_dma_read_buffer
);
377 set_hw_dma_write_buffer (hw
, passthrough_hw_dma_write_buffer
);
378 set_hw_unit_decode (hw
, generic_hw_unit_decode
);
379 set_hw_unit_encode (hw
, generic_hw_unit_encode
);
380 set_hw_unit_address_to_attach_address (hw
, generic_hw_unit_address_to_attach_address
);
381 set_hw_unit_size_to_attach_size (hw
, generic_hw_unit_size_to_attach_size
);
382 set_hw_attach_address (hw
, passthrough_hw_attach_address
);
383 set_hw_detach_address (hw
, passthrough_hw_detach_address
);
384 set_hw_delete (hw
, ignore_hw_delete
);
386 /* locate a descriptor */
388 const struct hw_descriptor
**table
;
389 for (table
= hw_descriptors
;
393 const struct hw_descriptor
*entry
;
395 entry
->family
!= NULL
;
398 if (strcmp (family
, entry
->family
) == 0)
400 hw
->base_of_hw
->descriptor
= entry
;
405 if (hw
->base_of_hw
->descriptor
== NULL
)
407 hw_abort (parent
, "Unknown device `%s'", family
);
411 /* Attach dummy ports */
412 create_hw_alloc_data (hw
);
413 create_hw_property_data (hw
);
414 create_hw_port_data (hw
);
415 create_hw_event_data (hw
);
416 create_hw_handle_data (hw
);
417 create_hw_instance_data (hw
);
424 hw_finished_p (struct hw
*me
)
426 return (me
->base_of_hw
->finished_p
);
430 hw_finish (struct hw
*me
)
432 if (hw_finished_p (me
))
433 hw_abort (me
, "Attempt to finish finished device");
435 /* Fill in the (hopefully) defined address/size cells values */
436 if (hw_find_property (me
, "#address-cells") != NULL
)
437 me
->nr_address_cells_of_hw_unit
=
438 hw_find_integer_property (me
, "#address-cells");
440 me
->nr_address_cells_of_hw_unit
= 2;
441 if (hw_find_property (me
, "#size-cells") != NULL
)
442 me
->nr_size_cells_of_hw_unit
=
443 hw_find_integer_property (me
, "#size-cells");
445 me
->nr_size_cells_of_hw_unit
= 1;
447 /* Fill in the (hopefully) defined trace variable */
448 if (hw_find_property (me
, "trace?") != NULL
)
449 me
->trace_of_hw_p
= hw_find_boolean_property (me
, "trace?");
450 /* allow global variable to define default tracing */
451 else if (! hw_trace_p (me
)
452 && hw_find_property (hw_root (me
), "global-trace?") != NULL
453 && hw_find_boolean_property (hw_root (me
), "global-trace?"))
454 me
->trace_of_hw_p
= 1;
457 /* Allow the real device to override any methods */
458 me
->base_of_hw
->descriptor
->to_finish (me
);
459 me
->base_of_hw
->finished_p
= 1;
464 hw_delete (struct hw
*me
)
466 /* give the object a chance to tidy up */
467 me
->base_of_hw
->to_delete (me
);
469 delete_hw_instance_data (me
);
470 delete_hw_handle_data (me
);
471 delete_hw_event_data (me
);
472 delete_hw_port_data (me
);
473 delete_hw_property_data (me
);
475 /* now unlink us from the tree */
478 struct hw
**sibling
= &hw_parent (me
)->child_of_hw
;
479 while (*sibling
!= NULL
)
483 *sibling
= me
->sibling_of_hw
;
484 me
->sibling_of_hw
= NULL
;
485 me
->parent_of_hw
= NULL
;
491 /* some sanity checks */
492 if (hw_child (me
) != NULL
)
494 hw_abort (me
, "attempt to delete device with children");
496 if (hw_sibling (me
) != NULL
)
498 hw_abort (me
, "attempt to delete device with siblings");
501 /* blow away all memory belonging to the device */
502 delete_hw_alloc_data (me
);
509 set_hw_delete (struct hw
*hw
, hw_delete_callback method
)
511 hw
->base_of_hw
->to_delete
= method
;
515 /* Go through the devices various reg properties for those that
516 specify attach addresses */
520 do_hw_attach_regs (struct hw
*hw
)
522 static const char *(reg_property_names
[]) = {
524 "assigned-addresses",
529 const char **reg_property_name
;
530 int nr_valid_reg_properties
= 0;
531 for (reg_property_name
= reg_property_names
;
532 *reg_property_name
!= NULL
;
535 if (hw_find_property (hw
, *reg_property_name
) != NULL
)
537 reg_property_spec reg
;
540 hw_find_reg_array_property (hw
, *reg_property_name
, reg_entry
,
544 unsigned_word attach_address
;
546 unsigned attach_size
;
547 if (!hw_unit_address_to_attach_address (hw_parent (hw
),
553 if (!hw_unit_size_to_attach_size (hw_parent (hw
),
557 hw_attach_address (hw_parent (hw
),
559 attach_space
, attach_address
, attach_size
,
561 nr_valid_reg_properties
++;
563 /* if first option matches don't try for any others */
564 if (reg_property_name
== reg_property_names
)