16d5ddbceSLemover/*************************************************************************************** 26d5ddbceSLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory 46d5ddbceSLemover* 56d5ddbceSLemover* XiangShan is licensed under Mulan PSL v2. 66d5ddbceSLemover* You can use this software according to the terms and conditions of the Mulan PSL v2. 76d5ddbceSLemover* You may obtain a copy of Mulan PSL v2 at: 86d5ddbceSLemover* http://license.coscl.org.cn/MulanPSL2 96d5ddbceSLemover* 106d5ddbceSLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 116d5ddbceSLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 126d5ddbceSLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 136d5ddbceSLemover* 146d5ddbceSLemover* See the Mulan PSL v2 for more details. 156d5ddbceSLemover***************************************************************************************/ 166d5ddbceSLemover 176d5ddbceSLemoverpackage xiangshan.cache.mmu 186d5ddbceSLemover 196d5ddbceSLemoverimport chipsalliance.rocketchip.config.Parameters 206d5ddbceSLemoverimport chisel3._ 216d5ddbceSLemoverimport chisel3.util._ 226d5ddbceSLemoverimport xiangshan._ 236d5ddbceSLemoverimport xiangshan.cache.{HasDCacheParameters, MemoryOpConstants} 246d5ddbceSLemoverimport utils._ 256d5ddbceSLemoverimport freechips.rocketchip.diplomacy.{LazyModule, LazyModuleImp} 266d5ddbceSLemoverimport freechips.rocketchip.tilelink._ 276d5ddbceSLemover 28a0301c0dSLemovercase class TLBParameters 29a0301c0dSLemover( 30a0301c0dSLemover name: String = "none", 31a0301c0dSLemover fetchi: Boolean = false, // TODO: remove it 32a0301c0dSLemover useDmode: Boolean = true, 33a0301c0dSLemover sameCycle: Boolean = false, 34fb90f54dSLemover missSameCycle: Boolean = false, 35a0301c0dSLemover normalNSets: Int = 1, // when da or sa 36a0301c0dSLemover normalNWays: Int = 8, // when fa or sa 37a0301c0dSLemover superNSets: Int = 1, 38a0301c0dSLemover superNWays: Int = 2, 39a0301c0dSLemover normalReplacer: Option[String] = Some("random"), 40a0301c0dSLemover superReplacer: Option[String] = Some("plru"), 41a0301c0dSLemover normalAssociative: String = "fa", // "fa", "sa", "da", "sa" is not supported 42a0301c0dSLemover superAssociative: String = "fa", // must be fa 43a0301c0dSLemover normalAsVictim: Boolean = false, // when get replace from fa, store it into sram 44a0301c0dSLemover outReplace: Boolean = false, 455cf62c1aSLemover shouldBlock: Boolean = false, // only for perf, not support for io 465b7ef044SLemover partialStaticPMP: Boolean = false, // partila static pmp result stored in entries 475cf62c1aSLemover saveLevel: Boolean = false 48a0301c0dSLemover) 49a0301c0dSLemover 505854c1edSLemovercase class L2TLBParameters 515854c1edSLemover( 525854c1edSLemover name: String = "l2tlb", 535854c1edSLemover // l1 545854c1edSLemover l1Size: Int = 16, 555854c1edSLemover l1Associative: String = "fa", 565854c1edSLemover l1Replacer: Option[String] = Some("plru"), 575854c1edSLemover // l2 58ecf1a4b8SLemover l2nSets: Int = 32, 59ecf1a4b8SLemover l2nWays: Int = 2, 605854c1edSLemover l2Replacer: Option[String] = Some("setplru"), 615854c1edSLemover // l3 62fa086d5eSLemover l3nSets: Int = 128, 63fa086d5eSLemover l3nWays: Int = 4, 645854c1edSLemover l3Replacer: Option[String] = Some("setplru"), 655854c1edSLemover // sp 665854c1edSLemover spSize: Int = 16, 675854c1edSLemover spReplacer: Option[String] = Some("plru"), 68bc063562SLemover // dtlb filter 69bc063562SLemover filterSize: Int = 8, 70d74a7bd3SLemover // miss queue, add more entries than 'must require' 71d74a7bd3SLemover // 0 for easier bug trigger, please set as big as u can, 8 maybe 72d74a7bd3SLemover missqueueExtendSize: Int = 0, 73*92e3bfefSLemover // llptw 74*92e3bfefSLemover llptwsize: Int = 6, 755854c1edSLemover // way size 767196f5a2SLemover blockBytes: Int = 64, 77bc063562SLemover // prefetch 78bc063562SLemover enablePrefetch: Boolean = true, 797196f5a2SLemover // ecc 807196f5a2SLemover ecc: Option[String] = Some("secded") 815854c1edSLemover) 825854c1edSLemover 836d5ddbceSLemovertrait HasTlbConst extends HasXSParameter { 846d5ddbceSLemover val Level = 3 856d5ddbceSLemover 866d5ddbceSLemover val offLen = 12 876d5ddbceSLemover val ppnLen = PAddrBits - offLen 886d5ddbceSLemover val vpnnLen = 9 896d5ddbceSLemover val vpnLen = VAddrBits - offLen 906d5ddbceSLemover val flagLen = 8 916d5ddbceSLemover val pteResLen = XLEN - ppnLen - 2 - flagLen 926d5ddbceSLemover 93a0301c0dSLemover val sramSinglePort = true 94a0301c0dSLemover 95acc88887SJiawei Lin val timeOutThreshold = 10000 969bd9cdfaSLemover 973889e11eSLemover def get_set_idx(vpn: UInt, nSets: Int): UInt = { 98e9092fe2SLemover require(nSets >= 1) 99a0301c0dSLemover vpn(log2Up(nSets)-1, 0) 1006d5ddbceSLemover } 1016d5ddbceSLemover 102e9092fe2SLemover def drop_set_idx(vpn: UInt, nSets: Int): UInt = { 103e9092fe2SLemover require(nSets >= 1) 104e9092fe2SLemover require(vpn.getWidth > log2Ceil(nSets)) 105e9092fe2SLemover vpn(vpn.getWidth-1, log2Ceil(nSets)) 106e9092fe2SLemover } 107e9092fe2SLemover 108e9092fe2SLemover def drop_set_equal(vpn1: UInt, vpn2: UInt, nSets: Int): Bool = { 109e9092fe2SLemover require(nSets >= 1) 110e9092fe2SLemover require(vpn1.getWidth == vpn2.getWidth) 111e9092fe2SLemover if (vpn1.getWidth <= log2Ceil(nSets)) { 112e9092fe2SLemover true.B 113e9092fe2SLemover } else { 114e9092fe2SLemover drop_set_idx(vpn1, nSets) === drop_set_idx(vpn2, nSets) 115e9092fe2SLemover } 116e9092fe2SLemover } 117e9092fe2SLemover 1186d5ddbceSLemover def replaceWrapper(v: UInt, lruIdx: UInt): UInt = { 1196d5ddbceSLemover val width = v.getWidth 1206d5ddbceSLemover val emptyIdx = ParallelPriorityMux((0 until width).map( i => (!v(i), i.U))) 1216d5ddbceSLemover val full = Cat(v).andR 1226d5ddbceSLemover Mux(full, lruIdx, emptyIdx) 1236d5ddbceSLemover } 1246d5ddbceSLemover 1256d5ddbceSLemover def replaceWrapper(v: Seq[Bool], lruIdx: UInt): UInt = { 1266d5ddbceSLemover replaceWrapper(VecInit(v).asUInt, lruIdx) 1276d5ddbceSLemover } 128a0301c0dSLemover 1296d5ddbceSLemover} 1306d5ddbceSLemover 1316d5ddbceSLemovertrait HasPtwConst extends HasTlbConst with MemoryOpConstants{ 1326d5ddbceSLemover val PtwWidth = 2 133bc063562SLemover val sourceWidth = { if (l2tlbParams.enablePrefetch) PtwWidth + 1 else PtwWidth} 134bc063562SLemover val prefetchID = PtwWidth 135bc063562SLemover val maxPrefetchNum = l2tlbParams.filterSize 136bc063562SLemover 1375854c1edSLemover val blockBits = l2tlbParams.blockBytes * 8 1386d5ddbceSLemover 1396d5ddbceSLemover val bPtwWidth = log2Up(PtwWidth) 140bc063562SLemover val bSourceWidth = log2Up(sourceWidth) 1416d5ddbceSLemover // ptwl1: fully-associated 1426d5ddbceSLemover val PtwL1TagLen = vpnnLen 1436d5ddbceSLemover 1446d5ddbceSLemover /* +-------+----------+-------------+ 1456d5ddbceSLemover * | Tag | SetIdx | SectorIdx | 1466d5ddbceSLemover * +-------+----------+-------------+ 1476d5ddbceSLemover */ 1486d5ddbceSLemover // ptwl2: 8-way group-associated 1495854c1edSLemover val l2tlbParams.l2nWays = l2tlbParams.l2nWays 1505854c1edSLemover val PtwL2SetNum = l2tlbParams.l2nSets 1515854c1edSLemover val PtwL2SectorSize = blockBits /XLEN 1525854c1edSLemover val PtwL2IdxLen = log2Up(PtwL2SetNum * PtwL2SectorSize) 1536d5ddbceSLemover val PtwL2SectorIdxLen = log2Up(PtwL2SectorSize) 1545854c1edSLemover val PtwL2SetIdxLen = log2Up(PtwL2SetNum) 1556d5ddbceSLemover val PtwL2TagLen = vpnnLen * 2 - PtwL2IdxLen 1566d5ddbceSLemover 1576d5ddbceSLemover // ptwl3: 16-way group-associated 1585854c1edSLemover val l2tlbParams.l3nWays = l2tlbParams.l3nWays 1595854c1edSLemover val PtwL3SetNum = l2tlbParams.l3nSets 1605854c1edSLemover val PtwL3SectorSize = blockBits / XLEN 1615854c1edSLemover val PtwL3IdxLen = log2Up(PtwL3SetNum * PtwL3SectorSize) 1626d5ddbceSLemover val PtwL3SectorIdxLen = log2Up(PtwL3SectorSize) 1635854c1edSLemover val PtwL3SetIdxLen = log2Up(PtwL3SetNum) 1646d5ddbceSLemover val PtwL3TagLen = vpnnLen * 3 - PtwL3IdxLen 1656d5ddbceSLemover 1666d5ddbceSLemover // super page, including 1GB and 2MB page 1676d5ddbceSLemover val SPTagLen = vpnnLen * 2 1686d5ddbceSLemover 169d74a7bd3SLemover // miss queue 170d74a7bd3SLemover val MSHRBaseSize = 1 + l2tlbParams.filterSize + l2tlbParams.missqueueExtendSize 171d74a7bd3SLemover val MSHRSize = { if (l2tlbParams.enablePrefetch) (MSHRBaseSize + 1) else MSHRBaseSize } 172*92e3bfefSLemover val MemReqWidth = l2tlbParams.llptwsize + 1 173*92e3bfefSLemover val FsmReqID = l2tlbParams.llptwsize 174*92e3bfefSLemover val bMemID = log2Up(MemReqWidth) 1756d5ddbceSLemover 1766d5ddbceSLemover def genPtwL2Idx(vpn: UInt) = { 1776d5ddbceSLemover (vpn(vpnLen - 1, vpnnLen))(PtwL2IdxLen - 1, 0) 1786d5ddbceSLemover } 1796d5ddbceSLemover 1806d5ddbceSLemover def genPtwL2SectorIdx(vpn: UInt) = { 1816d5ddbceSLemover genPtwL2Idx(vpn)(PtwL2SectorIdxLen - 1, 0) 1826d5ddbceSLemover } 1836d5ddbceSLemover 1846d5ddbceSLemover def genPtwL2SetIdx(vpn: UInt) = { 1856d5ddbceSLemover genPtwL2Idx(vpn)(PtwL2SetIdxLen + PtwL2SectorIdxLen - 1, PtwL2SectorIdxLen) 1866d5ddbceSLemover } 1876d5ddbceSLemover 1886d5ddbceSLemover def genPtwL3Idx(vpn: UInt) = { 1896d5ddbceSLemover vpn(PtwL3IdxLen - 1, 0) 1906d5ddbceSLemover } 1916d5ddbceSLemover 1926d5ddbceSLemover def genPtwL3SectorIdx(vpn: UInt) = { 1936d5ddbceSLemover genPtwL3Idx(vpn)(PtwL3SectorIdxLen - 1, 0) 1946d5ddbceSLemover } 1956d5ddbceSLemover 196cc5a5f22SLemover def dropL3SectorBits(vpn: UInt) = { 197cc5a5f22SLemover vpn(vpn.getWidth-1, PtwL3SectorIdxLen) 198cc5a5f22SLemover } 199cc5a5f22SLemover 2006d5ddbceSLemover def genPtwL3SetIdx(vpn: UInt) = { 2016d5ddbceSLemover genPtwL3Idx(vpn)(PtwL3SetIdxLen + PtwL3SectorIdxLen - 1, PtwL3SectorIdxLen) 2026d5ddbceSLemover } 2036d5ddbceSLemover 2046d5ddbceSLemover def MakeAddr(ppn: UInt, off: UInt) = { 2056d5ddbceSLemover require(off.getWidth == 9) 2066d5ddbceSLemover Cat(ppn, off, 0.U(log2Up(XLEN/8).W))(PAddrBits-1, 0) 2076d5ddbceSLemover } 2086d5ddbceSLemover 209b848eea5SLemover def getVpnn(vpn: UInt, idx: Int): UInt = { 2106d5ddbceSLemover vpn(vpnnLen*(idx+1)-1, vpnnLen*idx) 2116d5ddbceSLemover } 2126d5ddbceSLemover 2136d5ddbceSLemover def getVpnClip(vpn: UInt, level: Int) = { 2146d5ddbceSLemover // level 0 /* vpnn2 */ 2156d5ddbceSLemover // level 1 /* vpnn2 * vpnn1 */ 2166d5ddbceSLemover // level 2 /* vpnn2 * vpnn1 * vpnn0*/ 2176d5ddbceSLemover vpn(vpnLen - 1, (2 - level) * vpnnLen) 2186d5ddbceSLemover } 2196d5ddbceSLemover 220bc063562SLemover def get_next_line(vpn: UInt) = { 221bc063562SLemover Cat(dropL3SectorBits(vpn) + 1.U, 0.U(PtwL3SectorIdxLen.W)) 222bc063562SLemover } 223bc063562SLemover 224bc063562SLemover def same_l2entry(vpn1: UInt, vpn2: UInt) = { 225bc063562SLemover vpn1(vpnLen-1, vpnnLen) === vpn2(vpnLen-1, vpnnLen) 226bc063562SLemover } 227bc063562SLemover 228bc063562SLemover def from_pre(source: UInt) = { 229bc063562SLemover (source === prefetchID.U) 230bc063562SLemover } 231bc063562SLemover 2326d5ddbceSLemover def printVec[T <: Data](x: Seq[T]): Printable = { 2336d5ddbceSLemover (0 until x.length).map(i => p"(${i.U})${x(i)} ").reduce(_+_) 2346d5ddbceSLemover } 2356d5ddbceSLemover} 236