16d5ddbceSLemover/*************************************************************************************** 28882eb68SXin Tian* Copyright (c) 2021-2025 Beijing Institute of Open Source Chip (BOSC) 38882eb68SXin Tian* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 4f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory 58882eb68SXin Tian* Copyright (c) 2024-2025 Institute of Information Engineering, Chinese Academy of Sciences 66d5ddbceSLemover* 76d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2. 86d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2. 96d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at: 106d5ddbceSLemover* http://license.coscl.org.cn/MulanPSL2 116d5ddbceSLemover* 126d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 136d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 146d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 156d5ddbceSLemover* 166d5ddbceSLemover* See the Mulan PSL v2 for more details. 176d5ddbceSLemover***************************************************************************************/ 186d5ddbceSLemover 196d5ddbceSLemoverpackage xiangshan.cache.mmu 206d5ddbceSLemover 218891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 226d5ddbceSLemoverimport chisel3._ 236d5ddbceSLemoverimport chisel3.util._ 246d5ddbceSLemoverimport xiangshan._ 256d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants} 266d5ddbceSLemoverimport utils._ 273c02ee8fSwakafaimport utility._ 286d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 296d5ddbceSLemoverimport freechips.rocketchip.tilelink._ 30b6982e83SLemoverimport xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle} 316d5ddbceSLemover 3292e3bfefSLemover/** Page Table Walk is divided into two parts 3392e3bfefSLemover * One, PTW: page walk for pde, except for leaf entries, one by one 3492e3bfefSLemover * Two, LLPTW: page walk for pte, only the leaf entries(4KB), in parallel 356d5ddbceSLemover */ 3692e3bfefSLemover 3792e3bfefSLemover 3892e3bfefSLemover/** PTW : page table walker 3992e3bfefSLemover * a finite state machine 4092e3bfefSLemover * only take 1GB and 2MB page walks 4192e3bfefSLemover * or in other words, except the last level(leaf) 4292e3bfefSLemover **/ 4392e3bfefSLemoverclass PTWIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 446d5ddbceSLemover val req = Flipped(DecoupledIO(new Bundle { 4545f497a4Shappy-lx val req_info = new L2TlbInnerBundle() 463ea4388cSHaoyuan Feng val l3Hit = if (EnableSv48) Some(new Bool()) else None 473ea4388cSHaoyuan Feng val l2Hit = Bool() 4897929664SXiaokun-Pei val ppn = UInt(ptePPNLen.W) 4930104977Speixiaokun val stage1Hit = Bool() 5030104977Speixiaokun val stage1 = new PtwMergeResp 518882eb68SXin Tian val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle { 528882eb68SXin Tian val jmp_bitmap_check = Bool() // super page in PtwCache ptw hit, but need bitmap check 538882eb68SXin Tian val pte = UInt(XLEN.W) // Page Table Entry 548882eb68SXin Tian val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector 558882eb68SXin Tian val SPlevel = UInt(log2Up(Level).W) 568882eb68SXin Tian }) 576d5ddbceSLemover })) 586d5ddbceSLemover val resp = DecoupledIO(new Bundle { 59bc063562SLemover val source = UInt(bSourceWidth.W) 60eb4bf3f2Speixiaokun val s2xlate = UInt(2.W) 6163632028SHaoyuan Feng val resp = new PtwMergeResp 62d0de7e4aSpeixiaokun val h_resp = new HptwResp 636d5ddbceSLemover }) 646d5ddbceSLemover 6592e3bfefSLemover val llptw = DecoupledIO(new LLPTWInBundle()) 669c503409SLemover // NOTE: llptw change from "connect to llptw" to "connect to page cache" 679c503409SLemover // to avoid corner case that caused duplicate entries 68cc5a5f22SLemover 69d0de7e4aSpeixiaokun val hptw = new Bundle { 70d0de7e4aSpeixiaokun val req = DecoupledIO(new Bundle { 71eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 72d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 7397929664SXiaokun-Pei val gvpn = UInt(ptePPNLen.W) 74d0de7e4aSpeixiaokun }) 75d0de7e4aSpeixiaokun val resp = Flipped(Valid(new Bundle { 76d0de7e4aSpeixiaokun val h_resp = Output(new HptwResp) 77d0de7e4aSpeixiaokun })) 78d0de7e4aSpeixiaokun } 796d5ddbceSLemover val mem = new Bundle { 80b848eea5SLemover val req = DecoupledIO(new L2TlbMemReqBundle()) 815854c1edSLemover val resp = Flipped(ValidIO(UInt(XLEN.W))) 82cc5a5f22SLemover val mask = Input(Bool()) 836d5ddbceSLemover } 84b6982e83SLemover val pmp = new Bundle { 85b6982e83SLemover val req = ValidIO(new PMPReqBundle()) 86b6982e83SLemover val resp = Flipped(new PMPRespBundle()) 87b6982e83SLemover } 886d5ddbceSLemover 896d5ddbceSLemover val refill = Output(new Bundle { 9045f497a4Shappy-lx val req_info = new L2TlbInnerBundle() 913ea4388cSHaoyuan Feng val level = UInt(log2Up(Level + 1).W) 926d5ddbceSLemover }) 938882eb68SXin Tian val bitmap = Option.when(HasBitmapCheck)(new Bundle { 948882eb68SXin Tian val req = DecoupledIO(new bitmapReqBundle()) 958882eb68SXin Tian val resp = Flipped(DecoupledIO(new bitmapRespBundle())) 968882eb68SXin Tian }) 976d5ddbceSLemover} 986d5ddbceSLemover 9992e3bfefSLemoverclass PTW()(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents { 10092e3bfefSLemover val io = IO(new PTWIO) 1016d5ddbceSLemover val sfence = io.sfence 1026d5ddbceSLemover val mem = io.mem 103d0de7e4aSpeixiaokun val req_s2xlate = Reg(UInt(2.W)) 10403c1129fSpeixiaokun val enableS2xlate = req_s2xlate =/= noS2xlate 10503c1129fSpeixiaokun val onlyS1xlate = req_s2xlate === onlyStage1 10603c1129fSpeixiaokun val onlyS2xlate = req_s2xlate === onlyStage2 1078882eb68SXin Tian 1088882eb68SXin Tian // mbmc:bitmap csr 1098882eb68SXin Tian val mbmc = io.csr.mbmc 1108882eb68SXin Tian val bitmap_enable = (if (HasBitmapCheck) true.B else false.B) && mbmc.BME === 1.U && mbmc.CMODE === 0.U 1118882eb68SXin Tian 1123ea4388cSHaoyuan Feng val satp = Wire(new TlbSatpBundle()) 1133ea4388cSHaoyuan Feng when (io.req.fire) { 1143ea4388cSHaoyuan Feng satp := Mux(io.req.bits.req_info.s2xlate =/= noS2xlate, io.csr.vsatp, io.csr.satp) 1153ea4388cSHaoyuan Feng } .otherwise { 1163ea4388cSHaoyuan Feng satp := Mux(enableS2xlate, io.csr.vsatp, io.csr.satp) 1173ea4388cSHaoyuan Feng } 118dd286b6aSYanqin Li val s1Pbmte = Mux(req_s2xlate =/= noS2xlate, io.csr.hPBMTE, io.csr.mPBMTE) 1193ea4388cSHaoyuan Feng 1203ea4388cSHaoyuan Feng val mode = satp.mode 121d0de7e4aSpeixiaokun val hgatp = io.csr.hgatp 1225c5f442fSXiaokun-Pei val flush = io.sfence.valid || io.csr.satp.changed || io.csr.vsatp.changed || io.csr.hgatp.changed 123d0de7e4aSpeixiaokun val s2xlate = enableS2xlate && !onlyS1xlate 1243ea4388cSHaoyuan Feng val level = RegInit(3.U(log2Up(Level + 1).W)) 1253ea4388cSHaoyuan Feng val af_level = RegInit(3.U(log2Up(Level + 1).W)) // access fault return this level 12697929664SXiaokun-Pei val gpf_level = RegInit(3.U(log2Up(Level + 1).W)) 12797929664SXiaokun-Pei val ppn = Reg(UInt(ptePPNLen.W)) 1284c0e0181SXiaokun-Pei val vpn = Reg(UInt(vpnLen.W)) // vpn or gvpn(onlyS2xlate) 1293ea4388cSHaoyuan Feng val levelNext = level - 1.U 1303ea4388cSHaoyuan Feng val l3Hit = Reg(Bool()) 1313ea4388cSHaoyuan Feng val l2Hit = Reg(Bool()) 1328882eb68SXin Tian val jmp_bitmap_check_w = if (HasBitmapCheck) { io.req.bits.bitmapCheck.get.jmp_bitmap_check && io.req.bits.req_info.s2xlate =/= onlyStage2 } else { false.B } 1338882eb68SXin Tian val jmp_bitmap_check_r = if (HasBitmapCheck) { RegEnable(jmp_bitmap_check_w, io.req.fire) } else { false.B } 1348882eb68SXin Tian val cache_pte = Option.when(HasBitmapCheck)(RegEnable(io.req.bits.bitmapCheck.get.pte.asTypeOf(new PteBundle().cloneType), io.req.fire)) 1358882eb68SXin Tian val pte = if (HasBitmapCheck) { Mux(jmp_bitmap_check_r, cache_pte.get, io.mem.resp.bits.asTypeOf(new PteBundle().cloneType)) } else { mem.resp.bits.asTypeOf(new PteBundle()) } 1363ea4388cSHaoyuan Feng 13744b79566SXiaokun-Pei // s/w register 13844b79566SXiaokun-Pei val s_pmp_check = RegInit(true.B) 13944b79566SXiaokun-Pei val s_mem_req = RegInit(true.B) 14044b79566SXiaokun-Pei val s_llptw_req = RegInit(true.B) 14144b79566SXiaokun-Pei val w_mem_resp = RegInit(true.B) 142d0de7e4aSpeixiaokun val s_hptw_req = RegInit(true.B) 143d0de7e4aSpeixiaokun val w_hptw_resp = RegInit(true.B) 144d0de7e4aSpeixiaokun val s_last_hptw_req = RegInit(true.B) 145d0de7e4aSpeixiaokun val w_last_hptw_resp = RegInit(true.B) 14644b79566SXiaokun-Pei // for updating "level" 14744b79566SXiaokun-Pei val mem_addr_update = RegInit(false.B) 14844b79566SXiaokun-Pei 1498882eb68SXin Tian val s_bitmap_check = RegInit(true.B) 1508882eb68SXin Tian val w_bitmap_resp = RegInit(true.B) 1518882eb68SXin Tian val whether_need_bitmap_check = RegInit(false.B) 1528882eb68SXin Tian val bitmap_checkfailed = RegInit(false.B) 1538882eb68SXin Tian 15444b79566SXiaokun-Pei val idle = RegInit(true.B) 1552a906a65SHaoyuan Feng val finish = WireInit(false.B) 156e5ff9bcbSHaoyuan Feng dontTouch(finish) 15798ca902eSHaoyuan Feng val vs_finish = WireInit(false.B) // need to wait for G-stage translate, should not do pmp check 158e5ff9bcbSHaoyuan Feng dontTouch(vs_finish) 1596d5ddbceSLemover 160d0de7e4aSpeixiaokun val hptw_pageFault = RegInit(false.B) 161d0de7e4aSpeixiaokun val hptw_accessFault = RegInit(false.B) 162fa9d630eSXiaokun-Pei val need_last_s2xlate = RegInit(false.B) 1633222d00fSpeixiaokun val stage1Hit = RegEnable(io.req.bits.stage1Hit, io.req.fire) 1643222d00fSpeixiaokun val stage1 = RegEnable(io.req.bits.stage1, io.req.fire) 16509280d15Speixiaokun val hptw_resp_stage2 = Reg(Bool()) 16616de2f57SHaoyuan Feng val first_gvpn_check_fail = RegInit(false.B) 167d0de7e4aSpeixiaokun 1688882eb68SXin Tian // use accessfault repersent bitmap check failed 1698882eb68SXin Tian val pte_isAf = Mux(bitmap_enable, pte.isAf() || bitmap_checkfailed, pte.isAf()) 1708882eb68SXin Tian val ppn_af = if (HasBitmapCheck) { 1718882eb68SXin Tian Mux(enableS2xlate, Mux(onlyS1xlate, pte_isAf, false.B), pte_isAf) // In two-stage address translation, stage 1 ppn is a vpn for host, so don't need to check ppn_high 1728882eb68SXin Tian } else { 1738882eb68SXin Tian Mux(enableS2xlate, Mux(onlyS1xlate, pte.isAf(), false.B), pte.isAf()) // In two-stage address translation, stage 1 ppn is a vpn for host, so don't need to check ppn_high 1748882eb68SXin Tian } 175f8c4173dSHaoyuan Feng val pte_valid = RegInit(false.B) // avoid l1tlb pf from stage1 when gpf happens in the first s2xlate in PTW 1768882eb68SXin Tian 177f8c4173dSHaoyuan Feng val pageFault = pte.isPf(level, s1Pbmte) 1787263b595SXiaokun-Pei val find_pte = pte.isLeaf() || ppn_af || pageFault 17944b79566SXiaokun-Pei val to_find_pte = level === 1.U && find_pte === false.B 180935edac4STang Haojin val source = RegEnable(io.req.bits.req_info.source, io.req.fire) 1816d5ddbceSLemover 18298ca902eSHaoyuan Feng val sent_to_pmp = idle === false.B && (s_pmp_check === false.B || mem_addr_update) && !finish && !vs_finish && !first_gvpn_check_fail && !(find_pte && pte_valid) 183f8c4173dSHaoyuan Feng val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, false.B, sent_to_pmp) 184f8c4173dSHaoyuan Feng 1856aa6d737SHaoyuan Feng val l3addr = Wire(UInt(ptePaddrLen.W)) 1866aa6d737SHaoyuan Feng val l2addr = Wire(UInt(ptePaddrLen.W)) 1876aa6d737SHaoyuan Feng val l1addr = Wire(UInt(ptePaddrLen.W)) 1886aa6d737SHaoyuan Feng val hptw_addr = Wire(UInt(ptePaddrLen.W)) 1893ea4388cSHaoyuan Feng val mem_addr = Wire(UInt(PAddrBits.W)) 1903ea4388cSHaoyuan Feng 1913ea4388cSHaoyuan Feng l3addr := MakeAddr(satp.ppn, getVpnn(vpn, 3)) 1923ea4388cSHaoyuan Feng if (EnableSv48) { 1933ea4388cSHaoyuan Feng when (mode === Sv48) { 1943ea4388cSHaoyuan Feng l2addr := MakeAddr(Mux(l3Hit, ppn, pte.getPPN()), getVpnn(vpn, 2)) 1953ea4388cSHaoyuan Feng } .otherwise { 1963ea4388cSHaoyuan Feng l2addr := MakeAddr(satp.ppn, getVpnn(vpn, 2)) 1973ea4388cSHaoyuan Feng } 1983ea4388cSHaoyuan Feng } else { 1993ea4388cSHaoyuan Feng l2addr := MakeAddr(satp.ppn, getVpnn(vpn, 2)) 2003ea4388cSHaoyuan Feng } 2013ea4388cSHaoyuan Feng l1addr := MakeAddr(Mux(l2Hit, ppn, pte.getPPN()), getVpnn(vpn, 1)) 2026aa6d737SHaoyuan Feng hptw_addr := Mux(af_level === 3.U, l3addr, Mux(af_level === 2.U, l2addr, l1addr)) 2036aa6d737SHaoyuan Feng mem_addr := hptw_addr(PAddrBits - 1, 0) 20444b79566SXiaokun-Pei 20597929664SXiaokun-Pei val hptw_resp = Reg(new HptwResp) 20648639700SXu, Zefan 20748639700SXu, Zefan val update_full_gvpn_mem_resp = RegInit(false.B) 20848639700SXu, Zefan val full_gvpn_reg = Reg(UInt(ptePPNLen.W)) 20948639700SXu, Zefan val full_gvpn_wire = pte.getPPN() 21048639700SXu, Zefan val full_gvpn = Mux(update_full_gvpn_mem_resp, full_gvpn_wire, full_gvpn_reg) 21148639700SXu, Zefan 2126aa6d737SHaoyuan Feng val gpaddr = MuxCase(hptw_addr, Seq( 213faf7d50bSXiaokun-Pei (stage1Hit || onlyS2xlate) -> Cat(full_gvpn, 0.U(offLen.W)), 214faf7d50bSXiaokun-Pei !s_last_hptw_req -> Cat(MuxLookup(level, pte.getPPN())(Seq( 21597929664SXiaokun-Pei 3.U -> Cat(pte.getPPN()(ptePPNLen - 1, vpnnLen * 3), vpn(vpnnLen * 3 - 1, 0)), 21697929664SXiaokun-Pei 2.U -> Cat(pte.getPPN()(ptePPNLen - 1, vpnnLen * 2), vpn(vpnnLen * 2 - 1, 0)), 21797929664SXiaokun-Pei 1.U -> Cat(pte.getPPN()(ptePPNLen - 1, vpnnLen), vpn(vpnnLen - 1, 0) 218dcb10e8fSBL-GS ))), 219dcb10e8fSBL-GS 0.U(offLen.W)) 220c0991f6aSpeixiaokun )) 22148639700SXu, Zefan val gvpn_gpf = 22216de2f57SHaoyuan Feng (!(hptw_pageFault || hptw_accessFault || ((pageFault || ppn_af) && pte_valid)) && 22348639700SXu, Zefan Mux( 22448639700SXu, Zefan s2xlate && io.csr.hgatp.mode === Sv39x4, 22548639700SXu, Zefan full_gvpn(ptePPNLen - 1, GPAddrBitsSv39x4 - offLen) =/= 0.U, 22648639700SXu, Zefan Mux( 22748639700SXu, Zefan s2xlate && io.csr.hgatp.mode === Sv48x4, 22848639700SXu, Zefan full_gvpn(ptePPNLen - 1, GPAddrBitsSv48x4 - offLen) =/= 0.U, 22948639700SXu, Zefan false.B 23048639700SXu, Zefan ) 23116de2f57SHaoyuan Feng )) || first_gvpn_check_fail 23248639700SXu, Zefan 2338deba996SXiaokun-Pei val guestFault = hptw_pageFault || hptw_accessFault || gvpn_gpf 234cda84113Speixiaokun val hpaddr = Cat(hptw_resp.genPPNS2(get_pn(gpaddr)), get_off(gpaddr)) 23581ed4161SJiuyue Ma val fake_h_resp = WireInit(0.U.asTypeOf(new HptwResp)) 23608ae0d20SXiaokun-Pei fake_h_resp.entry.tag := get_pn(gpaddr) 23708ae0d20SXiaokun-Pei fake_h_resp.entry.vmid.map(_ := io.csr.hgatp.vmid) 23897929664SXiaokun-Pei fake_h_resp.gpf := true.B 23997929664SXiaokun-Pei 24081ed4161SJiuyue Ma val fake_pte = WireInit(0.U.asTypeOf(new PteBundle())) 241ad8d4021SXiaokun-Pei fake_pte.perm.v := false.B // tell L1TLB this is fake pte 242d15c2433SXiaokun-Pei fake_pte.ppn := ppn(ppnLen - 1, 0) 243d15c2433SXiaokun-Pei fake_pte.ppn_high := ppn(ptePPNLen - 1, ppnLen) 244d0de7e4aSpeixiaokun 24544b79566SXiaokun-Pei io.req.ready := idle 24630104977Speixiaokun val ptw_resp = Wire(new PtwMergeResp) 247c4ffb7e4SHaoyuan Feng // pageFault is always valid when pte_valid 248c4ffb7e4SHaoyuan Feng val resp_pf = pte_valid && pageFault 249c4ffb7e4SHaoyuan Feng // when (pte_valid && (pageFault || guestFault), should not report accessFault or ppn_af 250c4ffb7e4SHaoyuan Feng val resp_af = (accessFault || ppn_af) && !((pte_valid && pageFault) || guestFault) 251c4ffb7e4SHaoyuan Feng // should use af_level when accessFault && !((pte_valid && pageFault) || guestFault) 252c4ffb7e4SHaoyuan Feng val resp_level = Mux(accessFault && resp_af, af_level, Mux(guestFault, gpf_level, level)) 253c4ffb7e4SHaoyuan Feng // when ptw do not really send a memory request, should use fake_pte 254c4ffb7e4SHaoyuan Feng val resp_pte = Mux(pte_valid, pte, fake_pte) 255c4ffb7e4SHaoyuan Feng ptw_resp.apply(resp_pf, resp_af, resp_level, resp_pte, vpn, satp.asid, hgatp.vmid, vpn(sectortlbwidth - 1, 0), not_super = false, not_merge = false, bitmap_checkfailed.asBool) 25644b79566SXiaokun-Pei 257fa9d630eSXiaokun-Pei val normal_resp = idle === false.B && mem_addr_update && !need_last_s2xlate && (guestFault || (w_mem_resp && find_pte) || (s_pmp_check && accessFault) || onlyS2xlate ) 25809280d15Speixiaokun val stageHit_resp = idle === false.B && hptw_resp_stage2 25909280d15Speixiaokun io.resp.valid := Mux(stage1Hit, stageHit_resp, normal_resp) 26044b79566SXiaokun-Pei io.resp.bits.source := source 26197929664SXiaokun-Pei io.resp.bits.resp := Mux(stage1Hit || (l3Hit || l2Hit) && guestFault && !pte_valid, stage1, ptw_resp) 26297929664SXiaokun-Pei io.resp.bits.h_resp := Mux(gvpn_gpf, fake_h_resp, hptw_resp) 2636315ba2aSpeixiaokun io.resp.bits.s2xlate := req_s2xlate 26444b79566SXiaokun-Pei 26597929664SXiaokun-Pei io.llptw.valid := s_llptw_req === false.B && to_find_pte && !accessFault && !guestFault 26644b79566SXiaokun-Pei io.llptw.bits.req_info.source := source 26744b79566SXiaokun-Pei io.llptw.bits.req_info.vpn := vpn 26882978df9Speixiaokun io.llptw.bits.req_info.s2xlate := req_s2xlate 269eb4bf3f2Speixiaokun io.llptw.bits.ppn := DontCare 2708882eb68SXin Tian if (HasBitmapCheck) { 2718882eb68SXin Tian io.llptw.bits.bitmapCheck.get.jmp_bitmap_check := DontCare 2728882eb68SXin Tian io.llptw.bits.bitmapCheck.get.ptes := DontCare 2738882eb68SXin Tian io.llptw.bits.bitmapCheck.get.cfs := DontCare 2748882eb68SXin Tian io.llptw.bits.bitmapCheck.get.hitway := DontCare 2758882eb68SXin Tian } 27644b79566SXiaokun-Pei 277b6982e83SLemover io.pmp.req.valid := DontCare // samecycle, do not use valid 278d0de7e4aSpeixiaokun io.pmp.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr) 279b6982e83SLemover io.pmp.req.bits.size := 3.U // TODO: fix it 280b6982e83SLemover io.pmp.req.bits.cmd := TlbCmd.read 281b6982e83SLemover 2828882eb68SXin Tian if (HasBitmapCheck) { 2838882eb68SXin Tian val cache_level = RegEnable(io.req.bits.bitmapCheck.get.SPlevel, io.req.fire) 2848882eb68SXin Tian io.bitmap.get.req.valid := !s_bitmap_check 2858882eb68SXin Tian io.bitmap.get.req.bits.bmppn := pte.ppn 2868882eb68SXin Tian io.bitmap.get.req.bits.id := FsmReqID.U(bMemID.W) 2878882eb68SXin Tian io.bitmap.get.req.bits.vpn := vpn 2888882eb68SXin Tian io.bitmap.get.req.bits.level := Mux(jmp_bitmap_check_r, cache_level, level) 2898882eb68SXin Tian io.bitmap.get.req.bits.way_info := DontCare 2908882eb68SXin Tian io.bitmap.get.req.bits.hptw_bypassed := false.B 2918882eb68SXin Tian io.bitmap.get.resp.ready := !w_bitmap_resp 2928882eb68SXin Tian } 29344b79566SXiaokun-Pei mem.req.valid := s_mem_req === false.B && !mem.mask && !accessFault && s_pmp_check 294d0de7e4aSpeixiaokun mem.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr) 295bc063562SLemover mem.req.bits.id := FsmReqID.U(bMemID.W) 29683d93d53Speixiaokun mem.req.bits.hptw_bypassed := false.B 2976d5ddbceSLemover 2984ed5afbdSXiaokun-Pei io.refill.req_info.s2xlate := req_s2xlate 29945f497a4Shappy-lx io.refill.req_info.vpn := vpn 3006d5ddbceSLemover io.refill.level := level 30145f497a4Shappy-lx io.refill.req_info.source := source 3026d5ddbceSLemover 303d0de7e4aSpeixiaokun io.hptw.req.valid := !s_hptw_req || !s_last_hptw_req 304d0de7e4aSpeixiaokun io.hptw.req.bits.id := FsmReqID.U(bMemID.W) 305dcb10e8fSBL-GS io.hptw.req.bits.gvpn := get_pn(gpaddr) 306eb4bf3f2Speixiaokun io.hptw.req.bits.source := source 307d0de7e4aSpeixiaokun 3088882eb68SXin Tian if (HasBitmapCheck) { 3098882eb68SXin Tian when (io.req.fire && jmp_bitmap_check_w) { 3108882eb68SXin Tian idle := false.B 3118882eb68SXin Tian req_s2xlate := io.req.bits.req_info.s2xlate 3128882eb68SXin Tian vpn := io.req.bits.req_info.vpn 3138882eb68SXin Tian s_bitmap_check := false.B 3148882eb68SXin Tian need_last_s2xlate := false.B 3158882eb68SXin Tian hptw_pageFault := false.B 3168882eb68SXin Tian hptw_accessFault := false.B 3178882eb68SXin Tian level := io.req.bits.bitmapCheck.get.SPlevel 3188882eb68SXin Tian pte_valid := true.B 3198882eb68SXin Tian accessFault := false.B 3208882eb68SXin Tian } 3218882eb68SXin Tian } 3228882eb68SXin Tian 3238882eb68SXin Tian when (io.req.fire && io.req.bits.stage1Hit && (if (HasBitmapCheck) !jmp_bitmap_check_w else true.B)) { 32430104977Speixiaokun idle := false.B 32561c5d636Speixiaokun req_s2xlate := io.req.bits.req_info.s2xlate 326fffcb38cSXiaokun-Pei s_last_hptw_req := false.B 32709280d15Speixiaokun hptw_resp_stage2 := false.B 328fa9d630eSXiaokun-Pei need_last_s2xlate := false.B 3290dfe2fbdSpeixiaokun hptw_pageFault := false.B 3300dfe2fbdSpeixiaokun hptw_accessFault := false.B 33148639700SXu, Zefan full_gvpn_reg := io.req.bits.stage1.genPPN() 33230104977Speixiaokun } 333d0de7e4aSpeixiaokun 3343222d00fSpeixiaokun when (io.resp.fire && stage1Hit){ 33530104977Speixiaokun idle := true.B 33630104977Speixiaokun } 33730104977Speixiaokun 3388882eb68SXin Tian when (io.req.fire && !io.req.bits.stage1Hit && (if (HasBitmapCheck) !jmp_bitmap_check_w else true.B)) { 33944b79566SXiaokun-Pei val req = io.req.bits 3402d991346SXiaokun-Pei val gvpn_wire = Wire(UInt(ptePPNLen.W)) 3413ea4388cSHaoyuan Feng if (EnableSv48) { 3423ea4388cSHaoyuan Feng when (mode === Sv48) { 3433ea4388cSHaoyuan Feng level := Mux(req.l2Hit, 1.U, Mux(req.l3Hit.get, 2.U, 3.U)) 3443ea4388cSHaoyuan Feng af_level := Mux(req.l2Hit, 1.U, Mux(req.l3Hit.get, 2.U, 3.U)) 345ad8d4021SXiaokun-Pei gpf_level := Mux(req.l2Hit, 2.U, Mux(req.l3Hit.get, 3.U, 0.U)) 3463ea4388cSHaoyuan Feng ppn := Mux(req.l2Hit || req.l3Hit.get, io.req.bits.ppn, satp.ppn) 3473ea4388cSHaoyuan Feng l3Hit := req.l3Hit.get 3482d991346SXiaokun-Pei gvpn_wire := Mux(req.l2Hit || req.l3Hit.get, io.req.bits.ppn, satp.ppn) 3493ea4388cSHaoyuan Feng } .otherwise { 3503ea4388cSHaoyuan Feng level := Mux(req.l2Hit, 1.U, 2.U) 3513ea4388cSHaoyuan Feng af_level := Mux(req.l2Hit, 1.U, 2.U) 352220c4701SHaoyuan Feng gpf_level := Mux(req.l2Hit, 2.U, 0.U) 3533ea4388cSHaoyuan Feng ppn := Mux(req.l2Hit, io.req.bits.ppn, satp.ppn) 3543ea4388cSHaoyuan Feng l3Hit := false.B 3552d991346SXiaokun-Pei gvpn_wire := Mux(req.l2Hit, io.req.bits.ppn, satp.ppn) 3563ea4388cSHaoyuan Feng } 3573ea4388cSHaoyuan Feng } else { 3583ea4388cSHaoyuan Feng level := Mux(req.l2Hit, 1.U, 2.U) 3593ea4388cSHaoyuan Feng af_level := Mux(req.l2Hit, 1.U, 2.U) 360220c4701SHaoyuan Feng gpf_level := Mux(req.l2Hit, 2.U, 0.U) 3613ea4388cSHaoyuan Feng ppn := Mux(req.l2Hit, io.req.bits.ppn, satp.ppn) 3623ea4388cSHaoyuan Feng l3Hit := false.B 3632d991346SXiaokun-Pei gvpn_wire := Mux(req.l2Hit, io.req.bits.ppn, satp.ppn) 3643ea4388cSHaoyuan Feng } 36544b79566SXiaokun-Pei vpn := io.req.bits.req_info.vpn 3663ea4388cSHaoyuan Feng l2Hit := req.l2Hit 36744b79566SXiaokun-Pei accessFault := false.B 36844b79566SXiaokun-Pei idle := false.B 369d0de7e4aSpeixiaokun hptw_pageFault := false.B 3707263b595SXiaokun-Pei hptw_accessFault := false.B 371cc72e3f5SXiaokun-Pei pte_valid := false.B 37250c7aa78Speixiaokun req_s2xlate := io.req.bits.req_info.s2xlate 373fffcb38cSXiaokun-Pei when(io.req.bits.req_info.s2xlate === onlyStage2){ 37448639700SXu, Zefan full_gvpn_reg := io.req.bits.req_info.vpn 375f284fbffSXiaokun-Pei val onlys2_gpaddr = Cat(io.req.bits.req_info.vpn, 0.U(offLen.W)) // is 50 bits, don't need to check high bits when sv48x4 is enabled 376f284fbffSXiaokun-Pei val check_gpa_high_fail = Mux(io.req.bits.req_info.s2xlate === onlyStage2 && io.csr.hgatp.mode === Sv39x4, onlys2_gpaddr(onlys2_gpaddr.getWidth - 1, GPAddrBitsSv39x4) =/= 0.U, false.B) 377fa9d630eSXiaokun-Pei need_last_s2xlate := false.B 378fffcb38cSXiaokun-Pei when(check_gpa_high_fail){ 379fffcb38cSXiaokun-Pei mem_addr_update := true.B 38016de2f57SHaoyuan Feng first_gvpn_check_fail := true.B 38108ae0d20SXiaokun-Pei }.otherwise{ 382fffcb38cSXiaokun-Pei s_last_hptw_req := false.B 383fffcb38cSXiaokun-Pei } 384fffcb38cSXiaokun-Pei }.elsewhen(io.req.bits.req_info.s2xlate === allStage){ 38548639700SXu, Zefan full_gvpn_reg := 0.U 3862d991346SXiaokun-Pei val allstage_gpaddr = Cat(gvpn_wire, 0.U(offLen.W)) 3872d991346SXiaokun-Pei val check_gpa_high_fail = Mux(io.csr.hgatp.mode === Sv39x4, allstage_gpaddr(allstage_gpaddr.getWidth - 1, GPAddrBitsSv39x4) =/= 0.U, Mux(io.csr.hgatp.mode === Sv48x4, allstage_gpaddr(allstage_gpaddr.getWidth - 1, GPAddrBitsSv48x4) =/= 0.U, false.B)) 3882d991346SXiaokun-Pei when(check_gpa_high_fail){ 3892d991346SXiaokun-Pei mem_addr_update := true.B 39016de2f57SHaoyuan Feng first_gvpn_check_fail := true.B 3912d991346SXiaokun-Pei }.otherwise{ 392fa9d630eSXiaokun-Pei need_last_s2xlate := true.B 393d0de7e4aSpeixiaokun s_hptw_req := false.B 3942d991346SXiaokun-Pei } 395d0de7e4aSpeixiaokun }.otherwise { 39648639700SXu, Zefan full_gvpn_reg := 0.U 397fa9d630eSXiaokun-Pei need_last_s2xlate := false.B 398d0de7e4aSpeixiaokun s_pmp_check := false.B 399d0de7e4aSpeixiaokun } 400d0de7e4aSpeixiaokun } 401d0de7e4aSpeixiaokun 4023222d00fSpeixiaokun when(io.hptw.req.fire && s_hptw_req === false.B){ 403d0de7e4aSpeixiaokun s_hptw_req := true.B 404d0de7e4aSpeixiaokun w_hptw_resp := false.B 405d0de7e4aSpeixiaokun } 406d0de7e4aSpeixiaokun 407fffcb38cSXiaokun-Pei when(io.hptw.resp.fire && w_hptw_resp === false.B) { 408d0de7e4aSpeixiaokun w_hptw_resp := true.B 409903ff891SXiaokun-Pei val g_perm_fail = !io.hptw.resp.bits.h_resp.gaf && (!io.hptw.resp.bits.h_resp.entry.perm.get.r && !(io.csr.priv.mxr && io.hptw.resp.bits.h_resp.entry.perm.get.x)) 4108deba996SXiaokun-Pei hptw_pageFault := io.hptw.resp.bits.h_resp.gpf || g_perm_fail 4118deba996SXiaokun-Pei hptw_accessFault := io.hptw.resp.bits.h_resp.gaf 4128deba996SXiaokun-Pei hptw_resp := io.hptw.resp.bits.h_resp 4138deba996SXiaokun-Pei hptw_resp.gpf := io.hptw.resp.bits.h_resp.gpf || g_perm_fail 414fffcb38cSXiaokun-Pei when(!(g_perm_fail || io.hptw.resp.bits.h_resp.gpf || io.hptw.resp.bits.h_resp.gaf)) { 415d0de7e4aSpeixiaokun s_pmp_check := false.B 416093b2fcbSXiaokun-Pei }.otherwise { 417093b2fcbSXiaokun-Pei mem_addr_update := true.B 418fa9d630eSXiaokun-Pei need_last_s2xlate := false.B 419d0de7e4aSpeixiaokun } 420d0de7e4aSpeixiaokun } 421d0de7e4aSpeixiaokun 4223222d00fSpeixiaokun when(io.hptw.req.fire && s_last_hptw_req === false.B) { 423d0de7e4aSpeixiaokun w_last_hptw_resp := false.B 424d0de7e4aSpeixiaokun s_last_hptw_req := true.B 425d0de7e4aSpeixiaokun } 426d0de7e4aSpeixiaokun 427fffcb38cSXiaokun-Pei when (io.hptw.resp.fire && w_last_hptw_resp === false.B && stage1Hit){ 428fffcb38cSXiaokun-Pei w_last_hptw_resp := true.B 429fffcb38cSXiaokun-Pei hptw_resp_stage2 := true.B 430fffcb38cSXiaokun-Pei hptw_resp := io.hptw.resp.bits.h_resp 431fffcb38cSXiaokun-Pei } 432fffcb38cSXiaokun-Pei 433fffcb38cSXiaokun-Pei when(io.hptw.resp.fire && w_last_hptw_resp === false.B && !stage1Hit){ 434d0de7e4aSpeixiaokun hptw_pageFault := io.hptw.resp.bits.h_resp.gpf 435d0de7e4aSpeixiaokun hptw_accessFault := io.hptw.resp.bits.h_resp.gaf 43697929664SXiaokun-Pei hptw_resp := io.hptw.resp.bits.h_resp 437d0de7e4aSpeixiaokun w_last_hptw_resp := true.B 438d0de7e4aSpeixiaokun mem_addr_update := true.B 43944b79566SXiaokun-Pei } 44044b79566SXiaokun-Pei 44144b79566SXiaokun-Pei when(sent_to_pmp && mem_addr_update === false.B){ 44244b79566SXiaokun-Pei s_mem_req := false.B 44344b79566SXiaokun-Pei s_pmp_check := true.B 44444b79566SXiaokun-Pei } 44544b79566SXiaokun-Pei 44698ca902eSHaoyuan Feng when(accessFault && idle === false.B){ 44744b79566SXiaokun-Pei s_pmp_check := true.B 44844b79566SXiaokun-Pei s_mem_req := true.B 44944b79566SXiaokun-Pei w_mem_resp := true.B 45044b79566SXiaokun-Pei s_llptw_req := true.B 451d0de7e4aSpeixiaokun s_hptw_req := true.B 452d0de7e4aSpeixiaokun w_hptw_resp := true.B 453d0de7e4aSpeixiaokun s_last_hptw_req := true.B 454d0de7e4aSpeixiaokun w_last_hptw_resp := true.B 45544b79566SXiaokun-Pei mem_addr_update := true.B 456fa9d630eSXiaokun-Pei need_last_s2xlate := false.B 4578882eb68SXin Tian if (HasBitmapCheck) { 4588882eb68SXin Tian s_bitmap_check := true.B 4598882eb68SXin Tian w_bitmap_resp := true.B 4608882eb68SXin Tian whether_need_bitmap_check := false.B 4618882eb68SXin Tian bitmap_checkfailed := false.B 4628882eb68SXin Tian } 46344b79566SXiaokun-Pei } 46444b79566SXiaokun-Pei 46597929664SXiaokun-Pei when(guestFault && idle === false.B){ 4667263b595SXiaokun-Pei s_pmp_check := true.B 4677263b595SXiaokun-Pei s_mem_req := true.B 4687263b595SXiaokun-Pei w_mem_resp := true.B 4697263b595SXiaokun-Pei s_llptw_req := true.B 4707263b595SXiaokun-Pei s_hptw_req := true.B 4717263b595SXiaokun-Pei w_hptw_resp := true.B 4727263b595SXiaokun-Pei s_last_hptw_req := true.B 4737263b595SXiaokun-Pei w_last_hptw_resp := true.B 4747263b595SXiaokun-Pei mem_addr_update := true.B 475fa9d630eSXiaokun-Pei need_last_s2xlate := false.B 4768882eb68SXin Tian if (HasBitmapCheck) { 4778882eb68SXin Tian s_bitmap_check := true.B 4788882eb68SXin Tian w_bitmap_resp := true.B 4798882eb68SXin Tian whether_need_bitmap_check := false.B 4808882eb68SXin Tian bitmap_checkfailed := false.B 4818882eb68SXin Tian } 4827263b595SXiaokun-Pei } 4837263b595SXiaokun-Pei 484935edac4STang Haojin when (mem.req.fire){ 48544b79566SXiaokun-Pei s_mem_req := true.B 48644b79566SXiaokun-Pei w_mem_resp := false.B 48744b79566SXiaokun-Pei } 48844b79566SXiaokun-Pei 489935edac4STang Haojin when(mem.resp.fire && w_mem_resp === false.B){ 49044b79566SXiaokun-Pei w_mem_resp := true.B 4913ea4388cSHaoyuan Feng af_level := af_level - 1.U 492220c4701SHaoyuan Feng gpf_level := Mux(mode === Sv39 && !pte_valid && !l2Hit, gpf_level - 2.U, gpf_level - 1.U) 493cc72e3f5SXiaokun-Pei pte_valid := true.B 49448639700SXu, Zefan update_full_gvpn_mem_resp := true.B 4958882eb68SXin Tian if (HasBitmapCheck) { 4968882eb68SXin Tian when (bitmap_enable) { 4978882eb68SXin Tian whether_need_bitmap_check := true.B 4988882eb68SXin Tian } .otherwise { 499d6b0a27fSLMiaoH s_llptw_req := false.B 5008882eb68SXin Tian mem_addr_update := true.B 5018882eb68SXin Tian whether_need_bitmap_check := false.B 5028882eb68SXin Tian } 5038882eb68SXin Tian } else { 504d6b0a27fSLMiaoH s_llptw_req := false.B 5058882eb68SXin Tian mem_addr_update := true.B 5068882eb68SXin Tian } 50748639700SXu, Zefan } 50848639700SXu, Zefan 50948639700SXu, Zefan when(update_full_gvpn_mem_resp) { 51048639700SXu, Zefan update_full_gvpn_mem_resp := false.B 51148639700SXu, Zefan full_gvpn_reg := pte.getPPN() 51244b79566SXiaokun-Pei } 51344b79566SXiaokun-Pei 5148882eb68SXin Tian if (HasBitmapCheck) { 5158882eb68SXin Tian when (whether_need_bitmap_check) { 5168882eb68SXin Tian when (bitmap_enable && (!enableS2xlate || onlyS1xlate) && pte.isLeaf()) { 5178882eb68SXin Tian s_bitmap_check := false.B 5188882eb68SXin Tian whether_need_bitmap_check := false.B 5198882eb68SXin Tian } .otherwise { 5208882eb68SXin Tian mem_addr_update := true.B 521d6b0a27fSLMiaoH s_llptw_req := false.B 5228882eb68SXin Tian whether_need_bitmap_check := false.B 5238882eb68SXin Tian } 5248882eb68SXin Tian } 5258882eb68SXin Tian // bitmapcheck 5268882eb68SXin Tian when (io.bitmap.get.req.fire) { 5278882eb68SXin Tian s_bitmap_check := true.B 5288882eb68SXin Tian w_bitmap_resp := false.B 5298882eb68SXin Tian } 5308882eb68SXin Tian when (io.bitmap.get.resp.fire) { 5318882eb68SXin Tian w_bitmap_resp := true.B 5328882eb68SXin Tian mem_addr_update := true.B 5338882eb68SXin Tian bitmap_checkfailed := io.bitmap.get.resp.bits.cf 5348882eb68SXin Tian } 5358882eb68SXin Tian } 5368882eb68SXin Tian 53744b79566SXiaokun-Pei when(mem_addr_update){ 53897929664SXiaokun-Pei when(level >= 2.U && !onlyS2xlate && !(guestFault || find_pte || accessFault)) { 53944b79566SXiaokun-Pei level := levelNext 540d0de7e4aSpeixiaokun when(s2xlate){ 541d0de7e4aSpeixiaokun s_hptw_req := false.B 54298ca902eSHaoyuan Feng vs_finish := true.B 543d0de7e4aSpeixiaokun }.otherwise{ 54444b79566SXiaokun-Pei s_mem_req := false.B 545d0de7e4aSpeixiaokun } 54644b79566SXiaokun-Pei s_llptw_req := true.B 54744b79566SXiaokun-Pei mem_addr_update := false.B 5482a906a65SHaoyuan Feng }.elsewhen(io.llptw.valid){ 549935edac4STang Haojin when(io.llptw.fire) { 55044b79566SXiaokun-Pei idle := true.B 55144b79566SXiaokun-Pei s_llptw_req := true.B 55244b79566SXiaokun-Pei mem_addr_update := false.B 553fa9d630eSXiaokun-Pei need_last_s2xlate := false.B 5542a906a65SHaoyuan Feng } 5552a906a65SHaoyuan Feng finish := true.B 556fa9d630eSXiaokun-Pei }.elsewhen(s2xlate && need_last_s2xlate === true.B) { 557fa9d630eSXiaokun-Pei need_last_s2xlate := false.B 558fa9d630eSXiaokun-Pei when(!(guestFault || accessFault || pageFault || ppn_af)){ 559d0de7e4aSpeixiaokun s_last_hptw_req := false.B 560d0de7e4aSpeixiaokun mem_addr_update := false.B 5617c26eb06SXiaokun-Pei } 5622a906a65SHaoyuan Feng }.elsewhen(io.resp.valid){ 563935edac4STang Haojin when(io.resp.fire) { 56444b79566SXiaokun-Pei idle := true.B 56544b79566SXiaokun-Pei s_llptw_req := true.B 56644b79566SXiaokun-Pei mem_addr_update := false.B 56744b79566SXiaokun-Pei accessFault := false.B 56816de2f57SHaoyuan Feng first_gvpn_check_fail := false.B 56944b79566SXiaokun-Pei } 5702a906a65SHaoyuan Feng finish := true.B 5712a906a65SHaoyuan Feng } 57244b79566SXiaokun-Pei } 57344b79566SXiaokun-Pei 57444b79566SXiaokun-Pei 5755e237ba8SXiaokun-Pei when (flush) { 57644b79566SXiaokun-Pei idle := true.B 57744b79566SXiaokun-Pei s_pmp_check := true.B 57844b79566SXiaokun-Pei s_mem_req := true.B 57944b79566SXiaokun-Pei s_llptw_req := true.B 58044b79566SXiaokun-Pei w_mem_resp := true.B 58144b79566SXiaokun-Pei accessFault := false.B 582d826bce1SHaoyuan Feng mem_addr_update := false.B 58316de2f57SHaoyuan Feng first_gvpn_check_fail := false.B 584d0de7e4aSpeixiaokun s_hptw_req := true.B 585d0de7e4aSpeixiaokun w_hptw_resp := true.B 586d0de7e4aSpeixiaokun s_last_hptw_req := true.B 587d0de7e4aSpeixiaokun w_last_hptw_resp := true.B 5888882eb68SXin Tian if (HasBitmapCheck) { 5898882eb68SXin Tian s_bitmap_check := true.B 5908882eb68SXin Tian w_bitmap_resp := true.B 5918882eb68SXin Tian whether_need_bitmap_check := false.B 5928882eb68SXin Tian bitmap_checkfailed := false.B 5938882eb68SXin Tian } 59444b79566SXiaokun-Pei } 59544b79566SXiaokun-Pei 59644b79566SXiaokun-Pei 59744b79566SXiaokun-Pei XSDebug(p"[ptw] level:${level} notFound:${pageFault}\n") 5986d5ddbceSLemover 5996d5ddbceSLemover // perf 600935edac4STang Haojin XSPerfAccumulate("fsm_count", io.req.fire) 6016d5ddbceSLemover for (i <- 0 until PtwWidth) { 602935edac4STang Haojin XSPerfAccumulate(s"fsm_count_source${i}", io.req.fire && io.req.bits.req_info.source === i.U) 6036d5ddbceSLemover } 60444b79566SXiaokun-Pei XSPerfAccumulate("fsm_busy", !idle) 60544b79566SXiaokun-Pei XSPerfAccumulate("fsm_idle", idle) 6066d5ddbceSLemover XSPerfAccumulate("resp_blocked", io.resp.valid && !io.resp.ready) 607dd7fe201SHaoyuan Feng XSPerfAccumulate("ptw_ppn_af", io.resp.fire && ppn_af) 608935edac4STang Haojin XSPerfAccumulate("mem_count", mem.req.fire) 609935edac4STang Haojin XSPerfAccumulate("mem_cycle", BoolStopWatch(mem.req.fire, mem.resp.fire, true)) 6106d5ddbceSLemover XSPerfAccumulate("mem_blocked", mem.req.valid && !mem.req.ready) 611cc5a5f22SLemover 612cd365d4cSrvcoresjw val perfEvents = Seq( 613935edac4STang Haojin ("fsm_count ", io.req.fire ), 61444b79566SXiaokun-Pei ("fsm_busy ", !idle ), 61544b79566SXiaokun-Pei ("fsm_idle ", idle ), 616cd365d4cSrvcoresjw ("resp_blocked ", io.resp.valid && !io.resp.ready ), 617935edac4STang Haojin ("mem_count ", mem.req.fire ), 618935edac4STang Haojin ("mem_cycle ", BoolStopWatch(mem.req.fire, mem.resp.fire, true)), 619cd365d4cSrvcoresjw ("mem_blocked ", mem.req.valid && !mem.req.ready ), 620cd365d4cSrvcoresjw ) 6211ca0e4f3SYinan Xu generatePerfEvent() 6226d5ddbceSLemover} 62392e3bfefSLemover 62492e3bfefSLemover/*========================= LLPTW ==============================*/ 62592e3bfefSLemover 62692e3bfefSLemover/** LLPTW : Last Level Page Table Walker 62792e3bfefSLemover * the page walker that only takes 4KB(last level) page walk. 62892e3bfefSLemover **/ 62992e3bfefSLemover 63092e3bfefSLemoverclass LLPTWInBundle(implicit p: Parameters) extends XSBundle with HasPtwConst { 63192e3bfefSLemover val req_info = Output(new L2TlbInnerBundle()) 63297929664SXiaokun-Pei val ppn = Output(UInt(ptePPNLen.W)) 6338882eb68SXin Tian val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle { 6348882eb68SXin Tian val jmp_bitmap_check = Bool() // find pte in l0 or sp, but need bitmap check 6358882eb68SXin Tian val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) // Page Table Entry Vector 6368882eb68SXin Tian val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector 6378882eb68SXin Tian val hitway = UInt(l2tlbParams.l0nWays.W) 6388882eb68SXin Tian }) 63992e3bfefSLemover} 64092e3bfefSLemover 64192e3bfefSLemoverclass LLPTWIO(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 64292e3bfefSLemover val in = Flipped(DecoupledIO(new LLPTWInBundle())) 64392e3bfefSLemover val out = DecoupledIO(new Bundle { 64492e3bfefSLemover val req_info = Output(new L2TlbInnerBundle()) 64592e3bfefSLemover val id = Output(UInt(bMemID.W)) 646d0de7e4aSpeixiaokun val h_resp = Output(new HptwResp) 6476979864eSXiaokun-Pei val first_s2xlate_fault = Output(Bool()) // Whether the first stage 2 translation occurs pf/af 64892e3bfefSLemover val af = Output(Bool()) 6498882eb68SXin Tian val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle { 6508882eb68SXin Tian val jmp_bitmap_check = Bool() // find pte in l0 or sp, but need bitmap check 6518882eb68SXin Tian val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) // Page Table Entry Vector 6528882eb68SXin Tian val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector 6538882eb68SXin Tian }) 65492e3bfefSLemover }) 65592e3bfefSLemover val mem = new Bundle { 65692e3bfefSLemover val req = DecoupledIO(new L2TlbMemReqBundle()) 65792e3bfefSLemover val resp = Flipped(Valid(new Bundle { 65892e3bfefSLemover val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W)) 659ce5f4200SGuanghui Hu val value = Output(UInt(blockBits.W)) 66092e3bfefSLemover })) 66192e3bfefSLemover val enq_ptr = Output(UInt(log2Ceil(l2tlbParams.llptwsize).W)) 66292e3bfefSLemover val buffer_it = Output(Vec(l2tlbParams.llptwsize, Bool())) 66392e3bfefSLemover val refill = Output(new L2TlbInnerBundle()) 66492e3bfefSLemover val req_mask = Input(Vec(l2tlbParams.llptwsize, Bool())) 66597929664SXiaokun-Pei val flush_latch = Input(Vec(l2tlbParams.llptwsize, Bool())) 66692e3bfefSLemover } 6677797f035SbugGenerator val cache = DecoupledIO(new L2TlbInnerBundle()) 66892e3bfefSLemover val pmp = new Bundle { 66992e3bfefSLemover val req = Valid(new PMPReqBundle()) 67092e3bfefSLemover val resp = Flipped(new PMPRespBundle()) 67192e3bfefSLemover } 672d0de7e4aSpeixiaokun val hptw = new Bundle { 673d0de7e4aSpeixiaokun val req = DecoupledIO(new Bundle{ 674eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 675d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 67697929664SXiaokun-Pei val gvpn = UInt(ptePPNLen.W) 677d0de7e4aSpeixiaokun }) 678d0de7e4aSpeixiaokun val resp = Flipped(Valid(new Bundle { 679d0de7e4aSpeixiaokun val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W)) 680d0de7e4aSpeixiaokun val h_resp = Output(new HptwResp) 681d0de7e4aSpeixiaokun })) 682d0de7e4aSpeixiaokun } 6838882eb68SXin Tian val bitmap = Option.when(HasBitmapCheck)(new Bundle { 6848882eb68SXin Tian val req = DecoupledIO(new bitmapReqBundle()) 6858882eb68SXin Tian val resp = Flipped(DecoupledIO(new bitmapRespBundle())) 6868882eb68SXin Tian }) 6878882eb68SXin Tian 6888882eb68SXin Tian val l0_way_info = Option.when(HasBitmapCheck)(Input(UInt(l2tlbParams.l0nWays.W))) 68992e3bfefSLemover} 69092e3bfefSLemover 69192e3bfefSLemoverclass LLPTWEntry(implicit p: Parameters) extends XSBundle with HasPtwConst { 69292e3bfefSLemover val req_info = new L2TlbInnerBundle() 69397929664SXiaokun-Pei val ppn = UInt(ptePPNLen.W) 69492e3bfefSLemover val wait_id = UInt(log2Up(l2tlbParams.llptwsize).W) 69592e3bfefSLemover val af = Bool() 696dc05c713Speixiaokun val hptw_resp = new HptwResp() 6976979864eSXiaokun-Pei val first_s2xlate_fault = Output(Bool()) 6988882eb68SXin Tian val cf = Bool() 6998882eb68SXin Tian val from_l0 = Bool() 7008882eb68SXin Tian val way_info = UInt(l2tlbParams.l0nWays.W) 7018882eb68SXin Tian val jmp_bitmap_check = Bool() 7028882eb68SXin Tian val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) 7038882eb68SXin Tian val cfs = Vec(tlbcontiguous, Bool()) 70492e3bfefSLemover} 70592e3bfefSLemover 70692e3bfefSLemover 70792e3bfefSLemoverclass LLPTW(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents { 70892e3bfefSLemover val io = IO(new LLPTWIO()) 70992e3bfefSLemover 7108882eb68SXin Tian // mbmc:bitmap csr 7118882eb68SXin Tian val mbmc = io.csr.mbmc 7128882eb68SXin Tian val bitmap_enable = (if (HasBitmapCheck) true.B else false.B) && mbmc.BME === 1.U && mbmc.CMODE === 0.U 7138882eb68SXin Tian 7145c5f442fSXiaokun-Pei val flush = io.sfence.valid || io.csr.satp.changed || io.csr.vsatp.changed || io.csr.hgatp.changed 71597929664SXiaokun-Pei val entries = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(0.U.asTypeOf(new LLPTWEntry())))) 7168882eb68SXin Tian 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 :: state_bitmap_check :: state_bitmap_resp :: Nil = Enum(12) 71792e3bfefSLemover val state = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(state_idle))) 7187797f035SbugGenerator 71992e3bfefSLemover val is_emptys = state.map(_ === state_idle) 72092e3bfefSLemover val is_mems = state.map(_ === state_mem_req) 72192e3bfefSLemover val is_waiting = state.map(_ === state_mem_waiting) 72292e3bfefSLemover val is_having = state.map(_ === state_mem_out) 7237797f035SbugGenerator val is_cache = state.map(_ === state_cache) 724d0de7e4aSpeixiaokun val is_hptw_req = state.map(_ === state_hptw_req) 725d0de7e4aSpeixiaokun val is_last_hptw_req = state.map(_ === state_last_hptw_req) 726b7bdb307Speixiaokun val is_hptw_resp = state.map(_ === state_hptw_resp) 727b7bdb307Speixiaokun val is_last_hptw_resp = state.map(_ === state_last_hptw_resp) 7288882eb68SXin Tian val is_bitmap_req = state.map(_ === state_bitmap_check) 7298882eb68SXin Tian val is_bitmap_resp = state.map(_ === state_bitmap_resp) 73092e3bfefSLemover 731935edac4STang Haojin val full = !ParallelOR(is_emptys).asBool 73292e3bfefSLemover val enq_ptr = ParallelPriorityEncoder(is_emptys) 73392e3bfefSLemover 7347797f035SbugGenerator val mem_ptr = ParallelPriorityEncoder(is_having) // TODO: optimize timing, bad: entries -> ptr -> entry 7357be7e781Speixiaokun val mem_arb = Module(new RRArbiterInit(new LLPTWEntry(), l2tlbParams.llptwsize)) 73692e3bfefSLemover for (i <- 0 until l2tlbParams.llptwsize) { 73792e3bfefSLemover mem_arb.io.in(i).bits := entries(i) 73892e3bfefSLemover mem_arb.io.in(i).valid := is_mems(i) && !io.mem.req_mask(i) 73992e3bfefSLemover } 7402a1f48e7Speixiaokun 7412a1f48e7Speixiaokun // process hptw requests in serial 7427be7e781Speixiaokun val hyper_arb1 = Module(new RRArbiterInit(new LLPTWEntry(), l2tlbParams.llptwsize)) 743d0de7e4aSpeixiaokun for (i <- 0 until l2tlbParams.llptwsize) { 744d0de7e4aSpeixiaokun hyper_arb1.io.in(i).bits := entries(i) 7452a1f48e7Speixiaokun hyper_arb1.io.in(i).valid := is_hptw_req(i) && !(Cat(is_hptw_resp).orR) && !(Cat(is_last_hptw_resp).orR) 746d0de7e4aSpeixiaokun } 7477be7e781Speixiaokun val hyper_arb2 = Module(new RRArbiterInit(new LLPTWEntry(), l2tlbParams.llptwsize)) 748d0de7e4aSpeixiaokun for(i <- 0 until l2tlbParams.llptwsize) { 749d0de7e4aSpeixiaokun hyper_arb2.io.in(i).bits := entries(i) 7502a1f48e7Speixiaokun hyper_arb2.io.in(i).valid := is_last_hptw_req(i) && !(Cat(is_hptw_resp).orR) && !(Cat(is_last_hptw_resp).orR) 751d0de7e4aSpeixiaokun } 75292e3bfefSLemover 7538882eb68SXin Tian 7548882eb68SXin Tian val bitmap_arb = Option.when(HasBitmapCheck)(Module(new RRArbiter(new bitmapReqBundle(), l2tlbParams.llptwsize))) 7558882eb68SXin Tian val way_info = Option.when(HasBitmapCheck)(Wire(Vec(l2tlbParams.llptwsize, UInt(l2tlbParams.l0nWays.W)))) 7568882eb68SXin Tian if (HasBitmapCheck) { 7578882eb68SXin Tian for (i <- 0 until l2tlbParams.llptwsize) { 7588882eb68SXin Tian bitmap_arb.get.io.in(i).valid := is_bitmap_req(i) 7598882eb68SXin Tian bitmap_arb.get.io.in(i).bits.bmppn := entries(i).ppn 7608882eb68SXin Tian bitmap_arb.get.io.in(i).bits.vpn := entries(i).req_info.vpn 7618882eb68SXin Tian bitmap_arb.get.io.in(i).bits.id := i.U 7628882eb68SXin Tian bitmap_arb.get.io.in(i).bits.level := 0.U // last level 7638882eb68SXin Tian bitmap_arb.get.io.in(i).bits.way_info := Mux(entries(i).from_l0, entries(i).way_info, way_info.get(i)) 7648882eb68SXin Tian bitmap_arb.get.io.in(i).bits.hptw_bypassed := false.B 7658882eb68SXin Tian } 7668882eb68SXin Tian } 7678882eb68SXin Tian 768f3034303SHaoyuan Feng val cache_ptr = ParallelMux(is_cache, (0 until l2tlbParams.llptwsize).map(_.U(log2Up(l2tlbParams.llptwsize).W))) 7697797f035SbugGenerator 77092e3bfefSLemover // duplicate req 77192e3bfefSLemover // to_wait: wait for the last to access mem, set to mem_resp 77292e3bfefSLemover // to_cache: the last is back just right now, set to mem_cache 77392e3bfefSLemover val dup_vec = state.indices.map(i => 774cca17e78Speixiaokun dup(io.in.bits.req_info.vpn, entries(i).req_info.vpn) && io.in.bits.req_info.s2xlate === entries(i).req_info.s2xlate 77592e3bfefSLemover ) 776cca17e78Speixiaokun 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 7776979864eSXiaokun-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 77892e3bfefSLemover 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 7798882eb68SXin Tian val dup_vec_bitmap = dup_vec.zipWithIndex.map{case (d, i) => d && (is_bitmap_req(i) || is_bitmap_resp(i))} 780951f37e5Speixiaokun val dup_vec_last_hptw = dup_vec.zipWithIndex.map{case (d, i) => d && (is_last_hptw_req(i) || is_last_hptw_resp(i))} 78192e3bfefSLemover val wait_id = Mux(dup_req_fire, mem_arb.io.chosen, ParallelMux(dup_vec_wait zip entries.map(_.wait_id))) 78297929664SXiaokun-Pei val dup_wait_resp = io.mem.resp.fire && VecInit(dup_vec_wait)(io.mem.resp.bits.id) && !io.mem.flush_latch(io.mem.resp.bits.id) // dup with the entry that data coming next cycle 78392e3bfefSLemover val to_wait = Cat(dup_vec_wait).orR || dup_req_fire 784*57a8ca5eSHaoyuan Feng 7859467c5f4Speixiaokun val last_hptw_req_id = io.mem.resp.bits.id 7864c0e0181SXiaokun-Pei val req_paddr = MakeAddr(io.in.bits.ppn(ppnLen-1, 0), getVpnn(io.in.bits.req_info.vpn, 0)) 7879467c5f4Speixiaokun val req_hpaddr = MakeAddr(entries(last_hptw_req_id).hptw_resp.genPPNS2(get_pn(req_paddr)), getVpnn(io.in.bits.req_info.vpn, 0)) 7889467c5f4Speixiaokun val index = Mux(entries(last_hptw_req_id).req_info.s2xlate === allStage, req_hpaddr, req_paddr)(log2Up(l2tlbParams.blockBytes)-1, log2Up(XLEN/8)) 789*57a8ca5eSHaoyuan Feng val last_hptw_req_pte = io.mem.resp.bits.value.asTypeOf(Vec(blockBits / XLEN, new PteBundle()))(index) 790*57a8ca5eSHaoyuan Feng val last_hptw_req_ppn = Mux(last_hptw_req_pte.n === 0.U, last_hptw_req_pte.getPPN(), Cat(last_hptw_req_pte.getPPN()(ptePPNLen - 1, pteNapotBits), io.in.bits.req_info.vpn(pteNapotBits - 1, 0))) 791*57a8ca5eSHaoyuan Feng // in `to_last_hptw_req`, we have already judged whether s2xlate === allStage 792*57a8ca5eSHaoyuan Feng val last_hptw_vsStagePf = last_hptw_req_pte.isPf(0.U, io.csr.hPBMTE) || !last_hptw_req_pte.isLeaf() 793*57a8ca5eSHaoyuan Feng val last_hptw_gStagePf = last_hptw_req_pte.isStage1Gpf(io.csr.hgatp.mode) && !last_hptw_vsStagePf 794*57a8ca5eSHaoyuan Feng 795*57a8ca5eSHaoyuan Feng // noS2xlate || onlyStage1 || allStage but exception; do not need Stage2 translate 796*57a8ca5eSHaoyuan Feng val noStage2 = ((entries(io.mem.resp.bits.id).req_info.s2xlate === noS2xlate) || (entries(io.mem.resp.bits.id).req_info.s2xlate === onlyStage1)) || 797*57a8ca5eSHaoyuan Feng (entries(io.mem.resp.bits.id).req_info.s2xlate === allStage && (last_hptw_vsStagePf || last_hptw_gStagePf)) 798*57a8ca5eSHaoyuan Feng val to_mem_out = dup_wait_resp && noStage2 && !bitmap_enable 799*57a8ca5eSHaoyuan Feng val to_bitmap_req = (if (HasBitmapCheck) true.B else false.B) && dup_wait_resp && noStage2 && bitmap_enable 800*57a8ca5eSHaoyuan Feng val to_cache = if (HasBitmapCheck) Cat(dup_vec_bitmap).orR || Cat(dup_vec_having).orR || Cat(dup_vec_last_hptw).orR 801*57a8ca5eSHaoyuan Feng else Cat(dup_vec_having).orR || Cat(dup_vec_last_hptw).orR 802*57a8ca5eSHaoyuan Feng val to_hptw_req = io.in.bits.req_info.s2xlate === allStage 803*57a8ca5eSHaoyuan Feng val to_last_hptw_req = dup_wait_resp && entries(io.mem.resp.bits.id).req_info.s2xlate === allStage && !(last_hptw_vsStagePf || last_hptw_gStagePf) 804*57a8ca5eSHaoyuan Feng val last_hptw_excp = dup_wait_resp && entries(io.mem.resp.bits.id).req_info.s2xlate === allStage && (last_hptw_vsStagePf || last_hptw_gStagePf) 805*57a8ca5eSHaoyuan Feng 8067797f035SbugGenerator XSError(RegNext(dup_req_fire && Cat(dup_vec_wait).orR, init = false.B), "mem req but some entries already waiting, should not happed") 80792e3bfefSLemover 808935edac4STang Haojin XSError(io.in.fire && ((to_mem_out && to_cache) || (to_wait && to_cache)), "llptw enq, to cache conflict with to mem") 80992e3bfefSLemover val mem_resp_hit = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(false.B))) 8107274ec5cSpeixiaokun val enq_state_normal = MuxCase(state_addr_check, Seq( 8117274ec5cSpeixiaokun to_mem_out -> state_mem_out, // same to the blew, but the mem resp now 8128882eb68SXin Tian to_bitmap_req -> state_bitmap_check, 813871d1438Speixiaokun to_last_hptw_req -> state_last_hptw_req, 8147274ec5cSpeixiaokun to_wait -> state_mem_waiting, 8157274ec5cSpeixiaokun to_cache -> state_cache, 816871d1438Speixiaokun to_hptw_req -> state_hptw_req 8177274ec5cSpeixiaokun )) 8187797f035SbugGenerator val enq_state = Mux(from_pre(io.in.bits.req_info.source) && enq_state_normal =/= state_addr_check, state_idle, enq_state_normal) 8198882eb68SXin Tian when (io.in.fire && (if (HasBitmapCheck) !io.in.bits.bitmapCheck.get.jmp_bitmap_check else true.B)) { 82092e3bfefSLemover // if prefetch req does not need mem access, just give it up. 82192e3bfefSLemover // so there will be at most 1 + FilterSize entries that needs re-access page cache 82292e3bfefSLemover // so 2 + FilterSize is enough to avoid dead-lock 8237797f035SbugGenerator state(enq_ptr) := enq_state 82492e3bfefSLemover entries(enq_ptr).req_info := io.in.bits.req_info 825*57a8ca5eSHaoyuan Feng entries(enq_ptr).ppn := Mux(to_last_hptw_req || last_hptw_excp, last_hptw_req_ppn, io.in.bits.ppn) 82692e3bfefSLemover entries(enq_ptr).wait_id := Mux(to_wait, wait_id, enq_ptr) 82792e3bfefSLemover entries(enq_ptr).af := false.B 8288882eb68SXin Tian if (HasBitmapCheck) { 8298882eb68SXin Tian entries(enq_ptr).cf := false.B 8308882eb68SXin Tian entries(enq_ptr).from_l0 := false.B 8318882eb68SXin Tian entries(enq_ptr).way_info := 0.U 8328882eb68SXin Tian entries(enq_ptr).jmp_bitmap_check := false.B 8338882eb68SXin Tian for (i <- 0 until tlbcontiguous) { 8348882eb68SXin Tian entries(enq_ptr).ptes(i) := 0.U 8358882eb68SXin Tian } 8368882eb68SXin Tian entries(enq_ptr).cfs := io.in.bits.bitmapCheck.get.cfs 8378882eb68SXin Tian } 8382a1f48e7Speixiaokun 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)) 839*57a8ca5eSHaoyuan Feng entries(enq_ptr).hptw_resp.gpf := Mux(last_hptw_excp, last_hptw_gStagePf, false.B) 8406979864eSXiaokun-Pei entries(enq_ptr).first_s2xlate_fault := false.B 8418882eb68SXin Tian mem_resp_hit(enq_ptr) := to_bitmap_req || to_mem_out || to_last_hptw_req 8428882eb68SXin Tian } 8438882eb68SXin Tian 8448882eb68SXin Tian if (HasBitmapCheck) { 8458882eb68SXin Tian when (io.in.bits.bitmapCheck.get.jmp_bitmap_check && io.in.fire) { 8468882eb68SXin Tian state(enq_ptr) := state_bitmap_check 8478882eb68SXin Tian entries(enq_ptr).req_info := io.in.bits.req_info 8488882eb68SXin Tian entries(enq_ptr).ppn := io.in.bits.bitmapCheck.get.ptes(io.in.bits.req_info.vpn(sectortlbwidth - 1, 0)).asTypeOf(new PteBundle().cloneType).ppn 8498882eb68SXin Tian entries(enq_ptr).wait_id := enq_ptr 8508882eb68SXin Tian entries(enq_ptr).af := false.B 8518882eb68SXin Tian entries(enq_ptr).cf := false.B 8528882eb68SXin Tian entries(enq_ptr).from_l0 := true.B 8538882eb68SXin Tian entries(enq_ptr).way_info := io.in.bits.bitmapCheck.get.hitway 8548882eb68SXin Tian entries(enq_ptr).jmp_bitmap_check := io.in.bits.bitmapCheck.get.jmp_bitmap_check 8558882eb68SXin Tian entries(enq_ptr).ptes := io.in.bits.bitmapCheck.get.ptes 8568882eb68SXin Tian entries(enq_ptr).cfs := io.in.bits.bitmapCheck.get.cfs 8578882eb68SXin Tian mem_resp_hit(enq_ptr) := false.B 8588882eb68SXin Tian } 85992e3bfefSLemover } 8607797f035SbugGenerator 8617797f035SbugGenerator val enq_ptr_reg = RegNext(enq_ptr) 8628882eb68SXin Tian val need_addr_check = GatedValidRegNext(enq_state === state_addr_check && io.in.fire && !flush && (if (HasBitmapCheck) !io.in.bits.bitmapCheck.get.jmp_bitmap_check else true.B)) 8637274ec5cSpeixiaokun 8640214776eSpeixiaokun val hasHptwResp = ParallelOR(state.map(_ === state_hptw_resp)).asBool 8657274ec5cSpeixiaokun val hptw_resp_ptr_reg = RegNext(io.hptw.resp.bits.id) 866a664078aSpeixiaokun val hptw_need_addr_check = RegNext(hasHptwResp && io.hptw.resp.fire && !flush) && state(hptw_resp_ptr_reg) === state_addr_check 867d0de7e4aSpeixiaokun 868ce5f4200SGuanghui Hu val ptes = io.mem.resp.bits.value.asTypeOf(Vec(blockBits / XLEN, new PteBundle())) 8693211121aSXiaokun-Pei val gpaddr = MakeGPAddr(entries(hptw_resp_ptr_reg).ppn, getVpnn(entries(hptw_resp_ptr_reg).req_info.vpn, 0)) 87082e4705bSpeixiaokun val hptw_resp = entries(hptw_resp_ptr_reg).hptw_resp 871cda84113Speixiaokun val hpaddr = Cat(hptw_resp.genPPNS2(get_pn(gpaddr)), get_off(gpaddr)) 8724c0e0181SXiaokun-Pei val addr = RegEnable(MakeAddr(io.in.bits.ppn(ppnLen - 1, 0), getVpnn(io.in.bits.req_info.vpn, 0)), io.in.fire) 8737274ec5cSpeixiaokun io.pmp.req.valid := need_addr_check || hptw_need_addr_check 87482e4705bSpeixiaokun io.pmp.req.bits.addr := Mux(hptw_need_addr_check, hpaddr, addr) 8757797f035SbugGenerator io.pmp.req.bits.cmd := TlbCmd.read 8767797f035SbugGenerator io.pmp.req.bits.size := 3.U // TODO: fix it 8777797f035SbugGenerator val pmp_resp_valid = io.pmp.req.valid // same cycle 8787797f035SbugGenerator when (pmp_resp_valid) { 8797797f035SbugGenerator // NOTE: when pmp resp but state is not addr check, then the entry is dup with other entry, the state was changed before 8807797f035SbugGenerator // when dup with the req-ing entry, set to mem_waiting (above codes), and the ld must be false, so dontcare 8817274ec5cSpeixiaokun val ptr = Mux(hptw_need_addr_check, hptw_resp_ptr_reg, enq_ptr_reg); 8827797f035SbugGenerator val accessFault = io.pmp.resp.ld || io.pmp.resp.mmio 8837274ec5cSpeixiaokun entries(ptr).af := accessFault 8847274ec5cSpeixiaokun state(ptr) := Mux(accessFault, state_mem_out, state_mem_req) 8857797f035SbugGenerator } 8867797f035SbugGenerator 887935edac4STang Haojin when (mem_arb.io.out.fire) { 88892e3bfefSLemover for (i <- state.indices) { 889ec78ed87Speixiaokun when (state(i) =/= state_idle && state(i) =/= state_mem_out && state(i) =/= state_last_hptw_req && state(i) =/= state_last_hptw_resp 8908882eb68SXin Tian && (if (HasBitmapCheck) state(i) =/= state_bitmap_check && state(i) =/= state_bitmap_resp else true.B) 891ec78ed87Speixiaokun && entries(i).req_info.s2xlate === mem_arb.io.out.bits.req_info.s2xlate 892ec78ed87Speixiaokun && dup(entries(i).req_info.vpn, mem_arb.io.out.bits.req_info.vpn)) { 89392e3bfefSLemover // NOTE: "dup enq set state to mem_wait" -> "sending req set other dup entries to mem_wait" 89492e3bfefSLemover state(i) := state_mem_waiting 8952a1f48e7Speixiaokun entries(i).hptw_resp := entries(mem_arb.io.chosen).hptw_resp 89692e3bfefSLemover entries(i).wait_id := mem_arb.io.chosen 89792e3bfefSLemover } 89892e3bfefSLemover } 89992e3bfefSLemover } 900935edac4STang Haojin when (io.mem.resp.fire) { 90192e3bfefSLemover state.indices.map{i => 90292e3bfefSLemover when (state(i) === state_mem_waiting && io.mem.resp.bits.id === entries(i).wait_id) { 9034358f287Speixiaokun val req_paddr = MakeAddr(entries(i).ppn, getVpnn(entries(i).req_info.vpn, 0)) 9044358f287Speixiaokun val req_hpaddr = MakeAddr(entries(i).hptw_resp.genPPNS2(get_pn(req_paddr)), getVpnn(entries(i).req_info.vpn, 0)) 9054358f287Speixiaokun val index = Mux(entries(i).req_info.s2xlate === allStage, req_hpaddr, req_paddr)(log2Up(l2tlbParams.blockBytes)-1, log2Up(XLEN/8)) 906e7412eb4SHaoyuan Feng val enableS2xlate = entries(i).req_info.s2xlate =/= noS2xlate 907e7412eb4SHaoyuan Feng val s1Pbmte = Mux(enableS2xlate, io.csr.hPBMTE, io.csr.mPBMTE) 9088c9da034SHaoyuan Feng val vsStagePf = ptes(index).isPf(0.U, s1Pbmte) || !ptes(index).isLeaf() // Pagefault in vs-Stage 9098c9da034SHaoyuan Feng // Pagefault in g-Stage; when vsStagePf valid, should not check gStagepf 9108c9da034SHaoyuan Feng val gStagePf = ptes(index).isStage1Gpf(io.csr.hgatp.mode) && !vsStagePf 9118c9da034SHaoyuan Feng state(i) := Mux(entries(i).req_info.s2xlate === allStage && !(vsStagePf || gStagePf), 912e5429136SHaoyuan Feng state_last_hptw_req, 913e5429136SHaoyuan Feng Mux(bitmap_enable, state_bitmap_check, state_mem_out)) 914cf41a6eeSpeixiaokun mem_resp_hit(i) := true.B 915e65b7d6bSHaoyuan Feng entries(i).ppn := Mux(ptes(index).n === 0.U, ptes(index).getPPN(), Cat(ptes(index).getPPN()(ptePPNLen - 1, pteNapotBits), entries(i).req_info.vpn(pteNapotBits - 1, 0))) // for last stage 2 translation 916e5429136SHaoyuan Feng // af will be judged in L2 TLB `contiguous_pte_to_merge_ptwResp` 9178c9da034SHaoyuan Feng entries(i).hptw_resp.gpf := Mux(entries(i).req_info.s2xlate === allStage, gStagePf, false.B) 918ad0d9d89Speixiaokun } 919ad0d9d89Speixiaokun } 920ad0d9d89Speixiaokun } 921ad0d9d89Speixiaokun 9228882eb68SXin Tian if (HasBitmapCheck) { 9238882eb68SXin Tian for (i <- 0 until l2tlbParams.llptwsize) { 9248882eb68SXin Tian way_info.get(i) := DataHoldBypass(io.l0_way_info.get, mem_resp_hit(i)) 9258882eb68SXin Tian } 9268882eb68SXin Tian } 9278882eb68SXin Tian 9283222d00fSpeixiaokun when (hyper_arb1.io.out.fire) { 929d0de7e4aSpeixiaokun for (i <- state.indices) { 9306b742a19SXiaokun-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) { 931d0de7e4aSpeixiaokun state(i) := state_hptw_resp 932d0de7e4aSpeixiaokun entries(i).wait_id := hyper_arb1.io.chosen 933d0de7e4aSpeixiaokun } 934d0de7e4aSpeixiaokun } 935d0de7e4aSpeixiaokun } 936d0de7e4aSpeixiaokun 9373222d00fSpeixiaokun when (hyper_arb2.io.out.fire) { 938d0de7e4aSpeixiaokun for (i <- state.indices) { 9396b742a19SXiaokun-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) { 940d0de7e4aSpeixiaokun state(i) := state_last_hptw_resp 941d0de7e4aSpeixiaokun entries(i).wait_id := hyper_arb2.io.chosen 942d0de7e4aSpeixiaokun } 943d0de7e4aSpeixiaokun } 944d0de7e4aSpeixiaokun } 945d0de7e4aSpeixiaokun 9468882eb68SXin Tian if (HasBitmapCheck) { 9478882eb68SXin Tian when (bitmap_arb.get.io.out.fire) { 9488882eb68SXin Tian for (i <- state.indices) { 9498882eb68SXin Tian when (is_bitmap_req(i) && bitmap_arb.get.io.out.bits.bmppn === entries(i).ppn(ppnLen - 1, 0)) { 9508882eb68SXin Tian state(i) := state_bitmap_resp 9518882eb68SXin Tian entries(i).wait_id := bitmap_arb.get.io.chosen 9528882eb68SXin Tian } 9538882eb68SXin Tian } 9548882eb68SXin Tian } 9558882eb68SXin Tian 9568882eb68SXin Tian when (io.bitmap.get.resp.fire) { 9578882eb68SXin Tian for (i <- state.indices) { 9588882eb68SXin Tian when (is_bitmap_resp(i) && io.bitmap.get.resp.bits.id === entries(i).wait_id) { 9598882eb68SXin Tian entries(i).cfs := io.bitmap.get.resp.bits.cfs 9608882eb68SXin Tian entries(i).cf := io.bitmap.get.resp.bits.cf 9618882eb68SXin Tian state(i) := state_mem_out 9628882eb68SXin Tian } 9638882eb68SXin Tian } 9648882eb68SXin Tian } 9658882eb68SXin Tian } 9668882eb68SXin Tian 9673222d00fSpeixiaokun when (io.hptw.resp.fire) { 968d0de7e4aSpeixiaokun for (i <- state.indices) { 9692a1f48e7Speixiaokun 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) { 970903ff891SXiaokun-Pei val check_g_perm_fail = !io.hptw.resp.bits.h_resp.gaf && (!io.hptw.resp.bits.h_resp.entry.perm.get.r && !(io.csr.priv.mxr && io.hptw.resp.bits.h_resp.entry.perm.get.x)) 971fffcb38cSXiaokun-Pei when (check_g_perm_fail || io.hptw.resp.bits.h_resp.gaf || io.hptw.resp.bits.h_resp.gpf) { 97269f13e85SXiaokun-Pei state(i) := state_mem_out 97369f13e85SXiaokun-Pei entries(i).hptw_resp := io.hptw.resp.bits.h_resp 974fffcb38cSXiaokun-Pei entries(i).hptw_resp.gpf := io.hptw.resp.bits.h_resp.gpf || check_g_perm_fail 9756979864eSXiaokun-Pei entries(i).first_s2xlate_fault := io.hptw.resp.bits.h_resp.gaf || io.hptw.resp.bits.h_resp.gpf 97669f13e85SXiaokun-Pei }.otherwise{ // change the entry that is waiting hptw resp 97796b05afaSHaoyuan Feng val need_to_waiting_vec = state.indices.map(i => state(i) === state_mem_waiting && 97896b05afaSHaoyuan Feng dup(entries(i).req_info.vpn, entries(io.hptw.resp.bits.id).req_info.vpn) && 97996b05afaSHaoyuan Feng entries(i).req_info.s2xlate === entries(io.hptw.resp.bits.id).req_info.s2xlate) 9807f96e195Speixiaokun val waiting_index = ParallelMux(need_to_waiting_vec zip entries.map(_.wait_id)) 9817f96e195Speixiaokun state(i) := Mux(Cat(need_to_waiting_vec).orR, state_mem_waiting, state_addr_check) 982dc05c713Speixiaokun entries(i).hptw_resp := io.hptw.resp.bits.h_resp 9837f96e195Speixiaokun entries(i).wait_id := Mux(Cat(need_to_waiting_vec).orR, waiting_index, entries(i).wait_id) 9842a1f48e7Speixiaokun //To do: change the entry that is having the same hptw req 985d0de7e4aSpeixiaokun } 98669f13e85SXiaokun-Pei } 9872a1f48e7Speixiaokun 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) { 988d0de7e4aSpeixiaokun state(i) := state_mem_out 989dc05c713Speixiaokun entries(i).hptw_resp := io.hptw.resp.bits.h_resp 9902a1f48e7Speixiaokun //To do: change the entry that is having the same hptw req 991d0de7e4aSpeixiaokun } 992d0de7e4aSpeixiaokun } 993d0de7e4aSpeixiaokun } 994935edac4STang Haojin when (io.out.fire) { 99592e3bfefSLemover assert(state(mem_ptr) === state_mem_out) 99692e3bfefSLemover state(mem_ptr) := state_idle 99792e3bfefSLemover } 99892e3bfefSLemover mem_resp_hit.map(a => when (a) { a := false.B } ) 99992e3bfefSLemover 10007797f035SbugGenerator when (io.cache.fire) { 10017797f035SbugGenerator state(cache_ptr) := state_idle 100292e3bfefSLemover } 10037797f035SbugGenerator XSError(io.out.fire && io.cache.fire && (mem_ptr === cache_ptr), "mem resp and cache fire at the same time at same entry") 100492e3bfefSLemover 100592e3bfefSLemover when (flush) { 100692e3bfefSLemover state.map(_ := state_idle) 100792e3bfefSLemover } 100892e3bfefSLemover 100992e3bfefSLemover io.in.ready := !full 101092e3bfefSLemover 1011935edac4STang Haojin io.out.valid := ParallelOR(is_having).asBool 101292e3bfefSLemover io.out.bits.req_info := entries(mem_ptr).req_info 101392e3bfefSLemover io.out.bits.id := mem_ptr 10148882eb68SXin Tian if (HasBitmapCheck) { 10158882eb68SXin Tian io.out.bits.af := Mux(bitmap_enable, entries(mem_ptr).af || entries(mem_ptr).cf, entries(mem_ptr).af) 10168882eb68SXin Tian io.out.bits.bitmapCheck.get.jmp_bitmap_check := entries(mem_ptr).jmp_bitmap_check 10178882eb68SXin Tian io.out.bits.bitmapCheck.get.ptes := entries(mem_ptr).ptes 10188882eb68SXin Tian io.out.bits.bitmapCheck.get.cfs := entries(mem_ptr).cfs 10198882eb68SXin Tian } else { 102092e3bfefSLemover io.out.bits.af := entries(mem_ptr).af 10218882eb68SXin Tian } 10228882eb68SXin Tian 1023dc05c713Speixiaokun io.out.bits.h_resp := entries(mem_ptr).hptw_resp 10246979864eSXiaokun-Pei io.out.bits.first_s2xlate_fault := entries(mem_ptr).first_s2xlate_fault 1025d0de7e4aSpeixiaokun 102683d93d53Speixiaokun val hptw_req_arb = Module(new Arbiter(new Bundle{ 102783d93d53Speixiaokun val source = UInt(bSourceWidth.W) 102883d93d53Speixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 102997929664SXiaokun-Pei val ppn = UInt(ptePPNLen.W) 103083d93d53Speixiaokun } , 2)) 103183d93d53Speixiaokun // first stage 2 translation 103283d93d53Speixiaokun hptw_req_arb.io.in(0).valid := hyper_arb1.io.out.valid 103383d93d53Speixiaokun hptw_req_arb.io.in(0).bits.source := hyper_arb1.io.out.bits.req_info.source 103483d93d53Speixiaokun hptw_req_arb.io.in(0).bits.ppn := hyper_arb1.io.out.bits.ppn 103583d93d53Speixiaokun hptw_req_arb.io.in(0).bits.id := hyper_arb1.io.chosen 10362a1f48e7Speixiaokun hyper_arb1.io.out.ready := hptw_req_arb.io.in(0).ready 103783d93d53Speixiaokun // last stage 2 translation 103883d93d53Speixiaokun hptw_req_arb.io.in(1).valid := hyper_arb2.io.out.valid 103983d93d53Speixiaokun hptw_req_arb.io.in(1).bits.source := hyper_arb2.io.out.bits.req_info.source 104083d93d53Speixiaokun hptw_req_arb.io.in(1).bits.ppn := hyper_arb2.io.out.bits.ppn 104183d93d53Speixiaokun hptw_req_arb.io.in(1).bits.id := hyper_arb2.io.chosen 10422a1f48e7Speixiaokun hyper_arb2.io.out.ready := hptw_req_arb.io.in(1).ready 104383d93d53Speixiaokun hptw_req_arb.io.out.ready := io.hptw.req.ready 10442a1f48e7Speixiaokun io.hptw.req.valid := hptw_req_arb.io.out.fire && !flush 104583d93d53Speixiaokun io.hptw.req.bits.gvpn := hptw_req_arb.io.out.bits.ppn 104683d93d53Speixiaokun io.hptw.req.bits.id := hptw_req_arb.io.out.bits.id 104783d93d53Speixiaokun io.hptw.req.bits.source := hptw_req_arb.io.out.bits.source 104892e3bfefSLemover 104992e3bfefSLemover io.mem.req.valid := mem_arb.io.out.valid && !flush 1050dc05c713Speixiaokun val mem_paddr = MakeAddr(mem_arb.io.out.bits.ppn, getVpnn(mem_arb.io.out.bits.req_info.vpn, 0)) 1051cda84113Speixiaokun 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)) 10526b742a19SXiaokun-Pei io.mem.req.bits.addr := Mux(mem_arb.io.out.bits.req_info.s2xlate === allStage, mem_hpaddr, mem_paddr) 105392e3bfefSLemover io.mem.req.bits.id := mem_arb.io.chosen 105483d93d53Speixiaokun io.mem.req.bits.hptw_bypassed := false.B 105592e3bfefSLemover mem_arb.io.out.ready := io.mem.req.ready 1056933ec998Speixiaokun val mem_refill_id = RegNext(io.mem.resp.bits.id(log2Up(l2tlbParams.llptwsize)-1, 0)) 1057933ec998Speixiaokun io.mem.refill := entries(mem_refill_id).req_info 10584ed5afbdSXiaokun-Pei io.mem.refill.s2xlate := entries(mem_refill_id).req_info.s2xlate 105992e3bfefSLemover io.mem.buffer_it := mem_resp_hit 106092e3bfefSLemover io.mem.enq_ptr := enq_ptr 106192e3bfefSLemover 10627797f035SbugGenerator io.cache.valid := Cat(is_cache).orR 10637797f035SbugGenerator io.cache.bits := ParallelMux(is_cache, entries.map(_.req_info)) 10647797f035SbugGenerator 10658882eb68SXin Tian val has_bitmap_resp = ParallelOR(is_bitmap_resp).asBool 10668882eb68SXin Tian if (HasBitmapCheck) { 10678882eb68SXin Tian io.bitmap.get.req.valid := bitmap_arb.get.io.out.valid && !flush 10688882eb68SXin Tian io.bitmap.get.req.bits.bmppn := bitmap_arb.get.io.out.bits.bmppn 10698882eb68SXin Tian io.bitmap.get.req.bits.id := bitmap_arb.get.io.chosen 10708882eb68SXin Tian io.bitmap.get.req.bits.vpn := bitmap_arb.get.io.out.bits.vpn 10718882eb68SXin Tian io.bitmap.get.req.bits.level := 0.U 10728882eb68SXin Tian io.bitmap.get.req.bits.way_info := bitmap_arb.get.io.out.bits.way_info 10738882eb68SXin Tian io.bitmap.get.req.bits.hptw_bypassed := bitmap_arb.get.io.out.bits.hptw_bypassed 10748882eb68SXin Tian bitmap_arb.get.io.out.ready := io.bitmap.get.req.ready 10758882eb68SXin Tian io.bitmap.get.resp.ready := has_bitmap_resp 10768882eb68SXin Tian } 10778882eb68SXin Tian 1078935edac4STang Haojin XSPerfAccumulate("llptw_in_count", io.in.fire) 107992e3bfefSLemover XSPerfAccumulate("llptw_in_block", io.in.valid && !io.in.ready) 108092e3bfefSLemover for (i <- 0 until 7) { 1081935edac4STang Haojin XSPerfAccumulate(s"enq_state${i}", io.in.fire && enq_state === i.U) 108292e3bfefSLemover } 108392e3bfefSLemover for (i <- 0 until (l2tlbParams.llptwsize + 1)) { 108492e3bfefSLemover XSPerfAccumulate(s"util${i}", PopCount(is_emptys.map(!_)) === i.U) 108592e3bfefSLemover XSPerfAccumulate(s"mem_util${i}", PopCount(is_mems) === i.U) 108692e3bfefSLemover XSPerfAccumulate(s"waiting_util${i}", PopCount(is_waiting) === i.U) 108792e3bfefSLemover } 1088935edac4STang Haojin XSPerfAccumulate("mem_count", io.mem.req.fire) 108992e3bfefSLemover XSPerfAccumulate("mem_cycle", PopCount(is_waiting) =/= 0.U) 109092e3bfefSLemover XSPerfAccumulate("blocked_in", io.in.valid && !io.in.ready) 109192e3bfefSLemover 109292e3bfefSLemover val perfEvents = Seq( 1093935edac4STang Haojin ("tlbllptw_incount ", io.in.fire ), 109492e3bfefSLemover ("tlbllptw_inblock ", io.in.valid && !io.in.ready), 1095935edac4STang Haojin ("tlbllptw_memcount ", io.mem.req.fire ), 109692e3bfefSLemover ("tlbllptw_memcycle ", PopCount(is_waiting) ), 109792e3bfefSLemover ) 109892e3bfefSLemover generatePerfEvent() 109992e3bfefSLemover} 1100d0de7e4aSpeixiaokun 1101d0de7e4aSpeixiaokun/*========================= HPTW ==============================*/ 1102d0de7e4aSpeixiaokun 1103d0de7e4aSpeixiaokun/** HPTW : Hypervisor Page Table Walker 1104d0de7e4aSpeixiaokun * the page walker take the virtual machine's page walk. 1105d0de7e4aSpeixiaokun * guest physical address translation, guest physical address -> host physical address 1106d0de7e4aSpeixiaokun **/ 1107d0de7e4aSpeixiaokunclass HPTWIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 1108d0de7e4aSpeixiaokun val req = Flipped(DecoupledIO(new Bundle { 1109eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 1110d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 111197929664SXiaokun-Pei val gvpn = UInt(gvpnLen.W) 11126315ba2aSpeixiaokun val ppn = UInt(ppnLen.W) 11133ea4388cSHaoyuan Feng val l3Hit = if (EnableSv48) Some(new Bool()) else None 1114d0de7e4aSpeixiaokun val l2Hit = Bool() 11153ea4388cSHaoyuan Feng val l1Hit = Bool() 111683d93d53Speixiaokun val bypassed = Bool() // if bypass, don't refill 11178882eb68SXin Tian val bitmapCheck = Option.when(HasBitmapCheck)(new Bundle { 11188882eb68SXin Tian val jmp_bitmap_check = Bool() // find pte in l0 or sp, but need bitmap check 11198882eb68SXin Tian val pte = UInt(XLEN.W) // Page Table Entry 11208882eb68SXin Tian val ptes = Vec(tlbcontiguous, UInt(XLEN.W)) // Page Table Entry Vector 11218882eb68SXin Tian val cfs = Vec(tlbcontiguous, Bool()) // Bitmap Check Failed Vector 11228882eb68SXin Tian val hitway = UInt(l2tlbParams.l0nWays.W) 11238882eb68SXin Tian val fromSP = Bool() 11248882eb68SXin Tian val SPlevel = UInt(log2Up(Level).W) 11258882eb68SXin Tian }) 1126d0de7e4aSpeixiaokun })) 1127c2b430edSpeixiaokun val resp = DecoupledIO(new Bundle { 1128eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 1129d0de7e4aSpeixiaokun val resp = Output(new HptwResp()) 1130d0de7e4aSpeixiaokun val id = Output(UInt(bMemID.W)) 1131d0de7e4aSpeixiaokun }) 1132d0de7e4aSpeixiaokun 1133d0de7e4aSpeixiaokun val mem = new Bundle { 1134d0de7e4aSpeixiaokun val req = DecoupledIO(new L2TlbMemReqBundle()) 1135d0de7e4aSpeixiaokun val resp = Flipped(ValidIO(UInt(XLEN.W))) 1136d0de7e4aSpeixiaokun val mask = Input(Bool()) 1137d0de7e4aSpeixiaokun } 1138d0de7e4aSpeixiaokun val refill = Output(new Bundle { 1139d0de7e4aSpeixiaokun val req_info = new L2TlbInnerBundle() 11403ea4388cSHaoyuan Feng val level = UInt(log2Up(Level + 1).W) 1141d0de7e4aSpeixiaokun }) 1142d0de7e4aSpeixiaokun val pmp = new Bundle { 1143d0de7e4aSpeixiaokun val req = ValidIO(new PMPReqBundle()) 1144d0de7e4aSpeixiaokun val resp = Flipped(new PMPRespBundle()) 1145d0de7e4aSpeixiaokun } 11468882eb68SXin Tian val bitmap = Option.when(HasBitmapCheck)(new Bundle { 11478882eb68SXin Tian val req = DecoupledIO(new bitmapReqBundle()) 11488882eb68SXin Tian val resp = Flipped(DecoupledIO(new bitmapRespBundle())) 11498882eb68SXin Tian }) 11508882eb68SXin Tian 11518882eb68SXin Tian val l0_way_info = Option.when(HasBitmapCheck)(Input(UInt(l2tlbParams.l0nWays.W))) 1152d0de7e4aSpeixiaokun} 1153d0de7e4aSpeixiaokun 1154d0de7e4aSpeixiaokunclass HPTW()(implicit p: Parameters) extends XSModule with HasPtwConst { 1155d0de7e4aSpeixiaokun val io = IO(new HPTWIO) 1156d0de7e4aSpeixiaokun val hgatp = io.csr.hgatp 1157dd286b6aSYanqin Li val mpbmte = io.csr.mPBMTE 1158d0de7e4aSpeixiaokun val sfence = io.sfence 11591ae5db63SXiaokun-Pei val flush = sfence.valid || hgatp.changed || io.csr.satp.changed || io.csr.vsatp.changed 11603ea4388cSHaoyuan Feng val mode = hgatp.mode 1161d0de7e4aSpeixiaokun 11628882eb68SXin Tian // mbmc:bitmap csr 11638882eb68SXin Tian val mbmc = io.csr.mbmc 11648882eb68SXin Tian val bitmap_enable = (if (HasBitmapCheck) true.B else false.B) && mbmc.BME === 1.U && mbmc.CMODE === 0.U 11658882eb68SXin Tian 11663ea4388cSHaoyuan Feng val level = RegInit(3.U(log2Up(Level + 1).W)) 1167c1a1e232SHaoyuan Feng val af_level = RegInit(3.U(log2Up(Level + 1).W)) // access fault return this level 1168d0de7e4aSpeixiaokun val gpaddr = Reg(UInt(GPAddrBits.W)) 11694c4af37cSpeixiaokun val req_ppn = Reg(UInt(ppnLen.W)) 1170d0de7e4aSpeixiaokun val vpn = gpaddr(GPAddrBits-1, offLen) 11713ea4388cSHaoyuan Feng val levelNext = level - 1.U 11723ea4388cSHaoyuan Feng val l3Hit = Reg(Bool()) 1173d0de7e4aSpeixiaokun val l2Hit = Reg(Bool()) 11743ea4388cSHaoyuan Feng val l1Hit = Reg(Bool()) 117583d93d53Speixiaokun val bypassed = Reg(Bool()) 1176d0de7e4aSpeixiaokun// val pte = io.mem.resp.bits.MergeRespToPte() 11778882eb68SXin Tian val jmp_bitmap_check = if (HasBitmapCheck) RegEnable(io.req.bits.bitmapCheck.get.jmp_bitmap_check, io.req.fire) else false.B 11788882eb68SXin Tian val fromSP = if (HasBitmapCheck) RegEnable(io.req.bits.bitmapCheck.get.fromSP, io.req.fire) else false.B 11798882eb68SXin Tian val cache_pte = Option.when(HasBitmapCheck)(RegEnable(Mux(io.req.bits.bitmapCheck.get.fromSP, io.req.bits.bitmapCheck.get.pte.asTypeOf(new PteBundle().cloneType), io.req.bits.bitmapCheck.get.ptes(io.req.bits.gvpn(sectortlbwidth - 1, 0)).asTypeOf(new PteBundle().cloneType)), io.req.fire)) 11808882eb68SXin Tian val pte = if (HasBitmapCheck) Mux(jmp_bitmap_check, cache_pte.get, io.mem.resp.bits.asTypeOf(new PteBundle().cloneType)) else io.mem.resp.bits.asTypeOf(new PteBundle().cloneType) 11813ea4388cSHaoyuan Feng val ppn_l3 = Mux(l3Hit, req_ppn, pte.ppn) 11824c4af37cSpeixiaokun val ppn_l2 = Mux(l2Hit, req_ppn, pte.ppn) 11833ea4388cSHaoyuan Feng val ppn_l1 = Mux(l1Hit, req_ppn, pte.ppn) 11843ea4388cSHaoyuan Feng val ppn = Wire(UInt(PAddrBits.W)) 11853ea4388cSHaoyuan Feng val p_pte = MakeAddr(ppn, getVpnn(vpn, level)) 11863ea4388cSHaoyuan Feng val pg_base = Wire(UInt(PAddrBits.W)) 11873ea4388cSHaoyuan Feng val mem_addr = Wire(UInt(PAddrBits.W)) 11883ea4388cSHaoyuan Feng if (EnableSv48) { 11893ea4388cSHaoyuan Feng when (mode === Sv48) { 1190c1a1e232SHaoyuan Feng ppn := Mux(af_level === 2.U, ppn_l3, Mux(af_level === 1.U, ppn_l2, ppn_l1)) // for l2, l1 and l3 11913ea4388cSHaoyuan Feng pg_base := MakeGPAddr(hgatp.ppn, getGVpnn(vpn, 3.U, mode = Sv48)) // for l3 1192c1a1e232SHaoyuan Feng mem_addr := Mux(af_level === 3.U, pg_base, p_pte) 11933ea4388cSHaoyuan Feng } .otherwise { 1194c1a1e232SHaoyuan Feng ppn := Mux(af_level === 1.U, ppn_l2, ppn_l1) //for l1 and l2 11953ea4388cSHaoyuan Feng pg_base := MakeGPAddr(hgatp.ppn, getGVpnn(vpn, 2.U, mode = Sv39)) 1196c1a1e232SHaoyuan Feng mem_addr := Mux(af_level === 2.U, pg_base, p_pte) 11973ea4388cSHaoyuan Feng } 11983ea4388cSHaoyuan Feng } else { 1199c1a1e232SHaoyuan Feng ppn := Mux(af_level === 1.U, ppn_l2, ppn_l1) //for l1 and l2 12003ea4388cSHaoyuan Feng pg_base := MakeGPAddr(hgatp.ppn, getGVpnn(vpn, 2.U, mode = Sv39)) 1201c1a1e232SHaoyuan Feng mem_addr := Mux(af_level === 2.U, pg_base, p_pte) 12023ea4388cSHaoyuan Feng } 1203d0de7e4aSpeixiaokun 1204d0de7e4aSpeixiaokun //s/w register 1205d0de7e4aSpeixiaokun val s_pmp_check = RegInit(true.B) 1206d0de7e4aSpeixiaokun val s_mem_req = RegInit(true.B) 1207d0de7e4aSpeixiaokun val w_mem_resp = RegInit(true.B) 1208d0de7e4aSpeixiaokun val idle = RegInit(true.B) 120903c1129fSpeixiaokun val mem_addr_update = RegInit(false.B) 1210d0de7e4aSpeixiaokun val finish = WireInit(false.B) 12118882eb68SXin Tian val s_bitmap_check = RegInit(true.B) 12128882eb68SXin Tian val w_bitmap_resp = RegInit(true.B) 12138882eb68SXin Tian val whether_need_bitmap_check = RegInit(false.B) 12148882eb68SXin Tian val bitmap_checkfailed = RegInit(false.B) 1215d0de7e4aSpeixiaokun 1216d0de7e4aSpeixiaokun val sent_to_pmp = !idle && (!s_pmp_check || mem_addr_update) && !finish 1217dd286b6aSYanqin Li val pageFault = pte.isGpf(level, mpbmte) || (!pte.isLeaf() && level === 0.U) 1218d0de7e4aSpeixiaokun val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, sent_to_pmp) 1219d0de7e4aSpeixiaokun 12208882eb68SXin Tian // use access fault when bitmap check failed 12218882eb68SXin Tian val ppn_af = if (HasBitmapCheck) { 12228882eb68SXin Tian Mux(bitmap_enable, pte.isAf() || bitmap_checkfailed, pte.isAf()) 12238882eb68SXin Tian } else { 12248882eb68SXin Tian pte.isAf() 12258882eb68SXin Tian } 1226d0de7e4aSpeixiaokun val find_pte = pte.isLeaf() || ppn_af || pageFault 1227d0de7e4aSpeixiaokun 1228d0de7e4aSpeixiaokun val resp_valid = !idle && mem_addr_update && ((w_mem_resp && find_pte) || (s_pmp_check && accessFault)) 1229d0de7e4aSpeixiaokun val id = Reg(UInt(log2Up(l2tlbParams.llptwsize).W)) 12303222d00fSpeixiaokun val source = RegEnable(io.req.bits.source, io.req.fire) 1231eb4bf3f2Speixiaokun 1232d0de7e4aSpeixiaokun io.req.ready := idle 1233eb4bf3f2Speixiaokun val resp = Wire(new HptwResp()) 12346962b4ffSHaoyuan Feng // accessFault > pageFault > ppn_af 12356962b4ffSHaoyuan Feng resp.apply( 12366962b4ffSHaoyuan Feng gpf = pageFault && !accessFault, 12376962b4ffSHaoyuan Feng gaf = accessFault || (ppn_af && !pageFault), 12386962b4ffSHaoyuan Feng level = Mux(accessFault, af_level, level), 12396962b4ffSHaoyuan Feng pte = pte, 12406962b4ffSHaoyuan Feng vpn = vpn, 12416962b4ffSHaoyuan Feng vmid = hgatp.vmid 12426962b4ffSHaoyuan Feng ) 1243d0de7e4aSpeixiaokun io.resp.valid := resp_valid 1244d0de7e4aSpeixiaokun io.resp.bits.id := id 1245d0de7e4aSpeixiaokun io.resp.bits.resp := resp 1246eb4bf3f2Speixiaokun io.resp.bits.source := source 1247d0de7e4aSpeixiaokun 1248d0de7e4aSpeixiaokun io.pmp.req.valid := DontCare 1249d0de7e4aSpeixiaokun io.pmp.req.bits.addr := mem_addr 1250d0de7e4aSpeixiaokun io.pmp.req.bits.size := 3.U 1251d0de7e4aSpeixiaokun io.pmp.req.bits.cmd := TlbCmd.read 1252d0de7e4aSpeixiaokun 12538882eb68SXin Tian if (HasBitmapCheck) { 12548882eb68SXin Tian val way_info = DataHoldBypass(io.l0_way_info.get, RegNext(io.mem.resp.fire, init=false.B)) 12558882eb68SXin Tian val cache_hitway = RegEnable(io.req.bits.bitmapCheck.get.hitway, io.req.fire) 12568882eb68SXin Tian val cache_level = RegEnable(io.req.bits.bitmapCheck.get.SPlevel, io.req.fire) 12578882eb68SXin Tian io.bitmap.get.req.valid := !s_bitmap_check 12588882eb68SXin Tian io.bitmap.get.req.bits.bmppn := pte.ppn 12598882eb68SXin Tian io.bitmap.get.req.bits.id := HptwReqId.U(bMemID.W) 12608882eb68SXin Tian io.bitmap.get.req.bits.vpn := vpn 12618882eb68SXin Tian io.bitmap.get.req.bits.level := Mux(jmp_bitmap_check, Mux(fromSP,cache_level,0.U), level) 12628882eb68SXin Tian io.bitmap.get.req.bits.way_info := Mux(jmp_bitmap_check, cache_hitway, way_info) 12638882eb68SXin Tian io.bitmap.get.req.bits.hptw_bypassed := bypassed 12648882eb68SXin Tian io.bitmap.get.resp.ready := !w_bitmap_resp 12658882eb68SXin Tian } 12668882eb68SXin Tian 1267d0de7e4aSpeixiaokun io.mem.req.valid := !s_mem_req && !io.mem.mask && !accessFault && s_pmp_check 1268d0de7e4aSpeixiaokun io.mem.req.bits.addr := mem_addr 1269d0de7e4aSpeixiaokun io.mem.req.bits.id := HptwReqId.U(bMemID.W) 127083d93d53Speixiaokun io.mem.req.bits.hptw_bypassed := bypassed 1271d0de7e4aSpeixiaokun 127282978df9Speixiaokun io.refill.req_info.vpn := vpn 1273d0de7e4aSpeixiaokun io.refill.level := level 1274eb4bf3f2Speixiaokun io.refill.req_info.source := source 1275eb4bf3f2Speixiaokun io.refill.req_info.s2xlate := onlyStage2 12768882eb68SXin Tian 1277d0de7e4aSpeixiaokun when (idle){ 12788882eb68SXin Tian if (HasBitmapCheck) { 12798882eb68SXin Tian when (io.req.bits.bitmapCheck.get.jmp_bitmap_check && io.req.fire) { 12808882eb68SXin Tian idle := false.B 12818882eb68SXin Tian gpaddr := Cat(io.req.bits.gvpn, 0.U(offLen.W)) 12828882eb68SXin Tian s_bitmap_check := false.B 12838882eb68SXin Tian id := io.req.bits.id 12848882eb68SXin Tian level := Mux(io.req.bits.bitmapCheck.get.fromSP, io.req.bits.bitmapCheck.get.SPlevel, 0.U) 12858882eb68SXin Tian } 12868882eb68SXin Tian } 12878882eb68SXin Tian when (io.req.fire && (if (HasBitmapCheck) !io.req.bits.bitmapCheck.get.jmp_bitmap_check else true.B)) { 128883d93d53Speixiaokun bypassed := io.req.bits.bypassed 1289d0de7e4aSpeixiaokun idle := false.B 1290d0de7e4aSpeixiaokun gpaddr := Cat(io.req.bits.gvpn, 0.U(offLen.W)) 1291d0de7e4aSpeixiaokun accessFault := false.B 1292d0de7e4aSpeixiaokun s_pmp_check := false.B 1293d0de7e4aSpeixiaokun id := io.req.bits.id 12944c4af37cSpeixiaokun req_ppn := io.req.bits.ppn 12953ea4388cSHaoyuan Feng if (EnableSv48) { 12963ea4388cSHaoyuan Feng when (mode === Sv48) { 12973ea4388cSHaoyuan 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))) 1298c1a1e232SHaoyuan Feng af_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))) 12993ea4388cSHaoyuan Feng l3Hit := io.req.bits.l3Hit.get 13003ea4388cSHaoyuan Feng } .otherwise { 13013ea4388cSHaoyuan Feng level := Mux(io.req.bits.l1Hit, 0.U, Mux(io.req.bits.l2Hit, 1.U, 2.U)) 1302c1a1e232SHaoyuan Feng af_level := Mux(io.req.bits.l1Hit, 0.U, Mux(io.req.bits.l2Hit, 1.U, 2.U)) 13033ea4388cSHaoyuan Feng l3Hit := false.B 13043ea4388cSHaoyuan Feng } 13053ea4388cSHaoyuan Feng } else { 13063ea4388cSHaoyuan Feng level := Mux(io.req.bits.l1Hit, 0.U, Mux(io.req.bits.l2Hit, 1.U, 2.U)) 1307c1a1e232SHaoyuan Feng af_level := Mux(io.req.bits.l1Hit, 0.U, Mux(io.req.bits.l2Hit, 1.U, 2.U)) 13083ea4388cSHaoyuan Feng l3Hit := false.B 13093ea4388cSHaoyuan Feng } 1310d0de7e4aSpeixiaokun l2Hit := io.req.bits.l2Hit 13113ea4388cSHaoyuan Feng l1Hit := io.req.bits.l1Hit 1312d0de7e4aSpeixiaokun } 1313d0de7e4aSpeixiaokun } 1314d0de7e4aSpeixiaokun 1315d0de7e4aSpeixiaokun when(sent_to_pmp && !mem_addr_update){ 1316d0de7e4aSpeixiaokun s_mem_req := false.B 1317d0de7e4aSpeixiaokun s_pmp_check := true.B 1318d0de7e4aSpeixiaokun } 1319d0de7e4aSpeixiaokun 1320d0de7e4aSpeixiaokun when(accessFault && !idle){ 1321d0de7e4aSpeixiaokun s_pmp_check := true.B 1322d0de7e4aSpeixiaokun s_mem_req := true.B 1323d0de7e4aSpeixiaokun w_mem_resp := true.B 1324d0de7e4aSpeixiaokun mem_addr_update := true.B 13258882eb68SXin Tian if (HasBitmapCheck) { 13268882eb68SXin Tian s_bitmap_check := true.B 13278882eb68SXin Tian w_bitmap_resp := true.B 13288882eb68SXin Tian whether_need_bitmap_check := false.B 13298882eb68SXin Tian bitmap_checkfailed := false.B 13308882eb68SXin Tian } 1331d0de7e4aSpeixiaokun } 1332d0de7e4aSpeixiaokun 13333222d00fSpeixiaokun when(io.mem.req.fire){ 1334d0de7e4aSpeixiaokun s_mem_req := true.B 1335d0de7e4aSpeixiaokun w_mem_resp := false.B 1336d0de7e4aSpeixiaokun } 1337d0de7e4aSpeixiaokun 13383222d00fSpeixiaokun when(io.mem.resp.fire && !w_mem_resp){ 1339d0de7e4aSpeixiaokun w_mem_resp := true.B 1340c1a1e232SHaoyuan Feng af_level := af_level - 1.U 13418882eb68SXin Tian if (HasBitmapCheck) { 13428882eb68SXin Tian when (bitmap_enable) { 13438882eb68SXin Tian whether_need_bitmap_check := true.B 13448882eb68SXin Tian } .otherwise { 1345d0de7e4aSpeixiaokun mem_addr_update := true.B 13468882eb68SXin Tian whether_need_bitmap_check := false.B 13478882eb68SXin Tian } 13488882eb68SXin Tian } else { 13498882eb68SXin Tian mem_addr_update := true.B 13508882eb68SXin Tian } 13518882eb68SXin Tian } 13528882eb68SXin Tian 13538882eb68SXin Tian if (HasBitmapCheck) { 13548882eb68SXin Tian when (whether_need_bitmap_check) { 13558882eb68SXin Tian when (bitmap_enable && pte.isLeaf()) { 13568882eb68SXin Tian s_bitmap_check := false.B 13578882eb68SXin Tian whether_need_bitmap_check := false.B 13588882eb68SXin Tian } .otherwise { 13598882eb68SXin Tian mem_addr_update := true.B 13608882eb68SXin Tian whether_need_bitmap_check := false.B 13618882eb68SXin Tian } 13628882eb68SXin Tian } 13638882eb68SXin Tian // bitmapcheck 13648882eb68SXin Tian when (io.bitmap.get.req.fire) { 13658882eb68SXin Tian s_bitmap_check := true.B 13668882eb68SXin Tian w_bitmap_resp := false.B 13678882eb68SXin Tian } 13688882eb68SXin Tian when (io.bitmap.get.resp.fire) { 13698882eb68SXin Tian w_bitmap_resp := true.B 13708882eb68SXin Tian mem_addr_update := true.B 13718882eb68SXin Tian bitmap_checkfailed := io.bitmap.get.resp.bits.cf 13728882eb68SXin Tian } 1373d0de7e4aSpeixiaokun } 1374d0de7e4aSpeixiaokun 1375d0de7e4aSpeixiaokun when(mem_addr_update){ 1376d0de7e4aSpeixiaokun when(!(find_pte || accessFault)){ 1377d0de7e4aSpeixiaokun level := levelNext 1378d0de7e4aSpeixiaokun s_mem_req := false.B 1379d0de7e4aSpeixiaokun mem_addr_update := false.B 1380d0de7e4aSpeixiaokun }.elsewhen(resp_valid){ 13813222d00fSpeixiaokun when(io.resp.fire){ 1382d0de7e4aSpeixiaokun idle := true.B 1383d0de7e4aSpeixiaokun mem_addr_update := false.B 1384d0de7e4aSpeixiaokun accessFault := false.B 1385d0de7e4aSpeixiaokun } 1386d0de7e4aSpeixiaokun finish := true.B 1387d0de7e4aSpeixiaokun } 1388d0de7e4aSpeixiaokun } 13895961467fSXiaokun-Pei when (flush) { 13905961467fSXiaokun-Pei idle := true.B 13915961467fSXiaokun-Pei s_pmp_check := true.B 13925961467fSXiaokun-Pei s_mem_req := true.B 13935961467fSXiaokun-Pei w_mem_resp := true.B 13945961467fSXiaokun-Pei accessFault := false.B 13955961467fSXiaokun-Pei mem_addr_update := false.B 13968882eb68SXin Tian if (HasBitmapCheck) { 13978882eb68SXin Tian s_bitmap_check := true.B 13988882eb68SXin Tian w_bitmap_resp := true.B 13998882eb68SXin Tian whether_need_bitmap_check := false.B 14008882eb68SXin Tian bitmap_checkfailed := false.B 14018882eb68SXin Tian } 14025961467fSXiaokun-Pei } 1403d0de7e4aSpeixiaokun} 1404