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