blob: ea40245f753c1f7d0628f536b31fff0222dd0bf0 [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 Cross92d7ca62013-11-08 19:04:18 -080043 return close(fd);
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080044}
45
46static int ion_ioctl(int fd, int req, void *arg)
47{
Colin Cross92d7ca62013-11-08 19:04:18 -080048 int ret = ioctl(fd, req, arg);
49 if (ret < 0) {
50 ALOGE("ioctl %x failed with code %d: %s\n", req,
51 ret, strerror(errno));
52 return -errno;
53 }
54 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080055}
56
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -070057int ion_alloc(int fd, size_t len, size_t align, unsigned int heap_mask,
Colin Cross92d7ca62013-11-08 19:04:18 -080058 unsigned int flags, ion_user_handle_t *handle)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080059{
Colin Cross92d7ca62013-11-08 19:04:18 -080060 int ret;
61 struct ion_allocation_data data = {
62 .len = len,
63 .align = align,
64 .heap_id_mask = heap_mask,
65 .flags = flags,
66 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080067
Colin Cross92d7ca62013-11-08 19:04:18 -080068 ret = ion_ioctl(fd, ION_IOC_ALLOC, &data);
69 if (ret < 0)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080070 return ret;
Colin Cross92d7ca62013-11-08 19:04:18 -080071 *handle = data.handle;
72 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080073}
74
Rom Lemarchand969eac82013-10-21 15:19:56 -070075int ion_free(int fd, ion_user_handle_t handle)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080076{
Colin Cross92d7ca62013-11-08 19:04:18 -080077 struct ion_handle_data data = {
78 .handle = handle,
79 };
80 return ion_ioctl(fd, ION_IOC_FREE, &data);
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080081}
82
Rom Lemarchand969eac82013-10-21 15:19:56 -070083int ion_map(int fd, ion_user_handle_t handle, size_t length, int prot,
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080084 int flags, off_t offset, unsigned char **ptr, int *map_fd)
85{
Colin Cross92d7ca62013-11-08 19:04:18 -080086 struct ion_fd_data data = {
87 .handle = handle,
88 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080089
Colin Cross92d7ca62013-11-08 19:04:18 -080090 int ret = ion_ioctl(fd, ION_IOC_MAP, &data);
91 if (ret < 0)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -080092 return ret;
Colin Cross92d7ca62013-11-08 19:04:18 -080093 *map_fd = data.fd;
94 if (*map_fd < 0) {
95 ALOGE("map ioctl returned negative fd\n");
96 return -EINVAL;
97 }
98 *ptr = mmap(NULL, length, prot, flags, *map_fd, offset);
99 if (*ptr == MAP_FAILED) {
100 ALOGE("mmap failed: %s\n", strerror(errno));
101 return -errno;
102 }
103 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800104}
105
Rom Lemarchand969eac82013-10-21 15:19:56 -0700106int ion_share(int fd, ion_user_handle_t handle, int *share_fd)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800107{
Colin Cross92d7ca62013-11-08 19:04:18 -0800108 int map_fd;
109 struct ion_fd_data data = {
110 .handle = handle,
111 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800112
Colin Cross92d7ca62013-11-08 19:04:18 -0800113 int ret = ion_ioctl(fd, ION_IOC_SHARE, &data);
114 if (ret < 0)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800115 return ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800116 *share_fd = data.fd;
117 if (*share_fd < 0) {
118 ALOGE("share ioctl returned negative fd\n");
119 return -EINVAL;
120 }
121 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800122}
123
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700124int ion_alloc_fd(int fd, size_t len, size_t align, unsigned int heap_mask,
Colin Cross92d7ca62013-11-08 19:04:18 -0800125 unsigned int flags, int *handle_fd) {
126 ion_user_handle_t handle;
127 int ret;
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700128
Colin Cross92d7ca62013-11-08 19:04:18 -0800129 ret = ion_alloc(fd, len, align, heap_mask, flags, &handle);
130 if (ret < 0)
131 return ret;
132 ret = ion_share(fd, handle, handle_fd);
133 ion_free(fd, handle);
134 return ret;
Rebecca Schultz Zavina50fd552012-06-11 15:12:37 -0700135}
136
Rom Lemarchand969eac82013-10-21 15:19:56 -0700137int ion_import(int fd, int share_fd, ion_user_handle_t *handle)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800138{
Colin Cross92d7ca62013-11-08 19:04:18 -0800139 struct ion_fd_data data = {
140 .fd = share_fd,
141 };
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800142
Colin Cross92d7ca62013-11-08 19:04:18 -0800143 int ret = ion_ioctl(fd, ION_IOC_IMPORT, &data);
144 if (ret < 0)
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800145 return ret;
Colin Cross92d7ca62013-11-08 19:04:18 -0800146 *handle = data.handle;
147 return ret;
Rebecca Schultz Zavinafd91232012-02-22 16:36:55 -0800148}
Rebecca Schultz Zavin3cfcc302012-09-20 09:46:19 -0700149
150int ion_sync_fd(int fd, int handle_fd)
151{
152 struct ion_fd_data data = {
153 .fd = handle_fd,
154 };
155 return ion_ioctl(fd, ION_IOC_SYNC, &data);
156}