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