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