hikey_security.c 2.8 KB

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