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