This repository was archived by the owner on Feb 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnvmap.c
121 lines (97 loc) · 2.26 KB
/
nvmap.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
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <asm/types.h>
#include <nvmap_ioctl.h>
#include "nvmap.h"
static int nvmap_fd = -1;
int nvmap_open(void)
{
nvmap_fd = open("/dev/nvmap", O_DSYNC | O_RDWR);
if (nvmap_fd < 0)
return -1;
return 0;
}
void nvmap_close(void)
{
close(nvmap_fd);
nvmap_fd = -1;
}
int nvmap_get_fd(void)
{
return nvmap_fd;
}
nvmap_handle_t nvmap_create(unsigned int size)
{
struct nvmap_create_handle ch;
ch.size = size;
if (ioctl(nvmap_fd, NVMAP_IOC_CREATE, &ch) < 0)
return 0;
return ch.handle;
}
int nvmap_alloc(nvmap_handle_t h)
{
struct nvmap_alloc_handle ah;
ah.handle = h;
ah.heap_mask = 0x1;
ah.flags = 0x1; /* NVMAP_HANDLE_WRITE_COMBINE */
ah.align = 0x20;
return ioctl(nvmap_fd, NVMAP_IOC_ALLOC, &ah);
}
int nvmap_write(nvmap_handle_t h, size_t offset, const void *src, size_t size)
{
struct nvmap_rw_handle rwh;
rwh.handle = h;
rwh.offset = offset;
rwh.addr = (unsigned long)src;
rwh.elem_size = rwh.hmem_stride = rwh.user_stride = size;
rwh.count = 1;
return ioctl(nvmap_fd, NVMAP_IOC_WRITE, &rwh);
}
int nvmap_read(void *dst, nvmap_handle_t h, size_t offset, size_t size)
{
struct nvmap_rw_handle rwh;
rwh.handle = h;
rwh.offset = offset;
rwh.addr = (unsigned long)dst;
rwh.elem_size = rwh.hmem_stride = rwh.user_stride = size;
rwh.count = 1;
return ioctl(nvmap_fd, NVMAP_IOC_READ, &rwh);
}
void *nvmap_mmap(nvmap_handle_t h, size_t offset, size_t length, int flags)
{
void *ptr;
struct nvmap_map_caller mc;
mc.handle = h;
mc.offset = offset;
mc.length = length;
mc.flags = flags;
ptr = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, nvmap_fd, 0);
if (ptr == MAP_FAILED)
return NULL;
mc.addr = (long)ptr;
if (ioctl(nvmap_fd, NVMAP_IOC_MMAP, &mc))
return NULL;
return ptr;
}
int nvmap_cache(void *ptr, nvmap_handle_t handle, size_t len, int op)
{
struct nvmap_cache_op co;
co.addr = (long)ptr;
co.handle = handle;
co.len = len;
co.op = op;
return ioctl(nvmap_fd, NVMAP_IOC_CACHE, &co);
}
int nvmap_param(nvmap_handle_t handle, int param, unsigned long *result)
{
struct nvmap_handle_param hp;
hp.handle = handle;
hp.param = param;
if (ioctl(nvmap_fd, NVMAP_IOC_PARAM, &hp) < 0)
return -1;
if (result)
*result = hp.result;
return 0;
}