-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcdxalloc.c
More file actions
182 lines (161 loc) · 4.73 KB
/
cdxalloc.c
File metadata and controls
182 lines (161 loc) · 4.73 KB
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
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "cdxalloc.h"
#define cdxalloc_simple_debug 1
//#define cdxalloc_verbose_debug 0
int fd;
cedar_env_info_t *cedar_env;
void *pos;
mapping_t **mappings;
int num_mappings = 100;
/*
cdxalloc_open sets up the cdxalloc library, it is required
to be called before the library is used.
*/
int cdxalloc_open(void)
{
int i;
// alloc cedar_env_info_t structure
cedar_env = (cedar_env_info_t*)malloc(sizeof(cedar_env_info_t));
if (!cedar_env) {
return -1;
}
memset(cedar_env, 0,sizeof(cedar_env_info_t));
// allocate memory mappings
mappings = (mapping_t **) malloc(sizeof(mapping_t *) * num_mappings);
for(i = 0; i < num_mappings; i++)
{
mappings[i] = (mapping_t *) malloc(sizeof(mapping_t));
memset(mappings[i],0,sizeof(mapping_t));
}
// open cedar_dev device
fd = open(cedar_dev_name,O_RDWR);
if (fd == -1) {
return -1;
}
// load cedar environment info into user land
ioctl(fd,IOCTL_GET_ENV_INFO,cedar_env);
#ifdef cdxalloc_simple_debug
printf("phymem_start:0x%08x\n",cedar_env->phymem_start);
#endif
pos = (void *)cedar_env->phymem_start;
return 0;
}
/*
cdxalloc_close closes down the driver, freeing memory etc.
TODO: implement this.
*/
int cdxalloc_close(void)
{
// close fd
close(fd);
// free cedar memory
// free cedar_env
return 0;
}
/*
cdxalloc_alloc creates a memory mapping into the cedarx video
decoder of a specified size.
in the mmapcall the offset (defined by the integer pos) is the
physical address at which the mapping should start at.
Observations of libceaderxalloc has shown mappings all being created
on 4096 byte boundries - so this works in the same way.
*/
void* cdxalloc_alloc(int size)
{
printf("cdxalloc_alloc: size: 0x%08x\n",size);
void *ret;
ret = mmap(NULL,size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,(int) pos);
if (ret == MAP_FAILED) {
printf("Error allocating %d bytes\n",size);
return ret;
}
cdxalloc_createmapping(ret,pos,size);
pos = pos + size;
// align to 4kb boundry
if ((unsigned int)pos % 4096 != 0) {
pos = pos + 4096 - ((unsigned int)pos % 4096);
}
return ret;
}
/*
* cdxalloc_free frees memory allocated at a particular address
* TODO: implement this.
*
*/
void cdxalloc_free(void *address)
{
//munmap(address,size)
}
/*
cdxalloc_vir2phy takes a virtual user space address which
maps to physical memory owned by the cedarx video decoder.
This address is then converted from a virtual to a phyical
address.
*/
unsigned int cdxalloc_vir2phy(void *address)
{
int i;
unsigned int ret = 0;
for(i=0;i<num_mappings;i++) {
#ifdef cdxalloc_verbose_debug
printf("cmp: 0x%08x == 0x%08x\n",(unsigned int) mappings[i]->start_virt, (unsigned int) address);
#endif
if (mappings[i]->start_virt == address) {
ret = (unsigned int) mappings[i]->start_phys;
break;
} else if (address > mappings[i]->start_virt && address < (mappings[i]->start_virt + mappings[i]->size)) {
ret = (unsigned int) mappings[i]->start_phys + (address - mappings[i]->start_virt);
break;
}
}
if (ret == 0) {
printf("couldn't find address! 0x%08x\n",(unsigned int) address);
exit(1);
} else {
#ifdef cdxalloc_verbose_debug
printf("found: 0x%08x\n",(unsigned int) address);
#endif
}
ret = ret & 0x0fffffff;
return ret;
}
/*
cdxalloc_allocregs returns a pointer to 2048bytes of memory which
contain the registers for the cedarx video decoder.
*/
void* cdxalloc_allocregs()
{
return mmap(NULL, 2048, PROT_READ | PROT_WRITE, MAP_SHARED,fd,(int)cedar_env->address_macc);
}
/*
cdxalloc_createmapping creates an entry in the mapping array for
the given arguments. Used for cdxalloc_vir2phy.
*/
void cdxalloc_createmapping(void *virt,void *phys,int size)
{
#ifdef cdxalloc_simple_debug
printf("cdxalloc_createmapping virt: 0x%08x phys: 0x%08x size: 0x%08x\n",(unsigned int) virt,(unsigned int) phys, size);
#endif
// find first unallocated mapping
int i;
for (i=0;i<num_mappings;i++) {
if (mappings[i]->start_phys == 0) {
#ifdef cdxalloc_verbose_debug
printf("creating mapping at %d\n",i);
#endif
mappings[i]->start_phys = phys;
mappings[i]->start_virt = virt;
mappings[i]->size = size;
break;
}
}
}