1ca2f90a6SLemover/*************************************************************************************** 2ca2f90a6SLemover * Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3ca2f90a6SLemover * Copyright (c) 2020-2021 Peng Cheng Laboratory 4ca2f90a6SLemover * 5ca2f90a6SLemover * XiangShan is licensed under Mulan PSL v2. 6ca2f90a6SLemover * You can use this software according to the terms and conditions of the Mulan PSL v2. 7ca2f90a6SLemover * You may obtain a copy of Mulan PSL v2 at: 8ca2f90a6SLemover * http://license.coscl.org.cn/MulanPSL2 9ca2f90a6SLemover * 10ca2f90a6SLemover * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11ca2f90a6SLemover * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12ca2f90a6SLemover * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13ca2f90a6SLemover * 14ca2f90a6SLemover * See the Mulan PSL v2 for more details. 15ca2f90a6SLemover ***************************************************************************************/ 16ca2f90a6SLemover 17ca2f90a6SLemoverpackage xiangshan.backend.fu 18ca2f90a6SLemover 19ca2f90a6SLemoverimport chipsalliance.rocketchip.config.Parameters 20ca2f90a6SLemoverimport chisel3._ 21ca2f90a6SLemoverimport chisel3.internal.naming.chiselName 22ca2f90a6SLemoverimport chisel3.util._ 23*a15116bdSLemoverimport utils.ParallelPriorityMux 24ca2f90a6SLemoverimport xiangshan.{HasXSParameter, XSModule} 25ca2f90a6SLemoverimport xiangshan.backend.fu.util.HasCSRConst 26ca2f90a6SLemoverimport xiangshan.cache.mmu.TlbCmd 27ca2f90a6SLemover 28ca2f90a6SLemovertrait PMAMethod extends HasXSParameter with PMPConst { this: XSModule => 29ca2f90a6SLemover /** 30ca2f90a6SLemover def SimpleMemMapList = List( 31ca2f90a6SLemover // Base address Top address Width Description Mode (RWXIDSAC) 32ca2f90a6SLemover MemMap("h00_0000_0000", "h00_0FFF_FFFF", "h0", "Reserved", "RW"), 33ca2f90a6SLemover MemMap("h00_1000_0000", "h00_1FFF_FFFF", "h0", "QSPI_Flash", "RWX"), 34ca2f90a6SLemover MemMap("h00_2000_0000", "h00_2FFF_FFFF", "h0", "Reserved", "RW"), 35ca2f90a6SLemover MemMap("h00_3000_0000", "h00_3000_FFFF", "h0", "DMA", "RW"), 36ca2f90a6SLemover MemMap("h00_3001_0000", "h00_3004_FFFF", "h0", "GPU", "RWC"), 37ca2f90a6SLemover MemMap("h00_3005_0000", "h00_3006_FFFF", "h0", "USB/SDMMC", "RW"), 38ca2f90a6SLemover MemMap("h00_3007_0000", "h00_30FF_FFFF", "h0", "Reserved", "RW"), 39ca2f90a6SLemover MemMap("h00_3100_0000", "h00_3111_FFFF", "h0", "MMIO", "RW"), 40ca2f90a6SLemover MemMap("h00_3112_0000", "h00_37FF_FFFF", "h0", "Reserved", "RW"), 41ca2f90a6SLemover MemMap("h00_3800_0000", "h00_3800_FFFF", "h0", "CLINT", "RW"), 42ca2f90a6SLemover MemMap("h00_3801_0000", "h00_3801_FFFF", "h0", "BEU", "RW"), 43ca2f90a6SLemover MemMap("h00_3802_0000", "h00_3802_0FFF", "h0", "DebugModule", "RWX"), 44ca2f90a6SLemover MemMap("h00_3802_1000", "h00_3BFF_FFFF", "h0", "Reserved", ""), 45ca2f90a6SLemover MemMap("h00_3C00_0000", "h00_3FFF_FFFF", "h0", "PLIC", "RW"), 46ca2f90a6SLemover MemMap("h00_4000_0000", "h00_7FFF_FFFF", "h0", "PCIe", "RW"), 472f30d658SYinan Xu MemMap("h00_8000_0000", "h0F_FFFF_FFFF", "h0", "DDR", "RWXIDSA"), 48ca2f90a6SLemover ) 49ca2f90a6SLemover */ 50ca2f90a6SLemover 51ca2f90a6SLemover def pma_init() : (Vec[UInt], Vec[UInt], Vec[UInt]) = { 52ca2f90a6SLemover // the init value is zero 53ca2f90a6SLemover // from 0 to num(default 16) - 1, lower priority 54ca2f90a6SLemover // according to simple map, 9 entries is needed, pick 6-14, leave 0-5 & 15 unusedcfgMerged.map(_ := 0.U) 55ca2f90a6SLemover 56ca2f90a6SLemover val num = NumPMA 57ca2f90a6SLemover require(num >= 16) 58ca2f90a6SLemover val cfg = WireInit(0.U.asTypeOf(Vec(num, new PMPConfig()))) 59ca2f90a6SLemover 60ca2f90a6SLemover val addr = Wire(Vec(num, UInt((PAddrBits-PMPOffBits).W))) 61ca2f90a6SLemover val mask = Wire(Vec(NumPMP, UInt(PAddrBits.W))) 62ca2f90a6SLemover addr := DontCare 63ca2f90a6SLemover mask := DontCare 64ca2f90a6SLemover 652f30d658SYinan Xu addr(15) := 0x3FFFFFFFEL.U 662f30d658SYinan Xu cfg(15).a := 1.U; cfg(15).r := true.B; cfg(15).w := true.B; cfg(15).x := true.B; cfg(15).c := true.B; cfg(14).atomic := true.B 672f30d658SYinan Xu 68ca2f90a6SLemover // use tor instead of napot, for napot may be confusing and hard to understand 692f30d658SYinan Xu addr(14) := shift_addr(0xFFFFFFFFFL) 70ca2f90a6SLemover cfg(14).a := 1.U; cfg(14).r := true.B; cfg(14).w := true.B; cfg(14).x := true.B; cfg(14).c := true.B; cfg(14).atomic := true.B 71ca2f90a6SLemover 72ca2f90a6SLemover addr(13) := shift_addr(0x80000000L) 73ca2f90a6SLemover cfg(13).a := 1.U; cfg(13).r := true.B; cfg(13).w := true.B 74ca2f90a6SLemover 75ca2f90a6SLemover addr(12) := shift_addr(0x3C000000) 76ca2f90a6SLemover cfg(12).a := 1.U 77ca2f90a6SLemover 78ca2f90a6SLemover addr(11) := shift_addr(0x38021000) 79ca2f90a6SLemover cfg(11).a := 1.U; cfg(11).r := true.B; cfg(11).w := true.B; cfg(11).x := true.B 80ca2f90a6SLemover 81ca2f90a6SLemover addr(10) := shift_addr(0x38020000) 82ca2f90a6SLemover cfg(10).a := 1.U; cfg(10).r := true.B; cfg(10).w := true.B 83ca2f90a6SLemover 84ca2f90a6SLemover addr(9) := shift_addr( 0x30050000) 85ca2f90a6SLemover cfg(9).a := 1.U; cfg(9).r := true.B; cfg(9).w := true.B; cfg(8).c := true.B 86ca2f90a6SLemover 87ca2f90a6SLemover addr(8) := shift_addr( 0x30010000) 88ca2f90a6SLemover cfg(8).a := 1.U; cfg(8).r := true.B; cfg(8).w := true.B 89ca2f90a6SLemover 90ca2f90a6SLemover addr(7) := shift_addr( 0x20000000) 91ca2f90a6SLemover cfg(7).a := 1.U; cfg(7).r := true.B; cfg(7).w := true.B; cfg(7).x := true.B 92ca2f90a6SLemover 93ca2f90a6SLemover addr(6) := shift_addr( 0x10000000) 94ca2f90a6SLemover cfg(6).a := 1.U; cfg(6).r := true.B; cfg(6).w := true.B 95ca2f90a6SLemover 96ca2f90a6SLemover addr(5) := shift_addr(0) 97ca2f90a6SLemover 98ca2f90a6SLemover val cfgInitMerge = cfg.asTypeOf(Vec(num/8, UInt(XLEN.W))) 99ca2f90a6SLemover (cfgInitMerge, addr, mask) 100ca2f90a6SLemover } 101ca2f90a6SLemover 102ca2f90a6SLemover def shift_addr(addr: BigInt) = { 103ca2f90a6SLemover (addr >> 2).U 104ca2f90a6SLemover } 105ca2f90a6SLemover} 106ca2f90a6SLemover 107ca2f90a6SLemovertrait PMACheckMethod extends HasXSParameter with HasCSRConst { this: PMPChecker => 108ca2f90a6SLemover def pma_check(cmd: UInt, cfg: PMPConfig) = { 109ca2f90a6SLemover val resp = Wire(new PMPRespBundle) 110ca2f90a6SLemover resp.ld := TlbCmd.isRead(cmd) && !TlbCmd.isAtom(cmd) && !cfg.r 111ca2f90a6SLemover resp.st := (TlbCmd.isWrite(cmd) || TlbCmd.isAtom(cmd) && cfg.atomic) && !cfg.w 112ca2f90a6SLemover resp.instr := TlbCmd.isExec(cmd) && !cfg.x 113ca2f90a6SLemover resp.mmio := !cfg.c 114ca2f90a6SLemover resp 115ca2f90a6SLemover } 116ca2f90a6SLemover 117ca2f90a6SLemover def pma_match_res(addr: UInt, size: UInt, pmaEntries: Vec[PMPEntry], mode: UInt, lgMaxSize: Int) = { 118ca2f90a6SLemover val num = pmaEntries.size 119ca2f90a6SLemover require(num == NumPMA) 120ca2f90a6SLemover // pma should always be checked, could not be ignored 121ca2f90a6SLemover // like amo and cached, it is the attribute not protection 122ca2f90a6SLemover // so it must have initialization. 123ca2f90a6SLemover require(!pmaEntries.isEmpty) 124ca2f90a6SLemover 125*a15116bdSLemover val pmaDefault = WireInit(0.U.asTypeOf(new PMPEntry())) 126*a15116bdSLemover val match_vec = Wire(Vec(num+1, Bool())) 127*a15116bdSLemover val cfg_vec = Wire(Vec(num+1, new PMPEntry())) 128*a15116bdSLemover 129*a15116bdSLemover pmaEntries.zip(pmaDefault +: pmaEntries.take(num-1)).zipWithIndex.foreach{ case ((pma, last_pma), i) => 130ca2f90a6SLemover val is_match = pma.is_match(addr, size, lgMaxSize, last_pma) 131ca2f90a6SLemover val aligned = pma.aligned(addr, size, lgMaxSize, last_pma) 132ca2f90a6SLemover 133ca2f90a6SLemover val cur = WireInit(pma) 134ca2f90a6SLemover cur.cfg.r := aligned && pma.cfg.r 135ca2f90a6SLemover cur.cfg.w := aligned && pma.cfg.w 136ca2f90a6SLemover cur.cfg.x := aligned && pma.cfg.x 137ca2f90a6SLemover cur.cfg.atomic := aligned && pma.cfg.atomic 138ca2f90a6SLemover cur.cfg.c := aligned && pma.cfg.c 139ca2f90a6SLemover 140*a15116bdSLemover match_vec(i) := is_match 141*a15116bdSLemover cfg_vec(i) := cur 142ca2f90a6SLemover } 143*a15116bdSLemover 144*a15116bdSLemover match_vec(num) := true.B 145*a15116bdSLemover cfg_vec(num) := pmaDefault 146*a15116bdSLemover 147*a15116bdSLemover ParallelPriorityMux(match_vec, cfg_vec) 148ca2f90a6SLemover } 149ca2f90a6SLemover}