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._
|
2012-10-02 01:08:41 +02:00
|
|
|
import uncore._
|
2012-10-12 01:54:28 +02:00
|
|
|
import Util._
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2012-11-05 01:59:36 +01:00
|
|
|
case class ICacheConfig(sets: Int, assoc: Int, co: CoherencePolicyWithUncached,
|
2012-11-25 13:24:25 +01:00
|
|
|
ntlb: Int = 8, nbtb: Int = 8,
|
2012-11-25 07:00:43 +01:00
|
|
|
code: Code = new IdentityCode)
|
2011-10-26 08:02:47 +02:00
|
|
|
{
|
2012-10-10 06:35:03 +02:00
|
|
|
val w = 1
|
2012-11-06 08:52:32 +01:00
|
|
|
val ibytes = 4
|
2012-10-10 06:35:03 +02:00
|
|
|
|
|
|
|
val dm = assoc == 1
|
|
|
|
val lines = sets * assoc
|
|
|
|
val databits = MEM_DATA_BITS
|
|
|
|
val idxbits = log2Up(sets)
|
|
|
|
val offbits = OFFSET_BITS
|
|
|
|
val untagbits = idxbits + offbits
|
|
|
|
val tagbits = PADDR_BITS - untagbits
|
|
|
|
|
|
|
|
require(isPow2(sets) && isPow2(assoc))
|
|
|
|
require(isPow2(w) && isPow2(ibytes))
|
|
|
|
require(PGIDX_BITS >= untagbits)
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
|
2012-10-10 06:35:03 +02:00
|
|
|
class FrontendReq extends Bundle {
|
|
|
|
val pc = UFix(width = VADDR_BITS+1)
|
|
|
|
val mispredict = Bool()
|
|
|
|
val taken = Bool()
|
|
|
|
val currentpc = UFix(width = VADDR_BITS+1)
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
|
2012-11-06 08:52:32 +01:00
|
|
|
class FrontendResp(implicit conf: ICacheConfig) extends Bundle {
|
2012-10-10 06:35:03 +02:00
|
|
|
val pc = UFix(width = VADDR_BITS+1) // ID stage PC
|
2012-11-06 08:52:32 +01:00
|
|
|
val data = Bits(width = conf.ibytes*8)
|
2012-10-10 06:35:03 +02:00
|
|
|
val taken = Bool()
|
|
|
|
val xcpt_ma = Bool()
|
|
|
|
val xcpt_if = Bool()
|
2012-11-06 08:52:32 +01:00
|
|
|
|
|
|
|
override def clone = new FrontendResp().asInstanceOf[this.type]
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
2012-01-12 04:20:20 +01:00
|
|
|
|
2013-01-07 22:38:59 +01:00
|
|
|
class CPUFrontendIO(implicit conf: ICacheConfig) extends Bundle {
|
2012-10-10 06:35:03 +02:00
|
|
|
val req = new PipeIO()(new FrontendReq)
|
|
|
|
val resp = new FIFOIO()(new FrontendResp).flip
|
2013-01-07 22:38:59 +01:00
|
|
|
val ptw = new TLBPTWIO().flip
|
2012-11-16 10:55:45 +01:00
|
|
|
val invalidate = Bool(OUTPUT)
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
|
|
|
|
2013-01-16 00:50:37 +01:00
|
|
|
class Frontend(implicit c: ICacheConfig, lnconf: LogicalNetworkConfiguration) extends Component
|
2012-10-10 06:35:03 +02:00
|
|
|
{
|
|
|
|
val io = new Bundle {
|
2013-01-07 22:38:59 +01:00
|
|
|
val cpu = new CPUFrontendIO()(c).flip
|
2013-03-20 22:05:12 +01:00
|
|
|
val mem = new UncachedTileLinkIO
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2012-11-25 13:24:25 +01:00
|
|
|
val btb = new rocketDpathBTB(c.nbtb)
|
2012-11-06 08:52:32 +01:00
|
|
|
val icache = new ICache
|
2012-11-25 13:24:25 +01:00
|
|
|
val tlb = new TLB(c.ntlb)
|
2012-10-10 06:35:03 +02:00
|
|
|
|
|
|
|
val s1_pc = Reg() { UFix() }
|
2012-10-12 01:54:28 +02:00
|
|
|
val s1_same_block = Reg() { Bool() }
|
2012-10-10 06:35:03 +02:00
|
|
|
val s2_valid = Reg(resetVal = Bool(true))
|
|
|
|
val s2_pc = Reg(resetVal = UFix(START_ADDR))
|
|
|
|
val s2_btb_hit = Reg(resetVal = Bool(false))
|
|
|
|
val s2_xcpt_if = Reg(resetVal = Bool(false))
|
|
|
|
|
|
|
|
val btbTarget = Cat(btb.io.target(VADDR_BITS-1), btb.io.target)
|
|
|
|
val pcp4_0 = s1_pc + UFix(c.ibytes)
|
|
|
|
val pcp4 = Cat(s1_pc(VADDR_BITS-1) & pcp4_0(VADDR_BITS-1), pcp4_0(VADDR_BITS-1,0))
|
|
|
|
val icmiss = s2_valid && !icache.io.resp.valid
|
2012-10-12 01:54:28 +02:00
|
|
|
val predicted_npc = Mux(btb.io.hit, btbTarget, pcp4)
|
|
|
|
val npc = Mux(icmiss, s2_pc, predicted_npc).toUFix
|
|
|
|
val s0_same_block = !icmiss && !io.cpu.req.valid && (predicted_npc >> log2Up(c.databits/8)) === (s1_pc >> log2Up(c.databits/8))
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2012-10-12 01:54:28 +02:00
|
|
|
val stall = io.cpu.resp.valid && !io.cpu.resp.ready
|
2012-10-10 06:35:03 +02:00
|
|
|
when (!stall) {
|
2012-10-12 01:54:28 +02:00
|
|
|
s1_same_block := s0_same_block && !tlb.io.resp.miss
|
2012-10-10 06:35:03 +02:00
|
|
|
s1_pc := npc
|
|
|
|
s2_valid := !icmiss
|
2013-05-22 03:59:21 +02:00
|
|
|
when (!icmiss) {
|
|
|
|
s2_pc := s1_pc
|
|
|
|
s2_btb_hit := btb.io.hit
|
|
|
|
s2_xcpt_if := tlb.io.resp.xcpt_if
|
|
|
|
}
|
2011-11-05 04:52:21 +01:00
|
|
|
}
|
2012-10-10 06:35:03 +02:00
|
|
|
when (io.cpu.req.valid) {
|
2012-10-12 01:54:28 +02:00
|
|
|
s1_same_block := Bool(false)
|
2012-10-10 06:35:03 +02:00
|
|
|
s1_pc := io.cpu.req.bits.pc
|
|
|
|
s2_valid := Bool(false)
|
2011-11-05 04:52:21 +01:00
|
|
|
}
|
2012-10-10 06:35:03 +02:00
|
|
|
|
|
|
|
btb.io.current_pc := s1_pc
|
|
|
|
btb.io.wen := io.cpu.req.bits.mispredict
|
|
|
|
btb.io.clr := !io.cpu.req.bits.taken
|
|
|
|
btb.io.correct_pc := io.cpu.req.bits.currentpc
|
|
|
|
btb.io.correct_target := io.cpu.req.bits.pc
|
2012-11-16 10:55:45 +01:00
|
|
|
btb.io.invalidate := io.cpu.invalidate || io.cpu.ptw.invalidate
|
2012-10-10 06:35:03 +02:00
|
|
|
|
|
|
|
tlb.io.ptw <> io.cpu.ptw
|
|
|
|
tlb.io.req.valid := !stall && !icmiss
|
|
|
|
tlb.io.req.bits.vpn := s1_pc >> UFix(PGIDX_BITS)
|
|
|
|
tlb.io.req.bits.asid := UFix(0)
|
2012-11-06 17:13:44 +01:00
|
|
|
tlb.io.req.bits.passthrough := Bool(false)
|
2012-10-10 06:35:03 +02:00
|
|
|
tlb.io.req.bits.instruction := Bool(true)
|
|
|
|
|
|
|
|
icache.io.mem <> io.mem
|
2012-10-12 01:54:28 +02:00
|
|
|
icache.io.req.valid := !stall && !s0_same_block
|
2012-10-10 06:35:03 +02:00
|
|
|
icache.io.req.bits.idx := Mux(io.cpu.req.valid, io.cpu.req.bits.pc, npc)
|
2012-11-16 10:55:45 +01:00
|
|
|
icache.io.invalidate := io.cpu.invalidate
|
2012-10-10 06:35:03 +02:00
|
|
|
icache.io.req.bits.ppn := tlb.io.resp.ppn
|
2013-05-21 00:22:58 +02:00
|
|
|
icache.io.req.bits.kill := io.cpu.req.valid || tlb.io.resp.miss || icmiss
|
2012-10-12 01:54:28 +02:00
|
|
|
icache.io.resp.ready := !stall && !s1_same_block
|
2012-10-10 06:35:03 +02:00
|
|
|
|
|
|
|
io.cpu.resp.valid := s2_valid && (s2_xcpt_if || icache.io.resp.valid)
|
|
|
|
io.cpu.resp.bits.pc := s2_pc
|
2012-10-12 01:54:28 +02:00
|
|
|
io.cpu.resp.bits.data := icache.io.resp.bits.datablock >> (s2_pc(log2Up(c.databits/8)-1,log2Up(c.ibytes)) << log2Up(c.ibytes*8))
|
2012-10-10 06:35:03 +02:00
|
|
|
io.cpu.resp.bits.taken := s2_btb_hit
|
|
|
|
io.cpu.resp.bits.xcpt_ma := s2_pc(log2Up(c.ibytes)-1,0) != UFix(0)
|
|
|
|
io.cpu.resp.bits.xcpt_if := s2_xcpt_if
|
|
|
|
}
|
|
|
|
|
2013-01-16 00:50:37 +01:00
|
|
|
class ICache(implicit c: ICacheConfig, lnconf: LogicalNetworkConfiguration) extends Component
|
2012-10-10 06:35:03 +02:00
|
|
|
{
|
|
|
|
val io = new Bundle {
|
|
|
|
val req = new PipeIO()(new Bundle {
|
|
|
|
val idx = UFix(width = PGIDX_BITS)
|
|
|
|
val ppn = UFix(width = PPN_BITS) // delayed one cycle
|
|
|
|
val kill = Bool() // delayed one cycle
|
|
|
|
}).flip
|
|
|
|
val resp = new FIFOIO()(new Bundle {
|
2012-11-06 08:52:32 +01:00
|
|
|
val data = Bits(width = c.ibytes*8)
|
2012-10-10 06:35:03 +02:00
|
|
|
val datablock = Bits(width = c.databits)
|
|
|
|
})
|
2012-11-16 10:55:45 +01:00
|
|
|
val invalidate = Bool(INPUT)
|
2013-03-20 22:05:12 +01:00
|
|
|
val mem = new UncachedTileLinkIO
|
2012-01-25 00:13:49 +01:00
|
|
|
}
|
|
|
|
|
2012-10-10 06:35:03 +02:00
|
|
|
val s_ready :: s_request :: s_refill_wait :: s_refill :: Nil = Enum(4) { UFix() }
|
|
|
|
val state = Reg(resetVal = s_ready)
|
|
|
|
val invalidated = Reg() { Bool() }
|
|
|
|
val stall = !io.resp.ready
|
|
|
|
val rdy = Bool()
|
|
|
|
|
|
|
|
val s2_valid = Reg(resetVal = Bool(false))
|
|
|
|
val s2_addr = Reg { UFix(width = PADDR_BITS) }
|
2012-10-12 01:54:28 +02:00
|
|
|
val s2_any_tag_hit = Bool()
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2012-10-10 06:35:03 +02:00
|
|
|
val s1_valid = Reg(resetVal = Bool(false))
|
|
|
|
val s1_pgoff = Reg() { UFix(width = PGIDX_BITS) }
|
2012-11-05 01:39:25 +01:00
|
|
|
val s1_addr = Cat(io.req.bits.ppn, s1_pgoff).toUFix
|
|
|
|
val s1_tag = s1_addr(c.tagbits+c.untagbits-1,c.untagbits)
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2012-11-16 10:55:45 +01:00
|
|
|
val s0_valid = io.req.valid || s1_valid && stall
|
2012-10-10 06:35:03 +02:00
|
|
|
val s0_pgoff = Mux(io.req.valid, io.req.bits.idx, s1_pgoff)
|
|
|
|
|
2012-11-16 10:55:45 +01:00
|
|
|
s1_valid := io.req.valid && rdy || s1_valid && stall && !io.req.bits.kill
|
2012-10-10 06:35:03 +02:00
|
|
|
when (io.req.valid && rdy) {
|
|
|
|
s1_pgoff := s0_pgoff
|
2011-11-05 04:52:21 +01:00
|
|
|
}
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2012-10-12 01:54:28 +02:00
|
|
|
s2_valid := s1_valid && rdy && !io.req.bits.kill || io.resp.valid && stall
|
2012-10-10 06:35:03 +02:00
|
|
|
when (s1_valid && rdy && !stall) {
|
2012-11-05 01:39:25 +01:00
|
|
|
s2_addr := s1_addr
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
val s2_tag = s2_addr(c.tagbits+c.untagbits-1,c.untagbits)
|
|
|
|
val s2_idx = s2_addr(c.untagbits-1,c.offbits)
|
|
|
|
val s2_offset = s2_addr(c.offbits-1,0)
|
|
|
|
val s2_hit = s2_valid && s2_any_tag_hit
|
|
|
|
val s2_miss = s2_valid && !s2_any_tag_hit
|
|
|
|
rdy := state === s_ready && !s2_miss
|
|
|
|
|
2013-05-19 03:09:23 +02:00
|
|
|
//assert(!c.co.isVoluntary(io.mem.grant.bits.payload) || !io.mem.grant.valid, "UncachedRequestors shouldn't get voluntary grants.")
|
2013-05-02 06:01:20 +02:00
|
|
|
val (rf_cnt, refill_done) = Counter(io.mem.grant.valid, REFILL_CYCLES)
|
2012-10-10 06:35:03 +02:00
|
|
|
val repl_way = if (c.dm) UFix(0) else LFSR16(s2_miss)(log2Up(c.assoc)-1,0)
|
|
|
|
|
2012-11-25 07:00:43 +01:00
|
|
|
val enc_tagbits = c.code.width(c.tagbits)
|
|
|
|
val tag_array = Mem(c.sets, seqRead = true) { Bits(width = enc_tagbits*c.assoc) }
|
2013-02-21 01:09:46 +01:00
|
|
|
val tag_raddr = Reg{UFix()}
|
2012-10-10 06:35:03 +02:00
|
|
|
when (refill_done) {
|
2012-11-25 07:00:43 +01:00
|
|
|
val wmask = FillInterleaved(enc_tagbits, if (c.dm) Bits(1) else UFixToOH(repl_way))
|
|
|
|
val tag = c.code.encode(s2_tag)
|
2012-10-10 06:35:03 +02:00
|
|
|
tag_array.write(s2_idx, Fill(c.assoc, tag), wmask)
|
2012-11-05 01:39:25 +01:00
|
|
|
}
|
2013-01-24 04:27:53 +01:00
|
|
|
// /*.else*/when (s0_valid) { // uncomment ".else" to infer 6T SRAM
|
|
|
|
.elsewhen (s0_valid) {
|
2013-02-21 01:09:46 +01:00
|
|
|
tag_raddr := s0_pgoff(c.untagbits-1,c.offbits)
|
2012-07-12 23:50:12 +02:00
|
|
|
}
|
|
|
|
|
2012-10-10 06:35:03 +02:00
|
|
|
val vb_array = Reg(resetVal = Bits(0, c.lines))
|
|
|
|
when (refill_done && !invalidated) {
|
|
|
|
vb_array := vb_array.bitSet(Cat(repl_way, s2_idx), Bool(true))
|
|
|
|
}
|
2012-11-16 10:55:45 +01:00
|
|
|
when (io.invalidate) {
|
2012-07-12 23:50:12 +02:00
|
|
|
vb_array := Bits(0)
|
2012-10-10 06:35:03 +02:00
|
|
|
invalidated := Bool(true)
|
2012-07-12 23:50:12 +02:00
|
|
|
}
|
2012-10-10 06:35:03 +02:00
|
|
|
val s2_disparity = Vec(c.assoc) { Bool() }
|
|
|
|
for (i <- 0 until c.assoc)
|
|
|
|
when (s2_valid && s2_disparity(i)) { vb_array := vb_array.bitSet(Cat(UFix(i), s2_idx), Bool(false)) }
|
|
|
|
|
2012-11-05 01:39:25 +01:00
|
|
|
val s1_tag_match = Vec(c.assoc) { Bool() }
|
2012-10-10 06:35:03 +02:00
|
|
|
val s2_tag_hit = Vec(c.assoc) { Bool() }
|
2012-11-25 07:00:43 +01:00
|
|
|
val s2_dout = Vec(c.assoc){Reg{Bits()}}
|
|
|
|
|
2012-10-10 06:35:03 +02:00
|
|
|
for (i <- 0 until c.assoc) {
|
|
|
|
val s1_vb = vb_array(Cat(UFix(i), s1_pgoff(c.untagbits-1,c.offbits))).toBool
|
|
|
|
val s2_vb = Reg() { Bool() }
|
2012-11-05 01:39:25 +01:00
|
|
|
val s2_tag_disparity = Reg() { Bool() }
|
|
|
|
val s2_tag_match = Reg() { Bool() }
|
2013-02-21 01:09:46 +01:00
|
|
|
val tag_out = tag_array(tag_raddr)(enc_tagbits*(i+1)-1, enc_tagbits*i)
|
2012-10-10 06:35:03 +02:00
|
|
|
when (s1_valid && rdy && !stall) {
|
|
|
|
s2_vb := s1_vb
|
2012-11-25 07:00:43 +01:00
|
|
|
s2_tag_disparity := c.code.decode(tag_out).error
|
2012-11-05 01:39:25 +01:00
|
|
|
s2_tag_match := s1_tag_match(i)
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
2012-11-05 01:39:25 +01:00
|
|
|
s1_tag_match(i) := tag_out(c.tagbits-1,0) === s1_tag
|
|
|
|
s2_tag_hit(i) := s2_vb && s2_tag_match
|
2012-11-25 07:00:43 +01:00
|
|
|
s2_disparity(i) := s2_vb && (s2_tag_disparity || c.code.decode(s2_dout(i)).error)
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
|
|
|
s2_any_tag_hit := s2_tag_hit.reduceLeft(_||_) && !s2_disparity.reduceLeft(_||_)
|
|
|
|
|
|
|
|
for (i <- 0 until c.assoc) {
|
2012-11-25 07:00:43 +01:00
|
|
|
val data_array = Mem(c.sets*REFILL_CYCLES, seqRead = true){ Bits(width = c.code.width(c.databits)) }
|
2013-02-21 01:09:46 +01:00
|
|
|
val s1_raddr = Reg{UFix()}
|
2013-05-02 06:01:20 +02:00
|
|
|
when (io.mem.grant.valid && repl_way === UFix(i)) {
|
2013-01-22 02:18:23 +01:00
|
|
|
val d = io.mem.grant.bits.payload.data
|
2012-11-25 07:00:43 +01:00
|
|
|
data_array(Cat(s2_idx,rf_cnt)) := c.code.encode(d)
|
2012-11-05 01:39:25 +01:00
|
|
|
}
|
2013-01-24 04:27:53 +01:00
|
|
|
// /*.else*/when (s0_valid) { // uncomment ".else" to infer 6T SRAM
|
|
|
|
.elsewhen (s0_valid) {
|
2013-02-21 01:09:46 +01:00
|
|
|
s1_raddr := s0_pgoff(c.untagbits-1,c.offbits-rf_cnt.getWidth)
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
2012-11-05 01:39:25 +01:00
|
|
|
// if s1_tag_match is critical, replace with partial tag check
|
2013-02-21 01:09:46 +01:00
|
|
|
when (s1_valid && rdy && !stall && (Bool(c.dm) || s1_tag_match(i))) { s2_dout(i) := data_array(s1_raddr) }
|
2012-01-25 01:51:30 +01:00
|
|
|
}
|
2012-10-12 01:54:28 +02:00
|
|
|
val s2_dout_word = s2_dout.map(x => (x >> (s2_offset(log2Up(c.databits/8)-1,log2Up(c.ibytes)) << log2Up(c.ibytes*8)))(c.ibytes*8-1,0))
|
2012-10-10 06:35:03 +02:00
|
|
|
io.resp.bits.data := Mux1H(s2_tag_hit, s2_dout_word)
|
|
|
|
io.resp.bits.datablock := Mux1H(s2_tag_hit, s2_dout)
|
2011-12-04 04:41:15 +01:00
|
|
|
|
2013-01-22 02:18:23 +01:00
|
|
|
val finish_q = (new Queue(1)) { new GrantAck }
|
2013-01-29 01:44:17 +01:00
|
|
|
finish_q.io.enq.valid := refill_done && c.co.requiresAck(io.mem.grant.bits.payload)
|
2013-01-22 02:18:23 +01:00
|
|
|
finish_q.io.enq.bits.master_xact_id := io.mem.grant.bits.payload.master_xact_id
|
2012-03-07 00:47:19 +01:00
|
|
|
|
2011-11-07 09:58:25 +01:00
|
|
|
// output signals
|
2012-10-10 06:35:03 +02:00
|
|
|
io.resp.valid := s2_hit
|
2013-05-22 02:21:04 +02:00
|
|
|
io.mem.acquire.meta.valid := (state === s_request) && finish_q.io.enq.ready
|
|
|
|
io.mem.acquire.meta.bits.payload := c.co.getUncachedReadAcquire(s2_addr >> UFix(c.offbits), UFix(0))
|
|
|
|
io.mem.acquire.data.valid := Bool(false)
|
2013-01-22 02:18:23 +01:00
|
|
|
io.mem.grant_ack <> FIFOedLogicalNetworkIOWrapper(finish_q.io.deq)
|
|
|
|
io.mem.grant.ready := Bool(true)
|
2011-10-26 08:02:47 +02:00
|
|
|
|
|
|
|
// control state machine
|
|
|
|
switch (state) {
|
|
|
|
is (s_ready) {
|
2012-10-10 06:35:03 +02:00
|
|
|
when (s2_miss) { state := s_request }
|
2012-03-06 09:31:44 +01:00
|
|
|
invalidated := Bool(false)
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
2012-10-10 06:35:03 +02:00
|
|
|
is (s_request) {
|
2013-05-22 02:21:04 +02:00
|
|
|
when (io.mem.acquire.meta.ready && finish_q.io.enq.ready) { state := s_refill_wait }
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
is (s_refill_wait) {
|
2013-01-22 02:18:23 +01:00
|
|
|
when (io.mem.grant.valid) { state := s_refill }
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
|
|
|
is (s_refill) {
|
2012-10-10 06:35:03 +02:00
|
|
|
when (refill_done) { state := s_ready }
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
2011-10-26 08:02:47 +02:00
|
|
|
}
|