16d5ddbceSLemover/*************************************************************************************** 26d5ddbceSLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory 46d5ddbceSLemover* 56d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2. 66d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2. 76d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at: 86d5ddbceSLemover* http://license.coscl.org.cn/MulanPSL2 96d5ddbceSLemover* 106d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 116d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 126d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 136d5ddbceSLemover* 146d5ddbceSLemover* See the Mulan PSL v2 for more details. 156d5ddbceSLemover***************************************************************************************/ 166d5ddbceSLemover 176d5ddbceSLemoverpackage xiangshan.cache.mmu 186d5ddbceSLemover 198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 206d5ddbceSLemoverimport chisel3._ 216d5ddbceSLemoverimport chisel3.util._ 226d5ddbceSLemoverimport xiangshan._ 236d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants} 246d5ddbceSLemoverimport utils._ 253c02ee8fSwakafaimport utility._ 266d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 276d5ddbceSLemoverimport freechips.rocketchip.tilelink._ 28b6982e83SLemoverimport xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle} 296d5ddbceSLemover 3092e3bfefSLemover/** Page Table Walk is divided into two parts 3192e3bfefSLemover * One, PTW: page walk for pde, except for leaf entries, one by one 3292e3bfefSLemover * Two, LLPTW: page walk for pte, only the leaf entries(4KB), in parallel 336d5ddbceSLemover */ 3492e3bfefSLemover 3592e3bfefSLemover 3692e3bfefSLemover/** PTW : page table walker 3792e3bfefSLemover * a finite state machine 3892e3bfefSLemover * only take 1GB and 2MB page walks 3992e3bfefSLemover * or in other words, except the last level(leaf) 4092e3bfefSLemover **/ 4192e3bfefSLemoverclass PTWIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 426d5ddbceSLemover val req = Flipped(DecoupledIO(new Bundle { 4345f497a4Shappy-lx val req_info = new L2TlbInnerBundle() 446d5ddbceSLemover val l1Hit = Bool() 456d5ddbceSLemover val ppn = UInt(ppnLen.W) 4630104977Speixiaokun val stage1Hit = Bool() 4730104977Speixiaokun val stage1 = new PtwMergeResp 486d5ddbceSLemover })) 496d5ddbceSLemover val resp = DecoupledIO(new Bundle { 50bc063562SLemover val source = UInt(bSourceWidth.W) 51eb4bf3f2Speixiaokun val s2xlate = UInt(2.W) 5263632028SHaoyuan Feng val resp = new PtwMergeResp 53d0de7e4aSpeixiaokun val h_resp = new HptwResp 546d5ddbceSLemover }) 556d5ddbceSLemover 5692e3bfefSLemover val llptw = DecoupledIO(new LLPTWInBundle()) 579c503409SLemover // NOTE: llptw change from "connect to llptw" to "connect to page cache" 589c503409SLemover // to avoid corner case that caused duplicate entries 59cc5a5f22SLemover 60d0de7e4aSpeixiaokun val hptw = new Bundle { 61d0de7e4aSpeixiaokun val req = DecoupledIO(new Bundle { 62eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 63d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 6482978df9Speixiaokun val gvpn = UInt(vpnLen.W) 65d0de7e4aSpeixiaokun }) 66d0de7e4aSpeixiaokun val resp = Flipped(Valid(new Bundle { 67d0de7e4aSpeixiaokun val h_resp = Output(new HptwResp) 68d0de7e4aSpeixiaokun })) 69d0de7e4aSpeixiaokun } 706d5ddbceSLemover val mem = new Bundle { 71b848eea5SLemover val req = DecoupledIO(new L2TlbMemReqBundle()) 725854c1edSLemover val resp = Flipped(ValidIO(UInt(XLEN.W))) 73cc5a5f22SLemover val mask = Input(Bool()) 746d5ddbceSLemover } 75b6982e83SLemover val pmp = new Bundle { 76b6982e83SLemover val req = ValidIO(new PMPReqBundle()) 77b6982e83SLemover val resp = Flipped(new PMPRespBundle()) 78b6982e83SLemover } 796d5ddbceSLemover 806d5ddbceSLemover val refill = Output(new Bundle { 8145f497a4Shappy-lx val req_info = new L2TlbInnerBundle() 826d5ddbceSLemover val level = UInt(log2Up(Level).W) 836d5ddbceSLemover }) 846d5ddbceSLemover} 856d5ddbceSLemover 8692e3bfefSLemoverclass PTW()(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents { 8792e3bfefSLemover val io = IO(new PTWIO) 886d5ddbceSLemover val sfence = io.sfence 896d5ddbceSLemover val mem = io.mem 90d0de7e4aSpeixiaokun val req_s2xlate = Reg(UInt(2.W)) 9103c1129fSpeixiaokun val enableS2xlate = req_s2xlate =/= noS2xlate 9203c1129fSpeixiaokun val onlyS1xlate = req_s2xlate === onlyStage1 9303c1129fSpeixiaokun val onlyS2xlate = req_s2xlate === onlyStage2 94d0de7e4aSpeixiaokun 95d0de7e4aSpeixiaokun val satp = Mux(enableS2xlate, io.csr.vsatp, io.csr.satp) 96d0de7e4aSpeixiaokun val hgatp = io.csr.hgatp 97d0de7e4aSpeixiaokun val flush = io.sfence.valid || satp.changed 98d0de7e4aSpeixiaokun val s2xlate = enableS2xlate && !onlyS1xlate 996d5ddbceSLemover val level = RegInit(0.U(log2Up(Level).W)) 100b6982e83SLemover val af_level = RegInit(0.U(log2Up(Level).W)) // access fault return this level 1016d5ddbceSLemover val ppn = Reg(UInt(ppnLen.W)) 10282978df9Speixiaokun val vpn = Reg(UInt(vpnLen.W)) // vpn or gvpn 1036d5ddbceSLemover val levelNext = level + 1.U 1046d5ddbceSLemover val l1Hit = Reg(Bool()) 105d0de7e4aSpeixiaokun val pte = mem.resp.bits.asTypeOf(new PteBundle().cloneType) 1066d5ddbceSLemover 10744b79566SXiaokun-Pei // s/w register 10844b79566SXiaokun-Pei val s_pmp_check = RegInit(true.B) 10944b79566SXiaokun-Pei val s_mem_req = RegInit(true.B) 11044b79566SXiaokun-Pei val s_llptw_req = RegInit(true.B) 11144b79566SXiaokun-Pei val w_mem_resp = RegInit(true.B) 112d0de7e4aSpeixiaokun val s_hptw_req = RegInit(true.B) 113d0de7e4aSpeixiaokun val w_hptw_resp = RegInit(true.B) 114d0de7e4aSpeixiaokun val s_last_hptw_req = RegInit(true.B) 115d0de7e4aSpeixiaokun val w_last_hptw_resp = RegInit(true.B) 11644b79566SXiaokun-Pei // for updating "level" 11744b79566SXiaokun-Pei val mem_addr_update = RegInit(false.B) 11844b79566SXiaokun-Pei 11944b79566SXiaokun-Pei val idle = RegInit(true.B) 1202a906a65SHaoyuan Feng val finish = WireInit(false.B) 1212a906a65SHaoyuan Feng val sent_to_pmp = idle === false.B && (s_pmp_check === false.B || mem_addr_update) && !finish 12244b79566SXiaokun-Pei 123d0de7e4aSpeixiaokun val pageFault = pte.isPf(level) 12444b79566SXiaokun-Pei val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, sent_to_pmp) 1256d5ddbceSLemover 126d0de7e4aSpeixiaokun val hptw_pageFault = RegInit(false.B) 127d0de7e4aSpeixiaokun val hptw_accessFault = RegInit(false.B) 128d0de7e4aSpeixiaokun val last_s2xlate = RegInit(false.B) 1293222d00fSpeixiaokun val stage1Hit = RegEnable(io.req.bits.stage1Hit, io.req.fire) 1303222d00fSpeixiaokun val stage1 = RegEnable(io.req.bits.stage1, io.req.fire) 13109280d15Speixiaokun val hptw_resp_stage2 = Reg(Bool()) 132d0de7e4aSpeixiaokun 133d0de7e4aSpeixiaokun val ppn_af = pte.isAf() 134d0de7e4aSpeixiaokun val find_pte = pte.isLeaf() || ppn_af || pageFault 13544b79566SXiaokun-Pei val to_find_pte = level === 1.U && find_pte === false.B 136935edac4STang Haojin val source = RegEnable(io.req.bits.req_info.source, io.req.fire) 1376d5ddbceSLemover 1386d5ddbceSLemover val l1addr = MakeAddr(satp.ppn, getVpnn(vpn, 2)) 139d0de7e4aSpeixiaokun val l2addr = MakeAddr(Mux(l1Hit, ppn, pte.ppn), getVpnn(vpn, 1)) 140b6982e83SLemover val mem_addr = Mux(af_level === 0.U, l1addr, l2addr) 14144b79566SXiaokun-Pei 1423222d00fSpeixiaokun val hptw_resp = RegEnable(io.hptw.resp.bits.h_resp, io.hptw.resp.fire) 143c0991f6aSpeixiaokun val gpaddr = MuxCase(mem_addr, Seq( 144c0991f6aSpeixiaokun stage1Hit -> Cat(stage1.genPPN(), 0.U(offLen.W)), 145c0991f6aSpeixiaokun onlyS2xlate -> Cat(vpn, 0.U(offLen.W)), 146dcb10e8fSBL-GS !s_last_hptw_req -> Cat(MuxLookup(level, pte.ppn)(Seq( 14737fc3812SBL-GS 0.U -> Cat(pte.ppn(ppnLen - 1, vpnnLen * 2), vpn(vpnnLen * 2 - 1, 0)), 14837fc3812SBL-GS 1.U -> Cat(pte.ppn(ppnLen - 1, vpnnLen), vpn(vpnnLen - 1, 0) 149dcb10e8fSBL-GS ))), 150dcb10e8fSBL-GS 0.U(offLen.W)) 151c0991f6aSpeixiaokun )) 152cda84113Speixiaokun val hpaddr = Cat(hptw_resp.genPPNS2(get_pn(gpaddr)), get_off(gpaddr)) 153d0de7e4aSpeixiaokun 15444b79566SXiaokun-Pei io.req.ready := idle 15530104977Speixiaokun val ptw_resp = Wire(new PtwMergeResp) 15630104977Speixiaokun ptw_resp.apply(pageFault && !accessFault && !ppn_af, accessFault || ppn_af, Mux(accessFault, af_level,level), pte, vpn, satp.asid, hgatp.asid, vpn(sectortlbwidth - 1, 0), not_super = false) 15744b79566SXiaokun-Pei 15809280d15Speixiaokun val normal_resp = idle === false.B && mem_addr_update && !last_s2xlate && ((w_mem_resp && find_pte) || (s_pmp_check && accessFault) || onlyS2xlate) 15909280d15Speixiaokun val stageHit_resp = idle === false.B && hptw_resp_stage2 16009280d15Speixiaokun io.resp.valid := Mux(stage1Hit, stageHit_resp, normal_resp) 16144b79566SXiaokun-Pei io.resp.bits.source := source 16230104977Speixiaokun io.resp.bits.resp := Mux(stage1Hit, stage1, ptw_resp) 16379d4b70cSpeixiaokun io.resp.bits.h_resp := hptw_resp 1646315ba2aSpeixiaokun io.resp.bits.s2xlate := req_s2xlate 16544b79566SXiaokun-Pei 16644b79566SXiaokun-Pei io.llptw.valid := s_llptw_req === false.B && to_find_pte && !accessFault 16744b79566SXiaokun-Pei io.llptw.bits.req_info.source := source 16844b79566SXiaokun-Pei io.llptw.bits.req_info.vpn := vpn 16982978df9Speixiaokun io.llptw.bits.req_info.s2xlate := req_s2xlate 170eb4bf3f2Speixiaokun io.llptw.bits.ppn := DontCare 17144b79566SXiaokun-Pei 172b6982e83SLemover io.pmp.req.valid := DontCare // samecycle, do not use valid 173d0de7e4aSpeixiaokun io.pmp.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr) 174b6982e83SLemover io.pmp.req.bits.size := 3.U // TODO: fix it 175b6982e83SLemover io.pmp.req.bits.cmd := TlbCmd.read 176b6982e83SLemover 17744b79566SXiaokun-Pei mem.req.valid := s_mem_req === false.B && !mem.mask && !accessFault && s_pmp_check 178d0de7e4aSpeixiaokun mem.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr) 179bc063562SLemover mem.req.bits.id := FsmReqID.U(bMemID.W) 1806d5ddbceSLemover 181933ec998Speixiaokun io.refill.req_info.s2xlate := Mux(enableS2xlate, onlyStage1, req_s2xlate) // ptw refill the pte of stage 1 when s2xlate is enabled 18245f497a4Shappy-lx io.refill.req_info.vpn := vpn 1836d5ddbceSLemover io.refill.level := level 18445f497a4Shappy-lx io.refill.req_info.source := source 1856d5ddbceSLemover 186d0de7e4aSpeixiaokun io.hptw.req.valid := !s_hptw_req || !s_last_hptw_req 187d0de7e4aSpeixiaokun io.hptw.req.bits.id := FsmReqID.U(bMemID.W) 188dcb10e8fSBL-GS io.hptw.req.bits.gvpn := get_pn(gpaddr) 189eb4bf3f2Speixiaokun io.hptw.req.bits.source := source 190d0de7e4aSpeixiaokun 1913222d00fSpeixiaokun when (io.req.fire && io.req.bits.stage1Hit){ 19230104977Speixiaokun idle := false.B 19361c5d636Speixiaokun req_s2xlate := io.req.bits.req_info.s2xlate 19430104977Speixiaokun s_hptw_req := false.B 19509280d15Speixiaokun hptw_resp_stage2 := false.B 19630104977Speixiaokun } 197d0de7e4aSpeixiaokun 1983222d00fSpeixiaokun when (io.hptw.resp.fire && w_hptw_resp === false.B && stage1Hit){ 19930104977Speixiaokun w_hptw_resp := true.B 20009280d15Speixiaokun hptw_resp_stage2 := true.B 20130104977Speixiaokun } 20230104977Speixiaokun 2033222d00fSpeixiaokun when (io.resp.fire && stage1Hit){ 20430104977Speixiaokun idle := true.B 20530104977Speixiaokun } 20630104977Speixiaokun 2073222d00fSpeixiaokun when (io.req.fire && !io.req.bits.stage1Hit){ 20844b79566SXiaokun-Pei val req = io.req.bits 20944b79566SXiaokun-Pei level := Mux(req.l1Hit, 1.U, 0.U) 21044b79566SXiaokun-Pei af_level := Mux(req.l1Hit, 1.U, 0.U) 21144b79566SXiaokun-Pei ppn := Mux(req.l1Hit, io.req.bits.ppn, satp.ppn) 21244b79566SXiaokun-Pei vpn := io.req.bits.req_info.vpn 21344b79566SXiaokun-Pei l1Hit := req.l1Hit 21444b79566SXiaokun-Pei accessFault := false.B 21544b79566SXiaokun-Pei idle := false.B 216d0de7e4aSpeixiaokun hptw_pageFault := false.B 21750c7aa78Speixiaokun req_s2xlate := io.req.bits.req_info.s2xlate 21882978df9Speixiaokun when(io.req.bits.req_info.s2xlate =/= noS2xlate && io.req.bits.req_info.s2xlate =/= onlyStage1){ 219d0de7e4aSpeixiaokun last_s2xlate := true.B 220d0de7e4aSpeixiaokun s_hptw_req := false.B 221d0de7e4aSpeixiaokun }.otherwise { 222d0de7e4aSpeixiaokun s_pmp_check := false.B 223d0de7e4aSpeixiaokun } 224d0de7e4aSpeixiaokun } 225d0de7e4aSpeixiaokun 2263222d00fSpeixiaokun when(io.hptw.req.fire && s_hptw_req === false.B){ 227d0de7e4aSpeixiaokun s_hptw_req := true.B 228d0de7e4aSpeixiaokun w_hptw_resp := false.B 229d0de7e4aSpeixiaokun } 230d0de7e4aSpeixiaokun 2313222d00fSpeixiaokun when(io.hptw.resp.fire && w_hptw_resp === false.B && !stage1Hit) { 232d0de7e4aSpeixiaokun hptw_pageFault := io.hptw.resp.bits.h_resp.gpf 233d0de7e4aSpeixiaokun hptw_accessFault := io.hptw.resp.bits.h_resp.gaf 234d0de7e4aSpeixiaokun w_hptw_resp := true.B 235d0de7e4aSpeixiaokun when(onlyS2xlate){ 236d0de7e4aSpeixiaokun mem_addr_update := true.B 237d0de7e4aSpeixiaokun last_s2xlate := false.B 238d0de7e4aSpeixiaokun }.otherwise { 239d0de7e4aSpeixiaokun s_pmp_check := false.B 240d0de7e4aSpeixiaokun } 241d0de7e4aSpeixiaokun } 242d0de7e4aSpeixiaokun 2433222d00fSpeixiaokun when(io.hptw.req.fire && s_last_hptw_req === false.B) { 244d0de7e4aSpeixiaokun w_last_hptw_resp := false.B 245d0de7e4aSpeixiaokun s_last_hptw_req := true.B 246d0de7e4aSpeixiaokun } 247d0de7e4aSpeixiaokun 2483222d00fSpeixiaokun when(io.hptw.resp.fire && w_last_hptw_resp === false.B){ 249d0de7e4aSpeixiaokun hptw_pageFault := io.hptw.resp.bits.h_resp.gpf 250d0de7e4aSpeixiaokun hptw_accessFault := io.hptw.resp.bits.h_resp.gaf 251d0de7e4aSpeixiaokun w_last_hptw_resp := true.B 252d0de7e4aSpeixiaokun mem_addr_update := true.B 253d0de7e4aSpeixiaokun last_s2xlate := false.B 25444b79566SXiaokun-Pei } 25544b79566SXiaokun-Pei 25644b79566SXiaokun-Pei when(sent_to_pmp && mem_addr_update === false.B){ 25744b79566SXiaokun-Pei s_mem_req := false.B 25844b79566SXiaokun-Pei s_pmp_check := true.B 25944b79566SXiaokun-Pei } 26044b79566SXiaokun-Pei 26144b79566SXiaokun-Pei when(accessFault && idle === false.B){ 26244b79566SXiaokun-Pei s_pmp_check := true.B 26344b79566SXiaokun-Pei s_mem_req := true.B 26444b79566SXiaokun-Pei w_mem_resp := true.B 26544b79566SXiaokun-Pei s_llptw_req := true.B 266d0de7e4aSpeixiaokun s_hptw_req := true.B 267d0de7e4aSpeixiaokun w_hptw_resp := true.B 268d0de7e4aSpeixiaokun s_last_hptw_req := true.B 269d0de7e4aSpeixiaokun w_last_hptw_resp := true.B 27044b79566SXiaokun-Pei mem_addr_update := true.B 271d0de7e4aSpeixiaokun last_s2xlate := false.B 27244b79566SXiaokun-Pei } 27344b79566SXiaokun-Pei 274935edac4STang Haojin when (mem.req.fire){ 27544b79566SXiaokun-Pei s_mem_req := true.B 27644b79566SXiaokun-Pei w_mem_resp := false.B 27744b79566SXiaokun-Pei } 27844b79566SXiaokun-Pei 279935edac4STang Haojin when(mem.resp.fire && w_mem_resp === false.B){ 28044b79566SXiaokun-Pei w_mem_resp := true.B 28144b79566SXiaokun-Pei af_level := af_level + 1.U 28244b79566SXiaokun-Pei s_llptw_req := false.B 28344b79566SXiaokun-Pei mem_addr_update := true.B 28444b79566SXiaokun-Pei } 28544b79566SXiaokun-Pei 28644b79566SXiaokun-Pei when(mem_addr_update){ 287e1e498e7SBL-GS when(level === 0.U && !onlyS2xlate && !(find_pte || accessFault)){ 28844b79566SXiaokun-Pei level := levelNext 289d0de7e4aSpeixiaokun when(s2xlate){ 290d0de7e4aSpeixiaokun s_hptw_req := false.B 291d0de7e4aSpeixiaokun }.otherwise{ 29244b79566SXiaokun-Pei s_mem_req := false.B 293d0de7e4aSpeixiaokun } 29444b79566SXiaokun-Pei s_llptw_req := true.B 29544b79566SXiaokun-Pei mem_addr_update := false.B 2962a906a65SHaoyuan Feng }.elsewhen(io.llptw.valid){ 297935edac4STang Haojin when(io.llptw.fire) { 29844b79566SXiaokun-Pei idle := true.B 29944b79566SXiaokun-Pei s_llptw_req := true.B 30044b79566SXiaokun-Pei mem_addr_update := false.B 301d0de7e4aSpeixiaokun last_s2xlate := false.B 3022a906a65SHaoyuan Feng } 3032a906a65SHaoyuan Feng finish := true.B 304d0de7e4aSpeixiaokun }.elsewhen(s2xlate && last_s2xlate === true.B) { 305d0de7e4aSpeixiaokun s_last_hptw_req := false.B 306d0de7e4aSpeixiaokun mem_addr_update := false.B 3072a906a65SHaoyuan Feng }.elsewhen(io.resp.valid){ 308935edac4STang Haojin when(io.resp.fire) { 30944b79566SXiaokun-Pei idle := true.B 31044b79566SXiaokun-Pei s_llptw_req := true.B 31144b79566SXiaokun-Pei mem_addr_update := false.B 31244b79566SXiaokun-Pei accessFault := false.B 31344b79566SXiaokun-Pei } 3142a906a65SHaoyuan Feng finish := true.B 3152a906a65SHaoyuan Feng } 31644b79566SXiaokun-Pei } 31744b79566SXiaokun-Pei 31844b79566SXiaokun-Pei 31944b79566SXiaokun-Pei when (sfence.valid) { 32044b79566SXiaokun-Pei idle := true.B 32144b79566SXiaokun-Pei s_pmp_check := true.B 32244b79566SXiaokun-Pei s_mem_req := true.B 32344b79566SXiaokun-Pei s_llptw_req := true.B 32444b79566SXiaokun-Pei w_mem_resp := true.B 32544b79566SXiaokun-Pei accessFault := false.B 326d826bce1SHaoyuan Feng mem_addr_update := false.B 327d0de7e4aSpeixiaokun s_hptw_req := true.B 328d0de7e4aSpeixiaokun w_hptw_resp := true.B 329d0de7e4aSpeixiaokun s_last_hptw_req := true.B 330d0de7e4aSpeixiaokun w_last_hptw_resp := true.B 33144b79566SXiaokun-Pei } 33244b79566SXiaokun-Pei 33344b79566SXiaokun-Pei 33444b79566SXiaokun-Pei XSDebug(p"[ptw] level:${level} notFound:${pageFault}\n") 3356d5ddbceSLemover 3366d5ddbceSLemover // perf 337935edac4STang Haojin XSPerfAccumulate("fsm_count", io.req.fire) 3386d5ddbceSLemover for (i <- 0 until PtwWidth) { 339935edac4STang Haojin XSPerfAccumulate(s"fsm_count_source${i}", io.req.fire && io.req.bits.req_info.source === i.U) 3406d5ddbceSLemover } 34144b79566SXiaokun-Pei XSPerfAccumulate("fsm_busy", !idle) 34244b79566SXiaokun-Pei XSPerfAccumulate("fsm_idle", idle) 3436d5ddbceSLemover XSPerfAccumulate("resp_blocked", io.resp.valid && !io.resp.ready) 344dd7fe201SHaoyuan Feng XSPerfAccumulate("ptw_ppn_af", io.resp.fire && ppn_af) 345935edac4STang Haojin XSPerfAccumulate("mem_count", mem.req.fire) 346935edac4STang Haojin XSPerfAccumulate("mem_cycle", BoolStopWatch(mem.req.fire, mem.resp.fire, true)) 3476d5ddbceSLemover XSPerfAccumulate("mem_blocked", mem.req.valid && !mem.req.ready) 348cc5a5f22SLemover 34944b79566SXiaokun-Pei TimeOutAssert(!idle, timeOutThreshold, "page table walker time out") 350cd365d4cSrvcoresjw 351cd365d4cSrvcoresjw val perfEvents = Seq( 352935edac4STang Haojin ("fsm_count ", io.req.fire ), 35344b79566SXiaokun-Pei ("fsm_busy ", !idle ), 35444b79566SXiaokun-Pei ("fsm_idle ", idle ), 355cd365d4cSrvcoresjw ("resp_blocked ", io.resp.valid && !io.resp.ready ), 356935edac4STang Haojin ("mem_count ", mem.req.fire ), 357935edac4STang Haojin ("mem_cycle ", BoolStopWatch(mem.req.fire, mem.resp.fire, true)), 358cd365d4cSrvcoresjw ("mem_blocked ", mem.req.valid && !mem.req.ready ), 359cd365d4cSrvcoresjw ) 3601ca0e4f3SYinan Xu generatePerfEvent() 3616d5ddbceSLemover} 36292e3bfefSLemover 36392e3bfefSLemover/*========================= LLPTW ==============================*/ 36492e3bfefSLemover 36592e3bfefSLemover/** LLPTW : Last Level Page Table Walker 36692e3bfefSLemover * the page walker that only takes 4KB(last level) page walk. 36792e3bfefSLemover **/ 36892e3bfefSLemover 36992e3bfefSLemoverclass LLPTWInBundle(implicit p: Parameters) extends XSBundle with HasPtwConst { 37092e3bfefSLemover val req_info = Output(new L2TlbInnerBundle()) 371d61cd5eeSpeixiaokun val ppn = Output(if(HasHExtension) UInt((vpnLen.max(ppnLen)).W) else UInt(ppnLen.W)) 37292e3bfefSLemover} 37392e3bfefSLemover 37492e3bfefSLemoverclass LLPTWIO(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 37592e3bfefSLemover val in = Flipped(DecoupledIO(new LLPTWInBundle())) 37692e3bfefSLemover val out = DecoupledIO(new Bundle { 37792e3bfefSLemover val req_info = Output(new L2TlbInnerBundle()) 37892e3bfefSLemover val id = Output(UInt(bMemID.W)) 379d0de7e4aSpeixiaokun val h_resp = Output(new HptwResp) 38092e3bfefSLemover val af = Output(Bool()) 38192e3bfefSLemover }) 38292e3bfefSLemover val mem = new Bundle { 38392e3bfefSLemover val req = DecoupledIO(new L2TlbMemReqBundle()) 38492e3bfefSLemover val resp = Flipped(Valid(new Bundle { 38592e3bfefSLemover val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W)) 386ce5f4200SGuanghui Hu val value = Output(UInt(blockBits.W)) 38792e3bfefSLemover })) 38892e3bfefSLemover val enq_ptr = Output(UInt(log2Ceil(l2tlbParams.llptwsize).W)) 38992e3bfefSLemover val buffer_it = Output(Vec(l2tlbParams.llptwsize, Bool())) 39092e3bfefSLemover val refill = Output(new L2TlbInnerBundle()) 39192e3bfefSLemover val req_mask = Input(Vec(l2tlbParams.llptwsize, Bool())) 39292e3bfefSLemover } 3937797f035SbugGenerator val cache = DecoupledIO(new L2TlbInnerBundle()) 39492e3bfefSLemover val pmp = new Bundle { 39592e3bfefSLemover val req = Valid(new PMPReqBundle()) 39692e3bfefSLemover val resp = Flipped(new PMPRespBundle()) 39792e3bfefSLemover } 398d0de7e4aSpeixiaokun val hptw = new Bundle { 399d0de7e4aSpeixiaokun val req = DecoupledIO(new Bundle{ 400eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 401d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 40282978df9Speixiaokun val gvpn = UInt(vpnLen.W) 403d0de7e4aSpeixiaokun }) 404d0de7e4aSpeixiaokun val resp = Flipped(Valid(new Bundle { 405d0de7e4aSpeixiaokun val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W)) 406d0de7e4aSpeixiaokun val h_resp = Output(new HptwResp) 407d0de7e4aSpeixiaokun })) 408d0de7e4aSpeixiaokun } 40992e3bfefSLemover} 41092e3bfefSLemover 41192e3bfefSLemoverclass LLPTWEntry(implicit p: Parameters) extends XSBundle with HasPtwConst { 41292e3bfefSLemover val req_info = new L2TlbInnerBundle() 41392e3bfefSLemover val ppn = UInt(ppnLen.W) 41492e3bfefSLemover val wait_id = UInt(log2Up(l2tlbParams.llptwsize).W) 41592e3bfefSLemover val af = Bool() 416dc05c713Speixiaokun val hptw_resp = new HptwResp() 41792e3bfefSLemover} 41892e3bfefSLemover 41992e3bfefSLemover 42092e3bfefSLemoverclass LLPTW(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents { 42192e3bfefSLemover val io = IO(new LLPTWIO()) 42282978df9Speixiaokun val enableS2xlate = io.in.bits.req_info.s2xlate =/= noS2xlate 423d0de7e4aSpeixiaokun val satp = Mux(enableS2xlate, io.csr.vsatp, io.csr.satp) 42492e3bfefSLemover 425d0de7e4aSpeixiaokun val flush = io.sfence.valid || satp.changed 42692e3bfefSLemover val entries = Reg(Vec(l2tlbParams.llptwsize, new LLPTWEntry())) 427d0de7e4aSpeixiaokun 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) 42892e3bfefSLemover val state = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(state_idle))) 4297797f035SbugGenerator 43092e3bfefSLemover val is_emptys = state.map(_ === state_idle) 43192e3bfefSLemover val is_mems = state.map(_ === state_mem_req) 43292e3bfefSLemover val is_waiting = state.map(_ === state_mem_waiting) 43392e3bfefSLemover val is_having = state.map(_ === state_mem_out) 4347797f035SbugGenerator val is_cache = state.map(_ === state_cache) 435d0de7e4aSpeixiaokun val is_hptw_req = state.map(_ === state_hptw_req) 436d0de7e4aSpeixiaokun val is_last_hptw_req = state.map(_ === state_last_hptw_req) 437b7bdb307Speixiaokun val is_hptw_resp = state.map(_ === state_hptw_resp) 438b7bdb307Speixiaokun val is_last_hptw_resp = state.map(_ === state_last_hptw_resp) 43992e3bfefSLemover 440935edac4STang Haojin val full = !ParallelOR(is_emptys).asBool 44192e3bfefSLemover val enq_ptr = ParallelPriorityEncoder(is_emptys) 44292e3bfefSLemover 4437797f035SbugGenerator val mem_ptr = ParallelPriorityEncoder(is_having) // TODO: optimize timing, bad: entries -> ptr -> entry 44492e3bfefSLemover val mem_arb = Module(new RRArbiter(new LLPTWEntry(), l2tlbParams.llptwsize)) 44592e3bfefSLemover for (i <- 0 until l2tlbParams.llptwsize) { 44692e3bfefSLemover mem_arb.io.in(i).bits := entries(i) 44792e3bfefSLemover mem_arb.io.in(i).valid := is_mems(i) && !io.mem.req_mask(i) 44892e3bfefSLemover } 449d0de7e4aSpeixiaokun val hyper_arb1 = Module(new RRArbiter(new LLPTWEntry(), l2tlbParams.llptwsize)) 450d0de7e4aSpeixiaokun for (i <- 0 until l2tlbParams.llptwsize) { 451d0de7e4aSpeixiaokun hyper_arb1.io.in(i).bits := entries(i) 452d0de7e4aSpeixiaokun hyper_arb1.io.in(i).valid := is_hptw_req(i) 453d0de7e4aSpeixiaokun } 454d0de7e4aSpeixiaokun val hyper_arb2 = Module(new RRArbiter(new LLPTWEntry(), l2tlbParams.llptwsize)) 455d0de7e4aSpeixiaokun for(i <- 0 until l2tlbParams.llptwsize) { 456d0de7e4aSpeixiaokun hyper_arb2.io.in(i).bits := entries(i) 457d0de7e4aSpeixiaokun hyper_arb2.io.in(i).valid := is_last_hptw_req(i) 458d0de7e4aSpeixiaokun } 45992e3bfefSLemover 460f3034303SHaoyuan Feng val cache_ptr = ParallelMux(is_cache, (0 until l2tlbParams.llptwsize).map(_.U(log2Up(l2tlbParams.llptwsize).W))) 4617797f035SbugGenerator 46292e3bfefSLemover // duplicate req 46392e3bfefSLemover // to_wait: wait for the last to access mem, set to mem_resp 46492e3bfefSLemover // to_cache: the last is back just right now, set to mem_cache 46592e3bfefSLemover val dup_vec = state.indices.map(i => 466cca17e78Speixiaokun dup(io.in.bits.req_info.vpn, entries(i).req_info.vpn) && io.in.bits.req_info.s2xlate === entries(i).req_info.s2xlate 46792e3bfefSLemover ) 468cca17e78Speixiaokun 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 46992e3bfefSLemover val dup_vec_wait = dup_vec.zip(is_waiting).map{case (d, w) => d && w} // dup with "mem_waiting" entres, sending mem req already 47092e3bfefSLemover 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 471951f37e5Speixiaokun val dup_vec_last_hptw = dup_vec.zipWithIndex.map{case (d, i) => d && (is_last_hptw_req(i) || is_last_hptw_resp(i))} 47292e3bfefSLemover val wait_id = Mux(dup_req_fire, mem_arb.io.chosen, ParallelMux(dup_vec_wait zip entries.map(_.wait_id))) 473935edac4STang 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 47492e3bfefSLemover val to_wait = Cat(dup_vec_wait).orR || dup_req_fire 4754895c345Speixiaokun val to_mem_out = dup_wait_resp && entries(io.mem.resp.bits.id).req_info.s2xlate === noS2xlate 4764895c345Speixiaokun val to_last_hptw_req = dup_wait_resp && entries(io.mem.resp.bits.id).req_info.s2xlate =/= noS2xlate 477951f37e5Speixiaokun val to_cache = Cat(dup_vec_having).orR || Cat(dup_vec_last_hptw).orR 4784358f287Speixiaokun val to_hptw_req = io.in.bits.req_info.s2xlate =/= noS2xlate 4797797f035SbugGenerator XSError(RegNext(dup_req_fire && Cat(dup_vec_wait).orR, init = false.B), "mem req but some entries already waiting, should not happed") 48092e3bfefSLemover 481935edac4STang Haojin XSError(io.in.fire && ((to_mem_out && to_cache) || (to_wait && to_cache)), "llptw enq, to cache conflict with to mem") 48292e3bfefSLemover val mem_resp_hit = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(false.B))) 4837274ec5cSpeixiaokun val enq_state_normal = MuxCase(state_addr_check, Seq( 4847274ec5cSpeixiaokun to_mem_out -> state_mem_out, // same to the blew, but the mem resp now 485871d1438Speixiaokun to_last_hptw_req -> state_last_hptw_req, 4867274ec5cSpeixiaokun to_wait -> state_mem_waiting, 4877274ec5cSpeixiaokun to_cache -> state_cache, 488871d1438Speixiaokun to_hptw_req -> state_hptw_req 4897274ec5cSpeixiaokun )) 4907797f035SbugGenerator val enq_state = Mux(from_pre(io.in.bits.req_info.source) && enq_state_normal =/= state_addr_check, state_idle, enq_state_normal) 491935edac4STang Haojin when (io.in.fire) { 49292e3bfefSLemover // if prefetch req does not need mem access, just give it up. 49392e3bfefSLemover // so there will be at most 1 + FilterSize entries that needs re-access page cache 49492e3bfefSLemover // so 2 + FilterSize is enough to avoid dead-lock 4957797f035SbugGenerator state(enq_ptr) := enq_state 49692e3bfefSLemover entries(enq_ptr).req_info := io.in.bits.req_info 49792e3bfefSLemover entries(enq_ptr).ppn := io.in.bits.ppn 49892e3bfefSLemover entries(enq_ptr).wait_id := Mux(to_wait, wait_id, enq_ptr) 49992e3bfefSLemover entries(enq_ptr).af := false.B 50092e3bfefSLemover mem_resp_hit(enq_ptr) := to_mem_out 50192e3bfefSLemover } 5027797f035SbugGenerator 5037797f035SbugGenerator val enq_ptr_reg = RegNext(enq_ptr) 5043222d00fSpeixiaokun val need_addr_check = RegNext(enq_state === state_addr_check && io.in.fire && !flush) 5057274ec5cSpeixiaokun 5060214776eSpeixiaokun val hasHptwResp = ParallelOR(state.map(_ === state_hptw_resp)).asBool 5077274ec5cSpeixiaokun val hptw_resp_ptr_reg = RegNext(io.hptw.resp.bits.id) 508a664078aSpeixiaokun val hptw_need_addr_check = RegNext(hasHptwResp && io.hptw.resp.fire && !flush) && state(hptw_resp_ptr_reg) === state_addr_check 509d0de7e4aSpeixiaokun 510ce5f4200SGuanghui Hu val ptes = io.mem.resp.bits.value.asTypeOf(Vec(blockBits / XLEN, new PteBundle())) 511b24e0a78Speixiaokun val gpaddr = MakeGPAddr(io.in.bits.ppn, getVpnn(io.in.bits.req_info.vpn, 0)) 51282e4705bSpeixiaokun val hptw_resp = entries(hptw_resp_ptr_reg).hptw_resp 513cda84113Speixiaokun val hpaddr = Cat(hptw_resp.genPPNS2(get_pn(gpaddr)), get_off(gpaddr)) 5147274ec5cSpeixiaokun val addr = MakeAddr(io.in.bits.ppn, getVpnn(io.in.bits.req_info.vpn, 0)) 5157274ec5cSpeixiaokun io.pmp.req.valid := need_addr_check || hptw_need_addr_check 51682e4705bSpeixiaokun io.pmp.req.bits.addr := Mux(hptw_need_addr_check, hpaddr, addr) 5177797f035SbugGenerator io.pmp.req.bits.cmd := TlbCmd.read 5187797f035SbugGenerator io.pmp.req.bits.size := 3.U // TODO: fix it 5197797f035SbugGenerator val pmp_resp_valid = io.pmp.req.valid // same cycle 5207797f035SbugGenerator when (pmp_resp_valid) { 5217797f035SbugGenerator // NOTE: when pmp resp but state is not addr check, then the entry is dup with other entry, the state was changed before 5227797f035SbugGenerator // when dup with the req-ing entry, set to mem_waiting (above codes), and the ld must be false, so dontcare 5237274ec5cSpeixiaokun val ptr = Mux(hptw_need_addr_check, hptw_resp_ptr_reg, enq_ptr_reg); 5247797f035SbugGenerator val accessFault = io.pmp.resp.ld || io.pmp.resp.mmio 5257274ec5cSpeixiaokun entries(ptr).af := accessFault 5267274ec5cSpeixiaokun state(ptr) := Mux(accessFault, state_mem_out, state_mem_req) 5277797f035SbugGenerator } 5287797f035SbugGenerator 529935edac4STang Haojin when (mem_arb.io.out.fire) { 53092e3bfefSLemover for (i <- state.indices) { 531*ec78ed87Speixiaokun when (state(i) =/= state_idle && state(i) =/= state_mem_out && state(i) =/= state_last_hptw_req && state(i) =/= state_last_hptw_resp 532*ec78ed87Speixiaokun && entries(i).req_info.s2xlate === mem_arb.io.out.bits.req_info.s2xlate 533*ec78ed87Speixiaokun && dup(entries(i).req_info.vpn, mem_arb.io.out.bits.req_info.vpn)) { 53492e3bfefSLemover // NOTE: "dup enq set state to mem_wait" -> "sending req set other dup entries to mem_wait" 53592e3bfefSLemover state(i) := state_mem_waiting 53692e3bfefSLemover entries(i).wait_id := mem_arb.io.chosen 53792e3bfefSLemover } 53892e3bfefSLemover } 53992e3bfefSLemover } 540935edac4STang Haojin when (io.mem.resp.fire) { 54192e3bfefSLemover state.indices.map{i => 54292e3bfefSLemover when (state(i) === state_mem_waiting && io.mem.resp.bits.id === entries(i).wait_id) { 5434358f287Speixiaokun state(i) := Mux(entries(i).req_info.s2xlate =/= noS2xlate, state_last_hptw_req, state_mem_out) 54492e3bfefSLemover mem_resp_hit(i) := true.B 5454358f287Speixiaokun val req_paddr = MakeAddr(entries(i).ppn, getVpnn(entries(i).req_info.vpn, 0)) 5464358f287Speixiaokun val req_hpaddr = MakeAddr(entries(i).hptw_resp.genPPNS2(get_pn(req_paddr)), getVpnn(entries(i).req_info.vpn, 0)) 5474358f287Speixiaokun val index = Mux(entries(i).req_info.s2xlate === allStage, req_hpaddr, req_paddr)(log2Up(l2tlbParams.blockBytes)-1, log2Up(XLEN/8)) 5484358f287Speixiaokun entries(i).ppn := ptes(index).ppn // for last stage 2 translation 549ad0d9d89Speixiaokun } 550ad0d9d89Speixiaokun } 551ad0d9d89Speixiaokun } 552ad0d9d89Speixiaokun 5533222d00fSpeixiaokun when (hyper_arb1.io.out.fire) { 554d0de7e4aSpeixiaokun for (i <- state.indices) { 5554358f287Speixiaokun when (state(i) === state_hptw_req && entries(i).ppn === hyper_arb1.io.out.bits.ppn && entries(i).req_info.s2xlate =/= noS2xlate && hyper_arb1.io.chosen === i.U) { 556d0de7e4aSpeixiaokun state(i) := state_hptw_resp 557d0de7e4aSpeixiaokun entries(i).wait_id := hyper_arb1.io.chosen 558d0de7e4aSpeixiaokun } 559d0de7e4aSpeixiaokun } 560d0de7e4aSpeixiaokun } 561d0de7e4aSpeixiaokun 5623222d00fSpeixiaokun when (hyper_arb2.io.out.fire) { 563d0de7e4aSpeixiaokun for (i <- state.indices) { 5644358f287Speixiaokun when (state(i) === state_last_hptw_req && entries(i).ppn === hyper_arb2.io.out.bits.ppn && entries(i).req_info.s2xlate =/= noS2xlate && hyper_arb2.io.chosen === i.U) { 565d0de7e4aSpeixiaokun state(i) := state_last_hptw_resp 566d0de7e4aSpeixiaokun entries(i).wait_id := hyper_arb2.io.chosen 567d0de7e4aSpeixiaokun } 568d0de7e4aSpeixiaokun } 569d0de7e4aSpeixiaokun } 570d0de7e4aSpeixiaokun 5713222d00fSpeixiaokun when (io.hptw.resp.fire) { 572d0de7e4aSpeixiaokun for (i <- state.indices) { 573d0de7e4aSpeixiaokun when (state(i) === state_hptw_resp && io.hptw.resp.bits.id === entries(i).wait_id) { 574*ec78ed87Speixiaokun 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)) 5757f96e195Speixiaokun val waiting_index = ParallelMux(need_to_waiting_vec zip entries.map(_.wait_id)) 5767f96e195Speixiaokun state(i) := Mux(Cat(need_to_waiting_vec).orR, state_mem_waiting, state_addr_check) 577dc05c713Speixiaokun entries(i).hptw_resp := io.hptw.resp.bits.h_resp 5787f96e195Speixiaokun entries(i).wait_id := Mux(Cat(need_to_waiting_vec).orR, waiting_index, entries(i).wait_id) 579d0de7e4aSpeixiaokun } 580d0de7e4aSpeixiaokun when (state(i) === state_last_hptw_resp && io.hptw.resp.bits.id === entries(i).wait_id) { 581d0de7e4aSpeixiaokun state(i) := state_mem_out 582dc05c713Speixiaokun entries(i).hptw_resp := io.hptw.resp.bits.h_resp 583d0de7e4aSpeixiaokun } 584d0de7e4aSpeixiaokun } 585d0de7e4aSpeixiaokun } 586d0de7e4aSpeixiaokun 587935edac4STang Haojin when (io.out.fire) { 58892e3bfefSLemover assert(state(mem_ptr) === state_mem_out) 58992e3bfefSLemover state(mem_ptr) := state_idle 59092e3bfefSLemover } 59192e3bfefSLemover mem_resp_hit.map(a => when (a) { a := false.B } ) 59292e3bfefSLemover 5937797f035SbugGenerator when (io.cache.fire) { 5947797f035SbugGenerator state(cache_ptr) := state_idle 59592e3bfefSLemover } 5967797f035SbugGenerator XSError(io.out.fire && io.cache.fire && (mem_ptr === cache_ptr), "mem resp and cache fire at the same time at same entry") 59792e3bfefSLemover 59892e3bfefSLemover when (flush) { 59992e3bfefSLemover state.map(_ := state_idle) 60092e3bfefSLemover } 60192e3bfefSLemover 60292e3bfefSLemover io.in.ready := !full 60392e3bfefSLemover 604935edac4STang Haojin io.out.valid := ParallelOR(is_having).asBool 60592e3bfefSLemover io.out.bits.req_info := entries(mem_ptr).req_info 60692e3bfefSLemover io.out.bits.id := mem_ptr 60792e3bfefSLemover io.out.bits.af := entries(mem_ptr).af 608dc05c713Speixiaokun io.out.bits.h_resp := entries(mem_ptr).hptw_resp 609d0de7e4aSpeixiaokun 610dc05c713Speixiaokun val hptw_req_gvpn_1 = hyper_arb1.io.out.bits.ppn // first stage 2 translation 611ad0d9d89Speixiaokun val hptw_req_gvpn_2 = hyper_arb2.io.out.bits.ppn // last stage 2 translation 6126967f5d5Speixiaokun io.hptw.req.valid := (hyper_arb1.io.out.fire || hyper_arb2.io.out.fire) && !flush 613dc05c713Speixiaokun io.hptw.req.bits.gvpn := Mux(hyper_arb1.io.out.valid, hptw_req_gvpn_1, hptw_req_gvpn_2) 614d0de7e4aSpeixiaokun io.hptw.req.bits.id := Mux(hyper_arb1.io.out.valid, hyper_arb1.io.chosen, hyper_arb2.io.chosen) 615eb4bf3f2Speixiaokun io.hptw.req.bits.source := Mux(hyper_arb1.io.out.valid, hyper_arb1.io.out.bits.req_info.source, hyper_arb2.io.out.bits.req_info.source) 616b7bdb307Speixiaokun hyper_arb1.io.out.ready := io.hptw.req.ready && !(Cat(is_hptw_resp).orR) 617b7bdb307Speixiaokun hyper_arb2.io.out.ready := io.hptw.req.ready && !(Cat(is_last_hptw_resp).orR) 61892e3bfefSLemover 61992e3bfefSLemover io.mem.req.valid := mem_arb.io.out.valid && !flush 620dc05c713Speixiaokun val mem_paddr = MakeAddr(mem_arb.io.out.bits.ppn, getVpnn(mem_arb.io.out.bits.req_info.vpn, 0)) 621cda84113Speixiaokun 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)) 6224358f287Speixiaokun io.mem.req.bits.addr := Mux(mem_arb.io.out.bits.req_info.s2xlate =/= noS2xlate, mem_hpaddr, mem_paddr) 62392e3bfefSLemover io.mem.req.bits.id := mem_arb.io.chosen 62492e3bfefSLemover mem_arb.io.out.ready := io.mem.req.ready 625933ec998Speixiaokun val mem_refill_id = RegNext(io.mem.resp.bits.id(log2Up(l2tlbParams.llptwsize)-1, 0)) 626933ec998Speixiaokun io.mem.refill := entries(mem_refill_id).req_info 627933ec998Speixiaokun io.mem.refill.s2xlate := Mux(entries(mem_refill_id).req_info.s2xlate === noS2xlate, noS2xlate, onlyStage1) // llptw refill the pte of stage 1 62892e3bfefSLemover io.mem.buffer_it := mem_resp_hit 62992e3bfefSLemover io.mem.enq_ptr := enq_ptr 63092e3bfefSLemover 6317797f035SbugGenerator io.cache.valid := Cat(is_cache).orR 6327797f035SbugGenerator io.cache.bits := ParallelMux(is_cache, entries.map(_.req_info)) 6337797f035SbugGenerator 634935edac4STang Haojin XSPerfAccumulate("llptw_in_count", io.in.fire) 63592e3bfefSLemover XSPerfAccumulate("llptw_in_block", io.in.valid && !io.in.ready) 63692e3bfefSLemover for (i <- 0 until 7) { 637935edac4STang Haojin XSPerfAccumulate(s"enq_state${i}", io.in.fire && enq_state === i.U) 63892e3bfefSLemover } 63992e3bfefSLemover for (i <- 0 until (l2tlbParams.llptwsize + 1)) { 64092e3bfefSLemover XSPerfAccumulate(s"util${i}", PopCount(is_emptys.map(!_)) === i.U) 64192e3bfefSLemover XSPerfAccumulate(s"mem_util${i}", PopCount(is_mems) === i.U) 64292e3bfefSLemover XSPerfAccumulate(s"waiting_util${i}", PopCount(is_waiting) === i.U) 64392e3bfefSLemover } 644935edac4STang Haojin XSPerfAccumulate("mem_count", io.mem.req.fire) 64592e3bfefSLemover XSPerfAccumulate("mem_cycle", PopCount(is_waiting) =/= 0.U) 64692e3bfefSLemover XSPerfAccumulate("blocked_in", io.in.valid && !io.in.ready) 64792e3bfefSLemover 64892e3bfefSLemover for (i <- 0 until l2tlbParams.llptwsize) { 64992e3bfefSLemover TimeOutAssert(state(i) =/= state_idle, timeOutThreshold, s"missqueue time out no out ${i}") 65092e3bfefSLemover } 65192e3bfefSLemover 65292e3bfefSLemover val perfEvents = Seq( 653935edac4STang Haojin ("tlbllptw_incount ", io.in.fire ), 65492e3bfefSLemover ("tlbllptw_inblock ", io.in.valid && !io.in.ready), 655935edac4STang Haojin ("tlbllptw_memcount ", io.mem.req.fire ), 65692e3bfefSLemover ("tlbllptw_memcycle ", PopCount(is_waiting) ), 65792e3bfefSLemover ) 65892e3bfefSLemover generatePerfEvent() 65992e3bfefSLemover} 660d0de7e4aSpeixiaokun 661d0de7e4aSpeixiaokun/*========================= HPTW ==============================*/ 662d0de7e4aSpeixiaokun 663d0de7e4aSpeixiaokun/** HPTW : Hypervisor Page Table Walker 664d0de7e4aSpeixiaokun * the page walker take the virtual machine's page walk. 665d0de7e4aSpeixiaokun * guest physical address translation, guest physical address -> host physical address 666d0de7e4aSpeixiaokun **/ 667d0de7e4aSpeixiaokunclass HPTWIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 668d0de7e4aSpeixiaokun val req = Flipped(DecoupledIO(new Bundle { 669eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 670d0de7e4aSpeixiaokun val id = UInt(log2Up(l2tlbParams.llptwsize).W) 67182978df9Speixiaokun val gvpn = UInt(vpnLen.W) 6726315ba2aSpeixiaokun val ppn = UInt(ppnLen.W) 673d0de7e4aSpeixiaokun val l1Hit = Bool() 674d0de7e4aSpeixiaokun val l2Hit = Bool() 675d0de7e4aSpeixiaokun })) 676c2b430edSpeixiaokun val resp = DecoupledIO(new Bundle { 677eb4bf3f2Speixiaokun val source = UInt(bSourceWidth.W) 678d0de7e4aSpeixiaokun val resp = Output(new HptwResp()) 679d0de7e4aSpeixiaokun val id = Output(UInt(bMemID.W)) 680d0de7e4aSpeixiaokun }) 681d0de7e4aSpeixiaokun 682d0de7e4aSpeixiaokun val mem = new Bundle { 683d0de7e4aSpeixiaokun val req = DecoupledIO(new L2TlbMemReqBundle()) 684d0de7e4aSpeixiaokun val resp = Flipped(ValidIO(UInt(XLEN.W))) 685d0de7e4aSpeixiaokun val mask = Input(Bool()) 686d0de7e4aSpeixiaokun } 687d0de7e4aSpeixiaokun val refill = Output(new Bundle { 688d0de7e4aSpeixiaokun val req_info = new L2TlbInnerBundle() 689d0de7e4aSpeixiaokun val level = UInt(log2Up(Level).W) 690d0de7e4aSpeixiaokun }) 691d0de7e4aSpeixiaokun val pmp = new Bundle { 692d0de7e4aSpeixiaokun val req = ValidIO(new PMPReqBundle()) 693d0de7e4aSpeixiaokun val resp = Flipped(new PMPRespBundle()) 694d0de7e4aSpeixiaokun } 695d0de7e4aSpeixiaokun} 696d0de7e4aSpeixiaokun 697d0de7e4aSpeixiaokunclass HPTW()(implicit p: Parameters) extends XSModule with HasPtwConst { 698d0de7e4aSpeixiaokun val io = IO(new HPTWIO) 699d0de7e4aSpeixiaokun val hgatp = io.csr.hgatp 700d0de7e4aSpeixiaokun val sfence = io.sfence 701d0de7e4aSpeixiaokun val flush = sfence.valid || hgatp.changed 702d0de7e4aSpeixiaokun 703d0de7e4aSpeixiaokun val level = RegInit(0.U(log2Up(Level).W)) 704d0de7e4aSpeixiaokun val gpaddr = Reg(UInt(GPAddrBits.W)) 7054c4af37cSpeixiaokun val req_ppn = Reg(UInt(ppnLen.W)) 706d0de7e4aSpeixiaokun val vpn = gpaddr(GPAddrBits-1, offLen) 707d0de7e4aSpeixiaokun val levelNext = level + 1.U 708d0de7e4aSpeixiaokun val l1Hit = Reg(Bool()) 709d0de7e4aSpeixiaokun val l2Hit = Reg(Bool()) 710b24e0a78Speixiaokun val pg_base = MakeGPAddr(hgatp.ppn, getGVpnn(vpn, 2.U)) // for l0 711d0de7e4aSpeixiaokun// val pte = io.mem.resp.bits.MergeRespToPte() 712d0de7e4aSpeixiaokun val pte = io.mem.resp.bits.asTypeOf(new PteBundle().cloneType) 7134c4af37cSpeixiaokun val ppn_l1 = Mux(l1Hit, req_ppn, pte.ppn) 7144c4af37cSpeixiaokun val ppn_l2 = Mux(l2Hit, req_ppn, pte.ppn) 7156315ba2aSpeixiaokun val ppn = Mux(level === 1.U, ppn_l1, ppn_l2) //for l1 and l2 7166315ba2aSpeixiaokun val p_pte = MakeAddr(ppn, getVpnn(vpn, 2.U - level)) 717d0de7e4aSpeixiaokun val mem_addr = Mux(level === 0.U, pg_base, p_pte) 718d0de7e4aSpeixiaokun 719d0de7e4aSpeixiaokun //s/w register 720d0de7e4aSpeixiaokun val s_pmp_check = RegInit(true.B) 721d0de7e4aSpeixiaokun val s_mem_req = RegInit(true.B) 722d0de7e4aSpeixiaokun val w_mem_resp = RegInit(true.B) 723d0de7e4aSpeixiaokun val idle = RegInit(true.B) 72403c1129fSpeixiaokun val mem_addr_update = RegInit(false.B) 725d0de7e4aSpeixiaokun val finish = WireInit(false.B) 726d0de7e4aSpeixiaokun 727d0de7e4aSpeixiaokun val sent_to_pmp = !idle && (!s_pmp_check || mem_addr_update) && !finish 728d0de7e4aSpeixiaokun val pageFault = pte.isPf(level) 729d0de7e4aSpeixiaokun val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, sent_to_pmp) 730d0de7e4aSpeixiaokun 731d0de7e4aSpeixiaokun val ppn_af = pte.isAf() 732d0de7e4aSpeixiaokun val find_pte = pte.isLeaf() || ppn_af || pageFault 733d0de7e4aSpeixiaokun 734d0de7e4aSpeixiaokun val resp_valid = !idle && mem_addr_update && ((w_mem_resp && find_pte) || (s_pmp_check && accessFault)) 735d0de7e4aSpeixiaokun val id = Reg(UInt(log2Up(l2tlbParams.llptwsize).W)) 7363222d00fSpeixiaokun val source = RegEnable(io.req.bits.source, io.req.fire) 737eb4bf3f2Speixiaokun 738d0de7e4aSpeixiaokun io.req.ready := idle 739eb4bf3f2Speixiaokun val resp = Wire(new HptwResp()) 740d0de7e4aSpeixiaokun resp.apply(pageFault && !accessFault && !ppn_af, accessFault || ppn_af, level, pte, vpn, hgatp.asid) 741d0de7e4aSpeixiaokun io.resp.valid := resp_valid 742d0de7e4aSpeixiaokun io.resp.bits.id := id 743d0de7e4aSpeixiaokun io.resp.bits.resp := resp 744eb4bf3f2Speixiaokun io.resp.bits.source := source 745d0de7e4aSpeixiaokun 746d0de7e4aSpeixiaokun io.pmp.req.valid := DontCare 747d0de7e4aSpeixiaokun io.pmp.req.bits.addr := mem_addr 748d0de7e4aSpeixiaokun io.pmp.req.bits.size := 3.U 749d0de7e4aSpeixiaokun io.pmp.req.bits.cmd := TlbCmd.read 750d0de7e4aSpeixiaokun 751d0de7e4aSpeixiaokun io.mem.req.valid := !s_mem_req && !io.mem.mask && !accessFault && s_pmp_check 752d0de7e4aSpeixiaokun io.mem.req.bits.addr := mem_addr 753d0de7e4aSpeixiaokun io.mem.req.bits.id := HptwReqId.U(bMemID.W) 754d0de7e4aSpeixiaokun 75582978df9Speixiaokun io.refill.req_info.vpn := vpn 756d0de7e4aSpeixiaokun io.refill.level := level 757eb4bf3f2Speixiaokun io.refill.req_info.source := source 758eb4bf3f2Speixiaokun io.refill.req_info.s2xlate := onlyStage2 759d0de7e4aSpeixiaokun when (idle){ 7603222d00fSpeixiaokun when(io.req.fire){ 761d0de7e4aSpeixiaokun level := Mux(io.req.bits.l2Hit, 2.U, Mux(io.req.bits.l1Hit, 1.U, 0.U)) 762d0de7e4aSpeixiaokun idle := false.B 763d0de7e4aSpeixiaokun gpaddr := Cat(io.req.bits.gvpn, 0.U(offLen.W)) 764d0de7e4aSpeixiaokun accessFault := false.B 765d0de7e4aSpeixiaokun s_pmp_check := false.B 766d0de7e4aSpeixiaokun id := io.req.bits.id 7674c4af37cSpeixiaokun req_ppn := io.req.bits.ppn 768d0de7e4aSpeixiaokun l1Hit := io.req.bits.l1Hit 769d0de7e4aSpeixiaokun l2Hit := io.req.bits.l2Hit 770d0de7e4aSpeixiaokun } 771d0de7e4aSpeixiaokun } 772d0de7e4aSpeixiaokun 773d0de7e4aSpeixiaokun when(sent_to_pmp && !mem_addr_update){ 774d0de7e4aSpeixiaokun s_mem_req := false.B 775d0de7e4aSpeixiaokun s_pmp_check := true.B 776d0de7e4aSpeixiaokun } 777d0de7e4aSpeixiaokun 778d0de7e4aSpeixiaokun when(accessFault && !idle){ 779d0de7e4aSpeixiaokun s_pmp_check := true.B 780d0de7e4aSpeixiaokun s_mem_req := true.B 781d0de7e4aSpeixiaokun w_mem_resp := true.B 782d0de7e4aSpeixiaokun mem_addr_update := true.B 783d0de7e4aSpeixiaokun } 784d0de7e4aSpeixiaokun 7853222d00fSpeixiaokun when(io.mem.req.fire){ 786d0de7e4aSpeixiaokun s_mem_req := true.B 787d0de7e4aSpeixiaokun w_mem_resp := false.B 788d0de7e4aSpeixiaokun } 789d0de7e4aSpeixiaokun 7903222d00fSpeixiaokun when(io.mem.resp.fire && !w_mem_resp){ 791d0de7e4aSpeixiaokun w_mem_resp := true.B 792d0de7e4aSpeixiaokun mem_addr_update := true.B 793d0de7e4aSpeixiaokun } 794d0de7e4aSpeixiaokun 795d0de7e4aSpeixiaokun when(mem_addr_update){ 796d0de7e4aSpeixiaokun when(!(find_pte || accessFault)){ 797d0de7e4aSpeixiaokun level := levelNext 798d0de7e4aSpeixiaokun s_mem_req := false.B 799d0de7e4aSpeixiaokun mem_addr_update := false.B 800d0de7e4aSpeixiaokun }.elsewhen(resp_valid){ 8013222d00fSpeixiaokun when(io.resp.fire){ 802d0de7e4aSpeixiaokun idle := true.B 803d0de7e4aSpeixiaokun mem_addr_update := false.B 804d0de7e4aSpeixiaokun accessFault := false.B 805d0de7e4aSpeixiaokun } 806d0de7e4aSpeixiaokun finish := true.B 807d0de7e4aSpeixiaokun } 808d0de7e4aSpeixiaokun } 809d0de7e4aSpeixiaokun}