mtk_iommu_smc.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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_plat.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. /* smi larb configure */
  23. /*
  24. * If multimedia security config is enabled, the SMI config register must be
  25. * configurated in security world.
  26. * And the SRAM path is also configurated here to enhance security.
  27. */
  28. static void mtk_smi_larb_port_config_to_sram(
  29. const struct mtk_smi_larb_config *larb,
  30. uint32_t port_id)
  31. {
  32. mmio_clrbits_32(larb->base + SMI_LARB_SEC_CON_INT(port_id),
  33. MMU_MASK | SEC_MASK | DOMAIN_MASK);
  34. mmio_setbits_32(larb->base + SMI_LARB_NON_SEC_CON(port_id),
  35. PATH_SEL_MASK);
  36. }
  37. static void mtk_smi_port_config(const struct mtk_smi_larb_config *larb,
  38. uint32_t port_id, uint8_t mmu_en, uint8_t sec_en)
  39. {
  40. mmio_clrsetbits_32(larb->base + SMI_LARB_SEC_CON(port_id),
  41. MMU_MASK | SEC_MASK | DOMAIN_MASK,
  42. MMU_EN(mmu_en) | SEC_EN(sec_en));
  43. }
  44. static int mtk_smi_larb_port_config_sec(uint32_t larb_id, uint32_t mmu_en_msk)
  45. {
  46. uint32_t port_id, port_nr;
  47. const struct mtk_smi_larb_config *larb;
  48. uint32_t to_sram;
  49. uint8_t mmu_en;
  50. if (larb_id >= SMI_LARB_NUM) {
  51. return MTK_SIP_E_INVALID_PARAM;
  52. }
  53. larb = &g_larb_cfg[larb_id];
  54. port_nr = larb->port_nr;
  55. to_sram = larb->to_sram;
  56. for (port_id = 0; port_id < port_nr; port_id++) {
  57. if ((to_sram & BIT(port_id)) > 0U) {
  58. mtk_smi_larb_port_config_to_sram(larb, port_id);
  59. continue;
  60. }
  61. mmu_en = !!(mmu_en_msk & SMI_MMU_EN(port_id));
  62. mtk_smi_port_config(larb, port_id, mmu_en, 0);
  63. }
  64. return MTK_SIP_E_SUCCESS;
  65. }
  66. static int mtk_infra_master_config_sec(uint32_t dev_id_msk, uint32_t enable)
  67. {
  68. const struct mtk_ifr_mst_config *ifr_cfg;
  69. uint32_t dev_id, reg_addr, reg_mask;
  70. mtk_infra_iommu_enable_protect();
  71. if (dev_id_msk >= BIT(MMU_DEV_NUM)) {
  72. return MTK_SIP_E_INVALID_PARAM;
  73. }
  74. for (dev_id = 0U; dev_id < MMU_DEV_NUM; dev_id++) {
  75. if ((dev_id_msk & BIT(dev_id)) == 0U) {
  76. continue;
  77. }
  78. ifr_cfg = &g_ifr_mst_cfg[dev_id];
  79. reg_addr = g_ifr_mst_cfg_base[(ifr_cfg->cfg_addr_idx)] +
  80. g_ifr_mst_cfg_offs[(ifr_cfg->cfg_addr_idx)];
  81. reg_mask = IFR_CFG_MMU_EN_MSK(ifr_cfg->r_mmu_en_bit);
  82. if (enable > 0U) {
  83. mmio_setbits_32(reg_addr, reg_mask);
  84. } else {
  85. mmio_clrbits_32(reg_addr, reg_mask);
  86. }
  87. }
  88. return MTK_SIP_E_SUCCESS;
  89. }
  90. static u_register_t mtk_iommu_handler(u_register_t x1, u_register_t x2,
  91. u_register_t x3, u_register_t x4,
  92. void *handle, struct smccc_res *smccc_ret)
  93. {
  94. uint32_t cmd_id = x1, mdl_id = x2, val = x3;
  95. int ret = MTK_SIP_E_NOT_SUPPORTED;
  96. (void)x4;
  97. (void)handle;
  98. switch (cmd_id) {
  99. case IOMMU_ATF_CMD_CONFIG_SMI_LARB:
  100. ret = mtk_smi_larb_port_config_sec(mdl_id, val);
  101. break;
  102. case IOMMU_ATF_CMD_CONFIG_INFRA_IOMMU:
  103. ret = mtk_infra_master_config_sec(mdl_id, val);
  104. break;
  105. default:
  106. break;
  107. }
  108. return ret;
  109. }
  110. DECLARE_SMC_HANDLER(MTK_SIP_IOMMU_CONTROL, mtk_iommu_handler);