1 /* The remote-virtual-component simulator framework
2 for GDB, the GNU Debugger.
4 Copyright 2006-2021 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* This must come before any other includes. */
36 #ifdef HAVE_SYS_TYPES_H
37 #include <sys/types.h>
42 #include <sys/select.h>
44 /* Not guarded in dv-sockser.c, so why here. */
45 #include <netinet/in.h>
46 #include <arpa/inet.h>
48 #include <sys/socket.h>
54 rv - Remote Virtual component
60 Socket connection to a remote simulator component, for example one
61 for testing a verilog construction. Protocol defined below.
63 There is a set of 32-bit I/O ports, with a mapping from local to
64 remote addresses. There is a set of interrupts expressed as a
65 bit-mask, with a mapping from remote to local. There is a set of
66 memory ranges (actual memory defined elsewhere), also with a
67 mapping from remote to local addresses, that is expected to be
68 accessible to the remote simulator in 32-byte chunks (simulating
69 DMA). There is a mapping from remote cycles (or an appropriate
70 elsewhere defined time-slice) to local cycles.
74 reg = <address> <size>
75 The address (within the parent bus) that this device is to
78 remote-reg = <remote-address>
79 The address of reg on the remote side. Defaults to 0.
81 mem = <address> <size>
82 Specify an address-range (within the parent bus) that the remote
83 device can access. The memory is assumed to be already defined.
84 If there's no memory defined but the remote side asks for a memory
85 access, the simulation is aborted.
87 remote-mem = <remote-address>
88 The address of mem on the remote side. Defaults to 0.
91 Address of the mailbox interface. Writes to this address with the
92 local address of a mailbox command, a complete packet with length
93 and command; (4 or 6)) invokes the mailbox interface. Reads are
94 invalid. Replies are written to the same address. Address space
95 from <address> up-to-and-including <address>+3 is allocated.
97 max-poll-ticks = <local-count>
98 Sets the maximum interval between polling the external component,
99 expressed in internal cycles. Defaults to 10000.
101 watchdog-interval = <seconds>
102 Sets the wallclock seconds between watchdog packets sent to the
103 remote side (may be larger if there's no rv activity in that time).
104 Defaults to 30. If set to 0, no watchdog packets are sent.
106 intnum = <local-int-0> <local-int-1> ... <local-int-31>
107 Defines a map from remote bit numbers to local values to be emitted
108 on the "int" port, with the external bit number as the ordinal - 1
109 of the local translation. E.g. 43 121 would mean map external
110 (1<<0) to internal 43 and external (1<<1) to internal 121. The
111 default is unity; no translation. If more than one bit is set in
112 the remote interrupt word, the intmultiple property can be used to
113 control the translation.
115 intmultiple = <intvalue>
116 When more than one bit is set in the remote interrupt word, you may
117 want to map this situation to a separate interrupt value. If this
118 property is non-zero, it is used as that value. If it is zero, the
119 local value for the "int" port is the bitwise-or of the translated
123 The hostname or address where the simulator to be used listens.
124 Defaults to "127.0.0.1"
127 The hostname or address where the simulator to be used listens.
133 Don't connect to a remote side; use initial dummy contents from
134 <filename> (which has to be at least as big as the <size> argument
135 of reg above) or filled with byte-value <value>. Mailboxes are not
136 supported (can be defined but can not be used) and remote-memory
137 accesses don't apply. The main purpose for this property is to
138 simplify use of configuration and simulated hardware that is
139 e.g. only trivially initialized but not actually used.
145 Driven as a result of a remote interrupt request. The value is a
146 32-bit bitset of active interrupts.
156 This is version 1.0 of this protocol, defining packet format and
157 actions in a supposedly upward-compatible manner where client and
158 servers of different versions are expected to interoperate; the
159 format and the definitions below are hopefully generic enough to
162 Each connection has a server and a client (this code); the roles
163 are known beforehand. The client usually corresponds to a CPU and
164 memory system and the server corresponds to a memory-mapped
165 register hardware interface and/or a DMA controller. They
166 communicate using packets with specific commands, of which some
167 require replies from the other side; most are intiated by the
168 client with one exception. A reply uses the same format as the
171 Packets are at least three bytes long, where the first two bytes
172 form a header, a 16-bit little-endian number that is the total
173 length of the packet including the header. There is also a
174 one-byte command. The payload is optional, depending on the
177 [[16-bit-low-byte-of-length] [16-bit-high-byte-of-length]
178 [command/reply] [payload byte 0] [payload byte 1]
179 ... [payload byte (length-of-packet - 3)]]
183 A client or server that reads an undocumented command may exit with
184 a hard error. Payload not defined or disallowed below is ignored.
186 It is expected that future client versions find out the version of
187 the server side by polling with base commands, assuming earlier
188 versions if a certain reply isn't seen, with newly defined payload
189 parts where earlier versions left it undefined. New commands and
190 formats are sent only to the other side after the client and server
191 has found out each others version. Not all servers support all
192 commands; the type of server and supported set of commands is
193 expected to be known beforehand.
196 Initiated by the client, requires a reply from the server. The
197 payload from the client is at least 4 bytes, forming a 4-byte
198 little-endian address, the rest being undefined. The reply from
199 the server is at least 8 bytes, forming the same address data as in
200 the request and the second 4-byte data being the little-endian
204 Initiated by the client, requires a reply from the server. Payload
205 from the client is at least 8 bytes, forming a 4-byte little-endian
206 word being the address, the rest being the little-endian contents
207 to write. The reply from the server is 8 bytes unless elsewhere
208 agreed otherwise, forming the same address and data as in the
209 request. The data sent back may have been altered to correspond to
210 defined parts but can safely be discarded.
213 Initiated by the server, no reply. The payload is 4 bytes, forming
214 a little-endian word with bits numbers corresponding to currently
215 active interrupt sources; value (1<<N) indicating interrupt source
219 Initiated by the server, requires a reply. A client must know
220 beforehand when (in command sequence or constant) the server can
221 send this command and if so must then not send any commands of its
222 own (including watchdog commands); the server is allowed to assume
223 that incoming data is only replies to this command. The format is
224 8 bytes of data; 4 bytes of little-endian address followed by a
225 32-bit little endian word with the number of bytes to read. The
226 reply is the same address and number of bytes, followed by the data
230 Initiated by the server, no reply. The format is the same as a
231 reply to RV_MEM_RD_CMD; a 32-bit little-endian address, followed by
232 the 32-bit little-endian number of bytes to write (redundant
233 information but must be consistent with the packet header).
235 RV_MBOX_HANDLE_CMD = 5
236 Initiated by the client, requires a reply. The payload is 4
237 undefined bytes followed by an binary blob, the size of the
238 blob given by the packet header. The reply is a 32-bit little
239 endian number at the same index as the undefined bytes. Actual
240 semantics are application-specific.
243 Initiated by the client, requires a reply, with the reply using the
244 RV_MBOX_HANDLE_CMD reply format (i.e. *both* that command and
245 32-bit little-endian number). The payload is a 32-bit little
246 endian number followed by an undefined payload, at most 20 bytes
247 long. The reply is a 32-bit little endian number. Actual
248 semantics are application-specific.
251 Initiated by the client, no reply. A version 1.0 client sends no
252 payload; a version 1.0 server should ignore any such payload. A
253 version 1.0 server must not send a reply.
256 Possible future enhancements:
258 Synchronization; server and client reports the number of elapsed
259 cycles (unit to-be-defined) at each request or notification.
260 Pretty much the top-of-the-todo-list item.
262 Large addresses; 1.0 being restricted to 32-bit addresses.
264 Variable-size data; currently restricted to 32-bit register
267 Specified data endianness (not the packet header) perhaps as part
268 of an initial format request; currently little-endian only.
272 When used with servers sending RV_MEM_RD_CMD but being
273 narrow-minded about indata, set watchdog-interval to 0. Use
274 multiple rv instances when there are e.g. separate register and
275 memory servers. Alway log, setting "/rv/trace? true", at the
276 development phase. Borrow from the test-suite.
279 #define RV_FAMILY_NAME "rv"
287 RV_MBOX_HANDLE_CMD
= 5,
293 typedef struct _hw_rv_device
295 /* Mapping of remote interrupt bit-numbers to local ones. */
296 unsigned32 remote_to_local_int
[32];
298 /* When multiple bits are set, a non-zero value here indicates that
299 this value should be used instead. */
300 unsigned32 intmultiple
;
302 /* Local address of registers. */
303 unsigned32 reg_address
;
305 /* Size of register bank in bytes. */
308 /* Remote address of registers. */
309 unsigned32 remote_reg_address
;
311 /* Local address of DMA:able memory. */
312 unsigned32 mem_address
;
314 /* Size of DMA:able memory in bytes. */
317 /* Bitmask for valid DMA request size. */
318 unsigned32 mem_burst_mask
;
320 /* Remote address of DMA:able memory. */
321 unsigned32 remote_mem_address
;
323 /* (Local) address of mbox; where to put a pointer to the mbox to be
325 unsigned32 mbox_address
;
327 /* Probably not 127.0.0.1:10000. */
331 /* If non-NULL, points to memory to use instead of connection. */
334 /* File descriptor for the socket. Set to -1 when error. Only one
335 of dummy and this is active. */
338 /* Stashed errno, as we don't emit an error right away. */
341 /* This, plus latency because the CPU might not be checking until a
342 CTI insn (usually a branch or a jump) is the interval in cycles
343 between the rv is polled for e.g. DMA requests. */
344 unsigned32 max_tick_poll_interval
;
346 /* Running counter for exponential backoff up to
347 max_tick_poll_interval to avoid polling the connection
348 unnecessarily often. Set to 1 when rv activity (read/write
349 register, DMA request) is detected. */
350 unsigned32 next_period
;
352 /* This is the interval in wall-clock seconds between watchdog
353 packets are sent to the remote side. Zero means no watchdog
355 unsigned32 watchdog_interval
;
357 /* Last time we sent a watchdog packet. */
358 struct timeval last_wdog_time
;
360 /* Mostly used as a kludge for knowing which rv:s have poll events
362 struct hw_event
*poll_callback
;
366 /* We might add ports in the future, so keep this an enumeration. */
373 static const struct hw_port_descriptor hw_rv_ports
[] = {
374 { "int", INT_PORT
, 0, output_port
},
378 /* Send LEN bytes of data from BUF to the socket. Abort on
382 hw_rv_write (struct hw
*me
,
386 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
387 unsigned8
*bufp
= buf
;
389 /* If we don't have a valid fd here, it's because we got an error
390 initially, and we suppressed that error. */
392 hw_abort (me
, "couldn't open a connection to %s:%d because: %s",
393 rv
->host
, rv
->port
, strerror (rv
->saved_errno
));
397 ssize_t ret
= write (rv
->fd
, bufp
, len
);
399 /* FIXME: More graceful exit. */
400 hw_abort (me
, "write to %s:%d failed: %s\n", rv
->host
, rv
->port
,
408 /* Read LEN bytes of data into BUF from the socket. Set the file
409 descriptor to -1 if there's an error. */
412 hw_rv_read (struct hw
*me
,
416 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
417 unsigned8
*bufp
= buf
;
421 ssize_t ret
= read (rv
->fd
, bufp
, len
);
423 /* We get all zero if the remote end quits, but no error
424 indication; even select says there's data active. */
427 if (close (rv
->fd
) != 0)
428 /* FIXME: More graceful exit. */
429 hw_abort (me
, "read from %s:%d failed: %d\n", rv
->host
, rv
->port
, errno
);
439 /* Construct and send a packet of data of type CMD and len
440 LEN_NOHEADER (not counting the header...). */
443 hw_rv_send (struct hw
*me
,
446 unsigned int len_noheader
)
448 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
451 unsigned int len
= len_noheader
+ 3;
455 buf
[1] = (len
>> 8) & 255;
458 if (len
> sizeof (buf
))
460 hw_rv_write (me
, buf
, 3);
466 memcpy (buf
+ 3, msg
, len_noheader
);
470 hw_rv_write (me
, bufp
, len
);
473 /* Handle incoming DMA requests as per the RV_MEM_RD_CMD packet.
477 hw_rv_read_mem (struct hw
*me
, unsigned int len
)
479 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
480 /* If you change this size, please adjust the mem2 testcase. */
482 unsigned8
*bufp
= buf
;
489 hw_abort (me
, "expected DMA read request len 8+3, got %d+3", len
);
491 hw_rv_read (me
, &leaddr
, 4);
492 hw_rv_read (me
, &lelen
, 4);
493 len
= LE2H_4 (lelen
);
494 addr
= LE2H_4 (leaddr
);
496 if (addr
< rv
->remote_mem_address
497 || addr
>= rv
->remote_mem_address
+ rv
->mem_size
)
498 hw_abort (me
, "DMA read at remote 0x%x; outside [0x%x..0x%x-1]",
499 (unsigned) addr
, (unsigned) rv
->remote_mem_address
,
500 (unsigned) (rv
->remote_mem_address
+ rv
->mem_size
));
501 addr
= addr
- rv
->remote_mem_address
+ rv
->mem_address
;
504 hw_abort (me
, "DMA read request for 0 bytes isn't supported");
506 if (len
& ~rv
->mem_burst_mask
)
507 hw_abort (me
, "DMA trying to read %d bytes; not matching mask of 0x%x",
508 len
, rv
->mem_burst_mask
);
509 if (len
+ 8 > sizeof (buf
))
510 bufp
= hw_malloc (me
, len
+ 8);
512 HW_TRACE ((me
, "DMA R 0x%x..0x%x", addr
, addr
+ len
-1));
513 hw_dma_read_buffer (me
, bufp
+ 8, 0, addr
, len
);
515 for (i
= 0; i
< len
; i
+= 4)
516 HW_TRACE ((me
, "0x%x: %02x %02x %02x %02x",
518 bufp
[i
+8], bufp
[i
+9], bufp
[i
+10], bufp
[i
+11]));
520 memcpy (bufp
, &leaddr
, 4);
521 memcpy (bufp
+ 4, &lelen
, 4);
522 hw_rv_send (me
, RV_MEM_RD_CMD
, bufp
, len
+ 8);
527 /* Handle incoming DMA requests as per the RV_MEM_WR_CMD packet.
531 hw_rv_write_mem (struct hw
*me
, unsigned int plen
)
533 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
534 /* If you change this size, please adjust the mem2 testcase. */
536 unsigned8
*bufp
= buf
;
543 hw_rv_read (me
, &leaddr
, 4);
544 hw_rv_read (me
, &lelen
, 4);
545 len
= LE2H_4 (lelen
);
546 addr
= LE2H_4 (leaddr
);
550 "inconsistency in DMA write request packet: "
551 "envelope %d+3, inner %d bytes", plen
, len
);
553 if (addr
< rv
->remote_mem_address
554 || addr
>= rv
->remote_mem_address
+ rv
->mem_size
)
555 hw_abort (me
, "DMA write at remote 0x%x; outside [0x%x..0x%x-1]",
556 (unsigned) addr
, (unsigned) rv
->remote_mem_address
,
557 (unsigned) (rv
->remote_mem_address
+ rv
->mem_size
));
559 addr
= addr
- rv
->remote_mem_address
+ rv
->mem_address
;
561 hw_abort (me
, "DMA write request for 0 bytes isn't supported");
563 if (len
& ~rv
->mem_burst_mask
)
564 hw_abort (me
, "DMA trying to write %d bytes; not matching mask of 0x%x",
565 len
, rv
->mem_burst_mask
);
566 if (len
+ 8 > sizeof (buf
))
567 bufp
= hw_malloc (me
, len
+ 8);
569 hw_rv_read (me
, bufp
+ 8, len
);
570 HW_TRACE ((me
, "DMA W 0x%x..0x%x", addr
, addr
+ len
- 1));
571 hw_dma_write_buffer (me
, bufp
+ 8, 0, addr
, len
, 0);
573 for (i
= 0; i
< len
; i
+= 4)
574 HW_TRACE ((me
, "0x%x: %02x %02x %02x %02x",
576 bufp
[i
+8], bufp
[i
+9], bufp
[i
+10], bufp
[i
+11]));
582 hw_rv_irq (struct hw
*me
, unsigned int len
)
584 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
585 unsigned32 intbitsle
;
586 unsigned32 intbits_ext
;
587 unsigned32 intval
= 0;
591 hw_abort (me
, "IRQ with %d data not supported", len
);
593 hw_rv_read (me
, &intbitsle
, 4);
594 intbits_ext
= LE2H_4 (intbitsle
);
595 for (i
= 0; i
< 32; i
++)
596 if ((intbits_ext
& (1 << i
)) != 0)
597 intval
|= rv
->remote_to_local_int
[i
];
598 if ((intbits_ext
& ~(intbits_ext
- 1)) != intbits_ext
599 && rv
->intmultiple
!= 0)
600 intval
= rv
->intmultiple
;
602 HW_TRACE ((me
, "IRQ 0x%x", intval
));
603 hw_port_event (me
, INT_PORT
, intval
);
606 /* Handle incoming interrupt notifications as per the RV_IRQ_CMD
607 packet. Abort on errors. */
610 hw_rv_handle_incoming (struct hw
*me
,
613 unsigned int *return_len
)
615 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
622 hw_rv_read (me
, cbuf
, 3);
627 len
= cbuf
[0] + cbuf
[1] * 256 - 3;
630 /* These come in "asynchronously"; not as a reply. */
638 hw_rv_read_mem (me
, len
);
642 hw_rv_write_mem (me
, len
);
646 /* Something is incoming from the other side, so tighten up all
647 slack at the next wait. */
655 /* Don't try to handle more than one of these if we were'nt
656 expecting a reply. */
657 if (expected_type
== -1)
662 /* Require a match between this supposed-reply and the command
664 if (cmd
!= expected_type
)
665 hw_abort (me
, "unexpected reply, expected command %d, got %d",
670 case RV_MBOX_PUT_CMD
:
671 case RV_MBOX_HANDLE_CMD
:
674 hw_rv_read (me
, buf
, len
<= *return_len
? len
: *return_len
);
682 /* Send a watchdog packet. Make a note of wallclock time. */
685 hw_rv_send_wdog (struct hw
*me
)
687 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
688 HW_TRACE ((me
, "WD"));
689 gettimeofday (&rv
->last_wdog_time
, NULL
);
690 hw_rv_send (me
, RV_WATCHDOG_CMD
, "", 0);
693 /* Poll the remote side: see if there's any incoming traffic; handle a
694 packet if so. Send a watchdog packet if it's time to do so.
695 Beware that the Linux select call indicates traffic for a socket
696 that the remote side has closed (which may be because it was
697 finished; don't hork until we need to write something just because
701 hw_rv_poll_once (struct hw
*me
)
703 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
711 /* Connection has died or was never initiated. */
715 FD_SET (rv
->fd
, &rfds
);
717 FD_SET (rv
->fd
, &efds
);
721 ret
= select (rv
->fd
+ 1, &rfds
, NULL
, &efds
, &tv
);
722 gettimeofday (&now
, NULL
);
725 hw_abort (me
, "select failed: %d\n", errno
);
727 if (rv
->watchdog_interval
!= 0
728 && now
.tv_sec
- rv
->last_wdog_time
.tv_sec
>= rv
->watchdog_interval
)
729 hw_rv_send_wdog (me
);
731 if (FD_ISSET (rv
->fd
, &rfds
))
732 hw_rv_handle_incoming (me
, -1, NULL
, NULL
);
735 /* Initialize mapping of remote-to-local interrupt data. */
738 hw_rv_map_ints (struct hw
*me
)
740 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
743 for (i
= 0; i
< 32; i
++)
744 rv
->remote_to_local_int
[i
] = 1 << i
;
746 if (hw_find_property (me
, "intnum") != NULL
)
747 for (i
= 0; i
< 32; i
++)
749 signed_cell val
= -1;
750 if (hw_find_integer_array_property (me
, "intnum", i
, &val
) > 0)
753 rv
->remote_to_local_int
[i
] = val
;
755 hw_abort (me
, "property \"intnum@%d\" must be > 0; is %d",
761 /* Handle the after-N-ticks "poll event", calling the poll-the-fd
762 method. Update the period. */
765 do_poll_event (struct hw
*me
, void *data
)
767 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
768 unsigned32 new_period
;
770 if (rv
->dummy
!= NULL
)
773 hw_rv_poll_once (me
);
776 = hw_event_queue_schedule (me
, rv
->next_period
, do_poll_event
, NULL
);
778 new_period
= rv
->next_period
* 2;
779 if (new_period
<= rv
->max_tick_poll_interval
)
780 rv
->next_period
= new_period
;
783 /* HW tree traverse function for hw_rv_add_init. */
786 hw_rv_add_poller (struct hw
*me
, void *data
)
790 if (hw_family (me
) == NULL
791 || strcmp (hw_family (me
), RV_FAMILY_NAME
) != 0)
794 rv
= (hw_rv_device
*) hw_data (me
);
795 if (rv
->poll_callback
!= NULL
)
799 = hw_event_queue_schedule (me
, 1, do_poll_event
, NULL
);
802 /* Simulator module init function for hw_rv_add_init. */
804 /* FIXME: For the call so hw_tree_traverse, we need to know that the
805 first member of struct sim_hw is the struct hw *root, but there's
806 no accessor method and struct sim_hw is defined in sim-hw.c only.
807 Hence this hack, until an accessor is added, or there's a traverse
808 function that takes a SIM_DESC argument. */
809 struct sim_hw
{ struct hw
*tree
; };
812 hw_rv_add_rv_pollers (SIM_DESC sd
)
814 hw_tree_traverse (STATE_HW (sd
)->tree
, hw_rv_add_poller
, NULL
, NULL
);
818 /* We need to add events for polling, but we can't add one from the
819 finish-function, and there are no other call points, at least for
820 instances without "reg" (when there are just DMA requests from the
821 remote end; no locally initiated activity). Therefore we add a
822 simulator module init function, but those don't have private
823 payload arguments; just a SD argument. We cope by parsing the HW
824 root and making sure *all* "rv":s have poll callbacks installed.
825 Luckily, this is just an initialization step, and not many
826 simultaneous instances of rv are expected: we get a N**2 complexity
827 for visits to each rv node by this method. */
830 hw_rv_add_init (struct hw
*me
)
832 sim_module_add_init_fn (hw_system (me
), hw_rv_add_rv_pollers
);
835 /* Open up a connection to the other side. Abort on errors. */
838 hw_rv_init_socket (struct hw
*me
)
840 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
842 struct sockaddr_in server
;
846 if (rv
->dummy
!= NULL
)
849 memset (&server
, 0, sizeof (server
));
850 server
.sin_family
= AF_INET
;
851 server
.sin_addr
.s_addr
= inet_addr (rv
->host
);
853 /* Solaris 2.7 lacks this macro. */
855 #define INADDR_NONE -1
858 if (server
.sin_addr
.s_addr
== INADDR_NONE
)
861 h
= gethostbyname (rv
->host
);
864 memcpy (&server
.sin_addr
, h
->h_addr
, h
->h_length
);
865 server
.sin_family
= h
->h_addrtype
;
868 hw_abort (me
, "can't resolve host %s", rv
->host
);
871 server
.sin_port
= htons (rv
->port
);
872 sock
= socket (AF_INET
, SOCK_STREAM
, 0);
875 hw_abort (me
, "can't get a socket for %s:%d connection",
878 if (connect (sock
, (struct sockaddr
*) &server
, sizeof server
) >= 0)
882 /* FIXME: init packet here. Maybe start packet too. */
883 if (rv
->watchdog_interval
!= 0)
884 hw_rv_send_wdog (me
);
887 /* Stash the errno for later display, if some connection activity
888 is requested. Don't emit an error here; we might have been
889 called just for test purposes. */
890 rv
->saved_errno
= errno
;
893 /* Local rv register reads end up here. */
896 hw_rv_reg_read (struct hw
*me
,
900 unsigned int nr_bytes
)
902 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
903 unsigned8 addr_data
[8] = "";
904 unsigned32 a_l
= H2LE_4 (addr
- rv
->reg_address
+ rv
->remote_reg_address
);
905 unsigned int len
= 8;
908 hw_abort (me
, "must be four byte read");
910 if (addr
== rv
->mbox_address
)
911 hw_abort (me
, "invalid read of mbox address 0x%x",
912 (unsigned) rv
->mbox_address
);
914 memcpy (addr_data
, &a_l
, 4);
915 HW_TRACE ((me
, "REG R 0x%x", addr
));
916 if (rv
->dummy
!= NULL
)
919 memcpy (addr_data
+ 4, rv
->dummy
+ addr
- rv
->reg_address
, 4);
923 hw_rv_send (me
, RV_READ_CMD
, addr_data
, len
);
924 hw_rv_handle_incoming (me
, RV_READ_CMD
, addr_data
, &len
);
928 hw_abort (me
, "read %d != 8 bytes returned", len
);
929 HW_TRACE ((me
, ":= 0x%02x%02x%02x%02x",
930 addr_data
[7], addr_data
[6], addr_data
[5], addr_data
[4]));
931 memcpy (dest
, addr_data
+ 4, 4);
935 /* Local rv mbox requests (handle or put) end up here. */
938 hw_rv_mbox (struct hw
*me
, unsigned_word address
)
940 unsigned8 buf
[256+3];
945 = hw_dma_read_buffer (me
, buf
, 0, address
, 3);
948 hw_abort (me
, "mbox read %d != 3 bytes returned", len
);
951 if (cmd
!= RV_MBOX_HANDLE_CMD
&& cmd
!= RV_MBOX_PUT_CMD
)
952 hw_abort (me
, "unsupported mbox command %d", cmd
);
954 len
= buf
[0] + buf
[1]*256;
956 if (len
> sizeof (buf
))
957 hw_abort (me
, "mbox cmd %d send size %d unsupported", cmd
, len
);
959 rlen
= hw_dma_read_buffer (me
, buf
+ 3, 0, address
+ 3, len
- 3);
961 hw_abort (me
, "mbox read %d != %d bytes returned", rlen
, len
- 3);
963 HW_TRACE ((me
, "MBOX %s 0x%x..0x%x",
964 cmd
== RV_MBOX_HANDLE_CMD
? "H" : "P",
965 address
, address
+ len
- 1));
966 for (i
= 0; i
< rlen
; i
+= 8)
967 HW_TRACE ((me
, "0x%x: %02x %02x %02x %02x %02x %02x %02x %02x",
969 buf
[3+i
], buf
[4+i
], buf
[5+i
], buf
[6+i
], buf
[7+i
], buf
[8+i
],
970 buf
[9+i
], buf
[10+i
]));
973 hw_rv_send (me
, cmd
, buf
+ 3, len
);
975 /* Note: both ..._PUT and ..._HANDLE get the ..._HANDLE reply. */
976 hw_rv_handle_incoming (me
, RV_MBOX_HANDLE_CMD
, buf
+ 3, &len
);
977 if (len
> sizeof (buf
))
978 hw_abort (me
, "mbox cmd %d receive size %d unsupported", cmd
, len
);
979 HW_TRACE ((me
, "-> 0x%x..0x%x", address
, address
+ len
+ 3 - 1));
980 for (i
= 0; i
< len
; i
+= 8)
981 HW_TRACE ((me
, "0x%x: %02x %02x %02x %02x %02x %02x %02x %02x",
983 buf
[3+i
], buf
[4+i
], buf
[5+i
], buf
[6+i
], buf
[7+i
], buf
[8+i
],
984 buf
[9+i
], buf
[10+i
]));
989 rlen
= hw_dma_write_buffer (me
, buf
, 0, address
, len
, 0);
991 hw_abort (me
, "mbox write %d != %d bytes", rlen
, len
);
994 /* Local rv register writes end up here. */
997 hw_rv_reg_write (struct hw
*me
,
1001 unsigned int nr_bytes
)
1003 hw_rv_device
*rv
= (hw_rv_device
*) hw_data (me
);
1005 unsigned8 addr_data
[8] = "";
1006 unsigned32 a_l
= H2LE_4 (addr
- rv
->reg_address
+ rv
->remote_reg_address
);
1007 unsigned int len
= 8;
1010 hw_abort (me
, "must be four byte write");
1012 memcpy (addr_data
, &a_l
, 4);
1013 memcpy (addr_data
+ 4, source
, 4);
1015 if (addr
== rv
->mbox_address
)
1017 unsigned32 mbox_addr_le
;
1018 if (rv
->dummy
!= NULL
)
1019 hw_abort (me
, "mbox not supported for a dummy instance");
1020 memcpy (&mbox_addr_le
, source
, 4);
1021 hw_rv_mbox (me
, LE2H_4 (mbox_addr_le
));
1025 HW_TRACE ((me
, "REG W 0x%x := 0x%02x%02x%02x%02x", addr
,
1026 addr_data
[7], addr_data
[6], addr_data
[5], addr_data
[4]));
1027 if (rv
->dummy
!= NULL
)
1030 memcpy (rv
->dummy
+ addr
- rv
->reg_address
, addr_data
+ 4, 4);
1034 hw_rv_send (me
, RV_WRITE_CMD
, addr_data
, len
);
1035 hw_rv_handle_incoming (me
, RV_WRITE_CMD
, addr_data
, &len
);
1039 hw_abort (me
, "read %d != 8 bytes returned", len
);
1041 /* We had an access: tighten up all slack. */
1042 rv
->next_period
= 1;
1047 /* Instance initializer function. */
1050 hw_rv_finish (struct hw
*me
)
1052 hw_rv_device
*rv
= HW_ZALLOC (me
, hw_rv_device
);
1054 const struct hw_property
*mem_prop
;
1055 const struct hw_property
*dummy_prop
;
1056 const struct hw_property
*mbox_prop
;
1058 set_hw_data (me
, rv
);
1062 #define RV_GET_PROP(T, N, M, D) \
1065 if (hw_find_property (me, N) != NULL) \
1066 rv->M = hw_find_ ## T ## _property (me, N); \
1071 #define RV_GET_IPROP(N, M, D) RV_GET_PROP (integer, N, M, D)
1073 RV_GET_PROP (string
, "host", host
, "127.0.0.1");
1074 RV_GET_IPROP ("port", port
, 10000);
1075 RV_GET_IPROP ("remote-reg", remote_reg_address
, 0);
1076 RV_GET_IPROP ("max-poll-ticks", max_tick_poll_interval
, 10000);
1077 RV_GET_IPROP ("watchdog-interval", watchdog_interval
, 30);
1078 RV_GET_IPROP ("remote-mem", remote_mem_address
, 0);
1079 RV_GET_IPROP ("mem-burst-mask", mem_burst_mask
, 0xffff);
1080 RV_GET_IPROP ("intmultiple", intmultiple
, 0);
1082 set_hw_io_read_buffer (me
, hw_rv_reg_read
);
1083 set_hw_io_write_buffer (me
, hw_rv_reg_write
);
1084 set_hw_ports (me
, hw_rv_ports
);
1085 rv
->next_period
= 1;
1087 /* FIXME: We only support zero or one reg and zero or one mem area. */
1088 if (hw_find_property (me
, "reg") != NULL
)
1090 reg_property_spec reg
;
1091 if (hw_find_reg_array_property (me
, "reg", 0, ®
))
1093 unsigned_word attach_address
;
1095 unsigned int attach_size
;
1097 hw_unit_address_to_attach_address (hw_parent (me
),
1102 rv
->reg_address
= attach_address
;
1103 hw_unit_size_to_attach_size (hw_parent (me
),
1106 rv
->reg_size
= attach_size
;
1107 if ((attach_address
& 3) != 0)
1108 hw_abort (me
, "register block must be 4 byte aligned");
1109 hw_attach_address (hw_parent (me
),
1111 attach_space
, attach_address
, attach_size
,
1115 hw_abort (me
, "property \"reg\" has the wrong type");
1118 dummy_prop
= hw_find_property (me
, "dummy");
1119 if (dummy_prop
!= NULL
)
1121 if (rv
->reg_size
== 0)
1122 hw_abort (me
, "dummy argument requires a \"reg\" property");
1124 if (hw_property_type (dummy_prop
) == integer_property
)
1126 unsigned32 dummyfill
= hw_find_integer_property (me
, "dummy");
1127 unsigned8
*dummymem
= hw_malloc (me
, rv
->reg_size
);
1128 memset (dummymem
, dummyfill
, rv
->reg_size
);
1129 rv
->dummy
= dummymem
;
1133 const char *dummyarg
= hw_find_string_property (me
, "dummy");
1134 unsigned8
*dummymem
= hw_malloc (me
, rv
->reg_size
);
1135 FILE *f
= fopen (dummyarg
, "rb");
1138 hw_abort (me
, "opening dummy-file \"%s\": %s",
1139 dummyarg
, strerror (errno
));
1140 if (fread (dummymem
, 1, rv
->reg_size
, f
) != rv
->reg_size
)
1141 hw_abort (me
, "reading dummy-file \"%s\": %s",
1142 dummyarg
, strerror (errno
));
1144 rv
->dummy
= dummymem
;
1148 mbox_prop
= hw_find_property (me
, "mbox");
1149 if (mbox_prop
!= NULL
)
1151 if (hw_property_type (mbox_prop
) == integer_property
)
1153 signed_cell attach_address_sc
1154 = hw_find_integer_property (me
, "mbox");
1156 rv
->mbox_address
= (unsigned32
) attach_address_sc
;
1157 hw_attach_address (hw_parent (me
),
1159 0, (unsigned32
) attach_address_sc
, 4, me
);
1162 hw_abort (me
, "property \"mbox\" has the wrong type");
1165 mem_prop
= hw_find_property (me
, "mem");
1166 if (mem_prop
!= NULL
)
1168 signed_cell attach_address_sc
;
1169 signed_cell attach_size_sc
;
1171 /* Only specific names are reg_array_properties, the rest are
1172 array_properties. */
1173 if (hw_property_type (mem_prop
) == array_property
1174 && hw_property_sizeof_array (mem_prop
) == 2 * sizeof (attach_address_sc
)
1175 && hw_find_integer_array_property (me
, "mem", 0, &attach_address_sc
)
1176 && hw_find_integer_array_property (me
, "mem", 1, &attach_size_sc
))
1178 /* Unfortunate choice of types forces us to dance around a bit. */
1179 rv
->mem_address
= (unsigned32
) attach_address_sc
;
1180 rv
->mem_size
= (unsigned32
) attach_size_sc
;
1181 if ((attach_address_sc
& 3) != 0)
1182 hw_abort (me
, "memory block must be 4 byte aligned");
1185 hw_abort (me
, "property \"mem\" has the wrong type");
1188 hw_rv_map_ints (me
);
1190 hw_rv_init_socket (me
);
1192 /* We need an extra initialization pass, after all others currently
1193 scheduled (mostly, after the simulation events machinery has been
1194 initialized so the events we want don't get thrown out). */
1195 hw_rv_add_init (me
);
1198 /* Our root structure; see dv-* build machinery for usage. */
1200 const struct hw_descriptor dv_rv_descriptor
[] = {
1201 { RV_FAMILY_NAME
, hw_rv_finish
},