1package xiangshan.frontend 2 3import chisel3._ 4import chisel3.util._ 5import device.RAMHelper 6import xiangshan._ 7import utils._ 8import xiangshan.cache._ 9import chisel3.experimental.chiselName 10import freechips.rocketchip.tile.HasLazyRoCC 11import chisel3.ExcitingUtils._ 12import xiangshan.backend.ftq.FtqPtr 13 14trait HasInstrMMIOConst extends HasXSParameter with HasIFUConst{ 15 def mmioBusWidth = 64 16 def mmioBusBytes = mmioBusWidth /8 17 def mmioBeats = FetchWidth * 4 * 8 / mmioBusWidth 18 def mmioMask = VecInit(List.fill(PredictWidth)(true.B)).asUInt 19 def mmioBusAligned(pc :UInt): UInt = align(pc, mmioBusBytes) 20} 21 22trait HasIFUConst extends HasXSParameter { 23 val resetVector = 0x10000000L//TODO: set reset vec 24 def align(pc: UInt, bytes: Int): UInt = Cat(pc(VAddrBits-1, log2Ceil(bytes)), 0.U(log2Ceil(bytes).W)) 25 val groupBytes = 64 // correspond to cache line size 26 val groupOffsetBits = log2Ceil(groupBytes) 27 val groupWidth = groupBytes / instBytes 28 val packetBytes = PredictWidth * instBytes 29 val packetOffsetBits = log2Ceil(packetBytes) 30 def offsetInPacket(pc: UInt) = pc(packetOffsetBits-1, instOffsetBits) 31 def packetIdx(pc: UInt) = pc(VAddrBits-1, log2Ceil(packetBytes)) 32 def groupAligned(pc: UInt) = align(pc, groupBytes) 33 def packetAligned(pc: UInt) = align(pc, packetBytes) 34 def mask(pc: UInt): UInt = ((~(0.U(PredictWidth.W))) << offsetInPacket(pc))(PredictWidth-1,0) 35 def snpc(pc: UInt): UInt = packetAligned(pc) + packetBytes.U 36 37 val enableGhistRepair = true 38 val IFUDebug = true 39} 40 41class GlobalHistory extends XSBundle { 42 val predHist = UInt(HistoryLength.W) 43 def update(sawNTBr: Bool, takenOnBr: Bool, hist: UInt = predHist): GlobalHistory = { 44 val g = Wire(new GlobalHistory) 45 val shifted = takenOnBr || sawNTBr 46 g.predHist := Mux(shifted, (hist << 1) | takenOnBr.asUInt, hist) 47 g 48 } 49 50 final def === (that: GlobalHistory): Bool = { 51 predHist === that.predHist 52 } 53 54 final def =/= (that: GlobalHistory): Bool = !(this === that) 55 56 implicit val name = "IFU" 57 def debug(where: String) = XSDebug(p"[${where}_GlobalHistory] hist=${Binary(predHist)}\n") 58 // override def toString(): String = "histPtr=%d, sawNTBr=%d, takenOnBr=%d, saveHalfRVI=%d".format(histPtr, sawNTBr, takenOnBr, saveHalfRVI) 59} 60 61 62class IFUIO extends XSBundle 63{ 64 // to ibuffer 65 val fetchPacket = DecoupledIO(new FetchPacket) 66 // from backend 67 val redirect = Flipped(ValidIO(new Redirect)) 68 val bp_ctrl = Input(new BPUCtrl) 69 val commitUpdate = Flipped(ValidIO(new FtqEntry)) 70 val ftqEnqPtr = Input(new FtqPtr) 71 val ftqLeftOne = Input(Bool()) 72 // to backend 73 val toFtq = DecoupledIO(new FtqEntry) 74 // to icache 75 val icacheMemGrant = Flipped(DecoupledIO(new L1plusCacheResp)) 76 val fencei = Input(Bool()) 77 // from icache 78 val icacheMemAcq = DecoupledIO(new L1plusCacheReq) 79 val l1plusFlush = Output(Bool()) 80 val prefetchTrainReq = ValidIO(new IcacheMissReq) 81 // to tlb 82 val sfence = Input(new SfenceBundle) 83 val tlbCsr = Input(new TlbCsrBundle) 84 // from tlb 85 val ptw = new TlbPtwIO 86 // icache uncache 87 val mmio_acquire = DecoupledIO(new InsUncacheReq) 88 val mmio_grant = Flipped(DecoupledIO(new InsUncacheResp)) 89 val mmio_flush = Output(Bool()) 90} 91 92class PrevHalfInstr extends XSBundle { 93 val pc = UInt(VAddrBits.W) 94 val npc = UInt(VAddrBits.W) 95 val instr = UInt(16.W) 96 val ipf = Bool() 97} 98 99@chiselName 100class IFU extends XSModule with HasIFUConst with HasCircularQueuePtrHelper 101{ 102 val io = IO(new IFUIO) 103 val bpu = BPU(EnableBPU) 104 val icache = Module(new ICache) 105 106 io.ptw <> TLB( 107 in = Seq(icache.io.tlb), 108 sfence = io.sfence, 109 csr = io.tlbCsr, 110 width = 1, 111 isDtlb = false, 112 shouldBlock = true 113 ) 114 115 val if2_redirect, if3_redirect, if4_redirect = WireInit(false.B) 116 val if1_flush, if2_flush, if3_flush, if4_flush = WireInit(false.B) 117 118 val icacheResp = icache.io.resp.bits 119 120 if4_flush := io.redirect.valid 121 if3_flush := if4_flush || if4_redirect 122 if2_flush := if3_flush || if3_redirect 123 if1_flush := if2_flush || if2_redirect 124 125 //********************** IF1 ****************************// 126 val if1_valid = !reset.asBool && GTimer() > 500.U 127 val if1_npc = WireInit(0.U(VAddrBits.W)) 128 val if2_ready = WireInit(false.B) 129 val if2_valid = RegInit(init = false.B) 130 val if2_allReady = WireInit(if2_ready && icache.io.req.ready) 131 val if1_fire = if1_valid && if2_allReady 132 133 val bpu_req_gh = Wire(new GlobalHistory) 134 val if1_gh, if2_gh, if3_gh, if4_gh = Wire(new GlobalHistory) 135 val if2_predicted_gh, if3_predicted_gh, if4_predicted_gh = Wire(new GlobalHistory) 136 val final_gh = RegInit(0.U.asTypeOf(new GlobalHistory)) 137 val redirect_gh = WireInit(0.U.asTypeOf(new GlobalHistory)) 138 139 //********************** IF2 ****************************// 140 val if2_allValid = if2_valid && icache.io.tlb.resp.valid 141 val if3_ready = WireInit(false.B) 142 val if2_fire = if2_allValid && if3_ready 143 val if2_pc = RegEnable(next = if1_npc, init = resetVector.U, enable = if1_fire) 144 val if2_snpc = snpc(if2_pc) 145 val if2_predHist = RegEnable(bpu_req_gh.predHist, enable=if1_fire) 146 if2_ready := if3_ready && icache.io.tlb.resp.valid || !if2_valid 147 when (if1_fire) { if2_valid := true.B } 148 .elsewhen (if2_flush) { if2_valid := false.B } 149 .elsewhen (if2_fire) { if2_valid := false.B } 150 151 val npcGen = new PriorityMuxGenerator[UInt] 152 npcGen.register(true.B, RegNext(if1_npc), Some("stallPC")) 153 val if2_bp = bpu.io.out(0) 154 155 // if taken, bp_redirect should be true 156 // when taken on half RVI, we suppress this redirect signal 157 158 npcGen.register(if2_valid, Mux(if2_bp.taken, if2_bp.target, if2_snpc), Some("if2_target")) 159 160 if2_predicted_gh := if2_gh.update(if2_bp.hasNotTakenBrs, if2_bp.takenOnBr) 161 162 //********************** IF3 ****************************// 163 // if3 should wait for instructions resp to arrive 164 val if3_valid = RegInit(init = false.B) 165 val if4_ready = WireInit(false.B) 166 val if3_allValid = if3_valid && icache.io.resp.valid 167 val if3_fire = if3_allValid && if4_ready 168 val if3_pc = RegEnable(if2_pc, if2_fire) 169 val if3_snpc = RegEnable(if2_snpc, if2_fire) 170 val if3_predHist = RegEnable(if2_predHist, enable=if2_fire) 171 if3_ready := if4_ready && icache.io.resp.valid || !if3_valid 172 when (if3_flush) { 173 if3_valid := false.B 174 }.elsewhen (if2_fire && !if2_flush) { 175 if3_valid := true.B 176 }.elsewhen (if3_fire) { 177 if3_valid := false.B 178 } 179 180 val if3_bp = bpu.io.out(1) 181 if3_predicted_gh := if3_gh.update(if3_bp.hasNotTakenBrs, if3_bp.takenOnBr) 182 183 184 val prevHalfInstrReq = WireInit(0.U.asTypeOf(ValidUndirectioned(new PrevHalfInstr))) 185 // only valid when if4_fire 186 val hasPrevHalfInstrReq = prevHalfInstrReq.valid && HasCExtension.B 187 188 val if3_prevHalfInstr = RegInit(0.U.asTypeOf(ValidUndirectioned(new PrevHalfInstr))) 189 190 // 32-bit instr crosses 2 pages, and the higher 16-bit triggers page fault 191 val crossPageIPF = WireInit(false.B) 192 193 val if3_pendingPrevHalfInstr = if3_prevHalfInstr.valid && HasCExtension.B 194 195 // the previous half of RVI instruction waits until it meets its last half 196 val if3_prevHalfInstrMet = if3_pendingPrevHalfInstr && if3_prevHalfInstr.bits.npc === if3_pc && if3_valid 197 // set to invalid once consumed or redirect from backend 198 val if3_prevHalfConsumed = if3_prevHalfInstrMet && if3_fire 199 val if3_prevHalfFlush = if4_flush 200 when (if3_prevHalfFlush) { 201 if3_prevHalfInstr.valid := false.B 202 }.elsewhen (hasPrevHalfInstrReq) { 203 if3_prevHalfInstr.valid := true.B 204 }.elsewhen (if3_prevHalfConsumed) { 205 if3_prevHalfInstr.valid := false.B 206 } 207 when (hasPrevHalfInstrReq) { 208 if3_prevHalfInstr.bits := prevHalfInstrReq.bits 209 } 210 // when bp signal a redirect, we distinguish between taken and not taken 211 // if taken and saveHalfRVI is true, we do not redirect to the target 212 213 class IF3_PC_COMP extends XSModule { 214 val io = IO(new Bundle { 215 val if2_pc = Input(UInt(VAddrBits.W)) 216 val pc = Input(UInt(VAddrBits.W)) 217 val if2_valid = Input(Bool()) 218 val res = Output(Bool()) 219 }) 220 io.res := !io.if2_valid || io.if2_valid && io.if2_pc =/= io.pc 221 } 222 def if3_nextValidPCNotEquals(pc: UInt) = { 223 val comp = Module(new IF3_PC_COMP) 224 comp.io.if2_pc := if2_pc 225 comp.io.pc := pc 226 comp.io.if2_valid := if2_valid 227 comp.io.res 228 } 229 230 val if3_predTakenRedirectVec = VecInit((0 until PredictWidth).map(i => !if3_pendingPrevHalfInstr && if3_bp.takens(i) && if3_nextValidPCNotEquals(if3_bp.targets(i)))) 231 val if3_prevHalfNotMetRedirect = if3_pendingPrevHalfInstr && !if3_prevHalfInstrMet && if3_nextValidPCNotEquals(if3_prevHalfInstr.bits.npc) 232 val if3_predTakenRedirect = ParallelOR(if3_predTakenRedirectVec) 233 val if3_predNotTakenRedirect = !if3_pendingPrevHalfInstr && !if3_bp.taken && if3_nextValidPCNotEquals(if3_snpc) 234 // when pendingPrevHalfInstr, if3_GHInfo is set to the info of last prev half instr 235 // val if3_ghInfoNotIdenticalRedirect = !if3_pendingPrevHalfInstr && if3_GHInfo =/= if3_lastGHInfo && enableGhistRepair.B 236 237 if3_redirect := if3_valid && ( 238 // prevHalf does not match if3_pc and the next fetch packet is not snpc 239 if3_prevHalfNotMetRedirect && HasCExtension.B || 240 // pred taken and next fetch packet is not the predicted target 241 if3_predTakenRedirect || 242 // pred not taken and next fetch packet is not snpc 243 if3_predNotTakenRedirect 244 // GHInfo from last pred does not corresponds with this packet 245 // if3_ghInfoNotIdenticalRedirect 246 ) 247 248 val if3_target = WireInit(if3_snpc) 249 250 if3_target := Mux1H(Seq((if3_prevHalfNotMetRedirect -> if3_prevHalfInstr.bits.npc), 251 (if3_predTakenRedirect -> if3_bp.target), 252 (if3_predNotTakenRedirect -> if3_snpc))) 253 254 npcGen.register(if3_redirect, if3_target, Some("if3_target")) 255 256 257 //********************** IF4 ****************************// 258 val ftqEnqBuf_ready = Wire(Bool()) 259 val if4_ftqEnqPtr = Wire(new FtqPtr) 260 val if4_pd = RegEnable(icache.io.pd_out, if3_fire) 261 val if4_ipf = RegEnable(icacheResp.ipf || if3_prevHalfInstrMet && if3_prevHalfInstr.bits.ipf, if3_fire) 262 val if4_acf = RegEnable(icacheResp.acf, if3_fire) 263 val if4_crossPageIPF = RegEnable(crossPageIPF, if3_fire) 264 val if4_valid = RegInit(false.B) 265 val if4_fire = if4_valid && io.fetchPacket.ready && ftqEnqBuf_ready 266 val if4_pc = RegEnable(if3_pc, if3_fire) 267 val if4_snpc = RegEnable(if3_snpc, if3_fire) 268 // This is the real mask given from icache 269 val if4_mask = RegEnable(icacheResp.mask, if3_fire) 270 271 272 val if4_predHist = RegEnable(if3_predHist, enable=if3_fire) 273 // wait until prevHalfInstr written into reg 274 if4_ready := (io.fetchPacket.ready && !hasPrevHalfInstrReq && ftqEnqBuf_ready || !if4_valid) && GTimer() > 500.U 275 when (if4_flush) { 276 if4_valid := false.B 277 }.elsewhen (if3_fire && !if3_flush) { 278 if4_valid := Mux(if3_pendingPrevHalfInstr, if3_prevHalfInstrMet, true.B) 279 }.elsewhen (if4_fire) { 280 if4_valid := false.B 281 } 282 283 val if4_bp = Wire(new BranchPrediction) 284 if4_bp := bpu.io.out(2) 285 286 if4_predicted_gh := if4_gh.update(if4_bp.hasNotTakenBrs, if4_bp.takenOnBr) 287 288 def jal_offset(inst: UInt, rvc: Bool): SInt = { 289 Mux(rvc, 290 Cat(inst(12), inst(8), inst(10, 9), inst(6), inst(7), inst(2), inst(11), inst(5, 3), 0.U(1.W)).asSInt(), 291 Cat(inst(31), inst(19, 12), inst(20), inst(30, 21), 0.U(1.W)).asSInt() 292 ) 293 } 294 def br_offset(inst: UInt, rvc: Bool): SInt = { 295 Mux(rvc, 296 Cat(inst(12), inst(6, 5), inst(2), inst(11, 10), inst(4, 3), 0.U(1.W)).asSInt, 297 Cat(inst(31), inst(7), inst(30, 25), inst(11, 8), 0.U(1.W)).asSInt() 298 ) 299 } 300 val if4_instrs = if4_pd.instrs 301 val if4_jals = if4_bp.jalMask 302 val if4_jal_tgts = VecInit((0 until PredictWidth).map(i => (if4_pd.pc(i).asSInt + jal_offset(if4_instrs(i), if4_pd.pd(i).isRVC)).asUInt)) 303 val if4_brs = if4_bp.brMask 304 val if4_br_tgts = VecInit((0 until PredictWidth).map(i => (if4_pd.pc(i).asSInt + br_offset(if4_instrs(i), if4_pd.pd(i).isRVC)).asUInt)) 305 (0 until PredictWidth).foreach {i => 306 when (if4_jals(i)) { 307 if4_bp.targets(i) := if4_jal_tgts(i) 308 }.elsewhen (if4_brs(i)) { 309 if4_bp.targets(i) := if4_br_tgts(i) 310 } 311 } 312 313 // we need this to tell BPU the prediction of prev half 314 // because the prediction is with the start of each inst 315 val if4_prevHalfInstr = RegInit(0.U.asTypeOf(ValidUndirectioned(new PrevHalfInstr))) 316 val if4_pendingPrevHalfInstr = if4_prevHalfInstr.valid && HasCExtension.B 317 val if4_prevHalfInstrMet = if4_pendingPrevHalfInstr && if4_valid 318 val if4_prevHalfConsumed = if4_prevHalfInstrMet && if4_fire 319 val if4_prevHalfFlush = if4_flush 320 321 when (if4_prevHalfFlush) { 322 if4_prevHalfInstr.valid := false.B 323 }.elsewhen (if3_prevHalfConsumed) { 324 if4_prevHalfInstr.valid := if3_prevHalfInstr.valid 325 }.elsewhen (if4_prevHalfConsumed) { 326 if4_prevHalfInstr.valid := false.B 327 } 328 329 when (if3_prevHalfConsumed) { 330 if4_prevHalfInstr.bits := if3_prevHalfInstr.bits 331 } 332 333 prevHalfInstrReq.valid := if4_fire && if4_bp.saveHalfRVI && HasCExtension.B 334 335 // // this is result of the last half RVI 336 prevHalfInstrReq.bits.pc := if4_pd.pc(PredictWidth-1) 337 prevHalfInstrReq.bits.npc := snpc(if4_pc) 338 prevHalfInstrReq.bits.instr := if4_pd.instrs(PredictWidth-1)(15, 0) 339 prevHalfInstrReq.bits.ipf := if4_ipf 340 341 class IF4_PC_COMP extends XSModule { 342 val io = IO(new Bundle { 343 val if2_pc = Input(UInt(VAddrBits.W)) 344 val if3_pc = Input(UInt(VAddrBits.W)) 345 val pc = Input(UInt(VAddrBits.W)) 346 val if2_valid = Input(Bool()) 347 val if3_valid = Input(Bool()) 348 val res = Output(Bool()) 349 }) 350 io.res := io.if3_valid && io.if3_pc =/= io.pc || 351 !io.if3_valid && (io.if2_valid && io.if2_pc =/= io.pc) || 352 !io.if3_valid && !io.if2_valid 353 } 354 def if4_nextValidPCNotEquals(pc: UInt) = { 355 val comp = Module(new IF4_PC_COMP) 356 comp.io.if2_pc := if2_pc 357 comp.io.if3_pc := if3_pc 358 comp.io.pc := pc 359 comp.io.if2_valid := if2_valid 360 comp.io.if3_valid := if3_valid 361 comp.io.res 362 } 363 364 val if4_predTakenRedirectVec = VecInit((0 until PredictWidth).map(i => if4_bp.takens(i) && if4_nextValidPCNotEquals(if4_bp.targets(i)))) 365 366 val if4_prevHalfNextNotMet = hasPrevHalfInstrReq && if4_nextValidPCNotEquals(prevHalfInstrReq.bits.pc+2.U) 367 val if4_predTakenRedirect = ParallelORR(if4_predTakenRedirectVec) 368 val if4_predNotTakenRedirect = !if4_bp.taken && if4_nextValidPCNotEquals(if4_snpc) 369 // val if4_ghInfoNotIdenticalRedirect = if4_GHInfo =/= if4_lastGHInfo && enableGhistRepair.B 370 371 if4_redirect := if4_valid && ( 372 // when if4 has a lastHalfRVI, but the next fetch packet is not snpc 373 // if4_prevHalfNextNotMet || 374 // when if4 preds taken, but the pc of next fetch packet is not the target 375 if4_predTakenRedirect || 376 // when if4 preds not taken, but the pc of next fetch packet is not snpc 377 if4_predNotTakenRedirect 378 // GHInfo from last pred does not corresponds with this packet 379 // if4_ghInfoNotIdenticalRedirect 380 ) 381 382 val if4_target = WireInit(if4_snpc) 383 384 if4_target := Mux(if4_bp.taken, if4_bp.target, if4_snpc) 385 386 npcGen.register(if4_redirect, if4_target, Some("if4_target")) 387 388 when (if4_fire) { 389 final_gh := if4_predicted_gh 390 } 391 if4_gh := final_gh 392 if3_gh := Mux(if4_valid, if4_predicted_gh, if4_gh) 393 if2_gh := Mux(if3_valid && !if3_flush, if3_predicted_gh, if3_gh) 394 if1_gh := Mux(if2_valid && !if2_flush, if2_predicted_gh, if2_gh) 395 bpu_req_gh := Mux(io.redirect.valid, redirect_gh, if1_gh) 396 397 // ***************** Ftq enq buffer ******************** 398 val toFtqBuf = Wire(new FtqEntry) 399 val ftqEnqBuf = RegEnable(toFtqBuf, enable=if4_fire) 400 val ftqEnqBuf_valid = RegInit(false.B) 401 val ftqLeftOne = WireInit(false.B) // TODO: to be replaced 402 ftqEnqBuf_ready := io.toFtq.ready && !(io.ftqLeftOne && ftqEnqBuf_valid) 403 if4_ftqEnqPtr := Mux(ftqEnqBuf_valid, io.ftqEnqPtr+1.U, io.ftqEnqPtr) 404 when (io.redirect.valid) { ftqEnqBuf_valid := false.B } 405 .elsewhen (if4_fire) { ftqEnqBuf_valid := true.B } 406 .elsewhen (io.toFtq.fire) { ftqEnqBuf_valid := false.B } 407 408 io.toFtq.valid := ftqEnqBuf_valid 409 io.toFtq.bits := ftqEnqBuf 410 411 toFtqBuf := DontCare 412 toFtqBuf.ftqPC := if4_pc 413 toFtqBuf.lastPacketPC.valid := if4_pendingPrevHalfInstr 414 toFtqBuf.lastPacketPC.bits := if4_prevHalfInstr.bits.pc 415 416 toFtqBuf.hist := final_gh 417 toFtqBuf.predHist := if4_predHist.asTypeOf(new GlobalHistory) 418 toFtqBuf.rasSp := bpu.io.brInfo.rasSp 419 toFtqBuf.rasTop := bpu.io.brInfo.rasTop 420 toFtqBuf.specCnt := bpu.io.brInfo.specCnt 421 toFtqBuf.metas := bpu.io.brInfo.metas 422 423 // For perf counters 424 toFtqBuf.pd := if4_pd.pd 425 426 427 val if4_jmpIdx = WireInit(if4_bp.jmpIdx) 428 val if4_taken = WireInit(if4_bp.taken) 429 val if4_real_valids = if4_pd.mask & 430 (Fill(PredictWidth, !if4_taken) | 431 (Fill(PredictWidth, 1.U(1.W)) >> (~if4_jmpIdx))) 432 433 val cfiIsCall = if4_pd.pd(if4_jmpIdx).isCall 434 val cfiIsRet = if4_pd.pd(if4_jmpIdx).isRet 435 val cfiIsRVC = if4_pd.pd(if4_jmpIdx).isRVC 436 toFtqBuf.cfiIsCall := cfiIsCall 437 toFtqBuf.cfiIsRet := cfiIsRet 438 toFtqBuf.cfiIsRVC := cfiIsRVC 439 toFtqBuf.cfiIndex.valid := if4_taken 440 toFtqBuf.cfiIndex.bits := if4_jmpIdx 441 442 toFtqBuf.br_mask := if4_bp.brMask.asTypeOf(Vec(PredictWidth, Bool())) 443 toFtqBuf.rvc_mask := VecInit(if4_pd.pd.map(_.isRVC)) 444 toFtqBuf.valids := if4_real_valids.asTypeOf(Vec(PredictWidth, Bool())) 445 toFtqBuf.target := Mux(if4_taken, if4_target, if4_snpc) 446 447 448 449 val r = io.redirect 450 val cfiUpdate = io.redirect.bits.cfiUpdate 451 when (r.valid) { 452 val isMisPred = r.bits.level === 0.U 453 val b = cfiUpdate 454 val oldGh = b.hist 455 val sawNTBr = b.sawNotTakenBranch 456 val isBr = b.pd.isBr 457 val taken = Mux(isMisPred, b.taken, b.predTaken) 458 val updatedGh = oldGh.update(sawNTBr, isBr && taken) 459 final_gh := updatedGh 460 redirect_gh := updatedGh 461 } 462 463 npcGen.register(io.redirect.valid, io.redirect.bits.cfiUpdate.target, Some("backend_redirect")) 464 npcGen.register(RegNext(reset.asBool) && !reset.asBool, resetVector.U(VAddrBits.W), Some("reset_vector")) 465 466 if1_npc := npcGen() 467 468 469 icache.io.req.valid := if1_fire 470 icache.io.resp.ready := if4_ready 471 icache.io.req.bits.addr := if1_npc 472 icache.io.req.bits.mask := mask(if1_npc) 473 icache.io.flush := Cat(if3_flush, if2_flush) 474 icache.io.mem_grant <> io.icacheMemGrant 475 icache.io.fencei := io.fencei 476 icache.io.prev.valid := if3_prevHalfInstrMet 477 icache.io.prev.bits := if3_prevHalfInstr.bits.instr 478 icache.io.prev_ipf := if3_prevHalfInstr.bits.ipf 479 icache.io.prev_pc := if3_prevHalfInstr.bits.pc 480 icache.io.mmio_acquire <> io.mmio_acquire 481 icache.io.mmio_grant <> io.mmio_grant 482 icache.io.mmio_flush <> io.mmio_flush 483 io.icacheMemAcq <> icache.io.mem_acquire 484 io.l1plusFlush := icache.io.l1plusflush 485 io.prefetchTrainReq := icache.io.prefetchTrainReq 486 487 bpu.io.ctrl := RegNext(io.bp_ctrl) 488 bpu.io.commit <> io.commitUpdate 489 bpu.io.redirect <> io.redirect 490 491 bpu.io.inFire(0) := if1_fire 492 bpu.io.inFire(1) := if2_fire 493 bpu.io.inFire(2) := if3_fire 494 bpu.io.inFire(3) := if4_fire 495 bpu.io.in.pc := if1_npc 496 bpu.io.in.hist := bpu_req_gh.asUInt 497 bpu.io.in.inMask := mask(if1_npc) 498 bpu.io.predecode.mask := if4_pd.mask 499 bpu.io.predecode.lastHalf := if4_pd.lastHalf 500 bpu.io.predecode.pd := if4_pd.pd 501 bpu.io.predecode.hasLastHalfRVI := if4_prevHalfInstrMet 502 503 504 when (if3_prevHalfInstrMet && icacheResp.ipf && !if3_prevHalfInstr.bits.ipf) { 505 crossPageIPF := true.B // higher 16 bits page fault 506 } 507 508 val fetchPacketValid = if4_valid && !io.redirect.valid && ftqEnqBuf_ready 509 val fetchPacketWire = Wire(new FetchPacket) 510 511 fetchPacketWire.mask := if4_real_valids 512 //RVC expand 513 val expandedInstrs = Wire(Vec(PredictWidth, UInt(32.W))) 514 for(i <- 0 until PredictWidth){ 515 val expander = Module(new RVCExpander) 516 expander.io.in := if4_pd.instrs(i) 517 expandedInstrs(i) := expander.io.out.bits 518 } 519 fetchPacketWire.instrs := expandedInstrs 520 521 fetchPacketWire.pc := if4_pd.pc 522 523 fetchPacketWire.pdmask := if4_pd.mask 524 fetchPacketWire.pd := if4_pd.pd 525 fetchPacketWire.ipf := if4_ipf 526 fetchPacketWire.acf := if4_acf 527 fetchPacketWire.crossPageIPFFix := if4_crossPageIPF 528 fetchPacketWire.ftqPtr := if4_ftqEnqPtr 529 530 // predTaken Vec 531 fetchPacketWire.pred_taken := if4_bp.takens 532 533 io.fetchPacket.bits := fetchPacketWire 534 io.fetchPacket.valid := fetchPacketValid 535 536// if(IFUDebug) { 537 if (!env.FPGAPlatform) { 538 val predictor_s3 = RegEnable(Mux(if3_redirect, 1.U(log2Up(4).W), 0.U(log2Up(4).W)), if3_fire) 539 val predictor_s4 = Mux(if4_redirect, 2.U, predictor_s3) 540 val predictor = predictor_s4 541 toFtqBuf.metas.map(_.predictor := predictor) 542 } 543 // } 544 545 // val predRight = cfiUpdate.valid && !cfiUpdate.bits.isMisPred && !cfiUpdate.bits.isReplay 546 // val predWrong = cfiUpdate.valid && cfiUpdate.bits.isMisPred && !cfiUpdate.bits.isReplay 547 548 // val ubtbRight = predRight && cfiUpdate.bits.bpuMeta.predictor === 0.U 549 // val ubtbWrong = predWrong && cfiUpdate.bits.bpuMeta.predictor === 0.U 550 // val btbRight = predRight && cfiUpdate.bits.bpuMeta.predictor === 1.U 551 // val btbWrong = predWrong && cfiUpdate.bits.bpuMeta.predictor === 1.U 552 // val tageRight = predRight && cfiUpdate.bits.bpuMeta.predictor === 2.U 553 // val tageWrong = predWrong && cfiUpdate.bits.bpuMeta.predictor === 2.U 554 // val loopRight = predRight && cfiUpdate.bits.bpuMeta.predictor === 3.U 555 // val loopWrong = predWrong && cfiUpdate.bits.bpuMeta.predictor === 3.U 556 557 // ExcitingUtils.addSource(ubtbRight, "perfCntubtbRight", Perf) 558 // ExcitingUtils.addSource(ubtbWrong, "perfCntubtbWrong", Perf) 559 // ExcitingUtils.addSource(btbRight, "perfCntbtbRight", Perf) 560 // ExcitingUtils.addSource(btbWrong, "perfCntbtbWrong", Perf) 561 // ExcitingUtils.addSource(tageRight, "perfCnttageRight", Perf) 562 // ExcitingUtils.addSource(tageWrong, "perfCnttageWrong", Perf) 563 // ExcitingUtils.addSource(loopRight, "perfCntloopRight", Perf) 564 // ExcitingUtils.addSource(loopWrong, "perfCntloopWrong", Perf) 565 566 // debug info 567 if (IFUDebug) { 568 XSDebug(RegNext(reset.asBool) && !reset.asBool, "Reseting...\n") 569 XSDebug(icache.io.flush(0).asBool, "Flush icache stage2...\n") 570 XSDebug(icache.io.flush(1).asBool, "Flush icache stage3...\n") 571 XSDebug(io.redirect.valid, p"Redirect from backend! target=${Hexadecimal(io.redirect.bits.cfiUpdate.target)}\n") 572 573 XSDebug("[IF1] v=%d fire=%d flush=%d pc=%x mask=%b\n", if1_valid, if1_fire, if1_flush, if1_npc, mask(if1_npc)) 574 XSDebug("[IF2] v=%d r=%d fire=%d redirect=%d flush=%d pc=%x snpc=%x\n", if2_valid, if2_ready, if2_fire, if2_redirect, if2_flush, if2_pc, if2_snpc) 575 XSDebug("[IF3] v=%d r=%d fire=%d redirect=%d flush=%d pc=%x crossPageIPF=%d sawNTBrs=%d\n", if3_valid, if3_ready, if3_fire, if3_redirect, if3_flush, if3_pc, crossPageIPF, if3_bp.hasNotTakenBrs) 576 XSDebug("[IF4] v=%d r=%d fire=%d redirect=%d flush=%d pc=%x crossPageIPF=%d sawNTBrs=%d\n", if4_valid, if4_ready, if4_fire, if4_redirect, if4_flush, if4_pc, if4_crossPageIPF, if4_bp.hasNotTakenBrs) 577 XSDebug("[IF1][icacheReq] v=%d r=%d addr=%x\n", icache.io.req.valid, icache.io.req.ready, icache.io.req.bits.addr) 578 XSDebug("[IF1][ghr] hist=%b\n", if1_gh.asUInt) 579 XSDebug("[IF1][ghr] bpu_req_gh=%b\n\n", bpu_req_gh.asUInt) 580 581 XSDebug("[IF2][bp] taken=%d jmpIdx=%d hasNTBrs=%d target=%x saveHalfRVI=%d\n\n", if2_bp.taken, if2_bp.jmpIdx, if2_bp.hasNotTakenBrs, if2_bp.target, if2_bp.saveHalfRVI) 582 if2_gh.debug("if2") 583 584 XSDebug("[IF3][icacheResp] v=%d r=%d pc=%x mask=%b\n", icache.io.resp.valid, icache.io.resp.ready, icache.io.resp.bits.pc, icache.io.resp.bits.mask) 585 XSDebug("[IF3][bp] taken=%d jmpIdx=%d hasNTBrs=%d target=%x saveHalfRVI=%d\n", if3_bp.taken, if3_bp.jmpIdx, if3_bp.hasNotTakenBrs, if3_bp.target, if3_bp.saveHalfRVI) 586 XSDebug("[IF3][redirect]: v=%d, prevNMet=%d, predT=%d, predNT=%d\n", if3_redirect, if3_prevHalfNotMetRedirect, if3_predTakenRedirect, if3_predNotTakenRedirect) 587 // XSDebug("[IF3][prevHalfInstr] v=%d redirect=%d fetchpc=%x idx=%d tgt=%x taken=%d instr=%x\n\n", 588 // prev_half_valid, prev_half_redirect, prev_half_fetchpc, prev_half_idx, prev_half_tgt, prev_half_taken, prev_half_instr) 589 XSDebug("[IF3][if3_prevHalfInstr] v=%d pc=%x npc=%x instr=%x ipf=%d\n\n", 590 if3_prevHalfInstr.valid, if3_prevHalfInstr.bits.pc, if3_prevHalfInstr.bits.npc, if3_prevHalfInstr.bits.instr, if3_prevHalfInstr.bits.ipf) 591 if3_gh.debug("if3") 592 593 XSDebug("[IF4][predecode] mask=%b\n", if4_pd.mask) 594 XSDebug("[IF4][snpc]: %x, realMask=%b\n", if4_snpc, if4_mask) 595 XSDebug("[IF4][bp] taken=%d jmpIdx=%d hasNTBrs=%d target=%x saveHalfRVI=%d\n", if4_bp.taken, if4_bp.jmpIdx, if4_bp.hasNotTakenBrs, if4_bp.target, if4_bp.saveHalfRVI) 596 XSDebug("[IF4][redirect]: v=%d, prevNotMet=%d, predT=%d, predNT=%d\n", if4_redirect, if4_prevHalfNextNotMet, if4_predTakenRedirect, if4_predNotTakenRedirect) 597 XSDebug(if4_pd.pd(if4_bp.jmpIdx).isJal && if4_bp.taken, "[IF4] cfi is jal! instr=%x target=%x\n", if4_instrs(if4_bp.jmpIdx), if4_jal_tgts(if4_bp.jmpIdx)) 598 XSDebug("[IF4][ prevHalfInstrReq] v=%d pc=%x npc=%x instr=%x ipf=%d\n", 599 prevHalfInstrReq.valid, prevHalfInstrReq.bits.pc, prevHalfInstrReq.bits.npc, prevHalfInstrReq.bits.instr, prevHalfInstrReq.bits.ipf) 600 XSDebug("[IF4][if4_prevHalfInstr] v=%d pc=%x npc=%x instr=%x ipf=%d\n", 601 if4_prevHalfInstr.valid, if4_prevHalfInstr.bits.pc, if4_prevHalfInstr.bits.npc, if4_prevHalfInstr.bits.instr, if4_prevHalfInstr.bits.ipf) 602 if4_gh.debug("if4") 603 XSDebug(io.fetchPacket.fire(), "[IF4][fetchPacket] v=%d r=%d mask=%b ipf=%d acf=%d crossPageIPF=%d\n", 604 io.fetchPacket.valid, io.fetchPacket.ready, io.fetchPacket.bits.mask, io.fetchPacket.bits.ipf, io.fetchPacket.bits.acf, io.fetchPacket.bits.crossPageIPFFix) 605 for (i <- 0 until PredictWidth) { 606 XSDebug(io.fetchPacket.fire(), "[IF4][fetchPacket] %b %x pc=%x pd: rvc=%d brType=%b call=%d ret=%d\n", 607 io.fetchPacket.bits.mask(i), 608 io.fetchPacket.bits.instrs(i), 609 io.fetchPacket.bits.pc(i), 610 io.fetchPacket.bits.pd(i).isRVC, 611 io.fetchPacket.bits.pd(i).brType, 612 io.fetchPacket.bits.pd(i).isCall, 613 io.fetchPacket.bits.pd(i).isRet 614 ) 615 } 616 val b = ftqEnqBuf 617 XSDebug("[FtqEnqBuf] v=%d r=%d pc=%x cfiIndex(%d)=%d cfiIsCall=%d cfiIsRet=%d cfiIsRVC=%d\n", 618 ftqEnqBuf_valid, ftqEnqBuf_ready, b.ftqPC, b.cfiIndex.valid, b.cfiIndex.bits, b.cfiIsCall, b.cfiIsRet, b.cfiIsRVC) 619 XSDebug("[FtqEnqBuf] valids=%b br_mask=%b rvc_mask=%b hist=%x predHist=%x rasSp=%d rasTopAddr=%x rasTopCtr=%d\n", 620 b.valids.asUInt, b.br_mask.asUInt, b.rvc_mask.asUInt, b.hist.asUInt, b.predHist.asUInt, b.rasSp, b.rasTop.retAddr, b.rasTop.ctr) 621 XSDebug("[ToFTQ] v=%d r=%d leftOne=%d ptr=%d\n", io.toFtq.valid, io.toFtq.ready, io.ftqLeftOne, io.ftqEnqPtr.value) 622 } 623 624} 625