|
| 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 | +#include <algorithm> |
| 21 | + |
| 22 | +#include "hexagon_common.h" |
| 23 | +#include "hexagon_user_dma_descriptors.h" |
| 24 | +#include "hexagon_user_dma_instructions.h" |
| 25 | +#include "hexagon_user_dma_registers.h" |
| 26 | + |
| 27 | +namespace tvm { |
| 28 | +namespace runtime { |
| 29 | +namespace hexagon { |
| 30 | + |
| 31 | +int init_hexagon_user_dma() { |
| 32 | +#if defined(__hexagon__) |
| 33 | + // reset DMA engine |
| 34 | + unsigned int status = dmpause() & DM0_STATUS_MASK; |
| 35 | + if (status != DM0_STATUS_IDLE) { |
| 36 | + return DMA_FAILURE; |
| 37 | + } |
| 38 | +#endif |
| 39 | + return DMA_SUCCESS; |
| 40 | +} |
| 41 | + |
| 42 | +int hexagon_user_dma_1d_sync(void* dst, void* src, uint32_t length) { |
| 43 | +#if defined(__hexagon__) |
| 44 | + static int config_dma = init_hexagon_user_dma(); |
| 45 | + if (config_dma != DMA_SUCCESS) { |
| 46 | + return DMA_FAILURE; |
| 47 | + } |
| 48 | + |
| 49 | + uint64_t src64 = reinterpret_cast<uint64_t>(src); |
| 50 | + // source address limited to 32 bits |
| 51 | + if (src64 > DESC_SRC_MASK) { |
| 52 | + return DMA_FAILURE; |
| 53 | + } |
| 54 | + |
| 55 | + uint64_t dst64 = reinterpret_cast<uint64_t>(dst); |
| 56 | + // destination address limited to 32 bits |
| 57 | + if (dst64 > DESC_DST_MASK) { |
| 58 | + return DMA_FAILURE; |
| 59 | + } |
| 60 | + |
| 61 | + // length limited to 24 bits |
| 62 | + if (length > DESC_LENGTH_MASK) { |
| 63 | + return DMA_FAILURE; |
| 64 | + } |
| 65 | + |
| 66 | + uint32_t src32 = src64 & DESC_SRC_MASK; |
| 67 | + uint32_t dst32 = dst64 & DESC_DST_MASK; |
| 68 | + |
| 69 | + void* dma_desc = nullptr; |
| 70 | + |
| 71 | +#ifdef _WIN32 |
| 72 | + dma_desc = _aligned_malloc(DMA_DESC_2D_SIZE, DMA_DESC_2D_SIZE); |
| 73 | +#else |
| 74 | + int ret = posix_memalign(&dma_desc, DMA_DESC_2D_SIZE, DMA_DESC_2D_SIZE); |
| 75 | + if (ret) { |
| 76 | + return DMA_FAILURE; |
| 77 | + } |
| 78 | +#endif |
| 79 | + |
| 80 | + if (!dma_desc) { |
| 81 | + return DMA_FAILURE; |
| 82 | + } |
| 83 | + |
| 84 | + dma_desc_set_next(dma_desc, DMA_NULL_PTR); |
| 85 | + dma_desc_set_length(dma_desc, length); |
| 86 | + dma_desc_set_desctype(dma_desc, DESC_DESCTYPE_1D); |
| 87 | + dma_desc_set_dstcomp(dma_desc, DESC_COMP_NONE); |
| 88 | + dma_desc_set_srccomp(dma_desc, DESC_COMP_NONE); |
| 89 | + dma_desc_set_bypassdst(dma_desc, DESC_BYPASS_OFF); |
| 90 | + dma_desc_set_bypasssrc(dma_desc, DESC_BYPASS_OFF); |
| 91 | + dma_desc_set_order(dma_desc, DESC_ORDER_ORDER); |
| 92 | + dma_desc_set_dstate(dma_desc, DESC_DSTATE_INCOMPLETE); |
| 93 | + dma_desc_set_src(dma_desc, src32); |
| 94 | + dma_desc_set_dst(dma_desc, dst32); |
| 95 | + |
| 96 | + dmstart(dma_desc); |
| 97 | + unsigned int status = dmwait() & DM0_STATUS_MASK; |
| 98 | + unsigned int done = dma_desc_get_dstate(dma_desc); |
| 99 | + |
| 100 | +#ifdef _WIN32 |
| 101 | + _aligned_free(dma_desc); |
| 102 | +#else |
| 103 | + free(dma_desc); |
| 104 | +#endif |
| 105 | + |
| 106 | + if (status == DM0_STATUS_IDLE && done == DESC_DSTATE_COMPLETE) { |
| 107 | + return DMA_SUCCESS; |
| 108 | + } |
| 109 | + return DMA_FAILURE; |
| 110 | +#else |
| 111 | + memcpy(dst, src, length); |
| 112 | + return DMA_SUCCESS; |
| 113 | +#endif |
| 114 | +} |
| 115 | + |
| 116 | +} // namespace hexagon |
| 117 | +} // namespace runtime |
| 118 | +} // namespace tvm |
0 commit comments