stm32mp1_shared_resources.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * Copyright (c) 2017-2022, STMicroelectronics - All Rights Reserved
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <assert.h>
  7. #include <stdint.h>
  8. #include <common/debug.h>
  9. #include <drivers/st/etzpc.h>
  10. #include <drivers/st/stm32_gpio.h>
  11. #include <platform_def.h>
  12. #include <stm32mp_shared_resources.h>
  13. /*
  14. * Once one starts to get the resource registering state, one cannot register
  15. * new resources. This ensures resource state cannot change.
  16. */
  17. static bool registering_locked;
  18. /*
  19. * Shared peripherals and resources registration
  20. *
  21. * Each resource assignation is stored in a table. The state defaults
  22. * to PERIPH_UNREGISTERED if the resource is not explicitly assigned.
  23. *
  24. * Resource driver that as not embedded (a.k.a their related CFG_xxx build
  25. * directive is disabled) are assigned to the non-secure world.
  26. *
  27. * Each pin of the GPIOZ bank can be secure or non-secure.
  28. *
  29. * It is the platform responsibility the ensure resource assignation
  30. * matches the access permission firewalls configuration.
  31. */
  32. enum shres_state {
  33. SHRES_UNREGISTERED = 0,
  34. SHRES_SECURE,
  35. SHRES_NON_SECURE,
  36. };
  37. /* Force uint8_t array for array of enum shres_state for size considerations */
  38. static uint8_t shres_state[STM32MP1_SHRES_COUNT];
  39. static const char *shres2str_id_tbl[STM32MP1_SHRES_COUNT] __unused = {
  40. [STM32MP1_SHRES_GPIOZ(0)] = "GPIOZ0",
  41. [STM32MP1_SHRES_GPIOZ(1)] = "GPIOZ1",
  42. [STM32MP1_SHRES_GPIOZ(2)] = "GPIOZ2",
  43. [STM32MP1_SHRES_GPIOZ(3)] = "GPIOZ3",
  44. [STM32MP1_SHRES_GPIOZ(4)] = "GPIOZ4",
  45. [STM32MP1_SHRES_GPIOZ(5)] = "GPIOZ5",
  46. [STM32MP1_SHRES_GPIOZ(6)] = "GPIOZ6",
  47. [STM32MP1_SHRES_GPIOZ(7)] = "GPIOZ7",
  48. [STM32MP1_SHRES_IWDG1] = "IWDG1",
  49. [STM32MP1_SHRES_USART1] = "USART1",
  50. [STM32MP1_SHRES_SPI6] = "SPI6",
  51. [STM32MP1_SHRES_I2C4] = "I2C4",
  52. [STM32MP1_SHRES_RNG1] = "RNG1",
  53. [STM32MP1_SHRES_HASH1] = "HASH1",
  54. [STM32MP1_SHRES_CRYP1] = "CRYP1",
  55. [STM32MP1_SHRES_I2C6] = "I2C6",
  56. [STM32MP1_SHRES_RTC] = "RTC",
  57. [STM32MP1_SHRES_MCU] = "MCU",
  58. [STM32MP1_SHRES_MDMA] = "MDMA",
  59. [STM32MP1_SHRES_PLL3] = "PLL3",
  60. };
  61. static const char __unused *shres2str_id(enum stm32mp_shres id)
  62. {
  63. assert(id < ARRAY_SIZE(shres2str_id_tbl));
  64. return shres2str_id_tbl[id];
  65. }
  66. static const char __unused *shres2str_state_tbl[] = {
  67. [SHRES_UNREGISTERED] = "unregistered",
  68. [SHRES_NON_SECURE] = "non-secure",
  69. [SHRES_SECURE] = "secure",
  70. };
  71. static const char __unused *shres2str_state(unsigned int state)
  72. {
  73. assert(state < ARRAY_SIZE(shres2str_state_tbl));
  74. return shres2str_state_tbl[state];
  75. }
  76. /* Get resource state: these accesses lock the registering support */
  77. static void lock_registering(void)
  78. {
  79. registering_locked = true;
  80. }
  81. static bool periph_is_non_secure(enum stm32mp_shres id)
  82. {
  83. lock_registering();
  84. return (shres_state[id] == SHRES_NON_SECURE) ||
  85. (shres_state[id] == SHRES_UNREGISTERED);
  86. }
  87. static bool periph_is_secure(enum stm32mp_shres id)
  88. {
  89. return !periph_is_non_secure(id);
  90. }
  91. /* GPIOZ pin count is saved in RAM to prevent parsing FDT several times */
  92. static int8_t gpioz_nbpin = -1;
  93. static unsigned int get_gpio_nbpin(unsigned int bank)
  94. {
  95. if (bank != GPIO_BANK_Z) {
  96. int count = fdt_get_gpio_bank_pin_count(bank);
  97. assert((count >= 0) && ((unsigned int)count <= (GPIO_PIN_MAX + 1)));
  98. return (unsigned int)count;
  99. }
  100. if (gpioz_nbpin < 0) {
  101. int count = fdt_get_gpio_bank_pin_count(GPIO_BANK_Z);
  102. assert((count == 0) || (count == STM32MP_GPIOZ_PIN_MAX_COUNT));
  103. gpioz_nbpin = count;
  104. }
  105. return (unsigned int)gpioz_nbpin;
  106. }
  107. static unsigned int get_gpioz_nbpin(void)
  108. {
  109. return get_gpio_nbpin(GPIO_BANK_Z);
  110. }
  111. static void register_periph(enum stm32mp_shres id, unsigned int state)
  112. {
  113. assert((id < STM32MP1_SHRES_COUNT) &&
  114. ((state == SHRES_SECURE) || (state == SHRES_NON_SECURE)));
  115. if (registering_locked) {
  116. if (shres_state[id] == state) {
  117. return;
  118. }
  119. panic();
  120. }
  121. if ((shres_state[id] != SHRES_UNREGISTERED) &&
  122. (shres_state[id] != state)) {
  123. VERBOSE("Cannot change %s from %s to %s\n",
  124. shres2str_id(id),
  125. shres2str_state(shres_state[id]),
  126. shres2str_state(state));
  127. panic();
  128. }
  129. if (shres_state[id] == SHRES_UNREGISTERED) {
  130. VERBOSE("Register %s as %s\n",
  131. shres2str_id(id), shres2str_state(state));
  132. }
  133. if ((id >= STM32MP1_SHRES_GPIOZ(0)) &&
  134. (id <= STM32MP1_SHRES_GPIOZ(7)) &&
  135. ((unsigned int)(id - STM32MP1_SHRES_GPIOZ(0)) >= get_gpioz_nbpin())) {
  136. ERROR("Invalid GPIO pin %d, %u pin(s) available\n",
  137. (int)(id - STM32MP1_SHRES_GPIOZ(0)), get_gpioz_nbpin());
  138. panic();
  139. }
  140. shres_state[id] = (uint8_t)state;
  141. /* Explore clock tree to lock dependencies */
  142. if (state == SHRES_SECURE) {
  143. enum stm32mp_shres clock_res_id;
  144. switch (id) {
  145. case STM32MP1_SHRES_GPIOZ(0):
  146. case STM32MP1_SHRES_GPIOZ(1):
  147. case STM32MP1_SHRES_GPIOZ(2):
  148. case STM32MP1_SHRES_GPIOZ(3):
  149. case STM32MP1_SHRES_GPIOZ(4):
  150. case STM32MP1_SHRES_GPIOZ(5):
  151. case STM32MP1_SHRES_GPIOZ(6):
  152. case STM32MP1_SHRES_GPIOZ(7):
  153. clock_res_id = GPIOZ;
  154. break;
  155. case STM32MP1_SHRES_IWDG1:
  156. clock_res_id = IWDG1;
  157. break;
  158. case STM32MP1_SHRES_USART1:
  159. clock_res_id = USART1_K;
  160. break;
  161. case STM32MP1_SHRES_SPI6:
  162. clock_res_id = SPI6_K;
  163. break;
  164. case STM32MP1_SHRES_I2C4:
  165. clock_res_id = I2C4_K;
  166. break;
  167. case STM32MP1_SHRES_RNG1:
  168. clock_res_id = RNG1_K;
  169. break;
  170. case STM32MP1_SHRES_HASH1:
  171. clock_res_id = HASH1;
  172. break;
  173. case STM32MP1_SHRES_CRYP1:
  174. clock_res_id = CRYP1;
  175. break;
  176. case STM32MP1_SHRES_I2C6:
  177. clock_res_id = I2C6_K;
  178. break;
  179. case STM32MP1_SHRES_RTC:
  180. clock_res_id = RTC;
  181. break;
  182. default:
  183. /* No clock resource dependency */
  184. return;
  185. }
  186. stm32mp1_register_clock_parents_secure(clock_res_id);
  187. }
  188. }
  189. /* Register resource by ID */
  190. void stm32mp_register_secure_periph(enum stm32mp_shres id)
  191. {
  192. register_periph(id, SHRES_SECURE);
  193. }
  194. void stm32mp_register_non_secure_periph(enum stm32mp_shres id)
  195. {
  196. register_periph(id, SHRES_NON_SECURE);
  197. }
  198. static void register_periph_iomem(uintptr_t base, unsigned int state)
  199. {
  200. enum stm32mp_shres id;
  201. switch (base) {
  202. case CRYP1_BASE:
  203. id = STM32MP1_SHRES_CRYP1;
  204. break;
  205. case HASH1_BASE:
  206. id = STM32MP1_SHRES_HASH1;
  207. break;
  208. case I2C4_BASE:
  209. id = STM32MP1_SHRES_I2C4;
  210. break;
  211. case I2C6_BASE:
  212. id = STM32MP1_SHRES_I2C6;
  213. break;
  214. case IWDG1_BASE:
  215. id = STM32MP1_SHRES_IWDG1;
  216. break;
  217. case RNG1_BASE:
  218. id = STM32MP1_SHRES_RNG1;
  219. break;
  220. case RTC_BASE:
  221. id = STM32MP1_SHRES_RTC;
  222. break;
  223. case SPI6_BASE:
  224. id = STM32MP1_SHRES_SPI6;
  225. break;
  226. case USART1_BASE:
  227. id = STM32MP1_SHRES_USART1;
  228. break;
  229. case GPIOA_BASE:
  230. case GPIOB_BASE:
  231. case GPIOC_BASE:
  232. case GPIOD_BASE:
  233. case GPIOE_BASE:
  234. case GPIOF_BASE:
  235. case GPIOG_BASE:
  236. case GPIOH_BASE:
  237. case GPIOI_BASE:
  238. case GPIOJ_BASE:
  239. case GPIOK_BASE:
  240. case USART2_BASE:
  241. case USART3_BASE:
  242. case UART4_BASE:
  243. case UART5_BASE:
  244. case USART6_BASE:
  245. case UART7_BASE:
  246. case UART8_BASE:
  247. case IWDG2_BASE:
  248. /* Allow drivers to register some non-secure resources */
  249. VERBOSE("IO for non-secure resource 0x%x\n",
  250. (unsigned int)base);
  251. if (state != SHRES_NON_SECURE) {
  252. panic();
  253. }
  254. return;
  255. default:
  256. panic();
  257. }
  258. register_periph(id, state);
  259. }
  260. void stm32mp_register_secure_periph_iomem(uintptr_t base)
  261. {
  262. register_periph_iomem(base, SHRES_SECURE);
  263. }
  264. void stm32mp_register_non_secure_periph_iomem(uintptr_t base)
  265. {
  266. register_periph_iomem(base, SHRES_NON_SECURE);
  267. }
  268. void stm32mp_register_secure_gpio(unsigned int bank, unsigned int pin)
  269. {
  270. switch (bank) {
  271. case GPIO_BANK_Z:
  272. register_periph(STM32MP1_SHRES_GPIOZ(pin), SHRES_SECURE);
  273. break;
  274. default:
  275. ERROR("GPIO bank %u cannot be secured\n", bank);
  276. panic();
  277. }
  278. }
  279. void stm32mp_register_non_secure_gpio(unsigned int bank, unsigned int pin)
  280. {
  281. switch (bank) {
  282. case GPIO_BANK_Z:
  283. register_periph(STM32MP1_SHRES_GPIOZ(pin), SHRES_NON_SECURE);
  284. break;
  285. default:
  286. break;
  287. }
  288. }
  289. static bool stm32mp_gpio_bank_is_non_secure(unsigned int bank)
  290. {
  291. unsigned int non_secure = 0U;
  292. unsigned int i;
  293. lock_registering();
  294. if (bank != GPIO_BANK_Z) {
  295. return true;
  296. }
  297. for (i = 0U; i < get_gpioz_nbpin(); i++) {
  298. if (periph_is_non_secure(STM32MP1_SHRES_GPIOZ(i))) {
  299. non_secure++;
  300. }
  301. }
  302. return non_secure == get_gpioz_nbpin();
  303. }
  304. static bool stm32mp_gpio_bank_is_secure(unsigned int bank)
  305. {
  306. unsigned int secure = 0U;
  307. unsigned int i;
  308. lock_registering();
  309. if (bank != GPIO_BANK_Z) {
  310. return false;
  311. }
  312. for (i = 0U; i < get_gpioz_nbpin(); i++) {
  313. if (periph_is_secure(STM32MP1_SHRES_GPIOZ(i))) {
  314. secure++;
  315. }
  316. }
  317. return secure == get_gpioz_nbpin();
  318. }
  319. bool stm32mp_nsec_can_access_clock(unsigned long clock_id)
  320. {
  321. enum stm32mp_shres shres_id = STM32MP1_SHRES_COUNT;
  322. switch (clock_id) {
  323. case CK_CSI:
  324. case CK_HSE:
  325. case CK_HSE_DIV2:
  326. case CK_HSI:
  327. case CK_LSE:
  328. case CK_LSI:
  329. case PLL1_P:
  330. case PLL1_Q:
  331. case PLL1_R:
  332. case PLL2_P:
  333. case PLL2_Q:
  334. case PLL2_R:
  335. case PLL3_P:
  336. case PLL3_Q:
  337. case PLL3_R:
  338. case RTCAPB:
  339. return true;
  340. case GPIOZ:
  341. /* Allow clock access if at least one pin is non-secure */
  342. return !stm32mp_gpio_bank_is_secure(GPIO_BANK_Z);
  343. case CRYP1:
  344. shres_id = STM32MP1_SHRES_CRYP1;
  345. break;
  346. case HASH1:
  347. shres_id = STM32MP1_SHRES_HASH1;
  348. break;
  349. case I2C4_K:
  350. shres_id = STM32MP1_SHRES_I2C4;
  351. break;
  352. case I2C6_K:
  353. shres_id = STM32MP1_SHRES_I2C6;
  354. break;
  355. case IWDG1:
  356. shres_id = STM32MP1_SHRES_IWDG1;
  357. break;
  358. case RNG1_K:
  359. shres_id = STM32MP1_SHRES_RNG1;
  360. break;
  361. case RTC:
  362. shres_id = STM32MP1_SHRES_RTC;
  363. break;
  364. case SPI6_K:
  365. shres_id = STM32MP1_SHRES_SPI6;
  366. break;
  367. case USART1_K:
  368. shres_id = STM32MP1_SHRES_USART1;
  369. break;
  370. default:
  371. return false;
  372. }
  373. return periph_is_non_secure(shres_id);
  374. }
  375. bool stm32mp_nsec_can_access_reset(unsigned int reset_id)
  376. {
  377. enum stm32mp_shres shres_id = STM32MP1_SHRES_COUNT;
  378. switch (reset_id) {
  379. case CRYP1_R:
  380. shres_id = STM32MP1_SHRES_CRYP1;
  381. break;
  382. case GPIOZ_R:
  383. /* GPIOZ reset mandates all pins are non-secure */
  384. return stm32mp_gpio_bank_is_non_secure(GPIO_BANK_Z);
  385. case HASH1_R:
  386. shres_id = STM32MP1_SHRES_HASH1;
  387. break;
  388. case I2C4_R:
  389. shres_id = STM32MP1_SHRES_I2C4;
  390. break;
  391. case I2C6_R:
  392. shres_id = STM32MP1_SHRES_I2C6;
  393. break;
  394. case MCU_R:
  395. shres_id = STM32MP1_SHRES_MCU;
  396. break;
  397. case MDMA_R:
  398. shres_id = STM32MP1_SHRES_MDMA;
  399. break;
  400. case RNG1_R:
  401. shres_id = STM32MP1_SHRES_RNG1;
  402. break;
  403. case SPI6_R:
  404. shres_id = STM32MP1_SHRES_SPI6;
  405. break;
  406. case USART1_R:
  407. shres_id = STM32MP1_SHRES_USART1;
  408. break;
  409. default:
  410. return false;
  411. }
  412. return periph_is_non_secure(shres_id);
  413. }
  414. static bool mckprot_protects_periph(enum stm32mp_shres id)
  415. {
  416. switch (id) {
  417. case STM32MP1_SHRES_MCU:
  418. case STM32MP1_SHRES_PLL3:
  419. return true;
  420. default:
  421. return false;
  422. }
  423. }
  424. /* ETZPC configuration at drivers initialization completion */
  425. static enum etzpc_decprot_attributes shres2decprot_attr(enum stm32mp_shres id)
  426. {
  427. assert((id < STM32MP1_SHRES_GPIOZ(0)) ||
  428. (id > STM32MP1_SHRES_GPIOZ(7)));
  429. if (periph_is_non_secure(id)) {
  430. return ETZPC_DECPROT_NS_RW;
  431. }
  432. return ETZPC_DECPROT_S_RW;
  433. }
  434. static void set_etzpc_secure_configuration(void)
  435. {
  436. /* Some system peripherals shall be secure */
  437. etzpc_configure_decprot(STM32MP1_ETZPC_STGENC_ID, ETZPC_DECPROT_S_RW);
  438. etzpc_configure_decprot(STM32MP1_ETZPC_BKPSRAM_ID, ETZPC_DECPROT_S_RW);
  439. etzpc_configure_decprot(STM32MP1_ETZPC_DDRCTRL_ID,
  440. ETZPC_DECPROT_NS_R_S_W);
  441. etzpc_configure_decprot(STM32MP1_ETZPC_DDRPHYC_ID,
  442. ETZPC_DECPROT_NS_R_S_W);
  443. /* Configure ETZPC with peripheral registering */
  444. etzpc_configure_decprot(STM32MP1_ETZPC_CRYP1_ID,
  445. shres2decprot_attr(STM32MP1_SHRES_CRYP1));
  446. etzpc_configure_decprot(STM32MP1_ETZPC_HASH1_ID,
  447. shres2decprot_attr(STM32MP1_SHRES_HASH1));
  448. etzpc_configure_decprot(STM32MP1_ETZPC_I2C4_ID,
  449. shres2decprot_attr(STM32MP1_SHRES_I2C4));
  450. etzpc_configure_decprot(STM32MP1_ETZPC_I2C6_ID,
  451. shres2decprot_attr(STM32MP1_SHRES_I2C6));
  452. etzpc_configure_decprot(STM32MP1_ETZPC_IWDG1_ID,
  453. shres2decprot_attr(STM32MP1_SHRES_IWDG1));
  454. etzpc_configure_decprot(STM32MP1_ETZPC_RNG1_ID,
  455. shres2decprot_attr(STM32MP1_SHRES_RNG1));
  456. etzpc_configure_decprot(STM32MP1_ETZPC_USART1_ID,
  457. shres2decprot_attr(STM32MP1_SHRES_USART1));
  458. etzpc_configure_decprot(STM32MP1_ETZPC_SPI6_ID,
  459. shres2decprot_attr(STM32MP1_SHRES_SPI6));
  460. }
  461. static void check_rcc_secure_configuration(void)
  462. {
  463. uint32_t n;
  464. uint32_t error = 0U;
  465. bool mckprot = stm32mp1_rcc_is_mckprot();
  466. bool secure = stm32mp1_rcc_is_secure();
  467. for (n = 0U; n < ARRAY_SIZE(shres_state); n++) {
  468. if (shres_state[n] != SHRES_SECURE) {
  469. continue;
  470. }
  471. if (!secure || (mckprot_protects_periph(n) && (!mckprot))) {
  472. ERROR("RCC %s MCKPROT %s and %s secure\n",
  473. secure ? "secure" : "non-secure",
  474. mckprot ? "set" : "not set",
  475. shres2str_id(n));
  476. error++;
  477. }
  478. }
  479. if (error != 0U) {
  480. panic();
  481. }
  482. }
  483. static void set_gpio_secure_configuration(void)
  484. {
  485. uint32_t pin;
  486. for (pin = 0U; pin < get_gpioz_nbpin(); pin++) {
  487. bool secure_state = periph_is_secure(STM32MP1_SHRES_GPIOZ(pin));
  488. set_gpio_secure_cfg(GPIO_BANK_Z, pin, secure_state);
  489. }
  490. }
  491. static void print_shared_resources_state(void)
  492. {
  493. unsigned int id;
  494. for (id = 0U; id < STM32MP1_SHRES_COUNT; id++) {
  495. switch (shres_state[id]) {
  496. case SHRES_SECURE:
  497. INFO("stm32mp1 %s is secure\n", shres2str_id(id));
  498. break;
  499. case SHRES_NON_SECURE:
  500. case SHRES_UNREGISTERED:
  501. VERBOSE("stm32mp %s is non-secure\n", shres2str_id(id));
  502. break;
  503. default:
  504. VERBOSE("stm32mp %s is invalid\n", shres2str_id(id));
  505. panic();
  506. }
  507. }
  508. }
  509. void stm32mp_lock_periph_registering(void)
  510. {
  511. registering_locked = true;
  512. print_shared_resources_state();
  513. check_rcc_secure_configuration();
  514. set_etzpc_secure_configuration();
  515. set_gpio_secure_configuration();
  516. }