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 xiangshan.cache.mmu.TlbResp 27import xiangshan.backend.fu.PMPRespBundle 28 29import scala.math._ 30import java.util.ResourceBundle.Control 31 32class FrontendTopDownBundle(implicit p: Parameters) extends XSBundle { 33 val reasons = Vec(TopDownCounters.NumStallReasons.id, Bool()) 34 val stallWidth = UInt(log2Ceil(PredictWidth).W) 35} 36 37class FetchRequestBundle(implicit p: Parameters) extends XSBundle with HasICacheParameters { 38 39 //fast path: Timing critical 40 val startAddr = UInt(VAddrBits.W) 41 val nextlineStart = UInt(VAddrBits.W) 42 val nextStartAddr = UInt(VAddrBits.W) 43 //slow path 44 val ftqIdx = new FtqPtr 45 val ftqOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 46 47 val topdown_info = new FrontendTopDownBundle 48 49 def crossCacheline = startAddr(blockOffBits - 1) === 1.U 50 51 def fromFtqPcBundle(b: Ftq_RF_Components) = { 52 this.startAddr := b.startAddr 53 this.nextlineStart := b.nextLineAddr 54 // when (b.fallThruError) { 55 // val nextBlockHigherTemp = Mux(startAddr(log2Ceil(PredictWidth)+instOffsetBits), b.nextLineAddr, b.startAddr) 56 // val nextBlockHigher = nextBlockHigherTemp(VAddrBits-1, log2Ceil(PredictWidth)+instOffsetBits+1) 57 // this.nextStartAddr := 58 // Cat(nextBlockHigher, 59 // startAddr(log2Ceil(PredictWidth)+instOffsetBits) ^ 1.U(1.W), 60 // startAddr(log2Ceil(PredictWidth)+instOffsetBits-1, instOffsetBits), 61 // 0.U(instOffsetBits.W) 62 // ) 63 // } 64 this 65 } 66 override def toPrintable: Printable = { 67 p"[start] ${Hexadecimal(startAddr)} [next] ${Hexadecimal(nextlineStart)}" + 68 p"[tgt] ${Hexadecimal(nextStartAddr)} [ftqIdx] $ftqIdx [jmp] v:${ftqOffset.valid}" + 69 p" offset: ${ftqOffset.bits}\n" 70 } 71} 72 73class FtqICacheInfo(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 74 val startAddr = UInt(VAddrBits.W) 75 val nextlineStart = UInt(VAddrBits.W) 76 val ftqIdx = new FtqPtr 77 def crossCacheline = startAddr(blockOffBits - 1) === 1.U 78 def fromFtqPcBundle(b: Ftq_RF_Components) = { 79 this.startAddr := b.startAddr 80 this.nextlineStart := b.nextLineAddr 81 this 82 } 83} 84 85class IFUICacheIO(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 86 val icacheReady = Output(Bool()) 87 val resp = Vec(PortNumber, ValidIO(new ICacheMainPipeResp)) 88 val topdownIcacheMiss = Output(Bool()) 89 val topdownItlbMiss = Output(Bool()) 90} 91 92class FtqToICacheRequestBundle(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 93 val pcMemRead = Vec(5, new FtqICacheInfo) 94 val readValid = Vec(5, Bool()) 95 val backendIpf = Bool() 96 val backendIgpf = Bool() 97 val backendIaf = Bool() 98} 99 100 101class PredecodeWritebackBundle(implicit p:Parameters) extends XSBundle { 102 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 103 val pd = Vec(PredictWidth, new PreDecodeInfo) // TODO: redefine Predecode 104 val ftqIdx = new FtqPtr 105 val ftqOffset = UInt(log2Ceil(PredictWidth).W) 106 val misOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 107 val cfiOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 108 val target = UInt(VAddrBits.W) 109 val jalTarget = UInt(VAddrBits.W) 110 val instrRange = Vec(PredictWidth, Bool()) 111} 112 113class mmioCommitRead(implicit p: Parameters) extends XSBundle { 114 val mmioFtqPtr = Output(new FtqPtr) 115 val mmioLastCommit = Input(Bool()) 116} 117 118object ExceptionType { 119 def none : UInt = "b00".U 120 def pf : UInt = "b01".U // instruction page fault 121 def gpf : UInt = "b10".U // instruction guest page fault 122 def af : UInt = "b11".U // instruction access fault 123 def width : Int = 2 124 125 def fromOH(has_pf: Bool, has_gpf: Bool, has_af: Bool): UInt = { 126 assert( 127 PopCount(VecInit(has_pf, has_gpf, has_af)) <= 1.U, 128 "ExceptionType.fromOH receives input that is not one-hot: pf=%d, gpf=%d, af=%d", 129 has_pf, has_gpf, has_af 130 ) 131 // input is at-most-one-hot encoded, so we don't worry about priority here. 132 MuxCase(none, Seq( 133 has_pf -> pf, 134 has_gpf -> gpf, 135 has_af -> af 136 )) 137 } 138 139 // raise pf/gpf/af according to ftq(backend) request 140 def fromFtq(req: FtqToICacheRequestBundle): UInt = { 141 fromOH( 142 req.backendIpf, 143 req.backendIgpf, 144 req.backendIaf 145 ) 146 } 147 148 // raise pf/gpf/af according to itlb response 149 def fromTlbResp(resp: TlbResp, useDup: Int = 0): UInt = { 150 require(useDup >= 0 && useDup < resp.excp.length) 151 // itlb is guaranteed to respond at most one exception 152 fromOH( 153 resp.excp(useDup).pf.instr, 154 resp.excp(useDup).gpf.instr, 155 resp.excp(useDup).af.instr 156 ) 157 } 158 159 // raise af if pmp check failed 160 def fromPMPResp(resp: PMPRespBundle): UInt = { 161 Mux(resp.instr, af, none) 162 } 163 164 // raise af if meta/data array ecc check failed or l2 cache respond with tilelink corrupt 165 /* FIXME: RISC-V Machine ISA v1.13 (draft) introduced a "hardware error" exception, described as: 166 * > A Hardware Error exception is a synchronous exception triggered when corrupted or 167 * > uncorrectable data is accessed explicitly or implicitly by an instruction. In this context, 168 * > "data" encompasses all types of information used within a RISC-V hart. Upon a hardware 169 * > error exception, the xepc register is set to the address of the instruction that attempted to 170 * > access corrupted data, while the xtval register is set either to 0 or to the virtual address 171 * > of an instruction fetch, load, or store that attempted to access corrupted data. The priority 172 * > of Hardware Error exception is implementation-defined, but any given occurrence is 173 * > generally expected to be recognized at the point in the overall priority order at which the 174 * > hardware error is discovered. 175 * Maybe it's better to raise hardware error instead of access fault when ECC check failed. 176 * But it's draft and XiangShan backend does not implement this exception code yet, so we still raise af here. 177 */ 178 def fromECC(enable: Bool, corrupt: Bool): UInt = { 179 Mux(enable && corrupt, af, none) 180 } 181 182 /**Generates exception mux tree 183 * 184 * Exceptions that are further to the left in the parameter list have higher priority 185 * @example 186 * {{{ 187 * val itlb_exception = ExceptionType.fromTlbResp(io.itlb.resp.bits) 188 * // so as pmp_exception, meta_corrupt 189 * // ExceptionType.merge(itlb_exception, pmp_exception, meta_corrupt) is equivalent to: 190 * Mux( 191 * itlb_exception =/= none, 192 * itlb_exception, 193 * Mux(pmp_exception =/= none, pmp_exception, meta_corrupt) 194 * ) 195 * }}} 196 */ 197 def merge(exceptions: UInt*): UInt = { 198// // recursively generate mux tree 199// if (exceptions.length == 1) { 200// require(exceptions.head.getWidth == width) 201// exceptions.head 202// } else { 203// Mux(exceptions.head =/= none, exceptions.head, merge(exceptions.tail: _*)) 204// } 205 // use MuxCase with default 206 exceptions.foreach(e => require(e.getWidth == width)) 207 val mapping = exceptions.init.map(e => (e =/= none) -> e) 208 val default = exceptions.last 209 MuxCase(default, mapping) 210 } 211 212 /**Generates exception mux tree for multi-port exception vectors 213 * 214 * Exceptions that are further to the left in the parameter list have higher priority 215 * @example 216 * {{{ 217 * val itlb_exception = VecInit((0 until PortNumber).map(i => ExceptionType.fromTlbResp(io.itlb(i).resp.bits))) 218 * // so as pmp_exception, meta_corrupt 219 * // ExceptionType.merge(itlb_exception, pmp_exception, meta_corrupt) is equivalent to: 220 * VecInit((0 until PortNumber).map(i => Mux( 221 * itlb_exception(i) =/= none, 222 * itlb_exception(i), 223 * Mux(pmp_exception(i) =/= none, pmp_exception(i), meta_corrupt(i)) 224 * )) 225 * }}} 226 */ 227 def merge(exceptionVecs: Vec[UInt]*): Vec[UInt] = { 228// // recursively generate mux tree 229// if (exceptionVecs.length == 1) { 230// exceptionVecs.head.foreach(e => require(e.getWidth == width)) 231// exceptionVecs.head 232// } else { 233// require(exceptionVecs.head.length == exceptionVecs.last.length) 234// VecInit((exceptionVecs.head zip merge(exceptionVecs.tail: _*)).map{ case (high, low) => 235// Mux(high =/= none, high, low) 236// }) 237// } 238 // merge port-by-port 239 val length = exceptionVecs.head.length 240 exceptionVecs.tail.foreach(vec => require(vec.length == length)) 241 VecInit((0 until length).map{ i => 242 merge(exceptionVecs.map(_(i)): _*) 243 }) 244 } 245} 246 247class FetchToIBuffer(implicit p: Parameters) extends XSBundle { 248 val instrs = Vec(PredictWidth, UInt(32.W)) 249 val valid = UInt(PredictWidth.W) 250 val enqEnable = UInt(PredictWidth.W) 251 val pd = Vec(PredictWidth, new PreDecodeInfo) 252 val foldpc = Vec(PredictWidth, UInt(MemPredPCWidth.W)) 253 val ftqOffset = Vec(PredictWidth, ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 254 val exceptionFromBackend = Vec(PredictWidth, Bool()) 255 val exceptionType = Vec(PredictWidth, UInt(ExceptionType.width.W)) 256 val crossPageIPFFix = Vec(PredictWidth, Bool()) 257 val illegalInstr = Vec(PredictWidth, Bool()) 258 val triggered = Vec(PredictWidth, TriggerAction()) 259 val isLastInFtqEntry = Vec(PredictWidth, Bool()) 260 261 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 262 val ftqPtr = new FtqPtr 263 val topdown_info = new FrontendTopDownBundle 264} 265 266// class BitWiseUInt(val width: Int, val init: UInt) extends Module { 267// val io = IO(new Bundle { 268// val set 269// }) 270// } 271// Move from BPU 272abstract class GlobalHistory(implicit p: Parameters) extends XSBundle with HasBPUConst { 273 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): GlobalHistory 274} 275 276class ShiftingGlobalHistory(implicit p: Parameters) extends GlobalHistory { 277 val predHist = UInt(HistoryLength.W) 278 279 def update(shift: UInt, taken: Bool, hist: UInt = this.predHist): ShiftingGlobalHistory = { 280 val g = Wire(new ShiftingGlobalHistory) 281 g.predHist := (hist << shift) | taken 282 g 283 } 284 285 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): ShiftingGlobalHistory = { 286 require(br_valids.length == numBr) 287 require(real_taken_mask.length == numBr) 288 val last_valid_idx = PriorityMux( 289 br_valids.reverse :+ true.B, 290 (numBr to 0 by -1).map(_.U(log2Ceil(numBr+1).W)) 291 ) 292 val first_taken_idx = PriorityEncoder(false.B +: real_taken_mask) 293 val smaller = Mux(last_valid_idx < first_taken_idx, 294 last_valid_idx, 295 first_taken_idx 296 ) 297 val shift = smaller 298 val taken = real_taken_mask.reduce(_||_) 299 update(shift, taken, this.predHist) 300 } 301 302 // static read 303 def read(n: Int): Bool = predHist.asBools(n) 304 305 final def === (that: ShiftingGlobalHistory): Bool = { 306 predHist === that.predHist 307 } 308 309 final def =/= (that: ShiftingGlobalHistory): Bool = !(this === that) 310} 311 312// circular global history pointer 313class CGHPtr(implicit p: Parameters) extends CircularQueuePtr[CGHPtr]( 314 p => p(XSCoreParamsKey).HistoryLength 315){ 316} 317 318object CGHPtr { 319 def apply(f: Bool, v: UInt)(implicit p: Parameters): CGHPtr = { 320 val ptr = Wire(new CGHPtr) 321 ptr.flag := f 322 ptr.value := v 323 ptr 324 } 325 def inverse(ptr: CGHPtr)(implicit p: Parameters): CGHPtr = { 326 apply(!ptr.flag, ptr.value) 327 } 328} 329 330class CircularGlobalHistory(implicit p: Parameters) extends GlobalHistory { 331 val buffer = Vec(HistoryLength, Bool()) 332 type HistPtr = UInt 333 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): CircularGlobalHistory = { 334 this 335 } 336} 337 338class FoldedHistory(val len: Int, val compLen: Int, val max_update_num: Int)(implicit p: Parameters) 339 extends XSBundle with HasBPUConst { 340 require(compLen >= 1) 341 require(len > 0) 342 // require(folded_len <= len) 343 require(compLen >= max_update_num) 344 val folded_hist = UInt(compLen.W) 345 346 def need_oldest_bits = len > compLen 347 def info = (len, compLen) 348 def oldest_bit_to_get_from_ghr = (0 until max_update_num).map(len - _ - 1) 349 def oldest_bit_pos_in_folded = oldest_bit_to_get_from_ghr map (_ % compLen) 350 def oldest_bit_wrap_around = oldest_bit_to_get_from_ghr map (_ / compLen > 0) 351 def oldest_bit_start = oldest_bit_pos_in_folded.head 352 353 def get_oldest_bits_from_ghr(ghr: Vec[Bool], histPtr: CGHPtr) = { 354 // TODO: wrap inc for histPtr value 355 oldest_bit_to_get_from_ghr.map(i => ghr((histPtr + (i+1).U).value)) 356 } 357 358 def circular_shift_left(src: UInt, shamt: Int) = { 359 val srcLen = src.getWidth 360 val src_doubled = Cat(src, src) 361 val shifted = src_doubled(srcLen*2-1-shamt, srcLen-shamt) 362 shifted 363 } 364 365 // slow path, read bits from ghr 366 def update(ghr: Vec[Bool], histPtr: CGHPtr, num: Int, taken: Bool): FoldedHistory = { 367 val oldest_bits = VecInit(get_oldest_bits_from_ghr(ghr, histPtr)) 368 update(oldest_bits, num, taken) 369 } 370 371 372 // fast path, use pre-read oldest bits 373 def update(ob: Vec[Bool], num: Int, taken: Bool): FoldedHistory = { 374 // do xors for several bitsets at specified bits 375 def bitsets_xor(len: Int, bitsets: Seq[Seq[Tuple2[Int, Bool]]]) = { 376 val res = Wire(Vec(len, Bool())) 377 // println(f"num bitsets: ${bitsets.length}") 378 // println(f"bitsets $bitsets") 379 val resArr = Array.fill(len)(List[Bool]()) 380 for (bs <- bitsets) { 381 for ((n, b) <- bs) { 382 resArr(n) = b :: resArr(n) 383 } 384 } 385 // println(f"${resArr.mkString}") 386 // println(f"histLen: ${this.len}, foldedLen: $folded_len") 387 for (i <- 0 until len) { 388 // println(f"bit[$i], ${resArr(i).mkString}") 389 if (resArr(i).length == 0) { 390 println(f"[error] bits $i is not assigned in folded hist update logic! histlen:${this.len}, compLen:$compLen") 391 } 392 res(i) := resArr(i).foldLeft(false.B)(_^_) 393 } 394 res.asUInt 395 } 396 397 val new_folded_hist = if (need_oldest_bits) { 398 val oldest_bits = ob 399 require(oldest_bits.length == max_update_num) 400 // mask off bits that do not update 401 val oldest_bits_masked = oldest_bits.zipWithIndex.map{ 402 case (ob, i) => ob && (i < num).B 403 } 404 // if a bit does not wrap around, it should not be xored when it exits 405 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))) 406 407 // println(f"old bits pos ${oldest_bits_set.map(_._1)}") 408 409 // only the last bit could be 1, as we have at most one taken branch at a time 410 val newest_bits_masked = VecInit((0 until max_update_num).map(i => taken && ((i+1) == num).B)).asUInt 411 // if a bit does not wrap around, newest bits should not be xored onto it either 412 val newest_bits_set = (0 until max_update_num).map(i => (compLen-1-i, newest_bits_masked(i))) 413 414 // println(f"new bits set ${newest_bits_set.map(_._1)}") 415 // 416 val original_bits_masked = VecInit(folded_hist.asBools.zipWithIndex.map{ 417 case (fb, i) => fb && !(num >= (len-i)).B 418 }) 419 val original_bits_set = (0 until compLen).map(i => (i, original_bits_masked(i))) 420 421 // do xor then shift 422 val xored = bitsets_xor(compLen, Seq(original_bits_set, oldest_bits_set, newest_bits_set)) 423 circular_shift_left(xored, num) 424 } else { 425 // histLen too short to wrap around 426 ((folded_hist << num) | taken)(compLen-1,0) 427 } 428 429 val fh = WireInit(this) 430 fh.folded_hist := new_folded_hist 431 fh 432 } 433} 434 435class AheadFoldedHistoryOldestBits(val len: Int, val max_update_num: Int)(implicit p: Parameters) extends XSBundle { 436 val bits = Vec(max_update_num*2, Bool()) 437 // def info = (len, compLen) 438 def getRealOb(brNumOH: UInt): Vec[Bool] = { 439 val ob = Wire(Vec(max_update_num, Bool())) 440 for (i <- 0 until max_update_num) { 441 ob(i) := Mux1H(brNumOH, bits.drop(i).take(numBr+1)) 442 } 443 ob 444 } 445} 446 447class AllAheadFoldedHistoryOldestBits(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 448 val afhob = MixedVec(gen.filter(t => t._1 > t._2).map{_._1} 449 .toSet.toList.map(l => new AheadFoldedHistoryOldestBits(l, numBr))) // remove duplicates 450 require(gen.toSet.toList.equals(gen)) 451 def getObWithInfo(info: Tuple2[Int, Int]) = { 452 val selected = afhob.filter(_.len == info._1) 453 require(selected.length == 1) 454 selected(0) 455 } 456 def read(ghv: Vec[Bool], ptr: CGHPtr) = { 457 val hisLens = afhob.map(_.len) 458 val bitsToRead = hisLens.flatMap(l => (0 until numBr*2).map(i => l-i-1)).toSet // remove duplicates 459 val bitsWithInfo = bitsToRead.map(pos => (pos, ghv((ptr+(pos+1).U).value))) 460 for (ob <- afhob) { 461 for (i <- 0 until numBr*2) { 462 val pos = ob.len - i - 1 463 val bit_found = bitsWithInfo.filter(_._1 == pos).toList 464 require(bit_found.length == 1) 465 ob.bits(i) := bit_found(0)._2 466 } 467 } 468 } 469} 470 471class AllFoldedHistories(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 472 val hist = MixedVec(gen.map{case (l, cl) => new FoldedHistory(l, cl, numBr)}) 473 // println(gen.mkString) 474 require(gen.toSet.toList.equals(gen)) 475 def getHistWithInfo(info: Tuple2[Int, Int]) = { 476 val selected = hist.filter(_.info.equals(info)) 477 require(selected.length == 1) 478 selected(0) 479 } 480 def autoConnectFrom(that: AllFoldedHistories) = { 481 require(this.hist.length <= that.hist.length) 482 for (h <- this.hist) { 483 h := that.getHistWithInfo(h.info) 484 } 485 } 486 def update(ghv: Vec[Bool], ptr: CGHPtr, shift: Int, taken: Bool): AllFoldedHistories = { 487 val res = WireInit(this) 488 for (i <- 0 until this.hist.length) { 489 res.hist(i) := this.hist(i).update(ghv, ptr, shift, taken) 490 } 491 res 492 } 493 def update(afhob: AllAheadFoldedHistoryOldestBits, lastBrNumOH: UInt, shift: Int, taken: Bool): AllFoldedHistories = { 494 val res = WireInit(this) 495 for (i <- 0 until this.hist.length) { 496 val fh = this.hist(i) 497 if (fh.need_oldest_bits) { 498 val info = fh.info 499 val selectedAfhob = afhob.getObWithInfo(info) 500 val ob = selectedAfhob.getRealOb(lastBrNumOH) 501 res.hist(i) := this.hist(i).update(ob, shift, taken) 502 } else { 503 val dumb = Wire(Vec(numBr, Bool())) // not needed 504 dumb := DontCare 505 res.hist(i) := this.hist(i).update(dumb, shift, taken) 506 } 507 } 508 res 509 } 510 511 def display(cond: Bool) = { 512 for (h <- hist) { 513 XSDebug(cond, p"hist len ${h.len}, folded len ${h.compLen}, value ${Binary(h.folded_hist)}\n") 514 } 515 } 516} 517 518class TableAddr(val idxBits: Int, val banks: Int)(implicit p: Parameters) extends XSBundle{ 519 def tagBits = VAddrBits - idxBits - instOffsetBits 520 521 val tag = UInt(tagBits.W) 522 val idx = UInt(idxBits.W) 523 val offset = UInt(instOffsetBits.W) 524 525 def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this) 526 def getTag(x: UInt) = fromUInt(x).tag 527 def getIdx(x: UInt) = fromUInt(x).idx 528 def getBank(x: UInt) = if (banks > 1) getIdx(x)(log2Up(banks) - 1, 0) else 0.U 529 def getBankIdx(x: UInt) = if (banks > 1) getIdx(x)(idxBits - 1, log2Up(banks)) else getIdx(x) 530} 531 532trait BasicPrediction extends HasXSParameter { 533 def cfiIndex: ValidUndirectioned[UInt] 534 def target(pc: UInt): UInt 535 def lastBrPosOH: Vec[Bool] 536 def brTaken: Bool 537 def shouldShiftVec: Vec[Bool] 538 def fallThruError: Bool 539} 540 541// selectByTaken selects some data according to takenMask 542// allTargets should be in a Vec, like [taken0, taken1, ..., not taken, not hit] 543object selectByTaken { 544 def apply[T <: Data](takenMask: Vec[Bool], hit: Bool, allTargets: Vec[T]): T = { 545 val selVecOH = 546 takenMask.zipWithIndex.map { case (t, i) => !takenMask.take(i).fold(false.B)(_ || _) && t && hit } :+ 547 (!takenMask.asUInt.orR && hit) :+ !hit 548 Mux1H(selVecOH, allTargets) 549 } 550} 551 552class FullBranchPrediction(val isNotS3: Boolean)(implicit p: Parameters) extends XSBundle with HasBPUConst with BasicPrediction { 553 val br_taken_mask = Vec(numBr, Bool()) 554 555 val slot_valids = Vec(totalSlot, Bool()) 556 557 val targets = Vec(totalSlot, UInt(VAddrBits.W)) 558 val jalr_target = UInt(VAddrBits.W) // special path for indirect predictors 559 val offsets = Vec(totalSlot, UInt(log2Ceil(PredictWidth).W)) 560 val fallThroughAddr = UInt(VAddrBits.W) 561 val fallThroughErr = Bool() 562 val multiHit = Bool() 563 564 val is_jal = Bool() 565 val is_jalr = Bool() 566 val is_call = Bool() 567 val is_ret = Bool() 568 val last_may_be_rvi_call = Bool() 569 val is_br_sharing = Bool() 570 571 // val call_is_rvc = Bool() 572 val hit = Bool() 573 574 val predCycle = if (!env.FPGAPlatform) Some(UInt(64.W)) else None 575 576 def br_slot_valids = slot_valids.init 577 def tail_slot_valid = slot_valids.last 578 579 def br_valids = { 580 VecInit(br_slot_valids :+ (tail_slot_valid && is_br_sharing)) 581 } 582 583 def taken_mask_on_slot = { 584 VecInit( 585 (br_slot_valids zip br_taken_mask.init).map{ case (t, v) => t && v } :+ ( 586 tail_slot_valid && ( 587 is_br_sharing && br_taken_mask.last || !is_br_sharing 588 ) 589 ) 590 ) 591 } 592 593 def real_slot_taken_mask(): Vec[Bool] = { 594 VecInit(taken_mask_on_slot.map(_ && hit)) 595 } 596 597 // len numBr 598 def real_br_taken_mask(): Vec[Bool] = { 599 VecInit( 600 taken_mask_on_slot.map(_ && hit).init :+ 601 (br_taken_mask.last && tail_slot_valid && is_br_sharing && hit) 602 ) 603 } 604 605 // the vec indicating if ghr should shift on each branch 606 def shouldShiftVec = 607 VecInit(br_valids.zipWithIndex.map{ case (v, i) => 608 v && hit && !real_br_taken_mask().take(i).reduceOption(_||_).getOrElse(false.B)}) 609 610 def lastBrPosOH = 611 VecInit((!hit || !br_valids.reduce(_||_)) +: // not hit or no brs in entry 612 (0 until numBr).map(i => 613 br_valids(i) && 614 !real_br_taken_mask().take(i).reduceOption(_||_).getOrElse(false.B) && // no brs taken in front it 615 (real_br_taken_mask()(i) || !br_valids.drop(i+1).reduceOption(_||_).getOrElse(false.B)) && // no brs behind it 616 hit 617 ) 618 ) 619 620 def brTaken = (br_valids zip br_taken_mask).map{ case (a, b) => a && b && hit}.reduce(_||_) 621 622 def target(pc: UInt): UInt = { 623 if (isNotS3){ 624 selectByTaken(taken_mask_on_slot, hit, allTarget(pc)) 625 }else { 626 selectByTaken(taken_mask_on_slot, hit && !fallThroughErr, allTarget(pc)) 627 } 628 } 629 630 // allTarget return a Vec of all possible target of a BP stage 631 // in the following order: [taken_target0, taken_target1, ..., fallThroughAddr, not hit (plus fetch width)] 632 // 633 // This exposes internal targets for timing optimization, 634 // since usually targets are generated quicker than taken 635 def allTarget(pc: UInt): Vec[UInt] = { 636 VecInit(targets :+ fallThroughAddr :+ (pc + (FetchWidth * 4).U)) 637 } 638 639 def fallThruError: Bool = hit && fallThroughErr 640 def ftbMultiHit: Bool = hit && multiHit 641 642 def hit_taken_on_jmp = 643 !real_slot_taken_mask().init.reduce(_||_) && 644 real_slot_taken_mask().last && !is_br_sharing 645 def hit_taken_on_call = hit_taken_on_jmp && is_call 646 def hit_taken_on_ret = hit_taken_on_jmp && is_ret 647 def hit_taken_on_jalr = hit_taken_on_jmp && is_jalr 648 649 def cfiIndex = { 650 val cfiIndex = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 651 cfiIndex.valid := real_slot_taken_mask().asUInt.orR 652 // when no takens, set cfiIndex to PredictWidth-1 653 cfiIndex.bits := 654 ParallelPriorityMux(real_slot_taken_mask(), offsets) | 655 Fill(log2Ceil(PredictWidth), (!real_slot_taken_mask().asUInt.orR).asUInt) 656 cfiIndex 657 } 658 659 def taken = br_taken_mask.reduce(_||_) || slot_valids.last // || (is_jal || is_jalr) 660 661 def fromFtbEntry( 662 entry: FTBEntry, 663 pc: UInt, 664 last_stage_pc: Option[Tuple2[UInt, Bool]] = None, 665 last_stage_entry: Option[Tuple2[FTBEntry, Bool]] = None 666 ) = { 667 slot_valids := entry.brSlots.map(_.valid) :+ entry.tailSlot.valid 668 targets := entry.getTargetVec(pc, last_stage_pc) // Use previous stage pc for better timing 669 jalr_target := targets.last 670 offsets := entry.getOffsetVec 671 is_jal := entry.tailSlot.valid && entry.isJal 672 is_jalr := entry.tailSlot.valid && entry.isJalr 673 is_call := entry.tailSlot.valid && entry.isCall 674 is_ret := entry.tailSlot.valid && entry.isRet 675 last_may_be_rvi_call := entry.last_may_be_rvi_call 676 is_br_sharing := entry.tailSlot.valid && entry.tailSlot.sharing 677 predCycle.map(_ := GTimer()) 678 679 val startLower = Cat(0.U(1.W), pc(instOffsetBits+log2Ceil(PredictWidth)-1, instOffsetBits)) 680 val endLowerwithCarry = Cat(entry.carry, entry.pftAddr) 681 fallThroughErr := startLower >= endLowerwithCarry || endLowerwithCarry > (startLower + (PredictWidth).U) 682 fallThroughAddr := Mux(fallThroughErr, pc + (FetchWidth * 4).U, entry.getFallThrough(pc, last_stage_entry)) 683 } 684 685 def display(cond: Bool): Unit = { 686 XSDebug(cond, p"[taken_mask] ${Binary(br_taken_mask.asUInt)} [hit] $hit\n") 687 } 688} 689 690class SpeculativeInfo(implicit p: Parameters) extends XSBundle 691 with HasBPUConst with BPUUtils { 692 val histPtr = new CGHPtr 693 val ssp = UInt(log2Up(RasSize).W) 694 val sctr = UInt(RasCtrSize.W) 695 val TOSW = new RASPtr 696 val TOSR = new RASPtr 697 val NOS = new RASPtr 698 val topAddr = UInt(VAddrBits.W) 699} 700 701// 702class BranchPredictionBundle(val isNotS3: Boolean)(implicit p: Parameters) extends XSBundle 703 with HasBPUConst with BPUUtils { 704 val pc = Vec(numDup, UInt(VAddrBits.W)) 705 val valid = Vec(numDup, Bool()) 706 val hasRedirect = Vec(numDup, Bool()) 707 val ftq_idx = new FtqPtr 708 val full_pred = Vec(numDup, new FullBranchPrediction(isNotS3)) 709 710 711 def target(pc: UInt) = VecInit(full_pred.map(_.target(pc))) 712 def targets(pc: Vec[UInt]) = VecInit(pc.zipWithIndex.map{case (pc, idx) => full_pred(idx).target(pc)}) 713 def allTargets(pc: Vec[UInt]) = VecInit(pc.zipWithIndex.map{case (pc, idx) => full_pred(idx).allTarget(pc)}) 714 def cfiIndex = VecInit(full_pred.map(_.cfiIndex)) 715 def lastBrPosOH = VecInit(full_pred.map(_.lastBrPosOH)) 716 def brTaken = VecInit(full_pred.map(_.brTaken)) 717 def shouldShiftVec = VecInit(full_pred.map(_.shouldShiftVec)) 718 def fallThruError = VecInit(full_pred.map(_.fallThruError)) 719 def ftbMultiHit = VecInit(full_pred.map(_.ftbMultiHit)) 720 721 def taken = VecInit(cfiIndex.map(_.valid)) 722 723 def getTarget = targets(pc) 724 def getAllTargets = allTargets(pc) 725 726 def display(cond: Bool): Unit = { 727 XSDebug(cond, p"[pc] ${Hexadecimal(pc(0))}\n") 728 full_pred(0).display(cond) 729 } 730} 731 732class BranchPredictionResp(implicit p: Parameters) extends XSBundle with HasBPUConst { 733 val s1 = new BranchPredictionBundle(isNotS3 = true) 734 val s2 = new BranchPredictionBundle(isNotS3 = true) 735 val s3 = new BranchPredictionBundle(isNotS3 = false) 736 737 val s1_uftbHit = Bool() 738 val s1_uftbHasIndirect = Bool() 739 val s1_ftbCloseReq = Bool() 740 741 val last_stage_meta = UInt(MaxMetaLength.W) 742 val last_stage_spec_info = new Ftq_Redirect_SRAMEntry 743 val last_stage_ftb_entry = new FTBEntry 744 745 val topdown_info = new FrontendTopDownBundle 746 747 def selectedResp ={ 748 val res = 749 PriorityMux(Seq( 750 ((s3.valid(3) && s3.hasRedirect(3)) -> s3), 751 ((s2.valid(3) && s2.hasRedirect(3)) -> s2), 752 (s1.valid(3) -> s1) 753 )) 754 res 755 } 756 def selectedRespIdxForFtq = 757 PriorityMux(Seq( 758 ((s3.valid(3) && s3.hasRedirect(3)) -> BP_S3), 759 ((s2.valid(3) && s2.hasRedirect(3)) -> BP_S2), 760 (s1.valid(3) -> BP_S1) 761 )) 762 def lastStage = s3 763} 764 765class BpuToFtqBundle(implicit p: Parameters) extends BranchPredictionResp {} 766 767class BranchPredictionUpdate(implicit p: Parameters) extends XSBundle with HasBPUConst { 768 val pc = UInt(VAddrBits.W) 769 val spec_info = new SpeculativeInfo 770 val ftb_entry = new FTBEntry() 771 772 val cfi_idx = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 773 val br_taken_mask = Vec(numBr, Bool()) 774 val br_committed = Vec(numBr, Bool()) // High only when br valid && br committed 775 val jmp_taken = Bool() 776 val mispred_mask = Vec(numBr+1, Bool()) 777 val pred_hit = Bool() 778 val false_hit = Bool() 779 val new_br_insert_pos = Vec(numBr, Bool()) 780 val old_entry = Bool() 781 val meta = UInt(MaxMetaLength.W) 782 val full_target = UInt(VAddrBits.W) 783 val from_stage = UInt(2.W) 784 val ghist = UInt(HistoryLength.W) 785 786 def is_jal = ftb_entry.tailSlot.valid && ftb_entry.isJal 787 def is_jalr = ftb_entry.tailSlot.valid && ftb_entry.isJalr 788 def is_call = ftb_entry.tailSlot.valid && ftb_entry.isCall 789 def is_ret = ftb_entry.tailSlot.valid && ftb_entry.isRet 790 791 def is_call_taken = is_call && jmp_taken && cfi_idx.valid && cfi_idx.bits === ftb_entry.tailSlot.offset 792 def is_ret_taken = is_ret && jmp_taken && cfi_idx.valid && cfi_idx.bits === ftb_entry.tailSlot.offset 793 794 def display(cond: Bool) = { 795 XSDebug(cond, p"-----------BranchPredictionUpdate-----------\n") 796 XSDebug(cond, p"[mispred_mask] ${Binary(mispred_mask.asUInt)} [false_hit] $false_hit\n") 797 XSDebug(cond, p"[new_br_insert_pos] ${Binary(new_br_insert_pos.asUInt)}\n") 798 XSDebug(cond, p"--------------------------------------------\n") 799 } 800} 801 802class BranchPredictionRedirect(implicit p: Parameters) extends Redirect with HasBPUConst { 803 // override def toPrintable: Printable = { 804 // p"-----------BranchPredictionRedirect----------- " + 805 // p"-----------cfiUpdate----------- " + 806 // p"[pc] ${Hexadecimal(cfiUpdate.pc)} " + 807 // p"[predTaken] ${cfiUpdate.predTaken}, [taken] ${cfiUpdate.taken}, [isMisPred] ${cfiUpdate.isMisPred} " + 808 // p"[target] ${Hexadecimal(cfiUpdate.target)} " + 809 // p"------------------------------- " + 810 // p"[robPtr] f=${robIdx.flag} v=${robIdx.value} " + 811 // p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} " + 812 // p"[ftqOffset] ${ftqOffset} " + 813 // p"[level] ${level}, [interrupt] ${interrupt} " + 814 // p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value} " + 815 // p"[stFtqOffset] ${stFtqOffset} " + 816 // p"\n" 817 818 // } 819 820 // TODO: backend should pass topdown signals here 821 // must not change its parent since BPU has used asTypeOf(this type) from its parent class 822 require(isInstanceOf[Redirect]) 823 val BTBMissBubble = Bool() 824 def ControlRedirectBubble = debugIsCtrl 825 // if mispred br not in ftb, count as BTB miss 826 def ControlBTBMissBubble = ControlRedirectBubble && !cfiUpdate.br_hit && !cfiUpdate.jr_hit 827 def TAGEMissBubble = ControlRedirectBubble && cfiUpdate.br_hit && !cfiUpdate.sc_hit 828 def SCMissBubble = ControlRedirectBubble && cfiUpdate.br_hit && cfiUpdate.sc_hit 829 def ITTAGEMissBubble = ControlRedirectBubble && cfiUpdate.jr_hit && !cfiUpdate.pd.isRet 830 def RASMissBubble = ControlRedirectBubble && cfiUpdate.jr_hit && cfiUpdate.pd.isRet 831 def MemVioRedirectBubble = debugIsMemVio 832 def OtherRedirectBubble = !debugIsCtrl && !debugIsMemVio 833 834 def connectRedirect(source: Redirect): Unit = { 835 for ((name, data) <- this.elements) { 836 if (source.elements.contains(name)) { 837 data := source.elements(name) 838 } 839 } 840 } 841 842 def display(cond: Bool): Unit = { 843 XSDebug(cond, p"-----------BranchPredictionRedirect----------- \n") 844 XSDebug(cond, p"-----------cfiUpdate----------- \n") 845 XSDebug(cond, p"[pc] ${Hexadecimal(cfiUpdate.pc)}\n") 846 // XSDebug(cond, p"[hist] ${Binary(cfiUpdate.hist.predHist)}\n") 847 XSDebug(cond, p"[br_hit] ${cfiUpdate.br_hit} [isMisPred] ${cfiUpdate.isMisPred}\n") 848 XSDebug(cond, p"[pred_taken] ${cfiUpdate.predTaken} [taken] ${cfiUpdate.taken} [isMisPred] ${cfiUpdate.isMisPred}\n") 849 XSDebug(cond, p"[target] ${Hexadecimal(cfiUpdate.target)} \n") 850 XSDebug(cond, p"[shift] ${cfiUpdate.shift}\n") 851 XSDebug(cond, p"------------------------------- \n") 852 XSDebug(cond, p"[robPtr] f=${robIdx.flag} v=${robIdx.value}\n") 853 XSDebug(cond, p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} \n") 854 XSDebug(cond, p"[ftqOffset] ${ftqOffset} \n") 855 XSDebug(cond, p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value}\n") 856 XSDebug(cond, p"[stFtqOffset] ${stFtqOffset}\n") 857 XSDebug(cond, p"---------------------------------------------- \n") 858 } 859} 860