GNU Linux-libre 4.14.251-gnu1
[releases.git] / drivers / staging / media / atomisp / pci / atomisp2 / css2400 / base / circbuf / interface / ia_css_circbuf_desc.h
1 /*
2  * Support for Intel Camera Imaging ISP subsystem.
3  * Copyright (c) 2015, Intel Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  */
14
15 #ifndef _IA_CSS_CIRCBUF_DESC_H_
16 #define _IA_CSS_CIRCBUF_DESC_H_
17
18 #include <type_support.h>
19 #include <math_support.h>
20 #include <storage_class.h>
21 #include <platform_support.h>
22 #include <sp.h>
23 #include "ia_css_circbuf_comm.h"
24 /****************************************************************
25  *
26  * Inline functions.
27  *
28  ****************************************************************/
29 /**
30  * @brief Test if the circular buffer is empty.
31  *
32  * @param cb_desc The pointer to the circular buffer descriptor.
33  *
34  * @return
35  *      - true when it is empty.
36  *      - false when it is not empty.
37  */
38 STORAGE_CLASS_INLINE bool ia_css_circbuf_desc_is_empty(
39                 ia_css_circbuf_desc_t *cb_desc)
40 {
41         OP___assert(cb_desc != NULL);
42         return (cb_desc->end == cb_desc->start);
43 }
44
45 /**
46  * @brief Test if the circular buffer descriptor is full.
47  *
48  * @param cb_desc       The pointer to the circular buffer
49  *                      descriptor.
50  *
51  * @return
52  *      - true when it is full.
53  *      - false when it is not full.
54  */
55 STORAGE_CLASS_INLINE bool ia_css_circbuf_desc_is_full(
56                 ia_css_circbuf_desc_t *cb_desc)
57 {
58         OP___assert(cb_desc != NULL);
59         return (OP_std_modadd(cb_desc->end, 1, cb_desc->size) == cb_desc->start);
60 }
61
62 /**
63  * @brief Initialize the circular buffer descriptor
64  *
65  * @param cb_desc       The pointer circular buffer descriptor
66  * @param size          The size of the circular buffer
67  */
68 STORAGE_CLASS_INLINE void ia_css_circbuf_desc_init(
69         ia_css_circbuf_desc_t *cb_desc,
70         int8_t size)
71 {
72         OP___assert(cb_desc != NULL);
73         cb_desc->size = size;
74 }
75
76 /**
77  * @brief Get a position in the circular buffer descriptor.
78  *
79  * @param cb     The pointer to the circular buffer descriptor.
80  * @param base   The base position.
81  * @param offset The offset.
82  *
83  * @return the position in the circular buffer descriptor.
84  */
85 STORAGE_CLASS_INLINE uint8_t ia_css_circbuf_desc_get_pos_at_offset(
86         ia_css_circbuf_desc_t *cb_desc,
87         uint32_t base,
88         int offset)
89 {
90         uint8_t dest;
91         OP___assert(cb_desc != NULL);
92         OP___assert(cb_desc->size > 0);
93
94         /* step 1: adjust the offset  */
95         while (offset < 0) {
96                 offset += cb_desc->size;
97         }
98
99         /* step 2: shift and round by the upper limit */
100         dest = OP_std_modadd(base, offset, cb_desc->size);
101
102         return dest;
103 }
104
105 /**
106  * @brief Get the offset between two positions in the circular buffer
107  * descriptor.
108  * Get the offset from the source position to the terminal position,
109  * along the direction in which the new elements come in.
110  *
111  * @param cb_desc       The pointer to the circular buffer descriptor.
112  * @param src_pos       The source position.
113  * @param dest_pos      The terminal position.
114  *
115  * @return the offset.
116  */
117 STORAGE_CLASS_INLINE int ia_css_circbuf_desc_get_offset(
118         ia_css_circbuf_desc_t *cb_desc,
119         uint32_t src_pos,
120         uint32_t dest_pos)
121 {
122         int offset;
123         OP___assert(cb_desc != NULL);
124
125         offset = (int)(dest_pos - src_pos);
126         offset += (offset < 0) ? cb_desc->size : 0;
127
128         return offset;
129 }
130
131 /**
132  * @brief Get the number of available elements.
133  *
134  * @param cb_desc The pointer to the circular buffer.
135  *
136  * @return The number of available elements.
137  */
138 STORAGE_CLASS_INLINE uint32_t ia_css_circbuf_desc_get_num_elems(
139                 ia_css_circbuf_desc_t *cb_desc)
140 {
141         int num;
142         OP___assert(cb_desc != NULL);
143
144         num = ia_css_circbuf_desc_get_offset(cb_desc,
145                                 cb_desc->start,
146                                 cb_desc->end);
147
148         return (uint32_t)num;
149 }
150
151 /**
152  * @brief Get the number of free elements.
153  *
154  * @param cb_desc The pointer to the circular buffer descriptor.
155  *
156  * @return: The number of free elements.
157  */
158 STORAGE_CLASS_INLINE uint32_t ia_css_circbuf_desc_get_free_elems(
159                 ia_css_circbuf_desc_t *cb_desc)
160 {
161         uint32_t num;
162         OP___assert(cb_desc != NULL);
163
164         num = ia_css_circbuf_desc_get_offset(cb_desc,
165                                 cb_desc->start,
166                                 cb_desc->end);
167
168         return (cb_desc->size - num);
169 }
170 #endif /*_IA_CSS_CIRCBUF_DESC_H_ */