blob: ff34a91fc5ecc0de14a3bd7f429f2c376cfcabf7 [file] [log] [blame]
Marc Moreno Berengue4cc81fc2018-08-10 14:32:01 +01001/*
David Vincze2dbfab52019-05-10 14:39:01 +02002 * Copyright (c) 2018-2019, 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 {
David Vincze2dbfab52019-05-10 14:39:01 +020030 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 Vinczefc6d5f12019-03-19 17:44:30 +010033 PLAT_NV_COUNTER_3, /* Used by bootloader */
David Vincze0b41ee22019-06-28 14:28:20 +020034 PLAT_NV_COUNTER_4, /* Used by bootloader */
David Vinczefc6d5f12019-03-19 17:44:30 +010035 PLAT_NV_COUNTER_MAX
Marc Moreno Berengue4cc81fc2018-08-10 14:32:01 +010036};
37
38#ifdef __cplusplus
39extern "C" {
40#endif
41
42/**
43 * \brief Initialises all non-volatile (NV) counters.
44 *
45 * \return TFM_PLAT_ERR_SUCCESS if the initialization succeeds, otherwise
46 * TFM_PLAT_ERR_SYSTEM_ERR
47 */
48enum tfm_plat_err_t tfm_plat_init_nv_counter(void);
49
50/**
51 * \brief Reads the given non-volatile (NV) counter.
52 *
53 * \param[in] counter_id NV counter ID.
54 * \param[in] size Size of the buffer to store NV counter value
55 * in bytes.
56 * \param[out] val Pointer to store the current NV counter value.
57 *
58 * \return TFM_PLAT_ERR_SUCCESS if the value is read correctly. Otherwise,
59 * it returns TFM_PLAT_ERR_SYSTEM_ERR.
60 */
61enum tfm_plat_err_t tfm_plat_read_nv_counter(enum tfm_nv_counter_t counter_id,
62 uint32_t size, uint8_t *val);
63
64/**
65 * \brief Increments the given non-volatile (NV) counter.
66 *
67 * \param[in] counter_id NV counter ID.
68 *
69 * \return When the NV counter reaches its maximum value, the
70 * TFM_PLAT_ERR_MAX_VALUE error is returned to indicate the value
71 * cannot be incremented. Otherwise, it returns TFM_PLAT_ERR_SUCCESS.
72 */
73enum tfm_plat_err_t tfm_plat_increment_nv_counter(
74 enum tfm_nv_counter_t counter_id);
75
David Vincze2ff8c6a2019-05-21 11:53:00 +020076/**
77 * \brief Sets the given non-volatile (NV) counter to the specified value.
78 *
79 * \param[in] counter_id NV counter ID.
80 * \param[in] value New value of the NV counter. The maximum value that
81 * can be set depends on the constraints of the
82 * underlying implementation, but it always must be
83 * greater than or equal to the current NV counter value.
84 *
85 * \retval TFM_PLAT_ERR_SUCCESS The NV counter is set successfully
86 * \retval TFM_PLAT_ERR_INVALID_INPUT The new value is less than the current
87 * counter value
88 * \retval TFM_PLAT_ERR_MAX_VALUE The new value is greater than the
89 * maximum value of the NV counter
90 * \retval TFM_PLAT_ERR_UNSUPPORTED The function is not implemented for
91 * the given platform or the new value is
92 * not representable on the underlying
93 * counter implementation
94 * \retval TFM_PLAT_ERR_SYSTEM_ERR An unspecified error occurred
95 * (none of the other standard error codes
96 * are applicable)
97 */
98enum tfm_plat_err_t tfm_plat_set_nv_counter(enum tfm_nv_counter_t counter_id,
99 uint32_t value);
100
Marc Moreno Berengue4cc81fc2018-08-10 14:32:01 +0100101#ifdef __cplusplus
102}
103#endif
104
105#endif /* __TFM_PLAT_NV_COUNTERS_H__ */