aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/drivers/staging/media/atomisp/pci/base/circbuf/interface/ia_css_circbuf_desc.h
blob: 47c488cec8adf3e951ec62f586999c7d7bd70a1c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
 * Support for Intel Camera Imaging ISP subsystem.
 * Copyright (c) 2015, Intel Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms and conditions of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 */

#ifndef _IA_CSS_CIRCBUF_DESC_H_
#define _IA_CSS_CIRCBUF_DESC_H_

#include <type_support.h>
#include <math_support.h>
#include <platform_support.h>
#include <sp.h>
#include "ia_css_circbuf_comm.h"
/****************************************************************
 *
 * Inline functions.
 *
 ****************************************************************/
/**
 * @brief Test if the circular buffer is empty.
 *
 * @param cb_desc The pointer to the circular buffer descriptor.
 *
 * @return
 *	- true when it is empty.
 *	- false when it is not empty.
 */
static inline bool ia_css_circbuf_desc_is_empty(
    ia_css_circbuf_desc_t *cb_desc)
{
	OP___assert(cb_desc);
	return (cb_desc->end == cb_desc->start);
}

/**
 * @brief Test if the circular buffer descriptor is full.
 *
 * @param cb_desc	The pointer to the circular buffer
 *			descriptor.
 *
 * @return
 *	- true when it is full.
 *	- false when it is not full.
 */
static inline bool ia_css_circbuf_desc_is_full(
    ia_css_circbuf_desc_t *cb_desc)
{
	OP___assert(cb_desc);
	return (OP_std_modadd(cb_desc->end, 1, cb_desc->size) == cb_desc->start);
}

/**
 * @brief Initialize the circular buffer descriptor
 *
 * @param cb_desc	The pointer circular buffer descriptor
 * @param size		The size of the circular buffer
 */
static inline void ia_css_circbuf_desc_init(
    ia_css_circbuf_desc_t *cb_desc,
    int8_t size)
{
	OP___assert(cb_desc);
	cb_desc->size = size;
}

/**
 * @brief Get a position in the circular buffer descriptor.
 *
 * @param cb     The pointer to the circular buffer descriptor.
 * @param base   The base position.
 * @param offset The offset.
 *
 * @return the position in the circular buffer descriptor.
 */
static inline uint8_t ia_css_circbuf_desc_get_pos_at_offset(
    ia_css_circbuf_desc_t *cb_desc,
    u32 base,
    int offset)
{
	u8 dest;

	OP___assert(cb_desc);
	OP___assert(cb_desc->size > 0);

	/* step 1: adjust the offset  */
	while (offset < 0) {
		offset += cb_desc->size;
	}

	/* step 2: shift and round by the upper limit */
	dest = OP_std_modadd(base, offset, cb_desc->size);

	return dest;
}

/**
 * @brief Get the offset between two positions in the circular buffer
 * descriptor.
 * Get the offset from the source position to the terminal position,
 * along the direction in which the new elements come in.
 *
 * @param cb_desc	The pointer to the circular buffer descriptor.
 * @param src_pos	The source position.
 * @param dest_pos	The terminal position.
 *
 * @return the offset.
 */
static inline int ia_css_circbuf_desc_get_offset(
    ia_css_circbuf_desc_t *cb_desc,
    u32 src_pos,
    uint32_t dest_pos)
{
	int offset;

	OP___assert(cb_desc);

	offset = (int)(dest_pos - src_pos);
	offset += (offset < 0) ? cb_desc->size : 0;

	return offset;
}

/**
 * @brief Get the number of available elements.
 *
 * @param cb_desc The pointer to the circular buffer.
 *
 * @return The number of available elements.
 */
static inline uint32_t ia_css_circbuf_desc_get_num_elems(
    ia_css_circbuf_desc_t *cb_desc)
{
	int num;

	OP___assert(cb_desc);

	num = ia_css_circbuf_desc_get_offset(cb_desc,
					     cb_desc->start,
					     cb_desc->end);

	return (uint32_t)num;
}

/**
 * @brief Get the number of free elements.
 *
 * @param cb_desc The pointer to the circular buffer descriptor.
 *
 * @return: The number of free elements.
 */
static inline uint32_t ia_css_circbuf_desc_get_free_elems(
    ia_css_circbuf_desc_t *cb_desc)
{
	u32 num;

	OP___assert(cb_desc);

	num = ia_css_circbuf_desc_get_offset(cb_desc,
					     cb_desc->start,
					     cb_desc->end);

	return (cb_desc->size - num);
}
#endif /*_IA_CSS_CIRCBUF_DESC_H_ */