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 chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import chisel3.internal.naming.chiselName 23import xiangshan._ 24import xiangshan.cache.{HasDCacheParameters, MemoryOpConstants} 25import utils._ 26import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 27import freechips.rocketchip.tilelink._ 28import xiangshan.backend.fu.{PMPReqBundle, PMPRespBundle} 29 30/* ptw finite state machine, the actual page table walker 31 */ 32class PtwFsmIO()(implicit p: Parameters) extends PtwBundle { 33 val req = Flipped(DecoupledIO(new Bundle { 34 val source = UInt(bPtwWidth.W) 35 val l1Hit = Bool() 36 val vpn = UInt(vpnLen.W) 37 val ppn = UInt(ppnLen.W) 38 })) 39 val resp = DecoupledIO(new Bundle { 40 val source = UInt(bPtwWidth.W) 41 val resp = new PtwResp 42 }) 43 44 val mq = DecoupledIO(new L2TlbMQInBundle()) 45 46 val mem = new Bundle { 47 val req = DecoupledIO(new L2TlbMemReqBundle()) 48 val resp = Flipped(ValidIO(UInt(XLEN.W))) 49 val mask = Input(Bool()) 50 } 51 val pmp = new Bundle { 52 val req = ValidIO(new PMPReqBundle()) 53 val resp = Flipped(new PMPRespBundle()) 54 } 55 56 val csr = Input(new TlbCsrBundle) 57 val sfence = Input(new SfenceBundle) 58 val refill = Output(new Bundle { 59 val vpn = UInt(vpnLen.W) 60 val level = UInt(log2Up(Level).W) 61 }) 62} 63 64@chiselName 65class PtwFsm()(implicit p: Parameters) extends XSModule with HasPtwConst { 66 val io = IO(new PtwFsmIO) 67 68 val sfence = io.sfence 69 val mem = io.mem 70 val satp = io.csr.satp 71 72 val s_idle :: s_addr_check :: s_mem_req :: s_mem_resp :: s_check_pte :: Nil = Enum(5) 73 val state = RegInit(s_idle) 74 val level = RegInit(0.U(log2Up(Level).W)) 75 val af_level = RegInit(0.U(log2Up(Level).W)) // access fault return this level 76 val ppn = Reg(UInt(ppnLen.W)) 77 val vpn = Reg(UInt(vpnLen.W)) 78 val levelNext = level + 1.U 79 val l1Hit = Reg(Bool()) 80 val memPte = mem.resp.bits.asTypeOf(new PteBundle().cloneType) 81 io.req.ready := state === s_idle 82 83 val finish = WireInit(false.B) 84 val sent_to_pmp = state === s_addr_check || (state === s_check_pte && !finish) 85 val accessFault = RegEnable(io.pmp.resp.ld, sent_to_pmp) 86 val pageFault = memPte.isPf(level) 87 switch (state) { 88 is (s_idle) { 89 when (io.req.fire()) { 90 val req = io.req.bits 91 state := s_addr_check 92 level := Mux(req.l1Hit, 1.U, 0.U) 93 af_level := Mux(req.l1Hit, 1.U, 0.U) 94 ppn := Mux(req.l1Hit, io.req.bits.ppn, satp.ppn) 95 vpn := io.req.bits.vpn 96 l1Hit := req.l1Hit 97 accessFault := false.B 98 } 99 } 100 101 is (s_addr_check) { 102 state := s_mem_req 103 } 104 105 is (s_mem_req) { 106 when (mem.req.fire()) { 107 state := s_mem_resp 108 } 109 when (accessFault) { 110 state := s_check_pte 111 } 112 } 113 114 is (s_mem_resp) { 115 when(mem.resp.fire()) { 116 state := s_check_pte 117 af_level := af_level + 1.U 118 } 119 } 120 121 is (s_check_pte) { 122 when (io.resp.valid) { 123 when (io.resp.fire()) { 124 state := s_idle 125 } 126 finish := true.B 127 }.otherwise { 128 when (io.pmp.resp.ld) { 129 // do nothing 130 }.elsewhen (io.mq.valid) { 131 when (io.mq.fire()) { 132 state := s_idle 133 } 134 finish := true.B 135 }.otherwise { // when level is 1.U, finish 136 assert(level =/= 2.U) 137 level := levelNext 138 state := s_mem_req 139 } 140 } 141 } 142 } 143 144 when (sfence.valid) { 145 state := s_idle 146 accessFault := false.B 147 } 148 149 // memPte is valid when at s_check_pte. when mem.resp.fire, it's not ready. 150 val is_pte = memPte.isLeaf() || memPte.isPf(level) 151 val find_pte = is_pte 152 val to_find_pte = level === 1.U && !is_pte 153 val source = RegEnable(io.req.bits.source, io.req.fire()) 154 io.resp.valid := state === s_check_pte && (find_pte || accessFault) 155 io.resp.bits.source := source 156 io.resp.bits.resp.apply(pageFault && !accessFault, accessFault, Mux(accessFault, af_level, level), memPte, vpn) 157 158 io.mq.valid := state === s_check_pte && to_find_pte && !accessFault 159 io.mq.bits.source := source 160 io.mq.bits.vpn := vpn 161 io.mq.bits.l3.valid := true.B 162 io.mq.bits.l3.bits := memPte.ppn 163 164 assert(level =/= 2.U || level =/= 3.U) 165 166 val l1addr = MakeAddr(satp.ppn, getVpnn(vpn, 2)) 167 val l2addr = MakeAddr(Mux(l1Hit, ppn, memPte.ppn), getVpnn(vpn, 1)) 168 val mem_addr = Mux(af_level === 0.U, l1addr, l2addr) 169 io.pmp.req.valid := DontCare // samecycle, do not use valid 170 io.pmp.req.bits.addr := mem_addr 171 io.pmp.req.bits.size := 3.U // TODO: fix it 172 io.pmp.req.bits.cmd := TlbCmd.read 173 174 mem.req.valid := state === s_mem_req && !io.mem.mask && !accessFault 175 mem.req.bits.addr := mem_addr 176 mem.req.bits.id := MSHRSize.U(bMemID.W) 177 178 io.refill.vpn := vpn 179 io.refill.level := level 180 181 XSDebug(p"[fsm] state:${state} level:${level} notFound:${pageFault}\n") 182 183 // perf 184 XSPerfAccumulate("fsm_count", io.req.fire()) 185 for (i <- 0 until PtwWidth) { 186 XSPerfAccumulate(s"fsm_count_source${i}", io.req.fire() && io.req.bits.source === i.U) 187 } 188 XSPerfAccumulate("fsm_busy", state =/= s_idle) 189 XSPerfAccumulate("fsm_idle", state === s_idle) 190 XSPerfAccumulate("resp_blocked", io.resp.valid && !io.resp.ready) 191 XSPerfAccumulate("mem_count", mem.req.fire()) 192 XSPerfAccumulate("mem_cycle", BoolStopWatch(mem.req.fire, mem.resp.fire(), true)) 193 XSPerfAccumulate("mem_blocked", mem.req.valid && !mem.req.ready) 194 195 TimeOutAssert(state =/= s_idle, timeOutThreshold, "page table walker time out") 196} 197