xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/PageTableWalker.scala (revision e4c1a68e04e2f2f5fcd6d56aeea8e2af0543e3c6)
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)
1293222d00fSpeixiaokun  val stage1Hit = RegEnable(io.req.bits.stage1Hit, io.req.fire)
1303222d00fSpeixiaokun  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
1423222d00fSpeixiaokun  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)),
146dcb10e8fSBL-GS    !s_last_hptw_req -> Cat(MuxLookup(level, pte.ppn)(Seq(
14737fc3812SBL-GS      0.U -> Cat(pte.ppn(ppnLen - 1, vpnnLen * 2), vpn(vpnnLen * 2 - 1, 0)),
14837fc3812SBL-GS      1.U -> Cat(pte.ppn(ppnLen - 1, vpnnLen), vpn(vpnnLen - 1, 0)
149dcb10e8fSBL-GS    ))),
150dcb10e8fSBL-GS    0.U(offLen.W))
151c0991f6aSpeixiaokun  ))
152cda84113Speixiaokun  val hpaddr = Cat(hptw_resp.genPPNS2(get_pn(gpaddr)), get_off(gpaddr))
153d0de7e4aSpeixiaokun
15444b79566SXiaokun-Pei  io.req.ready := idle
15530104977Speixiaokun  val ptw_resp = Wire(new PtwMergeResp)
15630104977Speixiaokun  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)
15744b79566SXiaokun-Pei
15809280d15Speixiaokun  val normal_resp = idle === false.B && mem_addr_update && !last_s2xlate && ((w_mem_resp && find_pte) || (s_pmp_check && accessFault) || onlyS2xlate)
15909280d15Speixiaokun  val stageHit_resp = idle === false.B && hptw_resp_stage2
16009280d15Speixiaokun  io.resp.valid := Mux(stage1Hit, stageHit_resp, normal_resp)
16144b79566SXiaokun-Pei  io.resp.bits.source := source
16230104977Speixiaokun  io.resp.bits.resp := Mux(stage1Hit, stage1, ptw_resp)
16379d4b70cSpeixiaokun  io.resp.bits.h_resp := hptw_resp
1646315ba2aSpeixiaokun  io.resp.bits.s2xlate := req_s2xlate
16544b79566SXiaokun-Pei
16644b79566SXiaokun-Pei  io.llptw.valid := s_llptw_req === false.B && to_find_pte && !accessFault
16744b79566SXiaokun-Pei  io.llptw.bits.req_info.source := source
16844b79566SXiaokun-Pei  io.llptw.bits.req_info.vpn := vpn
16982978df9Speixiaokun  io.llptw.bits.req_info.s2xlate := req_s2xlate
170eb4bf3f2Speixiaokun  io.llptw.bits.ppn := DontCare
17144b79566SXiaokun-Pei
172b6982e83SLemover  io.pmp.req.valid := DontCare // samecycle, do not use valid
173d0de7e4aSpeixiaokun  io.pmp.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr)
174b6982e83SLemover  io.pmp.req.bits.size := 3.U // TODO: fix it
175b6982e83SLemover  io.pmp.req.bits.cmd := TlbCmd.read
176b6982e83SLemover
17744b79566SXiaokun-Pei  mem.req.valid := s_mem_req === false.B && !mem.mask && !accessFault && s_pmp_check
178d0de7e4aSpeixiaokun  mem.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr)
179bc063562SLemover  mem.req.bits.id := FsmReqID.U(bMemID.W)
18083d93d53Speixiaokun  mem.req.bits.hptw_bypassed := false.B
1816d5ddbceSLemover
182933ec998Speixiaokun  io.refill.req_info.s2xlate := Mux(enableS2xlate, onlyStage1, req_s2xlate) // ptw refill the pte of stage 1 when s2xlate is enabled
18345f497a4Shappy-lx  io.refill.req_info.vpn := vpn
1846d5ddbceSLemover  io.refill.level := level
18545f497a4Shappy-lx  io.refill.req_info.source := source
1866d5ddbceSLemover
187d0de7e4aSpeixiaokun  io.hptw.req.valid := !s_hptw_req || !s_last_hptw_req
188d0de7e4aSpeixiaokun  io.hptw.req.bits.id := FsmReqID.U(bMemID.W)
189dcb10e8fSBL-GS  io.hptw.req.bits.gvpn := get_pn(gpaddr)
190eb4bf3f2Speixiaokun  io.hptw.req.bits.source := source
191d0de7e4aSpeixiaokun
1923222d00fSpeixiaokun  when (io.req.fire && io.req.bits.stage1Hit){
19330104977Speixiaokun    idle := false.B
19461c5d636Speixiaokun    req_s2xlate := io.req.bits.req_info.s2xlate
19530104977Speixiaokun    s_hptw_req := false.B
19609280d15Speixiaokun    hptw_resp_stage2 := false.B
19730104977Speixiaokun  }
198d0de7e4aSpeixiaokun
1993222d00fSpeixiaokun  when (io.hptw.resp.fire && w_hptw_resp === false.B && stage1Hit){
20030104977Speixiaokun    w_hptw_resp := true.B
20109280d15Speixiaokun    hptw_resp_stage2 := true.B
20230104977Speixiaokun  }
20330104977Speixiaokun
2043222d00fSpeixiaokun  when (io.resp.fire && stage1Hit){
20530104977Speixiaokun    idle := true.B
20630104977Speixiaokun  }
20730104977Speixiaokun
2083222d00fSpeixiaokun  when (io.req.fire && !io.req.bits.stage1Hit){
20944b79566SXiaokun-Pei    val req = io.req.bits
21044b79566SXiaokun-Pei    level := Mux(req.l1Hit, 1.U, 0.U)
21144b79566SXiaokun-Pei    af_level := Mux(req.l1Hit, 1.U, 0.U)
21244b79566SXiaokun-Pei    ppn := Mux(req.l1Hit, io.req.bits.ppn, satp.ppn)
21344b79566SXiaokun-Pei    vpn := io.req.bits.req_info.vpn
21444b79566SXiaokun-Pei    l1Hit := req.l1Hit
21544b79566SXiaokun-Pei    accessFault := false.B
21644b79566SXiaokun-Pei    idle := false.B
217d0de7e4aSpeixiaokun    hptw_pageFault := false.B
21850c7aa78Speixiaokun    req_s2xlate := io.req.bits.req_info.s2xlate
21982978df9Speixiaokun    when(io.req.bits.req_info.s2xlate =/= noS2xlate && io.req.bits.req_info.s2xlate =/= onlyStage1){
220d0de7e4aSpeixiaokun      last_s2xlate := true.B
221d0de7e4aSpeixiaokun      s_hptw_req := false.B
222d0de7e4aSpeixiaokun    }.otherwise {
223d0de7e4aSpeixiaokun      s_pmp_check := false.B
224d0de7e4aSpeixiaokun    }
225d0de7e4aSpeixiaokun  }
226d0de7e4aSpeixiaokun
2273222d00fSpeixiaokun  when(io.hptw.req.fire && s_hptw_req === false.B){
228d0de7e4aSpeixiaokun    s_hptw_req := true.B
229d0de7e4aSpeixiaokun    w_hptw_resp := false.B
230d0de7e4aSpeixiaokun  }
231d0de7e4aSpeixiaokun
2323222d00fSpeixiaokun  when(io.hptw.resp.fire && w_hptw_resp === false.B && !stage1Hit) {
233d0de7e4aSpeixiaokun    hptw_pageFault := io.hptw.resp.bits.h_resp.gpf
234d0de7e4aSpeixiaokun    hptw_accessFault := io.hptw.resp.bits.h_resp.gaf
235d0de7e4aSpeixiaokun    w_hptw_resp := true.B
236d0de7e4aSpeixiaokun    when(onlyS2xlate){
237d0de7e4aSpeixiaokun      mem_addr_update := true.B
238d0de7e4aSpeixiaokun      last_s2xlate := false.B
239d0de7e4aSpeixiaokun    }.otherwise {
240d0de7e4aSpeixiaokun      s_pmp_check := false.B
241d0de7e4aSpeixiaokun    }
242d0de7e4aSpeixiaokun  }
243d0de7e4aSpeixiaokun
2443222d00fSpeixiaokun  when(io.hptw.req.fire && s_last_hptw_req === false.B) {
245d0de7e4aSpeixiaokun    w_last_hptw_resp := false.B
246d0de7e4aSpeixiaokun    s_last_hptw_req := true.B
247d0de7e4aSpeixiaokun  }
248d0de7e4aSpeixiaokun
2493222d00fSpeixiaokun  when(io.hptw.resp.fire && w_last_hptw_resp === false.B){
250d0de7e4aSpeixiaokun    hptw_pageFault := io.hptw.resp.bits.h_resp.gpf
251d0de7e4aSpeixiaokun    hptw_accessFault := io.hptw.resp.bits.h_resp.gaf
252d0de7e4aSpeixiaokun    w_last_hptw_resp := true.B
253d0de7e4aSpeixiaokun    mem_addr_update := true.B
254d0de7e4aSpeixiaokun    last_s2xlate := false.B
25544b79566SXiaokun-Pei  }
25644b79566SXiaokun-Pei
25744b79566SXiaokun-Pei  when(sent_to_pmp && mem_addr_update === false.B){
25844b79566SXiaokun-Pei    s_mem_req := false.B
25944b79566SXiaokun-Pei    s_pmp_check := true.B
26044b79566SXiaokun-Pei  }
26144b79566SXiaokun-Pei
26244b79566SXiaokun-Pei  when(accessFault && idle === false.B){
26344b79566SXiaokun-Pei    s_pmp_check := true.B
26444b79566SXiaokun-Pei    s_mem_req := true.B
26544b79566SXiaokun-Pei    w_mem_resp := true.B
26644b79566SXiaokun-Pei    s_llptw_req := true.B
267d0de7e4aSpeixiaokun    s_hptw_req := true.B
268d0de7e4aSpeixiaokun    w_hptw_resp := true.B
269d0de7e4aSpeixiaokun    s_last_hptw_req := true.B
270d0de7e4aSpeixiaokun    w_last_hptw_resp := true.B
27144b79566SXiaokun-Pei    mem_addr_update := true.B
272d0de7e4aSpeixiaokun    last_s2xlate := false.B
27344b79566SXiaokun-Pei  }
27444b79566SXiaokun-Pei
275935edac4STang Haojin  when (mem.req.fire){
27644b79566SXiaokun-Pei    s_mem_req := true.B
27744b79566SXiaokun-Pei    w_mem_resp := false.B
27844b79566SXiaokun-Pei  }
27944b79566SXiaokun-Pei
280935edac4STang Haojin  when(mem.resp.fire && w_mem_resp === false.B){
28144b79566SXiaokun-Pei    w_mem_resp := true.B
28244b79566SXiaokun-Pei    af_level := af_level + 1.U
28344b79566SXiaokun-Pei    s_llptw_req := false.B
28444b79566SXiaokun-Pei    mem_addr_update := true.B
28544b79566SXiaokun-Pei  }
28644b79566SXiaokun-Pei
28744b79566SXiaokun-Pei  when(mem_addr_update){
288e1e498e7SBL-GS    when(level === 0.U && !onlyS2xlate && !(find_pte || accessFault)){
28944b79566SXiaokun-Pei      level := levelNext
290d0de7e4aSpeixiaokun      when(s2xlate){
291d0de7e4aSpeixiaokun        s_hptw_req := false.B
292d0de7e4aSpeixiaokun      }.otherwise{
29344b79566SXiaokun-Pei        s_mem_req := false.B
294d0de7e4aSpeixiaokun      }
29544b79566SXiaokun-Pei      s_llptw_req := true.B
29644b79566SXiaokun-Pei      mem_addr_update := false.B
2972a906a65SHaoyuan Feng    }.elsewhen(io.llptw.valid){
298935edac4STang Haojin      when(io.llptw.fire) {
29944b79566SXiaokun-Pei        idle := true.B
30044b79566SXiaokun-Pei        s_llptw_req := true.B
30144b79566SXiaokun-Pei        mem_addr_update := false.B
302d0de7e4aSpeixiaokun        last_s2xlate := false.B
3032a906a65SHaoyuan Feng      }
3042a906a65SHaoyuan Feng      finish := true.B
305d0de7e4aSpeixiaokun    }.elsewhen(s2xlate && last_s2xlate === true.B) {
306d0de7e4aSpeixiaokun      s_last_hptw_req := false.B
307d0de7e4aSpeixiaokun      mem_addr_update := false.B
3082a906a65SHaoyuan Feng    }.elsewhen(io.resp.valid){
309935edac4STang Haojin      when(io.resp.fire) {
31044b79566SXiaokun-Pei        idle := true.B
31144b79566SXiaokun-Pei        s_llptw_req := true.B
31244b79566SXiaokun-Pei        mem_addr_update := false.B
31344b79566SXiaokun-Pei        accessFault := false.B
31444b79566SXiaokun-Pei      }
3152a906a65SHaoyuan Feng      finish := true.B
3162a906a65SHaoyuan Feng    }
31744b79566SXiaokun-Pei  }
31844b79566SXiaokun-Pei
31944b79566SXiaokun-Pei
32044b79566SXiaokun-Pei  when (sfence.valid) {
32144b79566SXiaokun-Pei    idle := true.B
32244b79566SXiaokun-Pei    s_pmp_check := true.B
32344b79566SXiaokun-Pei    s_mem_req := true.B
32444b79566SXiaokun-Pei    s_llptw_req := true.B
32544b79566SXiaokun-Pei    w_mem_resp := true.B
32644b79566SXiaokun-Pei    accessFault := false.B
327d826bce1SHaoyuan Feng    mem_addr_update := false.B
328d0de7e4aSpeixiaokun    s_hptw_req := true.B
329d0de7e4aSpeixiaokun    w_hptw_resp := true.B
330d0de7e4aSpeixiaokun    s_last_hptw_req := true.B
331d0de7e4aSpeixiaokun    w_last_hptw_resp := true.B
33244b79566SXiaokun-Pei  }
33344b79566SXiaokun-Pei
33444b79566SXiaokun-Pei
33544b79566SXiaokun-Pei  XSDebug(p"[ptw] level:${level} notFound:${pageFault}\n")
3366d5ddbceSLemover
3376d5ddbceSLemover  // perf
338935edac4STang Haojin  XSPerfAccumulate("fsm_count", io.req.fire)
3396d5ddbceSLemover  for (i <- 0 until PtwWidth) {
340935edac4STang Haojin    XSPerfAccumulate(s"fsm_count_source${i}", io.req.fire && io.req.bits.req_info.source === i.U)
3416d5ddbceSLemover  }
34244b79566SXiaokun-Pei  XSPerfAccumulate("fsm_busy", !idle)
34344b79566SXiaokun-Pei  XSPerfAccumulate("fsm_idle", idle)
3446d5ddbceSLemover  XSPerfAccumulate("resp_blocked", io.resp.valid && !io.resp.ready)
345dd7fe201SHaoyuan Feng  XSPerfAccumulate("ptw_ppn_af", io.resp.fire && ppn_af)
346935edac4STang Haojin  XSPerfAccumulate("mem_count", mem.req.fire)
347935edac4STang Haojin  XSPerfAccumulate("mem_cycle", BoolStopWatch(mem.req.fire, mem.resp.fire, true))
3486d5ddbceSLemover  XSPerfAccumulate("mem_blocked", mem.req.valid && !mem.req.ready)
349cc5a5f22SLemover
35044b79566SXiaokun-Pei  TimeOutAssert(!idle, timeOutThreshold, "page table walker time out")
351cd365d4cSrvcoresjw
352cd365d4cSrvcoresjw  val perfEvents = Seq(
353935edac4STang Haojin    ("fsm_count         ", io.req.fire                                     ),
35444b79566SXiaokun-Pei    ("fsm_busy          ", !idle                                             ),
35544b79566SXiaokun-Pei    ("fsm_idle          ", idle                                              ),
356cd365d4cSrvcoresjw    ("resp_blocked      ", io.resp.valid && !io.resp.ready                   ),
357935edac4STang Haojin    ("mem_count         ", mem.req.fire                                    ),
358935edac4STang Haojin    ("mem_cycle         ", BoolStopWatch(mem.req.fire, mem.resp.fire, true)),
359cd365d4cSrvcoresjw    ("mem_blocked       ", mem.req.valid && !mem.req.ready                   ),
360cd365d4cSrvcoresjw  )
3611ca0e4f3SYinan Xu  generatePerfEvent()
3626d5ddbceSLemover}
36392e3bfefSLemover
36492e3bfefSLemover/*========================= LLPTW ==============================*/
36592e3bfefSLemover
36692e3bfefSLemover/** LLPTW : Last Level Page Table Walker
36792e3bfefSLemover  * the page walker that only takes 4KB(last level) page walk.
36892e3bfefSLemover  **/
36992e3bfefSLemover
37092e3bfefSLemoverclass LLPTWInBundle(implicit p: Parameters) extends XSBundle with HasPtwConst {
37192e3bfefSLemover  val req_info = Output(new L2TlbInnerBundle())
372d61cd5eeSpeixiaokun  val ppn = Output(if(HasHExtension) UInt((vpnLen.max(ppnLen)).W) else UInt(ppnLen.W))
37392e3bfefSLemover}
37492e3bfefSLemover
37592e3bfefSLemoverclass LLPTWIO(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
37692e3bfefSLemover  val in = Flipped(DecoupledIO(new LLPTWInBundle()))
37792e3bfefSLemover  val out = DecoupledIO(new Bundle {
37892e3bfefSLemover    val req_info = Output(new L2TlbInnerBundle())
37992e3bfefSLemover    val id = Output(UInt(bMemID.W))
380d0de7e4aSpeixiaokun    val h_resp = Output(new HptwResp)
38192e3bfefSLemover    val af = Output(Bool())
38292e3bfefSLemover  })
38392e3bfefSLemover  val mem = new Bundle {
38492e3bfefSLemover    val req = DecoupledIO(new L2TlbMemReqBundle())
38592e3bfefSLemover    val resp = Flipped(Valid(new Bundle {
38692e3bfefSLemover      val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W))
387ce5f4200SGuanghui Hu      val value = Output(UInt(blockBits.W))
38892e3bfefSLemover    }))
38992e3bfefSLemover    val enq_ptr = Output(UInt(log2Ceil(l2tlbParams.llptwsize).W))
39092e3bfefSLemover    val buffer_it = Output(Vec(l2tlbParams.llptwsize, Bool()))
39192e3bfefSLemover    val refill = Output(new L2TlbInnerBundle())
39292e3bfefSLemover    val req_mask = Input(Vec(l2tlbParams.llptwsize, Bool()))
39392e3bfefSLemover  }
3947797f035SbugGenerator  val cache = DecoupledIO(new L2TlbInnerBundle())
39592e3bfefSLemover  val pmp = new Bundle {
39692e3bfefSLemover    val req = Valid(new PMPReqBundle())
39792e3bfefSLemover    val resp = Flipped(new PMPRespBundle())
39892e3bfefSLemover  }
399d0de7e4aSpeixiaokun  val hptw = new Bundle {
400d0de7e4aSpeixiaokun    val req = DecoupledIO(new Bundle{
401eb4bf3f2Speixiaokun      val source = UInt(bSourceWidth.W)
402d0de7e4aSpeixiaokun      val id = UInt(log2Up(l2tlbParams.llptwsize).W)
40382978df9Speixiaokun      val gvpn = UInt(vpnLen.W)
404d0de7e4aSpeixiaokun    })
405d0de7e4aSpeixiaokun    val resp = Flipped(Valid(new Bundle {
406d0de7e4aSpeixiaokun      val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W))
407d0de7e4aSpeixiaokun      val h_resp = Output(new HptwResp)
408d0de7e4aSpeixiaokun    }))
409d0de7e4aSpeixiaokun  }
41092e3bfefSLemover}
41192e3bfefSLemover
41292e3bfefSLemoverclass LLPTWEntry(implicit p: Parameters) extends XSBundle with HasPtwConst {
41392e3bfefSLemover  val req_info = new L2TlbInnerBundle()
41492e3bfefSLemover  val ppn = UInt(ppnLen.W)
41592e3bfefSLemover  val wait_id = UInt(log2Up(l2tlbParams.llptwsize).W)
41692e3bfefSLemover  val af = Bool()
417dc05c713Speixiaokun  val hptw_resp = new HptwResp()
41892e3bfefSLemover}
41992e3bfefSLemover
42092e3bfefSLemover
42192e3bfefSLemoverclass LLPTW(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents {
42292e3bfefSLemover  val io = IO(new LLPTWIO())
42382978df9Speixiaokun  val enableS2xlate = io.in.bits.req_info.s2xlate =/= noS2xlate
424d0de7e4aSpeixiaokun  val satp = Mux(enableS2xlate, io.csr.vsatp, io.csr.satp)
42592e3bfefSLemover
426d0de7e4aSpeixiaokun  val flush = io.sfence.valid || satp.changed
42792e3bfefSLemover  val entries = Reg(Vec(l2tlbParams.llptwsize, new LLPTWEntry()))
428d0de7e4aSpeixiaokun  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)
42992e3bfefSLemover  val state = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(state_idle)))
4307797f035SbugGenerator
43192e3bfefSLemover  val is_emptys = state.map(_ === state_idle)
43292e3bfefSLemover  val is_mems = state.map(_ === state_mem_req)
43392e3bfefSLemover  val is_waiting = state.map(_ === state_mem_waiting)
43492e3bfefSLemover  val is_having = state.map(_ === state_mem_out)
4357797f035SbugGenerator  val is_cache = state.map(_ === state_cache)
436d0de7e4aSpeixiaokun  val is_hptw_req = state.map(_ === state_hptw_req)
437d0de7e4aSpeixiaokun  val is_last_hptw_req = state.map(_ === state_last_hptw_req)
438b7bdb307Speixiaokun  val is_hptw_resp = state.map(_ === state_hptw_resp)
439b7bdb307Speixiaokun  val is_last_hptw_resp = state.map(_ === state_last_hptw_resp)
44092e3bfefSLemover
441935edac4STang Haojin  val full = !ParallelOR(is_emptys).asBool
44292e3bfefSLemover  val enq_ptr = ParallelPriorityEncoder(is_emptys)
44392e3bfefSLemover
4447797f035SbugGenerator  val mem_ptr = ParallelPriorityEncoder(is_having) // TODO: optimize timing, bad: entries -> ptr -> entry
44592e3bfefSLemover  val mem_arb = Module(new RRArbiter(new LLPTWEntry(), l2tlbParams.llptwsize))
44692e3bfefSLemover  for (i <- 0 until l2tlbParams.llptwsize) {
44792e3bfefSLemover    mem_arb.io.in(i).bits := entries(i)
44892e3bfefSLemover    mem_arb.io.in(i).valid := is_mems(i) && !io.mem.req_mask(i)
44992e3bfefSLemover  }
4502a1f48e7Speixiaokun
4512a1f48e7Speixiaokun  // process hptw requests in serial
452d0de7e4aSpeixiaokun  val hyper_arb1 = Module(new RRArbiter(new LLPTWEntry(), l2tlbParams.llptwsize))
453d0de7e4aSpeixiaokun  for (i <- 0 until l2tlbParams.llptwsize) {
454d0de7e4aSpeixiaokun    hyper_arb1.io.in(i).bits := entries(i)
4552a1f48e7Speixiaokun    hyper_arb1.io.in(i).valid := is_hptw_req(i) && !(Cat(is_hptw_resp).orR) && !(Cat(is_last_hptw_resp).orR)
456d0de7e4aSpeixiaokun  }
457d0de7e4aSpeixiaokun  val hyper_arb2 = Module(new RRArbiter(new LLPTWEntry(), l2tlbParams.llptwsize))
458d0de7e4aSpeixiaokun  for(i <- 0 until l2tlbParams.llptwsize) {
459d0de7e4aSpeixiaokun    hyper_arb2.io.in(i).bits := entries(i)
4602a1f48e7Speixiaokun    hyper_arb2.io.in(i).valid := is_last_hptw_req(i) && !(Cat(is_hptw_resp).orR) && !(Cat(is_last_hptw_resp).orR)
461d0de7e4aSpeixiaokun  }
46292e3bfefSLemover
463f3034303SHaoyuan Feng  val cache_ptr = ParallelMux(is_cache, (0 until l2tlbParams.llptwsize).map(_.U(log2Up(l2tlbParams.llptwsize).W)))
4647797f035SbugGenerator
46592e3bfefSLemover  // duplicate req
46692e3bfefSLemover  // to_wait: wait for the last to access mem, set to mem_resp
46792e3bfefSLemover  // to_cache: the last is back just right now, set to mem_cache
46892e3bfefSLemover  val dup_vec = state.indices.map(i =>
469cca17e78Speixiaokun    dup(io.in.bits.req_info.vpn, entries(i).req_info.vpn) && io.in.bits.req_info.s2xlate === entries(i).req_info.s2xlate
47092e3bfefSLemover  )
471cca17e78Speixiaokun  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
47292e3bfefSLemover  val dup_vec_wait = dup_vec.zip(is_waiting).map{case (d, w) => d && w} // dup with "mem_waiting" entres, sending mem req already
47392e3bfefSLemover  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
474951f37e5Speixiaokun  val dup_vec_last_hptw = dup_vec.zipWithIndex.map{case (d, i) => d && (is_last_hptw_req(i) || is_last_hptw_resp(i))}
47592e3bfefSLemover  val wait_id = Mux(dup_req_fire, mem_arb.io.chosen, ParallelMux(dup_vec_wait zip entries.map(_.wait_id)))
476935edac4STang 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
47792e3bfefSLemover  val to_wait = Cat(dup_vec_wait).orR || dup_req_fire
4784895c345Speixiaokun  val to_mem_out = dup_wait_resp && entries(io.mem.resp.bits.id).req_info.s2xlate === noS2xlate
479951f37e5Speixiaokun  val to_cache = Cat(dup_vec_having).orR || Cat(dup_vec_last_hptw).orR
4804358f287Speixiaokun  val to_hptw_req = io.in.bits.req_info.s2xlate =/= noS2xlate
4819467c5f4Speixiaokun  val to_last_hptw_req = dup_wait_resp && entries(io.mem.resp.bits.id).req_info.s2xlate =/= noS2xlate
4829467c5f4Speixiaokun  val last_hptw_req_id = io.mem.resp.bits.id
4839467c5f4Speixiaokun  val req_paddr = MakeAddr(io.in.bits.ppn, getVpnn(io.in.bits.req_info.vpn, 0))
4849467c5f4Speixiaokun  val req_hpaddr = MakeAddr(entries(last_hptw_req_id).hptw_resp.genPPNS2(get_pn(req_paddr)), getVpnn(io.in.bits.req_info.vpn, 0))
4859467c5f4Speixiaokun  val index =  Mux(entries(last_hptw_req_id).req_info.s2xlate === allStage, req_hpaddr, req_paddr)(log2Up(l2tlbParams.blockBytes)-1, log2Up(XLEN/8))
4869467c5f4Speixiaokun  val last_hptw_req_ppn = io.mem.resp.bits.value.asTypeOf(Vec(blockBits / XLEN, new PteBundle()))(index).ppn
4877797f035SbugGenerator  XSError(RegNext(dup_req_fire && Cat(dup_vec_wait).orR, init = false.B), "mem req but some entries already waiting, should not happed")
48892e3bfefSLemover
489935edac4STang Haojin  XSError(io.in.fire && ((to_mem_out && to_cache) || (to_wait && to_cache)), "llptw enq, to cache conflict with to mem")
49092e3bfefSLemover  val mem_resp_hit = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(false.B)))
4917274ec5cSpeixiaokun  val enq_state_normal = MuxCase(state_addr_check, Seq(
4927274ec5cSpeixiaokun    to_mem_out -> state_mem_out, // same to the blew, but the mem resp now
493871d1438Speixiaokun    to_last_hptw_req -> state_last_hptw_req,
4947274ec5cSpeixiaokun    to_wait -> state_mem_waiting,
4957274ec5cSpeixiaokun    to_cache -> state_cache,
496871d1438Speixiaokun    to_hptw_req -> state_hptw_req
4977274ec5cSpeixiaokun  ))
4987797f035SbugGenerator  val enq_state = Mux(from_pre(io.in.bits.req_info.source) && enq_state_normal =/= state_addr_check, state_idle, enq_state_normal)
499935edac4STang Haojin  when (io.in.fire) {
50092e3bfefSLemover    // if prefetch req does not need mem access, just give it up.
50192e3bfefSLemover    // so there will be at most 1 + FilterSize entries that needs re-access page cache
50292e3bfefSLemover    // so 2 + FilterSize is enough to avoid dead-lock
5037797f035SbugGenerator    state(enq_ptr) := enq_state
50492e3bfefSLemover    entries(enq_ptr).req_info := io.in.bits.req_info
5059467c5f4Speixiaokun    entries(enq_ptr).ppn := Mux(to_last_hptw_req, last_hptw_req_ppn, io.in.bits.ppn)
50692e3bfefSLemover    entries(enq_ptr).wait_id := Mux(to_wait, wait_id, enq_ptr)
50792e3bfefSLemover    entries(enq_ptr).af := false.B
5082a1f48e7Speixiaokun    entries(enq_ptr).hptw_resp := Mux(to_last_hptw_req, entries(last_hptw_req_id).hptw_resp, Mux(to_wait, entries(wait_id).hptw_resp, entries(enq_ptr).hptw_resp))
50992e3bfefSLemover    mem_resp_hit(enq_ptr) := to_mem_out
51092e3bfefSLemover  }
5117797f035SbugGenerator
5127797f035SbugGenerator  val enq_ptr_reg = RegNext(enq_ptr)
5133222d00fSpeixiaokun  val need_addr_check = RegNext(enq_state === state_addr_check && io.in.fire && !flush)
5147274ec5cSpeixiaokun
5150214776eSpeixiaokun  val hasHptwResp = ParallelOR(state.map(_ === state_hptw_resp)).asBool
5167274ec5cSpeixiaokun  val hptw_resp_ptr_reg = RegNext(io.hptw.resp.bits.id)
517a664078aSpeixiaokun  val hptw_need_addr_check = RegNext(hasHptwResp && io.hptw.resp.fire && !flush) && state(hptw_resp_ptr_reg) === state_addr_check
518d0de7e4aSpeixiaokun
519ce5f4200SGuanghui Hu  val ptes = io.mem.resp.bits.value.asTypeOf(Vec(blockBits / XLEN, new PteBundle()))
520b24e0a78Speixiaokun  val gpaddr = MakeGPAddr(io.in.bits.ppn, getVpnn(io.in.bits.req_info.vpn, 0))
52182e4705bSpeixiaokun  val hptw_resp = entries(hptw_resp_ptr_reg).hptw_resp
522cda84113Speixiaokun  val hpaddr = Cat(hptw_resp.genPPNS2(get_pn(gpaddr)), get_off(gpaddr))
523*e4c1a68eSpeixiaokun  val addr = RegEnable(MakeAddr(io.in.bits.ppn, getVpnn(io.in.bits.req_info.vpn, 0)), io.in.fire)
5247274ec5cSpeixiaokun  io.pmp.req.valid := need_addr_check || hptw_need_addr_check
52582e4705bSpeixiaokun  io.pmp.req.bits.addr := Mux(hptw_need_addr_check, hpaddr, addr)
5267797f035SbugGenerator  io.pmp.req.bits.cmd := TlbCmd.read
5277797f035SbugGenerator  io.pmp.req.bits.size := 3.U // TODO: fix it
5287797f035SbugGenerator  val pmp_resp_valid = io.pmp.req.valid // same cycle
5297797f035SbugGenerator  when (pmp_resp_valid) {
5307797f035SbugGenerator    // NOTE: when pmp resp but state is not addr check, then the entry is dup with other entry, the state was changed before
5317797f035SbugGenerator    //       when dup with the req-ing entry, set to mem_waiting (above codes), and the ld must be false, so dontcare
5327274ec5cSpeixiaokun    val ptr = Mux(hptw_need_addr_check, hptw_resp_ptr_reg, enq_ptr_reg);
5337797f035SbugGenerator    val accessFault = io.pmp.resp.ld || io.pmp.resp.mmio
5347274ec5cSpeixiaokun    entries(ptr).af := accessFault
5357274ec5cSpeixiaokun    state(ptr) := Mux(accessFault, state_mem_out, state_mem_req)
5367797f035SbugGenerator  }
5377797f035SbugGenerator
538935edac4STang Haojin  when (mem_arb.io.out.fire) {
53992e3bfefSLemover    for (i <- state.indices) {
540ec78ed87Speixiaokun      when (state(i) =/= state_idle && state(i) =/= state_mem_out && state(i) =/= state_last_hptw_req && state(i) =/= state_last_hptw_resp
541ec78ed87Speixiaokun      && entries(i).req_info.s2xlate === mem_arb.io.out.bits.req_info.s2xlate
542ec78ed87Speixiaokun      && dup(entries(i).req_info.vpn, mem_arb.io.out.bits.req_info.vpn)) {
54392e3bfefSLemover        // NOTE: "dup enq set state to mem_wait" -> "sending req set other dup entries to mem_wait"
54492e3bfefSLemover        state(i) := state_mem_waiting
5452a1f48e7Speixiaokun        entries(i).hptw_resp := entries(mem_arb.io.chosen).hptw_resp
54692e3bfefSLemover        entries(i).wait_id := mem_arb.io.chosen
54792e3bfefSLemover      }
54892e3bfefSLemover    }
54992e3bfefSLemover  }
550935edac4STang Haojin  when (io.mem.resp.fire) {
55192e3bfefSLemover    state.indices.map{i =>
55292e3bfefSLemover      when (state(i) === state_mem_waiting && io.mem.resp.bits.id === entries(i).wait_id) {
5534358f287Speixiaokun        state(i) := Mux(entries(i).req_info.s2xlate =/= noS2xlate, state_last_hptw_req, state_mem_out)
55492e3bfefSLemover        mem_resp_hit(i) := true.B
5554358f287Speixiaokun        val req_paddr = MakeAddr(entries(i).ppn, getVpnn(entries(i).req_info.vpn, 0))
5564358f287Speixiaokun        val req_hpaddr = MakeAddr(entries(i).hptw_resp.genPPNS2(get_pn(req_paddr)), getVpnn(entries(i).req_info.vpn, 0))
5574358f287Speixiaokun        val index =  Mux(entries(i).req_info.s2xlate === allStage, req_hpaddr, req_paddr)(log2Up(l2tlbParams.blockBytes)-1, log2Up(XLEN/8))
5584358f287Speixiaokun        entries(i).ppn := ptes(index).ppn // for last stage 2 translation
559ad0d9d89Speixiaokun      }
560ad0d9d89Speixiaokun    }
561ad0d9d89Speixiaokun  }
562ad0d9d89Speixiaokun
5633222d00fSpeixiaokun  when (hyper_arb1.io.out.fire) {
564d0de7e4aSpeixiaokun    for (i <- state.indices) {
5654358f287Speixiaokun      when (state(i) === state_hptw_req && entries(i).ppn === hyper_arb1.io.out.bits.ppn && entries(i).req_info.s2xlate =/= noS2xlate && hyper_arb1.io.chosen === i.U) {
566d0de7e4aSpeixiaokun        state(i) := state_hptw_resp
567d0de7e4aSpeixiaokun        entries(i).wait_id := hyper_arb1.io.chosen
568d0de7e4aSpeixiaokun      }
569d0de7e4aSpeixiaokun    }
570d0de7e4aSpeixiaokun  }
571d0de7e4aSpeixiaokun
5723222d00fSpeixiaokun  when (hyper_arb2.io.out.fire) {
573d0de7e4aSpeixiaokun    for (i <- state.indices) {
5744358f287Speixiaokun      when (state(i) === state_last_hptw_req && entries(i).ppn === hyper_arb2.io.out.bits.ppn && entries(i).req_info.s2xlate =/= noS2xlate && hyper_arb2.io.chosen === i.U) {
575d0de7e4aSpeixiaokun        state(i) := state_last_hptw_resp
576d0de7e4aSpeixiaokun        entries(i).wait_id := hyper_arb2.io.chosen
577d0de7e4aSpeixiaokun      }
578d0de7e4aSpeixiaokun    }
579d0de7e4aSpeixiaokun  }
580d0de7e4aSpeixiaokun
5813222d00fSpeixiaokun  when (io.hptw.resp.fire) {
582d0de7e4aSpeixiaokun    for (i <- state.indices) {
5832a1f48e7Speixiaokun      when (state(i) === state_hptw_resp && io.hptw.resp.bits.id === entries(i).wait_id && io.hptw.resp.bits.h_resp.entry.tag === entries(i).ppn) {
5842a1f48e7Speixiaokun        // change the entry that is waiting hptw resp
585ec78ed87Speixiaokun        val need_to_waiting_vec = state.indices.map(i => state(i) === state_mem_waiting && dup(entries(i).req_info.vpn, entries(io.hptw.resp.bits.id).req_info.vpn))
5867f96e195Speixiaokun        val waiting_index = ParallelMux(need_to_waiting_vec zip entries.map(_.wait_id))
5877f96e195Speixiaokun        state(i) := Mux(Cat(need_to_waiting_vec).orR, state_mem_waiting, state_addr_check)
588dc05c713Speixiaokun        entries(i).hptw_resp := io.hptw.resp.bits.h_resp
5897f96e195Speixiaokun        entries(i).wait_id := Mux(Cat(need_to_waiting_vec).orR, waiting_index, entries(i).wait_id)
5902a1f48e7Speixiaokun        //To do: change the entry that is having the same hptw req
591d0de7e4aSpeixiaokun      }
5922a1f48e7Speixiaokun      when (state(i) === state_last_hptw_resp && io.hptw.resp.bits.id === entries(i).wait_id && io.hptw.resp.bits.h_resp.entry.tag === entries(i).ppn) {
593d0de7e4aSpeixiaokun        state(i) := state_mem_out
594dc05c713Speixiaokun        entries(i).hptw_resp := io.hptw.resp.bits.h_resp
5952a1f48e7Speixiaokun        //To do: change the entry that is having the same hptw req
596d0de7e4aSpeixiaokun      }
597d0de7e4aSpeixiaokun    }
598d0de7e4aSpeixiaokun  }
599935edac4STang Haojin  when (io.out.fire) {
60092e3bfefSLemover    assert(state(mem_ptr) === state_mem_out)
60192e3bfefSLemover    state(mem_ptr) := state_idle
60292e3bfefSLemover  }
60392e3bfefSLemover  mem_resp_hit.map(a => when (a) { a := false.B } )
60492e3bfefSLemover
6057797f035SbugGenerator  when (io.cache.fire) {
6067797f035SbugGenerator    state(cache_ptr) := state_idle
60792e3bfefSLemover  }
6087797f035SbugGenerator  XSError(io.out.fire && io.cache.fire && (mem_ptr === cache_ptr), "mem resp and cache fire at the same time at same entry")
60992e3bfefSLemover
61092e3bfefSLemover  when (flush) {
61192e3bfefSLemover    state.map(_ := state_idle)
61292e3bfefSLemover  }
61392e3bfefSLemover
61492e3bfefSLemover  io.in.ready := !full
61592e3bfefSLemover
616935edac4STang Haojin  io.out.valid := ParallelOR(is_having).asBool
61792e3bfefSLemover  io.out.bits.req_info := entries(mem_ptr).req_info
61892e3bfefSLemover  io.out.bits.id := mem_ptr
61992e3bfefSLemover  io.out.bits.af := entries(mem_ptr).af
620dc05c713Speixiaokun  io.out.bits.h_resp := entries(mem_ptr).hptw_resp
621d0de7e4aSpeixiaokun
62283d93d53Speixiaokun  val hptw_req_arb = Module(new Arbiter(new Bundle{
62383d93d53Speixiaokun      val source = UInt(bSourceWidth.W)
62483d93d53Speixiaokun      val id = UInt(log2Up(l2tlbParams.llptwsize).W)
62583d93d53Speixiaokun      val ppn = UInt(vpnLen.W)
62683d93d53Speixiaokun    } , 2))
62783d93d53Speixiaokun  // first stage 2 translation
62883d93d53Speixiaokun  hptw_req_arb.io.in(0).valid := hyper_arb1.io.out.valid
62983d93d53Speixiaokun  hptw_req_arb.io.in(0).bits.source := hyper_arb1.io.out.bits.req_info.source
63083d93d53Speixiaokun  hptw_req_arb.io.in(0).bits.ppn := hyper_arb1.io.out.bits.ppn
63183d93d53Speixiaokun  hptw_req_arb.io.in(0).bits.id := hyper_arb1.io.chosen
6322a1f48e7Speixiaokun  hyper_arb1.io.out.ready := hptw_req_arb.io.in(0).ready
63383d93d53Speixiaokun  // last stage 2 translation
63483d93d53Speixiaokun  hptw_req_arb.io.in(1).valid := hyper_arb2.io.out.valid
63583d93d53Speixiaokun  hptw_req_arb.io.in(1).bits.source := hyper_arb2.io.out.bits.req_info.source
63683d93d53Speixiaokun  hptw_req_arb.io.in(1).bits.ppn := hyper_arb2.io.out.bits.ppn
63783d93d53Speixiaokun  hptw_req_arb.io.in(1).bits.id := hyper_arb2.io.chosen
6382a1f48e7Speixiaokun  hyper_arb2.io.out.ready := hptw_req_arb.io.in(1).ready
63983d93d53Speixiaokun  hptw_req_arb.io.out.ready := io.hptw.req.ready
6402a1f48e7Speixiaokun  io.hptw.req.valid := hptw_req_arb.io.out.fire && !flush
64183d93d53Speixiaokun  io.hptw.req.bits.gvpn := hptw_req_arb.io.out.bits.ppn
64283d93d53Speixiaokun  io.hptw.req.bits.id := hptw_req_arb.io.out.bits.id
64383d93d53Speixiaokun  io.hptw.req.bits.source := hptw_req_arb.io.out.bits.source
64492e3bfefSLemover
64592e3bfefSLemover  io.mem.req.valid := mem_arb.io.out.valid && !flush
646dc05c713Speixiaokun  val mem_paddr = MakeAddr(mem_arb.io.out.bits.ppn, getVpnn(mem_arb.io.out.bits.req_info.vpn, 0))
647cda84113Speixiaokun  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))
6484358f287Speixiaokun  io.mem.req.bits.addr := Mux(mem_arb.io.out.bits.req_info.s2xlate =/= noS2xlate, mem_hpaddr, mem_paddr)
64992e3bfefSLemover  io.mem.req.bits.id := mem_arb.io.chosen
65083d93d53Speixiaokun  io.mem.req.bits.hptw_bypassed := false.B
65192e3bfefSLemover  mem_arb.io.out.ready := io.mem.req.ready
652933ec998Speixiaokun  val mem_refill_id = RegNext(io.mem.resp.bits.id(log2Up(l2tlbParams.llptwsize)-1, 0))
653933ec998Speixiaokun  io.mem.refill := entries(mem_refill_id).req_info
654933ec998Speixiaokun  io.mem.refill.s2xlate := Mux(entries(mem_refill_id).req_info.s2xlate === noS2xlate, noS2xlate, onlyStage1) // llptw refill the pte of stage 1
65592e3bfefSLemover  io.mem.buffer_it := mem_resp_hit
65692e3bfefSLemover  io.mem.enq_ptr := enq_ptr
65792e3bfefSLemover
6587797f035SbugGenerator  io.cache.valid := Cat(is_cache).orR
6597797f035SbugGenerator  io.cache.bits := ParallelMux(is_cache, entries.map(_.req_info))
6607797f035SbugGenerator
661935edac4STang Haojin  XSPerfAccumulate("llptw_in_count", io.in.fire)
66292e3bfefSLemover  XSPerfAccumulate("llptw_in_block", io.in.valid && !io.in.ready)
66392e3bfefSLemover  for (i <- 0 until 7) {
664935edac4STang Haojin    XSPerfAccumulate(s"enq_state${i}", io.in.fire && enq_state === i.U)
66592e3bfefSLemover  }
66692e3bfefSLemover  for (i <- 0 until (l2tlbParams.llptwsize + 1)) {
66792e3bfefSLemover    XSPerfAccumulate(s"util${i}", PopCount(is_emptys.map(!_)) === i.U)
66892e3bfefSLemover    XSPerfAccumulate(s"mem_util${i}", PopCount(is_mems) === i.U)
66992e3bfefSLemover    XSPerfAccumulate(s"waiting_util${i}", PopCount(is_waiting) === i.U)
67092e3bfefSLemover  }
671935edac4STang Haojin  XSPerfAccumulate("mem_count", io.mem.req.fire)
67292e3bfefSLemover  XSPerfAccumulate("mem_cycle", PopCount(is_waiting) =/= 0.U)
67392e3bfefSLemover  XSPerfAccumulate("blocked_in", io.in.valid && !io.in.ready)
67492e3bfefSLemover
67592e3bfefSLemover  for (i <- 0 until l2tlbParams.llptwsize) {
67692e3bfefSLemover    TimeOutAssert(state(i) =/= state_idle, timeOutThreshold, s"missqueue time out no out ${i}")
67792e3bfefSLemover  }
67892e3bfefSLemover
67992e3bfefSLemover  val perfEvents = Seq(
680935edac4STang Haojin    ("tlbllptw_incount           ", io.in.fire               ),
68192e3bfefSLemover    ("tlbllptw_inblock           ", io.in.valid && !io.in.ready),
682935edac4STang Haojin    ("tlbllptw_memcount          ", io.mem.req.fire          ),
68392e3bfefSLemover    ("tlbllptw_memcycle          ", PopCount(is_waiting)       ),
68492e3bfefSLemover  )
68592e3bfefSLemover  generatePerfEvent()
68692e3bfefSLemover}
687d0de7e4aSpeixiaokun
688d0de7e4aSpeixiaokun/*========================= HPTW ==============================*/
689d0de7e4aSpeixiaokun
690d0de7e4aSpeixiaokun/** HPTW : Hypervisor Page Table Walker
691d0de7e4aSpeixiaokun  * the page walker take the virtual machine's page walk.
692d0de7e4aSpeixiaokun  * guest physical address translation, guest physical address -> host physical address
693d0de7e4aSpeixiaokun  **/
694d0de7e4aSpeixiaokunclass HPTWIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
695d0de7e4aSpeixiaokun  val req = Flipped(DecoupledIO(new Bundle {
696eb4bf3f2Speixiaokun    val source = UInt(bSourceWidth.W)
697d0de7e4aSpeixiaokun    val id = UInt(log2Up(l2tlbParams.llptwsize).W)
69882978df9Speixiaokun    val gvpn = UInt(vpnLen.W)
6996315ba2aSpeixiaokun    val ppn = UInt(ppnLen.W)
700d0de7e4aSpeixiaokun    val l1Hit = Bool()
701d0de7e4aSpeixiaokun    val l2Hit = Bool()
70283d93d53Speixiaokun    val bypassed = Bool() // if bypass, don't refill
703d0de7e4aSpeixiaokun  }))
704c2b430edSpeixiaokun  val resp = DecoupledIO(new Bundle {
705eb4bf3f2Speixiaokun    val source = UInt(bSourceWidth.W)
706d0de7e4aSpeixiaokun    val resp = Output(new HptwResp())
707d0de7e4aSpeixiaokun    val id = Output(UInt(bMemID.W))
708d0de7e4aSpeixiaokun  })
709d0de7e4aSpeixiaokun
710d0de7e4aSpeixiaokun  val mem = new Bundle {
711d0de7e4aSpeixiaokun    val req = DecoupledIO(new L2TlbMemReqBundle())
712d0de7e4aSpeixiaokun    val resp = Flipped(ValidIO(UInt(XLEN.W)))
713d0de7e4aSpeixiaokun    val mask = Input(Bool())
714d0de7e4aSpeixiaokun  }
715d0de7e4aSpeixiaokun  val refill = Output(new Bundle {
716d0de7e4aSpeixiaokun    val req_info = new L2TlbInnerBundle()
717d0de7e4aSpeixiaokun    val level = UInt(log2Up(Level).W)
718d0de7e4aSpeixiaokun  })
719d0de7e4aSpeixiaokun  val pmp = new Bundle {
720d0de7e4aSpeixiaokun    val req = ValidIO(new PMPReqBundle())
721d0de7e4aSpeixiaokun    val resp = Flipped(new PMPRespBundle())
722d0de7e4aSpeixiaokun  }
723d0de7e4aSpeixiaokun}
724d0de7e4aSpeixiaokun
725d0de7e4aSpeixiaokunclass HPTW()(implicit p: Parameters) extends XSModule with HasPtwConst {
726d0de7e4aSpeixiaokun  val io = IO(new HPTWIO)
727d0de7e4aSpeixiaokun  val hgatp = io.csr.hgatp
728d0de7e4aSpeixiaokun  val sfence = io.sfence
729d0de7e4aSpeixiaokun  val flush = sfence.valid || hgatp.changed
730d0de7e4aSpeixiaokun
731d0de7e4aSpeixiaokun  val level = RegInit(0.U(log2Up(Level).W))
732d0de7e4aSpeixiaokun  val gpaddr = Reg(UInt(GPAddrBits.W))
7334c4af37cSpeixiaokun  val req_ppn = Reg(UInt(ppnLen.W))
734d0de7e4aSpeixiaokun  val vpn = gpaddr(GPAddrBits-1, offLen)
735d0de7e4aSpeixiaokun  val levelNext = level + 1.U
736d0de7e4aSpeixiaokun  val l1Hit = Reg(Bool())
737d0de7e4aSpeixiaokun  val l2Hit = Reg(Bool())
73883d93d53Speixiaokun  val bypassed = Reg(Bool())
739b24e0a78Speixiaokun  val pg_base = MakeGPAddr(hgatp.ppn, getGVpnn(vpn, 2.U)) // for l0
740d0de7e4aSpeixiaokun//  val pte = io.mem.resp.bits.MergeRespToPte()
741d0de7e4aSpeixiaokun  val pte = io.mem.resp.bits.asTypeOf(new PteBundle().cloneType)
7424c4af37cSpeixiaokun  val ppn_l1 = Mux(l1Hit, req_ppn, pte.ppn)
7434c4af37cSpeixiaokun  val ppn_l2 = Mux(l2Hit, req_ppn, pte.ppn)
7446315ba2aSpeixiaokun  val ppn = Mux(level === 1.U, ppn_l1, ppn_l2) //for l1 and l2
7456315ba2aSpeixiaokun  val p_pte = MakeAddr(ppn, getVpnn(vpn, 2.U - level))
746d0de7e4aSpeixiaokun  val mem_addr = Mux(level === 0.U, pg_base, p_pte)
747d0de7e4aSpeixiaokun
748d0de7e4aSpeixiaokun  //s/w register
749d0de7e4aSpeixiaokun  val s_pmp_check = RegInit(true.B)
750d0de7e4aSpeixiaokun  val s_mem_req = RegInit(true.B)
751d0de7e4aSpeixiaokun  val w_mem_resp = RegInit(true.B)
752d0de7e4aSpeixiaokun  val idle = RegInit(true.B)
75303c1129fSpeixiaokun  val mem_addr_update = RegInit(false.B)
754d0de7e4aSpeixiaokun  val finish = WireInit(false.B)
755d0de7e4aSpeixiaokun
756d0de7e4aSpeixiaokun  val sent_to_pmp = !idle && (!s_pmp_check || mem_addr_update) && !finish
757d0de7e4aSpeixiaokun  val pageFault = pte.isPf(level)
758d0de7e4aSpeixiaokun  val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, sent_to_pmp)
759d0de7e4aSpeixiaokun
760d0de7e4aSpeixiaokun  val ppn_af = pte.isAf()
761d0de7e4aSpeixiaokun  val find_pte = pte.isLeaf() || ppn_af || pageFault
762d0de7e4aSpeixiaokun
763d0de7e4aSpeixiaokun  val resp_valid = !idle && mem_addr_update && ((w_mem_resp && find_pte) || (s_pmp_check && accessFault))
764d0de7e4aSpeixiaokun  val id = Reg(UInt(log2Up(l2tlbParams.llptwsize).W))
7653222d00fSpeixiaokun  val source = RegEnable(io.req.bits.source, io.req.fire)
766eb4bf3f2Speixiaokun
767d0de7e4aSpeixiaokun  io.req.ready := idle
768eb4bf3f2Speixiaokun  val resp = Wire(new HptwResp())
769d0de7e4aSpeixiaokun  resp.apply(pageFault && !accessFault && !ppn_af, accessFault || ppn_af, level, pte, vpn, hgatp.asid)
770d0de7e4aSpeixiaokun  io.resp.valid := resp_valid
771d0de7e4aSpeixiaokun  io.resp.bits.id := id
772d0de7e4aSpeixiaokun  io.resp.bits.resp := resp
773eb4bf3f2Speixiaokun  io.resp.bits.source := source
774d0de7e4aSpeixiaokun
775d0de7e4aSpeixiaokun  io.pmp.req.valid := DontCare
776d0de7e4aSpeixiaokun  io.pmp.req.bits.addr := mem_addr
777d0de7e4aSpeixiaokun  io.pmp.req.bits.size := 3.U
778d0de7e4aSpeixiaokun  io.pmp.req.bits.cmd := TlbCmd.read
779d0de7e4aSpeixiaokun
780d0de7e4aSpeixiaokun  io.mem.req.valid := !s_mem_req && !io.mem.mask && !accessFault && s_pmp_check
781d0de7e4aSpeixiaokun  io.mem.req.bits.addr := mem_addr
782d0de7e4aSpeixiaokun  io.mem.req.bits.id := HptwReqId.U(bMemID.W)
78383d93d53Speixiaokun  io.mem.req.bits.hptw_bypassed := bypassed
784d0de7e4aSpeixiaokun
78582978df9Speixiaokun  io.refill.req_info.vpn := vpn
786d0de7e4aSpeixiaokun  io.refill.level := level
787eb4bf3f2Speixiaokun  io.refill.req_info.source := source
788eb4bf3f2Speixiaokun  io.refill.req_info.s2xlate := onlyStage2
789d0de7e4aSpeixiaokun  when (idle){
7903222d00fSpeixiaokun    when(io.req.fire){
79183d93d53Speixiaokun      bypassed := io.req.bits.bypassed
792d0de7e4aSpeixiaokun      level := Mux(io.req.bits.l2Hit, 2.U, Mux(io.req.bits.l1Hit, 1.U, 0.U))
793d0de7e4aSpeixiaokun      idle := false.B
794d0de7e4aSpeixiaokun      gpaddr := Cat(io.req.bits.gvpn, 0.U(offLen.W))
795d0de7e4aSpeixiaokun      accessFault := false.B
796d0de7e4aSpeixiaokun      s_pmp_check := false.B
797d0de7e4aSpeixiaokun      id := io.req.bits.id
7984c4af37cSpeixiaokun      req_ppn := io.req.bits.ppn
799d0de7e4aSpeixiaokun      l1Hit := io.req.bits.l1Hit
800d0de7e4aSpeixiaokun      l2Hit := io.req.bits.l2Hit
801d0de7e4aSpeixiaokun    }
802d0de7e4aSpeixiaokun  }
803d0de7e4aSpeixiaokun
804d0de7e4aSpeixiaokun  when(sent_to_pmp && !mem_addr_update){
805d0de7e4aSpeixiaokun    s_mem_req := false.B
806d0de7e4aSpeixiaokun    s_pmp_check := true.B
807d0de7e4aSpeixiaokun  }
808d0de7e4aSpeixiaokun
809d0de7e4aSpeixiaokun  when(accessFault && !idle){
810d0de7e4aSpeixiaokun    s_pmp_check := true.B
811d0de7e4aSpeixiaokun    s_mem_req := true.B
812d0de7e4aSpeixiaokun    w_mem_resp := true.B
813d0de7e4aSpeixiaokun    mem_addr_update := true.B
814d0de7e4aSpeixiaokun  }
815d0de7e4aSpeixiaokun
8163222d00fSpeixiaokun  when(io.mem.req.fire){
817d0de7e4aSpeixiaokun    s_mem_req := true.B
818d0de7e4aSpeixiaokun    w_mem_resp := false.B
819d0de7e4aSpeixiaokun  }
820d0de7e4aSpeixiaokun
8213222d00fSpeixiaokun  when(io.mem.resp.fire && !w_mem_resp){
822d0de7e4aSpeixiaokun    w_mem_resp := true.B
823d0de7e4aSpeixiaokun    mem_addr_update := true.B
824d0de7e4aSpeixiaokun  }
825d0de7e4aSpeixiaokun
826d0de7e4aSpeixiaokun  when(mem_addr_update){
827d0de7e4aSpeixiaokun    when(!(find_pte || accessFault)){
828d0de7e4aSpeixiaokun      level := levelNext
829d0de7e4aSpeixiaokun      s_mem_req := false.B
830d0de7e4aSpeixiaokun      mem_addr_update := false.B
831d0de7e4aSpeixiaokun    }.elsewhen(resp_valid){
8323222d00fSpeixiaokun      when(io.resp.fire){
833d0de7e4aSpeixiaokun        idle := true.B
834d0de7e4aSpeixiaokun        mem_addr_update := false.B
835d0de7e4aSpeixiaokun        accessFault := false.B
836d0de7e4aSpeixiaokun      }
837d0de7e4aSpeixiaokun      finish := true.B
838d0de7e4aSpeixiaokun    }
839d0de7e4aSpeixiaokun  }
840d0de7e4aSpeixiaokun}