blob: 4b95939418876344a769a90c033de459a21438e7 [file] [log] [blame]
Jerome Forissierc52e55f2015-05-04 09:40:03 +02001/*
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 Forissierc52e55f2015-05-04 09:40:03 +02008#include <stdint.h>
Antonio Nino Diaz4b32e622018-08-16 16:52:57 +01009#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000010
11#include <platform_def.h>
12
13#include <common/debug.h>
14#include <lib/utils_def.h>
15
Jerome Forissierc52e55f2015-05-04 09:40:03 +020016#include "hikey_private.h"
17
18#define PORTNUM_MAX 5
19
20#define MDDRC_SECURITY_BASE 0xF7121000
21
22struct int_en_reg {
23 unsigned in_en:1;
24 unsigned reserved:31;
25};
26
27struct 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
34struct 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
45static 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
51static 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
57static 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 Griffin28c8ed22017-12-21 18:03:46 +000069static void sec_protect(uint32_t region_base, uint32_t region_size,
70 int region)
Jerome Forissierc52e55f2015-05-04 09:40:03 +020071{
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 Chong4fa170c2018-01-27 21:36:12 +090077 /* 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 Forissierc52e55f2015-05-04 09:40:03 +020083
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 Griffin28c8ed22017-12-21 18:03:46 +000091 rgn_map = get_rgn_map_reg(MDDRC_SECURITY_BASE, region, i);
92 rgn_attr = get_rgn_attr_reg(MDDRC_SECURITY_BASE, region, i);
Jerome Forissierc52e55f2015-05-04 09:40:03 +020093 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 ******************************************************************************/
104void hikey_security_setup(void)
105{
Peter Griffin28c8ed22017-12-21 18:03:46 +0000106 sec_protect(DDR_SEC_BASE, DDR_SEC_SIZE, 1);
107 sec_protect(DDR_SDP_BASE, DDR_SDP_SIZE, 2);
Jerome Forissierc52e55f2015-05-04 09:40:03 +0200108}