stm32mp1_fconf_firewall.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright (c) 2021-2022, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <common/debug.h>
  8. #include <common/fdt_wrappers.h>
  9. #include <drivers/arm/tzc400.h>
  10. #include <drivers/clk.h>
  11. #include <dt-bindings/clock/stm32mp1-clks.h>
  12. #include <lib/fconf/fconf.h>
  13. #include <lib/object_pool.h>
  14. #include <libfdt.h>
  15. #include <tools_share/firmware_image_package.h>
  16. #include <platform_def.h>
  17. #include <stm32mp_fconf_getter.h>
  18. #define STM32MP_REGION_PARAMS 4
  19. #define STM32MP_MAX_REGIONS 8
  20. #define FORCE_SEC_REGION BIT(31)
  21. static uint32_t nb_regions;
  22. struct dt_id_attr {
  23. fdt32_t id_attr[STM32MP_MAX_REGIONS];
  24. };
  25. void stm32mp1_arch_security_setup(void)
  26. {
  27. #if STM32MP13
  28. clk_enable(TZC);
  29. #endif
  30. #if STM32MP15
  31. clk_enable(TZC1);
  32. clk_enable(TZC2);
  33. #endif
  34. tzc400_init(STM32MP1_TZC_BASE);
  35. tzc400_disable_filters();
  36. /*
  37. * Region 0 set to cover all DRAM at 0xC000_0000
  38. * Only secure access is granted in read/write.
  39. */
  40. tzc400_configure_region0(TZC_REGION_S_RDWR, 0);
  41. tzc400_set_action(TZC_ACTION_ERR);
  42. tzc400_enable_filters();
  43. }
  44. void stm32mp1_security_setup(void)
  45. {
  46. uint8_t i;
  47. assert(nb_regions > 0U);
  48. tzc400_init(STM32MP1_TZC_BASE);
  49. tzc400_disable_filters();
  50. /*
  51. * Region 0 set to cover all DRAM at 0xC000_0000
  52. * No access is allowed.
  53. */
  54. tzc400_configure_region0(TZC_REGION_S_NONE, 0);
  55. for (i = 1U; i <= nb_regions; i++) {
  56. tzc400_update_filters(i, STM32MP1_FILTER_BIT_ALL);
  57. }
  58. tzc400_set_action(TZC_ACTION_INT);
  59. tzc400_enable_filters();
  60. }
  61. static int fconf_populate_stm32mp1_firewall(uintptr_t config)
  62. {
  63. int node, len;
  64. unsigned int i;
  65. const struct dt_id_attr *conf_list;
  66. const void *dtb = (const void *)config;
  67. /* Assert the node offset point to "st,mem-firewall" compatible property */
  68. const char *compatible_str = "st,mem-firewall";
  69. node = fdt_node_offset_by_compatible(dtb, -1, compatible_str);
  70. if (node < 0) {
  71. ERROR("FCONF: Can't find %s compatible in dtb\n", compatible_str);
  72. return node;
  73. }
  74. conf_list = (const struct dt_id_attr *)fdt_getprop(dtb, node, "memory-ranges", &len);
  75. if (conf_list == NULL) {
  76. WARN("FCONF: Read cell failed for %s\n", "memory-ranges");
  77. return -1;
  78. }
  79. /* Locate the memory cells and read all values */
  80. for (i = 0U; i < (unsigned int)(len / (sizeof(uint32_t) * STM32MP_REGION_PARAMS)); i++) {
  81. uint32_t idx = i * STM32MP_REGION_PARAMS;
  82. uint32_t base;
  83. uint32_t size;
  84. uint32_t sec_attr;
  85. uint32_t nsaid;
  86. base = fdt32_to_cpu(conf_list->id_attr[idx]);
  87. size = fdt32_to_cpu(conf_list->id_attr[idx + 1]);
  88. sec_attr = fdt32_to_cpu(conf_list->id_attr[idx + 2]);
  89. nsaid = fdt32_to_cpu(conf_list->id_attr[idx + 3]);
  90. VERBOSE("FCONF: stm32mp1-firewall cell found with value = 0x%x 0x%x 0x%x 0x%x\n",
  91. base, size, sec_attr, nsaid);
  92. nb_regions++;
  93. /* Configure region but keep disabled for secure access for BL2 load */
  94. tzc400_configure_region(0U, nb_regions, (unsigned long long)base,
  95. (unsigned long long)base + size - 1ULL, sec_attr, nsaid);
  96. }
  97. /* Force flush as the value will be used cache off */
  98. flush_dcache_range((uintptr_t)&nb_regions, sizeof(uint32_t));
  99. return 0;
  100. }
  101. FCONF_REGISTER_POPULATOR(FW_CONFIG, stm32mp1_firewall, fconf_populate_stm32mp1_firewall);