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 system 18 19import org.chipsalliance.cde.config.{Field, Parameters} 20import chisel3._ 21import chisel3.util._ 22import device.{DebugModule, TLPMA, TLPMAIO} 23import freechips.rocketchip.amba.axi4._ 24import freechips.rocketchip.devices.tilelink._ 25import freechips.rocketchip.diplomacy.{AddressSet, IdRange, InModuleBody, LazyModule, LazyModuleImp, MemoryDevice, RegionType, SimpleDevice, TransferSizes} 26import freechips.rocketchip.interrupts.{IntSourceNode, IntSourcePortSimple} 27import freechips.rocketchip.regmapper.{RegField, RegFieldDesc, RegFieldGroup} 28import freechips.rocketchip.tilelink._ 29import huancun._ 30import top.BusPerfMonitor 31import utility.{ReqSourceKey, TLClientsMerger, TLEdgeBuffer, TLLogger} 32import xiangshan.backend.fu.PMAConst 33import xiangshan.{DebugOptionsKey, XSTileKey} 34 35case object SoCParamsKey extends Field[SoCParameters] 36 37case class SoCParameters 38( 39 EnableILA: Boolean = false, 40 PAddrBits: Int = 36, 41 extIntrs: Int = 64, 42 L3NBanks: Int = 4, 43 L3CacheParamsOpt: Option[HCCacheParameters] = Some(HCCacheParameters( 44 name = "L3", 45 level = 3, 46 ways = 8, 47 sets = 2048 // 1MB per bank 48 )), 49 XSTopPrefix: Option[String] = None 50){ 51 // L3 configurations 52 val L3InnerBusWidth = 256 53 val L3BlockSize = 64 54 // on chip network configurations 55 val L3OuterBusWidth = 256 56} 57 58trait HasSoCParameter { 59 implicit val p: Parameters 60 61 val soc = p(SoCParamsKey) 62 val debugOpts = p(DebugOptionsKey) 63 val tiles = p(XSTileKey) 64 65 val NumCores = tiles.size 66 val EnableILA = soc.EnableILA 67 68 // L3 configurations 69 val L3InnerBusWidth = soc.L3InnerBusWidth 70 val L3BlockSize = soc.L3BlockSize 71 val L3NBanks = soc.L3NBanks 72 73 // on chip network configurations 74 val L3OuterBusWidth = soc.L3OuterBusWidth 75 76 val NrExtIntr = soc.extIntrs 77} 78 79class ILABundle extends Bundle {} 80 81 82abstract class BaseSoC()(implicit p: Parameters) extends LazyModule with HasSoCParameter { 83 val bankedNode = BankBinder(L3NBanks, L3BlockSize) 84 val peripheralXbar = TLXbar() 85 val l3_xbar = TLXbar() 86 val l3_banked_xbar = TLXbar() 87} 88 89// We adapt the following three traits from rocket-chip. 90// Source: rocket-chip/src/main/scala/subsystem/Ports.scala 91trait HaveSlaveAXI4Port { 92 this: BaseSoC => 93 94 val idBits = 14 95 96 val l3FrontendAXI4Node = AXI4MasterNode(Seq(AXI4MasterPortParameters( 97 Seq(AXI4MasterParameters( 98 name = "dma", 99 id = IdRange(0, 1 << idBits) 100 )) 101 ))) 102 private val errorDevice = LazyModule(new TLError( 103 params = DevNullParams( 104 address = Seq(AddressSet(0x0, 0x7fffffffL)), 105 maxAtomic = 8, 106 maxTransfer = 64), 107 beatBytes = L3InnerBusWidth / 8 108 )) 109 private val error_xbar = TLXbar() 110 111 l3_xbar := 112 TLFIFOFixer() := 113 TLWidthWidget(32) := 114 AXI4ToTL() := 115 AXI4UserYanker(Some(1)) := 116 AXI4Fragmenter() := 117 AXI4Buffer() := 118 AXI4Buffer() := 119 AXI4IdIndexer(1) := 120 l3FrontendAXI4Node 121 errorDevice.node := l3_xbar 122 123 val dma = InModuleBody { 124 l3FrontendAXI4Node.makeIOs() 125 } 126} 127 128trait HaveAXI4MemPort { 129 this: BaseSoC => 130 val device = new MemoryDevice 131 // 36-bit physical address 132 val memRange = AddressSet(0x00000000L, 0xfffffffffL).subtract(AddressSet(0x0L, 0x7fffffffL)) 133 val memAXI4SlaveNode = AXI4SlaveNode(Seq( 134 AXI4SlavePortParameters( 135 slaves = Seq( 136 AXI4SlaveParameters( 137 address = memRange, 138 regionType = RegionType.UNCACHED, 139 executable = true, 140 supportsRead = TransferSizes(1, L3BlockSize), 141 supportsWrite = TransferSizes(1, L3BlockSize), 142 interleavedId = Some(0), 143 resources = device.reg("mem") 144 ) 145 ), 146 beatBytes = L3OuterBusWidth / 8, 147 requestKeys = if (debugOpts.FPGAPlatform) Seq() else Seq(ReqSourceKey), 148 ) 149 )) 150 151 val mem_xbar = TLXbar() 152 val l3_mem_pmu = BusPerfMonitor(name = "L3_Mem", enable = !debugOpts.FPGAPlatform, stat_latency = true) 153 mem_xbar :=* 154 TLBuffer.chainNode(2) := 155 TLCacheCork() := 156 l3_mem_pmu := 157 TLClientsMerger() := 158 TLXbar() :=* 159 bankedNode 160 161 mem_xbar := 162 TLWidthWidget(8) := 163 TLBuffer.chainNode(3, name = Some("PeripheralXbar_to_MemXbar_buffer")) := 164 peripheralXbar 165 166 memAXI4SlaveNode := 167 AXI4Buffer() := 168 AXI4Buffer() := 169 AXI4Buffer() := 170 AXI4IdIndexer(idBits = 14) := 171 AXI4UserYanker() := 172 AXI4Deinterleaver(L3BlockSize) := 173 TLToAXI4() := 174 TLSourceShrinker(64) := 175 TLWidthWidget(L3OuterBusWidth / 8) := 176 TLBuffer.chainNode(2) := 177 mem_xbar 178 179 val memory = InModuleBody { 180 memAXI4SlaveNode.makeIOs() 181 } 182} 183 184trait HaveAXI4PeripheralPort { this: BaseSoC => 185 // on-chip devices: 0x3800_0000 - 0x3fff_ffff 0x0000_0000 - 0x0000_0fff 186 val onChipPeripheralRange = AddressSet(0x38000000L, 0x07ffffffL) 187 val uartRange = AddressSet(0x40600000, 0xf) 188 val uartDevice = new SimpleDevice("serial", Seq("xilinx,uartlite")) 189 val uartParams = AXI4SlaveParameters( 190 address = Seq(uartRange), 191 regionType = RegionType.UNCACHED, 192 supportsRead = TransferSizes(1, 8), 193 supportsWrite = TransferSizes(1, 8), 194 resources = uartDevice.reg 195 ) 196 val peripheralRange = AddressSet( 197 0x0, 0x7fffffff 198 ).subtract(onChipPeripheralRange).flatMap(x => x.subtract(uartRange)) 199 val peripheralNode = AXI4SlaveNode(Seq(AXI4SlavePortParameters( 200 Seq(AXI4SlaveParameters( 201 address = peripheralRange, 202 regionType = RegionType.UNCACHED, 203 supportsRead = TransferSizes(1, 8), 204 supportsWrite = TransferSizes(1, 8), 205 interleavedId = Some(0) 206 ), uartParams), 207 beatBytes = 8 208 ))) 209 210 peripheralNode := 211 AXI4UserYanker() := 212 AXI4IdIndexer(idBits = 2) := 213 AXI4Buffer() := 214 AXI4Buffer() := 215 AXI4Buffer() := 216 AXI4Buffer() := 217 AXI4UserYanker() := 218 AXI4Deinterleaver(8) := 219 TLToAXI4() := 220 TLBuffer.chainNode(3) := 221 peripheralXbar 222 223 val peripheral = InModuleBody { 224 peripheralNode.makeIOs() 225 } 226 227} 228 229class SoCMisc()(implicit p: Parameters) extends BaseSoC 230 with HaveAXI4MemPort 231 with HaveAXI4PeripheralPort 232 with PMAConst 233 with HaveSlaveAXI4Port 234{ 235 val peripheral_ports = Array.fill(NumCores) { TLTempNode() } 236 val core_to_l3_ports = Array.fill(NumCores) { TLTempNode() } 237 238 val l3_in = TLTempNode() 239 val l3_out = TLTempNode() 240 241 l3_in :*= TLEdgeBuffer(_ => true, Some("L3_in_buffer")) :*= l3_banked_xbar 242 bankedNode :*= TLLogger("MEM_L3", !debugOpts.FPGAPlatform && debugOpts.AlwaysBasicDB) :*= l3_out 243 244 if(soc.L3CacheParamsOpt.isEmpty){ 245 l3_out :*= l3_in 246 } 247 248 for(port <- peripheral_ports) { 249 peripheralXbar := TLBuffer.chainNode(2, Some("L2_to_L3_peripheral_buffer")) := port 250 } 251 252 for ((core_out, i) <- core_to_l3_ports.zipWithIndex){ 253 l3_banked_xbar :=* 254 TLLogger(s"L3_L2_$i", !debugOpts.FPGAPlatform && debugOpts.AlwaysBasicDB) :=* 255 TLBuffer() := 256 core_out 257 } 258 l3_banked_xbar := TLBuffer.chainNode(2) := l3_xbar 259 260 val clint = LazyModule(new CLINT(CLINTParams(0x38000000L), 8)) 261 clint.node := peripheralXbar 262 263 class IntSourceNodeToModule(val num: Int)(implicit p: Parameters) extends LazyModule { 264 val sourceNode = IntSourceNode(IntSourcePortSimple(num, ports = 1, sources = 1)) 265 class IntSourceNodeToModuleImp(wrapper: LazyModule) extends LazyModuleImp(wrapper) { 266 val in = IO(Input(Vec(num, Bool()))) 267 in.zip(sourceNode.out.head._1).foreach{ case (i, s) => s := i } 268 } 269 lazy val module = new IntSourceNodeToModuleImp(this) 270 } 271 272 val plic = LazyModule(new TLPLIC(PLICParams(0x3c000000L), 8)) 273 val plicSource = LazyModule(new IntSourceNodeToModule(NrExtIntr)) 274 275 plic.intnode := plicSource.sourceNode 276 plic.node := peripheralXbar 277 278 val pll_node = TLRegisterNode( 279 address = Seq(AddressSet(0x3a000000L, 0xfff)), 280 device = new SimpleDevice("pll_ctrl", Seq()), 281 beatBytes = 8, 282 concurrency = 1 283 ) 284 pll_node := peripheralXbar 285 286 val debugModule = LazyModule(new DebugModule(NumCores)(p)) 287 debugModule.debug.node := peripheralXbar 288 debugModule.debug.dmInner.dmInner.sb2tlOpt.foreach { sb2tl => 289 l3_xbar := TLBuffer() := sb2tl.node 290 } 291 292 val pma = LazyModule(new TLPMA) 293 pma.node := 294 TLBuffer.chainNode(4) := 295 peripheralXbar 296 297 class SoCMiscImp(wrapper: LazyModule) extends LazyModuleImp(wrapper) { 298 299 val debug_module_io = IO(new debugModule.DebugModuleIO) 300 val ext_intrs = IO(Input(UInt(NrExtIntr.W))) 301 val rtc_clock = IO(Input(Bool())) 302 val pll0_lock = IO(Input(Bool())) 303 val pll0_ctrl = IO(Output(Vec(6, UInt(32.W)))) 304 val cacheable_check = IO(new TLPMAIO) 305 306 debugModule.module.io <> debug_module_io 307 308 // sync external interrupts 309 require(plicSource.module.in.length == ext_intrs.getWidth) 310 for ((plic_in, interrupt) <- plicSource.module.in.zip(ext_intrs.asBools)) { 311 val ext_intr_sync = RegInit(0.U(3.W)) 312 ext_intr_sync := Cat(ext_intr_sync(1, 0), interrupt) 313 plic_in := ext_intr_sync(2) 314 } 315 316 pma.module.io <> cacheable_check 317 318 // positive edge sampling of the lower-speed rtc_clock 319 val rtcTick = RegInit(0.U(3.W)) 320 rtcTick := Cat(rtcTick(1, 0), rtc_clock) 321 clint.module.io.rtcTick := rtcTick(1) && !rtcTick(2) 322 323 val pll_ctrl_regs = Seq.fill(6){ RegInit(0.U(32.W)) } 324 val pll_lock = RegNext(next = pll0_lock, init = false.B) 325 326 pll0_ctrl <> VecInit(pll_ctrl_regs) 327 328 pll_node.regmap( 329 0x000 -> RegFieldGroup( 330 "Pll", Some("PLL ctrl regs"), 331 pll_ctrl_regs.zipWithIndex.map{ 332 case (r, i) => RegField(32, r, RegFieldDesc( 333 s"PLL_ctrl_$i", 334 desc = s"PLL ctrl register #$i" 335 )) 336 } :+ RegField.r(32, Cat(0.U(31.W), pll_lock), RegFieldDesc( 337 "PLL_lock", 338 "PLL lock register" 339 )) 340 ) 341 ) 342 } 343 344 lazy val module = new SoCMiscImp(this) 345} 346