• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2011-2015, 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 <sys/ioctl.h>
33 #include <sys/mman.h>
34 #include <stdlib.h>
35 #include <fcntl.h>
36 #include <cutils/log.h>
37 #include <errno.h>
38 #include <utils/Trace.h>
39 
40 #include "gralloc_priv.h"
41 #include "gr_utils.h"
42 #include "gr_ion_alloc.h"
43 
44 namespace gralloc1 {
45 
Init()46 bool IonAlloc::Init() {
47   if (ion_dev_fd_ == FD_INIT) {
48     ion_dev_fd_ = open(kIonDevice, O_RDONLY);
49   }
50 
51   if (ion_dev_fd_ < 0) {
52     ALOGE("%s: Failed to open ion device - %s", __FUNCTION__, strerror(errno));
53     ion_dev_fd_ = FD_INIT;
54     return false;
55   }
56 
57   return true;
58 }
59 
CloseIonDevice()60 void IonAlloc::CloseIonDevice() {
61   if (ion_dev_fd_ > FD_INIT) {
62     close(ion_dev_fd_);
63   }
64 
65   ion_dev_fd_ = FD_INIT;
66 }
67 
AllocBuffer(AllocData * data)68 int IonAlloc::AllocBuffer(AllocData *data) {
69   ATRACE_CALL();
70   int err = 0;
71   struct ion_handle_data handle_data;
72   struct ion_fd_data fd_data;
73   struct ion_allocation_data ion_alloc_data;
74   void *base = NULL;
75 
76   ion_alloc_data.len = data->size;
77   ion_alloc_data.align = data->align;
78   ion_alloc_data.heap_id_mask = data->heap_id;
79   ion_alloc_data.flags = data->flags;
80   ion_alloc_data.flags |= data->uncached ? 0 : ION_FLAG_CACHED;
81 
82   if (ioctl(ion_dev_fd_, INT(ION_IOC_ALLOC), &ion_alloc_data)) {
83     err = -errno;
84     ALOGE("ION_IOC_ALLOC failed with error - %s", strerror(errno));
85     return err;
86   }
87 
88   fd_data.handle = ion_alloc_data.handle;
89   handle_data.handle = ion_alloc_data.handle;
90   if (ioctl(ion_dev_fd_, INT(ION_IOC_MAP), &fd_data)) {
91     err = -errno;
92     ALOGE("%s: ION_IOC_MAP failed with error - %s", __FUNCTION__, strerror(errno));
93     ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
94     return err;
95   }
96 
97   if (!(INT(data->flags) & INT(ION_SECURE))) {
98     base = mmap(0, ion_alloc_data.len, PROT_READ | PROT_WRITE, MAP_SHARED, fd_data.fd, 0);
99     if (base == MAP_FAILED) {
100       err = -errno;
101       ALOGE("%s: Failed to map the allocated memory: %s", __FUNCTION__, strerror(errno));
102       ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
103       return err;
104     }
105   }
106 
107   data->base = base;
108   data->fd = fd_data.fd;
109   ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
110   ALOGD_IF(DEBUG, "ion: Allocated buffer base:%p size:%zu fd:%d", data->base, ion_alloc_data.len,
111            data->fd);
112 
113   return 0;
114 }
115 
FreeBuffer(void * base,unsigned int size,unsigned int offset,int fd)116 int IonAlloc::FreeBuffer(void *base, unsigned int size, unsigned int offset, int fd) {
117   ATRACE_CALL();
118   int err = 0;
119   ALOGD_IF(DEBUG, "ion: Freeing buffer base:%p size:%u fd:%d", base, size, fd);
120 
121   if (base) {
122     err = UnmapBuffer(base, size, offset);
123   }
124   close(fd);
125 
126   return err;
127 }
128 
MapBuffer(void ** base,unsigned int size,unsigned int offset,int fd)129 int IonAlloc::MapBuffer(void **base, unsigned int size, unsigned int offset, int fd) {
130   ATRACE_CALL();
131   int err = 0;
132   void *addr = 0;
133 
134   // It is a (quirky) requirement of ION to have opened the
135   // ion fd in the process that is doing the mapping
136   addr = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
137   *base = addr;
138   if (addr == MAP_FAILED) {
139     err = -errno;
140     ALOGE("ion: Failed to map memory in the client: %s", strerror(errno));
141   } else {
142     ALOGD_IF(DEBUG, "ion: Mapped buffer base:%p size:%u offset:%u fd:%d", addr, size, offset, fd);
143   }
144 
145   return err;
146 }
147 
UnmapBuffer(void * base,unsigned int size,unsigned int)148 int IonAlloc::UnmapBuffer(void *base, unsigned int size, unsigned int /*offset*/) {
149   ATRACE_CALL();
150   ALOGD_IF(DEBUG, "ion: Unmapping buffer  base:%p size:%u", base, size);
151 
152   int err = 0;
153   if (munmap(base, size)) {
154     err = -errno;
155     ALOGE("ion: Failed to unmap memory at %p : %s", base, strerror(errno));
156   }
157 
158   return err;
159 }
160 
CleanBuffer(void * base,unsigned int size,unsigned int offset,int fd,int op)161 int IonAlloc::CleanBuffer(void *base, unsigned int size, unsigned int offset, int fd, int op) {
162   ATRACE_CALL();
163   ATRACE_INT("operation id", op);
164   struct ion_flush_data flush_data;
165   struct ion_fd_data fd_data;
166   struct ion_handle_data handle_data;
167   int err = 0;
168 
169   fd_data.fd = fd;
170   if (ioctl(ion_dev_fd_, INT(ION_IOC_IMPORT), &fd_data)) {
171     err = -errno;
172     ALOGE("%s: ION_IOC_IMPORT failed with error - %s", __FUNCTION__, strerror(errno));
173     return err;
174   }
175 
176   handle_data.handle = fd_data.handle;
177   flush_data.handle = fd_data.handle;
178   flush_data.vaddr = base;
179   // offset and length are unsigned int
180   flush_data.offset = offset;
181   flush_data.length = size;
182 
183   struct ion_custom_data d;
184   switch (op) {
185     case CACHE_CLEAN:
186       d.cmd = ION_IOC_CLEAN_CACHES;
187       break;
188     case CACHE_INVALIDATE:
189       d.cmd = ION_IOC_INV_CACHES;
190       break;
191     case CACHE_CLEAN_AND_INVALIDATE:
192     default:
193       d.cmd = ION_IOC_CLEAN_INV_CACHES;
194   }
195 
196   d.arg = (unsigned long)(&flush_data);  // NOLINT
197   if (ioctl(ion_dev_fd_, INT(ION_IOC_CUSTOM), &d)) {
198     err = -errno;
199     ALOGE("%s: ION_IOC_CLEAN_INV_CACHES failed with error - %s", __FUNCTION__, strerror(errno));
200     ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
201     return err;
202   }
203 
204   ioctl(ion_dev_fd_, INT(ION_IOC_FREE), &handle_data);
205 
206   return 0;
207 }
208 
209 }  // namespace gralloc1
210