xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/MMUConst.scala (revision acc888877791f2eb92801cdc5650b2b68aa5a08d)
16d5ddbceSLemover/***************************************************************************************
26d5ddbceSLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory
46d5ddbceSLemover*
56d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2.
66d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
76d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at:
86d5ddbceSLemover*          http://license.coscl.org.cn/MulanPSL2
96d5ddbceSLemover*
106d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
116d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
126d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
136d5ddbceSLemover*
146d5ddbceSLemover* See the Mulan PSL v2 for more details.
156d5ddbceSLemover***************************************************************************************/
166d5ddbceSLemover
176d5ddbceSLemoverpackage xiangshan.cache.mmu
186d5ddbceSLemover
196d5ddbceSLemoverimport chipsalliance.rocketchip.config.Parameters
206d5ddbceSLemoverimport chisel3._
216d5ddbceSLemoverimport chisel3.util._
226d5ddbceSLemoverimport xiangshan._
236d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
246d5ddbceSLemoverimport utils._
256d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
266d5ddbceSLemoverimport freechips.rocketchip.tilelink._
276d5ddbceSLemover
28a0301c0dSLemovercase class TLBParameters
29a0301c0dSLemover(
30a0301c0dSLemover  name: String = "none",
31a0301c0dSLemover  fetchi: Boolean = false, // TODO: remove it
32a0301c0dSLemover  useDmode: Boolean = true,
33a0301c0dSLemover  sameCycle: Boolean = false,
34fb90f54dSLemover  missSameCycle: Boolean = false,
35a0301c0dSLemover  normalNSets: Int = 1, // when da or sa
36a0301c0dSLemover  normalNWays: Int = 8, // when fa or sa
37a0301c0dSLemover  superNSets: Int = 1,
38a0301c0dSLemover  superNWays: Int = 2,
39a0301c0dSLemover  normalReplacer: Option[String] = Some("random"),
40a0301c0dSLemover  superReplacer: Option[String] = Some("plru"),
41a0301c0dSLemover  normalAssociative: String = "fa", // "fa", "sa", "da", "sa" is not supported
42a0301c0dSLemover  superAssociative: String = "fa", // must be fa
43a0301c0dSLemover  normalAsVictim: Boolean = false, // when get replace from fa, store it into sram
44a0301c0dSLemover  outReplace: Boolean = false,
455cf62c1aSLemover  shouldBlock: Boolean = false, // only for perf, not support for io
465b7ef044SLemover  partialStaticPMP: Boolean = false, // partila static pmp result stored in entries
475cf62c1aSLemover  saveLevel: Boolean = false
48a0301c0dSLemover)
49a0301c0dSLemover
505854c1edSLemovercase class L2TLBParameters
515854c1edSLemover(
525854c1edSLemover  name: String = "l2tlb",
535854c1edSLemover  // l1
545854c1edSLemover  l1Size: Int = 16,
555854c1edSLemover  l1Associative: String = "fa",
565854c1edSLemover  l1Replacer: Option[String] = Some("plru"),
575854c1edSLemover  // l2
58ecf1a4b8SLemover  l2nSets: Int = 32,
59ecf1a4b8SLemover  l2nWays: Int = 2,
605854c1edSLemover  l2Replacer: Option[String] = Some("setplru"),
615854c1edSLemover  // l3
62fa086d5eSLemover  l3nSets: Int = 128,
63fa086d5eSLemover  l3nWays: Int = 4,
645854c1edSLemover  l3Replacer: Option[String] = Some("setplru"),
655854c1edSLemover  // sp
665854c1edSLemover  spSize: Int = 16,
675854c1edSLemover  spReplacer: Option[String] = Some("plru"),
68bc063562SLemover  // dtlb filter
69bc063562SLemover  filterSize: Int = 8,
70d74a7bd3SLemover  // miss queue, add more entries than 'must require'
71d74a7bd3SLemover  // 0 for easier bug trigger, please set as big as u can, 8 maybe
72d74a7bd3SLemover  missqueueExtendSize: Int = 0,
735854c1edSLemover  // way size
747196f5a2SLemover  blockBytes: Int = 64,
75bc063562SLemover  // prefetch
76bc063562SLemover  enablePrefetch: Boolean = true,
777196f5a2SLemover  // ecc
787196f5a2SLemover  ecc: Option[String] = Some("secded")
795854c1edSLemover)
805854c1edSLemover
816d5ddbceSLemovertrait HasTlbConst extends HasXSParameter {
826d5ddbceSLemover  val Level = 3
836d5ddbceSLemover
846d5ddbceSLemover  val offLen  = 12
856d5ddbceSLemover  val ppnLen  = PAddrBits - offLen
866d5ddbceSLemover  val vpnnLen = 9
876d5ddbceSLemover  val vpnLen  = VAddrBits - offLen
886d5ddbceSLemover  val flagLen = 8
896d5ddbceSLemover  val pteResLen = XLEN - ppnLen - 2 - flagLen
906d5ddbceSLemover
91a0301c0dSLemover  val sramSinglePort = true
92a0301c0dSLemover
93*acc88887SJiawei Lin  val timeOutThreshold = 10000
949bd9cdfaSLemover
953889e11eSLemover  def get_set_idx(vpn: UInt, nSets: Int): UInt = {
96e9092fe2SLemover    require(nSets >= 1)
97a0301c0dSLemover    vpn(log2Up(nSets)-1, 0)
986d5ddbceSLemover  }
996d5ddbceSLemover
100e9092fe2SLemover  def drop_set_idx(vpn: UInt, nSets: Int): UInt = {
101e9092fe2SLemover    require(nSets >= 1)
102e9092fe2SLemover    require(vpn.getWidth > log2Ceil(nSets))
103e9092fe2SLemover    vpn(vpn.getWidth-1, log2Ceil(nSets))
104e9092fe2SLemover  }
105e9092fe2SLemover
106e9092fe2SLemover  def drop_set_equal(vpn1: UInt, vpn2: UInt, nSets: Int): Bool = {
107e9092fe2SLemover    require(nSets >= 1)
108e9092fe2SLemover    require(vpn1.getWidth == vpn2.getWidth)
109e9092fe2SLemover    if (vpn1.getWidth <= log2Ceil(nSets)) {
110e9092fe2SLemover      true.B
111e9092fe2SLemover    } else {
112e9092fe2SLemover      drop_set_idx(vpn1, nSets) === drop_set_idx(vpn2, nSets)
113e9092fe2SLemover    }
114e9092fe2SLemover  }
115e9092fe2SLemover
1166d5ddbceSLemover  def replaceWrapper(v: UInt, lruIdx: UInt): UInt = {
1176d5ddbceSLemover    val width = v.getWidth
1186d5ddbceSLemover    val emptyIdx = ParallelPriorityMux((0 until width).map( i => (!v(i), i.U)))
1196d5ddbceSLemover    val full = Cat(v).andR
1206d5ddbceSLemover    Mux(full, lruIdx, emptyIdx)
1216d5ddbceSLemover  }
1226d5ddbceSLemover
1236d5ddbceSLemover  def replaceWrapper(v: Seq[Bool], lruIdx: UInt): UInt = {
1246d5ddbceSLemover    replaceWrapper(VecInit(v).asUInt, lruIdx)
1256d5ddbceSLemover  }
126a0301c0dSLemover
1276d5ddbceSLemover}
1286d5ddbceSLemover
1296d5ddbceSLemovertrait HasPtwConst extends HasTlbConst with MemoryOpConstants{
1306d5ddbceSLemover  val PtwWidth = 2
131bc063562SLemover  val sourceWidth = { if (l2tlbParams.enablePrefetch) PtwWidth + 1 else PtwWidth}
132bc063562SLemover  val prefetchID = PtwWidth
133bc063562SLemover  val maxPrefetchNum = l2tlbParams.filterSize
134bc063562SLemover
1355854c1edSLemover  val blockBits = l2tlbParams.blockBytes * 8
1366d5ddbceSLemover
1376d5ddbceSLemover  val bPtwWidth = log2Up(PtwWidth)
138bc063562SLemover  val bSourceWidth = log2Up(sourceWidth)
1396d5ddbceSLemover  // ptwl1: fully-associated
1406d5ddbceSLemover  val PtwL1TagLen = vpnnLen
1416d5ddbceSLemover
1426d5ddbceSLemover  /* +-------+----------+-------------+
1436d5ddbceSLemover   * |  Tag  |  SetIdx  |  SectorIdx  |
1446d5ddbceSLemover   * +-------+----------+-------------+
1456d5ddbceSLemover   */
1466d5ddbceSLemover  // ptwl2: 8-way group-associated
1475854c1edSLemover  val l2tlbParams.l2nWays = l2tlbParams.l2nWays
1485854c1edSLemover  val PtwL2SetNum = l2tlbParams.l2nSets
1495854c1edSLemover  val PtwL2SectorSize = blockBits /XLEN
1505854c1edSLemover  val PtwL2IdxLen = log2Up(PtwL2SetNum * PtwL2SectorSize)
1516d5ddbceSLemover  val PtwL2SectorIdxLen = log2Up(PtwL2SectorSize)
1525854c1edSLemover  val PtwL2SetIdxLen = log2Up(PtwL2SetNum)
1536d5ddbceSLemover  val PtwL2TagLen = vpnnLen * 2 - PtwL2IdxLen
1546d5ddbceSLemover
1556d5ddbceSLemover  // ptwl3: 16-way group-associated
1565854c1edSLemover  val l2tlbParams.l3nWays = l2tlbParams.l3nWays
1575854c1edSLemover  val PtwL3SetNum = l2tlbParams.l3nSets
1585854c1edSLemover  val PtwL3SectorSize =  blockBits / XLEN
1595854c1edSLemover  val PtwL3IdxLen = log2Up(PtwL3SetNum * PtwL3SectorSize)
1606d5ddbceSLemover  val PtwL3SectorIdxLen = log2Up(PtwL3SectorSize)
1615854c1edSLemover  val PtwL3SetIdxLen = log2Up(PtwL3SetNum)
1626d5ddbceSLemover  val PtwL3TagLen = vpnnLen * 3 - PtwL3IdxLen
1636d5ddbceSLemover
1646d5ddbceSLemover  // super page, including 1GB and 2MB page
1656d5ddbceSLemover  val SPTagLen = vpnnLen * 2
1666d5ddbceSLemover
167d74a7bd3SLemover  // miss queue
168d74a7bd3SLemover  val MSHRBaseSize = 1 + l2tlbParams.filterSize + l2tlbParams.missqueueExtendSize
169d74a7bd3SLemover  val MSHRSize =  { if (l2tlbParams.enablePrefetch) (MSHRBaseSize + 1) else MSHRBaseSize }
170b848eea5SLemover  val MemReqWidth = MSHRSize + 1
171bc063562SLemover  val FsmReqID = MSHRSize
172b848eea5SLemover  val bMemID = log2Up(MSHRSize + 1)
1736d5ddbceSLemover
1746d5ddbceSLemover  def genPtwL2Idx(vpn: UInt) = {
1756d5ddbceSLemover    (vpn(vpnLen - 1, vpnnLen))(PtwL2IdxLen - 1, 0)
1766d5ddbceSLemover  }
1776d5ddbceSLemover
1786d5ddbceSLemover  def genPtwL2SectorIdx(vpn: UInt) = {
1796d5ddbceSLemover    genPtwL2Idx(vpn)(PtwL2SectorIdxLen - 1, 0)
1806d5ddbceSLemover  }
1816d5ddbceSLemover
1826d5ddbceSLemover  def genPtwL2SetIdx(vpn: UInt) = {
1836d5ddbceSLemover    genPtwL2Idx(vpn)(PtwL2SetIdxLen + PtwL2SectorIdxLen - 1, PtwL2SectorIdxLen)
1846d5ddbceSLemover  }
1856d5ddbceSLemover
1866d5ddbceSLemover  def genPtwL3Idx(vpn: UInt) = {
1876d5ddbceSLemover    vpn(PtwL3IdxLen - 1, 0)
1886d5ddbceSLemover  }
1896d5ddbceSLemover
1906d5ddbceSLemover  def genPtwL3SectorIdx(vpn: UInt) = {
1916d5ddbceSLemover    genPtwL3Idx(vpn)(PtwL3SectorIdxLen - 1, 0)
1926d5ddbceSLemover  }
1936d5ddbceSLemover
194cc5a5f22SLemover  def dropL3SectorBits(vpn: UInt) = {
195cc5a5f22SLemover    vpn(vpn.getWidth-1, PtwL3SectorIdxLen)
196cc5a5f22SLemover  }
197cc5a5f22SLemover
1986d5ddbceSLemover  def genPtwL3SetIdx(vpn: UInt) = {
1996d5ddbceSLemover    genPtwL3Idx(vpn)(PtwL3SetIdxLen + PtwL3SectorIdxLen - 1, PtwL3SectorIdxLen)
2006d5ddbceSLemover  }
2016d5ddbceSLemover
2026d5ddbceSLemover  def MakeAddr(ppn: UInt, off: UInt) = {
2036d5ddbceSLemover    require(off.getWidth == 9)
2046d5ddbceSLemover    Cat(ppn, off, 0.U(log2Up(XLEN/8).W))(PAddrBits-1, 0)
2056d5ddbceSLemover  }
2066d5ddbceSLemover
207b848eea5SLemover  def getVpnn(vpn: UInt, idx: Int): UInt = {
2086d5ddbceSLemover    vpn(vpnnLen*(idx+1)-1, vpnnLen*idx)
2096d5ddbceSLemover  }
2106d5ddbceSLemover
2116d5ddbceSLemover  def getVpnClip(vpn: UInt, level: Int) = {
2126d5ddbceSLemover    // level 0  /* vpnn2 */
2136d5ddbceSLemover    // level 1  /* vpnn2 * vpnn1 */
2146d5ddbceSLemover    // level 2  /* vpnn2 * vpnn1 * vpnn0*/
2156d5ddbceSLemover    vpn(vpnLen - 1, (2 - level) * vpnnLen)
2166d5ddbceSLemover  }
2176d5ddbceSLemover
218bc063562SLemover  def get_next_line(vpn: UInt) = {
219bc063562SLemover    Cat(dropL3SectorBits(vpn) + 1.U, 0.U(PtwL3SectorIdxLen.W))
220bc063562SLemover  }
221bc063562SLemover
222bc063562SLemover  def same_l2entry(vpn1: UInt, vpn2: UInt) = {
223bc063562SLemover    vpn1(vpnLen-1, vpnnLen) === vpn2(vpnLen-1, vpnnLen)
224bc063562SLemover  }
225bc063562SLemover
226bc063562SLemover  def from_pre(source: UInt) = {
227bc063562SLemover    (source === prefetchID.U)
228bc063562SLemover  }
229bc063562SLemover
2306d5ddbceSLemover  def printVec[T <: Data](x: Seq[T]): Printable = {
2316d5ddbceSLemover    (0 until x.length).map(i => p"(${i.U})${x(i)} ").reduce(_+_)
2326d5ddbceSLemover  }
2336d5ddbceSLemover}
234