Merge pull request #114 from antonblanchard/dcache
[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.common.all;
25 use work.wishbone_types.all;
26
27 -- 64 bit direct mapped icache. All instructions are 4B aligned.
28
29 entity icache is
30 generic (
31 -- Line size in bytes
32 LINE_SIZE : positive := 64;
33 -- Number of lines in a set
34 NUM_LINES : positive := 32;
35 -- Number of ways
36 NUM_WAYS : positive := 4
37 );
38 port (
39 clk : in std_ulogic;
40 rst : in std_ulogic;
41
42 i_in : in Fetch1ToIcacheType;
43 i_out : out IcacheToFetch2Type;
44
45 stall_out : out std_ulogic;
46 flush_in : in std_ulogic;
47
48 wishbone_out : out wishbone_master_out;
49 wishbone_in : in wishbone_slave_out
50 );
51 end entity icache;
52
53 architecture rtl of icache is
54 function log2(i : natural) return integer is
55 variable tmp : integer := i;
56 variable ret : integer := 0;
57 begin
58 while tmp > 1 loop
59 ret := ret + 1;
60 tmp := tmp / 2;
61 end loop;
62 return ret;
63 end function;
64
65 function ispow2(i : integer) return boolean is
66 begin
67 if to_integer(to_unsigned(i, 32) and to_unsigned(i - 1, 32)) = 0 then
68 return true;
69 else
70 return false;
71 end if;
72 end function;
73
74 -- BRAM organisation: We never access more than wishbone_data_bits at
75 -- a time so to save resources we make the array only that wide, and
76 -- use consecutive indices for to make a cache "line"
77 --
78 -- ROW_SIZE is the width in bytes of the BRAM (based on WB, so 64-bits)
79 constant ROW_SIZE : natural := wishbone_data_bits / 8;
80 -- ROW_PER_LINE is the number of row (wishbone transactions) in a line
81 constant ROW_PER_LINE : natural := LINE_SIZE / ROW_SIZE;
82 -- BRAM_ROWS is the number of rows in BRAM needed to represent the full
83 -- icache
84 constant BRAM_ROWS : natural := NUM_LINES * ROW_PER_LINE;
85 -- INSN_PER_ROW is the number of 32bit instructions per BRAM row
86 constant INSN_PER_ROW : natural := wishbone_data_bits / 32;
87 -- Bit fields counts in the address
88
89 -- INSN_BITS is the number of bits to select an instruction in a row
90 constant INSN_BITS : natural := log2(INSN_PER_ROW);
91 -- ROW_BITS is the number of bits to select a row
92 constant ROW_BITS : natural := log2(BRAM_ROWS);
93 -- ROW_LINEBITS is the number of bits to select a row within a line
94 constant ROW_LINEBITS : natural := log2(ROW_PER_LINE);
95 -- LINE_OFF_BITS is the number of bits for the offset in a cache line
96 constant LINE_OFF_BITS : natural := log2(LINE_SIZE);
97 -- ROW_OFF_BITS is the number of bits for the offset in a row
98 constant ROW_OFF_BITS : natural := log2(ROW_SIZE);
99 -- INDEX_BITS is the number if bits to select a cache line
100 constant INDEX_BITS : natural := log2(NUM_LINES);
101 -- TAG_BITS is the number of bits of the tag part of the address
102 constant TAG_BITS : natural := 64 - LINE_OFF_BITS - INDEX_BITS;
103 -- WAY_BITS is the number of bits to select a way
104 constant WAY_BITS : natural := log2(NUM_WAYS);
105
106 -- Example of layout for 32 lines of 64 bytes:
107 --
108 -- .. tag |index| line |
109 -- .. | row | |
110 -- .. | | | |00| zero (2)
111 -- .. | | |-| | INSN_BITS (1)
112 -- .. | |---| | ROW_LINEBITS (3)
113 -- .. | |--- - --| LINE_OFF_BITS (6)
114 -- .. | |- --| ROW_OFF_BITS (3)
115 -- .. |----- ---| | ROW_BITS (8)
116 -- .. |-----| | INDEX_BITS (5)
117 -- .. --------| | TAG_BITS (53)
118
119 subtype row_t is integer range 0 to BRAM_ROWS-1;
120 subtype index_t is integer range 0 to NUM_LINES-1;
121 subtype way_t is integer range 0 to NUM_WAYS-1;
122
123 -- The cache data BRAM organized as described above for each way
124 subtype cache_row_t is std_ulogic_vector(wishbone_data_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
140 -- Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
141 signal cache_tags : cache_tags_array_t;
142 signal cache_valids : cache_valids_t;
143
144 attribute ram_style : string;
145 attribute ram_style of cache_tags : signal is "distributed";
146
147 -- Cache reload state machine
148 type state_t is (IDLE, WAIT_ACK);
149
150 type reg_internal_t is record
151 -- Cache hit state (Latches for 1 cycle BRAM access)
152 hit_way : way_t;
153 hit_nia : std_ulogic_vector(63 downto 0);
154 hit_smark : std_ulogic;
155 hit_valid : std_ulogic;
156
157 -- Cache miss state (reload state machine)
158 state : state_t;
159 wb : wishbone_master_out;
160 store_way : way_t;
161 store_index : index_t;
162 end record;
163
164 signal r : reg_internal_t;
165
166 -- Async signals on incoming request
167 signal req_index : index_t;
168 signal req_row : row_t;
169 signal req_hit_way : way_t;
170 signal req_tag : cache_tag_t;
171 signal req_is_hit : std_ulogic;
172 signal req_is_miss : std_ulogic;
173
174 -- Cache RAM interface
175 type cache_ram_out_t is array(way_t) of cache_row_t;
176 signal cache_out : cache_ram_out_t;
177
178 -- PLRU output interface
179 type plru_out_t is array(index_t) of std_ulogic_vector(WAY_BITS-1 downto 0);
180 signal plru_victim : plru_out_t;
181 signal replace_way : way_t;
182
183 -- Return the cache line index (tag index) for an address
184 function get_index(addr: std_ulogic_vector(63 downto 0)) return index_t is
185 begin
186 return to_integer(unsigned(addr(63-TAG_BITS downto LINE_OFF_BITS)));
187 end;
188
189 -- Return the cache row index (data memory) for an address
190 function get_row(addr: std_ulogic_vector(63 downto 0)) return row_t is
191 begin
192 return to_integer(unsigned(addr(63-TAG_BITS downto ROW_OFF_BITS)));
193 end;
194
195 -- Returns whether this is the last row of a line
196 function is_last_row(addr: std_ulogic_vector(63 downto 0)) return boolean is
197 constant ones : std_ulogic_vector(ROW_LINEBITS-1 downto 0) := (others => '1');
198 begin
199 return addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS) = ones;
200 end;
201
202 -- Return the address of the next row in the current cache line
203 function next_row_addr(addr: std_ulogic_vector(63 downto 0)) return std_ulogic_vector is
204 variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
205 variable result : std_ulogic_vector(63 downto 0);
206 begin
207 -- Is there no simpler way in VHDL to generate that 3 bits adder ?
208 row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
209 row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
210 result := addr;
211 result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
212 return result;
213 end;
214
215 -- Read the instruction word for the given address in the current cache row
216 function read_insn_word(addr: std_ulogic_vector(63 downto 0);
217 data: cache_row_t) return std_ulogic_vector is
218 variable word: integer range 0 to INSN_PER_ROW-1;
219 begin
220 word := to_integer(unsigned(addr(INSN_BITS+2-1 downto 2)));
221 return data(31+word*32 downto word*32);
222 end;
223
224 -- Get the tag value from the address
225 function get_tag(addr: std_ulogic_vector(63 downto 0)) return cache_tag_t is
226 begin
227 return addr(63 downto 64-TAG_BITS);
228 end;
229
230 -- Read a tag from a tag memory row
231 function read_tag(way: way_t; tagset: cache_tags_set_t) return cache_tag_t is
232 begin
233 return tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS);
234 end;
235
236 -- Write a tag to tag memory row
237 procedure write_tag(way: in way_t; tagset: inout cache_tags_set_t;
238 tag: cache_tag_t) is
239 begin
240 tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS) := tag;
241 end;
242
243 begin
244
245 assert LINE_SIZE mod ROW_SIZE = 0;
246 assert ispow2(LINE_SIZE) report "LINE_SIZE not power of 2" severity FAILURE;
247 assert ispow2(NUM_LINES) report "NUM_LINES not power of 2" severity FAILURE;
248 assert ispow2(ROW_PER_LINE) report "ROW_PER_LINE not power of 2" severity FAILURE;
249 assert ispow2(INSN_PER_ROW) report "INSN_PER_ROW not power of 2" severity FAILURE;
250 assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
251 report "geometry bits don't add up" severity FAILURE;
252 assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
253 report "geometry bits don't add up" severity FAILURE;
254 assert (64 = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
255 report "geometry bits don't add up" severity FAILURE;
256 assert (64 = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
257 report "geometry bits don't add up" severity FAILURE;
258
259 debug: process
260 begin
261 report "ROW_SIZE = " & natural'image(ROW_SIZE);
262 report "ROW_PER_LINE = " & natural'image(ROW_PER_LINE);
263 report "BRAM_ROWS = " & natural'image(BRAM_ROWS);
264 report "INSN_PER_ROW = " & natural'image(INSN_PER_ROW);
265 report "INSN_BITS = " & natural'image(INSN_BITS);
266 report "ROW_BITS = " & natural'image(ROW_BITS);
267 report "ROW_LINEBITS = " & natural'image(ROW_LINEBITS);
268 report "LINE_OFF_BITS = " & natural'image(LINE_OFF_BITS);
269 report "ROW_OFF_BITS = " & natural'image(ROW_OFF_BITS);
270 report "INDEX_BITS = " & natural'image(INDEX_BITS);
271 report "TAG_BITS = " & natural'image(TAG_BITS);
272 report "WAY_BITS = " & natural'image(WAY_BITS);
273 wait;
274 end process;
275
276 -- Generate a cache RAM for each way
277 rams: for i in 0 to NUM_WAYS-1 generate
278 signal do_read : std_ulogic;
279 signal do_write : std_ulogic;
280 signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
281 signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
282 signal dout : cache_row_t;
283 begin
284 way: entity work.cache_ram
285 generic map (
286 ROW_BITS => ROW_BITS,
287 WIDTH => wishbone_data_bits
288 )
289 port map (
290 clk => clk,
291 rd_en => do_read,
292 rd_addr => rd_addr,
293 rd_data => dout,
294 wr_en => do_write,
295 wr_sel => (others => '1'),
296 wr_addr => wr_addr,
297 wr_data => wishbone_in.dat
298 );
299 process(all)
300 begin
301 do_read <= '1';
302 do_write <= '0';
303 if wishbone_in.ack = '1' and r.store_way = i then
304 do_write <= '1';
305 end if;
306 cache_out(i) <= dout;
307 rd_addr <= std_ulogic_vector(to_unsigned(req_row, ROW_BITS));
308 wr_addr <= std_ulogic_vector(to_unsigned(get_row(r.wb.adr), ROW_BITS));
309 end process;
310 end generate;
311
312 -- Generate PLRUs
313 maybe_plrus: if NUM_WAYS > 1 generate
314 begin
315 plrus: for i in 0 to NUM_LINES-1 generate
316 -- PLRU interface
317 signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
318 signal plru_acc_en : std_ulogic;
319 signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
320
321 begin
322 plru : entity work.plru
323 generic map (
324 BITS => WAY_BITS
325 )
326 port map (
327 clk => clk,
328 rst => rst,
329 acc => plru_acc,
330 acc_en => plru_acc_en,
331 lru => plru_out
332 );
333
334 process(req_index, req_is_hit, req_hit_way, req_is_hit, plru_out)
335 begin
336 -- PLRU interface
337 if req_is_hit = '1' and req_index = i then
338 plru_acc_en <= req_is_hit;
339 else
340 plru_acc_en <= '0';
341 end if;
342 plru_acc <= std_ulogic_vector(to_unsigned(req_hit_way, WAY_BITS));
343 plru_victim(i) <= plru_out;
344 end process;
345 end generate;
346 end generate;
347
348 -- Cache hit detection, output to fetch2 and other misc logic
349 icache_comb : process(all)
350 variable is_hit : std_ulogic;
351 variable hit_way : way_t;
352 begin
353 -- Extract line, row and tag from request
354 req_index <= get_index(i_in.nia);
355 req_row <= get_row(i_in.nia);
356 req_tag <= get_tag(i_in.nia);
357
358 -- Test if pending request is a hit on any way
359 hit_way := 0;
360 is_hit := '0';
361 for i in way_t loop
362 if i_in.req = '1' and cache_valids(req_index)(i) = '1' then
363 if read_tag(i, cache_tags(req_index)) = req_tag then
364 hit_way := i;
365 is_hit := '1';
366 end if;
367 end if;
368 end loop;
369
370 -- Generate the "hit" and "miss" signals for the synchronous blocks
371 req_is_hit <= i_in.req and is_hit and not flush_in;
372 req_is_miss <= i_in.req and not is_hit and not flush_in;
373 req_hit_way <= hit_way;
374
375 -- The way to replace on a miss
376 replace_way <= to_integer(unsigned(plru_victim(req_index)));
377
378 -- Output instruction from current cache row
379 --
380 -- Note: This is a mild violation of our design principle of having pipeline
381 -- stages output from a clean latch. In this case we output the result
382 -- of a mux. The alternative would be output an entire row which
383 -- I prefer not to do just yet as it would force fetch2 to know about
384 -- some of the cache geometry information.
385 --
386 i_out.insn <= read_insn_word(r.hit_nia, cache_out(r.hit_way));
387 i_out.valid <= r.hit_valid;
388 i_out.nia <= r.hit_nia;
389 i_out.stop_mark <= r.hit_smark;
390
391 -- Stall fetch1 if we have a miss
392 stall_out <= not is_hit;
393
394 -- Wishbone requests output (from the cache miss reload machine)
395 wishbone_out <= r.wb;
396 end process;
397
398 -- Cache hit synchronous machine
399 icache_hit : process(clk)
400 begin
401 if rising_edge(clk) then
402 -- On a hit, latch the request for the next cycle, when the BRAM data
403 -- will be available on the cache_out output of the corresponding way
404 --
405 if req_is_hit = '1' then
406 r.hit_way <= req_hit_way;
407 r.hit_nia <= i_in.nia;
408 r.hit_smark <= i_in.stop_mark;
409 r.hit_valid <= '1';
410
411 report "cache hit nia:" & to_hstring(i_in.nia) &
412 " SM:" & std_ulogic'image(i_in.stop_mark) &
413 " idx:" & integer'image(req_index) &
414 " tag:" & to_hstring(req_tag) &
415 " way: " & integer'image(req_hit_way);
416 else
417 r.hit_valid <= '0';
418
419 -- Send stop marks down regardless of validity
420 r.hit_smark <= i_in.stop_mark;
421 end if;
422 end if;
423 end process;
424
425 -- Cache miss/reload synchronous machine
426 icache_miss : process(clk)
427 variable tagset : cache_tags_set_t;
428 begin
429 if rising_edge(clk) then
430 -- On reset, clear all valid bits to force misses
431 if rst = '1' then
432 for i in index_t loop
433 cache_valids(i) <= (others => '0');
434 end loop;
435 r.state <= IDLE;
436 r.wb.cyc <= '0';
437 r.wb.stb <= '0';
438
439 -- We only ever do reads on wishbone
440 r.wb.dat <= (others => '0');
441 r.wb.sel <= "11111111";
442 r.wb.we <= '0';
443
444 -- Not useful normally but helps avoiding tons of sim warnings
445 r.wb.adr <= (others => '0');
446 else
447 -- Main state machine
448 case r.state is
449 when IDLE =>
450 -- We need to read a cache line
451 if req_is_miss = '1' then
452 report "cache miss nia:" & to_hstring(i_in.nia) &
453 " SM:" & std_ulogic'image(i_in.stop_mark) &
454 " idx:" & integer'image(req_index) &
455 " way:" & integer'image(replace_way) &
456 " tag:" & to_hstring(req_tag);
457
458 -- Force misses on that way while reloading that line
459 cache_valids(req_index)(replace_way) <= '0';
460
461 -- Store new tag in selected way
462 for i in 0 to NUM_WAYS-1 loop
463 if i = replace_way then
464 tagset := cache_tags(req_index);
465 write_tag(i, tagset, req_tag);
466 cache_tags(req_index) <= tagset;
467 end if;
468 end loop;
469
470 -- Keep track of our index and way for subsequent stores
471 r.store_index <= req_index;
472 r.store_way <= replace_way;
473
474 -- Prep for first wishbone read. We calculate the address of
475 -- the start of the cache line
476 --
477 r.wb.adr <= i_in.nia(63 downto LINE_OFF_BITS) &
478 (LINE_OFF_BITS-1 downto 0 => '0');
479 r.wb.cyc <= '1';
480 r.wb.stb <= '1';
481
482 r.state <= WAIT_ACK;
483 end if;
484 when WAIT_ACK =>
485 if wishbone_in.ack = '1' then
486 -- That was the last word ? We are done
487 if is_last_row(r.wb.adr) then
488 cache_valids(r.store_index)(r.store_way) <= '1';
489 r.wb.cyc <= '0';
490 r.wb.stb <= '0';
491 r.state <= IDLE;
492 else
493 -- Otherwise, calculate the next row address
494 r.wb.adr <= next_row_addr(r.wb.adr);
495 end if;
496 end if;
497 end case;
498 end if;
499 end if;
500 end process;
501 end;