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