• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <string.h>
33 #include <sys/ioctl.h>
34 #include <sys/mman.h>
35 #include <stdlib.h>
36 #include <fcntl.h>
37 #include <cutils/log.h>
38 #include <errno.h>
39 #include <string.h>
40 #include <utils/Trace.h>
41 #include "gralloc_priv.h"
42 #include "ionalloc.h"
43 
44 using gralloc::IonAlloc;
45 
46 #define ION_DEVICE "/dev/ion"
47 
open_device()48 int IonAlloc::open_device()
49 {
50     if(mIonFd == FD_INIT)
51         mIonFd = open(ION_DEVICE, O_RDONLY);
52 
53     if(mIonFd < 0 ) {
54         ALOGE("%s: Failed to open ion device - %s",
55               __FUNCTION__, strerror(errno));
56         mIonFd = FD_INIT;
57         return -errno;
58     }
59     return 0;
60 }
61 
close_device()62 void IonAlloc::close_device()
63 {
64     if(mIonFd >= 0)
65         close(mIonFd);
66     mIonFd = FD_INIT;
67 }
68 
alloc_buffer(alloc_data & data)69 int IonAlloc::alloc_buffer(alloc_data& data)
70 {
71     ATRACE_CALL();
72     Locker::Autolock _l(mLock);
73     int err = 0;
74     struct ion_handle_data handle_data;
75     struct ion_fd_data fd_data;
76     struct ion_allocation_data ionAllocData;
77     void *base = 0;
78 
79     ionAllocData.len = data.size;
80     ionAllocData.align = data.align;
81     ionAllocData.heap_id_mask = data.flags & ~ION_SECURE;
82 #ifdef ION_FLAG_ALLOW_NON_CONTIG
83     ionAllocData.heap_id_mask &= (data.flags & ~ION_FLAG_ALLOW_NON_CONTIG);
84 #endif
85     ionAllocData.flags = data.uncached ? 0 : ION_FLAG_CACHED;
86     // ToDo: replace usage of alloc data structure with
87     //  ionallocdata structure.
88     if (data.flags & ION_SECURE)
89         ionAllocData.flags |= ION_SECURE;
90 #ifdef ION_FLAG_ALLOW_NON_CONTIG
91     if (data.flags & ION_FLAG_ALLOW_NON_CONTIG)
92         ionAllocData.flags |= ION_FLAG_ALLOW_NON_CONTIG;
93 #endif
94     err = open_device();
95     if (err)
96         return err;
97     if(ioctl(mIonFd, ION_IOC_ALLOC, &ionAllocData)) {
98         err = -errno;
99         ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno));
100         return err;
101     }
102 
103     fd_data.handle = ionAllocData.handle;
104     handle_data.handle = ionAllocData.handle;
105     if(ioctl(mIonFd, ION_IOC_MAP, &fd_data)) {
106         err = -errno;
107         ALOGE("%s: ION_IOC_MAP failed with error - %s",
108               __FUNCTION__, strerror(errno));
109         ioctl(mIonFd, ION_IOC_FREE, &handle_data);
110         return err;
111     }
112 
113     if(!(data.flags & ION_SECURE)) {
114         base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE,
115                     MAP_SHARED, fd_data.fd, 0);
116         if(base == MAP_FAILED) {
117             err = -errno;
118             ALOGE("%s: Failed to map the allocated memory: %s",
119                   __FUNCTION__, strerror(errno));
120             ioctl(mIonFd, ION_IOC_FREE, &handle_data);
121             return err;
122         }
123     }
124 
125     data.base = base;
126     data.fd = fd_data.fd;
127     ioctl(mIonFd, ION_IOC_FREE, &handle_data);
128     ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%zu fd:%d",
129           data.base, ionAllocData.len, data.fd);
130     return 0;
131 }
132 
133 
free_buffer(void * base,unsigned int size,unsigned int offset,int fd)134 int IonAlloc::free_buffer(void* base, unsigned int size, unsigned int offset,
135         int fd)
136 {
137     ATRACE_CALL();
138     Locker::Autolock _l(mLock);
139     ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d",
140           base, size, fd);
141     int err = 0;
142     err = open_device();
143     if (err)
144         return err;
145 
146     if(base)
147         err = unmap_buffer(base, size, offset);
148     close(fd);
149     return err;
150 }
151 
map_buffer(void ** pBase,unsigned int size,unsigned int offset,int fd)152 int IonAlloc::map_buffer(void **pBase, unsigned int size, unsigned int offset,
153         int fd)
154 {
155     ATRACE_CALL();
156     int err = 0;
157     void *base = 0;
158     // It is a (quirky) requirement of ION to have opened the
159     // ion fd in the process that is doing the mapping
160     err = open_device();
161     if (err)
162         return err;
163 
164     base = mmap(0, size, PROT_READ| PROT_WRITE,
165                 MAP_SHARED, fd, 0);
166     *pBase = base;
167     if(base == MAP_FAILED) {
168         err = -errno;
169         ALOGE("ion: Failed to map memory in the client: %s",
170               strerror(errno));
171     } else {
172         ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%u offset:%u fd:%d",
173               base, size, offset, fd);
174     }
175     return err;
176 }
177 
unmap_buffer(void * base,unsigned int size,unsigned int)178 int IonAlloc::unmap_buffer(void *base, unsigned int size,
179         unsigned int /*offset*/)
180 {
181     ATRACE_CALL();
182     ALOGD_IF(DEBUG, "ion: Unmapping buffer  base:%p size:%u", base, size);
183     int err = 0;
184     if(munmap(base, size)) {
185         err = -errno;
186         ALOGE("ion: Failed to unmap memory at %p : %s",
187               base, strerror(errno));
188     }
189     return err;
190 
191 }
clean_buffer(void * base,unsigned int size,unsigned int offset,int fd,int op)192 int IonAlloc::clean_buffer(void *base, unsigned int size, unsigned int offset,
193         int fd, int op)
194 {
195     ATRACE_CALL();
196     ATRACE_INT("operation id", op);
197     struct ion_flush_data flush_data;
198     struct ion_fd_data fd_data;
199     struct ion_handle_data handle_data;
200     int err = 0;
201 
202     err = open_device();
203     if (err)
204         return err;
205 
206     fd_data.fd = fd;
207     if (ioctl(mIonFd, ION_IOC_IMPORT, &fd_data)) {
208         err = -errno;
209         ALOGE("%s: ION_IOC_IMPORT failed with error - %s",
210               __FUNCTION__, strerror(errno));
211         return err;
212     }
213 
214     handle_data.handle = fd_data.handle;
215     flush_data.handle  = fd_data.handle;
216     flush_data.vaddr   = base;
217     // offset and length are unsigned int
218     flush_data.offset  = offset;
219     flush_data.length  = size;
220 
221     struct ion_custom_data d;
222     switch(op) {
223     case CACHE_CLEAN:
224         d.cmd = ION_IOC_CLEAN_CACHES;
225         break;
226     case CACHE_INVALIDATE:
227             d.cmd = ION_IOC_INV_CACHES;
228         break;
229     case CACHE_CLEAN_AND_INVALIDATE:
230     default:
231         d.cmd = ION_IOC_CLEAN_INV_CACHES;
232     }
233 
234     d.arg = (unsigned long int)&flush_data;
235 
236     if(ioctl(mIonFd, ION_IOC_CUSTOM, &d)) {
237         err = -errno;
238         ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s",
239 
240               __FUNCTION__, strerror(errno));
241         ioctl(mIonFd, ION_IOC_FREE, &handle_data);
242         return err;
243     }
244     ioctl(mIonFd, ION_IOC_FREE, &handle_data);
245     return 0;
246 }
247 
248