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