129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed/* 2a43fb8fce6d9be5577de1e0f49c99bd4fe2f6d44Duy Truong* Copyright (c) 2011, The Linux Foundation. All rights reserved. 329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* 429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* Redistribution and use in source and binary forms, with or without 529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* modification, are permitted provided that the following conditions are 629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* met: 729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* * Redistributions of source code must retain the above copyright 829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* notice, this list of conditions and the following disclaimer. 929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* * Redistributions in binary form must reproduce the above 1029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* copyright notice, this list of conditions and the following 1129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* disclaimer in the documentation and/or other materials provided 1229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* with the distribution. 13a43fb8fce6d9be5577de1e0f49c99bd4fe2f6d44Duy Truong* * Neither the name of The Linux Foundation nor the names of its 1429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* contributors may be used to endorse or promote products derived 1529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* from this software without specific prior written permission. 1629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* 1729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 1829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 1929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 2029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 2129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 2229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 2329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 2429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 2529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 2629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 2729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed*/ 2929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 3029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 3129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed#ifndef OVERLAY_MEM_H 3229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed#define OVERLAY_MEM_H 3329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 3429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed#include <sys/mman.h> 3529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed#include <fcntl.h> 3629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed#include <alloc_controller.h> 3729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed#include <memalloc.h> 3829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 3929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed#include "gralloc_priv.h" 4029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed#include "overlayUtils.h" 4129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 4229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmednamespace overlay { 4329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 4429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed/* 4529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* Holds base address, offset and the fd 4629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed* */ 4729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedclass OvMem { 4829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedpublic: 4929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* ctor init*/ 5029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed explicit OvMem(); 5129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 5229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* dtor DO NOT call close so it can be copied */ 5329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed ~OvMem(); 5429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 5529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* Use libgralloc to retrieve fd, base addr, alloc type */ 5629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed bool open(uint32_t numbufs, 57f48aef64b218d42bd2ede62dcb03a3d7831ebbf9Naseer Ahmed uint32_t bufSz, bool isSecure); 5829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 5929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* close fd. assign base address to invalid*/ 6029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed bool close(); 6129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 6229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* return underlying fd */ 6329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed int getFD() const; 6429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 6529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* return true if fd is valid and base address is valid */ 6629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed bool valid() const; 6729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 6829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* dump the state of the object */ 6929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed void dump() const; 7029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 7129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* return underlying address */ 7229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed void* addr() const; 7329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 7429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* return underlying offset */ 7529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed uint32_t bufSz() const; 7629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 7729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* return number of bufs */ 7829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed uint32_t numBufs() const ; 7929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 8029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedprivate: 8129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* actual os fd */ 8229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed int mFd; 8329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 8429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* points to base addr (mmap)*/ 8529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed void* mBaseAddr; 8629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 8729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* allocated buffer type determined by gralloc (ashmem, ion, etc) */ 8829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed int mAllocType; 8929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 9029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* holds buf size */ 9129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed uint32_t mBufSz; 9229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 9329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* num of bufs */ 9429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed uint32_t mNumBuffers; 9529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 9629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed /* gralloc alloc controller */ 9701d3fd3318a767e6ba75492ed08d57896df95d63Naseer Ahmed gralloc::IAllocController* mAlloc; 9829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed}; 9929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 10029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed//-------------------Inlines----------------------------------- 10129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 10229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedusing gralloc::IMemAlloc; 10329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedusing gralloc::alloc_data; 10429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 10529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline OvMem::OvMem() { 10629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mFd = -1; 10729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mBaseAddr = MAP_FAILED; 10829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mAllocType = 0; 10929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mBufSz = 0; 11029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mNumBuffers = 0; 11101d3fd3318a767e6ba75492ed08d57896df95d63Naseer Ahmed mAlloc = gralloc::IAllocController::getInstance(); 11229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed} 11329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 11429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline OvMem::~OvMem() { } 11529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 11629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline bool OvMem::open(uint32_t numbufs, 117f48aef64b218d42bd2ede62dcb03a3d7831ebbf9Naseer Ahmed uint32_t bufSz, bool isSecure) 11829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed{ 11929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed alloc_data data; 120f48aef64b218d42bd2ede62dcb03a3d7831ebbf9Naseer Ahmed int allocFlags = GRALLOC_USAGE_PRIVATE_IOMMU_HEAP; 121f48aef64b218d42bd2ede62dcb03a3d7831ebbf9Naseer Ahmed if(isSecure) { 122e94097f3908defaa9f83c0565ea218fe939329e1Sushil Chauhan allocFlags = GRALLOC_USAGE_PRIVATE_MM_HEAP; 123e94e0aa7e7259138ffe895bcd9daaa5bde9eafa5Sushil Chauhan allocFlags |= GRALLOC_USAGE_PROTECTED; 124e94097f3908defaa9f83c0565ea218fe939329e1Sushil Chauhan allocFlags |= GRALLOC_USAGE_PRIVATE_UNCACHED; 125f48aef64b218d42bd2ede62dcb03a3d7831ebbf9Naseer Ahmed } 12629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 127f48aef64b218d42bd2ede62dcb03a3d7831ebbf9Naseer Ahmed int err = 0; 12829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed OVASSERT(numbufs && bufSz, "numbufs=%d bufSz=%d", numbufs, bufSz); 12929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 13029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mBufSz = bufSz; 13129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mNumBuffers = numbufs; 13229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 13329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed data.base = 0; 13429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed data.fd = -1; 13529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed data.offset = 0; 13629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed data.size = mBufSz * mNumBuffers; 13729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed data.align = getpagesize(); 13829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed data.uncached = true; 13929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 14001d3fd3318a767e6ba75492ed08d57896df95d63Naseer Ahmed err = mAlloc->allocate(data, allocFlags); 1418dab7bc01d1e5e0f297494f95e445eb92bc211b7Naseer Ahmed if (err != 0) { 1428dab7bc01d1e5e0f297494f95e445eb92bc211b7Naseer Ahmed ALOGE("OvMem: Error allocating memory"); 143f48aef64b218d42bd2ede62dcb03a3d7831ebbf9Naseer Ahmed return false; 14429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed } 14529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 14629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mFd = data.fd; 14729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mBaseAddr = data.base; 14829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mAllocType = data.allocType; 14929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 15029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed return true; 15129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed} 15229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 15329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline bool OvMem::close() 15429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed{ 15529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed int ret = 0; 15629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 15729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed if(!valid()) { 15829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed return true; 15929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed } 16029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 16101d3fd3318a767e6ba75492ed08d57896df95d63Naseer Ahmed IMemAlloc* memalloc = mAlloc->getAllocator(mAllocType); 16229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed ret = memalloc->free_buffer(mBaseAddr, mBufSz * mNumBuffers, 0, mFd); 16329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed if (ret != 0) { 16429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed ALOGE("OvMem: error freeing buffer"); 165f48aef64b218d42bd2ede62dcb03a3d7831ebbf9Naseer Ahmed return false; 16629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed } 16729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 16829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mFd = -1; 16929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mBaseAddr = MAP_FAILED; 17029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mAllocType = 0; 17129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mBufSz = 0; 17229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed mNumBuffers = 0; 173f48aef64b218d42bd2ede62dcb03a3d7831ebbf9Naseer Ahmed return true; 17429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed} 17529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 17629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline bool OvMem::valid() const 17729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed{ 17829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed return (mFd != -1) && (mBaseAddr != MAP_FAILED); 17929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed} 18029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 18129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline int OvMem::getFD() const 18229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed{ 18329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed return mFd; 18429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed} 18529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 18629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline void* OvMem::addr() const 18729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed{ 18829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed return mBaseAddr; 18929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed} 19029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 19129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline uint32_t OvMem::bufSz() const 19229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed{ 19329a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed return mBufSz; 19429a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed} 19529a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 19629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline uint32_t OvMem::numBufs() const 19729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed{ 19829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed return mNumBuffers; 19929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed} 20029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 20129a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmedinline void OvMem::dump() const 20229a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed{ 203f48aef64b218d42bd2ede62dcb03a3d7831ebbf9Naseer Ahmed ALOGE("== Dump OvMem start =="); 204f48aef64b218d42bd2ede62dcb03a3d7831ebbf9Naseer Ahmed ALOGE("fd=%d addr=%p type=%d bufsz=%u", mFd, mBaseAddr, mAllocType, mBufSz); 205f48aef64b218d42bd2ede62dcb03a3d7831ebbf9Naseer Ahmed ALOGE("== Dump OvMem end =="); 20629a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed} 20729a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 20829a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed} // overlay 20929a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed 21029a26818d7294055539167b2fbfdaa168bcf725cNaseer Ahmed#endif // OVERLAY_MEM_H 211