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