xref: /XiangShan/src/main/scala/system/SoC.scala (revision c17003d25cda7f7d4824d462cffbd0413d277407)
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      cacheName = s"L2",
80      enablePerf = env.EnablePerfDebug && !env.FPGAPlatform
81    ),
82    InclusiveCacheMicroParameters(
83      writeBytes = 32
84    )
85  )))
86
87  private val l2prefetcher = Seq.fill(NumCores)(LazyModule(new L2Prefetcher()))
88
89  // L2 to L3 network
90  // -------------------------------------------------
91  private val l3_xbar = TLXbar()
92
93  private val l3_node = LazyModule(new InclusiveCache(
94    CacheParameters(
95      level = 3,
96      ways = L3NWays,
97      sets = L3NSets,
98      blockBytes = L3BlockSize,
99      beatBytes = L2BusWidth / 8,
100      cacheName = "L3",
101      enablePerf = env.EnablePerfDebug && !env.FPGAPlatform
102    ),
103    InclusiveCacheMicroParameters(
104      writeBytes = 32
105    )
106  )).node
107
108  // L3 to memory network
109  // -------------------------------------------------
110  private val memory_xbar = TLXbar()
111  private val mmioXbar = TLXbar()
112
113  // only mem, dma and extDev are visible externally
114  val mem = Seq.fill(L3NBanks)(AXI4IdentityNode())
115  val dma = AXI4IdentityNode()
116  val extDev = AXI4IdentityNode()
117
118  // connections
119  // -------------------------------------------------
120  for (i <- 0 until NumCores) {
121    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).memBlock.dcache.clientNode
122    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).l1pluscache.clientNode
123    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := xs_core(i).ptw.node
124    l2_xbar(i) := TLBuffer() := DebugIdentityNode() := l2prefetcher(i).clientNode
125
126    mmioXbar   := TLBuffer() := DebugIdentityNode() := xs_core(i).memBlock.uncache.clientNode
127    mmioXbar   := TLBuffer() := DebugIdentityNode() := xs_core(i).frontend.instrUncache.clientNode
128    l2cache(i).node := DataDontCareNode(a = true, b = true) := TLBuffer() := DebugIdentityNode() := l2_xbar(i)
129    l3_xbar := TLBuffer() := DebugIdentityNode() := l2cache(i).node
130  }
131
132  // DMA should not go to MMIO
133  val mmioRange = AddressSet(base = 0x0000000000L, mask = 0x007fffffffL)
134  // AXI4ToTL needs a TLError device to route error requests,
135  // add one here to make it happy.
136  val tlErrorParams = DevNullParams(
137    address = Seq(mmioRange),
138    maxAtomic = 8,
139    maxTransfer = 64)
140  val tlError = LazyModule(new TLError(params = tlErrorParams, beatBytes = L2BusWidth / 8))
141  private val tlError_xbar = TLXbar()
142  tlError_xbar :=
143    AXI4ToTL() :=
144    AXI4UserYanker(Some(1)) :=
145    AXI4Fragmenter() :=
146    AXI4IdIndexer(1) :=
147    dma
148  tlError.node := tlError_xbar
149
150  l3_xbar :=
151    TLBuffer() :=
152    DebugIdentityNode() :=
153    tlError_xbar
154
155  val bankedNode =
156    BankBinder(L3NBanks, L3BlockSize) :*= l3_node :*= TLBuffer() :*= DebugIdentityNode() :*= l3_xbar
157
158  for(i <- 0 until L3NBanks) {
159    mem(i) :=
160      AXI4UserYanker() :=
161      TLToAXI4() :=
162      TLWidthWidget(L3BusWidth / 8) :=
163      TLBuffer() :=
164      TLCacheCork() :=
165      bankedNode
166  }
167
168  private val clint = LazyModule(new TLTimer(
169    Seq(AddressSet(0x38000000L, 0x0000ffffL)),
170    sim = !env.FPGAPlatform
171  ))
172
173  clint.node := mmioXbar
174  extDev := AXI4UserYanker() := TLToAXI4() := mmioXbar
175
176  val fakeTreeNode = new GenericLogicalTreeNode
177
178  val beu = LazyModule(
179    new BusErrorUnit(new XSL1BusErrors(NumCores), BusErrorUnitParams(0x38010000), fakeTreeNode))
180  beu.node := mmioXbar
181
182  class BeuSinkNode()(implicit p: Parameters) extends LazyModule {
183    val intSinkNode = IntSinkNode(IntSinkPortSimple())
184    lazy val module = new LazyModuleImp(this){
185      val interrupt = IO(Output(Bool()))
186      interrupt := intSinkNode.in.head._1.head
187    }
188  }
189  val beuSink = LazyModule(new BeuSinkNode())
190  beuSink.intSinkNode := beu.intNode
191
192  val plic = LazyModule(new AXI4Plic(
193    Seq(AddressSet(0x3c000000L, 0x03ffffffL)),
194    sim = !env.FPGAPlatform
195  ))
196  plic.node := AXI4UserYanker() := TLToAXI4() := mmioXbar
197
198  lazy val module = new LazyModuleImp(this){
199    val io = IO(new Bundle{
200      val extIntrs = Input(UInt(NrExtIntr.W))
201      // val meip = Input(Vec(NumCores, Bool()))
202      val ila = if(env.FPGAPlatform && EnableILA) Some(Output(new ILABundle)) else None
203    })
204    val difftestIO0 = IO(new DifftestBundle())
205    val difftestIO1 = IO(new DifftestBundle())
206    val difftestIO = Seq(difftestIO0, difftestIO1)
207
208    val trapIO0 = IO(new xiangshan.TrapIO())
209    val trapIO1 = IO(new xiangshan.TrapIO())
210    val trapIO = Seq(trapIO0, trapIO1)
211
212    plic.module.io.extra.get.intrVec <> RegNext(beuSink.module.interrupt)
213
214    for (i <- 0 until NumCores) {
215      xs_core(i).module.io.hartId := i.U
216      xs_core(i).module.io.externalInterrupt.mtip := clint.module.io.mtip(i)
217      xs_core(i).module.io.externalInterrupt.msip := clint.module.io.msip(i)
218      beu.module.io.errors.l1plus(i) := RegNext(xs_core(i).module.io.l1plus_error)
219      beu.module.io.errors.icache(i) := RegNext(xs_core(i).module.io.icache_error)
220      beu.module.io.errors.dcache(i) := RegNext(xs_core(i).module.io.dcache_error)
221      // xs_core(i).module.io.externalInterrupt.meip := RegNext(RegNext(io.meip(i)))
222      xs_core(i).module.io.externalInterrupt.meip := plic.module.io.extra.get.meip(i)
223      l2prefetcher(i).module.io.enable := RegNext(xs_core(i).module.io.l2_pf_enable)
224      l2prefetcher(i).module.io.in <> l2cache(i).module.io
225    }
226
227    difftestIO0 <> xs_core(0).module.difftestIO
228    difftestIO1 <> DontCare
229    trapIO0 <> xs_core(0).module.trapIO
230    trapIO1 <> DontCare
231
232    if (env.DualCore) {
233      difftestIO1 <> xs_core(1).module.difftestIO
234      trapIO1 <> xs_core(1).module.trapIO
235    }
236    // do not let dma AXI signals optimized out
237    dontTouch(dma.out.head._1)
238    dontTouch(extDev.out.head._1)
239    dontTouch(io.extIntrs)
240  }
241
242}
243