3 based on Anton Blanchard microwatt dcache.vhdl
7 from enum
import Enum
, unique
9 from nmigen
import Module
, Signal
, Elaboratable
, Cat
, Repl
, Array
, Const
10 from nmigen
.cli
import main
11 from nmutil
.iocontrol
import RecordObject
12 from nmigen
.utils
import log2_int
13 from nmigen
.cli
import rtlil
16 from soc
.experiment
.mem_types
import (LoadStore1ToDCacheType
,
17 DCacheToLoadStore1Type
,
21 from soc
.experiment
.wb_types
import (WB_ADDR_BITS
, WB_DATA_BITS
, WB_SEL_BITS
,
22 WBAddrType
, WBDataType
, WBSelType
,
23 WBMasterOut
, WBSlaveOut
,
24 WBMasterOutVector
, WBSlaveOutVector
,
25 WBIOMasterOut
, WBIOSlaveOut
)
27 from soc
.experiment
.cache_ram
import CacheRam
28 from soc
.experiment
.plru
import PLRU
31 # TODO: make these parameters of DCache at some point
32 LINE_SIZE
= 64 # Line size in bytes
33 NUM_LINES
= 32 # Number of lines in a set
34 NUM_WAYS
= 4 # Number of ways
35 TLB_SET_SIZE
= 64 # L1 DTLB entries per set
36 TLB_NUM_WAYS
= 2 # L1 DTLB number of sets
37 TLB_LG_PGSZ
= 12 # L1 DTLB log_2(page_size)
38 LOG_LENGTH
= 0 # Non-zero to enable log data collection
40 # BRAM organisation: We never access more than
41 # -- WB_DATA_BITS at a time so to save
42 # -- resources we make the array only that wide, and
43 # -- use consecutive indices for to make a cache "line"
45 # -- ROW_SIZE is the width in bytes of the BRAM
46 # -- (based on WB, so 64-bits)
47 ROW_SIZE
= WB_DATA_BITS
// 8;
49 # ROW_PER_LINE is the number of row (wishbone
50 # transactions) in a line
51 ROW_PER_LINE
= LINE_SIZE
// ROW_SIZE
53 # BRAM_ROWS is the number of rows in BRAM needed
54 # to represent the full dcache
55 BRAM_ROWS
= NUM_LINES
* ROW_PER_LINE
58 # Bit fields counts in the address
60 # REAL_ADDR_BITS is the number of real address
64 # ROW_BITS is the number of bits to select a row
65 ROW_BITS
= log2_int(BRAM_ROWS
)
67 # ROW_LINE_BITS is the number of bits to select
69 ROW_LINE_BITS
= log2_int(ROW_PER_LINE
)
71 # LINE_OFF_BITS is the number of bits for
72 # the offset in a cache line
73 LINE_OFF_BITS
= log2_int(LINE_SIZE
)
75 # ROW_OFF_BITS is the number of bits for
77 ROW_OFF_BITS
= log2_int(ROW_SIZE
)
79 # INDEX_BITS is the number if bits to
81 INDEX_BITS
= log2_int(NUM_LINES
)
83 # SET_SIZE_BITS is the log base 2 of the set size
84 SET_SIZE_BITS
= LINE_OFF_BITS
+ INDEX_BITS
86 # TAG_BITS is the number of bits of
87 # the tag part of the address
88 TAG_BITS
= REAL_ADDR_BITS
- SET_SIZE_BITS
90 # TAG_WIDTH is the width in bits of each way of the tag RAM
91 TAG_WIDTH
= TAG_BITS
+ 7 - ((TAG_BITS
+ 7) % 8)
93 # WAY_BITS is the number of bits to select a way
94 WAY_BITS
= log2_int(NUM_WAYS
)
96 # Example of layout for 32 lines of 64 bytes:
98 # .. tag |index| line |
100 # .. | |---| | ROW_LINE_BITS (3)
101 # .. | |--- - --| LINE_OFF_BITS (6)
102 # .. | |- --| ROW_OFF_BITS (3)
103 # .. |----- ---| | ROW_BITS (8)
104 # .. |-----| | INDEX_BITS (5)
105 # .. --------| | TAG_BITS (45)
107 TAG_RAM_WIDTH
= TAG_WIDTH
* NUM_WAYS
110 return Array(Signal(TAG_RAM_WIDTH
) for x
in range(NUM_LINES
))
112 def CacheValidBitsArray():
113 return Array(Signal(INDEX_BITS
) for x
in range(NUM_LINES
))
115 def RowPerLineValidArray():
116 return Array(Signal() for x
in range(ROW_PER_LINE
))
119 TLB_SET_BITS
= log2_int(TLB_SET_SIZE
)
120 TLB_WAY_BITS
= log2_int(TLB_NUM_WAYS
)
121 TLB_EA_TAG_BITS
= 64 - (TLB_LG_PGSZ
+ TLB_SET_BITS
)
122 TLB_TAG_WAY_BITS
= TLB_NUM_WAYS
* TLB_EA_TAG_BITS
124 TLB_PTE_WAY_BITS
= TLB_NUM_WAYS
* TLB_PTE_BITS
;
126 assert (LINE_SIZE
% ROW_SIZE
) == 0, "LINE_SIZE not multiple of ROW_SIZE"
127 assert (LINE_SIZE
% 2) == 0, "LINE_SIZE not power of 2"
128 assert (NUM_LINES
% 2) == 0, "NUM_LINES not power of 2"
129 assert (ROW_PER_LINE
% 2) == 0, "ROW_PER_LINE not power of 2"
130 assert ROW_BITS
== (INDEX_BITS
+ ROW_LINE_BITS
), "geometry bits don't add up"
131 assert (LINE_OFF_BITS
== ROW_OFF_BITS
+ ROW_LINE_BITS
), \
132 "geometry bits don't add up"
133 assert REAL_ADDR_BITS
== (TAG_BITS
+ INDEX_BITS
+ LINE_OFF_BITS
), \
134 "geometry bits don't add up"
135 assert REAL_ADDR_BITS
== (TAG_BITS
+ ROW_BITS
+ ROW_OFF_BITS
), \
136 "geometry bits don't add up"
137 assert 64 == WB_DATA_BITS
, "Can't yet handle wb width that isn't 64-bits"
138 assert SET_SIZE_BITS
<= TLB_LG_PGSZ
, "Set indexed by virtual address"
141 def TLBValidBitsArray():
142 return Array(Signal(TLB_NUM_WAYS
) for x
in range(TLB_SET_SIZE
))
145 return Array(Signal(TLB_TAG_WAY_BITS
) for x
in range (TLB_SET_SIZE
))
148 return Array(Signal(TLB_PTE_WAY_BITS
) for x
in range(TLB_SET_SIZE
))
151 return Array(Signal(NUM_WAYS
) for x
in range(TLB_NUM_WAYS
))
153 # Cache RAM interface
155 return Array(Signal(WB_DATA_BITS
) for x
in range(NUM_WAYS
))
157 # PLRU output interface
159 return Array(Signal(WAY_BITS
) for x
in range(NUM_LINES
))
161 # TLB PLRU output interface
163 return Array(Signal(TLB_WAY_BITS
) for x
in range(TLB_SET_SIZE
))
165 # Helper functions to decode incoming requests
167 # Return the cache line index (tag index) for an address
169 return addr
[LINE_OFF_BITS
:SET_SIZE_BITS
]
171 # Return the cache row index (data memory) for an address
173 return addr
[ROW_OFF_BITS
:SET_SIZE_BITS
]
175 # Return the index of a row within a line
176 def get_row_of_line(row
):
177 row_v
= Signal(ROW_BITS
)
179 return row_v
[0:ROW_LINE_BITS
]
181 # Returns whether this is the last row of a line
182 def is_last_row_addr(addr
, last
):
183 return addr
[ROW_OFF_BITS
:LINE_OFF_BITS
] == last
185 # Returns whether this is the last row of a line
186 def is_last_row(row
, last
):
187 return get_row_of_line(row
) == last
189 # Return the address of the next row in the current cache line
190 def next_row_addr(addr
):
191 row_idx
= Signal(ROW_LINE_BITS
)
192 result
= WBAddrType()
193 # Is there no simpler way in VHDL to
194 # generate that 3 bits adder ?
195 row_idx
= addr
[ROW_OFF_BITS
:LINE_OFF_BITS
]
196 row_idx
= Signal(row_idx
+ 1)
198 result
[ROW_OFF_BITS
:LINE_OFF_BITS
] = row_idx
201 # Return the next row in the current cache line. We use a
202 # dedicated function in order to limit the size of the
203 # generated adder to be only the bits within a cache line
204 # (3 bits with default settings)
206 row_v
= row
[0:ROW_LINE_BITS
] + 1
207 return Cat(row_v
[:ROW_LINE_BITS
], row
[ROW_LINE_BITS
:])
209 # Get the tag value from the address
211 return addr
[SET_SIZE_BITS
:REAL_ADDR_BITS
]
213 # Read a tag from a tag memory row
214 def read_tag(way
, tagset
):
215 return tagset
[way
*TAG_WIDTH
:way
* TAG_WIDTH
+ TAG_BITS
]
217 # Read a TLB tag from a TLB tag memory row
218 def read_tlb_tag(way
, tags
):
219 j
= way
* TLB_EA_TAG_BITS
220 return tags
[j
:j
+ TLB_EA_TAG_BITS
]
222 # Write a TLB tag to a TLB tag memory row
223 def write_tlb_tag(way
, tags
, tag
):
224 j
= way
* TLB_EA_TAG_BITS
225 tags
[j
:j
+ TLB_EA_TAG_BITS
] = tag
227 # Read a PTE from a TLB PTE memory row
228 def read_tlb_pte(way
, ptes
):
229 j
= way
* TLB_PTE_BITS
230 return ptes
.bit_select(j
, TLB_PTE_BITS
)
232 def write_tlb_pte(way
, ptes
,newpte
):
233 j
= way
* TLB_PTE_BITS
234 return ptes
[j
:j
+ TLB_PTE_BITS
].eq(newpte
)
237 # Record for storing permission, attribute, etc. bits from a PTE
238 class PermAttr(RecordObject
):
241 self
.reference
= Signal()
242 self
.changed
= Signal()
243 self
.nocache
= Signal()
245 self
.rd_perm
= Signal()
246 self
.wr_perm
= Signal()
249 def extract_perm_attr(pte
):
251 pa
.reference
= pte
[8]
260 # Type of operation on a "valid" input
264 OP_BAD
= 1 # NC cache hit, TLB miss, prot/RC failure
265 OP_STCX_FAIL
= 2 # conditional store w/o reservation
266 OP_LOAD_HIT
= 3 # Cache hit on load
267 OP_LOAD_MISS
= 4 # Load missing cache
268 OP_LOAD_NC
= 5 # Non-cachable load
269 OP_STORE_HIT
= 6 # Store hitting cache
270 OP_STORE_MISS
= 7 # Store missing cache
273 # Cache state machine
276 IDLE
= 0 # Normal load hit processing
277 RELOAD_WAIT_ACK
= 1 # Cache reload wait ack
278 STORE_WAIT_ACK
= 2 # Store wait ack
279 NC_LOAD_WAIT_ACK
= 3 # Non-cachable load wait ack
284 # In order to make timing, we use the BRAMs with
285 # an output buffer, which means that the BRAM
286 # output is delayed by an extra cycle.
288 # Thus, the dcache has a 2-stage internal pipeline
289 # for cache hits with no stalls.
291 # All other operations are handled via stalling
292 # in the first stage.
294 # The second stage can thus complete a hit at the same
295 # time as the first stage emits a stall for a complex op.
297 # Stage 0 register, basically contains just the latched request
299 class RegStage0(RecordObject
):
302 self
.req
= LoadStore1ToDCacheType()
303 self
.tlbie
= Signal()
304 self
.doall
= Signal()
305 self
.tlbld
= Signal()
306 self
.mmu_req
= Signal() # indicates source of request
309 class MemAccessRequest(RecordObject
):
313 self
.valid
= Signal()
315 self
.real_addr
= Signal(REAL_ADDR_BITS
)
316 self
.data
= Signal(64)
317 self
.byte_sel
= Signal(8)
318 self
.hit_way
= Signal(WAY_BITS
)
319 self
.same_tag
= Signal()
320 self
.mmu_req
= Signal()
323 # First stage register, contains state for stage 1 of load hits
324 # and for the state machine used by all other operations
325 class RegStage1(RecordObject
):
328 # Info about the request
329 self
.full
= Signal() # have uncompleted request
330 self
.mmu_req
= Signal() # request is from MMU
331 self
.req
= MemAccessRequest()
334 self
.hit_way
= Signal(WAY_BITS
)
335 self
.hit_load_valid
= Signal()
336 self
.hit_index
= Signal(NUM_LINES
)
337 self
.cache_hit
= Signal()
340 self
.tlb_hit
= Signal()
341 self
.tlb_hit_way
= Signal(TLB_NUM_WAYS
)
342 self
.tlb_hit_index
= Signal(TLB_WAY_BITS
)
344 # 2-stage data buffer for data forwarded from writes to reads
345 self
.forward_data1
= Signal(64)
346 self
.forward_data2
= Signal(64)
347 self
.forward_sel1
= Signal(8)
348 self
.forward_valid1
= Signal()
349 self
.forward_way1
= Signal(WAY_BITS
)
350 self
.forward_row1
= Signal(ROW_BITS
)
351 self
.use_forward1
= Signal()
352 self
.forward_sel
= Signal(8)
354 # Cache miss state (reload state machine)
355 self
.state
= Signal(State
)
357 self
.write_bram
= Signal()
358 self
.write_tag
= Signal()
359 self
.slow_valid
= Signal()
360 self
.wb
= WBMasterOut()
361 self
.reload_tag
= Signal(TAG_BITS
)
362 self
.store_way
= Signal(WAY_BITS
)
363 self
.store_row
= Signal(ROW_BITS
)
364 self
.store_index
= Signal(INDEX_BITS
)
365 self
.end_row_ix
= Signal(log2_int(ROW_LINE_BITS
, False))
366 self
.rows_valid
= RowPerLineValidArray()
367 self
.acks_pending
= Signal(3)
368 self
.inc_acks
= Signal()
369 self
.dec_acks
= Signal()
371 # Signals to complete (possibly with error)
372 self
.ls_valid
= Signal()
373 self
.ls_error
= Signal()
374 self
.mmu_done
= Signal()
375 self
.mmu_error
= Signal()
376 self
.cache_paradox
= Signal()
378 # Signal to complete a failed stcx.
379 self
.stcx_fail
= Signal()
382 # Reservation information
383 class Reservation(RecordObject
):
386 self
.valid
= Signal()
387 self
.addr
= Signal(64-LINE_OFF_BITS
)
390 class DCache(Elaboratable
):
391 """Set associative dcache write-through
392 TODO (in no specific order):
393 * See list in icache.vhdl
394 * Complete load misses on the cycle when WB data comes instead of
395 at the end of line (this requires dealing with requests coming in
399 self
.d_in
= LoadStore1ToDCacheType()
400 self
.d_out
= DCacheToLoadStore1Type()
402 self
.m_in
= MMUToDCacheType()
403 self
.m_out
= DCacheToMMUType()
405 self
.stall_out
= Signal()
407 self
.wb_out
= WBMasterOut()
408 self
.wb_in
= WBSlaveOut()
410 self
.log_out
= Signal(20)
412 def stage_0(self
, m
, r0
, r1
, r0_full
):
413 """Latch the request in r0.req as long as we're not stalling
417 d_in
, d_out
, m_in
= self
.d_in
, self
.d_out
, self
.m_in
421 # TODO, this goes in unit tests and formal proofs
422 with m
.If(~
(d_in
.valid
& m_in
.valid
)):
423 #sync += Display("request collision loadstore vs MMU")
426 with m
.If(m_in
.valid
):
427 sync
+= r
.req
.valid
.eq(1)
428 sync
+= r
.req
.load
.eq(~
(m_in
.tlbie | m_in
.tlbld
))
429 sync
+= r
.req
.dcbz
.eq(0)
430 sync
+= r
.req
.nc
.eq(0)
431 sync
+= r
.req
.reserve
.eq(0)
432 sync
+= r
.req
.virt_mode
.eq(1)
433 sync
+= r
.req
.priv_mode
.eq(1)
434 sync
+= r
.req
.addr
.eq(m_in
.addr
)
435 sync
+= r
.req
.data
.eq(m_in
.pte
)
436 sync
+= r
.req
.byte_sel
.eq(~
0) # Const -1 sets all to 0b111....
437 sync
+= r
.tlbie
.eq(m_in
.tlbie
)
438 sync
+= r
.doall
.eq(m_in
.doall
)
439 sync
+= r
.tlbld
.eq(m_in
.tlbld
)
440 sync
+= r
.mmu_req
.eq(1)
442 sync
+= r
.req
.eq(d_in
)
443 sync
+= r
.tlbie
.eq(0)
444 sync
+= r
.doall
.eq(0)
445 sync
+= r
.tlbld
.eq(0)
446 sync
+= r
.mmu_req
.eq(0)
447 with m
.If(~
(r1
.full
& r0_full
)):
449 sync
+= r0_full
.eq(r
.req
.valid
)
451 def tlb_read(self
, m
, r0_stall
, tlb_valid_way
,
452 tlb_tag_way
, tlb_pte_way
, dtlb_valid_bits
,
453 dtlb_tags
, dtlb_ptes
):
455 Operates in the second cycle on the request latched in r0.req.
456 TLB updates write the entry at the end of the second cycle.
460 m_in
, d_in
= self
.m_in
, self
.d_in
462 index
= Signal(TLB_SET_BITS
)
463 addrbits
= Signal(TLB_SET_BITS
)
466 amax
= TLB_LG_PGSZ
+ TLB_SET_BITS
468 with m
.If(m_in
.valid
):
469 comb
+= addrbits
.eq(m_in
.addr
[amin
: amax
])
471 comb
+= addrbits
.eq(d_in
.addr
[amin
: amax
])
472 comb
+= index
.eq(addrbits
)
474 # If we have any op and the previous op isn't finished,
475 # then keep the same output for next cycle.
476 with m
.If(~r0_stall
):
477 sync
+= tlb_valid_way
.eq(dtlb_valid_bits
[index
])
478 sync
+= tlb_tag_way
.eq(dtlb_tags
[index
])
479 sync
+= tlb_pte_way
.eq(dtlb_ptes
[index
])
481 def maybe_tlb_plrus(self
, m
, r1
, tlb_plru_victim
, acc
, acc_en
, lru
):
482 """Generate TLB PLRUs
487 with m
.If(TLB_NUM_WAYS
> 1):
488 for i
in range(TLB_SET_SIZE
):
490 tlb_plru
= PLRU(TLB_WAY_BITS
)
491 setattr(m
.submodules
, "maybe_plru_%d" % i
, tlb_plru
)
492 tlb_plru_acc
= Signal(TLB_WAY_BITS
)
493 tlb_plru_acc_en
= Signal()
494 tlb_plru_out
= Signal(TLB_WAY_BITS
)
496 comb
+= tlb_plru
.acc
.eq(tlb_plru_acc
)
497 comb
+= tlb_plru
.acc_en
.eq(tlb_plru_acc_en
)
498 comb
+= tlb_plru
.lru
.eq(tlb_plru_out
)
501 with m
.If(r1
.tlb_hit_index
== i
):
502 comb
+= tlb_plru
.acc_en
.eq(r1
.tlb_hit
)
504 comb
+= tlb_plru
.acc_en
.eq(0)
505 comb
+= tlb_plru
.acc
.eq(r1
.tlb_hit_way
)
507 comb
+= tlb_plru_victim
[i
].eq(tlb_plru
.lru
)
509 def tlb_search(self
, m
, tlb_req_index
, r0
, r0_valid
,
510 tlb_valid_way
, tlb_tag_way
, tlb_hit_way
,
511 tlb_pte_way
, pte
, tlb_hit
, valid_ra
, perm_attr
, ra
):
516 hitway
= Signal(TLB_WAY_BITS
)
518 eatag
= Signal(TLB_EA_TAG_BITS
)
520 TLB_LG_END
= TLB_LG_PGSZ
+ TLB_SET_BITS
521 comb
+= tlb_req_index
.eq(r0
.req
.addr
[TLB_LG_PGSZ
: TLB_LG_END
])
522 comb
+= eatag
.eq(r0
.req
.addr
[TLB_LG_END
: 64 ])
524 for i
in range(TLB_NUM_WAYS
):
525 with m
.If(tlb_valid_way
[i
]
526 & read_tlb_tag(i
, tlb_tag_way
) == eatag
):
530 comb
+= tlb_hit
.eq(hit
& r0_valid
)
531 comb
+= tlb_hit_way
.eq(hitway
)
534 comb
+= pte
.eq(read_tlb_pte(hitway
, tlb_pte_way
))
537 comb
+= valid_ra
.eq(tlb_hit | ~r0
.req
.virt_mode
)
538 with m
.If(r0
.req
.virt_mode
):
539 comb
+= ra
.eq(Cat(Const(0, ROW_OFF_BITS
),
540 r0
.req
.addr
[ROW_OFF_BITS
:TLB_LG_PGSZ
],
541 pte
[TLB_LG_PGSZ
:REAL_ADDR_BITS
]))
542 comb
+= perm_attr
.eq(extract_perm_attr(pte
))
544 comb
+= ra
.eq(Cat(Const(0, ROW_OFF_BITS
),
545 r0
.req
.addr
[ROW_OFF_BITS
:REAL_ADDR_BITS
]))
547 comb
+= perm_attr
.reference
.eq(1)
548 comb
+= perm_attr
.changed
.eq(1)
549 comb
+= perm_attr
.priv
.eq(1)
550 comb
+= perm_attr
.nocache
.eq(0)
551 comb
+= perm_attr
.rd_perm
.eq(1)
552 comb
+= perm_attr
.wr_perm
.eq(1)
554 def tlb_update(self
, m
, r0_valid
, r0
, dtlb_valid_bits
, tlb_req_index
,
555 tlb_hit_way
, tlb_hit
, tlb_plru_victim
, tlb_tag_way
,
556 dtlb_tags
, tlb_pte_way
, dtlb_ptes
):
563 repl_way
= Signal(TLB_WAY_BITS
)
564 eatag
= Signal(TLB_EA_TAG_BITS
)
565 tagset
= Signal(TLB_TAG_WAY_BITS
)
566 pteset
= Signal(TLB_PTE_WAY_BITS
)
568 comb
+= tlbie
.eq(r0_valid
& r0
.tlbie
)
569 comb
+= tlbwe
.eq(r0_valid
& r0
.tlbld
)
571 with m
.If(tlbie
& r0
.doall
):
572 # clear all valid bits at once
573 for i
in range(TLB_SET_SIZE
):
574 sync
+= dtlb_valid_bits
[i
].eq(0)
578 sync
+= dtlb_valid_bits
[tlb_req_index
][tlb_hit_way
].eq(0)
581 comb
+= repl_way
.eq(tlb_hit_way
)
583 comb
+= repl_way
.eq(tlb_plru_victim
[tlb_req_index
])
584 comb
+= eatag
.eq(r0
.req
.addr
[TLB_LG_PGSZ
+ TLB_SET_BITS
:64])
585 comb
+= tagset
.eq(tlb_tag_way
)
586 sync
+= write_tlb_tag(repl_way
, tagset
, eatag
)
587 sync
+= dtlb_tags
[tlb_req_index
].eq(tagset
)
588 comb
+= pteset
.eq(tlb_pte_way
)
589 sync
+= write_tlb_pte(repl_way
, pteset
, r0
.req
.data
)
590 sync
+= dtlb_ptes
[tlb_req_index
].eq(pteset
)
591 sync
+= dtlb_valid_bits
[tlb_req_index
][repl_way
].eq(1)
593 def maybe_plrus(self
, r1
):
599 for i
in range(NUM_LINES
):
601 plru
= PLRU(TLB_WAY_BITS
)
602 setattr(m
.submodules
, "plru%d" % i
, plru
)
603 plru_acc
= Signal(WAY_BITS
)
604 plru_acc_en
= Signal()
605 plru_out
= Signal(WAY_BITS
)
607 comb
+= plru
.acc
.eq(plru_acc
)
608 comb
+= plru
.acc_en
.eq(plru_acc_en
)
609 comb
+= plru
.lru
.eq(plru_out
)
611 with m
.If(r1
.hit_index
== i
):
612 comb
+= plru_acc_en
.eq(r1
.cache_hit
)
614 comb
+= plru_acc
.eq(r1
.hit_way
)
615 comb
+= plru_victim
[i
].eq(plru_out
)
617 def cache_tag_read(self
, m
, r0_stall
, req_index
, cache_tag_set
, cache_tags
):
618 """Cache tag RAM read port
622 m_in
, d_in
= self
.m_in
, self
.d_in
624 index
= Signal(INDEX_BITS
)
627 comb
+= index
.eq(req_index
)
628 with m
.Elif(m_in
.valid
):
629 comb
+= index
.eq(get_index(m_in
.addr
))
631 comb
+= index
.eq(get_index(d_in
.addr
))
632 sync
+= cache_tag_set
.eq(cache_tags
[index
])
634 def dcache_request(self
, m
, r0
, ra
, req_index
, req_row
, req_tag
,
635 r0_valid
, r1
, cache_valid_bits
, replace_way
,
636 use_forward1_next
, use_forward2_next
,
637 req_hit_way
, plru_victim
, rc_ok
, perm_attr
,
638 valid_ra
, perm_ok
, access_ok
, req_op
, req_ok
,
639 r0_stall
, early_req_row
):
640 """Cache request parsing and hit detection
645 m_in
, d_in
= self
.m_in
, self
.d_in
648 hit_way
= Signal(WAY_BITS
)
654 s_tag
= Signal(TAG_BITS
)
655 s_pte
= Signal(TLB_PTE_BITS
)
656 s_ra
= Signal(REAL_ADDR_BITS
)
657 hit_set
= Signal(TLB_NUM_WAYS
)
658 hit_way_set
= HitWaySet()
659 rel_matches
= Signal(TLB_NUM_WAYS
)
662 # Extract line, row and tag from request
663 comb
+= req_index
.eq(get_index(r0
.req
.addr
))
664 comb
+= req_row
.eq(get_row(r0
.req
.addr
))
665 comb
+= req_tag
.eq(get_tag(ra
))
667 comb
+= go
.eq(r0_valid
& ~
(r0
.tlbie | r0
.tlbld
) & ~r1
.ls_error
)
669 # Test if pending request is a hit on any way
670 # In order to make timing in virtual mode,
671 # when we are using the TLB, we compare each
672 # way with each of the real addresses from each way of
673 # the TLB, and then decide later which match to use.
675 with m
.If(r0
.req
.virt_mode
):
676 comb
+= rel_matches
.eq(0)
677 for j
in range(TLB_NUM_WAYS
):
678 comb
+= s_pte
.eq(read_tlb_pte(j
, tlb_pte_way
))
679 comb
+= s_ra
.eq(Cat(r0
.req
.addr
[0:TLB_LG_PGSZ
],
680 s_pte
[TLB_LG_PGSZ
:REAL_ADDR_BITS
]))
681 comb
+= s_tag
.eq(get_tag(s_ra
))
683 for i
in range(NUM_WAYS
):
684 with m
.If(go
& cache_valid_bits
[req_index
][i
] &
685 read_tag(i
, cache_tag_set
) == s_tag
687 comb
+= hit_way_set
[j
].eq(i
)
689 comb
+= hit_set
[j
].eq(s_hit
)
690 with m
.If(s_tag
== r1
.reload_tag
):
691 comb
+= rel_matches
[j
].eq(1)
693 comb
+= is_hit
.eq(hit_set
[tlb_hit_way
])
694 comb
+= hit_way
.eq(hit_way_set
[tlb_hit_way
])
695 comb
+= rel_match
.eq(rel_matches
[tlb_hit_way
])
697 comb
+= s_tag
.eq(get_tag(r0
.req
.addr
))
698 for i
in range(NUM_WAYS
):
699 with m
.If(go
& cache_valid_bits
[req_index
][i
] &
700 read_tag(i
, cache_tag_set
) == s_tag
):
701 comb
+= hit_way
.eq(i
)
703 with m
.If(s_tag
== r1
.reload_tag
):
704 comb
+= rel_match
.eq(1)
705 comb
+= req_same_tag
.eq(rel_match
)
707 # See if the request matches the line currently being reloaded
708 with m
.If((r1
.state
== State
.RELOAD_WAIT_ACK
) &
709 (req_index
== r1
.store_index
) & rel_match
):
710 # For a store, consider this a hit even if the row isn't
711 # valid since it will be by the time we perform the store.
712 # For a load, check the appropriate row valid bit.
713 valid
= r1
.rows_valid
[req_row
% ROW_PER_LINE
]
714 comb
+= is_hit
.eq(~r0
.req
.load | valid
)
715 comb
+= hit_way
.eq(replace_way
)
717 # Whether to use forwarded data for a load or not
718 comb
+= use_forward1_next
.eq(0)
719 with m
.If((get_row(r1
.req
.real_addr
) == req_row
) &
720 (r1
.req
.hit_way
== hit_way
)):
721 # Only need to consider r1.write_bram here, since if we
722 # are writing refill data here, then we don't have a
723 # cache hit this cycle on the line being refilled.
724 # (There is the possibility that the load following the
725 # load miss that started the refill could be to the old
726 # contents of the victim line, since it is a couple of
727 # cycles after the refill starts before we see the updated
728 # cache tag. In that case we don't use the bypass.)
729 comb
+= use_forward1_next
.eq(r1
.write_bram
)
730 comb
+= use_forward2_next
.eq(0)
731 with m
.If((r1
.forward_row1
== req_row
) & (r1
.forward_way1
== hit_way
)):
732 comb
+= use_forward2_next
.eq(r1
.forward_valid1
)
734 # The way that matched on a hit
735 comb
+= req_hit_way
.eq(hit_way
)
737 # The way to replace on a miss
738 with m
.If(r1
.write_tag
):
739 replace_way
.eq(plru_victim
[r1
.store_index
])
741 comb
+= replace_way
.eq(r1
.store_way
)
743 # work out whether we have permission for this access
744 # NB we don't yet implement AMR, thus no KUAP
745 comb
+= rc_ok
.eq(perm_attr
.reference
746 & (r0
.req
.load | perm_attr
.changed
)
748 comb
+= perm_ok
.eq((r0
.req
.prive_mode | ~perm_attr
.priv
)
750 |
(r0
.req
.load
& perm_attr
.rd_perm
)
752 comb
+= access_ok
.eq(valid_ra
& perm_ok
& rc_ok
)
753 # Combine the request and cache hit status to decide what
754 # operation needs to be done
755 comb
+= nc
.eq(r0
.req
.nc | perm_attr
.nocache
)
756 comb
+= op
.eq(Op
.OP_NONE
)
758 with m
.If(~access_ok
):
759 comb
+= op
.eq(Op
.OP_BAD
)
760 with m
.Elif(cancel_store
):
761 comb
+= op
.eq(Op
.OP_STCX_FAIL
)
763 comb
+= opsel
.eq(Cat(is_hit
, nc
, r0
.req
.load
))
764 with m
.Switch(opsel
):
765 with m
.Case(Const(0b101, 3)):
766 comb
+= op
.eq(Op
.OP_LOAD_HIT
)
767 with m
.Case(Cosnt(0b100, 3)):
768 comb
+= op
.eq(Op
.OP_LOAD_MISS
)
769 with m
.Case(Const(0b110, 3)):
770 comb
+= op
.eq(Op
.OP_LOAD_NC
)
771 with m
.Case(Const(0b001, 3)):
772 comb
+= op
.eq(Op
.OP_STORE_HIT
)
773 with m
.Case(Const(0b000, 3)):
774 comb
+= op
.eq(Op
.OP_STORE_MISS
)
775 with m
.Case(Const(0b010, 3)):
776 comb
+= op
.eq(Op
.OP_STORE_MISS
)
777 with m
.Case(Const(0b011, 3)):
778 comb
+= op
.eq(Op
.OP_BAD
)
779 with m
.Case(Const(0b111, 3)):
780 comb
+= op
.eq(Op
.OP_BAD
)
782 comb
+= op
.eq(Op
.OP_NONE
)
783 comb
+= req_op
.eq(op
)
784 comb
+= req_go
.eq(go
)
786 # Version of the row number that is valid one cycle earlier
787 # in the cases where we need to read the cache data BRAM.
788 # If we're stalling then we need to keep reading the last
790 with m
.If(~r0_stall
):
791 with m
.If(m_in
.valid
):
792 comb
+= early_req_row
.eq(get_row(m_in
.addr
))
794 comb
+= early_req_row
.eq(get_row(d_in
.addr
))
796 comb
+= early_req_row
.eq(req_row
)
798 def reservation_comb(self
, m
, cancel_store
, set_rsrv
, clear_rsrv
,
799 r0_valid
, r0
, reservation
):
800 """Handle load-with-reservation and store-conditional instructions
805 with m
.If(r0_valid
& r0
.req
.reserve
):
807 # XXX generate alignment interrupt if address
808 # is not aligned XXX or if r0.req.nc = '1'
809 with m
.If(r0
.req
.load
):
810 comb
+= set_rsrv(1) # load with reservation
812 comb
+= clear_rsrv
.eq(1) # store conditional
813 with m
.If(~reservation
.valid | r0
.req
.addr
[LINE_OFF_BITS
:64]):
814 comb
+= cancel_store
.eq(1)
816 def reservation_reg(self
, m
, r0_valid
, access_ok
, clear_rsrv
,
822 with m
.If(r0_valid
& access_ok
):
823 with m
.If(clear_rsrv
):
824 sync
+= reservation
.valid
.eq(0)
825 with m
.Elif(set_rsrv
):
826 sync
+= reservation
.valid
.eq(1)
827 sync
+= reservation
.addr
.eq(r0
.req
.addr
[LINE_OFF_BITS
:64])
829 def writeback_control(self
, m
, r1
, cache_out
):
830 """Return data for loads & completion control logic
834 d_out
, m_out
= self
.d_out
, self
.m_out
836 data_out
= Signal(64)
837 data_fwd
= Signal(64)
839 # Use the bypass if are reading the row that was
840 # written 1 or 2 cycles ago, including for the
841 # slow_valid = 1 case (i.e. completing a load
842 # miss or a non-cacheable load).
843 with m
.If(r1
.use_forward1
):
844 comb
+= data_fwd
.eq(r1
.forward_data1
)
846 comb
+= data_fwd
.eq(r1
.forward_data2
)
848 comb
+= data_out
.eq(cache_out
[r1
.hit_way
])
851 with m
.If(r1
.forward_sel
[i
]):
852 dsel
= data_fwd
.word_select(i
, 8)
853 comb
+= data_out
.word_select(i
, 8).eq(dsel
)
855 comb
+= d_out
.valid
.eq(r1
.ls_valid
)
856 comb
+= d_out
.data
.eq(data_out
)
857 comb
+= d_out
.store_done
.eq(~r1
.stcx_fail
)
858 comb
+= d_out
.error
.eq(r1
.ls_error
)
859 comb
+= d_out
.cache_paradox
.eq(r1
.cache_paradox
)
862 comb
+= m_out
.done
.eq(r1
.mmu_done
)
863 comb
+= m_out
.err
.eq(r1
.mmu_error
)
864 comb
+= m_out
.data
.eq(data_out
)
866 # We have a valid load or store hit or we just completed
867 # a slow op such as a load miss, a NC load or a store
869 # Note: the load hit is delayed by one cycle. However it
870 # can still not collide with r.slow_valid (well unless I
871 # miscalculated) because slow_valid can only be set on a
872 # subsequent request and not on its first cycle (the state
873 # machine must have advanced), which makes slow_valid
874 # at least 2 cycles from the previous hit_load_valid.
876 # Sanity: Only one of these must be set in any given cycle
878 if False: # TODO: need Display to get this to work
879 assert (r1
.slow_valid
& r1
.stcx_fail
) != 1, \
880 "unexpected slow_valid collision with stcx_fail"
882 assert ((r1
.slow_valid | r1
.stcx_fail
) | r1
.hit_load_valid
) != 1, \
883 "unexpected hit_load_delayed collision with slow_valid"
885 with m
.If(~r1
._mmu_req
):
886 # Request came from loadstore1...
887 # Load hit case is the standard path
888 with m
.If(r1
.hit_load_valid
):
889 #Display(f"completing load hit data={data_out}")
892 # error cases complete without stalling
893 with m
.If(r1
.ls_error
):
894 # Display("completing ld/st with error")
897 # Slow ops (load miss, NC, stores)
898 with m
.If(r1
.slow_valid
):
899 #Display(f"completing store or load miss data={data_out}")
903 # Request came from MMU
904 with m
.If(r1
.hit_load_valid
):
905 # Display(f"completing load hit to MMU, data={m_out.data}")
907 # error cases complete without stalling
908 with m
.If(r1
.mmu_error
):
909 #Display("combpleting MMU ld with error")
912 # Slow ops (i.e. load miss)
913 with m
.If(r1
.slow_valid
):
914 #Display("completing MMU load miss, data={m_out.data}")
917 def rams(self
, m
, r1
):
919 Generate a cache RAM for each way. This handles the normal
920 reads, writes from reloads and the special store-hit update
923 Note: the BRAMs have an extra read buffer, meaning the output
924 is pipelined an extra cycle. This differs from the
925 icache. The writeback logic needs to take that into
926 account by using 1-cycle delayed signals for load hits.
931 for i
in range(NUM_WAYS
):
933 rd_addr
= Signal(ROW_BITS
)
935 wr_addr
= Signal(ROW_BITS
)
936 wr_data
= Signal(WB_DATA_BITS
)
937 wr_sel
= Signal(ROW_SIZE
)
938 wr_sel_m
= Signal(ROW_SIZE
)
939 _d_out
= Signal(WB_DATA_BITS
)
941 way
= CacheRam(ROW_BITS
, WB_DATA_BITS
, True)
942 setattr(m
.submodules
, "cacheram_%d" % i
, way
)
944 comb
+= way
.rd_en
.eq(do_read
)
945 comb
+= way
.rd_addr
.eq(rd_addr
)
946 comb
+= _d_out
.eq(way
.rd_data
)
947 comb
+= way
.wr_sel
.eq(wr_sel_m
)
948 comb
+= way
.wr_addr
.eq(wr_addr
)
949 comb
+= way
.wr_data
.eq(wr_data
)
952 comb
+= do_read
.eq(1)
953 comb
+= rd_addr
.eq(early_req_row
)
954 comb
+= cache_out
[i
].eq(_d_out
)
958 # Defaults to wishbone read responses (cache refill)
960 # For timing, the mux on wr_data/sel/addr is not
961 # dependent on anything other than the current state.
963 with m
.If(r1
.write_bram
):
964 # Write store data to BRAM. This happens one
965 # cycle after the store is in r0.
966 comb
+= wr_data
.eq(r1
.req
.data
)
967 comb
+= wr_sel
.eq(r1
.req
.byte_sel
)
968 comb
+= wr_addr
.eq(get_row(r1
.req
.real_addr
))
970 with m
.If(i
== r1
.req
.hit_way
):
971 comb
+= do_write
.eq(1)
973 # Otherwise, we might be doing a reload or a DCBZ
975 comb
+= wr_data
.eq(0)
977 comb
+= wr_data
.eq(wb_in
.dat
)
978 comb
+= wr_addr
.eq(r1
.store_row
)
979 comb
+= wr_sel
.eq(~
0) # all 1s
981 with m
.If((r1
.state
== State
.RELOAD_WAIT_ACK
)
982 & wb_in
.ack
& (replace_way
== i
)):
983 comb
+= do_write
.eq(1)
985 # Mask write selects with do_write since BRAM
986 # doesn't have a global write-enable
988 comb
+= wr_sel_m
.eq(wr_sel
)
990 # Cache hit synchronous machine for the easy case.
991 # This handles load hits.
992 # It also handles error cases (TLB miss, cache paradox)
993 def dcache_fast_hit(self
, m
, req_op
, r0_valid
, r1
):
998 with m
.If(req_op
!= Op
.OP_NONE
):
999 #Display(f"op:{req_op} addr:{r0.req.addr} nc: {r0.req.nc}" \
1000 # f"idx:{req_index} tag:{req_tag} way: {req_hit_way}"
1004 with m
.If(r0_valid
):
1005 sync
+= r1
.mmu_req
.eq(r0
.mmu_req
)
1007 # Fast path for load/store hits.
1008 # Set signals for the writeback controls.
1009 sync
+= r1
.hit_way
.eq(req_hit_way
)
1010 sync
+= r1
.hit_index
.eq(req_index
)
1012 with m
.If(req_op
== Op
.OP_LOAD_HIT
):
1013 sync
+= r1
.hit_load_valid
.eq(1)
1015 sync
+= r1
.hit_load_valid
.eq(0)
1017 with m
.If((req_op
== Op
.OP_LOAD_HIT
) |
(req_op
== Op
.OP_STORE_HIT
)):
1018 sync
+= r1
.cache_hit
.eq(1)
1020 sync
+= r1
.cache_hit
.eq(0)
1022 with m
.If(req_op
== Op
.OP_BAD
):
1023 # Display(f"Signalling ld/st error valid_ra={valid_ra}"
1024 # f"rc_ok={rc_ok} perm_ok={perm_ok}"
1025 sync
+= r1
.ls_error
.eq(~r0
.mmu_req
)
1026 sync
+= r1
.mmu_error
.eq(r0
.mmu_req
)
1027 sync
+= r1
.cache_paradox
.eq(access_ok
)
1030 sync
+= r1
.ls_error
.eq(0)
1031 sync
+= r1
.mmu_error
.eq(0)
1032 sync
+= r1
.cache_paradox
.eq(0)
1034 with m
.If(req_op
== Op
.OP_STCX_FAIL
):
1037 sync
+= r1
.stcx_fail
.eq(0)
1039 # Record TLB hit information for updating TLB PLRU
1040 sync
+= r1
.tlb_hit
.eq(tlb_hit
)
1041 sync
+= r1
.tlb_hit_way
.eq(tlb_hit_way
)
1042 sync
+= r1
.tlb_hit_index
.eq(tlb_req_index
)
1044 # Memory accesses are handled by this state machine:
1046 # * Cache load miss/reload (in conjunction with "rams")
1047 # * Load hits for non-cachable forms
1048 # * Stores (the collision case is handled in "rams")
1050 # All wishbone requests generation is done here.
1051 # This machine operates at stage 1.
1052 def dcache_slow(self
, m
, r1
, use_forward1_next
, cache_valid_bits
, r0
,
1053 r0_valid
, req_op
, cache_tag
, req_go
, ra
):
1059 req
= MemAccessRequest()
1061 adjust_acks
= Signal(3)
1063 sync
+= r1
.use_forward1
.eq(use_forward1_next
)
1064 sync
+= r1
.forward_sel
.eq(0)
1066 with m
.If(use_forward1_next
):
1067 sync
+= r1
.forward_sel
.eq(r1
.req
.byte_sel
)
1068 with m
.Elif(use_forward2_next
):
1069 sync
+= r1
.forward_sel
.eq(r1
.forward_sel1
)
1071 sync
+= r1
.forward_data2
.eq(r1
.forward_data1
)
1072 with m
.If(r1
.write_bram
):
1073 sync
+= r1
.forward_data1
.eq(r1
.req
.data
)
1074 sync
+= r1
.forward_sel1
.eq(r1
.req
.byte_sel
)
1075 sync
+= r1
.forward_way1
.eq(r1
.req
.hit_way
)
1076 sync
+= r1
.forward_row1
.eq(get_row(r1
.req
.real_addr
))
1077 sync
+= r1
.forward_valid1
.eq(1)
1080 sync
+= r1
.forward_data1
.eq(0)
1082 sync
+= r1
.forward_data1
.eq(wb_in
.dat
)
1083 sync
+= r1
.forward_sel1
.eq(~
0) # all 1s
1084 sync
+= r1
.forward_way1
.eq(replace_way
)
1085 sync
+= r1
.forward_row1
.eq(r1
.store_row
)
1086 sync
+= r1
.forward_valid1
.eq(0)
1088 # One cycle pulses reset
1089 sync
+= r1
.slow_valid
.eq(0)
1090 sync
+= r1
.write_bram
.eq(0)
1091 sync
+= r1
.inc_acks
.eq(0)
1092 sync
+= r1
.dec_acks
.eq(0)
1094 sync
+= r1
.ls_valid
.eq(0)
1095 # complete tlbies and TLB loads in the third cycle
1096 sync
+= r1
.mmu_done
.eq(r0_valid
& (r0
.tlbie | r0
.tlbld
))
1098 with m
.If((req_op
== Op
.OP_LOAD_HIT
)
1099 |
(req_op
== Op
.OP_STCX_FAIL
)):
1100 with m
.If(~r0
.mmu_req
):
1101 sync
+= r1
.ls_valid
.eq(1)
1103 sync
+= r1
.mmu_done
.eq(1)
1105 with m
.If(r1
.write_tag
):
1106 # Store new tag in selected way
1107 for i
in range(NUM_WAYS
):
1108 with m
.If(i
== replace_way
):
1109 idx
= r1
.store_index
1110 trange
= range(i
* TAG_WIDTH
, (i
+1) * TAG_WIDTH
)
1111 sync
+= cache_tag
[idx
][trange
].eq(r1
.reload_tag
)
1112 sync
+= r1
.store_way
.eq(replace_way
)
1113 sync
+= r1
.write_tag
.eq(0)
1115 # Take request from r1.req if there is one there,
1116 # else from req_op, ra, etc.
1118 comb
+= req
.eq(r1
.req
)
1120 comb
+= req
.op
.eq(req_op
)
1121 comb
+= req
.valid
.eq(req_go
)
1122 comb
+= req
.mmu_req
.eq(r0
.mmu_req
)
1123 comb
+= req
.dcbz
.eq(r0
.req
.dcbz
)
1124 comb
+= req
.real_addr
.eq(ra
)
1126 with m
.If(~r0
.req
.dcbz
):
1127 comb
+= req
.data
.eq(r0
.req
.data
)
1129 comb
+= req
.data
.eq(0)
1131 # Select all bytes for dcbz
1132 # and for cacheable loads
1133 with m
.If(r0
.req
.dcbz |
(r0
.req
.load
& ~r0
.req
.nc
)):
1134 comb
+= req
.byte_sel
.eq(~
0) # all 1s
1136 comb
+= req
.byte_sel
.eq(r0
.req
.byte_sel
)
1137 comb
+= req
.hit_way
.eq(req_hit_way
)
1138 comb
+= req
.same_tag
.eq(req_same_tag
)
1140 # Store the incoming request from r0,
1141 # if it is a slow request
1142 # Note that r1.full = 1 implies req_op = OP_NONE
1143 with m
.If((req_op
== Op
.OP_LOAD_MISS
)
1144 |
(req_op
== Op
.OP_LOAD_NC
)
1145 |
(req_op
== Op
.OP_STORE_MISS
)
1146 |
(req_op
== Op
.OP_STORE_HIT
)):
1148 sync
+= r1
.full
.eq(1)
1150 # Main state machine
1151 with m
.Switch(r1
.state
):
1153 with m
.Case(State
.IDLE
):
1154 # XXX check 'left downto. probably means len(r1.wb.adr)
1155 # r1.wb.adr <= req.real_addr(
1156 # r1.wb.adr'left downto 0
1158 sync
+= r1
.wb
.adr
.eq(req
.real_addr
[0:r1
.wb
.adr
])
1159 sync
+= r1
.wb
.sel
.eq(req
.byte_sel
)
1160 sync
+= r1
.wb
.dat
.eq(req
.data
)
1161 sync
+= r1
.dcbz
.eq(req
.dcbz
)
1163 # Keep track of our index and way
1164 # for subsequent stores.
1165 sync
+= r1
.store_index
.eq(get_index(req
.real_addr
))
1166 sync
+= r1
.store_row
.eq(get_row(req
.real_addr
))
1167 sync
+= r1
.end_row_ix
.eq(
1168 get_row_of_line(get_row(req
.real_addr
))
1170 sync
+= r1
.reload_tag
.eq(get_tag(req
.real_addr
))
1171 sync
+= r1
.req
.same_tag
.eq(1)
1173 with m
.If(req
.op
== Op
.OP_STORE_HIT
):
1174 sync
+= r1
.store_way
.eq(req
.hit_way
)
1176 # Reset per-row valid bits,
1177 # ready for handling OP_LOAD_MISS
1178 for i
in range(ROW_PER_LINE
):
1179 sync
+= r1
.rows_valid
[i
].eq(0)
1181 with m
.Switch(req
.op
):
1182 with m
.Case(Op
.OP_LOAD_HIT
):
1183 # stay in IDLE state
1186 with m
.Case(Op
.OP_LOAD_MISS
):
1187 #Display(f"cache miss real addr:" \
1188 # f"{req_real_addr}" \
1189 # f" idx:{get_index(req_real_addr)}" \
1190 # f" tag:{get_tag(req.real_addr)}")
1193 # Start the wishbone cycle
1194 sync
+= r1
.wb
.we
.eq(0)
1195 sync
+= r1
.wb
.cyc
.eq(1)
1196 sync
+= r1
.wb
.stb
.eq(1)
1198 # Track that we had one request sent
1199 sync
+= r1
.state
.eq(State
.RELOAD_WAIT_ACK
)
1200 sync
+= r1
.write_tag
.eq(1)
1202 with m
.Case(Op
.OP_LOAD_NC
):
1203 sync
+= r1
.wb
.cyc
.eq(1)
1204 sync
+= r1
.wb
.stb
.eq(1)
1205 sync
+= r1
.wb
.we
.eq(0)
1206 sync
+= r1
.state
.eq(State
.NC_LOAD_WAIT_ACK
)
1208 with m
.Case(Op
.OP_STORE_HIT
, Op
.OP_STORE_MISS
):
1209 with m
.If(~req
.bcbz
):
1210 sync
+= r1
.state
.eq(State
.STORE_WAIT_ACK
)
1211 sync
+= r1
.acks_pending
.eq(1)
1212 sync
+= r1
.full
.eq(0)
1213 sync
+= r1
.slow_valid
.eq(1)
1215 with m
.If(~req
.mmu_req
):
1216 sync
+= r1
.ls_valid
.eq(1)
1218 sync
+= r1
.mmu_done
.eq(1)
1220 with m
.If(req
.op
== Op
.OP_STORE_HIT
):
1221 sync
+= r1
.write_bram
.eq(1)
1223 sync
+= r1
.state
.eq(Op
.RELOAD_WAIT_ACK
)
1225 with m
.If(req
.op
== Op
.OP_STORE_MISS
):
1226 sync
+= r1
.write_tag
.eq(1)
1228 sync
+= r1
.wb
.we
.eq(1)
1229 sync
+= r1
.wb
.cyc
.eq(1)
1230 sync
+= r1
.wb
.stb
.eq(1)
1232 # OP_NONE and OP_BAD do nothing
1233 # OP_BAD & OP_STCX_FAIL were
1234 # handled above already
1235 with m
.Case(Op
.OP_NONE
):
1237 with m
.Case(OP_BAD
):
1239 with m
.Case(OP_STCX_FAIL
):
1242 with m
.Case(State
.RELOAD_WAIT_ACK
):
1243 # Requests are all sent if stb is 0
1244 comb
+= stbs_done
.eq(~r1
.wb
.stb
)
1246 with m
.If(~wb_in
.stall
& ~stbs_done
):
1247 # That was the last word?
1248 # We are done sending.
1249 # Clear stb and set stbs_done
1250 # so we can handle an eventual
1251 # last ack on the same cycle.
1252 with m
.If(is_last_row_addr(
1253 r1
.wb
.adr
, r1
.end_row_ix
)):
1254 sync
+= r1
.wb
.stb
.eq(0)
1255 comb
+= stbs_done
.eq(0)
1257 # Calculate the next row address
1258 sync
+= r1
.wb
.adr
.eq(next_row_addr(r1
.wb
.adr
))
1260 # Incoming acks processing
1261 sync
+= r1
.forward_valid1
.eq(wb_in
.ack
)
1262 with m
.If(wb_in
.ack
):
1263 # XXX needs an Array bit-accessor here
1264 sync
+= r1
.rows_valid
[r1
.store_row
% ROW_PER_LINE
].eq(1)
1266 # If this is the data we were looking for,
1267 # we can complete the request next cycle.
1268 # Compare the whole address in case the
1269 # request in r1.req is not the one that
1270 # started this refill.
1271 with m
.If(r1
.full
& r1
.req
.same_tag
&
1272 ((r1
.dcbz
& r1
.req
.dcbz
) |
1273 (~r1
.dcbz
& (r1
.req
.op
== Op
.OP_LOAD_MISS
))) &
1274 (r1
.store_row
== get_row(r1
.req
.real_addr
))):
1275 sync
+= r1
.full
.eq(0)
1276 sync
+= r1
.slow_valid
.eq(1)
1277 with m
.If(~r1
.mmu_req
):
1278 sync
+= r1
.ls_valid
.eq(1)
1280 sync
+= r1
.mmu_done
.eq(1)
1281 sync
+= r1
.forward_sel
.eq(~
0) # all 1s
1282 sync
+= r1
.use_forward1
.eq(1)
1284 # Check for completion
1285 with m
.If(stbs_done
& is_last_row(r1
.store_row
,
1287 # Complete wishbone cycle
1288 sync
+= r1
.wb
.cyc
.eq(0)
1290 # Cache line is now valid
1291 cv
= cache_valid_bits
[r1
.store_index
]
1292 sync
+= cv
[r1
.store_way
].eq(1)
1293 sync
+= r1
.state
.eq(State
.IDLE
)
1295 # Increment store row counter
1296 sync
+= r1
.store_row
.eq(next_row(r1
.store_row
))
1298 with m
.Case(State
.STORE_WAIT_ACK
):
1299 comb
+= stbs_done
.eq(~r1
.wb
.stb
)
1300 comb
+= acks
.eq(r1
.acks_pending
)
1302 with m
.If(r1
.inc_acks
!= r1
.dec_acks
):
1303 with m
.If(r1
.inc_acks
):
1304 comb
+= adjust_acks
.eq(acks
+ 1)
1306 comb
+= adjust_acks
.eq(acks
- 1)
1308 comb
+= adjust_acks
.eq(acks
)
1310 sync
+= r1
.acks_pending
.eq(adjust_acks
)
1312 # Clear stb when slave accepted request
1313 with m
.If(~wb_in
.stall
):
1314 # See if there is another store waiting
1315 # to be done which is in the same real page.
1316 with m
.If(req
.valid
):
1317 ra
= req
.real_addr
[0:SET_SIZE_BITS
]
1318 sync
+= r1
.wb
.adr
[0:SET_SIZE_BITS
].eq(ra
)
1319 sync
+= r1
.wb
.dat
.eq(req
.data
)
1320 sync
+= r1
.wb
.sel
.eq(req
.byte_sel
)
1322 with m
.Elif((adjust_acks
< 7) & req
.same_tag
&
1323 ((req
.op
== Op
.Op_STORE_MISS
)
1324 |
(req
.op
== Op
.OP_SOTRE_HIT
))):
1325 sync
+= r1
.wb
.stb
.eq(1)
1326 comb
+= stbs_done
.eq(0)
1328 with m
.If(req
.op
== Op
.OP_STORE_HIT
):
1329 sync
+= r1
.write_bram
.eq(1)
1330 sync
+= r1
.full
.eq(0)
1331 sync
+= r1
.slow_valid
.eq(1)
1333 # Store requests never come from the MMU
1334 sync
+= r1
.ls_valid
.eq(1)
1335 comb
+= stbs_done
.eq(0)
1336 sync
+= r1
.inc_acks
.eq(1)
1338 sync
+= r1
.wb
.stb
.eq(0)
1339 comb
+= stbs_done
.eq(1)
1341 # Got ack ? See if complete.
1342 with m
.If(wb_in
.ack
):
1343 with m
.If(stbs_done
& (adjust_acks
== 1)):
1344 sync
+= r1
.state
.eq(State
.IDLE
)
1345 sync
+= r1
.wb
.cyc
.eq(0)
1346 sync
+= r1
.wb
.stb
.eq(0)
1347 sync
+= r1
.dec_acks
.eq(1)
1349 with m
.Case(State
.NC_LOAD_WAIT_ACK
):
1350 # Clear stb when slave accepted request
1351 with m
.If(~wb_in
.stall
):
1352 sync
+= r1
.wb
.stb
.eq(0)
1354 # Got ack ? complete.
1355 with m
.If(wb_in
.ack
):
1356 sync
+= r1
.state
.eq(State
.IDLE
)
1357 sync
+= r1
.full
.eq(0)
1358 sync
+= r1
.slow_valid
.eq(1)
1360 with m
.If(~r1
.mmu_req
):
1361 sync
+= r1
.ls_valid
.eq(1)
1363 sync
+= r1
.mmu_done
.eq(1)
1365 sync
+= r1
.forward_sel
.eq(~
0) # all 1s
1366 sync
+= r1
.use_forward1
.eq(1)
1367 sync
+= r1
.wb
.cyc
.eq(0)
1368 sync
+= r1
.wb
.stb
.eq(0)
1370 def dcache_log(self
, m
, r1
, valid_ra
, tlb_hit_way
, stall_out
):
1373 d_out
, wb_in
, log_out
= self
.d_out
, self
.wb_in
, self
.log_out
1375 sync
+= log_out
.eq(Cat(r1
.state
[:3], valid_ra
, tlb_hit_way
[:3],
1376 stall_out
, req_op
[:3], d_out
.valid
, d_out
.error
,
1377 r1
.wb
.cyc
, r1
.wb
.stb
, wb_in
.ack
, wb_in
.stall
,
1380 def elaborate(self
, platform
):
1385 # Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
1386 cache_tags
= CacheTagArray()
1387 cache_tag_set
= Signal(TAG_RAM_WIDTH
)
1388 cache_valid_bits
= CacheValidBitsArray()
1390 # TODO attribute ram_style : string;
1391 # TODO attribute ram_style of cache_tags : signal is "distributed";
1393 """note: these are passed to nmigen.hdl.Memory as "attributes".
1394 don't know how, just that they are.
1396 dtlb_valid_bits
= TLBValidBitsArray()
1397 dtlb_tags
= TLBTagsArray()
1398 dtlb_ptes
= TLBPtesArray()
1399 # TODO attribute ram_style of
1400 # dtlb_tags : signal is "distributed";
1401 # TODO attribute ram_style of
1402 # dtlb_ptes : signal is "distributed";
1409 reservation
= Reservation()
1411 # Async signals on incoming request
1412 req_index
= Signal(INDEX_BITS
)
1413 req_row
= Signal(ROW_BITS
)
1414 req_hit_way
= Signal(WAY_BITS
)
1415 req_tag
= Signal(TAG_BITS
)
1417 req_data
= Signal(64)
1418 req_same_tag
= Signal()
1421 early_req_row
= Signal(ROW_BITS
)
1423 cancel_store
= Signal()
1425 clear_rsrv
= Signal()
1430 use_forward1_next
= Signal()
1431 use_forward2_next
= Signal()
1433 cache_out
= CacheRamOut()
1435 plru_victim
= PLRUOut()
1436 replace_way
= Signal(WAY_BITS
)
1438 # Wishbone read/write/cache write formatting signals
1442 tlb_tag_way
= Signal(TLB_TAG_WAY_BITS
)
1443 tlb_pte_way
= Signal(TLB_PTE_WAY_BITS
)
1444 tlb_valid_way
= Signal(TLB_NUM_WAYS
)
1445 tlb_req_index
= Signal(TLB_SET_BITS
)
1447 tlb_hit_way
= Signal(TLB_WAY_BITS
)
1448 pte
= Signal(TLB_PTE_BITS
)
1449 ra
= Signal(REAL_ADDR_BITS
)
1451 perm_attr
= PermAttr()
1454 access_ok
= Signal()
1456 tlb_plru_victim
= TLBPLRUOut()
1458 # we don't yet handle collisions between loadstore1 requests
1460 comb
+= self
.m_out
.stall
.eq(0)
1462 # Hold off the request in r0 when r1 has an uncompleted request
1463 comb
+= r0_stall
.eq(r0_full
& r1
.full
)
1464 comb
+= r0_valid
.eq(r0_full
& ~r1
.full
)
1465 comb
+= self
.stall_out
.eq(r0_stall
)
1467 # Wire up wishbone request latch out of stage 1
1468 comb
+= self
.wb_out
.eq(r1
.wb
)
1470 # call sub-functions putting everything together, using shared
1471 # signals established above
1472 self
.stage_0(m
, r0
, r1
, r0_full
)
1473 self
.tlb_read(m
, r0_stall
, tlb_valid_way
,
1474 tlb_tag_way
, tlb_pte_way
, dtlb_valid_bits
,
1475 dtlb_tags
, dtlb_ptes
)
1476 self
.tlb_search(m
, tlb_req_index
, r0
, r0_valid
,
1477 tlb_valid_way
, tlb_tag_way
, tlb_hit_way
,
1478 tlb_pte_way
, pte
, tlb_hit
, valid_ra
, perm_attr
, ra
)
1479 self
.tlb_update(m
, r0_valid
, r0
, dtlb_valid_bits
, tlb_req_index
,
1480 tlb_hit_way
, tlb_hit
, tlb_plru_victim
, tlb_tag_way
,
1481 dtlb_tags
, tlb_pte_way
, dtlb_ptes
)
1482 self
.maybe_plrus(r1
)
1483 self
.cache_tag_read(m
, r0_stall
, req_index
, cache_tag_set
, cache_tags
)
1484 self
.dcache_request(m
, r0
, ra
, req_index
, req_row
, req_tag
,
1485 r0_valid
, r1
, cache_valid_bits
, replace_way
,
1486 use_forward1_next
, use_forward2_next
,
1487 req_hit_way
, plru_victim
, rc_ok
, perm_attr
,
1488 valid_ra
, perm_ok
, access_ok
, req_op
, req_ok
,
1489 r0_stall
, early_req_row
)
1490 self
.reservation_comb(m
, cancel_store
, set_rsrv
, clear_rsrv
,
1491 r0_valid
, r0
, reservation
)
1492 self
.reservation_reg(m
, r0_valid
, access_ok
, clear_rsrv
,
1494 self
.writeback_control(m
, r1
, cache_out
)
1496 self
.dcache_fast_hit(m
, req_op
, r0_valid
, r1
)
1497 self
.dcache_slow(m
, r1
, use_forward1_next
, cache_valid_bits
, r0
,
1498 r0_valid
, req_op
, cache_tag
, req_go
, ra
)
1499 #self.dcache_log(m, r1, valid_ra, tlb_hit_way, stall_out)
1504 # entity dcache_tb is
1507 # architecture behave of dcache_tb is
1508 # signal clk : std_ulogic;
1509 # signal rst : std_ulogic;
1511 # signal d_in : Loadstore1ToDcacheType;
1512 # signal d_out : DcacheToLoadstore1Type;
1514 # signal m_in : MmuToDcacheType;
1515 # signal m_out : DcacheToMmuType;
1517 # signal wb_bram_in : wishbone_master_out;
1518 # signal wb_bram_out : wishbone_slave_out;
1520 # constant clk_period : time := 10 ns;
1522 # dcache0: entity work.dcache
1535 # wishbone_out => wb_bram_in,
1536 # wishbone_in => wb_bram_out
1539 # -- BRAM Memory slave
1540 # bram0: entity work.wishbone_bram_wrapper
1542 # MEMORY_SIZE => 1024,
1543 # RAM_INIT_FILE => "icache_test.bin"
1548 # wishbone_in => wb_bram_in,
1549 # wishbone_out => wb_bram_out
1552 # clk_process: process
1555 # wait for clk_period/2;
1557 # wait for clk_period/2;
1560 # rst_process: process
1563 # wait for 2*clk_period;
1571 # d_in.valid <= '0';
1574 # d_in.addr <= (others => '0');
1575 # d_in.data <= (others => '0');
1576 # m_in.valid <= '0';
1577 # m_in.addr <= (others => '0');
1578 # m_in.pte <= (others => '0');
1580 # wait for 4*clk_period;
1581 # wait until rising_edge(clk);
1583 # -- Cacheable read of address 4
1586 # d_in.addr <= x"0000000000000004";
1587 # d_in.valid <= '1';
1588 # wait until rising_edge(clk);
1589 # d_in.valid <= '0';
1591 # wait until rising_edge(clk) and d_out.valid = '1';
1592 # assert d_out.data = x"0000000100000000"
1593 # report "data @" & to_hstring(d_in.addr) &
1594 # "=" & to_hstring(d_out.data) &
1595 # " expected 0000000100000000"
1597 # -- wait for clk_period;
1599 # -- Cacheable read of address 30
1602 # d_in.addr <= x"0000000000000030";
1603 # d_in.valid <= '1';
1604 # wait until rising_edge(clk);
1605 # d_in.valid <= '0';
1607 # wait until rising_edge(clk) and d_out.valid = '1';
1608 # assert d_out.data = x"0000000D0000000C"
1609 # report "data @" & to_hstring(d_in.addr) &
1610 # "=" & to_hstring(d_out.data) &
1611 # " expected 0000000D0000000C"
1614 # -- Non-cacheable read of address 100
1617 # d_in.addr <= x"0000000000000100";
1618 # d_in.valid <= '1';
1619 # wait until rising_edge(clk);
1620 # d_in.valid <= '0';
1621 # wait until rising_edge(clk) and d_out.valid = '1';
1622 # assert d_out.data = x"0000004100000040"
1623 # report "data @" & to_hstring(d_in.addr) &
1624 # "=" & to_hstring(d_out.data) &
1625 # " expected 0000004100000040"
1628 # wait until rising_edge(clk);
1629 # wait until rising_edge(clk);
1630 # wait until rising_edge(clk);
1631 # wait until rising_edge(clk);
1636 def dcache_sim(dut
):
1638 yield dut
.d_in
.valid
.eq(0)
1639 yield dut
.d_in
.load
.eq(0)
1640 yield dut
.d_in
.nc
.eq(0)
1641 yield dut
.d_in
.adrr
.eq(0)
1642 yield dut
.d_in
.data
.eq(0)
1643 yield dut
.m_in
.valid
.eq(0)
1644 yield dut
.m_in
.addr
.eq(0)
1645 yield dut
.m_in
.pte
.eq(0)
1646 # wait 4 * clk_period
1651 # wait_until rising_edge(clk)
1653 # Cacheable read of address 4
1654 yield dut
.d_in
.load
.eq(1)
1655 yield dut
.d_in
.nc
.eq(0)
1656 yield dut
.d_in
.addr
.eq(Const(0x0000000000000004, 64))
1657 yield dut
.d_in
.valid
.eq(1)
1658 # wait-until rising_edge(clk)
1660 yield dut
.d_in
.valid
.eq(0)
1662 while not (yield dut
.d_out
.valid
):
1664 assert dut
.d_out
.data
== 0x0000000100000000, \
1665 f
"data @ {dut.d_in.addr}={dut.d_in.data} expected 0000000100000000"
1668 # Cacheable read of address 30
1669 yield dut
.d_in
.load
.eq(1)
1670 yield dut
.d_in
.nc
.eq(0)
1671 yield dut
.d_in
.addr
.eq(Const(0x0000000000000030, 64))
1672 yield dut
.d_in
.valid
.eq(1)
1674 yield dut
.d_in
.valid
.eq(0)
1676 while not (yield dut
.d_out
.valid
):
1678 assert dut
.d_out
.data
== 0x0000000D0000000C, \
1679 f
"data @{dut.d_in.addr}={dut.d_out.data} expected 0000000D0000000C"
1681 # Non-cacheable read of address 100
1682 yield dut
.d_in
.load
.eq(1)
1683 yield dut
.d_in
.nc
.eq(1)
1684 yield dut
.d_in
.addr
.eq(Const(0x0000000000000100, 64))
1685 yield dut
.d_in
.valid
.eq(1)
1687 yield dut
.d_in
.valid
.eq(0)
1689 while not (yield dut
.d_out
.valid
):
1691 assert dut
.d_out
.data
== 0x0000004100000040, \
1692 f
"data @ {dut.d_in.addr}={dut.d_out.data} expected 0000004100000040"
1702 vl
= rtlil
.convert(dut
, ports
=[])
1703 with
open("test_dcache.il", "w") as f
:
1706 run_simulation(dut
, dcache_sim(), vcd_name
='test_dcache.vcd')
1708 if __name__
== '__main__':