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

217 lines
7.4 KiB
Scala
Raw Normal View History

2014-09-13 03:06:41 +02:00
// See LICENSE for license details.
2012-10-10 06:35:03 +02:00
package rocket
import Chisel._
2015-03-14 10:49:07 +01:00
import Util._
import junctions._
import scala.math._
2012-10-10 06:35:03 +02:00
case object NTLBEntries extends Field[Int]
2015-10-07 03:20:19 +02:00
trait HasTLBParameters extends HasAddrMapParameters {
2015-10-06 06:48:05 +02:00
val entries = p(NTLBEntries)
val camAddrBits = ceil(log(entries)/log(2)).toInt
val camTagBits = asIdBits + vpnBits
}
2015-10-06 06:48:05 +02:00
abstract class TLBModule(implicit val p: Parameters) extends Module
with HasTLBParameters
abstract class TLBBundle(implicit val p: Parameters) extends ParameterizedBundle()(p)
with HasTLBParameters
2015-10-06 06:48:05 +02:00
class CAMIO(implicit p: Parameters) extends TLBBundle()(p) {
val clear = Bool(INPUT)
2015-03-14 10:49:07 +01:00
val clear_mask = Bits(INPUT, entries)
val tag = Bits(INPUT, camTagBits)
val hit = Bool(OUTPUT)
val hits = UInt(OUTPUT, entries)
val valid_bits = Bits(OUTPUT, entries)
2012-10-10 06:35:03 +02:00
val write = Bool(INPUT)
val write_tag = Bits(INPUT, camTagBits)
val write_addr = UInt(INPUT, camAddrBits)
2012-10-10 06:35:03 +02:00
}
2015-10-06 06:48:05 +02:00
class RocketCAM(implicit p: Parameters) extends TLBModule()(p) {
val io = new CAMIO
2015-09-30 23:36:26 +02:00
val cam_tags = Mem(entries, Bits(width = camTagBits))
2012-10-10 06:35:03 +02:00
2013-08-16 00:28:15 +02:00
val vb_array = Reg(init=Bits(0, entries))
2012-10-10 06:35:03 +02:00
when (io.write) {
vb_array := vb_array.bitSet(io.write_addr, Bool(true))
2012-10-10 06:35:03 +02:00
cam_tags(io.write_addr) := io.write_tag
}
when (io.clear) {
2015-03-14 10:49:07 +01:00
vb_array := vb_array & ~io.clear_mask
2012-10-10 06:35:03 +02:00
}
val hits = (0 until entries).map(i => vb_array(i) && cam_tags(i) === io.tag)
io.valid_bits := vb_array
2013-08-12 19:39:11 +02:00
io.hits := Vec(hits).toBits
2012-10-10 06:35:03 +02:00
io.hit := io.hits.orR
}
class PseudoLRU(n: Int)
{
2013-08-12 19:39:11 +02:00
val state = Reg(Bits(width = n))
def access(way: UInt) = {
2012-10-10 06:35:03 +02:00
var next_state = state
2013-08-12 19:39:11 +02:00
var idx = UInt(1,1)
2012-10-10 06:35:03 +02:00
for (i <- log2Up(n)-1 to 0 by -1) {
val bit = way(i)
2013-08-12 19:39:11 +02:00
val mask = (UInt(1,n) << idx)(n-1,0)
next_state = next_state & ~mask | Mux(bit, UInt(0), mask)
2012-10-10 06:35:03 +02:00
//next_state.bitSet(idx, !bit)
idx = Cat(idx, bit)
}
state := next_state
}
def replace = {
2013-08-12 19:39:11 +02:00
var idx = UInt(1,1)
2012-10-10 06:35:03 +02:00
for (i <- 0 until log2Up(n))
idx = Cat(idx, state(idx))
idx(log2Up(n)-1,0)
}
}
2015-10-06 06:48:05 +02:00
class TLBReq(implicit p: Parameters) extends CoreBundle()(p) {
val asid = UInt(width = asIdBits)
val vpn = UInt(width = vpnBits+1)
val passthrough = Bool()
2012-10-10 06:35:03 +02:00
val instruction = Bool()
2015-03-14 10:49:07 +01:00
val store = Bool()
2012-10-10 06:35:03 +02:00
}
2015-10-06 06:48:05 +02:00
class TLBRespNoHitIndex(implicit p: Parameters) extends CoreBundle()(p) {
2012-10-10 06:35:03 +02:00
// lookup responses
val miss = Bool(OUTPUT)
val ppn = UInt(OUTPUT, ppnBits)
2012-10-10 06:35:03 +02:00
val xcpt_ld = Bool(OUTPUT)
val xcpt_st = Bool(OUTPUT)
val xcpt_if = Bool(OUTPUT)
}
2015-10-06 06:48:05 +02:00
class TLBResp(implicit p: Parameters) extends TLBRespNoHitIndex()(p) with HasTLBParameters {
2015-02-04 04:32:08 +01:00
val hit_idx = UInt(OUTPUT, entries)
}
2015-10-06 06:48:05 +02:00
class TLB(implicit p: Parameters) extends TLBModule()(p) {
2012-10-10 06:35:03 +02:00
val io = new Bundle {
2013-08-12 19:39:11 +02:00
val req = Decoupled(new TLBReq).flip
val resp = new TLBResp
2013-01-07 22:38:59 +01:00
val ptw = new TLBPTWIO
2012-10-10 06:35:03 +02:00
}
2013-09-10 19:51:35 +02:00
val s_ready :: s_request :: s_wait :: s_wait_invalidate :: 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 r_refill_tag = Reg(UInt())
val r_refill_waddr = Reg(UInt())
2015-03-14 10:49:07 +01:00
val r_req = Reg(new TLBReq)
2012-10-10 06:35:03 +02:00
val tag_cam = Module(new RocketCAM)
2015-09-30 23:36:26 +02:00
val tag_ram = Mem(entries, io.ptw.resp.bits.pte.ppn)
2012-10-10 06:35:03 +02:00
2013-08-12 19:39:11 +02:00
val lookup_tag = Cat(io.req.bits.asid, io.req.bits.vpn).toUInt
2012-10-10 06:35:03 +02:00
tag_cam.io.tag := lookup_tag
tag_cam.io.write := state === s_wait && io.ptw.resp.valid
tag_cam.io.write_tag := r_refill_tag
tag_cam.io.write_addr := r_refill_waddr
2013-08-12 19:39:11 +02:00
val tag_hit_addr = OHToUInt(tag_cam.io.hits)
2012-10-10 06:35:03 +02:00
// permission bit arrays
val valid_array = Reg(Vec(Bool(), entries)) // PTE is valid (not equivalent to CAM tag valid bit!)
val ur_array = Reg(Vec(Bool(), entries)) // user read permission
val uw_array = Reg(Vec(Bool(), entries)) // user write permission
val ux_array = Reg(Vec(Bool(), entries)) // user execute permission
val sr_array = Reg(Vec(Bool(), entries)) // supervisor read permission
val sw_array = Reg(Vec(Bool(), entries)) // supervisor write permission
val sx_array = Reg(Vec(Bool(), entries)) // supervisor execute permission
val dirty_array = Reg(Vec(Bool(), entries)) // PTE dirty bit
2012-11-16 10:59:38 +01:00
when (io.ptw.resp.valid) {
val pte = io.ptw.resp.bits.pte
tag_ram(r_refill_waddr) := pte.ppn
valid_array(r_refill_waddr) := !io.ptw.resp.bits.error
ur_array(r_refill_waddr) := pte.ur() && !io.ptw.resp.bits.error
uw_array(r_refill_waddr) := pte.uw() && !io.ptw.resp.bits.error
ux_array(r_refill_waddr) := pte.ux() && !io.ptw.resp.bits.error
sr_array(r_refill_waddr) := pte.sr() && !io.ptw.resp.bits.error
sw_array(r_refill_waddr) := pte.sw() && !io.ptw.resp.bits.error
sx_array(r_refill_waddr) := pte.sx() && !io.ptw.resp.bits.error
dirty_array(r_refill_waddr) := pte.d
2012-10-10 06:35:03 +02:00
}
// high if there are any unused (invalid) entries in the TLB
val has_invalid_entry = !tag_cam.io.valid_bits.andR
val invalid_entry = PriorityEncoder(~tag_cam.io.valid_bits)
val plru = new PseudoLRU(entries)
val repl_waddr = Mux(has_invalid_entry, invalid_entry, plru.replace)
2015-03-14 10:49:07 +01:00
2015-05-19 03:23:58 +02:00
val priv = Mux(io.ptw.status.mprv && !io.req.bits.instruction, io.ptw.status.prv1, io.ptw.status.prv)
2015-03-14 10:49:07 +01:00
val priv_s = priv === PRV_S
val priv_uses_vm = priv <= PRV_S
val req_xwr = Cat(!r_req.store, r_req.store, !(r_req.instruction || r_req.store))
val r_array = Mux(priv_s, sr_array.toBits, ur_array.toBits)
val w_array = Mux(priv_s, sw_array.toBits, uw_array.toBits)
val x_array = Mux(priv_s, sx_array.toBits, ux_array.toBits)
2015-03-14 10:49:07 +01:00
val vm_enabled = io.ptw.status.vm(3) && priv_uses_vm && !io.req.bits.passthrough
val bad_va = io.req.bits.vpn(vpnBits) != io.req.bits.vpn(vpnBits-1)
2015-03-14 10:49:07 +01:00
// it's only a store hit if the dirty bit is set
2015-07-31 08:52:42 +02:00
val tag_hits = tag_cam.io.hits & (dirty_array.toBits | ~Mux(io.req.bits.store, w_array, UInt(0)))
2015-03-14 10:49:07 +01:00
val tag_hit = tag_hits.orR
val tlb_hit = vm_enabled && tag_hit
val tlb_miss = vm_enabled && !tag_hit && !bad_va
2012-10-10 06:35:03 +02:00
when (io.req.valid && tlb_hit) {
2013-08-12 19:39:11 +02:00
plru.access(OHToUInt(tag_cam.io.hits))
2012-10-10 06:35:03 +02:00
}
val paddr = Cat(io.resp.ppn, UInt(0, pgIdxBits))
val addr_ok = addrMap.isValid(paddr)
val addr_prot = addrMap.getProt(paddr)
2012-10-10 06:35:03 +02:00
io.req.ready := state === s_ready
io.resp.xcpt_ld := !addr_ok || !addr_prot.r || bad_va || tlb_hit && !(r_array & tag_cam.io.hits).orR
io.resp.xcpt_st := !addr_ok || !addr_prot.w || bad_va || tlb_hit && !(w_array & tag_cam.io.hits).orR
io.resp.xcpt_if := !addr_ok || !addr_prot.x || bad_va || tlb_hit && !(x_array & tag_cam.io.hits).orR
2012-10-10 06:35:03 +02:00
io.resp.miss := tlb_miss
2015-10-06 06:48:05 +02:00
io.resp.ppn := Mux(vm_enabled, Mux1H(tag_cam.io.hits, tag_ram), io.req.bits.vpn(ppnBits-1,0))
2012-10-10 06:35:03 +02:00
io.resp.hit_idx := tag_cam.io.hits
2015-03-14 10:49:07 +01:00
// clear invalid entries on access, or all entries on a TLB flush
tag_cam.io.clear := io.ptw.invalidate || io.req.fire()
tag_cam.io.clear_mask := ~valid_array.toBits | (tag_cam.io.hits & ~tag_hits)
2015-08-01 00:42:10 +02:00
when (io.ptw.invalidate) { tag_cam.io.clear_mask := ~UInt(0, entries) }
2012-10-10 06:35:03 +02:00
io.ptw.req.valid := state === s_request
2015-03-14 10:49:07 +01:00
io.ptw.req.bits.addr := r_refill_tag
io.ptw.req.bits.prv := io.ptw.status.prv
io.ptw.req.bits.store := r_req.store
io.ptw.req.bits.fetch := r_req.instruction
2012-10-10 06:35:03 +02:00
2015-09-27 06:27:36 +02:00
when (io.req.fire() && tlb_miss) {
2012-10-10 06:35:03 +02:00
state := s_request
r_refill_tag := lookup_tag
r_refill_waddr := repl_waddr
2015-03-14 10:49:07 +01:00
r_req := io.req.bits
2012-10-10 06:35:03 +02:00
}
when (state === s_request) {
when (io.ptw.invalidate) {
2012-10-10 06:35:03 +02:00
state := s_ready
}
when (io.ptw.req.ready) {
state := s_wait
when (io.ptw.invalidate) { state := s_wait_invalidate }
2012-10-10 06:35:03 +02:00
}
}
when (state === s_wait && io.ptw.invalidate) {
2012-10-10 06:35:03 +02:00
state := s_wait_invalidate
}
2012-11-16 10:59:38 +01:00
when (io.ptw.resp.valid) {
2012-10-10 06:35:03 +02:00
state := s_ready
}
}