1 /* 2 * 3 * SPDX-License-Identifier: GPL-2.0 4 * 5 * Copyright (C) 2011-2018 ARM or its affiliates 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; version 2. 10 * This program is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * for more details. 14 * You should have received a copy of the GNU General Public License along 15 * with this program; if not, write to the Free Software Foundation, Inc., 16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17 * 18 */ 19 20 #ifndef __SYSTEM_DMA_H__ 21 #define __SYSTEM_DMA_H__ 22 23 #include "acamera_types.h" 24 25 #define SYS_DMA_TO_DEVICE 0 26 #define SYS_DMA_FROM_DEVICE 1 27 28 typedef struct { 29 uint32_t address; 30 uint32_t size; 31 } dma_addr_pair_t; 32 33 typedef struct { 34 void *address; 35 size_t size; 36 } fwmem_addr_pair_t; 37 38 typedef void ( *dma_completion_callback )( void * ); 39 40 41 /** 42 * Initialize dma channel 43 * 44 * This function requests an exclusive dma channel from the system and return a pointer to it 45 * 46 * @param ctx - pointer to dma channel or NULL if error. 47 * 48 * @return 0 - on success or -1 on error 49 */ 50 int32_t system_dma_init( void **ctx ); 51 52 53 /** 54 * Destroy dma channel 55 * 56 * This function destroys previously allocated by system_dma_init channel. 57 * 58 * @param ctx - pointer to dma channel. 59 * 60 * @return 0 - on success or -1 on error 61 */ 62 int32_t system_dma_destroy( void *ctx ); 63 64 65 /** 66 * Copy data from memory to the device asynchronously 67 * 68 * This function copies memory allocated by kmalloc to the device physical memory. 69 * Please note that dst_mem MUST be a kernel virtual pointer. 70 * 71 * @param ctx - pointer to dma channel. 72 * @param dst_mem - virtual pointer to the memory region 73 * @param dev_phy_addr - device physical address 74 * @param size_to_copy - data size to copy in bytes 75 * 76 * @return 0 - on success or -1 on error 77 */ 78 int32_t system_dma_copy_device_to_memory_async( void *ctx, void *dst_mem, uint32_t dev_phy_addr, uint32_t size_to_copy, dma_completion_callback complete_func, void *args ); 79 80 81 /** 82 * Copy data from dev to memory asynchronously 83 * 84 * This function copies device physical memory to memory allocated by kmalloc. 85 * Please note that src_mem MUST be a kernel virtual pointer. 86 * 87 * @param ctx - pointer to dma channel. 88 * @param src_mem - virtual pointer to the memory region 89 * @param dev_phy_addr - device physical address 90 * @param size_to_copy - data size to copy in bytes 91 * 92 * @return 0 - on success or -1 on error 93 */ 94 int32_t system_dma_copy_memory_to_device_async( void *ctx, void *src_mem, uint32_t dev_phy_addr, uint32_t size_to_copy, dma_completion_callback complete_func, void *args ); 95 96 97 /** 98 * Copy data from memory to the device 99 * 100 * This function copies memory allocated by kmalloc to the device physical memory. 101 * Please note that dst_mem MUST be a kernel virtual pointer. 102 * 103 * @param ctx - pointer to dma channel. 104 * @param dst_mem - virtual pointer to the memory region 105 * @param dev_phy_addr - device physical address 106 * @param size_to_copy - data size to copy in bytes 107 * 108 * @return 0 - on success or -1 on error 109 */ 110 int32_t system_dma_copy_device_to_memory( void *ctx, void *dst_mem, uint32_t dev_phy_addr, uint32_t size_to_copy ); 111 112 113 /** 114 * Copy data from dev to memory 115 * 116 * This function copies device physical memory to memory allocated by kmalloc. 117 * Please note that src_mem MUST be a kernel virtual pointer. 118 * 119 * @param ctx - pointer to dma channel. 120 * @param src_mem - virtual pointer to the memory region 121 * @param dev_phy_addr - device physical address 122 * @param size_to_copy - data size to copy in bytes 123 * 124 * @return 0 - on success or -1 on error 125 */ 126 int32_t system_dma_copy_memory_to_device( void *ctx, void *src_mem, uint32_t dev_phy_addr, uint32_t size_to_copy ); 127 128 129 /** 130 * Setup firmware memory for scatter and gather dma feature from fw memory pairs of virtual address and lenght 131 * 132 * 133 * @param ctx - pointer to dma channel data or NULL if error. 134 * 135 * @return 0 - on success or -1 on error 136 */ 137 int32_t system_dma_sg_fwmem_setup( void *ctx, int32_t buff_loc, fwmem_addr_pair_t *fwmem_pair, int32_t addr_pairs, uint32_t fw_ctx_id ); 138 139 /** 140 * Setup isp device memory for scatter and gather dma feature from pairs of dma bus address and lenght 141 * 142 * @param ctx - pointer to dma channel data or NULL if error. 143 * @param buff_loc - points to ping or pong buffer. 144 * @param fwmem_pair - fw memory pairs of virtual address and lenght. 145 * 146 * @return 0 - on success or -1 on error 147 */ 148 int32_t system_dma_sg_device_setup( void *ctx, int32_t buff_loc, dma_addr_pair_t *device_addr_pair, int32_t addr_pairs, uint32_t fw_ctx_id ); 149 150 /** 151 * Needed to unmap the virtual address after dma completes 152 * 153 * @param ctx - pointer to dma channel data or NULL if error. 154 * @param buff_loc - points to ping or pong buffer. 155 * @param device_addr_pair - pairs of dma bus address and lenght. 156 * 157 * @return 0 - on success or -1 on error 158 */ 159 void system_dma_unmap_sg( void *ctx ); 160 161 /** 162 * Setup dma and run it 163 * 164 * @param ctx - pointer to dma channel data or NULL if error. 165 * @param buff_loc - points to ping or pong buffer. 166 * @param direction - to the device or from the device. 167 * @param complete_func - function to be called after dma; ctx will be passed to this function for system_dma_destroy 168 * 169 * @return 0 - on success or -1 on error 170 */ 171 int32_t system_dma_copy_sg( void *ctx, int32_t buff_loc, uint32_t direction, dma_completion_callback complete_func, uint32_t fw_ctx_id ); 172 173 174 #endif // __SYSTEM_DMA_H__ 175