2021-03-04 07:39:08 +00:00
|
|
|
///////////////////////////////////////////
|
2021-03-18 18:35:46 +00:00
|
|
|
// tlb.sv
|
2021-03-04 07:39:08 +00:00
|
|
|
//
|
|
|
|
// Written: jtorrey@hmc.edu 16 February 2021
|
|
|
|
// Modified:
|
|
|
|
//
|
2021-04-13 17:37:24 +00:00
|
|
|
// Purpose: Translation lookaside buffer
|
|
|
|
// Cache of virtural-to-physical address translations
|
2021-03-04 07:39:08 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
///////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* sv32 specs
|
|
|
|
* ----------
|
|
|
|
* Virtual address [31:0] (32 bits)
|
|
|
|
* [________________________________]
|
|
|
|
* |--VPN1--||--VPN0--||----OFF---|
|
|
|
|
* 10 10 12
|
|
|
|
*
|
|
|
|
* Physical address [33:0] (34 bits)
|
|
|
|
* [__________________________________]
|
|
|
|
* |---PPN1---||--PPN0--||----OFF---|
|
|
|
|
* 12 10 12
|
|
|
|
*
|
|
|
|
* Page Table Entry [31:0] (32 bits)
|
|
|
|
* [________________________________]
|
|
|
|
* |---PPN1---||--PPN0--|||DAGUXWRV
|
|
|
|
* 12 10 ^^
|
|
|
|
* RSW(2) -- for OS
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* *** TODO:
|
|
|
|
* - add LRU algorithm (select the write index based on which entry was used
|
|
|
|
* least recently)
|
|
|
|
*/
|
|
|
|
|
2021-04-13 16:27:12 +00:00
|
|
|
`include "wally-config.vh"
|
|
|
|
`include "wally-constants.vh"
|
|
|
|
|
2021-03-04 07:39:08 +00:00
|
|
|
// The TLB will have 2**ENTRY_BITS total entries
|
2021-03-04 08:11:34 +00:00
|
|
|
module tlb #(parameter ENTRY_BITS = 3) (
|
2021-03-04 07:39:08 +00:00
|
|
|
input clk, reset,
|
|
|
|
|
|
|
|
// Current value of satp CSR (from privileged unit)
|
2021-03-05 18:35:24 +00:00
|
|
|
input [`XLEN-1:0] SATP_REGW,
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-03-18 18:35:46 +00:00
|
|
|
// Current privilege level of the processeor
|
|
|
|
input [1:0] PrivilegeModeW,
|
|
|
|
|
2021-03-31 02:19:27 +00:00
|
|
|
// High if the TLB is currently being accessed
|
|
|
|
input TLBAccess,
|
|
|
|
|
2021-03-04 07:39:08 +00:00
|
|
|
// Virtual address input
|
|
|
|
input [`XLEN-1:0] VirtualAddress,
|
|
|
|
|
|
|
|
// Controls for writing a new entry to the TLB
|
|
|
|
input [`XLEN-1:0] PageTableEntryWrite,
|
2021-04-08 06:44:59 +00:00
|
|
|
input [1:0] PageTypeWrite,
|
2021-03-04 07:39:08 +00:00
|
|
|
input TLBWrite,
|
|
|
|
|
|
|
|
// Invalidate all TLB entries
|
|
|
|
input TLBFlush,
|
|
|
|
|
|
|
|
// Physical address outputs
|
|
|
|
output [`XLEN-1:0] PhysicalAddress,
|
|
|
|
output TLBMiss,
|
2021-04-08 06:44:59 +00:00
|
|
|
output TLBHit,
|
|
|
|
|
|
|
|
// Faults
|
|
|
|
output TLBPageFault
|
2021-03-04 07:39:08 +00:00
|
|
|
);
|
|
|
|
|
2021-03-05 18:35:24 +00:00
|
|
|
logic SvMode;
|
2021-03-18 18:35:46 +00:00
|
|
|
logic Translate;
|
2021-03-05 18:35:24 +00:00
|
|
|
|
2021-03-04 07:39:08 +00:00
|
|
|
generate
|
2021-03-05 18:35:24 +00:00
|
|
|
if (`XLEN == 32) begin
|
|
|
|
assign SvMode = SATP_REGW[31]; // *** change to an enum somehow?
|
|
|
|
end else begin
|
|
|
|
assign SvMode = SATP_REGW[63]; // currently just a boolean whether translation enabled
|
2021-03-04 07:39:08 +00:00
|
|
|
end
|
|
|
|
endgenerate
|
2021-03-18 18:35:46 +00:00
|
|
|
|
2021-04-08 07:24:10 +00:00
|
|
|
// Whether translation should occur
|
2021-03-18 18:35:46 +00:00
|
|
|
assign Translate = SvMode & (PrivilegeModeW != `M_MODE);
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-03-05 18:35:24 +00:00
|
|
|
// *** If we want to support multiple virtual memory modes (ie sv39 AND sv48),
|
|
|
|
// we could have some muxes that control which parameters are current.
|
|
|
|
// Although then some of the signals are not big enough. But that's a problem
|
|
|
|
// for much later.
|
|
|
|
|
2021-03-04 07:39:08 +00:00
|
|
|
// Index (currently random) to write the next TLB entry
|
|
|
|
logic [ENTRY_BITS-1:0] WriteIndex;
|
|
|
|
|
|
|
|
// Sections of the virtual and physical addresses
|
2021-03-05 18:35:24 +00:00
|
|
|
logic [`VPN_BITS-1:0] VirtualPageNumber;
|
2021-04-08 06:44:59 +00:00
|
|
|
logic [`PPN_BITS-1:0] PhysicalPageNumber, PhysicalPageNumberMixed;
|
2021-04-08 07:24:10 +00:00
|
|
|
logic [`PA_BITS-1:0] PhysicalAddressFull;
|
|
|
|
|
|
|
|
// Sections of the page table entry
|
2021-04-08 06:44:59 +00:00
|
|
|
logic [7:0] PTEAccessBits;
|
2021-03-05 18:35:24 +00:00
|
|
|
logic [11:0] PageOffset;
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-04-08 06:44:59 +00:00
|
|
|
// Pattern location in the CAM and type of page hit
|
2021-03-04 07:39:08 +00:00
|
|
|
logic [ENTRY_BITS-1:0] VPNIndex;
|
2021-04-08 06:44:59 +00:00
|
|
|
logic [1:0] HitPageType;
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-04-08 07:24:10 +00:00
|
|
|
// Whether the virtual address has a match in the CAM
|
|
|
|
logic CAMHit;
|
2021-03-31 02:19:27 +00:00
|
|
|
|
2021-03-05 18:35:24 +00:00
|
|
|
assign VirtualPageNumber = VirtualAddress[`VPN_BITS+11:12];
|
2021-03-04 07:39:08 +00:00
|
|
|
assign PageOffset = VirtualAddress[11:0];
|
|
|
|
|
|
|
|
// Currently use random replacement algorithm
|
2021-04-13 17:37:24 +00:00
|
|
|
// tlb_rand rdm(.*);
|
|
|
|
tlb_lru lru(.*);
|
2021-03-04 07:39:08 +00:00
|
|
|
|
|
|
|
tlb_ram #(ENTRY_BITS) ram(.*);
|
2021-04-08 06:44:59 +00:00
|
|
|
tlb_cam #(ENTRY_BITS, `VPN_BITS, `VPN_SEGMENT_BITS) cam(.*);
|
|
|
|
|
|
|
|
// *** check whether access is allowed, otherwise fault
|
|
|
|
assign TLBPageFault = 0; // *** temporary
|
|
|
|
|
2021-04-08 07:24:10 +00:00
|
|
|
// *** Not the cleanest solution.
|
|
|
|
// The highest segment of the physical page number has some extra bits
|
|
|
|
// than the highest segment of the virtual page number.
|
2021-04-08 06:44:59 +00:00
|
|
|
localparam EXTRA_PHYSICAL_BITS = `PPN_HIGH_SEGMENT_BITS - `VPN_SEGMENT_BITS;
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-04-08 07:24:10 +00:00
|
|
|
// Replace segments of the virtual page number with segments of the physical
|
|
|
|
// page number. For 4 KB pages, the entire virtual page number is replaced.
|
|
|
|
// For superpages, some segments are considered offsets into a larger page.
|
2021-04-08 06:44:59 +00:00
|
|
|
page_number_mixer #(`PPN_BITS, `PPN_HIGH_SEGMENT_BITS)
|
|
|
|
physical_mixer(PhysicalPageNumber,
|
|
|
|
{{EXTRA_PHYSICAL_BITS{1'b0}}, VirtualPageNumber},
|
|
|
|
HitPageType,
|
|
|
|
PhysicalPageNumberMixed);
|
|
|
|
|
2021-04-08 07:24:10 +00:00
|
|
|
// Provide physical address only on TLBHits to cause catastrophic errors if
|
|
|
|
// garbage address is used.
|
2021-04-08 06:44:59 +00:00
|
|
|
assign PhysicalAddressFull = (TLBHit) ?
|
|
|
|
{PhysicalPageNumberMixed, PageOffset} : '0;
|
2021-03-04 07:39:08 +00:00
|
|
|
|
2021-04-08 07:24:10 +00:00
|
|
|
// Output the hit physical address if translation is currently on.
|
2021-03-04 07:39:08 +00:00
|
|
|
generate
|
|
|
|
if (`XLEN == 32) begin
|
2021-03-18 18:35:46 +00:00
|
|
|
mux2 #(`XLEN) addressmux(VirtualAddress, PhysicalAddressFull[31:0], Translate, PhysicalAddress);
|
2021-03-04 07:39:08 +00:00
|
|
|
end else begin
|
2021-03-18 18:35:46 +00:00
|
|
|
mux2 #(`XLEN) addressmux(VirtualAddress, {8'b0, PhysicalAddressFull}, Translate, PhysicalAddress);
|
2021-03-04 07:39:08 +00:00
|
|
|
end
|
|
|
|
endgenerate
|
|
|
|
|
2021-03-31 02:19:27 +00:00
|
|
|
assign TLBHit = CAMHit & TLBAccess;
|
|
|
|
assign TLBMiss = ~TLBHit & ~TLBFlush & Translate & TLBAccess;
|
2021-03-04 07:39:08 +00:00
|
|
|
endmodule
|