Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions runtime/onert/backend/trix/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ message(STATUS "ONERT backend: Found TRIXEngine")

file(GLOB_RECURSE SOURCES "*.cc")

file(GLOB_RECURSE TESTS "*.test.cc")
list(REMOVE_ITEM SOURCES ${TESTS})

add_library(${LIB_ONERT_BACKEND_TRIX} SHARED ${SOURCES})

target_link_libraries(${LIB_ONERT_BACKEND_TRIX} PRIVATE onert_core)
Expand All @@ -24,3 +27,23 @@ set_target_properties(${LIB_ONERT_BACKEND_TRIX} PROPERTIES
INSTALL_RPATH ${ONERT_RPATH_PLUGIN})

install(TARGETS ${LIB_ONERT_BACKEND_TRIX} DESTINATION ${ONERT_INSTALL_BACKENDDIR})

if(NOT ENABLE_TEST)
return()
endif(NOT ENABLE_TEST)

# Unit Tests
set(TEST_ONERT_TRIX_BACKEND test_onert_trix_backend)

add_executable(${TEST_ONERT_TRIX_BACKEND} ${TESTS})
target_link_libraries(${TEST_ONERT_TRIX_BACKEND} onert_core)
target_link_libraries(${TEST_ONERT_TRIX_BACKEND} trix-engine)
target_link_libraries(${TEST_ONERT_TRIX_BACKEND} ${LIB_ONERT_BACKEND_TRIX})
target_link_libraries(${TEST_ONERT_TRIX_BACKEND} nnfw_common)
target_link_libraries(${TEST_ONERT_TRIX_BACKEND} nnfw_coverage)
target_link_libraries(${TEST_ONERT_TRIX_BACKEND} gtest gtest_main Threads::Threads)
set_target_properties(${TEST_ONERT_TRIX_BACKEND} PROPERTIES
INSTALL_RPATH "$ORIGIN/../${ONERT_INSTALL_COREDIR}:$ORIGIN/../${ONERT_INSTALL_BACKENDDIR}")

add_test(${TEST_ONERT_TRIX_BACKEND} ${TEST_ONERT_TRIX_BACKEND})
install(TARGETS ${TEST_ONERT_TRIX_BACKEND} DESTINATION unittest)
176 changes: 176 additions & 0 deletions runtime/onert/backend/trix/ops/BulkPipelineBuffer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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.
*/

#include "BulkPipelineBuffer.h"

#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <cstring>
#include <iostream>

namespace onert
{
namespace backend
{
namespace trix
{
namespace ops
{

// FIXME: Using higher level API instead of raw API
struct trix_ioctl_hwmem
{
int32_t type;
uint64_t size;
int32_t dbuf_fd;
} __attribute__((packed));

#define TRIX_IOCTL_HWMEM_ALLOC _IOW(136, 21, struct trix_ioctl_hwmem)
#define TRIX_IOCTL_HWMEM_DEALLOC _IOW(136, 22, struct trix_ioctl_hwmem)

BulkPipelineBuffer::BulkPipelineBuffer(BufferType type, size_t size, int device_id)
: _type(type), _size(size), _device_id(device_id)
{
// DO NOTHING
}

BulkPipelineBuffer::~BulkPipelineBuffer() { deallocate(); }

size_t BulkPipelineBuffer::size() const { return _buffer ? _buffer->size : 0; }

bool BulkPipelineBuffer::isReady() const { return _buffer && _buffer->addr != nullptr; }

void BulkPipelineBuffer::allocate()
{
if (_buffer && _buffer->addr != nullptr)
{
// Already allocated
return;
}

if (!_buffer)
{
_buffer = new generic_buffer{};
}

// Open the device
char devname[16];
snprintf(devname, sizeof(devname), "/dev/triv2-%d", _device_id);
_dev_fd = open(devname, O_RDWR);
if (_dev_fd < 0)
{
throw std::runtime_error("Failed to open NPU device: " + std::string(devname));
}

// Allocate a buffer
struct trix_ioctl_hwmem hwmem;
hwmem.type = (_type == BufferType::DMABUF_CONT) ? 0 : 1;
hwmem.size = getAlignedSize(_size);

_buffer->dmabuf = ioctl(_dev_fd, TRIX_IOCTL_HWMEM_ALLOC, &hwmem);
if (_buffer->dmabuf < 0)
{
close(_dev_fd);
_dev_fd = -1;
throw std::runtime_error("Failed to allocate DMA buffer, size: " + std::to_string(hwmem.size));
}

// Mapping the buffer
_buffer->addr = mmap(nullptr, hwmem.size, PROT_READ | PROT_WRITE, MAP_SHARED, _buffer->dmabuf, 0);
if (_buffer->addr == MAP_FAILED)
{
close(_buffer->dmabuf);
close(_dev_fd);
_buffer->dmabuf = -1;
_dev_fd = -1;
_buffer->addr = nullptr;
throw std::runtime_error("Failed to mmap DMA buffer");
}

_buffer->size = _size;
_buffer->type = BUFFER_DMABUF;
}

void BulkPipelineBuffer::deallocate()
{
if (!_buffer)
{
return;
}

if (_buffer->addr != nullptr)
{
size_t aligned_sz = getAlignedSize(_buffer->size);
munmap(_buffer->addr, aligned_sz);
_buffer->addr = nullptr;
}

if (_buffer->dmabuf >= 0)
{
struct trix_ioctl_hwmem hwmem;
hwmem.dbuf_fd = _buffer->dmabuf;
ioctl(_dev_fd, TRIX_IOCTL_HWMEM_DEALLOC, &hwmem);
close(_buffer->dmabuf);
_buffer->dmabuf = -1;
}

if (_dev_fd >= 0)
{
close(_dev_fd);
_dev_fd = -1;
}

delete _buffer;
_buffer = nullptr;
}

void BulkPipelineBuffer::fillFromFile(FILE *fp, size_t offset)
{
if (!isReady())
{
throw std::runtime_error("Buffer is not allocated");
}

if (!fp)
{
throw std::runtime_error("Invalid file pointer");
}

if (fseek(fp, static_cast<long>(offset), SEEK_SET) != 0)
{
throw std::runtime_error("Failed to seek file to offset: " + std::to_string(offset));
}

if (fread(_buffer->addr, _buffer->size, 1, fp) != 1)
{
throw std::runtime_error("Failed to read " + std::to_string(_buffer->size) +
" bytes from file");
}
}

size_t BulkPipelineBuffer::getAlignedSize(size_t size) const
{
// 4 KB (= Page size) aligned size
constexpr size_t _4KB_M_1 = (1 << 12) - 1;
return (size + _4KB_M_1) & ~_4KB_M_1;
}

} // namespace ops
} // namespace trix
} // namespace backend
} // namespace onert
78 changes: 78 additions & 0 deletions runtime/onert/backend/trix/ops/BulkPipelineBuffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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 __ONERT_BACKEND_TRIX_OPS_BULKPIPELINEBUFFER_H__
#define __ONERT_BACKEND_TRIX_OPS_BULKPIPELINEBUFFER_H__

