xref: /XiangShan/src/main/scala/xiangshan/cache/dcache/loadpipe/LoadPipe.scala (revision a4e57ea3a91431261d57a58df4810c0d9f0366ef)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17package xiangshan.cache
18
19import chipsalliance.rocketchip.config.Parameters
20import chisel3._
21import chisel3.util._
22import freechips.rocketchip.tilelink.ClientMetadata
23import utils.{HasPerfEvents, XSDebug, XSPerfAccumulate}
24import xiangshan.L1CacheErrorInfo
25
26class LoadPipe(id: Int)(implicit p: Parameters) extends DCacheModule with HasPerfEvents {
27  def metaBits = (new Meta).getWidth
28  def encMetaBits = cacheParams.tagCode.width((new MetaAndTag).getWidth) - tagBits
29  def getMeta(encMeta: UInt): UInt = {
30    require(encMeta.getWidth == encMetaBits)
31    encMeta(metaBits - 1, 0)
32  }
33  def getECC(encMeta: UInt): UInt = {
34    require(encMeta.getWidth == encMetaBits)
35    encMeta(encMetaBits - 1, metaBits)
36  }
37
38  val io = IO(new DCacheBundle {
39    // incoming requests
40    val lsu = Flipped(new DCacheLoadIO)
41    // req got nacked in stage 0?
42    val nack      = Input(Bool())
43
44    // meta and data array read port
45    val meta_read = DecoupledIO(new MetaReadReq)
46    val meta_resp = Input(Vec(nWays, UInt(encMetaBits.W)))
47    val error_flag_resp = Input(Vec(nWays, Bool()))
48
49    val tag_read = DecoupledIO(new TagReadReq)
50    val tag_resp = Input(Vec(nWays, UInt(tagBits.W)))
51
52    val banked_data_read = DecoupledIO(new L1BankedDataReadReq)
53    val banked_data_resp = Input(Vec(DCacheBanks, new L1BankedDataReadResult()))
54
55    // banked data read conflict
56    val bank_conflict_slow = Input(Bool())
57    val bank_conflict_fast = Input(Bool())
58
59    // send miss request to miss queue
60    val miss_req    = DecoupledIO(new MissReq)
61
62    // update state vec in replacement algo
63    val replace_access = ValidIO(new ReplacementAccessBundle)
64    // find the way to be replaced
65    val replace_way = new ReplacementWayReqIO
66
67    // load fast wakeup should be disabled when data read is not ready
68    val disable_ld_fast_wakeup = Input(Bool())
69
70    // ecc error
71    val tag_error = Output(new L1CacheErrorInfo())
72  })
73
74  assert(RegNext(io.meta_read.ready))
75
76  val s1_ready = Wire(Bool())
77  val s2_ready = Wire(Bool())
78  // LSU requests
79  // it you got nacked, you can directly passdown
80  val not_nacked_ready = io.meta_read.ready && io.tag_read.ready && s1_ready
81  val nacked_ready     = true.B
82
83  // ready can wait for valid
84  io.lsu.req.ready := (!io.nack && not_nacked_ready) || (io.nack && nacked_ready)
85  io.meta_read.valid := io.lsu.req.fire() && !io.nack
86  io.tag_read.valid := io.lsu.req.fire() && !io.nack
87
88  val meta_read = io.meta_read.bits
89  val tag_read = io.tag_read.bits
90
91  // Tag read for new requests
92  meta_read.idx := get_idx(io.lsu.req.bits.addr)
93  meta_read.way_en := ~0.U(nWays.W)
94//  meta_read.tag := DontCare
95
96  tag_read.idx := get_idx(io.lsu.req.bits.addr)
97  tag_read.way_en := ~0.U(nWays.W)
98
99  // Pipeline
100  // --------------------------------------------------------------------------------
101  // stage 0
102  val s0_valid = io.lsu.req.fire()
103  val s0_req = io.lsu.req.bits
104  val s0_fire = s0_valid && s1_ready
105
106  assert(RegNext(!(s0_valid && (s0_req.cmd =/= MemoryOpConstants.M_XRD && s0_req.cmd =/= MemoryOpConstants.M_PFR && s0_req.cmd =/= MemoryOpConstants.M_PFW))), "LoadPipe only accepts load req / softprefetch read or write!")
107  dump_pipeline_reqs("LoadPipe s0", s0_valid, s0_req)
108
109  // --------------------------------------------------------------------------------
110  // stage 1
111  val s1_valid = RegInit(false.B)
112  val s1_req = RegEnable(s0_req, s0_fire)
113  // in stage 1, load unit gets the physical address
114  val s1_addr = io.lsu.s1_paddr
115  val s1_vaddr = s1_req.addr
116  val s1_bank_oh = UIntToOH(addr_to_dcache_bank(s1_req.addr))
117  val s1_nack = RegNext(io.nack)
118  val s1_nack_data = !io.banked_data_read.ready
119  val s1_fire = s1_valid && s2_ready
120  s1_ready := !s1_valid || s1_fire
121
122  when (s0_fire) { s1_valid := true.B }
123  .elsewhen (s1_fire) { s1_valid := false.B }
124
125  dump_pipeline_reqs("LoadPipe s1", s1_valid, s1_req)
126
127  // tag check
128  val meta_resp = VecInit(io.meta_resp.map(r => getMeta(r).asTypeOf(new Meta)))
129  def wayMap[T <: Data](f: Int => T) = VecInit((0 until nWays).map(f))
130  val s1_tag_eq_way = wayMap((w: Int) => io.tag_resp(w) === (get_tag(s1_addr))).asUInt
131  val s1_tag_match_way = wayMap((w: Int) => s1_tag_eq_way(w) && meta_resp(w).coh.isValid()).asUInt
132  val s1_tag_match = s1_tag_match_way.orR
133  assert(RegNext(!s1_valid || PopCount(s1_tag_match_way) <= 1.U), "tag should not match with more than 1 way")
134
135  val s1_fake_meta = Wire(new Meta)
136//  s1_fake_meta.tag := get_tag(s1_addr)
137  s1_fake_meta.coh := ClientMetadata.onReset
138  val s1_fake_tag = get_tag(s1_addr)
139
140  // when there are no tag match, we give it a Fake Meta
141  // this simplifies our logic in s2 stage
142  val s1_hit_meta = Mux(s1_tag_match, Mux1H(s1_tag_match_way, wayMap((w: Int) => meta_resp(w))), s1_fake_meta)
143  val s1_hit_coh = s1_hit_meta.coh
144  val s1_hit_error = Mux(s1_tag_match, Mux1H(s1_tag_match_way, wayMap((w: Int) => io.error_flag_resp(w))), false.B)
145
146  io.replace_way.set.valid := RegNext(s0_fire)
147  io.replace_way.set.bits := get_idx(s1_vaddr)
148  val s1_repl_way_en = UIntToOH(io.replace_way.way)
149  val s1_repl_tag = Mux1H(s1_repl_way_en, wayMap(w => io.tag_resp(w)))
150  val s1_repl_coh = Mux1H(s1_repl_way_en, wayMap(w => meta_resp(w).coh))
151
152  val s1_need_replacement = !s1_tag_match
153  val s1_way_en = Mux(s1_need_replacement, s1_repl_way_en, s1_tag_match_way)
154  val s1_coh = Mux(s1_need_replacement, s1_repl_coh, s1_hit_coh)
155  val s1_tag = Mux(s1_need_replacement, s1_repl_tag, get_tag(s1_addr))
156
157  // data read
158  io.banked_data_read.valid := s1_fire && !s1_nack
159  io.banked_data_read.bits.addr := s1_vaddr
160  io.banked_data_read.bits.way_en := s1_tag_match_way
161
162  io.replace_access.valid := RegNext(RegNext(io.meta_read.fire()) && s1_tag_match && s1_valid)
163  io.replace_access.bits.set := RegNext(get_idx(s1_req.addr))
164  io.replace_access.bits.way := RegNext(OHToUInt(s1_tag_match_way))
165
166  // TODO: optimize implementation
167  val s1_has_permission = s1_hit_coh.onAccess(s1_req.cmd)._1
168  val s1_new_hit_coh = s1_hit_coh.onAccess(s1_req.cmd)._3
169  val s1_hit = s1_tag_match && s1_has_permission && s1_hit_coh === s1_new_hit_coh
170  val s1_will_send_miss_req = s1_valid && !s1_nack && !s1_nack_data && !s1_hit
171
172  // check ecc error
173  val ecc_resp = VecInit(io.meta_resp.map(r => getECC(r)))
174  val s1_ecc = Mux1H(s1_tag_match_way, wayMap((w: Int) => ecc_resp(w)))
175  val s1_eccMetaAndTag = Cat(s1_ecc, MetaAndTag(s1_hit_coh, get_tag(s1_addr)).asUInt)
176  val s1_tag_ecc_error = s1_hit && dcacheParameters.dataCode.decode(s1_eccMetaAndTag).error // error reported by tag ecc check
177  val s1_cache_flag_error = Mux(s1_need_replacement, false.B, s1_hit_error) // error reported by exist dcache error bit
178  val s1_error = s1_cache_flag_error || s1_tag_ecc_error
179
180  // --------------------------------------------------------------------------------
181  // stage 2
182  // val s2_valid = RegEnable(next = s1_valid && !io.lsu.s1_kill, init = false.B, enable = s1_fire)
183  val s2_valid = RegInit(false.B)
184  val s2_req = RegEnable(s1_req, s1_fire)
185  val s2_addr = RegEnable(s1_addr, s1_fire)
186  val s2_vaddr = RegEnable(s1_vaddr, s1_fire)
187  val s2_bank_oh = RegEnable(s1_bank_oh, s1_fire)
188  s2_ready := true.B
189
190  when (s1_fire) { s2_valid := !io.lsu.s1_kill }
191  .elsewhen(io.lsu.resp.fire()) { s2_valid := false.B }
192
193  dump_pipeline_reqs("LoadPipe s2", s2_valid, s2_req)
194
195  // hit, miss, nack, permission checking
196  val s2_tag_match_way = RegEnable(s1_tag_match_way, s1_fire)
197  val s2_tag_match = RegEnable(s1_tag_match, s1_fire)
198
199  val s2_hit_meta = RegEnable(s1_hit_meta, s1_fire)
200  val s2_hit_coh = RegEnable(s1_hit_coh, s1_fire)
201  val s2_has_permission = s2_hit_coh.onAccess(s2_req.cmd)._1
202  val s2_new_hit_coh = s2_hit_coh.onAccess(s2_req.cmd)._3
203
204  val s2_hit = s2_tag_match && s2_has_permission && s2_hit_coh === s2_new_hit_coh
205
206  val s2_way_en = RegEnable(s1_way_en, s1_fire)
207  val s2_repl_coh = RegEnable(s1_repl_coh, s1_fire)
208  val s2_repl_tag = RegEnable(s1_repl_tag, s1_fire)
209
210  // when req got nacked, upper levels should replay this request
211  // nacked or not
212  val s2_nack_hit = RegEnable(s1_nack, s1_fire)
213  // can no allocate mshr for load miss
214  val s2_nack_no_mshr = io.miss_req.valid && !io.miss_req.ready
215  // Bank conflict on data arrays
216  val s2_nack_data = RegEnable(!io.banked_data_read.ready, s1_fire)
217  val s2_nack = s2_nack_hit || s2_nack_no_mshr || s2_nack_data
218
219  val banked_data_resp = io.banked_data_resp
220  val s2_bank_addr = addr_to_dcache_bank(s2_addr)
221  val banked_data_resp_word = Mux1H(s2_bank_oh, io.banked_data_resp) // io.banked_data_resp(s2_bank_addr)
222  dontTouch(s2_bank_addr)
223
224  val s2_data_error = banked_data_resp_word.error
225  val s2_error = RegEnable(s1_error, s1_fire) || s2_data_error
226
227  val s2_instrtype = s2_req.instrtype
228
229  // only dump these signals when they are actually valid
230  dump_pipeline_valids("LoadPipe s2", "s2_hit", s2_valid && s2_hit)
231  dump_pipeline_valids("LoadPipe s2", "s2_nack", s2_valid && s2_nack)
232  dump_pipeline_valids("LoadPipe s2", "s2_nack_hit", s2_valid && s2_nack_hit)
233  dump_pipeline_valids("LoadPipe s2", "s2_nack_no_mshr", s2_valid && s2_nack_no_mshr)
234
235  val s2_can_send_miss_req = RegEnable(s1_will_send_miss_req, s1_fire)
236
237  // send load miss to miss queue
238  io.miss_req.valid := s2_valid && s2_can_send_miss_req
239  io.miss_req.bits := DontCare
240  io.miss_req.bits.source := s2_instrtype
241  io.miss_req.bits.cmd := s2_req.cmd
242  io.miss_req.bits.addr := get_block_addr(s2_addr)
243  io.miss_req.bits.vaddr := s2_vaddr
244  io.miss_req.bits.way_en := s2_way_en
245  io.miss_req.bits.req_coh := s2_hit_coh
246  io.miss_req.bits.replace_coh := s2_repl_coh
247  io.miss_req.bits.replace_tag := s2_repl_tag
248  io.miss_req.bits.cancel := io.lsu.s2_kill
249
250  // send back response
251  val resp = Wire(ValidIO(new DCacheWordResp))
252  resp.valid := s2_valid
253  resp.bits := DontCare
254  // resp.bits.data := s2_word_decoded
255  resp.bits.data := banked_data_resp_word.raw_data
256  // * on miss or nack, upper level should replay request
257  // but if we successfully sent the request to miss queue
258  // upper level does not need to replay request
259  // they can sit in load queue and wait for refill
260  //
261  // * report a miss if bank conflict is detected
262  val real_miss = !s2_hit
263  resp.bits.miss := real_miss || io.bank_conflict_slow
264  if (id == 0) {
265    // load pipe 0 will not be influenced by bank conflict
266    resp.bits.replay := resp.bits.miss && (!io.miss_req.fire() || s2_nack)
267  } else {
268    // load pipe 1 need replay when there is a bank conflict
269    resp.bits.replay := resp.bits.miss && (!io.miss_req.fire() || s2_nack) || io.bank_conflict_slow
270    XSPerfAccumulate("dcache_read_bank_conflict", io.bank_conflict_slow && s2_valid)
271  }
272  resp.bits.error := s2_error && s2_hit
273
274  io.lsu.resp.valid := resp.valid
275  io.lsu.resp.bits := resp.bits
276  assert(RegNext(!(resp.valid && !io.lsu.resp.ready)), "lsu should be ready in s2")
277
278  when (resp.valid) {
279    resp.bits.dump()
280  }
281
282  io.lsu.s1_hit_way := s1_tag_match_way
283  io.lsu.s1_disable_fast_wakeup := io.disable_ld_fast_wakeup
284  io.lsu.s1_bank_conflict := io.bank_conflict_fast
285  assert(RegNext(s1_ready && s2_ready), "load pipeline should never be blocked")
286
287  // report tag error (with paddr) to bus error unit
288  io.tag_error.ecc_error.valid := RegNext(s1_fire && s1_tag_ecc_error)
289  io.tag_error.ecc_error.bits := true.B
290  io.tag_error.paddr.valid := io.tag_error.ecc_error.valid
291  io.tag_error.paddr.bits := s2_addr
292
293  // -------
294  // Debug logging functions
295  def dump_pipeline_reqs(pipeline_stage_name: String, valid: Bool,
296    req: DCacheWordReq ) = {
297      when (valid) {
298        XSDebug(s"$pipeline_stage_name: ")
299        req.dump()
300      }
301  }
302
303  def dump_pipeline_valids(pipeline_stage_name: String, signal_name: String, valid: Bool) = {
304    when (valid) {
305      XSDebug(s"$pipeline_stage_name $signal_name\n")
306    }
307  }
308
309  // performance counters
310  XSPerfAccumulate("load_req", io.lsu.req.fire())
311  XSPerfAccumulate("load_s1_kill", s1_fire && io.lsu.s1_kill)
312  XSPerfAccumulate("load_hit_way", s1_fire && s1_tag_match)
313  XSPerfAccumulate("load_replay", io.lsu.resp.fire() && resp.bits.replay)
314  XSPerfAccumulate("load_replay_for_data_nack", io.lsu.resp.fire() && resp.bits.replay && s2_nack_data)
315  XSPerfAccumulate("load_replay_for_no_mshr", io.lsu.resp.fire() && resp.bits.replay && s2_nack_no_mshr)
316  XSPerfAccumulate("load_replay_for_conflict", io.lsu.resp.fire() && resp.bits.replay && io.bank_conflict_slow)
317  XSPerfAccumulate("load_hit", io.lsu.resp.fire() && !real_miss)
318  XSPerfAccumulate("load_miss", io.lsu.resp.fire() && real_miss)
319  XSPerfAccumulate("load_succeed", io.lsu.resp.fire() && !resp.bits.miss && !resp.bits.replay)
320  XSPerfAccumulate("load_miss_or_conflict", io.lsu.resp.fire() && resp.bits.miss)
321  XSPerfAccumulate("actual_ld_fast_wakeup", s1_fire && s1_tag_match && !io.disable_ld_fast_wakeup)
322  XSPerfAccumulate("ideal_ld_fast_wakeup", io.banked_data_read.fire() && s1_tag_match)
323
324  val perfEvents = Seq(
325    ("load_req                 ", io.lsu.req.fire()                                               ),
326    ("load_replay              ", io.lsu.resp.fire() && resp.bits.replay                          ),
327    ("load_replay_for_data_nack", io.lsu.resp.fire() && resp.bits.replay && s2_nack_data          ),
328    ("load_replay_for_no_mshr  ", io.lsu.resp.fire() && resp.bits.replay && s2_nack_no_mshr       ),
329    ("load_replay_for_conflict ", io.lsu.resp.fire() && resp.bits.replay && io.bank_conflict_slow ),
330  )
331  generatePerfEvent()
332}
333