109c6f1ddSLingrui98/*************************************************************************************** 209c6f1ddSLingrui98* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 309c6f1ddSLingrui98* Copyright (c) 2020-2021 Peng Cheng Laboratory 409c6f1ddSLingrui98* 509c6f1ddSLingrui98* XiangShan is licensed under Mulan PSL v2. 609c6f1ddSLingrui98* You can use this software according to the terms and conditions of the Mulan PSL v2. 709c6f1ddSLingrui98* You may obtain a copy of Mulan PSL v2 at: 809c6f1ddSLingrui98* http://license.coscl.org.cn/MulanPSL2 909c6f1ddSLingrui98* 1009c6f1ddSLingrui98* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 1109c6f1ddSLingrui98* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 1209c6f1ddSLingrui98* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 1309c6f1ddSLingrui98* 1409c6f1ddSLingrui98* See the Mulan PSL v2 for more details. 1509c6f1ddSLingrui98***************************************************************************************/ 1609c6f1ddSLingrui98 1709c6f1ddSLingrui98package xiangshan.frontend 1809c6f1ddSLingrui98 1909c6f1ddSLingrui98import chipsalliance.rocketchip.config.Parameters 2009c6f1ddSLingrui98import chisel3._ 2109c6f1ddSLingrui98import chisel3.util._ 2209c6f1ddSLingrui98import xiangshan._ 2309c6f1ddSLingrui98import xiangshan.cache._ 2409c6f1ddSLingrui98import xiangshan.cache.mmu._ 2509c6f1ddSLingrui98import chisel3.experimental.verification 2609c6f1ddSLingrui98import utils._ 27b6982e83SLemoverimport xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle} 2809c6f1ddSLingrui98 2909c6f1ddSLingrui98trait HasInstrMMIOConst extends HasXSParameter with HasIFUConst{ 3009c6f1ddSLingrui98 def mmioBusWidth = 64 3109c6f1ddSLingrui98 def mmioBusBytes = mmioBusWidth /8 320be662e4SJay def maxInstrLen = 32 3309c6f1ddSLingrui98} 3409c6f1ddSLingrui98 3509c6f1ddSLingrui98trait HasIFUConst extends HasXSParameter { 3609c6f1ddSLingrui98 def align(pc: UInt, bytes: Int): UInt = Cat(pc(VAddrBits-1, log2Ceil(bytes)), 0.U(log2Ceil(bytes).W)) 3709c6f1ddSLingrui98 // def groupAligned(pc: UInt) = align(pc, groupBytes) 3809c6f1ddSLingrui98 // def packetAligned(pc: UInt) = align(pc, packetBytes) 3909c6f1ddSLingrui98} 4009c6f1ddSLingrui98 4109c6f1ddSLingrui98class IfuToFtqIO(implicit p:Parameters) extends XSBundle { 4209c6f1ddSLingrui98 val pdWb = Valid(new PredecodeWritebackBundle) 4309c6f1ddSLingrui98} 4409c6f1ddSLingrui98 4509c6f1ddSLingrui98class FtqInterface(implicit p: Parameters) extends XSBundle { 4609c6f1ddSLingrui98 val fromFtq = Flipped(new FtqToIfuIO) 4709c6f1ddSLingrui98 val toFtq = new IfuToFtqIO 4809c6f1ddSLingrui98} 4909c6f1ddSLingrui98 500be662e4SJayclass UncacheInterface(implicit p: Parameters) extends XSBundle { 510be662e4SJay val fromUncache = Flipped(DecoupledIO(new InsUncacheResp)) 520be662e4SJay val toUncache = DecoupledIO( new InsUncacheReq ) 530be662e4SJay} 540be662e4SJay 5509c6f1ddSLingrui98class ICacheInterface(implicit p: Parameters) extends XSBundle { 5609c6f1ddSLingrui98 val toIMeta = Decoupled(new ICacheReadBundle) 5709c6f1ddSLingrui98 val toIData = Decoupled(new ICacheReadBundle) 5809c6f1ddSLingrui98 val toMissQueue = Vec(2,Decoupled(new ICacheMissReq)) 5909c6f1ddSLingrui98 val fromIMeta = Input(new ICacheMetaRespBundle) 6009c6f1ddSLingrui98 val fromIData = Input(new ICacheDataRespBundle) 6109c6f1ddSLingrui98 val fromMissQueue = Vec(2,Flipped(Decoupled(new ICacheMissResp))) 6209c6f1ddSLingrui98} 6309c6f1ddSLingrui98 6409c6f1ddSLingrui98class NewIFUIO(implicit p: Parameters) extends XSBundle { 6509c6f1ddSLingrui98 val ftqInter = new FtqInterface 6609c6f1ddSLingrui98 val icacheInter = new ICacheInterface 6709c6f1ddSLingrui98 val toIbuffer = Decoupled(new FetchToIBuffer) 6809c6f1ddSLingrui98 val iTLBInter = Vec(2, new BlockTlbRequestIO) 690be662e4SJay val uncacheInter = new UncacheInterface 70b6982e83SLemover val pmp = Vec(2, new Bundle { 71b6982e83SLemover val req = Valid(new PMPReqBundle()) 72ca2f90a6SLemover val resp = Flipped(new PMPRespBundle()) 73b6982e83SLemover }) 7409c6f1ddSLingrui98} 7509c6f1ddSLingrui98 7609c6f1ddSLingrui98// record the situation in which fallThruAddr falls into 7709c6f1ddSLingrui98// the middle of an RVI inst 7809c6f1ddSLingrui98class LastHalfInfo(implicit p: Parameters) extends XSBundle { 7909c6f1ddSLingrui98 val valid = Bool() 8009c6f1ddSLingrui98 val middlePC = UInt(VAddrBits.W) 8109c6f1ddSLingrui98 def matchThisBlock(startAddr: UInt) = valid && middlePC === startAddr 8209c6f1ddSLingrui98} 8309c6f1ddSLingrui98 8409c6f1ddSLingrui98class IfuToPreDecode(implicit p: Parameters) extends XSBundle { 8509c6f1ddSLingrui98 val data = if(HasCExtension) Vec(PredictWidth + 1, UInt(16.W)) else Vec(PredictWidth, UInt(32.W)) 8609c6f1ddSLingrui98 val startAddr = UInt(VAddrBits.W) 8709c6f1ddSLingrui98 val fallThruAddr = UInt(VAddrBits.W) 8809c6f1ddSLingrui98 val fallThruError = Bool() 8909c6f1ddSLingrui98 val isDoubleLine = Bool() 9009c6f1ddSLingrui98 val ftqOffset = Valid(UInt(log2Ceil(PredictWidth).W)) 9109c6f1ddSLingrui98 val target = UInt(VAddrBits.W) 9209c6f1ddSLingrui98 val pageFault = Vec(2, Bool()) 9309c6f1ddSLingrui98 val accessFault = Vec(2, Bool()) 9409c6f1ddSLingrui98 val instValid = Bool() 9509c6f1ddSLingrui98 val lastHalfMatch = Bool() 9609c6f1ddSLingrui98 val oversize = Bool() 970be662e4SJay val mmio = Bool() 9809c6f1ddSLingrui98} 9909c6f1ddSLingrui98 10009c6f1ddSLingrui98class NewIFU(implicit p: Parameters) extends XSModule with HasICacheParameters 10109c6f1ddSLingrui98{ 10209c6f1ddSLingrui98 println(s"icache ways: ${nWays} sets:${nSets}") 10309c6f1ddSLingrui98 val io = IO(new NewIFUIO) 10409c6f1ddSLingrui98 val (toFtq, fromFtq) = (io.ftqInter.toFtq, io.ftqInter.fromFtq) 10509c6f1ddSLingrui98 val (toMeta, toData, meta_resp, data_resp) = (io.icacheInter.toIMeta, io.icacheInter.toIData, io.icacheInter.fromIMeta, io.icacheInter.fromIData) 10609c6f1ddSLingrui98 val (toMissQueue, fromMissQueue) = (io.icacheInter.toMissQueue, io.icacheInter.fromMissQueue) 1070be662e4SJay val (toUncache, fromUncache) = (io.uncacheInter.toUncache , io.uncacheInter.fromUncache) 10809c6f1ddSLingrui98 val (toITLB, fromITLB) = (VecInit(io.iTLBInter.map(_.req)), VecInit(io.iTLBInter.map(_.resp))) 109b6982e83SLemover val fromPMP = io.pmp.map(_.resp) 11009c6f1ddSLingrui98 11109c6f1ddSLingrui98 def isCrossLineReq(start: UInt, end: UInt): Bool = start(blockOffBits) ^ end(blockOffBits) 11209c6f1ddSLingrui98 11309c6f1ddSLingrui98 def isLastInCacheline(fallThruAddr: UInt): Bool = fallThruAddr(blockOffBits - 1, 1) === 0.U 11409c6f1ddSLingrui98 11509c6f1ddSLingrui98 11609c6f1ddSLingrui98 //--------------------------------------------- 11709c6f1ddSLingrui98 // Fetch Stage 1 : 11809c6f1ddSLingrui98 // * Send req to ICache Meta/Data 11909c6f1ddSLingrui98 // * Check whether need 2 line fetch 12009c6f1ddSLingrui98 //--------------------------------------------- 12109c6f1ddSLingrui98 12209c6f1ddSLingrui98 val f0_valid = fromFtq.req.valid 12309c6f1ddSLingrui98 val f0_ftq_req = fromFtq.req.bits 12409c6f1ddSLingrui98 val f0_situation = VecInit(Seq(isCrossLineReq(f0_ftq_req.startAddr, f0_ftq_req.fallThruAddr), isLastInCacheline(f0_ftq_req.fallThruAddr))) 12509c6f1ddSLingrui98 val f0_doubleLine = f0_situation(0) || f0_situation(1) 12609c6f1ddSLingrui98 val f0_vSetIdx = VecInit(get_idx((f0_ftq_req.startAddr)), get_idx(f0_ftq_req.fallThruAddr)) 12709c6f1ddSLingrui98 val f0_fire = fromFtq.req.fire() 12809c6f1ddSLingrui98 12909c6f1ddSLingrui98 val f0_flush, f1_flush, f2_flush, f3_flush = WireInit(false.B) 13009c6f1ddSLingrui98 val from_bpu_f0_flush, from_bpu_f1_flush, from_bpu_f2_flush, from_bpu_f3_flush = WireInit(false.B) 13109c6f1ddSLingrui98 13209c6f1ddSLingrui98 from_bpu_f0_flush := fromFtq.flushFromBpu.shouldFlushByStage2(f0_ftq_req.ftqIdx) || 13309c6f1ddSLingrui98 fromFtq.flushFromBpu.shouldFlushByStage3(f0_ftq_req.ftqIdx) 13409c6f1ddSLingrui98 13509c6f1ddSLingrui98 val f3_redirect = WireInit(false.B) 13609c6f1ddSLingrui98 f3_flush := fromFtq.redirect.valid 13709c6f1ddSLingrui98 f2_flush := f3_flush || f3_redirect 13809c6f1ddSLingrui98 f1_flush := f2_flush || from_bpu_f1_flush 13909c6f1ddSLingrui98 f0_flush := f1_flush || from_bpu_f0_flush 14009c6f1ddSLingrui98 14109c6f1ddSLingrui98 val f1_ready, f2_ready, f3_ready = WireInit(false.B) 14209c6f1ddSLingrui98 14309c6f1ddSLingrui98 //fetch: send addr to Meta/TLB and Data simultaneously 14409c6f1ddSLingrui98 val fetch_req = List(toMeta, toData) 14509c6f1ddSLingrui98 for(i <- 0 until 2) { 14609c6f1ddSLingrui98 fetch_req(i).valid := f0_fire 14709c6f1ddSLingrui98 fetch_req(i).bits.isDoubleLine := f0_doubleLine 14809c6f1ddSLingrui98 fetch_req(i).bits.vSetIdx := f0_vSetIdx 14909c6f1ddSLingrui98 } 15009c6f1ddSLingrui98 15109c6f1ddSLingrui98 fromFtq.req.ready := fetch_req(0).ready && fetch_req(1).ready && f1_ready && GTimer() > 500.U 15209c6f1ddSLingrui98 153f7c29b0aSJinYue XSPerfAccumulate("ifu_bubble_ftq_not_valid", !f0_valid ) 154f7c29b0aSJinYue XSPerfAccumulate("ifu_bubble_pipe_stall", f0_valid && fetch_req(0).ready && fetch_req(1).ready && !f1_ready ) 155f7c29b0aSJinYue XSPerfAccumulate("ifu_bubble_sram_0_busy", f0_valid && !fetch_req(0).ready ) 156f7c29b0aSJinYue XSPerfAccumulate("ifu_bubble_sram_1_busy", f0_valid && !fetch_req(1).ready ) 157f7c29b0aSJinYue 15809c6f1ddSLingrui98 //--------------------------------------------- 15909c6f1ddSLingrui98 // Fetch Stage 2 : 16009c6f1ddSLingrui98 // * Send req to ITLB and TLB Response (Get Paddr) 16109c6f1ddSLingrui98 // * ICache Response (Get Meta and Data) 16209c6f1ddSLingrui98 // * Hit Check (Generate hit signal and hit vector) 16309c6f1ddSLingrui98 // * Get victim way 16409c6f1ddSLingrui98 //--------------------------------------------- 16509c6f1ddSLingrui98 16609c6f1ddSLingrui98 //TODO: handle fetch exceptions 16709c6f1ddSLingrui98 16809c6f1ddSLingrui98 val tlbRespAllValid = WireInit(false.B) 16909c6f1ddSLingrui98 17009c6f1ddSLingrui98 val f1_valid = RegInit(false.B) 17109c6f1ddSLingrui98 val f1_ftq_req = RegEnable(next = f0_ftq_req, enable=f0_fire) 17209c6f1ddSLingrui98 val f1_situation = RegEnable(next = f0_situation, enable=f0_fire) 17309c6f1ddSLingrui98 val f1_doubleLine = RegEnable(next = f0_doubleLine, enable=f0_fire) 17409c6f1ddSLingrui98 val f1_vSetIdx = RegEnable(next = f0_vSetIdx, enable=f0_fire) 17509c6f1ddSLingrui98 val f1_fire = f1_valid && tlbRespAllValid && f2_ready 17609c6f1ddSLingrui98 17709c6f1ddSLingrui98 f1_ready := f2_ready && tlbRespAllValid || !f1_valid 17809c6f1ddSLingrui98 17909c6f1ddSLingrui98 from_bpu_f1_flush := fromFtq.flushFromBpu.shouldFlushByStage3(f1_ftq_req.ftqIdx) 18009c6f1ddSLingrui98 18109c6f1ddSLingrui98 val preDecoder = Module(new PreDecode) 18209c6f1ddSLingrui98 val (preDecoderIn, preDecoderOut) = (preDecoder.io.in, preDecoder.io.out) 18309c6f1ddSLingrui98 18409c6f1ddSLingrui98 //flush generate and to Ftq 18509c6f1ddSLingrui98 val predecodeOutValid = WireInit(false.B) 18609c6f1ddSLingrui98 18709c6f1ddSLingrui98 when(f1_flush) {f1_valid := false.B} 18809c6f1ddSLingrui98 .elsewhen(f0_fire && !f0_flush) {f1_valid := true.B} 18909c6f1ddSLingrui98 .elsewhen(f1_fire) {f1_valid := false.B} 19009c6f1ddSLingrui98 19109c6f1ddSLingrui98 toITLB(0).valid := f1_valid 192b6982e83SLemover toITLB(0).bits.size := 3.U // TODO: fix the size 19309c6f1ddSLingrui98 toITLB(0).bits.vaddr := align(f1_ftq_req.startAddr, blockBytes) 19409c6f1ddSLingrui98 toITLB(0).bits.debug.pc := align(f1_ftq_req.startAddr, blockBytes) 19509c6f1ddSLingrui98 19609c6f1ddSLingrui98 toITLB(1).valid := f1_valid && f1_doubleLine 197b6982e83SLemover toITLB(1).bits.size := 3.U // TODO: fix the size 19809c6f1ddSLingrui98 toITLB(1).bits.vaddr := align(f1_ftq_req.fallThruAddr, blockBytes) 19909c6f1ddSLingrui98 toITLB(1).bits.debug.pc := align(f1_ftq_req.fallThruAddr, blockBytes) 20009c6f1ddSLingrui98 20109c6f1ddSLingrui98 toITLB.map{port => 20209c6f1ddSLingrui98 port.bits.cmd := TlbCmd.exec 2039aca92b9SYinan Xu port.bits.robIdx := DontCare 20409c6f1ddSLingrui98 port.bits.debug.isFirstIssue := DontCare 20509c6f1ddSLingrui98 } 20609c6f1ddSLingrui98 20709c6f1ddSLingrui98 fromITLB.map(_.ready := true.B) 20809c6f1ddSLingrui98 20909c6f1ddSLingrui98 val (tlbRespValid, tlbRespPAddr) = (fromITLB.map(_.valid), VecInit(fromITLB.map(_.bits.paddr))) 210ca2f90a6SLemover val (tlbRespMiss) = (fromITLB.map(port => port.bits.miss && port.valid)) 211b6982e83SLemover val (tlbExcpPF, tlbExcpAF) = (fromITLB.map(port => port.bits.excp.pf.instr && port.valid), 212b6982e83SLemover fromITLB.map(port => (port.bits.excp.af.instr) && port.valid)) //TODO: Temp treat mmio req as access fault 213b6982e83SLemover 21409c6f1ddSLingrui98 tlbRespAllValid := tlbRespValid(0) && (tlbRespValid(1) || !f1_doubleLine) 21509c6f1ddSLingrui98 21609c6f1ddSLingrui98 val f1_pAddrs = tlbRespPAddr //TODO: Temporary assignment 21703c39bdeSJinYue val f1_pTags = VecInit(f1_pAddrs.map(get_phy_tag(_))) 21809c6f1ddSLingrui98 val (f1_tags, f1_cacheline_valid, f1_datas) = (meta_resp.tags, meta_resp.valid, data_resp.datas) 21909c6f1ddSLingrui98 val bank0_hit_vec = VecInit(f1_tags(0).zipWithIndex.map{ case(way_tag,i) => f1_cacheline_valid(0)(i) && way_tag === f1_pTags(0) }) 22009c6f1ddSLingrui98 val bank1_hit_vec = VecInit(f1_tags(1).zipWithIndex.map{ case(way_tag,i) => f1_cacheline_valid(1)(i) && way_tag === f1_pTags(1) }) 22109c6f1ddSLingrui98 val (bank0_hit,bank1_hit) = (ParallelOR(bank0_hit_vec) && !tlbExcpPF(0) && !tlbExcpAF(0), ParallelOR(bank1_hit_vec) && !tlbExcpPF(1) && !tlbExcpAF(1)) 22209c6f1ddSLingrui98 val f1_hit = (bank0_hit && bank1_hit && f1_valid && f1_doubleLine) || (f1_valid && !f1_doubleLine && bank0_hit) 22309c6f1ddSLingrui98 val f1_bank_hit_vec = VecInit(Seq(bank0_hit_vec, bank1_hit_vec)) 22409c6f1ddSLingrui98 val f1_bank_hit = VecInit(Seq(bank0_hit, bank1_hit)) 22509c6f1ddSLingrui98 2260be662e4SJay //MMIO 2270be662e4SJay //MMIO only need 1 instruction 228ca2f90a6SLemover // val f1_mmio = tlbRespMMIO(0) && f1_valid 2290be662e4SJay 2300be662e4SJay 23109c6f1ddSLingrui98 val replacers = Seq.fill(2)(ReplacementPolicy.fromString(Some("random"),nWays,nSets/2)) 23209c6f1ddSLingrui98 val f1_victim_masks = VecInit(replacers.zipWithIndex.map{case (replacer, i) => UIntToOH(replacer.way(f1_vSetIdx(i)))}) 23309c6f1ddSLingrui98 23409c6f1ddSLingrui98 val touch_sets = Seq.fill(2)(Wire(Vec(2, UInt(log2Ceil(nSets/2).W)))) 23509c6f1ddSLingrui98 val touch_ways = Seq.fill(2)(Wire(Vec(2, Valid(UInt(log2Ceil(nWays).W)))) ) 23609c6f1ddSLingrui98 23709c6f1ddSLingrui98 ((replacers zip touch_sets) zip touch_ways).map{case ((r, s),w) => r.access(s,w)} 23809c6f1ddSLingrui98 23909c6f1ddSLingrui98 val f1_hit_data = VecInit(f1_datas.zipWithIndex.map { case(bank, i) => 24009c6f1ddSLingrui98 val bank_hit_data = Mux1H(f1_bank_hit_vec(i).asUInt, bank) 24109c6f1ddSLingrui98 bank_hit_data 24209c6f1ddSLingrui98 }) 24309c6f1ddSLingrui98 244f7c29b0aSJinYue (0 until nWays).map{ w => 245f7c29b0aSJinYue XSPerfAccumulate("line_0_hit_way_" + Integer.toString(w, 10), f1_fire && f1_bank_hit(0) && OHToUInt(f1_bank_hit_vec(0)) === w.U) 246f7c29b0aSJinYue } 247f7c29b0aSJinYue 248f7c29b0aSJinYue (0 until nWays).map{ w => 249f7c29b0aSJinYue XSPerfAccumulate("line_0_victim_way_" + Integer.toString(w, 10), f1_fire && !f1_bank_hit(0) && OHToUInt(f1_victim_masks(0)) === w.U) 250f7c29b0aSJinYue } 251f7c29b0aSJinYue 252f7c29b0aSJinYue (0 until nWays).map{ w => 253f7c29b0aSJinYue XSPerfAccumulate("line_1_hit_way_" + Integer.toString(w, 10), f1_fire && f1_doubleLine && f1_bank_hit(1) && OHToUInt(f1_bank_hit_vec(1)) === w.U) 254f7c29b0aSJinYue } 255f7c29b0aSJinYue 256f7c29b0aSJinYue (0 until nWays).map{ w => 257f7c29b0aSJinYue XSPerfAccumulate("line_1_victim_way_" + Integer.toString(w, 10), f1_fire && f1_doubleLine && !f1_bank_hit(1) && OHToUInt(f1_victim_masks(1)) === w.U) 258f7c29b0aSJinYue } 259f7c29b0aSJinYue 260f7c29b0aSJinYue XSPerfAccumulate("ifu_bubble_f1_tlb_miss", f1_valid && !tlbRespAllValid ) 26109c6f1ddSLingrui98 26209c6f1ddSLingrui98 //--------------------------------------------- 26309c6f1ddSLingrui98 // Fetch Stage 3 : 26409c6f1ddSLingrui98 // * get data from last stage (hit from f1_hit_data/miss from missQueue response) 26509c6f1ddSLingrui98 // * if at least one needed cacheline miss, wait for miss queue response (a wait_state machine) THIS IS TOO UGLY!!! 26609c6f1ddSLingrui98 // * cut cacheline(s) and send to PreDecode 26709c6f1ddSLingrui98 // * check if prediction is right (branch target and type, jump direction and type , jal target ) 26809c6f1ddSLingrui98 //--------------------------------------------- 26909c6f1ddSLingrui98 val f2_fetchFinish = Wire(Bool()) 27009c6f1ddSLingrui98 27109c6f1ddSLingrui98 val f2_valid = RegInit(false.B) 27209c6f1ddSLingrui98 val f2_ftq_req = RegEnable(next = f1_ftq_req, enable = f1_fire) 27309c6f1ddSLingrui98 val f2_situation = RegEnable(next = f1_situation, enable=f1_fire) 27409c6f1ddSLingrui98 val f2_doubleLine = RegEnable(next = f1_doubleLine, enable=f1_fire) 27509c6f1ddSLingrui98 val f2_fire = f2_valid && f2_fetchFinish && f3_ready 27609c6f1ddSLingrui98 27709c6f1ddSLingrui98 f2_ready := (f3_ready && f2_fetchFinish) || !f2_valid 27809c6f1ddSLingrui98 27909c6f1ddSLingrui98 when(f2_flush) {f2_valid := false.B} 28009c6f1ddSLingrui98 .elsewhen(f1_fire && !f1_flush) {f2_valid := true.B } 28109c6f1ddSLingrui98 .elsewhen(f2_fire) {f2_valid := false.B} 28209c6f1ddSLingrui98 283b6982e83SLemover val pmpExcpAF = fromPMP.map(port => port.instr) 284ca2f90a6SLemover val mmio = fromPMP.map(port => port.mmio) // TODO: handle it 28509c6f1ddSLingrui98 2860be662e4SJay 28709c6f1ddSLingrui98 val f2_pAddrs = RegEnable(next = f1_pAddrs, enable = f1_fire) 28809c6f1ddSLingrui98 val f2_hit = RegEnable(next = f1_hit , enable = f1_fire) 28909c6f1ddSLingrui98 val f2_bank_hit = RegEnable(next = f1_bank_hit, enable = f1_fire) 29009c6f1ddSLingrui98 val f2_miss = f2_valid && !f2_hit 29109c6f1ddSLingrui98 val (f2_vSetIdx, f2_pTags) = (RegEnable(next = f1_vSetIdx, enable = f1_fire), RegEnable(next = f1_pTags, enable = f1_fire)) 29209c6f1ddSLingrui98 val f2_waymask = RegEnable(next = f1_victim_masks, enable = f1_fire) 29309c6f1ddSLingrui98 //exception information 29409c6f1ddSLingrui98 val f2_except_pf = RegEnable(next = VecInit(tlbExcpPF), enable = f1_fire) 295b6982e83SLemover val f2_except_af = VecInit(RegEnable(next = VecInit(tlbExcpAF), enable = f1_fire).zip(pmpExcpAF).map(a => a._1 || DataHoldBypass(a._2, RegNext(f1_fire)).asBool)) 29609c6f1ddSLingrui98 val f2_except = VecInit((0 until 2).map{i => f2_except_pf(i) || f2_except_af(i)}) 29709c6f1ddSLingrui98 val f2_has_except = f2_valid && (f2_except_af.reduce(_||_) || f2_except_pf.reduce(_||_)) 2980be662e4SJay //MMIO 299*16c9060fSJay val f2_mmio = DataHoldBypass(io.pmp(0).resp.mmio && !f2_except_af(0) && !f2_except_pf(0), RegNext(f1_fire)).asBool() 3000be662e4SJay 301b6982e83SLemover io.pmp.zipWithIndex.map { case (p, i) => 302b6982e83SLemover p.req.valid := f2_fire 303b6982e83SLemover p.req.bits.addr := f2_pAddrs(i) 304b6982e83SLemover p.req.bits.size := 3.U // TODO 305b6982e83SLemover p.req.bits.cmd := TlbCmd.exec 306b6982e83SLemover } 30709c6f1ddSLingrui98 30809c6f1ddSLingrui98 //instruction 3090be662e4SJay val wait_idle :: wait_queue_ready :: wait_send_req :: wait_two_resp :: wait_0_resp :: wait_1_resp :: wait_one_resp ::wait_finish :: wait_send_mmio :: wait_mmio_resp ::Nil = Enum(10) 31009c6f1ddSLingrui98 val wait_state = RegInit(wait_idle) 31109c6f1ddSLingrui98 31209c6f1ddSLingrui98 fromMissQueue.map{port => port.ready := true.B} 31309c6f1ddSLingrui98 31409c6f1ddSLingrui98 val (miss0_resp, miss1_resp) = (fromMissQueue(0).fire(), fromMissQueue(1).fire()) 31509c6f1ddSLingrui98 val (bank0_fix, bank1_fix) = (miss0_resp && !f2_bank_hit(0), miss1_resp && f2_doubleLine && !f2_bank_hit(1)) 31609c6f1ddSLingrui98 3170be662e4SJay val only_0_miss = f2_valid && !f2_hit && !f2_doubleLine && !f2_has_except && !f2_mmio 3180be662e4SJay val only_0_hit = f2_valid && f2_hit && !f2_doubleLine && !f2_mmio 3190be662e4SJay val hit_0_hit_1 = f2_valid && f2_hit && f2_doubleLine && !f2_mmio 3200be662e4SJay val (hit_0_miss_1 , miss_0_hit_1, miss_0_miss_1) = ( (f2_valid && !f2_bank_hit(1) && f2_bank_hit(0) && f2_doubleLine && !f2_has_except && !f2_mmio), 3210be662e4SJay (f2_valid && !f2_bank_hit(0) && f2_bank_hit(1) && f2_doubleLine && !f2_has_except && !f2_mmio), 3220be662e4SJay (f2_valid && !f2_bank_hit(0) && !f2_bank_hit(1) && f2_doubleLine && !f2_has_except && !f2_mmio), 32309c6f1ddSLingrui98 ) 32409c6f1ddSLingrui98 32509c6f1ddSLingrui98 val hit_0_except_1 = f2_valid && f2_doubleLine && !f2_except(0) && f2_except(1) && f2_bank_hit(0) 32609c6f1ddSLingrui98 val miss_0_except_1 = f2_valid && f2_doubleLine && !f2_except(0) && f2_except(1) && !f2_bank_hit(0) 32709c6f1ddSLingrui98 //val fetch0_except_1 = hit_0_except_1 || miss_0_except_1 32809c6f1ddSLingrui98 val except_0 = f2_valid && f2_except(0) 32909c6f1ddSLingrui98 33009c6f1ddSLingrui98 val f2_mq_datas = Reg(Vec(2, UInt(blockBits.W))) 3310be662e4SJay val f2_mmio_data = Reg(UInt(maxInstrLen.W)) 33209c6f1ddSLingrui98 33309c6f1ddSLingrui98 when(fromMissQueue(0).fire) {f2_mq_datas(0) := fromMissQueue(0).bits.data} 33409c6f1ddSLingrui98 when(fromMissQueue(1).fire) {f2_mq_datas(1) := fromMissQueue(1).bits.data} 3350be662e4SJay when(fromUncache.fire()) {f2_mmio_data := fromUncache.bits.data} 33609c6f1ddSLingrui98 33709c6f1ddSLingrui98 switch(wait_state){ 33809c6f1ddSLingrui98 is(wait_idle){ 339ca2f90a6SLemover when(f2_mmio && f2_valid && !f2_except_af(0) && !f2_except_pf(0)){ 3400be662e4SJay wait_state := wait_send_mmio 3410be662e4SJay }.elsewhen(miss_0_except_1){ 34209c6f1ddSLingrui98 wait_state := Mux(toMissQueue(0).ready, wait_queue_ready ,wait_idle ) 34309c6f1ddSLingrui98 }.elsewhen( only_0_miss || miss_0_hit_1){ 34409c6f1ddSLingrui98 wait_state := Mux(toMissQueue(0).ready, wait_queue_ready ,wait_idle ) 34509c6f1ddSLingrui98 }.elsewhen(hit_0_miss_1){ 34609c6f1ddSLingrui98 wait_state := Mux(toMissQueue(1).ready, wait_queue_ready ,wait_idle ) 34709c6f1ddSLingrui98 }.elsewhen( miss_0_miss_1 ){ 34809c6f1ddSLingrui98 wait_state := Mux(toMissQueue(0).ready && toMissQueue(1).ready, wait_queue_ready ,wait_idle) 34909c6f1ddSLingrui98 } 35009c6f1ddSLingrui98 } 35109c6f1ddSLingrui98 3520be662e4SJay is(wait_send_mmio){ 3530be662e4SJay wait_state := Mux(toUncache.fire(), wait_mmio_resp,wait_send_mmio ) 3540be662e4SJay } 3550be662e4SJay 3560be662e4SJay is(wait_mmio_resp){ 3570be662e4SJay wait_state := Mux(fromUncache.fire(), wait_finish, wait_mmio_resp) 3580be662e4SJay } 3590be662e4SJay 36009c6f1ddSLingrui98 //TODO: naive logic for wait icache response 36109c6f1ddSLingrui98 is(wait_queue_ready){ 36209c6f1ddSLingrui98 wait_state := wait_send_req 36309c6f1ddSLingrui98 } 36409c6f1ddSLingrui98 36509c6f1ddSLingrui98 is(wait_send_req) { 36609c6f1ddSLingrui98 when(miss_0_except_1 || only_0_miss || hit_0_miss_1 || miss_0_hit_1){ 36709c6f1ddSLingrui98 wait_state := wait_one_resp 36809c6f1ddSLingrui98 }.elsewhen( miss_0_miss_1 ){ 36909c6f1ddSLingrui98 wait_state := wait_two_resp 37009c6f1ddSLingrui98 } 37109c6f1ddSLingrui98 } 37209c6f1ddSLingrui98 37309c6f1ddSLingrui98 is(wait_one_resp) { 37409c6f1ddSLingrui98 when( (miss_0_except_1 ||only_0_miss || miss_0_hit_1) && fromMissQueue(0).fire()){ 37509c6f1ddSLingrui98 wait_state := wait_finish 37609c6f1ddSLingrui98 }.elsewhen( hit_0_miss_1 && fromMissQueue(1).fire()){ 37709c6f1ddSLingrui98 wait_state := wait_finish 37809c6f1ddSLingrui98 } 37909c6f1ddSLingrui98 } 38009c6f1ddSLingrui98 38109c6f1ddSLingrui98 is(wait_two_resp) { 38209c6f1ddSLingrui98 when(fromMissQueue(0).fire() && fromMissQueue(1).fire()){ 38309c6f1ddSLingrui98 wait_state := wait_finish 38409c6f1ddSLingrui98 }.elsewhen( !fromMissQueue(0).fire() && fromMissQueue(1).fire() ){ 38509c6f1ddSLingrui98 wait_state := wait_0_resp 38609c6f1ddSLingrui98 }.elsewhen(fromMissQueue(0).fire() && !fromMissQueue(1).fire()){ 38709c6f1ddSLingrui98 wait_state := wait_1_resp 38809c6f1ddSLingrui98 } 38909c6f1ddSLingrui98 } 39009c6f1ddSLingrui98 39109c6f1ddSLingrui98 is(wait_0_resp) { 39209c6f1ddSLingrui98 when(fromMissQueue(0).fire()){ 39309c6f1ddSLingrui98 wait_state := wait_finish 39409c6f1ddSLingrui98 } 39509c6f1ddSLingrui98 } 39609c6f1ddSLingrui98 39709c6f1ddSLingrui98 is(wait_1_resp) { 39809c6f1ddSLingrui98 when(fromMissQueue(1).fire()){ 39909c6f1ddSLingrui98 wait_state := wait_finish 40009c6f1ddSLingrui98 } 40109c6f1ddSLingrui98 } 40209c6f1ddSLingrui98 40309c6f1ddSLingrui98 is(wait_finish) { 40409c6f1ddSLingrui98 when(f2_fire) {wait_state := wait_idle } 40509c6f1ddSLingrui98 } 40609c6f1ddSLingrui98 } 40709c6f1ddSLingrui98 40809c6f1ddSLingrui98 when(f2_flush) { wait_state := wait_idle } 40909c6f1ddSLingrui98 41009c6f1ddSLingrui98 (0 until 2).map { i => 41109c6f1ddSLingrui98 if(i == 1) toMissQueue(i).valid := (hit_0_miss_1 || miss_0_miss_1) && wait_state === wait_queue_ready 412eee4cb5cSJay else toMissQueue(i).valid := (only_0_miss || miss_0_hit_1 || miss_0_miss_1 || miss_0_except_1) && wait_state === wait_queue_ready 41309c6f1ddSLingrui98 toMissQueue(i).bits.addr := f2_pAddrs(i) 41409c6f1ddSLingrui98 toMissQueue(i).bits.vSetIdx := f2_vSetIdx(i) 41509c6f1ddSLingrui98 toMissQueue(i).bits.waymask := f2_waymask(i) 41609c6f1ddSLingrui98 toMissQueue(i).bits.clientID :=0.U 41709c6f1ddSLingrui98 } 41809c6f1ddSLingrui98 4190be662e4SJay toUncache.valid := (wait_state === wait_send_mmio) && !f2_except_af(0) 4200be662e4SJay //assert( (GTimer() < 5000.U && toUncache.fire()) || !toUncache.fire() ) 4210be662e4SJay toUncache.bits.addr := f2_ftq_req.startAddr 4220be662e4SJay 4230be662e4SJay fromUncache.ready := true.B 4240be662e4SJay 42509c6f1ddSLingrui98 val miss_all_fix = (wait_state === wait_finish) 42609c6f1ddSLingrui98 42709c6f1ddSLingrui98 f2_fetchFinish := ((f2_valid && f2_hit) || miss_all_fix || hit_0_except_1 || except_0) 42809c6f1ddSLingrui98 429f7c29b0aSJinYue XSPerfAccumulate("ifu_bubble_f2_miss", f2_valid && !f2_fetchFinish ) 43009c6f1ddSLingrui98 43109c6f1ddSLingrui98 (touch_ways zip touch_sets).zipWithIndex.map{ case((t_w,t_s), i) => 43209c6f1ddSLingrui98 t_s(0) := f1_vSetIdx(i) 43309c6f1ddSLingrui98 t_w(0).valid := f1_bank_hit(i) 43409c6f1ddSLingrui98 t_w(0).bits := OHToUInt(f1_bank_hit_vec(i)) 43509c6f1ddSLingrui98 43609c6f1ddSLingrui98 t_s(1) := f2_vSetIdx(i) 43709c6f1ddSLingrui98 t_w(1).valid := f2_valid && !f2_bank_hit(i) 43809c6f1ddSLingrui98 t_w(1).bits := OHToUInt(f2_waymask(i)) 43909c6f1ddSLingrui98 } 44009c6f1ddSLingrui98 44109c6f1ddSLingrui98 val sec_miss_reg = RegInit(0.U.asTypeOf(Vec(4, Bool()))) 44209c6f1ddSLingrui98 val reservedRefillData = Reg(Vec(2, UInt(blockBits.W))) 44309c6f1ddSLingrui98 val f2_hit_datas = RegEnable(next = f1_hit_data, enable = f1_fire) 44409c6f1ddSLingrui98 val f2_datas = Wire(Vec(2, UInt(blockBits.W))) 44509c6f1ddSLingrui98 44609c6f1ddSLingrui98 f2_datas.zipWithIndex.map{case(bank,i) => 44709c6f1ddSLingrui98 if(i == 0) bank := Mux(f2_bank_hit(i), f2_hit_datas(i),Mux(sec_miss_reg(2),reservedRefillData(1),Mux(sec_miss_reg(0),reservedRefillData(0), f2_mq_datas(i)))) 44809c6f1ddSLingrui98 else bank := Mux(f2_bank_hit(i), f2_hit_datas(i),Mux(sec_miss_reg(3),reservedRefillData(1),Mux(sec_miss_reg(1),reservedRefillData(0), f2_mq_datas(i)))) 44909c6f1ddSLingrui98 } 45009c6f1ddSLingrui98 45109c6f1ddSLingrui98 val f2_jump_valids = Fill(PredictWidth, !preDecoderOut.cfiOffset.valid) | Fill(PredictWidth, 1.U(1.W)) >> (~preDecoderOut.cfiOffset.bits) 45209c6f1ddSLingrui98 val f2_predecode_valids = VecInit(preDecoderOut.pd.map(instr => instr.valid)).asUInt & f2_jump_valids 45309c6f1ddSLingrui98 45409c6f1ddSLingrui98 def cut(cacheline: UInt, start: UInt) : Vec[UInt] ={ 45509c6f1ddSLingrui98 if(HasCExtension){ 45609c6f1ddSLingrui98 val result = Wire(Vec(PredictWidth + 1, UInt(16.W))) 45709c6f1ddSLingrui98 val dataVec = cacheline.asTypeOf(Vec(blockBytes * 2/ 2, UInt(16.W))) 45809c6f1ddSLingrui98 val startPtr = Cat(0.U(1.W), start(blockOffBits-1, 1)) 45909c6f1ddSLingrui98 (0 until PredictWidth + 1).foreach( i => 46009c6f1ddSLingrui98 result(i) := dataVec(startPtr + i.U) 46109c6f1ddSLingrui98 ) 46209c6f1ddSLingrui98 result 46309c6f1ddSLingrui98 } else { 46409c6f1ddSLingrui98 val result = Wire(Vec(PredictWidth, UInt(32.W)) ) 46509c6f1ddSLingrui98 val dataVec = cacheline.asTypeOf(Vec(blockBytes * 2/ 4, UInt(32.W))) 46609c6f1ddSLingrui98 val startPtr = Cat(0.U(1.W), start(blockOffBits-1, 2)) 46709c6f1ddSLingrui98 (0 until PredictWidth).foreach( i => 46809c6f1ddSLingrui98 result(i) := dataVec(startPtr + i.U) 46909c6f1ddSLingrui98 ) 47009c6f1ddSLingrui98 result 47109c6f1ddSLingrui98 } 47209c6f1ddSLingrui98 } 47309c6f1ddSLingrui98 47409c6f1ddSLingrui98 val f2_cut_data = cut( Cat(f2_datas.map(cacheline => cacheline.asUInt ).reverse).asUInt, f2_ftq_req.startAddr ) 4750be662e4SJay when(f2_mmio){ 4760be662e4SJay f2_cut_data(0) := f2_mmio_data(15, 0) 4770be662e4SJay f2_cut_data(1) := f2_mmio_data(31, 16) 4780be662e4SJay } 47909c6f1ddSLingrui98 48009c6f1ddSLingrui98 // deal with secondary miss in f1 48109c6f1ddSLingrui98 val f2_0_f1_0 = ((f2_valid && !f2_bank_hit(0)) && f1_valid && (get_block_addr(f2_ftq_req.startAddr) === get_block_addr(f1_ftq_req.startAddr))) 48209c6f1ddSLingrui98 val f2_0_f1_1 = ((f2_valid && !f2_bank_hit(0)) && f1_valid && f1_doubleLine && (get_block_addr(f2_ftq_req.startAddr) === get_block_addr(f1_ftq_req.startAddr + blockBytes.U))) 48309c6f1ddSLingrui98 val f2_1_f1_0 = ((f2_valid && !f2_bank_hit(1) && f2_doubleLine) && f1_valid && (get_block_addr(f2_ftq_req.startAddr+ blockBytes.U) === get_block_addr(f1_ftq_req.startAddr) )) 48409c6f1ddSLingrui98 val f2_1_f1_1 = ((f2_valid && !f2_bank_hit(1) && f2_doubleLine) && f1_valid && f1_doubleLine && (get_block_addr(f2_ftq_req.startAddr+ blockBytes.U) === get_block_addr(f1_ftq_req.startAddr + blockBytes.U) )) 48509c6f1ddSLingrui98 48609c6f1ddSLingrui98 val isSameLine = f2_0_f1_0 || f2_0_f1_1 || f2_1_f1_0 || f2_1_f1_1 48709c6f1ddSLingrui98 val sec_miss_sit = VecInit(Seq(f2_0_f1_0, f2_0_f1_1, f2_1_f1_0, f2_1_f1_1)) 48809c6f1ddSLingrui98 val hasSecMiss = RegInit(false.B) 48909c6f1ddSLingrui98 49009c6f1ddSLingrui98 when(f2_flush){ 49109c6f1ddSLingrui98 sec_miss_reg.map(sig => sig := false.B) 49209c6f1ddSLingrui98 hasSecMiss := false.B 49309c6f1ddSLingrui98 }.elsewhen(isSameLine && !f1_flush && f2_fire){ 49409c6f1ddSLingrui98 sec_miss_reg.zipWithIndex.map{case(sig, i) => sig := sec_miss_sit(i)} 49509c6f1ddSLingrui98 hasSecMiss := true.B 49609c6f1ddSLingrui98 }.elsewhen((!isSameLine || f1_flush) && hasSecMiss && f2_fire){ 49709c6f1ddSLingrui98 sec_miss_reg.map(sig => sig := false.B) 49809c6f1ddSLingrui98 hasSecMiss := false.B 49909c6f1ddSLingrui98 } 50009c6f1ddSLingrui98 50109c6f1ddSLingrui98 when((f2_0_f1_0 || f2_0_f1_1) && f2_fire){ 50209c6f1ddSLingrui98 reservedRefillData(0) := f2_mq_datas(0) 50309c6f1ddSLingrui98 } 50409c6f1ddSLingrui98 50509c6f1ddSLingrui98 when((f2_1_f1_0 || f2_1_f1_1) && f2_fire){ 50609c6f1ddSLingrui98 reservedRefillData(1) := f2_mq_datas(1) 50709c6f1ddSLingrui98 } 50809c6f1ddSLingrui98 50909c6f1ddSLingrui98 51009c6f1ddSLingrui98 //--------------------------------------------- 51109c6f1ddSLingrui98 // Fetch Stage 4 : 51209c6f1ddSLingrui98 // * get data from last stage (hit from f1_hit_data/miss from missQueue response) 51309c6f1ddSLingrui98 // * if at least one needed cacheline miss, wait for miss queue response (a wait_state machine) THIS IS TOO UGLY!!! 51409c6f1ddSLingrui98 // * cut cacheline(s) and send to PreDecode 51509c6f1ddSLingrui98 // * check if prediction is right (branch target and type, jump direction and type , jal target ) 51609c6f1ddSLingrui98 //--------------------------------------------- 51709c6f1ddSLingrui98 val f3_valid = RegInit(false.B) 51809c6f1ddSLingrui98 val f3_ftq_req = RegEnable(next = f2_ftq_req, enable=f2_fire) 51909c6f1ddSLingrui98 val f3_situation = RegEnable(next = f2_situation, enable=f2_fire) 52009c6f1ddSLingrui98 val f3_doubleLine = RegEnable(next = f2_doubleLine, enable=f2_fire) 52109c6f1ddSLingrui98 val f3_fire = io.toIbuffer.fire() 52209c6f1ddSLingrui98 52309c6f1ddSLingrui98 when(f3_flush) {f3_valid := false.B} 52409c6f1ddSLingrui98 .elsewhen(f2_fire && !f2_flush) {f3_valid := true.B } 52509c6f1ddSLingrui98 .elsewhen(io.toIbuffer.fire()) {f3_valid := false.B} 52609c6f1ddSLingrui98 52709c6f1ddSLingrui98 f3_ready := io.toIbuffer.ready || !f2_valid 52809c6f1ddSLingrui98 52909c6f1ddSLingrui98 val f3_cut_data = RegEnable(next = f2_cut_data, enable=f2_fire) 53009c6f1ddSLingrui98 val f3_except_pf = RegEnable(next = f2_except_pf, enable = f2_fire) 53109c6f1ddSLingrui98 val f3_except_af = RegEnable(next = f2_except_af, enable = f2_fire) 53209c6f1ddSLingrui98 val f3_hit = RegEnable(next = f2_hit , enable = f2_fire) 5330be662e4SJay val f3_mmio = RegEnable(next = f2_mmio , enable = f2_fire) 53409c6f1ddSLingrui98 53509c6f1ddSLingrui98 val f3_lastHalf = RegInit(0.U.asTypeOf(new LastHalfInfo)) 53609c6f1ddSLingrui98 val f3_lastHalfMatch = f3_lastHalf.matchThisBlock(f3_ftq_req.startAddr) 53709c6f1ddSLingrui98 val f3_except = VecInit((0 until 2).map{i => f3_except_pf(i) || f3_except_af(i)}) 53809c6f1ddSLingrui98 val f3_has_except = f3_valid && (f3_except_af.reduce(_||_) || f3_except_pf.reduce(_||_)) 53909c6f1ddSLingrui98 540f7c29b0aSJinYue //performance counter 541f7c29b0aSJinYue val f3_only_0_hit = RegEnable(next = only_0_hit, enable = f2_fire) 542f7c29b0aSJinYue val f3_only_0_miss = RegEnable(next = only_0_miss, enable = f2_fire) 543f7c29b0aSJinYue val f3_hit_0_hit_1 = RegEnable(next = hit_0_hit_1, enable = f2_fire) 544f7c29b0aSJinYue val f3_hit_0_miss_1 = RegEnable(next = hit_0_miss_1, enable = f2_fire) 545f7c29b0aSJinYue val f3_miss_0_hit_1 = RegEnable(next = miss_0_hit_1, enable = f2_fire) 546f7c29b0aSJinYue val f3_miss_0_miss_1 = RegEnable(next = miss_0_miss_1, enable = f2_fire) 547f7c29b0aSJinYue 548f7c29b0aSJinYue val f3_bank_hit = RegEnable(next = f2_bank_hit, enable = f2_fire) 549f7c29b0aSJinYue val f3_req_0 = io.toIbuffer.fire() 550f7c29b0aSJinYue val f3_req_1 = io.toIbuffer.fire() && f3_doubleLine 551f7c29b0aSJinYue val f3_hit_0 = io.toIbuffer.fire() & f3_bank_hit(0) 552f7c29b0aSJinYue val f3_hit_1 = io.toIbuffer.fire() && f3_doubleLine & f3_bank_hit(1) 553f7c29b0aSJinYue 55409c6f1ddSLingrui98 preDecoderIn.instValid := f3_valid && !f3_has_except 55509c6f1ddSLingrui98 preDecoderIn.data := f3_cut_data 55609c6f1ddSLingrui98 preDecoderIn.startAddr := f3_ftq_req.startAddr 55709c6f1ddSLingrui98 preDecoderIn.fallThruAddr := f3_ftq_req.fallThruAddr 55809c6f1ddSLingrui98 preDecoderIn.fallThruError := f3_ftq_req.fallThruError 55909c6f1ddSLingrui98 preDecoderIn.isDoubleLine := f3_doubleLine 56009c6f1ddSLingrui98 preDecoderIn.ftqOffset := f3_ftq_req.ftqOffset 56109c6f1ddSLingrui98 preDecoderIn.target := f3_ftq_req.target 56209c6f1ddSLingrui98 preDecoderIn.oversize := f3_ftq_req.oversize 56309c6f1ddSLingrui98 preDecoderIn.lastHalfMatch := f3_lastHalfMatch 56409c6f1ddSLingrui98 preDecoderIn.pageFault := f3_except_pf 56509c6f1ddSLingrui98 preDecoderIn.accessFault := f3_except_af 5660be662e4SJay preDecoderIn.mmio := f3_mmio 56709c6f1ddSLingrui98 56809c6f1ddSLingrui98 56909c6f1ddSLingrui98 // TODO: What if next packet does not match? 57009c6f1ddSLingrui98 when (f3_flush) { 57109c6f1ddSLingrui98 f3_lastHalf.valid := false.B 57209c6f1ddSLingrui98 }.elsewhen (io.toIbuffer.fire()) { 57309c6f1ddSLingrui98 f3_lastHalf.valid := preDecoderOut.hasLastHalf 57409c6f1ddSLingrui98 f3_lastHalf.middlePC := preDecoderOut.realEndPC 57509c6f1ddSLingrui98 } 57609c6f1ddSLingrui98 57709c6f1ddSLingrui98 val f3_predecode_range = VecInit(preDecoderOut.pd.map(inst => inst.valid)).asUInt 5780be662e4SJay val f3_mmio_range = VecInit((0 until PredictWidth).map(i => if(i ==0) true.B else false.B)) 57909c6f1ddSLingrui98 58009c6f1ddSLingrui98 io.toIbuffer.valid := f3_valid 58109c6f1ddSLingrui98 io.toIbuffer.bits.instrs := preDecoderOut.instrs 5820be662e4SJay io.toIbuffer.bits.valid := Mux(f3_mmio, f3_mmio_range.asUInt, f3_predecode_range & preDecoderOut.instrRange.asUInt) 58309c6f1ddSLingrui98 io.toIbuffer.bits.pd := preDecoderOut.pd 58409c6f1ddSLingrui98 io.toIbuffer.bits.ftqPtr := f3_ftq_req.ftqIdx 58509c6f1ddSLingrui98 io.toIbuffer.bits.pc := preDecoderOut.pc 5860be662e4SJay io.toIbuffer.bits.ftqOffset.zipWithIndex.map{case(a, i) => a.bits := i.U; a.valid := preDecoderOut.takens(i) && !f3_mmio} 58709c6f1ddSLingrui98 io.toIbuffer.bits.foldpc := preDecoderOut.pc.map(i => XORFold(i(VAddrBits-1,1), MemPredPCWidth)) 58809c6f1ddSLingrui98 io.toIbuffer.bits.ipf := preDecoderOut.pageFault 58909c6f1ddSLingrui98 io.toIbuffer.bits.acf := preDecoderOut.accessFault 59009c6f1ddSLingrui98 io.toIbuffer.bits.crossPageIPFFix := preDecoderOut.crossPageIPF 59109c6f1ddSLingrui98 59209c6f1ddSLingrui98 //Write back to Ftq 59309c6f1ddSLingrui98 val finishFetchMaskReg = RegNext(f3_valid && !(f2_fire && !f2_flush)) 59409c6f1ddSLingrui98 5950be662e4SJay val f3_mmio_missOffset = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 5960be662e4SJay f3_mmio_missOffset.valid := f3_mmio 5970be662e4SJay f3_mmio_missOffset.bits := 0.U 5980be662e4SJay 59909c6f1ddSLingrui98 toFtq.pdWb.valid := !finishFetchMaskReg && f3_valid 60009c6f1ddSLingrui98 toFtq.pdWb.bits.pc := preDecoderOut.pc 60109c6f1ddSLingrui98 toFtq.pdWb.bits.pd := preDecoderOut.pd 6020be662e4SJay toFtq.pdWb.bits.pd.zipWithIndex.map{case(instr,i) => instr.valid := Mux(f3_mmio, f3_mmio_range(i), f3_predecode_range(i))} 60309c6f1ddSLingrui98 toFtq.pdWb.bits.ftqIdx := f3_ftq_req.ftqIdx 60409c6f1ddSLingrui98 toFtq.pdWb.bits.ftqOffset := f3_ftq_req.ftqOffset.bits 6050be662e4SJay toFtq.pdWb.bits.misOffset := Mux(f3_mmio, f3_mmio_missOffset, preDecoderOut.misOffset) 60609c6f1ddSLingrui98 toFtq.pdWb.bits.cfiOffset := preDecoderOut.cfiOffset 6070be662e4SJay toFtq.pdWb.bits.target := Mux(f3_mmio,Mux(toFtq.pdWb.bits.pd(0).isRVC, toFtq.pdWb.bits.pc(0) + 2.U , toFtq.pdWb.bits.pc(0)+4.U) ,preDecoderOut.target) 60809c6f1ddSLingrui98 toFtq.pdWb.bits.jalTarget := preDecoderOut.jalTarget 6090be662e4SJay toFtq.pdWb.bits.instrRange := Mux(f3_mmio, f3_mmio_range, preDecoderOut.instrRange) 61009c6f1ddSLingrui98 6110be662e4SJay val predecodeFlush = ((preDecoderOut.misOffset.valid || f3_mmio) && f3_valid) 61209c6f1ddSLingrui98 val predecodeFlushReg = RegNext(predecodeFlush && !(f2_fire && !f2_flush)) 61309c6f1ddSLingrui98 614cd365d4cSrvcoresjw val perfinfo = IO(new Bundle(){ 615cd365d4cSrvcoresjw val perfEvents = Output(new PerfEventsBundle(15)) 616cd365d4cSrvcoresjw }) 617cd365d4cSrvcoresjw 618cd365d4cSrvcoresjw val perfEvents = Seq( 619cd365d4cSrvcoresjw ("frontendFlush ", f3_redirect ), 620cd365d4cSrvcoresjw ("ifu_req ", io.toIbuffer.fire() ), 621cd365d4cSrvcoresjw ("ifu_miss ", io.toIbuffer.fire() && !f3_hit ), 622cd365d4cSrvcoresjw ("ifu_req_cacheline_0 ", f3_req_0 ), 623cd365d4cSrvcoresjw ("ifu_req_cacheline_1 ", f3_req_1 ), 624cd365d4cSrvcoresjw ("ifu_req_cacheline_0_hit ", f3_hit_1 ), 625cd365d4cSrvcoresjw ("ifu_req_cacheline_1_hit ", f3_hit_1 ), 626cd365d4cSrvcoresjw ("only_0_hit ", f3_only_0_hit && io.toIbuffer.fire() ), 627cd365d4cSrvcoresjw ("only_0_miss ", f3_only_0_miss && io.toIbuffer.fire() ), 628cd365d4cSrvcoresjw ("hit_0_hit_1 ", f3_hit_0_hit_1 && io.toIbuffer.fire() ), 629cd365d4cSrvcoresjw ("hit_0_miss_1 ", f3_hit_0_miss_1 && io.toIbuffer.fire() ), 630cd365d4cSrvcoresjw ("miss_0_hit_1 ", f3_miss_0_hit_1 && io.toIbuffer.fire() ), 631cd365d4cSrvcoresjw ("miss_0_miss_1 ", f3_miss_0_miss_1 && io.toIbuffer.fire() ), 632cd365d4cSrvcoresjw ("cross_line_block ", io.toIbuffer.fire() && f3_situation(0) ), 633cd365d4cSrvcoresjw ("fall_through_is_cacheline_end", io.toIbuffer.fire() && f3_situation(1) ), 634cd365d4cSrvcoresjw ) 635cd365d4cSrvcoresjw 636cd365d4cSrvcoresjw for (((perf_out,(perf_name,perf)),i) <- perfinfo.perfEvents.perf_events.zip(perfEvents).zipWithIndex) { 637cd365d4cSrvcoresjw perf_out.incr_step := RegNext(perf) 638cd365d4cSrvcoresjw } 639f7c29b0aSJinYue 64009c6f1ddSLingrui98 f3_redirect := !predecodeFlushReg && predecodeFlush 64109c6f1ddSLingrui98 642f7c29b0aSJinYue XSPerfAccumulate("ifu_req", io.toIbuffer.fire() ) 643f7c29b0aSJinYue XSPerfAccumulate("ifu_miss", io.toIbuffer.fire() && !f3_hit ) 644f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_0", f3_req_0 ) 645f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_1", f3_req_1 ) 646f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_0_hit", f3_hit_0 ) 647f7c29b0aSJinYue XSPerfAccumulate("ifu_req_cacheline_1_hit", f3_hit_1 ) 64809c6f1ddSLingrui98 XSPerfAccumulate("frontendFlush", f3_redirect ) 649f7c29b0aSJinYue XSPerfAccumulate("only_0_hit", f3_only_0_hit && io.toIbuffer.fire() ) 650f7c29b0aSJinYue XSPerfAccumulate("only_0_miss", f3_only_0_miss && io.toIbuffer.fire() ) 651f7c29b0aSJinYue XSPerfAccumulate("hit_0_hit_1", f3_hit_0_hit_1 && io.toIbuffer.fire() ) 652f7c29b0aSJinYue XSPerfAccumulate("hit_0_miss_1", f3_hit_0_miss_1 && io.toIbuffer.fire() ) 653f7c29b0aSJinYue XSPerfAccumulate("miss_0_hit_1", f3_miss_0_hit_1 && io.toIbuffer.fire() ) 654f7c29b0aSJinYue XSPerfAccumulate("miss_0_miss_1", f3_miss_0_miss_1 && io.toIbuffer.fire() ) 655f7c29b0aSJinYue XSPerfAccumulate("cross_line_block", io.toIbuffer.fire() && f3_situation(0) ) 656f7c29b0aSJinYue XSPerfAccumulate("fall_through_is_cacheline_end", io.toIbuffer.fire() && f3_situation(1) ) 65709c6f1ddSLingrui98} 658