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._ 21cf7d6b7aSMuziimport org.chipsalliance.cde.config.Parameters 22b92f8445Sssszwicimport utility._ 23002c10a4SYanqin Liimport xiangshan.cache.mmu.Pbmt 24cf7d6b7aSMuziimport 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 { 42*415fcbe2Sxu_zh // NOTE: we don't use GPAddrBits here, refer to ICacheMainPipe.scala L43-48 and PR#3795 43dd980d61SXu, Zefan val gpaddr: UInt = UInt(PAddrBitsMax.W) 44ad415ae0SXiaokun-Pei val isForVSnonLeafPTE: Bool = Bool() 4591946104Sxu_zh} 4691946104Sxu_zh 47b92f8445Sssszwicclass WayLookupInfo(implicit p: Parameters) extends ICacheBundle { 4891946104Sxu_zh val entry = new WayLookupEntry 4991946104Sxu_zh val gpf = new WayLookupGPFEntry 5091946104Sxu_zh 5191946104Sxu_zh // for compatibility 5291946104Sxu_zh def vSetIdx: Vec[UInt] = entry.vSetIdx 5391946104Sxu_zh def waymask: Vec[UInt] = entry.waymask 5491946104Sxu_zh def ptag: Vec[UInt] = entry.ptag 5588895b11Sxu_zh def itlb_exception: Vec[UInt] = entry.itlb_exception 56002c10a4SYanqin Li def itlb_pbmt: Vec[UInt] = entry.itlb_pbmt 578966a895Sxu_zh def meta_codes: Vec[UInt] = entry.meta_codes 5891946104Sxu_zh def gpaddr: UInt = gpf.gpaddr 59ad415ae0SXiaokun-Pei def isForVSnonLeafPTE: Bool = gpf.isForVSnonLeafPTE 60b92f8445Sssszwic} 61b92f8445Sssszwic 62b92f8445Sssszwicclass WayLookupInterface(implicit p: Parameters) extends ICacheBundle { 63*415fcbe2Sxu_zh val flush: Bool = Input(Bool()) 64*415fcbe2Sxu_zh val read: DecoupledIO[WayLookupInfo] = DecoupledIO(new WayLookupInfo) 65*415fcbe2Sxu_zh val write: DecoupledIO[WayLookupInfo] = Flipped(DecoupledIO(new WayLookupInfo)) 66*415fcbe2Sxu_zh val update: Valid[ICacheMissResp] = Flipped(ValidIO(new ICacheMissResp)) 67b92f8445Sssszwic} 68b92f8445Sssszwic 69b92f8445Sssszwicclass WayLookup(implicit p: Parameters) extends ICacheModule { 7091946104Sxu_zh val io: WayLookupInterface = IO(new WayLookupInterface) 71b92f8445Sssszwic 72*415fcbe2Sxu_zh class WayLookupPtr extends CircularQueuePtr[WayLookupPtr](nWayLookupSize) 7391946104Sxu_zh private object WayLookupPtr { 74*415fcbe2Sxu_zh def apply(f: Bool, v: UInt): WayLookupPtr = { 75b92f8445Sssszwic val ptr = Wire(new WayLookupPtr) 76b92f8445Sssszwic ptr.flag := f 77b92f8445Sssszwic ptr.value := v 78b92f8445Sssszwic ptr 79b92f8445Sssszwic } 80b92f8445Sssszwic } 81b92f8445Sssszwic 8291946104Sxu_zh private val entries = RegInit(VecInit(Seq.fill(nWayLookupSize)(0.U.asTypeOf(new WayLookupEntry)))) 8391946104Sxu_zh private val readPtr = RegInit(WayLookupPtr(false.B, 0.U)) 8491946104Sxu_zh private val writePtr = RegInit(WayLookupPtr(false.B, 0.U)) 85b92f8445Sssszwic 8691946104Sxu_zh private val empty = readPtr === writePtr 8791946104Sxu_zh private val full = (readPtr.value === writePtr.value) && (readPtr.flag ^ writePtr.flag) 88b92f8445Sssszwic 89b92f8445Sssszwic when(io.flush) { 90b92f8445Sssszwic writePtr.value := 0.U 91b92f8445Sssszwic writePtr.flag := false.B 92b92f8445Sssszwic }.elsewhen(io.write.fire) { 93b92f8445Sssszwic writePtr := writePtr + 1.U 94b92f8445Sssszwic } 95b92f8445Sssszwic 96b92f8445Sssszwic when(io.flush) { 97b92f8445Sssszwic readPtr.value := 0.U 98b92f8445Sssszwic readPtr.flag := false.B 99b92f8445Sssszwic }.elsewhen(io.read.fire) { 100b92f8445Sssszwic readPtr := readPtr + 1.U 101b92f8445Sssszwic } 102b92f8445Sssszwic 10388895b11Sxu_zh private val gpf_entry = RegInit(0.U.asTypeOf(Valid(new WayLookupGPFEntry))) 10491946104Sxu_zh private val gpfPtr = RegInit(WayLookupPtr(false.B, 0.U)) 105b7a4433dSxu_zh private val gpf_hit = gpfPtr === readPtr && gpf_entry.valid 10691946104Sxu_zh 10791946104Sxu_zh when(io.flush) { 10891946104Sxu_zh // we don't need to reset gpfPtr, since the valid is actually gpf_entries.excp_tlb_gpf 10988895b11Sxu_zh gpf_entry.valid := false.B 11088895b11Sxu_zh gpf_entry.bits := 0.U.asTypeOf(new WayLookupGPFEntry) 11191946104Sxu_zh } 11291946104Sxu_zh 113b92f8445Sssszwic /** 114b92f8445Sssszwic ****************************************************************************** 115b92f8445Sssszwic * update 116b92f8445Sssszwic ****************************************************************************** 117b92f8445Sssszwic */ 11891946104Sxu_zh private val hits = Wire(Vec(nWayLookupSize, Bool())) 119b92f8445Sssszwic entries.zip(hits).foreach { case (entry, hit) => 120b92f8445Sssszwic val hit_vec = Wire(Vec(PortNumber, Bool())) 121b92f8445Sssszwic (0 until PortNumber).foreach { i => 122b92f8445Sssszwic val vset_same = (io.update.bits.vSetIdx === entry.vSetIdx(i)) && !io.update.bits.corrupt && io.update.valid 123b92f8445Sssszwic val ptag_same = getPhyTagFromBlk(io.update.bits.blkPaddr) === entry.ptag(i) 124b92f8445Sssszwic val way_same = io.update.bits.waymask === entry.waymask(i) 125b92f8445Sssszwic when(vset_same) { 126b92f8445Sssszwic when(ptag_same) { 127b92f8445Sssszwic // miss -> hit 128b92f8445Sssszwic entry.waymask(i) := io.update.bits.waymask 1298966a895Sxu_zh // also update meta_codes 130*415fcbe2Sxu_zh // NOTE: we have getPhyTagFromBlk(io.update.bits.blkPaddr) === entry.ptag(i), 131*415fcbe2Sxu_zh // so we can use entry.ptag(i) for better timing 1325ce94708Sxu_zh entry.meta_codes(i) := encodeMetaECC(entry.ptag(i)) 133b92f8445Sssszwic }.elsewhen(way_same) { 134b92f8445Sssszwic // data is overwritten: hit -> miss 135b92f8445Sssszwic entry.waymask(i) := 0.U 136*415fcbe2Sxu_zh // don't care meta_codes, since it's not used for a missed request 137b92f8445Sssszwic } 138b92f8445Sssszwic } 139b92f8445Sssszwic hit_vec(i) := vset_same && (ptag_same || way_same) 140b92f8445Sssszwic } 141b92f8445Sssszwic hit := hit_vec.reduce(_ || _) 142b92f8445Sssszwic } 143b92f8445Sssszwic 144b92f8445Sssszwic /** 145b92f8445Sssszwic ****************************************************************************** 146b92f8445Sssszwic * read 147b92f8445Sssszwic ****************************************************************************** 148b92f8445Sssszwic */ 149b7a4433dSxu_zh // if the entry is empty, but there is a valid write, we can bypass it to read port (maybe timing critical) 150b7a4433dSxu_zh private val can_bypass = empty && io.write.valid 151b92f8445Sssszwic io.read.valid := !empty || io.write.valid 152b7a4433dSxu_zh when(can_bypass) { 15391946104Sxu_zh io.read.bits := io.write.bits 154b7a4433dSxu_zh }.otherwise { // can't bypass 15591946104Sxu_zh io.read.bits.entry := entries(readPtr.value) 156b7a4433dSxu_zh when(gpf_hit) { // ptr match && entry valid 157b7a4433dSxu_zh io.read.bits.gpf := gpf_entry.bits 158*415fcbe2Sxu_zh // also clear gpf_entry.valid when it's read, note this will be overridden by write (L175) 159b7a4433dSxu_zh when(io.read.fire) { 160b7a4433dSxu_zh gpf_entry.valid := false.B 161b7a4433dSxu_zh } 162b7a4433dSxu_zh }.otherwise { // gpf not hit 163b7a4433dSxu_zh io.read.bits.gpf := 0.U.asTypeOf(new WayLookupGPFEntry) 164b7a4433dSxu_zh } 16591946104Sxu_zh } 166b92f8445Sssszwic 167b92f8445Sssszwic /** 168b92f8445Sssszwic ****************************************************************************** 169b92f8445Sssszwic * write 170b92f8445Sssszwic ****************************************************************************** 171b92f8445Sssszwic */ 172*415fcbe2Sxu_zh // if there is a valid gpf to be read, we should stall write 173b7a4433dSxu_zh private val gpf_stall = gpf_entry.valid && !(io.read.fire && gpf_hit) 174b7a4433dSxu_zh io.write.ready := !full && !gpf_stall 175b92f8445Sssszwic when(io.write.fire) { 17691946104Sxu_zh entries(writePtr.value) := io.write.bits.entry 177b7a4433dSxu_zh when(io.write.bits.itlb_exception.map(_ === ExceptionType.gpf).reduce(_ || _)) { 178b7a4433dSxu_zh // if gpf_entry is bypassed, we don't need to save it 179b7a4433dSxu_zh // note this will override the read (L156) 180b7a4433dSxu_zh gpf_entry.valid := !(can_bypass && io.read.fire) 18188895b11Sxu_zh gpf_entry.bits := io.write.bits.gpf 18291946104Sxu_zh gpfPtr := writePtr 18391946104Sxu_zh } 184b92f8445Sssszwic } 185b92f8445Sssszwic} 186