1 /*
2  * Arm SCP/MCP Software
3  * Copyright (c) 2022, Arm Limited and Contributors. All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #include "mod_armv8m_mpu.h"
9 
10 #include <fwk_assert.h>
11 #include <fwk_id.h>
12 #include <fwk_module.h>
13 #include <fwk_status.h>
14 
15 #include <fmw_cmsis.h>
16 
armv8m_mpu_init(fwk_id_t module_id,unsigned int element_count,const void * data)17 static int armv8m_mpu_init(
18     fwk_id_t module_id,
19     unsigned int element_count,
20     const void *data)
21 {
22     const struct mod_armv8m_mpu_config *config;
23     uint8_t attr_index;
24     int status;
25 
26     fwk_assert(element_count == 0);
27     fwk_assert(data != NULL);
28 
29     config = data;
30 
31     if (config->attributes_count <= (uint8_t)MPU_MAX_ATTR_COUNT) {
32         ARM_MPU_Disable();
33 
34         for (attr_index = 0U; attr_index < config->attributes_count;
35              attr_index++) {
36             ARM_MPU_SetMemAttr(attr_index, config->attributes[attr_index]);
37         }
38         ARM_MPU_Load(
39             config->first_region_number, config->regions, config->region_count);
40         ARM_MPU_Enable(MPU_CTRL_HFNMIENA_Msk);
41 
42         status = FWK_SUCCESS;
43     } else {
44         status = FWK_E_RANGE;
45     }
46 
47     return status;
48 }
49 
50 /* Module description */
51 const struct fwk_module module_armv8m_mpu = {
52     .type = FWK_MODULE_TYPE_DRIVER,
53     .init = armv8m_mpu_init,
54 };
55