xref: /XiangShan/src/main/scala/xiangshan/frontend/icache/WayLookup.scala (revision 002c10a4fe8276bf0c48ff55808d87308a284882)
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 org.chipsalliance.cde.config.Parameters
20b92f8445Sssszwicimport chisel3._
21b92f8445Sssszwicimport chisel3.util._
22b92f8445Sssszwicimport utility._
2388895b11Sxu_zhimport xiangshan.frontend.ExceptionType
24*002c10a4SYanqin Liimport xiangshan.cache.mmu.Pbmt
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))
37*002c10a4SYanqin Li  val itlb_pbmt      : Vec[UInt] = Vec(PortNumber, UInt(Pbmt.width.W))
3888895b11Sxu_zh  val meta_corrupt   : Vec[Bool] = Vec(PortNumber, Bool())
3991946104Sxu_zh}
4091946104Sxu_zh
4191946104Sxu_zhclass WayLookupGPFEntry(implicit p: Parameters) extends ICacheBundle {
4291946104Sxu_zh  val gpaddr         : UInt      = UInt(GPAddrBits.W)
4391946104Sxu_zh}
4491946104Sxu_zh
45b92f8445Sssszwicclass WayLookupInfo(implicit p: Parameters) extends ICacheBundle {
4691946104Sxu_zh  val entry = new WayLookupEntry
4791946104Sxu_zh  val gpf   = new WayLookupGPFEntry
4891946104Sxu_zh
4991946104Sxu_zh  // for compatibility
5091946104Sxu_zh  def vSetIdx        : Vec[UInt] = entry.vSetIdx
5191946104Sxu_zh  def waymask        : Vec[UInt] = entry.waymask
5291946104Sxu_zh  def ptag           : Vec[UInt] = entry.ptag
5388895b11Sxu_zh  def itlb_exception : Vec[UInt] = entry.itlb_exception
54*002c10a4SYanqin Li  def itlb_pbmt      : Vec[UInt] = entry.itlb_pbmt
5588895b11Sxu_zh  def meta_corrupt   : Vec[Bool] = entry.meta_corrupt
5691946104Sxu_zh  def gpaddr         : UInt      = gpf.gpaddr
57b92f8445Sssszwic}
58b92f8445Sssszwic
59b92f8445Sssszwicclass WayLookupInterface(implicit p: Parameters) extends ICacheBundle {
60b92f8445Sssszwic  val flush   = Input(Bool())
61b92f8445Sssszwic  val read    = DecoupledIO(new WayLookupInfo)
62b92f8445Sssszwic  val write   = Flipped(DecoupledIO(new WayLookupInfo))
63b92f8445Sssszwic  val update  = Flipped(ValidIO(new ICacheMissResp))
64b92f8445Sssszwic}
65b92f8445Sssszwic
66b92f8445Sssszwicclass WayLookup(implicit p: Parameters) extends ICacheModule {
6791946104Sxu_zh  val io: WayLookupInterface = IO(new WayLookupInterface)
68b92f8445Sssszwic
69b92f8445Sssszwic  class WayLookupPtr(implicit p: Parameters) extends CircularQueuePtr[WayLookupPtr](nWayLookupSize)
7091946104Sxu_zh  private object WayLookupPtr {
71b92f8445Sssszwic    def apply(f: Bool, v: UInt)(implicit p: Parameters): WayLookupPtr = {
72b92f8445Sssszwic      val ptr = Wire(new WayLookupPtr)
73b92f8445Sssszwic      ptr.flag := f
74b92f8445Sssszwic      ptr.value := v
75b92f8445Sssszwic      ptr
76b92f8445Sssszwic    }
77b92f8445Sssszwic  }
78b92f8445Sssszwic
7991946104Sxu_zh  private val entries  = RegInit(VecInit(Seq.fill(nWayLookupSize)(0.U.asTypeOf(new WayLookupEntry))))
8091946104Sxu_zh  private val readPtr  = RegInit(WayLookupPtr(false.B, 0.U))
8191946104Sxu_zh  private val writePtr = RegInit(WayLookupPtr(false.B, 0.U))
82b92f8445Sssszwic
8391946104Sxu_zh  private val empty = readPtr === writePtr
8491946104Sxu_zh  private val full  = (readPtr.value === writePtr.value) && (readPtr.flag ^ writePtr.flag)
85b92f8445Sssszwic
86b92f8445Sssszwic  when(io.flush) {
87b92f8445Sssszwic    writePtr.value := 0.U
88b92f8445Sssszwic    writePtr.flag  := false.B
89b92f8445Sssszwic  }.elsewhen(io.write.fire) {
90b92f8445Sssszwic    writePtr := writePtr + 1.U
91b92f8445Sssszwic  }
92b92f8445Sssszwic
93b92f8445Sssszwic  when(io.flush) {
94b92f8445Sssszwic    readPtr.value := 0.U
95b92f8445Sssszwic    readPtr.flag  := false.B
96b92f8445Sssszwic  }.elsewhen(io.read.fire) {
97b92f8445Sssszwic    readPtr := readPtr + 1.U
98b92f8445Sssszwic  }
99b92f8445Sssszwic
10088895b11Sxu_zh  private val gpf_entry = RegInit(0.U.asTypeOf(Valid(new WayLookupGPFEntry)))
10191946104Sxu_zh  private val gpfPtr    = RegInit(WayLookupPtr(false.B, 0.U))
10291946104Sxu_zh
10391946104Sxu_zh  when(io.flush) {
10491946104Sxu_zh    // we don't need to reset gpfPtr, since the valid is actually gpf_entries.excp_tlb_gpf
10588895b11Sxu_zh    gpf_entry.valid := false.B
10688895b11Sxu_zh    gpf_entry.bits  := 0.U.asTypeOf(new WayLookupGPFEntry)
10791946104Sxu_zh  }
10891946104Sxu_zh
109b92f8445Sssszwic  /**
110b92f8445Sssszwic    ******************************************************************************
111b92f8445Sssszwic    * update
112b92f8445Sssszwic    ******************************************************************************
113b92f8445Sssszwic    */
11491946104Sxu_zh  private val hits = Wire(Vec(nWayLookupSize, Bool()))
115b92f8445Sssszwic  entries.zip(hits).foreach{ case(entry, hit) =>
116b92f8445Sssszwic    val hit_vec = Wire(Vec(PortNumber, Bool()))
117b92f8445Sssszwic    (0 until PortNumber).foreach { i =>
118b92f8445Sssszwic      val vset_same = (io.update.bits.vSetIdx === entry.vSetIdx(i)) && !io.update.bits.corrupt && io.update.valid
119b92f8445Sssszwic      val ptag_same = getPhyTagFromBlk(io.update.bits.blkPaddr) === entry.ptag(i)
120b92f8445Sssszwic      val way_same = io.update.bits.waymask === entry.waymask(i)
121b92f8445Sssszwic      when(vset_same) {
122b92f8445Sssszwic        when(ptag_same) {
123b92f8445Sssszwic          // miss -> hit
124b92f8445Sssszwic          entry.waymask(i) := io.update.bits.waymask
125b92f8445Sssszwic          // also clear previously found errors since data/metaArray is refilled
12688895b11Sxu_zh          entry.meta_corrupt(i) := false.B
127b92f8445Sssszwic        }.elsewhen(way_same) {
128b92f8445Sssszwic          // data is overwritten: hit -> miss
129b92f8445Sssszwic          entry.waymask(i) := 0.U
130b92f8445Sssszwic          // do not clear previously found errors since way_same might be unreliable when error occurs
131b92f8445Sssszwic        }
132b92f8445Sssszwic      }
133b92f8445Sssszwic      hit_vec(i) := vset_same && (ptag_same || way_same)
134b92f8445Sssszwic    }
135b92f8445Sssszwic    hit := hit_vec.reduce(_||_)
136b92f8445Sssszwic  }
137b92f8445Sssszwic
138b92f8445Sssszwic  /**
139b92f8445Sssszwic    ******************************************************************************
140b92f8445Sssszwic    * read
141b92f8445Sssszwic    ******************************************************************************
142b92f8445Sssszwic    */
143b92f8445Sssszwic  io.read.valid := !empty || io.write.valid
14491946104Sxu_zh  when (empty && io.write.valid) {  // bypass
14591946104Sxu_zh    io.read.bits := io.write.bits
14691946104Sxu_zh  }.otherwise {
14791946104Sxu_zh    io.read.bits.entry := entries(readPtr.value)
14888895b11Sxu_zh    io.read.bits.gpf   := Mux(readPtr === gpfPtr && gpf_entry.valid, gpf_entry.bits, 0.U.asTypeOf(new WayLookupGPFEntry))
14991946104Sxu_zh  }
150b92f8445Sssszwic
151b92f8445Sssszwic  /**
152b92f8445Sssszwic    ******************************************************************************
153b92f8445Sssszwic    * write
154b92f8445Sssszwic    ******************************************************************************
155b92f8445Sssszwic    */
156b92f8445Sssszwic  io.write.ready := !full
157b92f8445Sssszwic  when(io.write.fire) {
15891946104Sxu_zh    entries(writePtr.value) := io.write.bits.entry
15991946104Sxu_zh    // save gpf iff no gpf is already saved
16088895b11Sxu_zh    when(!gpf_entry.valid && io.write.bits.itlb_exception.map(_ === ExceptionType.gpf).reduce(_||_)) {
16188895b11Sxu_zh      gpf_entry.valid := true.B
16288895b11Sxu_zh      gpf_entry.bits  := io.write.bits.gpf
16391946104Sxu_zh      gpfPtr := writePtr
16491946104Sxu_zh    }
165b92f8445Sssszwic  }
166b92f8445Sssszwic}
167