xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/MMUBundle.scala (revision 708ceed4afe43fb0ea3a52407e46b2794c573634)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17package xiangshan.cache.mmu
18
19import chipsalliance.rocketchip.config.Parameters
20import chisel3._
21import chisel3.util._
22import xiangshan._
23import utils._
24import xiangshan.backend.roq.RoqPtr
25import xiangshan.backend.fu.util.HasCSRConst
26import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
27import freechips.rocketchip.tilelink._
28
29abstract class TlbBundle(implicit p: Parameters) extends XSBundle with HasTlbConst
30abstract class TlbModule(implicit p: Parameters) extends XSModule with HasTlbConst
31
32
33
34// case class ITLBKey
35// case class LDTLBKey
36// case class STTLBKey
37
38class VaBundle(implicit p: Parameters) extends TlbBundle {
39  val vpn  = UInt(vpnLen.W)
40  val off  = UInt(offLen.W)
41}
42
43class PtePermBundle(implicit p: Parameters) extends TlbBundle {
44  val d = Bool()
45  val a = Bool()
46  val g = Bool()
47  val u = Bool()
48  val x = Bool()
49  val w = Bool()
50  val r = Bool()
51
52  override def toPrintable: Printable = {
53    p"d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"// +
54    //(if(hasV) (p"v:${v}") else p"")
55  }
56}
57
58class TlbPermBundle(implicit p: Parameters) extends TlbBundle {
59  val pf = Bool() // NOTE: if this is true, just raise pf
60  // pagetable perm (software defined)
61  val d = Bool()
62  val a = Bool()
63  val g = Bool()
64  val u = Bool()
65  val x = Bool()
66  val w = Bool()
67  val r = Bool()
68  // pma perm (hardwired)
69  val pr = Bool() //readable
70  val pw = Bool() //writeable
71  val pe = Bool() //executable
72  val pa = Bool() //atom op permitted
73  val pi = Bool() //icacheable
74  val pd = Bool() //dcacheable
75
76  override def toPrintable: Printable = {
77    p"pf:${pf} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"
78  }
79}
80
81// multi-read && single-write
82// input is data, output is hot-code(not one-hot)
83class CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule {
84  val io = IO(new Bundle {
85    val r = new Bundle {
86      val req = Input(Vec(readWidth, gen))
87      val resp = Output(Vec(readWidth, Vec(set, Bool())))
88    }
89    val w = Input(new Bundle {
90      val valid = Bool()
91      val bits = new Bundle {
92        val index = UInt(log2Up(set).W)
93        val data = gen
94      }
95    })
96  })
97
98  val wordType = UInt(gen.getWidth.W)
99  val array = Reg(Vec(set, wordType))
100
101  io.r.resp.zipWithIndex.map{ case (a,i) =>
102    a := array.map(io.r.req(i).asUInt === _)
103  }
104
105  when (io.w.valid) {
106    array(io.w.bits.index) := io.w.bits.data
107  }
108}
109
110class TlbSPMeta(implicit p: Parameters) extends TlbBundle {
111  val tag = UInt(vpnLen.W) // tag is vpn
112  val level = UInt(1.W) // 1 for 2MB, 0 for 1GB
113
114  def hit(vpn: UInt): Bool = {
115    val a = tag(vpnnLen*3-1, vpnnLen*2) === vpn(vpnnLen*3-1, vpnnLen*2)
116    val b = tag(vpnnLen*2-1, vpnnLen*1) === vpn(vpnnLen*2-1, vpnnLen*1)
117    XSDebug(Mux(level.asBool, a&b, a), p"Hit superpage: hit:${Mux(level.asBool, a&b, a)} tag:${Hexadecimal(tag)} level:${level} a:${a} b:${b} vpn:${Hexadecimal(vpn)}\n")
118    Mux(level.asBool, a&b, a)
119  }
120
121  def apply(vpn: UInt, level: UInt) = {
122    this.tag := vpn
123    this.level := level(0)
124
125    this
126  }
127
128}
129
130class TlbData(superpage: Boolean = false)(implicit p: Parameters) extends TlbBundle {
131  val level = if(superpage) Some(UInt(1.W)) else None // /*2 for 4KB,*/ 1 for 2MB, 0 for 1GB
132  val ppn = UInt(ppnLen.W)
133  val perm = new TlbPermBundle
134
135  def genPPN(vpn: UInt): UInt = {
136    if (superpage) {
137      val insideLevel = level.getOrElse(0.U)
138      Mux(insideLevel.asBool, Cat(ppn(ppn.getWidth-1, vpnnLen*1), vpn(vpnnLen*1-1, 0)),
139                              Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)))
140    } else {
141      ppn
142    }
143  }
144
145  def apply(ppn: UInt, level: UInt, perm: UInt, pf: Bool) = {
146    this.level.map(_ := level(0))
147    this.ppn := ppn
148    // refill pagetable perm
149    val ptePerm = perm.asTypeOf(new PtePermBundle)
150    this.perm.pf:= pf
151    this.perm.d := ptePerm.d
152    this.perm.a := ptePerm.a
153    this.perm.g := ptePerm.g
154    this.perm.u := ptePerm.u
155    this.perm.x := ptePerm.x
156    this.perm.w := ptePerm.w
157    this.perm.r := ptePerm.r
158
159    // get pma perm
160    val (pmaMode, accessWidth) = AddressSpace.memmapAddrMatch(Cat(ppn, 0.U(12.W)))
161    this.perm.pr := PMAMode.read(pmaMode)
162    this.perm.pw := PMAMode.write(pmaMode)
163    this.perm.pe := PMAMode.execute(pmaMode)
164    this.perm.pa := PMAMode.atomic(pmaMode)
165    this.perm.pi := PMAMode.icache(pmaMode)
166    this.perm.pd := PMAMode.dcache(pmaMode)
167
168    this
169  }
170
171  override def toPrintable: Printable = {
172    val insideLevel = level.getOrElse(0.U)
173    p"level:${insideLevel} ppn:${Hexadecimal(ppn)} perm:${perm}"
174  }
175
176  override def cloneType: this.type = (new TlbData(superpage)).asInstanceOf[this.type]
177}
178
179class TlbEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle {
180  require(pageNormal || pageSuper)
181
182  val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W)
183            else UInt(vpnLen.W)
184  val level = if (!pageNormal) Some(UInt(1.W))
185              else if (!pageSuper) None
186              else Some(UInt(2.W))
187  val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W)
188            else UInt(ppnLen.W)
189  val perm = new TlbPermBundle
190
191  def hit(vpn: UInt): Bool = {
192    if (!pageSuper) vpn === tag
193    else if (!pageNormal) MuxLookup(level.get, false.B, Seq(
194      0.U -> (tag(vpnnLen*2-1, vpnnLen) === vpn(vpnLen-1, vpnnLen*2)),
195      1.U -> (tag === vpn(vpnLen-1, vpnnLen)),
196    ))
197    else MuxLookup(level.get, false.B, Seq(
198      0.U -> (tag(vpnLen-1, vpnnLen*2) === vpn(vpnLen-1, vpnnLen*2)),
199      1.U -> (tag(vpnLen-1, vpnnLen) === vpn(vpnLen-1, vpnnLen)),
200      2.U -> (tag === vpn) // if pageNormal is false, this will always be false
201    ))
202  }
203
204  def apply(item: PtwResp): TlbEntry = {
205    this.tag := {if (pageNormal) item.entry.tag else item.entry.tag(vpnLen-1, vpnnLen)}
206    val inner_level = item.entry.level.getOrElse(0.U)
207    this.level.map(_ := { if (pageNormal && pageSuper) inner_level
208                          else if (pageSuper) inner_level(0)
209                          else 0.U})
210    this.ppn := { if (!pageNormal) item.entry.ppn(ppnLen-1, vpnnLen)
211                  else item.entry.ppn }
212    val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType)
213    this.perm.pf := item.pf
214    this.perm.d := ptePerm.d
215    this.perm.a := ptePerm.a
216    this.perm.g := ptePerm.g
217    this.perm.u := ptePerm.u
218    this.perm.x := ptePerm.x
219    this.perm.w := ptePerm.w
220    this.perm.r := ptePerm.r
221
222    // get pma perm
223    val (pmaMode, accessWidth) = AddressSpace.memmapAddrMatch(Cat(item.entry.ppn, 0.U(12.W)))
224    this.perm.pr := PMAMode.read(pmaMode)
225    this.perm.pw := PMAMode.write(pmaMode)
226    this.perm.pe := PMAMode.execute(pmaMode)
227    this.perm.pa := PMAMode.atomic(pmaMode)
228    this.perm.pi := PMAMode.icache(pmaMode)
229    this.perm.pd := PMAMode.dcache(pmaMode)
230
231    this
232  }
233
234  def genPPN(vpn: UInt) : UInt = {
235    if (!pageSuper) ppn
236    else if (!pageNormal) MuxLookup(level.get, 0.U, Seq(
237      0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen*2-1, 0)),
238      1.U -> Cat(ppn, vpn(vpnnLen-1, 0))
239    ))
240    else MuxLookup(level.get, 0.U, Seq(
241      0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)),
242      1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)),
243      2.U -> ppn
244    ))
245  }
246
247  override def toPrintable: Printable = {
248    val inner_level = level.getOrElse(2.U)
249    p"level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}"
250  }
251
252  override def cloneType: this.type = (new TlbEntry(pageNormal, pageSuper)).asInstanceOf[this.type]
253}
254
255object TlbCmd {
256  def read  = "b00".U
257  def write = "b01".U
258  def exec  = "b10".U
259
260  def atom_read  = "b100".U // lr
261  def atom_write = "b101".U // sc / amo
262
263  def apply() = UInt(3.W)
264  def isRead(a: UInt) = a(1,0)===read
265  def isWrite(a: UInt) = a(1,0)===write
266  def isExec(a: UInt) = a(1,0)===exec
267
268  def isAtom(a: UInt) = a(2)
269}
270
271class TlbStorageIO(nSets: Int, nWays: Int, ports: Int)(implicit p: Parameters) extends  TlbBundle {
272  val r = new Bundle {
273    val req = Vec(ports, Flipped(DecoupledIO(new Bundle {
274      val vpn = Output(UInt(vpnLen.W))
275    })))
276    val resp = Vec(ports, ValidIO(new Bundle{
277      val hit = Output(Bool())
278      val ppn = Output(UInt(ppnLen.W))
279      val perm = Output(new TlbPermBundle())
280      val hitVec = Output(UInt(nWays.W))
281    }))
282  }
283  val w = Flipped(ValidIO(new Bundle {
284    val wayIdx = Output(UInt(log2Up(nWays).W))
285    val data = Output(new PtwResp)
286  }))
287  val victim = new Bundle {
288    val out = ValidIO(Output(new TlbEntry(pageNormal = true, pageSuper = false)))
289    val in = Flipped(ValidIO(Output(new TlbEntry(pageNormal = true, pageSuper = false))))
290  }
291  val sfence = Input(new SfenceBundle())
292
293  def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = {
294    this.r.req(i).valid := valid
295    this.r.req(i).bits.vpn := vpn
296  }
297
298  def r_resp_apply(i: Int) = {
299    (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, this.r.resp(i).bits.hitVec)
300  }
301
302  def w_apply(valid: Bool, wayIdx: UInt, data: PtwResp): Unit = {
303    this.w.valid := valid
304    this.w.bits.wayIdx := wayIdx
305    this.w.bits.data := data
306  }
307
308  override def cloneType: this.type = new TlbStorageIO(nSets, nWays, ports).asInstanceOf[this.type]
309}
310
311class ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle {
312  val access = Flipped(new Bundle {
313    val sets = Output(Vec(Width, UInt(log2Up(nSets).W)))
314    val touch_ways = Vec(Width, ValidIO(Output(UInt(log2Up(nWays).W))))
315  })
316
317  val refillIdx = Output(UInt(log2Up(nWays).W))
318  val chosen_set = Flipped(Output(UInt(log2Up(nSets).W)))
319
320  def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = {
321    for (i <- 0 until Width) {
322      this.access.sets(i) := in(i).access.sets(0)
323      this.access.touch_ways(i) := in(i).access.touch_ways(0)
324      this.chosen_set := get_idx(vpn, nSets)
325      in(i).refillIdx := this.refillIdx
326    }
327  }
328}
329
330class TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends
331  TlbBundle {
332  val normalPage = new ReplaceIO(Width, q.normalNSets, q.normalNWays)
333  val superPage = new ReplaceIO(Width, q.superNSets, q.superNWays)
334
335  def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = {
336    this.normalPage.apply_sep(in.map(_.normalPage), vpn)
337    this.superPage.apply_sep(in.map(_.superPage), vpn)
338  }
339
340  override def cloneType = (new TlbReplaceIO(Width, q)).asInstanceOf[this.type]
341}
342
343class TlbReq(implicit p: Parameters) extends TlbBundle {
344  val vaddr = UInt(VAddrBits.W)
345  val cmd = TlbCmd()
346  val roqIdx = new RoqPtr
347  val debug = new Bundle {
348    val pc = UInt(XLEN.W)
349    val isFirstIssue = Bool()
350  }
351
352  override def toPrintable: Printable = {
353    p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} pc:0x${Hexadecimal(debug.pc)} roqIdx:${roqIdx}"
354  }
355}
356
357class TlbResp(implicit p: Parameters) extends TlbBundle {
358  val paddr = UInt(PAddrBits.W)
359  val miss = Bool()
360  val mmio = Bool()
361  val excp = new Bundle {
362    val pf = new Bundle {
363      val ld = Bool()
364      val st = Bool()
365      val instr = Bool()
366    }
367    val af = new Bundle {
368      val ld = Bool()
369      val st = Bool()
370      val instr = Bool()
371    }
372  }
373  val ptwBack = Bool() // when ptw back, wake up replay rs's state
374
375  override def toPrintable: Printable = {
376    p"paddr:0x${Hexadecimal(paddr)} miss:${miss} excp.pf: ld:${excp.pf.ld} st:${excp.pf.st} instr:${excp.pf.instr} ptwBack:${ptwBack}"
377  }
378}
379
380class TlbRequestIO()(implicit p: Parameters) extends TlbBundle {
381  val req = DecoupledIO(new TlbReq)
382  val resp = Flipped(DecoupledIO(new TlbResp))
383}
384
385class BlockTlbRequestIO()(implicit p: Parameters) extends TlbBundle {
386  val req = DecoupledIO(new TlbReq)
387  val resp = Flipped(DecoupledIO(new TlbResp))
388}
389
390class TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle {
391  val req = Vec(Width, DecoupledIO(new PtwReq))
392  val resp = Flipped(DecoupledIO(new PtwResp))
393
394  override def cloneType: this.type = (new TlbPtwIO(Width)).asInstanceOf[this.type]
395
396  override def toPrintable: Printable = {
397    p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}"
398  }
399}
400
401class TlbBaseBundle(implicit p: Parameters) extends TlbBundle {
402  val sfence = Input(new SfenceBundle)
403  val csr = Input(new TlbCsrBundle)
404}
405
406class TlbIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends
407  TlbBaseBundle {
408  val requestor = Vec(Width, Flipped(new TlbRequestIO))
409  val ptw = new TlbPtwIO(Width)
410  val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null
411
412  override def cloneType: this.type = (new TlbIO(Width, q)).asInstanceOf[this.type]
413}
414
415class BTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle {
416  val req = Vec(Width, DecoupledIO(new PtwReq))
417  val resp = Flipped(DecoupledIO(new Bundle {
418    val data = new PtwResp
419    val vector = Output(Vec(Width, Bool()))
420  }))
421
422  override def cloneType: this.type = (new BTlbPtwIO(Width)).asInstanceOf[this.type]
423}
424/****************************  Bridge TLB *******************************/
425
426class BridgeTLBIO(Width: Int)(implicit p: Parameters) extends TlbBaseBundle {
427  val requestor = Vec(Width, Flipped(new TlbPtwIO()))
428  val ptw = new BTlbPtwIO(Width)
429
430  override def cloneType: this.type = (new BridgeTLBIO(Width)).asInstanceOf[this.type]
431}
432
433
434/****************************  PTW  *************************************/
435abstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst
436abstract class PtwModule(outer: PTW) extends LazyModuleImp(outer)
437  with HasXSParameter with HasPtwConst
438
439class PteBundle(implicit p: Parameters) extends PtwBundle{
440  val reserved  = UInt(pteResLen.W)
441  val ppn  = UInt(ppnLen.W)
442  val rsw  = UInt(2.W)
443  val perm = new Bundle {
444    val d    = Bool()
445    val a    = Bool()
446    val g    = Bool()
447    val u    = Bool()
448    val x    = Bool()
449    val w    = Bool()
450    val r    = Bool()
451    val v    = Bool()
452  }
453
454  def unaligned(level: UInt) = {
455    isLeaf() && !(level === 2.U ||
456                  level === 1.U && ppn(vpnnLen-1,   0) === 0.U ||
457                  level === 0.U && ppn(vpnnLen*2-1, 0) === 0.U)
458  }
459
460  def isPf(level: UInt) = {
461    !perm.v || (!perm.r && perm.w) || unaligned(level)
462  }
463
464  def isLeaf() = {
465    perm.r || perm.x || perm.w
466  }
467
468  def getPerm() = {
469    val pm = Wire(new PtePermBundle)
470    pm.d := perm.d
471    pm.a := perm.a
472    pm.g := perm.g
473    pm.u := perm.u
474    pm.x := perm.x
475    pm.w := perm.w
476    pm.r := perm.r
477    pm
478  }
479
480  override def toPrintable: Printable = {
481    p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}"
482  }
483}
484
485class PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle {
486  val tag = UInt(tagLen.W)
487  val ppn = UInt(ppnLen.W)
488  val perm = if (hasPerm) Some(new PtePermBundle) else None
489  val level = if (hasLevel) Some(UInt(log2Up(Level).W)) else None
490
491  def hit(vpn: UInt, allType: Boolean = false) = {
492    require(vpn.getWidth == vpnLen)
493    if (allType) {
494      require(hasLevel)
495      val hit0 = tag(tagLen - 1,    vpnnLen*2) === vpn(tagLen - 1, vpnnLen*2)
496      val hit1 = tag(vpnnLen*2 - 1, vpnnLen)   === vpn(vpnnLen*2 - 1,  vpnnLen)
497      val hit2 = tag(vpnnLen - 1,     0)         === vpn(vpnnLen - 1, 0)
498      Mux(level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0))
499    } else if (hasLevel) {
500      val hit0 = tag(tagLen - 1, tagLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen)
501      val hit1 = tag(tagLen - vpnnLen - 1, tagLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2)
502      Mux(level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1)
503    } else {
504      tag === vpn(vpnLen - 1, vpnLen - tagLen)
505    }
506  }
507
508  def refill(vpn: UInt, pte: UInt, level: UInt = 0.U) {
509    tag := vpn(vpnLen - 1, vpnLen - tagLen)
510    ppn := pte.asTypeOf(new PteBundle().cloneType).ppn
511    perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm)
512    this.level.map(_ := level)
513  }
514
515  def genPtwEntry(vpn: UInt, pte: UInt, level: UInt = 0.U) = {
516    val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel))
517    e.refill(vpn, pte, level)
518    e
519  }
520
521  override def cloneType: this.type = (new PtwEntry(tagLen, hasPerm, hasLevel)).asInstanceOf[this.type]
522
523  override def toPrintable: Printable = {
524    // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}"
525    p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} " +
526      (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") +
527      (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"")
528  }
529}
530
531class PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle {
532  require(log2Up(num)==log2Down(num))
533
534  val tag  = UInt(tagLen.W)
535  val ppns = Vec(num, UInt(ppnLen.W))
536  val vs   = Vec(num, Bool())
537  val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None
538  // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1")
539
540  def tagClip(vpn: UInt) = {
541    require(vpn.getWidth == vpnLen)
542    vpn(vpnLen - 1, vpnLen - tagLen)
543  }
544
545  def sectorIdxClip(vpn: UInt, level: Int) = {
546    getVpnClip(vpn, level)(log2Up(num) - 1, 0)
547  }
548
549  def hit(vpn: UInt) = {
550    tag === tagClip(vpn) && vs(sectorIdxClip(vpn, level)) // TODO: optimize this. don't need to compare each with tag
551  }
552
553  def genEntries(vpn: UInt, data: UInt, levelUInt: UInt) = {
554    require((data.getWidth / XLEN) == num,
555      s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}")
556
557    val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm))
558    ps.tag := tagClip(vpn)
559    for (i <- 0 until num) {
560      val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle)
561      ps.ppns(i) := pte.ppn
562      ps.vs(i)   := !pte.isPf(levelUInt) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf())
563      ps.perms.map(_(i) := pte.perm)
564    }
565    ps
566  }
567
568  override def cloneType: this.type = (new PtwEntries(num, tagLen, level, hasPerm)).asInstanceOf[this.type]
569  override def toPrintable: Printable = {
570    // require(num == 4, "if num is not 4, please comment this toPrintable")
571    // NOTE: if num is not 4, please comment this toPrintable
572    val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle)))
573    p"tag:0x${Hexadecimal(tag)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " +
574      (if (hasPerm) p"perms:${printVec(permsInner)}" else p"")
575  }
576}
577
578class PtwReq(implicit p: Parameters) extends PtwBundle {
579  val vpn = UInt(vpnLen.W)
580
581  override def toPrintable: Printable = {
582    p"vpn:0x${Hexadecimal(vpn)}"
583  }
584}
585
586class PtwResp(implicit p: Parameters) extends PtwBundle {
587  val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true)
588  val pf  = Bool()
589
590  def apply(pf: Bool, level: UInt, pte: PteBundle, vpn: UInt) = {
591    this.entry.level.map(_ := level)
592    this.entry.tag := vpn
593    this.entry.perm.map(_ := pte.getPerm())
594    this.entry.ppn := pte.ppn
595    this.pf := pf
596  }
597
598  override def toPrintable: Printable = {
599    p"entry:${entry} pf:${pf}"
600  }
601}
602
603class PtwIO(implicit p: Parameters) extends PtwBundle {
604  val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO))
605  val sfence = Input(new SfenceBundle)
606  val csr = Input(new TlbCsrBundle)
607}
608
609class L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle {
610  val addr = UInt(PAddrBits.W)
611  val id = UInt(bMemID.W)
612}
613