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.frontend.icache 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import difftest._ 23import freechips.rocketchip.tilelink.ClientStates 24import xiangshan._ 25import xiangshan.cache.mmu._ 26import utils._ 27import utility._ 28import xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle} 29import xiangshan.frontend.{FtqICacheInfo, FtqToICacheRequestBundle} 30 31class ICacheMainPipeReq(implicit p: Parameters) extends ICacheBundle 32{ 33 val vaddr = UInt(VAddrBits.W) 34 def vsetIdx = get_idx(vaddr) 35} 36 37class ICacheMainPipeResp(implicit p: Parameters) extends ICacheBundle 38{ 39 val vaddr = UInt(VAddrBits.W) 40 // val registerData = UInt(blockBits.W) 41 // val sramData = UInt(blockBits.W) 42 // val select = Bool() 43 val data = UInt((blockBits/2).W) 44 val paddr = UInt(PAddrBits.W) 45 val tlbExcp = new Bundle{ 46 val pageFault = Bool() 47 val accessFault = Bool() 48 val mmio = Bool() 49 } 50} 51 52class ICacheMainPipeBundle(implicit p: Parameters) extends ICacheBundle 53{ 54 val req = Flipped(Decoupled(new FtqToICacheRequestBundle)) 55 val resp = Vec(PortNumber, ValidIO(new ICacheMainPipeResp)) 56 val topdownIcacheMiss = Output(Bool()) 57 val topdownItlbMiss = Output(Bool()) 58} 59 60class ICacheMetaReqBundle(implicit p: Parameters) extends ICacheBundle{ 61 val toIMeta = DecoupledIO(new ICacheReadBundle) 62 val fromIMeta = Input(new ICacheMetaRespBundle) 63} 64 65class ICacheDataReqBundle(implicit p: Parameters) extends ICacheBundle{ 66 val toIData = DecoupledIO(Vec(partWayNum, new ICacheReadBundle)) 67 val fromIData = Input(new ICacheDataRespBundle) 68} 69 70class ICacheMSHRBundle(implicit p: Parameters) extends ICacheBundle{ 71 val toMSHR = Decoupled(new ICacheMissReq) 72 val fromMSHR = Flipped(ValidIO(new ICacheMissResp)) 73} 74 75class ICachePMPBundle(implicit p: Parameters) extends ICacheBundle{ 76 val req = Valid(new PMPReqBundle()) 77 val resp = Input(new PMPRespBundle()) 78} 79 80class ICachePerfInfo(implicit p: Parameters) extends ICacheBundle{ 81 val only_0_hit = Bool() 82 val only_0_miss = Bool() 83 val hit_0_hit_1 = Bool() 84 val hit_0_miss_1 = Bool() 85 val miss_0_hit_1 = Bool() 86 val miss_0_miss_1 = Bool() 87 val hit_0_except_1 = Bool() 88 val miss_0_except_1 = Bool() 89 val except_0 = Bool() 90 val bank_hit = Vec(2,Bool()) 91 val hit = Bool() 92} 93 94class ICacheMainPipeInterface(implicit p: Parameters) extends ICacheBundle { 95 val hartId = Input(UInt(hartIdLen.W)) 96 /*** internal interface ***/ 97 val metaArray = new ICacheMetaReqBundle 98 val dataArray = new ICacheDataReqBundle 99 /** prefetch io */ 100 val IPFBufferRead = Flipped(new IPFBufferRead) 101 val PIQRead = Flipped(new PIQRead) 102 103 val IPFReplacer = Flipped(new IPFReplacer) 104 val ICacheMainPipeInfo = new ICacheMainPipeInfo 105 106 val mshr = Vec(PortNumber, new ICacheMSHRBundle) 107 val errors = Output(Vec(PortNumber, new L1CacheErrorInfo)) 108 /*** outside interface ***/ 109 //val fetch = Vec(PortNumber, new ICacheMainPipeBundle) 110 /* when ftq.valid is high in T + 1 cycle 111 * the ftq component must be valid in T cycle 112 */ 113 val fetch = new ICacheMainPipeBundle 114 val pmp = Vec(PortNumber, new ICachePMPBundle) 115 val itlb = Vec(PortNumber, new TlbRequestIO) 116 val respStall = Input(Bool()) 117 val perfInfo = Output(new ICachePerfInfo) 118 119 val csr_parity_enable = Input(Bool()) 120} 121 122class ICacheDB(implicit p: Parameters) extends ICacheBundle { 123 val blk_vaddr = UInt((VAddrBits - blockOffBits).W) 124 val blk_paddr = UInt((PAddrBits - blockOffBits).W) 125 val hit = Bool() 126} 127 128class ICacheMainPipe(implicit p: Parameters) extends ICacheModule 129{ 130 val io = IO(new ICacheMainPipeInterface) 131 132 /** Input/Output port */ 133 val (fromFtq, toIFU) = (io.fetch.req, io.fetch.resp) 134 val (toMeta, metaResp) = (io.metaArray.toIMeta, io.metaArray.fromIMeta) 135 val (toData, dataResp) = (io.dataArray.toIData, io.dataArray.fromIData) 136 val (toIPF, fromIPF) = (io.IPFBufferRead.req, io.IPFBufferRead.resp) 137 val (toPIQ, fromPIQ) = (io.PIQRead.req, io.PIQRead.resp) 138 val (toMSHR, fromMSHR) = (io.mshr.map(_.toMSHR), io.mshr.map(_.fromMSHR)) 139 val (toITLB, fromITLB) = (io.itlb.map(_.req), io.itlb.map(_.resp)) 140 val (toPMP, fromPMP) = (io.pmp.map(_.req), io.pmp.map(_.resp)) 141 142 val IPFReplacer = io.IPFReplacer 143 val toIPrefetch = io.ICacheMainPipeInfo 144 145 146 // Statistics on the frequency distribution of FTQ fire interval 147 val cntFtqFireInterval = RegInit(0.U(32.W)) 148 cntFtqFireInterval := Mux(fromFtq.fire, 1.U, cntFtqFireInterval + 1.U) 149 XSPerfHistogram("ftq2icache_fire_" + p(XSCoreParamsKey).HartId.toString, 150 cntFtqFireInterval, fromFtq.fire, 151 1, 300, 1, right_strict = true) 152 153 // Ftq RegNext Register 154 val fromFtqReq = fromFtq.bits.pcMemRead 155 156 /** pipeline control signal */ 157 val s1_ready, s2_ready = Wire(Bool()) 158 val s0_fire, s1_fire , s2_fire = Wire(Bool()) 159 160 /** 161 ****************************************************************************** 162 * ICache Stage 0 163 * - send req to ITLB and wait for tlb miss fixing 164 * - send req to Meta/Data SRAM 165 ****************************************************************************** 166 */ 167 168 /** s0 control */ 169 val s0_valid = fromFtq.valid 170 val s0_req_vaddr = (0 until partWayNum + 1).map(i => VecInit(Seq(fromFtqReq(i).startAddr, fromFtqReq(i).nextlineStart))) 171 val s0_req_vsetIdx = (0 until partWayNum + 1).map(i => VecInit(s0_req_vaddr(i).map(get_idx(_)))) 172 val s0_only_first = (0 until partWayNum + 1).map(i => fromFtq.bits.readValid(i) && !fromFtqReq(i).crossCacheline) 173 val s0_double_line = (0 until partWayNum + 1).map(i => fromFtq.bits.readValid(i) && fromFtqReq(i).crossCacheline) 174 175 val s0_final_valid = s0_valid 176 val s0_final_vaddr = s0_req_vaddr.head 177 val s0_final_vsetIdx = s0_req_vsetIdx.head 178 val s0_final_only_first = s0_only_first.head 179 val s0_final_double_line = s0_double_line.head 180 181 /** SRAM request */ 182 // 0,1,2,3 -> dataArray(data); 3 -> dataArray(code); 0 -> metaArray; 4 -> itlb 183 val ftq_req_to_data_doubleline = s0_double_line.init 184 val ftq_req_to_data_vset_idx = s0_req_vsetIdx.init 185 val ftq_req_to_data_valid = fromFtq.bits.readValid.init 186 187 val ftq_req_to_meta_doubleline = s0_double_line.head 188 val ftq_req_to_meta_vset_idx = s0_req_vsetIdx.head 189 val ftq_req_to_meta_valid = fromFtq.bits.readValid.head 190 191 val ftq_req_to_itlb_only_first = s0_only_first.last 192 val ftq_req_to_itlb_doubleline = s0_double_line.last 193 val ftq_req_to_itlb_vaddr = s0_req_vaddr.last 194 val ftq_req_to_itlb_vset_idx = s0_req_vsetIdx.last 195 196 /** Data request */ 197 for(i <- 0 until partWayNum) { 198 toData.valid := ftq_req_to_data_valid(i) 199 toData.bits(i).isDoubleLine := ftq_req_to_data_doubleline(i) 200 toData.bits(i).vSetIdx := ftq_req_to_data_vset_idx(i) 201 } 202 203 /** Meta request */ 204 toMeta.valid := ftq_req_to_meta_valid 205 toMeta.bits.isDoubleLine := ftq_req_to_meta_doubleline 206 toMeta.bits.vSetIdx := ftq_req_to_meta_vset_idx 207 208 val toITLB_s0_valid = VecInit(Seq(s0_valid, s0_valid && ftq_req_to_itlb_doubleline)) 209 val toITLB_s0_size = VecInit(Seq(3.U, 3.U)) // TODO: fix the size 210 val toITLB_s0_vaddr = ftq_req_to_itlb_vaddr 211 val toITLB_s0_debug_pc = ftq_req_to_itlb_vaddr 212 213 val itlb_can_go = toITLB(0).ready && toITLB(1).ready 214 val icache_can_go = toData.ready && toMeta.ready 215 val pipe_can_go = s1_ready 216 val s0_can_go = itlb_can_go && icache_can_go && pipe_can_go 217 s0_fire := s0_valid && s0_can_go 218 219 //TODO: fix GTimer() condition 220 fromFtq.ready := s0_can_go 221 222 /** 223 ****************************************************************************** 224 * ICache Stage 1 225 * - get tlb resp data (exceptiong info and physical addresses) 226 * - get Meta/Data SRAM read responses (latched for pipeline stop) 227 * - tag compare/hit check 228 * - check ipf and piq 229 ****************************************************************************** 230 */ 231 232 /** s1 control */ 233 val s1_valid = generatePipeControl(lastFire = s0_fire, thisFire = s1_fire, thisFlush = false.B, lastFlush = false.B) 234 235 val s1_req_vaddr = RegEnable(s0_final_vaddr, s0_fire) 236 val s1_req_vsetIdx = RegEnable(s0_final_vsetIdx, s0_fire) 237 val s1_double_line = RegEnable(s0_final_double_line, s0_fire) 238 239 /** tlb request and response */ 240 fromITLB.foreach(_.ready := true.B) 241 val s1_wait_itlb = RegInit(VecInit(Seq.fill(PortNumber)(false.B))) 242 243 (0 until PortNumber).foreach { i => 244 when(RegNext(s0_fire) && fromITLB(i).bits.miss) { 245 s1_wait_itlb(i) := true.B 246 }.elsewhen(s1_wait_itlb(i) && !fromITLB(i).bits.miss) { 247 s1_wait_itlb(i) := false.B 248 } 249 } 250 251 val s1_need_itlb = Seq((RegNext(s0_fire) || s1_wait_itlb(0)) && fromITLB(0).bits.miss, 252 (RegNext(s0_fire) || s1_wait_itlb(1)) && fromITLB(1).bits.miss && s1_double_line) 253 val toITLB_s1_valid = s1_need_itlb 254 val toITLB_s1_size = VecInit(Seq(3.U, 3.U)) // TODO: fix the size 255 val toITLB_s1_vaddr = s1_req_vaddr 256 val toITLB_s1_debug_pc = s1_req_vaddr 257 258 // chose tlb req between s0 and s1 259 for (i <- 0 until PortNumber) { 260 toITLB(i).valid := Mux(s1_need_itlb(i), toITLB_s1_valid(i), toITLB_s0_valid(i)) 261 toITLB(i).bits.size := Mux(s1_need_itlb(i), toITLB_s1_size(i), toITLB_s0_size(i)) 262 toITLB(i).bits.vaddr := Mux(s1_need_itlb(i), toITLB_s1_vaddr(i), toITLB_s0_vaddr(i)) 263 toITLB(i).bits.debug.pc := Mux(s1_need_itlb(i), toITLB_s1_debug_pc(i), toITLB_s0_debug_pc(i)) 264 } 265 toITLB.map{port => 266 port.bits.cmd := TlbCmd.exec 267 port.bits.memidx := DontCare 268 port.bits.debug.robIdx := DontCare 269 port.bits.no_translate := false.B 270 port.bits.debug.isFirstIssue := DontCare 271 port.bits.kill := DontCare 272 } 273 io.itlb.foreach(_.req_kill := false.B) 274 275 /** tlb response latch for pipeline stop */ 276 // val tlb_valid_tmp = VecInit((0 until PortNumber).map(i => 277 // (RegNext(s0_fire) || s1_wait_itlb(i)) && !fromITLB(i).bits.miss)) 278 val tlb_valid_tmp = VecInit(Seq((RegNext(s0_fire) || s1_wait_itlb(0)) && !fromITLB(0).bits.miss, 279 (RegNext(s0_fire) || s1_wait_itlb(1)) && !fromITLB(1).bits.miss && s1_double_line)) 280 val tlbRespPAddr = VecInit((0 until PortNumber).map(i => 281 ResultHoldBypass(valid = tlb_valid_tmp(i), data = fromITLB(i).bits.paddr(0)))) 282 val tlbExcpPF = VecInit((0 until PortNumber).map(i => 283 ResultHoldBypass(valid = tlb_valid_tmp(i), data = fromITLB(i).bits.excp(0).pf.instr))) 284 val tlbExcpAF = VecInit((0 until PortNumber).map(i => 285 ResultHoldBypass(valid = tlb_valid_tmp(i), data = fromITLB(i).bits.excp(0).af.instr))) 286 val tlbExcp = VecInit((0 until PortNumber).map(i => tlbExcpAF(i) || tlbExcpPF(i))) 287 288 val s1_tlb_valid = VecInit((0 until PortNumber).map(i => ValidHoldBypass(tlb_valid_tmp(i), s1_fire))) 289 val tlbRespAllValid = s1_tlb_valid(0) && (!s1_double_line || s1_double_line && s1_tlb_valid(1)) 290 291 292 def numOfStage = 3 293 val itlbMissStage = RegInit(VecInit(Seq.fill(numOfStage - 1)(0.B))) 294 itlbMissStage(0) := !tlbRespAllValid 295 for (i <- 1 until numOfStage - 1) { 296 itlbMissStage(i) := itlbMissStage(i - 1) 297 } 298 299 /** s1 hit check/tag compare */ 300 val s1_req_paddr = tlbRespPAddr 301 val s1_req_ptags = VecInit(s1_req_paddr.map(get_phy_tag(_))) 302 303 val s1_meta_ptags = ResultHoldBypass(data = metaResp.tags, valid = RegNext(s0_fire)) 304 val s1_meta_valids = ResultHoldBypass(data = metaResp.entryValid, valid = RegNext(s0_fire)) 305 val s1_meta_errors = ResultHoldBypass(data = metaResp.errors, valid = RegNext(s0_fire)) 306 307 val s1_data_cacheline = ResultHoldBypass(data = dataResp.datas, valid = RegNext(s0_fire)) 308 val s1_data_errorBits = ResultHoldBypass(data = dataResp.codes, valid = RegNext(s0_fire)) 309 310 val s1_tag_eq_vec = VecInit((0 until PortNumber).map( p => VecInit((0 until nWays).map( w => s1_meta_ptags(p)(w) === s1_req_ptags(p))))) 311 val s1_tag_match_vec = VecInit((0 until PortNumber).map( k => VecInit(s1_tag_eq_vec(k).zipWithIndex.map{ case(way_tag_eq, w) => way_tag_eq && s1_meta_valids(k)(w)}))) 312 val s1_tag_match = VecInit(s1_tag_match_vec.map(vector => ParallelOR(vector))) 313 314 val s1_port_hit = VecInit(Seq(s1_tag_match(0) && s1_valid, s1_tag_match(1) && s1_valid && s1_double_line)) 315 316 /** choose victim cacheline */ 317 val bank_vsetIdx = VecInit((0 until PortNumber).map( i => Mux(s1_req_vsetIdx(i)(0), s1_req_vsetIdx(1)(highestIdxBit, 1), s1_req_vsetIdx(0)(highestIdxBit, 1)))) 318 val replacers = Seq.fill(PortNumber)(ReplacementPolicy.fromString(cacheParams.replacer,nWays,nSets/PortNumber)) 319 val bank_victim_oh = ResultHoldBypass(data = VecInit(replacers.zipWithIndex.map{case (replacer, i) => UIntToOH(replacer.way(bank_vsetIdx(i)))}), valid = RegNext(s0_fire)) 320 val s1_victim_oh = VecInit((0 until PortNumber).map( i => Mux(s1_req_vsetIdx(i)(0), bank_victim_oh(1), bank_victim_oh(0)))) 321 322 when(s1_fire){ 323 assert(PopCount(s1_tag_match_vec(0)) <= 1.U && (PopCount(s1_tag_match_vec(1)) <= 1.U || !s1_double_line), 324 "Multiple hit in main pipe, port0:is=%d,ptag=0x%x,vidx=0x%x,vaddr=0x%x port1:is=%d,ptag=0x%x,vidx=0x%x,vaddr=0x%x ", 325 PopCount(s1_tag_match_vec(0)) > 1.U,s1_req_ptags(0), get_idx(s1_req_vaddr(0)), s1_req_vaddr(0), 326 PopCount(s1_tag_match_vec(1)) > 1.U && s1_double_line, s1_req_ptags(1), get_idx(s1_req_vaddr(1)), s1_req_vaddr(1)) 327 } 328 329 /** check ipf, get result at the same cycle */ 330 (0 until PortNumber).foreach { i => 331 toIPF(i).valid := tlb_valid_tmp(i) 332 toIPF(i).bits.paddr := s1_req_paddr(i) 333 } 334 val s1_ipf_hit = VecInit((0 until PortNumber).map(i => toIPF(i).valid && fromIPF(i).ipf_hit)) 335 val s1_ipf_hit_latch = VecInit((0 until PortNumber).map(i => holdReleaseLatch(valid = s1_ipf_hit(i), release = s1_fire, flush = false.B))) 336 val s1_ipf_data = VecInit((0 until PortNumber).map(i => ResultHoldBypass(data = fromIPF(i).cacheline, valid = s1_ipf_hit(i)))) 337 338 /** check in PIQ, if hit, wait until prefetch port hit */ 339 (0 until PortNumber).foreach { i => 340 toPIQ(i).valid := tlb_valid_tmp(i) 341 toPIQ(i).bits.paddr := s1_req_paddr(i) 342 } 343 val s1_piq_hit = VecInit((0 until PortNumber).map(i => toIPF(i).valid && fromPIQ(i).piq_hit)) 344 val s1_piq_hit_latch = VecInit((0 until PortNumber).map(i => holdReleaseLatch(valid = s1_piq_hit(i), release = s1_fire, flush = false.B))) 345 val wait_piq = VecInit((0 until PortNumber).map(i => toIPF(i).valid && fromPIQ(i).piq_hit && !fromPIQ(i).data_valid)) 346 val wait_piq_latch = VecInit((0 until PortNumber).map(i => holdReleaseLatch(valid = wait_piq(i), release = s1_fire || fromPIQ(i).data_valid, flush = false.B))) 347 val s1_piq_data = VecInit((0 until PortNumber).map(i => ResultHoldBypass(data = fromPIQ(i).cacheline, valid = (s1_piq_hit(i) || wait_piq_latch(i)) && fromPIQ(i).data_valid))) 348 349 val s1_wait = (0 until PortNumber).map(i => wait_piq_latch(i) && !fromPIQ(i).data_valid).reduce(_||_) 350 351 val s1_prefetch_hit = VecInit((0 until PortNumber).map(i => s1_ipf_hit_latch(i) || s1_piq_hit_latch(i))) 352 val s1_prefetch_hit_data = VecInit((0 until PortNumber).map(i => Mux(s1_ipf_hit_latch(i), s1_ipf_data(i), s1_piq_data(i)))) 353 354 s1_ready := s2_ready && tlbRespAllValid && !s1_wait || !s1_valid 355 s1_fire := s1_valid && tlbRespAllValid && s2_ready && !s1_wait 356 357 // record cacheline log 358 val isWriteICacheTable = WireInit(Constantin.createRecord("isWriteICacheTable" + p(XSCoreParamsKey).HartId.toString)) 359 val ICacheTable = ChiselDB.createTable("ICacheTable" + p(XSCoreParamsKey).HartId.toString, new ICacheDB) 360 361 val ICacheDumpData_req0 = Wire(new ICacheDB) 362 ICacheDumpData_req0.blk_paddr := getBlkAddr(s1_req_paddr(0)) 363 ICacheDumpData_req0.blk_vaddr := getBlkAddr(s1_req_vaddr(0)) 364 ICacheDumpData_req0.hit := s1_port_hit(0) || s1_prefetch_hit(0) 365 ICacheTable.log( 366 data = ICacheDumpData_req0, 367 en = isWriteICacheTable.orR && s1_fire, 368 clock = clock, 369 reset = reset 370 ) 371 372 val ICacheDumpData_req1 = Wire(new ICacheDB) 373 ICacheDumpData_req1.blk_paddr := getBlkAddr(s1_req_paddr(1)) 374 ICacheDumpData_req1.blk_vaddr := getBlkAddr(s1_req_vaddr(1)) 375 ICacheDumpData_req1.hit := s1_port_hit(1) || s1_prefetch_hit(1) 376 ICacheTable.log( 377 data = ICacheDumpData_req1, 378 en = isWriteICacheTable.orR && s1_fire && s1_double_line, 379 clock = clock, 380 reset = reset 381 ) 382 383 /** <PERF> replace victim way number */ 384 385 (0 until nWays).map{ w => 386 XSPerfAccumulate("line_0_hit_way_" + Integer.toString(w, 10), s1_fire && s1_port_hit(0) && OHToUInt(s1_tag_match_vec(0)) === w.U) 387 } 388 389 (0 until nWays).map{ w => 390 XSPerfAccumulate("line_0_victim_way_" + Integer.toString(w, 10), s1_fire && !s1_port_hit(0) && OHToUInt(s1_victim_oh(0)) === w.U) 391 } 392 393 (0 until nWays).map{ w => 394 XSPerfAccumulate("line_1_hit_way_" + Integer.toString(w, 10), s1_fire && s1_double_line && s1_port_hit(1) && OHToUInt(s1_tag_match_vec(1)) === w.U) 395 } 396 397 (0 until nWays).map{ w => 398 XSPerfAccumulate("line_1_victim_way_" + Integer.toString(w, 10), s1_fire && s1_double_line && !s1_port_hit(1) && OHToUInt(s1_victim_oh(1)) === w.U) 399 } 400 401 XSPerfAccumulate("mainPipe_stage1_block_by_piq_cycles", s1_valid && s1_wait) 402 403 /** 404 ****************************************************************************** 405 * ICache Stage 2 406 * - send request to MSHR if ICache miss 407 * - generate secondary miss status/data registers 408 * - response to IFU 409 ****************************************************************************** 410 */ 411 412 /** s2 control */ 413 val s2_fetch_finish = Wire(Bool()) 414 415 val s2_valid = generatePipeControl(lastFire = s1_fire, thisFire = s2_fire, thisFlush = false.B, lastFlush = false.B) 416 417 s2_ready := (s2_valid && s2_fetch_finish && !io.respStall) || !s2_valid 418 s2_fire := s2_valid && s2_fetch_finish && !io.respStall 419 420 /** s2 data */ 421 // val mmio = fromPMP.map(port => port.mmio) // TODO: handle it 422 val (s2_req_paddr , s2_req_vaddr) = (RegEnable(s1_req_paddr, s1_fire), RegEnable(s1_req_vaddr, s1_fire)) 423 val s2_req_vsetIdx = RegEnable(s1_req_vsetIdx, s1_fire) 424 val s2_req_ptags = RegEnable(s1_req_ptags, s1_fire) 425 val s2_double_line = RegEnable(s1_double_line, s1_fire) 426 val s2_port_hit = RegEnable(s1_port_hit, s1_fire) 427 val s2_waymask = RegEnable(s1_victim_oh, s1_fire) 428 val s2_tag_match_vec = RegEnable(s1_tag_match_vec, s1_fire) 429 430 val s2_meta_errors = RegEnable(s1_meta_errors, s1_fire) 431 val s2_data_errorBits = RegEnable(s1_data_errorBits, s1_fire) 432 val s2_data_cacheline = RegEnable(s1_data_cacheline, s1_fire) 433 434 /** send req info of s1 and s2 to IPrefetchPipe for filter request */ 435 toIPrefetch.s1Info(0).paddr := s1_req_paddr(0) 436 toIPrefetch.s1Info(0).valid := s1_valid 437 toIPrefetch.s1Info(1).paddr := s1_req_paddr(1) 438 toIPrefetch.s1Info(1).valid := s1_valid && s1_double_line 439 toIPrefetch.s2Info(0).paddr := s2_req_paddr(0) 440 toIPrefetch.s2Info(0).valid := s2_valid 441 toIPrefetch.s2Info(1).paddr := s2_req_paddr(1) 442 toIPrefetch.s2Info(1).valid := s2_valid && s2_double_line 443 444 assert(RegNext(!s2_valid || s2_req_paddr(0)(11,0) === s2_req_vaddr(0)(11,0), true.B)) 445 446 /** 447 ****************************************************************************** 448 * tlb exception and pmp logic 449 ****************************************************************************** 450 */ 451 // short delay exception signal 452 val s2_except_tlb_pf = RegEnable(tlbExcpPF, s1_fire) 453 val s2_except_tlb_af = RegEnable(tlbExcpAF, s1_fire) 454 val s2_except_tlb = VecInit(Seq(s2_except_tlb_pf(0) || s2_except_tlb_af(0), s2_double_line && (s2_except_tlb_pf(1) || s2_except_tlb_af(1)))) 455 val s2_has_except_tlb = s2_valid && s2_except_tlb.reduce(_||_) 456 // long delay exception signal 457 // exception information and mmio 458 val pmpExcpAF = VecInit(Seq(fromPMP(0).instr, fromPMP(1).instr && s2_double_line)) 459 val s2_except_pmp_af = DataHoldBypass(pmpExcpAF, RegNext(s1_fire)) 460 val s2_mmio = s2_valid && DataHoldBypass(fromPMP(0).mmio && !s2_except_tlb(0) && !s2_except_pmp_af(0), RegNext(s1_fire)).asBool 461 // pmp port 462 toPMP.zipWithIndex.map { case (p, i) => 463 p.valid := s2_valid 464 p.bits.addr := s2_req_paddr(i) 465 p.bits.size := 3.U // TODO 466 p.bits.cmd := TlbCmd.exec 467 } 468 469 /** 470 ****************************************************************************** 471 * look last miss data 472 ****************************************************************************** 473 */ 474 class MissSlot(implicit p: Parameters) extends ICacheBundle { 475 val paddr = RegInit(0.U(PAddrBits.W)) 476 val vSetIdx = RegInit(0.U(idxBits.W)) 477 val waymask = RegInit(0.U(nWays.W)) 478 val data = RegInit(0.U(blockBits.W)) 479 val corrupt = RegInit(false.B) 480 val finish = RegInit(true.B) 481 val valid = RegInit(false.B) 482 def data_vec = data.asTypeOf(Vec(2, UInt((blockBits/2).W))) 483 def pTag = get_phy_tag(paddr) 484 } 485 val missSlot = Seq.fill(2)(new MissSlot) 486 487 // whether hit in last miss req 488 def getMissSituat(missNum : Int, slotNum : Int ) :Bool = { 489 (missSlot(slotNum).vSetIdx === s1_req_vsetIdx(missNum)) && 490 (missSlot(slotNum).pTag === s1_req_ptags(missNum)) && 491 !missSlot(slotNum).corrupt && missSlot(slotNum).finish && 492 missSlot(slotNum).valid 493 } 494 495 // s2_hit_slot(0)(1): port 0 hit slot 1 496 // Use the signal of S1 to make a judgment for timing, the value of missSlot has benn set when s1 fire 497 val s1_hit_slot_vec = VecInit((0 until PortNumber).map(port => VecInit((0 until PortNumber).map(getMissSituat(port, _))))) 498 val s2_hit_slot_vec = RegEnable(s1_hit_slot_vec, s1_fire) 499 500 // select one from two missSlots to handle miss for every port 501 // slot(0) hit && slot(1) hit : don't case 502 // slot(0) hit && slot(1) miss: (a) missed port(0) -> slot(1); (b) missed port(1) -> slot(1) 503 // slot(0) miss && slot(1) hit : (a) missed port(0) -> slot(0); (b) missed port(1) -> slot(0) 504 // slot(0) miss && slot(1) miss: missed port(0) -> slot(0) missed port(1) -> slot(1) 505 val s1_curr_slot_id = Wire(Vec(2, Bool())) 506 s1_curr_slot_id(0) := s1_hit_slot_vec(0)(0) || s1_hit_slot_vec(1)(0) 507 s1_curr_slot_id(1) := !(s1_hit_slot_vec(0)(1) || s1_hit_slot_vec(1)(1)) 508 val s2_curr_slot_id = RegEnable(s1_curr_slot_id, s1_fire) 509 510 /** 511 ****************************************************************************** 512 * miss handle 513 ****************************************************************************** 514 */ 515 val s2_hit_slot = VecInit(s2_hit_slot_vec.map(_.asUInt.orR)) 516 val s2_fixed_port_hit = VecInit((0 until PortNumber).map(port => s2_port_hit(port) || s2_hit_slot(port))) 517 518 // only handle port0 miss when port1 have tlb except or pmp except 519 val s2_port_miss = Wire(Vec(PortNumber, Bool())) 520 521 s2_port_miss(0) := !s2_fixed_port_hit(0) && !s2_except_tlb(0) && !s2_except_pmp_af(0) && !s2_mmio 522 s2_port_miss(1) := !s2_fixed_port_hit(1) && s2_double_line && !s2_except_tlb(0) && !s2_except_tlb(1) && 523 !s2_except_pmp_af(0) && !s2_except_pmp_af(1) && !s2_mmio 524 525 (0 until PortNumber).map{ i => 526 when(s2_port_miss(i) && RegNext(s1_fire)) { 527 when(s2_curr_slot_id(i)) { 528 missSlot(1).vSetIdx := s2_req_vsetIdx(i) 529 missSlot(1).paddr := s2_req_paddr(i) 530 missSlot(1).waymask := s2_waymask(i) 531 missSlot(1).finish := false.B 532 missSlot(1).valid := true.B 533 }.otherwise { 534 missSlot(0).vSetIdx := s2_req_vsetIdx(i) 535 missSlot(0).paddr := s2_req_paddr(i) 536 missSlot(0).waymask := s2_waymask(i) 537 missSlot(0).finish := false.B 538 missSlot(0).valid := true.B 539 } 540 } 541 } 542 543 // which missSlot need to be issued 544 val s2_missSlot_issue = Wire(Vec(2, Bool())) 545 s2_missSlot_issue(0) := (s2_port_miss(0) && !s2_curr_slot_id(0)) || (s2_port_miss(1) && !s2_curr_slot_id(1)) 546 s2_missSlot_issue(1) := (s2_port_miss(0) && s2_curr_slot_id(0)) || (s2_port_miss(1) && s2_curr_slot_id(1)) 547 548 // state machine 549 val m_idle ::m_send_req :: m_wait_resp :: Nil = Enum(3) 550 val missStateQueue = RegInit(VecInit(Seq.fill(2)(m_idle))) 551 552 (0 until PortNumber).map{ i => 553 switch(missStateQueue(i)){ 554 is(m_idle) { 555 missStateQueue(i) := Mux(RegNext(s1_fire) && s2_missSlot_issue(i), m_send_req, m_idle) 556 } 557 is(m_send_req) { 558 missStateQueue(i) := Mux(toMSHR(i).fire, m_wait_resp, m_send_req) 559 } 560 is(m_wait_resp) { 561 missStateQueue(i) := Mux(fromMSHR(i).fire, m_idle, m_wait_resp) 562 } 563 } 564 } 565 566 // send req to MSHR 567 (0 until PortNumber).map{i => 568 toMSHR(i).valid := missStateQueue(i) === m_send_req 569 toMSHR(i).bits.paddr := missSlot(i).paddr 570 toMSHR(i).bits.vSetIdx := missSlot(i).vSetIdx 571 toMSHR(i).bits.waymask := missSlot(i).waymask 572 } 573 574 // recrive resp from MSHR to update missSlot 575 (0 until PortNumber).map{ i => 576 when((missStateQueue(i) === m_wait_resp) && fromMSHR(i).fire) { 577 missSlot(i).finish := true.B 578 missSlot(i).data := fromMSHR(i).bits.data 579 missSlot(i).corrupt := fromMSHR(i).bits.corrupt 580 } 581 } 582 583 // handle miss finish 584 s2_fetch_finish := (!s2_port_miss(0) && !s2_port_miss(1)) || (missSlot(0).finish && missSlot(1).finish && !RegNext(s1_fire)) 585 586 /** 587 ****************************************************************************** 588 * select data from hitted sram data, last missSlot and current missSlot 589 ****************************************************************************** 590 */ 591 val s2_hit_datas = Wire(Vec(2, UInt((blockBits/2).W))) 592 s2_hit_datas(0) := Mux1H(s2_tag_match_vec(0).asUInt, s2_data_cacheline(0)) 593 s2_hit_datas(1) := Mux1H(Mux(s2_double_line, s2_tag_match_vec(1).asUInt, s2_tag_match_vec(0).asUInt), s2_data_cacheline(1)) 594 595 // get cacheline from last slot 596 val s2_last_slot_cacheline = (0 until PortNumber).map(port => Mux1H(s2_hit_slot_vec(port).asUInt, missSlot.map(_.data_vec))) 597 // get cacheline from curr slot 598 val s2_curr_slot_cacheline = (0 until PortNumber).map(port => Mux(s2_curr_slot_id(port), missSlot(1).data_vec, missSlot(0).data_vec)) 599 val s2_slot_cacheline = (0 until PortNumber).map(port => Mux(s2_hit_slot(port), s2_last_slot_cacheline(port), s2_curr_slot_cacheline(port))) 600 val s2_slot_data = Wire(Vec(PortNumber, UInt((blockBits/2).W))) 601 s2_slot_data(0) := Mux(s2_double_line, s2_slot_cacheline(0)(1), s2_slot_cacheline(0)(0)) 602 s2_slot_data(1) := Mux(s2_double_line, s2_slot_cacheline(1)(0), s2_slot_cacheline(0)(1)) 603 604 val s2_fetch_data = Wire(Vec(2, UInt((blockBits/2).W))) 605 s2_fetch_data(0) := Mux(s2_port_hit(0), s2_hit_datas(0), s2_slot_data(0)) 606 s2_fetch_data(1) := Mux(s2_port_hit(1) || (s2_port_hit(0) && !s2_double_line), s2_hit_datas(1), s2_slot_data(1)) 607 608 val s2_corrupt = (0 until PortNumber).map(port => s2_port_miss(port) && Mux(s2_curr_slot_id(port), missSlot(1).corrupt, missSlot(0).corrupt)) 609 610 /** 611 ****************************************************************************** 612 * IFU data resp 613 ****************************************************************************** 614 */ 615 (0 until PortNumber).map{ i => 616 if(i ==0) toIFU(i).valid := s2_fire 617 else toIFU(i).valid := s2_fire && s2_double_line 618 toIFU(i).bits.paddr := s2_req_paddr(i) 619 toIFU(i).bits.vaddr := s2_req_vaddr(i) 620 toIFU(i).bits.data := s2_fetch_data(i) 621 toIFU(i).bits.tlbExcp.pageFault := s2_except_tlb_pf(i) 622 toIFU(i).bits.tlbExcp.accessFault := s2_except_tlb_af(i) || s2_corrupt(i) || s2_except_pmp_af(i) 623 toIFU(i).bits.tlbExcp.mmio := s2_mmio 624 } 625 626 /** 627 ****************************************************************************** 628 * error resp: MSHR error 629 ****************************************************************************** 630 */ 631 // data/meta parity error 632 val s2_data_errors = Wire(Vec(PortNumber,Vec(nWays, Bool()))) 633 (0 until PortNumber).map{ i => 634 val read_datas = s2_data_cacheline(i).asTypeOf(Vec(nWays,Vec(dataCodeUnitNum, UInt(dataCodeUnit.W)))) 635 val read_codes = s2_data_errorBits(i).asTypeOf(Vec(nWays,Vec(dataCodeUnitNum, UInt(dataCodeBits.W)))) 636 val data_full_wayBits = VecInit((0 until nWays).map( w => 637 VecInit((0 until dataCodeUnitNum).map( u => 638 Cat(read_codes(w)(u), read_datas(w)(u)))))) 639 val data_error_wayBits = VecInit((0 until nWays).map( w => 640 VecInit((0 until dataCodeUnitNum).map( u => 641 cacheParams.dataCode.decode(data_full_wayBits(w)(u)).error)))) 642 // register for timing 643 if(i == 0){ 644 (0 until nWays).map{ w => 645 s2_data_errors(i)(w) := RegNext(RegNext(s1_fire)) && RegNext(data_error_wayBits(w)).reduce(_||_) 646 } 647 } else { 648 (0 until nWays).map{ w => 649 s2_data_errors(i)(w) := RegNext(RegNext(s1_fire)) && RegNext(RegNext(s1_double_line)) && RegNext(data_error_wayBits(w)).reduce(_||_) 650 } 651 } 652 } 653 654 val s2_parity_meta_error = VecInit((0 until PortNumber).map(i => s2_meta_errors(i).reduce(_||_) && io.csr_parity_enable)) 655 val s2_parity_data_error = VecInit((0 until PortNumber).map(i => s2_data_errors(i).reduce(_||_) && io.csr_parity_enable)) 656 val s2_parity_error = VecInit((0 until PortNumber).map(i => RegNext(s2_parity_meta_error(i)) || s2_parity_data_error(i))) 657 658 for(i <- 0 until PortNumber){ 659 io.errors(i).valid := RegNext(s2_parity_error(i) && RegNext(RegNext(s1_fire))) 660 io.errors(i).report_to_beu := RegNext(s2_parity_error(i) && RegNext(RegNext(s1_fire))) 661 io.errors(i).paddr := RegNext(RegNext(s2_req_paddr(i))) 662 io.errors(i).source := DontCare 663 io.errors(i).source.tag := RegNext(RegNext(s2_parity_meta_error(i))) 664 io.errors(i).source.data := RegNext(s2_parity_data_error(i)) 665 io.errors(i).source.l2 := false.B 666 io.errors(i).opType := DontCare 667 io.errors(i).opType.fetch := true.B 668 } 669 670 // MSHR error 671 (0 until PortNumber).map{ i => 672 when(RegNext(s2_fire && s2_corrupt(i))){ 673 io.errors(i).valid := true.B 674 io.errors(i).report_to_beu := false.B // l2 should have report that to bus error unit, no need to do it again 675 io.errors(i).paddr := RegNext(s2_req_paddr(i)) 676 io.errors(i).source.tag := false.B 677 io.errors(i).source.data := false.B 678 io.errors(i).source.l2 := true.B 679 } 680 } 681 682 /** 683 ****************************************************************************** 684 * s2 prefetch port 685 ****************************************************************************** 686 */ 687 (0 until PortNumber).foreach{ i => 688 // TODO: consider corrupt of missSlot 689 toIPrefetch.missSlot(i).valid := missSlot(i).valid 690 toIPrefetch.missSlot(i).vSetIdx := missSlot(i).vSetIdx 691 toIPrefetch.missSlot(i).ptag := missSlot(i).pTag 692 } 693 694 /** 695 ****************************************************************************** 696 * update replacement status register 697 ****************************************************************************** 698 */ 699 /** replacement status register */ 700 val port_touch_sets = Seq.fill(PortNumber)(Wire(Vec(2, UInt(log2Ceil(nSets/2).W)))) 701 val port_touch_ways = Seq.fill(PortNumber)(Wire(Vec(2, Valid(UInt(log2Ceil(nWays).W))))) 702 (port_touch_ways zip port_touch_sets).zipWithIndex.map{ case((t_w,t_s), i) => 703 /** update replacement status register: 0 is hit access/ 1 is miss access */ 704 t_s(0) := s2_req_vsetIdx(i)(highestIdxBit, 1) 705 // hit in slot will be ignored, which generate a repeated access 706 t_w(0).valid := s2_valid && s2_port_hit(i) 707 t_w(0).bits := OHToUInt(s2_tag_match_vec(i)) 708 709 t_s(1) := s2_req_vsetIdx(i)(highestIdxBit, 1) 710 t_w(1).valid := s2_valid && s2_port_miss(i) 711 t_w(1).bits := OHToUInt(s2_waymask(i)) 712 } 713 714 val touch_ways = VecInit((0 until PortNumber).map( i => Mux(s2_req_vsetIdx(i)(0), port_touch_ways(1), port_touch_ways(0)))) 715 val touch_sets = VecInit((0 until PortNumber).map( i => Mux(s2_req_vsetIdx(i)(0), port_touch_sets(1), port_touch_sets(0)))) 716 ((replacers zip touch_sets) zip touch_ways).map{case ((r, s),w) => r.access(s,w)} 717 // TODO: need choose one replacer according to the bankid 718 IPFReplacer.waymask := UIntToOH(replacers(0).way(IPFReplacer.vsetIdx)) 719 720 /** 721 ****************************************************************************** 722 * performance info. TODO: need to simplify the logic 723 ***********************************************************s******************* 724 */ 725 io.fetch.topdownIcacheMiss := s2_port_miss(0) || s2_port_miss(1) 726 io.fetch.topdownItlbMiss := itlbMissStage(0) 727 728 io.perfInfo.only_0_hit := s2_fixed_port_hit(0) && !s2_double_line 729 io.perfInfo.only_0_miss := !s2_fixed_port_hit(0) && !s2_double_line 730 io.perfInfo.hit_0_hit_1 := s2_fixed_port_hit(0) && s2_fixed_port_hit(1) && s2_double_line 731 io.perfInfo.hit_0_miss_1 := s2_fixed_port_hit(0) && !s2_fixed_port_hit(1) && s2_double_line 732 io.perfInfo.miss_0_hit_1 := !s2_fixed_port_hit(0) && s2_fixed_port_hit(1) && s2_double_line 733 io.perfInfo.miss_0_miss_1 := !s2_fixed_port_hit(0) && !s2_fixed_port_hit(1) && s2_double_line 734 io.perfInfo.hit_0_except_1 := s2_fixed_port_hit(0) && (s2_except_tlb(1) || s2_except_pmp_af(1)) && s2_double_line 735 io.perfInfo.miss_0_except_1 := !s2_fixed_port_hit(0) && (s2_except_tlb(1) || s2_except_pmp_af(1)) && s2_double_line 736 io.perfInfo.bank_hit(0) := s2_fixed_port_hit(0) 737 io.perfInfo.bank_hit(1) := s2_fixed_port_hit(1) && s2_double_line 738 io.perfInfo.except_0 := s2_except_tlb(0) || s2_except_pmp_af(0) 739 io.perfInfo.hit := !s2_port_miss(0) && !s2_port_miss(1) 740 741 /** <PERF> fetch bubble generated by icache miss*/ 742 XSPerfAccumulate("icache_bubble_s2_miss", s2_valid && !s2_fetch_finish ) 743 XSPerfAccumulate("icache_bubble_s0_tlb_miss", s1_valid && !tlbRespAllValid) 744 745 /** 746 ****************************************************************************** 747 * difftest refill check 748 ****************************************************************************** 749 */ 750 if (env.EnableDifftest) { 751 val discards = (0 until PortNumber).map { i => 752 val discard = toIFU(i).bits.tlbExcp.pageFault || toIFU(i).bits.tlbExcp.accessFault || toIFU(i).bits.tlbExcp.mmio 753 discard 754 } 755 (0 until PortNumber).map { i => 756 val diffMainPipeOut = DifftestModule(new DiffRefillEvent, dontCare = true) 757 diffMainPipeOut.coreid := io.hartId 758 diffMainPipeOut.index := (4 + i).U 759 if (i == 0) { 760 diffMainPipeOut.valid := s2_fire && !discards(0) 761 diffMainPipeOut.addr := s2_req_paddr(0) 762 } else { 763 diffMainPipeOut.valid := s2_fire && !discards(0) && (!s2_double_line || (s2_double_line && !discards(1))) 764 diffMainPipeOut.addr := s2_req_paddr(0) + (blockBits/2).U 765 } 766 diffMainPipeOut.data := Cat(0.U((blockBits/2).W), toIFU(i).bits.data).asTypeOf(diffMainPipeOut.data) 767 // idtfr: 0 -> data from icache 1 -> reversedData 2 -> data from missUnit 768 diffMainPipeOut.idtfr := Mux(s2_port_hit(i), 0.U, Mux(s2_fixed_port_hit(i), 1.U, 2.U)) 769 diffMainPipeOut 770 } 771 } 772} 773