109c6f1ddSLingrui98/*************************************************************************************** 2e3da8badSTang Haojin* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC) 3e3da8badSTang Haojin* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 409c6f1ddSLingrui98* Copyright (c) 2020-2021 Peng Cheng Laboratory 509c6f1ddSLingrui98* 609c6f1ddSLingrui98* XiangShan is licensed under Mulan PSL v2. 709c6f1ddSLingrui98* You can use this software according to the terms and conditions of the Mulan PSL v2. 809c6f1ddSLingrui98* You may obtain a copy of Mulan PSL v2 at: 909c6f1ddSLingrui98* http://license.coscl.org.cn/MulanPSL2 1009c6f1ddSLingrui98* 1109c6f1ddSLingrui98* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 1209c6f1ddSLingrui98* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 1309c6f1ddSLingrui98* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 1409c6f1ddSLingrui98* 1509c6f1ddSLingrui98* See the Mulan PSL v2 for more details. 1609c6f1ddSLingrui98***************************************************************************************/ 1709c6f1ddSLingrui98package xiangshan.frontend 1809c6f1ddSLingrui98 198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 2009c6f1ddSLingrui98import chisel3._ 2109c6f1ddSLingrui98import chisel3.util._ 2209c6f1ddSLingrui98import xiangshan._ 2350780602SJeniusimport xiangshan.frontend.icache._ 2409c6f1ddSLingrui98import utils._ 253c02ee8fSwakafaimport utility._ 2688895b11Sxu_zhimport xiangshan.cache.mmu.TlbResp 2788895b11Sxu_zhimport xiangshan.backend.fu.PMPRespBundle 2888895b11Sxu_zh 29c2ad24ebSLingrui98import scala.math._ 30d2b20d1aSTang Haojinimport java.util.ResourceBundle.Control 31d2b20d1aSTang Haojin 32d2b20d1aSTang Haojinclass FrontendTopDownBundle(implicit p: Parameters) extends XSBundle { 33d2b20d1aSTang Haojin val reasons = Vec(TopDownCounters.NumStallReasons.id, Bool()) 34d2b20d1aSTang Haojin val stallWidth = UInt(log2Ceil(PredictWidth).W) 35d2b20d1aSTang Haojin} 3609c6f1ddSLingrui98 37b37e4b45SLingrui98class FetchRequestBundle(implicit p: Parameters) extends XSBundle with HasICacheParameters { 38c5c5edaeSJenius 39c5c5edaeSJenius //fast path: Timing critical 4009c6f1ddSLingrui98 val startAddr = UInt(VAddrBits.W) 4134a88126SJinYue val nextlineStart = UInt(VAddrBits.W) 42c5c5edaeSJenius val nextStartAddr = UInt(VAddrBits.W) 43c5c5edaeSJenius //slow path 4409c6f1ddSLingrui98 val ftqIdx = new FtqPtr 4509c6f1ddSLingrui98 val ftqOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 4609c6f1ddSLingrui98 47d2b20d1aSTang Haojin val topdown_info = new FrontendTopDownBundle 48d2b20d1aSTang Haojin 496ce52296SJinYue def crossCacheline = startAddr(blockOffBits - 1) === 1.U 506ce52296SJinYue 5109c6f1ddSLingrui98 def fromFtqPcBundle(b: Ftq_RF_Components) = { 5209c6f1ddSLingrui98 this.startAddr := b.startAddr 53b37e4b45SLingrui98 this.nextlineStart := b.nextLineAddr 54b37e4b45SLingrui98 when (b.fallThruError) { 55fd3aa057SYuandongliang val nextBlockHigherTemp = Mux(startAddr(log2Ceil(PredictWidth)+instOffsetBits), b.nextLineAddr, b.startAddr) 56b37e4b45SLingrui98 val nextBlockHigher = nextBlockHigherTemp(VAddrBits-1, log2Ceil(PredictWidth)+instOffsetBits+1) 57b37e4b45SLingrui98 this.nextStartAddr := 58b37e4b45SLingrui98 Cat(nextBlockHigher, 59b37e4b45SLingrui98 startAddr(log2Ceil(PredictWidth)+instOffsetBits) ^ 1.U(1.W), 60b37e4b45SLingrui98 startAddr(log2Ceil(PredictWidth)+instOffsetBits-1, instOffsetBits), 61b37e4b45SLingrui98 0.U(instOffsetBits.W) 62b37e4b45SLingrui98 ) 6309c6f1ddSLingrui98 } 6409c6f1ddSLingrui98 this 6509c6f1ddSLingrui98 } 6609c6f1ddSLingrui98 override def toPrintable: Printable = { 67b37e4b45SLingrui98 p"[start] ${Hexadecimal(startAddr)} [next] ${Hexadecimal(nextlineStart)}" + 68b37e4b45SLingrui98 p"[tgt] ${Hexadecimal(nextStartAddr)} [ftqIdx] $ftqIdx [jmp] v:${ftqOffset.valid}" + 6909c6f1ddSLingrui98 p" offset: ${ftqOffset.bits}\n" 7009c6f1ddSLingrui98 } 7109c6f1ddSLingrui98} 7209c6f1ddSLingrui98 73f22cf846SJeniusclass FtqICacheInfo(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 74c5c5edaeSJenius val startAddr = UInt(VAddrBits.W) 75c5c5edaeSJenius val nextlineStart = UInt(VAddrBits.W) 76b92f8445Sssszwic val ftqIdx = new FtqPtr 77c5c5edaeSJenius def crossCacheline = startAddr(blockOffBits - 1) === 1.U 78b004fa13SJenius def fromFtqPcBundle(b: Ftq_RF_Components) = { 79b004fa13SJenius this.startAddr := b.startAddr 80b004fa13SJenius this.nextlineStart := b.nextLineAddr 81b004fa13SJenius this 82b004fa13SJenius } 83f22cf846SJenius} 84f22cf846SJenius 8550780602SJeniusclass IFUICacheIO(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 8650780602SJenius val icacheReady = Output(Bool()) 8750780602SJenius val resp = Vec(PortNumber, ValidIO(new ICacheMainPipeResp)) 88d2b20d1aSTang Haojin val topdownIcacheMiss = Output(Bool()) 89d2b20d1aSTang Haojin val topdownItlbMiss = Output(Bool()) 9050780602SJenius} 9150780602SJenius 92f22cf846SJeniusclass FtqToICacheRequestBundle(implicit p: Parameters)extends XSBundle with HasICacheParameters{ 93f56177cbSJenius val pcMemRead = Vec(5, new FtqICacheInfo) 94dc270d3bSJenius val readValid = Vec(5, Bool()) 95c5c5edaeSJenius} 96c5c5edaeSJenius 97c5c5edaeSJenius 9809c6f1ddSLingrui98class PredecodeWritebackBundle(implicit p:Parameters) extends XSBundle { 9909c6f1ddSLingrui98 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 10009c6f1ddSLingrui98 val pd = Vec(PredictWidth, new PreDecodeInfo) // TODO: redefine Predecode 10109c6f1ddSLingrui98 val ftqIdx = new FtqPtr 10209c6f1ddSLingrui98 val ftqOffset = UInt(log2Ceil(PredictWidth).W) 10309c6f1ddSLingrui98 val misOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 10409c6f1ddSLingrui98 val cfiOffset = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 10509c6f1ddSLingrui98 val target = UInt(VAddrBits.W) 10609c6f1ddSLingrui98 val jalTarget = UInt(VAddrBits.W) 10709c6f1ddSLingrui98 val instrRange = Vec(PredictWidth, Bool()) 10809c6f1ddSLingrui98} 10909c6f1ddSLingrui98 1101d1e6d4dSJeniusclass mmioCommitRead(implicit p: Parameters) extends XSBundle { 1111d1e6d4dSJenius val mmioFtqPtr = Output(new FtqPtr) 1121d1e6d4dSJenius val mmioLastCommit = Input(Bool()) 1131d1e6d4dSJenius} 1141d1e6d4dSJenius 1156b46af8dSMuziobject ExceptionType { 11688895b11Sxu_zh def none : UInt = "b00".U 11788895b11Sxu_zh def pf : UInt = "b01".U // instruction page fault 11888895b11Sxu_zh def gpf : UInt = "b10".U // instruction guest page fault 11988895b11Sxu_zh def af : UInt = "b11".U // instruction access fault 12088895b11Sxu_zh def width : Int = 2 12188895b11Sxu_zh 12288895b11Sxu_zh // raise pf/gpf/af according to itlb response 12388895b11Sxu_zh def fromTlbResp(resp: TlbResp, useDup: Int = 0): UInt = { 12488895b11Sxu_zh require(useDup >= 0 && useDup < resp.excp.length) 12588895b11Sxu_zh assert( 12688895b11Sxu_zh PopCount(VecInit(resp.excp(useDup).af.instr, resp.excp(useDup).pf.instr, resp.excp(useDup).gpf.instr)) <= 1.U, 12788895b11Sxu_zh "tlb resp has more than 1 exception, af=%d, pf=%d, gpf=%d", 12888895b11Sxu_zh resp.excp(useDup).af.instr, resp.excp(useDup).pf.instr, resp.excp(useDup).gpf.instr 12988895b11Sxu_zh ) 13088895b11Sxu_zh // itlb is guaranteed to respond at most one exception, so we don't worry about priority here. 13188895b11Sxu_zh MuxCase(none, Seq( 13288895b11Sxu_zh resp.excp(useDup).pf.instr -> pf, 13388895b11Sxu_zh resp.excp(useDup).gpf.instr -> gpf, 13488895b11Sxu_zh resp.excp(useDup).af.instr -> af 13588895b11Sxu_zh )) 13688895b11Sxu_zh } 13788895b11Sxu_zh 13888895b11Sxu_zh // raise af if pmp check failed 13988895b11Sxu_zh def fromPMPResp(resp: PMPRespBundle): UInt = { 14088895b11Sxu_zh Mux(resp.instr, af, none) 14188895b11Sxu_zh } 14288895b11Sxu_zh 14388895b11Sxu_zh // raise af if meta/data array ecc check failed or l2 cache respond with tilelink corrupt 144*f80535c3Sxu_zh /* FIXME: RISC-V Machine ISA v1.13 (draft) introduced a "hardware error" exception, described as: 145*f80535c3Sxu_zh * > A Hardware Error exception is a synchronous exception triggered when corrupted or 146*f80535c3Sxu_zh * > uncorrectable data is accessed explicitly or implicitly by an instruction. In this context, 147*f80535c3Sxu_zh * > "data" encompasses all types of information used within a RISC-V hart. Upon a hardware 148*f80535c3Sxu_zh * > error exception, the xepc register is set to the address of the instruction that attempted to 149*f80535c3Sxu_zh * > access corrupted data, while the xtval register is set either to 0 or to the virtual address 150*f80535c3Sxu_zh * > of an instruction fetch, load, or store that attempted to access corrupted data. The priority 151*f80535c3Sxu_zh * > of Hardware Error exception is implementation-defined, but any given occurrence is 152*f80535c3Sxu_zh * > generally expected to be recognized at the point in the overall priority order at which the 153*f80535c3Sxu_zh * > hardware error is discovered. 154*f80535c3Sxu_zh * Maybe it's better to raise hardware error instead of access fault when ECC check failed. 155*f80535c3Sxu_zh * But it's draft and XiangShan backend does not implement this exception code yet, so we still raise af here. 156*f80535c3Sxu_zh */ 157*f80535c3Sxu_zh def fromECC(enable: Bool, corrupt: Bool): UInt = { 158*f80535c3Sxu_zh Mux(enable && corrupt, af, none) 15988895b11Sxu_zh } 16088895b11Sxu_zh 16188895b11Sxu_zh /**Generates exception mux tree 16288895b11Sxu_zh * 16388895b11Sxu_zh * Exceptions that are further to the left in the parameter list have higher priority 16488895b11Sxu_zh * @example 16588895b11Sxu_zh * {{{ 16688895b11Sxu_zh * val itlb_exception = ExceptionType.fromTlbResp(io.itlb.resp.bits) 16788895b11Sxu_zh * // so as pmp_exception, meta_corrupt 16888895b11Sxu_zh * // ExceptionType.merge(itlb_exception, pmp_exception, meta_corrupt) is equivalent to: 16988895b11Sxu_zh * Mux( 17088895b11Sxu_zh * itlb_exception =/= none, 17188895b11Sxu_zh * itlb_exception, 17288895b11Sxu_zh * Mux(pmp_exception =/= none, pmp_exception, meta_corrupt) 17388895b11Sxu_zh * ) 17488895b11Sxu_zh * }}} 17588895b11Sxu_zh */ 17688895b11Sxu_zh def merge(exceptions: UInt*): UInt = { 17788895b11Sxu_zh// // recursively generate mux tree 17888895b11Sxu_zh// if (exceptions.length == 1) { 17988895b11Sxu_zh// require(exceptions.head.getWidth == width) 18088895b11Sxu_zh// exceptions.head 18188895b11Sxu_zh// } else { 18288895b11Sxu_zh// Mux(exceptions.head =/= none, exceptions.head, merge(exceptions.tail: _*)) 18388895b11Sxu_zh// } 18488895b11Sxu_zh // use MuxCase with default 18588895b11Sxu_zh exceptions.foreach(e => require(e.getWidth == width)) 18688895b11Sxu_zh val mapping = exceptions.init.map(e => (e =/= none) -> e) 18788895b11Sxu_zh val default = exceptions.last 18888895b11Sxu_zh MuxCase(default, mapping) 18988895b11Sxu_zh } 19088895b11Sxu_zh 19188895b11Sxu_zh /**Generates exception mux tree for multi-port exception vectors 19288895b11Sxu_zh * 19388895b11Sxu_zh * Exceptions that are further to the left in the parameter list have higher priority 19488895b11Sxu_zh * @example 19588895b11Sxu_zh * {{{ 19688895b11Sxu_zh * val itlb_exception = VecInit((0 until PortNumber).map(i => ExceptionType.fromTlbResp(io.itlb(i).resp.bits))) 19788895b11Sxu_zh * // so as pmp_exception, meta_corrupt 19888895b11Sxu_zh * // ExceptionType.merge(itlb_exception, pmp_exception, meta_corrupt) is equivalent to: 19988895b11Sxu_zh * VecInit((0 until PortNumber).map(i => Mux( 20088895b11Sxu_zh * itlb_exception(i) =/= none, 20188895b11Sxu_zh * itlb_exception(i), 20288895b11Sxu_zh * Mux(pmp_exception(i) =/= none, pmp_exception(i), meta_corrupt(i)) 20388895b11Sxu_zh * )) 20488895b11Sxu_zh * }}} 20588895b11Sxu_zh */ 20688895b11Sxu_zh def merge(exceptionVecs: Vec[UInt]*): Vec[UInt] = { 20788895b11Sxu_zh// // recursively generate mux tree 20888895b11Sxu_zh// if (exceptionVecs.length == 1) { 20988895b11Sxu_zh// exceptionVecs.head.foreach(e => require(e.getWidth == width)) 21088895b11Sxu_zh// exceptionVecs.head 21188895b11Sxu_zh// } else { 21288895b11Sxu_zh// require(exceptionVecs.head.length == exceptionVecs.last.length) 21388895b11Sxu_zh// VecInit((exceptionVecs.head zip merge(exceptionVecs.tail: _*)).map{ case (high, low) => 21488895b11Sxu_zh// Mux(high =/= none, high, low) 21588895b11Sxu_zh// }) 21688895b11Sxu_zh// } 21788895b11Sxu_zh // merge port-by-port 21888895b11Sxu_zh val length = exceptionVecs.head.length 21988895b11Sxu_zh exceptionVecs.tail.foreach(vec => require(vec.length == length)) 22088895b11Sxu_zh VecInit((0 until length).map{ i => 22188895b11Sxu_zh merge(exceptionVecs.map(_(i)): _*) 22288895b11Sxu_zh }) 22388895b11Sxu_zh } 2246b46af8dSMuzi} 2256b46af8dSMuzi 22609c6f1ddSLingrui98class FetchToIBuffer(implicit p: Parameters) extends XSBundle { 22709c6f1ddSLingrui98 val instrs = Vec(PredictWidth, UInt(32.W)) 22809c6f1ddSLingrui98 val valid = UInt(PredictWidth.W) 2292a3050c2SJay val enqEnable = UInt(PredictWidth.W) 23009c6f1ddSLingrui98 val pd = Vec(PredictWidth, new PreDecodeInfo) 23109c6f1ddSLingrui98 val pc = Vec(PredictWidth, UInt(VAddrBits.W)) 23209c6f1ddSLingrui98 val foldpc = Vec(PredictWidth, UInt(MemPredPCWidth.W)) 23309c6f1ddSLingrui98 val ftqPtr = new FtqPtr 23409c6f1ddSLingrui98 val ftqOffset = Vec(PredictWidth, ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 2356b46af8dSMuzi val exceptionType = Vec(PredictWidth, UInt(ExceptionType.width.W)) 23609c6f1ddSLingrui98 val crossPageIPFFix = Vec(PredictWidth, Bool()) 23772951335SLi Qianruo val triggered = Vec(PredictWidth, new TriggerCf) 238d2b20d1aSTang Haojin val topdown_info = new FrontendTopDownBundle 23909c6f1ddSLingrui98} 24009c6f1ddSLingrui98 241c2ad24ebSLingrui98// class BitWiseUInt(val width: Int, val init: UInt) extends Module { 242c2ad24ebSLingrui98// val io = IO(new Bundle { 243c2ad24ebSLingrui98// val set 244c2ad24ebSLingrui98// }) 245c2ad24ebSLingrui98// } 24609c6f1ddSLingrui98// Move from BPU 247c2ad24ebSLingrui98abstract class GlobalHistory(implicit p: Parameters) extends XSBundle with HasBPUConst { 248c2ad24ebSLingrui98 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): GlobalHistory 249c2ad24ebSLingrui98} 250c2ad24ebSLingrui98 251c2ad24ebSLingrui98class ShiftingGlobalHistory(implicit p: Parameters) extends GlobalHistory { 25209c6f1ddSLingrui98 val predHist = UInt(HistoryLength.W) 25309c6f1ddSLingrui98 254c2ad24ebSLingrui98 def update(shift: UInt, taken: Bool, hist: UInt = this.predHist): ShiftingGlobalHistory = { 255c2ad24ebSLingrui98 val g = Wire(new ShiftingGlobalHistory) 25609c6f1ddSLingrui98 g.predHist := (hist << shift) | taken 25709c6f1ddSLingrui98 g 25809c6f1ddSLingrui98 } 25909c6f1ddSLingrui98 260c2ad24ebSLingrui98 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): ShiftingGlobalHistory = { 261eeb5ff92SLingrui98 require(br_valids.length == numBr) 262eeb5ff92SLingrui98 require(real_taken_mask.length == numBr) 263eeb5ff92SLingrui98 val last_valid_idx = PriorityMux( 264eeb5ff92SLingrui98 br_valids.reverse :+ true.B, 265eeb5ff92SLingrui98 (numBr to 0 by -1).map(_.U(log2Ceil(numBr+1).W)) 266eeb5ff92SLingrui98 ) 267eeb5ff92SLingrui98 val first_taken_idx = PriorityEncoder(false.B +: real_taken_mask) 268eeb5ff92SLingrui98 val smaller = Mux(last_valid_idx < first_taken_idx, 269eeb5ff92SLingrui98 last_valid_idx, 270eeb5ff92SLingrui98 first_taken_idx 271eeb5ff92SLingrui98 ) 272eeb5ff92SLingrui98 val shift = smaller 273eeb5ff92SLingrui98 val taken = real_taken_mask.reduce(_||_) 274eeb5ff92SLingrui98 update(shift, taken, this.predHist) 275eeb5ff92SLingrui98 } 276eeb5ff92SLingrui98 277c2ad24ebSLingrui98 // static read 278935edac4STang Haojin def read(n: Int): Bool = predHist.asBools(n) 279c2ad24ebSLingrui98 280c2ad24ebSLingrui98 final def === (that: ShiftingGlobalHistory): Bool = { 28109c6f1ddSLingrui98 predHist === that.predHist 28209c6f1ddSLingrui98 } 28309c6f1ddSLingrui98 284c2ad24ebSLingrui98 final def =/= (that: ShiftingGlobalHistory): Bool = !(this === that) 285c2ad24ebSLingrui98} 28609c6f1ddSLingrui98 287c2ad24ebSLingrui98// circular global history pointer 288c2ad24ebSLingrui98class CGHPtr(implicit p: Parameters) extends CircularQueuePtr[CGHPtr]( 289c2ad24ebSLingrui98 p => p(XSCoreParamsKey).HistoryLength 290c2ad24ebSLingrui98){ 291c2ad24ebSLingrui98} 292c7fabd05SSteve Gou 293c7fabd05SSteve Gouobject CGHPtr { 294c7fabd05SSteve Gou def apply(f: Bool, v: UInt)(implicit p: Parameters): CGHPtr = { 295c7fabd05SSteve Gou val ptr = Wire(new CGHPtr) 296c7fabd05SSteve Gou ptr.flag := f 297c7fabd05SSteve Gou ptr.value := v 298c7fabd05SSteve Gou ptr 299c7fabd05SSteve Gou } 300c7fabd05SSteve Gou def inverse(ptr: CGHPtr)(implicit p: Parameters): CGHPtr = { 301c7fabd05SSteve Gou apply(!ptr.flag, ptr.value) 302c7fabd05SSteve Gou } 303c7fabd05SSteve Gou} 304c7fabd05SSteve Gou 305c2ad24ebSLingrui98class CircularGlobalHistory(implicit p: Parameters) extends GlobalHistory { 306c2ad24ebSLingrui98 val buffer = Vec(HistoryLength, Bool()) 307c2ad24ebSLingrui98 type HistPtr = UInt 308c2ad24ebSLingrui98 def update(br_valids: Vec[Bool], real_taken_mask: Vec[Bool]): CircularGlobalHistory = { 309c2ad24ebSLingrui98 this 310c2ad24ebSLingrui98 } 311c2ad24ebSLingrui98} 312c2ad24ebSLingrui98 313dd6c0695SLingrui98class FoldedHistory(val len: Int, val compLen: Int, val max_update_num: Int)(implicit p: Parameters) 314c2ad24ebSLingrui98 extends XSBundle with HasBPUConst { 315dd6c0695SLingrui98 require(compLen >= 1) 316c2ad24ebSLingrui98 require(len > 0) 317c2ad24ebSLingrui98 // require(folded_len <= len) 318dd6c0695SLingrui98 require(compLen >= max_update_num) 319dd6c0695SLingrui98 val folded_hist = UInt(compLen.W) 320dd6c0695SLingrui98 32167402d75SLingrui98 def need_oldest_bits = len > compLen 322dd6c0695SLingrui98 def info = (len, compLen) 323c2ad24ebSLingrui98 def oldest_bit_to_get_from_ghr = (0 until max_update_num).map(len - _ - 1) 324c2ad24ebSLingrui98 def oldest_bit_pos_in_folded = oldest_bit_to_get_from_ghr map (_ % compLen) 325c2ad24ebSLingrui98 def oldest_bit_wrap_around = oldest_bit_to_get_from_ghr map (_ / compLen > 0) 326c2ad24ebSLingrui98 def oldest_bit_start = oldest_bit_pos_in_folded.head 327c2ad24ebSLingrui98 328dd6c0695SLingrui98 def get_oldest_bits_from_ghr(ghr: Vec[Bool], histPtr: CGHPtr) = { 329c2ad24ebSLingrui98 // TODO: wrap inc for histPtr value 330dd6c0695SLingrui98 oldest_bit_to_get_from_ghr.map(i => ghr((histPtr + (i+1).U).value)) 331c2ad24ebSLingrui98 } 332c2ad24ebSLingrui98 333ab890bfeSLingrui98 def circular_shift_left(src: UInt, shamt: Int) = { 334c2ad24ebSLingrui98 val srcLen = src.getWidth 335c2ad24ebSLingrui98 val src_doubled = Cat(src, src) 336ab890bfeSLingrui98 val shifted = src_doubled(srcLen*2-1-shamt, srcLen-shamt) 337ab890bfeSLingrui98 shifted 338c2ad24ebSLingrui98 } 339c2ad24ebSLingrui98 34067402d75SLingrui98 // slow path, read bits from ghr 341ab890bfeSLingrui98 def update(ghr: Vec[Bool], histPtr: CGHPtr, num: Int, taken: Bool): FoldedHistory = { 34267402d75SLingrui98 val oldest_bits = VecInit(get_oldest_bits_from_ghr(ghr, histPtr)) 34367402d75SLingrui98 update(oldest_bits, num, taken) 34467402d75SLingrui98 } 34567402d75SLingrui98 34667402d75SLingrui98 34767402d75SLingrui98 // fast path, use pre-read oldest bits 34867402d75SLingrui98 def update(ob: Vec[Bool], num: Int, taken: Bool): FoldedHistory = { 349c2ad24ebSLingrui98 // do xors for several bitsets at specified bits 350c2ad24ebSLingrui98 def bitsets_xor(len: Int, bitsets: Seq[Seq[Tuple2[Int, Bool]]]) = { 351c2ad24ebSLingrui98 val res = Wire(Vec(len, Bool())) 352c2ad24ebSLingrui98 // println(f"num bitsets: ${bitsets.length}") 353c2ad24ebSLingrui98 // println(f"bitsets $bitsets") 354c2ad24ebSLingrui98 val resArr = Array.fill(len)(List[Bool]()) 355c2ad24ebSLingrui98 for (bs <- bitsets) { 356c2ad24ebSLingrui98 for ((n, b) <- bs) { 357c2ad24ebSLingrui98 resArr(n) = b :: resArr(n) 358c2ad24ebSLingrui98 } 359c2ad24ebSLingrui98 } 360c2ad24ebSLingrui98 // println(f"${resArr.mkString}") 361c2ad24ebSLingrui98 // println(f"histLen: ${this.len}, foldedLen: $folded_len") 362c2ad24ebSLingrui98 for (i <- 0 until len) { 363c2ad24ebSLingrui98 // println(f"bit[$i], ${resArr(i).mkString}") 364c2ad24ebSLingrui98 if (resArr(i).length == 0) { 365dd6c0695SLingrui98 println(f"[error] bits $i is not assigned in folded hist update logic! histlen:${this.len}, compLen:$compLen") 366c2ad24ebSLingrui98 } 367c2ad24ebSLingrui98 res(i) := resArr(i).foldLeft(false.B)(_^_) 368c2ad24ebSLingrui98 } 369c2ad24ebSLingrui98 res.asUInt 370c2ad24ebSLingrui98 } 371c2ad24ebSLingrui98 37267402d75SLingrui98 val new_folded_hist = if (need_oldest_bits) { 37367402d75SLingrui98 val oldest_bits = ob 37467402d75SLingrui98 require(oldest_bits.length == max_update_num) 375c2ad24ebSLingrui98 // mask off bits that do not update 376c2ad24ebSLingrui98 val oldest_bits_masked = oldest_bits.zipWithIndex.map{ 377ab890bfeSLingrui98 case (ob, i) => ob && (i < num).B 378c2ad24ebSLingrui98 } 379c2ad24ebSLingrui98 // if a bit does not wrap around, it should not be xored when it exits 380c2ad24ebSLingrui98 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))) 381c2ad24ebSLingrui98 382c2ad24ebSLingrui98 // println(f"old bits pos ${oldest_bits_set.map(_._1)}") 383c2ad24ebSLingrui98 384c2ad24ebSLingrui98 // only the last bit could be 1, as we have at most one taken branch at a time 385ab890bfeSLingrui98 val newest_bits_masked = VecInit((0 until max_update_num).map(i => taken && ((i+1) == num).B)).asUInt 386c2ad24ebSLingrui98 // if a bit does not wrap around, newest bits should not be xored onto it either 387e992912cSLingrui98 val newest_bits_set = (0 until max_update_num).map(i => (compLen-1-i, newest_bits_masked(i))) 388c2ad24ebSLingrui98 389c2ad24ebSLingrui98 // println(f"new bits set ${newest_bits_set.map(_._1)}") 390c2ad24ebSLingrui98 // 391c2ad24ebSLingrui98 val original_bits_masked = VecInit(folded_hist.asBools.zipWithIndex.map{ 392ab890bfeSLingrui98 case (fb, i) => fb && !(num >= (len-i)).B 393c2ad24ebSLingrui98 }) 394c2ad24ebSLingrui98 val original_bits_set = (0 until compLen).map(i => (i, original_bits_masked(i))) 395c2ad24ebSLingrui98 396c2ad24ebSLingrui98 // do xor then shift 397c2ad24ebSLingrui98 val xored = bitsets_xor(compLen, Seq(original_bits_set, oldest_bits_set, newest_bits_set)) 398ab890bfeSLingrui98 circular_shift_left(xored, num) 39967402d75SLingrui98 } else { 40067402d75SLingrui98 // histLen too short to wrap around 40167402d75SLingrui98 ((folded_hist << num) | taken)(compLen-1,0) 402c2ad24ebSLingrui98 } 40367402d75SLingrui98 404c2ad24ebSLingrui98 val fh = WireInit(this) 405c2ad24ebSLingrui98 fh.folded_hist := new_folded_hist 406c2ad24ebSLingrui98 fh 407c2ad24ebSLingrui98 } 40809c6f1ddSLingrui98} 40909c6f1ddSLingrui98 41067402d75SLingrui98class AheadFoldedHistoryOldestBits(val len: Int, val max_update_num: Int)(implicit p: Parameters) extends XSBundle { 41167402d75SLingrui98 val bits = Vec(max_update_num*2, Bool()) 41267402d75SLingrui98 // def info = (len, compLen) 41367402d75SLingrui98 def getRealOb(brNumOH: UInt): Vec[Bool] = { 41467402d75SLingrui98 val ob = Wire(Vec(max_update_num, Bool())) 41567402d75SLingrui98 for (i <- 0 until max_update_num) { 41667402d75SLingrui98 ob(i) := Mux1H(brNumOH, bits.drop(i).take(numBr+1)) 41767402d75SLingrui98 } 41867402d75SLingrui98 ob 41967402d75SLingrui98 } 42067402d75SLingrui98} 42167402d75SLingrui98 42267402d75SLingrui98class AllAheadFoldedHistoryOldestBits(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 42367402d75SLingrui98 val afhob = MixedVec(gen.filter(t => t._1 > t._2).map{_._1} 42467402d75SLingrui98 .toSet.toList.map(l => new AheadFoldedHistoryOldestBits(l, numBr))) // remove duplicates 42567402d75SLingrui98 require(gen.toSet.toList.equals(gen)) 42667402d75SLingrui98 def getObWithInfo(info: Tuple2[Int, Int]) = { 42767402d75SLingrui98 val selected = afhob.filter(_.len == info._1) 42867402d75SLingrui98 require(selected.length == 1) 42967402d75SLingrui98 selected(0) 43067402d75SLingrui98 } 43167402d75SLingrui98 def read(ghv: Vec[Bool], ptr: CGHPtr) = { 43267402d75SLingrui98 val hisLens = afhob.map(_.len) 43367402d75SLingrui98 val bitsToRead = hisLens.flatMap(l => (0 until numBr*2).map(i => l-i-1)).toSet // remove duplicates 43467402d75SLingrui98 val bitsWithInfo = bitsToRead.map(pos => (pos, ghv((ptr+(pos+1).U).value))) 43567402d75SLingrui98 for (ob <- afhob) { 43667402d75SLingrui98 for (i <- 0 until numBr*2) { 43767402d75SLingrui98 val pos = ob.len - i - 1 43867402d75SLingrui98 val bit_found = bitsWithInfo.filter(_._1 == pos).toList 43967402d75SLingrui98 require(bit_found.length == 1) 44067402d75SLingrui98 ob.bits(i) := bit_found(0)._2 44167402d75SLingrui98 } 44267402d75SLingrui98 } 44367402d75SLingrui98 } 44467402d75SLingrui98} 44567402d75SLingrui98 44667402d75SLingrui98class AllFoldedHistories(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 44767402d75SLingrui98 val hist = MixedVec(gen.map{case (l, cl) => new FoldedHistory(l, cl, numBr)}) 44867402d75SLingrui98 // println(gen.mkString) 44967402d75SLingrui98 require(gen.toSet.toList.equals(gen)) 45067402d75SLingrui98 def getHistWithInfo(info: Tuple2[Int, Int]) = { 45167402d75SLingrui98 val selected = hist.filter(_.info.equals(info)) 45267402d75SLingrui98 require(selected.length == 1) 45367402d75SLingrui98 selected(0) 45467402d75SLingrui98 } 45567402d75SLingrui98 def autoConnectFrom(that: AllFoldedHistories) = { 45667402d75SLingrui98 require(this.hist.length <= that.hist.length) 45767402d75SLingrui98 for (h <- this.hist) { 45867402d75SLingrui98 h := that.getHistWithInfo(h.info) 45967402d75SLingrui98 } 46067402d75SLingrui98 } 46167402d75SLingrui98 def update(ghv: Vec[Bool], ptr: CGHPtr, shift: Int, taken: Bool): AllFoldedHistories = { 46267402d75SLingrui98 val res = WireInit(this) 46367402d75SLingrui98 for (i <- 0 until this.hist.length) { 46467402d75SLingrui98 res.hist(i) := this.hist(i).update(ghv, ptr, shift, taken) 46567402d75SLingrui98 } 46667402d75SLingrui98 res 46767402d75SLingrui98 } 46867402d75SLingrui98 def update(afhob: AllAheadFoldedHistoryOldestBits, lastBrNumOH: UInt, shift: Int, taken: Bool): AllFoldedHistories = { 46967402d75SLingrui98 val res = WireInit(this) 47067402d75SLingrui98 for (i <- 0 until this.hist.length) { 47167402d75SLingrui98 val fh = this.hist(i) 47267402d75SLingrui98 if (fh.need_oldest_bits) { 47367402d75SLingrui98 val info = fh.info 47467402d75SLingrui98 val selectedAfhob = afhob.getObWithInfo(info) 47567402d75SLingrui98 val ob = selectedAfhob.getRealOb(lastBrNumOH) 47667402d75SLingrui98 res.hist(i) := this.hist(i).update(ob, shift, taken) 47767402d75SLingrui98 } else { 47867402d75SLingrui98 val dumb = Wire(Vec(numBr, Bool())) // not needed 47967402d75SLingrui98 dumb := DontCare 48067402d75SLingrui98 res.hist(i) := this.hist(i).update(dumb, shift, taken) 48167402d75SLingrui98 } 48267402d75SLingrui98 } 48367402d75SLingrui98 res 48467402d75SLingrui98 } 48567402d75SLingrui98 48667402d75SLingrui98 def display(cond: Bool) = { 48767402d75SLingrui98 for (h <- hist) { 48867402d75SLingrui98 XSDebug(cond, p"hist len ${h.len}, folded len ${h.compLen}, value ${Binary(h.folded_hist)}\n") 48967402d75SLingrui98 } 49067402d75SLingrui98 } 49167402d75SLingrui98} 49267402d75SLingrui98 49309c6f1ddSLingrui98class TableAddr(val idxBits: Int, val banks: Int)(implicit p: Parameters) extends XSBundle{ 49409c6f1ddSLingrui98 def tagBits = VAddrBits - idxBits - instOffsetBits 49509c6f1ddSLingrui98 49609c6f1ddSLingrui98 val tag = UInt(tagBits.W) 49709c6f1ddSLingrui98 val idx = UInt(idxBits.W) 49809c6f1ddSLingrui98 val offset = UInt(instOffsetBits.W) 49909c6f1ddSLingrui98 50009c6f1ddSLingrui98 def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this) 50109c6f1ddSLingrui98 def getTag(x: UInt) = fromUInt(x).tag 50209c6f1ddSLingrui98 def getIdx(x: UInt) = fromUInt(x).idx 50309c6f1ddSLingrui98 def getBank(x: UInt) = if (banks > 1) getIdx(x)(log2Up(banks) - 1, 0) else 0.U 50409c6f1ddSLingrui98 def getBankIdx(x: UInt) = if (banks > 1) getIdx(x)(idxBits - 1, log2Up(banks)) else getIdx(x) 50509c6f1ddSLingrui98} 506eeb5ff92SLingrui98 507b37e4b45SLingrui98trait BasicPrediction extends HasXSParameter { 508b37e4b45SLingrui98 def cfiIndex: ValidUndirectioned[UInt] 509b37e4b45SLingrui98 def target(pc: UInt): UInt 510b37e4b45SLingrui98 def lastBrPosOH: Vec[Bool] 511b37e4b45SLingrui98 def brTaken: Bool 512b37e4b45SLingrui98 def shouldShiftVec: Vec[Bool] 513b37e4b45SLingrui98 def fallThruError: Bool 514b37e4b45SLingrui98} 515935edac4STang Haojin 516b166c0eaSEaston Man// selectByTaken selects some data according to takenMask 5172bf6e0ecSEaston Man// allTargets should be in a Vec, like [taken0, taken1, ..., not taken, not hit] 518b166c0eaSEaston Manobject selectByTaken { 519b166c0eaSEaston Man def apply[T <: Data](takenMask: Vec[Bool], hit: Bool, allTargets: Vec[T]): T = { 520b166c0eaSEaston Man val selVecOH = 521b166c0eaSEaston Man takenMask.zipWithIndex.map { case (t, i) => !takenMask.take(i).fold(false.B)(_ || _) && t && hit } :+ 522b166c0eaSEaston Man (!takenMask.asUInt.orR && hit) :+ !hit 523b166c0eaSEaston Man Mux1H(selVecOH, allTargets) 524b166c0eaSEaston Man } 525b166c0eaSEaston Man} 526b166c0eaSEaston Man 527b37e4b45SLingrui98class FullBranchPrediction(implicit p: Parameters) extends XSBundle with HasBPUConst with BasicPrediction { 528eeb5ff92SLingrui98 val br_taken_mask = Vec(numBr, Bool()) 52909c6f1ddSLingrui98 530eeb5ff92SLingrui98 val slot_valids = Vec(totalSlot, Bool()) 53109c6f1ddSLingrui98 532eeb5ff92SLingrui98 val targets = Vec(totalSlot, UInt(VAddrBits.W)) 533b30c10d6SLingrui98 val jalr_target = UInt(VAddrBits.W) // special path for indirect predictors 534a229ab6cSLingrui98 val offsets = Vec(totalSlot, UInt(log2Ceil(PredictWidth).W)) 535a229ab6cSLingrui98 val fallThroughAddr = UInt(VAddrBits.W) 536b37e4b45SLingrui98 val fallThroughErr = Bool() 537fd3aa057SYuandongliang val multiHit = Bool() 53809c6f1ddSLingrui98 53909c6f1ddSLingrui98 val is_jal = Bool() 54009c6f1ddSLingrui98 val is_jalr = Bool() 54109c6f1ddSLingrui98 val is_call = Bool() 54209c6f1ddSLingrui98 val is_ret = Bool() 543f4ebc4b2SLingrui98 val last_may_be_rvi_call = Bool() 544eeb5ff92SLingrui98 val is_br_sharing = Bool() 54509c6f1ddSLingrui98 54609c6f1ddSLingrui98 // val call_is_rvc = Bool() 54709c6f1ddSLingrui98 val hit = Bool() 54809c6f1ddSLingrui98 549209a4cafSSteve Gou val predCycle = if (!env.FPGAPlatform) Some(UInt(64.W)) else None 550209a4cafSSteve Gou 551eeb5ff92SLingrui98 def br_slot_valids = slot_valids.init 552eeb5ff92SLingrui98 def tail_slot_valid = slot_valids.last 553eeb5ff92SLingrui98 554eeb5ff92SLingrui98 def br_valids = { 555b37e4b45SLingrui98 VecInit(br_slot_valids :+ (tail_slot_valid && is_br_sharing)) 556eeb5ff92SLingrui98 } 557eeb5ff92SLingrui98 558eeb5ff92SLingrui98 def taken_mask_on_slot = { 559eeb5ff92SLingrui98 VecInit( 560eeb5ff92SLingrui98 (br_slot_valids zip br_taken_mask.init).map{ case (t, v) => t && v } :+ ( 561b30c10d6SLingrui98 tail_slot_valid && ( 562b30c10d6SLingrui98 is_br_sharing && br_taken_mask.last || !is_br_sharing 563b30c10d6SLingrui98 ) 564eeb5ff92SLingrui98 ) 565eeb5ff92SLingrui98 ) 566eeb5ff92SLingrui98 } 567eeb5ff92SLingrui98 568b37e4b45SLingrui98 def real_slot_taken_mask(): Vec[Bool] = { 569b37e4b45SLingrui98 VecInit(taken_mask_on_slot.map(_ && hit)) 570b37e4b45SLingrui98 } 571b37e4b45SLingrui98 572b37e4b45SLingrui98 // len numBr 573b37e4b45SLingrui98 def real_br_taken_mask(): Vec[Bool] = { 574b37e4b45SLingrui98 VecInit( 575b37e4b45SLingrui98 taken_mask_on_slot.map(_ && hit).init :+ 576b37e4b45SLingrui98 (br_taken_mask.last && tail_slot_valid && is_br_sharing && hit) 577b37e4b45SLingrui98 ) 578b37e4b45SLingrui98 } 579b37e4b45SLingrui98 580b37e4b45SLingrui98 // the vec indicating if ghr should shift on each branch 581b37e4b45SLingrui98 def shouldShiftVec = 582b37e4b45SLingrui98 VecInit(br_valids.zipWithIndex.map{ case (v, i) => 583e3da8badSTang Haojin v && !real_br_taken_mask().take(i).reduceOption(_||_).getOrElse(false.B)}) 584b37e4b45SLingrui98 585b37e4b45SLingrui98 def lastBrPosOH = 586b37e4b45SLingrui98 VecInit((!hit || !br_valids.reduce(_||_)) +: // not hit or no brs in entry 587b37e4b45SLingrui98 (0 until numBr).map(i => 588b37e4b45SLingrui98 br_valids(i) && 589e3da8badSTang Haojin !real_br_taken_mask().take(i).reduceOption(_||_).getOrElse(false.B) && // no brs taken in front it 590b37e4b45SLingrui98 (real_br_taken_mask()(i) || !br_valids.drop(i+1).reduceOption(_||_).getOrElse(false.B)) && // no brs behind it 591b37e4b45SLingrui98 hit 592b37e4b45SLingrui98 ) 593b37e4b45SLingrui98 ) 594b37e4b45SLingrui98 59586d9c530SLingrui98 def brTaken = (br_valids zip br_taken_mask).map{ case (a, b) => a && b && hit}.reduce(_||_) 596b37e4b45SLingrui98 597b37e4b45SLingrui98 def target(pc: UInt): UInt = { 598b166c0eaSEaston Man selectByTaken(taken_mask_on_slot, hit, allTarget(pc)) 599b166c0eaSEaston Man } 600b166c0eaSEaston Man 6012bf6e0ecSEaston Man // allTarget return a Vec of all possible target of a BP stage 6022bf6e0ecSEaston Man // in the following order: [taken_target0, taken_target1, ..., fallThroughAddr, not hit (plus fetch width)] 603b166c0eaSEaston Man // 604b166c0eaSEaston Man // This exposes internal targets for timing optimization, 605b166c0eaSEaston Man // since usually targets are generated quicker than taken 606b166c0eaSEaston Man def allTarget(pc: UInt): Vec[UInt] = { 607b166c0eaSEaston Man VecInit(targets :+ fallThroughAddr :+ (pc + (FetchWidth * 4).U)) 608b37e4b45SLingrui98 } 609b37e4b45SLingrui98 610b37e4b45SLingrui98 def fallThruError: Bool = hit && fallThroughErr 611fd3aa057SYuandongliang def ftbMultiHit: Bool = hit && multiHit 612b37e4b45SLingrui98 613b37e4b45SLingrui98 def hit_taken_on_jmp = 614b37e4b45SLingrui98 !real_slot_taken_mask().init.reduce(_||_) && 615b37e4b45SLingrui98 real_slot_taken_mask().last && !is_br_sharing 616b37e4b45SLingrui98 def hit_taken_on_call = hit_taken_on_jmp && is_call 617b37e4b45SLingrui98 def hit_taken_on_ret = hit_taken_on_jmp && is_ret 618b37e4b45SLingrui98 def hit_taken_on_jalr = hit_taken_on_jmp && is_jalr 619b37e4b45SLingrui98 620b37e4b45SLingrui98 def cfiIndex = { 621b37e4b45SLingrui98 val cfiIndex = Wire(ValidUndirectioned(UInt(log2Ceil(PredictWidth).W))) 622b37e4b45SLingrui98 cfiIndex.valid := real_slot_taken_mask().asUInt.orR 623b37e4b45SLingrui98 // when no takens, set cfiIndex to PredictWidth-1 624b37e4b45SLingrui98 cfiIndex.bits := 625b37e4b45SLingrui98 ParallelPriorityMux(real_slot_taken_mask(), offsets) | 626b37e4b45SLingrui98 Fill(log2Ceil(PredictWidth), (!real_slot_taken_mask().asUInt.orR).asUInt) 627b37e4b45SLingrui98 cfiIndex 628b37e4b45SLingrui98 } 629b37e4b45SLingrui98 630eeb5ff92SLingrui98 def taken = br_taken_mask.reduce(_||_) || slot_valids.last // || (is_jal || is_jalr) 63109c6f1ddSLingrui98 63247c003a9SEaston Man def fromFtbEntry( 63347c003a9SEaston Man entry: FTBEntry, 63447c003a9SEaston Man pc: UInt, 63547c003a9SEaston Man last_stage_pc: Option[Tuple2[UInt, Bool]] = None, 63647c003a9SEaston Man last_stage_entry: Option[Tuple2[FTBEntry, Bool]] = None 63747c003a9SEaston Man ) = { 638eeb5ff92SLingrui98 slot_valids := entry.brSlots.map(_.valid) :+ entry.tailSlot.valid 63947c003a9SEaston Man targets := entry.getTargetVec(pc, last_stage_pc) // Use previous stage pc for better timing 640b30c10d6SLingrui98 jalr_target := targets.last 641a229ab6cSLingrui98 offsets := entry.getOffsetVec 642eeb5ff92SLingrui98 is_jal := entry.tailSlot.valid && entry.isJal 643eeb5ff92SLingrui98 is_jalr := entry.tailSlot.valid && entry.isJalr 644eeb5ff92SLingrui98 is_call := entry.tailSlot.valid && entry.isCall 645eeb5ff92SLingrui98 is_ret := entry.tailSlot.valid && entry.isRet 646f4ebc4b2SLingrui98 last_may_be_rvi_call := entry.last_may_be_rvi_call 647eeb5ff92SLingrui98 is_br_sharing := entry.tailSlot.valid && entry.tailSlot.sharing 648209a4cafSSteve Gou predCycle.map(_ := GTimer()) 649a229ab6cSLingrui98 650a60a2901SLingrui98 val startLower = Cat(0.U(1.W), pc(instOffsetBits+log2Ceil(PredictWidth)-1, instOffsetBits)) 651b37e4b45SLingrui98 val endLowerwithCarry = Cat(entry.carry, entry.pftAddr) 652fd3aa057SYuandongliang fallThroughErr := startLower >= endLowerwithCarry || endLowerwithCarry > (startLower + (PredictWidth).U) 65347c003a9SEaston Man fallThroughAddr := Mux(fallThroughErr, pc + (FetchWidth * 4).U, entry.getFallThrough(pc, last_stage_entry)) 654a229ab6cSLingrui98 } 65509c6f1ddSLingrui98 65609c6f1ddSLingrui98 def display(cond: Bool): Unit = { 657eeb5ff92SLingrui98 XSDebug(cond, p"[taken_mask] ${Binary(br_taken_mask.asUInt)} [hit] $hit\n") 65809c6f1ddSLingrui98 } 65909c6f1ddSLingrui98} 66009c6f1ddSLingrui98 661803124a6SLingrui98class SpeculativeInfo(implicit p: Parameters) extends XSBundle 662803124a6SLingrui98 with HasBPUConst with BPUUtils { 663803124a6SLingrui98 val histPtr = new CGHPtr 664c89b4642SGuokai Chen val ssp = UInt(log2Up(RasSize).W) 665deb3a97eSGao-Zeyu val sctr = UInt(RasCtrSize.W) 666c89b4642SGuokai Chen val TOSW = new RASPtr 667c89b4642SGuokai Chen val TOSR = new RASPtr 668c89b4642SGuokai Chen val NOS = new RASPtr 669c89b4642SGuokai Chen val topAddr = UInt(VAddrBits.W) 670803124a6SLingrui98} 671803124a6SLingrui98 672b37e4b45SLingrui98class BranchPredictionBundle(implicit p: Parameters) extends XSBundle 673b37e4b45SLingrui98 with HasBPUConst with BPUUtils { 674adc0b8dfSGuokai Chen val pc = Vec(numDup, UInt(VAddrBits.W)) 675adc0b8dfSGuokai Chen val valid = Vec(numDup, Bool()) 676adc0b8dfSGuokai Chen val hasRedirect = Vec(numDup, Bool()) 67709c6f1ddSLingrui98 val ftq_idx = new FtqPtr 678adc0b8dfSGuokai Chen val full_pred = Vec(numDup, new FullBranchPrediction) 679b37e4b45SLingrui98 68009c6f1ddSLingrui98 681adc0b8dfSGuokai Chen def target(pc: UInt) = VecInit(full_pred.map(_.target(pc))) 682b166c0eaSEaston Man def targets(pc: Vec[UInt]) = VecInit(pc.zipWithIndex.map{case (pc, idx) => full_pred(idx).target(pc)}) 683b166c0eaSEaston Man def allTargets(pc: Vec[UInt]) = VecInit(pc.zipWithIndex.map{case (pc, idx) => full_pred(idx).allTarget(pc)}) 684adc0b8dfSGuokai Chen def cfiIndex = VecInit(full_pred.map(_.cfiIndex)) 685adc0b8dfSGuokai Chen def lastBrPosOH = VecInit(full_pred.map(_.lastBrPosOH)) 686adc0b8dfSGuokai Chen def brTaken = VecInit(full_pred.map(_.brTaken)) 687adc0b8dfSGuokai Chen def shouldShiftVec = VecInit(full_pred.map(_.shouldShiftVec)) 688adc0b8dfSGuokai Chen def fallThruError = VecInit(full_pred.map(_.fallThruError)) 689fd3aa057SYuandongliang def ftbMultiHit = VecInit(full_pred.map(_.ftbMultiHit)) 690eeb5ff92SLingrui98 691adc0b8dfSGuokai Chen def taken = VecInit(cfiIndex.map(_.valid)) 692adc0b8dfSGuokai Chen 693adc0b8dfSGuokai Chen def getTarget = targets(pc) 694b166c0eaSEaston Man def getAllTargets = allTargets(pc) 69509c6f1ddSLingrui98 69609c6f1ddSLingrui98 def display(cond: Bool): Unit = { 697adc0b8dfSGuokai Chen XSDebug(cond, p"[pc] ${Hexadecimal(pc(0))}\n") 698adc0b8dfSGuokai Chen full_pred(0).display(cond) 69909c6f1ddSLingrui98 } 70009c6f1ddSLingrui98} 70109c6f1ddSLingrui98 70209c6f1ddSLingrui98class BranchPredictionResp(implicit p: Parameters) extends XSBundle with HasBPUConst { 703b37e4b45SLingrui98 val s1 = new BranchPredictionBundle 704b37e4b45SLingrui98 val s2 = new BranchPredictionBundle 705cb4f77ceSLingrui98 val s3 = new BranchPredictionBundle 70609c6f1ddSLingrui98 707c4a59f19SYuandongliang val s1_uftbHit = Bool() 708c4a59f19SYuandongliang val s1_uftbHasIndirect = Bool() 709c4a59f19SYuandongliang val s1_ftbCloseReq = Bool() 710c4a59f19SYuandongliang 711c2d1ec7dSLingrui98 val last_stage_meta = UInt(MaxMetaLength.W) 7123711cf36S小造xu_zh val last_stage_spec_info = new Ftq_Redirect_SRAMEntry 713c2d1ec7dSLingrui98 val last_stage_ftb_entry = new FTBEntry 714c2d1ec7dSLingrui98 715d2b20d1aSTang Haojin val topdown_info = new FrontendTopDownBundle 716d2b20d1aSTang Haojin 717b37e4b45SLingrui98 def selectedResp ={ 718b37e4b45SLingrui98 val res = 71909c6f1ddSLingrui98 PriorityMux(Seq( 720adc0b8dfSGuokai Chen ((s3.valid(3) && s3.hasRedirect(3)) -> s3), 721adc0b8dfSGuokai Chen ((s2.valid(3) && s2.hasRedirect(3)) -> s2), 722adc0b8dfSGuokai Chen (s1.valid(3) -> s1) 72309c6f1ddSLingrui98 )) 724b37e4b45SLingrui98 res 725b37e4b45SLingrui98 } 726adc0b8dfSGuokai Chen def selectedRespIdxForFtq = 72709c6f1ddSLingrui98 PriorityMux(Seq( 728adc0b8dfSGuokai Chen ((s3.valid(3) && s3.hasRedirect(3)) -> BP_S3), 729adc0b8dfSGuokai Chen ((s2.valid(3) && s2.hasRedirect(3)) -> BP_S2), 730adc0b8dfSGuokai Chen (s1.valid(3) -> BP_S1) 73109c6f1ddSLingrui98 )) 732cb4f77ceSLingrui98 def lastStage = s3 73309c6f1ddSLingrui98} 73409c6f1ddSLingrui98 735c2d1ec7dSLingrui98class BpuToFtqBundle(implicit p: Parameters) extends BranchPredictionResp {} 73609c6f1ddSLingrui98 737803124a6SLingrui98class BranchPredictionUpdate(implicit p: Parameters) extends XSBundle with HasBPUConst { 738803124a6SLingrui98 val pc = UInt(VAddrBits.W) 739803124a6SLingrui98 val spec_info = new SpeculativeInfo 740803124a6SLingrui98 val ftb_entry = new FTBEntry() 741803124a6SLingrui98 742803124a6SLingrui98 val cfi_idx = ValidUndirectioned(UInt(log2Ceil(PredictWidth).W)) 743803124a6SLingrui98 val br_taken_mask = Vec(numBr, Bool()) 744cc2d1573SEaston Man val br_committed = Vec(numBr, Bool()) // High only when br valid && br committed 745803124a6SLingrui98 val jmp_taken = Bool() 74609c6f1ddSLingrui98 val mispred_mask = Vec(numBr+1, Bool()) 747edc18578SLingrui98 val pred_hit = Bool() 74809c6f1ddSLingrui98 val false_hit = Bool() 74909c6f1ddSLingrui98 val new_br_insert_pos = Vec(numBr, Bool()) 75009c6f1ddSLingrui98 val old_entry = Bool() 75109c6f1ddSLingrui98 val meta = UInt(MaxMetaLength.W) 752abdbe4b7SLingrui98 val full_target = UInt(VAddrBits.W) 753edc18578SLingrui98 val from_stage = UInt(2.W) 75486d9c530SLingrui98 val ghist = UInt(HistoryLength.W) 75509c6f1ddSLingrui98 756803124a6SLingrui98 def is_jal = ftb_entry.tailSlot.valid && ftb_entry.isJal 757803124a6SLingrui98 def is_jalr = ftb_entry.tailSlot.valid && ftb_entry.isJalr 758803124a6SLingrui98 def is_call = ftb_entry.tailSlot.valid && ftb_entry.isCall 759803124a6SLingrui98 def is_ret = ftb_entry.tailSlot.valid && ftb_entry.isRet 760803124a6SLingrui98 761c89b4642SGuokai Chen def is_call_taken = is_call && jmp_taken && cfi_idx.valid && cfi_idx.bits === ftb_entry.tailSlot.offset 762c89b4642SGuokai Chen def is_ret_taken = is_ret && jmp_taken && cfi_idx.valid && cfi_idx.bits === ftb_entry.tailSlot.offset 763c89b4642SGuokai Chen 764803124a6SLingrui98 def display(cond: Bool) = { 76509c6f1ddSLingrui98 XSDebug(cond, p"-----------BranchPredictionUpdate-----------\n") 76609c6f1ddSLingrui98 XSDebug(cond, p"[mispred_mask] ${Binary(mispred_mask.asUInt)} [false_hit] $false_hit\n") 76709c6f1ddSLingrui98 XSDebug(cond, p"[new_br_insert_pos] ${Binary(new_br_insert_pos.asUInt)}\n") 76809c6f1ddSLingrui98 XSDebug(cond, p"--------------------------------------------\n") 76909c6f1ddSLingrui98 } 77009c6f1ddSLingrui98} 77109c6f1ddSLingrui98 77209c6f1ddSLingrui98class BranchPredictionRedirect(implicit p: Parameters) extends Redirect with HasBPUConst { 77309c6f1ddSLingrui98 // override def toPrintable: Printable = { 77409c6f1ddSLingrui98 // p"-----------BranchPredictionRedirect----------- " + 77509c6f1ddSLingrui98 // p"-----------cfiUpdate----------- " + 77609c6f1ddSLingrui98 // p"[pc] ${Hexadecimal(cfiUpdate.pc)} " + 77709c6f1ddSLingrui98 // p"[predTaken] ${cfiUpdate.predTaken}, [taken] ${cfiUpdate.taken}, [isMisPred] ${cfiUpdate.isMisPred} " + 77809c6f1ddSLingrui98 // p"[target] ${Hexadecimal(cfiUpdate.target)} " + 77909c6f1ddSLingrui98 // p"------------------------------- " + 7809aca92b9SYinan Xu // p"[robPtr] f=${robIdx.flag} v=${robIdx.value} " + 78109c6f1ddSLingrui98 // p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} " + 78209c6f1ddSLingrui98 // p"[ftqOffset] ${ftqOffset} " + 78309c6f1ddSLingrui98 // p"[level] ${level}, [interrupt] ${interrupt} " + 78409c6f1ddSLingrui98 // p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value} " + 78509c6f1ddSLingrui98 // p"[stFtqOffset] ${stFtqOffset} " + 78609c6f1ddSLingrui98 // p"\n" 78709c6f1ddSLingrui98 78809c6f1ddSLingrui98 // } 78909c6f1ddSLingrui98 790d2b20d1aSTang Haojin // TODO: backend should pass topdown signals here 791d2b20d1aSTang Haojin // must not change its parent since BPU has used asTypeOf(this type) from its parent class 792d2b20d1aSTang Haojin require(isInstanceOf[Redirect]) 793d2b20d1aSTang Haojin val BTBMissBubble = Bool() 794d2b20d1aSTang Haojin def ControlRedirectBubble = debugIsCtrl 795d2b20d1aSTang Haojin // if mispred br not in ftb, count as BTB miss 796d2b20d1aSTang Haojin def ControlBTBMissBubble = ControlRedirectBubble && !cfiUpdate.br_hit && !cfiUpdate.jr_hit 797d2b20d1aSTang Haojin def TAGEMissBubble = ControlRedirectBubble && cfiUpdate.br_hit && !cfiUpdate.sc_hit 798d2b20d1aSTang Haojin def SCMissBubble = ControlRedirectBubble && cfiUpdate.br_hit && cfiUpdate.sc_hit 799d2b20d1aSTang Haojin def ITTAGEMissBubble = ControlRedirectBubble && cfiUpdate.jr_hit && !cfiUpdate.pd.isRet 800d2b20d1aSTang Haojin def RASMissBubble = ControlRedirectBubble && cfiUpdate.jr_hit && cfiUpdate.pd.isRet 801d2b20d1aSTang Haojin def MemVioRedirectBubble = debugIsMemVio 802d2b20d1aSTang Haojin def OtherRedirectBubble = !debugIsCtrl && !debugIsMemVio 803d2b20d1aSTang Haojin 804d2b20d1aSTang Haojin def connectRedirect(source: Redirect): Unit = { 805d2b20d1aSTang Haojin for ((name, data) <- this.elements) { 806d2b20d1aSTang Haojin if (source.elements.contains(name)) { 807d2b20d1aSTang Haojin data := source.elements(name) 808d2b20d1aSTang Haojin } 809d2b20d1aSTang Haojin } 810d2b20d1aSTang Haojin } 811d2b20d1aSTang Haojin 81209c6f1ddSLingrui98 def display(cond: Bool): Unit = { 81309c6f1ddSLingrui98 XSDebug(cond, p"-----------BranchPredictionRedirect----------- \n") 81409c6f1ddSLingrui98 XSDebug(cond, p"-----------cfiUpdate----------- \n") 81509c6f1ddSLingrui98 XSDebug(cond, p"[pc] ${Hexadecimal(cfiUpdate.pc)}\n") 816c2ad24ebSLingrui98 // XSDebug(cond, p"[hist] ${Binary(cfiUpdate.hist.predHist)}\n") 81709c6f1ddSLingrui98 XSDebug(cond, p"[br_hit] ${cfiUpdate.br_hit} [isMisPred] ${cfiUpdate.isMisPred}\n") 81809c6f1ddSLingrui98 XSDebug(cond, p"[pred_taken] ${cfiUpdate.predTaken} [taken] ${cfiUpdate.taken} [isMisPred] ${cfiUpdate.isMisPred}\n") 81909c6f1ddSLingrui98 XSDebug(cond, p"[target] ${Hexadecimal(cfiUpdate.target)} \n") 82009c6f1ddSLingrui98 XSDebug(cond, p"[shift] ${cfiUpdate.shift}\n") 82109c6f1ddSLingrui98 XSDebug(cond, p"------------------------------- \n") 8229aca92b9SYinan Xu XSDebug(cond, p"[robPtr] f=${robIdx.flag} v=${robIdx.value}\n") 82309c6f1ddSLingrui98 XSDebug(cond, p"[ftqPtr] f=${ftqIdx.flag} v=${ftqIdx.value} \n") 82409c6f1ddSLingrui98 XSDebug(cond, p"[ftqOffset] ${ftqOffset} \n") 82509c6f1ddSLingrui98 XSDebug(cond, p"[stFtqIdx] f=${stFtqIdx.flag} v=${stFtqIdx.value}\n") 82609c6f1ddSLingrui98 XSDebug(cond, p"[stFtqOffset] ${stFtqOffset}\n") 82709c6f1ddSLingrui98 XSDebug(cond, p"---------------------------------------------- \n") 82809c6f1ddSLingrui98 } 82909c6f1ddSLingrui98} 830