1package xiangshan.backend.fu.NewCSR 2 3import chisel3._ 4import chisel3.util.BitPat.bitPatToUInt 5import chisel3.util.{BitPat, Cat, Fill, Mux1H, MuxCase, ValidIO} 6import utility.{SignExt, ZeroExt} 7import freechips.rocketchip.rocket.CSRs 8import xiangshan.backend.fu.NewCSR.CSRBundles._ 9import xiangshan.backend.fu.NewCSR.CSRDefines._ 10import xiangshan.backend.fu.NewCSR.CSRFunc._ 11import xiangshan.backend.fu.NewCSR.CSRDefines.{CSRROField => RO, CSRRWField => RW, CSRWARLField => WARL, CSRWLRLField => WLRL, _} 12import xiangshan.backend.fu.NewCSR.CSRConfig._ 13import xiangshan.backend.fu.NewCSR.CSREvents.TrapEntryHSEventSinkBundle 14import xiangshan.backend.fu.NewCSR.CSREnumTypeImplicitCast._ 15import xiangshan.backend.fu.NewCSR.CSRBundleImplicitCast._ 16import xiangshan.backend.fu.NewCSR.ChiselRecordForField._ 17 18import scala.collection.immutable.SeqMap 19 20trait SupervisorLevel { self: NewCSR with MachineLevel => 21 val sie = Module(new CSRModule("Sie", new SieBundle) 22 with HasIpIeBundle 23 { 24 val toMie = IO(new SieToMie) 25 val fromVSie = IO(Flipped(new VSieToSie)) 26 27 // Sie is alias of mie when mideleg=1. 28 // Otherwise, sie has seperate writable registers 29 // There are no regs in CSR sie. 30 val mieIsAlias = mideleg 31 val usingReg = ~mideleg & mvien 32 regOut := (mieIsAlias & mie) | (usingReg & reg) 33 34 bundle.getFields.map(_.lsb).foreach { num => 35 val wtMie = toMie.getByNum(num) 36 val vsieWt = fromVSie.getByNum(num) 37 38 wtMie.specifyField( 39 _.valid := wen && mieIsAlias(num) && wtMie.bits.isRW.B, 40 _.bits := wen && mieIsAlias(num) && wtMie.bits.isRW.B &< wdata(num), 41 ) 42 43 when ( 44 wen && usingReg(num) && reg(num).isRW.B || 45 vsieWt.valid && usingReg(num) && vsieWt.bits.isRW.B 46 ) { 47 reg(num) := Mux1H(Seq( 48 wen -> wdata(num), 49 vsieWt.valid -> vsieWt.bits, 50 )) 51 }.otherwise { 52 reg(num) := reg(num) 53 } 54 } 55 56 regOut.getFields.foreach { field => 57 if (field.isHardWired) { 58 field := field.getHardWireValue 59 } 60 } 61 }) 62 .setAddr(CSRs.sie) 63 64 val stvec = Module(new CSRModule("Stvec", new XtvecBundle)) 65 .setAddr(CSRs.stvec) 66 67 val scounteren = Module(new CSRModule("Scounteren", new Counteren)) 68 .setAddr(CSRs.scounteren) 69 70 val senvcfg = Module(new CSRModule("Senvcfg", new SEnvCfg)) 71 .setAddr(CSRs.senvcfg) 72 73 val sscratch = Module(new CSRModule("Sscratch")) 74 .setAddr(CSRs.sscratch) 75 76 val sepc = Module(new CSRModule("Sepc", new Epc) with TrapEntryHSEventSinkBundle) 77 .setAddr(CSRs.sepc) 78 79 val scause = Module(new CSRModule("Scause", new CauseBundle) with TrapEntryHSEventSinkBundle) 80 .setAddr(CSRs.scause) 81 82 val stval = Module(new CSRModule("Stval", new XtvalBundle) with TrapEntryHSEventSinkBundle) 83 .setAddr(CSRs.stval) 84 85 val sip = Module(new CSRModule("Sip", new SipBundle) 86 with HasIpIeBundle 87 { 88 val toMip = IO(new SipToMip) 89 val toMvip = IO(new SipToMvip) 90 91 // Ref: 7.1.3. Supervisor Interrupt Registers (sip and sie) 92 // The sip and sie registers are subsets of the mip and mie registers. Reading any 93 // implemented field, or writing any writable field, of sip/sie effects a read or write of the 94 // homonymous field of mip/mie. 95 96 // Ref: 3.1.9. Machine Interrupt Registers (mip and mie) 97 // Restricted views of the mip and mie registers appear as the sip and sie registers for supervisor level. If 98 // an interrupt is delegated to S-mode by setting a bit in the mideleg register, it becomes visible in the 99 // sip register and is maskable using the sie register. Otherwise, the corresponding bits in sip and sie 100 // are **read-only zero**. 101 val mipIsAlias = mideleg 102 val mvipIsAlias = ~mideleg & mvien 103 104 dontTouch(mvipIsAlias) 105 106 regOut := mipIsAlias & mip | (mvipIsAlias & mvip) 107 108 bundle.getFields.map(_.lsb).foreach { num => 109 val wtMip = toMip.getByNum(num) 110 val wtMvip = toMvip.getByNum(num) 111 112 wtMip.specifyField( 113 _.valid := wen && mipIsAlias(num) && wtMip.bits.isRW.B, 114 _.bits := wen && mipIsAlias(num) && wtMip.bits.isRW.B &< wdata(num), 115 ) 116 117 wtMvip.specifyField( 118 _.valid := wen && mvipIsAlias(num) && wtMvip.bits.isRW.B, 119 _.bits := wen && mvipIsAlias(num) && wtMvip.bits.isRW.B &< wdata(num), 120 ) 121 } 122 123 regOut.getFields.foreach { field => 124 if (field.isHardWired) { 125 field := field.getHardWireValue 126 } 127 } 128 }) 129 .setAddr(CSRs.sip) 130 131 val stimecmp = Module(new CSRModule("Stimecmp", new CSRBundle { 132 val stimecmp = RW(63, 0).withReset(bitPatToUInt(BitPat.Y(64))) 133 })) 134 .setAddr(CSRs.stimecmp) 135 136 val satp = Module(new CSRModule("Satp", new SatpBundle) { 137 val ppnMask = ZeroExt(Fill(PPNLength, 1.U(1.W)).take(PAddrBits - PageOffsetWidth), PPNLength) 138 // If satp is written with an unsupported MODE, 139 // the entire write has no effect; no fields in satp are modified. 140 when (wen && wdata.MODE.isLegal) { 141 reg.MODE := wdata.MODE 142 reg.ASID := wdata.ASID 143 reg.PPN := wdata.PPN & ppnMask 144 }.otherwise { 145 reg := reg 146 } 147 }) 148 .setAddr(CSRs.satp) 149 150 // scountovf: This register enables supervisor-level overflow interrupt handler software to quickly and easily 151 // determine which counter(s) have overflowed (without needing to make an execution environment call 152 // or series of calls ultimately up to M-mode). 153 val scountovf = Module(new CSRModule("Scountovf", new CSRBundle { 154 override val len: Int = 32 155 val OFVEC = RO(31, 3).withReset(0.U) 156 }) with HasMhpmeventOfBundle { 157 reg.OFVEC := ofVec 158 regOut.OFVEC := Mux1H(Seq( 159 privState.isModeM -> reg.OFVEC.asUInt, 160 privState.isModeHS -> (mcounteren.HPM.asUInt & reg.OFVEC.asUInt), 161 privState.isModeVS -> (mcounteren.HPM.asUInt & hcounteren.HPM.asUInt & reg.OFVEC.asUInt), 162 ) 163 ) 164 }).setAddr(CSRs.scountovf) 165 166 val sstateen0 = Module(new CSRModule("Sstateen", new SstateenBundle0) with HasStateen0Bundle { 167 // For every bit in an mstateen CSR that is zero (whether read-only zero or set to zero), the same bit 168 // appears as read-only zero in the matching hstateen and sstateen CSRs. For every bit in an hstateen 169 // CSR that is zero (whether read-only zero or set to zero), the same bit appears as read-only zero in 170 // sstateen when accessed in VS-mode. 171 regOut := Mux(privState.isVirtual, fromHstateen0.asUInt, fromMstateen0.asUInt) & reg.asUInt 172 }).setAddr(CSRs.sstateen0) 173 174 val supervisorLevelCSRMods: Seq[CSRModule[_]] = Seq( 175 sie, 176 stvec, 177 scounteren, 178 senvcfg, 179 sscratch, 180 sepc, 181 scause, 182 stval, 183 sip, 184 stimecmp, 185 satp, 186 scountovf, 187 sstateen0, 188 ) 189 190 val supervisorLevelCSRMap: SeqMap[Int, (CSRAddrWriteBundle[_], UInt)] = SeqMap( 191 CSRs.sstatus -> (mstatus.wAliasSstatus, mstatus.sstatusRdata), 192 ) ++ SeqMap.from( 193 supervisorLevelCSRMods.map(csr => (csr.addr -> (csr.w, csr.rdata))).iterator 194 ) 195 196 val supervisorLevelCSROutMap: SeqMap[Int, UInt] = SeqMap( 197 CSRs.sstatus -> mstatus.sstatus.asUInt, 198 ) ++ SeqMap.from( 199 supervisorLevelCSRMods.map(csr => (csr.addr -> csr.regOut.asInstanceOf[CSRBundle].asUInt)).iterator 200 ) 201} 202 203class SstatusBundle extends CSRBundle { 204 val SIE = CSRWARLField (1, wNoFilter) 205 val SPIE = CSRWARLField (5, wNoFilter) 206 val UBE = CSRROField (6).withReset(0.U) 207 val SPP = CSRWARLField (8, wNoFilter).withReset(0.U) 208 val VS = ContextStatus (10, 9).withReset(ContextStatus.Off) 209 val FS = ContextStatus (14, 13).withReset(ContextStatus.Off) 210 val XS = ContextStatusRO(16, 15).withReset(0.U) 211 val SUM = CSRWARLField (18, wNoFilter).withReset(0.U) 212 val MXR = CSRWARLField (19, wNoFilter).withReset(0.U) 213 val SDT = CSRWARLField (24, wNoFilter).withReset(0.U) 214 val UXL = XLENField (33, 32).withReset(XLENField.XLEN64) 215 val SD = CSRROField (63, (_, _) => FS === ContextStatus.Dirty || VS === ContextStatus.Dirty) 216} 217 218class SieBundle extends InterruptEnableBundle { 219 this.getHS.foreach(_.setRW().withReset(0.U)) 220 this.STIE.setRO().withReset(0.U) 221 this.getLocal.foreach(_.setRW().withReset(0.U)) 222 this.getM .foreach(_.setHardWired(0.U)) 223 this.getVS.foreach(_.setHardWired(0.U)) 224 this.SGEIE.setHardWired(0.U) 225} 226 227class SipBundle extends InterruptPendingBundle { 228 // All pending bits in sip are aliases of mip or read-only 0 229 this.getM .foreach(_.setHardWired(0.U)) 230 this.getVS.foreach(_.setHardWired(0.U)) 231 this.SGEIP.setHardWired(0.U) 232} 233 234class SatpBundle extends CSRBundle { 235 val MODE = SatpMode(63, 60, null).withReset(SatpMode.Bare) 236 // WARL in privileged spec. 237 // RW, since we support max width of ASID 238 val ASID = RW(44 - 1 + ASIDLEN, 44).withReset(0.U) 239 // Do WARL in SatpModule/VSatpModule 240 val PPN = RW(43, 0).withReset(0.U) 241} 242 243class SEnvCfg extends EnvCfg 244 245class SipToMip extends IpValidBundle { 246 this.SSIP.bits.setRW() 247 this.LCOFIP.bits.setRW() 248} 249 250class SipToMvip extends IpValidBundle { 251 this.SSIP.bits.setRW() 252 this.getLocal.foreach(_.bits.setRW()) 253} 254 255class SieToMie extends IeValidBundle { 256 this.getHS.foreach(_.bits.setRW()) 257 this.getLocal.foreach(_.bits.setRW()) 258} 259 260trait HasMhpmeventOfBundle { self: CSRModule[_] => 261 val ofVec = IO(Input(UInt(perfCntNum.W))) 262 val privState = IO(Input(new PrivState)) 263 val mcounteren = IO(Input(new Counteren)) 264 val hcounteren = IO(Input(new Counteren)) 265}