xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/MMUBundle.scala (revision 5afdf73c2fdaffb2f720bbfe5d35cf7f5f914b82)
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 utils._
249aca92b9SYinan Xuimport xiangshan.backend.rob.RobPtr
256d5ddbceSLemoverimport xiangshan.backend.fu.util.HasCSRConst
266d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
276d5ddbceSLemoverimport freechips.rocketchip.tilelink._
285b7ef044SLemoverimport xiangshan.backend.fu.{PMPReqBundle, PMPConfig}
29f1fe8698SLemoverimport xiangshan.backend.fu.PMPBundle
305b7ef044SLemover
316d5ddbceSLemover
326d5ddbceSLemoverabstract class TlbBundle(implicit p: Parameters) extends XSBundle with HasTlbConst
336d5ddbceSLemoverabstract class TlbModule(implicit p: Parameters) extends XSModule with HasTlbConst
346d5ddbceSLemover
35a0301c0dSLemoverclass VaBundle(implicit p: Parameters) extends TlbBundle {
36a0301c0dSLemover  val vpn  = UInt(vpnLen.W)
37a0301c0dSLemover  val off  = UInt(offLen.W)
38a0301c0dSLemover}
39a0301c0dSLemover
406d5ddbceSLemoverclass PtePermBundle(implicit p: Parameters) extends TlbBundle {
416d5ddbceSLemover  val d = Bool()
426d5ddbceSLemover  val a = Bool()
436d5ddbceSLemover  val g = Bool()
446d5ddbceSLemover  val u = Bool()
456d5ddbceSLemover  val x = Bool()
466d5ddbceSLemover  val w = Bool()
476d5ddbceSLemover  val r = Bool()
486d5ddbceSLemover
496d5ddbceSLemover  override def toPrintable: Printable = {
506d5ddbceSLemover    p"d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"// +
516d5ddbceSLemover    //(if(hasV) (p"v:${v}") else p"")
526d5ddbceSLemover  }
536d5ddbceSLemover}
546d5ddbceSLemover
555b7ef044SLemoverclass TlbPMBundle(implicit p: Parameters) extends TlbBundle {
565b7ef044SLemover  val r = Bool()
575b7ef044SLemover  val w = Bool()
585b7ef044SLemover  val x = Bool()
595b7ef044SLemover  val c = Bool()
605b7ef044SLemover  val atomic = Bool()
615b7ef044SLemover
625b7ef044SLemover  def assign_ap(pm: PMPConfig) = {
635b7ef044SLemover    r := pm.r
645b7ef044SLemover    w := pm.w
655b7ef044SLemover    x := pm.x
665b7ef044SLemover    c := pm.c
675b7ef044SLemover    atomic := pm.atomic
685b7ef044SLemover  }
695b7ef044SLemover}
705b7ef044SLemover
716d5ddbceSLemoverclass TlbPermBundle(implicit p: Parameters) extends TlbBundle {
726d5ddbceSLemover  val pf = Bool() // NOTE: if this is true, just raise pf
73b6982e83SLemover  val af = Bool() // NOTE: if this is true, just raise af
746d5ddbceSLemover  // pagetable perm (software defined)
756d5ddbceSLemover  val d = Bool()
766d5ddbceSLemover  val a = Bool()
776d5ddbceSLemover  val g = Bool()
786d5ddbceSLemover  val u = Bool()
796d5ddbceSLemover  val x = Bool()
806d5ddbceSLemover  val w = Bool()
816d5ddbceSLemover  val r = Bool()
826d5ddbceSLemover
835b7ef044SLemover  val pm = new TlbPMBundle
845b7ef044SLemover
85f1fe8698SLemover  def apply(item: PtwResp, pm: PMPConfig) = {
86f1fe8698SLemover    val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType)
87f1fe8698SLemover    this.pf := item.pf
88f1fe8698SLemover    this.af := item.af
89f1fe8698SLemover    this.d := ptePerm.d
90f1fe8698SLemover    this.a := ptePerm.a
91f1fe8698SLemover    this.g := ptePerm.g
92f1fe8698SLemover    this.u := ptePerm.u
93f1fe8698SLemover    this.x := ptePerm.x
94f1fe8698SLemover    this.w := ptePerm.w
95f1fe8698SLemover    this.r := ptePerm.r
96f1fe8698SLemover
97f1fe8698SLemover    this.pm.assign_ap(pm)
98f1fe8698SLemover    this
99f1fe8698SLemover  }
1006d5ddbceSLemover  override def toPrintable: Printable = {
1015b7ef044SLemover    p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " +
1025b7ef044SLemover    p"pm:${pm}"
1036d5ddbceSLemover  }
1046d5ddbceSLemover}
1056d5ddbceSLemover
1066d5ddbceSLemover// multi-read && single-write
1076d5ddbceSLemover// input is data, output is hot-code(not one-hot)
1086d5ddbceSLemoverclass CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule {
1096d5ddbceSLemover  val io = IO(new Bundle {
1106d5ddbceSLemover    val r = new Bundle {
1116d5ddbceSLemover      val req = Input(Vec(readWidth, gen))
1126d5ddbceSLemover      val resp = Output(Vec(readWidth, Vec(set, Bool())))
1136d5ddbceSLemover    }
1146d5ddbceSLemover    val w = Input(new Bundle {
1156d5ddbceSLemover      val valid = Bool()
1166d5ddbceSLemover      val bits = new Bundle {
1176d5ddbceSLemover        val index = UInt(log2Up(set).W)
1186d5ddbceSLemover        val data = gen
1196d5ddbceSLemover      }
1206d5ddbceSLemover    })
1216d5ddbceSLemover  })
1226d5ddbceSLemover
1236d5ddbceSLemover  val wordType = UInt(gen.getWidth.W)
1246d5ddbceSLemover  val array = Reg(Vec(set, wordType))
1256d5ddbceSLemover
1266d5ddbceSLemover  io.r.resp.zipWithIndex.map{ case (a,i) =>
1276d5ddbceSLemover    a := array.map(io.r.req(i).asUInt === _)
1286d5ddbceSLemover  }
1296d5ddbceSLemover
1306d5ddbceSLemover  when (io.w.valid) {
13176e02f07SLingrui98    array(io.w.bits.index) := io.w.bits.data.asUInt
1326d5ddbceSLemover  }
1336d5ddbceSLemover}
1346d5ddbceSLemover
135a0301c0dSLemoverclass TlbEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle {
136a0301c0dSLemover  require(pageNormal || pageSuper)
137a0301c0dSLemover
138a0301c0dSLemover  val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W)
139a0301c0dSLemover            else UInt(vpnLen.W)
14045f497a4Shappy-lx  val asid = UInt(asidLen.W)
141a0301c0dSLemover  val level = if (!pageNormal) Some(UInt(1.W))
142a0301c0dSLemover              else if (!pageSuper) None
143a0301c0dSLemover              else Some(UInt(2.W))
144a0301c0dSLemover  val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W)
145a0301c0dSLemover            else UInt(ppnLen.W)
146a0301c0dSLemover  val perm = new TlbPermBundle
147a0301c0dSLemover
14856728e73SLemover  /** level usage:
14956728e73SLemover   *  !PageSuper: page is only normal, level is None, match all the tag
15056728e73SLemover   *  !PageNormal: page is only super, level is a Bool(), match high 9*2 parts
15156728e73SLemover   *  bits0  0: need mid 9bits
15256728e73SLemover   *         1: no need mid 9bits
15356728e73SLemover   *  PageSuper && PageNormal: page hold all the three type,
15456728e73SLemover   *  bits0  0: need low 9bits
15556728e73SLemover   *  bits1  0: need mid 9bits
15656728e73SLemover   */
15756728e73SLemover
158e9092fe2SLemover  def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false): Bool = {
15945f497a4Shappy-lx    val asid_hit = if (ignoreAsid) true.B else (this.asid === asid)
160e9092fe2SLemover
161e9092fe2SLemover    // NOTE: for timing, dont care low set index bits at hit check
162e9092fe2SLemover    //       do not need store the low bits actually
163e9092fe2SLemover    if (!pageSuper) asid_hit && drop_set_equal(vpn, tag, nSets)
16456728e73SLemover    else if (!pageNormal) {
16556728e73SLemover      val tag_match_hi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*3-1, vpnnLen*2)
16656728e73SLemover      val tag_match_mi = tag(vpnnLen-1, 0) === vpn(vpnnLen*2-1, vpnnLen)
16756728e73SLemover      val tag_match = tag_match_hi && (level.get.asBool() || tag_match_mi)
16856728e73SLemover      asid_hit && tag_match
16956728e73SLemover    }
17056728e73SLemover    else {
17156728e73SLemover      val tmp_level = level.get
17256728e73SLemover      val tag_match_hi = tag(vpnnLen*3-1, vpnnLen*2) === vpn(vpnnLen*3-1, vpnnLen*2)
17356728e73SLemover      val tag_match_mi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*2-1, vpnnLen)
17456728e73SLemover      val tag_match_lo = tag(vpnnLen-1, 0) === vpn(vpnnLen-1, 0) // if pageNormal is false, this will always be false
17556728e73SLemover      val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo)
17656728e73SLemover      asid_hit && tag_match
17756728e73SLemover    }
178a0301c0dSLemover  }
179a0301c0dSLemover
1805b7ef044SLemover  def apply(item: PtwResp, asid: UInt, pm: PMPConfig): TlbEntry = {
181a0301c0dSLemover    this.tag := {if (pageNormal) item.entry.tag else item.entry.tag(vpnLen-1, vpnnLen)}
18245f497a4Shappy-lx    this.asid := asid
183a0301c0dSLemover    val inner_level = item.entry.level.getOrElse(0.U)
18456728e73SLemover    this.level.map(_ := { if (pageNormal && pageSuper) MuxLookup(inner_level, 0.U, Seq(
18556728e73SLemover                                                        0.U -> 3.U,
18656728e73SLemover                                                        1.U -> 1.U,
18756728e73SLemover                                                        2.U -> 0.U ))
18856728e73SLemover                          else if (pageSuper) ~inner_level(0)
189a0301c0dSLemover                          else 0.U })
190a0301c0dSLemover    this.ppn := { if (!pageNormal) item.entry.ppn(ppnLen-1, vpnnLen)
191a0301c0dSLemover                  else item.entry.ppn }
192f1fe8698SLemover    this.perm.apply(item, pm)
193a0301c0dSLemover    this
194a0301c0dSLemover  }
195a0301c0dSLemover
19656728e73SLemover  // 4KB is normal entry, 2MB/1GB is considered as super entry
19756728e73SLemover  def is_normalentry(): Bool = {
19856728e73SLemover    if (!pageSuper) { true.B }
19956728e73SLemover    else if (!pageNormal) { false.B }
20056728e73SLemover    else { level.get === 0.U }
20156728e73SLemover  }
2025cf62c1aSLemover
20356728e73SLemover  def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = {
20456728e73SLemover    val inner_level = level.getOrElse(0.U)
20556728e73SLemover    val ppn_res = if (!pageSuper) ppn
20656728e73SLemover      else if (!pageNormal) Cat(ppn(ppnLen-vpnnLen-1, vpnnLen),
20756728e73SLemover        Mux(inner_level(0), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen-1,0)),
20856728e73SLemover        vpn(vpnnLen-1, 0))
20956728e73SLemover      else Cat(ppn(ppnLen-1, vpnnLen*2),
21056728e73SLemover        Mux(inner_level(1), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen*2-1, vpnnLen)),
21156728e73SLemover        Mux(inner_level(0), vpn(vpnnLen-1, 0), ppn(vpnnLen-1, 0)))
21256728e73SLemover
21356728e73SLemover    if (saveLevel) Cat(ppn(ppn.getWidth-1, vpnnLen*2), RegEnable(ppn_res(vpnnLen*2-1, 0), valid))
2145cf62c1aSLemover    else ppn_res
215a0301c0dSLemover  }
216a0301c0dSLemover
217a0301c0dSLemover  override def toPrintable: Printable = {
218a0301c0dSLemover    val inner_level = level.getOrElse(2.U)
21945f497a4Shappy-lx    p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}"
220a0301c0dSLemover  }
221a0301c0dSLemover
222a0301c0dSLemover}
223a0301c0dSLemover
2246d5ddbceSLemoverobject TlbCmd {
2256d5ddbceSLemover  def read  = "b00".U
2266d5ddbceSLemover  def write = "b01".U
2276d5ddbceSLemover  def exec  = "b10".U
2286d5ddbceSLemover
2296d5ddbceSLemover  def atom_read  = "b100".U // lr
2306d5ddbceSLemover  def atom_write = "b101".U // sc / amo
2316d5ddbceSLemover
2326d5ddbceSLemover  def apply() = UInt(3.W)
2336d5ddbceSLemover  def isRead(a: UInt) = a(1,0)===read
2346d5ddbceSLemover  def isWrite(a: UInt) = a(1,0)===write
2356d5ddbceSLemover  def isExec(a: UInt) = a(1,0)===exec
2366d5ddbceSLemover
2376d5ddbceSLemover  def isAtom(a: UInt) = a(2)
238a79fef67Swakafa  def isAmo(a: UInt) = a===atom_write // NOTE: sc mixed
2396d5ddbceSLemover}
2406d5ddbceSLemover
24103efd994Shappy-lxclass TlbStorageIO(nSets: Int, nWays: Int, ports: Int, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle {
242a0301c0dSLemover  val r = new Bundle {
243a0301c0dSLemover    val req = Vec(ports, Flipped(DecoupledIO(new Bundle {
244a0301c0dSLemover      val vpn = Output(UInt(vpnLen.W))
245a0301c0dSLemover    })))
246a0301c0dSLemover    val resp = Vec(ports, ValidIO(new Bundle{
247a0301c0dSLemover      val hit = Output(Bool())
24803efd994Shappy-lx      val ppn = Vec(nDups, Output(UInt(ppnLen.W)))
24903efd994Shappy-lx      val perm = Vec(nDups, Output(new TlbPermBundle()))
250a0301c0dSLemover    }))
251a0301c0dSLemover  }
252a0301c0dSLemover  val w = Flipped(ValidIO(new Bundle {
253a0301c0dSLemover    val wayIdx = Output(UInt(log2Up(nWays).W))
254a0301c0dSLemover    val data = Output(new PtwResp)
2555b7ef044SLemover    val data_replenish = Output(new PMPConfig)
256a0301c0dSLemover  }))
257a0301c0dSLemover  val victim = new Bundle {
25845f497a4Shappy-lx    val out = ValidIO(Output(new Bundle {
25945f497a4Shappy-lx      val entry = new TlbEntry(pageNormal = true, pageSuper = false)
26045f497a4Shappy-lx    }))
26145f497a4Shappy-lx    val in = Flipped(ValidIO(Output(new Bundle {
26245f497a4Shappy-lx      val entry = new TlbEntry(pageNormal = true, pageSuper = false)
26345f497a4Shappy-lx    })))
264a0301c0dSLemover  }
2653889e11eSLemover  val access = Vec(ports, new ReplaceAccessBundle(nSets, nWays))
266a0301c0dSLemover
267f1fe8698SLemover  def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = {
268a0301c0dSLemover    this.r.req(i).valid := valid
269a0301c0dSLemover    this.r.req(i).bits.vpn := vpn
270a0301c0dSLemover  }
271a0301c0dSLemover
272a0301c0dSLemover  def r_resp_apply(i: Int) = {
273f1fe8698SLemover    (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm)
274a0301c0dSLemover  }
275a0301c0dSLemover
2765b7ef044SLemover  def w_apply(valid: Bool, wayIdx: UInt, data: PtwResp, data_replenish: PMPConfig): Unit = {
277a0301c0dSLemover    this.w.valid := valid
278a0301c0dSLemover    this.w.bits.wayIdx := wayIdx
279a0301c0dSLemover    this.w.bits.data := data
2805b7ef044SLemover    this.w.bits.data_replenish := data_replenish
281a0301c0dSLemover  }
282a0301c0dSLemover
283a0301c0dSLemover}
284a0301c0dSLemover
28503efd994Shappy-lxclass TlbStorageWrapperIO(ports: Int, q: TLBParameters, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle {
286f1fe8698SLemover  val r = new Bundle {
287f1fe8698SLemover    val req = Vec(ports, Flipped(DecoupledIO(new Bundle {
288f1fe8698SLemover      val vpn = Output(UInt(vpnLen.W))
289f1fe8698SLemover    })))
290f1fe8698SLemover    val resp = Vec(ports, ValidIO(new Bundle{
291f1fe8698SLemover      val hit = Output(Bool())
29203efd994Shappy-lx      val ppn = Vec(nDups, Output(UInt(ppnLen.W)))
29303efd994Shappy-lx      val perm = Vec(nDups, Output(new TlbPermBundle()))
294f1fe8698SLemover      // below are dirty code for timing optimization
295f1fe8698SLemover      val super_hit = Output(Bool())
296f1fe8698SLemover      val super_ppn = Output(UInt(ppnLen.W))
297f1fe8698SLemover      val spm = Output(new TlbPMBundle)
298f1fe8698SLemover    }))
299f1fe8698SLemover  }
300f1fe8698SLemover  val w = Flipped(ValidIO(new Bundle {
301f1fe8698SLemover    val data = Output(new PtwResp)
302f1fe8698SLemover    val data_replenish = Output(new PMPConfig)
303f1fe8698SLemover  }))
304f1fe8698SLemover  val replace = if (q.outReplace) Flipped(new TlbReplaceIO(ports, q)) else null
305f1fe8698SLemover
306f1fe8698SLemover  def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = {
307f1fe8698SLemover    this.r.req(i).valid := valid
308f1fe8698SLemover    this.r.req(i).bits.vpn := vpn
309f1fe8698SLemover  }
310f1fe8698SLemover
311f1fe8698SLemover  def r_resp_apply(i: Int) = {
312f1fe8698SLemover    (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm,
313f1fe8698SLemover    this.r.resp(i).bits.super_hit, this.r.resp(i).bits.super_ppn, this.r.resp(i).bits.spm)
314f1fe8698SLemover  }
315f1fe8698SLemover
316f1fe8698SLemover  def w_apply(valid: Bool, data: PtwResp, data_replenish: PMPConfig): Unit = {
317f1fe8698SLemover    this.w.valid := valid
318f1fe8698SLemover    this.w.bits.data := data
319f1fe8698SLemover    this.w.bits.data_replenish := data_replenish
320f1fe8698SLemover  }
321f1fe8698SLemover}
322f1fe8698SLemover
3233889e11eSLemoverclass ReplaceAccessBundle(nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle {
3243889e11eSLemover  val sets = Output(UInt(log2Up(nSets).W))
3253889e11eSLemover  val touch_ways = ValidIO(Output(UInt(log2Up(nWays).W)))
3263889e11eSLemover
3273889e11eSLemover}
3283889e11eSLemover
329a0301c0dSLemoverclass ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle {
3303889e11eSLemover  val access = Vec(Width, Flipped(new ReplaceAccessBundle(nSets, nWays)))
331a0301c0dSLemover
332a0301c0dSLemover  val refillIdx = Output(UInt(log2Up(nWays).W))
333a0301c0dSLemover  val chosen_set = Flipped(Output(UInt(log2Up(nSets).W)))
334a0301c0dSLemover
335a0301c0dSLemover  def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = {
33653b8f1a7SLemover    for ((ac_rep, ac_tlb) <- access.zip(in.map(a => a.access.map(b => b)).flatten)) {
33753b8f1a7SLemover      ac_rep := ac_tlb
338a0301c0dSLemover    }
33953b8f1a7SLemover    this.chosen_set := get_set_idx(vpn, nSets)
34053b8f1a7SLemover    in.map(a => a.refillIdx := this.refillIdx)
341a0301c0dSLemover  }
342a0301c0dSLemover}
343a0301c0dSLemover
344a0301c0dSLemoverclass TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends
345a0301c0dSLemover  TlbBundle {
346a0301c0dSLemover  val normalPage = new ReplaceIO(Width, q.normalNSets, q.normalNWays)
347a0301c0dSLemover  val superPage = new ReplaceIO(Width, q.superNSets, q.superNWays)
348a0301c0dSLemover
349a0301c0dSLemover  def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = {
350a0301c0dSLemover    this.normalPage.apply_sep(in.map(_.normalPage), vpn)
351a0301c0dSLemover    this.superPage.apply_sep(in.map(_.superPage), vpn)
352a0301c0dSLemover  }
353a0301c0dSLemover
354a0301c0dSLemover}
355a0301c0dSLemover
3566d5ddbceSLemoverclass TlbReq(implicit p: Parameters) extends TlbBundle {
357ca2f90a6SLemover  val vaddr = Output(UInt(VAddrBits.W))
358ca2f90a6SLemover  val cmd = Output(TlbCmd())
359ca2f90a6SLemover  val size = Output(UInt(log2Ceil(log2Ceil(XLEN/8)+1).W))
360f1fe8698SLemover  val kill = Output(Bool()) // Use for blocked tlb that need sync with other module like icache
3616d5ddbceSLemover  val debug = new Bundle {
362ca2f90a6SLemover    val pc = Output(UInt(XLEN.W))
363f1fe8698SLemover    val robIdx = Output(new RobPtr)
364ca2f90a6SLemover    val isFirstIssue = Output(Bool())
3656d5ddbceSLemover  }
3666d5ddbceSLemover
367f1fe8698SLemover  // Maybe Block req needs a kill: for itlb, itlb and icache may not sync, itlb should wait icache to go ahead
3686d5ddbceSLemover  override def toPrintable: Printable = {
369f1fe8698SLemover    p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} kill:${kill} pc:0x${Hexadecimal(debug.pc)} robIdx:${debug.robIdx}"
3706d5ddbceSLemover  }
3716d5ddbceSLemover}
3726d5ddbceSLemover
373b6982e83SLemoverclass TlbExceptionBundle(implicit p: Parameters) extends TlbBundle {
374b6982e83SLemover  val ld = Output(Bool())
375b6982e83SLemover  val st = Output(Bool())
376b6982e83SLemover  val instr = Output(Bool())
377b6982e83SLemover}
378b6982e83SLemover
37903efd994Shappy-lxclass TlbResp(nDups: Int = 1)(implicit p: Parameters) extends TlbBundle {
38003efd994Shappy-lx  val paddr = Vec(nDups, Output(UInt(PAddrBits.W)))
381ca2f90a6SLemover  val miss = Output(Bool())
382cccfc98dSLemover  val fast_miss = Output(Bool()) // without sram part for timing optimization
38303efd994Shappy-lx  val excp = Vec(nDups, new Bundle {
384b6982e83SLemover    val pf = new TlbExceptionBundle()
385b6982e83SLemover    val af = new TlbExceptionBundle()
38603efd994Shappy-lx  })
3875b7ef044SLemover  val static_pm = Output(Valid(Bool())) // valid for static, bits for mmio result from normal entries
388ca2f90a6SLemover  val ptwBack = Output(Bool()) // when ptw back, wake up replay rs's state
3896d5ddbceSLemover
3906d5ddbceSLemover  override def toPrintable: Printable = {
39103efd994Shappy-lx    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}"
3926d5ddbceSLemover  }
3936d5ddbceSLemover}
3946d5ddbceSLemover
39503efd994Shappy-lxclass TlbRequestIO(nRespDups: Int = 1)(implicit p: Parameters) extends TlbBundle {
3966d5ddbceSLemover  val req = DecoupledIO(new TlbReq)
397c3b763d0SYinan Xu  val req_kill = Output(Bool())
39803efd994Shappy-lx  val resp = Flipped(DecoupledIO(new TlbResp(nRespDups)))
3996d5ddbceSLemover}
4006d5ddbceSLemover
4016d5ddbceSLemoverclass TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle {
4026d5ddbceSLemover  val req = Vec(Width, DecoupledIO(new PtwReq))
4036d5ddbceSLemover  val resp = Flipped(DecoupledIO(new PtwResp))
4046d5ddbceSLemover
4056d5ddbceSLemover
4066d5ddbceSLemover  override def toPrintable: Printable = {
4076d5ddbceSLemover    p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}"
4086d5ddbceSLemover  }
4096d5ddbceSLemover}
4106d5ddbceSLemover
41145f497a4Shappy-lxclass MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle {
412b052b972SLemover  val sfence = Input(new SfenceBundle)
413b052b972SLemover  val csr = Input(new TlbCsrBundle)
414f1fe8698SLemover
415f1fe8698SLemover  def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle): Unit = {
416f1fe8698SLemover    this.sfence <> sfence
417f1fe8698SLemover    this.csr <> csr
418f1fe8698SLemover  }
419f1fe8698SLemover
420f1fe8698SLemover  // overwrite satp. write satp will cause flushpipe but csr.priv won't
421f1fe8698SLemover  // satp will be dealyed several cycles from writing, but csr.priv won't
422f1fe8698SLemover  // so inside mmu, these two signals should be divided
423f1fe8698SLemover  def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle, satp: TlbSatpBundle) = {
424f1fe8698SLemover    this.sfence <> sfence
425f1fe8698SLemover    this.csr <> csr
426f1fe8698SLemover    this.csr.satp := satp
427f1fe8698SLemover  }
428a0301c0dSLemover}
4296d5ddbceSLemover
43003efd994Shappy-lxclass TlbIO(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends
43145f497a4Shappy-lx  MMUIOBaseBundle {
43203efd994Shappy-lx  val requestor = Vec(Width, Flipped(new TlbRequestIO(nRespDups)))
433f1fe8698SLemover  val flushPipe = Vec(Width, Input(Bool()))
434a0301c0dSLemover  val ptw = new TlbPtwIO(Width)
4355b7ef044SLemover  val ptw_replenish = Input(new PMPConfig())
436a0301c0dSLemover  val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null
437b6982e83SLemover  val pmp = Vec(Width, ValidIO(new PMPReqBundle()))
438a0301c0dSLemover
439a0301c0dSLemover}
440a0301c0dSLemover
441f1fe8698SLemoverclass VectorTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle {
442a0301c0dSLemover  val req = Vec(Width, DecoupledIO(new PtwReq))
443a0301c0dSLemover  val resp = Flipped(DecoupledIO(new Bundle {
444a0301c0dSLemover    val data = new PtwResp
445a0301c0dSLemover    val vector = Output(Vec(Width, Bool()))
446a0301c0dSLemover  }))
447a0301c0dSLemover
448f1fe8698SLemover  def connect(normal: TlbPtwIO): Unit = {
449f1fe8698SLemover    req <> normal.req
450f1fe8698SLemover    resp.ready := normal.resp.ready
451f1fe8698SLemover    normal.resp.bits := resp.bits.data
452f1fe8698SLemover    normal.resp.valid := resp.valid
453a0301c0dSLemover  }
4546d5ddbceSLemover}
4556d5ddbceSLemover
45692e3bfefSLemover/****************************  L2TLB  *************************************/
4576d5ddbceSLemoverabstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst
45892e3bfefSLemoverabstract class PtwModule(outer: L2TLB) extends LazyModuleImp(outer)
4596d5ddbceSLemover  with HasXSParameter with HasPtwConst
4606d5ddbceSLemover
4616d5ddbceSLemoverclass PteBundle(implicit p: Parameters) extends PtwBundle{
4626d5ddbceSLemover  val reserved  = UInt(pteResLen.W)
4636d5ddbceSLemover  val ppn  = UInt(ppnLen.W)
4646d5ddbceSLemover  val rsw  = UInt(2.W)
4656d5ddbceSLemover  val perm = new Bundle {
4666d5ddbceSLemover    val d    = Bool()
4676d5ddbceSLemover    val a    = Bool()
4686d5ddbceSLemover    val g    = Bool()
4696d5ddbceSLemover    val u    = Bool()
4706d5ddbceSLemover    val x    = Bool()
4716d5ddbceSLemover    val w    = Bool()
4726d5ddbceSLemover    val r    = Bool()
4736d5ddbceSLemover    val v    = Bool()
4746d5ddbceSLemover  }
4756d5ddbceSLemover
4766d5ddbceSLemover  def unaligned(level: UInt) = {
4776d5ddbceSLemover    isLeaf() && !(level === 2.U ||
4786d5ddbceSLemover                  level === 1.U && ppn(vpnnLen-1,   0) === 0.U ||
4796d5ddbceSLemover                  level === 0.U && ppn(vpnnLen*2-1, 0) === 0.U)
4806d5ddbceSLemover  }
4816d5ddbceSLemover
4826d5ddbceSLemover  def isPf(level: UInt) = {
4836d5ddbceSLemover    !perm.v || (!perm.r && perm.w) || unaligned(level)
4846d5ddbceSLemover  }
4856d5ddbceSLemover
4866d5ddbceSLemover  def isLeaf() = {
4876d5ddbceSLemover    perm.r || perm.x || perm.w
4886d5ddbceSLemover  }
4896d5ddbceSLemover
4906d5ddbceSLemover  def getPerm() = {
4916d5ddbceSLemover    val pm = Wire(new PtePermBundle)
4926d5ddbceSLemover    pm.d := perm.d
4936d5ddbceSLemover    pm.a := perm.a
4946d5ddbceSLemover    pm.g := perm.g
4956d5ddbceSLemover    pm.u := perm.u
4966d5ddbceSLemover    pm.x := perm.x
4976d5ddbceSLemover    pm.w := perm.w
4986d5ddbceSLemover    pm.r := perm.r
4996d5ddbceSLemover    pm
5006d5ddbceSLemover  }
5016d5ddbceSLemover
5026d5ddbceSLemover  override def toPrintable: Printable = {
5036d5ddbceSLemover    p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}"
5046d5ddbceSLemover  }
5056d5ddbceSLemover}
5066d5ddbceSLemover
5076d5ddbceSLemoverclass PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle {
5086d5ddbceSLemover  val tag = UInt(tagLen.W)
50945f497a4Shappy-lx  val asid = UInt(asidLen.W)
5106d5ddbceSLemover  val ppn = UInt(ppnLen.W)
5116d5ddbceSLemover  val perm = if (hasPerm) Some(new PtePermBundle) else None
5126d5ddbceSLemover  val level = if (hasLevel) Some(UInt(log2Up(Level).W)) else None
513bc063562SLemover  val prefetch = Bool()
5148d8ac704SLemover  val v = Bool()
5156d5ddbceSLemover
51656728e73SLemover  def is_normalentry(): Bool = {
51756728e73SLemover    if (!hasLevel) true.B
51856728e73SLemover    else level.get === 2.U
51956728e73SLemover  }
52056728e73SLemover
521f1fe8698SLemover  def genPPN(vpn: UInt): UInt = {
522f1fe8698SLemover    if (!hasLevel) ppn
523f1fe8698SLemover    else MuxLookup(level.get, 0.U, Seq(
524f1fe8698SLemover          0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)),
525f1fe8698SLemover          1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)),
526f1fe8698SLemover          2.U -> ppn)
527f1fe8698SLemover    )
528f1fe8698SLemover  }
529f1fe8698SLemover
53045f497a4Shappy-lx  def hit(vpn: UInt, asid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false) = {
5316d5ddbceSLemover    require(vpn.getWidth == vpnLen)
532cccfc98dSLemover//    require(this.asid.getWidth <= asid.getWidth)
53345f497a4Shappy-lx    val asid_hit = if (ignoreAsid) true.B else (this.asid === asid)
5346d5ddbceSLemover    if (allType) {
5356d5ddbceSLemover      require(hasLevel)
5366d5ddbceSLemover      val hit0 = tag(tagLen - 1,    vpnnLen*2) === vpn(tagLen - 1, vpnnLen*2)
5376d5ddbceSLemover      val hit1 = tag(vpnnLen*2 - 1, vpnnLen)   === vpn(vpnnLen*2 - 1,  vpnnLen)
5386d5ddbceSLemover      val hit2 = tag(vpnnLen - 1,     0)         === vpn(vpnnLen - 1, 0)
53945f497a4Shappy-lx
54045f497a4Shappy-lx      asid_hit && Mux(level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0))
5416d5ddbceSLemover    } else if (hasLevel) {
5426d5ddbceSLemover      val hit0 = tag(tagLen - 1, tagLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen)
5436d5ddbceSLemover      val hit1 = tag(tagLen - vpnnLen - 1, tagLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2)
54445f497a4Shappy-lx
54545f497a4Shappy-lx      asid_hit && Mux(level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1)
5466d5ddbceSLemover    } else {
54745f497a4Shappy-lx      asid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen)
5486d5ddbceSLemover    }
5496d5ddbceSLemover  }
5506d5ddbceSLemover
5518d8ac704SLemover  def refill(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) {
55245f497a4Shappy-lx    require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside
55345f497a4Shappy-lx
5546d5ddbceSLemover    tag := vpn(vpnLen - 1, vpnLen - tagLen)
555a0301c0dSLemover    ppn := pte.asTypeOf(new PteBundle().cloneType).ppn
556a0301c0dSLemover    perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm)
55745f497a4Shappy-lx    this.asid := asid
558bc063562SLemover    this.prefetch := prefetch
5598d8ac704SLemover    this.v := valid
5606d5ddbceSLemover    this.level.map(_ := level)
5616d5ddbceSLemover  }
5626d5ddbceSLemover
5638d8ac704SLemover  def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) = {
5646d5ddbceSLemover    val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel))
5658d8ac704SLemover    e.refill(vpn, asid, pte, level, prefetch, valid)
5666d5ddbceSLemover    e
5676d5ddbceSLemover  }
5686d5ddbceSLemover
5696d5ddbceSLemover
570f1fe8698SLemover
5716d5ddbceSLemover  override def toPrintable: Printable = {
5726d5ddbceSLemover    // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}"
5736d5ddbceSLemover    p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} " +
5746d5ddbceSLemover      (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") +
575bc063562SLemover      (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") +
576bc063562SLemover      p"prefetch:${prefetch}"
5776d5ddbceSLemover  }
5786d5ddbceSLemover}
5796d5ddbceSLemover
5806d5ddbceSLemoverclass PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle {
5816d5ddbceSLemover  require(log2Up(num)==log2Down(num))
5821f4a7c0cSLemover  // NOTE: hasPerm means that is leaf or not.
5836d5ddbceSLemover
5846d5ddbceSLemover  val tag  = UInt(tagLen.W)
58545f497a4Shappy-lx  val asid = UInt(asidLen.W)
5866d5ddbceSLemover  val ppns = Vec(num, UInt(ppnLen.W))
5876d5ddbceSLemover  val vs   = Vec(num, Bool())
5886d5ddbceSLemover  val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None
589bc063562SLemover  val prefetch = Bool()
5906d5ddbceSLemover  // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1")
5911f4a7c0cSLemover  // NOTE: vs is used for different usage:
5921f4a7c0cSLemover  // for l3, which store the leaf(leaves), vs is page fault or not.
5931f4a7c0cSLemover  // for l2, which shoule not store leaf, vs is valid or not, that will anticipate in hit check
5941f4a7c0cSLemover  // Because, l2 should not store leaf(no perm), it doesn't store perm.
5951f4a7c0cSLemover  // If l2 hit a leaf, the perm is still unavailble. Should still page walk. Complex but nothing helpful.
5961f4a7c0cSLemover  // TODO: divide vs into validVec and pfVec
5971f4a7c0cSLemover  // for l2: may valid but pf, so no need for page walk, return random pte with pf.
5986d5ddbceSLemover
5996d5ddbceSLemover  def tagClip(vpn: UInt) = {
6006d5ddbceSLemover    require(vpn.getWidth == vpnLen)
6016d5ddbceSLemover    vpn(vpnLen - 1, vpnLen - tagLen)
6026d5ddbceSLemover  }
6036d5ddbceSLemover
6046d5ddbceSLemover  def sectorIdxClip(vpn: UInt, level: Int) = {
6056d5ddbceSLemover    getVpnClip(vpn, level)(log2Up(num) - 1, 0)
6066d5ddbceSLemover  }
6076d5ddbceSLemover
60845f497a4Shappy-lx  def hit(vpn: UInt, asid: UInt, ignoreAsid: Boolean = false) = {
60945f497a4Shappy-lx    val asid_hit = if (ignoreAsid) true.B else (this.asid === asid)
6101f4a7c0cSLemover    asid_hit && tag === tagClip(vpn) && (if (hasPerm) true.B else vs(sectorIdxClip(vpn, level)))
6116d5ddbceSLemover  }
6126d5ddbceSLemover
61345f497a4Shappy-lx  def genEntries(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = {
6146d5ddbceSLemover    require((data.getWidth / XLEN) == num,
6155854c1edSLemover      s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}")
6166d5ddbceSLemover
6176d5ddbceSLemover    val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm))
6186d5ddbceSLemover    ps.tag := tagClip(vpn)
61945f497a4Shappy-lx    ps.asid := asid
620bc063562SLemover    ps.prefetch := prefetch
6216d5ddbceSLemover    for (i <- 0 until num) {
6226d5ddbceSLemover      val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle)
6236d5ddbceSLemover      ps.ppns(i) := pte.ppn
6246d5ddbceSLemover      ps.vs(i)   := !pte.isPf(levelUInt) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf())
6256d5ddbceSLemover      ps.perms.map(_(i) := pte.perm)
6266d5ddbceSLemover    }
6276d5ddbceSLemover    ps
6286d5ddbceSLemover  }
6296d5ddbceSLemover
6306d5ddbceSLemover  override def toPrintable: Printable = {
6316d5ddbceSLemover    // require(num == 4, "if num is not 4, please comment this toPrintable")
6326d5ddbceSLemover    // NOTE: if num is not 4, please comment this toPrintable
6336d5ddbceSLemover    val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle)))
63445f497a4Shappy-lx    p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " +
6356d5ddbceSLemover      (if (hasPerm) p"perms:${printVec(permsInner)}" else p"")
6366d5ddbceSLemover  }
6376d5ddbceSLemover}
6386d5ddbceSLemover
6397196f5a2SLemoverclass PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle {
6407196f5a2SLemover  val entries = new PtwEntries(num, tagLen, level, hasPerm)
6417196f5a2SLemover
6423889e11eSLemover  val ecc_block = XLEN
6433889e11eSLemover  val ecc_info = get_ecc_info()
6443889e11eSLemover  val ecc = UInt(ecc_info._1.W)
6453889e11eSLemover
6463889e11eSLemover  def get_ecc_info(): (Int, Int, Int, Int) = {
6473889e11eSLemover    val eccBits_per = eccCode.width(ecc_block) - ecc_block
6483889e11eSLemover
6493889e11eSLemover    val data_length = entries.getWidth
6503889e11eSLemover    val data_align_num = data_length / ecc_block
6513889e11eSLemover    val data_not_align = (data_length % ecc_block) != 0 // ugly code
6523889e11eSLemover    val data_unalign_length = data_length - data_align_num * ecc_block
6533889e11eSLemover    val eccBits_unalign = eccCode.width(data_unalign_length) - data_unalign_length
6543889e11eSLemover
6553889e11eSLemover    val eccBits = eccBits_per * data_align_num + eccBits_unalign
6563889e11eSLemover    (eccBits, eccBits_per, data_align_num, data_unalign_length)
6573889e11eSLemover  }
6583889e11eSLemover
6593889e11eSLemover  def encode() = {
6603889e11eSLemover    val data = entries.asUInt()
6613889e11eSLemover    val ecc_slices = Wire(Vec(ecc_info._3, UInt(ecc_info._2.W)))
6623889e11eSLemover    for (i <- 0 until ecc_info._3) {
6633889e11eSLemover      ecc_slices(i) := eccCode.encode(data((i+1)*ecc_block-1, i*ecc_block)) >> ecc_block
6643889e11eSLemover    }
6653889e11eSLemover    if (ecc_info._4 != 0) {
6663889e11eSLemover      val ecc_unaligned = eccCode.encode(data(data.getWidth-1, ecc_info._3*ecc_block)) >> ecc_info._4
6673889e11eSLemover      ecc := Cat(ecc_unaligned, ecc_slices.asUInt())
6683889e11eSLemover    } else { ecc := ecc_slices.asUInt() }
6693889e11eSLemover  }
6703889e11eSLemover
6713889e11eSLemover  def decode(): Bool = {
6723889e11eSLemover    val data = entries.asUInt()
6733889e11eSLemover    val res = Wire(Vec(ecc_info._3 + 1, Bool()))
6743889e11eSLemover    for (i <- 0 until ecc_info._3) {
6755197bac8SZiyue-Zhang      res(i) := {if (ecc_info._2 != 0) eccCode.decode(Cat(ecc((i+1)*ecc_info._2-1, i*ecc_info._2), data((i+1)*ecc_block-1, i*ecc_block))).error else false.B}
6763889e11eSLemover    }
6775197bac8SZiyue-Zhang    if (ecc_info._2 != 0 && ecc_info._4 != 0) {
6783889e11eSLemover      res(ecc_info._3) := eccCode.decode(
6793889e11eSLemover        Cat(ecc(ecc_info._1-1, ecc_info._2*ecc_info._3), data(data.getWidth-1, ecc_info._3*ecc_block))).error
6803889e11eSLemover    } else { res(ecc_info._3) := false.B }
6813889e11eSLemover
6823889e11eSLemover    Cat(res).orR
6833889e11eSLemover  }
6843889e11eSLemover
6853889e11eSLemover  def gen(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = {
6863889e11eSLemover    this.entries := entries.genEntries(vpn, asid, data, levelUInt, prefetch)
6873889e11eSLemover    this.encode()
6883889e11eSLemover  }
6897196f5a2SLemover}
6907196f5a2SLemover
6916d5ddbceSLemoverclass PtwReq(implicit p: Parameters) extends PtwBundle {
6926d5ddbceSLemover  val vpn = UInt(vpnLen.W)
6936d5ddbceSLemover
6946d5ddbceSLemover  override def toPrintable: Printable = {
6956d5ddbceSLemover    p"vpn:0x${Hexadecimal(vpn)}"
6966d5ddbceSLemover  }
6976d5ddbceSLemover}
6986d5ddbceSLemover
6996d5ddbceSLemoverclass PtwResp(implicit p: Parameters) extends PtwBundle {
7006d5ddbceSLemover  val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true)
7016d5ddbceSLemover  val pf = Bool()
702b6982e83SLemover  val af = Bool()
7036d5ddbceSLemover
70445f497a4Shappy-lx  def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = {
7055854c1edSLemover    this.entry.level.map(_ := level)
7065854c1edSLemover    this.entry.tag := vpn
7075854c1edSLemover    this.entry.perm.map(_ := pte.getPerm())
7085854c1edSLemover    this.entry.ppn := pte.ppn
709bc063562SLemover    this.entry.prefetch := DontCare
71045f497a4Shappy-lx    this.entry.asid := asid
7118d8ac704SLemover    this.entry.v := !pf
7125854c1edSLemover    this.pf := pf
713b6982e83SLemover    this.af := af
7145854c1edSLemover  }
7155854c1edSLemover
7166d5ddbceSLemover  override def toPrintable: Printable = {
717b6982e83SLemover    p"entry:${entry} pf:${pf} af:${af}"
7186d5ddbceSLemover  }
7196d5ddbceSLemover}
7206d5ddbceSLemover
72192e3bfefSLemoverclass L2TLBIO(implicit p: Parameters) extends PtwBundle {
7226d5ddbceSLemover  val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO))
7236d5ddbceSLemover  val sfence = Input(new SfenceBundle)
724b6982e83SLemover  val csr = new Bundle {
725b6982e83SLemover    val tlb = Input(new TlbCsrBundle)
726b6982e83SLemover    val distribute_csr = Flipped(new DistributedCSRIO)
727b6982e83SLemover  }
7286d5ddbceSLemover}
7296d5ddbceSLemover
730b848eea5SLemoverclass L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle {
731b848eea5SLemover  val addr = UInt(PAddrBits.W)
732b848eea5SLemover  val id = UInt(bMemID.W)
733b848eea5SLemover}
73445f497a4Shappy-lx
73545f497a4Shappy-lxclass L2TlbInnerBundle(implicit p: Parameters) extends PtwReq {
73645f497a4Shappy-lx  val source = UInt(bSourceWidth.W)
73745f497a4Shappy-lx}
738f1fe8698SLemover
739f1fe8698SLemover
740f1fe8698SLemoverobject ValidHoldBypass{
741f1fe8698SLemover  def apply(infire: Bool, outfire: Bool, flush: Bool = false.B) = {
742f1fe8698SLemover    val valid = RegInit(false.B)
743f1fe8698SLemover    when (infire) { valid := true.B }
744f1fe8698SLemover    when (outfire) { valid := false.B } // ATTENTION: order different with ValidHold
745f1fe8698SLemover    when (flush) { valid := false.B } // NOTE: the flush will flush in & out, is that ok?
746f1fe8698SLemover    valid || infire
747f1fe8698SLemover  }
748f1fe8698SLemover}
749*5afdf73cSHaoyuan Feng
750*5afdf73cSHaoyuan Fengclass L1TlbDB(implicit p: Parameters) extends TlbBundle {
751*5afdf73cSHaoyuan Feng  val vpn = UInt(vpnLen.W)
752*5afdf73cSHaoyuan Feng}
753*5afdf73cSHaoyuan Feng
754*5afdf73cSHaoyuan Fengclass PageCacheDB(implicit p: Parameters) extends TlbBundle with HasPtwConst {
755*5afdf73cSHaoyuan Feng  val vpn = UInt(vpnLen.W)
756*5afdf73cSHaoyuan Feng  val source = UInt(bSourceWidth.W)
757*5afdf73cSHaoyuan Feng  val bypassed = Bool()
758*5afdf73cSHaoyuan Feng  val is_first = Bool()
759*5afdf73cSHaoyuan Feng  val prefetched = Bool()
760*5afdf73cSHaoyuan Feng  val prefetch = Bool()
761*5afdf73cSHaoyuan Feng  val l2Hit = Bool()
762*5afdf73cSHaoyuan Feng  val l1Hit = Bool()
763*5afdf73cSHaoyuan Feng  val hit = Bool()
764*5afdf73cSHaoyuan Feng}
765*5afdf73cSHaoyuan Feng
766*5afdf73cSHaoyuan Fengclass PTWDB(implicit p: Parameters) extends TlbBundle with HasPtwConst {
767*5afdf73cSHaoyuan Feng  val vpn = UInt(vpnLen.W)
768*5afdf73cSHaoyuan Feng  val source = UInt(bSourceWidth.W)
769*5afdf73cSHaoyuan Feng}
770*5afdf73cSHaoyuan Feng
771*5afdf73cSHaoyuan Fengclass L2TlbPrefetchDB(implicit p: Parameters) extends TlbBundle {
772*5afdf73cSHaoyuan Feng  val vpn = UInt(vpnLen.W)
773*5afdf73cSHaoyuan Feng}
774*5afdf73cSHaoyuan Feng
775*5afdf73cSHaoyuan Fengclass L2TlbMissQueueDB(implicit p: Parameters) extends TlbBundle {
776*5afdf73cSHaoyuan Feng  val vpn = UInt(vpnLen.W)
777*5afdf73cSHaoyuan Feng}
778