xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/MMUConst.scala (revision d29457077dba131b5b0f793bbf7a71463640ac2a)
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 org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import xiangshan._
23import xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
24import utils._
25import utility._
26import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
27import freechips.rocketchip.tilelink._
28
29
30case class TLBParameters
31(
32  name: String = "none",
33  fetchi: Boolean = false, // TODO: remove it
34  fenceDelay: Int = 2,
35  useDmode: Boolean = true,
36  NSets: Int = 1,
37  NWays: Int = 2,
38  Replacer: Option[String] = Some("plru"),
39  Associative: String = "fa", // must be fa
40  outReplace: Boolean = false,
41  partialStaticPMP: Boolean = false, // partial static pmp result stored in entries
42  outsideRecvFlush: Boolean = false, // if outside moudle waiting for tlb recv flush pipe
43  saveLevel: Boolean = false
44)
45
46case class L2TLBParameters
47(
48  name: String = "l2tlb",
49  // l1
50  l1Size: Int = 16,
51  l1Associative: String = "fa",
52  l1Replacer: Option[String] = Some("plru"),
53  // l2
54  l2nSets: Int = 8,
55  l2nWays: Int = 4,
56  l2Replacer: Option[String] = Some("setplru"),
57  // l3
58  l3nSets: Int = 32,
59  l3nWays: Int = 8,
60  l3Replacer: Option[String] = Some("setplru"),
61  // sp
62  spSize: Int = 16,
63  spReplacer: Option[String] = Some("plru"),
64  // filter
65  ifilterSize: Int = 8,
66  dfilterSize: Int = 32,
67  // miss queue, add more entries than 'must require'
68  // 0 for easier bug trigger, please set as big as u can, 8 maybe
69  missqueueExtendSize: Int = 0,
70  // llptw
71  llptwsize: Int = 6,
72  // way size
73  blockBytes: Int = 64,
74  // prefetch
75  enablePrefetch: Boolean = true,
76  // ecc
77  ecc: Option[String] = Some("secded")
78)
79
80trait HasTlbConst extends HasXSParameter {
81  val Level = 3
82
83  val offLen  = 12
84  val ppnLen  = PAddrBits - offLen
85  val vpnnLen = 9
86  val vpnLen  = VAddrBits - offLen
87  val flagLen = 8
88  val pteResLen = XLEN - 44 - 2 - flagLen
89  val ppnHignLen = 44 - ppnLen
90
91  val tlbcontiguous = 8
92  val sectortlbwidth = log2Up(tlbcontiguous)
93  val sectorppnLen = ppnLen - sectortlbwidth
94  val sectorvpnLen = vpnLen - sectortlbwidth
95
96  val loadfiltersize = 16
97  val storefiltersize = 8
98  val prefetchfiltersize = 8
99
100  val sramSinglePort = true
101
102  val timeOutThreshold = 10000
103
104  def get_pn(addr: UInt) = {
105    require(addr.getWidth > offLen)
106    addr(addr.getWidth-1, offLen)
107  }
108  def get_off(addr: UInt) = {
109    require(addr.getWidth > offLen)
110    addr(offLen-1, 0)
111  }
112
113  def get_set_idx(vpn: UInt, nSets: Int): UInt = {
114    require(nSets >= 1)
115    vpn(log2Up(nSets)-1, 0)
116  }
117
118  def drop_set_idx(vpn: UInt, nSets: Int): UInt = {
119    require(nSets >= 1)
120    require(vpn.getWidth > log2Ceil(nSets))
121    vpn(vpn.getWidth-1, log2Ceil(nSets))
122  }
123
124  def drop_set_equal(vpn1: UInt, vpn2: UInt, nSets: Int): Bool = {
125    require(nSets >= 1)
126    require(vpn1.getWidth == vpn2.getWidth)
127    if (vpn1.getWidth <= log2Ceil(nSets)) {
128      true.B
129    } else {
130      drop_set_idx(vpn1, nSets) === drop_set_idx(vpn2, nSets)
131    }
132  }
133
134  def replaceWrapper(v: UInt, lruIdx: UInt): UInt = {
135    val width = v.getWidth
136    val emptyIdx = ParallelPriorityMux((0 until width).map( i => (!v(i), i.U(log2Up(width).W))))
137    val full = Cat(v).andR
138    Mux(full, lruIdx, emptyIdx)
139  }
140
141  def replaceWrapper(v: Seq[Bool], lruIdx: UInt): UInt = {
142    replaceWrapper(VecInit(v).asUInt, lruIdx)
143  }
144
145  implicit def ptwresp_to_tlbperm(ptwResp: PtwSectorRespwithMemIdx): TlbPermBundle = {
146    val tp = Wire(new TlbPermBundle)
147    val ptePerm = ptwResp.entry.perm.get.asTypeOf(new PtePermBundle().cloneType)
148    tp.pf := ptwResp.pf
149    tp.af := ptwResp.af
150    tp.d := ptePerm.d
151    tp.a := ptePerm.a
152    tp.g := ptePerm.g
153    tp.u := ptePerm.u
154    tp.x := ptePerm.x
155    tp.w := ptePerm.w
156    tp.r := ptePerm.r
157    tp
158  }
159}
160
161trait HasPtwConst extends HasTlbConst with MemoryOpConstants{
162  val PtwWidth = 2
163  val sourceWidth = { if (l2tlbParams.enablePrefetch) PtwWidth + 1 else PtwWidth}
164  val prefetchID = PtwWidth
165
166  val blockBits = l2tlbParams.blockBytes * 8
167
168  val bPtwWidth = log2Up(PtwWidth)
169  val bSourceWidth = log2Up(sourceWidth)
170  // ptwl1: fully-associated
171  val PtwL1TagLen = vpnnLen
172
173  /* +-------+----------+-------------+
174   * |  Tag  |  SetIdx  |  SectorIdx  |
175   * +-------+----------+-------------+
176   */
177  // ptwl2: 8-way group-associated
178  val l2tlbParams.l2nWays = l2tlbParams.l2nWays
179  val PtwL2SetNum = l2tlbParams.l2nSets
180  val PtwL2SectorSize = blockBits / XLEN
181  val PtwL2IdxLen = log2Up(PtwL2SetNum * PtwL2SectorSize)
182  val PtwL2SectorIdxLen = log2Up(PtwL2SectorSize)
183  val PtwL2SetIdxLen = log2Up(PtwL2SetNum)
184  val PtwL2TagLen = vpnnLen * 2 - PtwL2IdxLen
185
186  // ptwl3: 16-way group-associated
187  val l2tlbParams.l3nWays = l2tlbParams.l3nWays
188  val PtwL3SetNum = l2tlbParams.l3nSets
189  val PtwL3SectorSize =  blockBits / XLEN
190  val PtwL3IdxLen = log2Up(PtwL3SetNum * PtwL3SectorSize)
191  val PtwL3SectorIdxLen = log2Up(PtwL3SectorSize)
192  val PtwL3SetIdxLen = log2Up(PtwL3SetNum)
193  val PtwL3TagLen = vpnnLen * 3 - PtwL3IdxLen
194
195  // super page, including 1GB and 2MB page
196  val SPTagLen = vpnnLen * 2
197
198  // miss queue
199  val MissQueueSize = l2tlbParams.ifilterSize + l2tlbParams.dfilterSize
200  val MemReqWidth = l2tlbParams.llptwsize + 1
201  val FsmReqID = l2tlbParams.llptwsize
202  val bMemID = log2Up(MemReqWidth)
203
204  def genPtwL2Idx(vpn: UInt) = {
205    (vpn(vpnLen - 1, vpnnLen))(PtwL2IdxLen - 1, 0)
206  }
207
208  def genPtwL2SectorIdx(vpn: UInt) = {
209    genPtwL2Idx(vpn)(PtwL2SectorIdxLen - 1, 0)
210  }
211
212  def genPtwL2SetIdx(vpn: UInt) = {
213    genPtwL2Idx(vpn)(PtwL2SetIdxLen + PtwL2SectorIdxLen - 1, PtwL2SectorIdxLen)
214  }
215
216  def genPtwL3Idx(vpn: UInt) = {
217    vpn(PtwL3IdxLen - 1, 0)
218  }
219
220  def genPtwL3SectorIdx(vpn: UInt) = {
221    genPtwL3Idx(vpn)(PtwL3SectorIdxLen - 1, 0)
222  }
223
224  def dropL3SectorBits(vpn: UInt) = {
225    vpn(vpn.getWidth-1, PtwL3SectorIdxLen)
226  }
227
228  def genPtwL3SetIdx(vpn: UInt) = {
229    genPtwL3Idx(vpn)(PtwL3SetIdxLen + PtwL3SectorIdxLen - 1, PtwL3SectorIdxLen)
230  }
231
232  def MakeAddr(ppn: UInt, off: UInt) = {
233    require(off.getWidth == 9)
234    Cat(ppn, off, 0.U(log2Up(XLEN/8).W))(PAddrBits-1, 0)
235  }
236
237  def getVpnn(vpn: UInt, idx: Int): UInt = {
238    vpn(vpnnLen*(idx+1)-1, vpnnLen*idx)
239  }
240
241  def getVpnClip(vpn: UInt, level: Int) = {
242    // level 0  /* vpnn2 */
243    // level 1  /* vpnn2 * vpnn1 */
244    // level 2  /* vpnn2 * vpnn1 * vpnn0*/
245    vpn(vpnLen - 1, (2 - level) * vpnnLen)
246  }
247
248  def get_next_line(vpn: UInt) = {
249    Cat(dropL3SectorBits(vpn) + 1.U, 0.U(PtwL3SectorIdxLen.W))
250  }
251
252  def same_l2entry(vpn1: UInt, vpn2: UInt) = {
253    vpn1(vpnLen-1, vpnnLen) === vpn2(vpnLen-1, vpnnLen)
254  }
255
256  def from_pre(source: UInt) = {
257    (source === prefetchID.U)
258  }
259
260  def sel_data(data: UInt, index: UInt): UInt = {
261    val inner_data = data.asTypeOf(Vec(data.getWidth / XLEN, UInt(XLEN.W)))
262    inner_data(index)
263  }
264
265  // vpn1 and vpn2 is at same cacheline
266  def dup(vpn1: UInt, vpn2: UInt): Bool = {
267    dropL3SectorBits(vpn1) === dropL3SectorBits(vpn2)
268  }
269
270
271  def printVec[T <: Data](x: Seq[T]): Printable = {
272    (0 until x.length).map(i => p"(${i.U})${x(i)} ").reduce(_+_)
273  }
274}
275