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.{XSDebug, XSPerfAccumulate, PerfEventsBundle} 24 25class LoadPipe(id: Int)(implicit p: Parameters) extends DCacheModule { 26 def metaBits = (new Meta).getWidth 27 def encMetaBits = cacheParams.tagCode.width((new MetaAndTag).getWidth) - tagBits 28 def getMeta(encMeta: UInt): UInt = { 29 require(encMeta.getWidth == encMetaBits) 30 encMeta(metaBits - 1, 0) 31 } 32 33 val io = IO(new DCacheBundle { 34 // incoming requests 35 val lsu = Flipped(new DCacheLoadIO) 36 // req got nacked in stage 0? 37 val nack = Input(Bool()) 38 39 // meta and data array read port 40// val meta_read = DecoupledIO(new L1MetaReadReq) 41// val meta_resp = Input(Vec(nWays, UInt(encMetaBits.W))) 42 val meta_read = DecoupledIO(new MetaReadReq) 43 val meta_resp = Input(Vec(nWays, UInt(encMetaBits.W))) 44 45 val tag_read = DecoupledIO(new TagReadReq) 46 val tag_resp = Input(Vec(nWays, UInt(tagBits.W))) 47 48 val banked_data_read = DecoupledIO(new L1BankedDataReadReq) 49 val banked_data_resp = Input(Vec(DCacheBanks, new L1BankedDataReadResult())) 50 51 // banked data read conflict 52 val bank_conflict_slow = Input(Bool()) 53 val bank_conflict_fast = Input(Bool()) 54 55 // send miss request to miss queue 56 val miss_req = DecoupledIO(new MissReq) 57 58 // update state vec in replacement algo 59 val replace_access = ValidIO(new ReplacementAccessBundle) 60 // find the way to be replaced 61 val replace_way = new ReplacementWayReqIO 62 63 // load fast wakeup should be disabled when data read is not ready 64 val disable_ld_fast_wakeup = Input(Bool()) 65 }) 66 67 assert(RegNext(io.meta_read.ready)) 68 69 val s1_ready = Wire(Bool()) 70 val s2_ready = Wire(Bool()) 71 // LSU requests 72 // it you got nacked, you can directly passdown 73 val not_nacked_ready = io.meta_read.ready && io.tag_read.ready && s1_ready 74 val nacked_ready = true.B 75 76 // ready can wait for valid 77 io.lsu.req.ready := (!io.nack && not_nacked_ready) || (io.nack && nacked_ready) 78 io.meta_read.valid := io.lsu.req.fire() && !io.nack 79 io.tag_read.valid := io.lsu.req.fire() && !io.nack 80 81 val meta_read = io.meta_read.bits 82 val tag_read = io.tag_read.bits 83 84 // Tag read for new requests 85 meta_read.idx := get_idx(io.lsu.req.bits.addr) 86 meta_read.way_en := ~0.U(nWays.W) 87// meta_read.tag := DontCare 88 89 tag_read.idx := get_idx(io.lsu.req.bits.addr) 90 tag_read.way_en := ~0.U(nWays.W) 91 92 // Pipeline 93 // -------------------------------------------------------------------------------- 94 // stage 0 95 val s0_valid = io.lsu.req.fire() 96 val s0_req = io.lsu.req.bits 97 val s0_fire = s0_valid && s1_ready 98 99 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!") 100 dump_pipeline_reqs("LoadPipe s0", s0_valid, s0_req) 101 102 // -------------------------------------------------------------------------------- 103 // stage 1 104 val s1_valid = RegInit(false.B) 105 val s1_req = RegEnable(s0_req, s0_fire) 106 // in stage 1, load unit gets the physical address 107 val s1_addr = io.lsu.s1_paddr 108 val s1_vaddr = s1_req.addr 109 val s1_bank_oh = UIntToOH(addr_to_dcache_bank(s1_req.addr)) 110 val s1_nack = RegNext(io.nack) 111 val s1_nack_data = !io.banked_data_read.ready 112 val s1_fire = s1_valid && s2_ready 113 s1_ready := !s1_valid || s1_fire 114 115 when (s0_fire) { s1_valid := true.B } 116 .elsewhen (s1_fire) { s1_valid := false.B } 117 118 dump_pipeline_reqs("LoadPipe s1", s1_valid, s1_req) 119 120 // tag check 121 val meta_resp = VecInit(io.meta_resp.map(r => getMeta(r).asTypeOf(new Meta))) 122 def wayMap[T <: Data](f: Int => T) = VecInit((0 until nWays).map(f)) 123 val s1_tag_eq_way = wayMap((w: Int) => io.tag_resp(w) === (get_tag(s1_addr))).asUInt 124 val s1_tag_match_way = wayMap((w: Int) => s1_tag_eq_way(w) && meta_resp(w).coh.isValid()).asUInt 125 val s1_tag_match = s1_tag_match_way.orR 126 assert(RegNext(!s1_valid || PopCount(s1_tag_match_way) <= 1.U), "tag should not match with more than 1 way") 127 128 val s1_fake_meta = Wire(new Meta) 129// s1_fake_meta.tag := get_tag(s1_addr) 130 s1_fake_meta.coh := ClientMetadata.onReset 131 val s1_fake_tag = get_tag(s1_addr) 132 133 // when there are no tag match, we give it a Fake Meta 134 // this simplifies our logic in s2 stage 135 val s1_hit_meta = Mux(s1_tag_match, Mux1H(s1_tag_match_way, wayMap((w: Int) => meta_resp(w))), s1_fake_meta) 136 val s1_hit_coh = s1_hit_meta.coh 137 138 io.replace_way.set.valid := RegNext(s0_fire) 139 io.replace_way.set.bits := get_idx(s1_vaddr) 140 val s1_repl_way_en = UIntToOH(io.replace_way.way) 141 val s1_repl_tag = Mux1H(s1_repl_way_en, wayMap(w => io.tag_resp(w))) 142 val s1_repl_coh = Mux1H(s1_repl_way_en, wayMap(w => meta_resp(w).coh)) 143 144 val s1_need_replacement = !s1_tag_match 145 val s1_way_en = Mux(s1_need_replacement, s1_repl_way_en, s1_tag_match_way) 146 val s1_coh = Mux(s1_need_replacement, s1_repl_coh, s1_hit_coh) 147 val s1_tag = Mux(s1_need_replacement, s1_repl_tag, get_tag(s1_addr)) 148 149 // data read 150 io.banked_data_read.valid := s1_fire && !s1_nack 151 io.banked_data_read.bits.addr := s1_vaddr 152 io.banked_data_read.bits.way_en := s1_tag_match_way 153 154 io.replace_access.valid := RegNext(RegNext(io.meta_read.fire()) && s1_tag_match && s1_valid) 155 io.replace_access.bits.set := RegNext(get_idx(s1_req.addr)) 156 io.replace_access.bits.way := RegNext(OHToUInt(s1_tag_match_way)) 157 158 // TODO: optimize implementation 159 val s1_has_permission = s1_hit_coh.onAccess(s1_req.cmd)._1 160 val s1_new_hit_coh = s1_hit_coh.onAccess(s1_req.cmd)._3 161 val s1_hit = s1_tag_match && s1_has_permission && s1_hit_coh === s1_new_hit_coh 162 val s1_will_send_miss_req = s1_valid && !s1_nack && !s1_nack_data && !s1_hit 163 164 // tag ecc check 165 // (0 until nWays).foreach(w => assert(!RegNext(s1_valid && s1_tag_match_way(w) && cacheParams.tagCode.decode(io.meta_resp(w)).uncorrectable))) 166 167 // -------------------------------------------------------------------------------- 168 // stage 2 169 // val s2_valid = RegEnable(next = s1_valid && !io.lsu.s1_kill, init = false.B, enable = s1_fire) 170 val s2_valid = RegInit(false.B) 171 val s2_req = RegEnable(s1_req, s1_fire) 172 val s2_addr = RegEnable(s1_addr, s1_fire) 173 val s2_vaddr = RegEnable(s1_vaddr, s1_fire) 174 val s2_bank_oh = RegEnable(s1_bank_oh, s1_fire) 175 s2_ready := true.B 176 177 when (s1_fire) { s2_valid := !io.lsu.s1_kill } 178 .elsewhen(io.lsu.resp.fire()) { s2_valid := false.B } 179 180 dump_pipeline_reqs("LoadPipe s2", s2_valid, s2_req) 181 182 // hit, miss, nack, permission checking 183 val s2_tag_match_way = RegEnable(s1_tag_match_way, s1_fire) 184 val s2_tag_match = RegEnable(s1_tag_match, s1_fire) 185 186 val s2_hit_meta = RegEnable(s1_hit_meta, s1_fire) 187 val s2_hit_coh = RegEnable(s1_hit_coh, s1_fire) 188 val s2_has_permission = s2_hit_coh.onAccess(s2_req.cmd)._1 189 val s2_new_hit_coh = s2_hit_coh.onAccess(s2_req.cmd)._3 190 191 val s2_hit = s2_tag_match && s2_has_permission && s2_hit_coh === s2_new_hit_coh 192 193 val s2_way_en = RegEnable(s1_way_en, s1_fire) 194 val s2_repl_coh = RegEnable(s1_repl_coh, s1_fire) 195 val s2_repl_tag = RegEnable(s1_repl_tag, s1_fire) 196 197 // when req got nacked, upper levels should replay this request 198 // nacked or not 199 val s2_nack_hit = RegEnable(s1_nack, s1_fire) 200 // can no allocate mshr for load miss 201 val s2_nack_no_mshr = io.miss_req.valid && !io.miss_req.ready 202 // Bank conflict on data arrays 203 val s2_nack_data = RegEnable(!io.banked_data_read.ready, s1_fire) 204 val s2_nack = s2_nack_hit || s2_nack_no_mshr || s2_nack_data 205 206 val banked_data_resp = io.banked_data_resp 207 val s2_bank_addr = addr_to_dcache_bank(s2_addr) 208 val banked_data_resp_word = Mux1H(s2_bank_oh, io.banked_data_resp) // io.banked_data_resp(s2_bank_addr) 209 dontTouch(s2_bank_addr) 210 211 val s2_instrtype = s2_req.instrtype 212 213 // only dump these signals when they are actually valid 214 dump_pipeline_valids("LoadPipe s2", "s2_hit", s2_valid && s2_hit) 215 dump_pipeline_valids("LoadPipe s2", "s2_nack", s2_valid && s2_nack) 216 dump_pipeline_valids("LoadPipe s2", "s2_nack_hit", s2_valid && s2_nack_hit) 217 dump_pipeline_valids("LoadPipe s2", "s2_nack_no_mshr", s2_valid && s2_nack_no_mshr) 218 219 val s2_can_send_miss_req = RegEnable(s1_will_send_miss_req, s1_fire) 220 221 // send load miss to miss queue 222 io.miss_req.valid := s2_valid && s2_can_send_miss_req 223 io.miss_req.bits := DontCare 224 io.miss_req.bits.source := s2_instrtype 225 io.miss_req.bits.cmd := s2_req.cmd 226 io.miss_req.bits.addr := get_block_addr(s2_addr) 227 io.miss_req.bits.vaddr := s2_vaddr 228 io.miss_req.bits.way_en := s2_way_en 229 io.miss_req.bits.req_coh := s2_hit_coh 230 io.miss_req.bits.replace_coh := s2_repl_coh 231 io.miss_req.bits.replace_tag := s2_repl_tag 232 io.miss_req.bits.cancel := io.lsu.s2_kill 233 234 // send back response 235 val resp = Wire(ValidIO(new DCacheWordResp)) 236 resp.valid := s2_valid 237 resp.bits := DontCare 238 // resp.bits.data := s2_word_decoded 239 resp.bits.data := banked_data_resp_word.raw_data 240 // * on miss or nack, upper level should replay request 241 // but if we successfully sent the request to miss queue 242 // upper level does not need to replay request 243 // they can sit in load queue and wait for refill 244 // 245 // * report a miss if bank conflict is detected 246 val real_miss = !s2_hit || s2_nack 247 resp.bits.miss := real_miss || io.bank_conflict_slow 248 if (id == 0) { 249 // load pipe 0 will not be influenced by bank conflict 250 resp.bits.replay := resp.bits.miss && (!io.miss_req.fire() || s2_nack) 251 } else { 252 // load pipe 1 need replay when there is a bank conflict 253 resp.bits.replay := resp.bits.miss && (!io.miss_req.fire() || s2_nack) || io.bank_conflict_slow 254 XSPerfAccumulate("dcache_read_bank_conflict", io.bank_conflict_slow && s2_valid) 255 } 256 257 resp.bits.miss_enter := io.miss_req.fire() 258 259 io.lsu.resp.valid := resp.valid 260 io.lsu.resp.bits := resp.bits 261 assert(RegNext(!(resp.valid && !io.lsu.resp.ready)), "lsu should be ready in s2") 262 263 when (resp.valid) { 264 resp.bits.dump() 265 } 266 267 io.lsu.s1_hit_way := s1_tag_match_way 268 io.lsu.s1_disable_fast_wakeup := io.disable_ld_fast_wakeup 269 io.lsu.s1_bank_conflict := io.bank_conflict_fast 270 assert(RegNext(s1_ready && s2_ready), "load pipeline should never be blocked") 271 272 // ------- 273 // Debug logging functions 274 def dump_pipeline_reqs(pipeline_stage_name: String, valid: Bool, 275 req: DCacheWordReq ) = { 276 when (valid) { 277 XSDebug(s"$pipeline_stage_name: ") 278 req.dump() 279 } 280 } 281 282 def dump_pipeline_valids(pipeline_stage_name: String, signal_name: String, valid: Bool) = { 283 when (valid) { 284 XSDebug(s"$pipeline_stage_name $signal_name\n") 285 } 286 } 287 288 // performance counters 289 XSPerfAccumulate("load_req", io.lsu.req.fire()) 290 XSPerfAccumulate("load_s1_kill", s1_fire && io.lsu.s1_kill) 291 XSPerfAccumulate("load_hit_way", s1_fire && s1_tag_match) 292 XSPerfAccumulate("load_replay", io.lsu.resp.fire() && resp.bits.replay) 293 XSPerfAccumulate("load_replay_for_data_nack", io.lsu.resp.fire() && resp.bits.replay && s2_nack_data) 294 XSPerfAccumulate("load_replay_for_no_mshr", io.lsu.resp.fire() && resp.bits.replay && s2_nack_no_mshr) 295 XSPerfAccumulate("load_replay_for_conflict", io.lsu.resp.fire() && resp.bits.replay && io.bank_conflict_slow) 296 XSPerfAccumulate("load_hit", io.lsu.resp.fire() && !real_miss) 297 XSPerfAccumulate("load_miss", io.lsu.resp.fire() && real_miss) 298 XSPerfAccumulate("load_succeed", io.lsu.resp.fire() && !resp.bits.miss) 299 XSPerfAccumulate("load_miss_or_conflict", io.lsu.resp.fire() && resp.bits.miss) 300 XSPerfAccumulate("actual_ld_fast_wakeup", s1_fire && s1_tag_match && !io.disable_ld_fast_wakeup) 301 XSPerfAccumulate("ideal_ld_fast_wakeup", io.banked_data_read.fire() && s1_tag_match) 302 303 val perfinfo = IO(new Bundle(){ 304 val perfEvents = Output(new PerfEventsBundle(5)) 305 }) 306 val perfEvents = Seq( 307 ("load_req ", io.lsu.req.fire() ), 308 ("load_replay ", io.lsu.resp.fire() && resp.bits.replay ), 309 ("load_replay_for_data_nack ", io.lsu.resp.fire() && resp.bits.replay && s2_nack_data ), 310 ("load_replay_for_no_mshr ", io.lsu.resp.fire() && resp.bits.replay && s2_nack_no_mshr ), 311 ("load_replay_for_conflict ", io.lsu.resp.fire() && resp.bits.replay && io.bank_conflict_slow ), 312 ) 313 314 for (((perf_out,(perf_name,perf)),i) <- perfinfo.perfEvents.perf_events.zip(perfEvents).zipWithIndex) { 315 perf_out.incr_step := RegNext(perf) 316 } 317} 318