dcache: Ease timing on calculation of acks remaining
[microwatt.git] / icache.vhdl
1 --
2 -- Set associative icache
3 --
4 -- TODO (in no specific order):
5 --
6 -- * Add debug interface to inspect cache content
7 -- * Add snoop/invalidate path
8 -- * Add multi-hit error detection
9 -- * Pipelined bus interface (wb or axi)
10 -- * Maybe add parity ? There's a few bits free in each BRAM row on Xilinx
11 -- * Add optimization: service hits on partially loaded lines
12 -- * Add optimization: (maybe) interrupt reload on fluch/redirect
13 -- * Check if playing with the geometry of the cache tags allow for more
14 -- efficient use of distributed RAM and less logic/muxes. Currently we
15 -- write TAG_BITS width which may not match full ram blocks and might
16 -- cause muxes to be inferred for "partial writes".
17 -- * Check if making the read size of PLRU a ROM helps utilization
18 --
19 library ieee;
20 use ieee.std_logic_1164.all;
21 use ieee.numeric_std.all;
22
23 library work;
24 use work.utils.all;
25 use work.common.all;
26 use work.wishbone_types.all;
27
28 -- 64 bit direct mapped icache. All instructions are 4B aligned.
29
30 entity icache is
31 generic (
32 SIM : boolean := false;
33 -- Line size in bytes
34 LINE_SIZE : positive := 64;
35 -- BRAM organisation: We never access more than wishbone_data_bits at
36 -- a time so to save resources we make the array only that wide, and
37 -- use consecutive indices for to make a cache "line"
38 --
39 -- ROW_SIZE is the width in bytes of the BRAM (based on WB, so 64-bits)
40 ROW_SIZE : positive := wishbone_data_bits / 8;
41 -- Number of lines in a set
42 NUM_LINES : positive := 32;
43 -- Number of ways
44 NUM_WAYS : positive := 4;
45 -- L1 ITLB number of entries (direct mapped)
46 TLB_SIZE : positive := 64;
47 -- L1 ITLB log_2(page_size)
48 TLB_LG_PGSZ : positive := 12;
49 -- Number of real address bits that we store
50 REAL_ADDR_BITS : positive := 56;
51 -- Non-zero to enable log data collection
52 LOG_LENGTH : natural := 0
53 );
54 port (
55 clk : in std_ulogic;
56 rst : in std_ulogic;
57
58 i_in : in Fetch1ToIcacheType;
59 i_out : out IcacheToDecode1Type;
60
61 m_in : in MmuToIcacheType;
62
63 stall_in : in std_ulogic;
64 stall_out : out std_ulogic;
65 flush_in : in std_ulogic;
66 inval_in : in std_ulogic;
67
68 wishbone_out : out wishbone_master_out;
69 wishbone_in : in wishbone_slave_out;
70
71 log_out : out std_ulogic_vector(53 downto 0)
72 );
73 end entity icache;
74
75 architecture rtl of icache is
76 constant ROW_SIZE_BITS : natural := ROW_SIZE*8;
77 -- ROW_PER_LINE is the number of row (wishbone transactions) in a line
78 constant ROW_PER_LINE : natural := LINE_SIZE / ROW_SIZE;
79 -- BRAM_ROWS is the number of rows in BRAM needed to represent the full
80 -- icache
81 constant BRAM_ROWS : natural := NUM_LINES * ROW_PER_LINE;
82 -- INSN_PER_ROW is the number of 32bit instructions per BRAM row
83 constant INSN_PER_ROW : natural := ROW_SIZE_BITS / 32;
84 -- Bit fields counts in the address
85
86 -- INSN_BITS is the number of bits to select an instruction in a row
87 constant INSN_BITS : natural := log2(INSN_PER_ROW);
88 -- ROW_BITS is the number of bits to select a row
89 constant ROW_BITS : natural := log2(BRAM_ROWS);
90 -- ROW_LINEBITS is the number of bits to select a row within a line
91 constant ROW_LINEBITS : natural := log2(ROW_PER_LINE);
92 -- LINE_OFF_BITS is the number of bits for the offset in a cache line
93 constant LINE_OFF_BITS : natural := log2(LINE_SIZE);
94 -- ROW_OFF_BITS is the number of bits for the offset in a row
95 constant ROW_OFF_BITS : natural := log2(ROW_SIZE);
96 -- INDEX_BITS is the number of bits to select a cache line
97 constant INDEX_BITS : natural := log2(NUM_LINES);
98 -- SET_SIZE_BITS is the log base 2 of the set size
99 constant SET_SIZE_BITS : natural := LINE_OFF_BITS + INDEX_BITS;
100 -- TAG_BITS is the number of bits of the tag part of the address
101 constant TAG_BITS : natural := REAL_ADDR_BITS - SET_SIZE_BITS;
102 -- WAY_BITS is the number of bits to select a way
103 constant WAY_BITS : natural := log2(NUM_WAYS);
104
105 -- Example of layout for 32 lines of 64 bytes:
106 --
107 -- .. tag |index| line |
108 -- .. | row | |
109 -- .. | | | |00| zero (2)
110 -- .. | | |-| | INSN_BITS (1)
111 -- .. | |---| | ROW_LINEBITS (3)
112 -- .. | |--- - --| LINE_OFF_BITS (6)
113 -- .. | |- --| ROW_OFF_BITS (3)
114 -- .. |----- ---| | ROW_BITS (8)
115 -- .. |-----| | INDEX_BITS (5)
116 -- .. --------| | TAG_BITS (53)
117
118 subtype row_t is integer range 0 to BRAM_ROWS-1;
119 subtype index_t is integer range 0 to NUM_LINES-1;
120 subtype way_t is integer range 0 to NUM_WAYS-1;
121 subtype row_in_line_t is unsigned(ROW_LINEBITS-1 downto 0);
122
123 -- The cache data BRAM organized as described above for each way
124 subtype cache_row_t is std_ulogic_vector(ROW_SIZE_BITS-1 downto 0);
125
126 -- The cache tags LUTRAM has a row per set. Vivado is a pain and will
127 -- not handle a clean (commented) definition of the cache tags as a 3d
128 -- memory. For now, work around 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_BITS * NUM_WAYS;
133 subtype cache_tags_set_t is std_logic_vector(TAG_RAM_WIDTH-1 downto 0);
134 type cache_tags_array_t is array(index_t) of cache_tags_set_t;
135
136 -- The cache valid bits
137 subtype cache_way_valids_t is std_ulogic_vector(NUM_WAYS-1 downto 0);
138 type cache_valids_t is array(index_t) of cache_way_valids_t;
139 type row_per_line_valid_t is array(0 to ROW_PER_LINE - 1) of std_ulogic;
140
141 -- Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
142 signal cache_tags : cache_tags_array_t;
143 signal cache_valids : cache_valids_t;
144
145 attribute ram_style : string;
146 attribute ram_style of cache_tags : signal is "distributed";
147
148 -- L1 ITLB.
149 constant TLB_BITS : natural := log2(TLB_SIZE);
150 constant TLB_EA_TAG_BITS : natural := 64 - (TLB_LG_PGSZ + TLB_BITS);
151 constant TLB_PTE_BITS : natural := 64;
152
153 subtype tlb_index_t is integer range 0 to TLB_SIZE - 1;
154 type tlb_valids_t is array(tlb_index_t) of std_ulogic;
155 subtype tlb_tag_t is std_ulogic_vector(TLB_EA_TAG_BITS - 1 downto 0);
156 type tlb_tags_t is array(tlb_index_t) of tlb_tag_t;
157 subtype tlb_pte_t is std_ulogic_vector(TLB_PTE_BITS - 1 downto 0);
158 type tlb_ptes_t is array(tlb_index_t) of tlb_pte_t;
159
160 signal itlb_valids : tlb_valids_t;
161 signal itlb_tags : tlb_tags_t;
162 signal itlb_ptes : tlb_ptes_t;
163 attribute ram_style of itlb_tags : signal is "distributed";
164 attribute ram_style of itlb_ptes : signal is "distributed";
165
166 -- Privilege bit from PTE EAA field
167 signal eaa_priv : std_ulogic;
168
169 -- Cache reload state machine
170 type state_t is (IDLE, CLR_TAG, WAIT_ACK);
171
172 type reg_internal_t is record
173 -- Cache hit state (Latches for 1 cycle BRAM access)
174 hit_way : way_t;
175 hit_nia : std_ulogic_vector(63 downto 0);
176 hit_smark : std_ulogic;
177 hit_valid : std_ulogic;
178
179 -- Cache miss state (reload state machine)
180 state : state_t;
181 wb : wishbone_master_out;
182 store_way : way_t;
183 store_index : index_t;
184 store_row : row_t;
185 store_tag : cache_tag_t;
186 store_valid : std_ulogic;
187 end_row_ix : row_in_line_t;
188 rows_valid : row_per_line_valid_t;
189
190 -- TLB miss state
191 fetch_failed : std_ulogic;
192 end record;
193
194 signal r : reg_internal_t;
195
196 -- Async signals on incoming request
197 signal req_index : index_t;
198 signal req_row : row_t;
199 signal req_hit_way : way_t;
200 signal req_tag : cache_tag_t;
201 signal req_is_hit : std_ulogic;
202 signal req_is_miss : std_ulogic;
203 signal req_laddr : std_ulogic_vector(63 downto 0);
204
205 signal tlb_req_index : tlb_index_t;
206 signal real_addr : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
207 signal ra_valid : std_ulogic;
208 signal priv_fault : std_ulogic;
209 signal access_ok : std_ulogic;
210 signal use_previous : std_ulogic;
211
212 -- Cache RAM interface
213 type cache_ram_out_t is array(way_t) of cache_row_t;
214 signal cache_out : cache_ram_out_t;
215
216 -- PLRU output interface
217 type plru_out_t is array(index_t) of std_ulogic_vector(WAY_BITS-1 downto 0);
218 signal plru_victim : plru_out_t;
219 signal replace_way : way_t;
220
221 -- Return the cache line index (tag index) for an address
222 function get_index(addr: std_ulogic_vector(63 downto 0)) return index_t is
223 begin
224 return to_integer(unsigned(addr(SET_SIZE_BITS - 1 downto LINE_OFF_BITS)));
225 end;
226
227 -- Return the cache row index (data memory) for an address
228 function get_row(addr: std_ulogic_vector(63 downto 0)) return row_t is
229 begin
230 return to_integer(unsigned(addr(SET_SIZE_BITS - 1 downto ROW_OFF_BITS)));
231 end;
232
233 -- Return the index of a row within a line
234 function get_row_of_line(row: row_t) return row_in_line_t is
235 variable row_v : unsigned(ROW_BITS-1 downto 0);
236 begin
237 row_v := to_unsigned(row, ROW_BITS);
238 return row_v(ROW_LINEBITS-1 downto 0);
239 end;
240
241 -- Returns whether this is the last row of a line
242 function is_last_row_addr(addr: wishbone_addr_type; last: row_in_line_t) return boolean is
243 begin
244 return unsigned(addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS)) = last;
245 end;
246
247 -- Returns whether this is the last row of a line
248 function is_last_row(row: row_t; last: row_in_line_t) return boolean is
249 begin
250 return get_row_of_line(row) = last;
251 end;
252
253 -- Return the address of the next row in the current cache line
254 function next_row_addr(addr: wishbone_addr_type)
255 return std_ulogic_vector is
256 variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
257 variable result : wishbone_addr_type;
258 begin
259 -- Is there no simpler way in VHDL to generate that 3 bits adder ?
260 row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
261 row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
262 result := addr;
263 result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
264 return result;
265 end;
266
267 -- Return the next row in the current cache line. We use a dedicated
268 -- function in order to limit the size of the generated adder to be
269 -- only the bits within a cache line (3 bits with default settings)
270 --
271 function next_row(row: row_t) return row_t is
272 variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
273 variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
274 variable result : std_ulogic_vector(ROW_BITS-1 downto 0);
275 begin
276 row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
277 row_idx := row_v(ROW_LINEBITS-1 downto 0);
278 row_v(ROW_LINEBITS-1 downto 0) := std_ulogic_vector(unsigned(row_idx) + 1);
279 return to_integer(unsigned(row_v));
280 end;
281
282 -- Read the instruction word for the given address in the current cache row
283 function read_insn_word(addr: std_ulogic_vector(63 downto 0);
284 data: cache_row_t) return std_ulogic_vector is
285 variable word: integer range 0 to INSN_PER_ROW-1;
286 begin
287 word := to_integer(unsigned(addr(INSN_BITS+2-1 downto 2)));
288 return data(31+word*32 downto word*32);
289 end;
290
291 -- Get the tag value from the address
292 function get_tag(addr: std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0)) return cache_tag_t is
293 begin
294 return addr(REAL_ADDR_BITS - 1 downto SET_SIZE_BITS);
295 end;
296
297 -- Read a tag from a tag memory row
298 function read_tag(way: way_t; tagset: cache_tags_set_t) return cache_tag_t is
299 begin
300 return tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS);
301 end;
302
303 -- Write a tag to tag memory row
304 procedure write_tag(way: in way_t; tagset: inout cache_tags_set_t;
305 tag: cache_tag_t) is
306 begin
307 tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS) := tag;
308 end;
309
310 -- Simple hash for direct-mapped TLB index
311 function hash_ea(addr: std_ulogic_vector(63 downto 0)) return tlb_index_t is
312 variable hash : std_ulogic_vector(TLB_BITS - 1 downto 0);
313 begin
314 hash := addr(TLB_LG_PGSZ + TLB_BITS - 1 downto TLB_LG_PGSZ)
315 xor addr(TLB_LG_PGSZ + 2 * TLB_BITS - 1 downto TLB_LG_PGSZ + TLB_BITS)
316 xor addr(TLB_LG_PGSZ + 3 * TLB_BITS - 1 downto TLB_LG_PGSZ + 2 * TLB_BITS);
317 return to_integer(unsigned(hash));
318 end;
319 begin
320
321 assert LINE_SIZE mod ROW_SIZE = 0;
322 assert ispow2(LINE_SIZE) report "LINE_SIZE not power of 2" severity FAILURE;
323 assert ispow2(NUM_LINES) report "NUM_LINES not power of 2" severity FAILURE;
324 assert ispow2(ROW_PER_LINE) report "ROW_PER_LINE not power of 2" severity FAILURE;
325 assert ispow2(INSN_PER_ROW) report "INSN_PER_ROW not power of 2" severity FAILURE;
326 assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
327 report "geometry bits don't add up" severity FAILURE;
328 assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
329 report "geometry bits don't add up" severity FAILURE;
330 assert (REAL_ADDR_BITS = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
331 report "geometry bits don't add up" severity FAILURE;
332 assert (REAL_ADDR_BITS = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
333 report "geometry bits don't add up" severity FAILURE;
334
335 sim_debug: if SIM generate
336 debug: process
337 begin
338 report "ROW_SIZE = " & natural'image(ROW_SIZE);
339 report "ROW_PER_LINE = " & natural'image(ROW_PER_LINE);
340 report "BRAM_ROWS = " & natural'image(BRAM_ROWS);
341 report "INSN_PER_ROW = " & natural'image(INSN_PER_ROW);
342 report "INSN_BITS = " & natural'image(INSN_BITS);
343 report "ROW_BITS = " & natural'image(ROW_BITS);
344 report "ROW_LINEBITS = " & natural'image(ROW_LINEBITS);
345 report "LINE_OFF_BITS = " & natural'image(LINE_OFF_BITS);
346 report "ROW_OFF_BITS = " & natural'image(ROW_OFF_BITS);
347 report "INDEX_BITS = " & natural'image(INDEX_BITS);
348 report "TAG_BITS = " & natural'image(TAG_BITS);
349 report "WAY_BITS = " & natural'image(WAY_BITS);
350 wait;
351 end process;
352 end generate;
353
354 -- Generate a cache RAM for each way
355 rams: for i in 0 to NUM_WAYS-1 generate
356 signal do_read : std_ulogic;
357 signal do_write : std_ulogic;
358 signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
359 signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
360 signal dout : cache_row_t;
361 signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
362 begin
363 way: entity work.cache_ram
364 generic map (
365 ROW_BITS => ROW_BITS,
366 WIDTH => ROW_SIZE_BITS
367 )
368 port map (
369 clk => clk,
370 rd_en => do_read,
371 rd_addr => rd_addr,
372 rd_data => dout,
373 wr_sel => wr_sel,
374 wr_addr => wr_addr,
375 wr_data => wishbone_in.dat
376 );
377 process(all)
378 begin
379 do_read <= not (stall_in or use_previous);
380 do_write <= '0';
381 if wishbone_in.ack = '1' and replace_way = i then
382 do_write <= '1';
383 end if;
384 cache_out(i) <= dout;
385 rd_addr <= std_ulogic_vector(to_unsigned(req_row, ROW_BITS));
386 wr_addr <= std_ulogic_vector(to_unsigned(r.store_row, ROW_BITS));
387 for i in 0 to ROW_SIZE-1 loop
388 wr_sel(i) <= do_write;
389 end loop;
390 end process;
391 end generate;
392
393 -- Generate PLRUs
394 maybe_plrus: if NUM_WAYS > 1 generate
395 begin
396 plrus: for i in 0 to NUM_LINES-1 generate
397 -- PLRU interface
398 signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
399 signal plru_acc_en : std_ulogic;
400 signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
401
402 begin
403 plru : entity work.plru
404 generic map (
405 BITS => WAY_BITS
406 )
407 port map (
408 clk => clk,
409 rst => rst,
410 acc => plru_acc,
411 acc_en => plru_acc_en,
412 lru => plru_out
413 );
414
415 process(all)
416 begin
417 -- PLRU interface
418 if get_index(r.hit_nia) = i then
419 plru_acc_en <= r.hit_valid;
420 else
421 plru_acc_en <= '0';
422 end if;
423 plru_acc <= std_ulogic_vector(to_unsigned(r.hit_way, WAY_BITS));
424 plru_victim(i) <= plru_out;
425 end process;
426 end generate;
427 end generate;
428
429 -- TLB hit detection and real address generation
430 itlb_lookup : process(all)
431 variable pte : tlb_pte_t;
432 variable ttag : tlb_tag_t;
433 begin
434 tlb_req_index <= hash_ea(i_in.nia);
435 pte := itlb_ptes(tlb_req_index);
436 ttag := itlb_tags(tlb_req_index);
437 if i_in.virt_mode = '1' then
438 real_addr <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
439 i_in.nia(TLB_LG_PGSZ - 1 downto 0);
440 if ttag = i_in.nia(63 downto TLB_LG_PGSZ + TLB_BITS) then
441 ra_valid <= itlb_valids(tlb_req_index);
442 else
443 ra_valid <= '0';
444 end if;
445 eaa_priv <= pte(3);
446 else
447 real_addr <= i_in.nia(REAL_ADDR_BITS - 1 downto 0);
448 ra_valid <= '1';
449 eaa_priv <= '1';
450 end if;
451
452 -- no IAMR, so no KUEP support for now
453 priv_fault <= eaa_priv and not i_in.priv_mode;
454 access_ok <= ra_valid and not priv_fault;
455 end process;
456
457 -- iTLB update
458 itlb_update: process(clk)
459 variable wr_index : tlb_index_t;
460 begin
461 if rising_edge(clk) then
462 wr_index := hash_ea(m_in.addr);
463 if rst = '1' or (m_in.tlbie = '1' and m_in.doall = '1') then
464 -- clear all valid bits
465 for i in tlb_index_t loop
466 itlb_valids(i) <= '0';
467 end loop;
468 elsif m_in.tlbie = '1' then
469 -- clear entry regardless of hit or miss
470 itlb_valids(wr_index) <= '0';
471 elsif m_in.tlbld = '1' then
472 itlb_tags(wr_index) <= m_in.addr(63 downto TLB_LG_PGSZ + TLB_BITS);
473 itlb_ptes(wr_index) <= m_in.pte;
474 itlb_valids(wr_index) <= '1';
475 end if;
476 end if;
477 end process;
478
479 -- Cache hit detection, output to fetch2 and other misc logic
480 icache_comb : process(all)
481 variable is_hit : std_ulogic;
482 variable hit_way : way_t;
483 begin
484 -- i_in.sequential means that i_in.nia this cycle is 4 more than
485 -- last cycle. If we read more than 32 bits at a time, had a cache hit
486 -- last cycle, and we don't want the first 32-bit chunk, then we can
487 -- keep the data we read last cycle and just use that.
488 if unsigned(i_in.nia(INSN_BITS+2-1 downto 2)) /= 0 then
489 use_previous <= i_in.sequential and r.hit_valid;
490 else
491 use_previous <= '0';
492 end if;
493
494 -- Extract line, row and tag from request
495 req_index <= get_index(i_in.nia);
496 req_row <= get_row(i_in.nia);
497 req_tag <= get_tag(real_addr);
498
499 -- Calculate address of beginning of cache row, will be
500 -- used for cache miss processing if needed
501 --
502 req_laddr <= (63 downto REAL_ADDR_BITS => '0') &
503 real_addr(REAL_ADDR_BITS - 1 downto ROW_OFF_BITS) &
504 (ROW_OFF_BITS-1 downto 0 => '0');
505
506 -- Test if pending request is a hit on any way
507 hit_way := 0;
508 is_hit := '0';
509 for i in way_t loop
510 if i_in.req = '1' and
511 (cache_valids(req_index)(i) = '1' or
512 (r.state = WAIT_ACK and
513 req_index = r.store_index and
514 i = r.store_way and
515 r.rows_valid(req_row mod ROW_PER_LINE) = '1')) then
516 if read_tag(i, cache_tags(req_index)) = req_tag then
517 hit_way := i;
518 is_hit := '1';
519 end if;
520 end if;
521 end loop;
522
523 -- Generate the "hit" and "miss" signals for the synchronous blocks
524 if i_in.req = '1' and access_ok = '1' and flush_in = '0' and rst = '0' then
525 req_is_hit <= is_hit;
526 req_is_miss <= not is_hit;
527 else
528 req_is_hit <= '0';
529 req_is_miss <= '0';
530 end if;
531 req_hit_way <= hit_way;
532
533 -- The way to replace on a miss
534 if r.state = CLR_TAG then
535 replace_way <= to_integer(unsigned(plru_victim(r.store_index)));
536 else
537 replace_way <= r.store_way;
538 end if;
539
540 -- Output instruction from current cache row
541 --
542 -- Note: This is a mild violation of our design principle of having pipeline
543 -- stages output from a clean latch. In this case we output the result
544 -- of a mux. The alternative would be output an entire row which
545 -- I prefer not to do just yet as it would force fetch2 to know about
546 -- some of the cache geometry information.
547 --
548 i_out.insn <= read_insn_word(r.hit_nia, cache_out(r.hit_way));
549 i_out.valid <= r.hit_valid;
550 i_out.nia <= r.hit_nia;
551 i_out.stop_mark <= r.hit_smark;
552 i_out.fetch_failed <= r.fetch_failed;
553
554 -- Stall fetch1 if we have a miss on cache or TLB or a protection fault
555 stall_out <= not (is_hit and access_ok);
556
557 -- Wishbone requests output (from the cache miss reload machine)
558 wishbone_out <= r.wb;
559 end process;
560
561 -- Cache hit synchronous machine
562 icache_hit : process(clk)
563 begin
564 if rising_edge(clk) then
565 -- keep outputs to fetch2 unchanged on a stall
566 -- except that flush or reset sets valid to 0
567 -- If use_previous, keep the same data as last cycle and use the second half
568 if stall_in = '1' or use_previous = '1' then
569 if rst = '1' or flush_in = '1' then
570 r.hit_valid <= '0';
571 end if;
572 else
573 -- On a hit, latch the request for the next cycle, when the BRAM data
574 -- will be available on the cache_out output of the corresponding way
575 --
576 r.hit_valid <= req_is_hit;
577 if req_is_hit = '1' then
578 r.hit_way <= req_hit_way;
579
580 report "cache hit nia:" & to_hstring(i_in.nia) &
581 " IR:" & std_ulogic'image(i_in.virt_mode) &
582 " SM:" & std_ulogic'image(i_in.stop_mark) &
583 " idx:" & integer'image(req_index) &
584 " tag:" & to_hstring(req_tag) &
585 " way:" & integer'image(req_hit_way) &
586 " RA:" & to_hstring(real_addr);
587 end if;
588 end if;
589 if stall_in = '0' then
590 -- Send stop marks and NIA down regardless of validity
591 r.hit_smark <= i_in.stop_mark;
592 r.hit_nia <= i_in.nia;
593 end if;
594 end if;
595 end process;
596
597 -- Cache miss/reload synchronous machine
598 icache_miss : process(clk)
599 variable tagset : cache_tags_set_t;
600 variable stbs_done : boolean;
601 begin
602 if rising_edge(clk) then
603 -- On reset, clear all valid bits to force misses
604 if rst = '1' then
605 for i in index_t loop
606 cache_valids(i) <= (others => '0');
607 end loop;
608 r.state <= IDLE;
609 r.wb.cyc <= '0';
610 r.wb.stb <= '0';
611
612 -- We only ever do reads on wishbone
613 r.wb.dat <= (others => '0');
614 r.wb.sel <= "11111111";
615 r.wb.we <= '0';
616
617 -- Not useful normally but helps avoiding tons of sim warnings
618 r.wb.adr <= (others => '0');
619 else
620 -- Process cache invalidations
621 if inval_in = '1' then
622 for i in index_t loop
623 cache_valids(i) <= (others => '0');
624 end loop;
625 r.store_valid <= '0';
626 end if;
627
628 -- Main state machine
629 case r.state is
630 when IDLE =>
631 -- Reset per-row valid flags, only used in WAIT_ACK
632 for i in 0 to ROW_PER_LINE - 1 loop
633 r.rows_valid(i) <= '0';
634 end loop;
635
636 -- We need to read a cache line
637 if req_is_miss = '1' then
638 report "cache miss nia:" & to_hstring(i_in.nia) &
639 " IR:" & std_ulogic'image(i_in.virt_mode) &
640 " SM:" & std_ulogic'image(i_in.stop_mark) &
641 " idx:" & integer'image(req_index) &
642 " way:" & integer'image(replace_way) &
643 " tag:" & to_hstring(req_tag) &
644 " RA:" & to_hstring(real_addr);
645
646 -- Keep track of our index and way for subsequent stores
647 r.store_index <= req_index;
648 r.store_row <= get_row(req_laddr);
649 r.store_tag <= req_tag;
650 r.store_valid <= '1';
651 r.end_row_ix <= get_row_of_line(get_row(req_laddr)) - 1;
652
653 -- Prep for first wishbone read. We calculate the address of
654 -- the start of the cache line and start the WB cycle.
655 --
656 r.wb.adr <= req_laddr(r.wb.adr'left downto 0);
657 r.wb.cyc <= '1';
658 r.wb.stb <= '1';
659
660 -- Track that we had one request sent
661 r.state <= CLR_TAG;
662 end if;
663
664 when CLR_TAG | WAIT_ACK =>
665 if r.state = CLR_TAG then
666 -- Get victim way from plru
667 r.store_way <= replace_way;
668
669 -- Force misses on that way while reloading that line
670 cache_valids(req_index)(replace_way) <= '0';
671
672 -- Store new tag in selected way
673 for i in 0 to NUM_WAYS-1 loop
674 if i = replace_way then
675 tagset := cache_tags(r.store_index);
676 write_tag(i, tagset, r.store_tag);
677 cache_tags(r.store_index) <= tagset;
678 end if;
679 end loop;
680
681 r.state <= WAIT_ACK;
682 end if;
683 -- Requests are all sent if stb is 0
684 stbs_done := r.wb.stb = '0';
685
686 -- If we are still sending requests, was one accepted ?
687 if wishbone_in.stall = '0' and not stbs_done then
688 -- That was the last word ? We are done sending. Clear
689 -- stb and set stbs_done so we can handle an eventual last
690 -- ack on the same cycle.
691 --
692 if is_last_row_addr(r.wb.adr, r.end_row_ix) then
693 r.wb.stb <= '0';
694 stbs_done := true;
695 end if;
696
697 -- Calculate the next row address
698 r.wb.adr <= next_row_addr(r.wb.adr);
699 end if;
700
701 -- Incoming acks processing
702 if wishbone_in.ack = '1' then
703 r.rows_valid(r.store_row mod ROW_PER_LINE) <= '1';
704 -- Check for completion
705 if stbs_done and is_last_row(r.store_row, r.end_row_ix) then
706 -- Complete wishbone cycle
707 r.wb.cyc <= '0';
708
709 -- Cache line is now valid
710 cache_valids(r.store_index)(replace_way) <= r.store_valid and not inval_in;
711
712 -- We are done
713 r.state <= IDLE;
714 end if;
715
716 -- Increment store row counter
717 r.store_row <= next_row(r.store_row);
718 end if;
719 end case;
720 end if;
721
722 -- TLB miss and protection fault processing
723 if rst = '1' or flush_in = '1' or m_in.tlbld = '1' then
724 r.fetch_failed <= '0';
725 elsif i_in.req = '1' and access_ok = '0' and stall_in = '0' then
726 r.fetch_failed <= '1';
727 end if;
728 end if;
729 end process;
730
731 icache_log: if LOG_LENGTH > 0 generate
732 -- Output data to logger
733 signal log_data : std_ulogic_vector(53 downto 0);
734 begin
735 data_log: process(clk)
736 variable lway: way_t;
737 variable wstate: std_ulogic;
738 begin
739 if rising_edge(clk) then
740 lway := req_hit_way;
741 wstate := '0';
742 if r.state /= IDLE then
743 wstate := '1';
744 end if;
745 log_data <= i_out.valid &
746 i_out.insn &
747 wishbone_in.ack &
748 r.wb.adr(5 downto 3) &
749 r.wb.stb & r.wb.cyc &
750 wishbone_in.stall &
751 stall_out &
752 r.fetch_failed &
753 r.hit_nia(5 downto 2) &
754 wstate &
755 std_ulogic_vector(to_unsigned(lway, 3)) &
756 req_is_hit & req_is_miss &
757 access_ok &
758 ra_valid;
759 end if;
760 end process;
761 log_out <= log_data;
762 end generate;
763 end;