From 71e0e80dbdd20d97534c57e79ed1012dfe1e6d6a Mon Sep 17 00:00:00 2001 From: Naseer Ahmed Date: Thu, 29 Dec 2011 15:17:39 +0530 Subject: [PATCH] display: Add support for protected heaps (cherry picked from commit c932690c5a9d0aedfd2580e5c5e9f79e32f54261) Change-Id: Ibddab4252ecefa1c410430b89f94aba4d3ae1c96 --- libgralloc/alloc_controller.cpp | 2 ++ libgralloc/gralloc_priv.h | 1 + libgralloc/ionalloc.cpp | 37 +++++++++++++++++---------------- libgralloc/mapper.cpp | 3 ++- libhwcomposer/hwcomposer.cpp | 4 ++++ liboverlay/overlayLib.cpp | 30 +++++++++++++++++++------- liboverlay/overlayLib.h | 4 +++- 7 files changed, 53 insertions(+), 28 deletions(-) diff --git a/libgralloc/alloc_controller.cpp b/libgralloc/alloc_controller.cpp index 4d471bd..fbe2113 100644 --- a/libgralloc/alloc_controller.cpp +++ b/libgralloc/alloc_controller.cpp @@ -159,6 +159,8 @@ int IonController::allocate(alloc_data& data, int usage, data.allocType = private_handle_t::PRIV_FLAGS_USES_ION; if(noncontig) data.allocType |= private_handle_t::PRIV_FLAGS_NONCONTIGUOUS_MEM; + if(ionFlags & ION_SECURE) + data.allocType |= private_handle_t::PRIV_FLAGS_SECURE_BUFFER; } diff --git a/libgralloc/gralloc_priv.h b/libgralloc/gralloc_priv.h index eaa3b80..e1dc1ef 100644 --- a/libgralloc/gralloc_priv.h +++ b/libgralloc/gralloc_priv.h @@ -302,6 +302,7 @@ struct private_handle_t { PRIV_FLAGS_SW_LOCK = 0x00000080, PRIV_FLAGS_NONCONTIGUOUS_MEM = 0x00000100, PRIV_FLAGS_HWC_LOCK = 0x00000200, // Set by HWC when storing the handle + PRIV_FLAGS_SECURE_BUFFER = 0x00000400, }; // file-descriptors diff --git a/libgralloc/ionalloc.cpp b/libgralloc/ionalloc.cpp index e91ee2f..d1d3ce8 100644 --- a/libgralloc/ionalloc.cpp +++ b/libgralloc/ionalloc.cpp @@ -123,25 +123,29 @@ int IonAlloc::alloc_buffer(alloc_data& data) return err; } - base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE, - MAP_SHARED, fd_data.fd, 0); - if(base == MAP_FAILED) { - LOGD("%s: Failed to map the allocated memory: %s", - __FUNCTION__, strerror(errno)); - err = -errno; - ioctl(mIonFd, ION_IOC_FREE, &handle_data); - close_device(); - ionSyncFd = FD_INIT; - return err; + if(!(data.flags & ION_SECURE)) { + + base = mmap(0, ionAllocData.len, PROT_READ|PROT_WRITE, + MAP_SHARED, fd_data.fd, 0); + if(base == MAP_FAILED) { + LOGD("%s: Failed to map the allocated memory: %s", + __FUNCTION__, strerror(errno)); + err = -errno; + ioctl(mIonFd, ION_IOC_FREE, &handle_data); + close_device(); + ionSyncFd = FD_INIT; + return err; + } + memset(base, 0, ionAllocData.len); + // Clean cache after memset + clean_buffer(base, data.size, data.offset, fd_data.fd); } + //Close the uncached FD since we no longer need it; if(ionSyncFd >= 0) close(ionSyncFd); ionSyncFd = FD_INIT; - memset(base, 0, ionAllocData.len); - // Clean cache after memset - clean_buffer(base, data.size, data.offset, fd_data.fd); data.base = base; data.fd = fd_data.fd; ioctl(mIonFd, ION_IOC_FREE, &handle_data); @@ -161,11 +165,8 @@ int IonAlloc::free_buffer(void* base, size_t size, int offset, int fd) if (err) return err; - if(!base) { - LOGE("Invalid free"); - return -EINVAL; - } - err = unmap_buffer(base, size, offset); + if(base) + err = unmap_buffer(base, size, offset); close(fd); return err; } diff --git a/libgralloc/mapper.cpp b/libgralloc/mapper.cpp index 0120ecd..5ff703e 100755 --- a/libgralloc/mapper.cpp +++ b/libgralloc/mapper.cpp @@ -63,7 +63,8 @@ static int gralloc_map(gralloc_module_t const* module, { private_handle_t* hnd = (private_handle_t*)handle; void *mappedAddress; - if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER)) { + if (!(hnd->flags & private_handle_t::PRIV_FLAGS_FRAMEBUFFER) && + !(hnd->flags & private_handle_t::PRIV_FLAGS_SECURE_BUFFER)) { size_t size = hnd->size; sp memalloc = getAllocator(hnd->flags) ; int err = memalloc->map_buffer(&mappedAddress, size, diff --git a/libhwcomposer/hwcomposer.cpp b/libhwcomposer/hwcomposer.cpp index a20283f..2d826d0 100755 --- a/libhwcomposer/hwcomposer.cpp +++ b/libhwcomposer/hwcomposer.cpp @@ -279,6 +279,8 @@ static int prepareOverlay(hwc_context_t *ctx, hwc_layer_t *layer, const bool wai info.height = hnd->height; info.format = hnd->format; info.size = hnd->size; + info.secure = (hnd->flags & + private_handle_t::PRIV_FLAGS_SECURE_BUFFER)? true:false; ret = ovLibObject->setSource(info, layer->transform, (ovLibObject->getHDMIStatus()?true:false), waitForVsync); @@ -461,6 +463,8 @@ static int prepareBypass(hwc_context_t *ctx, hwc_layer_t *layer, int index, info.height = sourceCrop.bottom - sourceCrop.top; info.format = hnd->format; info.size = hnd->size; + info.secure = (hnd->flags & + private_handle_t::PRIV_FLAGS_SECURE_BUFFER)? true:false; const bool useVGPipe = true; //only last layer should wait for vsync const bool waitForVsync = (index == lastLayerIndex); diff --git a/liboverlay/overlayLib.cpp b/liboverlay/overlayLib.cpp index c9aecff..addebc2 100755 --- a/liboverlay/overlayLib.cpp +++ b/liboverlay/overlayLib.cpp @@ -308,6 +308,7 @@ Overlay::Overlay() : mChannelUP(false), mHDMIConnected(false), mCroppedSrcHeight(0), mState(-1) { mOVBufferInfo.width = mOVBufferInfo.height = 0; mOVBufferInfo.format = mOVBufferInfo.size = 0; + mOVBufferInfo.secure = false; } Overlay::~Overlay() { @@ -345,7 +346,7 @@ bool Overlay::startChannel(const overlay_buffer_info& info, int fbnum, } objOvCtrlChannel[channel].setSize(info.size); return objOvDataChannel[channel].startDataChannel(objOvCtrlChannel[channel], fbnum, - norot, uichannel, num_buffers); + norot, info.secure, uichannel, num_buffers); } bool Overlay::closeChannel() { @@ -373,6 +374,7 @@ bool Overlay::closeChannel() { mOVBufferInfo.height = 0; mOVBufferInfo.format = 0; mOVBufferInfo.size = 0; + mOVBufferInfo.secure = false; mState = -1; return true; } @@ -993,6 +995,11 @@ bool OverlayControlChannel::setOverlayInformation(const overlay_buffer_info& inf else mOVInfo.flags &= ~MDP_OV_PLAY_NOWAIT; + if(info.secure) + mOVInfo.flags |= MDP_SECURE_OVERLAY_SESSION; + else + mOVInfo.flags &= MDP_SECURE_OVERLAY_SESSION; + return true; } @@ -1434,9 +1441,11 @@ OverlayDataChannel::~OverlayDataChannel() { bool OverlayDataChannel::startDataChannel( const OverlayControlChannel& objOvCtrlChannel, - int fbnum, bool norot, bool uichannel, int num_buffers) { + int fbnum, bool norot, bool secure, bool uichannel, + int num_buffers) { int ovid, rotid, size; mNoRot = norot; + mSecure = secure; memset(&mOvData, 0, sizeof(mOvData)); memset(&mOvDataRot, 0, sizeof(mOvDataRot)); memset(&mRotData, 0, sizeof(mRotData)); @@ -1489,12 +1498,17 @@ bool OverlayDataChannel::mapRotatorMemory(int num_buffers, bool uiChannel, int r data.align = getpagesize(); data.uncached = true; - int allocFlags = GRALLOC_USAGE_PRIVATE_MM_HEAP | - GRALLOC_USAGE_PRIVATE_WRITEBACK_HEAP | - GRALLOC_USAGE_PRIVATE_ADSP_HEAP | - GRALLOC_USAGE_PRIVATE_IOMMU_HEAP; - if((requestType == NEW_REQUEST) && !uiChannel) - allocFlags |= GRALLOC_USAGE_PRIVATE_SMI_HEAP; + int allocFlags = GRALLOC_USAGE_PRIVATE_MM_HEAP | + GRALLOC_USAGE_PRIVATE_WRITEBACK_HEAP; + + if(mSecure) { + allocFlags |= GRALLOC_USAGE_PROTECTED; + } else { + allocFlags |= GRALLOC_USAGE_PRIVATE_ADSP_HEAP | + GRALLOC_USAGE_PRIVATE_IOMMU_HEAP; + if((requestType == NEW_REQUEST) && !uiChannel) + allocFlags |= GRALLOC_USAGE_PRIVATE_SMI_HEAP; + } int err = mAlloc->allocate(data, allocFlags, 0); if(err) { diff --git a/liboverlay/overlayLib.h b/liboverlay/overlayLib.h index 1448f83..580ad55 100755 --- a/liboverlay/overlayLib.h +++ b/liboverlay/overlayLib.h @@ -108,6 +108,7 @@ struct overlay_buffer_info { int height; int format; int size; + bool secure; }; /* values for copybit_set_parameter(OVERLAY_TRANSFORM) */ @@ -219,6 +220,7 @@ public: class OverlayDataChannel { bool mNoRot; + bool mSecure; int mFD; int mRotFD; int mPmemFD; @@ -243,7 +245,7 @@ public: OverlayDataChannel(); ~OverlayDataChannel(); bool startDataChannel(const OverlayControlChannel& objOvCtrlChannel, - int fbnum, bool norot = false, + int fbnum, bool norot = false, bool secure = false, bool uichannel = false, int num_buffers = 2); bool startDataChannel(int ovid, int rotid, int size, int fbnum, bool norot = false, bool uichannel = false,