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.{CHIAsyncBridgeSink, PortIO} 33import freechips.rocketchip.tile.MaxHartIdBits 34import freechips.rocketchip.util.{AsyncQueueParams, AsyncQueueSource} 35import chisel3.experimental.{ChiselAnnotation, annotate} 36import sifive.enterprise.firrtl.NestedPrefixModulesAnnotation 37import utility.sram.SramBroadcastBundle 38 39import difftest.common.DifftestWiring 40import difftest.util.Profile 41 42class XSNoCTop()(implicit p: Parameters) extends BaseXSSoc with HasSoCParameter 43{ 44 override lazy val desiredName: String = "XSTop" 45 46 ResourceBinding { 47 val width = ResourceInt(2) 48 val model = "freechips,rocketchip-unknown" 49 Resource(ResourceAnchors.root, "model").bind(ResourceString(model)) 50 Resource(ResourceAnchors.root, "compat").bind(ResourceString(model + "-dev")) 51 Resource(ResourceAnchors.soc, "compat").bind(ResourceString(model + "-soc")) 52 Resource(ResourceAnchors.root, "width").bind(width) 53 Resource(ResourceAnchors.soc, "width").bind(width) 54 Resource(ResourceAnchors.cpus, "width").bind(ResourceInt(1)) 55 def bindManagers(xbar: TLNexusNode) = { 56 ManagerUnification(xbar.edges.in.head.manager.managers).foreach{ manager => 57 manager.resources.foreach(r => r.bind(manager.toResource)) 58 } 59 } 60 } 61 62 require(enableCHI) 63 64 // xstile 65 val core_with_l2 = LazyModule(new XSTileWrap()(p.alter((site, here, up) => { 66 case XSCoreParamsKey => tiles.head 67 case PerfCounterOptionsKey => up(PerfCounterOptionsKey).copy(perfDBHartID = tiles.head.HartId) 68 }))) 69 70 // imsic bus top 71 val u_imsic_bus_top = LazyModule(new imsic_bus_top( 72 useTL = soc.IMSICUseTL, 73 baseAddress = (0x3A800000, 0x3B000000) 74 )) 75 76 // interrupts 77 val clintIntNode = IntSourceNode(IntSourcePortSimple(1, 1, 2)) 78 val debugIntNode = IntSourceNode(IntSourcePortSimple(1, 1, 1)) 79 val plicIntNode = IntSourceNode(IntSourcePortSimple(1, 2, 1)) 80 val nmiIntNode = IntSourceNode(IntSourcePortSimple(1, 1, (new NonmaskableInterruptIO).elements.size)) 81 val beuIntNode = IntSinkNode(IntSinkPortSimple(1, 1)) 82 core_with_l2.clintIntNode := clintIntNode 83 core_with_l2.debugIntNode := debugIntNode 84 core_with_l2.plicIntNode :*= plicIntNode 85 core_with_l2.nmiIntNode := nmiIntNode 86 beuIntNode := core_with_l2.beuIntNode 87 val clint = InModuleBody(clintIntNode.makeIOs()) 88 val debug = InModuleBody(debugIntNode.makeIOs()) 89 val plic = InModuleBody(plicIntNode.makeIOs()) 90 val nmi = InModuleBody(nmiIntNode.makeIOs()) 91 val beu = InModuleBody(beuIntNode.makeIOs()) 92 93 // seperate DebugModule bus 94 val EnableDMAsync = EnableDMAsyncBridge.isDefined 95 // asynchronous bridge sink node 96 val dmAsyncSinkOpt = Option.when(SeperateDMBus && EnableDMAsync)( 97 LazyModule(new TLAsyncCrossingSink(EnableDMAsyncBridge.get)) 98 ) 99 dmAsyncSinkOpt.foreach(_.node := core_with_l2.dmAsyncSourceOpt.get.node) 100 // synchronous sink node 101 val dmSyncSinkOpt = Option.when(SeperateDMBus && !EnableDMAsync)(TLTempNode()) 102 dmSyncSinkOpt.foreach(_ := core_with_l2.dmSyncSourceOpt.get) 103 104 // The Manager Node is only used to make IO. Standalone DM should be used for XSNoCTopConfig 105 val dm = Option.when(SeperateDMBus)(TLManagerNode(Seq( 106 TLSlavePortParameters.v1( 107 managers = Seq( 108 TLSlaveParameters.v1( 109 address = Seq(p(DebugModuleKey).get.address), 110 regionType = RegionType.UNCACHED, 111 supportsGet = TransferSizes(1, p(SoCParamsKey).L3BlockSize), 112 supportsPutPartial = TransferSizes(1, p(SoCParamsKey).L3BlockSize), 113 supportsPutFull = TransferSizes(1, p(SoCParamsKey).L3BlockSize), 114 fifoId = Some(0) 115 ) 116 ), 117 beatBytes = 8 118 ) 119 ))) 120 val dmXbar = Option.when(SeperateDMBus)(TLXbar()) 121 dmAsyncSinkOpt.foreach(sink => dmXbar.get := sink.node) 122 dmSyncSinkOpt.foreach(sink => dmXbar.get := sink) 123 dm.foreach(_ := dmXbar.get) 124 // seperate debug module io 125 val io_dm = dm.map(x => InModuleBody(x.makeIOs())) 126 127 // reset nodes 128 val core_rst_node = BundleBridgeSource(() => Reset()) 129 core_with_l2.tile.core_reset_sink := core_rst_node 130 131 class XSNoCTopImp(wrapper: XSNoCTop) extends LazyRawModuleImp(wrapper) { 132 soc.XSTopPrefix.foreach { prefix => 133 val mod = this.toNamed 134 annotate(new ChiselAnnotation { 135 def toFirrtl = NestedPrefixModulesAnnotation(mod, prefix, true) 136 }) 137 } 138 FileRegisters.add("dts", dts) 139 FileRegisters.add("graphml", graphML) 140 FileRegisters.add("json", json) 141 FileRegisters.add("plusArgs", freechips.rocketchip.util.PlusArgArtefacts.serialize_cHeader()) 142 143 val clock = IO(Input(Clock())) 144 val reset = IO(Input(AsyncReset())) 145 val noc_clock = EnableCHIAsyncBridge.map(_ => IO(Input(Clock()))) 146 val noc_reset = EnableCHIAsyncBridge.map(_ => IO(Input(AsyncReset()))) 147 val soc_clock = IO(Input(Clock())) 148 val soc_reset = IO(Input(AsyncReset())) 149 private val hasMbist = tiles.head.hasMbist 150 val io = IO(new Bundle { 151 val hartId = Input(UInt(p(MaxHartIdBits).W)) 152 val riscv_halt = Output(Bool()) 153 val riscv_critical_error = Output(Bool()) 154 val hartResetReq = Input(Bool()) 155 val hartIsInReset = Output(Bool()) 156 val riscv_rst_vec = Input(UInt(soc.PAddrBits.W)) 157 val chi = new PortIO 158 val nodeID = Input(UInt(soc.NodeIDWidthList(issue).W)) 159 val clintTime = Input(ValidIO(UInt(64.W))) 160 val traceCoreInterface = new Bundle { 161 val fromEncoder = Input(new Bundle { 162 val enable = Bool() 163 val stall = Bool() 164 }) 165 val toEncoder = Output(new Bundle { 166 val cause = UInt(TraceCauseWidth.W) 167 val tval = UInt(TraceTvalWidth.W) 168 val priv = UInt(TracePrivWidth.W) 169 val iaddr = UInt((TraceTraceGroupNum * TraceIaddrWidth).W) 170 val itype = UInt((TraceTraceGroupNum * TraceItypeWidth).W) 171 val iretire = UInt((TraceTraceGroupNum * TraceIretireWidthCompressed).W) 172 val ilastsize = UInt((TraceTraceGroupNum * TraceIlastsizeWidth).W) 173 }) 174 } 175 val dft = if(hasMbist) Some(Input(new SramBroadcastBundle)) else None 176 val dft_reset = if(hasMbist) Some(Input(new DFTResetSignals())) else None 177 val lp = Option.when(EnablePowerDown) (new LowPowerIO) 178 }) 179 // imsic axi4lite io 180 val imsic_axi4lite = wrapper.u_imsic_bus_top.module.axi4lite.map(x => IO(chiselTypeOf(x))) 181 // imsic tl io 182 val imsic_m_tl = wrapper.u_imsic_bus_top.tl_m.map(x => IO(chiselTypeOf(x.getWrappedValue))) 183 val imsic_s_tl = wrapper.u_imsic_bus_top.tl_s.map(x => IO(chiselTypeOf(x.getWrappedValue))) 184 185 val noc_reset_sync = EnableCHIAsyncBridge.map(_ => withClockAndReset(noc_clock, noc_reset) { ResetGen(2, io.dft_reset) }) 186 val soc_reset_sync = withClockAndReset(soc_clock, soc_reset) { ResetGen(2, io.dft_reset) } 187 wrapper.core_with_l2.module.io.dft.zip(io.dft).foreach({case(a, b) => a := b}) 188 wrapper.core_with_l2.module.io.dft_reset.zip(io.dft_reset).foreach({case(a, b) => a := b}) 189 // device clock and reset 190 wrapper.u_imsic_bus_top.module.clock := soc_clock 191 wrapper.u_imsic_bus_top.module.reset := soc_reset_sync 192 193 // imsic axi4lite io connection 194 wrapper.u_imsic_bus_top.module.axi4lite.foreach(_ <> imsic_axi4lite.get) 195 196 // imsic tl io connection 197 wrapper.u_imsic_bus_top.tl_m.foreach(_ <> imsic_m_tl.get) 198 wrapper.u_imsic_bus_top.tl_s.foreach(_ <> imsic_s_tl.get) 199 200 // input 201 dontTouch(io) 202 203 /* 204 SoC control the sequence of power on/off with isolation/reset/clock 205 */ 206 val soc_rst_n = io.lp.map(_.i_cpu_sw_rst_n).getOrElse(true.B) 207 val soc_iso_en = io.lp.map(_.i_cpu_iso_en).getOrElse(false.B) 208 209 /* Core+L2 reset when: 210 1. normal reset from SoC 211 2. SoC initialize reset during Power on/off flow 212 */ 213 val cpuReset = reset.asBool || !soc_rst_n 214 215 //Interrupt sources collect 216 val msip = clint.head(0) 217 val mtip = clint.head(1) 218 val meip = plic.head(0) 219 val seip = plic.last(0) 220 val nmi_31 = nmi.head(0) 221 val nmi_43 = nmi.head(1) 222 val msi_info_vld = core_with_l2.module.io.msiInfo.valid 223 val intSrc = Cat(msip, mtip, meip, seip, nmi_31, nmi_43, msi_info_vld) 224 225 /* 226 * CPU Low Power State: 227 * 1. core+L2 Low power state transactions is triggered by l2 flush request from core CSR 228 * 2. wait L2 flush done 229 * 3. wait Core to wfi -> send out < io.o_cpu_no_op > 230 */ 231 val sIDLE :: sL2FLUSH :: sWAITWFI :: sEXITCO :: sPOFFREQ :: Nil = Enum(5) 232 val lpState = withClockAndReset(clock, cpuReset.asAsyncReset) {RegInit(sIDLE)} 233 val l2_flush_en = core_with_l2.module.io.l2_flush_en.getOrElse(false.B) 234 val l2_flush_done = core_with_l2.module.io.l2_flush_done.getOrElse(false.B) 235 val isWFI = core_with_l2.module.io.cpu_halt 236 val exitco = !io.chi.syscoreq & !io.chi.syscoack 237 lpState := lpStateNext(lpState, l2_flush_en, l2_flush_done, isWFI, exitco) 238 io.lp.foreach { lp => lp.o_cpu_no_op := lpState === sPOFFREQ } // inform SoC core+l2 want to power off 239 240 /*WFI clock Gating state 241 1. works only when lpState is IDLE means Core+L2 works in normal state 242 2. when Core is in wfi state, core+l2 clock is gated 243 3. only reset/interrupt/snoop could recover core+l2 clock 244 */ 245 val sNORMAL :: sGCLOCK :: sAWAKE :: Nil = Enum(3) 246 val wfiState = withClockAndReset(clock, cpuReset.asAsyncReset) {RegInit(sNORMAL)} 247 val isNormal = lpState === sIDLE 248 val wfiGateClock = withClockAndReset(clock, cpuReset.asAsyncReset) {RegInit(false.B)} 249 wfiState := WfiStateNext(wfiState, isWFI, isNormal, io.chi.rx.snp.flitpend, intSrc) 250 251 if (WFIClockGate) { 252 wfiGateClock := (wfiState === sGCLOCK) 253 }else { 254 wfiGateClock := false.B 255 } 256 257 258 259 /* during power down sequence, SoC reset will gate clock */ 260 val pwrdownGateClock = withClockAndReset(clock, cpuReset.asAsyncReset) {RegInit(false.B)} 261 pwrdownGateClock := !soc_rst_n && lpState === sPOFFREQ 262 /* 263 physical power off handshake: 264 i_cpu_pwrdown_req_n 265 o_cpu_pwrdown_ack_n means all power is safely on 266 */ 267 val soc_pwrdown_n = io.lp.map(_.i_cpu_pwrdown_req_n).getOrElse(true.B) 268 io.lp.foreach { lp => lp.o_cpu_pwrdown_ack_n := core_with_l2.module.io.pwrdown_ack_n.getOrElse(true.B) } 269 270 271 /* Core+L2 hardware initial clock gating as: 272 1. Gate clock when SoC reset CPU with < io.i_cpu_sw_rst_n > valid 273 2. Gate clock when SoC is enable clock (Core+L2 in normal state) and core is in wfi state 274 3. Disable clock gate at the cycle of Flitpend valid in rx.snp channel 275 */ 276 val cpuClockEn = !wfiGateClock && !pwrdownGateClock | io.chi.rx.snp.flitpend 277 278 dontTouch(wfiGateClock) 279 dontTouch(pwrdownGateClock) 280 dontTouch(cpuClockEn) 281 282 core_with_l2.module.clock := ClockGate(false.B, cpuClockEn, clock) 283 core_with_l2.module.reset := cpuReset.asAsyncReset 284 core_with_l2.module.noc_reset.foreach(_ := noc_reset.get) 285 core_with_l2.module.soc_reset := soc_reset 286 core_with_l2.module.io.hartId := io.hartId 287 core_with_l2.module.io.nodeID.get := io.nodeID 288 io.riscv_halt := core_with_l2.module.io.cpu_halt 289 io.riscv_critical_error := core_with_l2.module.io.cpu_crtical_error 290 core_with_l2.module.io.hartResetReq := io.hartResetReq 291 io.hartIsInReset := core_with_l2.module.io.hartIsInReset 292 core_with_l2.module.io.reset_vector := io.riscv_rst_vec 293 core_with_l2.module.io.iso_en.foreach { _ := false.B } 294 core_with_l2.module.io.pwrdown_req_n.foreach { _ := true.B } 295 // trace Interface 296 val traceInterface = core_with_l2.module.io.traceCoreInterface 297 traceInterface.fromEncoder := io.traceCoreInterface.fromEncoder 298 io.traceCoreInterface.toEncoder.priv := traceInterface.toEncoder.priv 299 io.traceCoreInterface.toEncoder.cause := traceInterface.toEncoder.trap.cause 300 io.traceCoreInterface.toEncoder.tval := traceInterface.toEncoder.trap.tval 301 io.traceCoreInterface.toEncoder.iaddr := VecInit(traceInterface.toEncoder.groups.map(_.bits.iaddr)).asUInt 302 io.traceCoreInterface.toEncoder.itype := VecInit(traceInterface.toEncoder.groups.map(_.bits.itype)).asUInt 303 io.traceCoreInterface.toEncoder.iretire := VecInit(traceInterface.toEncoder.groups.map(_.bits.iretire)).asUInt 304 io.traceCoreInterface.toEncoder.ilastsize := VecInit(traceInterface.toEncoder.groups.map(_.bits.ilastsize)).asUInt 305 306 EnableClintAsyncBridge match { 307 case Some(param) => 308 withClockAndReset(soc_clock, soc_reset_sync) { 309 val source = Module(new AsyncQueueSource(UInt(64.W), param)) 310 source.io.enq.valid := io.clintTime.valid 311 source.io.enq.bits := io.clintTime.bits 312 core_with_l2.module.io.clintTime <> source.io.async 313 } 314 case None => 315 core_with_l2.module.io.clintTime <> io.clintTime 316 } 317 318 EnableCHIAsyncBridge match { 319 case Some(param) => 320 withClockAndReset(noc_clock.get, noc_reset_sync.get) { 321 val sink = Module(new CHIAsyncBridgeSink(param)) 322 sink.io.async <> core_with_l2.module.io.chi 323 io.chi <> sink.io.deq 324 } 325 case None => 326 io.chi <> core_with_l2.module.io.chi 327 } 328 329 // Seperate DebugModule TL Async Queue Sink 330 if (SeperateDMBus && EnableDMAsync) { 331 dmAsyncSinkOpt.get.module.clock := soc_clock 332 dmAsyncSinkOpt.get.module.reset := soc_reset_sync 333 } 334 335 core_with_l2.module.io.msiInfo.valid := wrapper.u_imsic_bus_top.module.o_msi_info_vld 336 core_with_l2.module.io.msiInfo.bits.info := wrapper.u_imsic_bus_top.module.o_msi_info 337 // tie off core soft reset 338 core_rst_node.out.head._1 := false.B.asAsyncReset 339 340 core_with_l2.module.io.debugTopDown.l3MissMatch := false.B 341 core_with_l2.module.io.l3Miss := false.B 342 } 343 344 lazy val module = new XSNoCTopImp(this) 345} 346 347class XSNoCDiffTop(implicit p: Parameters) extends Module { 348 override val desiredName: String = "XSDiffTop" 349 val l_soc = LazyModule(new XSNoCTop()) 350 val soc = Module(l_soc.module) 351 352 // Expose XSTop IOs outside, i.e. io 353 def exposeIO(data: Data, name: String): Unit = { 354 val dummy = IO(chiselTypeOf(data)).suggestName(name) 355 dummy <> data 356 } 357 def exposeOptionIO(data: Option[Data], name: String): Unit = { 358 if (data.isDefined) { 359 val dummy = IO(chiselTypeOf(data.get)).suggestName(name) 360 dummy <> data.get 361 } 362 } 363 exposeIO(l_soc.clint, "clint") 364 exposeIO(l_soc.debug, "debug") 365 exposeIO(l_soc.plic, "plic") 366 exposeIO(l_soc.beu, "beu") 367 exposeIO(l_soc.nmi, "nmi") 368 soc.clock := clock 369 soc.reset := reset.asAsyncReset 370 exposeIO(soc.soc_clock, "soc_clock") 371 exposeIO(soc.soc_reset, "soc_reset") 372 exposeIO(soc.io, "io") 373 exposeOptionIO(soc.noc_clock, "noc_clock") 374 exposeOptionIO(soc.noc_reset, "noc_reset") 375 exposeOptionIO(soc.imsic_axi4lite, "imsic_axi4lite") 376 377 // TODO: 378 // XSDiffTop is only part of DUT, we can not instantiate difftest here. 379 // Temporarily we collect Performance counters for each DiffTop, need control signals passed from Difftest 380 val timer = IO(Input(UInt(64.W))) 381 val logEnable = IO(Input(Bool())) 382 val clean = IO(Input(Bool())) 383 val dump = IO(Input(Bool())) 384 XSLog.collect(timer, logEnable, clean, dump) 385 DifftestWiring.createAndConnectExtraIOs() 386 Profile.generateJson("XiangShan") 387 XSNoCDiffTopChecker() 388} 389 390// TODO: 391// Currently we use two-step XiangShan-Difftest, generating XS(with Diff Interface only) and Difftest seperately 392// To avoid potential interface problem between XS and Diff, we add Checker and CI(dual-core) 393// We will try one-step XS-Diff later 394object XSNoCDiffTopChecker { 395 def apply(): Unit = { 396 val verilog = 397 """ 398 |`define CONFIG_XSCORE_NR 2 399 |`include "gateway_interface.svh" 400 |module XSDiffTopChecker( 401 | input cpu_clk, 402 | input cpu_rstn, 403 | input sys_clk, 404 | input sys_rstn 405 |); 406 |wire [63:0] timer; 407 |wire logEnable; 408 |wire clean; 409 |wire dump; 410 |// FIXME: use siganls from Difftest rather than default value 411 |assign timer = 64'b0; 412 |assign logEnable = 1'b0; 413 |assign clean = 1'b0; 414 |assign dump = 1'b0; 415 |gateway_if gateway_if_i(); 416 |core_if core_if_o[`CONFIG_XSCORE_NR](); 417 |generate 418 | genvar i; 419 | for (i = 0; i < `CONFIG_XSCORE_NR; i = i+1) 420 | begin: u_CPU_TOP 421 | // FIXME: add missing ports 422 | XSDiffTop u_XSTop ( 423 | .clock (cpu_clk), 424 | .noc_clock (sys_clk), 425 | .soc_clock (sys_clk), 426 | .io_hartId (6'h0 + i), 427 | .timer (timer), 428 | .logEnable (logEnable), 429 | .clean (clean), 430 | .dump (dump), 431 | .gateway_out (core_if_o[i]) 432 | ); 433 | end 434 |endgenerate 435 | CoreToGateway u_CoreToGateway( 436 | .gateway_out (gateway_if_i.out), 437 | .core_in (core_if_o) 438 | ); 439 | GatewayEndpoint u_GatewayEndpoint( 440 | .clock (sys_clk), 441 | .reset (sys_rstn), 442 | .gateway_in (gateway_if_i.in), 443 | .step () 444 | ); 445 | 446 |endmodule 447 """.stripMargin 448 FileRegisters.writeOutputFile("./build", "XSDiffTopChecker.sv", verilog) 449 } 450} 451