-
Notifications
You must be signed in to change notification settings - Fork 100
/
wbsafety.v
587 lines (530 loc) · 14.6 KB
/
wbsafety.v
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
////////////////////////////////////////////////////////////////////////////////
//
// Filename: wbsafety.v
// {{{
// Project: WB2AXIPSP: bus bridges and other odds and ends
//
// Purpose: A WB bus fault isolator. This core will isolate any downstream
// WB slave faults from the upstream channel. It sits as a bump
// in the wire between upstream and downstream channels, and so it will
// consume two clocks--slowing down the slave, but potentially allowing
// the developer to recover in case of a fault.
//
// This core is configured by a couple parameters, which are key to its
// functionality.
//
// OPT_TIMEOUT Set this to a number to be roughly the longest time
// period you expect the slave to stall the bus, or likewise
// the longest time period you expect it to wait for a response.
// If the slave takes longer for either task, a fault will be
// detected and reported.
//
// OPT_SELF_RESET If set, this will send a reset signal to the downstream
// core so that you can attempt to restart it without reloading
// the FPGA. If set, the o_reset signal will be used to reset
// the downstream core.
//
// A second key feature of this core is the outgoing fault detector,
// o_fault. If this signal is ever raised, the slave has (somehow)
// violated protocol. Such a violation may (or may not) return an
// error upstream. For example, if the slave returns a response
// following no requests from the master, then no error will be returned
// up stream (doing so would be a protocol violation), but a fault will
// be detected. Use this line to trigger any internal logic analyzers.
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2019-2024, Gisselquist Technology, LLC
// {{{
// This file is part of the WB2AXIP project.
//
// The WB2AXIP project contains free software and gateware, licensed under the
// Apache License, Version 2.0 (the "License"). You may not use this project,
// or this file, except in compliance with the License. You may obtain a copy
// of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
//
////////////////////////////////////////////////////////////////////////////////
//
//
`default_nettype none
// }}}
module wbsafety #(
// {{{
parameter AW = 28, DW = 32,
parameter OPT_TIMEOUT = 12,
parameter MAX_DEPTH = (OPT_TIMEOUT),
parameter [0:0] OPT_SELF_RESET = 1'b1,
parameter [0:0] F_OPT_FAULTLESS = 1'b1
// }}}
) (
// {{{
input wire i_clk, i_reset,
//
// The incoming WB interface from the (trusted) master
// {{{
// This interface is guaranteed to follow the protocol.
input wire i_wb_cyc, i_wb_stb, i_wb_we,
input wire [AW-1:0] i_wb_addr,
input wire [DW-1:0] i_wb_data,
input wire [DW/8-1:0] i_wb_sel,
output reg o_wb_stall, o_wb_ack,
output reg [DW-1:0] o_wb_idata,
output reg o_wb_err,
// }}}
//
// The outgoing interface to the untrusted slave
// {{{
// This interface may or may not follow the WB protocol
output reg o_reset,
output reg o_wb_cyc, o_wb_stb, o_wb_we,
output reg [AW-1:0] o_wb_addr,
output reg [DW-1:0] o_wb_data,
output reg [DW/8-1:0] o_wb_sel,
input wire i_wb_stall, i_wb_ack,
input wire [DW-1:0] i_wb_idata,
input wire i_wb_err,
// }}}
//
// The fault signal, indicating the downstream slave was
// misbehaving
output reg o_fault
// }}}
);
// Declarations
// {{{
localparam LGTIMEOUT = $clog2(OPT_TIMEOUT+1);
localparam LGDEPTH = $clog2(MAX_DEPTH+1);
reg none_expected;
reg [LGDEPTH-1:0] expected_returns;
reg [LGTIMEOUT-1:0] stall_timer, wait_timer;
reg timeout;
reg faulty_return;
wire skd_stb, skd_o_ready, skd_we;
reg skd_stall;
wire [AW-1:0] skd_addr;
wire [DW-1:0] skd_data;
wire [DW/8-1:0] skd_sel;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Start with a skid buffer on all incoming signals
// {{{
////////////////////////////////////////////////////////////////////////
//
//
`ifdef FORMAL
// {{{
// We know the skid buffer works. It's irrelevant to our proof.
// Therefore, remove it during formal testing, lest we need to
// check it as well. Further, we make this a parameter--but only
// when FORMAL is defined--so that it may be overridden in that case.
//
parameter [0:0] SKID_PASSTHROUGH = 1'b1;
`else
localparam [0:0] SKID_PASSTHROUGH = 1'b0;
// }}}
`endif
skidbuffer #(
// {{{
.DW(1+AW+DW+(DW/8)),
.OPT_PASSTHROUGH(SKID_PASSTHROUGH)
// }}}
) skd(
// {{{
.i_clk(i_clk), .i_reset(i_reset || !i_wb_cyc),
.i_valid(i_wb_stb), .o_ready(skd_o_ready),
.i_data({ i_wb_we, i_wb_addr, i_wb_data, i_wb_sel }),
.o_valid(skd_stb), .i_ready(!skd_stall),
.o_data({ skd_we, skd_addr, skd_data, skd_sel })
// }}}
);
always @(*)
o_wb_stall = !skd_o_ready;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Timeout checking
//
//
// Insist on a maximum number of downstream stalls
//
initial stall_timer = 0;
always @(posedge i_clk)
if (!i_reset && o_wb_stb && i_wb_stall)
begin
if (stall_timer <= OPT_TIMEOUT)
stall_timer <= stall_timer + 1;
end else
stall_timer <= 0;
//
// Insist on a maximum number cyles waiting for an acknowledgment
//
initial wait_timer = 0;
always @(posedge i_clk)
if (!i_reset && o_wb_cyc && !o_wb_stb && !i_wb_ack && !i_wb_err
&& !none_expected)
begin
if (wait_timer <= OPT_TIMEOUT)
wait_timer <= wait_timer + 1;
end else
wait_timer <= 0;
//
// Generate a timeout signal on any error
//
initial timeout = 0;
always @(posedge i_clk)
if (timeout && o_wb_err)
timeout <= 0;
else
timeout <= (i_wb_stall)&&(stall_timer >= OPT_TIMEOUT)
|| ((!i_wb_ack && !i_wb_err)&&(wait_timer >= OPT_TIMEOUT));
////////////////////////////////////////////////////////////////////////
//
// Return counting
// {{{
initial none_expected = 1;
initial expected_returns = 0;
always @(posedge i_clk)
if (i_reset || o_reset || o_wb_err || !i_wb_cyc)
begin
expected_returns <= 0;
none_expected <= 1;
end else case({skd_stb && !skd_stall, o_wb_ack })
2'b10: begin
expected_returns <= expected_returns + 1;
none_expected <= 1'b0;
end
2'b01: begin
expected_returns <= expected_returns - 1;
none_expected <= (expected_returns == 1);
end
default: begin end
endcase
// }}}
////////////////////////////////////////////////////////////////////////
//
// Downstream reset generation
// {{{
generate if (OPT_SELF_RESET)
begin : SELF_RESET
initial o_reset = 1;
always @(posedge i_clk)
if (i_reset || o_fault)
o_reset <= 1;
else begin
o_reset <= 0;
if (o_wb_cyc && none_expected
&&(i_wb_ack || i_wb_err))
o_reset <= 1;
if (timeout)
o_reset <= 1;
end
end else begin : FORWARD_RESET
always @(*)
o_reset = i_reset || o_fault;
end endgenerate
// }}}
////////////////////////////////////////////////////////////////////////
//
// Fault detection
// {{{
// faulty_return
// {{{
// A faulty return is a response from the slave at a time, or in
// a fashion that it unexpected and violates protocol
//
always @(*)
begin
faulty_return = 0;
if (expected_returns <= ((o_wb_stb && i_wb_stall) ? 1:0)
+ ((o_wb_ack || o_wb_err) ? 1:0))
faulty_return = i_wb_ack || i_wb_err;
if (i_wb_ack && i_wb_err)
faulty_return = 1;
if (!i_wb_cyc || !o_wb_cyc)
faulty_return = 0;
end
// }}}
// o_fault
// {{{
initial o_fault = 0;
always @(posedge i_clk)
if (o_reset && !i_wb_cyc)
o_fault <= 0;
else begin
if (o_wb_cyc && faulty_return)
o_fault <= 1;
if (i_wb_cyc && timeout)
o_fault <= 1;
end
// }}}
// }}}
////////////////////////////////////////////////////////////////////////
//
// Downstream bus signal generation
//
// o_wb_cyc
// {{{
initial o_wb_cyc = 1'b0;
always @(posedge i_clk)
if (i_reset || (o_wb_cyc && i_wb_err) || o_reset || o_fault
|| (i_wb_cyc && o_wb_err))
o_wb_cyc <= 1'b0;
else
o_wb_cyc <= i_wb_cyc && (o_wb_cyc || i_wb_stb);
// }}}
// o_wb_stb
// {{{
initial o_wb_stb = 1'b0;
always @(posedge i_clk)
if (i_reset || (o_wb_cyc && i_wb_err) || o_reset || o_fault || !i_wb_cyc
|| (i_wb_cyc && o_wb_err))
o_wb_stb <= 1'b0;
else if (!o_wb_stb || !i_wb_stall)
o_wb_stb <= skd_stb;
// }}}
// o_wb_we, o_wb_addr, o_wb_data, o_wb_sel
// {{{
always @(posedge i_clk)
if (!o_wb_stb || !i_wb_stall)
begin
o_wb_we <= skd_we;
o_wb_addr <= skd_addr;
o_wb_data <= skd_data;
o_wb_sel <= skd_sel;
end
// }}}
// o_wb_idata
// {{{
always @(posedge i_clk)
o_wb_idata <= i_wb_idata;
// }}}
////////////////////////////////////////////////////////////////////////
//
// Return signal generation
//
// skd_stall
// {{{
always @(*)
begin
skd_stall = (o_wb_stb && i_wb_stall);
if (i_reset)
skd_stall = 1'b1;
if (o_fault)
skd_stall = 1'b0;
else if (o_reset)
skd_stall = 1'b1;
end
// }}}
// o_wb_ack, o_wb_err
// {{{
initial o_wb_ack = 0;
initial o_wb_err = 0;
always @(posedge i_clk)
if (i_reset || !i_wb_cyc)
begin
o_wb_ack <= 1'b0;
o_wb_err <= 1'b0;
end else if (!o_reset && !o_fault)
begin
if (timeout || faulty_return || i_wb_err)
begin
o_wb_ack <= 1'b0;
o_wb_err <= (expected_returns > ((o_wb_ack||o_wb_err) ? 1:0));
end else begin
o_wb_ack <= o_wb_cyc && i_wb_ack && !i_wb_err;
o_wb_err <= 1'b0;
end
end else begin
o_wb_ack <= 1'b0;
o_wb_err <= (i_wb_stb && !skd_stall);
end
// }}}
// Make Verilator happy
// {{{
// Verilator lint_off UNUSED
wire unused;
assign unused = &{ 1'b0, F_OPT_FAULTLESS };
// Verilator lint_on UNUSED
// }}}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//
// Formal property section
// {{{
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
`ifdef FORMAL
//
// The following proof comes in several parts.
//
// 1. PROVE that the upstream properties will hold independent of
// what the downstream slave ever does.
//
// 2. PROVE that if the downstream slave follows protocol, then
// o_fault will never get raised.
//
// We then repeat these proofs again with both OPT_SELF_RESET set and
// clear. Which of the four proofs is accomplished is dependent upon
// parameters set by the formal engine.
//
//
localparam DOWNSTREAM_ACK_DELAY = OPT_TIMEOUT/2-1;
localparam UPSTREAM_ACK_DELAY = OPT_TIMEOUT + 3;
wire [LGDEPTH-1:0] fwbs_nreqs, fwbs_nacks, fwbs_outstanding;
reg f_past_valid;
initial f_past_valid = 0;
always @(posedge i_clk)
f_past_valid <= 1;
////////////////////////////////////////////////////////////////////////
//
// Upstream master Bus properties
//
always @(*)
if (!f_past_valid)
assume(i_reset);
fwb_slave #(
// {{{
.AW(AW), .DW(DW),
// .F_MAX_ACK_DELAY(UPSTREAM_ACK_DELAY),
.F_LGDEPTH(LGDEPTH),
.F_OPT_DISCONTINUOUS(1),
.F_OPT_MINCLOCK_DELAY(0)
// }}}
) wbs (
// {{{
i_clk, i_reset,
i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data, i_wb_sel,
o_wb_ack, o_wb_stall, o_wb_idata, o_wb_err,
fwbs_nreqs, fwbs_nacks, fwbs_outstanding
// }}}
);
always @(*)
assert(none_expected == (expected_returns == 0));
// Just so we pass the skid buffer's assumptions ...
always @(posedge i_clk)
if (f_past_valid && $past(i_wb_stb && o_wb_stall))
assume($stable(i_wb_data));
always @(*)
if (i_wb_cyc && !o_wb_err && !o_fault)
assert(expected_returns == fwbs_outstanding);
generate if (F_OPT_FAULTLESS)
begin : FAULTLESS_PROPERTIES
// {{{
////////////////////////////////////////////////////////////////
//
// Assume the downstream core is protocol compliant, and
// prove that o_fault stays low.
//
wire [LGDEPTH-1:0] fwbm_nreqs, fwbm_nacks,fwbm_outstanding;
reg [LGDEPTH-1:0] mreqs, sacks;
fwb_master #(
// {{{
.AW(AW), .DW(DW),
.F_MAX_ACK_DELAY(DOWNSTREAM_ACK_DELAY),
.F_MAX_STALL(DOWNSTREAM_ACK_DELAY),
.F_LGDEPTH(LGDEPTH),
.F_OPT_DISCONTINUOUS(1),
.F_OPT_MINCLOCK_DELAY(0)
// }}}
) wbm (
// {{{
i_clk, o_reset,
o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data,
o_wb_sel,
i_wb_ack, i_wb_stall, i_wb_idata, i_wb_err,
fwbm_nreqs, fwbm_nacks, fwbm_outstanding
// }}}
);
//
// Here's the big proof
always @(*)
assert(!o_fault);
////////////////////////////////////////////////////////////////
//
// The following properties are necessary for passing induction
//
always @(*)
if (!i_reset && i_wb_cyc && o_wb_cyc)
assert(expected_returns == fwbm_outstanding
+ (o_wb_stb ? 1:0)
+ ((o_wb_ack|o_wb_err) ? 1:0));
always @(*)
assert(!timeout);
always @(*)
if (o_wb_err)
assert(!o_wb_cyc);
always @(*)
sacks = fwbs_nacks + (o_wb_ack ? 1:0);
always @(*)
if (!o_wb_err && i_wb_cyc && o_wb_cyc)
assert(sacks == fwbm_nacks);
always @(posedge i_clk)
if (!i_reset && i_wb_cyc && expected_returns > 0)
assert(o_wb_cyc || o_wb_err);
always @(*)
mreqs = fwbm_nreqs + (o_wb_stb ? 1:0);
always @(*)
if (!o_wb_err && i_wb_cyc && o_wb_cyc)
assert(fwbs_nreqs == mreqs);
always @(*)
if (i_wb_cyc && o_wb_cyc && fwbs_outstanding > 0)
assert(i_wb_we == o_wb_we);
always @(*)
if (fwbs_nacks != 0 && i_wb_cyc)
assert(o_wb_cyc || o_wb_err);
// }}}
end else begin
// {{{
////////////////////////////////////////////////////////////////
//
// cover() checks, checks that only make sense if faults are
// possible
//
always @(posedge i_clk)
cover(o_fault);
always @(posedge i_clk)
if (f_past_valid && $past(faulty_return))
cover(o_fault);
always @(posedge i_clk)
if (f_past_valid && $past(timeout))
cover(o_fault);
if (OPT_SELF_RESET)
begin
////////////////////////////////////////////////////////
//
// Prove that we can actually reset the downstream
// bus/core as desired
//
reg faulted;
initial faulted = 0;
always @(posedge i_clk)
if (i_reset)
faulted <= 0;
else if (o_fault)
faulted <= 1;
always @(posedge i_clk)
cover(faulted && $fell(o_reset));
always @(posedge i_clk)
cover(faulted && !o_reset && o_wb_ack);
end
// }}}
end endgenerate
always @(*)
cover(!i_reset && fwbs_nacks > 4);
`endif
// }}}
endmodule