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 xiangshan 18 19import chipsalliance.rocketchip.config.{Field, Parameters} 20import chisel3._ 21import chisel3.util._ 22import xiangshan.backend.exu._ 23import xiangshan.backend.dispatch.DispatchParameters 24import xiangshan.cache.DCacheParameters 25import xiangshan.cache.prefetch._ 26import xiangshan.frontend.{BasePredictor, BranchPredictionResp, FTB, FakePredictor, RAS, Tage, ITTage, Tage_SC, FauFTB} 27import xiangshan.frontend.icache.ICacheParameters 28import xiangshan.cache.mmu.{L2TLBParameters, TLBParameters} 29import freechips.rocketchip.diplomacy.AddressSet 30import system.SoCParamsKey 31import huancun._ 32import huancun.debug._ 33import scala.math.min 34 35case object XSTileKey extends Field[Seq[XSCoreParameters]] 36 37case object XSCoreParamsKey extends Field[XSCoreParameters] 38 39case class XSCoreParameters 40( 41 HasPrefetch: Boolean = false, 42 HartId: Int = 0, 43 XLEN: Int = 64, 44 HasMExtension: Boolean = true, 45 HasCExtension: Boolean = true, 46 HasDiv: Boolean = true, 47 HasICache: Boolean = true, 48 HasDCache: Boolean = true, 49 AddrBits: Int = 64, 50 VAddrBits: Int = 39, 51 HasFPU: Boolean = true, 52 HasVPU: Boolean = false, 53 HasCustomCSRCacheOp: Boolean = true, 54 FetchWidth: Int = 8, 55 AsidLength: Int = 16, 56 EnableBPU: Boolean = true, 57 EnableBPD: Boolean = true, 58 EnableRAS: Boolean = true, 59 EnableLB: Boolean = false, 60 EnableLoop: Boolean = true, 61 EnableSC: Boolean = true, 62 EnbaleTlbDebug: Boolean = false, 63 EnableJal: Boolean = false, 64 EnableFauFTB: Boolean = true, 65 UbtbGHRLength: Int = 4, 66 // HistoryLength: Int = 512, 67 EnableGHistDiff: Boolean = true, 68 UbtbSize: Int = 256, 69 FtbSize: Int = 2048, 70 RasSize: Int = 32, 71 CacheLineSize: Int = 512, 72 FtbWays: Int = 4, 73 TageTableInfos: Seq[Tuple3[Int,Int,Int]] = 74 // Sets Hist Tag 75 // Seq(( 2048, 2, 8), 76 // ( 2048, 9, 8), 77 // ( 2048, 13, 8), 78 // ( 2048, 20, 8), 79 // ( 2048, 26, 8), 80 // ( 2048, 44, 8), 81 // ( 2048, 73, 8), 82 // ( 2048, 256, 8)), 83 Seq(( 4096, 8, 8), 84 ( 4096, 13, 8), 85 ( 4096, 32, 8), 86 ( 4096, 119, 8)), 87 ITTageTableInfos: Seq[Tuple3[Int,Int,Int]] = 88 // Sets Hist Tag 89 Seq(( 256, 4, 9), 90 ( 256, 8, 9), 91 ( 512, 13, 9), 92 ( 512, 16, 9), 93 ( 512, 32, 9)), 94 SCNRows: Int = 512, 95 SCNTables: Int = 4, 96 SCCtrBits: Int = 6, 97 SCHistLens: Seq[Int] = Seq(0, 4, 10, 16), 98 numBr: Int = 2, 99 branchPredictor: Function2[BranchPredictionResp, Parameters, Tuple2[Seq[BasePredictor], BranchPredictionResp]] = 100 ((resp_in: BranchPredictionResp, p: Parameters) => { 101 val ftb = Module(new FTB()(p)) 102 val ubtb =Module(new FauFTB()(p)) 103 // val bim = Module(new BIM()(p)) 104 val tage = Module(new Tage_SC()(p)) 105 val ras = Module(new RAS()(p)) 106 val ittage = Module(new ITTage()(p)) 107 val preds = Seq(ubtb, tage, ftb, ittage, ras) 108 preds.map(_.io := DontCare) 109 110 // ubtb.io.resp_in(0) := resp_in 111 // bim.io.resp_in(0) := ubtb.io.resp 112 // btb.io.resp_in(0) := bim.io.resp 113 // tage.io.resp_in(0) := btb.io.resp 114 // loop.io.resp_in(0) := tage.io.resp 115 ubtb.io.in.bits.resp_in(0) := resp_in 116 tage.io.in.bits.resp_in(0) := ubtb.io.out 117 ftb.io.in.bits.resp_in(0) := tage.io.out 118 ittage.io.in.bits.resp_in(0) := ftb.io.out 119 ras.io.in.bits.resp_in(0) := ittage.io.out 120 121 (preds, ras.io.out) 122 }), 123 IBufSize: Int = 48, 124 DecodeWidth: Int = 6, 125 RenameWidth: Int = 6, 126 CommitWidth: Int = 6, 127 FtqSize: Int = 64, 128 EnableLoadFastWakeUp: Boolean = true, // NOTE: not supported now, make it false 129 IssQueSize: Int = 16, 130 NRPhyRegs: Int = 192, 131 LoadQueueSize: Int = 80, 132 LoadQueueNWriteBanks: Int = 8, 133 StoreQueueSize: Int = 64, 134 StoreQueueNWriteBanks: Int = 8, 135 VlsQueueSize: Int = 8, 136 RobSize: Int = 256, 137 dpParams: DispatchParameters = DispatchParameters( 138 IntDqSize = 16, 139 FpDqSize = 16, 140 LsDqSize = 16, 141 IntDqDeqWidth = 4, 142 FpDqDeqWidth = 4, 143 LsDqDeqWidth = 4 144 ), 145 exuParameters: ExuParameters = ExuParameters( 146 JmpCnt = 1, 147 AluCnt = 4, 148 MulCnt = 0, 149 MduCnt = 2, 150 FmacCnt = 4, 151 FmiscCnt = 2, 152 FmiscDivSqrtCnt = 0, 153 LduCnt = 2, 154 StuCnt = 2 155 ), 156 LoadPipelineWidth: Int = 2, 157 StorePipelineWidth: Int = 2, 158 VecMemSrcInWidth: Int = 2, 159 VecMemInstWbWidth: Int = 1, 160 VecMemDispatchWidth: Int = 1, 161 StoreBufferSize: Int = 16, 162 StoreBufferThreshold: Int = 7, 163 EnsbufferWidth: Int = 2, 164 UncacheBufferSize: Int = 4, 165 EnableLoadToLoadForward: Boolean = true, 166 EnableFastForward: Boolean = false, 167 EnableLdVioCheckAfterReset: Boolean = true, 168 EnableSoftPrefetchAfterReset: Boolean = true, 169 EnableCacheErrorAfterReset: Boolean = true, 170 EnableAccurateLoadError: Boolean = true, 171 EnableUncacheWriteOutstanding: Boolean = true, 172 MMUAsidLen: Int = 16, // max is 16, 0 is not supported now 173 ReSelectLen: Int = 6, // load replay queue replay select counter len 174 itlbParameters: TLBParameters = TLBParameters( 175 name = "itlb", 176 fetchi = true, 177 useDmode = false, 178 normalNWays = 32, 179 normalReplacer = Some("plru"), 180 superNWays = 4, 181 superReplacer = Some("plru") 182 ), 183 ldtlbParameters: TLBParameters = TLBParameters( 184 name = "ldtlb", 185 normalNSets = 64, 186 normalNWays = 1, 187 normalAssociative = "sa", 188 normalReplacer = Some("setplru"), 189 superNWays = 16, 190 normalAsVictim = true, 191 outReplace = false, 192 partialStaticPMP = true, 193 outsideRecvFlush = true, 194 saveLevel = true 195 ), 196 sttlbParameters: TLBParameters = TLBParameters( 197 name = "sttlb", 198 normalNSets = 64, 199 normalNWays = 1, 200 normalAssociative = "sa", 201 normalReplacer = Some("setplru"), 202 superNWays = 16, 203 normalAsVictim = true, 204 outReplace = false, 205 partialStaticPMP = true, 206 outsideRecvFlush = true, 207 saveLevel = true 208 ), 209 refillBothTlb: Boolean = false, 210 btlbParameters: TLBParameters = TLBParameters( 211 name = "btlb", 212 normalNSets = 1, 213 normalNWays = 64, 214 superNWays = 4, 215 ), 216 l2tlbParameters: L2TLBParameters = L2TLBParameters(), 217 NumPerfCounters: Int = 16, 218 icacheParameters: ICacheParameters = ICacheParameters( 219 tagECC = Some("parity"), 220 dataECC = Some("parity"), 221 replacer = Some("setplru"), 222 nMissEntries = 2, 223 nProbeEntries = 2, 224 nPrefetchEntries = 2, 225 hasPrefetch = true, 226 ), 227 dcacheParametersOpt: Option[DCacheParameters] = Some(DCacheParameters( 228 tagECC = Some("secded"), 229 dataECC = Some("secded"), 230 replacer = Some("setplru"), 231 nMissEntries = 16, 232 nProbeEntries = 8, 233 nReleaseEntries = 18 234 )), 235 L2CacheParamsOpt: Option[HCCacheParameters] = Some(HCCacheParameters( 236 name = "l2", 237 level = 2, 238 ways = 8, 239 sets = 1024, // default 512KB L2 240 prefetch = Some(huancun.prefetch.BOPParameters()) 241 )), 242 L2NBanks: Int = 1, 243 usePTWRepeater: Boolean = false, 244 softPTW: Boolean = false // dpi-c debug only 245){ 246 val allHistLens = SCHistLens ++ ITTageTableInfos.map(_._2) ++ TageTableInfos.map(_._2) :+ UbtbGHRLength 247 val HistoryLength = allHistLens.max + numBr * FtqSize + 9 // 256 for the predictor configs now 248 249 val loadExuConfigs = Seq.fill(exuParameters.LduCnt)(LdExeUnitCfg) 250 val storeExuConfigs = Seq.fill(exuParameters.StuCnt)(StaExeUnitCfg) ++ Seq.fill(exuParameters.StuCnt)(StdExeUnitCfg) 251 252 val intExuConfigs = (Seq.fill(exuParameters.AluCnt)(AluExeUnitCfg) ++ 253 Seq.fill(exuParameters.MduCnt)(MulDivExeUnitCfg) :+ JumpCSRExeUnitCfg) 254 255 val fpExuConfigs = 256 Seq.fill(exuParameters.FmacCnt)(FmacExeUnitCfg) ++ 257 Seq.fill(exuParameters.FmiscCnt)(FmiscExeUnitCfg) 258 259 val exuConfigs: Seq[ExuConfig] = intExuConfigs ++ fpExuConfigs ++ loadExuConfigs ++ storeExuConfigs 260} 261 262case object DebugOptionsKey extends Field[DebugOptions] 263 264case class DebugOptions 265( 266 FPGAPlatform: Boolean = false, 267 EnableDifftest: Boolean = false, 268 AlwaysBasicDiff: Boolean = true, 269 EnableDebug: Boolean = false, 270 EnablePerfDebug: Boolean = true, 271 UseDRAMSim: Boolean = false, 272 EnableTopDown: Boolean = false 273) 274 275trait HasXSParameter { 276 277 implicit val p: Parameters 278 279 val PAddrBits = p(SoCParamsKey).PAddrBits // PAddrBits is Phyical Memory addr bits 280 281 val coreParams = p(XSCoreParamsKey) 282 val env = p(DebugOptionsKey) 283 284 val XLEN = coreParams.XLEN 285 val minFLen = 32 286 val fLen = 64 287 def xLen = XLEN 288 289 val HasMExtension = coreParams.HasMExtension 290 val HasCExtension = coreParams.HasCExtension 291 val HasDiv = coreParams.HasDiv 292 val HasIcache = coreParams.HasICache 293 val HasDcache = coreParams.HasDCache 294 val AddrBits = coreParams.AddrBits // AddrBits is used in some cases 295 val VAddrBits = coreParams.VAddrBits // VAddrBits is Virtual Memory addr bits 296 val AsidLength = coreParams.AsidLength 297 val ReSelectLen = coreParams.ReSelectLen 298 val AddrBytes = AddrBits / 8 // unused 299 val DataBits = XLEN 300 val DataBytes = DataBits / 8 301 val HasFPU = coreParams.HasFPU 302 val HasVPU = coreParams.HasVPU 303 val HasCustomCSRCacheOp = coreParams.HasCustomCSRCacheOp 304 val FetchWidth = coreParams.FetchWidth 305 val PredictWidth = FetchWidth * (if (HasCExtension) 2 else 1) 306 val EnableBPU = coreParams.EnableBPU 307 val EnableBPD = coreParams.EnableBPD // enable backing predictor(like Tage) in BPUStage3 308 val EnableRAS = coreParams.EnableRAS 309 val EnableLB = coreParams.EnableLB 310 val EnableLoop = coreParams.EnableLoop 311 val EnableSC = coreParams.EnableSC 312 val EnbaleTlbDebug = coreParams.EnbaleTlbDebug 313 val HistoryLength = coreParams.HistoryLength 314 val EnableGHistDiff = coreParams.EnableGHistDiff 315 val UbtbGHRLength = coreParams.UbtbGHRLength 316 val UbtbSize = coreParams.UbtbSize 317 val EnableFauFTB = coreParams.EnableFauFTB 318 val FtbSize = coreParams.FtbSize 319 val FtbWays = coreParams.FtbWays 320 val RasSize = coreParams.RasSize 321 322 def getBPDComponents(resp_in: BranchPredictionResp, p: Parameters) = { 323 coreParams.branchPredictor(resp_in, p) 324 } 325 val numBr = coreParams.numBr 326 val TageTableInfos = coreParams.TageTableInfos 327 val TageBanks = coreParams.numBr 328 val SCNRows = coreParams.SCNRows 329 val SCCtrBits = coreParams.SCCtrBits 330 val SCHistLens = coreParams.SCHistLens 331 val SCNTables = coreParams.SCNTables 332 333 val SCTableInfos = Seq.fill(SCNTables)((SCNRows, SCCtrBits)) zip SCHistLens map { 334 case ((n, cb), h) => (n, cb, h) 335 } 336 val ITTageTableInfos = coreParams.ITTageTableInfos 337 type FoldedHistoryInfo = Tuple2[Int, Int] 338 val foldedGHistInfos = 339 (TageTableInfos.map{ case (nRows, h, t) => 340 if (h > 0) 341 Set((h, min(log2Ceil(nRows/numBr), h)), (h, min(h, t)), (h, min(h, t-1))) 342 else 343 Set[FoldedHistoryInfo]() 344 }.reduce(_++_).toSet ++ 345 SCTableInfos.map{ case (nRows, _, h) => 346 if (h > 0) 347 Set((h, min(log2Ceil(nRows/TageBanks), h))) 348 else 349 Set[FoldedHistoryInfo]() 350 }.reduce(_++_).toSet ++ 351 ITTageTableInfos.map{ case (nRows, h, t) => 352 if (h > 0) 353 Set((h, min(log2Ceil(nRows), h)), (h, min(h, t)), (h, min(h, t-1))) 354 else 355 Set[FoldedHistoryInfo]() 356 }.reduce(_++_) ++ 357 Set[FoldedHistoryInfo]((UbtbGHRLength, log2Ceil(UbtbSize))) 358 ).toList 359 360 361 362 val CacheLineSize = coreParams.CacheLineSize 363 val CacheLineHalfWord = CacheLineSize / 16 364 val ExtHistoryLength = HistoryLength + 64 365 val IBufSize = coreParams.IBufSize 366 val DecodeWidth = coreParams.DecodeWidth 367 val RenameWidth = coreParams.RenameWidth 368 val CommitWidth = coreParams.CommitWidth 369 val FtqSize = coreParams.FtqSize 370 val IssQueSize = coreParams.IssQueSize 371 val EnableLoadFastWakeUp = coreParams.EnableLoadFastWakeUp 372 val NRPhyRegs = coreParams.NRPhyRegs 373 val PhyRegIdxWidth = log2Up(NRPhyRegs) 374 val RobSize = coreParams.RobSize 375 val IntRefCounterWidth = log2Ceil(RobSize) 376 val LoadQueueSize = coreParams.LoadQueueSize 377 val LoadQueueNWriteBanks = coreParams.LoadQueueNWriteBanks 378 val StoreQueueSize = coreParams.StoreQueueSize 379 val StoreQueueNWriteBanks = coreParams.StoreQueueNWriteBanks 380 val VlsQueueSize = coreParams.VlsQueueSize 381 val dpParams = coreParams.dpParams 382 val exuParameters = coreParams.exuParameters 383 val NRMemReadPorts = exuParameters.LduCnt + 2 * exuParameters.StuCnt 384 val NRIntReadPorts = 2 * exuParameters.AluCnt + NRMemReadPorts 385 val NRIntWritePorts = exuParameters.AluCnt + exuParameters.MduCnt + exuParameters.LduCnt 386 val NRFpReadPorts = 3 * exuParameters.FmacCnt + exuParameters.StuCnt 387 val NRFpWritePorts = exuParameters.FpExuCnt + exuParameters.LduCnt 388 val LoadPipelineWidth = coreParams.LoadPipelineWidth 389 val StorePipelineWidth = coreParams.StorePipelineWidth 390 val VecMemSrcInWidth = coreParams.VecMemSrcInWidth 391 val VecMemInstWbWidth = coreParams.VecMemInstWbWidth 392 val VecMemDispatchWidth = coreParams.VecMemDispatchWidth 393 val StoreBufferSize = coreParams.StoreBufferSize 394 val StoreBufferThreshold = coreParams.StoreBufferThreshold 395 val EnsbufferWidth = coreParams.EnsbufferWidth 396 val UncacheBufferSize = coreParams.UncacheBufferSize 397 val EnableLoadToLoadForward = coreParams.EnableLoadToLoadForward 398 val EnableFastForward = coreParams.EnableFastForward 399 val EnableLdVioCheckAfterReset = coreParams.EnableLdVioCheckAfterReset 400 val EnableSoftPrefetchAfterReset = coreParams.EnableSoftPrefetchAfterReset 401 val EnableCacheErrorAfterReset = coreParams.EnableCacheErrorAfterReset 402 val EnableAccurateLoadError = coreParams.EnableAccurateLoadError 403 val EnableUncacheWriteOutstanding = coreParams.EnableUncacheWriteOutstanding 404 val asidLen = coreParams.MMUAsidLen 405 val BTLBWidth = coreParams.LoadPipelineWidth + coreParams.StorePipelineWidth 406 val refillBothTlb = coreParams.refillBothTlb 407 val itlbParams = coreParams.itlbParameters 408 val ldtlbParams = coreParams.ldtlbParameters 409 val sttlbParams = coreParams.sttlbParameters 410 val btlbParams = coreParams.btlbParameters 411 val l2tlbParams = coreParams.l2tlbParameters 412 val NumPerfCounters = coreParams.NumPerfCounters 413 414 val NumRs = (exuParameters.JmpCnt+1)/2 + (exuParameters.AluCnt+1)/2 + (exuParameters.MulCnt+1)/2 + 415 (exuParameters.MduCnt+1)/2 + (exuParameters.FmacCnt+1)/2 + + (exuParameters.FmiscCnt+1)/2 + 416 (exuParameters.FmiscDivSqrtCnt+1)/2 + (exuParameters.LduCnt+1)/2 + 417 (exuParameters.StuCnt+1)/2 + (exuParameters.StuCnt+1)/2 418 419 val instBytes = if (HasCExtension) 2 else 4 420 val instOffsetBits = log2Ceil(instBytes) 421 422 val icacheParameters = coreParams.icacheParameters 423 val dcacheParameters = coreParams.dcacheParametersOpt.getOrElse(DCacheParameters()) 424 425 // dcache block cacheline when lr for LRSCCycles - LRSCBackOff cycles 426 // for constrained LR/SC loop 427 val LRSCCycles = 64 428 // for lr storm 429 val LRSCBackOff = 8 430 431 // cache hierarchy configurations 432 val l1BusDataWidth = 256 433 434 // load violation predict 435 val ResetTimeMax2Pow = 20 //1078576 436 val ResetTimeMin2Pow = 10 //1024 437 // wait table parameters 438 val WaitTableSize = 1024 439 val MemPredPCWidth = log2Up(WaitTableSize) 440 val LWTUse2BitCounter = true 441 // store set parameters 442 val SSITSize = WaitTableSize 443 val LFSTSize = 32 444 val SSIDWidth = log2Up(LFSTSize) 445 val LFSTWidth = 4 446 val StoreSetEnable = true // LWT will be disabled if SS is enabled 447 448 val loadExuConfigs = coreParams.loadExuConfigs 449 val storeExuConfigs = coreParams.storeExuConfigs 450 451 val intExuConfigs = coreParams.intExuConfigs 452 453 val fpExuConfigs = coreParams.fpExuConfigs 454 455 val exuConfigs = coreParams.exuConfigs 456 457 val PCntIncrStep: Int = 6 458 val numPCntHc: Int = 25 459 val numPCntPtw: Int = 19 460 461 val numCSRPCntFrontend = 8 462 val numCSRPCntCtrl = 8 463 val numCSRPCntLsu = 8 464 val numCSRPCntHc = 5 465} 466