Merge pull request #81 from antonblanchard/logical
[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
182 -- Return the cache line index (tag index) for an address
183 function get_index(addr: std_ulogic_vector(63 downto 0)) return index_t is
184 begin
185 return to_integer(unsigned(addr(63-TAG_BITS downto LINE_OFF_BITS)));
186 end;
187
188 -- Return the cache row index (data memory) for an address
189 function get_row(addr: std_ulogic_vector(63 downto 0)) return row_t is
190 begin
191 return to_integer(unsigned(addr(63-TAG_BITS downto ROW_OFF_BITS)));
192 end;
193
194 -- Returns whether this is the last row of a line
195 function is_last_row(addr: std_ulogic_vector(63 downto 0)) return boolean is
196 constant ones : std_ulogic_vector(ROW_LINEBITS-1 downto 0) := (others => '1');
197 begin
198 return addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS) = ones;
199 end;
200
201 -- Return the address of the next row in the current cache line
202 function next_row_addr(addr: std_ulogic_vector(63 downto 0)) return std_ulogic_vector is
203 variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
204 variable result : std_ulogic_vector(63 downto 0);
205 begin
206 -- Is there no simpler way in VHDL to generate that 3 bits adder ?
207 row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
208 row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
209 result := addr;
210 result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
211 return result;
212 end;
213
214 -- Read the instruction word for the given address in the current cache row
215 function read_insn_word(addr: std_ulogic_vector(63 downto 0);
216 data: cache_row_t) return std_ulogic_vector is
217 variable word: integer range 0 to INSN_PER_ROW-1;
218 begin
219 word := to_integer(unsigned(addr(INSN_BITS+2-1 downto 2)));
220 return data(31+word*32 downto word*32);
221 end;
222
223 -- Get the tag value from the address
224 function get_tag(addr: std_ulogic_vector(63 downto 0)) return cache_tag_t is
225 begin
226 return addr(63 downto 64-TAG_BITS);
227 end;
228
229 -- Read a tag from a tag memory row
230 function read_tag(way: way_t; tagset: cache_tags_set_t) return cache_tag_t is
231 begin
232 return tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS);
233 end;
234
235 -- Write a tag to tag memory row
236 procedure write_tag(way: in way_t; tagset: inout cache_tags_set_t;
237 tag: cache_tag_t) is
238 begin
239 tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS) := tag;
240 end;
241
242 begin
243
244 assert LINE_SIZE mod ROW_SIZE = 0;
245 assert ispow2(LINE_SIZE) report "LINE_SIZE not power of 2" severity FAILURE;
246 assert ispow2(NUM_LINES) report "NUM_LINES not power of 2" severity FAILURE;
247 assert ispow2(ROW_PER_LINE) report "ROW_PER_LINE not power of 2" severity FAILURE;
248 assert ispow2(INSN_PER_ROW) report "INSN_PER_ROW not power of 2" severity FAILURE;
249 assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
250 report "geometry bits don't add up" severity FAILURE;
251 assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
252 report "geometry bits don't add up" severity FAILURE;
253 assert (64 = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
254 report "geometry bits don't add up" severity FAILURE;
255 assert (64 = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
256 report "geometry bits don't add up" severity FAILURE;
257
258 debug: process
259 begin
260 report "ROW_SIZE = " & natural'image(ROW_SIZE);
261 report "ROW_PER_LINE = " & natural'image(ROW_PER_LINE);
262 report "BRAM_ROWS = " & natural'image(BRAM_ROWS);
263 report "INSN_PER_ROW = " & natural'image(INSN_PER_ROW);
264 report "INSN_BITS = " & natural'image(INSN_BITS);
265 report "ROW_BITS = " & natural'image(ROW_BITS);
266 report "ROW_LINEBITS = " & natural'image(ROW_LINEBITS);
267 report "LINE_OFF_BITS = " & natural'image(LINE_OFF_BITS);
268 report "ROW_OFF_BITS = " & natural'image(ROW_OFF_BITS);
269 report "INDEX_BITS = " & natural'image(INDEX_BITS);
270 report "TAG_BITS = " & natural'image(TAG_BITS);
271 report "WAY_BITS = " & natural'image(WAY_BITS);
272 wait;
273 end process;
274
275 -- Generate a cache RAM for each way
276 rams: for i in 0 to NUM_WAYS-1 generate
277 signal do_write : std_ulogic;
278 signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
279 signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
280 signal dout : cache_row_t;
281 begin
282 way: entity work.cache_ram
283 generic map (
284 ROW_BITS => ROW_BITS,
285 WIDTH => wishbone_data_bits
286 )
287 port map (
288 clk => clk,
289 rd_en => '1', -- fixme
290 rd_addr => rd_addr,
291 rd_data => dout,
292 wr_en => do_write,
293 wr_addr => wr_addr,
294 wr_data => wishbone_in.dat
295 );
296 process(all)
297 begin
298 do_write <= '0';
299 if wishbone_in.ack = '1' and r.store_way = i then
300 do_write <= '1';
301 end if;
302 cache_out(i) <= dout;
303 rd_addr <= std_ulogic_vector(to_unsigned(req_row, ROW_BITS));
304 wr_addr <= std_ulogic_vector(to_unsigned(get_row(r.wb.adr), ROW_BITS));
305 end process;
306 end generate;
307
308 -- Generate PLRUs
309 maybe_plrus: if NUM_WAYS > 1 generate
310 begin
311 plrus: for i in 0 to NUM_LINES-1 generate
312 -- PLRU interface
313 signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
314 signal plru_acc_en : std_ulogic;
315 signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
316
317 begin
318 plru : entity work.plru
319 generic map (
320 BITS => WAY_BITS
321 )
322 port map (
323 clk => clk,
324 rst => rst,
325 acc => plru_acc,
326 acc_en => plru_acc_en,
327 lru => plru_out
328 );
329
330 process(req_index, req_is_hit, req_hit_way, req_is_hit, plru_out)
331 begin
332 -- PLRU interface
333 if req_is_hit = '1' and req_index = i then
334 plru_acc_en <= req_is_hit;
335 else
336 plru_acc_en <= '0';
337 end if;
338 plru_acc <= std_ulogic_vector(to_unsigned(req_hit_way, WAY_BITS));
339 plru_victim(i) <= plru_out;
340 end process;
341 end generate;
342 end generate;
343
344 -- Cache hit detection, output to fetch2 and other misc logic
345 icache_comb : process(all)
346 variable is_hit : std_ulogic;
347 variable hit_way : way_t;
348 begin
349 -- Extract line, row and tag from request
350 req_index <= get_index(i_in.nia);
351 req_row <= get_row(i_in.nia);
352 req_tag <= get_tag(i_in.nia);
353
354 -- Test if pending request is a hit on any way
355 hit_way := 0;
356 is_hit := '0';
357 for i in way_t loop
358 if read_tag(i, cache_tags(req_index)) = req_tag and
359 cache_valids(req_index)(i) = '1' then
360 hit_way := i;
361 is_hit := '1';
362 end if;
363 end loop;
364
365 -- Generate the "hit" and "miss" signals for the synchronous blocks
366 req_is_hit <= i_in.req and is_hit and not flush_in;
367 req_is_miss <= i_in.req and not is_hit and not flush_in;
368 req_hit_way <= hit_way;
369
370 -- Output instruction from current cache row
371 --
372 -- Note: This is a mild violation of our design principle of having pipeline
373 -- stages output from a clean latch. In this case we output the result
374 -- of a mux. The alternative would be output an entire row which
375 -- I prefer not to do just yet as it would force fetch2 to know about
376 -- some of the cache geometry information.
377 --
378 i_out.insn <= read_insn_word(r.hit_nia, cache_out(r.hit_way));
379 i_out.valid <= r.hit_valid;
380 i_out.nia <= r.hit_nia;
381 i_out.stop_mark <= r.hit_smark;
382
383 -- Stall fetch1 if we have a miss
384 stall_out <= not is_hit;
385
386 -- Wishbone requests output (from the cache miss reload machine)
387 wishbone_out <= r.wb;
388 end process;
389
390 -- Cache hit synchronous machine
391 icache_hit : process(clk)
392 begin
393 if rising_edge(clk) then
394 -- On a hit, latch the request for the next cycle, when the BRAM data
395 -- will be available on the cache_out output of the corresponding way
396 --
397 if req_is_hit = '1' then
398 r.hit_way <= req_hit_way;
399 r.hit_nia <= i_in.nia;
400 r.hit_smark <= i_in.stop_mark;
401 r.hit_valid <= '1';
402
403 report "cache hit nia:" & to_hstring(i_in.nia) &
404 " SM:" & std_ulogic'image(i_in.stop_mark) &
405 " idx:" & integer'image(req_index) &
406 " tag:" & to_hstring(req_tag) &
407 " way: " & integer'image(req_hit_way);
408 else
409 r.hit_valid <= '0';
410
411 -- Send stop marks down regardless of validity
412 r.hit_smark <= i_in.stop_mark;
413 end if;
414 end if;
415 end process;
416
417 -- Cache miss/reload synchronous machine
418 icache_miss : process(clk)
419 variable way : integer range 0 to NUM_WAYS-1;
420 variable tagset : cache_tags_set_t;
421 begin
422 if rising_edge(clk) then
423 -- On reset, clear all valid bits to force misses
424 if rst = '1' then
425 for i in index_t loop
426 cache_valids(i) <= (others => '0');
427 end loop;
428 r.state <= IDLE;
429 r.wb.cyc <= '0';
430 r.wb.stb <= '0';
431
432 -- We only ever do reads on wishbone
433 r.wb.dat <= (others => '0');
434 r.wb.sel <= "11111111";
435 r.wb.we <= '0';
436 else
437 -- Main state machine
438 case r.state is
439 when IDLE =>
440 -- We need to read a cache line
441 if req_is_miss = '1' then
442 way := to_integer(unsigned(plru_victim(req_index)));
443
444 report "cache miss nia:" & to_hstring(i_in.nia) &
445 " SM:" & std_ulogic'image(i_in.stop_mark) &
446 " idx:" & integer'image(req_index) &
447 " way:" & integer'image(way) &
448 " tag:" & to_hstring(req_tag);
449
450 -- Force misses on that way while reloading that line
451 cache_valids(req_index)(way) <= '0';
452
453 -- Store new tag in selected way
454 for i in 0 to NUM_WAYS-1 loop
455 if i = way then
456 tagset := cache_tags(req_index);
457 write_tag(i, tagset, req_tag);
458 cache_tags(req_index) <= tagset;
459 end if;
460 end loop;
461
462 -- Keep track of our index and way for subsequent stores
463 r.store_index <= req_index;
464 r.store_way <= way;
465
466 -- Prep for first wishbone read. We calculate the address of
467 -- the start of the cache line
468 --
469 r.wb.adr <= i_in.nia(63 downto LINE_OFF_BITS) &
470 (LINE_OFF_BITS-1 downto 0 => '0');
471 r.wb.cyc <= '1';
472 r.wb.stb <= '1';
473
474 r.state <= WAIT_ACK;
475 end if;
476 when WAIT_ACK =>
477 if wishbone_in.ack = '1' then
478 -- That was the last word ? We are done
479 if is_last_row(r.wb.adr) then
480 cache_valids(r.store_index)(way) <= '1';
481 r.wb.cyc <= '0';
482 r.wb.stb <= '0';
483 r.state <= IDLE;
484 else
485 -- Otherwise, calculate the next row address
486 r.wb.adr <= next_row_addr(r.wb.adr);
487 end if;
488 end if;
489 end case;
490 end if;
491 end if;
492 end process;
493 end;