blob: 231dbd65f387d666ca45f9940c76a1238d501d48 [file] [log] [blame]
Dima Zavinfc4b5d52012-02-29 16:40:35 -08001/*
2 * Copyright (C) 2012 Samsung Electronics Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <ion.h>
18#include <fcntl.h>
19#include <sys/mman.h>
20#include <sys/ioctl.h>
Rebecca Schultz Zavineb42d132012-06-11 16:53:41 -070021#include <cutils/log.h>
Dima Zavinfc4b5d52012-02-29 16:40:35 -080022
23typedef unsigned long ion_handle;
24
25struct ion_allocation_data {
26 size_t len;
27 size_t align;
Rebecca Schultz Zavineb42d132012-06-11 16:53:41 -070028 unsigned int heap_mask;
Dima Zavinfc4b5d52012-02-29 16:40:35 -080029 unsigned int flags;
30 ion_handle handle;
31};
32
33struct ion_fd_data {
34 ion_handle handle;
35 int fd;
36};
37
38struct ion_handle_data {
39 ion_handle handle;
40};
41
42struct ion_custom_data {
43 unsigned int cmd;
44 unsigned long arg;
45};
46
47#define ION_IOC_MAGIC 'I'
48#define ION_IOC_ALLOC _IOWR(ION_IOC_MAGIC, 0, struct ion_allocation_data)
49#define ION_IOC_FREE _IOWR(ION_IOC_MAGIC, 1, struct ion_handle_data)
50#define ION_IOC_MAP _IOWR(ION_IOC_MAGIC, 2, struct ion_fd_data)
51#define ION_IOC_SHARE _IOWR(ION_IOC_MAGIC, 4, struct ion_fd_data)
Rebecca Schultz Zavineb42d132012-06-11 16:53:41 -070052#define ION_IOC_IMPORT _IOWR(ION_IOC_MAGIC, 5, struct ion_fd_data)
Dima Zavinfc4b5d52012-02-29 16:40:35 -080053#define ION_IOC_CUSTOM _IOWR(ION_IOC_MAGIC, 6, struct ion_custom_data)
Rebecca Schultz Zavineb42d132012-06-11 16:53:41 -070054#define ION_IOC_SYNC _IOWR(ION_IOC_MAGIC, 7, struct ion_fd_data)
Dima Zavinfc4b5d52012-02-29 16:40:35 -080055
56struct ion_msync_data {
57 long flags;
58 ion_buffer buf;
59 size_t size;
60 off_t offset;
61};
62
63enum ION_EXYNOS_CUSTOM_CMD {
64 ION_EXYNOS_CUSTOM_MSYNC
65};
66
67ion_client ion_client_create(void)
68{
69 return open("/dev/ion", O_RDWR);
70}
71
72void ion_client_destroy(ion_client client)
73{
74 close(client);
75}
76
77ion_buffer ion_alloc(ion_client client, size_t len, size_t align,
Rebecca Schultz Zavineb42d132012-06-11 16:53:41 -070078 unsigned int heap_mask, unsigned int flags)
Dima Zavinfc4b5d52012-02-29 16:40:35 -080079{
80 int ret;
81 struct ion_handle_data arg_free;
82 struct ion_fd_data arg_share;
83 struct ion_allocation_data arg_alloc;
84
85 arg_alloc.len = len;
86 arg_alloc.align = align;
Rebecca Schultz Zavineb42d132012-06-11 16:53:41 -070087 arg_alloc.heap_mask = heap_mask;
Dima Zavinfc4b5d52012-02-29 16:40:35 -080088 arg_alloc.flags = flags;
89
90 ret = ioctl(client, ION_IOC_ALLOC, &arg_alloc);
91 if (ret < 0)
92 return ret;
93
94 arg_share.handle = arg_alloc.handle;
95 ret = ioctl(client, ION_IOC_SHARE, &arg_share);
96
97 arg_free.handle = arg_alloc.handle;
98 ioctl(client, ION_IOC_FREE, &arg_free);
99
100 if (ret < 0)
101 return ret;
102
103 return arg_share.fd;
104}
105
106void ion_free(ion_buffer buffer)
107{
108 close(buffer);
109}
110
111void *ion_map(ion_buffer buffer, size_t len, off_t offset)
112{
113 return mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
114 buffer, offset);
115}
116
117int ion_unmap(void *addr, size_t len)
118{
119 return munmap(addr, len);
120}
121
Rebecca Schultz Zavineb42d132012-06-11 16:53:41 -0700122int ion_sync(ion_client client, ion_buffer buffer)
Dima Zavinfc4b5d52012-02-29 16:40:35 -0800123{
Rebecca Schultz Zavineb42d132012-06-11 16:53:41 -0700124 struct ion_fd_data data;
Dima Zavinfc4b5d52012-02-29 16:40:35 -0800125
Rebecca Schultz Zavineb42d132012-06-11 16:53:41 -0700126 data.fd = buffer;
Dima Zavinfc4b5d52012-02-29 16:40:35 -0800127
Rebecca Schultz Zavineb42d132012-06-11 16:53:41 -0700128 return ioctl(client, ION_IOC_SYNC, &data);
Dima Zavinfc4b5d52012-02-29 16:40:35 -0800129}
SeungBeom Kim57e06162013-07-30 07:41:12 +0900130
131int ion_incRef(int fd, int share_fd, unsigned long **handle)
132{
133 struct ion_fd_data data;
134
135 data.fd = share_fd;
136
137 int ret = ioctl(fd, ION_IOC_IMPORT, &data);
138 if (ret < 0)
139 return ret;
140 *handle = (unsigned long*)(data.handle);
141 return ret;
142}
143
144int ion_decRef(int fd, unsigned long *handle)
145{
146 struct ion_handle_data data;
147 data.handle = (ion_handle)handle;
148
149 return ioctl(fd, ION_IOC_FREE, &data);
150}