1*523fa7a6SAndroid Build Coastguard Worker /* 2*523fa7a6SAndroid Build Coastguard Worker * Copyright (c) Qualcomm Innovation Center, Inc. 3*523fa7a6SAndroid Build Coastguard Worker * All rights reserved. 4*523fa7a6SAndroid Build Coastguard Worker * 5*523fa7a6SAndroid Build Coastguard Worker * This source code is licensed under the BSD-style license found in the 6*523fa7a6SAndroid Build Coastguard Worker * LICENSE file in the root directory of this source tree. 7*523fa7a6SAndroid Build Coastguard Worker */ 8*523fa7a6SAndroid Build Coastguard Worker #pragma once 9*523fa7a6SAndroid Build Coastguard Worker #include <QnnTypes.h> 10*523fa7a6SAndroid Build Coastguard Worker #include <executorch/backends/qualcomm/runtime/QnnExecuTorch.h> 11*523fa7a6SAndroid Build Coastguard Worker #include <executorch/runtime/core/error.h> 12*523fa7a6SAndroid Build Coastguard Worker #include <atomic> 13*523fa7a6SAndroid Build Coastguard Worker #include <cstdint> 14*523fa7a6SAndroid Build Coastguard Worker #include <memory> 15*523fa7a6SAndroid Build Coastguard Worker #include <mutex> 16*523fa7a6SAndroid Build Coastguard Worker #include <unordered_map> 17*523fa7a6SAndroid Build Coastguard Worker #include <unordered_set> 18*523fa7a6SAndroid Build Coastguard Worker 19*523fa7a6SAndroid Build Coastguard Worker using RpcMemAllocFn_t = void* (*)(int, uint32_t, int); 20*523fa7a6SAndroid Build Coastguard Worker using RpcMemFreeFn_t = void (*)(void*); 21*523fa7a6SAndroid Build Coastguard Worker using RpcMemToFdFn_t = int (*)(void*); 22*523fa7a6SAndroid Build Coastguard Worker 23*523fa7a6SAndroid Build Coastguard Worker // TODO Finad a better file to place CustomMemTensorInfo 24*523fa7a6SAndroid Build Coastguard Worker bool operator==(const CustomMemTensorInfo& lhs, const CustomMemTensorInfo& rhs); 25*523fa7a6SAndroid Build Coastguard Worker template <> 26*523fa7a6SAndroid Build Coastguard Worker struct std::hash<CustomMemTensorInfo> { 27*523fa7a6SAndroid Build Coastguard Worker std::size_t operator()(const CustomMemTensorInfo& info) const noexcept; 28*523fa7a6SAndroid Build Coastguard Worker }; 29*523fa7a6SAndroid Build Coastguard Worker 30*523fa7a6SAndroid Build Coastguard Worker namespace executorch { 31*523fa7a6SAndroid Build Coastguard Worker namespace backends { 32*523fa7a6SAndroid Build Coastguard Worker namespace qnn { 33*523fa7a6SAndroid Build Coastguard Worker 34*523fa7a6SAndroid Build Coastguard Worker class SharedBuffer final { 35*523fa7a6SAndroid Build Coastguard Worker public: 36*523fa7a6SAndroid Build Coastguard Worker SharedBuffer(const SharedBuffer&) = delete; 37*523fa7a6SAndroid Build Coastguard Worker SharedBuffer& operator=(const SharedBuffer&) = delete; 38*523fa7a6SAndroid Build Coastguard Worker SharedBuffer(SharedBuffer&&) = delete; 39*523fa7a6SAndroid Build Coastguard Worker SharedBuffer& operator=(SharedBuffer&&) = delete; 40*523fa7a6SAndroid Build Coastguard Worker ~SharedBuffer(); 41*523fa7a6SAndroid Build Coastguard Worker 42*523fa7a6SAndroid Build Coastguard Worker static SharedBuffer& GetSharedBufferManager(); 43*523fa7a6SAndroid Build Coastguard Worker void* AllocMem(size_t bytes, size_t alignment); 44*523fa7a6SAndroid Build Coastguard Worker // map a buffer allocated via RPCMem to a file descriptor so it can be 45*523fa7a6SAndroid Build Coastguard Worker // registered with a backend via QnnMem_register() 46*523fa7a6SAndroid Build Coastguard Worker int32_t MemToFd(void* buf); 47*523fa7a6SAndroid Build Coastguard Worker 48*523fa7a6SAndroid Build Coastguard Worker void FreeMem(void* buf); 49*523fa7a6SAndroid Build Coastguard Worker 50*523fa7a6SAndroid Build Coastguard Worker bool IsAllocated(void* buf); 51*523fa7a6SAndroid Build Coastguard Worker 52*523fa7a6SAndroid Build Coastguard Worker bool GetInitialize() { 53*523fa7a6SAndroid Build Coastguard Worker return initialize_; 54*523fa7a6SAndroid Build Coastguard Worker } 55*523fa7a6SAndroid Build Coastguard Worker void SetInitialize(bool initialize) { 56*523fa7a6SAndroid Build Coastguard Worker initialize_ = initialize; 57*523fa7a6SAndroid Build Coastguard Worker } 58*523fa7a6SAndroid Build Coastguard Worker 59*523fa7a6SAndroid Build Coastguard Worker // memory handle is registered during execution 60*523fa7a6SAndroid Build Coastguard Worker void AddCusomMemTensorAddr(void* tensor_addr, void* custom_mem); 61*523fa7a6SAndroid Build Coastguard Worker 62*523fa7a6SAndroid Build Coastguard Worker // memory handle can be registered before execution 63*523fa7a6SAndroid Build Coastguard Worker void AddCusomMemTensorInfo(const CustomMemTensorInfo& info); 64*523fa7a6SAndroid Build Coastguard Worker 65*523fa7a6SAndroid Build Coastguard Worker size_t GetAllocatedSize(void* buf); 66*523fa7a6SAndroid Build Coastguard Worker 67*523fa7a6SAndroid Build Coastguard Worker void* GetCustomMemBase(void* buf); 68*523fa7a6SAndroid Build Coastguard Worker 69*523fa7a6SAndroid Build Coastguard Worker void* GetUnAlignedAddr(void* buf); 70*523fa7a6SAndroid Build Coastguard Worker 71*523fa7a6SAndroid Build Coastguard Worker const std::unordered_set<CustomMemTensorInfo>& GetCustomMemTensorInfoSet() { 72*523fa7a6SAndroid Build Coastguard Worker return custom_mem_tensor_info_set_; 73*523fa7a6SAndroid Build Coastguard Worker }; 74*523fa7a6SAndroid Build Coastguard Worker 75*523fa7a6SAndroid Build Coastguard Worker private: 76*523fa7a6SAndroid Build Coastguard Worker SharedBuffer() = default; 77*523fa7a6SAndroid Build Coastguard Worker 78*523fa7a6SAndroid Build Coastguard Worker // dlopen RPCMem library and dlysm required functions 79*523fa7a6SAndroid Build Coastguard Worker executorch::runtime::Error Load(); 80*523fa7a6SAndroid Build Coastguard Worker 81*523fa7a6SAndroid Build Coastguard Worker executorch::runtime::Error UnLoad(); 82*523fa7a6SAndroid Build Coastguard Worker 83*523fa7a6SAndroid Build Coastguard Worker // Pointer to the dlopen'd libcdsprpc.so shared library which contains 84*523fa7a6SAndroid Build Coastguard Worker // rpcmem_alloc, rpcmem_free, rpcmem_to_fd APIs 85*523fa7a6SAndroid Build Coastguard Worker void* lib_cdsp_rpc_; 86*523fa7a6SAndroid Build Coastguard Worker // Function pointer to rpcmem_alloc 87*523fa7a6SAndroid Build Coastguard Worker RpcMemAllocFn_t rpc_mem_alloc_; 88*523fa7a6SAndroid Build Coastguard Worker // Function pointer to rpcmem_free 89*523fa7a6SAndroid Build Coastguard Worker RpcMemFreeFn_t rpc_mem_free_; 90*523fa7a6SAndroid Build Coastguard Worker // Function pointer to rpcmem_to_fd 91*523fa7a6SAndroid Build Coastguard Worker RpcMemToFdFn_t rpc_mem_to_fd_; 92*523fa7a6SAndroid Build Coastguard Worker std::unordered_map<void*, void*> restore_map_; 93*523fa7a6SAndroid Build Coastguard Worker std::unordered_map<void*, size_t> allocated_size_map_; 94*523fa7a6SAndroid Build Coastguard Worker // Maps for the custom memory 95*523fa7a6SAndroid Build Coastguard Worker std::unordered_map<void*, void*> tensor_addr_to_custom_mem_; 96*523fa7a6SAndroid Build Coastguard Worker std::unordered_set<CustomMemTensorInfo> custom_mem_tensor_info_set_; 97*523fa7a6SAndroid Build Coastguard Worker std::atomic_bool initialize_{false}; 98*523fa7a6SAndroid Build Coastguard Worker static std::mutex init_mutex_; 99*523fa7a6SAndroid Build Coastguard Worker }; 100*523fa7a6SAndroid Build Coastguard Worker 101*523fa7a6SAndroid Build Coastguard Worker } // namespace qnn 102*523fa7a6SAndroid Build Coastguard Worker } // namespace backends 103*523fa7a6SAndroid Build Coastguard Worker } // namespace executorch 104