blob: 9b21ee8b46fb3c63154581b0ce81170d1ea51348 [file] [log] [blame]
Jonny Svärd991af2b2021-04-15 17:31:01 +02001/*
Rajasekaran Kalidoss8c940862024-05-16 21:47:21 +02002 * SPDX-FileCopyrightText: Copyright 2020-2024 Arm Limited and/or its affiliates <open-source-office@arm.com>
Jonny Svärd991af2b2021-04-15 17:31:01 +02003 * 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 Garboviche9cdc632021-11-23 20:00:04 +020022#include <mpu.hpp>
Jonny Svärd991af2b2021-04-15 17:31:01 +020023
Rajasekaran Kalidoss8c940862024-05-16 21:47:21 +020024#if CMSIS_VER == 6
25#include <m-profile/armv7m_cachel1.h>
26#else
Jonny Svärd991af2b2021-04-15 17:31:01 +020027#include <cachel1_armv7.h>
Rajasekaran Kalidoss8c940862024-05-16 21:47:21 +020028#endif
29
Jonny Svärd991af2b2021-04-15 17:31:01 +020030#include <inttypes.h>
31#include <stdio.h>
32
33using namespace std;
34
35/****************************************************************************
36 * Functions
37 ****************************************************************************/
38
39namespace EthosU {
40namespace Mpu {
41
42void dump() {
43#ifdef ARM_MPU_ARMV8_H
44 uint32_t mpuRegions = (MPU->TYPE & MPU_TYPE_DREGION_Msk) >> MPU_TYPE_DREGION_Pos;
45
Kristofer Jonsson29467e02021-11-26 16:10:43 +010046 printf("MPU available with %" PRIu32 " regions.\n", mpuRegions);
Jonny Svärd991af2b2021-04-15 17:31:01 +020047
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 Dajab0aacb42023-02-17 09:37:58 +010054 printf("-- Region %2u - RBAR:%08" PRIx32 " RLAR:%08" PRIx32 "\n", region, MPU->RBAR, MPU->RLAR);
Jonny Svärd991af2b2021-04-15 17:31:01 +020055 }
56#endif
57}
58
Jonny Svärd991af2b2021-04-15 17:31:01 +020059#ifdef ARM_MPU_ARMV8_H
Davide Grohmannfe0d0432022-02-10 13:43:11 +010060static void initializeAttributes() {
Jonny Svärd991af2b2021-04-15 17:31:01 +020061 /* 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 Jonsson01c32d42022-10-18 11:34:23 +020065 const uint8_t WTWARA =
66 ARM_MPU_ATTR_MEMORY_(1, 0, 1, 1); // Non-transient, Write-Through, Read-allocate, Write-allocate
Jonny Svärd991af2b2021-04-15 17:31:01 +020067
68 ARM_MPU_SetMemAttr(WTRA_index, ARM_MPU_ATTR(WTRA, WTRA));
69 ARM_MPU_SetMemAttr(WBWARA_index, ARM_MPU_ATTR(WBWARA, WBWARA));
Kristofer Jonsson01c32d42022-10-18 11:34:23 +020070 ARM_MPU_SetMemAttr(WTWARA_index, ARM_MPU_ATTR(WTWARA, WTWARA));
Jonny Svärd991af2b2021-04-15 17:31:01 +020071}
Davide Grohmannfe0d0432022-02-10 13:43:11 +010072#endif
Jonny Svärd991af2b2021-04-15 17:31:01 +020073
74void 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 Garboviche9cdc632021-11-23 20:00:04 +020081 ARM_MPU_Enable((1 << MPU_CTRL_PRIVDEFENA_Pos) & MPU_CTRL_PRIVDEFENA_Msk);
Jonny Svärdece79822024-06-24 16:41:23 +020082#else
83 (void)table;
84 (void)cnt;
Jonny Svärd991af2b2021-04-15 17:31:01 +020085#endif
86}
87
88}; // namespace Mpu
89}; // namespace EthosU