2012-02-26 02:09:26 +01:00
|
|
|
package rocket
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2012-10-08 05:15:54 +02:00
|
|
|
import Chisel._
|
2013-07-24 05:26:17 +02:00
|
|
|
import Util._
|
2012-10-08 05:15:54 +02:00
|
|
|
import Node._
|
2014-08-08 21:23:02 +02:00
|
|
|
import uncore._
|
2012-10-08 05:15:54 +02:00
|
|
|
import scala.math._
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2012-11-27 10:28:06 +01:00
|
|
|
class Status extends Bundle {
|
2013-03-26 07:26:47 +01:00
|
|
|
val ip = Bits(width = 8)
|
2012-11-27 10:28:06 +01:00
|
|
|
val im = Bits(width = 8)
|
|
|
|
val zero = Bits(width = 7)
|
2013-09-13 07:34:38 +02:00
|
|
|
val er = Bool()
|
2012-11-27 10:28:06 +01:00
|
|
|
val vm = Bool()
|
|
|
|
val s64 = Bool()
|
|
|
|
val u64 = Bool()
|
|
|
|
val ef = Bool()
|
2013-08-24 06:16:28 +02:00
|
|
|
val pei = Bool()
|
|
|
|
val ei = Bool()
|
|
|
|
val ps = Bool()
|
|
|
|
val s = Bool()
|
2012-11-27 10:28:06 +01:00
|
|
|
}
|
|
|
|
|
2013-11-25 13:35:15 +01:00
|
|
|
object CSR
|
2011-10-26 08:02:47 +02:00
|
|
|
{
|
2012-11-27 10:28:06 +01:00
|
|
|
// commands
|
2013-11-25 13:35:15 +01:00
|
|
|
val SZ = 2
|
|
|
|
val X = Bits("b??", 2)
|
|
|
|
val N = Bits(0,2)
|
|
|
|
val W = Bits(1,2)
|
|
|
|
val S = Bits(2,2)
|
|
|
|
val C = Bits(3,2)
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
|
2014-08-08 21:23:02 +02:00
|
|
|
class CSRFileIO extends Bundle {
|
|
|
|
val host = new HTIFIO
|
2014-05-10 04:26:43 +02:00
|
|
|
val rw = new Bundle {
|
|
|
|
val addr = UInt(INPUT, 12)
|
|
|
|
val cmd = Bits(INPUT, CSR.SZ)
|
2014-08-08 21:23:02 +02:00
|
|
|
val rdata = Bits(OUTPUT, params(XprLen))
|
|
|
|
val wdata = Bits(INPUT, params(XprLen))
|
2014-05-10 04:26:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val status = new Status().asOutput
|
2014-08-08 21:23:02 +02:00
|
|
|
val ptbr = UInt(OUTPUT, params(PAddrBits))
|
|
|
|
val evec = UInt(OUTPUT, params(VAddrBits)+1)
|
2014-05-10 04:26:43 +02:00
|
|
|
val exception = Bool(INPUT)
|
2014-08-08 21:23:02 +02:00
|
|
|
val retire = UInt(INPUT, log2Up(1+params(RetireWidth)))
|
|
|
|
val uarch_counters = Vec.fill(16)(UInt(INPUT, log2Up(1+params(RetireWidth))))
|
|
|
|
val cause = UInt(INPUT, params(XprLen))
|
2014-05-10 04:26:43 +02:00
|
|
|
val badvaddr_wen = Bool(INPUT)
|
2014-08-08 21:23:02 +02:00
|
|
|
val pc = UInt(INPUT, params(VAddrBits)+1)
|
2014-05-10 04:26:43 +02:00
|
|
|
val sret = Bool(INPUT)
|
|
|
|
val fatc = Bool(OUTPUT)
|
|
|
|
val replay = Bool(OUTPUT)
|
2014-08-08 21:23:02 +02:00
|
|
|
val time = UInt(OUTPUT, params(XprLen))
|
2014-05-10 04:26:43 +02:00
|
|
|
val fcsr_rm = Bits(OUTPUT, FPConstants.RM_SZ)
|
|
|
|
val fcsr_flags = Valid(Bits(width = FPConstants.FLAGS_SZ)).flip
|
|
|
|
val rocc = new RoCCInterface().flip
|
|
|
|
}
|
|
|
|
|
2014-08-08 21:23:02 +02:00
|
|
|
class CSRFile extends Module
|
2011-10-26 08:02:47 +02:00
|
|
|
{
|
2014-05-10 04:26:43 +02:00
|
|
|
val io = new CSRFileIO
|
2012-11-27 10:28:06 +01:00
|
|
|
|
2014-08-08 21:23:02 +02:00
|
|
|
val reg_epc = Reg(Bits(width = params(VAddrBits)+1))
|
|
|
|
val reg_badvaddr = Reg(Bits(width = params(VAddrBits)))
|
|
|
|
val reg_evec = Reg(Bits(width = params(VAddrBits)))
|
2013-08-12 19:39:11 +02:00
|
|
|
val reg_compare = Reg(Bits(width = 32))
|
2014-08-08 21:23:02 +02:00
|
|
|
val reg_cause = Reg(Bits(width = params(XprLen)))
|
|
|
|
val reg_tohost = Reg(init=Bits(0, params(XprLen)))
|
|
|
|
val reg_fromhost = Reg(init=Bits(0, params(XprLen)))
|
|
|
|
val reg_sup0 = Reg(Bits(width = params(XprLen)))
|
|
|
|
val reg_sup1 = Reg(Bits(width = params(XprLen)))
|
|
|
|
val reg_ptbr = Reg(UInt(width = params(PAddrBits)))
|
2013-08-16 00:28:15 +02:00
|
|
|
val reg_stats = Reg(init=Bool(false))
|
2013-08-12 19:39:11 +02:00
|
|
|
val reg_status = Reg(new Status) // reset down below
|
2014-08-08 21:23:02 +02:00
|
|
|
val reg_time = WideCounter(params(XprLen))
|
|
|
|
val reg_instret = WideCounter(params(XprLen), io.retire)
|
|
|
|
val reg_uarch_counters = io.uarch_counters.map(WideCounter(params(XprLen), _))
|
2013-11-25 13:35:15 +01:00
|
|
|
val reg_fflags = Reg(UInt(width = 5))
|
|
|
|
val reg_frm = Reg(UInt(width = 3))
|
2012-11-27 10:28:06 +01:00
|
|
|
|
2013-08-16 00:28:15 +02:00
|
|
|
val r_irq_timer = Reg(init=Bool(false))
|
|
|
|
val r_irq_ipi = Reg(init=Bool(true))
|
2014-08-08 21:23:02 +02:00
|
|
|
val irq_rocc = Bool(!params(BuildRoCC).isEmpty) && io.rocc.interrupt
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2013-11-25 13:35:15 +01:00
|
|
|
val cpu_req_valid = io.rw.cmd != CSR.N
|
2013-08-12 19:39:11 +02:00
|
|
|
val host_pcr_req_valid = Reg(Bool()) // don't reset
|
2013-11-25 13:35:15 +01:00
|
|
|
val host_pcr_req_fire = host_pcr_req_valid && !cpu_req_valid
|
2013-08-12 19:39:11 +02:00
|
|
|
val host_pcr_rep_valid = Reg(Bool()) // don't reset
|
|
|
|
val host_pcr_bits = Reg(io.host.pcr_req.bits)
|
2012-12-06 23:22:07 +01:00
|
|
|
io.host.pcr_req.ready := !host_pcr_req_valid && !host_pcr_rep_valid
|
|
|
|
io.host.pcr_rep.valid := host_pcr_rep_valid
|
|
|
|
io.host.pcr_rep.bits := host_pcr_bits.data
|
|
|
|
when (io.host.pcr_req.fire()) {
|
|
|
|
host_pcr_req_valid := true
|
|
|
|
host_pcr_bits := io.host.pcr_req.bits
|
|
|
|
}
|
|
|
|
when (host_pcr_req_fire) {
|
|
|
|
host_pcr_req_valid := false
|
|
|
|
host_pcr_rep_valid := true
|
2013-04-02 23:43:01 +02:00
|
|
|
host_pcr_bits.data := io.rw.rdata
|
2012-12-06 23:22:07 +01:00
|
|
|
}
|
|
|
|
when (io.host.pcr_rep.fire()) { host_pcr_rep_valid := false }
|
2013-09-25 10:16:32 +02:00
|
|
|
|
|
|
|
io.host.debug_stats_pcr := reg_stats // direct export up the hierarchy
|
2012-02-20 08:15:45 +01:00
|
|
|
|
2013-11-25 13:35:15 +01:00
|
|
|
val addr = Mux(cpu_req_valid, io.rw.addr, host_pcr_bits.addr | 0x500)
|
|
|
|
val decoded_addr = {
|
2014-05-26 08:58:53 +02:00
|
|
|
val map = for ((v, i) <- CSRs.all.zipWithIndex)
|
|
|
|
yield v -> UInt(BigInt(1) << i)
|
|
|
|
val out = ROM(map)(addr)
|
2013-11-25 13:35:15 +01:00
|
|
|
val a = Array.fill(CSRs.all.max+1)(null.asInstanceOf[Bool])
|
|
|
|
for (i <- 0 until CSRs.all.size)
|
2014-05-26 08:58:53 +02:00
|
|
|
a(CSRs.all(i)) = out(i)
|
2013-11-25 13:35:15 +01:00
|
|
|
a
|
|
|
|
}
|
|
|
|
|
|
|
|
val wen = cpu_req_valid || host_pcr_req_fire && host_pcr_bits.rw
|
|
|
|
val wdata = Mux(cpu_req_valid, io.rw.wdata, host_pcr_bits.data)
|
2012-02-20 08:15:45 +01:00
|
|
|
|
2012-11-27 10:28:06 +01:00
|
|
|
io.status := reg_status
|
2014-02-06 12:06:55 +01:00
|
|
|
io.status.ip := Cat(r_irq_timer, reg_fromhost.orR, r_irq_ipi, Bool(false),
|
|
|
|
Bool(false), irq_rocc, Bool(false), Bool(false))
|
2013-11-25 13:35:15 +01:00
|
|
|
io.fatc := wen && decoded_addr(CSRs.fatc)
|
2013-08-24 06:16:28 +02:00
|
|
|
io.evec := Mux(io.exception, reg_evec.toSInt, reg_epc).toUInt
|
2012-11-27 10:28:06 +01:00
|
|
|
io.ptbr := reg_ptbr
|
2013-07-31 01:36:28 +02:00
|
|
|
|
2013-09-13 07:34:38 +02:00
|
|
|
when (io.badvaddr_wen) {
|
|
|
|
val wdata = io.rw.wdata
|
2014-08-08 21:23:02 +02:00
|
|
|
val (upper, lower) = Split(wdata, params(VAddrBits))
|
2013-08-12 19:39:11 +02:00
|
|
|
val sign = Mux(lower.toSInt < SInt(0), upper.andR, upper.orR)
|
|
|
|
reg_badvaddr := Cat(sign, lower).toSInt
|
2012-03-14 22:15:28 +01:00
|
|
|
}
|
2012-02-12 02:20:33 +01:00
|
|
|
|
|
|
|
when (io.exception) {
|
2013-01-06 14:18:33 +01:00
|
|
|
reg_status.s := true
|
|
|
|
reg_status.ps := reg_status.s
|
2013-08-24 06:16:28 +02:00
|
|
|
reg_status.ei := false
|
|
|
|
reg_status.pei := reg_status.ei
|
2013-08-12 19:39:11 +02:00
|
|
|
reg_epc := io.pc.toSInt
|
2013-01-06 14:18:33 +01:00
|
|
|
reg_cause := io.cause
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
2011-11-14 22:48:49 +01:00
|
|
|
|
2013-11-25 13:35:15 +01:00
|
|
|
when (io.sret) {
|
2012-11-27 10:28:06 +01:00
|
|
|
reg_status.s := reg_status.ps
|
2013-08-24 06:16:28 +02:00
|
|
|
reg_status.ei := reg_status.pei
|
2012-01-27 04:33:55 +01:00
|
|
|
}
|
2012-02-12 02:20:33 +01:00
|
|
|
|
2013-11-25 13:35:15 +01:00
|
|
|
when (reg_time(reg_compare.getWidth-1,0) === reg_compare) {
|
|
|
|
r_irq_timer := true
|
2011-11-13 09:27:57 +01:00
|
|
|
}
|
2012-02-12 02:20:33 +01:00
|
|
|
|
2013-11-25 13:35:15 +01:00
|
|
|
io.time := reg_time
|
|
|
|
io.host.ipi_req.valid := cpu_req_valid && decoded_addr(CSRs.send_ipi)
|
2013-04-02 23:43:01 +02:00
|
|
|
io.host.ipi_req.bits := io.rw.wdata
|
2012-08-04 04:00:34 +02:00
|
|
|
io.replay := io.host.ipi_req.valid && !io.host.ipi_req.ready
|
|
|
|
|
2013-11-25 13:35:15 +01:00
|
|
|
when (host_pcr_req_fire && !host_pcr_bits.rw && decoded_addr(CSRs.tohost)) { reg_tohost := UInt(0) }
|
2012-11-27 10:28:06 +01:00
|
|
|
|
|
|
|
val read_impl = Bits(2)
|
2014-08-12 03:36:23 +02:00
|
|
|
val read_ptbr = reg_ptbr(params(PAddrBits)-1, params(PgIdxBits)) << UInt(params(PgIdxBits))
|
2013-11-25 13:35:15 +01:00
|
|
|
|
2014-03-16 01:31:48 +01:00
|
|
|
val read_mapping = collection.mutable.LinkedHashMap[Int,Bits](
|
2014-08-08 21:23:02 +02:00
|
|
|
CSRs.fflags -> (if (!params(BuildFPU).isEmpty) reg_fflags else UInt(0)),
|
|
|
|
CSRs.frm -> (if (!params(BuildFPU).isEmpty) reg_frm else UInt(0)),
|
|
|
|
CSRs.fcsr -> (if (!params(BuildFPU).isEmpty) Cat(reg_frm, reg_fflags) else UInt(0)),
|
2013-11-25 13:35:15 +01:00
|
|
|
CSRs.cycle -> reg_time,
|
|
|
|
CSRs.time -> reg_time,
|
|
|
|
CSRs.instret -> reg_instret,
|
|
|
|
CSRs.sup0 -> reg_sup0,
|
|
|
|
CSRs.sup1 -> reg_sup1,
|
|
|
|
CSRs.epc -> reg_epc,
|
|
|
|
CSRs.badvaddr -> reg_badvaddr,
|
|
|
|
CSRs.ptbr -> read_ptbr,
|
|
|
|
CSRs.asid -> UInt(0),
|
|
|
|
CSRs.count -> reg_time,
|
|
|
|
CSRs.compare -> reg_compare,
|
|
|
|
CSRs.evec -> reg_evec,
|
2014-01-25 00:56:01 +01:00
|
|
|
CSRs.cause -> reg_cause,
|
2013-11-25 13:35:15 +01:00
|
|
|
CSRs.status -> io.status.toBits,
|
|
|
|
CSRs.hartid -> io.host.id,
|
|
|
|
CSRs.impl -> read_impl,
|
|
|
|
CSRs.fatc -> read_impl, // don't care
|
|
|
|
CSRs.send_ipi -> read_impl, // don't care
|
|
|
|
CSRs.clear_ipi -> read_impl, // don't care
|
|
|
|
CSRs.stats -> reg_stats,
|
|
|
|
CSRs.tohost -> reg_tohost,
|
|
|
|
CSRs.fromhost -> reg_fromhost)
|
|
|
|
|
2014-02-06 09:13:02 +01:00
|
|
|
for (i <- 0 until reg_uarch_counters.size)
|
|
|
|
read_mapping += (CSRs.uarch0 + i) -> reg_uarch_counters(i)
|
|
|
|
|
2014-03-16 01:31:48 +01:00
|
|
|
io.rw.rdata := Mux1H(for ((k, v) <- read_mapping) yield decoded_addr(k) -> v)
|
2013-11-25 13:35:15 +01:00
|
|
|
|
|
|
|
io.fcsr_rm := reg_frm
|
|
|
|
when (io.fcsr_flags.valid) {
|
|
|
|
reg_fflags := reg_fflags | io.fcsr_flags.bits
|
|
|
|
}
|
2011-11-13 09:27:57 +01:00
|
|
|
|
2012-02-20 08:15:45 +01:00
|
|
|
when (wen) {
|
2013-11-25 13:35:15 +01:00
|
|
|
when (decoded_addr(CSRs.status)) {
|
2014-01-22 01:17:39 +01:00
|
|
|
reg_status := new Status().fromBits(wdata)
|
2013-08-24 06:16:28 +02:00
|
|
|
reg_status.s64 := true
|
|
|
|
reg_status.u64 := true
|
2012-11-27 10:28:06 +01:00
|
|
|
reg_status.zero := 0
|
2014-08-08 21:23:02 +02:00
|
|
|
if (!params(UseVM)) reg_status.vm := false
|
|
|
|
if (params(BuildRoCC).isEmpty) reg_status.er := false
|
|
|
|
if (params(BuildFPU).isEmpty) reg_status.ef := false
|
2012-02-12 02:20:33 +01:00
|
|
|
}
|
2014-01-22 01:17:39 +01:00
|
|
|
when (decoded_addr(CSRs.fflags)) { reg_fflags := wdata }
|
|
|
|
when (decoded_addr(CSRs.frm)) { reg_frm := wdata }
|
|
|
|
when (decoded_addr(CSRs.fcsr)) { reg_fflags := wdata; reg_frm := wdata >> reg_fflags.getWidth }
|
2014-08-08 21:23:02 +02:00
|
|
|
when (decoded_addr(CSRs.epc)) { reg_epc := wdata(params(VAddrBits),0).toSInt }
|
|
|
|
when (decoded_addr(CSRs.evec)) { reg_evec := wdata(params(VAddrBits)-1,0).toSInt }
|
2014-01-22 01:17:39 +01:00
|
|
|
when (decoded_addr(CSRs.count)) { reg_time := wdata.toUInt }
|
|
|
|
when (decoded_addr(CSRs.compare)) { reg_compare := wdata(31,0).toUInt; r_irq_timer := Bool(false) }
|
|
|
|
when (decoded_addr(CSRs.fromhost)) { when (reg_fromhost === UInt(0) || !host_pcr_req_fire) { reg_fromhost := wdata } }
|
|
|
|
when (decoded_addr(CSRs.tohost)) { when (reg_tohost === UInt(0) || host_pcr_req_fire) { reg_tohost := wdata } }
|
|
|
|
when (decoded_addr(CSRs.clear_ipi)){ r_irq_ipi := wdata(0) }
|
|
|
|
when (decoded_addr(CSRs.sup0)) { reg_sup0 := wdata }
|
|
|
|
when (decoded_addr(CSRs.sup1)) { reg_sup1 := wdata }
|
2014-08-08 21:23:02 +02:00
|
|
|
when (decoded_addr(CSRs.ptbr)) { reg_ptbr := Cat(wdata(params(PAddrBits)-1, params(PgIdxBits)), Bits(0, params(PgIdxBits))).toUInt }
|
2014-01-22 01:17:39 +01:00
|
|
|
when (decoded_addr(CSRs.stats)) { reg_stats := wdata(0) }
|
2012-02-12 02:20:33 +01:00
|
|
|
}
|
|
|
|
|
2012-08-04 04:00:34 +02:00
|
|
|
io.host.ipi_rep.ready := Bool(true)
|
|
|
|
when (io.host.ipi_rep.valid) { r_irq_ipi := Bool(true) }
|
|
|
|
|
2013-08-13 05:51:54 +02:00
|
|
|
when(this.reset) {
|
2013-08-24 06:16:28 +02:00
|
|
|
reg_status.ei := false
|
|
|
|
reg_status.pei := false
|
2012-11-27 10:28:06 +01:00
|
|
|
reg_status.ef := false
|
2013-09-13 07:34:38 +02:00
|
|
|
reg_status.er := false
|
2012-11-27 10:28:06 +01:00
|
|
|
reg_status.ps := false
|
|
|
|
reg_status.s := true
|
|
|
|
reg_status.u64 := true
|
|
|
|
reg_status.s64 := true
|
|
|
|
reg_status.vm := false
|
|
|
|
reg_status.zero := 0
|
|
|
|
reg_status.im := 0
|
2013-03-26 07:26:47 +01:00
|
|
|
reg_status.ip := 0
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
}
|