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