stm32mp_shres_helpers.h 1.68 KB
Newer Older
Yann Gautier's avatar
Yann Gautier committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * Copyright (C) 2018-2019, STMicroelectronics - All Rights Reserved
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#ifndef STM32MP_SHRES_HELPERS_H
#define STM32MP_SHRES_HELPERS_H

#include <stdint.h>

#include <common/debug.h>

/*
 * Shared reference counter: increments by 2 on secure increment
 * request, decrements by 2 on secure decrement request. Bit #0
 * is set to 1 on non-secure increment request and reset to 0 on
 * non-secure decrement request. The counter initializes to
 * either 0, 1 or 2 upon their expect default state.
 * Counters saturates once above UINT_MAX / 2.
 */
#define SHREFCNT_NONSECURE_FLAG		0x1UL
#define SHREFCNT_SECURE_STEP		0x2UL
#define SHREFCNT_MAX			(UINT32_MAX / 2)

/* Return 1 if refcnt increments from 0, else return 0 */
static inline int stm32mp_incr_shrefcnt(unsigned int *refcnt, bool secure)
{
	int rc = !*refcnt;

	if (secure) {
		*refcnt += SHREFCNT_SECURE_STEP;
		if (*refcnt >= SHREFCNT_MAX) {
			panic();
		}
	} else {
		*refcnt |= SHREFCNT_NONSECURE_FLAG;
	}

	return rc;
}

/* Return 1 if refcnt decrements to 0, else return 0 */
static inline int stm32mp_decr_shrefcnt(unsigned int *refcnt, bool secure)
{
	int  rc = 0;

	if (secure) {
		if (*refcnt < SHREFCNT_MAX) {
			if (*refcnt < SHREFCNT_SECURE_STEP) {
				panic();
			}
			*refcnt -= SHREFCNT_SECURE_STEP;
			rc = !*refcnt;
		}
	} else {
		rc = (*refcnt == SHREFCNT_NONSECURE_FLAG) ? 1 : 0;
		*refcnt &= ~SHREFCNT_NONSECURE_FLAG;
	}

	return rc;
}

static inline int stm32mp_incr_refcnt(unsigned int *refcnt)
{
	return stm32mp_incr_shrefcnt(refcnt, true);
}

static inline int stm32mp_decr_refcnt(unsigned int *refcnt)
{
	return stm32mp_decr_shrefcnt(refcnt, true);
}

#endif /* STM32MP_SHRES_HELPERS_H */