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 gpaddr = Vec(PredictWidth, UInt(GPAddrBits.W)) 134 val topdown_info = new FrontendTopDownBundle 135} 136 137// class BitWiseUInt(val width: Int, val init: UInt) extends Module { 138// val io = IO(new Bundle { 139// val set 140// }) 141// } 142// Move from BPU 143abstract class GlobalHistory(implicit p: Parameters) extends XSBundle with HasBPUConst { 144 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): GlobalHistory 145} 146 147class ShiftingGlobalHistory(implicit p: Parameters) extends GlobalHistory { 148 val predHist = UInt(HistoryLength.W) 149 150 def update(shift: UInt, taken: Bool, hist: UInt = this.predHist): ShiftingGlobalHistory = { 151 val g = Wire(new ShiftingGlobalHistory) 152 g.predHist := (hist << shift) | taken 153 g 154 } 155 156 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): ShiftingGlobalHistory = { 157 require(br_valids.length == numBr) 158 require(real_taken_mask.length == numBr) 159 val last_valid_idx = PriorityMux( 160 br_valids.reverse :+ true.B, 161 (numBr to 0 by -1).map(_.U(log2Ceil(numBr+1).W)) 162 ) 163 val first_taken_idx = PriorityEncoder(false.B +: real_taken_mask) 164 val smaller = Mux(last_valid_idx < first_taken_idx, 165 last_valid_idx, 166 first_taken_idx 167 ) 168 val shift = smaller 169 val taken = real_taken_mask.reduce(_||_) 170 update(shift, taken, this.predHist) 171 } 172 173 // static read 174 def read(n: Int): Bool = predHist.asBools(n) 175 176 final def === (that: ShiftingGlobalHistory): Bool = { 177 predHist === that.predHist 178 } 179 180 final def =/= (that: ShiftingGlobalHistory): Bool = !(this === that) 181} 182 183// circular global history pointer 184class CGHPtr(implicit p: Parameters) extends CircularQueuePtr[CGHPtr]( 185 p => p(XSCoreParamsKey).HistoryLength 186){ 187} 188 189object CGHPtr { 190 def apply(f: Bool, v: UInt)(implicit p: Parameters): CGHPtr = { 191 val ptr = Wire(new CGHPtr) 192 ptr.flag := f 193 ptr.value := v 194 ptr 195 } 196 def inverse(ptr: CGHPtr)(implicit p: Parameters): CGHPtr = { 197 apply(!ptr.flag, ptr.value) 198 } 199} 200 201class CircularGlobalHistory(implicit p: Parameters) extends GlobalHistory { 202 val buffer = Vec(HistoryLength, Bool()) 203 type HistPtr = UInt 204 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): CircularGlobalHistory = { 205 this 206 } 207} 208 209class FoldedHistory(val len: Int, val compLen: Int, val max_update_num: Int)(implicit p: Parameters) 210 extends XSBundle with HasBPUConst { 211 require(compLen >= 1) 212 require(len > 0) 213 // require(folded_len <= len) 214 require(compLen >= max_update_num) 215 val folded_hist = UInt(compLen.W) 216 217 def need_oldest_bits = len > compLen 218 def info = (len, compLen) 219 def oldest_bit_to_get_from_ghr = (0 until max_update_num).map(len - _ - 1) 220 def oldest_bit_pos_in_folded = oldest_bit_to_get_from_ghr map (_ % compLen) 221 def oldest_bit_wrap_around = oldest_bit_to_get_from_ghr map (_ / compLen > 0) 222 def oldest_bit_start = oldest_bit_pos_in_folded.head 223 224 def get_oldest_bits_from_ghr(ghr: Vec[Bool], histPtr: CGHPtr) = { 225 // TODO: wrap inc for histPtr value 226 oldest_bit_to_get_from_ghr.map(i => ghr((histPtr + (i+1).U).value)) 227 } 228 229 def circular_shift_left(src: UInt, shamt: Int) = { 230 val srcLen = src.getWidth 231 val src_doubled = Cat(src, src) 232 val shifted = src_doubled(srcLen*2-1-shamt, srcLen-shamt) 233 shifted 234 } 235 236 // slow path, read bits from ghr 237 def update(ghr: Vec[Bool], histPtr: CGHPtr, num: Int, taken: Bool): FoldedHistory = { 238 val oldest_bits = VecInit(get_oldest_bits_from_ghr(ghr, histPtr)) 239 update(oldest_bits, num, taken) 240 } 241 242 243 // fast path, use pre-read oldest bits 244 def update(ob: Vec[Bool], num: Int, taken: Bool): FoldedHistory = { 245 // do xors for several bitsets at specified bits 246 def bitsets_xor(len: Int, bitsets: Seq[Seq[Tuple2[Int, Bool]]]) = { 247 val res = Wire(Vec(len, Bool())) 248 // println(f"num bitsets: ${bitsets.length}") 249 // println(f"bitsets $bitsets") 250 val resArr = Array.fill(len)(List[Bool]()) 251 for (bs <- bitsets) { 252 for ((n, b) <- bs) { 253 resArr(n) = b :: resArr(n) 254 } 255 } 256 // println(f"${resArr.mkString}") 257 // println(f"histLen: ${this.len}, foldedLen: $folded_len") 258 for (i <- 0 until len) { 259 // println(f"bit[$i], ${resArr(i).mkString}") 260 if (resArr(i).length > 2) { 261 println(f"[warning] update logic of foldest history has two or more levels of xor gates! " + 262 f"histlen:${this.len}, compLen:$compLen, at bit $i") 263 } 264 if (resArr(i).length == 0) { 265 println(f"[error] bits $i is not assigned in folded hist update logic! histlen:${this.len}, compLen:$compLen") 266 } 267 res(i) := resArr(i).foldLeft(false.B)(_^_) 268 } 269 res.asUInt 270 } 271 272 val new_folded_hist = if (need_oldest_bits) { 273 val oldest_bits = ob 274 require(oldest_bits.length == max_update_num) 275 // mask off bits that do not update 276 val oldest_bits_masked = oldest_bits.zipWithIndex.map{ 277 case (ob, i) => ob && (i < num).B 278 } 279 // if a bit does not wrap around, it should not be xored when it exits 280 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))) 281 282 // println(f"old bits pos ${oldest_bits_set.map(_._1)}") 283 284 // only the last bit could be 1, as we have at most one taken branch at a time 285 val newest_bits_masked = VecInit((0 until max_update_num).map(i => taken && ((i+1) == num).B)).asUInt 286 // if a bit does not wrap around, newest bits should not be xored onto it either 287 val newest_bits_set = (0 until max_update_num).map(i => (compLen-1-i, newest_bits_masked(i))) 288 289 // println(f"new bits set ${newest_bits_set.map(_._1)}") 290 // 291 val original_bits_masked = VecInit(folded_hist.asBools.zipWithIndex.map{ 292 case (fb, i) => fb && !(num >= (len-i)).B 293 }) 294 val original_bits_set = (0 until compLen).map(i => (i, original_bits_masked(i))) 295 296 // do xor then shift 297 val xored = bitsets_xor(compLen, Seq(original_bits_set, oldest_bits_set, newest_bits_set)) 298 circular_shift_left(xored, num) 299 } else { 300 // histLen too short to wrap around 301 ((folded_hist << num) | taken)(compLen-1,0) 302 } 303 304 val fh = WireInit(this) 305 fh.folded_hist := new_folded_hist 306 fh 307 } 308} 309 310class AheadFoldedHistoryOldestBits(val len: Int, val max_update_num: Int)(implicit p: Parameters) extends XSBundle { 311 val bits = Vec(max_update_num*2, Bool()) 312 // def info = (len, compLen) 313 def getRealOb(brNumOH: UInt): Vec[Bool] = { 314 val ob = Wire(Vec(max_update_num, Bool())) 315 for (i <- 0 until max_update_num) { 316 ob(i) := Mux1H(brNumOH, bits.drop(i).take(numBr+1)) 317 } 318 ob 319 } 320} 321 322class AllAheadFoldedHistoryOldestBits(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 323 val afhob = MixedVec(gen.filter(t => t._1 > t._2).map{_._1} 324 .toSet.toList.map(l => new AheadFoldedHistoryOldestBits(l, numBr))) // remove duplicates 325 require(gen.toSet.toList.equals(gen)) 326 def getObWithInfo(info: Tuple2[Int, Int]) = { 327 val selected = afhob.filter(_.len == info._1) 328 require(selected.length == 1) 329 selected(0) 330 } 331 def read(ghv: Vec[Bool], ptr: CGHPtr) = { 332 val hisLens = afhob.map(_.len) 333 val bitsToRead = hisLens.flatMap(l => (0 until numBr*2).map(i => l-i-1)).toSet // remove duplicates 334 val bitsWithInfo = bitsToRead.map(pos => (pos, ghv((ptr+(pos+1).U).value))) 335 for (ob <- afhob) { 336 for (i <- 0 until numBr*2) { 337 val pos = ob.len - i - 1 338 val bit_found = bitsWithInfo.filter(_._1 == pos).toList 339 require(bit_found.length == 1) 340 ob.bits(i) := bit_found(0)._2 341 } 342 } 343 } 344} 345 346class AllFoldedHistories(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 347 val hist = MixedVec(gen.map{case (l, cl) => new FoldedHistory(l, cl, numBr)}) 348 // println(gen.mkString) 349 require(gen.toSet.toList.equals(gen)) 350 def getHistWithInfo(info: Tuple2[Int, Int]) = { 351 val selected = hist.filter(_.info.equals(info)) 352 require(selected.length == 1) 353 selected(0) 354 } 355 def autoConnectFrom(that: AllFoldedHistories) = { 356 require(this.hist.length <= that.hist.length) 357 for (h <- this.hist) { 358 h := that.getHistWithInfo(h.info) 359 } 360 } 361 def update(ghv: Vec[Bool], ptr: CGHPtr, shift: Int, taken: Bool): AllFoldedHistories = { 362 val res = WireInit(this) 363 for (i <- 0 until this.hist.length) { 364 res.hist(i) := this.hist(i).update(ghv, ptr, shift, taken) 365 } 366 res 367 } 368 def update(afhob: AllAheadFoldedHistoryOldestBits, lastBrNumOH: UInt, shift: Int, taken: Bool): AllFoldedHistories = { 369 val res = WireInit(this) 370 for (i <- 0 until this.hist.length) { 371 val fh = this.hist(i) 372 if (fh.need_oldest_bits) { 373 val info = fh.info 374 val selectedAfhob = afhob.getObWithInfo(info) 375 val ob = selectedAfhob.getRealOb(lastBrNumOH) 376 res.hist(i) := this.hist(i).update(ob, shift, taken) 377 } else { 378 val dumb = Wire(Vec(numBr, Bool())) // not needed 379 dumb := DontCare 380 res.hist(i) := this.hist(i).update(dumb, shift, taken) 381 } 382 } 383 res 384 } 385 386 def display(cond: Bool) = { 387 for (h <- hist) { 388 XSDebug(cond, p"hist len ${h.len}, folded len ${h.compLen}, value ${Binary(h.folded_hist)}\n") 389 } 390 } 391} 392 393class TableAddr(val idxBits: Int, val banks: Int)(implicit p: Parameters) extends XSBundle{ 394 def tagBits = VAddrBits - idxBits - instOffsetBits 395 396 val tag = UInt(tagBits.W) 397 val idx = UInt(idxBits.W) 398 val offset = UInt(instOffsetBits.W) 399 400 def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this) 401 def getTag(x: UInt) = fromUInt(x).tag 402 def getIdx(x: UInt) = fromUInt(x).idx 403 def getBank(x: UInt) = if (banks > 1) getIdx(x)(log2Up(banks) - 1, 0) else 0.U 404 def getBankIdx(x: UInt) = if (banks > 1) getIdx(x)(idxBits - 1, log2Up(banks)) else getIdx(x) 405} 406 407trait BasicPrediction extends HasXSParameter { 408 def cfiIndex: ValidUndirectioned[UInt] 409 def target(pc: UInt): UInt 410 def lastBrPosOH: Vec[Bool] 411 def brTaken: Bool 412 def shouldShiftVec: Vec[Bool] 413 def fallThruError: Bool 414} 415 416// selectByTaken selects some data according to takenMask 417// allTargets should be in a Vec, like [taken0, taken1, ..., not taken, not hit] 418object selectByTaken { 419 def apply[T <: Data](takenMask: Vec[Bool], hit: Bool, allTargets: Vec[T]): T = { 420 val selVecOH = 421 takenMask.zipWithIndex.map { case (t, i) => !takenMask.take(i).fold(false.B)(_ || _) && t && hit } :+ 422 (!takenMask.asUInt.orR && hit) :+ !hit 423 Mux1H(selVecOH, allTargets) 424 } 425} 426 427class FullBranchPrediction(implicit p: Parameters) extends XSBundle with HasBPUConst with BasicPrediction { 428 val br_taken_mask = Vec(numBr, Bool()) 429 430 val slot_valids = Vec(totalSlot, Bool()) 431 432 val targets = Vec(totalSlot, UInt(VAddrBits.W)) 433 val jalr_target = UInt(VAddrBits.W) // special path for indirect predictors 434 val offsets = Vec(totalSlot, UInt(log2Ceil(PredictWidth).W)) 435 val fallThroughAddr = UInt(VAddrBits.W) 436 val fallThroughErr = Bool() 437 438 val is_jal = Bool() 439 val is_jalr = Bool() 440 val is_call = Bool() 441 val is_ret = Bool() 442 val last_may_be_rvi_call = Bool() 443 val is_br_sharing = Bool() 444 445 // val call_is_rvc = Bool() 446 val hit = Bool() 447 448 val predCycle = if (!env.FPGAPlatform) Some(UInt(64.W)) else None 449 450 def br_slot_valids = slot_valids.init 451 def tail_slot_valid = slot_valids.last 452 453 def br_valids = { 454 VecInit(br_slot_valids :+ (tail_slot_valid && is_br_sharing)) 455 } 456 457 def taken_mask_on_slot = { 458 VecInit( 459 (br_slot_valids zip br_taken_mask.init).map{ case (t, v) => t && v } :+ ( 460 tail_slot_valid && ( 461 is_br_sharing && br_taken_mask.last || !is_br_sharing 462 ) 463 ) 464 ) 465 } 466 467 def real_slot_taken_mask(): Vec[Bool] = { 468 VecInit(taken_mask_on_slot.map(_ && hit)) 469 } 470 471 // len numBr 472 def real_br_taken_mask(): Vec[Bool] = { 473 VecInit( 474 taken_mask_on_slot.map(_ && hit).init :+ 475 (br_taken_mask.last && tail_slot_valid && is_br_sharing && hit) 476 ) 477 } 478 479 // the vec indicating if ghr should shift on each branch 480 def shouldShiftVec = 481 VecInit(br_valids.zipWithIndex.map{ case (v, i) => 482 v && !real_br_taken_mask.take(i).reduceOption(_||_).getOrElse(false.B)}) 483 484 def lastBrPosOH = 485 VecInit((!hit || !br_valids.reduce(_||_)) +: // not hit or no brs in entry 486 (0 until numBr).map(i => 487 br_valids(i) && 488 !real_br_taken_mask.take(i).reduceOption(_||_).getOrElse(false.B) && // no brs taken in front it 489 (real_br_taken_mask()(i) || !br_valids.drop(i+1).reduceOption(_||_).getOrElse(false.B)) && // no brs behind it 490 hit 491 ) 492 ) 493 494 def brTaken = (br_valids zip br_taken_mask).map{ case (a, b) => a && b && hit}.reduce(_||_) 495 496 def target(pc: UInt): UInt = { 497 selectByTaken(taken_mask_on_slot, hit, allTarget(pc)) 498 } 499 500 // allTarget return a Vec of all possible target of a BP stage 501 // in the following order: [taken_target0, taken_target1, ..., fallThroughAddr, not hit (plus fetch width)] 502 // 503 // This exposes internal targets for timing optimization, 504 // since usually targets are generated quicker than taken 505 def allTarget(pc: UInt): Vec[UInt] = { 506 VecInit(targets :+ fallThroughAddr :+ (pc + (FetchWidth * 4).U)) 507 } 508 509 def fallThruError: Bool = hit && fallThroughErr 510 511 def hit_taken_on_jmp = 512 !real_slot_taken_mask().init.reduce(_||_) && 513 real_slot_taken_mask().last && !is_br_sharing 514 def hit_taken_on_call = hit_taken_on_jmp && is_call 515 def hit_taken_on_ret = hit_taken_on_jmp && is_ret 516 def hit_taken_on_jalr = hit_taken_on_jmp && is_jalr 517 518 def cfiIndex = { 519 val cfiIndex = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 520 cfiIndex.valid := real_slot_taken_mask().asUInt.orR 521 // when no takens, set cfiIndex to PredictWidth-1 522 cfiIndex.bits := 523 ParallelPriorityMux(real_slot_taken_mask(), offsets) | 524 Fill(log2Ceil(PredictWidth), (!real_slot_taken_mask().asUInt.orR).asUInt) 525 cfiIndex 526 } 527 528 def taken = br_taken_mask.reduce(_||_) || slot_valids.last // || (is_jal || is_jalr) 529 530 def fromFtbEntry( 531 entry: FTBEntry, 532 pc: UInt, 533 last_stage_pc: Option[Tuple2[UInt, Bool]] = None, 534 last_stage_entry: Option[Tuple2[FTBEntry, Bool]] = None 535 ) = { 536 slot_valids := entry.brSlots.map(_.valid) :+ entry.tailSlot.valid 537 targets := entry.getTargetVec(pc, last_stage_pc) // Use previous stage pc for better timing 538 jalr_target := targets.last 539 offsets := entry.getOffsetVec 540 is_jal := entry.tailSlot.valid && entry.isJal 541 is_jalr := entry.tailSlot.valid && entry.isJalr 542 is_call := entry.tailSlot.valid && entry.isCall 543 is_ret := entry.tailSlot.valid && entry.isRet 544 last_may_be_rvi_call := entry.last_may_be_rvi_call 545 is_br_sharing := entry.tailSlot.valid && entry.tailSlot.sharing 546 predCycle.map(_ := GTimer()) 547 548 val startLower = Cat(0.U(1.W), pc(instOffsetBits+log2Ceil(PredictWidth)-1, instOffsetBits)) 549 val endLowerwithCarry = Cat(entry.carry, entry.pftAddr) 550 fallThroughErr := startLower >= endLowerwithCarry 551 fallThroughAddr := Mux(fallThroughErr, pc + (FetchWidth * 4).U, entry.getFallThrough(pc, last_stage_entry)) 552 } 553 554 def display(cond: Bool): Unit = { 555 XSDebug(cond, p"[taken_mask] ${Binary(br_taken_mask.asUInt)} [hit] $hit\n") 556 } 557} 558 559class SpeculativeInfo(implicit p: Parameters) extends XSBundle 560 with HasBPUConst with BPUUtils { 561 val folded_hist = new AllFoldedHistories(foldedGHistInfos) 562 val afhob = new AllAheadFoldedHistoryOldestBits(foldedGHistInfos) 563 val lastBrNumOH = UInt((numBr+1).W) 564 val histPtr = new CGHPtr 565 val ssp = UInt(log2Up(RasSize).W) 566 val sctr = UInt(log2Up(RasCtrSize).W) 567 val TOSW = new RASPtr 568 val TOSR = new RASPtr 569 val NOS = new RASPtr 570 val topAddr = UInt(VAddrBits.W) 571} 572 573class BranchPredictionBundle(implicit p: Parameters) extends XSBundle 574 with HasBPUConst with BPUUtils { 575 val pc = Vec(numDup, UInt(VAddrBits.W)) 576 val valid = Vec(numDup, Bool()) 577 val hasRedirect = Vec(numDup, Bool()) 578 val ftq_idx = new FtqPtr 579 val full_pred = Vec(numDup, new FullBranchPrediction) 580 581 582 def target(pc: UInt) = VecInit(full_pred.map(_.target(pc))) 583 def targets(pc: Vec[UInt]) = VecInit(pc.zipWithIndex.map{case (pc, idx) => full_pred(idx).target(pc)}) 584 def allTargets(pc: Vec[UInt]) = VecInit(pc.zipWithIndex.map{case (pc, idx) => full_pred(idx).allTarget(pc)}) 585 def cfiIndex = VecInit(full_pred.map(_.cfiIndex)) 586 def lastBrPosOH = VecInit(full_pred.map(_.lastBrPosOH)) 587 def brTaken = VecInit(full_pred.map(_.brTaken)) 588 def shouldShiftVec = VecInit(full_pred.map(_.shouldShiftVec)) 589 def fallThruError = VecInit(full_pred.map(_.fallThruError)) 590 591 def taken = VecInit(cfiIndex.map(_.valid)) 592 593 def getTarget = targets(pc) 594 def getAllTargets = allTargets(pc) 595 596 def display(cond: Bool): Unit = { 597 XSDebug(cond, p"[pc] ${Hexadecimal(pc(0))}\n") 598 full_pred(0).display(cond) 599 } 600} 601 602class BranchPredictionResp(implicit p: Parameters) extends XSBundle with HasBPUConst { 603 // val valids = Vec(3, Bool()) 604 val s1 = new BranchPredictionBundle 605 val s2 = new BranchPredictionBundle 606 val s3 = new BranchPredictionBundle 607 608 val last_stage_meta = UInt(MaxMetaLength.W) 609 val last_stage_spec_info = new Ftq_Redirect_SRAMEntry 610 val last_stage_ftb_entry = new FTBEntry 611 612 val topdown_info = new FrontendTopDownBundle 613 614 def selectedResp ={ 615 val res = 616 PriorityMux(Seq( 617 ((s3.valid(3) && s3.hasRedirect(3)) -> s3), 618 ((s2.valid(3) && s2.hasRedirect(3)) -> s2), 619 (s1.valid(3) -> s1) 620 )) 621 res 622 } 623 def selectedRespIdxForFtq = 624 PriorityMux(Seq( 625 ((s3.valid(3) && s3.hasRedirect(3)) -> BP_S3), 626 ((s2.valid(3) && s2.hasRedirect(3)) -> BP_S2), 627 (s1.valid(3) -> BP_S1) 628 )) 629 def lastStage = s3 630} 631 632class BpuToFtqBundle(implicit p: Parameters) extends BranchPredictionResp {} 633 634class BranchPredictionUpdate(implicit p: Parameters) extends XSBundle with HasBPUConst { 635 val pc = UInt(VAddrBits.W) 636 val spec_info = new SpeculativeInfo 637 val ftb_entry = new FTBEntry() 638 639 val cfi_idx = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 640 val br_taken_mask = Vec(numBr, Bool()) 641 val br_committed = Vec(numBr, Bool()) // High only when br valid && br committed 642 val jmp_taken = Bool() 643 val mispred_mask = Vec(numBr+1, Bool()) 644 val pred_hit = Bool() 645 val false_hit = Bool() 646 val new_br_insert_pos = Vec(numBr, Bool()) 647 val old_entry = Bool() 648 val meta = UInt(MaxMetaLength.W) 649 val full_target = UInt(VAddrBits.W) 650 val from_stage = UInt(2.W) 651 val ghist = UInt(HistoryLength.W) 652 653 def is_jal = ftb_entry.tailSlot.valid && ftb_entry.isJal 654 def is_jalr = ftb_entry.tailSlot.valid && ftb_entry.isJalr 655 def is_call = ftb_entry.tailSlot.valid && ftb_entry.isCall 656 def is_ret = ftb_entry.tailSlot.valid && ftb_entry.isRet 657 658 def is_call_taken = is_call && jmp_taken && cfi_idx.valid && cfi_idx.bits === ftb_entry.tailSlot.offset 659 def is_ret_taken = is_ret && jmp_taken && cfi_idx.valid && cfi_idx.bits === ftb_entry.tailSlot.offset 660 661 def display(cond: Bool) = { 662 XSDebug(cond, p"-----------BranchPredictionUpdate-----------\n") 663 XSDebug(cond, p"[mispred_mask] ${Binary(mispred_mask.asUInt)} [false_hit] $false_hit\n") 664 XSDebug(cond, p"[new_br_insert_pos] ${Binary(new_br_insert_pos.asUInt)}\n") 665 XSDebug(cond, p"--------------------------------------------\n") 666 } 667} 668 669class BranchPredictionRedirect(implicit p: Parameters) extends Redirect with HasBPUConst { 670 // override def toPrintable: Printable = { 671 // p"-----------BranchPredictionRedirect----------- " + 672 // p"-----------cfiUpdate----------- " + 673 // p"[pc] ${Hexadecimal(cfiUpdate.pc)} " + 674 // p"[predTaken] ${cfiUpdate.predTaken}, [taken] ${cfiUpdate.taken}, [isMisPred] ${cfiUpdate.isMisPred} " + 675 // p"[target] ${Hexadecimal(cfiUpdate.target)} " + 676 // p"------------------------------- " + 677 // p"[robPtr] f=${robIdx.flag} v=${robIdx.value} " + 678 // p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} " + 679 // p"[ftqOffset] ${ftqOffset} " + 680 // p"[level] ${level}, [interrupt] ${interrupt} " + 681 // p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value} " + 682 // p"[stFtqOffset] ${stFtqOffset} " + 683 // p"\n" 684 685 // } 686 687 // TODO: backend should pass topdown signals here 688 // must not change its parent since BPU has used asTypeOf(this type) from its parent class 689 require(isInstanceOf[Redirect]) 690 val BTBMissBubble = Bool() 691 def ControlRedirectBubble = debugIsCtrl 692 // if mispred br not in ftb, count as BTB miss 693 def ControlBTBMissBubble = ControlRedirectBubble && !cfiUpdate.br_hit && !cfiUpdate.jr_hit 694 def TAGEMissBubble = ControlRedirectBubble && cfiUpdate.br_hit && !cfiUpdate.sc_hit 695 def SCMissBubble = ControlRedirectBubble && cfiUpdate.br_hit && cfiUpdate.sc_hit 696 def ITTAGEMissBubble = ControlRedirectBubble && cfiUpdate.jr_hit && !cfiUpdate.pd.isRet 697 def RASMissBubble = ControlRedirectBubble && cfiUpdate.jr_hit && cfiUpdate.pd.isRet 698 def MemVioRedirectBubble = debugIsMemVio 699 def OtherRedirectBubble = !debugIsCtrl && !debugIsMemVio 700 701 def connectRedirect(source: Redirect): Unit = { 702 for ((name, data) <- this.elements) { 703 if (source.elements.contains(name)) { 704 data := source.elements(name) 705 } 706 } 707 } 708 709 def display(cond: Bool): Unit = { 710 XSDebug(cond, p"-----------BranchPredictionRedirect----------- \n") 711 XSDebug(cond, p"-----------cfiUpdate----------- \n") 712 XSDebug(cond, p"[pc] ${Hexadecimal(cfiUpdate.pc)}\n") 713 // XSDebug(cond, p"[hist] ${Binary(cfiUpdate.hist.predHist)}\n") 714 XSDebug(cond, p"[br_hit] ${cfiUpdate.br_hit} [isMisPred] ${cfiUpdate.isMisPred}\n") 715 XSDebug(cond, p"[pred_taken] ${cfiUpdate.predTaken} [taken] ${cfiUpdate.taken} [isMisPred] ${cfiUpdate.isMisPred}\n") 716 XSDebug(cond, p"[target] ${Hexadecimal(cfiUpdate.target)} \n") 717 XSDebug(cond, p"[shift] ${cfiUpdate.shift}\n") 718 XSDebug(cond, p"------------------------------- \n") 719 XSDebug(cond, p"[robPtr] f=${robIdx.flag} v=${robIdx.value}\n") 720 XSDebug(cond, p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} \n") 721 XSDebug(cond, p"[ftqOffset] ${ftqOffset} \n") 722 XSDebug(cond, p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value}\n") 723 XSDebug(cond, p"[stFtqOffset] ${stFtqOffset}\n") 724 XSDebug(cond, p"---------------------------------------------- \n") 725 } 726} 727