xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/MMUBundle.scala (revision e4f69d78f24895ac36a5a6c704cec53e4af72485)
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._
243c02ee8fSwakafaimport utility._
259aca92b9SYinan Xuimport xiangshan.backend.rob.RobPtr
266d5ddbceSLemoverimport xiangshan.backend.fu.util.HasCSRConst
276d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
286d5ddbceSLemoverimport freechips.rocketchip.tilelink._
295b7ef044SLemoverimport xiangshan.backend.fu.{PMPReqBundle, PMPConfig}
30f1fe8698SLemoverimport xiangshan.backend.fu.PMPBundle
315b7ef044SLemover
326d5ddbceSLemover
336d5ddbceSLemoverabstract class TlbBundle(implicit p: Parameters) extends XSBundle with HasTlbConst
346d5ddbceSLemoverabstract class TlbModule(implicit p: Parameters) extends XSModule with HasTlbConst
356d5ddbceSLemover
36a0301c0dSLemoverclass VaBundle(implicit p: Parameters) extends TlbBundle {
37a0301c0dSLemover  val vpn  = UInt(vpnLen.W)
38a0301c0dSLemover  val off  = UInt(offLen.W)
39a0301c0dSLemover}
40a0301c0dSLemover
416d5ddbceSLemoverclass PtePermBundle(implicit p: Parameters) extends TlbBundle {
426d5ddbceSLemover  val d = Bool()
436d5ddbceSLemover  val a = Bool()
446d5ddbceSLemover  val g = Bool()
456d5ddbceSLemover  val u = Bool()
466d5ddbceSLemover  val x = Bool()
476d5ddbceSLemover  val w = Bool()
486d5ddbceSLemover  val r = Bool()
496d5ddbceSLemover
506d5ddbceSLemover  override def toPrintable: Printable = {
516d5ddbceSLemover    p"d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"// +
526d5ddbceSLemover    //(if(hasV) (p"v:${v}") else p"")
536d5ddbceSLemover  }
546d5ddbceSLemover}
556d5ddbceSLemover
565b7ef044SLemoverclass TlbPMBundle(implicit p: Parameters) extends TlbBundle {
575b7ef044SLemover  val r = Bool()
585b7ef044SLemover  val w = Bool()
595b7ef044SLemover  val x = Bool()
605b7ef044SLemover  val c = Bool()
615b7ef044SLemover  val atomic = Bool()
625b7ef044SLemover
635b7ef044SLemover  def assign_ap(pm: PMPConfig) = {
645b7ef044SLemover    r := pm.r
655b7ef044SLemover    w := pm.w
665b7ef044SLemover    x := pm.x
675b7ef044SLemover    c := pm.c
685b7ef044SLemover    atomic := pm.atomic
695b7ef044SLemover  }
705b7ef044SLemover}
715b7ef044SLemover
726d5ddbceSLemoverclass TlbPermBundle(implicit p: Parameters) extends TlbBundle {
736d5ddbceSLemover  val pf = Bool() // NOTE: if this is true, just raise pf
74b6982e83SLemover  val af = Bool() // NOTE: if this is true, just raise af
756d5ddbceSLemover  // pagetable perm (software defined)
766d5ddbceSLemover  val d = Bool()
776d5ddbceSLemover  val a = Bool()
786d5ddbceSLemover  val g = Bool()
796d5ddbceSLemover  val u = Bool()
806d5ddbceSLemover  val x = Bool()
816d5ddbceSLemover  val w = Bool()
826d5ddbceSLemover  val r = Bool()
836d5ddbceSLemover
84b0fa7106SHaoyuan Feng  val pm = new TlbPMBundle
85b0fa7106SHaoyuan Feng
86b0fa7106SHaoyuan Feng  def apply(item: PtwSectorResp, pm: PMPConfig) = {
87b0fa7106SHaoyuan Feng    val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType)
88b0fa7106SHaoyuan Feng    this.pf := item.pf
89b0fa7106SHaoyuan Feng    this.af := item.af
90b0fa7106SHaoyuan Feng    this.d := ptePerm.d
91b0fa7106SHaoyuan Feng    this.a := ptePerm.a
92b0fa7106SHaoyuan Feng    this.g := ptePerm.g
93b0fa7106SHaoyuan Feng    this.u := ptePerm.u
94b0fa7106SHaoyuan Feng    this.x := ptePerm.x
95b0fa7106SHaoyuan Feng    this.w := ptePerm.w
96b0fa7106SHaoyuan Feng    this.r := ptePerm.r
97b0fa7106SHaoyuan Feng
98b0fa7106SHaoyuan Feng    this.pm.assign_ap(pm)
99b0fa7106SHaoyuan Feng    this
100b0fa7106SHaoyuan Feng  }
101b0fa7106SHaoyuan Feng  override def toPrintable: Printable = {
102b0fa7106SHaoyuan Feng    p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " +
103b0fa7106SHaoyuan Feng    p"pm:${pm}"
104b0fa7106SHaoyuan Feng  }
105b0fa7106SHaoyuan Feng}
106b0fa7106SHaoyuan Feng
107b0fa7106SHaoyuan Fengclass TlbSectorPermBundle(implicit p: Parameters) extends TlbBundle {
108b0fa7106SHaoyuan Feng  val pf = Bool() // NOTE: if this is true, just raise pf
109b0fa7106SHaoyuan Feng  val af = Bool() // NOTE: if this is true, just raise af
110b0fa7106SHaoyuan Feng  // pagetable perm (software defined)
111b0fa7106SHaoyuan Feng  val d = Bool()
112b0fa7106SHaoyuan Feng  val a = Bool()
113b0fa7106SHaoyuan Feng  val g = Bool()
114b0fa7106SHaoyuan Feng  val u = Bool()
115b0fa7106SHaoyuan Feng  val x = Bool()
116b0fa7106SHaoyuan Feng  val w = Bool()
117b0fa7106SHaoyuan Feng  val r = Bool()
118b0fa7106SHaoyuan Feng
11963632028SHaoyuan Feng  // static pmp & pma check has a minimum grain size of 4K
12063632028SHaoyuan Feng  // So sector tlb will use eight static pm entries
12163632028SHaoyuan Feng  val pm = Vec(tlbcontiguous, new TlbPMBundle)
1225b7ef044SLemover
12363632028SHaoyuan Feng  def apply(item: PtwSectorResp, pm: Seq[PMPConfig]) = {
124f1fe8698SLemover    val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType)
125f1fe8698SLemover    this.pf := item.pf
126f1fe8698SLemover    this.af := item.af
127f1fe8698SLemover    this.d := ptePerm.d
128f1fe8698SLemover    this.a := ptePerm.a
129f1fe8698SLemover    this.g := ptePerm.g
130f1fe8698SLemover    this.u := ptePerm.u
131f1fe8698SLemover    this.x := ptePerm.x
132f1fe8698SLemover    this.w := ptePerm.w
133f1fe8698SLemover    this.r := ptePerm.r
134f1fe8698SLemover
13563632028SHaoyuan Feng    for (i <- 0 until tlbcontiguous) {
13663632028SHaoyuan Feng      this.pm(i).assign_ap(pm(i))
13763632028SHaoyuan Feng    }
138f1fe8698SLemover    this
139f1fe8698SLemover  }
1406d5ddbceSLemover  override def toPrintable: Printable = {
1415b7ef044SLemover    p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " +
1425b7ef044SLemover    p"pm:${pm}"
1436d5ddbceSLemover  }
1446d5ddbceSLemover}
1456d5ddbceSLemover
1466d5ddbceSLemover// multi-read && single-write
1476d5ddbceSLemover// input is data, output is hot-code(not one-hot)
1486d5ddbceSLemoverclass CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule {
1496d5ddbceSLemover  val io = IO(new Bundle {
1506d5ddbceSLemover    val r = new Bundle {
1516d5ddbceSLemover      val req = Input(Vec(readWidth, gen))
1526d5ddbceSLemover      val resp = Output(Vec(readWidth, Vec(set, Bool())))
1536d5ddbceSLemover    }
1546d5ddbceSLemover    val w = Input(new Bundle {
1556d5ddbceSLemover      val valid = Bool()
1566d5ddbceSLemover      val bits = new Bundle {
1576d5ddbceSLemover        val index = UInt(log2Up(set).W)
1586d5ddbceSLemover        val data = gen
1596d5ddbceSLemover      }
1606d5ddbceSLemover    })
1616d5ddbceSLemover  })
1626d5ddbceSLemover
1636d5ddbceSLemover  val wordType = UInt(gen.getWidth.W)
1646d5ddbceSLemover  val array = Reg(Vec(set, wordType))
1656d5ddbceSLemover
1666d5ddbceSLemover  io.r.resp.zipWithIndex.map{ case (a,i) =>
1676d5ddbceSLemover    a := array.map(io.r.req(i).asUInt === _)
1686d5ddbceSLemover  }
1696d5ddbceSLemover
1706d5ddbceSLemover  when (io.w.valid) {
17176e02f07SLingrui98    array(io.w.bits.index) := io.w.bits.data.asUInt
1726d5ddbceSLemover  }
1736d5ddbceSLemover}
1746d5ddbceSLemover
175a0301c0dSLemoverclass TlbEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle {
176a0301c0dSLemover  require(pageNormal || pageSuper)
177a0301c0dSLemover
178a0301c0dSLemover  val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W)
179b0fa7106SHaoyuan Feng  else UInt(vpnLen.W)
180b0fa7106SHaoyuan Feng  val asid = UInt(asidLen.W)
181b0fa7106SHaoyuan Feng  val level = if (!pageNormal) Some(UInt(1.W))
182b0fa7106SHaoyuan Feng  else if (!pageSuper) None
183b0fa7106SHaoyuan Feng  else Some(UInt(2.W))
184b0fa7106SHaoyuan Feng  val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W)
185b0fa7106SHaoyuan Feng  else UInt(ppnLen.W)
186b0fa7106SHaoyuan Feng  val perm = new TlbPermBundle
187b0fa7106SHaoyuan Feng
188b0fa7106SHaoyuan Feng  /** level usage:
189b0fa7106SHaoyuan Feng    *  !PageSuper: page is only normal, level is None, match all the tag
190b0fa7106SHaoyuan Feng    *  !PageNormal: page is only super, level is a Bool(), match high 9*2 parts
191b0fa7106SHaoyuan Feng    *  bits0  0: need mid 9bits
192b0fa7106SHaoyuan Feng    *         1: no need mid 9bits
193b0fa7106SHaoyuan Feng    *  PageSuper && PageNormal: page hold all the three type,
194b0fa7106SHaoyuan Feng    *  bits0  0: need low 9bits
195b0fa7106SHaoyuan Feng    *  bits1  0: need mid 9bits
196b0fa7106SHaoyuan Feng    */
197b0fa7106SHaoyuan Feng
198b0fa7106SHaoyuan Feng  def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false): Bool = {
199b0fa7106SHaoyuan Feng    val asid_hit = if (ignoreAsid) true.B else (this.asid === asid)
200b0fa7106SHaoyuan Feng
201b0fa7106SHaoyuan Feng    // NOTE: for timing, dont care low set index bits at hit check
202b0fa7106SHaoyuan Feng    //       do not need store the low bits actually
203b0fa7106SHaoyuan Feng    if (!pageSuper) asid_hit && drop_set_equal(vpn, tag, nSets)
204b0fa7106SHaoyuan Feng    else if (!pageNormal) {
205b0fa7106SHaoyuan Feng      val tag_match_hi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*3-1, vpnnLen*2)
206b0fa7106SHaoyuan Feng      val tag_match_mi = tag(vpnnLen-1, 0) === vpn(vpnnLen*2-1, vpnnLen)
207b0fa7106SHaoyuan Feng      val tag_match = tag_match_hi && (level.get.asBool() || tag_match_mi)
208b0fa7106SHaoyuan Feng      asid_hit && tag_match
209b0fa7106SHaoyuan Feng    }
210b0fa7106SHaoyuan Feng    else {
211b0fa7106SHaoyuan Feng      val tmp_level = level.get
212b0fa7106SHaoyuan Feng      val tag_match_hi = tag(vpnnLen*3-1, vpnnLen*2) === vpn(vpnnLen*3-1, vpnnLen*2)
213b0fa7106SHaoyuan Feng      val tag_match_mi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*2-1, vpnnLen)
214b0fa7106SHaoyuan Feng      val tag_match_lo = tag(vpnnLen-1, 0) === vpn(vpnnLen-1, 0) // if pageNormal is false, this will always be false
215b0fa7106SHaoyuan Feng      val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo)
216b0fa7106SHaoyuan Feng      asid_hit && tag_match
217b0fa7106SHaoyuan Feng    }
218b0fa7106SHaoyuan Feng  }
219b0fa7106SHaoyuan Feng
220b0fa7106SHaoyuan Feng  def apply(item: PtwSectorResp, asid: UInt, pm: PMPConfig): TlbEntry = {
221b0fa7106SHaoyuan Feng    this.tag := {if (pageNormal) Cat(item.entry.tag, OHToUInt(item.pteidx)) else item.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth)}
222b0fa7106SHaoyuan Feng    this.asid := asid
223b0fa7106SHaoyuan Feng    val inner_level = item.entry.level.getOrElse(0.U)
224b0fa7106SHaoyuan Feng    this.level.map(_ := { if (pageNormal && pageSuper) MuxLookup(inner_level, 0.U, Seq(
225b0fa7106SHaoyuan Feng      0.U -> 3.U,
226b0fa7106SHaoyuan Feng      1.U -> 1.U,
227b0fa7106SHaoyuan Feng      2.U -> 0.U ))
228b0fa7106SHaoyuan Feng    else if (pageSuper) ~inner_level(0)
229b0fa7106SHaoyuan Feng    else 0.U })
230b0fa7106SHaoyuan Feng    this.ppn := { if (!pageNormal) item.entry.ppn(sectorppnLen - 1, vpnnLen - sectortlbwidth)
231b0fa7106SHaoyuan Feng                  else Cat(item.entry.ppn, item.ppn_low(OHToUInt(item.pteidx))) }
232b0fa7106SHaoyuan Feng    this.perm.apply(item, pm)
233b0fa7106SHaoyuan Feng    this
234b0fa7106SHaoyuan Feng  }
235b0fa7106SHaoyuan Feng
236b0fa7106SHaoyuan Feng  // 4KB is normal entry, 2MB/1GB is considered as super entry
237b0fa7106SHaoyuan Feng  def is_normalentry(): Bool = {
238b0fa7106SHaoyuan Feng    if (!pageSuper) { true.B }
239b0fa7106SHaoyuan Feng    else if (!pageNormal) { false.B }
240b0fa7106SHaoyuan Feng    else { level.get === 0.U }
241b0fa7106SHaoyuan Feng  }
242b0fa7106SHaoyuan Feng
243b0fa7106SHaoyuan Feng  def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = {
244b0fa7106SHaoyuan Feng    val inner_level = level.getOrElse(0.U)
245b0fa7106SHaoyuan Feng    val ppn_res = if (!pageSuper) ppn
246b0fa7106SHaoyuan Feng    else if (!pageNormal) Cat(ppn(ppnLen-vpnnLen-1, vpnnLen),
247b0fa7106SHaoyuan Feng      Mux(inner_level(0), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen-1,0)),
248b0fa7106SHaoyuan Feng      vpn(vpnnLen-1, 0))
249b0fa7106SHaoyuan Feng    else Cat(ppn(ppnLen-1, vpnnLen*2),
250b0fa7106SHaoyuan Feng      Mux(inner_level(1), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen*2-1, vpnnLen)),
251b0fa7106SHaoyuan Feng      Mux(inner_level(0), vpn(vpnnLen-1, 0), ppn(vpnnLen-1, 0)))
252b0fa7106SHaoyuan Feng
253b0fa7106SHaoyuan Feng    if (saveLevel) Cat(ppn(ppn.getWidth-1, vpnnLen*2), RegEnable(ppn_res(vpnnLen*2-1, 0), valid))
254b0fa7106SHaoyuan Feng    else ppn_res
255b0fa7106SHaoyuan Feng  }
256b0fa7106SHaoyuan Feng
257b0fa7106SHaoyuan Feng  override def toPrintable: Printable = {
258b0fa7106SHaoyuan Feng    val inner_level = level.getOrElse(2.U)
259b0fa7106SHaoyuan Feng    p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}"
260b0fa7106SHaoyuan Feng  }
261b0fa7106SHaoyuan Feng
262b0fa7106SHaoyuan Feng}
263b0fa7106SHaoyuan Feng
264b0fa7106SHaoyuan Fengclass TlbSectorEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle {
265b0fa7106SHaoyuan Feng  require(pageNormal || pageSuper)
266b0fa7106SHaoyuan Feng
267b0fa7106SHaoyuan Feng  val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W)
26863632028SHaoyuan Feng            else UInt(sectorvpnLen.W)
26945f497a4Shappy-lx  val asid = UInt(asidLen.W)
270a0301c0dSLemover  val level = if (!pageNormal) Some(UInt(1.W))
271a0301c0dSLemover              else if (!pageSuper) None
272a0301c0dSLemover              else Some(UInt(2.W))
273a0301c0dSLemover  val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W)
27463632028SHaoyuan Feng            else UInt(sectorppnLen.W)
275b0fa7106SHaoyuan Feng  val perm = new TlbSectorPermBundle
27663632028SHaoyuan Feng  val valididx = Vec(tlbcontiguous, Bool())
277b0fa7106SHaoyuan Feng  val pteidx = Vec(tlbcontiguous, Bool())
27863632028SHaoyuan Feng  val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W))
279a0301c0dSLemover
28056728e73SLemover  /** level usage:
28156728e73SLemover   *  !PageSuper: page is only normal, level is None, match all the tag
28256728e73SLemover   *  !PageNormal: page is only super, level is a Bool(), match high 9*2 parts
28356728e73SLemover   *  bits0  0: need mid 9bits
28456728e73SLemover   *         1: no need mid 9bits
28556728e73SLemover   *  PageSuper && PageNormal: page hold all the three type,
28656728e73SLemover   *  bits0  0: need low 9bits
28756728e73SLemover   *  bits1  0: need mid 9bits
28856728e73SLemover   */
28956728e73SLemover
290e9092fe2SLemover  def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false): Bool = {
29145f497a4Shappy-lx    val asid_hit = if (ignoreAsid) true.B else (this.asid === asid)
29263632028SHaoyuan Feng    val addr_low_hit = valididx(vpn(2, 0))
293e9092fe2SLemover
294e9092fe2SLemover    // NOTE: for timing, dont care low set index bits at hit check
295e9092fe2SLemover    //       do not need store the low bits actually
29663632028SHaoyuan Feng    if (!pageSuper) asid_hit && drop_set_equal(vpn(vpn.getWidth - 1, sectortlbwidth), tag, nSets) && addr_low_hit
29756728e73SLemover    else if (!pageNormal) {
29856728e73SLemover      val tag_match_hi = tag(vpnnLen * 2 - 1, vpnnLen) === vpn(vpnnLen * 3 - 1, vpnnLen * 2)
29956728e73SLemover      val tag_match_mi = tag(vpnnLen - 1, 0) === vpn(vpnnLen * 2 - 1, vpnnLen)
30056728e73SLemover      val tag_match = tag_match_hi && (level.get.asBool() || tag_match_mi)
30163632028SHaoyuan Feng      asid_hit && tag_match && addr_low_hit
30256728e73SLemover    }
30356728e73SLemover    else {
30456728e73SLemover      val tmp_level = level.get
30563632028SHaoyuan Feng      val tag_match_hi = tag(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2)
30663632028SHaoyuan Feng      val tag_match_mi = tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen)
30763632028SHaoyuan Feng      val tag_match_lo = tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) // if pageNormal is false, this will always be false
30856728e73SLemover      val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo)
30963632028SHaoyuan Feng      asid_hit && tag_match && addr_low_hit
31056728e73SLemover    }
311a0301c0dSLemover  }
312a0301c0dSLemover
31363632028SHaoyuan Feng  def wbhit(data: PtwSectorResp, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false): Bool = {
31463632028SHaoyuan Feng    val vpn = Cat(data.entry.tag, 0.U(sectortlbwidth.W))
31563632028SHaoyuan Feng    val asid_hit = if (ignoreAsid) true.B else (this.asid === asid)
31663632028SHaoyuan Feng    val vpn_hit = Wire(Bool())
31763632028SHaoyuan Feng    val index_hit = Wire(Vec(tlbcontiguous, Bool()))
31863632028SHaoyuan Feng
31963632028SHaoyuan Feng    // NOTE: for timing, dont care low set index bits at hit check
32063632028SHaoyuan Feng    //       do not need store the low bits actually
32163632028SHaoyuan Feng    if (!pageSuper) {
32263632028SHaoyuan Feng      vpn_hit := asid_hit && drop_set_equal(vpn(vpn.getWidth - 1, sectortlbwidth), tag, nSets)
32363632028SHaoyuan Feng    }
32463632028SHaoyuan Feng    else if (!pageNormal) {
32563632028SHaoyuan Feng      val tag_match_hi = tag(vpnnLen * 2 - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2)
32663632028SHaoyuan Feng      val tag_match_mi = tag(vpnnLen - 1, 0) === vpn(vpnnLen * 2 - 1, vpnnLen)
32763632028SHaoyuan Feng      val tag_match = tag_match_hi && (level.get.asBool() || tag_match_mi)
32863632028SHaoyuan Feng      vpn_hit := asid_hit && tag_match
32963632028SHaoyuan Feng    }
33063632028SHaoyuan Feng    else {
33163632028SHaoyuan Feng      val tmp_level = level.get
33263632028SHaoyuan Feng      val tag_match_hi = tag(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2)
33363632028SHaoyuan Feng      val tag_match_mi = tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen)
33463632028SHaoyuan Feng      val tag_match_lo = tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) // if pageNormal is false, this will always be false
33563632028SHaoyuan Feng      val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo)
33663632028SHaoyuan Feng      vpn_hit := asid_hit && tag_match
33763632028SHaoyuan Feng    }
33863632028SHaoyuan Feng
33963632028SHaoyuan Feng    for (i <- 0 until tlbcontiguous) {
34063632028SHaoyuan Feng      index_hit(i) := data.valididx(i) && valididx(i)
34163632028SHaoyuan Feng    }
34263632028SHaoyuan Feng
34363632028SHaoyuan Feng    // For example, tlb req to page cache with vpn 0x10
34463632028SHaoyuan Feng    // At this time, 0x13 has not been paged, so page cache only resp 0x10
34563632028SHaoyuan Feng    // When 0x13 refill to page cache, previous item will be flushed
34663632028SHaoyuan Feng    // Now 0x10 and 0x13 are both valid in page cache
34763632028SHaoyuan Feng    // However, when 0x13 refill to tlb, will trigger multi hit
34863632028SHaoyuan Feng    // So will only trigger multi-hit when PopCount(data.valididx) = 1
34963632028SHaoyuan Feng    vpn_hit && index_hit.reduce(_ || _) && PopCount(data.valididx) === 1.U
35063632028SHaoyuan Feng  }
35163632028SHaoyuan Feng
352b0fa7106SHaoyuan Feng  def apply(item: PtwSectorResp, asid: UInt, pm: Seq[PMPConfig]): TlbSectorEntry = {
35363632028SHaoyuan Feng    this.tag := {if (pageNormal) item.entry.tag else item.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth)}
35445f497a4Shappy-lx    this.asid := asid
355a0301c0dSLemover    val inner_level = item.entry.level.getOrElse(0.U)
35656728e73SLemover    this.level.map(_ := { if (pageNormal && pageSuper) MuxLookup(inner_level, 0.U, Seq(
35756728e73SLemover                                                        0.U -> 3.U,
35856728e73SLemover                                                        1.U -> 1.U,
35956728e73SLemover                                                        2.U -> 0.U ))
36056728e73SLemover                          else if (pageSuper) ~inner_level(0)
361a0301c0dSLemover                          else 0.U })
36263632028SHaoyuan Feng    this.ppn := { if (!pageNormal) item.entry.ppn(sectorppnLen - 1, vpnnLen - sectortlbwidth)
363a0301c0dSLemover                  else item.entry.ppn }
364f1fe8698SLemover    this.perm.apply(item, pm)
36563632028SHaoyuan Feng    this.ppn_low := item.ppn_low
36663632028SHaoyuan Feng    this.valididx := item.valididx
367b0fa7106SHaoyuan Feng    this.pteidx := item.pteidx
368a0301c0dSLemover    this
369a0301c0dSLemover  }
370a0301c0dSLemover
37156728e73SLemover  // 4KB is normal entry, 2MB/1GB is considered as super entry
37256728e73SLemover  def is_normalentry(): Bool = {
37356728e73SLemover    if (!pageSuper) { true.B }
37456728e73SLemover    else if (!pageNormal) { false.B }
37556728e73SLemover    else { level.get === 0.U }
37656728e73SLemover  }
3775cf62c1aSLemover
37856728e73SLemover  def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = {
37956728e73SLemover    val inner_level = level.getOrElse(0.U)
38063632028SHaoyuan Feng    val ppn_res = if (!pageSuper) Cat(ppn, ppn_low(vpn(sectortlbwidth - 1, 0)))
38156728e73SLemover      else if (!pageNormal) Cat(ppn(ppnLen - vpnnLen - 1, vpnnLen),
38256728e73SLemover        Mux(inner_level(0), vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen - 1,0)),
38356728e73SLemover        vpn(vpnnLen - 1, 0))
38463632028SHaoyuan Feng      else Cat(ppn(sectorppnLen - 1, vpnnLen * 2 - sectortlbwidth),
38563632028SHaoyuan Feng        Mux(inner_level(1), vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth)),
38663632028SHaoyuan Feng        Mux(inner_level(0), vpn(vpnnLen - 1, 0), Cat(ppn(vpnnLen - sectortlbwidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0)))))
38756728e73SLemover
38863632028SHaoyuan Feng    if (saveLevel) {
38963632028SHaoyuan Feng      if (ppn.getWidth == ppnLen - vpnnLen) {
39063632028SHaoyuan Feng        Cat(ppn(ppn.getWidth - 1, vpnnLen * 2), RegEnable(ppn_res(vpnnLen * 2 - 1, 0), valid))
39163632028SHaoyuan Feng      } else {
39263632028SHaoyuan Feng        require(ppn.getWidth == sectorppnLen)
39363632028SHaoyuan Feng        Cat(ppn(ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), RegEnable(ppn_res(vpnnLen * 2 - 1, 0), valid))
39463632028SHaoyuan Feng      }
39563632028SHaoyuan Feng    }
3965cf62c1aSLemover    else ppn_res
397a0301c0dSLemover  }
398a0301c0dSLemover
399a0301c0dSLemover  override def toPrintable: Printable = {
400a0301c0dSLemover    val inner_level = level.getOrElse(2.U)
40145f497a4Shappy-lx    p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}"
402a0301c0dSLemover  }
403a0301c0dSLemover
404a0301c0dSLemover}
405a0301c0dSLemover
4066d5ddbceSLemoverobject TlbCmd {
4076d5ddbceSLemover  def read  = "b00".U
4086d5ddbceSLemover  def write = "b01".U
4096d5ddbceSLemover  def exec  = "b10".U
4106d5ddbceSLemover
4116d5ddbceSLemover  def atom_read  = "b100".U // lr
4126d5ddbceSLemover  def atom_write = "b101".U // sc / amo
4136d5ddbceSLemover
4146d5ddbceSLemover  def apply() = UInt(3.W)
4156d5ddbceSLemover  def isRead(a: UInt) = a(1,0)===read
4166d5ddbceSLemover  def isWrite(a: UInt) = a(1,0)===write
4176d5ddbceSLemover  def isExec(a: UInt) = a(1,0)===exec
4186d5ddbceSLemover
4196d5ddbceSLemover  def isAtom(a: UInt) = a(2)
420a79fef67Swakafa  def isAmo(a: UInt) = a===atom_write // NOTE: sc mixed
4216d5ddbceSLemover}
4226d5ddbceSLemover
42303efd994Shappy-lxclass TlbStorageIO(nSets: Int, nWays: Int, ports: Int, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle {
424a0301c0dSLemover  val r = new Bundle {
425a0301c0dSLemover    val req = Vec(ports, Flipped(DecoupledIO(new Bundle {
426a0301c0dSLemover      val vpn = Output(UInt(vpnLen.W))
427a0301c0dSLemover    })))
428a0301c0dSLemover    val resp = Vec(ports, ValidIO(new Bundle{
429a0301c0dSLemover      val hit = Output(Bool())
43003efd994Shappy-lx      val ppn = Vec(nDups, Output(UInt(ppnLen.W)))
431b0fa7106SHaoyuan Feng      val perm = Vec(nDups, Output(new TlbSectorPermBundle()))
432a0301c0dSLemover    }))
433a0301c0dSLemover  }
434a0301c0dSLemover  val w = Flipped(ValidIO(new Bundle {
435a0301c0dSLemover    val wayIdx = Output(UInt(log2Up(nWays).W))
43663632028SHaoyuan Feng    val data = Output(new PtwSectorResp)
43763632028SHaoyuan Feng    val data_replenish = Vec(tlbcontiguous, Output(new PMPConfig))
438a0301c0dSLemover  }))
439a0301c0dSLemover  val victim = new Bundle {
44045f497a4Shappy-lx    val out = ValidIO(Output(new Bundle {
44145f497a4Shappy-lx      val entry = new TlbEntry(pageNormal = true, pageSuper = false)
44245f497a4Shappy-lx    }))
44345f497a4Shappy-lx    val in = Flipped(ValidIO(Output(new Bundle {
44445f497a4Shappy-lx      val entry = new TlbEntry(pageNormal = true, pageSuper = false)
44545f497a4Shappy-lx    })))
446a0301c0dSLemover  }
4473889e11eSLemover  val access = Vec(ports, new ReplaceAccessBundle(nSets, nWays))
448a0301c0dSLemover
449f1fe8698SLemover  def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = {
450a0301c0dSLemover    this.r.req(i).valid := valid
451a0301c0dSLemover    this.r.req(i).bits.vpn := vpn
452a0301c0dSLemover  }
453a0301c0dSLemover
454a0301c0dSLemover  def r_resp_apply(i: Int) = {
455f1fe8698SLemover    (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm)
456a0301c0dSLemover  }
457a0301c0dSLemover
45863632028SHaoyuan Feng  def w_apply(valid: Bool, wayIdx: UInt, data: PtwSectorResp, data_replenish: Seq[PMPConfig]): Unit = {
459a0301c0dSLemover    this.w.valid := valid
460a0301c0dSLemover    this.w.bits.wayIdx := wayIdx
461a0301c0dSLemover    this.w.bits.data := data
4625b7ef044SLemover    this.w.bits.data_replenish := data_replenish
463a0301c0dSLemover  }
464a0301c0dSLemover
465a0301c0dSLemover}
466a0301c0dSLemover
46703efd994Shappy-lxclass TlbStorageWrapperIO(ports: Int, q: TLBParameters, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle {
468f1fe8698SLemover  val r = new Bundle {
469f1fe8698SLemover    val req = Vec(ports, Flipped(DecoupledIO(new Bundle {
470f1fe8698SLemover      val vpn = Output(UInt(vpnLen.W))
471f1fe8698SLemover    })))
472f1fe8698SLemover    val resp = Vec(ports, ValidIO(new Bundle{
473f1fe8698SLemover      val hit = Output(Bool())
47403efd994Shappy-lx      val ppn = Vec(nDups, Output(UInt(ppnLen.W)))
47503efd994Shappy-lx      val perm = Vec(nDups, Output(new TlbPermBundle()))
476f1fe8698SLemover      // below are dirty code for timing optimization
477f1fe8698SLemover      val super_hit = Output(Bool())
478f1fe8698SLemover      val super_ppn = Output(UInt(ppnLen.W))
479f1fe8698SLemover      val spm = Output(new TlbPMBundle)
480f1fe8698SLemover    }))
481f1fe8698SLemover  }
482f1fe8698SLemover  val w = Flipped(ValidIO(new Bundle {
48363632028SHaoyuan Feng    val data = Output(new PtwSectorResp)
48463632028SHaoyuan Feng    val data_replenish = Vec(tlbcontiguous, Output(new PMPConfig))
485f1fe8698SLemover  }))
486f1fe8698SLemover  val replace = if (q.outReplace) Flipped(new TlbReplaceIO(ports, q)) else null
487f1fe8698SLemover
488f1fe8698SLemover  def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = {
489f1fe8698SLemover    this.r.req(i).valid := valid
490f1fe8698SLemover    this.r.req(i).bits.vpn := vpn
491f1fe8698SLemover  }
492f1fe8698SLemover
493f1fe8698SLemover  def r_resp_apply(i: Int) = {
494f1fe8698SLemover    (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm,
495f1fe8698SLemover    this.r.resp(i).bits.super_hit, this.r.resp(i).bits.super_ppn, this.r.resp(i).bits.spm)
496f1fe8698SLemover  }
497f1fe8698SLemover
49863632028SHaoyuan Feng  def w_apply(valid: Bool, data: PtwSectorResp, data_replenish: Seq[PMPConfig]): Unit = {
499f1fe8698SLemover    this.w.valid := valid
500f1fe8698SLemover    this.w.bits.data := data
501f1fe8698SLemover    this.w.bits.data_replenish := data_replenish
502f1fe8698SLemover  }
503f1fe8698SLemover}
504f1fe8698SLemover
5053889e11eSLemoverclass ReplaceAccessBundle(nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle {
5063889e11eSLemover  val sets = Output(UInt(log2Up(nSets).W))
5073889e11eSLemover  val touch_ways = ValidIO(Output(UInt(log2Up(nWays).W)))
5083889e11eSLemover}
5093889e11eSLemover
510a0301c0dSLemoverclass ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle {
5113889e11eSLemover  val access = Vec(Width, Flipped(new ReplaceAccessBundle(nSets, nWays)))
512a0301c0dSLemover
513a0301c0dSLemover  val refillIdx = Output(UInt(log2Up(nWays).W))
514a0301c0dSLemover  val chosen_set = Flipped(Output(UInt(log2Up(nSets).W)))
515a0301c0dSLemover
516a0301c0dSLemover  def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = {
51753b8f1a7SLemover    for ((ac_rep, ac_tlb) <- access.zip(in.map(a => a.access.map(b => b)).flatten)) {
51853b8f1a7SLemover      ac_rep := ac_tlb
519a0301c0dSLemover    }
52053b8f1a7SLemover    this.chosen_set := get_set_idx(vpn, nSets)
52153b8f1a7SLemover    in.map(a => a.refillIdx := this.refillIdx)
522a0301c0dSLemover  }
523a0301c0dSLemover}
524a0301c0dSLemover
525a0301c0dSLemoverclass TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends
526a0301c0dSLemover  TlbBundle {
527a0301c0dSLemover  val normalPage = new ReplaceIO(Width, q.normalNSets, q.normalNWays)
528a0301c0dSLemover  val superPage = new ReplaceIO(Width, q.superNSets, q.superNWays)
529a0301c0dSLemover
530a0301c0dSLemover  def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = {
531a0301c0dSLemover    this.normalPage.apply_sep(in.map(_.normalPage), vpn)
532a0301c0dSLemover    this.superPage.apply_sep(in.map(_.superPage), vpn)
533a0301c0dSLemover  }
534a0301c0dSLemover
535a0301c0dSLemover}
536a0301c0dSLemover
5378744445eSMaxpicca-Liclass MemBlockidxBundle(implicit p: Parameters) extends TlbBundle {
5388744445eSMaxpicca-Li  val is_ld = Bool()
5398744445eSMaxpicca-Li  val is_st = Bool()
5408744445eSMaxpicca-Li  val idx =
541*e4f69d78Ssfencevma    if (VirtualLoadQueueSize >= StoreQueueSize) {
542*e4f69d78Ssfencevma      val idx = UInt(log2Ceil(VirtualLoadQueueSize).W)
5438744445eSMaxpicca-Li      idx
5448744445eSMaxpicca-Li    } else {
5458744445eSMaxpicca-Li      val idx = UInt(log2Ceil(StoreQueueSize).W)
5468744445eSMaxpicca-Li      idx
5478744445eSMaxpicca-Li    }
5488744445eSMaxpicca-Li}
5498744445eSMaxpicca-Li
5506d5ddbceSLemoverclass TlbReq(implicit p: Parameters) extends TlbBundle {
551ca2f90a6SLemover  val vaddr = Output(UInt(VAddrBits.W))
552ca2f90a6SLemover  val cmd = Output(TlbCmd())
553ca2f90a6SLemover  val size = Output(UInt(log2Ceil(log2Ceil(XLEN/8)+1).W))
554f1fe8698SLemover  val kill = Output(Bool()) // Use for blocked tlb that need sync with other module like icache
5558744445eSMaxpicca-Li  val memidx = Output(new MemBlockidxBundle)
556b52348aeSWilliam Wang  // do not translate, but still do pmp/pma check
557b52348aeSWilliam Wang  val no_translate = Output(Bool())
5586d5ddbceSLemover  val debug = new Bundle {
559ca2f90a6SLemover    val pc = Output(UInt(XLEN.W))
560f1fe8698SLemover    val robIdx = Output(new RobPtr)
561ca2f90a6SLemover    val isFirstIssue = Output(Bool())
5626d5ddbceSLemover  }
5636d5ddbceSLemover
564f1fe8698SLemover  // Maybe Block req needs a kill: for itlb, itlb and icache may not sync, itlb should wait icache to go ahead
5656d5ddbceSLemover  override def toPrintable: Printable = {
566f1fe8698SLemover    p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} kill:${kill} pc:0x${Hexadecimal(debug.pc)} robIdx:${debug.robIdx}"
5676d5ddbceSLemover  }
5686d5ddbceSLemover}
5696d5ddbceSLemover
570b6982e83SLemoverclass TlbExceptionBundle(implicit p: Parameters) extends TlbBundle {
571b6982e83SLemover  val ld = Output(Bool())
572b6982e83SLemover  val st = Output(Bool())
573b6982e83SLemover  val instr = Output(Bool())
574b6982e83SLemover}
575b6982e83SLemover
57603efd994Shappy-lxclass TlbResp(nDups: Int = 1)(implicit p: Parameters) extends TlbBundle {
57703efd994Shappy-lx  val paddr = Vec(nDups, Output(UInt(PAddrBits.W)))
578ca2f90a6SLemover  val miss = Output(Bool())
579cccfc98dSLemover  val fast_miss = Output(Bool()) // without sram part for timing optimization
58003efd994Shappy-lx  val excp = Vec(nDups, new Bundle {
581b6982e83SLemover    val pf = new TlbExceptionBundle()
582b6982e83SLemover    val af = new TlbExceptionBundle()
58303efd994Shappy-lx  })
5845b7ef044SLemover  val static_pm = Output(Valid(Bool())) // valid for static, bits for mmio result from normal entries
585ca2f90a6SLemover  val ptwBack = Output(Bool()) // when ptw back, wake up replay rs's state
5868744445eSMaxpicca-Li  val memidx = Output(new MemBlockidxBundle)
5876d5ddbceSLemover
5888744445eSMaxpicca-Li  val debug = new Bundle {
5898744445eSMaxpicca-Li    val robIdx = Output(new RobPtr)
5908744445eSMaxpicca-Li    val isFirstIssue = Output(Bool())
5918744445eSMaxpicca-Li  }
5926d5ddbceSLemover  override def toPrintable: Printable = {
59303efd994Shappy-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}"
5946d5ddbceSLemover  }
5956d5ddbceSLemover}
5966d5ddbceSLemover
59703efd994Shappy-lxclass TlbRequestIO(nRespDups: Int = 1)(implicit p: Parameters) extends TlbBundle {
5986d5ddbceSLemover  val req = DecoupledIO(new TlbReq)
599c3b763d0SYinan Xu  val req_kill = Output(Bool())
60003efd994Shappy-lx  val resp = Flipped(DecoupledIO(new TlbResp(nRespDups)))
6016d5ddbceSLemover}
6026d5ddbceSLemover
6036d5ddbceSLemoverclass TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle {
6046d5ddbceSLemover  val req = Vec(Width, DecoupledIO(new PtwReq))
60563632028SHaoyuan Feng  val resp = Flipped(DecoupledIO(new PtwSectorResp))
6066d5ddbceSLemover
6076d5ddbceSLemover
6086d5ddbceSLemover  override def toPrintable: Printable = {
6096d5ddbceSLemover    p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}"
6106d5ddbceSLemover  }
6116d5ddbceSLemover}
6126d5ddbceSLemover
6138744445eSMaxpicca-Liclass TlbPtwIOwithMemIdx(Width: Int = 1)(implicit p: Parameters) extends TlbBundle {
6148744445eSMaxpicca-Li  val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx))
61563632028SHaoyuan Feng  val resp = Flipped(DecoupledIO(new PtwSectorRespwithMemIdx))
6168744445eSMaxpicca-Li
6178744445eSMaxpicca-Li
6188744445eSMaxpicca-Li  override def toPrintable: Printable = {
6198744445eSMaxpicca-Li    p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}"
6208744445eSMaxpicca-Li  }
6218744445eSMaxpicca-Li}
6228744445eSMaxpicca-Li
62345f497a4Shappy-lxclass MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle {
624b052b972SLemover  val sfence = Input(new SfenceBundle)
625b052b972SLemover  val csr = Input(new TlbCsrBundle)
626f1fe8698SLemover
627f1fe8698SLemover  def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle): Unit = {
628f1fe8698SLemover    this.sfence <> sfence
629f1fe8698SLemover    this.csr <> csr
630f1fe8698SLemover  }
631f1fe8698SLemover
632f1fe8698SLemover  // overwrite satp. write satp will cause flushpipe but csr.priv won't
633f1fe8698SLemover  // satp will be dealyed several cycles from writing, but csr.priv won't
634f1fe8698SLemover  // so inside mmu, these two signals should be divided
635f1fe8698SLemover  def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle, satp: TlbSatpBundle) = {
636f1fe8698SLemover    this.sfence <> sfence
637f1fe8698SLemover    this.csr <> csr
638f1fe8698SLemover    this.csr.satp := satp
639f1fe8698SLemover  }
640a0301c0dSLemover}
6416d5ddbceSLemover
6428744445eSMaxpicca-Liclass TlbRefilltoMemIO()(implicit p: Parameters) extends TlbBundle {
6438744445eSMaxpicca-Li  val valid = Bool()
6448744445eSMaxpicca-Li  val memidx = new MemBlockidxBundle
6458744445eSMaxpicca-Li}
6468744445eSMaxpicca-Li
64703efd994Shappy-lxclass TlbIO(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends
64845f497a4Shappy-lx  MMUIOBaseBundle {
64903efd994Shappy-lx  val requestor = Vec(Width, Flipped(new TlbRequestIO(nRespDups)))
650f1fe8698SLemover  val flushPipe = Vec(Width, Input(Bool()))
6518744445eSMaxpicca-Li  val ptw = new TlbPtwIOwithMemIdx(Width)
6528744445eSMaxpicca-Li  val refill_to_mem = Output(new TlbRefilltoMemIO())
65363632028SHaoyuan Feng  val ptw_replenish = Vec(tlbcontiguous, Input(new PMPConfig()))
654a0301c0dSLemover  val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null
655b6982e83SLemover  val pmp = Vec(Width, ValidIO(new PMPReqBundle()))
656a0301c0dSLemover
657a0301c0dSLemover}
658a0301c0dSLemover
659f1fe8698SLemoverclass VectorTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle {
6608744445eSMaxpicca-Li  val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx()))
661a0301c0dSLemover  val resp = Flipped(DecoupledIO(new Bundle {
66263632028SHaoyuan Feng    val data = new PtwSectorRespwithMemIdx
663a0301c0dSLemover    val vector = Output(Vec(Width, Bool()))
664a0301c0dSLemover  }))
665a0301c0dSLemover
6668744445eSMaxpicca-Li  def connect(normal: TlbPtwIOwithMemIdx): Unit = {
667f1fe8698SLemover    req <> normal.req
668f1fe8698SLemover    resp.ready := normal.resp.ready
669f1fe8698SLemover    normal.resp.bits := resp.bits.data
670f1fe8698SLemover    normal.resp.valid := resp.valid
671a0301c0dSLemover  }
6726d5ddbceSLemover}
6736d5ddbceSLemover
67492e3bfefSLemover/****************************  L2TLB  *************************************/
6756d5ddbceSLemoverabstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst
67692e3bfefSLemoverabstract class PtwModule(outer: L2TLB) extends LazyModuleImp(outer)
6776d5ddbceSLemover  with HasXSParameter with HasPtwConst
6786d5ddbceSLemover
6796d5ddbceSLemoverclass PteBundle(implicit p: Parameters) extends PtwBundle{
6806d5ddbceSLemover  val reserved  = UInt(pteResLen.W)
6810d94d540SHaoyuan Feng  val ppn_high = UInt(ppnHignLen.W)
6826d5ddbceSLemover  val ppn  = UInt(ppnLen.W)
6836d5ddbceSLemover  val rsw  = UInt(2.W)
6846d5ddbceSLemover  val perm = new Bundle {
6856d5ddbceSLemover    val d    = Bool()
6866d5ddbceSLemover    val a    = Bool()
6876d5ddbceSLemover    val g    = Bool()
6886d5ddbceSLemover    val u    = Bool()
6896d5ddbceSLemover    val x    = Bool()
6906d5ddbceSLemover    val w    = Bool()
6916d5ddbceSLemover    val r    = Bool()
6926d5ddbceSLemover    val v    = Bool()
6936d5ddbceSLemover  }
6946d5ddbceSLemover
6956d5ddbceSLemover  def unaligned(level: UInt) = {
6966d5ddbceSLemover    isLeaf() && !(level === 2.U ||
6976d5ddbceSLemover                  level === 1.U && ppn(vpnnLen-1,   0) === 0.U ||
6986d5ddbceSLemover                  level === 0.U && ppn(vpnnLen*2-1, 0) === 0.U)
6996d5ddbceSLemover  }
7006d5ddbceSLemover
7016d5ddbceSLemover  def isPf(level: UInt) = {
7026d5ddbceSLemover    !perm.v || (!perm.r && perm.w) || unaligned(level)
7036d5ddbceSLemover  }
7046d5ddbceSLemover
7050d94d540SHaoyuan Feng  // paddr of Xiangshan is 36 bits but ppn of sv39 is 44 bits
7060d94d540SHaoyuan Feng  // access fault will be raised when ppn >> ppnLen is not zero
7070d94d540SHaoyuan Feng  def isAf() = {
7080d94d540SHaoyuan Feng    !(ppn_high === 0.U)
7090d94d540SHaoyuan Feng  }
7100d94d540SHaoyuan Feng
7116d5ddbceSLemover  def isLeaf() = {
7126d5ddbceSLemover    perm.r || perm.x || perm.w
7136d5ddbceSLemover  }
7146d5ddbceSLemover
7156d5ddbceSLemover  def getPerm() = {
7166d5ddbceSLemover    val pm = Wire(new PtePermBundle)
7176d5ddbceSLemover    pm.d := perm.d
7186d5ddbceSLemover    pm.a := perm.a
7196d5ddbceSLemover    pm.g := perm.g
7206d5ddbceSLemover    pm.u := perm.u
7216d5ddbceSLemover    pm.x := perm.x
7226d5ddbceSLemover    pm.w := perm.w
7236d5ddbceSLemover    pm.r := perm.r
7246d5ddbceSLemover    pm
7256d5ddbceSLemover  }
7266d5ddbceSLemover
7276d5ddbceSLemover  override def toPrintable: Printable = {
7286d5ddbceSLemover    p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}"
7296d5ddbceSLemover  }
7306d5ddbceSLemover}
7316d5ddbceSLemover
7326d5ddbceSLemoverclass PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle {
7336d5ddbceSLemover  val tag = UInt(tagLen.W)
73445f497a4Shappy-lx  val asid = UInt(asidLen.W)
7356d5ddbceSLemover  val ppn = UInt(ppnLen.W)
7366d5ddbceSLemover  val perm = if (hasPerm) Some(new PtePermBundle) else None
7376d5ddbceSLemover  val level = if (hasLevel) Some(UInt(log2Up(Level).W)) else None
738bc063562SLemover  val prefetch = Bool()
7398d8ac704SLemover  val v = Bool()
7406d5ddbceSLemover
74156728e73SLemover  def is_normalentry(): Bool = {
74256728e73SLemover    if (!hasLevel) true.B
74356728e73SLemover    else level.get === 2.U
74456728e73SLemover  }
74556728e73SLemover
746f1fe8698SLemover  def genPPN(vpn: UInt): UInt = {
747f1fe8698SLemover    if (!hasLevel) ppn
748f1fe8698SLemover    else MuxLookup(level.get, 0.U, Seq(
749f1fe8698SLemover          0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)),
750f1fe8698SLemover          1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)),
751f1fe8698SLemover          2.U -> ppn)
752f1fe8698SLemover    )
753f1fe8698SLemover  }
754f1fe8698SLemover
75545f497a4Shappy-lx  def hit(vpn: UInt, asid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false) = {
7566d5ddbceSLemover    require(vpn.getWidth == vpnLen)
757cccfc98dSLemover//    require(this.asid.getWidth <= asid.getWidth)
75845f497a4Shappy-lx    val asid_hit = if (ignoreAsid) true.B else (this.asid === asid)
7596d5ddbceSLemover    if (allType) {
7606d5ddbceSLemover      require(hasLevel)
7616d5ddbceSLemover      val hit0 = tag(tagLen - 1,    vpnnLen*2) === vpn(tagLen - 1, vpnnLen*2)
7626d5ddbceSLemover      val hit1 = tag(vpnnLen*2 - 1, vpnnLen)   === vpn(vpnnLen*2 - 1,  vpnnLen)
7636d5ddbceSLemover      val hit2 = tag(vpnnLen - 1,     0)         === vpn(vpnnLen - 1, 0)
76445f497a4Shappy-lx
76545f497a4Shappy-lx      asid_hit && Mux(level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0))
7666d5ddbceSLemover    } else if (hasLevel) {
7676d5ddbceSLemover      val hit0 = tag(tagLen - 1, tagLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen)
7686d5ddbceSLemover      val hit1 = tag(tagLen - vpnnLen - 1, tagLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2)
76945f497a4Shappy-lx
77045f497a4Shappy-lx      asid_hit && Mux(level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1)
7716d5ddbceSLemover    } else {
77245f497a4Shappy-lx      asid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen)
7736d5ddbceSLemover    }
7746d5ddbceSLemover  }
7756d5ddbceSLemover
7768d8ac704SLemover  def refill(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) {
77745f497a4Shappy-lx    require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside
77845f497a4Shappy-lx
7796d5ddbceSLemover    tag := vpn(vpnLen - 1, vpnLen - tagLen)
780a0301c0dSLemover    ppn := pte.asTypeOf(new PteBundle().cloneType).ppn
781a0301c0dSLemover    perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm)
78245f497a4Shappy-lx    this.asid := asid
783bc063562SLemover    this.prefetch := prefetch
7848d8ac704SLemover    this.v := valid
7856d5ddbceSLemover    this.level.map(_ := level)
7866d5ddbceSLemover  }
7876d5ddbceSLemover
7888d8ac704SLemover  def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) = {
7896d5ddbceSLemover    val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel))
7908d8ac704SLemover    e.refill(vpn, asid, pte, level, prefetch, valid)
7916d5ddbceSLemover    e
7926d5ddbceSLemover  }
7936d5ddbceSLemover
7946d5ddbceSLemover
795f1fe8698SLemover
7966d5ddbceSLemover  override def toPrintable: Printable = {
7976d5ddbceSLemover    // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}"
7986d5ddbceSLemover    p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} " +
7996d5ddbceSLemover      (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") +
800bc063562SLemover      (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") +
801bc063562SLemover      p"prefetch:${prefetch}"
8026d5ddbceSLemover  }
8036d5ddbceSLemover}
8046d5ddbceSLemover
80563632028SHaoyuan Fengclass PtwSectorEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwEntry(tagLen, hasPerm, hasLevel) {
80663632028SHaoyuan Feng  override val ppn = UInt(sectorppnLen.W)
80763632028SHaoyuan Feng}
80863632028SHaoyuan Feng
80963632028SHaoyuan Fengclass PtwMergeEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwSectorEntry(tagLen, hasPerm, hasLevel) {
81063632028SHaoyuan Feng  val ppn_low = UInt(sectortlbwidth.W)
81163632028SHaoyuan Feng  val af = Bool()
81263632028SHaoyuan Feng  val pf = Bool()
81363632028SHaoyuan Feng}
81463632028SHaoyuan Feng
8156d5ddbceSLemoverclass PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle {
8166d5ddbceSLemover  require(log2Up(num)==log2Down(num))
8171f4a7c0cSLemover  // NOTE: hasPerm means that is leaf or not.
8186d5ddbceSLemover
8196d5ddbceSLemover  val tag  = UInt(tagLen.W)
82045f497a4Shappy-lx  val asid = UInt(asidLen.W)
8216d5ddbceSLemover  val ppns = Vec(num, UInt(ppnLen.W))
8226d5ddbceSLemover  val vs   = Vec(num, Bool())
8236d5ddbceSLemover  val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None
824bc063562SLemover  val prefetch = Bool()
8256d5ddbceSLemover  // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1")
8261f4a7c0cSLemover  // NOTE: vs is used for different usage:
8271f4a7c0cSLemover  // for l3, which store the leaf(leaves), vs is page fault or not.
8281f4a7c0cSLemover  // for l2, which shoule not store leaf, vs is valid or not, that will anticipate in hit check
8291f4a7c0cSLemover  // Because, l2 should not store leaf(no perm), it doesn't store perm.
8301f4a7c0cSLemover  // If l2 hit a leaf, the perm is still unavailble. Should still page walk. Complex but nothing helpful.
8311f4a7c0cSLemover  // TODO: divide vs into validVec and pfVec
8321f4a7c0cSLemover  // for l2: may valid but pf, so no need for page walk, return random pte with pf.
8336d5ddbceSLemover
8346d5ddbceSLemover  def tagClip(vpn: UInt) = {
8356d5ddbceSLemover    require(vpn.getWidth == vpnLen)
8366d5ddbceSLemover    vpn(vpnLen - 1, vpnLen - tagLen)
8376d5ddbceSLemover  }
8386d5ddbceSLemover
8396d5ddbceSLemover  def sectorIdxClip(vpn: UInt, level: Int) = {
8406d5ddbceSLemover    getVpnClip(vpn, level)(log2Up(num) - 1, 0)
8416d5ddbceSLemover  }
8426d5ddbceSLemover
84345f497a4Shappy-lx  def hit(vpn: UInt, asid: UInt, ignoreAsid: Boolean = false) = {
84445f497a4Shappy-lx    val asid_hit = if (ignoreAsid) true.B else (this.asid === asid)
8451f4a7c0cSLemover    asid_hit && tag === tagClip(vpn) && (if (hasPerm) true.B else vs(sectorIdxClip(vpn, level)))
8466d5ddbceSLemover  }
8476d5ddbceSLemover
84845f497a4Shappy-lx  def genEntries(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = {
8496d5ddbceSLemover    require((data.getWidth / XLEN) == num,
8505854c1edSLemover      s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}")
8516d5ddbceSLemover
8526d5ddbceSLemover    val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm))
8536d5ddbceSLemover    ps.tag := tagClip(vpn)
85445f497a4Shappy-lx    ps.asid := asid
855bc063562SLemover    ps.prefetch := prefetch
8566d5ddbceSLemover    for (i <- 0 until num) {
8576d5ddbceSLemover      val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle)
8586d5ddbceSLemover      ps.ppns(i) := pte.ppn
8596d5ddbceSLemover      ps.vs(i)   := !pte.isPf(levelUInt) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf())
8606d5ddbceSLemover      ps.perms.map(_(i) := pte.perm)
8616d5ddbceSLemover    }
8626d5ddbceSLemover    ps
8636d5ddbceSLemover  }
8646d5ddbceSLemover
8656d5ddbceSLemover  override def toPrintable: Printable = {
8666d5ddbceSLemover    // require(num == 4, "if num is not 4, please comment this toPrintable")
8676d5ddbceSLemover    // NOTE: if num is not 4, please comment this toPrintable
8686d5ddbceSLemover    val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle)))
86945f497a4Shappy-lx    p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " +
8706d5ddbceSLemover      (if (hasPerm) p"perms:${printVec(permsInner)}" else p"")
8716d5ddbceSLemover  }
8726d5ddbceSLemover}
8736d5ddbceSLemover
8747196f5a2SLemoverclass PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle {
8757196f5a2SLemover  val entries = new PtwEntries(num, tagLen, level, hasPerm)
8767196f5a2SLemover
8773889e11eSLemover  val ecc_block = XLEN
8783889e11eSLemover  val ecc_info = get_ecc_info()
8793889e11eSLemover  val ecc = UInt(ecc_info._1.W)
8803889e11eSLemover
8813889e11eSLemover  def get_ecc_info(): (Int, Int, Int, Int) = {
8823889e11eSLemover    val eccBits_per = eccCode.width(ecc_block) - ecc_block
8833889e11eSLemover
8843889e11eSLemover    val data_length = entries.getWidth
8853889e11eSLemover    val data_align_num = data_length / ecc_block
8863889e11eSLemover    val data_not_align = (data_length % ecc_block) != 0 // ugly code
8873889e11eSLemover    val data_unalign_length = data_length - data_align_num * ecc_block
8883889e11eSLemover    val eccBits_unalign = eccCode.width(data_unalign_length) - data_unalign_length
8893889e11eSLemover
8903889e11eSLemover    val eccBits = eccBits_per * data_align_num + eccBits_unalign
8913889e11eSLemover    (eccBits, eccBits_per, data_align_num, data_unalign_length)
8923889e11eSLemover  }
8933889e11eSLemover
8943889e11eSLemover  def encode() = {
8953889e11eSLemover    val data = entries.asUInt()
8963889e11eSLemover    val ecc_slices = Wire(Vec(ecc_info._3, UInt(ecc_info._2.W)))
8973889e11eSLemover    for (i <- 0 until ecc_info._3) {
8983889e11eSLemover      ecc_slices(i) := eccCode.encode(data((i+1)*ecc_block-1, i*ecc_block)) >> ecc_block
8993889e11eSLemover    }
9003889e11eSLemover    if (ecc_info._4 != 0) {
9013889e11eSLemover      val ecc_unaligned = eccCode.encode(data(data.getWidth-1, ecc_info._3*ecc_block)) >> ecc_info._4
9023889e11eSLemover      ecc := Cat(ecc_unaligned, ecc_slices.asUInt())
9033889e11eSLemover    } else { ecc := ecc_slices.asUInt() }
9043889e11eSLemover  }
9053889e11eSLemover
9063889e11eSLemover  def decode(): Bool = {
9073889e11eSLemover    val data = entries.asUInt()
9083889e11eSLemover    val res = Wire(Vec(ecc_info._3 + 1, Bool()))
9093889e11eSLemover    for (i <- 0 until ecc_info._3) {
9105197bac8SZiyue-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}
9113889e11eSLemover    }
9125197bac8SZiyue-Zhang    if (ecc_info._2 != 0 && ecc_info._4 != 0) {
9133889e11eSLemover      res(ecc_info._3) := eccCode.decode(
9143889e11eSLemover        Cat(ecc(ecc_info._1-1, ecc_info._2*ecc_info._3), data(data.getWidth-1, ecc_info._3*ecc_block))).error
9153889e11eSLemover    } else { res(ecc_info._3) := false.B }
9163889e11eSLemover
9173889e11eSLemover    Cat(res).orR
9183889e11eSLemover  }
9193889e11eSLemover
9203889e11eSLemover  def gen(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = {
9213889e11eSLemover    this.entries := entries.genEntries(vpn, asid, data, levelUInt, prefetch)
9223889e11eSLemover    this.encode()
9233889e11eSLemover  }
9247196f5a2SLemover}
9257196f5a2SLemover
9266d5ddbceSLemoverclass PtwReq(implicit p: Parameters) extends PtwBundle {
9276d5ddbceSLemover  val vpn = UInt(vpnLen.W)
9286d5ddbceSLemover
9296d5ddbceSLemover  override def toPrintable: Printable = {
9306d5ddbceSLemover    p"vpn:0x${Hexadecimal(vpn)}"
9316d5ddbceSLemover  }
9326d5ddbceSLemover}
9336d5ddbceSLemover
9348744445eSMaxpicca-Liclass PtwReqwithMemIdx(implicit p: Parameters) extends PtwReq {
9358744445eSMaxpicca-Li  val memidx = new MemBlockidxBundle
9368744445eSMaxpicca-Li}
9378744445eSMaxpicca-Li
9386d5ddbceSLemoverclass PtwResp(implicit p: Parameters) extends PtwBundle {
9396d5ddbceSLemover  val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true)
9406d5ddbceSLemover  val pf = Bool()
941b6982e83SLemover  val af = Bool()
9426d5ddbceSLemover
94345f497a4Shappy-lx  def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = {
9445854c1edSLemover    this.entry.level.map(_ := level)
9455854c1edSLemover    this.entry.tag := vpn
9465854c1edSLemover    this.entry.perm.map(_ := pte.getPerm())
9475854c1edSLemover    this.entry.ppn := pte.ppn
948bc063562SLemover    this.entry.prefetch := DontCare
94945f497a4Shappy-lx    this.entry.asid := asid
9508d8ac704SLemover    this.entry.v := !pf
9515854c1edSLemover    this.pf := pf
952b6982e83SLemover    this.af := af
9535854c1edSLemover  }
9545854c1edSLemover
9556d5ddbceSLemover  override def toPrintable: Printable = {
956b6982e83SLemover    p"entry:${entry} pf:${pf} af:${af}"
9576d5ddbceSLemover  }
9586d5ddbceSLemover}
9596d5ddbceSLemover
96063632028SHaoyuan Fengclass PtwResptomerge (implicit p: Parameters) extends PtwBundle {
96163632028SHaoyuan Feng  val entry = UInt(blockBits.W)
96263632028SHaoyuan Feng  val vpn = UInt(vpnLen.W)
96363632028SHaoyuan Feng  val level = UInt(log2Up(Level).W)
96463632028SHaoyuan Feng  val pf = Bool()
96563632028SHaoyuan Feng  val af = Bool()
96663632028SHaoyuan Feng  val asid = UInt(asidLen.W)
96763632028SHaoyuan Feng
96863632028SHaoyuan Feng  def apply(pf: Bool, af: Bool, level: UInt, pte: UInt, vpn: UInt, asid: UInt) = {
96963632028SHaoyuan Feng    this.entry := pte
97063632028SHaoyuan Feng    this.pf := pf
97163632028SHaoyuan Feng    this.af := af
97263632028SHaoyuan Feng    this.level := level
97363632028SHaoyuan Feng    this.vpn := vpn
97463632028SHaoyuan Feng    this.asid := asid
97563632028SHaoyuan Feng  }
97663632028SHaoyuan Feng
97763632028SHaoyuan Feng  override def toPrintable: Printable = {
97863632028SHaoyuan Feng    p"entry:${entry} pf:${pf} af:${af}"
97963632028SHaoyuan Feng  }
98063632028SHaoyuan Feng}
98163632028SHaoyuan Feng
9828744445eSMaxpicca-Liclass PtwRespwithMemIdx(implicit p: Parameters) extends PtwResp {
9838744445eSMaxpicca-Li  val memidx = new MemBlockidxBundle
9848744445eSMaxpicca-Li}
9858744445eSMaxpicca-Li
98663632028SHaoyuan Fengclass PtwSectorRespwithMemIdx(implicit p: Parameters) extends PtwSectorResp {
98763632028SHaoyuan Feng  val memidx = new MemBlockidxBundle
98863632028SHaoyuan Feng}
98963632028SHaoyuan Feng
99063632028SHaoyuan Fengclass PtwSectorResp(implicit p: Parameters) extends PtwBundle {
99163632028SHaoyuan Feng  val entry = new PtwSectorEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)
99263632028SHaoyuan Feng  val addr_low = UInt(sectortlbwidth.W)
99363632028SHaoyuan Feng  val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W))
99463632028SHaoyuan Feng  val valididx = Vec(tlbcontiguous, Bool())
995b0fa7106SHaoyuan Feng  val pteidx = Vec(tlbcontiguous, Bool())
99663632028SHaoyuan Feng  val pf = Bool()
99763632028SHaoyuan Feng  val af = Bool()
99863632028SHaoyuan Feng
99963632028SHaoyuan Feng  def genPPN(vpn: UInt): UInt = {
100063632028SHaoyuan Feng    MuxLookup(entry.level.get, 0.U, Seq(
100163632028SHaoyuan Feng      0.U -> Cat(entry.ppn(entry.ppn.getWidth-1, vpnnLen * 2 - sectortlbwidth), vpn(vpnnLen*2-1, 0)),
100263632028SHaoyuan Feng      1.U -> Cat(entry.ppn(entry.ppn.getWidth-1, vpnnLen - sectortlbwidth), vpn(vpnnLen-1, 0)),
100363632028SHaoyuan Feng      2.U -> Cat(entry.ppn(entry.ppn.getWidth-1, 0), ppn_low(vpn(sectortlbwidth - 1, 0))))
100463632028SHaoyuan Feng    )
100563632028SHaoyuan Feng  }
100663632028SHaoyuan Feng
100763632028SHaoyuan Feng  def hit(vpn: UInt, asid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false) = {
100863632028SHaoyuan Feng    require(vpn.getWidth == vpnLen)
100963632028SHaoyuan Feng    //    require(this.asid.getWidth <= asid.getWidth)
101063632028SHaoyuan Feng    val asid_hit = if (ignoreAsid) true.B else (this.entry.asid === asid)
101163632028SHaoyuan Feng    if (allType) {
101263632028SHaoyuan Feng      val hit0 = entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnLen - 1, vpnnLen * 2)
101363632028SHaoyuan Feng      val hit1 = entry.tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth)   === vpn(vpnnLen * 2 - 1,  vpnnLen)
101463632028SHaoyuan Feng      val hit2 = entry.tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth)
101563632028SHaoyuan Feng      val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0))
101663632028SHaoyuan Feng
101763632028SHaoyuan Feng      asid_hit && Mux(entry.level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(entry.level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) && addr_low_hit
101863632028SHaoyuan Feng    } else {
101963632028SHaoyuan Feng      val hit0 = entry.tag(sectorvpnLen - 1, sectorvpnLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen)
102063632028SHaoyuan Feng      val hit1 = entry.tag(sectorvpnLen - vpnnLen - 1, sectorvpnLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2)
102163632028SHaoyuan Feng      val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0))
102263632028SHaoyuan Feng
102363632028SHaoyuan Feng      asid_hit && Mux(entry.level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) && addr_low_hit
102463632028SHaoyuan Feng    }
102563632028SHaoyuan Feng  }
102663632028SHaoyuan Feng}
102763632028SHaoyuan Feng
102863632028SHaoyuan Fengclass PtwMergeResp(implicit p: Parameters) extends PtwBundle {
102963632028SHaoyuan Feng  val entry = Vec(tlbcontiguous, new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true))
103063632028SHaoyuan Feng  val pteidx = Vec(tlbcontiguous, Bool())
103163632028SHaoyuan Feng  val not_super = Bool()
103263632028SHaoyuan Feng
103363632028SHaoyuan Feng  def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt, addr_low : UInt, not_super : Boolean = true) = {
103463632028SHaoyuan Feng    assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!")
103563632028SHaoyuan Feng
103663632028SHaoyuan Feng    val ptw_resp = Wire(new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true))
103763632028SHaoyuan Feng    ptw_resp.ppn := pte.ppn(ppnLen - 1, sectortlbwidth)
103863632028SHaoyuan Feng    ptw_resp.ppn_low := pte.ppn(sectortlbwidth - 1, 0)
103963632028SHaoyuan Feng    ptw_resp.level.map(_ := level)
104063632028SHaoyuan Feng    ptw_resp.perm.map(_ := pte.getPerm())
104163632028SHaoyuan Feng    ptw_resp.tag := vpn(vpnLen - 1, sectortlbwidth)
104263632028SHaoyuan Feng    ptw_resp.pf := pf
104363632028SHaoyuan Feng    ptw_resp.af := af
104463632028SHaoyuan Feng    ptw_resp.v := !pf
104563632028SHaoyuan Feng    ptw_resp.prefetch := DontCare
104663632028SHaoyuan Feng    ptw_resp.asid := asid
104763632028SHaoyuan Feng    this.pteidx := UIntToOH(addr_low).asBools
104863632028SHaoyuan Feng    this.not_super := not_super.B
104963632028SHaoyuan Feng    for (i <- 0 until tlbcontiguous) {
105063632028SHaoyuan Feng      this.entry(i) := ptw_resp
105163632028SHaoyuan Feng    }
105263632028SHaoyuan Feng  }
105363632028SHaoyuan Feng}
10548744445eSMaxpicca-Li
105592e3bfefSLemoverclass L2TLBIO(implicit p: Parameters) extends PtwBundle {
10566d5ddbceSLemover  val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO))
10576d5ddbceSLemover  val sfence = Input(new SfenceBundle)
1058b6982e83SLemover  val csr = new Bundle {
1059b6982e83SLemover    val tlb = Input(new TlbCsrBundle)
1060b6982e83SLemover    val distribute_csr = Flipped(new DistributedCSRIO)
1061b6982e83SLemover  }
10626d5ddbceSLemover}
10636d5ddbceSLemover
1064b848eea5SLemoverclass L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle {
1065b848eea5SLemover  val addr = UInt(PAddrBits.W)
1066b848eea5SLemover  val id = UInt(bMemID.W)
1067b848eea5SLemover}
106845f497a4Shappy-lx
106945f497a4Shappy-lxclass L2TlbInnerBundle(implicit p: Parameters) extends PtwReq {
107045f497a4Shappy-lx  val source = UInt(bSourceWidth.W)
107145f497a4Shappy-lx}
1072f1fe8698SLemover
1073f1fe8698SLemover
1074f1fe8698SLemoverobject ValidHoldBypass{
1075f1fe8698SLemover  def apply(infire: Bool, outfire: Bool, flush: Bool = false.B) = {
1076f1fe8698SLemover    val valid = RegInit(false.B)
1077f1fe8698SLemover    when (infire) { valid := true.B }
1078f1fe8698SLemover    when (outfire) { valid := false.B } // ATTENTION: order different with ValidHold
1079f1fe8698SLemover    when (flush) { valid := false.B } // NOTE: the flush will flush in & out, is that ok?
1080f1fe8698SLemover    valid || infire
1081f1fe8698SLemover  }
1082f1fe8698SLemover}
10835afdf73cSHaoyuan Feng
10845afdf73cSHaoyuan Fengclass L1TlbDB(implicit p: Parameters) extends TlbBundle {
10855afdf73cSHaoyuan Feng  val vpn = UInt(vpnLen.W)
10865afdf73cSHaoyuan Feng}
10875afdf73cSHaoyuan Feng
10885afdf73cSHaoyuan Fengclass PageCacheDB(implicit p: Parameters) extends TlbBundle with HasPtwConst {
10895afdf73cSHaoyuan Feng  val vpn = UInt(vpnLen.W)
10905afdf73cSHaoyuan Feng  val source = UInt(bSourceWidth.W)
10915afdf73cSHaoyuan Feng  val bypassed = Bool()
10925afdf73cSHaoyuan Feng  val is_first = Bool()
10935afdf73cSHaoyuan Feng  val prefetched = Bool()
10945afdf73cSHaoyuan Feng  val prefetch = Bool()
10955afdf73cSHaoyuan Feng  val l2Hit = Bool()
10965afdf73cSHaoyuan Feng  val l1Hit = Bool()
10975afdf73cSHaoyuan Feng  val hit = Bool()
10985afdf73cSHaoyuan Feng}
10995afdf73cSHaoyuan Feng
11005afdf73cSHaoyuan Fengclass PTWDB(implicit p: Parameters) extends TlbBundle with HasPtwConst {
11015afdf73cSHaoyuan Feng  val vpn = UInt(vpnLen.W)
11025afdf73cSHaoyuan Feng  val source = UInt(bSourceWidth.W)
11035afdf73cSHaoyuan Feng}
11045afdf73cSHaoyuan Feng
11055afdf73cSHaoyuan Fengclass L2TlbPrefetchDB(implicit p: Parameters) extends TlbBundle {
11065afdf73cSHaoyuan Feng  val vpn = UInt(vpnLen.W)
11075afdf73cSHaoyuan Feng}
11085afdf73cSHaoyuan Feng
11095afdf73cSHaoyuan Fengclass L2TlbMissQueueDB(implicit p: Parameters) extends TlbBundle {
11105afdf73cSHaoyuan Feng  val vpn = UInt(vpnLen.W)
11115afdf73cSHaoyuan Feng}
1112