2014-09-13 03:06:41 +02:00
|
|
|
// See LICENSE for license details.
|
|
|
|
|
2012-10-10 06:35:03 +02:00
|
|
|
package rocket
|
|
|
|
|
2013-07-24 05:26:17 +02:00
|
|
|
import Chisel._
|
2015-03-14 10:49:07 +01:00
|
|
|
import Util._
|
2015-07-30 02:22:22 +02:00
|
|
|
import junctions._
|
2013-07-24 05:26:17 +02:00
|
|
|
import scala.math._
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2015-02-02 05:04:13 +01:00
|
|
|
case object NTLBEntries extends Field[Int]
|
|
|
|
|
|
|
|
abstract trait TLBParameters extends CoreParameters {
|
|
|
|
val entries = params(NTLBEntries)
|
|
|
|
val camAddrBits = ceil(log(entries)/log(2)).toInt
|
|
|
|
val camTagBits = asIdBits + vpnBits
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract class TLBBundle extends Bundle with TLBParameters
|
|
|
|
abstract class TLBModule extends Module with TLBParameters
|
|
|
|
|
|
|
|
class CAMIO extends TLBBundle {
|
2014-01-14 06:43:56 +01:00
|
|
|
val clear = Bool(INPUT)
|
2015-03-14 10:49:07 +01:00
|
|
|
val clear_mask = Bits(INPUT, entries)
|
2015-02-02 05:04:13 +01:00
|
|
|
val tag = Bits(INPUT, camTagBits)
|
2014-01-14 06:43:56 +01:00
|
|
|
val hit = Bool(OUTPUT)
|
|
|
|
val hits = UInt(OUTPUT, entries)
|
|
|
|
val valid_bits = Bits(OUTPUT, entries)
|
2012-10-10 06:35:03 +02:00
|
|
|
|
2014-01-14 06:43:56 +01:00
|
|
|
val write = Bool(INPUT)
|
2015-02-02 05:04:13 +01:00
|
|
|
val write_tag = Bits(INPUT, camTagBits)
|
|
|
|
val write_addr = UInt(INPUT, camAddrBits)
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
|
|
|
|
2015-02-02 05:04:13 +01:00
|
|
|
class RocketCAM extends TLBModule {
|
|
|
|
val io = new CAMIO
|
|
|
|
val cam_tags = Mem(Bits(width = camTagBits), entries)
|
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) {
|
2014-01-14 06:43:56 +01:00
|
|
|
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)
|
|
|
|
|
2014-01-14 06:43:56 +01:00
|
|
|
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-02-04 04:32:08 +01:00
|
|
|
class TLBReq extends CoreBundle {
|
2015-02-02 05:04:13 +01:00
|
|
|
val asid = UInt(width = asIdBits)
|
|
|
|
val vpn = UInt(width = vpnBits+1)
|
2012-11-06 17:13:44 +01:00
|
|
|
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-02-04 04:32:08 +01:00
|
|
|
class TLBRespNoHitIndex extends CoreBundle {
|
2012-10-10 06:35:03 +02:00
|
|
|
// lookup responses
|
|
|
|
val miss = Bool(OUTPUT)
|
2015-02-02 05:04:13 +01:00
|
|
|
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-02-04 04:32:08 +01:00
|
|
|
class TLBResp extends TLBRespNoHitIndex with TLBParameters {
|
|
|
|
val hit_idx = UInt(OUTPUT, entries)
|
|
|
|
}
|
|
|
|
|
2015-02-02 05:04:13 +01:00
|
|
|
class TLB extends TLBModule {
|
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
|
2015-02-02 05:04:13 +01:00
|
|
|
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
|
|
|
|
2015-02-02 05:04:13 +01:00
|
|
|
val tag_cam = Module(new RocketCAM)
|
2015-07-16 02:30:50 +02:00
|
|
|
val tag_ram = Mem(io.ptw.resp.bits.pte.ppn, entries)
|
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
|
2015-07-30 00:03:13 +02:00
|
|
|
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) {
|
2015-03-28 00:20:59 +01:00
|
|
|
val pte = io.ptw.resp.bits.pte
|
|
|
|
tag_ram(r_refill_waddr) := pte.ppn
|
2015-07-30 00:03:13 +02:00
|
|
|
valid_array(r_refill_waddr) := !io.ptw.resp.bits.error
|
2015-03-28 00:20:59 +01:00
|
|
|
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))
|
|
|
|
|
2015-07-30 00:03:13 +02:00
|
|
|
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
|
|
|
|
2015-09-27 05:29:51 +02:00
|
|
|
val vm_enabled = io.ptw.status.vm(3) && priv_uses_vm && !io.req.bits.passthrough
|
2015-02-02 05:04:13 +01:00
|
|
|
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
|
2015-09-22 18:42:27 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-09-22 18:42:27 +02:00
|
|
|
val addrMap = params(NASTIAddrHashMap)
|
|
|
|
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
|
2015-09-22 18:42:27 +02:00
|
|
|
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-09-27 05:29:51 +02:00
|
|
|
io.resp.ppn := Mux(vm_enabled, Mux1H(tag_cam.io.hits, tag_ram), io.req.bits.vpn(params(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()
|
2015-07-30 00:03:13 +02:00
|
|
|
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
|
2015-03-28 00:20:59 +01:00
|
|
|
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) {
|
2012-11-06 17:13:44 +01:00
|
|
|
when (io.ptw.invalidate) {
|
2012-10-10 06:35:03 +02:00
|
|
|
state := s_ready
|
|
|
|
}
|
|
|
|
when (io.ptw.req.ready) {
|
|
|
|
state := s_wait
|
2012-11-06 17:13:44 +01:00
|
|
|
when (io.ptw.invalidate) { state := s_wait_invalidate }
|
2012-10-10 06:35:03 +02:00
|
|
|
}
|
|
|
|
}
|
2012-11-06 17:13:44 +01: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
|
|
|
|
}
|
|
|
|
}
|