blob: 32781f7a9cd85fff544b3f2bb108db223e6ef88f [file] [log] [blame]
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -08001/*
2 * ion.c
3 *
4 * Memory Allocator functions for ion
5 *
6 * Copyright 2011 Google, Inc
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20#define LOG_TAG "ion"
21
22#include <cutils/log.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <stdio.h>
26#include <sys/ioctl.h>
27#include <sys/mman.h>
28#include <sys/types.h>
29
30#include <linux/ion.h>
31#include <ion/ion.h>
32
33int ion_open()
34{
Colin Cross92d7ca62013-11-08 19:04:18 -080035 int fd = open("/dev/ion", O_RDWR);
36 if (fd < 0)
37 ALOGE("open /dev/ion failed!\n");
38 return fd;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080039}
40
41int ion_close(int fd)
42{
Colin Crossaab47b22013-12-18 15:17:21 -080043 int ret = close(fd);
44 if (ret < 0)
45 return -errno;
46 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080047}
48
49static int ion_ioctl(int fd, int req, void *arg)
50{
Colin Cross92d7ca62013-11-08 19:04:18 -080051 int ret = ioctl(fd, req, arg);
52 if (ret < 0) {
53 ALOGE("ioctl %x failed with code %d: %s\n", req,
54 ret, strerror(errno));
55 return -errno;
56 }
57 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080058}
59
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -070060int ion_alloc(int fd, size_t len, size_t align, unsigned int heap_mask,
Colin Cross92d7ca62013-11-08 19:04:18 -080061 unsigned int flags, ion_user_handle_t *handle)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080062{
Colin Cross92d7ca62013-11-08 19:04:18 -080063 int ret;
64 struct ion_allocation_data data = {
65 .len = len,
66 .align = align,
67 .heap_id_mask = heap_mask,
68 .flags = flags,
69 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080070
Colin Cross74963182013-11-08 19:07:38 -080071 if (handle == NULL)
72 return -EINVAL;
73
Colin Cross92d7ca62013-11-08 19:04:18 -080074 ret = ion_ioctl(fd, ION_IOC_ALLOC, &data);
75 if (ret < 0)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080076 return ret;
Colin Cross92d7ca62013-11-08 19:04:18 -080077 *handle = data.handle;
78 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080079}
80
Rom Lemarchand969eac82013-10-21 15:19:56 -070081int ion_free(int fd, ion_user_handle_t handle)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080082{
Colin Cross92d7ca62013-11-08 19:04:18 -080083 struct ion_handle_data data = {
84 .handle = handle,
85 };
86 return ion_ioctl(fd, ION_IOC_FREE, &data);
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080087}
88
Rom Lemarchand969eac82013-10-21 15:19:56 -070089int ion_map(int fd, ion_user_handle_t handle, size_t length, int prot,
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080090 int flags, off_t offset, unsigned char **ptr, int *map_fd)
91{
Colin Cross74963182013-11-08 19:07:38 -080092 int ret;
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -070093 unsigned char *tmp_ptr;
Colin Cross92d7ca62013-11-08 19:04:18 -080094 struct ion_fd_data data = {
95 .handle = handle,
96 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080097
Colin Cross74963182013-11-08 19:07:38 -080098 if (map_fd == NULL)
99 return -EINVAL;
100 if (ptr == NULL)
101 return -EINVAL;
102
103 ret = ion_ioctl(fd, ION_IOC_MAP, &data);
Colin Cross92d7ca62013-11-08 19:04:18 -0800104 if (ret < 0)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800105 return ret;
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700106 if (data.fd < 0) {
Colin Cross92d7ca62013-11-08 19:04:18 -0800107 ALOGE("map ioctl returned negative fd\n");
108 return -EINVAL;
109 }
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700110 tmp_ptr = mmap(NULL, length, prot, flags, data.fd, offset);
111 if (tmp_ptr == MAP_FAILED) {
Colin Cross92d7ca62013-11-08 19:04:18 -0800112 ALOGE("mmap failed: %s\n", strerror(errno));
113 return -errno;
114 }
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700115 *map_fd = data.fd;
116 *ptr = tmp_ptr;
Colin Cross92d7ca62013-11-08 19:04:18 -0800117 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800118}
119
Rom Lemarchand969eac82013-10-21 15:19:56 -0700120int ion_share(int fd, ion_user_handle_t handle, int *share_fd)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800121{
Colin Cross92d7ca62013-11-08 19:04:18 -0800122 int map_fd;
Colin Cross74963182013-11-08 19:07:38 -0800123 int ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800124 struct ion_fd_data data = {
125 .handle = handle,
126 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800127
Colin Cross74963182013-11-08 19:07:38 -0800128 if (share_fd == NULL)
129 return -EINVAL;
130
131 ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
Colin Cross92d7ca62013-11-08 19:04:18 -0800132 if (ret < 0)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800133 return ret;
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700134 if (data.fd < 0) {
Colin Cross92d7ca62013-11-08 19:04:18 -0800135 ALOGE("share ioctl returned negative fd\n");
136 return -EINVAL;
137 }
Rom Lemarchand5cdcdce2014-10-27 16:42:10 -0700138 *share_fd = data.fd;
Colin Cross92d7ca62013-11-08 19:04:18 -0800139 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800140}
141
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700142int ion_alloc_fd(int fd, size_t len, size_t align, unsigned int heap_mask,
Colin Cross92d7ca62013-11-08 19:04:18 -0800143 unsigned int flags, int *handle_fd) {
144 ion_user_handle_t handle;
145 int ret;
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700146
Colin Cross92d7ca62013-11-08 19:04:18 -0800147 ret = ion_alloc(fd, len, align, heap_mask, flags, &handle);
148 if (ret < 0)
149 return ret;
150 ret = ion_share(fd, handle, handle_fd);
151 ion_free(fd, handle);
152 return ret;
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700153}
154
Rom Lemarchand969eac82013-10-21 15:19:56 -0700155int ion_import(int fd, int share_fd, ion_user_handle_t *handle)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800156{
Colin Cross74963182013-11-08 19:07:38 -0800157 int ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800158 struct ion_fd_data data = {
159 .fd = share_fd,
160 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800161
Colin Cross74963182013-11-08 19:07:38 -0800162 if (handle == NULL)
163 return -EINVAL;
164
165 ret = ion_ioctl(fd, ION_IOC_IMPORT, &data);
Colin Cross92d7ca62013-11-08 19:04:18 -0800166 if (ret < 0)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800167 return ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800168 *handle = data.handle;
169 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800170}
Rebecca Schultz Zavin3cfcc302012-09-20 09:46:19 -0700171
172int ion_sync_fd(int fd, int handle_fd)
173{
174 struct ion_fd_data data = {
175 .fd = handle_fd,
176 };
177 return ion_ioctl(fd, ION_IOC_SYNC, &data);
178}