summaryrefslogtreecommitdiffstats
path: root/exynos4/hal/libsecion/libsecion.cpp
blob: 614c6e1c8ca69e0eaa5a0307cea68dad17902a63 (plain)
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
/*
 * Copyright (C) 2012 Samsung Electronics Co., Ltd.
 *
 * 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.
 */

#define LOG_TAG "libsecion"

#include <secion.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <cutils/log.h>

#define ION_IOC_MAGIC 'I'
#define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, struct ion_allocation_data)
#define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
#define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
#define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
#define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, int)
#define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)

typedef unsigned long ion_handle;

struct ion_allocation_data {
    size_t len;
    size_t align;
    unsigned int flags;
    ion_handle *handle;
};

struct ion_fd_data {
    ion_handle *handle;
    int fd;
};

struct ion_handle_data {
    ion_handle *handle;
};

struct ion_custom_data {
    unsigned int cmd;
    unsigned long arg;
};

struct ion_msync_data {
    enum ION_MSYNC_FLAGS dir;
    int fd;
    size_t size;
    off_t offset;
};

struct ion_phys_data {
    int fd;
    ion_phys_addr_t phys;
    size_t size;
};

enum ION_EXYNOS_CUSTOM_CMD {
    ION_EXYNOS_CUSTOM_MSYNC,
    ION_EXYNOS_CUSTOM_PHYS
};

ion_client ion_client_create(void)
{
    return open("/dev/ion", O_RDWR);
}

void ion_client_destroy(ion_client client)
{
    close(client);
}

ion_buffer ion_alloc(ion_client client, size_t len, size_t align, unsigned int flags)
{
    int ret;
    struct ion_handle_data arg_free;
    struct ion_fd_data arg_share;
    struct ion_allocation_data arg_alloc;

    arg_alloc.len = len;
    arg_alloc.align = align;
    arg_alloc.flags = flags;

    ret = ioctl(client, ION_IOC_ALLOC, &arg_alloc);
    if (ret < 0)
        return ret;

    arg_share.handle = arg_alloc.handle;
    ret = ioctl(client, ION_IOC_SHARE, &arg_share);

    arg_free.handle = arg_alloc.handle;
    ioctl(client, ION_IOC_FREE, &arg_free);

    if (ret < 0)
        return ret;

    return arg_share.fd;
}

void ion_free(ion_buffer buffer)
{
    close(buffer);
}

void *ion_map(ion_buffer buffer, size_t len, off_t offset)
{
    return mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
                buffer, offset);
}

int ion_unmap(void *addr, size_t len)
{
    return munmap(addr, len);
}

int ion_msync(ion_client client, ion_buffer buffer, enum ION_MSYNC_FLAGS flags, size_t size, off_t offset)
{
    struct ion_msync_data arg_cdata;
    arg_cdata.size = size;
    arg_cdata.dir = flags;
    arg_cdata.fd = buffer;
    arg_cdata.offset = offset;

    struct ion_custom_data arg_custom;
    arg_custom.cmd = ION_EXYNOS_CUSTOM_MSYNC;
    arg_custom.arg = (unsigned long) &arg_cdata;

    return ioctl(client, ION_IOC_CUSTOM, &arg_custom);
}

ion_phys_addr_t ion_getphys(ion_client client, ion_buffer buffer)
{
    struct ion_phys_data arg_cdata;
    arg_cdata.fd = buffer;

    struct ion_custom_data arg_custom;
    arg_custom.cmd = ION_EXYNOS_CUSTOM_PHYS;
    arg_custom.arg = (unsigned long) &arg_cdata;

    if(ioctl(client, ION_IOC_CUSTOM, &arg_custom) < 0)
        return 0;

    return arg_cdata.phys;
}

int createIONMem(struct secion_param *param, size_t size, unsigned int flags)
{
    if(param->client < 0 && (param->client = ion_client_create()) < 0) {
        ALOGE("createIONMem:: ion_client_create fail\n");
        goto fail;
    }

    if(param->buffer < 0 && (param->buffer = ion_alloc(param->client, size, 0x10000, flags)) < 0) {
        ALOGE("createIONMem:: ion_alloc fail\n");
        goto fail;
    }

    if((param->physaddr = ion_getphys(param->client, param->buffer)) == 0) {
        ALOGE("createIONMem:: ion_getphys fail, phys_addr = 0\n");
        goto fail;
    }

    if((param->memory = ion_map(param->buffer, size, 0)) == (void*)-1) {
        ALOGE("createIONMem:: ion_map fail\n");
        goto fail;
    } else {
        param->size = size;
        return 0;
    }

fail:
    if(param->memory > 0) munmap(param->memory, size);
    if(param->buffer > 0) ion_free(param->buffer);
    param->buffer = -1;
    param->size = 0;
    param->memory = 0;
    param->physaddr = 0;
    return -1;
}

int destroyIONMem(struct secion_param *param)
{
    if(param->memory != 0) munmap(param->memory, param->size);
    if(param->buffer >= 0) ion_free(param->buffer);
    param->buffer = -1;
    param->size = 0;
    param->memory = 0;
    param->physaddr = 0;
    return 0;
}