blob: 935c66a48e9b8de5a3491a7170e2fcad1686c68d [file] [log] [blame]
/*
* Copyright (C) 2021 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef _ION_H
#define _ION_H
#include <errno.h>
#include <log/log.h>
class SystemInterface {
public:
virtual ~SystemInterface() { }
virtual int Open(const char *path);
virtual int Ioctl(int fd, unsigned int cmd, void *data);
virtual int Ioctl(int fd, unsigned int cmd);
virtual int Close(int fd);
};
#define MAX_HEAP_PATH 64
static const char DmaHeapRoot[] = "/dev/dma_heap/";
enum exp_version {
UNKNOWN_VERSION,
ION_MODERN_VERSION,
ION_LEGACY_VERSION,
DMAHEAP_VERSION,
};
class DmabufExporter {
public:
DmabufExporter(SystemInterface &_systemInterface) : systemInterface(_systemInterface), dma_buf_trace_supported(true) {
char path[MAX_HEAP_PATH];
strcpy(path, DmaHeapRoot);
strcat(path, "system");
int fd = systemInterface.Open(path);
if (fd >= 0) {
version = DMAHEAP_VERSION;
} else {
fd = systemInterface.Open("/dev/ion");
if (fd < 0) {
ALOGE("%s Failed to find BOTH DMA-HEAP and ION", __func__);
version = UNKNOWN_VERSION;
return;
}
legacy_free_handle(fd, 0);
version = (errno == ENOTTY) ? ION_MODERN_VERSION : ION_LEGACY_VERSION;
}
systemInterface.Close(fd);
}
int open();
int close(int fd);
int alloc(int ion_fd, size_t len, unsigned int legacy_heap_mask, unsigned int flags);
int import_handle(int ion_fd, int fd, int* handle);
int legacy_free_handle(int ion_fd, int handle);
int free_handle(int ion_fd, int handle);
int sync_fd(int ion_fd, int fd);
int sync_fd_partial(int ion_fd, int fd, off_t offset, size_t len);
int sync(int ion_fd, int fd, int direction, int sync);
int trace_buffer(int fd);
int untrace_buffer(int fd);
private:
int alloc_legacy(int ion_fd, size_t len, unsigned int legacy_heap_mask, unsigned int flags);
int alloc_modern(int ion_fd, size_t len, unsigned int legacy_heap_mask, unsigned int flags);
int alloc_dma_heap(size_t len, unsigned int legacy_heap_mask, unsigned int flags);
int query_heap_id(int ion_fd, unsigned int legacy_heap_mask);
SystemInterface &systemInterface;
bool dma_buf_trace_supported;
enum exp_version version;
};
#endif