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 727acf8b76SXiaokun-Pei val v = Bool() // if stage1 pte is fake_pte, v is false 736d5ddbceSLemover // pagetable perm (software defined) 746d5ddbceSLemover val d = Bool() 756d5ddbceSLemover val a = Bool() 766d5ddbceSLemover val g = Bool() 776d5ddbceSLemover val u = Bool() 786d5ddbceSLemover val x = Bool() 796d5ddbceSLemover val w = Bool() 806d5ddbceSLemover val r = Bool() 816d5ddbceSLemover 82f9ac118cSHaoyuan Feng def apply(item: PtwSectorResp) = { 83b0fa7106SHaoyuan Feng val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 84b0fa7106SHaoyuan Feng this.pf := item.pf 85b0fa7106SHaoyuan Feng this.af := item.af 867acf8b76SXiaokun-Pei this.v := item.v 87b0fa7106SHaoyuan Feng this.d := ptePerm.d 88b0fa7106SHaoyuan Feng this.a := ptePerm.a 89b0fa7106SHaoyuan Feng this.g := ptePerm.g 90b0fa7106SHaoyuan Feng this.u := ptePerm.u 91b0fa7106SHaoyuan Feng this.x := ptePerm.x 92b0fa7106SHaoyuan Feng this.w := ptePerm.w 93b0fa7106SHaoyuan Feng this.r := ptePerm.r 94b0fa7106SHaoyuan Feng 95b0fa7106SHaoyuan Feng this 96b0fa7106SHaoyuan Feng } 97d0de7e4aSpeixiaokun 9887d0ba30Speixiaokun def applyS2(item: HptwResp) = { 99d0de7e4aSpeixiaokun val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 100d0de7e4aSpeixiaokun this.pf := item.gpf 101d0de7e4aSpeixiaokun this.af := item.gaf 1027acf8b76SXiaokun-Pei this.v := DontCare 103d0de7e4aSpeixiaokun this.d := ptePerm.d 104d0de7e4aSpeixiaokun this.a := ptePerm.a 105d0de7e4aSpeixiaokun this.g := ptePerm.g 106d0de7e4aSpeixiaokun this.u := ptePerm.u 107d0de7e4aSpeixiaokun this.x := ptePerm.x 108d0de7e4aSpeixiaokun this.w := ptePerm.w 109d0de7e4aSpeixiaokun this.r := ptePerm.r 110d0de7e4aSpeixiaokun 111d0de7e4aSpeixiaokun this 112d0de7e4aSpeixiaokun } 11387d0ba30Speixiaokun 114b0fa7106SHaoyuan Feng override def toPrintable: Printable = { 115f9ac118cSHaoyuan Feng p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " 116b0fa7106SHaoyuan Feng } 117b0fa7106SHaoyuan Feng} 118b0fa7106SHaoyuan Feng 119b0fa7106SHaoyuan Fengclass TlbSectorPermBundle(implicit p: Parameters) extends TlbBundle { 120b0fa7106SHaoyuan Feng val pf = Bool() // NOTE: if this is true, just raise pf 121b0fa7106SHaoyuan Feng val af = Bool() // NOTE: if this is true, just raise af 1227acf8b76SXiaokun-Pei val v = Bool() // if stage1 pte is fake_pte, v is false 123b0fa7106SHaoyuan Feng // pagetable perm (software defined) 124b0fa7106SHaoyuan Feng val d = Bool() 125b0fa7106SHaoyuan Feng val a = Bool() 126b0fa7106SHaoyuan Feng val g = Bool() 127b0fa7106SHaoyuan Feng val u = Bool() 128b0fa7106SHaoyuan Feng val x = Bool() 129b0fa7106SHaoyuan Feng val w = Bool() 130b0fa7106SHaoyuan Feng val r = Bool() 131b0fa7106SHaoyuan Feng 132f9ac118cSHaoyuan Feng def apply(item: PtwSectorResp) = { 133f1fe8698SLemover val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 134f1fe8698SLemover this.pf := item.pf 135f1fe8698SLemover this.af := item.af 1367acf8b76SXiaokun-Pei this.v := item.v 137f1fe8698SLemover this.d := ptePerm.d 138f1fe8698SLemover this.a := ptePerm.a 139f1fe8698SLemover this.g := ptePerm.g 140f1fe8698SLemover this.u := ptePerm.u 141f1fe8698SLemover this.x := ptePerm.x 142f1fe8698SLemover this.w := ptePerm.w 143f1fe8698SLemover this.r := ptePerm.r 144f1fe8698SLemover 145f1fe8698SLemover this 146f1fe8698SLemover } 1476d5ddbceSLemover override def toPrintable: Printable = { 148f9ac118cSHaoyuan Feng p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " 1496d5ddbceSLemover } 1506d5ddbceSLemover} 1516d5ddbceSLemover 1526d5ddbceSLemover// multi-read && single-write 1536d5ddbceSLemover// input is data, output is hot-code(not one-hot) 1546d5ddbceSLemoverclass CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule { 1556d5ddbceSLemover val io = IO(new Bundle { 1566d5ddbceSLemover val r = new Bundle { 1576d5ddbceSLemover val req = Input(Vec(readWidth, gen)) 1586d5ddbceSLemover val resp = Output(Vec(readWidth, Vec(set, Bool()))) 1596d5ddbceSLemover } 1606d5ddbceSLemover val w = Input(new Bundle { 1616d5ddbceSLemover val valid = Bool() 1626d5ddbceSLemover val bits = new Bundle { 1636d5ddbceSLemover val index = UInt(log2Up(set).W) 1646d5ddbceSLemover val data = gen 1656d5ddbceSLemover } 1666d5ddbceSLemover }) 1676d5ddbceSLemover }) 1686d5ddbceSLemover 1696d5ddbceSLemover val wordType = UInt(gen.getWidth.W) 1706d5ddbceSLemover val array = Reg(Vec(set, wordType)) 1716d5ddbceSLemover 1726d5ddbceSLemover io.r.resp.zipWithIndex.map{ case (a,i) => 1736d5ddbceSLemover a := array.map(io.r.req(i).asUInt === _) 1746d5ddbceSLemover } 1756d5ddbceSLemover 1766d5ddbceSLemover when (io.w.valid) { 17776e02f07SLingrui98 array(io.w.bits.index) := io.w.bits.data.asUInt 1786d5ddbceSLemover } 1796d5ddbceSLemover} 1806d5ddbceSLemover 181b0fa7106SHaoyuan Fengclass TlbSectorEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle { 1823ea4388cSHaoyuan Feng require(pageNormal && pageSuper) 183b0fa7106SHaoyuan Feng 1843ea4388cSHaoyuan Feng val tag = UInt(sectorvpnLen.W) 18545f497a4Shappy-lx val asid = UInt(asidLen.W) 1863ea4388cSHaoyuan Feng /* level, 11: 512GB size page(only for sv48) 1873ea4388cSHaoyuan Feng 10: 1GB size page 1883ea4388cSHaoyuan Feng 01: 2MB size page 1893ea4388cSHaoyuan Feng 00: 4KB size page 1903ea4388cSHaoyuan Feng future sv57 extension should change level width 1913ea4388cSHaoyuan Feng */ 1923ea4388cSHaoyuan Feng val level = Some(UInt(2.W)) 1933ea4388cSHaoyuan Feng val ppn = UInt(sectorppnLen.W) 194718a93f5SHaoyuan Feng val n = UInt(pteNLen.W) 195002c10a4SYanqin Li val pbmt = UInt(ptePbmtLen.W) 196002c10a4SYanqin Li val g_pbmt = UInt(ptePbmtLen.W) 197b0fa7106SHaoyuan Feng val perm = new TlbSectorPermBundle 19863632028SHaoyuan Feng val valididx = Vec(tlbcontiguous, Bool()) 199b0fa7106SHaoyuan Feng val pteidx = Vec(tlbcontiguous, Bool()) 20063632028SHaoyuan Feng val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W)) 201a0301c0dSLemover 202d0de7e4aSpeixiaokun val g_perm = new TlbPermBundle 203d0de7e4aSpeixiaokun val vmid = UInt(vmidLen.W) 204d61cd5eeSpeixiaokun val s2xlate = UInt(2.W) 205d0de7e4aSpeixiaokun 206a0301c0dSLemover 20756728e73SLemover /** level usage: 20856728e73SLemover * !PageSuper: page is only normal, level is None, match all the tag 20956728e73SLemover * !PageNormal: page is only super, level is a Bool(), match high 9*2 parts 21056728e73SLemover * bits0 0: need mid 9bits 21156728e73SLemover * 1: no need mid 9bits 21256728e73SLemover * PageSuper && PageNormal: page hold all the three type, 21356728e73SLemover * bits0 0: need low 9bits 21456728e73SLemover * bits1 0: need mid 9bits 21556728e73SLemover */ 21656728e73SLemover 21786b5ba4aSpeixiaokun 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 = { 21882978df9Speixiaokun val asid_hit = Mux(hasS2xlate && onlyS2, true.B, if (ignoreAsid) true.B else (this.asid === asid)) 21963632028SHaoyuan Feng val addr_low_hit = valididx(vpn(2, 0)) 22082978df9Speixiaokun val vmid_hit = Mux(hasS2xlate, this.vmid === vmid, true.B) 22186b5ba4aSpeixiaokun val isPageSuper = !(level.getOrElse(0.U) === 0.U) 222718a93f5SHaoyuan Feng val pteidx_hit = Mux(hasS2xlate && !isPageSuper && !onlyS1 && n === 0.U, pteidx(vpn(2, 0)), true.B) 2233ea4388cSHaoyuan Feng 22456728e73SLemover val tmp_level = level.get 2253ea4388cSHaoyuan Feng val tag_matchs = Wire(Vec(Level + 1, Bool())) 226718a93f5SHaoyuan Feng tag_matchs(0) := Mux(n === 0.U, tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth), tag(vpnnLen - sectortlbwidth - 1, pteNapotBits - sectortlbwidth) === vpn(vpnnLen - 1, pteNapotBits)) 22798451f8cSXiaokun-Pei for (i <- 1 until Level) { 2283ea4388cSHaoyuan Feng tag_matchs(i) := tag(vpnnLen * (i + 1) - sectortlbwidth - 1, vpnnLen * i - sectortlbwidth) === vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) 22956728e73SLemover } 23098451f8cSXiaokun-Pei tag_matchs(Level) := tag(sectorvpnLen - 1, vpnnLen * Level - sectortlbwidth) === vpn(vpnLen - 1, vpnnLen * Level) 2313ea4388cSHaoyuan Feng val level_matchs = Wire(Vec(Level + 1, Bool())) 2323ea4388cSHaoyuan Feng for (i <- 0 until Level) { 2333ea4388cSHaoyuan Feng level_matchs(i) := tag_matchs(i) || tmp_level >= (i + 1).U 2343ea4388cSHaoyuan Feng } 2353ea4388cSHaoyuan Feng level_matchs(Level) := tag_matchs(Level) 2363ea4388cSHaoyuan Feng 2373ea4388cSHaoyuan Feng asid_hit && level_matchs.asUInt.andR && addr_low_hit && vmid_hit && pteidx_hit 238a0301c0dSLemover } 239a0301c0dSLemover 240d6b32cb0SHaoyuan Feng def wbhit(data: PtwRespS2, asid: UInt, vmid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false, s2xlate: UInt): Bool = { 241933ec998Speixiaokun val s1vpn = data.s1.entry.tag 242aae99c05Speixiaokun val s2vpn = data.s2.entry.tag(vpnLen - 1, sectortlbwidth) 243933ec998Speixiaokun val wb_vpn = Mux(s2xlate === onlyStage2, s2vpn, s1vpn) 244933ec998Speixiaokun val vpn = Cat(wb_vpn, 0.U(sectortlbwidth.W)) 24563632028SHaoyuan Feng val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 24663632028SHaoyuan Feng val vpn_hit = Wire(Bool()) 24763632028SHaoyuan Feng val index_hit = Wire(Vec(tlbcontiguous, Bool())) 248933ec998Speixiaokun val wb_valididx = Wire(Vec(tlbcontiguous, Bool())) 249ab093818Speixiaokun val hasS2xlate = this.s2xlate =/= noS2xlate 250ab093818Speixiaokun val onlyS1 = this.s2xlate === onlyStage1 251ab093818Speixiaokun val onlyS2 = this.s2xlate === onlyStage2 252d6b32cb0SHaoyuan Feng val vmid_hit = Mux(hasS2xlate, this.vmid === vmid, true.B) 253ab093818Speixiaokun val pteidx_hit = MuxCase(true.B, Seq( 254ab093818Speixiaokun onlyS2 -> (VecInit(UIntToOH(data.s2.entry.tag(sectortlbwidth - 1, 0))).asUInt === pteidx.asUInt), 255ab093818Speixiaokun hasS2xlate -> (pteidx.asUInt === data.s1.pteidx.asUInt) 256ab093818Speixiaokun )) 257718a93f5SHaoyuan Feng wb_valididx := Mux(s2xlate === onlyStage2, 258718a93f5SHaoyuan Feng Mux(data.s2.entry.n.getOrElse(0.U) === 0.U, VecInit(UIntToOH(data.s2.entry.tag(sectortlbwidth - 1, 0)).asBools), VecInit(Fill(wb_valididx.getWidth, true.B).asBools)), 259718a93f5SHaoyuan Feng Mux(data.s1.entry.n.getOrElse(0.U) === 0.U, data.s1.valididx, VecInit(Fill(wb_valididx.getWidth, true.B).asBools))) 260933ec998Speixiaokun val s2xlate_hit = s2xlate === this.s2xlate 2613ea4388cSHaoyuan Feng 26263632028SHaoyuan Feng val tmp_level = level.get 2633ea4388cSHaoyuan Feng val tag_matchs = Wire(Vec(Level + 1, Bool())) 264718a93f5SHaoyuan Feng tag_matchs(0) := Mux(n === 0.U, tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth), tag(vpnnLen - sectortlbwidth - 1, pteNapotBits - sectortlbwidth) === vpn(vpnnLen - 1, pteNapotBits)) 26598451f8cSXiaokun-Pei for (i <- 1 until Level) { 2663ea4388cSHaoyuan Feng tag_matchs(i) := tag(vpnnLen * (i + 1) - sectortlbwidth - 1, vpnnLen * i - sectortlbwidth) === vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) 26763632028SHaoyuan Feng } 26898451f8cSXiaokun-Pei tag_matchs(Level) := tag(sectorvpnLen - 1, vpnnLen * Level - sectortlbwidth) === vpn(vpnLen - 1, vpnnLen * Level) 2693ea4388cSHaoyuan Feng val level_matchs = Wire(Vec(Level + 1, Bool())) 2703ea4388cSHaoyuan Feng for (i <- 0 until Level) { 2713ea4388cSHaoyuan Feng level_matchs(i) := tag_matchs(i) || tmp_level >= (i + 1).U 2723ea4388cSHaoyuan Feng } 2733ea4388cSHaoyuan Feng level_matchs(Level) := tag_matchs(Level) 274d6b32cb0SHaoyuan Feng vpn_hit := asid_hit && vmid_hit && level_matchs.asUInt.andR 2753ea4388cSHaoyuan Feng 27663632028SHaoyuan Feng for (i <- 0 until tlbcontiguous) { 277933ec998Speixiaokun index_hit(i) := wb_valididx(i) && valididx(i) 27863632028SHaoyuan Feng } 27963632028SHaoyuan Feng 28063632028SHaoyuan Feng // For example, tlb req to page cache with vpn 0x10 28163632028SHaoyuan Feng // At this time, 0x13 has not been paged, so page cache only resp 0x10 28263632028SHaoyuan Feng // When 0x13 refill to page cache, previous item will be flushed 28363632028SHaoyuan Feng // Now 0x10 and 0x13 are both valid in page cache 28463632028SHaoyuan Feng // However, when 0x13 refill to tlb, will trigger multi hit 28563632028SHaoyuan Feng // So will only trigger multi-hit when PopCount(data.valididx) = 1 286ab093818Speixiaokun vpn_hit && index_hit.reduce(_ || _) && PopCount(wb_valididx) === 1.U && s2xlate_hit && pteidx_hit 28763632028SHaoyuan Feng } 28863632028SHaoyuan Feng 289d0de7e4aSpeixiaokun def apply(item: PtwRespS2): TlbSectorEntry = { 290d0de7e4aSpeixiaokun this.asid := item.s1.entry.asid 2916f508cb5Speixiaokun val inner_level = MuxLookup(item.s2xlate, 2.U)(Seq( 2927e664aa3Speixiaokun onlyStage1 -> item.s1.entry.level.getOrElse(0.U), 2937e664aa3Speixiaokun onlyStage2 -> item.s2.entry.level.getOrElse(0.U), 2943ea4388cSHaoyuan Feng allStage -> (item.s1.entry.level.getOrElse(0.U) min item.s2.entry.level.getOrElse(0.U)), 2957e664aa3Speixiaokun noS2xlate -> item.s1.entry.level.getOrElse(0.U) 2967e664aa3Speixiaokun )) 2973ea4388cSHaoyuan Feng this.level.map(_ := inner_level) 298d0de7e4aSpeixiaokun this.perm.apply(item.s1) 299002c10a4SYanqin Li this.pbmt := item.s1.entry.pbmt 300d0de7e4aSpeixiaokun 3013ea4388cSHaoyuan Feng val s1tag = item.s1.entry.tag 30297929664SXiaokun-Pei val s2tag = item.s2.entry.tag(gvpnLen - 1, sectortlbwidth) 3035e0a22e7SXiaokun-Pei this.tag := Mux(item.s2xlate === onlyStage2, s2tag, s1tag) 30439e2cc5bSHaoyuan Feng val s2page_pageSuper = item.s2.entry.level.getOrElse(0.U) =/= 0.U || item.s2.entry.n.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. 3091f23fd0fSHaoyuan Feng val s1ppn = item.s1.entry.ppn(sectorppnLen - 1, 0) 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)), 314718a93f5SHaoyuan Feng 1.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen), item.s2.entry.tag(vpnnLen - 1, sectortlbwidth)), 315718a93f5SHaoyuan Feng 0.U -> Mux(item.s2.entry.n.getOrElse(0.U) === 0.U, item.s2.entry.ppn(ppnLen - 1, sectortlbwidth), Cat(item.s2.entry.ppn(ppnLen - 1, pteNapotBits), item.s2.entry.tag(pteNapotBits - 1, sectortlbwidth))) 3168c34f10bSpeixiaokun )) 3173ea4388cSHaoyuan Feng val s2ppn_tmp = MuxLookup(item.s2.entry.level.getOrElse(0.U), item.s2.entry.ppn(ppnLen - 1, 0))(Seq( 3183ea4388cSHaoyuan Feng 3.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 3), item.s2.entry.tag(vpnnLen * 3 - 1, 0)), 3193ea4388cSHaoyuan Feng 2.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen * 2), item.s2.entry.tag(vpnnLen * 2 - 1, 0)), 320718a93f5SHaoyuan Feng 1.U -> Cat(item.s2.entry.ppn(ppnLen - 1, vpnnLen), item.s2.entry.tag(vpnnLen - 1, 0)), 321718a93f5SHaoyuan Feng 0.U -> Mux(item.s2.entry.n.getOrElse(0.U) === 0.U, item.s2.entry.ppn(ppnLen - 1, 0), Cat(item.s2.entry.ppn(ppnLen - 1, pteNapotBits), item.s2.entry.tag(pteNapotBits - 1, 0))) 3228c34f10bSpeixiaokun )) 3238c34f10bSpeixiaokun val s2ppn_low = VecInit(Seq.fill(tlbcontiguous)(s2ppn_tmp(sectortlbwidth - 1, 0))) 32482978df9Speixiaokun this.ppn := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, s1ppn, s2ppn) 32582978df9Speixiaokun this.ppn_low := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, s1ppn_low, s2ppn_low) 326718a93f5SHaoyuan Feng // When all stage, the size of the TLB entry is the smaller one of two-stage translation result 327718a93f5SHaoyuan Feng // n is valid (represents a 64KB page) when: 328718a93f5SHaoyuan Feng // 1. s1 is napot(64KB) and s2 is superpage(greater than or equal to 2MB) 329718a93f5SHaoyuan Feng // 2. s2 is napot(64KB) and s1 is superpage(greater than or equal to 2MB) 330718a93f5SHaoyuan Feng // 3. s1 is napot(64KB) and s2 is also napot(64KB) 3314aeb0b1fSHaoyuan Feng val allStage_n = (item.s1.entry.n.getOrElse(0.U) =/= 0.U && item.s2.entry.level.getOrElse(0.U) =/= 0.U) || 332718a93f5SHaoyuan Feng (item.s2.entry.n.getOrElse(0.U) =/= 0.U && item.s1.entry.level.getOrElse(0.U) =/= 0.U) || 3335ffa384aSHaoyuan Feng (item.s1.entry.n.getOrElse(0.U) =/= 0.U && item.s2.entry.n.getOrElse(0.U) =/= 0.U) 3344aeb0b1fSHaoyuan Feng this.n := MuxLookup(item.s2xlate, 2.U)(Seq( 3354aeb0b1fSHaoyuan Feng onlyStage1 -> item.s1.entry.n.getOrElse(0.U), 3364aeb0b1fSHaoyuan Feng onlyStage2 -> item.s2.entry.n.getOrElse(0.U), 3374aeb0b1fSHaoyuan Feng allStage -> allStage_n, 3384aeb0b1fSHaoyuan Feng noS2xlate -> item.s1.entry.n.getOrElse(0.U) 3394aeb0b1fSHaoyuan Feng )) 3406cbf16a0SHaoyuan Feng this.vmid := Mux(item.s2xlate === onlyStage2, item.s2.entry.vmid.getOrElse(0.U), item.s1.entry.vmid.getOrElse(0.U)) 341002c10a4SYanqin Li this.g_pbmt := item.s2.entry.pbmt 34287d0ba30Speixiaokun this.g_perm.applyS2(item.s2) 34382978df9Speixiaokun this.s2xlate := item.s2xlate 344a0301c0dSLemover this 345a0301c0dSLemover } 346a0301c0dSLemover 34756728e73SLemover // 4KB is normal entry, 2MB/1GB is considered as super entry 34856728e73SLemover def is_normalentry(): Bool = { 34956728e73SLemover if (!pageSuper) { true.B } 35056728e73SLemover else if (!pageNormal) { false.B } 35156728e73SLemover else { level.get === 0.U } 35256728e73SLemover } 3535cf62c1aSLemover 354d0de7e4aSpeixiaokun 35556728e73SLemover def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = { 35656728e73SLemover val inner_level = level.getOrElse(0.U) 3573ea4388cSHaoyuan Feng val ppn_res = Cat(ppn(sectorppnLen - 1, vpnnLen * 3 - sectortlbwidth), 3583ea4388cSHaoyuan Feng Mux(inner_level >= "b11".U , vpn(vpnnLen * 3 - 1, vpnnLen * 2), ppn(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth)), 3593ea4388cSHaoyuan Feng Mux(inner_level >= "b10".U , vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth)), 360718a93f5SHaoyuan Feng Mux(inner_level >= "b01".U , vpn(vpnnLen - 1, 0), 361718a93f5SHaoyuan Feng // inner_level == "b00".U (4KB), need to check whether n is 0 362718a93f5SHaoyuan Feng Mux(n === 0.U, Cat(ppn(vpnnLen - sectortlbwidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0))), Cat(ppn(vpnnLen - sectortlbwidth - 1, pteNapotBits - sectortlbwidth), vpn(pteNapotBits - 1, 0))))) 36356728e73SLemover 3643ea4388cSHaoyuan Feng if (saveLevel) 3653ea4388cSHaoyuan Feng RegEnable(ppn_res, valid) 3663ea4388cSHaoyuan Feng else 3673ea4388cSHaoyuan Feng ppn_res 368a0301c0dSLemover } 369a0301c0dSLemover 370d61cd5eeSpeixiaokun def hasS2xlate(): Bool = { 371d61cd5eeSpeixiaokun this.s2xlate =/= noS2xlate 372d61cd5eeSpeixiaokun } 373d61cd5eeSpeixiaokun 374a0301c0dSLemover override def toPrintable: Printable = { 375a0301c0dSLemover val inner_level = level.getOrElse(2.U) 37645f497a4Shappy-lx p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}" 377a0301c0dSLemover } 378a0301c0dSLemover 379a0301c0dSLemover} 380a0301c0dSLemover 3816d5ddbceSLemoverobject TlbCmd { 3826d5ddbceSLemover def read = "b00".U 3836d5ddbceSLemover def write = "b01".U 3846d5ddbceSLemover def exec = "b10".U 3856d5ddbceSLemover 3866d5ddbceSLemover def atom_read = "b100".U // lr 3876d5ddbceSLemover def atom_write = "b101".U // sc / amo 3886d5ddbceSLemover 3896d5ddbceSLemover def apply() = UInt(3.W) 3906d5ddbceSLemover def isRead(a: UInt) = a(1,0)===read 3916d5ddbceSLemover def isWrite(a: UInt) = a(1,0)===write 3926d5ddbceSLemover def isExec(a: UInt) = a(1,0)===exec 3936d5ddbceSLemover 3946d5ddbceSLemover def isAtom(a: UInt) = a(2) 395a79fef67Swakafa def isAmo(a: UInt) = a===atom_write // NOTE: sc mixed 3966d5ddbceSLemover} 3976d5ddbceSLemover 398002c10a4SYanqin Li// Svpbmt extension 399002c10a4SYanqin Liobject Pbmt { 400002c10a4SYanqin Li def pma: UInt = "b00".U // None 401002c10a4SYanqin Li def nc: UInt = "b01".U // Non-cacheable, idempotent, weakly-ordered (RVWMO), main memory 402002c10a4SYanqin Li def io: UInt = "b10".U // Non-cacheable, non-idempotent, strongly-ordered (I/O ordering), I/O 403002c10a4SYanqin Li def rsvd: UInt = "b11".U // Reserved for future standard use 404002c10a4SYanqin Li def width: Int = 2 405002c10a4SYanqin Li 406002c10a4SYanqin Li def apply() = UInt(2.W) 407002c10a4SYanqin Li def isUncache(a: UInt) = a===nc || a===io 408c7353d05SYanqin Li def isPMA(a: UInt) = a===pma 409c7353d05SYanqin Li def isNC(a: UInt) = a===nc 410c7353d05SYanqin Li def isIO(a: UInt) = a===io 411002c10a4SYanqin Li} 412002c10a4SYanqin Li 41303efd994Shappy-lxclass TlbStorageIO(nSets: Int, nWays: Int, ports: Int, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 414a0301c0dSLemover val r = new Bundle { 415a0301c0dSLemover val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 416a0301c0dSLemover val vpn = Output(UInt(vpnLen.W)) 417875ae3b4SXiaokun-Pei val s2xlate = Output(UInt(2.W)) 418a0301c0dSLemover }))) 419a0301c0dSLemover val resp = Vec(ports, ValidIO(new Bundle{ 420a0301c0dSLemover val hit = Output(Bool()) 42103efd994Shappy-lx val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 422002c10a4SYanqin Li val pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W))) 423002c10a4SYanqin Li val g_pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W))) 424b0fa7106SHaoyuan Feng val perm = Vec(nDups, Output(new TlbSectorPermBundle())) 425d0de7e4aSpeixiaokun val g_perm = Vec(nDups, Output(new TlbPermBundle())) 426d0de7e4aSpeixiaokun val s2xlate = Vec(nDups, Output(UInt(2.W))) 427a0301c0dSLemover })) 428a0301c0dSLemover } 429a0301c0dSLemover val w = Flipped(ValidIO(new Bundle { 430a0301c0dSLemover val wayIdx = Output(UInt(log2Up(nWays).W)) 431d0de7e4aSpeixiaokun val data = Output(new PtwRespS2) 432a0301c0dSLemover })) 4333889e11eSLemover val access = Vec(ports, new ReplaceAccessBundle(nSets, nWays)) 434a0301c0dSLemover 43582978df9Speixiaokun def r_req_apply(valid: Bool, vpn: UInt, i: Int, s2xlate:UInt): Unit = { 436a0301c0dSLemover this.r.req(i).valid := valid 437a0301c0dSLemover this.r.req(i).bits.vpn := vpn 438d0de7e4aSpeixiaokun this.r.req(i).bits.s2xlate := s2xlate 439d0de7e4aSpeixiaokun 440a0301c0dSLemover } 441a0301c0dSLemover 442a0301c0dSLemover def r_resp_apply(i: Int) = { 443002c10a4SYanqin 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) 444a0301c0dSLemover } 445a0301c0dSLemover 446d0de7e4aSpeixiaokun def w_apply(valid: Bool, wayIdx: UInt, data: PtwRespS2): Unit = { 447a0301c0dSLemover this.w.valid := valid 448a0301c0dSLemover this.w.bits.wayIdx := wayIdx 449a0301c0dSLemover this.w.bits.data := data 450a0301c0dSLemover } 451a0301c0dSLemover 452a0301c0dSLemover} 453a0301c0dSLemover 45403efd994Shappy-lxclass TlbStorageWrapperIO(ports: Int, q: TLBParameters, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 455f1fe8698SLemover val r = new Bundle { 456f1fe8698SLemover val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 457f1fe8698SLemover val vpn = Output(UInt(vpnLen.W)) 458d0de7e4aSpeixiaokun val s2xlate = Output(UInt(2.W)) 459f1fe8698SLemover }))) 460f1fe8698SLemover val resp = Vec(ports, ValidIO(new Bundle{ 461f1fe8698SLemover val hit = Output(Bool()) 46203efd994Shappy-lx val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 463002c10a4SYanqin Li val pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W))) 464002c10a4SYanqin Li val g_pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W))) 46503efd994Shappy-lx val perm = Vec(nDups, Output(new TlbPermBundle())) 466d0de7e4aSpeixiaokun val g_perm = Vec(nDups, Output(new TlbPermBundle())) 467d0de7e4aSpeixiaokun val s2xlate = Vec(nDups, Output(UInt(2.W))) 468f1fe8698SLemover })) 469f1fe8698SLemover } 470f1fe8698SLemover val w = Flipped(ValidIO(new Bundle { 471d0de7e4aSpeixiaokun val data = Output(new PtwRespS2) 472f1fe8698SLemover })) 473f1fe8698SLemover val replace = if (q.outReplace) Flipped(new TlbReplaceIO(ports, q)) else null 474f1fe8698SLemover 475d0de7e4aSpeixiaokun def r_req_apply(valid: Bool, vpn: UInt, i: Int, s2xlate: UInt): Unit = { 476f1fe8698SLemover this.r.req(i).valid := valid 477f1fe8698SLemover this.r.req(i).bits.vpn := vpn 478d0de7e4aSpeixiaokun this.r.req(i).bits.s2xlate := s2xlate 479f1fe8698SLemover } 480f1fe8698SLemover 481f1fe8698SLemover def r_resp_apply(i: Int) = { 482002c10a4SYanqin 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) 483f1fe8698SLemover } 484f1fe8698SLemover 485d0de7e4aSpeixiaokun def w_apply(valid: Bool, data: PtwRespS2): Unit = { 486f1fe8698SLemover this.w.valid := valid 487f1fe8698SLemover this.w.bits.data := data 488f1fe8698SLemover } 489f1fe8698SLemover} 490f1fe8698SLemover 4913889e11eSLemoverclass ReplaceAccessBundle(nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 4923889e11eSLemover val sets = Output(UInt(log2Up(nSets).W)) 4933889e11eSLemover val touch_ways = ValidIO(Output(UInt(log2Up(nWays).W))) 4943889e11eSLemover} 4953889e11eSLemover 496a0301c0dSLemoverclass ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 4973889e11eSLemover val access = Vec(Width, Flipped(new ReplaceAccessBundle(nSets, nWays))) 498a0301c0dSLemover 499a0301c0dSLemover val refillIdx = Output(UInt(log2Up(nWays).W)) 500a0301c0dSLemover val chosen_set = Flipped(Output(UInt(log2Up(nSets).W))) 501a0301c0dSLemover 502a0301c0dSLemover def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = { 50353b8f1a7SLemover for ((ac_rep, ac_tlb) <- access.zip(in.map(a => a.access.map(b => b)).flatten)) { 50453b8f1a7SLemover ac_rep := ac_tlb 505a0301c0dSLemover } 50653b8f1a7SLemover this.chosen_set := get_set_idx(vpn, nSets) 50753b8f1a7SLemover in.map(a => a.refillIdx := this.refillIdx) 508a0301c0dSLemover } 509a0301c0dSLemover} 510a0301c0dSLemover 511a0301c0dSLemoverclass TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends 512a0301c0dSLemover TlbBundle { 513f9ac118cSHaoyuan Feng val page = new ReplaceIO(Width, q.NSets, q.NWays) 514a0301c0dSLemover 515a0301c0dSLemover def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = { 516f9ac118cSHaoyuan Feng this.page.apply_sep(in.map(_.page), vpn) 517a0301c0dSLemover } 518a0301c0dSLemover 519a0301c0dSLemover} 520a0301c0dSLemover 5218744445eSMaxpicca-Liclass MemBlockidxBundle(implicit p: Parameters) extends TlbBundle { 5228744445eSMaxpicca-Li val is_ld = Bool() 5238744445eSMaxpicca-Li val is_st = Bool() 524be867ebcSAnzooooo val idx = UInt(log2Ceil(VirtualLoadQueueMaxStoreQueueSize).W) 5258744445eSMaxpicca-Li} 5268744445eSMaxpicca-Li 5276d5ddbceSLemoverclass TlbReq(implicit p: Parameters) extends TlbBundle { 528ca2f90a6SLemover val vaddr = Output(UInt(VAddrBits.W)) 529db6cfb5aSHaoyuan Feng val fullva = Output(UInt(XLEN.W)) 530db6cfb5aSHaoyuan Feng val checkfullva = Output(Bool()) 531ca2f90a6SLemover val cmd = Output(TlbCmd()) 532d0de7e4aSpeixiaokun val hyperinst = Output(Bool()) 533d0de7e4aSpeixiaokun val hlvx = Output(Bool()) 53426af847eSgood-circle val size = Output(UInt(log2Ceil(log2Ceil(VLEN/8)+1).W)) 535f1fe8698SLemover val kill = Output(Bool()) // Use for blocked tlb that need sync with other module like icache 5368744445eSMaxpicca-Li val memidx = Output(new MemBlockidxBundle) 5378a4dab4dSHaoyuan Feng val isPrefetch = Output(Bool()) 538b52348aeSWilliam Wang // do not translate, but still do pmp/pma check 539b52348aeSWilliam Wang val no_translate = Output(Bool()) 540149a2326Sweiding liu val pmp_addr = Output(UInt(PAddrBits.W)) // load s1 send prefetch paddr 5416d5ddbceSLemover val debug = new Bundle { 542ca2f90a6SLemover val pc = Output(UInt(XLEN.W)) 543f1fe8698SLemover val robIdx = Output(new RobPtr) 544ca2f90a6SLemover val isFirstIssue = Output(Bool()) 5456d5ddbceSLemover } 5466d5ddbceSLemover 547f1fe8698SLemover // Maybe Block req needs a kill: for itlb, itlb and icache may not sync, itlb should wait icache to go ahead 5486d5ddbceSLemover override def toPrintable: Printable = { 549f1fe8698SLemover p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} kill:${kill} pc:0x${Hexadecimal(debug.pc)} robIdx:${debug.robIdx}" 5506d5ddbceSLemover } 5516d5ddbceSLemover} 5526d5ddbceSLemover 553b6982e83SLemoverclass TlbExceptionBundle(implicit p: Parameters) extends TlbBundle { 554b6982e83SLemover val ld = Output(Bool()) 555b6982e83SLemover val st = Output(Bool()) 556b6982e83SLemover val instr = Output(Bool()) 557b6982e83SLemover} 558b6982e83SLemover 55903efd994Shappy-lxclass TlbResp(nDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 56003efd994Shappy-lx val paddr = Vec(nDups, Output(UInt(PAddrBits.W))) 561db6cfb5aSHaoyuan Feng val gpaddr = Vec(nDups, Output(UInt(XLEN.W))) 562189833a1SHaoyuan Feng val fullva = Output(UInt(XLEN.W)) // For pointer masking 563002c10a4SYanqin Li val pbmt = Vec(nDups, Output(UInt(ptePbmtLen.W))) 564ca2f90a6SLemover val miss = Output(Bool()) 56508b0bc30Shappy-lx val fastMiss = Output(Bool()) 566ad415ae0SXiaokun-Pei val isForVSnonLeafPTE = Output(Bool()) 56703efd994Shappy-lx val excp = Vec(nDups, new Bundle { 56846e9ee74SHaoyuan Feng val vaNeedExt = Output(Bool()) 56946e9ee74SHaoyuan Feng val isHyper = Output(Bool()) 570d0de7e4aSpeixiaokun val gpf = new TlbExceptionBundle() 571b6982e83SLemover val pf = new TlbExceptionBundle() 572b6982e83SLemover val af = new TlbExceptionBundle() 57303efd994Shappy-lx }) 574ca2f90a6SLemover val ptwBack = Output(Bool()) // when ptw back, wake up replay rs's state 5758744445eSMaxpicca-Li val memidx = Output(new MemBlockidxBundle) 5766d5ddbceSLemover 5778744445eSMaxpicca-Li val debug = new Bundle { 5788744445eSMaxpicca-Li val robIdx = Output(new RobPtr) 5798744445eSMaxpicca-Li val isFirstIssue = Output(Bool()) 5808744445eSMaxpicca-Li } 5816d5ddbceSLemover override def toPrintable: Printable = { 58203efd994Shappy-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}" 5836d5ddbceSLemover } 5846d5ddbceSLemover} 5856d5ddbceSLemover 58603efd994Shappy-lxclass TlbRequestIO(nRespDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 5876d5ddbceSLemover val req = DecoupledIO(new TlbReq) 588c3b763d0SYinan Xu val req_kill = Output(Bool()) 58903efd994Shappy-lx val resp = Flipped(DecoupledIO(new TlbResp(nRespDups))) 5906d5ddbceSLemover} 5916d5ddbceSLemover 5926d5ddbceSLemoverclass TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 5936d5ddbceSLemover val req = Vec(Width, DecoupledIO(new PtwReq)) 594d0de7e4aSpeixiaokun val resp = Flipped(DecoupledIO(new PtwRespS2)) 5956d5ddbceSLemover 5966d5ddbceSLemover 5976d5ddbceSLemover override def toPrintable: Printable = { 5986d5ddbceSLemover p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 5996d5ddbceSLemover } 6006d5ddbceSLemover} 6016d5ddbceSLemover 6028744445eSMaxpicca-Liclass TlbPtwIOwithMemIdx(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 6038744445eSMaxpicca-Li val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx)) 604d0de7e4aSpeixiaokun val resp = Flipped(DecoupledIO(new PtwRespS2withMemIdx())) 6058744445eSMaxpicca-Li 6068744445eSMaxpicca-Li 6078744445eSMaxpicca-Li override def toPrintable: Printable = { 6088744445eSMaxpicca-Li p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 6098744445eSMaxpicca-Li } 6108744445eSMaxpicca-Li} 6118744445eSMaxpicca-Li 612185e6164SHaoyuan Fengclass TlbHintReq(implicit p: Parameters) extends TlbBundle { 613185e6164SHaoyuan Feng val id = Output(UInt(log2Up(loadfiltersize).W)) 614185e6164SHaoyuan Feng val full = Output(Bool()) 615185e6164SHaoyuan Feng} 616185e6164SHaoyuan Feng 617185e6164SHaoyuan Fengclass TLBHintResp(implicit p: Parameters) extends TlbBundle { 618185e6164SHaoyuan Feng val id = Output(UInt(log2Up(loadfiltersize).W)) 619185e6164SHaoyuan Feng // When there are multiple matching entries for PTW resp in filter 620185e6164SHaoyuan Feng // e.g. vaddr 0, 0x80000000. vaddr 1, 0x80010000 621185e6164SHaoyuan Feng // these two vaddrs are not in a same 4K Page, so will send to ptw twice 622185e6164SHaoyuan Feng // However, when ptw resp, if they are in a 1G or 2M huge page 623185e6164SHaoyuan Feng // The two entries will both hit, and both need to replay 624185e6164SHaoyuan Feng val replay_all = Output(Bool()) 625185e6164SHaoyuan Feng} 626185e6164SHaoyuan Feng 627185e6164SHaoyuan Fengclass TlbHintIO(implicit p: Parameters) extends TlbBundle { 62871489510SXuan Hu val req = Vec(backendParams.LdExuCnt, new TlbHintReq) 629185e6164SHaoyuan Feng val resp = ValidIO(new TLBHintResp) 630185e6164SHaoyuan Feng} 631185e6164SHaoyuan Feng 63245f497a4Shappy-lxclass MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle { 633b052b972SLemover val sfence = Input(new SfenceBundle) 634b052b972SLemover val csr = Input(new TlbCsrBundle) 635f1fe8698SLemover 636f1fe8698SLemover def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 637f1fe8698SLemover this.sfence <> sfence 638f1fe8698SLemover this.csr <> csr 639f1fe8698SLemover } 640f1fe8698SLemover 641f1fe8698SLemover // overwrite satp. write satp will cause flushpipe but csr.priv won't 642f1fe8698SLemover // satp will be dealyed several cycles from writing, but csr.priv won't 643f1fe8698SLemover // so inside mmu, these two signals should be divided 644f1fe8698SLemover def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle, satp: TlbSatpBundle) = { 645f1fe8698SLemover this.sfence <> sfence 646f1fe8698SLemover this.csr <> csr 647f1fe8698SLemover this.csr.satp := satp 648f1fe8698SLemover } 649a0301c0dSLemover} 6506d5ddbceSLemover 6518744445eSMaxpicca-Liclass TlbRefilltoMemIO()(implicit p: Parameters) extends TlbBundle { 6528744445eSMaxpicca-Li val valid = Bool() 6538744445eSMaxpicca-Li val memidx = new MemBlockidxBundle 6548744445eSMaxpicca-Li} 6558744445eSMaxpicca-Li 65603efd994Shappy-lxclass TlbIO(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends 65745f497a4Shappy-lx MMUIOBaseBundle { 658f57f7f2aSYangyu Chen val hartId = Input(UInt(hartIdLen.W)) 65903efd994Shappy-lx val requestor = Vec(Width, Flipped(new TlbRequestIO(nRespDups))) 660f1fe8698SLemover val flushPipe = Vec(Width, Input(Bool())) 661a4f9c77fSpeixiaokun val redirect = Flipped(ValidIO(new Redirect)) // flush the signal need_gpa in tlb 6628744445eSMaxpicca-Li val ptw = new TlbPtwIOwithMemIdx(Width) 6638744445eSMaxpicca-Li val refill_to_mem = Output(new TlbRefilltoMemIO()) 664a0301c0dSLemover val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null 66526af847eSgood-circle val pmp = Vec(Width, ValidIO(new PMPReqBundle(q.lgMaxSize))) 666185e6164SHaoyuan Feng val tlbreplay = Vec(Width, Output(Bool())) 667a0301c0dSLemover} 668a0301c0dSLemover 669f1fe8698SLemoverclass VectorTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle { 6708744445eSMaxpicca-Li val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx())) 671a0301c0dSLemover val resp = Flipped(DecoupledIO(new Bundle { 672d0de7e4aSpeixiaokun val data = new PtwRespS2withMemIdx 673a0301c0dSLemover val vector = Output(Vec(Width, Bool())) 674a4f9c77fSpeixiaokun val getGpa = Output(Vec(Width, Bool())) 675a0301c0dSLemover })) 676a0301c0dSLemover 6778744445eSMaxpicca-Li def connect(normal: TlbPtwIOwithMemIdx): Unit = { 678f1fe8698SLemover req <> normal.req 679f1fe8698SLemover resp.ready := normal.resp.ready 680f1fe8698SLemover normal.resp.bits := resp.bits.data 681f1fe8698SLemover normal.resp.valid := resp.valid 682a0301c0dSLemover } 6836d5ddbceSLemover} 6846d5ddbceSLemover 68592e3bfefSLemover/**************************** L2TLB *************************************/ 6866d5ddbceSLemoverabstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst 68792e3bfefSLemoverabstract class PtwModule(outer: L2TLB) extends LazyModuleImp(outer) 6886d5ddbceSLemover with HasXSParameter with HasPtwConst 6896d5ddbceSLemover 6906d5ddbceSLemoverclass PteBundle(implicit p: Parameters) extends PtwBundle{ 691002c10a4SYanqin Li val n = UInt(pteNLen.W) 692002c10a4SYanqin Li val pbmt = UInt(ptePbmtLen.W) 6936d5ddbceSLemover val reserved = UInt(pteResLen.W) 6940d94d540SHaoyuan Feng val ppn_high = UInt(ppnHignLen.W) 6956d5ddbceSLemover val ppn = UInt(ppnLen.W) 696002c10a4SYanqin Li val rsw = UInt(pteRswLen.W) 6976d5ddbceSLemover val perm = new Bundle { 6986d5ddbceSLemover val d = Bool() 6996d5ddbceSLemover val a = Bool() 7006d5ddbceSLemover val g = Bool() 7016d5ddbceSLemover val u = Bool() 7026d5ddbceSLemover val x = Bool() 7036d5ddbceSLemover val w = Bool() 7046d5ddbceSLemover val r = Bool() 7056d5ddbceSLemover val v = Bool() 7066d5ddbceSLemover } 7076d5ddbceSLemover 7086d5ddbceSLemover def unaligned(level: UInt) = { 7093ea4388cSHaoyuan Feng isLeaf() && 7103ea4388cSHaoyuan Feng !(level === 0.U || 7116d5ddbceSLemover level === 1.U && ppn(vpnnLen-1, 0) === 0.U || 7123ea4388cSHaoyuan Feng level === 2.U && ppn(vpnnLen*2-1, 0) === 0.U || 7133ea4388cSHaoyuan Feng level === 3.U && ppn(vpnnLen*3-1, 0) === 0.U) 7146d5ddbceSLemover } 7156d5ddbceSLemover 71697929664SXiaokun-Pei def isLeaf() = { 71797929664SXiaokun-Pei (perm.r || perm.x || perm.w) && perm.v 71897929664SXiaokun-Pei } 71997929664SXiaokun-Pei 720135df6a7SXiaokun-Pei def isNext() = { 721135df6a7SXiaokun-Pei !(perm.r || perm.x || perm.w) && perm.v 722135df6a7SXiaokun-Pei } 723135df6a7SXiaokun-Pei 724dd286b6aSYanqin Li def isPf(level: UInt, pbmte: Bool) = { 725135df6a7SXiaokun-Pei val pf = WireInit(false.B) 7265ec7c921SXiaokun-Pei when (reserved =/= 0.U){ 7275ec7c921SXiaokun-Pei pf := true.B 728dd286b6aSYanqin Li }.elsewhen(pbmt === 3.U || (!pbmte && pbmt =/= 0.U)){ 7295ec7c921SXiaokun-Pei pf := true.B 7305ec7c921SXiaokun-Pei }.elsewhen (isNext()) { 7315ec7c921SXiaokun-Pei pf := (perm.u || perm.a || perm.d || n =/= 0.U || pbmt =/= 0.U) 732135df6a7SXiaokun-Pei }.elsewhen (!perm.v || (!perm.r && perm.w)) { 733135df6a7SXiaokun-Pei pf := true.B 734718a93f5SHaoyuan Feng // 1. only support 64KB napot page now (ppn(3, 0) === 4'b1000) 735718a93f5SHaoyuan Feng // 2. n should always be 0 when superpage (when level =/= 0.U) 736718a93f5SHaoyuan Feng }.elsewhen (n =/= 0.U && (ppn(3, 0) =/= 8.U || level =/= 0.U)) { 737dd286b6aSYanqin Li pf := true.B 738135df6a7SXiaokun-Pei }.otherwise { 73957ff69b1SXiaokun-Pei pf := unaligned(level) 740135df6a7SXiaokun-Pei } 741135df6a7SXiaokun-Pei pf 742135df6a7SXiaokun-Pei } 743135df6a7SXiaokun-Pei 7447543e8e3SXiaokun-Pei // G-stage which for supporting VS-stage is LOAD type, only need to check A bit 7457543e8e3SXiaokun-Pei // The check of D bit is in L1TLB 746dd286b6aSYanqin Li def isGpf(level: UInt, pbmte: Bool) = { 747135df6a7SXiaokun-Pei val gpf = WireInit(false.B) 748dd286b6aSYanqin Li when (reserved =/= 0.U){ 749dd286b6aSYanqin Li gpf := true.B 750dd286b6aSYanqin Li }.elsewhen(pbmt === 3.U || (!pbmte && pbmt =/= 0.U)){ 751dd286b6aSYanqin Li gpf := true.B 752dd286b6aSYanqin Li }.elsewhen (isNext()) { 753dd286b6aSYanqin Li gpf := (perm.u || perm.a || perm.d || n =/= 0.U || pbmt =/= 0.U) 754135df6a7SXiaokun-Pei }.elsewhen (!perm.v || (!perm.r && perm.w)) { 755135df6a7SXiaokun-Pei gpf := true.B 756135df6a7SXiaokun-Pei }.elsewhen (!perm.u) { 757135df6a7SXiaokun-Pei gpf := true.B 758dd286b6aSYanqin Li }.elsewhen (n =/= 0.U && ppn(3, 0) =/= 8.U) { 759dd286b6aSYanqin Li gpf := true.B 7607543e8e3SXiaokun-Pei }.elsewhen (unaligned(level)) { 7617543e8e3SXiaokun-Pei gpf := true.B 7627543e8e3SXiaokun-Pei }.elsewhen (!perm.a) { 7637543e8e3SXiaokun-Pei gpf := true.B 764135df6a7SXiaokun-Pei } 765135df6a7SXiaokun-Pei gpf 7666d5ddbceSLemover } 7676d5ddbceSLemover 7684e811ad7SHaoyuan Feng // ppn of Xiangshan is 48 - 12 bits but ppn of sv48 is 44 bits 7690d94d540SHaoyuan Feng // access fault will be raised when ppn >> ppnLen is not zero 7704e811ad7SHaoyuan Feng def isAf(): Bool = { 7710709d54aSXiaokun-Pei !(ppn_high === 0.U) && perm.v 7720d94d540SHaoyuan Feng } 7730d94d540SHaoyuan Feng 7740b1b8ed1SXiaokun-Pei def isStage1Gpf(mode: UInt) = { 7750b1b8ed1SXiaokun-Pei val sv39_high = Cat(ppn_high, ppn) >> (GPAddrBitsSv39x4 - offLen) 7760b1b8ed1SXiaokun-Pei val sv48_high = Cat(ppn_high, ppn) >> (GPAddrBitsSv48x4 - offLen) 7770709d54aSXiaokun-Pei !(Mux(mode === Sv39, sv39_high, Mux(mode === Sv48, sv48_high, 0.U)) === 0.U) && perm.v 7784c0e0181SXiaokun-Pei } 7794c0e0181SXiaokun-Pei 780718a93f5SHaoyuan Feng def isNapot(level: UInt): Bool = { 781718a93f5SHaoyuan Feng isLeaf() && (n === true.B) 782718a93f5SHaoyuan Feng } 783718a93f5SHaoyuan Feng 7846d5ddbceSLemover def getPerm() = { 7856d5ddbceSLemover val pm = Wire(new PtePermBundle) 7866d5ddbceSLemover pm.d := perm.d 7876d5ddbceSLemover pm.a := perm.a 7886d5ddbceSLemover pm.g := perm.g 7896d5ddbceSLemover pm.u := perm.u 7906d5ddbceSLemover pm.x := perm.x 7916d5ddbceSLemover pm.w := perm.w 7926d5ddbceSLemover pm.r := perm.r 7936d5ddbceSLemover pm 7946d5ddbceSLemover } 7954c0e0181SXiaokun-Pei def getPPN() = { 7964c0e0181SXiaokun-Pei Cat(ppn_high, ppn) 7974c0e0181SXiaokun-Pei } 798e0c1f271SHaoyuan Feng 799e0c1f271SHaoyuan Feng def canRefill(levelUInt: UInt, s2xlate: UInt, pbmte: Bool, mode: UInt) = { 800e0c1f271SHaoyuan Feng val canRefill = WireInit(false.B) 801e0c1f271SHaoyuan Feng switch (s2xlate) { 802e0c1f271SHaoyuan Feng is (allStage) { 803e0c1f271SHaoyuan Feng canRefill := !isStage1Gpf(mode) && !isPf(levelUInt, pbmte) 804e0c1f271SHaoyuan Feng } 805e0c1f271SHaoyuan Feng is (onlyStage1) { 806e0c1f271SHaoyuan Feng canRefill := !isAf() && !isPf(levelUInt, pbmte) 807e0c1f271SHaoyuan Feng } 808e0c1f271SHaoyuan Feng is (onlyStage2) { 809e0c1f271SHaoyuan Feng canRefill := !isAf() && !isGpf(levelUInt, pbmte) 810e0c1f271SHaoyuan Feng } 811e0c1f271SHaoyuan Feng is (noS2xlate) { 812e0c1f271SHaoyuan Feng canRefill := !isAf() && !isPf(levelUInt, pbmte) 813e0c1f271SHaoyuan Feng } 814e0c1f271SHaoyuan Feng } 815e0c1f271SHaoyuan Feng canRefill 816e0c1f271SHaoyuan Feng } 817e0c1f271SHaoyuan Feng 818e0c1f271SHaoyuan Feng def onlyPf(levelUInt: UInt, s2xlate: UInt, pbmte: Bool) = { 819e0c1f271SHaoyuan Feng s2xlate === noS2xlate && isPf(levelUInt, pbmte) && !isAf() 820e0c1f271SHaoyuan Feng } 821e0c1f271SHaoyuan Feng 8226d5ddbceSLemover override def toPrintable: Printable = { 8236d5ddbceSLemover p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}" 8246d5ddbceSLemover } 8256d5ddbceSLemover} 8266d5ddbceSLemover 827718a93f5SHaoyuan Fengclass PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false, hasNapot: Boolean = false)(implicit p: Parameters) extends PtwBundle { 8286d5ddbceSLemover val tag = UInt(tagLen.W) 82945f497a4Shappy-lx val asid = UInt(asidLen.W) 830d0de7e4aSpeixiaokun val vmid = if (HasHExtension) Some(UInt(vmidLen.W)) else None 831718a93f5SHaoyuan Feng val n = if (hasNapot) Some(UInt(pteNLen.W)) else None 832002c10a4SYanqin Li val pbmt = UInt(ptePbmtLen.W) 83397929664SXiaokun-Pei val ppn = UInt(gvpnLen.W) 8346d5ddbceSLemover val perm = if (hasPerm) Some(new PtePermBundle) else None 8353ea4388cSHaoyuan Feng val level = if (hasLevel) Some(UInt(log2Up(Level + 1).W)) else None 836bc063562SLemover val prefetch = Bool() 8378d8ac704SLemover val v = Bool() 8386d5ddbceSLemover 83956728e73SLemover def is_normalentry(): Bool = { 84056728e73SLemover if (!hasLevel) true.B 84156728e73SLemover else level.get === 2.U 84256728e73SLemover } 84356728e73SLemover 844f1fe8698SLemover def genPPN(vpn: UInt): UInt = { 8453ea4388cSHaoyuan Feng if (!hasLevel) { 8463ea4388cSHaoyuan Feng ppn 8473ea4388cSHaoyuan Feng } else { 8483ea4388cSHaoyuan Feng MuxLookup(level.get, 0.U)(Seq( 8493ea4388cSHaoyuan Feng 3.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*3), vpn(vpnnLen*3-1, 0)), 8503ea4388cSHaoyuan Feng 2.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)), 851f1fe8698SLemover 1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)), 8523ea4388cSHaoyuan Feng 0.U -> ppn) 853f1fe8698SLemover ) 854f1fe8698SLemover } 8553ea4388cSHaoyuan Feng } 856f1fe8698SLemover 857d0de7e4aSpeixiaokun //s2xlate control whether compare vmid or not 858b188e334Speixiaokun def hit(vpn: UInt, asid: UInt, vasid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false, s2xlate: Bool) = { 8596d5ddbceSLemover require(vpn.getWidth == vpnLen) 860cccfc98dSLemover// require(this.asid.getWidth <= asid.getWidth) 861b188e334Speixiaokun val asid_value = Mux(s2xlate, vasid, asid) 862b188e334Speixiaokun val asid_hit = if (ignoreAsid) true.B else (this.asid === asid_value) 863d61cd5eeSpeixiaokun val vmid_hit = Mux(s2xlate, (this.vmid.getOrElse(0.U) === vmid), true.B) 8646d5ddbceSLemover if (allType) { 8656d5ddbceSLemover require(hasLevel) 866718a93f5SHaoyuan Feng val tag_match = Wire(Vec(4, Bool())) // 512GB, 1GB, 2MB or 4KB(including SVNapot), not parameterized here 867718a93f5SHaoyuan Feng when (n.getOrElse(0.U) =/= 0.U) { 868718a93f5SHaoyuan Feng tag_match(0) := tag(vpnnLen - 1, pteNapotBits) === vpn(vpnnLen - 1, pteNapotBits) 869718a93f5SHaoyuan Feng } .otherwise { 870718a93f5SHaoyuan Feng tag_match(0) := tag(vpnnLen - 1, 0) === vpn(vpnnLen - 1, 0) 871718a93f5SHaoyuan Feng } 872718a93f5SHaoyuan Feng for (i <- 1 until 3) { 8733ea4388cSHaoyuan Feng tag_match(i) := tag(vpnnLen * (i + 1) - 1, vpnnLen * i) === vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) 8743ea4388cSHaoyuan Feng } 8753ea4388cSHaoyuan Feng tag_match(3) := tag(tagLen - 1, vpnnLen * 3) === vpn(tagLen - 1, vpnnLen * 3) 87645f497a4Shappy-lx 8773ea4388cSHaoyuan Feng val level_match = MuxLookup(level.getOrElse(0.U), false.B)(Seq( 8783ea4388cSHaoyuan Feng 3.U -> tag_match(3), 8793ea4388cSHaoyuan Feng 2.U -> (tag_match(3) && tag_match(2)), 8803ea4388cSHaoyuan Feng 1.U -> (tag_match(3) && tag_match(2) && tag_match(1)), 8813ea4388cSHaoyuan Feng 0.U -> (tag_match(3) && tag_match(2) && tag_match(1) && tag_match(0))) 8823ea4388cSHaoyuan Feng ) 8833ea4388cSHaoyuan Feng 8843ea4388cSHaoyuan Feng asid_hit && vmid_hit && level_match 8856d5ddbceSLemover } else if (hasLevel) { 8863ea4388cSHaoyuan Feng val tag_match = Wire(Vec(3, Bool())) // SuperPage, 512GB, 1GB or 2MB 8873ea4388cSHaoyuan Feng tag_match(0) := tag(tagLen - 1, tagLen - vpnnLen - extendVpnnBits) === vpn(vpnLen - 1, vpnLen - vpnnLen - extendVpnnBits) 8883ea4388cSHaoyuan Feng for (i <- 1 until 3) { 8893ea4388cSHaoyuan 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) 8903ea4388cSHaoyuan Feng } 89145f497a4Shappy-lx 8923ea4388cSHaoyuan Feng val level_match = MuxLookup(level.getOrElse(0.U), false.B)(Seq( 8933ea4388cSHaoyuan Feng 3.U -> tag_match(0), 8943ea4388cSHaoyuan Feng 2.U -> (tag_match(0) && tag_match(1)), 8953ea4388cSHaoyuan Feng 1.U -> (tag_match(0) && tag_match(1) && tag_match(2))) 8963ea4388cSHaoyuan Feng ) 8973ea4388cSHaoyuan Feng 8983ea4388cSHaoyuan Feng asid_hit && vmid_hit && level_match 8996d5ddbceSLemover } else { 90082978df9Speixiaokun asid_hit && vmid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen) 9016d5ddbceSLemover } 9026d5ddbceSLemover } 9036d5ddbceSLemover 904e3da8badSTang Haojin def refill(vpn: UInt, asid: UInt, vmid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B): Unit = { 90545f497a4Shappy-lx require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside 90645f497a4Shappy-lx 9076d5ddbceSLemover tag := vpn(vpnLen - 1, vpnLen - tagLen) 908002c10a4SYanqin Li pbmt := pte.asTypeOf(new PteBundle().cloneType).pbmt 909b9bfce82SHaoyuan Feng ppn := pte.asTypeOf(new PteBundle().cloneType).getPPN() 910a0301c0dSLemover perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm) 911718a93f5SHaoyuan Feng n.map(_ := pte.asTypeOf(new PteBundle().cloneType).n) 91245f497a4Shappy-lx this.asid := asid 913d61cd5eeSpeixiaokun this.vmid.map(_ := vmid) 914bc063562SLemover this.prefetch := prefetch 9158d8ac704SLemover this.v := valid 9166d5ddbceSLemover this.level.map(_ := level) 9176d5ddbceSLemover } 9186d5ddbceSLemover 9198d8ac704SLemover def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) = { 9206d5ddbceSLemover val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel)) 9218d8ac704SLemover e.refill(vpn, asid, pte, level, prefetch, valid) 9226d5ddbceSLemover e 9236d5ddbceSLemover } 9246d5ddbceSLemover 9256d5ddbceSLemover 926f1fe8698SLemover 9276d5ddbceSLemover override def toPrintable: Printable = { 9286d5ddbceSLemover // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}" 929002c10a4SYanqin Li p"tag:0x${Hexadecimal(tag)} pbmt: ${pbmt} ppn:0x${Hexadecimal(ppn)} " + 9306d5ddbceSLemover (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") + 931bc063562SLemover (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") + 932bc063562SLemover p"prefetch:${prefetch}" 9336d5ddbceSLemover } 9346d5ddbceSLemover} 9356d5ddbceSLemover 936718a93f5SHaoyuan Fengclass PtwSectorEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false, hasNapot: Boolean = false)(implicit p: Parameters) extends PtwEntry(tagLen, hasPerm, hasLevel, hasNapot) { 93797929664SXiaokun-Pei override val ppn = UInt(sectorptePPNLen.W) 93863632028SHaoyuan Feng} 93963632028SHaoyuan Feng 940718a93f5SHaoyuan Fengclass PtwMergeEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false, hasNapot: Boolean = false)(implicit p: Parameters) extends PtwSectorEntry(tagLen, hasPerm, hasLevel, hasNapot) { 94163632028SHaoyuan Feng val ppn_low = UInt(sectortlbwidth.W) 94263632028SHaoyuan Feng val af = Bool() 94363632028SHaoyuan Feng val pf = Bool() 9448882eb68SXin Tian val cf = Bool() // Bitmap Check Failed 94563632028SHaoyuan Feng} 94663632028SHaoyuan Feng 947abc4432bSHaoyuan Fengclass PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean, ReservedBits: Int)(implicit p: Parameters) extends PtwBundle { 9486d5ddbceSLemover require(log2Up(num)==log2Down(num)) 9491f4a7c0cSLemover // NOTE: hasPerm means that is leaf or not. 9506d5ddbceSLemover 9516d5ddbceSLemover val tag = UInt(tagLen.W) 95245f497a4Shappy-lx val asid = UInt(asidLen.W) 9534c0e0181SXiaokun-Pei val vmid = Some(UInt(vmidLen.W)) 954002c10a4SYanqin Li val pbmts = Vec(num, UInt(ptePbmtLen.W)) 9554c0e0181SXiaokun-Pei val ppns = Vec(num, UInt(gvpnLen.W)) 956e0c1f271SHaoyuan Feng // valid or not, vs = 0 will not hit 9576d5ddbceSLemover val vs = Vec(num, Bool()) 958e0c1f271SHaoyuan Feng // only pf or not, onlypf = 1 means only trigger pf when nox2late 959e0c1f271SHaoyuan Feng val onlypf = Vec(num, Bool()) 9606d5ddbceSLemover val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None 961bc063562SLemover val prefetch = Bool() 962abc4432bSHaoyuan Feng val reservedBits = if(ReservedBits > 0) Some(UInt(ReservedBits.W)) else None 9636d5ddbceSLemover // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1") 9641f4a7c0cSLemover // NOTE: vs is used for different usage: 9656962b4ffSHaoyuan Feng // for l0, which store the leaf(leaves), vs is page fault or not. 9666962b4ffSHaoyuan Feng // for l1, which shoule not store leaf, vs is valid or not, that will anticipate in hit check 9676962b4ffSHaoyuan Feng // Because, l1 should not store leaf(no perm), it doesn't store perm. 9686962b4ffSHaoyuan Feng // If l1 hit a leaf, the perm is still unavailble. Should still page walk. Complex but nothing helpful. 9691f4a7c0cSLemover // TODO: divide vs into validVec and pfVec 9706962b4ffSHaoyuan Feng // for l1: may valid but pf, so no need for page walk, return random pte with pf. 9716d5ddbceSLemover 9726d5ddbceSLemover def tagClip(vpn: UInt) = { 9736d5ddbceSLemover require(vpn.getWidth == vpnLen) 9746d5ddbceSLemover vpn(vpnLen - 1, vpnLen - tagLen) 9756d5ddbceSLemover } 9766d5ddbceSLemover 9776d5ddbceSLemover def sectorIdxClip(vpn: UInt, level: Int) = { 9786d5ddbceSLemover getVpnClip(vpn, level)(log2Up(num) - 1, 0) 9796d5ddbceSLemover } 9806d5ddbceSLemover 981b188e334Speixiaokun def hit(vpn: UInt, asid: UInt, vasid: UInt, vmid:UInt, ignoreAsid: Boolean = false, s2xlate: Bool) = { 982b188e334Speixiaokun val asid_value = Mux(s2xlate, vasid, asid) 983b188e334Speixiaokun val asid_hit = if (ignoreAsid) true.B else (this.asid === asid_value) 984d61cd5eeSpeixiaokun val vmid_hit = Mux(s2xlate, this.vmid.getOrElse(0.U) === vmid, true.B) 985e0c1f271SHaoyuan Feng asid_hit && vmid_hit && tag === tagClip(vpn) && vs(sectorIdxClip(vpn, level)) 9866d5ddbceSLemover } 9876d5ddbceSLemover 988e0c1f271SHaoyuan Feng def genEntries(vpn: UInt, asid: UInt, vmid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool, s2xlate: UInt, pbmte: Bool, mode: UInt) = { 9896d5ddbceSLemover require((data.getWidth / XLEN) == num, 9905854c1edSLemover s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}") 9916d5ddbceSLemover 992abc4432bSHaoyuan Feng val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm, ReservedBits)) 9936d5ddbceSLemover ps.tag := tagClip(vpn) 99445f497a4Shappy-lx ps.asid := asid 995d61cd5eeSpeixiaokun ps.vmid.map(_ := vmid) 996bc063562SLemover ps.prefetch := prefetch 9976d5ddbceSLemover for (i <- 0 until num) { 9986d5ddbceSLemover val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle) 999002c10a4SYanqin Li ps.pbmts(i) := pte.pbmt 1000b9bfce82SHaoyuan Feng ps.ppns(i) := pte.getPPN() 100126175c3fSHaoyuan Feng ps.vs(i) := (pte.canRefill(levelUInt, s2xlate, pbmte, mode) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf())) || (if (hasPerm) pte.onlyPf(levelUInt, s2xlate, pbmte) else false.B) 1002e0c1f271SHaoyuan Feng ps.onlypf(i) := pte.onlyPf(levelUInt, s2xlate, pbmte) 10036d5ddbceSLemover ps.perms.map(_(i) := pte.perm) 10046d5ddbceSLemover } 1005abc4432bSHaoyuan Feng ps.reservedBits.map(_ := true.B) 10066d5ddbceSLemover ps 10076d5ddbceSLemover } 10086d5ddbceSLemover 10096d5ddbceSLemover override def toPrintable: Printable = { 10106d5ddbceSLemover // require(num == 4, "if num is not 4, please comment this toPrintable") 10116d5ddbceSLemover // NOTE: if num is not 4, please comment this toPrintable 10126d5ddbceSLemover val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle))) 1013002c10a4SYanqin Li p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} pbmt:${printVec(pbmts)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " + 10146d5ddbceSLemover (if (hasPerm) p"perms:${printVec(permsInner)}" else p"") 10156d5ddbceSLemover } 10166d5ddbceSLemover} 10176d5ddbceSLemover 1018abc4432bSHaoyuan Fengclass PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean, ReservedBits: Int = 0)(implicit p: Parameters) extends PtwBundle { 1019abc4432bSHaoyuan Feng val entries = new PtwEntries(num, tagLen, level, hasPerm, ReservedBits) 10207196f5a2SLemover 10213889e11eSLemover val ecc_block = XLEN 10223889e11eSLemover val ecc_info = get_ecc_info() 1023eef81af7SHaoyuan Feng val ecc = if (l2tlbParams.enablePTWECC) Some(UInt(ecc_info._1.W)) else None 10243889e11eSLemover 10253889e11eSLemover def get_ecc_info(): (Int, Int, Int, Int) = { 10263889e11eSLemover val eccBits_per = eccCode.width(ecc_block) - ecc_block 10273889e11eSLemover 10283889e11eSLemover val data_length = entries.getWidth 10293889e11eSLemover val data_align_num = data_length / ecc_block 10303889e11eSLemover val data_not_align = (data_length % ecc_block) != 0 // ugly code 10313889e11eSLemover val data_unalign_length = data_length - data_align_num * ecc_block 10323889e11eSLemover val eccBits_unalign = eccCode.width(data_unalign_length) - data_unalign_length 10333889e11eSLemover 10343889e11eSLemover val eccBits = eccBits_per * data_align_num + eccBits_unalign 10353889e11eSLemover (eccBits, eccBits_per, data_align_num, data_unalign_length) 10363889e11eSLemover } 10373889e11eSLemover 10383889e11eSLemover def encode() = { 1039935edac4STang Haojin val data = entries.asUInt 10403889e11eSLemover val ecc_slices = Wire(Vec(ecc_info._3, UInt(ecc_info._2.W))) 10413889e11eSLemover for (i <- 0 until ecc_info._3) { 10423889e11eSLemover ecc_slices(i) := eccCode.encode(data((i+1)*ecc_block-1, i*ecc_block)) >> ecc_block 10433889e11eSLemover } 10443889e11eSLemover if (ecc_info._4 != 0) { 10453889e11eSLemover val ecc_unaligned = eccCode.encode(data(data.getWidth-1, ecc_info._3*ecc_block)) >> ecc_info._4 1046eef81af7SHaoyuan Feng ecc.map(_ := Cat(ecc_unaligned, ecc_slices.asUInt)) 1047eef81af7SHaoyuan Feng } else { ecc.map(_ := ecc_slices.asUInt)} 10483889e11eSLemover } 10493889e11eSLemover 10503889e11eSLemover def decode(): Bool = { 1051935edac4STang Haojin val data = entries.asUInt 10523889e11eSLemover val res = Wire(Vec(ecc_info._3 + 1, Bool())) 10533889e11eSLemover for (i <- 0 until ecc_info._3) { 1054eef81af7SHaoyuan 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} 10553889e11eSLemover } 10565197bac8SZiyue-Zhang if (ecc_info._2 != 0 && ecc_info._4 != 0) { 10573889e11eSLemover res(ecc_info._3) := eccCode.decode( 1058eef81af7SHaoyuan Feng Cat(ecc.get(ecc_info._1-1, ecc_info._2*ecc_info._3), data(data.getWidth-1, ecc_info._3*ecc_block))).error 10593889e11eSLemover } else { res(ecc_info._3) := false.B } 10603889e11eSLemover 10613889e11eSLemover Cat(res).orR 10623889e11eSLemover } 10633889e11eSLemover 1064e0c1f271SHaoyuan Feng def gen(vpn: UInt, asid: UInt, vmid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool, s2xlate: UInt, pbmte: Bool, mode: UInt) = { 1065e0c1f271SHaoyuan Feng this.entries := entries.genEntries(vpn, asid, vmid, data, levelUInt, prefetch, s2xlate, pbmte, mode) 10663889e11eSLemover this.encode() 10673889e11eSLemover } 10687196f5a2SLemover} 10697196f5a2SLemover 10706d5ddbceSLemoverclass PtwReq(implicit p: Parameters) extends PtwBundle { 107182978df9Speixiaokun val vpn = UInt(vpnLen.W) //vpn or gvpn 107286b5ba4aSpeixiaokun val s2xlate = UInt(2.W) 1073d61cd5eeSpeixiaokun def hasS2xlate(): Bool = { 1074d61cd5eeSpeixiaokun this.s2xlate =/= noS2xlate 1075d61cd5eeSpeixiaokun } 1076e3da8badSTang Haojin def isOnlyStage2: Bool = { 107786b5ba4aSpeixiaokun this.s2xlate === onlyStage2 107886b5ba4aSpeixiaokun } 10796d5ddbceSLemover override def toPrintable: Printable = { 10806d5ddbceSLemover p"vpn:0x${Hexadecimal(vpn)}" 10816d5ddbceSLemover } 10826d5ddbceSLemover} 10836d5ddbceSLemover 10848744445eSMaxpicca-Liclass PtwReqwithMemIdx(implicit p: Parameters) extends PtwReq { 10858744445eSMaxpicca-Li val memidx = new MemBlockidxBundle 1086a4f9c77fSpeixiaokun val getGpa = Bool() // this req is to get gpa when having guest page fault 10878744445eSMaxpicca-Li} 10888744445eSMaxpicca-Li 10896d5ddbceSLemoverclass PtwResp(implicit p: Parameters) extends PtwBundle { 10906d5ddbceSLemover val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true) 10916d5ddbceSLemover val pf = Bool() 1092b6982e83SLemover val af = Bool() 10936d5ddbceSLemover 109445f497a4Shappy-lx def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = { 10955854c1edSLemover this.entry.level.map(_ := level) 10965854c1edSLemover this.entry.tag := vpn 10975854c1edSLemover this.entry.perm.map(_ := pte.getPerm()) 10985854c1edSLemover this.entry.ppn := pte.ppn 1099002c10a4SYanqin Li this.entry.pbmt := pte.pbmt 1100bc063562SLemover this.entry.prefetch := DontCare 110145f497a4Shappy-lx this.entry.asid := asid 11028d8ac704SLemover this.entry.v := !pf 11035854c1edSLemover this.pf := pf 1104b6982e83SLemover this.af := af 11055854c1edSLemover } 11065854c1edSLemover 11076d5ddbceSLemover override def toPrintable: Printable = { 1108b6982e83SLemover p"entry:${entry} pf:${pf} af:${af}" 11096d5ddbceSLemover } 11106d5ddbceSLemover} 11116d5ddbceSLemover 1112d0de7e4aSpeixiaokunclass HptwResp(implicit p: Parameters) extends PtwBundle { 1113718a93f5SHaoyuan Feng val entry = new PtwEntry(tagLen = gvpnLen, hasPerm = true, hasLevel = true, hasNapot = true) 1114d0de7e4aSpeixiaokun val gpf = Bool() 1115d0de7e4aSpeixiaokun val gaf = Bool() 1116d0de7e4aSpeixiaokun 1117d0de7e4aSpeixiaokun def apply(gpf: Bool, gaf: Bool, level: UInt, pte: PteBundle, vpn: UInt, vmid: UInt) = { 11182b16f0c2SXiaokun-Pei val resp_pte = Mux(gaf, 0.U.asTypeOf(pte), pte) 1119d0de7e4aSpeixiaokun this.entry.level.map(_ := level) 1120d0de7e4aSpeixiaokun this.entry.tag := vpn 11212b16f0c2SXiaokun-Pei this.entry.perm.map(_ := resp_pte.getPerm()) 11222b16f0c2SXiaokun-Pei this.entry.ppn := resp_pte.ppn 1123718a93f5SHaoyuan Feng this.entry.n.map(_ := resp_pte.n) 1124002c10a4SYanqin Li this.entry.pbmt := resp_pte.pbmt 1125d0de7e4aSpeixiaokun this.entry.prefetch := DontCare 1126d0de7e4aSpeixiaokun this.entry.asid := DontCare 1127d61cd5eeSpeixiaokun this.entry.vmid.map(_ := vmid) 1128d0de7e4aSpeixiaokun this.entry.v := !gpf 1129d0de7e4aSpeixiaokun this.gpf := gpf 1130d0de7e4aSpeixiaokun this.gaf := gaf 1131d0de7e4aSpeixiaokun } 1132d0de7e4aSpeixiaokun 1133cda84113Speixiaokun def genPPNS2(vpn: UInt): UInt = { 11348c34f10bSpeixiaokun MuxLookup(entry.level.get, 0.U)(Seq( 11353ea4388cSHaoyuan Feng 3.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 3), vpn(vpnnLen * 3 - 1, 0)), 11363ea4388cSHaoyuan Feng 2.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 2), vpn(vpnnLen * 2 - 1, 0)), 1137cda84113Speixiaokun 1.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen), vpn(vpnnLen - 1, 0)), 1138718a93f5SHaoyuan Feng 0.U -> Mux(entry.n.getOrElse(0.U) === 0.U, entry.ppn(entry.ppn.getWidth - 1, 0), Cat(entry.ppn(entry.ppn.getWidth - 1, pteNapotBits), vpn(pteNapotBits - 1, 0))) 1139d0de7e4aSpeixiaokun )) 1140d0de7e4aSpeixiaokun } 1141d0de7e4aSpeixiaokun 1142d0de7e4aSpeixiaokun def hit(gvpn: UInt, vmid: UInt): Bool = { 1143d61cd5eeSpeixiaokun val vmid_hit = this.entry.vmid.getOrElse(0.U) === vmid 11443ea4388cSHaoyuan Feng val tag_match = Wire(Vec(4, Bool())) // 512GB, 1GB, 2MB or 4KB, not parameterized here 11453ea4388cSHaoyuan Feng for (i <- 0 until 3) { 11463ea4388cSHaoyuan Feng tag_match(i) := entry.tag(vpnnLen * (i + 1) - 1, vpnnLen * i) === gvpn(vpnnLen * (i + 1) - 1, vpnnLen * i) 1147d0de7e4aSpeixiaokun } 114897929664SXiaokun-Pei tag_match(3) := entry.tag(gvpnLen - 1, vpnnLen * 3) === gvpn(gvpnLen - 1, vpnnLen * 3) 1149d0de7e4aSpeixiaokun 11503ea4388cSHaoyuan Feng val level_match = MuxLookup(entry.level.getOrElse(0.U), false.B)(Seq( 11513ea4388cSHaoyuan Feng 3.U -> tag_match(3), 11523ea4388cSHaoyuan Feng 2.U -> (tag_match(3) && tag_match(2)), 11533ea4388cSHaoyuan Feng 1.U -> (tag_match(3) && tag_match(2) && tag_match(1)), 11543ea4388cSHaoyuan Feng 0.U -> (tag_match(3) && tag_match(2) && tag_match(1) && tag_match(0))) 11553ea4388cSHaoyuan Feng ) 115663632028SHaoyuan Feng 11573ea4388cSHaoyuan Feng vmid_hit && level_match 115863632028SHaoyuan Feng } 115963632028SHaoyuan Feng} 116063632028SHaoyuan Feng 116163632028SHaoyuan Fengclass PtwSectorResp(implicit p: Parameters) extends PtwBundle { 1162718a93f5SHaoyuan Feng val entry = new PtwSectorEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true, hasNapot = true) 116363632028SHaoyuan Feng val addr_low = UInt(sectortlbwidth.W) 116463632028SHaoyuan Feng val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W)) 116563632028SHaoyuan Feng val valididx = Vec(tlbcontiguous, Bool()) 1166b0fa7106SHaoyuan Feng val pteidx = Vec(tlbcontiguous, Bool()) 116763632028SHaoyuan Feng val pf = Bool() 116863632028SHaoyuan Feng val af = Bool() 116963632028SHaoyuan Feng 1170d0de7e4aSpeixiaokun 117163632028SHaoyuan Feng def genPPN(vpn: UInt): UInt = { 117245f43e6eSTang Haojin MuxLookup(entry.level.get, 0.U)(Seq( 11733ea4388cSHaoyuan Feng 3.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 3 - sectortlbwidth), vpn(vpnnLen * 3 - 1, 0)), 11743ea4388cSHaoyuan Feng 2.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), vpn(vpnnLen * 2 - 1, 0)), 117563632028SHaoyuan Feng 1.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen - sectortlbwidth), vpn(vpnnLen - 1, 0)), 1176718a93f5SHaoyuan Feng 0.U -> Mux(entry.n.getOrElse(0.U) === 0.U, Cat(entry.ppn(entry.ppn.getWidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0))), Cat(entry.ppn(entry.ppn.getWidth - 1, pteNapotBits - sectortlbwidth), vpn(pteNapotBits - 1, 0)))) 117763632028SHaoyuan Feng ) 117863632028SHaoyuan Feng } 117963632028SHaoyuan Feng 11802ea10b44SXiaokun-Pei def genGVPN(vpn: UInt): UInt = { 11812ea10b44SXiaokun-Pei val isNonLeaf = !(entry.perm.get.r || entry.perm.get.x || entry.perm.get.w) && entry.v && !pf && !af 11822ea10b44SXiaokun-Pei Mux(isNonLeaf, Cat(entry.ppn(entry.ppn.getWidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0))), genPPN(vpn)) 11832ea10b44SXiaokun-Pei } 11842ea10b44SXiaokun-Pei 1185ad8d4021SXiaokun-Pei def isLeaf() = { 1186ad8d4021SXiaokun-Pei (entry.perm.get.r || entry.perm.get.x || entry.perm.get.w) && entry.v 1187ad8d4021SXiaokun-Pei } 1188ad8d4021SXiaokun-Pei 1189ad8d4021SXiaokun-Pei def isFakePte() = { 11907acf8b76SXiaokun-Pei !pf && !entry.v && !af 1191ad8d4021SXiaokun-Pei } 1192ad8d4021SXiaokun-Pei 1193d0de7e4aSpeixiaokun def hit(vpn: UInt, asid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false, s2xlate: Bool): Bool = { 119463632028SHaoyuan Feng require(vpn.getWidth == vpnLen) 119563632028SHaoyuan Feng // require(this.asid.getWidth <= asid.getWidth) 119663632028SHaoyuan Feng val asid_hit = if (ignoreAsid) true.B else (this.entry.asid === asid) 1197d61cd5eeSpeixiaokun val vmid_hit = Mux(s2xlate, this.entry.vmid.getOrElse(0.U) === vmid, true.B) 119863632028SHaoyuan Feng if (allType) { 119963632028SHaoyuan Feng val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0)) 12003ea4388cSHaoyuan Feng val tag_match = Wire(Vec(4, Bool())) // 512GB, 1GB, 2MB or 4KB, not parameterized here 12013ea4388cSHaoyuan Feng tag_match(0) := entry.tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) 12023ea4388cSHaoyuan Feng for (i <- 1 until 3) { 12033ea4388cSHaoyuan Feng tag_match(i) := entry.tag(vpnnLen * (i + 1) - sectortlbwidth - 1, vpnnLen * i - sectortlbwidth) === vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) 12043ea4388cSHaoyuan Feng } 12053ea4388cSHaoyuan Feng tag_match(3) := entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth) === vpn(vpnLen - 1, vpnnLen * 3) 120663632028SHaoyuan Feng 12073ea4388cSHaoyuan Feng val level_match = MuxLookup(entry.level.getOrElse(0.U), false.B)(Seq( 12083ea4388cSHaoyuan Feng 3.U -> tag_match(3), 12093ea4388cSHaoyuan Feng 2.U -> (tag_match(3) && tag_match(2)), 12103ea4388cSHaoyuan Feng 1.U -> (tag_match(3) && tag_match(2) && tag_match(1)), 12113ea4388cSHaoyuan Feng 0.U -> (tag_match(3) && tag_match(2) && tag_match(1) && tag_match(0))) 12123ea4388cSHaoyuan Feng ) 12133ea4388cSHaoyuan Feng 12143ea4388cSHaoyuan Feng asid_hit && vmid_hit && level_match && addr_low_hit 121563632028SHaoyuan Feng } else { 121663632028SHaoyuan Feng val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0)) 12173ea4388cSHaoyuan Feng val tag_match = Wire(Vec(3, Bool())) // SuperPage, 512GB, 1GB or 2MB 12183ea4388cSHaoyuan Feng for (i <- 0 until 3) { 12193ea4388cSHaoyuan Feng tag_match(i) := entry.tag(sectorvpnLen - vpnnLen * i - 1, sectorvpnLen - vpnnLen * (i + 1)) === vpn(vpnLen - vpnnLen * i - 1, vpnLen - vpnnLen * (i + 1)) 12203ea4388cSHaoyuan Feng } 122163632028SHaoyuan Feng 12223ea4388cSHaoyuan Feng val level_match = MuxLookup(entry.level.getOrElse(0.U), false.B)(Seq( 12233ea4388cSHaoyuan Feng 3.U -> tag_match(0), 12243ea4388cSHaoyuan Feng 2.U -> (tag_match(0) && tag_match(1)), 12253ea4388cSHaoyuan Feng 1.U -> (tag_match(0) && tag_match(1) && tag_match(2))) 12263ea4388cSHaoyuan Feng ) 12273ea4388cSHaoyuan Feng 12283ea4388cSHaoyuan Feng asid_hit && vmid_hit && level_match && addr_low_hit 122963632028SHaoyuan Feng } 123063632028SHaoyuan Feng } 123163632028SHaoyuan Feng} 123263632028SHaoyuan Feng 123363632028SHaoyuan Fengclass PtwMergeResp(implicit p: Parameters) extends PtwBundle { 1234718a93f5SHaoyuan Feng val entry = Vec(tlbcontiguous, new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true, hasNapot = true)) 123563632028SHaoyuan Feng val pteidx = Vec(tlbcontiguous, Bool()) 123663632028SHaoyuan Feng val not_super = Bool() 12376962b4ffSHaoyuan Feng val not_merge = Bool() 123863632028SHaoyuan Feng 12398882eb68SXin Tian def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt, vmid:UInt, addr_low : UInt, not_super : Boolean = true, not_merge: Boolean = false, cf : Bool) = { 124063632028SHaoyuan Feng assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!") 12417263b595SXiaokun-Pei val resp_pte = pte 1242718a93f5SHaoyuan Feng val ptw_resp = Wire(new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true, hasNapot = true)) 124397929664SXiaokun-Pei ptw_resp.ppn := resp_pte.getPPN()(ptePPNLen - 1, sectortlbwidth) 12444c0e0181SXiaokun-Pei ptw_resp.ppn_low := resp_pte.getPPN()(sectortlbwidth - 1, 0) 1245718a93f5SHaoyuan Feng ptw_resp.n.map(_ := resp_pte.n) 1246002c10a4SYanqin Li ptw_resp.pbmt := resp_pte.pbmt 124763632028SHaoyuan Feng ptw_resp.level.map(_ := level) 12482b16f0c2SXiaokun-Pei ptw_resp.perm.map(_ := resp_pte.getPerm()) 124963632028SHaoyuan Feng ptw_resp.tag := vpn(vpnLen - 1, sectortlbwidth) 125063632028SHaoyuan Feng ptw_resp.pf := pf 125163632028SHaoyuan Feng ptw_resp.af := af 12528882eb68SXin Tian ptw_resp.cf := cf // Bitmap Check Failed 1253ad8d4021SXiaokun-Pei ptw_resp.v := resp_pte.perm.v 125463632028SHaoyuan Feng ptw_resp.prefetch := DontCare 125563632028SHaoyuan Feng ptw_resp.asid := asid 1256eb4bf3f2Speixiaokun ptw_resp.vmid.map(_ := vmid) 125763632028SHaoyuan Feng this.pteidx := UIntToOH(addr_low).asBools 125863632028SHaoyuan Feng this.not_super := not_super.B 12596962b4ffSHaoyuan Feng this.not_merge := not_merge.B 1260d0de7e4aSpeixiaokun 126163632028SHaoyuan Feng for (i <- 0 until tlbcontiguous) { 126263632028SHaoyuan Feng this.entry(i) := ptw_resp 126363632028SHaoyuan Feng } 126463632028SHaoyuan Feng } 126530104977Speixiaokun 126630104977Speixiaokun def genPPN(): UInt = { 126730104977Speixiaokun val idx = OHToUInt(pteidx) 126809280d15Speixiaokun val tag = Cat(entry(idx).tag, idx(sectortlbwidth - 1, 0)) 12696f508cb5Speixiaokun MuxLookup(entry(idx).level.get, 0.U)(Seq( 12703ea4388cSHaoyuan Feng 3.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen * 3 - sectortlbwidth), tag(vpnnLen * 3 - 1, 0)), 12713ea4388cSHaoyuan Feng 2.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), tag(vpnnLen * 2 - 1, 0)), 127209280d15Speixiaokun 1.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen - sectortlbwidth), tag(vpnnLen - 1, 0)), 1273*cfbfe74eSHaoyuan Feng 0.U -> Mux(entry(idx).n.getOrElse(0.U) === 0.U, 1274*cfbfe74eSHaoyuan Feng Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, 0), entry(idx).ppn_low), 1275*cfbfe74eSHaoyuan Feng Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, pteNapotBits - sectortlbwidth), tag(pteNapotBits - 1, 0)))) 127630104977Speixiaokun ) 127730104977Speixiaokun } 127863632028SHaoyuan Feng} 12798744445eSMaxpicca-Li 1280d0de7e4aSpeixiaokunclass PtwRespS2(implicit p: Parameters) extends PtwBundle { 1281d0de7e4aSpeixiaokun val s2xlate = UInt(2.W) 1282d0de7e4aSpeixiaokun val s1 = new PtwSectorResp() 1283d0de7e4aSpeixiaokun val s2 = new HptwResp() 128486b5ba4aSpeixiaokun 1285e3da8badSTang Haojin def hasS2xlate: Bool = { 128686b5ba4aSpeixiaokun this.s2xlate =/= noS2xlate 128786b5ba4aSpeixiaokun } 128886b5ba4aSpeixiaokun 1289e3da8badSTang Haojin def isOnlyStage2: Bool = { 129086b5ba4aSpeixiaokun this.s2xlate === onlyStage2 129186b5ba4aSpeixiaokun } 129286b5ba4aSpeixiaokun 12939cb05b4dSXiaokun-Pei def getVpn(vpn: UInt): UInt = { 12943ea4388cSHaoyuan Feng val level = s1.entry.level.getOrElse(0.U) min s2.entry.level.getOrElse(0.U) 12959cb05b4dSXiaokun-Pei val s1tag = Cat(s1.entry.tag, OHToUInt(s1.pteidx)) 12969cb05b4dSXiaokun-Pei val s1_vpn = MuxLookup(level, s1tag)(Seq( 12975e0a22e7SXiaokun-Pei 3.U -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 3 - sectortlbwidth), vpn(vpnnLen * 3 - 1, 0)), 12985e0a22e7SXiaokun-Pei 2.U -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth), vpn(vpnnLen * 2 - 1, 0)), 12995e0a22e7SXiaokun-Pei 1.U -> Cat(s1.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth), vpn(vpnnLen - 1, 0))) 13009cb05b4dSXiaokun-Pei ) 13019cb05b4dSXiaokun-Pei val s2_vpn = s2.entry.tag 13029cb05b4dSXiaokun-Pei Mux(s2xlate === onlyStage2, s2_vpn, Mux(s2xlate === allStage, s1_vpn, s1tag)) 1303c3d5cfb3Speixiaokun } 13044c4af37cSpeixiaokun 13054c4af37cSpeixiaokun def hit(vpn: UInt, asid: UInt, vasid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false): Bool = { 1306e3da8badSTang Haojin val noS2_hit = s1.hit(vpn, Mux(this.hasS2xlate, vasid, asid), vmid, allType, ignoreAsid, this.hasS2xlate) 130768750422Speixiaokun val onlyS2_hit = s2.hit(vpn, vmid) 130868750422Speixiaokun // allstage and onlys1 hit 130968750422Speixiaokun val s1vpn = Cat(s1.entry.tag, s1.addr_low) 13104b9ddb8aSHaoyuan Feng val level = Mux(this.s2xlate === onlyStage1, 13114b9ddb8aSHaoyuan Feng s1.entry.level.getOrElse(0.U), 13124b9ddb8aSHaoyuan Feng // when allStage, level is the smaller one of stage1 and stage2 13134b9ddb8aSHaoyuan Feng // e.g. stage1 is 1GB page, stage2 is 2MB page,then level is 2MB 13144b9ddb8aSHaoyuan Feng s1.entry.level.getOrElse(0.U) min s2.entry.level.getOrElse(0.U)) 13153ea4388cSHaoyuan Feng 13163ea4388cSHaoyuan Feng val tag_match = Wire(Vec(4, Bool())) // 512GB, 1GB, 2MB or 4KB, not parameterized here 131798451f8cSXiaokun-Pei for (i <- 0 until 3) { 13183ea4388cSHaoyuan Feng tag_match(i) := vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) === s1vpn(vpnnLen * (i + 1) - 1, vpnnLen * i) 13193ea4388cSHaoyuan Feng } 132098451f8cSXiaokun-Pei tag_match(3) := vpn(vpnLen - 1, vpnnLen * 3) === s1vpn(vpnLen - 1, vpnnLen * 3) 13213ea4388cSHaoyuan Feng val level_match = MuxLookup(level, false.B)(Seq( 13223ea4388cSHaoyuan Feng 3.U -> tag_match(3), 13233ea4388cSHaoyuan Feng 2.U -> (tag_match(3) && tag_match(2)), 13243ea4388cSHaoyuan Feng 1.U -> (tag_match(3) && tag_match(2) && tag_match(1)), 13253ea4388cSHaoyuan Feng 0.U -> (tag_match(3) && tag_match(2) && tag_match(1) && tag_match(0))) 13263ea4388cSHaoyuan Feng ) 13273ea4388cSHaoyuan Feng 13283ea4388cSHaoyuan Feng val vpn_hit = level_match 13296cbf16a0SHaoyuan Feng val vmid_hit = s1.entry.vmid.getOrElse(0.U) === vmid 133068750422Speixiaokun val vasid_hit = if (ignoreAsid) true.B else (s1.entry.asid === vasid) 133168750422Speixiaokun val all_onlyS1_hit = vpn_hit && vmid_hit && vasid_hit 133268750422Speixiaokun Mux(this.s2xlate === noS2xlate, noS2_hit, 133368750422Speixiaokun Mux(this.s2xlate === onlyStage2, onlyS2_hit, all_onlyS1_hit)) 13344c4af37cSpeixiaokun } 1335d0de7e4aSpeixiaokun} 1336d0de7e4aSpeixiaokun 1337d0de7e4aSpeixiaokunclass PtwRespS2withMemIdx(implicit p: Parameters) extends PtwRespS2 { 1338d0de7e4aSpeixiaokun val memidx = new MemBlockidxBundle() 1339a4f9c77fSpeixiaokun val getGpa = Bool() // this req is to get gpa when having guest page fault 1340d0de7e4aSpeixiaokun} 1341d0de7e4aSpeixiaokun 134292e3bfefSLemoverclass L2TLBIO(implicit p: Parameters) extends PtwBundle { 1343f57f7f2aSYangyu Chen val hartId = Input(UInt(hartIdLen.W)) 13446d5ddbceSLemover val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO)) 13456d5ddbceSLemover val sfence = Input(new SfenceBundle) 1346b6982e83SLemover val csr = new Bundle { 1347b6982e83SLemover val tlb = Input(new TlbCsrBundle) 1348b6982e83SLemover val distribute_csr = Flipped(new DistributedCSRIO) 1349b6982e83SLemover } 13506d5ddbceSLemover} 13516d5ddbceSLemover 1352b848eea5SLemoverclass L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle { 1353b848eea5SLemover val addr = UInt(PAddrBits.W) 1354b848eea5SLemover val id = UInt(bMemID.W) 135583d93d53Speixiaokun val hptw_bypassed = Bool() 1356b848eea5SLemover} 135745f497a4Shappy-lx 135845f497a4Shappy-lxclass L2TlbInnerBundle(implicit p: Parameters) extends PtwReq { 135945f497a4Shappy-lx val source = UInt(bSourceWidth.W) 136045f497a4Shappy-lx} 1361f1fe8698SLemover 13626967f5d5Speixiaokunclass L2TlbWithHptwIdBundle(implicit p: Parameters) extends PtwBundle { 13636967f5d5Speixiaokun val req_info = new L2TlbInnerBundle 1364325f0a4eSpeixiaokun val isHptwReq = Bool() 13657f6221c5Speixiaokun val isLLptw = Bool() 13666967f5d5Speixiaokun val hptwId = UInt(log2Up(l2tlbParams.llptwsize).W) 13676967f5d5Speixiaokun} 1368f1fe8698SLemover 1369f1fe8698SLemoverobject ValidHoldBypass{ 1370f1fe8698SLemover def apply(infire: Bool, outfire: Bool, flush: Bool = false.B) = { 1371f1fe8698SLemover val valid = RegInit(false.B) 1372f1fe8698SLemover when (infire) { valid := true.B } 1373f1fe8698SLemover when (outfire) { valid := false.B } // ATTENTION: order different with ValidHold 1374f1fe8698SLemover when (flush) { valid := false.B } // NOTE: the flush will flush in & out, is that ok? 1375f1fe8698SLemover valid || infire 1376f1fe8698SLemover } 1377f1fe8698SLemover} 13785afdf73cSHaoyuan Feng 13795afdf73cSHaoyuan Fengclass L1TlbDB(implicit p: Parameters) extends TlbBundle { 13805afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 13815afdf73cSHaoyuan Feng} 13825afdf73cSHaoyuan Feng 13835afdf73cSHaoyuan Fengclass PageCacheDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 13845afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 13855afdf73cSHaoyuan Feng val source = UInt(bSourceWidth.W) 13865afdf73cSHaoyuan Feng val bypassed = Bool() 13875afdf73cSHaoyuan Feng val is_first = Bool() 13885afdf73cSHaoyuan Feng val prefetched = Bool() 13895afdf73cSHaoyuan Feng val prefetch = Bool() 13905afdf73cSHaoyuan Feng val l2Hit = Bool() 13915afdf73cSHaoyuan Feng val l1Hit = Bool() 13925afdf73cSHaoyuan Feng val hit = Bool() 13935afdf73cSHaoyuan Feng} 13945afdf73cSHaoyuan Feng 13955afdf73cSHaoyuan Fengclass PTWDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 13965afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 13975afdf73cSHaoyuan Feng val source = UInt(bSourceWidth.W) 13985afdf73cSHaoyuan Feng} 13995afdf73cSHaoyuan Feng 14005afdf73cSHaoyuan Fengclass L2TlbPrefetchDB(implicit p: Parameters) extends TlbBundle { 14015afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 14025afdf73cSHaoyuan Feng} 14035afdf73cSHaoyuan Feng 14045afdf73cSHaoyuan Fengclass L2TlbMissQueueDB(implicit p: Parameters) extends TlbBundle { 14055afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 14065afdf73cSHaoyuan Feng} 1407