2010-04-22 00:33:32 +00:00
|
|
|
/*
|
2011-11-19 17:55:49 +00:00
|
|
|
* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
|
|
|
|
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above
|
|
|
|
* copyright notice, this list of conditions and the following
|
|
|
|
* disclaimer in the documentation and/or other materials provided
|
|
|
|
* with the distribution.
|
|
|
|
* * Neither the name of Code Aurora Forum, Inc. nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
2010-04-22 00:33:32 +00:00
|
|
|
*
|
2011-11-19 17:55:49 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
|
|
|
|
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
|
|
|
|
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
|
|
|
|
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
|
|
|
|
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2010-04-22 00:33:32 +00:00
|
|
|
*/
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/mman.h>
|
2011-11-19 17:55:49 +00:00
|
|
|
#include <stdlib.h>
|
2010-04-22 00:33:32 +00:00
|
|
|
#include <cutils/log.h>
|
2011-11-19 17:55:49 +00:00
|
|
|
#include <errno.h>
|
|
|
|
#include <linux/android_pmem.h>
|
2010-04-22 00:33:32 +00:00
|
|
|
#include "gralloc_priv.h"
|
|
|
|
#include "pmemalloc.h"
|
2011-11-19 17:55:49 +00:00
|
|
|
#include "pmem_bestfit_alloc.h"
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2012-03-31 02:11:00 +00:00
|
|
|
#define DEBUG_PMEM (0)
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
using namespace gralloc;
|
|
|
|
using android::sp;
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
// Common functions between userspace
|
|
|
|
// and kernel allocators
|
|
|
|
static int getPmemTotalSize(int fd, size_t* size)
|
|
|
|
{
|
|
|
|
//XXX: 7x27
|
|
|
|
int err = 0;
|
|
|
|
pmem_region region;
|
2012-01-26 19:33:18 +00:00
|
|
|
if (ioctl(fd, PMEM_GET_TOTAL_SIZE, ®ion)) {
|
|
|
|
err = -errno;
|
2012-01-31 09:04:43 +00:00
|
|
|
} else {
|
2011-11-19 17:55:49 +00:00
|
|
|
*size = region.len;
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
2011-11-19 17:55:49 +00:00
|
|
|
return err;
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
static int getOpenFlags(bool uncached)
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
2011-11-19 17:55:49 +00:00
|
|
|
if(uncached)
|
|
|
|
return O_RDWR | O_SYNC;
|
|
|
|
else
|
|
|
|
return O_RDWR;
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
static int connectPmem(int fd, int master_fd) {
|
2012-01-26 19:33:18 +00:00
|
|
|
if (ioctl(fd, PMEM_CONNECT, master_fd))
|
|
|
|
return -errno;
|
|
|
|
return 0;
|
2011-11-19 17:55:49 +00:00
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
static int mapSubRegion(int fd, int offset, size_t size) {
|
|
|
|
struct pmem_region sub = { offset, size };
|
2012-01-26 19:33:18 +00:00
|
|
|
if (ioctl(fd, PMEM_MAP, &sub))
|
|
|
|
return -errno;
|
|
|
|
return 0;
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
static int unmapSubRegion(int fd, int offset, size_t size) {
|
|
|
|
struct pmem_region sub = { offset, size };
|
2012-01-26 19:33:18 +00:00
|
|
|
if (ioctl(fd, PMEM_UNMAP, &sub))
|
|
|
|
return -errno;
|
|
|
|
return 0;
|
2011-11-19 17:55:49 +00:00
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
static int alignPmem(int fd, size_t size, int align) {
|
|
|
|
struct pmem_allocation allocation;
|
|
|
|
allocation.size = size;
|
|
|
|
allocation.align = align;
|
2012-01-26 19:33:18 +00:00
|
|
|
if (ioctl(fd, PMEM_ALLOCATE_ALIGNED, &allocation))
|
|
|
|
return -errno;
|
|
|
|
return 0;
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
static int cleanPmem(void *base, size_t size, int offset, int fd) {
|
|
|
|
struct pmem_addr pmem_addr;
|
|
|
|
pmem_addr.vaddr = (unsigned long) base;
|
|
|
|
pmem_addr.offset = offset;
|
|
|
|
pmem_addr.length = size;
|
2012-01-26 19:33:18 +00:00
|
|
|
if (ioctl(fd, PMEM_CLEAN_INV_CACHES, &pmem_addr))
|
|
|
|
return -errno;
|
|
|
|
return 0;
|
2011-11-19 17:55:49 +00:00
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
//-------------- PmemUserspaceAlloc-----------------------//
|
|
|
|
PmemUserspaceAlloc::PmemUserspaceAlloc()
|
|
|
|
{
|
|
|
|
mPmemDev = DEVICE_PMEM;
|
|
|
|
mMasterFd = FD_INIT;
|
|
|
|
mAllocator = new SimpleBestFitAllocator();
|
|
|
|
pthread_mutex_init(&mLock, NULL);
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
PmemUserspaceAlloc::~PmemUserspaceAlloc()
|
|
|
|
{
|
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int PmemUserspaceAlloc::init_pmem_area_locked()
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
2011-11-19 17:55:49 +00:00
|
|
|
LOGD("%s: Opening master pmem FD", __FUNCTION__);
|
2010-04-22 00:33:32 +00:00
|
|
|
int err = 0;
|
2011-11-19 17:55:49 +00:00
|
|
|
int fd = open(mPmemDev, O_RDWR, 0);
|
2010-04-22 00:33:32 +00:00
|
|
|
if (fd >= 0) {
|
|
|
|
size_t size = 0;
|
2011-11-19 17:55:49 +00:00
|
|
|
err = getPmemTotalSize(fd, &size);
|
|
|
|
LOGD("%s: Total pmem size: %d", __FUNCTION__, size);
|
2010-04-22 00:33:32 +00:00
|
|
|
if (err < 0) {
|
2011-11-19 17:55:49 +00:00
|
|
|
LOGE("%s: PMEM_GET_TOTAL_SIZE failed (%d), limp mode", mPmemDev,
|
2010-04-22 00:33:32 +00:00
|
|
|
err);
|
|
|
|
size = 8<<20; // 8 MiB
|
|
|
|
}
|
2011-11-19 17:55:49 +00:00
|
|
|
mAllocator->setSize(size);
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
void* base = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd,
|
2010-04-22 00:33:32 +00:00
|
|
|
0);
|
|
|
|
if (base == MAP_FAILED) {
|
2012-01-26 19:33:18 +00:00
|
|
|
err = -errno;
|
2011-11-19 17:55:49 +00:00
|
|
|
LOGE("%s: Failed to map pmem master fd: %s", mPmemDev,
|
|
|
|
strerror(errno));
|
2010-04-22 00:33:32 +00:00
|
|
|
base = 0;
|
2011-11-19 17:55:49 +00:00
|
|
|
close(fd);
|
2010-04-22 00:33:32 +00:00
|
|
|
fd = -1;
|
|
|
|
} else {
|
2011-11-19 17:55:49 +00:00
|
|
|
mMasterFd = fd;
|
|
|
|
mMasterBase = base;
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-26 19:33:18 +00:00
|
|
|
err = -errno;
|
2011-11-19 17:55:49 +00:00
|
|
|
LOGE("%s: Failed to open pmem device: %s", mPmemDev,
|
|
|
|
strerror(errno));
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int PmemUserspaceAlloc::init_pmem_area()
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
2011-11-19 17:55:49 +00:00
|
|
|
pthread_mutex_lock(&mLock);
|
|
|
|
int err = mMasterFd;
|
|
|
|
if (err == FD_INIT) {
|
2010-04-22 00:33:32 +00:00
|
|
|
// first time, try to initialize pmem
|
2011-11-19 17:55:49 +00:00
|
|
|
LOGD("%s: Initializing pmem area", __FUNCTION__);
|
2010-04-22 00:33:32 +00:00
|
|
|
err = init_pmem_area_locked();
|
|
|
|
if (err) {
|
2011-11-19 17:55:49 +00:00
|
|
|
LOGE("%s: failed to initialize pmem area", mPmemDev);
|
|
|
|
mMasterFd = err;
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
} else if (err < 0) {
|
|
|
|
// pmem couldn't be initialized, never use it
|
|
|
|
} else {
|
|
|
|
// pmem OK
|
|
|
|
err = 0;
|
|
|
|
}
|
2011-11-19 17:55:49 +00:00
|
|
|
pthread_mutex_unlock(&mLock);
|
2010-04-22 00:33:32 +00:00
|
|
|
return err;
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int PmemUserspaceAlloc::alloc_buffer(alloc_data& data)
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
|
|
|
int err = init_pmem_area();
|
|
|
|
if (err == 0) {
|
2011-11-19 17:55:49 +00:00
|
|
|
void* base = mMasterBase;
|
|
|
|
size_t size = data.size;
|
|
|
|
int offset = mAllocator->allocate(size);
|
2010-04-22 00:33:32 +00:00
|
|
|
if (offset < 0) {
|
|
|
|
// no more pmem memory
|
2011-11-19 17:55:49 +00:00
|
|
|
LOGE("%s: No more pmem available", mPmemDev);
|
2010-04-22 00:33:32 +00:00
|
|
|
err = -ENOMEM;
|
|
|
|
} else {
|
2011-11-19 17:55:49 +00:00
|
|
|
int openFlags = getOpenFlags(data.uncached);
|
2010-04-22 00:33:32 +00:00
|
|
|
|
|
|
|
// now create the "sub-heap"
|
2011-11-19 17:55:49 +00:00
|
|
|
int fd = open(mPmemDev, openFlags, 0);
|
2010-04-22 00:33:32 +00:00
|
|
|
err = fd < 0 ? fd : 0;
|
|
|
|
|
|
|
|
// and connect to it
|
|
|
|
if (err == 0)
|
2011-11-19 17:55:49 +00:00
|
|
|
err = connectPmem(fd, mMasterFd);
|
2010-04-22 00:33:32 +00:00
|
|
|
|
|
|
|
// and make it available to the client process
|
|
|
|
if (err == 0)
|
2011-11-19 17:55:49 +00:00
|
|
|
err = mapSubRegion(fd, offset, size);
|
2010-04-22 00:33:32 +00:00
|
|
|
|
|
|
|
if (err < 0) {
|
2011-11-19 17:55:49 +00:00
|
|
|
LOGE("%s: Failed to initialize pmem sub-heap: %d", mPmemDev,
|
2010-04-22 00:33:32 +00:00
|
|
|
err);
|
2011-11-19 17:55:49 +00:00
|
|
|
close(fd);
|
|
|
|
mAllocator->deallocate(offset);
|
2010-04-22 00:33:32 +00:00
|
|
|
fd = -1;
|
|
|
|
} else {
|
2012-03-31 02:11:00 +00:00
|
|
|
LOGD_IF(DEBUG_PMEM,"%s: Allocated buffer base:%p size:%d offset:%d fd:%d",
|
2011-11-19 17:55:49 +00:00
|
|
|
mPmemDev, base, size, offset, fd);
|
2010-04-22 00:33:32 +00:00
|
|
|
memset((char*)base + offset, 0, size);
|
2011-07-29 04:12:07 +00:00
|
|
|
//Clean cache before flushing to ensure pmem is properly flushed
|
2011-11-19 17:55:49 +00:00
|
|
|
err = clean_buffer((void*)((intptr_t) base + offset), size, offset, fd);
|
2011-09-02 01:08:34 +00:00
|
|
|
if (err < 0) {
|
2011-11-19 17:55:49 +00:00
|
|
|
LOGE("cleanPmem failed: (%s)", strerror(errno));
|
2011-09-02 01:08:34 +00:00
|
|
|
}
|
2011-11-19 17:55:49 +00:00
|
|
|
cacheflush(intptr_t(base) + offset, intptr_t(base) + offset + size, 0);
|
|
|
|
data.base = base;
|
|
|
|
data.offset = offset;
|
|
|
|
data.fd = fd;
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int PmemUserspaceAlloc::free_buffer(void* base, size_t size, int offset, int fd)
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
2012-03-31 02:11:00 +00:00
|
|
|
LOGD_IF(DEBUG_PMEM,"%s: Freeing buffer base:%p size:%d offset:%d fd:%d",
|
2011-11-19 17:55:49 +00:00
|
|
|
mPmemDev, base, size, offset, fd);
|
2010-04-22 00:33:32 +00:00
|
|
|
int err = 0;
|
|
|
|
if (fd >= 0) {
|
2011-11-19 17:55:49 +00:00
|
|
|
int err = unmapSubRegion(fd, offset, size);
|
2010-04-22 00:33:32 +00:00
|
|
|
LOGE_IF(err<0, "PMEM_UNMAP failed (%s), fd=%d, sub.offset=%u, "
|
2011-11-19 17:55:49 +00:00
|
|
|
"sub.size=%u", strerror(errno), fd, offset, size);
|
2010-04-22 00:33:32 +00:00
|
|
|
if (err == 0) {
|
|
|
|
// we can't deallocate the memory in case of UNMAP failure
|
|
|
|
// because it would give that process access to someone else's
|
|
|
|
// surfaces, which would be a security breach.
|
2011-11-19 17:55:49 +00:00
|
|
|
mAllocator->deallocate(offset);
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
2011-11-19 17:55:49 +00:00
|
|
|
close(fd);
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int PmemUserspaceAlloc::map_buffer(void **pBase, size_t size, int offset, int fd)
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
2011-11-19 17:55:49 +00:00
|
|
|
int err = 0;
|
|
|
|
size += offset;
|
|
|
|
void *base = mmap(0, size, PROT_READ| PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0);
|
|
|
|
*pBase = base;
|
|
|
|
if(base == MAP_FAILED) {
|
2012-01-26 19:33:18 +00:00
|
|
|
err = -errno;
|
2011-12-30 13:43:28 +00:00
|
|
|
LOGE("%s: Failed to map buffer size:%d offset:%d fd:%d Error: %s",
|
|
|
|
mPmemDev, size, offset, fd, strerror(errno));
|
2011-11-19 17:55:49 +00:00
|
|
|
} else {
|
2012-03-31 02:11:00 +00:00
|
|
|
LOGD_IF(DEBUG_PMEM,"%s: Mapped buffer base:%p size:%d offset:%d fd:%d",
|
2011-11-19 17:55:49 +00:00
|
|
|
mPmemDev, base, size, offset, fd);
|
|
|
|
}
|
|
|
|
return err;
|
2010-04-22 00:33:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int PmemUserspaceAlloc::unmap_buffer(void *base, size_t size, int offset)
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
2011-11-19 17:55:49 +00:00
|
|
|
int err = 0;
|
|
|
|
//pmem hack
|
|
|
|
base = (void*)(intptr_t(base) - offset);
|
|
|
|
size += offset;
|
2012-03-31 02:11:00 +00:00
|
|
|
LOGD_IF(DEBUG_PMEM,"%s: Unmapping buffer base:%p size:%d offset:%d",
|
2011-11-19 17:55:49 +00:00
|
|
|
mPmemDev , base, size, offset);
|
|
|
|
if (munmap(base, size) < 0) {
|
2012-01-26 19:33:18 +00:00
|
|
|
err = -errno;
|
2011-12-30 13:43:28 +00:00
|
|
|
LOGE("%s: Failed to unmap memory at %p :%s",
|
|
|
|
mPmemDev, base, strerror(errno));
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
return err;
|
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int PmemUserspaceAlloc::clean_buffer(void *base, size_t size, int offset, int fd)
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
2011-11-19 17:55:49 +00:00
|
|
|
return cleanPmem(base, size, offset, fd);
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
//-------------- PmemKernelAlloc-----------------------//
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
PmemKernelAlloc::PmemKernelAlloc(const char* pmemdev) :
|
|
|
|
mPmemDev(pmemdev)
|
|
|
|
{
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
PmemKernelAlloc::~PmemKernelAlloc()
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
2011-11-19 17:55:49 +00:00
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int PmemKernelAlloc::alloc_buffer(alloc_data& data)
|
|
|
|
{
|
2011-03-08 01:35:59 +00:00
|
|
|
int err, offset = 0;
|
2011-11-19 17:55:49 +00:00
|
|
|
int openFlags = getOpenFlags(data.uncached);
|
|
|
|
int size = data.size;
|
2011-03-08 01:35:59 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int fd = open(mPmemDev, openFlags, 0);
|
2011-05-26 19:37:07 +00:00
|
|
|
if (fd < 0) {
|
2011-11-19 17:55:49 +00:00
|
|
|
err = -errno;
|
|
|
|
LOGE("%s: Error opening %s", __FUNCTION__, mPmemDev);
|
2010-04-22 00:33:32 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
if (data.align == 8192) {
|
2011-03-08 02:00:54 +00:00
|
|
|
// Tile format buffers need physical alignment to 8K
|
2011-11-19 17:55:49 +00:00
|
|
|
// Default page size does not need this ioctl
|
|
|
|
err = alignPmem(fd, size, 8192);
|
2011-03-08 02:00:54 +00:00
|
|
|
if (err < 0) {
|
|
|
|
LOGE("alignPmem failed");
|
|
|
|
}
|
|
|
|
}
|
2011-11-19 17:55:49 +00:00
|
|
|
void* base = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
|
2010-04-22 00:33:32 +00:00
|
|
|
if (base == MAP_FAILED) {
|
2012-01-26 19:33:18 +00:00
|
|
|
err = -errno;
|
2011-11-19 17:55:49 +00:00
|
|
|
LOGE("%s: failed to map pmem fd: %s", mPmemDev,
|
|
|
|
strerror(errno));
|
|
|
|
close(fd);
|
2010-04-22 00:33:32 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
memset(base, 0, size);
|
2012-02-08 11:25:44 +00:00
|
|
|
clean_buffer((void*)((intptr_t) base + offset), size, offset, fd);
|
2011-11-19 17:55:49 +00:00
|
|
|
data.base = base;
|
|
|
|
data.offset = 0;
|
|
|
|
data.fd = fd;
|
2012-03-31 02:11:00 +00:00
|
|
|
LOGD_IF(DEBUG_PMEM,"%s: Allocated buffer base:%p size:%d fd:%d",
|
2011-12-30 13:43:28 +00:00
|
|
|
mPmemDev, base, size, fd);
|
2010-04-22 00:33:32 +00:00
|
|
|
return 0;
|
2011-11-19 17:55:49 +00:00
|
|
|
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int PmemKernelAlloc::free_buffer(void* base, size_t size, int offset, int fd)
|
|
|
|
{
|
2012-03-31 02:11:00 +00:00
|
|
|
LOGD_IF(DEBUG_PMEM,"%s: Freeing buffer base:%p size:%d fd:%d",
|
2011-12-30 13:43:28 +00:00
|
|
|
mPmemDev, base, size, fd);
|
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int err = unmap_buffer(base, size, offset);
|
|
|
|
close(fd);
|
|
|
|
return err;
|
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int PmemKernelAlloc::map_buffer(void **pBase, size_t size, int offset, int fd)
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
2011-11-19 17:55:49 +00:00
|
|
|
int err = 0;
|
|
|
|
void *base = mmap(0, size, PROT_READ| PROT_WRITE,
|
|
|
|
MAP_SHARED, fd, 0);
|
|
|
|
*pBase = base;
|
|
|
|
if(base == MAP_FAILED) {
|
2012-01-26 19:33:18 +00:00
|
|
|
err = -errno;
|
2011-12-30 13:43:28 +00:00
|
|
|
LOGE("%s: Failed to map memory in the client: %s",
|
|
|
|
mPmemDev, strerror(errno));
|
2011-11-19 17:55:49 +00:00
|
|
|
} else {
|
2012-03-31 02:11:00 +00:00
|
|
|
LOGD_IF(DEBUG_PMEM,"%s: Mapped buffer base:%p size:%d, fd:%d",
|
2011-12-30 13:43:28 +00:00
|
|
|
mPmemDev, base, size, fd);
|
2011-11-19 17:55:49 +00:00
|
|
|
}
|
|
|
|
return err;
|
|
|
|
|
|
|
|
}
|
2010-04-22 00:33:32 +00:00
|
|
|
|
2011-11-19 17:55:49 +00:00
|
|
|
int PmemKernelAlloc::unmap_buffer(void *base, size_t size, int offset)
|
|
|
|
{
|
|
|
|
int err = 0;
|
2012-01-26 19:33:18 +00:00
|
|
|
if (munmap(base, size)) {
|
2011-11-19 17:55:49 +00:00
|
|
|
err = -errno;
|
2012-03-31 02:11:00 +00:00
|
|
|
LOGE("%s: Error unmapping memory at %p: %s",
|
2011-12-30 13:43:28 +00:00
|
|
|
mPmemDev, base, strerror(err));
|
2011-03-08 01:35:59 +00:00
|
|
|
}
|
2011-11-19 17:55:49 +00:00
|
|
|
return err;
|
2011-03-08 01:46:28 +00:00
|
|
|
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
2011-11-19 17:55:49 +00:00
|
|
|
int PmemKernelAlloc::clean_buffer(void *base, size_t size, int offset, int fd)
|
2010-04-22 00:33:32 +00:00
|
|
|
{
|
2011-11-19 17:55:49 +00:00
|
|
|
return cleanPmem(base, size, offset, fd);
|
2010-04-22 00:33:32 +00:00
|
|
|
}
|
2011-11-19 17:55:49 +00:00
|
|
|
|