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 huancun.PrefetchRecv 24import utility._ 25import system._ 26import device._ 27import chisel3.stage.ChiselGeneratorAnnotation 28import org.chipsalliance.cde.config._ 29import freechips.rocketchip.diplomacy._ 30import freechips.rocketchip.tilelink._ 31import freechips.rocketchip.jtag.JTAGIO 32import huancun.{HCCacheParamsKey, HuanCun, HCCacheParameters} 33 34abstract class BaseXSSoc()(implicit p: Parameters) extends LazyModule 35 with BindingScope 36{ 37 val misc = LazyModule(new SoCMisc()) 38 lazy val dts = DTS(bindingTree) 39 lazy val json = JSON(bindingTree) 40} 41 42class XSTop()(implicit p: Parameters) extends BaseXSSoc() with HasSoCParameter 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 bindManagers(misc.l3_xbar.asInstanceOf[TLNexusNode]) 59 bindManagers(misc.peripheralXbar.asInstanceOf[TLNexusNode]) 60 } 61 62 println(s"FPGASoC cores: $NumCores banks: $L3NBanks block size: $L3BlockSize bus size: $L3OuterBusWidth") 63 64 val core_with_l2 = tiles.map(coreParams => 65 LazyModule(new XSTile()(p.alterPartial({ 66 case XSCoreParamsKey => coreParams 67 }))) 68 ) 69 70 val l3cacheOpt = soc.L3CacheParamsOpt.map(l3param => 71 LazyModule(new HuanCun()(new Config((_, _, _) => { 72 case HCCacheParamsKey => l3param.copy( 73 hartIds = tiles.map(_.HartId), 74 FPGAPlatform = debugOpts.FPGAPlatform 75 ) 76 }))) 77 ) 78 79 // recieve all prefetch req from cores 80 val memblock_pf_recv_nodes: Seq[Option[BundleBridgeSink[PrefetchRecv]]] = core_with_l2.map(_.core_l3_pf_port).map{ 81 x => x.map(_ => BundleBridgeSink(Some(() => new PrefetchRecv))) 82 } 83 84 val l3_pf_sender_opt = soc.L3CacheParamsOpt.getOrElse(HCCacheParameters()).prefetch match { 85 case Some(pf) => Some(BundleBridgeSource(() => new PrefetchRecv)) 86 case None => None 87 } 88 89 for (i <- 0 until NumCores) { 90 core_with_l2(i).clint_int_sink := misc.clint.intnode 91 core_with_l2(i).plic_int_sink :*= misc.plic.intnode 92 core_with_l2(i).debug_int_sink := misc.debugModule.debug.dmOuter.dmOuter.intnode 93 misc.plic.intnode := IntBuffer() := core_with_l2(i).beu_int_source 94 misc.peripheral_ports(i) := core_with_l2(i).uncache 95 misc.core_to_l3_ports(i) :=* core_with_l2(i).memory_port 96 memblock_pf_recv_nodes(i).map(recv => { 97 println(s"Connecting Core_${i}'s L1 pf source to L3!") 98 recv := core_with_l2(i).core_l3_pf_port.get 99 }) 100 } 101 102 l3cacheOpt.map(_.ctlnode.map(_ := misc.peripheralXbar)) 103 l3cacheOpt.map(_.intnode.map(int => { 104 misc.plic.intnode := IntBuffer() := int 105 })) 106 107 val core_rst_nodes = if(l3cacheOpt.nonEmpty && l3cacheOpt.get.rst_nodes.nonEmpty){ 108 l3cacheOpt.get.rst_nodes.get 109 } else { 110 core_with_l2.map(_ => BundleBridgeSource(() => Reset())) 111 } 112 113 core_rst_nodes.zip(core_with_l2.map(_.core_reset_sink)).foreach({ 114 case (source, sink) => sink := source 115 }) 116 117 l3cacheOpt match { 118 case Some(l3) => 119 misc.l3_out :*= l3.node :*= misc.l3_banked_xbar 120 l3.pf_recv_node.map(recv => { 121 println("Connecting L1 prefetcher to L3!") 122 recv := l3_pf_sender_opt.get 123 }) 124 case None => 125 } 126 127 class XSTopImp(wrapper: LazyModule) extends LazyRawModuleImp(wrapper) { 128 FileRegisters.add("dts", dts) 129 FileRegisters.add("graphml", graphML) 130 FileRegisters.add("json", json) 131 FileRegisters.add("plusArgs", freechips.rocketchip.util.PlusArgArtefacts.serialize_cHeader()) 132 133 val dma = IO(Flipped(misc.dma.cloneType)) 134 val peripheral = IO(misc.peripheral.cloneType) 135 val memory = IO(misc.memory.cloneType) 136 137 misc.dma <> dma 138 peripheral <> misc.peripheral 139 memory <> misc.memory 140 141 val io = IO(new Bundle { 142 val clock = Input(Bool()) 143 val reset = Input(AsyncReset()) 144 val sram_config = Input(UInt(16.W)) 145 val extIntrs = Input(UInt(NrExtIntr.W)) 146 val pll0_lock = Input(Bool()) 147 val pll0_ctrl = Output(Vec(6, UInt(32.W))) 148 val systemjtag = new Bundle { 149 val jtag = Flipped(new JTAGIO(hasTRSTn = false)) 150 val reset = Input(AsyncReset()) // No reset allowed on top 151 val mfr_id = Input(UInt(11.W)) 152 val part_number = Input(UInt(16.W)) 153 val version = Input(UInt(4.W)) 154 } 155 val debug_reset = Output(Bool()) 156 val rtc_clock = Input(Bool()) 157 val cacheable_check = new TLPMAIO() 158 val riscv_halt = Output(Vec(NumCores, Bool())) 159 val riscv_rst_vec = Input(Vec(NumCores, UInt(38.W))) 160 }) 161 162 val reset_sync = withClockAndReset(io.clock.asClock, io.reset) { ResetGen() } 163 val jtag_reset_sync = withClockAndReset(io.systemjtag.jtag.TCK, io.systemjtag.reset) { ResetGen() } 164 165 // override LazyRawModuleImp's clock and reset 166 childClock := io.clock.asClock 167 childReset := reset_sync 168 169 // output 170 io.debug_reset := misc.module.debug_module_io.debugIO.ndreset 171 172 // input 173 dontTouch(dma) 174 dontTouch(io) 175 dontTouch(peripheral) 176 dontTouch(memory) 177 misc.module.ext_intrs := io.extIntrs 178 misc.module.rtc_clock := io.rtc_clock 179 misc.module.pll0_lock := io.pll0_lock 180 misc.module.cacheable_check <> io.cacheable_check 181 182 io.pll0_ctrl <> misc.module.pll0_ctrl 183 184 for ((core, i) <- core_with_l2.zipWithIndex) { 185 core.module.io.hartId := i.U 186 io.riscv_halt(i) := core.module.io.cpu_halt 187 core.module.io.reset_vector := io.riscv_rst_vec(i) 188 } 189 190 if(l3cacheOpt.isEmpty || l3cacheOpt.get.rst_nodes.isEmpty){ 191 // tie off core soft reset 192 for(node <- core_rst_nodes){ 193 node.out.head._1 := false.B.asAsyncReset 194 } 195 } 196 197 l3cacheOpt match { 198 case Some(l3) => 199 l3.pf_recv_node match { 200 case Some(recv) => 201 l3_pf_sender_opt.get.out.head._1.addr_valid := VecInit(memblock_pf_recv_nodes.map(_.get.in.head._1.addr_valid)).asUInt.orR 202 for (i <- 0 until NumCores) { 203 when(memblock_pf_recv_nodes(i).get.in.head._1.addr_valid) { 204 l3_pf_sender_opt.get.out.head._1.addr := memblock_pf_recv_nodes(i).get.in.head._1.addr 205 l3_pf_sender_opt.get.out.head._1.l2_pf_en := memblock_pf_recv_nodes(i).get.in.head._1.l2_pf_en 206 } 207 } 208 case None => 209 } 210 l3.module.io.debugTopDown.robHeadPaddr := core_with_l2.map(_.module.io.debugTopDown.robHeadPaddr) 211 core_with_l2.zip(l3.module.io.debugTopDown.addrMatch).foreach { case (tile, l3Match) => tile.module.io.debugTopDown.l3MissMatch := l3Match } 212 case None => core_with_l2.foreach(_.module.io.debugTopDown.l3MissMatch := false.B) 213 } 214 215 misc.module.debug_module_io.resetCtrl.hartIsInReset := core_with_l2.map(_.module.reset.asBool) 216 misc.module.debug_module_io.clock := io.clock 217 misc.module.debug_module_io.reset := reset_sync 218 219 misc.module.debug_module_io.debugIO.reset := misc.module.reset 220 misc.module.debug_module_io.debugIO.clock := io.clock.asClock 221 // TODO: delay 3 cycles? 222 misc.module.debug_module_io.debugIO.dmactiveAck := misc.module.debug_module_io.debugIO.dmactive 223 // jtag connector 224 misc.module.debug_module_io.debugIO.systemjtag.foreach { x => 225 x.jtag <> io.systemjtag.jtag 226 x.reset := jtag_reset_sync 227 x.mfr_id := io.systemjtag.mfr_id 228 x.part_number := io.systemjtag.part_number 229 x.version := io.systemjtag.version 230 } 231 232 withClockAndReset(io.clock.asClock, reset_sync) { 233 // Modules are reset one by one 234 // reset ----> SYNC --> {SoCMisc, L3 Cache, Cores} 235 val resetChain = Seq(Seq(misc.module) ++ l3cacheOpt.map(_.module) ++ core_with_l2.map(_.module)) 236 ResetGen(resetChain, reset_sync, !debugOpts.FPGAPlatform) 237 } 238 239 } 240 241 lazy val module = new XSTopImp(this) 242} 243 244object TopMain extends App { 245 override def main(args: Array[String]): Unit = { 246 val (config, firrtlOpts, firrtlComplier, firtoolOpts) = ArgParser.parse(args) 247 248 // tools: init to close dpi-c when in fpga 249 val envInFPGA = config(DebugOptionsKey).FPGAPlatform 250 val enableChiselDB = config(DebugOptionsKey).EnableChiselDB 251 val enableConstantin = config(DebugOptionsKey).EnableConstantin 252 Constantin.init(enableConstantin && !envInFPGA) 253 ChiselDB.init(enableChiselDB && !envInFPGA) 254 255 val soc = DisableMonitors(p => LazyModule(new XSTop()(p)))(config) 256 Generator.execute(firrtlOpts, soc.module, firrtlComplier, firtoolOpts) 257 FileRegisters.write(fileDir = "./build", filePrefix = "XSTop.") 258 } 259} 260