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