|
| 1 | +/* |
| 2 | + * Copyright (C) 2023 Bennet Blischke <[email protected]> |
| 3 | + * |
| 4 | + * This file is subject to the terms and conditions of the GNU Lesser General |
| 5 | + * Public License v2.1. See the file LICENSE in the top level directory for more |
| 6 | + * details. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * @ingroup cpu_riscv_common |
| 11 | + * @{ |
| 12 | + * |
| 13 | + * @file |
| 14 | + * @brief RISC-V PMP configuration options |
| 15 | + * |
| 16 | + * RISCV implementations using this peripheral must define the `NUM_PMP_ENTRIES` |
| 17 | + * `NUM_PMP_ENTRIES` must be 16 or 64. |
| 18 | + * |
| 19 | + * @author Bennet Blischke |
| 20 | + */ |
| 21 | + |
| 22 | +#ifndef PMP_H |
| 23 | +#define PMP_H |
| 24 | + |
| 25 | +#include <assert.h> |
| 26 | +#include <stdint.h> |
| 27 | + |
| 28 | +#ifdef __cplusplus |
| 29 | +extern "C" { |
| 30 | +#endif |
| 31 | + |
| 32 | +/** |
| 33 | + * @name Bit masks for the PMP configuration register |
| 34 | + * @{ |
| 35 | + */ |
| 36 | +#define PMP_NONE 0x00 /**< No access allowed at all */ |
| 37 | +#define PMP_R 0x01 /**< Allow read access */ |
| 38 | +#define PMP_W 0x02 /**< Allow write access */ |
| 39 | +#define PMP_X 0x04 /**< Allow execution */ |
| 40 | + |
| 41 | +#define PMP_A 0x18 /**< Addressing mode mask */ |
| 42 | +#define PMP_OFF 0x00 /**< Disable this pmp entry */ |
| 43 | +#define PMP_TOR 0x08 /**< Top-of-range addressing mode */ |
| 44 | +#define PMP_NA4 0x10 /**< Naturally aligned four-byte region */ |
| 45 | +#define PMP_NAPOT 0x18 /**< Naturally aligned power-of-two region, ≥8 bytes */ |
| 46 | + |
| 47 | +#define PMP_L 0x80 /**< Lock; read-only config & applies to machine-mode */ |
| 48 | +/** @} */ |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief Create a NAPOT formatted address |
| 52 | + * |
| 53 | + * @param addr Base address, must be aligned to the size of the region |
| 54 | + * @param size Size of the region in bytes |
| 55 | + */ |
| 56 | +static inline uint32_t make_napot(uint32_t addr, uint32_t size) |
| 57 | +{ |
| 58 | + assert(addr % size == 0); |
| 59 | + return addr | ((size - 1) >> 1); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * @brief Writes a complete pmpcfg register |
| 64 | + * |
| 65 | + * @param reg_num Register number |
| 66 | + * @param value Value to write |
| 67 | + */ |
| 68 | +void write_pmpcfg(uint8_t reg_num, uint32_t value); |
| 69 | + |
| 70 | +/** |
| 71 | + * @brief Read a complete pmpcfg register |
| 72 | + * |
| 73 | + * @param[in] reg_num Register number |
| 74 | + * |
| 75 | + * @return Contents of the specified register |
| 76 | + */ |
| 77 | +uint32_t read_pmpcfg(uint8_t reg_num); |
| 78 | + |
| 79 | +/** |
| 80 | + * @brief Writes a complete pmpaddr register |
| 81 | + * |
| 82 | + * @param reg_num Register number |
| 83 | + * @param value Value to write |
| 84 | + */ |
| 85 | +void write_pmpaddr(uint8_t reg_num, uint32_t value); |
| 86 | + |
| 87 | +/** |
| 88 | + * @brief Read a complete pmpaddr register |
| 89 | + * |
| 90 | + * @param[in] reg_num Register number |
| 91 | + * |
| 92 | + * @return Contents of the specified register |
| 93 | + */ |
| 94 | +uint32_t read_pmpaddr(uint8_t reg_num); |
| 95 | + |
| 96 | +/** |
| 97 | + * @brief Read a single pmpcfg sub-register |
| 98 | + * |
| 99 | + * @param[in] entry Sub-register number |
| 100 | + * |
| 101 | + * @return Contents of the specified sub-register |
| 102 | + */ |
| 103 | +uint8_t get_pmpcfg(uint8_t entry); |
| 104 | + |
| 105 | +/** |
| 106 | + * @brief Set's a single pmpcfg sub-register |
| 107 | + * |
| 108 | + * @param entry Sub-register number |
| 109 | + * @param value Value to write |
| 110 | + */ |
| 111 | +void set_pmpcfg(uint8_t entry, uint8_t value); |
| 112 | + |
| 113 | +/** |
| 114 | + * @brief Prints a single pmpcfg sub-register human readable |
| 115 | + * |
| 116 | + * @param entry Register number to print |
| 117 | + */ |
| 118 | +void print_pmpcfg(uint8_t entry); |
| 119 | + |
| 120 | +#ifdef __cplusplus |
| 121 | +} |
| 122 | +#endif |
| 123 | + |
| 124 | +#endif /* PMP_H */ |
| 125 | +/** @} */ |
0 commit comments