1
0
rocket-chip/rocket/src/main/scala/dpath_util.scala

294 lines
9.1 KiB
Scala
Raw Normal View History

package rocket
2012-10-08 05:15:54 +02:00
import Chisel._
import Util._
2012-10-08 05:15:54 +02:00
import Node._
2013-11-08 00:42:03 +01:00
import uncore.HTIFIO
import uncore.constants.AddressConstants._
2012-10-08 05:15:54 +02:00
import scala.math._
2013-01-07 22:38:59 +01:00
class DpathBTBIO extends Bundle
{
2013-08-12 19:39:11 +02:00
val current_pc = UInt(INPUT, VADDR_BITS);
val hit = Bool(OUTPUT);
2013-08-12 19:39:11 +02:00
val target = UInt(OUTPUT, VADDR_BITS);
val wen = Bool(INPUT);
val clr = Bool(INPUT);
val invalidate = Bool(INPUT);
2013-08-12 19:39:11 +02:00
val correct_pc = UInt(INPUT, VADDR_BITS);
val correct_target = UInt(INPUT, VADDR_BITS);
}
// fully-associative branch target buffer
2013-08-12 19:39:11 +02:00
class rocketDpathBTB(entries: Int) extends Module
{
2013-01-07 22:38:59 +01:00
val io = new DpathBTBIO
var hit_reduction = Bool(false)
2012-05-24 19:33:15 +02:00
val hit = Bool()
val update = Bool()
var update_reduction = Bool(false)
val valid = Vec.fill(entries){Reg(init=Bool(false))}
2013-08-12 19:39:11 +02:00
val hits = Vec.fill(entries){Bool()}
val updates = Vec.fill(entries){Bool()}
val targets = Vec.fill(entries){Reg(UInt())}
val anyUpdate = updates.toBits.orR
val random_way = Random(entries, io.wen)
val invalid_way = valid.indexWhere((x: Bool) => !x)
val repl_way = Mux(valid.contains(Bool(false)), invalid_way, random_way)
for (i <- 0 until entries) {
2013-08-12 19:39:11 +02:00
val tag = Reg(UInt())
hits(i) := valid(i) && tag === io.current_pc
updates(i) := valid(i) && tag === io.correct_pc
2013-08-12 19:39:11 +02:00
when (io.wen && (updates(i) || !anyUpdate && UInt(i) === repl_way)) {
valid(i) := Bool(false)
2012-10-10 06:35:03 +02:00
when (!io.clr) {
valid(i) := Bool(true)
2012-10-10 06:35:03 +02:00
tag := io.correct_pc
targets(i) := io.correct_target
2012-10-10 06:35:03 +02:00
}
}
}
io.hit := hits.toBits.orR
io.target := Mux1H(hits, targets)
}
2012-11-27 10:28:06 +01:00
class Status extends Bundle {
val ip = Bits(width = 8)
2012-11-27 10:28:06 +01:00
val im = Bits(width = 8)
val zero = Bits(width = 7)
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
}
object PCR
{
2012-11-27 10:28:06 +01:00
// commands
val SZ = 3
val X = Bits("b???", 3)
val N = Bits(0,3)
val F = Bits(1,3) // mfpcr
val T = Bits(4,3) // mtpcr
val C = Bits(6,3) // clearpcr
val S = Bits(7,3) // setpcr
// regs
2013-08-24 06:16:28 +02:00
val SUP0 = 0
val SUP1 = 1
val EPC = 2
val BADVADDR = 3
val PTBR = 4
val ASID = 5
val COUNT = 6
val COMPARE = 7
val EVEC = 8
val CAUSE = 9
val STATUS = 10
val HARTID = 11
val IMPL = 12
val FATC = 13
val SEND_IPI = 14
val CLR_IPI = 15
val STATS = 28
2012-11-27 10:28:06 +01:00
val RESET = 29
val TOHOST = 30
val FROMHOST = 31
}
2013-08-12 19:39:11 +02:00
class PCR(implicit conf: RocketConfiguration) extends Module
{
2012-11-27 10:28:06 +01:00
val io = new Bundle {
val host = new HTIFIO(conf.tl.ln.nClients)
val rw = new Bundle {
2013-08-12 19:39:11 +02:00
val addr = UInt(INPUT, log2Up(conf.nxpr))
val cmd = Bits(INPUT, PCR.SZ)
2013-04-10 22:47:30 +02:00
val rdata = Bits(OUTPUT, conf.xprlen)
val wdata = Bits(INPUT, conf.xprlen)
}
2013-08-25 07:42:51 +02:00
// there is a fixed constant related to this in PCRReq.addr
require(log2Up(conf.nxpr) == 5)
2012-11-27 10:28:06 +01:00
val status = new Status().asOutput
2013-08-12 19:39:11 +02:00
val ptbr = UInt(OUTPUT, PADDR_BITS)
2013-08-24 06:16:28 +02:00
val evec = UInt(OUTPUT, VADDR_BITS+1)
2012-11-27 10:28:06 +01:00
val exception = Bool(INPUT)
2013-08-12 19:39:11 +02:00
val cause = UInt(INPUT, 6)
2012-11-27 10:28:06 +01:00
val badvaddr_wen = Bool(INPUT)
2013-08-12 19:39:11 +02:00
val pc = UInt(INPUT, VADDR_BITS+1)
2012-11-27 10:28:06 +01:00
val eret = Bool(INPUT)
val ei = Bool(INPUT)
val di = Bool(INPUT)
2013-08-24 06:16:28 +02:00
val fatc = Bool(OUTPUT)
2012-11-27 10:28:06 +01:00
val irq_timer = Bool(OUTPUT)
val irq_ipi = Bool(OUTPUT)
val replay = Bool(OUTPUT)
}
import PCR._
2013-08-24 06:16:28 +02:00
val reg_epc = Reg(Bits(width = VADDR_BITS+1))
val reg_badvaddr = Reg(Bits(width = VADDR_BITS))
val reg_evec = Reg(Bits(width = VADDR_BITS))
val reg_count = WideCounter(32)
2013-08-12 19:39:11 +02:00
val reg_compare = Reg(Bits(width = 32))
val reg_cause = Reg(Bits(width = io.cause.getWidth))
2013-08-16 00:28:15 +02:00
val reg_tohost = Reg(init=Bits(0, conf.xprlen))
val reg_fromhost = Reg(init=Bits(0, conf.xprlen))
2013-08-24 06:16:28 +02:00
val reg_sup0 = Reg(Bits(width = conf.xprlen))
val reg_sup1 = Reg(Bits(width = conf.xprlen))
2013-08-12 19:39:11 +02:00
val reg_ptbr = Reg(UInt(width = PADDR_BITS))
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
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))
2013-08-12 19:39:11 +02:00
val host_pcr_req_valid = Reg(Bool()) // don't reset
val host_pcr_req_fire = host_pcr_req_valid && io.rw.cmd === PCR.N
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
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 }
io.host.debug_stats_pcr := reg_stats // direct export up the hierarchy
val addr = Mux(io.rw.cmd != PCR.N, io.rw.addr, host_pcr_bits.addr)
val wen = io.rw.cmd === PCR.T || io.rw.cmd === PCR.S || io.rw.cmd === PCR.C ||
host_pcr_req_fire && host_pcr_bits.rw
val wdata = Mux(io.rw.cmd != PCR.N, io.rw.wdata, host_pcr_bits.data)
2012-11-27 10:28:06 +01:00
io.status := reg_status
io.status.ip := Cat(r_irq_timer, reg_fromhost.orR, r_irq_ipi, Bool(false),
Bool(false), Bool(false), Bool(false), Bool(false))
2013-08-24 06:16:28 +02:00
io.fatc := wen && addr === FATC
io.evec := Mux(io.exception, reg_evec.toSInt, reg_epc).toUInt
2012-11-27 10:28:06 +01:00
io.ptbr := reg_ptbr
when (io.badvaddr_wen) {
val wdata = io.rw.wdata
val (upper, lower) = Split(wdata, VADDR_BITS)
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) {
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
reg_cause := io.cause
}
2011-11-14 22:48:49 +01:00
2012-02-12 02:20:33 +01:00
when (io.eret) {
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-02-12 02:20:33 +01:00
2011-11-13 09:27:57 +01:00
when (reg_count === reg_compare) {
2012-02-12 02:20:33 +01:00
r_irq_timer := Bool(true);
2011-11-13 09:27:57 +01:00
}
2012-02-12 02:20:33 +01:00
2011-11-14 12:24:02 +01:00
io.irq_timer := r_irq_timer;
io.irq_ipi := r_irq_ipi;
io.host.ipi_req.valid := io.rw.cmd === PCR.T && io.rw.addr === SEND_IPI
io.host.ipi_req.bits := io.rw.wdata
io.replay := io.host.ipi_req.valid && !io.host.ipi_req.ready
2013-08-12 19:39:11 +02:00
when (host_pcr_req_fire && !host_pcr_bits.rw && host_pcr_bits.addr === TOHOST) { reg_tohost := UInt(0) }
2012-11-27 10:28:06 +01:00
val read_impl = Bits(2)
val read_ptbr = reg_ptbr(PADDR_BITS-1,PGIDX_BITS) << PGIDX_BITS
val read_cause = reg_cause(reg_cause.getWidth-1) << conf.xprlen-1 | reg_cause(reg_cause.getWidth-2,0)
io.rw.rdata := AVec[Bits](
reg_sup0, reg_sup1, reg_epc, reg_badvaddr,
reg_ptbr, Bits(0)/*asid*/, reg_count, reg_compare,
reg_evec, reg_cause, io.status.toBits, io.host.id,
read_impl, read_impl/*x*/, read_impl/*x*/, read_impl/*x*/,
reg_stats/*x*/, reg_fromhost/*x*/, reg_tohost/*x*/, reg_fromhost/*x*/,
reg_stats/*x*/, reg_fromhost/*x*/, reg_tohost/*x*/, reg_fromhost/*x*/,
reg_stats/*x*/, reg_fromhost/*x*/, reg_tohost/*x*/, reg_fromhost/*x*/,
reg_stats, reg_fromhost/*x*/, reg_tohost, reg_fromhost
)(addr)
2011-11-13 09:27:57 +01:00
when (wen) {
when (addr === STATUS) {
val sr_wdata = Mux(io.rw.cmd === PCR.S, reg_status.toBits | wdata,
Mux(io.rw.cmd === PCR.C, reg_status.toBits & ~wdata,
wdata))
reg_status := new Status().fromBits(sr_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
2013-09-24 22:58:23 +02:00
if (!conf.vm) reg_status.vm := false
if (conf.rocc.isEmpty) reg_status.er := false
2012-11-27 10:28:06 +01:00
if (!conf.fpu) reg_status.ef := false
2012-02-12 02:20:33 +01:00
}
2013-08-12 19:39:11 +02:00
when (addr === EPC) { reg_epc := wdata(VADDR_BITS,0).toSInt }
2013-08-24 06:16:28 +02:00
when (addr === EVEC) { reg_evec := wdata(VADDR_BITS-1,0).toSInt }
2013-08-12 19:39:11 +02:00
when (addr === COUNT) { reg_count := wdata.toUInt }
when (addr === COMPARE) { reg_compare := wdata(31,0).toUInt; r_irq_timer := Bool(false); }
when (addr === FROMHOST) { when (reg_fromhost === UInt(0) || !host_pcr_req_fire) { reg_fromhost := wdata } }
when (addr === TOHOST) { when (reg_tohost === UInt(0)) { reg_tohost := wdata } }
when (addr === CLR_IPI) { r_irq_ipi := wdata(0) }
2013-08-24 06:16:28 +02:00
when (addr === SUP0) { reg_sup0 := wdata; }
when (addr === SUP1) { reg_sup1 := wdata; }
2013-08-12 19:39:11 +02:00
when (addr === PTBR) { reg_ptbr := Cat(wdata(PADDR_BITS-1, PGIDX_BITS), Bits(0, PGIDX_BITS)).toUInt; }
when (addr === STATS) { reg_stats := wdata(0) }
2012-02-12 02:20:33 +01: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
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
reg_status.ip := 0
}
}
class ioReadPort(d: Int, w: Int) extends Bundle
{
override def clone = new ioReadPort(d, w).asInstanceOf[this.type]
}
class ioWritePort(d: Int, w: Int) extends Bundle
{
2013-08-12 19:39:11 +02:00
val addr = UInt(INPUT, log2Up(d))
val en = Bool(INPUT)
val data = Bits(INPUT, w)
override def clone = new ioWritePort(d, w).asInstanceOf[this.type]
}