|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#ifndef TVM_RUNTIME_HEXAGON_ANDROID_HEXAGON_DEVICE_H_ |
| 21 | +#define TVM_RUNTIME_HEXAGON_ANDROID_HEXAGON_DEVICE_H_ |
| 22 | + |
| 23 | +#include <tvm/runtime/logging.h> |
| 24 | +#include <tvm/runtime/module.h> |
| 25 | + |
| 26 | +#include <memory> |
| 27 | +#include <string> |
| 28 | + |
| 29 | +#include "../../meta_data.h" |
| 30 | + |
| 31 | +namespace tvm { |
| 32 | +namespace runtime { |
| 33 | +namespace hexagon { |
| 34 | + |
| 35 | +/*! |
| 36 | + * \brief Low-level interface for communicating with Hexagon devices. |
| 37 | + */ |
| 38 | +class Device { |
| 39 | + public: |
| 40 | + /*! |
| 41 | + * \brief Allocate memory on device. |
| 42 | + * \param size Requested size. |
| 43 | + * \param align Requested alignment. |
| 44 | + * \return Pointer (local to the device) of the allocated memory, |
| 45 | + * or nullptr if allocation failed. |
| 46 | + */ |
| 47 | + virtual void* Alloc(unsigned size, unsigned align) = 0; |
| 48 | + /*! |
| 49 | + * \brief Release allocated memory on device. |
| 50 | + * \param ptr Pointer to memory previously allocated by \ref Alloc. |
| 51 | + */ |
| 52 | + virtual void Free(void* ptr) = 0; |
| 53 | + /*! |
| 54 | + * \brief Allocate VTCM memory on device. |
| 55 | + * \param size Requested size. |
| 56 | + * \param align Requested alignment. |
| 57 | + * \return Pointer (local to the device) of the allocated memory, |
| 58 | + * or nullptr if allocation failed. |
| 59 | + */ |
| 60 | + virtual void* AllocVtcm(unsigned size, unsigned align) = 0; |
| 61 | + /*! |
| 62 | + * \brief Release allocated VTCM memory on device. |
| 63 | + * \param ptr Pointer to memory previously allocated by \ref AllocVtcm. |
| 64 | + */ |
| 65 | + virtual void FreeVtcm(void* ptr) = 0; |
| 66 | + /*! |
| 67 | + * \brief Copy a block of data on device to another location on the device. |
| 68 | + * \param dst Pointer (local to device) to the destination buffer. |
| 69 | + * \param src Pointer (local to device) of the source buffer. |
| 70 | + * \param len Number of bytes to copy. |
| 71 | + */ |
| 72 | + virtual void CopyDeviceToDevice(void* dst, const void* src, unsigned len) = 0; |
| 73 | + /*! |
| 74 | + * \brief Copy a block of data from device to host. |
| 75 | + * \param host_dst Pointer (local to host) to the destination buffer. |
| 76 | + * \param src Pointer (local to device) to the source buffer. |
| 77 | + * \param len Number of bytes to copy. |
| 78 | + */ |
| 79 | + virtual void CopyDeviceToHost(void* host_dst, const void* src, unsigned len) = 0; |
| 80 | + /*! |
| 81 | + * \brief Copy a block of data from host to device. |
| 82 | + * \param dst Pointer (local to device) to the destination buffer. |
| 83 | + * \param host_src Pointer (local to host) to the source buffer. |
| 84 | + * \param len Number of bytes to copy. |
| 85 | + */ |
| 86 | + virtual void CopyHostToDevice(void* dst, const void* host_src, unsigned len) = 0; |
| 87 | + /*! |
| 88 | + * \brief Load a module (typically a shared library) into device. |
| 89 | + * \param data Name of the shared library. |
| 90 | + * \param fmt Format of the library (currently ignored). |
| 91 | + * \return Pointer to the loaded module. |
| 92 | + * \note Currently only one module can be loaded at any given time. |
| 93 | + */ |
| 94 | + virtual void* Load(const std::string& data, const std::string& fmt) = 0; |
| 95 | + /*! |
| 96 | + * \brief Unload a module from device. |
| 97 | + * \param mod Pointer to a loaded module returned by \ref Load. |
| 98 | + */ |
| 99 | + virtual void Unload(void* mod) = 0; |
| 100 | + /*! |
| 101 | + * \brief Find the address of an object in the currently loaded module. |
| 102 | + * \param sym Name of the object. |
| 103 | + * \return Address of the located object, or nullptr if object was |
| 104 | + * not found. |
| 105 | + */ |
| 106 | + virtual void* Resolve(const std::string& sym) = 0; |
| 107 | + /*! |
| 108 | + * \brief Invoke a function on device with given arguments. |
| 109 | + * \param func Address (local to device) of the function to call. |
| 110 | + * \param scalar Pointer to an array of 32-bit values that will be |
| 111 | + * passed via consecutive registers: r0..r5. This array |
| 112 | + * includes dummy values for skipped registers. |
| 113 | + * \param sc_num Number of values in the "scalar" array. |
| 114 | + * \param stack Pointer to an array of 32-bit values that will be |
| 115 | + * passed on the stack. This array includes dummy values |
| 116 | + * for padding. |
| 117 | + * \param st_num Number of values in the "stack" array. |
| 118 | + */ |
| 119 | + virtual void Call(void* func, uint32_t* scalar, unsigned sc_num, uint32_t* stack, |
| 120 | + unsigned st_num) = 0; |
| 121 | + |
| 122 | + virtual ~Device() = 0; |
| 123 | + |
| 124 | + static std::shared_ptr<Device> Global(); |
| 125 | + static bool ValidateDeviceId(decltype(DLDevice::device_id) device_id) { |
| 126 | + // Only supporting a single device for now. |
| 127 | + return device_id == 0; |
| 128 | + } |
| 129 | +}; |
| 130 | + |
| 131 | +} // namespace hexagon |
| 132 | + |
| 133 | +} // namespace runtime |
| 134 | +} // namespace tvm |
| 135 | +#endif // TVM_RUNTIME_HEXAGON_ANDROID_HEXAGON_DEVICE_H_ |
0 commit comments