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
|
/*
* Samsung Exynos SoC series VPU driver
*
* Copyright (c) 2015 Samsung Electronics Co., Ltd
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/syscalls.h>
#include <linux/vmalloc.h>
#include "vpu-config.h"
#include "vpu-binary.h"
static noinline_for_stack long __get_file_size(struct file *file)
{
struct kstat st;
if (vfs_getattr(&file->f_path, &st))
return -1;
if (!S_ISREG(st.mode))
return -1;
if (st.size != (long)st.size)
return -1;
return st.size;
}
int vpu_binary_init(struct vpu_binary *binary,
struct device *dev,
char *fpath1,
char *fpath2,
char *fname)
{
int ret = 0;
BUG_ON(!binary);
BUG_ON(!dev);
binary->dev = dev;
binary->image_size = 0;
snprintf(binary->fpath1, sizeof(binary->fpath1), "%s%s", fpath1, fname);
snprintf(binary->fpath2, sizeof(binary->fpath2), "%s%s", fpath2, fname);
return ret;
}
int vpu_binary_g_size(struct vpu_binary *binary, size_t *size)
{
int ret = 0;
mm_segment_t old_fs;
struct file *fp;
size_t fsize = 0;
old_fs = get_fs();
set_fs(KERNEL_DS);
fp = filp_open(binary->fpath1, O_RDONLY, 0);
if (IS_ERR_OR_NULL(fp)) {
set_fs(old_fs);
ret = -EINVAL;
goto p_err;
}
fsize = __get_file_size(fp);
if (fsize <= 0) {
vpu_err("__get_file_size is fail(%ld)\n", fsize);
filp_close(fp, current->files);
set_fs(old_fs);
ret = -EINVAL;
goto p_err;
}
p_err:
*size = fsize;
return ret;
}
int vpu_binary_read(struct vpu_binary *binary,
void *target,
size_t target_size)
{
int ret = 0;
const struct firmware *fw_blob;
u8 *buf;
struct file *fp;
mm_segment_t old_fs;
long fsize, nread;
BUG_ON(!binary);
old_fs = get_fs();
set_fs(KERNEL_DS);
fp = filp_open(binary->fpath1, O_RDONLY, 0);
if (IS_ERR_OR_NULL(fp)) {
fp = filp_open(binary->fpath2, O_RDONLY, 0);
if (IS_ERR_OR_NULL(fp)) {
set_fs(old_fs);
goto request_fw;
}
}
fsize = __get_file_size(fp);
if (fsize <= 0) {
vpu_err("__get_file_size is fail(%ld)\n", fsize);
ret = -EBADF;
goto p_err;
}
buf = vmalloc(fsize);
if (!buf) {
vpu_err("vmalloc is fail\n");
ret = -ENOMEM;
goto p_err;
}
nread = kernel_read(fp, 0, buf, fsize);
if (nread != fsize) {
vpu_err("kernel_read is fail(%ld != %ld)\n", nread, fsize);
ret = -EIO;
goto p_err;
}
if (fsize > target_size) {
vpu_err("image size is over(%ld > %ld)\n", fsize, target_size);
ret = -EIO;
goto p_err;
}
binary->image_size = fsize;
/* no cache operation, because target is sram of vpu */
#ifdef CONFIG_EXYNOS_VPU_HARDWARE
memcpy(target, (void *)buf, fsize);
#endif
vpu_info("FW(%s, %ld) were applied successfully.\n", binary->fpath1, fsize);
p_err:
vfree(buf);
filp_close(fp, current->files);
set_fs(old_fs);
return ret;
request_fw:
ret = request_firmware(&fw_blob, VPU_FW_NAME, binary->dev);
if (ret) {
vpu_err("request_firmware(%s) is fail(%d)", binary->fpath2, ret);
ret = -EINVAL;
goto request_err;
}
if (!fw_blob) {
vpu_err("fw_blob is NULL\n");
ret = -EINVAL;
goto request_err;
}
if (!fw_blob->data) {
vpu_err("fw_blob->data is NULL\n");
ret = -EINVAL;
goto request_err;
}
if (fw_blob->size > target_size) {
vpu_err("image size is over(%ld > %ld)\n", binary->image_size, target_size);
ret = -EIO;
goto p_err;
}
binary->image_size = fw_blob->size;
#ifdef CONFIG_EXYNOS_VPU_HARDWARE
memcpy(target, fw_blob->data, fw_blob->size);
#endif
vpu_info("Binay(%s, %ld) were applied successfully.\n", binary->fpath2, fw_blob->size);
request_err:
release_firmware(fw_blob);
return ret;
}
int vpu_binary_write(struct vpu_binary *binary,
void *target,
size_t target_size)
{
int ret = 0;
struct file *fp;
mm_segment_t old_fs;
long nwrite;
BUG_ON(!binary);
old_fs = get_fs();
set_fs(KERNEL_DS);
fp = filp_open(binary->fpath1, O_RDWR | O_CREAT, 0);
if (IS_ERR_OR_NULL(fp)) {
set_fs(old_fs);
vpu_err("filp_open is fail(%p)\n", fp);
ret = -EBADF;
goto p_err;
}
nwrite = kernel_write(fp, target, target_size, 0);
if (nwrite != target_size) {
filp_close(fp, current->files);
set_fs(old_fs);
vpu_err("kernel_write is fail(%ld != %ld)\n", nwrite, target_size);
ret = -EIO;
goto p_err;
}
binary->image_size = target_size;
vpu_info("Binay(%s, %ld) were applied successfully.\n", binary->fpath1, target_size);
filp_close(fp, current->files);
set_fs(old_fs);
p_err:
return ret;
}
|