stm32_rifsc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2023, STMicroelectronics - All Rights Reserved
  4. */
  5. #include <linux/bitfield.h>
  6. #include <linux/bits.h>
  7. #include <linux/device.h>
  8. #include <linux/err.h>
  9. #include <linux/init.h>
  10. #include <linux/io.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_platform.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/types.h>
  17. #include "stm32_firewall.h"
  18. /*
  19. * RIFSC offset register
  20. */
  21. #define RIFSC_RISC_SECCFGR0 0x10
  22. #define RIFSC_RISC_PRIVCFGR0 0x30
  23. #define RIFSC_RISC_PER0_CIDCFGR 0x100
  24. #define RIFSC_RISC_PER0_SEMCR 0x104
  25. #define RIFSC_RISC_HWCFGR2 0xFEC
  26. /*
  27. * SEMCR register
  28. */
  29. #define SEMCR_MUTEX BIT(0)
  30. /*
  31. * HWCFGR2 register
  32. */
  33. #define HWCFGR2_CONF1_MASK GENMASK(15, 0)
  34. #define HWCFGR2_CONF2_MASK GENMASK(23, 16)
  35. #define HWCFGR2_CONF3_MASK GENMASK(31, 24)
  36. /*
  37. * RIFSC miscellaneous
  38. */
  39. #define RIFSC_RISC_CFEN_MASK BIT(0)
  40. #define RIFSC_RISC_SEM_EN_MASK BIT(1)
  41. #define RIFSC_RISC_SCID_MASK GENMASK(6, 4)
  42. #define RIFSC_RISC_SEML_SHIFT 16
  43. #define RIFSC_RISC_SEMWL_MASK GENMASK(23, 16)
  44. #define RIFSC_RISC_PER_ID_MASK GENMASK(31, 24)
  45. #define RIFSC_RISC_PERx_CID_MASK (RIFSC_RISC_CFEN_MASK | \
  46. RIFSC_RISC_SEM_EN_MASK | \
  47. RIFSC_RISC_SCID_MASK | \
  48. RIFSC_RISC_SEMWL_MASK)
  49. #define IDS_PER_RISC_SEC_PRIV_REGS 32
  50. /* RIF miscellaneous */
  51. /*
  52. * CIDCFGR register fields
  53. */
  54. #define CIDCFGR_CFEN BIT(0)
  55. #define CIDCFGR_SEMEN BIT(1)
  56. #define CIDCFGR_SEMWL(x) BIT(RIFSC_RISC_SEML_SHIFT + (x))
  57. #define SEMWL_SHIFT 16
  58. /* Compartiment IDs */
  59. #define RIF_CID0 0x0
  60. #define RIF_CID1 0x1
  61. static bool stm32_rifsc_is_semaphore_available(void __iomem *addr)
  62. {
  63. return !(readl(addr) & SEMCR_MUTEX);
  64. }
  65. static int stm32_rif_acquire_semaphore(struct stm32_firewall_controller *stm32_firewall_controller,
  66. int id)
  67. {
  68. void __iomem *addr = stm32_firewall_controller->mmio + RIFSC_RISC_PER0_SEMCR + 0x8 * id;
  69. writel(SEMCR_MUTEX, addr);
  70. /* Check that CID1 has the semaphore */
  71. if (stm32_rifsc_is_semaphore_available(addr) ||
  72. FIELD_GET(RIFSC_RISC_SCID_MASK, readl(addr)) != RIF_CID1)
  73. return -EACCES;
  74. return 0;
  75. }
  76. static void stm32_rif_release_semaphore(struct stm32_firewall_controller *stm32_firewall_controller,
  77. int id)
  78. {
  79. void __iomem *addr = stm32_firewall_controller->mmio + RIFSC_RISC_PER0_SEMCR + 0x8 * id;
  80. if (stm32_rifsc_is_semaphore_available(addr))
  81. return;
  82. writel(SEMCR_MUTEX, addr);
  83. /* Ok if another compartment takes the semaphore before the check */
  84. WARN_ON(!stm32_rifsc_is_semaphore_available(addr) &&
  85. FIELD_GET(RIFSC_RISC_SCID_MASK, readl(addr)) == RIF_CID1);
  86. }
  87. static int stm32_rifsc_grant_access(struct stm32_firewall_controller *ctrl, u32 firewall_id)
  88. {
  89. struct stm32_firewall_controller *rifsc_controller = ctrl;
  90. u32 reg_offset, reg_id, sec_reg_value, cid_reg_value;
  91. int rc;
  92. if (firewall_id >= rifsc_controller->max_entries) {
  93. dev_err(rifsc_controller->dev, "Invalid sys bus ID %u", firewall_id);
  94. return -EINVAL;
  95. }
  96. /*
  97. * RIFSC_RISC_PRIVCFGRx and RIFSC_RISC_SECCFGRx both handle configuration access for
  98. * 32 peripherals. On the other hand, there is one _RIFSC_RISC_PERx_CIDCFGR register
  99. * per peripheral
  100. */
  101. reg_id = firewall_id / IDS_PER_RISC_SEC_PRIV_REGS;
  102. reg_offset = firewall_id % IDS_PER_RISC_SEC_PRIV_REGS;
  103. sec_reg_value = readl(rifsc_controller->mmio + RIFSC_RISC_SECCFGR0 + 0x4 * reg_id);
  104. cid_reg_value = readl(rifsc_controller->mmio + RIFSC_RISC_PER0_CIDCFGR + 0x8 * firewall_id);
  105. /* First check conditions for semaphore mode, which doesn't take into account static CID. */
  106. if ((cid_reg_value & CIDCFGR_SEMEN) && (cid_reg_value & CIDCFGR_CFEN)) {
  107. if (cid_reg_value & BIT(RIF_CID1 + SEMWL_SHIFT)) {
  108. /* Static CID is irrelevant if semaphore mode */
  109. goto skip_cid_check;
  110. } else {
  111. dev_dbg(rifsc_controller->dev,
  112. "Invalid bus semaphore configuration: index %d\n", firewall_id);
  113. return -EACCES;
  114. }
  115. }
  116. /*
  117. * Skip CID check if CID filtering isn't enabled or filtering is enabled on CID0, which
  118. * corresponds to whatever CID.
  119. */
  120. if (!(cid_reg_value & CIDCFGR_CFEN) ||
  121. FIELD_GET(RIFSC_RISC_SCID_MASK, cid_reg_value) == RIF_CID0)
  122. goto skip_cid_check;
  123. /* Coherency check with the CID configuration */
  124. if (FIELD_GET(RIFSC_RISC_SCID_MASK, cid_reg_value) != RIF_CID1) {
  125. dev_dbg(rifsc_controller->dev, "Invalid CID configuration for peripheral: %d\n",
  126. firewall_id);
  127. return -EACCES;
  128. }
  129. skip_cid_check:
  130. /* Check security configuration */
  131. if (sec_reg_value & BIT(reg_offset)) {
  132. dev_dbg(rifsc_controller->dev,
  133. "Invalid security configuration for peripheral: %d\n", firewall_id);
  134. return -EACCES;
  135. }
  136. /*
  137. * If the peripheral is in semaphore mode, take the semaphore so that
  138. * the CID1 has the ownership.
  139. */
  140. if ((cid_reg_value & CIDCFGR_SEMEN) && (cid_reg_value & CIDCFGR_CFEN)) {
  141. rc = stm32_rif_acquire_semaphore(rifsc_controller, firewall_id);
  142. if (rc) {
  143. dev_err(rifsc_controller->dev,
  144. "Couldn't acquire semaphore for peripheral: %d\n", firewall_id);
  145. return rc;
  146. }
  147. }
  148. return 0;
  149. }
  150. static void stm32_rifsc_release_access(struct stm32_firewall_controller *ctrl, u32 firewall_id)
  151. {
  152. stm32_rif_release_semaphore(ctrl, firewall_id);
  153. }
  154. static int stm32_rifsc_probe(struct platform_device *pdev)
  155. {
  156. struct stm32_firewall_controller *rifsc_controller;
  157. struct device_node *np = pdev->dev.of_node;
  158. u32 nb_risup, nb_rimu, nb_risal;
  159. struct resource *res;
  160. void __iomem *mmio;
  161. int rc;
  162. rifsc_controller = devm_kzalloc(&pdev->dev, sizeof(*rifsc_controller), GFP_KERNEL);
  163. if (!rifsc_controller)
  164. return -ENOMEM;
  165. mmio = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  166. if (IS_ERR(mmio))
  167. return PTR_ERR(mmio);
  168. rifsc_controller->dev = &pdev->dev;
  169. rifsc_controller->mmio = mmio;
  170. rifsc_controller->name = dev_driver_string(rifsc_controller->dev);
  171. rifsc_controller->type = STM32_PERIPHERAL_FIREWALL | STM32_MEMORY_FIREWALL;
  172. rifsc_controller->grant_access = stm32_rifsc_grant_access;
  173. rifsc_controller->release_access = stm32_rifsc_release_access;
  174. /* Get number of RIFSC entries*/
  175. nb_risup = readl(rifsc_controller->mmio + RIFSC_RISC_HWCFGR2) & HWCFGR2_CONF1_MASK;
  176. nb_rimu = readl(rifsc_controller->mmio + RIFSC_RISC_HWCFGR2) & HWCFGR2_CONF2_MASK;
  177. nb_risal = readl(rifsc_controller->mmio + RIFSC_RISC_HWCFGR2) & HWCFGR2_CONF3_MASK;
  178. rifsc_controller->max_entries = nb_risup + nb_rimu + nb_risal;
  179. platform_set_drvdata(pdev, rifsc_controller);
  180. rc = stm32_firewall_controller_register(rifsc_controller);
  181. if (rc) {
  182. dev_err(rifsc_controller->dev, "Couldn't register as a firewall controller: %d",
  183. rc);
  184. return rc;
  185. }
  186. rc = stm32_firewall_populate_bus(rifsc_controller);
  187. if (rc) {
  188. dev_err(rifsc_controller->dev, "Couldn't populate RIFSC bus: %d",
  189. rc);
  190. return rc;
  191. }
  192. /* Populate all allowed nodes */
  193. return of_platform_populate(np, NULL, NULL, &pdev->dev);
  194. }
  195. static const struct of_device_id stm32_rifsc_of_match[] = {
  196. { .compatible = "st,stm32mp25-rifsc" },
  197. {}
  198. };
  199. MODULE_DEVICE_TABLE(of, stm32_rifsc_of_match);
  200. static struct platform_driver stm32_rifsc_driver = {
  201. .probe = stm32_rifsc_probe,
  202. .driver = {
  203. .name = "stm32-rifsc",
  204. .of_match_table = stm32_rifsc_of_match,
  205. },
  206. };
  207. module_platform_driver(stm32_rifsc_driver);
  208. MODULE_AUTHOR("Gatien Chevallier <gatien.chevallier@foss.st.com>");
  209. MODULE_DESCRIPTION("STMicroelectronics RIFSC driver");
  210. MODULE_LICENSE("GPL");