sim: switch config.h usage to defs.h
[binutils-gdb.git] / sim / cris / dv-rv.c
1 /* The remote-virtual-component simulator framework
2 for GDB, the GNU Debugger.
3
4 Copyright 2006-2021 Free Software Foundation, Inc.
5
6 This file is part of GDB.
7
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.
12
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.
17
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/>. */
20
21 /* This must come before any other includes. */
22 #include "defs.h"
23
24 #include "sim-main.h"
25 #include "hw-main.h"
26
27 #include "hw-tree.h"
28
29 #include <ctype.h>
30 #include <errno.h>
31 #include <string.h>
32 #ifdef HAVE_UNISTD_H
33 #include <unistd.h>
34 #endif
35 #include <stdlib.h>
36 #ifdef HAVE_SYS_TYPES_H
37 #include <sys/types.h>
38 #endif
39
40 #include <sys/time.h>
41
42 #ifdef HAVE_SYS_SELECT_H
43 #include <sys/select.h>
44 #endif
45
46 /* Not guarded in dv-sockser.c, so why here. */
47 #include <netinet/in.h>
48 #include <arpa/inet.h>
49 #include <netdb.h>
50 #include <sys/socket.h>
51
52
53 /* DEVICE
54
55
56 rv - Remote Virtual component
57
58
59 DESCRIPTION
60
61
62 Socket connection to a remote simulator component, for example one
63 for testing a verilog construction. Protocol defined below.
64
65 There is a set of 32-bit I/O ports, with a mapping from local to
66 remote addresses. There is a set of interrupts expressed as a
67 bit-mask, with a mapping from remote to local. There is a set of
68 memory ranges (actual memory defined elsewhere), also with a
69 mapping from remote to local addresses, that is expected to be
70 accessible to the remote simulator in 32-byte chunks (simulating
71 DMA). There is a mapping from remote cycles (or an appropriate
72 elsewhere defined time-slice) to local cycles.
73
74 PROPERTIES
75
76 reg = <address> <size>
77 The address (within the parent bus) that this device is to
78 be located.
79
80 remote-reg = <remote-address>
81 The address of reg on the remote side. Defaults to 0.
82
83 mem = <address> <size>
84 Specify an address-range (within the parent bus) that the remote
85 device can access. The memory is assumed to be already defined.
86 If there's no memory defined but the remote side asks for a memory
87 access, the simulation is aborted.
88
89 remote-mem = <remote-address>
90 The address of mem on the remote side. Defaults to 0.
91
92 mbox = <address>
93 Address of the mailbox interface. Writes to this address with the
94 local address of a mailbox command, a complete packet with length
95 and command; (4 or 6)) invokes the mailbox interface. Reads are
96 invalid. Replies are written to the same address. Address space
97 from <address> up-to-and-including <address>+3 is allocated.
98
99 max-poll-ticks = <local-count>
100 Sets the maximum interval between polling the external component,
101 expressed in internal cycles. Defaults to 10000.
102
103 watchdog-interval = <seconds>
104 Sets the wallclock seconds between watchdog packets sent to the
105 remote side (may be larger if there's no rv activity in that time).
106 Defaults to 30. If set to 0, no watchdog packets are sent.
107
108 intnum = <local-int-0> <local-int-1> ... <local-int-31>
109 Defines a map from remote bit numbers to local values to be emitted
110 on the "int" port, with the external bit number as the ordinal - 1
111 of the local translation. E.g. 43 121 would mean map external
112 (1<<0) to internal 43 and external (1<<1) to internal 121. The
113 default is unity; no translation. If more than one bit is set in
114 the remote interrupt word, the intmultiple property can be used to
115 control the translation.
116
117 intmultiple = <intvalue>
118 When more than one bit is set in the remote interrupt word, you may
119 want to map this situation to a separate interrupt value. If this
120 property is non-zero, it is used as that value. If it is zero, the
121 local value for the "int" port is the bitwise-or of the translated
122 local values.
123
124 host = <hostid>
125 The hostname or address where the simulator to be used listens.
126 Defaults to "127.0.0.1"
127
128 port = <portnumber>
129 The hostname or address where the simulator to be used listens.
130 Defaults to 10000.
131
132 dummy = <value>
133 or
134 dummy = <filename>
135 Don't connect to a remote side; use initial dummy contents from
136 <filename> (which has to be at least as big as the <size> argument
137 of reg above) or filled with byte-value <value>. Mailboxes are not
138 supported (can be defined but can not be used) and remote-memory
139 accesses don't apply. The main purpose for this property is to
140 simplify use of configuration and simulated hardware that is
141 e.g. only trivially initialized but not actually used.
142
143
144 PORTS
145
146 int (output)
147 Driven as a result of a remote interrupt request. The value is a
148 32-bit bitset of active interrupts.
149
150
151 BUGS
152
153 All and none.
154
155
156 PROTOCOL
157
158 This is version 1.0 of this protocol, defining packet format and
159 actions in a supposedly upward-compatible manner where client and
160 servers of different versions are expected to interoperate; the
161 format and the definitions below are hopefully generic enough to
162 allow this.
163
164 Each connection has a server and a client (this code); the roles
165 are known beforehand. The client usually corresponds to a CPU and
166 memory system and the server corresponds to a memory-mapped
167 register hardware interface and/or a DMA controller. They
168 communicate using packets with specific commands, of which some
169 require replies from the other side; most are intiated by the
170 client with one exception. A reply uses the same format as the
171 command.
172
173 Packets are at least three bytes long, where the first two bytes
174 form a header, a 16-bit little-endian number that is the total
175 length of the packet including the header. There is also a
176 one-byte command. The payload is optional, depending on the
177 command.
178
179 [[16-bit-low-byte-of-length] [16-bit-high-byte-of-length]
180 [command/reply] [payload byte 0] [payload byte 1]
181 ... [payload byte (length-of-packet - 3)]]
182
183 Commands:
184
185 A client or server that reads an undocumented command may exit with
186 a hard error. Payload not defined or disallowed below is ignored.
187
188 It is expected that future client versions find out the version of
189 the server side by polling with base commands, assuming earlier
190 versions if a certain reply isn't seen, with newly defined payload
191 parts where earlier versions left it undefined. New commands and
192 formats are sent only to the other side after the client and server
193 has found out each others version. Not all servers support all
194 commands; the type of server and supported set of commands is
195 expected to be known beforehand.
196
197 RV_READ_CMD = 0
198 Initiated by the client, requires a reply from the server. The
199 payload from the client is at least 4 bytes, forming a 4-byte
200 little-endian address, the rest being undefined. The reply from
201 the server is at least 8 bytes, forming the same address data as in
202 the request and the second 4-byte data being the little-endian
203 contents.
204
205 RV_WRITE_CMD = 1
206 Initiated by the client, requires a reply from the server. Payload
207 from the client is at least 8 bytes, forming a 4-byte little-endian
208 word being the address, the rest being the little-endian contents
209 to write. The reply from the server is 8 bytes unless elsewhere
210 agreed otherwise, forming the same address and data as in the
211 request. The data sent back may have been altered to correspond to
212 defined parts but can safely be discarded.
213
214 RV_IRQ_CMD = 2
215 Initiated by the server, no reply. The payload is 4 bytes, forming
216 a little-endian word with bits numbers corresponding to currently
217 active interrupt sources; value (1<<N) indicating interrupt source
218 N being active.
219
220 RV_MEM_RD_CMD = 3
221 Initiated by the server, requires a reply. A client must know
222 beforehand when (in command sequence or constant) the server can
223 send this command and if so must then not send any commands of its
224 own (including watchdog commands); the server is allowed to assume
225 that incoming data is only replies to this command. The format is
226 8 bytes of data; 4 bytes of little-endian address followed by a
227 32-bit little endian word with the number of bytes to read. The
228 reply is the same address and number of bytes, followed by the data
229 that had been read.
230
231 RV_MEM_WR_CMD = 4
232 Initiated by the server, no reply. The format is the same as a
233 reply to RV_MEM_RD_CMD; a 32-bit little-endian address, followed by
234 the 32-bit little-endian number of bytes to write (redundant
235 information but must be consistent with the packet header).
236
237 RV_MBOX_HANDLE_CMD = 5
238 Initiated by the client, requires a reply. The payload is 4
239 undefined bytes followed by an binary blob, the size of the
240 blob given by the packet header. The reply is a 32-bit little
241 endian number at the same index as the undefined bytes. Actual
242 semantics are application-specific.
243
244 RV_MBOX_PUT_CMD = 6
245 Initiated by the client, requires a reply, with the reply using the
246 RV_MBOX_HANDLE_CMD reply format (i.e. *both* that command and
247 32-bit little-endian number). The payload is a 32-bit little
248 endian number followed by an undefined payload, at most 20 bytes
249 long. The reply is a 32-bit little endian number. Actual
250 semantics are application-specific.
251
252 RV_WATCHDOG_CMD = 7
253 Initiated by the client, no reply. A version 1.0 client sends no
254 payload; a version 1.0 server should ignore any such payload. A
255 version 1.0 server must not send a reply.
256
257
258 Possible future enhancements:
259
260 Synchronization; server and client reports the number of elapsed
261 cycles (unit to-be-defined) at each request or notification.
262 Pretty much the top-of-the-todo-list item.
263
264 Large addresses; 1.0 being restricted to 32-bit addresses.
265
266 Variable-size data; currently restricted to 32-bit register
267 accesses.
268
269 Specified data endianness (not the packet header) perhaps as part
270 of an initial format request; currently little-endian only.
271
272
273 Usage notes:
274 When used with servers sending RV_MEM_RD_CMD but being
275 narrow-minded about indata, set watchdog-interval to 0. Use
276 multiple rv instances when there are e.g. separate register and
277 memory servers. Alway log, setting "/rv/trace? true", at the
278 development phase. Borrow from the test-suite.
279 */
280
281 #define RV_FAMILY_NAME "rv"
282
283 enum rv_command {
284 RV_READ_CMD = 0,
285 RV_WRITE_CMD = 1,
286 RV_IRQ_CMD = 2,
287 RV_MEM_RD_CMD = 3,
288 RV_MEM_WR_CMD = 4,
289 RV_MBOX_HANDLE_CMD = 5,
290 RV_MBOX_PUT_CMD = 6,
291 RV_WATCHDOG_CMD = 7
292 };
293
294
295 typedef struct _hw_rv_device
296 {
297 /* Mapping of remote interrupt bit-numbers to local ones. */
298 unsigned32 remote_to_local_int[32];
299
300 /* When multiple bits are set, a non-zero value here indicates that
301 this value should be used instead. */
302 unsigned32 intmultiple;
303
304 /* Local address of registers. */
305 unsigned32 reg_address;
306
307 /* Size of register bank in bytes. */
308 unsigned32 reg_size;
309
310 /* Remote address of registers. */
311 unsigned32 remote_reg_address;
312
313 /* Local address of DMA:able memory. */
314 unsigned32 mem_address;
315
316 /* Size of DMA:able memory in bytes. */
317 unsigned32 mem_size;
318
319 /* Bitmask for valid DMA request size. */
320 unsigned32 mem_burst_mask;
321
322 /* Remote address of DMA:able memory. */
323 unsigned32 remote_mem_address;
324
325 /* (Local) address of mbox; where to put a pointer to the mbox to be
326 sent. */
327 unsigned32 mbox_address;
328
329 /* Probably not 127.0.0.1:10000. */
330 const char *host;
331 int port;
332
333 /* If non-NULL, points to memory to use instead of connection. */
334 unsigned8 *dummy;
335
336 /* File descriptor for the socket. Set to -1 when error. Only one
337 of dummy and this is active. */
338 int fd;
339
340 /* Stashed errno, as we don't emit an error right away. */
341 int saved_errno;
342
343 /* This, plus latency because the CPU might not be checking until a
344 CTI insn (usually a branch or a jump) is the interval in cycles
345 between the rv is polled for e.g. DMA requests. */
346 unsigned32 max_tick_poll_interval;
347
348 /* Running counter for exponential backoff up to
349 max_tick_poll_interval to avoid polling the connection
350 unnecessarily often. Set to 1 when rv activity (read/write
351 register, DMA request) is detected. */
352 unsigned32 next_period;
353
354 /* This is the interval in wall-clock seconds between watchdog
355 packets are sent to the remote side. Zero means no watchdog
356 packets. */
357 unsigned32 watchdog_interval;
358
359 /* Last time we sent a watchdog packet. */
360 struct timeval last_wdog_time;
361
362 /* Mostly used as a kludge for knowing which rv:s have poll events
363 active. */
364 struct hw_event *poll_callback;
365 } hw_rv_device;
366
367
368 /* We might add ports in the future, so keep this an enumeration. */
369 enum
370 {
371 INT_PORT
372 };
373
374 /* Our ports. */
375 static const struct hw_port_descriptor hw_rv_ports[] = {
376 { "int", INT_PORT, 0, output_port },
377 { NULL }
378 };
379
380 /* Send LEN bytes of data from BUF to the socket. Abort on
381 errors. */
382
383 static void
384 hw_rv_write (struct hw *me,
385 void *buf,
386 unsigned int len)
387 {
388 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
389 unsigned8 *bufp = buf;
390
391 /* If we don't have a valid fd here, it's because we got an error
392 initially, and we suppressed that error. */
393 if (rv->fd == -1)
394 hw_abort (me, "couldn't open a connection to %s:%d because: %s",
395 rv->host, rv->port, strerror (rv->saved_errno));
396
397 while (len > 0)
398 {
399 ssize_t ret = write (rv->fd, bufp, len);
400 if (ret < 0)
401 /* FIXME: More graceful exit. */
402 hw_abort (me, "write to %s:%d failed: %s\n", rv->host, rv->port,
403 strerror (errno));
404
405 len -= ret;
406 bufp += ret;
407 }
408 }
409
410 /* Read LEN bytes of data into BUF from the socket. Set the file
411 descriptor to -1 if there's an error. */
412
413 static void
414 hw_rv_read (struct hw *me,
415 void *buf,
416 unsigned int len)
417 {
418 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
419 unsigned8 *bufp = buf;
420
421 while (len > 0)
422 {
423 ssize_t ret = read (rv->fd, bufp, len);
424
425 /* We get all zero if the remote end quits, but no error
426 indication; even select says there's data active. */
427 if (ret <= 0)
428 {
429 if (close (rv->fd) != 0)
430 /* FIXME: More graceful exit. */
431 hw_abort (me, "read from %s:%d failed: %d\n", rv->host, rv->port, errno);
432 rv->fd = -1;
433 return;
434 }
435
436 len -= ret;
437 bufp += ret;
438 }
439 }
440
441 /* Construct and send a packet of data of type CMD and len
442 LEN_NOHEADER (not counting the header...). */
443
444 static void
445 hw_rv_send (struct hw *me,
446 unsigned int cmd,
447 void *msg,
448 unsigned int len_noheader)
449 {
450 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
451 unsigned8 buf[32+3];
452 unsigned8 *bufp;
453 unsigned int len = len_noheader + 3;
454 int ret;
455
456 buf[0] = len & 255;
457 buf[1] = (len >> 8) & 255;
458 buf[2] = cmd;
459
460 if (len > sizeof (buf))
461 {
462 hw_rv_write (me, buf, 3);
463 len = len_noheader;
464 bufp = msg;
465 }
466 else
467 {
468 memcpy (buf + 3, msg, len_noheader);
469 bufp = buf;
470 }
471
472 hw_rv_write (me, bufp, len);
473 }
474
475 /* Handle incoming DMA requests as per the RV_MEM_RD_CMD packet.
476 Abort on errors. */
477
478 static void
479 hw_rv_read_mem (struct hw *me, unsigned int len)
480 {
481 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
482 /* If you change this size, please adjust the mem2 testcase. */
483 unsigned8 buf[32+8];
484 unsigned8 *bufp = buf;
485 unsigned32 leaddr;
486 unsigned32 addr;
487 unsigned32 lelen;
488 unsigned32 i;
489
490 if (len != 8)
491 hw_abort (me, "expected DMA read request len 8+3, got %d+3", len);
492
493 hw_rv_read (me, &leaddr, 4);
494 hw_rv_read (me, &lelen, 4);
495 len = LE2H_4 (lelen);
496 addr = LE2H_4 (leaddr);
497
498 if (addr < rv->remote_mem_address
499 || addr >= rv->remote_mem_address + rv->mem_size)
500 hw_abort (me, "DMA read at remote 0x%x; outside [0x%x..0x%x-1]",
501 (unsigned) addr, (unsigned) rv->remote_mem_address,
502 (unsigned) (rv->remote_mem_address + rv->mem_size));
503 addr = addr - rv->remote_mem_address + rv->mem_address;
504
505 if (len == 0)
506 hw_abort (me, "DMA read request for 0 bytes isn't supported");
507
508 if (len & ~rv->mem_burst_mask)
509 hw_abort (me, "DMA trying to read %d bytes; not matching mask of 0x%x",
510 len, rv->mem_burst_mask);
511 if (len + 8 > sizeof (buf))
512 bufp = hw_malloc (me, len + 8);
513
514 HW_TRACE ((me, "DMA R 0x%x..0x%x", addr, addr + len -1));
515 hw_dma_read_buffer (me, bufp + 8, 0, addr, len);
516 if (hw_trace_p (me))
517 for (i = 0; i < len; i += 4)
518 HW_TRACE ((me, "0x%x: %02x %02x %02x %02x",
519 addr + i,
520 bufp[i+8], bufp[i+9], bufp[i+10], bufp[i+11]));
521
522 memcpy (bufp, &leaddr, 4);
523 memcpy (bufp + 4, &lelen, 4);
524 hw_rv_send (me, RV_MEM_RD_CMD, bufp, len + 8);
525 if (bufp != buf)
526 hw_free (me, bufp);
527 }
528
529 /* Handle incoming DMA requests as per the RV_MEM_WR_CMD packet.
530 Abort on errors. */
531
532 static void
533 hw_rv_write_mem (struct hw *me, unsigned int plen)
534 {
535 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
536 /* If you change this size, please adjust the mem2 testcase. */
537 unsigned8 buf[32+8];
538 unsigned8 *bufp = buf;
539 unsigned32 leaddr;
540 unsigned32 addr;
541 unsigned32 lelen;
542 unsigned32 len;
543 unsigned32 i;
544
545 hw_rv_read (me, &leaddr, 4);
546 hw_rv_read (me, &lelen, 4);
547 len = LE2H_4 (lelen);
548 addr = LE2H_4 (leaddr);
549
550 if (len != plen - 8)
551 hw_abort (me,
552 "inconsistency in DMA write request packet: "
553 "envelope %d+3, inner %d bytes", plen, len);
554
555 if (addr < rv->remote_mem_address
556 || addr >= rv->remote_mem_address + rv->mem_size)
557 hw_abort (me, "DMA write at remote 0x%x; outside [0x%x..0x%x-1]",
558 (unsigned) addr, (unsigned) rv->remote_mem_address,
559 (unsigned) (rv->remote_mem_address + rv->mem_size));
560
561 addr = addr - rv->remote_mem_address + rv->mem_address;
562 if (len == 0)
563 hw_abort (me, "DMA write request for 0 bytes isn't supported");
564
565 if (len & ~rv->mem_burst_mask)
566 hw_abort (me, "DMA trying to write %d bytes; not matching mask of 0x%x",
567 len, rv->mem_burst_mask);
568 if (len + 8 > sizeof (buf))
569 bufp = hw_malloc (me, len + 8);
570
571 hw_rv_read (me, bufp + 8, len);
572 HW_TRACE ((me, "DMA W 0x%x..0x%x", addr, addr + len - 1));
573 hw_dma_write_buffer (me, bufp + 8, 0, addr, len, 0);
574 if (hw_trace_p (me))
575 for (i = 0; i < len; i += 4)
576 HW_TRACE ((me, "0x%x: %02x %02x %02x %02x",
577 addr + i,
578 bufp[i+8], bufp[i+9], bufp[i+10], bufp[i+11]));
579 if (bufp != buf)
580 hw_free (me, bufp);
581 }
582
583 static void
584 hw_rv_irq (struct hw *me, unsigned int len)
585 {
586 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
587 unsigned32 intbitsle;
588 unsigned32 intbits_ext;
589 unsigned32 intval = 0;
590 int i;
591
592 if (len != 4)
593 hw_abort (me, "IRQ with %d data not supported", len);
594
595 hw_rv_read (me, &intbitsle, 4);
596 intbits_ext = LE2H_4 (intbitsle);
597 for (i = 0; i < 32; i++)
598 if ((intbits_ext & (1 << i)) != 0)
599 intval |= rv->remote_to_local_int[i];
600 if ((intbits_ext & ~(intbits_ext - 1)) != intbits_ext
601 && rv->intmultiple != 0)
602 intval = rv->intmultiple;
603
604 HW_TRACE ((me, "IRQ 0x%x", intval));
605 hw_port_event (me, INT_PORT, intval);
606 }
607
608 /* Handle incoming interrupt notifications as per the RV_IRQ_CMD
609 packet. Abort on errors. */
610
611 static void
612 hw_rv_handle_incoming (struct hw *me,
613 int expected_type,
614 unsigned8 *buf,
615 unsigned int *return_len)
616 {
617 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
618 unsigned8 cbuf[32];
619 unsigned int len;
620 unsigned int cmd;
621
622 while (1)
623 {
624 hw_rv_read (me, cbuf, 3);
625
626 if (rv->fd == -1)
627 return;
628
629 len = cbuf[0] + cbuf[1] * 256 - 3;
630 cmd = cbuf[2];
631
632 /* These come in "asynchronously"; not as a reply. */
633 switch (cmd)
634 {
635 case RV_IRQ_CMD:
636 hw_rv_irq (me, len);
637 break;
638
639 case RV_MEM_RD_CMD:
640 hw_rv_read_mem (me, len);
641 break;
642
643 case RV_MEM_WR_CMD:
644 hw_rv_write_mem (me, len);
645 break;
646 }
647
648 /* Something is incoming from the other side, so tighten up all
649 slack at the next wait. */
650 rv->next_period = 1;
651
652 switch (cmd)
653 {
654 case RV_MEM_RD_CMD:
655 case RV_MEM_WR_CMD:
656 case RV_IRQ_CMD:
657 /* Don't try to handle more than one of these if we were'nt
658 expecting a reply. */
659 if (expected_type == -1)
660 return;
661 continue;
662 }
663
664 /* Require a match between this supposed-reply and the command
665 for the rest. */
666 if (cmd != expected_type)
667 hw_abort (me, "unexpected reply, expected command %d, got %d",
668 expected_type, cmd);
669
670 switch (cmd)
671 {
672 case RV_MBOX_PUT_CMD:
673 case RV_MBOX_HANDLE_CMD:
674 case RV_WRITE_CMD:
675 case RV_READ_CMD:
676 hw_rv_read (me, buf, len <= *return_len ? len : *return_len);
677 *return_len = len;
678 break;
679 }
680 break;
681 }
682 }
683
684 /* Send a watchdog packet. Make a note of wallclock time. */
685
686 static void
687 hw_rv_send_wdog (struct hw *me)
688 {
689 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
690 HW_TRACE ((me, "WD"));
691 gettimeofday (&rv->last_wdog_time, NULL);
692 hw_rv_send (me, RV_WATCHDOG_CMD, "", 0);
693 }
694
695 /* Poll the remote side: see if there's any incoming traffic; handle a
696 packet if so. Send a watchdog packet if it's time to do so.
697 Beware that the Linux select call indicates traffic for a socket
698 that the remote side has closed (which may be because it was
699 finished; don't hork until we need to write something just because
700 we're polling). */
701
702 static void
703 hw_rv_poll_once (struct hw *me)
704 {
705 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
706 fd_set rfds;
707 fd_set efds;
708 struct timeval now;
709 int ret;
710 struct timeval tv;
711
712 if (rv->fd == -1)
713 /* Connection has died or was never initiated. */
714 return;
715
716 FD_ZERO (&rfds);
717 FD_SET (rv->fd, &rfds);
718 FD_ZERO (&efds);
719 FD_SET (rv->fd, &efds);
720 tv.tv_sec = 0;
721 tv.tv_usec = 0;
722
723 ret = select (rv->fd + 1, &rfds, NULL, &efds, &tv);
724 gettimeofday (&now, NULL);
725
726 if (ret < 0)
727 hw_abort (me, "select failed: %d\n", errno);
728
729 if (rv->watchdog_interval != 0
730 && now.tv_sec - rv->last_wdog_time.tv_sec >= rv->watchdog_interval)
731 hw_rv_send_wdog (me);
732
733 if (FD_ISSET (rv->fd, &rfds))
734 hw_rv_handle_incoming (me, -1, NULL, NULL);
735 }
736
737 /* Initialize mapping of remote-to-local interrupt data. */
738
739 static void
740 hw_rv_map_ints (struct hw *me)
741 {
742 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
743 int i;
744
745 for (i = 0; i < 32; i++)
746 rv->remote_to_local_int[i] = 1 << i;
747
748 if (hw_find_property (me, "intnum") != NULL)
749 for (i = 0; i < 32; i++)
750 {
751 signed_cell val = -1;
752 if (hw_find_integer_array_property (me, "intnum", i, &val) > 0)
753 {
754 if (val > 0)
755 rv->remote_to_local_int[i] = val;
756 else
757 hw_abort (me, "property \"intnum@%d\" must be > 0; is %d",
758 i, (int) val);
759 }
760 }
761 }
762
763 /* Handle the after-N-ticks "poll event", calling the poll-the-fd
764 method. Update the period. */
765
766 static void
767 do_poll_event (struct hw *me, void *data)
768 {
769 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
770 unsigned32 new_period;
771
772 if (rv->dummy != NULL)
773 return;
774
775 hw_rv_poll_once (me);
776 if (rv->fd >= 0)
777 rv->poll_callback
778 = hw_event_queue_schedule (me, rv->next_period, do_poll_event, NULL);
779
780 new_period = rv->next_period * 2;
781 if (new_period <= rv->max_tick_poll_interval)
782 rv->next_period = new_period;
783 }
784
785 /* HW tree traverse function for hw_rv_add_init. */
786
787 static void
788 hw_rv_add_poller (struct hw *me, void *data)
789 {
790 hw_rv_device *rv;
791
792 if (hw_family (me) == NULL
793 || strcmp (hw_family (me), RV_FAMILY_NAME) != 0)
794 return;
795
796 rv = (hw_rv_device *) hw_data (me);
797 if (rv->poll_callback != NULL)
798 return;
799
800 rv->poll_callback
801 = hw_event_queue_schedule (me, 1, do_poll_event, NULL);
802 }
803
804 /* Simulator module init function for hw_rv_add_init. */
805
806 /* FIXME: For the call so hw_tree_traverse, we need to know that the
807 first member of struct sim_hw is the struct hw *root, but there's
808 no accessor method and struct sim_hw is defined in sim-hw.c only.
809 Hence this hack, until an accessor is added, or there's a traverse
810 function that takes a SIM_DESC argument. */
811 struct sim_hw { struct hw *tree; };
812
813 static SIM_RC
814 hw_rv_add_rv_pollers (SIM_DESC sd)
815 {
816 hw_tree_traverse (STATE_HW (sd)->tree, hw_rv_add_poller, NULL, NULL);
817 return SIM_RC_OK;
818 }
819
820 /* We need to add events for polling, but we can't add one from the
821 finish-function, and there are no other call points, at least for
822 instances without "reg" (when there are just DMA requests from the
823 remote end; no locally initiated activity). Therefore we add a
824 simulator module init function, but those don't have private
825 payload arguments; just a SD argument. We cope by parsing the HW
826 root and making sure *all* "rv":s have poll callbacks installed.
827 Luckily, this is just an initialization step, and not many
828 simultaneous instances of rv are expected: we get a N**2 complexity
829 for visits to each rv node by this method. */
830
831 static void
832 hw_rv_add_init (struct hw *me)
833 {
834 sim_module_add_init_fn (hw_system (me), hw_rv_add_rv_pollers);
835 }
836
837 /* Open up a connection to the other side. Abort on errors. */
838
839 static void
840 hw_rv_init_socket (struct hw *me)
841 {
842 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
843 int sock;
844 struct sockaddr_in server;
845
846 rv->fd = -1;
847
848 if (rv->dummy != NULL)
849 return;
850
851 memset (&server, 0, sizeof (server));
852 server.sin_family = AF_INET;
853 server.sin_addr.s_addr = inet_addr (rv->host);
854
855 /* Solaris 2.7 lacks this macro. */
856 #ifndef INADDR_NONE
857 #define INADDR_NONE -1
858 #endif
859
860 if (server.sin_addr.s_addr == INADDR_NONE)
861 {
862 struct hostent *h;
863 h = gethostbyname (rv->host);
864 if (h != NULL)
865 {
866 memcpy (&server.sin_addr, h->h_addr, h->h_length);
867 server.sin_family = h->h_addrtype;
868 }
869 else
870 hw_abort (me, "can't resolve host %s", rv->host);
871 }
872
873 server.sin_port = htons (rv->port);
874 sock = socket (AF_INET, SOCK_STREAM, 0);
875
876 if (sock == -1)
877 hw_abort (me, "can't get a socket for %s:%d connection",
878 rv->host, rv->port);
879
880 if (connect (sock, (struct sockaddr *) &server, sizeof server) >= 0)
881 {
882 rv->fd = sock;
883
884 /* FIXME: init packet here. Maybe start packet too. */
885 if (rv->watchdog_interval != 0)
886 hw_rv_send_wdog (me);
887 }
888 else
889 /* Stash the errno for later display, if some connection activity
890 is requested. Don't emit an error here; we might have been
891 called just for test purposes. */
892 rv->saved_errno = errno;
893 }
894
895 /* Local rv register reads end up here. */
896
897 static unsigned int
898 hw_rv_reg_read (struct hw *me,
899 void *dest,
900 int space,
901 unsigned_word addr,
902 unsigned int nr_bytes)
903 {
904 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
905 unsigned8 addr_data[8] = "";
906 unsigned32 a_l = H2LE_4 (addr - rv->reg_address + rv->remote_reg_address);
907 unsigned int len = 8;
908
909 if (nr_bytes != 4)
910 hw_abort (me, "must be four byte read");
911
912 if (addr == rv->mbox_address)
913 hw_abort (me, "invalid read of mbox address 0x%x",
914 (unsigned) rv->mbox_address);
915
916 memcpy (addr_data, &a_l, 4);
917 HW_TRACE ((me, "REG R 0x%x", addr));
918 if (rv->dummy != NULL)
919 {
920 len = 8;
921 memcpy (addr_data + 4, rv->dummy + addr - rv->reg_address, 4);
922 }
923 else
924 {
925 hw_rv_send (me, RV_READ_CMD, addr_data, len);
926 hw_rv_handle_incoming (me, RV_READ_CMD, addr_data, &len);
927 }
928
929 if (len != 8)
930 hw_abort (me, "read %d != 8 bytes returned", len);
931 HW_TRACE ((me, ":= 0x%02x%02x%02x%02x",
932 addr_data[7], addr_data[6], addr_data[5], addr_data[4]));
933 memcpy (dest, addr_data + 4, 4);
934 return nr_bytes;
935 }
936
937 /* Local rv mbox requests (handle or put) end up here. */
938
939 static void
940 hw_rv_mbox (struct hw *me, unsigned_word address)
941 {
942 unsigned8 buf[256+3];
943 unsigned int cmd;
944 unsigned int rlen;
945 unsigned32 i;
946 unsigned int len
947 = hw_dma_read_buffer (me, buf, 0, address, 3);
948
949 if (len != 3)
950 hw_abort (me, "mbox read %d != 3 bytes returned", len);
951
952 cmd = buf[2];
953 if (cmd != RV_MBOX_HANDLE_CMD && cmd != RV_MBOX_PUT_CMD)
954 hw_abort (me, "unsupported mbox command %d", cmd);
955
956 len = buf[0] + buf[1]*256;
957
958 if (len > sizeof (buf))
959 hw_abort (me, "mbox cmd %d send size %d unsupported", cmd, len);
960
961 rlen = hw_dma_read_buffer (me, buf + 3, 0, address + 3, len - 3);
962 if (rlen != len - 3)
963 hw_abort (me, "mbox read %d != %d bytes returned", rlen, len - 3);
964
965 HW_TRACE ((me, "MBOX %s 0x%x..0x%x",
966 cmd == RV_MBOX_HANDLE_CMD ? "H" : "P",
967 address, address + len - 1));
968 for (i = 0; i < rlen; i += 8)
969 HW_TRACE ((me, "0x%x: %02x %02x %02x %02x %02x %02x %02x %02x",
970 address + 3 + i,
971 buf[3+i], buf[4+i], buf[5+i], buf[6+i], buf[7+i], buf[8+i],
972 buf[9+i], buf[10+i]));
973
974 len -= 3;
975 hw_rv_send (me, cmd, buf + 3, len);
976
977 /* Note: both ..._PUT and ..._HANDLE get the ..._HANDLE reply. */
978 hw_rv_handle_incoming (me, RV_MBOX_HANDLE_CMD, buf + 3, &len);
979 if (len > sizeof (buf))
980 hw_abort (me, "mbox cmd %d receive size %d unsupported", cmd, len);
981 HW_TRACE ((me, "-> 0x%x..0x%x", address, address + len + 3 - 1));
982 for (i = 0; i < len; i += 8)
983 HW_TRACE ((me, "0x%x: %02x %02x %02x %02x %02x %02x %02x %02x",
984 address + 3 + i,
985 buf[3+i], buf[4+i], buf[5+i], buf[6+i], buf[7+i], buf[8+i],
986 buf[9+i], buf[10+i]));
987
988 len += 3;
989 buf[0] = len & 255;
990 buf[1] = len / 256;
991 rlen = hw_dma_write_buffer (me, buf, 0, address, len, 0);
992 if (rlen != len)
993 hw_abort (me, "mbox write %d != %d bytes", rlen, len);
994 }
995
996 /* Local rv register writes end up here. */
997
998 static unsigned int
999 hw_rv_reg_write (struct hw *me,
1000 const void *source,
1001 int space,
1002 unsigned_word addr,
1003 unsigned int nr_bytes)
1004 {
1005 hw_rv_device *rv = (hw_rv_device *) hw_data (me);
1006
1007 unsigned8 addr_data[8] = "";
1008 unsigned32 a_l = H2LE_4 (addr - rv->reg_address + rv->remote_reg_address);
1009 unsigned int len = 8;
1010
1011 if (nr_bytes != 4)
1012 hw_abort (me, "must be four byte write");
1013
1014 memcpy (addr_data, &a_l, 4);
1015 memcpy (addr_data + 4, source, 4);
1016
1017 if (addr == rv->mbox_address)
1018 {
1019 unsigned32 mbox_addr_le;
1020 if (rv->dummy != NULL)
1021 hw_abort (me, "mbox not supported for a dummy instance");
1022 memcpy (&mbox_addr_le, source, 4);
1023 hw_rv_mbox (me, LE2H_4 (mbox_addr_le));
1024 return nr_bytes;
1025 }
1026
1027 HW_TRACE ((me, "REG W 0x%x := 0x%02x%02x%02x%02x", addr,
1028 addr_data[7], addr_data[6], addr_data[5], addr_data[4]));
1029 if (rv->dummy != NULL)
1030 {
1031 len = 8;
1032 memcpy (rv->dummy + addr - rv->reg_address, addr_data + 4, 4);
1033 }
1034 else
1035 {
1036 hw_rv_send (me, RV_WRITE_CMD, addr_data, len);
1037 hw_rv_handle_incoming (me, RV_WRITE_CMD, addr_data, &len);
1038 }
1039
1040 if (len != 8)
1041 hw_abort (me, "read %d != 8 bytes returned", len);
1042
1043 /* We had an access: tighten up all slack. */
1044 rv->next_period = 1;
1045
1046 return nr_bytes;
1047 }
1048
1049 /* Instance initializer function. */
1050
1051 static void
1052 hw_rv_finish (struct hw *me)
1053 {
1054 hw_rv_device *rv = HW_ZALLOC (me, hw_rv_device);
1055 int i;
1056 const struct hw_property *mem_prop;
1057 const struct hw_property *dummy_prop;
1058 const struct hw_property *mbox_prop;
1059
1060 set_hw_data (me, rv);
1061
1062 #undef RV_GET_IPROP
1063 #undef RV_GET_PROP
1064 #define RV_GET_PROP(T, N, M, D) \
1065 do \
1066 { \
1067 if (hw_find_property (me, N) != NULL) \
1068 rv->M = hw_find_ ## T ## _property (me, N); \
1069 else \
1070 rv->M = (D); \
1071 } \
1072 while (0)
1073 #define RV_GET_IPROP(N, M, D) RV_GET_PROP (integer, N, M, D)
1074
1075 RV_GET_PROP (string, "host", host, "127.0.0.1");
1076 RV_GET_IPROP ("port", port, 10000);
1077 RV_GET_IPROP ("remote-reg", remote_reg_address, 0);
1078 RV_GET_IPROP ("max-poll-ticks", max_tick_poll_interval, 10000);
1079 RV_GET_IPROP ("watchdog-interval", watchdog_interval, 30);
1080 RV_GET_IPROP ("remote-mem", remote_mem_address, 0);
1081 RV_GET_IPROP ("mem-burst-mask", mem_burst_mask, 0xffff);
1082 RV_GET_IPROP ("intmultiple", intmultiple, 0);
1083
1084 set_hw_io_read_buffer (me, hw_rv_reg_read);
1085 set_hw_io_write_buffer (me, hw_rv_reg_write);
1086 set_hw_ports (me, hw_rv_ports);
1087 rv->next_period = 1;
1088
1089 /* FIXME: We only support zero or one reg and zero or one mem area. */
1090 if (hw_find_property (me, "reg") != NULL)
1091 {
1092 reg_property_spec reg;
1093 if (hw_find_reg_array_property (me, "reg", 0, &reg))
1094 {
1095 unsigned_word attach_address;
1096 int attach_space;
1097 unsigned int attach_size;
1098
1099 hw_unit_address_to_attach_address (hw_parent (me),
1100 &reg.address,
1101 &attach_space,
1102 &attach_address,
1103 me);
1104 rv->reg_address = attach_address;
1105 hw_unit_size_to_attach_size (hw_parent (me),
1106 &reg.size,
1107 &attach_size, me);
1108 rv->reg_size = attach_size;
1109 if ((attach_address & 3) != 0)
1110 hw_abort (me, "register block must be 4 byte aligned");
1111 hw_attach_address (hw_parent (me),
1112 0,
1113 attach_space, attach_address, attach_size,
1114 me);
1115 }
1116 else
1117 hw_abort (me, "property \"reg\" has the wrong type");
1118 }
1119
1120 dummy_prop = hw_find_property (me, "dummy");
1121 if (dummy_prop != NULL)
1122 {
1123 if (rv->reg_size == 0)
1124 hw_abort (me, "dummy argument requires a \"reg\" property");
1125
1126 if (hw_property_type (dummy_prop) == integer_property)
1127 {
1128 unsigned32 dummyfill = hw_find_integer_property (me, "dummy");
1129 unsigned8 *dummymem = hw_malloc (me, rv->reg_size);
1130 memset (dummymem, dummyfill, rv->reg_size);
1131 rv->dummy = dummymem;
1132 }
1133 else
1134 {
1135 const char *dummyarg = hw_find_string_property (me, "dummy");
1136 unsigned8 *dummymem = hw_malloc (me, rv->reg_size);
1137 FILE *f = fopen (dummyarg, "rb");
1138
1139 if (f == NULL)
1140 hw_abort (me, "opening dummy-file \"%s\": %s",
1141 dummyarg, strerror (errno));
1142 if (fread (dummymem, 1, rv->reg_size, f) != rv->reg_size)
1143 hw_abort (me, "reading dummy-file \"%s\": %s",
1144 dummyarg, strerror (errno));
1145 fclose (f);
1146 rv->dummy = dummymem;
1147 }
1148 }
1149
1150 mbox_prop = hw_find_property (me, "mbox");
1151 if (mbox_prop != NULL)
1152 {
1153 if (hw_property_type (mbox_prop) == integer_property)
1154 {
1155 signed_cell attach_address_sc
1156 = hw_find_integer_property (me, "mbox");
1157
1158 rv->mbox_address = (unsigned32) attach_address_sc;
1159 hw_attach_address (hw_parent (me),
1160 0,
1161 0, (unsigned32) attach_address_sc, 4, me);
1162 }
1163 else
1164 hw_abort (me, "property \"mbox\" has the wrong type");
1165 }
1166
1167 mem_prop = hw_find_property (me, "mem");
1168 if (mem_prop != NULL)
1169 {
1170 signed_cell attach_address_sc;
1171 signed_cell attach_size_sc;
1172
1173 /* Only specific names are reg_array_properties, the rest are
1174 array_properties. */
1175 if (hw_property_type (mem_prop) == array_property
1176 && hw_property_sizeof_array (mem_prop) == 2 * sizeof (attach_address_sc)
1177 && hw_find_integer_array_property (me, "mem", 0, &attach_address_sc)
1178 && hw_find_integer_array_property (me, "mem", 1, &attach_size_sc))
1179 {
1180 /* Unfortunate choice of types forces us to dance around a bit. */
1181 rv->mem_address = (unsigned32) attach_address_sc;
1182 rv->mem_size = (unsigned32) attach_size_sc;
1183 if ((attach_address_sc & 3) != 0)
1184 hw_abort (me, "memory block must be 4 byte aligned");
1185 }
1186 else
1187 hw_abort (me, "property \"mem\" has the wrong type");
1188 }
1189
1190 hw_rv_map_ints (me);
1191
1192 hw_rv_init_socket (me);
1193
1194 /* We need an extra initialization pass, after all others currently
1195 scheduled (mostly, after the simulation events machinery has been
1196 initialized so the events we want don't get thrown out). */
1197 hw_rv_add_init (me);
1198 }
1199
1200 /* Our root structure; see dv-* build machinery for usage. */
1201
1202 const struct hw_descriptor dv_rv_descriptor[] = {
1203 { RV_FAMILY_NAME, hw_rv_finish },
1204 { NULL }
1205 };