1/*************************************************************************************** 2* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC) 3* Copyright (c) 2024 Institute of Computing Technology, Chinese Academy of Sciences 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 top 18 19import chisel3._ 20import chisel3.util._ 21import xiangshan._ 22import utils._ 23import utility._ 24import system._ 25import device._ 26import org.chipsalliance.cde.config._ 27import freechips.rocketchip.amba.axi4._ 28import freechips.rocketchip.devices.debug.DebugModuleKey 29import freechips.rocketchip.diplomacy._ 30import freechips.rocketchip.interrupts._ 31import freechips.rocketchip.tilelink._ 32import coupledL2.tl2chi.{PortIO, CHIAsyncBridgeSink} 33import freechips.rocketchip.tile.MaxHartIdBits 34import freechips.rocketchip.util.{AsyncQueueSource, AsyncQueueParams} 35import chisel3.experimental.{annotate, ChiselAnnotation} 36import sifive.enterprise.firrtl.NestedPrefixModulesAnnotation 37 38import difftest.common.DifftestWiring 39import difftest.util.Profile 40 41class XSNoCTop()(implicit p: Parameters) extends BaseXSSoc with HasSoCParameter 42{ 43 override lazy val desiredName: String = "XSTop" 44 45 ResourceBinding { 46 val width = ResourceInt(2) 47 val model = "freechips,rocketchip-unknown" 48 Resource(ResourceAnchors.root, "model").bind(ResourceString(model)) 49 Resource(ResourceAnchors.root, "compat").bind(ResourceString(model + "-dev")) 50 Resource(ResourceAnchors.soc, "compat").bind(ResourceString(model + "-soc")) 51 Resource(ResourceAnchors.root, "width").bind(width) 52 Resource(ResourceAnchors.soc, "width").bind(width) 53 Resource(ResourceAnchors.cpus, "width").bind(ResourceInt(1)) 54 def bindManagers(xbar: TLNexusNode) = { 55 ManagerUnification(xbar.edges.in.head.manager.managers).foreach{ manager => 56 manager.resources.foreach(r => r.bind(manager.toResource)) 57 } 58 } 59 } 60 61 require(enableCHI) 62 63 // xstile 64 val core_with_l2 = LazyModule(new XSTileWrap()(p.alter((site, here, up) => { 65 case XSCoreParamsKey => tiles.head 66 case PerfCounterOptionsKey => up(PerfCounterOptionsKey).copy(perfDBHartID = tiles.head.HartId) 67 }))) 68 69 // imsic bus top 70 val u_imsic_bus_top = LazyModule(new imsic_bus_top( 71 useTL = soc.IMSICUseTL, 72 baseAddress = (0x3A800000, 0x3B000000) 73 )) 74 75 // interrupts 76 val clintIntNode = IntSourceNode(IntSourcePortSimple(1, 1, 2)) 77 val debugIntNode = IntSourceNode(IntSourcePortSimple(1, 1, 1)) 78 val plicIntNode = IntSourceNode(IntSourcePortSimple(1, 2, 1)) 79 val nmiIntNode = IntSourceNode(IntSourcePortSimple(1, 1, (new NonmaskableInterruptIO).elements.size)) 80 val beuIntNode = IntSinkNode(IntSinkPortSimple(1, 1)) 81 core_with_l2.clintIntNode := clintIntNode 82 core_with_l2.debugIntNode := debugIntNode 83 core_with_l2.plicIntNode :*= plicIntNode 84 core_with_l2.nmiIntNode := nmiIntNode 85 beuIntNode := core_with_l2.beuIntNode 86 val clint = InModuleBody(clintIntNode.makeIOs()) 87 val debug = InModuleBody(debugIntNode.makeIOs()) 88 val plic = InModuleBody(plicIntNode.makeIOs()) 89 val nmi = InModuleBody(nmiIntNode.makeIOs()) 90 val beu = InModuleBody(beuIntNode.makeIOs()) 91 92 // seperate DebugModule bus 93 val EnableDMAsync = EnableDMAsyncBridge.isDefined 94 // asynchronous bridge sink node 95 val dmAsyncSinkOpt = Option.when(SeperateDMBus && EnableDMAsync)( 96 LazyModule(new TLAsyncCrossingSink(EnableDMAsyncBridge.get)) 97 ) 98 dmAsyncSinkOpt.foreach(_.node := core_with_l2.dmAsyncSourceOpt.get.node) 99 // synchronous sink node 100 val dmSyncSinkOpt = Option.when(SeperateDMBus && !EnableDMAsync)(TLTempNode()) 101 dmSyncSinkOpt.foreach(_ := core_with_l2.dmSyncSourceOpt.get) 102 103 // The Manager Node is only used to make IO. Standalone DM should be used for XSNoCTopConfig 104 val dm = Option.when(SeperateDMBus)(TLManagerNode(Seq( 105 TLSlavePortParameters.v1( 106 managers = Seq( 107 TLSlaveParameters.v1( 108 address = Seq(p(DebugModuleKey).get.address), 109 regionType = RegionType.UNCACHED, 110 supportsGet = TransferSizes(1, p(SoCParamsKey).L3BlockSize), 111 supportsPutPartial = TransferSizes(1, p(SoCParamsKey).L3BlockSize), 112 supportsPutFull = TransferSizes(1, p(SoCParamsKey).L3BlockSize), 113 fifoId = Some(0) 114 ) 115 ), 116 beatBytes = 8 117 ) 118 ))) 119 val dmXbar = Option.when(SeperateDMBus)(TLXbar()) 120 dmAsyncSinkOpt.foreach(sink => dmXbar.get := sink.node) 121 dmSyncSinkOpt.foreach(sink => dmXbar.get := sink) 122 dm.foreach(_ := dmXbar.get) 123 // seperate debug module io 124 val io_dm = dm.map(x => InModuleBody(x.makeIOs())) 125 126 // reset nodes 127 val core_rst_node = BundleBridgeSource(() => Reset()) 128 core_with_l2.tile.core_reset_sink := core_rst_node 129 130 class XSNoCTopImp(wrapper: XSNoCTop) extends LazyRawModuleImp(wrapper) { 131 soc.XSTopPrefix.foreach { prefix => 132 val mod = this.toNamed 133 annotate(new ChiselAnnotation { 134 def toFirrtl = NestedPrefixModulesAnnotation(mod, prefix, true) 135 }) 136 } 137 FileRegisters.add("dts", dts) 138 FileRegisters.add("graphml", graphML) 139 FileRegisters.add("json", json) 140 FileRegisters.add("plusArgs", freechips.rocketchip.util.PlusArgArtefacts.serialize_cHeader()) 141 142 val clock = IO(Input(Clock())) 143 val reset = IO(Input(AsyncReset())) 144 val noc_clock = EnableCHIAsyncBridge.map(_ => IO(Input(Clock()))) 145 val noc_reset = EnableCHIAsyncBridge.map(_ => IO(Input(AsyncReset()))) 146 val soc_clock = IO(Input(Clock())) 147 val soc_reset = IO(Input(AsyncReset())) 148 val io = IO(new Bundle { 149 val hartId = Input(UInt(p(MaxHartIdBits).W)) 150 val riscv_halt = Output(Bool()) 151 val riscv_critical_error = Output(Bool()) 152 val hartResetReq = Input(Bool()) 153 val hartIsInReset = Output(Bool()) 154 val riscv_rst_vec = Input(UInt(soc.PAddrBits.W)) 155 val chi = new PortIO 156 val nodeID = Input(UInt(soc.NodeIDWidthList(issue).W)) 157 val clintTime = Input(ValidIO(UInt(64.W))) 158 val traceCoreInterface = new Bundle { 159 val fromEncoder = Input(new Bundle { 160 val enable = Bool() 161 val stall = Bool() 162 }) 163 val toEncoder = Output(new Bundle { 164 val cause = UInt(TraceCauseWidth.W) 165 val tval = UInt(TraceTvalWidth.W) 166 val priv = UInt(TracePrivWidth.W) 167 val iaddr = UInt((TraceTraceGroupNum * TraceIaddrWidth).W) 168 val itype = UInt((TraceTraceGroupNum * TraceItypeWidth).W) 169 val iretire = UInt((TraceTraceGroupNum * TraceIretireWidthCompressed).W) 170 val ilastsize = UInt((TraceTraceGroupNum * TraceIlastsizeWidth).W) 171 }) 172 } 173 }) 174 // imsic axi4lite io 175 val imsic_axi4lite = wrapper.u_imsic_bus_top.module.axi4lite.map(x => IO(chiselTypeOf(x))) 176 // imsic tl io 177 val imsic_m_tl = wrapper.u_imsic_bus_top.tl_m.map(x => IO(chiselTypeOf(x.getWrappedValue))) 178 val imsic_s_tl = wrapper.u_imsic_bus_top.tl_s.map(x => IO(chiselTypeOf(x.getWrappedValue))) 179 180 val noc_reset_sync = EnableCHIAsyncBridge.map(_ => withClockAndReset(noc_clock, noc_reset) { ResetGen() }) 181 val soc_reset_sync = withClockAndReset(soc_clock, soc_reset) { ResetGen() } 182 183 // device clock and reset 184 wrapper.u_imsic_bus_top.module.clock := soc_clock 185 wrapper.u_imsic_bus_top.module.reset := soc_reset_sync 186 187 // imsic axi4lite io connection 188 wrapper.u_imsic_bus_top.module.axi4lite.foreach(_ <> imsic_axi4lite.get) 189 190 // imsic tl io connection 191 wrapper.u_imsic_bus_top.tl_m.foreach(_ <> imsic_m_tl.get) 192 wrapper.u_imsic_bus_top.tl_s.foreach(_ <> imsic_s_tl.get) 193 194 // input 195 dontTouch(io) 196 197 core_with_l2.module.clock := clock 198 core_with_l2.module.reset := reset 199 core_with_l2.module.noc_reset.foreach(_ := noc_reset.get) 200 core_with_l2.module.soc_reset := soc_reset 201 core_with_l2.module.io.hartId := io.hartId 202 core_with_l2.module.io.nodeID.get := io.nodeID 203 io.riscv_halt := core_with_l2.module.io.cpu_halt 204 io.riscv_critical_error := core_with_l2.module.io.cpu_crtical_error 205 core_with_l2.module.io.hartResetReq := io.hartResetReq 206 io.hartIsInReset := core_with_l2.module.io.hartIsInReset 207 core_with_l2.module.io.reset_vector := io.riscv_rst_vec 208 // trace Interface 209 val traceInterface = core_with_l2.module.io.traceCoreInterface 210 traceInterface.fromEncoder := io.traceCoreInterface.fromEncoder 211 io.traceCoreInterface.toEncoder.priv := traceInterface.toEncoder.priv 212 io.traceCoreInterface.toEncoder.cause := traceInterface.toEncoder.trap.cause 213 io.traceCoreInterface.toEncoder.tval := traceInterface.toEncoder.trap.tval 214 io.traceCoreInterface.toEncoder.iaddr := VecInit(traceInterface.toEncoder.groups.map(_.bits.iaddr)).asUInt 215 io.traceCoreInterface.toEncoder.itype := VecInit(traceInterface.toEncoder.groups.map(_.bits.itype)).asUInt 216 io.traceCoreInterface.toEncoder.iretire := VecInit(traceInterface.toEncoder.groups.map(_.bits.iretire)).asUInt 217 io.traceCoreInterface.toEncoder.ilastsize := VecInit(traceInterface.toEncoder.groups.map(_.bits.ilastsize)).asUInt 218 219 EnableClintAsyncBridge match { 220 case Some(param) => 221 withClockAndReset(soc_clock, soc_reset_sync) { 222 val source = Module(new AsyncQueueSource(UInt(64.W), param)) 223 source.io.enq.valid := io.clintTime.valid 224 source.io.enq.bits := io.clintTime.bits 225 core_with_l2.module.io.clintTime <> source.io.async 226 } 227 case None => 228 core_with_l2.module.io.clintTime <> io.clintTime 229 } 230 231 EnableCHIAsyncBridge match { 232 case Some(param) => 233 withClockAndReset(noc_clock.get, noc_reset_sync.get) { 234 val sink = Module(new CHIAsyncBridgeSink(param)) 235 sink.io.async <> core_with_l2.module.io.chi 236 io.chi <> sink.io.deq 237 } 238 case None => 239 io.chi <> core_with_l2.module.io.chi 240 } 241 242 // Seperate DebugModule TL Async Queue Sink 243 if (SeperateDMBus && EnableDMAsync) { 244 dmAsyncSinkOpt.get.module.clock := soc_clock 245 dmAsyncSinkOpt.get.module.reset := soc_reset_sync 246 } 247 248 core_with_l2.module.io.msiInfo.valid := wrapper.u_imsic_bus_top.module.o_msi_info_vld 249 core_with_l2.module.io.msiInfo.bits.info := wrapper.u_imsic_bus_top.module.o_msi_info 250 // tie off core soft reset 251 core_rst_node.out.head._1 := false.B.asAsyncReset 252 253 core_with_l2.module.io.debugTopDown.l3MissMatch := false.B 254 core_with_l2.module.io.l3Miss := false.B 255 } 256 257 lazy val module = new XSNoCTopImp(this) 258} 259 260class XSNoCDiffTop(implicit p: Parameters) extends Module { 261 override val desiredName: String = "XSDiffTop" 262 val l_soc = LazyModule(new XSNoCTop()) 263 val soc = Module(l_soc.module) 264 265 // Expose XSTop IOs outside, i.e. io 266 def exposeIO(data: Data, name: String): Unit = { 267 val dummy = IO(chiselTypeOf(data)).suggestName(name) 268 dummy <> data 269 } 270 def exposeOptionIO(data: Option[Data], name: String): Unit = { 271 if (data.isDefined) { 272 val dummy = IO(chiselTypeOf(data.get)).suggestName(name) 273 dummy <> data.get 274 } 275 } 276 exposeIO(l_soc.clint, "clint") 277 exposeIO(l_soc.debug, "debug") 278 exposeIO(l_soc.plic, "plic") 279 exposeIO(l_soc.beu, "beu") 280 exposeIO(l_soc.nmi, "nmi") 281 soc.clock := clock 282 soc.reset := reset.asAsyncReset 283 exposeIO(soc.soc_clock, "soc_clock") 284 exposeIO(soc.soc_reset, "soc_reset") 285 exposeIO(soc.io, "io") 286 exposeOptionIO(soc.noc_clock, "noc_clock") 287 exposeOptionIO(soc.noc_reset, "noc_reset") 288 exposeOptionIO(soc.imsic_axi4lite, "imsic_axi4lite") 289 290 // TODO: 291 // XSDiffTop is only part of DUT, we can not instantiate difftest here. 292 // Temporarily we collect Performance counters for each DiffTop, need control signals passed from Difftest 293 val timer = IO(Input(UInt(64.W))) 294 val logEnable = IO(Input(Bool())) 295 val clean = IO(Input(Bool())) 296 val dump = IO(Input(Bool())) 297 XSLog.collect(timer, logEnable, clean, dump) 298 DifftestWiring.createAndConnectExtraIOs() 299 Profile.generateJson("XiangShan") 300 XSNoCDiffTopChecker() 301} 302 303//TODO: 304//Currently we use two-step XiangShan-Difftest, generating XS(with Diff Interface only) and Difftest seperately 305//To avoid potential interface problem between XS and Diff, we add Checker and CI(dual-core) 306//We will try one-step XS-Diff later 307object XSNoCDiffTopChecker { 308 def apply(): Unit = { 309 val verilog = 310 """ 311 |`define CONFIG_XSCORE_NR 2 312 |`include "gateway_interface.svh" 313 |module XSDiffTopChecker( 314 | input cpu_clk, 315 | input cpu_rstn, 316 | input sys_clk, 317 | input sys_rstn 318 |); 319 |wire [63:0] timer; 320 |wire logEnable; 321 |wire clean; 322 |wire dump; 323 |// FIXME: use siganls from Difftest rather than default value 324 |assign timer = 64'b0; 325 |assign logEnable = 1'b0; 326 |assign clean = 1'b0; 327 |assign dump = 1'b0; 328 |gateway_if gateway_if_i(); 329 |core_if core_if_o[`CONFIG_XSCORE_NR](); 330 |generate 331 | genvar i; 332 | for (i = 0; i < `CONFIG_XSCORE_NR; i = i+1) 333 | begin: u_CPU_TOP 334 | // FIXME: add missing ports 335 | XSDiffTop u_XSTop ( 336 | .clock (cpu_clk), 337 | .noc_clock (sys_clk), 338 | .soc_clock (sys_clk), 339 | .io_hartId (6'h0 + i), 340 | .timer (timer), 341 | .logEnable (logEnable), 342 | .clean (clean), 343 | .dump (dump), 344 | .gateway_out (core_if_o[i]) 345 | ); 346 | end 347 |endgenerate 348 | CoreToGateway u_CoreToGateway( 349 | .gateway_out (gateway_if_i.out), 350 | .core_in (core_if_o) 351 | ); 352 | GatewayEndpoint u_GatewayEndpoint( 353 | .clock (sys_clk), 354 | .reset (sys_rstn), 355 | .gateway_in (gateway_if_i.in), 356 | .step () 357 | ); 358 | 359 |endmodule 360 """.stripMargin 361 FileRegisters.writeOutputFile("./build", "XSDiffTopChecker.sv", verilog) 362 } 363} 364