dcache: Reduce back-to-back store latency from 3 cycles to 2
[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 m_in : in MmuToDcacheType;
44 m_out : out DcacheToMmuType;
45
46 stall_out : out std_ulogic;
47
48 wishbone_out : out wishbone_master_out;
49 wishbone_in : in wishbone_slave_out;
50
51 log_out : out std_ulogic_vector(19 downto 0)
52 );
53 end entity dcache;
54
55 architecture rtl of dcache is
56 -- BRAM organisation: We never access more than wishbone_data_bits at
57 -- a time so to save resources we make the array only that wide, and
58 -- use consecutive indices for to make a cache "line"
59 --
60 -- ROW_SIZE is the width in bytes of the BRAM (based on WB, so 64-bits)
61 constant ROW_SIZE : natural := wishbone_data_bits / 8;
62 -- ROW_PER_LINE is the number of row (wishbone transactions) in a line
63 constant ROW_PER_LINE : natural := LINE_SIZE / ROW_SIZE;
64 -- BRAM_ROWS is the number of rows in BRAM needed to represent the full
65 -- dcache
66 constant BRAM_ROWS : natural := NUM_LINES * ROW_PER_LINE;
67
68 -- Bit fields counts in the address
69
70 -- REAL_ADDR_BITS is the number of real address bits that we store
71 constant REAL_ADDR_BITS : positive := 56;
72 -- ROW_BITS is the number of bits to select a row
73 constant ROW_BITS : natural := log2(BRAM_ROWS);
74 -- ROW_LINEBITS is the number of bits to select a row within a line
75 constant ROW_LINEBITS : natural := log2(ROW_PER_LINE);
76 -- LINE_OFF_BITS is the number of bits for the offset in a cache line
77 constant LINE_OFF_BITS : natural := log2(LINE_SIZE);
78 -- ROW_OFF_BITS is the number of bits for the offset in a row
79 constant ROW_OFF_BITS : natural := log2(ROW_SIZE);
80 -- INDEX_BITS is the number if bits to select a cache line
81 constant INDEX_BITS : natural := log2(NUM_LINES);
82 -- SET_SIZE_BITS is the log base 2 of the set size
83 constant SET_SIZE_BITS : natural := LINE_OFF_BITS + INDEX_BITS;
84 -- TAG_BITS is the number of bits of the tag part of the address
85 constant TAG_BITS : natural := REAL_ADDR_BITS - SET_SIZE_BITS;
86 -- TAG_WIDTH is the width in bits of each way of the tag RAM
87 constant TAG_WIDTH : natural := TAG_BITS + 7 - ((TAG_BITS + 7) mod 8);
88 -- WAY_BITS is the number of bits to select a way
89 constant WAY_BITS : natural := log2(NUM_WAYS);
90
91 -- Example of layout for 32 lines of 64 bytes:
92 --
93 -- .. tag |index| line |
94 -- .. | row | |
95 -- .. | |---| | ROW_LINEBITS (3)
96 -- .. | |--- - --| LINE_OFF_BITS (6)
97 -- .. | |- --| ROW_OFF_BITS (3)
98 -- .. |----- ---| | ROW_BITS (8)
99 -- .. |-----| | INDEX_BITS (5)
100 -- .. --------| | TAG_BITS (45)
101
102 subtype row_t is integer range 0 to BRAM_ROWS-1;
103 subtype index_t is integer range 0 to NUM_LINES-1;
104 subtype way_t is integer range 0 to NUM_WAYS-1;
105 subtype row_in_line_t is unsigned(ROW_LINEBITS-1 downto 0);
106
107 -- The cache data BRAM organized as described above for each way
108 subtype cache_row_t is std_ulogic_vector(wishbone_data_bits-1 downto 0);
109
110 -- The cache tags LUTRAM has a row per set. Vivado is a pain and will
111 -- not handle a clean (commented) definition of the cache tags as a 3d
112 -- memory. For now, work around it by putting all the tags
113 subtype cache_tag_t is std_logic_vector(TAG_BITS-1 downto 0);
114 -- type cache_tags_set_t is array(way_t) of cache_tag_t;
115 -- type cache_tags_array_t is array(index_t) of cache_tags_set_t;
116 constant TAG_RAM_WIDTH : natural := TAG_WIDTH * NUM_WAYS;
117 subtype cache_tags_set_t is std_logic_vector(TAG_RAM_WIDTH-1 downto 0);
118 type cache_tags_array_t is array(index_t) of cache_tags_set_t;
119
120 -- The cache valid bits
121 subtype cache_way_valids_t is std_ulogic_vector(NUM_WAYS-1 downto 0);
122 type cache_valids_t is array(index_t) of cache_way_valids_t;
123 type row_per_line_valid_t is array(0 to ROW_PER_LINE - 1) of std_ulogic;
124
125 -- Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
126 signal cache_tags : cache_tags_array_t;
127 signal cache_tag_set : cache_tags_set_t;
128 signal cache_valids : cache_valids_t;
129
130 attribute ram_style : string;
131 attribute ram_style of cache_tags : signal is "distributed";
132
133 -- L1 TLB.
134 constant TLB_SET_BITS : natural := log2(TLB_SET_SIZE);
135 constant TLB_WAY_BITS : natural := log2(TLB_NUM_WAYS);
136 constant TLB_EA_TAG_BITS : natural := 64 - (TLB_LG_PGSZ + TLB_SET_BITS);
137 constant TLB_TAG_WAY_BITS : natural := TLB_NUM_WAYS * TLB_EA_TAG_BITS;
138 constant TLB_PTE_BITS : natural := 64;
139 constant TLB_PTE_WAY_BITS : natural := TLB_NUM_WAYS * TLB_PTE_BITS;
140
141 subtype tlb_way_t is integer range 0 to TLB_NUM_WAYS - 1;
142 subtype tlb_index_t is integer range 0 to TLB_SET_SIZE - 1;
143 subtype tlb_way_valids_t is std_ulogic_vector(TLB_NUM_WAYS-1 downto 0);
144 type tlb_valids_t is array(tlb_index_t) of tlb_way_valids_t;
145 subtype tlb_tag_t is std_ulogic_vector(TLB_EA_TAG_BITS - 1 downto 0);
146 subtype tlb_way_tags_t is std_ulogic_vector(TLB_TAG_WAY_BITS-1 downto 0);
147 type tlb_tags_t is array(tlb_index_t) of tlb_way_tags_t;
148 subtype tlb_pte_t is std_ulogic_vector(TLB_PTE_BITS - 1 downto 0);
149 subtype tlb_way_ptes_t is std_ulogic_vector(TLB_PTE_WAY_BITS-1 downto 0);
150 type tlb_ptes_t is array(tlb_index_t) of tlb_way_ptes_t;
151 type hit_way_set_t is array(tlb_way_t) of way_t;
152
153 signal dtlb_valids : tlb_valids_t;
154 signal dtlb_tags : tlb_tags_t;
155 signal dtlb_ptes : tlb_ptes_t;
156 attribute ram_style of dtlb_tags : signal is "distributed";
157 attribute ram_style of dtlb_ptes : signal is "distributed";
158
159 -- Record for storing permission, attribute, etc. bits from a PTE
160 type perm_attr_t is record
161 reference : std_ulogic;
162 changed : std_ulogic;
163 nocache : std_ulogic;
164 priv : std_ulogic;
165 rd_perm : std_ulogic;
166 wr_perm : std_ulogic;
167 end record;
168
169 function extract_perm_attr(pte : std_ulogic_vector(TLB_PTE_BITS - 1 downto 0)) return perm_attr_t is
170 variable pa : perm_attr_t;
171 begin
172 pa.reference := pte(8);
173 pa.changed := pte(7);
174 pa.nocache := pte(5);
175 pa.priv := pte(3);
176 pa.rd_perm := pte(2);
177 pa.wr_perm := pte(1);
178 return pa;
179 end;
180
181 constant real_mode_perm_attr : perm_attr_t := (nocache => '0', others => '1');
182
183 -- Type of operation on a "valid" input
184 type op_t is (OP_NONE,
185 OP_BAD, -- NC cache hit, TLB miss, prot/RC failure
186 OP_STCX_FAIL, -- conditional store w/o reservation
187 OP_LOAD_HIT, -- Cache hit on load
188 OP_LOAD_MISS, -- Load missing cache
189 OP_LOAD_NC, -- Non-cachable load
190 OP_STORE_HIT, -- Store hitting cache
191 OP_STORE_MISS); -- Store missing cache
192
193 -- Cache state machine
194 type state_t is (IDLE, -- Normal load hit processing
195 RELOAD_WAIT_ACK, -- Cache reload wait ack
196 STORE_WAIT_ACK, -- Store wait ack
197 NC_LOAD_WAIT_ACK);-- Non-cachable load wait ack
198
199
200 --
201 -- Dcache operations:
202 --
203 -- In order to make timing, we use the BRAMs with an output buffer,
204 -- which means that the BRAM output is delayed by an extra cycle.
205 --
206 -- Thus, the dcache has a 2-stage internal pipeline for cache hits
207 -- with no stalls.
208 --
209 -- All other operations are handled via stalling in the first stage.
210 --
211 -- The second stage can thus complete a hit at the same time as the
212 -- first stage emits a stall for a complex op.
213 --
214
215 -- Stage 0 register, basically contains just the latched request
216 type reg_stage_0_t is record
217 req : Loadstore1ToDcacheType;
218 tlbie : std_ulogic;
219 doall : std_ulogic;
220 tlbld : std_ulogic;
221 mmu_req : std_ulogic; -- indicates source of request
222 end record;
223
224 signal r0 : reg_stage_0_t;
225 signal r0_full : std_ulogic;
226
227 type mem_access_request_t is record
228 op : op_t;
229 dcbz : std_ulogic;
230 real_addr : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
231 data : std_ulogic_vector(63 downto 0);
232 byte_sel : std_ulogic_vector(7 downto 0);
233 hit_way : way_t;
234 repl_way : way_t;
235 same_tag : std_ulogic;
236 end record;
237
238 -- First stage register, contains state for stage 1 of load hits
239 -- and for the state machine used by all other operations
240 --
241 type reg_stage_1_t is record
242 -- Info about the request
243 full : std_ulogic; -- have uncompleted request
244 mmu_req : std_ulogic; -- request is from MMU
245 req : mem_access_request_t;
246
247 -- Cache hit state
248 hit_way : way_t;
249 hit_load_valid : std_ulogic;
250
251 -- 2-stage data buffer for data forwarded from writes to reads
252 forward_data1 : std_ulogic_vector(63 downto 0);
253 forward_data2 : std_ulogic_vector(63 downto 0);
254 forward_sel1 : std_ulogic_vector(7 downto 0);
255 forward_valid1 : std_ulogic;
256 forward_way1 : way_t;
257 forward_row1 : row_t;
258 use_forward1 : std_ulogic;
259 forward_sel : std_ulogic_vector(7 downto 0);
260
261 -- Cache miss state (reload state machine)
262 state : state_t;
263 dcbz : std_ulogic;
264 write_bram : std_ulogic;
265 write_tag : std_ulogic;
266 slow_valid : std_ulogic;
267 wb : wishbone_master_out;
268 reload_tag : cache_tag_t;
269 store_way : way_t;
270 store_row : row_t;
271 store_index : index_t;
272 end_row_ix : row_in_line_t;
273 rows_valid : row_per_line_valid_t;
274 acks_pending : unsigned(2 downto 0);
275
276 -- Signals to complete with error
277 error_done : std_ulogic;
278 cache_paradox : std_ulogic;
279
280 -- Signal to complete a failed stcx.
281 stcx_fail : std_ulogic;
282
283 -- completion signal for tlbie
284 tlbie_done : std_ulogic;
285 end record;
286
287 signal r1 : reg_stage_1_t;
288
289 -- Reservation information
290 --
291 type reservation_t is record
292 valid : std_ulogic;
293 addr : std_ulogic_vector(63 downto LINE_OFF_BITS);
294 end record;
295
296 signal reservation : reservation_t;
297
298 -- Async signals on incoming request
299 signal req_index : index_t;
300 signal req_row : row_t;
301 signal req_hit_way : way_t;
302 signal req_tag : cache_tag_t;
303 signal req_op : op_t;
304 signal req_data : std_ulogic_vector(63 downto 0);
305 signal req_same_tag : std_ulogic;
306
307 signal early_req_row : row_t;
308
309 signal cancel_store : std_ulogic;
310 signal set_rsrv : std_ulogic;
311 signal clear_rsrv : std_ulogic;
312
313 signal r0_valid : std_ulogic;
314 signal r0_stall : std_ulogic;
315
316 signal use_forward1_next : std_ulogic;
317 signal use_forward2_next : std_ulogic;
318
319 -- Cache RAM interface
320 type cache_ram_out_t is array(way_t) of cache_row_t;
321 signal cache_out : cache_ram_out_t;
322
323 -- PLRU output interface
324 type plru_out_t is array(index_t) of std_ulogic_vector(WAY_BITS-1 downto 0);
325 signal plru_victim : plru_out_t;
326 signal replace_way : way_t;
327
328 -- Wishbone read/write/cache write formatting signals
329 signal bus_sel : std_ulogic_vector(7 downto 0);
330
331 -- TLB signals
332 signal tlb_tag_way : tlb_way_tags_t;
333 signal tlb_pte_way : tlb_way_ptes_t;
334 signal tlb_valid_way : tlb_way_valids_t;
335 signal tlb_req_index : tlb_index_t;
336 signal tlb_hit : std_ulogic;
337 signal tlb_hit_way : tlb_way_t;
338 signal pte : tlb_pte_t;
339 signal ra : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
340 signal valid_ra : std_ulogic;
341 signal perm_attr : perm_attr_t;
342 signal rc_ok : std_ulogic;
343 signal perm_ok : std_ulogic;
344 signal access_ok : std_ulogic;
345
346 -- TLB PLRU output interface
347 type tlb_plru_out_t is array(tlb_index_t) of std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
348 signal tlb_plru_victim : tlb_plru_out_t;
349
350 --
351 -- Helper functions to decode incoming requests
352 --
353
354 -- Return the cache line index (tag index) for an address
355 function get_index(addr: std_ulogic_vector) return index_t is
356 begin
357 return to_integer(unsigned(addr(SET_SIZE_BITS - 1 downto LINE_OFF_BITS)));
358 end;
359
360 -- Return the cache row index (data memory) for an address
361 function get_row(addr: std_ulogic_vector) return row_t is
362 begin
363 return to_integer(unsigned(addr(SET_SIZE_BITS - 1 downto ROW_OFF_BITS)));
364 end;
365
366 -- Return the index of a row within a line
367 function get_row_of_line(row: row_t) return row_in_line_t is
368 variable row_v : unsigned(ROW_BITS-1 downto 0);
369 begin
370 row_v := to_unsigned(row, ROW_BITS);
371 return row_v(ROW_LINEBITS-1 downto 0);
372 end;
373
374 -- Returns whether this is the last row of a line
375 function is_last_row_addr(addr: wishbone_addr_type; last: row_in_line_t) return boolean is
376 begin
377 return unsigned(addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS)) = last;
378 end;
379
380 -- Returns whether this is the last row of a line
381 function is_last_row(row: row_t; last: row_in_line_t) return boolean is
382 begin
383 return get_row_of_line(row) = last;
384 end;
385
386 -- Return the address of the next row in the current cache line
387 function next_row_addr(addr: wishbone_addr_type) return std_ulogic_vector is
388 variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
389 variable result : wishbone_addr_type;
390 begin
391 -- Is there no simpler way in VHDL to generate that 3 bits adder ?
392 row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
393 row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
394 result := addr;
395 result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
396 return result;
397 end;
398
399 -- Return the next row in the current cache line. We use a dedicated
400 -- function in order to limit the size of the generated adder to be
401 -- only the bits within a cache line (3 bits with default settings)
402 --
403 function next_row(row: row_t) return row_t is
404 variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
405 variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
406 variable result : std_ulogic_vector(ROW_BITS-1 downto 0);
407 begin
408 row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
409 row_idx := row_v(ROW_LINEBITS-1 downto 0);
410 row_v(ROW_LINEBITS-1 downto 0) := std_ulogic_vector(unsigned(row_idx) + 1);
411 return to_integer(unsigned(row_v));
412 end;
413
414 -- Get the tag value from the address
415 function get_tag(addr: std_ulogic_vector) return cache_tag_t is
416 begin
417 return addr(REAL_ADDR_BITS - 1 downto SET_SIZE_BITS);
418 end;
419
420 -- Read a tag from a tag memory row
421 function read_tag(way: way_t; tagset: cache_tags_set_t) return cache_tag_t is
422 begin
423 return tagset(way * TAG_WIDTH + TAG_BITS - 1 downto way * TAG_WIDTH);
424 end;
425
426 -- Read a TLB tag from a TLB tag memory row
427 function read_tlb_tag(way: tlb_way_t; tags: tlb_way_tags_t) return tlb_tag_t is
428 variable j : integer;
429 begin
430 j := way * TLB_EA_TAG_BITS;
431 return tags(j + TLB_EA_TAG_BITS - 1 downto j);
432 end;
433
434 -- Write a TLB tag to a TLB tag memory row
435 procedure write_tlb_tag(way: tlb_way_t; tags: inout tlb_way_tags_t;
436 tag: tlb_tag_t) is
437 variable j : integer;
438 begin
439 j := way * TLB_EA_TAG_BITS;
440 tags(j + TLB_EA_TAG_BITS - 1 downto j) := tag;
441 end;
442
443 -- Read a PTE from a TLB PTE memory row
444 function read_tlb_pte(way: tlb_way_t; ptes: tlb_way_ptes_t) return tlb_pte_t is
445 variable j : integer;
446 begin
447 j := way * TLB_PTE_BITS;
448 return ptes(j + TLB_PTE_BITS - 1 downto j);
449 end;
450
451 procedure write_tlb_pte(way: tlb_way_t; ptes: inout tlb_way_ptes_t; newpte: tlb_pte_t) is
452 variable j : integer;
453 begin
454 j := way * TLB_PTE_BITS;
455 ptes(j + TLB_PTE_BITS - 1 downto j) := newpte;
456 end;
457
458 signal log_data : std_ulogic_vector(19 downto 0);
459
460 begin
461
462 assert LINE_SIZE mod ROW_SIZE = 0 report "LINE_SIZE not multiple of ROW_SIZE" severity FAILURE;
463 assert ispow2(LINE_SIZE) report "LINE_SIZE not power of 2" severity FAILURE;
464 assert ispow2(NUM_LINES) report "NUM_LINES not power of 2" severity FAILURE;
465 assert ispow2(ROW_PER_LINE) report "ROW_PER_LINE not power of 2" severity FAILURE;
466 assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
467 report "geometry bits don't add up" severity FAILURE;
468 assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
469 report "geometry bits don't add up" severity FAILURE;
470 assert (REAL_ADDR_BITS = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
471 report "geometry bits don't add up" severity FAILURE;
472 assert (REAL_ADDR_BITS = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
473 report "geometry bits don't add up" severity FAILURE;
474 assert (64 = wishbone_data_bits)
475 report "Can't yet handle a wishbone width that isn't 64-bits" severity FAILURE;
476 assert SET_SIZE_BITS <= TLB_LG_PGSZ report "Set indexed by virtual address" severity FAILURE;
477
478 -- Latch the request in r0.req as long as we're not stalling
479 stage_0 : process(clk)
480 variable r : reg_stage_0_t;
481 begin
482 if rising_edge(clk) then
483 assert (d_in.valid and m_in.valid) = '0' report
484 "request collision loadstore vs MMU";
485 if m_in.valid = '1' then
486 r.req.valid := '1';
487 r.req.load := not (m_in.tlbie or m_in.tlbld);
488 r.req.dcbz := '0';
489 r.req.nc := '0';
490 r.req.reserve := '0';
491 r.req.virt_mode := '0';
492 r.req.priv_mode := '1';
493 r.req.addr := m_in.addr;
494 r.req.data := m_in.pte;
495 r.req.byte_sel := (others => '1');
496 r.tlbie := m_in.tlbie;
497 r.doall := m_in.doall;
498 r.tlbld := m_in.tlbld;
499 r.mmu_req := '1';
500 else
501 r.req := d_in;
502 r.tlbie := '0';
503 r.doall := '0';
504 r.tlbld := '0';
505 r.mmu_req := '0';
506 end if;
507 if rst = '1' then
508 r0_full <= '0';
509 elsif r1.full = '0' or r0_full = '0' then
510 r0 <= r;
511 r0_full <= r.req.valid;
512 end if;
513 end if;
514 end process;
515
516 -- we don't yet handle collisions between loadstore1 requests and MMU requests
517 m_out.stall <= '0';
518
519 -- Hold off the request in r0 when r1 has an uncompleted request
520 r0_stall <= r0_full and r1.full;
521 r0_valid <= r0_full and not r1.full;
522 stall_out <= r0_stall;
523
524 -- TLB
525 -- Operates in the second cycle on the request latched in r0.req.
526 -- TLB updates write the entry at the end of the second cycle.
527 tlb_read : process(clk)
528 variable index : tlb_index_t;
529 variable addrbits : std_ulogic_vector(TLB_SET_BITS - 1 downto 0);
530 begin
531 if rising_edge(clk) then
532 if m_in.valid = '1' then
533 addrbits := m_in.addr(TLB_LG_PGSZ + TLB_SET_BITS - 1 downto TLB_LG_PGSZ);
534 else
535 addrbits := d_in.addr(TLB_LG_PGSZ + TLB_SET_BITS - 1 downto TLB_LG_PGSZ);
536 end if;
537 index := to_integer(unsigned(addrbits));
538 -- If we have any op and the previous op isn't finished,
539 -- then keep the same output for next cycle.
540 if r0_stall = '0' then
541 tlb_valid_way <= dtlb_valids(index);
542 tlb_tag_way <= dtlb_tags(index);
543 tlb_pte_way <= dtlb_ptes(index);
544 end if;
545 end if;
546 end process;
547
548 -- Generate TLB PLRUs
549 maybe_tlb_plrus: if TLB_NUM_WAYS > 1 generate
550 begin
551 tlb_plrus: for i in 0 to TLB_SET_SIZE - 1 generate
552 -- TLB PLRU interface
553 signal tlb_plru_acc : std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
554 signal tlb_plru_acc_en : std_ulogic;
555 signal tlb_plru_out : std_ulogic_vector(TLB_WAY_BITS-1 downto 0);
556 begin
557 tlb_plru : entity work.plru
558 generic map (
559 BITS => TLB_WAY_BITS
560 )
561 port map (
562 clk => clk,
563 rst => rst,
564 acc => tlb_plru_acc,
565 acc_en => tlb_plru_acc_en,
566 lru => tlb_plru_out
567 );
568
569 process(tlb_req_index, tlb_hit, tlb_hit_way, tlb_plru_out)
570 begin
571 -- PLRU interface
572 if tlb_hit = '1' and tlb_req_index = i then
573 tlb_plru_acc_en <= '1';
574 else
575 tlb_plru_acc_en <= '0';
576 end if;
577 tlb_plru_acc <= std_ulogic_vector(to_unsigned(tlb_hit_way, TLB_WAY_BITS));
578 tlb_plru_victim(i) <= tlb_plru_out;
579 end process;
580 end generate;
581 end generate;
582
583 tlb_search : process(all)
584 variable hitway : tlb_way_t;
585 variable hit : std_ulogic;
586 variable eatag : tlb_tag_t;
587 begin
588 tlb_req_index <= to_integer(unsigned(r0.req.addr(TLB_LG_PGSZ + TLB_SET_BITS - 1
589 downto TLB_LG_PGSZ)));
590 hitway := 0;
591 hit := '0';
592 eatag := r0.req.addr(63 downto TLB_LG_PGSZ + TLB_SET_BITS);
593 for i in tlb_way_t loop
594 if tlb_valid_way(i) = '1' and
595 read_tlb_tag(i, tlb_tag_way) = eatag then
596 hitway := i;
597 hit := '1';
598 end if;
599 end loop;
600 tlb_hit <= hit and r0_valid;
601 tlb_hit_way <= hitway;
602 if tlb_hit = '1' then
603 pte <= read_tlb_pte(hitway, tlb_pte_way);
604 else
605 pte <= (others => '0');
606 end if;
607 valid_ra <= tlb_hit or not r0.req.virt_mode;
608 if r0.req.virt_mode = '1' then
609 ra <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
610 r0.req.addr(TLB_LG_PGSZ - 1 downto ROW_OFF_BITS) &
611 (ROW_OFF_BITS-1 downto 0 => '0');
612 perm_attr <= extract_perm_attr(pte);
613 else
614 ra <= r0.req.addr(REAL_ADDR_BITS - 1 downto ROW_OFF_BITS) &
615 (ROW_OFF_BITS-1 downto 0 => '0');
616 perm_attr <= real_mode_perm_attr;
617 end if;
618 end process;
619
620 tlb_update : process(clk)
621 variable tlbie : std_ulogic;
622 variable tlbwe : std_ulogic;
623 variable repl_way : tlb_way_t;
624 variable eatag : tlb_tag_t;
625 variable tagset : tlb_way_tags_t;
626 variable pteset : tlb_way_ptes_t;
627 begin
628 if rising_edge(clk) then
629 tlbie := r0_valid and r0.tlbie;
630 tlbwe := r0_valid and r0.tlbld;
631 if rst = '1' or (tlbie = '1' and r0.doall = '1') then
632 -- clear all valid bits at once
633 for i in tlb_index_t loop
634 dtlb_valids(i) <= (others => '0');
635 end loop;
636 elsif tlbie = '1' then
637 if tlb_hit = '1' then
638 dtlb_valids(tlb_req_index)(tlb_hit_way) <= '0';
639 end if;
640 elsif tlbwe = '1' then
641 if tlb_hit = '1' then
642 repl_way := tlb_hit_way;
643 else
644 repl_way := to_integer(unsigned(tlb_plru_victim(tlb_req_index)));
645 end if;
646 eatag := r0.req.addr(63 downto TLB_LG_PGSZ + TLB_SET_BITS);
647 tagset := tlb_tag_way;
648 write_tlb_tag(repl_way, tagset, eatag);
649 dtlb_tags(tlb_req_index) <= tagset;
650 pteset := tlb_pte_way;
651 write_tlb_pte(repl_way, pteset, r0.req.data);
652 dtlb_ptes(tlb_req_index) <= pteset;
653 dtlb_valids(tlb_req_index)(repl_way) <= '1';
654 end if;
655 end if;
656 end process;
657
658 -- Generate PLRUs
659 maybe_plrus: if NUM_WAYS > 1 generate
660 begin
661 plrus: for i in 0 to NUM_LINES-1 generate
662 -- PLRU interface
663 signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
664 signal plru_acc_en : std_ulogic;
665 signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
666
667 begin
668 plru : entity work.plru
669 generic map (
670 BITS => WAY_BITS
671 )
672 port map (
673 clk => clk,
674 rst => rst,
675 acc => plru_acc,
676 acc_en => plru_acc_en,
677 lru => plru_out
678 );
679
680 process(req_index, req_op, req_hit_way, plru_out)
681 begin
682 -- PLRU interface
683 if (req_op = OP_LOAD_HIT or
684 req_op = OP_STORE_HIT) and req_index = i then
685 plru_acc_en <= '1';
686 else
687 plru_acc_en <= '0';
688 end if;
689 plru_acc <= std_ulogic_vector(to_unsigned(req_hit_way, WAY_BITS));
690 plru_victim(i) <= plru_out;
691 end process;
692 end generate;
693 end generate;
694
695 -- Cache tag RAM read port
696 cache_tag_read : process(clk)
697 variable index : index_t;
698 begin
699 if rising_edge(clk) then
700 if r0_stall = '1' then
701 index := req_index;
702 elsif m_in.valid = '1' then
703 index := get_index(m_in.addr);
704 else
705 index := get_index(d_in.addr);
706 end if;
707 cache_tag_set <= cache_tags(index);
708 end if;
709 end process;
710
711 -- Cache request parsing and hit detection
712 dcache_request : process(all)
713 variable is_hit : std_ulogic;
714 variable hit_way : way_t;
715 variable op : op_t;
716 variable opsel : std_ulogic_vector(2 downto 0);
717 variable go : std_ulogic;
718 variable nc : std_ulogic;
719 variable s_hit : std_ulogic;
720 variable s_tag : cache_tag_t;
721 variable s_pte : tlb_pte_t;
722 variable s_ra : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
723 variable hit_set : std_ulogic_vector(TLB_NUM_WAYS - 1 downto 0);
724 variable hit_way_set : hit_way_set_t;
725 variable rel_matches : std_ulogic_vector(TLB_NUM_WAYS - 1 downto 0);
726 variable rel_match : std_ulogic;
727 begin
728 -- Extract line, row and tag from request
729 req_index <= get_index(r0.req.addr);
730 req_row <= get_row(r0.req.addr);
731 req_tag <= get_tag(ra);
732
733 go := r0_valid and not (r0.tlbie or r0.tlbld) and not r1.error_done;
734
735 -- Test if pending request is a hit on any way
736 -- In order to make timing in virtual mode, when we are using the TLB,
737 -- we compare each way with each of the real addresses from each way of
738 -- the TLB, and then decide later which match to use.
739 hit_way := 0;
740 is_hit := '0';
741 rel_match := '0';
742 if r0.req.virt_mode = '1' then
743 rel_matches := (others => '0');
744 for j in tlb_way_t loop
745 hit_way_set(j) := 0;
746 s_hit := '0';
747 s_pte := read_tlb_pte(j, tlb_pte_way);
748 s_ra := s_pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
749 r0.req.addr(TLB_LG_PGSZ - 1 downto 0);
750 s_tag := get_tag(s_ra);
751 for i in way_t loop
752 if go = '1' and cache_valids(req_index)(i) = '1' and
753 read_tag(i, cache_tag_set) = s_tag and
754 tlb_valid_way(j) = '1' then
755 hit_way_set(j) := i;
756 s_hit := '1';
757 end if;
758 end loop;
759 hit_set(j) := s_hit;
760 if s_tag = r1.reload_tag then
761 rel_matches(j) := '1';
762 end if;
763 end loop;
764 if tlb_hit = '1' then
765 is_hit := hit_set(tlb_hit_way);
766 hit_way := hit_way_set(tlb_hit_way);
767 rel_match := rel_matches(tlb_hit_way);
768 end if;
769 else
770 s_tag := get_tag(r0.req.addr);
771 for i in way_t loop
772 if go = '1' and cache_valids(req_index)(i) = '1' and
773 read_tag(i, cache_tag_set) = s_tag then
774 hit_way := i;
775 is_hit := '1';
776 end if;
777 end loop;
778 if s_tag = r1.reload_tag then
779 rel_match := '1';
780 end if;
781 end if;
782 req_same_tag <= rel_match;
783
784 -- See if the request matches the line currently being reloaded
785 if r1.state = RELOAD_WAIT_ACK and req_index = r1.store_index and
786 rel_match = '1' then
787 -- For a store, consider this a hit even if the row isn't valid
788 -- since it will be by the time we perform the store.
789 -- For a load, check the appropriate row valid bit.
790 is_hit := not r0.req.load or r1.rows_valid(req_row mod ROW_PER_LINE);
791 hit_way := r1.store_way;
792 end if;
793
794 -- Whether to use forwarded data for a load or not
795 use_forward1_next <= '0';
796 if get_row(r1.req.real_addr) = req_row and r1.req.hit_way = hit_way then
797 -- Only need to consider r1.write_bram here, since if we are
798 -- writing refill data here, then we don't have a cache hit this
799 -- cycle on the line being refilled. (There is the possibility
800 -- that the load following the load miss that started the refill
801 -- could be to the old contents of the victim line, since it is a
802 -- couple of cycles after the refill starts before we see the
803 -- updated cache tag. In that case we don't use the bypass.)
804 use_forward1_next <= r1.write_bram;
805 end if;
806 use_forward2_next <= '0';
807 if r1.forward_row1 = req_row and r1.forward_way1 = hit_way then
808 use_forward2_next <= r1.forward_valid1;
809 end if;
810
811 -- The way that matched on a hit
812 req_hit_way <= hit_way;
813
814 -- The way to replace on a miss
815 replace_way <= to_integer(unsigned(plru_victim(req_index)));
816
817 -- work out whether we have permission for this access
818 -- NB we don't yet implement AMR, thus no KUAP
819 rc_ok <= perm_attr.reference and (r0.req.load or perm_attr.changed);
820 perm_ok <= (r0.req.priv_mode or not perm_attr.priv) and
821 (perm_attr.wr_perm or (r0.req.load and perm_attr.rd_perm));
822 access_ok <= valid_ra and perm_ok and rc_ok;
823
824 -- Combine the request and cache hit status to decide what
825 -- operation needs to be done
826 --
827 nc := r0.req.nc or perm_attr.nocache;
828 op := OP_NONE;
829 if go = '1' then
830 if access_ok = '0' then
831 op := OP_BAD;
832 elsif cancel_store = '1' then
833 op := OP_STCX_FAIL;
834 else
835 opsel := r0.req.load & nc & is_hit;
836 case opsel is
837 when "101" => op := OP_LOAD_HIT;
838 when "100" => op := OP_LOAD_MISS;
839 when "110" => op := OP_LOAD_NC;
840 when "001" => op := OP_STORE_HIT;
841 when "000" => op := OP_STORE_MISS;
842 when "010" => op := OP_STORE_MISS;
843 when "011" => op := OP_BAD;
844 when "111" => op := OP_BAD;
845 when others => op := OP_NONE;
846 end case;
847 end if;
848 end if;
849 req_op <= op;
850
851 -- Version of the row number that is valid one cycle earlier
852 -- in the cases where we need to read the cache data BRAM.
853 -- If we're stalling then we need to keep reading the last
854 -- row requested.
855 if r0_stall = '0' then
856 if m_in.valid = '1' then
857 early_req_row <= get_row(m_in.addr);
858 else
859 early_req_row <= get_row(d_in.addr);
860 end if;
861 else
862 early_req_row <= req_row;
863 end if;
864 end process;
865
866 -- Wire up wishbone request latch out of stage 1
867 wishbone_out <= r1.wb;
868
869 -- Handle load-with-reservation and store-conditional instructions
870 reservation_comb: process(all)
871 begin
872 cancel_store <= '0';
873 set_rsrv <= '0';
874 clear_rsrv <= '0';
875 if r0_valid = '1' and r0.req.reserve = '1' then
876 -- XXX generate alignment interrupt if address is not aligned
877 -- XXX or if r0.req.nc = '1'
878 if r0.req.load = '1' then
879 -- load with reservation
880 set_rsrv <= '1';
881 else
882 -- store conditional
883 clear_rsrv <= '1';
884 if reservation.valid = '0' or
885 r0.req.addr(63 downto LINE_OFF_BITS) /= reservation.addr then
886 cancel_store <= '1';
887 end if;
888 end if;
889 end if;
890 end process;
891
892 reservation_reg: process(clk)
893 begin
894 if rising_edge(clk) then
895 if rst = '1' then
896 reservation.valid <= '0';
897 elsif r0_valid = '1' and access_ok = '1' then
898 if clear_rsrv = '1' then
899 reservation.valid <= '0';
900 elsif set_rsrv = '1' then
901 reservation.valid <= '1';
902 reservation.addr <= r0.req.addr(63 downto LINE_OFF_BITS);
903 end if;
904 end if;
905 end if;
906 end process;
907
908 -- Return data for loads & completion control logic
909 --
910 writeback_control: process(all)
911 variable data_out : std_ulogic_vector(63 downto 0);
912 variable data_fwd : std_ulogic_vector(63 downto 0);
913 variable j : integer;
914 begin
915 -- Use the bypass if are reading the row that was written 1 or 2 cycles
916 -- ago, including for the slow_valid = 1 case (i.e. completing a load
917 -- miss or a non-cacheable load).
918 if r1.use_forward1 = '1' then
919 data_fwd := r1.forward_data1;
920 else
921 data_fwd := r1.forward_data2;
922 end if;
923 data_out := cache_out(r1.hit_way);
924 for i in 0 to 7 loop
925 j := i * 8;
926 if r1.forward_sel(i) = '1' then
927 data_out(j + 7 downto j) := data_fwd(j + 7 downto j);
928 end if;
929 end loop;
930
931 d_out.valid <= '0';
932 d_out.data <= data_out;
933 d_out.store_done <= '0';
934 d_out.error <= '0';
935 d_out.cache_paradox <= '0';
936
937 -- Outputs to MMU
938 m_out.done <= r1.tlbie_done;
939 m_out.err <= '0';
940 m_out.data <= data_out;
941
942 -- We have a valid load or store hit or we just completed a slow
943 -- op such as a load miss, a NC load or a store
944 --
945 -- Note: the load hit is delayed by one cycle. However it can still
946 -- not collide with r.slow_valid (well unless I miscalculated) because
947 -- slow_valid can only be set on a subsequent request and not on its
948 -- first cycle (the state machine must have advanced), which makes
949 -- slow_valid at least 2 cycles from the previous hit_load_valid.
950 --
951
952 -- Sanity: Only one of these must be set in any given cycle
953 assert (r1.slow_valid and r1.stcx_fail) /= '1' report
954 "unexpected slow_valid collision with stcx_fail"
955 severity FAILURE;
956 assert ((r1.slow_valid or r1.stcx_fail) and r1.hit_load_valid) /= '1' report
957 "unexpected hit_load_delayed collision with slow_valid"
958 severity FAILURE;
959
960 if r1.mmu_req = '0' then
961 -- Request came from loadstore1...
962 -- Load hit case is the standard path
963 if r1.hit_load_valid = '1' then
964 report "completing load hit data=" & to_hstring(data_out);
965 d_out.valid <= '1';
966 end if;
967
968 -- error cases complete without stalling
969 if r1.error_done = '1' then
970 report "completing ld/st with error";
971 d_out.error <= '1';
972 d_out.cache_paradox <= r1.cache_paradox;
973 d_out.valid <= '1';
974 end if;
975
976 -- Slow ops (load miss, NC, stores)
977 if r1.slow_valid = '1' then
978 d_out.store_done <= '1';
979 report "completing store or load miss data=" & to_hstring(data_out);
980 d_out.valid <= '1';
981 end if;
982
983 if r1.stcx_fail = '1' then
984 d_out.store_done <= '0';
985 d_out.valid <= '1';
986 end if;
987
988 else
989 -- Request came from MMU
990 if r1.hit_load_valid = '1' then
991 report "completing load hit to MMU, data=" & to_hstring(m_out.data);
992 m_out.done <= '1';
993 end if;
994
995 -- error cases complete without stalling
996 if r1.error_done = '1' then
997 report "completing MMU ld with error";
998 m_out.err <= '1';
999 m_out.done <= '1';
1000 end if;
1001
1002 -- Slow ops (i.e. load miss)
1003 if r1.slow_valid = '1' then
1004 report "completing MMU load miss, data=" & to_hstring(m_out.data);
1005 m_out.done <= '1';
1006 end if;
1007 end if;
1008
1009 end process;
1010
1011 --
1012 -- Generate a cache RAM for each way. This handles the normal
1013 -- reads, writes from reloads and the special store-hit update
1014 -- path as well.
1015 --
1016 -- Note: the BRAMs have an extra read buffer, meaning the output
1017 -- is pipelined an extra cycle. This differs from the
1018 -- icache. The writeback logic needs to take that into
1019 -- account by using 1-cycle delayed signals for load hits.
1020 --
1021 rams: for i in 0 to NUM_WAYS-1 generate
1022 signal do_read : std_ulogic;
1023 signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1024 signal do_write : std_ulogic;
1025 signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
1026 signal wr_data : std_ulogic_vector(wishbone_data_bits-1 downto 0);
1027 signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
1028 signal wr_sel_m : std_ulogic_vector(ROW_SIZE-1 downto 0);
1029 signal dout : cache_row_t;
1030 begin
1031 way: entity work.cache_ram
1032 generic map (
1033 ROW_BITS => ROW_BITS,
1034 WIDTH => wishbone_data_bits,
1035 ADD_BUF => true
1036 )
1037 port map (
1038 clk => clk,
1039 rd_en => do_read,
1040 rd_addr => rd_addr,
1041 rd_data => dout,
1042 wr_sel => wr_sel_m,
1043 wr_addr => wr_addr,
1044 wr_data => wr_data
1045 );
1046 process(all)
1047 begin
1048 -- Cache hit reads
1049 do_read <= '1';
1050 rd_addr <= std_ulogic_vector(to_unsigned(early_req_row, ROW_BITS));
1051 cache_out(i) <= dout;
1052
1053 -- Write mux:
1054 --
1055 -- Defaults to wishbone read responses (cache refill),
1056 --
1057 -- For timing, the mux on wr_data/sel/addr is not dependent on anything
1058 -- other than the current state.
1059 --
1060 wr_sel_m <= (others => '0');
1061
1062 do_write <= '0';
1063 if r1.write_bram = '1' then
1064 -- Write store data to BRAM. This happens one cycle after the
1065 -- store is in r0.
1066 wr_data <= r1.req.data;
1067 wr_sel <= r1.req.byte_sel;
1068 wr_addr <= std_ulogic_vector(to_unsigned(get_row(r1.req.real_addr), ROW_BITS));
1069 if i = r1.req.hit_way then
1070 do_write <= '1';
1071 end if;
1072 else
1073 -- Otherwise, we might be doing a reload or a DCBZ
1074 if r1.dcbz = '1' then
1075 wr_data <= (others => '0');
1076 else
1077 wr_data <= wishbone_in.dat;
1078 end if;
1079 wr_addr <= std_ulogic_vector(to_unsigned(r1.store_row, ROW_BITS));
1080 wr_sel <= (others => '1');
1081
1082 if r1.state = RELOAD_WAIT_ACK and wishbone_in.ack = '1' and r1.store_way = i then
1083 do_write <= '1';
1084 end if;
1085 end if;
1086
1087 -- Mask write selects with do_write since BRAM doesn't
1088 -- have a global write-enable
1089 if do_write = '1' then
1090 wr_sel_m <= wr_sel;
1091 end if;
1092
1093 end process;
1094 end generate;
1095
1096 --
1097 -- Cache hit synchronous machine for the easy case. This handles load hits.
1098 -- It also handles error cases (TLB miss, cache paradox)
1099 --
1100 dcache_fast_hit : process(clk)
1101 begin
1102 if rising_edge(clk) then
1103 if req_op /= OP_NONE then
1104 report "op:" & op_t'image(req_op) &
1105 " addr:" & to_hstring(r0.req.addr) &
1106 " nc:" & std_ulogic'image(r0.req.nc) &
1107 " idx:" & integer'image(req_index) &
1108 " tag:" & to_hstring(req_tag) &
1109 " way: " & integer'image(req_hit_way);
1110 end if;
1111 if r0_valid = '1' then
1112 r1.mmu_req <= r0.mmu_req;
1113 end if;
1114
1115 -- Fast path for load/store hits. Set signals for the writeback controls.
1116 if req_op = OP_LOAD_HIT then
1117 r1.hit_way <= req_hit_way;
1118 r1.hit_load_valid <= '1';
1119 else
1120 r1.hit_load_valid <= '0';
1121 end if;
1122
1123 if req_op = OP_BAD then
1124 report "Signalling ld/st error valid_ra=" & std_ulogic'image(valid_ra) &
1125 " rc_ok=" & std_ulogic'image(rc_ok) & " perm_ok=" & std_ulogic'image(perm_ok);
1126 r1.error_done <= '1';
1127 r1.cache_paradox <= access_ok;
1128 else
1129 r1.error_done <= '0';
1130 r1.cache_paradox <= '0';
1131 end if;
1132
1133 if req_op = OP_STCX_FAIL then
1134 r1.stcx_fail <= '1';
1135 else
1136 r1.stcx_fail <= '0';
1137 end if;
1138
1139 -- complete tlbies and TLB loads in the third cycle
1140 r1.tlbie_done <= r0_valid and (r0.tlbie or r0.tlbld);
1141 end if;
1142 end process;
1143
1144 --
1145 -- Memory accesses are handled by this state machine:
1146 --
1147 -- * Cache load miss/reload (in conjunction with "rams")
1148 -- * Load hits for non-cachable forms
1149 -- * Stores (the collision case is handled in "rams")
1150 --
1151 -- All wishbone requests generation is done here. This machine
1152 -- operates at stage 1.
1153 --
1154 dcache_slow : process(clk)
1155 variable stbs_done : boolean;
1156 variable req : mem_access_request_t;
1157 variable acks : unsigned(2 downto 0);
1158 begin
1159 if rising_edge(clk) then
1160 r1.use_forward1 <= use_forward1_next;
1161 r1.forward_sel <= (others => '0');
1162 if use_forward1_next = '1' then
1163 r1.forward_sel <= r1.req.byte_sel;
1164 elsif use_forward2_next = '1' then
1165 r1.forward_sel <= r1.forward_sel1;
1166 end if;
1167
1168 r1.forward_data2 <= r1.forward_data1;
1169 if r1.write_bram = '1' then
1170 r1.forward_data1 <= r1.req.data;
1171 r1.forward_sel1 <= r1.req.byte_sel;
1172 r1.forward_way1 <= r1.req.hit_way;
1173 r1.forward_row1 <= get_row(r1.req.real_addr);
1174 r1.forward_valid1 <= '1';
1175 else
1176 if r1.dcbz = '1' then
1177 r1.forward_data1 <= (others => '0');
1178 else
1179 r1.forward_data1 <= wishbone_in.dat;
1180 end if;
1181 r1.forward_sel1 <= (others => '1');
1182 r1.forward_way1 <= r1.store_way;
1183 r1.forward_row1 <= r1.store_row;
1184 r1.forward_valid1 <= '0';
1185 end if;
1186
1187 -- On reset, clear all valid bits to force misses
1188 if rst = '1' then
1189 for i in index_t loop
1190 cache_valids(i) <= (others => '0');
1191 end loop;
1192 r1.state <= IDLE;
1193 r1.full <= '0';
1194 r1.slow_valid <= '0';
1195 r1.wb.cyc <= '0';
1196 r1.wb.stb <= '0';
1197
1198 -- Not useful normally but helps avoiding tons of sim warnings
1199 r1.wb.adr <= (others => '0');
1200 else
1201 -- One cycle pulses reset
1202 r1.slow_valid <= '0';
1203 r1.write_bram <= '0';
1204
1205 if r1.write_tag = '1' then
1206 -- Store new tag in selected way
1207 for i in 0 to NUM_WAYS-1 loop
1208 if i = r1.store_way then
1209 cache_tags(r1.store_index)((i + 1) * TAG_WIDTH - 1 downto i * TAG_WIDTH) <=
1210 (TAG_WIDTH - 1 downto TAG_BITS => '0') & r1.reload_tag;
1211 end if;
1212 end loop;
1213 r1.write_tag <= '0';
1214 end if;
1215
1216 -- Take request from r1.req if there is one there,
1217 -- else from req_op, ra, etc.
1218 if r1.full = '1' then
1219 req := r1.req;
1220 else
1221 req.op := req_op;
1222 req.dcbz := r0.req.dcbz;
1223 req.real_addr := ra;
1224 req.data := r0.req.data;
1225 req.byte_sel := r0.req.byte_sel;
1226 req.hit_way := req_hit_way;
1227 req.repl_way := replace_way;
1228 req.same_tag := req_same_tag;
1229
1230 -- Store the incoming request from r0, if it is a slow request
1231 -- Note that r1.full = 1 implies req_op = OP_NONE
1232 if req_op = OP_LOAD_MISS or req_op = OP_LOAD_NC or
1233 req_op = OP_STORE_MISS or req_op = OP_STORE_HIT then
1234 r1.req <= req;
1235 r1.full <= '1';
1236 end if;
1237 end if;
1238
1239 -- Main state machine
1240 case r1.state is
1241 when IDLE =>
1242 r1.wb.adr <= req.real_addr(r1.wb.adr'left downto 0);
1243 r1.dcbz <= '0';
1244
1245 -- Keep track of our index and way for subsequent stores.
1246 r1.store_index <= get_index(req.real_addr);
1247 r1.store_row <= get_row(req.real_addr);
1248 r1.end_row_ix <= get_row_of_line(get_row(req.real_addr)) - 1;
1249 r1.reload_tag <= get_tag(req.real_addr);
1250 r1.req.same_tag <= '1';
1251
1252 if req.op = OP_STORE_HIT then
1253 r1.store_way <= req.hit_way;
1254 else
1255 r1.store_way <= req.repl_way;
1256 end if;
1257
1258 -- Reset per-row valid bits, ready for handling OP_LOAD_MISS
1259 for i in 0 to ROW_PER_LINE - 1 loop
1260 r1.rows_valid(i) <= '0';
1261 end loop;
1262
1263 case req.op is
1264 when OP_LOAD_HIT =>
1265 -- stay in IDLE state
1266
1267 when OP_LOAD_MISS =>
1268 -- Normal load cache miss, start the reload machine
1269 --
1270 report "cache miss real addr:" & to_hstring(req.real_addr) &
1271 " idx:" & integer'image(get_index(req.real_addr)) &
1272 " way:" & integer'image(req.repl_way) &
1273 " tag:" & to_hstring(get_tag(req.real_addr));
1274
1275 -- Start the wishbone cycle
1276 r1.wb.sel <= (others => '1');
1277 r1.wb.we <= '0';
1278 r1.wb.cyc <= '1';
1279 r1.wb.stb <= '1';
1280
1281 -- Track that we had one request sent
1282 r1.state <= RELOAD_WAIT_ACK;
1283 r1.write_tag <= '1';
1284
1285 when OP_LOAD_NC =>
1286 r1.wb.sel <= req.byte_sel;
1287 r1.wb.cyc <= '1';
1288 r1.wb.stb <= '1';
1289 r1.wb.we <= '0';
1290 r1.state <= NC_LOAD_WAIT_ACK;
1291
1292 when OP_STORE_HIT | OP_STORE_MISS =>
1293 if req.dcbz = '0' then
1294 r1.wb.sel <= req.byte_sel;
1295 r1.wb.dat <= req.data;
1296 r1.state <= STORE_WAIT_ACK;
1297 r1.acks_pending <= to_unsigned(1, 3);
1298 r1.full <= '0';
1299 r1.slow_valid <= '1';
1300 if req.op = OP_STORE_HIT then
1301 r1.write_bram <= '1';
1302 end if;
1303 else
1304 -- dcbz is handled much like a load miss except
1305 -- that we are writing to memory instead of reading
1306
1307 -- Start the wishbone writes
1308 r1.wb.sel <= (others => '1');
1309 r1.wb.dat <= (others => '0');
1310
1311 -- Handle the rest like a load miss
1312 r1.state <= RELOAD_WAIT_ACK;
1313 r1.write_tag <= '1';
1314 r1.dcbz <= '1';
1315 end if;
1316 r1.wb.we <= '1';
1317 r1.wb.cyc <= '1';
1318 r1.wb.stb <= '1';
1319
1320 -- OP_NONE and OP_BAD do nothing
1321 -- OP_BAD & OP_STCX_FAIL were handled above already
1322 when OP_NONE =>
1323 when OP_BAD =>
1324 when OP_STCX_FAIL =>
1325 end case;
1326
1327 when RELOAD_WAIT_ACK =>
1328 -- Requests are all sent if stb is 0
1329 stbs_done := r1.wb.stb = '0';
1330
1331 -- If we are still sending requests, was one accepted ?
1332 if wishbone_in.stall = '0' and not stbs_done then
1333 -- That was the last word ? We are done sending. Clear
1334 -- stb and set stbs_done so we can handle an eventual last
1335 -- ack on the same cycle.
1336 --
1337 if is_last_row_addr(r1.wb.adr, r1.end_row_ix) then
1338 r1.wb.stb <= '0';
1339 stbs_done := true;
1340 end if;
1341
1342 -- Calculate the next row address
1343 r1.wb.adr <= next_row_addr(r1.wb.adr);
1344 end if;
1345
1346 -- Incoming acks processing
1347 r1.forward_valid1 <= wishbone_in.ack;
1348 if wishbone_in.ack = '1' then
1349 r1.rows_valid(r1.store_row mod ROW_PER_LINE) <= '1';
1350 -- If this is the data we were looking for, we can
1351 -- complete the request next cycle.
1352 -- Compare the whole address in case the request in
1353 -- r1.req is not the one that started this refill.
1354 if r1.full = '1' and r1.req.same_tag = '1' and
1355 ((r1.dcbz = '1' and r1.req.dcbz = '1') or
1356 (r1.dcbz = '0' and r1.req.op = OP_LOAD_MISS)) and
1357 r1.store_row = get_row(r1.req.real_addr) then
1358 r1.full <= '0';
1359 r1.slow_valid <= '1';
1360 r1.forward_sel <= (others => '1');
1361 r1.use_forward1 <= '1';
1362 end if;
1363
1364 -- Check for completion
1365 if stbs_done and is_last_row(r1.store_row, r1.end_row_ix) then
1366 -- Complete wishbone cycle
1367 r1.wb.cyc <= '0';
1368
1369 -- Cache line is now valid
1370 cache_valids(r1.store_index)(r1.store_way) <= '1';
1371
1372 r1.state <= IDLE;
1373 end if;
1374
1375 -- Increment store row counter
1376 r1.store_row <= next_row(r1.store_row);
1377 end if;
1378
1379 when STORE_WAIT_ACK =>
1380 stbs_done := r1.wb.stb = '0';
1381 acks := r1.acks_pending;
1382 -- Clear stb when slave accepted request
1383 if wishbone_in.stall = '0' then
1384 -- See if there is another store waiting to be done
1385 -- which is in the same real page.
1386 if acks < 7 and req.same_tag = '1' and
1387 (req.op = OP_STORE_MISS or req.op = OP_STORE_HIT) then
1388 r1.wb.adr <= req.real_addr(r1.wb.adr'left downto 0);
1389 r1.wb.dat <= req.data;
1390 r1.wb.sel <= req.byte_sel;
1391 r1.wb.stb <= '1';
1392 stbs_done := false;
1393 if req.op = OP_STORE_HIT then
1394 r1.write_bram <= '1';
1395 end if;
1396 r1.full <= '0';
1397 r1.slow_valid <= '1';
1398 acks := acks + 1;
1399 else
1400 r1.wb.stb <= '0';
1401 stbs_done := true;
1402 end if;
1403 end if;
1404
1405 -- Got ack ? See if complete.
1406 if wishbone_in.ack = '1' then
1407 if stbs_done and acks = 1 then
1408 r1.state <= IDLE;
1409 r1.wb.cyc <= '0';
1410 r1.wb.stb <= '0';
1411 end if;
1412 acks := acks - 1;
1413 end if;
1414 r1.acks_pending <= acks;
1415
1416 when NC_LOAD_WAIT_ACK =>
1417 -- Clear stb when slave accepted request
1418 if wishbone_in.stall = '0' then
1419 r1.wb.stb <= '0';
1420 end if;
1421
1422 -- Got ack ? complete.
1423 if wishbone_in.ack = '1' then
1424 r1.state <= IDLE;
1425 r1.full <= '0';
1426 r1.slow_valid <= '1';
1427 r1.forward_sel <= (others => '1');
1428 r1.use_forward1 <= '1';
1429 r1.wb.cyc <= '0';
1430 r1.wb.stb <= '0';
1431 end if;
1432 end case;
1433 end if;
1434 end if;
1435 end process;
1436
1437 dcache_log: process(clk)
1438 begin
1439 if rising_edge(clk) then
1440 log_data <= r1.wb.adr(5 downto 3) &
1441 wishbone_in.stall &
1442 wishbone_in.ack &
1443 r1.wb.stb & r1.wb.cyc &
1444 d_out.error &
1445 d_out.valid &
1446 std_ulogic_vector(to_unsigned(op_t'pos(req_op), 3)) &
1447 stall_out &
1448 std_ulogic_vector(to_unsigned(tlb_hit_way, 3)) &
1449 valid_ra &
1450 std_ulogic_vector(to_unsigned(state_t'pos(r1.state), 3));
1451 end if;
1452 end process;
1453 log_out <= log_data;
1454 end;