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