1/*************************************************************************************** 2* Copyright (c) 2021-2025 Beijing Institute of Open Source Chip (BOSC) 3* Copyright (c) 2020-2025 Institute of Computing Technology, Chinese Academy of Sciences 4* Copyright (c) 2020-2021 Peng Cheng Laboratory 5* 6* XiangShan is licensed under Mulan PSL v2. 7* You can use this software according to the terms and conditions of the Mulan PSL v2. 8* You may obtain a copy of Mulan PSL v2 at: 9* http://license.coscl.org.cn/MulanPSL2 10* 11* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 12* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 13* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 14* 15* See the Mulan PSL v2 for more details. 16***************************************************************************************/ 17 18package top 19 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import utils._ 24import utility._ 25import system._ 26import org.chipsalliance.cde.config._ 27import freechips.rocketchip.tile.{BusErrorUnit, BusErrorUnitParams, MaxHartIdBits, XLen} 28import xiangshan.frontend.icache.ICacheParameters 29import freechips.rocketchip.devices.debug._ 30import openLLC.OpenLLCParam 31import freechips.rocketchip.diplomacy._ 32import xiangshan.backend.dispatch.DispatchParameters 33import xiangshan.backend.regfile.{IntPregParams, VfPregParams} 34import xiangshan.cache.DCacheParameters 35import xiangshan.cache.mmu.{L2TLBParameters, TLBParameters} 36import device.EnableJtag 37import huancun._ 38import coupledL2._ 39import coupledL2.prefetch._ 40 41class BaseConfig(n: Int) extends Config((site, here, up) => { 42 case XLen => 64 43 case DebugOptionsKey => DebugOptions() 44 case SoCParamsKey => SoCParameters() 45 case PMParameKey => PMParameters() 46 case XSTileKey => Seq.tabulate(n){ i => XSCoreParameters(HartId = i) } 47 case ExportDebug => DebugAttachParams(protocols = Set(JTAG)) 48 case DebugModuleKey => Some(DebugModuleParams( 49 nAbstractDataWords = (if (site(XLen) == 32) 1 else if (site(XLen) == 64) 2 else 4), 50 maxSupportedSBAccess = site(XLen), 51 hasBusMaster = true, 52 baseAddress = BigInt(0x38020000), 53 nScratch = 2, 54 crossingHasSafeReset = false, 55 hasHartResets = true 56 )) 57 case JtagDTMKey => JtagDTMKey 58 case MaxHartIdBits => log2Up(n) max 6 59 case EnableJtag => true.B 60}) 61 62// Synthesizable minimal XiangShan 63// * It is still an out-of-order, super-scalaer arch 64// * L1 cache included 65// * L2 cache NOT included 66// * L3 cache included 67class MinimalConfig(n: Int = 1) extends Config( 68 new BaseConfig(n).alter((site, here, up) => { 69 case XSTileKey => up(XSTileKey).map( 70 p => p.copy( 71 DecodeWidth = 6, 72 RenameWidth = 6, 73 RobCommitWidth = 8, 74 FetchWidth = 4, 75 VirtualLoadQueueSize = 24, 76 LoadQueueRARSize = 24, 77 LoadQueueRAWSize = 12, 78 LoadQueueReplaySize = 24, 79 LoadUncacheBufferSize = 8, 80 LoadQueueNWriteBanks = 4, // NOTE: make sure that LoadQueue{RAR, RAW, Replay}Size is divided by LoadQueueNWriteBanks. 81 RollbackGroupSize = 8, 82 StoreQueueSize = 20, 83 StoreQueueNWriteBanks = 4, // NOTE: make sure that StoreQueueSize is divided by StoreQueueNWriteBanks 84 StoreQueueForwardWithMask = true, 85 // ============ VLSU ============ 86 VlMergeBufferSize = 16, 87 VsMergeBufferSize = 8, 88 UopWritebackWidth = 2, 89 // ============================== 90 RobSize = 48, 91 RabSize = 96, 92 FtqSize = 8, 93 IBufSize = 24, 94 IBufNBank = 6, 95 StoreBufferSize = 4, 96 StoreBufferThreshold = 3, 97 IssueQueueSize = 10, 98 IssueQueueCompEntrySize = 4, 99 dpParams = DispatchParameters( 100 IntDqSize = 12, 101 FpDqSize = 12, 102 LsDqSize = 12, 103 IntDqDeqWidth = 8, 104 FpDqDeqWidth = 6, 105 VecDqDeqWidth = 6, 106 LsDqDeqWidth = 6 107 ), 108 intPreg = IntPregParams( 109 numEntries = 64, 110 numRead = None, 111 numWrite = None, 112 ), 113 vfPreg = VfPregParams( 114 numEntries = 160, 115 numRead = None, 116 numWrite = None, 117 ), 118 icacheParameters = ICacheParameters( 119 nSets = 64, // 16KB ICache 120 tagECC = Some("parity"), 121 dataECC = Some("parity"), 122 replacer = Some("setplru"), 123 cacheCtrlAddressOpt = Some(AddressSet(0x38022080, 0x7f)), 124 ), 125 dcacheParametersOpt = Some(DCacheParameters( 126 nSets = 64, // 32KB DCache 127 nWays = 8, 128 tagECC = Some("secded"), 129 dataECC = Some("secded"), 130 replacer = Some("setplru"), 131 nMissEntries = 4, 132 nProbeEntries = 4, 133 nReleaseEntries = 8, 134 nMaxPrefetchEntry = 2, 135 enableTagEcc = true, 136 enableDataEcc = true, 137 cacheCtrlAddressOpt = Some(AddressSet(0x38022000, 0x7f)) 138 )), 139 // ============ BPU =============== 140 EnableLoop = false, 141 EnableGHistDiff = false, 142 FtbSize = 256, 143 FtbWays = 2, 144 RasSize = 8, 145 RasSpecSize = 16, 146 TageTableInfos = 147 Seq((512, 4, 6), 148 (512, 9, 6), 149 (1024, 19, 6)), 150 SCNRows = 128, 151 SCNTables = 2, 152 SCHistLens = Seq(0, 5), 153 ITTageTableInfos = 154 Seq((256, 4, 7), 155 (256, 8, 7), 156 (512, 16, 7)), 157 // ================================ 158 itlbParameters = TLBParameters( 159 name = "itlb", 160 fetchi = true, 161 useDmode = false, 162 NWays = 4, 163 ), 164 ldtlbParameters = TLBParameters( 165 name = "ldtlb", 166 NWays = 4, 167 partialStaticPMP = true, 168 outsideRecvFlush = true, 169 outReplace = false, 170 lgMaxSize = 4 171 ), 172 sttlbParameters = TLBParameters( 173 name = "sttlb", 174 NWays = 4, 175 partialStaticPMP = true, 176 outsideRecvFlush = true, 177 outReplace = false, 178 lgMaxSize = 4 179 ), 180 hytlbParameters = TLBParameters( 181 name = "hytlb", 182 NWays = 4, 183 partialStaticPMP = true, 184 outsideRecvFlush = true, 185 outReplace = false, 186 lgMaxSize = 4 187 ), 188 pftlbParameters = TLBParameters( 189 name = "pftlb", 190 NWays = 4, 191 partialStaticPMP = true, 192 outsideRecvFlush = true, 193 outReplace = false, 194 lgMaxSize = 4 195 ), 196 btlbParameters = TLBParameters( 197 name = "btlb", 198 NWays = 4, 199 ), 200 l2tlbParameters = L2TLBParameters( 201 l3Size = 4, 202 l2Size = 4, 203 l1nSets = 4, 204 l1nWays = 4, 205 l1ReservedBits = 1, 206 l0nSets = 4, 207 l0nWays = 8, 208 l0ReservedBits = 0, 209 spSize = 4, 210 ), 211 L2CacheParamsOpt = Some(L2Param( 212 name = "L2", 213 ways = 8, 214 sets = 128, 215 echoField = Seq(huancun.DirtyField()), 216 prefetch = Nil, 217 clientCaches = Seq(L1Param( 218 "dcache", 219 isKeywordBitsOpt = p.dcacheParametersOpt.get.isKeywordBitsOpt 220 )), 221 )), 222 L2NBanks = 2, 223 prefetcher = None // if L2 pf_recv_node does not exist, disable SMS prefetcher 224 ) 225 ) 226 case SoCParamsKey => 227 val tiles = site(XSTileKey) 228 up(SoCParamsKey).copy( 229 L3CacheParamsOpt = Option.when(!up(EnableCHI))(up(SoCParamsKey).L3CacheParamsOpt.get.copy( 230 sets = 1024, 231 inclusive = false, 232 clientCaches = tiles.map{ core => 233 val clientDirBytes = tiles.map{ t => 234 t.L2NBanks * t.L2CacheParamsOpt.map(_.toCacheParams.capacity).getOrElse(0) 235 }.sum 236 val l2params = core.L2CacheParamsOpt.get.toCacheParams 237 l2params.copy(sets = 2 * clientDirBytes / core.L2NBanks / l2params.ways / 64) 238 }, 239 simulation = !site(DebugOptionsKey).FPGAPlatform, 240 prefetch = None 241 )), 242 OpenLLCParamsOpt = Option.when(up(EnableCHI))(OpenLLCParam( 243 name = "LLC", 244 ways = 8, 245 sets = 2048, 246 banks = 4, 247 clientCaches = Seq(L2Param()) 248 )), 249 L3NBanks = 1 250 ) 251 }) 252) 253 254// Non-synthesizable MinimalConfig, for fast simulation only 255class MinimalSimConfig(n: Int = 1) extends Config( 256 new MinimalConfig(n).alter((site, here, up) => { 257 case XSTileKey => up(XSTileKey).map(_.copy( 258 dcacheParametersOpt = None, 259 softPTW = true 260 )) 261 case SoCParamsKey => up(SoCParamsKey).copy( 262 L3CacheParamsOpt = None, 263 OpenLLCParamsOpt = None 264 ) 265 }) 266) 267 268case class WithNKBL1D(n: Int, ways: Int = 8) extends Config((site, here, up) => { 269 case XSTileKey => 270 val sets = n * 1024 / ways / 64 271 up(XSTileKey).map(_.copy( 272 dcacheParametersOpt = Some(DCacheParameters( 273 nSets = sets, 274 nWays = ways, 275 tagECC = Some("secded"), 276 dataECC = Some("secded"), 277 replacer = Some("setplru"), 278 nMissEntries = 16, 279 nProbeEntries = 8, 280 nReleaseEntries = 18, 281 nMaxPrefetchEntry = 6, 282 enableTagEcc = true, 283 enableDataEcc = true, 284 cacheCtrlAddressOpt = Some(AddressSet(0x38022000, 0x7f)) 285 )) 286 )) 287}) 288 289case class L2CacheConfig 290( 291 size: String, 292 ways: Int = 8, 293 inclusive: Boolean = true, 294 banks: Int = 1, 295 tp: Boolean = true 296) extends Config((site, here, up) => { 297 case XSTileKey => 298 require(inclusive, "L2 must be inclusive") 299 val nKB = size.toUpperCase() match { 300 case s"${k}KB" => k.trim().toInt 301 case s"${m}MB" => (m.trim().toDouble * 1024).toInt 302 } 303 val upParams = up(XSTileKey) 304 val l2sets = nKB * 1024 / banks / ways / 64 305 upParams.map(p => p.copy( 306 L2CacheParamsOpt = Some(L2Param( 307 name = "L2", 308 ways = ways, 309 sets = l2sets, 310 clientCaches = Seq(L1Param( 311 "dcache", 312 sets = 2 * p.dcacheParametersOpt.get.nSets / banks, 313 ways = p.dcacheParametersOpt.get.nWays + 2, 314 aliasBitsOpt = p.dcacheParametersOpt.get.aliasBitsOpt, 315 vaddrBitsOpt = Some(p.GPAddrBitsSv48x4 - log2Up(p.dcacheParametersOpt.get.blockBytes)), 316 isKeywordBitsOpt = p.dcacheParametersOpt.get.isKeywordBitsOpt 317 )), 318 reqField = Seq(utility.ReqSourceField()), 319 echoField = Seq(huancun.DirtyField()), 320 tagECC = Some("secded"), 321 dataECC = Some("secded"), 322 enableTagECC = true, 323 enableDataECC = true, 324 dataCheck = Some("oddparity"), 325 prefetch = Seq(BOPParameters()) ++ 326 (if (tp) Seq(TPParameters()) else Nil) ++ 327 (if (p.prefetcher.nonEmpty) Seq(PrefetchReceiverParams()) else Nil), 328 enablePerf = !site(DebugOptionsKey).FPGAPlatform && site(DebugOptionsKey).EnablePerfDebug, 329 enableRollingDB = site(DebugOptionsKey).EnableRollingDB, 330 enableMonitor = site(DebugOptionsKey).AlwaysBasicDB, 331 elaboratedTopDown = !site(DebugOptionsKey).FPGAPlatform 332 )), 333 L2NBanks = banks 334 )) 335}) 336 337case class L3CacheConfig(size: String, ways: Int = 8, inclusive: Boolean = true, banks: Int = 1) extends Config((site, here, up) => { 338 case SoCParamsKey => 339 val nKB = size.toUpperCase() match { 340 case s"${k}KB" => k.trim().toInt 341 case s"${m}MB" => (m.trim().toDouble * 1024).toInt 342 } 343 val sets = nKB * 1024 / banks / ways / 64 344 val tiles = site(XSTileKey) 345 val clientDirBytes = tiles.map{ t => 346 t.L2NBanks * t.L2CacheParamsOpt.map(_.toCacheParams.capacity).getOrElse(0) 347 }.sum 348 up(SoCParamsKey).copy( 349 L3NBanks = banks, 350 L3CacheParamsOpt = Option.when(!up(EnableCHI))(HCCacheParameters( 351 name = "L3", 352 level = 3, 353 ways = ways, 354 sets = sets, 355 inclusive = inclusive, 356 clientCaches = tiles.map{ core => 357 val l2params = core.L2CacheParamsOpt.get.toCacheParams 358 l2params.copy(sets = 2 * clientDirBytes / core.L2NBanks / l2params.ways / 64, ways = l2params.ways + 2) 359 }, 360 enablePerf = !site(DebugOptionsKey).FPGAPlatform && site(DebugOptionsKey).EnablePerfDebug, 361 ctrl = Some(CacheCtrl( 362 address = 0x39000000, 363 numCores = tiles.size 364 )), 365 reqField = Seq(utility.ReqSourceField()), 366 sramClkDivBy2 = true, 367 sramDepthDiv = 4, 368 tagECC = Some("secded"), 369 dataECC = Some("secded"), 370 simulation = !site(DebugOptionsKey).FPGAPlatform, 371 prefetch = Some(huancun.prefetch.L3PrefetchReceiverParams()), 372 tpmeta = Some(huancun.prefetch.DefaultTPmetaParameters()) 373 )), 374 OpenLLCParamsOpt = Option.when(up(EnableCHI))(OpenLLCParam( 375 name = "LLC", 376 ways = ways, 377 sets = sets, 378 banks = banks, 379 fullAddressBits = 48, 380 clientCaches = tiles.map { core => 381 val l2params = core.L2CacheParamsOpt.get 382 l2params.copy(sets = 2 * clientDirBytes / core.L2NBanks / l2params.ways / 64, ways = l2params.ways + 2) 383 }, 384 enablePerf = !site(DebugOptionsKey).FPGAPlatform && site(DebugOptionsKey).EnablePerfDebug, 385 elaboratedTopDown = !site(DebugOptionsKey).FPGAPlatform 386 )) 387 ) 388}) 389 390class WithL3DebugConfig extends Config( 391 L3CacheConfig("256KB", inclusive = false) ++ L2CacheConfig("64KB") 392) 393 394class MinimalL3DebugConfig(n: Int = 1) extends Config( 395 new WithL3DebugConfig ++ new MinimalConfig(n) 396) 397 398class DefaultL3DebugConfig(n: Int = 1) extends Config( 399 new WithL3DebugConfig ++ new BaseConfig(n) 400) 401 402class WithFuzzer extends Config((site, here, up) => { 403 case DebugOptionsKey => up(DebugOptionsKey).copy( 404 EnablePerfDebug = false, 405 ) 406 case SoCParamsKey => up(SoCParamsKey).copy( 407 L3CacheParamsOpt = up(SoCParamsKey).L3CacheParamsOpt.map(_.copy( 408 enablePerf = false, 409 )), 410 OpenLLCParamsOpt = up(SoCParamsKey).OpenLLCParamsOpt.map(_.copy( 411 enablePerf = false, 412 )), 413 ) 414 case XSTileKey => up(XSTileKey).zipWithIndex.map{ case (p, i) => 415 p.copy( 416 L2CacheParamsOpt = Some(up(XSTileKey)(i).L2CacheParamsOpt.get.copy( 417 enablePerf = false, 418 )), 419 ) 420 } 421}) 422 423class MinimalAliasDebugConfig(n: Int = 1) extends Config( 424 L3CacheConfig("512KB", inclusive = false) 425 ++ L2CacheConfig("256KB", inclusive = true) 426 ++ WithNKBL1D(128) 427 ++ new MinimalConfig(n) 428) 429 430class MediumConfig(n: Int = 1) extends Config( 431 L3CacheConfig("4MB", inclusive = false, banks = 4) 432 ++ L2CacheConfig("512KB", inclusive = true) 433 ++ WithNKBL1D(128) 434 ++ new BaseConfig(n) 435) 436 437class FuzzConfig(dummy: Int = 0) extends Config( 438 new WithFuzzer 439 ++ new DefaultConfig(1) 440) 441 442class DefaultConfig(n: Int = 1) extends Config( 443 L3CacheConfig("16MB", inclusive = false, banks = 4, ways = 16) 444 ++ L2CacheConfig("1MB", inclusive = true, banks = 4) 445 ++ WithNKBL1D(64, ways = 4) 446 ++ new BaseConfig(n) 447) 448 449class WithCHI extends Config((_, _, _) => { 450 case EnableCHI => true 451}) 452 453class KunminghuV2Config(n: Int = 1) extends Config( 454 L2CacheConfig("1MB", inclusive = true, banks = 4, tp = false) 455 ++ new DefaultConfig(n) 456 ++ new WithCHI 457) 458 459class KunminghuV2MinimalConfig(n: Int = 1) extends Config( 460 L2CacheConfig("128KB", inclusive = true, banks = 1, tp = false) 461 ++ WithNKBL1D(32, ways = 4) 462 ++ new MinimalConfig(n) 463 ++ new WithCHI 464) 465 466class XSNoCTopConfig(n: Int = 1) extends Config( 467 (new KunminghuV2Config(n)).alter((site, here, up) => { 468 case SoCParamsKey => up(SoCParamsKey).copy(UseXSNoCTop = true) 469 }) 470) 471 472class XSNoCTopMinimalConfig(n: Int = 1) extends Config( 473 (new KunminghuV2MinimalConfig(n)).alter((site, here, up) => { 474 case SoCParamsKey => up(SoCParamsKey).copy(UseXSNoCTop = true) 475 }) 476) 477 478class XSNoCDiffTopConfig(n: Int = 1) extends Config( 479 (new XSNoCTopConfig(n)).alter((site, here, up) => { 480 case SoCParamsKey => up(SoCParamsKey).copy(UseXSNoCDiffTop = true) 481 }) 482) 483 484class XSNoCDiffTopMinimalConfig(n: Int = 1) extends Config( 485 (new XSNoCTopConfig(n)).alter((site, here, up) => { 486 case SoCParamsKey => up(SoCParamsKey).copy(UseXSNoCDiffTop = true) 487 }) 488) 489 490class FpgaDefaultConfig(n: Int = 1) extends Config( 491 (L3CacheConfig("3MB", inclusive = false, banks = 1, ways = 6) 492 ++ L2CacheConfig("1MB", inclusive = true, banks = 4) 493 ++ WithNKBL1D(64, ways = 4) 494 ++ new BaseConfig(n)).alter((site, here, up) => { 495 case DebugOptionsKey => up(DebugOptionsKey).copy( 496 AlwaysBasicDiff = false, 497 AlwaysBasicDB = false 498 ) 499 case SoCParamsKey => up(SoCParamsKey).copy( 500 L3CacheParamsOpt = Some(up(SoCParamsKey).L3CacheParamsOpt.get.copy( 501 sramClkDivBy2 = false, 502 )), 503 ) 504 }) 505) 506 507class FpgaDiffDefaultConfig(n: Int = 1) extends Config( 508 (L3CacheConfig("3MB", inclusive = false, banks = 1, ways = 6) 509 ++ L2CacheConfig("1MB", inclusive = true, banks = 4) 510 ++ WithNKBL1D(64, ways = 8) 511 ++ new BaseConfig(n)).alter((site, here, up) => { 512 case DebugOptionsKey => up(DebugOptionsKey).copy( 513 AlwaysBasicDiff = true, 514 AlwaysBasicDB = false 515 ) 516 case SoCParamsKey => up(SoCParamsKey).copy( 517 L3CacheParamsOpt = Some(up(SoCParamsKey).L3CacheParamsOpt.get.copy( 518 sramClkDivBy2 = false, 519 )), 520 ) 521 }) 522) 523