Jerome Forissier | c52e55f | 2015-05-04 09:40:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018, ARM Limited and Contributors. All rights reserved. |
| 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <assert.h> |
Jerome Forissier | c52e55f | 2015-05-04 09:40:03 +0200 | [diff] [blame] | 8 | #include <stdint.h> |
Antonio Nino Diaz | 4b32e62 | 2018-08-16 16:52:57 +0100 | [diff] [blame] | 9 | #include <string.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | |
| 11 | #include <platform_def.h> |
| 12 | |
| 13 | #include <common/debug.h> |
| 14 | #include <lib/utils_def.h> |
| 15 | |
Jerome Forissier | c52e55f | 2015-05-04 09:40:03 +0200 | [diff] [blame] | 16 | #include "hikey_private.h" |
| 17 | |
| 18 | #define PORTNUM_MAX 5 |
| 19 | |
| 20 | #define MDDRC_SECURITY_BASE 0xF7121000 |
| 21 | |
| 22 | struct int_en_reg { |
| 23 | unsigned in_en:1; |
| 24 | unsigned reserved:31; |
| 25 | }; |
| 26 | |
| 27 | struct rgn_map_reg { |
| 28 | unsigned rgn_base_addr:24; |
| 29 | unsigned rgn_size:6; |
| 30 | unsigned reserved:1; |
| 31 | unsigned rgn_en:1; |
| 32 | }; |
| 33 | |
| 34 | struct rgn_attr_reg { |
| 35 | unsigned sp:4; |
| 36 | unsigned security_inv:1; |
| 37 | unsigned reserved_0:3; |
| 38 | unsigned mid_en:1; |
| 39 | unsigned mid_inv:1; |
| 40 | unsigned reserved_1:6; |
| 41 | unsigned rgn_en:1; |
| 42 | unsigned subrgn_disable:16; |
| 43 | }; |
| 44 | |
| 45 | static volatile struct int_en_reg *get_int_en_reg(uint32_t base) |
| 46 | { |
| 47 | uint64_t addr = base + 0x20; |
| 48 | return (struct int_en_reg *)addr; |
| 49 | } |
| 50 | |
| 51 | static volatile struct rgn_map_reg *get_rgn_map_reg(uint32_t base, int region, int port) |
| 52 | { |
| 53 | uint64_t addr = base + 0x100 + 0x10 * region + 0x400 * (uint64_t)port; |
| 54 | return (struct rgn_map_reg *)addr; |
| 55 | } |
| 56 | |
| 57 | static volatile struct rgn_attr_reg *get_rgn_attr_reg(uint32_t base, int region, |
| 58 | int port) |
| 59 | { |
| 60 | uint64_t addr = base + 0x104 + 0x10 * region + 0x400 * (uint64_t)port; |
| 61 | return (struct rgn_attr_reg *)addr; |
| 62 | } |
| 63 | |
| 64 | /* |
| 65 | * Configure secure memory region |
| 66 | * region_size must be a power of 2 and at least 64KB |
| 67 | * region_base must be region_size aligned |
| 68 | */ |
Peter Griffin | 28c8ed2 | 2017-12-21 18:03:46 +0000 | [diff] [blame] | 69 | static void sec_protect(uint32_t region_base, uint32_t region_size, |
| 70 | int region) |
Jerome Forissier | c52e55f | 2015-05-04 09:40:03 +0200 | [diff] [blame] | 71 | { |
| 72 | volatile struct int_en_reg *int_en; |
| 73 | volatile struct rgn_map_reg *rgn_map; |
| 74 | volatile struct rgn_attr_reg *rgn_attr; |
| 75 | uint32_t i = 0; |
| 76 | |
Victor Chong | 4fa170c | 2018-01-27 21:36:12 +0900 | [diff] [blame] | 77 | /* ensure secure region number is between 1-15 */ |
| 78 | assert(region > 0 && region < 16); |
| 79 | /* ensure secure region size is a power of 2 >= 64KB */ |
| 80 | assert(IS_POWER_OF_TWO(region_size) && region_size >= 0x10000); |
| 81 | /* ensure secure region address is aligned to region size */ |
| 82 | assert(!(region_base & (region_size - 1))); |
Jerome Forissier | c52e55f | 2015-05-04 09:40:03 +0200 | [diff] [blame] | 83 | |
| 84 | INFO("BL2: TrustZone: protecting %u bytes of memory at 0x%x\n", region_size, |
| 85 | region_base); |
| 86 | |
| 87 | int_en = get_int_en_reg(MDDRC_SECURITY_BASE); |
| 88 | int_en->in_en = 0x1; |
| 89 | |
| 90 | for (i = 0; i < PORTNUM_MAX; i++) { |
Peter Griffin | 28c8ed2 | 2017-12-21 18:03:46 +0000 | [diff] [blame] | 91 | rgn_map = get_rgn_map_reg(MDDRC_SECURITY_BASE, region, i); |
| 92 | rgn_attr = get_rgn_attr_reg(MDDRC_SECURITY_BASE, region, i); |
Jerome Forissier | c52e55f | 2015-05-04 09:40:03 +0200 | [diff] [blame] | 93 | rgn_map->rgn_base_addr = region_base >> 16; |
| 94 | rgn_attr->subrgn_disable = 0x0; |
| 95 | rgn_attr->sp = (i == 3) ? 0xC : 0x0; |
| 96 | rgn_map->rgn_size = __builtin_ffs(region_size) - 2; |
| 97 | rgn_map->rgn_en = 0x1; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /******************************************************************************* |
| 102 | * Initialize the secure environment. |
| 103 | ******************************************************************************/ |
| 104 | void hikey_security_setup(void) |
| 105 | { |
Peter Griffin | 28c8ed2 | 2017-12-21 18:03:46 +0000 | [diff] [blame] | 106 | sec_protect(DDR_SEC_BASE, DDR_SEC_SIZE, 1); |
| 107 | sec_protect(DDR_SDP_BASE, DDR_SDP_SIZE, 2); |
Jerome Forissier | c52e55f | 2015-05-04 09:40:03 +0200 | [diff] [blame] | 108 | } |