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 l1Hit = Bool() 45 val ppn = UInt(ppnLen.W) 46 })) 47 val resp = DecoupledIO(new Bundle { 48 val source = UInt(bSourceWidth.W) 49 val s2xlate = UInt(2.W) // 0 bit: has s2xlate, 1 bit: Only valid when 0 bit is 1. If 0, all stage; if 1, only stage 2 50 val resp = new PtwMergeResp 51 val h_resp = new HptwResp 52 }) 53 54 val llptw = DecoupledIO(new LLPTWInBundle()) 55 // NOTE: llptw change from "connect to llptw" to "connect to page cache" 56 // to avoid corner case that caused duplicate entries 57 58 val hptw = new Bundle { 59 val req = DecoupledIO(new Bundle { 60 val id = UInt(log2Up(l2tlbParams.llptwsize).W) 61 val gvpn = UInt(vpnLen.W) 62 }) 63 val resp = Flipped(Valid(new Bundle { 64 val h_resp = Output(new HptwResp) 65 })) 66 } 67 val mem = new Bundle { 68 val req = DecoupledIO(new L2TlbMemReqBundle()) 69 val resp = Flipped(ValidIO(UInt(XLEN.W))) 70 val mask = Input(Bool()) 71 } 72 val pmp = new Bundle { 73 val req = ValidIO(new PMPReqBundle()) 74 val resp = Flipped(new PMPRespBundle()) 75 } 76 77 val refill = Output(new Bundle { 78 val req_info = new L2TlbInnerBundle() 79 val level = UInt(log2Up(Level).W) 80 }) 81} 82 83class PTW()(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents { 84 val io = IO(new PTWIO) 85 val sfence = io.sfence 86 val mem = io.mem 87 val req_s2xlate = Reg(UInt(2.W)) 88 val enableS2xlate = RegInit(false.B) 89 val onlyS1xlate = RegInit(false.B) 90 val onlyS2xlate = RegInit(false.B) 91 92 val satp = Mux(enableS2xlate, io.csr.vsatp, io.csr.satp) 93 val hgatp = io.csr.hgatp 94 val flush = io.sfence.valid || satp.changed 95 val s2xlate = enableS2xlate && !onlyS1xlate 96 val level = RegInit(0.U(log2Up(Level).W)) 97 val af_level = RegInit(0.U(log2Up(Level).W)) // access fault return this level 98 val ppn = Reg(UInt(ppnLen.W)) 99 val vpn = Reg(UInt(vpnLen.W)) // vpn or gvpn 100 val levelNext = level + 1.U 101 val l1Hit = Reg(Bool()) 102 val pte = mem.resp.bits.asTypeOf(new PteBundle().cloneType) 103 104 // s/w register 105 val s_pmp_check = RegInit(true.B) 106 val s_mem_req = RegInit(true.B) 107 val s_llptw_req = RegInit(true.B) 108 val w_mem_resp = RegInit(true.B) 109 val s_hptw_req = RegInit(true.B) 110 val w_hptw_resp = RegInit(true.B) 111 val s_last_hptw_req = RegInit(true.B) 112 val w_last_hptw_resp = RegInit(true.B) 113 // for updating "level" 114 val mem_addr_update = RegInit(false.B) 115 116 val idle = RegInit(true.B) 117 val finish = WireInit(false.B) 118 val sent_to_pmp = idle === false.B && (s_pmp_check === false.B || mem_addr_update) && !finish 119 120 val pageFault = pte.isPf(level) 121 val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, sent_to_pmp) 122 123 val hptw_pageFault = RegInit(false.B) 124 val hptw_accessFault = RegInit(false.B) 125 val last_s2xlate = RegInit(false.B) 126 127 val ppn_af = pte.isAf() 128 val find_pte = pte.isLeaf() || ppn_af || pageFault 129 val to_find_pte = level === 1.U && find_pte === false.B 130 val source = RegEnable(io.req.bits.req_info.source, io.req.fire) 131 132 val l1addr = MakeAddr(satp.ppn, getVpnn(vpn, 2)) 133 val l2addr = MakeAddr(Mux(l1Hit, ppn, pte.ppn), getVpnn(vpn, 1)) 134 val mem_addr = Mux(af_level === 0.U, l1addr, l2addr) 135 136 val hptw_resp = io.hptw.resp.bits.h_resp 137 val gpaddr = Mux(onlyS2xlate, Cat(vpn, 0.U(offLen.W)), mem_addr) 138 val hpaddr = Cat(hptw_resp.entry.ppn, 0.U(offLen.W)) 139 140 io.req.ready := idle 141 142 io.resp.valid := idle === false.B && mem_addr_update && !last_s2xlate && ((w_mem_resp && find_pte) || (s_pmp_check && accessFault) || onlyS2xlate) 143 io.resp.bits.source := source 144 io.resp.bits.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) 145 io.resp.bits.h_resp := io.hptw.resp.bits.h_resp 146 io.resp.bits.s2xlate := s2xlate 147 148 io.llptw.valid := s_llptw_req === false.B && to_find_pte && !accessFault 149 io.llptw.bits.req_info.source := source 150 io.llptw.bits.req_info.vpn := vpn 151 io.llptw.bits.req_info.s2xlate := req_s2xlate 152 153 io.pmp.req.valid := DontCare // samecycle, do not use valid 154 io.pmp.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr) 155 io.pmp.req.bits.size := 3.U // TODO: fix it 156 io.pmp.req.bits.cmd := TlbCmd.read 157 158 mem.req.valid := s_mem_req === false.B && !mem.mask && !accessFault && s_pmp_check 159 mem.req.bits.addr := Mux(s2xlate, hpaddr, mem_addr) 160 mem.req.bits.id := FsmReqID.U(bMemID.W) 161 162 io.refill.req_info.vpn := vpn 163 io.refill.level := level 164 io.refill.req_info.source := source 165 166 io.hptw.req.valid := !s_hptw_req || !s_last_hptw_req 167 io.hptw.req.bits.id := FsmReqID.U(bMemID.W) 168 io.hptw.req.bits.gvpn := get_pn(gpaddr) 169 170 io.hptw.req.valid := !s_hptw_req || !s_last_hptw_req 171 io.hptw.req.bits.id := FsmReqID.U(bMemID.W) 172 io.hptw.req.bits.gvpn := gvpn 173 174 when (io.req.fire){ 175 val req = io.req.bits 176 level := Mux(req.l1Hit, 1.U, 0.U) 177 af_level := Mux(req.l1Hit, 1.U, 0.U) 178 ppn := Mux(req.l1Hit, io.req.bits.ppn, satp.ppn) 179 vpn := io.req.bits.req_info.vpn 180 enableS2xlate := io.req.bits.req_info.s2xlate =/= noS2xlate 181 onlyS1xlate := io.req.bits.req_info.s2xlate === onlyS1xlate 182 onlyS2xlate := io.req.bits.req_info.s2xlate === onlyS2xlate 183 l1Hit := req.l1Hit 184 accessFault := false.B 185 s_pmp_check := false.B 186 idle := false.B 187 hptw_pageFault := false.B 188 s2xlate := io.req.bits.req_info.s2xlate 189 when(io.req.bits.req_info.s2xlate =/= noS2xlate && io.req.bits.req_info.s2xlate =/= onlyStage1){ 190 last_s2xlate := true.B 191 s_hptw_req := false.B 192 }.otherwise { 193 s_pmp_check := false.B 194 } 195 } 196 197 when(io.hptw.req.fire() && s_hptw_req === false.B){ 198 s_hptw_req := true.B 199 w_hptw_resp := false.B 200 } 201 202 when(io.hptw.resp.fire() && w_hptw_resp === false.B) { 203 hptw_pageFault := io.hptw.resp.bits.h_resp.gpf 204 hptw_accessFault := io.hptw.resp.bits.h_resp.gaf 205 w_hptw_resp := true.B 206 when(onlyS2xlate){ 207 mem_addr_update := true.B 208 last_s2xlate := false.B 209 }.otherwise { 210 s_pmp_check := false.B 211 } 212 } 213 214 when(io.hptw.req.fire() && s_last_hptw_req === false.B) { 215 w_last_hptw_resp := false.B 216 s_last_hptw_req := true.B 217 } 218 219 when(io.hptw.resp.fire() && w_last_hptw_resp === false.B){ 220 hptw_pageFault := io.hptw.resp.bits.h_resp.gpf 221 hptw_accessFault := io.hptw.resp.bits.h_resp.gaf 222 w_last_hptw_resp := true.B 223 mem_addr_update := true.B 224 last_s2xlate := false.B 225 } 226 227 when(sent_to_pmp && mem_addr_update === false.B){ 228 s_mem_req := false.B 229 s_pmp_check := true.B 230 } 231 232 when(accessFault && idle === false.B){ 233 s_pmp_check := true.B 234 s_mem_req := true.B 235 w_mem_resp := true.B 236 s_llptw_req := true.B 237 s_hptw_req := true.B 238 w_hptw_resp := true.B 239 s_last_hptw_req := true.B 240 w_last_hptw_resp := true.B 241 mem_addr_update := true.B 242 last_s2xlate := false.B 243 } 244 245 when (mem.req.fire){ 246 s_mem_req := true.B 247 w_mem_resp := false.B 248 } 249 250 when(mem.resp.fire && w_mem_resp === false.B){ 251 w_mem_resp := true.B 252 af_level := af_level + 1.U 253 s_llptw_req := false.B 254 mem_addr_update := true.B 255 } 256 257 when(mem_addr_update){ 258 when(level === 0.U && !(find_pte || accessFault)){ 259 level := levelNext 260 when(s2xlate){ 261 s_hptw_req := false.B 262 }.otherwise{ 263 s_mem_req := false.B 264 } 265 s_llptw_req := true.B 266 mem_addr_update := false.B 267 }.elsewhen(io.llptw.valid){ 268 when(io.llptw.fire) { 269 idle := true.B 270 s_llptw_req := true.B 271 mem_addr_update := false.B 272 last_s2xlate := false.B 273 } 274 finish := true.B 275 }.elsewhen(s2xlate && last_s2xlate === true.B) { 276 s_last_hptw_req := false.B 277 mem_addr_update := false.B 278 }.elsewhen(io.resp.valid){ 279 when(io.resp.fire) { 280 idle := true.B 281 s_llptw_req := true.B 282 mem_addr_update := false.B 283 accessFault := false.B 284 } 285 finish := true.B 286 } 287 } 288 289 290 when (sfence.valid) { 291 idle := true.B 292 s_pmp_check := true.B 293 s_mem_req := true.B 294 s_llptw_req := true.B 295 w_mem_resp := true.B 296 accessFault := false.B 297 mem_addr_update := false.B 298 s_hptw_req := true.B 299 w_hptw_resp := true.B 300 s_last_hptw_req := true.B 301 w_last_hptw_resp := true.B 302 } 303 304 305 XSDebug(p"[ptw] level:${level} notFound:${pageFault}\n") 306 307 // perf 308 XSPerfAccumulate("fsm_count", io.req.fire) 309 for (i <- 0 until PtwWidth) { 310 XSPerfAccumulate(s"fsm_count_source${i}", io.req.fire && io.req.bits.req_info.source === i.U) 311 } 312 XSPerfAccumulate("fsm_busy", !idle) 313 XSPerfAccumulate("fsm_idle", idle) 314 XSPerfAccumulate("resp_blocked", io.resp.valid && !io.resp.ready) 315 XSPerfAccumulate("ptw_ppn_af", io.resp.fire && ppn_af) 316 XSPerfAccumulate("mem_count", mem.req.fire) 317 XSPerfAccumulate("mem_cycle", BoolStopWatch(mem.req.fire, mem.resp.fire, true)) 318 XSPerfAccumulate("mem_blocked", mem.req.valid && !mem.req.ready) 319 320 TimeOutAssert(!idle, timeOutThreshold, "page table walker time out") 321 322 val perfEvents = Seq( 323 ("fsm_count ", io.req.fire ), 324 ("fsm_busy ", !idle ), 325 ("fsm_idle ", idle ), 326 ("resp_blocked ", io.resp.valid && !io.resp.ready ), 327 ("mem_count ", mem.req.fire ), 328 ("mem_cycle ", BoolStopWatch(mem.req.fire, mem.resp.fire, true)), 329 ("mem_blocked ", mem.req.valid && !mem.req.ready ), 330 ) 331 generatePerfEvent() 332} 333 334/*========================= LLPTW ==============================*/ 335 336/** LLPTW : Last Level Page Table Walker 337 * the page walker that only takes 4KB(last level) page walk. 338 **/ 339 340class LLPTWInBundle(implicit p: Parameters) extends XSBundle with HasPtwConst { 341 val req_info = Output(new L2TlbInnerBundle()) 342 val ppn = Output(if(HasHExtension) UInt((vpnLen.max(ppnLen)).W) else UInt(ppnLen.W)) 343} 344 345class LLPTWIO(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 346 val in = Flipped(DecoupledIO(new LLPTWInBundle())) 347 val out = DecoupledIO(new Bundle { 348 val req_info = Output(new L2TlbInnerBundle()) 349 val id = Output(UInt(bMemID.W)) 350 val h_resp = Output(new HptwResp) 351 val af = Output(Bool()) 352 }) 353 val mem = new Bundle { 354 val req = DecoupledIO(new L2TlbMemReqBundle()) 355 val resp = Flipped(Valid(new Bundle { 356 val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W)) 357 })) 358 val enq_ptr = Output(UInt(log2Ceil(l2tlbParams.llptwsize).W)) 359 val buffer_it = Output(Vec(l2tlbParams.llptwsize, Bool())) 360 val refill = Output(new L2TlbInnerBundle()) 361 val req_mask = Input(Vec(l2tlbParams.llptwsize, Bool())) 362 } 363 val cache = DecoupledIO(new L2TlbInnerBundle()) 364 val pmp = new Bundle { 365 val req = Valid(new PMPReqBundle()) 366 val resp = Flipped(new PMPRespBundle()) 367 } 368 val hptw = new Bundle { 369 val req = DecoupledIO(new Bundle{ 370 val id = UInt(log2Up(l2tlbParams.llptwsize).W) 371 val gvpn = UInt(vpnLen.W) 372 }) 373 val resp = Flipped(Valid(new Bundle { 374 val id = Output(UInt(log2Up(l2tlbParams.llptwsize).W)) 375 val h_resp = Output(new HptwResp) 376 })) 377 } 378} 379 380class LLPTWEntry(implicit p: Parameters) extends XSBundle with HasPtwConst { 381 val req_info = new L2TlbInnerBundle() 382 val s2xlate = Bool() 383 val ppn = UInt(ppnLen.W) 384 val wait_id = UInt(log2Up(l2tlbParams.llptwsize).W) 385 val af = Bool() 386 val gaf = Bool() 387 val gpf = Bool() 388} 389 390 391class LLPTW(implicit p: Parameters) extends XSModule with HasPtwConst with HasPerfEvents { 392 val io = IO(new LLPTWIO()) 393 val enableS2xlate = io.in.bits.req_info.s2xlate =/= noS2xlate 394 val satp = Mux(enableS2xlate, io.csr.vsatp, io.csr.satp) 395 396 val flush = io.sfence.valid || satp.changed 397 val entries = Reg(Vec(l2tlbParams.llptwsize, new LLPTWEntry())) 398 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) 399 val state = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(state_idle))) 400 401 val is_emptys = state.map(_ === state_idle) 402 val is_mems = state.map(_ === state_mem_req) 403 val is_waiting = state.map(_ === state_mem_waiting) 404 val is_having = state.map(_ === state_mem_out) 405 val is_cache = state.map(_ === state_cache) 406 val is_hptw_req = state.map(_ === state_hptw_req) 407 val is_last_hptw_req = state.map(_ === state_last_hptw_req) 408 409 val full = !ParallelOR(is_emptys).asBool 410 val enq_ptr = ParallelPriorityEncoder(is_emptys) 411 412 val mem_ptr = ParallelPriorityEncoder(is_having) // TODO: optimize timing, bad: entries -> ptr -> entry 413 val mem_arb = Module(new RRArbiter(new LLPTWEntry(), l2tlbParams.llptwsize)) 414 for (i <- 0 until l2tlbParams.llptwsize) { 415 mem_arb.io.in(i).bits := entries(i) 416 mem_arb.io.in(i).valid := is_mems(i) && !io.mem.req_mask(i) 417 } 418 val hyper_arb1 = Module(new RRArbiter(new LLPTWEntry(), l2tlbParams.llptwsize)) 419 for (i <- 0 until l2tlbParams.llptwsize) { 420 hyper_arb1.io.in(i).bits := entries(i) 421 hyper_arb1.io.in(i).valid := is_hptw_req(i) 422 } 423 val hyper_arb2 = Module(new RRArbiter(new LLPTWEntry(), l2tlbParams.llptwsize)) 424 for(i <- 0 until l2tlbParams.llptwsize) { 425 hyper_arb2.io.in(i).bits := entries(i) 426 hyper_arb2.io.in(i).valid := is_last_hptw_req(i) 427 } 428 429 val cache_ptr = ParallelMux(is_cache, (0 until l2tlbParams.llptwsize).map(_.U(log2Up(l2tlbParams.llptwsize).W))) 430 431 // duplicate req 432 // to_wait: wait for the last to access mem, set to mem_resp 433 // to_cache: the last is back just right now, set to mem_cache 434 val dup_vec = state.indices.map(i => 435 dup(io.in.bits.req_info.vpn, entries(i).req_info.vpn) && io.in.bits.req_info.hyperinst === entries(i).req_info.hyperinst 436 ) 437 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.hyperinst === entries(i).req_info.hyperinst // dup with the req fire entry 438 val dup_vec_wait = dup_vec.zip(is_waiting).map{case (d, w) => d && w} // dup with "mem_waiting" entres, sending mem req already 439 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 440 val wait_id = Mux(dup_req_fire, mem_arb.io.chosen, ParallelMux(dup_vec_wait zip entries.map(_.wait_id))) 441 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 442 val to_wait = Cat(dup_vec_wait).orR || dup_req_fire 443 val to_mem_out = dup_wait_resp 444 val to_cache = Cat(dup_vec_having).orR 445 XSError(RegNext(dup_req_fire && Cat(dup_vec_wait).orR, init = false.B), "mem req but some entries already waiting, should not happed") 446 447 XSError(io.in.fire && ((to_mem_out && to_cache) || (to_wait && to_cache)), "llptw enq, to cache conflict with to mem") 448 val mem_resp_hit = RegInit(VecInit(Seq.fill(l2tlbParams.llptwsize)(false.B))) 449 val enq_state_normal = Mux(to_mem_out, state_mem_out, // same to the blew, but the mem resp now 450 Mux(to_wait, state_mem_waiting, 451 Mux(to_cache, state_cache, state_addr_check))) 452 val enq_state = Mux(from_pre(io.in.bits.req_info.source) && enq_state_normal =/= state_addr_check, state_idle, enq_state_normal) 453 when (io.in.fire) { 454 // if prefetch req does not need mem access, just give it up. 455 // so there will be at most 1 + FilterSize entries that needs re-access page cache 456 // so 2 + FilterSize is enough to avoid dead-lock 457 state(enq_ptr) := enq_state 458 entries(enq_ptr).req_info := io.in.bits.req_info 459 entries(enq_ptr).ppn := io.in.bits.ppn 460 entries(enq_ptr).wait_id := Mux(to_wait, wait_id, enq_ptr) 461 entries(enq_ptr).af := false.B 462 entries(enq_ptr).gaf := false.B 463 entries(enq_ptr).gpf := false.B 464 entries(enq_ptr).s2xlate := enableS2xlate 465 mem_resp_hit(enq_ptr) := to_mem_out 466 } 467 468 val enq_ptr_reg = RegNext(enq_ptr) 469 val need_addr_check = RegNext(enq_state === state_addr_check && (io.in.fire() || io.hptw.resp.fire()) && !flush) 470 471 val gpaddr = MakeGAddr(io.in.bits.ppn, getVpnn(io.in.bits.req_info.vpn, 0)) 472 val hpaddr = Cat(io.in.bits.ppn, gpaddr(offLen-1, 0)) 473 474 val addr = Mux(enableS2xlate, hpaddr, MakeAddr(io.in.bits.ppn, getVpnn(io.in.bits.req_info.vpn, 0))) 475 476 io.pmp.req.valid := need_addr_check 477 io.pmp.req.bits.addr := RegEnable(addr, io.in.fire) 478 io.pmp.req.bits.cmd := TlbCmd.read 479 io.pmp.req.bits.size := 3.U // TODO: fix it 480 val pmp_resp_valid = io.pmp.req.valid // same cycle 481 when (pmp_resp_valid) { 482 // NOTE: when pmp resp but state is not addr check, then the entry is dup with other entry, the state was changed before 483 // when dup with the req-ing entry, set to mem_waiting (above codes), and the ld must be false, so dontcare 484 val accessFault = io.pmp.resp.ld || io.pmp.resp.mmio 485 entries(enq_ptr_reg).af := accessFault 486 state(enq_ptr_reg) := Mux(accessFault, state_mem_out, state_mem_req) 487 } 488 489 when (mem_arb.io.out.fire) { 490 for (i <- state.indices) { 491 when (state(i) =/= state_idle && dup(entries(i).req_info.vpn, mem_arb.io.out.bits.req_info.vpn)) { 492 // NOTE: "dup enq set state to mem_wait" -> "sending req set other dup entries to mem_wait" 493 state(i) := state_mem_waiting 494 entries(i).wait_id := mem_arb.io.chosen 495 } 496 } 497 } 498 when (io.mem.resp.fire) { 499 state.indices.map{i => 500 when (state(i) === state_mem_waiting && io.mem.resp.bits.id === entries(i).wait_id) { 501 state(i) := Mux(entries(i).s2xlate, state_last_hptw_req, state_mem_out) 502 mem_resp_hit(i) := true.B 503 } 504 } 505 } 506 507 when (hyper_arb1.io.out.fire()) { 508 for (i <- state.indices) { 509 when (state(i) === state_hptw_req && entries(i).ppn === hyper_arb1.io.out.bits.ppn && entries(i).s2xlate) { 510 state(i) := state_hptw_resp 511 entries(i).wait_id := hyper_arb1.io.chosen 512 } 513 } 514 } 515 516 when (hyper_arb2.io.out.fire()) { 517 for (i <- state.indices) { 518 when (state(i) === state_last_hptw_req && entries(i).ppn === hyper_arb2.io.out.bits.ppn && entries(i).s2xlate) { 519 state(i) := state_last_hptw_resp 520 entries(i).wait_id := hyper_arb2.io.chosen 521 } 522 } 523 } 524 525 when (io.hptw.resp.fire()) { 526 for (i <- state.indices) { 527 when (state(i) === state_hptw_resp && io.hptw.resp.bits.id === entries(i).wait_id) { 528 state(i) := state_addr_check 529 entries(i).gpf := io.hptw.resp.bits.h_resp.gpf 530 entries(i).gaf := io.hptw.resp.bits.h_resp.gaf 531 } 532 when (state(i) === state_last_hptw_resp && io.hptw.resp.bits.id === entries(i).wait_id) { 533 state(i) := state_mem_out 534 entries(i).gpf := io.hptw.resp.bits.h_resp.gpf 535 entries(i).gaf := io.hptw.resp.bits.h_resp.gaf 536 } 537 } 538 } 539 540 when (io.out.fire) { 541 assert(state(mem_ptr) === state_mem_out) 542 state(mem_ptr) := state_idle 543 } 544 mem_resp_hit.map(a => when (a) { a := false.B } ) 545 546 when (io.cache.fire) { 547 state(cache_ptr) := state_idle 548 } 549 XSError(io.out.fire && io.cache.fire && (mem_ptr === cache_ptr), "mem resp and cache fire at the same time at same entry") 550 551 when (flush) { 552 state.map(_ := state_idle) 553 } 554 555 io.in.ready := !full 556 557 io.out.valid := ParallelOR(is_having).asBool 558 io.out.bits.req_info := entries(mem_ptr).req_info 559 io.out.bits.id := mem_ptr 560 io.out.bits.af := entries(mem_ptr).af 561 io.out.bits.h_resp := io.hptw.resp.bits.h_resp 562 563 io.hptw.req.valid := (hyper_arb1.io.out.valid || hyper_arb2.io.out.valid) && !flush 564 io.hptw.req.bits.gvpn := Mux(hyper_arb1.io.out.valid, hyper_arb1.io.out.bits.ppn, hyper_arb2.io.out.bits.ppn) 565 io.hptw.req.bits.id := Mux(hyper_arb1.io.out.valid, hyper_arb1.io.chosen, hyper_arb2.io.chosen) 566 hyper_arb1.io.out.ready := io.hptw.req.ready 567 hyper_arb2.io.out.ready := io.hptw.req.ready 568 569 io.mem.req.valid := mem_arb.io.out.valid && !flush 570 io.mem.req.bits.addr := MakeAddr(mem_arb.io.out.bits.ppn, getVpnn(mem_arb.io.out.bits.req_info.vpn, 0)) 571 io.mem.req.bits.id := mem_arb.io.chosen 572 mem_arb.io.out.ready := io.mem.req.ready 573 io.mem.refill := entries(RegNext(io.mem.resp.bits.id(log2Up(l2tlbParams.llptwsize)-1, 0))).req_info 574 io.mem.buffer_it := mem_resp_hit 575 io.mem.enq_ptr := enq_ptr 576 577 io.cache.valid := Cat(is_cache).orR 578 io.cache.bits := ParallelMux(is_cache, entries.map(_.req_info)) 579 580 XSPerfAccumulate("llptw_in_count", io.in.fire) 581 XSPerfAccumulate("llptw_in_block", io.in.valid && !io.in.ready) 582 for (i <- 0 until 7) { 583 XSPerfAccumulate(s"enq_state${i}", io.in.fire && enq_state === i.U) 584 } 585 for (i <- 0 until (l2tlbParams.llptwsize + 1)) { 586 XSPerfAccumulate(s"util${i}", PopCount(is_emptys.map(!_)) === i.U) 587 XSPerfAccumulate(s"mem_util${i}", PopCount(is_mems) === i.U) 588 XSPerfAccumulate(s"waiting_util${i}", PopCount(is_waiting) === i.U) 589 } 590 XSPerfAccumulate("mem_count", io.mem.req.fire) 591 XSPerfAccumulate("mem_cycle", PopCount(is_waiting) =/= 0.U) 592 XSPerfAccumulate("blocked_in", io.in.valid && !io.in.ready) 593 594 for (i <- 0 until l2tlbParams.llptwsize) { 595 TimeOutAssert(state(i) =/= state_idle, timeOutThreshold, s"missqueue time out no out ${i}") 596 } 597 598 val perfEvents = Seq( 599 ("tlbllptw_incount ", io.in.fire ), 600 ("tlbllptw_inblock ", io.in.valid && !io.in.ready), 601 ("tlbllptw_memcount ", io.mem.req.fire ), 602 ("tlbllptw_memcycle ", PopCount(is_waiting) ), 603 ) 604 generatePerfEvent() 605} 606 607/*========================= HPTW ==============================*/ 608 609/** HPTW : Hypervisor Page Table Walker 610 * the page walker take the virtual machine's page walk. 611 * guest physical address translation, guest physical address -> host physical address 612 **/ 613class HPTWIO()(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst { 614 val req = Flipped(DecoupledIO(new Bundle { 615 val id = UInt(log2Up(l2tlbParams.llptwsize).W) 616 val gvpn = UInt(vpnLen.W) 617 val l1Hit = Bool() 618 val l2Hit = Bool() 619 val ppn = UInt(ppnLen.W) 620 })) 621 val resp = Valid(new Bundle { 622 val resp = Output(new HptwResp()) 623 val id = Output(UInt(bMemID.W)) 624 }) 625 626 val mem = new Bundle { 627 val req = DecoupledIO(new L2TlbMemReqBundle()) 628 val resp = Flipped(ValidIO(UInt(XLEN.W))) 629 val mask = Input(Bool()) 630 } 631 val refill = Output(new Bundle { 632 val req_info = new L2TlbInnerBundle() 633 val level = UInt(log2Up(Level).W) 634 }) 635 val pmp = new Bundle { 636 val req = ValidIO(new PMPReqBundle()) 637 val resp = Flipped(new PMPRespBundle()) 638 } 639} 640 641@chiselName 642class HPTW()(implicit p: Parameters) extends XSModule with HasPtwConst { 643 val io = IO(new HPTWIO) 644 val hgatp = io.csr.hgatp 645 val sfence = io.sfence 646 val flush = sfence.valid || hgatp.changed 647 648 val level = RegInit(0.U(log2Up(Level).W)) 649 val gpaddr = Reg(UInt(GPAddrBits.W)) 650 val vpn = gpaddr(GPAddrBits-1, offLen) 651 val levelNext = level + 1.U 652 val l1Hit = Reg(Bool()) 653 val l2Hit = Reg(Bool()) 654 val ppn = Reg(UInt(ppnLen.W)) 655 val pg_base = MakeAddr(hgatp.ppn, getGVpnn(vpn, 2.U)) 656// val pte = io.mem.resp.bits.MergeRespToPte() 657 val pte = io.mem.resp.bits.asTypeOf(new PteBundle().cloneType) 658 val p_pte = MakeAddr(ppn, getVpnn(vpn, 2.U - level)) 659 val mem_addr = Mux(level === 0.U, pg_base, p_pte) 660 661 //s/w register 662 val s_pmp_check = RegInit(true.B) 663 val s_mem_req = RegInit(true.B) 664 val w_mem_resp = RegInit(true.B) 665 val mem_addr_update = RegInit(true.B) 666 val idle = RegInit(true.B) 667 val finish = WireInit(false.B) 668 669 val sent_to_pmp = !idle && (!s_pmp_check || mem_addr_update) && !finish 670 val pageFault = pte.isPf(level) 671 val accessFault = RegEnable(io.pmp.resp.ld || io.pmp.resp.mmio, sent_to_pmp) 672 673 val ppn_af = pte.isAf() 674 val find_pte = pte.isLeaf() || ppn_af || pageFault 675 676 val resp_valid = !idle && mem_addr_update && ((w_mem_resp && find_pte) || (s_pmp_check && accessFault)) 677 val id = Reg(UInt(log2Up(l2tlbParams.llptwsize).W)) 678 io.req.ready := idle 679 val resp = new HptwResp() 680 resp.apply(pageFault && !accessFault && !ppn_af, accessFault || ppn_af, level, pte, vpn, hgatp.asid) 681 io.resp.valid := resp_valid 682 io.resp.bits.id := id 683 io.resp.bits.resp := resp 684 685 io.pmp.req.valid := DontCare 686 io.pmp.req.bits.addr := mem_addr 687 io.pmp.req.bits.size := 3.U 688 io.pmp.req.bits.cmd := TlbCmd.read 689 690 io.mem.req.valid := !s_mem_req && !io.mem.mask && !accessFault && s_pmp_check 691 io.mem.req.bits.addr := mem_addr 692 io.mem.req.bits.id := HptwReqId.U(bMemID.W) 693 694 io.refill.req_info.vpn := vpn 695 io.refill.level := level 696 when (idle){ 697 when(io.req.fire()){ 698 level := Mux(io.req.bits.l2Hit, 2.U, Mux(io.req.bits.l1Hit, 1.U, 0.U)) 699 idle := false.B 700 gpaddr := Cat(io.req.bits.gvpn, 0.U(offLen.W)) 701 accessFault := false.B 702 s_pmp_check := false.B 703 id := io.req.bits.id 704 l1Hit := io.req.bits.l1Hit 705 l2Hit := io.req.bits.l2Hit 706 ppn := io.req.bits.ppn 707 } 708 } 709 710 when(sent_to_pmp && !mem_addr_update){ 711 s_mem_req := false.B 712 s_pmp_check := true.B 713 } 714 715 when(accessFault && !idle){ 716 s_pmp_check := true.B 717 s_mem_req := true.B 718 w_mem_resp := true.B 719 mem_addr_update := true.B 720 } 721 722 when(io.mem.req.fire()){ 723 s_mem_req := true.B 724 w_mem_resp := false.B 725 } 726 727 when(io.mem.resp.fire() && !w_mem_resp){ 728 ppn := pte.ppn 729 w_mem_resp := true.B 730 mem_addr_update := true.B 731 } 732 733 when(mem_addr_update){ 734 when(!(find_pte || accessFault)){ 735 level := levelNext 736 s_mem_req := false.B 737 mem_addr_update := false.B 738 }.elsewhen(resp_valid){ 739 when(io.resp.fire()){ 740 idle := true.B 741 mem_addr_update := false.B 742 accessFault := false.B 743 } 744 finish := true.B 745 } 746 } 747}