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

206 lines
6.8 KiB
Scala
Raw Normal View History

package rocket
2011-11-09 23:52:17 +01:00
import Chisel._;
import Node._;
import Constants._;
import scala.math._;
2012-02-27 02:37:56 +01:00
class ioDmemArbiter(n: Int) extends Bundle
2011-11-09 23:52:17 +01:00
{
2012-03-02 05:48:46 +01:00
val dmem = new ioDmem().flip
2012-02-27 02:37:56 +01:00
val requestor = Vec(n) { new ioDmem() }
2011-11-09 23:52:17 +01:00
}
2012-02-27 02:37:56 +01:00
class rocketDmemArbiter(n: Int) extends Component
2011-11-09 23:52:17 +01:00
{
2012-02-27 02:37:56 +01:00
val io = new ioDmemArbiter(n)
require(DCACHE_TAG_BITS >= log2up(n) + CPU_TAG_BITS)
2011-11-09 23:52:17 +01:00
2012-02-27 02:37:56 +01:00
var req_val = Bool(false)
var req_rdy = io.dmem.req_rdy
for (i <- 0 until n)
{
io.requestor(i).req_rdy := req_rdy
req_val = req_val || io.requestor(i).req_val
req_rdy = req_rdy && !io.requestor(i).req_val
}
2012-02-27 02:37:56 +01:00
var req_cmd = io.requestor(n-1).req_cmd
var req_type = io.requestor(n-1).req_type
var req_idx = io.requestor(n-1).req_idx
var req_ppn = io.requestor(n-1).req_ppn
var req_data = io.requestor(n-1).req_data
var req_tag = io.requestor(n-1).req_tag
var req_kill = io.requestor(n-1).req_kill
for (i <- n-1 to 0 by -1)
{
req_cmd = Mux(io.requestor(i).req_val, io.requestor(i).req_cmd, req_cmd)
req_type = Mux(io.requestor(i).req_val, io.requestor(i).req_type, req_type)
req_idx = Mux(io.requestor(i).req_val, io.requestor(i).req_idx, req_idx)
req_ppn = Mux(Reg(io.requestor(i).req_val), io.requestor(i).req_ppn, req_ppn)
req_data = Mux(Reg(io.requestor(i).req_val), io.requestor(i).req_data, req_data)
req_tag = Mux(io.requestor(i).req_val, Cat(io.requestor(i).req_tag, UFix(i, log2up(n))), req_tag)
req_kill = Mux(Reg(io.requestor(i).req_val), io.requestor(i).req_kill, req_kill)
}
2011-11-09 23:52:17 +01:00
2012-02-27 02:37:56 +01:00
io.dmem.req_val := req_val
io.dmem.req_cmd := req_cmd
io.dmem.req_type := req_type
io.dmem.req_idx := req_idx
io.dmem.req_ppn := req_ppn
io.dmem.req_data := req_data
io.dmem.req_tag := req_tag
io.dmem.req_kill := req_kill
2012-02-27 02:37:56 +01:00
for (i <- 0 until n)
{
val tag_hit = io.dmem.resp_tag(log2up(n)-1,0) === UFix(i)
io.requestor(i).xcpt_ma_ld := io.dmem.xcpt_ma_ld && Reg(io.requestor(i).req_val)
io.requestor(i).xcpt_ma_st := io.dmem.xcpt_ma_st && Reg(io.requestor(i).req_val)
2012-02-27 02:37:56 +01:00
io.requestor(i).resp_nack := io.dmem.resp_nack && Reg(io.requestor(i).req_val)
io.requestor(i).resp_miss := io.dmem.resp_miss && tag_hit
2012-02-27 02:37:56 +01:00
io.requestor(i).resp_val := io.dmem.resp_val && tag_hit
io.requestor(i).resp_replay := io.dmem.resp_replay && tag_hit
io.requestor(i).resp_data := io.dmem.resp_data
2012-02-27 03:26:29 +01:00
io.requestor(i).resp_data_subword := io.dmem.resp_data_subword
io.requestor(i).resp_type := io.dmem.resp_type
2012-02-27 02:37:56 +01:00
io.requestor(i).resp_tag := io.dmem.resp_tag >> UFix(log2up(n))
}
2011-11-09 23:52:17 +01:00
}
class ioPTW extends Bundle
{
2012-03-02 05:48:46 +01:00
val itlb = new ioTLB_PTW().flip
val dtlb = new ioTLB_PTW().flip
2012-03-18 07:00:51 +01:00
val vitlb = new ioTLB_PTW().flip
2012-03-02 05:48:46 +01:00
val dmem = new ioDmem().flip
val ptbr = UFix(PADDR_BITS, INPUT);
2011-11-09 23:52:17 +01:00
}
class rocketPTW extends Component
{
val io = new ioPTW();
val levels = 3
val bitsPerLevel = VPN_BITS/levels
require(VPN_BITS == levels * bitsPerLevel)
val count = Reg() { UFix(width = log2up(levels)) }
val s_ready :: s_req :: s_wait :: s_done :: s_error :: Nil = Enum(5) { UFix() };
2011-11-09 23:52:17 +01:00
val state = Reg(resetVal = s_ready);
2012-01-02 01:09:40 +01:00
val r_req_vpn = Reg() { Bits() }
2012-03-18 07:00:51 +01:00
val r_req_dest = Reg() { Bits() }
2011-11-09 23:52:17 +01:00
val req_addr = Reg() { Bits() }
2012-01-02 01:09:40 +01:00
val r_resp_ppn = Reg() { Bits() };
val r_resp_perm = Reg() { Bits() };
2011-11-09 23:52:17 +01:00
val vpn_idxs = (1 until levels).map(i => r_req_vpn((levels-i)*bitsPerLevel-1, (levels-i-1)*bitsPerLevel))
val vpn_idx = (2 until levels).foldRight(vpn_idxs(0))((i,j) => Mux(count === UFix(i-1), vpn_idxs(i-1), j))
2012-03-18 07:00:51 +01:00
val req_val = io.itlb.req_val || io.dtlb.req_val || io.vitlb.req_val
2011-11-09 23:52:17 +01:00
2011-11-10 09:23:29 +01:00
// give ITLB requests priority over DTLB requests
val req_itlb_val = io.itlb.req_val;
val req_dtlb_val = io.dtlb.req_val && !io.itlb.req_val;
2012-03-18 07:00:51 +01:00
val req_vitlb_val = io.vitlb.req_val && !io.itlb.req_val && !io.dtlb.req_val
2011-11-10 09:23:29 +01:00
2012-03-18 07:00:51 +01:00
when ((state === s_ready) && req_itlb_val) {
r_req_vpn := io.itlb.req_vpn;
r_req_dest := Bits(0)
req_addr := Cat(io.ptbr(PADDR_BITS-1,PGIDX_BITS), io.itlb.req_vpn(VPN_BITS-1,VPN_BITS-bitsPerLevel), Bits(0,3))
2012-03-18 07:00:51 +01:00
}
2012-02-12 02:20:33 +01:00
when ((state === s_ready) && req_dtlb_val) {
r_req_vpn := io.dtlb.req_vpn;
2012-03-18 07:00:51 +01:00
r_req_dest := Bits(1)
req_addr := Cat(io.ptbr(PADDR_BITS-1,PGIDX_BITS), io.dtlb.req_vpn(VPN_BITS-1,VPN_BITS-bitsPerLevel), Bits(0,3))
2011-11-09 23:52:17 +01:00
}
2011-11-10 09:23:29 +01:00
2012-03-18 07:00:51 +01:00
when ((state === s_ready) && req_vitlb_val) {
r_req_vpn := io.vitlb.req_vpn;
r_req_dest := Bits(2)
req_addr := Cat(io.ptbr(PADDR_BITS-1,PGIDX_BITS), io.vitlb.req_vpn(VPN_BITS-1,VPN_BITS-bitsPerLevel), Bits(0,3))
2011-11-10 09:23:29 +01:00
}
val dmem_resp_val = Reg(io.dmem.resp_val, resetVal = Bool(false))
when (dmem_resp_val) {
req_addr := Cat(io.dmem.resp_data_subword(PADDR_BITS-1, PGIDX_BITS), vpn_idx, Bits(0,3))
r_resp_perm := io.dmem.resp_data_subword(9,4);
r_resp_ppn := io.dmem.resp_data_subword(PADDR_BITS-1, PGIDX_BITS);
2011-11-09 23:52:17 +01:00
}
io.dmem.req_val := state === s_req
2011-12-17 12:26:11 +01:00
io.dmem.req_cmd := M_XRD;
2011-11-09 23:52:17 +01:00
io.dmem.req_type := MT_D;
2011-11-12 03:18:47 +01:00
io.dmem.req_idx := req_addr(PGIDX_BITS-1,0);
2012-02-27 02:37:56 +01:00
io.dmem.req_ppn := Reg(req_addr(PADDR_BITS-1,PGIDX_BITS))
io.dmem.req_kill := Bool(false)
val resp_val = state === s_done
val resp_err = state === s_error
2011-11-10 09:23:29 +01:00
val resp_ptd = io.dmem.resp_data_subword(1,0) === Bits(1)
val resp_pte = io.dmem.resp_data_subword(1,0) === Bits(2)
2011-11-10 09:23:29 +01:00
2012-03-18 07:00:51 +01:00
io.itlb.req_rdy := (state === s_ready)
io.dtlb.req_rdy := (state === s_ready) && !io.itlb.req_val
io.vitlb.req_rdy := (state === s_ready) && !io.itlb.req_val && !io.dtlb.req_val
io.itlb.resp_val := r_req_dest === Bits(0) && resp_val
io.dtlb.resp_val := r_req_dest === Bits(1) && resp_val
io.vitlb.resp_val := r_req_dest === Bits(2) && resp_val
io.itlb.resp_err := r_req_dest === Bits(0) && resp_err
io.dtlb.resp_err := r_req_dest === Bits(1) && resp_err
io.vitlb.resp_err := r_req_dest === Bits(2) && resp_err
io.itlb.resp_perm := r_resp_perm
io.dtlb.resp_perm := r_resp_perm
io.vitlb.resp_perm:= r_resp_perm
val resp_ppns = (0 until levels-1).map(i => Cat(r_resp_ppn(PPN_BITS-1, VPN_BITS-bitsPerLevel*(i+1)), r_req_vpn(VPN_BITS-1-bitsPerLevel*(i+1), 0)))
val resp_ppn = (0 until levels-1).foldRight(r_resp_ppn)((i,j) => Mux(count === UFix(i), resp_ppns(i), j))
2011-11-10 09:23:29 +01:00
io.itlb.resp_ppn := resp_ppn;
2012-03-18 07:00:51 +01:00
io.dtlb.resp_ppn := resp_ppn;
io.vitlb.resp_ppn := resp_ppn;
2011-11-09 23:52:17 +01:00
// control state machine
switch (state) {
is (s_ready) {
2011-11-10 09:23:29 +01:00
when (req_val) {
state := s_req;
}
count := UFix(0)
2011-11-09 23:52:17 +01:00
}
is (s_req) {
2011-11-09 23:52:17 +01:00
when (io.dmem.req_rdy) {
state := s_wait;
2011-11-09 23:52:17 +01:00
}
}
is (s_wait) {
when (io.dmem.resp_nack) {
state := s_req
}
when (dmem_resp_val) {
2011-11-09 23:52:17 +01:00
when (resp_pte) { // page table entry
state := s_done
2011-11-09 23:52:17 +01:00
}
2012-02-12 02:20:33 +01:00
.otherwise {
count := count + UFix(1)
when (resp_ptd && count < UFix(levels-1)) {
state := s_req
}
.otherwise {
state := s_error
}
2011-11-09 23:52:17 +01:00
}
}
}
2011-11-09 23:52:17 +01:00
is (s_done) {
2012-02-12 02:20:33 +01:00
state := s_ready;
2011-11-09 23:52:17 +01:00
}
is (s_error) {
2012-02-12 02:20:33 +01:00
state := s_ready;
2011-11-09 23:52:17 +01:00
}
}
}