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 chipsalliance.rocketchip.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 acf = Vec(PredictWidth, Bool()) 130 val crossPageIPFFix = Vec(PredictWidth, Bool()) 131 val triggered = Vec(PredictWidth, new TriggerCf) 132 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 > 2) { 260 println(f"[warning] update logic of foldest history has two or more levels of xor gates! " + 261 f"histlen:${this.len}, compLen:$compLen, at bit $i") 262 } 263 if (resArr(i).length == 0) { 264 println(f"[error] bits $i is not assigned in folded hist update logic! histlen:${this.len}, compLen:$compLen") 265 } 266 res(i) := resArr(i).foldLeft(false.B)(_^_) 267 } 268 res.asUInt 269 } 270 271 val new_folded_hist = if (need_oldest_bits) { 272 val oldest_bits = ob 273 require(oldest_bits.length == max_update_num) 274 // mask off bits that do not update 275 val oldest_bits_masked = oldest_bits.zipWithIndex.map{ 276 case (ob, i) => ob && (i < num).B 277 } 278 // if a bit does not wrap around, it should not be xored when it exits 279 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))) 280 281 // println(f"old bits pos ${oldest_bits_set.map(_._1)}") 282 283 // only the last bit could be 1, as we have at most one taken branch at a time 284 val newest_bits_masked = VecInit((0 until max_update_num).map(i => taken && ((i+1) == num).B)).asUInt 285 // if a bit does not wrap around, newest bits should not be xored onto it either 286 val newest_bits_set = (0 until max_update_num).map(i => (compLen-1-i, newest_bits_masked(i))) 287 288 // println(f"new bits set ${newest_bits_set.map(_._1)}") 289 // 290 val original_bits_masked = VecInit(folded_hist.asBools.zipWithIndex.map{ 291 case (fb, i) => fb && !(num >= (len-i)).B 292 }) 293 val original_bits_set = (0 until compLen).map(i => (i, original_bits_masked(i))) 294 295 // do xor then shift 296 val xored = bitsets_xor(compLen, Seq(original_bits_set, oldest_bits_set, newest_bits_set)) 297 circular_shift_left(xored, num) 298 } else { 299 // histLen too short to wrap around 300 ((folded_hist << num) | taken)(compLen-1,0) 301 } 302 303 val fh = WireInit(this) 304 fh.folded_hist := new_folded_hist 305 fh 306 } 307} 308 309class AheadFoldedHistoryOldestBits(val len: Int, val max_update_num: Int)(implicit p: Parameters) extends XSBundle { 310 val bits = Vec(max_update_num*2, Bool()) 311 // def info = (len, compLen) 312 def getRealOb(brNumOH: UInt): Vec[Bool] = { 313 val ob = Wire(Vec(max_update_num, Bool())) 314 for (i <- 0 until max_update_num) { 315 ob(i) := Mux1H(brNumOH, bits.drop(i).take(numBr+1)) 316 } 317 ob 318 } 319} 320 321class AllAheadFoldedHistoryOldestBits(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 322 val afhob = MixedVec(gen.filter(t => t._1 > t._2).map{_._1} 323 .toSet.toList.map(l => new AheadFoldedHistoryOldestBits(l, numBr))) // remove duplicates 324 require(gen.toSet.toList.equals(gen)) 325 def getObWithInfo(info: Tuple2[Int, Int]) = { 326 val selected = afhob.filter(_.len == info._1) 327 require(selected.length == 1) 328 selected(0) 329 } 330 def read(ghv: Vec[Bool], ptr: CGHPtr) = { 331 val hisLens = afhob.map(_.len) 332 val bitsToRead = hisLens.flatMap(l => (0 until numBr*2).map(i => l-i-1)).toSet // remove duplicates 333 val bitsWithInfo = bitsToRead.map(pos => (pos, ghv((ptr+(pos+1).U).value))) 334 for (ob <- afhob) { 335 for (i <- 0 until numBr*2) { 336 val pos = ob.len - i - 1 337 val bit_found = bitsWithInfo.filter(_._1 == pos).toList 338 require(bit_found.length == 1) 339 ob.bits(i) := bit_found(0)._2 340 } 341 } 342 } 343} 344 345class AllFoldedHistories(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 346 val hist = MixedVec(gen.map{case (l, cl) => new FoldedHistory(l, cl, numBr)}) 347 // println(gen.mkString) 348 require(gen.toSet.toList.equals(gen)) 349 def getHistWithInfo(info: Tuple2[Int, Int]) = { 350 val selected = hist.filter(_.info.equals(info)) 351 require(selected.length == 1) 352 selected(0) 353 } 354 def autoConnectFrom(that: AllFoldedHistories) = { 355 require(this.hist.length <= that.hist.length) 356 for (h <- this.hist) { 357 h := that.getHistWithInfo(h.info) 358 } 359 } 360 def update(ghv: Vec[Bool], ptr: CGHPtr, shift: Int, taken: Bool): AllFoldedHistories = { 361 val res = WireInit(this) 362 for (i <- 0 until this.hist.length) { 363 res.hist(i) := this.hist(i).update(ghv, ptr, shift, taken) 364 } 365 res 366 } 367 def update(afhob: AllAheadFoldedHistoryOldestBits, lastBrNumOH: UInt, shift: Int, taken: Bool): AllFoldedHistories = { 368 val res = WireInit(this) 369 for (i <- 0 until this.hist.length) { 370 val fh = this.hist(i) 371 if (fh.need_oldest_bits) { 372 val info = fh.info 373 val selectedAfhob = afhob.getObWithInfo(info) 374 val ob = selectedAfhob.getRealOb(lastBrNumOH) 375 res.hist(i) := this.hist(i).update(ob, shift, taken) 376 } else { 377 val dumb = Wire(Vec(numBr, Bool())) // not needed 378 dumb := DontCare 379 res.hist(i) := this.hist(i).update(dumb, shift, taken) 380 } 381 } 382 res 383 } 384 385 def display(cond: Bool) = { 386 for (h <- hist) { 387 XSDebug(cond, p"hist len ${h.len}, folded len ${h.compLen}, value ${Binary(h.folded_hist)}\n") 388 } 389 } 390} 391 392class TableAddr(val idxBits: Int, val banks: Int)(implicit p: Parameters) extends XSBundle{ 393 def tagBits = VAddrBits - idxBits - instOffsetBits 394 395 val tag = UInt(tagBits.W) 396 val idx = UInt(idxBits.W) 397 val offset = UInt(instOffsetBits.W) 398 399 def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this) 400 def getTag(x: UInt) = fromUInt(x).tag 401 def getIdx(x: UInt) = fromUInt(x).idx 402 def getBank(x: UInt) = if (banks > 1) getIdx(x)(log2Up(banks) - 1, 0) else 0.U 403 def getBankIdx(x: UInt) = if (banks > 1) getIdx(x)(idxBits - 1, log2Up(banks)) else getIdx(x) 404} 405 406trait BasicPrediction extends HasXSParameter { 407 def cfiIndex: ValidUndirectioned[UInt] 408 def target(pc: UInt): UInt 409 def lastBrPosOH: Vec[Bool] 410 def brTaken: Bool 411 def shouldShiftVec: Vec[Bool] 412 def fallThruError: Bool 413} 414 415class FullBranchPrediction(implicit p: Parameters) extends XSBundle with HasBPUConst with BasicPrediction { 416 val br_taken_mask = Vec(numBr, Bool()) 417 418 val slot_valids = Vec(totalSlot, Bool()) 419 420 val targets = Vec(totalSlot, UInt(VAddrBits.W)) 421 val jalr_target = UInt(VAddrBits.W) // special path for indirect predictors 422 val offsets = Vec(totalSlot, UInt(log2Ceil(PredictWidth).W)) 423 val fallThroughAddr = UInt(VAddrBits.W) 424 val fallThroughErr = Bool() 425 426 val is_jal = Bool() 427 val is_jalr = Bool() 428 val is_call = Bool() 429 val is_ret = Bool() 430 val last_may_be_rvi_call = Bool() 431 val is_br_sharing = Bool() 432 433 // val call_is_rvc = Bool() 434 val hit = Bool() 435 436 val predCycle = if (!env.FPGAPlatform) Some(UInt(64.W)) else None 437 438 def br_slot_valids = slot_valids.init 439 def tail_slot_valid = slot_valids.last 440 441 def br_valids = { 442 VecInit(br_slot_valids :+ (tail_slot_valid && is_br_sharing)) 443 } 444 445 def taken_mask_on_slot = { 446 VecInit( 447 (br_slot_valids zip br_taken_mask.init).map{ case (t, v) => t && v } :+ ( 448 tail_slot_valid && ( 449 is_br_sharing && br_taken_mask.last || !is_br_sharing 450 ) 451 ) 452 ) 453 } 454 455 def real_slot_taken_mask(): Vec[Bool] = { 456 VecInit(taken_mask_on_slot.map(_ && hit)) 457 } 458 459 // len numBr 460 def real_br_taken_mask(): Vec[Bool] = { 461 VecInit( 462 taken_mask_on_slot.map(_ && hit).init :+ 463 (br_taken_mask.last && tail_slot_valid && is_br_sharing && hit) 464 ) 465 } 466 467 // the vec indicating if ghr should shift on each branch 468 def shouldShiftVec = 469 VecInit(br_valids.zipWithIndex.map{ case (v, i) => 470 v && !real_br_taken_mask.take(i).reduceOption(_||_).getOrElse(false.B)}) 471 472 def lastBrPosOH = 473 VecInit((!hit || !br_valids.reduce(_||_)) +: // not hit or no brs in entry 474 (0 until numBr).map(i => 475 br_valids(i) && 476 !real_br_taken_mask.take(i).reduceOption(_||_).getOrElse(false.B) && // no brs taken in front it 477 (real_br_taken_mask()(i) || !br_valids.drop(i+1).reduceOption(_||_).getOrElse(false.B)) && // no brs behind it 478 hit 479 ) 480 ) 481 482 def brTaken = (br_valids zip br_taken_mask).map{ case (a, b) => a && b && hit}.reduce(_||_) 483 484 def target(pc: UInt): UInt = { 485 val targetVec = targets :+ fallThroughAddr :+ (pc + (FetchWidth * 4).U) 486 val tm = taken_mask_on_slot 487 val selVecOH = 488 tm.zipWithIndex.map{ case (t, i) => !tm.take(i).fold(false.B)(_||_) && t && hit} :+ 489 (!tm.asUInt.orR && hit) :+ !hit 490 Mux1H(selVecOH, targetVec) 491 } 492 493 def fallThruError: Bool = hit && fallThroughErr 494 495 def hit_taken_on_jmp = 496 !real_slot_taken_mask().init.reduce(_||_) && 497 real_slot_taken_mask().last && !is_br_sharing 498 def hit_taken_on_call = hit_taken_on_jmp && is_call 499 def hit_taken_on_ret = hit_taken_on_jmp && is_ret 500 def hit_taken_on_jalr = hit_taken_on_jmp && is_jalr 501 502 def cfiIndex = { 503 val cfiIndex = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 504 cfiIndex.valid := real_slot_taken_mask().asUInt.orR 505 // when no takens, set cfiIndex to PredictWidth-1 506 cfiIndex.bits := 507 ParallelPriorityMux(real_slot_taken_mask(), offsets) | 508 Fill(log2Ceil(PredictWidth), (!real_slot_taken_mask().asUInt.orR).asUInt) 509 cfiIndex 510 } 511 512 def taken = br_taken_mask.reduce(_||_) || slot_valids.last // || (is_jal || is_jalr) 513 514 def fromFtbEntry( 515 entry: FTBEntry, 516 pc: UInt, 517 last_stage_pc: Option[Tuple2[UInt, Bool]] = None, 518 last_stage_entry: Option[Tuple2[FTBEntry, Bool]] = None 519 ) = { 520 slot_valids := entry.brSlots.map(_.valid) :+ entry.tailSlot.valid 521 targets := entry.getTargetVec(pc, last_stage_pc) // Use previous stage pc for better timing 522 jalr_target := targets.last 523 offsets := entry.getOffsetVec 524 is_jal := entry.tailSlot.valid && entry.isJal 525 is_jalr := entry.tailSlot.valid && entry.isJalr 526 is_call := entry.tailSlot.valid && entry.isCall 527 is_ret := entry.tailSlot.valid && entry.isRet 528 last_may_be_rvi_call := entry.last_may_be_rvi_call 529 is_br_sharing := entry.tailSlot.valid && entry.tailSlot.sharing 530 predCycle.map(_ := GTimer()) 531 532 val startLower = Cat(0.U(1.W), pc(instOffsetBits+log2Ceil(PredictWidth)-1, instOffsetBits)) 533 val endLowerwithCarry = Cat(entry.carry, entry.pftAddr) 534 fallThroughErr := startLower >= endLowerwithCarry 535 fallThroughAddr := Mux(fallThroughErr, pc + (FetchWidth * 4).U, entry.getFallThrough(pc, last_stage_entry)) 536 } 537 538 def display(cond: Bool): Unit = { 539 XSDebug(cond, p"[taken_mask] ${Binary(br_taken_mask.asUInt)} [hit] $hit\n") 540 } 541} 542 543class SpeculativeInfo(implicit p: Parameters) extends XSBundle 544 with HasBPUConst with BPUUtils { 545 val folded_hist = new AllFoldedHistories(foldedGHistInfos) 546 val afhob = new AllAheadFoldedHistoryOldestBits(foldedGHistInfos) 547 val lastBrNumOH = UInt((numBr+1).W) 548 val histPtr = new CGHPtr 549 val ssp = UInt(log2Up(RasSize).W) 550 val sctr = UInt(log2Up(RasCtrSize).W) 551 val TOSW = new RASPtr 552 val TOSR = new RASPtr 553 val NOS = new RASPtr 554 val topAddr = UInt(VAddrBits.W) 555} 556 557class BranchPredictionBundle(implicit p: Parameters) extends XSBundle 558 with HasBPUConst with BPUUtils { 559 val pc = Vec(numDup, UInt(VAddrBits.W)) 560 val valid = Vec(numDup, Bool()) 561 val hasRedirect = Vec(numDup, Bool()) 562 val ftq_idx = new FtqPtr 563 val full_pred = Vec(numDup, new FullBranchPrediction) 564 565 566 def target(pc: UInt) = VecInit(full_pred.map(_.target(pc))) 567 def targets(pc: Vec[UInt]) = VecInit(pc.zipWithIndex.map{case (a, i) => full_pred(i).target(a)}) 568 def cfiIndex = VecInit(full_pred.map(_.cfiIndex)) 569 def lastBrPosOH = VecInit(full_pred.map(_.lastBrPosOH)) 570 def brTaken = VecInit(full_pred.map(_.brTaken)) 571 def shouldShiftVec = VecInit(full_pred.map(_.shouldShiftVec)) 572 def fallThruError = VecInit(full_pred.map(_.fallThruError)) 573 574 def taken = VecInit(cfiIndex.map(_.valid)) 575 576 def getTarget = targets(pc) 577 578 def display(cond: Bool): Unit = { 579 XSDebug(cond, p"[pc] ${Hexadecimal(pc(0))}\n") 580 full_pred(0).display(cond) 581 } 582} 583 584class BranchPredictionResp(implicit p: Parameters) extends XSBundle with HasBPUConst { 585 // val valids = Vec(3, Bool()) 586 val s1 = new BranchPredictionBundle 587 val s2 = new BranchPredictionBundle 588 val s3 = new BranchPredictionBundle 589 590 val last_stage_meta = UInt(MaxMetaLength.W) 591 val last_stage_spec_info = new SpeculativeInfo 592 val last_stage_ftb_entry = new FTBEntry 593 594 val topdown_info = new FrontendTopDownBundle 595 596 def selectedResp ={ 597 val res = 598 PriorityMux(Seq( 599 ((s3.valid(3) && s3.hasRedirect(3)) -> s3), 600 ((s2.valid(3) && s2.hasRedirect(3)) -> s2), 601 (s1.valid(3) -> s1) 602 )) 603 res 604 } 605 def selectedRespIdxForFtq = 606 PriorityMux(Seq( 607 ((s3.valid(3) && s3.hasRedirect(3)) -> BP_S3), 608 ((s2.valid(3) && s2.hasRedirect(3)) -> BP_S2), 609 (s1.valid(3) -> BP_S1) 610 )) 611 def lastStage = s3 612} 613 614class BpuToFtqBundle(implicit p: Parameters) extends BranchPredictionResp {} 615 616class BranchPredictionUpdate(implicit p: Parameters) extends XSBundle with HasBPUConst { 617 val pc = UInt(VAddrBits.W) 618 val spec_info = new SpeculativeInfo 619 val ftb_entry = new FTBEntry() 620 621 val cfi_idx = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 622 val br_taken_mask = Vec(numBr, Bool()) 623 val br_committed = Vec(numBr, Bool()) // High only when br valid && br committed 624 val jmp_taken = Bool() 625 val mispred_mask = Vec(numBr+1, Bool()) 626 val pred_hit = Bool() 627 val false_hit = Bool() 628 val new_br_insert_pos = Vec(numBr, Bool()) 629 val old_entry = Bool() 630 val meta = UInt(MaxMetaLength.W) 631 val full_target = UInt(VAddrBits.W) 632 val from_stage = UInt(2.W) 633 val ghist = UInt(HistoryLength.W) 634 635 def is_jal = ftb_entry.tailSlot.valid && ftb_entry.isJal 636 def is_jalr = ftb_entry.tailSlot.valid && ftb_entry.isJalr 637 def is_call = ftb_entry.tailSlot.valid && ftb_entry.isCall 638 def is_ret = ftb_entry.tailSlot.valid && ftb_entry.isRet 639 640 def is_call_taken = is_call && jmp_taken && cfi_idx.valid && cfi_idx.bits === ftb_entry.tailSlot.offset 641 def is_ret_taken = is_ret && jmp_taken && cfi_idx.valid && cfi_idx.bits === ftb_entry.tailSlot.offset 642 643 def display(cond: Bool) = { 644 XSDebug(cond, p"-----------BranchPredictionUpdate-----------\n") 645 XSDebug(cond, p"[mispred_mask] ${Binary(mispred_mask.asUInt)} [false_hit] $false_hit\n") 646 XSDebug(cond, p"[new_br_insert_pos] ${Binary(new_br_insert_pos.asUInt)}\n") 647 XSDebug(cond, p"--------------------------------------------\n") 648 } 649} 650 651class BranchPredictionRedirect(implicit p: Parameters) extends Redirect with HasBPUConst { 652 // override def toPrintable: Printable = { 653 // p"-----------BranchPredictionRedirect----------- " + 654 // p"-----------cfiUpdate----------- " + 655 // p"[pc] ${Hexadecimal(cfiUpdate.pc)} " + 656 // p"[predTaken] ${cfiUpdate.predTaken}, [taken] ${cfiUpdate.taken}, [isMisPred] ${cfiUpdate.isMisPred} " + 657 // p"[target] ${Hexadecimal(cfiUpdate.target)} " + 658 // p"------------------------------- " + 659 // p"[robPtr] f=${robIdx.flag} v=${robIdx.value} " + 660 // p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} " + 661 // p"[ftqOffset] ${ftqOffset} " + 662 // p"[level] ${level}, [interrupt] ${interrupt} " + 663 // p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value} " + 664 // p"[stFtqOffset] ${stFtqOffset} " + 665 // p"\n" 666 667 // } 668 669 // TODO: backend should pass topdown signals here 670 // must not change its parent since BPU has used asTypeOf(this type) from its parent class 671 require(isInstanceOf[Redirect]) 672 val BTBMissBubble = Bool() 673 def ControlRedirectBubble = debugIsCtrl 674 // if mispred br not in ftb, count as BTB miss 675 def ControlBTBMissBubble = ControlRedirectBubble && !cfiUpdate.br_hit && !cfiUpdate.jr_hit 676 def TAGEMissBubble = ControlRedirectBubble && cfiUpdate.br_hit && !cfiUpdate.sc_hit 677 def SCMissBubble = ControlRedirectBubble && cfiUpdate.br_hit && cfiUpdate.sc_hit 678 def ITTAGEMissBubble = ControlRedirectBubble && cfiUpdate.jr_hit && !cfiUpdate.pd.isRet 679 def RASMissBubble = ControlRedirectBubble && cfiUpdate.jr_hit && cfiUpdate.pd.isRet 680 def MemVioRedirectBubble = debugIsMemVio 681 def OtherRedirectBubble = !debugIsCtrl && !debugIsMemVio 682 683 def connectRedirect(source: Redirect): Unit = { 684 for ((name, data) <- this.elements) { 685 if (source.elements.contains(name)) { 686 data := source.elements(name) 687 } 688 } 689 } 690 691 def display(cond: Bool): Unit = { 692 XSDebug(cond, p"-----------BranchPredictionRedirect----------- \n") 693 XSDebug(cond, p"-----------cfiUpdate----------- \n") 694 XSDebug(cond, p"[pc] ${Hexadecimal(cfiUpdate.pc)}\n") 695 // XSDebug(cond, p"[hist] ${Binary(cfiUpdate.hist.predHist)}\n") 696 XSDebug(cond, p"[br_hit] ${cfiUpdate.br_hit} [isMisPred] ${cfiUpdate.isMisPred}\n") 697 XSDebug(cond, p"[pred_taken] ${cfiUpdate.predTaken} [taken] ${cfiUpdate.taken} [isMisPred] ${cfiUpdate.isMisPred}\n") 698 XSDebug(cond, p"[target] ${Hexadecimal(cfiUpdate.target)} \n") 699 XSDebug(cond, p"[shift] ${cfiUpdate.shift}\n") 700 XSDebug(cond, p"------------------------------- \n") 701 XSDebug(cond, p"[robPtr] f=${robIdx.flag} v=${robIdx.value}\n") 702 XSDebug(cond, p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} \n") 703 XSDebug(cond, p"[ftqOffset] ${ftqOffset} \n") 704 XSDebug(cond, p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value}\n") 705 XSDebug(cond, p"[stFtqOffset] ${stFtqOffset}\n") 706 XSDebug(cond, p"---------------------------------------------- \n") 707 } 708} 709