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