Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 1 | /* |
Rajasekaran Kalidoss | 8c94086 | 2024-05-16 21:47:21 +0200 | [diff] [blame] | 2 | * SPDX-FileCopyrightText: Copyright 2020-2024 Arm Limited and/or its affiliates <open-source-office@arm.com> |
Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 3 | * SPDX-License-Identifier: Apache-2.0 |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| 6 | * not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | /**************************************************************************** |
| 19 | * Includes |
| 20 | ****************************************************************************/ |
| 21 | |
Yulia Garbovich | e9cdc63 | 2021-11-23 20:00:04 +0200 | [diff] [blame] | 22 | #include <mpu.hpp> |
Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 23 | |
Rajasekaran Kalidoss | 8c94086 | 2024-05-16 21:47:21 +0200 | [diff] [blame] | 24 | #if CMSIS_VER == 6 |
| 25 | #include <m-profile/armv7m_cachel1.h> |
| 26 | #else |
Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 27 | #include <cachel1_armv7.h> |
Rajasekaran Kalidoss | 8c94086 | 2024-05-16 21:47:21 +0200 | [diff] [blame] | 28 | #endif |
| 29 | |
Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 30 | #include <inttypes.h> |
| 31 | #include <stdio.h> |
| 32 | |
| 33 | using namespace std; |
| 34 | |
| 35 | /**************************************************************************** |
| 36 | * Functions |
| 37 | ****************************************************************************/ |
| 38 | |
| 39 | namespace EthosU { |
| 40 | namespace Mpu { |
| 41 | |
| 42 | void dump() { |
| 43 | #ifdef ARM_MPU_ARMV8_H |
| 44 | uint32_t mpuRegions = (MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos; |
| 45 | |
Kristofer Jonsson | 29467e0 | 2021-11-26 16:10:43 +0100 | [diff] [blame] | 46 | printf("MPU available with %" PRIu32 " regions.\n", mpuRegions); |
Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 47 | |
| 48 | printf(" PRIVDEFENA : %lx\n", (MPU->CTRL & MPU_CTRL_PRIVDEFENA_Msk) >> MPU_CTRL_PRIVDEFENA_Pos); |
| 49 | printf(" HFNMIENA : %lx\n", (MPU->CTRL & MPU_CTRL_HFNMIENA_Msk) >> MPU_CTRL_HFNMIENA_Pos); |
| 50 | printf(" ENABLE : %lx\n", (MPU->CTRL & MPU_CTRL_ENABLE_Msk) >> MPU_CTRL_ENABLE_Pos); |
| 51 | |
| 52 | for (size_t region = 0; region < mpuRegions; region++) { |
| 53 | MPU->RNR = region; |
Ledion Daja | b0aacb4 | 2023-02-17 09:37:58 +0100 | [diff] [blame] | 54 | printf("-- Region %2u - RBAR:%08" PRIx32 " RLAR:%08" PRIx32 "\n", region, MPU->RBAR, MPU->RLAR); |
Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 55 | } |
| 56 | #endif |
| 57 | } |
| 58 | |
Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 59 | #ifdef ARM_MPU_ARMV8_H |
Davide Grohmann | fe0d043 | 2022-02-10 13:43:11 +0100 | [diff] [blame] | 60 | static void initializeAttributes() { |
Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 61 | /* Initialize attributes corresponding to the enums defined in mpu.hpp */ |
| 62 | const uint8_t WTRA = |
| 63 | ARM_MPU_ATTR_MEMORY_(1, 0, 1, 0); // Non-transient, Write-Through, Read-allocate, Not Write-allocate |
| 64 | const uint8_t WBWARA = ARM_MPU_ATTR_MEMORY_(1, 1, 1, 1); // Non-transient, Write-Back, Read-allocate, Write-allocate |
Kristofer Jonsson | 01c32d4 | 2022-10-18 11:34:23 +0200 | [diff] [blame] | 65 | const uint8_t WTWARA = |
| 66 | ARM_MPU_ATTR_MEMORY_(1, 0, 1, 1); // Non-transient, Write-Through, Read-allocate, Write-allocate |
Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 67 | |
| 68 | ARM_MPU_SetMemAttr(WTRA_index, ARM_MPU_ATTR(WTRA, WTRA)); |
| 69 | ARM_MPU_SetMemAttr(WBWARA_index, ARM_MPU_ATTR(WBWARA, WBWARA)); |
Kristofer Jonsson | 01c32d4 | 2022-10-18 11:34:23 +0200 | [diff] [blame] | 70 | ARM_MPU_SetMemAttr(WTWARA_index, ARM_MPU_ATTR(WTWARA, WTWARA)); |
Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 71 | } |
Davide Grohmann | fe0d043 | 2022-02-10 13:43:11 +0100 | [diff] [blame] | 72 | #endif |
Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 73 | |
| 74 | void loadAndEnableConfig(ARM_MPU_Region_t const *table, uint32_t cnt) { |
| 75 | #ifdef ARM_MPU_ARMV8_H |
| 76 | initializeAttributes(); |
| 77 | |
| 78 | ARM_MPU_Load(0, table, cnt); |
| 79 | |
| 80 | // Enable MPU with default priv access to all other regions |
Yulia Garbovich | e9cdc63 | 2021-11-23 20:00:04 +0200 | [diff] [blame] | 81 | ARM_MPU_Enable((1 << MPU_CTRL_PRIVDEFENA_Pos) & MPU_CTRL_PRIVDEFENA_Msk); |
Jonny Svärd | ece7982 | 2024-06-24 16:41:23 +0200 | [diff] [blame^] | 82 | #else |
| 83 | (void)table; |
| 84 | (void)cnt; |
Jonny Svärd | 991af2b | 2021-04-15 17:31:01 +0200 | [diff] [blame] | 85 | #endif |
| 86 | } |
| 87 | |
| 88 | }; // namespace Mpu |
| 89 | }; // namespace EthosU |