Marc Moreno Berengue | 4cc81fc | 2018-08-10 14:32:01 +0100 | [diff] [blame] | 1 | /* |
David Vincze | 2dbfab5 | 2019-05-10 14:39:01 +0200 | [diff] [blame] | 2 | * Copyright (c) 2018-2019, Arm Limited. All rights reserved. |
Marc Moreno Berengue | 4cc81fc | 2018-08-10 14:32:01 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #ifndef __TFM_PLAT_NV_COUNTERS_H__ |
| 9 | #define __TFM_PLAT_NV_COUNTERS_H__ |
| 10 | |
| 11 | /** |
| 12 | * \file tfm_plat_nv_counters.h |
| 13 | * |
| 14 | * \note The interfaces defined in this file must be implemented for each |
| 15 | * SoC. |
David Vincze | 2ff8c6a | 2019-05-21 11:53:00 +0200 | [diff] [blame] | 16 | * \note The interface must be implemented in a fail-safe way that is |
| 17 | * resistant to asynchronous power failures or it can use hardware |
| 18 | * counters that have this capability, if supported by the platform. |
| 19 | * When a counter incrementation was interrupted it must be able to |
| 20 | * continue the incrementation process or recover the previous consistent |
| 21 | * status of the counters. If the counters have reached a stable status |
| 22 | * (every counter incrementation operation has finished), from that point |
| 23 | * their value cannot decrease due to any kind of power failure. |
Marc Moreno Berengue | 4cc81fc | 2018-08-10 14:32:01 +0100 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include <stdint.h> |
| 27 | #include "tfm_plat_defs.h" |
| 28 | |
| 29 | enum tfm_nv_counter_t { |
David Vincze | 2dbfab5 | 2019-05-10 14:39:01 +0200 | [diff] [blame] | 30 | PLAT_NV_COUNTER_0 = 0, /* Used by SST service */ |
| 31 | PLAT_NV_COUNTER_1, /* Used by SST service */ |
| 32 | PLAT_NV_COUNTER_2, /* Used by SST service */ |
David Vincze | fc6d5f1 | 2019-03-19 17:44:30 +0100 | [diff] [blame] | 33 | PLAT_NV_COUNTER_3, /* Used by bootloader */ |
| 34 | PLAT_NV_COUNTER_MAX |
Marc Moreno Berengue | 4cc81fc | 2018-08-10 14:32:01 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
| 41 | /** |
| 42 | * \brief Initialises all non-volatile (NV) counters. |
| 43 | * |
| 44 | * \return TFM_PLAT_ERR_SUCCESS if the initialization succeeds, otherwise |
| 45 | * TFM_PLAT_ERR_SYSTEM_ERR |
| 46 | */ |
| 47 | enum tfm_plat_err_t tfm_plat_init_nv_counter(void); |
| 48 | |
| 49 | /** |
| 50 | * \brief Reads the given non-volatile (NV) counter. |
| 51 | * |
| 52 | * \param[in] counter_id NV counter ID. |
| 53 | * \param[in] size Size of the buffer to store NV counter value |
| 54 | * in bytes. |
| 55 | * \param[out] val Pointer to store the current NV counter value. |
| 56 | * |
| 57 | * \return TFM_PLAT_ERR_SUCCESS if the value is read correctly. Otherwise, |
| 58 | * it returns TFM_PLAT_ERR_SYSTEM_ERR. |
| 59 | */ |
| 60 | enum tfm_plat_err_t tfm_plat_read_nv_counter(enum tfm_nv_counter_t counter_id, |
| 61 | uint32_t size, uint8_t *val); |
| 62 | |
| 63 | /** |
| 64 | * \brief Increments the given non-volatile (NV) counter. |
| 65 | * |
| 66 | * \param[in] counter_id NV counter ID. |
| 67 | * |
| 68 | * \return When the NV counter reaches its maximum value, the |
| 69 | * TFM_PLAT_ERR_MAX_VALUE error is returned to indicate the value |
| 70 | * cannot be incremented. Otherwise, it returns TFM_PLAT_ERR_SUCCESS. |
| 71 | */ |
| 72 | enum tfm_plat_err_t tfm_plat_increment_nv_counter( |
| 73 | enum tfm_nv_counter_t counter_id); |
| 74 | |
David Vincze | 2ff8c6a | 2019-05-21 11:53:00 +0200 | [diff] [blame] | 75 | /** |
| 76 | * \brief Sets the given non-volatile (NV) counter to the specified value. |
| 77 | * |
| 78 | * \param[in] counter_id NV counter ID. |
| 79 | * \param[in] value New value of the NV counter. The maximum value that |
| 80 | * can be set depends on the constraints of the |
| 81 | * underlying implementation, but it always must be |
| 82 | * greater than or equal to the current NV counter value. |
| 83 | * |
| 84 | * \retval TFM_PLAT_ERR_SUCCESS The NV counter is set successfully |
| 85 | * \retval TFM_PLAT_ERR_INVALID_INPUT The new value is less than the current |
| 86 | * counter value |
| 87 | * \retval TFM_PLAT_ERR_MAX_VALUE The new value is greater than the |
| 88 | * maximum value of the NV counter |
| 89 | * \retval TFM_PLAT_ERR_UNSUPPORTED The function is not implemented for |
| 90 | * the given platform or the new value is |
| 91 | * not representable on the underlying |
| 92 | * counter implementation |
| 93 | * \retval TFM_PLAT_ERR_SYSTEM_ERR An unspecified error occurred |
| 94 | * (none of the other standard error codes |
| 95 | * are applicable) |
| 96 | */ |
| 97 | enum tfm_plat_err_t tfm_plat_set_nv_counter(enum tfm_nv_counter_t counter_id, |
| 98 | uint32_t value); |
| 99 | |
Marc Moreno Berengue | 4cc81fc | 2018-08-10 14:32:01 +0100 | [diff] [blame] | 100 | #ifdef __cplusplus |
| 101 | } |
| 102 | #endif |
| 103 | |
| 104 | #endif /* __TFM_PLAT_NV_COUNTERS_H__ */ |