#include <memory>
#include <cstdio>
#include <stdexcept>
#include <libnpuhost.h>

namespace onert
{
namespace backend
{
namespace trix
{
namespace ops
{

class BulkPipelineBuffer
{
public:
enum class BufferType
{
DMABUF_CONT, // Contiguous DMA buffer
DMABUF_IOMMU // IOMMU DMA buffer
};

public:
BulkPipelineBuffer(BufferType type, size_t size, int device_id);
~BulkPipelineBuffer();

// Disallow copying
BulkPipelineBuffer(const BulkPipelineBuffer &) = delete;
BulkPipelineBuffer &operator=(const BulkPipelineBuffer &) = delete;

// Buffer management functions
void allocate();
void deallocate();
size_t size() const;

generic_buffer *getGenericBuffer() { return _buffer; }

// Data manipulation functions
void fillFromFile(FILE *fp, size_t offset = 0);
bool isReady() const;

private:
size_t getAlignedSize(size_t size) const;

private:
BufferType _type;
size_t _size;
int _device_id;
int _dev_fd{-1};
generic_buffer *_buffer{nullptr};
};

} // namespace ops
} // namespace trix
} // namespace backend
} // namespace onert

#endif // __ONERT_BACKEND_TRIX_OPS_BULKPIPELINEBUFFER_H__
73 changes: 73 additions & 0 deletions runtime/onert/backend/trix/ops/test/BulkPipelineBuffer.test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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.
*/

#include "../BulkPipelineBuffer.h"
#include <gtest/gtest.h>

#include "mock_syscalls.h"

using namespace onert::backend::trix::ops;

class BulkPipelineBufferTest : public ::testing::Test
{
protected:
void SetUp() override
{
// Create a standard buffer for testing
buffer =
std::make_unique<BulkPipelineBuffer>(BulkPipelineBuffer::BufferType::DMABUF_CONT, 1024, 0);
}

void TearDown() override
{
// Ensure buffer is properly deallocated
if (buffer && buffer->isReady())
{
buffer->deallocate();
}
}

std::unique_ptr<BulkPipelineBuffer> buffer;
};

TEST_F(BulkPipelineBufferTest, test_allocate)
{
EXPECT_NO_THROW(buffer->allocate());
EXPECT_TRUE(buffer->isReady());
EXPECT_EQ(buffer->size(), 1024);
}

TEST_F(BulkPipelineBufferTest, test_deallocate)
{
buffer->allocate();
buffer->deallocate();
EXPECT_FALSE(buffer->isReady());
EXPECT_EQ(buffer->size(), 0);
}

TEST_F(BulkPipelineBufferTest, test_fillFromFile)
{
auto dummy_fp = fopen("/dev/null", "r");
ASSERT_NE(dummy_fp, nullptr) << "Failed to open /dev/null for testing";

EXPECT_ANY_THROW(buffer->fillFromFile(nullptr, 0));

buffer->allocate();
EXPECT_NO_THROW(buffer->fillFromFile(dummy_fp, 0));
buffer->deallocate();

fclose(dummy_fp);
}
33 changes: 33 additions & 0 deletions runtime/onert/backend/trix/ops/test/mock_syscalls.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2025 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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 _MOCK_SYSCALLS_H_
#define _MOCK_SYSCALLS_H_

#include <sys/mman.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>

int open(const char *, int, ...) { return 0; }
void *mmap(void *, size_t, int, int, int, off_t) { return (void *)0x1; }
int munmap(void *, size_t) { return 0; }
int close(int) { return 0; }
int ioctl(int, unsigned long, ...) { return 0; }
size_t fread(void *, size_t, size_t, FILE *) { return 1; }
int fseek(FILE *, long, int) { return 0; }

#endif