xref: /XiangShan/src/main/scala/xiangshan/cache/mmu/L2TlbPrefetch.scala (revision 5afdf73c2fdaffb2f720bbfe5d35cf7f5f914b82)
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._
23bc063562SLemover
2445f497a4Shappy-lxclass L2TlbPrefetchIO(implicit p: Parameters) extends MMUIOBaseBundle with HasPtwConst {
25bc063562SLemover  val in = Flipped(ValidIO(new Bundle {
26bc063562SLemover    val vpn = UInt(vpnLen.W)
27bc063562SLemover  }))
28bc063562SLemover  val out = DecoupledIO(new Bundle {
29bc063562SLemover    val vpn = UInt(vpnLen.W)
30bc063562SLemover    val source = UInt(bSourceWidth.W)
31bc063562SLemover  })
32bc063562SLemover}
33bc063562SLemover
34bc063562SLemoverclass L2TlbPrefetch(implicit p: Parameters) extends XSModule with HasPtwConst {
35bc063562SLemover  val io = IO(new L2TlbPrefetchIO())
36bc063562SLemover
377797f035SbugGenerator  val OldRecordSize = 4
387797f035SbugGenerator  val old_reqs = Reg(Vec(OldRecordSize, UInt(vpnLen.W)))
39*5afdf73cSHaoyuan Feng  val old_v = RegInit(VecInit(Seq.fill(OldRecordSize)(false.B)))
407797f035SbugGenerator  val old_index = RegInit(0.U(log2Ceil(OldRecordSize).W))
417797f035SbugGenerator
427797f035SbugGenerator  def already_have(vpn: UInt): Bool = {
437797f035SbugGenerator    Cat(old_reqs.zip(old_v).map{ case (o,v) => dup(o,vpn) && v}).orR
447797f035SbugGenerator  }
457797f035SbugGenerator
4645f497a4Shappy-lx  val flush = io.sfence.valid || io.csr.satp.changed
477797f035SbugGenerator  val next_line = get_next_line(io.in.bits.vpn)
487797f035SbugGenerator  val next_req = RegEnable(next_line, io.in.valid)
497797f035SbugGenerator  val input_valid = io.in.valid && !flush && !already_have(next_line)
507797f035SbugGenerator  val v = ValidHold(input_valid, io.out.fire(), flush)
51bc063562SLemover
52bc063562SLemover  io.out.valid := v
537797f035SbugGenerator  io.out.bits.vpn := next_req
54bc063562SLemover  io.out.bits.source := prefetchID.U
557797f035SbugGenerator
567797f035SbugGenerator  when (io.out.fire) {
577797f035SbugGenerator    old_v(old_index) := true.B
587797f035SbugGenerator    old_reqs(old_index) := next_req
597797f035SbugGenerator    old_index := Mux((old_index === (OldRecordSize-1).U), 0.U, old_index + 1.U)
607797f035SbugGenerator  }
617797f035SbugGenerator
627797f035SbugGenerator  when (flush) {
637797f035SbugGenerator    old_v.map(_ := false.B)
647797f035SbugGenerator  }
657797f035SbugGenerator
66*5afdf73cSHaoyuan Feng  XSPerfAccumulate("l2tlb_prefetch_input_count", io.in.valid)
67*5afdf73cSHaoyuan Feng  XSPerfAccumulate("l2tlb_prefetch_valid_count", input_valid)
687797f035SbugGenerator  XSPerfAccumulate("l2tlb_prefetch_output_count", io.out.fire())
69bc063562SLemover}
70