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