icache.vhdl 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. --
  2. -- Set associative icache
  3. --
  4. -- TODO (in no specific order):
  5. --
  6. -- * Add debug interface to inspect cache content
  7. -- * Add snoop/invalidate path
  8. -- * Add multi-hit error detection
  9. -- * Pipelined bus interface (wb or axi)
  10. -- * Maybe add parity ? There's a few bits free in each BRAM row on Xilinx
  11. -- * Add optimization: service hits on partially loaded lines
  12. -- * Add optimization: (maybe) interrupt reload on fluch/redirect
  13. -- * Check if playing with the geometry of the cache tags allow for more
  14. -- efficient use of distributed RAM and less logic/muxes. Currently we
  15. -- write TAG_BITS width which may not match full ram blocks and might
  16. -- cause muxes to be inferred for "partial writes".
  17. -- * Check if making the read size of PLRU a ROM helps utilization
  18. --
  19. library ieee;
  20. use ieee.std_logic_1164.all;
  21. use ieee.numeric_std.all;
  22. library work;
  23. use work.utils.all;
  24. use work.common.all;
  25. use work.wishbone_types.all;
  26. -- 64 bit direct mapped icache. All instructions are 4B aligned.
  27. entity icache is
  28. generic (
  29. SIM : boolean := false;
  30. -- Line size in bytes
  31. LINE_SIZE : positive := 64;
  32. -- BRAM organisation: We never access more than wishbone_data_bits at
  33. -- a time so to save resources we make the array only that wide, and
  34. -- use consecutive indices for to make a cache "line"
  35. --
  36. -- ROW_SIZE is the width in bytes of the BRAM (based on WB, so 64-bits)
  37. ROW_SIZE : positive := wishbone_data_bits / 8;
  38. -- Number of lines in a set
  39. NUM_LINES : positive := 32;
  40. -- Number of ways
  41. NUM_WAYS : positive := 4;
  42. -- L1 ITLB number of entries (direct mapped)
  43. TLB_SIZE : positive := 64;
  44. -- L1 ITLB log_2(page_size)
  45. TLB_LG_PGSZ : positive := 12;
  46. -- Number of real address bits that we store
  47. REAL_ADDR_BITS : positive := 56;
  48. -- Non-zero to enable log data collection
  49. LOG_LENGTH : natural := 0
  50. );
  51. port (
  52. clk : in std_ulogic;
  53. rst : in std_ulogic;
  54. i_in : in Fetch1ToIcacheType;
  55. i_out : out IcacheToDecode1Type;
  56. m_in : in MmuToIcacheType;
  57. stall_in : in std_ulogic;
  58. stall_out : out std_ulogic;
  59. flush_in : in std_ulogic;
  60. inval_in : in std_ulogic;
  61. wishbone_out : out wishbone_master_out;
  62. wishbone_in : in wishbone_slave_out;
  63. log_out : out std_ulogic_vector(53 downto 0)
  64. );
  65. end entity icache;
  66. architecture rtl of icache is
  67. constant ROW_SIZE_BITS : natural := ROW_SIZE*8;
  68. -- ROW_PER_LINE is the number of row (wishbone transactions) in a line
  69. constant ROW_PER_LINE : natural := LINE_SIZE / ROW_SIZE;
  70. -- BRAM_ROWS is the number of rows in BRAM needed to represent the full
  71. -- icache
  72. constant BRAM_ROWS : natural := NUM_LINES * ROW_PER_LINE;
  73. -- INSN_PER_ROW is the number of 32bit instructions per BRAM row
  74. constant INSN_PER_ROW : natural := ROW_SIZE_BITS / 32;
  75. -- Bit fields counts in the address
  76. -- INSN_BITS is the number of bits to select an instruction in a row
  77. constant INSN_BITS : natural := log2(INSN_PER_ROW);
  78. -- ROW_BITS is the number of bits to select a row
  79. constant ROW_BITS : natural := log2(BRAM_ROWS);
  80. -- ROW_LINEBITS is the number of bits to select a row within a line
  81. constant ROW_LINEBITS : natural := log2(ROW_PER_LINE);
  82. -- LINE_OFF_BITS is the number of bits for the offset in a cache line
  83. constant LINE_OFF_BITS : natural := log2(LINE_SIZE);
  84. -- ROW_OFF_BITS is the number of bits for the offset in a row
  85. constant ROW_OFF_BITS : natural := log2(ROW_SIZE);
  86. -- INDEX_BITS is the number of bits to select a cache line
  87. constant INDEX_BITS : natural := log2(NUM_LINES);
  88. -- SET_SIZE_BITS is the log base 2 of the set size
  89. constant SET_SIZE_BITS : natural := LINE_OFF_BITS + INDEX_BITS;
  90. -- TAG_BITS is the number of bits of the tag part of the address
  91. -- the +1 is to allow the endianness to be stored in the tag
  92. constant TAG_BITS : natural := REAL_ADDR_BITS - SET_SIZE_BITS + 1;
  93. -- WAY_BITS is the number of bits to select a way
  94. constant WAY_BITS : natural := log2(NUM_WAYS);
  95. -- Example of layout for 32 lines of 64 bytes:
  96. --
  97. -- .. tag |index| line |
  98. -- .. | row | |
  99. -- .. | | | |00| zero (2)
  100. -- .. | | |-| | INSN_BITS (1)
  101. -- .. | |---| | ROW_LINEBITS (3)
  102. -- .. | |--- - --| LINE_OFF_BITS (6)
  103. -- .. | |- --| ROW_OFF_BITS (3)
  104. -- .. |----- ---| | ROW_BITS (8)
  105. -- .. |-----| | INDEX_BITS (5)
  106. -- .. --------| | TAG_BITS (53)
  107. subtype row_t is integer range 0 to BRAM_ROWS-1;
  108. subtype index_t is integer range 0 to NUM_LINES-1;
  109. subtype way_t is integer range 0 to NUM_WAYS-1;
  110. subtype row_in_line_t is unsigned(ROW_LINEBITS-1 downto 0);
  111. -- The cache data BRAM organized as described above for each way
  112. subtype cache_row_t is std_ulogic_vector(ROW_SIZE_BITS-1 downto 0);
  113. -- The cache tags LUTRAM has a row per set. Vivado is a pain and will
  114. -- not handle a clean (commented) definition of the cache tags as a 3d
  115. -- memory. For now, work around it by putting all the tags
  116. subtype cache_tag_t is std_logic_vector(TAG_BITS-1 downto 0);
  117. -- type cache_tags_set_t is array(way_t) of cache_tag_t;
  118. -- type cache_tags_array_t is array(index_t) of cache_tags_set_t;
  119. constant TAG_RAM_WIDTH : natural := TAG_BITS * NUM_WAYS;
  120. subtype cache_tags_set_t is std_logic_vector(TAG_RAM_WIDTH-1 downto 0);
  121. type cache_tags_array_t is array(index_t) of cache_tags_set_t;
  122. -- The cache valid bits
  123. subtype cache_way_valids_t is std_ulogic_vector(NUM_WAYS-1 downto 0);
  124. type cache_valids_t is array(index_t) of cache_way_valids_t;
  125. type row_per_line_valid_t is array(0 to ROW_PER_LINE - 1) of std_ulogic;
  126. -- Storage. Hopefully "cache_rows" is a BRAM, the rest is LUTs
  127. signal cache_tags : cache_tags_array_t;
  128. signal cache_valids : cache_valids_t;
  129. attribute ram_style : string;
  130. attribute ram_style of cache_tags : signal is "distributed";
  131. -- L1 ITLB.
  132. constant TLB_BITS : natural := log2(TLB_SIZE);
  133. constant TLB_EA_TAG_BITS : natural := 64 - (TLB_LG_PGSZ + TLB_BITS);
  134. constant TLB_PTE_BITS : natural := 64;
  135. subtype tlb_index_t is integer range 0 to TLB_SIZE - 1;
  136. type tlb_valids_t is array(tlb_index_t) of std_ulogic;
  137. subtype tlb_tag_t is std_ulogic_vector(TLB_EA_TAG_BITS - 1 downto 0);
  138. type tlb_tags_t is array(tlb_index_t) of tlb_tag_t;
  139. subtype tlb_pte_t is std_ulogic_vector(TLB_PTE_BITS - 1 downto 0);
  140. type tlb_ptes_t is array(tlb_index_t) of tlb_pte_t;
  141. signal itlb_valids : tlb_valids_t;
  142. signal itlb_tags : tlb_tags_t;
  143. signal itlb_ptes : tlb_ptes_t;
  144. attribute ram_style of itlb_tags : signal is "distributed";
  145. attribute ram_style of itlb_ptes : signal is "distributed";
  146. -- Privilege bit from PTE EAA field
  147. signal eaa_priv : std_ulogic;
  148. -- Cache reload state machine
  149. type state_t is (IDLE, CLR_TAG, WAIT_ACK);
  150. type reg_internal_t is record
  151. -- Cache hit state (Latches for 1 cycle BRAM access)
  152. hit_way : way_t;
  153. hit_nia : std_ulogic_vector(63 downto 0);
  154. hit_smark : std_ulogic;
  155. hit_valid : std_ulogic;
  156. big_endian: std_ulogic;
  157. -- Cache miss state (reload state machine)
  158. state : state_t;
  159. wb : wishbone_master_out;
  160. store_way : way_t;
  161. store_index : index_t;
  162. store_row : row_t;
  163. store_tag : cache_tag_t;
  164. store_valid : std_ulogic;
  165. end_row_ix : row_in_line_t;
  166. rows_valid : row_per_line_valid_t;
  167. -- TLB miss state
  168. fetch_failed : std_ulogic;
  169. end record;
  170. signal r : reg_internal_t;
  171. -- Async signals on incoming request
  172. signal req_index : index_t;
  173. signal req_row : row_t;
  174. signal req_hit_way : way_t;
  175. signal req_tag : cache_tag_t;
  176. signal req_is_hit : std_ulogic;
  177. signal req_is_miss : std_ulogic;
  178. signal req_laddr : std_ulogic_vector(63 downto 0);
  179. signal tlb_req_index : tlb_index_t;
  180. signal real_addr : std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
  181. signal ra_valid : std_ulogic;
  182. signal priv_fault : std_ulogic;
  183. signal access_ok : std_ulogic;
  184. signal use_previous : std_ulogic;
  185. -- Cache RAM interface
  186. type cache_ram_out_t is array(way_t) of cache_row_t;
  187. signal cache_out : cache_ram_out_t;
  188. -- PLRU output interface
  189. type plru_out_t is array(index_t) of std_ulogic_vector(WAY_BITS-1 downto 0);
  190. signal plru_victim : plru_out_t;
  191. signal replace_way : way_t;
  192. -- Return the cache line index (tag index) for an address
  193. function get_index(addr: std_ulogic_vector(63 downto 0)) return index_t is
  194. begin
  195. return to_integer(unsigned(addr(SET_SIZE_BITS - 1 downto LINE_OFF_BITS)));
  196. end;
  197. -- Return the cache row index (data memory) for an address
  198. function get_row(addr: std_ulogic_vector(63 downto 0)) return row_t is
  199. begin
  200. return to_integer(unsigned(addr(SET_SIZE_BITS - 1 downto ROW_OFF_BITS)));
  201. end;
  202. -- Return the index of a row within a line
  203. function get_row_of_line(row: row_t) return row_in_line_t is
  204. variable row_v : unsigned(ROW_BITS-1 downto 0);
  205. begin
  206. row_v := to_unsigned(row, ROW_BITS);
  207. return row_v(ROW_LINEBITS-1 downto 0);
  208. end;
  209. -- Returns whether this is the last row of a line
  210. function is_last_row_addr(addr: wishbone_addr_type; last: row_in_line_t) return boolean is
  211. begin
  212. return unsigned(addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS)) = last;
  213. end;
  214. -- Returns whether this is the last row of a line
  215. function is_last_row(row: row_t; last: row_in_line_t) return boolean is
  216. begin
  217. return get_row_of_line(row) = last;
  218. end;
  219. -- Return the address of the next row in the current cache line
  220. function next_row_addr(addr: wishbone_addr_type)
  221. return std_ulogic_vector is
  222. variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
  223. variable result : wishbone_addr_type;
  224. begin
  225. -- Is there no simpler way in VHDL to generate that 3 bits adder ?
  226. row_idx := addr(LINE_OFF_BITS-1 downto ROW_OFF_BITS);
  227. row_idx := std_ulogic_vector(unsigned(row_idx) + 1);
  228. result := addr;
  229. result(LINE_OFF_BITS-1 downto ROW_OFF_BITS) := row_idx;
  230. return result;
  231. end;
  232. -- Return the next row in the current cache line. We use a dedicated
  233. -- function in order to limit the size of the generated adder to be
  234. -- only the bits within a cache line (3 bits with default settings)
  235. --
  236. function next_row(row: row_t) return row_t is
  237. variable row_v : std_ulogic_vector(ROW_BITS-1 downto 0);
  238. variable row_idx : std_ulogic_vector(ROW_LINEBITS-1 downto 0);
  239. variable result : std_ulogic_vector(ROW_BITS-1 downto 0);
  240. begin
  241. row_v := std_ulogic_vector(to_unsigned(row, ROW_BITS));
  242. row_idx := row_v(ROW_LINEBITS-1 downto 0);
  243. row_v(ROW_LINEBITS-1 downto 0) := std_ulogic_vector(unsigned(row_idx) + 1);
  244. return to_integer(unsigned(row_v));
  245. end;
  246. -- Read the instruction word for the given address in the current cache row
  247. function read_insn_word(addr: std_ulogic_vector(63 downto 0);
  248. data: cache_row_t) return std_ulogic_vector is
  249. variable word: integer range 0 to INSN_PER_ROW-1;
  250. begin
  251. word := to_integer(unsigned(addr(INSN_BITS+2-1 downto 2)));
  252. return data(31+word*32 downto word*32);
  253. end;
  254. -- Get the tag value from the address
  255. function get_tag(addr: std_ulogic_vector(REAL_ADDR_BITS - 1 downto 0);
  256. endian: std_ulogic) return cache_tag_t is
  257. begin
  258. return endian & addr(REAL_ADDR_BITS - 1 downto SET_SIZE_BITS);
  259. end;
  260. -- Read a tag from a tag memory row
  261. function read_tag(way: way_t; tagset: cache_tags_set_t) return cache_tag_t is
  262. begin
  263. return tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS);
  264. end;
  265. -- Write a tag to tag memory row
  266. procedure write_tag(way: in way_t; tagset: inout cache_tags_set_t;
  267. tag: cache_tag_t) is
  268. begin
  269. tagset((way+1) * TAG_BITS - 1 downto way * TAG_BITS) := tag;
  270. end;
  271. -- Simple hash for direct-mapped TLB index
  272. function hash_ea(addr: std_ulogic_vector(63 downto 0)) return tlb_index_t is
  273. variable hash : std_ulogic_vector(TLB_BITS - 1 downto 0);
  274. begin
  275. hash := addr(TLB_LG_PGSZ + TLB_BITS - 1 downto TLB_LG_PGSZ)
  276. xor addr(TLB_LG_PGSZ + 2 * TLB_BITS - 1 downto TLB_LG_PGSZ + TLB_BITS)
  277. xor addr(TLB_LG_PGSZ + 3 * TLB_BITS - 1 downto TLB_LG_PGSZ + 2 * TLB_BITS);
  278. return to_integer(unsigned(hash));
  279. end;
  280. begin
  281. assert LINE_SIZE mod ROW_SIZE = 0;
  282. assert ispow2(LINE_SIZE) report "LINE_SIZE not power of 2" severity FAILURE;
  283. assert ispow2(NUM_LINES) report "NUM_LINES not power of 2" severity FAILURE;
  284. assert ispow2(ROW_PER_LINE) report "ROW_PER_LINE not power of 2" severity FAILURE;
  285. assert ispow2(INSN_PER_ROW) report "INSN_PER_ROW not power of 2" severity FAILURE;
  286. assert (ROW_BITS = INDEX_BITS + ROW_LINEBITS)
  287. report "geometry bits don't add up" severity FAILURE;
  288. assert (LINE_OFF_BITS = ROW_OFF_BITS + ROW_LINEBITS)
  289. report "geometry bits don't add up" severity FAILURE;
  290. assert (REAL_ADDR_BITS + 1 = TAG_BITS + INDEX_BITS + LINE_OFF_BITS)
  291. report "geometry bits don't add up" severity FAILURE;
  292. assert (REAL_ADDR_BITS + 1 = TAG_BITS + ROW_BITS + ROW_OFF_BITS)
  293. report "geometry bits don't add up" severity FAILURE;
  294. sim_debug: if SIM generate
  295. debug: process
  296. begin
  297. report "ROW_SIZE = " & natural'image(ROW_SIZE);
  298. report "ROW_PER_LINE = " & natural'image(ROW_PER_LINE);
  299. report "BRAM_ROWS = " & natural'image(BRAM_ROWS);
  300. report "INSN_PER_ROW = " & natural'image(INSN_PER_ROW);
  301. report "INSN_BITS = " & natural'image(INSN_BITS);
  302. report "ROW_BITS = " & natural'image(ROW_BITS);
  303. report "ROW_LINEBITS = " & natural'image(ROW_LINEBITS);
  304. report "LINE_OFF_BITS = " & natural'image(LINE_OFF_BITS);
  305. report "ROW_OFF_BITS = " & natural'image(ROW_OFF_BITS);
  306. report "INDEX_BITS = " & natural'image(INDEX_BITS);
  307. report "TAG_BITS = " & natural'image(TAG_BITS);
  308. report "WAY_BITS = " & natural'image(WAY_BITS);
  309. wait;
  310. end process;
  311. end generate;
  312. -- Generate a cache RAM for each way
  313. rams: for i in 0 to NUM_WAYS-1 generate
  314. signal do_read : std_ulogic;
  315. signal do_write : std_ulogic;
  316. signal rd_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
  317. signal wr_addr : std_ulogic_vector(ROW_BITS-1 downto 0);
  318. signal dout : cache_row_t;
  319. signal wr_sel : std_ulogic_vector(ROW_SIZE-1 downto 0);
  320. signal wr_dat : std_ulogic_vector(wishbone_in.dat'left downto 0);
  321. begin
  322. way: entity work.cache_ram
  323. generic map (
  324. ROW_BITS => ROW_BITS,
  325. WIDTH => ROW_SIZE_BITS
  326. )
  327. port map (
  328. clk => clk,
  329. rd_en => do_read,
  330. rd_addr => rd_addr,
  331. rd_data => dout,
  332. wr_sel => wr_sel,
  333. wr_addr => wr_addr,
  334. wr_data => wr_dat
  335. );
  336. process(all)
  337. variable j: integer;
  338. begin
  339. -- byte-swap read data if big endian
  340. if r.store_tag(TAG_BITS - 1) = '0' then
  341. wr_dat <= wishbone_in.dat;
  342. else
  343. for ii in 0 to (wishbone_in.dat'length / 8) - 1 loop
  344. j := ((ii / 4) * 4) + (3 - (ii mod 4));
  345. wr_dat(ii * 8 + 7 downto ii * 8) <= wishbone_in.dat(j * 8 + 7 downto j * 8);
  346. end loop;
  347. end if;
  348. do_read <= not (stall_in or use_previous);
  349. do_write <= '0';
  350. if wishbone_in.ack = '1' and replace_way = i then
  351. do_write <= '1';
  352. end if;
  353. cache_out(i) <= dout;
  354. rd_addr <= std_ulogic_vector(to_unsigned(req_row, ROW_BITS));
  355. wr_addr <= std_ulogic_vector(to_unsigned(r.store_row, ROW_BITS));
  356. for ii in 0 to ROW_SIZE-1 loop
  357. wr_sel(ii) <= do_write;
  358. end loop;
  359. end process;
  360. end generate;
  361. -- Generate PLRUs
  362. maybe_plrus: if NUM_WAYS > 1 generate
  363. begin
  364. plrus: for i in 0 to NUM_LINES-1 generate
  365. -- PLRU interface
  366. signal plru_acc : std_ulogic_vector(WAY_BITS-1 downto 0);
  367. signal plru_acc_en : std_ulogic;
  368. signal plru_out : std_ulogic_vector(WAY_BITS-1 downto 0);
  369. begin
  370. plru : entity work.plru
  371. generic map (
  372. BITS => WAY_BITS
  373. )
  374. port map (
  375. clk => clk,
  376. rst => rst,
  377. acc => plru_acc,
  378. acc_en => plru_acc_en,
  379. lru => plru_out
  380. );
  381. process(all)
  382. begin
  383. -- PLRU interface
  384. if get_index(r.hit_nia) = i then
  385. plru_acc_en <= r.hit_valid;
  386. else
  387. plru_acc_en <= '0';
  388. end if;
  389. plru_acc <= std_ulogic_vector(to_unsigned(r.hit_way, WAY_BITS));
  390. plru_victim(i) <= plru_out;
  391. end process;
  392. end generate;
  393. end generate;
  394. -- TLB hit detection and real address generation
  395. itlb_lookup : process(all)
  396. variable pte : tlb_pte_t;
  397. variable ttag : tlb_tag_t;
  398. begin
  399. tlb_req_index <= hash_ea(i_in.nia);
  400. pte := itlb_ptes(tlb_req_index);
  401. ttag := itlb_tags(tlb_req_index);
  402. if i_in.virt_mode = '1' then
  403. real_addr <= pte(REAL_ADDR_BITS - 1 downto TLB_LG_PGSZ) &
  404. i_in.nia(TLB_LG_PGSZ - 1 downto 0);
  405. if ttag = i_in.nia(63 downto TLB_LG_PGSZ + TLB_BITS) then
  406. ra_valid <= itlb_valids(tlb_req_index);
  407. else
  408. ra_valid <= '0';
  409. end if;
  410. eaa_priv <= pte(3);
  411. else
  412. real_addr <= i_in.nia(REAL_ADDR_BITS - 1 downto 0);
  413. ra_valid <= '1';
  414. eaa_priv <= '1';
  415. end if;
  416. -- no IAMR, so no KUEP support for now
  417. priv_fault <= eaa_priv and not i_in.priv_mode;
  418. access_ok <= ra_valid and not priv_fault;
  419. end process;
  420. -- iTLB update
  421. itlb_update: process(clk)
  422. variable wr_index : tlb_index_t;
  423. begin
  424. if rising_edge(clk) then
  425. wr_index := hash_ea(m_in.addr);
  426. if rst = '1' or (m_in.tlbie = '1' and m_in.doall = '1') then
  427. -- clear all valid bits
  428. for i in tlb_index_t loop
  429. itlb_valids(i) <= '0';
  430. end loop;
  431. elsif m_in.tlbie = '1' then
  432. -- clear entry regardless of hit or miss
  433. itlb_valids(wr_index) <= '0';
  434. elsif m_in.tlbld = '1' then
  435. itlb_tags(wr_index) <= m_in.addr(63 downto TLB_LG_PGSZ + TLB_BITS);
  436. itlb_ptes(wr_index) <= m_in.pte;
  437. itlb_valids(wr_index) <= '1';
  438. end if;
  439. end if;
  440. end process;
  441. -- Cache hit detection, output to fetch2 and other misc logic
  442. icache_comb : process(all)
  443. variable is_hit : std_ulogic;
  444. variable hit_way : way_t;
  445. begin
  446. -- i_in.sequential means that i_in.nia this cycle is 4 more than
  447. -- last cycle. If we read more than 32 bits at a time, had a cache hit
  448. -- last cycle, and we don't want the first 32-bit chunk, then we can
  449. -- keep the data we read last cycle and just use that.
  450. if unsigned(i_in.nia(INSN_BITS+2-1 downto 2)) /= 0 then
  451. use_previous <= i_in.req and i_in.sequential and r.hit_valid;
  452. else
  453. use_previous <= '0';
  454. end if;
  455. -- Extract line, row and tag from request
  456. req_index <= get_index(i_in.nia);
  457. req_row <= get_row(i_in.nia);
  458. req_tag <= get_tag(real_addr, i_in.big_endian);
  459. -- Calculate address of beginning of cache row, will be
  460. -- used for cache miss processing if needed
  461. --
  462. req_laddr <= (63 downto REAL_ADDR_BITS => '0') &
  463. real_addr(REAL_ADDR_BITS - 1 downto ROW_OFF_BITS) &
  464. (ROW_OFF_BITS-1 downto 0 => '0');
  465. -- Test if pending request is a hit on any way
  466. hit_way := 0;
  467. is_hit := '0';
  468. for i in way_t loop
  469. if i_in.req = '1' and
  470. (cache_valids(req_index)(i) = '1' or
  471. (r.state = WAIT_ACK and
  472. req_index = r.store_index and
  473. i = r.store_way and
  474. r.rows_valid(req_row mod ROW_PER_LINE) = '1')) then
  475. if read_tag(i, cache_tags(req_index)) = req_tag then
  476. hit_way := i;
  477. is_hit := '1';
  478. end if;
  479. end if;
  480. end loop;
  481. -- Generate the "hit" and "miss" signals for the synchronous blocks
  482. if i_in.req = '1' and access_ok = '1' and flush_in = '0' and rst = '0' then
  483. req_is_hit <= is_hit;
  484. req_is_miss <= not is_hit;
  485. else
  486. req_is_hit <= '0';
  487. req_is_miss <= '0';
  488. end if;
  489. req_hit_way <= hit_way;
  490. -- The way to replace on a miss
  491. if r.state = CLR_TAG then
  492. replace_way <= to_integer(unsigned(plru_victim(r.store_index)));
  493. else
  494. replace_way <= r.store_way;
  495. end if;
  496. -- Output instruction from current cache row
  497. --
  498. -- Note: This is a mild violation of our design principle of having pipeline
  499. -- stages output from a clean latch. In this case we output the result
  500. -- of a mux. The alternative would be output an entire row which
  501. -- I prefer not to do just yet as it would force fetch2 to know about
  502. -- some of the cache geometry information.
  503. --
  504. i_out.insn <= read_insn_word(r.hit_nia, cache_out(r.hit_way));
  505. i_out.valid <= r.hit_valid;
  506. i_out.nia <= r.hit_nia;
  507. i_out.stop_mark <= r.hit_smark;
  508. i_out.fetch_failed <= r.fetch_failed;
  509. i_out.big_endian <= r.big_endian;
  510. i_out.next_predicted <= i_in.predicted;
  511. -- Stall fetch1 if we have a miss on cache or TLB or a protection fault
  512. stall_out <= not (is_hit and access_ok);
  513. -- Wishbone requests output (from the cache miss reload machine)
  514. wishbone_out <= r.wb;
  515. end process;
  516. -- Cache hit synchronous machine
  517. icache_hit : process(clk)
  518. begin
  519. if rising_edge(clk) then
  520. -- keep outputs to fetch2 unchanged on a stall
  521. -- except that flush or reset sets valid to 0
  522. -- If use_previous, keep the same data as last cycle and use the second half
  523. if stall_in = '1' or use_previous = '1' then
  524. if rst = '1' or flush_in = '1' then
  525. r.hit_valid <= '0';
  526. end if;
  527. else
  528. -- On a hit, latch the request for the next cycle, when the BRAM data
  529. -- will be available on the cache_out output of the corresponding way
  530. --
  531. r.hit_valid <= req_is_hit;
  532. if req_is_hit = '1' then
  533. r.hit_way <= req_hit_way;
  534. report "cache hit nia:" & to_hstring(i_in.nia) &
  535. " IR:" & std_ulogic'image(i_in.virt_mode) &
  536. " SM:" & std_ulogic'image(i_in.stop_mark) &
  537. " idx:" & integer'image(req_index) &
  538. " tag:" & to_hstring(req_tag) &
  539. " way:" & integer'image(req_hit_way) &
  540. " RA:" & to_hstring(real_addr);
  541. end if;
  542. end if;
  543. if stall_in = '0' then
  544. -- Send stop marks and NIA down regardless of validity
  545. r.hit_smark <= i_in.stop_mark;
  546. r.hit_nia <= i_in.nia;
  547. r.big_endian <= i_in.big_endian;
  548. end if;
  549. end if;
  550. end process;
  551. -- Cache miss/reload synchronous machine
  552. icache_miss : process(clk)
  553. variable tagset : cache_tags_set_t;
  554. variable stbs_done : boolean;
  555. begin
  556. if rising_edge(clk) then
  557. -- On reset, clear all valid bits to force misses
  558. if rst = '1' then
  559. for i in index_t loop
  560. cache_valids(i) <= (others => '0');
  561. end loop;
  562. r.state <= IDLE;
  563. r.wb.cyc <= '0';
  564. r.wb.stb <= '0';
  565. -- We only ever do reads on wishbone
  566. r.wb.dat <= (others => '0');
  567. r.wb.sel <= "11111111";
  568. r.wb.we <= '0';
  569. -- Not useful normally but helps avoiding tons of sim warnings
  570. r.wb.adr <= (others => '0');
  571. else
  572. -- Process cache invalidations
  573. if inval_in = '1' then
  574. for i in index_t loop
  575. cache_valids(i) <= (others => '0');
  576. end loop;
  577. r.store_valid <= '0';
  578. end if;
  579. -- Main state machine
  580. case r.state is
  581. when IDLE =>
  582. -- Reset per-row valid flags, only used in WAIT_ACK
  583. for i in 0 to ROW_PER_LINE - 1 loop
  584. r.rows_valid(i) <= '0';
  585. end loop;
  586. -- We need to read a cache line
  587. if req_is_miss = '1' then
  588. report "cache miss nia:" & to_hstring(i_in.nia) &
  589. " IR:" & std_ulogic'image(i_in.virt_mode) &
  590. " SM:" & std_ulogic'image(i_in.stop_mark) &
  591. " idx:" & integer'image(req_index) &
  592. " way:" & integer'image(replace_way) &
  593. " tag:" & to_hstring(req_tag) &
  594. " RA:" & to_hstring(real_addr);
  595. -- Keep track of our index and way for subsequent stores
  596. r.store_index <= req_index;
  597. r.store_row <= get_row(req_laddr);
  598. r.store_tag <= req_tag;
  599. r.store_valid <= '1';
  600. r.end_row_ix <= get_row_of_line(get_row(req_laddr)) - 1;
  601. -- Prep for first wishbone read. We calculate the address of
  602. -- the start of the cache line and start the WB cycle.
  603. --
  604. r.wb.adr <= req_laddr(r.wb.adr'left downto 0);
  605. r.wb.cyc <= '1';
  606. r.wb.stb <= '1';
  607. -- Track that we had one request sent
  608. r.state <= CLR_TAG;
  609. end if;
  610. when CLR_TAG | WAIT_ACK =>
  611. if r.state = CLR_TAG then
  612. -- Get victim way from plru
  613. r.store_way <= replace_way;
  614. -- Force misses on that way while reloading that line
  615. cache_valids(req_index)(replace_way) <= '0';
  616. -- Store new tag in selected way
  617. for i in 0 to NUM_WAYS-1 loop
  618. if i = replace_way then
  619. tagset := cache_tags(r.store_index);
  620. write_tag(i, tagset, r.store_tag);
  621. cache_tags(r.store_index) <= tagset;
  622. end if;
  623. end loop;
  624. r.state <= WAIT_ACK;
  625. end if;
  626. -- Requests are all sent if stb is 0
  627. stbs_done := r.wb.stb = '0';
  628. -- If we are still sending requests, was one accepted ?
  629. if wishbone_in.stall = '0' and not stbs_done then
  630. -- That was the last word ? We are done sending. Clear
  631. -- stb and set stbs_done so we can handle an eventual last
  632. -- ack on the same cycle.
  633. --
  634. if is_last_row_addr(r.wb.adr, r.end_row_ix) then
  635. r.wb.stb <= '0';
  636. stbs_done := true;
  637. end if;
  638. -- Calculate the next row address
  639. r.wb.adr <= next_row_addr(r.wb.adr);
  640. end if;
  641. -- Incoming acks processing
  642. if wishbone_in.ack = '1' then
  643. r.rows_valid(r.store_row mod ROW_PER_LINE) <= '1';
  644. -- Check for completion
  645. if stbs_done and is_last_row(r.store_row, r.end_row_ix) then
  646. -- Complete wishbone cycle
  647. r.wb.cyc <= '0';
  648. -- Cache line is now valid
  649. cache_valids(r.store_index)(replace_way) <= r.store_valid and not inval_in;
  650. -- We are done
  651. r.state <= IDLE;
  652. end if;
  653. -- Increment store row counter
  654. r.store_row <= next_row(r.store_row);
  655. end if;
  656. end case;
  657. end if;
  658. -- TLB miss and protection fault processing
  659. if rst = '1' or flush_in = '1' or m_in.tlbld = '1' then
  660. r.fetch_failed <= '0';
  661. elsif i_in.req = '1' and access_ok = '0' and stall_in = '0' then
  662. r.fetch_failed <= '1';
  663. end if;
  664. end if;
  665. end process;
  666. icache_log: if LOG_LENGTH > 0 generate
  667. -- Output data to logger
  668. signal log_data : std_ulogic_vector(53 downto 0);
  669. begin
  670. data_log: process(clk)
  671. variable lway: way_t;
  672. variable wstate: std_ulogic;
  673. begin
  674. if rising_edge(clk) then
  675. lway := req_hit_way;
  676. wstate := '0';
  677. if r.state /= IDLE then
  678. wstate := '1';
  679. end if;
  680. log_data <= i_out.valid &
  681. i_out.insn &
  682. wishbone_in.ack &
  683. r.wb.adr(5 downto 3) &
  684. r.wb.stb & r.wb.cyc &
  685. wishbone_in.stall &
  686. stall_out &
  687. r.fetch_failed &
  688. r.hit_nia(5 downto 2) &
  689. wstate &
  690. std_ulogic_vector(to_unsigned(lway, 3)) &
  691. req_is_hit & req_is_miss &
  692. access_ok &
  693. ra_valid;
  694. end if;
  695. end process;
  696. log_out <= log_data;
  697. end generate;
  698. end;