xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/MMUBundle.scala (revision 20156f774775cea2950c4a680f68fd44c1aaa0e3)
1/***************************************************************************************
2* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC)
3* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences
4* Copyright (c) 2020-2021 Peng Cheng Laboratory
5*
6* XiangShan is licensed under Mulan PSL v2.
7* You can use this software according to the terms and conditions of the Mulan PSL v2.
8* You may obtain a copy of Mulan PSL v2 at:
9*          http://license.coscl.org.cn/MulanPSL2
10*
11* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
12* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
13* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
14*
15* See the Mulan PSL v2 for more details.
16***************************************************************************************/
17
18package xiangshan.cache.mmu
19
20import org.chipsalliance.cde.config.Parameters
21import chisel3._
22import chisel3.util._
23import xiangshan._
24import utils._
25import utility._
26import xiangshan.backend.rob.RobPtr
27import xiangshan.backend.fu.util.HasCSRConst
28import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
29import freechips.rocketchip.tilelink._
30import xiangshan.backend.fu.{PMPReqBundle, PMPConfig}
31import xiangshan.backend.fu.PMPBundle
32
33
34abstract class TlbBundle(implicit p: Parameters) extends XSBundle with HasTlbConst
35abstract class TlbModule(implicit p: Parameters) extends XSModule with HasTlbConst
36
37
38class PtePermBundle(implicit p: Parameters) extends TlbBundle {
39  val d = Bool()
40  val a = Bool()
41  val g = Bool()
42  val u = Bool()
43  val x = Bool()
44  val w = Bool()
45  val r = Bool()
46
47  override def toPrintable: Printable = {
48    p"d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"// +
49    //(if(hasV) (p"v:${v}") else p"")
50  }
51}
52
53class TlbPMBundle(implicit p: Parameters) extends TlbBundle {
54  val r = Bool()
55  val w = Bool()
56  val x = Bool()
57  val c = Bool()
58  val atomic = Bool()
59
60  def assign_ap(pm: PMPConfig) = {
61    r := pm.r
62    w := pm.w
63    x := pm.x
64    c := pm.c
65    atomic := pm.atomic
66  }
67}
68
69class TlbPermBundle(implicit p: Parameters) extends TlbBundle {
70  val pf = Bool() // NOTE: if this is true, just raise pf
71  val af = Bool() // NOTE: if this is true, just raise af
72  val v = Bool() // if stage1 pte is fake_pte, v is false
73  // pagetable perm (software defined)
74  val d = Bool()
75  val a = Bool()
76  val g = Bool()
77  val u = Bool()
78  val x = Bool()
79  val w = Bool()
80  val r = Bool()
81
82  def apply(item: PtwSectorResp) = {
83    val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType)
84    this.pf := item.pf
85    this.af := item.af
86    this.v := item.v
87    this.d := ptePerm.d
88    this.a := ptePerm.a
89    this.g := ptePerm.g
90    this.u := ptePerm.u
91    this.x := ptePerm.x
92    this.w := ptePerm.w
93    this.r := ptePerm.r
94
95    this
96  }
97
98  def applyS2(item: HptwResp) = {
99    val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType)
100    this.pf := item.gpf
101    this.af := item.gaf
102    this.v := DontCare
103    this.d := ptePerm.d
104    this.a := ptePerm.a
105    this.g := ptePerm.g
106    this.u := ptePerm.u
107    this.x := ptePerm.x
108    this.w := ptePerm.w
109    this.r := ptePerm.r
110
111    this
112  }
113
114  override def toPrintable: Printable = {
115    p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} "
116  }
117}
118
119class TlbSectorPermBundle(implicit p: Parameters) extends TlbBundle {
120  val pf = Bool() // NOTE: if this is true, just raise pf
121  val af = Bool() // NOTE: if this is true, just raise af
122  val v = Bool() // if stage1 pte is fake_pte, v is false
123  // pagetable perm (software defined)
124  val d = Bool()
125  val a = Bool()
126  val g = Bool()
127  val u = Bool()
128  val x = Bool()
129  val w = Bool()
130  val r = Bool()
131
132  def apply(item: PtwSectorResp) = {
133    val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType)
134    this.pf := item.pf
135    this.af := item.af
136    this.v := item.v
137    this.d := ptePerm.d
138    this.a := ptePerm.a
139    this.g := ptePerm.g
140    this.u := ptePerm.u
141    this.x := ptePerm.x
142    this.w := ptePerm.w
143    this.r := ptePerm.r
144
145    this
146  }
147  override def toPrintable: Printable = {
148    p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} "
149  }
150}
151
152// multi-read && single-write
153// input is data, output is hot-code(not one-hot)
154class CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule {
155  val io = IO(new Bundle {
156    val r = new Bundle {
157      val req = Input(Vec(readWidth, gen))
158      val resp = Output(Vec(readWidth, Vec(set, Bool())))
159    }
160    val w = Input(new Bundle {
161      val valid = Bool()
162      val bits = new Bundle {
163        val index = UInt(log2Up(set).W)
164        val data = gen
165      }
166    })
167  })
168
169  val wordType = UInt(gen.getWidth.W)
170  val array = Reg(Vec(set, wordType))
171
172  io.r.resp.zipWithIndex.map{ case (a,i) =>
173    a := array.map(io.r.req(i).asUInt === _)
174  }
175
176  when (io.w.valid) {
177    array(io.w.bits.index) := io.w.bits.data.asUInt
178  }
179}
180
181class TlbSectorEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle {
182  require(pageNormal && pageSuper)
183
184  val tag = UInt(sectorvpnLen.W)
185  val asid = UInt(asidLen.W)
186  /* level, 11: 512GB size page(only for sv48)
187            10: 1GB size page
188            01: 2MB size page
189            00: 4KB size page
190     future sv57 extension should change level width
191  */
192  val level = Some(UInt(2.W))
193  val ppn = UInt(sectorppnLen.W)
194  val pbmt = UInt(ptePbmtLen.W)
195  val g_pbmt = UInt(ptePbmtLen.W)
196  val perm = new TlbSectorPermBundle
197  val valididx = Vec(tlbcontiguous, Bool())
198  val pteidx = Vec(tlbcontiguous, Bool())
199  val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W))
200
201  val g_perm = new TlbPermBundle
202  val vmid = UInt(vmidLen.W)
203  val s2xlate = UInt(2.W)
204
205
206  /** level usage:
207   *  !PageSuper: page is only normal, level is None, match all the tag
208   *  !PageNormal: page is only super, level is a Bool(), match high 9*2 parts
209   *  bits0  0: need mid 9bits
210   *         1: no need mid 9bits
211   *  PageSuper && PageNormal: page hold all the three type,
212   *  bits0  0: need low 9bits
213   *  bits1  0: need mid 9bits
214   */
215
216  def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false, vmid: UInt, hasS2xlate: Bool, onlyS2: Bool = false.B, onlyS1: Bool = false.B): Bool = {
217    val asid_hit = Mux(hasS2xlate && onlyS2, true.B, if (ignoreAsid) true.B else (this.asid === asid))
218    val addr_low_hit = valididx(vpn(2, 0))
219    val vmid_hit = Mux(hasS2xlate, this.vmid === vmid, true.B)
220    val isPageSuper = !(level.getOrElse(0.U) === 0.U)
221    val pteidx_hit = Mux(hasS2xlate && !isPageSuper && !onlyS1, pteidx(vpn(2, 0)), true.B)
222
223    val tmp_level = level.get
224    val tag_matchs = Wire(Vec(Level + 1, Bool()))
225    tag_matchs(0) := tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth)
226    for (i <- 1 until Level) {
227      tag_matchs(i) := tag(vpnnLen * (i + 1) - sectortlbwidth - 1, vpnnLen * i - sectortlbwidth) === vpn(vpnnLen * (i + 1) - 1, vpnnLen * i)
228    }
229    tag_matchs(Level) := tag(sectorvpnLen - 1, vpnnLen * Level - sectortlbwidth) === vpn(vpnLen - 1, vpnnLen * Level)
230    val level_matchs = Wire(Vec(Level + 1, Bool()))
231    for (i <- 0 until Level) {
232      level_matchs(i) := tag_matchs(i) || tmp_level >= (i + 1).U
233    }
234    level_matchs(Level) := tag_matchs(Level)
235
236    asid_hit && level_matchs.asUInt.andR && addr_low_hit && vmid_hit && pteidx_hit
237  }
238
239  def wbhit(data: PtwRespS2, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false, s2xlate: UInt): Bool = {
240    val s1vpn = data.s1.entry.tag
241    val s2vpn = data.s2.entry.tag(vpnLen - 1, sectortlbwidth)
242    val wb_vpn = Mux(s2xlate === onlyStage2, s2vpn, s1vpn)
243    val vpn = Cat(wb_vpn, 0.U(sectortlbwidth.W))
244    val asid_hit = if (ignoreAsid) true.B else (this.asid === asid)
245    val vpn_hit = Wire(Bool())
246    val index_hit = Wire(Vec(tlbcontiguous, Bool()))
247    val wb_valididx = Wire(Vec(tlbcontiguous, Bool()))
248    val hasS2xlate = this.s2xlate =/= noS2xlate
249    val onlyS1 = this.s2xlate === onlyStage1
250    val onlyS2 = this.s2xlate === onlyStage2
251    val pteidx_hit = MuxCase(true.B, Seq(
252      onlyS2 -> (VecInit(UIntToOH(data.s2.entry.tag(sectortlbwidth - 1, 0))).asUInt === pteidx.asUInt),
253      hasS2xlate -> (pteidx.asUInt === data.s1.pteidx.asUInt)
254    ))
255    wb_valididx := Mux(s2xlate === onlyStage2, VecInit(UIntToOH(data.s2.entry.tag(sectortlbwidth - 1, 0)).asBools), data.s1.valididx)
256    val s2xlate_hit = s2xlate === this.s2xlate
257
258    val tmp_level = level.get
259    val tag_matchs = Wire(Vec(Level + 1, Bool()))
260    tag_matchs(0) := tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth)
261    for (i <- 1 until Level) {
262      tag_matchs(i) := tag(vpnnLen * (i + 1) - sectortlbwidth - 1, vpnnLen * i - sectortlbwidth) === vpn(vpnnLen * (i + 1) - 1, vpnnLen * i)
263    }
264    tag_matchs(Level) := tag(sectorvpnLen - 1, vpnnLen * Level - sectortlbwidth) === vpn(vpnLen - 1, vpnnLen * Level)
265    val level_matchs = Wire(Vec(Level + 1, Bool()))
266    for (i <- 0 until Level) {
267      level_matchs(i) := tag_matchs(i) || tmp_level >= (i + 1).U
268    }
269    level_matchs(Level) := tag_matchs(Level)
270    vpn_hit := asid_hit && level_matchs.asUInt.andR
271
272    for (i <- 0 until tlbcontiguous) {
273      index_hit(i) := wb_valididx(i) && valididx(i)
274    }
275
276    // For example, tlb req to page cache with vpn 0x10
277    // At this time, 0x13 has not been paged, so page cache only resp 0x10
278    // When 0x13 refill to page cache, previous item will be flushed
279    // Now 0x10 and 0x13 are both valid in page cache
280    // However, when 0x13 refill to tlb, will trigger multi hit
281    // So will only trigger multi-hit when PopCount(data.valididx) = 1
282    vpn_hit && index_hit.reduce(_ || _) && PopCount(wb_valididx) === 1.U && s2xlate_hit && pteidx_hit
283  }
284
285  def apply(item: PtwRespS2): TlbSectorEntry = {
286    this.asid := item.s1.entry.asid
287    val inner_level = MuxLookup(item.s2xlate, 2.U)(Seq(
288      onlyStage1 -> item.s1.entry.level.getOrElse(0.U),
289      onlyStage2 -> item.s2.entry.level.getOrElse(0.U),
290      allStage -> (item.s1.entry.level.getOrElse(0.U) min item.s2.entry.level.getOrElse(0.U)),
291      noS2xlate -> item.s1.entry.level.getOrElse(0.U)
292    ))
293    this.level.map(_ := inner_level)
294    this.perm.apply(item.s1)
295    this.pbmt := item.s1.entry.pbmt
296
297    val s1tag = item.s1.entry.tag
298    val s2tag = item.s2.entry.tag(gvpnLen - 1, sectortlbwidth)
299    // if stage1 page is larger than stage2 page, need to merge s1tag and s2tag.
300    val s1tagFix = MuxCase(s1tag, Seq(
301      (item.s1.entry.level.getOrElse(0.U) === 3.U && item.s2.entry.level.getOrElse(0.U) === 2.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 3 - 1, vpnnLen * 2), 0.U((vpnnLen * 2 - sectortlbwidth).W)),
302      (item.s1.entry.level.getOrElse(0.U) === 3.U && item.s2.entry.level.getOrElse(0.U) === 1.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 3 - 1, vpnnLen), 0.U((vpnnLen - sectortlbwidth).W)),
303      (item.s1.entry.level.getOrElse(0.U) === 3.U && item.s2.entry.level.getOrElse(0.U) === 0.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 3 - 1, sectortlbwidth)),
304      (item.s1.entry.level.getOrElse(0.U) === 2.U && item.s2.entry.level.getOrElse(0.U) === 1.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 2 - 1, vpnnLen), 0.U((vpnnLen - sectortlbwidth).W)),
305      (item.s1.entry.level.getOrElse(0.U) === 2.U && item.s2.entry.level.getOrElse(0.U) === 0.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 2 - 1, sectortlbwidth)),
306      (item.s1.entry.level.getOrElse(0.U) === 1.U && item.s2.entry.level.getOrElse(0.U) === 0.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth), item.s2.entry.tag(vpnnLen - 1, sectortlbwidth))
307    ))
308    this.tag := Mux(item.s2xlate === onlyStage2, s2tag, Mux(item.s2xlate === allStage, s1tagFix, s1tag))
309    val s2page_pageSuper = item.s2.entry.level.getOrElse(0.U) =/= 0.U
310    this.pteidx := Mux(item.s2xlate === onlyStage2, VecInit(UIntToOH(item.s2.entry.tag(sectortlbwidth - 1, 0)).asBools),  item.s1.pteidx)
311    val s2_valid = Mux(s2page_pageSuper, VecInit(Seq.fill(tlbcontiguous)(true.B)), VecInit(UIntToOH(item.s2.entry.tag(sectortlbwidth - 1, 0)).asBools))
312    this.valididx := Mux(item.s2xlate === onlyStage2, s2_valid, item.s1.valididx)
313    // if stage2 page is larger than stage1 page, need to merge s2tag and s2ppn to get a new s2ppn.
314    val s1ppn = item.s1.entry.ppn
315    val s1ppn_low = item.s1.ppn_low
316    val s2ppn = MuxLookup(item.s2.entry.level.getOrElse(0.U), item.s2.entry.ppn(ppnLen - 1, sectortlbwidth))(Seq(
317      3.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 3), item.s2.entry.tag(vpnnLen * 3 - 1, sectortlbwidth)),
318      2.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 2), item.s2.entry.tag(vpnnLen * 2 - 1, sectortlbwidth)),
319      1.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen), item.s2.entry.tag(vpnnLen - 1, sectortlbwidth))
320    ))
321    val s2ppn_tmp = MuxLookup(item.s2.entry.level.getOrElse(0.U), item.s2.entry.ppn(ppnLen - 1, 0))(Seq(
322      3.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 3), item.s2.entry.tag(vpnnLen * 3 - 1, 0)),
323      2.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 2), item.s2.entry.tag(vpnnLen * 2 - 1, 0)),
324      1.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen), item.s2.entry.tag(vpnnLen - 1, 0))
325    ))
326    val s2ppn_low = VecInit(Seq.fill(tlbcontiguous)(s2ppn_tmp(sectortlbwidth - 1, 0)))
327    this.ppn := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, s1ppn, s2ppn)
328    this.ppn_low := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, s1ppn_low, s2ppn_low)
329    this.vmid := item.s1.entry.vmid.getOrElse(0.U)
330    this.g_pbmt := item.s2.entry.pbmt
331    this.g_perm.applyS2(item.s2)
332    this.s2xlate := item.s2xlate
333    this
334  }
335
336  // 4KB is normal entry, 2MB/1GB is considered as super entry
337  def is_normalentry(): Bool = {
338    if (!pageSuper) { true.B }
339    else if (!pageNormal) { false.B }
340    else { level.get === 0.U }
341  }
342
343
344  def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = {
345    val inner_level = level.getOrElse(0.U)
346    val ppn_res = Cat(ppn(sectorppnLen - 1, vpnnLen * 3 - sectortlbwidth),
347      Mux(inner_level >= "b11".U , vpn(vpnnLen * 3 - 1, vpnnLen * 2), ppn(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth)),
348      Mux(inner_level >= "b10".U , vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth)),
349      Mux(inner_level >= "b01".U , vpn(vpnnLen - 1, 0), Cat(ppn(vpnnLen - sectortlbwidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0)))))
350
351    if (saveLevel)
352      RegEnable(ppn_res, valid)
353    else
354      ppn_res
355  }
356
357  def hasS2xlate(): Bool = {
358    this.s2xlate =/= noS2xlate
359  }
360
361  override def toPrintable: Printable = {
362    val inner_level = level.getOrElse(2.U)
363    p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}"
364  }
365
366}
367
368object TlbCmd {
369  def read  = "b00".U
370  def write = "b01".U
371  def exec  = "b10".U
372
373  def atom_read  = "b100".U // lr
374  def atom_write = "b101".U // sc / amo
375
376  def apply() = UInt(3.W)
377  def isRead(a: UInt) = a(1,0)===read
378  def isWrite(a: UInt) = a(1,0)===write
379  def isExec(a: UInt) = a(1,0)===exec
380
381  def isAtom(a: UInt) = a(2)
382  def isAmo(a: UInt) = a===atom_write // NOTE: sc mixed
383}
384
385// Svpbmt extension
386object Pbmt {
387  def pma:  UInt = "b00".U  // None
388  def nc:   UInt = "b01".U  // Non-cacheable, idempotent, weakly-ordered (RVWMO), main memory
389  def io:   UInt = "b10".U  // Non-cacheable, non-idempotent, strongly-ordered (I/O ordering), I/O
390  def rsvd: UInt = "b11".U  // Reserved for future standard use
391  def width: Int = 2
392
393  def apply() = UInt(2.W)
394  def isUncache(a: UInt) = a===nc || a===io
395}
396
397class TlbStorageIO(nSets: Int, nWays: Int, ports: Int, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle {
398  val r = new Bundle {
399    val req = Vec(ports, Flipped(DecoupledIO(new Bundle {
400      val vpn = Output(UInt(vpnLen.W))
401      val s2xlate = Output(UInt(2.W))
402    })))
403    val resp = Vec(ports, ValidIO(new Bundle{
404      val hit = Output(Bool())
405      val ppn = Vec(nDups, Output(UInt(ppnLen.W)))
406      val pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W)))
407      val g_pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W)))
408      val perm = Vec(nDups, Output(new TlbSectorPermBundle()))
409      val g_perm = Vec(nDups, Output(new TlbPermBundle()))
410      val s2xlate = Vec(nDups, Output(UInt(2.W)))
411    }))
412  }
413  val w = Flipped(ValidIO(new Bundle {
414    val wayIdx = Output(UInt(log2Up(nWays).W))
415    val data = Output(new PtwRespS2)
416  }))
417  val access = Vec(ports, new ReplaceAccessBundle(nSets, nWays))
418
419  def r_req_apply(valid: Bool, vpn: UInt, i: Int, s2xlate:UInt): Unit = {
420    this.r.req(i).valid := valid
421    this.r.req(i).bits.vpn := vpn
422    this.r.req(i).bits.s2xlate := s2xlate
423
424  }
425
426  def r_resp_apply(i: Int) = {
427    (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, this.r.resp(i).bits.g_perm, this.r.resp(i).bits.pbmt, this.r.resp(i).bits.g_pbmt)
428  }
429
430  def w_apply(valid: Bool, wayIdx: UInt, data: PtwRespS2): Unit = {
431    this.w.valid := valid
432    this.w.bits.wayIdx := wayIdx
433    this.w.bits.data := data
434  }
435
436}
437
438class TlbStorageWrapperIO(ports: Int, q: TLBParameters, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle {
439  val r = new Bundle {
440    val req = Vec(ports, Flipped(DecoupledIO(new Bundle {
441      val vpn = Output(UInt(vpnLen.W))
442      val s2xlate = Output(UInt(2.W))
443    })))
444    val resp = Vec(ports, ValidIO(new Bundle{
445      val hit = Output(Bool())
446      val ppn = Vec(nDups, Output(UInt(ppnLen.W)))
447      val pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W)))
448      val g_pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W)))
449      val perm = Vec(nDups, Output(new TlbPermBundle()))
450      val g_perm = Vec(nDups, Output(new TlbPermBundle()))
451      val s2xlate = Vec(nDups, Output(UInt(2.W)))
452    }))
453  }
454  val w = Flipped(ValidIO(new Bundle {
455    val data = Output(new PtwRespS2)
456  }))
457  val replace = if (q.outReplace) Flipped(new TlbReplaceIO(ports, q)) else null
458
459  def r_req_apply(valid: Bool, vpn: UInt, i: Int, s2xlate: UInt): Unit = {
460    this.r.req(i).valid := valid
461    this.r.req(i).bits.vpn := vpn
462    this.r.req(i).bits.s2xlate := s2xlate
463  }
464
465  def r_resp_apply(i: Int) = {
466    (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, this.r.resp(i).bits.g_perm, this.r.resp(i).bits.s2xlate, this.r.resp(i).bits.pbmt, this.r.resp(i).bits.g_pbmt)
467  }
468
469  def w_apply(valid: Bool, data: PtwRespS2): Unit = {
470    this.w.valid := valid
471    this.w.bits.data := data
472  }
473}
474
475class ReplaceAccessBundle(nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle {
476  val sets = Output(UInt(log2Up(nSets).W))
477  val touch_ways = ValidIO(Output(UInt(log2Up(nWays).W)))
478}
479
480class ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle {
481  val access = Vec(Width, Flipped(new ReplaceAccessBundle(nSets, nWays)))
482
483  val refillIdx = Output(UInt(log2Up(nWays).W))
484  val chosen_set = Flipped(Output(UInt(log2Up(nSets).W)))
485
486  def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = {
487    for ((ac_rep, ac_tlb) <- access.zip(in.map(a => a.access.map(b => b)).flatten)) {
488      ac_rep := ac_tlb
489    }
490    this.chosen_set := get_set_idx(vpn, nSets)
491    in.map(a => a.refillIdx := this.refillIdx)
492  }
493}
494
495class TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends
496  TlbBundle {
497  val page = new ReplaceIO(Width, q.NSets, q.NWays)
498
499  def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = {
500    this.page.apply_sep(in.map(_.page), vpn)
501  }
502
503}
504
505class MemBlockidxBundle(implicit p: Parameters) extends TlbBundle {
506  val is_ld = Bool()
507  val is_st = Bool()
508  val idx = UInt(log2Ceil(VirtualLoadQueueMaxStoreQueueSize).W)
509}
510
511class TlbReq(implicit p: Parameters) extends TlbBundle {
512  val vaddr = Output(UInt(VAddrBits.W))
513  val cmd = Output(TlbCmd())
514  val hyperinst = Output(Bool())
515  val hlvx = Output(Bool())
516  val size = Output(UInt(log2Ceil(log2Ceil(VLEN/8)+1).W))
517  val kill = Output(Bool()) // Use for blocked tlb that need sync with other module like icache
518  val memidx = Output(new MemBlockidxBundle)
519  // do not translate, but still do pmp/pma check
520  val no_translate = Output(Bool())
521  val pmp_addr = Output(UInt(PAddrBits.W)) // load s1 send prefetch paddr
522  val debug = new Bundle {
523    val pc = Output(UInt(XLEN.W))
524    val robIdx = Output(new RobPtr)
525    val isFirstIssue = Output(Bool())
526  }
527
528  // Maybe Block req needs a kill: for itlb, itlb and icache may not sync, itlb should wait icache to go ahead
529  override def toPrintable: Printable = {
530    p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} kill:${kill} pc:0x${Hexadecimal(debug.pc)} robIdx:${debug.robIdx}"
531  }
532}
533
534class TlbExceptionBundle(implicit p: Parameters) extends TlbBundle {
535  val ld = Output(Bool())
536  val st = Output(Bool())
537  val instr = Output(Bool())
538}
539
540class TlbResp(nDups: Int = 1)(implicit p: Parameters) extends TlbBundle {
541  val paddr = Vec(nDups, Output(UInt(PAddrBits.W)))
542  val gpaddr = Vec(nDups, Output(UInt(GPAddrBits.W)))
543  val pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W)))
544  val miss = Output(Bool())
545  val fastMiss = Output(Bool())
546  val excp = Vec(nDups, new Bundle {
547    val gpf = new TlbExceptionBundle()
548    val pf = new TlbExceptionBundle()
549    val af = new TlbExceptionBundle()
550  })
551  val ptwBack = Output(Bool()) // when ptw back, wake up replay rs's state
552  val memidx = Output(new MemBlockidxBundle)
553
554  val debug = new Bundle {
555    val robIdx = Output(new RobPtr)
556    val isFirstIssue = Output(Bool())
557  }
558  override def toPrintable: Printable = {
559    p"paddr:0x${Hexadecimal(paddr(0))} miss:${miss} excp.pf: ld:${excp(0).pf.ld} st:${excp(0).pf.st} instr:${excp(0).pf.instr} ptwBack:${ptwBack}"
560  }
561}
562
563class TlbRequestIO(nRespDups: Int = 1)(implicit p: Parameters) extends TlbBundle {
564  val req = DecoupledIO(new TlbReq)
565  val req_kill = Output(Bool())
566  val resp = Flipped(DecoupledIO(new TlbResp(nRespDups)))
567}
568
569class TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle {
570  val req = Vec(Width, DecoupledIO(new PtwReq))
571  val resp = Flipped(DecoupledIO(new PtwRespS2))
572
573
574  override def toPrintable: Printable = {
575    p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}"
576  }
577}
578
579class TlbPtwIOwithMemIdx(Width: Int = 1)(implicit p: Parameters) extends TlbBundle {
580  val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx))
581  val resp = Flipped(DecoupledIO(new PtwRespS2withMemIdx()))
582
583
584  override def toPrintable: Printable = {
585    p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}"
586  }
587}
588
589class TlbHintReq(implicit p: Parameters) extends TlbBundle {
590  val id = Output(UInt(log2Up(loadfiltersize).W))
591  val full = Output(Bool())
592}
593
594class TLBHintResp(implicit p: Parameters) extends TlbBundle {
595  val id = Output(UInt(log2Up(loadfiltersize).W))
596  // When there are multiple matching entries for PTW resp in filter
597  // e.g. vaddr 0, 0x80000000. vaddr 1, 0x80010000
598  // these two vaddrs are not in a same 4K Page, so will send to ptw twice
599  // However, when ptw resp, if they are in a 1G or 2M huge page
600  // The two entries will both hit, and both need to replay
601  val replay_all = Output(Bool())
602}
603
604class TlbHintIO(implicit p: Parameters) extends TlbBundle {
605  val req = Vec(backendParams.LdExuCnt, new TlbHintReq)
606  val resp = ValidIO(new TLBHintResp)
607}
608
609class MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle {
610  val sfence = Input(new SfenceBundle)
611  val csr = Input(new TlbCsrBundle)
612
613  def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle): Unit = {
614    this.sfence <> sfence
615    this.csr <> csr
616  }
617
618  // overwrite satp. write satp will cause flushpipe but csr.priv won't
619  // satp will be dealyed several cycles from writing, but csr.priv won't
620  // so inside mmu, these two signals should be divided
621  def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle, satp: TlbSatpBundle) = {
622    this.sfence <> sfence
623    this.csr <> csr
624    this.csr.satp := satp
625  }
626}
627
628class TlbRefilltoMemIO()(implicit p: Parameters) extends TlbBundle {
629  val valid = Bool()
630  val memidx = new MemBlockidxBundle
631}
632
633class TlbIO(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends
634  MMUIOBaseBundle {
635  val hartId = Input(UInt(hartIdLen.W))
636  val requestor = Vec(Width, Flipped(new TlbRequestIO(nRespDups)))
637  val flushPipe = Vec(Width, Input(Bool()))
638  val redirect = Flipped(ValidIO(new Redirect)) // flush the signal need_gpa in tlb
639  val ptw = new TlbPtwIOwithMemIdx(Width)
640  val refill_to_mem = Output(new TlbRefilltoMemIO())
641  val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null
642  val pmp = Vec(Width, ValidIO(new PMPReqBundle(q.lgMaxSize)))
643  val tlbreplay = Vec(Width, Output(Bool()))
644}
645
646class VectorTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle {
647  val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx()))
648  val resp = Flipped(DecoupledIO(new Bundle {
649    val data = new PtwRespS2withMemIdx
650    val vector = Output(Vec(Width, Bool()))
651    val getGpa = Output(Vec(Width, Bool()))
652  }))
653
654  def connect(normal: TlbPtwIOwithMemIdx): Unit = {
655    req <> normal.req
656    resp.ready := normal.resp.ready
657    normal.resp.bits := resp.bits.data
658    normal.resp.valid := resp.valid
659  }
660}
661
662/****************************  L2TLB  *************************************/
663abstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst
664abstract class PtwModule(outer: L2TLB) extends LazyModuleImp(outer)
665  with HasXSParameter with HasPtwConst
666
667class PteBundle(implicit p: Parameters) extends PtwBundle{
668  val n = UInt(pteNLen.W)
669  val pbmt = UInt(ptePbmtLen.W)
670  val reserved  = UInt(pteResLen.W)
671  val ppn_high = UInt(ppnHignLen.W)
672  val ppn  = UInt(ppnLen.W)
673  val rsw  = UInt(pteRswLen.W)
674  val perm = new Bundle {
675    val d    = Bool()
676    val a    = Bool()
677    val g    = Bool()
678    val u    = Bool()
679    val x    = Bool()
680    val w    = Bool()
681    val r    = Bool()
682    val v    = Bool()
683  }
684
685  def unaligned(level: UInt) = {
686    isLeaf() &&
687      !(level === 0.U ||
688        level === 1.U && ppn(vpnnLen-1,   0) === 0.U ||
689        level === 2.U && ppn(vpnnLen*2-1, 0) === 0.U ||
690        level === 3.U && ppn(vpnnLen*3-1, 0) === 0.U)
691  }
692
693  def isLeaf() = {
694    (perm.r || perm.x || perm.w) && perm.v
695  }
696
697  def isNext() = {
698    !(perm.r || perm.x || perm.w) && perm.v
699  }
700
701  def isPf(level: UInt, pbmte: Bool) = {
702    val pf = WireInit(false.B)
703    when (reserved =/= 0.U){
704      pf := true.B
705    }.elsewhen(pbmt === 3.U || (!pbmte && pbmt =/= 0.U)){
706      pf := true.B
707    }.elsewhen (isNext()) {
708      pf := (perm.u || perm.a || perm.d || n =/= 0.U || pbmt =/= 0.U)
709    }.elsewhen (!perm.v || (!perm.r && perm.w)) {
710      pf := true.B
711    }.elsewhen (n =/= 0.U && ppn(3, 0) =/= 8.U) {
712      pf := true.B
713    }.otherwise{
714      pf := unaligned(level)
715    }
716    pf
717  }
718
719  def isGpf(level: UInt, pbmte: Bool) = {
720    val gpf = WireInit(false.B)
721    when (reserved =/= 0.U){
722      gpf := true.B
723    }.elsewhen(pbmt === 3.U || (!pbmte && pbmt =/= 0.U)){
724      gpf := true.B
725    }.elsewhen (isNext()) {
726      gpf := (perm.u || perm.a || perm.d || n =/= 0.U || pbmt =/= 0.U)
727    }.elsewhen (!perm.v || (!perm.r && perm.w)) {
728      gpf := true.B
729    }.elsewhen (!perm.u) {
730      gpf := true.B
731    }.elsewhen (n =/= 0.U && ppn(3, 0) =/= 8.U) {
732      gpf := true.B
733    }.otherwise{
734      gpf := unaligned(level)
735    }
736    gpf
737  }
738
739  // ppn of Xiangshan is 48 - 12 bits but ppn of sv48 is 44 bits
740  // access fault will be raised when ppn >> ppnLen is not zero
741  def isAf(): Bool = {
742    !(ppn_high === 0.U) && perm.v
743  }
744
745  def isStage1Gpf(mode: UInt) = {
746    val sv39_high = Cat(ppn_high, ppn) >> (GPAddrBitsSv39x4 - offLen)
747    val sv48_high = Cat(ppn_high, ppn) >> (GPAddrBitsSv48x4 - offLen)
748    !(Mux(mode === Sv39, sv39_high, Mux(mode === Sv48, sv48_high, 0.U)) === 0.U) && perm.v
749  }
750
751  def getPerm() = {
752    val pm = Wire(new PtePermBundle)
753    pm.d := perm.d
754    pm.a := perm.a
755    pm.g := perm.g
756    pm.u := perm.u
757    pm.x := perm.x
758    pm.w := perm.w
759    pm.r := perm.r
760    pm
761  }
762  def getPPN() = {
763    Cat(ppn_high, ppn)
764  }
765
766  def canRefill(levelUInt: UInt, s2xlate: UInt, pbmte: Bool, mode: UInt) = {
767    val canRefill = WireInit(false.B)
768    switch (s2xlate) {
769      is (allStage) {
770        canRefill := !isStage1Gpf(mode) && !isPf(levelUInt, pbmte)
771      }
772      is (onlyStage1) {
773        canRefill := !isAf() && !isPf(levelUInt, pbmte)
774      }
775      is (onlyStage2) {
776        canRefill := !isAf() && !isGpf(levelUInt, pbmte)
777      }
778      is (noS2xlate) {
779        canRefill := !isAf() && !isPf(levelUInt, pbmte)
780      }
781    }
782    canRefill
783  }
784
785  def onlyPf(levelUInt: UInt, s2xlate: UInt, pbmte: Bool) = {
786    s2xlate === noS2xlate && isPf(levelUInt, pbmte) && !isAf()
787  }
788
789  override def toPrintable: Printable = {
790    p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}"
791  }
792}
793
794class PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle {
795  val tag = UInt(tagLen.W)
796  val asid = UInt(asidLen.W)
797  val vmid = if (HasHExtension) Some(UInt(vmidLen.W)) else None
798  val pbmt = UInt(ptePbmtLen.W)
799  val ppn = UInt(gvpnLen.W)
800  val perm = if (hasPerm) Some(new PtePermBundle) else None
801  val level = if (hasLevel) Some(UInt(log2Up(Level + 1).W)) else None
802  val prefetch = Bool()
803  val v = Bool()
804
805  def is_normalentry(): Bool = {
806    if (!hasLevel) true.B
807    else level.get === 2.U
808  }
809
810  def genPPN(vpn: UInt): UInt = {
811    if (!hasLevel) {
812      ppn
813    } else {
814      MuxLookup(level.get, 0.U)(Seq(
815        3.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*3), vpn(vpnnLen*3-1, 0)),
816        2.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)),
817        1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)),
818        0.U -> ppn)
819      )
820    }
821  }
822
823  //s2xlate control whether compare vmid or not
824  def hit(vpn: UInt, asid: UInt, vasid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false, s2xlate: Bool) = {
825    require(vpn.getWidth == vpnLen)
826//    require(this.asid.getWidth <= asid.getWidth)
827    val asid_value = Mux(s2xlate, vasid, asid)
828    val asid_hit = if (ignoreAsid) true.B else (this.asid === asid_value)
829    val vmid_hit = Mux(s2xlate, (this.vmid.getOrElse(0.U) === vmid), true.B)
830    if (allType) {
831      require(hasLevel)
832      val tag_match = Wire(Vec(4, Bool())) // 512GB, 1GB, 2MB or 4KB, not parameterized here
833      for (i <- 0 until 3) {
834        tag_match(i) := tag(vpnnLen * (i + 1) - 1, vpnnLen * i) === vpn(vpnnLen * (i + 1) - 1, vpnnLen * i)
835      }
836      tag_match(3) := tag(tagLen - 1, vpnnLen * 3) === vpn(tagLen - 1, vpnnLen * 3)
837
838      val level_match = MuxLookup(level.getOrElse(0.U), false.B)(Seq(
839        3.U -> tag_match(3),
840        2.U -> (tag_match(3) && tag_match(2)),
841        1.U -> (tag_match(3) && tag_match(2) && tag_match(1)),
842        0.U -> (tag_match(3) && tag_match(2) && tag_match(1) && tag_match(0)))
843      )
844
845      asid_hit && vmid_hit && level_match
846    } else if (hasLevel) {
847      val tag_match = Wire(Vec(3, Bool())) // SuperPage, 512GB, 1GB or 2MB
848      tag_match(0) := tag(tagLen - 1, tagLen - vpnnLen - extendVpnnBits) === vpn(vpnLen - 1, vpnLen - vpnnLen - extendVpnnBits)
849      for (i <- 1 until 3) {
850        tag_match(i) := tag(tagLen - vpnnLen * i - extendVpnnBits - 1, tagLen - vpnnLen * (i + 1) - extendVpnnBits) === vpn(vpnLen - vpnnLen * i - extendVpnnBits - 1, vpnLen - vpnnLen * (i + 1) - extendVpnnBits)
851      }
852
853      val level_match = MuxLookup(level.getOrElse(0.U), false.B)(Seq(
854        3.U -> tag_match(0),
855        2.U -> (tag_match(0) && tag_match(1)),
856        1.U -> (tag_match(0) && tag_match(1) && tag_match(2)))
857      )
858
859      asid_hit && vmid_hit && level_match
860    } else {
861      asid_hit && vmid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen)
862    }
863  }
864
865  def refill(vpn: UInt, asid: UInt, vmid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B): Unit = {
866    require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside
867
868    tag := vpn(vpnLen - 1, vpnLen - tagLen)
869    pbmt := pte.asTypeOf(new PteBundle().cloneType).pbmt
870    ppn := pte.asTypeOf(new PteBundle().cloneType).ppn
871    perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm)
872    this.asid := asid
873    this.vmid.map(_ := vmid)
874    this.prefetch := prefetch
875    this.v := valid
876    this.level.map(_ := level)
877  }
878
879  def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) = {
880    val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel))
881    e.refill(vpn, asid, pte, level, prefetch, valid)
882    e
883  }
884
885
886
887  override def toPrintable: Printable = {
888    // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}"
889    p"tag:0x${Hexadecimal(tag)} pbmt: ${pbmt} ppn:0x${Hexadecimal(ppn)} " +
890      (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") +
891      (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") +
892      p"prefetch:${prefetch}"
893  }
894}
895
896class PtwSectorEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwEntry(tagLen, hasPerm, hasLevel) {
897  override val ppn = UInt(sectorptePPNLen.W)
898}
899
900class PtwMergeEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwSectorEntry(tagLen, hasPerm, hasLevel) {
901  val ppn_low = UInt(sectortlbwidth.W)
902  val af = Bool()
903  val pf = Bool()
904}
905
906class PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean, ReservedBits: Int)(implicit p: Parameters) extends PtwBundle {
907  require(log2Up(num)==log2Down(num))
908  // NOTE: hasPerm means that is leaf or not.
909
910  val tag  = UInt(tagLen.W)
911  val asid = UInt(asidLen.W)
912  val vmid = Some(UInt(vmidLen.W))
913  val pbmts = Vec(num, UInt(ptePbmtLen.W))
914  val ppns = Vec(num, UInt(gvpnLen.W))
915  // valid or not, vs = 0 will not hit
916  val vs   = Vec(num, Bool())
917  // only pf or not, onlypf = 1 means only trigger pf when nox2late
918  val onlypf = Vec(num, Bool())
919  val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None
920  val prefetch = Bool()
921  val reservedBits = if(ReservedBits > 0) Some(UInt(ReservedBits.W)) else None
922  // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1")
923  // NOTE: vs is used for different usage:
924  // for l0, which store the leaf(leaves), vs is page fault or not.
925  // for l1, which shoule not store leaf, vs is valid or not, that will anticipate in hit check
926  // Because, l1 should not store leaf(no perm), it doesn't store perm.
927  // If l1 hit a leaf, the perm is still unavailble. Should still page walk. Complex but nothing helpful.
928  // TODO: divide vs into validVec and pfVec
929  // for l1: may valid but pf, so no need for page walk, return random pte with pf.
930
931  def tagClip(vpn: UInt) = {
932    require(vpn.getWidth == vpnLen)
933    vpn(vpnLen - 1, vpnLen - tagLen)
934  }
935
936  def sectorIdxClip(vpn: UInt, level: Int) = {
937    getVpnClip(vpn, level)(log2Up(num) - 1, 0)
938  }
939
940  def hit(vpn: UInt, asid: UInt, vasid: UInt, vmid:UInt, ignoreAsid: Boolean = false, s2xlate: Bool) = {
941    val asid_value = Mux(s2xlate, vasid, asid)
942    val asid_hit = if (ignoreAsid) true.B else (this.asid === asid_value)
943    val vmid_hit = Mux(s2xlate, this.vmid.getOrElse(0.U) === vmid, true.B)
944    asid_hit && vmid_hit && tag === tagClip(vpn) && vs(sectorIdxClip(vpn, level))
945  }
946
947  def genEntries(vpn: UInt, asid: UInt, vmid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool, s2xlate: UInt, pbmte: Bool, mode: UInt) = {
948    require((data.getWidth / XLEN) == num,
949      s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}")
950
951    val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm, ReservedBits))
952    ps.tag := tagClip(vpn)
953    ps.asid := asid
954    ps.vmid.map(_ := vmid)
955    ps.prefetch := prefetch
956    for (i <- 0 until num) {
957      val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle)
958      ps.pbmts(i) := pte.pbmt
959      ps.ppns(i) := pte.ppn
960      ps.vs(i)   := (pte.canRefill(levelUInt, s2xlate, pbmte, mode) || (if (hasPerm) pte.onlyPf(levelUInt, s2xlate, pbmte) else false.B)) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf())
961      ps.onlypf(i) := pte.onlyPf(levelUInt, s2xlate, pbmte)
962      ps.perms.map(_(i) := pte.perm)
963    }
964    ps.reservedBits.map(_ := true.B)
965    ps
966  }
967
968  override def toPrintable: Printable = {
969    // require(num == 4, "if num is not 4, please comment this toPrintable")
970    // NOTE: if num is not 4, please comment this toPrintable
971    val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle)))
972    p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} pbmt:${printVec(pbmts)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " +
973      (if (hasPerm) p"perms:${printVec(permsInner)}" else p"")
974  }
975}
976
977class PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean, ReservedBits: Int = 0)(implicit p: Parameters) extends PtwBundle {
978  val entries = new PtwEntries(num, tagLen, level, hasPerm, ReservedBits)
979
980  val ecc_block = XLEN
981  val ecc_info = get_ecc_info()
982  val ecc = if (l2tlbParams.enablePTWECC) Some(UInt(ecc_info._1.W)) else None
983
984  def get_ecc_info(): (Int, Int, Int, Int) = {
985    val eccBits_per = eccCode.width(ecc_block) - ecc_block
986
987    val data_length = entries.getWidth
988    val data_align_num = data_length / ecc_block
989    val data_not_align = (data_length % ecc_block) != 0 // ugly code
990    val data_unalign_length = data_length - data_align_num * ecc_block
991    val eccBits_unalign = eccCode.width(data_unalign_length) - data_unalign_length
992
993    val eccBits = eccBits_per * data_align_num + eccBits_unalign
994    (eccBits, eccBits_per, data_align_num, data_unalign_length)
995  }
996
997  def encode() = {
998    val data = entries.asUInt
999    val ecc_slices = Wire(Vec(ecc_info._3, UInt(ecc_info._2.W)))
1000    for (i <- 0 until ecc_info._3) {
1001      ecc_slices(i) := eccCode.encode(data((i+1)*ecc_block-1, i*ecc_block)) >> ecc_block
1002    }
1003    if (ecc_info._4 != 0) {
1004      val ecc_unaligned = eccCode.encode(data(data.getWidth-1, ecc_info._3*ecc_block)) >> ecc_info._4
1005      ecc.map(_ := Cat(ecc_unaligned, ecc_slices.asUInt))
1006    } else { ecc.map(_ := ecc_slices.asUInt)}
1007  }
1008
1009  def decode(): Bool = {
1010    val data = entries.asUInt
1011    val res = Wire(Vec(ecc_info._3 + 1, Bool()))
1012    for (i <- 0 until ecc_info._3) {
1013      res(i) := {if (ecc_info._2 != 0) eccCode.decode(Cat(ecc.get((i+1)*ecc_info._2-1, i*ecc_info._2), data((i+1)*ecc_block-1, i*ecc_block))).error else false.B}
1014    }
1015    if (ecc_info._2 != 0 && ecc_info._4 != 0) {
1016      res(ecc_info._3) := eccCode.decode(
1017        Cat(ecc.get(ecc_info._1-1, ecc_info._2*ecc_info._3), data(data.getWidth-1, ecc_info._3*ecc_block))).error
1018    } else { res(ecc_info._3) := false.B }
1019
1020    Cat(res).orR
1021  }
1022
1023  def gen(vpn: UInt, asid: UInt, vmid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool, s2xlate: UInt, pbmte: Bool, mode: UInt) = {
1024    this.entries := entries.genEntries(vpn, asid, vmid, data, levelUInt, prefetch, s2xlate, pbmte, mode)
1025    this.encode()
1026  }
1027}
1028
1029class PtwReq(implicit p: Parameters) extends PtwBundle {
1030  val vpn = UInt(vpnLen.W) //vpn or gvpn
1031  val s2xlate = UInt(2.W)
1032  def hasS2xlate(): Bool = {
1033    this.s2xlate =/= noS2xlate
1034  }
1035  def isOnlyStage2: Bool = {
1036    this.s2xlate === onlyStage2
1037  }
1038  override def toPrintable: Printable = {
1039    p"vpn:0x${Hexadecimal(vpn)}"
1040  }
1041}
1042
1043class PtwReqwithMemIdx(implicit p: Parameters) extends PtwReq {
1044  val memidx = new MemBlockidxBundle
1045  val getGpa = Bool() // this req is to get gpa when having guest page fault
1046}
1047
1048class PtwResp(implicit p: Parameters) extends PtwBundle {
1049  val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true)
1050  val pf = Bool()
1051  val af = Bool()
1052
1053  def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = {
1054    this.entry.level.map(_ := level)
1055    this.entry.tag := vpn
1056    this.entry.perm.map(_ := pte.getPerm())
1057    this.entry.ppn := pte.ppn
1058    this.entry.pbmt := pte.pbmt
1059    this.entry.prefetch := DontCare
1060    this.entry.asid := asid
1061    this.entry.v := !pf
1062    this.pf := pf
1063    this.af := af
1064  }
1065
1066  override def toPrintable: Printable = {
1067    p"entry:${entry} pf:${pf} af:${af}"
1068  }
1069}
1070
1071class HptwResp(implicit p: Parameters) extends PtwBundle {
1072  val entry = new PtwEntry(tagLen = gvpnLen, hasPerm = true, hasLevel = true)
1073  val gpf = Bool()
1074  val gaf = Bool()
1075
1076  def apply(gpf: Bool, gaf: Bool, level: UInt, pte: PteBundle, vpn: UInt, vmid: UInt) = {
1077    val resp_pte = Mux(gaf, 0.U.asTypeOf(pte), pte)
1078    this.entry.level.map(_ := level)
1079    this.entry.tag := vpn
1080    this.entry.perm.map(_ := resp_pte.getPerm())
1081    this.entry.ppn := resp_pte.ppn
1082    this.entry.pbmt := resp_pte.pbmt
1083    this.entry.prefetch := DontCare
1084    this.entry.asid := DontCare
1085    this.entry.vmid.map(_ := vmid)
1086    this.entry.v := !gpf
1087    this.gpf := gpf
1088    this.gaf := gaf
1089  }
1090
1091  def genPPNS2(vpn: UInt): UInt = {
1092    MuxLookup(entry.level.get, 0.U)(Seq(
1093      3.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 3), vpn(vpnnLen * 3 - 1, 0)),
1094      2.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 2), vpn(vpnnLen * 2 - 1, 0)),
1095      1.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen), vpn(vpnnLen - 1, 0)),
1096      0.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, 0))
1097    ))
1098  }
1099
1100  def hit(gvpn: UInt, vmid: UInt): Bool = {
1101    val vmid_hit = this.entry.vmid.getOrElse(0.U) === vmid
1102    val tag_match = Wire(Vec(4, Bool())) // 512GB, 1GB, 2MB or 4KB, not parameterized here
1103    for (i <- 0 until 3) {
1104      tag_match(i) := entry.tag(vpnnLen * (i + 1)  - 1, vpnnLen * i) === gvpn(vpnnLen * (i + 1)  - 1, vpnnLen * i)
1105    }
1106    tag_match(3) := entry.tag(gvpnLen - 1, vpnnLen * 3) === gvpn(gvpnLen - 1, vpnnLen * 3)
1107
1108    val level_match = MuxLookup(entry.level.getOrElse(0.U), false.B)(Seq(
1109      3.U -> tag_match(3),
1110      2.U -> (tag_match(3) && tag_match(2)),
1111      1.U -> (tag_match(3) && tag_match(2) && tag_match(1)),
1112      0.U -> (tag_match(3) && tag_match(2) && tag_match(1) && tag_match(0)))
1113    )
1114
1115    vmid_hit && level_match
1116  }
1117}
1118
1119class PtwSectorResp(implicit p: Parameters) extends PtwBundle {
1120  val entry = new PtwSectorEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)
1121  val addr_low = UInt(sectortlbwidth.W)
1122  val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W))
1123  val valididx = Vec(tlbcontiguous, Bool())
1124  val pteidx = Vec(tlbcontiguous, Bool())
1125  val pf = Bool()
1126  val af = Bool()
1127
1128
1129  def genPPN(vpn: UInt): UInt = {
1130    MuxLookup(entry.level.get, 0.U)(Seq(
1131      3.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 3 - sectortlbwidth), vpn(vpnnLen * 3 - 1, 0)),
1132      2.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), vpn(vpnnLen * 2 - 1, 0)),
1133      1.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen - sectortlbwidth), vpn(vpnnLen - 1, 0)),
1134      0.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0))))
1135    )
1136  }
1137
1138   def genGVPN(vpn: UInt): UInt = {
1139    val isNonLeaf = !(entry.perm.get.r || entry.perm.get.x || entry.perm.get.w) && entry.v && !pf && !af
1140    Mux(isNonLeaf, Cat(entry.ppn(entry.ppn.getWidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0))), genPPN(vpn))
1141  }
1142
1143  def isLeaf() = {
1144    (entry.perm.get.r || entry.perm.get.x || entry.perm.get.w) && entry.v
1145  }
1146
1147  def isFakePte() = {
1148    !pf && !entry.v && !af
1149  }
1150
1151  def hit(vpn: UInt, asid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false, s2xlate: Bool): Bool = {
1152    require(vpn.getWidth == vpnLen)
1153    //    require(this.asid.getWidth <= asid.getWidth)
1154    val asid_hit = if (ignoreAsid) true.B else (this.entry.asid === asid)
1155    val vmid_hit = Mux(s2xlate, this.entry.vmid.getOrElse(0.U) === vmid, true.B)
1156    if (allType) {
1157      val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0))
1158      val tag_match = Wire(Vec(4, Bool())) // 512GB, 1GB, 2MB or 4KB, not parameterized here
1159      tag_match(0) := entry.tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth)
1160      for (i <- 1 until 3) {
1161        tag_match(i) := entry.tag(vpnnLen * (i + 1) - sectortlbwidth - 1, vpnnLen * i - sectortlbwidth) === vpn(vpnnLen * (i + 1) - 1, vpnnLen * i)
1162      }
1163      tag_match(3) := entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth) === vpn(vpnLen - 1, vpnnLen * 3)
1164
1165      val level_match = MuxLookup(entry.level.getOrElse(0.U), false.B)(Seq(
1166        3.U -> tag_match(3),
1167        2.U -> (tag_match(3) && tag_match(2)),
1168        1.U -> (tag_match(3) && tag_match(2) && tag_match(1)),
1169        0.U -> (tag_match(3) && tag_match(2) && tag_match(1) && tag_match(0)))
1170      )
1171
1172      asid_hit && vmid_hit && level_match && addr_low_hit
1173    } else {
1174      val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0))
1175      val tag_match = Wire(Vec(3, Bool())) // SuperPage, 512GB, 1GB or 2MB
1176      for (i <- 0 until 3) {
1177        tag_match(i) := entry.tag(sectorvpnLen - vpnnLen * i - 1, sectorvpnLen - vpnnLen * (i + 1)) === vpn(vpnLen - vpnnLen * i - 1, vpnLen - vpnnLen * (i + 1))
1178      }
1179
1180      val level_match = MuxLookup(entry.level.getOrElse(0.U), false.B)(Seq(
1181        3.U -> tag_match(0),
1182        2.U -> (tag_match(0) && tag_match(1)),
1183        1.U -> (tag_match(0) && tag_match(1) && tag_match(2)))
1184      )
1185
1186      asid_hit && vmid_hit && level_match && addr_low_hit
1187    }
1188  }
1189}
1190
1191class PtwMergeResp(implicit p: Parameters) extends PtwBundle {
1192  val entry = Vec(tlbcontiguous, new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true))
1193  val pteidx = Vec(tlbcontiguous, Bool())
1194  val not_super = Bool()
1195  val not_merge = Bool()
1196
1197  def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt, vmid:UInt, addr_low : UInt, not_super : Boolean = true, not_merge: Boolean = false) = {
1198    assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!")
1199    val resp_pte = pte
1200    val ptw_resp = Wire(new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true))
1201    ptw_resp.ppn := resp_pte.getPPN()(ptePPNLen - 1, sectortlbwidth)
1202    ptw_resp.ppn_low := resp_pte.getPPN()(sectortlbwidth - 1, 0)
1203    ptw_resp.pbmt := resp_pte.pbmt
1204    ptw_resp.level.map(_ := level)
1205    ptw_resp.perm.map(_ := resp_pte.getPerm())
1206    ptw_resp.tag := vpn(vpnLen - 1, sectortlbwidth)
1207    ptw_resp.pf := pf
1208    ptw_resp.af := af
1209    ptw_resp.v := resp_pte.perm.v
1210    ptw_resp.prefetch := DontCare
1211    ptw_resp.asid := asid
1212    ptw_resp.vmid.map(_ := vmid)
1213    this.pteidx := UIntToOH(addr_low).asBools
1214    this.not_super := not_super.B
1215    this.not_merge := not_merge.B
1216
1217    for (i <- 0 until tlbcontiguous) {
1218      this.entry(i) := ptw_resp
1219    }
1220  }
1221
1222  def genPPN(): UInt = {
1223    val idx = OHToUInt(pteidx)
1224    val tag = Cat(entry(idx).tag, idx(sectortlbwidth - 1, 0))
1225    MuxLookup(entry(idx).level.get, 0.U)(Seq(
1226      3.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen * 3 - sectortlbwidth), tag(vpnnLen * 3 - 1, 0)),
1227      2.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), tag(vpnnLen * 2 - 1, 0)),
1228      1.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen - sectortlbwidth), tag(vpnnLen - 1, 0)),
1229      0.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, 0), entry(idx).ppn_low))
1230    )
1231  }
1232}
1233
1234class PtwRespS2(implicit p: Parameters) extends PtwBundle {
1235  val s2xlate = UInt(2.W)
1236  val s1 = new PtwSectorResp()
1237  val s2 = new HptwResp()
1238
1239  def hasS2xlate: Bool = {
1240    this.s2xlate =/= noS2xlate
1241  }
1242
1243  def isOnlyStage2: Bool = {
1244    this.s2xlate === onlyStage2
1245  }
1246
1247  def getVpn(vpn: UInt): UInt = {
1248    val level = s1.entry.level.getOrElse(0.U) min s2.entry.level.getOrElse(0.U)
1249    val s1tag = Cat(s1.entry.tag, OHToUInt(s1.pteidx))
1250    val s1tagFix = MuxCase(s1.entry.tag, Seq(
1251      (s1.entry.level.getOrElse(0.U) === 3.U && s2.entry.level.getOrElse(0.U) === 2.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), s2.entry.tag(vpnnLen * 3 - 1, vpnnLen * 2), 0.U((vpnnLen * 2 - sectortlbwidth).W)),
1252      (s1.entry.level.getOrElse(0.U) === 3.U && s2.entry.level.getOrElse(0.U) === 1.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), s2.entry.tag(vpnnLen * 3 - 1, vpnnLen), 0.U((vpnnLen - sectortlbwidth).W)),
1253      (s1.entry.level.getOrElse(0.U) === 3.U && s2.entry.level.getOrElse(0.U) === 0.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), s2.entry.tag(vpnnLen * 3 - 1, sectortlbwidth)),
1254      (s1.entry.level.getOrElse(0.U) === 2.U && s2.entry.level.getOrElse(0.U) === 1.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), s2.entry.tag(vpnnLen * 2 - 1, vpnnLen), 0.U((vpnnLen - sectortlbwidth).W)),
1255      (s1.entry.level.getOrElse(0.U) === 2.U && s2.entry.level.getOrElse(0.U) === 0.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), s2.entry.tag(vpnnLen * 2 - 1, sectortlbwidth)),
1256      (s1.entry.level.getOrElse(0.U) === 1.U && s2.entry.level.getOrElse(0.U) === 0.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth), s2.entry.tag(vpnnLen - 1, sectortlbwidth))
1257    ))
1258    val s1_vpn = MuxLookup(level, s1tag)(Seq(
1259      3.U -> Cat(s1tagFix(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), vpn(vpnnLen * 3 - 1, 0)),
1260      2.U -> Cat(s1tagFix(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), vpn(vpnnLen * 2 - 1, 0)),
1261      1.U -> Cat(s1tagFix(sectorvpnLen - 1, vpnnLen - sectortlbwidth), vpn(vpnnLen - 1, 0)))
1262    )
1263    val s2_vpn = s2.entry.tag
1264    Mux(s2xlate === onlyStage2, s2_vpn, Mux(s2xlate === allStage, s1_vpn, s1tag))
1265  }
1266
1267  def hit(vpn: UInt, asid: UInt, vasid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false): Bool = {
1268    val noS2_hit = s1.hit(vpn, Mux(this.hasS2xlate, vasid, asid), vmid, allType, ignoreAsid, this.hasS2xlate)
1269    val onlyS2_hit = s2.hit(vpn, vmid)
1270    // allstage and onlys1 hit
1271    val s1vpn = Cat(s1.entry.tag, s1.addr_low)
1272    val level = s1.entry.level.getOrElse(0.U) min s2.entry.level.getOrElse(0.U)
1273
1274    val tag_match = Wire(Vec(4, Bool())) // 512GB, 1GB, 2MB or 4KB, not parameterized here
1275    for (i <- 0 until 3) {
1276      tag_match(i) := vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) === s1vpn(vpnnLen * (i + 1) - 1, vpnnLen * i)
1277    }
1278    tag_match(3) := vpn(vpnLen - 1, vpnnLen * 3) === s1vpn(vpnLen - 1, vpnnLen * 3)
1279    val level_match = MuxLookup(level, false.B)(Seq(
1280      3.U -> tag_match(3),
1281      2.U -> (tag_match(3) && tag_match(2)),
1282      1.U -> (tag_match(3) && tag_match(2) && tag_match(1)),
1283      0.U -> (tag_match(3) && tag_match(2) && tag_match(1) && tag_match(0)))
1284    )
1285
1286    val vpn_hit = level_match
1287    val vmid_hit = Mux(this.s2xlate === allStage, s2.entry.vmid.getOrElse(0.U) === vmid, true.B)
1288    val vasid_hit = if (ignoreAsid) true.B else (s1.entry.asid === vasid)
1289    val all_onlyS1_hit = vpn_hit && vmid_hit && vasid_hit
1290    Mux(this.s2xlate === noS2xlate, noS2_hit,
1291      Mux(this.s2xlate === onlyStage2, onlyS2_hit, all_onlyS1_hit))
1292  }
1293}
1294
1295class PtwRespS2withMemIdx(implicit p: Parameters) extends PtwRespS2 {
1296  val memidx = new MemBlockidxBundle()
1297  val getGpa = Bool() // this req is to get gpa when having guest page fault
1298}
1299
1300class L2TLBIO(implicit p: Parameters) extends PtwBundle {
1301  val hartId = Input(UInt(hartIdLen.W))
1302  val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO))
1303  val sfence = Input(new SfenceBundle)
1304  val csr = new Bundle {
1305    val tlb = Input(new TlbCsrBundle)
1306    val distribute_csr = Flipped(new DistributedCSRIO)
1307  }
1308}
1309
1310class L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle {
1311  val addr = UInt(PAddrBits.W)
1312  val id = UInt(bMemID.W)
1313  val hptw_bypassed = Bool()
1314}
1315
1316class L2TlbInnerBundle(implicit p: Parameters) extends PtwReq {
1317  val source = UInt(bSourceWidth.W)
1318}
1319
1320class L2TlbWithHptwIdBundle(implicit p: Parameters) extends PtwBundle {
1321  val req_info = new L2TlbInnerBundle
1322  val isHptwReq = Bool()
1323  val isLLptw = Bool()
1324  val hptwId = UInt(log2Up(l2tlbParams.llptwsize).W)
1325}
1326
1327object ValidHoldBypass{
1328  def apply(infire: Bool, outfire: Bool, flush: Bool = false.B) = {
1329    val valid = RegInit(false.B)
1330    when (infire) { valid := true.B }
1331    when (outfire) { valid := false.B } // ATTENTION: order different with ValidHold
1332    when (flush) { valid := false.B } // NOTE: the flush will flush in & out, is that ok?
1333    valid || infire
1334  }
1335}
1336
1337class L1TlbDB(implicit p: Parameters) extends TlbBundle {
1338  val vpn = UInt(vpnLen.W)
1339}
1340
1341class PageCacheDB(implicit p: Parameters) extends TlbBundle with HasPtwConst {
1342  val vpn = UInt(vpnLen.W)
1343  val source = UInt(bSourceWidth.W)
1344  val bypassed = Bool()
1345  val is_first = Bool()
1346  val prefetched = Bool()
1347  val prefetch = Bool()
1348  val l2Hit = Bool()
1349  val l1Hit = Bool()
1350  val hit = Bool()
1351}
1352
1353class PTWDB(implicit p: Parameters) extends TlbBundle with HasPtwConst {
1354  val vpn = UInt(vpnLen.W)
1355  val source = UInt(bSourceWidth.W)
1356}
1357
1358class L2TlbPrefetchDB(implicit p: Parameters) extends TlbBundle {
1359  val vpn = UInt(vpnLen.W)
1360}
1361
1362class L2TlbMissQueueDB(implicit p: Parameters) extends TlbBundle {
1363  val vpn = UInt(vpnLen.W)
1364}
1365