xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision 6639e9a467468f4e1b05a25a5de4500772aedeb1)
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*
17* Acknowledgement
18*
19* This implementation is inspired by several key papers:
20* [1] Alex Ramirez, Oliverio J. Santana, Josep L. Larriba-Pey, and Mateo Valero. "[Fetching instruction streams.]
21* (https://doi.org/10.1109/MICRO.2002.1176264)" 35th Annual IEEE/ACM International Symposium on Microarchitecture
22* (MICRO). 2002.
23* [2] Yasuo Ishii, Jaekyu Lee, Krishnendra Nathella, and Dam Sunwoo. "[Rebasing instruction prefetching: An industry
24* perspective.](https://doi.org/10.1109/LCA.2020.3035068)" IEEE Computer Architecture Letters 19.2: 147-150. 2020.
25* [3] Yasuo Ishii, Jaekyu Lee, Krishnendra Nathella, and Dam Sunwoo. "[Re-establishing fetch-directed instruction
26* prefetching: An industry perspective.](https://doi.org/10.1109/ISPASS51385.2021.00034)" 2021 IEEE International
27* Symposium on Performance Analysis of Systems and Software (ISPASS). 2021.
28***************************************************************************************/
29
30package xiangshan.frontend
31import chisel3._
32import chisel3.util._
33import freechips.rocketchip.diplomacy.LazyModule
34import freechips.rocketchip.diplomacy.LazyModuleImp
35import org.chipsalliance.cde.config.Parameters
36import utility._
37import xiangshan._
38import xiangshan.backend.fu.PFEvent
39import xiangshan.backend.fu.PMP
40import xiangshan.backend.fu.PMPChecker
41import xiangshan.backend.fu.PMPReqBundle
42import xiangshan.cache.mmu._
43import xiangshan.frontend.icache._
44
45class Frontend()(implicit p: Parameters) extends LazyModule with HasXSParameter {
46  override def shouldBeInlined: Boolean = false
47  val inner       = LazyModule(new FrontendInlined)
48  lazy val module = new FrontendImp(this)
49}
50
51class FrontendImp(wrapper: Frontend)(implicit p: Parameters) extends LazyModuleImp(wrapper) {
52  val io      = IO(wrapper.inner.module.io.cloneType)
53  val io_perf = IO(wrapper.inner.module.io_perf.cloneType)
54  io <> wrapper.inner.module.io
55  io_perf <> wrapper.inner.module.io_perf
56  if (p(DebugOptionsKey).ResetGen) {
57    ResetGen(ResetGenNode(Seq(ModuleNode(wrapper.inner.module))), reset, sim = false)
58  }
59}
60
61class FrontendInlined()(implicit p: Parameters) extends LazyModule with HasXSParameter {
62  override def shouldBeInlined: Boolean = true
63
64  val instrUncache = LazyModule(new InstrUncache())
65  val icache       = LazyModule(new ICache())
66
67  lazy val module = new FrontendInlinedImp(this)
68}
69
70class FrontendInlinedImp(outer: FrontendInlined) extends LazyModuleImp(outer)
71    with HasXSParameter
72    with HasPerfEvents {
73  val io = IO(new Bundle() {
74    val hartId       = Input(UInt(hartIdLen.W))
75    val reset_vector = Input(UInt(PAddrBits.W))
76    val fencei       = Input(Bool())
77    val ptw          = new TlbPtwIO()
78    val backend      = new FrontendToCtrlIO
79    val softPrefetch = Vec(backendParams.LduCnt, Flipped(Valid(new SoftIfetchPrefetchBundle)))
80    val sfence       = Input(new SfenceBundle)
81    val tlbCsr       = Input(new TlbCsrBundle)
82    val csrCtrl      = Input(new CustomCSRCtrlIO)
83    val error        = ValidIO(new L1CacheErrorInfo)
84    val frontendInfo = new Bundle {
85      val ibufFull = Output(Bool())
86      val bpuInfo = new Bundle {
87        val bpRight = Output(UInt(XLEN.W))
88        val bpWrong = Output(UInt(XLEN.W))
89      }
90    }
91    val resetInFrontend = Output(Bool())
92    val debugTopDown = new Bundle {
93      val robHeadVaddr = Flipped(Valid(UInt(VAddrBits.W)))
94    }
95  })
96
97  // decouped-frontend modules
98  val instrUncache = outer.instrUncache.module
99  val icache       = outer.icache.module
100  val bpu          = Module(new Predictor)
101  val ifu          = Module(new NewIFU)
102  val ibuffer      = Module(new IBuffer)
103  val ftq          = Module(new Ftq)
104
105  val needFlush            = RegNext(io.backend.toFtq.redirect.valid)
106  val FlushControlRedirect = RegNext(io.backend.toFtq.redirect.bits.debugIsCtrl)
107  val FlushMemVioRedirect  = RegNext(io.backend.toFtq.redirect.bits.debugIsMemVio)
108  val FlushControlBTBMiss  = Wire(Bool())
109  val FlushTAGEMiss        = Wire(Bool())
110  val FlushSCMiss          = Wire(Bool())
111  val FlushITTAGEMiss      = Wire(Bool())
112  val FlushRASMiss         = Wire(Bool())
113
114  val tlbCsr  = DelayN(io.tlbCsr, 2)
115  val csrCtrl = DelayN(io.csrCtrl, 2)
116  val sfence  = RegNext(RegNext(io.sfence))
117
118  // trigger
119  ifu.io.frontendTrigger := csrCtrl.frontend_trigger
120
121  // RVCDecoder fsIsOff
122  ifu.io.csr_fsIsOff := csrCtrl.fsIsOff
123
124  // bpu ctrl
125  bpu.io.ctrl         := csrCtrl.bp_ctrl
126  bpu.io.reset_vector := io.reset_vector
127
128  // pmp
129  val PortNumber = ICacheParameters().PortNumber
130  val pmp        = Module(new PMP())
131  val pmp_check  = VecInit(Seq.fill(coreParams.ipmpPortNum)(Module(new PMPChecker(3, sameCycle = true)).io))
132  pmp.io.distribute_csr := csrCtrl.distribute_csr
133  val pmp_req_vec = Wire(Vec(coreParams.ipmpPortNum, Valid(new PMPReqBundle())))
134  (0 until 2 * PortNumber).foreach(i => pmp_req_vec(i) <> icache.io.pmp(i).req)
135  pmp_req_vec.last <> ifu.io.pmp.req
136
137  for (i <- pmp_check.indices) {
138    pmp_check(i).apply(tlbCsr.priv.imode, pmp.io.pmp, pmp.io.pma, pmp_req_vec(i))
139  }
140  (0 until 2 * PortNumber).foreach(i => icache.io.pmp(i).resp <> pmp_check(i).resp)
141  ifu.io.pmp.resp <> pmp_check.last.resp
142
143  val itlb =
144    Module(new TLB(coreParams.itlbPortNum, nRespDups = 1, Seq.fill(PortNumber)(false) ++ Seq(true), itlbParams))
145  itlb.io.requestor.take(PortNumber) zip icache.io.itlb foreach { case (a, b) => a <> b }
146  itlb.io.requestor.last <> ifu.io.iTLBInter // mmio may need re-tlb, blocked
147  itlb.io.hartId := io.hartId
148  itlb.io.base_connect(sfence, tlbCsr)
149  itlb.io.flushPipe.map(_ := needFlush)
150  itlb.io.redirect := DontCare // itlb has flushpipe, don't need redirect signal
151
152  val itlb_ptw = Wire(new VectorTlbPtwIO(coreParams.itlbPortNum))
153  itlb_ptw.connect(itlb.io.ptw)
154  val itlbRepeater1 = PTWFilter(itlbParams.fenceDelay, itlb_ptw, sfence, tlbCsr, l2tlbParams.ifilterSize)
155  val itlbRepeater2 =
156    PTWRepeaterNB(passReady = false, itlbParams.fenceDelay, itlbRepeater1.io.ptw, io.ptw, sfence, tlbCsr)
157
158  icache.io.ftqPrefetch <> ftq.io.toPrefetch
159  icache.io.softPrefetch <> io.softPrefetch
160
161  // IFU-Ftq
162  ifu.io.ftqInter.fromFtq <> ftq.io.toIfu
163  ftq.io.toIfu.req.ready := ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
164
165  ftq.io.fromIfu <> ifu.io.ftqInter.toFtq
166  bpu.io.ftq_to_bpu <> ftq.io.toBpu
167  ftq.io.fromBpu <> bpu.io.bpu_to_ftq
168
169  ftq.io.mmioCommitRead <> ifu.io.mmioCommitRead
170
171  // IFU-ICache
172  icache.io.fetch.req <> ftq.io.toICache.req
173  ftq.io.toICache.req.ready := ifu.io.ftqInter.fromFtq.req.ready && icache.io.fetch.req.ready
174
175  ifu.io.icacheInter.resp <> icache.io.fetch.resp
176  ifu.io.icacheInter.icacheReady       := icache.io.toIFU
177  ifu.io.icacheInter.topdownIcacheMiss := icache.io.fetch.topdownIcacheMiss
178  ifu.io.icacheInter.topdownItlbMiss   := icache.io.fetch.topdownItlbMiss
179  icache.io.stop                       := ifu.io.icacheStop
180  icache.io.flush                      := ftq.io.icacheFlush
181
182  ifu.io.icachePerfInfo := icache.io.perfInfo
183
184  icache.io.csr_pf_enable     := RegNext(csrCtrl.l1I_pf_enable)
185  icache.io.csr_parity_enable := RegNext(csrCtrl.icache_parity_enable)
186
187  icache.io.fencei := RegNext(io.fencei)
188
189  // IFU-Ibuffer
190  ifu.io.toIbuffer <> ibuffer.io.in
191
192  ftq.io.fromBackend <> io.backend.toFtq
193  io.backend.fromFtq := ftq.io.toBackend
194  io.backend.fromIfu := ifu.io.toBackend
195  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
196
197  val checkPcMem = Reg(Vec(FtqSize, new Ftq_RF_Components))
198  when(ftq.io.toBackend.pc_mem_wen) {
199    checkPcMem(ftq.io.toBackend.pc_mem_waddr) := ftq.io.toBackend.pc_mem_wdata
200  }
201
202  val checkTargetIdx = Wire(Vec(DecodeWidth, UInt(log2Up(FtqSize).W)))
203  val checkTarget    = Wire(Vec(DecodeWidth, UInt(VAddrBits.W)))
204
205  for (i <- 0 until DecodeWidth) {
206    checkTargetIdx(i) := ibuffer.io.out(i).bits.ftqPtr.value
207    checkTarget(i) := Mux(
208      ftq.io.toBackend.newest_entry_ptr.value === checkTargetIdx(i),
209      ftq.io.toBackend.newest_entry_target,
210      checkPcMem(checkTargetIdx(i) + 1.U).startAddr
211    )
212  }
213
214  // commented out for this br could be the last instruction in the fetch block
215  def checkNotTakenConsecutive = {
216    val prevNotTakenValid  = RegInit(0.B)
217    val prevNotTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W))
218    for (i <- 0 until DecodeWidth - 1) {
219      // for instrs that is not the last, if a not-taken br, the next instr should have the same ftqPtr
220      // for instrs that is the last, record and check next request
221      when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr) {
222        when(ibuffer.io.out(i + 1).fire) {
223          // not last br, check now
224          XSError(checkTargetIdx(i) =/= checkTargetIdx(i + 1), "not-taken br should have same ftqPtr\n")
225        }.otherwise {
226          // last br, record its info
227          prevNotTakenValid  := true.B
228          prevNotTakenFtqIdx := checkTargetIdx(i)
229        }
230      }
231    }
232    when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr) {
233      // last instr is a br, record its info
234      prevNotTakenValid  := true.B
235      prevNotTakenFtqIdx := checkTargetIdx(DecodeWidth - 1)
236    }
237    when(prevNotTakenValid && ibuffer.io.out(0).fire) {
238      XSError(prevNotTakenFtqIdx =/= checkTargetIdx(0), "not-taken br should have same ftqPtr\n")
239      prevNotTakenValid := false.B
240    }
241    when(needFlush) {
242      prevNotTakenValid := false.B
243    }
244  }
245
246  def checkTakenNotConsecutive = {
247    val prevTakenValid  = RegInit(0.B)
248    val prevTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W))
249    for (i <- 0 until DecodeWidth - 1) {
250      // for instrs that is not the last, if a taken br, the next instr should not have the same ftqPtr
251      // for instrs that is the last, record and check next request
252      when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && ibuffer.io.out(i).bits.pred_taken) {
253        when(ibuffer.io.out(i + 1).fire) {
254          // not last br, check now
255          XSError(checkTargetIdx(i) + 1.U =/= checkTargetIdx(i + 1), "taken br should have consecutive ftqPtr\n")
256        }.otherwise {
257          // last br, record its info
258          prevTakenValid  := true.B
259          prevTakenFtqIdx := checkTargetIdx(i)
260        }
261      }
262    }
263    when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && ibuffer.io.out(
264      DecodeWidth - 1
265    ).bits.pred_taken) {
266      // last instr is a br, record its info
267      prevTakenValid  := true.B
268      prevTakenFtqIdx := checkTargetIdx(DecodeWidth - 1)
269    }
270    when(prevTakenValid && ibuffer.io.out(0).fire) {
271      XSError(prevTakenFtqIdx + 1.U =/= checkTargetIdx(0), "taken br should have consecutive ftqPtr\n")
272      prevTakenValid := false.B
273    }
274    when(needFlush) {
275      prevTakenValid := false.B
276    }
277  }
278
279  def checkNotTakenPC = {
280    val prevNotTakenPC    = Reg(UInt(VAddrBits.W))
281    val prevIsRVC         = Reg(Bool())
282    val prevNotTakenValid = RegInit(0.B)
283
284    for (i <- 0 until DecodeWidth - 1) {
285      when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && !ibuffer.io.out(i).bits.pred_taken) {
286        when(ibuffer.io.out(i + 1).fire) {
287          XSError(
288            ibuffer.io.out(i).bits.pc + Mux(ibuffer.io.out(i).bits.pd.isRVC, 2.U, 4.U) =/= ibuffer.io.out(
289              i + 1
290            ).bits.pc,
291            "not-taken br should have consecutive pc\n"
292          )
293        }.otherwise {
294          prevNotTakenValid := true.B
295          prevIsRVC         := ibuffer.io.out(i).bits.pd.isRVC
296          prevNotTakenPC    := ibuffer.io.out(i).bits.pc
297        }
298      }
299    }
300    when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && !ibuffer.io.out(
301      DecodeWidth - 1
302    ).bits.pred_taken) {
303      prevNotTakenValid := true.B
304      prevIsRVC         := ibuffer.io.out(DecodeWidth - 1).bits.pd.isRVC
305      prevNotTakenPC    := ibuffer.io.out(DecodeWidth - 1).bits.pc
306    }
307    when(prevNotTakenValid && ibuffer.io.out(0).fire) {
308      XSError(
309        prevNotTakenPC + Mux(prevIsRVC, 2.U, 4.U) =/= ibuffer.io.out(0).bits.pc,
310        "not-taken br should have same pc\n"
311      )
312      prevNotTakenValid := false.B
313    }
314    when(needFlush) {
315      prevNotTakenValid := false.B
316    }
317  }
318
319  def checkTakenPC = {
320    val prevTakenFtqIdx = Reg(UInt(log2Up(FtqSize).W))
321    val prevTakenValid  = RegInit(0.B)
322    val prevTakenTarget = Wire(UInt(VAddrBits.W))
323    prevTakenTarget := checkPcMem(prevTakenFtqIdx + 1.U).startAddr
324
325    for (i <- 0 until DecodeWidth - 1) {
326      when(ibuffer.io.out(i).fire && !ibuffer.io.out(i).bits.pd.notCFI && ibuffer.io.out(i).bits.pred_taken) {
327        when(ibuffer.io.out(i + 1).fire) {
328          XSError(checkTarget(i) =/= ibuffer.io.out(i + 1).bits.pc, "taken instr should follow target pc\n")
329        }.otherwise {
330          prevTakenValid  := true.B
331          prevTakenFtqIdx := checkTargetIdx(i)
332        }
333      }
334    }
335    when(ibuffer.io.out(DecodeWidth - 1).fire && !ibuffer.io.out(DecodeWidth - 1).bits.pd.notCFI && ibuffer.io.out(
336      DecodeWidth - 1
337    ).bits.pred_taken) {
338      prevTakenValid  := true.B
339      prevTakenFtqIdx := checkTargetIdx(DecodeWidth - 1)
340    }
341    when(prevTakenValid && ibuffer.io.out(0).fire) {
342      XSError(prevTakenTarget =/= ibuffer.io.out(0).bits.pc, "taken instr should follow target pc\n")
343      prevTakenValid := false.B
344    }
345    when(needFlush) {
346      prevTakenValid := false.B
347    }
348  }
349
350  // checkNotTakenConsecutive
351  checkTakenNotConsecutive
352  checkTakenPC
353  checkNotTakenPC
354
355  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
356
357  ibuffer.io.flush                := needFlush
358  ibuffer.io.ControlRedirect      := FlushControlRedirect
359  ibuffer.io.MemVioRedirect       := FlushMemVioRedirect
360  ibuffer.io.ControlBTBMissBubble := FlushControlBTBMiss
361  ibuffer.io.TAGEMissBubble       := FlushTAGEMiss
362  ibuffer.io.SCMissBubble         := FlushSCMiss
363  ibuffer.io.ITTAGEMissBubble     := FlushITTAGEMiss
364  ibuffer.io.RASMissBubble        := FlushRASMiss
365  ibuffer.io.decodeCanAccept      := io.backend.canAccept
366
367  FlushControlBTBMiss := ftq.io.ControlBTBMissBubble
368  FlushTAGEMiss       := ftq.io.TAGEMissBubble
369  FlushSCMiss         := ftq.io.SCMissBubble
370  FlushITTAGEMiss     := ftq.io.ITTAGEMissBubble
371  FlushRASMiss        := ftq.io.RASMissBubble
372
373  io.backend.cfVec <> ibuffer.io.out
374  io.backend.stallReason <> ibuffer.io.stallReason
375
376  instrUncache.io.req <> ifu.io.uncacheInter.toUncache
377  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
378  instrUncache.io.flush := false.B
379  io.error <> RegNext(RegNext(icache.io.error))
380
381  icache.io.hartId := io.hartId
382
383  itlbRepeater1.io.debugTopDown.robHeadVaddr := io.debugTopDown.robHeadVaddr
384
385  val frontendBubble = Mux(io.backend.canAccept, DecodeWidth.U - PopCount(ibuffer.io.out.map(_.valid)), 0.U)
386  XSPerfAccumulate("FrontendBubble", frontendBubble)
387  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
388  io.resetInFrontend       := reset.asBool
389
390  // PFEvent
391  val pfevent = Module(new PFEvent)
392  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
393  val csrevents = pfevent.io.hpmevent.take(8)
394
395  val perfFromUnits = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerfEvents)
396  val perfFromIO    = Seq()
397  val perfBlock     = Seq()
398  // let index = 0 be no event
399  val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock
400
401  if (printEventCoding) {
402    for (((name, inc), i) <- allPerfEvents.zipWithIndex) {
403      println("Frontend perfEvents Set", name, inc, i)
404    }
405  }
406
407  val allPerfInc          = allPerfEvents.map(_._2.asTypeOf(new PerfEvent))
408  override val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents
409  generatePerfEvent()
410}
411