16d5ddbceSLemover/*************************************************************************************** 26d5ddbceSLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory 46d5ddbceSLemover* 56d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2. 66d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2. 76d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at: 86d5ddbceSLemover* http://license.coscl.org.cn/MulanPSL2 96d5ddbceSLemover* 106d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 116d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 126d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 136d5ddbceSLemover* 146d5ddbceSLemover* See the Mulan PSL v2 for more details. 156d5ddbceSLemover***************************************************************************************/ 166d5ddbceSLemover 176d5ddbceSLemoverpackage xiangshan.cache.mmu 186d5ddbceSLemover 196d5ddbceSLemoverimport chipsalliance.rocketchip.config.Parameters 206d5ddbceSLemoverimport chisel3._ 216d5ddbceSLemoverimport chisel3.util._ 226d5ddbceSLemoverimport xiangshan._ 236d5ddbceSLemoverimport utils._ 243c02ee8fSwakafaimport utility._ 259aca92b9SYinan Xuimport xiangshan.backend.rob.RobPtr 266d5ddbceSLemoverimport xiangshan.backend.fu.util.HasCSRConst 276d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 286d5ddbceSLemoverimport freechips.rocketchip.tilelink._ 295b7ef044SLemoverimport xiangshan.backend.fu.{PMPReqBundle, PMPConfig} 30f1fe8698SLemoverimport xiangshan.backend.fu.PMPBundle 315b7ef044SLemover 326d5ddbceSLemover 336d5ddbceSLemoverabstract class TlbBundle(implicit p: Parameters) extends XSBundle with HasTlbConst 346d5ddbceSLemoverabstract class TlbModule(implicit p: Parameters) extends XSModule with HasTlbConst 356d5ddbceSLemover 36a0301c0dSLemoverclass VaBundle(implicit p: Parameters) extends TlbBundle { 37a0301c0dSLemover val vpn = UInt(vpnLen.W) 38a0301c0dSLemover val off = UInt(offLen.W) 39a0301c0dSLemover} 40a0301c0dSLemover 416d5ddbceSLemoverclass PtePermBundle(implicit p: Parameters) extends TlbBundle { 426d5ddbceSLemover val d = Bool() 436d5ddbceSLemover val a = Bool() 446d5ddbceSLemover val g = Bool() 456d5ddbceSLemover val u = Bool() 466d5ddbceSLemover val x = Bool() 476d5ddbceSLemover val w = Bool() 486d5ddbceSLemover val r = Bool() 496d5ddbceSLemover 506d5ddbceSLemover override def toPrintable: Printable = { 516d5ddbceSLemover p"d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"// + 526d5ddbceSLemover //(if(hasV) (p"v:${v}") else p"") 536d5ddbceSLemover } 546d5ddbceSLemover} 556d5ddbceSLemover 565b7ef044SLemoverclass TlbPMBundle(implicit p: Parameters) extends TlbBundle { 575b7ef044SLemover val r = Bool() 585b7ef044SLemover val w = Bool() 595b7ef044SLemover val x = Bool() 605b7ef044SLemover val c = Bool() 615b7ef044SLemover val atomic = Bool() 625b7ef044SLemover 635b7ef044SLemover def assign_ap(pm: PMPConfig) = { 645b7ef044SLemover r := pm.r 655b7ef044SLemover w := pm.w 665b7ef044SLemover x := pm.x 675b7ef044SLemover c := pm.c 685b7ef044SLemover atomic := pm.atomic 695b7ef044SLemover } 705b7ef044SLemover} 715b7ef044SLemover 726d5ddbceSLemoverclass TlbPermBundle(implicit p: Parameters) extends TlbBundle { 736d5ddbceSLemover val pf = Bool() // NOTE: if this is true, just raise pf 74b6982e83SLemover val af = Bool() // NOTE: if this is true, just raise af 756d5ddbceSLemover // pagetable perm (software defined) 766d5ddbceSLemover val d = Bool() 776d5ddbceSLemover val a = Bool() 786d5ddbceSLemover val g = Bool() 796d5ddbceSLemover val u = Bool() 806d5ddbceSLemover val x = Bool() 816d5ddbceSLemover val w = Bool() 826d5ddbceSLemover val r = Bool() 836d5ddbceSLemover 845b7ef044SLemover val pm = new TlbPMBundle 855b7ef044SLemover 86f1fe8698SLemover def apply(item: PtwResp, pm: PMPConfig) = { 87f1fe8698SLemover val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 88f1fe8698SLemover this.pf := item.pf 89f1fe8698SLemover this.af := item.af 90f1fe8698SLemover this.d := ptePerm.d 91f1fe8698SLemover this.a := ptePerm.a 92f1fe8698SLemover this.g := ptePerm.g 93f1fe8698SLemover this.u := ptePerm.u 94f1fe8698SLemover this.x := ptePerm.x 95f1fe8698SLemover this.w := ptePerm.w 96f1fe8698SLemover this.r := ptePerm.r 97f1fe8698SLemover 98f1fe8698SLemover this.pm.assign_ap(pm) 99f1fe8698SLemover this 100f1fe8698SLemover } 1016d5ddbceSLemover override def toPrintable: Printable = { 1025b7ef044SLemover p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " + 1035b7ef044SLemover p"pm:${pm}" 1046d5ddbceSLemover } 1056d5ddbceSLemover} 1066d5ddbceSLemover 1076d5ddbceSLemover// multi-read && single-write 1086d5ddbceSLemover// input is data, output is hot-code(not one-hot) 1096d5ddbceSLemoverclass CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule { 1106d5ddbceSLemover val io = IO(new Bundle { 1116d5ddbceSLemover val r = new Bundle { 1126d5ddbceSLemover val req = Input(Vec(readWidth, gen)) 1136d5ddbceSLemover val resp = Output(Vec(readWidth, Vec(set, Bool()))) 1146d5ddbceSLemover } 1156d5ddbceSLemover val w = Input(new Bundle { 1166d5ddbceSLemover val valid = Bool() 1176d5ddbceSLemover val bits = new Bundle { 1186d5ddbceSLemover val index = UInt(log2Up(set).W) 1196d5ddbceSLemover val data = gen 1206d5ddbceSLemover } 1216d5ddbceSLemover }) 1226d5ddbceSLemover }) 1236d5ddbceSLemover 1246d5ddbceSLemover val wordType = UInt(gen.getWidth.W) 1256d5ddbceSLemover val array = Reg(Vec(set, wordType)) 1266d5ddbceSLemover 1276d5ddbceSLemover io.r.resp.zipWithIndex.map{ case (a,i) => 1286d5ddbceSLemover a := array.map(io.r.req(i).asUInt === _) 1296d5ddbceSLemover } 1306d5ddbceSLemover 1316d5ddbceSLemover when (io.w.valid) { 13276e02f07SLingrui98 array(io.w.bits.index) := io.w.bits.data.asUInt 1336d5ddbceSLemover } 1346d5ddbceSLemover} 1356d5ddbceSLemover 136a0301c0dSLemoverclass TlbEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle { 137a0301c0dSLemover require(pageNormal || pageSuper) 138a0301c0dSLemover 139a0301c0dSLemover val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W) 140a0301c0dSLemover else UInt(vpnLen.W) 14145f497a4Shappy-lx val asid = UInt(asidLen.W) 142a0301c0dSLemover val level = if (!pageNormal) Some(UInt(1.W)) 143a0301c0dSLemover else if (!pageSuper) None 144a0301c0dSLemover else Some(UInt(2.W)) 145a0301c0dSLemover val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W) 146a0301c0dSLemover else UInt(ppnLen.W) 147a0301c0dSLemover val perm = new TlbPermBundle 148a0301c0dSLemover 14956728e73SLemover /** level usage: 15056728e73SLemover * !PageSuper: page is only normal, level is None, match all the tag 15156728e73SLemover * !PageNormal: page is only super, level is a Bool(), match high 9*2 parts 15256728e73SLemover * bits0 0: need mid 9bits 15356728e73SLemover * 1: no need mid 9bits 15456728e73SLemover * PageSuper && PageNormal: page hold all the three type, 15556728e73SLemover * bits0 0: need low 9bits 15656728e73SLemover * bits1 0: need mid 9bits 15756728e73SLemover */ 15856728e73SLemover 159e9092fe2SLemover def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false): Bool = { 16045f497a4Shappy-lx val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 161e9092fe2SLemover 162e9092fe2SLemover // NOTE: for timing, dont care low set index bits at hit check 163e9092fe2SLemover // do not need store the low bits actually 164e9092fe2SLemover if (!pageSuper) asid_hit && drop_set_equal(vpn, tag, nSets) 16556728e73SLemover else if (!pageNormal) { 16656728e73SLemover val tag_match_hi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*3-1, vpnnLen*2) 16756728e73SLemover val tag_match_mi = tag(vpnnLen-1, 0) === vpn(vpnnLen*2-1, vpnnLen) 16856728e73SLemover val tag_match = tag_match_hi && (level.get.asBool() || tag_match_mi) 16956728e73SLemover asid_hit && tag_match 17056728e73SLemover } 17156728e73SLemover else { 17256728e73SLemover val tmp_level = level.get 17356728e73SLemover val tag_match_hi = tag(vpnnLen*3-1, vpnnLen*2) === vpn(vpnnLen*3-1, vpnnLen*2) 17456728e73SLemover val tag_match_mi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*2-1, vpnnLen) 17556728e73SLemover val tag_match_lo = tag(vpnnLen-1, 0) === vpn(vpnnLen-1, 0) // if pageNormal is false, this will always be false 17656728e73SLemover val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 17756728e73SLemover asid_hit && tag_match 17856728e73SLemover } 179a0301c0dSLemover } 180a0301c0dSLemover 1815b7ef044SLemover def apply(item: PtwResp, asid: UInt, pm: PMPConfig): TlbEntry = { 182a0301c0dSLemover this.tag := {if (pageNormal) item.entry.tag else item.entry.tag(vpnLen-1, vpnnLen)} 18345f497a4Shappy-lx this.asid := asid 184a0301c0dSLemover val inner_level = item.entry.level.getOrElse(0.U) 18556728e73SLemover this.level.map(_ := { if (pageNormal && pageSuper) MuxLookup(inner_level, 0.U, Seq( 18656728e73SLemover 0.U -> 3.U, 18756728e73SLemover 1.U -> 1.U, 18856728e73SLemover 2.U -> 0.U )) 18956728e73SLemover else if (pageSuper) ~inner_level(0) 190a0301c0dSLemover else 0.U }) 191a0301c0dSLemover this.ppn := { if (!pageNormal) item.entry.ppn(ppnLen-1, vpnnLen) 192a0301c0dSLemover else item.entry.ppn } 193f1fe8698SLemover this.perm.apply(item, pm) 194a0301c0dSLemover this 195a0301c0dSLemover } 196a0301c0dSLemover 19756728e73SLemover // 4KB is normal entry, 2MB/1GB is considered as super entry 19856728e73SLemover def is_normalentry(): Bool = { 19956728e73SLemover if (!pageSuper) { true.B } 20056728e73SLemover else if (!pageNormal) { false.B } 20156728e73SLemover else { level.get === 0.U } 20256728e73SLemover } 2035cf62c1aSLemover 20456728e73SLemover def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = { 20556728e73SLemover val inner_level = level.getOrElse(0.U) 20656728e73SLemover val ppn_res = if (!pageSuper) ppn 20756728e73SLemover else if (!pageNormal) Cat(ppn(ppnLen-vpnnLen-1, vpnnLen), 20856728e73SLemover Mux(inner_level(0), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen-1,0)), 20956728e73SLemover vpn(vpnnLen-1, 0)) 21056728e73SLemover else Cat(ppn(ppnLen-1, vpnnLen*2), 21156728e73SLemover Mux(inner_level(1), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen*2-1, vpnnLen)), 21256728e73SLemover Mux(inner_level(0), vpn(vpnnLen-1, 0), ppn(vpnnLen-1, 0))) 21356728e73SLemover 21456728e73SLemover if (saveLevel) Cat(ppn(ppn.getWidth-1, vpnnLen*2), RegEnable(ppn_res(vpnnLen*2-1, 0), valid)) 2155cf62c1aSLemover else ppn_res 216a0301c0dSLemover } 217a0301c0dSLemover 218a0301c0dSLemover override def toPrintable: Printable = { 219a0301c0dSLemover val inner_level = level.getOrElse(2.U) 22045f497a4Shappy-lx p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}" 221a0301c0dSLemover } 222a0301c0dSLemover 223a0301c0dSLemover} 224a0301c0dSLemover 2256d5ddbceSLemoverobject TlbCmd { 2266d5ddbceSLemover def read = "b00".U 2276d5ddbceSLemover def write = "b01".U 2286d5ddbceSLemover def exec = "b10".U 2296d5ddbceSLemover 2306d5ddbceSLemover def atom_read = "b100".U // lr 2316d5ddbceSLemover def atom_write = "b101".U // sc / amo 2326d5ddbceSLemover 2336d5ddbceSLemover def apply() = UInt(3.W) 2346d5ddbceSLemover def isRead(a: UInt) = a(1,0)===read 2356d5ddbceSLemover def isWrite(a: UInt) = a(1,0)===write 2366d5ddbceSLemover def isExec(a: UInt) = a(1,0)===exec 2376d5ddbceSLemover 2386d5ddbceSLemover def isAtom(a: UInt) = a(2) 239a79fef67Swakafa def isAmo(a: UInt) = a===atom_write // NOTE: sc mixed 2406d5ddbceSLemover} 2416d5ddbceSLemover 24203efd994Shappy-lxclass TlbStorageIO(nSets: Int, nWays: Int, ports: Int, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 243a0301c0dSLemover val r = new Bundle { 244a0301c0dSLemover val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 245a0301c0dSLemover val vpn = Output(UInt(vpnLen.W)) 246a0301c0dSLemover }))) 247a0301c0dSLemover val resp = Vec(ports, ValidIO(new Bundle{ 248a0301c0dSLemover val hit = Output(Bool()) 24903efd994Shappy-lx val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 25003efd994Shappy-lx val perm = Vec(nDups, Output(new TlbPermBundle())) 251a0301c0dSLemover })) 252a0301c0dSLemover } 253a0301c0dSLemover val w = Flipped(ValidIO(new Bundle { 254a0301c0dSLemover val wayIdx = Output(UInt(log2Up(nWays).W)) 255a0301c0dSLemover val data = Output(new PtwResp) 2565b7ef044SLemover val data_replenish = Output(new PMPConfig) 257a0301c0dSLemover })) 258a0301c0dSLemover val victim = new Bundle { 25945f497a4Shappy-lx val out = ValidIO(Output(new Bundle { 26045f497a4Shappy-lx val entry = new TlbEntry(pageNormal = true, pageSuper = false) 26145f497a4Shappy-lx })) 26245f497a4Shappy-lx val in = Flipped(ValidIO(Output(new Bundle { 26345f497a4Shappy-lx val entry = new TlbEntry(pageNormal = true, pageSuper = false) 26445f497a4Shappy-lx }))) 265a0301c0dSLemover } 2663889e11eSLemover val access = Vec(ports, new ReplaceAccessBundle(nSets, nWays)) 267a0301c0dSLemover 268f1fe8698SLemover def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = { 269a0301c0dSLemover this.r.req(i).valid := valid 270a0301c0dSLemover this.r.req(i).bits.vpn := vpn 271a0301c0dSLemover } 272a0301c0dSLemover 273a0301c0dSLemover def r_resp_apply(i: Int) = { 274f1fe8698SLemover (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm) 275a0301c0dSLemover } 276a0301c0dSLemover 2775b7ef044SLemover def w_apply(valid: Bool, wayIdx: UInt, data: PtwResp, data_replenish: PMPConfig): Unit = { 278a0301c0dSLemover this.w.valid := valid 279a0301c0dSLemover this.w.bits.wayIdx := wayIdx 280a0301c0dSLemover this.w.bits.data := data 2815b7ef044SLemover this.w.bits.data_replenish := data_replenish 282a0301c0dSLemover } 283a0301c0dSLemover 284a0301c0dSLemover} 285a0301c0dSLemover 28603efd994Shappy-lxclass TlbStorageWrapperIO(ports: Int, q: TLBParameters, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 287f1fe8698SLemover val r = new Bundle { 288f1fe8698SLemover val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 289f1fe8698SLemover val vpn = Output(UInt(vpnLen.W)) 290f1fe8698SLemover }))) 291f1fe8698SLemover val resp = Vec(ports, ValidIO(new Bundle{ 292f1fe8698SLemover val hit = Output(Bool()) 29303efd994Shappy-lx val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 29403efd994Shappy-lx val perm = Vec(nDups, Output(new TlbPermBundle())) 295f1fe8698SLemover // below are dirty code for timing optimization 296f1fe8698SLemover val super_hit = Output(Bool()) 297f1fe8698SLemover val super_ppn = Output(UInt(ppnLen.W)) 298f1fe8698SLemover val spm = Output(new TlbPMBundle) 299f1fe8698SLemover })) 300f1fe8698SLemover } 301f1fe8698SLemover val w = Flipped(ValidIO(new Bundle { 302f1fe8698SLemover val data = Output(new PtwResp) 303f1fe8698SLemover val data_replenish = Output(new PMPConfig) 304f1fe8698SLemover })) 305f1fe8698SLemover val replace = if (q.outReplace) Flipped(new TlbReplaceIO(ports, q)) else null 306f1fe8698SLemover 307f1fe8698SLemover def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = { 308f1fe8698SLemover this.r.req(i).valid := valid 309f1fe8698SLemover this.r.req(i).bits.vpn := vpn 310f1fe8698SLemover } 311f1fe8698SLemover 312f1fe8698SLemover def r_resp_apply(i: Int) = { 313f1fe8698SLemover (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, 314f1fe8698SLemover this.r.resp(i).bits.super_hit, this.r.resp(i).bits.super_ppn, this.r.resp(i).bits.spm) 315f1fe8698SLemover } 316f1fe8698SLemover 317f1fe8698SLemover def w_apply(valid: Bool, data: PtwResp, data_replenish: PMPConfig): Unit = { 318f1fe8698SLemover this.w.valid := valid 319f1fe8698SLemover this.w.bits.data := data 320f1fe8698SLemover this.w.bits.data_replenish := data_replenish 321f1fe8698SLemover } 322f1fe8698SLemover} 323f1fe8698SLemover 3243889e11eSLemoverclass ReplaceAccessBundle(nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 3253889e11eSLemover val sets = Output(UInt(log2Up(nSets).W)) 3263889e11eSLemover val touch_ways = ValidIO(Output(UInt(log2Up(nWays).W))) 3273889e11eSLemover 3283889e11eSLemover} 3293889e11eSLemover 330a0301c0dSLemoverclass ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 3313889e11eSLemover val access = Vec(Width, Flipped(new ReplaceAccessBundle(nSets, nWays))) 332a0301c0dSLemover 333a0301c0dSLemover val refillIdx = Output(UInt(log2Up(nWays).W)) 334a0301c0dSLemover val chosen_set = Flipped(Output(UInt(log2Up(nSets).W))) 335a0301c0dSLemover 336a0301c0dSLemover def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = { 33753b8f1a7SLemover for ((ac_rep, ac_tlb) <- access.zip(in.map(a => a.access.map(b => b)).flatten)) { 33853b8f1a7SLemover ac_rep := ac_tlb 339a0301c0dSLemover } 34053b8f1a7SLemover this.chosen_set := get_set_idx(vpn, nSets) 34153b8f1a7SLemover in.map(a => a.refillIdx := this.refillIdx) 342a0301c0dSLemover } 343a0301c0dSLemover} 344a0301c0dSLemover 345a0301c0dSLemoverclass TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends 346a0301c0dSLemover TlbBundle { 347a0301c0dSLemover val normalPage = new ReplaceIO(Width, q.normalNSets, q.normalNWays) 348a0301c0dSLemover val superPage = new ReplaceIO(Width, q.superNSets, q.superNWays) 349a0301c0dSLemover 350a0301c0dSLemover def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = { 351a0301c0dSLemover this.normalPage.apply_sep(in.map(_.normalPage), vpn) 352a0301c0dSLemover this.superPage.apply_sep(in.map(_.superPage), vpn) 353a0301c0dSLemover } 354a0301c0dSLemover 355a0301c0dSLemover} 356a0301c0dSLemover 357*8744445eSMaxpicca-Liclass MemBlockidxBundle(implicit p: Parameters) extends TlbBundle { 358*8744445eSMaxpicca-Li val is_ld = Bool() 359*8744445eSMaxpicca-Li val is_st = Bool() 360*8744445eSMaxpicca-Li val idx = 361*8744445eSMaxpicca-Li if (LoadQueueSize >= StoreQueueSize) { 362*8744445eSMaxpicca-Li val idx = UInt(log2Ceil(LoadQueueSize).W) 363*8744445eSMaxpicca-Li idx 364*8744445eSMaxpicca-Li } else { 365*8744445eSMaxpicca-Li val idx = UInt(log2Ceil(StoreQueueSize).W) 366*8744445eSMaxpicca-Li idx 367*8744445eSMaxpicca-Li } 368*8744445eSMaxpicca-Li} 369*8744445eSMaxpicca-Li 3706d5ddbceSLemoverclass TlbReq(implicit p: Parameters) extends TlbBundle { 371ca2f90a6SLemover val vaddr = Output(UInt(VAddrBits.W)) 372ca2f90a6SLemover val cmd = Output(TlbCmd()) 373ca2f90a6SLemover val size = Output(UInt(log2Ceil(log2Ceil(XLEN/8)+1).W)) 374f1fe8698SLemover val kill = Output(Bool()) // Use for blocked tlb that need sync with other module like icache 375*8744445eSMaxpicca-Li val memidx = Output(new MemBlockidxBundle) 376b52348aeSWilliam Wang // do not translate, but still do pmp/pma check 377b52348aeSWilliam Wang val no_translate = Output(Bool()) 3786d5ddbceSLemover val debug = new Bundle { 379ca2f90a6SLemover val pc = Output(UInt(XLEN.W)) 380f1fe8698SLemover val robIdx = Output(new RobPtr) 381ca2f90a6SLemover val isFirstIssue = Output(Bool()) 3826d5ddbceSLemover } 3836d5ddbceSLemover 384f1fe8698SLemover // Maybe Block req needs a kill: for itlb, itlb and icache may not sync, itlb should wait icache to go ahead 3856d5ddbceSLemover override def toPrintable: Printable = { 386f1fe8698SLemover p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} kill:${kill} pc:0x${Hexadecimal(debug.pc)} robIdx:${debug.robIdx}" 3876d5ddbceSLemover } 3886d5ddbceSLemover} 3896d5ddbceSLemover 390b6982e83SLemoverclass TlbExceptionBundle(implicit p: Parameters) extends TlbBundle { 391b6982e83SLemover val ld = Output(Bool()) 392b6982e83SLemover val st = Output(Bool()) 393b6982e83SLemover val instr = Output(Bool()) 394b6982e83SLemover} 395b6982e83SLemover 39603efd994Shappy-lxclass TlbResp(nDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 39703efd994Shappy-lx val paddr = Vec(nDups, Output(UInt(PAddrBits.W))) 398ca2f90a6SLemover val miss = Output(Bool()) 399cccfc98dSLemover val fast_miss = Output(Bool()) // without sram part for timing optimization 40003efd994Shappy-lx val excp = Vec(nDups, new Bundle { 401b6982e83SLemover val pf = new TlbExceptionBundle() 402b6982e83SLemover val af = new TlbExceptionBundle() 40303efd994Shappy-lx }) 4045b7ef044SLemover val static_pm = Output(Valid(Bool())) // valid for static, bits for mmio result from normal entries 405ca2f90a6SLemover val ptwBack = Output(Bool()) // when ptw back, wake up replay rs's state 406*8744445eSMaxpicca-Li val memidx = Output(new MemBlockidxBundle) 4076d5ddbceSLemover 408*8744445eSMaxpicca-Li val debug = new Bundle { 409*8744445eSMaxpicca-Li val robIdx = Output(new RobPtr) 410*8744445eSMaxpicca-Li val isFirstIssue = Output(Bool()) 411*8744445eSMaxpicca-Li } 4126d5ddbceSLemover override def toPrintable: Printable = { 41303efd994Shappy-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}" 4146d5ddbceSLemover } 4156d5ddbceSLemover} 4166d5ddbceSLemover 41703efd994Shappy-lxclass TlbRequestIO(nRespDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 4186d5ddbceSLemover val req = DecoupledIO(new TlbReq) 419c3b763d0SYinan Xu val req_kill = Output(Bool()) 42003efd994Shappy-lx val resp = Flipped(DecoupledIO(new TlbResp(nRespDups))) 4216d5ddbceSLemover} 4226d5ddbceSLemover 4236d5ddbceSLemoverclass TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 4246d5ddbceSLemover val req = Vec(Width, DecoupledIO(new PtwReq)) 4256d5ddbceSLemover val resp = Flipped(DecoupledIO(new PtwResp)) 4266d5ddbceSLemover 4276d5ddbceSLemover 4286d5ddbceSLemover override def toPrintable: Printable = { 4296d5ddbceSLemover p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 4306d5ddbceSLemover } 4316d5ddbceSLemover} 4326d5ddbceSLemover 433*8744445eSMaxpicca-Liclass TlbPtwIOwithMemIdx(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 434*8744445eSMaxpicca-Li val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx)) 435*8744445eSMaxpicca-Li val resp = Flipped(DecoupledIO(new PtwRespwithMemIdx)) 436*8744445eSMaxpicca-Li 437*8744445eSMaxpicca-Li 438*8744445eSMaxpicca-Li override def toPrintable: Printable = { 439*8744445eSMaxpicca-Li p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 440*8744445eSMaxpicca-Li } 441*8744445eSMaxpicca-Li} 442*8744445eSMaxpicca-Li 44345f497a4Shappy-lxclass MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle { 444b052b972SLemover val sfence = Input(new SfenceBundle) 445b052b972SLemover val csr = Input(new TlbCsrBundle) 446f1fe8698SLemover 447f1fe8698SLemover def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 448f1fe8698SLemover this.sfence <> sfence 449f1fe8698SLemover this.csr <> csr 450f1fe8698SLemover } 451f1fe8698SLemover 452f1fe8698SLemover // overwrite satp. write satp will cause flushpipe but csr.priv won't 453f1fe8698SLemover // satp will be dealyed several cycles from writing, but csr.priv won't 454f1fe8698SLemover // so inside mmu, these two signals should be divided 455f1fe8698SLemover def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle, satp: TlbSatpBundle) = { 456f1fe8698SLemover this.sfence <> sfence 457f1fe8698SLemover this.csr <> csr 458f1fe8698SLemover this.csr.satp := satp 459f1fe8698SLemover } 460a0301c0dSLemover} 4616d5ddbceSLemover 462*8744445eSMaxpicca-Liclass TlbRefilltoMemIO()(implicit p: Parameters) extends TlbBundle { 463*8744445eSMaxpicca-Li val valid = Bool() 464*8744445eSMaxpicca-Li val memidx = new MemBlockidxBundle 465*8744445eSMaxpicca-Li} 466*8744445eSMaxpicca-Li 46703efd994Shappy-lxclass TlbIO(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends 46845f497a4Shappy-lx MMUIOBaseBundle { 46903efd994Shappy-lx val requestor = Vec(Width, Flipped(new TlbRequestIO(nRespDups))) 470f1fe8698SLemover val flushPipe = Vec(Width, Input(Bool())) 471*8744445eSMaxpicca-Li val ptw = new TlbPtwIOwithMemIdx(Width) 472*8744445eSMaxpicca-Li val refill_to_mem = Output(new TlbRefilltoMemIO()) 4735b7ef044SLemover val ptw_replenish = Input(new PMPConfig()) 474a0301c0dSLemover val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null 475b6982e83SLemover val pmp = Vec(Width, ValidIO(new PMPReqBundle())) 476a0301c0dSLemover 477a0301c0dSLemover} 478a0301c0dSLemover 479f1fe8698SLemoverclass VectorTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle { 480*8744445eSMaxpicca-Li val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx())) 481a0301c0dSLemover val resp = Flipped(DecoupledIO(new Bundle { 482*8744445eSMaxpicca-Li val data = new PtwRespwithMemIdx 483a0301c0dSLemover val vector = Output(Vec(Width, Bool())) 484a0301c0dSLemover })) 485a0301c0dSLemover 486*8744445eSMaxpicca-Li def connect(normal: TlbPtwIOwithMemIdx): Unit = { 487f1fe8698SLemover req <> normal.req 488f1fe8698SLemover resp.ready := normal.resp.ready 489f1fe8698SLemover normal.resp.bits := resp.bits.data 490f1fe8698SLemover normal.resp.valid := resp.valid 491a0301c0dSLemover } 4926d5ddbceSLemover} 4936d5ddbceSLemover 49492e3bfefSLemover/**************************** L2TLB *************************************/ 4956d5ddbceSLemoverabstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst 49692e3bfefSLemoverabstract class PtwModule(outer: L2TLB) extends LazyModuleImp(outer) 4976d5ddbceSLemover with HasXSParameter with HasPtwConst 4986d5ddbceSLemover 4996d5ddbceSLemoverclass PteBundle(implicit p: Parameters) extends PtwBundle{ 5006d5ddbceSLemover val reserved = UInt(pteResLen.W) 5010d94d540SHaoyuan Feng val ppn_high = UInt(ppnHignLen.W) 5026d5ddbceSLemover val ppn = UInt(ppnLen.W) 5036d5ddbceSLemover val rsw = UInt(2.W) 5046d5ddbceSLemover val perm = new Bundle { 5056d5ddbceSLemover val d = Bool() 5066d5ddbceSLemover val a = Bool() 5076d5ddbceSLemover val g = Bool() 5086d5ddbceSLemover val u = Bool() 5096d5ddbceSLemover val x = Bool() 5106d5ddbceSLemover val w = Bool() 5116d5ddbceSLemover val r = Bool() 5126d5ddbceSLemover val v = Bool() 5136d5ddbceSLemover } 5146d5ddbceSLemover 5156d5ddbceSLemover def unaligned(level: UInt) = { 5166d5ddbceSLemover isLeaf() && !(level === 2.U || 5176d5ddbceSLemover level === 1.U && ppn(vpnnLen-1, 0) === 0.U || 5186d5ddbceSLemover level === 0.U && ppn(vpnnLen*2-1, 0) === 0.U) 5196d5ddbceSLemover } 5206d5ddbceSLemover 5216d5ddbceSLemover def isPf(level: UInt) = { 5226d5ddbceSLemover !perm.v || (!perm.r && perm.w) || unaligned(level) 5236d5ddbceSLemover } 5246d5ddbceSLemover 5250d94d540SHaoyuan Feng // paddr of Xiangshan is 36 bits but ppn of sv39 is 44 bits 5260d94d540SHaoyuan Feng // access fault will be raised when ppn >> ppnLen is not zero 5270d94d540SHaoyuan Feng def isAf() = { 5280d94d540SHaoyuan Feng !(ppn_high === 0.U) 5290d94d540SHaoyuan Feng } 5300d94d540SHaoyuan Feng 5316d5ddbceSLemover def isLeaf() = { 5326d5ddbceSLemover perm.r || perm.x || perm.w 5336d5ddbceSLemover } 5346d5ddbceSLemover 5356d5ddbceSLemover def getPerm() = { 5366d5ddbceSLemover val pm = Wire(new PtePermBundle) 5376d5ddbceSLemover pm.d := perm.d 5386d5ddbceSLemover pm.a := perm.a 5396d5ddbceSLemover pm.g := perm.g 5406d5ddbceSLemover pm.u := perm.u 5416d5ddbceSLemover pm.x := perm.x 5426d5ddbceSLemover pm.w := perm.w 5436d5ddbceSLemover pm.r := perm.r 5446d5ddbceSLemover pm 5456d5ddbceSLemover } 5466d5ddbceSLemover 5476d5ddbceSLemover override def toPrintable: Printable = { 5486d5ddbceSLemover p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}" 5496d5ddbceSLemover } 5506d5ddbceSLemover} 5516d5ddbceSLemover 5526d5ddbceSLemoverclass PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle { 5536d5ddbceSLemover val tag = UInt(tagLen.W) 55445f497a4Shappy-lx val asid = UInt(asidLen.W) 5556d5ddbceSLemover val ppn = UInt(ppnLen.W) 5566d5ddbceSLemover val perm = if (hasPerm) Some(new PtePermBundle) else None 5576d5ddbceSLemover val level = if (hasLevel) Some(UInt(log2Up(Level).W)) else None 558bc063562SLemover val prefetch = Bool() 5598d8ac704SLemover val v = Bool() 5606d5ddbceSLemover 56156728e73SLemover def is_normalentry(): Bool = { 56256728e73SLemover if (!hasLevel) true.B 56356728e73SLemover else level.get === 2.U 56456728e73SLemover } 56556728e73SLemover 566f1fe8698SLemover def genPPN(vpn: UInt): UInt = { 567f1fe8698SLemover if (!hasLevel) ppn 568f1fe8698SLemover else MuxLookup(level.get, 0.U, Seq( 569f1fe8698SLemover 0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)), 570f1fe8698SLemover 1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)), 571f1fe8698SLemover 2.U -> ppn) 572f1fe8698SLemover ) 573f1fe8698SLemover } 574f1fe8698SLemover 57545f497a4Shappy-lx def hit(vpn: UInt, asid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false) = { 5766d5ddbceSLemover require(vpn.getWidth == vpnLen) 577cccfc98dSLemover// require(this.asid.getWidth <= asid.getWidth) 57845f497a4Shappy-lx val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 5796d5ddbceSLemover if (allType) { 5806d5ddbceSLemover require(hasLevel) 5816d5ddbceSLemover val hit0 = tag(tagLen - 1, vpnnLen*2) === vpn(tagLen - 1, vpnnLen*2) 5826d5ddbceSLemover val hit1 = tag(vpnnLen*2 - 1, vpnnLen) === vpn(vpnnLen*2 - 1, vpnnLen) 5836d5ddbceSLemover val hit2 = tag(vpnnLen - 1, 0) === vpn(vpnnLen - 1, 0) 58445f497a4Shappy-lx 58545f497a4Shappy-lx asid_hit && Mux(level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) 5866d5ddbceSLemover } else if (hasLevel) { 5876d5ddbceSLemover val hit0 = tag(tagLen - 1, tagLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen) 5886d5ddbceSLemover val hit1 = tag(tagLen - vpnnLen - 1, tagLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2) 58945f497a4Shappy-lx 59045f497a4Shappy-lx asid_hit && Mux(level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) 5916d5ddbceSLemover } else { 59245f497a4Shappy-lx asid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen) 5936d5ddbceSLemover } 5946d5ddbceSLemover } 5956d5ddbceSLemover 5968d8ac704SLemover def refill(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) { 59745f497a4Shappy-lx require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside 59845f497a4Shappy-lx 5996d5ddbceSLemover tag := vpn(vpnLen - 1, vpnLen - tagLen) 600a0301c0dSLemover ppn := pte.asTypeOf(new PteBundle().cloneType).ppn 601a0301c0dSLemover perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm) 60245f497a4Shappy-lx this.asid := asid 603bc063562SLemover this.prefetch := prefetch 6048d8ac704SLemover this.v := valid 6056d5ddbceSLemover this.level.map(_ := level) 6066d5ddbceSLemover } 6076d5ddbceSLemover 6088d8ac704SLemover def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) = { 6096d5ddbceSLemover val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel)) 6108d8ac704SLemover e.refill(vpn, asid, pte, level, prefetch, valid) 6116d5ddbceSLemover e 6126d5ddbceSLemover } 6136d5ddbceSLemover 6146d5ddbceSLemover 615f1fe8698SLemover 6166d5ddbceSLemover override def toPrintable: Printable = { 6176d5ddbceSLemover // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}" 6186d5ddbceSLemover p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} " + 6196d5ddbceSLemover (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") + 620bc063562SLemover (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") + 621bc063562SLemover p"prefetch:${prefetch}" 6226d5ddbceSLemover } 6236d5ddbceSLemover} 6246d5ddbceSLemover 6256d5ddbceSLemoverclass PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 6266d5ddbceSLemover require(log2Up(num)==log2Down(num)) 6271f4a7c0cSLemover // NOTE: hasPerm means that is leaf or not. 6286d5ddbceSLemover 6296d5ddbceSLemover val tag = UInt(tagLen.W) 63045f497a4Shappy-lx val asid = UInt(asidLen.W) 6316d5ddbceSLemover val ppns = Vec(num, UInt(ppnLen.W)) 6326d5ddbceSLemover val vs = Vec(num, Bool()) 6336d5ddbceSLemover val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None 634bc063562SLemover val prefetch = Bool() 6356d5ddbceSLemover // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1") 6361f4a7c0cSLemover // NOTE: vs is used for different usage: 6371f4a7c0cSLemover // for l3, which store the leaf(leaves), vs is page fault or not. 6381f4a7c0cSLemover // for l2, which shoule not store leaf, vs is valid or not, that will anticipate in hit check 6391f4a7c0cSLemover // Because, l2 should not store leaf(no perm), it doesn't store perm. 6401f4a7c0cSLemover // If l2 hit a leaf, the perm is still unavailble. Should still page walk. Complex but nothing helpful. 6411f4a7c0cSLemover // TODO: divide vs into validVec and pfVec 6421f4a7c0cSLemover // for l2: may valid but pf, so no need for page walk, return random pte with pf. 6436d5ddbceSLemover 6446d5ddbceSLemover def tagClip(vpn: UInt) = { 6456d5ddbceSLemover require(vpn.getWidth == vpnLen) 6466d5ddbceSLemover vpn(vpnLen - 1, vpnLen - tagLen) 6476d5ddbceSLemover } 6486d5ddbceSLemover 6496d5ddbceSLemover def sectorIdxClip(vpn: UInt, level: Int) = { 6506d5ddbceSLemover getVpnClip(vpn, level)(log2Up(num) - 1, 0) 6516d5ddbceSLemover } 6526d5ddbceSLemover 65345f497a4Shappy-lx def hit(vpn: UInt, asid: UInt, ignoreAsid: Boolean = false) = { 65445f497a4Shappy-lx val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 6551f4a7c0cSLemover asid_hit && tag === tagClip(vpn) && (if (hasPerm) true.B else vs(sectorIdxClip(vpn, level))) 6566d5ddbceSLemover } 6576d5ddbceSLemover 65845f497a4Shappy-lx def genEntries(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 6596d5ddbceSLemover require((data.getWidth / XLEN) == num, 6605854c1edSLemover s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}") 6616d5ddbceSLemover 6626d5ddbceSLemover val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm)) 6636d5ddbceSLemover ps.tag := tagClip(vpn) 66445f497a4Shappy-lx ps.asid := asid 665bc063562SLemover ps.prefetch := prefetch 6666d5ddbceSLemover for (i <- 0 until num) { 6676d5ddbceSLemover val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle) 6686d5ddbceSLemover ps.ppns(i) := pte.ppn 6696d5ddbceSLemover ps.vs(i) := !pte.isPf(levelUInt) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf()) 6706d5ddbceSLemover ps.perms.map(_(i) := pte.perm) 6716d5ddbceSLemover } 6726d5ddbceSLemover ps 6736d5ddbceSLemover } 6746d5ddbceSLemover 6756d5ddbceSLemover override def toPrintable: Printable = { 6766d5ddbceSLemover // require(num == 4, "if num is not 4, please comment this toPrintable") 6776d5ddbceSLemover // NOTE: if num is not 4, please comment this toPrintable 6786d5ddbceSLemover val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle))) 67945f497a4Shappy-lx p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " + 6806d5ddbceSLemover (if (hasPerm) p"perms:${printVec(permsInner)}" else p"") 6816d5ddbceSLemover } 6826d5ddbceSLemover} 6836d5ddbceSLemover 6847196f5a2SLemoverclass PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 6857196f5a2SLemover val entries = new PtwEntries(num, tagLen, level, hasPerm) 6867196f5a2SLemover 6873889e11eSLemover val ecc_block = XLEN 6883889e11eSLemover val ecc_info = get_ecc_info() 6893889e11eSLemover val ecc = UInt(ecc_info._1.W) 6903889e11eSLemover 6913889e11eSLemover def get_ecc_info(): (Int, Int, Int, Int) = { 6923889e11eSLemover val eccBits_per = eccCode.width(ecc_block) - ecc_block 6933889e11eSLemover 6943889e11eSLemover val data_length = entries.getWidth 6953889e11eSLemover val data_align_num = data_length / ecc_block 6963889e11eSLemover val data_not_align = (data_length % ecc_block) != 0 // ugly code 6973889e11eSLemover val data_unalign_length = data_length - data_align_num * ecc_block 6983889e11eSLemover val eccBits_unalign = eccCode.width(data_unalign_length) - data_unalign_length 6993889e11eSLemover 7003889e11eSLemover val eccBits = eccBits_per * data_align_num + eccBits_unalign 7013889e11eSLemover (eccBits, eccBits_per, data_align_num, data_unalign_length) 7023889e11eSLemover } 7033889e11eSLemover 7043889e11eSLemover def encode() = { 7053889e11eSLemover val data = entries.asUInt() 7063889e11eSLemover val ecc_slices = Wire(Vec(ecc_info._3, UInt(ecc_info._2.W))) 7073889e11eSLemover for (i <- 0 until ecc_info._3) { 7083889e11eSLemover ecc_slices(i) := eccCode.encode(data((i+1)*ecc_block-1, i*ecc_block)) >> ecc_block 7093889e11eSLemover } 7103889e11eSLemover if (ecc_info._4 != 0) { 7113889e11eSLemover val ecc_unaligned = eccCode.encode(data(data.getWidth-1, ecc_info._3*ecc_block)) >> ecc_info._4 7123889e11eSLemover ecc := Cat(ecc_unaligned, ecc_slices.asUInt()) 7133889e11eSLemover } else { ecc := ecc_slices.asUInt() } 7143889e11eSLemover } 7153889e11eSLemover 7163889e11eSLemover def decode(): Bool = { 7173889e11eSLemover val data = entries.asUInt() 7183889e11eSLemover val res = Wire(Vec(ecc_info._3 + 1, Bool())) 7193889e11eSLemover for (i <- 0 until ecc_info._3) { 7205197bac8SZiyue-Zhang res(i) := {if (ecc_info._2 != 0) eccCode.decode(Cat(ecc((i+1)*ecc_info._2-1, i*ecc_info._2), data((i+1)*ecc_block-1, i*ecc_block))).error else false.B} 7213889e11eSLemover } 7225197bac8SZiyue-Zhang if (ecc_info._2 != 0 && ecc_info._4 != 0) { 7233889e11eSLemover res(ecc_info._3) := eccCode.decode( 7243889e11eSLemover Cat(ecc(ecc_info._1-1, ecc_info._2*ecc_info._3), data(data.getWidth-1, ecc_info._3*ecc_block))).error 7253889e11eSLemover } else { res(ecc_info._3) := false.B } 7263889e11eSLemover 7273889e11eSLemover Cat(res).orR 7283889e11eSLemover } 7293889e11eSLemover 7303889e11eSLemover def gen(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 7313889e11eSLemover this.entries := entries.genEntries(vpn, asid, data, levelUInt, prefetch) 7323889e11eSLemover this.encode() 7333889e11eSLemover } 7347196f5a2SLemover} 7357196f5a2SLemover 7366d5ddbceSLemoverclass PtwReq(implicit p: Parameters) extends PtwBundle { 7376d5ddbceSLemover val vpn = UInt(vpnLen.W) 7386d5ddbceSLemover 7396d5ddbceSLemover override def toPrintable: Printable = { 7406d5ddbceSLemover p"vpn:0x${Hexadecimal(vpn)}" 7416d5ddbceSLemover } 7426d5ddbceSLemover} 7436d5ddbceSLemover 744*8744445eSMaxpicca-Liclass PtwReqwithMemIdx(implicit p: Parameters) extends PtwReq { 745*8744445eSMaxpicca-Li val memidx = new MemBlockidxBundle 746*8744445eSMaxpicca-Li} 747*8744445eSMaxpicca-Li 7486d5ddbceSLemoverclass PtwResp(implicit p: Parameters) extends PtwBundle { 7496d5ddbceSLemover val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true) 7506d5ddbceSLemover val pf = Bool() 751b6982e83SLemover val af = Bool() 7526d5ddbceSLemover 75345f497a4Shappy-lx def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = { 7545854c1edSLemover this.entry.level.map(_ := level) 7555854c1edSLemover this.entry.tag := vpn 7565854c1edSLemover this.entry.perm.map(_ := pte.getPerm()) 7575854c1edSLemover this.entry.ppn := pte.ppn 758bc063562SLemover this.entry.prefetch := DontCare 75945f497a4Shappy-lx this.entry.asid := asid 7608d8ac704SLemover this.entry.v := !pf 7615854c1edSLemover this.pf := pf 762b6982e83SLemover this.af := af 7635854c1edSLemover } 7645854c1edSLemover 7656d5ddbceSLemover override def toPrintable: Printable = { 766b6982e83SLemover p"entry:${entry} pf:${pf} af:${af}" 7676d5ddbceSLemover } 7686d5ddbceSLemover} 7696d5ddbceSLemover 770*8744445eSMaxpicca-Liclass PtwRespwithMemIdx(implicit p: Parameters) extends PtwResp { 771*8744445eSMaxpicca-Li val memidx = new MemBlockidxBundle 772*8744445eSMaxpicca-Li} 773*8744445eSMaxpicca-Li 774*8744445eSMaxpicca-Li 77592e3bfefSLemoverclass L2TLBIO(implicit p: Parameters) extends PtwBundle { 7766d5ddbceSLemover val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO)) 7776d5ddbceSLemover val sfence = Input(new SfenceBundle) 778b6982e83SLemover val csr = new Bundle { 779b6982e83SLemover val tlb = Input(new TlbCsrBundle) 780b6982e83SLemover val distribute_csr = Flipped(new DistributedCSRIO) 781b6982e83SLemover } 7826d5ddbceSLemover} 7836d5ddbceSLemover 784b848eea5SLemoverclass L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle { 785b848eea5SLemover val addr = UInt(PAddrBits.W) 786b848eea5SLemover val id = UInt(bMemID.W) 787b848eea5SLemover} 78845f497a4Shappy-lx 78945f497a4Shappy-lxclass L2TlbInnerBundle(implicit p: Parameters) extends PtwReq { 79045f497a4Shappy-lx val source = UInt(bSourceWidth.W) 79145f497a4Shappy-lx} 792f1fe8698SLemover 793f1fe8698SLemover 794f1fe8698SLemoverobject ValidHoldBypass{ 795f1fe8698SLemover def apply(infire: Bool, outfire: Bool, flush: Bool = false.B) = { 796f1fe8698SLemover val valid = RegInit(false.B) 797f1fe8698SLemover when (infire) { valid := true.B } 798f1fe8698SLemover when (outfire) { valid := false.B } // ATTENTION: order different with ValidHold 799f1fe8698SLemover when (flush) { valid := false.B } // NOTE: the flush will flush in & out, is that ok? 800f1fe8698SLemover valid || infire 801f1fe8698SLemover } 802f1fe8698SLemover} 8035afdf73cSHaoyuan Feng 8045afdf73cSHaoyuan Fengclass L1TlbDB(implicit p: Parameters) extends TlbBundle { 8055afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 8065afdf73cSHaoyuan Feng} 8075afdf73cSHaoyuan Feng 8085afdf73cSHaoyuan Fengclass PageCacheDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 8095afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 8105afdf73cSHaoyuan Feng val source = UInt(bSourceWidth.W) 8115afdf73cSHaoyuan Feng val bypassed = Bool() 8125afdf73cSHaoyuan Feng val is_first = Bool() 8135afdf73cSHaoyuan Feng val prefetched = Bool() 8145afdf73cSHaoyuan Feng val prefetch = Bool() 8155afdf73cSHaoyuan Feng val l2Hit = Bool() 8165afdf73cSHaoyuan Feng val l1Hit = Bool() 8175afdf73cSHaoyuan Feng val hit = Bool() 8185afdf73cSHaoyuan Feng} 8195afdf73cSHaoyuan Feng 8205afdf73cSHaoyuan Fengclass PTWDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 8215afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 8225afdf73cSHaoyuan Feng val source = UInt(bSourceWidth.W) 8235afdf73cSHaoyuan Feng} 8245afdf73cSHaoyuan Feng 8255afdf73cSHaoyuan Fengclass L2TlbPrefetchDB(implicit p: Parameters) extends TlbBundle { 8265afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 8275afdf73cSHaoyuan Feng} 8285afdf73cSHaoyuan Feng 8295afdf73cSHaoyuan Fengclass L2TlbMissQueueDB(implicit p: Parameters) extends TlbBundle { 8305afdf73cSHaoyuan Feng val vpn = UInt(vpnLen.W) 8315afdf73cSHaoyuan Feng} 832