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