forked from open-power/snap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction_memcopy.c
146 lines (124 loc) · 3.57 KB
/
action_memcopy.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
/*
* Copyright 2016, 2017 International Business Machines
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.
*/
/*
* Example to use the FPGA to find patterns in a byte-stream.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <endian.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libsnap.h>
#include <linux/types.h> /* __be64 */
#include <asm/byteorder.h>
#include <snap_internal.h>
#include <snap_tools.h>
#include <action_memcopy.h>
/* Name is defined by address and size */
#define MEMORY_FILE "action_memory_%016llx_%016llx.bin"
static int mmio_write32(struct snap_card *card,
uint64_t offs, uint32_t data)
{
act_trace(" %s(%p, %llx, %x)\n", __func__, card,
(long long)offs, data);
return 0;
}
static int mmio_read32(struct snap_card *card,
uint64_t offs, uint32_t *data)
{
act_trace(" %s(%p, %llx, %x)\n", __func__, card,
(long long)offs, *data);
return 0;
}
static int action_main(struct snap_sim_action *action,
void *job, unsigned int job_len)
{
int rc;
struct memcopy_job *js = (struct memcopy_job *)job;
void *src, *dst;
size_t len;
void *ibuf = NULL;
void *obuf = NULL;
char ifname[128];
char ofname[128];
/* No error checking ... */
act_trace("%s(%p, %p, %d) type_in=%d type_out=%d jobsize %ld bytes\n",
__func__, action, job, job_len, js->in.type, js->out.type,
sizeof(*js));
__hexdump(stderr, js, sizeof(*js));
len = js->out.size;
dst = (void *)js->out.addr;
if (js->in.size != js->out.size) {
act_trace(" err: size does not match in %d bytes versus "
"out %d bytes!\n", js->in.size, js->out.size);
goto out_err;
}
/* checking parameters ... */
if (js->in.type != SNAP_ADDRTYPE_HOST_DRAM) {
snprintf(ifname, sizeof(ifname), MEMORY_FILE,
(long long)js->in.addr, (long long)js->in.size);
act_trace(" loading input data from %s\n", ifname);
ibuf = malloc(len);
if (ibuf == NULL)
goto out_err;
rc = __file_read(ifname, ibuf, len);
if (rc < 0)
goto out_err;
src = ibuf;
} else
src = (void *)js->in.addr;
if (js->out.type != SNAP_ADDRTYPE_HOST_DRAM) {
snprintf(ofname, sizeof(ofname), MEMORY_FILE,
(long long)js->out.addr, (long long)js->out.size);
act_trace(" writing output data to %s\n", ofname);
rc = __file_write(ofname, src, len);
if (rc < 0)
goto out_err;
goto out_ok;
} else {
act_trace(" copy %p to %p %ld bytes\n", src, dst, len);
memcpy(dst, src, len);
}
out_ok:
action->job.retc = SNAP_RETC_SUCCESS;
return 0;
out_err:
__free(ibuf);
__free(obuf);
action->job.retc = SNAP_RETC_FAILURE;
return 0;
}
static struct snap_sim_action action = {
.vendor_id = SNAP_VENDOR_ID_ANY,
.device_id = SNAP_DEVICE_ID_ANY,
.action_type = MEMCOPY_ACTION_TYPE,
.job = { .retc = SNAP_RETC_FAILURE, },
.state = ACTION_IDLE,
.main = action_main,
.priv_data = NULL, /* this is passed back as void *card */
.mmio_write32 = mmio_write32,
.mmio_read32 = mmio_read32,
.next = NULL,
};
static void _init(void) __attribute__((constructor));
static void _init(void)
{
snap_action_register(&action);
}