blob: 490893258cce3cad7ae2fe94f73979a8d9cfe8a7 [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>
Elliott Hughesa744b052015-01-28 11:37:57 -080026#include <string.h>
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080027#include <sys/ioctl.h>
28#include <sys/mman.h>
29#include <sys/types.h>
30
31#include <linux/ion.h>
32#include <ion/ion.h>
33
34int ion_open()
35{
Colin Cross92d7ca62013-11-08 19:04:18 -080036 int fd = open("/dev/ion", O_RDWR);
37 if (fd < 0)
38 ALOGE("open /dev/ion failed!\n");
39 return fd;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080040}
41
42int ion_close(int fd)
43{
Colin Crossaab47b22013-12-18 15:17:21 -080044 int ret = close(fd);
45 if (ret < 0)
46 return -errno;
47 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080048}
49
50static int ion_ioctl(int fd, int req, void *arg)
51{
Colin Cross92d7ca62013-11-08 19:04:18 -080052 int ret = ioctl(fd, req, arg);
53 if (ret < 0) {
54 ALOGE("ioctl %x failed with code %d: %s\n", req,
55 ret, strerror(errno));
56 return -errno;
57 }
58 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080059}
60
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -070061int ion_alloc(int fd, size_t len, size_t align, unsigned int heap_mask,
Colin Cross92d7ca62013-11-08 19:04:18 -080062 unsigned int flags, ion_user_handle_t *handle)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080063{
Colin Cross92d7ca62013-11-08 19:04:18 -080064 int ret;
65 struct ion_allocation_data data = {
66 .len = len,
67 .align = align,
68 .heap_id_mask = heap_mask,
69 .flags = flags,
70 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080071
Colin Cross74963182013-11-08 19:07:38 -080072 if (handle == NULL)
73 return -EINVAL;
74
Colin Cross92d7ca62013-11-08 19:04:18 -080075 ret = ion_ioctl(fd, ION_IOC_ALLOC, &data);
76 if (ret < 0)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080077 return ret;
Colin Cross92d7ca62013-11-08 19:04:18 -080078 *handle = data.handle;
79 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080080}
81
Rom Lemarchand969eac82013-10-21 15:19:56 -070082int ion_free(int fd, ion_user_handle_t handle)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080083{
Colin Cross92d7ca62013-11-08 19:04:18 -080084 struct ion_handle_data data = {
85 .handle = handle,
86 };
87 return ion_ioctl(fd, ION_IOC_FREE, &data);
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080088}
89
Rom Lemarchand969eac82013-10-21 15:19:56 -070090int ion_map(int fd, ion_user_handle_t handle, size_t length, int prot,
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080091 int flags, off_t offset, unsigned char **ptr, int *map_fd)
92{
Colin Cross74963182013-11-08 19:07:38 -080093 int ret;
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;
Colin Cross92d7ca62013-11-08 19:04:18 -0800106 *map_fd = data.fd;
107 if (*map_fd < 0) {
108 ALOGE("map ioctl returned negative fd\n");
109 return -EINVAL;
110 }
111 *ptr = mmap(NULL, length, prot, flags, *map_fd, offset);
112 if (*ptr == MAP_FAILED) {
113 ALOGE("mmap failed: %s\n", strerror(errno));
114 return -errno;
115 }
116 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800117}
118
Rom Lemarchand969eac82013-10-21 15:19:56 -0700119int ion_share(int fd, ion_user_handle_t handle, int *share_fd)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800120{
Colin Cross74963182013-11-08 19:07:38 -0800121 int ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800122 struct ion_fd_data data = {
123 .handle = handle,
124 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800125
Colin Cross74963182013-11-08 19:07:38 -0800126 if (share_fd == NULL)
127 return -EINVAL;
128
129 ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
Colin Cross92d7ca62013-11-08 19:04:18 -0800130 if (ret < 0)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800131 return ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800132 *share_fd = data.fd;
133 if (*share_fd < 0) {
134 ALOGE("share ioctl returned negative fd\n");
135 return -EINVAL;
136 }
137 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800138}
139
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700140int ion_alloc_fd(int fd, size_t len, size_t align, unsigned int heap_mask,
Colin Cross92d7ca62013-11-08 19:04:18 -0800141 unsigned int flags, int *handle_fd) {
142 ion_user_handle_t handle;
143 int ret;
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700144
Colin Cross92d7ca62013-11-08 19:04:18 -0800145 ret = ion_alloc(fd, len, align, heap_mask, flags, &handle);
146 if (ret < 0)
147 return ret;
148 ret = ion_share(fd, handle, handle_fd);
149 ion_free(fd, handle);
150 return ret;
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700151}
152
Rom Lemarchand969eac82013-10-21 15:19:56 -0700153int ion_import(int fd, int share_fd, ion_user_handle_t *handle)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800154{
Colin Cross74963182013-11-08 19:07:38 -0800155 int ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800156 struct ion_fd_data data = {
157 .fd = share_fd,
158 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800159
Colin Cross74963182013-11-08 19:07:38 -0800160 if (handle == NULL)
161 return -EINVAL;
162
163 ret = ion_ioctl(fd, ION_IOC_IMPORT, &data);
Colin Cross92d7ca62013-11-08 19:04:18 -0800164 if (ret < 0)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800165 return ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800166 *handle = data.handle;
167 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800168}
Rebecca Schultz Zavin3cfcc302012-09-20 09:46:19 -0700169
170int ion_sync_fd(int fd, int handle_fd)
171{
172 struct ion_fd_data data = {
173 .fd = handle_fd,
174 };
175 return ion_ioctl(fd, ION_IOC_SYNC, &data);
176}