1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.frontend 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import utils._ 24import utility._ 25import xiangshan.ExceptionNO._ 26 27class IBufPtr(implicit p: Parameters) extends CircularQueuePtr[IBufPtr]( 28 p => p(XSCoreParamsKey).IBufSize 29) { 30} 31 32class IBufInBankPtr(implicit p: Parameters) extends CircularQueuePtr[IBufInBankPtr]( 33 p => p(XSCoreParamsKey).IBufSize / p(XSCoreParamsKey).IBufNBank 34) { 35} 36 37class IBufBankPtr(implicit p: Parameters) extends CircularQueuePtr[IBufBankPtr]( 38 p => p(XSCoreParamsKey).IBufNBank 39) { 40} 41 42class IBufferIO(implicit p: Parameters) extends XSBundle { 43 val flush = Input(Bool()) 44 val ControlRedirect = Input(Bool()) 45 val ControlBTBMissBubble = Input(Bool()) 46 val TAGEMissBubble = Input(Bool()) 47 val SCMissBubble = Input(Bool()) 48 val ITTAGEMissBubble = Input(Bool()) 49 val RASMissBubble = Input(Bool()) 50 val MemVioRedirect = Input(Bool()) 51 val in = Flipped(DecoupledIO(new FetchToIBuffer)) 52 val out = Vec(DecodeWidth, DecoupledIO(new CtrlFlow)) 53 val full = Output(Bool()) 54 val decodeCanAccept = Input(Bool()) 55 val stallReason = new StallReasonIO(DecodeWidth) 56} 57 58class IBufEntry(implicit p: Parameters) extends XSBundle { 59 val inst = UInt(32.W) 60 val pc = UInt(VAddrBits.W) 61 val foldpc = UInt(MemPredPCWidth.W) 62 val pd = new PreDecodeInfo 63 val pred_taken = Bool() 64 val ftqPtr = new FtqPtr 65 val ftqOffset = UInt(log2Ceil(PredictWidth).W) 66 val exceptionType = IBufferExceptionType() 67 val triggered = TriggerAction() 68 69 def fromFetch(fetch: FetchToIBuffer, i: Int): IBufEntry = { 70 inst := fetch.instrs(i) 71 pc := fetch.pc(i) 72 foldpc := fetch.foldpc(i) 73 pd := fetch.pd(i) 74 pred_taken := fetch.ftqOffset(i).valid 75 ftqPtr := fetch.ftqPtr 76 ftqOffset := fetch.ftqOffset(i).bits 77 exceptionType := IBufferExceptionType.cvtFromFetchExcpAndCrossPageAndRVCII( 78 fetch.exceptionType(i), 79 fetch.crossPageIPFFix(i), 80 fetch.illegalInstr(i), 81 ) 82 triggered := fetch.triggered(i) 83 this 84 } 85 86 def toCtrlFlow: CtrlFlow = { 87 val cf = Wire(new CtrlFlow) 88 cf.instr := inst 89 cf.pc := pc 90 cf.foldpc := foldpc 91 cf.exceptionVec := 0.U.asTypeOf(ExceptionVec()) 92 cf.exceptionVec(instrPageFault) := IBufferExceptionType.isPF (this.exceptionType) 93 cf.exceptionVec(instrGuestPageFault) := IBufferExceptionType.isGPF(this.exceptionType) 94 cf.exceptionVec(instrAccessFault) := IBufferExceptionType.isAF (this.exceptionType) 95 cf.exceptionVec(EX_II) := IBufferExceptionType.isRVCII(this.exceptionType) 96 cf.trigger := triggered 97 cf.pd := pd 98 cf.pred_taken := pred_taken 99 cf.crossPageIPFFix := IBufferExceptionType.isCrossPage(this.exceptionType) 100 cf.storeSetHit := DontCare 101 cf.waitForRobIdx := DontCare 102 cf.loadWaitBit := DontCare 103 cf.loadWaitStrict := DontCare 104 cf.ssid := DontCare 105 cf.ftqPtr := ftqPtr 106 cf.ftqOffset := ftqOffset 107 cf 108 } 109 110 object IBufferExceptionType extends NamedUInt(3) { 111 def None = "b000".U 112 def NonCrossPF = "b001".U 113 def NonCrossGPF = "b010".U 114 def NonCrossAF = "b011".U 115 // illegal instruction 116 def rvcII = "b100".U 117 def CrossPF = "b101".U 118 def CrossGPF = "b110".U 119 def CrossAF = "b111".U 120 121 def cvtFromFetchExcpAndCrossPageAndRVCII(fetchExcp: UInt, crossPage: Bool, rvcIll: Bool): UInt = { 122 require( 123 fetchExcp.getWidth == ExceptionType.width, 124 s"The width(${fetchExcp.getWidth}) of fetchExcp should be equal to " + 125 s"the width(${ExceptionType.width}) of frontend.ExceptionType." 126 ) 127 MuxCase(0.U, Seq( 128 crossPage -> Cat(1.U(1.W), fetchExcp), 129 fetchExcp.orR -> fetchExcp, 130 rvcIll -> this.rvcII, 131 )) 132 } 133 134 def isRVCII(uint: UInt): Bool = { 135 this.checkInputWidth(uint) 136 uint(2) && uint(1, 0) === 0.U 137 } 138 139 def isCrossPage(uint: UInt): Bool = { 140 this.checkInputWidth(uint) 141 uint(2) && uint(1, 0) =/= 0.U 142 } 143 144 def isPF (uint: UInt): Bool = uint(1, 0) === this.NonCrossPF (1, 0) 145 def isGPF(uint: UInt): Bool = uint(1, 0) === this.NonCrossGPF(1, 0) 146 def isAF (uint: UInt): Bool = uint(1, 0) === this.NonCrossAF (1, 0) 147 } 148} 149 150class IBuffer(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper with HasPerfEvents { 151 val io = IO(new IBufferIO) 152 153 // io alias 154 private val decodeCanAccept = io.decodeCanAccept 155 156 // Parameter Check 157 private val bankSize = IBufSize / IBufNBank 158 require(IBufSize % IBufNBank == 0, s"IBufNBank should divide IBufSize, IBufNBank: $IBufNBank, IBufSize: $IBufSize") 159 require(IBufNBank >= DecodeWidth, 160 s"IBufNBank should be equal or larger than DecodeWidth, IBufNBank: $IBufNBank, DecodeWidth: $DecodeWidth") 161 162 // IBuffer is organized as raw registers 163 // This is due to IBuffer is a huge queue, read & write port logic should be precisely controlled 164 // . + + E E E - . 165 // . + + E E E - . 166 // . . + E E E - . 167 // . . + E E E E - 168 // As shown above, + means enqueue, - means dequeue, E is current content 169 // When dequeue, read port is organized like a banked FIFO 170 // Dequeue reads no more than 1 entry from each bank sequentially, this can be exploit to reduce area 171 // Enqueue writes cannot benefit from this characteristic unless use a SRAM 172 // For detail see Enqueue and Dequeue below 173 private val ibuf: Vec[IBufEntry] = RegInit(VecInit.fill(IBufSize)(0.U.asTypeOf(new IBufEntry))) 174 private val bankedIBufView: Vec[Vec[IBufEntry]] = VecInit.tabulate(IBufNBank)( 175 bankID => VecInit.tabulate(bankSize)( 176 inBankOffset => ibuf(bankID + inBankOffset * IBufNBank) 177 ) 178 ) 179 180 181 // Bypass wire 182 private val bypassEntries = WireDefault(VecInit.fill(DecodeWidth)(0.U.asTypeOf(Valid(new IBufEntry)))) 183 // Normal read wire 184 private val deqEntries = WireDefault(VecInit.fill(DecodeWidth)(0.U.asTypeOf(Valid(new IBufEntry)))) 185 // Output register 186 private val outputEntries = RegInit(VecInit.fill(DecodeWidth)(0.U.asTypeOf(Valid(new IBufEntry)))) 187 private val outputEntriesValidNum = PriorityMuxDefault(outputEntries.map(_.valid).zip(Seq.range(1, DecodeWidth).map(_.U)).reverse.toSeq, 0.U) 188 189 // Between Bank 190 private val deqBankPtrVec: Vec[IBufBankPtr] = RegInit(VecInit.tabulate(DecodeWidth)(_.U.asTypeOf(new IBufBankPtr))) 191 private val deqBankPtr: IBufBankPtr = deqBankPtrVec(0) 192 private val deqBankPtrVecNext = Wire(deqBankPtrVec.cloneType) 193 // Inside Bank 194 private val deqInBankPtr: Vec[IBufInBankPtr] = RegInit(VecInit.fill(IBufNBank)(0.U.asTypeOf(new IBufInBankPtr))) 195 private val deqInBankPtrNext = Wire(deqInBankPtr.cloneType) 196 197 val deqPtr = RegInit(0.U.asTypeOf(new IBufPtr)) 198 val deqPtrNext = Wire(deqPtr.cloneType) 199 200 val enqPtrVec = RegInit(VecInit.tabulate(PredictWidth)(_.U.asTypeOf(new IBufPtr))) 201 val enqPtr = enqPtrVec(0) 202 203 val numTryEnq = WireDefault(0.U) 204 val numEnq = Mux(io.in.fire, numTryEnq, 0.U) 205 206 // empty and decode can accept insts 207 val useBypass = enqPtr === deqPtr && decodeCanAccept 208 209 // The number of decode accepted insts. 210 // Since decode promises accepting insts in order, use priority encoder to simplify the accumulation. 211 private val numOut = Wire(UInt(log2Ceil(DecodeWidth).W)) 212 private val numDeq = numOut 213 214 // counter current number of valid 215 val numValid = distanceBetween(enqPtr, deqPtr) 216 val numValidAfterDeq = numValid - numDeq 217 // counter next number of valid 218 val numValidNext = numValid + numEnq - numDeq 219 val allowEnq = RegInit(true.B) 220 val numFromFetch = Mux(io.in.valid, PopCount(io.in.bits.enqEnable), 0.U) 221 222 allowEnq := (IBufSize - PredictWidth).U >= numValidNext // Disable when almost full 223 224 val enqOffset = VecInit.tabulate(PredictWidth)(i => PopCount(io.in.bits.valid.asBools.take(i))) 225 val enqData = VecInit.tabulate(PredictWidth)(i => Wire(new IBufEntry).fromFetch(io.in.bits, i)) 226 227 val outputEntriesIsNotFull = !outputEntries(DecodeWidth-1).valid 228 when(decodeCanAccept) { 229 numOut := Mux(numValid >= DecodeWidth.U, DecodeWidth.U, numValid) 230 }.elsewhen(outputEntriesIsNotFull) { 231 numOut := Mux(numValid >= DecodeWidth.U - outputEntriesValidNum, DecodeWidth.U - outputEntriesValidNum, numValid) 232 }.otherwise { 233 numOut := 0.U 234 } 235 val numBypass = Wire(UInt(log2Ceil(DecodeWidth).W)) 236 // when using bypass, bypassed entries do not enqueue 237 when(useBypass) { 238 when(numFromFetch >= DecodeWidth.U) { 239 numTryEnq := numFromFetch - DecodeWidth.U 240 numBypass := DecodeWidth.U 241 } .otherwise { 242 numTryEnq := 0.U 243 numBypass := numFromFetch 244 } 245 } .otherwise { 246 numTryEnq := numFromFetch 247 numBypass := 0.U 248 } 249 250 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 251 // Bypass 252 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 253 bypassEntries.zipWithIndex.foreach { 254 case (entry, idx) => 255 // Select 256 val validOH = Range(0, PredictWidth).map { 257 i => 258 io.in.bits.valid(i) && 259 io.in.bits.enqEnable(i) && 260 enqOffset(i) === idx.asUInt 261 } // Should be OneHot 262 entry.valid := validOH.reduce(_ || _) && io.in.fire && !io.flush 263 entry.bits := Mux1H(validOH, enqData) 264 265 // Debug Assertion 266 XSError(io.in.valid && PopCount(validOH) > 1.asUInt, "validOH is not OneHot") 267 } 268 269 // => Decode Output 270 // clean register output 271 io.out zip outputEntries foreach { 272 case (io, reg) => 273 io.valid := reg.valid 274 io.bits := reg.bits.toCtrlFlow 275 } 276 (outputEntries zip bypassEntries).zipWithIndex.foreach { 277 case ((out, bypass), i) => 278 when(decodeCanAccept) { 279 when(useBypass && io.in.valid) { 280 out := bypass 281 }.otherwise { 282 out := deqEntries(i) 283 } 284 }.elsewhen(outputEntriesIsNotFull){ 285 out.valid := deqEntries(i).valid 286 out.bits := Mux(i.U < outputEntriesValidNum, out.bits, VecInit(deqEntries.take(i + 1).map(_.bits))(i.U - outputEntriesValidNum)) 287 } 288 } 289 290 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 291 // Enqueue 292 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 293 io.in.ready := allowEnq 294 // Data 295 ibuf.zipWithIndex.foreach { 296 case (entry, idx) => { 297 // Select 298 val validOH = Range(0, PredictWidth).map { 299 i => 300 val useBypassMatch = enqOffset(i) >= DecodeWidth.U && 301 enqPtrVec(enqOffset(i) - DecodeWidth.U).value === idx.asUInt 302 val normalMatch = enqPtrVec(enqOffset(i)).value === idx.asUInt 303 val m = Mux(useBypass, useBypassMatch, normalMatch) // when using bypass, bypassed entries do not enqueue 304 305 io.in.bits.valid(i) && io.in.bits.enqEnable(i) && m 306 } // Should be OneHot 307 val wen = validOH.reduce(_ || _) && io.in.fire && !io.flush 308 309 // Write port 310 // Each IBuffer entry has a PredictWidth -> 1 Mux 311 val writeEntry = Mux1H(validOH, enqData) 312 entry := Mux(wen, writeEntry, entry) 313 314 // Debug Assertion 315 XSError(io.in.valid && PopCount(validOH) > 1.asUInt, "validOH is not OneHot") 316 } 317 } 318 // Pointer maintenance 319 when (io.in.fire && !io.flush) { 320 enqPtrVec := VecInit(enqPtrVec.map(_ + numTryEnq)) 321 } 322 323 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 324 // Dequeue 325 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 326 val outputEntriesValidNumNext = Wire(UInt(log2Ceil(DecodeWidth).W)) 327 XSError(outputEntriesValidNumNext > DecodeWidth.U, "Ibuffer: outputEntriesValidNumNext > DecodeWidth.U") 328 val validVec = UIntToMask(outputEntriesValidNumNext(log2Ceil(DecodeWidth) - 1, 0), DecodeWidth) 329 when(decodeCanAccept) { 330 outputEntriesValidNumNext := Mux(useBypass, numBypass, numDeq) 331 }.elsewhen(outputEntriesIsNotFull) { 332 outputEntriesValidNumNext := outputEntriesValidNum + numDeq 333 }.otherwise { 334 outputEntriesValidNumNext := outputEntriesValidNum 335 } 336 // Data 337 // Read port 338 // 2-stage, IBufNBank * (bankSize -> 1) + IBufNBank -> 1 339 // Should be better than IBufSize -> 1 in area, with no significant latency increase 340 private val readStage1: Vec[IBufEntry] = VecInit.tabulate(IBufNBank)( 341 bankID => Mux1H(UIntToOH(deqInBankPtr(bankID).value), bankedIBufView(bankID)) 342 ) 343 for (i <- 0 until DecodeWidth) { 344 deqEntries(i).valid := validVec(i) 345 deqEntries(i).bits := Mux1H(UIntToOH(deqBankPtrVec(i).value), readStage1) 346 } 347 // Pointer maintenance 348 deqBankPtrVecNext := VecInit(deqBankPtrVec.map(_ + numDeq)) 349 deqPtrNext := deqPtr + numDeq 350 deqInBankPtrNext.zip(deqInBankPtr).zipWithIndex.foreach { 351 case ((ptrNext, ptr), idx) => { 352 // validVec[k] == bankValid[deqBankPtr + k] 353 // So bankValid[n] == validVec[n - deqBankPtr] 354 val validIdx = Mux(idx.asUInt >= deqBankPtr.value, 355 idx.asUInt - deqBankPtr.value, 356 ((idx + IBufNBank).asUInt - deqBankPtr.value)(log2Ceil(IBufNBank) - 1, 0) 357 )(log2Ceil(DecodeWidth) - 1, 0) 358 val bankAdvance = numOut > validIdx 359 ptrNext := Mux(bankAdvance , ptr + 1.U, ptr) 360 } 361 } 362 363 // Flush 364 when (io.flush) { 365 allowEnq := true.B 366 enqPtrVec := enqPtrVec.indices.map(_.U.asTypeOf(new IBufPtr)) 367 deqBankPtrVec := deqBankPtrVec.indices.map(_.U.asTypeOf(new IBufBankPtr)) 368 deqInBankPtr := VecInit.fill(IBufNBank)(0.U.asTypeOf(new IBufInBankPtr)) 369 deqPtr := 0.U.asTypeOf(new IBufPtr()) 370 outputEntries.foreach(_.valid := false.B) 371 }.otherwise { 372 deqPtr := deqPtrNext 373 deqInBankPtr := deqInBankPtrNext 374 deqBankPtrVec := deqBankPtrVecNext 375 } 376 io.full := !allowEnq 377 378 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 379 // TopDown 380 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 381 val topdown_stage = RegInit(0.U.asTypeOf(new FrontendTopDownBundle)) 382 topdown_stage := io.in.bits.topdown_info 383 when(io.flush) { 384 when(io.ControlRedirect) { 385 when(io.ControlBTBMissBubble) { 386 topdown_stage.reasons(TopDownCounters.BTBMissBubble.id) := true.B 387 }.elsewhen(io.TAGEMissBubble) { 388 topdown_stage.reasons(TopDownCounters.TAGEMissBubble.id) := true.B 389 }.elsewhen(io.SCMissBubble) { 390 topdown_stage.reasons(TopDownCounters.SCMissBubble.id) := true.B 391 }.elsewhen(io.ITTAGEMissBubble) { 392 topdown_stage.reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 393 }.elsewhen(io.RASMissBubble) { 394 topdown_stage.reasons(TopDownCounters.RASMissBubble.id) := true.B 395 } 396 }.elsewhen(io.MemVioRedirect) { 397 topdown_stage.reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 398 }.otherwise { 399 topdown_stage.reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 400 } 401 } 402 403 404 val matchBubble = Wire(UInt(log2Up(TopDownCounters.NumStallReasons.id).W)) 405 val deqValidCount = PopCount(validVec.asBools) 406 val deqWasteCount = DecodeWidth.U - deqValidCount 407 matchBubble := (TopDownCounters.NumStallReasons.id - 1).U - PriorityEncoder(topdown_stage.reasons.reverse) 408 409 io.stallReason.reason.map(_ := 0.U) 410 for (i <- 0 until DecodeWidth) { 411 when(i.U < deqWasteCount) { 412 io.stallReason.reason(DecodeWidth - i - 1) := matchBubble 413 } 414 } 415 416 when(!(deqWasteCount === DecodeWidth.U || topdown_stage.reasons.asUInt.orR)) { 417 // should set reason for FetchFragmentationStall 418 // topdown_stage.reasons(TopDownCounters.FetchFragmentationStall.id) := true.B 419 for (i <- 0 until DecodeWidth) { 420 when(i.U < deqWasteCount) { 421 io.stallReason.reason(DecodeWidth - i - 1) := TopDownCounters.FetchFragBubble.id.U 422 } 423 } 424 } 425 426 when(io.stallReason.backReason.valid) { 427 io.stallReason.reason.map(_ := io.stallReason.backReason.bits) 428 } 429 430 // Debug info 431 XSError( 432 deqPtr.value =/= deqBankPtr.value + deqInBankPtr(deqBankPtr.value).value * IBufNBank.asUInt, 433 "Dequeue PTR mismatch" 434 ) 435 XSError(isBefore(enqPtr, deqPtr) && !isFull(enqPtr, deqPtr), "\ndeqPtr is older than enqPtr!\n") 436 437 XSDebug(io.flush, "IBuffer Flushed\n") 438 439 when(io.in.fire) { 440 XSDebug("Enque:\n") 441 XSDebug(p"MASK=${Binary(io.in.bits.valid)}\n") 442 for(i <- 0 until PredictWidth){ 443 XSDebug(p"PC=${Hexadecimal(io.in.bits.pc(i))} ${Hexadecimal(io.in.bits.instrs(i))}\n") 444 } 445 } 446 447 for (i <- 0 until DecodeWidth) { 448 XSDebug(io.out(i).fire, 449 p"deq: ${Hexadecimal(io.out(i).bits.instr)} PC=${Hexadecimal(io.out(i).bits.pc)}" + 450 p"v=${io.out(i).valid} r=${io.out(i).ready} " + 451 p"excpVec=${Binary(io.out(i).bits.exceptionVec.asUInt)} crossPageIPF=${io.out(i).bits.crossPageIPFFix}\n") 452 } 453 454 XSDebug(p"numValid: ${numValid}\n") 455 XSDebug(p"EnqNum: ${numEnq}\n") 456 XSDebug(p"DeqNum: ${numDeq}\n") 457 458 val afterInit = RegInit(false.B) 459 val headBubble = RegInit(false.B) 460 when (io.in.fire) { afterInit := true.B } 461 when (io.flush) { 462 headBubble := true.B 463 } .elsewhen(numValid =/= 0.U) { 464 headBubble := false.B 465 } 466 val instrHungry = afterInit && (numValid === 0.U) && !headBubble 467 468 QueuePerf(IBufSize, numValid, !allowEnq) 469 XSPerfAccumulate("flush", io.flush) 470 XSPerfAccumulate("hungry", instrHungry) 471 472 val ibuffer_IDWidth_hvButNotFull = afterInit && (numValid =/= 0.U) && (numValid < DecodeWidth.U) && !headBubble 473 XSPerfAccumulate("ibuffer_IDWidth_hvButNotFull", ibuffer_IDWidth_hvButNotFull) 474 475 val FrontBubble = Mux(decodeCanAccept, DecodeWidth.U - numOut, 0.U) 476 477 val perfEvents = Seq( 478 ("IBuffer_Flushed ", io.flush), 479 ("IBuffer_hungry ", instrHungry), 480 ("IBuffer_1_4_valid", (numValid > (0 * (IBufSize / 4)).U) & (numValid < (1 * (IBufSize / 4)).U)), 481 ("IBuffer_2_4_valid", (numValid >= (1 * (IBufSize / 4)).U) & (numValid < (2 * (IBufSize / 4)).U)), 482 ("IBuffer_3_4_valid", (numValid >= (2 * (IBufSize / 4)).U) & (numValid < (3 * (IBufSize / 4)).U)), 483 ("IBuffer_4_4_valid", (numValid >= (3 * (IBufSize / 4)).U) & (numValid < (4 * (IBufSize / 4)).U)), 484 ("IBuffer_full ", numValid.andR), 485 ("Front_Bubble ", FrontBubble) 486 ) 487 generatePerfEvent() 488} 489