Kristofer Jonsson | 116a635 | 2020-08-20 17:25:23 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) COPYRIGHT 2020 ARM Limited. All rights reserved. |
| 3 | * |
| 4 | * This program is free software and is provided to you under the terms of the |
| 5 | * GNU General Public License version 2 as published by the Free Software |
| 6 | * Foundation, and any use by you of this program is subject to the terms |
| 7 | * of such GNU licence. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, you can access it online at |
| 16 | * http://www.gnu.org/licenses/gpl-2.0.html. |
| 17 | * |
| 18 | * SPDX-License-Identifier: GPL-2.0-only |
| 19 | */ |
| 20 | |
| 21 | #ifndef ETHOSU_BUFFER_H |
| 22 | #define ETHOSU_BUFFER_H |
| 23 | |
| 24 | /**************************************************************************** |
| 25 | * Includes |
| 26 | ****************************************************************************/ |
| 27 | |
| 28 | #include <linux/kref.h> |
| 29 | #include <linux/types.h> |
| 30 | |
| 31 | /**************************************************************************** |
| 32 | * Types |
| 33 | ****************************************************************************/ |
| 34 | |
| 35 | struct ethosu_device; |
| 36 | struct device; |
| 37 | |
| 38 | /** |
| 39 | * struct ethosu_buffer - Buffer |
| 40 | * @dev: Device |
| 41 | * @file: File |
| 42 | * @kref: Reference counting |
| 43 | * @capacity: Maximum capacity of the buffer |
| 44 | * @offset: Offset to first byte of buffer |
| 45 | * @size: Size of the data in the buffer |
| 46 | * @cpu_addr: Kernel mapped address |
| 47 | * @dma_addr: DMA address |
| 48 | * @dma_addr_orig: Original DMA address before range mapping |
| 49 | * |
| 50 | * 'offset + size' must not be larger than 'capacity'. |
| 51 | */ |
| 52 | struct ethosu_buffer { |
| 53 | struct ethosu_device *edev; |
| 54 | struct file *file; |
| 55 | struct kref kref; |
| 56 | size_t capacity; |
| 57 | size_t offset; |
| 58 | size_t size; |
| 59 | void *cpu_addr; |
| 60 | dma_addr_t dma_addr; |
| 61 | dma_addr_t dma_addr_orig; |
| 62 | }; |
| 63 | |
| 64 | /**************************************************************************** |
| 65 | * Functions |
| 66 | ****************************************************************************/ |
| 67 | |
| 68 | /** |
| 69 | * ethosu_buffer_create() - Create buffer |
| 70 | * |
| 71 | * This function must be called in the context of a user space process. |
| 72 | * |
| 73 | * Return: fd on success, else error code. |
| 74 | */ |
| 75 | int ethosu_buffer_create(struct ethosu_device *edev, |
| 76 | size_t capacity); |
| 77 | |
| 78 | /** |
| 79 | * ethosu_buffer_get_from_fd() - Get buffer handle from fd |
| 80 | * |
| 81 | * This function must be called from a user space context. |
| 82 | * |
| 83 | * Return: Pointer on success, else ERR_PTR. |
| 84 | */ |
| 85 | struct ethosu_buffer *ethosu_buffer_get_from_fd(int fd); |
| 86 | |
| 87 | /** |
| 88 | * ethosu_buffer_get() - Put buffer |
| 89 | */ |
| 90 | void ethosu_buffer_get(struct ethosu_buffer *buf); |
| 91 | |
| 92 | /** |
| 93 | * ethosu_buffer_put() - Put buffer |
| 94 | */ |
| 95 | void ethosu_buffer_put(struct ethosu_buffer *buf); |
| 96 | |
| 97 | /** |
| 98 | * ethosu_buffer_resize() - Resize and validate buffer |
| 99 | * |
| 100 | * Return: 0 on success, else error code. |
| 101 | */ |
| 102 | int ethosu_buffer_resize(struct ethosu_buffer *buf, |
| 103 | size_t size, |
| 104 | size_t offset); |
| 105 | |
| 106 | #endif /* ETHOSU_BUFFER_H */ |