blob: bc5958c4034d2aa69558b2a02c1bcb2cf84037c9 [file] [log] [blame]
Kristofer Jonsson116a6352020-08-20 17:25:23 +02001/*
Kristofer Jonssond779a082023-01-04 17:09:47 +01002 * Copyright 2020,2022-2023 Arm Limited and/or its affiliates
Kristofer Jonsson116a6352020-08-20 17:25:23 +02003 *
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
35struct ethosu_device;
36struct 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 */
52struct ethosu_buffer {
Kristofer Jonssonec477042023-01-20 13:38:13 +010053 struct device *dev;
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;
Kristofer Jonsson116a6352020-08-20 17:25:23 +020061};
62
63/****************************************************************************
64 * Functions
65 ****************************************************************************/
66
67/**
68 * ethosu_buffer_create() - Create buffer
69 *
70 * This function must be called in the context of a user space process.
71 *
72 * Return: fd on success, else error code.
73 */
Kristofer Jonssonec477042023-01-20 13:38:13 +010074int ethosu_buffer_create(struct device *dev,
Kristofer Jonsson116a6352020-08-20 17:25:23 +020075 size_t capacity);
76
77/**
78 * ethosu_buffer_get_from_fd() - Get buffer handle from fd
79 *
80 * This function must be called from a user space context.
81 *
82 * Return: Pointer on success, else ERR_PTR.
83 */
84struct ethosu_buffer *ethosu_buffer_get_from_fd(int fd);
85
86/**
87 * ethosu_buffer_get() - Put buffer
88 */
89void ethosu_buffer_get(struct ethosu_buffer *buf);
90
91/**
92 * ethosu_buffer_put() - Put buffer
93 */
94void ethosu_buffer_put(struct ethosu_buffer *buf);
95
96/**
97 * ethosu_buffer_resize() - Resize and validate buffer
98 *
99 * Return: 0 on success, else error code.
100 */
101int ethosu_buffer_resize(struct ethosu_buffer *buf,
102 size_t size,
103 size_t offset);
104
105#endif /* ETHOSU_BUFFER_H */