1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <common/bk_include.h>
16 #include <components/system.h>
17 #include <driver/mpc.h>
18 #include <os/os.h>
19 #include <os/mem.h>
20 #include "mpc_driver.h"
21 #include "mpc_hal.h"
22
23 typedef struct {
24 mpc_hal_t hal;
25 } mpc_driver_t;
26
27 #define MPC_RETURN_ON_DRIVER_NOT_INIT() do {\
28 if (!s_mpc_driver_is_init) {\
29 MPC_LOGE("MPC driver not init\r\n");\
30 return BK_ERR_MPC_DRIVER_NOT_INIT;\
31 }\
32 } while(0)
33
34 static mpc_driver_t s_mpc[MPC_DEV_MAX] = {0};
35 static bool s_mpc_driver_is_init = false;
36
bk_mpc_driver_init(void)37 bk_err_t bk_mpc_driver_init(void)
38 {
39 if (s_mpc_driver_is_init) {
40 return BK_OK;
41 }
42
43 os_memset(&s_mpc, 0, sizeof(s_mpc));
44 for (uint32_t dev_id = 0; dev_id < MPC_DEV_MAX; dev_id++) {
45 s_mpc[dev_id].hal.dev = dev_id;
46 mpc_hal_init(&s_mpc[dev_id].hal);
47 }
48 s_mpc_driver_is_init = true;
49
50 return BK_OK;
51 }
52
bk_mpc_driver_deinit(void)53 bk_err_t bk_mpc_driver_deinit(void)
54 {
55 if (!s_mpc_driver_is_init) {
56 return BK_OK;
57 }
58 os_memset(&s_mpc, 0, sizeof(s_mpc));
59 s_mpc_driver_is_init = false;
60
61 return BK_OK;
62 }
63
bk_mpc_get_block_size(mpc_dev_t dev)64 uint32_t bk_mpc_get_block_size(mpc_dev_t dev)
65 {
66 return mpc_hal_get_block_size(&s_mpc[dev].hal);
67 }
68
bk_mpc_get_max_block_index(mpc_dev_t dev)69 uint32_t bk_mpc_get_max_block_index(mpc_dev_t dev)
70 {
71 return mpc_hal_get_max_block_index(&s_mpc[dev].hal);
72 }
73
bk_mpc_set_secure_attribute(mpc_dev_t dev,uint32_t mem_addr_offset,uint32_t block_num,mpc_block_secure_type_t secure_type)74 bk_err_t bk_mpc_set_secure_attribute(mpc_dev_t dev, uint32_t mem_addr_offset, uint32_t block_num, mpc_block_secure_type_t secure_type)
75 {
76 MPC_RETURN_ON_DRIVER_NOT_INIT();
77
78 uint32_t block_offset = 0;
79 uint32_t block_index = 0;
80 uint32_t block_size = bk_mpc_get_block_size(dev);
81 bk_err_t ret = BK_OK;
82 mpc_hal_t *hal = &s_mpc[dev].hal;
83
84 if (mem_addr_offset % block_size) {
85 MPC_LOGW("mem_addr_offset not block size:%d aligned\r\n", block_size);
86 }
87
88 /* enable block index auto increase */
89 mpc_hal_enable_auto_increase(hal);
90
91 /* set mpc block index */
92 block_offset = mem_addr_offset / (bk_mpc_get_block_size(dev));
93 block_index = block_offset / MPC_BLOCK_LUT_MAX_BIT_NUM;
94 if (block_index > bk_mpc_get_max_block_index(dev)) {
95 MPC_LOGW("block index:%d is out of range\r\n", block_index);
96 return BK_ERR_MPC_BLOCK_INDEX_OUT_OF_RANGE;
97 } else {
98 mpc_hal_set_block_index(hal, block_index);
99 }
100
101 /* set block lut */
102 ret = mpc_hal_config_block_lut(hal, block_offset, block_num, secure_type);
103
104 /* disable block index auto increase */
105 mpc_hal_disable_auto_increase(hal);
106
107 return ret;
108 }
109
bk_mpc_lockdown(mpc_dev_t dev)110 bk_err_t bk_mpc_lockdown(mpc_dev_t dev)
111 {
112 MPC_RETURN_ON_DRIVER_NOT_INIT();
113
114 mpc_hal_enable_sec_lock(&s_mpc[dev].hal);
115
116 return BK_OK;
117 }
118
119