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