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