Implement data storage interrupts
[microwatt.git] / dcache.vhdl
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 );
36 port (
37 clk : in std_ulogic;
38 rst : in std_ulogic;
39
40 d_in : in Loadstore1ToDcacheType;
41 d_out : out DcacheToLoadstore1Type;
42
43 stall_out : out std_ulogic;
44
45 wishbone_out : out wishbone_master_out;
46 wishbone_in : in wishbone_slave_out
47 );
48 end entity dcache;
49
50 architecture rtl of dcache is
51 -- BRAM organisation: We never access more than wishbone_data_bits at
52 -- a time so to save resources we make the array only that wide, and
53 -- use consecutive indices for to make a cache "line"
54 --
55 -- ROW_SIZE is the width in bytes of the BRAM (based on WB, so 64-bits)
56 constant ROW_SIZE : natural := wishbone_data_bits / 8;
57 -- ROW_PER_LINE is the number of row (wishbone transactions) in a line
58 constant ROW_PER_LINE : natural := LINE_SIZE / ROW_SIZE;
59 -- BRAM_ROWS is the number of rows in BRAM needed to represent the full
60 -- dcache
61 constant BRAM_ROWS : natural := NUM_LINES * ROW_PER_LINE;
62
63 -- Bit fields counts in the address
64
65 -- REAL_ADDR_BITS is the number of real address bits that we store
66 constant REAL_ADDR_BITS : positive := 56;
67 -- ROW_BITS is the number of bits to select a row
68 constant ROW_BITS : natural := log2(BRAM_ROWS);
69 -- ROW_LINEBITS is the number of bits to select a row within a line
70 constant ROW_LINEBITS : natural := log2(ROW_PER_LINE);
71 -- LINE_OFF_BITS is the number of bits for the offset in a cache line
72 constant LINE_OFF_BITS : natural := log2(LINE_SIZE);
73 -- ROW_OFF_BITS is the number of bits for the offset in a row
74 constant ROW_OFF_BITS : natural := log2(ROW_SIZE);
75 -- INDEX_BITS is the number if bits to select a cache line
76 constant INDEX_BITS : natural := log2(NUM_LINES);
77 -- SET_SIZE_BITS is the log base 2 of the set size
78 constant SET_SIZE_BITS : natural := LINE_OFF_BITS + INDEX_BITS;
79 -- TAG_BITS is the number of bits of the tag part of the address
80 constant TAG_BITS : natural := REAL_ADDR_BITS - SET_SIZE_BITS;
81 -- WAY_BITS is the number of bits to select a way
82 constant WAY_BITS : natural := log2(NUM_WAYS);
83
84 -- Example of layout for 32 lines of 64 bytes:
85 --
86 -- .. tag |index| line |
87 -- .. | row | |
88 -- .. | |---| | ROW_LINEBITS (3)
89 -- .. | |--- - --| LINE_OFF_BITS (6)
90 -- .. | |- --| ROW_OFF_BITS (3)
91 -- .. |----- ---| | ROW_BITS (8)
92 -- .. |-----| | INDEX_BITS (5)
93 -- .. --------| | TAG_BITS (45)
94
95 subtype row_t is integer range 0 to BRAM_ROWS-1;
96 subtype index_t is integer range 0 to NUM_LINES-1;
97 subtype way_t is integer range 0 to NUM_WAYS-1;
98
99 -- The cache data BRAM organized as described above for each way
100 subtype cache_row_t is std_ulogic_vector(wishbone_data_bits-1 downto 0);
101
102 -- The cache tags LUTRAM has a row per set. Vivado is a pain and will
103 -- not handle a clean (commented) definition of the cache tags as a 3d
104 -- memory. For now, work around it by putting all the tags
105 subtype cache_tag_t is std_logic_vector(TAG_BITS-1 downto 0);
106 -- type cache_tags_set_t is array(way_t) of cache_tag_t;
107 -- type cache_tags_array_t is array(index_t) of cache_tags_set_t;
108 constant TAG_RAM_WIDTH : natural := TAG_BITS * NUM_WAYS;
109 subtype cache_tags_set_t is std_logic_vector(TAG_RAM_WIDTH-1 downto 0);
110 type cache_tags_array_t is array(index_t) of cache_tags_set_t;
111
112 -- The cache valid bits
113 subtype cache_way_valids_t is std_ulogic_vector(NUM_WAYS-1 downto 0);
114 type cache_valids_t is array(index_t) of cache_way_valids_t;
115
116 -- Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
117 signal cache_tags : cache_tags_array_t;
118 signal cache_valids : cache_valids_t;
119
120 attribute ram_style : string;
121 attribute ram_style of cache_tags : signal is "distributed";
122
123 -- L1 TLB.
124 constant TLB_SET_BITS : natural := log2(TLB_SET_SIZE);
125 constant TLB_WAY_BITS : natural := log2(TLB_NUM_WAYS);
126 constant TLB_EA_TAG_BITS : natural := 64 - (TLB_LG_PGSZ + TLB_SET_BITS);
127 constant TLB_TAG_WAY_BITS : natural := TLB_NUM_WAYS * TLB_EA_TAG_BITS;
128 constant TLB_PTE_BITS : natural := 64;
129 constant TLB_PTE_WAY_BITS : natural := TLB_NUM_WAYS * TLB_PTE_BITS;
130
131 subtype tlb_way_t is integer range 0 to TLB_NUM_WAYS - 1;
132 subtype tlb_index_t is integer range 0 to TLB_SET_SIZE - 1;
133 subtype tlb_way_valids_t is std_ulogic_vector(TLB_NUM_WAYS-1 downto 0);
134 type tlb_valids_t is array(tlb_index_t) of tlb_way_valids_t;
135 subtype tlb_tag_t is std_ulogic_vector(TLB_EA_TAG_BITS - 1 downto 0);
136 subtype tlb_way_tags_t is std_ulogic_vector(TLB_TAG_WAY_BITS-1 downto 0);
137 type tlb_tags_t is array(tlb_index_t) of tlb_way_tags_t;
138 subtype tlb_pte_t is std_ulogic_vector(TLB_PTE_BITS - 1 downto 0);
139 subtype tlb_way_ptes_t is std_ulogic_vector(TLB_PTE_WAY_BITS-1 downto 0);
140 type tlb_ptes_t is array(tlb_index_t) of tlb_way_ptes_t;
141 type hit_way_set_t is array(tlb_way_t) of way_t;
142
143 signal dtlb_valids : tlb_valids_t;
144 signal dtlb_tags : tlb_tags_t;
145 signal dtlb_ptes : tlb_ptes_t;
146 attribute ram_style of dtlb_tags : signal is "distributed";
147 attribute ram_style of dtlb_ptes : signal is "distributed";
148
149 signal r0 : Loadstore1ToDcacheType;
150 signal r0_valid : std_ulogic;
151
152 -- Type of operation on a "valid" input
153 type op_t is (OP_NONE,
154 OP_LOAD_HIT, -- Cache hit on load
155 OP_LOAD_MISS, -- Load missing cache
156 OP_LOAD_NC, -- Non-cachable load
157 OP_BAD, -- BAD: Cache hit on NC load/store
158 OP_STORE_HIT, -- Store hitting cache
159 OP_STORE_MISS); -- Store missing cache
160
161 -- Cache state machine
162 type state_t is (IDLE, -- Normal load hit processing
163 RELOAD_WAIT_ACK, -- Cache reload wait ack
164 FINISH_LD_MISS, -- Extra cycle after load miss
165 STORE_WAIT_ACK, -- Store wait ack
166 NC_LOAD_WAIT_ACK);-- Non-cachable load wait ack
167
168
169 --
170 -- Dcache operations:
171 --
172 -- In order to make timing, we use the BRAMs with an output buffer,
173 -- which means that the BRAM output is delayed by an extra cycle.
174 --
175 -- Thus, the dcache has a 2-stage internal pipeline for cache hits
176 -- with no stalls.
177 --
178 -- All other operations are handled via stalling in the first stage.
179 --
180 -- The second stage can thus complete a hit at the same time as the
181 -- first stage emits a stall for a complex op.
182 --
183
184 -- First stage register, contains state for stage 1 of load hits
185 -- and for the state machine used by all other operations
186 --
187 type reg_stage_1_t is record
188 -- Latch the complete request from ls1
189 req : Loadstore1ToDcacheType;
190
191 -- Cache hit state
192 hit_way : way_t;
193 hit_load_valid : std_ulogic;
194
195 -- Data buffer for "slow" read ops (load miss and NC loads).
196 slow_data : std_ulogic_vector(63 downto 0);
197 slow_valid : std_ulogic;
198
199 -- Signal to complete a failed stcx.
200 stcx_fail : std_ulogic;
201
202 -- Cache miss state (reload state machine)
203 state : state_t;
204 wb : wishbone_master_out;
205 store_way : way_t;
206 store_row : row_t;
207 store_index : index_t;
208
209 -- Signals to complete with error
210 error_done : std_ulogic;
211 tlb_miss : std_ulogic;
212
213 -- completion signal for tlbie
214 tlbie_done : std_ulogic;
215 end record;
216
217 signal r1 : reg_stage_1_t;
218
219 -- Reservation information
220 --
221 type reservation_t is record
222 valid : std_ulogic;
223 addr : std_ulogic_vector(63 downto LINE_OFF_BITS);
224 end record;
225
226 signal reservation : reservation_t;
227
228 -- Async signals on incoming request
229 signal req_index : index_t;
230 signal req_row : row_t;
231 signal req_hit_way : way_t;
232 signal req_tag : cache_tag_t;
233 signal req_op : op_t;
234 signal req_data : std_ulogic_vector(63 downto 0);
235 signal req_laddr : std_ulogic_vector(63 downto 0);
236
237 signal early_req_row : row_t;
238
239 signal cancel_store : std_ulogic;
240 signal set_rsrv : std_ulogic;
241 signal clear_rsrv : std_ulogic;
242
243 -- Cache RAM interface
244 type cache_ram_out_t is array(way_t) of cache_row_t;
245 signal cache_out : cache_ram_out_t;
246
247 -- PLRU output interface
248 type plru_out_t is array(index_t) of std_ulogic_vector(WAY_BITS-1 downto 0);
249 signal plru_victim : plru_out_t;
250 signal replace_way : way_t;
251
252 -- Wishbone read/write/cache write formatting signals
253 signal bus_sel : std_ulogic_vector(7 downto 0);
254
255 -- TLB signals
256 signal tlb_tag_way : tlb_way_tags_t;
257 signal tlb_pte_way : tlb_way_ptes_t;
258 signal tlb_valid_way : tlb_way_valids_t;
259 signal tlb_req_index : tlb_index_t;
260 signal tlb_hit : std_ulogic;
261 signal tlb_hit_way : tlb_way_t;
262 signal pte : tlb_pte_t;
263 signal ra : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
264 signal valid_ra : std_ulogic;
265
266 -- TLB PLRU output interface
267 type tlb_plru_out_t is array(tlb_index_t) of std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
268 signal tlb_plru_victim : tlb_plru_out_t;
269
270 --
271 -- Helper functions to decode incoming requests
272 --
273
274 -- Return the cache line index (tag index) for an address
275 function get_index(addr: std_ulogic_vector(63 downto 0)) return index_t is
276 begin
277 return to_integer(unsigned(addr(SET_SIZE_BITS - 1 downto LINE_OFF_BITS)));
278 end;
279
280 -- Return the cache row index (data memory) for an address
281 function get_row(addr: std_ulogic_vector(63 downto 0)) return row_t is
282 begin
283 return to_integer(unsigned(addr(SET_SIZE_BITS - 1 downto ROW_OFF_BITS)));
284 end;
285
286 -- Returns whether this is the last row of a line
287 function is_last_row_addr(addr: wishbone_addr_type) return boolean is
288 constant ones : std_ulogic_vector(ROW_LINEBITS-1 downto 0) := (others => '1');
289 begin
290 return addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS) = ones;
291 end;
292
293 -- Returns whether this is the last row of a line
294 function is_last_row(row: row_t) return boolean is
295 variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
296 constant ones : std_ulogic_vector(ROW_LINEBITS-1 downto 0) := (others => '1');
297 begin
298 row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
299 return row_v(ROW_LINEBITS-1 downto 0) = ones;
300 end;
301
302 -- Return the address of the next row in the current cache line
303 function next_row_addr(addr: wishbone_addr_type) return std_ulogic_vector is
304 variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
305 variable result : wishbone_addr_type;
306 begin
307 -- Is there no simpler way in VHDL to generate that 3 bits adder ?
308 row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
309 row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
310 result := addr;
311 result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
312 return result;
313 end;
314
315 -- Return the next row in the current cache line. We use a dedicated
316 -- function in order to limit the size of the generated adder to be
317 -- only the bits within a cache line (3 bits with default settings)
318 --
319 function next_row(row: row_t) return row_t is
320 variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
321 variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
322 variable result : std_ulogic_vector(ROW_BITS-1 downto 0);
323 begin
324 row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
325 row_idx := row_v(ROW_LINEBITS-1 downto 0);
326 row_v(ROW_LINEBITS-1 downto 0) := std_ulogic_vector(unsigned(row_idx) + 1);
327 return to_integer(unsigned(row_v));
328 end;
329
330 -- Get the tag value from the address
331 function get_tag(addr: std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0)) return cache_tag_t is
332 begin
333 return addr(REAL_ADDR_BITS - 1 downto SET_SIZE_BITS);
334 end;
335
336 -- Read a tag from a tag memory row
337 function read_tag(way: way_t; tagset: cache_tags_set_t) return cache_tag_t is
338 begin
339 return tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS);
340 end;
341
342 -- Write a tag to tag memory row
343 procedure write_tag(way: in way_t; tagset: inout cache_tags_set_t;
344 tag: cache_tag_t) is
345 begin
346 tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS) := tag;
347 end;
348
349 -- Read a TLB tag from a TLB tag memory row
350 function read_tlb_tag(way: tlb_way_t; tags: tlb_way_tags_t) return tlb_tag_t is
351 variable j : integer;
352 begin
353 j := way * TLB_EA_TAG_BITS;
354 return tags(j + TLB_EA_TAG_BITS - 1 downto j);
355 end;
356
357 -- Write a TLB tag to a TLB tag memory row
358 procedure write_tlb_tag(way: tlb_way_t; tags: inout tlb_way_tags_t;
359 tag: tlb_tag_t) is
360 variable j : integer;
361 begin
362 j := way * TLB_EA_TAG_BITS;
363 tags(j + TLB_EA_TAG_BITS - 1 downto j) := tag;
364 end;
365
366 -- Read a PTE from a TLB PTE memory row
367 function read_tlb_pte(way: tlb_way_t; ptes: tlb_way_ptes_t) return tlb_pte_t is
368 variable j : integer;
369 begin
370 j := way * TLB_PTE_BITS;
371 return ptes(j + TLB_PTE_BITS - 1 downto j);
372 end;
373
374 procedure write_tlb_pte(way: tlb_way_t; ptes: inout tlb_way_ptes_t; newpte: tlb_pte_t) is
375 variable j : integer;
376 begin
377 j := way * TLB_PTE_BITS;
378 ptes(j + TLB_PTE_BITS - 1 downto j) := newpte;
379 end;
380
381 begin
382
383 assert LINE_SIZE mod ROW_SIZE = 0 report "LINE_SIZE not multiple of ROW_SIZE" severity FAILURE;
384 assert ispow2(LINE_SIZE) report "LINE_SIZE not power of 2" severity FAILURE;
385 assert ispow2(NUM_LINES) report "NUM_LINES not power of 2" severity FAILURE;
386 assert ispow2(ROW_PER_LINE) report "ROW_PER_LINE not power of 2" severity FAILURE;
387 assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
388 report "geometry bits don't add up" severity FAILURE;
389 assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
390 report "geometry bits don't add up" severity FAILURE;
391 assert (REAL_ADDR_BITS = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
392 report "geometry bits don't add up" severity FAILURE;
393 assert (REAL_ADDR_BITS = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
394 report "geometry bits don't add up" severity FAILURE;
395 assert (64 = wishbone_data_bits)
396 report "Can't yet handle a wishbone width that isn't 64-bits" severity FAILURE;
397
398 -- Latch the request in r0 as long as we're not stalling
399 stage_0 : process(clk)
400 begin
401 if rising_edge(clk) then
402 if rst = '1' then
403 r0.valid <= '0';
404 elsif stall_out = '0' then
405 r0 <= d_in;
406 end if;
407 end if;
408 end process;
409
410 -- Hold off the request in r0 when stalling,
411 -- and cancel it if we get an error in a previous request.
412 r0_valid <= r0.valid and not stall_out and not r1.error_done;
413
414 -- TLB
415 -- Operates in the second cycle on the request latched in r0.
416 -- TLB updates write the entry at the end of the second cycle.
417 tlb_read : process(clk)
418 variable index : tlb_index_t;
419 begin
420 if rising_edge(clk) then
421 if stall_out = '1' then
422 -- keep reading the same thing while stalled
423 index := tlb_req_index;
424 else
425 index := to_integer(unsigned(d_in.addr(TLB_LG_PGSZ + TLB_SET_BITS - 1
426 downto TLB_LG_PGSZ)));
427 end if;
428 tlb_valid_way <= dtlb_valids(index);
429 tlb_tag_way <= dtlb_tags(index);
430 tlb_pte_way <= dtlb_ptes(index);
431 end if;
432 end process;
433
434 -- Generate TLB PLRUs
435 maybe_tlb_plrus: if TLB_NUM_WAYS > 1 generate
436 begin
437 tlb_plrus: for i in 0 to TLB_SET_SIZE - 1 generate
438 -- TLB PLRU interface
439 signal tlb_plru_acc : std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
440 signal tlb_plru_acc_en : std_ulogic;
441 signal tlb_plru_out : std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
442 begin
443 tlb_plru : entity work.plru
444 generic map (
445 BITS => TLB_WAY_BITS
446 )
447 port map (
448 clk => clk,
449 rst => rst,
450 acc => tlb_plru_acc,
451 acc_en => tlb_plru_acc_en,
452 lru => tlb_plru_out
453 );
454
455 process(tlb_req_index, tlb_hit, tlb_hit_way, tlb_plru_out)
456 begin
457 -- PLRU interface
458 if tlb_hit = '1' and tlb_req_index = i then
459 tlb_plru_acc_en <= '1';
460 else
461 tlb_plru_acc_en <= '0';
462 end if;
463 tlb_plru_acc <= std_ulogic_vector(to_unsigned(tlb_hit_way, TLB_WAY_BITS));
464 tlb_plru_victim(i) <= tlb_plru_out;
465 end process;
466 end generate;
467 end generate;
468
469 tlb_search : process(all)
470 variable hitway : tlb_way_t;
471 variable hit : std_ulogic;
472 variable eatag : tlb_tag_t;
473 begin
474 tlb_req_index <= to_integer(unsigned(r0.addr(TLB_LG_PGSZ + TLB_SET_BITS - 1
475 downto TLB_LG_PGSZ)));
476 hitway := 0;
477 hit := '0';
478 eatag := r0.addr(63 downto TLB_LG_PGSZ + TLB_SET_BITS);
479 for i in tlb_way_t loop
480 if tlb_valid_way(i) = '1' and
481 read_tlb_tag(i, tlb_tag_way) = eatag then
482 hitway := i;
483 hit := '1';
484 end if;
485 end loop;
486 tlb_hit <= hit and r0_valid;
487 tlb_hit_way <= hitway;
488 pte <= read_tlb_pte(hitway, tlb_pte_way);
489 valid_ra <= tlb_hit or not r0.virt_mode;
490 if r0.virt_mode = '1' then
491 ra <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
492 r0.addr(TLB_LG_PGSZ - 1 downto 0);
493 else
494 ra <= r0.addr(REAL_ADDR_BITS - 1 downto 0);
495 end if;
496 end process;
497
498 tlb_update : process(clk)
499 variable tlbie : std_ulogic;
500 variable tlbia : std_ulogic;
501 variable tlbwe : std_ulogic;
502 variable repl_way : tlb_way_t;
503 variable eatag : tlb_tag_t;
504 variable tagset : tlb_way_tags_t;
505 variable pteset : tlb_way_ptes_t;
506 begin
507 if rising_edge(clk) then
508 tlbie := '0';
509 tlbia := '0';
510 tlbwe := '0';
511 if r0_valid = '1' and r0.tlbie = '1' then
512 if r0.addr(11 downto 10) /= "00" then
513 tlbia := '1';
514 elsif r0.addr(9) = '1' then
515 tlbwe := '1';
516 else
517 tlbie := '1';
518 end if;
519 end if;
520 if rst = '1' or tlbia = '1' then
521 -- clear all valid bits at once
522 for i in tlb_index_t loop
523 dtlb_valids(i) <= (others => '0');
524 end loop;
525 elsif tlbie = '1' then
526 if tlb_hit = '1' then
527 dtlb_valids(tlb_req_index)(tlb_hit_way) <= '0';
528 end if;
529 elsif tlbwe = '1' then
530 if tlb_hit = '1' then
531 repl_way := tlb_hit_way;
532 else
533 repl_way := to_integer(unsigned(tlb_plru_victim(tlb_req_index)));
534 end if;
535 eatag := r0.addr(63 downto TLB_LG_PGSZ + TLB_SET_BITS);
536 tagset := tlb_tag_way;
537 write_tlb_tag(repl_way, tagset, eatag);
538 dtlb_tags(tlb_req_index) <= tagset;
539 pteset := tlb_pte_way;
540 write_tlb_pte(repl_way, pteset, r0.data);
541 dtlb_ptes(tlb_req_index) <= pteset;
542 dtlb_valids(tlb_req_index)(repl_way) <= '1';
543 end if;
544 end if;
545 end process;
546
547 -- Generate PLRUs
548 maybe_plrus: if NUM_WAYS > 1 generate
549 begin
550 plrus: for i in 0 to NUM_LINES-1 generate
551 -- PLRU interface
552 signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
553 signal plru_acc_en : std_ulogic;
554 signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
555
556 begin
557 plru : entity work.plru
558 generic map (
559 BITS => WAY_BITS
560 )
561 port map (
562 clk => clk,
563 rst => rst,
564 acc => plru_acc,
565 acc_en => plru_acc_en,
566 lru => plru_out
567 );
568
569 process(req_index, req_op, req_hit_way, plru_out)
570 begin
571 -- PLRU interface
572 if (req_op = OP_LOAD_HIT or
573 req_op = OP_STORE_HIT) and req_index = i then
574 plru_acc_en <= '1';
575 else
576 plru_acc_en <= '0';
577 end if;
578 plru_acc <= std_ulogic_vector(to_unsigned(req_hit_way, WAY_BITS));
579 plru_victim(i) <= plru_out;
580 end process;
581 end generate;
582 end generate;
583
584 -- Cache request parsing and hit detection
585 dcache_request : process(all)
586 variable is_hit : std_ulogic;
587 variable hit_way : way_t;
588 variable op : op_t;
589 variable opsel : std_ulogic_vector(2 downto 0);
590 variable go : std_ulogic;
591 variable s_hit : std_ulogic;
592 variable s_tag : cache_tag_t;
593 variable s_pte : tlb_pte_t;
594 variable s_ra : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
595 variable hit_set : std_ulogic_vector(TLB_NUM_WAYS - 1 downto 0);
596 variable hit_way_set : hit_way_set_t;
597 begin
598 -- Extract line, row and tag from request
599 req_index <= get_index(r0.addr);
600 req_row <= get_row(r0.addr);
601 req_tag <= get_tag(ra);
602
603 -- Only do anything if not being stalled by stage 1
604 go := r0_valid and not r0.tlbie;
605
606 -- Calculate address of beginning of cache line, will be
607 -- used for cache miss processing if needed
608 --
609 req_laddr <= (63 downto REAL_ADDR_BITS => '0') &
610 ra(REAL_ADDR_BITS - 1 downto LINE_OFF_BITS) &
611 (LINE_OFF_BITS-1 downto 0 => '0');
612
613 -- Test if pending request is a hit on any way
614 -- In order to make timing in virtual mode, when we are using the TLB,
615 -- we compare each way with each of the real addresses from each way of
616 -- the TLB, and then decide later which match to use.
617 hit_way := 0;
618 is_hit := '0';
619 if r0.virt_mode = '1' then
620 for j in tlb_way_t loop
621 hit_way_set(j) := 0;
622 s_hit := '0';
623 s_pte := read_tlb_pte(j, tlb_pte_way);
624 s_ra := s_pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
625 r0.addr(TLB_LG_PGSZ - 1 downto 0);
626 s_tag := get_tag(s_ra);
627 for i in way_t loop
628 if go = '1' and cache_valids(req_index)(i) = '1' and
629 read_tag(i, cache_tags(req_index)) = s_tag and
630 tlb_valid_way(j) = '1' then
631 hit_way_set(j) := i;
632 s_hit := '1';
633 end if;
634 end loop;
635 hit_set(j) := s_hit;
636 end loop;
637 if tlb_hit = '1' then
638 is_hit := hit_set(tlb_hit_way);
639 hit_way := hit_way_set(tlb_hit_way);
640 end if;
641 else
642 s_tag := get_tag(r0.addr(REAL_ADDR_BITS - 1 downto 0));
643 for i in way_t loop
644 if go = '1' and cache_valids(req_index)(i) = '1' and
645 read_tag(i, cache_tags(req_index)) = s_tag then
646 hit_way := i;
647 is_hit := '1';
648 end if;
649 end loop;
650 end if;
651
652 -- The way that matched on a hit
653 req_hit_way <= hit_way;
654
655 -- The way to replace on a miss
656 replace_way <= to_integer(unsigned(plru_victim(req_index)));
657
658 -- Combine the request and cache his status to decide what
659 -- operation needs to be done
660 --
661 op := OP_NONE;
662 if go = '1' then
663 if valid_ra = '1' then
664 opsel := r0.load & r0.nc & is_hit;
665 case opsel is
666 when "101" => op := OP_LOAD_HIT;
667 when "100" => op := OP_LOAD_MISS;
668 when "110" => op := OP_LOAD_NC;
669 when "001" => op := OP_STORE_HIT;
670 when "000" => op := OP_STORE_MISS;
671 when "010" => op := OP_STORE_MISS;
672 when "011" => op := OP_BAD;
673 when "111" => op := OP_BAD;
674 when others => op := OP_NONE;
675 end case;
676 else
677 op := OP_BAD;
678 end if;
679 end if;
680 req_op <= op;
681
682 -- Version of the row number that is valid one cycle earlier
683 -- in the cases where we need to read the cache data BRAM.
684 -- If we're stalling then we need to keep reading the last
685 -- row requested.
686 if stall_out = '0' then
687 early_req_row <= get_row(d_in.addr);
688 else
689 early_req_row <= req_row;
690 end if;
691 end process;
692
693 -- Wire up wishbone request latch out of stage 1
694 wishbone_out <= r1.wb;
695
696 -- Generate stalls from stage 1 state machine
697 stall_out <= '1' when r1.state /= IDLE else '0';
698
699 -- Handle load-with-reservation and store-conditional instructions
700 reservation_comb: process(all)
701 begin
702 cancel_store <= '0';
703 set_rsrv <= '0';
704 clear_rsrv <= '0';
705 if r0_valid = '1' and r0.reserve = '1' then
706 -- XXX generate alignment interrupt if address is not aligned
707 -- XXX or if r0.nc = '1'
708 if r0.load = '1' then
709 -- load with reservation
710 set_rsrv <= '1';
711 else
712 -- store conditional
713 clear_rsrv <= '1';
714 if reservation.valid = '0' or
715 r0.addr(63 downto LINE_OFF_BITS) /= reservation.addr then
716 cancel_store <= '1';
717 end if;
718 end if;
719 end if;
720 end process;
721
722 reservation_reg: process(clk)
723 begin
724 if rising_edge(clk) then
725 if rst = '1' or clear_rsrv = '1' then
726 reservation.valid <= '0';
727 elsif set_rsrv = '1' then
728 reservation.valid <= '1';
729 reservation.addr <= r0.addr(63 downto LINE_OFF_BITS);
730 end if;
731 end if;
732 end process;
733
734 -- Return data for loads & completion control logic
735 --
736 writeback_control: process(all)
737 begin
738
739 -- The mux on d_out.data defaults to the normal load hit case.
740 d_out.valid <= '0';
741 d_out.data <= cache_out(r1.hit_way);
742 d_out.store_done <= '0';
743 d_out.error <= '0';
744 d_out.tlb_miss <= '0';
745
746 -- We have a valid load or store hit or we just completed a slow
747 -- op such as a load miss, a NC load or a store
748 --
749 -- Note: the load hit is delayed by one cycle. However it can still
750 -- not collide with r.slow_valid (well unless I miscalculated) because
751 -- slow_valid can only be set on a subsequent request and not on its
752 -- first cycle (the state machine must have advanced), which makes
753 -- slow_valid at least 2 cycles from the previous hit_load_valid.
754 --
755
756 -- Sanity: Only one of these must be set in any given cycle
757 assert (r1.slow_valid and r1.stcx_fail) /= '1' report
758 "unexpected slow_valid collision with stcx_fail"
759 severity FAILURE;
760 assert ((r1.slow_valid or r1.stcx_fail) and r1.hit_load_valid) /= '1' report
761 "unexpected hit_load_delayed collision with slow_valid"
762 severity FAILURE;
763
764 -- Load hit case is the standard path
765 if r1.hit_load_valid = '1' then
766 report "completing load hit";
767 d_out.valid <= '1';
768 end if;
769
770 -- error cases complete without stalling
771 if r1.error_done = '1' then
772 report "completing ld/st with error";
773 d_out.error <= '1';
774 d_out.tlb_miss <= r1.tlb_miss;
775 d_out.valid <= '1';
776 end if;
777
778 -- tlbie is handled above and doesn't go through the cache state machine
779 if r1.tlbie_done = '1' then
780 report "completing tlbie";
781 d_out.valid <= '1';
782 end if;
783
784 -- Slow ops (load miss, NC, stores)
785 if r1.slow_valid = '1' then
786 -- If it's a load, enable register writeback and switch
787 -- mux accordingly
788 --
789 if r1.req.load then
790 -- Read data comes from the slow data latch
791 d_out.data <= r1.slow_data;
792 end if;
793 d_out.store_done <= '1';
794
795 report "completing store or load miss";
796 d_out.valid <= '1';
797 end if;
798
799 if r1.stcx_fail = '1' then
800 d_out.store_done <= '0';
801 d_out.valid <= '1';
802 end if;
803
804 end process;
805
806 --
807 -- Generate a cache RAM for each way. This handles the normal
808 -- reads, writes from reloads and the special store-hit update
809 -- path as well.
810 --
811 -- Note: the BRAMs have an extra read buffer, meaning the output
812 -- is pipelined an extra cycle. This differs from the
813 -- icache. The writeback logic needs to take that into
814 -- account by using 1-cycle delayed signals for load hits.
815 --
816 rams: for i in 0 to NUM_WAYS-1 generate
817 signal do_read : std_ulogic;
818 signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
819 signal do_write : std_ulogic;
820 signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
821 signal wr_data : std_ulogic_vector(wishbone_data_bits-1 downto 0);
822 signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
823 signal dout : cache_row_t;
824 begin
825 way: entity work.cache_ram
826 generic map (
827 ROW_BITS => ROW_BITS,
828 WIDTH => wishbone_data_bits,
829 ADD_BUF => true
830 )
831 port map (
832 clk => clk,
833 rd_en => do_read,
834 rd_addr => rd_addr,
835 rd_data => dout,
836 wr_en => do_write,
837 wr_sel => wr_sel,
838 wr_addr => wr_addr,
839 wr_data => wr_data
840 );
841 process(all)
842 variable tmp_adr : std_ulogic_vector(63 downto 0);
843 variable reloading : boolean;
844 begin
845 -- Cache hit reads
846 do_read <= '1';
847 rd_addr <= std_ulogic_vector(to_unsigned(early_req_row, ROW_BITS));
848 cache_out(i) <= dout;
849
850 -- Write mux:
851 --
852 -- Defaults to wishbone read responses (cache refill),
853 --
854 -- For timing, the mux on wr_data/sel/addr is not dependent on anything
855 -- other than the current state. Only the do_write signal is.
856 --
857 if r1.state = IDLE then
858 -- In IDLE state, the only write path is the store-hit update case
859 wr_addr <= std_ulogic_vector(to_unsigned(req_row, ROW_BITS));
860 wr_data <= r0.data;
861 wr_sel <= r0.byte_sel;
862 else
863 -- Otherwise, we might be doing a reload or a DCBZ
864 if r1.req.dcbz = '1' then
865 wr_data <= (others => '0');
866 else
867 wr_data <= wishbone_in.dat;
868 end if;
869 wr_sel <= (others => '1');
870 wr_addr <= std_ulogic_vector(to_unsigned(r1.store_row, ROW_BITS));
871 end if;
872
873 -- The two actual write cases here
874 do_write <= '0';
875 reloading := r1.state = RELOAD_WAIT_ACK;
876 if reloading and wishbone_in.ack = '1' and r1.store_way = i then
877 do_write <= '1';
878 end if;
879 if req_op = OP_STORE_HIT and req_hit_way = i and cancel_store = '0' and
880 r1.req.dcbz = '0' then
881 assert not reloading report "Store hit while in state:" &
882 state_t'image(r1.state)
883 severity FAILURE;
884 do_write <= '1';
885 end if;
886 end process;
887 end generate;
888
889 --
890 -- Cache hit synchronous machine for the easy case. This handles load hits.
891 -- It also handles error cases (TLB miss, cache paradox)
892 --
893 dcache_fast_hit : process(clk)
894 begin
895 if rising_edge(clk) then
896 -- If we have a request incoming, we have to latch it as r0.valid
897 -- is only set for a single cycle. It's up to the control logic to
898 -- ensure we don't override an uncompleted request (for now we are
899 -- single issue on load/stores so we are fine, later, we can generate
900 -- a stall output if necessary).
901
902 if req_op /= OP_NONE and stall_out = '0' then
903 r1.req <= r0;
904 report "op:" & op_t'image(req_op) &
905 " addr:" & to_hstring(r0.addr) &
906 " nc:" & std_ulogic'image(r0.nc) &
907 " idx:" & integer'image(req_index) &
908 " tag:" & to_hstring(req_tag) &
909 " way: " & integer'image(req_hit_way);
910 end if;
911
912 -- Fast path for load/store hits. Set signals for the writeback controls.
913 if req_op = OP_LOAD_HIT then
914 r1.hit_way <= req_hit_way;
915 r1.hit_load_valid <= '1';
916 else
917 r1.hit_load_valid <= '0';
918 end if;
919
920 if req_op = OP_BAD then
921 r1.error_done <= '1';
922 r1.tlb_miss <= not valid_ra;
923 else
924 r1.error_done <= '0';
925 end if;
926
927 -- complete tlbies in the third cycle
928 r1.tlbie_done <= r0_valid and r0.tlbie;
929 end if;
930 end process;
931
932 --
933 -- Every other case is handled by this state machine:
934 --
935 -- * Cache load miss/reload (in conjunction with "rams")
936 -- * Load hits for non-cachable forms
937 -- * Stores (the collision case is handled in "rams")
938 --
939 -- All wishbone requests generation is done here. This machine
940 -- operates at stage 1.
941 --
942 dcache_slow : process(clk)
943 variable tagset : cache_tags_set_t;
944 variable stbs_done : boolean;
945 begin
946 if rising_edge(clk) then
947 -- On reset, clear all valid bits to force misses
948 if rst = '1' then
949 for i in index_t loop
950 cache_valids(i) <= (others => '0');
951 end loop;
952 r1.state <= IDLE;
953 r1.slow_valid <= '0';
954 r1.wb.cyc <= '0';
955 r1.wb.stb <= '0';
956
957 -- Not useful normally but helps avoiding tons of sim warnings
958 r1.wb.adr <= (others => '0');
959 else
960 -- One cycle pulses reset
961 r1.slow_valid <= '0';
962 r1.stcx_fail <= '0';
963
964 -- Main state machine
965 case r1.state is
966 when IDLE =>
967 case req_op is
968 when OP_LOAD_HIT =>
969 -- stay in IDLE state
970
971 when OP_LOAD_MISS =>
972 -- Normal load cache miss, start the reload machine
973 --
974 report "cache miss addr:" & to_hstring(r0.addr) &
975 " idx:" & integer'image(req_index) &
976 " way:" & integer'image(replace_way) &
977 " tag:" & to_hstring(req_tag);
978
979 -- Force misses on that way while reloading that line
980 cache_valids(req_index)(replace_way) <= '0';
981
982 -- Store new tag in selected way
983 for i in 0 to NUM_WAYS-1 loop
984 if i = replace_way then
985 tagset := cache_tags(req_index);
986 write_tag(i, tagset, req_tag);
987 cache_tags(req_index) <= tagset;
988 end if;
989 end loop;
990
991 -- Keep track of our index and way for subsequent stores.
992 r1.store_index <= req_index;
993 r1.store_way <= replace_way;
994 r1.store_row <= get_row(req_laddr);
995
996 -- Prep for first wishbone read. We calculate the address of
997 -- the start of the cache line and start the WB cycle
998 --
999 r1.wb.adr <= req_laddr(r1.wb.adr'left downto 0);
1000 r1.wb.sel <= (others => '1');
1001 r1.wb.we <= '0';
1002 r1.wb.cyc <= '1';
1003 r1.wb.stb <= '1';
1004
1005 -- Track that we had one request sent
1006 r1.state <= RELOAD_WAIT_ACK;
1007
1008 when OP_LOAD_NC =>
1009 r1.wb.sel <= r0.byte_sel;
1010 r1.wb.adr <= ra(r1.wb.adr'left downto 3) & "000";
1011 r1.wb.cyc <= '1';
1012 r1.wb.stb <= '1';
1013 r1.wb.we <= '0';
1014 r1.state <= NC_LOAD_WAIT_ACK;
1015
1016 when OP_STORE_HIT | OP_STORE_MISS =>
1017 if r0.dcbz = '0' then
1018 r1.wb.sel <= r0.byte_sel;
1019 r1.wb.adr <= ra(r1.wb.adr'left downto 3) & "000";
1020 r1.wb.dat <= r0.data;
1021 if cancel_store = '0' then
1022 r1.wb.cyc <= '1';
1023 r1.wb.stb <= '1';
1024 r1.wb.we <= '1';
1025 r1.state <= STORE_WAIT_ACK;
1026 else
1027 r1.stcx_fail <= '1';
1028 r1.state <= IDLE;
1029 end if;
1030 else
1031 -- dcbz is handled much like a load miss except
1032 -- that we are writing to memory instead of reading
1033 r1.store_index <= req_index;
1034 r1.store_row <= get_row(req_laddr);
1035
1036 if req_op = OP_STORE_HIT then
1037 r1.store_way <= req_hit_way;
1038 else
1039 r1.store_way <= replace_way;
1040
1041 -- Force misses on the victim way while zeroing
1042 cache_valids(req_index)(replace_way) <= '0';
1043
1044 -- Store new tag in selected way
1045 for i in 0 to NUM_WAYS-1 loop
1046 if i = replace_way then
1047 tagset := cache_tags(req_index);
1048 write_tag(i, tagset, req_tag);
1049 cache_tags(req_index) <= tagset;
1050 end if;
1051 end loop;
1052 end if;
1053
1054 -- Set up for wishbone writes
1055 r1.wb.adr <= req_laddr(r1.wb.adr'left downto 0);
1056 r1.wb.sel <= (others => '1');
1057 r1.wb.we <= '1';
1058 r1.wb.dat <= (others => '0');
1059 r1.wb.cyc <= '1';
1060 r1.wb.stb <= '1';
1061
1062 -- Handle the rest like a load miss
1063 r1.state <= RELOAD_WAIT_ACK;
1064 end if;
1065
1066 -- OP_NONE and OP_BAD do nothing
1067 -- OP_BAD was handled above already
1068 when OP_NONE =>
1069 when OP_BAD =>
1070 end case;
1071
1072 when RELOAD_WAIT_ACK =>
1073 -- Requests are all sent if stb is 0
1074 stbs_done := r1.wb.stb = '0';
1075
1076 -- If we are still sending requests, was one accepted ?
1077 if wishbone_in.stall = '0' and not stbs_done then
1078 -- That was the last word ? We are done sending. Clear
1079 -- stb and set stbs_done so we can handle an eventual last
1080 -- ack on the same cycle.
1081 --
1082 if is_last_row_addr(r1.wb.adr) then
1083 r1.wb.stb <= '0';
1084 stbs_done := true;
1085 end if;
1086
1087 -- Calculate the next row address
1088 r1.wb.adr <= next_row_addr(r1.wb.adr);
1089 end if;
1090
1091 -- Incoming acks processing
1092 if wishbone_in.ack = '1' then
1093 -- Is this the data we were looking for ? Latch it so
1094 -- we can respond later. We don't currently complete the
1095 -- pending miss request immediately, we wait for the
1096 -- whole line to be loaded. The reason is that if we
1097 -- did, we would potentially get new requests in while
1098 -- not idle, which we don't currently know how to deal
1099 -- with.
1100 --
1101 if r1.store_row = get_row(r1.req.addr) and r1.req.dcbz = '0' then
1102 r1.slow_data <= wishbone_in.dat;
1103 end if;
1104
1105 -- Check for completion
1106 if stbs_done and is_last_row(r1.store_row) then
1107 -- Complete wishbone cycle
1108 r1.wb.cyc <= '0';
1109
1110 -- Cache line is now valid
1111 cache_valids(r1.store_index)(r1.store_way) <= '1';
1112
1113 -- Don't complete and go idle until next cycle, in
1114 -- case the next request is for the last dword of
1115 -- the cache line we just loaded.
1116 r1.state <= FINISH_LD_MISS;
1117 end if;
1118
1119 -- Increment store row counter
1120 r1.store_row <= next_row(r1.store_row);
1121 end if;
1122
1123 when FINISH_LD_MISS =>
1124 -- Write back the load data that we got
1125 r1.slow_valid <= '1';
1126 r1.state <= IDLE;
1127 report "completing miss !";
1128
1129 when STORE_WAIT_ACK | NC_LOAD_WAIT_ACK =>
1130 -- Clear stb when slave accepted request
1131 if wishbone_in.stall = '0' then
1132 r1.wb.stb <= '0';
1133 end if;
1134
1135 -- Got ack ? complete.
1136 if wishbone_in.ack = '1' then
1137 if r1.state = NC_LOAD_WAIT_ACK then
1138 r1.slow_data <= wishbone_in.dat;
1139 end if;
1140 r1.state <= IDLE;
1141 r1.slow_valid <= '1';
1142 r1.wb.cyc <= '0';
1143 r1.wb.stb <= '0';
1144 end if;
1145 end case;
1146 end if;
1147 end if;
1148 end process;
1149 end;