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