xref: /XiangShan/src/main/scala/system/SoC.scala (revision 57bb43b5f11c3f1e89ac52f232fe73056b35d9bd)
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 system
18
19import chipsalliance.rocketchip.config.{Field, Parameters}
20import chisel3._
21import chisel3.util._
22import device.{DebugModule, TLPMA, TLPMAIO}
23import freechips.rocketchip.devices.tilelink.{CLINT, CLINTParams, DevNullParams, PLICParams, TLError, TLPLIC}
24import freechips.rocketchip.diplomacy.{AddressSet, IdRange, InModuleBody, LazyModule, LazyModuleImp, MemoryDevice, RegionType, SimpleDevice, TransferSizes}
25import freechips.rocketchip.interrupts.{IntSourceNode, IntSourcePortSimple}
26import freechips.rocketchip.regmapper.{RegField, RegFieldAccessType, RegFieldDesc, RegFieldGroup}
27import utils.{BinaryArbiter, TLEdgeBuffer}
28import xiangshan.{DebugOptionsKey, HasXSParameter, XSBundle, XSCore, XSCoreParameters, XSTileKey}
29import freechips.rocketchip.amba.axi4._
30import freechips.rocketchip.tilelink._
31import top.BusPerfMonitor
32import xiangshan.backend.fu.PMAConst
33import huancun._
34import huancun.debug.TLLogger
35
36case object SoCParamsKey extends Field[SoCParameters]
37
38case class SoCParameters
39(
40  EnableILA: Boolean = false,
41  PAddrBits: Int = 36,
42  extIntrs: Int = 64,
43  L3NBanks: Int = 4,
44  L3CacheParamsOpt: Option[HCCacheParameters] = Some(HCCacheParameters(
45    name = "l3",
46    level = 3,
47    ways = 8,
48    sets = 2048 // 1MB per bank
49  ))
50){
51  // L3 configurations
52  val L3InnerBusWidth = 256
53  val L3BlockSize = 64
54  // on chip network configurations
55  val L3OuterBusWidth = 256
56}
57
58trait HasSoCParameter {
59  implicit val p: Parameters
60
61  val soc = p(SoCParamsKey)
62  val debugOpts = p(DebugOptionsKey)
63  val tiles = p(XSTileKey)
64
65  val NumCores = tiles.size
66  val EnableILA = soc.EnableILA
67
68  // L3 configurations
69  val L3InnerBusWidth = soc.L3InnerBusWidth
70  val L3BlockSize = soc.L3BlockSize
71  val L3NBanks = soc.L3NBanks
72
73  // on chip network configurations
74  val L3OuterBusWidth = soc.L3OuterBusWidth
75
76  val NrExtIntr = soc.extIntrs
77}
78
79class ILABundle extends Bundle {}
80
81
82abstract class BaseSoC()(implicit p: Parameters) extends LazyModule with HasSoCParameter {
83  val bankedNode = BankBinder(L3NBanks, L3BlockSize)
84  val peripheralXbar = TLXbar()
85  val l3_xbar = TLXbar()
86  val l3_banked_xbar = TLXbar()
87}
88
89// We adapt the following three traits from rocket-chip.
90// Source: rocket-chip/src/main/scala/subsystem/Ports.scala
91trait HaveSlaveAXI4Port {
92  this: BaseSoC =>
93
94  val idBits = 14
95
96  val l3FrontendAXI4Node = AXI4MasterNode(Seq(AXI4MasterPortParameters(
97    Seq(AXI4MasterParameters(
98      name = "dma",
99      id = IdRange(0, 1 << idBits)
100    ))
101  )))
102  private val errorDevice = LazyModule(new TLError(
103    params = DevNullParams(
104      address = Seq(AddressSet(0x0, 0x7fffffffL)),
105      maxAtomic = 8,
106      maxTransfer = 64),
107    beatBytes = L3InnerBusWidth / 8
108  ))
109  private val error_xbar = TLXbar()
110
111  l3_xbar :=
112    TLFIFOFixer() :=
113    TLWidthWidget(32) :=
114    AXI4ToTL() :=
115    AXI4UserYanker(Some(1)) :=
116    AXI4Fragmenter() :=
117    AXI4Buffer() :=
118    AXI4Buffer() :=
119    AXI4IdIndexer(1) :=
120    l3FrontendAXI4Node
121  errorDevice.node := l3_xbar
122
123  val dma = InModuleBody {
124    l3FrontendAXI4Node.makeIOs()
125  }
126}
127
128trait HaveAXI4MemPort {
129  this: BaseSoC =>
130  val device = new MemoryDevice
131  // 36-bit physical address
132  val memRange = AddressSet(0x00000000L, 0xfffffffffL).subtract(AddressSet(0x0L, 0x7fffffffL))
133  val memAXI4SlaveNode = AXI4SlaveNode(Seq(
134    AXI4SlavePortParameters(
135      slaves = Seq(
136        AXI4SlaveParameters(
137          address = memRange,
138          regionType = RegionType.UNCACHED,
139          executable = true,
140          supportsRead = TransferSizes(1, L3BlockSize),
141          supportsWrite = TransferSizes(1, L3BlockSize),
142          interleavedId = Some(0),
143          resources = device.reg("mem")
144        )
145      ),
146      beatBytes = L3OuterBusWidth / 8
147    )
148  ))
149
150  val mem_xbar = TLXbar()
151  mem_xbar :=*
152    TLXbar() :=*
153    TLBuffer.chainNode(2) :=*
154    TLCacheCork() :=*
155    bankedNode
156
157  mem_xbar :=
158    TLWidthWidget(8) :=
159    TLBuffer.chainNode(3, name = Some("PeripheralXbar_to_MemXbar_buffer")) :=
160    peripheralXbar
161
162  memAXI4SlaveNode :=
163    AXI4Buffer() :=
164    AXI4Buffer() :=
165    AXI4Buffer() :=
166    AXI4IdIndexer(idBits = 14) :=
167    AXI4UserYanker() :=
168    AXI4Deinterleaver(L3BlockSize) :=
169    TLToAXI4() :=
170    TLSourceShrinker(64) :=
171    TLWidthWidget(L3OuterBusWidth / 8) :=
172    TLBuffer.chainNode(2) :=
173    mem_xbar
174
175  val memory = InModuleBody {
176    memAXI4SlaveNode.makeIOs()
177  }
178}
179
180trait HaveAXI4PeripheralPort { this: BaseSoC =>
181  // on-chip devices: 0x3800_0000 - 0x3fff_ffff 0x0000_0000 - 0x0000_0fff
182  val onChipPeripheralRange = AddressSet(0x38000000L, 0x07ffffffL)
183  val uartRange = AddressSet(0x40600000, 0xf)
184  val uartDevice = new SimpleDevice("serial", Seq("xilinx,uartlite"))
185  val uartParams = AXI4SlaveParameters(
186    address = Seq(uartRange),
187    regionType = RegionType.UNCACHED,
188    supportsRead = TransferSizes(1, 8),
189    supportsWrite = TransferSizes(1, 8),
190    resources = uartDevice.reg
191  )
192  val peripheralRange = AddressSet(
193    0x0, 0x7fffffff
194  ).subtract(onChipPeripheralRange).flatMap(x => x.subtract(uartRange))
195  val peripheralNode = AXI4SlaveNode(Seq(AXI4SlavePortParameters(
196    Seq(AXI4SlaveParameters(
197      address = peripheralRange,
198      regionType = RegionType.UNCACHED,
199      supportsRead = TransferSizes(1, 8),
200      supportsWrite = TransferSizes(1, 8),
201      interleavedId = Some(0)
202    ), uartParams),
203    beatBytes = 8
204  )))
205
206  peripheralNode :=
207    AXI4IdIndexer(idBits = 2) :=
208    AXI4Buffer() :=
209    AXI4Buffer() :=
210    AXI4Buffer() :=
211    AXI4Buffer() :=
212    AXI4UserYanker() :=
213    AXI4Deinterleaver(8) :=
214    TLToAXI4() :=
215    TLBuffer.chainNode(3) :=
216    peripheralXbar
217
218  val peripheral = InModuleBody {
219    peripheralNode.makeIOs()
220  }
221
222}
223
224class SoCMisc()(implicit p: Parameters) extends BaseSoC
225  with HaveAXI4MemPort
226  with HaveAXI4PeripheralPort
227  with PMAConst
228  with HaveSlaveAXI4Port
229{
230  val peripheral_ports = Array.fill(NumCores) { TLTempNode() }
231  val core_to_l3_ports = Array.fill(NumCores) { TLTempNode() }
232
233  val l3_in = TLTempNode()
234  val l3_out = TLTempNode()
235  val l3_mem_pmu = BusPerfMonitor(enable = !debugOpts.FPGAPlatform)
236
237  l3_in :*= TLEdgeBuffer(_ => true, Some("L3_in_buffer")) :*= l3_banked_xbar
238  bankedNode :*= TLLogger("MEM_L3", !debugOpts.FPGAPlatform) :*= l3_mem_pmu :*= l3_out
239
240  if(soc.L3CacheParamsOpt.isEmpty){
241    l3_out :*= l3_in
242  }
243
244  for(port <- peripheral_ports) {
245    peripheralXbar := TLBuffer.chainNode(2, Some("L2_to_L3_peripheral_buffer")) := port
246  }
247
248  for ((core_out, i) <- core_to_l3_ports.zipWithIndex){
249    l3_banked_xbar :=*
250      TLLogger(s"L3_L2_$i", !debugOpts.FPGAPlatform) :=*
251      TLBuffer() :=
252      core_out
253  }
254  l3_banked_xbar := TLBuffer.chainNode(2) := l3_xbar
255
256  val clint = LazyModule(new CLINT(CLINTParams(0x38000000L), 8))
257  clint.node := peripheralXbar
258
259  class IntSourceNodeToModule(val num: Int)(implicit p: Parameters) extends LazyModule {
260    val sourceNode = IntSourceNode(IntSourcePortSimple(num, ports = 1, sources = 1))
261    lazy val module = new LazyModuleImp(this){
262      val in = IO(Input(Vec(num, Bool())))
263      in.zip(sourceNode.out.head._1).foreach{ case (i, s) => s := i }
264    }
265  }
266
267  val plic = LazyModule(new TLPLIC(PLICParams(0x3c000000L), 8))
268  val plicSource = LazyModule(new IntSourceNodeToModule(NrExtIntr))
269
270  plic.intnode := plicSource.sourceNode
271  plic.node := peripheralXbar
272
273  val pll_node = TLRegisterNode(
274    address = Seq(AddressSet(0x3a000000L, 0xfff)),
275    device = new SimpleDevice("pll_ctrl", Seq()),
276    beatBytes = 8,
277    concurrency = 1
278  )
279  pll_node := peripheralXbar
280
281  val debugModule = LazyModule(new DebugModule(NumCores)(p))
282  debugModule.debug.node := peripheralXbar
283  debugModule.debug.dmInner.dmInner.sb2tlOpt.foreach { sb2tl  =>
284    l3_xbar := TLBuffer() := sb2tl.node
285  }
286
287  val pma = LazyModule(new TLPMA)
288  pma.node :=
289    TLBuffer.chainNode(4) :=
290    peripheralXbar
291
292  lazy val module = new LazyModuleImp(this){
293
294    val debug_module_io = IO(chiselTypeOf(debugModule.module.io))
295    val ext_intrs = IO(Input(UInt(NrExtIntr.W)))
296    val pll0_lock = IO(Input(Bool()))
297    val pll0_ctrl = IO(Output(Vec(6, UInt(32.W))))
298    val cacheable_check = IO(new TLPMAIO)
299
300    val ext_intrs_sync = RegNext(RegNext(RegNext(ext_intrs)))
301    val ext_intrs_wire = Wire(UInt(NrExtIntr.W))
302    ext_intrs_wire := ext_intrs_sync
303    debugModule.module.io <> debug_module_io
304    plicSource.module.in := ext_intrs_wire.asBools
305    pma.module.io <> cacheable_check
306
307    val freq = 100
308    val cnt = RegInit(freq.U)
309    val tick = cnt === 0.U
310    cnt := Mux(tick, freq.U, cnt - 1.U)
311    clint.module.io.rtcTick := tick
312
313    val pll_ctrl_regs = Seq.fill(6){ RegInit(0.U(32.W)) }
314    val pll_lock = RegNext(next = pll0_lock, init = false.B)
315
316    pll0_ctrl <> VecInit(pll_ctrl_regs)
317
318    pll_node.regmap(
319      0x000 -> RegFieldGroup(
320        "Pll", Some("PLL ctrl regs"),
321        pll_ctrl_regs.zipWithIndex.map{
322          case (r, i) => RegField(32, r, RegFieldDesc(
323            s"PLL_ctrl_$i",
324            desc = s"PLL ctrl register #$i"
325          ))
326        } :+ RegField.r(32, Cat(0.U(31.W), pll_lock), RegFieldDesc(
327          "PLL_lock",
328          "PLL lock register"
329        ))
330      )
331    )
332  }
333}
334