xref: /XiangShan/src/main/scala/xiangshan/XSTile.scala (revision dcbc69cb2a7ea07707ede3d8f7c74421ef450202)
1package xiangshan
2
3import chisel3._
4import chipsalliance.rocketchip.config.{Config, Parameters}
5import chisel3.util.{Valid, ValidIO}
6import freechips.rocketchip.diplomacy.{BundleBridgeSink, LazyModule, LazyModuleImp, LazyModuleImpLike}
7import freechips.rocketchip.diplomaticobjectmodel.logicaltree.GenericLogicalTreeNode
8import freechips.rocketchip.interrupts.{IntSinkNode, IntSinkPortParameters, IntSinkPortSimple}
9import freechips.rocketchip.tile.{BusErrorUnit, BusErrorUnitParams, BusErrors}
10import freechips.rocketchip.tilelink.{BankBinder, TLBuffer, TLIdentityNode, TLNode, TLTempNode, TLXbar}
11import huancun.debug.TLLogger
12import huancun.{HCCacheParamsKey, HuanCun}
13import system.HasSoCParameter
14import top.BusPerfMonitor
15import utils.{ResetGen, TLEdgeBuffer}
16
17class L1CacheErrorInfo(implicit val p: Parameters) extends Bundle with HasSoCParameter {
18  val paddr = Valid(UInt(soc.PAddrBits.W))
19  // for now, we only detect ecc
20  val ecc_error = Valid(Bool())
21}
22
23class XSL1BusErrors()(implicit val p: Parameters) extends BusErrors {
24  val icache = new L1CacheErrorInfo
25  val dcache = new L1CacheErrorInfo
26
27  override def toErrorList: List[Option[(ValidIO[UInt], String, String)]] =
28    List(
29      Some(icache.paddr, s"IBUS", s"Icache bus error"),
30      Some(icache.ecc_error, s"I_ECC", s"Icache ecc error"),
31      Some(dcache.paddr, s"DBUS", s"Dcache bus error"),
32      Some(dcache.ecc_error, s"D_ECC", s"Dcache ecc error")
33    )
34}
35
36/**
37  *   XSTileMisc contains every module except Core and L2 Cache
38  */
39class XSTileMisc()(implicit p: Parameters) extends LazyModule
40  with HasXSParameter
41  with HasSoCParameter
42{
43  val l1_xbar = TLXbar()
44  val mmio_xbar = TLXbar()
45  val memory_port = TLIdentityNode()
46  val beu = LazyModule(new BusErrorUnit(
47    new XSL1BusErrors(), BusErrorUnitParams(0x38010000), new GenericLogicalTreeNode
48  ))
49  val busPMU = BusPerfMonitor(enable = !debugOpts.FPGAPlatform)
50  val l1d_logger = TLLogger(s"L2_L1D_${coreParams.HartId}", !debugOpts.FPGAPlatform)
51  val l2_binder = coreParams.L2CacheParamsOpt.map(_ => BankBinder(coreParams.L2NBanks, 64))
52
53  val i_mmio_port = TLTempNode()
54  val d_mmio_port = TLTempNode()
55
56  busPMU := l1d_logger
57  l1_xbar :=* busPMU
58
59  l2_binder match {
60    case Some(binder) =>
61      memory_port :=* TLEdgeBuffer(_ => true, Some("l2_to_l3_buffer")) :=* binder
62    case None =>
63      memory_port := l1_xbar
64  }
65
66  mmio_xbar := TLBuffer() := i_mmio_port
67  mmio_xbar := TLBuffer() := d_mmio_port
68  beu.node := TLBuffer() := mmio_xbar
69
70  lazy val module = new LazyModuleImp(this){
71    val beu_errors = IO(Input(chiselTypeOf(beu.module.io.errors)))
72    beu.module.io.errors <> beu_errors
73  }
74}
75
76class XSTile()(implicit p: Parameters) extends LazyModule
77  with HasXSParameter
78  with HasSoCParameter
79{
80  private val core = LazyModule(new XSCore())
81  private val misc = LazyModule(new XSTileMisc())
82  private val l2cache = coreParams.L2CacheParamsOpt.map(l2param =>
83    LazyModule(new HuanCun()(new Config((_, _, _) => {
84      case HCCacheParamsKey => l2param
85    })))
86  )
87
88  // public ports
89  val memory_port = misc.memory_port
90  val uncache = misc.mmio_xbar
91  val clint_int_sink = core.clint_int_sink
92  val plic_int_sink = core.plic_int_sink
93  val debug_int_sink = core.debug_int_sink
94  val beu_int_source = misc.beu.intNode
95  val core_reset_sink = BundleBridgeSink(Some(() => Bool()))
96
97  if (coreParams.dcacheParametersOpt.nonEmpty) {
98    misc.l1d_logger :=
99      TLBuffer.chainNode(1, Some("L1D_to_L2_buffer")) :=
100      core.memBlock.dcache.clientNode
101  }
102  misc.busPMU :=
103    TLLogger(s"L2_L1I_${coreParams.HartId}", !debugOpts.FPGAPlatform) :=
104    TLBuffer.chainNode(1, Some("L1I_to_L2_buffer")) :=
105    core.frontend.icache.clientNode
106
107  if (!coreParams.softPTW) {
108    misc.busPMU :=
109      TLLogger(s"L2_PTW_${coreParams.HartId}", !debugOpts.FPGAPlatform) :=
110      TLBuffer.chainNode(3, Some("PTW_to_L2_buffer")) :=
111      core.ptw.node
112  }
113  l2cache match {
114    case Some(l2) =>
115      misc.l2_binder.get :*= l2.node :*= misc.l1_xbar
116    case None =>
117  }
118
119  misc.i_mmio_port := core.frontend.instrUncache.clientNode
120  misc.d_mmio_port := core.memBlock.uncache.clientNode
121
122  lazy val module = new LazyModuleImp(this){
123    val io = IO(new Bundle {
124      val hartId = Input(UInt(64.W))
125    })
126
127    dontTouch(io.hartId)
128
129    val core_soft_rst = core_reset_sink.in.head._1
130
131    core.module.io.hartId := io.hartId
132    if(l2cache.isDefined){
133      core.module.io.perfEvents <> l2cache.get.module.io.perfEvents.flatten
134    }
135    else {
136      core.module.io.perfEvents <> DontCare
137    }
138
139    misc.module.beu_errors <> core.module.io.beu_errors
140
141    // Modules are reset one by one
142    // io_reset ----
143    //             |
144    //             v
145    // reset ----> OR_SYNC --> {Misc, L2 Cache, Cores}
146    val l2cacheMod = if (l2cache.isDefined) Seq(l2cache.get.module) else Seq()
147    val resetChain = Seq(
148      Seq(misc.module, core.module) ++ l2cacheMod
149    )
150    ResetGen(resetChain, reset.asBool || core_soft_rst, !debugOpts.FPGAPlatform)
151  }
152}
153