xref: /XiangShan/src/main/scala/top/XSNoCTop.scala (revision 7ff4ebdc0266245ae92077cf8398595a3daa630a)
1720dd621STang Haojin/***************************************************************************************
2720dd621STang Haojin* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC)
3720dd621STang Haojin* Copyright (c) 2024 Institute of Computing Technology, Chinese Academy of Sciences
4720dd621STang Haojin*
5720dd621STang Haojin* XiangShan is licensed under Mulan PSL v2.
6720dd621STang Haojin* You can use this software according to the terms and conditions of the Mulan PSL v2.
7720dd621STang Haojin* You may obtain a copy of Mulan PSL v2 at:
8720dd621STang Haojin*          http://license.coscl.org.cn/MulanPSL2
9720dd621STang Haojin*
10720dd621STang Haojin* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11720dd621STang Haojin* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12720dd621STang Haojin* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13720dd621STang Haojin*
14720dd621STang Haojin* See the Mulan PSL v2 for more details.
15720dd621STang Haojin***************************************************************************************/
16720dd621STang Haojin
17720dd621STang Haojinpackage top
18720dd621STang Haojin
19720dd621STang Haojinimport chisel3._
20720dd621STang Haojinimport chisel3.util._
21720dd621STang Haojinimport xiangshan._
22720dd621STang Haojinimport utils._
23720dd621STang Haojinimport utility._
24720dd621STang Haojinimport system._
25720dd621STang Haojinimport device._
26720dd621STang Haojinimport org.chipsalliance.cde.config._
27720dd621STang Haojinimport freechips.rocketchip.amba.axi4._
28720dd621STang Haojinimport freechips.rocketchip.diplomacy._
29720dd621STang Haojinimport freechips.rocketchip.interrupts._
30720dd621STang Haojinimport freechips.rocketchip.tilelink._
318537b88aSTang Haojinimport coupledL2.tl2chi.{PortIO, CHIAsyncBridgeSink}
32720dd621STang Haojinimport freechips.rocketchip.tile.MaxHartIdBits
338537b88aSTang Haojinimport freechips.rocketchip.util.{AsyncQueueSource, AsyncQueueParams}
348e93c8f6STang Haojinimport chisel3.experimental.{annotate, ChiselAnnotation}
358e93c8f6STang Haojinimport sifive.enterprise.firrtl.NestedPrefixModulesAnnotation
36720dd621STang Haojin
37720dd621STang Haojinclass XSNoCTop()(implicit p: Parameters) extends BaseXSSoc with HasSoCParameter
38720dd621STang Haojin{
39720dd621STang Haojin  override lazy val desiredName: String = "XSTop"
40720dd621STang Haojin
41720dd621STang Haojin  ResourceBinding {
42720dd621STang Haojin    val width = ResourceInt(2)
43720dd621STang Haojin    val model = "freechips,rocketchip-unknown"
44720dd621STang Haojin    Resource(ResourceAnchors.root, "model").bind(ResourceString(model))
45720dd621STang Haojin    Resource(ResourceAnchors.root, "compat").bind(ResourceString(model + "-dev"))
46720dd621STang Haojin    Resource(ResourceAnchors.soc, "compat").bind(ResourceString(model + "-soc"))
47720dd621STang Haojin    Resource(ResourceAnchors.root, "width").bind(width)
48720dd621STang Haojin    Resource(ResourceAnchors.soc, "width").bind(width)
49720dd621STang Haojin    Resource(ResourceAnchors.cpus, "width").bind(ResourceInt(1))
50720dd621STang Haojin    def bindManagers(xbar: TLNexusNode) = {
51720dd621STang Haojin      ManagerUnification(xbar.edges.in.head.manager.managers).foreach{ manager =>
52720dd621STang Haojin        manager.resources.foreach(r => r.bind(manager.toResource))
53720dd621STang Haojin      }
54720dd621STang Haojin    }
55720dd621STang Haojin  }
56720dd621STang Haojin
57e2725c9eSzhanglinjuan  require(enableCHI)
58e2725c9eSzhanglinjuan
59720dd621STang Haojin  // xstile
608537b88aSTang Haojin  val core_with_l2 = LazyModule(new XSTileWrap()(p.alter((site, here, up) => {
61720dd621STang Haojin    case XSCoreParamsKey => tiles.head
62bb2f3f51STang Haojin    case PerfCounterOptionsKey => up(PerfCounterOptionsKey).copy(perfDBHartID = tiles.head.HartId)
63720dd621STang Haojin  })))
64720dd621STang Haojin
65720dd621STang Haojin  // imsic bus top
669143e232SJiuyue Ma  val u_imsic_bus_top = LazyModule(new imsic_bus_top(
679143e232SJiuyue Ma    useTL = soc.IMSICUseTL,
689143e232SJiuyue Ma    baseAddress = (0x3A800000, 0x3B000000)
699143e232SJiuyue Ma  ))
70720dd621STang Haojin
71720dd621STang Haojin  // interrupts
72720dd621STang Haojin  val clintIntNode = IntSourceNode(IntSourcePortSimple(1, 1, 2))
73720dd621STang Haojin  val debugIntNode = IntSourceNode(IntSourcePortSimple(1, 1, 1))
74720dd621STang Haojin  val plicIntNode = IntSourceNode(IntSourcePortSimple(1, 2, 1))
75720dd621STang Haojin  val beuIntNode = IntSinkNode(IntSinkPortSimple(1, 1))
768537b88aSTang Haojin  core_with_l2.clintIntNode := clintIntNode
778537b88aSTang Haojin  core_with_l2.debugIntNode := debugIntNode
788537b88aSTang Haojin  core_with_l2.plicIntNode :*= plicIntNode
79*7ff4ebdcSTang Haojin  beuIntNode := core_with_l2.beuIntNode
80720dd621STang Haojin  val clint = InModuleBody(clintIntNode.makeIOs())
81720dd621STang Haojin  val debug = InModuleBody(debugIntNode.makeIOs())
82720dd621STang Haojin  val plic = InModuleBody(plicIntNode.makeIOs())
83720dd621STang Haojin  val beu = InModuleBody(beuIntNode.makeIOs())
84720dd621STang Haojin
85720dd621STang Haojin  // reset nodes
86720dd621STang Haojin  val core_rst_node = BundleBridgeSource(() => Reset())
878537b88aSTang Haojin  core_with_l2.tile.core_reset_sink := core_rst_node
88720dd621STang Haojin
89720dd621STang Haojin  class XSNoCTopImp(wrapper: XSNoCTop) extends LazyRawModuleImp(wrapper) {
908e93c8f6STang Haojin    soc.XSTopPrefix.foreach { prefix =>
918e93c8f6STang Haojin      val mod = this.toNamed
928e93c8f6STang Haojin      annotate(new ChiselAnnotation {
938e93c8f6STang Haojin        def toFirrtl = NestedPrefixModulesAnnotation(mod, prefix, true)
948e93c8f6STang Haojin      })
958e93c8f6STang Haojin    }
96720dd621STang Haojin    FileRegisters.add("dts", dts)
97720dd621STang Haojin    FileRegisters.add("graphml", graphML)
98720dd621STang Haojin    FileRegisters.add("json", json)
99720dd621STang Haojin    FileRegisters.add("plusArgs", freechips.rocketchip.util.PlusArgArtefacts.serialize_cHeader())
100720dd621STang Haojin
101720dd621STang Haojin    val clock = IO(Input(Clock()))
102720dd621STang Haojin    val reset = IO(Input(AsyncReset()))
10369652e6eSTang Haojin    val noc_clock = EnableCHIAsyncBridge.map(_ => IO(Input(Clock())))
10469652e6eSTang Haojin    val noc_reset = EnableCHIAsyncBridge.map(_ => IO(Input(AsyncReset())))
1058537b88aSTang Haojin    val soc_clock = IO(Input(Clock()))
1068537b88aSTang Haojin    val soc_reset = IO(Input(AsyncReset()))
107720dd621STang Haojin    val io = IO(new Bundle {
108720dd621STang Haojin      val hartId = Input(UInt(p(MaxHartIdBits).W))
109720dd621STang Haojin      val riscv_halt = Output(Bool())
110b30cb8bfSGuanghui Cheng      val hartIsInReset = Output(Bool())
1110700cab2STang Haojin      val riscv_rst_vec = Input(UInt(soc.PAddrBits.W))
112720dd621STang Haojin      val chi = new PortIO
1138537b88aSTang Haojin      val nodeID = Input(UInt(soc.NodeIDWidthList(issue).W))
1146cb0b9a3SsinceforYy      val clintTime = Input(ValidIO(UInt(64.W)))
115720dd621STang Haojin    })
116720dd621STang Haojin    // imsic axi4lite io
1179143e232SJiuyue Ma    val imsic_axi4lite = wrapper.u_imsic_bus_top.module.axi4lite.map(x => IO(chiselTypeOf(x)))
118720dd621STang Haojin    // imsic tl io
119720dd621STang Haojin    val imsic_m_tl = wrapper.u_imsic_bus_top.tl_m.map(x => IO(chiselTypeOf(x.getWrappedValue)))
120720dd621STang Haojin    val imsic_s_tl = wrapper.u_imsic_bus_top.tl_s.map(x => IO(chiselTypeOf(x.getWrappedValue)))
121720dd621STang Haojin
12203459344STang Haojin    val noc_reset_sync = EnableCHIAsyncBridge.map(_ => withClockAndReset(noc_clock, noc_reset) { ResetGen() })
1238537b88aSTang Haojin    val soc_reset_sync = withClockAndReset(soc_clock, soc_reset) { ResetGen() }
124720dd621STang Haojin
1258537b88aSTang Haojin    // device clock and reset
1268537b88aSTang Haojin    wrapper.u_imsic_bus_top.module.clock := soc_clock
1278537b88aSTang Haojin    wrapper.u_imsic_bus_top.module.reset := soc_reset_sync
128720dd621STang Haojin
129720dd621STang Haojin    // imsic axi4lite io connection
1309143e232SJiuyue Ma    wrapper.u_imsic_bus_top.module.axi4lite.foreach(_ <> imsic_axi4lite.get)
131720dd621STang Haojin
132720dd621STang Haojin    // imsic tl io connection
133720dd621STang Haojin    wrapper.u_imsic_bus_top.tl_m.foreach(_ <> imsic_m_tl.get)
134720dd621STang Haojin    wrapper.u_imsic_bus_top.tl_s.foreach(_ <> imsic_s_tl.get)
135720dd621STang Haojin
136720dd621STang Haojin    // input
137720dd621STang Haojin    dontTouch(io)
138720dd621STang Haojin
139*7ff4ebdcSTang Haojin    core_with_l2.module.clock := clock
140*7ff4ebdcSTang Haojin    core_with_l2.module.reset := reset
141*7ff4ebdcSTang Haojin    core_with_l2.module.noc_reset.foreach(_ := noc_reset.get)
142*7ff4ebdcSTang Haojin    core_with_l2.module.soc_reset := soc_reset
143720dd621STang Haojin    core_with_l2.module.io.hartId := io.hartId
144720dd621STang Haojin    core_with_l2.module.io.nodeID.get := io.nodeID
145720dd621STang Haojin    io.riscv_halt := core_with_l2.module.io.cpu_halt
146b30cb8bfSGuanghui Cheng    io.hartIsInReset := core_with_l2.module.io.hartIsInReset
147720dd621STang Haojin    core_with_l2.module.io.reset_vector := io.riscv_rst_vec
1482f9ea954STang Haojin
149e2725c9eSzhanglinjuan    EnableClintAsyncBridge match {
150e2725c9eSzhanglinjuan      case Some(param) =>
151*7ff4ebdcSTang Haojin        withClockAndReset(soc_clock, soc_reset_sync) {
152*7ff4ebdcSTang Haojin          val source = Module(new AsyncQueueSource(UInt(64.W), param))
153e2725c9eSzhanglinjuan          source.io.enq.valid := io.clintTime.valid
154e2725c9eSzhanglinjuan          source.io.enq.bits := io.clintTime.bits
155*7ff4ebdcSTang Haojin          core_with_l2.module.io.clintTime <> source.io.async
156*7ff4ebdcSTang Haojin        }
157e2725c9eSzhanglinjuan      case None =>
158*7ff4ebdcSTang Haojin        core_with_l2.module.io.clintTime <> io.clintTime
159e2725c9eSzhanglinjuan    }
160e2725c9eSzhanglinjuan
161e2725c9eSzhanglinjuan    EnableCHIAsyncBridge match {
162e2725c9eSzhanglinjuan      case Some(param) =>
163*7ff4ebdcSTang Haojin        withClockAndReset(noc_clock.get, noc_reset_sync.get) {
164*7ff4ebdcSTang Haojin          val sink = Module(new CHIAsyncBridgeSink(param))
165*7ff4ebdcSTang Haojin          sink.io.async <> core_with_l2.module.io.chi
166e2725c9eSzhanglinjuan          io.chi <> sink.io.deq
167*7ff4ebdcSTang Haojin        }
168e2725c9eSzhanglinjuan      case None =>
169*7ff4ebdcSTang Haojin        io.chi <> core_with_l2.module.io.chi
170e2725c9eSzhanglinjuan    }
1716cb0b9a3SsinceforYy
1726cb0b9a3SsinceforYy    core_with_l2.module.io.msiInfo.valid := wrapper.u_imsic_bus_top.module.o_msi_info_vld
1736cb0b9a3SsinceforYy    core_with_l2.module.io.msiInfo.bits.info := wrapper.u_imsic_bus_top.module.o_msi_info
174720dd621STang Haojin    // tie off core soft reset
175720dd621STang Haojin    core_rst_node.out.head._1 := false.B.asAsyncReset
176720dd621STang Haojin
177720dd621STang Haojin    core_with_l2.module.io.debugTopDown.l3MissMatch := false.B
178720dd621STang Haojin  }
179720dd621STang Haojin
180720dd621STang Haojin  lazy val module = new XSNoCTopImp(this)
181720dd621STang Haojin}
182