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