xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/PageTableWalker.scala (revision ad0d9d89a70b9ad22b0fa772551b3b58f2e46d43)
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
198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
206d5ddbceSLemoverimport chisel3._
216d5ddbceSLemoverimport chisel3.util._
226d5ddbceSLemoverimport xiangshan._
236d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants}
246d5ddbceSLemoverimport utils._
253c02ee8fSwakafaimport utility._
266d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp}
276d5ddbceSLemoverimport freechips.rocketchip.tilelink._
28b6982e83SLemoverimport xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle}
296d5ddbceSLemover
3092e3bfefSLemover/** Page Table Walk is divided into two parts
3192e3bfefSLemover  * One,   PTW: page walk for pde, except for leaf entries, one by one
3292e3bfefSLemover  * Two, LLPTW: page walk for pte, only the leaf entries(4KB), in parallel
336d5ddbceSLemover  */
3492e3bfefSLemover
3592e3bfefSLemover
3692e3bfefSLemover/** PTW : page table walker
3792e3bfefSLemover  * a finite state machine
3892e3bfefSLemover  * only take 1GB and 2MB page walks
3992e3bfefSLemover  * or in other words, except the last level(leaf)
4092e3bfefSLemover  **/
4192e3bfefSLemoverclass PTWIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
426d5ddbceSLemover  val req = Flipped(DecoupledIO(new Bundle {
4345f497a4Shappy-lx    val req_info = new L2TlbInnerBundle()
446d5ddbceSLemover    val l1Hit = Bool()
456d5ddbceSLemover    val ppn = UInt(ppnLen.W)
4630104977Speixiaokun    val stage1Hit = Bool()
4730104977Speixiaokun    val stage1 = new PtwMergeResp
486d5ddbceSLemover  }))
496d5ddbceSLemover  val resp = DecoupledIO(new Bundle {
50bc063562SLemover    val source = UInt(bSourceWidth.W)
51eb4bf3f2Speixiaokun    val s2xlate = UInt(2.W)
5263632028SHaoyuan Feng    val resp = new PtwMergeResp
53d0de7e4aSpeixiaokun    val h_resp = new HptwResp
546d5ddbceSLemover  })
556d5ddbceSLemover
5692e3bfefSLemover  val llptw = DecoupledIO(new LLPTWInBundle())
579c503409SLemover  // NOTE: llptw change from "connect to llptw" to "connect to page cache"
589c503409SLemover  // to avoid corner case that caused duplicate entries
59cc5a5f22SLemover
60d0de7e4aSpeixiaokun  val hptw = new Bundle {
61d0de7e4aSpeixiaokun    val req = DecoupledIO(new Bundle {
62eb4bf3f2Speixiaokun      val source = UInt(bSourceWidth.W)
63d0de7e4aSpeixiaokun      val id = UInt(log2Up(l2tlbParams.llptwsize).W)
6482978df9Speixiaokun      val gvpn = UInt(vpnLen.W)
65d0de7e4aSpeixiaokun    })
66d0de7e4aSpeixiaokun    val resp = Flipped(Valid(new Bundle {
67d0de7e4aSpeixiaokun      val h_resp = Output(new HptwResp)
68d0de7e4aSpeixiaokun    }))
69d0de7e4aSpeixiaokun  }
706d5ddbceSLemover  val mem = new Bundle {
71b848eea5SLemover    val req = DecoupledIO(new L2TlbMemReqBundle())
725854c1edSLemover    val resp = Flipped(ValidIO(UInt(XLEN.W)))
73cc5a5f22SLemover    val mask = Input(Bool())
746d5ddbceSLemover  }
75b6982e83SLemover  val pmp = new Bundle {
76b6982e83SLemover    val req = ValidIO(new PMPReqBundle())
77b6982e83SLemover    val resp = Flipped(new PMPRespBundle())
78b6982e83SLemover  }
796d5ddbceSLemover
806d5ddbceSLemover  val refill = Output(new Bundle {
8145f497a4Shappy-lx    val req_info = new L2TlbInnerBundle()
826d5ddbceSLemover    val level = UInt(log2Up(Level).W)
836d5ddbceSLemover  })
846d5ddbceSLemover}
856d5ddbceSLemover
8692e3bfefSLemoverclass PTW()(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents {
8792e3bfefSLemover  val io = IO(new PTWIO)
886d5ddbceSLemover  val sfence = io.sfence
896d5ddbceSLemover  val mem = io.mem
90d0de7e4aSpeixiaokun  val req_s2xlate = Reg(UInt(2.W))
9103c1129fSpeixiaokun  val enableS2xlate = req_s2xlate =/= noS2xlate
9203c1129fSpeixiaokun  val onlyS1xlate = req_s2xlate === onlyStage1
9303c1129fSpeixiaokun  val onlyS2xlate = req_s2xlate === onlyStage2
94d0de7e4aSpeixiaokun
95d0de7e4aSpeixiaokun  val satp = Mux(enableS2xlate, io.csr.vsatp, io.csr.satp)
96d0de7e4aSpeixiaokun  val hgatp = io.csr.hgatp
97d0de7e4aSpeixiaokun  val flush = io.sfence.valid || satp.changed
98d0de7e4aSpeixiaokun  val s2xlate = enableS2xlate && !onlyS1xlate
996d5ddbceSLemover  val level = RegInit(0.U(log2Up(Level).W))
100b6982e83SLemover  val af_level = RegInit(0.U(log2Up(Level).W)) // access fault return this level
1016d5ddbceSLemover  val ppn = Reg(UInt(ppnLen.W))
10282978df9Speixiaokun  val vpn = Reg(UInt(vpnLen.W)) // vpn or gvpn
1036d5ddbceSLemover  val levelNext = level + 1.U
1046d5ddbceSLemover  val l1Hit = Reg(Bool())
105d0de7e4aSpeixiaokun  val pte = mem.resp.bits.asTypeOf(new PteBundle().cloneType)
1066d5ddbceSLemover
10744b79566SXiaokun-Pei  // s/w register
10844b79566SXiaokun-Pei  val s_pmp_check = RegInit(true.B)
10944b79566SXiaokun-Pei  val s_mem_req = RegInit(true.B)
11044b79566SXiaokun-Pei  val s_llptw_req = RegInit(true.B)
11144b79566SXiaokun-Pei  val w_mem_resp = RegInit(true.B)
112d0de7e4aSpeixiaokun  val s_hptw_req = RegInit(true.B)
113d0de7e4aSpeixiaokun  val w_hptw_resp = RegInit(true.B)
114d0de7e4aSpeixiaokun  val s_last_hptw_req = RegInit(true.B)
115d0de7e4aSpeixiaokun  val w_last_hptw_resp = RegInit(true.B)
11644b79566SXiaokun-Pei  // for updating "level"
11744b79566SXiaokun-Pei  val mem_addr_update = RegInit(false.B)
11844b79566SXiaokun-Pei
11944b79566SXiaokun-Pei  val idle = RegInit(true.B)
1202a906a65SHaoyuan Feng  val finish = WireInit(false.B)
1212a906a65SHaoyuan Feng  val sent_to_pmp = idle === false.B && (s_pmp_check === false.B || mem_addr_update) && !finish
12244b79566SXiaokun-Pei
123d0de7e4aSpeixiaokun  val pageFault = pte.isPf(level)
12444b79566SXiaokun-Pei  val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, sent_to_pmp)
1256d5ddbceSLemover
126d0de7e4aSpeixiaokun  val hptw_pageFault = RegInit(false.B)
127d0de7e4aSpeixiaokun  val hptw_accessFault = RegInit(false.B)
128d0de7e4aSpeixiaokun  val last_s2xlate = RegInit(false.B)
12930104977Speixiaokun  val stage1Hit = RegEnable(io.req.bits.stage1Hit, io.req.fire())
13030104977Speixiaokun  val stage1 = RegEnable(io.req.bits.stage1, io.req.fire())
13109280d15Speixiaokun  val hptw_resp_stage2 = Reg(Bool())
132d0de7e4aSpeixiaokun
133d0de7e4aSpeixiaokun  val ppn_af = pte.isAf()
134d0de7e4aSpeixiaokun  val find_pte = pte.isLeaf() || ppn_af || pageFault
13544b79566SXiaokun-Pei  val to_find_pte = level === 1.U && find_pte === false.B
136935edac4STang Haojin  val source = RegEnable(io.req.bits.req_info.source, io.req.fire)
1376d5ddbceSLemover
1386d5ddbceSLemover  val l1addr = MakeAddr(satp.ppn, getVpnn(vpn, 2))
139d0de7e4aSpeixiaokun  val l2addr = MakeAddr(Mux(l1Hit, ppn, pte.ppn), getVpnn(vpn, 1))
140b6982e83SLemover  val mem_addr = Mux(af_level === 0.U, l1addr, l2addr)
14144b79566SXiaokun-Pei
142b24e0a78Speixiaokun  val hptw_resp = RegEnable(io.hptw.resp.bits.h_resp, io.hptw.resp.fire())
143c0991f6aSpeixiaokun  val gpaddr = MuxCase(mem_addr, Seq(
144c0991f6aSpeixiaokun    stage1Hit -> Cat(stage1.genPPN(), 0.U(offLen.W)),
145c0991f6aSpeixiaokun    onlyS2xlate -> Cat(vpn, 0.U(offLen.W)),
146c0991f6aSpeixiaokun    !s_last_hptw_req -> Cat(pte.ppn, 0.U(offLen.W))
147c0991f6aSpeixiaokun  ))
1487e664aa3Speixiaokun  val hpaddr = Cat(hptw_resp.genPPNS2(get_pn(gpaddr)), get_off(gpaddr))
149d0de7e4aSpeixiaokun
15044b79566SXiaokun-Pei  io.req.ready := idle
15130104977Speixiaokun  val ptw_resp = Wire(new PtwMergeResp)
15230104977Speixiaokun  ptw_resp.apply(pageFault && !accessFault && !ppn_af, accessFault || ppn_af, Mux(accessFault, af_level,level), pte, vpn, satp.asid, hgatp.asid, vpn(sectortlbwidth - 1, 0), not_super = false)
15344b79566SXiaokun-Pei
15409280d15Speixiaokun  val normal_resp = idle === false.B && mem_addr_update && !last_s2xlate && ((w_mem_resp && find_pte) || (s_pmp_check && accessFault) || onlyS2xlate)
15509280d15Speixiaokun  val stageHit_resp = idle === false.B && hptw_resp_stage2
15609280d15Speixiaokun  io.resp.valid := Mux(stage1Hit, stageHit_resp, normal_resp)
15744b79566SXiaokun-Pei  io.resp.bits.source := source
15830104977Speixiaokun  io.resp.bits.resp := Mux(stage1Hit, stage1, ptw_resp)
15979d4b70cSpeixiaokun  io.resp.bits.h_resp := hptw_resp
1606315ba2aSpeixiaokun  io.resp.bits.s2xlate := req_s2xlate
16144b79566SXiaokun-Pei
16244b79566SXiaokun-Pei  io.llptw.valid := s_llptw_req === false.B && to_find_pte && !accessFault
16344b79566SXiaokun-Pei  io.llptw.bits.req_info.source := source
16444b79566SXiaokun-Pei  io.llptw.bits.req_info.vpn := vpn
16582978df9Speixiaokun  io.llptw.bits.req_info.s2xlate := req_s2xlate
166eb4bf3f2Speixiaokun  io.llptw.bits.ppn := DontCare
16744b79566SXiaokun-Pei
168b6982e83SLemover  io.pmp.req.valid := DontCare // samecycle, do not use valid
169d0de7e4aSpeixiaokun  io.pmp.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr)
170b6982e83SLemover  io.pmp.req.bits.size := 3.U // TODO: fix it
171b6982e83SLemover  io.pmp.req.bits.cmd := TlbCmd.read
172b6982e83SLemover
17344b79566SXiaokun-Pei  mem.req.valid := s_mem_req === false.B && !mem.mask && !accessFault && s_pmp_check
174d0de7e4aSpeixiaokun  mem.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr)
175bc063562SLemover  mem.req.bits.id := FsmReqID.U(bMemID.W)
1766d5ddbceSLemover
177933ec998Speixiaokun  io.refill.req_info.s2xlate := Mux(enableS2xlate, onlyStage1, req_s2xlate) // ptw refill the pte of stage 1 when s2xlate is enabled
17845f497a4Shappy-lx  io.refill.req_info.vpn := vpn
1796d5ddbceSLemover  io.refill.level := level
18045f497a4Shappy-lx  io.refill.req_info.source := source
1816d5ddbceSLemover
182d0de7e4aSpeixiaokun  io.hptw.req.valid := !s_hptw_req || !s_last_hptw_req
183d0de7e4aSpeixiaokun  io.hptw.req.bits.id := FsmReqID.U(bMemID.W)
18482978df9Speixiaokun  io.hptw.req.bits.gvpn := get_pn(gpaddr)
185eb4bf3f2Speixiaokun  io.hptw.req.bits.source := source
186d0de7e4aSpeixiaokun
18730104977Speixiaokun  when (io.req.fire() && io.req.bits.stage1Hit){
18830104977Speixiaokun    idle := false.B
18961c5d636Speixiaokun    req_s2xlate := io.req.bits.req_info.s2xlate
19030104977Speixiaokun    s_hptw_req := false.B
19109280d15Speixiaokun    hptw_resp_stage2 := false.B
19230104977Speixiaokun  }
193d0de7e4aSpeixiaokun
19430104977Speixiaokun  when (io.hptw.resp.fire() && w_hptw_resp === false.B && stage1Hit){
19530104977Speixiaokun    w_hptw_resp := true.B
19609280d15Speixiaokun    hptw_resp_stage2 := true.B
19730104977Speixiaokun  }
19830104977Speixiaokun
19930104977Speixiaokun  when (io.resp.fire() && stage1Hit){
20030104977Speixiaokun    idle := true.B
20130104977Speixiaokun  }
20230104977Speixiaokun
20330104977Speixiaokun  when (io.req.fire() && !io.req.bits.stage1Hit){
20444b79566SXiaokun-Pei    val req = io.req.bits
20544b79566SXiaokun-Pei    level := Mux(req.l1Hit, 1.U, 0.U)
20644b79566SXiaokun-Pei    af_level := Mux(req.l1Hit, 1.U, 0.U)
20744b79566SXiaokun-Pei    ppn := Mux(req.l1Hit, io.req.bits.ppn, satp.ppn)
20844b79566SXiaokun-Pei    vpn := io.req.bits.req_info.vpn
20944b79566SXiaokun-Pei    l1Hit := req.l1Hit
21044b79566SXiaokun-Pei    accessFault := false.B
21144b79566SXiaokun-Pei    idle := false.B
212d0de7e4aSpeixiaokun    hptw_pageFault := false.B
21350c7aa78Speixiaokun    req_s2xlate := io.req.bits.req_info.s2xlate
21482978df9Speixiaokun    when(io.req.bits.req_info.s2xlate =/= noS2xlate && io.req.bits.req_info.s2xlate =/= onlyStage1){
215d0de7e4aSpeixiaokun      last_s2xlate := true.B
216d0de7e4aSpeixiaokun      s_hptw_req := false.B
217d0de7e4aSpeixiaokun    }.otherwise {
218d0de7e4aSpeixiaokun      s_pmp_check := false.B
219d0de7e4aSpeixiaokun    }
220d0de7e4aSpeixiaokun  }
221d0de7e4aSpeixiaokun
222d0de7e4aSpeixiaokun  when(io.hptw.req.fire() && s_hptw_req === false.B){
223d0de7e4aSpeixiaokun    s_hptw_req := true.B
224d0de7e4aSpeixiaokun    w_hptw_resp := false.B
225d0de7e4aSpeixiaokun  }
226d0de7e4aSpeixiaokun
22730104977Speixiaokun  when(io.hptw.resp.fire() && w_hptw_resp === false.B && !stage1Hit) {
228d0de7e4aSpeixiaokun    hptw_pageFault := io.hptw.resp.bits.h_resp.gpf
229d0de7e4aSpeixiaokun    hptw_accessFault := io.hptw.resp.bits.h_resp.gaf
230d0de7e4aSpeixiaokun    w_hptw_resp := true.B
231d0de7e4aSpeixiaokun    when(onlyS2xlate){
232d0de7e4aSpeixiaokun      mem_addr_update := true.B
233d0de7e4aSpeixiaokun      last_s2xlate := false.B
234d0de7e4aSpeixiaokun    }.otherwise {
235d0de7e4aSpeixiaokun      s_pmp_check := false.B
236d0de7e4aSpeixiaokun    }
237d0de7e4aSpeixiaokun  }
238d0de7e4aSpeixiaokun
239d0de7e4aSpeixiaokun  when(io.hptw.req.fire() && s_last_hptw_req === false.B) {
240d0de7e4aSpeixiaokun    w_last_hptw_resp := false.B
241d0de7e4aSpeixiaokun    s_last_hptw_req := true.B
242d0de7e4aSpeixiaokun  }
243d0de7e4aSpeixiaokun
244d0de7e4aSpeixiaokun  when(io.hptw.resp.fire() && w_last_hptw_resp === false.B){
245d0de7e4aSpeixiaokun    hptw_pageFault := io.hptw.resp.bits.h_resp.gpf
246d0de7e4aSpeixiaokun    hptw_accessFault := io.hptw.resp.bits.h_resp.gaf
247d0de7e4aSpeixiaokun    w_last_hptw_resp := true.B
248d0de7e4aSpeixiaokun    mem_addr_update := true.B
249d0de7e4aSpeixiaokun    last_s2xlate := false.B
25044b79566SXiaokun-Pei  }
25144b79566SXiaokun-Pei
25244b79566SXiaokun-Pei  when(sent_to_pmp && mem_addr_update === false.B){
25344b79566SXiaokun-Pei    s_mem_req := false.B
25444b79566SXiaokun-Pei    s_pmp_check := true.B
25544b79566SXiaokun-Pei  }
25644b79566SXiaokun-Pei
25744b79566SXiaokun-Pei  when(accessFault && idle === false.B){
25844b79566SXiaokun-Pei    s_pmp_check := true.B
25944b79566SXiaokun-Pei    s_mem_req := true.B
26044b79566SXiaokun-Pei    w_mem_resp := true.B
26144b79566SXiaokun-Pei    s_llptw_req := true.B
262d0de7e4aSpeixiaokun    s_hptw_req := true.B
263d0de7e4aSpeixiaokun    w_hptw_resp := true.B
264d0de7e4aSpeixiaokun    s_last_hptw_req := true.B
265d0de7e4aSpeixiaokun    w_last_hptw_resp := true.B
26644b79566SXiaokun-Pei    mem_addr_update := true.B
267d0de7e4aSpeixiaokun    last_s2xlate := false.B
26844b79566SXiaokun-Pei  }
26944b79566SXiaokun-Pei
270935edac4STang Haojin  when (mem.req.fire){
27144b79566SXiaokun-Pei    s_mem_req := true.B
27244b79566SXiaokun-Pei    w_mem_resp := false.B
27344b79566SXiaokun-Pei  }
27444b79566SXiaokun-Pei
275935edac4STang Haojin  when(mem.resp.fire && w_mem_resp === false.B){
27644b79566SXiaokun-Pei    w_mem_resp := true.B
27744b79566SXiaokun-Pei    af_level := af_level + 1.U
27844b79566SXiaokun-Pei    s_llptw_req := false.B
27944b79566SXiaokun-Pei    mem_addr_update := true.B
28044b79566SXiaokun-Pei  }
28144b79566SXiaokun-Pei
28244b79566SXiaokun-Pei  when(mem_addr_update){
28344b79566SXiaokun-Pei    when(level === 0.U && !(find_pte || accessFault)){
28444b79566SXiaokun-Pei      level := levelNext
285d0de7e4aSpeixiaokun      when(s2xlate){
286d0de7e4aSpeixiaokun        s_hptw_req := false.B
287d0de7e4aSpeixiaokun      }.otherwise{
28844b79566SXiaokun-Pei        s_mem_req := false.B
289d0de7e4aSpeixiaokun      }
29044b79566SXiaokun-Pei      s_llptw_req := true.B
29144b79566SXiaokun-Pei      mem_addr_update := false.B
2922a906a65SHaoyuan Feng    }.elsewhen(io.llptw.valid){
293935edac4STang Haojin      when(io.llptw.fire) {
29444b79566SXiaokun-Pei        idle := true.B
29544b79566SXiaokun-Pei        s_llptw_req := true.B
29644b79566SXiaokun-Pei        mem_addr_update := false.B
297d0de7e4aSpeixiaokun        last_s2xlate := false.B
2982a906a65SHaoyuan Feng      }
2992a906a65SHaoyuan Feng      finish := true.B
300d0de7e4aSpeixiaokun    }.elsewhen(s2xlate && last_s2xlate === true.B) {
301d0de7e4aSpeixiaokun      s_last_hptw_req := false.B
302d0de7e4aSpeixiaokun      mem_addr_update := false.B
3032a906a65SHaoyuan Feng    }.elsewhen(io.resp.valid){
304935edac4STang Haojin      when(io.resp.fire) {
30544b79566SXiaokun-Pei        idle := true.B
30644b79566SXiaokun-Pei        s_llptw_req := true.B
30744b79566SXiaokun-Pei        mem_addr_update := false.B
30844b79566SXiaokun-Pei        accessFault := false.B
30944b79566SXiaokun-Pei      }
3102a906a65SHaoyuan Feng      finish := true.B
3112a906a65SHaoyuan Feng    }
31244b79566SXiaokun-Pei  }
31344b79566SXiaokun-Pei
31444b79566SXiaokun-Pei
31544b79566SXiaokun-Pei  when (sfence.valid) {
31644b79566SXiaokun-Pei    idle := true.B
31744b79566SXiaokun-Pei    s_pmp_check := true.B
31844b79566SXiaokun-Pei    s_mem_req := true.B
31944b79566SXiaokun-Pei    s_llptw_req := true.B
32044b79566SXiaokun-Pei    w_mem_resp := true.B
32144b79566SXiaokun-Pei    accessFault := false.B
322d826bce1SHaoyuan Feng    mem_addr_update := false.B
323d0de7e4aSpeixiaokun    s_hptw_req := true.B
324d0de7e4aSpeixiaokun    w_hptw_resp := true.B
325d0de7e4aSpeixiaokun    s_last_hptw_req := true.B
326d0de7e4aSpeixiaokun    w_last_hptw_resp := true.B
32744b79566SXiaokun-Pei  }
32844b79566SXiaokun-Pei
32944b79566SXiaokun-Pei
33044b79566SXiaokun-Pei  XSDebug(p"[ptw] level:${level} notFound:${pageFault}\n")
3316d5ddbceSLemover
3326d5ddbceSLemover  // perf
333935edac4STang Haojin  XSPerfAccumulate("fsm_count", io.req.fire)
3346d5ddbceSLemover  for (i <- 0 until PtwWidth) {
335935edac4STang Haojin    XSPerfAccumulate(s"fsm_count_source${i}", io.req.fire && io.req.bits.req_info.source === i.U)
3366d5ddbceSLemover  }
33744b79566SXiaokun-Pei  XSPerfAccumulate("fsm_busy", !idle)
33844b79566SXiaokun-Pei  XSPerfAccumulate("fsm_idle", idle)
3396d5ddbceSLemover  XSPerfAccumulate("resp_blocked", io.resp.valid && !io.resp.ready)
340dd7fe201SHaoyuan Feng  XSPerfAccumulate("ptw_ppn_af", io.resp.fire && ppn_af)
341935edac4STang Haojin  XSPerfAccumulate("mem_count", mem.req.fire)
342935edac4STang Haojin  XSPerfAccumulate("mem_cycle", BoolStopWatch(mem.req.fire, mem.resp.fire, true))
3436d5ddbceSLemover  XSPerfAccumulate("mem_blocked", mem.req.valid && !mem.req.ready)
344cc5a5f22SLemover
34544b79566SXiaokun-Pei  TimeOutAssert(!idle, timeOutThreshold, "page table walker time out")
346cd365d4cSrvcoresjw
347cd365d4cSrvcoresjw  val perfEvents = Seq(
348935edac4STang Haojin    ("fsm_count         ", io.req.fire                                     ),
34944b79566SXiaokun-Pei    ("fsm_busy          ", !idle                                             ),
35044b79566SXiaokun-Pei    ("fsm_idle          ", idle                                              ),
351cd365d4cSrvcoresjw    ("resp_blocked      ", io.resp.valid && !io.resp.ready                   ),
352935edac4STang Haojin    ("mem_count         ", mem.req.fire                                    ),
353935edac4STang Haojin    ("mem_cycle         ", BoolStopWatch(mem.req.fire, mem.resp.fire, true)),
354cd365d4cSrvcoresjw    ("mem_blocked       ", mem.req.valid && !mem.req.ready                   ),
355cd365d4cSrvcoresjw  )
3561ca0e4f3SYinan Xu  generatePerfEvent()
3576d5ddbceSLemover}
35892e3bfefSLemover
35992e3bfefSLemover/*========================= LLPTW ==============================*/
36092e3bfefSLemover
36192e3bfefSLemover/** LLPTW : Last Level Page Table Walker
36292e3bfefSLemover  * the page walker that only takes 4KB(last level) page walk.
36392e3bfefSLemover  **/
36492e3bfefSLemover
36592e3bfefSLemoverclass LLPTWInBundle(implicit p: Parameters) extends XSBundle with HasPtwConst {
36692e3bfefSLemover  val req_info = Output(new L2TlbInnerBundle())
367d61cd5eeSpeixiaokun  val ppn = Output(if(HasHExtension) UInt((vpnLen.max(ppnLen)).W) else UInt(ppnLen.W))
36892e3bfefSLemover}
36992e3bfefSLemover
37092e3bfefSLemoverclass LLPTWIO(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
37192e3bfefSLemover  val in = Flipped(DecoupledIO(new LLPTWInBundle()))
37292e3bfefSLemover  val out = DecoupledIO(new Bundle {
37392e3bfefSLemover    val req_info = Output(new L2TlbInnerBundle())
37492e3bfefSLemover    val id = Output(UInt(bMemID.W))
375d0de7e4aSpeixiaokun    val h_resp = Output(new HptwResp)
37692e3bfefSLemover    val af = Output(Bool())
37792e3bfefSLemover  })
37892e3bfefSLemover  val mem = new Bundle {
37992e3bfefSLemover    val req = DecoupledIO(new L2TlbMemReqBundle())
38092e3bfefSLemover    val resp = Flipped(Valid(new Bundle {
38192e3bfefSLemover      val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W))
382dc05c713Speixiaokun      val value = Output(UInt(XLEN.W))
38392e3bfefSLemover    }))
38492e3bfefSLemover    val enq_ptr = Output(UInt(log2Ceil(l2tlbParams.llptwsize).W))
38592e3bfefSLemover    val buffer_it = Output(Vec(l2tlbParams.llptwsize, Bool()))
38692e3bfefSLemover    val refill = Output(new L2TlbInnerBundle())
38792e3bfefSLemover    val req_mask = Input(Vec(l2tlbParams.llptwsize, Bool()))
38892e3bfefSLemover  }
3897797f035SbugGenerator  val cache = DecoupledIO(new L2TlbInnerBundle())
39092e3bfefSLemover  val pmp = new Bundle {
39192e3bfefSLemover    val req = Valid(new PMPReqBundle())
39292e3bfefSLemover    val resp = Flipped(new PMPRespBundle())
39392e3bfefSLemover  }
394d0de7e4aSpeixiaokun  val hptw = new Bundle {
395d0de7e4aSpeixiaokun    val req = DecoupledIO(new Bundle{
396eb4bf3f2Speixiaokun      val source = UInt(bSourceWidth.W)
397d0de7e4aSpeixiaokun      val id = UInt(log2Up(l2tlbParams.llptwsize).W)
39882978df9Speixiaokun      val gvpn = UInt(vpnLen.W)
399d0de7e4aSpeixiaokun    })
400d0de7e4aSpeixiaokun    val resp = Flipped(Valid(new Bundle {
401d0de7e4aSpeixiaokun      val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W))
402d0de7e4aSpeixiaokun      val h_resp = Output(new HptwResp)
403d0de7e4aSpeixiaokun    }))
404d0de7e4aSpeixiaokun  }
40592e3bfefSLemover}
40692e3bfefSLemover
40792e3bfefSLemoverclass LLPTWEntry(implicit p: Parameters) extends XSBundle with HasPtwConst {
40892e3bfefSLemover  val req_info = new L2TlbInnerBundle()
409d0de7e4aSpeixiaokun  val s2xlate = Bool()
41092e3bfefSLemover  val ppn = UInt(ppnLen.W)
41192e3bfefSLemover  val wait_id = UInt(log2Up(l2tlbParams.llptwsize).W)
41292e3bfefSLemover  val af = Bool()
413dc05c713Speixiaokun  val hptw_resp = new HptwResp()
41492e3bfefSLemover}
41592e3bfefSLemover
41692e3bfefSLemover
41792e3bfefSLemoverclass LLPTW(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents {
41892e3bfefSLemover  val io = IO(new LLPTWIO())
41982978df9Speixiaokun  val enableS2xlate = io.in.bits.req_info.s2xlate =/= noS2xlate
420d0de7e4aSpeixiaokun  val satp = Mux(enableS2xlate, io.csr.vsatp, io.csr.satp)
42192e3bfefSLemover
422d0de7e4aSpeixiaokun  val flush = io.sfence.valid || satp.changed
42392e3bfefSLemover  val entries = Reg(Vec(l2tlbParams.llptwsize, new LLPTWEntry()))
424d0de7e4aSpeixiaokun  val state_idle :: state_hptw_req :: state_hptw_resp :: state_addr_check :: state_mem_req :: state_mem_waiting :: state_mem_out :: state_last_hptw_req :: state_last_hptw_resp :: state_cache :: Nil = Enum(10)
42592e3bfefSLemover  val state = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(state_idle)))
4267797f035SbugGenerator
42792e3bfefSLemover  val is_emptys = state.map(_ === state_idle)
42892e3bfefSLemover  val is_mems = state.map(_ === state_mem_req)
42992e3bfefSLemover  val is_waiting = state.map(_ === state_mem_waiting)
43092e3bfefSLemover  val is_having = state.map(_ === state_mem_out)
4317797f035SbugGenerator  val is_cache = state.map(_ === state_cache)
432d0de7e4aSpeixiaokun  val is_hptw_req = state.map(_ === state_hptw_req)
433d0de7e4aSpeixiaokun  val is_last_hptw_req = state.map(_ === state_last_hptw_req)
43492e3bfefSLemover
435935edac4STang Haojin  val full = !ParallelOR(is_emptys).asBool
43692e3bfefSLemover  val enq_ptr = ParallelPriorityEncoder(is_emptys)
43792e3bfefSLemover
4387797f035SbugGenerator  val mem_ptr = ParallelPriorityEncoder(is_having) // TODO: optimize timing, bad: entries -> ptr -> entry
43992e3bfefSLemover  val mem_arb = Module(new RRArbiter(new LLPTWEntry(), l2tlbParams.llptwsize))
44092e3bfefSLemover  for (i <- 0 until l2tlbParams.llptwsize) {
44192e3bfefSLemover    mem_arb.io.in(i).bits := entries(i)
44292e3bfefSLemover    mem_arb.io.in(i).valid := is_mems(i) && !io.mem.req_mask(i)
44392e3bfefSLemover  }
444d0de7e4aSpeixiaokun  val hyper_arb1 = Module(new RRArbiter(new LLPTWEntry(), l2tlbParams.llptwsize))
445d0de7e4aSpeixiaokun  for (i <- 0 until l2tlbParams.llptwsize) {
446d0de7e4aSpeixiaokun    hyper_arb1.io.in(i).bits := entries(i)
447d0de7e4aSpeixiaokun    hyper_arb1.io.in(i).valid := is_hptw_req(i)
448d0de7e4aSpeixiaokun  }
449d0de7e4aSpeixiaokun  val hyper_arb2 = Module(new RRArbiter(new LLPTWEntry(), l2tlbParams.llptwsize))
450d0de7e4aSpeixiaokun  for(i <- 0 until l2tlbParams.llptwsize) {
451d0de7e4aSpeixiaokun    hyper_arb2.io.in(i).bits := entries(i)
452d0de7e4aSpeixiaokun    hyper_arb2.io.in(i).valid := is_last_hptw_req(i)
453d0de7e4aSpeixiaokun  }
45492e3bfefSLemover
455f3034303SHaoyuan Feng  val cache_ptr = ParallelMux(is_cache, (0 until l2tlbParams.llptwsize).map(_.U(log2Up(l2tlbParams.llptwsize).W)))
4567797f035SbugGenerator
45792e3bfefSLemover  // duplicate req
45892e3bfefSLemover  // to_wait: wait for the last to access mem, set to mem_resp
45992e3bfefSLemover  // to_cache: the last is back just right now, set to mem_cache
46092e3bfefSLemover  val dup_vec = state.indices.map(i =>
461cca17e78Speixiaokun    dup(io.in.bits.req_info.vpn, entries(i).req_info.vpn) && io.in.bits.req_info.s2xlate === entries(i).req_info.s2xlate
46292e3bfefSLemover  )
463cca17e78Speixiaokun  val dup_req_fire = mem_arb.io.out.fire && dup(io.in.bits.req_info.vpn, mem_arb.io.out.bits.req_info.vpn) && io.in.bits.req_info.s2xlate === mem_arb.io.out.bits.req_info.s2xlate // dup with the req fire entry
46492e3bfefSLemover  val dup_vec_wait = dup_vec.zip(is_waiting).map{case (d, w) => d && w} // dup with "mem_waiting" entres, sending mem req already
46592e3bfefSLemover  val dup_vec_having = dup_vec.zipWithIndex.map{case (d, i) => d && is_having(i)} // dup with the "mem_out" entry recv the data just now
46692e3bfefSLemover  val wait_id = Mux(dup_req_fire, mem_arb.io.chosen, ParallelMux(dup_vec_wait zip entries.map(_.wait_id)))
467935edac4STang Haojin  val dup_wait_resp = io.mem.resp.fire && VecInit(dup_vec_wait)(io.mem.resp.bits.id) // dup with the entry that data coming next cycle
46892e3bfefSLemover  val to_wait = Cat(dup_vec_wait).orR || dup_req_fire
46992e3bfefSLemover  val to_mem_out = dup_wait_resp
4707797f035SbugGenerator  val to_cache = Cat(dup_vec_having).orR
4717274ec5cSpeixiaokun  val to_hptw = io.in.bits.req_info.s2xlate =/= noS2xlate
4727797f035SbugGenerator  XSError(RegNext(dup_req_fire && Cat(dup_vec_wait).orR, init = false.B), "mem req but some entries already waiting, should not happed")
47392e3bfefSLemover
474935edac4STang Haojin  XSError(io.in.fire && ((to_mem_out && to_cache) || (to_wait && to_cache)), "llptw enq, to cache conflict with to mem")
47592e3bfefSLemover  val mem_resp_hit = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(false.B)))
4767274ec5cSpeixiaokun  val enq_state_normal = MuxCase(state_addr_check, Seq(
4777274ec5cSpeixiaokun    to_mem_out -> state_mem_out, // same to the blew, but the mem resp now
4787274ec5cSpeixiaokun    to_wait -> state_mem_waiting,
4797274ec5cSpeixiaokun    to_cache -> state_cache,
4807274ec5cSpeixiaokun    to_hptw -> state_hptw_req
4817274ec5cSpeixiaokun  ))
4827797f035SbugGenerator  val enq_state = Mux(from_pre(io.in.bits.req_info.source) && enq_state_normal =/= state_addr_check, state_idle, enq_state_normal)
483935edac4STang Haojin  when (io.in.fire) {
48492e3bfefSLemover    // if prefetch req does not need mem access, just give it up.
48592e3bfefSLemover    // so there will be at most 1 + FilterSize entries that needs re-access page cache
48692e3bfefSLemover    // so 2 + FilterSize is enough to avoid dead-lock
4877797f035SbugGenerator    state(enq_ptr) := enq_state
48892e3bfefSLemover    entries(enq_ptr).req_info := io.in.bits.req_info
48992e3bfefSLemover    entries(enq_ptr).ppn := io.in.bits.ppn
49092e3bfefSLemover    entries(enq_ptr).wait_id := Mux(to_wait, wait_id, enq_ptr)
49192e3bfefSLemover    entries(enq_ptr).af := false.B
492d0de7e4aSpeixiaokun    entries(enq_ptr).s2xlate := enableS2xlate
49392e3bfefSLemover    mem_resp_hit(enq_ptr) := to_mem_out
49492e3bfefSLemover  }
4957797f035SbugGenerator
4967797f035SbugGenerator  val enq_ptr_reg = RegNext(enq_ptr)
4977274ec5cSpeixiaokun  val need_addr_check = RegNext(enq_state === state_addr_check && io.in.fire() && !flush)
4987274ec5cSpeixiaokun
4997274ec5cSpeixiaokun  val hasHptwResp = ParallelOR(state.map(_ === state_hptw_resp)).asBool()
5007274ec5cSpeixiaokun  val hptw_resp_ptr_reg = RegNext(io.hptw.resp.bits.id)
5017274ec5cSpeixiaokun  val hptw_need_addr_check = RegNext(hasHptwResp && io.hptw.resp.fire() && !flush)
502d0de7e4aSpeixiaokun
503*ad0d9d89Speixiaokun  val pte = io.mem.resp.bits.value.asTypeOf(new PteBundle().cloneType)
504b24e0a78Speixiaokun  val gpaddr = MakeGPAddr(io.in.bits.ppn, getVpnn(io.in.bits.req_info.vpn, 0))
5057274ec5cSpeixiaokun  val hptw_resp = io.hptw.resp.bits.h_resp
5067e664aa3Speixiaokun  val hpaddr = Cat(hptw_resp.genPPNS2(get_pn(gpaddr)), get_off(gpaddr))
5077274ec5cSpeixiaokun  val hpaddr_reg = RegEnable(hpaddr, hasHptwResp && io.hptw.resp.fire())
5087274ec5cSpeixiaokun  val addr = MakeAddr(io.in.bits.ppn, getVpnn(io.in.bits.req_info.vpn, 0))
5097274ec5cSpeixiaokun  val addr_reg = RegEnable(addr, io.in.fire())
5107274ec5cSpeixiaokun  io.pmp.req.valid := need_addr_check || hptw_need_addr_check
5117274ec5cSpeixiaokun  io.pmp.req.bits.addr := Mux(enableS2xlate, hpaddr, addr)
5127797f035SbugGenerator  io.pmp.req.bits.cmd := TlbCmd.read
5137797f035SbugGenerator  io.pmp.req.bits.size := 3.U // TODO: fix it
5147797f035SbugGenerator  val pmp_resp_valid = io.pmp.req.valid // same cycle
5157797f035SbugGenerator  when (pmp_resp_valid) {
5167797f035SbugGenerator    // NOTE: when pmp resp but state is not addr check, then the entry is dup with other entry, the state was changed before
5177797f035SbugGenerator    //       when dup with the req-ing entry, set to mem_waiting (above codes), and the ld must be false, so dontcare
5187274ec5cSpeixiaokun    val ptr = Mux(hptw_need_addr_check, hptw_resp_ptr_reg, enq_ptr_reg);
5197797f035SbugGenerator    val accessFault = io.pmp.resp.ld || io.pmp.resp.mmio
5207274ec5cSpeixiaokun    entries(ptr).af := accessFault
5217274ec5cSpeixiaokun    state(ptr) := Mux(accessFault, state_mem_out, state_mem_req)
5227797f035SbugGenerator  }
5237797f035SbugGenerator
524935edac4STang Haojin  when (mem_arb.io.out.fire) {
52592e3bfefSLemover    for (i <- state.indices) {
52692e3bfefSLemover      when (state(i) =/= state_idle && dup(entries(i).req_info.vpn, mem_arb.io.out.bits.req_info.vpn)) {
52792e3bfefSLemover        // NOTE: "dup enq set state to mem_wait" -> "sending req set other dup entries to mem_wait"
52892e3bfefSLemover        state(i) := state_mem_waiting
52992e3bfefSLemover        entries(i).wait_id := mem_arb.io.chosen
53092e3bfefSLemover      }
53192e3bfefSLemover    }
53292e3bfefSLemover  }
533935edac4STang Haojin  when (io.mem.resp.fire) {
53492e3bfefSLemover    state.indices.map{i =>
53592e3bfefSLemover      when (state(i) === state_mem_waiting && io.mem.resp.bits.id === entries(i).wait_id) {
536d0de7e4aSpeixiaokun        state(i) := Mux(entries(i).s2xlate, state_last_hptw_req, state_mem_out)
53792e3bfefSLemover        mem_resp_hit(i) := true.B
538*ad0d9d89Speixiaokun        entries(i).ppn := pte.ppn // for last stage 2 translation
539*ad0d9d89Speixiaokun      }
540*ad0d9d89Speixiaokun    }
541*ad0d9d89Speixiaokun  }
542*ad0d9d89Speixiaokun
543*ad0d9d89Speixiaokun  when (DelayN(io.mem.resp.fire(), 1)) {
544*ad0d9d89Speixiaokun    state.indices.map{i =>
545*ad0d9d89Speixiaokun      when (state(i) === state_last_hptw_req && io.mem.resp.bits.id === entries(i).wait_id) {
546*ad0d9d89Speixiaokun        entries(i).ppn := pte.ppn // for last stage 2 translation
54792e3bfefSLemover      }
54892e3bfefSLemover    }
54992e3bfefSLemover  }
550d0de7e4aSpeixiaokun
551d0de7e4aSpeixiaokun  when (hyper_arb1.io.out.fire()) {
552d0de7e4aSpeixiaokun    for (i <- state.indices) {
553d0de7e4aSpeixiaokun      when (state(i) === state_hptw_req && entries(i).ppn === hyper_arb1.io.out.bits.ppn && entries(i).s2xlate) {
554d0de7e4aSpeixiaokun        state(i) := state_hptw_resp
555d0de7e4aSpeixiaokun        entries(i).wait_id := hyper_arb1.io.chosen
556d0de7e4aSpeixiaokun      }
557d0de7e4aSpeixiaokun    }
558d0de7e4aSpeixiaokun  }
559d0de7e4aSpeixiaokun
560d0de7e4aSpeixiaokun  when (hyper_arb2.io.out.fire()) {
561d0de7e4aSpeixiaokun    for (i <- state.indices) {
562d0de7e4aSpeixiaokun      when (state(i) === state_last_hptw_req && entries(i).ppn === hyper_arb2.io.out.bits.ppn && entries(i).s2xlate) {
563d0de7e4aSpeixiaokun        state(i) := state_last_hptw_resp
564d0de7e4aSpeixiaokun        entries(i).wait_id := hyper_arb2.io.chosen
565d0de7e4aSpeixiaokun      }
566d0de7e4aSpeixiaokun    }
567d0de7e4aSpeixiaokun  }
568d0de7e4aSpeixiaokun
569d0de7e4aSpeixiaokun  when (io.hptw.resp.fire()) {
570d0de7e4aSpeixiaokun    for (i <- state.indices) {
571d0de7e4aSpeixiaokun      when (state(i) === state_hptw_resp && io.hptw.resp.bits.id === entries(i).wait_id) {
572d0de7e4aSpeixiaokun        state(i) := state_addr_check
573dc05c713Speixiaokun        entries(i).hptw_resp := io.hptw.resp.bits.h_resp
574d0de7e4aSpeixiaokun      }
575d0de7e4aSpeixiaokun      when (state(i) === state_last_hptw_resp && io.hptw.resp.bits.id === entries(i).wait_id) {
576d0de7e4aSpeixiaokun        state(i) := state_mem_out
577dc05c713Speixiaokun        entries(i).hptw_resp := io.hptw.resp.bits.h_resp
578d0de7e4aSpeixiaokun      }
579d0de7e4aSpeixiaokun    }
580d0de7e4aSpeixiaokun  }
581d0de7e4aSpeixiaokun
582935edac4STang Haojin  when (io.out.fire) {
58392e3bfefSLemover    assert(state(mem_ptr) === state_mem_out)
58492e3bfefSLemover    state(mem_ptr) := state_idle
58592e3bfefSLemover  }
58692e3bfefSLemover  mem_resp_hit.map(a => when (a) { a := false.B } )
58792e3bfefSLemover
5887797f035SbugGenerator  when (io.cache.fire) {
5897797f035SbugGenerator    state(cache_ptr) := state_idle
59092e3bfefSLemover  }
5917797f035SbugGenerator  XSError(io.out.fire && io.cache.fire && (mem_ptr === cache_ptr), "mem resp and cache fire at the same time at same entry")
59292e3bfefSLemover
59392e3bfefSLemover  when (flush) {
59492e3bfefSLemover    state.map(_ := state_idle)
59592e3bfefSLemover  }
59692e3bfefSLemover
59792e3bfefSLemover  io.in.ready := !full
59892e3bfefSLemover
599935edac4STang Haojin  io.out.valid := ParallelOR(is_having).asBool
60092e3bfefSLemover  io.out.bits.req_info := entries(mem_ptr).req_info
60192e3bfefSLemover  io.out.bits.id := mem_ptr
60292e3bfefSLemover  io.out.bits.af := entries(mem_ptr).af
603dc05c713Speixiaokun  io.out.bits.h_resp := entries(mem_ptr).hptw_resp
604d0de7e4aSpeixiaokun
605dc05c713Speixiaokun  val hptw_req_gvpn_1 = hyper_arb1.io.out.bits.ppn // first stage 2 translation
606*ad0d9d89Speixiaokun  val hptw_req_gvpn_2 = hyper_arb2.io.out.bits.ppn // last stage 2 translation
607d0de7e4aSpeixiaokun  io.hptw.req.valid := (hyper_arb1.io.out.valid || hyper_arb2.io.out.valid) && !flush
608dc05c713Speixiaokun  io.hptw.req.bits.gvpn := Mux(hyper_arb1.io.out.valid, hptw_req_gvpn_1, hptw_req_gvpn_2)
609d0de7e4aSpeixiaokun  io.hptw.req.bits.id := Mux(hyper_arb1.io.out.valid, hyper_arb1.io.chosen, hyper_arb2.io.chosen)
610eb4bf3f2Speixiaokun  io.hptw.req.bits.source := Mux(hyper_arb1.io.out.valid, hyper_arb1.io.out.bits.req_info.source, hyper_arb2.io.out.bits.req_info.source)
611d0de7e4aSpeixiaokun  hyper_arb1.io.out.ready := io.hptw.req.ready
612d0de7e4aSpeixiaokun  hyper_arb2.io.out.ready := io.hptw.req.ready
61392e3bfefSLemover
61492e3bfefSLemover  io.mem.req.valid := mem_arb.io.out.valid && !flush
615dc05c713Speixiaokun  val mem_paddr = MakeAddr(mem_arb.io.out.bits.ppn, getVpnn(mem_arb.io.out.bits.req_info.vpn, 0))
6167e664aa3Speixiaokun  val mem_hpaddr = MakeAddr(mem_arb.io.out.bits.hptw_resp.genPPNS2(get_pn(mem_paddr)), getVpnn(mem_arb.io.out.bits.req_info.vpn, 0))
617dc05c713Speixiaokun  io.mem.req.bits.addr := Mux(mem_arb.io.out.bits.s2xlate, mem_hpaddr, mem_paddr)
61892e3bfefSLemover  io.mem.req.bits.id := mem_arb.io.chosen
61992e3bfefSLemover  mem_arb.io.out.ready := io.mem.req.ready
620933ec998Speixiaokun  val mem_refill_id = RegNext(io.mem.resp.bits.id(log2Up(l2tlbParams.llptwsize)-1, 0))
621933ec998Speixiaokun  io.mem.refill := entries(mem_refill_id).req_info
622933ec998Speixiaokun  io.mem.refill.s2xlate := Mux(entries(mem_refill_id).req_info.s2xlate === noS2xlate, noS2xlate, onlyStage1) // llptw refill the pte of stage 1
62392e3bfefSLemover  io.mem.buffer_it := mem_resp_hit
62492e3bfefSLemover  io.mem.enq_ptr := enq_ptr
62592e3bfefSLemover
6267797f035SbugGenerator  io.cache.valid := Cat(is_cache).orR
6277797f035SbugGenerator  io.cache.bits := ParallelMux(is_cache, entries.map(_.req_info))
6287797f035SbugGenerator
629935edac4STang Haojin  XSPerfAccumulate("llptw_in_count", io.in.fire)
63092e3bfefSLemover  XSPerfAccumulate("llptw_in_block", io.in.valid && !io.in.ready)
63192e3bfefSLemover  for (i <- 0 until 7) {
632935edac4STang Haojin    XSPerfAccumulate(s"enq_state${i}", io.in.fire && enq_state === i.U)
63392e3bfefSLemover  }
63492e3bfefSLemover  for (i <- 0 until (l2tlbParams.llptwsize + 1)) {
63592e3bfefSLemover    XSPerfAccumulate(s"util${i}", PopCount(is_emptys.map(!_)) === i.U)
63692e3bfefSLemover    XSPerfAccumulate(s"mem_util${i}", PopCount(is_mems) === i.U)
63792e3bfefSLemover    XSPerfAccumulate(s"waiting_util${i}", PopCount(is_waiting) === i.U)
63892e3bfefSLemover  }
639935edac4STang Haojin  XSPerfAccumulate("mem_count", io.mem.req.fire)
64092e3bfefSLemover  XSPerfAccumulate("mem_cycle", PopCount(is_waiting) =/= 0.U)
64192e3bfefSLemover  XSPerfAccumulate("blocked_in", io.in.valid && !io.in.ready)
64292e3bfefSLemover
64392e3bfefSLemover  for (i <- 0 until l2tlbParams.llptwsize) {
64492e3bfefSLemover    TimeOutAssert(state(i) =/= state_idle, timeOutThreshold, s"missqueue time out no out ${i}")
64592e3bfefSLemover  }
64692e3bfefSLemover
64792e3bfefSLemover  val perfEvents = Seq(
648935edac4STang Haojin    ("tlbllptw_incount           ", io.in.fire               ),
64992e3bfefSLemover    ("tlbllptw_inblock           ", io.in.valid && !io.in.ready),
650935edac4STang Haojin    ("tlbllptw_memcount          ", io.mem.req.fire          ),
65192e3bfefSLemover    ("tlbllptw_memcycle          ", PopCount(is_waiting)       ),
65292e3bfefSLemover  )
65392e3bfefSLemover  generatePerfEvent()
65492e3bfefSLemover}
655d0de7e4aSpeixiaokun
656d0de7e4aSpeixiaokun/*========================= HPTW ==============================*/
657d0de7e4aSpeixiaokun
658d0de7e4aSpeixiaokun/** HPTW : Hypervisor Page Table Walker
659d0de7e4aSpeixiaokun  * the page walker take the virtual machine's page walk.
660d0de7e4aSpeixiaokun  * guest physical address translation, guest physical address -> host physical address
661d0de7e4aSpeixiaokun  **/
662d0de7e4aSpeixiaokunclass HPTWIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
663d0de7e4aSpeixiaokun  val req = Flipped(DecoupledIO(new Bundle {
664eb4bf3f2Speixiaokun    val source = UInt(bSourceWidth.W)
665d0de7e4aSpeixiaokun    val id = UInt(log2Up(l2tlbParams.llptwsize).W)
66682978df9Speixiaokun    val gvpn = UInt(vpnLen.W)
6676315ba2aSpeixiaokun    val ppn = UInt(ppnLen.W)
668d0de7e4aSpeixiaokun    val l1Hit = Bool()
669d0de7e4aSpeixiaokun    val l2Hit = Bool()
670d0de7e4aSpeixiaokun  }))
671d0de7e4aSpeixiaokun  val resp = Valid(new Bundle {
672eb4bf3f2Speixiaokun    val source = UInt(bSourceWidth.W)
673d0de7e4aSpeixiaokun    val resp = Output(new HptwResp())
674d0de7e4aSpeixiaokun    val id = Output(UInt(bMemID.W))
675d0de7e4aSpeixiaokun  })
676d0de7e4aSpeixiaokun
677d0de7e4aSpeixiaokun  val mem = new Bundle {
678d0de7e4aSpeixiaokun    val req = DecoupledIO(new L2TlbMemReqBundle())
679d0de7e4aSpeixiaokun    val resp = Flipped(ValidIO(UInt(XLEN.W)))
680d0de7e4aSpeixiaokun    val mask = Input(Bool())
681d0de7e4aSpeixiaokun  }
682d0de7e4aSpeixiaokun  val refill = Output(new Bundle {
683d0de7e4aSpeixiaokun    val req_info = new L2TlbInnerBundle()
684d0de7e4aSpeixiaokun    val level = UInt(log2Up(Level).W)
685d0de7e4aSpeixiaokun  })
686d0de7e4aSpeixiaokun  val pmp = new Bundle {
687d0de7e4aSpeixiaokun    val req = ValidIO(new PMPReqBundle())
688d0de7e4aSpeixiaokun    val resp = Flipped(new PMPRespBundle())
689d0de7e4aSpeixiaokun  }
690d0de7e4aSpeixiaokun}
691d0de7e4aSpeixiaokun
692d0de7e4aSpeixiaokun@chiselName
693d0de7e4aSpeixiaokunclass HPTW()(implicit p: Parameters) extends XSModule with HasPtwConst {
694d0de7e4aSpeixiaokun  val io = IO(new HPTWIO)
695d0de7e4aSpeixiaokun  val hgatp = io.csr.hgatp
696d0de7e4aSpeixiaokun  val sfence = io.sfence
697d0de7e4aSpeixiaokun  val flush = sfence.valid || hgatp.changed
698d0de7e4aSpeixiaokun
699d0de7e4aSpeixiaokun  val level = RegInit(0.U(log2Up(Level).W))
700d0de7e4aSpeixiaokun  val gpaddr = Reg(UInt(GPAddrBits.W))
701d0de7e4aSpeixiaokun  val vpn = gpaddr(GPAddrBits-1, offLen)
702d0de7e4aSpeixiaokun  val levelNext = level + 1.U
703d0de7e4aSpeixiaokun  val l1Hit = Reg(Bool())
704d0de7e4aSpeixiaokun  val l2Hit = Reg(Bool())
705b24e0a78Speixiaokun  val pg_base = MakeGPAddr(hgatp.ppn, getGVpnn(vpn, 2.U)) // for l0
706d0de7e4aSpeixiaokun//  val pte = io.mem.resp.bits.MergeRespToPte()
707d0de7e4aSpeixiaokun  val pte = io.mem.resp.bits.asTypeOf(new PteBundle().cloneType)
7086315ba2aSpeixiaokun  val ppn_l1 = Mux(l1Hit, io.req.bits.ppn, pte.ppn)
7096315ba2aSpeixiaokun  val ppn_l2 = Mux(l2Hit, io.req.bits.ppn, pte.ppn)
7106315ba2aSpeixiaokun  val ppn = Mux(level === 1.U, ppn_l1, ppn_l2) //for l1 and l2
7116315ba2aSpeixiaokun  val p_pte = MakeAddr(ppn, getVpnn(vpn, 2.U - level))
712d0de7e4aSpeixiaokun  val mem_addr = Mux(level === 0.U, pg_base, p_pte)
713d0de7e4aSpeixiaokun
714d0de7e4aSpeixiaokun  //s/w register
715d0de7e4aSpeixiaokun  val s_pmp_check = RegInit(true.B)
716d0de7e4aSpeixiaokun  val s_mem_req = RegInit(true.B)
717d0de7e4aSpeixiaokun  val w_mem_resp = RegInit(true.B)
718d0de7e4aSpeixiaokun  val idle = RegInit(true.B)
71903c1129fSpeixiaokun  val mem_addr_update = RegInit(false.B)
720d0de7e4aSpeixiaokun  val finish = WireInit(false.B)
721d0de7e4aSpeixiaokun
722d0de7e4aSpeixiaokun  val sent_to_pmp = !idle && (!s_pmp_check || mem_addr_update) && !finish
723d0de7e4aSpeixiaokun  val pageFault = pte.isPf(level)
724d0de7e4aSpeixiaokun  val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, sent_to_pmp)
725d0de7e4aSpeixiaokun
726d0de7e4aSpeixiaokun  val ppn_af = pte.isAf()
727d0de7e4aSpeixiaokun  val find_pte = pte.isLeaf() || ppn_af || pageFault
728d0de7e4aSpeixiaokun
729d0de7e4aSpeixiaokun  val resp_valid = !idle && mem_addr_update && ((w_mem_resp && find_pte) || (s_pmp_check && accessFault))
730d0de7e4aSpeixiaokun  val id = Reg(UInt(log2Up(l2tlbParams.llptwsize).W))
731eb4bf3f2Speixiaokun  val source = RegEnable(io.req.bits.source, io.req.fire())
732eb4bf3f2Speixiaokun
733d0de7e4aSpeixiaokun  io.req.ready := idle
734eb4bf3f2Speixiaokun  val resp = Wire(new HptwResp())
735d0de7e4aSpeixiaokun  resp.apply(pageFault && !accessFault && !ppn_af, accessFault || ppn_af, level, pte, vpn, hgatp.asid)
736d0de7e4aSpeixiaokun  io.resp.valid := resp_valid
737d0de7e4aSpeixiaokun  io.resp.bits.id := id
738d0de7e4aSpeixiaokun  io.resp.bits.resp := resp
739eb4bf3f2Speixiaokun  io.resp.bits.source := source
740d0de7e4aSpeixiaokun
741d0de7e4aSpeixiaokun  io.pmp.req.valid := DontCare
742d0de7e4aSpeixiaokun  io.pmp.req.bits.addr := mem_addr
743d0de7e4aSpeixiaokun  io.pmp.req.bits.size := 3.U
744d0de7e4aSpeixiaokun  io.pmp.req.bits.cmd := TlbCmd.read
745d0de7e4aSpeixiaokun
746d0de7e4aSpeixiaokun  io.mem.req.valid := !s_mem_req && !io.mem.mask && !accessFault && s_pmp_check
747d0de7e4aSpeixiaokun  io.mem.req.bits.addr := mem_addr
748d0de7e4aSpeixiaokun  io.mem.req.bits.id := HptwReqId.U(bMemID.W)
749d0de7e4aSpeixiaokun
75082978df9Speixiaokun  io.refill.req_info.vpn := vpn
751d0de7e4aSpeixiaokun  io.refill.level := level
752eb4bf3f2Speixiaokun  io.refill.req_info.source := source
753eb4bf3f2Speixiaokun  io.refill.req_info.s2xlate := onlyStage2
754d0de7e4aSpeixiaokun  when (idle){
755d0de7e4aSpeixiaokun    when(io.req.fire()){
756d0de7e4aSpeixiaokun      level := Mux(io.req.bits.l2Hit, 2.U, Mux(io.req.bits.l1Hit, 1.U, 0.U))
757d0de7e4aSpeixiaokun      idle := false.B
758d0de7e4aSpeixiaokun      gpaddr := Cat(io.req.bits.gvpn, 0.U(offLen.W))
759d0de7e4aSpeixiaokun      accessFault := false.B
760d0de7e4aSpeixiaokun      s_pmp_check := false.B
761d0de7e4aSpeixiaokun      id := io.req.bits.id
762d0de7e4aSpeixiaokun      l1Hit := io.req.bits.l1Hit
763d0de7e4aSpeixiaokun      l2Hit := io.req.bits.l2Hit
764d0de7e4aSpeixiaokun    }
765d0de7e4aSpeixiaokun  }
766d0de7e4aSpeixiaokun
767d0de7e4aSpeixiaokun  when(sent_to_pmp && !mem_addr_update){
768d0de7e4aSpeixiaokun    s_mem_req := false.B
769d0de7e4aSpeixiaokun    s_pmp_check := true.B
770d0de7e4aSpeixiaokun  }
771d0de7e4aSpeixiaokun
772d0de7e4aSpeixiaokun  when(accessFault && !idle){
773d0de7e4aSpeixiaokun    s_pmp_check := true.B
774d0de7e4aSpeixiaokun    s_mem_req := true.B
775d0de7e4aSpeixiaokun    w_mem_resp := true.B
776d0de7e4aSpeixiaokun    mem_addr_update := true.B
777d0de7e4aSpeixiaokun  }
778d0de7e4aSpeixiaokun
779d0de7e4aSpeixiaokun  when(io.mem.req.fire()){
780d0de7e4aSpeixiaokun    s_mem_req := true.B
781d0de7e4aSpeixiaokun    w_mem_resp := false.B
782d0de7e4aSpeixiaokun  }
783d0de7e4aSpeixiaokun
784d0de7e4aSpeixiaokun  when(io.mem.resp.fire() && !w_mem_resp){
785d0de7e4aSpeixiaokun    w_mem_resp := true.B
786d0de7e4aSpeixiaokun    mem_addr_update := true.B
787d0de7e4aSpeixiaokun  }
788d0de7e4aSpeixiaokun
789d0de7e4aSpeixiaokun  when(mem_addr_update){
790d0de7e4aSpeixiaokun    when(!(find_pte || accessFault)){
791d0de7e4aSpeixiaokun      level := levelNext
792d0de7e4aSpeixiaokun      s_mem_req := false.B
793d0de7e4aSpeixiaokun      mem_addr_update := false.B
794d0de7e4aSpeixiaokun    }.elsewhen(resp_valid){
795d0de7e4aSpeixiaokun      when(io.resp.fire()){
796d0de7e4aSpeixiaokun        idle := true.B
797d0de7e4aSpeixiaokun        mem_addr_update := false.B
798d0de7e4aSpeixiaokun        accessFault := false.B
799d0de7e4aSpeixiaokun      }
800d0de7e4aSpeixiaokun      finish := true.B
801d0de7e4aSpeixiaokun    }
802d0de7e4aSpeixiaokun  }
803d0de7e4aSpeixiaokun}