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