xref: /XiangShan/src/main/scala/xiangshan/XSCore.scala (revision 141a6449de9d8f61eb4f2f5e670af29782902672)
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 chipsalliance.rocketchip.config
20import chipsalliance.rocketchip.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 freechips.rocketchip.tilelink.TLBuffer
27import system.HasSoCParameter
28import utility._
29import utils._
30import xiangshan.backend._
31import xiangshan.cache.mmu._
32import xiangshan.frontend._
33import xiangshan.v2backend._
34
35abstract class XSModule(implicit val p: Parameters) extends Module
36  with HasXSParameter
37  with HasFPUParameters
38
39//remove this trait after impl module logic
40trait NeedImpl {
41  this: RawModule =>
42  override protected def IO[T <: Data](iodef: T): T = {
43    println(s"[Warn]: (${this.name}) please reomve 'NeedImpl' after implement this module")
44    val io = chisel3.experimental.IO(iodef)
45    io <> DontCare
46    io
47  }
48}
49
50//class WritebackSourceParams(
51//  var exuConfigs: Seq[Seq[ExuConfig]] = Seq()
52// ) {
53//  def length: Int = exuConfigs.length
54//  def ++(that: WritebackSourceParams): WritebackSourceParams = {
55//    new WritebackSourceParams(exuConfigs ++ that.exuConfigs)
56//  }
57//}
58
59//trait HasWritebackSource {
60//  val writebackSourceParams: Seq[WritebackSourceParams]
61//  final def writebackSource(sourceMod: HasWritebackSourceImp): Seq[Seq[Valid[ExuOutput]]] = {
62//    require(sourceMod.writebackSource.isDefined, "should not use Valid[ExuOutput]")
63//    val source = sourceMod.writebackSource.get
64//    require(source.length == writebackSourceParams.length, "length mismatch between sources")
65//    for ((s, p) <- source.zip(writebackSourceParams)) {
66//      require(s.length == p.length, "params do not match with the exuOutput")
67//    }
68//    source
69//  }
70//  final def writebackSource1(sourceMod: HasWritebackSourceImp): Seq[Seq[DecoupledIO[ExuOutput]]] = {
71//    require(sourceMod.writebackSource1.isDefined, "should not use DecoupledIO[ExuOutput]")
72//    val source = sourceMod.writebackSource1.get
73//    require(source.length == writebackSourceParams.length, "length mismatch between sources")
74//    for ((s, p) <- source.zip(writebackSourceParams)) {
75//      require(s.length == p.length, "params do not match with the exuOutput")
76//    }
77//    source
78//  }
79//  val writebackSourceImp: HasWritebackSourceImp
80//}
81
82//trait HasWritebackSourceImp {
83//  def writebackSource: Option[Seq[Seq[Valid[ExuOutput]]]] = None
84//  def writebackSource1: Option[Seq[Seq[DecoupledIO[ExuOutput]]]] = None
85//}
86
87//trait HasWritebackSink {
88//  // Caches all sources. The selected source will be the one with smallest length.
89//  var writebackSinks = ListBuffer.empty[(Seq[HasWritebackSource], Seq[Int])]
90//  def addWritebackSink(source: Seq[HasWritebackSource], index: Option[Seq[Int]] = None): HasWritebackSink = {
91//    val realIndex = if (index.isDefined) index.get else Seq.fill(source.length)(0)
92//    writebackSinks += ((source, realIndex))
93//    this
94//  }
95//
96//  def writebackSinksParams: Seq[WritebackSourceParams] = {
97//    writebackSinks.map{ case (s, i) => s.zip(i).map(x => x._1.writebackSourceParams(x._2)).reduce(_ ++ _) }
98//  }
99//  final def writebackSinksMod(
100//     thisMod: Option[HasWritebackSource] = None,
101//     thisModImp: Option[HasWritebackSourceImp] = None
102//   ): Seq[Seq[HasWritebackSourceImp]] = {
103//    require(thisMod.isDefined == thisModImp.isDefined)
104//    writebackSinks.map(_._1.map(source =>
105//      if (thisMod.isDefined && source == thisMod.get) thisModImp.get else source.writebackSourceImp)
106//    )
107//  }
108//  final def writebackSinksImp(
109//    thisMod: Option[HasWritebackSource] = None,
110//    thisModImp: Option[HasWritebackSourceImp] = None
111//  ): Seq[Seq[ValidIO[ExuOutput]]] = {
112//    val sourceMod = writebackSinksMod(thisMod, thisModImp)
113//    writebackSinks.zip(sourceMod).map{ case ((s, i), m) =>
114//      s.zip(i).zip(m).flatMap(x => x._1._1.writebackSource(x._2)(x._1._2))
115//    }
116//  }
117//  def selWritebackSinks(func: WritebackSourceParams => Int): Int = {
118//    writebackSinksParams.zipWithIndex.minBy(params => func(params._1))._2
119//  }
120//  def generateWritebackIO(
121//    thisMod: Option[HasWritebackSource] = None,
122//    thisModImp: Option[HasWritebackSourceImp] = None
123//   ): Unit
124//}
125
126abstract class XSBundle(implicit val p: Parameters) extends Bundle
127  with HasXSParameter
128
129abstract class XSCoreBase()(implicit p: config.Parameters) extends LazyModule
130  with HasXSParameter
131{
132  // interrupt sinks
133  val clint_int_sink = IntSinkNode(IntSinkPortSimple(1, 2))
134  val debug_int_sink = IntSinkNode(IntSinkPortSimple(1, 1))
135  val plic_int_sink = IntSinkNode(IntSinkPortSimple(2, 1))
136  // outer facing nodes
137  val frontend = LazyModule(new Frontend())
138  val ptw = LazyModule(new L2TLBWrapper())
139  val ptw_to_l2_buffer = if (!coreParams.softPTW) LazyModule(new TLBuffer) else null
140  val csrOut = BundleBridgeSource(Some(() => new DistributedCSRIO()))
141  val backend = LazyModule(new Backend(backendParams))
142
143  if (!coreParams.softPTW) {
144    ptw_to_l2_buffer.node := ptw.node
145  }
146
147  val memBlock = LazyModule(new MemBlock()(p.alter((site, here, up) => {
148    case XSCoreParamsKey => up(XSCoreParamsKey).copy(
149      IssQueSize = 16 // Todo
150    )
151  })))
152}
153
154class XSCore()(implicit p: config.Parameters) extends XSCoreBase
155  with HasXSDts
156{
157  lazy val module = new XSCoreImp(this)
158}
159
160class XSCoreImp(outer: XSCoreBase) extends LazyModuleImp(outer)
161  with HasXSParameter
162  with HasSoCParameter {
163  val io = IO(new Bundle {
164    val hartId = Input(UInt(64.W))
165    val reset_vector = Input(UInt(PAddrBits.W))
166    val cpu_halt = Output(Bool())
167    val l2_pf_enable = Output(Bool())
168    val perfEvents = Input(Vec(numPCntHc * coreParams.L2NBanks, new PerfEvent))
169    val beu_errors = Output(new XSL1BusErrors())
170  })
171
172  println(s"FPGAPlatform:${env.FPGAPlatform} EnableDebug:${env.EnableDebug}")
173
174  val frontend = outer.frontend.module
175  val backend = outer.backend.module
176  val memBlock = outer.memBlock.module
177  val ptw = outer.ptw.module
178  val ptw_to_l2_buffer = if (!coreParams.softPTW) outer.ptw_to_l2_buffer.module else null
179
180  val fenceio = backend.io.fenceio
181
182  frontend.io.hartId  := io.hartId
183  frontend.io.backend <> backend.io.frontend
184  frontend.io.sfence <> backend.io.frontendSfence
185  frontend.io.tlbCsr <> backend.io.frontendTlbCsr
186  frontend.io.csrCtrl <> backend.io.frontendCsrCtrl
187  frontend.io.fencei <> fenceio.fencei
188
189  backend.io.fromTop.hartId := io.hartId
190  backend.io.fromTop.externalInterrupt.msip := outer.clint_int_sink.in.head._1(0)
191  backend.io.fromTop.externalInterrupt.mtip := outer.clint_int_sink.in.head._1(1)
192  backend.io.fromTop.externalInterrupt.meip := outer.plic_int_sink.in.head._1(0)
193  backend.io.fromTop.externalInterrupt.seip := outer.plic_int_sink.in.last._1(0)
194  backend.io.fromTop.externalInterrupt.debug := outer.debug_int_sink.in.head._1(0)
195
196  backend.io.frontendCsrDistributedUpdate := frontend.io.csrUpdate
197
198  backend.io.mem.stIn.zip(memBlock.io.stIn).foreach { case (sink, source) =>
199    sink.valid := source.valid
200    sink.bits := 0.U.asTypeOf(sink.bits)
201    sink.bits.robIdx := source.bits.uop.robIdx
202    sink.bits.ssid := source.bits.uop.ssid
203    sink.bits.storeSetHit := source.bits.uop.storeSetHit
204    // The other signals have not been used
205  }
206  backend.io.mem.memoryViolation <> memBlock.io.memoryViolation
207  backend.io.mem.lsqEnqIO <> memBlock.io.enqLsq
208  backend.io.mem.sqDeq := memBlock.io.sqDeq
209  backend.io.mem.lqCancelCnt := memBlock.io.lqCancelCnt
210  backend.io.mem.sqCancelCnt := memBlock.io.sqCancelCnt
211  backend.io.mem.otherFastWakeup := memBlock.io.otherFastWakeup
212  backend.io.mem.writeBack <> memBlock.io.writeback
213
214  frontend.io.reset_vector := io.reset_vector
215
216  io.cpu_halt := backend.io.toTop.cpuHalted
217
218  // memblock error exception writeback, 1 cycle after normal writeback
219  backend.io.mem.s3_delayed_load_error <> memBlock.io.s3_delayed_load_error
220
221  io.beu_errors.icache <> frontend.io.error.toL1BusErrorUnitInfo()
222  io.beu_errors.dcache <> memBlock.io.error.toL1BusErrorUnitInfo()
223
224  memBlock.io.hartId := io.hartId
225  memBlock.io.issue <> backend.io.mem.issueUops
226  // By default, instructions do not have exceptions when they enter the function units.
227  memBlock.io.issue.map(_.bits.uop.clearExceptions())
228  backend.io.mem.loadFastMatch <> memBlock.io.loadFastMatch
229  backend.io.mem.loadFastImm <> memBlock.io.loadFastImm
230  backend.io.mem.exceptionVAddr := memBlock.io.lsqio.exceptionAddr.vaddr
231  backend.io.mem.csrDistributedUpdate := memBlock.io.csrUpdate
232
233  backend.io.perf.frontendInfo := frontend.io.frontendInfo
234  backend.io.perf.memInfo := memBlock.io.memInfo
235  backend.io.perf.perfEventsFrontend := frontend.getPerf
236  backend.io.perf.perfEventsLsu := memBlock.getPerf
237  backend.io.perf.perfEventsHc := io.perfEvents
238
239  //  XSPerfHistogram("fastIn_count", PopCount(allFastUop1.map(_.valid)), true.B, 0, allFastUop1.length, 1)
240//  XSPerfHistogram("wakeup_count", PopCount(rfWriteback.map(_.valid)), true.B, 0, rfWriteback.length, 1)
241
242//  ctrlBlock.perfinfo.perfEventsEu0 := intExuBlock.getPerf.dropRight(outer.intExuBlock.scheduler.numRs)
243//  ctrlBlock.perfinfo.perfEventsEu1 := vecExuBlock.getPerf.dropRight(outer.vecExuBlock.scheduler.numRs)
244  if (!coreParams.softPTW) {
245    memBlock.io.perfEventsPTW := ptw.getPerf
246  } else {
247    memBlock.io.perfEventsPTW := DontCare
248  }
249//  ctrlBlock.perfinfo.perfEventsRs  := outer.exuBlocks.flatMap(b => b.module.getPerf.takeRight(b.scheduler.numRs))
250
251  memBlock.io.sfence <> backend.io.mem.sfence
252  memBlock.io.fenceToSbuffer <> backend.io.mem.toSbuffer
253
254  memBlock.io.redirect <> backend.io.mem.redirect
255  memBlock.io.rsfeedback <> backend.io.mem.rsFeedBack
256  memBlock.io.csrCtrl <> backend.io.mem.csrCtrl
257  memBlock.io.tlbCsr <> backend.io.mem.tlbCsr
258  memBlock.io.lsqio.rob <> backend.io.mem.robLsqIO
259  memBlock.io.lsqio.exceptionAddr.isStore := backend.io.mem.isStoreException
260
261  val itlbRepeater1 = PTWFilter(itlbParams.fenceDelay,frontend.io.ptw, fenceio.sfence, backend.io.tlb, l2tlbParams.ifilterSize)
262  val itlbRepeater2 = PTWRepeaterNB(passReady = false, itlbParams.fenceDelay, itlbRepeater1.io.ptw, ptw.io.tlb(0), fenceio.sfence, backend.io.tlb)
263  val dtlbRepeater1  = PTWFilter(ldtlbParams.fenceDelay, memBlock.io.ptw, fenceio.sfence, backend.io.tlb, l2tlbParams.dfilterSize)
264  val dtlbRepeater2  = PTWRepeaterNB(passReady = false, ldtlbParams.fenceDelay, dtlbRepeater1.io.ptw, ptw.io.tlb(1), fenceio.sfence, backend.io.tlb)
265  ptw.io.sfence <> fenceio.sfence
266  ptw.io.csr.tlb <> backend.io.tlb
267  ptw.io.csr.distribute_csr <> backend.io.csrCustomCtrl.distribute_csr
268
269  // if l2 prefetcher use stream prefetch, it should be placed in XSCore
270  io.l2_pf_enable := backend.io.csrCustomCtrl.l2_pf_enable
271
272  // Modules are reset one by one
273  val resetTree = ResetGenNode(
274    Seq(
275      ModuleNode(memBlock), ModuleNode(dtlbRepeater1),
276      ResetGenNode(Seq(
277        ModuleNode(itlbRepeater2),
278        ModuleNode(ptw),
279        ModuleNode(dtlbRepeater2),
280        ModuleNode(ptw_to_l2_buffer),
281      )),
282      ResetGenNode(Seq(
283        ModuleNode(backend),
284        ResetGenNode(Seq(
285          ResetGenNode(Seq(
286            ModuleNode(frontend), ModuleNode(itlbRepeater1)
287          ))
288        ))
289      ))
290    )
291  )
292
293  ResetGen(resetTree, reset, !debugOpts.FPGAPlatform)
294
295}
296