zynqmp_pm_svc_main.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * Copyright (c) 2013-2022, Arm Limited and Contributors. All rights reserved.
  3. * Copyright (c) 2023, Advanced Micro Devices, Inc. All rights reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. /*
  8. * Top-level SMC handler for ZynqMP power management calls and
  9. * IPI setup functions for communication with PMU.
  10. */
  11. #include <errno.h>
  12. #include <arch_helpers.h>
  13. #include <common/runtime_svc.h>
  14. #include <drivers/arm/gicv2.h>
  15. #include <lib/mmio.h>
  16. #include <lib/spinlock.h>
  17. #include <plat/common/platform.h>
  18. #include <plat_private.h>
  19. #include "pm_client.h"
  20. #include "pm_ipi.h"
  21. #include "pm_svc_main.h"
  22. #include "zynqmp_pm_api_sys.h"
  23. #include "zynqmp_pm_defs.h"
  24. /* pm_up = !0 - UP, pm_up = 0 - DOWN */
  25. static int32_t pm_up, ipi_irq_flag;
  26. #if ZYNQMP_WDT_RESTART
  27. static spinlock_t inc_lock;
  28. static int active_cores = 0;
  29. #endif
  30. /**
  31. * typedef pm_ctx_t - Structure which contains data for power management.
  32. * @api_version: version of PM API, must match with one on PMU side.
  33. * @payload: payload array used to store received.
  34. * data from ipi buffer registers.
  35. *
  36. */
  37. typedef struct {
  38. uint32_t api_version;
  39. uint32_t payload[PAYLOAD_ARG_CNT];
  40. } pm_ctx_t;
  41. static pm_ctx_t pm_ctx;
  42. #if ZYNQMP_WDT_RESTART
  43. /**
  44. * trigger_wdt_restart() - Trigger warm restart event to APU cores.
  45. *
  46. * This function triggers SGI for all active APU CPUs. SGI handler then
  47. * power down CPU and call system reset.
  48. *
  49. */
  50. static void trigger_wdt_restart(void)
  51. {
  52. uint32_t core_count = 0;
  53. uint32_t core_status[3];
  54. uint32_t target_cpu_list = 0;
  55. int i;
  56. for (i = 0; i < 4; i++) {
  57. pm_get_node_status(NODE_APU_0 + i, core_status);
  58. if (core_status[0] == 1) {
  59. core_count++;
  60. target_cpu_list |= (1 << i);
  61. }
  62. }
  63. spin_lock(&inc_lock);
  64. active_cores = core_count;
  65. spin_unlock(&inc_lock);
  66. INFO("Active Cores: %d\n", active_cores);
  67. for (i = PLATFORM_CORE_COUNT - 1; i >= 0; i--) {
  68. if (target_cpu_list & (1 << i)) {
  69. /* trigger SGI to active cores */
  70. plat_ic_raise_el3_sgi(ARM_IRQ_SEC_SGI_7, i);
  71. }
  72. }
  73. }
  74. /**
  75. * ttc_fiq_handler() - TTC Handler for timer event.
  76. * @id: number of the highest priority pending interrupt of the type
  77. * that this handler was registered for.
  78. * @flags: security state, bit[0].
  79. * @handle: pointer to 'cpu_context' structure of the current CPU for the
  80. * security state specified in the 'flags' parameter.
  81. * @cookie: unused.
  82. *
  83. * Function registered as INTR_TYPE_EL3 interrupt handler.
  84. *
  85. * When WDT event is received in PMU, PMU needs to notify master to do cleanup
  86. * if required. PMU sets up timer and starts timer to overflow in zero time upon
  87. * WDT event. TF-A handles this timer event and takes necessary action required
  88. * for warm restart.
  89. *
  90. * In presence of non-secure software layers (EL1/2) sets the interrupt
  91. * at registered entrance in GIC and informs that PMU responded or demands
  92. * action.
  93. *
  94. * Return: 0 on success.
  95. *
  96. */
  97. static uint64_t ttc_fiq_handler(uint32_t id, uint32_t flags, void *handle,
  98. void *cookie)
  99. {
  100. INFO("BL31: Got TTC FIQ\n");
  101. plat_ic_end_of_interrupt(id);
  102. /* Clear TTC interrupt by reading interrupt register */
  103. mmio_read_32(TTC3_INTR_REGISTER_1);
  104. /* Disable the timer interrupts */
  105. mmio_write_32(TTC3_INTR_ENABLE_1, 0);
  106. trigger_wdt_restart();
  107. return 0;
  108. }
  109. /**
  110. * zynqmp_sgi7_irq() - Handler for SGI7 IRQ.
  111. * @id: number of the highest priority pending interrupt of the type
  112. * that this handler was registered for.
  113. * @flags: security state, bit[0].
  114. * @handle: pointer to 'cpu_context' structure of the current CPU for the
  115. * security state specified in the 'flags' parameter.
  116. * @cookie: unused.
  117. *
  118. * Function registered as INTR_TYPE_EL3 interrupt handler
  119. *
  120. * On receiving WDT event from PMU, TF-A generates SGI7 to all running CPUs.
  121. * In response to SGI7 interrupt, each CPUs do clean up if required and last
  122. * running CPU calls system restart.
  123. *
  124. * Return: This function does not return a value and it enters into wfi.
  125. */
  126. static uint64_t __unused __dead2 zynqmp_sgi7_irq(uint32_t id, uint32_t flags,
  127. void *handle, void *cookie)
  128. {
  129. int i;
  130. uint32_t value;
  131. /* enter wfi and stay there */
  132. INFO("Entering wfi\n");
  133. spin_lock(&inc_lock);
  134. active_cores--;
  135. for (i = 0; i < 4; i++) {
  136. mmio_write_32(BASE_GICD_BASE + GICD_CPENDSGIR + 4 * i,
  137. 0xffffffff);
  138. }
  139. dsb();
  140. spin_unlock(&inc_lock);
  141. if (active_cores == 0) {
  142. pm_mmio_read(PMU_GLOBAL_GEN_STORAGE4, &value);
  143. value = (value & RESTART_SCOPE_MASK) >> RESTART_SCOPE_SHIFT;
  144. pm_system_shutdown(PMF_SHUTDOWN_TYPE_RESET, value);
  145. }
  146. /* enter wfi and stay there */
  147. while (1)
  148. wfi();
  149. }
  150. /**
  151. * pm_wdt_restart_setup() - Setup warm restart interrupts.
  152. *
  153. * Return: Returns status, 0 on success or error+reason.
  154. *
  155. * This function sets up handler for SGI7 and TTC interrupts
  156. * used for warm restart.
  157. */
  158. static int pm_wdt_restart_setup(void)
  159. {
  160. int ret;
  161. /* register IRQ handler for SGI7 */
  162. ret = request_intr_type_el3(ARM_IRQ_SEC_SGI_7, zynqmp_sgi7_irq);
  163. if (ret) {
  164. WARN("BL31: registering SGI7 interrupt failed\n");
  165. goto err;
  166. }
  167. ret = request_intr_type_el3(IRQ_TTC3_1, ttc_fiq_handler);
  168. if (ret)
  169. WARN("BL31: registering TTC3 interrupt failed\n");
  170. err:
  171. return ret;
  172. }
  173. #endif
  174. /**
  175. * pm_setup() - PM service setup.
  176. *
  177. * Return: On success, the initialization function must return 0.
  178. * Any other return value will cause the framework to ignore
  179. * the service.
  180. *
  181. * Initialization functions for ZynqMP power management for
  182. * communicaton with PMU.
  183. *
  184. * Called from sip_svc_setup initialization function with the
  185. * rt_svc_init signature.
  186. *
  187. */
  188. int32_t pm_setup(void)
  189. {
  190. enum pm_ret_status err;
  191. pm_ipi_init(primary_proc);
  192. err = pm_get_api_version(&pm_ctx.api_version);
  193. if (err != PM_RET_SUCCESS) {
  194. ERROR("BL31: Failed to read Platform Management API version. "
  195. "Return: %d\n", err);
  196. return -EINVAL;
  197. }
  198. if (pm_ctx.api_version < PM_VERSION) {
  199. ERROR("BL31: Platform Management API version error. Expected: "
  200. "v%d.%d - Found: v%d.%d\n", PM_VERSION_MAJOR,
  201. PM_VERSION_MINOR, pm_ctx.api_version >> 16,
  202. pm_ctx.api_version & 0xFFFFU);
  203. return -EINVAL;
  204. }
  205. int32_t status = 0, ret = 0;
  206. #if ZYNQMP_WDT_RESTART
  207. status = pm_wdt_restart_setup();
  208. if (status)
  209. WARN("BL31: warm-restart setup failed\n");
  210. #endif
  211. if (status >= 0) {
  212. INFO("BL31: PM Service Init Complete: API v%d.%d\n",
  213. PM_VERSION_MAJOR, PM_VERSION_MINOR);
  214. ret = 0;
  215. } else {
  216. INFO("BL31: PM Service Init Failed, Error Code %d!\n", status);
  217. ret = status;
  218. }
  219. pm_up = !status;
  220. return ret;
  221. }
  222. /**
  223. * pm_smc_handler() - SMC handler for PM-API calls coming from EL1/EL2.
  224. * @smc_fid: Function Identifier.
  225. * @x1: Arguments.
  226. * @x2: Arguments.
  227. * @x3: Arguments.
  228. * @x4: Arguments.
  229. * @cookie: Unused.
  230. * @handle: Pointer to caller's context structure.
  231. * @flags: SECURE_FLAG or NON_SECURE_FLAG.
  232. *
  233. * Determines that smc_fid is valid and supported PM SMC Function ID from the
  234. * list of pm_api_ids, otherwise completes the request with
  235. * the unknown SMC Function ID.
  236. *
  237. * The SMC calls for PM service are forwarded from SIP Service SMC handler
  238. * function with rt_svc_handle signature.
  239. *
  240. * Return: Unused.
  241. *
  242. */
  243. uint64_t pm_smc_handler(uint32_t smc_fid, uint64_t x1, uint64_t x2, uint64_t x3,
  244. uint64_t x4, const void *cookie, void *handle, uint64_t flags)
  245. {
  246. (void)x4;
  247. (void)cookie;
  248. (void)flags;
  249. enum pm_ret_status ret;
  250. uint32_t payload[PAYLOAD_ARG_CNT];
  251. uint32_t pm_arg[5];
  252. uint32_t result[RET_PAYLOAD_ARG_CNT] = {0};
  253. uint32_t api_id;
  254. /* Handle case where PM wasn't initialized properly */
  255. if (pm_up == 0)
  256. SMC_RET1(handle, SMC_UNK);
  257. pm_arg[0] = (uint32_t)x1;
  258. pm_arg[1] = (uint32_t)(x1 >> 32);
  259. pm_arg[2] = (uint32_t)x2;
  260. pm_arg[3] = (uint32_t)(x2 >> 32);
  261. pm_arg[4] = (uint32_t)x3;
  262. api_id = smc_fid & FUNCID_NUM_MASK;
  263. switch (api_id) {
  264. /* PM API Functions */
  265. case PM_SELF_SUSPEND:
  266. ret = pm_self_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
  267. pm_arg[3]);
  268. SMC_RET1(handle, (uint64_t)ret);
  269. case PM_REQ_SUSPEND:
  270. ret = pm_req_suspend(pm_arg[0], pm_arg[1], pm_arg[2],
  271. pm_arg[3]);
  272. SMC_RET1(handle, (uint64_t)ret);
  273. case PM_REQ_WAKEUP:
  274. {
  275. /* Use address flag is encoded in the 1st bit of the low-word */
  276. uint32_t set_addr = pm_arg[1] & 0x1U;
  277. uint64_t address = (uint64_t)pm_arg[2] << 32U;
  278. address |= pm_arg[1] & (~0x1U);
  279. ret = pm_req_wakeup(pm_arg[0], set_addr, address,
  280. pm_arg[3]);
  281. SMC_RET1(handle, (uint64_t)ret);
  282. }
  283. case PM_FORCE_POWERDOWN:
  284. ret = pm_force_powerdown(pm_arg[0], pm_arg[1]);
  285. SMC_RET1(handle, (uint64_t)ret);
  286. case PM_ABORT_SUSPEND:
  287. ret = pm_abort_suspend(pm_arg[0]);
  288. SMC_RET1(handle, (uint64_t)ret);
  289. case PM_SET_WAKEUP_SOURCE:
  290. ret = pm_set_wakeup_source(pm_arg[0], pm_arg[1], pm_arg[2]);
  291. SMC_RET1(handle, (uint64_t)ret);
  292. case PM_SYSTEM_SHUTDOWN:
  293. ret = pm_system_shutdown(pm_arg[0], pm_arg[1]);
  294. SMC_RET1(handle, (uint64_t)ret);
  295. case PM_REQ_NODE:
  296. ret = pm_req_node(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
  297. SMC_RET1(handle, (uint64_t)ret);
  298. case PM_SET_REQUIREMENT:
  299. ret = pm_set_requirement(pm_arg[0], pm_arg[1], pm_arg[2],
  300. pm_arg[3]);
  301. SMC_RET1(handle, (uint64_t)ret);
  302. case PM_GET_API_VERSION:
  303. if (ipi_irq_flag == 0U) {
  304. /*
  305. * Enable IPI IRQ
  306. * assume the rich OS is OK to handle callback IRQs now.
  307. * Even if we were wrong, it would not enable the IRQ in
  308. * the GIC.
  309. */
  310. pm_ipi_irq_enable(primary_proc);
  311. ipi_irq_flag = 1U;
  312. }
  313. SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
  314. ((uint64_t)pm_ctx.api_version << 32));
  315. case PM_FPGA_LOAD:
  316. ret = pm_fpga_load(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
  317. SMC_RET1(handle, (uint64_t)ret);
  318. case PM_FPGA_GET_STATUS:
  319. {
  320. uint32_t value = 0U;
  321. ret = pm_fpga_get_status(&value);
  322. SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32)));
  323. }
  324. case PM_SECURE_RSA_AES:
  325. ret = pm_secure_rsaaes(pm_arg[0], pm_arg[1], pm_arg[2],
  326. pm_arg[3]);
  327. SMC_RET1(handle, (uint64_t)ret);
  328. case PM_GET_CALLBACK_DATA:
  329. ret = pm_get_callbackdata(result, ARRAY_SIZE(result));
  330. if (ret != PM_RET_SUCCESS) {
  331. result[0] = ret;
  332. }
  333. SMC_RET2(handle,
  334. ((uint64_t)result[0] | ((uint64_t)result[1] << 32)),
  335. ((uint64_t)result[2] | ((uint64_t)result[3] << 32)));
  336. case PM_IOCTL:
  337. {
  338. uint32_t value = 0U;
  339. ret = pm_ioctl(pm_arg[0], pm_arg[1], pm_arg[2],
  340. pm_arg[3], &value);
  341. SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32)));
  342. }
  343. case PM_QUERY_DATA:
  344. {
  345. uint32_t data[4] = { 0 };
  346. pm_query_data(pm_arg[0], pm_arg[1], pm_arg[2],
  347. pm_arg[3], data);
  348. SMC_RET2(handle, ((uint64_t)data[0] | ((uint64_t)data[1] << 32)),
  349. ((uint64_t)data[2] | ((uint64_t)data[3] << 32)));
  350. }
  351. case PM_CLOCK_ENABLE:
  352. ret = pm_clock_enable(pm_arg[0]);
  353. SMC_RET1(handle, (uint64_t)ret);
  354. case PM_CLOCK_DISABLE:
  355. ret = pm_clock_disable(pm_arg[0]);
  356. SMC_RET1(handle, (uint64_t)ret);
  357. case PM_CLOCK_GETSTATE:
  358. {
  359. uint32_t value = 0U;
  360. ret = pm_clock_getstate(pm_arg[0], &value);
  361. SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32)));
  362. }
  363. case PM_CLOCK_SETDIVIDER:
  364. ret = pm_clock_setdivider(pm_arg[0], pm_arg[1]);
  365. SMC_RET1(handle, (uint64_t)ret);
  366. case PM_CLOCK_GETDIVIDER:
  367. {
  368. uint32_t value = 0U;
  369. ret = pm_clock_getdivider(pm_arg[0], &value);
  370. SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32)));
  371. }
  372. case PM_CLOCK_SETPARENT:
  373. ret = pm_clock_setparent(pm_arg[0], pm_arg[1]);
  374. SMC_RET1(handle, (uint64_t)ret);
  375. case PM_CLOCK_GETPARENT:
  376. {
  377. uint32_t value = 0U;
  378. ret = pm_clock_getparent(pm_arg[0], &value);
  379. SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32U)));
  380. }
  381. case PM_GET_TRUSTZONE_VERSION:
  382. SMC_RET1(handle, (uint64_t)PM_RET_SUCCESS |
  383. ((uint64_t)ZYNQMP_TZ_VERSION << 32U));
  384. case PM_SET_SUSPEND_MODE:
  385. ret = pm_set_suspend_mode(pm_arg[0]);
  386. SMC_RET1(handle, (uint64_t)ret);
  387. case PM_SECURE_SHA:
  388. ret = pm_sha_hash(pm_arg[0], pm_arg[1], pm_arg[2],
  389. pm_arg[3]);
  390. SMC_RET1(handle, (uint64_t)ret);
  391. case PM_SECURE_RSA:
  392. ret = pm_rsa_core(pm_arg[0], pm_arg[1], pm_arg[2],
  393. pm_arg[3]);
  394. SMC_RET1(handle, (uint64_t)ret);
  395. case PM_SECURE_IMAGE:
  396. {
  397. ret = pm_secure_image(pm_arg[0], pm_arg[1], pm_arg[2],
  398. pm_arg[3], &result[0]);
  399. SMC_RET2(handle, ((uint64_t)ret | ((uint64_t)result[0] << 32U)),
  400. result[1]);
  401. }
  402. case PM_FPGA_READ:
  403. {
  404. uint32_t value = 0U;
  405. ret = pm_fpga_read(pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3],
  406. &value);
  407. SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32U)));
  408. }
  409. case PM_SECURE_AES:
  410. {
  411. uint32_t value = 0U;
  412. ret = pm_aes_engine(pm_arg[0], pm_arg[1], &value);
  413. SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32U)));
  414. }
  415. case PM_PLL_SET_PARAMETER:
  416. ret = pm_pll_set_parameter(pm_arg[0], pm_arg[1], pm_arg[2]);
  417. SMC_RET1(handle, (uint64_t)ret);
  418. case PM_PLL_GET_PARAMETER:
  419. {
  420. uint32_t value = 0U;
  421. ret = pm_pll_get_parameter(pm_arg[0], pm_arg[1], &value);
  422. SMC_RET1(handle, ((uint64_t)ret | ((uint64_t)value << 32U)));
  423. }
  424. case PM_PLL_SET_MODE:
  425. ret = pm_pll_set_mode(pm_arg[0], pm_arg[1]);
  426. SMC_RET1(handle, (uint64_t)ret);
  427. case PM_PLL_GET_MODE:
  428. {
  429. uint32_t mode = 0U;
  430. ret = pm_pll_get_mode(pm_arg[0], &mode);
  431. SMC_RET1(handle, ((uint64_t)ret | ((uint64_t)mode << 32U)));
  432. }
  433. case PM_REGISTER_ACCESS:
  434. {
  435. uint32_t value = 0U;
  436. ret = pm_register_access(pm_arg[0], pm_arg[1], pm_arg[2],
  437. pm_arg[3], &value);
  438. SMC_RET1(handle, ((uint64_t)ret | (((uint64_t)value) << 32U)));
  439. }
  440. case PM_EFUSE_ACCESS:
  441. {
  442. uint32_t value = 0U;
  443. #if defined(ZYNQMP_SECURE_EFUSES)
  444. if (is_caller_non_secure(flags)) {
  445. SMC_RET1(handle,
  446. (((uint64_t)PM_RET_ERROR_NOT_ENABLED) << 32U) |
  447. (uint64_t)PM_RET_ERROR_ACCESS);
  448. }
  449. #endif
  450. ret = pm_efuse_access(pm_arg[0], pm_arg[1], &value);
  451. SMC_RET1(handle, (uint64_t)ret | (((uint64_t)value) << 32U));
  452. }
  453. case PM_FPGA_GET_VERSION:
  454. case PM_FPGA_GET_FEATURE_LIST:
  455. {
  456. uint32_t ret_payload[PAYLOAD_ARG_CNT];
  457. PM_PACK_PAYLOAD5(payload, smc_fid & FUNCID_NUM_MASK,
  458. pm_arg[0], pm_arg[1], pm_arg[2], pm_arg[3]);
  459. ret = pm_ipi_send_sync(primary_proc, payload, ret_payload, 3U);
  460. SMC_RET2(handle, ((uint64_t)ret | ((uint64_t)ret_payload[0] << 32U)),
  461. ((uint64_t)ret_payload[1] | ((uint64_t)ret_payload[2] << 32U)));
  462. }
  463. case PM_FEATURE_CHECK:
  464. {
  465. uint32_t version_type = 0;
  466. uint32_t bit_mask[2] = {0};
  467. ret = pm_feature_check(pm_arg[0], &version_type, bit_mask,
  468. ARRAY_SIZE(bit_mask));
  469. SMC_RET2(handle, ((uint64_t)ret | ((uint64_t)version_type << 32U)),
  470. ((uint64_t)bit_mask[0] | ((uint64_t)bit_mask[1] << 32U)));
  471. }
  472. default:
  473. /* Send request to the PMU */
  474. PM_PACK_PAYLOAD6(payload, api_id, pm_arg[0], pm_arg[1],
  475. pm_arg[2], pm_arg[3], pm_arg[4]);
  476. ret = pm_ipi_send_sync(primary_proc, payload, result,
  477. RET_PAYLOAD_ARG_CNT);
  478. SMC_RET2(handle, ((uint64_t)ret | ((uint64_t)result[0] << 32U)),
  479. ((uint64_t)result[1] | ((uint64_t)result[2] << 32U)));
  480. }
  481. }