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