-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathve.c
271 lines (224 loc) · 5.17 KB
/
ve.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
/*
* Copyright (c) 2013 Jens Kuske <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <stropts.h>
#include <sys/mman.h>
#include "ve.h"
#define DEVICE "/dev/cedar_dev"
#define PAGE_OFFSET (0xc0000000) // from kernel
#define PAGE_SIZE (4096)
enum IOCTL_CMD
{
IOCTL_UNKOWN = 0x100,
IOCTL_GET_ENV_INFO,
#ifdef MAINLINE
IOCTL_WAIT_VE_DE,
IOCTL_WAIT_VE_EN,
#else
IOCTL_WAIT_VE,
#endif
IOCTL_RESET_VE,
IOCTL_ENABLE_VE,
IOCTL_DISABLE_VE,
IOCTL_SET_VE_FREQ,
IOCTL_CONFIG_AVS2 = 0x200,
IOCTL_GETVALUE_AVS2 ,
IOCTL_PAUSE_AVS2 ,
IOCTL_START_AVS2 ,
IOCTL_RESET_AVS2 ,
IOCTL_ADJUST_AVS2,
IOCTL_ENGINE_REQ,
IOCTL_ENGINE_REL,
IOCTL_ENGINE_CHECK_DELAY,
IOCTL_GET_IC_VER,
IOCTL_ADJUST_AVS2_ABS,
IOCTL_FLUSH_CACHE,
IOCTL_SET_REFCOUNT,
IOCTL_FLUSH_CACHE_ALL,
IOCTL_TEST_VERSION,
IOCTL_READ_REG = 0x300,
IOCTL_WRITE_REG,
IOCTL_SET_VOL = 0x400,
IOCTL_WAIT_JPEG_DEC = 0x500,
/*for get the ve ref_count for ipc to delete the semphore*/
IOCTL_GET_REFCOUNT,
};
struct ve_info
{
uint32_t reserved_mem;
int reserved_mem_size;
uint32_t registers;
};
struct cedarv_cache_range
{
long start;
long end;
};
static int fd = -1;
void *regs = NULL;
static int version = 0;
struct memchunk_t
{
uint32_t phys_addr;
int size;
void *virt_addr;
struct memchunk_t *next;
};
static struct memchunk_t first_memchunk = { .phys_addr = 0x0, .size = 0, .virt_addr = NULL, .next = NULL };
int ve_open(void)
{
if (fd != -1)
return 0;
struct ve_info ve;
fd = open(DEVICE, O_RDWR);
if (fd == -1)
return 0;
if (ioctl(fd, IOCTL_GET_ENV_INFO, (void *)(&ve)) == -1)
{
close(fd);
fd = -1;
return 0;
}
regs = mmap(NULL, 0x800, PROT_READ | PROT_WRITE, MAP_SHARED, fd, ve.registers);
first_memchunk.phys_addr = ve.reserved_mem - PAGE_OFFSET;
first_memchunk.size = ve.reserved_mem_size;
ioctl(fd, IOCTL_ENGINE_REQ, 0);
ioctl(fd, IOCTL_ENABLE_VE, 0);
ioctl(fd, IOCTL_SET_VE_FREQ, 320);
ioctl(fd, IOCTL_RESET_VE, 0);
writel(0x00130007, regs + VE_CTRL);
version = readl(regs + VE_VERSION) >> 16;
printf("[SUNXI] VE version 0x%04x opened.\n", version);
return 1;
}
void ve_close(void)
{
if (fd == -1)
return;
ioctl(fd, IOCTL_DISABLE_VE, 0);
ioctl(fd, IOCTL_ENGINE_REL, 0);
munmap(regs, 0x800);
close(fd);
fd = -1;
}
void ve_flush_cache(void *start, int len)
{
if (fd == -1)
return;
struct cedarv_cache_range range =
{
.start = (int)start,
.end = (int)(start + len)
};
ioctl(fd, IOCTL_FLUSH_CACHE, (void*)(&range));
}
void *ve_get_regs(void)
{
if (fd == -1)
return NULL;
return regs;
}
int ve_get_version(void)
{
return version;
}
int ve_wait(int timeout)
{
if (fd == -1)
return 0;
#ifdef MAINLINE
return ioctl(fd, IOCTL_WAIT_VE_EN, timeout);
#else
return ioctl(fd, IOCTL_WAIT_VE, timeout);
#endif
}
void *ve_malloc(int size)
{
if (fd == -1)
return NULL;
size = (size + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1);
struct memchunk_t *c, *best_chunk = NULL;
for (c = &first_memchunk; c != NULL; c = c->next)
if(c->virt_addr == NULL && c->size >= size)
{
if (best_chunk == NULL || c->size < best_chunk->size)
best_chunk = c;
if (c->size == size)
break;
}
if (!best_chunk)
return NULL;
int left_size = best_chunk->size - size;
best_chunk->virt_addr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, best_chunk->phys_addr + PAGE_OFFSET);
best_chunk->size = size;
if (left_size > 0)
{
c = malloc(sizeof(struct memchunk_t));
c->phys_addr = best_chunk->phys_addr + size;
c->size = left_size;
c->virt_addr = NULL;
c->next = best_chunk->next;
best_chunk->next = c;
}
return best_chunk->virt_addr;
}
void ve_free(void *ptr)
{
if (fd == -1)
return;
if (ptr == NULL)
return;
struct memchunk_t *c;
for (c = &first_memchunk; c != NULL; c = c->next)
if (c->virt_addr == ptr)
{
munmap(ptr, c->size);
c->virt_addr = NULL;
break;
}
for (c = &first_memchunk; c != NULL; c = c->next)
if (c->virt_addr == NULL)
while (c->next != NULL && c->next->virt_addr == NULL)
{
struct memchunk_t *n = c->next;
c->size += n->size;
c->next = n->next;
free(n);
}
}
uint32_t ve_virt2phys(void *ptr)
{
if (fd == -1)
return 0;
struct memchunk_t *c;
for (c = &first_memchunk; c != NULL; c = c->next)
{
if (c->virt_addr == NULL)
continue;
if (c->virt_addr == ptr)
return c->phys_addr;
else if (ptr > c->virt_addr && ptr < (c->virt_addr + c->size))
return c->phys_addr + (ptr - c->virt_addr);
}
return 0;
}