xref: /XiangShan/src/main/scala/system/SoC.scala (revision 9d0addb22faa559f3bb1eebc4850ca6a568c7c4d)
1package system
2
3import chipsalliance.rocketchip.config.Parameters
4import device.{AXI4Plic, AXI4Timer, TLTimer}
5import chisel3._
6import chisel3.util._
7import freechips.rocketchip.diplomacy.{AddressSet, LazyModule, LazyModuleImp}
8import freechips.rocketchip.tilelink.{BankBinder, TLBuffer, TLBundleParameters, TLCacheCork, TLClientNode, TLFilter, TLFuzzer, TLIdentityNode, TLToAXI4, TLWidthWidget, TLXbar}
9import utils.{DataDontCareNode, DebugIdentityNode}
10import utils.XSInfo
11import xiangshan.{DifftestBundle, HasXSLog, HasXSParameter, XSBundle, XSCore}
12import xiangshan.cache.prefetch._
13import sifive.blocks.inclusivecache.{CacheParameters, InclusiveCache, InclusiveCacheMicroParameters}
14import freechips.rocketchip.diplomacy.{AddressSet, LazyModule, LazyModuleImp}
15import freechips.rocketchip.devices.tilelink.{DevNullParams, TLError}
16import freechips.rocketchip.amba.axi4.{AXI4Deinterleaver, AXI4Fragmenter, AXI4IdIndexer, AXI4IdentityNode, AXI4ToTL, AXI4UserYanker}
17import freechips.rocketchip.diplomaticobjectmodel.logicaltree.GenericLogicalTreeNode
18import freechips.rocketchip.interrupts.{IntSinkNode, IntSinkParameters, IntSinkPortParameters, IntSinkPortSimple}
19import freechips.rocketchip.tile.{BusErrorUnit, BusErrorUnitParams, BusErrors, L1BusErrors}
20
21case class SoCParameters
22(
23  NumCores: Integer = 1,
24  EnableILA: Boolean = false,
25  HasL2Cache: Boolean = false,
26  HasPrefetch: Boolean = false
27)
28
29trait HasSoCParameter extends HasXSParameter{
30  val soc = top.Parameters.get.socParameters
31  val NumCores = soc.NumCores
32  val EnableILA = soc.EnableILA
33  val HasL2cache = soc.HasL2Cache
34  val HasPrefetch = soc.HasPrefetch
35}
36
37class ILABundle extends Bundle {}
38
39
40class L1CacheErrorInfo extends XSBundle{
41  val paddr = Valid(UInt(PAddrBits.W))
42  // for now, we only detect ecc
43  val ecc_error = Valid(Bool())
44}
45
46class XSL1BusErrors(val nCores: Int) extends  BusErrors {
47  val icache = Vec(nCores, new L1CacheErrorInfo)
48  val l1plus = Vec(nCores, new L1CacheErrorInfo)
49  val dcache = Vec(nCores, new L1CacheErrorInfo)
50
51  override def toErrorList: List[Option[(ValidIO[UInt], String, String)]] =
52    List.tabulate(nCores){i =>
53      List(
54        Some(icache(i).paddr, s"IBUS_$i", s"Icache_$i bus error"),
55        Some(icache(i).ecc_error, s"I_ECC_$i", s"Icache_$i ecc error"),
56        Some(l1plus(i).paddr, s"L1PLUS_$i", s"L1PLUS_$i bus error"),
57        Some(l1plus(i).ecc_error, s"L1PLUS_ECC_$i", s"L1PLUS_$i ecc error"),
58        Some(dcache(i).paddr, s"DBUS_$i", s"Dcache_$i bus error"),
59        Some(dcache(i).ecc_error, s"D_ECC_$i", s"Dcache_$i ecc error")
60      )
61    }.flatten
62}
63
64class XSSoc()(implicit p: Parameters) extends LazyModule with HasSoCParameter {
65  // CPU Cores
66  private val xs_core = Seq.fill(NumCores)(LazyModule(new XSCore()))
67
68  // L1 to L2 network
69  // -------------------------------------------------
70  private val l2_xbar = Seq.fill(NumCores)(TLXbar())
71
72  private val l2cache = Seq.fill(NumCores)(LazyModule(new InclusiveCache(
73    CacheParameters(
74      level = 2,
75      ways = L2NWays,
76      sets = L2NSets,
77      blockBytes = L2BlockSize,
78      beatBytes = L1BusWidth / 8, // beatBytes = l1BusDataWidth / 8
79      replacement = "plru",
80      cacheName = s"L2",
81      enablePerf = env.EnablePerfDebug && !env.FPGAPlatform
82    ),
83    InclusiveCacheMicroParameters(
84      writeBytes = 32
85    )
86  )))
87
88  private val l2prefetcher = Seq.fill(NumCores)(LazyModule(new L2Prefetcher()))
89
90  // L2 to L3 network
91  // -------------------------------------------------
92  private val l3_xbar = TLXbar()
93
94  private val l3_node = LazyModule(new InclusiveCache(
95    CacheParameters(
96      level = 3,
97      ways = L3NWays,
98      sets = L3NSets,
99      blockBytes = L3BlockSize,
100      beatBytes = L2BusWidth / 8,
101      replacement = "plru",
102      cacheName = "L3",
103      enablePerf = env.EnablePerfDebug && !env.FPGAPlatform
104    ),
105    InclusiveCacheMicroParameters(
106      writeBytes = 32
107    )
108  )).node
109
110  // L3 to memory network
111  // -------------------------------------------------
112  private val memory_xbar = TLXbar()
113  private val mmioXbar = TLXbar()
114
115  // only mem, dma and extDev are visible externally
116  val mem = Seq.fill(L3NBanks)(AXI4IdentityNode())
117  val dma = AXI4IdentityNode()
118  val extDev = AXI4IdentityNode()
119
120  // connections
121  // -------------------------------------------------
122  for (i <- 0 until NumCores) {
123    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).memBlock.dcache.clientNode
124    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).l1pluscache.clientNode
125    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).ptw.node
126    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := l2prefetcher(i).clientNode
127
128    mmioXbar   := TLBuffer() := DebugIdentityNode() := xs_core(i).memBlock.uncache.clientNode
129    mmioXbar   := TLBuffer() := DebugIdentityNode() := xs_core(i).frontend.instrUncache.clientNode
130    l2cache(i).node := DataDontCareNode(a = true, b = true) := TLBuffer() := DebugIdentityNode() := l2_xbar(i)
131    l3_xbar := TLBuffer() := DebugIdentityNode() := l2cache(i).node
132  }
133
134  // DMA should not go to MMIO
135  val mmioRange = AddressSet(base = 0x0000000000L, mask = 0x007fffffffL)
136  // AXI4ToTL needs a TLError device to route error requests,
137  // add one here to make it happy.
138  val tlErrorParams = DevNullParams(
139    address = Seq(mmioRange),
140    maxAtomic = 8,
141    maxTransfer = 64)
142  val tlError = LazyModule(new TLError(params = tlErrorParams, beatBytes = L2BusWidth / 8))
143  private val tlError_xbar = TLXbar()
144  tlError_xbar :=
145    AXI4ToTL() :=
146    AXI4UserYanker(Some(1)) :=
147    AXI4Fragmenter() :=
148    AXI4IdIndexer(1) :=
149    dma
150  tlError.node := tlError_xbar
151
152  l3_xbar :=
153    TLBuffer() :=
154    DebugIdentityNode() :=
155    tlError_xbar
156
157  val bankedNode =
158    BankBinder(L3NBanks, L3BlockSize) :*= l3_node :*= TLBuffer() :*= DebugIdentityNode() :*= l3_xbar
159
160  for(i <- 0 until L3NBanks) {
161    mem(i) :=
162      AXI4UserYanker() :=
163      TLToAXI4() :=
164      TLWidthWidget(L3BusWidth / 8) :=
165      TLBuffer() :=
166      TLCacheCork() :=
167      bankedNode
168  }
169
170  private val clint = LazyModule(new TLTimer(
171    Seq(AddressSet(0x38000000L, 0x0000ffffL)),
172    sim = !env.FPGAPlatform
173  ))
174
175  clint.node := mmioXbar
176  extDev := AXI4UserYanker() := TLToAXI4() := mmioXbar
177
178  val fakeTreeNode = new GenericLogicalTreeNode
179
180  val beu = LazyModule(
181    new BusErrorUnit(new XSL1BusErrors(NumCores), BusErrorUnitParams(0x38010000), fakeTreeNode))
182  beu.node := mmioXbar
183
184  class BeuSinkNode()(implicit p: Parameters) extends LazyModule {
185    val intSinkNode = IntSinkNode(IntSinkPortSimple())
186    lazy val module = new LazyModuleImp(this){
187      val interrupt = IO(Output(Bool()))
188      interrupt := intSinkNode.in.head._1.head
189    }
190  }
191  val beuSink = LazyModule(new BeuSinkNode())
192  beuSink.intSinkNode := beu.intNode
193
194  val plic = LazyModule(new AXI4Plic(
195    Seq(AddressSet(0x3c000000L, 0x03ffffffL)),
196    sim = !env.FPGAPlatform
197  ))
198  plic.node := AXI4UserYanker() := TLToAXI4() := mmioXbar
199
200  lazy val module = new LazyModuleImp(this){
201    val io = IO(new Bundle{
202      val extIntrs = Input(UInt(NrExtIntr.W))
203      // val meip = Input(Vec(NumCores, Bool()))
204      val ila = if(env.FPGAPlatform && EnableILA) Some(Output(new ILABundle)) else None
205    })
206    val difftestIO0 = IO(new DifftestBundle())
207    val difftestIO1 = IO(new DifftestBundle())
208    val difftestIO = Seq(difftestIO0, difftestIO1)
209
210    val trapIO0 = IO(new xiangshan.TrapIO())
211    val trapIO1 = IO(new xiangshan.TrapIO())
212    val trapIO = Seq(trapIO0, trapIO1)
213
214    plic.module.io.extra.get.intrVec <> RegNext(beuSink.module.interrupt)
215
216    for (i <- 0 until NumCores) {
217      xs_core(i).module.io.hartId := i.U
218      xs_core(i).module.io.externalInterrupt.mtip := clint.module.io.mtip(i)
219      xs_core(i).module.io.externalInterrupt.msip := clint.module.io.msip(i)
220      beu.module.io.errors.l1plus(i) := RegNext(xs_core(i).module.io.l1plus_error)
221      beu.module.io.errors.icache(i) := RegNext(xs_core(i).module.io.icache_error)
222      beu.module.io.errors.dcache(i) := RegNext(xs_core(i).module.io.dcache_error)
223      // xs_core(i).module.io.externalInterrupt.meip := RegNext(RegNext(io.meip(i)))
224      xs_core(i).module.io.externalInterrupt.meip := plic.module.io.extra.get.meip(i)
225      l2prefetcher(i).module.io.enable := RegNext(xs_core(i).module.io.l2_pf_enable)
226      l2prefetcher(i).module.io.in <> l2cache(i).module.io
227    }
228
229    difftestIO0 <> xs_core(0).module.difftestIO
230    difftestIO1 <> DontCare
231    trapIO0 <> xs_core(0).module.trapIO
232    trapIO1 <> DontCare
233
234    if (env.DualCore) {
235      difftestIO1 <> xs_core(1).module.difftestIO
236      trapIO1 <> xs_core(1).module.trapIO
237    }
238    // do not let dma AXI signals optimized out
239    dontTouch(dma.out.head._1)
240    dontTouch(extDev.out.head._1)
241    dontTouch(io.extIntrs)
242  }
243
244}
245