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