Home | History | Annotate | Line # | Download | only in common
hw-device.h revision 1.1.1.3
      1 /* The common simulator framework for GDB, the GNU Debugger.
      2 
      3    Copyright 2002-2016 Free Software Foundation, Inc.
      4 
      5    Contributed by Andrew Cagney and Red Hat.
      6 
      7    This file is part of GDB.
      8 
      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.
     13 
     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.
     18 
     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/>.  */
     21 
     22 
     23 #ifndef HW_DEVICE_H
     24 #define HW_DEVICE_H
     25 
     26 
     27 /* Introduction:
     28 
     29    As explained in earlier sections, the device, device instance,
     30    property and ports lie at the heart of PSIM's device model.
     31 
     32    In the below a synopsis of the device object and the operations it
     33    supports are given.
     34    */
     35 
     36 
     37 /* Creation:
     38 
     39    The devices are created using a sequence of steps.  In particular:
     40 
     41 	o	A tree framework is created.
     42 
     43 		At this point, properties can be modified and extra
     44 		devices inserted (or removed?).
     45 
     46 #if LATER
     47 
     48 		Any properties that have a run-time value (eg ihandle
     49 		or device instance pointer properties) are entered
     50 		into the device tree using a named reference to the
     51 		corresponding runtime object that is to be created.
     52 
     53 #endif
     54 
     55 	o	Real devices are created for all the dummy devices.
     56 
     57 		A device can assume that all of its parents have been
     58 		initialized.
     59 
     60 		A device can assume that all non run-time properties
     61 		have been initialized.
     62 
     63 		As part of being created, the device normally attaches
     64 		itself to its parent bus.
     65 
     66 #if LATER
     67 
     68 		Device instance data is initialized.
     69 
     70 #endif
     71 
     72 #if LATER
     73 
     74 	o	Any run-time properties are created.
     75 
     76 #endif
     77 
     78 #if MUCH_MUCH_LATER
     79 
     80 	o	Some devices, as part of their initialization
     81 		might want to refer to ihandle properties
     82 		in the device tree.
     83 
     84 #endif
     85 
     86    NOTES:
     87 
     88 	o	It is important to separate the creation
     89 		of an actual device from the creation
     90 		of the tree.  The alternative creating
     91 		the device in two stages: As a separate
     92 		entity and then as a part of the tree.
     93 
     94 #if LATER
     95 	o	Run-time properties can not be created
     96 		until after the devices in the tree
     97 		have been created.  Hence an extra pass
     98 		for handling them.
     99 #endif
    100 
    101    */
    102 
    103 /* Relationships:
    104 
    105    A device is able to determine its relationship to other devices
    106    within the tree.  Operations include querying for a devices parent,
    107    sibling, child, name, and path (from the root).
    108 
    109    */
    110 
    111 
    112 #define hw_parent(hw) ((hw)->parent_of_hw + 0)
    113 
    114 #define hw_sibling(hw) ((hw)->sibling_of_hw + 0)
    115 
    116 #define hw_child(hw) ((hw)->child_of_hw + 0)
    117 
    118 
    119 
    120 /* Herritage:
    121 
    122  */
    123 
    124 #define hw_family(hw) ((hw)->family_of_hw + 0)
    125 
    126 #define hw_name(hw) ((hw)->name_of_hw + 0)
    127 
    128 #define hw_args(hw) ((hw)->args_of_hw + 0)
    129 
    130 #define hw_path(hw) ((hw)->path_of_hw + 0)
    131 
    132 
    133 
    134 /* Short cut to the root node of the tree */
    135 
    136 #define hw_root(hw) ((hw)->root_of_hw + 0)
    137 
    138 /* Short cut back to the simulator object */
    139 
    140 #define hw_system(hw) ((hw)->system_of_hw)
    141 
    142 /* For requests initiated by a CPU the cpu that initiated the request */
    143 
    144 struct _sim_cpu *hw_system_cpu (struct hw *hw);
    145 
    146 
    147 /* Device private data */
    148 
    149 #define hw_data(hw) ((hw)->data_of_hw)
    150 
    151 #define set_hw_data(hw, value) \
    152 ((hw)->data_of_hw = (value))
    153 
    154 
    155 
    156 /* Perform a soft reset of the device */
    158 
    159 typedef unsigned (hw_reset_method)
    160      (struct hw *me);
    161 
    162 #define hw_reset(hw) ((hw)->to_reset (hw))
    163 
    164 #define set_hw_reset(hw, method) \
    165 ((hw)->to_reset = method)
    166 
    167 
    168 /* Hardware operations:
    170 
    171    Connecting a parent to its children is a common bus. The parent
    172    node is described as the bus owner and is responisble for
    173    co-ordinating bus operations. On the bus, a SPACE:ADDR pair is used
    174    to specify an address.  A device that is both a bus owner (parent)
    175    and bus client (child) are referred to as a bridging device.
    176 
    177    A child performing a data (DMA) transfer will pass its request to
    178    the bus owner (the devices parent).  The bus owner will then either
    179    reflect the request to one of the other devices attached to the bus
    180    (a child of the bus owner) or bridge the request up the tree to the
    181    next bus. */
    182 
    183 
    184 /* Children attached to a bus can register (attach) themselves to
    185    specific addresses on their attached bus.
    186 
    187    (A device may also be implicitly attached to certain bus
    188    addresses).
    189 
    190    The SPACE:ADDR pair specify an address on the common bus that
    191    connects the parent and child devices. */
    192 
    193 typedef void (hw_attach_address_method)
    194      (struct hw *me,
    195       int level,
    196       int space,
    197       address_word addr,
    198       address_word nr_bytes,
    199       struct hw *client); /*callback/default*/
    200 
    201 #define hw_attach_address(me, level, space, addr, nr_bytes, client) \
    202 ((me)->to_attach_address (me, level, space, addr, nr_bytes, client))
    203 
    204 #define set_hw_attach_address(hw, method) \
    205 ((hw)->to_attach_address = (method))
    206 
    207 typedef void (hw_detach_address_method)
    208      (struct hw *me,
    209       int level,
    210       int space,
    211       address_word addr,
    212       address_word nr_bytes,
    213       struct hw *client); /*callback/default*/
    214 
    215 #define hw_detach_address(me, level, space, addr, nr_bytes, client) \
    216 ((me)->to_detach_address (me, level, space, addr, nr_bytes, client))
    217 
    218 #define set_hw_detach_address(hw, method) \
    219 ((hw)->to_detach_address = (method))
    220 
    221 
    222 /* An IO operation from a parent to a child via the conecting bus.
    223 
    224    The SPACE:ADDR pair specify an address on the bus shared between
    225    the parent and child devices. */
    226 
    227 typedef unsigned (hw_io_read_buffer_method)
    228      (struct hw *me,
    229       void *dest,
    230       int space,
    231       unsigned_word addr,
    232       unsigned nr_bytes);
    233 
    234 #define hw_io_read_buffer(hw, dest, space, addr, nr_bytes) \
    235 ((hw)->to_io_read_buffer (hw, dest, space, addr, nr_bytes))
    236 
    237 #define set_hw_io_read_buffer(hw, method) \
    238 ((hw)->to_io_read_buffer = (method))
    239 
    240 typedef unsigned (hw_io_write_buffer_method)
    241      (struct hw *me,
    242       const void *source,
    243       int space,
    244       unsigned_word addr,
    245       unsigned nr_bytes);
    246 
    247 #define hw_io_write_buffer(hw, src, space, addr, nr_bytes) \
    248 ((hw)->to_io_write_buffer (hw, src, space, addr, nr_bytes))
    249 
    250 #define set_hw_io_write_buffer(hw, method) \
    251 ((hw)->to_io_write_buffer = (method))
    252 
    253 
    254 /* Conversly, the device pci1000,1@1 may need to perform a dma transfer
    255    into the cpu/memory core.  Just as I/O moves towards the leaves,
    256    dma transfers move towards the core via the initiating devices
    257    parent nodes.  The root device (special) converts the DMA transfer
    258    into reads/writes to memory.
    259 
    260    The SPACE:ADDR pair specify an address on the common bus connecting
    261    the parent and child devices. */
    262 
    263 typedef unsigned (hw_dma_read_buffer_method)
    264      (struct hw *bus,
    265       void *dest,
    266       int space,
    267       unsigned_word addr,
    268       unsigned nr_bytes);
    269 
    270 #define hw_dma_read_buffer(bus, dest, space, addr, nr_bytes) \
    271 ((bus)->to_dma_read_buffer (bus, dest, space, addr, nr_bytes))
    272 
    273 #define set_hw_dma_read_buffer(me, method) \
    274 ((me)->to_dma_read_buffer = (method))
    275 
    276 typedef unsigned (hw_dma_write_buffer_method)
    277      (struct hw *bus,
    278       const void *source,
    279       int space,
    280       unsigned_word addr,
    281       unsigned nr_bytes,
    282       int violate_read_only_section);
    283 
    284 #define hw_dma_write_buffer(bus, src, space, addr, nr_bytes, violate_ro) \
    285 ((bus)->to_dma_write_buffer (bus, src, space, addr, nr_bytes, violate_ro))
    286 
    287 #define set_hw_dma_write_buffer(me, method) \
    288 ((me)->to_dma_write_buffer = (method))
    289 
    290 /* Address/size specs for devices are encoded following a convention
    292    similar to that used by OpenFirmware.  In particular, an
    293    address/size is packed into a sequence of up to four cell words.
    294    The number of words determined by the number of {address,size}
    295    cells attributes of the device. */
    296 
    297 typedef struct _hw_unit
    298 {
    299   int nr_cells;
    300   unsigned_cell cells[4]; /* unused cells are zero */
    301 } hw_unit;
    302 
    303 
    304 /* For the given bus, the number of address and size cells used in a
    305    hw_unit. */
    306 
    307 #define hw_unit_nr_address_cells(bus) ((bus)->nr_address_cells_of_hw_unit + 0)
    308 
    309 #define hw_unit_nr_size_cells(bus) ((bus)->nr_size_cells_of_hw_unit + 0)
    310 
    311 
    312 /* For the given device, its identifying hw_unit address.
    313 
    314    Each device has an identifying hw_unit address.  That address is
    315    used when identifying one of a number of identical devices on a
    316    common controller bus. ex fd0&fd1. */
    317 
    318 const hw_unit *hw_unit_address
    319 (struct hw *me);
    320 
    321 
    322 /* Convert between a textual and the internal representation of a
    323    hw_unit address/size.
    324 
    325    NOTE: A device asks its parent to translate between a hw_unit and
    326    textual representation.  This is because the textual address of a
    327    device is specified using the parent busses notation. */
    328 
    329 typedef int (hw_unit_decode_method)
    330      (struct hw *bus,
    331       const char *encoded,
    332       hw_unit *unit);
    333 
    334 #define hw_unit_decode(bus, encoded, unit) \
    335 ((bus)->to_unit_decode (bus, encoded, unit))
    336 
    337 #define set_hw_unit_decode(hw, method) \
    338 ((hw)->to_unit_decode = (method))
    339 
    340 typedef int (hw_unit_encode_method)
    341      (struct hw *bus,
    342       const hw_unit *unit,
    343       char *encoded,
    344       int sizeof_buf);
    345 
    346 #define hw_unit_encode(bus, unit, encoded, sizeof_encoded) \
    347 ((bus)->to_unit_encode (bus, unit, encoded, sizeof_encoded))
    348 
    349 #define set_hw_unit_encode(hw, method) \
    350 ((hw)->to_unit_encode = (method))
    351 
    352 
    353 /* As the bus that the device is attached too, to translate a devices
    354    hw_unit address/size into a form suitable for an attach address
    355    call.
    356 
    357    Return a zero result if the address should be ignored when looking
    358    for attach addresses. */
    359 
    360 typedef int (hw_unit_address_to_attach_address_method)
    361      (struct hw *bus,
    362       const hw_unit *unit_addr,
    363       int *attach_space,
    364       unsigned_word *attach_addr,
    365       struct hw *client);
    366 
    367 #define hw_unit_address_to_attach_address(bus, unit_addr, attach_space, attach_addr, client) \
    368 ((bus)->to_unit_address_to_attach_address (bus, unit_addr, attach_space, attach_addr, client))
    369 
    370 #define set_hw_unit_address_to_attach_address(hw, method) \
    371 ((hw)->to_unit_address_to_attach_address = (method))
    372 
    373 typedef int (hw_unit_size_to_attach_size_method)
    374      (struct hw *bus,
    375       const hw_unit *unit_size,
    376       unsigned *attach_size,
    377       struct hw *client);
    378 
    379 #define hw_unit_size_to_attach_size(bus, unit_size, attach_size, client) \
    380 ((bus)->to_unit_size_to_attach_size (bus, unit_size, attach_size, client))
    381 
    382 #define set_hw_unit_size_to_attach_size(hw, method) \
    383 ((hw)->to_unit_size_to_attach_size = (method))
    384 
    385 
    386 extern char *hw_strdup (struct hw *me, const char *str);
    388 
    389 
    390 /* Utilities:
    392 
    393    */
    394 
    395 /* IOCTL::
    396 
    397    Often devices require `out of band' operations to be performed.
    398    For instance a pal device may need to notify a PCI bridge device
    399    that an interrupt ack cycle needs to be performed on the PCI bus.
    400    Within PSIM such operations are performed by using the generic
    401    ioctl call <<hw_ioctl()>>.
    402 
    403    */
    404 
    405 typedef enum
    406 {
    407   hw_ioctl_break, /* unsigned_word requested_break */
    408   hw_ioctl_set_trace, /* void */
    409   hw_ioctl_create_stack, /* unsigned_word *sp, char **argv, char **envp */
    410   hw_ioctl_change_media, /* const char *new_image (possibly NULL) */
    411   nr_hw_ioctl_requests,
    412 } hw_ioctl_request;
    413 
    414 typedef int (hw_ioctl_method)
    415      (struct hw *me,
    416       hw_ioctl_request request,
    417       va_list ap);
    418 
    419 int hw_ioctl
    420 (struct hw *me,
    421  hw_ioctl_request request,
    422  ...);
    423 
    424 
    425 /* Error reporting::
    426 
    427    So that errors originating from devices appear in a consistent
    428    format, the <<hw_abort()>> function can be used.  Formats and
    429    outputs the error message before aborting the simulation
    430 
    431    Devices should use this function to abort the simulation except
    432    when the abort reason leaves the simulation in a hazardous
    433    condition (for instance a failed malloc).
    434 
    435    */
    436 
    437 void hw_abort
    438 (struct hw *me,
    439  const char *fmt,
    440  ...) __attribute__ ((format (printf, 2, 3), noreturn));
    441 
    442 void hw_vabort
    443 (struct hw *me,
    444  const char *fmt,
    445  va_list ap) __attribute__ ((noreturn));
    446 
    447 void hw_halt
    448 (struct hw *me,
    449  int reason,
    450  int status) __attribute__ ((noreturn));
    451 
    452 
    453 #define hw_trace_p(hw) ((hw)->trace_of_hw_p + 0)
    454 
    455 void hw_trace
    456 (struct hw *me,
    457  const char *fmt,
    458  ...) __attribute__ ((format (printf, 2, 3)));
    459 
    460 #define HW_TRACE(ARGS) \
    461 do { \
    462   if (hw_trace_p (me)) \
    463     { \
    464       hw_trace ARGS; \
    465     } \
    466 } while (0)
    467 
    468 
    469 /* Some of the related functions require specific types */
    470 
    471 struct hw_property_data;
    472 struct hw_port_data;
    473 struct hw_base_data;
    474 struct hw_alloc_data;
    475 struct hw_event_data;
    476 struct hw_handle_data;
    477 struct hw_instance_data;
    478 
    479 /* Finally the hardware device - keep your grubby little mits off of
    480    these internals! :-) */
    481 
    482 struct hw
    483 {
    484 
    485   /* our relatives */
    486   struct hw *parent_of_hw;
    487   struct hw *sibling_of_hw;
    488   struct hw *child_of_hw;
    489 
    490   /* our identity */
    491   const char *name_of_hw;
    492   const char *family_of_hw;
    493   const char *args_of_hw;
    494   const char *path_of_hw;
    495 
    496   /* our data */
    497   void *data_of_hw;
    498 
    499   /* hot links */
    500   struct hw *root_of_hw;
    501   struct sim_state *system_of_hw;
    502 
    503   /* identifying data */
    504   hw_unit unit_address_of_hw;
    505   int nr_address_cells_of_hw_unit;
    506   int nr_size_cells_of_hw_unit;
    507 
    508   /* Soft reset */
    509   hw_reset_method *to_reset;
    510 
    511   /* Basic callbacks */
    512   hw_io_read_buffer_method *to_io_read_buffer;
    513   hw_io_write_buffer_method *to_io_write_buffer;
    514   hw_dma_read_buffer_method *to_dma_read_buffer;
    515   hw_dma_write_buffer_method *to_dma_write_buffer;
    516   hw_attach_address_method *to_attach_address;
    517   hw_detach_address_method *to_detach_address;
    518 
    519   /* More complicated callbacks */
    520   hw_ioctl_method *to_ioctl;
    521   int trace_of_hw_p;
    522 
    523   /* address callbacks */
    524   hw_unit_decode_method *to_unit_decode;
    525   hw_unit_encode_method *to_unit_encode;
    526   hw_unit_address_to_attach_address_method *to_unit_address_to_attach_address;
    527   hw_unit_size_to_attach_size_method *to_unit_size_to_attach_size;
    528 
    529   /* related data */
    530   struct hw_property_data *properties_of_hw;
    531   struct hw_port_data *ports_of_hw;
    532   struct hw_base_data *base_of_hw;
    533   struct hw_alloc_data *alloc_of_hw;
    534   struct hw_event_data *events_of_hw;
    535   struct hw_handle_data *handles_of_hw;
    536   struct hw_instance_data *instances_of_hw;
    537 
    538 };
    539 
    540 
    541 #endif
    542