1/*************************************************************************************** 2* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC) 3* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 4* Copyright (c) 2020-2021 Peng Cheng Laboratory 5* 6* XiangShan is licensed under Mulan PSL v2. 7* You can use this software according to the terms and conditions of the Mulan PSL v2. 8* You may obtain a copy of Mulan PSL v2 at: 9* http://license.coscl.org.cn/MulanPSL2 10* 11* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 12* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 13* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 14* 15* See the Mulan PSL v2 for more details. 16***************************************************************************************/ 17package xiangshan.frontend 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import xiangshan.frontend.icache._ 24import utils._ 25import utility._ 26import scala.math._ 27import java.util.ResourceBundle.Control 28 29class FrontendTopDownBundle(implicit p: Parameters) extends XSBundle { 30 val reasons = Vec(TopDownCounters.NumStallReasons.id, Bool()) 31 val stallWidth = UInt(log2Ceil(PredictWidth).W) 32} 33 34class FetchRequestBundle(implicit p: Parameters) extends XSBundle with HasICacheParameters { 35 36 //fast path: Timing critical 37 val startAddr = UInt(VAddrBits.W) 38 val nextlineStart = UInt(VAddrBits.W) 39 val nextStartAddr = UInt(VAddrBits.W) 40 //slow path 41 val ftqIdx = new FtqPtr 42 val ftqOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 43 44 val topdown_info = new FrontendTopDownBundle 45 46 def crossCacheline = startAddr(blockOffBits - 1) === 1.U 47 48 def fromFtqPcBundle(b: Ftq_RF_Components) = { 49 this.startAddr := b.startAddr 50 this.nextlineStart := b.nextLineAddr 51 when (b.fallThruError) { 52 val nextBlockHigherTemp = Mux(startAddr(log2Ceil(PredictWidth)+instOffsetBits), b.nextLineAddr, b.startAddr) 53 val nextBlockHigher = nextBlockHigherTemp(VAddrBits-1, log2Ceil(PredictWidth)+instOffsetBits+1) 54 this.nextStartAddr := 55 Cat(nextBlockHigher, 56 startAddr(log2Ceil(PredictWidth)+instOffsetBits) ^ 1.U(1.W), 57 startAddr(log2Ceil(PredictWidth)+instOffsetBits-1, instOffsetBits), 58 0.U(instOffsetBits.W) 59 ) 60 } 61 this 62 } 63 override def toPrintable: Printable = { 64 p"[start] ${Hexadecimal(startAddr)} [next] ${Hexadecimal(nextlineStart)}" + 65 p"[tgt] ${Hexadecimal(nextStartAddr)} [ftqIdx] $ftqIdx [jmp] v:${ftqOffset.valid}" + 66 p" offset: ${ftqOffset.bits}\n" 67 } 68} 69 70class FtqICacheInfo(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 71 val startAddr = UInt(VAddrBits.W) 72 val nextlineStart = UInt(VAddrBits.W) 73 val ftqIdx = new FtqPtr 74 def crossCacheline = startAddr(blockOffBits - 1) === 1.U 75 def fromFtqPcBundle(b: Ftq_RF_Components) = { 76 this.startAddr := b.startAddr 77 this.nextlineStart := b.nextLineAddr 78 this 79 } 80} 81 82class IFUICacheIO(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 83 val icacheReady = Output(Bool()) 84 val resp = Vec(PortNumber, ValidIO(new ICacheMainPipeResp)) 85 val topdownIcacheMiss = Output(Bool()) 86 val topdownItlbMiss = Output(Bool()) 87} 88 89class FtqToICacheRequestBundle(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 90 val pcMemRead = Vec(5, new FtqICacheInfo) 91 val readValid = Vec(5, Bool()) 92} 93 94 95class PredecodeWritebackBundle(implicit p:Parameters) extends XSBundle { 96 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 97 val pd = Vec(PredictWidth, new PreDecodeInfo) // TODO: redefine Predecode 98 val ftqIdx = new FtqPtr 99 val ftqOffset = UInt(log2Ceil(PredictWidth).W) 100 val misOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 101 val cfiOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 102 val target = UInt(VAddrBits.W) 103 val jalTarget = UInt(VAddrBits.W) 104 val instrRange = Vec(PredictWidth, Bool()) 105} 106 107class mmioCommitRead(implicit p: Parameters) extends XSBundle { 108 val mmioFtqPtr = Output(new FtqPtr) 109 val mmioLastCommit = Input(Bool()) 110} 111 112object ExceptionType { 113 def none = "b00".U 114 def ipf = "b01".U 115 def igpf = "b10".U 116 def acf = "b11".U 117 def width = 2 118} 119 120class FetchToIBuffer(implicit p: Parameters) extends XSBundle { 121 val instrs = Vec(PredictWidth, UInt(32.W)) 122 val valid = UInt(PredictWidth.W) 123 val enqEnable = UInt(PredictWidth.W) 124 val pd = Vec(PredictWidth, new PreDecodeInfo) 125 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 126 val foldpc = Vec(PredictWidth, UInt(MemPredPCWidth.W)) 127 val ftqPtr = new FtqPtr 128 val ftqOffset = Vec(PredictWidth, ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 129 val exceptionType = Vec(PredictWidth, UInt(ExceptionType.width.W)) 130 val crossPageIPFFix = Vec(PredictWidth, Bool()) 131 val triggered = Vec(PredictWidth, new TriggerCf) 132 val topdown_info = new FrontendTopDownBundle 133} 134 135// class BitWiseUInt(val width: Int, val init: UInt) extends Module { 136// val io = IO(new Bundle { 137// val set 138// }) 139// } 140// Move from BPU 141abstract class GlobalHistory(implicit p: Parameters) extends XSBundle with HasBPUConst { 142 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): GlobalHistory 143} 144 145class ShiftingGlobalHistory(implicit p: Parameters) extends GlobalHistory { 146 val predHist = UInt(HistoryLength.W) 147 148 def update(shift: UInt, taken: Bool, hist: UInt = this.predHist): ShiftingGlobalHistory = { 149 val g = Wire(new ShiftingGlobalHistory) 150 g.predHist := (hist << shift) | taken 151 g 152 } 153 154 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): ShiftingGlobalHistory = { 155 require(br_valids.length == numBr) 156 require(real_taken_mask.length == numBr) 157 val last_valid_idx = PriorityMux( 158 br_valids.reverse :+ true.B, 159 (numBr to 0 by -1).map(_.U(log2Ceil(numBr+1).W)) 160 ) 161 val first_taken_idx = PriorityEncoder(false.B +: real_taken_mask) 162 val smaller = Mux(last_valid_idx < first_taken_idx, 163 last_valid_idx, 164 first_taken_idx 165 ) 166 val shift = smaller 167 val taken = real_taken_mask.reduce(_||_) 168 update(shift, taken, this.predHist) 169 } 170 171 // static read 172 def read(n: Int): Bool = predHist.asBools(n) 173 174 final def === (that: ShiftingGlobalHistory): Bool = { 175 predHist === that.predHist 176 } 177 178 final def =/= (that: ShiftingGlobalHistory): Bool = !(this === that) 179} 180 181// circular global history pointer 182class CGHPtr(implicit p: Parameters) extends CircularQueuePtr[CGHPtr]( 183 p => p(XSCoreParamsKey).HistoryLength 184){ 185} 186 187object CGHPtr { 188 def apply(f: Bool, v: UInt)(implicit p: Parameters): CGHPtr = { 189 val ptr = Wire(new CGHPtr) 190 ptr.flag := f 191 ptr.value := v 192 ptr 193 } 194 def inverse(ptr: CGHPtr)(implicit p: Parameters): CGHPtr = { 195 apply(!ptr.flag, ptr.value) 196 } 197} 198 199class CircularGlobalHistory(implicit p: Parameters) extends GlobalHistory { 200 val buffer = Vec(HistoryLength, Bool()) 201 type HistPtr = UInt 202 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): CircularGlobalHistory = { 203 this 204 } 205} 206 207class FoldedHistory(val len: Int, val compLen: Int, val max_update_num: Int)(implicit p: Parameters) 208 extends XSBundle with HasBPUConst { 209 require(compLen >= 1) 210 require(len > 0) 211 // require(folded_len <= len) 212 require(compLen >= max_update_num) 213 val folded_hist = UInt(compLen.W) 214 215 def need_oldest_bits = len > compLen 216 def info = (len, compLen) 217 def oldest_bit_to_get_from_ghr = (0 until max_update_num).map(len - _ - 1) 218 def oldest_bit_pos_in_folded = oldest_bit_to_get_from_ghr map (_ % compLen) 219 def oldest_bit_wrap_around = oldest_bit_to_get_from_ghr map (_ / compLen > 0) 220 def oldest_bit_start = oldest_bit_pos_in_folded.head 221 222 def get_oldest_bits_from_ghr(ghr: Vec[Bool], histPtr: CGHPtr) = { 223 // TODO: wrap inc for histPtr value 224 oldest_bit_to_get_from_ghr.map(i => ghr((histPtr + (i+1).U).value)) 225 } 226 227 def circular_shift_left(src: UInt, shamt: Int) = { 228 val srcLen = src.getWidth 229 val src_doubled = Cat(src, src) 230 val shifted = src_doubled(srcLen*2-1-shamt, srcLen-shamt) 231 shifted 232 } 233 234 // slow path, read bits from ghr 235 def update(ghr: Vec[Bool], histPtr: CGHPtr, num: Int, taken: Bool): FoldedHistory = { 236 val oldest_bits = VecInit(get_oldest_bits_from_ghr(ghr, histPtr)) 237 update(oldest_bits, num, taken) 238 } 239 240 241 // fast path, use pre-read oldest bits 242 def update(ob: Vec[Bool], num: Int, taken: Bool): FoldedHistory = { 243 // do xors for several bitsets at specified bits 244 def bitsets_xor(len: Int, bitsets: Seq[Seq[Tuple2[Int, Bool]]]) = { 245 val res = Wire(Vec(len, Bool())) 246 // println(f"num bitsets: ${bitsets.length}") 247 // println(f"bitsets $bitsets") 248 val resArr = Array.fill(len)(List[Bool]()) 249 for (bs <- bitsets) { 250 for ((n, b) <- bs) { 251 resArr(n) = b :: resArr(n) 252 } 253 } 254 // println(f"${resArr.mkString}") 255 // println(f"histLen: ${this.len}, foldedLen: $folded_len") 256 for (i <- 0 until len) { 257 // println(f"bit[$i], ${resArr(i).mkString}") 258 if (resArr(i).length == 0) { 259 println(f"[error] bits $i is not assigned in folded hist update logic! histlen:${this.len}, compLen:$compLen") 260 } 261 res(i) := resArr(i).foldLeft(false.B)(_^_) 262 } 263 res.asUInt 264 } 265 266 val new_folded_hist = if (need_oldest_bits) { 267 val oldest_bits = ob 268 require(oldest_bits.length == max_update_num) 269 // mask off bits that do not update 270 val oldest_bits_masked = oldest_bits.zipWithIndex.map{ 271 case (ob, i) => ob && (i < num).B 272 } 273 // if a bit does not wrap around, it should not be xored when it exits 274 val oldest_bits_set = (0 until max_update_num).filter(oldest_bit_wrap_around).map(i => (oldest_bit_pos_in_folded(i), oldest_bits_masked(i))) 275 276 // println(f"old bits pos ${oldest_bits_set.map(_._1)}") 277 278 // only the last bit could be 1, as we have at most one taken branch at a time 279 val newest_bits_masked = VecInit((0 until max_update_num).map(i => taken && ((i+1) == num).B)).asUInt 280 // if a bit does not wrap around, newest bits should not be xored onto it either 281 val newest_bits_set = (0 until max_update_num).map(i => (compLen-1-i, newest_bits_masked(i))) 282 283 // println(f"new bits set ${newest_bits_set.map(_._1)}") 284 // 285 val original_bits_masked = VecInit(folded_hist.asBools.zipWithIndex.map{ 286 case (fb, i) => fb && !(num >= (len-i)).B 287 }) 288 val original_bits_set = (0 until compLen).map(i => (i, original_bits_masked(i))) 289 290 // do xor then shift 291 val xored = bitsets_xor(compLen, Seq(original_bits_set, oldest_bits_set, newest_bits_set)) 292 circular_shift_left(xored, num) 293 } else { 294 // histLen too short to wrap around 295 ((folded_hist << num) | taken)(compLen-1,0) 296 } 297 298 val fh = WireInit(this) 299 fh.folded_hist := new_folded_hist 300 fh 301 } 302} 303 304class AheadFoldedHistoryOldestBits(val len: Int, val max_update_num: Int)(implicit p: Parameters) extends XSBundle { 305 val bits = Vec(max_update_num*2, Bool()) 306 // def info = (len, compLen) 307 def getRealOb(brNumOH: UInt): Vec[Bool] = { 308 val ob = Wire(Vec(max_update_num, Bool())) 309 for (i <- 0 until max_update_num) { 310 ob(i) := Mux1H(brNumOH, bits.drop(i).take(numBr+1)) 311 } 312 ob 313 } 314} 315 316class AllAheadFoldedHistoryOldestBits(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 317 val afhob = MixedVec(gen.filter(t => t._1 > t._2).map{_._1} 318 .toSet.toList.map(l => new AheadFoldedHistoryOldestBits(l, numBr))) // remove duplicates 319 require(gen.toSet.toList.equals(gen)) 320 def getObWithInfo(info: Tuple2[Int, Int]) = { 321 val selected = afhob.filter(_.len == info._1) 322 require(selected.length == 1) 323 selected(0) 324 } 325 def read(ghv: Vec[Bool], ptr: CGHPtr) = { 326 val hisLens = afhob.map(_.len) 327 val bitsToRead = hisLens.flatMap(l => (0 until numBr*2).map(i => l-i-1)).toSet // remove duplicates 328 val bitsWithInfo = bitsToRead.map(pos => (pos, ghv((ptr+(pos+1).U).value))) 329 for (ob <- afhob) { 330 for (i <- 0 until numBr*2) { 331 val pos = ob.len - i - 1 332 val bit_found = bitsWithInfo.filter(_._1 == pos).toList 333 require(bit_found.length == 1) 334 ob.bits(i) := bit_found(0)._2 335 } 336 } 337 } 338} 339 340class AllFoldedHistories(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 341 val hist = MixedVec(gen.map{case (l, cl) => new FoldedHistory(l, cl, numBr)}) 342 // println(gen.mkString) 343 require(gen.toSet.toList.equals(gen)) 344 def getHistWithInfo(info: Tuple2[Int, Int]) = { 345 val selected = hist.filter(_.info.equals(info)) 346 require(selected.length == 1) 347 selected(0) 348 } 349 def autoConnectFrom(that: AllFoldedHistories) = { 350 require(this.hist.length <= that.hist.length) 351 for (h <- this.hist) { 352 h := that.getHistWithInfo(h.info) 353 } 354 } 355 def update(ghv: Vec[Bool], ptr: CGHPtr, shift: Int, taken: Bool): AllFoldedHistories = { 356 val res = WireInit(this) 357 for (i <- 0 until this.hist.length) { 358 res.hist(i) := this.hist(i).update(ghv, ptr, shift, taken) 359 } 360 res 361 } 362 def update(afhob: AllAheadFoldedHistoryOldestBits, lastBrNumOH: UInt, shift: Int, taken: Bool): AllFoldedHistories = { 363 val res = WireInit(this) 364 for (i <- 0 until this.hist.length) { 365 val fh = this.hist(i) 366 if (fh.need_oldest_bits) { 367 val info = fh.info 368 val selectedAfhob = afhob.getObWithInfo(info) 369 val ob = selectedAfhob.getRealOb(lastBrNumOH) 370 res.hist(i) := this.hist(i).update(ob, shift, taken) 371 } else { 372 val dumb = Wire(Vec(numBr, Bool())) // not needed 373 dumb := DontCare 374 res.hist(i) := this.hist(i).update(dumb, shift, taken) 375 } 376 } 377 res 378 } 379 380 def display(cond: Bool) = { 381 for (h <- hist) { 382 XSDebug(cond, p"hist len ${h.len}, folded len ${h.compLen}, value ${Binary(h.folded_hist)}\n") 383 } 384 } 385} 386 387class TableAddr(val idxBits: Int, val banks: Int)(implicit p: Parameters) extends XSBundle{ 388 def tagBits = VAddrBits - idxBits - instOffsetBits 389 390 val tag = UInt(tagBits.W) 391 val idx = UInt(idxBits.W) 392 val offset = UInt(instOffsetBits.W) 393 394 def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this) 395 def getTag(x: UInt) = fromUInt(x).tag 396 def getIdx(x: UInt) = fromUInt(x).idx 397 def getBank(x: UInt) = if (banks > 1) getIdx(x)(log2Up(banks) - 1, 0) else 0.U 398 def getBankIdx(x: UInt) = if (banks > 1) getIdx(x)(idxBits - 1, log2Up(banks)) else getIdx(x) 399} 400 401trait BasicPrediction extends HasXSParameter { 402 def cfiIndex: ValidUndirectioned[UInt] 403 def target(pc: UInt): UInt 404 def lastBrPosOH: Vec[Bool] 405 def brTaken: Bool 406 def shouldShiftVec: Vec[Bool] 407 def fallThruError: Bool 408} 409 410// selectByTaken selects some data according to takenMask 411// allTargets should be in a Vec, like [taken0, taken1, ..., not taken, not hit] 412object selectByTaken { 413 def apply[T <: Data](takenMask: Vec[Bool], hit: Bool, allTargets: Vec[T]): T = { 414 val selVecOH = 415 takenMask.zipWithIndex.map { case (t, i) => !takenMask.take(i).fold(false.B)(_ || _) && t && hit } :+ 416 (!takenMask.asUInt.orR && hit) :+ !hit 417 Mux1H(selVecOH, allTargets) 418 } 419} 420 421class FullBranchPrediction(implicit p: Parameters) extends XSBundle with HasBPUConst with BasicPrediction { 422 val br_taken_mask = Vec(numBr, Bool()) 423 424 val slot_valids = Vec(totalSlot, Bool()) 425 426 val targets = Vec(totalSlot, UInt(VAddrBits.W)) 427 val jalr_target = UInt(VAddrBits.W) // special path for indirect predictors 428 val offsets = Vec(totalSlot, UInt(log2Ceil(PredictWidth).W)) 429 val fallThroughAddr = UInt(VAddrBits.W) 430 val fallThroughErr = Bool() 431 val multiHit = Bool() 432 433 val is_jal = Bool() 434 val is_jalr = Bool() 435 val is_call = Bool() 436 val is_ret = Bool() 437 val last_may_be_rvi_call = Bool() 438 val is_br_sharing = Bool() 439 440 // val call_is_rvc = Bool() 441 val hit = Bool() 442 443 val predCycle = if (!env.FPGAPlatform) Some(UInt(64.W)) else None 444 445 def br_slot_valids = slot_valids.init 446 def tail_slot_valid = slot_valids.last 447 448 def br_valids = { 449 VecInit(br_slot_valids :+ (tail_slot_valid && is_br_sharing)) 450 } 451 452 def taken_mask_on_slot = { 453 VecInit( 454 (br_slot_valids zip br_taken_mask.init).map{ case (t, v) => t && v } :+ ( 455 tail_slot_valid && ( 456 is_br_sharing && br_taken_mask.last || !is_br_sharing 457 ) 458 ) 459 ) 460 } 461 462 def real_slot_taken_mask(): Vec[Bool] = { 463 VecInit(taken_mask_on_slot.map(_ && hit)) 464 } 465 466 // len numBr 467 def real_br_taken_mask(): Vec[Bool] = { 468 VecInit( 469 taken_mask_on_slot.map(_ && hit).init :+ 470 (br_taken_mask.last && tail_slot_valid && is_br_sharing && hit) 471 ) 472 } 473 474 // the vec indicating if ghr should shift on each branch 475 def shouldShiftVec = 476 VecInit(br_valids.zipWithIndex.map{ case (v, i) => 477 v && !real_br_taken_mask().take(i).reduceOption(_||_).getOrElse(false.B)}) 478 479 def lastBrPosOH = 480 VecInit((!hit || !br_valids.reduce(_||_)) +: // not hit or no brs in entry 481 (0 until numBr).map(i => 482 br_valids(i) && 483 !real_br_taken_mask().take(i).reduceOption(_||_).getOrElse(false.B) && // no brs taken in front it 484 (real_br_taken_mask()(i) || !br_valids.drop(i+1).reduceOption(_||_).getOrElse(false.B)) && // no brs behind it 485 hit 486 ) 487 ) 488 489 def brTaken = (br_valids zip br_taken_mask).map{ case (a, b) => a && b && hit}.reduce(_||_) 490 491 def target(pc: UInt): UInt = { 492 selectByTaken(taken_mask_on_slot, hit, allTarget(pc)) 493 } 494 495 // allTarget return a Vec of all possible target of a BP stage 496 // in the following order: [taken_target0, taken_target1, ..., fallThroughAddr, not hit (plus fetch width)] 497 // 498 // This exposes internal targets for timing optimization, 499 // since usually targets are generated quicker than taken 500 def allTarget(pc: UInt): Vec[UInt] = { 501 VecInit(targets :+ fallThroughAddr :+ (pc + (FetchWidth * 4).U)) 502 } 503 504 def fallThruError: Bool = hit && fallThroughErr 505 def ftbMultiHit: Bool = hit && multiHit 506 507 def hit_taken_on_jmp = 508 !real_slot_taken_mask().init.reduce(_||_) && 509 real_slot_taken_mask().last && !is_br_sharing 510 def hit_taken_on_call = hit_taken_on_jmp && is_call 511 def hit_taken_on_ret = hit_taken_on_jmp && is_ret 512 def hit_taken_on_jalr = hit_taken_on_jmp && is_jalr 513 514 def cfiIndex = { 515 val cfiIndex = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 516 cfiIndex.valid := real_slot_taken_mask().asUInt.orR 517 // when no takens, set cfiIndex to PredictWidth-1 518 cfiIndex.bits := 519 ParallelPriorityMux(real_slot_taken_mask(), offsets) | 520 Fill(log2Ceil(PredictWidth), (!real_slot_taken_mask().asUInt.orR).asUInt) 521 cfiIndex 522 } 523 524 def taken = br_taken_mask.reduce(_||_) || slot_valids.last // || (is_jal || is_jalr) 525 526 def fromFtbEntry( 527 entry: FTBEntry, 528 pc: UInt, 529 last_stage_pc: Option[Tuple2[UInt, Bool]] = None, 530 last_stage_entry: Option[Tuple2[FTBEntry, Bool]] = None 531 ) = { 532 slot_valids := entry.brSlots.map(_.valid) :+ entry.tailSlot.valid 533 targets := entry.getTargetVec(pc, last_stage_pc) // Use previous stage pc for better timing 534 jalr_target := targets.last 535 offsets := entry.getOffsetVec 536 is_jal := entry.tailSlot.valid && entry.isJal 537 is_jalr := entry.tailSlot.valid && entry.isJalr 538 is_call := entry.tailSlot.valid && entry.isCall 539 is_ret := entry.tailSlot.valid && entry.isRet 540 last_may_be_rvi_call := entry.last_may_be_rvi_call 541 is_br_sharing := entry.tailSlot.valid && entry.tailSlot.sharing 542 predCycle.map(_ := GTimer()) 543 544 val startLower = Cat(0.U(1.W), pc(instOffsetBits+log2Ceil(PredictWidth)-1, instOffsetBits)) 545 val endLowerwithCarry = Cat(entry.carry, entry.pftAddr) 546 fallThroughErr := startLower >= endLowerwithCarry || endLowerwithCarry > (startLower + (PredictWidth).U) 547 fallThroughAddr := Mux(fallThroughErr, pc + (FetchWidth * 4).U, entry.getFallThrough(pc, last_stage_entry)) 548 } 549 550 def display(cond: Bool): Unit = { 551 XSDebug(cond, p"[taken_mask] ${Binary(br_taken_mask.asUInt)} [hit] $hit\n") 552 } 553} 554 555class SpeculativeInfo(implicit p: Parameters) extends XSBundle 556 with HasBPUConst with BPUUtils { 557 val histPtr = new CGHPtr 558 val ssp = UInt(log2Up(RasSize).W) 559 val sctr = UInt(RasCtrSize.W) 560 val TOSW = new RASPtr 561 val TOSR = new RASPtr 562 val NOS = new RASPtr 563 val topAddr = UInt(VAddrBits.W) 564} 565 566class BranchPredictionBundle(implicit p: Parameters) extends XSBundle 567 with HasBPUConst with BPUUtils { 568 val pc = Vec(numDup, UInt(VAddrBits.W)) 569 val valid = Vec(numDup, Bool()) 570 val hasRedirect = Vec(numDup, Bool()) 571 val ftq_idx = new FtqPtr 572 val full_pred = Vec(numDup, new FullBranchPrediction) 573 574 575 def target(pc: UInt) = VecInit(full_pred.map(_.target(pc))) 576 def targets(pc: Vec[UInt]) = VecInit(pc.zipWithIndex.map{case (pc, idx) => full_pred(idx).target(pc)}) 577 def allTargets(pc: Vec[UInt]) = VecInit(pc.zipWithIndex.map{case (pc, idx) => full_pred(idx).allTarget(pc)}) 578 def cfiIndex = VecInit(full_pred.map(_.cfiIndex)) 579 def lastBrPosOH = VecInit(full_pred.map(_.lastBrPosOH)) 580 def brTaken = VecInit(full_pred.map(_.brTaken)) 581 def shouldShiftVec = VecInit(full_pred.map(_.shouldShiftVec)) 582 def fallThruError = VecInit(full_pred.map(_.fallThruError)) 583 def ftbMultiHit = VecInit(full_pred.map(_.ftbMultiHit)) 584 585 def taken = VecInit(cfiIndex.map(_.valid)) 586 587 def getTarget = targets(pc) 588 def getAllTargets = allTargets(pc) 589 590 def display(cond: Bool): Unit = { 591 XSDebug(cond, p"[pc] ${Hexadecimal(pc(0))}\n") 592 full_pred(0).display(cond) 593 } 594} 595 596class BranchPredictionResp(implicit p: Parameters) extends XSBundle with HasBPUConst { 597 val s1 = new BranchPredictionBundle 598 val s2 = new BranchPredictionBundle 599 val s3 = new BranchPredictionBundle 600 601 val s1_uftbHit = Bool() 602 val s1_uftbHasIndirect = Bool() 603 val s1_ftbCloseReq = Bool() 604 605 val last_stage_meta = UInt(MaxMetaLength.W) 606 val last_stage_spec_info = new Ftq_Redirect_SRAMEntry 607 val last_stage_ftb_entry = new FTBEntry 608 609 val topdown_info = new FrontendTopDownBundle 610 611 def selectedResp ={ 612 val res = 613 PriorityMux(Seq( 614 ((s3.valid(3) && s3.hasRedirect(3)) -> s3), 615 ((s2.valid(3) && s2.hasRedirect(3)) -> s2), 616 (s1.valid(3) -> s1) 617 )) 618 res 619 } 620 def selectedRespIdxForFtq = 621 PriorityMux(Seq( 622 ((s3.valid(3) && s3.hasRedirect(3)) -> BP_S3), 623 ((s2.valid(3) && s2.hasRedirect(3)) -> BP_S2), 624 (s1.valid(3) -> BP_S1) 625 )) 626 def lastStage = s3 627} 628 629class BpuToFtqBundle(implicit p: Parameters) extends BranchPredictionResp {} 630 631class BranchPredictionUpdate(implicit p: Parameters) extends XSBundle with HasBPUConst { 632 val pc = UInt(VAddrBits.W) 633 val spec_info = new SpeculativeInfo 634 val ftb_entry = new FTBEntry() 635 636 val cfi_idx = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 637 val br_taken_mask = Vec(numBr, Bool()) 638 val br_committed = Vec(numBr, Bool()) // High only when br valid && br committed 639 val jmp_taken = Bool() 640 val mispred_mask = Vec(numBr+1, Bool()) 641 val pred_hit = Bool() 642 val false_hit = Bool() 643 val new_br_insert_pos = Vec(numBr, Bool()) 644 val old_entry = Bool() 645 val meta = UInt(MaxMetaLength.W) 646 val full_target = UInt(VAddrBits.W) 647 val from_stage = UInt(2.W) 648 val ghist = UInt(HistoryLength.W) 649 650 def is_jal = ftb_entry.tailSlot.valid && ftb_entry.isJal 651 def is_jalr = ftb_entry.tailSlot.valid && ftb_entry.isJalr 652 def is_call = ftb_entry.tailSlot.valid && ftb_entry.isCall 653 def is_ret = ftb_entry.tailSlot.valid && ftb_entry.isRet 654 655 def is_call_taken = is_call && jmp_taken && cfi_idx.valid && cfi_idx.bits === ftb_entry.tailSlot.offset 656 def is_ret_taken = is_ret && jmp_taken && cfi_idx.valid && cfi_idx.bits === ftb_entry.tailSlot.offset 657 658 def display(cond: Bool) = { 659 XSDebug(cond, p"-----------BranchPredictionUpdate-----------\n") 660 XSDebug(cond, p"[mispred_mask] ${Binary(mispred_mask.asUInt)} [false_hit] $false_hit\n") 661 XSDebug(cond, p"[new_br_insert_pos] ${Binary(new_br_insert_pos.asUInt)}\n") 662 XSDebug(cond, p"--------------------------------------------\n") 663 } 664} 665 666class BranchPredictionRedirect(implicit p: Parameters) extends Redirect with HasBPUConst { 667 // override def toPrintable: Printable = { 668 // p"-----------BranchPredictionRedirect----------- " + 669 // p"-----------cfiUpdate----------- " + 670 // p"[pc] ${Hexadecimal(cfiUpdate.pc)} " + 671 // p"[predTaken] ${cfiUpdate.predTaken}, [taken] ${cfiUpdate.taken}, [isMisPred] ${cfiUpdate.isMisPred} " + 672 // p"[target] ${Hexadecimal(cfiUpdate.target)} " + 673 // p"------------------------------- " + 674 // p"[robPtr] f=${robIdx.flag} v=${robIdx.value} " + 675 // p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} " + 676 // p"[ftqOffset] ${ftqOffset} " + 677 // p"[level] ${level}, [interrupt] ${interrupt} " + 678 // p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value} " + 679 // p"[stFtqOffset] ${stFtqOffset} " + 680 // p"\n" 681 682 // } 683 684 // TODO: backend should pass topdown signals here 685 // must not change its parent since BPU has used asTypeOf(this type) from its parent class 686 require(isInstanceOf[Redirect]) 687 val BTBMissBubble = Bool() 688 def ControlRedirectBubble = debugIsCtrl 689 // if mispred br not in ftb, count as BTB miss 690 def ControlBTBMissBubble = ControlRedirectBubble && !cfiUpdate.br_hit && !cfiUpdate.jr_hit 691 def TAGEMissBubble = ControlRedirectBubble && cfiUpdate.br_hit && !cfiUpdate.sc_hit 692 def SCMissBubble = ControlRedirectBubble && cfiUpdate.br_hit && cfiUpdate.sc_hit 693 def ITTAGEMissBubble = ControlRedirectBubble && cfiUpdate.jr_hit && !cfiUpdate.pd.isRet 694 def RASMissBubble = ControlRedirectBubble && cfiUpdate.jr_hit && cfiUpdate.pd.isRet 695 def MemVioRedirectBubble = debugIsMemVio 696 def OtherRedirectBubble = !debugIsCtrl && !debugIsMemVio 697 698 def connectRedirect(source: Redirect): Unit = { 699 for ((name, data) <- this.elements) { 700 if (source.elements.contains(name)) { 701 data := source.elements(name) 702 } 703 } 704 } 705 706 def display(cond: Bool): Unit = { 707 XSDebug(cond, p"-----------BranchPredictionRedirect----------- \n") 708 XSDebug(cond, p"-----------cfiUpdate----------- \n") 709 XSDebug(cond, p"[pc] ${Hexadecimal(cfiUpdate.pc)}\n") 710 // XSDebug(cond, p"[hist] ${Binary(cfiUpdate.hist.predHist)}\n") 711 XSDebug(cond, p"[br_hit] ${cfiUpdate.br_hit} [isMisPred] ${cfiUpdate.isMisPred}\n") 712 XSDebug(cond, p"[pred_taken] ${cfiUpdate.predTaken} [taken] ${cfiUpdate.taken} [isMisPred] ${cfiUpdate.isMisPred}\n") 713 XSDebug(cond, p"[target] ${Hexadecimal(cfiUpdate.target)} \n") 714 XSDebug(cond, p"[shift] ${cfiUpdate.shift}\n") 715 XSDebug(cond, p"------------------------------- \n") 716 XSDebug(cond, p"[robPtr] f=${robIdx.flag} v=${robIdx.value}\n") 717 XSDebug(cond, p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} \n") 718 XSDebug(cond, p"[ftqOffset] ${ftqOffset} \n") 719 XSDebug(cond, p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value}\n") 720 XSDebug(cond, p"[stFtqOffset] ${stFtqOffset}\n") 721 XSDebug(cond, p"---------------------------------------------- \n") 722 } 723} 724