165c5b75cSMichael Clark /* 265c5b75cSMichael Clark * QEMU RISC-V PMP (Physical Memory Protection) 365c5b75cSMichael Clark * 465c5b75cSMichael Clark * Author: Daire McNamara, daire.mcnamara@emdalo.com 565c5b75cSMichael Clark * Ivan Griffin, ivan.griffin@emdalo.com 665c5b75cSMichael Clark * 765c5b75cSMichael Clark * This provides a RISC-V Physical Memory Protection interface 865c5b75cSMichael Clark * 965c5b75cSMichael Clark * This program is free software; you can redistribute it and/or modify it 1065c5b75cSMichael Clark * under the terms and conditions of the GNU General Public License, 1165c5b75cSMichael Clark * version 2 or later, as published by the Free Software Foundation. 1265c5b75cSMichael Clark * 1365c5b75cSMichael Clark * This program is distributed in the hope it will be useful, but WITHOUT 1465c5b75cSMichael Clark * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1565c5b75cSMichael Clark * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 1665c5b75cSMichael Clark * more details. 1765c5b75cSMichael Clark * 1865c5b75cSMichael Clark * You should have received a copy of the GNU General Public License along with 1965c5b75cSMichael Clark * this program. If not, see <http://www.gnu.org/licenses/>. 2065c5b75cSMichael Clark */ 2165c5b75cSMichael Clark 22a8b991b5SMarkus Armbruster #ifndef RISCV_PMP_H 23a8b991b5SMarkus Armbruster #define RISCV_PMP_H 2465c5b75cSMichael Clark 253cb1a410SPhilippe Mathieu-Daudé #include "cpu.h" 263cb1a410SPhilippe Mathieu-Daudé 2765c5b75cSMichael Clark typedef enum { 2865c5b75cSMichael Clark PMP_READ = 1 << 0, 2965c5b75cSMichael Clark PMP_WRITE = 1 << 1, 3065c5b75cSMichael Clark PMP_EXEC = 1 << 2, 314bf501dcSMayuresh Chitale PMP_AMATCH = (3 << 3), 3265c5b75cSMichael Clark PMP_LOCK = 1 << 7 3365c5b75cSMichael Clark } pmp_priv_t; 3465c5b75cSMichael Clark 3565c5b75cSMichael Clark typedef enum { 3665c5b75cSMichael Clark PMP_AMATCH_OFF, /* Null (off) */ 3765c5b75cSMichael Clark PMP_AMATCH_TOR, /* Top of Range */ 3865c5b75cSMichael Clark PMP_AMATCH_NA4, /* Naturally aligned four-byte region */ 3965c5b75cSMichael Clark PMP_AMATCH_NAPOT /* Naturally aligned power-of-two region */ 4065c5b75cSMichael Clark } pmp_am_t; 4165c5b75cSMichael Clark 422582a95cSHou Weiying typedef enum { 432582a95cSHou Weiying MSECCFG_MML = 1 << 0, 442582a95cSHou Weiying MSECCFG_MMWP = 1 << 1, 4577442380SWeiwei Li MSECCFG_RLB = 1 << 2, 4677442380SWeiwei Li MSECCFG_USEED = 1 << 8, 474923f672SDeepak Gupta MSECCFG_SSEED = 1 << 9, 484923f672SDeepak Gupta MSECCFG_MLPE = 1 << 10, 49*33ca99a1SAlexey Baturo MSECCFG_PMM = 3ULL << 32, 502582a95cSHou Weiying } mseccfg_field_t; 512582a95cSHou Weiying 5265c5b75cSMichael Clark typedef struct { 5365c5b75cSMichael Clark target_ulong addr_reg; 5465c5b75cSMichael Clark uint8_t cfg_reg; 5565c5b75cSMichael Clark } pmp_entry_t; 5665c5b75cSMichael Clark 5765c5b75cSMichael Clark typedef struct { 586f5bb7d4SIvan Klokov hwaddr sa; 596f5bb7d4SIvan Klokov hwaddr ea; 6065c5b75cSMichael Clark } pmp_addr_t; 6165c5b75cSMichael Clark 6265c5b75cSMichael Clark typedef struct { 6365c5b75cSMichael Clark pmp_entry_t pmp[MAX_RISCV_PMPS]; 6465c5b75cSMichael Clark pmp_addr_t addr[MAX_RISCV_PMPS]; 6565c5b75cSMichael Clark uint32_t num_rules; 6665c5b75cSMichael Clark } pmp_table_t; 6765c5b75cSMichael Clark 6865c5b75cSMichael Clark void pmpcfg_csr_write(CPURISCVState *env, uint32_t reg_index, 6965c5b75cSMichael Clark target_ulong val); 7065c5b75cSMichael Clark target_ulong pmpcfg_csr_read(CPURISCVState *env, uint32_t reg_index); 712582a95cSHou Weiying 722582a95cSHou Weiying void mseccfg_csr_write(CPURISCVState *env, target_ulong val); 732582a95cSHou Weiying target_ulong mseccfg_csr_read(CPURISCVState *env); 742582a95cSHou Weiying 7565c5b75cSMichael Clark void pmpaddr_csr_write(CPURISCVState *env, uint32_t addr_index, 7665c5b75cSMichael Clark target_ulong val); 7765c5b75cSMichael Clark target_ulong pmpaddr_csr_read(CPURISCVState *env, uint32_t addr_index); 786f5bb7d4SIvan Klokov bool pmp_hart_has_privs(CPURISCVState *env, hwaddr addr, 79c45eff30SWeiwei Li target_ulong size, pmp_priv_t privs, 80c45eff30SWeiwei Li pmp_priv_t *allowed_privs, 81b297129aSJim Shu target_ulong mode); 826f5bb7d4SIvan Klokov target_ulong pmp_get_tlb_size(CPURISCVState *env, hwaddr addr); 8324beb03eSYifei Jiang void pmp_update_rule_addr(CPURISCVState *env, uint32_t pmp_index); 8424beb03eSYifei Jiang void pmp_update_rule_nums(CPURISCVState *env); 85d102f19aSAtish Patra uint32_t pmp_get_num_rules(CPURISCVState *env); 86b297129aSJim Shu int pmp_priv_to_page_prot(pmp_priv_t pmp_priv); 874bf501dcSMayuresh Chitale void pmp_unlock_entries(CPURISCVState *env); 8865c5b75cSMichael Clark 892582a95cSHou Weiying #define MSECCFG_MML_ISSET(env) get_field(env->mseccfg, MSECCFG_MML) 902582a95cSHou Weiying #define MSECCFG_MMWP_ISSET(env) get_field(env->mseccfg, MSECCFG_MMWP) 912582a95cSHou Weiying #define MSECCFG_RLB_ISSET(env) get_field(env->mseccfg, MSECCFG_RLB) 922582a95cSHou Weiying 9365c5b75cSMichael Clark #endif 94