dcache.py replace functions that return signals with constants, generate
[soc.git] / src / soc / experiment / dcache.py
1 """Dcache
2
3 based on Anton Blanchard microwatt dcache.vhdl
4
5 """
6
7 from enum import Enum, unique
8
9 from nmigen import Module, Signal, Elaboratable,
10 Cat, Repl
11 from nmigen.cli import main
12 from nmigen.iocontrol import RecordObject
13 from nmigen.util import log2_int
14
15 from experiment.mem_types import LoadStore1ToDcacheType,
16 DcacheToLoadStore1Type,
17 MmuToDcacheType,
18 DcacheToMmuType
19
20 from experiment.wb_types import WB_ADDR_BITS, WB_DATA_BITS, WB_SEL_BITS,
21 WBAddrType, WBDataType, WBSelType,
22 WbMasterOut, WBSlaveOut, WBMasterOutVector,
23 WBSlaveOutVector, WBIOMasterOut,
24 WBIOSlaveOut
25
26 # --
27 # -- Set associative dcache write-through
28 # --
29 # -- TODO (in no specific order):
30 # --
31 # -- * See list in icache.vhdl
32 # -- * Complete load misses on the cycle when WB data comes instead of
33 # -- at the end of line (this requires dealing with requests coming in
34 # -- while not idle...)
35 # --
36 # library ieee;
37 # use ieee.std_logic_1164.all;
38 # use ieee.numeric_std.all;
39 #
40 # library work;
41 # use work.utils.all;
42 # use work.common.all;
43 # use work.helpers.all;
44 # use work.wishbone_types.all;
45 #
46 # entity dcache is
47 class Dcache(Elaboratable):
48 # generic (
49 # -- Line size in bytes
50 # LINE_SIZE : positive := 64;
51 # -- Number of lines in a set
52 # NUM_LINES : positive := 32;
53 # -- Number of ways
54 # NUM_WAYS : positive := 4;
55 # -- L1 DTLB entries per set
56 # TLB_SET_SIZE : positive := 64;
57 # -- L1 DTLB number of sets
58 # TLB_NUM_WAYS : positive := 2;
59 # -- L1 DTLB log_2(page_size)
60 # TLB_LG_PGSZ : positive := 12;
61 # -- Non-zero to enable log data collection
62 # LOG_LENGTH : natural := 0
63 # );
64 def __init__(self):
65 # Line size in bytes
66 self.LINE_SIZE = 64
67 # Number of lines in a set
68 self.NUM_LINES = 32
69 # Number of ways
70 self.NUM_WAYS = 4
71 # L1 DTLB entries per set
72 self.TLB_SET_SIZE = 64
73 # L1 DTLB number of sets
74 self.TLB_NUM_WAYS = 2
75 # L1 DTLB log_2(page_size)
76 self.TLB_LG_PGSZ = 12
77 # Non-zero to enable log data collection
78 self.LOG_LENGTH = 0
79 # port (
80 # clk : in std_ulogic;
81 # rst : in std_ulogic;
82 #
83 # d_in : in Loadstore1ToDcacheType;
84 # d_out : out DcacheToLoadstore1Type;
85 #
86 # m_in : in MmuToDcacheType;
87 # m_out : out DcacheToMmuType;
88 #
89 # stall_out : out std_ulogic;
90 #
91 # wishbone_out : out wishbone_master_out;
92 # wishbone_in : in wishbone_slave_out;
93 #
94 # log_out : out std_ulogic_vector(19 downto 0)
95 # );
96 self.d_in = LoadStore1ToDcacheType()
97 self.d_out = DcacheToLoadStore1Type()
98
99 self.m_in = MmuToDcacheType()
100 self.m_out = DcacheToMmuType()
101
102 self.stall_out = Signal()
103
104 self.wb_out = WBMasterOut()
105 self.wb_in = WBSlaveOut()
106
107 self.log_out = Signal(20)
108 # end entity dcache;
109
110 # architecture rtl of dcache is
111 def elaborate(self, platform):
112 LINE_SIZE = self.LINE_SIZE
113 NUM_LINES = self.NUM_LINES
114 NUM_WAYS = self.NUM_WAYS
115 TLB_SET_SIZE = self.TLB_SET_SIZE
116 TLB_NUM_WAYS = self.TLB_NUM_WAYS
117 TLB_LG_PGSZ = self.TLB_LG_PGSZ
118 LOG_LENGTH = self.LOG_LENGTH
119
120 # -- BRAM organisation: We never access more than
121 # -- wishbone_data_bits at a time so to save
122 # -- resources we make the array only that wide, and
123 # -- use consecutive indices for to make a cache "line"
124 # --
125 # -- ROW_SIZE is the width in bytes of the BRAM
126 # -- (based on WB, so 64-bits)
127 # constant ROW_SIZE : natural := wishbone_data_bits / 8;
128 # BRAM organisation: We never access more than
129 # -- wishbone_data_bits at a time so to save
130 # -- resources we make the array only that wide, and
131 # -- use consecutive indices for to make a cache "line"
132 # --
133 # -- ROW_SIZE is the width in bytes of the BRAM
134 # -- (based on WB, so 64-bits)
135 ROW_SIZE = WB_DATA_BITS / 8;
136
137 # -- ROW_PER_LINE is the number of row (wishbone
138 # -- transactions) in a line
139 # constant ROW_PER_LINE : natural := LINE_SIZE / ROW_SIZE;
140 # -- BRAM_ROWS is the number of rows in BRAM needed
141 # -- to represent the full dcache
142 # constant BRAM_ROWS : natural := NUM_LINES * ROW_PER_LINE;
143 # ROW_PER_LINE is the number of row (wishbone
144 # transactions) in a line
145 ROW_PER_LINE = LINE_SIZE / ROW_SIZE
146 # BRAM_ROWS is the number of rows in BRAM needed
147 # to represent the full dcache
148 BRAM_ROWS = NUM_LINES * ROW_PER_LINE
149
150 # -- Bit fields counts in the address
151 #
152 # -- REAL_ADDR_BITS is the number of real address
153 # -- bits that we store
154 # constant REAL_ADDR_BITS : positive := 56;
155 # -- ROW_BITS is the number of bits to select a row
156 # constant ROW_BITS : natural := log2(BRAM_ROWS);
157 # -- ROW_LINEBITS is the number of bits to select
158 # -- a row within a line
159 # constant ROW_LINEBITS : natural := log2(ROW_PER_LINE);
160 # -- LINE_OFF_BITS is the number of bits for
161 # -- the offset in a cache line
162 # constant LINE_OFF_BITS : natural := log2(LINE_SIZE);
163 # -- ROW_OFF_BITS is the number of bits for
164 # -- the offset in a row
165 # constant ROW_OFF_BITS : natural := log2(ROW_SIZE);
166 # -- INDEX_BITS is the number if bits to
167 # -- select a cache line
168 # constant INDEX_BITS : natural := log2(NUM_LINES);
169 # -- SET_SIZE_BITS is the log base 2 of the set size
170 # constant SET_SIZE_BITS : natural := LINE_OFF_BITS
171 # + INDEX_BITS;
172 # -- TAG_BITS is the number of bits of
173 # -- the tag part of the address
174 # constant TAG_BITS : natural := REAL_ADDR_BITS - SET_SIZE_BITS;
175 # -- TAG_WIDTH is the width in bits of each way of the tag RAM
176 # constant TAG_WIDTH : natural := TAG_BITS + 7
177 # - ((TAG_BITS + 7) mod 8);
178 # -- WAY_BITS is the number of bits to select a way
179 # constant WAY_BITS : natural := log2(NUM_WAYS);
180 # Bit fields counts in the address
181
182 # REAL_ADDR_BITS is the number of real address
183 # bits that we store
184 REAL_ADDR_BITS = 56
185 # ROW_BITS is the number of bits to select a row
186 ROW_BITS = log2_int(BRAM_ROWS)
187 # ROW_LINE_BITS is the number of bits to select
188 # a row within a line
189 ROW_LINE_BITS = log2_int(ROW_PER_LINE)
190 # LINE_OFF_BITS is the number of bits for
191 # the offset in a cache line
192 LINE_OFF_BITS = log2_int(LINE_SIZE)
193 # ROW_OFF_BITS is the number of bits for
194 # the offset in a row
195 ROW_OFF_BITS = log2_int(ROW_SIZE)
196 # INDEX_BITS is the number if bits to
197 # select a cache line
198 INDEX_BITS = log2_int(NUM_LINES)
199 # SET_SIZE_BITS is the log base 2 of the set size
200 SET_SIZE_BITS = LINE_OFF_BITS + INDEX_BITS
201 # TAG_BITS is the number of bits of
202 # the tag part of the address
203 TAG_BITS = REAL_ADDR_BITS - SET_SIZE_BITS
204 # TAG_WIDTH is the width in bits of each way of the tag RAM
205 TAG_WIDTH = TAG_BITS + 7 - ((TAG_BITS + 7) % 8)
206 # WAY_BITS is the number of bits to select a way
207 WAY_BITS = log2_int(NUM_WAYS)
208
209 # -- Example of layout for 32 lines of 64 bytes:
210 # --
211 # -- .. tag |index| line |
212 # -- .. | row | |
213 # -- .. | |---| | ROW_LINEBITS (3)
214 # -- .. | |--- - --| LINE_OFF_BITS (6)
215 # -- .. | |- --| ROW_OFF_BITS (3)
216 # -- .. |----- ---| | ROW_BITS (8)
217 # -- .. |-----| | INDEX_BITS (5)
218 # -- .. --------| | TAG_BITS (45)
219 # Example of layout for 32 lines of 64 bytes:
220 #
221 # .. tag |index| line |
222 # .. | row | |
223 # .. | |---| | ROW_LINE_BITS (3)
224 # .. | |--- - --| LINE_OFF_BITS (6)
225 # .. | |- --| ROW_OFF_BITS (3)
226 # .. |----- ---| | ROW_BITS (8)
227 # .. |-----| | INDEX_BITS (5)
228 # .. --------| | TAG_BITS (45)
229
230
231 # subtype row_t is integer range 0 to BRAM_ROWS-1;
232 # subtype index_t is integer range 0 to NUM_LINES-1;
233 # subtype way_t is integer range 0 to NUM_WAYS-1;
234 # subtype row_in_line_t is unsigned(ROW_LINE_BITS-1 downto 0);
235 ROW = BRAM_ROWS
236 INDEX = NUM_LINES
237 WAY = NUM_WAYS
238 ROW_IN_LINE = ROW_LINE_BITS
239
240 # -- The cache data BRAM organized as described above for each way
241 # subtype cache_row_t is
242 # std_ulogic_vector(wishbone_data_bits-1 downto 0);
243 # The cache data BRAM organized as described above for each way
244 CACHE_ROW = WB_DATA_BITS
245
246 # -- The cache tags LUTRAM has a row per set.
247 # -- Vivado is a pain and will not handle a
248 # -- clean (commented) definition of the cache
249 # -- tags as a 3d memory. For now, work around
250 # -- it by putting all the tags
251 # subtype cache_tag_t is std_logic_vector(TAG_BITS-1 downto 0);
252 # The cache tags LUTRAM has a row per set.
253 # Vivado is a pain and will not handle a
254 # clean (commented) definition of the cache
255 # tags as a 3d memory. For now, work around
256 # it by putting all the tags
257 CACHE_TAG = TAG_BITS
258
259 # -- type cache_tags_set_t is array(way_t) of cache_tag_t;
260 # -- type cache_tags_array_t is array(index_t) of cache_tags_set_t;
261 # constant TAG_RAM_WIDTH : natural := TAG_WIDTH * NUM_WAYS;
262 # subtype cache_tags_set_t is
263 # std_logic_vector(TAG_RAM_WIDTH-1 downto 0);
264 # type cache_tags_array_t is array(index_t) of cache_tags_set_t;
265 # type cache_tags_set_t is array(way_t) of cache_tag_t;
266 # type cache_tags_array_t is array(index_t) of cache_tags_set_t;
267 TAG_RAM_WIDTH = TAG_WIDTH * NUM_WAYS
268
269 CACHE_TAG_SET = TAG_RAM_WIDTH
270
271 def CacheTagArray():
272 return Array(CacheTagSet() for x in range(INDEX))
273
274 # -- The cache valid bits
275 # subtype cache_way_valids_t is
276 # std_ulogic_vector(NUM_WAYS-1 downto 0);
277 # type cache_valids_t is array(index_t) of cache_way_valids_t;
278 # type row_per_line_valid_t is
279 # array(0 to ROW_PER_LINE - 1) of std_ulogic;
280 # The cache valid bits
281 CACHE_WAY_VALID_BITS = NUM_WAYS
282
283 def CacheValidBitsArray():
284 return Array(CacheWayValidBits() for x in range(INDEX))
285
286 def RowPerLineValidArray():
287 return Array(Signal() for x in range(ROW_PER_LINE))
288
289 # -- Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
290 # signal cache_tags : cache_tags_array_t;
291 # signal cache_tag_set : cache_tags_set_t;
292 # signal cache_valids : cache_valids_t;
293 #
294 # attribute ram_style : string;
295 # attribute ram_style of cache_tags : signal is "distributed";
296 # Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
297 cache_tags = CacheTagArray()
298 cache_tag_set = Signal(CACHE_TAG_SET)
299 cache_valid_bits = CacheValidBitsArray()
300
301 # TODO attribute ram_style : string;
302 # TODO attribute ram_style of cache_tags : signal is "distributed";
303
304 # -- L1 TLB.
305 # constant TLB_SET_BITS : natural := log2(TLB_SET_SIZE);
306 # constant TLB_WAY_BITS : natural := log2(TLB_NUM_WAYS);
307 # constant TLB_EA_TAG_BITS : natural :=
308 # 64 - (TLB_LG_PGSZ + TLB_SET_BITS);
309 # constant TLB_TAG_WAY_BITS : natural :=
310 # TLB_NUM_WAYS * TLB_EA_TAG_BITS;
311 # constant TLB_PTE_BITS : natural := 64;
312 # constant TLB_PTE_WAY_BITS : natural :=
313 # TLB_NUM_WAYS * TLB_PTE_BITS;
314 # L1 TLB
315 TLB_SET_BITS = log2_int(TLB_SET_SIZE)
316 TLB_WAY_BITS = log2_int(TLB_NUM_WAYS)
317 TLB_EA_TAG_BITS = 64 - (TLB_LG_PGSZ + TLB_SET_BITS)
318 TLB_TAG_WAY_BITS = TLB_NUM_WAYS * TLB_EA_TAG_BITS
319 TLB_PTE_BITS = 64
320 TLB_PTE_WAY_BITS = TLB_NUM_WAYS * TLB_PTE_BITS;
321
322 # subtype tlb_way_t is integer range 0 to TLB_NUM_WAYS - 1;
323 # subtype tlb_index_t is integer range 0 to TLB_SET_SIZE - 1;
324 # subtype tlb_way_valids_t is
325 # std_ulogic_vector(TLB_NUM_WAYS-1 downto 0);
326 # type tlb_valids_t is
327 # array(tlb_index_t) of tlb_way_valids_t;
328 # subtype tlb_tag_t is
329 # std_ulogic_vector(TLB_EA_TAG_BITS - 1 downto 0);
330 # subtype tlb_way_tags_t is
331 # std_ulogic_vector(TLB_TAG_WAY_BITS-1 downto 0);
332 # type tlb_tags_t is
333 # array(tlb_index_t) of tlb_way_tags_t;
334 # subtype tlb_pte_t is
335 # std_ulogic_vector(TLB_PTE_BITS - 1 downto 0);
336 # subtype tlb_way_ptes_t is
337 # std_ulogic_vector(TLB_PTE_WAY_BITS-1 downto 0);
338 # type tlb_ptes_t is array(tlb_index_t) of tlb_way_ptes_t;
339 # type hit_way_set_t is array(tlb_way_t) of way_t;
340 TLB_WAY = TLB_NUM_WAYS
341
342 TLB_INDEX = TLB_SET_SIZE
343
344 TLB_WAY_VALID_BITS = TLB_NUM_WAYS
345
346 def TLBValidBitsArray():
347 return Array(
348 Signal(TLB_WAY_VALID_BITS) for x in range(TLB_SET_SIZE)
349 )
350
351 TLB_TAG = TLB_EA_TAG_BITS
352
353 TLB_WAY_TAGS = TLB_TAG_WAY_BITS
354
355 def TLBTagsArray():
356 return Array(
357 Signal(TLB_WAY_TAGS) for x in range (TLB_SET_SIZE)
358 )
359
360 TLB_PTE = TLB_PTE_BITS
361
362 TLB_WAY_PTES = TLB_PTE_WAY_BITS
363
364 def TLBPtesArray():
365 return Array(
366 Signal(TLB_WAY_PTES) for x in range(TLB_SET_SIZE)
367 )
368
369 def HitWaySet():
370 return Array(Signal(WAY) for x in range(TLB_NUM_WAYS))
371
372 # signal dtlb_valids : tlb_valids_t;
373 # signal dtlb_tags : tlb_tags_t;
374 # signal dtlb_ptes : tlb_ptes_t;
375
376 """note: these are passed to nmigen.hdl.Memory as "attributes". don't
377 know how, just that they are.
378 """
379 # attribute ram_style of dtlb_tags : signal is "distributed";
380 # attribute ram_style of dtlb_ptes : signal is "distributed";
381 dtlb_valids = TLBValidBitsArray()
382 dtlb_tags = TLBTagsArray()
383 dtlb_ptes = TLBPtesArray()
384 # TODO attribute ram_style of dtlb_tags : signal is "distributed";
385 # TODO attribute ram_style of dtlb_ptes : signal is "distributed";
386
387
388 # -- Record for storing permission, attribute, etc. bits from a PTE
389 # type perm_attr_t is record
390 # reference : std_ulogic;
391 # changed : std_ulogic;
392 # nocache : std_ulogic;
393 # priv : std_ulogic;
394 # rd_perm : std_ulogic;
395 # wr_perm : std_ulogic;
396 # end record;
397 # Record for storing permission, attribute, etc. bits from a PTE
398 class PermAttr(RecordObject):
399 def __init__(self):
400 super().__init__()
401 self.reference = Signal()
402 self.changed = Signal()
403 self.nocache = Signal()
404 self.priv = Signal()
405 self.rd_perm = Signal()
406 self.wr_perm = Signal()
407
408 # function extract_perm_attr(
409 # pte : std_ulogic_vector(TLB_PTE_BITS - 1 downto 0))
410 # return perm_attr_t is
411 # variable pa : perm_attr_t;
412 # begin
413 # pa.reference := pte(8);
414 # pa.changed := pte(7);
415 # pa.nocache := pte(5);
416 # pa.priv := pte(3);
417 # pa.rd_perm := pte(2);
418 # pa.wr_perm := pte(1);
419 # return pa;
420 # end;
421 def extract_perm_attr(pte=Signal(TLB_PTE_BITS)):
422 pa = PermAttr()
423 pa.reference = pte[8]
424 pa.changed = pte[7]
425 pa.nocache = pte[5]
426 pa.priv = pte[3]
427 pa.rd_perm = pte[2]
428 pa.wr_perm = pte[1]
429 return pa;
430
431 # constant real_mode_perm_attr : perm_attr_t :=
432 # (nocache => '0', others => '1');
433 REAL_MODE_PERM_ATTR = PermAttr()
434 REAL_MODE_PERM_ATTR.reference = 1
435 REAL_MODE_PERM_ATTR.changed = 1
436 REAL_MODE_PERM_ATTR.priv = 1
437 REAL_MODE_PERM_ATTR.rd_perm = 1
438 REAL_MODE_PERM_ATTR.wr_perm = 1
439
440 # -- Type of operation on a "valid" input
441 # type op_t is
442 # (
443 # OP_NONE,
444 # OP_BAD, -- NC cache hit, TLB miss, prot/RC failure
445 # OP_STCX_FAIL, -- conditional store w/o reservation
446 # OP_LOAD_HIT, -- Cache hit on load
447 # OP_LOAD_MISS, -- Load missing cache
448 # OP_LOAD_NC, -- Non-cachable load
449 # OP_STORE_HIT, -- Store hitting cache
450 # OP_STORE_MISS -- Store missing cache
451 # );
452 # Type of operation on a "valid" input
453 @unique
454 class OP(Enum):
455 OP_NONE = 0
456 OP_BAD = 1 # NC cache hit, TLB miss, prot/RC failure
457 OP_STCX_FAIL = 2 # conditional store w/o reservation
458 OP_LOAD_HIT = 3 # Cache hit on load
459 OP_LOAD_MISS = 4 # Load missing cache
460 OP_LOAD_NC = 5 # Non-cachable load
461 OP_STORE_HIT = 6 # Store hitting cache
462 OP_STORE_MISS = 7 # Store missing cache
463
464 # -- Cache state machine
465 # type state_t is
466 # (
467 # IDLE, -- Normal load hit processing
468 # RELOAD_WAIT_ACK, -- Cache reload wait ack
469 # STORE_WAIT_ACK, -- Store wait ack
470 # NC_LOAD_WAIT_ACK -- Non-cachable load wait ack
471 # );
472 # Cache state machine
473 @unique
474 class State(Enum):
475 IDLE = 0 # Normal load hit processing
476 RELOAD_WAIT_ACK = 1 # Cache reload wait ack
477 STORE_WAIT_ACK = 2 # Store wait ack
478 NC_LOAD_WAIT_ACK = 3 # Non-cachable load wait ack
479
480 # -- Dcache operations:
481 # --
482 # -- In order to make timing, we use the BRAMs with
483 # -- an output buffer, which means that the BRAM
484 # -- output is delayed by an extra cycle.
485 # --
486 # -- Thus, the dcache has a 2-stage internal pipeline
487 # -- for cache hits with no stalls.
488 # --
489 # -- All other operations are handled via stalling
490 # -- in the first stage.
491 # --
492 # -- The second stage can thus complete a hit at the same
493 # -- time as the first stage emits a stall for a complex op.
494 #
495 # -- Stage 0 register, basically contains just the latched request
496 # type reg_stage_0_t is record
497 # req : Loadstore1ToDcacheType;
498 # tlbie : std_ulogic;
499 # doall : std_ulogic;
500 # tlbld : std_ulogic;
501 # mmu_req : std_ulogic; -- indicates source of request
502 # end record;
503 # Dcache operations:
504 #
505 # In order to make timing, we use the BRAMs with
506 # an output buffer, which means that the BRAM
507 # output is delayed by an extra cycle.
508 #
509 # Thus, the dcache has a 2-stage internal pipeline
510 # for cache hits with no stalls.
511 #
512 # All other operations are handled via stalling
513 # in the first stage.
514 #
515 # The second stage can thus complete a hit at the same
516 # time as the first stage emits a stall for a complex op.
517 #
518 # Stage 0 register, basically contains just the latched request
519 class RegStage0(RecordObject):
520 def __init__(self):
521 super().__init__()
522 self.req = LoadStore1ToDcacheType()
523 self.tlbie = Signal()
524 self.doall = Signal()
525 self.tlbld = Signal()
526 self.mmu_req = Signal() # indicates source of request
527
528 # signal r0 : reg_stage_0_t;
529 # signal r0_full : std_ulogic;
530 r0 = RegStage0()
531 r0_full = Signal()
532
533 # type mem_access_request_t is record
534 # op : op_t;
535 # valid : std_ulogic;
536 # dcbz : std_ulogic;
537 # real_addr : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
538 # data : std_ulogic_vector(63 downto 0);
539 # byte_sel : std_ulogic_vector(7 downto 0);
540 # hit_way : way_t;
541 # same_tag : std_ulogic;
542 # mmu_req : std_ulogic;
543 # end record;
544 class MemAccessRequest(RecordObject):
545 def __init__(self):
546 super().__init__()
547 self.op = Op()
548 self.valid = Signal()
549 self.dcbz = Signal()
550 self.real_addr = Signal(REAL_ADDR_BITS)
551 self.data = Signal(64)
552 self.byte_sel = Signal(8)
553 self.hit_way = Signal(WAY)
554 self.same_tag = Signal()
555 self.mmu_req = Signal()
556
557 # -- First stage register, contains state for stage 1 of load hits
558 # -- and for the state machine used by all other operations
559 # type reg_stage_1_t is record
560 # -- Info about the request
561 # full : std_ulogic; -- have uncompleted request
562 # mmu_req : std_ulogic; -- request is from MMU
563 # req : mem_access_request_t;
564 #
565 # -- Cache hit state
566 # hit_way : way_t;
567 # hit_load_valid : std_ulogic;
568 # hit_index : index_t;
569 # cache_hit : std_ulogic;
570 #
571 # -- TLB hit state
572 # tlb_hit : std_ulogic;
573 # tlb_hit_way : tlb_way_t;
574 # tlb_hit_index : tlb_index_t;
575 #
576 # -- 2-stage data buffer for data forwarded from writes to reads
577 # forward_data1 : std_ulogic_vector(63 downto 0);
578 # forward_data2 : std_ulogic_vector(63 downto 0);
579 # forward_sel1 : std_ulogic_vector(7 downto 0);
580 # forward_valid1 : std_ulogic;
581 # forward_way1 : way_t;
582 # forward_row1 : row_t;
583 # use_forward1 : std_ulogic;
584 # forward_sel : std_ulogic_vector(7 downto 0);
585 #
586 # -- Cache miss state (reload state machine)
587 # state : state_t;
588 # dcbz : std_ulogic;
589 # write_bram : std_ulogic;
590 # write_tag : std_ulogic;
591 # slow_valid : std_ulogic;
592 # wb : wishbone_master_out;
593 # reload_tag : cache_tag_t;
594 # store_way : way_t;
595 # store_row : row_t;
596 # store_index : index_t;
597 # end_row_ix : row_in_line_t;
598 # rows_valid : row_per_line_valid_t;
599 # acks_pending : unsigned(2 downto 0);
600 # inc_acks : std_ulogic;
601 # dec_acks : std_ulogic;
602 #
603 # -- Signals to complete (possibly with error)
604 # ls_valid : std_ulogic;
605 # ls_error : std_ulogic;
606 # mmu_done : std_ulogic;
607 # mmu_error : std_ulogic;
608 # cache_paradox : std_ulogic;
609 #
610 # -- Signal to complete a failed stcx.
611 # stcx_fail : std_ulogic;
612 # end record;
613 # First stage register, contains state for stage 1 of load hits
614 # and for the state machine used by all other operations
615 class RegStage1(RecordObject):
616 def __init__(self):
617 super().__init__()
618 # Info about the request
619 self.full = Signal() # have uncompleted request
620 self.mmu_req = Signal() # request is from MMU
621 self.req = MemAccessRequest()
622
623 # Cache hit state
624 self.hit_way = Signal(WAY)
625 self.hit_load_valid = Signal()
626 self.hit_index = Signal(INDEX)
627 self.cache_hit = Signal()
628
629 # TLB hit state
630 self.tlb_hit = Signal()
631 self.tlb_hit_way = Signal(TLB_WAY)
632 self.tlb_hit_index = Signal(TLB_SET_SIZE)
633 self.
634 # 2-stage data buffer for data forwarded from writes to reads
635 self.forward_data1 = Signal(64)
636 self.forward_data2 = Signal(64)
637 self.forward_sel1 = Signal(8)
638 self.forward_valid1 = Signal()
639 self.forward_way1 = Signal(WAY)
640 self.forward_row1 = Signal(ROW)
641 self.use_forward1 = Signal()
642 self.forward_sel = Signal(8)
643
644 # Cache miss state (reload state machine)
645 self.state = State()
646 self.dcbz = Signal()
647 self.write_bram = Signal()
648 self.write_tag = Signal()
649 self.slow_valid = Signal()
650 self.wb = WishboneMasterOut()
651 self.reload_tag = Signal(CACHE_TAG)
652 self.store_way = Signal(WAY)
653 self.store_row = Signal(ROW)
654 self.store_index = Signal(INDEX)
655 self.end_row_ix = Signal(ROW_IN_LINE)
656 self.rows_valid = RowPerLineValidArray()
657 self.acks_pending = Signal(3)
658 self.inc_acks = Signal()
659 self.dec_acks = Signal()
660
661 # Signals to complete (possibly with error)
662 self.ls_valid = Signal()
663 self.ls_error = Signal()
664 self.mmu_done = Signal()
665 self.mmu_error = Signal()
666 self.cache_paradox = Signal()
667
668 # Signal to complete a failed stcx.
669 self.stcx_fail = Signal()
670
671 # signal r1 : reg_stage_1_t;
672 r1 = RegStage1()
673
674 # -- Reservation information
675 # --
676 # type reservation_t is record
677 # valid : std_ulogic;
678 # addr : std_ulogic_vector(63 downto LINE_OFF_BITS);
679 # end record;
680 # Reservation information
681
682 class Reservation(RecordObject):
683 def __init__(self):
684 super().__init__()
685 valid = Signal()
686 # TODO LINE_OFF_BITS is 6
687 addr = Signal(63 downto LINE_OFF_BITS)
688
689 # signal reservation : reservation_t;
690 reservation = Reservation()
691
692 # -- Async signals on incoming request
693 # signal req_index : index_t;
694 # signal req_row : row_t;
695 # signal req_hit_way : way_t;
696 # signal req_tag : cache_tag_t;
697 # signal req_op : op_t;
698 # signal req_data : std_ulogic_vector(63 downto 0);
699 # signal req_same_tag : std_ulogic;
700 # signal req_go : std_ulogic;
701 # Async signals on incoming request
702 req_index = Signal(INDEX)
703 req_row = Signal(ROW)
704 req_hit_way = Signal(WAY)
705 req_tag = Signal(CACHE_TAG)
706 req_op = Op()
707 req_data = Signal(64)
708 req_same_tag = Signal()
709 req_go = Signal()
710
711 # signal early_req_row : row_t;
712 #
713 # signal cancel_store : std_ulogic;
714 # signal set_rsrv : std_ulogic;
715 # signal clear_rsrv : std_ulogic;
716 #
717 # signal r0_valid : std_ulogic;
718 # signal r0_stall : std_ulogic;
719 #
720 # signal use_forward1_next : std_ulogic;
721 # signal use_forward2_next : std_ulogic;
722 early_req_row = Signal(ROW)
723
724 cancel_store = Signal()
725 set_rsrv = Signal()
726 clear_rsrv = Signal()
727
728 r0_valid = Signal()
729 r0_stall = Signal()
730
731 use_forward1_next = Signal()
732 use_forward2_next = Signal()
733
734 # -- Cache RAM interface
735 # type cache_ram_out_t is array(way_t) of cache_row_t;
736 # signal cache_out : cache_ram_out_t;
737 # Cache RAM interface
738 def CacheRamOut():
739 return Array(Signal(CACHE_ROW) for x in range(NUM_WAYS))
740
741 cache_out = CacheRamOut()
742
743 # -- PLRU output interface
744 # type plru_out_t is array(index_t) of
745 # std_ulogic_vector(WAY_BITS-1 downto 0);
746 # signal plru_victim : plru_out_t;
747 # signal replace_way : way_t;
748 # PLRU output interface
749 def PLRUOut():
750 return Array(Signal(WAY_BITS) for x in range(Index()))
751
752 plru_victim = PLRUOut()
753 replace_way = Signal(WAY)
754
755 # -- Wishbone read/write/cache write formatting signals
756 # signal bus_sel : std_ulogic_vector(7 downto 0);
757 # Wishbone read/write/cache write formatting signals
758 bus_sel = Signal(8)
759
760 # -- TLB signals
761 # signal tlb_tag_way : tlb_way_tags_t;
762 # signal tlb_pte_way : tlb_way_ptes_t;
763 # signal tlb_valid_way : tlb_way_valids_t;
764 # signal tlb_req_index : tlb_index_t;
765 # signal tlb_hit : std_ulogic;
766 # signal tlb_hit_way : tlb_way_t;
767 # signal pte : tlb_pte_t;
768 # signal ra : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
769 # signal valid_ra : std_ulogic;
770 # signal perm_attr : perm_attr_t;
771 # signal rc_ok : std_ulogic;
772 # signal perm_ok : std_ulogic;
773 # signal access_ok : std_ulogic;
774 # TLB signals
775 tlb_tag_way = Signal(TLB_WAY_TAGS)
776 tlb_pte_way = Signal(TLB_WAY_PTES)
777 tlb_valid_way = Signal(TLB_WAY_VALID_BITS)
778 tlb_req_index = Signal(TLB_SET_SIZE)
779 tlb_hit = Signal()
780 tlb_hit_way = Signal(TLB_WAY)
781 pte = Signal(TLB_PTE)
782 ra = Signal(REAL_ADDR_BITS)
783 valid_ra = Signal()
784 perm_attr = PermAttr()
785 rc_ok = Signal()
786 perm_ok = Signal()
787 access_ok = Signal()
788
789 # -- TLB PLRU output interface
790 # type tlb_plru_out_t is array(tlb_index_t) of
791 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
792 # signal tlb_plru_victim : tlb_plru_out_t;
793 # TLB PLRU output interface
794 DEF TLBPLRUOut():
795 return Array(Signal(TLB_WAY_BITS) for x in range(TLB_SET_SIZE))
796
797 tlb_plru_victim = TLBPLRUOut()
798
799 # -- Helper functions to decode incoming requests
800 #
801 # -- Return the cache line index (tag index) for an address
802 # function get_index(addr: std_ulogic_vector) return index_t is
803 # begin
804 # return to_integer(
805 # unsigned(addr(SET_SIZE_BITS - 1 downto LINE_OFF_BITS))
806 # );
807 # end;
808 # Helper functions to decode incoming requests
809 #
810 # Return the cache line index (tag index) for an address
811 def get_index(addr):
812 return addr[LINE_OFF_BITS:SET_SIZE_BITS]
813
814 # -- Return the cache row index (data memory) for an address
815 # function get_row(addr: std_ulogic_vector) return row_t is
816 # begin
817 # return to_integer(
818 # unsigned(addr(SET_SIZE_BITS - 1 downto ROW_OFF_BITS))
819 # );
820 # end;
821 # Return the cache row index (data memory) for an address
822 def get_row(addr):
823 return addr[ROW_OFF_BITS:SET_SIZE_BITS]
824
825 # -- Return the index of a row within a line
826 # function get_row_of_line(row: row_t) return row_in_line_t is
827 # variable row_v : unsigned(ROW_BITS-1 downto 0);
828 # begin
829 # row_v := to_unsigned(row, ROW_BITS);
830 # return row_v(ROW_LINEBITS-1 downto 0);
831 # end;
832 # Return the index of a row within a line
833 def get_row_of_line(row):
834 row_v = Signal(ROW_BITS)
835 row_v = Signal(row)
836 return row_v[0:ROW_LINE_BITS]
837
838 # -- Returns whether this is the last row of a line
839 # function is_last_row_addr(addr: wishbone_addr_type;
840 # last: row_in_line_t) return boolean is
841 # begin
842 # return
843 # unsigned(addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS)) = last;
844 # end;
845 # Returns whether this is the last row of a line
846 def is_last_row_addr(addr, last):
847 return addr[ROW_OFF_BITS:LINE_OFF_BITS] == last
848
849 # -- Returns whether this is the last row of a line
850 # function is_last_row(row: row_t; last: row_in_line_t)
851 # return boolean is
852 # begin
853 # return get_row_of_line(row) = last;
854 # end;
855 # Returns whether this is the last row of a line
856 def is_last_row(row, last):
857 return get_row_of_line(row) == last
858
859 # -- Return the address of the next row in the current cache line
860 # function next_row_addr(addr: wishbone_addr_type)
861 # return std_ulogic_vector is
862 # variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
863 # variable result : wishbone_addr_type;
864 # begin
865 # -- Is there no simpler way in VHDL to
866 # -- generate that 3 bits adder ?
867 # row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
868 # row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
869 # result := addr;
870 # result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
871 # return result;
872 # end;
873 # Return the address of the next row in the current cache line
874 def next_row_addr(addr):
875 row_idx = Signal(ROW_LINE_BITS)
876 result = WBAddrType()
877 # Is there no simpler way in VHDL to
878 # generate that 3 bits adder ?
879 row_idx = addr[ROW_OFF_BITS:LINE_OFF_BITS]
880 row_idx = Signal(row_idx + 1)
881 result = addr
882 result[ROW_OFF_BITS:LINE_OFF_BITS] = row_idx
883 return result
884
885 # -- Return the next row in the current cache line. We use a
886 # -- dedicated function in order to limit the size of the
887 # -- generated adder to be only the bits within a cache line
888 # -- (3 bits with default settings)
889 # function next_row(row: row_t) return row_t is
890 # variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
891 # variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
892 # variable result : std_ulogic_vector(ROW_BITS-1 downto 0);
893 # begin
894 # row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
895 # row_idx := row_v(ROW_LINEBITS-1 downto 0);
896 # row_v(ROW_LINEBITS-1 downto 0) :=
897 # std_ulogic_vector(unsigned(row_idx) + 1);
898 # return to_integer(unsigned(row_v));
899 # end;
900 # Return the next row in the current cache line. We use a
901 # dedicated function in order to limit the size of the
902 # generated adder to be only the bits within a cache line
903 # (3 bits with default settings)
904 def next_row(row)
905 row_v = Signal(ROW_BITS)
906 row_idx = Signal(ROW_LINE_BITS)
907 result = Signal(ROW_BITS)
908
909 row_v = Signal(row)
910 row_idx = row_v[ROW_LINE_BITS]
911 row_v[0:ROW_LINE_BITS] = Signal(row_idx + 1)
912 return row_v
913
914 # -- Get the tag value from the address
915 # function get_tag(addr: std_ulogic_vector) return cache_tag_t is
916 # begin
917 # return addr(REAL_ADDR_BITS - 1 downto SET_SIZE_BITS);
918 # end;
919 # Get the tag value from the address
920 def get_tag(addr):
921 return addr[SET_SIZE_BITS:REAL_ADDR_BITS]
922
923 # -- Read a tag from a tag memory row
924 # function read_tag(way: way_t; tagset: cache_tags_set_t)
925 # return cache_tag_t is
926 # begin
927 # return tagset(way * TAG_WIDTH + TAG_BITS
928 # - 1 downto way * TAG_WIDTH);
929 # end;
930 # Read a tag from a tag memory row
931 def read_tag(way, tagset):
932 return tagset[way *TAG_WIDTH:way * TAG_WIDTH + TAG_BITS]
933
934 # -- Read a TLB tag from a TLB tag memory row
935 # function read_tlb_tag(way: tlb_way_t; tags: tlb_way_tags_t)
936 # return tlb_tag_t is
937 # variable j : integer;
938 # begin
939 # j := way * TLB_EA_TAG_BITS;
940 # return tags(j + TLB_EA_TAG_BITS - 1 downto j);
941 # end;
942 # Read a TLB tag from a TLB tag memory row
943 def read_tlb_tag(way, tags):
944 j = Signal()
945
946 j = way * TLB_EA_TAG_BITS
947 return tags[j:j + TLB_EA_TAG_BITS]
948
949 # -- Write a TLB tag to a TLB tag memory row
950 # procedure write_tlb_tag(way: tlb_way_t; tags: inout tlb_way_tags_t;
951 # tag: tlb_tag_t) is
952 # variable j : integer;
953 # begin
954 # j := way * TLB_EA_TAG_BITS;
955 # tags(j + TLB_EA_TAG_BITS - 1 downto j) := tag;
956 # end;
957 # Write a TLB tag to a TLB tag memory row
958 def write_tlb_tag(way, tags), tag):
959 j = Signal()
960
961 j = way * TLB_EA_TAG_BITS
962 tags[j:j + TLB_EA_TAG_BITS] = tag
963
964 # -- Read a PTE from a TLB PTE memory row
965 # function read_tlb_pte(way: tlb_way_t; ptes: tlb_way_ptes_t)
966 # return tlb_pte_t is
967 # variable j : integer;
968 # begin
969 # j := way * TLB_PTE_BITS;
970 # return ptes(j + TLB_PTE_BITS - 1 downto j);
971 # end;
972 # Read a PTE from a TLB PTE memory row
973 def read_tlb_pte(way, ptes):
974 j = Signal()
975
976 j = way * TLB_PTE_BITS
977 return ptes[j:j + TLB_PTE_BITS]
978
979 # procedure write_tlb_pte(way: tlb_way_t;
980 # ptes: inout tlb_way_ptes_t; newpte: tlb_pte_t) is
981 # variable j : integer;
982 # begin
983 # j := way * TLB_PTE_BITS;
984 # ptes(j + TLB_PTE_BITS - 1 downto j) := newpte;
985 # end;
986 def write_tlb_pte(way, ptes,newpte):
987 j = Signal()
988
989 j = way * TLB_PTE_BITS
990 return ptes[j:j + TLB_PTE_BITS] = newpte
991
992 # begin
993 #
994 """these, because they are constants, can actually be done *as*
995 python asserts:
996 assert LINE_SIZE % ROWSIZE == 0, "line size not ...."
997 """
998 # assert LINE_SIZE mod ROW_SIZE = 0
999 # report "LINE_SIZE not multiple of ROW_SIZE" severity FAILURE;
1000 # assert ispow2(LINE_SIZE)
1001 # report "LINE_SIZE not power of 2" severity FAILURE;
1002 # assert ispow2(NUM_LINES)
1003 # report "NUM_LINES not power of 2" severity FAILURE;
1004 # assert ispow2(ROW_PER_LINE)
1005 # report "ROW_PER_LINE not power of 2" severity FAILURE;
1006 # assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
1007 # report "geometry bits don't add up" severity FAILURE;
1008 # assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
1009 # report "geometry bits don't add up" severity FAILURE;
1010 # assert (REAL_ADDR_BITS = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
1011 # report "geometry bits don't add up" severity FAILURE;
1012 # assert (REAL_ADDR_BITS = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
1013 # report "geometry bits don't add up" severity FAILURE;
1014 # assert (64 = wishbone_data_bits)
1015 # report "Can't yet handle a wishbone width that isn't 64-bits"
1016 # severity FAILURE;
1017 # assert SET_SIZE_BITS <= TLB_LG_PGSZ
1018 # report "Set indexed by virtual address" severity FAILURE;
1019 assert (LINE_SIZE % ROW_SIZE) == 0 "LINE_SIZE not " \
1020 "multiple of ROW_SIZE -!- severity FAILURE"
1021
1022 assert (LINE_SIZE % 2) == 0 "LINE_SIZE not power of" \
1023 "2 -!- severity FAILURE"
1024
1025 assert (NUM_LINES % 2) == 0 "NUM_LINES not power of
1026 2 -!- severity FAILURE"
1027
1028 assert (ROW_PER_LINE % 2) == 0 "ROW_PER_LINE not
1029 power of 2 -!- severity FAILURE"
1030
1031 assert ROW_BITS == (INDEX_BITS + ROW_LINE_BITS)
1032 "geometry bits don't add up -!- severity FAILURE"
1033
1034 assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
1035 "geometry bits don't add up -!- severity FAILURE"
1036
1037 assert REAL_ADDR_BITS == (TAG_BITS + INDEX_BITS
1038 + LINE_OFF_BITS) "geometry bits don't add up -!-
1039 severity FAILURE"
1040
1041 assert REAL_ADDR_BITS == (TAG_BITS + ROW_BITS + ROW_OFF_BITS)
1042 "geometry bits don't add up -!- severity FAILURE"
1043
1044 assert 64 == wishbone_data_bits "Can't yet handle a
1045 wishbone width that isn't 64-bits -!- severity FAILURE"
1046
1047 assert SET_SIZE_BITS <= TLB_LG_PGSZ "Set indexed by
1048 virtual address -!- severity FAILURE"
1049
1050 # -- Latch the request in r0.req as long as we're not stalling
1051 # stage_0 : process(clk)
1052 # Latch the request in r0.req as long as we're not stalling
1053 class Stage0(Elaboratable):
1054 def __init__(self):
1055 pass
1056
1057 def elaborate(self, platform):
1058 m = Module()
1059
1060 comb = m.d.comb
1061 sync = m.d.sync
1062
1063 # variable r : reg_stage_0_t;
1064 r = RegStage0()
1065 comb += r
1066
1067 # begin
1068 # if rising_edge(clk) then
1069 # assert (d_in.valid and m_in.valid) = '0'
1070 # report "request collision loadstore vs MMU";
1071 assert ~(d_in.valid & m_in.valid) "request collision
1072 loadstore vs MMU"
1073
1074 # if m_in.valid = '1' then
1075 with m.If(m_in.valid):
1076 # r.req.valid := '1';
1077 # r.req.load := not (m_in.tlbie or m_in.tlbld);
1078 # r.req.dcbz := '0';
1079 # r.req.nc := '0';
1080 # r.req.reserve := '0';
1081 # r.req.virt_mode := '0';
1082 # r.req.priv_mode := '1';
1083 # r.req.addr := m_in.addr;
1084 # r.req.data := m_in.pte;
1085 # r.req.byte_sel := (others => '1');
1086 # r.tlbie := m_in.tlbie;
1087 # r.doall := m_in.doall;
1088 # r.tlbld := m_in.tlbld;
1089 # r.mmu_req := '1';
1090 sync += r.req.valid.eq(1)
1091 sync += r.req.load.eq(~(m_in.tlbie | m_in.tlbld))
1092 sync += r.req.priv_mode.eq(1)
1093 sync += r.req.addr.eq(m_in.addr)
1094 sync += r.req.data.eq(m_in.pte)
1095 sync += r.req.byte_sel.eq(1)
1096 sync += r.tlbie.eq(m_in.tlbie)
1097 sync += r.doall.eq(m_in.doall)
1098 sync += r.tlbld.eq(m_in.tlbld)
1099 sync += r.mmu_req.eq(1)
1100 # else
1101 with m.Else():
1102 # r.req := d_in;
1103 # r.tlbie := '0';
1104 # r.doall := '0';
1105 # r.tlbld := '0';
1106 # r.mmu_req := '0';
1107 sync += r.req.eq(d_in)
1108 # end if;
1109 # if rst = '1' then
1110 # r0_full <= '0';
1111 # elsif r1.full = '0' or r0_full = '0' then
1112 with m.If(~r1.full | ~r0_full):
1113 # r0 <= r;
1114 # r0_full <= r.req.valid;
1115 sync += r0.eq(r)
1116 sync += r0_full.eq(r.req.valid)
1117 # end if;
1118 # end if;
1119 # end process;
1120 #
1121 # -- we don't yet handle collisions between loadstore1 requests
1122 # -- and MMU requests
1123 # m_out.stall <= '0';
1124 # we don't yet handle collisions between loadstore1 requests
1125 # and MMU requests
1126 comb += m_out.stall.eq(0)
1127
1128 # -- Hold off the request in r0 when r1 has an uncompleted request
1129 # r0_stall <= r0_full and r1.full;
1130 # r0_valid <= r0_full and not r1.full;
1131 # stall_out <= r0_stall;
1132 # Hold off the request in r0 when r1 has an uncompleted request
1133 comb += r0_stall.eq(r0_full & r1.full)
1134 comb += r0_valid.eq(r0_full & ~r1.full)
1135 comb += stall_out.eq(r0_stall)
1136
1137 # -- TLB
1138 # -- Operates in the second cycle on the request latched in r0.req.
1139 # -- TLB updates write the entry at the end of the second cycle.
1140 # tlb_read : process(clk)
1141 # TLB
1142 # Operates in the second cycle on the request latched in r0.req.
1143 # TLB updates write the entry at the end of the second cycle.
1144 class TLBRead(Elaboratable):
1145 def __init__(self):
1146 pass
1147
1148 def elaborate(self, platform):
1149 m = Module()
1150
1151 comb = m.d.comb
1152 sync = m.d.sync
1153
1154 # variable index : tlb_index_t;
1155 # variable addrbits :
1156 # std_ulogic_vector(TLB_SET_BITS - 1 downto 0);
1157 index = TLB_SET_SIZE
1158 addrbits = Signal(TLB_SET_BITS)
1159
1160 comb += index
1161 comb += addrbits
1162
1163 # begin
1164 # if rising_edge(clk) then
1165 # if m_in.valid = '1' then
1166 with m.If(m_in.valid):
1167 # addrbits := m_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
1168 # - 1 downto TLB_LG_PGSZ);
1169 sync += addrbits.eq(m_in.addr[
1170 TLB_LG_PGSZ:TLB_LG_PGSZ + TLB_SET_BITS
1171 ])
1172 # else
1173 with m.Else():
1174 # addrbits := d_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
1175 # - 1 downto TLB_LG_PGSZ);
1176 sync += addrbits.eq(d_in.addr[
1177 TLB_LG_PGSZ:TLB_LG_PGSZ + TLB_SET_BITS
1178 ])
1179 # end if;
1180
1181 # index := to_integer(unsigned(addrbits));
1182 sync += index.eq(addrbits)
1183 # -- If we have any op and the previous op isn't finished,
1184 # -- then keep the same output for next cycle.
1185 # if r0_stall = '0' then
1186 # If we have any op and the previous op isn't finished,
1187 # then keep the same output for next cycle.
1188 with m.If(~r0_stall):
1189 sync += tlb_valid_way.eq(dtlb_valids[index])
1190 sync += tlb_tag_way.eq(dtlb_tags[index])
1191 sync += tlb_pte_way.eq(dtlb_ptes[index])
1192 # end if;
1193 # end if;
1194 # end process;
1195
1196 # -- Generate TLB PLRUs
1197 # maybe_tlb_plrus: if TLB_NUM_WAYS > 1 generate
1198 # Generate TLB PLRUs
1199 class MaybeTLBPLRUs(Elaboratable):
1200 def __init__(self):
1201 pass
1202
1203 def elaborate(self, platform):
1204 m = Module()
1205
1206 comb = m.d.comb
1207 sync = m.d.sync
1208
1209 with m.If(TLB_NUM_WAYS > 1):
1210 # begin
1211 # TODO understand how to conver generate statements
1212 # tlb_plrus: for i in 0 to TLB_SET_SIZE - 1 generate
1213 # -- TLB PLRU interface
1214 # signal tlb_plru_acc :
1215 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
1216 # signal tlb_plru_acc_en : std_ulogic;
1217 # signal tlb_plru_out :
1218 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
1219 # begin
1220 # tlb_plru : entity work.plru
1221 # generic map (
1222 # BITS => TLB_WAY_BITS
1223 # )
1224 # port map (
1225 # clk => clk,
1226 # rst => rst,
1227 # acc => tlb_plru_acc,
1228 # acc_en => tlb_plru_acc_en,
1229 # lru => tlb_plru_out
1230 # );
1231 #
1232 # process(all)
1233 # begin
1234 # -- PLRU interface
1235 # if r1.tlb_hit_index = i then
1236 # tlb_plru_acc_en <= r1.tlb_hit;
1237 # else
1238 # tlb_plru_acc_en <= '0';
1239 # end if;
1240 # tlb_plru_acc <=
1241 # std_ulogic_vector(to_unsigned(
1242 # r1.tlb_hit_way, TLB_WAY_BITS
1243 # ));
1244 # tlb_plru_victim(i) <= tlb_plru_out;
1245 # end process;
1246 # end generate;
1247 # end generate;
1248 # end TODO
1249 #
1250 # tlb_search : process(all)
1251 class TLBSearch(Elaboratable):
1252 def __init__(self):
1253 pass
1254
1255 def elborate(self, platform):
1256 m = Module()
1257
1258 comb = m.d.comb
1259 sync = m.d.sync
1260
1261 # variable hitway : tlb_way_t;
1262 # variable hit : std_ulogic;
1263 # variable eatag : tlb_tag_t;
1264 hitway = TLBWay()
1265 hit = Signal()
1266 eatag = TLBTag()
1267
1268 comb += hitway
1269 comb += hit
1270 comb += eatag
1271
1272 # begin
1273 # tlb_req_index <=
1274 # to_integer(unsigned(r0.req.addr(
1275 # TLB_LG_PGSZ + TLB_SET_BITS - 1 downto TLB_LG_PGSZ
1276 # )));
1277 # hitway := 0;
1278 # hit := '0';
1279 # eatag := r0.req.addr(63 downto TLB_LG_PGSZ + TLB_SET_BITS);
1280 # for i in tlb_way_t loop
1281 # if tlb_valid_way(i) = '1' and
1282 # read_tlb_tag(i, tlb_tag_way) = eatag then
1283 # hitway := i;
1284 # hit := '1';
1285 # end if;
1286 # end loop;
1287 # tlb_hit <= hit and r0_valid;
1288 # tlb_hit_way <= hitway;
1289 comb += tlb_req_index.eq(r0.req.addr[
1290 TLB_LG_PGSZ:TLB_LG_PGSZ + TLB_SET_BITS
1291 ])
1292
1293 comb += eatag.eq(r0.req.addr[
1294 TLB_LG_PGSZ + TLB_SET_BITS:64
1295 ])
1296
1297 for i in TLBWay():
1298 with m.If(tlb_valid_way(i)
1299 & read_tlb_tag(i, tlb_tag_way) == eatag):
1300
1301 comb += hitway.eq(i)
1302 comb += hit.eq(1)
1303
1304 comb += tlb_hit.eq(hit & r0_valid)
1305 comb += tlb_hit_way.eq(hitway)
1306
1307 # if tlb_hit = '1' then
1308 with m.If(tlb_hit):
1309 # pte <= read_tlb_pte(hitway, tlb_pte_way);
1310 comb += pte.eq(read_tlb_pte(hitway, tlb_pte_way))
1311 # else
1312 with m.Else():
1313 # pte <= (others => '0');
1314 comb += pte.eq(0)
1315 # end if;
1316 # valid_ra <= tlb_hit or not r0.req.virt_mode;
1317 comb += valid_ra.eq(tlb_hit | ~r0.req.virt_mode)
1318 # if r0.req.virt_mode = '1' then
1319 with m.If(r0.req.virt_mode):
1320 # ra <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
1321 # r0.req.addr(TLB_LG_PGSZ - 1 downto ROW_OFF_BITS) &
1322 # (ROW_OFF_BITS-1 downto 0 => '0');
1323 # perm_attr <= extract_perm_attr(pte);
1324 comb += ra.eq(Cat(
1325 Const(ROW_OFF_BITS, ROW_OFF_BITS),
1326 r0.req.addr[ROW_OFF_BITS:TLB_LG_PGSZ],
1327 pte[TLB_LG_PGSZ:REAL_ADDR_BITS]
1328 ))
1329 comb += perm_attr.eq(extract_perm_attr(pte))
1330 # else
1331 with m.Else():
1332 # ra <= r0.req.addr(
1333 # REAL_ADDR_BITS - 1 downto ROW_OFF_BITS
1334 # ) & (ROW_OFF_BITS-1 downto 0 => '0');
1335 comb += ra.eq(Cat(
1336 Const(ROW_OFF_BITS, ROW_OFF_BITS),
1337 r0.rq.addr[ROW_OFF_BITS:REAL_ADDR_BITS]
1338 )
1339
1340 # perm_attr <= real_mode_perm_attr;
1341 comb += perm_attr.eq(real_mode_perm_attr)
1342 # end if;
1343 # end process;
1344
1345 # tlb_update : process(clk)
1346 class TLBUpdate(Elaboratable):
1347 def __init__(self):
1348 pass
1349
1350 def elaborate(self, platform):
1351 m = Module()
1352
1353 comb = m.d.comb
1354 sync = m.d.sync
1355
1356 # variable tlbie : std_ulogic;
1357 # variable tlbwe : std_ulogic;
1358 # variable repl_way : tlb_way_t;
1359 # variable eatag : tlb_tag_t;
1360 # variable tagset : tlb_way_tags_t;
1361 # variable pteset : tlb_way_ptes_t;
1362 tlbie = Signal()
1363 tlbwe = Signal()
1364 repl_way = TLBWay()
1365 eatag = TLBTag()
1366 tagset = TLBWayTags()
1367 pteset = TLBWayPtes()
1368
1369 comb += tlbie
1370 comb += tlbwe
1371 comb += repl_way
1372 comb += eatag
1373 comb += tagset
1374 comb += pteset
1375
1376 # begin
1377 # if rising_edge(clk) then
1378 # tlbie := r0_valid and r0.tlbie;
1379 # tlbwe := r0_valid and r0.tlbldoi;
1380 sync += tlbie.eq(r0_valid & r0.tlbie)
1381 sync += tlbwe.eq(r0_valid & r0.tlbldoi)
1382
1383 # if rst = '1' or (tlbie = '1' and r0.doall = '1') then
1384 # with m.If (TODO understand how signal resets work in nmigen)
1385 # -- clear all valid bits at once
1386 # for i in tlb_index_t loop
1387 # dtlb_valids(i) <= (others => '0');
1388 # end loop;
1389 # clear all valid bits at once
1390 for i in range(TLB_SET_SIZE):
1391 sync += dtlb_valids[i].eq(0)
1392 # elsif tlbie = '1' then
1393 with m.Elif(tlbie):
1394 # if tlb_hit = '1' then
1395 with m.If(tlb_hit):
1396 # dtlb_valids(tlb_req_index)(tlb_hit_way) <= '0';
1397 sync += dtlb_valids[tlb_req_index][tlb_hit_way].eq(0)
1398 # end if;
1399 # elsif tlbwe = '1' then
1400 with m.Elif(tlbwe):
1401 # if tlb_hit = '1' then
1402 with m.If(tlb_hit):
1403 # repl_way := tlb_hit_way;
1404 sync += repl_way.eq(tlb_hit_way)
1405 # else
1406 with m.Else():
1407 # repl_way := to_integer(unsigned(
1408 # tlb_plru_victim(tlb_req_index)));
1409 sync += repl_way.eq(tlb_plru_victim[tlb_req_index])
1410 # end if;
1411 # eatag := r0.req.addr(
1412 # 63 downto TLB_LG_PGSZ + TLB_SET_BITS
1413 # );
1414 # tagset := tlb_tag_way;
1415 # write_tlb_tag(repl_way, tagset, eatag);
1416 # dtlb_tags(tlb_req_index) <= tagset;
1417 # pteset := tlb_pte_way;
1418 # write_tlb_pte(repl_way, pteset, r0.req.data);
1419 # dtlb_ptes(tlb_req_index) <= pteset;
1420 # dtlb_valids(tlb_req_index)(repl_way) <= '1';
1421 sync += eatag.eq(r0.req.addr[TLB_LG_PGSZ + TLB_SET_BITS:64])
1422 sync += tagset.eq(tlb_tag_way)
1423 sync += write_tlb_tag(repl_way, tagset, eatag)
1424 sync += dtlb_tags[tlb_req_index].eq(tagset)
1425 sync += pteset.eq(tlb_pte_way)
1426 sync += write_tlb_pte(repl_way, pteset, r0.req.data)
1427 sync += dtlb_ptes[tlb_req_index].eq(pteset)
1428 sync += dtlb_valids[tlb_req_index][repl_way].eq(1)
1429 # end if;
1430 # end if;
1431 # end process;
1432
1433 # -- Generate PLRUs
1434 # maybe_plrus: if NUM_WAYS > 1 generate
1435 class MaybePLRUs(Elaboratable):
1436 def __init__(self):
1437 pass
1438
1439 def elaborate(self, platform):
1440 m = Module()
1441
1442 comb = m.d.comb
1443 sync = m.d.sync
1444
1445 # begin
1446 # TODO learn translation of generate into nmgien @lkcl
1447 # plrus: for i in 0 to NUM_LINES-1 generate
1448 # -- PLRU interface
1449 # signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
1450 # signal plru_acc_en : std_ulogic;
1451 # signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
1452 #
1453 # begin
1454 # TODO learn tranlation of entity, generic map, port map in
1455 # nmigen @lkcl
1456 # plru : entity work.plru
1457 # generic map (
1458 # BITS => WAY_BITS
1459 # )
1460 # port map (
1461 # clk => clk,
1462 # rst => rst,
1463 # acc => plru_acc,
1464 # acc_en => plru_acc_en,
1465 # lru => plru_out
1466 # );
1467 #
1468 # process(all)
1469 # begin
1470 # -- PLRU interface
1471 # if r1.hit_index = i then
1472 # plru_acc_en <= r1.cache_hit;
1473 # else
1474 # plru_acc_en <= '0';
1475 # end if;
1476 # plru_acc <= std_ulogic_vector(to_unsigned(
1477 # r1.hit_way, WAY_BITS
1478 # ));
1479 # plru_victim(i) <= plru_out;
1480 # end process;
1481 # end generate;
1482 # end generate;
1483 #
1484 # -- Cache tag RAM read port
1485 # cache_tag_read : process(clk)
1486 # variable index : index_t;
1487 # begin
1488 # if rising_edge(clk) then
1489 # if r0_stall = '1' then
1490 # index := req_index;
1491 # elsif m_in.valid = '1' then
1492 # index := get_index(m_in.addr);
1493 # else
1494 # index := get_index(d_in.addr);
1495 # end if;
1496 # cache_tag_set <= cache_tags(index);
1497 # end if;
1498 # end process;
1499 #
1500 # -- Cache request parsing and hit detection
1501 # dcache_request : process(all)
1502 # variable is_hit : std_ulogic;
1503 # variable hit_way : way_t;
1504 # variable op : op_t;
1505 # variable opsel : std_ulogic_vector(2 downto 0);
1506 # variable go : std_ulogic;
1507 # variable nc : std_ulogic;
1508 # variable s_hit : std_ulogic;
1509 # variable s_tag : cache_tag_t;
1510 # variable s_pte : tlb_pte_t;
1511 # variable s_ra : std_ulogic_vector(
1512 # REAL_ADDR_BITS - 1 downto 0
1513 # );
1514 # variable hit_set : std_ulogic_vector(
1515 # TLB_NUM_WAYS - 1 downto 0
1516 # );
1517 # variable hit_way_set : hit_way_set_t;
1518 # variable rel_matches : std_ulogic_vector(
1519 # TLB_NUM_WAYS - 1 downto 0
1520 # );
1521 # variable rel_match : std_ulogic;
1522 # begin
1523 # -- Extract line, row and tag from request
1524 # req_index <= get_index(r0.req.addr);
1525 # req_row <= get_row(r0.req.addr);
1526 # req_tag <= get_tag(ra);
1527 #
1528 # go := r0_valid and not (r0.tlbie or r0.tlbld)
1529 # and not r1.ls_error;
1530 #
1531 # -- Test if pending request is a hit on any way
1532 # -- In order to make timing in virtual mode,
1533 # -- when we are using the TLB, we compare each
1534 # --way with each of the real addresses from each way of
1535 # -- the TLB, and then decide later which match to use.
1536 # hit_way := 0;
1537 # is_hit := '0';
1538 # rel_match := '0';
1539 # if r0.req.virt_mode = '1' then
1540 # rel_matches := (others => '0');
1541 # for j in tlb_way_t loop
1542 # hit_way_set(j) := 0;
1543 # s_hit := '0';
1544 # s_pte := read_tlb_pte(j, tlb_pte_way);
1545 # s_ra :=
1546 # s_pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ)
1547 # & r0.req.addr(TLB_LG_PGSZ - 1 downto 0);
1548 # s_tag := get_tag(s_ra);
1549 # for i in way_t loop
1550 # if go = '1' and cache_valids(req_index)(i) = '1'
1551 # and read_tag(i, cache_tag_set) = s_tag
1552 # and tlb_valid_way(j) = '1' then
1553 # hit_way_set(j) := i;
1554 # s_hit := '1';
1555 # end if;
1556 # end loop;
1557 # hit_set(j) := s_hit;
1558 # if s_tag = r1.reload_tag then
1559 # rel_matches(j) := '1';
1560 # end if;
1561 # end loop;
1562 # if tlb_hit = '1' then
1563 # is_hit := hit_set(tlb_hit_way);
1564 # hit_way := hit_way_set(tlb_hit_way);
1565 # rel_match := rel_matches(tlb_hit_way);
1566 # end if;
1567 # else
1568 # s_tag := get_tag(r0.req.addr);
1569 # for i in way_t loop
1570 # if go = '1' and cache_valids(req_index)(i) = '1' and
1571 # read_tag(i, cache_tag_set) = s_tag then
1572 # hit_way := i;
1573 # is_hit := '1';
1574 # end if;
1575 # end loop;
1576 # if s_tag = r1.reload_tag then
1577 # rel_match := '1';
1578 # end if;
1579 # end if;
1580 # req_same_tag <= rel_match;
1581 #
1582 # -- See if the request matches the line currently being reloaded
1583 # if r1.state = RELOAD_WAIT_ACK and req_index = r1.store_index
1584 # and rel_match = '1' then
1585 # -- For a store, consider this a hit even if the row isn't
1586 # -- valid since it will be by the time we perform the store.
1587 # -- For a load, check the appropriate row valid bit.
1588 # is_hit :=
1589 # not r0.req.load or r1.rows_valid(req_row mod ROW_PER_LINE);
1590 # hit_way := replace_way;
1591 # end if;
1592 #
1593 # -- Whether to use forwarded data for a load or not
1594 # use_forward1_next <= '0';
1595 # if get_row(r1.req.real_addr) = req_row
1596 # and r1.req.hit_way = hit_way then
1597 # -- Only need to consider r1.write_bram here, since if we
1598 # -- are writing refill data here, then we don't have a
1599 # -- cache hit this cycle on the line being refilled.
1600 # -- (There is the possibility that the load following the
1601 # -- load miss that started the refill could be to the old
1602 # -- contents of the victim line, since it is a couple of
1603 # -- cycles after the refill starts before we see the updated
1604 # -- cache tag. In that case we don't use the bypass.)
1605 # use_forward1_next <= r1.write_bram;
1606 # end if;
1607 # use_forward2_next <= '0';
1608 # if r1.forward_row1 = req_row and r1.forward_way1 = hit_way then
1609 # use_forward2_next <= r1.forward_valid1;
1610 # end if;
1611 #
1612 # -- The way that matched on a hit
1613 # req_hit_way <= hit_way;
1614 #
1615 # -- The way to replace on a miss
1616 # if r1.write_tag = '1' then
1617 # replace_way <= to_integer(unsigned(
1618 # plru_victim(r1.store_index)
1619 # ));
1620 # else
1621 # replace_way <= r1.store_way;
1622 # end if;
1623 #
1624 # -- work out whether we have permission for this access
1625 # -- NB we don't yet implement AMR, thus no KUAP
1626 # rc_ok <= perm_attr.reference and
1627 # (r0.req.load or perm_attr.changed);
1628 # perm_ok <= (r0.req.priv_mode or not perm_attr.priv) and
1629 # (perm_attr.wr_perm or (r0.req.load
1630 # and perm_attr.rd_perm));
1631 # access_ok <= valid_ra and perm_ok and rc_ok;
1632 #
1633 # -- Combine the request and cache hit status to decide what
1634 # -- operation needs to be done
1635 # --
1636 # nc := r0.req.nc or perm_attr.nocache;
1637 # op := OP_NONE;
1638 # if go = '1' then
1639 # if access_ok = '0' then
1640 # op := OP_BAD;
1641 # elsif cancel_store = '1' then
1642 # op := OP_STCX_FAIL;
1643 # else
1644 # opsel := r0.req.load & nc & is_hit;
1645 # case opsel is
1646 # when "101" => op := OP_LOAD_HIT;
1647 # when "100" => op := OP_LOAD_MISS;
1648 # when "110" => op := OP_LOAD_NC;
1649 # when "001" => op := OP_STORE_HIT;
1650 # when "000" => op := OP_STORE_MISS;
1651 # when "010" => op := OP_STORE_MISS;
1652 # when "011" => op := OP_BAD;
1653 # when "111" => op := OP_BAD;
1654 # when others => op := OP_NONE;
1655 # end case;
1656 # end if;
1657 # end if;
1658 # req_op <= op;
1659 # req_go <= go;
1660 #
1661 # -- Version of the row number that is valid one cycle earlier
1662 # -- in the cases where we need to read the cache data BRAM.
1663 # -- If we're stalling then we need to keep reading the last
1664 # -- row requested.
1665 # if r0_stall = '0' then
1666 # if m_in.valid = '1' then
1667 # early_req_row <= get_row(m_in.addr);
1668 # else
1669 # early_req_row <= get_row(d_in.addr);
1670 # end if;
1671 # else
1672 # early_req_row <= req_row;
1673 # end if;
1674 # end process;
1675 #
1676 # -- Wire up wishbone request latch out of stage 1
1677 # wishbone_out <= r1.wb;
1678 #
1679 # -- Handle load-with-reservation and store-conditional instructions
1680 # reservation_comb: process(all)
1681 # begin
1682 # cancel_store <= '0';
1683 # set_rsrv <= '0';
1684 # clear_rsrv <= '0';
1685 # if r0_valid = '1' and r0.req.reserve = '1' then
1686 # -- XXX generate alignment interrupt if address
1687 # -- is not aligned XXX or if r0.req.nc = '1'
1688 # if r0.req.load = '1' then
1689 # -- load with reservation
1690 # set_rsrv <= '1';
1691 # else
1692 # -- store conditional
1693 # clear_rsrv <= '1';
1694 # if reservation.valid = '0' or r0.req.addr(63
1695 # downto LINE_OFF_BITS) /= reservation.addr then
1696 # cancel_store <= '1';
1697 # end if;
1698 # end if;
1699 # end if;
1700 # end process;
1701 #
1702 # reservation_reg: process(clk)
1703 # begin
1704 # if rising_edge(clk) then
1705 # if rst = '1' then
1706 # reservation.valid <= '0';
1707 # elsif r0_valid = '1' and access_ok = '1' then
1708 # if clear_rsrv = '1' then
1709 # reservation.valid <= '0';
1710 # elsif set_rsrv = '1' then
1711 # reservation.valid <= '1';
1712 # reservation.addr <=
1713 # r0.req.addr(63 downto LINE_OFF_BITS);
1714 # end if;
1715 # end if;
1716 # end if;
1717 # end process;
1718 #
1719 # -- Return data for loads & completion control logic
1720 # --
1721 # writeback_control: process(all)
1722 # variable data_out : std_ulogic_vector(63 downto 0);
1723 # variable data_fwd : std_ulogic_vector(63 downto 0);
1724 # variable j : integer;
1725 # begin
1726 # -- Use the bypass if are reading the row that was
1727 # -- written 1 or 2 cycles ago, including for the
1728 # -- slow_valid = 1 case (i.e. completing a load
1729 # -- miss or a non-cacheable load).
1730 # if r1.use_forward1 = '1' then
1731 # data_fwd := r1.forward_data1;
1732 # else
1733 # data_fwd := r1.forward_data2;
1734 # end if;
1735 # data_out := cache_out(r1.hit_way);
1736 # for i in 0 to 7 loop
1737 # j := i * 8;
1738 # if r1.forward_sel(i) = '1' then
1739 # data_out(j + 7 downto j) := data_fwd(j + 7 downto j);
1740 # end if;
1741 # end loop;
1742 #
1743 # d_out.valid <= r1.ls_valid;
1744 # d_out.data <= data_out;
1745 # d_out.store_done <= not r1.stcx_fail;
1746 # d_out.error <= r1.ls_error;
1747 # d_out.cache_paradox <= r1.cache_paradox;
1748 #
1749 # -- Outputs to MMU
1750 # m_out.done <= r1.mmu_done;
1751 # m_out.err <= r1.mmu_error;
1752 # m_out.data <= data_out;
1753 #
1754 # -- We have a valid load or store hit or we just completed
1755 # -- a slow op such as a load miss, a NC load or a store
1756 # --
1757 # -- Note: the load hit is delayed by one cycle. However it
1758 # -- can still not collide with r.slow_valid (well unless I
1759 # -- miscalculated) because slow_valid can only be set on a
1760 # -- subsequent request and not on its first cycle (the state
1761 # -- machine must have advanced), which makes slow_valid
1762 # -- at least 2 cycles from the previous hit_load_valid.
1763 #
1764 # -- Sanity: Only one of these must be set in any given cycle
1765 # assert (r1.slow_valid and r1.stcx_fail) /= '1'
1766 # report "unexpected slow_valid collision with stcx_fail"
1767 # severity FAILURE;
1768 # assert ((r1.slow_valid or r1.stcx_fail) and r1.hit_load_valid)
1769 # /= '1' report "unexpected hit_load_delayed collision with
1770 # slow_valid" severity FAILURE;
1771 #
1772 # if r1.mmu_req = '0' then
1773 # -- Request came from loadstore1...
1774 # -- Load hit case is the standard path
1775 # if r1.hit_load_valid = '1' then
1776 # report
1777 # "completing load hit data=" & to_hstring(data_out);
1778 # end if;
1779 #
1780 # -- error cases complete without stalling
1781 # if r1.ls_error = '1' then
1782 # report "completing ld/st with error";
1783 # end if;
1784 #
1785 # -- Slow ops (load miss, NC, stores)
1786 # if r1.slow_valid = '1' then
1787 # report
1788 # "completing store or load miss data="
1789 # & to_hstring(data_out);
1790 # end if;
1791 #
1792 # else
1793 # -- Request came from MMU
1794 # if r1.hit_load_valid = '1' then
1795 # report "completing load hit to MMU, data="
1796 # & to_hstring(m_out.data);
1797 # end if;
1798 #
1799 # -- error cases complete without stalling
1800 # if r1.mmu_error = '1' then
1801 # report "completing MMU ld with error";
1802 # end if;
1803 #
1804 # -- Slow ops (i.e. load miss)
1805 # if r1.slow_valid = '1' then
1806 # report "completing MMU load miss, data="
1807 # & to_hstring(m_out.data);
1808 # end if;
1809 # end if;
1810 #
1811 # end process;
1812 #
1813 #
1814 # -- Generate a cache RAM for each way. This handles the normal
1815 # -- reads, writes from reloads and the special store-hit update
1816 # -- path as well.
1817 # --
1818 # -- Note: the BRAMs have an extra read buffer, meaning the output
1819 # -- is pipelined an extra cycle. This differs from the
1820 # -- icache. The writeback logic needs to take that into
1821 # -- account by using 1-cycle delayed signals for load hits.
1822 # --
1823 # rams: for i in 0 to NUM_WAYS-1 generate
1824 # signal do_read : std_ulogic;
1825 # signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1826 # signal do_write : std_ulogic;
1827 # signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1828 # signal wr_data :
1829 # std_ulogic_vector(wishbone_data_bits-1 downto 0);
1830 # signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
1831 # signal wr_sel_m : std_ulogic_vector(ROW_SIZE-1 downto 0);
1832 # signal dout : cache_row_t;
1833 # begin
1834 # way: entity work.cache_ram
1835 # generic map (
1836 # ROW_BITS => ROW_BITS,
1837 # WIDTH => wishbone_data_bits,
1838 # ADD_BUF => true
1839 # )
1840 # port map (
1841 # clk => clk,
1842 # rd_en => do_read,
1843 # rd_addr => rd_addr,
1844 # rd_data => dout,
1845 # wr_sel => wr_sel_m,
1846 # wr_addr => wr_addr,
1847 # wr_data => wr_data
1848 # );
1849 # process(all)
1850 # begin
1851 # -- Cache hit reads
1852 # do_read <= '1';
1853 # rd_addr <=
1854 # std_ulogic_vector(to_unsigned(early_req_row, ROW_BITS));
1855 # cache_out(i) <= dout;
1856 #
1857 # -- Write mux:
1858 # --
1859 # -- Defaults to wishbone read responses (cache refill)
1860 # --
1861 # -- For timing, the mux on wr_data/sel/addr is not
1862 # -- dependent on anything other than the current state.
1863 # wr_sel_m <= (others => '0');
1864 #
1865 # do_write <= '0';
1866 # if r1.write_bram = '1' then
1867 # -- Write store data to BRAM. This happens one
1868 # -- cycle after the store is in r0.
1869 # wr_data <= r1.req.data;
1870 # wr_sel <= r1.req.byte_sel;
1871 # wr_addr <= std_ulogic_vector(to_unsigned(
1872 # get_row(r1.req.real_addr), ROW_BITS
1873 # ));
1874 # if i = r1.req.hit_way then
1875 # do_write <= '1';
1876 # end if;
1877 # else
1878 # -- Otherwise, we might be doing a reload or a DCBZ
1879 # if r1.dcbz = '1' then
1880 # wr_data <= (others => '0');
1881 # else
1882 # wr_data <= wishbone_in.dat;
1883 # end if;
1884 # wr_addr <= std_ulogic_vector(to_unsigned(
1885 # r1.store_row, ROW_BITS
1886 # ));
1887 # wr_sel <= (others => '1');
1888 #
1889 # if r1.state = RELOAD_WAIT_ACK and
1890 # wishbone_in.ack = '1' and replace_way = i then
1891 # do_write <= '1';
1892 # end if;
1893 # end if;
1894 #
1895 # -- Mask write selects with do_write since BRAM
1896 # -- doesn't have a global write-enable
1897 # if do_write = '1' then
1898 # wr_sel_m <= wr_sel;
1899 # end if;
1900 #
1901 # end process;
1902 # end generate;
1903 #
1904 # -- Cache hit synchronous machine for the easy case.
1905 # -- This handles load hits.
1906 # -- It also handles error cases (TLB miss, cache paradox)
1907 # dcache_fast_hit : process(clk)
1908 # begin
1909 # if rising_edge(clk) then
1910 # if req_op /= OP_NONE then
1911 # report "op:" & op_t'image(req_op) &
1912 # " addr:" & to_hstring(r0.req.addr) &
1913 # " nc:" & std_ulogic'image(r0.req.nc) &
1914 # " idx:" & integer'image(req_index) &
1915 # " tag:" & to_hstring(req_tag) &
1916 # " way: " & integer'image(req_hit_way);
1917 # end if;
1918 # if r0_valid = '1' then
1919 # r1.mmu_req <= r0.mmu_req;
1920 # end if;
1921 #
1922 # -- Fast path for load/store hits.
1923 # -- Set signals for the writeback controls.
1924 # r1.hit_way <= req_hit_way;
1925 # r1.hit_index <= req_index;
1926 # if req_op = OP_LOAD_HIT then
1927 # r1.hit_load_valid <= '1';
1928 # else
1929 # r1.hit_load_valid <= '0';
1930 # end if;
1931 # if req_op = OP_LOAD_HIT or req_op = OP_STORE_HIT then
1932 # r1.cache_hit <= '1';
1933 # else
1934 # r1.cache_hit <= '0';
1935 # end if;
1936 #
1937 # if req_op = OP_BAD then
1938 # report "Signalling ld/st error valid_ra=" &
1939 # std_ulogic'image(valid_ra) & " rc_ok=" &
1940 # std_ulogic'image(rc_ok) & " perm_ok=" &
1941 # std_ulogic'image(perm_ok);
1942 # r1.ls_error <= not r0.mmu_req;
1943 # r1.mmu_error <= r0.mmu_req;
1944 # r1.cache_paradox <= access_ok;
1945 # else
1946 # r1.ls_error <= '0';
1947 # r1.mmu_error <= '0';
1948 # r1.cache_paradox <= '0';
1949 # end if;
1950 #
1951 # if req_op = OP_STCX_FAIL then
1952 # r1.stcx_fail <= '1';
1953 # else
1954 # r1.stcx_fail <= '0';
1955 # end if;
1956 #
1957 # -- Record TLB hit information for updating TLB PLRU
1958 # r1.tlb_hit <= tlb_hit;
1959 # r1.tlb_hit_way <= tlb_hit_way;
1960 # r1.tlb_hit_index <= tlb_req_index;
1961 #
1962 # end if;
1963 # end process;
1964 #
1965 # -- Memory accesses are handled by this state machine:
1966 # --
1967 # -- * Cache load miss/reload (in conjunction with "rams")
1968 # -- * Load hits for non-cachable forms
1969 # -- * Stores (the collision case is handled in "rams")
1970 # --
1971 # -- All wishbone requests generation is done here.
1972 # -- This machine operates at stage 1.
1973 # dcache_slow : process(clk)
1974 # variable stbs_done : boolean;
1975 # variable req : mem_access_request_t;
1976 # variable acks : unsigned(2 downto 0);
1977 # begin
1978 # if rising_edge(clk) then
1979 # r1.use_forward1 <= use_forward1_next;
1980 # r1.forward_sel <= (others => '0');
1981 # if use_forward1_next = '1' then
1982 # r1.forward_sel <= r1.req.byte_sel;
1983 # elsif use_forward2_next = '1' then
1984 # r1.forward_sel <= r1.forward_sel1;
1985 # end if;
1986 #
1987 # r1.forward_data2 <= r1.forward_data1;
1988 # if r1.write_bram = '1' then
1989 # r1.forward_data1 <= r1.req.data;
1990 # r1.forward_sel1 <= r1.req.byte_sel;
1991 # r1.forward_way1 <= r1.req.hit_way;
1992 # r1.forward_row1 <= get_row(r1.req.real_addr);
1993 # r1.forward_valid1 <= '1';
1994 # else
1995 # if r1.dcbz = '1' then
1996 # r1.forward_data1 <= (others => '0');
1997 # else
1998 # r1.forward_data1 <= wishbone_in.dat;
1999 # end if;
2000 # r1.forward_sel1 <= (others => '1');
2001 # r1.forward_way1 <= replace_way;
2002 # r1.forward_row1 <= r1.store_row;
2003 # r1.forward_valid1 <= '0';
2004 # end if;
2005 #
2006 # -- On reset, clear all valid bits to force misses
2007 # if rst = '1' then
2008 # for i in index_t loop
2009 # cache_valids(i) <= (others => '0');
2010 # end loop;
2011 # r1.state <= IDLE;
2012 # r1.full <= '0';
2013 # r1.slow_valid <= '0';
2014 # r1.wb.cyc <= '0';
2015 # r1.wb.stb <= '0';
2016 # r1.ls_valid <= '0';
2017 # r1.mmu_done <= '0';
2018 #
2019 # -- Not useful normally but helps avoiding
2020 # -- tons of sim warnings
2021 # r1.wb.adr <= (others => '0');
2022 # else
2023 # -- One cycle pulses reset
2024 # r1.slow_valid <= '0';
2025 # r1.write_bram <= '0';
2026 # r1.inc_acks <= '0';
2027 # r1.dec_acks <= '0';
2028 #
2029 # r1.ls_valid <= '0';
2030 # -- complete tlbies and TLB loads in the third cycle
2031 # r1.mmu_done <= r0_valid and (r0.tlbie or r0.tlbld);
2032 # if req_op = OP_LOAD_HIT or req_op = OP_STCX_FAIL then
2033 # if r0.mmu_req = '0' then
2034 # r1.ls_valid <= '1';
2035 # else
2036 # r1.mmu_done <= '1';
2037 # end if;
2038 # end if;
2039 #
2040 # if r1.write_tag = '1' then
2041 # -- Store new tag in selected way
2042 # for i in 0 to NUM_WAYS-1 loop
2043 # if i = replace_way then
2044 # cache_tags(r1.store_index)(
2045 # (i + 1) * TAG_WIDTH - 1
2046 # downto i * TAG_WIDTH
2047 # ) <=
2048 # (TAG_WIDTH - 1 downto TAG_BITS => '0')
2049 # & r1.reload_tag;
2050 # end if;
2051 # end loop;
2052 # r1.store_way <= replace_way;
2053 # r1.write_tag <= '0';
2054 # end if;
2055 #
2056 # -- Take request from r1.req if there is one there,
2057 # -- else from req_op, ra, etc.
2058 # if r1.full = '1' then
2059 # req := r1.req;
2060 # else
2061 # req.op := req_op;
2062 # req.valid := req_go;
2063 # req.mmu_req := r0.mmu_req;
2064 # req.dcbz := r0.req.dcbz;
2065 # req.real_addr := ra;
2066 # -- Force data to 0 for dcbz
2067 # if r0.req.dcbz = '0' then
2068 # req.data := r0.req.data;
2069 # else
2070 # req.data := (others => '0');
2071 # end if;
2072 # -- Select all bytes for dcbz
2073 # -- and for cacheable loads
2074 # if r0.req.dcbz = '1'
2075 # or (r0.req.load = '1' and r0.req.nc = '0') then
2076 # req.byte_sel := (others => '1');
2077 # else
2078 # req.byte_sel := r0.req.byte_sel;
2079 # end if;
2080 # req.hit_way := req_hit_way;
2081 # req.same_tag := req_same_tag;
2082 #
2083 # -- Store the incoming request from r0,
2084 # -- if it is a slow request
2085 # -- Note that r1.full = 1 implies req_op = OP_NONE
2086 # if req_op = OP_LOAD_MISS or req_op = OP_LOAD_NC
2087 # or req_op = OP_STORE_MISS
2088 # or req_op = OP_STORE_HIT then
2089 # r1.req <= req;
2090 # r1.full <= '1';
2091 # end if;
2092 # end if;
2093 #
2094 # -- Main state machine
2095 # case r1.state is
2096 # when IDLE =>
2097 # r1.wb.adr <= req.real_addr(r1.wb.adr'left downto 0);
2098 # r1.wb.sel <= req.byte_sel;
2099 # r1.wb.dat <= req.data;
2100 # r1.dcbz <= req.dcbz;
2101 #
2102 # -- Keep track of our index and way
2103 # -- for subsequent stores.
2104 # r1.store_index <= get_index(req.real_addr);
2105 # r1.store_row <= get_row(req.real_addr);
2106 # r1.end_row_ix <=
2107 # get_row_of_line(get_row(req.real_addr)) - 1;
2108 # r1.reload_tag <= get_tag(req.real_addr);
2109 # r1.req.same_tag <= '1';
2110 #
2111 # if req.op = OP_STORE_HIT then
2112 # r1.store_way <= req.hit_way;
2113 # end if;
2114 #
2115 # -- Reset per-row valid bits,
2116 # -- ready for handling OP_LOAD_MISS
2117 # for i in 0 to ROW_PER_LINE - 1 loop
2118 # r1.rows_valid(i) <= '0';
2119 # end loop;
2120 #
2121 # case req.op is
2122 # when OP_LOAD_HIT =>
2123 # -- stay in IDLE state
2124 #
2125 # when OP_LOAD_MISS =>
2126 # -- Normal load cache miss,
2127 # -- start the reload machine
2128 # report "cache miss real addr:" &
2129 # to_hstring(req.real_addr) & " idx:" &
2130 # integer'image(get_index(req.real_addr)) &
2131 # " tag:" & to_hstring(get_tag(req.real_addr));
2132 #
2133 # -- Start the wishbone cycle
2134 # r1.wb.we <= '0';
2135 # r1.wb.cyc <= '1';
2136 # r1.wb.stb <= '1';
2137 #
2138 # -- Track that we had one request sent
2139 # r1.state <= RELOAD_WAIT_ACK;
2140 # r1.write_tag <= '1';
2141 #
2142 # when OP_LOAD_NC =>
2143 # r1.wb.cyc <= '1';
2144 # r1.wb.stb <= '1';
2145 # r1.wb.we <= '0';
2146 # r1.state <= NC_LOAD_WAIT_ACK;
2147 #
2148 # when OP_STORE_HIT | OP_STORE_MISS =>
2149 # if req.dcbz = '0' then
2150 # r1.state <= STORE_WAIT_ACK;
2151 # r1.acks_pending <= to_unsigned(1, 3);
2152 # r1.full <= '0';
2153 # r1.slow_valid <= '1';
2154 # if req.mmu_req = '0' then
2155 # r1.ls_valid <= '1';
2156 # else
2157 # r1.mmu_done <= '1';
2158 # end if;
2159 # if req.op = OP_STORE_HIT then
2160 # r1.write_bram <= '1';
2161 # end if;
2162 # else
2163 # -- dcbz is handled much like a load
2164 # -- miss except that we are writing
2165 # -- to memory instead of reading
2166 # r1.state <= RELOAD_WAIT_ACK;
2167 # if req.op = OP_STORE_MISS then
2168 # r1.write_tag <= '1';
2169 # end if;
2170 # end if;
2171 # r1.wb.we <= '1';
2172 # r1.wb.cyc <= '1';
2173 # r1.wb.stb <= '1';
2174 #
2175 # -- OP_NONE and OP_BAD do nothing
2176 # -- OP_BAD & OP_STCX_FAIL were handled above already
2177 # when OP_NONE =>
2178 # when OP_BAD =>
2179 # when OP_STCX_FAIL =>
2180 # end case;
2181 #
2182 # when RELOAD_WAIT_ACK =>
2183 # -- Requests are all sent if stb is 0
2184 # stbs_done := r1.wb.stb = '0';
2185 #
2186 # -- If we are still sending requests,
2187 # -- was one accepted?
2188 # if wishbone_in.stall = '0' and not stbs_done then
2189 # -- That was the last word ? We are done sending.
2190 # -- Clear stb and set stbs_done so we can handle
2191 # -- an eventual last ack on the same cycle.
2192 # if is_last_row_addr(r1.wb.adr, r1.end_row_ix) then
2193 # r1.wb.stb <= '0';
2194 # stbs_done := true;
2195 # end if;
2196 #
2197 # -- Calculate the next row address
2198 # r1.wb.adr <= next_row_addr(r1.wb.adr);
2199 # end if;
2200 #
2201 # -- Incoming acks processing
2202 # r1.forward_valid1 <= wishbone_in.ack;
2203 # if wishbone_in.ack = '1' then
2204 # r1.rows_valid(
2205 # r1.store_row mod ROW_PER_LINE
2206 # ) <= '1';
2207 # -- If this is the data we were looking for,
2208 # -- we can complete the request next cycle.
2209 # -- Compare the whole address in case the
2210 # -- request in r1.req is not the one that
2211 # -- started this refill.
2212 # if r1.full = '1' and r1.req.same_tag = '1'
2213 # and ((r1.dcbz = '1' and r1.req.dcbz = '1')
2214 # or (r1.dcbz = '0' and r1.req.op = OP_LOAD_MISS))
2215 # and r1.store_row = get_row(r1.req.real_addr) then
2216 # r1.full <= '0';
2217 # r1.slow_valid <= '1';
2218 # if r1.mmu_req = '0' then
2219 # r1.ls_valid <= '1';
2220 # else
2221 # r1.mmu_done <= '1';
2222 # end if;
2223 # r1.forward_sel <= (others => '1');
2224 # r1.use_forward1 <= '1';
2225 # end if;
2226 #
2227 # -- Check for completion
2228 # if stbs_done and is_last_row(r1.store_row,
2229 # r1.end_row_ix) then
2230 # -- Complete wishbone cycle
2231 # r1.wb.cyc <= '0';
2232 #
2233 # -- Cache line is now valid
2234 # cache_valids(r1.store_index)(
2235 # r1.store_way
2236 # ) <= '1';
2237 #
2238 # r1.state <= IDLE;
2239 # end if;
2240 #
2241 # -- Increment store row counter
2242 # r1.store_row <= next_row(r1.store_row);
2243 # end if;
2244 #
2245 # when STORE_WAIT_ACK =>
2246 # stbs_done := r1.wb.stb = '0';
2247 # acks := r1.acks_pending;
2248 # if r1.inc_acks /= r1.dec_acks then
2249 # if r1.inc_acks = '1' then
2250 # acks := acks + 1;
2251 # else
2252 # acks := acks - 1;
2253 # end if;
2254 # end if;
2255 # r1.acks_pending <= acks;
2256 # -- Clear stb when slave accepted request
2257 # if wishbone_in.stall = '0' then
2258 # -- See if there is another store waiting
2259 # -- to be done which is in the same real page.
2260 # if req.valid = '1' then
2261 # r1.wb.adr(
2262 # SET_SIZE_BITS - 1 downto 0
2263 # ) <= req.real_addr(
2264 # SET_SIZE_BITS - 1 downto 0
2265 # );
2266 # r1.wb.dat <= req.data;
2267 # r1.wb.sel <= req.byte_sel;
2268 # end if;
2269 # if acks < 7 and req.same_tag = '1'
2270 # and (req.op = OP_STORE_MISS
2271 # or req.op = OP_STORE_HIT) then
2272 # r1.wb.stb <= '1';
2273 # stbs_done := false;
2274 # if req.op = OP_STORE_HIT then
2275 # r1.write_bram <= '1';
2276 # end if;
2277 # r1.full <= '0';
2278 # r1.slow_valid <= '1';
2279 # -- Store requests never come from the MMU
2280 # r1.ls_valid <= '1';
2281 # stbs_done := false;
2282 # r1.inc_acks <= '1';
2283 # else
2284 # r1.wb.stb <= '0';
2285 # stbs_done := true;
2286 # end if;
2287 # end if;
2288 #
2289 # -- Got ack ? See if complete.
2290 # if wishbone_in.ack = '1' then
2291 # if stbs_done and acks = 1 then
2292 # r1.state <= IDLE;
2293 # r1.wb.cyc <= '0';
2294 # r1.wb.stb <= '0';
2295 # end if;
2296 # r1.dec_acks <= '1';
2297 # end if;
2298 #
2299 # when NC_LOAD_WAIT_ACK =>
2300 # -- Clear stb when slave accepted request
2301 # if wishbone_in.stall = '0' then
2302 # r1.wb.stb <= '0';
2303 # end if;
2304 #
2305 # -- Got ack ? complete.
2306 # if wishbone_in.ack = '1' then
2307 # r1.state <= IDLE;
2308 # r1.full <= '0';
2309 # r1.slow_valid <= '1';
2310 # if r1.mmu_req = '0' then
2311 # r1.ls_valid <= '1';
2312 # else
2313 # r1.mmu_done <= '1';
2314 # end if;
2315 # r1.forward_sel <= (others => '1');
2316 # r1.use_forward1 <= '1';
2317 # r1.wb.cyc <= '0';
2318 # r1.wb.stb <= '0';
2319 # end if;
2320 # end case;
2321 # end if;
2322 # end if;
2323 # end process;
2324 #
2325 # dc_log: if LOG_LENGTH > 0 generate
2326 # signal log_data : std_ulogic_vector(19 downto 0);
2327 # begin
2328 # dcache_log: process(clk)
2329 # begin
2330 # if rising_edge(clk) then
2331 # log_data <= r1.wb.adr(5 downto 3) &
2332 # wishbone_in.stall &
2333 # wishbone_in.ack &
2334 # r1.wb.stb & r1.wb.cyc &
2335 # d_out.error &
2336 # d_out.valid &
2337 # std_ulogic_vector(
2338 # to_unsigned(op_t'pos(req_op), 3)) &
2339 # stall_out &
2340 # std_ulogic_vector(
2341 # to_unsigned(tlb_hit_way, 3)) &
2342 # valid_ra &
2343 # std_ulogic_vector(
2344 # to_unsigned(state_t'pos(r1.state), 3));
2345 # end if;
2346 # end process;
2347 # log_out <= log_data;
2348 # end generate;
2349 # end;