xref: /XiangShan/src/main/scala/xiangshan/frontend/Frontend.scala (revision 491c16ade93d4956fec6dde187943d72bb010bc4)
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.foreach(_ := icache.io.itlbFlushPipe)
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
186  icache.io.fencei := RegNext(io.fencei)
187
188  // IFU-Ibuffer
189  ifu.io.toIbuffer <> ibuffer.io.in
190
191  ftq.io.fromBackend <> io.backend.toFtq
192  io.backend.fromFtq := ftq.io.toBackend
193  io.backend.fromIfu := ifu.io.toBackend
194  io.frontendInfo.bpuInfo <> ftq.io.bpuInfo
195
196  val checkPcMem = Reg(Vec(FtqSize, new Ftq_RF_Components))
197  when(ftq.io.toBackend.pc_mem_wen) {
198    checkPcMem(ftq.io.toBackend.pc_mem_waddr) := ftq.io.toBackend.pc_mem_wdata
199  }
200
201  val checkTargetPtr = Wire(Vec(DecodeWidth, new FtqPtr))
202  val checkTarget    = Wire(Vec(DecodeWidth, UInt(VAddrBits.W)))
203
204  for (i <- 0 until DecodeWidth) {
205    checkTargetPtr(i) := ibuffer.io.out(i).bits.ftqPtr
206    checkTarget(i) := Mux(
207      ftq.io.toBackend.newest_entry_ptr.value === checkTargetPtr(i).value,
208      ftq.io.toBackend.newest_entry_target,
209      checkPcMem((checkTargetPtr(i) + 1.U).value).startAddr
210    )
211  }
212
213  // commented out for this br could be the last instruction in the fetch block
214  def checkNotTakenConsecutive = {
215    val prevNotTakenValid  = RegInit(0.B)
216    val prevNotTakenFtqPtr = Reg(new FtqPtr)
217    for (i <- 0 until DecodeWidth - 1) {
218      // for instrs that is not the last, if a not-taken br, the next instr should have the same ftqPtr
219      // for instrs that is the last, record and check next request
220      when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr) {
221        when(ibuffer.io.out(i + 1).fire) {
222          // not last br, check now
223        }.otherwise {
224          // last br, record its info
225          prevNotTakenValid  := true.B
226          prevNotTakenFtqPtr := checkTargetPtr(i)
227        }
228      }
229      XSError(
230        ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr &&
231          ibuffer.io.out(i + 1).fire &&
232          checkTargetPtr(i).value =/= checkTargetPtr(i + 1).value,
233        "not-taken br should have same ftqPtr\n"
234      )
235    }
236    when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr) {
237      // last instr is a br, record its info
238      prevNotTakenValid  := true.B
239      prevNotTakenFtqPtr := checkTargetPtr(DecodeWidth - 1)
240    }
241    when(prevNotTakenValid && ibuffer.io.out(0).fire) {
242      prevNotTakenValid := false.B
243    }
244    XSError(
245      prevNotTakenValid && ibuffer.io.out(0).fire &&
246        prevNotTakenFtqPtr.value =/= checkTargetPtr(0).value,
247      "not-taken br should have same ftqPtr\n"
248    )
249
250    when(needFlush) {
251      prevNotTakenValid := false.B
252    }
253  }
254
255  def checkTakenNotConsecutive = {
256    val prevTakenValid  = RegInit(0.B)
257    val prevTakenFtqPtr = Reg(new FtqPtr)
258    for (i <- 0 until DecodeWidth - 1) {
259      // for instrs that is not the last, if a taken br, the next instr should not have the same ftqPtr
260      // for instrs that is the last, record and check next request
261      when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && ibuffer.io.out(i).bits.pred_taken) {
262        when(ibuffer.io.out(i + 1).fire) {
263          // not last br, check now
264        }.otherwise {
265          // last br, record its info
266          prevTakenValid  := true.B
267          prevTakenFtqPtr := checkTargetPtr(i)
268        }
269      }
270      XSError(
271        ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && ibuffer.io.out(i).bits.pred_taken &&
272          ibuffer.io.out(i + 1).fire &&
273          (checkTargetPtr(i) + 1.U).value =/= checkTargetPtr(i + 1).value,
274        "taken br should have consecutive ftqPtr\n"
275      )
276    }
277    when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && ibuffer.io.out(
278      DecodeWidth - 1
279    ).bits.pred_taken) {
280      // last instr is a br, record its info
281      prevTakenValid  := true.B
282      prevTakenFtqPtr := checkTargetPtr(DecodeWidth - 1)
283    }
284    when(prevTakenValid && ibuffer.io.out(0).fire) {
285      prevTakenValid := false.B
286    }
287    XSError(
288      prevTakenValid && ibuffer.io.out(0).fire &&
289        (prevTakenFtqPtr + 1.U).value =/= checkTargetPtr(0).value,
290      "taken br should have consecutive ftqPtr\n"
291    )
292    when(needFlush) {
293      prevTakenValid := false.B
294    }
295  }
296
297  def checkNotTakenPC = {
298    val prevNotTakenPC    = Reg(UInt(VAddrBits.W))
299    val prevIsRVC         = Reg(Bool())
300    val prevNotTakenValid = RegInit(0.B)
301
302    for (i <- 0 until DecodeWidth - 1) {
303      when(ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && !ibuffer.io.out(i).bits.pred_taken) {
304        when(ibuffer.io.out(i + 1).fire) {}.otherwise {
305          prevNotTakenValid := true.B
306          prevIsRVC         := ibuffer.io.out(i).bits.pd.isRVC
307          prevNotTakenPC    := ibuffer.io.out(i).bits.pc
308        }
309      }
310      XSError(
311        ibuffer.io.out(i).fire && ibuffer.io.out(i).bits.pd.isBr && !ibuffer.io.out(i).bits.pred_taken &&
312          ibuffer.io.out(i + 1).fire &&
313          ibuffer.io.out(i).bits.pc + Mux(ibuffer.io.out(i).bits.pd.isRVC, 2.U, 4.U) =/= ibuffer.io.out(
314            i + 1
315          ).bits.pc,
316        "not-taken br should have consecutive pc\n"
317      )
318    }
319    when(ibuffer.io.out(DecodeWidth - 1).fire && ibuffer.io.out(DecodeWidth - 1).bits.pd.isBr && !ibuffer.io.out(
320      DecodeWidth - 1
321    ).bits.pred_taken) {
322      prevNotTakenValid := true.B
323      prevIsRVC         := ibuffer.io.out(DecodeWidth - 1).bits.pd.isRVC
324      prevNotTakenPC    := ibuffer.io.out(DecodeWidth - 1).bits.pc
325    }
326    when(prevNotTakenValid && ibuffer.io.out(0).fire) {
327      prevNotTakenValid := false.B
328    }
329    XSError(
330      prevNotTakenValid && ibuffer.io.out(0).fire &&
331        prevNotTakenPC + Mux(prevIsRVC, 2.U, 4.U) =/= ibuffer.io.out(0).bits.pc,
332      "not-taken br should have same pc\n"
333    )
334    when(needFlush) {
335      prevNotTakenValid := false.B
336    }
337  }
338
339  def checkTakenPC = {
340    val prevTakenFtqPtr = Reg(new FtqPtr)
341    val prevTakenValid  = RegInit(0.B)
342    val prevTakenTarget = Wire(UInt(VAddrBits.W))
343    prevTakenTarget := checkPcMem((prevTakenFtqPtr + 1.U).value).startAddr
344
345    for (i <- 0 until DecodeWidth - 1) {
346      when(ibuffer.io.out(i).fire && !ibuffer.io.out(i).bits.pd.notCFI && ibuffer.io.out(i).bits.pred_taken) {
347        when(ibuffer.io.out(i + 1).fire) {}.otherwise {
348          prevTakenValid  := true.B
349          prevTakenFtqPtr := checkTargetPtr(i)
350        }
351      }
352      XSError(
353        ibuffer.io.out(i).fire && !ibuffer.io.out(i).bits.pd.notCFI && ibuffer.io.out(i).bits.pred_taken &&
354          ibuffer.io.out(i + 1).fire &&
355          checkTarget(i) =/= ibuffer.io.out(i + 1).bits.pc,
356        "taken instr should follow target pc\n"
357      )
358    }
359    when(ibuffer.io.out(DecodeWidth - 1).fire && !ibuffer.io.out(DecodeWidth - 1).bits.pd.notCFI && ibuffer.io.out(
360      DecodeWidth - 1
361    ).bits.pred_taken) {
362      prevTakenValid  := true.B
363      prevTakenFtqPtr := checkTargetPtr(DecodeWidth - 1)
364    }
365    when(prevTakenValid && ibuffer.io.out(0).fire) {
366      prevTakenValid := false.B
367    }
368    XSError(
369      prevTakenValid && ibuffer.io.out(0).fire &&
370        prevTakenTarget =/= ibuffer.io.out(0).bits.pc,
371      "taken instr should follow target pc\n"
372    )
373    when(needFlush) {
374      prevTakenValid := false.B
375    }
376  }
377
378  // checkNotTakenConsecutive
379  checkTakenNotConsecutive
380  checkTakenPC
381  checkNotTakenPC
382
383  ifu.io.rob_commits <> io.backend.toFtq.rob_commits
384
385  ibuffer.io.flush                := needFlush
386  ibuffer.io.ControlRedirect      := FlushControlRedirect
387  ibuffer.io.MemVioRedirect       := FlushMemVioRedirect
388  ibuffer.io.ControlBTBMissBubble := FlushControlBTBMiss
389  ibuffer.io.TAGEMissBubble       := FlushTAGEMiss
390  ibuffer.io.SCMissBubble         := FlushSCMiss
391  ibuffer.io.ITTAGEMissBubble     := FlushITTAGEMiss
392  ibuffer.io.RASMissBubble        := FlushRASMiss
393  ibuffer.io.decodeCanAccept      := io.backend.canAccept
394
395  FlushControlBTBMiss := ftq.io.ControlBTBMissBubble
396  FlushTAGEMiss       := ftq.io.TAGEMissBubble
397  FlushSCMiss         := ftq.io.SCMissBubble
398  FlushITTAGEMiss     := ftq.io.ITTAGEMissBubble
399  FlushRASMiss        := ftq.io.RASMissBubble
400
401  io.backend.cfVec <> ibuffer.io.out
402  io.backend.stallReason <> ibuffer.io.stallReason
403
404  instrUncache.io.req <> ifu.io.uncacheInter.toUncache
405  ifu.io.uncacheInter.fromUncache <> instrUncache.io.resp
406  instrUncache.io.flush := false.B
407  io.error <> RegNext(RegNext(icache.io.error))
408
409  icache.io.hartId := io.hartId
410
411  itlbRepeater1.io.debugTopDown.robHeadVaddr := io.debugTopDown.robHeadVaddr
412
413  val frontendBubble = Mux(io.backend.canAccept, DecodeWidth.U - PopCount(ibuffer.io.out.map(_.valid)), 0.U)
414  XSPerfAccumulate("FrontendBubble", frontendBubble)
415  io.frontendInfo.ibufFull := RegNext(ibuffer.io.full)
416  io.resetInFrontend       := reset.asBool
417
418  // PFEvent
419  val pfevent = Module(new PFEvent)
420  pfevent.io.distribute_csr := io.csrCtrl.distribute_csr
421  val csrevents = pfevent.io.hpmevent.take(8)
422
423  val perfFromUnits = Seq(ifu, ibuffer, icache, ftq, bpu).flatMap(_.getPerfEvents)
424  val perfFromIO    = Seq()
425  val perfBlock     = Seq()
426  // let index = 0 be no event
427  val allPerfEvents = Seq(("noEvent", 0.U)) ++ perfFromUnits ++ perfFromIO ++ perfBlock
428
429  if (printEventCoding) {
430    for (((name, inc), i) <- allPerfEvents.zipWithIndex) {
431      println("Frontend perfEvents Set", name, inc, i)
432    }
433  }
434
435  val allPerfInc          = allPerfEvents.map(_._2.asTypeOf(new PerfEvent))
436  override val perfEvents = HPerfMonitor(csrevents, allPerfInc).getPerfEvents
437  generatePerfEvent()
438}
439