blob: cff9972ea43bd121ec0e25304a7331221a331887 [file] [log] [blame]
Marc Moreno Berengue4cc81fc2018-08-10 14:32:01 +01001/*
Galanakis, Minosef5118b2020-01-21 15:27:14 +00002 * Copyright (c) 2018-2020, Arm Limited. All rights reserved.
Marc Moreno Berengue4cc81fc2018-08-10 14:32:01 +01003 *
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 Vincze2ff8c6a2019-05-21 11:53:00 +020016 * \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 Berengue4cc81fc2018-08-10 14:32:01 +010024 */
25
26#include <stdint.h>
27#include "tfm_plat_defs.h"
28
29enum tfm_nv_counter_t {
Kevin Pengc6d74502020-03-04 16:55:37 +080030 PLAT_NV_COUNTER_0 = 0, /* Used by PS service */
31 PLAT_NV_COUNTER_1, /* Used by PS service */
32 PLAT_NV_COUNTER_2, /* Used by PS service */
Chris Brandf28dd412020-03-24 15:10:54 -070033#ifdef BL2
David Vinczefc6d5f12019-03-19 17:44:30 +010034 PLAT_NV_COUNTER_3, /* Used by bootloader */
David Vincze0b41ee22019-06-28 14:28:20 +020035 PLAT_NV_COUNTER_4, /* Used by bootloader */
Chris Brandf28dd412020-03-24 15:10:54 -070036#endif
Galanakis, Minosef5118b2020-01-21 15:27:14 +000037 PLAT_NV_COUNTER_MAX,
38 PLAT_NV_COUNTER_BOUNDARY = UINT32_MAX /* Fix tfm_nv_counter_t size
39 to 4 bytes */
Marc Moreno Berengue4cc81fc2018-08-10 14:32:01 +010040};
41
42#ifdef __cplusplus
43extern "C" {
44#endif
45
46/**
47 * \brief Initialises all non-volatile (NV) counters.
48 *
49 * \return TFM_PLAT_ERR_SUCCESS if the initialization succeeds, otherwise
50 * TFM_PLAT_ERR_SYSTEM_ERR
51 */
52enum tfm_plat_err_t tfm_plat_init_nv_counter(void);
53
54/**
55 * \brief Reads the given non-volatile (NV) counter.
56 *
57 * \param[in] counter_id NV counter ID.
58 * \param[in] size Size of the buffer to store NV counter value
59 * in bytes.
60 * \param[out] val Pointer to store the current NV counter value.
61 *
62 * \return TFM_PLAT_ERR_SUCCESS if the value is read correctly. Otherwise,
63 * it returns TFM_PLAT_ERR_SYSTEM_ERR.
64 */
65enum tfm_plat_err_t tfm_plat_read_nv_counter(enum tfm_nv_counter_t counter_id,
66 uint32_t size, uint8_t *val);
67
68/**
69 * \brief Increments the given non-volatile (NV) counter.
70 *
71 * \param[in] counter_id NV counter ID.
72 *
73 * \return When the NV counter reaches its maximum value, the
74 * TFM_PLAT_ERR_MAX_VALUE error is returned to indicate the value
75 * cannot be incremented. Otherwise, it returns TFM_PLAT_ERR_SUCCESS.
76 */
77enum tfm_plat_err_t tfm_plat_increment_nv_counter(
78 enum tfm_nv_counter_t counter_id);
79
David Vincze2ff8c6a2019-05-21 11:53:00 +020080/**
81 * \brief Sets the given non-volatile (NV) counter to the specified value.
82 *
83 * \param[in] counter_id NV counter ID.
84 * \param[in] value New value of the NV counter. The maximum value that
85 * can be set depends on the constraints of the
86 * underlying implementation, but it always must be
87 * greater than or equal to the current NV counter value.
88 *
89 * \retval TFM_PLAT_ERR_SUCCESS The NV counter is set successfully
90 * \retval TFM_PLAT_ERR_INVALID_INPUT The new value is less than the current
91 * counter value
92 * \retval TFM_PLAT_ERR_MAX_VALUE The new value is greater than the
93 * maximum value of the NV counter
94 * \retval TFM_PLAT_ERR_UNSUPPORTED The function is not implemented for
95 * the given platform or the new value is
96 * not representable on the underlying
97 * counter implementation
98 * \retval TFM_PLAT_ERR_SYSTEM_ERR An unspecified error occurred
99 * (none of the other standard error codes
100 * are applicable)
101 */
102enum tfm_plat_err_t tfm_plat_set_nv_counter(enum tfm_nv_counter_t counter_id,
103 uint32_t value);
104
Marc Moreno Berengue4cc81fc2018-08-10 14:32:01 +0100105#ifdef __cplusplus
106}
107#endif
108
109#endif /* __TFM_PLAT_NV_COUNTERS_H__ */