Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 1 | /* |
Paul Beesley | 1fbc97b | 2019-01-11 18:26:51 +0000 | [diff] [blame] | 2 | * Copyright (c) 2014-2019, ARM Limited and Contributors. All rights reserved. |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 3 | * |
dp-arm | fa3cf0b | 2017-05-03 09:38:09 +0100 | [diff] [blame] | 4 | * SPDX-License-Identifier: BSD-3-Clause |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 5 | */ |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 6 | |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | |
| 9 | #include <arch_helpers.h> |
| 10 | #include <plat/common/platform.h> |
| 11 | |
Dan Handley | e2c27f5 | 2014-08-01 17:58:27 +0100 | [diff] [blame] | 12 | #include "tsp_private.h" |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 13 | |
| 14 | /******************************************************************************* |
| 15 | * Data structure to keep track of per-cpu secure generic timer context across |
| 16 | * power management operations. |
| 17 | ******************************************************************************/ |
| 18 | typedef struct timer_context { |
| 19 | uint64_t cval; |
| 20 | uint32_t ctl; |
| 21 | } timer_context_t; |
| 22 | |
| 23 | static timer_context_t pcpu_timer_context[PLATFORM_CORE_COUNT]; |
| 24 | |
| 25 | /******************************************************************************* |
| 26 | * This function initializes the generic timer to fire every 0.5 second |
| 27 | ******************************************************************************/ |
Juan Castillo | 2d55240 | 2014-06-13 17:05:10 +0100 | [diff] [blame] | 28 | void tsp_generic_timer_start(void) |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 29 | { |
| 30 | uint64_t cval; |
| 31 | uint32_t ctl = 0; |
| 32 | |
| 33 | /* The timer will fire every 0.5 second */ |
| 34 | cval = read_cntpct_el0() + (read_cntfrq_el0() >> 1); |
| 35 | write_cntps_cval_el1(cval); |
| 36 | |
| 37 | /* Enable the secure physical timer */ |
| 38 | set_cntp_ctl_enable(ctl); |
| 39 | write_cntps_ctl_el1(ctl); |
| 40 | } |
| 41 | |
| 42 | /******************************************************************************* |
| 43 | * This function deasserts the timer interrupt and sets it up again |
| 44 | ******************************************************************************/ |
Juan Castillo | 2d55240 | 2014-06-13 17:05:10 +0100 | [diff] [blame] | 45 | void tsp_generic_timer_handler(void) |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 46 | { |
| 47 | /* Ensure that the timer did assert the interrupt */ |
| 48 | assert(get_cntp_ctl_istatus(read_cntps_ctl_el1())); |
| 49 | |
Sandrine Bailleux | 1fe4336 | 2014-07-17 09:56:29 +0100 | [diff] [blame] | 50 | /* |
| 51 | * Disable the timer and reprogram it. The barriers ensure that there is |
| 52 | * no reordering of instructions around the reprogramming code. |
| 53 | */ |
| 54 | isb(); |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 55 | write_cntps_ctl_el1(0); |
| 56 | tsp_generic_timer_start(); |
Sandrine Bailleux | 1fe4336 | 2014-07-17 09:56:29 +0100 | [diff] [blame] | 57 | isb(); |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /******************************************************************************* |
| 61 | * This function deasserts the timer interrupt prior to cpu power down |
| 62 | ******************************************************************************/ |
Juan Castillo | 2d55240 | 2014-06-13 17:05:10 +0100 | [diff] [blame] | 63 | void tsp_generic_timer_stop(void) |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 64 | { |
| 65 | /* Disable the timer */ |
| 66 | write_cntps_ctl_el1(0); |
| 67 | } |
| 68 | |
| 69 | /******************************************************************************* |
| 70 | * This function saves the timer context prior to cpu suspension |
| 71 | ******************************************************************************/ |
Juan Castillo | 2d55240 | 2014-06-13 17:05:10 +0100 | [diff] [blame] | 72 | void tsp_generic_timer_save(void) |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 73 | { |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 74 | uint32_t linear_id = plat_my_core_pos(); |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 75 | |
| 76 | pcpu_timer_context[linear_id].cval = read_cntps_cval_el1(); |
| 77 | pcpu_timer_context[linear_id].ctl = read_cntps_ctl_el1(); |
| 78 | flush_dcache_range((uint64_t) &pcpu_timer_context[linear_id], |
| 79 | sizeof(pcpu_timer_context[linear_id])); |
| 80 | } |
| 81 | |
| 82 | /******************************************************************************* |
Paul Beesley | 1fbc97b | 2019-01-11 18:26:51 +0000 | [diff] [blame] | 83 | * This function restores the timer context post cpu resumption |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 84 | ******************************************************************************/ |
Juan Castillo | 2d55240 | 2014-06-13 17:05:10 +0100 | [diff] [blame] | 85 | void tsp_generic_timer_restore(void) |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 86 | { |
Soby Mathew | da43b66 | 2015-07-08 21:45:46 +0100 | [diff] [blame] | 87 | uint32_t linear_id = plat_my_core_pos(); |
Achin Gupta | 405406d | 2014-05-09 12:00:17 +0100 | [diff] [blame] | 88 | |
| 89 | write_cntps_cval_el1(pcpu_timer_context[linear_id].cval); |
| 90 | write_cntps_ctl_el1(pcpu_timer_context[linear_id].ctl); |
| 91 | } |