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