47 AampIonMemorySystem::AampIonMemoryContext::AampIonMemoryContext(
AampLogManager *logObj) : fd_(0), handle_(0), mLogObj(logObj) {};
49 bool AampIonMemorySystem::AampIonMemoryContext::createBuffer(
size_t len) {
52 AAMPLOG_WARN(
"Calling ion_open(): %d", fd_);
55 AAMPLOG_INFO(
"Got %d from ion_open()", fd_);
57 int ret = ion_alloc(fd_, len,
58 AAMP_ION_MEMORY_ALIGN,
59 AAMP_ION_MEMORY_REGION,
60 AAMP_ION_MEMORY_FLAGS, &handle_);
63 AAMPLOG_WARN(
"Calling ion_alloc(): %d", fd_);
78 bool closeOnDestruct_;
82 , closeOnDestruct_(
true) {}
84 if (closeOnDestruct_) {
88 void setNoCloseOnDestruct() {
89 closeOnDestruct_ =
false;
93 AampIonMemorySystem::AampIonMemoryContext::~AampIonMemoryContext() {
97 void AampIonMemorySystem::AampIonMemoryContext::close() {
99 int ret = ion_free(fd_, handle_);
102 AAMPLOG_ERR(
"ion_free_buffer failed");
109 AAMPLOG_INFO(
"Closed ION memory ok");
112 bool AampIonMemorySystem::AampIonMemoryContext::share(
int& shareFd) {
113 int status = ion_share(fd_, handle_, &shareFd);
117 bool AampIonMemorySystem::AampIonMemoryContext::phyAddr(
unsigned long *phyAddr) {
119 int status = ion_phys(fd_, handle_, phyAddr, &size);
130 if (!context_.createBuffer(dataInSz))
132 AAMPLOG_WARN(
"Failed to create Ion memory object: %d", errno);
137 if (!context_.share(share_fd))
139 AAMPLOG_WARN(
"Failed to share Ion memory object: %d", errno);
145 void *dataWr = mmap(NULL, dataInSz, PROT_WRITE | PROT_READ, MAP_SHARED, share_fd, 0);
148 AAMPLOG_WARN(
"Failed to map the Ion memory object %d", errno);
152 memmove(dataWr, dataIn, dataInSz);
154 int status = munmap(dataWr, dataInSz);
157 AAMPLOG_WARN(
"Failed to unmap the Ion memory object %d", errno);
162 if (!context_.phyAddr(&ib.phyAddr)) {
163 AAMPLOG_WARN(
"Failed to find the physical address of the Ion memory object %d", errno);
166 ib.size =
sizeof(ib);
167 ib.dataSize = dataInSz;
170 dataOut.resize(
sizeof(ib));
171 memcpy(dataOut.data(), &ib,
sizeof(ib));
172 closer.setNoCloseOnDestruct();
190 unsigned long phyAddr;
191 if (!context_.phyAddr(&phyAddr)) {
192 AAMPLOG_WARN(
"Failed to get physical address of ION memory: %d", errno);
196 AAMPLOG_WARN(
"Physical address of ION memory not what was sent... %d", errno);
201 if (!context_.share(share_fd))
203 AAMPLOG_WARN(
"Failed to share Ion memory object: %d", errno);
209 uint32_t packetSize = pib->
dataSize;
210 void *dataRd = mmap(NULL, packetSize, PROT_READ, MAP_SHARED, share_fd, 0);
213 AAMPLOG_WARN(
"Failed to map the Ion memory object %d", errno);
217 if (packetSize > dataOutSz)
219 AAMPLOG_WARN(
"Received data is bigger than provided buffer. %d > %d", packetSize, dataOutSz);
221 memmove(dataOut, dataRd, std::min(packetSize, dataOutSz));
223 int status = munmap(dataRd, packetSize);
226 AAMPLOG_WARN(
"Failed to unmap the shared memory object %d", errno);
238 AAMPLOG_WARN(
"closing transfer early");