xref: /XiangShan/src/main/scala/xiangshan/XSCore.scala (revision 4d8915fda0e6d6f5e1468fbbcaadb531c95e25c4)
1package xiangshan
2
3import chisel3._
4import chisel3.util._
5import noop.{Cache, CacheConfig, HasExceptionNO, TLB, TLBConfig}
6import top.Parameters
7import xiangshan.backend._
8import xiangshan.backend.dispatch.DispatchParameters
9import xiangshan.backend.exu.ExuParameters
10import xiangshan.frontend._
11import xiangshan.mem._
12import xiangshan.cache.{ICache, DCache, DCacheParameters, ICacheParameters, L1plusCacheParameters, PTW, Uncache}
13import chipsalliance.rocketchip.config
14import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
15import freechips.rocketchip.tilelink.{TLBundleParameters, TLCacheCork, TLBuffer, TLClientNode, TLIdentityNode, TLXbar}
16import sifive.blocks.inclusivecache.{CacheParameters, InclusiveCache, InclusiveCacheMicroParameters}
17import utils._
18
19case class XSCoreParameters
20(
21  XLEN: Int = 64,
22  HasMExtension: Boolean = true,
23  HasCExtension: Boolean = true,
24  HasDiv: Boolean = true,
25  HasICache: Boolean = true,
26  HasDCache: Boolean = true,
27  EnableStoreQueue: Boolean = true,
28  AddrBits: Int = 64,
29  VAddrBits: Int = 39,
30  PAddrBits: Int = 40,
31  HasFPU: Boolean = false,
32  FectchWidth: Int = 8,
33  EnableBPU: Boolean = true,
34  EnableBPD: Boolean = true,
35  EnableRAS: Boolean = true,
36  EnableLB: Boolean = false,
37  EnableLoop: Boolean = false,
38  HistoryLength: Int = 64,
39  BtbSize: Int = 2048,
40  JbtacSize: Int = 1024,
41  JbtacBanks: Int = 8,
42  RasSize: Int = 16,
43  CacheLineSize: Int = 512,
44  UBtbWays: Int = 16,
45  BtbWays: Int = 2,
46  IBufSize: Int = 64,
47  DecodeWidth: Int = 6,
48  RenameWidth: Int = 6,
49  CommitWidth: Int = 6,
50  BrqSize: Int = 12,
51  IssQueSize: Int = 8,
52  NRPhyRegs: Int = 72,
53  NRIntReadPorts: Int = 8,
54  NRIntWritePorts: Int = 8,
55  NRFpReadPorts: Int = 14,
56  NRFpWritePorts: Int = 8,
57  EnableUnifiedLSQ: Boolean = false,
58  LsroqSize: Int = 16,
59  LoadQueueSize: Int = 12,
60  StoreQueueSize: Int = 10,
61  RoqSize: Int = 32,
62  dpParams: DispatchParameters = DispatchParameters(
63    DqEnqWidth = 4,
64    IntDqSize = 24,
65    FpDqSize = 16,
66    LsDqSize = 16,
67    IntDqDeqWidth = 4,
68    FpDqDeqWidth = 4,
69    LsDqDeqWidth = 4,
70    IntDqReplayWidth = 4,
71    FpDqReplayWidth = 4,
72    LsDqReplayWidth = 4
73  ),
74  exuParameters: ExuParameters = ExuParameters(
75    JmpCnt = 1,
76    AluCnt = 4,
77    MulCnt = 0,
78    MduCnt = 2,
79    FmacCnt = 0,
80    FmiscCnt = 0,
81    FmiscDivSqrtCnt = 0,
82    LduCnt = 2,
83    StuCnt = 2
84  ),
85  LoadPipelineWidth: Int = 2,
86  StorePipelineWidth: Int = 2,
87  StoreBufferSize: Int = 16,
88  RefillSize: Int = 512,
89  TlbEntrySize: Int = 32,
90  TlbL2EntrySize: Int = 256, // or 512
91  PtwL1EntrySize: Int = 16,
92  PtwL2EntrySize: Int = 256
93)
94
95trait HasXSParameter {
96
97  val core = Parameters.get.coreParameters
98  val env = Parameters.get.envParameters
99
100  val XLEN = core.XLEN
101  val HasMExtension = core.HasMExtension
102  val HasCExtension = core.HasCExtension
103  val HasDiv = core.HasDiv
104  val HasIcache = core.HasICache
105  val HasDcache = core.HasDCache
106  val EnableStoreQueue = core.EnableStoreQueue
107  val AddrBits = core.AddrBits // AddrBits is used in some cases
108  val VAddrBits = core.VAddrBits // VAddrBits is Virtual Memory addr bits
109  val PAddrBits = core.PAddrBits // PAddrBits is Phyical Memory addr bits
110  val AddrBytes = AddrBits / 8 // unused
111  val DataBits = XLEN
112  val DataBytes = DataBits / 8
113  val HasFPU = core.HasFPU
114  val FetchWidth = core.FectchWidth
115  val PredictWidth = FetchWidth * 2
116  val EnableBPU = core.EnableBPU
117  val EnableBPD = core.EnableBPD // enable backing predictor(like Tage) in BPUStage3
118  val EnableRAS = core.EnableRAS
119  val EnableLB = core.EnableLB
120  val EnableLoop = core.EnableLoop
121  val HistoryLength = core.HistoryLength
122  val BtbSize = core.BtbSize
123  // val BtbWays = 4
124  val BtbBanks = PredictWidth
125  // val BtbSets = BtbSize / BtbWays
126  val JbtacSize = core.JbtacSize
127  val JbtacBanks = core.JbtacBanks
128  val RasSize = core.RasSize
129  val CacheLineSize = core.CacheLineSize
130  val CacheLineHalfWord = CacheLineSize / 16
131  val ExtHistoryLength = HistoryLength + 64
132  val UBtbWays = core.UBtbWays
133  val BtbWays = core.BtbWays
134  val IBufSize = core.IBufSize
135  val DecodeWidth = core.DecodeWidth
136  val RenameWidth = core.RenameWidth
137  val CommitWidth = core.CommitWidth
138  val BrqSize = core.BrqSize
139  val IssQueSize = core.IssQueSize
140  val BrTagWidth = log2Up(BrqSize)
141  val NRPhyRegs = core.NRPhyRegs
142  val PhyRegIdxWidth = log2Up(NRPhyRegs)
143  val RoqSize = core.RoqSize
144  val EnableUnifiedLSQ = core.EnableUnifiedLSQ
145  val LsroqSize = core.LsroqSize // 64
146  val InnerLsroqIdxWidth = log2Up(LsroqSize)
147  val LsroqIdxWidth = InnerLsroqIdxWidth + 1
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
168  val l1BusDataWidth = 256
169
170  val icacheParameters = ICacheParameters(
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
190trait HasXSLog { this: RawModule =>
191  implicit val moduleName: String = this.name
192}
193
194abstract class XSModule extends Module
195  with HasXSParameter
196  with HasExceptionNO
197  with HasXSLog
198
199//remove this trait after impl module logic
200trait NeedImpl { this: Module =>
201  override protected def IO[T <: Data](iodef: T): T = {
202    val io = chisel3.experimental.IO(iodef)
203    io <> DontCare
204    io
205  }
206}
207
208abstract class XSBundle extends Bundle
209  with HasXSParameter
210
211case class EnviromentParameters
212(
213  FPGAPlatform: Boolean = true,
214  EnableDebug: Boolean = false
215)
216
217object AddressSpace extends HasXSParameter {
218  // (start, size)
219  // address out of MMIO will be considered as DRAM
220  def mmio = List(
221    (0x30000000L, 0x10000000L),  // internal devices, such as CLINT and PLIC
222    (0x40000000L, 0x40000000L) // external devices
223  )
224
225  def isMMIO(addr: UInt): Bool = mmio.map(range => {
226    require(isPow2(range._2))
227    val bits = log2Up(range._2)
228    (addr ^ range._1.U)(PAddrBits-1, bits) === 0.U
229  }).reduce(_ || _)
230}
231
232
233
234class XSCore()(implicit p: config.Parameters) extends LazyModule {
235
236  val dcache = LazyModule(new DCache())
237  val uncache = LazyModule(new Uncache())
238  val icache = LazyModule(new ICache())
239  val ptw = LazyModule(new PTW())
240
241  val mem = TLIdentityNode()
242  val mmio = uncache.clientNode
243
244  // TODO: refactor these params
245  private val l2 = LazyModule(new InclusiveCache(
246    CacheParameters(
247      level = 2,
248      ways = 4,
249      sets = 512 * 1024 / (64 * 4),
250      blockBytes = 64,
251      beatBytes = 32 // beatBytes = l1BusDataWidth / 8
252    ),
253    InclusiveCacheMicroParameters(
254      writeBytes = 8
255    )
256  ))
257
258  private val xbar = TLXbar()
259
260  xbar := TLBuffer() := DebugIdentityNode() := dcache.clientNode
261  xbar := TLBuffer() := DebugIdentityNode() := icache.clientNode
262  xbar := TLBuffer() := DebugIdentityNode() := ptw.node
263
264  l2.node := xbar
265
266  mem := TLBuffer() := TLCacheCork() := TLBuffer() := l2.node
267
268  lazy val module = new XSCoreImp(this)
269}
270
271class XSCoreImp(outer: XSCore) extends LazyModuleImp(outer) with HasXSParameter {
272
273  val front = Module(new Frontend)
274  val backend = Module(new Backend)
275  val mem = Module(new Memend)
276
277  val dcache = outer.dcache.module
278  val uncache = outer.uncache.module
279  val icache = outer.icache.module
280  val ptw = outer.ptw.module
281
282  // TODO: connect this
283
284  front.io.backend <> backend.io.frontend
285  front.io.icacheResp <> icache.io.resp
286  front.io.icacheToTlb <> icache.io.tlb
287  icache.io.req <> front.io.icacheReq
288  icache.io.flush <> front.io.icacheFlush
289  mem.io.backend   <> backend.io.mem
290
291  ptw.io.tlb(0) <> mem.io.ptw
292  ptw.io.tlb(1) <> front.io.ptw
293
294  dcache.io.lsu.load    <> mem.io.loadUnitToDcacheVec
295  dcache.io.lsu.lsroq   <> mem.io.loadMiss
296  dcache.io.lsu.atomics <> mem.io.atomics
297  dcache.io.lsu.store   <> mem.io.sbufferToDcache
298  uncache.io.lsroq      <> mem.io.uncache
299
300}
301