xref: /XiangShan/src/main/scala/xiangshan/XSCore.scala (revision dbb91cb575375a8099ec458438e2c256da45afd2)
1package xiangshan
2
3import chisel3._
4import chisel3.util._
5import top.Parameters
6import xiangshan.backend._
7import xiangshan.backend.dispatch.DispatchParameters
8import xiangshan.backend.exu.ExuParameters
9import xiangshan.backend.exu.Exu._
10import xiangshan.frontend._
11import xiangshan.mem._
12import xiangshan.backend.fu.HasExceptionNO
13import xiangshan.cache.{ICache, DCache, L1plusCache, DCacheParameters, ICacheParameters, L1plusCacheParameters, PTW, Uncache}
14import chipsalliance.rocketchip.config
15import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp, AddressSet}
16import freechips.rocketchip.tilelink.{TLBundleParameters, TLCacheCork, TLBuffer, TLClientNode, TLIdentityNode, TLXbar, TLWidthWidget, TLFilter, TLToAXI4}
17import freechips.rocketchip.devices.tilelink.{TLError, DevNullParams}
18import sifive.blocks.inclusivecache.{CacheParameters, InclusiveCache, InclusiveCacheMicroParameters}
19import freechips.rocketchip.amba.axi4.{AXI4ToTL, AXI4IdentityNode, AXI4UserYanker, AXI4Fragmenter, AXI4IdIndexer, AXI4Deinterleaver}
20import utils._
21
22case class XSCoreParameters
23(
24  XLEN: Int = 64,
25  HasMExtension: Boolean = true,
26  HasCExtension: Boolean = true,
27  HasDiv: Boolean = true,
28  HasICache: Boolean = true,
29  HasDCache: Boolean = true,
30  EnableStoreQueue: Boolean = true,
31  AddrBits: Int = 64,
32  VAddrBits: Int = 39,
33  PAddrBits: Int = 40,
34  HasFPU: Boolean = true,
35  FectchWidth: Int = 8,
36  EnableBPU: Boolean = true,
37  EnableBPD: Boolean = true,
38  EnableRAS: Boolean = true,
39  EnableLB: Boolean = true,
40  EnableLoop: Boolean = true,
41  EnableSC: Boolean = false,
42  HistoryLength: Int = 64,
43  BtbSize: Int = 2048,
44  JbtacSize: Int = 1024,
45  JbtacBanks: Int = 8,
46  RasSize: Int = 16,
47  CacheLineSize: Int = 512,
48  UBtbWays: Int = 16,
49  BtbWays: Int = 2,
50  IBufSize: Int = 64,
51  DecodeWidth: Int = 6,
52  RenameWidth: Int = 6,
53  CommitWidth: Int = 6,
54  BrqSize: Int = 32,
55  IssQueSize: Int = 8,
56  NRPhyRegs: Int = 160,
57  NRIntReadPorts: Int = 14,
58  NRIntWritePorts: Int = 8,
59  NRFpReadPorts: Int = 14,
60  NRFpWritePorts: Int = 8,
61  LoadQueueSize: Int = 64,
62  StoreQueueSize: Int = 48,
63  RoqSize: Int = 192,
64  dpParams: DispatchParameters = DispatchParameters(
65    DqEnqWidth = 4,
66    IntDqSize = 128,
67    FpDqSize = 128,
68    LsDqSize = 96,
69    IntDqDeqWidth = 4,
70    FpDqDeqWidth = 4,
71    LsDqDeqWidth = 4,
72    IntDqReplayWidth = 4,
73    FpDqReplayWidth = 4,
74    LsDqReplayWidth = 4
75  ),
76  exuParameters: ExuParameters = ExuParameters(
77    JmpCnt = 1,
78    AluCnt = 4,
79    MulCnt = 0,
80    MduCnt = 2,
81    FmacCnt = 4,
82    FmiscCnt = 2,
83    FmiscDivSqrtCnt = 0,
84    LduCnt = 2,
85    StuCnt = 2
86  ),
87  LoadPipelineWidth: Int = 2,
88  StorePipelineWidth: Int = 2,
89  StoreBufferSize: Int = 16,
90  RefillSize: Int = 512,
91  TlbEntrySize: Int = 32,
92  TlbL2EntrySize: Int = 256, // or 512
93  PtwL1EntrySize: Int = 16,
94  PtwL2EntrySize: Int = 256,
95  NumPerfCounters: Int = 16
96)
97
98trait HasXSParameter {
99
100  val core = Parameters.get.coreParameters
101  val env = Parameters.get.envParameters
102
103  val XLEN = core.XLEN
104  val HasMExtension = core.HasMExtension
105  val HasCExtension = core.HasCExtension
106  val HasDiv = core.HasDiv
107  val HasIcache = core.HasICache
108  val HasDcache = core.HasDCache
109  val EnableStoreQueue = core.EnableStoreQueue
110  val AddrBits = core.AddrBits // AddrBits is used in some cases
111  val VAddrBits = core.VAddrBits // VAddrBits is Virtual Memory addr bits
112  val PAddrBits = core.PAddrBits // PAddrBits is Phyical Memory addr bits
113  val AddrBytes = AddrBits / 8 // unused
114  val DataBits = XLEN
115  val DataBytes = DataBits / 8
116  val HasFPU = core.HasFPU
117  val FetchWidth = core.FectchWidth
118  val PredictWidth = FetchWidth * 2
119  val EnableBPU = core.EnableBPU
120  val EnableBPD = core.EnableBPD // enable backing predictor(like Tage) in BPUStage3
121  val EnableRAS = core.EnableRAS
122  val EnableLB = core.EnableLB
123  val EnableLoop = core.EnableLoop
124  val EnableSC = core.EnableSC
125  val HistoryLength = core.HistoryLength
126  val BtbSize = core.BtbSize
127  // val BtbWays = 4
128  val BtbBanks = PredictWidth
129  // val BtbSets = BtbSize / BtbWays
130  val JbtacSize = core.JbtacSize
131  val JbtacBanks = core.JbtacBanks
132  val RasSize = core.RasSize
133  val CacheLineSize = core.CacheLineSize
134  val CacheLineHalfWord = CacheLineSize / 16
135  val ExtHistoryLength = HistoryLength + 64
136  val UBtbWays = core.UBtbWays
137  val BtbWays = core.BtbWays
138  val IBufSize = core.IBufSize
139  val DecodeWidth = core.DecodeWidth
140  val RenameWidth = core.RenameWidth
141  val CommitWidth = core.CommitWidth
142  val BrqSize = core.BrqSize
143  val IssQueSize = core.IssQueSize
144  val BrTagWidth = log2Up(BrqSize)
145  val NRPhyRegs = core.NRPhyRegs
146  val PhyRegIdxWidth = log2Up(NRPhyRegs)
147  val RoqSize = core.RoqSize
148  val LoadQueueSize = core.LoadQueueSize
149  val StoreQueueSize = core.StoreQueueSize
150  val dpParams = core.dpParams
151  val ReplayWidth = dpParams.IntDqReplayWidth + dpParams.FpDqReplayWidth + dpParams.LsDqReplayWidth
152  val exuParameters = core.exuParameters
153  val NRIntReadPorts = core.NRIntReadPorts
154  val NRIntWritePorts = core.NRIntWritePorts
155  val NRMemReadPorts = exuParameters.LduCnt + 2*exuParameters.StuCnt
156  val NRFpReadPorts = core.NRFpReadPorts
157  val NRFpWritePorts = core.NRFpWritePorts
158  val LoadPipelineWidth = core.LoadPipelineWidth
159  val StorePipelineWidth = core.StorePipelineWidth
160  val StoreBufferSize = core.StoreBufferSize
161  val RefillSize = core.RefillSize
162  val DTLBWidth = core.LoadPipelineWidth + core.StorePipelineWidth
163  val TlbEntrySize = core.TlbEntrySize
164  val TlbL2EntrySize = core.TlbL2EntrySize
165  val PtwL1EntrySize = core.PtwL1EntrySize
166  val PtwL2EntrySize = core.PtwL2EntrySize
167  val NumPerfCounters = core.NumPerfCounters
168
169  val icacheParameters = ICacheParameters(
170    nMissEntries = 2
171  )
172
173  val l1plusCacheParameters = L1plusCacheParameters(
174    tagECC = Some("secded"),
175    dataECC = Some("secded"),
176    nMissEntries = 8
177  )
178
179  val dcacheParameters = DCacheParameters(
180    tagECC = Some("secded"),
181    dataECC = Some("secded"),
182    nMissEntries = 16,
183    nLoadMissEntries = 8,
184    nStoreMissEntries = 8
185  )
186
187  val LRSCCycles = 100
188
189
190  // cache hierarchy configurations
191  val l1BusDataWidth = 256
192
193  // L2 configurations
194  val L1BusWidth = 256
195  val L2Size = 512 * 1024 // 512KB
196  val L2BlockSize = 64
197  val L2NWays = 8
198  val L2NSets = L2Size / L2BlockSize / L2NWays
199
200  // L3 configurations
201  val L2BusWidth = 256
202  val L3Size = 4 * 1024 * 1024 // 4MB
203  val L3BlockSize = 64
204  val L3NBanks = 4
205  val L3NWays = 8
206  val L3NSets = L3Size / L3BlockSize / L3NBanks / L3NWays
207
208  // on chip network configurations
209  val L3BusWidth = 256
210}
211
212trait HasXSLog { this: RawModule =>
213  implicit val moduleName: String = this.name
214}
215
216abstract class XSModule extends MultiIOModule
217  with HasXSParameter
218  with HasExceptionNO
219  with HasXSLog
220{
221  def io: Record
222}
223
224//remove this trait after impl module logic
225trait NeedImpl { this: RawModule =>
226  override protected def IO[T <: Data](iodef: T): T = {
227    println(s"[Warn]: (${this.name}) please reomve 'NeedImpl' after implement this module")
228    val io = chisel3.experimental.IO(iodef)
229    io <> DontCare
230    io
231  }
232}
233
234abstract class XSBundle extends Bundle
235  with HasXSParameter
236
237case class EnviromentParameters
238(
239  FPGAPlatform: Boolean = true,
240  EnableDebug: Boolean = false
241)
242
243object AddressSpace extends HasXSParameter {
244  // (start, size)
245  // address out of MMIO will be considered as DRAM
246  def mmio = List(
247    (0x00000000L, 0x40000000L),  // internal devices, such as CLINT and PLIC
248    (0x40000000L, 0x40000000L)   // external devices
249  )
250
251  def isMMIO(addr: UInt): Bool = mmio.map(range => {
252    require(isPow2(range._2))
253    val bits = log2Up(range._2)
254    (addr ^ range._1.U)(PAddrBits-1, bits) === 0.U
255  }).reduce(_ || _)
256}
257
258
259
260class XSCore()(implicit p: config.Parameters) extends LazyModule with HasXSParameter {
261
262  // outer facing nodes
263  val dcache = LazyModule(new DCache())
264  val uncache = LazyModule(new Uncache())
265  val l1pluscache = LazyModule(new L1plusCache())
266  val ptw = LazyModule(new PTW())
267
268  lazy val module = new XSCoreImp(this)
269}
270
271class XSCoreImp(outer: XSCore) extends LazyModuleImp(outer)
272  with HasXSParameter
273  with HasExeBlockHelper
274{
275  val io = IO(new Bundle {
276    val externalInterrupt = new ExternalInterruptIO
277  })
278
279  println(s"FPGAPlatform:${env.FPGAPlatform} EnableDebug:${env.EnableDebug}")
280
281  // to fast wake up fp, mem rs
282  val intBlockFastWakeUpFp = intExuConfigs.filter(fpFastFilter)
283  val intBlockSlowWakeUpFp = intExuConfigs.filter(fpSlowFilter)
284  val intBlockFastWakeUpInt = intExuConfigs.filter(intFastFilter)
285  val intBlockSlowWakeUpInt = intExuConfigs.filter(intSlowFilter)
286
287  val fpBlockFastWakeUpFp = fpExuConfigs.filter(fpFastFilter)
288  val fpBlockSlowWakeUpFp = fpExuConfigs.filter(fpSlowFilter)
289  val fpBlockFastWakeUpInt = fpExuConfigs.filter(intFastFilter)
290  val fpBlockSlowWakeUpInt = fpExuConfigs.filter(intSlowFilter)
291
292  val frontend = Module(new Frontend)
293  val ctrlBlock = Module(new CtrlBlock)
294  val integerBlock = Module(new IntegerBlock(
295    fastWakeUpIn = fpBlockFastWakeUpInt,
296    slowWakeUpIn = fpBlockSlowWakeUpInt ++ loadExuConfigs,
297    fastFpOut = intBlockFastWakeUpFp,
298    slowFpOut = intBlockSlowWakeUpFp,
299    fastIntOut = intBlockFastWakeUpInt,
300    slowIntOut = intBlockSlowWakeUpInt
301  ))
302  val floatBlock = Module(new FloatBlock(
303    fastWakeUpIn = intBlockFastWakeUpFp,
304    slowWakeUpIn = intBlockSlowWakeUpFp ++ loadExuConfigs,
305    fastFpOut = fpBlockFastWakeUpFp,
306    slowFpOut = fpBlockSlowWakeUpFp,
307    fastIntOut = fpBlockFastWakeUpInt,
308    slowIntOut = fpBlockSlowWakeUpInt
309  ))
310  val memBlock = Module(new MemBlock(
311    fastWakeUpIn = intBlockFastWakeUpInt ++ intBlockFastWakeUpFp ++ fpBlockFastWakeUpInt ++ fpBlockFastWakeUpFp,
312    slowWakeUpIn = intBlockSlowWakeUpInt ++ intBlockSlowWakeUpFp ++ fpBlockSlowWakeUpInt ++ fpBlockSlowWakeUpFp,
313    fastFpOut = Seq(),
314    slowFpOut = loadExuConfigs,
315    fastIntOut = Seq(),
316    slowIntOut = loadExuConfigs
317  ))
318
319  val dcache = outer.dcache.module
320  val uncache = outer.uncache.module
321  val l1pluscache = outer.l1pluscache.module
322  val ptw = outer.ptw.module
323  val icache = Module(new ICache)
324
325  frontend.io.backend <> ctrlBlock.io.frontend
326  frontend.io.icacheResp <> icache.io.resp
327  frontend.io.icacheToTlb <> icache.io.tlb
328  icache.io.req <> frontend.io.icacheReq
329  icache.io.flush <> frontend.io.icacheFlush
330  frontend.io.sfence <> integerBlock.io.fenceio.sfence
331  frontend.io.tlbCsr <> integerBlock.io.csrio.tlb
332
333  icache.io.mem_acquire <> l1pluscache.io.req
334  l1pluscache.io.resp <> icache.io.mem_grant
335  l1pluscache.io.flush := icache.io.l1plusflush
336  icache.io.fencei := integerBlock.io.fenceio.fencei
337
338  ctrlBlock.io.fromIntBlock <> integerBlock.io.toCtrlBlock
339  ctrlBlock.io.fromFpBlock <> floatBlock.io.toCtrlBlock
340  ctrlBlock.io.fromLsBlock <> memBlock.io.toCtrlBlock
341  ctrlBlock.io.toIntBlock <> integerBlock.io.fromCtrlBlock
342  ctrlBlock.io.toFpBlock <> floatBlock.io.fromCtrlBlock
343  ctrlBlock.io.toLsBlock <> memBlock.io.fromCtrlBlock
344
345  integerBlock.io.wakeUpIn.fastUops <> floatBlock.io.wakeUpIntOut.fastUops
346  integerBlock.io.wakeUpIn.fast <> floatBlock.io.wakeUpIntOut.fast
347  integerBlock.io.wakeUpIn.slow <> floatBlock.io.wakeUpIntOut.slow ++ memBlock.io.wakeUpIntOut.slow
348
349  floatBlock.io.wakeUpIn.fastUops <> integerBlock.io.wakeUpFpOut.fastUops
350  floatBlock.io.wakeUpIn.fast <> integerBlock.io.wakeUpFpOut.fast
351  floatBlock.io.wakeUpIn.slow <> integerBlock.io.wakeUpFpOut.slow ++ memBlock.io.wakeUpFpOut.slow
352
353
354  integerBlock.io.wakeUpIntOut.fast.map(_.ready := true.B)
355  integerBlock.io.wakeUpIntOut.slow.map(_.ready := true.B)
356  floatBlock.io.wakeUpFpOut.fast.map(_.ready := true.B)
357  floatBlock.io.wakeUpFpOut.slow.map(_.ready := true.B)
358
359  val wakeUpMem = Seq(
360    integerBlock.io.wakeUpIntOut,
361    integerBlock.io.wakeUpFpOut,
362    floatBlock.io.wakeUpIntOut,
363    floatBlock.io.wakeUpFpOut
364  )
365  memBlock.io.wakeUpIn.fastUops <> wakeUpMem.flatMap(_.fastUops)
366  memBlock.io.wakeUpIn.fast <> wakeUpMem.flatMap(w => w.fast.map(f => {
367	val raw = WireInit(f)
368	raw
369  }))
370  memBlock.io.wakeUpIn.slow <> wakeUpMem.flatMap(w => w.slow.map(s => {
371	val raw = WireInit(s)
372	raw
373  }))
374
375  integerBlock.io.csrio.fflags <> ctrlBlock.io.roqio.toCSR.fflags
376  integerBlock.io.csrio.dirty_fs <> ctrlBlock.io.roqio.toCSR.dirty_fs
377  integerBlock.io.csrio.exception <> ctrlBlock.io.roqio.exception
378  integerBlock.io.csrio.isInterrupt <> ctrlBlock.io.roqio.isInterrupt
379  integerBlock.io.csrio.trapTarget <> ctrlBlock.io.roqio.toCSR.trapTarget
380  integerBlock.io.csrio.interrupt <> ctrlBlock.io.roqio.toCSR.intrBitSet
381  integerBlock.io.csrio.memExceptionVAddr <> memBlock.io.lsqio.exceptionAddr.vaddr
382  integerBlock.io.csrio.externalInterrupt <> io.externalInterrupt
383  integerBlock.io.csrio.tlb <> memBlock.io.tlbCsr
384  integerBlock.io.fenceio.sfence <> memBlock.io.sfence
385  integerBlock.io.fenceio.sbuffer <> memBlock.io.fenceToSbuffer
386
387  floatBlock.io.frm <> integerBlock.io.csrio.frm
388
389  memBlock.io.lsqio.commits <> ctrlBlock.io.roqio.commits
390  memBlock.io.lsqio.roqDeqPtr <> ctrlBlock.io.roqio.roqDeqPtr
391  memBlock.io.lsqio.oldestStore <> ctrlBlock.io.oldestStore
392  memBlock.io.lsqio.exceptionAddr.lsIdx.lqIdx := ctrlBlock.io.roqio.exception.bits.lqIdx
393  memBlock.io.lsqio.exceptionAddr.lsIdx.sqIdx := ctrlBlock.io.roqio.exception.bits.sqIdx
394  memBlock.io.lsqio.exceptionAddr.isStore := CommitType.lsInstIsStore(ctrlBlock.io.roqio.exception.bits.ctrl.commitType)
395
396  ptw.io.tlb(0) <> memBlock.io.ptw
397  ptw.io.tlb(1) <> frontend.io.ptw
398  ptw.io.sfence <> integerBlock.io.fenceio.sfence
399  ptw.io.csr <> integerBlock.io.csrio.tlb
400
401  dcache.io.lsu.load    <> memBlock.io.dcache.loadUnitToDcacheVec
402  dcache.io.lsu.lsq   <> memBlock.io.dcache.loadMiss
403  dcache.io.lsu.atomics <> memBlock.io.dcache.atomics
404  dcache.io.lsu.store   <> memBlock.io.dcache.sbufferToDcache
405  uncache.io.lsq      <> memBlock.io.dcache.uncache
406
407  if (!env.FPGAPlatform) {
408    val debugIntReg, debugFpReg = WireInit(VecInit(Seq.fill(32)(0.U(XLEN.W))))
409    ExcitingUtils.addSink(debugIntReg, "DEBUG_INT_ARCH_REG", ExcitingUtils.Debug)
410    ExcitingUtils.addSink(debugFpReg, "DEBUG_FP_ARCH_REG", ExcitingUtils.Debug)
411    val debugArchReg = WireInit(VecInit(debugIntReg ++ debugFpReg))
412    ExcitingUtils.addSource(debugArchReg, "difftestRegs", ExcitingUtils.Debug)
413  }
414
415}
416