mtk_iommu_smc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * Copyright (c) 2022-2023, MediaTek Inc. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stddef.h>
  7. #include <mtk_iommu_priv.h>
  8. /* defination */
  9. /* smi larb */
  10. #define SMI_LARB_NON_SEC_CON(port) (0x380 + ((port) << 2))
  11. #define PATH_SEL_MASK (0xf0000) /* to sram (INT) */
  12. #define SMI_LARB_SEC_CON_INT(port) (0xf00 + ((port) << 2))
  13. #define SMI_LARB_SEC_CON(port) (0xf80 + ((port) << 2))
  14. #define MMU_MASK BIT(0)
  15. #define MMU_EN(en) ((!!(en)) << 0)
  16. #define SEC_MASK BIT(1)
  17. #define SEC_EN(en) ((!!(en)) << 1)
  18. #define DOMAIN_MASK (0x1f << 4)
  19. #define SMI_MMU_EN(port) (0x1 << (port))
  20. /* infra master */
  21. #define IFR_CFG_MMU_EN_MSK(r_bit) (0x3 << (r_bit))
  22. /* secure iommu */
  23. #define MMU_INT_CONTROL0 (0x120)
  24. #define INT_CLR BIT(12)
  25. #define MMU_FAULT_ST1 (0x134)
  26. #define MMU_AXI_0_ERR_MASK GENMASK(6, 0)
  27. #define MMU_AXI_FAULT_STATUS(bus) (0x13c + (bus) * 8)
  28. #define MMU_AXI_INVLD_PA(bus) (0x140 + (bus) * 8)
  29. #define MMU_AXI_INT_ID(bus) (0x150 + (bus) * 4)
  30. /* smi larb configure */
  31. /*
  32. * If multimedia security config is enabled, the SMI config register must be
  33. * configurated in security world.
  34. * And the SRAM path is also configurated here to enhance security.
  35. */
  36. #ifdef ATF_MTK_SMI_LARB_CFG_SUPPORT
  37. static void mtk_smi_larb_port_config_to_sram(
  38. const struct mtk_smi_larb_config *larb,
  39. uint32_t port_id)
  40. {
  41. mmio_clrbits_32(larb->base + SMI_LARB_SEC_CON_INT(port_id),
  42. MMU_MASK | SEC_MASK | DOMAIN_MASK);
  43. mmio_setbits_32(larb->base + SMI_LARB_NON_SEC_CON(port_id),
  44. PATH_SEL_MASK);
  45. }
  46. static void mtk_smi_port_config(const struct mtk_smi_larb_config *larb,
  47. uint32_t port_id, uint8_t mmu_en, uint8_t sec_en)
  48. {
  49. mmio_clrsetbits_32(larb->base + SMI_LARB_SEC_CON(port_id),
  50. MMU_MASK | SEC_MASK | DOMAIN_MASK,
  51. MMU_EN(mmu_en) | SEC_EN(sec_en));
  52. }
  53. static int mtk_smi_larb_port_config_sec(uint32_t larb_id, uint32_t mmu_en_msk)
  54. {
  55. uint32_t port_id, port_nr;
  56. const struct mtk_smi_larb_config *larb;
  57. uint32_t to_sram;
  58. uint8_t mmu_en;
  59. if (larb_id >= g_larb_num) {
  60. return MTK_SIP_E_INVALID_PARAM;
  61. }
  62. larb = &g_larb_cfg[larb_id];
  63. port_nr = larb->port_nr;
  64. to_sram = larb->to_sram;
  65. for (port_id = 0; port_id < port_nr; port_id++) {
  66. if ((to_sram & BIT(port_id)) > 0U) {
  67. mtk_smi_larb_port_config_to_sram(larb, port_id);
  68. continue;
  69. }
  70. mmu_en = !!(mmu_en_msk & SMI_MMU_EN(port_id));
  71. mtk_smi_port_config(larb, port_id, mmu_en, 0);
  72. }
  73. return MTK_SIP_E_SUCCESS;
  74. }
  75. #endif /* ATF_MTK_SMI_LARB_CFG_SUPPORT */
  76. /* infra iommu configure */
  77. #ifdef ATF_MTK_INFRA_MASTER_CFG_SUPPORT
  78. static int mtk_infra_master_config_sec(uint32_t dev_id_msk, uint32_t enable)
  79. {
  80. const struct mtk_ifr_mst_config *ifr_cfg;
  81. uint32_t dev_id, reg_addr, reg_mask;
  82. mtk_infra_iommu_enable_protect();
  83. if (dev_id_msk >= BIT(g_ifr_mst_num)) {
  84. return MTK_SIP_E_INVALID_PARAM;
  85. }
  86. for (dev_id = 0U; dev_id < g_ifr_mst_num; dev_id++) {
  87. if ((dev_id_msk & BIT(dev_id)) == 0U) {
  88. continue;
  89. }
  90. ifr_cfg = &g_ifr_mst_cfg[dev_id];
  91. reg_addr = g_ifr_mst_cfg_base[(ifr_cfg->cfg_addr_idx)] +
  92. g_ifr_mst_cfg_offs[(ifr_cfg->cfg_addr_idx)];
  93. reg_mask = IFR_CFG_MMU_EN_MSK(ifr_cfg->r_mmu_en_bit);
  94. if (enable > 0U) {
  95. mmio_setbits_32(reg_addr, reg_mask);
  96. } else {
  97. mmio_clrbits_32(reg_addr, reg_mask);
  98. }
  99. }
  100. return MTK_SIP_E_SUCCESS;
  101. }
  102. #endif /* ATF_MTK_INFRA_MASTER_CFG_SUPPORT */
  103. /* secure iommu */
  104. #ifdef ATF_MTK_IOMMU_CFG_SUPPORT
  105. /* Report secure IOMMU fault status to normal world for the debug version */
  106. static int mtk_secure_iommu_fault_report(uint32_t sec_mmu_base,
  107. uint32_t *f_sta, uint32_t *f_pa,
  108. uint32_t *f_id)
  109. {
  110. const struct mtk_secure_iommu_config *mmu_cfg = NULL;
  111. uint32_t __maybe_unused bus_id, fault_type;
  112. uint32_t i;
  113. int ret = MTK_SIP_E_NOT_SUPPORTED;
  114. for (i = 0; i < g_sec_iommu_num; i++) {
  115. if (g_sec_iommu_cfg[i].base == sec_mmu_base) {
  116. mmu_cfg = &g_sec_iommu_cfg[i];
  117. break;
  118. }
  119. }
  120. if (!mmu_cfg)
  121. return MTK_SIP_E_INVALID_PARAM;
  122. #if DEBUG
  123. fault_type = mmio_read_32(mmu_cfg->base + MMU_FAULT_ST1);
  124. bus_id = (fault_type & MMU_AXI_0_ERR_MASK) ? 0 : 1;
  125. if (f_sta)
  126. *f_sta = mmio_read_32(mmu_cfg->base + MMU_AXI_FAULT_STATUS(bus_id));
  127. if (f_pa)
  128. *f_pa = mmio_read_32(mmu_cfg->base + MMU_AXI_INVLD_PA(bus_id));
  129. if (f_id)
  130. *f_id = mmio_read_32(mmu_cfg->base + MMU_AXI_INT_ID(bus_id));
  131. ret = MTK_SIP_E_SUCCESS;
  132. #endif
  133. mmio_setbits_32(mmu_cfg->base + MMU_INT_CONTROL0, INT_CLR);
  134. return ret;
  135. }
  136. #endif /* ATF_MTK_IOMMU_CFG_SUPPORT */
  137. u_register_t mtk_iommu_handler(u_register_t x1, u_register_t x2,
  138. u_register_t x3, u_register_t x4,
  139. void *handle, struct smccc_res *smccc_ret)
  140. {
  141. uint32_t cmd_id = x1, mdl_id = x2, val = x3;
  142. int ret = MTK_SIP_E_NOT_SUPPORTED;
  143. (void)x4;
  144. (void)handle;
  145. switch (cmd_id) {
  146. #ifdef ATF_MTK_SMI_LARB_CFG_SUPPORT
  147. case IOMMU_ATF_CMD_CONFIG_SMI_LARB:
  148. ret = mtk_smi_larb_port_config_sec(mdl_id, val);
  149. break;
  150. #endif
  151. #ifdef ATF_MTK_INFRA_MASTER_CFG_SUPPORT
  152. case IOMMU_ATF_CMD_CONFIG_INFRA_IOMMU:
  153. ret = mtk_infra_master_config_sec(mdl_id, val);
  154. break;
  155. #endif
  156. #ifdef ATF_MTK_IOMMU_CFG_SUPPORT
  157. case IOMMU_ATF_CMD_GET_SECURE_IOMMU_STATUS:
  158. (void)val;
  159. ret = mtk_secure_iommu_fault_report(mdl_id,
  160. (uint32_t *)&smccc_ret->a1,
  161. (uint32_t *)&smccc_ret->a2,
  162. (uint32_t *)&smccc_ret->a3);
  163. break;
  164. #endif
  165. default:
  166. break;
  167. }
  168. return ret;
  169. }
  170. DECLARE_SMC_HANDLER(MTK_SIP_IOMMU_CONTROL, mtk_iommu_handler);