bpmp.c 5.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <arch_helpers.h>
#include <assert.h>
#include <bpmp.h>
#include <common/debug.h>
#include <delay_timer.h>
#include <errno.h>
#include <mmio.h>
#include <platform.h>
#include <stdbool.h>
#include <string.h>
#include <tegra_def.h>

19
#define BPMP_TIMEOUT	500 /* 500ms */
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

static uint32_t channel_base[NR_CHANNELS];
static uint32_t bpmp_init_state = BPMP_INIT_PENDING;

static uint32_t channel_field(unsigned int ch)
{
	return mmio_read_32(TEGRA_RES_SEMA_BASE + STA_OFFSET) & CH_MASK(ch);
}

static bool master_free(unsigned int ch)
{
	return channel_field(ch) == MA_FREE(ch);
}

static bool master_acked(unsigned int ch)
{
	return channel_field(ch) == MA_ACKD(ch);
}

static void signal_slave(unsigned int ch)
{
	mmio_write_32(TEGRA_RES_SEMA_BASE + CLR_OFFSET, CH_MASK(ch));
}

static void free_master(unsigned int ch)
{
	mmio_write_32(TEGRA_RES_SEMA_BASE + CLR_OFFSET,
		      MA_ACKD(ch) ^ MA_FREE(ch));
}

/* should be called with local irqs disabled */
int32_t tegra_bpmp_send_receive_atomic(int mrq, const void *ob_data, int ob_sz,
		void *ib_data, int ib_sz)
{
	unsigned int ch = (unsigned int)plat_my_core_pos();
	mb_data_t *p = (mb_data_t *)(uintptr_t)channel_base[ch];
	int32_t ret = -ETIMEDOUT, timeout = 0;

	if (bpmp_init_state == BPMP_INIT_COMPLETE) {

		/* loop until BPMP is free */
61
		for (timeout = 0; timeout < BPMP_TIMEOUT; timeout++) {
62
63
64
65
			if (master_free(ch) == true) {
				break;
			}

66
			mdelay(1);
67
68
		}

69
		if (timeout != BPMP_TIMEOUT) {
70
71
72
73
74
75
76
77
78

			/* generate the command struct */
			p->code = mrq;
			p->flags = DO_ACK;
			(void)memcpy((void *)p->data, ob_data, (size_t)ob_sz);

			/* signal command ready to the BPMP */
			signal_slave(ch);
			mmio_write_32(TEGRA_PRI_ICTLR_BASE + CPU_IEP_FIR_SET,
79
				      (1U << INT_SHR_SEM_OUTBOX_FULL));
80
81

			/* loop until the command is executed */
82
			for (timeout = 0; timeout < BPMP_TIMEOUT; timeout++) {
83
84
85
86
				if (master_acked(ch) == true) {
					break;
				}

87
				mdelay(1);
88
89
			}

90
			if (timeout != BPMP_TIMEOUT) {
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

				/* get the command response */
				(void)memcpy(ib_data, (const void *)p->data,
					     (size_t)ib_sz);

				/* return error code */
				ret = p->code;

				/* free this channel */
				free_master(ch);
			}
		}

	} else {
		/* return error code */
		ret = -EINVAL;
	}

109
110
	if (timeout == BPMP_TIMEOUT) {
		ERROR("Timed out waiting for bpmp's response\n");
111
112
113
114
115
116
117
	}

	return ret;
}

int tegra_bpmp_init(void)
{
118
	uint32_t val, base, timeout = BPMP_TIMEOUT;
119
120
121
	unsigned int ch;
	int ret = 0;

122
	if (bpmp_init_state == BPMP_INIT_PENDING) {
123
124

		/* check if the bpmp processor is alive. */
125
126
127
128
129
130
		do {
			val = mmio_read_32(TEGRA_RES_SEMA_BASE + STA_OFFSET);
			if (val != SIGN_OF_LIFE) {
				mdelay(1);
				timeout--;
			}
131

132
		} while ((val != SIGN_OF_LIFE) && (timeout > 0U));
133

134
		if (val == SIGN_OF_LIFE) {
135

136
137
138
139
140
			/* check if clock for the atomics block is enabled */
			val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_CLK_ENB_V);
			if ((val & CAR_ENABLE_ATOMICS) == 0) {
				ERROR("Clock to the atomics block is disabled\n");
			}
141

142
143
144
145
146
			/* check if the atomics block is out of reset */
			val = mmio_read_32(TEGRA_CAR_RESET_BASE + TEGRA_RST_DEV_CLR_V);
			if ((val & CAR_ENABLE_ATOMICS) == CAR_ENABLE_ATOMICS) {
				ERROR("Reset to the atomics block is asserted\n");
			}
147

148
149
			/* base address to get the result from Atomics */
			base = TEGRA_ATOMICS_BASE + RESULT0_REG_OFFSET;
150

151
152
			/* channel area is setup by BPMP before signaling handshake */
			for (ch = 0; ch < NR_CHANNELS; ch++) {
153

154
155
156
				/* issue command to get the channel base address */
				mmio_write_32(base, (ch << TRIGGER_ID_SHIFT) |
					      ATOMIC_CMD_GET);
157

158
159
				/* get the base address for the channel */
				channel_base[ch] = mmio_read_32(base);
160

161
162
163
164
165
166
167
168
169
170
171
172
173
174
				/* increment result register offset */
				base += 4U;
			}

			/* mark state as "initialized" */
			bpmp_init_state = BPMP_INIT_COMPLETE;

			/* the channel values have to be visible across all cpus */
			flush_dcache_range((uint64_t)channel_base,
					   sizeof(channel_base));
			flush_dcache_range((uint64_t)&bpmp_init_state,
					   sizeof(bpmp_init_state));

			INFO("%s: done\n", __func__);
175

176
177
		} else {
			ERROR("BPMP not powered on\n");
178
179
180
181
182

			/* bpmp is not present in the system */
			bpmp_init_state = BPMP_NOT_PRESENT;

			/* communication timed out */
183
184
			ret = -ETIMEDOUT;
		}
185
186
187
188
	}

	return ret;
}
189
190
191
192

void tegra_bpmp_suspend(void)
{
	/* freeze the interface */
193
194
195
196
197
	if (bpmp_init_state == BPMP_INIT_COMPLETE) {
		bpmp_init_state = BPMP_SUSPEND_ENTRY;
		flush_dcache_range((uint64_t)&bpmp_init_state,
				   sizeof(bpmp_init_state));
	}
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
}

void tegra_bpmp_resume(void)
{
	uint32_t val, timeout = 0;

	if (bpmp_init_state == BPMP_SUSPEND_ENTRY) {

		/* check if the bpmp processor is alive. */
		do {

			val = mmio_read_32(TEGRA_RES_SEMA_BASE + STA_OFFSET);
			if (val != SIGN_OF_LIFE) {
				mdelay(1);
				timeout++;
			}

		} while ((val != SIGN_OF_LIFE) && (timeout < BPMP_TIMEOUT));

		if (val == SIGN_OF_LIFE) {

			INFO("%s: BPMP took %d ms to resume\n", __func__, timeout);

			/* mark state as "initialized" */
			bpmp_init_state = BPMP_INIT_COMPLETE;

			/* state has to be visible across all cpus */
			flush_dcache_range((uint64_t)&bpmp_init_state,
					   sizeof(bpmp_init_state));
		} else {
			ERROR("BPMP not powered on\n");
		}
	}
}