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.diplomacy._ 29import freechips.rocketchip.interrupts._ 30import freechips.rocketchip.tilelink._ 31import coupledL2.tl2chi.{PortIO, CHIAsyncBridgeSink} 32import freechips.rocketchip.tile.MaxHartIdBits 33import freechips.rocketchip.util.{AsyncQueueSource, AsyncQueueParams} 34import chisel3.experimental.{annotate, ChiselAnnotation} 35import sifive.enterprise.firrtl.NestedPrefixModulesAnnotation 36 37import difftest.common.DifftestWiring 38import difftest.util.Profile 39 40class XSNoCTop()(implicit p: Parameters) extends BaseXSSoc with HasSoCParameter 41{ 42 override lazy val desiredName: String = "XSTop" 43 44 ResourceBinding { 45 val width = ResourceInt(2) 46 val model = "freechips,rocketchip-unknown" 47 Resource(ResourceAnchors.root, "model").bind(ResourceString(model)) 48 Resource(ResourceAnchors.root, "compat").bind(ResourceString(model + "-dev")) 49 Resource(ResourceAnchors.soc, "compat").bind(ResourceString(model + "-soc")) 50 Resource(ResourceAnchors.root, "width").bind(width) 51 Resource(ResourceAnchors.soc, "width").bind(width) 52 Resource(ResourceAnchors.cpus, "width").bind(ResourceInt(1)) 53 def bindManagers(xbar: TLNexusNode) = { 54 ManagerUnification(xbar.edges.in.head.manager.managers).foreach{ manager => 55 manager.resources.foreach(r => r.bind(manager.toResource)) 56 } 57 } 58 } 59 60 require(enableCHI) 61 62 // xstile 63 val core_with_l2 = LazyModule(new XSTileWrap()(p.alter((site, here, up) => { 64 case XSCoreParamsKey => tiles.head 65 case PerfCounterOptionsKey => up(PerfCounterOptionsKey).copy(perfDBHartID = tiles.head.HartId) 66 }))) 67 68 // imsic bus top 69 val u_imsic_bus_top = LazyModule(new imsic_bus_top( 70 useTL = soc.IMSICUseTL, 71 baseAddress = (0x3A800000, 0x3B000000) 72 )) 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 // reset nodes 92 val core_rst_node = BundleBridgeSource(() => Reset()) 93 core_with_l2.tile.core_reset_sink := core_rst_node 94 95 class XSNoCTopImp(wrapper: XSNoCTop) extends LazyRawModuleImp(wrapper) { 96 soc.XSTopPrefix.foreach { prefix => 97 val mod = this.toNamed 98 annotate(new ChiselAnnotation { 99 def toFirrtl = NestedPrefixModulesAnnotation(mod, prefix, true) 100 }) 101 } 102 FileRegisters.add("dts", dts) 103 FileRegisters.add("graphml", graphML) 104 FileRegisters.add("json", json) 105 FileRegisters.add("plusArgs", freechips.rocketchip.util.PlusArgArtefacts.serialize_cHeader()) 106 107 val clock = IO(Input(Clock())) 108 val reset = IO(Input(AsyncReset())) 109 val noc_clock = EnableCHIAsyncBridge.map(_ => IO(Input(Clock()))) 110 val noc_reset = EnableCHIAsyncBridge.map(_ => IO(Input(AsyncReset()))) 111 val soc_clock = IO(Input(Clock())) 112 val soc_reset = IO(Input(AsyncReset())) 113 val io = IO(new Bundle { 114 val hartId = Input(UInt(p(MaxHartIdBits).W)) 115 val riscv_halt = Output(Bool()) 116 val riscv_critical_error = Output(Bool()) 117 val hartResetReq = Input(Bool()) 118 val hartIsInReset = Output(Bool()) 119 val riscv_rst_vec = Input(UInt(soc.PAddrBits.W)) 120 val chi = new PortIO 121 val nodeID = Input(UInt(soc.NodeIDWidthList(issue).W)) 122 val clintTime = Input(ValidIO(UInt(64.W))) 123 val traceCoreInterface = new Bundle { 124 val fromEncoder = Input(new Bundle { 125 val enable = Bool() 126 val stall = Bool() 127 }) 128 val toEncoder = Output(new Bundle { 129 val cause = UInt(TraceCauseWidth.W) 130 val tval = UInt(TraceTvalWidth.W) 131 val priv = UInt(TracePrivWidth.W) 132 val iaddr = UInt((TraceTraceGroupNum * TraceIaddrWidth).W) 133 val itype = UInt((TraceTraceGroupNum * TraceItypeWidth).W) 134 val iretire = UInt((TraceTraceGroupNum * TraceIretireWidthCompressed).W) 135 val ilastsize = UInt((TraceTraceGroupNum * TraceIlastsizeWidth).W) 136 }) 137 } 138 }) 139 // imsic axi4lite io 140 val imsic_axi4lite = wrapper.u_imsic_bus_top.module.axi4lite.map(x => IO(chiselTypeOf(x))) 141 // imsic tl io 142 val imsic_m_tl = wrapper.u_imsic_bus_top.tl_m.map(x => IO(chiselTypeOf(x.getWrappedValue))) 143 val imsic_s_tl = wrapper.u_imsic_bus_top.tl_s.map(x => IO(chiselTypeOf(x.getWrappedValue))) 144 145 val noc_reset_sync = EnableCHIAsyncBridge.map(_ => withClockAndReset(noc_clock, noc_reset) { ResetGen() }) 146 val soc_reset_sync = withClockAndReset(soc_clock, soc_reset) { ResetGen() } 147 148 // device clock and reset 149 wrapper.u_imsic_bus_top.module.clock := soc_clock 150 wrapper.u_imsic_bus_top.module.reset := soc_reset_sync 151 152 // imsic axi4lite io connection 153 wrapper.u_imsic_bus_top.module.axi4lite.foreach(_ <> imsic_axi4lite.get) 154 155 // imsic tl io connection 156 wrapper.u_imsic_bus_top.tl_m.foreach(_ <> imsic_m_tl.get) 157 wrapper.u_imsic_bus_top.tl_s.foreach(_ <> imsic_s_tl.get) 158 159 // input 160 dontTouch(io) 161 162 core_with_l2.module.clock := clock 163 core_with_l2.module.reset := reset 164 core_with_l2.module.noc_reset.foreach(_ := noc_reset.get) 165 core_with_l2.module.soc_reset := soc_reset 166 core_with_l2.module.io.hartId := io.hartId 167 core_with_l2.module.io.nodeID.get := io.nodeID 168 io.riscv_halt := core_with_l2.module.io.cpu_halt 169 io.riscv_critical_error := core_with_l2.module.io.cpu_crtical_error 170 core_with_l2.module.io.hartResetReq := io.hartResetReq 171 io.hartIsInReset := core_with_l2.module.io.hartIsInReset 172 core_with_l2.module.io.reset_vector := io.riscv_rst_vec 173 // trace Interface 174 val traceInterface = core_with_l2.module.io.traceCoreInterface 175 traceInterface.fromEncoder := io.traceCoreInterface.fromEncoder 176 io.traceCoreInterface.toEncoder.priv := traceInterface.toEncoder.priv 177 io.traceCoreInterface.toEncoder.cause := traceInterface.toEncoder.trap.cause 178 io.traceCoreInterface.toEncoder.tval := traceInterface.toEncoder.trap.tval 179 io.traceCoreInterface.toEncoder.iaddr := VecInit(traceInterface.toEncoder.groups.map(_.bits.iaddr)).asUInt 180 io.traceCoreInterface.toEncoder.itype := VecInit(traceInterface.toEncoder.groups.map(_.bits.itype)).asUInt 181 io.traceCoreInterface.toEncoder.iretire := VecInit(traceInterface.toEncoder.groups.map(_.bits.iretire)).asUInt 182 io.traceCoreInterface.toEncoder.ilastsize := VecInit(traceInterface.toEncoder.groups.map(_.bits.ilastsize)).asUInt 183 184 EnableClintAsyncBridge match { 185 case Some(param) => 186 withClockAndReset(soc_clock, soc_reset_sync) { 187 val source = Module(new AsyncQueueSource(UInt(64.W), param)) 188 source.io.enq.valid := io.clintTime.valid 189 source.io.enq.bits := io.clintTime.bits 190 core_with_l2.module.io.clintTime <> source.io.async 191 } 192 case None => 193 core_with_l2.module.io.clintTime <> io.clintTime 194 } 195 196 EnableCHIAsyncBridge match { 197 case Some(param) => 198 withClockAndReset(noc_clock.get, noc_reset_sync.get) { 199 val sink = Module(new CHIAsyncBridgeSink(param)) 200 sink.io.async <> core_with_l2.module.io.chi 201 io.chi <> sink.io.deq 202 } 203 case None => 204 io.chi <> core_with_l2.module.io.chi 205 } 206 207 core_with_l2.module.io.msiInfo.valid := wrapper.u_imsic_bus_top.module.o_msi_info_vld 208 core_with_l2.module.io.msiInfo.bits.info := wrapper.u_imsic_bus_top.module.o_msi_info 209 // tie off core soft reset 210 core_rst_node.out.head._1 := false.B.asAsyncReset 211 212 core_with_l2.module.io.debugTopDown.l3MissMatch := false.B 213 core_with_l2.module.io.l3Miss := false.B 214 } 215 216 lazy val module = new XSNoCTopImp(this) 217} 218 219class XSNoCDiffTop(implicit p: Parameters) extends Module { 220 override val desiredName: String = "XSDiffTop" 221 val l_soc = LazyModule(new XSNoCTop()) 222 val soc = Module(l_soc.module) 223 224 // Expose XSTop IOs outside, i.e. io 225 def exposeIO(data: Data, name: String): Unit = { 226 val dummy = IO(chiselTypeOf(data)).suggestName(name) 227 dummy <> data 228 } 229 def exposeOptionIO(data: Option[Data], name: String): Unit = { 230 if (data.isDefined) { 231 val dummy = IO(chiselTypeOf(data.get)).suggestName(name) 232 dummy <> data.get 233 } 234 } 235 exposeIO(l_soc.clint, "clint") 236 exposeIO(l_soc.debug, "debug") 237 exposeIO(l_soc.plic, "plic") 238 exposeIO(l_soc.beu, "beu") 239 exposeIO(l_soc.nmi, "nmi") 240 soc.clock := clock 241 soc.reset := reset.asAsyncReset 242 exposeIO(soc.soc_clock, "soc_clock") 243 exposeIO(soc.soc_reset, "soc_reset") 244 exposeIO(soc.io, "io") 245 exposeOptionIO(soc.noc_clock, "noc_clock") 246 exposeOptionIO(soc.noc_reset, "noc_reset") 247 exposeOptionIO(soc.imsic_axi4lite, "imsic_axi4lite") 248 249 // TODO: 250 // XSDiffTop is only part of DUT, we can not instantiate difftest here. 251 // Temporarily we collect Performance counters for each DiffTop, need control signals passed from Difftest 252 val timer = IO(Input(UInt(64.W))) 253 val logEnable = IO(Input(Bool())) 254 val clean = IO(Input(Bool())) 255 val dump = IO(Input(Bool())) 256 XSLog.collect(timer, logEnable, clean, dump) 257 DifftestWiring.createAndConnectExtraIOs() 258 Profile.generateJson("XiangShan") 259 XSNoCDiffTopChecker() 260} 261 262//TODO: 263//Currently we use two-step XiangShan-Difftest, generating XS(with Diff Interface only) and Difftest seperately 264//To avoid potential interface problem between XS and Diff, we add Checker and CI(dual-core) 265//We will try one-step XS-Diff later 266object XSNoCDiffTopChecker { 267 def apply(): Unit = { 268 val verilog = 269 """ 270 |`define CONFIG_XSCORE_NR 2 271 |`include "gateway_interface.svh" 272 |module XSDiffTopChecker( 273 | input cpu_clk, 274 | input cpu_rstn, 275 | input sys_clk, 276 | input sys_rstn 277 |); 278 |wire [63:0] timer; 279 |wire logEnable; 280 |wire clean; 281 |wire dump; 282 |// FIXME: use siganls from Difftest rather than default value 283 |assign timer = 64'b0; 284 |assign logEnable = 1'b0; 285 |assign clean = 1'b0; 286 |assign dump = 1'b0; 287 |gateway_if gateway_if_i(); 288 |core_if core_if_o[`CONFIG_XSCORE_NR](); 289 |generate 290 | genvar i; 291 | for (i = 0; i < `CONFIG_XSCORE_NR; i = i+1) 292 | begin: u_CPU_TOP 293 | // FIXME: add missing ports 294 | XSDiffTop u_XSTop ( 295 | .clock (cpu_clk), 296 | .noc_clock (sys_clk), 297 | .soc_clock (sys_clk), 298 | .io_hartId (6'h0 + i), 299 | .timer (timer), 300 | .logEnable (logEnable), 301 | .clean (clean), 302 | .dump (dump), 303 | .gateway_out (core_if_o[i]) 304 | ); 305 | end 306 |endgenerate 307 | CoreToGateway u_CoreToGateway( 308 | .gateway_out (gateway_if_i.out), 309 | .core_in (core_if_o) 310 | ); 311 | GatewayEndpoint u_GatewayEndpoint( 312 | .clock (sys_clk), 313 | .reset (sys_rstn), 314 | .gateway_in (gateway_if_i.in), 315 | .step () 316 | ); 317 | 318 |endmodule 319 """.stripMargin 320 FileRegisters.writeOutputFile("./build", "XSDiffTopChecker.sv", verilog) 321 } 322} 323