xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/L2TlbPrefetch.scala (revision 3c02ee8f82edea481fa8336c7f54ffc17fafba91)
1bc063562SLemover/***************************************************************************************
2bc063562SLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3bc063562SLemover* Copyright (c) 2020-2021 Peng Cheng Laboratory
4bc063562SLemover*
5bc063562SLemover* XiangShan is licensed under Mulan PSL v2.
6bc063562SLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
7bc063562SLemover* You may obtain a copy of Mulan PSL v2 at:
8bc063562SLemover*          http://license.coscl.org.cn/MulanPSL2
9bc063562SLemover*
10bc063562SLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11bc063562SLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12bc063562SLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13bc063562SLemover*
14bc063562SLemover* See the Mulan PSL v2 for more details.
15bc063562SLemover***************************************************************************************/
16bc063562SLemoverpackage xiangshan.cache.mmu
17bc063562SLemover
18bc063562SLemoverimport chisel3._
19bc063562SLemoverimport chisel3.util._
20bc063562SLemoverimport chipsalliance.rocketchip.config.Parameters
21bc063562SLemoverimport xiangshan.{SfenceBundle, XSModule}
22bc063562SLemoverimport utils._
23*3c02ee8fSwakafaimport utility._
24bc063562SLemover
2545f497a4Shappy-lxclass L2TlbPrefetchIO(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
26bc063562SLemover  val in = Flipped(ValidIO(new Bundle {
27bc063562SLemover    val vpn = UInt(vpnLen.W)
28bc063562SLemover  }))
29bc063562SLemover  val out = DecoupledIO(new Bundle {
30bc063562SLemover    val vpn = UInt(vpnLen.W)
31bc063562SLemover    val source = UInt(bSourceWidth.W)
32bc063562SLemover  })
33bc063562SLemover}
34bc063562SLemover
35bc063562SLemoverclass L2TlbPrefetch(implicit p: Parameters) extends XSModule with HasPtwConst {
36bc063562SLemover  val io = IO(new L2TlbPrefetchIO())
37bc063562SLemover
387797f035SbugGenerator  val OldRecordSize = 4
397797f035SbugGenerator  val old_reqs = Reg(Vec(OldRecordSize, UInt(vpnLen.W)))
405afdf73cSHaoyuan Feng  val old_v = RegInit(VecInit(Seq.fill(OldRecordSize)(false.B)))
417797f035SbugGenerator  val old_index = RegInit(0.U(log2Ceil(OldRecordSize).W))
427797f035SbugGenerator
437797f035SbugGenerator  def already_have(vpn: UInt): Bool = {
447797f035SbugGenerator    Cat(old_reqs.zip(old_v).map{ case (o,v) => dup(o,vpn) && v}).orR
457797f035SbugGenerator  }
467797f035SbugGenerator
4745f497a4Shappy-lx  val flush = io.sfence.valid || io.csr.satp.changed
487797f035SbugGenerator  val next_line = get_next_line(io.in.bits.vpn)
497797f035SbugGenerator  val next_req = RegEnable(next_line, io.in.valid)
507797f035SbugGenerator  val input_valid = io.in.valid && !flush && !already_have(next_line)
517797f035SbugGenerator  val v = ValidHold(input_valid, io.out.fire(), flush)
52bc063562SLemover
53bc063562SLemover  io.out.valid := v
547797f035SbugGenerator  io.out.bits.vpn := next_req
55bc063562SLemover  io.out.bits.source := prefetchID.U
567797f035SbugGenerator
577797f035SbugGenerator  when (io.out.fire) {
587797f035SbugGenerator    old_v(old_index) := true.B
597797f035SbugGenerator    old_reqs(old_index) := next_req
607797f035SbugGenerator    old_index := Mux((old_index === (OldRecordSize-1).U), 0.U, old_index + 1.U)
617797f035SbugGenerator  }
627797f035SbugGenerator
637797f035SbugGenerator  when (flush) {
647797f035SbugGenerator    old_v.map(_ := false.B)
657797f035SbugGenerator  }
667797f035SbugGenerator
675afdf73cSHaoyuan Feng  XSPerfAccumulate("l2tlb_prefetch_input_count", io.in.valid)
685afdf73cSHaoyuan Feng  XSPerfAccumulate("l2tlb_prefetch_valid_count", input_valid)
697797f035SbugGenerator  XSPerfAccumulate("l2tlb_prefetch_output_count", io.out.fire())
70bc063562SLemover}
71