xref: /XiangShan/src/main/scala/xiangshan/XSCore.scala (revision b1e920234888fd3e5463ceb2a99c9bdca087f585)
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
18
19import org.chipsalliance.cde.config
20import org.chipsalliance.cde.config.Parameters
21import chisel3._
22import chisel3.util._
23import freechips.rocketchip.diplomacy.{BundleBridgeSource, LazyModule, LazyModuleImp}
24import freechips.rocketchip.interrupts.{IntSinkNode, IntSinkPortSimple}
25import freechips.rocketchip.tile.HasFPUParameters
26import system.HasSoCParameter
27import utils._
28import utility._
29import xiangshan.backend._
30import xiangshan.cache.mmu._
31import xiangshan.frontend._
32import xiangshan.mem.L1PrefetchFuzzer
33
34abstract class XSModule(implicit val p: Parameters) extends Module
35  with HasXSParameter
36  with HasFPUParameters
37
38//remove this trait after impl module logic
39trait NeedImpl {
40  this: RawModule =>
41  protected def IO[T <: Data](iodef: T): T = {
42    println(s"[Warn]: (${this.name}) please reomve 'NeedImpl' after implement this module")
43    val io = chisel3.IO(iodef)
44    io <> DontCare
45    io
46  }
47}
48
49abstract class XSBundle(implicit val p: Parameters) extends Bundle
50  with HasXSParameter
51
52abstract class XSCoreBase()(implicit p: config.Parameters) extends LazyModule
53  with HasXSParameter
54{
55  override def shouldBeInlined: Boolean = false
56  // interrupt sinks
57  val clint_int_sink = IntSinkNode(IntSinkPortSimple(1, 2))
58  val debug_int_sink = IntSinkNode(IntSinkPortSimple(1, 1))
59  val plic_int_sink = IntSinkNode(IntSinkPortSimple(2, 1))
60  // outer facing nodes
61  val frontend = LazyModule(new Frontend())
62  val csrOut = BundleBridgeSource(Some(() => new DistributedCSRIO()))
63  val backend = LazyModule(new Backend(backendParams))
64
65  val memBlock = LazyModule(new MemBlock)
66}
67
68class XSCore()(implicit p: config.Parameters) extends XSCoreBase
69  with HasXSDts
70{
71  lazy val module = new XSCoreImp(this)
72}
73
74class XSCoreImp(outer: XSCoreBase) extends LazyModuleImp(outer)
75  with HasXSParameter
76  with HasSoCParameter {
77  val io = IO(new Bundle {
78    val hartId = Input(UInt(64.W))
79    val reset_vector = Input(UInt(PAddrBits.W))
80    val cpu_halt = Output(Bool())
81    val l2_pf_enable = Output(Bool())
82    val perfEvents = Input(Vec(numPCntHc * coreParams.L2NBanks, new PerfEvent))
83    val beu_errors = Output(new XSL1BusErrors())
84    val l2_hint = Input(Valid(new L2ToL1Hint()))
85    val l2PfqBusy = Input(Bool())
86    val debugTopDown = new Bundle {
87      val robHeadPaddr = Valid(UInt(PAddrBits.W))
88      val l2MissMatch = Input(Bool())
89      val l3MissMatch = Input(Bool())
90    }
91  })
92
93  println(s"FPGAPlatform:${env.FPGAPlatform} EnableDebug:${env.EnableDebug}")
94
95  val frontend = outer.frontend.module
96  val backend = outer.backend.module
97  val memBlock = outer.memBlock.module
98
99  val fenceio = backend.io.fenceio
100  fenceio.disableSfence := DontCare
101
102  frontend.io.hartId  := io.hartId
103  frontend.io.backend <> backend.io.frontend
104  frontend.io.sfence <> backend.io.frontendSfence
105  frontend.io.tlbCsr <> backend.io.frontendTlbCsr
106  frontend.io.csrCtrl <> backend.io.frontendCsrCtrl
107  frontend.io.fencei <> fenceio.fencei
108
109  backend.io.fromTop.hartId := io.hartId
110  backend.io.fromTop.externalInterrupt.msip := outer.clint_int_sink.in.head._1(0)
111  backend.io.fromTop.externalInterrupt.mtip := outer.clint_int_sink.in.head._1(1)
112  backend.io.fromTop.externalInterrupt.meip := outer.plic_int_sink.in.head._1(0)
113  backend.io.fromTop.externalInterrupt.seip := outer.plic_int_sink.in.last._1(0)
114  backend.io.fromTop.externalInterrupt.debug := outer.debug_int_sink.in.head._1(0)
115
116  backend.io.frontendCsrDistributedUpdate := frontend.io.csrUpdate
117
118  backend.io.mem.stIn.zip(memBlock.io.mem_to_ooo.stIn).foreach { case (sink, source) =>
119    sink.valid := source.valid
120    sink.bits := 0.U.asTypeOf(sink.bits)
121    sink.bits.robIdx := source.bits.uop.robIdx
122    sink.bits.ssid := source.bits.uop.ssid
123    sink.bits.storeSetHit := source.bits.uop.storeSetHit
124    // The other signals have not been used
125  }
126  backend.io.mem.memoryViolation <> memBlock.io.mem_to_ooo.memoryViolation
127  backend.io.mem.lsqEnqIO <> memBlock.io.ooo_to_mem.enqLsq
128  backend.io.mem.sqDeq := memBlock.io.mem_to_ooo.sqDeq
129  backend.io.mem.lqDeq := memBlock.io.mem_to_ooo.lqDeq
130  backend.io.mem.sqDeqPtr := memBlock.io.mem_to_ooo.sqDeqPtr
131  backend.io.mem.lqDeqPtr := memBlock.io.mem_to_ooo.lqDeqPtr
132  backend.io.mem.lqCancelCnt := memBlock.io.mem_to_ooo.lqCancelCnt
133  backend.io.mem.sqCancelCnt := memBlock.io.mem_to_ooo.sqCancelCnt
134  backend.io.mem.otherFastWakeup := memBlock.io.mem_to_ooo.otherFastWakeup
135  backend.io.mem.stIssuePtr := memBlock.io.mem_to_ooo.stIssuePtr
136  backend.io.mem.ldaIqFeedback <> memBlock.io.mem_to_ooo.ldaIqFeedback
137  backend.io.mem.staIqFeedback <> memBlock.io.mem_to_ooo.staIqFeedback
138  backend.io.mem.hyuIqFeedback <> memBlock.io.mem_to_ooo.hyuIqFeedback
139  backend.io.mem.ldCancel <> memBlock.io.mem_to_ooo.ldCancel
140  backend.io.mem.writebackLda <> memBlock.io.mem_to_ooo.writebackLda
141  backend.io.mem.writebackSta <> memBlock.io.mem_to_ooo.writebackSta
142  backend.io.mem.writebackHyuLda <> memBlock.io.mem_to_ooo.writebackHyuLda
143  backend.io.mem.writebackHyuSta <> memBlock.io.mem_to_ooo.writebackHyuSta
144  backend.io.mem.writebackStd <> memBlock.io.mem_to_ooo.writebackStd
145  backend.io.mem.writebackVldu <> memBlock.io.mem_to_ooo.writebackVldu
146  backend.io.mem.robLsqIO.mmio := memBlock.io.mem_to_ooo.lsqio.mmio
147  backend.io.mem.robLsqIO.uop := memBlock.io.mem_to_ooo.lsqio.uop
148
149  frontend.io.reset_vector := io.reset_vector
150
151  io.cpu_halt := backend.io.toTop.cpuHalted
152
153  // memblock error exception writeback, 1 cycle after normal writeback
154  backend.io.mem.s3_delayed_load_error <> memBlock.io.mem_to_ooo.s3_delayed_load_error
155
156  io.beu_errors.icache <> frontend.io.error.toL1BusErrorUnitInfo()
157  io.beu_errors.dcache <> memBlock.io.error.toL1BusErrorUnitInfo()
158  io.beu_errors.l2 <> DontCare
159
160  memBlock.io.hartId := io.hartId
161  memBlock.io.ooo_to_mem.issueLda <> backend.io.mem.issueLda
162  memBlock.io.ooo_to_mem.issueSta <> backend.io.mem.issueSta
163  memBlock.io.ooo_to_mem.issueStd <> backend.io.mem.issueStd
164  memBlock.io.ooo_to_mem.issueHya <> backend.io.mem.issueHylda
165  backend.io.mem.issueHysta.map(_.ready := false.B) // this fake port should not be used
166  memBlock.io.ooo_to_mem.issueVldu <> backend.io.mem.issueVldu
167
168  // By default, instructions do not have exceptions when they enter the function units.
169  memBlock.io.ooo_to_mem.issueUops.map(_.bits.uop.clearExceptions())
170  memBlock.io.ooo_to_mem.loadPc := backend.io.mem.loadPcRead
171  memBlock.io.ooo_to_mem.storePc := backend.io.mem.storePcRead
172  memBlock.io.ooo_to_mem.hybridPc := backend.io.mem.hyuPcRead
173  memBlock.io.ooo_to_mem.flushSb := backend.io.fenceio.sbuffer.flushSb
174  memBlock.io.ooo_to_mem.loadFastMatch := 0.U.asTypeOf(memBlock.io.ooo_to_mem.loadFastMatch)
175  memBlock.io.ooo_to_mem.loadFastImm := 0.U.asTypeOf(memBlock.io.ooo_to_mem.loadFastImm)
176  memBlock.io.ooo_to_mem.loadFastFuOpType := 0.U.asTypeOf(memBlock.io.ooo_to_mem.loadFastFuOpType)
177
178  backend.io.mem.exceptionVAddr := memBlock.io.mem_to_ooo.lsqio.vaddr
179  backend.io.mem.csrDistributedUpdate := memBlock.io.mem_to_ooo.csrUpdate
180  backend.io.mem.debugLS := memBlock.io.debug_ls
181  backend.io.mem.lsTopdownInfo := memBlock.io.mem_to_ooo.lsTopdownInfo
182  backend.io.mem.lqCanAccept := memBlock.io.mem_to_ooo.lsqio.lqCanAccept
183  backend.io.mem.sqCanAccept := memBlock.io.mem_to_ooo.lsqio.sqCanAccept
184  backend.io.fenceio.sbuffer.sbIsEmpty := memBlock.io.mem_to_ooo.sbIsEmpty
185
186  backend.io.perf.frontendInfo := frontend.io.frontendInfo
187  backend.io.perf.memInfo := memBlock.io.memInfo
188  backend.io.perf.perfEventsFrontend := frontend.getPerf
189  backend.io.perf.perfEventsLsu := memBlock.getPerf
190  backend.io.perf.perfEventsHc := io.perfEvents
191  backend.io.perf.perfEventsCtrl := DontCare
192  backend.io.perf.retiredInstr := DontCare
193  backend.io.perf.ctrlInfo := DontCare
194
195
196  memBlock.io.ooo_to_mem.sfence <> backend.io.mem.sfence
197
198  memBlock.io.redirect <> backend.io.mem.redirect
199  memBlock.io.ooo_to_mem.csrCtrl <> backend.io.mem.csrCtrl
200  memBlock.io.ooo_to_mem.tlbCsr <> backend.io.mem.tlbCsr
201  memBlock.io.ooo_to_mem.lsqio.lcommit        := backend.io.mem.robLsqIO.lcommit
202  memBlock.io.ooo_to_mem.lsqio.scommit        := backend.io.mem.robLsqIO.scommit
203  memBlock.io.ooo_to_mem.lsqio.pendingld      := backend.io.mem.robLsqIO.pendingld
204  memBlock.io.ooo_to_mem.lsqio.pendingst      := backend.io.mem.robLsqIO.pendingst
205  memBlock.io.ooo_to_mem.lsqio.commit         := backend.io.mem.robLsqIO.commit
206  memBlock.io.ooo_to_mem.lsqio.pendingPtr     := backend.io.mem.robLsqIO.pendingPtr
207  memBlock.io.ooo_to_mem.lsqio.pendingPtrNext := backend.io.mem.robLsqIO.pendingPtrNext
208  memBlock.io.ooo_to_mem.isStore              := backend.io.mem.isStoreException
209
210  memBlock.io.fetch_to_mem.itlb <> frontend.io.ptw
211  memBlock.io.l2_hint.valid := io.l2_hint.valid
212  memBlock.io.l2_hint.bits.sourceId := io.l2_hint.bits.sourceId
213  memBlock.io.l2PfqBusy := io.l2PfqBusy
214
215  // if l2 prefetcher use stream prefetch, it should be placed in XSCore
216  io.l2_pf_enable := backend.io.csrCustomCtrl.l2_pf_enable
217
218  // top-down info
219  memBlock.io.debugTopDown.robHeadVaddr := backend.io.debugTopDown.fromRob.robHeadVaddr
220  frontend.io.debugTopDown.robHeadVaddr := backend.io.debugTopDown.fromRob.robHeadVaddr
221  io.debugTopDown.robHeadPaddr := backend.io.debugTopDown.fromRob.robHeadPaddr
222  backend.io.debugTopDown.fromCore.l2MissMatch := io.debugTopDown.l2MissMatch
223  backend.io.debugTopDown.fromCore.l3MissMatch := io.debugTopDown.l3MissMatch
224  backend.io.debugTopDown.fromCore.fromMem := memBlock.io.debugTopDown.toCore
225  memBlock.io.debugRolling := backend.io.debugRolling
226
227  // Modules are reset one by one
228  val resetTree = ResetGenNode(
229    Seq(
230      ModuleNode(memBlock),
231      ResetGenNode(Seq(
232        ModuleNode(backend),
233        ResetGenNode(Seq(
234          ResetGenNode(Seq(
235            ModuleNode(frontend)
236          ))
237        ))
238      ))
239    )
240  )
241
242  ResetGen(resetTree, reset, !debugOpts.FPGAPlatform)
243
244}
245