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 chipsalliance.rocketchip.config.{Field, Parameters} 20import chisel3._ 21import chisel3.util._ 22import device.DebugModule 23import freechips.rocketchip.amba.axi4.{AXI4Buffer, AXI4Deinterleaver, AXI4Fragmenter, AXI4IdIndexer, AXI4MasterNode, AXI4MasterParameters, AXI4MasterPortParameters, AXI4SlaveNode, AXI4SlaveParameters, AXI4SlavePortParameters, AXI4ToTL, AXI4UserYanker} 24import freechips.rocketchip.devices.tilelink.{CLINT, CLINTParams, DevNullParams, PLICParams, TLError, TLPLIC} 25import freechips.rocketchip.diplomacy.{AddressSet, IdRange, InModuleBody, LazyModule, LazyModuleImp, MemoryDevice, RegionType, SimpleDevice, TransferSizes} 26import freechips.rocketchip.interrupts.{IntSourceNode, IntSourcePortSimple} 27import xiangshan.{DebugOptionsKey, HasXSParameter, XSBundle, XSCore, XSCoreParameters} 28import freechips.rocketchip.tile.{BusErrorUnit, BusErrorUnitParams, BusErrors, L1BusErrors} 29import freechips.rocketchip.tilelink.{BankBinder, TLBuffer, TLCacheCork, TLFIFOFixer, TLTempNode, TLToAXI4, TLWidthWidget, TLXbar} 30import huancun.debug.TLLogger 31import huancun.{CacheParameters, HCCacheParameters} 32import top.BusPerfMonitor 33 34case object SoCParamsKey extends Field[SoCParameters] 35 36case class SoCParameters 37( 38 cores: List[XSCoreParameters], 39 EnableILA: Boolean = false, 40 extIntrs: Int = 150, 41 L3NBanks: Int = 4, 42 L3CacheParamsOpt: Option[HCCacheParameters] = Some(HCCacheParameters( 43 name = "l3", 44 level = 3, 45 ways = 8, 46 sets = 2048 // 1MB per bank 47 )) 48){ 49 val PAddrBits = cores.map(_.PAddrBits).reduce((x, y) => if(x > y) x else y) 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 NumCores = soc.cores.size 63 val EnableILA = soc.EnableILA 64 65 // L3 configurations 66 val L3InnerBusWidth = soc.L3InnerBusWidth 67 val L3BlockSize = soc.L3BlockSize 68 val L3NBanks = soc.L3NBanks 69 70 // on chip network configurations 71 val L3OuterBusWidth = soc.L3OuterBusWidth 72 73 val NrExtIntr = soc.extIntrs 74} 75 76class ILABundle extends Bundle {} 77 78 79abstract class BaseSoC()(implicit p: Parameters) extends LazyModule with HasSoCParameter { 80 val bankedNode = BankBinder(L3NBanks, L3BlockSize) 81 val peripheralXbar = TLXbar() 82 val l3_xbar = TLXbar() 83} 84 85// We adapt the following three traits from rocket-chip. 86// Source: rocket-chip/src/main/scala/subsystem/Ports.scala 87trait HaveSlaveAXI4Port { 88 this: BaseSoC => 89 90 val idBits = 14 91 92 val l3FrontendAXI4Node = AXI4MasterNode(Seq(AXI4MasterPortParameters( 93 Seq(AXI4MasterParameters( 94 name = "dma", 95 id = IdRange(0, 1 << idBits) 96 )) 97 ))) 98 private val errorDevice = LazyModule(new TLError( 99 params = DevNullParams( 100 address = Seq(AddressSet(0x0, 0x7fffffffL)), 101 maxAtomic = 8, 102 maxTransfer = 64), 103 beatBytes = L3InnerBusWidth / 8 104 )) 105 private val error_xbar = TLXbar() 106 107 error_xbar := 108 TLFIFOFixer() := 109 TLWidthWidget(16) := 110 AXI4ToTL() := 111 AXI4UserYanker(Some(1)) := 112 AXI4Fragmenter() := 113 AXI4IdIndexer(1) := 114 l3FrontendAXI4Node 115 errorDevice.node := error_xbar 116 l3_xbar := 117 TLBuffer() := 118 error_xbar 119 120 val dma = InModuleBody { 121 l3FrontendAXI4Node.makeIOs() 122 } 123} 124 125trait HaveAXI4MemPort { 126 this: BaseSoC => 127 val device = new MemoryDevice 128 // 40-bit physical address 129 val memRange = AddressSet(0x00000000L, 0xffffffffffL).subtract(AddressSet(0x0L, 0x7fffffffL)) 130 val memAXI4SlaveNode = AXI4SlaveNode(Seq( 131 AXI4SlavePortParameters( 132 slaves = Seq( 133 AXI4SlaveParameters( 134 address = memRange, 135 regionType = RegionType.UNCACHED, 136 executable = true, 137 supportsRead = TransferSizes(1, L3BlockSize), 138 supportsWrite = TransferSizes(1, L3BlockSize), 139 interleavedId = Some(0), 140 resources = device.reg("mem") 141 ) 142 ), 143 beatBytes = L3OuterBusWidth / 8 144 ) 145 )) 146 147 def mem_buffN(n: Int) = { 148 val buffers = (0 until n).map(_ => AXI4Buffer()) 149 buffers.reduce((l, r) => l := r) 150 (buffers.head, buffers.last) 151 } 152 val mem_xbar = TLXbar() 153 mem_xbar :=* TLCacheCork() :=* bankedNode 154 val (buf_l, buf_r) = mem_buffN(5) 155 memAXI4SlaveNode := buf_l 156 buf_r := 157 AXI4UserYanker() := 158 AXI4Deinterleaver(L3BlockSize) := 159 TLToAXI4() := 160 TLWidthWidget(L3OuterBusWidth / 8) := 161 mem_xbar 162 163 val memory = InModuleBody { 164 memAXI4SlaveNode.makeIOs() 165 } 166} 167 168trait HaveAXI4PeripheralPort { this: BaseSoC => 169 // on-chip devices: 0x3800_0000 - 0x3fff_ffff 0x0000_0000 - 0x0000_0fff 170 val onChipPeripheralRange = AddressSet(0x38000000L, 0x07ffffffL) 171 val uartRange = AddressSet(0x40600000, 0xf) 172 val uartDevice = new SimpleDevice("serial", Seq("xilinx,uartlite")) 173 val uartParams = AXI4SlaveParameters( 174 address = Seq(uartRange), 175 regionType = RegionType.UNCACHED, 176 supportsRead = TransferSizes(1, 8), 177 supportsWrite = TransferSizes(1, 8), 178 resources = uartDevice.reg 179 ) 180 val peripheralRange = AddressSet( 181 0x0, 0x7fffffff 182 ).subtract(onChipPeripheralRange).flatMap(x => x.subtract(uartRange)) 183 val peripheralNode = AXI4SlaveNode(Seq(AXI4SlavePortParameters( 184 Seq(AXI4SlaveParameters( 185 address = peripheralRange, 186 regionType = RegionType.UNCACHED, 187 supportsRead = TransferSizes(1, 8), 188 supportsWrite = TransferSizes(1, 8), 189 interleavedId = Some(0) 190 ), uartParams), 191 beatBytes = 8 192 ))) 193 194 peripheralNode := 195 AXI4UserYanker() := 196 AXI4Deinterleaver(8) := 197 TLToAXI4() := 198 peripheralXbar 199 200 val peripheral = InModuleBody { 201 peripheralNode.makeIOs() 202 } 203 204} 205 206class SoCMisc()(implicit p: Parameters) extends BaseSoC 207 with HaveAXI4MemPort 208 with HaveAXI4PeripheralPort 209 with HaveSlaveAXI4Port 210{ 211 val peripheral_ports = Array.fill(NumCores) { TLTempNode() } 212 val core_to_l3_ports = Array.fill(NumCores) { TLTempNode() } 213 214 val l3_in = TLTempNode() 215 val l3_out = TLTempNode() 216 val l3_mem_pmu = BusPerfMonitor(enable = !debugOpts.FPGAPlatform) 217 218 l3_in :*= l3_xbar 219 bankedNode :*= TLLogger("MEM_L3", !debugOpts.FPGAPlatform) :*= l3_mem_pmu :*= l3_out 220 221 if(soc.L3CacheParamsOpt.isEmpty){ 222 l3_out :*= l3_in 223 } 224 225 for(port <- peripheral_ports) { 226 peripheralXbar := port 227 } 228 229 for ((core_out, i) <- core_to_l3_ports.zipWithIndex){ 230 l3_xbar :=* TLLogger(s"L3_L2_$i", !debugOpts.FPGAPlatform) :=* core_out 231 } 232 233 val clint = LazyModule(new CLINT(CLINTParams(0x38000000L), 8)) 234 clint.node := peripheralXbar 235 236 class IntSourceNodeToModule(val num: Int)(implicit p: Parameters) extends LazyModule { 237 val sourceNode = IntSourceNode(IntSourcePortSimple(num, ports = 1, sources = 1)) 238 lazy val module = new LazyModuleImp(this){ 239 val in = IO(Input(Vec(num, Bool()))) 240 in.zip(sourceNode.out.head._1).foreach{ case (i, s) => s := i } 241 } 242 } 243 244 val plic = LazyModule(new TLPLIC(PLICParams(0x3c000000L), 8)) 245 val plicSource = LazyModule(new IntSourceNodeToModule(NrExtIntr)) 246 247 plic.intnode := plicSource.sourceNode 248 plic.node := peripheralXbar 249 250 val debugModule = LazyModule(new DebugModule(NumCores)(p)) 251 debugModule.debug.node := peripheralXbar 252 debugModule.debug.dmInner.dmInner.sb2tlOpt.foreach { sb2tl => 253 l3_xbar := TLBuffer() := TLWidthWidget(1) := sb2tl.node 254 } 255 256 lazy val module = new LazyModuleImp(this){ 257 258 val debug_module_io = IO(chiselTypeOf(debugModule.module.io)) 259 val ext_intrs = IO(Input(UInt(NrExtIntr.W))) 260 261 debugModule.module.io <> debug_module_io 262 plicSource.module.in := ext_intrs.asBools 263 264 val freq = 100 265 val cnt = RegInit(freq.U) 266 val tick = cnt === 0.U 267 cnt := Mux(tick, freq.U, cnt - 1.U) 268 clint.module.io.rtcTick := tick 269 } 270} 271