1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.cache.mmu 18 19import org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import xiangshan.cache.{HasDCacheParameters, MemoryOpConstants} 24import utils._ 25import utility._ 26import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 27import freechips.rocketchip.tilelink._ 28import xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle} 29 30/** Page Table Walk is divided into two parts 31 * One, PTW: page walk for pde, except for leaf entries, one by one 32 * Two, LLPTW: page walk for pte, only the leaf entries(4KB), in parallel 33 */ 34 35 36/** PTW : page table walker 37 * a finite state machine 38 * only take 1GB and 2MB page walks 39 * or in other words, except the last level(leaf) 40 **/ 41class PTWIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 42 val req = Flipped(DecoupledIO(new Bundle { 43 val req_info = new L2TlbInnerBundle() 44 val l3Hit = if (EnableSv48) Some(new Bool()) else None 45 val l2Hit = Bool() 46 val ppn = UInt(ptePPNLen.W) 47 val stage1Hit = Bool() 48 val stage1 = new PtwMergeResp 49 })) 50 val resp = DecoupledIO(new Bundle { 51 val source = UInt(bSourceWidth.W) 52 val s2xlate = UInt(2.W) 53 val resp = new PtwMergeResp 54 val h_resp = new HptwResp 55 }) 56 57 val llptw = DecoupledIO(new LLPTWInBundle()) 58 // NOTE: llptw change from "connect to llptw" to "connect to page cache" 59 // to avoid corner case that caused duplicate entries 60 61 val hptw = new Bundle { 62 val req = DecoupledIO(new Bundle { 63 val source = UInt(bSourceWidth.W) 64 val id = UInt(log2Up(l2tlbParams.llptwsize).W) 65 val gvpn = UInt(ptePPNLen.W) 66 }) 67 val resp = Flipped(Valid(new Bundle { 68 val h_resp = Output(new HptwResp) 69 })) 70 } 71 val mem = new Bundle { 72 val req = DecoupledIO(new L2TlbMemReqBundle()) 73 val resp = Flipped(ValidIO(UInt(XLEN.W))) 74 val mask = Input(Bool()) 75 } 76 val pmp = new Bundle { 77 val req = ValidIO(new PMPReqBundle()) 78 val resp = Flipped(new PMPRespBundle()) 79 } 80 81 val refill = Output(new Bundle { 82 val req_info = new L2TlbInnerBundle() 83 val level = UInt(log2Up(Level + 1).W) 84 }) 85} 86 87class PTW()(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents { 88 val io = IO(new PTWIO) 89 val sfence = io.sfence 90 val mem = io.mem 91 val req_s2xlate = Reg(UInt(2.W)) 92 val enableS2xlate = req_s2xlate =/= noS2xlate 93 val onlyS1xlate = req_s2xlate === onlyStage1 94 val onlyS2xlate = req_s2xlate === onlyStage2 95 96 val satp = Wire(new TlbSatpBundle()) 97 when (io.req.fire) { 98 satp := Mux(io.req.bits.req_info.s2xlate =/= noS2xlate, io.csr.vsatp, io.csr.satp) 99 } .otherwise { 100 satp := Mux(enableS2xlate, io.csr.vsatp, io.csr.satp) 101 } 102 103 val mode = satp.mode 104 val hgatp = io.csr.hgatp 105 val flush = io.sfence.valid || io.csr.satp.changed || io.csr.vsatp.changed || io.csr.hgatp.changed 106 val s2xlate = enableS2xlate && !onlyS1xlate 107 val level = RegInit(3.U(log2Up(Level + 1).W)) 108 val af_level = RegInit(3.U(log2Up(Level + 1).W)) // access fault return this level 109 val gpf_level = RegInit(3.U(log2Up(Level + 1).W)) 110 val ppn = Reg(UInt(ptePPNLen.W)) 111 val vpn = Reg(UInt(vpnLen.W)) // vpn or gvpn(onlyS2xlate) 112 val levelNext = level - 1.U 113 val l3Hit = Reg(Bool()) 114 val l2Hit = Reg(Bool()) 115 val pte = mem.resp.bits.asTypeOf(new PteBundle()) 116 117 // s/w register 118 val s_pmp_check = RegInit(true.B) 119 val s_mem_req = RegInit(true.B) 120 val s_llptw_req = RegInit(true.B) 121 val w_mem_resp = RegInit(true.B) 122 val s_hptw_req = RegInit(true.B) 123 val w_hptw_resp = RegInit(true.B) 124 val s_last_hptw_req = RegInit(true.B) 125 val w_last_hptw_resp = RegInit(true.B) 126 // for updating "level" 127 val mem_addr_update = RegInit(false.B) 128 129 val idle = RegInit(true.B) 130 val finish = WireInit(false.B) 131 val sent_to_pmp = idle === false.B && (s_pmp_check === false.B || mem_addr_update) && !finish 132 133 val pageFault = pte.isPf(level) 134 val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, false.B, sent_to_pmp) 135 136 val hptw_pageFault = RegInit(false.B) 137 val hptw_accessFault = RegInit(false.B) 138 val last_s2xlate = RegInit(false.B) 139 val stage1Hit = RegEnable(io.req.bits.stage1Hit, io.req.fire) 140 val stage1 = RegEnable(io.req.bits.stage1, io.req.fire) 141 val hptw_resp_stage2 = Reg(Bool()) 142 143 val ppn_af = Mux(enableS2xlate, Mux(onlyS1xlate, pte.isAf() && !pte.isStage1Gpf(io.csr.vsatp.mode), 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 144 val find_pte = pte.isLeaf() || ppn_af || pageFault 145 val to_find_pte = level === 1.U && find_pte === false.B 146 val source = RegEnable(io.req.bits.req_info.source, io.req.fire) 147 148 val l3addr = Wire(UInt(PAddrBits.W)) 149 val l2addr = Wire(UInt(PAddrBits.W)) 150 val l1addr = Wire(UInt(PAddrBits.W)) 151 val mem_addr = Wire(UInt(PAddrBits.W)) 152 153 l3addr := MakeAddr(satp.ppn, getVpnn(vpn, 3)) 154 if (EnableSv48) { 155 when (mode === Sv48) { 156 l2addr := MakeAddr(Mux(l3Hit, ppn, pte.getPPN()), getVpnn(vpn, 2)) 157 } .otherwise { 158 l2addr := MakeAddr(satp.ppn, getVpnn(vpn, 2)) 159 } 160 } else { 161 l2addr := MakeAddr(satp.ppn, getVpnn(vpn, 2)) 162 } 163 l1addr := MakeAddr(Mux(l2Hit, ppn, pte.getPPN()), getVpnn(vpn, 1)) 164 mem_addr := Mux(af_level === 3.U, l3addr, Mux(af_level === 2.U, l2addr, l1addr)) 165 166 val hptw_resp = Reg(new HptwResp) 167 val gpaddr = MuxCase(mem_addr, Seq( 168 stage1Hit -> Cat(stage1.genPPN(), 0.U(offLen.W)), 169 onlyS2xlate -> Cat(vpn, 0.U(offLen.W)), 170 !s_last_hptw_req -> Cat(MuxLookup(level, pte.getPPN())(Seq( 171 3.U -> Cat(pte.getPPN()(ptePPNLen - 1, vpnnLen * 3), vpn(vpnnLen * 3 - 1, 0)), 172 2.U -> Cat(pte.getPPN()(ptePPNLen - 1, vpnnLen * 2), vpn(vpnnLen * 2 - 1, 0)), 173 1.U -> Cat(pte.getPPN()(ptePPNLen - 1, vpnnLen), vpn(vpnnLen - 1, 0) 174 ))), 175 0.U(offLen.W)) 176 )) 177 val gvpn_gpf = Mux(enableS2xlate && io.csr.hgatp.mode === Sv39x4, gpaddr(gpaddr.getWidth - 1, GPAddrBitsSv39x4) =/= 0.U, Mux(enableS2xlate && io.csr.hgatp.mode === Sv48x4, gpaddr(gpaddr.getWidth - 1, GPAddrBitsSv48x4) =/= 0.U, false.B)) 178 val guestFault = hptw_pageFault || hptw_accessFault || gvpn_gpf 179 val hpaddr = Cat(hptw_resp.genPPNS2(get_pn(gpaddr)), get_off(gpaddr)) 180 val fake_h_resp = 0.U.asTypeOf(new HptwResp) 181 fake_h_resp.entry.tag := get_pn(gpaddr) 182 fake_h_resp.entry.vmid.map(_ := io.csr.hgatp.vmid) 183 fake_h_resp.gpf := true.B 184 185 val pte_valid = RegInit(false.B) // avoid l1tlb pf from stage1 when gpf happens in the first s2xlate in PTW 186 val fake_pte = 0.U.asTypeOf(new PteBundle()) 187 fake_pte.perm.v := false.B // tell L1TLB this is fake pte 188 fake_pte.perm.r := true.B 189 fake_pte.perm.w := true.B 190 fake_pte.perm.x := true.B 191 fake_pte.perm.a := true.B 192 fake_pte.perm.d := true.B 193 fake_pte.ppn := ppn(ppnLen - 1, 0) 194 fake_pte.ppn_high := ppn(ptePPNLen - 1, ppnLen) 195 196 io.req.ready := idle 197 val ptw_resp = Wire(new PtwMergeResp) 198 ptw_resp.apply(Mux(pte_valid, pageFault && !accessFault && !ppn_af, false.B), accessFault || ppn_af, Mux(accessFault, af_level, Mux(guestFault, gpf_level, level)), Mux(pte_valid, pte, fake_pte), vpn, satp.asid, hgatp.vmid, vpn(sectortlbwidth - 1, 0), not_super = false) 199 200 val normal_resp = idle === false.B && mem_addr_update && !last_s2xlate && (guestFault || (w_mem_resp && find_pte) || (s_pmp_check && accessFault) || onlyS2xlate ) 201 val stageHit_resp = idle === false.B && hptw_resp_stage2 202 io.resp.valid := Mux(stage1Hit, stageHit_resp, normal_resp) 203 io.resp.bits.source := source 204 io.resp.bits.resp := Mux(stage1Hit || (l3Hit || l2Hit) && guestFault && !pte_valid, stage1, ptw_resp) 205 io.resp.bits.h_resp := Mux(gvpn_gpf, fake_h_resp, hptw_resp) 206 io.resp.bits.s2xlate := req_s2xlate 207 208 io.llptw.valid := s_llptw_req === false.B && to_find_pte && !accessFault && !guestFault 209 io.llptw.bits.req_info.source := source 210 io.llptw.bits.req_info.vpn := vpn 211 io.llptw.bits.req_info.s2xlate := req_s2xlate 212 io.llptw.bits.ppn := DontCare 213 214 io.pmp.req.valid := DontCare // samecycle, do not use valid 215 io.pmp.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr) 216 io.pmp.req.bits.size := 3.U // TODO: fix it 217 io.pmp.req.bits.cmd := TlbCmd.read 218 219 mem.req.valid := s_mem_req === false.B && !mem.mask && !accessFault && s_pmp_check 220 mem.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr) 221 mem.req.bits.id := FsmReqID.U(bMemID.W) 222 mem.req.bits.hptw_bypassed := false.B 223 224 io.refill.req_info.s2xlate := req_s2xlate 225 io.refill.req_info.vpn := vpn 226 io.refill.level := level 227 io.refill.req_info.source := source 228 229 io.hptw.req.valid := !s_hptw_req || !s_last_hptw_req 230 io.hptw.req.bits.id := FsmReqID.U(bMemID.W) 231 io.hptw.req.bits.gvpn := get_pn(gpaddr) 232 io.hptw.req.bits.source := source 233 234 when (io.req.fire && io.req.bits.stage1Hit){ 235 idle := false.B 236 req_s2xlate := io.req.bits.req_info.s2xlate 237 s_hptw_req := false.B 238 hptw_resp_stage2 := false.B 239 last_s2xlate := false.B 240 hptw_pageFault := false.B 241 hptw_accessFault := false.B 242 } 243 244 when (io.hptw.resp.fire && w_hptw_resp === false.B && stage1Hit){ 245 w_hptw_resp := true.B 246 hptw_resp_stage2 := true.B 247 hptw_resp := io.hptw.resp.bits.h_resp 248 } 249 250 when (io.resp.fire && stage1Hit){ 251 idle := true.B 252 } 253 254 when (io.req.fire && !io.req.bits.stage1Hit){ 255 val req = io.req.bits 256 if (EnableSv48) { 257 when (mode === Sv48) { 258 level := Mux(req.l2Hit, 1.U, Mux(req.l3Hit.get, 2.U, 3.U)) 259 af_level := Mux(req.l2Hit, 1.U, Mux(req.l3Hit.get, 2.U, 3.U)) 260 gpf_level := Mux(req.l2Hit, 2.U, Mux(req.l3Hit.get, 3.U, 0.U)) 261 ppn := Mux(req.l2Hit || req.l3Hit.get, io.req.bits.ppn, satp.ppn) 262 l3Hit := req.l3Hit.get 263 } .otherwise { 264 level := Mux(req.l2Hit, 1.U, 2.U) 265 af_level := Mux(req.l2Hit, 1.U, 2.U) 266 gpf_level := 0.U 267 ppn := Mux(req.l2Hit, io.req.bits.ppn, satp.ppn) 268 l3Hit := false.B 269 } 270 } else { 271 level := Mux(req.l2Hit, 1.U, 2.U) 272 af_level := Mux(req.l2Hit, 1.U, 2.U) 273 gpf_level := 0.U 274 ppn := Mux(req.l2Hit, io.req.bits.ppn, satp.ppn) 275 l3Hit := false.B 276 } 277 vpn := io.req.bits.req_info.vpn 278 l2Hit := req.l2Hit 279 accessFault := false.B 280 idle := false.B 281 hptw_pageFault := false.B 282 hptw_accessFault := false.B 283 pte_valid := false.B 284 req_s2xlate := io.req.bits.req_info.s2xlate 285 when(io.req.bits.req_info.s2xlate =/= noS2xlate && io.req.bits.req_info.s2xlate =/= onlyStage1){ 286 when(io.req.bits.req_info.s2xlate === onlyStage2 && gvpn_gpf){ 287 mem_addr_update := true.B 288 last_s2xlate := false.B 289 }.otherwise{ 290 last_s2xlate := true.B 291 s_hptw_req := false.B 292 } 293 }.otherwise { 294 last_s2xlate := false.B 295 s_pmp_check := false.B 296 } 297 } 298 299 when(io.hptw.req.fire && s_hptw_req === false.B){ 300 s_hptw_req := true.B 301 w_hptw_resp := false.B 302 } 303 304 when(io.hptw.resp.fire && w_hptw_resp === false.B && !stage1Hit) { 305 hptw_pageFault := io.hptw.resp.bits.h_resp.gpf 306 hptw_accessFault := io.hptw.resp.bits.h_resp.gaf 307 hptw_resp := io.hptw.resp.bits.h_resp 308 w_hptw_resp := true.B 309 when(onlyS2xlate){ 310 mem_addr_update := true.B 311 last_s2xlate := false.B 312 }.elsewhen(!(io.hptw.resp.bits.h_resp.gpf || io.hptw.resp.bits.h_resp.gaf)) { 313 s_pmp_check := false.B 314 } 315 } 316 317 when(io.hptw.req.fire && s_last_hptw_req === false.B) { 318 w_last_hptw_resp := false.B 319 s_last_hptw_req := true.B 320 } 321 322 when(io.hptw.resp.fire && w_last_hptw_resp === false.B){ 323 hptw_pageFault := io.hptw.resp.bits.h_resp.gpf 324 hptw_accessFault := io.hptw.resp.bits.h_resp.gaf 325 hptw_resp := io.hptw.resp.bits.h_resp 326 w_last_hptw_resp := true.B 327 mem_addr_update := true.B 328 last_s2xlate := false.B 329 } 330 331 when(sent_to_pmp && mem_addr_update === false.B){ 332 s_mem_req := false.B 333 s_pmp_check := true.B 334 } 335 336 when(accessFault && idle === false.B){ 337 s_pmp_check := true.B 338 s_mem_req := true.B 339 w_mem_resp := true.B 340 s_llptw_req := true.B 341 s_hptw_req := true.B 342 w_hptw_resp := true.B 343 s_last_hptw_req := true.B 344 w_last_hptw_resp := true.B 345 mem_addr_update := true.B 346 last_s2xlate := false.B 347 } 348 349 when(guestFault && idle === false.B){ 350 s_pmp_check := true.B 351 s_mem_req := true.B 352 w_mem_resp := true.B 353 s_llptw_req := true.B 354 s_hptw_req := true.B 355 w_hptw_resp := true.B 356 s_last_hptw_req := true.B 357 w_last_hptw_resp := true.B 358 mem_addr_update := true.B 359 last_s2xlate := false.B 360 } 361 362 when (mem.req.fire){ 363 s_mem_req := true.B 364 w_mem_resp := false.B 365 } 366 367 when(mem.resp.fire && w_mem_resp === false.B){ 368 w_mem_resp := true.B 369 af_level := af_level - 1.U 370 s_llptw_req := false.B 371 mem_addr_update := true.B 372 gpf_level := Mux(mode === Sv39 && !pte_valid && !(l3Hit || l2Hit), gpf_level - 2.U, gpf_level - 1.U) 373 pte_valid := true.B 374 } 375 376 when(mem_addr_update){ 377 when(level >= 2.U && !onlyS2xlate && !(guestFault || find_pte || accessFault)) { 378 level := levelNext 379 when(s2xlate){ 380 s_hptw_req := false.B 381 }.otherwise{ 382 s_mem_req := false.B 383 } 384 s_llptw_req := true.B 385 mem_addr_update := false.B 386 }.elsewhen(io.llptw.valid){ 387 when(io.llptw.fire) { 388 idle := true.B 389 s_llptw_req := true.B 390 mem_addr_update := false.B 391 last_s2xlate := false.B 392 } 393 finish := true.B 394 }.elsewhen(s2xlate && last_s2xlate === true.B) { 395 when(accessFault || pageFault || ppn_af){ 396 last_s2xlate := false.B 397 }.otherwise{ 398 s_last_hptw_req := false.B 399 mem_addr_update := false.B 400 } 401 }.elsewhen(io.resp.valid){ 402 when(io.resp.fire) { 403 idle := true.B 404 s_llptw_req := true.B 405 mem_addr_update := false.B 406 accessFault := false.B 407 } 408 finish := true.B 409 } 410 } 411 412 413 when (flush) { 414 idle := true.B 415 s_pmp_check := true.B 416 s_mem_req := true.B 417 s_llptw_req := true.B 418 w_mem_resp := true.B 419 accessFault := false.B 420 mem_addr_update := false.B 421 s_hptw_req := true.B 422 w_hptw_resp := true.B 423 s_last_hptw_req := true.B 424 w_last_hptw_resp := true.B 425 } 426 427 428 XSDebug(p"[ptw] level:${level} notFound:${pageFault}\n") 429 430 // perf 431 XSPerfAccumulate("fsm_count", io.req.fire) 432 for (i <- 0 until PtwWidth) { 433 XSPerfAccumulate(s"fsm_count_source${i}", io.req.fire && io.req.bits.req_info.source === i.U) 434 } 435 XSPerfAccumulate("fsm_busy", !idle) 436 XSPerfAccumulate("fsm_idle", idle) 437 XSPerfAccumulate("resp_blocked", io.resp.valid && !io.resp.ready) 438 XSPerfAccumulate("ptw_ppn_af", io.resp.fire && ppn_af) 439 XSPerfAccumulate("mem_count", mem.req.fire) 440 XSPerfAccumulate("mem_cycle", BoolStopWatch(mem.req.fire, mem.resp.fire, true)) 441 XSPerfAccumulate("mem_blocked", mem.req.valid && !mem.req.ready) 442 443 TimeOutAssert(!idle, timeOutThreshold, "page table walker time out") 444 445 val perfEvents = Seq( 446 ("fsm_count ", io.req.fire ), 447 ("fsm_busy ", !idle ), 448 ("fsm_idle ", idle ), 449 ("resp_blocked ", io.resp.valid && !io.resp.ready ), 450 ("mem_count ", mem.req.fire ), 451 ("mem_cycle ", BoolStopWatch(mem.req.fire, mem.resp.fire, true)), 452 ("mem_blocked ", mem.req.valid && !mem.req.ready ), 453 ) 454 generatePerfEvent() 455} 456 457/*========================= LLPTW ==============================*/ 458 459/** LLPTW : Last Level Page Table Walker 460 * the page walker that only takes 4KB(last level) page walk. 461 **/ 462 463class LLPTWInBundle(implicit p: Parameters) extends XSBundle with HasPtwConst { 464 val req_info = Output(new L2TlbInnerBundle()) 465 val ppn = Output(UInt(ptePPNLen.W)) 466} 467 468class LLPTWIO(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 469 val in = Flipped(DecoupledIO(new LLPTWInBundle())) 470 val out = DecoupledIO(new Bundle { 471 val req_info = Output(new L2TlbInnerBundle()) 472 val id = Output(UInt(bMemID.W)) 473 val h_resp = Output(new HptwResp) 474 val first_s2xlate_fault = Output(Bool()) // Whether the first stage 2 translation occurs pf/af 475 val af = Output(Bool()) 476 }) 477 val mem = new Bundle { 478 val req = DecoupledIO(new L2TlbMemReqBundle()) 479 val resp = Flipped(Valid(new Bundle { 480 val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W)) 481 val value = Output(UInt(blockBits.W)) 482 })) 483 val enq_ptr = Output(UInt(log2Ceil(l2tlbParams.llptwsize).W)) 484 val buffer_it = Output(Vec(l2tlbParams.llptwsize, Bool())) 485 val refill = Output(new L2TlbInnerBundle()) 486 val req_mask = Input(Vec(l2tlbParams.llptwsize, Bool())) 487 val flush_latch = Input(Vec(l2tlbParams.llptwsize, Bool())) 488 } 489 val cache = DecoupledIO(new L2TlbInnerBundle()) 490 val pmp = new Bundle { 491 val req = Valid(new PMPReqBundle()) 492 val resp = Flipped(new PMPRespBundle()) 493 } 494 val hptw = new Bundle { 495 val req = DecoupledIO(new Bundle{ 496 val source = UInt(bSourceWidth.W) 497 val id = UInt(log2Up(l2tlbParams.llptwsize).W) 498 val gvpn = UInt(ptePPNLen.W) 499 }) 500 val resp = Flipped(Valid(new Bundle { 501 val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W)) 502 val h_resp = Output(new HptwResp) 503 })) 504 } 505} 506 507class LLPTWEntry(implicit p: Parameters) extends XSBundle with HasPtwConst { 508 val req_info = new L2TlbInnerBundle() 509 val ppn = UInt(ptePPNLen.W) 510 val wait_id = UInt(log2Up(l2tlbParams.llptwsize).W) 511 val af = Bool() 512 val hptw_resp = new HptwResp() 513 val first_s2xlate_fault = Output(Bool()) 514} 515 516 517class LLPTW(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents { 518 val io = IO(new LLPTWIO()) 519 val enableS2xlate = io.in.bits.req_info.s2xlate =/= noS2xlate 520 val satp = Mux(enableS2xlate, io.csr.vsatp, io.csr.satp) 521 522 val flush = io.sfence.valid || io.csr.satp.changed || io.csr.vsatp.changed || io.csr.hgatp.changed 523 val entries = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(0.U.asTypeOf(new LLPTWEntry())))) 524 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) 525 val state = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(state_idle))) 526 527 val is_emptys = state.map(_ === state_idle) 528 val is_mems = state.map(_ === state_mem_req) 529 val is_waiting = state.map(_ === state_mem_waiting) 530 val is_having = state.map(_ === state_mem_out) 531 val is_cache = state.map(_ === state_cache) 532 val is_hptw_req = state.map(_ === state_hptw_req) 533 val is_last_hptw_req = state.map(_ === state_last_hptw_req) 534 val is_hptw_resp = state.map(_ === state_hptw_resp) 535 val is_last_hptw_resp = state.map(_ === state_last_hptw_resp) 536 537 val full = !ParallelOR(is_emptys).asBool 538 val enq_ptr = ParallelPriorityEncoder(is_emptys) 539 540 val mem_ptr = ParallelPriorityEncoder(is_having) // TODO: optimize timing, bad: entries -> ptr -> entry 541 val mem_arb = Module(new RRArbiterInit(new LLPTWEntry(), l2tlbParams.llptwsize)) 542 for (i <- 0 until l2tlbParams.llptwsize) { 543 mem_arb.io.in(i).bits := entries(i) 544 mem_arb.io.in(i).valid := is_mems(i) && !io.mem.req_mask(i) 545 } 546 547 // process hptw requests in serial 548 val hyper_arb1 = Module(new RRArbiterInit(new LLPTWEntry(), l2tlbParams.llptwsize)) 549 for (i <- 0 until l2tlbParams.llptwsize) { 550 hyper_arb1.io.in(i).bits := entries(i) 551 hyper_arb1.io.in(i).valid := is_hptw_req(i) && !(Cat(is_hptw_resp).orR) && !(Cat(is_last_hptw_resp).orR) 552 } 553 val hyper_arb2 = Module(new RRArbiterInit(new LLPTWEntry(), l2tlbParams.llptwsize)) 554 for(i <- 0 until l2tlbParams.llptwsize) { 555 hyper_arb2.io.in(i).bits := entries(i) 556 hyper_arb2.io.in(i).valid := is_last_hptw_req(i) && !(Cat(is_hptw_resp).orR) && !(Cat(is_last_hptw_resp).orR) 557 } 558 559 val cache_ptr = ParallelMux(is_cache, (0 until l2tlbParams.llptwsize).map(_.U(log2Up(l2tlbParams.llptwsize).W))) 560 561 // duplicate req 562 // to_wait: wait for the last to access mem, set to mem_resp 563 // to_cache: the last is back just right now, set to mem_cache 564 val dup_vec = state.indices.map(i => 565 dup(io.in.bits.req_info.vpn, entries(i).req_info.vpn) && io.in.bits.req_info.s2xlate === entries(i).req_info.s2xlate 566 ) 567 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 568 val dup_vec_wait = dup_vec.zip(is_waiting).map{case (d, w) => d && w} // dup with "mem_waiting" entries, sending mem req already 569 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 570 val dup_vec_last_hptw = dup_vec.zipWithIndex.map{case (d, i) => d && (is_last_hptw_req(i) || is_last_hptw_resp(i))} 571 val wait_id = Mux(dup_req_fire, mem_arb.io.chosen, ParallelMux(dup_vec_wait zip entries.map(_.wait_id))) 572 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 573 val to_wait = Cat(dup_vec_wait).orR || dup_req_fire 574 val to_mem_out = dup_wait_resp && ((entries(io.mem.resp.bits.id).req_info.s2xlate === noS2xlate) || (entries(io.mem.resp.bits.id).req_info.s2xlate === onlyStage1)) 575 val to_cache = Cat(dup_vec_having).orR || Cat(dup_vec_last_hptw).orR 576 val to_hptw_req = io.in.bits.req_info.s2xlate === allStage 577 val to_last_hptw_req = dup_wait_resp && entries(io.mem.resp.bits.id).req_info.s2xlate === allStage 578 val last_hptw_req_id = io.mem.resp.bits.id 579 val req_paddr = MakeAddr(io.in.bits.ppn(ppnLen-1, 0), getVpnn(io.in.bits.req_info.vpn, 0)) 580 val req_hpaddr = MakeAddr(entries(last_hptw_req_id).hptw_resp.genPPNS2(get_pn(req_paddr)), getVpnn(io.in.bits.req_info.vpn, 0)) 581 val index = Mux(entries(last_hptw_req_id).req_info.s2xlate === allStage, req_hpaddr, req_paddr)(log2Up(l2tlbParams.blockBytes)-1, log2Up(XLEN/8)) 582 val last_hptw_req_ppn = io.mem.resp.bits.value.asTypeOf(Vec(blockBits / XLEN, new PteBundle()))(index).getPPN() 583 XSError(RegNext(dup_req_fire && Cat(dup_vec_wait).orR, init = false.B), "mem req but some entries already waiting, should not happed") 584 585 XSError(io.in.fire && ((to_mem_out && to_cache) || (to_wait && to_cache)), "llptw enq, to cache conflict with to mem") 586 val mem_resp_hit = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(false.B))) 587 val enq_state_normal = MuxCase(state_addr_check, Seq( 588 to_mem_out -> state_mem_out, // same to the blew, but the mem resp now 589 to_last_hptw_req -> state_last_hptw_req, 590 to_wait -> state_mem_waiting, 591 to_cache -> state_cache, 592 to_hptw_req -> state_hptw_req 593 )) 594 val enq_state = Mux(from_pre(io.in.bits.req_info.source) && enq_state_normal =/= state_addr_check, state_idle, enq_state_normal) 595 when (io.in.fire) { 596 // if prefetch req does not need mem access, just give it up. 597 // so there will be at most 1 + FilterSize entries that needs re-access page cache 598 // so 2 + FilterSize is enough to avoid dead-lock 599 state(enq_ptr) := enq_state 600 entries(enq_ptr).req_info := io.in.bits.req_info 601 entries(enq_ptr).ppn := Mux(to_last_hptw_req, last_hptw_req_ppn, io.in.bits.ppn) 602 entries(enq_ptr).wait_id := Mux(to_wait, wait_id, enq_ptr) 603 entries(enq_ptr).af := false.B 604 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)) 605 entries(enq_ptr).first_s2xlate_fault := false.B 606 mem_resp_hit(enq_ptr) := to_mem_out || to_last_hptw_req 607 } 608 609 val enq_ptr_reg = RegNext(enq_ptr) 610 val need_addr_check = GatedValidRegNext(enq_state === state_addr_check && io.in.fire && !flush) 611 612 val hasHptwResp = ParallelOR(state.map(_ === state_hptw_resp)).asBool 613 val hptw_resp_ptr_reg = RegNext(io.hptw.resp.bits.id) 614 val hptw_need_addr_check = RegNext(hasHptwResp && io.hptw.resp.fire && !flush) && state(hptw_resp_ptr_reg) === state_addr_check 615 616 val ptes = io.mem.resp.bits.value.asTypeOf(Vec(blockBits / XLEN, new PteBundle())) 617 val gpaddr = MakeGPAddr(entries(hptw_resp_ptr_reg).ppn, getVpnn(entries(hptw_resp_ptr_reg).req_info.vpn, 0)) 618 val hptw_resp = entries(hptw_resp_ptr_reg).hptw_resp 619 val hpaddr = Cat(hptw_resp.genPPNS2(get_pn(gpaddr)), get_off(gpaddr)) 620 val addr = RegEnable(MakeAddr(io.in.bits.ppn(ppnLen - 1, 0), getVpnn(io.in.bits.req_info.vpn, 0)), io.in.fire) 621 io.pmp.req.valid := need_addr_check || hptw_need_addr_check 622 io.pmp.req.bits.addr := Mux(hptw_need_addr_check, hpaddr, addr) 623 io.pmp.req.bits.cmd := TlbCmd.read 624 io.pmp.req.bits.size := 3.U // TODO: fix it 625 val pmp_resp_valid = io.pmp.req.valid // same cycle 626 when (pmp_resp_valid) { 627 // NOTE: when pmp resp but state is not addr check, then the entry is dup with other entry, the state was changed before 628 // when dup with the req-ing entry, set to mem_waiting (above codes), and the ld must be false, so dontcare 629 val ptr = Mux(hptw_need_addr_check, hptw_resp_ptr_reg, enq_ptr_reg); 630 val accessFault = io.pmp.resp.ld || io.pmp.resp.mmio 631 entries(ptr).af := accessFault 632 state(ptr) := Mux(accessFault, state_mem_out, state_mem_req) 633 } 634 635 when (mem_arb.io.out.fire) { 636 for (i <- state.indices) { 637 when (state(i) =/= state_idle && state(i) =/= state_mem_out && state(i) =/= state_last_hptw_req && state(i) =/= state_last_hptw_resp 638 && entries(i).req_info.s2xlate === mem_arb.io.out.bits.req_info.s2xlate 639 && dup(entries(i).req_info.vpn, mem_arb.io.out.bits.req_info.vpn)) { 640 // NOTE: "dup enq set state to mem_wait" -> "sending req set other dup entries to mem_wait" 641 state(i) := state_mem_waiting 642 entries(i).hptw_resp := entries(mem_arb.io.chosen).hptw_resp 643 entries(i).wait_id := mem_arb.io.chosen 644 } 645 } 646 } 647 when (io.mem.resp.fire) { 648 state.indices.map{i => 649 when (state(i) === state_mem_waiting && io.mem.resp.bits.id === entries(i).wait_id) { 650 val req_paddr = MakeAddr(entries(i).ppn, getVpnn(entries(i).req_info.vpn, 0)) 651 val req_hpaddr = MakeAddr(entries(i).hptw_resp.genPPNS2(get_pn(req_paddr)), getVpnn(entries(i).req_info.vpn, 0)) 652 val index = Mux(entries(i).req_info.s2xlate === allStage, req_hpaddr, req_paddr)(log2Up(l2tlbParams.blockBytes)-1, log2Up(XLEN/8)) 653 state(i) := Mux(entries(i).req_info.s2xlate === allStage && !(ptes(index).isPf(0.U) || !ptes(index).isLeaf() || ptes(index).isAf() || ptes(index).isStage1Gpf(io.csr.vsatp.mode)) 654 , state_last_hptw_req, state_mem_out) 655 mem_resp_hit(i) := true.B 656 entries(i).ppn := ptes(index).getPPN() // for last stage 2 translation 657 // when onlystage1, gpf has higher priority 658 entries(i).af := Mux(entries(i).req_info.s2xlate === allStage, false.B, Mux(entries(i).req_info.s2xlate === onlyStage1, ptes(index).isAf() && !ptes(index).isStage1Gpf(io.csr.vsatp.mode), ptes(index).isAf())) 659 entries(i).hptw_resp.gpf := Mux(entries(i).req_info.s2xlate === allStage || entries(i).req_info.s2xlate === onlyStage1, ptes(index).isStage1Gpf(io.csr.vsatp.mode), false.B) 660 } 661 } 662 } 663 664 when (hyper_arb1.io.out.fire) { 665 for (i <- state.indices) { 666 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) { 667 state(i) := state_hptw_resp 668 entries(i).wait_id := hyper_arb1.io.chosen 669 } 670 } 671 } 672 673 when (hyper_arb2.io.out.fire) { 674 for (i <- state.indices) { 675 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) { 676 state(i) := state_last_hptw_resp 677 entries(i).wait_id := hyper_arb2.io.chosen 678 } 679 } 680 } 681 682 when (io.hptw.resp.fire) { 683 for (i <- state.indices) { 684 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) { 685 when (io.hptw.resp.bits.h_resp.gaf || io.hptw.resp.bits.h_resp.gpf) { 686 state(i) := state_mem_out 687 entries(i).hptw_resp := io.hptw.resp.bits.h_resp 688 entries(i).first_s2xlate_fault := io.hptw.resp.bits.h_resp.gaf || io.hptw.resp.bits.h_resp.gpf 689 }.otherwise{ // change the entry that is waiting hptw resp 690 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)) 691 val waiting_index = ParallelMux(need_to_waiting_vec zip entries.map(_.wait_id)) 692 state(i) := Mux(Cat(need_to_waiting_vec).orR, state_mem_waiting, state_addr_check) 693 entries(i).hptw_resp := io.hptw.resp.bits.h_resp 694 entries(i).wait_id := Mux(Cat(need_to_waiting_vec).orR, waiting_index, entries(i).wait_id) 695 //To do: change the entry that is having the same hptw req 696 } 697 } 698 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) { 699 state(i) := state_mem_out 700 entries(i).hptw_resp := io.hptw.resp.bits.h_resp 701 //To do: change the entry that is having the same hptw req 702 } 703 } 704 } 705 when (io.out.fire) { 706 assert(state(mem_ptr) === state_mem_out) 707 state(mem_ptr) := state_idle 708 } 709 mem_resp_hit.map(a => when (a) { a := false.B } ) 710 711 when (io.cache.fire) { 712 state(cache_ptr) := state_idle 713 } 714 XSError(io.out.fire && io.cache.fire && (mem_ptr === cache_ptr), "mem resp and cache fire at the same time at same entry") 715 716 when (flush) { 717 state.map(_ := state_idle) 718 } 719 720 io.in.ready := !full 721 722 io.out.valid := ParallelOR(is_having).asBool 723 io.out.bits.req_info := entries(mem_ptr).req_info 724 io.out.bits.id := mem_ptr 725 io.out.bits.af := entries(mem_ptr).af 726 io.out.bits.h_resp := entries(mem_ptr).hptw_resp 727 io.out.bits.first_s2xlate_fault := entries(mem_ptr).first_s2xlate_fault 728 729 val hptw_req_arb = Module(new Arbiter(new Bundle{ 730 val source = UInt(bSourceWidth.W) 731 val id = UInt(log2Up(l2tlbParams.llptwsize).W) 732 val ppn = UInt(ptePPNLen.W) 733 } , 2)) 734 // first stage 2 translation 735 hptw_req_arb.io.in(0).valid := hyper_arb1.io.out.valid 736 hptw_req_arb.io.in(0).bits.source := hyper_arb1.io.out.bits.req_info.source 737 hptw_req_arb.io.in(0).bits.ppn := hyper_arb1.io.out.bits.ppn 738 hptw_req_arb.io.in(0).bits.id := hyper_arb1.io.chosen 739 hyper_arb1.io.out.ready := hptw_req_arb.io.in(0).ready 740 // last stage 2 translation 741 hptw_req_arb.io.in(1).valid := hyper_arb2.io.out.valid 742 hptw_req_arb.io.in(1).bits.source := hyper_arb2.io.out.bits.req_info.source 743 hptw_req_arb.io.in(1).bits.ppn := hyper_arb2.io.out.bits.ppn 744 hptw_req_arb.io.in(1).bits.id := hyper_arb2.io.chosen 745 hyper_arb2.io.out.ready := hptw_req_arb.io.in(1).ready 746 hptw_req_arb.io.out.ready := io.hptw.req.ready 747 io.hptw.req.valid := hptw_req_arb.io.out.fire && !flush 748 io.hptw.req.bits.gvpn := hptw_req_arb.io.out.bits.ppn 749 io.hptw.req.bits.id := hptw_req_arb.io.out.bits.id 750 io.hptw.req.bits.source := hptw_req_arb.io.out.bits.source 751 752 io.mem.req.valid := mem_arb.io.out.valid && !flush 753 val mem_paddr = MakeAddr(mem_arb.io.out.bits.ppn, getVpnn(mem_arb.io.out.bits.req_info.vpn, 0)) 754 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)) 755 io.mem.req.bits.addr := Mux(mem_arb.io.out.bits.req_info.s2xlate === allStage, mem_hpaddr, mem_paddr) 756 io.mem.req.bits.id := mem_arb.io.chosen 757 io.mem.req.bits.hptw_bypassed := false.B 758 mem_arb.io.out.ready := io.mem.req.ready 759 val mem_refill_id = RegNext(io.mem.resp.bits.id(log2Up(l2tlbParams.llptwsize)-1, 0)) 760 io.mem.refill := entries(mem_refill_id).req_info 761 io.mem.refill.s2xlate := entries(mem_refill_id).req_info.s2xlate 762 io.mem.buffer_it := mem_resp_hit 763 io.mem.enq_ptr := enq_ptr 764 765 io.cache.valid := Cat(is_cache).orR 766 io.cache.bits := ParallelMux(is_cache, entries.map(_.req_info)) 767 768 XSPerfAccumulate("llptw_in_count", io.in.fire) 769 XSPerfAccumulate("llptw_in_block", io.in.valid && !io.in.ready) 770 for (i <- 0 until 7) { 771 XSPerfAccumulate(s"enq_state${i}", io.in.fire && enq_state === i.U) 772 } 773 for (i <- 0 until (l2tlbParams.llptwsize + 1)) { 774 XSPerfAccumulate(s"util${i}", PopCount(is_emptys.map(!_)) === i.U) 775 XSPerfAccumulate(s"mem_util${i}", PopCount(is_mems) === i.U) 776 XSPerfAccumulate(s"waiting_util${i}", PopCount(is_waiting) === i.U) 777 } 778 XSPerfAccumulate("mem_count", io.mem.req.fire) 779 XSPerfAccumulate("mem_cycle", PopCount(is_waiting) =/= 0.U) 780 XSPerfAccumulate("blocked_in", io.in.valid && !io.in.ready) 781 782 for (i <- 0 until l2tlbParams.llptwsize) { 783 TimeOutAssert(state(i) =/= state_idle, timeOutThreshold, s"missqueue time out no out ${i}") 784 } 785 786 val perfEvents = Seq( 787 ("tlbllptw_incount ", io.in.fire ), 788 ("tlbllptw_inblock ", io.in.valid && !io.in.ready), 789 ("tlbllptw_memcount ", io.mem.req.fire ), 790 ("tlbllptw_memcycle ", PopCount(is_waiting) ), 791 ) 792 generatePerfEvent() 793} 794 795/*========================= HPTW ==============================*/ 796 797/** HPTW : Hypervisor Page Table Walker 798 * the page walker take the virtual machine's page walk. 799 * guest physical address translation, guest physical address -> host physical address 800 **/ 801class HPTWIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 802 val req = Flipped(DecoupledIO(new Bundle { 803 val source = UInt(bSourceWidth.W) 804 val id = UInt(log2Up(l2tlbParams.llptwsize).W) 805 val gvpn = UInt(gvpnLen.W) 806 val ppn = UInt(ppnLen.W) 807 val l3Hit = if (EnableSv48) Some(new Bool()) else None 808 val l2Hit = Bool() 809 val l1Hit = Bool() 810 val bypassed = Bool() // if bypass, don't refill 811 })) 812 val resp = DecoupledIO(new Bundle { 813 val source = UInt(bSourceWidth.W) 814 val resp = Output(new HptwResp()) 815 val id = Output(UInt(bMemID.W)) 816 }) 817 818 val mem = new Bundle { 819 val req = DecoupledIO(new L2TlbMemReqBundle()) 820 val resp = Flipped(ValidIO(UInt(XLEN.W))) 821 val mask = Input(Bool()) 822 } 823 val refill = Output(new Bundle { 824 val req_info = new L2TlbInnerBundle() 825 val level = UInt(log2Up(Level + 1).W) 826 }) 827 val pmp = new Bundle { 828 val req = ValidIO(new PMPReqBundle()) 829 val resp = Flipped(new PMPRespBundle()) 830 } 831} 832 833class HPTW()(implicit p: Parameters) extends XSModule with HasPtwConst { 834 val io = IO(new HPTWIO) 835 val hgatp = io.csr.hgatp 836 val sfence = io.sfence 837 val flush = sfence.valid || hgatp.changed || io.csr.satp.changed || io.csr.vsatp.changed 838 val mode = hgatp.mode 839 840 val level = RegInit(3.U(log2Up(Level + 1).W)) 841 val af_level = RegInit(3.U(log2Up(Level + 1).W)) // access fault return this level 842 val gpaddr = Reg(UInt(GPAddrBits.W)) 843 val req_ppn = Reg(UInt(ppnLen.W)) 844 val vpn = gpaddr(GPAddrBits-1, offLen) 845 val levelNext = level - 1.U 846 val l3Hit = Reg(Bool()) 847 val l2Hit = Reg(Bool()) 848 val l1Hit = Reg(Bool()) 849 val bypassed = Reg(Bool()) 850// val pte = io.mem.resp.bits.MergeRespToPte() 851 val pte = io.mem.resp.bits.asTypeOf(new PteBundle().cloneType) 852 val ppn_l3 = Mux(l3Hit, req_ppn, pte.ppn) 853 val ppn_l2 = Mux(l2Hit, req_ppn, pte.ppn) 854 val ppn_l1 = Mux(l1Hit, req_ppn, pte.ppn) 855 val ppn = Wire(UInt(PAddrBits.W)) 856 val p_pte = MakeAddr(ppn, getVpnn(vpn, level)) 857 val pg_base = Wire(UInt(PAddrBits.W)) 858 val mem_addr = Wire(UInt(PAddrBits.W)) 859 if (EnableSv48) { 860 when (mode === Sv48) { 861 ppn := Mux(af_level === 2.U, ppn_l3, Mux(af_level === 1.U, ppn_l2, ppn_l1)) // for l2, l1 and l3 862 pg_base := MakeGPAddr(hgatp.ppn, getGVpnn(vpn, 3.U, mode = Sv48)) // for l3 863 mem_addr := Mux(af_level === 3.U, pg_base, p_pte) 864 } .otherwise { 865 ppn := Mux(af_level === 1.U, ppn_l2, ppn_l1) //for l1 and l2 866 pg_base := MakeGPAddr(hgatp.ppn, getGVpnn(vpn, 2.U, mode = Sv39)) 867 mem_addr := Mux(af_level === 2.U, pg_base, p_pte) 868 } 869 } else { 870 ppn := Mux(af_level === 1.U, ppn_l2, ppn_l1) //for l1 and l2 871 pg_base := MakeGPAddr(hgatp.ppn, getGVpnn(vpn, 2.U, mode = Sv39)) 872 mem_addr := Mux(af_level === 2.U, pg_base, p_pte) 873 } 874 875 //s/w register 876 val s_pmp_check = RegInit(true.B) 877 val s_mem_req = RegInit(true.B) 878 val w_mem_resp = RegInit(true.B) 879 val idle = RegInit(true.B) 880 val mem_addr_update = RegInit(false.B) 881 val finish = WireInit(false.B) 882 883 val sent_to_pmp = !idle && (!s_pmp_check || mem_addr_update) && !finish 884 val pageFault = pte.isGpf(level) || (!pte.isLeaf() && level === 0.U) 885 val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, sent_to_pmp) 886 887 val ppn_af = pte.isAf() 888 val find_pte = pte.isLeaf() || ppn_af || pageFault 889 890 val resp_valid = !idle && mem_addr_update && ((w_mem_resp && find_pte) || (s_pmp_check && accessFault)) 891 val id = Reg(UInt(log2Up(l2tlbParams.llptwsize).W)) 892 val source = RegEnable(io.req.bits.source, io.req.fire) 893 894 io.req.ready := idle 895 val resp = Wire(new HptwResp()) 896 resp.apply(pageFault && !accessFault && !ppn_af, accessFault || ppn_af, Mux(accessFault, af_level, level), pte, vpn, hgatp.vmid) 897 io.resp.valid := resp_valid 898 io.resp.bits.id := id 899 io.resp.bits.resp := resp 900 io.resp.bits.source := source 901 902 io.pmp.req.valid := DontCare 903 io.pmp.req.bits.addr := mem_addr 904 io.pmp.req.bits.size := 3.U 905 io.pmp.req.bits.cmd := TlbCmd.read 906 907 io.mem.req.valid := !s_mem_req && !io.mem.mask && !accessFault && s_pmp_check 908 io.mem.req.bits.addr := mem_addr 909 io.mem.req.bits.id := HptwReqId.U(bMemID.W) 910 io.mem.req.bits.hptw_bypassed := bypassed 911 912 io.refill.req_info.vpn := vpn 913 io.refill.level := level 914 io.refill.req_info.source := source 915 io.refill.req_info.s2xlate := onlyStage2 916 when (idle){ 917 when(io.req.fire){ 918 bypassed := io.req.bits.bypassed 919 idle := false.B 920 gpaddr := Cat(io.req.bits.gvpn, 0.U(offLen.W)) 921 accessFault := false.B 922 s_pmp_check := false.B 923 id := io.req.bits.id 924 req_ppn := io.req.bits.ppn 925 if (EnableSv48) { 926 when (mode === Sv48) { 927 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))) 928 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))) 929 l3Hit := io.req.bits.l3Hit.get 930 } .otherwise { 931 level := Mux(io.req.bits.l1Hit, 0.U, Mux(io.req.bits.l2Hit, 1.U, 2.U)) 932 af_level := Mux(io.req.bits.l1Hit, 0.U, Mux(io.req.bits.l2Hit, 1.U, 2.U)) 933 l3Hit := false.B 934 } 935 } else { 936 level := Mux(io.req.bits.l1Hit, 0.U, Mux(io.req.bits.l2Hit, 1.U, 2.U)) 937 af_level := Mux(io.req.bits.l1Hit, 0.U, Mux(io.req.bits.l2Hit, 1.U, 2.U)) 938 l3Hit := false.B 939 } 940 l2Hit := io.req.bits.l2Hit 941 l1Hit := io.req.bits.l1Hit 942 } 943 } 944 945 when(sent_to_pmp && !mem_addr_update){ 946 s_mem_req := false.B 947 s_pmp_check := true.B 948 } 949 950 when(accessFault && !idle){ 951 s_pmp_check := true.B 952 s_mem_req := true.B 953 w_mem_resp := true.B 954 mem_addr_update := true.B 955 } 956 957 when(io.mem.req.fire){ 958 s_mem_req := true.B 959 w_mem_resp := false.B 960 } 961 962 when(io.mem.resp.fire && !w_mem_resp){ 963 w_mem_resp := true.B 964 af_level := af_level - 1.U 965 mem_addr_update := true.B 966 } 967 968 when(mem_addr_update){ 969 when(!(find_pte || accessFault)){ 970 level := levelNext 971 s_mem_req := false.B 972 mem_addr_update := false.B 973 }.elsewhen(resp_valid){ 974 when(io.resp.fire){ 975 idle := true.B 976 mem_addr_update := false.B 977 accessFault := false.B 978 } 979 finish := true.B 980 } 981 } 982 when (flush) { 983 idle := true.B 984 s_pmp_check := true.B 985 s_mem_req := true.B 986 w_mem_resp := true.B 987 accessFault := false.B 988 mem_addr_update := false.B 989 } 990} 991