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 org.chipsalliance.cde.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 36 37class PtePermBundle(implicit p: Parameters) extends TlbBundle { 38 val d = Bool() 39 val a = Bool() 40 val g = Bool() 41 val u = Bool() 42 val x = Bool() 43 val w = Bool() 44 val r = Bool() 45 46 override def toPrintable: Printable = { 47 p"d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r}"// + 48 //(if(hasV) (p"v:${v}") else p"") 49 } 50} 51 52class TlbPMBundle(implicit p: Parameters) extends TlbBundle { 53 val r = Bool() 54 val w = Bool() 55 val x = Bool() 56 val c = Bool() 57 val atomic = Bool() 58 59 def assign_ap(pm: PMPConfig) = { 60 r := pm.r 61 w := pm.w 62 x := pm.x 63 c := pm.c 64 atomic := pm.atomic 65 } 66} 67 68class TlbPermBundle(implicit p: Parameters) extends TlbBundle { 69 val pf = Bool() // NOTE: if this is true, just raise pf 70 val af = Bool() // NOTE: if this is true, just raise af 71 // pagetable perm (software defined) 72 val d = Bool() 73 val a = Bool() 74 val g = Bool() 75 val u = Bool() 76 val x = Bool() 77 val w = Bool() 78 val r = Bool() 79 80 def apply(item: PtwSectorResp) = { 81 val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 82 this.pf := item.pf 83 this.af := item.af 84 this.d := ptePerm.d 85 this.a := ptePerm.a 86 this.g := ptePerm.g 87 this.u := ptePerm.u 88 this.x := ptePerm.x 89 this.w := ptePerm.w 90 this.r := ptePerm.r 91 92 this 93 } 94 95 def applyS2(item: HptwResp, pm: PMPConfig) = { 96 val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 97 this.pf := item.gpf 98 this.af := item.gaf 99 this.d := ptePerm.d 100 this.a := ptePerm.a 101 this.g := ptePerm.g 102 this.u := ptePerm.u 103 this.x := ptePerm.x 104 this.w := ptePerm.w 105 this.r := ptePerm.r 106 107 this 108 } 109 override def toPrintable: Printable = { 110 p"pf:${pf} af:${af} d:${d} a:${a} g:${g} u:${u} x:${x} w:${w} r:${r} " 111 } 112} 113 114class TlbSectorPermBundle(implicit p: Parameters) extends TlbBundle { 115 val pf = Bool() // NOTE: if this is true, just raise pf 116 val af = Bool() // NOTE: if this is true, just raise af 117 // pagetable perm (software defined) 118 val d = Bool() 119 val a = Bool() 120 val g = Bool() 121 val u = Bool() 122 val x = Bool() 123 val w = Bool() 124 val r = Bool() 125 126 def apply(item: PtwSectorResp) = { 127 val ptePerm = item.entry.perm.get.asTypeOf(new PtePermBundle().cloneType) 128 this.pf := item.pf 129 this.af := item.af 130 this.d := ptePerm.d 131 this.a := ptePerm.a 132 this.g := ptePerm.g 133 this.u := ptePerm.u 134 this.x := ptePerm.x 135 this.w := ptePerm.w 136 this.r := ptePerm.r 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 } 143} 144 145// multi-read && single-write 146// input is data, output is hot-code(not one-hot) 147class CAMTemplate[T <: Data](val gen: T, val set: Int, val readWidth: Int)(implicit p: Parameters) extends TlbModule { 148 val io = IO(new Bundle { 149 val r = new Bundle { 150 val req = Input(Vec(readWidth, gen)) 151 val resp = Output(Vec(readWidth, Vec(set, Bool()))) 152 } 153 val w = Input(new Bundle { 154 val valid = Bool() 155 val bits = new Bundle { 156 val index = UInt(log2Up(set).W) 157 val data = gen 158 } 159 }) 160 }) 161 162 val wordType = UInt(gen.getWidth.W) 163 val array = Reg(Vec(set, wordType)) 164 165 io.r.resp.zipWithIndex.map{ case (a,i) => 166 a := array.map(io.r.req(i).asUInt === _) 167 } 168 169 when (io.w.valid) { 170 array(io.w.bits.index) := io.w.bits.data.asUInt 171 } 172} 173 174class TlbEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle { 175 require(pageNormal || pageSuper) 176 177 val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W) 178 else UInt(vpnLen.W) 179 val asid = UInt(asidLen.W) 180 val level = if (!pageNormal) Some(UInt(1.W)) 181 else if (!pageSuper) None 182 else Some(UInt(2.W)) 183 val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W) 184 else UInt(ppnLen.W) 185 val perm = new TlbPermBundle 186 187 val g_perm = new TlbPermBundle 188 val vmid = UInt(vmidLen.W) 189 val s2xlate = UInt(2.W) 190 191 /** s2xlate usage: 192 * bits0 0: disable s2xlate 193 * 1: enable s2xlate 194 * bits1 0: stage 1 and stage 2 if bits0 is 1 195 * 1: Only stage 2 if bits0 is 1 196 * */ 197 198 /** level usage: 199 * !PageSuper: page is only normal, level is None, match all the tag 200 * !PageNormal: page is only super, level is a Bool(), match high 9*2 parts 201 * bits0 0: need mid 9bits 202 * 1: no need mid 9bits 203 * PageSuper && PageNormal: page hold all the three type, 204 * bits0 0: need low 9bits 205 * bits1 0: need mid 9bits 206 */ 207 208 209 def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false, vmid: UInt, hasS2xlate: Bool, onlyS2: Bool): Bool = { 210 val asid_hit = Mux(hasS2xlate && onlyS2, true.B, if (ignoreAsid) true.B else (this.asid === asid)) 211 val vmid_hit = Mux(hasS2xlate, this.vmid === vmid, true.B) 212 213 // NOTE: for timing, dont care low set index bits at hit check 214 // do not need store the low bits actually 215 if (!pageSuper) asid_hit && drop_set_equal(vpn, tag, nSets) && vmid_hit 216 else if (!pageNormal) { 217 val tag_match_hi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*3-1, vpnnLen*2) 218 val tag_match_mi = tag(vpnnLen-1, 0) === vpn(vpnnLen*2-1, vpnnLen) 219 val tag_match = tag_match_hi && (level.get.asBool || tag_match_mi) 220 asid_hit && tag_match && vmid_hit 221 } 222 else { 223 val tmp_level = level.get 224 val tag_match_hi = tag(vpnnLen*3-1, vpnnLen*2) === vpn(vpnnLen*3-1, vpnnLen*2) 225 val tag_match_mi = tag(vpnnLen*2-1, vpnnLen) === vpn(vpnnLen*2-1, vpnnLen) 226 val tag_match_lo = tag(vpnnLen-1, 0) === vpn(vpnnLen-1, 0) // if pageNormal is false, this will always be false 227 val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 228 asid_hit && tag_match && vmid_hit 229 } 230 } 231 232 def apply(item: PtwRespS2, pm: PMPConfig): TlbEntry = { 233 this.asid := item.s1.entry.asid 234 val inner_level = item.s1.entry.level.getOrElse(0.U) max item.s2.entry.level.getOrElse(0.U) 235 this.level.map(_ := { if (pageNormal && pageSuper) MuxLookup(inner_level, 0.U)(Seq( 236 0.U -> 3.U, 237 1.U -> 1.U, 238 2.U -> 0.U )) 239 else if (pageSuper) ~inner_level(0) 240 else 0.U }) 241 val s1tag = {if (pageNormal) Cat(item.s1.entry.tag, OHToUInt(item.s1.pteidx)) else item.s1.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth)} 242 val s2tag = {if (pageNormal) item.s2.entry.tag else item.s2.entry.tag(vpnLen - 1, vpnnLen)} 243 this.tag := Mux(item.s2xlate === onlyStage2, s2tag, s1tag) 244 245 val s1ppn = { 246 if (!pageNormal) item.s1.entry.ppn(sectorppnLen - 1, vpnnLen - sectortlbwidth) 247 else Cat(item.s1.entry.ppn, item.s1.ppn_low(OHToUInt(item.s1.pteidx))) 248 } 249 val s2ppn = { 250 if (!pageNormal) item.s2.entry.ppn(ppnLen - 1, vpnnLen) 251 else item.s2.entry.ppn 252 } 253 this.ppn := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, s1ppn, s2ppn) 254 this.perm.apply(item.s1) 255 this.vmid := item.s1.entry.vmid.getOrElse(0.U) 256 this.g_perm.applyS2(item.s2, pm) 257 this.s2xlate := item.s2xlate 258 this 259 } 260 261 // 4KB is normal entry, 2MB/1GB is considered as super entry 262 def is_normalentry(): Bool = { 263 if (!pageSuper) { true.B } 264 else if (!pageNormal) { false.B } 265 else { level.get === 0.U } 266 } 267 268 269 def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = { 270 val inner_level = level.getOrElse(0.U) 271 val ppn_res = if (!pageSuper) ppn 272 else if (!pageNormal) Cat(ppn(ppnLen-vpnnLen-1, vpnnLen), 273 Mux(inner_level(0), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen-1,0)), 274 vpn(vpnnLen-1, 0)) 275 else Cat(ppn(ppnLen-1, vpnnLen*2), 276 Mux(inner_level(1), vpn(vpnnLen*2-1, vpnnLen), ppn(vpnnLen*2-1, vpnnLen)), 277 Mux(inner_level(0), vpn(vpnnLen-1, 0), ppn(vpnnLen-1, 0))) 278 279 if (saveLevel) Cat(ppn(ppn.getWidth-1, vpnnLen*2), RegEnable(ppn_res(vpnnLen*2-1, 0), valid)) 280 else ppn_res 281 } 282 283 override def toPrintable: Printable = { 284 val inner_level = level.getOrElse(2.U) 285 p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}" 286 } 287 288} 289 290class TlbSectorEntry(pageNormal: Boolean, pageSuper: Boolean)(implicit p: Parameters) extends TlbBundle { 291 require(pageNormal || pageSuper) 292 293 val tag = if (!pageNormal) UInt((vpnLen - vpnnLen).W) 294 else UInt(sectorvpnLen.W) 295 val asid = UInt(asidLen.W) 296 val level = if (!pageNormal) Some(UInt(1.W)) 297 else if (!pageSuper) None 298 else Some(UInt(2.W)) 299 val ppn = if (!pageNormal) UInt((ppnLen - vpnnLen).W) 300 else UInt(sectorppnLen.W) //only used when disable s2xlate 301 val perm = new TlbSectorPermBundle 302 val valididx = Vec(tlbcontiguous, Bool()) 303 val pteidx = Vec(tlbcontiguous, Bool()) 304 val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W)) 305 306 val g_perm = new TlbPermBundle 307 val vmid = UInt(vmidLen.W) 308 val s2xlate = UInt(2.W) 309 310 /** s2xlate usage: 311 * bits0 0: disable s2xlate 312 * 1: enable s2xlate 313 * bits1 0: stage 1 and stage 2 if bits0 is 1 314 * 1: Only stage 2 if bits0 is 1 315 * */ 316 317 /** level usage: 318 * !PageSuper: page is only normal, level is None, match all the tag 319 * !PageNormal: page is only super, level is a Bool(), match high 9*2 parts 320 * bits0 0: need mid 9bits 321 * 1: no need mid 9bits 322 * PageSuper && PageNormal: page hold all the three type, 323 * bits0 0: need low 9bits 324 * bits1 0: need mid 9bits 325 */ 326 327 def hit(vpn: UInt, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false, vmid: UInt, hasS2xlate: Bool, onlyS2: Bool): Bool = { 328 val asid_hit = Mux(hasS2xlate && onlyS2, true.B, if (ignoreAsid) true.B else (this.asid === asid)) 329 val addr_low_hit = valididx(vpn(2, 0)) 330 val vmid_hit = Mux(hasS2xlate, this.vmid === vmid, true.B) 331 val pteidx_hit = Mux(hasS2xlate, pteidx(vpn(2, 0)), true.B) 332 // NOTE: for timing, dont care low set index bits at hit check 333 // do not need store the low bits actually 334 if (!pageSuper) asid_hit && drop_set_equal(vpn(vpn.getWidth - 1, sectortlbwidth), tag, nSets) && addr_low_hit && vmid_hit && pteidx_hit 335 else if (!pageNormal) { 336 val tag_match_hi = tag(vpnnLen * 2 - 1, vpnnLen) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 337 val tag_match_mi = tag(vpnnLen - 1, 0) === vpn(vpnnLen * 2 - 1, vpnnLen) 338 val tag_match = tag_match_hi && (level.get.asBool || tag_match_mi) 339 asid_hit && tag_match && addr_low_hit && vmid_hit && pteidx_hit 340 } 341 else { 342 val tmp_level = level.get 343 val tag_match_hi = tag(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 344 val tag_match_mi = tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen) 345 val tag_match_lo = tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) // if pageNormal is false, this will always be false 346 val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 347 asid_hit && tag_match && addr_low_hit && vmid_hit && pteidx_hit 348 } 349 } 350 351 def wbhit(data: PtwSectorResp, asid: UInt, nSets: Int = 1, ignoreAsid: Boolean = false): Bool = { 352 val vpn = Cat(data.entry.tag, 0.U(sectortlbwidth.W)) 353 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 354 val vpn_hit = Wire(Bool()) 355 val index_hit = Wire(Vec(tlbcontiguous, Bool())) 356 357 // NOTE: for timing, dont care low set index bits at hit check 358 // do not need store the low bits actually 359 if (!pageSuper) { 360 vpn_hit := asid_hit && drop_set_equal(vpn(vpn.getWidth - 1, sectortlbwidth), tag, nSets) 361 } 362 else if (!pageNormal) { 363 val tag_match_hi = tag(vpnnLen * 2 - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 364 val tag_match_mi = tag(vpnnLen - 1, 0) === vpn(vpnnLen * 2 - 1, vpnnLen) 365 val tag_match = tag_match_hi && (level.get.asBool || tag_match_mi) 366 vpn_hit := asid_hit && tag_match 367 } 368 else { 369 val tmp_level = level.get 370 val tag_match_hi = tag(vpnnLen * 3 - sectortlbwidth - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnnLen * 3 - 1, vpnnLen * 2) 371 val tag_match_mi = tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen) 372 val tag_match_lo = tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) // if pageNormal is false, this will always be false 373 val tag_match = tag_match_hi && (tmp_level(1) || tag_match_mi) && (tmp_level(0) || tag_match_lo) 374 vpn_hit := asid_hit && tag_match 375 } 376 377 for (i <- 0 until tlbcontiguous) { 378 index_hit(i) := data.valididx(i) && valididx(i) 379 } 380 381 // For example, tlb req to page cache with vpn 0x10 382 // At this time, 0x13 has not been paged, so page cache only resp 0x10 383 // When 0x13 refill to page cache, previous item will be flushed 384 // Now 0x10 and 0x13 are both valid in page cache 385 // However, when 0x13 refill to tlb, will trigger multi hit 386 // So will only trigger multi-hit when PopCount(data.valididx) = 1 387 vpn_hit && index_hit.reduce(_ || _) && PopCount(data.valididx) === 1.U 388 } 389 390 def apply(item: PtwRespS2): TlbSectorEntry = { 391 this.asid := item.s1.entry.asid 392 val inner_level = item.s1.entry.level.getOrElse(0.U) max item.s2.entry.level.getOrElse(0.U) 393 this.level.map(_ := { if (pageNormal && pageSuper) MuxLookup(inner_level, 0.U)(Seq( 394 0.U -> 3.U, 395 1.U -> 1.U, 396 2.U -> 0.U )) 397 else if (pageSuper) ~inner_level(0) 398 else 0.U }) 399 this.perm.apply(item.s1) 400 401 this.pteidx := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, item.s1.pteidx, UIntToOH(item.s2.entry.tag(sectortlbwidth - 1, 0))) 402 this.valididx := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, item.s1.valididx, OHToUInt(this.pteidx)) 403 404 val s1tag = {if (pageNormal) item.s1.entry.tag else item.s1.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth)} 405 val s2tag = {if (pageNormal) item.s2.entry.tag else item.s2.entry.tag(sectorvpnLen - 1, vpnnLen - sectortlbwidth)} 406 this.tag := Mux(item.s2xlate === onlyStage2, s2tag, s1tag) 407 408 val s1ppn = { 409 if (!pageNormal) item.s1.entry.ppn(sectorppnLen - 1, vpnnLen - sectortlbwidth) else item.s1.entry.ppn 410 } 411 val s1ppn_low = item.s1.ppn_low 412 val s2ppn = { 413 if (!pageNormal) item.s2.entry.ppn(ppnLen - 1, vpnnLen) else item.s2.entry.ppn(ppnLen - 1, sectortlbwidth) 414 } 415 val s2ppn_low = VecInit(Seq.fill(tlbcontiguous)(item.s2.entry.ppn(sectortlbwidth - 1, 0))) 416 this.ppn := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, s1ppn, s2ppn) 417 this.ppn_low := Mux(item.s2xlate === noS2xlate || item.s2xlate === onlyStage1, s1ppn_low, s2ppn_low) 418 this.vmid := item.s1.entry.vmid.getOrElse(0.U) 419 this.g_perm.applyS2(item.s2, pm(0)) 420 this.s2xlate := item.s2xlate 421 this 422 } 423 424 // 4KB is normal entry, 2MB/1GB is considered as super entry 425 def is_normalentry(): Bool = { 426 if (!pageSuper) { true.B } 427 else if (!pageNormal) { false.B } 428 else { level.get === 0.U } 429 } 430 431 432 def genPPN(saveLevel: Boolean = false, valid: Bool = false.B)(vpn: UInt) : UInt = { 433 val inner_level = level.getOrElse(0.U) 434 val ppn_res = if (!pageSuper) Cat(ppn, ppn_low(vpn(sectortlbwidth - 1, 0))) 435 else if (!pageNormal) Cat(ppn(ppnLen - vpnnLen - 1, vpnnLen), 436 Mux(inner_level(0), vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen - 1,0)), 437 vpn(vpnnLen - 1, 0)) 438 else Cat(ppn(sectorppnLen - 1, vpnnLen * 2 - sectortlbwidth), 439 Mux(inner_level(1), vpn(vpnnLen * 2 - 1, vpnnLen), ppn(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth)), 440 Mux(inner_level(0), vpn(vpnnLen - 1, 0), Cat(ppn(vpnnLen - sectortlbwidth - 1, 0), ppn_low(vpn(sectortlbwidth - 1, 0))))) 441 442 if (saveLevel) { 443 if (ppn.getWidth == ppnLen - vpnnLen) { 444 Cat(ppn(ppn.getWidth - 1, vpnnLen * 2), RegEnable(ppn_res(vpnnLen * 2 - 1, 0), valid)) 445 } else { 446 require(ppn.getWidth == sectorppnLen) 447 Cat(ppn(ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), RegEnable(ppn_res(vpnnLen * 2 - 1, 0), valid)) 448 } 449 } 450 else ppn_res 451 } 452 453 def hasS2xlate(): Bool = { 454 this.s2xlate =/= noS2xlate 455 } 456 457 override def toPrintable: Printable = { 458 val inner_level = level.getOrElse(2.U) 459 p"asid: ${asid} level:${inner_level} vpn:${Hexadecimal(tag)} ppn:${Hexadecimal(ppn)} perm:${perm}" 460 } 461 462} 463 464object TlbCmd { 465 def read = "b00".U 466 def write = "b01".U 467 def exec = "b10".U 468 469 def atom_read = "b100".U // lr 470 def atom_write = "b101".U // sc / amo 471 472 def apply() = UInt(3.W) 473 def isRead(a: UInt) = a(1,0)===read 474 def isWrite(a: UInt) = a(1,0)===write 475 def isExec(a: UInt) = a(1,0)===exec 476 477 def isAtom(a: UInt) = a(2) 478 def isAmo(a: UInt) = a===atom_write // NOTE: sc mixed 479} 480 481class TlbStorageIO(nSets: Int, nWays: Int, ports: Int, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 482 val r = new Bundle { 483 val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 484 val vpn = Output(UInt(vpnLen.W)) 485 val s2xlate = Output(UInt(2.W)) // 0 bit: has s2xlate, 1 bit: Only valid when 0 bit is 1. If 0, all stage; if 1, only stage 2 486 }))) 487 val resp = Vec(ports, ValidIO(new Bundle{ 488 val hit = Output(Bool()) 489 val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 490 val perm = Vec(nDups, Output(new TlbSectorPermBundle())) 491 val g_perm = Vec(nDups, Output(new TlbPermBundle())) 492 val s2xlate = Vec(nDups, Output(UInt(2.W))) 493 })) 494 } 495 val w = Flipped(ValidIO(new Bundle { 496 val wayIdx = Output(UInt(log2Up(nWays).W)) 497 val data = Output(new PtwRespS2) 498 })) 499 val access = Vec(ports, new ReplaceAccessBundle(nSets, nWays)) 500 501 def r_req_apply(valid: Bool, vpn: UInt, i: Int, s2xlate:UInt): Unit = { 502 this.r.req(i).valid := valid 503 this.r.req(i).bits.vpn := vpn 504 this.r.req(i).bits.s2xlate := s2xlate 505 506 } 507 508 def r_resp_apply(i: Int) = { 509 (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) 510 } 511 512 def w_apply(valid: Bool, wayIdx: UInt, data: PtwRespS2): Unit = { 513 this.w.valid := valid 514 this.w.bits.wayIdx := wayIdx 515 this.w.bits.data := data 516 } 517 518} 519 520class TlbStorageWrapperIO(ports: Int, q: TLBParameters, nDups: Int = 1)(implicit p: Parameters) extends MMUIOBaseBundle { 521 val r = new Bundle { 522 val req = Vec(ports, Flipped(DecoupledIO(new Bundle { 523 val vpn = Output(UInt(vpnLen.W)) 524 val s2xlate = Output(UInt(2.W)) 525 }))) 526 val resp = Vec(ports, ValidIO(new Bundle{ 527 val hit = Output(Bool()) 528 val ppn = Vec(nDups, Output(UInt(ppnLen.W))) 529 val perm = Vec(nDups, Output(new TlbPermBundle())) 530 val g_perm = Vec(nDups, Output(new TlbPermBundle())) 531 val s2xlate = Vec(nDups, Output(UInt(2.W))) 532 })) 533 } 534 val w = Flipped(ValidIO(new Bundle { 535 val data = Output(new PtwRespS2) 536 })) 537 val replace = if (q.outReplace) Flipped(new TlbReplaceIO(ports, q)) else null 538 539 def r_req_apply(valid: Bool, vpn: UInt, i: Int, s2xlate: UInt): Unit = { 540 this.r.req(i).valid := valid 541 this.r.req(i).bits.vpn := vpn 542 this.r.req(i).bits.s2xlate := s2xlate 543 } 544 545 def r_resp_apply(i: Int) = { 546 (this.r.resp(i).bits.hit, this.r.resp(i).bits.ppn, this.r.resp(i).bits.perm, 547 this.r.resp(i).bits.super_hit, this.r.resp(i).bits.super_ppn, this.r.resp(i).bits.spm, 548 this.r.resp(i).bits.g_perm, this.r.resp(i).bits.s2xlate) 549 } 550 551 def w_apply(valid: Bool, data: PtwRespS2): Unit = { 552 this.w.valid := valid 553 this.w.bits.data := data 554 } 555} 556 557class ReplaceAccessBundle(nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 558 val sets = Output(UInt(log2Up(nSets).W)) 559 val touch_ways = ValidIO(Output(UInt(log2Up(nWays).W))) 560} 561 562class ReplaceIO(Width: Int, nSets: Int, nWays: Int)(implicit p: Parameters) extends TlbBundle { 563 val access = Vec(Width, Flipped(new ReplaceAccessBundle(nSets, nWays))) 564 565 val refillIdx = Output(UInt(log2Up(nWays).W)) 566 val chosen_set = Flipped(Output(UInt(log2Up(nSets).W))) 567 568 def apply_sep(in: Seq[ReplaceIO], vpn: UInt): Unit = { 569 for ((ac_rep, ac_tlb) <- access.zip(in.map(a => a.access.map(b => b)).flatten)) { 570 ac_rep := ac_tlb 571 } 572 this.chosen_set := get_set_idx(vpn, nSets) 573 in.map(a => a.refillIdx := this.refillIdx) 574 } 575} 576 577class TlbReplaceIO(Width: Int, q: TLBParameters)(implicit p: Parameters) extends 578 TlbBundle { 579 val page = new ReplaceIO(Width, q.NSets, q.NWays) 580 581 def apply_sep(in: Seq[TlbReplaceIO], vpn: UInt) = { 582 this.page.apply_sep(in.map(_.page), vpn) 583 } 584 585} 586 587class MemBlockidxBundle(implicit p: Parameters) extends TlbBundle { 588 val is_ld = Bool() 589 val is_st = Bool() 590 val idx = 591 if (VirtualLoadQueueSize >= StoreQueueSize) { 592 val idx = UInt(log2Ceil(VirtualLoadQueueSize).W) 593 idx 594 } else { 595 val idx = UInt(log2Ceil(StoreQueueSize).W) 596 idx 597 } 598} 599 600class TlbReq(implicit p: Parameters) extends TlbBundle { 601 val vaddr = Output(UInt(VAddrBits.W)) 602 val cmd = Output(TlbCmd()) 603 val hyperinst = Output(Bool()) 604 val hlvx = Output(Bool()) 605 val size = Output(UInt(log2Ceil(log2Ceil(XLEN/8)+1).W)) 606 val kill = Output(Bool()) // Use for blocked tlb that need sync with other module like icache 607 val memidx = Output(new MemBlockidxBundle) 608 // do not translate, but still do pmp/pma check 609 val no_translate = Output(Bool()) 610 val debug = new Bundle { 611 val pc = Output(UInt(XLEN.W)) 612 val robIdx = Output(new RobPtr) 613 val isFirstIssue = Output(Bool()) 614 } 615 616 // Maybe Block req needs a kill: for itlb, itlb and icache may not sync, itlb should wait icache to go ahead 617 override def toPrintable: Printable = { 618 p"vaddr:0x${Hexadecimal(vaddr)} cmd:${cmd} kill:${kill} pc:0x${Hexadecimal(debug.pc)} robIdx:${debug.robIdx}" 619 } 620} 621 622class TlbExceptionBundle(implicit p: Parameters) extends TlbBundle { 623 val ld = Output(Bool()) 624 val st = Output(Bool()) 625 val instr = Output(Bool()) 626} 627 628class TlbResp(nDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 629 val paddr = Vec(nDups, Output(UInt(PAddrBits.W))) 630 val gpaddr = Vec(nDups, Output(UInt(GPAddrBits.W))) 631 val miss = Output(Bool()) 632 val excp = Vec(nDups, new Bundle { 633 val gpf = new TlbExceptionBundle() 634 val pf = new TlbExceptionBundle() 635 val af = new TlbExceptionBundle() 636 }) 637 val ptwBack = Output(Bool()) // when ptw back, wake up replay rs's state 638 val memidx = Output(new MemBlockidxBundle) 639 640 val debug = new Bundle { 641 val robIdx = Output(new RobPtr) 642 val isFirstIssue = Output(Bool()) 643 } 644 override def toPrintable: Printable = { 645 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}" 646 } 647} 648 649class TlbRequestIO(nRespDups: Int = 1)(implicit p: Parameters) extends TlbBundle { 650 val req = DecoupledIO(new TlbReq) 651 val req_kill = Output(Bool()) 652 val resp = Flipped(DecoupledIO(new TlbResp(nRespDups))) 653} 654 655class TlbPtwIO(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 656 val req = Vec(Width, DecoupledIO(new PtwReq)) 657 val resp = Flipped(DecoupledIO(new PtwRespS2)) 658 659 660 override def toPrintable: Printable = { 661 p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 662 } 663} 664 665class TlbPtwIOwithMemIdx(Width: Int = 1)(implicit p: Parameters) extends TlbBundle { 666 val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx)) 667 val resp = Flipped(DecoupledIO(new PtwRespS2withMemIdx())) 668 669 670 override def toPrintable: Printable = { 671 p"req(0):${req(0).valid} ${req(0).ready} ${req(0).bits} | resp:${resp.valid} ${resp.ready} ${resp.bits}" 672 } 673} 674 675class TlbHintReq(implicit p: Parameters) extends TlbBundle { 676 val id = Output(UInt(log2Up(loadfiltersize).W)) 677 val full = Output(Bool()) 678} 679 680class TLBHintResp(implicit p: Parameters) extends TlbBundle { 681 val id = Output(UInt(log2Up(loadfiltersize).W)) 682 // When there are multiple matching entries for PTW resp in filter 683 // e.g. vaddr 0, 0x80000000. vaddr 1, 0x80010000 684 // these two vaddrs are not in a same 4K Page, so will send to ptw twice 685 // However, when ptw resp, if they are in a 1G or 2M huge page 686 // The two entries will both hit, and both need to replay 687 val replay_all = Output(Bool()) 688} 689 690class TlbHintIO(implicit p: Parameters) extends TlbBundle { 691 val req = Vec(exuParameters.LduCnt, new TlbHintReq) 692 val resp = ValidIO(new TLBHintResp) 693} 694 695class MMUIOBaseBundle(implicit p: Parameters) extends TlbBundle { 696 val sfence = Input(new SfenceBundle) 697 val csr = Input(new TlbCsrBundle) 698 699 def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle): Unit = { 700 this.sfence <> sfence 701 this.csr <> csr 702 } 703 704 // overwrite satp. write satp will cause flushpipe but csr.priv won't 705 // satp will be dealyed several cycles from writing, but csr.priv won't 706 // so inside mmu, these two signals should be divided 707 def base_connect(sfence: SfenceBundle, csr: TlbCsrBundle, satp: TlbSatpBundle) = { 708 this.sfence <> sfence 709 this.csr <> csr 710 this.csr.satp := satp 711 } 712} 713 714class TlbRefilltoMemIO()(implicit p: Parameters) extends TlbBundle { 715 val valid = Bool() 716 val memidx = new MemBlockidxBundle 717} 718 719class TlbIO(Width: Int, nRespDups: Int = 1, q: TLBParameters)(implicit p: Parameters) extends 720 MMUIOBaseBundle { 721 val hartId = Input(UInt(hartIdLen.W)) 722 val requestor = Vec(Width, Flipped(new TlbRequestIO(nRespDups))) 723 val flushPipe = Vec(Width, Input(Bool())) 724 val ptw = new TlbPtwIOwithMemIdx(Width) 725 val refill_to_mem = Output(new TlbRefilltoMemIO()) 726 val replace = if (q.outReplace) Flipped(new TlbReplaceIO(Width, q)) else null 727 val pmp = Vec(Width, ValidIO(new PMPReqBundle())) 728 val tlbreplay = Vec(Width, Output(Bool())) 729} 730 731class VectorTlbPtwIO(Width: Int)(implicit p: Parameters) extends TlbBundle { 732 val req = Vec(Width, DecoupledIO(new PtwReqwithMemIdx())) 733 val resp = Flipped(DecoupledIO(new Bundle { 734 val data = new PtwRespS2withMemIdx 735 val vector = Output(Vec(Width, Bool())) 736 })) 737 738 def connect(normal: TlbPtwIOwithMemIdx): Unit = { 739 req <> normal.req 740 resp.ready := normal.resp.ready 741 normal.resp.bits := resp.bits.data 742 normal.resp.valid := resp.valid 743 } 744} 745 746/**************************** L2TLB *************************************/ 747abstract class PtwBundle(implicit p: Parameters) extends XSBundle with HasPtwConst 748abstract class PtwModule(outer: L2TLB) extends LazyModuleImp(outer) 749 with HasXSParameter with HasPtwConst 750 751class PteBundle(implicit p: Parameters) extends PtwBundle{ 752 val reserved = UInt(pteResLen.W) 753 val ppn_high = UInt(ppnHignLen.W) 754 val ppn = UInt(ppnLen.W) 755 val rsw = UInt(2.W) 756 val perm = new Bundle { 757 val d = Bool() 758 val a = Bool() 759 val g = Bool() 760 val u = Bool() 761 val x = Bool() 762 val w = Bool() 763 val r = Bool() 764 val v = Bool() 765 } 766 767 def unaligned(level: UInt) = { 768 isLeaf() && !(level === 2.U || 769 level === 1.U && ppn(vpnnLen-1, 0) === 0.U || 770 level === 0.U && ppn(vpnnLen*2-1, 0) === 0.U) 771 } 772 773 def isPf(level: UInt) = { 774 !perm.v || (!perm.r && perm.w) || unaligned(level) 775 } 776 777 // paddr of Xiangshan is 36 bits but ppn of sv39 is 44 bits 778 // access fault will be raised when ppn >> ppnLen is not zero 779 def isAf() = { 780 !(ppn_high === 0.U) 781 } 782 783 def isLeaf() = { 784 perm.r || perm.x || perm.w 785 } 786 787 def getPerm() = { 788 val pm = Wire(new PtePermBundle) 789 pm.d := perm.d 790 pm.a := perm.a 791 pm.g := perm.g 792 pm.u := perm.u 793 pm.x := perm.x 794 pm.w := perm.w 795 pm.r := perm.r 796 pm 797 } 798 799 override def toPrintable: Printable = { 800 p"ppn:0x${Hexadecimal(ppn)} perm:b${Binary(perm.asUInt)}" 801 } 802} 803 804class PtwEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwBundle { 805 val tag = UInt(tagLen.W) 806 val asid = UInt(asidLen.W) 807 val vmid = if (HasHExtension) Some(UInt(vmidLen.W)) else None 808 val ppn = UInt(ppnLen.W) 809 val perm = if (hasPerm) Some(new PtePermBundle) else None 810 val level = if (hasLevel) Some(UInt(log2Up(Level).W)) else None 811 val prefetch = Bool() 812 val v = Bool() 813 814 def is_normalentry(): Bool = { 815 if (!hasLevel) true.B 816 else level.get === 2.U 817 } 818 819 def genPPN(vpn: UInt): UInt = { 820 if (!hasLevel) ppn 821 else MuxLookup(level.get, 0.U)(Seq( 822 0.U -> Cat(ppn(ppn.getWidth-1, vpnnLen*2), vpn(vpnnLen*2-1, 0)), 823 1.U -> Cat(ppn(ppn.getWidth-1, vpnnLen), vpn(vpnnLen-1, 0)), 824 2.U -> ppn) 825 ) 826 } 827 828 //s2xlate control whether compare vmid or not 829 def hit(vpn: UInt, asid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false, s2xlate: Bool) = { 830 require(vpn.getWidth == vpnLen) 831// require(this.asid.getWidth <= asid.getWidth) 832 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 833 val vmid_hit = Mux(s2xlate, (this.vmid.getOrElse(0.U) === vmid), true.B) 834 if (allType) { 835 require(hasLevel) 836 val hit0 = tag(tagLen - 1, vpnnLen*2) === vpn(tagLen - 1, vpnnLen*2) 837 val hit1 = tag(vpnnLen*2 - 1, vpnnLen) === vpn(vpnnLen*2 - 1, vpnnLen) 838 val hit2 = tag(vpnnLen - 1, 0) === vpn(vpnnLen - 1, 0) 839 840 asid_hit && vmid_hit && Mux(level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) 841 } else if (hasLevel) { 842 val hit0 = tag(tagLen - 1, tagLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen) 843 val hit1 = tag(tagLen - vpnnLen - 1, tagLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2) 844 845 asid_hit && vmid_hit && Mux(level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) 846 } else { 847 asid_hit && vmid_hit && tag === vpn(vpnLen - 1, vpnLen - tagLen) 848 } 849 } 850 851 def refill(vpn: UInt, asid: UInt, vmid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) { 852 require(this.asid.getWidth <= asid.getWidth) // maybe equal is better, but ugly outside 853 854 tag := vpn(vpnLen - 1, vpnLen - tagLen) 855 ppn := pte.asTypeOf(new PteBundle().cloneType).ppn 856 perm.map(_ := pte.asTypeOf(new PteBundle().cloneType).perm) 857 this.asid := asid 858 this.vmid.map(_ := vmid) 859 this.prefetch := prefetch 860 this.v := valid 861 this.level.map(_ := level) 862 } 863 864 def genPtwEntry(vpn: UInt, asid: UInt, pte: UInt, level: UInt = 0.U, prefetch: Bool, valid: Bool = false.B) = { 865 val e = Wire(new PtwEntry(tagLen, hasPerm, hasLevel)) 866 e.refill(vpn, asid, pte, level, prefetch, valid) 867 e 868 } 869 870 871 872 override def toPrintable: Printable = { 873 // p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} perm:${perm}" 874 p"tag:0x${Hexadecimal(tag)} ppn:0x${Hexadecimal(ppn)} " + 875 (if (hasPerm) p"perm:${perm.getOrElse(0.U.asTypeOf(new PtePermBundle))} " else p"") + 876 (if (hasLevel) p"level:${level.getOrElse(0.U)}" else p"") + 877 p"prefetch:${prefetch}" 878 } 879} 880 881class PtwSectorEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwEntry(tagLen, hasPerm, hasLevel) { 882 override val ppn = UInt(sectorppnLen.W) 883} 884 885class PtwMergeEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwSectorEntry(tagLen, hasPerm, hasLevel) { 886 val ppn_low = UInt(sectortlbwidth.W) 887 val af = Bool() 888 val pf = Bool() 889} 890 891class HptwMergeEntry(tagLen: Int, hasPerm: Boolean = false, hasLevel: Boolean = false)(implicit p: Parameters) extends PtwMergeEntry(tagLen, hasPerm, hasLevel) { 892 val vmid = UInt(vmidLen.W) 893} 894 895class PtwEntries(num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 896 require(log2Up(num)==log2Down(num)) 897 // NOTE: hasPerm means that is leaf or not. 898 899 val tag = UInt(tagLen.W) 900 val asid = UInt(asidLen.W) 901 val vmid = if (HasHExtension) Some(UInt(vmidLen.W)) else None 902 val ppns = if (HasHExtension) Vec(num, UInt((vpnLen.max(ppnLen)).W)) else Vec(num, UInt(ppnLen.W)) 903 val vs = Vec(num, Bool()) 904 val perms = if (hasPerm) Some(Vec(num, new PtePermBundle)) else None 905 val prefetch = Bool() 906 // println(s"PtwEntries: tag:1*${tagLen} ppns:${num}*${ppnLen} vs:${num}*1") 907 // NOTE: vs is used for different usage: 908 // for l3, which store the leaf(leaves), vs is page fault or not. 909 // for l2, which shoule not store leaf, vs is valid or not, that will anticipate in hit check 910 // Because, l2 should not store leaf(no perm), it doesn't store perm. 911 // If l2 hit a leaf, the perm is still unavailble. Should still page walk. Complex but nothing helpful. 912 // TODO: divide vs into validVec and pfVec 913 // for l2: may valid but pf, so no need for page walk, return random pte with pf. 914 915 def tagClip(vpn: UInt) = { 916 require(vpn.getWidth == vpnLen) 917 vpn(vpnLen - 1, vpnLen - tagLen) 918 } 919 920 def sectorIdxClip(vpn: UInt, level: Int) = { 921 getVpnClip(vpn, level)(log2Up(num) - 1, 0) 922 } 923 924 def hit(vpn: UInt, asid: UInt, vmid:UInt, ignoreAsid: Boolean = false, s2xlate: Bool) = { 925 val asid_hit = if (ignoreAsid) true.B else (this.asid === asid) 926 val vmid_hit = Mux(s2xlate, this.vmid.getOrElse(0.U) === vmid, true.B) 927 asid_hit && vmid_hit && tag === tagClip(vpn) && (if (hasPerm) true.B else vs(sectorIdxClip(vpn, level))) 928 } 929 930 def genEntries(vpn: UInt, asid: UInt, vmid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 931 require((data.getWidth / XLEN) == num, 932 s"input data length must be multiple of pte length: data.length:${data.getWidth} num:${num}") 933 934 val ps = Wire(new PtwEntries(num, tagLen, level, hasPerm)) 935 ps.tag := tagClip(vpn) 936 ps.asid := asid 937 ps.vmid.map(_ := vmid) 938 ps.prefetch := prefetch 939 for (i <- 0 until num) { 940 val pte = data((i+1)*XLEN-1, i*XLEN).asTypeOf(new PteBundle) 941 ps.ppns(i) := pte.ppn 942 ps.vs(i) := !pte.isPf(levelUInt) && (if (hasPerm) pte.isLeaf() else !pte.isLeaf()) 943 ps.perms.map(_(i) := pte.perm) 944 } 945 ps 946 } 947 948 override def toPrintable: Printable = { 949 // require(num == 4, "if num is not 4, please comment this toPrintable") 950 // NOTE: if num is not 4, please comment this toPrintable 951 val permsInner = perms.getOrElse(0.U.asTypeOf(Vec(num, new PtePermBundle))) 952 p"asid: ${Hexadecimal(asid)} tag:0x${Hexadecimal(tag)} ppns:${printVec(ppns)} vs:${Binary(vs.asUInt)} " + 953 (if (hasPerm) p"perms:${printVec(permsInner)}" else p"") 954 } 955} 956 957class PTWEntriesWithEcc(eccCode: Code, num: Int, tagLen: Int, level: Int, hasPerm: Boolean)(implicit p: Parameters) extends PtwBundle { 958 val entries = new PtwEntries(num, tagLen, level, hasPerm) 959 960 val ecc_block = XLEN 961 val ecc_info = get_ecc_info() 962 val ecc = UInt(ecc_info._1.W) 963 964 def get_ecc_info(): (Int, Int, Int, Int) = { 965 val eccBits_per = eccCode.width(ecc_block) - ecc_block 966 967 val data_length = entries.getWidth 968 val data_align_num = data_length / ecc_block 969 val data_not_align = (data_length % ecc_block) != 0 // ugly code 970 val data_unalign_length = data_length - data_align_num * ecc_block 971 val eccBits_unalign = eccCode.width(data_unalign_length) - data_unalign_length 972 973 val eccBits = eccBits_per * data_align_num + eccBits_unalign 974 (eccBits, eccBits_per, data_align_num, data_unalign_length) 975 } 976 977 def encode() = { 978 val data = entries.asUInt 979 val ecc_slices = Wire(Vec(ecc_info._3, UInt(ecc_info._2.W))) 980 for (i <- 0 until ecc_info._3) { 981 ecc_slices(i) := eccCode.encode(data((i+1)*ecc_block-1, i*ecc_block)) >> ecc_block 982 } 983 if (ecc_info._4 != 0) { 984 val ecc_unaligned = eccCode.encode(data(data.getWidth-1, ecc_info._3*ecc_block)) >> ecc_info._4 985 ecc := Cat(ecc_unaligned, ecc_slices.asUInt) 986 } else { ecc := ecc_slices.asUInt } 987 } 988 989 def decode(): Bool = { 990 val data = entries.asUInt 991 val res = Wire(Vec(ecc_info._3 + 1, Bool())) 992 for (i <- 0 until ecc_info._3) { 993 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} 994 } 995 if (ecc_info._2 != 0 && ecc_info._4 != 0) { 996 res(ecc_info._3) := eccCode.decode( 997 Cat(ecc(ecc_info._1-1, ecc_info._2*ecc_info._3), data(data.getWidth-1, ecc_info._3*ecc_block))).error 998 } else { res(ecc_info._3) := false.B } 999 1000 Cat(res).orR 1001 } 1002 1003 def gen(vpn: UInt, asid: UInt, vmid: UInt, data: UInt, levelUInt: UInt, prefetch: Bool) = { 1004 this.entries := entries.genEntries(vpn, asid, vmid, data, levelUInt, prefetch) 1005 this.encode() 1006 } 1007} 1008 1009class PtwReq(implicit p: Parameters) extends PtwBundle { 1010 val vpn = UInt(vpnLen.W) //vpn or gvpn 1011 val s2xlate = UInt(2.W) // 0 bit: s2xlate, 1 bit: stage 1 or stage 2 1012 def hasS2xlate(): Bool = { 1013 this.s2xlate =/= noS2xlate 1014 } 1015 override def toPrintable: Printable = { 1016 p"vpn:0x${Hexadecimal(vpn)}" 1017 } 1018} 1019 1020class PtwReqwithMemIdx(implicit p: Parameters) extends PtwReq { 1021 val memidx = new MemBlockidxBundle 1022} 1023 1024class PtwResp(implicit p: Parameters) extends PtwBundle { 1025 val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true) 1026 val pf = Bool() 1027 val af = Bool() 1028 1029 def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt) = { 1030 this.entry.level.map(_ := level) 1031 this.entry.tag := vpn 1032 this.entry.perm.map(_ := pte.getPerm()) 1033 this.entry.ppn := pte.ppn 1034 this.entry.prefetch := DontCare 1035 this.entry.asid := asid 1036 this.entry.v := !pf 1037 this.pf := pf 1038 this.af := af 1039 } 1040 1041 override def toPrintable: Printable = { 1042 p"entry:${entry} pf:${pf} af:${af}" 1043 } 1044} 1045 1046class HptwResp(implicit p: Parameters) extends PtwBundle { 1047 val entry = new PtwEntry(tagLen = vpnLen, hasPerm = true, hasLevel = true) 1048 val gpf = Bool() 1049 val gaf = Bool() 1050 1051 def apply(gpf: Bool, gaf: Bool, level: UInt, pte: PteBundle, vpn: UInt, vmid: UInt) = { 1052 this.entry.level.map(_ := level) 1053 this.entry.tag := vpn 1054 this.entry.perm.map(_ := pte.getPerm()) 1055 this.entry.ppn := pte.ppn 1056 this.entry.prefetch := DontCare 1057 this.entry.asid := DontCare 1058 this.entry.vmid.map(_ := vmid) 1059 this.entry.v := !gpf 1060 this.gpf := gpf 1061 this.gaf := gaf 1062 } 1063 1064 def genPPNS2(): UInt = { 1065 MuxLookup(entry.level.get, 0.U, Seq( 1066 0.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen * 2), entry.tag(vpnnLen * 2 - 1, 0)), 1067 1.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, vpnnLen), entry.tag(vpnnLen - 1, 0)), 1068 2.U -> Cat(entry.ppn(entry.ppn.getWidth - 1, 0)) 1069 )) 1070 } 1071 1072 def hit(gvpn: UInt, vmid: UInt): Bool = { 1073 val vmid_hit = this.entry.vmid.getOrElse(0.U) === vmid 1074 val hit0 = entry.tag(vpnLen - 1, vpnnLen * 2) === gvpn(vpnLen - 1, vpnnLen * 2) 1075 val hit1 = entry.tag(vpnnLen * 2 - 1, vpnnLen) === gvpn(vpnnLen * 2 - 1, vpnnLen) 1076 val hit2 = entry.tag(vpnnLen - 1, 0) === gvpn(vpnnLen - 1, 0) 1077 vmid_hit && Mux(entry.level.getOrElse(0.U) === 2.U, hit2 && hit1 && hit0, Mux(entry.level.getOrElse(0.U) === 1.U, hit1 && hit0, hit0)) 1078 } 1079} 1080 1081class PtwResptomerge (implicit p: Parameters) extends PtwBundle { 1082 val entry = UInt(blockBits.W) 1083 val vpn = UInt(vpnLen.W) 1084 val level = UInt(log2Up(Level).W) 1085 val pf = Bool() 1086 val af = Bool() 1087 val asid = UInt(asidLen.W) 1088 1089 def apply(pf: Bool, af: Bool, level: UInt, pte: UInt, vpn: UInt, asid: UInt) = { 1090 this.entry := pte 1091 this.pf := pf 1092 this.af := af 1093 this.level := level 1094 this.vpn := vpn 1095 this.asid := asid 1096 } 1097 1098 override def toPrintable: Printable = { 1099 p"entry:${entry} pf:${pf} af:${af}" 1100 } 1101} 1102 1103class PtwRespwithMemIdx(implicit p: Parameters) extends PtwResp { 1104 val memidx = new MemBlockidxBundle 1105} 1106 1107class PtwSectorRespwithMemIdx(implicit p: Parameters) extends PtwSectorResp { 1108 val memidx = new MemBlockidxBundle 1109} 1110 1111class PtwSectorResp(implicit p: Parameters) extends PtwBundle { 1112 val entry = new PtwSectorEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true) 1113 val addr_low = UInt(sectortlbwidth.W) 1114 val ppn_low = Vec(tlbcontiguous, UInt(sectortlbwidth.W)) 1115 val valididx = Vec(tlbcontiguous, Bool()) 1116 val pteidx = Vec(tlbcontiguous, Bool()) 1117 val pf = Bool() 1118 val af = Bool() 1119 1120 1121 def genPPN(vpn: UInt): UInt = { 1122 MuxLookup(entry.level.get, 0.U)(Seq( 1123 0.U -> Cat(entry.ppn(entry.ppn.getWidth-1, vpnnLen * 2 - sectortlbwidth), vpn(vpnnLen*2-1, 0)), 1124 1.U -> Cat(entry.ppn(entry.ppn.getWidth-1, vpnnLen - sectortlbwidth), vpn(vpnnLen-1, 0)), 1125 2.U -> Cat(entry.ppn(entry.ppn.getWidth-1, 0), ppn_low(vpn(sectortlbwidth - 1, 0)))) 1126 ) 1127 } 1128 1129 def hit(vpn: UInt, asid: UInt, vmid: UInt, allType: Boolean = false, ignoreAsid: Boolean = false, s2xlate: Bool): Bool = { 1130 require(vpn.getWidth == vpnLen) 1131 // require(this.asid.getWidth <= asid.getWidth) 1132 val asid_hit = if (ignoreAsid) true.B else (this.entry.asid === asid) 1133 val vmid_hit = Mux(s2xlate, this.entry.vmid.getOrElse(0.U) === vmid, true.B) 1134 if (allType) { 1135 val hit0 = entry.tag(sectorvpnLen - 1, vpnnLen * 2 - sectortlbwidth) === vpn(vpnLen - 1, vpnnLen * 2) 1136 val hit1 = entry.tag(vpnnLen * 2 - sectortlbwidth - 1, vpnnLen - sectortlbwidth) === vpn(vpnnLen * 2 - 1, vpnnLen) 1137 val hit2 = entry.tag(vpnnLen - sectortlbwidth - 1, 0) === vpn(vpnnLen - 1, sectortlbwidth) 1138 val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0)) 1139 1140 asid_hit && vmid_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 1141 } else { 1142 val hit0 = entry.tag(sectorvpnLen - 1, sectorvpnLen - vpnnLen) === vpn(vpnLen - 1, vpnLen - vpnnLen) 1143 val hit1 = entry.tag(sectorvpnLen - vpnnLen - 1, sectorvpnLen - vpnnLen * 2) === vpn(vpnLen - vpnnLen - 1, vpnLen - vpnnLen * 2) 1144 val addr_low_hit = valididx(vpn(sectortlbwidth - 1, 0)) 1145 1146 asid_hit && vmid_hit && Mux(entry.level.getOrElse(0.U) === 0.U, hit0, hit0 && hit1) && addr_low_hit 1147 } 1148 } 1149} 1150 1151class PtwMergeResp(implicit p: Parameters) extends PtwBundle { 1152 val entry = Vec(tlbcontiguous, new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)) 1153 val pteidx = Vec(tlbcontiguous, Bool()) 1154 val not_super = Bool() 1155 1156 def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, asid: UInt, vmid:UInt, addr_low : UInt, not_super : Boolean = true) = { 1157 assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!") 1158 1159 val ptw_resp = Wire(new PtwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)) 1160 ptw_resp.ppn := pte.ppn(ppnLen - 1, sectortlbwidth) 1161 ptw_resp.ppn_low := pte.ppn(sectortlbwidth - 1, 0) 1162 ptw_resp.level.map(_ := level) 1163 ptw_resp.perm.map(_ := pte.getPerm()) 1164 ptw_resp.tag := vpn(vpnLen - 1, sectortlbwidth) 1165 ptw_resp.pf := pf 1166 ptw_resp.af := af 1167 ptw_resp.v := !pf 1168 ptw_resp.prefetch := DontCare 1169 ptw_resp.asid := asid 1170 this.pteidx := UIntToOH(addr_low).asBools 1171 this.not_super := not_super.B 1172 1173 1174 for (i <- 0 until tlbcontiguous) { 1175 this.entry(i) := ptw_resp 1176 } 1177 } 1178} 1179 1180class HptwMergeResp(implicit p: Parameters) extends PtwBundle { 1181 val entry = Vec(tlbcontiguous, new HptwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)) 1182 val pteidx = Vec(tlbcontiguous, Bool()) 1183 val not_super = Bool() 1184 1185 def genPPN(): UInt = { 1186 val idx = OHToUInt(pteidx) 1187 MuxLookup(entry(idx).level.get, 0.U, Seq( 1188 0.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen * 2 - sectortlbwidth), entry(idx).tag(vpnnLen * 2 - 1, 0)), 1189 1.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, vpnnLen - sectortlbwidth), entry(idx).tag(vpnnLen - 1, 0)), 1190 2.U -> Cat(entry(idx).ppn(entry(idx).ppn.getWidth - 1, 0), entry(idx).ppn_low)) 1191 ) 1192 } 1193 1194 def isAf(): Bool = { 1195 val idx = OHToUInt(pteidx) 1196 entry(idx).af 1197 } 1198 1199 def isPf(): Bool = { 1200 val idx = OHToUInt(pteidx) 1201 entry(idx).pf 1202 } 1203 1204 def MergeRespToPte(): PteBundle = { 1205 val idx = OHToUInt(pteidx) 1206 val resp = Wire(new PteBundle()) 1207 resp.ppn := Cat(entry(idx).ppn, entry(idx).ppn_low) 1208 resp.perm := entry(idx).perm.getOrElse(0.U) 1209 resp 1210 } 1211 1212 def apply(pf: Bool, af: Bool, level: UInt, pte: PteBundle, vpn: UInt, vmid: UInt, addr_low: UInt, not_super: Boolean = true) = { 1213 assert(tlbcontiguous == 8, "Only support tlbcontiguous = 8!") 1214 1215 val ptw_resp = Wire(new HptwMergeEntry(tagLen = sectorvpnLen, hasPerm = true, hasLevel = true)) 1216 ptw_resp.ppn := pte.ppn(ppnLen - 1, sectortlbwidth) 1217 ptw_resp.ppn_low := pte.ppn(sectortlbwidth - 1, 0) 1218 ptw_resp.level.map(_ := level) 1219 ptw_resp.perm.map(_ := pte.getPerm()) 1220 ptw_resp.tag := vpn(vpnLen - 1, sectortlbwidth) 1221 ptw_resp.pf := pf 1222 ptw_resp.af := af 1223 ptw_resp.v := !pf 1224 ptw_resp.prefetch := DontCare 1225 ptw_resp.vmid := vmid 1226 this.pteidx := UIntToOH(addr_low).asBools 1227 this.not_super := not_super.B 1228 1229 1230 for (i <- 0 until tlbcontiguous) { 1231 this.entry(i) := ptw_resp 1232 } 1233 } 1234} 1235 1236class PtwRespS2(implicit p: Parameters) extends PtwBundle { 1237 val s2xlate = UInt(2.W) 1238 val s1 = new PtwSectorResp() 1239 val s2 = new HptwResp() 1240 def genPPNS2(i: Int):UInt = { 1241 val s1ppn = Cat(this.s1.entry.ppn, this.s1.ppn_low(i), 0.U(12.W)).asUInt 1242 val s2ppn = this.s2.entry.ppn 1243 Mux(s2xlate =/= noS2xlate, s2ppn, s1ppn) 1244 } 1245} 1246 1247class PtwRespS2withMemIdx(implicit p: Parameters) extends PtwRespS2 { 1248 val memidx = new MemBlockidxBundle() 1249} 1250 1251class L2TLBIO(implicit p: Parameters) extends PtwBundle { 1252 val hartId = Input(UInt(hartIdLen.W)) 1253 val tlb = Vec(PtwWidth, Flipped(new TlbPtwIO)) 1254 val sfence = Input(new SfenceBundle) 1255 val csr = new Bundle { 1256 val tlb = Input(new TlbCsrBundle) 1257 val distribute_csr = Flipped(new DistributedCSRIO) 1258 } 1259} 1260 1261class L2TlbMemReqBundle(implicit p: Parameters) extends PtwBundle { 1262 val addr = UInt(PAddrBits.W) 1263 val id = UInt(bMemID.W) 1264} 1265 1266class L2TlbInnerBundle(implicit p: Parameters) extends PtwReq { 1267 val source = UInt(bSourceWidth.W) 1268} 1269 1270 1271object ValidHoldBypass{ 1272 def apply(infire: Bool, outfire: Bool, flush: Bool = false.B) = { 1273 val valid = RegInit(false.B) 1274 when (infire) { valid := true.B } 1275 when (outfire) { valid := false.B } // ATTENTION: order different with ValidHold 1276 when (flush) { valid := false.B } // NOTE: the flush will flush in & out, is that ok? 1277 valid || infire 1278 } 1279} 1280 1281class L1TlbDB(implicit p: Parameters) extends TlbBundle { 1282 val vpn = UInt(vpnLen.W) 1283} 1284 1285class PageCacheDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 1286 val vpn = UInt(vpnLen.W) 1287 val source = UInt(bSourceWidth.W) 1288 val bypassed = Bool() 1289 val is_first = Bool() 1290 val prefetched = Bool() 1291 val prefetch = Bool() 1292 val l2Hit = Bool() 1293 val l1Hit = Bool() 1294 val hit = Bool() 1295} 1296 1297class PTWDB(implicit p: Parameters) extends TlbBundle with HasPtwConst { 1298 val vpn = UInt(vpnLen.W) 1299 val source = UInt(bSourceWidth.W) 1300} 1301 1302class L2TlbPrefetchDB(implicit p: Parameters) extends TlbBundle { 1303 val vpn = UInt(vpnLen.W) 1304} 1305 1306class L2TlbMissQueueDB(implicit p: Parameters) extends TlbBundle { 1307 val vpn = UInt(vpnLen.W) 1308} 1309