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 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import xiangshan.cache._ 24import xiangshan.cache.mmu._ 25import chisel3.experimental.verification 26import utils._ 27import xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle} 28 29trait HasInstrMMIOConst extends HasXSParameter with HasIFUConst{ 30 def mmioBusWidth = 64 31 def mmioBusBytes = mmioBusWidth / 8 32 def maxInstrLen = 32 33} 34 35trait HasIFUConst extends HasXSParameter { 36 def align(pc: UInt, bytes: Int): UInt = Cat(pc(VAddrBits-1, log2Ceil(bytes)), 0.U(log2Ceil(bytes).W)) 37 // def groupAligned(pc: UInt) = align(pc, groupBytes) 38 // def packetAligned(pc: UInt) = align(pc, packetBytes) 39} 40 41class IfuToFtqIO(implicit p:Parameters) extends XSBundle { 42 val pdWb = Valid(new PredecodeWritebackBundle) 43} 44 45class FtqInterface(implicit p: Parameters) extends XSBundle { 46 val fromFtq = Flipped(new FtqToIfuIO) 47 val toFtq = new IfuToFtqIO 48} 49 50class UncacheInterface(implicit p: Parameters) extends XSBundle { 51 val fromUncache = Flipped(DecoupledIO(new InsUncacheResp)) 52 val toUncache = DecoupledIO( new InsUncacheReq ) 53} 54 55class ICacheInterface(implicit p: Parameters) extends XSBundle { 56 val toIMeta = Decoupled(new ICacheReadBundle) 57 val toIData = Decoupled(new ICacheReadBundle) 58 val toMissQueue = Vec(2,Decoupled(new ICacheMissReq)) 59 val fromIMeta = Input(new ICacheMetaRespBundle) 60 val fromIData = Input(new ICacheDataRespBundle) 61 val fromMissQueue = Vec(2,Flipped(Decoupled(new ICacheMissResp))) 62} 63 64class NewIFUIO(implicit p: Parameters) extends XSBundle { 65 val ftqInter = new FtqInterface 66 val icacheInter = new ICacheInterface 67 val toIbuffer = Decoupled(new FetchToIBuffer) 68 val iTLBInter = Vec(2, new BlockTlbRequestIO) 69 val uncacheInter = new UncacheInterface 70 val pmp = Vec(2, new Bundle { 71 val req = Valid(new PMPReqBundle()) 72 val resp = Flipped(new PMPRespBundle()) 73 }) 74 val frontendTrigger = Flipped(new FrontendTdataDistributeIO) 75 val csrTriggerEnable = Input(Vec(4, Bool())) 76 val rob_commits = Flipped(Vec(CommitWidth, Valid(new RobCommitInfo))) 77} 78 79// record the situation in which fallThruAddr falls into 80// the middle of an RVI inst 81class LastHalfInfo(implicit p: Parameters) extends XSBundle { 82 val valid = Bool() 83 val middlePC = UInt(VAddrBits.W) 84 def matchThisBlock(startAddr: UInt) = valid && middlePC === startAddr 85} 86 87class IfuToPreDecode(implicit p: Parameters) extends XSBundle { 88 val data = if(HasCExtension) Vec(PredictWidth + 1, UInt(16.W)) else Vec(PredictWidth, UInt(32.W)) 89 val startAddr = UInt(VAddrBits.W) 90 val fallThruAddr = UInt(VAddrBits.W) 91 val fallThruError = Bool() 92 val isDoubleLine = Bool() 93 val ftqOffset = Valid(UInt(log2Ceil(PredictWidth).W)) 94 val target = UInt(VAddrBits.W) 95 val pageFault = Vec(2, Bool()) 96 val accessFault = Vec(2, Bool()) 97 val instValid = Bool() 98 val lastHalfMatch = Bool() 99 val oversize = Bool() 100 val mmio = Bool() 101 val frontendTrigger = new FrontendTdataDistributeIO 102 val csrTriggerEnable = Vec(4, Bool()) 103} 104 105class NewIFU(implicit p: Parameters) extends XSModule with HasICacheParameters 106with HasCircularQueuePtrHelper 107{ 108 println(s"icache ways: ${nWays} sets:${nSets}") 109 val io = IO(new NewIFUIO) 110 val (toFtq, fromFtq) = (io.ftqInter.toFtq, io.ftqInter.fromFtq) 111 val (toMeta, toData, meta_resp, data_resp) = (io.icacheInter.toIMeta, io.icacheInter.toIData, io.icacheInter.fromIMeta, io.icacheInter.fromIData) 112 val (toMissQueue, fromMissQueue) = (io.icacheInter.toMissQueue, io.icacheInter.fromMissQueue) 113 val (toUncache, fromUncache) = (io.uncacheInter.toUncache , io.uncacheInter.fromUncache) 114 val (toITLB, fromITLB) = (VecInit(io.iTLBInter.map(_.req)), VecInit(io.iTLBInter.map(_.resp))) 115 val fromPMP = io.pmp.map(_.resp) 116 117 def isCrossLineReq(start: UInt, end: UInt): Bool = start(blockOffBits) ^ end(blockOffBits) 118 119 def isLastInCacheline(fallThruAddr: UInt): Bool = fallThruAddr(blockOffBits - 1, 1) === 0.U 120 121 def ResultHoldBypass[T<:Data](data: T, valid: Bool): T = { 122 Mux(valid, data, RegEnable(data, valid)) 123 } 124 125 //--------------------------------------------- 126 // Fetch Stage 1 : 127 // * Send req to ICache Meta/Data 128 // * Check whether need 2 line fetch 129 //--------------------------------------------- 130 131 val f0_valid = fromFtq.req.valid 132 val f0_ftq_req = fromFtq.req.bits 133 val f0_situation = VecInit(Seq(isCrossLineReq(f0_ftq_req.startAddr, f0_ftq_req.fallThruAddr), isLastInCacheline(f0_ftq_req.fallThruAddr))) 134 val f0_doubleLine = f0_situation(0) || f0_situation(1) 135 val f0_vSetIdx = VecInit(get_idx((f0_ftq_req.startAddr)), get_idx(f0_ftq_req.fallThruAddr)) 136 val f0_fire = fromFtq.req.fire() 137 138 val f0_flush, f1_flush, f2_flush, f3_flush = WireInit(false.B) 139 val from_bpu_f0_flush, from_bpu_f1_flush, from_bpu_f2_flush, from_bpu_f3_flush = WireInit(false.B) 140 141 from_bpu_f0_flush := fromFtq.flushFromBpu.shouldFlushByStage2(f0_ftq_req.ftqIdx) || 142 fromFtq.flushFromBpu.shouldFlushByStage3(f0_ftq_req.ftqIdx) 143 144 val f3_redirect = WireInit(false.B) 145 f3_flush := fromFtq.redirect.valid 146 f2_flush := f3_flush || f3_redirect 147 f1_flush := f2_flush || from_bpu_f1_flush 148 f0_flush := f1_flush || from_bpu_f0_flush 149 150 val f1_ready, f2_ready, f3_ready = WireInit(false.B) 151 152 //fetch: send addr to Meta/TLB and Data simultaneously 153 val fetch_req = List(toMeta, toData) 154 for(i <- 0 until 2) { 155 fetch_req(i).valid := f0_fire 156 fetch_req(i).bits.isDoubleLine := f0_doubleLine 157 fetch_req(i).bits.vSetIdx := f0_vSetIdx 158 } 159 160 fromFtq.req.ready := fetch_req(0).ready && fetch_req(1).ready && f1_ready && GTimer() > 500.U 161 162 XSPerfAccumulate("ifu_bubble_ftq_not_valid", !f0_valid ) 163 XSPerfAccumulate("ifu_bubble_pipe_stall", f0_valid && fetch_req(0).ready && fetch_req(1).ready && !f1_ready ) 164 XSPerfAccumulate("ifu_bubble_sram_0_busy", f0_valid && !fetch_req(0).ready ) 165 XSPerfAccumulate("ifu_bubble_sram_1_busy", f0_valid && !fetch_req(1).ready ) 166 167 //--------------------------------------------- 168 // Fetch Stage 2 : 169 // * Send req to ITLB and TLB Response (Get Paddr) 170 // * ICache Response (Get Meta and Data) 171 // * Hit Check (Generate hit signal and hit vector) 172 // * Get victim way 173 //--------------------------------------------- 174 175 //TODO: handle fetch exceptions 176 177 val tlbRespAllValid = WireInit(false.B) 178 179 val f1_valid = RegInit(false.B) 180 val f1_ftq_req = RegEnable(next = f0_ftq_req, enable=f0_fire) 181 val f1_situation = RegEnable(next = f0_situation, enable=f0_fire) 182 val f1_doubleLine = RegEnable(next = f0_doubleLine, enable=f0_fire) 183 val f1_vSetIdx = RegEnable(next = f0_vSetIdx, enable=f0_fire) 184 val f1_fire = f1_valid && tlbRespAllValid && f2_ready 185 186 f1_ready := f2_ready && tlbRespAllValid || !f1_valid 187 188 from_bpu_f1_flush := fromFtq.flushFromBpu.shouldFlushByStage3(f1_ftq_req.ftqIdx) 189 190 val preDecoder = Module(new PreDecode) 191 val (preDecoderIn, preDecoderOut) = (preDecoder.io.in, preDecoder.io.out) 192 193 //flush generate and to Ftq 194 val predecodeOutValid = WireInit(false.B) 195 196 when(f1_flush) {f1_valid := false.B} 197 .elsewhen(f0_fire && !f0_flush) {f1_valid := true.B} 198 .elsewhen(f1_fire) {f1_valid := false.B} 199 200 toITLB(0).valid := f1_valid 201 toITLB(0).bits.size := 3.U // TODO: fix the size 202 toITLB(0).bits.vaddr := f1_ftq_req.startAddr 203 toITLB(0).bits.debug.pc := f1_ftq_req.startAddr 204 205 toITLB(1).valid := f1_valid && f1_doubleLine 206 toITLB(1).bits.size := 3.U // TODO: fix the size 207 toITLB(1).bits.vaddr := f1_ftq_req.fallThruAddr 208 toITLB(1).bits.debug.pc := f1_ftq_req.fallThruAddr 209 210 toITLB.map{port => 211 port.bits.cmd := TlbCmd.exec 212 port.bits.robIdx := DontCare 213 port.bits.debug.isFirstIssue := DontCare 214 } 215 216 fromITLB.map(_.ready := true.B) 217 218 val (tlbRespValid, tlbRespPAddr) = (fromITLB.map(_.valid), VecInit(fromITLB.map(_.bits.paddr))) 219 val (tlbRespMiss) = (fromITLB.map(port => port.bits.miss && port.valid)) 220 val (tlbExcpPF, tlbExcpAF) = (fromITLB.map(port => port.bits.excp.pf.instr && port.valid), 221 fromITLB.map(port => (port.bits.excp.af.instr) && port.valid)) //TODO: Temp treat mmio req as access fault 222 223 tlbRespAllValid := tlbRespValid(0) && (tlbRespValid(1) || !f1_doubleLine) 224 225 val f1_pAddrs = tlbRespPAddr 226 val f1_pTags = VecInit(f1_pAddrs.map(get_phy_tag(_))) 227 228 val f1_tags = ResultHoldBypass(data = meta_resp.tags, valid = RegNext(toMeta.fire())) 229 val f1_cacheline_valid = ResultHoldBypass(data = meta_resp.valid, valid = RegNext(toMeta.fire())) 230 val f1_datas = ResultHoldBypass(data = data_resp.datas, valid = RegNext(toData.fire())) 231 232 val bank0_hit_vec = VecInit(f1_tags(0).zipWithIndex.map{ case(way_tag,i) => f1_cacheline_valid(0)(i) && way_tag === f1_pTags(0) }) 233 val bank1_hit_vec = VecInit(f1_tags(1).zipWithIndex.map{ case(way_tag,i) => f1_cacheline_valid(1)(i) && way_tag === f1_pTags(1) }) 234 val (bank0_hit,bank1_hit) = (ParallelOR(bank0_hit_vec) && !tlbExcpPF(0) && !tlbExcpAF(0), ParallelOR(bank1_hit_vec) && !tlbExcpPF(1) && !tlbExcpAF(1)) 235 val f1_hit = (bank0_hit && bank1_hit && f1_valid && f1_doubleLine) || (f1_valid && !f1_doubleLine && bank0_hit) 236 val f1_bank_hit_vec = VecInit(Seq(bank0_hit_vec, bank1_hit_vec)) 237 val f1_bank_hit = VecInit(Seq(bank0_hit, bank1_hit)) 238 239 240 val replacers = Seq.fill(2)(ReplacementPolicy.fromString(Some("random"),nWays,nSets/2)) 241 val f1_victim_masks = VecInit(replacers.zipWithIndex.map{case (replacer, i) => UIntToOH(replacer.way(f1_vSetIdx(i)))}) 242 243 val touch_sets = Seq.fill(2)(Wire(Vec(2, UInt(log2Ceil(nSets/2).W)))) 244 val touch_ways = Seq.fill(2)(Wire(Vec(2, Valid(UInt(log2Ceil(nWays).W)))) ) 245 246 ((replacers zip touch_sets) zip touch_ways).map{case ((r, s),w) => r.access(s,w)} 247 248 val f1_hit_data = VecInit(f1_datas.zipWithIndex.map { case(bank, i) => 249 val bank_hit_data = Mux1H(f1_bank_hit_vec(i).asUInt, bank) 250 bank_hit_data 251 }) 252 253 (0 until nWays).map{ w => 254 XSPerfAccumulate("line_0_hit_way_" + Integer.toString(w, 10), f1_fire && f1_bank_hit(0) && OHToUInt(f1_bank_hit_vec(0)) === w.U) 255 } 256 257 (0 until nWays).map{ w => 258 XSPerfAccumulate("line_0_victim_way_" + Integer.toString(w, 10), f1_fire && !f1_bank_hit(0) && OHToUInt(f1_victim_masks(0)) === w.U) 259 } 260 261 (0 until nWays).map{ w => 262 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) 263 } 264 265 (0 until nWays).map{ w => 266 XSPerfAccumulate("line_1_victim_way_" + Integer.toString(w, 10), f1_fire && f1_doubleLine && !f1_bank_hit(1) && OHToUInt(f1_victim_masks(1)) === w.U) 267 } 268 269 XSPerfAccumulate("ifu_bubble_f1_tlb_miss", f1_valid && !tlbRespAllValid ) 270 271 //--------------------------------------------- 272 // Fetch Stage 3 : 273 // * get data from last stage (hit from f1_hit_data/miss from missQueue response) 274 // * if at least one needed cacheline miss, wait for miss queue response (a wait_state machine) THIS IS TOO UGLY!!! 275 // * cut cacheline(s) and send to PreDecode 276 // * check if prediction is right (branch target and type, jump direction and type , jal target ) 277 //--------------------------------------------- 278 val f2_fetchFinish = Wire(Bool()) 279 280 val f2_valid = RegInit(false.B) 281 val f2_ftq_req = RegEnable(next = f1_ftq_req, enable = f1_fire) 282 val f2_situation = RegEnable(next = f1_situation, enable=f1_fire) 283 val f2_doubleLine = RegEnable(next = f1_doubleLine, enable=f1_fire) 284 val f2_fire = f2_valid && f2_fetchFinish && f3_ready 285 286 when(f2_flush) {f2_valid := false.B} 287 .elsewhen(f1_fire && !f1_flush) {f2_valid := true.B } 288 .elsewhen(f2_fire) {f2_valid := false.B} 289 290 val pmpExcpAF = fromPMP.map(port => port.instr) 291 val mmio = fromPMP.map(port => port.mmio) // TODO: handle it 292 293 294 val f2_pAddrs = RegEnable(next = f1_pAddrs, enable = f1_fire) 295 val f2_hit = RegEnable(next = f1_hit , enable = f1_fire) 296 val f2_bank_hit = RegEnable(next = f1_bank_hit, enable = f1_fire) 297 val f2_miss = f2_valid && !f2_hit 298 val (f2_vSetIdx, f2_pTags) = (RegEnable(next = f1_vSetIdx, enable = f1_fire), RegEnable(next = f1_pTags, enable = f1_fire)) 299 val f2_waymask = RegEnable(next = f1_victim_masks, enable = f1_fire) 300 //exception information 301 val f2_except_pf = RegEnable(next = VecInit(tlbExcpPF), enable = f1_fire) 302 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)) 303 val f2_except = VecInit((0 until 2).map{i => f2_except_pf(i) || f2_except_af(i)}) 304 val f2_has_except = f2_valid && (f2_except_af.reduce(_||_) || f2_except_pf.reduce(_||_)) 305 val f2_mmio = io.pmp(0).resp.mmio && !f2_except_af(0) && !f2_except_pf(0) && f2_valid 306 307 f2_ready := (f3_ready && f2_fetchFinish) || !f2_valid 308 309 310 io.pmp.zipWithIndex.map { case (p, i) => 311 p.req.valid := f2_fire 312 p.req.bits.addr := f2_pAddrs(i) 313 p.req.bits.size := 3.U // TODO 314 p.req.bits.cmd := TlbCmd.exec 315 } 316 317 //instruction 318 val wait_idle :: wait_queue_ready :: wait_send_req :: wait_two_resp :: wait_0_resp :: wait_1_resp :: wait_one_resp ::wait_finish ::Nil = Enum(8) 319 val wait_state = RegInit(wait_idle) 320 321 fromMissQueue.map{port => port.ready := true.B} 322 323 val (miss0_resp, miss1_resp) = (fromMissQueue(0).fire(), fromMissQueue(1).fire()) 324 val (bank0_fix, bank1_fix) = (miss0_resp && !f2_bank_hit(0), miss1_resp && f2_doubleLine && !f2_bank_hit(1)) 325 326 val only_0_miss = f2_valid && !f2_hit && !f2_doubleLine && !f2_has_except && !f2_mmio 327 val only_0_hit = f2_valid && f2_hit && !f2_doubleLine && !f2_mmio 328 val hit_0_hit_1 = f2_valid && f2_hit && f2_doubleLine && !f2_mmio 329 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), 330 (f2_valid && !f2_bank_hit(0) && f2_bank_hit(1) && f2_doubleLine && !f2_has_except && !f2_mmio), 331 (f2_valid && !f2_bank_hit(0) && !f2_bank_hit(1) && f2_doubleLine && !f2_has_except && !f2_mmio), 332 ) 333 334 val hit_0_except_1 = f2_valid && f2_doubleLine && !f2_except(0) && f2_except(1) && f2_bank_hit(0) 335 val miss_0_except_1 = f2_valid && f2_doubleLine && !f2_except(0) && f2_except(1) && !f2_bank_hit(0) 336 //val fetch0_except_1 = hit_0_except_1 || miss_0_except_1 337 val except_0 = f2_valid && f2_except(0) 338 339 val f2_mq_datas = Reg(Vec(2, UInt(blockBits.W))) 340 341 when(fromMissQueue(0).fire) {f2_mq_datas(0) := fromMissQueue(0).bits.data} 342 when(fromMissQueue(1).fire) {f2_mq_datas(1) := fromMissQueue(1).bits.data} 343 344 switch(wait_state){ 345 is(wait_idle){ 346 when(miss_0_except_1){ 347 wait_state := Mux(toMissQueue(0).ready, wait_queue_ready ,wait_idle ) 348 }.elsewhen( only_0_miss || miss_0_hit_1){ 349 wait_state := Mux(toMissQueue(0).ready, wait_queue_ready ,wait_idle ) 350 }.elsewhen(hit_0_miss_1){ 351 wait_state := Mux(toMissQueue(1).ready, wait_queue_ready ,wait_idle ) 352 }.elsewhen( miss_0_miss_1 ){ 353 wait_state := Mux(toMissQueue(0).ready && toMissQueue(1).ready, wait_queue_ready ,wait_idle) 354 } 355 } 356 357 //TODO: naive logic for wait icache response 358 is(wait_queue_ready){ 359 wait_state := wait_send_req 360 } 361 362 is(wait_send_req) { 363 when(miss_0_except_1 || only_0_miss || hit_0_miss_1 || miss_0_hit_1){ 364 wait_state := wait_one_resp 365 }.elsewhen( miss_0_miss_1 ){ 366 wait_state := wait_two_resp 367 } 368 } 369 370 is(wait_one_resp) { 371 when( (miss_0_except_1 ||only_0_miss || miss_0_hit_1) && fromMissQueue(0).fire()){ 372 wait_state := wait_finish 373 }.elsewhen( hit_0_miss_1 && fromMissQueue(1).fire()){ 374 wait_state := wait_finish 375 } 376 } 377 378 is(wait_two_resp) { 379 when(fromMissQueue(0).fire() && fromMissQueue(1).fire()){ 380 wait_state := wait_finish 381 }.elsewhen( !fromMissQueue(0).fire() && fromMissQueue(1).fire() ){ 382 wait_state := wait_0_resp 383 }.elsewhen(fromMissQueue(0).fire() && !fromMissQueue(1).fire()){ 384 wait_state := wait_1_resp 385 } 386 } 387 388 is(wait_0_resp) { 389 when(fromMissQueue(0).fire()){ 390 wait_state := wait_finish 391 } 392 } 393 394 is(wait_1_resp) { 395 when(fromMissQueue(1).fire()){ 396 wait_state := wait_finish 397 } 398 } 399 400 is(wait_finish) { 401 when(f2_fire) {wait_state := wait_idle } 402 } 403 } 404 405 when(f2_flush) { wait_state := wait_idle } 406 407 (0 until 2).map { i => 408 if(i == 1) toMissQueue(i).valid := (hit_0_miss_1 || miss_0_miss_1) && wait_state === wait_queue_ready 409 else toMissQueue(i).valid := (only_0_miss || miss_0_hit_1 || miss_0_miss_1 || miss_0_except_1) && wait_state === wait_queue_ready 410 toMissQueue(i).bits.addr := f2_pAddrs(i) 411 toMissQueue(i).bits.vSetIdx := f2_vSetIdx(i) 412 toMissQueue(i).bits.waymask := f2_waymask(i) 413 toMissQueue(i).bits.clientID :=0.U 414 } 415 416 417 val miss_all_fix = (wait_state === wait_finish) 418 419 f2_fetchFinish := ((f2_valid && f2_hit) || (f2_valid && f2_mmio) || miss_all_fix || hit_0_except_1 || except_0) 420 421 XSPerfAccumulate("ifu_bubble_f2_miss", f2_valid && !f2_fetchFinish ) 422 423 (touch_ways zip touch_sets).zipWithIndex.map{ case((t_w,t_s), i) => 424 t_s(0) := f1_vSetIdx(i) 425 t_w(0).valid := f1_bank_hit(i) 426 t_w(0).bits := OHToUInt(f1_bank_hit_vec(i)) 427 428 t_s(1) := f2_vSetIdx(i) 429 t_w(1).valid := f2_valid && !f2_bank_hit(i) 430 t_w(1).bits := OHToUInt(f2_waymask(i)) 431 } 432 433 val sec_miss_reg = RegInit(0.U.asTypeOf(Vec(4, Bool()))) 434 val reservedRefillData = Reg(Vec(2, UInt(blockBits.W))) 435 val f2_hit_datas = RegEnable(next = f1_hit_data, enable = f1_fire) 436 val f2_datas = Wire(Vec(2, UInt(blockBits.W))) 437 438 f2_datas.zipWithIndex.map{case(bank,i) => 439 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)))) 440 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)))) 441 } 442 443 val f2_jump_valids = Fill(PredictWidth, !preDecoderOut.cfiOffset.valid) | Fill(PredictWidth, 1.U(1.W)) >> (~preDecoderOut.cfiOffset.bits) 444 val f2_predecode_valids = VecInit(preDecoderOut.pd.map(instr => instr.valid)).asUInt & f2_jump_valids 445 446 def cut(cacheline: UInt, start: UInt) : Vec[UInt] ={ 447 if(HasCExtension){ 448 val result = Wire(Vec(PredictWidth + 1, UInt(16.W))) 449 val dataVec = cacheline.asTypeOf(Vec(blockBytes * 2/ 2, UInt(16.W))) 450 val startPtr = Cat(0.U(1.W), start(blockOffBits-1, 1)) 451 (0 until PredictWidth + 1).foreach( i => 452 result(i) := dataVec(startPtr + i.U) 453 ) 454 result 455 } else { 456 val result = Wire(Vec(PredictWidth, UInt(32.W)) ) 457 val dataVec = cacheline.asTypeOf(Vec(blockBytes * 2/ 4, UInt(32.W))) 458 val startPtr = Cat(0.U(1.W), start(blockOffBits-1, 2)) 459 (0 until PredictWidth).foreach( i => 460 result(i) := dataVec(startPtr + i.U) 461 ) 462 result 463 } 464 } 465 466 val f2_cut_data = cut( Cat(f2_datas.map(cacheline => cacheline.asUInt ).reverse).asUInt, f2_ftq_req.startAddr ) 467 468 // deal with secondary miss in f1 469 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))) 470 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))) 471 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) )) 472 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) )) 473 474 val isSameLine = f2_0_f1_0 || f2_0_f1_1 || f2_1_f1_0 || f2_1_f1_1 475 val sec_miss_sit = VecInit(Seq(f2_0_f1_0, f2_0_f1_1, f2_1_f1_0, f2_1_f1_1)) 476 val hasSecMiss = RegInit(false.B) 477 478 when(f2_flush){ 479 sec_miss_reg.map(sig => sig := false.B) 480 hasSecMiss := false.B 481 }.elsewhen(isSameLine && !f1_flush && f2_fire){ 482 sec_miss_reg.zipWithIndex.map{case(sig, i) => sig := sec_miss_sit(i)} 483 hasSecMiss := true.B 484 }.elsewhen((!isSameLine || f1_flush) && hasSecMiss && f2_fire){ 485 sec_miss_reg.map(sig => sig := false.B) 486 hasSecMiss := false.B 487 } 488 489 when((f2_0_f1_0 || f2_0_f1_1) && f2_fire){ 490 reservedRefillData(0) := f2_mq_datas(0) 491 } 492 493 when((f2_1_f1_0 || f2_1_f1_1) && f2_fire){ 494 reservedRefillData(1) := f2_mq_datas(1) 495 } 496 497 498 //--------------------------------------------- 499 // Fetch Stage 4 : 500 // * get data from last stage (hit from f1_hit_data/miss from missQueue response) 501 // * if at least one needed cacheline miss, wait for miss queue response (a wait_state machine) THIS IS TOO UGLY!!! 502 // * cut cacheline(s) and send to PreDecode 503 // * check if prediction is right (branch target and type, jump direction and type , jal target ) 504 //--------------------------------------------- 505 val f3_valid = RegInit(false.B) 506 val f3_ftq_req = RegEnable(next = f2_ftq_req, enable=f2_fire) 507 val f3_situation = RegEnable(next = f2_situation, enable=f2_fire) 508 val f3_doubleLine = RegEnable(next = f2_doubleLine, enable=f2_fire) 509 510 val f3_cut_data = RegEnable(next = f2_cut_data, enable=f2_fire) 511 val f3_except_pf = RegEnable(next = f2_except_pf, enable = f2_fire) 512 val f3_except_af = RegEnable(next = f2_except_af, enable = f2_fire) 513 val f3_hit = RegEnable(next = f2_hit , enable = f2_fire) 514 val f3_mmio = RegEnable(next = f2_mmio , enable = f2_fire) 515 516 //assert((f3_ftq_req.startAddr + 34.U) >= f3_ftq_req.fallThruAddr, "Fall through address exceeds the limit") 517 518 val f3_lastHalf = RegInit(0.U.asTypeOf(new LastHalfInfo)) 519 val f3_lastHalfMatch = f3_lastHalf.matchThisBlock(f3_ftq_req.startAddr) 520 val f3_except = VecInit((0 until 2).map{i => f3_except_pf(i) || f3_except_af(i)}) 521 val f3_has_except = f3_valid && (f3_except_af.reduce(_||_) || f3_except_pf.reduce(_||_)) 522 val f3_pAddrs = RegEnable(next = f2_pAddrs, enable = f2_fire) 523 524 val f3_mmio_data = Reg(UInt(maxInstrLen.W)) 525 526 val f3_data = if(HasCExtension) Wire(Vec(PredictWidth + 1, UInt(16.W))) else Wire(Vec(PredictWidth, UInt(32.W))) 527 f3_data := f3_cut_data 528 529 //performance counter 530 val f3_only_0_hit = RegEnable(next = only_0_hit, enable = f2_fire) 531 val f3_only_0_miss = RegEnable(next = only_0_miss, enable = f2_fire) 532 val f3_hit_0_hit_1 = RegEnable(next = hit_0_hit_1, enable = f2_fire) 533 val f3_hit_0_miss_1 = RegEnable(next = hit_0_miss_1, enable = f2_fire) 534 val f3_miss_0_hit_1 = RegEnable(next = miss_0_hit_1, enable = f2_fire) 535 val f3_miss_0_miss_1 = RegEnable(next = miss_0_miss_1, enable = f2_fire) 536 537 val mmio_idle :: mmio_send_req :: mmio_w_resp :: mmio_resend :: mmio_resend_w_resp :: mmio_w_commit :: Nil = Enum(6) 538 val mmio_state = RegInit(mmio_idle) 539 540 val f3_req_is_mmio = f3_mmio && f3_valid 541 val mmio_has_commited = VecInit(io.rob_commits.map{commit => commit.valid && commit.bits.ftqIdx === f3_ftq_req.ftqIdx && commit.bits.ftqOffset === 0.U}).asUInt.orR 542 val f3_mmio_req_commit = f3_req_is_mmio && mmio_state === mmio_w_commit && mmio_has_commited 543 544 val f3_mmio_to_commit = f3_req_is_mmio && mmio_state === mmio_w_commit 545 val f3_mmio_to_commit_next = RegNext(f3_mmio_to_commit) 546 val f3_mmio_can_go = f3_mmio_to_commit && !f3_mmio_to_commit_next 547 548 val f3_ftq_flush_self = fromFtq.redirect.valid && RedirectLevel.flushItself(fromFtq.redirect.bits.level) 549 val f3_ftq_flush_by_older = fromFtq.redirect.valid && isBefore(fromFtq.redirect.bits.ftqIdx, f3_ftq_req.ftqIdx) 550 551 val f3_need_not_flush = f3_req_is_mmio && fromFtq.redirect.valid && !f3_ftq_flush_self && !f3_ftq_flush_by_older 552 553 when(f3_flush && !f3_need_not_flush) {f3_valid := false.B} 554 .elsewhen(f2_fire && !f2_flush) {f3_valid := true.B } 555 .elsewhen(io.toIbuffer.fire() && !f3_req_is_mmio) {f3_valid := false.B} 556 .elsewhen{f3_req_is_mmio && f3_mmio_req_commit} {f3_valid := false.B} 557 558 val f3_mmio_use_seq_pc = RegInit(false.B) 559 560 val (redirect_ftqIdx, redirect_ftqOffset) = (fromFtq.redirect.bits.ftqIdx,fromFtq.redirect.bits.ftqOffset) 561 val redirect_mmio_req = fromFtq.redirect.valid && redirect_ftqIdx === f3_ftq_req.ftqIdx && redirect_ftqOffset === 0.U 562 563 when(RegNext(f2_fire && !f2_flush) && f3_req_is_mmio) { f3_mmio_use_seq_pc := true.B } 564 .elsewhen(redirect_mmio_req) { f3_mmio_use_seq_pc := false.B } 565 566 f3_ready := Mux(f3_req_is_mmio, io.toIbuffer.ready && f3_mmio_req_commit || !f3_valid , io.toIbuffer.ready || !f3_valid) 567 568 when(f3_req_is_mmio){ 569 f3_data(0) := f3_mmio_data(15, 0) 570 f3_data(1) := f3_mmio_data(31, 16) 571 } 572 573 when(fromUncache.fire()) {f3_mmio_data := fromUncache.bits.data} 574 575 576 switch(mmio_state){ 577 is(mmio_idle){ 578 when(f3_req_is_mmio){ 579 mmio_state := mmio_send_req 580 } 581 } 582 583 is(mmio_send_req){ 584 mmio_state := Mux(toUncache.fire(), mmio_w_resp, mmio_send_req ) 585 } 586 587 is(mmio_w_resp){ 588 when(fromUncache.fire()){ 589 val isRVC = fromUncache.bits.data(1,0) =/= 3.U 590 mmio_state := Mux(isRVC, mmio_resend , mmio_w_commit) 591 } 592 } 593 594 is(mmio_resend){ 595 mmio_state := Mux(toUncache.fire(), mmio_resend_w_resp, mmio_resend ) 596 } 597 598 is(mmio_resend_w_resp){ 599 when(fromUncache.fire()){ 600 mmio_state := mmio_w_commit 601 } 602 } 603 604 is(mmio_w_commit){ 605 when(mmio_has_commited){ 606 mmio_state := mmio_idle 607 } 608 } 609 } 610 611 when(f3_ftq_flush_self || f3_ftq_flush_by_older) { 612 mmio_state := mmio_idle 613 f3_mmio_data := 0.U 614 } 615 616 toUncache.valid := ((mmio_state === mmio_send_req) || (mmio_state === mmio_resend)) && f3_req_is_mmio 617 toUncache.bits.addr := Mux((mmio_state === mmio_resend), f3_pAddrs(0) + 2.U, f3_pAddrs(0)) 618 fromUncache.ready := true.B 619 620 val f3_bank_hit = RegEnable(next = f2_bank_hit, enable = f2_fire) 621 val f3_req_0 = io.toIbuffer.fire() 622 val f3_req_1 = io.toIbuffer.fire() && f3_doubleLine 623 val f3_hit_0 = io.toIbuffer.fire() & f3_bank_hit(0) 624 val f3_hit_1 = io.toIbuffer.fire() && f3_doubleLine & f3_bank_hit(1) 625 626 preDecoderIn.instValid := f3_valid && !f3_has_except 627 preDecoderIn.data := f3_data 628 preDecoderIn.startAddr := f3_ftq_req.startAddr 629 preDecoderIn.fallThruAddr := f3_ftq_req.fallThruAddr 630 preDecoderIn.fallThruError := f3_ftq_req.fallThruError 631 preDecoderIn.isDoubleLine := f3_doubleLine 632 preDecoderIn.ftqOffset := f3_ftq_req.ftqOffset 633 preDecoderIn.target := f3_ftq_req.target 634 preDecoderIn.oversize := f3_ftq_req.oversize 635 preDecoderIn.lastHalfMatch := f3_lastHalfMatch 636 preDecoderIn.pageFault := f3_except_pf 637 preDecoderIn.accessFault := f3_except_af 638 preDecoderIn.mmio := f3_mmio 639 preDecoderIn.frontendTrigger := io.frontendTrigger 640 preDecoderIn.csrTriggerEnable := io.csrTriggerEnable 641 642 643 // TODO: What if next packet does not match? 644 when (f3_flush) { 645 f3_lastHalf.valid := false.B 646 }.elsewhen (io.toIbuffer.fire()) { 647 f3_lastHalf.valid := preDecoderOut.hasLastHalf 648 f3_lastHalf.middlePC := preDecoderOut.realEndPC 649 } 650 651 val f3_predecode_range = VecInit(preDecoderOut.pd.map(inst => inst.valid)).asUInt 652 val f3_mmio_range = VecInit((0 until PredictWidth).map(i => if(i ==0) true.B else false.B)) 653 654 io.toIbuffer.valid := f3_valid && (!f3_req_is_mmio || f3_mmio_can_go) 655 io.toIbuffer.bits.instrs := preDecoderOut.instrs 656 io.toIbuffer.bits.valid := Mux(f3_req_is_mmio, f3_mmio_range.asUInt, f3_predecode_range & preDecoderOut.instrRange.asUInt) 657 io.toIbuffer.bits.pd := preDecoderOut.pd 658 io.toIbuffer.bits.ftqPtr := f3_ftq_req.ftqIdx 659 io.toIbuffer.bits.pc := preDecoderOut.pc 660 io.toIbuffer.bits.ftqOffset.zipWithIndex.map{case(a, i) => a.bits := i.U; a.valid := preDecoderOut.takens(i) && !f3_req_is_mmio} 661 io.toIbuffer.bits.foldpc := preDecoderOut.pc.map(i => XORFold(i(VAddrBits-1,1), MemPredPCWidth)) 662 io.toIbuffer.bits.ipf := preDecoderOut.pageFault 663 io.toIbuffer.bits.acf := preDecoderOut.accessFault 664 io.toIbuffer.bits.crossPageIPFFix := preDecoderOut.crossPageIPF 665 io.toIbuffer.bits.triggered := preDecoderOut.triggered 666 667 //Write back to Ftq 668 val f3_cache_fetch = f3_valid && !(f2_fire && !f2_flush) 669 val finishFetchMaskReg = RegNext(f3_cache_fetch) 670 671 672 val f3_mmio_missOffset = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 673 f3_mmio_missOffset.valid := f3_req_is_mmio 674 f3_mmio_missOffset.bits := 0.U 675 676 toFtq.pdWb.valid := (!finishFetchMaskReg && f3_valid && !f3_req_is_mmio) || (f3_mmio_req_commit && f3_mmio_use_seq_pc) 677 toFtq.pdWb.bits.pc := preDecoderOut.pc 678 toFtq.pdWb.bits.pd := preDecoderOut.pd 679 toFtq.pdWb.bits.pd.zipWithIndex.map{case(instr,i) => instr.valid := Mux(f3_req_is_mmio, f3_mmio_range(i), f3_predecode_range(i))} 680 toFtq.pdWb.bits.ftqIdx := f3_ftq_req.ftqIdx 681 toFtq.pdWb.bits.ftqOffset := f3_ftq_req.ftqOffset.bits 682 toFtq.pdWb.bits.misOffset := Mux(f3_req_is_mmio, f3_mmio_missOffset, preDecoderOut.misOffset) 683 toFtq.pdWb.bits.cfiOffset := preDecoderOut.cfiOffset 684 toFtq.pdWb.bits.target := Mux(f3_req_is_mmio,Mux((f3_mmio_data(1,0) =/= 3.U), f3_ftq_req.startAddr + 2.U , f3_ftq_req.startAddr + 4.U) ,preDecoderOut.target) 685 toFtq.pdWb.bits.jalTarget := preDecoderOut.jalTarget 686 toFtq.pdWb.bits.instrRange := Mux(f3_req_is_mmio, f3_mmio_range, preDecoderOut.instrRange) 687 688 val predecodeFlush = preDecoderOut.misOffset.valid && f3_valid 689 val predecodeFlushReg = RegNext(predecodeFlush && !(f2_fire && !f2_flush)) 690 691 val perfinfo = IO(new Bundle(){ 692 val perfEvents = Output(new PerfEventsBundle(15)) 693 }) 694 695 val perfEvents = Seq( 696 ("frontendFlush ", f3_redirect ), 697 ("ifu_req ", io.toIbuffer.fire() ), 698 ("ifu_miss ", io.toIbuffer.fire() && !f3_hit ), 699 ("ifu_req_cacheline_0 ", f3_req_0 ), 700 ("ifu_req_cacheline_1 ", f3_req_1 ), 701 ("ifu_req_cacheline_0_hit ", f3_hit_1 ), 702 ("ifu_req_cacheline_1_hit ", f3_hit_1 ), 703 ("only_0_hit ", f3_only_0_hit && io.toIbuffer.fire() ), 704 ("only_0_miss ", f3_only_0_miss && io.toIbuffer.fire() ), 705 ("hit_0_hit_1 ", f3_hit_0_hit_1 && io.toIbuffer.fire() ), 706 ("hit_0_miss_1 ", f3_hit_0_miss_1 && io.toIbuffer.fire() ), 707 ("miss_0_hit_1 ", f3_miss_0_hit_1 && io.toIbuffer.fire() ), 708 ("miss_0_miss_1 ", f3_miss_0_miss_1 && io.toIbuffer.fire() ), 709 ("cross_line_block ", io.toIbuffer.fire() && f3_situation(0) ), 710 ("fall_through_is_cacheline_end", io.toIbuffer.fire() && f3_situation(1) ), 711 ) 712 713 for (((perf_out,(perf_name,perf)),i) <- perfinfo.perfEvents.perf_events.zip(perfEvents).zipWithIndex) { 714 perf_out.incr_step := RegNext(perf) 715 } 716 717 f3_redirect := (!predecodeFlushReg && predecodeFlush && !f3_req_is_mmio) || (f3_mmio_req_commit && f3_mmio_use_seq_pc) 718 719 XSPerfAccumulate("ifu_req", io.toIbuffer.fire() ) 720 XSPerfAccumulate("ifu_miss", io.toIbuffer.fire() && !f3_hit ) 721 XSPerfAccumulate("ifu_req_cacheline_0", f3_req_0 ) 722 XSPerfAccumulate("ifu_req_cacheline_1", f3_req_1 ) 723 XSPerfAccumulate("ifu_req_cacheline_0_hit", f3_hit_0 ) 724 XSPerfAccumulate("ifu_req_cacheline_1_hit", f3_hit_1 ) 725 XSPerfAccumulate("frontendFlush", f3_redirect ) 726 XSPerfAccumulate("only_0_hit", f3_only_0_hit && io.toIbuffer.fire() ) 727 XSPerfAccumulate("only_0_miss", f3_only_0_miss && io.toIbuffer.fire() ) 728 XSPerfAccumulate("hit_0_hit_1", f3_hit_0_hit_1 && io.toIbuffer.fire() ) 729 XSPerfAccumulate("hit_0_miss_1", f3_hit_0_miss_1 && io.toIbuffer.fire() ) 730 XSPerfAccumulate("miss_0_hit_1", f3_miss_0_hit_1 && io.toIbuffer.fire() ) 731 XSPerfAccumulate("miss_0_miss_1", f3_miss_0_miss_1 && io.toIbuffer.fire() ) 732 XSPerfAccumulate("cross_line_block", io.toIbuffer.fire() && f3_situation(0) ) 733 XSPerfAccumulate("fall_through_is_cacheline_end", io.toIbuffer.fire() && f3_situation(1) ) 734} 735