stm32_rifsc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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/debugfs.h>
  8. #include <linux/device.h>
  9. #include <linux/err.h>
  10. #include <linux/init.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/of.h>
  15. #include <linux/of_platform.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/types.h>
  18. #include "stm32_firewall.h"
  19. /*
  20. * RIFSC offset register
  21. */
  22. #define RIFSC_RISC_SECCFGR0 0x10
  23. #define RIFSC_RISC_PRIVCFGR0 0x30
  24. #define RIFSC_RISC_PER0_CIDCFGR 0x100
  25. #define RIFSC_RISC_PER0_SEMCR 0x104
  26. #define RIFSC_RISC_REG0_ACFGR 0x900
  27. #define RIFSC_RISC_REG3_AADDR 0x924
  28. #define RIFSC_RISC_HWCFGR2 0xFEC
  29. /*
  30. * SEMCR register
  31. */
  32. #define SEMCR_MUTEX BIT(0)
  33. /*
  34. * HWCFGR2 register
  35. */
  36. #define HWCFGR2_CONF1_MASK GENMASK(15, 0)
  37. #define HWCFGR2_CONF2_MASK GENMASK(23, 16)
  38. #define HWCFGR2_CONF3_MASK GENMASK(31, 24)
  39. /*
  40. * RIFSC miscellaneous
  41. */
  42. #define RIFSC_RISC_CFEN_MASK BIT(0)
  43. #define RIFSC_RISC_SEM_EN_MASK BIT(1)
  44. #define RIFSC_RISC_SCID_MASK GENMASK(6, 4)
  45. #define RIFSC_RISC_SEML_SHIFT 16
  46. #define RIFSC_RISC_SEMWL_MASK GENMASK(23, 16)
  47. #define RIFSC_RISC_PER_ID_MASK GENMASK(31, 24)
  48. #define RIFSC_RISC_PERx_CID_MASK (RIFSC_RISC_CFEN_MASK | \
  49. RIFSC_RISC_SEM_EN_MASK | \
  50. RIFSC_RISC_SCID_MASK | \
  51. RIFSC_RISC_SEMWL_MASK)
  52. #define IDS_PER_RISC_SEC_PRIV_REGS 32
  53. /* RIF miscellaneous */
  54. /*
  55. * CIDCFGR register fields
  56. */
  57. #define CIDCFGR_CFEN BIT(0)
  58. #define CIDCFGR_SEMEN BIT(1)
  59. #define CIDCFGR_SEMWL(x) BIT(RIFSC_RISC_SEML_SHIFT + (x))
  60. #define SEMWL_SHIFT 16
  61. /* Compartiment IDs */
  62. #define RIF_CID0 0x0
  63. #define RIF_CID1 0x1
  64. #if defined(CONFIG_DEBUG_FS)
  65. #define RIFSC_RISUP_ENTRIES 128
  66. #define RIFSC_RIMU_ENTRIES 16
  67. #define RIFSC_RISAL_SUBREGIONS 2
  68. #define RIFSC_RISAL_GRANULARITY 8
  69. #define RIFSC_RIMC_ATTR0 0xC10
  70. #define RIFSC_RIMC_CIDSEL BIT(2)
  71. #define RIFSC_RIMC_MCID_MASK GENMASK(6, 4)
  72. #define RIFSC_RIMC_MSEC BIT(8)
  73. #define RIFSC_RIMC_MPRIV BIT(9)
  74. #define RIFSC_RISC_SRCID_MASK GENMASK(6, 4)
  75. #define RIFSC_RISC_SRPRIV BIT(9)
  76. #define RIFSC_RISC_SRSEC BIT(8)
  77. #define RIFSC_RISC_SRRLOCK BIT(1)
  78. #define RIFSC_RISC_SREN BIT(0)
  79. #define RIFSC_RISC_SRLENGTH_MASK GENMASK(27, 16)
  80. #define RIFSC_RISC_SRSTART_MASK GENMASK(10, 0)
  81. static const char *stm32mp21_rifsc_rimu_names[RIFSC_RIMU_ENTRIES] = {
  82. "ETR",
  83. "SDMMC1",
  84. "SDMMC2",
  85. "SDMMC3",
  86. "OTG_HS",
  87. "USBH",
  88. "ETH1",
  89. "ETH2",
  90. "RESERVED",
  91. "RESERVED",
  92. "DCMIPP",
  93. "LTDC_L1/L2",
  94. "LTDC_L3",
  95. "RESERVED",
  96. "RESERVED",
  97. "RESERVED",
  98. };
  99. static const char *stm32mp25_rifsc_rimu_names[RIFSC_RIMU_ENTRIES] = {
  100. "ETR",
  101. "SDMMC1",
  102. "SDMMC2",
  103. "SDMMC3",
  104. "USB3DR",
  105. "USBH",
  106. "ETH1",
  107. "ETH2",
  108. "PCIE",
  109. "GPU",
  110. "DMCIPP",
  111. "LTDC_L0/L1",
  112. "LTDC_L2",
  113. "LTDC_ROT",
  114. "VDEC",
  115. "VENC"
  116. };
  117. static const char *stm32mp21_rifsc_risup_names[RIFSC_RISUP_ENTRIES] = {
  118. "TIM1",
  119. "TIM2",
  120. "TIM3",
  121. "TIM4",
  122. "TIM5",
  123. "TIM6",
  124. "TIM7",
  125. "TIM8",
  126. "TIM10",
  127. "TIM11",
  128. "TIM12",
  129. "TIM13",
  130. "TIM14",
  131. "TIM15",
  132. "TIM16",
  133. "TIM17",
  134. "RESERVED",
  135. "LPTIM1",
  136. "LPTIM2",
  137. "LPTIM3",
  138. "LPTIM4",
  139. "LPTIM5",
  140. "SPI1",
  141. "SPI2",
  142. "SPI3",
  143. "SPI4",
  144. "SPI5",
  145. "SPI6",
  146. "RESERVED",
  147. "RESERVED",
  148. "SPDIFRX",
  149. "USART1",
  150. "USART2",
  151. "USART3",
  152. "UART4",
  153. "UART5",
  154. "USART6",
  155. "UART7",
  156. "RESERVED",
  157. "RESERVED",
  158. "LPUART1",
  159. "I2C1",
  160. "I2C2",
  161. "I2C3",
  162. "RESERVED",
  163. "RESERVED",
  164. "RESERVED",
  165. "RESERVED",
  166. "RESERVED",
  167. "SAI1",
  168. "SAI2",
  169. "SAI3",
  170. "SAI4",
  171. "RESERVED",
  172. "MDF1",
  173. "RESERVED",
  174. "FDCAN",
  175. "HDP",
  176. "ADC1",
  177. "ADC2",
  178. "ETH1",
  179. "ETH2",
  180. "RESERVED",
  181. "USBH",
  182. "RESERVED",
  183. "RESERVED",
  184. "OTG_HS",
  185. "DDRPERFM",
  186. "RESERVED",
  187. "RESERVED",
  188. "RESERVED",
  189. "RESERVED",
  190. "RESERVED",
  191. "STGEN",
  192. "OCTOSPI1",
  193. "RESERVED",
  194. "SDMMC1",
  195. "SDMMC2",
  196. "SDMMC3",
  197. "RESERVED",
  198. "LTDC_CMN",
  199. "RESERVED",
  200. "RESERVED",
  201. "RESERVED",
  202. "RESERVED",
  203. "RESERVED",
  204. "CSI",
  205. "DCMIPP",
  206. "DCMI_PSSI",
  207. "RESERVED",
  208. "RESERVED",
  209. "RESERVED",
  210. "RNG1",
  211. "RNG2",
  212. "PKA",
  213. "SAES",
  214. "HASH1",
  215. "HASH2",
  216. "CRYP1",
  217. "CRYP2",
  218. "IWDG1",
  219. "IWDG2",
  220. "IWDG3",
  221. "IWDG4",
  222. "WWDG1",
  223. "RESERVED",
  224. "VREFBUF",
  225. "DTS",
  226. "RAMCFG",
  227. "CRC",
  228. "SERC",
  229. "RESERVED",
  230. "RESERVED",
  231. "RESERVED",
  232. "I3C1",
  233. "I3C2",
  234. "I3C3",
  235. "RESERVED",
  236. "ICACHE_DCACHE",
  237. "LTDC_L1L2",
  238. "LTDC_L3",
  239. "RESERVED",
  240. "RESERVED",
  241. "RESERVED",
  242. "RESERVED",
  243. "OTFDEC1",
  244. "RESERVED",
  245. "IAC",
  246. };
  247. static const char *stm32mp25_rifsc_risup_names[RIFSC_RISUP_ENTRIES] = {
  248. "TIM1",
  249. "TIM2",
  250. "TIM3",
  251. "TIM4",
  252. "TIM5",
  253. "TIM6",
  254. "TIM7",
  255. "TIM8",
  256. "TIM10",
  257. "TIM11",
  258. "TIM12",
  259. "TIM13",
  260. "TIM14",
  261. "TIM15",
  262. "TIM16",
  263. "TIM17",
  264. "TIM20",
  265. "LPTIM1",
  266. "LPTIM2",
  267. "LPTIM3",
  268. "LPTIM4",
  269. "LPTIM5",
  270. "SPI1",
  271. "SPI2",
  272. "SPI3",
  273. "SPI4",
  274. "SPI5",
  275. "SPI6",
  276. "SPI7",
  277. "SPI8",
  278. "SPDIFRX",
  279. "USART1",
  280. "USART2",
  281. "USART3",
  282. "UART4",
  283. "UART5",
  284. "USART6",
  285. "UART7",
  286. "UART8",
  287. "UART9",
  288. "LPUART1",
  289. "I2C1",
  290. "I2C2",
  291. "I2C3",
  292. "I2C4",
  293. "I2C5",
  294. "I2C6",
  295. "I2C7",
  296. "I2C8",
  297. "SAI1",
  298. "SAI2",
  299. "SAI3",
  300. "SAI4",
  301. "RESERVED",
  302. "MDF1",
  303. "ADF1",
  304. "FDCAN",
  305. "HDP",
  306. "ADC12",
  307. "ADC3",
  308. "ETH1",
  309. "ETH2",
  310. "RESERVED",
  311. "USBH",
  312. "RESERVED",
  313. "RESERVED",
  314. "USB3DR",
  315. "COMBOPHY",
  316. "PCIE",
  317. "UCPD1",
  318. "ETHSW_DEIP",
  319. "ETHSW_ACM_CF",
  320. "ETHSW_ACM_MSGBU",
  321. "STGEN",
  322. "OCTOSPI1",
  323. "OCTOSPI2",
  324. "SDMMC1",
  325. "SDMMC2",
  326. "SDMMC3",
  327. "GPU",
  328. "LTDC_CMN",
  329. "DSI_CMN",
  330. "RESERVED",
  331. "RESERVED",
  332. "LVDS",
  333. "RESERVED",
  334. "CSI",
  335. "DCMIPP",
  336. "DCMI_PSSI",
  337. "VDEC",
  338. "VENC",
  339. "RESERVED",
  340. "RNG",
  341. "PKA",
  342. "SAES",
  343. "HASH",
  344. "CRYP1",
  345. "CRYP2",
  346. "IWDG1",
  347. "IWDG2",
  348. "IWDG3",
  349. "IWDG4",
  350. "IWDG5",
  351. "WWDG1",
  352. "WWDG2",
  353. "RESERVED",
  354. "VREFBUF",
  355. "DTS",
  356. "RAMCFG",
  357. "CRC",
  358. "SERC",
  359. "OCTOSPIM",
  360. "GICV2M",
  361. "RESERVED",
  362. "I3C1",
  363. "I3C2",
  364. "I3C3",
  365. "I3C4",
  366. "ICACHE_DCACHE",
  367. "LTDC_L0L1",
  368. "LTDC_L2",
  369. "LTDC_ROT",
  370. "DSI_TRIG",
  371. "DSI_RDFIFO",
  372. "RESERVED",
  373. "OTFDEC1",
  374. "OTFDEC2",
  375. "IAC",
  376. };
  377. struct rifsc_risup_debug_data {
  378. char dev_name[15];
  379. u8 dev_cid;
  380. u8 dev_sem_cids;
  381. u8 dev_id;
  382. bool dev_cid_filt_en;
  383. bool dev_sem_en;
  384. bool dev_priv;
  385. bool dev_sec;
  386. };
  387. struct rifsc_rimu_debug_data {
  388. char m_name[11];
  389. u8 m_cid;
  390. bool cidsel;
  391. bool m_sec;
  392. bool m_priv;
  393. };
  394. struct rifsc_subreg_debug_data {
  395. bool sr_sec;
  396. bool sr_priv;
  397. u8 sr_cid;
  398. bool sr_rlock;
  399. bool sr_enable;
  400. u16 sr_start;
  401. u16 sr_length;
  402. };
  403. struct stm32_rifsc_resources_names {
  404. const char **device_names;
  405. const char **initiator_names;
  406. };
  407. struct rifsc_dbg_private {
  408. const struct stm32_rifsc_resources_names *res_names;
  409. void __iomem *mmio;
  410. unsigned int nb_risup;
  411. unsigned int nb_rimu;
  412. unsigned int nb_risal;
  413. };
  414. static const struct stm32_rifsc_resources_names rifsc_mp21_res_names = {
  415. .device_names = stm32mp21_rifsc_risup_names,
  416. .initiator_names = stm32mp21_rifsc_rimu_names,
  417. };
  418. static const struct stm32_rifsc_resources_names rifsc_mp25_res_names = {
  419. .device_names = stm32mp25_rifsc_risup_names,
  420. .initiator_names = stm32mp25_rifsc_rimu_names,
  421. };
  422. static void stm32_rifsc_fill_rimu_dbg_entry(struct rifsc_dbg_private *rifsc,
  423. struct rifsc_rimu_debug_data *dbg_entry, int i)
  424. {
  425. const struct stm32_rifsc_resources_names *dbg_names = rifsc->res_names;
  426. u32 rimc_attr = readl_relaxed(rifsc->mmio + RIFSC_RIMC_ATTR0 + 0x4 * i);
  427. snprintf(dbg_entry->m_name, sizeof(dbg_entry->m_name), "%s", dbg_names->initiator_names[i]);
  428. dbg_entry->m_cid = FIELD_GET(RIFSC_RIMC_MCID_MASK, rimc_attr);
  429. dbg_entry->cidsel = rimc_attr & RIFSC_RIMC_CIDSEL;
  430. dbg_entry->m_sec = rimc_attr & RIFSC_RIMC_MSEC;
  431. dbg_entry->m_priv = rimc_attr & RIFSC_RIMC_MPRIV;
  432. }
  433. static void stm32_rifsc_fill_dev_dbg_entry(struct rifsc_dbg_private *rifsc,
  434. struct rifsc_risup_debug_data *dbg_entry, int i)
  435. {
  436. const struct stm32_rifsc_resources_names *dbg_names = rifsc->res_names;
  437. u32 cid_cfgr, sec_cfgr, priv_cfgr;
  438. u8 reg_id = i / IDS_PER_RISC_SEC_PRIV_REGS;
  439. u8 reg_offset = i % IDS_PER_RISC_SEC_PRIV_REGS;
  440. cid_cfgr = readl_relaxed(rifsc->mmio + RIFSC_RISC_PER0_CIDCFGR + 0x8 * i);
  441. sec_cfgr = readl_relaxed(rifsc->mmio + RIFSC_RISC_SECCFGR0 + 0x4 * reg_id);
  442. priv_cfgr = readl_relaxed(rifsc->mmio + RIFSC_RISC_PRIVCFGR0 + 0x4 * reg_id);
  443. snprintf(dbg_entry->dev_name, sizeof(dbg_entry->dev_name), "%s",
  444. dbg_names->device_names[i]);
  445. dbg_entry->dev_id = i;
  446. dbg_entry->dev_cid_filt_en = cid_cfgr & CIDCFGR_CFEN;
  447. dbg_entry->dev_sem_en = cid_cfgr & CIDCFGR_SEMEN;
  448. dbg_entry->dev_cid = FIELD_GET(RIFSC_RISC_SCID_MASK, cid_cfgr);
  449. dbg_entry->dev_sem_cids = FIELD_GET(RIFSC_RISC_SEMWL_MASK, cid_cfgr);
  450. dbg_entry->dev_sec = sec_cfgr & BIT(reg_offset) ? true : false;
  451. dbg_entry->dev_priv = priv_cfgr & BIT(reg_offset) ? true : false;
  452. }
  453. static void stm32_rifsc_fill_subreg_dbg_entry(struct rifsc_dbg_private *rifsc,
  454. struct rifsc_subreg_debug_data *dbg_entry, int i,
  455. int j)
  456. {
  457. u32 risc_xcfgr = readl_relaxed(rifsc->mmio + RIFSC_RISC_REG0_ACFGR + 0x10 * i + 0x8 * j);
  458. u32 risc_xaddr;
  459. dbg_entry->sr_sec = risc_xcfgr & RIFSC_RISC_SRSEC;
  460. dbg_entry->sr_priv = risc_xcfgr & RIFSC_RISC_SRPRIV;
  461. dbg_entry->sr_cid = FIELD_GET(RIFSC_RISC_SRCID_MASK, risc_xcfgr);
  462. dbg_entry->sr_rlock = risc_xcfgr & RIFSC_RISC_SRRLOCK;
  463. dbg_entry->sr_enable = risc_xcfgr & RIFSC_RISC_SREN;
  464. if (i == 2) {
  465. risc_xaddr = readl_relaxed(rifsc->mmio + RIFSC_RISC_REG3_AADDR + 0x8 * j);
  466. dbg_entry->sr_length = FIELD_GET(RIFSC_RISC_SRLENGTH_MASK, risc_xaddr);
  467. dbg_entry->sr_start = FIELD_GET(RIFSC_RISC_SRSTART_MASK, risc_xaddr);
  468. } else {
  469. dbg_entry->sr_start = 0;
  470. dbg_entry->sr_length = U16_MAX;
  471. }
  472. }
  473. static int stm32_rifsc_conf_dump_show(struct seq_file *s, void *data)
  474. {
  475. struct rifsc_dbg_private *rifsc = (struct rifsc_dbg_private *)s->private;
  476. int i, j;
  477. seq_puts(s, "\n=============================================\n");
  478. seq_puts(s, " RIFSC dump\n");
  479. seq_puts(s, "=============================================\n\n");
  480. seq_puts(s, "\n=============================================\n");
  481. seq_puts(s, " RISUP dump\n");
  482. seq_puts(s, "=============================================\n");
  483. seq_printf(s, "\n| %-15s |", "Peripheral name");
  484. seq_puts(s, "| Firewall ID |");
  485. seq_puts(s, "| N/SECURE |");
  486. seq_puts(s, "| N/PRIVILEGED |");
  487. seq_puts(s, "| CID filtering |");
  488. seq_puts(s, "| Semaphore mode |");
  489. seq_puts(s, "| SCID |");
  490. seq_printf(s, "| %7s |\n", "SEMWL");
  491. for (i = 0; i < RIFSC_RISUP_ENTRIES && i < rifsc->nb_risup; i++) {
  492. struct rifsc_risup_debug_data d_dbg_entry;
  493. stm32_rifsc_fill_dev_dbg_entry(rifsc, &d_dbg_entry, i);
  494. seq_printf(s, "| %-15s |", d_dbg_entry.dev_name);
  495. seq_printf(s, "| %-11d |", d_dbg_entry.dev_id);
  496. seq_printf(s, "| %-8s |", d_dbg_entry.dev_sec ? "SEC" : "NSEC");
  497. seq_printf(s, "| %-12s |", d_dbg_entry.dev_priv ? "PRIV" : "NPRIV");
  498. seq_printf(s, "| %-13s |", str_enabled_disabled(d_dbg_entry.dev_cid_filt_en));
  499. seq_printf(s, "| %-14s |", str_enabled_disabled(d_dbg_entry.dev_sem_en));
  500. seq_printf(s, "| %-4d |", d_dbg_entry.dev_cid);
  501. seq_printf(s, "| %#-7x |\n", d_dbg_entry.dev_sem_cids);
  502. }
  503. seq_puts(s, "\n=============================================\n");
  504. seq_puts(s, " RIMU dump\n");
  505. seq_puts(s, "=============================================\n");
  506. seq_puts(s, "| RIMU's name |");
  507. seq_puts(s, "| CIDSEL |");
  508. seq_puts(s, "| MCID |");
  509. seq_puts(s, "| N/SECURE |");
  510. seq_puts(s, "| N/PRIVILEGED |\n");
  511. for (i = 0; i < RIFSC_RIMU_ENTRIES && rifsc->nb_rimu; i++) {
  512. struct rifsc_rimu_debug_data m_dbg_entry;
  513. stm32_rifsc_fill_rimu_dbg_entry(rifsc, &m_dbg_entry, i);
  514. seq_printf(s, "| %-11s |", m_dbg_entry.m_name);
  515. seq_printf(s, "| %-6s |", m_dbg_entry.cidsel ? "CIDSEL" : "");
  516. seq_printf(s, "| %-4d |", m_dbg_entry.m_cid);
  517. seq_printf(s, "| %-8s |", m_dbg_entry.m_sec ? "SEC" : "NSEC");
  518. seq_printf(s, "| %-12s |\n", m_dbg_entry.m_priv ? "PRIV" : "NPRIV");
  519. }
  520. if (rifsc->nb_risal > 0) {
  521. seq_puts(s, "\n=============================================\n");
  522. seq_puts(s, " RISAL dump\n");
  523. seq_puts(s, "=============================================\n");
  524. seq_puts(s, "| Memory |");
  525. seq_puts(s, "| Subreg. |");
  526. seq_puts(s, "| N/SECURE |");
  527. seq_puts(s, "| N/PRIVILEGED |");
  528. seq_puts(s, "| Subreg. CID |");
  529. seq_puts(s, "| Resource lock |");
  530. seq_puts(s, "| Subreg. enable |");
  531. seq_puts(s, "| Subreg. start |");
  532. seq_puts(s, "| Subreg. end |\n");
  533. for (i = 0; i < rifsc->nb_risal; i++) {
  534. for (j = 0; j < RIFSC_RISAL_SUBREGIONS; j++) {
  535. struct rifsc_subreg_debug_data sr_dbg_entry;
  536. stm32_rifsc_fill_subreg_dbg_entry(rifsc, &sr_dbg_entry, i, j);
  537. seq_printf(s, "| LPSRAM%1d |", i + 1);
  538. seq_printf(s, "| %1s |", (j == 0) ? "A" : "B");
  539. seq_printf(s, "| %-8s |", sr_dbg_entry.sr_sec ? "SEC" : "NSEC");
  540. seq_printf(s, "| %-12s |", sr_dbg_entry.sr_priv ? "PRIV" : "NPRIV");
  541. seq_printf(s, "| 0x%-9x |", sr_dbg_entry.sr_cid);
  542. seq_printf(s, "| %-13s |",
  543. sr_dbg_entry.sr_rlock ? "locked (1)" : "unlocked (0)");
  544. seq_printf(s, "| %-14s |",
  545. str_enabled_disabled(sr_dbg_entry.sr_enable));
  546. seq_printf(s, "| 0x%-11x |", sr_dbg_entry.sr_start);
  547. seq_printf(s, "| 0x%-11x |\n", sr_dbg_entry.sr_start +
  548. sr_dbg_entry.sr_length - 1);
  549. }
  550. }
  551. }
  552. return 0;
  553. }
  554. DEFINE_SHOW_ATTRIBUTE(stm32_rifsc_conf_dump);
  555. static int stm32_rifsc_register_debugfs(struct stm32_firewall_controller *rifsc_controller,
  556. u32 nb_risup, u32 nb_rimu, u32 nb_risal)
  557. {
  558. struct rifsc_dbg_private *rifsc_priv;
  559. struct dentry *root = NULL;
  560. rifsc_priv = devm_kzalloc(rifsc_controller->dev, sizeof(*rifsc_priv), GFP_KERNEL);
  561. if (!rifsc_priv)
  562. return -ENOMEM;
  563. rifsc_priv->mmio = rifsc_controller->mmio;
  564. rifsc_priv->nb_risup = nb_risup;
  565. rifsc_priv->nb_rimu = nb_rimu;
  566. rifsc_priv->nb_risal = nb_risal;
  567. rifsc_priv->res_names = of_device_get_match_data(rifsc_controller->dev);
  568. root = debugfs_lookup("stm32_firewall", NULL);
  569. if (!root)
  570. root = debugfs_create_dir("stm32_firewall", NULL);
  571. if (IS_ERR(root))
  572. return PTR_ERR(root);
  573. debugfs_create_file("rifsc", 0444, root, rifsc_priv, &stm32_rifsc_conf_dump_fops);
  574. return 0;
  575. }
  576. #endif /* defined(CONFIG_DEBUG_FS) */
  577. static bool stm32_rifsc_is_semaphore_available(void __iomem *addr)
  578. {
  579. return !(readl(addr) & SEMCR_MUTEX);
  580. }
  581. static int stm32_rif_acquire_semaphore(struct stm32_firewall_controller *stm32_firewall_controller,
  582. int id)
  583. {
  584. void __iomem *addr = stm32_firewall_controller->mmio + RIFSC_RISC_PER0_SEMCR + 0x8 * id;
  585. writel(SEMCR_MUTEX, addr);
  586. /* Check that CID1 has the semaphore */
  587. if (stm32_rifsc_is_semaphore_available(addr) ||
  588. FIELD_GET(RIFSC_RISC_SCID_MASK, readl(addr)) != RIF_CID1)
  589. return -EACCES;
  590. return 0;
  591. }
  592. static void stm32_rif_release_semaphore(struct stm32_firewall_controller *stm32_firewall_controller,
  593. int id)
  594. {
  595. void __iomem *addr = stm32_firewall_controller->mmio + RIFSC_RISC_PER0_SEMCR + 0x8 * id;
  596. if (stm32_rifsc_is_semaphore_available(addr))
  597. return;
  598. writel(SEMCR_MUTEX, addr);
  599. /* Ok if another compartment takes the semaphore before the check */
  600. WARN_ON(!stm32_rifsc_is_semaphore_available(addr) &&
  601. FIELD_GET(RIFSC_RISC_SCID_MASK, readl(addr)) == RIF_CID1);
  602. }
  603. static int stm32_rifsc_grant_access(struct stm32_firewall_controller *ctrl, u32 firewall_id)
  604. {
  605. struct stm32_firewall_controller *rifsc_controller = ctrl;
  606. u32 reg_offset, reg_id, sec_reg_value, cid_reg_value;
  607. int rc;
  608. if (firewall_id >= rifsc_controller->max_entries) {
  609. dev_err(rifsc_controller->dev, "Invalid sys bus ID %u", firewall_id);
  610. return -EINVAL;
  611. }
  612. /*
  613. * RIFSC_RISC_PRIVCFGRx and RIFSC_RISC_SECCFGRx both handle configuration access for
  614. * 32 peripherals. On the other hand, there is one _RIFSC_RISC_PERx_CIDCFGR register
  615. * per peripheral
  616. */
  617. reg_id = firewall_id / IDS_PER_RISC_SEC_PRIV_REGS;
  618. reg_offset = firewall_id % IDS_PER_RISC_SEC_PRIV_REGS;
  619. sec_reg_value = readl(rifsc_controller->mmio + RIFSC_RISC_SECCFGR0 + 0x4 * reg_id);
  620. cid_reg_value = readl(rifsc_controller->mmio + RIFSC_RISC_PER0_CIDCFGR + 0x8 * firewall_id);
  621. /* First check conditions for semaphore mode, which doesn't take into account static CID. */
  622. if ((cid_reg_value & CIDCFGR_SEMEN) && (cid_reg_value & CIDCFGR_CFEN)) {
  623. if (cid_reg_value & BIT(RIF_CID1 + SEMWL_SHIFT)) {
  624. /* Static CID is irrelevant if semaphore mode */
  625. goto skip_cid_check;
  626. } else {
  627. dev_dbg(rifsc_controller->dev,
  628. "Invalid bus semaphore configuration: index %d\n", firewall_id);
  629. return -EACCES;
  630. }
  631. }
  632. /*
  633. * Skip CID check if CID filtering isn't enabled or filtering is enabled on CID0, which
  634. * corresponds to whatever CID.
  635. */
  636. if (!(cid_reg_value & CIDCFGR_CFEN) ||
  637. FIELD_GET(RIFSC_RISC_SCID_MASK, cid_reg_value) == RIF_CID0)
  638. goto skip_cid_check;
  639. /* Coherency check with the CID configuration */
  640. if (FIELD_GET(RIFSC_RISC_SCID_MASK, cid_reg_value) != RIF_CID1) {
  641. dev_dbg(rifsc_controller->dev, "Invalid CID configuration for peripheral: %d\n",
  642. firewall_id);
  643. return -EACCES;
  644. }
  645. skip_cid_check:
  646. /* Check security configuration */
  647. if (sec_reg_value & BIT(reg_offset)) {
  648. dev_dbg(rifsc_controller->dev,
  649. "Invalid security configuration for peripheral: %d\n", firewall_id);
  650. return -EACCES;
  651. }
  652. /*
  653. * If the peripheral is in semaphore mode, take the semaphore so that
  654. * the CID1 has the ownership.
  655. */
  656. if ((cid_reg_value & CIDCFGR_SEMEN) && (cid_reg_value & CIDCFGR_CFEN)) {
  657. rc = stm32_rif_acquire_semaphore(rifsc_controller, firewall_id);
  658. if (rc) {
  659. dev_err(rifsc_controller->dev,
  660. "Couldn't acquire semaphore for peripheral: %d\n", firewall_id);
  661. return rc;
  662. }
  663. }
  664. return 0;
  665. }
  666. static void stm32_rifsc_release_access(struct stm32_firewall_controller *ctrl, u32 firewall_id)
  667. {
  668. stm32_rif_release_semaphore(ctrl, firewall_id);
  669. }
  670. static int stm32_rifsc_probe(struct platform_device *pdev)
  671. {
  672. struct stm32_firewall_controller *rifsc_controller;
  673. struct device_node *np = pdev->dev.of_node;
  674. u32 nb_risup, nb_rimu, nb_risal;
  675. struct resource *res;
  676. void __iomem *mmio;
  677. int rc;
  678. rifsc_controller = devm_kzalloc(&pdev->dev, sizeof(*rifsc_controller), GFP_KERNEL);
  679. if (!rifsc_controller)
  680. return -ENOMEM;
  681. mmio = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  682. if (IS_ERR(mmio))
  683. return PTR_ERR(mmio);
  684. rifsc_controller->dev = &pdev->dev;
  685. rifsc_controller->mmio = mmio;
  686. rifsc_controller->name = dev_driver_string(rifsc_controller->dev);
  687. rifsc_controller->type = STM32_PERIPHERAL_FIREWALL | STM32_MEMORY_FIREWALL;
  688. rifsc_controller->grant_access = stm32_rifsc_grant_access;
  689. rifsc_controller->release_access = stm32_rifsc_release_access;
  690. /* Get number of RIFSC entries*/
  691. nb_risup = FIELD_GET(HWCFGR2_CONF1_MASK,
  692. readl(rifsc_controller->mmio + RIFSC_RISC_HWCFGR2));
  693. nb_rimu = FIELD_GET(HWCFGR2_CONF2_MASK,
  694. readl(rifsc_controller->mmio + RIFSC_RISC_HWCFGR2));
  695. nb_risal = FIELD_GET(HWCFGR2_CONF3_MASK,
  696. readl(rifsc_controller->mmio + RIFSC_RISC_HWCFGR2));
  697. /*
  698. * On STM32MP21, RIFSC_RISC_HWCFGR2 shows an incorrect number of RISAL (NUM_RISAL is 3
  699. * instead of 0). A software workaround is implemented using the st,mem-map property in the
  700. * device tree. This property is absent or left empty if there is no RISAL.
  701. */
  702. if (of_device_is_compatible(np, "st,stm32mp21-rifsc"))
  703. nb_risal = 0;
  704. rifsc_controller->max_entries = nb_risup + nb_rimu + nb_risal;
  705. platform_set_drvdata(pdev, rifsc_controller);
  706. rc = stm32_firewall_controller_register(rifsc_controller);
  707. if (rc) {
  708. dev_err(rifsc_controller->dev, "Couldn't register as a firewall controller: %d",
  709. rc);
  710. return rc;
  711. }
  712. rc = stm32_firewall_populate_bus(rifsc_controller);
  713. if (rc) {
  714. dev_err(rifsc_controller->dev, "Couldn't populate RIFSC bus: %d",
  715. rc);
  716. return rc;
  717. }
  718. #if defined(CONFIG_DEBUG_FS)
  719. rc = stm32_rifsc_register_debugfs(rifsc_controller, nb_risup, nb_rimu, nb_risal);
  720. if (rc)
  721. return dev_err_probe(rifsc_controller->dev, rc, "Failed creating debugfs entry\n");
  722. #endif
  723. /* Populate all allowed nodes */
  724. return of_platform_populate(np, NULL, NULL, &pdev->dev);
  725. }
  726. static const struct of_device_id stm32_rifsc_of_match[] = {
  727. {
  728. .compatible = "st,stm32mp25-rifsc",
  729. #if defined(CONFIG_DEBUG_FS)
  730. .data = &rifsc_mp25_res_names,
  731. #endif
  732. },
  733. {
  734. .compatible = "st,stm32mp21-rifsc",
  735. #if defined(CONFIG_DEBUG_FS)
  736. .data = &rifsc_mp21_res_names,
  737. #endif
  738. },
  739. {}
  740. };
  741. MODULE_DEVICE_TABLE(of, stm32_rifsc_of_match);
  742. static struct platform_driver stm32_rifsc_driver = {
  743. .probe = stm32_rifsc_probe,
  744. .driver = {
  745. .name = "stm32-rifsc",
  746. .of_match_table = stm32_rifsc_of_match,
  747. },
  748. };
  749. module_platform_driver(stm32_rifsc_driver);
  750. MODULE_AUTHOR("Gatien Chevallier <gatien.chevallier@foss.st.com>");
  751. MODULE_DESCRIPTION("STMicroelectronics RIFSC driver");
  752. MODULE_LICENSE("GPL");