apusys_security_ctrl_perm_plat.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (c) 2024, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <errno.h>
  7. #include <common/debug.h>
  8. #include <lib/mmio.h>
  9. #include "apusys_security_ctrl_perm.h"
  10. #include "apusys_security_ctrl_perm_plat.h"
  11. #define SEC_CTRL_APU_SEC_CON_BASE (0x190F5000)
  12. #define SEC_CTRL_RV_DOMAIN_OFS (0x60)
  13. #define SEC_CTRL_RV_NS_OFS (0x64)
  14. #define SEC_CTRL_RV_DOMAIN_SHF (4)
  15. #define SEC_CTRL_RV_NS_SHF (1)
  16. #define SEC_LEVEL_NORMAL_DOMAIN (7)
  17. #define SEC_LEVEL_NORMAL_NS (1)
  18. #define SEC_LEVEL_SAPU_DOMAIN (5)
  19. #define SEC_LEVEL_SAPU_NS (1)
  20. #define SEC_LEVEL_AOV_DOMAIN (14)
  21. #define SEC_LEVEL_AOV_NS (1)
  22. #define SEC_LEVEL_UP_SECURE_DOMAIN (5)
  23. #define SEC_LEVEL_UP_SECURE_NS (0)
  24. #define SEC_LEVEL_MVPU_SECURE_DOMAIN (7)
  25. #define SEC_LEVEL_MVPU_SECURE_NS (0)
  26. #define SEC_LEVEL_MDLA_SECURE_DOMAIN (14)
  27. #define SEC_LEVEL_MDLA_SECURE_NS (0)
  28. #define DOMAIN(SEC_LVL) SEC_LEVEL_##SEC_LVL##_DOMAIN
  29. #define NS(SEC_LVL) SEC_LEVEL_##SEC_LVL##_NS
  30. int sec_get_dns(enum apusys_dev_type dev_type, enum apusys_sec_level sec_level,
  31. uint8_t *domain, uint8_t *ns)
  32. {
  33. if ((dev_type < 0) || (dev_type >= APUSYS_DEVICE_NUM)) {
  34. ERROR("invalid dev type %d\n", dev_type);
  35. return -EINVAL;
  36. }
  37. if ((sec_level < 0) || (sec_level >= SEC_LEVEL_NUM)) {
  38. ERROR("invalid sec_level %d\n", sec_level);
  39. return -EINVAL;
  40. }
  41. switch (sec_level) {
  42. case SEC_LEVEL_NORMAL:
  43. *domain = DOMAIN(NORMAL);
  44. *ns = NS(NORMAL);
  45. break;
  46. case SEC_LEVEL_SECURE:
  47. switch (dev_type) {
  48. case APUSYS_DEVICE_MVPU:
  49. *domain = DOMAIN(MVPU_SECURE);
  50. *ns = NS(MVPU_SECURE);
  51. break;
  52. case APUSYS_DEVICE_MDLA:
  53. *domain = DOMAIN(MDLA_SECURE);
  54. *ns = NS(MDLA_SECURE);
  55. break;
  56. case APUSYS_DEVICE_UP:
  57. *domain = DOMAIN(UP_SECURE);
  58. *ns = NS(UP_SECURE);
  59. break;
  60. default:
  61. ERROR("invalid dev type %d\n", dev_type);
  62. return -EINVAL;
  63. };
  64. break;
  65. case SEC_LEVEL_SAPU:
  66. *domain = DOMAIN(SAPU);
  67. *ns = NS(SAPU);
  68. break;
  69. case SEC_LEVEL_AOV:
  70. *domain = DOMAIN(AOV);
  71. *ns = NS(AOV);
  72. break;
  73. default:
  74. ERROR("invalid sec_level %d\n", sec_level);
  75. return -EINVAL;
  76. };
  77. return 0;
  78. }
  79. int sec_set_rv_dns(void)
  80. {
  81. uint8_t normal_domain;
  82. uint8_t normal_ns;
  83. uint8_t sec_domain;
  84. uint8_t sec_ns;
  85. int ret;
  86. ret = sec_get_dns(APUSYS_DEVICE_UP, SEC_LEVEL_SECURE, &sec_domain, &sec_ns);
  87. if (ret) {
  88. ERROR("%s failed.\n", __func__);
  89. return ret;
  90. }
  91. ret = sec_get_dns(APUSYS_DEVICE_UP, SEC_LEVEL_NORMAL, &normal_domain, &normal_ns);
  92. if (ret) {
  93. ERROR("%s failed.\n", __func__);
  94. return ret;
  95. }
  96. mmio_write_32(SEC_CTRL_APU_SEC_CON_BASE + SEC_CTRL_RV_DOMAIN_OFS,
  97. (sec_domain << SEC_CTRL_RV_DOMAIN_SHF) | normal_domain);
  98. mmio_write_32(SEC_CTRL_APU_SEC_CON_BASE + SEC_CTRL_RV_NS_OFS,
  99. (sec_ns << SEC_CTRL_RV_NS_SHF) | normal_ns);
  100. return 0;
  101. }