1package xiangshan 2 3import chisel3._ 4import chisel3.util._ 5import top.Parameters 6import xiangshan.backend._ 7import xiangshan.backend.dispatch.DispatchParameters 8import xiangshan.backend.exu.ExuParameters 9import xiangshan.backend.exu.Exu._ 10import xiangshan.frontend._ 11import xiangshan.mem._ 12import xiangshan.backend.fu.HasExceptionNO 13import xiangshan.cache.{DCache, InstrUncache, DCacheParameters, ICache, ICacheParameters, L1plusCache, L1plusCacheParameters, PTW, PTWRepeater, Uncache, MemoryOpConstants, MissReq} 14import xiangshan.cache.prefetch._ 15import chipsalliance.rocketchip.config 16import freechips.rocketchip.diplomacy.{AddressSet, LazyModule, LazyModuleImp} 17import freechips.rocketchip.tilelink.{TLBuffer, TLBundleParameters, TLCacheCork, TLClientNode, TLFilter, TLIdentityNode, TLToAXI4, TLWidthWidget, TLXbar} 18import freechips.rocketchip.devices.tilelink.{DevNullParams, TLError} 19import sifive.blocks.inclusivecache.{CacheParameters, InclusiveCache, InclusiveCacheMicroParameters} 20import freechips.rocketchip.amba.axi4.{AXI4Deinterleaver, AXI4Fragmenter, AXI4IdIndexer, AXI4IdentityNode, AXI4ToTL, AXI4UserYanker} 21import freechips.rocketchip.tile.HasFPUParameters 22import sifive.blocks.inclusivecache.PrefetcherIO 23import utils._ 24 25object hartIdCore extends (() => Int) { 26 var x = 0 27 28 def apply(): Int = { 29 x = x + 1 30 x - 1 31 } 32} 33 34case class XSCoreParameters 35( 36 XLEN: Int = 64, 37 HasMExtension: Boolean = true, 38 HasCExtension: Boolean = true, 39 HasDiv: Boolean = true, 40 HasICache: Boolean = true, 41 HasDCache: Boolean = true, 42 EnableStoreQueue: Boolean = true, 43 AddrBits: Int = 64, 44 VAddrBits: Int = 39, 45 PAddrBits: Int = 40, 46 HasFPU: Boolean = true, 47 FetchWidth: Int = 8, 48 EnableBPU: Boolean = true, 49 EnableBPD: Boolean = true, 50 EnableRAS: Boolean = true, 51 EnableLB: Boolean = false, 52 EnableLoop: Boolean = true, 53 EnableSC: Boolean = false, 54 EnbaleTlbDebug: Boolean = false, 55 EnableJal: Boolean = false, 56 EnableUBTB: Boolean = true, 57 HistoryLength: Int = 64, 58 BtbSize: Int = 2048, 59 JbtacSize: Int = 1024, 60 JbtacBanks: Int = 8, 61 RasSize: Int = 16, 62 CacheLineSize: Int = 512, 63 UBtbWays: Int = 16, 64 BtbWays: Int = 2, 65 66 EnableL1plusPrefetcher: Boolean = true, 67 IBufSize: Int = 32, 68 DecodeWidth: Int = 6, 69 RenameWidth: Int = 6, 70 CommitWidth: Int = 6, 71 BrqSize: Int = 32, 72 FtqSize: Int = 48, 73 IssQueSize: Int = 12, 74 NRPhyRegs: Int = 160, 75 NRIntReadPorts: Int = 14, 76 NRIntWritePorts: Int = 8, 77 NRFpReadPorts: Int = 14, 78 NRFpWritePorts: Int = 8, 79 LoadQueueSize: Int = 64, 80 StoreQueueSize: Int = 48, 81 RoqSize: Int = 192, 82 dpParams: DispatchParameters = DispatchParameters( 83 IntDqSize = 16, 84 FpDqSize = 16, 85 LsDqSize = 16, 86 IntDqDeqWidth = 4, 87 FpDqDeqWidth = 4, 88 LsDqDeqWidth = 4 89 ), 90 exuParameters: ExuParameters = ExuParameters( 91 JmpCnt = 1, 92 AluCnt = 4, 93 MulCnt = 0, 94 MduCnt = 2, 95 FmacCnt = 4, 96 FmiscCnt = 2, 97 FmiscDivSqrtCnt = 0, 98 LduCnt = 2, 99 StuCnt = 2 100 ), 101 LoadPipelineWidth: Int = 2, 102 StorePipelineWidth: Int = 2, 103 StoreBufferSize: Int = 16, 104 RefillSize: Int = 512, 105 TlbEntrySize: Int = 32, 106 TlbSPEntrySize: Int = 4, 107 PtwL3EntrySize: Int = 4096, //(256 * 16) or 512 108 PtwSPEntrySize: Int = 16, 109 PtwL1EntrySize: Int = 16, 110 PtwL2EntrySize: Int = 2048, //(256 * 8) 111 NumPerfCounters: Int = 16, 112 NrExtIntr: Int = 150 113) 114 115trait HasXSParameter { 116 117 val core = Parameters.get.coreParameters 118 val env = Parameters.get.envParameters 119 120 val XLEN = 64 121 val minFLen = 32 122 val fLen = 64 123 124 def xLen = 64 125 126 val HasMExtension = core.HasMExtension 127 val HasCExtension = core.HasCExtension 128 val HasDiv = core.HasDiv 129 val HasIcache = core.HasICache 130 val HasDcache = core.HasDCache 131 val EnableStoreQueue = core.EnableStoreQueue 132 val AddrBits = core.AddrBits // AddrBits is used in some cases 133 val VAddrBits = core.VAddrBits // VAddrBits is Virtual Memory addr bits 134 val PAddrBits = core.PAddrBits // PAddrBits is Phyical Memory addr bits 135 val AddrBytes = AddrBits / 8 // unused 136 val DataBits = XLEN 137 val DataBytes = DataBits / 8 138 val HasFPU = core.HasFPU 139 val FetchWidth = core.FetchWidth 140 val PredictWidth = FetchWidth * (if (HasCExtension) 2 else 1) 141 val EnableBPU = core.EnableBPU 142 val EnableBPD = core.EnableBPD // enable backing predictor(like Tage) in BPUStage3 143 val EnableRAS = core.EnableRAS 144 val EnableLB = core.EnableLB 145 val EnableLoop = core.EnableLoop 146 val EnableSC = core.EnableSC 147 val EnbaleTlbDebug = core.EnbaleTlbDebug 148 val HistoryLength = core.HistoryLength 149 val BtbSize = core.BtbSize 150 // val BtbWays = 4 151 val BtbBanks = PredictWidth 152 // val BtbSets = BtbSize / BtbWays 153 val JbtacSize = core.JbtacSize 154 val JbtacBanks = core.JbtacBanks 155 val RasSize = core.RasSize 156 val CacheLineSize = core.CacheLineSize 157 val CacheLineHalfWord = CacheLineSize / 16 158 val ExtHistoryLength = HistoryLength + 64 159 val UBtbWays = core.UBtbWays 160 val BtbWays = core.BtbWays 161 val EnableL1plusPrefetcher = core.EnableL1plusPrefetcher 162 val IBufSize = core.IBufSize 163 val DecodeWidth = core.DecodeWidth 164 val RenameWidth = core.RenameWidth 165 val CommitWidth = core.CommitWidth 166 val BrqSize = core.BrqSize 167 val FtqSize = core.FtqSize 168 val IssQueSize = core.IssQueSize 169 val BrTagWidth = log2Up(BrqSize) 170 val NRPhyRegs = core.NRPhyRegs 171 val PhyRegIdxWidth = log2Up(NRPhyRegs) 172 val RoqSize = core.RoqSize 173 val LoadQueueSize = core.LoadQueueSize 174 val StoreQueueSize = core.StoreQueueSize 175 val dpParams = core.dpParams 176 val exuParameters = core.exuParameters 177 val NRIntReadPorts = core.NRIntReadPorts 178 val NRIntWritePorts = core.NRIntWritePorts 179 val NRMemReadPorts = exuParameters.LduCnt + 2 * exuParameters.StuCnt 180 val NRFpReadPorts = core.NRFpReadPorts 181 val NRFpWritePorts = core.NRFpWritePorts 182 val LoadPipelineWidth = core.LoadPipelineWidth 183 val StorePipelineWidth = core.StorePipelineWidth 184 val StoreBufferSize = core.StoreBufferSize 185 val RefillSize = core.RefillSize 186 val DTLBWidth = core.LoadPipelineWidth + core.StorePipelineWidth 187 val TlbEntrySize = core.TlbEntrySize 188 val TlbSPEntrySize = core.TlbSPEntrySize 189 val PtwL3EntrySize = core.PtwL3EntrySize 190 val PtwSPEntrySize = core.PtwSPEntrySize 191 val PtwL1EntrySize = core.PtwL1EntrySize 192 val PtwL2EntrySize = core.PtwL2EntrySize 193 val NumPerfCounters = core.NumPerfCounters 194 val NrExtIntr = core.NrExtIntr 195 196 val instBytes = if (HasCExtension) 2 else 4 197 val instOffsetBits = log2Ceil(instBytes) 198 199 val icacheParameters = ICacheParameters( 200 tagECC = Some("parity"), 201 dataECC = Some("parity"), 202 replacer = Some("setlru"), 203 nMissEntries = 2 204 ) 205 206 val l1plusCacheParameters = L1plusCacheParameters( 207 tagECC = Some("secded"), 208 dataECC = Some("secded"), 209 nMissEntries = 8 210 ) 211 212 val dcacheParameters = DCacheParameters( 213 tagECC = Some("secded"), 214 dataECC = Some("secded"), 215 nMissEntries = 16, 216 nProbeEntries = 16, 217 nReleaseEntries = 16, 218 nStoreReplayEntries = 16 219 ) 220 221 val LRSCCycles = 100 222 223 224 // cache hierarchy configurations 225 val l1BusDataWidth = 256 226 227 // L2 configurations 228 val L1BusWidth = 256 229 val L2Size = 512 * 1024 // 512KB 230 val L2BlockSize = 64 231 val L2NWays = 8 232 val L2NSets = L2Size / L2BlockSize / L2NWays 233 234 // L3 configurations 235 val L2BusWidth = 256 236 val L3Size = 4 * 1024 * 1024 // 4MB 237 val L3BlockSize = 64 238 val L3NBanks = 4 239 val L3NWays = 8 240 val L3NSets = L3Size / L3BlockSize / L3NBanks / L3NWays 241 242 // on chip network configurations 243 val L3BusWidth = 256 244 245 // icache prefetcher 246 val l1plusPrefetcherParameters = L1plusPrefetcherParameters( 247 enable = true, 248 _type = "stream", 249 streamParams = StreamPrefetchParameters( 250 streamCnt = 2, 251 streamSize = 4, 252 ageWidth = 4, 253 blockBytes = l1plusCacheParameters.blockBytes, 254 reallocStreamOnMissInstantly = true, 255 cacheName = "icache" 256 ) 257 ) 258 259 // dcache prefetcher 260 val l2PrefetcherParameters = L2PrefetcherParameters( 261 enable = true, 262 _type = "bop", // "stream" or "bop" 263 streamParams = StreamPrefetchParameters( 264 streamCnt = 4, 265 streamSize = 4, 266 ageWidth = 4, 267 blockBytes = L2BlockSize, 268 reallocStreamOnMissInstantly = true, 269 cacheName = "dcache" 270 ), 271 bopParams = BOPParameters( 272 rrTableEntries = 256, 273 rrTagBits = 12, 274 scoreBits = 5, 275 roundMax = 50, 276 badScore = 1, 277 blockBytes = L2BlockSize, 278 nEntries = dcacheParameters.nMissEntries * 2 // TODO: this is too large 279 ), 280 ) 281} 282 283trait HasXSLog { 284 this: RawModule => 285 implicit val moduleName: String = this.name 286} 287 288abstract class XSModule extends MultiIOModule 289 with HasXSParameter 290 with HasExceptionNO 291 with HasXSLog 292 with HasFPUParameters { 293 def io: Record 294} 295 296//remove this trait after impl module logic 297trait NeedImpl { 298 this: RawModule => 299 override protected def IO[T <: Data](iodef: T): T = { 300 println(s"[Warn]: (${this.name}) please reomve 'NeedImpl' after implement this module") 301 val io = chisel3.experimental.IO(iodef) 302 io <> DontCare 303 io 304 } 305} 306 307abstract class XSBundle extends Bundle 308 with HasXSParameter 309 310case class EnviromentParameters 311( 312 FPGAPlatform: Boolean = true, 313 EnableDebug: Boolean = false, 314 EnablePerfDebug: Boolean = false, 315 DualCore: Boolean = false 316) 317 318// object AddressSpace extends HasXSParameter { 319// // (start, size) 320// // address out of MMIO will be considered as DRAM 321// def mmio = List( 322// (0x00000000L, 0x40000000L), // internal devices, such as CLINT and PLIC 323// (0x40000000L, 0x40000000L) // external devices 324// ) 325 326// def isMMIO(addr: UInt): Bool = mmio.map(range => { 327// require(isPow2(range._2)) 328// val bits = log2Up(range._2) 329// (addr ^ range._1.U)(PAddrBits-1, bits) === 0.U 330// }).reduce(_ || _) 331// } 332 333 334class XSCore()(implicit p: config.Parameters) extends LazyModule 335 with HasXSParameter 336 with HasExeBlockHelper { 337 // outer facing nodes 338 val frontend = LazyModule(new Frontend()) 339 val l1pluscache = LazyModule(new L1plusCache()) 340 val ptw = LazyModule(new PTW()) 341 val l2Prefetcher = LazyModule(new L2Prefetcher()) 342 val memBlock = LazyModule(new MemBlock( 343 fastWakeUpIn = intExuConfigs.filter(_.hasCertainLatency), 344 slowWakeUpIn = intExuConfigs.filter(_.hasUncertainlatency) ++ fpExuConfigs, 345 fastWakeUpOut = Seq(), 346 slowWakeUpOut = loadExuConfigs 347 )) 348 349 lazy val module = new XSCoreImp(this) 350} 351 352class XSCoreImp(outer: XSCore) extends LazyModuleImp(outer) 353 with HasXSParameter 354 with HasExeBlockHelper { 355 val io = IO(new Bundle { 356 val hartId = Input(UInt(64.W)) 357 val externalInterrupt = new ExternalInterruptIO 358 val l2ToPrefetcher = Flipped(new PrefetcherIO(PAddrBits)) 359 }) 360 361 val difftestIO = IO(new DifftestBundle()) 362 difftestIO <> DontCare 363 364 val trapIO = IO(new TrapIO()) 365 trapIO <> DontCare 366 367 println(s"FPGAPlatform:${env.FPGAPlatform} EnableDebug:${env.EnableDebug}") 368 AddressSpace.checkMemmap() 369 AddressSpace.printMemmap() 370 371 // to fast wake up fp, mem rs 372 val intBlockFastWakeUp = intExuConfigs.filter(_.hasCertainLatency) 373 val intBlockSlowWakeUp = intExuConfigs.filter(_.hasUncertainlatency) 374 375 val ctrlBlock = Module(new CtrlBlock) 376 val integerBlock = Module(new IntegerBlock( 377 fastWakeUpIn = Seq(), 378 slowWakeUpIn = fpExuConfigs.filter(_.writeIntRf) ++ loadExuConfigs, 379 fastWakeUpOut = intBlockFastWakeUp, 380 slowWakeUpOut = intBlockSlowWakeUp 381 )) 382 val floatBlock = Module(new FloatBlock( 383 fastWakeUpIn = Seq(), 384 slowWakeUpIn = intExuConfigs.filter(_.writeFpRf) ++ loadExuConfigs, 385 fastWakeUpOut = Seq(), 386 slowWakeUpOut = fpExuConfigs 387 )) 388 389 val frontend = outer.frontend.module 390 val memBlock = outer.memBlock.module 391 val l1pluscache = outer.l1pluscache.module 392 val ptw = outer.ptw.module 393 val l2Prefetcher = outer.l2Prefetcher.module 394 395 frontend.io.backend <> ctrlBlock.io.frontend 396 frontend.io.sfence <> integerBlock.io.fenceio.sfence 397 frontend.io.tlbCsr <> integerBlock.io.csrio.tlb 398 399 frontend.io.icacheMemAcq <> l1pluscache.io.req 400 l1pluscache.io.resp <> frontend.io.icacheMemGrant 401 l1pluscache.io.flush := frontend.io.l1plusFlush 402 frontend.io.fencei := integerBlock.io.fenceio.fencei 403 404 ctrlBlock.io.fromIntBlock <> integerBlock.io.toCtrlBlock 405 ctrlBlock.io.fromFpBlock <> floatBlock.io.toCtrlBlock 406 ctrlBlock.io.fromLsBlock <> memBlock.io.toCtrlBlock 407 ctrlBlock.io.toIntBlock <> integerBlock.io.fromCtrlBlock 408 ctrlBlock.io.toFpBlock <> floatBlock.io.fromCtrlBlock 409 ctrlBlock.io.toLsBlock <> memBlock.io.fromCtrlBlock 410 411 val memBlockWakeUpInt = memBlock.io.wakeUpOut.slow.map(x => intOutValid(x)) 412 val memBlockWakeUpFp = memBlock.io.wakeUpOut.slow.map(x => fpOutValid(x)) 413 memBlock.io.wakeUpOut.slow.foreach(_.ready := true.B) 414 415 fpExuConfigs.zip(floatBlock.io.wakeUpOut.slow).filterNot(_._1.writeIntRf).map(_._2.ready := true.B) 416 val fpBlockWakeUpInt = fpExuConfigs 417 .zip(floatBlock.io.wakeUpOut.slow) 418 .filter(_._1.writeIntRf) 419 .map(_._2).map(x => intOutValid(x, connectReady = true)) 420 421 intExuConfigs.zip(integerBlock.io.wakeUpOut.slow).filterNot(_._1.writeFpRf).map(_._2.ready := true.B) 422 val intBlockWakeUpFp = intExuConfigs.filter(_.hasUncertainlatency) 423 .zip(integerBlock.io.wakeUpOut.slow) 424 .filter(_._1.writeFpRf) 425 .map(_._2).map(x => fpOutValid(x, connectReady = true)) 426 427 integerBlock.io.wakeUpIn.slow <> fpBlockWakeUpInt ++ memBlockWakeUpInt 428 integerBlock.io.toMemBlock <> memBlock.io.fromIntBlock 429 430 floatBlock.io.wakeUpIn.slow <> intBlockWakeUpFp ++ memBlockWakeUpFp 431 floatBlock.io.toMemBlock <> memBlock.io.fromFpBlock 432 433 val wakeUpMem = Seq( 434 integerBlock.io.wakeUpOut, 435 floatBlock.io.wakeUpOut, 436 ) 437 memBlock.io.wakeUpIn.fastUops <> wakeUpMem.flatMap(_.fastUops) 438 memBlock.io.wakeUpIn.fast <> wakeUpMem.flatMap(_.fast) 439 // Note: 'WireInit' is used to block 'ready's from memBlock, 440 // we don't need 'ready's from memBlock 441 memBlock.io.wakeUpIn.slow <> wakeUpMem.flatMap(_.slow.map(x => WireInit(x))) 442 443 integerBlock.io.csrio.hartId <> io.hartId 444 integerBlock.io.csrio.fflags <> ctrlBlock.io.roqio.toCSR.fflags 445 integerBlock.io.csrio.dirty_fs <> ctrlBlock.io.roqio.toCSR.dirty_fs 446 integerBlock.io.csrio.exception <> ctrlBlock.io.roqio.exception 447 integerBlock.io.csrio.trapTarget <> ctrlBlock.io.roqio.toCSR.trapTarget 448 integerBlock.io.csrio.isXRet <> ctrlBlock.io.roqio.toCSR.isXRet 449 integerBlock.io.csrio.interrupt <> ctrlBlock.io.roqio.toCSR.intrBitSet 450 integerBlock.io.csrio.memExceptionVAddr <> memBlock.io.lsqio.exceptionAddr.vaddr 451 integerBlock.io.csrio.externalInterrupt <> io.externalInterrupt 452 integerBlock.io.csrio.perfinfo <> ctrlBlock.io.roqio.toCSR.perfinfo 453 integerBlock.io.fenceio.sfence <> memBlock.io.sfence 454 integerBlock.io.fenceio.sbuffer <> memBlock.io.fenceToSbuffer 455 memBlock.io.tlbCsr <> integerBlock.io.csrio.tlb 456 457 floatBlock.io.frm <> integerBlock.io.csrio.frm 458 459 memBlock.io.lsqio.roq <> ctrlBlock.io.roqio.lsq 460 memBlock.io.lsqio.exceptionAddr.lsIdx.lqIdx := ctrlBlock.io.roqio.exception.bits.uop.lqIdx 461 memBlock.io.lsqio.exceptionAddr.lsIdx.sqIdx := ctrlBlock.io.roqio.exception.bits.uop.sqIdx 462 memBlock.io.lsqio.exceptionAddr.isStore := CommitType.lsInstIsStore(ctrlBlock.io.roqio.exception.bits.uop.ctrl.commitType) 463 464 val itlbRepester = Module(new PTWRepeater()) 465 val dtlbRepester = Module(new PTWRepeater()) 466 itlbRepester.io.tlb <> frontend.io.ptw 467 dtlbRepester.io.tlb <> memBlock.io.ptw 468 itlbRepester.io.sfence <> integerBlock.io.fenceio.sfence 469 dtlbRepester.io.sfence <> integerBlock.io.fenceio.sfence 470 ptw.io.tlb(0) <> dtlbRepester.io.ptw 471 ptw.io.tlb(1) <> itlbRepester.io.ptw 472 ptw.io.sfence <> integerBlock.io.fenceio.sfence 473 ptw.io.csr <> integerBlock.io.csrio.tlb 474 475 val l2PrefetcherIn = Wire(Decoupled(new MissReq)) 476 if (l2PrefetcherParameters.enable && l2PrefetcherParameters._type == "bop") { 477 l2PrefetcherIn.valid := io.l2ToPrefetcher.acquire.valid 478 l2PrefetcherIn.bits := DontCare 479 l2PrefetcherIn.bits.addr := io.l2ToPrefetcher.acquire.bits.address 480 l2PrefetcherIn.bits.cmd := Mux(io.l2ToPrefetcher.acquire.bits.write, MemoryOpConstants.M_XWR, MemoryOpConstants.M_XRD) 481 } else { 482 l2PrefetcherIn <> memBlock.io.toDCachePrefetch 483 } 484 l2Prefetcher.io.in <> l2PrefetcherIn 485 486 if (!env.FPGAPlatform) { 487 val id = hartIdCore() 488 difftestIO.fromSbuffer <> memBlock.difftestIO.fromSbuffer 489 difftestIO.fromSQ <> memBlock.difftestIO.fromSQ 490 difftestIO.fromCSR <> integerBlock.difftestIO.fromCSR 491 difftestIO.fromRoq <> ctrlBlock.difftestIO.fromRoq 492 difftestIO.fromAtomic <> memBlock.difftestIO.fromAtomic 493 difftestIO.fromPtw <> ptw.difftestIO 494 trapIO <> ctrlBlock.trapIO 495 496 val debugIntReg, debugFpReg = WireInit(VecInit(Seq.fill(32)(0.U(XLEN.W)))) 497 ExcitingUtils.addSink(debugIntReg, s"DEBUG_INT_ARCH_REG$id", ExcitingUtils.Debug) 498 ExcitingUtils.addSink(debugFpReg, s"DEBUG_FP_ARCH_REG$id", ExcitingUtils.Debug) 499 val debugArchReg = WireInit(VecInit(debugIntReg ++ debugFpReg)) 500 difftestIO.fromXSCore.r := debugArchReg 501 } 502 503} 504