1 /*
2 * Copyright (C) 2018 Marvell International Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 * https://spdx.org/licenses
6 */
7
8 #include <common/debug.h>
9 #include <drivers/marvell/addr_map.h>
10 #include <lib/mmio.h>
11
12 #include <mvebu_def.h>
13
14 #include "mc_trustzone.h"
15
16 #define TZ_SIZE(x) ((x) >> 13)
17
fls(int x)18 static int fls(int x)
19 {
20 if (!x)
21 return 0;
22
23 return 32 - __builtin_clz(x);
24 }
25
26 /* To not duplicate types, the addr_map_win is used, but the "target"
27 * filed is referring to attributes instead of "target".
28 */
tz_enable_win(int ap_index,const struct addr_map_win * win,int win_id)29 void tz_enable_win(int ap_index, const struct addr_map_win *win, int win_id)
30 {
31 int tz_size;
32 uint32_t val, base = win->base_addr;
33
34 if ((win_id < 0) || (win_id > MVEBU_TZ_MAX_WINS)) {
35 ERROR("Enabling wrong MC TrustZone window %d!\n", win_id);
36 return;
37 }
38
39 /* map the window size to trustzone register convention */
40 tz_size = fls(TZ_SIZE(win->win_size));
41
42 VERBOSE("%s: window size = 0x%llx maps to tz_size %d\n",
43 __func__, win->win_size, tz_size);
44 if (tz_size < 0 || tz_size > 31) {
45 ERROR("Using not allowed size for MC TrustZone window %d!\n",
46 win_id);
47 return;
48 }
49
50 if (base & 0xfff) {
51 base = base & ~0xfff;
52 WARN("Attempt to open MC TZ win. at 0x%llx, truncate to 0x%x\n",
53 win->base_addr, base);
54 }
55
56 val = base | (tz_size << 7) | win->target_id | TZ_VALID;
57
58 VERBOSE("%s: base 0x%x, tz_size moved 0x%x, attr 0x%x, val 0x%x\n",
59 __func__, base, (tz_size << 7), win->target_id, val);
60
61 mmio_write_32(MVEBU_AP_MC_TRUSTZONE_REG_LOW(ap_index, win_id), val);
62
63 VERBOSE("%s: Win%d[0x%x] configured to 0x%x\n", __func__, win_id,
64 MVEBU_AP_MC_TRUSTZONE_REG_LOW(ap_index, win_id),
65 mmio_read_32(MVEBU_AP_MC_TRUSTZONE_REG_LOW(ap_index, win_id)));
66
67 mmio_write_32(MVEBU_AP_MC_TRUSTZONE_REG_HIGH(ap_index, win_id),
68 (win->base_addr >> 32));
69
70 VERBOSE("%s: Win%d[0x%x] configured to 0x%x\n", __func__, win_id,
71 MVEBU_AP_MC_TRUSTZONE_REG_HIGH(ap_index, win_id),
72 mmio_read_32(MVEBU_AP_MC_TRUSTZONE_REG_HIGH(ap_index, win_id)));
73 }
74