Line 3... |
Line 3... |
-- # ********************************************************************************************* #
|
-- # ********************************************************************************************* #
|
-- # Instruction and data bus interfaces and physical memory protection (PMP). #
|
-- # Instruction and data bus interfaces and physical memory protection (PMP). #
|
-- # ********************************************************************************************* #
|
-- # ********************************************************************************************* #
|
-- # BSD 3-Clause License #
|
-- # BSD 3-Clause License #
|
-- # #
|
-- # #
|
-- # Copyright (c) 2020, Stephan Nolting. All rights reserved. #
|
-- # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
|
-- # #
|
-- # #
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
-- # Redistribution and use in source and binary forms, with or without modification, are #
|
-- # permitted provided that the following conditions are met: #
|
-- # permitted provided that the following conditions are met: #
|
-- # #
|
-- # #
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
-- # 1. Redistributions of source code must retain the above copyright notice, this list of #
|
Line 43... |
Line 43... |
|
|
entity neorv32_cpu_bus is
|
entity neorv32_cpu_bus is
|
generic (
|
generic (
|
CPU_EXTENSION_RISCV_C : boolean := true; -- implement compressed extension?
|
CPU_EXTENSION_RISCV_C : boolean := true; -- implement compressed extension?
|
-- Physical memory protection (PMP) --
|
-- Physical memory protection (PMP) --
|
PMP_USE : boolean := false; -- implement physical memory protection?
|
PMP_NUM_REGIONS : natural := 0; -- number of regions (0..64)
|
|
PMP_MIN_GRANULARITY : natural := 64*1024; -- minimal region granularity in bytes, has to be a power of 2, min 8 bytes
|
-- Bus Timeout --
|
-- Bus Timeout --
|
BUS_TIMEOUT : natural := 63 -- cycles after an UNACKNOWLEDGED bus access triggers a bus fault exception
|
BUS_TIMEOUT : natural := 63 -- cycles after an UNACKNOWLEDGED bus access triggers a bus fault exception
|
);
|
);
|
port (
|
port (
|
-- global control --
|
-- global control --
|
Line 109... |
Line 110... |
--constant pmp_tor_mode_c : std_ulogic_vector(1 downto 0) := "01"; -- top of range
|
--constant pmp_tor_mode_c : std_ulogic_vector(1 downto 0) := "01"; -- top of range
|
--constant pmp_na4_mode_c : std_ulogic_vector(1 downto 0) := "10"; -- naturally aligned four-byte region
|
--constant pmp_na4_mode_c : std_ulogic_vector(1 downto 0) := "10"; -- naturally aligned four-byte region
|
constant pmp_napot_mode_c : std_ulogic_vector(1 downto 0) := "11"; -- naturally aligned power-of-two region (>= 8 bytes)
|
constant pmp_napot_mode_c : std_ulogic_vector(1 downto 0) := "11"; -- naturally aligned power-of-two region (>= 8 bytes)
|
|
|
-- PMP granularity --
|
-- PMP granularity --
|
constant pmp_g_c : natural := index_size_f(pmp_min_granularity_c);
|
constant pmp_g_c : natural := index_size_f(PMP_MIN_GRANULARITY);
|
|
|
-- PMP configuration register bits --
|
-- PMP configuration register bits --
|
constant pmp_cfg_r_c : natural := 0; -- read permit
|
constant pmp_cfg_r_c : natural := 0; -- read permit
|
constant pmp_cfg_w_c : natural := 1; -- write permit
|
constant pmp_cfg_w_c : natural := 1; -- write permit
|
constant pmp_cfg_x_c : natural := 2; -- execute permit
|
constant pmp_cfg_x_c : natural := 2; -- execute permit
|
Line 141... |
Line 142... |
timeout : std_ulogic_vector(index_size_f(BUS_TIMEOUT)-1 downto 0);
|
timeout : std_ulogic_vector(index_size_f(BUS_TIMEOUT)-1 downto 0);
|
end record;
|
end record;
|
signal i_arbiter, d_arbiter : bus_arbiter_t;
|
signal i_arbiter, d_arbiter : bus_arbiter_t;
|
|
|
-- physical memory protection --
|
-- physical memory protection --
|
type pmp_addr_t is array (0 to pmp_num_regions_c-1) of std_ulogic_vector(data_width_c-1 downto 0);
|
type pmp_addr_t is array (0 to PMP_NUM_REGIONS-1) of std_ulogic_vector(data_width_c-1 downto 0);
|
type pmp_t is record
|
type pmp_t is record
|
addr_mask : pmp_addr_t;
|
addr_mask : pmp_addr_t;
|
region_base : pmp_addr_t; -- region config base address
|
region_base : pmp_addr_t; -- region config base address
|
region_i_addr : pmp_addr_t; -- masked instruction access base address for comparator
|
region_i_addr : pmp_addr_t; -- masked instruction access base address for comparator
|
region_d_addr : pmp_addr_t; -- masked data access base address for comparator
|
region_d_addr : pmp_addr_t; -- masked data access base address for comparator
|
i_match : std_ulogic_vector(pmp_num_regions_c-1 downto 0); -- region match for instruction interface
|
i_match : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region match for instruction interface
|
d_match : std_ulogic_vector(pmp_num_regions_c-1 downto 0); -- region match for data interface
|
d_match : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region match for data interface
|
if_fault : std_ulogic_vector(pmp_num_regions_c-1 downto 0); -- region access fault for fetch operation
|
if_fault : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region access fault for fetch operation
|
ld_fault : std_ulogic_vector(pmp_num_regions_c-1 downto 0); -- region access fault for load operation
|
ld_fault : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region access fault for load operation
|
st_fault : std_ulogic_vector(pmp_num_regions_c-1 downto 0); -- region access fault for store operation
|
st_fault : std_ulogic_vector(PMP_NUM_REGIONS-1 downto 0); -- region access fault for store operation
|
end record;
|
end record;
|
signal pmp : pmp_t;
|
signal pmp : pmp_t;
|
|
|
-- pmp faults anybody? --
|
-- pmp faults anybody? --
|
signal if_pmp_fault : std_ulogic; -- pmp instruction access fault
|
signal if_pmp_fault : std_ulogic; -- pmp instruction access fault
|
Line 389... |
Line 390... |
-- -------------------------------------------------------------------------------------------
|
-- -------------------------------------------------------------------------------------------
|
-- compute address masks (ITERATIVE!!!) --
|
-- compute address masks (ITERATIVE!!!) --
|
pmp_masks: process(clk_i)
|
pmp_masks: process(clk_i)
|
begin
|
begin
|
if rising_edge(clk_i) then -- address mask computation (not the actual address check!) has a latency of max +32 cycles
|
if rising_edge(clk_i) then -- address mask computation (not the actual address check!) has a latency of max +32 cycles
|
for r in 0 to pmp_num_regions_c-1 loop -- iterate over all regions
|
for r in 0 to PMP_NUM_REGIONS-1 loop -- iterate over all regions
|
pmp.addr_mask(r) <= (others => '0');
|
pmp.addr_mask(r) <= (others => '0');
|
for i in pmp_g_c to data_width_c-1 loop
|
for i in pmp_g_c to data_width_c-1 loop
|
pmp.addr_mask(r)(i) <= pmp.addr_mask(r)(i-1) or (not pmp_addr_i(r)(i-1));
|
pmp.addr_mask(r)(i) <= pmp.addr_mask(r)(i-1) or (not pmp_addr_i(r)(i-1));
|
end loop; -- i
|
end loop; -- i
|
end loop; -- r
|
end loop; -- r
|
Line 401... |
Line 402... |
end process pmp_masks;
|
end process pmp_masks;
|
|
|
|
|
-- address access check --
|
-- address access check --
|
pmp_address_check:
|
pmp_address_check:
|
for r in 0 to pmp_num_regions_c-1 generate -- iterate over all regions
|
for r in 0 to PMP_NUM_REGIONS-1 generate -- iterate over all regions
|
pmp.region_i_addr(r) <= fetch_pc_i and pmp.addr_mask(r);
|
pmp.region_i_addr(r) <= fetch_pc_i and pmp.addr_mask(r);
|
pmp.region_d_addr(r) <= mar and pmp.addr_mask(r);
|
pmp.region_d_addr(r) <= mar and pmp.addr_mask(r);
|
pmp.region_base(r) <= pmp_addr_i(r)(data_width_c+1 downto 2) and pmp.addr_mask(r);
|
pmp.region_base(r) <= pmp_addr_i(r)(data_width_c+1 downto 2) and pmp.addr_mask(r);
|
--
|
--
|
pmp.i_match(r) <= '1' when (pmp.region_i_addr(r)(data_width_c-1 downto pmp_g_c) = pmp.region_base(r)(data_width_c-1 downto pmp_g_c)) else '0';
|
pmp.i_match(r) <= '1' when (pmp.region_i_addr(r)(data_width_c-1 downto pmp_g_c) = pmp.region_base(r)(data_width_c-1 downto pmp_g_c)) else '0';
|
Line 414... |
Line 415... |
|
|
|
|
-- check access type and regions's permissions --
|
-- check access type and regions's permissions --
|
pmp_check_permission: process(pmp, pmp_ctrl_i, ctrl_i)
|
pmp_check_permission: process(pmp, pmp_ctrl_i, ctrl_i)
|
begin
|
begin
|
for r in 0 to pmp_num_regions_c-1 loop -- iterate over all regions
|
for r in 0 to PMP_NUM_REGIONS-1 loop -- iterate over all regions
|
if ((ctrl_i(ctrl_priv_lvl_msb_c downto ctrl_priv_lvl_lsb_c) = priv_mode_u_c) or (pmp_ctrl_i(r)(pmp_cfg_l_c) = '1')) and -- user privilege level or locked pmp entry -> enforce permissions also for machine mode
|
if ((ctrl_i(ctrl_priv_lvl_msb_c downto ctrl_priv_lvl_lsb_c) = priv_mode_u_c) or (pmp_ctrl_i(r)(pmp_cfg_l_c) = '1')) and -- user privilege level or locked pmp entry -> enforce permissions also for machine mode
|
(pmp_ctrl_i(r)(pmp_cfg_ah_c downto pmp_cfg_al_c) /= pmp_off_mode_c) then -- active entry
|
(pmp_ctrl_i(r)(pmp_cfg_ah_c downto pmp_cfg_al_c) /= pmp_off_mode_c) then -- active entry
|
pmp.if_fault(r) <= pmp.i_match(r) and (not pmp_ctrl_i(r)(pmp_cfg_x_c)); -- fetch access match no execute permission
|
pmp.if_fault(r) <= pmp.i_match(r) and (not pmp_ctrl_i(r)(pmp_cfg_x_c)); -- fetch access match no execute permission
|
pmp.ld_fault(r) <= pmp.d_match(r) and (not pmp_ctrl_i(r)(pmp_cfg_r_c)); -- load access match no read permission
|
pmp.ld_fault(r) <= pmp.d_match(r) and (not pmp_ctrl_i(r)(pmp_cfg_r_c)); -- load access match no read permission
|
pmp.st_fault(r) <= pmp.d_match(r) and (not pmp_ctrl_i(r)(pmp_cfg_w_c)); -- store access match no write permission
|
pmp.st_fault(r) <= pmp.d_match(r) and (not pmp_ctrl_i(r)(pmp_cfg_w_c)); -- store access match no write permission
|
Line 430... |
Line 431... |
end loop; -- r
|
end loop; -- r
|
end process pmp_check_permission;
|
end process pmp_check_permission;
|
|
|
|
|
-- final PMP access fault signals --
|
-- final PMP access fault signals --
|
if_pmp_fault <= or_all_f(pmp.if_fault) when (PMP_USE = true) else '0';
|
if_pmp_fault <= or_all_f(pmp.if_fault) when (PMP_NUM_REGIONS > 0) else '0';
|
ld_pmp_fault <= or_all_f(pmp.ld_fault) when (PMP_USE = true) else '0';
|
ld_pmp_fault <= or_all_f(pmp.ld_fault) when (PMP_NUM_REGIONS > 0) else '0';
|
st_pmp_fault <= or_all_f(pmp.st_fault) when (PMP_USE = true) else '0';
|
st_pmp_fault <= or_all_f(pmp.st_fault) when (PMP_NUM_REGIONS > 0) else '0';
|
|
|
|
|
end neorv32_cpu_bus_rtl;
|
end neorv32_cpu_bus_rtl;
|
|
|
No newline at end of file
|
No newline at end of file
|