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 = Some(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 L3NBanks = 1 243 ) 244 }) 245) 246 247// Non-synthesizable MinimalConfig, for fast simulation only 248class MinimalSimConfig(n: Int = 1) extends Config( 249 new MinimalConfig(n).alter((site, here, up) => { 250 case XSTileKey => up(XSTileKey).map(_.copy( 251 dcacheParametersOpt = None, 252 softPTW = true 253 )) 254 case SoCParamsKey => up(SoCParamsKey).copy( 255 L3CacheParamsOpt = None 256 ) 257 }) 258) 259 260class WithNKBL1D(n: Int, ways: Int = 8) extends Config((site, here, up) => { 261 case XSTileKey => 262 val sets = n * 1024 / ways / 64 263 up(XSTileKey).map(_.copy( 264 dcacheParametersOpt = Some(DCacheParameters( 265 nSets = sets, 266 nWays = ways, 267 tagECC = Some("secded"), 268 dataECC = Some("secded"), 269 replacer = Some("setplru"), 270 nMissEntries = 16, 271 nProbeEntries = 8, 272 nReleaseEntries = 18, 273 nMaxPrefetchEntry = 6, 274 enableTagEcc = true, 275 enableDataEcc = true, 276 cacheCtrlAddressOpt = Some(AddressSet(0x38022000, 0x7f)) 277 )) 278 )) 279}) 280 281class WithNKBL2 282( 283 n: Int, 284 ways: Int = 8, 285 inclusive: Boolean = true, 286 banks: Int = 1, 287 tp: Boolean = true 288) extends Config((site, here, up) => { 289 case XSTileKey => 290 require(inclusive, "L2 must be inclusive") 291 val upParams = up(XSTileKey) 292 val l2sets = n * 1024 / banks / ways / 64 293 upParams.map(p => p.copy( 294 L2CacheParamsOpt = Some(L2Param( 295 name = "L2", 296 ways = ways, 297 sets = l2sets, 298 clientCaches = Seq(L1Param( 299 "dcache", 300 sets = 2 * p.dcacheParametersOpt.get.nSets / banks, 301 ways = p.dcacheParametersOpt.get.nWays + 2, 302 aliasBitsOpt = p.dcacheParametersOpt.get.aliasBitsOpt, 303 vaddrBitsOpt = Some(p.GPAddrBitsSv48x4 - log2Up(p.dcacheParametersOpt.get.blockBytes)), 304 isKeywordBitsOpt = p.dcacheParametersOpt.get.isKeywordBitsOpt 305 )), 306 reqField = Seq(utility.ReqSourceField()), 307 echoField = Seq(huancun.DirtyField()), 308 tagECC = Some("secded"), 309 dataECC = Some("secded"), 310 enableTagECC = true, 311 enableDataECC = true, 312 dataCheck = Some("oddparity"), 313 prefetch = Seq(BOPParameters()) ++ 314 (if (tp) Seq(TPParameters()) else Nil) ++ 315 (if (p.prefetcher.nonEmpty) Seq(PrefetchReceiverParams()) else Nil), 316 enablePerf = !site(DebugOptionsKey).FPGAPlatform && site(DebugOptionsKey).EnablePerfDebug, 317 enableRollingDB = site(DebugOptionsKey).EnableRollingDB, 318 enableMonitor = site(DebugOptionsKey).AlwaysBasicDB, 319 elaboratedTopDown = !site(DebugOptionsKey).FPGAPlatform 320 )), 321 L2NBanks = banks 322 )) 323}) 324 325class WithNKBL3(n: Int, ways: Int = 8, inclusive: Boolean = true, banks: Int = 1) extends Config((site, here, up) => { 326 case SoCParamsKey => 327 val sets = n * 1024 / banks / ways / 64 328 val tiles = site(XSTileKey) 329 val clientDirBytes = tiles.map{ t => 330 t.L2NBanks * t.L2CacheParamsOpt.map(_.toCacheParams.capacity).getOrElse(0) 331 }.sum 332 up(SoCParamsKey).copy( 333 L3NBanks = banks, 334 L3CacheParamsOpt = Some(HCCacheParameters( 335 name = "L3", 336 level = 3, 337 ways = ways, 338 sets = sets, 339 inclusive = inclusive, 340 clientCaches = tiles.map{ core => 341 val l2params = core.L2CacheParamsOpt.get.toCacheParams 342 l2params.copy(sets = 2 * clientDirBytes / core.L2NBanks / l2params.ways / 64, ways = l2params.ways + 2) 343 }, 344 enablePerf = !site(DebugOptionsKey).FPGAPlatform && site(DebugOptionsKey).EnablePerfDebug, 345 ctrl = Some(CacheCtrl( 346 address = 0x39000000, 347 numCores = tiles.size 348 )), 349 reqField = Seq(utility.ReqSourceField()), 350 sramClkDivBy2 = true, 351 sramDepthDiv = 4, 352 tagECC = Some("secded"), 353 dataECC = Some("secded"), 354 simulation = !site(DebugOptionsKey).FPGAPlatform, 355 prefetch = Some(huancun.prefetch.L3PrefetchReceiverParams()), 356 tpmeta = Some(huancun.prefetch.DefaultTPmetaParameters()) 357 )), 358 OpenLLCParamsOpt = Some(OpenLLCParam( 359 name = "LLC", 360 ways = ways, 361 sets = sets, 362 banks = banks, 363 fullAddressBits = 48, 364 clientCaches = tiles.map { core => 365 val l2params = core.L2CacheParamsOpt.get 366 l2params.copy(sets = 2 * clientDirBytes / core.L2NBanks / l2params.ways / 64, ways = l2params.ways + 2) 367 }, 368 enablePerf = !site(DebugOptionsKey).FPGAPlatform && site(DebugOptionsKey).EnablePerfDebug, 369 elaboratedTopDown = !site(DebugOptionsKey).FPGAPlatform 370 )) 371 ) 372}) 373 374class WithL3DebugConfig extends Config( 375 new WithNKBL3(256, inclusive = false) ++ new WithNKBL2(64) 376) 377 378class MinimalL3DebugConfig(n: Int = 1) extends Config( 379 new WithL3DebugConfig ++ new MinimalConfig(n) 380) 381 382class DefaultL3DebugConfig(n: Int = 1) extends Config( 383 new WithL3DebugConfig ++ new BaseConfig(n) 384) 385 386class WithFuzzer extends Config((site, here, up) => { 387 case DebugOptionsKey => up(DebugOptionsKey).copy( 388 EnablePerfDebug = false, 389 ) 390 case SoCParamsKey => up(SoCParamsKey).copy( 391 L3CacheParamsOpt = Some(up(SoCParamsKey).L3CacheParamsOpt.get.copy( 392 enablePerf = false, 393 )), 394 ) 395 case XSTileKey => up(XSTileKey).zipWithIndex.map{ case (p, i) => 396 p.copy( 397 L2CacheParamsOpt = Some(up(XSTileKey)(i).L2CacheParamsOpt.get.copy( 398 enablePerf = false, 399 )), 400 ) 401 } 402}) 403 404class MinimalAliasDebugConfig(n: Int = 1) extends Config( 405 new WithNKBL3(512, inclusive = false) ++ 406 new WithNKBL2(256, inclusive = true) ++ 407 new WithNKBL1D(128) ++ 408 new MinimalConfig(n) 409) 410 411class MediumConfig(n: Int = 1) extends Config( 412 new WithNKBL3(4096, inclusive = false, banks = 4) 413 ++ new WithNKBL2(512, inclusive = true) 414 ++ new WithNKBL1D(128) 415 ++ new BaseConfig(n) 416) 417 418class FuzzConfig(dummy: Int = 0) extends Config( 419 new WithFuzzer 420 ++ new DefaultConfig(1) 421) 422 423class DefaultConfig(n: Int = 1) extends Config( 424 new WithNKBL3(16 * 1024, inclusive = false, banks = 4, ways = 16) 425 ++ new WithNKBL2(2 * 512, inclusive = true, banks = 4) 426 ++ new WithNKBL1D(64, ways = 4) 427 ++ new BaseConfig(n) 428) 429 430class WithCHI extends Config((_, _, _) => { 431 case EnableCHI => true 432}) 433 434class KunminghuV2Config(n: Int = 1) extends Config( 435 new WithCHI 436 ++ new Config((site, here, up) => { 437 case SoCParamsKey => up(SoCParamsKey).copy(L3CacheParamsOpt = None) // There will be no L3 438 }) 439 ++ new WithNKBL2(2 * 512, inclusive = true, banks = 4, tp = false) 440 ++ new WithNKBL1D(64, ways = 4) 441 ++ new DefaultConfig(n) 442) 443 444class KunminghuV2MinimalConfig(n: Int = 1) extends Config( 445 new WithCHI 446 ++ new Config((site, here, up) => { 447 case SoCParamsKey => up(SoCParamsKey).copy(L3CacheParamsOpt = None) // There will be no L3 448 }) 449 ++ new WithNKBL2(128, inclusive = true, banks = 1, tp = false) 450 ++ new WithNKBL1D(32, ways = 4) 451 ++ new MinimalConfig(n) 452) 453 454class XSNoCTopConfig(n: Int = 1) extends Config( 455 (new KunminghuV2Config(n)).alter((site, here, up) => { 456 case SoCParamsKey => up(SoCParamsKey).copy(UseXSNoCTop = true) 457 }) 458) 459 460class XSNoCTopMinimalConfig(n: Int = 1) extends Config( 461 (new KunminghuV2MinimalConfig(n)).alter((site, here, up) => { 462 case SoCParamsKey => up(SoCParamsKey).copy(UseXSNoCTop = true) 463 }) 464) 465 466class FpgaDefaultConfig(n: Int = 1) extends Config( 467 (new WithNKBL3(3 * 1024, inclusive = false, banks = 1, ways = 6) 468 ++ new WithNKBL2(2 * 512, inclusive = true, banks = 4) 469 ++ new WithNKBL1D(64, ways = 4) 470 ++ new BaseConfig(n)).alter((site, here, up) => { 471 case DebugOptionsKey => up(DebugOptionsKey).copy( 472 AlwaysBasicDiff = false, 473 AlwaysBasicDB = false 474 ) 475 case SoCParamsKey => up(SoCParamsKey).copy( 476 L3CacheParamsOpt = Some(up(SoCParamsKey).L3CacheParamsOpt.get.copy( 477 sramClkDivBy2 = false, 478 )), 479 ) 480 }) 481) 482 483class FpgaDiffDefaultConfig(n: Int = 1) extends Config( 484 (new WithNKBL3(3 * 1024, inclusive = false, banks = 1, ways = 6) 485 ++ new WithNKBL2(2 * 512, inclusive = true, banks = 4) 486 ++ new WithNKBL1D(64, ways = 8) 487 ++ new BaseConfig(n)).alter((site, here, up) => { 488 case DebugOptionsKey => up(DebugOptionsKey).copy( 489 AlwaysBasicDiff = true, 490 AlwaysBasicDB = false 491 ) 492 case SoCParamsKey => up(SoCParamsKey).copy( 493 L3CacheParamsOpt = Some(up(SoCParamsKey).L3CacheParamsOpt.get.copy( 494 sramClkDivBy2 = false, 495 )), 496 ) 497 }) 498) 499