1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.cache.mmu 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import utils._ 24import utility._ 25import xiangshan.backend.rob.RobPtr 26import xiangshan.backend.fu.util.HasCSRConst 27import freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 28import freechips.rocketchip.tilelink._ 29import xiangshan.backend.fu.{PMPReqBundle, PMPConfig} 30import xiangshan.backend.fu.PMPBundle 31 32 33abstract class TlbBundle(implicit p: Parameters) extends XSBundle with HasTlbConst 34abstract class TlbModule(implicit p: Parameters) extends XSModule with HasTlbConst 35 36class VaBundle(implicit p: Parameters) extends TlbBundle { 37 val vpn = UInt(vpnLen.W) 38 val off = UInt(offLen.W) 39} 40 41class PtePermBundle(implicit p: Parameters) extends TlbBundle { 42 val d = Bool() 43 val a = Bool() 44 val g = Bool() 45 val u = Bool() 46 val x = Bool() 47 val w = Bool() 48 val r = Bool() 49 50 override def toPrintable: Printable = { 51 p"d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"// + 52 //(if(hasV) (p"v:${v}") else p"") 53 } 54} 55 56class TlbPMBundle(implicit p: Parameters) extends TlbBundle { 57 val r = Bool() 58 val w = Bool() 59 val x = Bool() 60 val c = Bool() 61 val atomic = Bool() 62 63 def assign_ap(pm: PMPConfig) = { 64 r := pm.r 65 w := pm.w 66 x := pm.x 67 c := pm.c 68 atomic := pm.atomic 69 } 70} 71 72class TlbPermBundle(implicit p: Parameters) extends TlbBundle { 73 val pf = Bool() // NOTE: if this is true, just raise pf 74 val af = Bool() // NOTE: if this is true, just raise af 75 // pagetable perm (software defined) 76 val d = Bool() 77 val a = Bool() 78 val g = Bool() 79 val u = Bool() 80 val x = Bool() 81 val w = Bool() 82 val r = Bool() 83 84 val pm = new TlbPMBundle 85 86 def apply(item: PtwSectorResp, pm: PMPConfig) = { 87 val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 88 this.pf := item.pf 89 this.af := item.af 90 this.d := ptePerm.d 91 this.a := ptePerm.a 92 this.g := ptePerm.g 93 this.u := ptePerm.u 94 this.x := ptePerm.x 95 this.w := ptePerm.w 96 this.r := ptePerm.r 97 98 this.pm.assign_ap(pm) 99 this 100 } 101 override def toPrintable: Printable = { 102 p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " + 103 p"pm:${pm}" 104 } 105} 106 107class TlbSectorPermBundle(implicit p: Parameters) extends TlbBundle { 108 val pf = Bool() // NOTE: if this is true, just raise pf 109 val af = Bool() // NOTE: if this is true, just raise af 110 // pagetable perm (software defined) 111 val d = Bool() 112 val a = Bool() 113 val g = Bool() 114 val u = Bool() 115 val x = Bool() 116 val w = Bool() 117 val r = Bool() 118 119 // static pmp & pma check has a minimum grain size of 4K 120 // So sector tlb will use eight static pm entries 121 val pm = Vec(tlbcontiguous, new TlbPMBundle) 122 123 def apply(item: PtwSectorResp, pm: Seq[PMPConfig]) = { 124 val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 125 this.pf := item.pf 126 this.af := item.af 127 this.d := ptePerm.d 128 this.a := ptePerm.a 129 this.g := ptePerm.g 130 this.u := ptePerm.u 131 this.x := ptePerm.x 132 this.w := ptePerm.w 133 this.r := ptePerm.r 134 135 for (i <- 0 until tlbcontiguous) { 136 this.pm(i).assign_ap(pm(i)) 137 } 138 this 139 } 140 override def toPrintable: Printable = { 141 p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " + 142 p"pm:${pm}" 143 } 144} 145 146// multi-read && single-write 147// input is data, output is hot-code(not one-hot) 148class CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule { 149 val io = IO(new Bundle { 150 val r = new Bundle { 151 val req = Input(Vec(readWidth, gen)) 152 val resp = Output(Vec(readWidth, Vec(set, Bool()))) 153 } 154 val w = Input(new Bundle { 155 val valid = Bool() 156 val bits = new Bundle { 157 val index = UInt(log2Up(set).W) 158 val data = gen 159 } 160 }) 161 }) 162 163 val wordType = UInt(gen.getWidth.W) 164 val array = Reg(Vec(set, wordType)) 165 166 io.r.resp.zipWithIndex.map{ case (a,i) => 167 a := array.map(io.r.req(i).asUInt === _) 168 } 169 170 when (io.w.valid) { 171 array(io.w.bits.index) := io.w.bits.data.asUInt 172 } 173} 174 175class TlbEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle { 176 require(pageNormal || pageSuper) 177 178 val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W) 179 else UInt(vpnLen.W) 180 val asid = UInt(asidLen.W) 181 val level = if (!pageNormal) Some(UInt(1.W)) 182 else if (!pageSuper) None 183 else Some(UInt(2.W)) 184 val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W) 185 else UInt(ppnLen.W) 186 val perm = new TlbPermBundle 187 188 /** level usage: 189 * !PageSuper: page is only normal, level is None, match all the tag 190 * !PageNormal: page is only super, level is a Bool(), match high 9*2 parts 191 * bits0 0: need mid 9bits 192 * 1: no need mid 9bits 193 * PageSuper && PageNormal: page hold all the three type, 194 * bits0 0: need low 9bits 195 * bits1 0: need mid 9bits 196 */ 197 198 def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false): Bool = { 199 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 200 201 // NOTE: for timing, dont care low set index bits at hit check 202 // do not need store the low bits actually 203 if (!pageSuper) asid_hit && drop_set_equal(vpn, tag, nSets) 204 else if (!pageNormal) { 205 val tag_match_hi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*3-1, vpnnLen*2) 206 val tag_match_mi = tag(vpnnLen-1, 0) === vpn(vpnnLen*2-1, vpnnLen) 207 val tag_match = tag_match_hi && (level.get.asBool() || tag_match_mi) 208 asid_hit && tag_match 209 } 210 else { 211 val tmp_level = level.get 212 val tag_match_hi = tag(vpnnLen*3-1, vpnnLen*2) === vpn(vpnnLen*3-1, vpnnLen*2) 213 val tag_match_mi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*2-1, vpnnLen) 214 val tag_match_lo = tag(vpnnLen-1, 0) === vpn(vpnnLen-1, 0) // if pageNormal is false, this will always be false 215 val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 216 asid_hit && tag_match 217 } 218 } 219 220 def apply(item: PtwSectorResp, asid: UInt, pm: PMPConfig): TlbEntry = { 221 this.tag := {if (pageNormal) Cat(item.entry.tag, OHToUInt(item.pteidx)) else item.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth)} 222 this.asid := asid 223 val inner_level = item.entry.level.getOrElse(0.U) 224 this.level.map(_ := { if (pageNormal && pageSuper) MuxLookup(inner_level, 0.U, Seq( 225 0.U -> 3.U, 226 1.U -> 1.U, 227 2.U -> 0.U )) 228 else if (pageSuper) ~inner_level(0) 229 else 0.U }) 230 this.ppn := { if (!pageNormal) item.entry.ppn(sectorppnLen - 1, vpnnLen - sectortlbwidth) 231 else Cat(item.entry.ppn, item.ppn_low(OHToUInt(item.pteidx))) } 232 this.perm.apply(item, pm) 233 this 234 } 235 236 // 4KB is normal entry, 2MB/1GB is considered as super entry 237 def is_normalentry(): Bool = { 238 if (!pageSuper) { true.B } 239 else if (!pageNormal) { false.B } 240 else { level.get === 0.U } 241 } 242 243 def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = { 244 val inner_level = level.getOrElse(0.U) 245 val ppn_res = if (!pageSuper) ppn 246 else if (!pageNormal) Cat(ppn(ppnLen-vpnnLen-1, vpnnLen), 247 Mux(inner_level(0), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen-1,0)), 248 vpn(vpnnLen-1, 0)) 249 else Cat(ppn(ppnLen-1, vpnnLen*2), 250 Mux(inner_level(1), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen*2-1, vpnnLen)), 251 Mux(inner_level(0), vpn(vpnnLen-1, 0), ppn(vpnnLen-1, 0))) 252 253 if (saveLevel) Cat(ppn(ppn.getWidth-1, vpnnLen*2), RegEnable(ppn_res(vpnnLen*2-1, 0), valid)) 254 else ppn_res 255 } 256 257 override def toPrintable: Printable = { 258 val inner_level = level.getOrElse(2.U) 259 p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}" 260 } 261 262} 263 264class TlbSectorEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle { 265 require(pageNormal || pageSuper) 266 267 val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W) 268 else UInt(sectorvpnLen.W) 269 val asid = UInt(asidLen.W) 270 val level = if (!pageNormal) Some(UInt(1.W)) 271 else if (!pageSuper) None 272 else Some(UInt(2.W)) 273 val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W) 274 else UInt(sectorppnLen.W) 275 val perm = new TlbSectorPermBundle 276 val valididx = Vec(tlbcontiguous, Bool()) 277 val pteidx = Vec(tlbcontiguous, Bool()) 278 val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W)) 279 280 /** level usage: 281 * !PageSuper: page is only normal, level is None, match all the tag 282 * !PageNormal: page is only super, level is a Bool(), match high 9*2 parts 283 * bits0 0: need mid 9bits 284 * 1: no need mid 9bits 285 * PageSuper && PageNormal: page hold all the three type, 286 * bits0 0: need low 9bits 287 * bits1 0: need mid 9bits 288 */ 289 290 def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false): Bool = { 291 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 292 val addr_low_hit = valididx(vpn(2, 0)) 293 294 // NOTE: for timing, dont care low set index bits at hit check 295 // do not need store the low bits actually 296 if (!pageSuper) asid_hit && drop_set_equal(vpn(vpn.getWidth - 1, sectortlbwidth), tag, nSets) && addr_low_hit 297 else if (!pageNormal) { 298 val tag_match_hi = tag(vpnnLen * 2 - 1, vpnnLen) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 299 val tag_match_mi = tag(vpnnLen - 1, 0) === vpn(vpnnLen * 2 - 1, vpnnLen) 300 val tag_match = tag_match_hi && (level.get.asBool() || tag_match_mi) 301 asid_hit && tag_match && addr_low_hit 302 } 303 else { 304 val tmp_level = level.get 305 val tag_match_hi = tag(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 306 val tag_match_mi = tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen) 307 val tag_match_lo = tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) // if pageNormal is false, this will always be false 308 val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 309 asid_hit && tag_match && addr_low_hit 310 } 311 } 312 313 def wbhit(data: PtwSectorResp, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false): Bool = { 314 val vpn = Cat(data.entry.tag, 0.U(sectortlbwidth.W)) 315 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 316 val vpn_hit = Wire(Bool()) 317 val index_hit = Wire(Vec(tlbcontiguous, Bool())) 318 319 // NOTE: for timing, dont care low set index bits at hit check 320 // do not need store the low bits actually 321 if (!pageSuper) { 322 vpn_hit := asid_hit && drop_set_equal(vpn(vpn.getWidth - 1, sectortlbwidth), tag, nSets) 323 } 324 else if (!pageNormal) { 325 val tag_match_hi = tag(vpnnLen * 2 - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 326 val tag_match_mi = tag(vpnnLen - 1, 0) === vpn(vpnnLen * 2 - 1, vpnnLen) 327 val tag_match = tag_match_hi && (level.get.asBool() || tag_match_mi) 328 vpn_hit := asid_hit && tag_match 329 } 330 else { 331 val tmp_level = level.get 332 val tag_match_hi = tag(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 333 val tag_match_mi = tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen) 334 val tag_match_lo = tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) // if pageNormal is false, this will always be false 335 val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 336 vpn_hit := asid_hit && tag_match 337 } 338 339 for (i <- 0 until tlbcontiguous) { 340 index_hit(i) := data.valididx(i) && valididx(i) 341 } 342 343 // For example, tlb req to page cache with vpn 0x10 344 // At this time, 0x13 has not been paged, so page cache only resp 0x10 345 // When 0x13 refill to page cache, previous item will be flushed 346 // Now 0x10 and 0x13 are both valid in page cache 347 // However, when 0x13 refill to tlb, will trigger multi hit 348 // So will only trigger multi-hit when PopCount(data.valididx) = 1 349 vpn_hit && index_hit.reduce(_ || _) && PopCount(data.valididx) === 1.U 350 } 351 352 def apply(item: PtwSectorResp, asid: UInt, pm: Seq[PMPConfig]): TlbSectorEntry = { 353 this.tag := {if (pageNormal) item.entry.tag else item.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth)} 354 this.asid := asid 355 val inner_level = item.entry.level.getOrElse(0.U) 356 this.level.map(_ := { if (pageNormal && pageSuper) MuxLookup(inner_level, 0.U, Seq( 357 0.U -> 3.U, 358 1.U -> 1.U, 359 2.U -> 0.U )) 360 else if (pageSuper) ~inner_level(0) 361 else 0.U }) 362 this.ppn := { if (!pageNormal) item.entry.ppn(sectorppnLen - 1, vpnnLen - sectortlbwidth) 363 else item.entry.ppn } 364 this.perm.apply(item, pm) 365 this.ppn_low := item.ppn_low 366 this.valididx := item.valididx 367 this.pteidx := item.pteidx 368 this 369 } 370 371 // 4KB is normal entry, 2MB/1GB is considered as super entry 372 def is_normalentry(): Bool = { 373 if (!pageSuper) { true.B } 374 else if (!pageNormal) { false.B } 375 else { level.get === 0.U } 376 } 377 378 def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = { 379 val inner_level = level.getOrElse(0.U) 380 val ppn_res = if (!pageSuper) Cat(ppn, ppn_low(vpn(sectortlbwidth - 1, 0))) 381 else if (!pageNormal) Cat(ppn(ppnLen - vpnnLen - 1, vpnnLen), 382 Mux(inner_level(0), vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen - 1,0)), 383 vpn(vpnnLen - 1, 0)) 384 else Cat(ppn(sectorppnLen - 1, vpnnLen * 2 - sectortlbwidth), 385 Mux(inner_level(1), vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth)), 386 Mux(inner_level(0), vpn(vpnnLen - 1, 0), Cat(ppn(vpnnLen - sectortlbwidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0))))) 387 388 if (saveLevel) { 389 if (ppn.getWidth == ppnLen - vpnnLen) { 390 Cat(ppn(ppn.getWidth - 1, vpnnLen * 2), RegEnable(ppn_res(vpnnLen * 2 - 1, 0), valid)) 391 } else { 392 require(ppn.getWidth == sectorppnLen) 393 Cat(ppn(ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), RegEnable(ppn_res(vpnnLen * 2 - 1, 0), valid)) 394 } 395 } 396 else ppn_res 397 } 398 399 override def toPrintable: Printable = { 400 val inner_level = level.getOrElse(2.U) 401 p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}" 402 } 403 404} 405 406object TlbCmd { 407 def read = "b00".U 408 def write = "b01".U 409 def exec = "b10".U 410 411 def atom_read = "b100".U // lr 412 def atom_write = "b101".U // sc / amo 413 414 def apply() = UInt(3.W) 415 def isRead(a: UInt) = a(1,0)===read 416 def isWrite(a: UInt) = a(1,0)===write 417 def isExec(a: UInt) = a(1,0)===exec 418 419 def isAtom(a: UInt) = a(2) 420 def isAmo(a: UInt) = a===atom_write // NOTE: sc mixed 421} 422 423class TlbStorageIO(nSets: Int, nWays: Int, ports: Int, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 424 val r = new Bundle { 425 val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 426 val vpn = Output(UInt(vpnLen.W)) 427 }))) 428 val resp = Vec(ports, ValidIO(new Bundle{ 429 val hit = Output(Bool()) 430 val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 431 val perm = Vec(nDups, Output(new TlbSectorPermBundle())) 432 })) 433 } 434 val w = Flipped(ValidIO(new Bundle { 435 val wayIdx = Output(UInt(log2Up(nWays).W)) 436 val data = Output(new PtwSectorResp) 437 val data_replenish = Vec(tlbcontiguous, Output(new PMPConfig)) 438 })) 439 val victim = new Bundle { 440 val out = ValidIO(Output(new Bundle { 441 val entry = new TlbEntry(pageNormal = true, pageSuper = false) 442 })) 443 val in = Flipped(ValidIO(Output(new Bundle { 444 val entry = new TlbEntry(pageNormal = true, pageSuper = false) 445 }))) 446 } 447 val access = Vec(ports, new ReplaceAccessBundle(nSets, nWays)) 448 449 def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = { 450 this.r.req(i).valid := valid 451 this.r.req(i).bits.vpn := vpn 452 } 453 454 def r_resp_apply(i: Int) = { 455 (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm) 456 } 457 458 def w_apply(valid: Bool, wayIdx: UInt, data: PtwSectorResp, data_replenish: Seq[PMPConfig]): Unit = { 459 this.w.valid := valid 460 this.w.bits.wayIdx := wayIdx 461 this.w.bits.data := data 462 this.w.bits.data_replenish := data_replenish 463 } 464 465} 466 467class TlbStorageWrapperIO(ports: Int, q: TLBParameters, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 468 val r = new Bundle { 469 val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 470 val vpn = Output(UInt(vpnLen.W)) 471 }))) 472 val resp = Vec(ports, ValidIO(new Bundle{ 473 val hit = Output(Bool()) 474 val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 475 val perm = Vec(nDups, Output(new TlbPermBundle())) 476 // below are dirty code for timing optimization 477 val super_hit = Output(Bool()) 478 val super_ppn = Output(UInt(ppnLen.W)) 479 val spm = Output(new TlbPMBundle) 480 })) 481 } 482 val w = Flipped(ValidIO(new Bundle { 483 val data = Output(new PtwSectorResp) 484 val data_replenish = Vec(tlbcontiguous, Output(new PMPConfig)) 485 })) 486 val replace = if (q.outReplace) Flipped(new TlbReplaceIO(ports, q)) else null 487 488 def r_req_apply(valid: Bool, vpn: UInt, i: Int): Unit = { 489 this.r.req(i).valid := valid 490 this.r.req(i).bits.vpn := vpn 491 } 492 493 def r_resp_apply(i: Int) = { 494 (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, 495 this.r.resp(i).bits.super_hit, this.r.resp(i).bits.super_ppn, this.r.resp(i).bits.spm) 496 } 497 498 def w_apply(valid: Bool, data: PtwSectorResp, data_replenish: Seq[PMPConfig]): Unit = { 499 this.w.valid := valid 500 this.w.bits.data := data 501 this.w.bits.data_replenish := data_replenish 502 } 503} 504 505class ReplaceAccessBundle(nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 506 val sets = Output(UInt(log2Up(nSets).W)) 507 val touch_ways = ValidIO(Output(UInt(log2Up(nWays).W))) 508} 509 510class ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 511 val access = Vec(Width, Flipped(new ReplaceAccessBundle(nSets, nWays))) 512 513 val refillIdx = Output(UInt(log2Up(nWays).W)) 514 val chosen_set = Flipped(Output(UInt(log2Up(nSets).W))) 515 516 def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = { 517 for ((ac_rep, ac_tlb) <- access.zip(in.map(a => a.access.map(b => b)).flatten)) { 518 ac_rep := ac_tlb 519 } 520 this.chosen_set := get_set_idx(vpn, nSets) 521 in.map(a => a.refillIdx := this.refillIdx) 522 } 523} 524 525class TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends 526 TlbBundle { 527 val normalPage = new ReplaceIO(Width, q.normalNSets, q.normalNWays) 528 val superPage = new ReplaceIO(Width, q.superNSets, q.superNWays) 529 530 def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = { 531 this.normalPage.apply_sep(in.map(_.normalPage), vpn) 532 this.superPage.apply_sep(in.map(_.superPage), vpn) 533 } 534 535} 536 537class MemBlockidxBundle(implicit p: Parameters) extends TlbBundle { 538 val is_ld = Bool() 539 val is_st = Bool() 540 val idx = 541 if (VirtualLoadQueueSize >= StoreQueueSize) { 542 val idx = UInt(log2Ceil(VirtualLoadQueueSize).W) 543 idx 544 } else { 545 val idx = UInt(log2Ceil(StoreQueueSize).W) 546 idx 547 } 548} 549 550class TlbReq(implicit p: Parameters) extends TlbBundle { 551 val vaddr = Output(UInt(VAddrBits.W)) 552 val cmd = Output(TlbCmd()) 553 val size = Output(UInt(log2Ceil(log2Ceil(XLEN/8)+1).W)) 554 val kill = Output(Bool()) // Use for blocked tlb that need sync with other module like icache 555 val memidx = Output(new MemBlockidxBundle) 556 // do not translate, but still do pmp/pma check 557 val no_translate = Output(Bool()) 558 val debug = new Bundle { 559 val pc = Output(UInt(XLEN.W)) 560 val robIdx = Output(new RobPtr) 561 val isFirstIssue = Output(Bool()) 562 } 563 564 // Maybe Block req needs a kill: for itlb, itlb and icache may not sync, itlb should wait icache to go ahead 565 override def toPrintable: Printable = { 566 p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} kill:${kill} pc:0x${Hexadecimal(debug.pc)} robIdx:${debug.robIdx}" 567 } 568} 569 570class TlbExceptionBundle(implicit p: Parameters) extends TlbBundle { 571 val ld = Output(Bool()) 572 val st = Output(Bool()) 573 val instr = Output(Bool()) 574} 575 576class TlbResp(nDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 577 val paddr = Vec(nDups, Output(UInt(PAddrBits.W))) 578 val miss = Output(Bool()) 579 val fast_miss = Output(Bool()) // without sram part for timing optimization 580 val excp = Vec(nDups, new Bundle { 581 val pf = new TlbExceptionBundle() 582 val af = new TlbExceptionBundle() 583 }) 584 val static_pm = Output(Valid(Bool())) // valid for static, bits for mmio result from normal entries 585 val ptwBack = Output(Bool()) // when ptw back, wake up replay rs's state 586 val memidx = Output(new MemBlockidxBundle) 587 588 val debug = new Bundle { 589 val robIdx = Output(new RobPtr) 590 val isFirstIssue = Output(Bool()) 591 } 592 override def toPrintable: Printable = { 593 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}" 594 } 595} 596 597class TlbRequestIO(nRespDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 598 val req = DecoupledIO(new TlbReq) 599 val req_kill = Output(Bool()) 600 val resp = Flipped(DecoupledIO(new TlbResp(nRespDups))) 601} 602 603class TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 604 val req = Vec(Width, DecoupledIO(new PtwReq)) 605 val resp = Flipped(DecoupledIO(new PtwSectorResp)) 606 607 608 override def toPrintable: Printable = { 609 p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 610 } 611} 612 613class TlbPtwIOwithMemIdx(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 614 val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx)) 615 val resp = Flipped(DecoupledIO(new PtwSectorRespwithMemIdx)) 616 617 618 override def toPrintable: Printable = { 619 p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 620 } 621} 622 623class MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle { 624 val sfence = Input(new SfenceBundle) 625 val csr = Input(new TlbCsrBundle) 626 627 def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 628 this.sfence <> sfence 629 this.csr <> csr 630 } 631 632 // overwrite satp. write satp will cause flushpipe but csr.priv won't 633 // satp will be dealyed several cycles from writing, but csr.priv won't 634 // so inside mmu, these two signals should be divided 635 def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle, satp: TlbSatpBundle) = { 636 this.sfence <> sfence 637 this.csr <> csr 638 this.csr.satp := satp 639 } 640} 641 642class TlbRefilltoMemIO()(implicit p: Parameters) extends TlbBundle { 643 val valid = Bool() 644 val memidx = new MemBlockidxBundle 645} 646 647class TlbIO(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends 648 MMUIOBaseBundle { 649 val requestor = Vec(Width, Flipped(new TlbRequestIO(nRespDups))) 650 val flushPipe = Vec(Width, Input(Bool())) 651 val ptw = new TlbPtwIOwithMemIdx(Width) 652 val refill_to_mem = Output(new TlbRefilltoMemIO()) 653 val ptw_replenish = Vec(tlbcontiguous, Input(new PMPConfig())) 654 val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null 655 val pmp = Vec(Width, ValidIO(new PMPReqBundle())) 656 657} 658 659class VectorTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle { 660 val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx())) 661 val resp = Flipped(DecoupledIO(new Bundle { 662 val data = new PtwSectorRespwithMemIdx 663 val vector = Output(Vec(Width, Bool())) 664 })) 665 666 def connect(normal: TlbPtwIOwithMemIdx): Unit = { 667 req <> normal.req 668 resp.ready := normal.resp.ready 669 normal.resp.bits := resp.bits.data 670 normal.resp.valid := resp.valid 671 } 672} 673 674/**************************** L2TLB *************************************/ 675abstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst 676abstract class PtwModule(outer: L2TLB) extends LazyModuleImp(outer) 677 with HasXSParameter with HasPtwConst 678 679class PteBundle(implicit p: Parameters) extends PtwBundle{ 680 val reserved = UInt(pteResLen.W) 681 val ppn_high = UInt(ppnHignLen.W) 682 val ppn = UInt(ppnLen.W) 683 val rsw = UInt(2.W) 684 val perm = new Bundle { 685 val d = Bool() 686 val a = Bool() 687 val g = Bool() 688 val u = Bool() 689 val x = Bool() 690 val w = Bool() 691 val r = Bool() 692 val v = Bool() 693 } 694 695 def unaligned(level: UInt) = { 696 isLeaf() && !(level === 2.U || 697 level === 1.U && ppn(vpnnLen-1, 0) === 0.U || 698 level === 0.U && ppn(vpnnLen*2-1, 0) === 0.U) 699 } 700 701 def isPf(level: UInt) = { 702 !perm.v || (!perm.r && perm.w) || unaligned(level) 703 } 704 705 // paddr of Xiangshan is 36 bits but ppn of sv39 is 44 bits 706 // access fault will be raised when ppn >> ppnLen is not zero 707 def isAf() = { 708 !(ppn_high === 0.U) 709 } 710 711 def isLeaf() = { 712 perm.r || perm.x || perm.w 713 } 714 715 def getPerm() = { 716 val pm = Wire(new PtePermBundle) 717 pm.d := perm.d 718 pm.a := perm.a 719 pm.g := perm.g 720 pm.u := perm.u 721 pm.x := perm.x 722 pm.w := perm.w 723 pm.r := perm.r 724 pm 725 } 726 727 override def toPrintable: Printable = { 728 p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}" 729 } 730} 731 732class PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle { 733 val tag = UInt(tagLen.W) 734 val asid = UInt(asidLen.W) 735 val ppn = UInt(ppnLen.W) 736 val perm = if (hasPerm) Some(new PtePermBundle) else None 737 val level = if (hasLevel) Some(UInt(log2Up(Level).W)) else None 738 val prefetch = Bool() 739 val v = Bool() 740 741 def is_normalentry(): Bool = { 742 if (!hasLevel) true.B 743 else level.get === 2.U 744 } 745 746 def genPPN(vpn: UInt): UInt = { 747 if (!hasLevel) ppn 748 else MuxLookup(level.get, 0.U, Seq( 749 0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)), 750 1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)), 751 2.U -> ppn) 752 ) 753 } 754 755 def hit(vpn: UInt, asid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false) = { 756 require(vpn.getWidth == vpnLen) 757// require(this.asid.getWidth <= asid.getWidth) 758 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 759 if (allType) { 760 require(hasLevel) 761 val hit0 = tag(tagLen - 1, vpnnLen*2) === vpn(tagLen - 1, vpnnLen*2) 762 val hit1 = tag(vpnnLen*2 - 1, vpnnLen) === vpn(vpnnLen*2 - 1, vpnnLen) 763 val hit2 = tag(vpnnLen - 1, 0) === vpn(vpnnLen - 1, 0) 764 765 asid_hit && Mux(level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) 766 } else if (hasLevel) { 767 val hit0 = tag(tagLen - 1, tagLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen) 768 val hit1 = tag(tagLen - vpnnLen - 1, tagLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2) 769 770 asid_hit && Mux(level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) 771 } else { 772 asid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen) 773 } 774 } 775 776 def refill(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) { 777 require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside 778 779 tag := vpn(vpnLen - 1, vpnLen - tagLen) 780 ppn := pte.asTypeOf(new PteBundle().cloneType).ppn 781 perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm) 782 this.asid := asid 783 this.prefetch := prefetch 784 this.v := valid 785 this.level.map(_ := level) 786 } 787 788 def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) = { 789 val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel)) 790 e.refill(vpn, asid, pte, level, prefetch, valid) 791 e 792 } 793 794 795 796 override def toPrintable: Printable = { 797 // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}" 798 p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} " + 799 (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") + 800 (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") + 801 p"prefetch:${prefetch}" 802 } 803} 804 805class PtwSectorEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwEntry(tagLen, hasPerm, hasLevel) { 806 override val ppn = UInt(sectorppnLen.W) 807} 808 809class PtwMergeEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwSectorEntry(tagLen, hasPerm, hasLevel) { 810 val ppn_low = UInt(sectortlbwidth.W) 811 val af = Bool() 812 val pf = Bool() 813} 814 815class PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 816 require(log2Up(num)==log2Down(num)) 817 // NOTE: hasPerm means that is leaf or not. 818 819 val tag = UInt(tagLen.W) 820 val asid = UInt(asidLen.W) 821 val ppns = Vec(num, UInt(ppnLen.W)) 822 val vs = Vec(num, Bool()) 823 val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None 824 val prefetch = Bool() 825 // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1") 826 // NOTE: vs is used for different usage: 827 // for l3, which store the leaf(leaves), vs is page fault or not. 828 // for l2, which shoule not store leaf, vs is valid or not, that will anticipate in hit check 829 // Because, l2 should not store leaf(no perm), it doesn't store perm. 830 // If l2 hit a leaf, the perm is still unavailble. Should still page walk. Complex but nothing helpful. 831 // TODO: divide vs into validVec and pfVec 832 // for l2: may valid but pf, so no need for page walk, return random pte with pf. 833 834 def tagClip(vpn: UInt) = { 835 require(vpn.getWidth == vpnLen) 836 vpn(vpnLen - 1, vpnLen - tagLen) 837 } 838 839 def sectorIdxClip(vpn: UInt, level: Int) = { 840 getVpnClip(vpn, level)(log2Up(num) - 1, 0) 841 } 842 843 def hit(vpn: UInt, asid: UInt, ignoreAsid: Boolean = false) = { 844 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 845 asid_hit && tag === tagClip(vpn) && (if (hasPerm) true.B else vs(sectorIdxClip(vpn, level))) 846 } 847 848 def genEntries(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 849 require((data.getWidth / XLEN) == num, 850 s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}") 851 852 val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm)) 853 ps.tag := tagClip(vpn) 854 ps.asid := asid 855 ps.prefetch := prefetch 856 for (i <- 0 until num) { 857 val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle) 858 ps.ppns(i) := pte.ppn 859 ps.vs(i) := !pte.isPf(levelUInt) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf()) 860 ps.perms.map(_(i) := pte.perm) 861 } 862 ps 863 } 864 865 override def toPrintable: Printable = { 866 // require(num == 4, "if num is not 4, please comment this toPrintable") 867 // NOTE: if num is not 4, please comment this toPrintable 868 val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle))) 869 p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " + 870 (if (hasPerm) p"perms:${printVec(permsInner)}" else p"") 871 } 872} 873 874class PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 875 val entries = new PtwEntries(num, tagLen, level, hasPerm) 876 877 val ecc_block = XLEN 878 val ecc_info = get_ecc_info() 879 val ecc = UInt(ecc_info._1.W) 880 881 def get_ecc_info(): (Int, Int, Int, Int) = { 882 val eccBits_per = eccCode.width(ecc_block) - ecc_block 883 884 val data_length = entries.getWidth 885 val data_align_num = data_length / ecc_block 886 val data_not_align = (data_length % ecc_block) != 0 // ugly code 887 val data_unalign_length = data_length - data_align_num * ecc_block 888 val eccBits_unalign = eccCode.width(data_unalign_length) - data_unalign_length 889 890 val eccBits = eccBits_per * data_align_num + eccBits_unalign 891 (eccBits, eccBits_per, data_align_num, data_unalign_length) 892 } 893 894 def encode() = { 895 val data = entries.asUInt() 896 val ecc_slices = Wire(Vec(ecc_info._3, UInt(ecc_info._2.W))) 897 for (i <- 0 until ecc_info._3) { 898 ecc_slices(i) := eccCode.encode(data((i+1)*ecc_block-1, i*ecc_block)) >> ecc_block 899 } 900 if (ecc_info._4 != 0) { 901 val ecc_unaligned = eccCode.encode(data(data.getWidth-1, ecc_info._3*ecc_block)) >> ecc_info._4 902 ecc := Cat(ecc_unaligned, ecc_slices.asUInt()) 903 } else { ecc := ecc_slices.asUInt() } 904 } 905 906 def decode(): Bool = { 907 val data = entries.asUInt() 908 val res = Wire(Vec(ecc_info._3 + 1, Bool())) 909 for (i <- 0 until ecc_info._3) { 910 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} 911 } 912 if (ecc_info._2 != 0 && ecc_info._4 != 0) { 913 res(ecc_info._3) := eccCode.decode( 914 Cat(ecc(ecc_info._1-1, ecc_info._2*ecc_info._3), data(data.getWidth-1, ecc_info._3*ecc_block))).error 915 } else { res(ecc_info._3) := false.B } 916 917 Cat(res).orR 918 } 919 920 def gen(vpn: UInt, asid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 921 this.entries := entries.genEntries(vpn, asid, data, levelUInt, prefetch) 922 this.encode() 923 } 924} 925 926class PtwReq(implicit p: Parameters) extends PtwBundle { 927 val vpn = UInt(vpnLen.W) 928 929 override def toPrintable: Printable = { 930 p"vpn:0x${Hexadecimal(vpn)}" 931 } 932} 933 934class PtwReqwithMemIdx(implicit p: Parameters) extends PtwReq { 935 val memidx = new MemBlockidxBundle 936} 937 938class PtwResp(implicit p: Parameters) extends PtwBundle { 939 val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true) 940 val pf = Bool() 941 val af = Bool() 942 943 def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = { 944 this.entry.level.map(_ := level) 945 this.entry.tag := vpn 946 this.entry.perm.map(_ := pte.getPerm()) 947 this.entry.ppn := pte.ppn 948 this.entry.prefetch := DontCare 949 this.entry.asid := asid 950 this.entry.v := !pf 951 this.pf := pf 952 this.af := af 953 } 954 955 override def toPrintable: Printable = { 956 p"entry:${entry} pf:${pf} af:${af}" 957 } 958} 959 960class PtwResptomerge (implicit p: Parameters) extends PtwBundle { 961 val entry = UInt(blockBits.W) 962 val vpn = UInt(vpnLen.W) 963 val level = UInt(log2Up(Level).W) 964 val pf = Bool() 965 val af = Bool() 966 val asid = UInt(asidLen.W) 967 968 def apply(pf: Bool, af: Bool, level: UInt, pte: UInt, vpn: UInt, asid: UInt) = { 969 this.entry := pte 970 this.pf := pf 971 this.af := af 972 this.level := level 973 this.vpn := vpn 974 this.asid := asid 975 } 976 977 override def toPrintable: Printable = { 978 p"entry:${entry} pf:${pf} af:${af}" 979 } 980} 981 982class PtwRespwithMemIdx(implicit p: Parameters) extends PtwResp { 983 val memidx = new MemBlockidxBundle 984} 985 986class PtwSectorRespwithMemIdx(implicit p: Parameters) extends PtwSectorResp { 987 val memidx = new MemBlockidxBundle 988} 989 990class PtwSectorResp(implicit p: Parameters) extends PtwBundle { 991 val entry = new PtwSectorEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true) 992 val addr_low = UInt(sectortlbwidth.W) 993 val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W)) 994 val valididx = Vec(tlbcontiguous, Bool()) 995 val pteidx = Vec(tlbcontiguous, Bool()) 996 val pf = Bool() 997 val af = Bool() 998 999 def genPPN(vpn: UInt): UInt = { 1000 MuxLookup(entry.level.get, 0.U, Seq( 1001 0.U -> Cat(entry.ppn(entry.ppn.getWidth-1, vpnnLen * 2 - sectortlbwidth), vpn(vpnnLen*2-1, 0)), 1002 1.U -> Cat(entry.ppn(entry.ppn.getWidth-1, vpnnLen - sectortlbwidth), vpn(vpnnLen-1, 0)), 1003 2.U -> Cat(entry.ppn(entry.ppn.getWidth-1, 0), ppn_low(vpn(sectortlbwidth - 1, 0)))) 1004 ) 1005 } 1006 1007 def hit(vpn: UInt, asid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false) = { 1008 require(vpn.getWidth == vpnLen) 1009 // require(this.asid.getWidth <= asid.getWidth) 1010 val asid_hit = if (ignoreAsid) true.B else (this.entry.asid === asid) 1011 if (allType) { 1012 val hit0 = entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnLen - 1, vpnnLen * 2) 1013 val hit1 = entry.tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen) 1014 val hit2 = entry.tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) 1015 val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0)) 1016 1017 asid_hit && Mux(entry.level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(entry.level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) && addr_low_hit 1018 } else { 1019 val hit0 = entry.tag(sectorvpnLen - 1, sectorvpnLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen) 1020 val hit1 = entry.tag(sectorvpnLen - vpnnLen - 1, sectorvpnLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2) 1021 val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0)) 1022 1023 asid_hit && Mux(entry.level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) && addr_low_hit 1024 } 1025 } 1026} 1027 1028class PtwMergeResp(implicit p: Parameters) extends PtwBundle { 1029 val entry = Vec(tlbcontiguous, new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)) 1030 val pteidx = Vec(tlbcontiguous, Bool()) 1031 val not_super = Bool() 1032 1033 def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt, addr_low : UInt, not_super : Boolean = true) = { 1034 assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!") 1035 1036 val ptw_resp = Wire(new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)) 1037 ptw_resp.ppn := pte.ppn(ppnLen - 1, sectortlbwidth) 1038 ptw_resp.ppn_low := pte.ppn(sectortlbwidth - 1, 0) 1039 ptw_resp.level.map(_ := level) 1040 ptw_resp.perm.map(_ := pte.getPerm()) 1041 ptw_resp.tag := vpn(vpnLen - 1, sectortlbwidth) 1042 ptw_resp.pf := pf 1043 ptw_resp.af := af 1044 ptw_resp.v := !pf 1045 ptw_resp.prefetch := DontCare 1046 ptw_resp.asid := asid 1047 this.pteidx := UIntToOH(addr_low).asBools 1048 this.not_super := not_super.B 1049 for (i <- 0 until tlbcontiguous) { 1050 this.entry(i) := ptw_resp 1051 } 1052 } 1053} 1054 1055class L2TLBIO(implicit p: Parameters) extends PtwBundle { 1056 val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO)) 1057 val sfence = Input(new SfenceBundle) 1058 val csr = new Bundle { 1059 val tlb = Input(new TlbCsrBundle) 1060 val distribute_csr = Flipped(new DistributedCSRIO) 1061 } 1062} 1063 1064class L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle { 1065 val addr = UInt(PAddrBits.W) 1066 val id = UInt(bMemID.W) 1067} 1068 1069class L2TlbInnerBundle(implicit p: Parameters) extends PtwReq { 1070 val source = UInt(bSourceWidth.W) 1071} 1072 1073 1074object ValidHoldBypass{ 1075 def apply(infire: Bool, outfire: Bool, flush: Bool = false.B) = { 1076 val valid = RegInit(false.B) 1077 when (infire) { valid := true.B } 1078 when (outfire) { valid := false.B } // ATTENTION: order different with ValidHold 1079 when (flush) { valid := false.B } // NOTE: the flush will flush in & out, is that ok? 1080 valid || infire 1081 } 1082} 1083 1084class L1TlbDB(implicit p: Parameters) extends TlbBundle { 1085 val vpn = UInt(vpnLen.W) 1086} 1087 1088class PageCacheDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 1089 val vpn = UInt(vpnLen.W) 1090 val source = UInt(bSourceWidth.W) 1091 val bypassed = Bool() 1092 val is_first = Bool() 1093 val prefetched = Bool() 1094 val prefetch = Bool() 1095 val l2Hit = Bool() 1096 val l1Hit = Bool() 1097 val hit = Bool() 1098} 1099 1100class PTWDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 1101 val vpn = UInt(vpnLen.W) 1102 val source = UInt(bSourceWidth.W) 1103} 1104 1105class L2TlbPrefetchDB(implicit p: Parameters) extends TlbBundle { 1106 val vpn = UInt(vpnLen.W) 1107} 1108 1109class L2TlbMissQueueDB(implicit p: Parameters) extends TlbBundle { 1110 val vpn = UInt(vpnLen.W) 1111} 1112