1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 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 system._ 24import device._ 25import chisel3.stage.ChiselGeneratorAnnotation 26import chipsalliance.rocketchip.config._ 27import device.{AXI4Plic, DebugModule, TLTimer} 28import freechips.rocketchip.diplomacy._ 29import freechips.rocketchip.tilelink._ 30import freechips.rocketchip.amba.axi4._ 31import freechips.rocketchip.devices.tilelink._ 32import freechips.rocketchip.diplomaticobjectmodel.logicaltree.GenericLogicalTreeNode 33import freechips.rocketchip.interrupts._ 34import freechips.rocketchip.jtag.JTAGIO 35import freechips.rocketchip.tile.{BusErrorUnit, BusErrorUnitParams, XLen} 36import freechips.rocketchip.tilelink 37import freechips.rocketchip.util.{ElaborationArtefacts, HasRocketChipStageUtils, UIntToOH1} 38import huancun.debug.TLLogger 39import huancun.{HCCacheParamsKey, HuanCun} 40import freechips.rocketchip.devices.debug.{DebugIO, ResetCtrlIO} 41 42abstract class BaseXSSoc()(implicit p: Parameters) extends LazyModule 43 with BindingScope 44{ 45 val misc = LazyModule(new SoCMisc()) 46 lazy val dts = DTS(bindingTree) 47 lazy val json = JSON(bindingTree) 48} 49 50class XSTop()(implicit p: Parameters) extends BaseXSSoc() with HasSoCParameter 51{ 52 ResourceBinding { 53 val width = ResourceInt(2) 54 val model = "freechips,rocketchip-unknown" 55 Resource(ResourceAnchors.root, "model").bind(ResourceString(model)) 56 Resource(ResourceAnchors.root, "compat").bind(ResourceString(model + "-dev")) 57 Resource(ResourceAnchors.soc, "compat").bind(ResourceString(model + "-soc")) 58 Resource(ResourceAnchors.root, "width").bind(width) 59 Resource(ResourceAnchors.soc, "width").bind(width) 60 Resource(ResourceAnchors.cpus, "width").bind(ResourceInt(1)) 61 def bindManagers(xbar: TLNexusNode) = { 62 ManagerUnification(xbar.edges.in.head.manager.managers).foreach{ manager => 63 manager.resources.foreach(r => r.bind(manager.toResource)) 64 } 65 } 66 bindManagers(misc.l3_xbar.asInstanceOf[TLNexusNode]) 67 bindManagers(misc.peripheralXbar.asInstanceOf[TLNexusNode]) 68 } 69 70 println(s"FPGASoC cores: $NumCores banks: $L3NBanks block size: $L3BlockSize bus size: $L3OuterBusWidth") 71 72 val core_with_l2 = tiles.map(coreParams => 73 LazyModule(new XSTile()(p.alterPartial({ 74 case XSCoreParamsKey => coreParams 75 }))) 76 ) 77 78 val l3cacheOpt = soc.L3CacheParamsOpt.map(l3param => 79 LazyModule(new HuanCun()(new Config((_, _, _) => { 80 case HCCacheParamsKey => l3param 81 }))) 82 ) 83 84 for (i <- 0 until NumCores) { 85 core_with_l2(i).clint_int_sink := misc.clint.intnode 86 core_with_l2(i).plic_int_sink :*= misc.plic.intnode 87 core_with_l2(i).debug_int_sink := misc.debugModule.debug.dmOuter.dmOuter.intnode 88 misc.plic.intnode := IntBuffer() := core_with_l2(i).beu_int_source 89 misc.peripheral_ports(i) := core_with_l2(i).uncache 90 misc.core_to_l3_ports(i) :=* core_with_l2(i).memory_port 91 } 92 93 l3cacheOpt.map(_.ctlnode.map(_ := misc.peripheralXbar)) 94 95 val core_rst_nodes = if(l3cacheOpt.nonEmpty && l3cacheOpt.get.rst_nodes.nonEmpty){ 96 l3cacheOpt.get.rst_nodes.get 97 } else { 98 core_with_l2.map(_ => BundleBridgeSource(() => Bool())) 99 } 100 101 core_rst_nodes.zip(core_with_l2.map(_.core_reset_sink)).foreach({ 102 case (source, sink) => sink := source 103 }) 104 105 l3cacheOpt match { 106 case Some(l3) => 107 misc.l3_out :*= l3.node :*= TLBuffer() :*= misc.l3_banked_xbar 108 case None => 109 } 110 111 lazy val module = new LazyRawModuleImp(this) { 112 ElaborationArtefacts.add("dts", dts) 113 ElaborationArtefacts.add("graphml", graphML) 114 ElaborationArtefacts.add("json", json) 115 ElaborationArtefacts.add("plusArgs", freechips.rocketchip.util.PlusArgArtefacts.serialize_cHeader()) 116 117 val dma = IO(Flipped(misc.dma.cloneType)) 118 val peripheral = IO(misc.peripheral.cloneType) 119 val memory = IO(misc.memory.cloneType) 120 121 misc.dma <> dma 122 peripheral <> misc.peripheral 123 memory <> misc.memory 124 125 val io = IO(new Bundle { 126 val clock = Input(Bool()) 127 val reset = Input(Bool()) 128 val sram_config = Input(UInt(16.W)) 129 val extIntrs = Input(UInt(NrExtIntr.W)) 130 val pll0_lock = Input(Bool()) 131 val pll0_ctrl = Output(Vec(6, UInt(32.W))) 132 val systemjtag = new Bundle { 133 val jtag = Flipped(new JTAGIO(hasTRSTn = false)) 134 val reset = Input(Bool()) // No reset allowed on top 135 val mfr_id = Input(UInt(11.W)) 136 val part_number = Input(UInt(16.W)) 137 val version = Input(UInt(4.W)) 138 } 139 val debug_reset = Output(Bool()) 140 }) 141 // override LazyRawModuleImp's clock and reset 142 childClock := io.clock.asClock 143 childReset := io.reset 144 145 // output 146 io.debug_reset := misc.module.debug_module_io.debugIO.ndreset 147 148 // input 149 dontTouch(io.sram_config) 150 dontTouch(io.pll0_lock) 151 misc.module.ext_intrs := io.extIntrs 152 misc.module.pll0_lock := io.pll0_lock 153 154 io.pll0_ctrl <> misc.module.pll0_ctrl 155 156 for ((core, i) <- core_with_l2.zipWithIndex) { 157 core.module.io.hartId := i.U 158 } 159 160 if(l3cacheOpt.isEmpty || l3cacheOpt.get.rst_nodes.isEmpty){ 161 // tie off core soft reset 162 for(node <- core_rst_nodes){ 163 node.out.head._1 := false.B 164 } 165 } 166 167 misc.module.debug_module_io.resetCtrl.hartIsInReset := core_with_l2.map(_.module.reset.asBool) 168 misc.module.debug_module_io.clock := io.clock 169 misc.module.debug_module_io.reset := io.reset 170 171 // TODO: use synchronizer? 172 misc.module.debug_module_io.debugIO.reset := io.systemjtag.reset 173 misc.module.debug_module_io.debugIO.clock := io.clock.asClock 174 // TODO: delay 3 cycles? 175 misc.module.debug_module_io.debugIO.dmactiveAck := misc.module.debug_module_io.debugIO.dmactive 176 // jtag connector 177 misc.module.debug_module_io.debugIO.systemjtag.foreach { x => 178 x.jtag <> io.systemjtag.jtag 179 x.reset := io.systemjtag.reset 180 x.mfr_id := io.systemjtag.mfr_id 181 x.part_number := io.systemjtag.part_number 182 x.version := io.systemjtag.version 183 } 184 185 withClockAndReset(io.clock.asClock, io.reset) { 186 // Modules are reset one by one 187 // reset ----> SYNC --> {L3 Cache, Cores} 188 // | 189 // v 190 // misc 191 val l3cacheMod = if (l3cacheOpt.isDefined) Seq(l3cacheOpt.get.module) else Seq() 192 val resetChain = Seq(l3cacheMod ++ core_with_l2.map(_.module)) 193 ResetGen(resetChain, io.reset, !debugOpts.FPGAPlatform) 194 } 195 196 } 197} 198 199object TopMain extends App with HasRocketChipStageUtils { 200 override def main(args: Array[String]): Unit = { 201 val (config, firrtlOpts) = ArgParser.parse(args) 202 val soc = DisableMonitors(p => LazyModule(new XSTop()(p)))(config) 203 XiangShanStage.execute(firrtlOpts, Seq( 204 ChiselGeneratorAnnotation(() => { 205 soc.module 206 }) 207 )) 208 ElaborationArtefacts.files.foreach{ case (extension, contents) => 209 writeOutputFile("./build", s"XSTop.${extension}", contents()) 210 } 211 } 212} 213