2021-06-04 15:59:14 +00:00
///////////////////////////////////////////
// mmu.sv
//
// Written: david_harris@hmc.edu and kmacsaigoren@hmc.edu 4 June 2021
// Modified:
//
// Purpose: Memory management unit, including TLB, PMA, PMP
//
// A component of the Wally configurable RISC-V project.
//
// Copyright (C) 2021 Harvey Mudd College & Oklahoma State University
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
// BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
// OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
///////////////////////////////////////////
`include " wally-config.vh "
2021-07-04 21:52:00 +00:00
module mmu # ( parameter TLB_ENTRIES = 8 , // nuber of TLB Entries
2021-06-04 21:05:07 +00:00
parameter IMMU = 0 ) (
input logic clk , reset ,
2021-06-04 15:59:14 +00:00
// Current value of satp CSR (from privileged unit)
2021-06-04 21:05:07 +00:00
input logic [ `XLEN - 1 : 0 ] SATP_REGW ,
2021-07-04 17:20:29 +00:00
input logic STATUS_MXR , STATUS_SUM , STATUS_MPRV ,
input logic [ 1 : 0 ] STATUS_MPP ,
2021-06-04 15:59:14 +00:00
// Current privilege level of the processeor
2021-06-04 21:05:07 +00:00
input logic [ 1 : 0 ] PrivilegeModeW ,
2021-06-04 15:59:14 +00:00
// 00 - TLB is not being accessed
// 1x - TLB is accessed for a read (or an instruction)
// x1 - TLB is accessed for a write
// 11 - TLB is accessed for both read and write
2021-06-23 21:43:22 +00:00
input logic DisableTranslation ,
2021-06-04 15:59:14 +00:00
2021-07-19 15:33:27 +00:00
// VAdr goes to the TLB only. Virtual if the TLB is active.
// PAdr goes to address mux bypassing the TLB. PAdr used when there is no translation.
// Comes from either the program address (instruction address or load/store address)
// or from the hardware pagetable walker.
// PAdr is intended to used as a phsycial address. Discarded by the address mux when translation is
// performed.
// PhysicalAddress is selected to be PAdr when no translation or the translated VAdr (TLBPAdr)
// when there is translation.
input logic [ `PA_BITS - 1 : 0 ] PAdr , // *** consider renaming this.
input logic [ `XLEN - 1 : 0 ] VAdr ,
2021-06-21 05:27:02 +00:00
input logic [ 1 : 0 ] Size , // 00 = 8 bits, 01 = 16 bits, 10 = 32 bits , 11 = 64 bits
2021-06-04 15:59:14 +00:00
// Controls for writing a new entry to the TLB
2021-07-06 07:25:11 +00:00
input logic [ `XLEN - 1 : 0 ] PTE ,
2021-06-08 17:39:32 +00:00
input logic [ 1 : 0 ] PageTypeWriteVal ,
2021-06-04 21:05:07 +00:00
input logic TLBWrite ,
2021-06-04 15:59:14 +00:00
// Invalidate all TLB entries
2021-06-04 21:05:07 +00:00
input logic TLBFlush ,
2021-06-04 15:59:14 +00:00
// Physical address outputs
2021-06-18 13:11:31 +00:00
output logic [ `PA_BITS - 1 : 0 ] PhysicalAddress ,
2021-06-04 21:05:07 +00:00
output logic TLBMiss ,
output logic TLBHit ,
2021-07-13 22:24:59 +00:00
output logic Cacheable , Idempotent , AtomicAllowed ,
2021-06-04 15:59:14 +00:00
// Faults
2021-06-04 21:05:07 +00:00
output logic TLBPageFault ,
2021-07-06 18:43:53 +00:00
output logic InstrAccessFaultF , LoadAccessFaultM , StoreAccessFaultM ,
2021-06-04 15:59:14 +00:00
2021-06-04 21:05:07 +00:00
// PMA checker signals
2021-06-04 15:59:14 +00:00
input logic AtomicAccessM , ExecuteAccessF , WriteAccessM , ReadAccessM ,
2021-07-04 15:39:59 +00:00
input var logic [ 7 : 0 ] PMPCFG_ARRAY_REGW [ `PMP_ENTRIES - 1 : 0 ] ,
2021-07-02 15:04:13 +00:00
input var logic [ `XLEN - 1 : 0 ] PMPADDR_ARRAY_REGW [ `PMP_ENTRIES - 1 : 0 ] ,
2021-06-04 21:05:07 +00:00
2021-07-06 18:43:53 +00:00
output logic SquashBusAccess // *** send to privileged unit
2021-06-24 23:59:29 +00:00
// output logic [5:0] SelRegions
2021-06-04 15:59:14 +00:00
) ;
2021-07-08 20:52:06 +00:00
logic [ `PA_BITS - 1 : 0 ] TLBPAdr ;
2021-06-04 21:05:07 +00:00
logic PMPSquashBusAccess , PMASquashBusAccess ;
2021-06-04 15:59:14 +00:00
// Translation lookaside buffer
2021-07-06 18:43:53 +00:00
logic PMAInstrAccessFaultF , PMPInstrAccessFaultF ;
logic PMALoadAccessFaultM , PMPLoadAccessFaultM ;
logic PMAStoreAccessFaultM , PMPStoreAccessFaultM ;
2021-07-06 19:29:42 +00:00
logic Translate ;
2021-07-06 18:43:53 +00:00
2021-07-06 00:35:31 +00:00
// only instantiate TLB if Virtual Memory is supported
generate
2021-07-06 07:25:11 +00:00
if ( `MEM_VIRTMEM ) begin
logic ReadAccess , WriteAccess ;
assign ReadAccess = ExecuteAccessF | ReadAccessM ; // execute also acts as a TLB read. Execute and Read are never active for the same MMU, so safe to mix pipestages
assign WriteAccess = WriteAccessM ;
2021-07-08 20:52:06 +00:00
tlb # ( . TLB_ENTRIES ( TLB_ENTRIES ) , . ITLB ( IMMU ) )
tlb ( . SATP_MODE ( SATP_REGW [ `XLEN - 1 : `XLEN - `SVMODE_BITS ] ) ,
2021-07-19 15:33:27 +00:00
. SATP_ASID ( SATP_REGW [ `ASID_BASE + `ASID_BITS - 1 : `ASID_BASE ] ) ,
. VAdr ,
. * ) ;
2021-07-08 20:52:06 +00:00
2021-07-06 07:25:11 +00:00
end else begin // just pass address through as physical
2021-07-06 19:29:42 +00:00
assign Translate = 0 ;
2021-07-06 00:35:31 +00:00
assign TLBMiss = 0 ;
2021-07-06 19:29:42 +00:00
assign TLBHit = 1 ; // *** is this necessary
2021-07-06 00:35:31 +00:00
assign TLBPageFault = 0 ;
end
endgenerate
2021-07-06 19:29:42 +00:00
// If translation is occuring, select translated physical address from TLB
2021-07-19 15:33:27 +00:00
mux2 # ( `PA_BITS ) addressmux ( PAdr , TLBPAdr , Translate , PhysicalAddress ) ;
2021-07-06 00:35:31 +00:00
2021-06-04 15:59:14 +00:00
///////////////////////////////////////////
// Check physical memory accesses
///////////////////////////////////////////
pmachecker pmachecker ( . * ) ;
pmpchecker pmpchecker ( . * ) ;
2021-06-04 21:05:07 +00:00
2021-07-16 17:22:13 +00:00
// If TLB miss and translating we want to not have faults from the PMA and PMP checkers.
2021-07-06 18:43:53 +00:00
assign SquashBusAccess = PMASquashBusAccess | PMPSquashBusAccess ;
2021-07-16 17:22:13 +00:00
assign InstrAccessFaultF = ( PMAInstrAccessFaultF | PMPInstrAccessFaultF ) & ~ ( Translate & ~ TLBHit ) ;
assign LoadAccessFaultM = ( PMALoadAccessFaultM | PMPLoadAccessFaultM ) & ~ ( Translate & ~ TLBHit ) ;
assign StoreAccessFaultM = ( PMAStoreAccessFaultM | PMPStoreAccessFaultM ) & ~ ( Translate & ~ TLBHit ) ;
2021-06-04 15:59:14 +00:00
2021-06-23 21:43:22 +00:00
endmodule