1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.backend.fu 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import difftest._ 23import freechips.rocketchip.util._ 24import utils.MaskedRegMap.WritableMask 25import utils._ 26import xiangshan.ExceptionNO._ 27import xiangshan._ 28import xiangshan.backend.fu.util._ 29import xiangshan.cache._ 30 31// Trigger Tdata1 bundles 32trait HasTriggerConst { 33 def I_Trigger = 0.U 34 def S_Trigger = 1.U 35 def L_Trigger = 2.U 36 def GenESL(triggerType: UInt) = Cat((triggerType === I_Trigger), (triggerType === S_Trigger), (triggerType === L_Trigger)) 37} 38 39class TdataBundle extends Bundle { 40 val ttype = UInt(4.W) 41 val dmode = Bool() 42 val maskmax = UInt(6.W) 43 val zero1 = UInt(30.W) 44 val sizehi = UInt(2.W) 45 val hit = Bool() 46 val select = Bool() 47 val timing = Bool() 48 val sizelo = UInt(2.W) 49 val action = UInt(4.W) 50 val chain = Bool() 51 val matchType = UInt(4.W) 52 val m = Bool() 53 val zero2 = Bool() 54 val s = Bool() 55 val u = Bool() 56 val execute = Bool() 57 val store = Bool() 58 val load = Bool() 59} 60 61class FpuCsrIO extends Bundle { 62 val fflags = Output(Valid(UInt(5.W))) 63 val isIllegal = Output(Bool()) 64 val dirty_fs = Output(Bool()) 65 val frm = Input(UInt(3.W)) 66} 67 68 69class PerfCounterIO(implicit p: Parameters) extends XSBundle { 70 val perfEventsFrontend = Vec(numCSRPCntFrontend, new PerfEvent) 71 val perfEventsCtrl = Vec(numCSRPCntCtrl, new PerfEvent) 72 val perfEventsLsu = Vec(numCSRPCntLsu, new PerfEvent) 73 val perfEventsHc = Vec(numPCntHc * coreParams.L2NBanks, new PerfEvent) 74 val retiredInstr = UInt(3.W) 75 val frontendInfo = new Bundle { 76 val ibufFull = Bool() 77 val bpuInfo = new Bundle { 78 val bpRight = UInt(XLEN.W) 79 val bpWrong = UInt(XLEN.W) 80 } 81 } 82 val ctrlInfo = new Bundle { 83 val robFull = Bool() 84 val intdqFull = Bool() 85 val fpdqFull = Bool() 86 val lsdqFull = Bool() 87 } 88 val memInfo = new Bundle { 89 val sqFull = Bool() 90 val lqFull = Bool() 91 val dcacheMSHRFull = Bool() 92 } 93 94 val cacheInfo = new Bundle { 95 val l2MSHRFull = Bool() 96 val l3MSHRFull = Bool() 97 val l2nAcquire = UInt(XLEN.W) 98 val l2nAcquireMiss = UInt(XLEN.W) 99 val l3nAcquire = UInt(XLEN.W) 100 val l3nAcquireMiss = UInt(XLEN.W) 101 } 102} 103 104class CSRFileIO(implicit p: Parameters) extends XSBundle { 105 val hartId = Input(UInt(8.W)) 106 // output (for func === CSROpType.jmp) 107 val perf = Input(new PerfCounterIO) 108 val isPerfCnt = Output(Bool()) 109 // to FPU 110 val fpu = Flipped(new FpuCsrIO) 111 // from rob 112 val exception = Flipped(ValidIO(new ExceptionInfo)) 113 // to ROB 114 val isXRet = Output(Bool()) 115 val trapTarget = Output(UInt(VAddrBits.W)) 116 val interrupt = Output(Bool()) 117 // from LSQ 118 val memExceptionVAddr = Input(UInt(VAddrBits.W)) 119 // from outside cpu,externalInterrupt 120 val externalInterrupt = new ExternalInterruptIO 121 // TLB 122 val tlb = Output(new TlbCsrBundle) 123 // Debug Mode 124 val singleStep = Output(Bool()) 125 val debugMode = Output(Bool()) 126 // to Fence to disable sfence 127 val disableSfence = Output(Bool()) 128 // Custom microarchiture ctrl signal 129 val customCtrl = Output(new CustomCSRCtrlIO) 130 // distributed csr write 131 val distributedUpdate = Vec(2, Flipped(new DistributedCSRUpdateReq)) 132} 133 134class CSR(implicit p: Parameters) extends FunctionUnit with HasCSRConst with PMPMethod with PMAMethod with HasTriggerConst 135{ 136 val csrio = IO(new CSRFileIO) 137 138 val cfIn = io.in.bits.uop.cf 139 val cfOut = Wire(new CtrlFlow) 140 cfOut := cfIn 141 val flushPipe = Wire(Bool()) 142 143 val (valid, src1, src2, func) = ( 144 io.in.valid, 145 io.in.bits.src(0), 146 io.in.bits.uop.ctrl.imm, 147 io.in.bits.uop.ctrl.fuOpType 148 ) 149 150 // CSR define 151 152 class Priv extends Bundle { 153 val m = Output(Bool()) 154 val h = Output(Bool()) 155 val s = Output(Bool()) 156 val u = Output(Bool()) 157 } 158 159 val csrNotImplemented = RegInit(UInt(XLEN.W), 0.U) 160 161 class DcsrStruct extends Bundle { 162 val xdebugver = Output(UInt(2.W)) 163 val zero4 = Output(UInt(2.W)) 164 val zero3 = Output(UInt(12.W)) 165 val ebreakm = Output(Bool()) 166 val ebreakh = Output(Bool()) 167 val ebreaks = Output(Bool()) 168 val ebreaku = Output(Bool()) 169 val zero2 = Output(Bool()) 170 val stopcycle = Output(Bool()) 171 val stoptime = Output(Bool()) 172 val cause = Output(UInt(3.W)) 173 val zero1 = Output(UInt(3.W)) 174 val step = Output(Bool()) 175 val prv = Output(UInt(2.W)) 176 } 177 178 class MstatusStruct extends Bundle { 179 val sd = Output(UInt(1.W)) 180 181 val pad1 = if (XLEN == 64) Output(UInt(25.W)) else null 182 val mbe = if (XLEN == 64) Output(UInt(1.W)) else null 183 val sbe = if (XLEN == 64) Output(UInt(1.W)) else null 184 val sxl = if (XLEN == 64) Output(UInt(2.W)) else null 185 val uxl = if (XLEN == 64) Output(UInt(2.W)) else null 186 val pad0 = if (XLEN == 64) Output(UInt(9.W)) else Output(UInt(8.W)) 187 188 val tsr = Output(UInt(1.W)) 189 val tw = Output(UInt(1.W)) 190 val tvm = Output(UInt(1.W)) 191 val mxr = Output(UInt(1.W)) 192 val sum = Output(UInt(1.W)) 193 val mprv = Output(UInt(1.W)) 194 val xs = Output(UInt(2.W)) 195 val fs = Output(UInt(2.W)) 196 val mpp = Output(UInt(2.W)) 197 val hpp = Output(UInt(2.W)) 198 val spp = Output(UInt(1.W)) 199 val pie = new Priv 200 val ie = new Priv 201 assert(this.getWidth == XLEN) 202 203 def ube = pie.h // a little ugly 204 def ube_(r: UInt): Unit = { 205 pie.h := r(0) 206 } 207 } 208 209 class Interrupt extends Bundle { 210// val d = Output(Bool()) // Debug 211 val e = new Priv 212 val t = new Priv 213 val s = new Priv 214 } 215 216 // Debug CSRs 217 val dcsr = RegInit(UInt(32.W), 0x4000b010.U) 218 val dpc = Reg(UInt(64.W)) 219 val dscratch = Reg(UInt(64.W)) 220 val dscratch1 = Reg(UInt(64.W)) 221 val debugMode = RegInit(false.B) 222 val debugIntrEnable = RegInit(true.B) 223 csrio.debugMode := debugMode 224 225 val dpcPrev = RegNext(dpc) 226 XSDebug(dpcPrev =/= dpc, "Debug Mode: dpc is altered! Current is %x, previous is %x\n", dpc, dpcPrev) 227 228 // dcsr value table 229 // | debugver | 0100 230 // | zero | 10 bits of 0 231 // | ebreakvs | 0 232 // | ebreakvu | 0 233 // | ebreakm | 1 if ebreak enters debug 234 // | zero | 0 235 // | ebreaks | 236 // | ebreaku | 237 // | stepie | 0 disable interrupts in singlestep 238 // | stopcount| stop counter, 0 239 // | stoptime | stop time, 0 240 // | cause | 3 bits read only 241 // | v | 0 242 // | mprven | 1 243 // | nmip | read only 244 // | step | 245 // | prv | 2 bits 246 247 val dcsrData = Wire(new DcsrStruct) 248 dcsrData := dcsr.asTypeOf(new DcsrStruct) 249 val dcsrMask = ZeroExt(GenMask(15) | GenMask(13, 11) | GenMask(2, 0), XLEN)// Dcsr write mask 250 def dcsrUpdateSideEffect(dcsr: UInt): UInt = { 251 val dcsrOld = WireInit(dcsr.asTypeOf(new DcsrStruct)) 252 val dcsrNew = dcsr | (dcsrOld.prv(0) | dcsrOld.prv(1)).asUInt // turn 10 priv into 11 253 dcsrNew 254 } 255 csrio.singleStep := dcsrData.step 256 csrio.customCtrl.singlestep := dcsrData.step 257 258 // Trigger CSRs 259 260 val type_config = Array( 261 0.U -> I_Trigger, 1.U -> I_Trigger, 262 2.U -> S_Trigger, 3.U -> S_Trigger, 263 4.U -> L_Trigger, 5.U -> L_Trigger, // No.5 Load Trigger 264 6.U -> I_Trigger, 7.U -> S_Trigger, 265 8.U -> I_Trigger, 9.U -> L_Trigger 266 ) 267 def TypeLookup(select: UInt) = MuxLookup(select, I_Trigger, type_config) 268 269 val tdata1Phy = RegInit(VecInit(List.fill(10) {(2L << 60L).U(64.W)})) // init ttype 2 270 val tdata2Phy = Reg(Vec(10, UInt(64.W))) 271 val tselectPhy = RegInit(0.U(4.W)) 272 val tDummy1 = WireInit(0.U(64.W)) 273 val tDummy2 = WireInit(0.U(64.W)) 274 val tdata1Wire = Wire(UInt(64.W)) 275 val tdata2Wire = Wire(UInt(64.W)) 276 val tinfo = RegInit(2.U(64.W)) 277 val tControlPhy = RegInit(0.U(64.W)) 278 val triggerAction = RegInit(false.B) 279 tdata1Wire := tdata1Phy(tselectPhy) 280 tdata2Wire := tdata2Phy(tselectPhy) 281 tDummy1 := tdata1Phy(tselectPhy) 282 tDummy2 := tdata2Phy(tselectPhy) 283 284 def ReadTdata1(rdata: UInt) = { 285 val tdata1 = WireInit(tdata1Wire) 286 val read_data = tdata1Wire 287 XSDebug(src2(11, 0) === Tdata1.U && valid, p"\nDebug Mode: tdata1(${tselectPhy})is read, the actual value is ${Binary(tdata1)}\n") 288 read_data | (triggerAction << 12) // fix action 289 } 290 def WriteTdata1(wdata: UInt) = { 291 val tdata1 = WireInit(tdata1Wire.asTypeOf(new TdataBundle)) 292 val wdata_wire = WireInit(wdata.asTypeOf(new TdataBundle)) 293 val tdata1_new = WireInit(wdata.asTypeOf(new TdataBundle)) 294 XSDebug(src2(11, 0) === Tdata1.U && valid && func =/= CSROpType.jmp, p"Debug Mode: tdata1(${tselectPhy})is written, the actual value is ${wdata}\n") 295// tdata1_new.hit := wdata(20) 296 tdata1_new.ttype := tdata1.ttype 297 tdata1_new.dmode := Mux(debugMode, wdata_wire.dmode, tdata1.dmode) 298 tdata1_new.maskmax := 0.U 299 tdata1_new.hit := 0.U 300 tdata1_new.select := (TypeLookup(tselectPhy) === I_Trigger) && wdata_wire.select 301 when(wdata_wire.action <= 1.U){ 302 triggerAction := tdata1_new.action(0) 303 } .otherwise{ 304 tdata1_new.action := tdata1.action 305 } 306 tdata1_new.timing := false.B // hardwire this because we have singlestep 307 tdata1_new.zero1 := 0.U 308 tdata1_new.zero2 := 0.U 309 tdata1_new.chain := !tselectPhy(0) && wdata_wire.chain 310 when(wdata_wire.matchType =/= 0.U && wdata_wire.matchType =/= 2.U && wdata_wire.matchType =/= 3.U) { 311 tdata1_new.matchType := tdata1.matchType 312 } 313 tdata1_new.sizehi := Mux(wdata_wire.select && TypeLookup(tselectPhy) === I_Trigger, 0.U, 1.U) 314 tdata1_new.sizelo:= Mux(wdata_wire.select && TypeLookup(tselectPhy) === I_Trigger, 3.U, 1.U) 315 tdata1_new.execute := TypeLookup(tselectPhy) === I_Trigger 316 tdata1_new.store := TypeLookup(tselectPhy) === S_Trigger 317 tdata1_new.load := TypeLookup(tselectPhy) === L_Trigger 318 when(valid && func =/= CSROpType.jmp && addr === Tdata1.U) { 319 tdata1Phy(tselectPhy) := tdata1_new.asUInt() 320 } 321 0.U 322 } 323 324 def WriteTselect(wdata: UInt) = { 325 Mux(wdata < 10.U, wdata(3, 0), tselectPhy) 326 } 327 328 def ReadTdata2(tdata: UInt) = tdata2Phy(tselectPhy) 329 def WriteTdata2(wdata: UInt) = { 330 when(valid && func =/= CSROpType.jmp && addr === Tdata2.U) { 331 tdata2Phy(tselectPhy) := wdata 332 } 333 0.U 334 } 335 336 337 val tcontrolWriteMask = ZeroExt(GenMask(3) | GenMask(7), XLEN) 338 339 340 def GenTdataDistribute(tdata1: TdataBundle, tdata2: UInt): MatchTriggerIO = { 341 val res = Wire(new MatchTriggerIO) 342 res.matchType := tdata1.matchType 343 res.select := tdata1.select 344 res.timing := tdata1.timing 345 res.action := triggerAction 346 res.chain := tdata1.chain 347 res.tdata2 := tdata2 348 res 349 } 350 351 csrio.customCtrl.frontend_trigger.t.bits.addr := MuxLookup(tselectPhy, 0.U, Seq( 352 0.U -> 0.U, 353 1.U -> 1.U, 354 6.U -> 2.U, 355 8.U -> 3.U 356 )) 357 csrio.customCtrl.mem_trigger.t.bits.addr := MuxLookup(tselectPhy, 0.U, Seq( 358 2.U -> 0.U, 359 3.U -> 1.U, 360 4.U -> 2.U, 361 5.U -> 3.U, 362 7.U -> 4.U, 363 9.U -> 5.U 364 )) 365 csrio.customCtrl.frontend_trigger.t.bits.tdata := GenTdataDistribute(tdata1Phy(tselectPhy).asTypeOf(new TdataBundle), tdata2Phy(tselectPhy)) 366 csrio.customCtrl.mem_trigger.t.bits.tdata := GenTdataDistribute(tdata1Phy(tselectPhy).asTypeOf(new TdataBundle), tdata2Phy(tselectPhy)) 367 368 // Machine-Level CSRs 369 370 val mtvec = RegInit(UInt(XLEN.W), 0.U) 371 val mcounteren = RegInit(UInt(XLEN.W), 0.U) 372 val mcause = RegInit(UInt(XLEN.W), 0.U) 373 val mtval = RegInit(UInt(XLEN.W), 0.U) 374 val mepc = Reg(UInt(XLEN.W)) 375 // Page 36 in riscv-priv: The low bit of mepc (mepc[0]) is always zero. 376 val mepcMask = ~(0x1.U(XLEN.W)) 377 378 val mie = RegInit(0.U(XLEN.W)) 379 val mipWire = WireInit(0.U.asTypeOf(new Interrupt)) 380 val mipReg = RegInit(0.U(XLEN.W)) 381 val mipFixMask = ZeroExt(GenMask(9) | GenMask(5) | GenMask(1), XLEN) 382 val mip = (mipWire.asUInt | mipReg).asTypeOf(new Interrupt) 383 384 def getMisaMxl(mxl: Int): UInt = {mxl.U << (XLEN-2)}.asUInt() 385 def getMisaExt(ext: Char): UInt = {1.U << (ext.toInt - 'a'.toInt)}.asUInt() 386 var extList = List('a', 's', 'i', 'u') 387 if (HasMExtension) { extList = extList :+ 'm' } 388 if (HasCExtension) { extList = extList :+ 'c' } 389 if (HasFPU) { extList = extList ++ List('f', 'd') } 390 val misaInitVal = getMisaMxl(2) | extList.foldLeft(0.U)((sum, i) => sum | getMisaExt(i)) //"h8000000000141105".U 391 val misa = RegInit(UInt(XLEN.W), misaInitVal) 392 393 // MXL = 2 | 0 | EXT = b 00 0000 0100 0001 0001 0000 0101 394 // (XLEN-1, XLEN-2) | |(25, 0) ZY XWVU TSRQ PONM LKJI HGFE DCBA 395 396 val mvendorid = RegInit(UInt(XLEN.W), 0.U) // this is a non-commercial implementation 397 val marchid = RegInit(UInt(XLEN.W), 0.U) // return 0 to indicate the field is not implemented 398 val mimpid = RegInit(UInt(XLEN.W), 0.U) // provides a unique encoding of the version of the processor implementation 399 val mhartid = RegInit(UInt(XLEN.W), csrio.hartId) // the hardware thread running the code 400 val mconfigptr = RegInit(UInt(XLEN.W), 0.U) // the read-only pointer pointing to the platform config structure, 0 for not supported. 401 val mstatus = RegInit("ha00000000".U(XLEN.W)) 402 403 // mstatus Value Table 404 // | sd | 405 // | pad1 | 406 // | sxl | hardlinked to 10, use 00 to pass xv6 test 407 // | uxl | hardlinked to 10 408 // | pad0 | 409 // | tsr | 410 // | tw | 411 // | tvm | 412 // | mxr | 413 // | sum | 414 // | mprv | 415 // | xs | 00 | 416 // | fs | 00 | 417 // | mpp | 00 | 418 // | hpp | 00 | 419 // | spp | 0 | 420 // | pie | 0000 | pie.h is used as UBE 421 // | ie | 0000 | uie hardlinked to 0, as N ext is not implemented 422 423 val mstatusStruct = mstatus.asTypeOf(new MstatusStruct) 424 def mstatusUpdateSideEffect(mstatus: UInt): UInt = { 425 val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct)) 426 val mstatusNew = Cat(mstatusOld.xs === "b11".U || mstatusOld.fs === "b11".U, mstatus(XLEN-2, 0)) 427 mstatusNew 428 } 429 430 val mstatusWMask = (~ZeroExt(( 431 GenMask(XLEN - 2, 36) | // WPRI 432 GenMask(35, 32) | // SXL and UXL cannot be changed 433 GenMask(31, 23) | // WPRI 434 GenMask(16, 15) | // XS is read-only 435 GenMask(10, 9) | // WPRI 436 GenMask(6) | // WPRI 437 GenMask(2) // WPRI 438 ), 64)).asUInt() 439 val mstatusMask = (~ZeroExt(( 440 GenMask(XLEN - 2, 36) | // WPRI 441 GenMask(31, 23) | // WPRI 442 GenMask(10, 9) | // WPRI 443 GenMask(6) | // WPRI 444 GenMask(2) // WPRI 445 ), 64)).asUInt() 446 447 val medeleg = RegInit(UInt(XLEN.W), 0.U) 448 val mideleg = RegInit(UInt(XLEN.W), 0.U) 449 val mscratch = RegInit(UInt(XLEN.W), 0.U) 450 451 // PMP Mapping 452 val pmp = Wire(Vec(NumPMP, new PMPEntry())) // just used for method parameter 453 val pma = Wire(Vec(NumPMA, new PMPEntry())) // just used for method parameter 454 val pmpMapping = pmp_gen_mapping(pmp_init, NumPMP, PmpcfgBase, PmpaddrBase, pmp) 455 val pmaMapping = pmp_gen_mapping(pma_init, NumPMA, PmacfgBase, PmaaddrBase, pma) 456 457 // Superviser-Level CSRs 458 459 // val sstatus = RegInit(UInt(XLEN.W), "h00000000".U) 460 val sstatusWmask = "hc6122".U(XLEN.W) 461 // Sstatus Write Mask 462 // ------------------------------------------------------- 463 // 19 9 5 2 464 // 0 1100 0000 0001 0010 0010 465 // 0 c 0 1 2 2 466 // ------------------------------------------------------- 467 val sstatusRmask = sstatusWmask | "h8000000300018000".U 468 // Sstatus Read Mask = (SSTATUS_WMASK | (0xf << 13) | (1ull << 63) | (3ull << 32)) 469 val stvec = RegInit(UInt(XLEN.W), 0.U) 470 // val sie = RegInit(0.U(XLEN.W)) 471 val sieMask = "h222".U & mideleg 472 val sipMask = "h222".U & mideleg 473 val sipWMask = "h2".U(XLEN.W) // ssip is writeable in smode 474 val satp = if(EnbaleTlbDebug) RegInit(UInt(XLEN.W), "h8000000000087fbe".U) else RegInit(0.U(XLEN.W)) 475 // val satp = RegInit(UInt(XLEN.W), "h8000000000087fbe".U) // only use for tlb naive debug 476 // val satpMask = "h80000fffffffffff".U(XLEN.W) // disable asid, mode can only be 8 / 0 477 // TODO: use config to control the length of asid 478 // val satpMask = "h8fffffffffffffff".U(XLEN.W) // enable asid, mode can only be 8 / 0 479 val satpMask = Cat("h8".U(Satp_Mode_len.W), satp_part_wmask(Satp_Asid_len, AsidLength), satp_part_wmask(Satp_Addr_len, PAddrBits-12)) 480 val sepc = RegInit(UInt(XLEN.W), 0.U) 481 // Page 60 in riscv-priv: The low bit of sepc (sepc[0]) is always zero. 482 val sepcMask = ~(0x1.U(XLEN.W)) 483 val scause = RegInit(UInt(XLEN.W), 0.U) 484 val stval = Reg(UInt(XLEN.W)) 485 val sscratch = RegInit(UInt(XLEN.W), 0.U) 486 val scounteren = RegInit(UInt(XLEN.W), 0.U) 487 488 // sbpctl 489 // Bits 0-7: {LOOP, RAS, SC, TAGE, BIM, BTB, uBTB} 490 val sbpctl = RegInit(UInt(XLEN.W), "h7f".U) 491 csrio.customCtrl.bp_ctrl.ubtb_enable := sbpctl(0) 492 csrio.customCtrl.bp_ctrl.btb_enable := sbpctl(1) 493 csrio.customCtrl.bp_ctrl.bim_enable := sbpctl(2) 494 csrio.customCtrl.bp_ctrl.tage_enable := sbpctl(3) 495 csrio.customCtrl.bp_ctrl.sc_enable := sbpctl(4) 496 csrio.customCtrl.bp_ctrl.ras_enable := sbpctl(5) 497 csrio.customCtrl.bp_ctrl.loop_enable := sbpctl(6) 498 499 // spfctl Bit 0: L1plusCache Prefetcher Enable 500 // spfctl Bit 1: L2Cache Prefetcher Enable 501 val spfctl = RegInit(UInt(XLEN.W), "h3".U) 502 csrio.customCtrl.l1plus_pf_enable := spfctl(0) 503 csrio.customCtrl.l2_pf_enable := spfctl(1) 504 505 // sdsid: Differentiated Services ID 506 val sdsid = RegInit(UInt(XLEN.W), 0.U) 507 csrio.customCtrl.dsid := sdsid 508 509 // slvpredctl: load violation predict settings 510 val slvpredctl = RegInit(UInt(XLEN.W), "h70".U) // default reset period: 2^17 511 csrio.customCtrl.lvpred_disable := slvpredctl(0) 512 csrio.customCtrl.no_spec_load := slvpredctl(1) 513 csrio.customCtrl.storeset_wait_store := slvpredctl(2) 514 csrio.customCtrl.storeset_no_fast_wakeup := slvpredctl(3) 515 csrio.customCtrl.lvpred_timeout := slvpredctl(8, 4) 516 517 // smblockctl: memory block configurations 518 // bits 0-3: store buffer flush threshold (default: 8 entries) 519 val smblockctl_init_val = 520 ("hf".U & StoreBufferThreshold.U) | 521 (EnableLdVioCheckAfterReset.B.asUInt << 4) 522 val smblockctl = RegInit(UInt(XLEN.W), smblockctl_init_val) 523 csrio.customCtrl.sbuffer_threshold := smblockctl(3, 0) 524 // bits 4: enable load load violation check 525 csrio.customCtrl.ldld_vio_check := smblockctl(4) 526 527 val srnctl = RegInit(UInt(XLEN.W), "h3".U) 528 csrio.customCtrl.move_elim_enable := srnctl(0) 529 csrio.customCtrl.svinval_enable := srnctl(1) 530 531 val tlbBundle = Wire(new TlbCsrBundle) 532 tlbBundle.satp.apply(satp) 533 534 csrio.tlb := tlbBundle 535 536 // User-Level CSRs 537 val uepc = Reg(UInt(XLEN.W)) 538 539 // fcsr 540 class FcsrStruct extends Bundle { 541 val reserved = UInt((XLEN-3-5).W) 542 val frm = UInt(3.W) 543 val fflags = UInt(5.W) 544 assert(this.getWidth == XLEN) 545 } 546 val fcsr = RegInit(0.U(XLEN.W)) 547 // set mstatus->sd and mstatus->fs when true 548 val csrw_dirty_fp_state = WireInit(false.B) 549 550 def frm_wfn(wdata: UInt): UInt = { 551 val fcsrOld = WireInit(fcsr.asTypeOf(new FcsrStruct)) 552 csrw_dirty_fp_state := true.B 553 fcsrOld.frm := wdata(2,0) 554 fcsrOld.asUInt() 555 } 556 def frm_rfn(rdata: UInt): UInt = rdata(7,5) 557 558 def fflags_wfn(update: Boolean)(wdata: UInt): UInt = { 559 val fcsrOld = fcsr.asTypeOf(new FcsrStruct) 560 val fcsrNew = WireInit(fcsrOld) 561 csrw_dirty_fp_state := true.B 562 if (update) { 563 fcsrNew.fflags := wdata(4,0) | fcsrOld.fflags 564 } else { 565 fcsrNew.fflags := wdata(4,0) 566 } 567 fcsrNew.asUInt() 568 } 569 def fflags_rfn(rdata:UInt): UInt = rdata(4,0) 570 571 def fcsr_wfn(wdata: UInt): UInt = { 572 val fcsrOld = WireInit(fcsr.asTypeOf(new FcsrStruct)) 573 csrw_dirty_fp_state := true.B 574 Cat(fcsrOld.reserved, wdata.asTypeOf(fcsrOld).frm, wdata.asTypeOf(fcsrOld).fflags) 575 } 576 577 val fcsrMapping = Map( 578 MaskedRegMap(Fflags, fcsr, wfn = fflags_wfn(update = false), rfn = fflags_rfn), 579 MaskedRegMap(Frm, fcsr, wfn = frm_wfn, rfn = frm_rfn), 580 MaskedRegMap(Fcsr, fcsr, wfn = fcsr_wfn) 581 ) 582 583 // Hart Priviledge Mode 584 val priviledgeMode = RegInit(UInt(2.W), ModeM) 585 586 //val perfEventscounten = List.fill(nrPerfCnts)(RegInit(false(Bool()))) 587 // Perf Counter 588 val nrPerfCnts = 29 // 3...31 589 val priviledgeModeOH = UIntToOH(priviledgeMode) 590 val perfEventscounten = RegInit(0.U.asTypeOf(Vec(nrPerfCnts, Bool()))) 591 val perfCnts = List.fill(nrPerfCnts)(RegInit(0.U(XLEN.W))) 592 val perfEvents = List.fill(8)(RegInit("h0000000000".U(XLEN.W))) ++ 593 List.fill(8)(RegInit("h4010040100".U(XLEN.W))) ++ 594 List.fill(8)(RegInit("h8020080200".U(XLEN.W))) ++ 595 List.fill(5)(RegInit("hc0300c0300".U(XLEN.W))) 596 for (i <-0 until nrPerfCnts) { 597 perfEventscounten(i) := (Cat(perfEvents(i)(62),perfEvents(i)(61),(perfEvents(i)(61,60))) & priviledgeModeOH).orR 598 } 599 600 val hpmEvents = Wire(Vec(numPCntHc * coreParams.L2NBanks, new PerfEvent)) 601 for (i <- 0 until numPCntHc * coreParams.L2NBanks) { 602 hpmEvents(i) := csrio.perf.perfEventsHc(i) 603 } 604 605 val csrevents = perfEvents.slice(24, 29) 606 val hpm_hc = HPerfMonitor(csrevents, hpmEvents) 607 val mcountinhibit = RegInit(0.U(XLEN.W)) 608 val mcycle = RegInit(0.U(XLEN.W)) 609 mcycle := mcycle + 1.U 610 val minstret = RegInit(0.U(XLEN.W)) 611 val perf_events = csrio.perf.perfEventsFrontend ++ 612 csrio.perf.perfEventsCtrl ++ 613 csrio.perf.perfEventsLsu ++ 614 hpm_hc.getPerf 615 minstret := minstret + RegNext(csrio.perf.retiredInstr) 616 for(i <- 0 until 29){ 617 perfCnts(i) := Mux(mcountinhibit(i+3) | !perfEventscounten(i), perfCnts(i), perfCnts(i) + perf_events(i).value) 618 } 619 620 // CSR reg map 621 val basicPrivMapping = Map( 622 623 //--- User Trap Setup --- 624 // MaskedRegMap(Ustatus, ustatus), 625 // MaskedRegMap(Uie, uie, 0.U, MaskedRegMap.Unwritable), 626 // MaskedRegMap(Utvec, utvec), 627 628 //--- User Trap Handling --- 629 // MaskedRegMap(Uscratch, uscratch), 630 // MaskedRegMap(Uepc, uepc), 631 // MaskedRegMap(Ucause, ucause), 632 // MaskedRegMap(Utval, utval), 633 // MaskedRegMap(Uip, uip), 634 635 //--- User Counter/Timers --- 636 // MaskedRegMap(Cycle, cycle), 637 // MaskedRegMap(Time, time), 638 // MaskedRegMap(Instret, instret), 639 640 //--- Supervisor Trap Setup --- 641 MaskedRegMap(Sstatus, mstatus, sstatusWmask, mstatusUpdateSideEffect, sstatusRmask), 642 // MaskedRegMap(Sedeleg, Sedeleg), 643 // MaskedRegMap(Sideleg, Sideleg), 644 MaskedRegMap(Sie, mie, sieMask, MaskedRegMap.NoSideEffect, sieMask), 645 MaskedRegMap(Stvec, stvec), 646 MaskedRegMap(Scounteren, scounteren), 647 648 //--- Supervisor Trap Handling --- 649 MaskedRegMap(Sscratch, sscratch), 650 MaskedRegMap(Sepc, sepc, sepcMask, MaskedRegMap.NoSideEffect, sepcMask), 651 MaskedRegMap(Scause, scause), 652 MaskedRegMap(Stval, stval), 653 MaskedRegMap(Sip, mip.asUInt, sipWMask, MaskedRegMap.Unwritable, sipMask), 654 655 //--- Supervisor Protection and Translation --- 656 MaskedRegMap(Satp, satp, satpMask, MaskedRegMap.NoSideEffect, satpMask), 657 658 //--- Supervisor Custom Read/Write Registers 659 MaskedRegMap(Sbpctl, sbpctl), 660 MaskedRegMap(Spfctl, spfctl), 661 MaskedRegMap(Sdsid, sdsid), 662 MaskedRegMap(Slvpredctl, slvpredctl), 663 MaskedRegMap(Smblockctl, smblockctl), 664 MaskedRegMap(Srnctl, srnctl), 665 666 //--- Machine Information Registers --- 667 MaskedRegMap(Mvendorid, mvendorid, 0.U(XLEN.W), MaskedRegMap.Unwritable), 668 MaskedRegMap(Marchid, marchid, 0.U(XLEN.W), MaskedRegMap.Unwritable), 669 MaskedRegMap(Mimpid, mimpid, 0.U(XLEN.W), MaskedRegMap.Unwritable), 670 MaskedRegMap(Mhartid, mhartid, 0.U(XLEN.W), MaskedRegMap.Unwritable), 671 MaskedRegMap(Mconfigptr, mconfigptr, 0.U(XLEN.W), MaskedRegMap.Unwritable), 672 673 //--- Machine Trap Setup --- 674 MaskedRegMap(Mstatus, mstatus, mstatusWMask, mstatusUpdateSideEffect, mstatusMask), 675 MaskedRegMap(Misa, misa), // now MXL, EXT is not changeable 676 MaskedRegMap(Medeleg, medeleg, "hf3ff".U(XLEN.W)), 677 MaskedRegMap(Mideleg, mideleg, "h222".U(XLEN.W)), 678 MaskedRegMap(Mie, mie), 679 MaskedRegMap(Mtvec, mtvec), 680 MaskedRegMap(Mcounteren, mcounteren), 681 682 //--- Machine Trap Handling --- 683 MaskedRegMap(Mscratch, mscratch), 684 MaskedRegMap(Mepc, mepc, mepcMask, MaskedRegMap.NoSideEffect, mepcMask), 685 MaskedRegMap(Mcause, mcause), 686 MaskedRegMap(Mtval, mtval), 687 MaskedRegMap(Mip, mip.asUInt, 0.U(XLEN.W), MaskedRegMap.Unwritable), 688 689 //--- Trigger --- 690 MaskedRegMap(Tselect, tselectPhy, WritableMask, WriteTselect), 691 MaskedRegMap(Tdata1, tDummy1, WritableMask, WriteTdata1, WritableMask, ReadTdata1), 692 MaskedRegMap(Tdata2, tDummy2, WritableMask, WriteTdata2, WritableMask, ReadTdata2), 693 MaskedRegMap(Tinfo, tinfo, 0.U(XLEN.W), MaskedRegMap.Unwritable), 694 MaskedRegMap(Tcontrol, tControlPhy, tcontrolWriteMask), 695 696 //--- Debug Mode --- 697 MaskedRegMap(Dcsr, dcsr, dcsrMask, dcsrUpdateSideEffect), 698 MaskedRegMap(Dpc, dpc), 699 MaskedRegMap(Dscratch, dscratch), 700 MaskedRegMap(Dscratch1, dscratch1), 701 MaskedRegMap(Mcountinhibit, mcountinhibit), 702 MaskedRegMap(Mcycle, mcycle), 703 MaskedRegMap(Minstret, minstret), 704 ) 705 706 val perfCntMapping = (0 until 29).map(i => {Map( 707 MaskedRegMap(addr = Mhpmevent3 +i, 708 reg = perfEvents(i), 709 wmask = "hf87fff3fcff3fcff".U(XLEN.W)), 710 MaskedRegMap(addr = Mhpmcounter3 +i, 711 reg = perfCnts(i)) 712 )}).fold(Map())((a,b) => a ++ b) 713 // TODO: mechanism should be implemented later 714 // val MhpmcounterStart = Mhpmcounter3 715 // val MhpmeventStart = Mhpmevent3 716 // for (i <- 0 until nrPerfCnts) { 717 // perfCntMapping += MaskedRegMap(MhpmcounterStart + i, perfCnts(i)) 718 // perfCntMapping += MaskedRegMap(MhpmeventStart + i, perfEvents(i)) 719 // } 720 721 val cacheopRegs = CacheInstrucion.CacheInsRegisterList.map{case (name, attribute) => { 722 name -> RegInit(0.U(attribute("width").toInt.W)) 723 }} 724 val cacheopMapping = CacheInstrucion.CacheInsRegisterList.map{case (name, attribute) => { 725 MaskedRegMap( 726 Scachebase + attribute("offset").toInt, 727 cacheopRegs(name) 728 ) 729 }} 730 731 val mapping = basicPrivMapping ++ 732 perfCntMapping ++ 733 pmpMapping ++ 734 pmaMapping ++ 735 (if (HasFPU) fcsrMapping else Nil) ++ 736 (if (HasCustomCSRCacheOp) cacheopMapping else Nil) 737 738 val addr = src2(11, 0) 739 val csri = ZeroExt(src2(16, 12), XLEN) 740 val rdata = Wire(UInt(XLEN.W)) 741 val wdata = LookupTree(func, List( 742 CSROpType.wrt -> src1, 743 CSROpType.set -> (rdata | src1), 744 CSROpType.clr -> (rdata & (~src1).asUInt()), 745 CSROpType.wrti -> csri, 746 CSROpType.seti -> (rdata | csri), 747 CSROpType.clri -> (rdata & (~csri).asUInt()) 748 )) 749 750 val addrInPerfCnt = (addr >= Mcycle.U) && (addr <= Mhpmcounter31.U) || 751 (addr >= Mcountinhibit.U) && (addr <= Mhpmevent31.U) 752 csrio.isPerfCnt := addrInPerfCnt && valid && func =/= CSROpType.jmp 753 754 // satp wen check 755 val satpLegalMode = (wdata.asTypeOf(new SatpStruct).mode===0.U) || (wdata.asTypeOf(new SatpStruct).mode===8.U) 756 757 // csr access check, special case 758 val tvmNotPermit = (priviledgeMode === ModeS && mstatusStruct.tvm.asBool) 759 val accessPermitted = !(addr === Satp.U && tvmNotPermit) 760 csrio.disableSfence := tvmNotPermit 761 762 // general CSR wen check 763 val wen = valid && func =/= CSROpType.jmp && (addr=/=Satp.U || satpLegalMode) 764 val dcsrPermitted = dcsrPermissionCheck(addr, false.B, debugMode) 765 val triggerPermitted = triggerPermissionCheck(addr, true.B, debugMode) // todo dmode 766 val modePermitted = csrAccessPermissionCheck(addr, false.B, priviledgeMode) && dcsrPermitted && triggerPermitted 767 val perfcntPermitted = perfcntPermissionCheck(addr, priviledgeMode, mcounteren, scounteren) 768 val permitted = Mux(addrInPerfCnt, perfcntPermitted, modePermitted) && accessPermitted 769 770 MaskedRegMap.generate(mapping, addr, rdata, wen && permitted, wdata) 771 io.out.bits.data := rdata 772 io.out.bits.uop := io.in.bits.uop 773 io.out.bits.uop.cf := cfOut 774 io.out.bits.uop.ctrl.flushPipe := flushPipe 775 776 // send distribute csr a w signal 777 csrio.customCtrl.distribute_csr.w.valid := wen && permitted 778 csrio.customCtrl.distribute_csr.w.bits.data := wdata 779 csrio.customCtrl.distribute_csr.w.bits.addr := addr 780 781 // Fix Mip/Sip write 782 val fixMapping = Map( 783 MaskedRegMap(Mip, mipReg.asUInt, mipFixMask), 784 MaskedRegMap(Sip, mipReg.asUInt, sipWMask, MaskedRegMap.NoSideEffect, sipMask) 785 ) 786 val rdataFix = Wire(UInt(XLEN.W)) 787 val wdataFix = LookupTree(func, List( 788 CSROpType.wrt -> src1, 789 CSROpType.set -> (rdataFix | src1), 790 CSROpType.clr -> (rdataFix & (~src1).asUInt()), 791 CSROpType.wrti -> csri, 792 CSROpType.seti -> (rdataFix | csri), 793 CSROpType.clri -> (rdataFix & (~csri).asUInt()) 794 )) 795 MaskedRegMap.generate(fixMapping, addr, rdataFix, wen && permitted, wdataFix) 796 797 when (csrio.fpu.fflags.valid) { 798 fcsr := fflags_wfn(update = true)(csrio.fpu.fflags.bits) 799 } 800 // set fs and sd in mstatus 801 when (csrw_dirty_fp_state || csrio.fpu.dirty_fs) { 802 val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct)) 803 mstatusNew.fs := "b11".U 804 mstatusNew.sd := true.B 805 mstatus := mstatusNew.asUInt() 806 } 807 csrio.fpu.frm := fcsr.asTypeOf(new FcsrStruct).frm 808 809 810 // Trigger Ctrl 811 csrio.customCtrl.trigger_enable := tdata1Phy.map{t => 812 def tdata1 = t.asTypeOf(new TdataBundle) 813 tdata1.m && priviledgeMode === ModeM || 814 tdata1.s && priviledgeMode === ModeS || tdata1.u && priviledgeMode === ModeU 815 } 816 csrio.customCtrl.frontend_trigger.t.valid := RegNext(wen && (addr === Tdata1.U || addr === Tdata2.U) && TypeLookup(tselectPhy) === I_Trigger) 817 csrio.customCtrl.mem_trigger.t.valid := RegNext(wen && (addr === Tdata1.U || addr === Tdata2.U) && TypeLookup(tselectPhy) =/= I_Trigger) 818 XSDebug(csrio.customCtrl.trigger_enable.asUInt.orR(), p"Debug Mode: At least 1 trigger is enabled, trigger enable is ${Binary(csrio.customCtrl.trigger_enable.asUInt())}\n") 819 820 // CSR inst decode 821 val isEbreak = addr === privEbreak && func === CSROpType.jmp 822 val isEcall = addr === privEcall && func === CSROpType.jmp 823 val isMret = addr === privMret && func === CSROpType.jmp 824 val isSret = addr === privSret && func === CSROpType.jmp 825 val isUret = addr === privUret && func === CSROpType.jmp 826 val isDret = addr === privDret && func === CSROpType.jmp 827 828 XSDebug(wen, "csr write: pc %x addr %x rdata %x wdata %x func %x\n", cfIn.pc, addr, rdata, wdata, func) 829 XSDebug(wen, "pc %x mstatus %x mideleg %x medeleg %x mode %x\n", cfIn.pc, mstatus, mideleg , medeleg, priviledgeMode) 830 831 // Illegal priviledged operation list 832 val illegalSModeSret = valid && isSret && priviledgeMode === ModeS && mstatusStruct.tsr.asBool 833 834 // Illegal priviledged instruction check 835 val isIllegalAddr = MaskedRegMap.isIllegalAddr(mapping, addr) 836 val isIllegalAccess = !permitted 837 val isIllegalPrivOp = illegalSModeSret 838 839 // expose several csr bits for tlb 840 tlbBundle.priv.mxr := mstatusStruct.mxr.asBool 841 tlbBundle.priv.sum := mstatusStruct.sum.asBool 842 tlbBundle.priv.imode := priviledgeMode 843 tlbBundle.priv.dmode := Mux(mstatusStruct.mprv.asBool, mstatusStruct.mpp, priviledgeMode) 844 845 // Branch control 846 val retTarget = Wire(UInt(VAddrBits.W)) 847 val resetSatp = addr === Satp.U && wen // write to satp will cause the pipeline be flushed 848 flushPipe := resetSatp || (valid && func === CSROpType.jmp && !isEcall && !isEbreak) 849 850 retTarget := DontCare 851 // val illegalEret = TODO 852 853 when (valid && isDret) { 854 val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct)) 855 val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct)) 856 val dcsrNew = WireInit(dcsr.asTypeOf(new DcsrStruct)) 857 val debugModeNew = WireInit(debugMode) 858 when (dcsr.asTypeOf(new DcsrStruct).prv =/= ModeM) {mstatusNew.mprv := 0.U} //If the new privilege mode is less privileged than M-mode, MPRV in mstatus is cleared. 859 mstatus := mstatusNew.asUInt 860 priviledgeMode := dcsrNew.prv 861 retTarget := dpc(VAddrBits-1, 0) 862 debugModeNew := false.B 863 debugIntrEnable := true.B 864 debugMode := debugModeNew 865 XSDebug("Debug Mode: Dret executed, returning to %x.", retTarget) 866 } 867 868 when (valid && isMret) { 869 val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct)) 870 val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct)) 871 mstatusNew.ie.m := mstatusOld.pie.m 872 priviledgeMode := mstatusOld.mpp 873 mstatusNew.pie.m := true.B 874 mstatusNew.mpp := ModeU 875 when (mstatusOld.mpp =/= ModeM) { mstatusNew.mprv := 0.U } 876 mstatus := mstatusNew.asUInt 877 // lr := false.B 878 retTarget := mepc(VAddrBits-1, 0) 879 } 880 881 when (valid && isSret && !illegalSModeSret) { 882 val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct)) 883 val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct)) 884 mstatusNew.ie.s := mstatusOld.pie.s 885 priviledgeMode := Cat(0.U(1.W), mstatusOld.spp) 886 mstatusNew.pie.s := true.B 887 mstatusNew.spp := ModeU 888 mstatus := mstatusNew.asUInt 889 when (mstatusOld.spp =/= ModeM) { mstatusNew.mprv := 0.U } 890 // lr := false.B 891 retTarget := sepc(VAddrBits-1, 0) 892 } 893 894 when (valid && isUret) { 895 val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct)) 896 val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct)) 897 // mstatusNew.mpp.m := ModeU //TODO: add mode U 898 mstatusNew.ie.u := mstatusOld.pie.u 899 priviledgeMode := ModeU 900 mstatusNew.pie.u := true.B 901 mstatus := mstatusNew.asUInt 902 retTarget := uepc(VAddrBits-1, 0) 903 } 904 905 io.in.ready := true.B 906 io.out.valid := valid 907 908 val ebreakCauseException = (priviledgeMode === ModeM && dcsrData.ebreakm) || (priviledgeMode === ModeS && dcsrData.ebreaks) || (priviledgeMode === ModeU && dcsrData.ebreaku) 909 910 val csrExceptionVec = WireInit(cfIn.exceptionVec) 911 csrExceptionVec(breakPoint) := io.in.valid && isEbreak && ebreakCauseException 912 csrExceptionVec(ecallM) := priviledgeMode === ModeM && io.in.valid && isEcall 913 csrExceptionVec(ecallS) := priviledgeMode === ModeS && io.in.valid && isEcall 914 csrExceptionVec(ecallU) := priviledgeMode === ModeU && io.in.valid && isEcall 915 // Trigger an illegal instr exception when: 916 // * unimplemented csr is being read/written 917 // * csr access is illegal 918 csrExceptionVec(illegalInstr) := (isIllegalAddr || isIllegalAccess) && wen 919 cfOut.exceptionVec := csrExceptionVec 920 921 XSDebug(io.in.valid && isEbreak, s"Debug Mode: an Ebreak is executed, ebreak cause exception ? ${ebreakCauseException}\n") 922 923 /** 924 * Exception and Intr 925 */ 926 val ideleg = (mideleg & mip.asUInt) 927 def priviledgedEnableDetect(x: Bool): Bool = Mux(x, ((priviledgeMode === ModeS) && mstatusStruct.ie.s) || (priviledgeMode < ModeS), 928 ((priviledgeMode === ModeM) && mstatusStruct.ie.m) || (priviledgeMode < ModeM)) 929 930 val debugIntr = csrio.externalInterrupt.debug & debugIntrEnable 931 XSDebug(debugIntr, "Debug Mode: debug interrupt is asserted and valid!") 932 // send interrupt information to ROB 933 val intrVecEnable = Wire(Vec(12, Bool())) 934 intrVecEnable.zip(ideleg.asBools).map{case(x,y) => x := priviledgedEnableDetect(y)} 935 val intrVec = Cat(debugIntr, (mie(11,0) & mip.asUInt & intrVecEnable.asUInt)) 936 val intrBitSet = intrVec.orR() 937 csrio.interrupt := intrBitSet 938 mipWire.t.m := csrio.externalInterrupt.mtip 939 mipWire.s.m := csrio.externalInterrupt.msip 940 mipWire.e.m := csrio.externalInterrupt.meip 941 mipWire.e.s := csrio.externalInterrupt.seip 942 943 // interrupts 944 val intrNO = IntPriority.foldRight(0.U)((i: Int, sum: UInt) => Mux(intrVec(i), i.U, sum)) 945 val raiseIntr = csrio.exception.valid && csrio.exception.bits.isInterrupt 946 XSDebug(raiseIntr, "interrupt: pc=0x%x, %d\n", csrio.exception.bits.uop.cf.pc, intrNO) 947 val raiseDebugIntr = intrNO === IRQ_DEBUG.U && raiseIntr 948 949 // exceptions 950 val raiseException = csrio.exception.valid && !csrio.exception.bits.isInterrupt 951 val hasInstrPageFault = csrio.exception.bits.uop.cf.exceptionVec(instrPageFault) && raiseException 952 val hasLoadPageFault = csrio.exception.bits.uop.cf.exceptionVec(loadPageFault) && raiseException 953 val hasStorePageFault = csrio.exception.bits.uop.cf.exceptionVec(storePageFault) && raiseException 954 val hasStoreAddrMisaligned = csrio.exception.bits.uop.cf.exceptionVec(storeAddrMisaligned) && raiseException 955 val hasLoadAddrMisaligned = csrio.exception.bits.uop.cf.exceptionVec(loadAddrMisaligned) && raiseException 956 val hasInstrAccessFault = csrio.exception.bits.uop.cf.exceptionVec(instrAccessFault) && raiseException 957 val hasLoadAccessFault = csrio.exception.bits.uop.cf.exceptionVec(loadAccessFault) && raiseException 958 val hasStoreAccessFault = csrio.exception.bits.uop.cf.exceptionVec(storeAccessFault) && raiseException 959 val hasbreakPoint = csrio.exception.bits.uop.cf.exceptionVec(breakPoint) && raiseException 960 val hasSingleStep = csrio.exception.bits.uop.ctrl.singleStep && raiseException 961 val hasTriggerHit = (csrio.exception.bits.uop.cf.trigger.hit) && raiseException 962 963 XSDebug(hasSingleStep, "Debug Mode: single step exception\n") 964 XSDebug(hasTriggerHit, p"Debug Mode: trigger hit, is frontend? ${Binary(csrio.exception.bits.uop.cf.trigger.frontendHit.asUInt)} " + 965 p"backend hit vec ${Binary(csrio.exception.bits.uop.cf.trigger.backendHit.asUInt)}\n") 966 967 val raiseExceptionVec = csrio.exception.bits.uop.cf.exceptionVec 968 val regularExceptionNO = ExceptionNO.priorities.foldRight(0.U)((i: Int, sum: UInt) => Mux(raiseExceptionVec(i), i.U, sum)) 969 val exceptionNO = Mux(hasSingleStep || hasTriggerHit, 3.U, regularExceptionNO) 970 val causeNO = (raiseIntr << (XLEN-1)).asUInt() | Mux(raiseIntr, intrNO, exceptionNO) 971 972 val raiseExceptionIntr = csrio.exception.valid 973 974 val raiseDebugExceptionIntr = !debugMode && (hasbreakPoint || raiseDebugIntr || hasSingleStep || hasTriggerHit && triggerAction) // TODO 975 val ebreakEnterParkLoop = debugMode && raiseExceptionIntr 976 977 XSDebug(raiseExceptionIntr, "int/exc: pc %x int (%d):%x exc: (%d):%x\n", 978 csrio.exception.bits.uop.cf.pc, intrNO, intrVec, exceptionNO, raiseExceptionVec.asUInt 979 ) 980 XSDebug(raiseExceptionIntr, 981 "pc %x mstatus %x mideleg %x medeleg %x mode %x\n", 982 csrio.exception.bits.uop.cf.pc, 983 mstatus, 984 mideleg, 985 medeleg, 986 priviledgeMode 987 ) 988 989 // mtval write logic 990 // Due to timing reasons of memExceptionVAddr, we delay the write of mtval and stval 991 val memExceptionAddr = SignExt(csrio.memExceptionVAddr, XLEN) 992 when (RegNext(RegNext(hasInstrPageFault || hasLoadPageFault || hasStorePageFault))) { 993 val tval = RegNext(Mux( 994 RegNext(hasInstrPageFault), 995 RegNext(Mux( 996 csrio.exception.bits.uop.cf.crossPageIPFFix, 997 SignExt(csrio.exception.bits.uop.cf.pc + 2.U, XLEN), 998 SignExt(csrio.exception.bits.uop.cf.pc, XLEN) 999 )), 1000 memExceptionAddr 1001 )) 1002 when (RegNext(RegNext(priviledgeMode === ModeM))) { 1003 mtval := tval 1004 }.otherwise { 1005 stval := tval 1006 } 1007 } 1008 1009 when (RegNext(RegNext(hasLoadAddrMisaligned || hasStoreAddrMisaligned))) { 1010 mtval := RegNext(memExceptionAddr) 1011 } 1012 1013 val debugTrapTarget = Mux(!isEbreak && debugMode, 0x38020808.U, 0x38020800.U) // 0x808 is when an exception occurs in debug mode prog buf exec 1014 val deleg = Mux(raiseIntr, mideleg , medeleg) 1015 // val delegS = ((deleg & (1 << (causeNO & 0xf))) != 0) && (priviledgeMode < ModeM); 1016 val delegS = deleg(causeNO(3,0)) && (priviledgeMode < ModeM) 1017 val tvalWen = !(hasInstrPageFault || hasLoadPageFault || hasStorePageFault || hasLoadAddrMisaligned || hasStoreAddrMisaligned) || raiseIntr // TODO: need check 1018 val isXRet = io.in.valid && func === CSROpType.jmp && !isEcall && !isEbreak 1019 1020 // ctrl block will use theses later for flush 1021 val isXRetFlag = RegInit(false.B) 1022 val retTargetReg = Reg(retTarget.cloneType) 1023 when (io.redirectIn.valid) { 1024 isXRetFlag := false.B 1025 }.elsewhen (isXRet) { 1026 isXRetFlag := true.B 1027 retTargetReg := retTarget 1028 } 1029 csrio.isXRet := isXRetFlag 1030 csrio.trapTarget := Mux(isXRetFlag, 1031 retTargetReg, 1032 Mux(raiseDebugExceptionIntr || ebreakEnterParkLoop, debugTrapTarget, 1033 Mux(delegS, stvec, mtvec))(VAddrBits-1, 0) 1034 ) 1035 1036 when (raiseExceptionIntr) { 1037 val mstatusOld = WireInit(mstatus.asTypeOf(new MstatusStruct)) 1038 val mstatusNew = WireInit(mstatus.asTypeOf(new MstatusStruct)) 1039 val dcsrNew = WireInit(dcsr.asTypeOf(new DcsrStruct)) 1040 val debugModeNew = WireInit(debugMode) 1041 1042 when (raiseDebugExceptionIntr) { 1043 when (raiseDebugIntr) { 1044 debugModeNew := true.B 1045 mstatusNew.mprv := false.B 1046 dpc := SignExt(csrio.exception.bits.uop.cf.pc, XLEN) 1047 dcsrNew.cause := 1.U 1048 dcsrNew.prv := priviledgeMode 1049 priviledgeMode := ModeM 1050 XSDebug(raiseDebugIntr, "Debug Mode: Trap to %x at pc %x\n", debugTrapTarget, dpc) 1051 }.elsewhen ((hasbreakPoint || hasSingleStep) && !debugMode) { 1052 // ebreak or ss in running hart 1053 debugModeNew := true.B 1054 dpc := SignExt(csrio.exception.bits.uop.cf.pc, XLEN) 1055 dcsrNew.cause := Mux(hasbreakPoint, 3.U, 0.U) 1056 dcsrNew.prv := priviledgeMode // TODO 1057 priviledgeMode := ModeM 1058 mstatusNew.mprv := false.B 1059 } 1060 dcsr := dcsrNew.asUInt 1061 debugIntrEnable := false.B 1062 }.elsewhen (delegS) { 1063 scause := causeNO 1064 sepc := SignExt(csrio.exception.bits.uop.cf.pc, XLEN) 1065 mstatusNew.spp := priviledgeMode 1066 mstatusNew.pie.s := mstatusOld.ie.s 1067 mstatusNew.ie.s := false.B 1068 priviledgeMode := ModeS 1069 when (tvalWen) { stval := 0.U } 1070 }.otherwise { 1071 mcause := causeNO 1072 mepc := SignExt(csrio.exception.bits.uop.cf.pc, XLEN) 1073 mstatusNew.mpp := priviledgeMode 1074 mstatusNew.pie.m := mstatusOld.ie.m 1075 mstatusNew.ie.m := false.B 1076 priviledgeMode := ModeM 1077 when (tvalWen) { mtval := 0.U } 1078 } 1079 mstatus := mstatusNew.asUInt 1080 debugMode := debugModeNew 1081 } 1082 1083 XSDebug(raiseExceptionIntr && delegS, "sepc is writen!!! pc:%x\n", cfIn.pc) 1084 1085 // Distributed CSR update req 1086 // 1087 // For now we use it to implement customized cache op 1088 // It can be delayed if necessary 1089 1090 val delayedUpdate0 = DelayN(csrio.distributedUpdate(0), 2) 1091 val delayedUpdate1 = DelayN(csrio.distributedUpdate(1), 2) 1092 val distributedUpdateValid = delayedUpdate0.w.valid || delayedUpdate1.w.valid 1093 val distributedUpdateAddr = Mux(delayedUpdate0.w.valid, 1094 delayedUpdate0.w.bits.addr, 1095 delayedUpdate1.w.bits.addr 1096 ) 1097 val distributedUpdateData = Mux(delayedUpdate0.w.valid, 1098 delayedUpdate0.w.bits.data, 1099 delayedUpdate1.w.bits.data 1100 ) 1101 1102 assert(!(delayedUpdate0.w.valid && delayedUpdate1.w.valid)) 1103 1104 when(distributedUpdateValid){ 1105 // cacheopRegs can be distributed updated 1106 CacheInstrucion.CacheInsRegisterList.map{case (name, attribute) => { 1107 when((Scachebase + attribute("offset").toInt).U === distributedUpdateAddr){ 1108 cacheopRegs(name) := distributedUpdateData 1109 } 1110 }} 1111 } 1112 1113 // Implicit add reset values for mepc[0] and sepc[0] 1114 // TODO: rewrite mepc and sepc using a struct-like style with the LSB always being 0 1115 when (reset.asBool) { 1116 mepc := Cat(mepc(XLEN - 1, 1), 0.U(1.W)) 1117 sepc := Cat(sepc(XLEN - 1, 1), 0.U(1.W)) 1118 } 1119 1120 def readWithScala(addr: Int): UInt = mapping(addr)._1 1121 1122 val difftestIntrNO = Mux(raiseIntr, causeNO, 0.U) 1123 1124 // Always instantiate basic difftest modules. 1125 if (env.AlwaysBasicDiff || env.EnableDifftest) { 1126 val difftest = Module(new DifftestArchEvent) 1127 difftest.io.clock := clock 1128 difftest.io.coreid := csrio.hartId 1129 difftest.io.intrNO := RegNext(RegNext(RegNext(difftestIntrNO))) 1130 difftest.io.cause := RegNext(RegNext(RegNext(Mux(csrio.exception.valid, causeNO, 0.U)))) 1131 difftest.io.exceptionPC := RegNext(RegNext(RegNext(SignExt(csrio.exception.bits.uop.cf.pc, XLEN)))) 1132 } 1133 1134 // Always instantiate basic difftest modules. 1135 if (env.AlwaysBasicDiff || env.EnableDifftest) { 1136 val difftest = Module(new DifftestCSRState) 1137 difftest.io.clock := clock 1138 difftest.io.coreid := csrio.hartId 1139 difftest.io.priviledgeMode := priviledgeMode 1140 difftest.io.mstatus := mstatus 1141 difftest.io.sstatus := mstatus & sstatusRmask 1142 difftest.io.mepc := mepc 1143 difftest.io.sepc := sepc 1144 difftest.io.mtval:= mtval 1145 difftest.io.stval:= stval 1146 difftest.io.mtvec := mtvec 1147 difftest.io.stvec := stvec 1148 difftest.io.mcause := mcause 1149 difftest.io.scause := scause 1150 difftest.io.satp := satp 1151 difftest.io.mip := mipReg 1152 difftest.io.mie := mie 1153 difftest.io.mscratch := mscratch 1154 difftest.io.sscratch := sscratch 1155 difftest.io.mideleg := mideleg 1156 difftest.io.medeleg := medeleg 1157 } 1158} 1159 1160class PFEvent(implicit p: Parameters) extends XSModule with HasCSRConst { 1161 val io = IO(new Bundle { 1162 val distribute_csr = Flipped(new DistributedCSRIO()) 1163 val hpmevent = Output(Vec(29, UInt(XLEN.W))) 1164 }) 1165 1166 val w = io.distribute_csr.w 1167 1168 val perfEvents = List.fill(8)(RegInit("h0000000000".U(XLEN.W))) ++ 1169 List.fill(8)(RegInit("h4010040100".U(XLEN.W))) ++ 1170 List.fill(8)(RegInit("h8020080200".U(XLEN.W))) ++ 1171 List.fill(5)(RegInit("hc0300c0300".U(XLEN.W))) 1172 1173 val perfEventMapping = (0 until 29).map(i => {Map( 1174 MaskedRegMap(addr = Mhpmevent3 +i, 1175 reg = perfEvents(i), 1176 wmask = "hf87fff3fcff3fcff".U(XLEN.W)) 1177 )}).fold(Map())((a,b) => a ++ b) 1178 1179 val rdata = Wire(UInt(XLEN.W)) 1180 MaskedRegMap.generate(perfEventMapping, w.bits.addr, rdata, w.valid, w.bits.data) 1181 for(i <- 0 until 29){ 1182 io.hpmevent(i) := perfEvents(i) 1183 } 1184} 1185 1186