16d5ddbceSLemover/*************************************************************************************** 2e3da8badSTang Haojin* Copyright (c) 2024 Beijing Institute of Open Source Chip (BOSC) 3e3da8badSTang Haojin* Copyright (c) 2020-2024 Institute of Computing Technology, Chinese Academy of Sciences 4f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory 56d5ddbceSLemover* 66d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2. 76d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2. 86d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at: 96d5ddbceSLemover* http://license.coscl.org.cn/MulanPSL2 106d5ddbceSLemover* 116d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 126d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 136d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 146d5ddbceSLemover* 156d5ddbceSLemover* See the Mulan PSL v2 for more details. 166d5ddbceSLemover***************************************************************************************/ 176d5ddbceSLemover 186d5ddbceSLemoverpackage xiangshan.cache.mmu 196d5ddbceSLemover 208891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters 216d5ddbceSLemoverimport chisel3._ 226d5ddbceSLemoverimport chisel3.util._ 236d5ddbceSLemoverimport xiangshan._ 246d5ddbceSLemoverimport utils._ 253c02ee8fSwakafaimport utility._ 269aca92b9SYinan Xuimport xiangshan.backend.rob.RobPtr 276d5ddbceSLemoverimport xiangshan.backend.fu.util.HasCSRConst 286d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 296d5ddbceSLemoverimport freechips.rocketchip.tilelink._ 305b7ef044SLemoverimport xiangshan.backend.fu.{PMPReqBundle, PMPConfig} 31f1fe8698SLemoverimport xiangshan.backend.fu.PMPBundle 325b7ef044SLemover 336d5ddbceSLemover 346d5ddbceSLemoverabstract class TlbBundle(implicit p: Parameters) extends XSBundle with HasTlbConst 356d5ddbceSLemoverabstract class TlbModule(implicit p: Parameters) extends XSModule with HasTlbConst 366d5ddbceSLemover 37a0301c0dSLemover 386d5ddbceSLemoverclass PtePermBundle(implicit p: Parameters) extends TlbBundle { 396d5ddbceSLemover val d = Bool() 406d5ddbceSLemover val a = Bool() 416d5ddbceSLemover val g = Bool() 426d5ddbceSLemover val u = Bool() 436d5ddbceSLemover val x = Bool() 446d5ddbceSLemover val w = Bool() 456d5ddbceSLemover val r = Bool() 466d5ddbceSLemover 476d5ddbceSLemover override def toPrintable: Printable = { 486d5ddbceSLemover p"d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"// + 496d5ddbceSLemover //(if(hasV) (p"v:${v}") else p"") 506d5ddbceSLemover } 516d5ddbceSLemover} 526d5ddbceSLemover 535b7ef044SLemoverclass TlbPMBundle(implicit p: Parameters) extends TlbBundle { 545b7ef044SLemover val r = Bool() 555b7ef044SLemover val w = Bool() 565b7ef044SLemover val x = Bool() 575b7ef044SLemover val c = Bool() 585b7ef044SLemover val atomic = Bool() 595b7ef044SLemover 605b7ef044SLemover def assign_ap(pm: PMPConfig) = { 615b7ef044SLemover r := pm.r 625b7ef044SLemover w := pm.w 635b7ef044SLemover x := pm.x 645b7ef044SLemover c := pm.c 655b7ef044SLemover atomic := pm.atomic 665b7ef044SLemover } 675b7ef044SLemover} 685b7ef044SLemover 696d5ddbceSLemoverclass TlbPermBundle(implicit p: Parameters) extends TlbBundle { 706d5ddbceSLemover val pf = Bool() // NOTE: if this is true, just raise pf 71b6982e83SLemover val af = Bool() // NOTE: if this is true, just raise af 726d5ddbceSLemover // pagetable perm (software defined) 736d5ddbceSLemover val d = Bool() 746d5ddbceSLemover val a = Bool() 756d5ddbceSLemover val g = Bool() 766d5ddbceSLemover val u = Bool() 776d5ddbceSLemover val x = Bool() 786d5ddbceSLemover val w = Bool() 796d5ddbceSLemover val r = Bool() 806d5ddbceSLemover 81f9ac118cSHaoyuan Feng def apply(item: PtwSectorResp) = { 82b0fa7106SHaoyuan Feng val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 83b0fa7106SHaoyuan Feng this.pf := item.pf 84b0fa7106SHaoyuan Feng this.af := item.af 85b0fa7106SHaoyuan Feng this.d := ptePerm.d 86b0fa7106SHaoyuan Feng this.a := ptePerm.a 87b0fa7106SHaoyuan Feng this.g := ptePerm.g 88b0fa7106SHaoyuan Feng this.u := ptePerm.u 89b0fa7106SHaoyuan Feng this.x := ptePerm.x 90b0fa7106SHaoyuan Feng this.w := ptePerm.w 91b0fa7106SHaoyuan Feng this.r := ptePerm.r 92b0fa7106SHaoyuan Feng 93b0fa7106SHaoyuan Feng this 94b0fa7106SHaoyuan Feng } 95d0de7e4aSpeixiaokun 9687d0ba30Speixiaokun def applyS2(item: HptwResp) = { 97d0de7e4aSpeixiaokun val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 98d0de7e4aSpeixiaokun this.pf := item.gpf 99d0de7e4aSpeixiaokun this.af := item.gaf 100d0de7e4aSpeixiaokun this.d := ptePerm.d 101d0de7e4aSpeixiaokun this.a := ptePerm.a 102d0de7e4aSpeixiaokun this.g := ptePerm.g 103d0de7e4aSpeixiaokun this.u := ptePerm.u 104d0de7e4aSpeixiaokun this.x := ptePerm.x 105d0de7e4aSpeixiaokun this.w := ptePerm.w 106d0de7e4aSpeixiaokun this.r := ptePerm.r 107d0de7e4aSpeixiaokun 108d0de7e4aSpeixiaokun this 109d0de7e4aSpeixiaokun } 11087d0ba30Speixiaokun 111b0fa7106SHaoyuan Feng override def toPrintable: Printable = { 112f9ac118cSHaoyuan Feng p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " 113b0fa7106SHaoyuan Feng } 114b0fa7106SHaoyuan Feng} 115b0fa7106SHaoyuan Feng 116b0fa7106SHaoyuan Fengclass TlbSectorPermBundle(implicit p: Parameters) extends TlbBundle { 117b0fa7106SHaoyuan Feng val pf = Bool() // NOTE: if this is true, just raise pf 118b0fa7106SHaoyuan Feng val af = Bool() // NOTE: if this is true, just raise af 119b0fa7106SHaoyuan Feng // pagetable perm (software defined) 120b0fa7106SHaoyuan Feng val d = Bool() 121b0fa7106SHaoyuan Feng val a = Bool() 122b0fa7106SHaoyuan Feng val g = Bool() 123b0fa7106SHaoyuan Feng val u = Bool() 124b0fa7106SHaoyuan Feng val x = Bool() 125b0fa7106SHaoyuan Feng val w = Bool() 126b0fa7106SHaoyuan Feng val r = Bool() 127b0fa7106SHaoyuan Feng 128f9ac118cSHaoyuan Feng def apply(item: PtwSectorResp) = { 129f1fe8698SLemover val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 130f1fe8698SLemover this.pf := item.pf 131f1fe8698SLemover this.af := item.af 132f1fe8698SLemover this.d := ptePerm.d 133f1fe8698SLemover this.a := ptePerm.a 134f1fe8698SLemover this.g := ptePerm.g 135f1fe8698SLemover this.u := ptePerm.u 136f1fe8698SLemover this.x := ptePerm.x 137f1fe8698SLemover this.w := ptePerm.w 138f1fe8698SLemover this.r := ptePerm.r 139f1fe8698SLemover 140f1fe8698SLemover this 141f1fe8698SLemover } 1426d5ddbceSLemover override def toPrintable: Printable = { 143f9ac118cSHaoyuan Feng p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " 1446d5ddbceSLemover } 1456d5ddbceSLemover} 1466d5ddbceSLemover 1476d5ddbceSLemover// multi-read && single-write 1486d5ddbceSLemover// input is data, output is hot-code(not one-hot) 1496d5ddbceSLemoverclass CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule { 1506d5ddbceSLemover val io = IO(new Bundle { 1516d5ddbceSLemover val r = new Bundle { 1526d5ddbceSLemover val req = Input(Vec(readWidth, gen)) 1536d5ddbceSLemover val resp = Output(Vec(readWidth, Vec(set, Bool()))) 1546d5ddbceSLemover } 1556d5ddbceSLemover val w = Input(new Bundle { 1566d5ddbceSLemover val valid = Bool() 1576d5ddbceSLemover val bits = new Bundle { 1586d5ddbceSLemover val index = UInt(log2Up(set).W) 1596d5ddbceSLemover val data = gen 1606d5ddbceSLemover } 1616d5ddbceSLemover }) 1626d5ddbceSLemover }) 1636d5ddbceSLemover 1646d5ddbceSLemover val wordType = UInt(gen.getWidth.W) 1656d5ddbceSLemover val array = Reg(Vec(set, wordType)) 1666d5ddbceSLemover 1676d5ddbceSLemover io.r.resp.zipWithIndex.map{ case (a,i) => 1686d5ddbceSLemover a := array.map(io.r.req(i).asUInt === _) 1696d5ddbceSLemover } 1706d5ddbceSLemover 1716d5ddbceSLemover when (io.w.valid) { 17276e02f07SLingrui98 array(io.w.bits.index) := io.w.bits.data.asUInt 1736d5ddbceSLemover } 1746d5ddbceSLemover} 1756d5ddbceSLemover 176b0fa7106SHaoyuan Fengclass TlbSectorEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle { 1773ea4388cSHaoyuan Feng require(pageNormal && pageSuper) 178b0fa7106SHaoyuan Feng 1793ea4388cSHaoyuan Feng val tag = UInt(sectorvpnLen.W) 18045f497a4Shappy-lx val asid = UInt(asidLen.W) 1813ea4388cSHaoyuan Feng /* level, 11: 512GB size page(only for sv48) 1823ea4388cSHaoyuan Feng 10: 1GB size page 1833ea4388cSHaoyuan Feng 01: 2MB size page 1843ea4388cSHaoyuan Feng 00: 4KB size page 1853ea4388cSHaoyuan Feng future sv57 extension should change level width 1863ea4388cSHaoyuan Feng */ 1873ea4388cSHaoyuan Feng val level = Some(UInt(2.W)) 1883ea4388cSHaoyuan Feng val ppn = UInt(sectorppnLen.W) 189002c10a4SYanqin Li val pbmt = UInt(ptePbmtLen.W) 190002c10a4SYanqin Li val g_pbmt = UInt(ptePbmtLen.W) 191b0fa7106SHaoyuan Feng val perm = new TlbSectorPermBundle 19263632028SHaoyuan Feng val valididx = Vec(tlbcontiguous, Bool()) 193b0fa7106SHaoyuan Feng val pteidx = Vec(tlbcontiguous, Bool()) 19463632028SHaoyuan Feng val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W)) 195a0301c0dSLemover 196d0de7e4aSpeixiaokun val g_perm = new TlbPermBundle 197d0de7e4aSpeixiaokun val vmid = UInt(vmidLen.W) 198d61cd5eeSpeixiaokun val s2xlate = UInt(2.W) 199d0de7e4aSpeixiaokun 200a0301c0dSLemover 20156728e73SLemover /** level usage: 20256728e73SLemover * !PageSuper: page is only normal, level is None, match all the tag 20356728e73SLemover * !PageNormal: page is only super, level is a Bool(), match high 9*2 parts 20456728e73SLemover * bits0 0: need mid 9bits 20556728e73SLemover * 1: no need mid 9bits 20656728e73SLemover * PageSuper && PageNormal: page hold all the three type, 20756728e73SLemover * bits0 0: need low 9bits 20856728e73SLemover * bits1 0: need mid 9bits 20956728e73SLemover */ 21056728e73SLemover 21186b5ba4aSpeixiaokun def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false, vmid: UInt, hasS2xlate: Bool, onlyS2: Bool = false.B, onlyS1: Bool = false.B): Bool = { 21282978df9Speixiaokun val asid_hit = Mux(hasS2xlate && onlyS2, true.B, if (ignoreAsid) true.B else (this.asid === asid)) 21363632028SHaoyuan Feng val addr_low_hit = valididx(vpn(2, 0)) 21482978df9Speixiaokun val vmid_hit = Mux(hasS2xlate, this.vmid === vmid, true.B) 21586b5ba4aSpeixiaokun val isPageSuper = !(level.getOrElse(0.U) === 0.U) 21686b5ba4aSpeixiaokun val pteidx_hit = Mux(hasS2xlate && !isPageSuper && !onlyS1, pteidx(vpn(2, 0)), true.B) 2173ea4388cSHaoyuan Feng 21856728e73SLemover val tmp_level = level.get 2193ea4388cSHaoyuan Feng val tag_matchs = Wire(Vec(Level + 1, Bool())) 2203ea4388cSHaoyuan Feng tag_matchs(0) := tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) 2213ea4388cSHaoyuan Feng for (i <- 1 until Level + 1) { 2223ea4388cSHaoyuan Feng tag_matchs(i) := tag(vpnnLen * (i + 1) - sectortlbwidth - 1, vpnnLen * i - sectortlbwidth) === vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) 22356728e73SLemover } 2243ea4388cSHaoyuan Feng 2253ea4388cSHaoyuan Feng val level_matchs = Wire(Vec(Level + 1, Bool())) 2263ea4388cSHaoyuan Feng for (i <- 0 until Level) { 2273ea4388cSHaoyuan Feng level_matchs(i) := tag_matchs(i) || tmp_level >= (i + 1).U 2283ea4388cSHaoyuan Feng } 2293ea4388cSHaoyuan Feng level_matchs(Level) := tag_matchs(Level) 2303ea4388cSHaoyuan Feng 2313ea4388cSHaoyuan Feng asid_hit && level_matchs.asUInt.andR && addr_low_hit && vmid_hit && pteidx_hit 232a0301c0dSLemover } 233a0301c0dSLemover 234933ec998Speixiaokun def wbhit(data: PtwRespS2, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false, s2xlate: UInt): Bool = { 235933ec998Speixiaokun val s1vpn = data.s1.entry.tag 236aae99c05Speixiaokun val s2vpn = data.s2.entry.tag(vpnLen - 1, sectortlbwidth) 237933ec998Speixiaokun val wb_vpn = Mux(s2xlate === onlyStage2, s2vpn, s1vpn) 238933ec998Speixiaokun val vpn = Cat(wb_vpn, 0.U(sectortlbwidth.W)) 23963632028SHaoyuan Feng val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 24063632028SHaoyuan Feng val vpn_hit = Wire(Bool()) 24163632028SHaoyuan Feng val index_hit = Wire(Vec(tlbcontiguous, Bool())) 242933ec998Speixiaokun val wb_valididx = Wire(Vec(tlbcontiguous, Bool())) 243ab093818Speixiaokun val hasS2xlate = this.s2xlate =/= noS2xlate 244ab093818Speixiaokun val onlyS1 = this.s2xlate === onlyStage1 245ab093818Speixiaokun val onlyS2 = this.s2xlate === onlyStage2 246ab093818Speixiaokun val pteidx_hit = MuxCase(true.B, Seq( 247ab093818Speixiaokun onlyS2 -> (VecInit(UIntToOH(data.s2.entry.tag(sectortlbwidth - 1, 0))).asUInt === pteidx.asUInt), 248ab093818Speixiaokun hasS2xlate -> (pteidx.asUInt === data.s1.pteidx.asUInt) 249ab093818Speixiaokun )) 250933ec998Speixiaokun wb_valididx := Mux(s2xlate === onlyStage2, VecInit(UIntToOH(data.s2.entry.tag(sectortlbwidth - 1, 0)).asBools), data.s1.valididx) 251933ec998Speixiaokun val s2xlate_hit = s2xlate === this.s2xlate 2523ea4388cSHaoyuan Feng 25363632028SHaoyuan Feng val tmp_level = level.get 2543ea4388cSHaoyuan Feng val tag_matchs = Wire(Vec(Level + 1, Bool())) 2553ea4388cSHaoyuan Feng tag_matchs(0) := tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) 2563ea4388cSHaoyuan Feng for (i <- 1 until Level + 1) { 2573ea4388cSHaoyuan Feng tag_matchs(i) := tag(vpnnLen * (i + 1) - sectortlbwidth - 1, vpnnLen * i - sectortlbwidth) === vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) 25863632028SHaoyuan Feng } 25963632028SHaoyuan Feng 2603ea4388cSHaoyuan Feng val level_matchs = Wire(Vec(Level + 1, Bool())) 2613ea4388cSHaoyuan Feng for (i <- 0 until Level) { 2623ea4388cSHaoyuan Feng level_matchs(i) := tag_matchs(i) || tmp_level >= (i + 1).U 2633ea4388cSHaoyuan Feng } 2643ea4388cSHaoyuan Feng level_matchs(Level) := tag_matchs(Level) 2653ea4388cSHaoyuan Feng vpn_hit := asid_hit && level_matchs.asUInt.andR 2663ea4388cSHaoyuan Feng 26763632028SHaoyuan Feng for (i <- 0 until tlbcontiguous) { 268933ec998Speixiaokun index_hit(i) := wb_valididx(i) && valididx(i) 26963632028SHaoyuan Feng } 27063632028SHaoyuan Feng 27163632028SHaoyuan Feng // For example, tlb req to page cache with vpn 0x10 27263632028SHaoyuan Feng // At this time, 0x13 has not been paged, so page cache only resp 0x10 27363632028SHaoyuan Feng // When 0x13 refill to page cache, previous item will be flushed 27463632028SHaoyuan Feng // Now 0x10 and 0x13 are both valid in page cache 27563632028SHaoyuan Feng // However, when 0x13 refill to tlb, will trigger multi hit 27663632028SHaoyuan Feng // So will only trigger multi-hit when PopCount(data.valididx) = 1 277ab093818Speixiaokun vpn_hit && index_hit.reduce(_ || _) && PopCount(wb_valididx) === 1.U && s2xlate_hit && pteidx_hit 27863632028SHaoyuan Feng } 27963632028SHaoyuan Feng 280d0de7e4aSpeixiaokun def apply(item: PtwRespS2): TlbSectorEntry = { 281d0de7e4aSpeixiaokun this.asid := item.s1.entry.asid 2826f508cb5Speixiaokun val inner_level = MuxLookup(item.s2xlate, 2.U)(Seq( 2837e664aa3Speixiaokun onlyStage1 -> item.s1.entry.level.getOrElse(0.U), 2847e664aa3Speixiaokun onlyStage2 -> item.s2.entry.level.getOrElse(0.U), 2853ea4388cSHaoyuan Feng allStage -> (item.s1.entry.level.getOrElse(0.U) min item.s2.entry.level.getOrElse(0.U)), 2867e664aa3Speixiaokun noS2xlate -> item.s1.entry.level.getOrElse(0.U) 2877e664aa3Speixiaokun )) 2883ea4388cSHaoyuan Feng this.level.map(_ := inner_level) 289d0de7e4aSpeixiaokun this.perm.apply(item.s1) 290002c10a4SYanqin Li this.pbmt := item.s1.entry.pbmt 291d0de7e4aSpeixiaokun 2923ea4388cSHaoyuan Feng val s1tag = item.s1.entry.tag 29397929664SXiaokun-Pei val s2tag = item.s2.entry.tag(gvpnLen - 1, sectortlbwidth) 2949cb05b4dSXiaokun-Pei // if stage1 page is larger than stage2 page, need to merge s1tag and s2tag. 2953ea4388cSHaoyuan Feng val s1tagFix = MuxCase(s1tag, Seq( 2963ea4388cSHaoyuan Feng (item.s1.entry.level.getOrElse(0.U) === 3.U && item.s2.entry.level.getOrElse(0.U) === 2.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 3 - 1, vpnnLen * 2), 0.U((vpnnLen * 2 - sectortlbwidth).W)), 2973ea4388cSHaoyuan Feng (item.s1.entry.level.getOrElse(0.U) === 3.U && item.s2.entry.level.getOrElse(0.U) === 1.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 3 - 1, vpnnLen), 0.U((vpnnLen - sectortlbwidth).W)), 2983ea4388cSHaoyuan Feng (item.s1.entry.level.getOrElse(0.U) === 3.U && item.s2.entry.level.getOrElse(0.U) === 0.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 3 - 1, sectortlbwidth)), 2993ea4388cSHaoyuan Feng (item.s1.entry.level.getOrElse(0.U) === 2.U && item.s2.entry.level.getOrElse(0.U) === 1.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 2 - 1, vpnnLen), 0.U((vpnnLen - sectortlbwidth).W)), 3003ea4388cSHaoyuan Feng (item.s1.entry.level.getOrElse(0.U) === 2.U && item.s2.entry.level.getOrElse(0.U) === 0.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), item.s2.entry.tag(vpnnLen * 2 - 1, sectortlbwidth)), 3013ea4388cSHaoyuan Feng (item.s1.entry.level.getOrElse(0.U) === 1.U && item.s2.entry.level.getOrElse(0.U) === 0.U) -> Cat(item.s1.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth), item.s2.entry.tag(vpnnLen - 1, sectortlbwidth)) 3029cb05b4dSXiaokun-Pei )) 3039cb05b4dSXiaokun-Pei this.tag := Mux(item.s2xlate === onlyStage2, s2tag, Mux(item.s2xlate === allStage, s1tagFix, s1tag)) 3043ea4388cSHaoyuan Feng val s2page_pageSuper = item.s2.entry.level.getOrElse(0.U) =/= 0.U 305496c751cSpeixiaokun this.pteidx := Mux(item.s2xlate === onlyStage2, VecInit(UIntToOH(item.s2.entry.tag(sectortlbwidth - 1, 0)).asBools), item.s1.pteidx) 30686b5ba4aSpeixiaokun val s2_valid = Mux(s2page_pageSuper, VecInit(Seq.fill(tlbcontiguous)(true.B)), VecInit(UIntToOH(item.s2.entry.tag(sectortlbwidth - 1, 0)).asBools)) 30786b5ba4aSpeixiaokun this.valididx := Mux(item.s2xlate === onlyStage2, s2_valid, item.s1.valididx) 3089cb05b4dSXiaokun-Pei // if stage2 page is larger than stage1 page, need to merge s2tag and s2ppn to get a new s2ppn. 3093ea4388cSHaoyuan Feng val s1ppn = item.s1.entry.ppn 31082978df9Speixiaokun val s1ppn_low = item.s1.ppn_low 3113ea4388cSHaoyuan Feng val s2ppn = MuxLookup(item.s2.entry.level.getOrElse(0.U), item.s2.entry.ppn(ppnLen - 1, sectortlbwidth))(Seq( 3123ea4388cSHaoyuan Feng 3.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 3), item.s2.entry.tag(vpnnLen * 3 - 1, sectortlbwidth)), 3133ea4388cSHaoyuan Feng 2.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 2), item.s2.entry.tag(vpnnLen * 2 - 1, sectortlbwidth)), 3148c34f10bSpeixiaokun 1.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen), item.s2.entry.tag(vpnnLen - 1, sectortlbwidth)) 3158c34f10bSpeixiaokun )) 3163ea4388cSHaoyuan Feng val s2ppn_tmp = MuxLookup(item.s2.entry.level.getOrElse(0.U), item.s2.entry.ppn(ppnLen - 1, 0))(Seq( 3173ea4388cSHaoyuan Feng 3.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 3), item.s2.entry.tag(vpnnLen * 3 - 1, 0)), 3183ea4388cSHaoyuan Feng 2.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 2), item.s2.entry.tag(vpnnLen * 2 - 1, 0)), 3198c34f10bSpeixiaokun 1.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen), item.s2.entry.tag(vpnnLen - 1, 0)) 3208c34f10bSpeixiaokun )) 3218c34f10bSpeixiaokun val s2ppn_low = VecInit(Seq.fill(tlbcontiguous)(s2ppn_tmp(sectortlbwidth - 1, 0))) 32282978df9Speixiaokun this.ppn := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, s1ppn, s2ppn) 32382978df9Speixiaokun this.ppn_low := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, s1ppn_low, s2ppn_low) 324d61cd5eeSpeixiaokun this.vmid := item.s1.entry.vmid.getOrElse(0.U) 325002c10a4SYanqin Li this.g_pbmt := item.s2.entry.pbmt 32687d0ba30Speixiaokun this.g_perm.applyS2(item.s2) 32782978df9Speixiaokun this.s2xlate := item.s2xlate 328a0301c0dSLemover this 329a0301c0dSLemover } 330a0301c0dSLemover 33156728e73SLemover // 4KB is normal entry, 2MB/1GB is considered as super entry 33256728e73SLemover def is_normalentry(): Bool = { 33356728e73SLemover if (!pageSuper) { true.B } 33456728e73SLemover else if (!pageNormal) { false.B } 33556728e73SLemover else { level.get === 0.U } 33656728e73SLemover } 3375cf62c1aSLemover 338d0de7e4aSpeixiaokun 33956728e73SLemover def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = { 34056728e73SLemover val inner_level = level.getOrElse(0.U) 3413ea4388cSHaoyuan Feng val ppn_res = Cat(ppn(sectorppnLen - 1, vpnnLen * 3 - sectortlbwidth), 3423ea4388cSHaoyuan Feng Mux(inner_level >= "b11".U , vpn(vpnnLen * 3 - 1, vpnnLen * 2), ppn(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth)), 3433ea4388cSHaoyuan Feng Mux(inner_level >= "b10".U , vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth)), 3443ea4388cSHaoyuan Feng Mux(inner_level >= "b01".U , vpn(vpnnLen - 1, 0), Cat(ppn(vpnnLen - sectortlbwidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0))))) 34556728e73SLemover 3463ea4388cSHaoyuan Feng if (saveLevel) 3473ea4388cSHaoyuan Feng RegEnable(ppn_res, valid) 3483ea4388cSHaoyuan Feng else 3493ea4388cSHaoyuan Feng ppn_res 350a0301c0dSLemover } 351a0301c0dSLemover 352d61cd5eeSpeixiaokun def hasS2xlate(): Bool = { 353d61cd5eeSpeixiaokun this.s2xlate =/= noS2xlate 354d61cd5eeSpeixiaokun } 355d61cd5eeSpeixiaokun 356a0301c0dSLemover override def toPrintable: Printable = { 357a0301c0dSLemover val inner_level = level.getOrElse(2.U) 35845f497a4Shappy-lx p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}" 359a0301c0dSLemover } 360a0301c0dSLemover 361a0301c0dSLemover} 362a0301c0dSLemover 3636d5ddbceSLemoverobject TlbCmd { 3646d5ddbceSLemover def read = "b00".U 3656d5ddbceSLemover def write = "b01".U 3666d5ddbceSLemover def exec = "b10".U 3676d5ddbceSLemover 3686d5ddbceSLemover def atom_read = "b100".U // lr 3696d5ddbceSLemover def atom_write = "b101".U // sc / amo 3706d5ddbceSLemover 3716d5ddbceSLemover def apply() = UInt(3.W) 3726d5ddbceSLemover def isRead(a: UInt) = a(1,0)===read 3736d5ddbceSLemover def isWrite(a: UInt) = a(1,0)===write 3746d5ddbceSLemover def isExec(a: UInt) = a(1,0)===exec 3756d5ddbceSLemover 3766d5ddbceSLemover def isAtom(a: UInt) = a(2) 377a79fef67Swakafa def isAmo(a: UInt) = a===atom_write // NOTE: sc mixed 3786d5ddbceSLemover} 3796d5ddbceSLemover 380002c10a4SYanqin Li// Svpbmt extension 381002c10a4SYanqin Liobject Pbmt { 382002c10a4SYanqin Li def pma: UInt = "b00".U // None 383002c10a4SYanqin Li def nc: UInt = "b01".U // Non-cacheable, idempotent, weakly-ordered (RVWMO), main memory 384002c10a4SYanqin Li def io: UInt = "b10".U // Non-cacheable, non-idempotent, strongly-ordered (I/O ordering), I/O 385002c10a4SYanqin Li def rsvd: UInt = "b11".U // Reserved for future standard use 386002c10a4SYanqin Li def width: Int = 2 387002c10a4SYanqin Li 388002c10a4SYanqin Li def apply() = UInt(2.W) 389002c10a4SYanqin Li def isUncache(a: UInt) = a===nc || a===io 390002c10a4SYanqin Li} 391002c10a4SYanqin Li 39203efd994Shappy-lxclass TlbStorageIO(nSets: Int, nWays: Int, ports: Int, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 393a0301c0dSLemover val r = new Bundle { 394a0301c0dSLemover val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 395a0301c0dSLemover val vpn = Output(UInt(vpnLen.W)) 396875ae3b4SXiaokun-Pei val s2xlate = Output(UInt(2.W)) 397a0301c0dSLemover }))) 398a0301c0dSLemover val resp = Vec(ports, ValidIO(new Bundle{ 399a0301c0dSLemover val hit = Output(Bool()) 40003efd994Shappy-lx val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 401002c10a4SYanqin Li val pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W))) 402002c10a4SYanqin Li val g_pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W))) 403b0fa7106SHaoyuan Feng val perm = Vec(nDups, Output(new TlbSectorPermBundle())) 404d0de7e4aSpeixiaokun val g_perm = Vec(nDups, Output(new TlbPermBundle())) 405d0de7e4aSpeixiaokun val s2xlate = Vec(nDups, Output(UInt(2.W))) 406a0301c0dSLemover })) 407a0301c0dSLemover } 408a0301c0dSLemover val w = Flipped(ValidIO(new Bundle { 409a0301c0dSLemover val wayIdx = Output(UInt(log2Up(nWays).W)) 410d0de7e4aSpeixiaokun val data = Output(new PtwRespS2) 411a0301c0dSLemover })) 4123889e11eSLemover val access = Vec(ports, new ReplaceAccessBundle(nSets, nWays)) 413a0301c0dSLemover 41482978df9Speixiaokun def r_req_apply(valid: Bool, vpn: UInt, i: Int, s2xlate:UInt): Unit = { 415a0301c0dSLemover this.r.req(i).valid := valid 416a0301c0dSLemover this.r.req(i).bits.vpn := vpn 417d0de7e4aSpeixiaokun this.r.req(i).bits.s2xlate := s2xlate 418d0de7e4aSpeixiaokun 419a0301c0dSLemover } 420a0301c0dSLemover 421a0301c0dSLemover def r_resp_apply(i: Int) = { 422002c10a4SYanqin Li (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, this.r.resp(i).bits.g_perm, this.r.resp(i).bits.pbmt, this.r.resp(i).bits.g_pbmt) 423a0301c0dSLemover } 424a0301c0dSLemover 425d0de7e4aSpeixiaokun def w_apply(valid: Bool, wayIdx: UInt, data: PtwRespS2): Unit = { 426a0301c0dSLemover this.w.valid := valid 427a0301c0dSLemover this.w.bits.wayIdx := wayIdx 428a0301c0dSLemover this.w.bits.data := data 429a0301c0dSLemover } 430a0301c0dSLemover 431a0301c0dSLemover} 432a0301c0dSLemover 43303efd994Shappy-lxclass TlbStorageWrapperIO(ports: Int, q: TLBParameters, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 434f1fe8698SLemover val r = new Bundle { 435f1fe8698SLemover val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 436f1fe8698SLemover val vpn = Output(UInt(vpnLen.W)) 437d0de7e4aSpeixiaokun val s2xlate = Output(UInt(2.W)) 438f1fe8698SLemover }))) 439f1fe8698SLemover val resp = Vec(ports, ValidIO(new Bundle{ 440f1fe8698SLemover val hit = Output(Bool()) 44103efd994Shappy-lx val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 442002c10a4SYanqin Li val pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W))) 443002c10a4SYanqin Li val g_pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W))) 44403efd994Shappy-lx val perm = Vec(nDups, Output(new TlbPermBundle())) 445d0de7e4aSpeixiaokun val g_perm = Vec(nDups, Output(new TlbPermBundle())) 446d0de7e4aSpeixiaokun val s2xlate = Vec(nDups, Output(UInt(2.W))) 447f1fe8698SLemover })) 448f1fe8698SLemover } 449f1fe8698SLemover val w = Flipped(ValidIO(new Bundle { 450d0de7e4aSpeixiaokun val data = Output(new PtwRespS2) 451f1fe8698SLemover })) 452f1fe8698SLemover val replace = if (q.outReplace) Flipped(new TlbReplaceIO(ports, q)) else null 453f1fe8698SLemover 454d0de7e4aSpeixiaokun def r_req_apply(valid: Bool, vpn: UInt, i: Int, s2xlate: UInt): Unit = { 455f1fe8698SLemover this.r.req(i).valid := valid 456f1fe8698SLemover this.r.req(i).bits.vpn := vpn 457d0de7e4aSpeixiaokun this.r.req(i).bits.s2xlate := s2xlate 458f1fe8698SLemover } 459f1fe8698SLemover 460f1fe8698SLemover def r_resp_apply(i: Int) = { 461002c10a4SYanqin Li (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, this.r.resp(i).bits.g_perm, this.r.resp(i).bits.s2xlate, this.r.resp(i).bits.pbmt, this.r.resp(i).bits.g_pbmt) 462f1fe8698SLemover } 463f1fe8698SLemover 464d0de7e4aSpeixiaokun def w_apply(valid: Bool, data: PtwRespS2): Unit = { 465f1fe8698SLemover this.w.valid := valid 466f1fe8698SLemover this.w.bits.data := data 467f1fe8698SLemover } 468f1fe8698SLemover} 469f1fe8698SLemover 4703889e11eSLemoverclass ReplaceAccessBundle(nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 4713889e11eSLemover val sets = Output(UInt(log2Up(nSets).W)) 4723889e11eSLemover val touch_ways = ValidIO(Output(UInt(log2Up(nWays).W))) 4733889e11eSLemover} 4743889e11eSLemover 475a0301c0dSLemoverclass ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 4763889e11eSLemover val access = Vec(Width, Flipped(new ReplaceAccessBundle(nSets, nWays))) 477a0301c0dSLemover 478a0301c0dSLemover val refillIdx = Output(UInt(log2Up(nWays).W)) 479a0301c0dSLemover val chosen_set = Flipped(Output(UInt(log2Up(nSets).W))) 480a0301c0dSLemover 481a0301c0dSLemover def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = { 48253b8f1a7SLemover for ((ac_rep, ac_tlb) <- access.zip(in.map(a => a.access.map(b => b)).flatten)) { 48353b8f1a7SLemover ac_rep := ac_tlb 484a0301c0dSLemover } 48553b8f1a7SLemover this.chosen_set := get_set_idx(vpn, nSets) 48653b8f1a7SLemover in.map(a => a.refillIdx := this.refillIdx) 487a0301c0dSLemover } 488a0301c0dSLemover} 489a0301c0dSLemover 490a0301c0dSLemoverclass TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends 491a0301c0dSLemover TlbBundle { 492f9ac118cSHaoyuan Feng val page = new ReplaceIO(Width, q.NSets, q.NWays) 493a0301c0dSLemover 494a0301c0dSLemover def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = { 495f9ac118cSHaoyuan Feng this.page.apply_sep(in.map(_.page), vpn) 496a0301c0dSLemover } 497a0301c0dSLemover 498a0301c0dSLemover} 499a0301c0dSLemover 5008744445eSMaxpicca-Liclass MemBlockidxBundle(implicit p: Parameters) extends TlbBundle { 5018744445eSMaxpicca-Li val is_ld = Bool() 5028744445eSMaxpicca-Li val is_st = Bool() 503be867ebcSAnzooooo val idx = UInt(log2Ceil(VirtualLoadQueueMaxStoreQueueSize).W) 5048744445eSMaxpicca-Li} 5058744445eSMaxpicca-Li 5066d5ddbceSLemoverclass TlbReq(implicit p: Parameters) extends TlbBundle { 507ca2f90a6SLemover val vaddr = Output(UInt(VAddrBits.W)) 508ca2f90a6SLemover val cmd = Output(TlbCmd()) 509d0de7e4aSpeixiaokun val hyperinst = Output(Bool()) 510d0de7e4aSpeixiaokun val hlvx = Output(Bool()) 51126af847eSgood-circle val size = Output(UInt(log2Ceil(log2Ceil(VLEN/8)+1).W)) 512f1fe8698SLemover val kill = Output(Bool()) // Use for blocked tlb that need sync with other module like icache 5138744445eSMaxpicca-Li val memidx = Output(new MemBlockidxBundle) 514b52348aeSWilliam Wang // do not translate, but still do pmp/pma check 515b52348aeSWilliam Wang val no_translate = Output(Bool()) 516149a2326Sweiding liu val pmp_addr = Output(UInt(PAddrBits.W)) // load s1 send prefetch paddr 5176d5ddbceSLemover val debug = new Bundle { 518ca2f90a6SLemover val pc = Output(UInt(XLEN.W)) 519f1fe8698SLemover val robIdx = Output(new RobPtr) 520ca2f90a6SLemover val isFirstIssue = Output(Bool()) 5216d5ddbceSLemover } 5226d5ddbceSLemover 523f1fe8698SLemover // Maybe Block req needs a kill: for itlb, itlb and icache may not sync, itlb should wait icache to go ahead 5246d5ddbceSLemover override def toPrintable: Printable = { 525f1fe8698SLemover p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} kill:${kill} pc:0x${Hexadecimal(debug.pc)} robIdx:${debug.robIdx}" 5266d5ddbceSLemover } 5276d5ddbceSLemover} 5286d5ddbceSLemover 529b6982e83SLemoverclass TlbExceptionBundle(implicit p: Parameters) extends TlbBundle { 530b6982e83SLemover val ld = Output(Bool()) 531b6982e83SLemover val st = Output(Bool()) 532b6982e83SLemover val instr = Output(Bool()) 533b6982e83SLemover} 534b6982e83SLemover 53503efd994Shappy-lxclass TlbResp(nDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 53603efd994Shappy-lx val paddr = Vec(nDups, Output(UInt(PAddrBits.W))) 537d0de7e4aSpeixiaokun val gpaddr = Vec(nDups, Output(UInt(GPAddrBits.W))) 538002c10a4SYanqin Li val pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W))) 539ca2f90a6SLemover val miss = Output(Bool()) 54003efd994Shappy-lx val excp = Vec(nDups, new Bundle { 541d0de7e4aSpeixiaokun val gpf = new TlbExceptionBundle() 542b6982e83SLemover val pf = new TlbExceptionBundle() 543b6982e83SLemover val af = new TlbExceptionBundle() 54403efd994Shappy-lx }) 545ca2f90a6SLemover val ptwBack = Output(Bool()) // when ptw back, wake up replay rs's state 5468744445eSMaxpicca-Li val memidx = Output(new MemBlockidxBundle) 5476d5ddbceSLemover 5488744445eSMaxpicca-Li val debug = new Bundle { 5498744445eSMaxpicca-Li val robIdx = Output(new RobPtr) 5508744445eSMaxpicca-Li val isFirstIssue = Output(Bool()) 5518744445eSMaxpicca-Li } 5526d5ddbceSLemover override def toPrintable: Printable = { 55303efd994Shappy-lx p"paddr:0x${Hexadecimal(paddr(0))} miss:${miss} excp.pf: ld:${excp(0).pf.ld} st:${excp(0).pf.st} instr:${excp(0).pf.instr} ptwBack:${ptwBack}" 5546d5ddbceSLemover } 5556d5ddbceSLemover} 5566d5ddbceSLemover 55703efd994Shappy-lxclass TlbRequestIO(nRespDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 5586d5ddbceSLemover val req = DecoupledIO(new TlbReq) 559c3b763d0SYinan Xu val req_kill = Output(Bool()) 56003efd994Shappy-lx val resp = Flipped(DecoupledIO(new TlbResp(nRespDups))) 5616d5ddbceSLemover} 5626d5ddbceSLemover 5636d5ddbceSLemoverclass TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 5646d5ddbceSLemover val req = Vec(Width, DecoupledIO(new PtwReq)) 565d0de7e4aSpeixiaokun val resp = Flipped(DecoupledIO(new PtwRespS2)) 5666d5ddbceSLemover 5676d5ddbceSLemover 5686d5ddbceSLemover override def toPrintable: Printable = { 5696d5ddbceSLemover p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 5706d5ddbceSLemover } 5716d5ddbceSLemover} 5726d5ddbceSLemover 5738744445eSMaxpicca-Liclass TlbPtwIOwithMemIdx(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 5748744445eSMaxpicca-Li val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx)) 575d0de7e4aSpeixiaokun val resp = Flipped(DecoupledIO(new PtwRespS2withMemIdx())) 5768744445eSMaxpicca-Li 5778744445eSMaxpicca-Li 5788744445eSMaxpicca-Li override def toPrintable: Printable = { 5798744445eSMaxpicca-Li p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 5808744445eSMaxpicca-Li } 5818744445eSMaxpicca-Li} 5828744445eSMaxpicca-Li 583185e6164SHaoyuan Fengclass TlbHintReq(implicit p: Parameters) extends TlbBundle { 584185e6164SHaoyuan Feng val id = Output(UInt(log2Up(loadfiltersize).W)) 585185e6164SHaoyuan Feng val full = Output(Bool()) 586185e6164SHaoyuan Feng} 587185e6164SHaoyuan Feng 588185e6164SHaoyuan Fengclass TLBHintResp(implicit p: Parameters) extends TlbBundle { 589185e6164SHaoyuan Feng val id = Output(UInt(log2Up(loadfiltersize).W)) 590185e6164SHaoyuan Feng // When there are multiple matching entries for PTW resp in filter 591185e6164SHaoyuan Feng // e.g. vaddr 0, 0x80000000. vaddr 1, 0x80010000 592185e6164SHaoyuan Feng // these two vaddrs are not in a same 4K Page, so will send to ptw twice 593185e6164SHaoyuan Feng // However, when ptw resp, if they are in a 1G or 2M huge page 594185e6164SHaoyuan Feng // The two entries will both hit, and both need to replay 595185e6164SHaoyuan Feng val replay_all = Output(Bool()) 596185e6164SHaoyuan Feng} 597185e6164SHaoyuan Feng 598185e6164SHaoyuan Fengclass TlbHintIO(implicit p: Parameters) extends TlbBundle { 59971489510SXuan Hu val req = Vec(backendParams.LdExuCnt, new TlbHintReq) 600185e6164SHaoyuan Feng val resp = ValidIO(new TLBHintResp) 601185e6164SHaoyuan Feng} 602185e6164SHaoyuan Feng 60345f497a4Shappy-lxclass MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle { 604b052b972SLemover val sfence = Input(new SfenceBundle) 605b052b972SLemover val csr = Input(new TlbCsrBundle) 606f1fe8698SLemover 607f1fe8698SLemover def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 608f1fe8698SLemover this.sfence <> sfence 609f1fe8698SLemover this.csr <> csr 610f1fe8698SLemover } 611f1fe8698SLemover 612f1fe8698SLemover // overwrite satp. write satp will cause flushpipe but csr.priv won't 613f1fe8698SLemover // satp will be dealyed several cycles from writing, but csr.priv won't 614f1fe8698SLemover // so inside mmu, these two signals should be divided 615f1fe8698SLemover def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle, satp: TlbSatpBundle) = { 616f1fe8698SLemover this.sfence <> sfence 617f1fe8698SLemover this.csr <> csr 618f1fe8698SLemover this.csr.satp := satp 619f1fe8698SLemover } 620a0301c0dSLemover} 6216d5ddbceSLemover 6228744445eSMaxpicca-Liclass TlbRefilltoMemIO()(implicit p: Parameters) extends TlbBundle { 6238744445eSMaxpicca-Li val valid = Bool() 6248744445eSMaxpicca-Li val memidx = new MemBlockidxBundle 6258744445eSMaxpicca-Li} 6268744445eSMaxpicca-Li 62703efd994Shappy-lxclass TlbIO(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends 62845f497a4Shappy-lx MMUIOBaseBundle { 629f57f7f2aSYangyu Chen val hartId = Input(UInt(hartIdLen.W)) 63003efd994Shappy-lx val requestor = Vec(Width, Flipped(new TlbRequestIO(nRespDups))) 631f1fe8698SLemover val flushPipe = Vec(Width, Input(Bool())) 632a4f9c77fSpeixiaokun val redirect = Flipped(ValidIO(new Redirect)) // flush the signal need_gpa in tlb 6338744445eSMaxpicca-Li val ptw = new TlbPtwIOwithMemIdx(Width) 6348744445eSMaxpicca-Li val refill_to_mem = Output(new TlbRefilltoMemIO()) 635a0301c0dSLemover val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null 63626af847eSgood-circle val pmp = Vec(Width, ValidIO(new PMPReqBundle(q.lgMaxSize))) 637185e6164SHaoyuan Feng val tlbreplay = Vec(Width, Output(Bool())) 638a0301c0dSLemover} 639a0301c0dSLemover 640f1fe8698SLemoverclass VectorTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle { 6418744445eSMaxpicca-Li val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx())) 642a0301c0dSLemover val resp = Flipped(DecoupledIO(new Bundle { 643d0de7e4aSpeixiaokun val data = new PtwRespS2withMemIdx 644a0301c0dSLemover val vector = Output(Vec(Width, Bool())) 645a4f9c77fSpeixiaokun val getGpa = Output(Vec(Width, Bool())) 646a0301c0dSLemover })) 647a0301c0dSLemover 6488744445eSMaxpicca-Li def connect(normal: TlbPtwIOwithMemIdx): Unit = { 649f1fe8698SLemover req <> normal.req 650f1fe8698SLemover resp.ready := normal.resp.ready 651f1fe8698SLemover normal.resp.bits := resp.bits.data 652f1fe8698SLemover normal.resp.valid := resp.valid 653a0301c0dSLemover } 6546d5ddbceSLemover} 6556d5ddbceSLemover 65692e3bfefSLemover/**************************** L2TLB *************************************/ 6576d5ddbceSLemoverabstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst 65892e3bfefSLemoverabstract class PtwModule(outer: L2TLB) extends LazyModuleImp(outer) 6596d5ddbceSLemover with HasXSParameter with HasPtwConst 6606d5ddbceSLemover 6616d5ddbceSLemoverclass PteBundle(implicit p: Parameters) extends PtwBundle{ 662002c10a4SYanqin Li val n = UInt(pteNLen.W) 663002c10a4SYanqin Li val pbmt = UInt(ptePbmtLen.W) 6646d5ddbceSLemover val reserved = UInt(pteResLen.W) 6650d94d540SHaoyuan Feng val ppn_high = UInt(ppnHignLen.W) 6666d5ddbceSLemover val ppn = UInt(ppnLen.W) 667002c10a4SYanqin Li val rsw = UInt(pteRswLen.W) 6686d5ddbceSLemover val perm = new Bundle { 6696d5ddbceSLemover val d = Bool() 6706d5ddbceSLemover val a = Bool() 6716d5ddbceSLemover val g = Bool() 6726d5ddbceSLemover val u = Bool() 6736d5ddbceSLemover val x = Bool() 6746d5ddbceSLemover val w = Bool() 6756d5ddbceSLemover val r = Bool() 6766d5ddbceSLemover val v = Bool() 6776d5ddbceSLemover } 6786d5ddbceSLemover 6796d5ddbceSLemover def unaligned(level: UInt) = { 6803ea4388cSHaoyuan Feng isLeaf() && 6813ea4388cSHaoyuan Feng !(level === 0.U || 6826d5ddbceSLemover level === 1.U && ppn(vpnnLen-1, 0) === 0.U || 6833ea4388cSHaoyuan Feng level === 2.U && ppn(vpnnLen*2-1, 0) === 0.U || 6843ea4388cSHaoyuan Feng level === 3.U && ppn(vpnnLen*3-1, 0) === 0.U) 6856d5ddbceSLemover } 6866d5ddbceSLemover 68797929664SXiaokun-Pei def isLeaf() = { 68897929664SXiaokun-Pei (perm.r || perm.x || perm.w) && perm.v 68997929664SXiaokun-Pei } 69097929664SXiaokun-Pei 691135df6a7SXiaokun-Pei def isNext() = { 692135df6a7SXiaokun-Pei !(perm.r || perm.x || perm.w) && perm.v 693135df6a7SXiaokun-Pei } 694135df6a7SXiaokun-Pei 6956d5ddbceSLemover def isPf(level: UInt) = { 696135df6a7SXiaokun-Pei val pf = WireInit(false.B) 697*5ec7c921SXiaokun-Pei when (reserved =/= 0.U){ 698*5ec7c921SXiaokun-Pei pf := true.B 699*5ec7c921SXiaokun-Pei }.elsewhen(pbmt === 3.U){ 700*5ec7c921SXiaokun-Pei pf := true.B 701*5ec7c921SXiaokun-Pei }.elsewhen (isNext()) { 702*5ec7c921SXiaokun-Pei pf := (perm.u || perm.a || perm.d || n =/= 0.U || pbmt =/= 0.U) 703135df6a7SXiaokun-Pei }.elsewhen (!perm.v || (!perm.r && perm.w)) { 704135df6a7SXiaokun-Pei pf := true.B 705135df6a7SXiaokun-Pei }.otherwise{ 70657ff69b1SXiaokun-Pei pf := unaligned(level) 707135df6a7SXiaokun-Pei } 708135df6a7SXiaokun-Pei pf 709135df6a7SXiaokun-Pei } 710135df6a7SXiaokun-Pei 711135df6a7SXiaokun-Pei def isGpf(level: UInt) = { 712135df6a7SXiaokun-Pei val gpf = WireInit(false.B) 713135df6a7SXiaokun-Pei when (isNext()) { 714135df6a7SXiaokun-Pei gpf := (perm.u || perm.a || perm.d ) 715135df6a7SXiaokun-Pei }.elsewhen (!perm.v || (!perm.r && perm.w)) { 716135df6a7SXiaokun-Pei gpf := true.B 717135df6a7SXiaokun-Pei }.elsewhen (!perm.u) { 718135df6a7SXiaokun-Pei gpf := true.B 719135df6a7SXiaokun-Pei }.otherwise{ 72057ff69b1SXiaokun-Pei gpf := unaligned(level) 721135df6a7SXiaokun-Pei } 722135df6a7SXiaokun-Pei gpf 7236d5ddbceSLemover } 7246d5ddbceSLemover 7254e811ad7SHaoyuan Feng // ppn of Xiangshan is 48 - 12 bits but ppn of sv48 is 44 bits 7260d94d540SHaoyuan Feng // access fault will be raised when ppn >> ppnLen is not zero 7274e811ad7SHaoyuan Feng def isAf(): Bool = { 7284e811ad7SHaoyuan Feng !(ppn_high === 0.U) 7290d94d540SHaoyuan Feng } 7300d94d540SHaoyuan Feng 7310b1b8ed1SXiaokun-Pei def isStage1Gpf(mode: UInt) = { 7320b1b8ed1SXiaokun-Pei val sv39_high = Cat(ppn_high, ppn) >> (GPAddrBitsSv39x4 - offLen) 7330b1b8ed1SXiaokun-Pei val sv48_high = Cat(ppn_high, ppn) >> (GPAddrBitsSv48x4 - offLen) 7340b1b8ed1SXiaokun-Pei !(Mux(mode === Sv39, sv39_high, Mux(mode === Sv48, sv48_high, 0.U)) === 0.U) 7354c0e0181SXiaokun-Pei } 7364c0e0181SXiaokun-Pei 7376d5ddbceSLemover def getPerm() = { 7386d5ddbceSLemover val pm = Wire(new PtePermBundle) 7396d5ddbceSLemover pm.d := perm.d 7406d5ddbceSLemover pm.a := perm.a 7416d5ddbceSLemover pm.g := perm.g 7426d5ddbceSLemover pm.u := perm.u 7436d5ddbceSLemover pm.x := perm.x 7446d5ddbceSLemover pm.w := perm.w 7456d5ddbceSLemover pm.r := perm.r 7466d5ddbceSLemover pm 7476d5ddbceSLemover } 7484c0e0181SXiaokun-Pei def getPPN() = { 7494c0e0181SXiaokun-Pei Cat(ppn_high, ppn) 7504c0e0181SXiaokun-Pei } 7516d5ddbceSLemover override def toPrintable: Printable = { 7526d5ddbceSLemover p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}" 7536d5ddbceSLemover } 7546d5ddbceSLemover} 7556d5ddbceSLemover 7566d5ddbceSLemoverclass PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle { 7576d5ddbceSLemover val tag = UInt(tagLen.W) 75845f497a4Shappy-lx val asid = UInt(asidLen.W) 759d0de7e4aSpeixiaokun val vmid = if (HasHExtension) Some(UInt(vmidLen.W)) else None 760002c10a4SYanqin Li val pbmt = UInt(ptePbmtLen.W) 76197929664SXiaokun-Pei val ppn = UInt(gvpnLen.W) 7626d5ddbceSLemover val perm = if (hasPerm) Some(new PtePermBundle) else None 7633ea4388cSHaoyuan Feng val level = if (hasLevel) Some(UInt(log2Up(Level + 1).W)) else None 764bc063562SLemover val prefetch = Bool() 7658d8ac704SLemover val v = Bool() 7666d5ddbceSLemover 76756728e73SLemover def is_normalentry(): Bool = { 76856728e73SLemover if (!hasLevel) true.B 76956728e73SLemover else level.get === 2.U 77056728e73SLemover } 77156728e73SLemover 772f1fe8698SLemover def genPPN(vpn: UInt): UInt = { 7733ea4388cSHaoyuan Feng if (!hasLevel) { 7743ea4388cSHaoyuan Feng ppn 7753ea4388cSHaoyuan Feng } else { 7763ea4388cSHaoyuan Feng MuxLookup(level.get, 0.U)(Seq( 7773ea4388cSHaoyuan Feng 3.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*3), vpn(vpnnLen*3-1, 0)), 7783ea4388cSHaoyuan Feng 2.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)), 779f1fe8698SLemover 1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)), 7803ea4388cSHaoyuan Feng 0.U -> ppn) 781f1fe8698SLemover ) 782f1fe8698SLemover } 7833ea4388cSHaoyuan Feng } 784f1fe8698SLemover 785d0de7e4aSpeixiaokun //s2xlate control whether compare vmid or not 786b188e334Speixiaokun def hit(vpn: UInt, asid: UInt, vasid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false, s2xlate: Bool) = { 7876d5ddbceSLemover require(vpn.getWidth == vpnLen) 788cccfc98dSLemover// require(this.asid.getWidth <= asid.getWidth) 789b188e334Speixiaokun val asid_value = Mux(s2xlate, vasid, asid) 790b188e334Speixiaokun val asid_hit = if (ignoreAsid) true.B else (this.asid === asid_value) 791d61cd5eeSpeixiaokun val vmid_hit = Mux(s2xlate, (this.vmid.getOrElse(0.U) === vmid), true.B) 7926d5ddbceSLemover if (allType) { 7936d5ddbceSLemover require(hasLevel) 7943ea4388cSHaoyuan Feng val tag_match = Wire(Vec(4, Bool())) // 512GB, 1GB, 2MB or 4KB, not parameterized here 7953ea4388cSHaoyuan Feng for (i <- 0 until 3) { 7963ea4388cSHaoyuan Feng tag_match(i) := tag(vpnnLen * (i + 1) - 1, vpnnLen * i) === vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) 7973ea4388cSHaoyuan Feng } 7983ea4388cSHaoyuan Feng tag_match(3) := tag(tagLen - 1, vpnnLen * 3) === vpn(tagLen - 1, vpnnLen * 3) 79945f497a4Shappy-lx 8003ea4388cSHaoyuan Feng val level_match = MuxLookup(level.getOrElse(0.U), false.B)(Seq( 8013ea4388cSHaoyuan Feng 3.U -> tag_match(3), 8023ea4388cSHaoyuan Feng 2.U -> (tag_match(3) && tag_match(2)), 8033ea4388cSHaoyuan Feng 1.U -> (tag_match(3) && tag_match(2) && tag_match(1)), 8043ea4388cSHaoyuan Feng 0.U -> (tag_match(3) && tag_match(2) && tag_match(1) && tag_match(0))) 8053ea4388cSHaoyuan Feng ) 8063ea4388cSHaoyuan Feng 8073ea4388cSHaoyuan Feng asid_hit && vmid_hit && level_match 8086d5ddbceSLemover } else if (hasLevel) { 8093ea4388cSHaoyuan Feng val tag_match = Wire(Vec(3, Bool())) // SuperPage, 512GB, 1GB or 2MB 8103ea4388cSHaoyuan Feng tag_match(0) := tag(tagLen - 1, tagLen - vpnnLen - extendVpnnBits) === vpn(vpnLen - 1, vpnLen - vpnnLen - extendVpnnBits) 8113ea4388cSHaoyuan Feng for (i <- 1 until 3) { 8123ea4388cSHaoyuan Feng tag_match(i) := tag(tagLen - vpnnLen * i - extendVpnnBits - 1, tagLen - vpnnLen * (i + 1) - extendVpnnBits) === vpn(vpnLen - vpnnLen * i - extendVpnnBits - 1, vpnLen - vpnnLen * (i + 1) - extendVpnnBits) 8133ea4388cSHaoyuan Feng } 81445f497a4Shappy-lx 8153ea4388cSHaoyuan Feng val level_match = MuxLookup(level.getOrElse(0.U), false.B)(Seq( 8163ea4388cSHaoyuan Feng 3.U -> tag_match(0), 8173ea4388cSHaoyuan Feng 2.U -> (tag_match(0) && tag_match(1)), 8183ea4388cSHaoyuan Feng 1.U -> (tag_match(0) && tag_match(1) && tag_match(2))) 8193ea4388cSHaoyuan Feng ) 8203ea4388cSHaoyuan Feng 8213ea4388cSHaoyuan Feng asid_hit && vmid_hit && level_match 8226d5ddbceSLemover } else { 82382978df9Speixiaokun asid_hit && vmid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen) 8246d5ddbceSLemover } 8256d5ddbceSLemover } 8266d5ddbceSLemover 827e3da8badSTang Haojin def refill(vpn: UInt, asid: UInt, vmid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B): Unit = { 82845f497a4Shappy-lx require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside 82945f497a4Shappy-lx 8306d5ddbceSLemover tag := vpn(vpnLen - 1, vpnLen - tagLen) 831002c10a4SYanqin Li pbmt := pte.asTypeOf(new PteBundle().cloneType).pbmt 832a0301c0dSLemover ppn := pte.asTypeOf(new PteBundle().cloneType).ppn 833a0301c0dSLemover perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm) 83445f497a4Shappy-lx this.asid := asid 835d61cd5eeSpeixiaokun this.vmid.map(_ := vmid) 836bc063562SLemover this.prefetch := prefetch 8378d8ac704SLemover this.v := valid 8386d5ddbceSLemover this.level.map(_ := level) 8396d5ddbceSLemover } 8406d5ddbceSLemover 8418d8ac704SLemover def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) = { 8426d5ddbceSLemover val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel)) 8438d8ac704SLemover e.refill(vpn, asid, pte, level, prefetch, valid) 8446d5ddbceSLemover e 8456d5ddbceSLemover } 8466d5ddbceSLemover 8476d5ddbceSLemover 848f1fe8698SLemover 8496d5ddbceSLemover override def toPrintable: Printable = { 8506d5ddbceSLemover // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}" 851002c10a4SYanqin Li p"tag:0x${Hexadecimal(tag)} pbmt: ${pbmt} ppn:0x${Hexadecimal(ppn)} " + 8526d5ddbceSLemover (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") + 853bc063562SLemover (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") + 854bc063562SLemover p"prefetch:${prefetch}" 8556d5ddbceSLemover } 8566d5ddbceSLemover} 8576d5ddbceSLemover 85863632028SHaoyuan Fengclass PtwSectorEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwEntry(tagLen, hasPerm, hasLevel) { 85997929664SXiaokun-Pei override val ppn = UInt(sectorptePPNLen.W) 86063632028SHaoyuan Feng} 86163632028SHaoyuan Feng 86263632028SHaoyuan Fengclass PtwMergeEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwSectorEntry(tagLen, hasPerm, hasLevel) { 86363632028SHaoyuan Feng val ppn_low = UInt(sectortlbwidth.W) 86463632028SHaoyuan Feng val af = Bool() 86563632028SHaoyuan Feng val pf = Bool() 86663632028SHaoyuan Feng} 86763632028SHaoyuan Feng 868b9e793f1SHaoyuan Fengclass PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean, hasReservedBitforMbist: Boolean)(implicit p: Parameters) extends PtwBundle { 8696d5ddbceSLemover require(log2Up(num)==log2Down(num)) 8701f4a7c0cSLemover // NOTE: hasPerm means that is leaf or not. 8716d5ddbceSLemover 8726d5ddbceSLemover val tag = UInt(tagLen.W) 87345f497a4Shappy-lx val asid = UInt(asidLen.W) 8744c0e0181SXiaokun-Pei val vmid = Some(UInt(vmidLen.W)) 875002c10a4SYanqin Li val pbmts = Vec(num, UInt(ptePbmtLen.W)) 8764c0e0181SXiaokun-Pei val ppns = Vec(num, UInt(gvpnLen.W)) 8776d5ddbceSLemover val vs = Vec(num, Bool()) 878854ed348SHaoyuan Feng val af = Vec(num, Bool()) 8796d5ddbceSLemover val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None 880bc063562SLemover val prefetch = Bool() 881b9e793f1SHaoyuan Feng val reservedbit = if(hasReservedBitforMbist) Some(Bool()) else None 8826d5ddbceSLemover // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1") 8831f4a7c0cSLemover // NOTE: vs is used for different usage: 8841f4a7c0cSLemover // for l3, which store the leaf(leaves), vs is page fault or not. 8851f4a7c0cSLemover // for l2, which shoule not store leaf, vs is valid or not, that will anticipate in hit check 8861f4a7c0cSLemover // Because, l2 should not store leaf(no perm), it doesn't store perm. 8871f4a7c0cSLemover // If l2 hit a leaf, the perm is still unavailble. Should still page walk. Complex but nothing helpful. 8881f4a7c0cSLemover // TODO: divide vs into validVec and pfVec 8891f4a7c0cSLemover // for l2: may valid but pf, so no need for page walk, return random pte with pf. 8906d5ddbceSLemover 8916d5ddbceSLemover def tagClip(vpn: UInt) = { 8926d5ddbceSLemover require(vpn.getWidth == vpnLen) 8936d5ddbceSLemover vpn(vpnLen - 1, vpnLen - tagLen) 8946d5ddbceSLemover } 8956d5ddbceSLemover 8966d5ddbceSLemover def sectorIdxClip(vpn: UInt, level: Int) = { 8976d5ddbceSLemover getVpnClip(vpn, level)(log2Up(num) - 1, 0) 8986d5ddbceSLemover } 8996d5ddbceSLemover 900b188e334Speixiaokun def hit(vpn: UInt, asid: UInt, vasid: UInt, vmid:UInt, ignoreAsid: Boolean = false, s2xlate: Bool) = { 901b188e334Speixiaokun val asid_value = Mux(s2xlate, vasid, asid) 902b188e334Speixiaokun val asid_hit = if (ignoreAsid) true.B else (this.asid === asid_value) 903d61cd5eeSpeixiaokun val vmid_hit = Mux(s2xlate, this.vmid.getOrElse(0.U) === vmid, true.B) 9044ed5afbdSXiaokun-Pei asid_hit && vmid_hit && tag === tagClip(vpn) && (if (hasPerm) true.B else vs(sectorIdxClip(vpn, level))) 9056d5ddbceSLemover } 9066d5ddbceSLemover 9074ed5afbdSXiaokun-Pei def genEntries(vpn: UInt, asid: UInt, vmid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool, s2xlate: UInt) = { 9086d5ddbceSLemover require((data.getWidth / XLEN) == num, 9095854c1edSLemover s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}") 9106d5ddbceSLemover 911b9e793f1SHaoyuan Feng val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm, hasReservedBitforMbist)) 9126d5ddbceSLemover ps.tag := tagClip(vpn) 91345f497a4Shappy-lx ps.asid := asid 914d61cd5eeSpeixiaokun ps.vmid.map(_ := vmid) 915bc063562SLemover ps.prefetch := prefetch 9166d5ddbceSLemover for (i <- 0 until num) { 9176d5ddbceSLemover val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle) 918002c10a4SYanqin Li ps.pbmts(i) := pte.pbmt 9196d5ddbceSLemover ps.ppns(i) := pte.ppn 9206d5ddbceSLemover ps.vs(i) := !pte.isPf(levelUInt) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf()) 9214ed5afbdSXiaokun-Pei ps.af(i) := Mux(s2xlate === allStage, false.B, pte.isAf()) // if allstage, this refill is from ptw or llptw, so the af is invalid 9226d5ddbceSLemover ps.perms.map(_(i) := pte.perm) 9236d5ddbceSLemover } 924b9e793f1SHaoyuan Feng ps.reservedbit.map(_ := true.B) 9256d5ddbceSLemover ps 9266d5ddbceSLemover } 9276d5ddbceSLemover 9286d5ddbceSLemover override def toPrintable: Printable = { 9296d5ddbceSLemover // require(num == 4, "if num is not 4, please comment this toPrintable") 9306d5ddbceSLemover // NOTE: if num is not 4, please comment this toPrintable 9316d5ddbceSLemover val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle))) 932002c10a4SYanqin Li p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} pbmt:${printVec(pbmts)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " + 9336d5ddbceSLemover (if (hasPerm) p"perms:${printVec(permsInner)}" else p"") 9346d5ddbceSLemover } 9356d5ddbceSLemover} 9366d5ddbceSLemover 937b9e793f1SHaoyuan Fengclass PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean, hasReservedBitforMbist: Boolean = false)(implicit p: Parameters) extends PtwBundle { 938b9e793f1SHaoyuan Feng val entries = new PtwEntries(num, tagLen, level, hasPerm, hasReservedBitforMbist) 9397196f5a2SLemover 9403889e11eSLemover val ecc_block = XLEN 9413889e11eSLemover val ecc_info = get_ecc_info() 942eef81af7SHaoyuan Feng val ecc = if (l2tlbParams.enablePTWECC) Some(UInt(ecc_info._1.W)) else None 9433889e11eSLemover 9443889e11eSLemover def get_ecc_info(): (Int, Int, Int, Int) = { 9453889e11eSLemover val eccBits_per = eccCode.width(ecc_block) - ecc_block 9463889e11eSLemover 9473889e11eSLemover val data_length = entries.getWidth 9483889e11eSLemover val data_align_num = data_length / ecc_block 9493889e11eSLemover val data_not_align = (data_length % ecc_block) != 0 // ugly code 9503889e11eSLemover val data_unalign_length = data_length - data_align_num * ecc_block 9513889e11eSLemover val eccBits_unalign = eccCode.width(data_unalign_length) - data_unalign_length 9523889e11eSLemover 9533889e11eSLemover val eccBits = eccBits_per * data_align_num + eccBits_unalign 9543889e11eSLemover (eccBits, eccBits_per, data_align_num, data_unalign_length) 9553889e11eSLemover } 9563889e11eSLemover 9573889e11eSLemover def encode() = { 958935edac4STang Haojin val data = entries.asUInt 9593889e11eSLemover val ecc_slices = Wire(Vec(ecc_info._3, UInt(ecc_info._2.W))) 9603889e11eSLemover for (i <- 0 until ecc_info._3) { 9613889e11eSLemover ecc_slices(i) := eccCode.encode(data((i+1)*ecc_block-1, i*ecc_block)) >> ecc_block 9623889e11eSLemover } 9633889e11eSLemover if (ecc_info._4 != 0) { 9643889e11eSLemover val ecc_unaligned = eccCode.encode(data(data.getWidth-1, ecc_info._3*ecc_block)) >> ecc_info._4 965eef81af7SHaoyuan Feng ecc.map(_ := Cat(ecc_unaligned, ecc_slices.asUInt)) 966eef81af7SHaoyuan Feng } else { ecc.map(_ := ecc_slices.asUInt)} 9673889e11eSLemover } 9683889e11eSLemover 9693889e11eSLemover def decode(): Bool = { 970935edac4STang Haojin val data = entries.asUInt 9713889e11eSLemover val res = Wire(Vec(ecc_info._3 + 1, Bool())) 9723889e11eSLemover for (i <- 0 until ecc_info._3) { 973eef81af7SHaoyuan Feng res(i) := {if (ecc_info._2 != 0) eccCode.decode(Cat(ecc.get((i+1)*ecc_info._2-1, i*ecc_info._2), data((i+1)*ecc_block-1, i*ecc_block))).error else false.B} 9743889e11eSLemover } 9755197bac8SZiyue-Zhang if (ecc_info._2 != 0 && ecc_info._4 != 0) { 9763889e11eSLemover res(ecc_info._3) := eccCode.decode( 977eef81af7SHaoyuan Feng Cat(ecc.get(ecc_info._1-1, ecc_info._2*ecc_info._3), data(data.getWidth-1, ecc_info._3*ecc_block))).error 9783889e11eSLemover } else { res(ecc_info._3) := false.B } 9793889e11eSLemover 9803889e11eSLemover Cat(res).orR 9813889e11eSLemover } 9823889e11eSLemover 9834ed5afbdSXiaokun-Pei def gen(vpn: UInt, asid: UInt, vmid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool, s2xlate: UInt) = { 9844ed5afbdSXiaokun-Pei this.entries := entries.genEntries(vpn, asid, vmid, data, levelUInt, prefetch, s2xlate) 9853889e11eSLemover this.encode() 9863889e11eSLemover } 9877196f5a2SLemover} 9887196f5a2SLemover 9896d5ddbceSLemoverclass PtwReq(implicit p: Parameters) extends PtwBundle { 99082978df9Speixiaokun val vpn = UInt(vpnLen.W) //vpn or gvpn 99186b5ba4aSpeixiaokun val s2xlate = UInt(2.W) 992d61cd5eeSpeixiaokun def hasS2xlate(): Bool = { 993d61cd5eeSpeixiaokun this.s2xlate =/= noS2xlate 994d61cd5eeSpeixiaokun } 995e3da8badSTang Haojin def isOnlyStage2: Bool = { 99686b5ba4aSpeixiaokun this.s2xlate === onlyStage2 99786b5ba4aSpeixiaokun } 9986d5ddbceSLemover override def toPrintable: Printable = { 9996d5ddbceSLemover p"vpn:0x${Hexadecimal(vpn)}" 10006d5ddbceSLemover } 10016d5ddbceSLemover} 10026d5ddbceSLemover 10038744445eSMaxpicca-Liclass PtwReqwithMemIdx(implicit p: Parameters) extends PtwReq { 10048744445eSMaxpicca-Li val memidx = new MemBlockidxBundle 1005a4f9c77fSpeixiaokun val getGpa = Bool() // this req is to get gpa when having guest page fault 10068744445eSMaxpicca-Li} 10078744445eSMaxpicca-Li 10086d5ddbceSLemoverclass PtwResp(implicit p: Parameters) extends PtwBundle { 10096d5ddbceSLemover val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true) 10106d5ddbceSLemover val pf = Bool() 1011b6982e83SLemover val af = Bool() 10126d5ddbceSLemover 101345f497a4Shappy-lx def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = { 10145854c1edSLemover this.entry.level.map(_ := level) 10155854c1edSLemover this.entry.tag := vpn 10165854c1edSLemover this.entry.perm.map(_ := pte.getPerm()) 10175854c1edSLemover this.entry.ppn := pte.ppn 1018002c10a4SYanqin Li this.entry.pbmt := pte.pbmt 1019bc063562SLemover this.entry.prefetch := DontCare 102045f497a4Shappy-lx this.entry.asid := asid 10218d8ac704SLemover this.entry.v := !pf 10225854c1edSLemover this.pf := pf 1023b6982e83SLemover this.af := af 10245854c1edSLemover } 10255854c1edSLemover 10266d5ddbceSLemover override def toPrintable: Printable = { 1027b6982e83SLemover p"entry:${entry} pf:${pf} af:${af}" 10286d5ddbceSLemover } 10296d5ddbceSLemover} 10306d5ddbceSLemover 1031d0de7e4aSpeixiaokunclass HptwResp(implicit p: Parameters) extends PtwBundle { 103297929664SXiaokun-Pei val entry = new PtwEntry(tagLen = gvpnLen, hasPerm = true, hasLevel = true) 1033d0de7e4aSpeixiaokun val gpf = Bool() 1034d0de7e4aSpeixiaokun val gaf = Bool() 1035d0de7e4aSpeixiaokun 1036d0de7e4aSpeixiaokun def apply(gpf: Bool, gaf: Bool, level: UInt, pte: PteBundle, vpn: UInt, vmid: UInt) = { 10372b16f0c2SXiaokun-Pei val resp_pte = Mux(gaf, 0.U.asTypeOf(pte), pte) 1038d0de7e4aSpeixiaokun this.entry.level.map(_ := level) 1039d0de7e4aSpeixiaokun this.entry.tag := vpn 10402b16f0c2SXiaokun-Pei this.entry.perm.map(_ := resp_pte.getPerm()) 10412b16f0c2SXiaokun-Pei this.entry.ppn := resp_pte.ppn 1042002c10a4SYanqin Li this.entry.pbmt := resp_pte.pbmt 1043d0de7e4aSpeixiaokun this.entry.prefetch := DontCare 1044d0de7e4aSpeixiaokun this.entry.asid := DontCare 1045d61cd5eeSpeixiaokun this.entry.vmid.map(_ := vmid) 1046d0de7e4aSpeixiaokun this.entry.v := !gpf 1047d0de7e4aSpeixiaokun this.gpf := gpf 1048d0de7e4aSpeixiaokun this.gaf := gaf 1049d0de7e4aSpeixiaokun } 1050d0de7e4aSpeixiaokun 1051cda84113Speixiaokun def genPPNS2(vpn: UInt): UInt = { 10528c34f10bSpeixiaokun MuxLookup(entry.level.get, 0.U)(Seq( 10533ea4388cSHaoyuan Feng 3.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 3), vpn(vpnnLen * 3 - 1, 0)), 10543ea4388cSHaoyuan Feng 2.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 2), vpn(vpnnLen * 2 - 1, 0)), 1055cda84113Speixiaokun 1.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen), vpn(vpnnLen - 1, 0)), 10563ea4388cSHaoyuan Feng 0.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, 0)) 1057d0de7e4aSpeixiaokun )) 1058d0de7e4aSpeixiaokun } 1059d0de7e4aSpeixiaokun 1060d0de7e4aSpeixiaokun def hit(gvpn: UInt, vmid: UInt): Bool = { 1061d61cd5eeSpeixiaokun val vmid_hit = this.entry.vmid.getOrElse(0.U) === vmid 10623ea4388cSHaoyuan Feng val tag_match = Wire(Vec(4, Bool())) // 512GB, 1GB, 2MB or 4KB, not parameterized here 10633ea4388cSHaoyuan Feng for (i <- 0 until 3) { 10643ea4388cSHaoyuan Feng tag_match(i) := entry.tag(vpnnLen * (i + 1) - 1, vpnnLen * i) === gvpn(vpnnLen * (i + 1) - 1, vpnnLen * i) 1065d0de7e4aSpeixiaokun } 106697929664SXiaokun-Pei tag_match(3) := entry.tag(gvpnLen - 1, vpnnLen * 3) === gvpn(gvpnLen - 1, vpnnLen * 3) 1067d0de7e4aSpeixiaokun 10683ea4388cSHaoyuan Feng val level_match = MuxLookup(entry.level.getOrElse(0.U), false.B)(Seq( 10693ea4388cSHaoyuan Feng 3.U -> tag_match(3), 10703ea4388cSHaoyuan Feng 2.U -> (tag_match(3) && tag_match(2)), 10713ea4388cSHaoyuan Feng 1.U -> (tag_match(3) && tag_match(2) && tag_match(1)), 10723ea4388cSHaoyuan Feng 0.U -> (tag_match(3) && tag_match(2) && tag_match(1) && tag_match(0))) 10733ea4388cSHaoyuan Feng ) 107463632028SHaoyuan Feng 10753ea4388cSHaoyuan Feng vmid_hit && level_match 107663632028SHaoyuan Feng } 107763632028SHaoyuan Feng} 107863632028SHaoyuan Feng 107963632028SHaoyuan Fengclass PtwSectorResp(implicit p: Parameters) extends PtwBundle { 108063632028SHaoyuan Feng val entry = new PtwSectorEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true) 108163632028SHaoyuan Feng val addr_low = UInt(sectortlbwidth.W) 108263632028SHaoyuan Feng val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W)) 108363632028SHaoyuan Feng val valididx = Vec(tlbcontiguous, Bool()) 1084b0fa7106SHaoyuan Feng val pteidx = Vec(tlbcontiguous, Bool()) 108563632028SHaoyuan Feng val pf = Bool() 108663632028SHaoyuan Feng val af = Bool() 108763632028SHaoyuan Feng 1088d0de7e4aSpeixiaokun 108963632028SHaoyuan Feng def genPPN(vpn: UInt): UInt = { 109045f43e6eSTang Haojin MuxLookup(entry.level.get, 0.U)(Seq( 10913ea4388cSHaoyuan Feng 3.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 3 - sectortlbwidth), vpn(vpnnLen * 3 - 1, 0)), 10923ea4388cSHaoyuan Feng 2.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), vpn(vpnnLen * 2 - 1, 0)), 109363632028SHaoyuan Feng 1.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen - sectortlbwidth), vpn(vpnnLen - 1, 0)), 10943ea4388cSHaoyuan Feng 0.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0)))) 109563632028SHaoyuan Feng ) 109663632028SHaoyuan Feng } 109763632028SHaoyuan Feng 1098ad8d4021SXiaokun-Pei def isLeaf() = { 1099ad8d4021SXiaokun-Pei (entry.perm.get.r || entry.perm.get.x || entry.perm.get.w) && entry.v 1100ad8d4021SXiaokun-Pei } 1101ad8d4021SXiaokun-Pei 1102ad8d4021SXiaokun-Pei def isFakePte() = { 1103ad8d4021SXiaokun-Pei !pf && !entry.v 1104ad8d4021SXiaokun-Pei } 1105ad8d4021SXiaokun-Pei 1106d0de7e4aSpeixiaokun def hit(vpn: UInt, asid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false, s2xlate: Bool): Bool = { 110763632028SHaoyuan Feng require(vpn.getWidth == vpnLen) 110863632028SHaoyuan Feng // require(this.asid.getWidth <= asid.getWidth) 110963632028SHaoyuan Feng val asid_hit = if (ignoreAsid) true.B else (this.entry.asid === asid) 1110d61cd5eeSpeixiaokun val vmid_hit = Mux(s2xlate, this.entry.vmid.getOrElse(0.U) === vmid, true.B) 111163632028SHaoyuan Feng if (allType) { 111263632028SHaoyuan Feng val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0)) 11133ea4388cSHaoyuan Feng val tag_match = Wire(Vec(4, Bool())) // 512GB, 1GB, 2MB or 4KB, not parameterized here 11143ea4388cSHaoyuan Feng tag_match(0) := entry.tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) 11153ea4388cSHaoyuan Feng for (i <- 1 until 3) { 11163ea4388cSHaoyuan Feng tag_match(i) := entry.tag(vpnnLen * (i + 1) - sectortlbwidth - 1, vpnnLen * i - sectortlbwidth) === vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) 11173ea4388cSHaoyuan Feng } 11183ea4388cSHaoyuan Feng tag_match(3) := entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth) === vpn(vpnLen - 1, vpnnLen * 3) 111963632028SHaoyuan Feng 11203ea4388cSHaoyuan Feng val level_match = MuxLookup(entry.level.getOrElse(0.U), false.B)(Seq( 11213ea4388cSHaoyuan Feng 3.U -> tag_match(3), 11223ea4388cSHaoyuan Feng 2.U -> (tag_match(3) && tag_match(2)), 11233ea4388cSHaoyuan Feng 1.U -> (tag_match(3) && tag_match(2) && tag_match(1)), 11243ea4388cSHaoyuan Feng 0.U -> (tag_match(3) && tag_match(2) && tag_match(1) && tag_match(0))) 11253ea4388cSHaoyuan Feng ) 11263ea4388cSHaoyuan Feng 11273ea4388cSHaoyuan Feng asid_hit && vmid_hit && level_match && addr_low_hit 112863632028SHaoyuan Feng } else { 112963632028SHaoyuan Feng val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0)) 11303ea4388cSHaoyuan Feng val tag_match = Wire(Vec(3, Bool())) // SuperPage, 512GB, 1GB or 2MB 11313ea4388cSHaoyuan Feng for (i <- 0 until 3) { 11323ea4388cSHaoyuan Feng tag_match(i) := entry.tag(sectorvpnLen - vpnnLen * i - 1, sectorvpnLen - vpnnLen * (i + 1)) === vpn(vpnLen - vpnnLen * i - 1, vpnLen - vpnnLen * (i + 1)) 11333ea4388cSHaoyuan Feng } 113463632028SHaoyuan Feng 11353ea4388cSHaoyuan Feng val level_match = MuxLookup(entry.level.getOrElse(0.U), false.B)(Seq( 11363ea4388cSHaoyuan Feng 3.U -> tag_match(0), 11373ea4388cSHaoyuan Feng 2.U -> (tag_match(0) && tag_match(1)), 11383ea4388cSHaoyuan Feng 1.U -> (tag_match(0) && tag_match(1) && tag_match(2))) 11393ea4388cSHaoyuan Feng ) 11403ea4388cSHaoyuan Feng 11413ea4388cSHaoyuan Feng asid_hit && vmid_hit && level_match && addr_low_hit 114263632028SHaoyuan Feng } 114363632028SHaoyuan Feng } 114463632028SHaoyuan Feng} 114563632028SHaoyuan Feng 114663632028SHaoyuan Fengclass PtwMergeResp(implicit p: Parameters) extends PtwBundle { 114763632028SHaoyuan Feng val entry = Vec(tlbcontiguous, new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)) 114863632028SHaoyuan Feng val pteidx = Vec(tlbcontiguous, Bool()) 114963632028SHaoyuan Feng val not_super = Bool() 115063632028SHaoyuan Feng 1151d0de7e4aSpeixiaokun def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt, vmid:UInt, addr_low : UInt, not_super : Boolean = true) = { 115263632028SHaoyuan Feng assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!") 11537263b595SXiaokun-Pei val resp_pte = pte 115463632028SHaoyuan Feng val ptw_resp = Wire(new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)) 115597929664SXiaokun-Pei ptw_resp.ppn := resp_pte.getPPN()(ptePPNLen - 1, sectortlbwidth) 11564c0e0181SXiaokun-Pei ptw_resp.ppn_low := resp_pte.getPPN()(sectortlbwidth - 1, 0) 1157002c10a4SYanqin Li ptw_resp.pbmt := resp_pte.pbmt 115863632028SHaoyuan Feng ptw_resp.level.map(_ := level) 11592b16f0c2SXiaokun-Pei ptw_resp.perm.map(_ := resp_pte.getPerm()) 116063632028SHaoyuan Feng ptw_resp.tag := vpn(vpnLen - 1, sectortlbwidth) 116163632028SHaoyuan Feng ptw_resp.pf := pf 116263632028SHaoyuan Feng ptw_resp.af := af 1163ad8d4021SXiaokun-Pei ptw_resp.v := resp_pte.perm.v 116463632028SHaoyuan Feng ptw_resp.prefetch := DontCare 116563632028SHaoyuan Feng ptw_resp.asid := asid 1166eb4bf3f2Speixiaokun ptw_resp.vmid.map(_ := vmid) 116763632028SHaoyuan Feng this.pteidx := UIntToOH(addr_low).asBools 116863632028SHaoyuan Feng this.not_super := not_super.B 1169d0de7e4aSpeixiaokun 1170d0de7e4aSpeixiaokun 117163632028SHaoyuan Feng for (i <- 0 until tlbcontiguous) { 117263632028SHaoyuan Feng this.entry(i) := ptw_resp 117363632028SHaoyuan Feng } 117463632028SHaoyuan Feng } 117530104977Speixiaokun 117630104977Speixiaokun def genPPN(): UInt = { 117730104977Speixiaokun val idx = OHToUInt(pteidx) 117809280d15Speixiaokun val tag = Cat(entry(idx).tag, idx(sectortlbwidth - 1, 0)) 11796f508cb5Speixiaokun MuxLookup(entry(idx).level.get, 0.U)(Seq( 11803ea4388cSHaoyuan Feng 3.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen * 3 - sectortlbwidth), tag(vpnnLen * 3 - 1, 0)), 11813ea4388cSHaoyuan Feng 2.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), tag(vpnnLen * 2 - 1, 0)), 118209280d15Speixiaokun 1.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen - sectortlbwidth), tag(vpnnLen - 1, 0)), 11833ea4388cSHaoyuan Feng 0.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, 0), entry(idx).ppn_low)) 118430104977Speixiaokun ) 118530104977Speixiaokun } 118663632028SHaoyuan Feng} 11878744445eSMaxpicca-Li 1188d0de7e4aSpeixiaokunclass PtwRespS2(implicit p: Parameters) extends PtwBundle { 1189d0de7e4aSpeixiaokun val s2xlate = UInt(2.W) 1190d0de7e4aSpeixiaokun val s1 = new PtwSectorResp() 1191d0de7e4aSpeixiaokun val s2 = new HptwResp() 119286b5ba4aSpeixiaokun 1193e3da8badSTang Haojin def hasS2xlate: Bool = { 119486b5ba4aSpeixiaokun this.s2xlate =/= noS2xlate 119586b5ba4aSpeixiaokun } 119686b5ba4aSpeixiaokun 1197e3da8badSTang Haojin def isOnlyStage2: Bool = { 119886b5ba4aSpeixiaokun this.s2xlate === onlyStage2 119986b5ba4aSpeixiaokun } 120086b5ba4aSpeixiaokun 12019cb05b4dSXiaokun-Pei def getVpn(vpn: UInt): UInt = { 12023ea4388cSHaoyuan Feng val level = s1.entry.level.getOrElse(0.U) min s2.entry.level.getOrElse(0.U) 12039cb05b4dSXiaokun-Pei val s1tag = Cat(s1.entry.tag, OHToUInt(s1.pteidx)) 12049cb05b4dSXiaokun-Pei val s1tagFix = MuxCase(s1.entry.tag, Seq( 12053ea4388cSHaoyuan Feng (s1.entry.level.getOrElse(0.U) === 3.U && s2.entry.level.getOrElse(0.U) === 2.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), s2.entry.tag(vpnnLen * 3 - 1, vpnnLen * 2), 0.U((vpnnLen * 2 - sectortlbwidth).W)), 12063ea4388cSHaoyuan Feng (s1.entry.level.getOrElse(0.U) === 3.U && s2.entry.level.getOrElse(0.U) === 1.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), s2.entry.tag(vpnnLen * 3 - 1, vpnnLen), 0.U((vpnnLen - sectortlbwidth).W)), 12073ea4388cSHaoyuan Feng (s1.entry.level.getOrElse(0.U) === 3.U && s2.entry.level.getOrElse(0.U) === 0.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), s2.entry.tag(vpnnLen * 3 - 1, sectortlbwidth)), 12083ea4388cSHaoyuan Feng (s1.entry.level.getOrElse(0.U) === 2.U && s2.entry.level.getOrElse(0.U) === 1.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), s2.entry.tag(vpnnLen * 2 - 1, vpnnLen), 0.U((vpnnLen - sectortlbwidth).W)), 12093ea4388cSHaoyuan Feng (s1.entry.level.getOrElse(0.U) === 2.U && s2.entry.level.getOrElse(0.U) === 0.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), s2.entry.tag(vpnnLen * 2 - 1, sectortlbwidth)), 12103ea4388cSHaoyuan Feng (s1.entry.level.getOrElse(0.U) === 1.U && s2.entry.level.getOrElse(0.U) === 0.U) -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth), s2.entry.tag(vpnnLen - 1, sectortlbwidth)) 12119cb05b4dSXiaokun-Pei )) 12129cb05b4dSXiaokun-Pei val s1_vpn = MuxLookup(level, s1tag)(Seq( 12133ea4388cSHaoyuan Feng 3.U -> Cat(s1tagFix(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), vpn(vpnnLen * 3 - 1, 0)), 12143ea4388cSHaoyuan Feng 2.U -> Cat(s1tagFix(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), vpn(vpnnLen * 2 - 1, 0)), 12159cb05b4dSXiaokun-Pei 1.U -> Cat(s1tagFix(sectorvpnLen - 1, vpnnLen - sectortlbwidth), vpn(vpnnLen - 1, 0))) 12169cb05b4dSXiaokun-Pei ) 12179cb05b4dSXiaokun-Pei val s2_vpn = s2.entry.tag 12189cb05b4dSXiaokun-Pei Mux(s2xlate === onlyStage2, s2_vpn, Mux(s2xlate === allStage, s1_vpn, s1tag)) 1219c3d5cfb3Speixiaokun } 12204c4af37cSpeixiaokun 12214c4af37cSpeixiaokun def hit(vpn: UInt, asid: UInt, vasid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false): Bool = { 1222e3da8badSTang Haojin val noS2_hit = s1.hit(vpn, Mux(this.hasS2xlate, vasid, asid), vmid, allType, ignoreAsid, this.hasS2xlate) 122368750422Speixiaokun val onlyS2_hit = s2.hit(vpn, vmid) 122468750422Speixiaokun // allstage and onlys1 hit 122568750422Speixiaokun val s1vpn = Cat(s1.entry.tag, s1.addr_low) 12263ea4388cSHaoyuan Feng val level = s1.entry.level.getOrElse(0.U) min s2.entry.level.getOrElse(0.U) 12273ea4388cSHaoyuan Feng 12283ea4388cSHaoyuan Feng val tag_match = Wire(Vec(4, Bool())) // 512GB, 1GB, 2MB or 4KB, not parameterized here 12293ea4388cSHaoyuan Feng for (i <- 0 until 4) { 12303ea4388cSHaoyuan Feng tag_match(i) := vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) === s1vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) 12313ea4388cSHaoyuan Feng } 12323ea4388cSHaoyuan Feng 12333ea4388cSHaoyuan Feng val level_match = MuxLookup(level, false.B)(Seq( 12343ea4388cSHaoyuan Feng 3.U -> tag_match(3), 12353ea4388cSHaoyuan Feng 2.U -> (tag_match(3) && tag_match(2)), 12363ea4388cSHaoyuan Feng 1.U -> (tag_match(3) && tag_match(2) && tag_match(1)), 12373ea4388cSHaoyuan Feng 0.U -> (tag_match(3) && tag_match(2) && tag_match(1) && tag_match(0))) 12383ea4388cSHaoyuan Feng ) 12393ea4388cSHaoyuan Feng 12403ea4388cSHaoyuan Feng val vpn_hit = level_match 124168750422Speixiaokun val vmid_hit = Mux(this.s2xlate === allStage, s2.entry.vmid.getOrElse(0.U) === vmid, true.B) 124268750422Speixiaokun val vasid_hit = if (ignoreAsid) true.B else (s1.entry.asid === vasid) 124368750422Speixiaokun val all_onlyS1_hit = vpn_hit && vmid_hit && vasid_hit 124468750422Speixiaokun Mux(this.s2xlate === noS2xlate, noS2_hit, 124568750422Speixiaokun Mux(this.s2xlate === onlyStage2, onlyS2_hit, all_onlyS1_hit)) 12464c4af37cSpeixiaokun } 1247d0de7e4aSpeixiaokun} 1248d0de7e4aSpeixiaokun 1249d0de7e4aSpeixiaokunclass PtwRespS2withMemIdx(implicit p: Parameters) extends PtwRespS2 { 1250d0de7e4aSpeixiaokun val memidx = new MemBlockidxBundle() 1251a4f9c77fSpeixiaokun val getGpa = Bool() // this req is to get gpa when having guest page fault 1252d0de7e4aSpeixiaokun} 1253d0de7e4aSpeixiaokun 125492e3bfefSLemoverclass L2TLBIO(implicit p: Parameters) extends PtwBundle { 1255f57f7f2aSYangyu Chen val hartId = Input(UInt(hartIdLen.W)) 12566d5ddbceSLemover val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO)) 12576d5ddbceSLemover val sfence = Input(new SfenceBundle) 1258b6982e83SLemover val csr = new Bundle { 1259b6982e83SLemover val tlb = Input(new TlbCsrBundle) 1260b6982e83SLemover val distribute_csr = Flipped(new DistributedCSRIO) 1261b6982e83SLemover } 12626d5ddbceSLemover} 12636d5ddbceSLemover 1264b848eea5SLemoverclass L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle { 1265b848eea5SLemover val addr = UInt(PAddrBits.W) 1266b848eea5SLemover val id = UInt(bMemID.W) 126783d93d53Speixiaokun val hptw_bypassed = Bool() 1268b848eea5SLemover} 126945f497a4Shappy-lx 127045f497a4Shappy-lxclass L2TlbInnerBundle(implicit p: Parameters) extends PtwReq { 127145f497a4Shappy-lx val source = UInt(bSourceWidth.W) 127245f497a4Shappy-lx} 1273f1fe8698SLemover 12746967f5d5Speixiaokunclass L2TlbWithHptwIdBundle(implicit p: Parameters) extends PtwBundle { 12756967f5d5Speixiaokun val req_info = new L2TlbInnerBundle 1276325f0a4eSpeixiaokun val isHptwReq = Bool() 12777f6221c5Speixiaokun val isLLptw = Bool() 12786967f5d5Speixiaokun val hptwId = UInt(log2Up(l2tlbParams.llptwsize).W) 12796967f5d5Speixiaokun} 1280f1fe8698SLemover 1281f1fe8698SLemoverobject ValidHoldBypass{ 1282f1fe8698SLemover def apply(infire: Bool, outfire: Bool, flush: Bool = false.B) = { 1283f1fe8698SLemover val valid = RegInit(false.B) 1284f1fe8698SLemover when (infire) { valid := true.B } 1285f1fe8698SLemover when (outfire) { valid := false.B } // ATTENTION: order different with ValidHold 1286f1fe8698SLemover when (flush) { valid := false.B } // NOTE: the flush will flush in & out, is that ok? 1287f1fe8698SLemover valid || infire 1288f1fe8698SLemover } 1289f1fe8698SLemover} 12905afdf73cSHaoyuan Feng 12915afdf73cSHaoyuan Fengclass L1TlbDB(implicit p: Parameters) extends TlbBundle { 12925afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 12935afdf73cSHaoyuan Feng} 12945afdf73cSHaoyuan Feng 12955afdf73cSHaoyuan Fengclass PageCacheDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 12965afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 12975afdf73cSHaoyuan Feng val source = UInt(bSourceWidth.W) 12985afdf73cSHaoyuan Feng val bypassed = Bool() 12995afdf73cSHaoyuan Feng val is_first = Bool() 13005afdf73cSHaoyuan Feng val prefetched = Bool() 13015afdf73cSHaoyuan Feng val prefetch = Bool() 13025afdf73cSHaoyuan Feng val l2Hit = Bool() 13035afdf73cSHaoyuan Feng val l1Hit = Bool() 13045afdf73cSHaoyuan Feng val hit = Bool() 13055afdf73cSHaoyuan Feng} 13065afdf73cSHaoyuan Feng 13075afdf73cSHaoyuan Fengclass PTWDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 13085afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 13095afdf73cSHaoyuan Feng val source = UInt(bSourceWidth.W) 13105afdf73cSHaoyuan Feng} 13115afdf73cSHaoyuan Feng 13125afdf73cSHaoyuan Fengclass L2TlbPrefetchDB(implicit p: Parameters) extends TlbBundle { 13135afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 13145afdf73cSHaoyuan Feng} 13155afdf73cSHaoyuan Feng 13165afdf73cSHaoyuan Fengclass L2TlbMissQueueDB(implicit p: Parameters) extends TlbBundle { 13175afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 13185afdf73cSHaoyuan Feng} 1319