-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDMABUFFAllocator.cpp
More file actions
146 lines (132 loc) · 4.47 KB
/
Copy pathDMABUFFAllocator.cpp
File metadata and controls
146 lines (132 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
// SPDX-License-Identifier: BSD-3-Clause-Clear
#include "QC/Infras/Memory/DMABUFFAllocator.hpp"
#include <linux/dma-heap.h>
#include <plat_dmabuf.h>
namespace QC
{
namespace Memory
{
DMABUFFAllocator::DMABUFFAllocator( const QCMemoryAllocatorConfigInit_t &config,
const QCMemoryAllocator_e allocator )
: QCMemoryAllocatorIfs( config, allocator )
{
(void) QC_LOGGER_INIT( GetConfiguration().name.c_str(), LOGGER_LEVEL_ERROR );
m_dmaBufDevFdCached = dmabufheap_init( ID_DMA_BUF_HEAP_CACHED );
QC_DEBUG( " dmabufheap_init(ID_DMA_BUF_HEAP_CACHED) = %d ", m_dmaBufDevFdCached );
}
DMABUFFAllocator::~DMABUFFAllocator()
{
dmabufheap_release( m_dmaBufDevFdCached );
m_dmaBufDevFdCached = DMABUF_HEAP_ALLOCATOR_DEFAULT_FD_CACHED;
(void) QC_LOGGER_DEINIT();
}
QCStatus_e DMABUFFAllocator::Allocate( const QCBufferPropBase_t &request,
QCBufferDescriptorBase_t &response )
{
QCStatus_e status = QC_STATUS_OK;
response.pBuf = nullptr;
int fd = DMABUF_HEAP_ALLOCATOR_DEFAULT_FD_CACHED;
if ( request.cache != QC_CACHEABLE )
{
status = QC_STATUS_UNSUPPORTED;
QC_ERROR( " request.cache != QC_CACHEABLE, unsupported request" );
}
else if ( m_dmaBufDevFdCached < 0 )
{
status = QC_STATUS_FAIL;
QC_ERROR( " m_dmaBufDevFdCached < 0, DMA_BUFF chached device file descriptor not "
"initialized" );
}
else if ( 0 == request.size )
{
status = QC_STATUS_BAD_ARGUMENTS;
QC_ERROR( " 0 == request.size" );
}
else if ( request.alignment > QC_MEMORY_DEFAULT_ALLIGNMENT )
{
status = QC_STATUS_UNSUPPORTED;
QC_ERROR( " request.alignment (%d) > QC_MEMORY_DEFAULT_ALLIGNMENT (%d), lerger than "
"supported",
request.alignment, QC_MEMORY_DEFAULT_ALLIGNMENT );
}
else
{
int rc = dmabufheap_alloc( m_dmaBufDevFdCached, request.size, 0, &fd );
if ( rc < 0 )
{
QC_ERROR( "DmaAllocate failed to do dmabuf heap alloc: %d", rc );
status = QC_STATUS_NOMEM;
}
else
{
QC_DEBUG( "DmaAllocate allocated fd: %d", fd );
}
}
if ( QC_STATUS_OK == status )
{
void *pAddr = mmap( NULL, request.size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 );
if ( ( nullptr != pAddr ) && ( MAP_FAILED != pAddr ) )
{
response.pBuf = pAddr;
response.dmaHandle = static_cast<uint64_t>( fd );
response.size = request.size;
response.cache = QC_CACHEABLE;
response.allocatorType = GetConfiguration().type;
response.pid = getpid();
response.name = GetConfiguration().name + "-" + std::to_string( response.dmaHandle );
QC_DEBUG( "DmaAllocate allocated fd: %d with pointer %p", fd, pAddr );
}
else
{
QC_ERROR( "mmap failed to mmap: %d", errno );
status = QC_STATUS_FAIL;
(void) dmabufheap_free( fd );
}
}
return status;
}
QCStatus_e DMABUFFAllocator::Free( const QCBufferDescriptorBase_t &BufferDescriptor )
{
QCStatus_e status = QC_STATUS_OK;
int fd = static_cast<int>( BufferDescriptor.dmaHandle );
if ( nullptr == BufferDescriptor.pBuf )
{
QC_ERROR( "nullptr == BufferDescriptor.pBuf" );
status = QC_STATUS_BAD_ARGUMENTS;
}
else if ( m_dmaBufDevFdCached < 0 )
{
status = QC_STATUS_FAIL;
QC_ERROR( " m_dmaBufDevFdCached < 0, DMA_BUFF chached device file descriptor not "
"initialized" );
}
else if ( fd < 0 )
{
QC_ERROR( "BufferDescriptor.dmaHandle < 0" );
status = QC_STATUS_BAD_ARGUMENTS;
}
else if ( 0 == BufferDescriptor.size )
{
QC_ERROR( "BufferDescriptor.size != 0" );
status = QC_STATUS_BAD_ARGUMENTS;
}
else
{
int rc = munmap( BufferDescriptor.pBuf, BufferDescriptor.size );
if ( 0 != rc )
{
QC_ERROR( "munmap failed buffer %p: %d", BufferDescriptor.pBuf, rc );
status = QC_STATUS_FAIL;
}
rc = dmabufheap_free( fd );
if ( 0 != rc )
{
QC_ERROR( "Close fd %ull failed , rc %d", BufferDescriptor.dmaHandle, rc );
status = QC_STATUS_FAIL;
}
}
return status;
}
} // namespace Memory
} // namespace QC