Skip to content

Commit

Permalink
Support RK3576 and RKNPU1
Browse files Browse the repository at this point in the history
Signed-off-by: Randall Zhuo <randall.zhuo@rock-chips.com>
  • Loading branch information
Randall Zhuo committed Mar 25, 2024
1 parent cc15d6c commit 9bed8a5
Show file tree
Hide file tree
Showing 223 changed files with 23,134 additions and 6,456 deletions.
52 changes: 52 additions & 0 deletions .gitignore
@@ -0,0 +1,52 @@
*.rknn
*.pt
*.torchscript
*.onnx
*.log
*.pyc
*.zip
*.npy
*.pkl
*.pb

check*.onnx

# ignore the following folder name
build
install

datasets/COCO/annotations/*
datasets/COCO/val2017/*

breakpoint_output/

models/CV/body_analysis/
models/CV/classification/
models/CV/image_segmentation/
tmp/

dump_data_distribute/
snapshot/
snapshot*/

# ignore file
models/CV/object_detection/yolo/RKNN_python_demo/*.json
capi_tools/internal/fixed_frequency_android.sh
capi_tools/internal/fixed_frequency.sh
common/onnx_breakpoint_tools.py
report.yml

developer_conference/
internal_data/
.vscode/
model_cvt
datasets/COCO/try_load.py
common/utils/run_and_gen_result.py
common/utils/update_driver.py
result/
*DS_Store*


# for unittest
unittest/example_test_result
unittest/env_config.py
14 changes: 12 additions & 2 deletions 3rdparty/CMakeLists.txt
Expand Up @@ -25,16 +25,26 @@ set(LIBJPEG ${JPEG_PATH}/${CMAKE_SYSTEM_NAME}/${TARGET_LIB_ARCH}/libturbojpeg.a
set(LIBJPEG_INCLUDES ${JPEG_PATH}/include PARENT_SCOPE)

# rknn runtime
if (TARGET_SOC STREQUAL "rk3588" OR TARGET_SOC STREQUAL "rk356x" OR TARGET_SOC STREQUAL "rv1106" OR TARGET_SOC STREQUAL "rv1103")
# for rknpu2
if (TARGET_SOC STREQUAL "rk3588" OR TARGET_SOC STREQUAL "rk3576" OR TARGET_SOC STREQUAL "rk356x" OR TARGET_SOC STREQUAL "rv1106" OR TARGET_SOC STREQUAL "rv1103")
set(RKNN_PATH ${CMAKE_CURRENT_SOURCE_DIR}/rknpu2)
if (TARGET_SOC STREQUAL "rk3588" OR TARGET_SOC STREQUAL "rk356x")
if (TARGET_SOC STREQUAL "rk3588" OR TARGET_SOC STREQUAL "rk356x" OR TARGET_SOC STREQUAL "rk3576" )
set(LIBRKNNRT ${RKNN_PATH}/${CMAKE_SYSTEM_NAME}/${TARGET_LIB_ARCH}/librknnrt.so)
endif()
if (TARGET_SOC STREQUAL "rv1106" OR TARGET_SOC STREQUAL "rv1103")
set(LIBRKNNRT ${RKNN_PATH}/${CMAKE_SYSTEM_NAME}/armhf-uclibc/librknnmrt.so)
endif()
set(LIBRKNNRT_INCLUDES ${RKNN_PATH}/include PARENT_SCOPE)
endif()

# for rknpu1
if(TARGET_SOC STREQUAL "rk1808" OR TARGET_SOC STREQUAL "rv1109" OR TARGET_SOC STREQUAL "rv1126")
set(RKNN_PATH ${CMAKE_CURRENT_SOURCE_DIR}/rknpu1)

set(LIBRKNNRT ${RKNN_PATH}/${CMAKE_SYSTEM_NAME}/${TARGET_LIB_ARCH}/librknn_api.so)

set(LIBRKNNRT_INCLUDES ${RKNN_PATH}/include PARENT_SCOPE)
endif()
install(PROGRAMS ${LIBRKNNRT} DESTINATION lib)
set(LIBRKNNRT ${LIBRKNNRT} PARENT_SCOPE)

Expand Down
163 changes: 163 additions & 0 deletions 3rdparty/allocator/dma/dma_alloc.hpp
@@ -0,0 +1,163 @@
/*
* Copyright (C) 2022 Rockchip Electronics Co., Ltd.
* Authors:
* Cerf Yu <cerf.yu@rock-chips.com>
*
* 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 <getopt.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <time.h>
#include <stdbool.h>
#include <assert.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/poll.h>
#include <unistd.h>
#include <stdbool.h>
#include <sys/eventfd.h>

#include <sched.h>
#include <pthread.h>

#include <stdint.h>
#include <math.h>
#include <memory.h>
#include <sys/time.h>

#include "dma_alloc.h"
#include "RgaUtils.h"

typedef unsigned long long __u64;
typedef unsigned int __u32;

struct dma_heap_allocation_data {
__u64 len;
__u32 fd;
__u32 fd_flags;
__u64 heap_flags;
};

struct dma_heap_import_data {
__u64 len;
__u32 fd;
__u32 fd_flags;
__u64 heap_flags;
};

#define DMA_HEAP_IOC_MAGIC 'H'
#define DMA_HEAP_IOCTL_ALLOC _IOWR(DMA_HEAP_IOC_MAGIC, 0x0,\
struct dma_heap_allocation_data)
#define DMA_HEAP_IOCTL_IMPORT _IOWR(DMA_HEAP_IOC_MAGIC, 0x1,\
struct dma_heap_import_data)

#define DMA_BUF_SYNC_READ (1 << 0)
#define DMA_BUF_SYNC_WRITE (2 << 0)
#define DMA_BUF_SYNC_RW (DMA_BUF_SYNC_READ | DMA_BUF_SYNC_WRITE)
#define DMA_BUF_SYNC_START (0 << 2)
#define DMA_BUF_SYNC_END (1 << 2)

struct dma_buf_sync {
__u64 flags;
};

#define DMA_BUF_BASE 'b'
#define DMA_BUF_IOCTL_SYNC _IOW(DMA_BUF_BASE, 0, struct dma_buf_sync)

#define CMA_HEAP_SIZE 1024 * 1024

int dma_sync_device_to_cpu(int fd) {
struct dma_buf_sync sync = {0};

sync.flags = DMA_BUF_SYNC_START | DMA_BUF_SYNC_RW;
return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
}

int dma_sync_cpu_to_device(int fd) {
struct dma_buf_sync sync = {0};

sync.flags = DMA_BUF_SYNC_END | DMA_BUF_SYNC_RW;
return ioctl(fd, DMA_BUF_IOCTL_SYNC, &sync);
}

int dma_buf_alloc(const char *path, size_t size, int *fd, void **va) {
int ret;
int prot;
void *mmap_va;
int dma_heap_fd = -1;
struct dma_heap_allocation_data buf_data;

/* open dma_heap fd */
if (dma_heap_fd < 0) {
dma_heap_fd = open(path, O_RDWR);
if (dma_heap_fd < 0) {
printf("open %s fail!\n", path);
return dma_heap_fd;
}
}

/* alloc buffer */
memset(&buf_data, 0x0, sizeof(struct dma_heap_allocation_data));

buf_data.len = size;
buf_data.fd_flags = O_CLOEXEC | O_RDWR;
ret = ioctl(dma_heap_fd, DMA_HEAP_IOCTL_ALLOC, &buf_data);
if (ret < 0) {
printf("RK_DMA_HEAP_ALLOC_BUFFER failed\n");
return ret;
}

/* mmap va */
if (fcntl(buf_data.fd, F_GETFL) & O_RDWR)
prot = PROT_READ | PROT_WRITE;
else
prot = PROT_READ;

/* mmap contiguors buffer to user */
mmap_va = (void *)mmap(NULL, buf_data.len, prot, MAP_SHARED, buf_data.fd, 0);
if (mmap_va == MAP_FAILED) {
printf("mmap failed: %s\n", strerror(errno));
return -errno;
}

*va = mmap_va;
*fd = buf_data.fd;

close(dma_heap_fd);

return 0;
}

void dma_buf_free(size_t size, int *fd, void *va) {
int len;

len = size;
munmap(va, len);

close(*fd);
*fd = -1;
}



0 comments on commit 9bed8a5

Please sign in to comment.