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._
|
2015-10-22 03:18:32 +02:00
|
|
|
import cde.{Parameters, Field}
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2015-12-03 02:17:49 +01:00
|
|
|
case object ICacheBufferWays extends Field[Boolean]
|
|
|
|
|
2015-10-06 06:48:05 +02:00
|
|
|
trait HasL1CacheParameters extends HasCacheParameters with HasCoreParameters {
|
2015-10-14 08:42:53 +02:00
|
|
|
val outerDataBeats = p(TLKey(p(TLId))).dataBeats
|
|
|
|
val outerDataBits = p(TLKey(p(TLId))).dataBitsPerBeat
|
2015-03-01 02:00:05 +01:00
|
|
|
val refillCyclesPerBeat = outerDataBits/rowBits
|
2014-12-17 23:28:05 +01:00
|
|
|
val refillCycles = refillCyclesPerBeat*outerDataBeats
|
2014-09-01 22:28:58 +02:00
|
|
|
}
|
2014-08-12 03:36:23 +02:00
|
|
|
|
2015-10-06 06:48:05 +02:00
|
|
|
class ICacheReq(implicit p: Parameters) extends CoreBundle()(p) {
|
2014-08-12 03:36:23 +02:00
|
|
|
val idx = UInt(width = pgIdxBits)
|
2015-02-02 05:04:13 +01:00
|
|
|
val ppn = UInt(width = ppnBits) // delayed one cycle
|
2013-08-12 19:39:11 +02:00
|
|
|
val kill = Bool() // delayed one cycle
|
|
|
|
}
|
|
|
|
|
2015-10-06 06:48:05 +02:00
|
|
|
class ICacheResp(implicit p: Parameters) extends CoreBundle()(p) with HasL1CacheParameters {
|
2014-09-01 22:28:58 +02:00
|
|
|
val data = Bits(width = coreInstBits)
|
2014-08-12 03:36:23 +02:00
|
|
|
val datablock = Bits(width = rowBits)
|
2013-08-12 19:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-10-06 06:48:05 +02:00
|
|
|
class ICache(implicit p: Parameters) extends CoreModule()(p) with HasL1CacheParameters {
|
2012-10-10 06:35:03 +02:00
|
|
|
val io = new Bundle {
|
2013-08-12 19:39:11 +02:00
|
|
|
val req = Valid(new ICacheReq).flip
|
|
|
|
val resp = Decoupled(new ICacheResp)
|
2012-11-16 10:55:45 +01:00
|
|
|
val invalidate = Bool(INPUT)
|
2015-04-18 01:56:53 +02:00
|
|
|
val mem = new ClientUncachedTileLinkIO
|
2012-01-25 00:13:49 +01:00
|
|
|
}
|
2014-08-08 21:23:02 +02:00
|
|
|
require(isPow2(nSets) && isPow2(nWays))
|
2014-09-01 22:28:58 +02:00
|
|
|
require(isPow2(coreInstBytes))
|
2014-08-12 03:36:23 +02:00
|
|
|
require(pgIdxBits >= untagBits)
|
2012-01-25 00:13:49 +01:00
|
|
|
|
2013-09-10 19:51:35 +02:00
|
|
|
val s_ready :: s_request :: s_refill_wait :: s_refill :: Nil = Enum(UInt(), 4)
|
2013-08-16 00:28:15 +02:00
|
|
|
val state = Reg(init=s_ready)
|
2013-08-12 19:39:11 +02:00
|
|
|
val invalidated = Reg(Bool())
|
2012-10-10 06:35:03 +02:00
|
|
|
val stall = !io.resp.ready
|
2015-07-16 05:24:18 +02:00
|
|
|
val rdy = Wire(Bool())
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2015-04-22 20:26:03 +02:00
|
|
|
val refill_addr = Reg(UInt(width = paddrBits))
|
2016-01-12 21:42:57 +01:00
|
|
|
val s1_any_tag_hit = Wire(Bool())
|
2011-10-26 08:02:47 +02:00
|
|
|
|
2013-08-16 00:28:15 +02:00
|
|
|
val s1_valid = Reg(init=Bool(false))
|
2014-08-12 03:36:23 +02:00
|
|
|
val s1_pgoff = Reg(UInt(width = pgIdxBits))
|
2013-08-12 19:39:11 +02:00
|
|
|
val s1_addr = Cat(io.req.bits.ppn, s1_pgoff).toUInt
|
2014-08-12 03:36:23 +02:00
|
|
|
val s1_tag = s1_addr(tagBits+untagBits-1,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
|
2013-11-24 23:19:46 +01:00
|
|
|
val s0_pgoff = Mux(s1_valid && stall, s1_pgoff, io.req.bits.idx)
|
2012-10-10 06:35:03 +02:00
|
|
|
|
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) {
|
2013-11-24 23:19:46 +01:00
|
|
|
s1_pgoff := io.req.bits.idx
|
2011-11-05 04:52:21 +01:00
|
|
|
}
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2015-04-22 20:26:03 +02:00
|
|
|
val out_valid = s1_valid && !io.req.bits.kill && state === s_ready
|
|
|
|
val s1_idx = s1_addr(untagBits-1,blockOffBits)
|
|
|
|
val s1_offset = s1_addr(blockOffBits-1,0)
|
|
|
|
val s1_hit = out_valid && s1_any_tag_hit
|
|
|
|
val s1_miss = out_valid && !s1_any_tag_hit
|
|
|
|
rdy := state === s_ready && !s1_miss
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2015-04-22 20:26:03 +02:00
|
|
|
when (s1_valid && state === s_ready && s1_miss) {
|
|
|
|
refill_addr := s1_addr
|
|
|
|
}
|
|
|
|
val refill_tag = refill_addr(tagBits+untagBits-1,untagBits)
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2015-03-27 07:26:03 +01:00
|
|
|
val narrow_grant = FlowThroughSerializer(io.mem.grant, refillCyclesPerBeat)
|
|
|
|
val (refill_cnt, refill_wrap) = Counter(narrow_grant.fire(), refillCycles) //TODO Zero width wire
|
2014-12-07 12:09:54 +01:00
|
|
|
val refill_done = state === s_refill && refill_wrap
|
2015-03-27 07:26:03 +01:00
|
|
|
narrow_grant.ready := Bool(true)
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2015-04-22 20:26:03 +02:00
|
|
|
val repl_way = if (isDM) UInt(0) else LFSR16(s1_miss)(log2Up(nWays)-1,0)
|
2014-08-12 03:36:23 +02:00
|
|
|
val entagbits = code.width(tagBits)
|
2016-01-12 21:42:57 +01:00
|
|
|
val tag_array = SeqMem(nSets, Vec(nWays, Bits(width = entagbits)))
|
2015-07-11 22:32:45 +02:00
|
|
|
val tag_rdata = tag_array.read(s0_pgoff(untagBits-1,blockOffBits), !refill_done && s0_valid)
|
2012-10-10 06:35:03 +02:00
|
|
|
when (refill_done) {
|
2015-04-22 20:26:03 +02:00
|
|
|
val tag = code.encode(refill_tag).toUInt
|
2015-10-18 22:09:17 +02:00
|
|
|
tag_array.write(s1_idx, Vec.fill(nWays)(tag), Vec.tabulate(nWays)(repl_way === _))
|
2012-11-05 01:39:25 +01:00
|
|
|
}
|
2012-07-12 23:50:12 +02:00
|
|
|
|
2014-08-08 21:23:02 +02:00
|
|
|
val vb_array = Reg(init=Bits(0, nSets*nWays))
|
2012-10-10 06:35:03 +02:00
|
|
|
when (refill_done && !invalidated) {
|
2015-04-22 20:26:03 +02:00
|
|
|
vb_array := vb_array.bitSet(Cat(repl_way, s1_idx), Bool(true))
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
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
|
|
|
}
|
2016-01-12 21:42:57 +01:00
|
|
|
val s1_disparity = Wire(Vec(nWays, Bool()))
|
2014-08-08 21:23:02 +02:00
|
|
|
for (i <- 0 until nWays)
|
2015-04-22 20:26:03 +02:00
|
|
|
when (s1_valid && s1_disparity(i)) { vb_array := vb_array.bitSet(Cat(UInt(i), s1_idx), Bool(false)) }
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2016-01-12 21:42:57 +01:00
|
|
|
val s1_tag_match = Wire(Vec(nWays, Bool()))
|
|
|
|
val s1_tag_hit = Wire(Vec(nWays, Bool()))
|
|
|
|
val s1_dout = Wire(Vec(nWays, Bits(width = rowBits)))
|
2012-11-25 07:00:43 +01:00
|
|
|
|
2014-08-08 21:23:02 +02:00
|
|
|
for (i <- 0 until nWays) {
|
2015-01-05 01:40:16 +01:00
|
|
|
val s1_vb = !io.invalidate && vb_array(Cat(UInt(i), s1_pgoff(untagBits-1,blockOffBits))).toBool
|
2015-09-12 00:43:07 +02:00
|
|
|
val tag_out = tag_rdata(i)
|
2015-04-22 20:26:03 +02:00
|
|
|
val s1_tag_disparity = code.decode(tag_out).error
|
2012-10-10 06:35:03 +02:00
|
|
|
when (s1_valid && rdy && !stall) {
|
|
|
|
}
|
2014-08-12 03:36:23 +02:00
|
|
|
s1_tag_match(i) := tag_out(tagBits-1,0) === s1_tag
|
2015-04-22 20:26:03 +02:00
|
|
|
s1_tag_hit(i) := s1_vb && s1_tag_match(i)
|
|
|
|
s1_disparity(i) := s1_vb && (s1_tag_disparity || code.decode(s1_dout(i)).error)
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
2015-04-22 20:26:03 +02:00
|
|
|
s1_any_tag_hit := s1_tag_hit.reduceLeft(_||_) && !s1_disparity.reduceLeft(_||_)
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2014-08-08 21:23:02 +02:00
|
|
|
for (i <- 0 until nWays) {
|
2016-01-12 21:42:57 +01:00
|
|
|
val data_array = SeqMem(nSets * refillCycles, Bits(width = code.width(rowBits)))
|
2015-07-11 22:32:45 +02:00
|
|
|
val wen = narrow_grant.valid && repl_way === UInt(i)
|
|
|
|
when (wen) {
|
|
|
|
val e_d = code.encode(narrow_grant.bits.data).toUInt
|
2015-08-06 17:03:10 +02:00
|
|
|
if(refillCycles > 1) data_array.write(Cat(s1_idx, refill_cnt), e_d)
|
|
|
|
else data_array.write(s1_idx, e_d)
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
2015-07-11 22:32:45 +02:00
|
|
|
val s0_raddr = s0_pgoff(untagBits-1,blockOffBits-(if(refillCycles > 1) refill_cnt.getWidth else 0))
|
|
|
|
val s1_rdata = data_array.read(s0_raddr, !wen && s0_valid)
|
2012-11-05 01:39:25 +01:00
|
|
|
// if s1_tag_match is critical, replace with partial tag check
|
2015-04-22 20:26:03 +02:00
|
|
|
s1_dout(i) := 0
|
2015-08-06 17:03:10 +02:00
|
|
|
when (s1_valid && rdy && !stall && (Bool(isDM) || s1_tag_match(i))) { s1_dout(i) := s1_rdata }
|
2012-01-25 01:51:30 +01:00
|
|
|
}
|
2011-12-04 04:41:15 +01:00
|
|
|
|
2011-11-07 09:58:25 +01:00
|
|
|
// output signals
|
2015-12-03 02:17:49 +01:00
|
|
|
if (p(ICacheBufferWays)) {
|
|
|
|
val s2_hit = RegEnable(s1_hit, !stall)
|
|
|
|
val s2_tag_hit = RegEnable(s1_tag_hit, !stall)
|
|
|
|
val s2_dout = RegEnable(s1_dout, !stall)
|
|
|
|
io.resp.bits.datablock := Mux1H(s2_tag_hit, s2_dout)
|
|
|
|
io.resp.valid := s2_hit
|
|
|
|
} else {
|
|
|
|
io.resp.bits.datablock := Mux1H(s1_tag_hit, s1_dout)
|
|
|
|
io.resp.valid := s1_hit
|
|
|
|
}
|
2015-03-27 07:26:03 +01:00
|
|
|
io.mem.acquire.valid := (state === s_request)
|
2015-10-18 22:09:17 +02:00
|
|
|
io.mem.acquire.bits := GetBlock(addr_block = refill_addr >> blockOffBits)
|
2011-10-26 08:02:47 +02:00
|
|
|
|
|
|
|
// control state machine
|
|
|
|
switch (state) {
|
|
|
|
is (s_ready) {
|
2015-04-22 20:26:03 +02:00
|
|
|
when (s1_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) {
|
2015-03-27 07:26:03 +01:00
|
|
|
when (io.mem.acquire.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
|
|
|
}
|