forked from aws/aws-fpga
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfpga_hal_mbox.c
320 lines (267 loc) · 7.51 KB
/
fpga_hal_mbox.c
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
* Copyright 2015-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may
* not use this file except in compliance with the License. A copy of the
* License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
/** @file
* FPGA HAL mailbox operations
*/
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <time.h>
#include <errno.h>
#include <hal/fpga_common.h>
#include <fpga_hal_mbox.h>
#include <utils/lcd.h>
#include "fpga_hal_mbox_regs.h"
static struct fpga_hal_mbox_private {
struct fpga_hal_mbox mbox;
} priv;
static void
fpga_hal_mbox_print_reg_offsets(void)
{
log_debug("FMB_BASE=0x%08x, status=0x%08x",
FMB_BASE, FMB_REG_STATUS);
log_debug("Reg Offsets: mb_wr_index=0x%08x, mb_wr_data=0x%08x, mb_wr_len=0x%08x",
FMB_REG_WR_INDEX, FMB_REG_WR_DATA, FMB_REG_WR_LEN);
log_debug("Reg Offsets: mb_rd_index=0x%08x, mb_rd_data=0x%08x, mb_rd_len=0x%08x",
FMB_REG_RD_INDEX, FMB_REG_RD_DATA, FMB_REG_RD_LEN);
}
int
fpga_hal_mbox_init(struct fpga_hal_mbox *mbox)
{
log_debug("enter");
assert(mbox);
priv.mbox = *mbox;
#if 1
fpga_hal_mbox_print_reg_offsets();
#endif
return 0;
}
int
fpga_hal_mbox_reset(pci_bar_handle_t handle)
{
/** Clear any existing state */
uint32_t val;
int ret = fpga_pci_peek(handle, FMB_REG_STATUS, &val);
fail_on(ret != 0, err, "fpga_pci_peek(status) failed");
if (val & FMB_RX_EVT) {
ret = fpga_pci_poke(handle, FMB_REG_STATUS, FMB_RX_EVT);
fail_on(ret != 0, err, "fpga_pci_poke(status) failed");
}
if (val & FMB_TX_EVT) {
ret = fpga_pci_poke(handle, FMB_REG_STATUS, FMB_TX_EVT);
fail_on(ret != 0, err, "fpga_pci_poke(status) failed");
}
return 0;
err:
return -1;
}
int
fpga_hal_mbox_attach(pci_bar_handle_t handle, bool clear_state)
{
if (clear_state) {
int ret = fpga_hal_mbox_reset(handle);
fail_on(ret != 0, err, "fpga_hal_mbox_reset failed");
}
return 0;
err:
return -1;
}
int
fpga_hal_mbox_detach(pci_bar_handle_t handle, bool clear_state)
{
if (clear_state) {
int ret = fpga_hal_mbox_reset(handle);
fail_on(ret != 0, err, "fpga_hal_mbox_reset failed");
}
return 0;
err:
return -1;
}
int
fpga_hal_mbox_get_versions(pci_bar_handle_t handle,
struct fpga_hal_mbox_versions *ver)
{
log_debug("enter");
int ret = fpga_pci_peek(handle, FMB_REG_SH_VERSION, &ver->sh_version);
fail_on(ret != 0, err, "Error reading sh_version register");
log_debug("returning sh_version=0x%08x", ver->sh_version);
return 0;
err:
return -1;
}
static int
fpga_hal_mbox_check_len(uint32_t len)
{
fail_on(len < sizeof(uint32_t), err, "len(%u) < %u",
len, (uint32_t) sizeof(uint32_t));
fail_on(len & 0x3, err, "Len must be a multiple of 4");
fail_on(len > FPGA_MBOX_MSG_DATA_LEN, err, "len(%u) > %u",
len, FPGA_MBOX_MSG_DATA_LEN);
return 0;
err:
return -1;
}
int
fpga_hal_mbox_read_async(pci_bar_handle_t handle, void *msg, uint32_t *len)
{
log_debug("enter");
assert(msg);
assert(len);
uint32_t val;
int ret = fpga_pci_peek(handle, FMB_REG_STATUS, &val);
fail_on(ret != 0, err, "fpga_pci_peek(status) failed");
/** Check if an RX event is available */
if (!(val & FMB_RX_EVT)) {
log_debug("RX msg not available");
goto err_again;
}
/** Read and check the length */
uint32_t mb_rd_len;
ret = fpga_pci_peek(handle, FMB_REG_RD_LEN, &mb_rd_len);
fail_on(ret != 0, err_rx_ack, "fpga_pci_peek(mb_rd_len) failed");
ret = fpga_hal_mbox_check_len(mb_rd_len << 2);
fail_on(ret != 0, err_rx_ack, "fpga_hal_mbox_check_len failed");
/** Reset the read index to 0 */
ret = fpga_pci_poke(handle, FMB_REG_RD_INDEX, 0);
fail_on(ret != 0, err_rx_ack, "fpga_pci_poke(mb_rd_index) failed");
/** Read the data. Index is auto-incremented */
uint32_t i;
uint32_t *m32 = msg;
for (i = 0; i < mb_rd_len; i++) {
ret = fpga_pci_peek(handle, FMB_REG_RD_DATA, m32);
fail_on(ret != 0, err_rx_ack, "fpga_pci_peek(mb_rd_data) failed");
m32++;
}
/** Acknowledge the RX event */
ret = fpga_pci_poke(handle, FMB_REG_STATUS, FMB_RX_EVT);
fail_on(ret != 0, err, "fpga_pci_poke(status) failed");
*len = mb_rd_len << 2;
log_debug("Read len=%u", *len);
return 0;
err_again:
return -EAGAIN;
err_rx_ack:
/** Acknowledge the RX event */
ret = fpga_pci_poke(handle, FMB_REG_STATUS, FMB_RX_EVT);
fail_on(ret != 0, err, "fpga_pci_poke(status) failed");
err:
return -1;
}
/** Test and Clear (TC) async write acknowledgement */
int
fpga_hal_mbox_write_async_tc_ack(pci_bar_handle_t handle, bool *ack)
{
uint32_t val;
int ret = fpga_pci_peek(handle, FMB_REG_STATUS, &val);
fail_on(ret != 0, err, "fpga_pci_peek(status) failed");
/** Check for TX event */
if (val & FMB_TX_EVT) {
/** Acknowledge the TX event */
ret = fpga_pci_poke(handle, FMB_REG_STATUS, FMB_TX_EVT);
fail_on(ret != 0, err, "fpga_pci_poke(status) failed");
/** Setup the return */
*ack = true;
} else {
/** Setup the return */
*ack = false;
}
return 0;
err:
return -1;
}
int
fpga_hal_mbox_write_async(pci_bar_handle_t handle, void *msg, uint32_t len)
{
log_debug("enter");
assert(msg);
int ret = fpga_hal_mbox_check_len(len);
fail_on(ret != 0, err, "fpga_hal_mbox_check_len failed");
/** Clear any previous async write state */
bool ack;
ret = fpga_hal_mbox_write_async_tc_ack(handle, &ack);
fail_on(ret != 0, err, "fpga_hal_mbox_write_async_tc_ack failed");
/** Reset the write index to 0 */
ret = fpga_pci_poke(handle, FMB_REG_WR_INDEX, 0);
fail_on(ret != 0, err, "fpga_pci_poke(mb_wr_index) failed");
/** Write the data. Index is auto-incremented */
uint32_t mb_wr_len = len >> 2;
uint32_t *m32 = msg;
uint32_t i;
for (i = 0; i < mb_wr_len; i++) {
ret = fpga_pci_poke(handle, FMB_REG_WR_DATA, *m32);
fail_on(ret != 0, err, "fpga_pci_poke(mb_wr_data) failed");
m32++;
}
/** Write the (32b word) data length */
ret = fpga_pci_poke(handle, FMB_REG_WR_LEN, mb_wr_len);
fail_on(ret != 0, err, "fpga_pci_poke(mb_wr_len) failed");
log_debug("Wrote len=%u", len);
return 0;
err:
return -1;
}
int
fpga_hal_mbox_read(pci_bar_handle_t handle, void *msg, uint32_t *len)
{
log_debug("enter");
assert(msg);
assert(len);
uint32_t count = priv.mbox.timeout;
while (count) {
int ret = fpga_hal_mbox_read_async(handle, msg, len);
if (ret == 0) {
goto out;
}
/** Sleep and retry on EAGAIN, otherwise error out of loop */
fail_on(ret != -EAGAIN, err, "fpga_hal_mbox_read_async failed");
msleep(priv.mbox.delay_msec);
count--;
}
fail_on(!count, err, "Timeout on mbox read, timeout=%u, delay_msec=%u",
priv.mbox.timeout, priv.mbox.delay_msec);
out:
return 0;
err:
return -1;
}
int
fpga_hal_mbox_write(pci_bar_handle_t handle, void *msg, uint32_t len)
{
log_debug("enter");
assert(msg);
int ret = fpga_hal_mbox_write_async(handle, msg, len);
fail_on(ret != 0, err, "fpga_hal_mbox_write_async failed");
uint32_t count = priv.mbox.timeout;
while (count) {
bool ack = false;;
ret = fpga_hal_mbox_write_async_tc_ack(handle, &ack);
fail_on(ret != 0, err, "fpga_hal_mbox_write_async_tc_ack failed");
if (ack) {
goto out;
}
/** Sleep and try again */
msleep(priv.mbox.delay_msec);
count--;
}
fail_on(!count, err, "Timeout on mbox write, timeout=%u, delay_msec=%u",
priv.mbox.timeout, priv.mbox.delay_msec);
out:
return 0;
err:
return -1;
}