xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/MMUConst.scala (revision 7a2fc509e2d355879c4db3dc3f17a6ccacd3d09e)
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.cache.mmu
18
19import chipsalliance.rocketchip.config.Parameters
20import chisel3._
21import chisel3.util._
22import xiangshan._
23import xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
24import utils._
25import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
26import freechips.rocketchip.tilelink._
27
28case class TLBParameters
29(
30  name: String = "none",
31  fetchi: Boolean = false, // TODO: remove it
32  useDmode: Boolean = true,
33  sameCycle: Boolean = false,
34  missSameCycle: Boolean = false,
35  normalNSets: Int = 1, // when da or sa
36  normalNWays: Int = 8, // when fa or sa
37  superNSets: Int = 1,
38  superNWays: Int = 2,
39  normalReplacer: Option[String] = Some("random"),
40  superReplacer: Option[String] = Some("plru"),
41  normalAssociative: String = "fa", // "fa", "sa", "da", "sa" is not supported
42  superAssociative: String = "fa", // must be fa
43  normalAsVictim: Boolean = false, // when get replace from fa, store it into sram
44  outReplace: Boolean = false,
45  shouldBlock: Boolean = false, // only for perf, not support for io
46  partialStaticPMP: Boolean = false, // partila static pmp result stored in entries
47  saveLevel: Boolean = false
48)
49
50case class L2TLBParameters
51(
52  name: String = "l2tlb",
53  // l1
54  l1Size: Int = 16,
55  l1Associative: String = "fa",
56  l1Replacer: Option[String] = Some("plru"),
57  // l2
58  l2nSets: Int = 32,
59  l2nWays: Int = 2,
60  l2Replacer: Option[String] = Some("setplru"),
61  // l3
62  l3nSets: Int = 128,
63  l3nWays: Int = 4,
64  l3Replacer: Option[String] = Some("setplru"),
65  // sp
66  spSize: Int = 16,
67  spReplacer: Option[String] = Some("plru"),
68  // dtlb filter
69  filterSize: Int = 8,
70  // miss queue, add more entries than 'must require'
71  // 0 for easier bug trigger, please set as big as u can, 8 maybe
72  missqueueExtendSize: Int = 0,
73  // llptw
74  llptwsize: Int = 6,
75  // way size
76  blockBytes: Int = 64,
77  // prefetch
78  enablePrefetch: Boolean = true,
79  // ecc
80  ecc: Option[String] = Some("secded")
81)
82
83trait HasTlbConst extends HasXSParameter {
84  val Level = 3
85
86  val offLen  = 12
87  val ppnLen  = PAddrBits - offLen
88  val vpnnLen = 9
89  val vpnLen  = VAddrBits - offLen
90  val flagLen = 8
91  val pteResLen = XLEN - ppnLen - 2 - flagLen
92
93  val sramSinglePort = true
94
95  val timeOutThreshold = 10000
96
97  def get_set_idx(vpn: UInt, nSets: Int): UInt = {
98    require(nSets >= 1)
99    vpn(log2Up(nSets)-1, 0)
100  }
101
102  def drop_set_idx(vpn: UInt, nSets: Int): UInt = {
103    require(nSets >= 1)
104    require(vpn.getWidth > log2Ceil(nSets))
105    vpn(vpn.getWidth-1, log2Ceil(nSets))
106  }
107
108  def drop_set_equal(vpn1: UInt, vpn2: UInt, nSets: Int): Bool = {
109    require(nSets >= 1)
110    require(vpn1.getWidth == vpn2.getWidth)
111    if (vpn1.getWidth <= log2Ceil(nSets)) {
112      true.B
113    } else {
114      drop_set_idx(vpn1, nSets) === drop_set_idx(vpn2, nSets)
115    }
116  }
117
118  def replaceWrapper(v: UInt, lruIdx: UInt): UInt = {
119    val width = v.getWidth
120    val emptyIdx = ParallelPriorityMux((0 until width).map( i => (!v(i), i.U)))
121    val full = Cat(v).andR
122    Mux(full, lruIdx, emptyIdx)
123  }
124
125  def replaceWrapper(v: Seq[Bool], lruIdx: UInt): UInt = {
126    replaceWrapper(VecInit(v).asUInt, lruIdx)
127  }
128
129}
130
131trait HasPtwConst extends HasTlbConst with MemoryOpConstants{
132  val PtwWidth = 2
133  val sourceWidth = { if (l2tlbParams.enablePrefetch) PtwWidth + 1 else PtwWidth}
134  val prefetchID = PtwWidth
135  val maxPrefetchNum = l2tlbParams.filterSize
136
137  val blockBits = l2tlbParams.blockBytes * 8
138
139  val bPtwWidth = log2Up(PtwWidth)
140  val bSourceWidth = log2Up(sourceWidth)
141  // ptwl1: fully-associated
142  val PtwL1TagLen = vpnnLen
143
144  /* +-------+----------+-------------+
145   * |  Tag  |  SetIdx  |  SectorIdx  |
146   * +-------+----------+-------------+
147   */
148  // ptwl2: 8-way group-associated
149  val l2tlbParams.l2nWays = l2tlbParams.l2nWays
150  val PtwL2SetNum = l2tlbParams.l2nSets
151  val PtwL2SectorSize = blockBits /XLEN
152  val PtwL2IdxLen = log2Up(PtwL2SetNum * PtwL2SectorSize)
153  val PtwL2SectorIdxLen = log2Up(PtwL2SectorSize)
154  val PtwL2SetIdxLen = log2Up(PtwL2SetNum)
155  val PtwL2TagLen = vpnnLen * 2 - PtwL2IdxLen
156
157  // ptwl3: 16-way group-associated
158  val l2tlbParams.l3nWays = l2tlbParams.l3nWays
159  val PtwL3SetNum = l2tlbParams.l3nSets
160  val PtwL3SectorSize =  blockBits / XLEN
161  val PtwL3IdxLen = log2Up(PtwL3SetNum * PtwL3SectorSize)
162  val PtwL3SectorIdxLen = log2Up(PtwL3SectorSize)
163  val PtwL3SetIdxLen = log2Up(PtwL3SetNum)
164  val PtwL3TagLen = vpnnLen * 3 - PtwL3IdxLen
165
166  // super page, including 1GB and 2MB page
167  val SPTagLen = vpnnLen * 2
168
169  // miss queue
170  val MSHRBaseSize = 1 + l2tlbParams.filterSize + l2tlbParams.missqueueExtendSize
171  val MSHRSize =  { if (l2tlbParams.enablePrefetch) (MSHRBaseSize + 1) else MSHRBaseSize }
172  val MemReqWidth = l2tlbParams.llptwsize + 1
173  val FsmReqID = l2tlbParams.llptwsize
174  val bMemID = log2Up(MemReqWidth)
175
176  def genPtwL2Idx(vpn: UInt) = {
177    (vpn(vpnLen - 1, vpnnLen))(PtwL2IdxLen - 1, 0)
178  }
179
180  def genPtwL2SectorIdx(vpn: UInt) = {
181    genPtwL2Idx(vpn)(PtwL2SectorIdxLen - 1, 0)
182  }
183
184  def genPtwL2SetIdx(vpn: UInt) = {
185    genPtwL2Idx(vpn)(PtwL2SetIdxLen + PtwL2SectorIdxLen - 1, PtwL2SectorIdxLen)
186  }
187
188  def genPtwL3Idx(vpn: UInt) = {
189    vpn(PtwL3IdxLen - 1, 0)
190  }
191
192  def genPtwL3SectorIdx(vpn: UInt) = {
193    genPtwL3Idx(vpn)(PtwL3SectorIdxLen - 1, 0)
194  }
195
196  def dropL3SectorBits(vpn: UInt) = {
197    vpn(vpn.getWidth-1, PtwL3SectorIdxLen)
198  }
199
200  def genPtwL3SetIdx(vpn: UInt) = {
201    genPtwL3Idx(vpn)(PtwL3SetIdxLen + PtwL3SectorIdxLen - 1, PtwL3SectorIdxLen)
202  }
203
204  def MakeAddr(ppn: UInt, off: UInt) = {
205    require(off.getWidth == 9)
206    Cat(ppn, off, 0.U(log2Up(XLEN/8).W))(PAddrBits-1, 0)
207  }
208
209  def getVpnn(vpn: UInt, idx: Int): UInt = {
210    vpn(vpnnLen*(idx+1)-1, vpnnLen*idx)
211  }
212
213  def getVpnClip(vpn: UInt, level: Int) = {
214    // level 0  /* vpnn2 */
215    // level 1  /* vpnn2 * vpnn1 */
216    // level 2  /* vpnn2 * vpnn1 * vpnn0*/
217    vpn(vpnLen - 1, (2 - level) * vpnnLen)
218  }
219
220  def get_next_line(vpn: UInt) = {
221    Cat(dropL3SectorBits(vpn) + 1.U, 0.U(PtwL3SectorIdxLen.W))
222  }
223
224  def same_l2entry(vpn1: UInt, vpn2: UInt) = {
225    vpn1(vpnLen-1, vpnnLen) === vpn2(vpnLen-1, vpnnLen)
226  }
227
228  def from_pre(source: UInt) = {
229    (source === prefetchID.U)
230  }
231
232  def printVec[T <: Data](x: Seq[T]): Printable = {
233    (0 until x.length).map(i => p"(${i.U})${x(i)} ").reduce(_+_)
234  }
235}
236