1 /*
2  * Copyright (c) 2011-2014, The Linux Foundation. All rights reserved.
3 
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *   * Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *   * Redistributions in binary form must reproduce the above
10  *     copyright notice, this list of conditions and the following
11  *     disclaimer in the documentation and/or other materials provided
12  *     with the distribution.
13  *   * Neither the name of The Linux Foundation nor the names of its
14  *     contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #define DEBUG 0
31 #define ATRACE_TAG (ATRACE_TAG_GRAPHICS | ATRACE_TAG_HAL)
32 
33 #include <errno.h>
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <sys/ioctl.h>
37 #include <sys/mman.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40 
41 #include <cutils/trace.h>
42 #include <log/log.h>
43 #include <utils/Trace.h>
44 
45 #include "gralloc_priv.h"
46 #include "ionalloc.h"
47 
48 using gralloc::IonAlloc;
49 
50 #define ION_DEVICE "/dev/ion"
51 
open_device()52 int IonAlloc::open_device()
53 {
54     if(mIonFd == FD_INIT)
55         mIonFd = open(ION_DEVICE, O_RDONLY);
56 
57     if(mIonFd < 0 ) {
58         ALOGE("%s: Failed to open ion device - %s",
59               __FUNCTION__, strerror(errno));
60         mIonFd = FD_INIT;
61         return -errno;
62     }
63     return 0;
64 }
65 
close_device()66 void IonAlloc::close_device()
67 {
68     if(mIonFd >= 0)
69         close(mIonFd);
70     mIonFd = FD_INIT;
71 }
72 
alloc_buffer(alloc_data & data)73 int IonAlloc::alloc_buffer(alloc_data& data)
74 {
75     ATRACE_CALL();
76     Locker::Autolock _l(mLock);
77     int err = 0;
78     struct ion_handle_data handle_data;
79     struct ion_fd_data fd_data;
80     struct ion_allocation_data ionAllocData;
81     void *base = 0;
82 
83     ionAllocData.len = data.size;
84     ionAllocData.align = data.align;
85     ionAllocData.heap_id_mask = data.heapId;
86     ionAllocData.flags = data.flags;
87     ionAllocData.flags |= data.uncached ? 0 : ION_FLAG_CACHED;
88     err = open_device();
89     if (err)
90         return err;
91     if(ioctl(mIonFd, ION_IOC_ALLOC, &ionAllocData)) {
92         err = -errno;
93         ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno));
94         return err;
95     }
96 
97     fd_data.handle = ionAllocData.handle;
98     handle_data.handle = ionAllocData.handle;
99     if(ioctl(mIonFd, ION_IOC_MAP, &fd_data)) {
100         err = -errno;
101         ALOGE("%s: ION_IOC_MAP failed with error - %s",
102               __FUNCTION__, strerror(errno));
103         ioctl(mIonFd, ION_IOC_FREE, &handle_data);
104         return err;
105     }
106 
107     if(!(data.flags & ION_SECURE)) {
108         base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE,
109                     MAP_SHARED, fd_data.fd, 0);
110         if(base == MAP_FAILED) {
111             err = -errno;
112             ALOGE("%s: Failed to map the allocated memory: %s",
113                   __FUNCTION__, strerror(errno));
114             ioctl(mIonFd, ION_IOC_FREE, &handle_data);
115             return err;
116         }
117     }
118 
119     data.base = base;
120     data.fd = fd_data.fd;
121     ioctl(mIonFd, ION_IOC_FREE, &handle_data);
122     ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%zu fd:%d",
123           data.base, ionAllocData.len, data.fd);
124     return 0;
125 }
126 
127 
free_buffer(void * base,unsigned int size,unsigned int offset,int fd)128 int IonAlloc::free_buffer(void* base, unsigned int size, unsigned int offset,
129         int fd)
130 {
131     ATRACE_CALL();
132     Locker::Autolock _l(mLock);
133     ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d",
134           base, size, fd);
135     int err = 0;
136     err = open_device();
137     if (err)
138         return err;
139 
140     if(base)
141         err = unmap_buffer(base, size, offset);
142     close(fd);
143     return err;
144 }
145 
map_buffer(void ** pBase,unsigned int size,unsigned int offset,int fd)146 int IonAlloc::map_buffer(void **pBase, unsigned int size, unsigned int offset,
147         int fd)
148 {
149     ATRACE_CALL();
150     int err = 0;
151     void *base = 0;
152     // It is a (quirky) requirement of ION to have opened the
153     // ion fd in the process that is doing the mapping
154     err = open_device();
155     if (err)
156         return err;
157 
158     base = mmap(0, size, PROT_READ| PROT_WRITE,
159                 MAP_SHARED, fd, 0);
160     *pBase = base;
161     if(base == MAP_FAILED) {
162         err = -errno;
163         ALOGE("ion: Failed to map memory in the client: %s",
164               strerror(errno));
165     } else {
166         ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%u offset:%u fd:%d",
167               base, size, offset, fd);
168     }
169     return err;
170 }
171 
unmap_buffer(void * base,unsigned int size,unsigned int)172 int IonAlloc::unmap_buffer(void *base, unsigned int size,
173         unsigned int /*offset*/)
174 {
175     ATRACE_CALL();
176     ALOGD_IF(DEBUG, "ion: Unmapping buffer  base:%p size:%u", base, size);
177     int err = 0;
178     if(munmap(base, size)) {
179         err = -errno;
180         ALOGE("ion: Failed to unmap memory at %p : %s",
181               base, strerror(errno));
182     }
183     return err;
184 
185 }
clean_buffer(void * base,unsigned int size,unsigned int offset,int fd,int op)186 int IonAlloc::clean_buffer(void *base, unsigned int size, unsigned int offset,
187         int fd, int op)
188 {
189     ATRACE_CALL();
190     ATRACE_INT("operation id", op);
191     struct ion_flush_data flush_data;
192     struct ion_fd_data fd_data;
193     struct ion_handle_data handle_data;
194     int err = 0;
195 
196     err = open_device();
197     if (err)
198         return err;
199 
200     fd_data.fd = fd;
201     if (ioctl(mIonFd, ION_IOC_IMPORT, &fd_data)) {
202         err = -errno;
203         ALOGE("%s: ION_IOC_IMPORT failed with error - %s",
204               __FUNCTION__, strerror(errno));
205         return err;
206     }
207 
208     handle_data.handle = fd_data.handle;
209     flush_data.handle  = fd_data.handle;
210     flush_data.vaddr   = base;
211     // offset and length are unsigned int
212     flush_data.offset  = offset;
213     flush_data.length  = size;
214 
215     struct ion_custom_data d;
216     switch(op) {
217     case CACHE_CLEAN:
218         d.cmd = ION_IOC_CLEAN_CACHES;
219         break;
220     case CACHE_INVALIDATE:
221             d.cmd = ION_IOC_INV_CACHES;
222         break;
223     case CACHE_CLEAN_AND_INVALIDATE:
224     default:
225         d.cmd = ION_IOC_CLEAN_INV_CACHES;
226     }
227 
228     d.arg = (unsigned long int)&flush_data;
229 
230     if(ioctl(mIonFd, ION_IOC_CUSTOM, &d)) {
231         err = -errno;
232         ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s",
233 
234               __FUNCTION__, strerror(errno));
235         ioctl(mIonFd, ION_IOC_FREE, &handle_data);
236         return err;
237     }
238     ioctl(mIonFd, ION_IOC_FREE, &handle_data);
239     return 0;
240 }
241 
242