2014-03-25 13:22:04 +01:00
|
|
|
package rocket
|
|
|
|
|
|
|
|
import Chisel._
|
|
|
|
import Util._
|
|
|
|
import Node._
|
|
|
|
|
2014-04-02 02:15:46 +02:00
|
|
|
case class BTBConfig(as: uncore.AddressSpaceConfiguration, entries: Int, nras: Int = 0) {
|
|
|
|
val matchBits = as.pgIdxBits
|
2014-04-08 00:58:49 +02:00
|
|
|
val pages0 = 1 max log2Up(entries) // is this sensible?
|
2014-03-25 13:22:04 +01:00
|
|
|
val pages = (pages0+1)/2*2 // control logic assumes 2 divides pages
|
2014-04-02 00:01:27 +02:00
|
|
|
val opaqueBits = log2Up(entries)
|
2014-04-08 00:58:49 +02:00
|
|
|
val nbht = 1 << log2Up(entries * 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
class RAS(implicit conf: BTBConfig) {
|
|
|
|
def push(addr: UInt): Unit = {
|
|
|
|
when (count < conf.nras) { count := count + 1 }
|
|
|
|
val nextPos = Mux(Bool(isPow2(conf.nras)) || pos > 0, pos+1, UInt(0))
|
|
|
|
stack(nextPos) := addr
|
|
|
|
pos := nextPos
|
|
|
|
}
|
|
|
|
def peek: UInt = stack(pos)
|
|
|
|
def pop: Unit = when (!isEmpty) {
|
|
|
|
count := count - 1
|
|
|
|
pos := Mux(Bool(isPow2(conf.nras)) || pos > 0, pos-1, UInt(conf.nras-1))
|
|
|
|
}
|
|
|
|
def clear: Unit = count := UInt(0)
|
|
|
|
def isEmpty: Bool = count === UInt(0)
|
|
|
|
|
|
|
|
private val count = Reg(init=UInt(0,log2Up(conf.nras+1)))
|
|
|
|
private val pos = Reg(init=UInt(0,log2Up(conf.nras)))
|
|
|
|
private val stack = Vec.fill(conf.nras){Reg(UInt())}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BHTResp(implicit conf: BTBConfig) extends Bundle {
|
|
|
|
val index = UInt(width = log2Up(conf.nbht).max(1))
|
|
|
|
val value = UInt(width = 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
class BHT(implicit conf: BTBConfig) {
|
|
|
|
def get(addr: UInt): BHTResp = {
|
|
|
|
val res = new BHTResp
|
|
|
|
res.index := addr(log2Up(conf.nbht)+1,2) ^ history
|
|
|
|
res.value := table(res.index)
|
|
|
|
res
|
|
|
|
}
|
|
|
|
def update(d: BHTResp, taken: Bool): Unit = {
|
|
|
|
table(d.index) := Cat(taken, (d.value(1) & d.value(0)) | ((d.value(1) | d.value(0)) & taken))
|
|
|
|
history := Cat(taken, history(log2Up(conf.nbht)-1,1))
|
|
|
|
}
|
|
|
|
|
|
|
|
private val table = Mem(UInt(width = 2), conf.nbht)
|
|
|
|
val history = Reg(UInt(width = log2Up(conf.nbht)))
|
2014-04-02 00:01:27 +02:00
|
|
|
}
|
|
|
|
|
2014-05-10 04:30:05 +02:00
|
|
|
class BTBUpdate(implicit val conf: BTBConfig) extends BundleWithConf {
|
2014-04-02 00:01:27 +02:00
|
|
|
val prediction = Valid(new BTBResp)
|
2014-04-02 02:15:46 +02:00
|
|
|
val pc = UInt(width = conf.as.vaddrBits)
|
|
|
|
val target = UInt(width = conf.as.vaddrBits)
|
|
|
|
val returnAddr = UInt(width = conf.as.vaddrBits)
|
2014-04-02 00:01:27 +02:00
|
|
|
val taken = Bool()
|
2014-04-08 00:58:49 +02:00
|
|
|
val isJump = Bool()
|
2014-04-02 00:01:27 +02:00
|
|
|
val isCall = Bool()
|
|
|
|
val isReturn = Bool()
|
|
|
|
val incorrectTarget = Bool()
|
|
|
|
}
|
|
|
|
|
2014-05-10 04:30:05 +02:00
|
|
|
class BTBResp(implicit val conf: BTBConfig) extends BundleWithConf {
|
2014-04-02 00:01:27 +02:00
|
|
|
val taken = Bool()
|
2014-04-02 02:15:46 +02:00
|
|
|
val target = UInt(width = conf.as.vaddrBits)
|
2014-04-08 00:58:49 +02:00
|
|
|
val entry = UInt(width = conf.opaqueBits)
|
|
|
|
val bht = new BHTResp
|
2014-04-02 00:01:27 +02:00
|
|
|
}
|
|
|
|
|
2014-03-25 13:22:04 +01:00
|
|
|
// fully-associative branch target buffer
|
2014-04-02 00:01:27 +02:00
|
|
|
class BTB(implicit conf: BTBConfig) extends Module {
|
2014-03-25 13:22:04 +01:00
|
|
|
val io = new Bundle {
|
2014-04-02 02:15:46 +02:00
|
|
|
val req = UInt(INPUT, conf.as.vaddrBits)
|
2014-04-02 00:01:27 +02:00
|
|
|
val resp = Valid(new BTBResp)
|
|
|
|
val update = Valid(new BTBUpdate).flip
|
|
|
|
val invalidate = Bool(INPUT)
|
2014-03-25 13:22:04 +01:00
|
|
|
}
|
|
|
|
|
2014-05-19 04:25:43 +02:00
|
|
|
val idxValid = Reg(init=UInt(0, conf.entries))
|
|
|
|
val idxs = Mem(UInt(width=conf.matchBits), conf.entries)
|
|
|
|
val idxPages = Mem(UInt(width=log2Up(conf.pages)), conf.entries)
|
|
|
|
val tgts = Mem(UInt(width=conf.matchBits), conf.entries)
|
|
|
|
val tgtPages = Mem(UInt(width=log2Up(conf.pages)), conf.entries)
|
|
|
|
val pages = Mem(UInt(width=conf.as.vaddrBits-conf.matchBits), conf.pages)
|
|
|
|
val pageValid = Reg(init=UInt(0, conf.pages))
|
2014-04-02 00:01:27 +02:00
|
|
|
val idxPagesOH = idxPages.map(UIntToOH(_)(conf.pages-1,0))
|
|
|
|
val tgtPagesOH = tgtPages.map(UIntToOH(_)(conf.pages-1,0))
|
|
|
|
|
2014-05-26 08:58:25 +02:00
|
|
|
val useRAS = Reg(UInt(width = conf.entries))
|
|
|
|
val isJump = Reg(UInt(width = conf.entries))
|
2014-03-25 13:22:04 +01:00
|
|
|
|
|
|
|
private def page(addr: UInt) = addr >> conf.matchBits
|
|
|
|
private def pageMatch(addr: UInt) = {
|
|
|
|
val p = page(addr)
|
2014-05-19 04:25:43 +02:00
|
|
|
Vec(pages.map(_ === p)).toBits & pageValid
|
2014-03-25 13:22:04 +01:00
|
|
|
}
|
|
|
|
private def tagMatch(addr: UInt, pgMatch: UInt): UInt = {
|
|
|
|
val idx = addr(conf.matchBits-1,0)
|
|
|
|
val idxMatch = idxs.map(_ === idx).toBits
|
|
|
|
val idxPageMatch = idxPagesOH.map(_ & pgMatch).map(_.orR).toBits
|
2014-05-19 04:25:43 +02:00
|
|
|
idxValid & idxMatch & idxPageMatch
|
2014-03-25 13:22:04 +01:00
|
|
|
}
|
|
|
|
|
2014-04-02 00:01:27 +02:00
|
|
|
val update = Pipe(io.update)
|
|
|
|
val update_target = io.req
|
|
|
|
|
|
|
|
val pageHit = pageMatch(io.req)
|
|
|
|
val hits = tagMatch(io.req, pageHit)
|
|
|
|
val updatePageHit = pageMatch(update.bits.pc)
|
|
|
|
val updateHits = tagMatch(update.bits.pc, updatePageHit)
|
2014-03-25 13:22:04 +01:00
|
|
|
|
2014-04-02 00:01:27 +02:00
|
|
|
private var lfsr = LFSR16(update.valid)
|
2014-03-25 13:22:04 +01:00
|
|
|
def rand(width: Int) = {
|
|
|
|
lfsr = lfsr(lfsr.getWidth-1,1)
|
|
|
|
Random.oneHot(width, lfsr)
|
|
|
|
}
|
|
|
|
|
2014-04-08 00:58:49 +02:00
|
|
|
val updateHit = update.bits.prediction.valid
|
|
|
|
val updateValid = update.bits.incorrectTarget || updateHit && Bool(conf.nbht > 0)
|
|
|
|
val updateTarget = updateValid && update.bits.incorrectTarget
|
2014-03-25 13:22:04 +01:00
|
|
|
|
2014-04-02 00:01:27 +02:00
|
|
|
val useUpdatePageHit = updatePageHit.orR
|
2014-04-08 00:58:49 +02:00
|
|
|
val doIdxPageRepl = updateTarget && !useUpdatePageHit
|
|
|
|
val idxPageRepl = UInt()
|
|
|
|
val idxPageUpdateOH = Mux(useUpdatePageHit, updatePageHit, idxPageRepl)
|
|
|
|
val idxPageUpdate = OHToUInt(idxPageUpdateOH)
|
2014-03-25 13:22:04 +01:00
|
|
|
val idxPageReplEn = Mux(doIdxPageRepl, idxPageRepl, UInt(0))
|
|
|
|
|
2014-04-02 00:01:27 +02:00
|
|
|
val samePage = page(update.bits.pc) === page(update_target)
|
|
|
|
val usePageHit = (pageHit & ~idxPageReplEn).orR
|
2014-04-08 00:58:49 +02:00
|
|
|
val doTgtPageRepl = updateTarget && !samePage && !usePageHit
|
|
|
|
val tgtPageRepl = Mux(samePage, idxPageUpdateOH, idxPageUpdateOH(conf.pages-2,0) << 1 | idxPageUpdateOH(conf.pages-1))
|
|
|
|
val tgtPageUpdate = OHToUInt(Mux(usePageHit, pageHit, tgtPageRepl))
|
2014-03-25 13:22:04 +01:00
|
|
|
val tgtPageReplEn = Mux(doTgtPageRepl, tgtPageRepl, UInt(0))
|
2014-05-19 04:25:43 +02:00
|
|
|
val doPageRepl = doIdxPageRepl || doTgtPageRepl
|
2014-03-25 13:22:04 +01:00
|
|
|
|
|
|
|
val pageReplEn = idxPageReplEn | tgtPageReplEn
|
2014-05-19 04:25:43 +02:00
|
|
|
idxPageRepl := UIntToOH(Counter(update.valid && doPageRepl, conf.pages)._1)
|
2014-04-08 00:58:49 +02:00
|
|
|
|
|
|
|
when (update.valid && !(updateValid && !updateTarget)) {
|
|
|
|
val nextRepl = Counter(!updateHit && updateValid, conf.entries)._1
|
|
|
|
val waddr = Mux(updateHit, update.bits.prediction.bits.entry, nextRepl)
|
2014-03-25 13:22:04 +01:00
|
|
|
|
2014-05-20 03:56:30 +02:00
|
|
|
// invalidate entries if we stomp on pages they depend upon
|
|
|
|
idxValid := idxValid & ~Vec.tabulate(conf.entries)(i => (pageReplEn & (idxPagesOH(i) | tgtPagesOH(i))).orR).toBits
|
|
|
|
|
|
|
|
idxValid(waddr) := updateValid
|
2014-05-19 04:25:43 +02:00
|
|
|
when (updateTarget) {
|
|
|
|
assert(io.req === update.bits.target, "BTB request != I$ target")
|
|
|
|
idxs(waddr) := update.bits.pc
|
|
|
|
tgts(waddr) := update_target
|
|
|
|
idxPages(waddr) := idxPageUpdate
|
|
|
|
tgtPages(waddr) := tgtPageUpdate
|
|
|
|
useRAS(waddr) := update.bits.isReturn
|
|
|
|
isJump(waddr) := update.bits.isJump
|
2014-03-25 13:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
require(conf.pages % 2 == 0)
|
2014-04-08 00:58:49 +02:00
|
|
|
val idxWritesEven = (idxPageUpdateOH & Fill(conf.pages/2, UInt(1,2))).orR
|
2014-03-25 13:22:04 +01:00
|
|
|
|
2014-05-19 04:25:43 +02:00
|
|
|
def writeBank(i: Int, mod: Int, en: Bool, data: UInt) =
|
|
|
|
for (i <- i until conf.pages by mod)
|
|
|
|
when (en && pageReplEn(i)) { pages(i) := data }
|
|
|
|
|
2014-03-25 13:22:04 +01:00
|
|
|
writeBank(0, 2, Mux(idxWritesEven, doIdxPageRepl, doTgtPageRepl),
|
2014-04-02 00:01:27 +02:00
|
|
|
Mux(idxWritesEven, page(update.bits.pc), page(update_target)))
|
2014-03-25 13:22:04 +01:00
|
|
|
writeBank(1, 2, Mux(idxWritesEven, doTgtPageRepl, doIdxPageRepl),
|
2014-04-02 00:01:27 +02:00
|
|
|
Mux(idxWritesEven, page(update_target), page(update.bits.pc)))
|
2014-05-19 04:25:43 +02:00
|
|
|
|
|
|
|
when (doPageRepl) { pageValid := pageValid | pageReplEn }
|
2014-03-25 13:22:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
when (io.invalidate) {
|
2014-05-19 04:25:43 +02:00
|
|
|
idxValid := 0
|
|
|
|
pageValid := 0
|
2014-03-25 13:22:04 +01:00
|
|
|
}
|
|
|
|
|
2014-05-26 08:58:25 +02:00
|
|
|
io.resp.valid := hits.orR
|
2014-04-02 00:01:27 +02:00
|
|
|
io.resp.bits.taken := io.resp.valid
|
|
|
|
io.resp.bits.target := Cat(Mux1H(Mux1H(hits, tgtPagesOH), pages), Mux1H(hits, tgts))
|
2014-04-08 00:58:49 +02:00
|
|
|
io.resp.bits.entry := OHToUInt(hits)
|
|
|
|
|
|
|
|
if (conf.nbht > 0) {
|
|
|
|
val bht = new BHT
|
|
|
|
val res = bht.get(io.req)
|
|
|
|
when (update.valid && updateHit && !update.bits.isJump) { bht.update(update.bits.prediction.bits.bht, update.bits.taken) }
|
|
|
|
when (!res.value(0) && !Mux1H(hits, isJump)) { io.resp.bits.taken := false }
|
|
|
|
io.resp.bits.bht := res
|
|
|
|
}
|
2014-04-02 00:01:27 +02:00
|
|
|
|
|
|
|
if (conf.nras > 0) {
|
|
|
|
val ras = new RAS
|
2014-04-08 08:47:53 +02:00
|
|
|
val doPeek = Mux1H(hits, useRAS)
|
|
|
|
when (!ras.isEmpty && doPeek) {
|
2014-04-08 00:58:49 +02:00
|
|
|
io.resp.bits.target := ras.peek
|
2014-04-02 00:01:27 +02:00
|
|
|
}
|
2014-04-08 00:58:49 +02:00
|
|
|
when (io.update.valid) {
|
|
|
|
when (io.update.bits.isCall) {
|
|
|
|
ras.push(io.update.bits.returnAddr)
|
2014-04-08 08:47:53 +02:00
|
|
|
when (doPeek) {
|
|
|
|
io.resp.bits.target := io.update.bits.returnAddr
|
|
|
|
}
|
2014-04-08 00:58:49 +02:00
|
|
|
}.elsewhen (io.update.bits.isReturn && io.update.bits.prediction.valid) {
|
|
|
|
ras.pop
|
|
|
|
}
|
2014-04-02 00:01:27 +02:00
|
|
|
}
|
|
|
|
when (io.invalidate) { ras.clear }
|
|
|
|
}
|
2014-03-25 13:22:04 +01:00
|
|
|
}
|