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() 44*3ea4388cSHaoyuan Feng val l3Hit = if (EnableSv48) Some(new Bool()) else None 45*3ea4388cSHaoyuan Feng val l2Hit = Bool() 464c0e0181SXiaokun-Pei val ppn = UInt(gvpnLen.W) 4730104977Speixiaokun val stage1Hit = Bool() 4830104977Speixiaokun val stage1 = new PtwMergeResp 496d5ddbceSLemover })) 506d5ddbceSLemover val resp = DecoupledIO(new Bundle { 51bc063562SLemover val source = UInt(bSourceWidth.W) 52eb4bf3f2Speixiaokun val s2xlate = UInt(2.W) 5363632028SHaoyuan Feng val resp = new PtwMergeResp 54d0de7e4aSpeixiaokun val h_resp = new HptwResp 556d5ddbceSLemover }) 566d5ddbceSLemover 5792e3bfefSLemover val llptw = DecoupledIO(new LLPTWInBundle()) 589c503409SLemover // NOTE: llptw change from "connect to llptw" to "connect to page cache" 599c503409SLemover // to avoid corner case that caused duplicate entries 60cc5a5f22SLemover 61d0de7e4aSpeixiaokun val hptw = new Bundle { 62d0de7e4aSpeixiaokun val req = DecoupledIO(new Bundle { 63eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 64d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 6582978df9Speixiaokun val gvpn = UInt(vpnLen.W) 66d0de7e4aSpeixiaokun }) 67d0de7e4aSpeixiaokun val resp = Flipped(Valid(new Bundle { 68d0de7e4aSpeixiaokun val h_resp = Output(new HptwResp) 69d0de7e4aSpeixiaokun })) 70d0de7e4aSpeixiaokun } 716d5ddbceSLemover val mem = new Bundle { 72b848eea5SLemover val req = DecoupledIO(new L2TlbMemReqBundle()) 735854c1edSLemover val resp = Flipped(ValidIO(UInt(XLEN.W))) 74cc5a5f22SLemover val mask = Input(Bool()) 756d5ddbceSLemover } 76b6982e83SLemover val pmp = new Bundle { 77b6982e83SLemover val req = ValidIO(new PMPReqBundle()) 78b6982e83SLemover val resp = Flipped(new PMPRespBundle()) 79b6982e83SLemover } 806d5ddbceSLemover 816d5ddbceSLemover val refill = Output(new Bundle { 8245f497a4Shappy-lx val req_info = new L2TlbInnerBundle() 83*3ea4388cSHaoyuan Feng val level = UInt(log2Up(Level + 1).W) 846d5ddbceSLemover }) 856d5ddbceSLemover} 866d5ddbceSLemover 8792e3bfefSLemoverclass PTW()(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents { 8892e3bfefSLemover val io = IO(new PTWIO) 896d5ddbceSLemover val sfence = io.sfence 906d5ddbceSLemover val mem = io.mem 91d0de7e4aSpeixiaokun val req_s2xlate = Reg(UInt(2.W)) 9203c1129fSpeixiaokun val enableS2xlate = req_s2xlate =/= noS2xlate 9303c1129fSpeixiaokun val onlyS1xlate = req_s2xlate === onlyStage1 9403c1129fSpeixiaokun val onlyS2xlate = req_s2xlate === onlyStage2 95d0de7e4aSpeixiaokun 96*3ea4388cSHaoyuan Feng val satp = Wire(new TlbSatpBundle()) 97*3ea4388cSHaoyuan Feng when (io.req.fire) { 98*3ea4388cSHaoyuan Feng satp := Mux(io.req.bits.req_info.s2xlate =/= noS2xlate, io.csr.vsatp, io.csr.satp) 99*3ea4388cSHaoyuan Feng } .otherwise { 100*3ea4388cSHaoyuan Feng satp := Mux(enableS2xlate, io.csr.vsatp, io.csr.satp) 101*3ea4388cSHaoyuan Feng } 102*3ea4388cSHaoyuan Feng 103*3ea4388cSHaoyuan Feng val mode = satp.mode 104d0de7e4aSpeixiaokun val hgatp = io.csr.hgatp 1055c5f442fSXiaokun-Pei val flush = io.sfence.valid || io.csr.satp.changed || io.csr.vsatp.changed || io.csr.hgatp.changed 106d0de7e4aSpeixiaokun val s2xlate = enableS2xlate && !onlyS1xlate 107*3ea4388cSHaoyuan Feng val level = RegInit(3.U(log2Up(Level + 1).W)) 108*3ea4388cSHaoyuan Feng val af_level = RegInit(3.U(log2Up(Level + 1).W)) // access fault return this level 1094c0e0181SXiaokun-Pei val ppn = Reg(UInt(gvpnLen.W)) 1104c0e0181SXiaokun-Pei val vpn = Reg(UInt(vpnLen.W)) // vpn or gvpn(onlyS2xlate) 111*3ea4388cSHaoyuan Feng val levelNext = level - 1.U 112*3ea4388cSHaoyuan Feng val l3Hit = Reg(Bool()) 113*3ea4388cSHaoyuan Feng val l2Hit = Reg(Bool()) 114505c893aSXiaokun-Pei val pte_valid = RegInit(false.B) // avoid the x states 115505c893aSXiaokun-Pei val fake_pte = 0.U.asTypeOf(new PteBundle()) 116505c893aSXiaokun-Pei fake_pte.perm.v := true.B 117505c893aSXiaokun-Pei fake_pte.perm.r := true.B 118505c893aSXiaokun-Pei fake_pte.perm.w := true.B 119505c893aSXiaokun-Pei fake_pte.perm.x := true.B 120505c893aSXiaokun-Pei val pte = Mux(pte_valid, mem.resp.bits.asTypeOf(new PteBundle()), fake_pte) 121*3ea4388cSHaoyuan Feng 12244b79566SXiaokun-Pei // s/w register 12344b79566SXiaokun-Pei val s_pmp_check = RegInit(true.B) 12444b79566SXiaokun-Pei val s_mem_req = RegInit(true.B) 12544b79566SXiaokun-Pei val s_llptw_req = RegInit(true.B) 12644b79566SXiaokun-Pei val w_mem_resp = RegInit(true.B) 127d0de7e4aSpeixiaokun val s_hptw_req = RegInit(true.B) 128d0de7e4aSpeixiaokun val w_hptw_resp = RegInit(true.B) 129d0de7e4aSpeixiaokun val s_last_hptw_req = RegInit(true.B) 130d0de7e4aSpeixiaokun val w_last_hptw_resp = RegInit(true.B) 13144b79566SXiaokun-Pei // for updating "level" 13244b79566SXiaokun-Pei val mem_addr_update = RegInit(false.B) 13344b79566SXiaokun-Pei 13444b79566SXiaokun-Pei val idle = RegInit(true.B) 1352a906a65SHaoyuan Feng val finish = WireInit(false.B) 1362a906a65SHaoyuan Feng val sent_to_pmp = idle === false.B && (s_pmp_check === false.B || mem_addr_update) && !finish 13744b79566SXiaokun-Pei 138d0de7e4aSpeixiaokun val pageFault = pte.isPf(level) 13944b79566SXiaokun-Pei val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, sent_to_pmp) 1406d5ddbceSLemover 141d0de7e4aSpeixiaokun val hptw_pageFault = RegInit(false.B) 142d0de7e4aSpeixiaokun val hptw_accessFault = RegInit(false.B) 143d0de7e4aSpeixiaokun val last_s2xlate = RegInit(false.B) 1443222d00fSpeixiaokun val stage1Hit = RegEnable(io.req.bits.stage1Hit, io.req.fire) 1453222d00fSpeixiaokun val stage1 = RegEnable(io.req.bits.stage1, io.req.fire) 14609280d15Speixiaokun val hptw_resp_stage2 = Reg(Bool()) 147d0de7e4aSpeixiaokun 1484c0e0181SXiaokun-Pei val ppn_af = Mux(s2xlate, pte.isStage1Af(), pte.isAf()) // In two-stage address translation, stage 1 ppn is a vpn for host, so don't need to check ppn_high 1497263b595SXiaokun-Pei val guest_fault = hptw_pageFault || hptw_accessFault 1507263b595SXiaokun-Pei val find_pte = pte.isLeaf() || ppn_af || pageFault 15144b79566SXiaokun-Pei val to_find_pte = level === 1.U && find_pte === false.B 152935edac4STang Haojin val source = RegEnable(io.req.bits.req_info.source, io.req.fire) 1536d5ddbceSLemover 154*3ea4388cSHaoyuan Feng val l3addr = Wire(UInt(PAddrBits.W)) 155*3ea4388cSHaoyuan Feng val l2addr = Wire(UInt(PAddrBits.W)) 156*3ea4388cSHaoyuan Feng val l1addr = Wire(UInt(PAddrBits.W)) 157*3ea4388cSHaoyuan Feng val mem_addr = Wire(UInt(PAddrBits.W)) 158*3ea4388cSHaoyuan Feng 159*3ea4388cSHaoyuan Feng l3addr := MakeAddr(satp.ppn, getVpnn(vpn, 3)) 160*3ea4388cSHaoyuan Feng if (EnableSv48) { 161*3ea4388cSHaoyuan Feng when (mode === Sv48) { 162*3ea4388cSHaoyuan Feng l2addr := MakeAddr(Mux(l3Hit, ppn, pte.getPPN()), getVpnn(vpn, 2)) 163*3ea4388cSHaoyuan Feng } .otherwise { 164*3ea4388cSHaoyuan Feng l2addr := MakeAddr(satp.ppn, getVpnn(vpn, 2)) 165*3ea4388cSHaoyuan Feng } 166*3ea4388cSHaoyuan Feng } else { 167*3ea4388cSHaoyuan Feng l2addr := MakeAddr(satp.ppn, getVpnn(vpn, 2)) 168*3ea4388cSHaoyuan Feng } 169*3ea4388cSHaoyuan Feng l1addr := MakeAddr(Mux(l2Hit, ppn, pte.getPPN()), getVpnn(vpn, 1)) 170*3ea4388cSHaoyuan Feng mem_addr := Mux(af_level === 3.U, l3addr, Mux(af_level === 2.U, l2addr, l1addr)) 17144b79566SXiaokun-Pei 1723222d00fSpeixiaokun val hptw_resp = RegEnable(io.hptw.resp.bits.h_resp, io.hptw.resp.fire) 173c0991f6aSpeixiaokun val gpaddr = MuxCase(mem_addr, Seq( 174c0991f6aSpeixiaokun stage1Hit -> Cat(stage1.genPPN(), 0.U(offLen.W)), 175c0991f6aSpeixiaokun onlyS2xlate -> Cat(vpn, 0.U(offLen.W)), 176*3ea4388cSHaoyuan Feng !s_last_hptw_req -> Cat(MuxLookup(level, pte.ppn)(Seq( 177*3ea4388cSHaoyuan Feng 3.U -> Cat(pte.getPPN()(gvpnLen - 1, vpnnLen * 3), vpn(vpnnLen * 3 - 1, 0)), 178*3ea4388cSHaoyuan Feng 2.U -> Cat(pte.getPPN()(gvpnLen - 1, vpnnLen * 2), vpn(vpnnLen * 2 - 1, 0)), 1794c0e0181SXiaokun-Pei 1.U -> Cat(pte.getPPN()(gvpnLen - 1, vpnnLen), vpn(vpnnLen - 1, 0) 180dcb10e8fSBL-GS ))), 181dcb10e8fSBL-GS 0.U(offLen.W)) 182c0991f6aSpeixiaokun )) 183cda84113Speixiaokun val hpaddr = Cat(hptw_resp.genPPNS2(get_pn(gpaddr)), get_off(gpaddr)) 184d0de7e4aSpeixiaokun 18544b79566SXiaokun-Pei io.req.ready := idle 18630104977Speixiaokun val ptw_resp = Wire(new PtwMergeResp) 187505c893aSXiaokun-Pei 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) 18844b79566SXiaokun-Pei 1890dfe2fbdSpeixiaokun val normal_resp = idle === false.B && mem_addr_update && !last_s2xlate && (guest_fault || (w_mem_resp && find_pte) || (s_pmp_check && accessFault) || onlyS2xlate) 19009280d15Speixiaokun val stageHit_resp = idle === false.B && hptw_resp_stage2 19109280d15Speixiaokun io.resp.valid := Mux(stage1Hit, stageHit_resp, normal_resp) 19244b79566SXiaokun-Pei io.resp.bits.source := source 19330104977Speixiaokun io.resp.bits.resp := Mux(stage1Hit, stage1, ptw_resp) 19479d4b70cSpeixiaokun io.resp.bits.h_resp := hptw_resp 1956315ba2aSpeixiaokun io.resp.bits.s2xlate := req_s2xlate 19644b79566SXiaokun-Pei 19744b79566SXiaokun-Pei io.llptw.valid := s_llptw_req === false.B && to_find_pte && !accessFault 19844b79566SXiaokun-Pei io.llptw.bits.req_info.source := source 19944b79566SXiaokun-Pei io.llptw.bits.req_info.vpn := vpn 20082978df9Speixiaokun io.llptw.bits.req_info.s2xlate := req_s2xlate 201eb4bf3f2Speixiaokun io.llptw.bits.ppn := DontCare 20244b79566SXiaokun-Pei 203b6982e83SLemover io.pmp.req.valid := DontCare // samecycle, do not use valid 204d0de7e4aSpeixiaokun io.pmp.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr) 205b6982e83SLemover io.pmp.req.bits.size := 3.U // TODO: fix it 206b6982e83SLemover io.pmp.req.bits.cmd := TlbCmd.read 207b6982e83SLemover 20844b79566SXiaokun-Pei mem.req.valid := s_mem_req === false.B && !mem.mask && !accessFault && s_pmp_check 209d0de7e4aSpeixiaokun mem.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr) 210bc063562SLemover mem.req.bits.id := FsmReqID.U(bMemID.W) 21183d93d53Speixiaokun mem.req.bits.hptw_bypassed := false.B 2126d5ddbceSLemover 2134ed5afbdSXiaokun-Pei io.refill.req_info.s2xlate := req_s2xlate 21445f497a4Shappy-lx io.refill.req_info.vpn := vpn 2156d5ddbceSLemover io.refill.level := level 21645f497a4Shappy-lx io.refill.req_info.source := source 2176d5ddbceSLemover 218d0de7e4aSpeixiaokun io.hptw.req.valid := !s_hptw_req || !s_last_hptw_req 219d0de7e4aSpeixiaokun io.hptw.req.bits.id := FsmReqID.U(bMemID.W) 220dcb10e8fSBL-GS io.hptw.req.bits.gvpn := get_pn(gpaddr) 221eb4bf3f2Speixiaokun io.hptw.req.bits.source := source 222d0de7e4aSpeixiaokun 2233222d00fSpeixiaokun when (io.req.fire && io.req.bits.stage1Hit){ 22430104977Speixiaokun idle := false.B 22561c5d636Speixiaokun req_s2xlate := io.req.bits.req_info.s2xlate 22630104977Speixiaokun s_hptw_req := false.B 22709280d15Speixiaokun hptw_resp_stage2 := false.B 2286bb8be21SXiaokun-Pei last_s2xlate := false.B 2290dfe2fbdSpeixiaokun hptw_pageFault := false.B 2300dfe2fbdSpeixiaokun hptw_accessFault := false.B 23130104977Speixiaokun } 232d0de7e4aSpeixiaokun 2333222d00fSpeixiaokun when (io.hptw.resp.fire && w_hptw_resp === false.B && stage1Hit){ 23430104977Speixiaokun w_hptw_resp := true.B 23509280d15Speixiaokun hptw_resp_stage2 := true.B 23630104977Speixiaokun } 23730104977Speixiaokun 2383222d00fSpeixiaokun when (io.resp.fire && stage1Hit){ 23930104977Speixiaokun idle := true.B 24030104977Speixiaokun } 24130104977Speixiaokun 2423222d00fSpeixiaokun when (io.req.fire && !io.req.bits.stage1Hit){ 24344b79566SXiaokun-Pei val req = io.req.bits 244*3ea4388cSHaoyuan Feng if (EnableSv48) { 245*3ea4388cSHaoyuan Feng when (mode === Sv48) { 246*3ea4388cSHaoyuan Feng level := Mux(req.l2Hit, 1.U, Mux(req.l3Hit.get, 2.U, 3.U)) 247*3ea4388cSHaoyuan Feng af_level := Mux(req.l2Hit, 1.U, Mux(req.l3Hit.get, 2.U, 3.U)) 248*3ea4388cSHaoyuan Feng ppn := Mux(req.l2Hit || req.l3Hit.get, io.req.bits.ppn, satp.ppn) 249*3ea4388cSHaoyuan Feng l3Hit := req.l3Hit.get 250*3ea4388cSHaoyuan Feng } .otherwise { 251*3ea4388cSHaoyuan Feng level := Mux(req.l2Hit, 1.U, 2.U) 252*3ea4388cSHaoyuan Feng af_level := Mux(req.l2Hit, 1.U, 2.U) 253*3ea4388cSHaoyuan Feng ppn := Mux(req.l2Hit, io.req.bits.ppn, satp.ppn) 254*3ea4388cSHaoyuan Feng l3Hit := false.B 255*3ea4388cSHaoyuan Feng } 256*3ea4388cSHaoyuan Feng } else { 257*3ea4388cSHaoyuan Feng level := Mux(req.l2Hit, 1.U, 2.U) 258*3ea4388cSHaoyuan Feng af_level := Mux(req.l2Hit, 1.U, 2.U) 259*3ea4388cSHaoyuan Feng ppn := Mux(req.l2Hit, io.req.bits.ppn, satp.ppn) 260*3ea4388cSHaoyuan Feng l3Hit := false.B 261*3ea4388cSHaoyuan Feng } 26244b79566SXiaokun-Pei vpn := io.req.bits.req_info.vpn 263*3ea4388cSHaoyuan Feng l2Hit := req.l2Hit 26444b79566SXiaokun-Pei accessFault := false.B 26544b79566SXiaokun-Pei idle := false.B 266d0de7e4aSpeixiaokun hptw_pageFault := false.B 2677263b595SXiaokun-Pei hptw_accessFault := false.B 268cc72e3f5SXiaokun-Pei pte_valid := false.B 26950c7aa78Speixiaokun req_s2xlate := io.req.bits.req_info.s2xlate 27082978df9Speixiaokun when(io.req.bits.req_info.s2xlate =/= noS2xlate && io.req.bits.req_info.s2xlate =/= onlyStage1){ 271d0de7e4aSpeixiaokun last_s2xlate := true.B 272d0de7e4aSpeixiaokun s_hptw_req := false.B 273d0de7e4aSpeixiaokun }.otherwise { 2746bb8be21SXiaokun-Pei last_s2xlate := false.B 275d0de7e4aSpeixiaokun s_pmp_check := false.B 276d0de7e4aSpeixiaokun } 277d0de7e4aSpeixiaokun } 278d0de7e4aSpeixiaokun 2793222d00fSpeixiaokun when(io.hptw.req.fire && s_hptw_req === false.B){ 280d0de7e4aSpeixiaokun s_hptw_req := true.B 281d0de7e4aSpeixiaokun w_hptw_resp := false.B 282d0de7e4aSpeixiaokun } 283d0de7e4aSpeixiaokun 2843222d00fSpeixiaokun when(io.hptw.resp.fire && w_hptw_resp === false.B && !stage1Hit) { 285d0de7e4aSpeixiaokun hptw_pageFault := io.hptw.resp.bits.h_resp.gpf 286d0de7e4aSpeixiaokun hptw_accessFault := io.hptw.resp.bits.h_resp.gaf 287d0de7e4aSpeixiaokun w_hptw_resp := true.B 2883b805a93SXiaokun-Pei when(onlyS2xlate){ 289d0de7e4aSpeixiaokun mem_addr_update := true.B 290d0de7e4aSpeixiaokun last_s2xlate := false.B 2913b805a93SXiaokun-Pei }.elsewhen(!(io.hptw.resp.bits.h_resp.gpf || io.hptw.resp.bits.h_resp.gaf)) { 292d0de7e4aSpeixiaokun s_pmp_check := false.B 293d0de7e4aSpeixiaokun } 294d0de7e4aSpeixiaokun } 295d0de7e4aSpeixiaokun 2963222d00fSpeixiaokun when(io.hptw.req.fire && s_last_hptw_req === false.B) { 297d0de7e4aSpeixiaokun w_last_hptw_resp := false.B 298d0de7e4aSpeixiaokun s_last_hptw_req := true.B 299d0de7e4aSpeixiaokun } 300d0de7e4aSpeixiaokun 3013222d00fSpeixiaokun when(io.hptw.resp.fire && w_last_hptw_resp === false.B){ 302d0de7e4aSpeixiaokun hptw_pageFault := io.hptw.resp.bits.h_resp.gpf 303d0de7e4aSpeixiaokun hptw_accessFault := io.hptw.resp.bits.h_resp.gaf 304d0de7e4aSpeixiaokun w_last_hptw_resp := true.B 305d0de7e4aSpeixiaokun mem_addr_update := true.B 306d0de7e4aSpeixiaokun last_s2xlate := false.B 30744b79566SXiaokun-Pei } 30844b79566SXiaokun-Pei 30944b79566SXiaokun-Pei when(sent_to_pmp && mem_addr_update === false.B){ 31044b79566SXiaokun-Pei s_mem_req := false.B 31144b79566SXiaokun-Pei s_pmp_check := true.B 31244b79566SXiaokun-Pei } 31344b79566SXiaokun-Pei 31444b79566SXiaokun-Pei when(accessFault && idle === false.B){ 31544b79566SXiaokun-Pei s_pmp_check := true.B 31644b79566SXiaokun-Pei s_mem_req := true.B 31744b79566SXiaokun-Pei w_mem_resp := true.B 31844b79566SXiaokun-Pei s_llptw_req := true.B 319d0de7e4aSpeixiaokun s_hptw_req := true.B 320d0de7e4aSpeixiaokun w_hptw_resp := true.B 321d0de7e4aSpeixiaokun s_last_hptw_req := true.B 322d0de7e4aSpeixiaokun w_last_hptw_resp := true.B 32344b79566SXiaokun-Pei mem_addr_update := true.B 324d0de7e4aSpeixiaokun last_s2xlate := false.B 32544b79566SXiaokun-Pei } 32644b79566SXiaokun-Pei 3277263b595SXiaokun-Pei when(guest_fault && idle === false.B){ 3287263b595SXiaokun-Pei s_pmp_check := true.B 3297263b595SXiaokun-Pei s_mem_req := true.B 3307263b595SXiaokun-Pei w_mem_resp := true.B 3317263b595SXiaokun-Pei s_llptw_req := true.B 3327263b595SXiaokun-Pei s_hptw_req := true.B 3337263b595SXiaokun-Pei w_hptw_resp := true.B 3347263b595SXiaokun-Pei s_last_hptw_req := true.B 3357263b595SXiaokun-Pei w_last_hptw_resp := true.B 3367263b595SXiaokun-Pei mem_addr_update := true.B 3377263b595SXiaokun-Pei last_s2xlate := false.B 3387263b595SXiaokun-Pei } 3397263b595SXiaokun-Pei 340935edac4STang Haojin when (mem.req.fire){ 34144b79566SXiaokun-Pei s_mem_req := true.B 34244b79566SXiaokun-Pei w_mem_resp := false.B 34344b79566SXiaokun-Pei } 34444b79566SXiaokun-Pei 345935edac4STang Haojin when(mem.resp.fire && w_mem_resp === false.B){ 34644b79566SXiaokun-Pei w_mem_resp := true.B 347*3ea4388cSHaoyuan Feng af_level := af_level - 1.U 34844b79566SXiaokun-Pei s_llptw_req := false.B 34944b79566SXiaokun-Pei mem_addr_update := true.B 350cc72e3f5SXiaokun-Pei pte_valid := true.B 35144b79566SXiaokun-Pei } 35244b79566SXiaokun-Pei 35344b79566SXiaokun-Pei when(mem_addr_update){ 354*3ea4388cSHaoyuan Feng when(level >= 2.U && !onlyS2xlate && !(guest_fault || find_pte || accessFault)) { 35544b79566SXiaokun-Pei level := levelNext 356d0de7e4aSpeixiaokun when(s2xlate){ 357d0de7e4aSpeixiaokun s_hptw_req := false.B 358d0de7e4aSpeixiaokun }.otherwise{ 35944b79566SXiaokun-Pei s_mem_req := false.B 360d0de7e4aSpeixiaokun } 36144b79566SXiaokun-Pei s_llptw_req := true.B 36244b79566SXiaokun-Pei mem_addr_update := false.B 3632a906a65SHaoyuan Feng }.elsewhen(io.llptw.valid){ 364935edac4STang Haojin when(io.llptw.fire) { 36544b79566SXiaokun-Pei idle := true.B 36644b79566SXiaokun-Pei s_llptw_req := true.B 36744b79566SXiaokun-Pei mem_addr_update := false.B 368d0de7e4aSpeixiaokun last_s2xlate := false.B 3692a906a65SHaoyuan Feng } 3702a906a65SHaoyuan Feng finish := true.B 371d0de7e4aSpeixiaokun }.elsewhen(s2xlate && last_s2xlate === true.B) { 3727c26eb06SXiaokun-Pei when(accessFault || pageFault || ppn_af){ 3737c26eb06SXiaokun-Pei last_s2xlate := false.B 3747c26eb06SXiaokun-Pei }.otherwise{ 375d0de7e4aSpeixiaokun s_last_hptw_req := false.B 376d0de7e4aSpeixiaokun mem_addr_update := false.B 3777c26eb06SXiaokun-Pei } 3782a906a65SHaoyuan Feng }.elsewhen(io.resp.valid){ 379935edac4STang Haojin when(io.resp.fire) { 38044b79566SXiaokun-Pei idle := true.B 38144b79566SXiaokun-Pei s_llptw_req := true.B 38244b79566SXiaokun-Pei mem_addr_update := false.B 38344b79566SXiaokun-Pei accessFault := false.B 38444b79566SXiaokun-Pei } 3852a906a65SHaoyuan Feng finish := true.B 3862a906a65SHaoyuan Feng } 38744b79566SXiaokun-Pei } 38844b79566SXiaokun-Pei 38944b79566SXiaokun-Pei 3905e237ba8SXiaokun-Pei when (flush) { 39144b79566SXiaokun-Pei idle := true.B 39244b79566SXiaokun-Pei s_pmp_check := true.B 39344b79566SXiaokun-Pei s_mem_req := true.B 39444b79566SXiaokun-Pei s_llptw_req := true.B 39544b79566SXiaokun-Pei w_mem_resp := true.B 39644b79566SXiaokun-Pei accessFault := false.B 397d826bce1SHaoyuan Feng mem_addr_update := false.B 398d0de7e4aSpeixiaokun s_hptw_req := true.B 399d0de7e4aSpeixiaokun w_hptw_resp := true.B 400d0de7e4aSpeixiaokun s_last_hptw_req := true.B 401d0de7e4aSpeixiaokun w_last_hptw_resp := true.B 40244b79566SXiaokun-Pei } 40344b79566SXiaokun-Pei 40444b79566SXiaokun-Pei 40544b79566SXiaokun-Pei XSDebug(p"[ptw] level:${level} notFound:${pageFault}\n") 4066d5ddbceSLemover 4076d5ddbceSLemover // perf 408935edac4STang Haojin XSPerfAccumulate("fsm_count", io.req.fire) 4096d5ddbceSLemover for (i <- 0 until PtwWidth) { 410935edac4STang Haojin XSPerfAccumulate(s"fsm_count_source${i}", io.req.fire && io.req.bits.req_info.source === i.U) 4116d5ddbceSLemover } 41244b79566SXiaokun-Pei XSPerfAccumulate("fsm_busy", !idle) 41344b79566SXiaokun-Pei XSPerfAccumulate("fsm_idle", idle) 4146d5ddbceSLemover XSPerfAccumulate("resp_blocked", io.resp.valid && !io.resp.ready) 415dd7fe201SHaoyuan Feng XSPerfAccumulate("ptw_ppn_af", io.resp.fire && ppn_af) 416935edac4STang Haojin XSPerfAccumulate("mem_count", mem.req.fire) 417935edac4STang Haojin XSPerfAccumulate("mem_cycle", BoolStopWatch(mem.req.fire, mem.resp.fire, true)) 4186d5ddbceSLemover XSPerfAccumulate("mem_blocked", mem.req.valid && !mem.req.ready) 419cc5a5f22SLemover 42044b79566SXiaokun-Pei TimeOutAssert(!idle, timeOutThreshold, "page table walker time out") 421cd365d4cSrvcoresjw 422cd365d4cSrvcoresjw val perfEvents = Seq( 423935edac4STang Haojin ("fsm_count ", io.req.fire ), 42444b79566SXiaokun-Pei ("fsm_busy ", !idle ), 42544b79566SXiaokun-Pei ("fsm_idle ", idle ), 426cd365d4cSrvcoresjw ("resp_blocked ", io.resp.valid && !io.resp.ready ), 427935edac4STang Haojin ("mem_count ", mem.req.fire ), 428935edac4STang Haojin ("mem_cycle ", BoolStopWatch(mem.req.fire, mem.resp.fire, true)), 429cd365d4cSrvcoresjw ("mem_blocked ", mem.req.valid && !mem.req.ready ), 430cd365d4cSrvcoresjw ) 4311ca0e4f3SYinan Xu generatePerfEvent() 4326d5ddbceSLemover} 43392e3bfefSLemover 43492e3bfefSLemover/*========================= LLPTW ==============================*/ 43592e3bfefSLemover 43692e3bfefSLemover/** LLPTW : Last Level Page Table Walker 43792e3bfefSLemover * the page walker that only takes 4KB(last level) page walk. 43892e3bfefSLemover **/ 43992e3bfefSLemover 44092e3bfefSLemoverclass LLPTWInBundle(implicit p: Parameters) extends XSBundle with HasPtwConst { 44192e3bfefSLemover val req_info = Output(new L2TlbInnerBundle()) 4424c0e0181SXiaokun-Pei val ppn = Output(UInt(gvpnLen.W)) 44392e3bfefSLemover} 44492e3bfefSLemover 44592e3bfefSLemoverclass LLPTWIO(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 44692e3bfefSLemover val in = Flipped(DecoupledIO(new LLPTWInBundle())) 44792e3bfefSLemover val out = DecoupledIO(new Bundle { 44892e3bfefSLemover val req_info = Output(new L2TlbInnerBundle()) 44992e3bfefSLemover val id = Output(UInt(bMemID.W)) 450d0de7e4aSpeixiaokun val h_resp = Output(new HptwResp) 4516979864eSXiaokun-Pei val first_s2xlate_fault = Output(Bool()) // Whether the first stage 2 translation occurs pf/af 45292e3bfefSLemover val af = Output(Bool()) 45392e3bfefSLemover }) 45492e3bfefSLemover val mem = new Bundle { 45592e3bfefSLemover val req = DecoupledIO(new L2TlbMemReqBundle()) 45692e3bfefSLemover val resp = Flipped(Valid(new Bundle { 45792e3bfefSLemover val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W)) 458ce5f4200SGuanghui Hu val value = Output(UInt(blockBits.W)) 45992e3bfefSLemover })) 46092e3bfefSLemover val enq_ptr = Output(UInt(log2Ceil(l2tlbParams.llptwsize).W)) 46192e3bfefSLemover val buffer_it = Output(Vec(l2tlbParams.llptwsize, Bool())) 46292e3bfefSLemover val refill = Output(new L2TlbInnerBundle()) 46392e3bfefSLemover val req_mask = Input(Vec(l2tlbParams.llptwsize, Bool())) 46492e3bfefSLemover } 4657797f035SbugGenerator val cache = DecoupledIO(new L2TlbInnerBundle()) 46692e3bfefSLemover val pmp = new Bundle { 46792e3bfefSLemover val req = Valid(new PMPReqBundle()) 46892e3bfefSLemover val resp = Flipped(new PMPRespBundle()) 46992e3bfefSLemover } 470d0de7e4aSpeixiaokun val hptw = new Bundle { 471d0de7e4aSpeixiaokun val req = DecoupledIO(new Bundle{ 472eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 473d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 47482978df9Speixiaokun val gvpn = UInt(vpnLen.W) 475d0de7e4aSpeixiaokun }) 476d0de7e4aSpeixiaokun val resp = Flipped(Valid(new Bundle { 477d0de7e4aSpeixiaokun val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W)) 478d0de7e4aSpeixiaokun val h_resp = Output(new HptwResp) 479d0de7e4aSpeixiaokun })) 480d0de7e4aSpeixiaokun } 48192e3bfefSLemover} 48292e3bfefSLemover 48392e3bfefSLemoverclass LLPTWEntry(implicit p: Parameters) extends XSBundle with HasPtwConst { 48492e3bfefSLemover val req_info = new L2TlbInnerBundle() 4854c0e0181SXiaokun-Pei val ppn = UInt(gvpnLen.W) 48692e3bfefSLemover val wait_id = UInt(log2Up(l2tlbParams.llptwsize).W) 48792e3bfefSLemover val af = Bool() 488dc05c713Speixiaokun val hptw_resp = new HptwResp() 4896979864eSXiaokun-Pei val first_s2xlate_fault = Output(Bool()) 49092e3bfefSLemover} 49192e3bfefSLemover 49292e3bfefSLemover 49392e3bfefSLemoverclass LLPTW(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents { 49492e3bfefSLemover val io = IO(new LLPTWIO()) 49582978df9Speixiaokun val enableS2xlate = io.in.bits.req_info.s2xlate =/= noS2xlate 496d0de7e4aSpeixiaokun val satp = Mux(enableS2xlate, io.csr.vsatp, io.csr.satp) 49792e3bfefSLemover 4985c5f442fSXiaokun-Pei val flush = io.sfence.valid || io.csr.satp.changed || io.csr.vsatp.changed || io.csr.hgatp.changed 49992e3bfefSLemover val entries = Reg(Vec(l2tlbParams.llptwsize, new LLPTWEntry())) 500d0de7e4aSpeixiaokun 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) 50192e3bfefSLemover val state = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(state_idle))) 5027797f035SbugGenerator 50392e3bfefSLemover val is_emptys = state.map(_ === state_idle) 50492e3bfefSLemover val is_mems = state.map(_ === state_mem_req) 50592e3bfefSLemover val is_waiting = state.map(_ === state_mem_waiting) 50692e3bfefSLemover val is_having = state.map(_ === state_mem_out) 5077797f035SbugGenerator val is_cache = state.map(_ === state_cache) 508d0de7e4aSpeixiaokun val is_hptw_req = state.map(_ === state_hptw_req) 509d0de7e4aSpeixiaokun val is_last_hptw_req = state.map(_ === state_last_hptw_req) 510b7bdb307Speixiaokun val is_hptw_resp = state.map(_ === state_hptw_resp) 511b7bdb307Speixiaokun val is_last_hptw_resp = state.map(_ === state_last_hptw_resp) 51292e3bfefSLemover 513935edac4STang Haojin val full = !ParallelOR(is_emptys).asBool 51492e3bfefSLemover val enq_ptr = ParallelPriorityEncoder(is_emptys) 51592e3bfefSLemover 5167797f035SbugGenerator val mem_ptr = ParallelPriorityEncoder(is_having) // TODO: optimize timing, bad: entries -> ptr -> entry 5177be7e781Speixiaokun val mem_arb = Module(new RRArbiterInit(new LLPTWEntry(), l2tlbParams.llptwsize)) 51892e3bfefSLemover for (i <- 0 until l2tlbParams.llptwsize) { 51992e3bfefSLemover mem_arb.io.in(i).bits := entries(i) 52092e3bfefSLemover mem_arb.io.in(i).valid := is_mems(i) && !io.mem.req_mask(i) 52192e3bfefSLemover } 5222a1f48e7Speixiaokun 5232a1f48e7Speixiaokun // process hptw requests in serial 5247be7e781Speixiaokun val hyper_arb1 = Module(new RRArbiterInit(new LLPTWEntry(), l2tlbParams.llptwsize)) 525d0de7e4aSpeixiaokun for (i <- 0 until l2tlbParams.llptwsize) { 526d0de7e4aSpeixiaokun hyper_arb1.io.in(i).bits := entries(i) 5272a1f48e7Speixiaokun hyper_arb1.io.in(i).valid := is_hptw_req(i) && !(Cat(is_hptw_resp).orR) && !(Cat(is_last_hptw_resp).orR) 528d0de7e4aSpeixiaokun } 5297be7e781Speixiaokun val hyper_arb2 = Module(new RRArbiterInit(new LLPTWEntry(), l2tlbParams.llptwsize)) 530d0de7e4aSpeixiaokun for(i <- 0 until l2tlbParams.llptwsize) { 531d0de7e4aSpeixiaokun hyper_arb2.io.in(i).bits := entries(i) 5322a1f48e7Speixiaokun hyper_arb2.io.in(i).valid := is_last_hptw_req(i) && !(Cat(is_hptw_resp).orR) && !(Cat(is_last_hptw_resp).orR) 533d0de7e4aSpeixiaokun } 53492e3bfefSLemover 535f3034303SHaoyuan Feng val cache_ptr = ParallelMux(is_cache, (0 until l2tlbParams.llptwsize).map(_.U(log2Up(l2tlbParams.llptwsize).W))) 5367797f035SbugGenerator 53792e3bfefSLemover // duplicate req 53892e3bfefSLemover // to_wait: wait for the last to access mem, set to mem_resp 53992e3bfefSLemover // to_cache: the last is back just right now, set to mem_cache 54092e3bfefSLemover val dup_vec = state.indices.map(i => 541cca17e78Speixiaokun dup(io.in.bits.req_info.vpn, entries(i).req_info.vpn) && io.in.bits.req_info.s2xlate === entries(i).req_info.s2xlate 54292e3bfefSLemover ) 543cca17e78Speixiaokun 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 5446979864eSXiaokun-Pei val dup_vec_wait = dup_vec.zip(is_waiting).map{case (d, w) => d && w} // dup with "mem_waiting" entries, sending mem req already 54592e3bfefSLemover 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 546951f37e5Speixiaokun val dup_vec_last_hptw = dup_vec.zipWithIndex.map{case (d, i) => d && (is_last_hptw_req(i) || is_last_hptw_resp(i))} 54792e3bfefSLemover val wait_id = Mux(dup_req_fire, mem_arb.io.chosen, ParallelMux(dup_vec_wait zip entries.map(_.wait_id))) 548935edac4STang 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 54992e3bfefSLemover val to_wait = Cat(dup_vec_wait).orR || dup_req_fire 550c6655c9aSXiaokun-Pei val to_mem_out = dup_wait_resp && ((entries(io.mem.resp.bits.id).req_info.s2xlate === noS2xlate) || (entries(io.mem.resp.bits.id).req_info.s2xlate === onlyStage1)) 551951f37e5Speixiaokun val to_cache = Cat(dup_vec_having).orR || Cat(dup_vec_last_hptw).orR 5526b742a19SXiaokun-Pei val to_hptw_req = io.in.bits.req_info.s2xlate === allStage 5536b742a19SXiaokun-Pei val to_last_hptw_req = dup_wait_resp && entries(io.mem.resp.bits.id).req_info.s2xlate === allStage 5549467c5f4Speixiaokun val last_hptw_req_id = io.mem.resp.bits.id 5554c0e0181SXiaokun-Pei val req_paddr = MakeAddr(io.in.bits.ppn(ppnLen-1, 0), getVpnn(io.in.bits.req_info.vpn, 0)) 5569467c5f4Speixiaokun val req_hpaddr = MakeAddr(entries(last_hptw_req_id).hptw_resp.genPPNS2(get_pn(req_paddr)), getVpnn(io.in.bits.req_info.vpn, 0)) 5579467c5f4Speixiaokun val index = Mux(entries(last_hptw_req_id).req_info.s2xlate === allStage, req_hpaddr, req_paddr)(log2Up(l2tlbParams.blockBytes)-1, log2Up(XLEN/8)) 5584c0e0181SXiaokun-Pei val last_hptw_req_ppn = io.mem.resp.bits.value.asTypeOf(Vec(blockBits / XLEN, new PteBundle()))(index).getPPN() 5597797f035SbugGenerator XSError(RegNext(dup_req_fire && Cat(dup_vec_wait).orR, init = false.B), "mem req but some entries already waiting, should not happed") 56092e3bfefSLemover 561935edac4STang Haojin XSError(io.in.fire && ((to_mem_out && to_cache) || (to_wait && to_cache)), "llptw enq, to cache conflict with to mem") 56292e3bfefSLemover val mem_resp_hit = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(false.B))) 5637274ec5cSpeixiaokun val enq_state_normal = MuxCase(state_addr_check, Seq( 5647274ec5cSpeixiaokun to_mem_out -> state_mem_out, // same to the blew, but the mem resp now 565871d1438Speixiaokun to_last_hptw_req -> state_last_hptw_req, 5667274ec5cSpeixiaokun to_wait -> state_mem_waiting, 5677274ec5cSpeixiaokun to_cache -> state_cache, 568871d1438Speixiaokun to_hptw_req -> state_hptw_req 5697274ec5cSpeixiaokun )) 5707797f035SbugGenerator val enq_state = Mux(from_pre(io.in.bits.req_info.source) && enq_state_normal =/= state_addr_check, state_idle, enq_state_normal) 571935edac4STang Haojin when (io.in.fire) { 57292e3bfefSLemover // if prefetch req does not need mem access, just give it up. 57392e3bfefSLemover // so there will be at most 1 + FilterSize entries that needs re-access page cache 57492e3bfefSLemover // so 2 + FilterSize is enough to avoid dead-lock 5757797f035SbugGenerator state(enq_ptr) := enq_state 57692e3bfefSLemover entries(enq_ptr).req_info := io.in.bits.req_info 5779467c5f4Speixiaokun entries(enq_ptr).ppn := Mux(to_last_hptw_req, last_hptw_req_ppn, io.in.bits.ppn) 57892e3bfefSLemover entries(enq_ptr).wait_id := Mux(to_wait, wait_id, enq_ptr) 57992e3bfefSLemover entries(enq_ptr).af := false.B 5802a1f48e7Speixiaokun 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)) 5816979864eSXiaokun-Pei entries(enq_ptr).first_s2xlate_fault := false.B 5827299828dSXiaokun-Pei mem_resp_hit(enq_ptr) := to_mem_out || to_last_hptw_req 58392e3bfefSLemover } 5847797f035SbugGenerator 5857797f035SbugGenerator val enq_ptr_reg = RegNext(enq_ptr) 5865adc4829SYanqin Li val need_addr_check = GatedValidRegNext(enq_state === state_addr_check && io.in.fire && !flush) 5877274ec5cSpeixiaokun 5880214776eSpeixiaokun val hasHptwResp = ParallelOR(state.map(_ === state_hptw_resp)).asBool 5897274ec5cSpeixiaokun val hptw_resp_ptr_reg = RegNext(io.hptw.resp.bits.id) 590a664078aSpeixiaokun val hptw_need_addr_check = RegNext(hasHptwResp && io.hptw.resp.fire && !flush) && state(hptw_resp_ptr_reg) === state_addr_check 591d0de7e4aSpeixiaokun 592ce5f4200SGuanghui Hu val ptes = io.mem.resp.bits.value.asTypeOf(Vec(blockBits / XLEN, new PteBundle())) 5933211121aSXiaokun-Pei val gpaddr = MakeGPAddr(entries(hptw_resp_ptr_reg).ppn, getVpnn(entries(hptw_resp_ptr_reg).req_info.vpn, 0)) 59482e4705bSpeixiaokun val hptw_resp = entries(hptw_resp_ptr_reg).hptw_resp 595cda84113Speixiaokun val hpaddr = Cat(hptw_resp.genPPNS2(get_pn(gpaddr)), get_off(gpaddr)) 5964c0e0181SXiaokun-Pei val addr = RegEnable(MakeAddr(io.in.bits.ppn(ppnLen - 1, 0), getVpnn(io.in.bits.req_info.vpn, 0)), io.in.fire) 5977274ec5cSpeixiaokun io.pmp.req.valid := need_addr_check || hptw_need_addr_check 59882e4705bSpeixiaokun io.pmp.req.bits.addr := Mux(hptw_need_addr_check, hpaddr, addr) 5997797f035SbugGenerator io.pmp.req.bits.cmd := TlbCmd.read 6007797f035SbugGenerator io.pmp.req.bits.size := 3.U // TODO: fix it 6017797f035SbugGenerator val pmp_resp_valid = io.pmp.req.valid // same cycle 6027797f035SbugGenerator when (pmp_resp_valid) { 6037797f035SbugGenerator // NOTE: when pmp resp but state is not addr check, then the entry is dup with other entry, the state was changed before 6047797f035SbugGenerator // when dup with the req-ing entry, set to mem_waiting (above codes), and the ld must be false, so dontcare 6057274ec5cSpeixiaokun val ptr = Mux(hptw_need_addr_check, hptw_resp_ptr_reg, enq_ptr_reg); 6067797f035SbugGenerator val accessFault = io.pmp.resp.ld || io.pmp.resp.mmio 6077274ec5cSpeixiaokun entries(ptr).af := accessFault 6087274ec5cSpeixiaokun state(ptr) := Mux(accessFault, state_mem_out, state_mem_req) 6097797f035SbugGenerator } 6107797f035SbugGenerator 611935edac4STang Haojin when (mem_arb.io.out.fire) { 61292e3bfefSLemover for (i <- state.indices) { 613ec78ed87Speixiaokun when (state(i) =/= state_idle && state(i) =/= state_mem_out && state(i) =/= state_last_hptw_req && state(i) =/= state_last_hptw_resp 614ec78ed87Speixiaokun && entries(i).req_info.s2xlate === mem_arb.io.out.bits.req_info.s2xlate 615ec78ed87Speixiaokun && dup(entries(i).req_info.vpn, mem_arb.io.out.bits.req_info.vpn)) { 61692e3bfefSLemover // NOTE: "dup enq set state to mem_wait" -> "sending req set other dup entries to mem_wait" 61792e3bfefSLemover state(i) := state_mem_waiting 6182a1f48e7Speixiaokun entries(i).hptw_resp := entries(mem_arb.io.chosen).hptw_resp 61992e3bfefSLemover entries(i).wait_id := mem_arb.io.chosen 62092e3bfefSLemover } 62192e3bfefSLemover } 62292e3bfefSLemover } 623935edac4STang Haojin when (io.mem.resp.fire) { 62492e3bfefSLemover state.indices.map{i => 62592e3bfefSLemover when (state(i) === state_mem_waiting && io.mem.resp.bits.id === entries(i).wait_id) { 6264358f287Speixiaokun val req_paddr = MakeAddr(entries(i).ppn, getVpnn(entries(i).req_info.vpn, 0)) 6274358f287Speixiaokun val req_hpaddr = MakeAddr(entries(i).hptw_resp.genPPNS2(get_pn(req_paddr)), getVpnn(entries(i).req_info.vpn, 0)) 6284358f287Speixiaokun val index = Mux(entries(i).req_info.s2xlate === allStage, req_hpaddr, req_paddr)(log2Up(l2tlbParams.blockBytes)-1, log2Up(XLEN/8)) 629*3ea4388cSHaoyuan Feng state(i) := Mux(entries(i).req_info.s2xlate === allStage && !(ptes(index).isPf(0.U) || !ptes(index).isLeaf() || ptes(index).isAf()), state_last_hptw_req, state_mem_out) 630cf41a6eeSpeixiaokun mem_resp_hit(i) := true.B 6314c0e0181SXiaokun-Pei entries(i).ppn := ptes(index).getPPN() // for last stage 2 translation 632ad0d9d89Speixiaokun } 633ad0d9d89Speixiaokun } 634ad0d9d89Speixiaokun } 635ad0d9d89Speixiaokun 6363222d00fSpeixiaokun when (hyper_arb1.io.out.fire) { 637d0de7e4aSpeixiaokun for (i <- state.indices) { 6386b742a19SXiaokun-Pei when (state(i) === state_hptw_req && entries(i).ppn === hyper_arb1.io.out.bits.ppn && entries(i).req_info.s2xlate === allStage && hyper_arb1.io.chosen === i.U) { 639d0de7e4aSpeixiaokun state(i) := state_hptw_resp 640d0de7e4aSpeixiaokun entries(i).wait_id := hyper_arb1.io.chosen 641d0de7e4aSpeixiaokun } 642d0de7e4aSpeixiaokun } 643d0de7e4aSpeixiaokun } 644d0de7e4aSpeixiaokun 6453222d00fSpeixiaokun when (hyper_arb2.io.out.fire) { 646d0de7e4aSpeixiaokun for (i <- state.indices) { 6476b742a19SXiaokun-Pei when (state(i) === state_last_hptw_req && entries(i).ppn === hyper_arb2.io.out.bits.ppn && entries(i).req_info.s2xlate === allStage && hyper_arb2.io.chosen === i.U) { 648d0de7e4aSpeixiaokun state(i) := state_last_hptw_resp 649d0de7e4aSpeixiaokun entries(i).wait_id := hyper_arb2.io.chosen 650d0de7e4aSpeixiaokun } 651d0de7e4aSpeixiaokun } 652d0de7e4aSpeixiaokun } 653d0de7e4aSpeixiaokun 6543222d00fSpeixiaokun when (io.hptw.resp.fire) { 655d0de7e4aSpeixiaokun for (i <- state.indices) { 6562a1f48e7Speixiaokun 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) { 65769f13e85SXiaokun-Pei when (io.hptw.resp.bits.h_resp.gaf || io.hptw.resp.bits.h_resp.gpf) { 65869f13e85SXiaokun-Pei state(i) := state_mem_out 65969f13e85SXiaokun-Pei entries(i).hptw_resp := io.hptw.resp.bits.h_resp 6606979864eSXiaokun-Pei entries(i).first_s2xlate_fault := io.hptw.resp.bits.h_resp.gaf || io.hptw.resp.bits.h_resp.gpf 66169f13e85SXiaokun-Pei }.otherwise{ // change the entry that is waiting hptw resp 662ec78ed87Speixiaokun 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)) 6637f96e195Speixiaokun val waiting_index = ParallelMux(need_to_waiting_vec zip entries.map(_.wait_id)) 6647f96e195Speixiaokun state(i) := Mux(Cat(need_to_waiting_vec).orR, state_mem_waiting, state_addr_check) 665dc05c713Speixiaokun entries(i).hptw_resp := io.hptw.resp.bits.h_resp 6667f96e195Speixiaokun entries(i).wait_id := Mux(Cat(need_to_waiting_vec).orR, waiting_index, entries(i).wait_id) 6672a1f48e7Speixiaokun //To do: change the entry that is having the same hptw req 668d0de7e4aSpeixiaokun } 66969f13e85SXiaokun-Pei } 6702a1f48e7Speixiaokun 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) { 671d0de7e4aSpeixiaokun state(i) := state_mem_out 672dc05c713Speixiaokun entries(i).hptw_resp := io.hptw.resp.bits.h_resp 6732a1f48e7Speixiaokun //To do: change the entry that is having the same hptw req 674d0de7e4aSpeixiaokun } 675d0de7e4aSpeixiaokun } 676d0de7e4aSpeixiaokun } 677935edac4STang Haojin when (io.out.fire) { 67892e3bfefSLemover assert(state(mem_ptr) === state_mem_out) 67992e3bfefSLemover state(mem_ptr) := state_idle 68092e3bfefSLemover } 68192e3bfefSLemover mem_resp_hit.map(a => when (a) { a := false.B } ) 68292e3bfefSLemover 6837797f035SbugGenerator when (io.cache.fire) { 6847797f035SbugGenerator state(cache_ptr) := state_idle 68592e3bfefSLemover } 6867797f035SbugGenerator XSError(io.out.fire && io.cache.fire && (mem_ptr === cache_ptr), "mem resp and cache fire at the same time at same entry") 68792e3bfefSLemover 68892e3bfefSLemover when (flush) { 68992e3bfefSLemover state.map(_ := state_idle) 69092e3bfefSLemover } 69192e3bfefSLemover 69292e3bfefSLemover io.in.ready := !full 69392e3bfefSLemover 694935edac4STang Haojin io.out.valid := ParallelOR(is_having).asBool 69592e3bfefSLemover io.out.bits.req_info := entries(mem_ptr).req_info 69692e3bfefSLemover io.out.bits.id := mem_ptr 69792e3bfefSLemover io.out.bits.af := entries(mem_ptr).af 698dc05c713Speixiaokun io.out.bits.h_resp := entries(mem_ptr).hptw_resp 6996979864eSXiaokun-Pei io.out.bits.first_s2xlate_fault := entries(mem_ptr).first_s2xlate_fault 700d0de7e4aSpeixiaokun 70183d93d53Speixiaokun val hptw_req_arb = Module(new Arbiter(new Bundle{ 70283d93d53Speixiaokun val source = UInt(bSourceWidth.W) 70383d93d53Speixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 7044c0e0181SXiaokun-Pei val ppn = UInt(gvpnLen.W) 70583d93d53Speixiaokun } , 2)) 70683d93d53Speixiaokun // first stage 2 translation 70783d93d53Speixiaokun hptw_req_arb.io.in(0).valid := hyper_arb1.io.out.valid 70883d93d53Speixiaokun hptw_req_arb.io.in(0).bits.source := hyper_arb1.io.out.bits.req_info.source 70983d93d53Speixiaokun hptw_req_arb.io.in(0).bits.ppn := hyper_arb1.io.out.bits.ppn 71083d93d53Speixiaokun hptw_req_arb.io.in(0).bits.id := hyper_arb1.io.chosen 7112a1f48e7Speixiaokun hyper_arb1.io.out.ready := hptw_req_arb.io.in(0).ready 71283d93d53Speixiaokun // last stage 2 translation 71383d93d53Speixiaokun hptw_req_arb.io.in(1).valid := hyper_arb2.io.out.valid 71483d93d53Speixiaokun hptw_req_arb.io.in(1).bits.source := hyper_arb2.io.out.bits.req_info.source 71583d93d53Speixiaokun hptw_req_arb.io.in(1).bits.ppn := hyper_arb2.io.out.bits.ppn 71683d93d53Speixiaokun hptw_req_arb.io.in(1).bits.id := hyper_arb2.io.chosen 7172a1f48e7Speixiaokun hyper_arb2.io.out.ready := hptw_req_arb.io.in(1).ready 71883d93d53Speixiaokun hptw_req_arb.io.out.ready := io.hptw.req.ready 7192a1f48e7Speixiaokun io.hptw.req.valid := hptw_req_arb.io.out.fire && !flush 72083d93d53Speixiaokun io.hptw.req.bits.gvpn := hptw_req_arb.io.out.bits.ppn 72183d93d53Speixiaokun io.hptw.req.bits.id := hptw_req_arb.io.out.bits.id 72283d93d53Speixiaokun io.hptw.req.bits.source := hptw_req_arb.io.out.bits.source 72392e3bfefSLemover 72492e3bfefSLemover io.mem.req.valid := mem_arb.io.out.valid && !flush 725dc05c713Speixiaokun val mem_paddr = MakeAddr(mem_arb.io.out.bits.ppn, getVpnn(mem_arb.io.out.bits.req_info.vpn, 0)) 726cda84113Speixiaokun 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)) 7276b742a19SXiaokun-Pei io.mem.req.bits.addr := Mux(mem_arb.io.out.bits.req_info.s2xlate === allStage, mem_hpaddr, mem_paddr) 72892e3bfefSLemover io.mem.req.bits.id := mem_arb.io.chosen 72983d93d53Speixiaokun io.mem.req.bits.hptw_bypassed := false.B 73092e3bfefSLemover mem_arb.io.out.ready := io.mem.req.ready 731933ec998Speixiaokun val mem_refill_id = RegNext(io.mem.resp.bits.id(log2Up(l2tlbParams.llptwsize)-1, 0)) 732933ec998Speixiaokun io.mem.refill := entries(mem_refill_id).req_info 7334ed5afbdSXiaokun-Pei io.mem.refill.s2xlate := entries(mem_refill_id).req_info.s2xlate 73492e3bfefSLemover io.mem.buffer_it := mem_resp_hit 73592e3bfefSLemover io.mem.enq_ptr := enq_ptr 73692e3bfefSLemover 7377797f035SbugGenerator io.cache.valid := Cat(is_cache).orR 7387797f035SbugGenerator io.cache.bits := ParallelMux(is_cache, entries.map(_.req_info)) 7397797f035SbugGenerator 740935edac4STang Haojin XSPerfAccumulate("llptw_in_count", io.in.fire) 74192e3bfefSLemover XSPerfAccumulate("llptw_in_block", io.in.valid && !io.in.ready) 74292e3bfefSLemover for (i <- 0 until 7) { 743935edac4STang Haojin XSPerfAccumulate(s"enq_state${i}", io.in.fire && enq_state === i.U) 74492e3bfefSLemover } 74592e3bfefSLemover for (i <- 0 until (l2tlbParams.llptwsize + 1)) { 74692e3bfefSLemover XSPerfAccumulate(s"util${i}", PopCount(is_emptys.map(!_)) === i.U) 74792e3bfefSLemover XSPerfAccumulate(s"mem_util${i}", PopCount(is_mems) === i.U) 74892e3bfefSLemover XSPerfAccumulate(s"waiting_util${i}", PopCount(is_waiting) === i.U) 74992e3bfefSLemover } 750935edac4STang Haojin XSPerfAccumulate("mem_count", io.mem.req.fire) 75192e3bfefSLemover XSPerfAccumulate("mem_cycle", PopCount(is_waiting) =/= 0.U) 75292e3bfefSLemover XSPerfAccumulate("blocked_in", io.in.valid && !io.in.ready) 75392e3bfefSLemover 75492e3bfefSLemover for (i <- 0 until l2tlbParams.llptwsize) { 75592e3bfefSLemover TimeOutAssert(state(i) =/= state_idle, timeOutThreshold, s"missqueue time out no out ${i}") 75692e3bfefSLemover } 75792e3bfefSLemover 75892e3bfefSLemover val perfEvents = Seq( 759935edac4STang Haojin ("tlbllptw_incount ", io.in.fire ), 76092e3bfefSLemover ("tlbllptw_inblock ", io.in.valid && !io.in.ready), 761935edac4STang Haojin ("tlbllptw_memcount ", io.mem.req.fire ), 76292e3bfefSLemover ("tlbllptw_memcycle ", PopCount(is_waiting) ), 76392e3bfefSLemover ) 76492e3bfefSLemover generatePerfEvent() 76592e3bfefSLemover} 766d0de7e4aSpeixiaokun 767d0de7e4aSpeixiaokun/*========================= HPTW ==============================*/ 768d0de7e4aSpeixiaokun 769d0de7e4aSpeixiaokun/** HPTW : Hypervisor Page Table Walker 770d0de7e4aSpeixiaokun * the page walker take the virtual machine's page walk. 771d0de7e4aSpeixiaokun * guest physical address translation, guest physical address -> host physical address 772d0de7e4aSpeixiaokun **/ 773d0de7e4aSpeixiaokunclass HPTWIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 774d0de7e4aSpeixiaokun val req = Flipped(DecoupledIO(new Bundle { 775eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 776d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 77782978df9Speixiaokun val gvpn = UInt(vpnLen.W) 7786315ba2aSpeixiaokun val ppn = UInt(ppnLen.W) 779*3ea4388cSHaoyuan Feng val l3Hit = if (EnableSv48) Some(new Bool()) else None 780d0de7e4aSpeixiaokun val l2Hit = Bool() 781*3ea4388cSHaoyuan Feng val l1Hit = Bool() 78283d93d53Speixiaokun val bypassed = Bool() // if bypass, don't refill 783d0de7e4aSpeixiaokun })) 784c2b430edSpeixiaokun val resp = DecoupledIO(new Bundle { 785eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 786d0de7e4aSpeixiaokun val resp = Output(new HptwResp()) 787d0de7e4aSpeixiaokun val id = Output(UInt(bMemID.W)) 788d0de7e4aSpeixiaokun }) 789d0de7e4aSpeixiaokun 790d0de7e4aSpeixiaokun val mem = new Bundle { 791d0de7e4aSpeixiaokun val req = DecoupledIO(new L2TlbMemReqBundle()) 792d0de7e4aSpeixiaokun val resp = Flipped(ValidIO(UInt(XLEN.W))) 793d0de7e4aSpeixiaokun val mask = Input(Bool()) 794d0de7e4aSpeixiaokun } 795d0de7e4aSpeixiaokun val refill = Output(new Bundle { 796d0de7e4aSpeixiaokun val req_info = new L2TlbInnerBundle() 797*3ea4388cSHaoyuan Feng val level = UInt(log2Up(Level + 1).W) 798d0de7e4aSpeixiaokun }) 799d0de7e4aSpeixiaokun val pmp = new Bundle { 800d0de7e4aSpeixiaokun val req = ValidIO(new PMPReqBundle()) 801d0de7e4aSpeixiaokun val resp = Flipped(new PMPRespBundle()) 802d0de7e4aSpeixiaokun } 803d0de7e4aSpeixiaokun} 804d0de7e4aSpeixiaokun 805d0de7e4aSpeixiaokunclass HPTW()(implicit p: Parameters) extends XSModule with HasPtwConst { 806d0de7e4aSpeixiaokun val io = IO(new HPTWIO) 807d0de7e4aSpeixiaokun val hgatp = io.csr.hgatp 808d0de7e4aSpeixiaokun val sfence = io.sfence 8091ae5db63SXiaokun-Pei val flush = sfence.valid || hgatp.changed || io.csr.satp.changed || io.csr.vsatp.changed 810*3ea4388cSHaoyuan Feng val mode = hgatp.mode 811d0de7e4aSpeixiaokun 812*3ea4388cSHaoyuan Feng val level = RegInit(3.U(log2Up(Level + 1).W)) 813d0de7e4aSpeixiaokun val gpaddr = Reg(UInt(GPAddrBits.W)) 8144c4af37cSpeixiaokun val req_ppn = Reg(UInt(ppnLen.W)) 815d0de7e4aSpeixiaokun val vpn = gpaddr(GPAddrBits-1, offLen) 816*3ea4388cSHaoyuan Feng val levelNext = level - 1.U 817*3ea4388cSHaoyuan Feng val l3Hit = Reg(Bool()) 818d0de7e4aSpeixiaokun val l2Hit = Reg(Bool()) 819*3ea4388cSHaoyuan Feng val l1Hit = Reg(Bool()) 82083d93d53Speixiaokun val bypassed = Reg(Bool()) 821d0de7e4aSpeixiaokun// val pte = io.mem.resp.bits.MergeRespToPte() 822d0de7e4aSpeixiaokun val pte = io.mem.resp.bits.asTypeOf(new PteBundle().cloneType) 823*3ea4388cSHaoyuan Feng val ppn_l3 = Mux(l3Hit, req_ppn, pte.ppn) 8244c4af37cSpeixiaokun val ppn_l2 = Mux(l2Hit, req_ppn, pte.ppn) 825*3ea4388cSHaoyuan Feng val ppn_l1 = Mux(l1Hit, req_ppn, pte.ppn) 826*3ea4388cSHaoyuan Feng val ppn = Wire(UInt(PAddrBits.W)) 827*3ea4388cSHaoyuan Feng val p_pte = MakeAddr(ppn, getVpnn(vpn, level)) 828*3ea4388cSHaoyuan Feng val pg_base = Wire(UInt(PAddrBits.W)) 829*3ea4388cSHaoyuan Feng val mem_addr = Wire(UInt(PAddrBits.W)) 830*3ea4388cSHaoyuan Feng if (EnableSv48) { 831*3ea4388cSHaoyuan Feng when (mode === Sv48) { 832*3ea4388cSHaoyuan Feng ppn := Mux(level === 2.U, ppn_l3, Mux(level === 1.U, ppn_l2, ppn_l1)) // for l2, l1 and l3 833*3ea4388cSHaoyuan Feng pg_base := MakeGPAddr(hgatp.ppn, getGVpnn(vpn, 3.U, mode = Sv48)) // for l3 834*3ea4388cSHaoyuan Feng mem_addr := Mux(level === 3.U, pg_base, p_pte) 835*3ea4388cSHaoyuan Feng } .otherwise { 836*3ea4388cSHaoyuan Feng ppn := Mux(level === 1.U, ppn_l2, ppn_l1) //for l1 and l2 837*3ea4388cSHaoyuan Feng pg_base := MakeGPAddr(hgatp.ppn, getGVpnn(vpn, 2.U, mode = Sv39)) 838*3ea4388cSHaoyuan Feng mem_addr := Mux(level === 2.U, pg_base, p_pte) 839*3ea4388cSHaoyuan Feng } 840*3ea4388cSHaoyuan Feng } else { 841*3ea4388cSHaoyuan Feng ppn := Mux(level === 1.U, ppn_l2, ppn_l1) //for l1 and l2 842*3ea4388cSHaoyuan Feng pg_base := MakeGPAddr(hgatp.ppn, getGVpnn(vpn, 2.U, mode = Sv39)) 843*3ea4388cSHaoyuan Feng mem_addr := Mux(level === 2.U, pg_base, p_pte) 844*3ea4388cSHaoyuan Feng } 845d0de7e4aSpeixiaokun 846d0de7e4aSpeixiaokun //s/w register 847d0de7e4aSpeixiaokun val s_pmp_check = RegInit(true.B) 848d0de7e4aSpeixiaokun val s_mem_req = RegInit(true.B) 849d0de7e4aSpeixiaokun val w_mem_resp = RegInit(true.B) 850d0de7e4aSpeixiaokun val idle = RegInit(true.B) 85103c1129fSpeixiaokun val mem_addr_update = RegInit(false.B) 852d0de7e4aSpeixiaokun val finish = WireInit(false.B) 853d0de7e4aSpeixiaokun 854d0de7e4aSpeixiaokun val sent_to_pmp = !idle && (!s_pmp_check || mem_addr_update) && !finish 855*3ea4388cSHaoyuan Feng val pageFault = pte.isPf(level) || (!pte.isLeaf() && level === 0.U) 856d0de7e4aSpeixiaokun val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, sent_to_pmp) 857d0de7e4aSpeixiaokun 858d0de7e4aSpeixiaokun val ppn_af = pte.isAf() 859d0de7e4aSpeixiaokun val find_pte = pte.isLeaf() || ppn_af || pageFault 860d0de7e4aSpeixiaokun 861d0de7e4aSpeixiaokun val resp_valid = !idle && mem_addr_update && ((w_mem_resp && find_pte) || (s_pmp_check && accessFault)) 862d0de7e4aSpeixiaokun val id = Reg(UInt(log2Up(l2tlbParams.llptwsize).W)) 8633222d00fSpeixiaokun val source = RegEnable(io.req.bits.source, io.req.fire) 864eb4bf3f2Speixiaokun 865d0de7e4aSpeixiaokun io.req.ready := idle 866eb4bf3f2Speixiaokun val resp = Wire(new HptwResp()) 867d0de7e4aSpeixiaokun resp.apply(pageFault && !accessFault && !ppn_af, accessFault || ppn_af, level, pte, vpn, hgatp.asid) 868d0de7e4aSpeixiaokun io.resp.valid := resp_valid 869d0de7e4aSpeixiaokun io.resp.bits.id := id 870d0de7e4aSpeixiaokun io.resp.bits.resp := resp 871eb4bf3f2Speixiaokun io.resp.bits.source := source 872d0de7e4aSpeixiaokun 873d0de7e4aSpeixiaokun io.pmp.req.valid := DontCare 874d0de7e4aSpeixiaokun io.pmp.req.bits.addr := mem_addr 875d0de7e4aSpeixiaokun io.pmp.req.bits.size := 3.U 876d0de7e4aSpeixiaokun io.pmp.req.bits.cmd := TlbCmd.read 877d0de7e4aSpeixiaokun 878d0de7e4aSpeixiaokun io.mem.req.valid := !s_mem_req && !io.mem.mask && !accessFault && s_pmp_check 879d0de7e4aSpeixiaokun io.mem.req.bits.addr := mem_addr 880d0de7e4aSpeixiaokun io.mem.req.bits.id := HptwReqId.U(bMemID.W) 88183d93d53Speixiaokun io.mem.req.bits.hptw_bypassed := bypassed 882d0de7e4aSpeixiaokun 88382978df9Speixiaokun io.refill.req_info.vpn := vpn 884d0de7e4aSpeixiaokun io.refill.level := level 885eb4bf3f2Speixiaokun io.refill.req_info.source := source 886eb4bf3f2Speixiaokun io.refill.req_info.s2xlate := onlyStage2 887d0de7e4aSpeixiaokun when (idle){ 8883222d00fSpeixiaokun when(io.req.fire){ 88983d93d53Speixiaokun bypassed := io.req.bits.bypassed 890d0de7e4aSpeixiaokun idle := false.B 891d0de7e4aSpeixiaokun gpaddr := Cat(io.req.bits.gvpn, 0.U(offLen.W)) 892d0de7e4aSpeixiaokun accessFault := false.B 893d0de7e4aSpeixiaokun s_pmp_check := false.B 894d0de7e4aSpeixiaokun id := io.req.bits.id 8954c4af37cSpeixiaokun req_ppn := io.req.bits.ppn 896*3ea4388cSHaoyuan Feng if (EnableSv48) { 897*3ea4388cSHaoyuan Feng when (mode === Sv48) { 898*3ea4388cSHaoyuan Feng level := Mux(io.req.bits.l1Hit, 0.U, Mux(io.req.bits.l2Hit, 1.U, Mux(io.req.bits.l3Hit.get, 2.U, 3.U))) 899*3ea4388cSHaoyuan Feng l3Hit := io.req.bits.l3Hit.get 900*3ea4388cSHaoyuan Feng } .otherwise { 901*3ea4388cSHaoyuan Feng level := Mux(io.req.bits.l1Hit, 0.U, Mux(io.req.bits.l2Hit, 1.U, 2.U)) 902*3ea4388cSHaoyuan Feng l3Hit := false.B 903*3ea4388cSHaoyuan Feng } 904*3ea4388cSHaoyuan Feng } else { 905*3ea4388cSHaoyuan Feng level := Mux(io.req.bits.l1Hit, 0.U, Mux(io.req.bits.l2Hit, 1.U, 2.U)) 906*3ea4388cSHaoyuan Feng l3Hit := false.B 907*3ea4388cSHaoyuan Feng } 908d0de7e4aSpeixiaokun l2Hit := io.req.bits.l2Hit 909*3ea4388cSHaoyuan Feng l1Hit := io.req.bits.l1Hit 910d0de7e4aSpeixiaokun } 911d0de7e4aSpeixiaokun } 912d0de7e4aSpeixiaokun 913d0de7e4aSpeixiaokun when(sent_to_pmp && !mem_addr_update){ 914d0de7e4aSpeixiaokun s_mem_req := false.B 915d0de7e4aSpeixiaokun s_pmp_check := true.B 916d0de7e4aSpeixiaokun } 917d0de7e4aSpeixiaokun 918d0de7e4aSpeixiaokun when(accessFault && !idle){ 919d0de7e4aSpeixiaokun s_pmp_check := true.B 920d0de7e4aSpeixiaokun s_mem_req := true.B 921d0de7e4aSpeixiaokun w_mem_resp := true.B 922d0de7e4aSpeixiaokun mem_addr_update := true.B 923d0de7e4aSpeixiaokun } 924d0de7e4aSpeixiaokun 9253222d00fSpeixiaokun when(io.mem.req.fire){ 926d0de7e4aSpeixiaokun s_mem_req := true.B 927d0de7e4aSpeixiaokun w_mem_resp := false.B 928d0de7e4aSpeixiaokun } 929d0de7e4aSpeixiaokun 9303222d00fSpeixiaokun when(io.mem.resp.fire && !w_mem_resp){ 931d0de7e4aSpeixiaokun w_mem_resp := true.B 932d0de7e4aSpeixiaokun mem_addr_update := true.B 933d0de7e4aSpeixiaokun } 934d0de7e4aSpeixiaokun 935d0de7e4aSpeixiaokun when(mem_addr_update){ 936d0de7e4aSpeixiaokun when(!(find_pte || accessFault)){ 937d0de7e4aSpeixiaokun level := levelNext 938d0de7e4aSpeixiaokun s_mem_req := false.B 939d0de7e4aSpeixiaokun mem_addr_update := false.B 940d0de7e4aSpeixiaokun }.elsewhen(resp_valid){ 9413222d00fSpeixiaokun when(io.resp.fire){ 942d0de7e4aSpeixiaokun idle := true.B 943d0de7e4aSpeixiaokun mem_addr_update := false.B 944d0de7e4aSpeixiaokun accessFault := false.B 945d0de7e4aSpeixiaokun } 946d0de7e4aSpeixiaokun finish := true.B 947d0de7e4aSpeixiaokun } 948d0de7e4aSpeixiaokun } 9495961467fSXiaokun-Pei when (flush) { 9505961467fSXiaokun-Pei idle := true.B 9515961467fSXiaokun-Pei s_pmp_check := true.B 9525961467fSXiaokun-Pei s_mem_req := true.B 9535961467fSXiaokun-Pei w_mem_resp := true.B 9545961467fSXiaokun-Pei accessFault := false.B 9555961467fSXiaokun-Pei mem_addr_update := false.B 9565961467fSXiaokun-Pei } 957d0de7e4aSpeixiaokun} 958