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