Initial commit of translation of microwatt dcache.vhdl into nmigen
[soc.git] / src / soc / experiment / dcache.py
1 # --
2 # -- Set associative dcache write-through
3 # --
4 # -- TODO (in no specific order):
5 # --
6 # -- * See list in icache.vhdl
7 # -- * Complete load misses on the cycle when WB data comes instead of
8 # -- at the end of line (this requires dealing with requests coming in
9 # -- while not idle...)
10 # --
11 # library ieee;
12 # use ieee.std_logic_1164.all;
13 # use ieee.numeric_std.all;
14 #
15 # library work;
16 # use work.utils.all;
17 # use work.common.all;
18 # use work.helpers.all;
19 # use work.wishbone_types.all;
20 #
21 # entity dcache is
22 # generic (
23 # -- Line size in bytes
24 # LINE_SIZE : positive := 64;
25 # -- Number of lines in a set
26 # NUM_LINES : positive := 32;
27 # -- Number of ways
28 # NUM_WAYS : positive := 4;
29 # -- L1 DTLB entries per set
30 # TLB_SET_SIZE : positive := 64;
31 # -- L1 DTLB number of sets
32 # TLB_NUM_WAYS : positive := 2;
33 # -- L1 DTLB log_2(page_size)
34 # TLB_LG_PGSZ : positive := 12;
35 # -- Non-zero to enable log data collection
36 # LOG_LENGTH : natural := 0
37 # );
38 # port (
39 # clk : in std_ulogic;
40 # rst : in std_ulogic;
41 #
42 # d_in : in Loadstore1ToDcacheType;
43 # d_out : out DcacheToLoadstore1Type;
44 #
45 # m_in : in MmuToDcacheType;
46 # m_out : out DcacheToMmuType;
47 #
48 # stall_out : out std_ulogic;
49 #
50 # wishbone_out : out wishbone_master_out;
51 # wishbone_in : in wishbone_slave_out;
52 #
53 # log_out : out std_ulogic_vector(19 downto 0)
54 # );
55 # end entity dcache;
56 #
57 # architecture rtl of dcache is
58 # -- BRAM organisation: We never access more than
59 # -- wishbone_data_bits at a time so to save
60 # -- resources we make the array only that wide, and
61 # -- use consecutive indices for to make a cache "line"
62 # --
63 # -- ROW_SIZE is the width in bytes of the BRAM
64 # -- (based on WB, so 64-bits)
65 # constant ROW_SIZE : natural := wishbone_data_bits / 8;
66 # -- ROW_PER_LINE is the number of row (wishbone
67 # -- transactions) in a line
68 # constant ROW_PER_LINE : natural := LINE_SIZE / ROW_SIZE;
69 # -- BRAM_ROWS is the number of rows in BRAM needed
70 # -- to represent the full dcache
71 # constant BRAM_ROWS : natural := NUM_LINES * ROW_PER_LINE;
72 #
73 # -- Bit fields counts in the address
74 #
75 # -- REAL_ADDR_BITS is the number of real address
76 # -- bits that we store
77 # constant REAL_ADDR_BITS : positive := 56;
78 # -- ROW_BITS is the number of bits to select a row
79 # constant ROW_BITS : natural := log2(BRAM_ROWS);
80 # -- ROW_LINEBITS is the number of bits to select
81 # -- a row within a line
82 # constant ROW_LINEBITS : natural := log2(ROW_PER_LINE);
83 # -- LINE_OFF_BITS is the number of bits for
84 # -- the offset in a cache line
85 # constant LINE_OFF_BITS : natural := log2(LINE_SIZE);
86 # -- ROW_OFF_BITS is the number of bits for
87 # -- the offset in a row
88 # constant ROW_OFF_BITS : natural := log2(ROW_SIZE);
89 # -- INDEX_BITS is the number if bits to
90 # -- select a cache line
91 # constant INDEX_BITS : natural := log2(NUM_LINES);
92 # -- SET_SIZE_BITS is the log base 2 of the set size
93 # constant SET_SIZE_BITS : natural := LINE_OFF_BITS
94 # + INDEX_BITS;
95 # -- TAG_BITS is the number of bits of
96 # -- the tag part of the address
97 # constant TAG_BITS : natural := REAL_ADDR_BITS - SET_SIZE_BITS;
98 # -- TAG_WIDTH is the width in bits of each way of the tag RAM
99 # constant TAG_WIDTH : natural := TAG_BITS + 7
100 # - ((TAG_BITS + 7) mod 8);
101 # -- WAY_BITS is the number of bits to select a way
102 # constant WAY_BITS : natural := log2(NUM_WAYS);
103 #
104 # -- Example of layout for 32 lines of 64 bytes:
105 # --
106 # -- .. tag |index| line |
107 # -- .. | row | |
108 # -- .. | |---| | ROW_LINEBITS (3)
109 # -- .. | |--- - --| LINE_OFF_BITS (6)
110 # -- .. | |- --| ROW_OFF_BITS (3)
111 # -- .. |----- ---| | ROW_BITS (8)
112 # -- .. |-----| | INDEX_BITS (5)
113 # -- .. --------| | TAG_BITS (45)
114 #
115 # subtype row_t is integer range 0 to BRAM_ROWS-1;
116 # subtype index_t is integer range 0 to NUM_LINES-1;
117 # subtype way_t is integer range 0 to NUM_WAYS-1;
118 # subtype row_in_line_t is unsigned(ROW_LINEBITS-1 downto 0);
119 #
120 # -- The cache data BRAM organized as described above for each way
121 # subtype cache_row_t is
122 # std_ulogic_vector(wishbone_data_bits-1 downto 0);
123 #
124 # -- The cache tags LUTRAM has a row per set.
125 # -- Vivado is a pain and will not handle a
126 # -- clean (commented) definition of the cache
127 # -- tags as a 3d memory. For now, work around
128 # -- it by putting all the tags
129 # subtype cache_tag_t is std_logic_vector(TAG_BITS-1 downto 0);
130 # -- type cache_tags_set_t is array(way_t) of cache_tag_t;
131 # -- type cache_tags_array_t is array(index_t) of cache_tags_set_t;
132 # constant TAG_RAM_WIDTH : natural := TAG_WIDTH * NUM_WAYS;
133 # subtype cache_tags_set_t is
134 # std_logic_vector(TAG_RAM_WIDTH-1 downto 0);
135 # type cache_tags_array_t is array(index_t) of cache_tags_set_t;
136 #
137 # -- The cache valid bits
138 # subtype cache_way_valids_t is
139 # std_ulogic_vector(NUM_WAYS-1 downto 0);
140 # type cache_valids_t is array(index_t) of cache_way_valids_t;
141 # type row_per_line_valid_t is
142 # array(0 to ROW_PER_LINE - 1) of std_ulogic;
143 #
144 # -- Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
145 # signal cache_tags : cache_tags_array_t;
146 # signal cache_tag_set : cache_tags_set_t;
147 # signal cache_valids : cache_valids_t;
148 #
149 # attribute ram_style : string;
150 # attribute ram_style of cache_tags : signal is "distributed";
151 #
152 # -- L1 TLB.
153 # constant TLB_SET_BITS : natural := log2(TLB_SET_SIZE);
154 # constant TLB_WAY_BITS : natural := log2(TLB_NUM_WAYS);
155 # constant TLB_EA_TAG_BITS : natural :=
156 # 64 - (TLB_LG_PGSZ + TLB_SET_BITS);
157 # constant TLB_TAG_WAY_BITS : natural :=
158 # TLB_NUM_WAYS * TLB_EA_TAG_BITS;
159 # constant TLB_PTE_BITS : natural := 64;
160 # constant TLB_PTE_WAY_BITS : natural :=
161 # TLB_NUM_WAYS * TLB_PTE_BITS;
162 #
163 # subtype tlb_way_t is integer range 0 to TLB_NUM_WAYS - 1;
164 # subtype tlb_index_t is integer range 0 to TLB_SET_SIZE - 1;
165 # subtype tlb_way_valids_t is
166 # std_ulogic_vector(TLB_NUM_WAYS-1 downto 0);
167 # type tlb_valids_t is
168 # array(tlb_index_t) of tlb_way_valids_t;
169 # subtype tlb_tag_t is
170 # std_ulogic_vector(TLB_EA_TAG_BITS - 1 downto 0);
171 # subtype tlb_way_tags_t is
172 # std_ulogic_vector(TLB_TAG_WAY_BITS-1 downto 0);
173 # type tlb_tags_t is
174 # array(tlb_index_t) of tlb_way_tags_t;
175 # subtype tlb_pte_t is
176 # std_ulogic_vector(TLB_PTE_BITS - 1 downto 0);
177 # subtype tlb_way_ptes_t is
178 # std_ulogic_vector(TLB_PTE_WAY_BITS-1 downto 0);
179 # type tlb_ptes_t is array(tlb_index_t) of tlb_way_ptes_t;
180 # type hit_way_set_t is array(tlb_way_t) of way_t;
181 #
182 # signal dtlb_valids : tlb_valids_t;
183 # signal dtlb_tags : tlb_tags_t;
184 # signal dtlb_ptes : tlb_ptes_t;
185 # attribute ram_style of dtlb_tags : signal is "distributed";
186 # attribute ram_style of dtlb_ptes : signal is "distributed";
187 #
188 # -- Record for storing permission, attribute, etc. bits from a PTE
189 # type perm_attr_t is record
190 # reference : std_ulogic;
191 # changed : std_ulogic;
192 # nocache : std_ulogic;
193 # priv : std_ulogic;
194 # rd_perm : std_ulogic;
195 # wr_perm : std_ulogic;
196 # end record;
197 #
198 # function extract_perm_attr(
199 # pte : std_ulogic_vector(TLB_PTE_BITS - 1 downto 0))
200 # return perm_attr_t is
201 # variable pa : perm_attr_t;
202 # begin
203 # pa.reference := pte(8);
204 # pa.changed := pte(7);
205 # pa.nocache := pte(5);
206 # pa.priv := pte(3);
207 # pa.rd_perm := pte(2);
208 # pa.wr_perm := pte(1);
209 # return pa;
210 # end;
211 #
212 # constant real_mode_perm_attr : perm_attr_t :=
213 # (nocache => '0', others => '1');
214 #
215 # -- Type of operation on a "valid" input
216 # type op_t is
217 # (
218 # OP_NONE,
219 # OP_BAD, -- NC cache hit, TLB miss, prot/RC failure
220 # OP_STCX_FAIL, -- conditional store w/o reservation
221 # OP_LOAD_HIT, -- Cache hit on load
222 # OP_LOAD_MISS, -- Load missing cache
223 # OP_LOAD_NC, -- Non-cachable load
224 # OP_STORE_HIT, -- Store hitting cache
225 # OP_STORE_MISS -- Store missing cache
226 # );
227 #
228 # -- Cache state machine
229 # type state_t is
230 # (
231 # IDLE, -- Normal load hit processing
232 # RELOAD_WAIT_ACK, -- Cache reload wait ack
233 # STORE_WAIT_ACK, -- Store wait ack
234 # NC_LOAD_WAIT_ACK -- Non-cachable load wait ack
235 # );
236 #
237 # --
238 # -- Dcache operations:
239 # --
240 # -- In order to make timing, we use the BRAMs with
241 # -- an output buffer, which means that the BRAM
242 # -- output is delayed by an extra cycle.
243 # --
244 # -- Thus, the dcache has a 2-stage internal pipeline
245 # -- for cache hits with no stalls.
246 # --
247 # -- All other operations are handled via stalling
248 # -- in the first stage.
249 # --
250 # -- The second stage can thus complete a hit at the same
251 # -- time as the first stage emits a stall for a complex op.
252 #
253 # -- Stage 0 register, basically contains just the latched request
254 # type reg_stage_0_t is record
255 # req : Loadstore1ToDcacheType;
256 # tlbie : std_ulogic;
257 # doall : std_ulogic;
258 # tlbld : std_ulogic;
259 # mmu_req : std_ulogic; -- indicates source of request
260 # end record;
261 #
262 # signal r0 : reg_stage_0_t;
263 # signal r0_full : std_ulogic;
264 #
265 # type mem_access_request_t is record
266 # op : op_t;
267 # valid : std_ulogic;
268 # dcbz : std_ulogic;
269 # real_addr : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
270 # data : std_ulogic_vector(63 downto 0);
271 # byte_sel : std_ulogic_vector(7 downto 0);
272 # hit_way : way_t;
273 # same_tag : std_ulogic;
274 # mmu_req : std_ulogic;
275 # end record;
276 #
277 # -- First stage register, contains state for stage 1 of load hits
278 # -- and for the state machine used by all other operations
279 # type reg_stage_1_t is record
280 # -- Info about the request
281 # full : std_ulogic; -- have uncompleted request
282 # mmu_req : std_ulogic; -- request is from MMU
283 # req : mem_access_request_t;
284 #
285 # -- Cache hit state
286 # hit_way : way_t;
287 # hit_load_valid : std_ulogic;
288 # hit_index : index_t;
289 # cache_hit : std_ulogic;
290 #
291 # -- TLB hit state
292 # tlb_hit : std_ulogic;
293 # tlb_hit_way : tlb_way_t;
294 # tlb_hit_index : tlb_index_t;
295 #
296 # -- 2-stage data buffer for data forwarded from writes to reads
297 # forward_data1 : std_ulogic_vector(63 downto 0);
298 # forward_data2 : std_ulogic_vector(63 downto 0);
299 # forward_sel1 : std_ulogic_vector(7 downto 0);
300 # forward_valid1 : std_ulogic;
301 # forward_way1 : way_t;
302 # forward_row1 : row_t;
303 # use_forward1 : std_ulogic;
304 # forward_sel : std_ulogic_vector(7 downto 0);
305 #
306 # -- Cache miss state (reload state machine)
307 # state : state_t;
308 # dcbz : std_ulogic;
309 # write_bram : std_ulogic;
310 # write_tag : std_ulogic;
311 # slow_valid : std_ulogic;
312 # wb : wishbone_master_out;
313 # reload_tag : cache_tag_t;
314 # store_way : way_t;
315 # store_row : row_t;
316 # store_index : index_t;
317 # end_row_ix : row_in_line_t;
318 # rows_valid : row_per_line_valid_t;
319 # acks_pending : unsigned(2 downto 0);
320 # inc_acks : std_ulogic;
321 # dec_acks : std_ulogic;
322 #
323 # -- Signals to complete (possibly with error)
324 # ls_valid : std_ulogic;
325 # ls_error : std_ulogic;
326 # mmu_done : std_ulogic;
327 # mmu_error : std_ulogic;
328 # cache_paradox : std_ulogic;
329 #
330 # -- Signal to complete a failed stcx.
331 # stcx_fail : std_ulogic;
332 # end record;
333 #
334 # signal r1 : reg_stage_1_t;
335 #
336 # -- Reservation information
337 # --
338 # type reservation_t is record
339 # valid : std_ulogic;
340 # addr : std_ulogic_vector(63 downto LINE_OFF_BITS);
341 # end record;
342 #
343 # signal reservation : reservation_t;
344 #
345 # -- Async signals on incoming request
346 # signal req_index : index_t;
347 # signal req_row : row_t;
348 # signal req_hit_way : way_t;
349 # signal req_tag : cache_tag_t;
350 # signal req_op : op_t;
351 # signal req_data : std_ulogic_vector(63 downto 0);
352 # signal req_same_tag : std_ulogic;
353 # signal req_go : std_ulogic;
354 #
355 # signal early_req_row : row_t;
356 #
357 # signal cancel_store : std_ulogic;
358 # signal set_rsrv : std_ulogic;
359 # signal clear_rsrv : std_ulogic;
360 #
361 # signal r0_valid : std_ulogic;
362 # signal r0_stall : std_ulogic;
363 #
364 # signal use_forward1_next : std_ulogic;
365 # signal use_forward2_next : std_ulogic;
366 #
367 # -- Cache RAM interface
368 # type cache_ram_out_t is array(way_t) of cache_row_t;
369 # signal cache_out : cache_ram_out_t;
370 #
371 # -- PLRU output interface
372 # type plru_out_t is array(index_t) of
373 # std_ulogic_vector(WAY_BITS-1 downto 0);
374 # signal plru_victim : plru_out_t;
375 # signal replace_way : way_t;
376 #
377 # -- Wishbone read/write/cache write formatting signals
378 # signal bus_sel : std_ulogic_vector(7 downto 0);
379 #
380 # -- TLB signals
381 # signal tlb_tag_way : tlb_way_tags_t;
382 # signal tlb_pte_way : tlb_way_ptes_t;
383 # signal tlb_valid_way : tlb_way_valids_t;
384 # signal tlb_req_index : tlb_index_t;
385 # signal tlb_hit : std_ulogic;
386 # signal tlb_hit_way : tlb_way_t;
387 # signal pte : tlb_pte_t;
388 # signal ra : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
389 # signal valid_ra : std_ulogic;
390 # signal perm_attr : perm_attr_t;
391 # signal rc_ok : std_ulogic;
392 # signal perm_ok : std_ulogic;
393 # signal access_ok : std_ulogic;
394 #
395 # -- TLB PLRU output interface
396 # type tlb_plru_out_t is array(tlb_index_t) of
397 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
398 # signal tlb_plru_victim : tlb_plru_out_t;
399 #
400 # --
401 # -- Helper functions to decode incoming requests
402 # --
403 #
404 # -- Return the cache line index (tag index) for an address
405 # function get_index(addr: std_ulogic_vector) return index_t is
406 # begin
407 # return to_integer(
408 # unsigned(addr(SET_SIZE_BITS - 1 downto LINE_OFF_BITS))
409 # );
410 # end;
411 #
412 # -- Return the cache row index (data memory) for an address
413 # function get_row(addr: std_ulogic_vector) return row_t is
414 # begin
415 # return to_integer(
416 # unsigned(addr(SET_SIZE_BITS - 1 downto ROW_OFF_BITS))
417 # );
418 # end;
419 #
420 # -- Return the index of a row within a line
421 # function get_row_of_line(row: row_t) return row_in_line_t is
422 # variable row_v : unsigned(ROW_BITS-1 downto 0);
423 # begin
424 # row_v := to_unsigned(row, ROW_BITS);
425 # return row_v(ROW_LINEBITS-1 downto 0);
426 # end;
427 #
428 # -- Returns whether this is the last row of a line
429 # function is_last_row_addr(addr: wishbone_addr_type;
430 # last: row_in_line_t) return boolean is
431 # begin
432 # return
433 # unsigned(addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS)) = last;
434 # end;
435 #
436 # -- Returns whether this is the last row of a line
437 # function is_last_row(row: row_t; last: row_in_line_t)
438 # return boolean is
439 # begin
440 # return get_row_of_line(row) = last;
441 # end;
442 #
443 # -- Return the address of the next row in the current cache line
444 # function next_row_addr(addr: wishbone_addr_type)
445 # return std_ulogic_vector is
446 # variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
447 # variable result : wishbone_addr_type;
448 # begin
449 # -- Is there no simpler way in VHDL to
450 # -- generate that 3 bits adder ?
451 # row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
452 # row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
453 # result := addr;
454 # result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
455 # return result;
456 # end;
457 #
458 # -- Return the next row in the current cache line. We use a
459 # -- dedicated function in order to limit the size of the
460 # -- generated adder to be only the bits within a cache line
461 # -- (3 bits with default settings)
462 # function next_row(row: row_t) return row_t is
463 # variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
464 # variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
465 # variable result : std_ulogic_vector(ROW_BITS-1 downto 0);
466 # begin
467 # row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
468 # row_idx := row_v(ROW_LINEBITS-1 downto 0);
469 # row_v(ROW_LINEBITS-1 downto 0) :=
470 # std_ulogic_vector(unsigned(row_idx) + 1);
471 # return to_integer(unsigned(row_v));
472 # end;
473 #
474 # -- Get the tag value from the address
475 # function get_tag(addr: std_ulogic_vector) return cache_tag_t is
476 # begin
477 # return addr(REAL_ADDR_BITS - 1 downto SET_SIZE_BITS);
478 # end;
479 #
480 # -- Read a tag from a tag memory row
481 # function read_tag(way: way_t; tagset: cache_tags_set_t)
482 # return cache_tag_t is
483 # begin
484 # return tagset(way * TAG_WIDTH + TAG_BITS
485 # - 1 downto way * TAG_WIDTH);
486 # end;
487 #
488 # -- Read a TLB tag from a TLB tag memory row
489 # function read_tlb_tag(way: tlb_way_t; tags: tlb_way_tags_t)
490 # return tlb_tag_t is
491 # variable j : integer;
492 # begin
493 # j := way * TLB_EA_TAG_BITS;
494 # return tags(j + TLB_EA_TAG_BITS - 1 downto j);
495 # end;
496 #
497 # -- Write a TLB tag to a TLB tag memory row
498 # procedure write_tlb_tag(way: tlb_way_t; tags: inout tlb_way_tags_t;
499 # tag: tlb_tag_t) is
500 # variable j : integer;
501 # begin
502 # j := way * TLB_EA_TAG_BITS;
503 # tags(j + TLB_EA_TAG_BITS - 1 downto j) := tag;
504 # end;
505 #
506 # -- Read a PTE from a TLB PTE memory row
507 # function read_tlb_pte(way: tlb_way_t; ptes: tlb_way_ptes_t)
508 # return tlb_pte_t is
509 # variable j : integer;
510 # begin
511 # j := way * TLB_PTE_BITS;
512 # return ptes(j + TLB_PTE_BITS - 1 downto j);
513 # end;
514 #
515 # procedure write_tlb_pte(way: tlb_way_t;
516 # ptes: inout tlb_way_ptes_t; newpte: tlb_pte_t) is
517 # variable j : integer;
518 # begin
519 # j := way * TLB_PTE_BITS;
520 # ptes(j + TLB_PTE_BITS - 1 downto j) := newpte;
521 # end;
522 #
523 # begin
524 #
525 # assert LINE_SIZE mod ROW_SIZE = 0
526 # report "LINE_SIZE not multiple of ROW_SIZE" severity FAILURE;
527 # assert ispow2(LINE_SIZE)
528 # report "LINE_SIZE not power of 2" severity FAILURE;
529 # assert ispow2(NUM_LINES)
530 # report "NUM_LINES not power of 2" severity FAILURE;
531 # assert ispow2(ROW_PER_LINE)
532 # report "ROW_PER_LINE not power of 2" severity FAILURE;
533 # assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
534 # report "geometry bits don't add up" severity FAILURE;
535 # assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
536 # report "geometry bits don't add up" severity FAILURE;
537 # assert (REAL_ADDR_BITS = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
538 # report "geometry bits don't add up" severity FAILURE;
539 # assert (REAL_ADDR_BITS = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
540 # report "geometry bits don't add up" severity FAILURE;
541 # assert (64 = wishbone_data_bits)
542 # report "Can't yet handle a wishbone width that isn't 64-bits"
543 # severity FAILURE;
544 # assert SET_SIZE_BITS <= TLB_LG_PGSZ
545 # report "Set indexed by virtual address" severity FAILURE;
546 #
547 # -- Latch the request in r0.req as long as we're not stalling
548 # stage_0 : process(clk)
549 # variable r : reg_stage_0_t;
550 # begin
551 # if rising_edge(clk) then
552 # assert (d_in.valid and m_in.valid) = '0'
553 # report "request collision loadstore vs MMU";
554 # if m_in.valid = '1' then
555 # r.req.valid := '1';
556 # r.req.load := not (m_in.tlbie or m_in.tlbld);
557 # r.req.dcbz := '0';
558 # r.req.nc := '0';
559 # r.req.reserve := '0';
560 # r.req.virt_mode := '0';
561 # r.req.priv_mode := '1';
562 # r.req.addr := m_in.addr;
563 # r.req.data := m_in.pte;
564 # r.req.byte_sel := (others => '1');
565 # r.tlbie := m_in.tlbie;
566 # r.doall := m_in.doall;
567 # r.tlbld := m_in.tlbld;
568 # r.mmu_req := '1';
569 # else
570 # r.req := d_in;
571 # r.tlbie := '0';
572 # r.doall := '0';
573 # r.tlbld := '0';
574 # r.mmu_req := '0';
575 # end if;
576 # if rst = '1' then
577 # r0_full <= '0';
578 # elsif r1.full = '0' or r0_full = '0' then
579 # r0 <= r;
580 # r0_full <= r.req.valid;
581 # end if;
582 # end if;
583 # end process;
584 #
585 # -- we don't yet handle collisions between loadstore1 requests
586 # -- and MMU requests
587 # m_out.stall <= '0';
588 #
589 # -- Hold off the request in r0 when r1 has an uncompleted request
590 # r0_stall <= r0_full and r1.full;
591 # r0_valid <= r0_full and not r1.full;
592 # stall_out <= r0_stall;
593 #
594 # -- TLB
595 # -- Operates in the second cycle on the request latched in r0.req.
596 # -- TLB updates write the entry at the end of the second cycle.
597 # tlb_read : process(clk)
598 # variable index : tlb_index_t;
599 # variable addrbits :
600 # std_ulogic_vector(TLB_SET_BITS - 1 downto 0);
601 # begin
602 # if rising_edge(clk) then
603 # if m_in.valid = '1' then
604 # addrbits := m_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
605 # - 1 downto TLB_LG_PGSZ);
606 # else
607 # addrbits := d_in.addr(TLB_LG_PGSZ + TLB_SET_BITS
608 # - 1 downto TLB_LG_PGSZ);
609 # end if;
610 # index := to_integer(unsigned(addrbits));
611 # -- If we have any op and the previous op isn't finished,
612 # -- then keep the same output for next cycle.
613 # if r0_stall = '0' then
614 # tlb_valid_way <= dtlb_valids(index);
615 # tlb_tag_way <= dtlb_tags(index);
616 # tlb_pte_way <= dtlb_ptes(index);
617 # end if;
618 # end if;
619 # end process;
620 #
621 # -- Generate TLB PLRUs
622 # maybe_tlb_plrus: if TLB_NUM_WAYS > 1 generate
623 # begin
624 # tlb_plrus: for i in 0 to TLB_SET_SIZE - 1 generate
625 # -- TLB PLRU interface
626 # signal tlb_plru_acc :
627 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
628 # signal tlb_plru_acc_en : std_ulogic;
629 # signal tlb_plru_out :
630 # std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
631 # begin
632 # tlb_plru : entity work.plru
633 # generic map (
634 # BITS => TLB_WAY_BITS
635 # )
636 # port map (
637 # clk => clk,
638 # rst => rst,
639 # acc => tlb_plru_acc,
640 # acc_en => tlb_plru_acc_en,
641 # lru => tlb_plru_out
642 # );
643 #
644 # process(all)
645 # begin
646 # -- PLRU interface
647 # if r1.tlb_hit_index = i then
648 # tlb_plru_acc_en <= r1.tlb_hit;
649 # else
650 # tlb_plru_acc_en <= '0';
651 # end if;
652 # tlb_plru_acc <=
653 # std_ulogic_vector(to_unsigned(
654 # r1.tlb_hit_way, TLB_WAY_BITS
655 # ));
656 # tlb_plru_victim(i) <= tlb_plru_out;
657 # end process;
658 # end generate;
659 # end generate;
660 #
661 # tlb_search : process(all)
662 # variable hitway : tlb_way_t;
663 # variable hit : std_ulogic;
664 # variable eatag : tlb_tag_t;
665 # begin
666 # tlb_req_index <=
667 # to_integer(unsigned(r0.req.addr(
668 # TLB_LG_PGSZ + TLB_SET_BITS - 1 downto TLB_LG_PGSZ
669 # )));
670 # hitway := 0;
671 # hit := '0';
672 # eatag := r0.req.addr(63 downto TLB_LG_PGSZ + TLB_SET_BITS);
673 # for i in tlb_way_t loop
674 # if tlb_valid_way(i) = '1' and
675 # read_tlb_tag(i, tlb_tag_way) = eatag then
676 # hitway := i;
677 # hit := '1';
678 # end if;
679 # end loop;
680 # tlb_hit <= hit and r0_valid;
681 # tlb_hit_way <= hitway;
682 # if tlb_hit = '1' then
683 # pte <= read_tlb_pte(hitway, tlb_pte_way);
684 # else
685 # pte <= (others => '0');
686 # end if;
687 # valid_ra <= tlb_hit or not r0.req.virt_mode;
688 # if r0.req.virt_mode = '1' then
689 # ra <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
690 # r0.req.addr(TLB_LG_PGSZ - 1 downto ROW_OFF_BITS) &
691 # (ROW_OFF_BITS-1 downto 0 => '0');
692 # perm_attr <= extract_perm_attr(pte);
693 # else
694 # ra <= r0.req.addr(
695 # REAL_ADDR_BITS - 1 downto ROW_OFF_BITS
696 # ) & (ROW_OFF_BITS-1 downto 0 => '0');
697 # perm_attr <= real_mode_perm_attr;
698 # end if;
699 # end process;
700 #
701 # tlb_update : process(clk)
702 # variable tlbie : std_ulogic;
703 # variable tlbwe : std_ulogic;
704 # variable repl_way : tlb_way_t;
705 # variable eatag : tlb_tag_t;
706 # variable tagset : tlb_way_tags_t;
707 # variable pteset : tlb_way_ptes_t;
708 # begin
709 # if rising_edge(clk) then
710 # tlbie := r0_valid and r0.tlbie;
711 # tlbwe := r0_valid and r0.tlbld;
712 # if rst = '1' or (tlbie = '1' and r0.doall = '1') then
713 # -- clear all valid bits at once
714 # for i in tlb_index_t loop
715 # dtlb_valids(i) <= (others => '0');
716 # end loop;
717 # elsif tlbie = '1' then
718 # if tlb_hit = '1' then
719 # dtlb_valids(tlb_req_index)(tlb_hit_way) <= '0';
720 # end if;
721 # elsif tlbwe = '1' then
722 # if tlb_hit = '1' then
723 # repl_way := tlb_hit_way;
724 # else
725 # repl_way := to_integer(unsigned(
726 # tlb_plru_victim(tlb_req_index)));
727 # end if;
728 # eatag := r0.req.addr(
729 # 63 downto TLB_LG_PGSZ + TLB_SET_BITS
730 # );
731 # tagset := tlb_tag_way;
732 # write_tlb_tag(repl_way, tagset, eatag);
733 # dtlb_tags(tlb_req_index) <= tagset;
734 # pteset := tlb_pte_way;
735 # write_tlb_pte(repl_way, pteset, r0.req.data);
736 # dtlb_ptes(tlb_req_index) <= pteset;
737 # dtlb_valids(tlb_req_index)(repl_way) <= '1';
738 # end if;
739 # end if;
740 # end process;
741 #
742 # -- Generate PLRUs
743 # maybe_plrus: if NUM_WAYS > 1 generate
744 # begin
745 # plrus: for i in 0 to NUM_LINES-1 generate
746 # -- PLRU interface
747 # signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
748 # signal plru_acc_en : std_ulogic;
749 # signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
750 #
751 # begin
752 # plru : entity work.plru
753 # generic map (
754 # BITS => WAY_BITS
755 # )
756 # port map (
757 # clk => clk,
758 # rst => rst,
759 # acc => plru_acc,
760 # acc_en => plru_acc_en,
761 # lru => plru_out
762 # );
763 #
764 # process(all)
765 # begin
766 # -- PLRU interface
767 # if r1.hit_index = i then
768 # plru_acc_en <= r1.cache_hit;
769 # else
770 # plru_acc_en <= '0';
771 # end if;
772 # plru_acc <= std_ulogic_vector(to_unsigned(
773 # r1.hit_way, WAY_BITS
774 # ));
775 # plru_victim(i) <= plru_out;
776 # end process;
777 # end generate;
778 # end generate;
779 #
780 # -- Cache tag RAM read port
781 # cache_tag_read : process(clk)
782 # variable index : index_t;
783 # begin
784 # if rising_edge(clk) then
785 # if r0_stall = '1' then
786 # index := req_index;
787 # elsif m_in.valid = '1' then
788 # index := get_index(m_in.addr);
789 # else
790 # index := get_index(d_in.addr);
791 # end if;
792 # cache_tag_set <= cache_tags(index);
793 # end if;
794 # end process;
795 #
796 # -- Cache request parsing and hit detection
797 # dcache_request : process(all)
798 # variable is_hit : std_ulogic;
799 # variable hit_way : way_t;
800 # variable op : op_t;
801 # variable opsel : std_ulogic_vector(2 downto 0);
802 # variable go : std_ulogic;
803 # variable nc : std_ulogic;
804 # variable s_hit : std_ulogic;
805 # variable s_tag : cache_tag_t;
806 # variable s_pte : tlb_pte_t;
807 # variable s_ra : std_ulogic_vector(
808 # REAL_ADDR_BITS - 1 downto 0
809 # );
810 # variable hit_set : std_ulogic_vector(
811 # TLB_NUM_WAYS - 1 downto 0
812 # );
813 # variable hit_way_set : hit_way_set_t;
814 # variable rel_matches : std_ulogic_vector(
815 # TLB_NUM_WAYS - 1 downto 0
816 # );
817 # variable rel_match : std_ulogic;
818 # begin
819 # -- Extract line, row and tag from request
820 # req_index <= get_index(r0.req.addr);
821 # req_row <= get_row(r0.req.addr);
822 # req_tag <= get_tag(ra);
823 #
824 # go := r0_valid and not (r0.tlbie or r0.tlbld)
825 # and not r1.ls_error;
826 #
827 # -- Test if pending request is a hit on any way
828 # -- In order to make timing in virtual mode,
829 # -- when we are using the TLB, we compare each
830 # --way with each of the real addresses from each way of
831 # -- the TLB, and then decide later which match to use.
832 # hit_way := 0;
833 # is_hit := '0';
834 # rel_match := '0';
835 # if r0.req.virt_mode = '1' then
836 # rel_matches := (others => '0');
837 # for j in tlb_way_t loop
838 # hit_way_set(j) := 0;
839 # s_hit := '0';
840 # s_pte := read_tlb_pte(j, tlb_pte_way);
841 # s_ra :=
842 # s_pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ)
843 # & r0.req.addr(TLB_LG_PGSZ - 1 downto 0);
844 # s_tag := get_tag(s_ra);
845 # for i in way_t loop
846 # if go = '1' and cache_valids(req_index)(i) = '1'
847 # and read_tag(i, cache_tag_set) = s_tag
848 # and tlb_valid_way(j) = '1' then
849 # hit_way_set(j) := i;
850 # s_hit := '1';
851 # end if;
852 # end loop;
853 # hit_set(j) := s_hit;
854 # if s_tag = r1.reload_tag then
855 # rel_matches(j) := '1';
856 # end if;
857 # end loop;
858 # if tlb_hit = '1' then
859 # is_hit := hit_set(tlb_hit_way);
860 # hit_way := hit_way_set(tlb_hit_way);
861 # rel_match := rel_matches(tlb_hit_way);
862 # end if;
863 # else
864 # s_tag := get_tag(r0.req.addr);
865 # for i in way_t loop
866 # if go = '1' and cache_valids(req_index)(i) = '1' and
867 # read_tag(i, cache_tag_set) = s_tag then
868 # hit_way := i;
869 # is_hit := '1';
870 # end if;
871 # end loop;
872 # if s_tag = r1.reload_tag then
873 # rel_match := '1';
874 # end if;
875 # end if;
876 # req_same_tag <= rel_match;
877 #
878 # -- See if the request matches the line currently being reloaded
879 # if r1.state = RELOAD_WAIT_ACK and req_index = r1.store_index
880 # and rel_match = '1' then
881 # -- For a store, consider this a hit even if the row isn't
882 # -- valid since it will be by the time we perform the store.
883 # -- For a load, check the appropriate row valid bit.
884 # is_hit :=
885 # not r0.req.load or r1.rows_valid(req_row mod ROW_PER_LINE);
886 # hit_way := replace_way;
887 # end if;
888 #
889 # -- Whether to use forwarded data for a load or not
890 # use_forward1_next <= '0';
891 # if get_row(r1.req.real_addr) = req_row
892 # and r1.req.hit_way = hit_way then
893 # -- Only need to consider r1.write_bram here, since if we
894 # -- are writing refill data here, then we don't have a
895 # -- cache hit this cycle on the line being refilled.
896 # -- (There is the possibility that the load following the
897 # -- load miss that started the refill could be to the old
898 # -- contents of the victim line, since it is a couple of
899 # -- cycles after the refill starts before we see the updated
900 # -- cache tag. In that case we don't use the bypass.)
901 # use_forward1_next <= r1.write_bram;
902 # end if;
903 # use_forward2_next <= '0';
904 # if r1.forward_row1 = req_row and r1.forward_way1 = hit_way then
905 # use_forward2_next <= r1.forward_valid1;
906 # end if;
907 #
908 # -- The way that matched on a hit
909 # req_hit_way <= hit_way;
910 #
911 # -- The way to replace on a miss
912 # if r1.write_tag = '1' then
913 # replace_way <= to_integer(unsigned(
914 # plru_victim(r1.store_index)
915 # ));
916 # else
917 # replace_way <= r1.store_way;
918 # end if;
919 #
920 # -- work out whether we have permission for this access
921 # -- NB we don't yet implement AMR, thus no KUAP
922 # rc_ok <= perm_attr.reference and
923 # (r0.req.load or perm_attr.changed);
924 # perm_ok <= (r0.req.priv_mode or not perm_attr.priv) and
925 # (perm_attr.wr_perm or (r0.req.load
926 # and perm_attr.rd_perm));
927 # access_ok <= valid_ra and perm_ok and rc_ok;
928 #
929 # -- Combine the request and cache hit status to decide what
930 # -- operation needs to be done
931 # --
932 # nc := r0.req.nc or perm_attr.nocache;
933 # op := OP_NONE;
934 # if go = '1' then
935 # if access_ok = '0' then
936 # op := OP_BAD;
937 # elsif cancel_store = '1' then
938 # op := OP_STCX_FAIL;
939 # else
940 # opsel := r0.req.load & nc & is_hit;
941 # case opsel is
942 # when "101" => op := OP_LOAD_HIT;
943 # when "100" => op := OP_LOAD_MISS;
944 # when "110" => op := OP_LOAD_NC;
945 # when "001" => op := OP_STORE_HIT;
946 # when "000" => op := OP_STORE_MISS;
947 # when "010" => op := OP_STORE_MISS;
948 # when "011" => op := OP_BAD;
949 # when "111" => op := OP_BAD;
950 # when others => op := OP_NONE;
951 # end case;
952 # end if;
953 # end if;
954 # req_op <= op;
955 # req_go <= go;
956 #
957 # -- Version of the row number that is valid one cycle earlier
958 # -- in the cases where we need to read the cache data BRAM.
959 # -- If we're stalling then we need to keep reading the last
960 # -- row requested.
961 # if r0_stall = '0' then
962 # if m_in.valid = '1' then
963 # early_req_row <= get_row(m_in.addr);
964 # else
965 # early_req_row <= get_row(d_in.addr);
966 # end if;
967 # else
968 # early_req_row <= req_row;
969 # end if;
970 # end process;
971 #
972 # -- Wire up wishbone request latch out of stage 1
973 # wishbone_out <= r1.wb;
974 #
975 # -- Handle load-with-reservation and store-conditional instructions
976 # reservation_comb: process(all)
977 # begin
978 # cancel_store <= '0';
979 # set_rsrv <= '0';
980 # clear_rsrv <= '0';
981 # if r0_valid = '1' and r0.req.reserve = '1' then
982 # -- XXX generate alignment interrupt if address
983 # -- is not aligned XXX or if r0.req.nc = '1'
984 # if r0.req.load = '1' then
985 # -- load with reservation
986 # set_rsrv <= '1';
987 # else
988 # -- store conditional
989 # clear_rsrv <= '1';
990 # if reservation.valid = '0' or r0.req.addr(63
991 # downto LINE_OFF_BITS) /= reservation.addr then
992 # cancel_store <= '1';
993 # end if;
994 # end if;
995 # end if;
996 # end process;
997 #
998 # reservation_reg: process(clk)
999 # begin
1000 # if rising_edge(clk) then
1001 # if rst = '1' then
1002 # reservation.valid <= '0';
1003 # elsif r0_valid = '1' and access_ok = '1' then
1004 # if clear_rsrv = '1' then
1005 # reservation.valid <= '0';
1006 # elsif set_rsrv = '1' then
1007 # reservation.valid <= '1';
1008 # reservation.addr <=
1009 # r0.req.addr(63 downto LINE_OFF_BITS);
1010 # end if;
1011 # end if;
1012 # end if;
1013 # end process;
1014 #
1015 # -- Return data for loads & completion control logic
1016 # --
1017 # writeback_control: process(all)
1018 # variable data_out : std_ulogic_vector(63 downto 0);
1019 # variable data_fwd : std_ulogic_vector(63 downto 0);
1020 # variable j : integer;
1021 # begin
1022 # -- Use the bypass if are reading the row that was
1023 # -- written 1 or 2 cycles ago, including for the
1024 # -- slow_valid = 1 case (i.e. completing a load
1025 # -- miss or a non-cacheable load).
1026 # if r1.use_forward1 = '1' then
1027 # data_fwd := r1.forward_data1;
1028 # else
1029 # data_fwd := r1.forward_data2;
1030 # end if;
1031 # data_out := cache_out(r1.hit_way);
1032 # for i in 0 to 7 loop
1033 # j := i * 8;
1034 # if r1.forward_sel(i) = '1' then
1035 # data_out(j + 7 downto j) := data_fwd(j + 7 downto j);
1036 # end if;
1037 # end loop;
1038 #
1039 # d_out.valid <= r1.ls_valid;
1040 # d_out.data <= data_out;
1041 # d_out.store_done <= not r1.stcx_fail;
1042 # d_out.error <= r1.ls_error;
1043 # d_out.cache_paradox <= r1.cache_paradox;
1044 #
1045 # -- Outputs to MMU
1046 # m_out.done <= r1.mmu_done;
1047 # m_out.err <= r1.mmu_error;
1048 # m_out.data <= data_out;
1049 #
1050 # -- We have a valid load or store hit or we just completed
1051 # -- a slow op such as a load miss, a NC load or a store
1052 # --
1053 # -- Note: the load hit is delayed by one cycle. However it
1054 # -- can still not collide with r.slow_valid (well unless I
1055 # -- miscalculated) because slow_valid can only be set on a
1056 # -- subsequent request and not on its first cycle (the state
1057 # -- machine must have advanced), which makes slow_valid
1058 # -- at least 2 cycles from the previous hit_load_valid.
1059 #
1060 # -- Sanity: Only one of these must be set in any given cycle
1061 # assert (r1.slow_valid and r1.stcx_fail) /= '1'
1062 # report "unexpected slow_valid collision with stcx_fail"
1063 # severity FAILURE;
1064 # assert ((r1.slow_valid or r1.stcx_fail) and r1.hit_load_valid)
1065 # /= '1' report "unexpected hit_load_delayed collision with
1066 # slow_valid" severity FAILURE;
1067 #
1068 # if r1.mmu_req = '0' then
1069 # -- Request came from loadstore1...
1070 # -- Load hit case is the standard path
1071 # if r1.hit_load_valid = '1' then
1072 # report
1073 # "completing load hit data=" & to_hstring(data_out);
1074 # end if;
1075 #
1076 # -- error cases complete without stalling
1077 # if r1.ls_error = '1' then
1078 # report "completing ld/st with error";
1079 # end if;
1080 #
1081 # -- Slow ops (load miss, NC, stores)
1082 # if r1.slow_valid = '1' then
1083 # report
1084 # "completing store or load miss data="
1085 # & to_hstring(data_out);
1086 # end if;
1087 #
1088 # else
1089 # -- Request came from MMU
1090 # if r1.hit_load_valid = '1' then
1091 # report "completing load hit to MMU, data="
1092 # & to_hstring(m_out.data);
1093 # end if;
1094 #
1095 # -- error cases complete without stalling
1096 # if r1.mmu_error = '1' then
1097 # report "completing MMU ld with error";
1098 # end if;
1099 #
1100 # -- Slow ops (i.e. load miss)
1101 # if r1.slow_valid = '1' then
1102 # report "completing MMU load miss, data="
1103 # & to_hstring(m_out.data);
1104 # end if;
1105 # end if;
1106 #
1107 # end process;
1108 #
1109 #
1110 # -- Generate a cache RAM for each way. This handles the normal
1111 # -- reads, writes from reloads and the special store-hit update
1112 # -- path as well.
1113 # --
1114 # -- Note: the BRAMs have an extra read buffer, meaning the output
1115 # -- is pipelined an extra cycle. This differs from the
1116 # -- icache. The writeback logic needs to take that into
1117 # -- account by using 1-cycle delayed signals for load hits.
1118 # --
1119 # rams: for i in 0 to NUM_WAYS-1 generate
1120 # signal do_read : std_ulogic;
1121 # signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1122 # signal do_write : std_ulogic;
1123 # signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1124 # signal wr_data :
1125 # std_ulogic_vector(wishbone_data_bits-1 downto 0);
1126 # signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
1127 # signal wr_sel_m : std_ulogic_vector(ROW_SIZE-1 downto 0);
1128 # signal dout : cache_row_t;
1129 # begin
1130 # way: entity work.cache_ram
1131 # generic map (
1132 # ROW_BITS => ROW_BITS,
1133 # WIDTH => wishbone_data_bits,
1134 # ADD_BUF => true
1135 # )
1136 # port map (
1137 # clk => clk,
1138 # rd_en => do_read,
1139 # rd_addr => rd_addr,
1140 # rd_data => dout,
1141 # wr_sel => wr_sel_m,
1142 # wr_addr => wr_addr,
1143 # wr_data => wr_data
1144 # );
1145 # process(all)
1146 # begin
1147 # -- Cache hit reads
1148 # do_read <= '1';
1149 # rd_addr <=
1150 # std_ulogic_vector(to_unsigned(early_req_row, ROW_BITS));
1151 # cache_out(i) <= dout;
1152 #
1153 # -- Write mux:
1154 # --
1155 # -- Defaults to wishbone read responses (cache refill)
1156 # --
1157 # -- For timing, the mux on wr_data/sel/addr is not
1158 # -- dependent on anything other than the current state.
1159 # wr_sel_m <= (others => '0');
1160 #
1161 # do_write <= '0';
1162 # if r1.write_bram = '1' then
1163 # -- Write store data to BRAM. This happens one
1164 # -- cycle after the store is in r0.
1165 # wr_data <= r1.req.data;
1166 # wr_sel <= r1.req.byte_sel;
1167 # wr_addr <= std_ulogic_vector(to_unsigned(
1168 # get_row(r1.req.real_addr), ROW_BITS
1169 # ));
1170 # if i = r1.req.hit_way then
1171 # do_write <= '1';
1172 # end if;
1173 # else
1174 # -- Otherwise, we might be doing a reload or a DCBZ
1175 # if r1.dcbz = '1' then
1176 # wr_data <= (others => '0');
1177 # else
1178 # wr_data <= wishbone_in.dat;
1179 # end if;
1180 # wr_addr <= std_ulogic_vector(to_unsigned(
1181 # r1.store_row, ROW_BITS
1182 # ));
1183 # wr_sel <= (others => '1');
1184 #
1185 # if r1.state = RELOAD_WAIT_ACK and
1186 # wishbone_in.ack = '1' and replace_way = i then
1187 # do_write <= '1';
1188 # end if;
1189 # end if;
1190 #
1191 # -- Mask write selects with do_write since BRAM
1192 # -- doesn't have a global write-enable
1193 # if do_write = '1' then
1194 # wr_sel_m <= wr_sel;
1195 # end if;
1196 #
1197 # end process;
1198 # end generate;
1199 #
1200 # -- Cache hit synchronous machine for the easy case.
1201 # -- This handles load hits.
1202 # -- It also handles error cases (TLB miss, cache paradox)
1203 # dcache_fast_hit : process(clk)
1204 # begin
1205 # if rising_edge(clk) then
1206 # if req_op /= OP_NONE then
1207 # report "op:" & op_t'image(req_op) &
1208 # " addr:" & to_hstring(r0.req.addr) &
1209 # " nc:" & std_ulogic'image(r0.req.nc) &
1210 # " idx:" & integer'image(req_index) &
1211 # " tag:" & to_hstring(req_tag) &
1212 # " way: " & integer'image(req_hit_way);
1213 # end if;
1214 # if r0_valid = '1' then
1215 # r1.mmu_req <= r0.mmu_req;
1216 # end if;
1217 #
1218 # -- Fast path for load/store hits.
1219 # -- Set signals for the writeback controls.
1220 # r1.hit_way <= req_hit_way;
1221 # r1.hit_index <= req_index;
1222 # if req_op = OP_LOAD_HIT then
1223 # r1.hit_load_valid <= '1';
1224 # else
1225 # r1.hit_load_valid <= '0';
1226 # end if;
1227 # if req_op = OP_LOAD_HIT or req_op = OP_STORE_HIT then
1228 # r1.cache_hit <= '1';
1229 # else
1230 # r1.cache_hit <= '0';
1231 # end if;
1232 #
1233 # if req_op = OP_BAD then
1234 # report "Signalling ld/st error valid_ra=" &
1235 # std_ulogic'image(valid_ra) & " rc_ok=" &
1236 # std_ulogic'image(rc_ok) & " perm_ok=" &
1237 # std_ulogic'image(perm_ok);
1238 # r1.ls_error <= not r0.mmu_req;
1239 # r1.mmu_error <= r0.mmu_req;
1240 # r1.cache_paradox <= access_ok;
1241 # else
1242 # r1.ls_error <= '0';
1243 # r1.mmu_error <= '0';
1244 # r1.cache_paradox <= '0';
1245 # end if;
1246 #
1247 # if req_op = OP_STCX_FAIL then
1248 # r1.stcx_fail <= '1';
1249 # else
1250 # r1.stcx_fail <= '0';
1251 # end if;
1252 #
1253 # -- Record TLB hit information for updating TLB PLRU
1254 # r1.tlb_hit <= tlb_hit;
1255 # r1.tlb_hit_way <= tlb_hit_way;
1256 # r1.tlb_hit_index <= tlb_req_index;
1257 #
1258 # end if;
1259 # end process;
1260 #
1261 # -- Memory accesses are handled by this state machine:
1262 # --
1263 # -- * Cache load miss/reload (in conjunction with "rams")
1264 # -- * Load hits for non-cachable forms
1265 # -- * Stores (the collision case is handled in "rams")
1266 # --
1267 # -- All wishbone requests generation is done here.
1268 # -- This machine operates at stage 1.
1269 # dcache_slow : process(clk)
1270 # variable stbs_done : boolean;
1271 # variable req : mem_access_request_t;
1272 # variable acks : unsigned(2 downto 0);
1273 # begin
1274 # if rising_edge(clk) then
1275 # r1.use_forward1 <= use_forward1_next;
1276 # r1.forward_sel <= (others => '0');
1277 # if use_forward1_next = '1' then
1278 # r1.forward_sel <= r1.req.byte_sel;
1279 # elsif use_forward2_next = '1' then
1280 # r1.forward_sel <= r1.forward_sel1;
1281 # end if;
1282 #
1283 # r1.forward_data2 <= r1.forward_data1;
1284 # if r1.write_bram = '1' then
1285 # r1.forward_data1 <= r1.req.data;
1286 # r1.forward_sel1 <= r1.req.byte_sel;
1287 # r1.forward_way1 <= r1.req.hit_way;
1288 # r1.forward_row1 <= get_row(r1.req.real_addr);
1289 # r1.forward_valid1 <= '1';
1290 # else
1291 # if r1.dcbz = '1' then
1292 # r1.forward_data1 <= (others => '0');
1293 # else
1294 # r1.forward_data1 <= wishbone_in.dat;
1295 # end if;
1296 # r1.forward_sel1 <= (others => '1');
1297 # r1.forward_way1 <= replace_way;
1298 # r1.forward_row1 <= r1.store_row;
1299 # r1.forward_valid1 <= '0';
1300 # end if;
1301 #
1302 # -- On reset, clear all valid bits to force misses
1303 # if rst = '1' then
1304 # for i in index_t loop
1305 # cache_valids(i) <= (others => '0');
1306 # end loop;
1307 # r1.state <= IDLE;
1308 # r1.full <= '0';
1309 # r1.slow_valid <= '0';
1310 # r1.wb.cyc <= '0';
1311 # r1.wb.stb <= '0';
1312 # r1.ls_valid <= '0';
1313 # r1.mmu_done <= '0';
1314 #
1315 # -- Not useful normally but helps avoiding
1316 # -- tons of sim warnings
1317 # r1.wb.adr <= (others => '0');
1318 # else
1319 # -- One cycle pulses reset
1320 # r1.slow_valid <= '0';
1321 # r1.write_bram <= '0';
1322 # r1.inc_acks <= '0';
1323 # r1.dec_acks <= '0';
1324 #
1325 # r1.ls_valid <= '0';
1326 # -- complete tlbies and TLB loads in the third cycle
1327 # r1.mmu_done <= r0_valid and (r0.tlbie or r0.tlbld);
1328 # if req_op = OP_LOAD_HIT or req_op = OP_STCX_FAIL then
1329 # if r0.mmu_req = '0' then
1330 # r1.ls_valid <= '1';
1331 # else
1332 # r1.mmu_done <= '1';
1333 # end if;
1334 # end if;
1335 #
1336 # if r1.write_tag = '1' then
1337 # -- Store new tag in selected way
1338 # for i in 0 to NUM_WAYS-1 loop
1339 # if i = replace_way then
1340 # cache_tags(r1.store_index)(
1341 # (i + 1) * TAG_WIDTH - 1
1342 # downto i * TAG_WIDTH
1343 # ) <=
1344 # (TAG_WIDTH - 1 downto TAG_BITS => '0')
1345 # & r1.reload_tag;
1346 # end if;
1347 # end loop;
1348 # r1.store_way <= replace_way;
1349 # r1.write_tag <= '0';
1350 # end if;
1351 #
1352 # -- Take request from r1.req if there is one there,
1353 # -- else from req_op, ra, etc.
1354 # if r1.full = '1' then
1355 # req := r1.req;
1356 # else
1357 # req.op := req_op;
1358 # req.valid := req_go;
1359 # req.mmu_req := r0.mmu_req;
1360 # req.dcbz := r0.req.dcbz;
1361 # req.real_addr := ra;
1362 # -- Force data to 0 for dcbz
1363 # if r0.req.dcbz = '0' then
1364 # req.data := r0.req.data;
1365 # else
1366 # req.data := (others => '0');
1367 # end if;
1368 # -- Select all bytes for dcbz
1369 # -- and for cacheable loads
1370 # if r0.req.dcbz = '1'
1371 # or (r0.req.load = '1' and r0.req.nc = '0') then
1372 # req.byte_sel := (others => '1');
1373 # else
1374 # req.byte_sel := r0.req.byte_sel;
1375 # end if;
1376 # req.hit_way := req_hit_way;
1377 # req.same_tag := req_same_tag;
1378 #
1379 # -- Store the incoming request from r0,
1380 # -- if it is a slow request
1381 # -- Note that r1.full = 1 implies req_op = OP_NONE
1382 # if req_op = OP_LOAD_MISS or req_op = OP_LOAD_NC
1383 # or req_op = OP_STORE_MISS
1384 # or req_op = OP_STORE_HIT then
1385 # r1.req <= req;
1386 # r1.full <= '1';
1387 # end if;
1388 # end if;
1389 #
1390 # -- Main state machine
1391 # case r1.state is
1392 # when IDLE =>
1393 # r1.wb.adr <= req.real_addr(r1.wb.adr'left downto 0);
1394 # r1.wb.sel <= req.byte_sel;
1395 # r1.wb.dat <= req.data;
1396 # r1.dcbz <= req.dcbz;
1397 #
1398 # -- Keep track of our index and way
1399 # -- for subsequent stores.
1400 # r1.store_index <= get_index(req.real_addr);
1401 # r1.store_row <= get_row(req.real_addr);
1402 # r1.end_row_ix <=
1403 # get_row_of_line(get_row(req.real_addr)) - 1;
1404 # r1.reload_tag <= get_tag(req.real_addr);
1405 # r1.req.same_tag <= '1';
1406 #
1407 # if req.op = OP_STORE_HIT then
1408 # r1.store_way <= req.hit_way;
1409 # end if;
1410 #
1411 # -- Reset per-row valid bits,
1412 # -- ready for handling OP_LOAD_MISS
1413 # for i in 0 to ROW_PER_LINE - 1 loop
1414 # r1.rows_valid(i) <= '0';
1415 # end loop;
1416 #
1417 # case req.op is
1418 # when OP_LOAD_HIT =>
1419 # -- stay in IDLE state
1420 #
1421 # when OP_LOAD_MISS =>
1422 # -- Normal load cache miss,
1423 # -- start the reload machine
1424 # report "cache miss real addr:" &
1425 # to_hstring(req.real_addr) & " idx:" &
1426 # integer'image(get_index(req.real_addr)) &
1427 # " tag:" & to_hstring(get_tag(req.real_addr));
1428 #
1429 # -- Start the wishbone cycle
1430 # r1.wb.we <= '0';
1431 # r1.wb.cyc <= '1';
1432 # r1.wb.stb <= '1';
1433 #
1434 # -- Track that we had one request sent
1435 # r1.state <= RELOAD_WAIT_ACK;
1436 # r1.write_tag <= '1';
1437 #
1438 # when OP_LOAD_NC =>
1439 # r1.wb.cyc <= '1';
1440 # r1.wb.stb <= '1';
1441 # r1.wb.we <= '0';
1442 # r1.state <= NC_LOAD_WAIT_ACK;
1443 #
1444 # when OP_STORE_HIT | OP_STORE_MISS =>
1445 # if req.dcbz = '0' then
1446 # r1.state <= STORE_WAIT_ACK;
1447 # r1.acks_pending <= to_unsigned(1, 3);
1448 # r1.full <= '0';
1449 # r1.slow_valid <= '1';
1450 # if req.mmu_req = '0' then
1451 # r1.ls_valid <= '1';
1452 # else
1453 # r1.mmu_done <= '1';
1454 # end if;
1455 # if req.op = OP_STORE_HIT then
1456 # r1.write_bram <= '1';
1457 # end if;
1458 # else
1459 # -- dcbz is handled much like a load
1460 # -- miss except that we are writing
1461 # -- to memory instead of reading
1462 # r1.state <= RELOAD_WAIT_ACK;
1463 # if req.op = OP_STORE_MISS then
1464 # r1.write_tag <= '1';
1465 # end if;
1466 # end if;
1467 # r1.wb.we <= '1';
1468 # r1.wb.cyc <= '1';
1469 # r1.wb.stb <= '1';
1470 #
1471 # -- OP_NONE and OP_BAD do nothing
1472 # -- OP_BAD & OP_STCX_FAIL were handled above already
1473 # when OP_NONE =>
1474 # when OP_BAD =>
1475 # when OP_STCX_FAIL =>
1476 # end case;
1477 #
1478 # when RELOAD_WAIT_ACK =>
1479 # -- Requests are all sent if stb is 0
1480 # stbs_done := r1.wb.stb = '0';
1481 #
1482 # -- If we are still sending requests,
1483 # -- was one accepted?
1484 # if wishbone_in.stall = '0' and not stbs_done then
1485 # -- That was the last word ? We are done sending.
1486 # -- Clear stb and set stbs_done so we can handle
1487 # -- an eventual last ack on the same cycle.
1488 # if is_last_row_addr(r1.wb.adr, r1.end_row_ix) then
1489 # r1.wb.stb <= '0';
1490 # stbs_done := true;
1491 # end if;
1492 #
1493 # -- Calculate the next row address
1494 # r1.wb.adr <= next_row_addr(r1.wb.adr);
1495 # end if;
1496 #
1497 # -- Incoming acks processing
1498 # r1.forward_valid1 <= wishbone_in.ack;
1499 # if wishbone_in.ack = '1' then
1500 # r1.rows_valid(
1501 # r1.store_row mod ROW_PER_LINE
1502 # ) <= '1';
1503 # -- If this is the data we were looking for,
1504 # -- we can complete the request next cycle.
1505 # -- Compare the whole address in case the
1506 # -- request in r1.req is not the one that
1507 # -- started this refill.
1508 # if r1.full = '1' and r1.req.same_tag = '1'
1509 # and ((r1.dcbz = '1' and r1.req.dcbz = '1')
1510 # or (r1.dcbz = '0' and r1.req.op = OP_LOAD_MISS))
1511 # and r1.store_row = get_row(r1.req.real_addr) then
1512 # r1.full <= '0';
1513 # r1.slow_valid <= '1';
1514 # if r1.mmu_req = '0' then
1515 # r1.ls_valid <= '1';
1516 # else
1517 # r1.mmu_done <= '1';
1518 # end if;
1519 # r1.forward_sel <= (others => '1');
1520 # r1.use_forward1 <= '1';
1521 # end if;
1522 #
1523 # -- Check for completion
1524 # if stbs_done and is_last_row(r1.store_row,
1525 # r1.end_row_ix) then
1526 # -- Complete wishbone cycle
1527 # r1.wb.cyc <= '0';
1528 #
1529 # -- Cache line is now valid
1530 # cache_valids(r1.store_index)(
1531 # r1.store_way
1532 # ) <= '1';
1533 #
1534 # r1.state <= IDLE;
1535 # end if;
1536 #
1537 # -- Increment store row counter
1538 # r1.store_row <= next_row(r1.store_row);
1539 # end if;
1540 #
1541 # when STORE_WAIT_ACK =>
1542 # stbs_done := r1.wb.stb = '0';
1543 # acks := r1.acks_pending;
1544 # if r1.inc_acks /= r1.dec_acks then
1545 # if r1.inc_acks = '1' then
1546 # acks := acks + 1;
1547 # else
1548 # acks := acks - 1;
1549 # end if;
1550 # end if;
1551 # r1.acks_pending <= acks;
1552 # -- Clear stb when slave accepted request
1553 # if wishbone_in.stall = '0' then
1554 # -- See if there is another store waiting
1555 # -- to be done which is in the same real page.
1556 # if req.valid = '1' then
1557 # r1.wb.adr(
1558 # SET_SIZE_BITS - 1 downto 0
1559 # ) <= req.real_addr(
1560 # SET_SIZE_BITS - 1 downto 0
1561 # );
1562 # r1.wb.dat <= req.data;
1563 # r1.wb.sel <= req.byte_sel;
1564 # end if;
1565 # if acks < 7 and req.same_tag = '1'
1566 # and (req.op = OP_STORE_MISS
1567 # or req.op = OP_STORE_HIT) then
1568 # r1.wb.stb <= '1';
1569 # stbs_done := false;
1570 # if req.op = OP_STORE_HIT then
1571 # r1.write_bram <= '1';
1572 # end if;
1573 # r1.full <= '0';
1574 # r1.slow_valid <= '1';
1575 # -- Store requests never come from the MMU
1576 # r1.ls_valid <= '1';
1577 # stbs_done := false;
1578 # r1.inc_acks <= '1';
1579 # else
1580 # r1.wb.stb <= '0';
1581 # stbs_done := true;
1582 # end if;
1583 # end if;
1584 #
1585 # -- Got ack ? See if complete.
1586 # if wishbone_in.ack = '1' then
1587 # if stbs_done and acks = 1 then
1588 # r1.state <= IDLE;
1589 # r1.wb.cyc <= '0';
1590 # r1.wb.stb <= '0';
1591 # end if;
1592 # r1.dec_acks <= '1';
1593 # end if;
1594 #
1595 # when NC_LOAD_WAIT_ACK =>
1596 # -- Clear stb when slave accepted request
1597 # if wishbone_in.stall = '0' then
1598 # r1.wb.stb <= '0';
1599 # end if;
1600 #
1601 # -- Got ack ? complete.
1602 # if wishbone_in.ack = '1' then
1603 # r1.state <= IDLE;
1604 # r1.full <= '0';
1605 # r1.slow_valid <= '1';
1606 # if r1.mmu_req = '0' then
1607 # r1.ls_valid <= '1';
1608 # else
1609 # r1.mmu_done <= '1';
1610 # end if;
1611 # r1.forward_sel <= (others => '1');
1612 # r1.use_forward1 <= '1';
1613 # r1.wb.cyc <= '0';
1614 # r1.wb.stb <= '0';
1615 # end if;
1616 # end case;
1617 # end if;
1618 # end if;
1619 # end process;
1620 #
1621 # dc_log: if LOG_LENGTH > 0 generate
1622 # signal log_data : std_ulogic_vector(19 downto 0);
1623 # begin
1624 # dcache_log: process(clk)
1625 # begin
1626 # if rising_edge(clk) then
1627 # log_data <= r1.wb.adr(5 downto 3) &
1628 # wishbone_in.stall &
1629 # wishbone_in.ack &
1630 # r1.wb.stb & r1.wb.cyc &
1631 # d_out.error &
1632 # d_out.valid &
1633 # std_ulogic_vector(
1634 # to_unsigned(op_t'pos(req_op), 3)) &
1635 # stall_out &
1636 # std_ulogic_vector(
1637 # to_unsigned(tlb_hit_way, 3)) &
1638 # valid_ra &
1639 # std_ulogic_vector(
1640 # to_unsigned(state_t'pos(r1.state), 3));
1641 # end if;
1642 # end process;
1643 # log_out <= log_data;
1644 # end generate;
1645 # end;