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.{DCacheParameters, ICacheParameters, L1plusCache, L1plusCacheParameters, PTW, PTWRepeater} 14import xiangshan.cache.prefetch._ 15import chipsalliance.rocketchip.config 16import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 17import freechips.rocketchip.tile.HasFPUParameters 18import system.L1CacheErrorInfo 19import utils._ 20 21object hartIdCore extends (() => Int) { 22 var x = 0 23 24 def apply(): Int = { 25 x = x + 1 26 x - 1 27 } 28} 29 30case class XSCoreParameters 31( 32 XLEN: Int = 64, 33 HasMExtension: Boolean = true, 34 HasCExtension: Boolean = true, 35 HasDiv: Boolean = true, 36 HasICache: Boolean = true, 37 HasDCache: Boolean = true, 38 EnableStoreQueue: Boolean = true, 39 AddrBits: Int = 64, 40 VAddrBits: Int = 39, 41 PAddrBits: Int = 40, 42 HasFPU: Boolean = true, 43 FetchWidth: Int = 8, 44 EnableBPU: Boolean = true, 45 EnableBPD: Boolean = true, 46 EnableRAS: Boolean = true, 47 EnableLB: Boolean = false, 48 EnableLoop: Boolean = true, 49 EnableSC: Boolean = true, 50 EnbaleTlbDebug: Boolean = false, 51 EnableJal: Boolean = false, 52 EnableUBTB: Boolean = true, 53 HistoryLength: Int = 64, 54 BtbSize: Int = 2048, 55 JbtacSize: Int = 1024, 56 JbtacBanks: Int = 8, 57 RasSize: Int = 16, 58 CacheLineSize: Int = 512, 59 UBtbWays: Int = 16, 60 BtbWays: Int = 2, 61 62 EnableL1plusPrefetcher: Boolean = true, 63 IBufSize: Int = 32, 64 DecodeWidth: Int = 6, 65 RenameWidth: Int = 6, 66 CommitWidth: Int = 6, 67 BrqSize: Int = 32, 68 FtqSize: Int = 48, 69 EnableLoadFastWakeUp: Boolean = true, // NOTE: not supported now, make it false 70 IssQueSize: Int = 12, 71 NRPhyRegs: Int = 160, 72 NRIntReadPorts: Int = 14, 73 NRIntWritePorts: Int = 8, 74 NRFpReadPorts: Int = 14, 75 NRFpWritePorts: Int = 8, 76 LoadQueueSize: Int = 64, 77 StoreQueueSize: Int = 48, 78 RoqSize: Int = 192, 79 dpParams: DispatchParameters = DispatchParameters( 80 IntDqSize = 16, 81 FpDqSize = 16, 82 LsDqSize = 16, 83 IntDqDeqWidth = 4, 84 FpDqDeqWidth = 4, 85 LsDqDeqWidth = 4 86 ), 87 exuParameters: ExuParameters = ExuParameters( 88 JmpCnt = 1, 89 AluCnt = 4, 90 MulCnt = 0, 91 MduCnt = 2, 92 FmacCnt = 4, 93 FmiscCnt = 2, 94 FmiscDivSqrtCnt = 0, 95 LduCnt = 2, 96 StuCnt = 2 97 ), 98 LoadPipelineWidth: Int = 2, 99 StorePipelineWidth: Int = 2, 100 StoreBufferSize: Int = 16, 101 RefillSize: Int = 512, 102 TlbEntrySize: Int = 32, 103 TlbSPEntrySize: Int = 4, 104 PtwL3EntrySize: Int = 4096, //(256 * 16) or 512 105 PtwSPEntrySize: Int = 16, 106 PtwL1EntrySize: Int = 16, 107 PtwL2EntrySize: Int = 2048, //(256 * 8) 108 NumPerfCounters: Int = 16, 109 NrExtIntr: Int = 150 110) 111 112trait HasXSParameter { 113 114 val coreParams = Parameters.get.coreParameters 115 val env = Parameters.get.envParameters 116 117 val XLEN = 64 118 val minFLen = 32 119 val fLen = 64 120 121 def xLen = 64 122 123 val HasMExtension = coreParams.HasMExtension 124 val HasCExtension = coreParams.HasCExtension 125 val HasDiv = coreParams.HasDiv 126 val HasIcache = coreParams.HasICache 127 val HasDcache = coreParams.HasDCache 128 val EnableStoreQueue = coreParams.EnableStoreQueue 129 val AddrBits = coreParams.AddrBits // AddrBits is used in some cases 130 val VAddrBits = coreParams.VAddrBits // VAddrBits is Virtual Memory addr bits 131 val PAddrBits = coreParams.PAddrBits // PAddrBits is Phyical Memory addr bits 132 val AddrBytes = AddrBits / 8 // unused 133 val DataBits = XLEN 134 val DataBytes = DataBits / 8 135 val HasFPU = coreParams.HasFPU 136 val FetchWidth = coreParams.FetchWidth 137 val PredictWidth = FetchWidth * (if (HasCExtension) 2 else 1) 138 val EnableBPU = coreParams.EnableBPU 139 val EnableBPD = coreParams.EnableBPD // enable backing predictor(like Tage) in BPUStage3 140 val EnableRAS = coreParams.EnableRAS 141 val EnableLB = coreParams.EnableLB 142 val EnableLoop = coreParams.EnableLoop 143 val EnableSC = coreParams.EnableSC 144 val EnbaleTlbDebug = coreParams.EnbaleTlbDebug 145 val HistoryLength = coreParams.HistoryLength 146 val BtbSize = coreParams.BtbSize 147 // val BtbWays = 4 148 val BtbBanks = PredictWidth 149 // val BtbSets = BtbSize / BtbWays 150 val JbtacSize = coreParams.JbtacSize 151 val JbtacBanks = coreParams.JbtacBanks 152 val RasSize = coreParams.RasSize 153 val CacheLineSize = coreParams.CacheLineSize 154 val CacheLineHalfWord = CacheLineSize / 16 155 val ExtHistoryLength = HistoryLength + 64 156 val UBtbWays = coreParams.UBtbWays 157 val BtbWays = coreParams.BtbWays 158 val EnableL1plusPrefetcher = coreParams.EnableL1plusPrefetcher 159 val IBufSize = coreParams.IBufSize 160 val DecodeWidth = coreParams.DecodeWidth 161 val RenameWidth = coreParams.RenameWidth 162 val CommitWidth = coreParams.CommitWidth 163 val BrqSize = coreParams.BrqSize 164 val FtqSize = coreParams.FtqSize 165 val IssQueSize = coreParams.IssQueSize 166 val EnableLoadFastWakeUp = coreParams.EnableLoadFastWakeUp 167 val BrTagWidth = log2Up(BrqSize) 168 val NRPhyRegs = coreParams.NRPhyRegs 169 val PhyRegIdxWidth = log2Up(NRPhyRegs) 170 val RoqSize = coreParams.RoqSize 171 val LoadQueueSize = coreParams.LoadQueueSize 172 val StoreQueueSize = coreParams.StoreQueueSize 173 val dpParams = coreParams.dpParams 174 val exuParameters = coreParams.exuParameters 175 val NRIntReadPorts = coreParams.NRIntReadPorts 176 val NRIntWritePorts = coreParams.NRIntWritePorts 177 val NRMemReadPorts = exuParameters.LduCnt + 2 * exuParameters.StuCnt 178 val NRFpReadPorts = coreParams.NRFpReadPorts 179 val NRFpWritePorts = coreParams.NRFpWritePorts 180 val LoadPipelineWidth = coreParams.LoadPipelineWidth 181 val StorePipelineWidth = coreParams.StorePipelineWidth 182 val StoreBufferSize = coreParams.StoreBufferSize 183 val RefillSize = coreParams.RefillSize 184 val DTLBWidth = coreParams.LoadPipelineWidth + coreParams.StorePipelineWidth 185 val TlbEntrySize = coreParams.TlbEntrySize 186 val TlbSPEntrySize = coreParams.TlbSPEntrySize 187 val PtwL3EntrySize = coreParams.PtwL3EntrySize 188 val PtwSPEntrySize = coreParams.PtwSPEntrySize 189 val PtwL1EntrySize = coreParams.PtwL1EntrySize 190 val PtwL2EntrySize = coreParams.PtwL2EntrySize 191 val NumPerfCounters = coreParams.NumPerfCounters 192 val NrExtIntr = coreParams.NrExtIntr 193 194 val instBytes = if (HasCExtension) 2 else 4 195 val instOffsetBits = log2Ceil(instBytes) 196 197 val icacheParameters = ICacheParameters( 198 tagECC = Some("parity"), 199 dataECC = Some("parity"), 200 replacer = Some("setplru"), 201 nMissEntries = 2 202 ) 203 204 val l1plusCacheParameters = L1plusCacheParameters( 205 tagECC = Some("secded"), 206 dataECC = Some("secded"), 207 replacer = Some("setplru"), 208 nMissEntries = 8 209 ) 210 211 val dcacheParameters = DCacheParameters( 212 tagECC = Some("secded"), 213 dataECC = Some("secded"), 214 replacer = Some("setplru"), 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 = true, 315 DualCore: Boolean = false 316) 317 318class XSCore()(implicit p: config.Parameters) extends LazyModule 319 with HasXSParameter 320 with HasExeBlockHelper { 321 // outer facing nodes 322 val frontend = LazyModule(new Frontend()) 323 val l1pluscache = LazyModule(new L1plusCache()) 324 val ptw = LazyModule(new PTW()) 325 val memBlock = LazyModule(new MemBlock( 326 fastWakeUpIn = intExuConfigs.filter(_.hasCertainLatency), 327 slowWakeUpIn = intExuConfigs.filter(_.hasUncertainlatency) ++ fpExuConfigs, 328 fastWakeUpOut = Seq(), 329 slowWakeUpOut = loadExuConfigs, 330 numIntWakeUpFp = intExuConfigs.count(_.writeFpRf) 331 )) 332 333 lazy val module = new XSCoreImp(this) 334} 335 336class XSCoreImp(outer: XSCore) extends LazyModuleImp(outer) 337 with HasXSParameter 338 with HasExeBlockHelper { 339 val io = IO(new Bundle { 340 val hartId = Input(UInt(64.W)) 341 val externalInterrupt = new ExternalInterruptIO 342 val l2_pf_enable = Output(Bool()) 343 val l1plus_error, icache_error, dcache_error = Output(new L1CacheErrorInfo) 344 }) 345 346 val difftestIO = IO(new DifftestBundle()) 347 difftestIO <> DontCare 348 349 val trapIO = IO(new TrapIO()) 350 trapIO <> DontCare 351 352 println(s"FPGAPlatform:${env.FPGAPlatform} EnableDebug:${env.EnableDebug}") 353 AddressSpace.checkMemmap() 354 AddressSpace.printMemmap() 355 356 // to fast wake up fp, mem rs 357 val intBlockFastWakeUp = intExuConfigs.filter(_.hasCertainLatency) 358 val intBlockSlowWakeUp = intExuConfigs.filter(_.hasUncertainlatency) 359 360 val ctrlBlock = Module(new CtrlBlock) 361 val integerBlock = Module(new IntegerBlock( 362 fastWakeUpIn = Seq(), 363 slowWakeUpIn = fpExuConfigs.filter(_.writeIntRf) ++ loadExuConfigs, 364 memFastWakeUpIn = loadExuConfigs, 365 fastWakeUpOut = intBlockFastWakeUp, 366 slowWakeUpOut = intBlockSlowWakeUp 367 )) 368 val floatBlock = Module(new FloatBlock( 369 intSlowWakeUpIn = intExuConfigs.filter(_.writeFpRf), 370 memSlowWakeUpIn = loadExuConfigs, 371 fastWakeUpOut = Seq(), 372 slowWakeUpOut = fpExuConfigs 373 )) 374 375 val frontend = outer.frontend.module 376 val memBlock = outer.memBlock.module 377 val l1pluscache = outer.l1pluscache.module 378 val ptw = outer.ptw.module 379 380 io.l1plus_error <> l1pluscache.io.error 381 io.icache_error <> frontend.io.error 382 io.dcache_error <> memBlock.io.error 383 384 frontend.io.backend <> ctrlBlock.io.frontend 385 frontend.io.sfence <> integerBlock.io.fenceio.sfence 386 frontend.io.tlbCsr <> integerBlock.io.csrio.tlb 387 frontend.io.csrCtrl <> integerBlock.io.csrio.customCtrl 388 389 frontend.io.icacheMemAcq <> l1pluscache.io.req 390 l1pluscache.io.resp <> frontend.io.icacheMemGrant 391 l1pluscache.io.flush := frontend.io.l1plusFlush 392 frontend.io.fencei := integerBlock.io.fenceio.fencei 393 394 ctrlBlock.io.fromIntBlock <> integerBlock.io.toCtrlBlock 395 ctrlBlock.io.fromFpBlock <> floatBlock.io.toCtrlBlock 396 ctrlBlock.io.fromLsBlock <> memBlock.io.toCtrlBlock 397 ctrlBlock.io.toIntBlock <> integerBlock.io.fromCtrlBlock 398 ctrlBlock.io.toFpBlock <> floatBlock.io.fromCtrlBlock 399 ctrlBlock.io.toLsBlock <> memBlock.io.fromCtrlBlock 400 ctrlBlock.io.csrCtrl <> integerBlock.io.csrio.customCtrl 401 402 val memBlockWakeUpInt = memBlock.io.wakeUpOutInt.slow.map(WireInit(_)) 403 val memBlockWakeUpFp = memBlock.io.wakeUpOutFp.slow.map(WireInit(_)) 404 memBlock.io.wakeUpOutInt.slow.foreach(_.ready := true.B) 405 memBlock.io.wakeUpOutFp.slow.foreach(_.ready := true.B) 406 407 fpExuConfigs.zip(floatBlock.io.wakeUpOut.slow).filterNot(_._1.writeIntRf).map(_._2.ready := true.B) 408 val fpBlockWakeUpInt = fpExuConfigs 409 .zip(floatBlock.io.wakeUpOut.slow) 410 .filter(_._1.writeIntRf) 411 .map(_._2) 412 413 intExuConfigs.zip(integerBlock.io.wakeUpOut.slow).filterNot(_._1.writeFpRf).map(_._2.ready := true.B) 414 val intBlockWakeUpFp = intExuConfigs.filter(_.hasUncertainlatency) 415 .zip(integerBlock.io.wakeUpOut.slow) 416 .filter(_._1.writeFpRf) 417 .map(_._2) 418 419 integerBlock.io.wakeUpIn.slow <> fpBlockWakeUpInt ++ memBlockWakeUpInt 420 integerBlock.io.toMemBlock <> memBlock.io.fromIntBlock 421 integerBlock.io.memFastWakeUp <> memBlock.io.ldFastWakeUpInt 422 423 floatBlock.io.intWakeUpFp <> intBlockWakeUpFp 424 floatBlock.io.memWakeUpFp <> memBlockWakeUpFp 425 floatBlock.io.toMemBlock <> memBlock.io.fromFpBlock 426 427 val wakeUpMem = Seq( 428 integerBlock.io.wakeUpOut, 429 floatBlock.io.wakeUpOut, 430 ) 431 memBlock.io.wakeUpIn.fastUops <> wakeUpMem.flatMap(_.fastUops) 432 memBlock.io.wakeUpIn.fast <> wakeUpMem.flatMap(_.fast) 433 // Note: 'WireInit' is used to block 'ready's from memBlock, 434 // we don't need 'ready's from memBlock 435 memBlock.io.wakeUpIn.slow <> wakeUpMem.flatMap(_.slow.map(x => WireInit(x))) 436 memBlock.io.intWakeUpFp <> floatBlock.io.intWakeUpOut 437 438 integerBlock.io.csrio.hartId <> io.hartId 439 integerBlock.io.csrio.perf <> DontCare 440 integerBlock.io.csrio.perf.retiredInstr <> ctrlBlock.io.roqio.toCSR.perfinfo.retiredInstr 441 integerBlock.io.csrio.fpu.fflags <> ctrlBlock.io.roqio.toCSR.fflags 442 integerBlock.io.csrio.fpu.isIllegal := false.B 443 integerBlock.io.csrio.fpu.dirty_fs <> ctrlBlock.io.roqio.toCSR.dirty_fs 444 integerBlock.io.csrio.fpu.frm <> floatBlock.io.frm 445 integerBlock.io.csrio.exception <> ctrlBlock.io.roqio.exception 446 integerBlock.io.csrio.isXRet <> ctrlBlock.io.roqio.toCSR.isXRet 447 integerBlock.io.csrio.trapTarget <> ctrlBlock.io.roqio.toCSR.trapTarget 448 integerBlock.io.csrio.interrupt <> ctrlBlock.io.roqio.toCSR.intrBitSet 449 integerBlock.io.csrio.memExceptionVAddr <> memBlock.io.lsqio.exceptionAddr.vaddr 450 integerBlock.io.csrio.externalInterrupt <> io.externalInterrupt 451 452 integerBlock.io.fenceio.sfence <> memBlock.io.sfence 453 integerBlock.io.fenceio.sbuffer <> memBlock.io.fenceToSbuffer 454 455 memBlock.io.csrCtrl <> integerBlock.io.csrio.customCtrl 456 memBlock.io.tlbCsr <> integerBlock.io.csrio.tlb 457 memBlock.io.lsqio.roq <> ctrlBlock.io.roqio.lsq 458 memBlock.io.lsqio.exceptionAddr.lsIdx.lqIdx := ctrlBlock.io.roqio.exception.bits.uop.lqIdx 459 memBlock.io.lsqio.exceptionAddr.lsIdx.sqIdx := ctrlBlock.io.roqio.exception.bits.uop.sqIdx 460 memBlock.io.lsqio.exceptionAddr.isStore := CommitType.lsInstIsStore(ctrlBlock.io.roqio.exception.bits.uop.ctrl.commitType) 461 462 val itlbRepeater = Module(new PTWRepeater()) 463 val dtlbRepeater = Module(new PTWRepeater()) 464 itlbRepeater.io.tlb <> frontend.io.ptw 465 dtlbRepeater.io.tlb <> memBlock.io.ptw 466 itlbRepeater.io.sfence <> integerBlock.io.fenceio.sfence 467 dtlbRepeater.io.sfence <> integerBlock.io.fenceio.sfence 468 ptw.io.tlb(0) <> dtlbRepeater.io.ptw 469 ptw.io.tlb(1) <> itlbRepeater.io.ptw 470 ptw.io.sfence <> integerBlock.io.fenceio.sfence 471 ptw.io.csr <> integerBlock.io.csrio.tlb 472 473 // if l2 prefetcher use stream prefetch, it should be placed in XSCore 474 assert(l2PrefetcherParameters._type == "bop") 475 io.l2_pf_enable := integerBlock.io.csrio.customCtrl.l2_pf_enable 476 477 if (!env.FPGAPlatform) { 478 val id = hartIdCore() 479 difftestIO.fromSbuffer <> memBlock.difftestIO.fromSbuffer 480 difftestIO.fromSQ <> memBlock.difftestIO.fromSQ 481 difftestIO.fromCSR <> integerBlock.difftestIO.fromCSR 482 difftestIO.fromRoq <> ctrlBlock.difftestIO.fromRoq 483 difftestIO.fromAtomic <> memBlock.difftestIO.fromAtomic 484 difftestIO.fromPtw <> ptw.difftestIO 485 trapIO <> ctrlBlock.trapIO 486 487 val debugIntReg, debugFpReg = WireInit(VecInit(Seq.fill(32)(0.U(XLEN.W)))) 488 ExcitingUtils.addSink(debugIntReg, s"DEBUG_INT_ARCH_REG$id", ExcitingUtils.Debug) 489 ExcitingUtils.addSink(debugFpReg, s"DEBUG_FP_ARCH_REG$id", ExcitingUtils.Debug) 490 val debugArchReg = WireInit(VecInit(debugIntReg ++ debugFpReg)) 491 difftestIO.fromXSCore.r := debugArchReg 492 } 493 494} 495