xref: /XiangShan/src/main/scala/xiangshan/frontend/icache/WayLookup.scala (revision cf7d6b7a1a781c73aeb87de112de2e7fe5ea3b7c)
1b92f8445Sssszwic/***************************************************************************************
2b92f8445Sssszwic  * Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3b92f8445Sssszwic  * Copyright (c) 2020-2021 Peng Cheng Laboratory
4b92f8445Sssszwic  *
5b92f8445Sssszwic  * XiangShan is licensed under Mulan PSL v2.
6b92f8445Sssszwic  * You can use this software according to the terms and conditions of the Mulan PSL v2.
7b92f8445Sssszwic  * You may obtain a copy of Mulan PSL v2 at:
8b92f8445Sssszwic  *          http://license.coscl.org.cn/MulanPSL2
9b92f8445Sssszwic  *
10b92f8445Sssszwic  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11b92f8445Sssszwic  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12b92f8445Sssszwic  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13b92f8445Sssszwic  *
14b92f8445Sssszwic  * See the Mulan PSL v2 for more details.
15b92f8445Sssszwic  ***************************************************************************************/
16b92f8445Sssszwic
17b92f8445Sssszwicpackage xiangshan.frontend.icache
18b92f8445Sssszwic
19b92f8445Sssszwicimport chisel3._
20b92f8445Sssszwicimport chisel3.util._
21*cf7d6b7aSMuziimport org.chipsalliance.cde.config.Parameters
22b92f8445Sssszwicimport utility._
23002c10a4SYanqin Liimport xiangshan.cache.mmu.Pbmt
24*cf7d6b7aSMuziimport xiangshan.frontend.ExceptionType
25b92f8445Sssszwic
2691946104Sxu_zh/* WayLookupEntry is for internal storage, while WayLookupInfo is for interface
2791946104Sxu_zh * Notes:
2891946104Sxu_zh *   1. there must be a flush (caused by guest page fault) after excp_tlb_gpf === true.B,
2991946104Sxu_zh *      so, we need only the first excp_tlb_gpf and the corresponding gpaddr.
3091946104Sxu_zh *      to save area, we separate those signals from WayLookupEntry and store only once.
3191946104Sxu_zh */
3291946104Sxu_zhclass WayLookupEntry(implicit p: Parameters) extends ICacheBundle {
3391946104Sxu_zh  val vSetIdx:        Vec[UInt] = Vec(PortNumber, UInt(idxBits.W))
3491946104Sxu_zh  val waymask:        Vec[UInt] = Vec(PortNumber, UInt(nWays.W))
3591946104Sxu_zh  val ptag:           Vec[UInt] = Vec(PortNumber, UInt(tagBits.W))
3688895b11Sxu_zh  val itlb_exception: Vec[UInt] = Vec(PortNumber, UInt(ExceptionType.width.W))
37002c10a4SYanqin Li  val itlb_pbmt:      Vec[UInt] = Vec(PortNumber, UInt(Pbmt.width.W))
388966a895Sxu_zh  val meta_codes:     Vec[UInt] = Vec(PortNumber, UInt(ICacheMetaCodeBits.W))
3991946104Sxu_zh}
4091946104Sxu_zh
4191946104Sxu_zhclass WayLookupGPFEntry(implicit p: Parameters) extends ICacheBundle {
4291946104Sxu_zh  val gpaddr:            UInt = UInt(GPAddrBits.W)
43ad415ae0SXiaokun-Pei  val isForVSnonLeafPTE: Bool = Bool()
4491946104Sxu_zh}
4591946104Sxu_zh
46b92f8445Sssszwicclass WayLookupInfo(implicit p: Parameters) extends ICacheBundle {
4791946104Sxu_zh  val entry = new WayLookupEntry
4891946104Sxu_zh  val gpf   = new WayLookupGPFEntry
4991946104Sxu_zh
5091946104Sxu_zh  // for compatibility
5191946104Sxu_zh  def vSetIdx:           Vec[UInt] = entry.vSetIdx
5291946104Sxu_zh  def waymask:           Vec[UInt] = entry.waymask
5391946104Sxu_zh  def ptag:              Vec[UInt] = entry.ptag
5488895b11Sxu_zh  def itlb_exception:    Vec[UInt] = entry.itlb_exception
55002c10a4SYanqin Li  def itlb_pbmt:         Vec[UInt] = entry.itlb_pbmt
568966a895Sxu_zh  def meta_codes:        Vec[UInt] = entry.meta_codes
5791946104Sxu_zh  def gpaddr:            UInt      = gpf.gpaddr
58ad415ae0SXiaokun-Pei  def isForVSnonLeafPTE: Bool      = gpf.isForVSnonLeafPTE
59b92f8445Sssszwic}
60b92f8445Sssszwic
61b92f8445Sssszwicclass WayLookupInterface(implicit p: Parameters) extends ICacheBundle {
62b92f8445Sssszwic  val flush  = Input(Bool())
63b92f8445Sssszwic  val read   = DecoupledIO(new WayLookupInfo)
64b92f8445Sssszwic  val write  = Flipped(DecoupledIO(new WayLookupInfo))
65b92f8445Sssszwic  val update = Flipped(ValidIO(new ICacheMissResp))
66b92f8445Sssszwic}
67b92f8445Sssszwic
68b92f8445Sssszwicclass WayLookup(implicit p: Parameters) extends ICacheModule {
6991946104Sxu_zh  val io: WayLookupInterface = IO(new WayLookupInterface)
70b92f8445Sssszwic
71b92f8445Sssszwic  class WayLookupPtr(implicit p: Parameters) extends CircularQueuePtr[WayLookupPtr](nWayLookupSize)
7291946104Sxu_zh  private object WayLookupPtr {
73b92f8445Sssszwic    def apply(f: Bool, v: UInt)(implicit p: Parameters): WayLookupPtr = {
74b92f8445Sssszwic      val ptr = Wire(new WayLookupPtr)
75b92f8445Sssszwic      ptr.flag  := f
76b92f8445Sssszwic      ptr.value := v
77b92f8445Sssszwic      ptr
78b92f8445Sssszwic    }
79b92f8445Sssszwic  }
80b92f8445Sssszwic
8191946104Sxu_zh  private val entries  = RegInit(VecInit(Seq.fill(nWayLookupSize)(0.U.asTypeOf(new WayLookupEntry))))
8291946104Sxu_zh  private val readPtr  = RegInit(WayLookupPtr(false.B, 0.U))
8391946104Sxu_zh  private val writePtr = RegInit(WayLookupPtr(false.B, 0.U))
84b92f8445Sssszwic
8591946104Sxu_zh  private val empty = readPtr === writePtr
8691946104Sxu_zh  private val full  = (readPtr.value === writePtr.value) && (readPtr.flag ^ writePtr.flag)
87b92f8445Sssszwic
88b92f8445Sssszwic  when(io.flush) {
89b92f8445Sssszwic    writePtr.value := 0.U
90b92f8445Sssszwic    writePtr.flag  := false.B
91b92f8445Sssszwic  }.elsewhen(io.write.fire) {
92b92f8445Sssszwic    writePtr := writePtr + 1.U
93b92f8445Sssszwic  }
94b92f8445Sssszwic
95b92f8445Sssszwic  when(io.flush) {
96b92f8445Sssszwic    readPtr.value := 0.U
97b92f8445Sssszwic    readPtr.flag  := false.B
98b92f8445Sssszwic  }.elsewhen(io.read.fire) {
99b92f8445Sssszwic    readPtr := readPtr + 1.U
100b92f8445Sssszwic  }
101b92f8445Sssszwic
10288895b11Sxu_zh  private val gpf_entry = RegInit(0.U.asTypeOf(Valid(new WayLookupGPFEntry)))
10391946104Sxu_zh  private val gpfPtr    = RegInit(WayLookupPtr(false.B, 0.U))
104b7a4433dSxu_zh  private val gpf_hit   = gpfPtr === readPtr && gpf_entry.valid
10591946104Sxu_zh
10691946104Sxu_zh  when(io.flush) {
10791946104Sxu_zh    // we don't need to reset gpfPtr, since the valid is actually gpf_entries.excp_tlb_gpf
10888895b11Sxu_zh    gpf_entry.valid := false.B
10988895b11Sxu_zh    gpf_entry.bits  := 0.U.asTypeOf(new WayLookupGPFEntry)
11091946104Sxu_zh  }
11191946104Sxu_zh
112b92f8445Sssszwic  /**
113b92f8445Sssszwic    ******************************************************************************
114b92f8445Sssszwic    * update
115b92f8445Sssszwic    ******************************************************************************
116b92f8445Sssszwic    */
11791946104Sxu_zh  private val hits = Wire(Vec(nWayLookupSize, Bool()))
118b92f8445Sssszwic  entries.zip(hits).foreach { case (entry, hit) =>
119b92f8445Sssszwic    val hit_vec = Wire(Vec(PortNumber, Bool()))
120b92f8445Sssszwic    (0 until PortNumber).foreach { i =>
121b92f8445Sssszwic      val vset_same = (io.update.bits.vSetIdx === entry.vSetIdx(i)) && !io.update.bits.corrupt && io.update.valid
122b92f8445Sssszwic      val ptag_same = getPhyTagFromBlk(io.update.bits.blkPaddr) === entry.ptag(i)
123b92f8445Sssszwic      val way_same  = io.update.bits.waymask === entry.waymask(i)
124b92f8445Sssszwic      when(vset_same) {
125b92f8445Sssszwic        when(ptag_same) {
126b92f8445Sssszwic          // miss -> hit
127b92f8445Sssszwic          entry.waymask(i) := io.update.bits.waymask
1288966a895Sxu_zh          // also update meta_codes
1295ce94708Sxu_zh          // we have getPhyTagFromBlk(io.update.bits.blkPaddr) === entry.ptag(i), so we can use entry.ptag(i) for better timing
1305ce94708Sxu_zh          entry.meta_codes(i) := encodeMetaECC(entry.ptag(i))
131b92f8445Sssszwic        }.elsewhen(way_same) {
132b92f8445Sssszwic          // data is overwritten: hit -> miss
133b92f8445Sssszwic          entry.waymask(i) := 0.U
1345ce94708Sxu_zh          // dont care meta_codes, since it's not used for a missed request
135b92f8445Sssszwic        }
136b92f8445Sssszwic      }
137b92f8445Sssszwic      hit_vec(i) := vset_same && (ptag_same || way_same)
138b92f8445Sssszwic    }
139b92f8445Sssszwic    hit := hit_vec.reduce(_ || _)
140b92f8445Sssszwic  }
141b92f8445Sssszwic
142b92f8445Sssszwic  /**
143b92f8445Sssszwic    ******************************************************************************
144b92f8445Sssszwic    * read
145b92f8445Sssszwic    ******************************************************************************
146b92f8445Sssszwic    */
147b7a4433dSxu_zh  // if the entry is empty, but there is a valid write, we can bypass it to read port (maybe timing critical)
148b7a4433dSxu_zh  private val can_bypass = empty && io.write.valid
149b92f8445Sssszwic  io.read.valid := !empty || io.write.valid
150b7a4433dSxu_zh  when(can_bypass) {
15191946104Sxu_zh    io.read.bits := io.write.bits
152b7a4433dSxu_zh  }.otherwise { // can't bypass
15391946104Sxu_zh    io.read.bits.entry := entries(readPtr.value)
154b7a4433dSxu_zh    when(gpf_hit) { // ptr match && entry valid
155b7a4433dSxu_zh      io.read.bits.gpf := gpf_entry.bits
156b7a4433dSxu_zh      // also clear gpf_entry.valid when it's read, note this will be override by write (L175)
157b7a4433dSxu_zh      when(io.read.fire) {
158b7a4433dSxu_zh        gpf_entry.valid := false.B
159b7a4433dSxu_zh      }
160b7a4433dSxu_zh    }.otherwise { // gpf not hit
161b7a4433dSxu_zh      io.read.bits.gpf := 0.U.asTypeOf(new WayLookupGPFEntry)
162b7a4433dSxu_zh    }
16391946104Sxu_zh  }
164b92f8445Sssszwic
165b92f8445Sssszwic  /**
166b92f8445Sssszwic    ******************************************************************************
167b92f8445Sssszwic    * write
168b92f8445Sssszwic    ******************************************************************************
169b92f8445Sssszwic    */
170b7a4433dSxu_zh  // if there is a valid gpf to be read, we should stall the write
171b7a4433dSxu_zh  private val gpf_stall = gpf_entry.valid && !(io.read.fire && gpf_hit)
172b7a4433dSxu_zh  io.write.ready := !full && !gpf_stall
173b92f8445Sssszwic  when(io.write.fire) {
17491946104Sxu_zh    entries(writePtr.value) := io.write.bits.entry
175b7a4433dSxu_zh    when(io.write.bits.itlb_exception.map(_ === ExceptionType.gpf).reduce(_ || _)) {
176b7a4433dSxu_zh      // if gpf_entry is bypassed, we don't need to save it
177b7a4433dSxu_zh      // note this will override the read (L156)
178b7a4433dSxu_zh      gpf_entry.valid := !(can_bypass && io.read.fire)
17988895b11Sxu_zh      gpf_entry.bits  := io.write.bits.gpf
18091946104Sxu_zh      gpfPtr          := writePtr
18191946104Sxu_zh    }
182b92f8445Sssszwic  }
183b92f8445Sssszwic}
184