Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / staging / lttng / lib / ringbuffer / frontend.h
1 #ifndef _LIB_RING_BUFFER_FRONTEND_H
2 #define _LIB_RING_BUFFER_FRONTEND_H
3
4 /*
5  * lib/ringbuffer/frontend.h
6  *
7  * Ring Buffer Library Synchronization Header (API).
8  *
9  * Copyright (C) 2005-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; only
14  * version 2.1 of the License.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  *
25  * Author:
26  *      Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
27  *
28  * See ring_buffer_frontend.c for more information on wait-free algorithms.
29  */
30
31 #include <linux/pipe_fs_i.h>
32 #include <linux/rcupdate.h>
33 #include <linux/cpumask.h>
34 #include <linux/module.h>
35 #include <linux/bitops.h>
36 #include <linux/splice.h>
37 #include <linux/string.h>
38 #include <linux/timer.h>
39 #include <linux/sched.h>
40 #include <linux/cache.h>
41 #include <linux/time.h>
42 #include <linux/slab.h>
43 #include <linux/init.h>
44 #include <linux/stat.h>
45 #include <linux/cpu.h>
46 #include <linux/fs.h>
47
48 #include <asm/atomic.h>
49 #include <asm/local.h>
50
51 /* Internal helpers */
52 #include "../../wrapper/ringbuffer/frontend_internal.h"
53
54 /* Buffer creation/removal and setup operations */
55
56 /*
57  * switch_timer_interval is the time interval (in us) to fill sub-buffers with
58  * padding to let readers get those sub-buffers.  Used for live streaming.
59  *
60  * read_timer_interval is the time interval (in us) to wake up pending readers.
61  *
62  * buf_addr is a pointer the the beginning of the preallocated buffer contiguous
63  * address mapping. It is used only by RING_BUFFER_STATIC configuration. It can
64  * be set to NULL for other backends.
65  */
66
67 extern
68 struct channel *channel_create(const struct lib_ring_buffer_config *config,
69                                const char *name, void *priv,
70                                void *buf_addr,
71                                size_t subbuf_size, size_t num_subbuf,
72                                unsigned int switch_timer_interval,
73                                unsigned int read_timer_interval);
74
75 /*
76  * channel_destroy returns the private data pointer. It finalizes all channel's
77  * buffers, waits for readers to release all references, and destroys the
78  * channel.
79  */
80 extern
81 void *channel_destroy(struct channel *chan);
82
83
84 /* Buffer read operations */
85
86 /*
87  * Iteration on channel cpumask needs to issue a read barrier to match the write
88  * barrier in cpu hotplug. It orders the cpumask read before read of per-cpu
89  * buffer data. The per-cpu buffer is never removed by cpu hotplug; teardown is
90  * only performed at channel destruction.
91  */
92 #define for_each_channel_cpu(cpu, chan)                                 \
93         for ((cpu) = -1;                                                \
94                 ({ (cpu) = cpumask_next(cpu, (chan)->backend.cpumask);  \
95                    smp_read_barrier_depends(); (cpu) < nr_cpu_ids; });)
96
97 extern struct lib_ring_buffer *channel_get_ring_buffer(
98                                 const struct lib_ring_buffer_config *config,
99                                 struct channel *chan, int cpu);
100 extern int lib_ring_buffer_open_read(struct lib_ring_buffer *buf);
101 extern void lib_ring_buffer_release_read(struct lib_ring_buffer *buf);
102
103 /*
104  * Read sequence: snapshot, many get_subbuf/put_subbuf, move_consumer.
105  */
106 extern int lib_ring_buffer_snapshot(struct lib_ring_buffer *buf,
107                                     unsigned long *consumed,
108                                     unsigned long *produced);
109 extern void lib_ring_buffer_move_consumer(struct lib_ring_buffer *buf,
110                                           unsigned long consumed_new);
111
112 extern int lib_ring_buffer_get_subbuf(struct lib_ring_buffer *buf,
113                                       unsigned long consumed);
114 extern void lib_ring_buffer_put_subbuf(struct lib_ring_buffer *buf);
115
116 /*
117  * lib_ring_buffer_get_next_subbuf/lib_ring_buffer_put_next_subbuf are helpers
118  * to read sub-buffers sequentially.
119  */
120 static inline int lib_ring_buffer_get_next_subbuf(struct lib_ring_buffer *buf)
121 {
122         int ret;
123
124         ret = lib_ring_buffer_snapshot(buf, &buf->cons_snapshot,
125                                        &buf->prod_snapshot);
126         if (ret)
127                 return ret;
128         ret = lib_ring_buffer_get_subbuf(buf, buf->cons_snapshot);
129         return ret;
130 }
131
132 static inline void lib_ring_buffer_put_next_subbuf(struct lib_ring_buffer *buf)
133 {
134         lib_ring_buffer_put_subbuf(buf);
135         lib_ring_buffer_move_consumer(buf, subbuf_align(buf->cons_snapshot,
136                                                     buf->backend.chan));
137 }
138
139 extern void channel_reset(struct channel *chan);
140 extern void lib_ring_buffer_reset(struct lib_ring_buffer *buf);
141
142 static inline
143 unsigned long lib_ring_buffer_get_offset(const struct lib_ring_buffer_config *config,
144                                          struct lib_ring_buffer *buf)
145 {
146         return v_read(config, &buf->offset);
147 }
148
149 static inline
150 unsigned long lib_ring_buffer_get_consumed(const struct lib_ring_buffer_config *config,
151                                            struct lib_ring_buffer *buf)
152 {
153         return atomic_long_read(&buf->consumed);
154 }
155
156 /*
157  * Must call lib_ring_buffer_is_finalized before reading counters (memory
158  * ordering enforced with respect to trace teardown).
159  */
160 static inline
161 int lib_ring_buffer_is_finalized(const struct lib_ring_buffer_config *config,
162                                  struct lib_ring_buffer *buf)
163 {
164         int finalized = ACCESS_ONCE(buf->finalized);
165         /*
166          * Read finalized before counters.
167          */
168         smp_rmb();
169         return finalized;
170 }
171
172 static inline
173 int lib_ring_buffer_channel_is_finalized(const struct channel *chan)
174 {
175         return chan->finalized;
176 }
177
178 static inline
179 int lib_ring_buffer_channel_is_disabled(const struct channel *chan)
180 {
181         return atomic_read(&chan->record_disabled);
182 }
183
184 static inline
185 unsigned long lib_ring_buffer_get_read_data_size(
186                                 const struct lib_ring_buffer_config *config,
187                                 struct lib_ring_buffer *buf)
188 {
189         return subbuffer_get_read_data_size(config, &buf->backend);
190 }
191
192 static inline
193 unsigned long lib_ring_buffer_get_records_count(
194                                 const struct lib_ring_buffer_config *config,
195                                 struct lib_ring_buffer *buf)
196 {
197         return v_read(config, &buf->records_count);
198 }
199
200 static inline
201 unsigned long lib_ring_buffer_get_records_overrun(
202                                 const struct lib_ring_buffer_config *config,
203                                 struct lib_ring_buffer *buf)
204 {
205         return v_read(config, &buf->records_overrun);
206 }
207
208 static inline
209 unsigned long lib_ring_buffer_get_records_lost_full(
210                                 const struct lib_ring_buffer_config *config,
211                                 struct lib_ring_buffer *buf)
212 {
213         return v_read(config, &buf->records_lost_full);
214 }
215
216 static inline
217 unsigned long lib_ring_buffer_get_records_lost_wrap(
218                                 const struct lib_ring_buffer_config *config,
219                                 struct lib_ring_buffer *buf)
220 {
221         return v_read(config, &buf->records_lost_wrap);
222 }
223
224 static inline
225 unsigned long lib_ring_buffer_get_records_lost_big(
226                                 const struct lib_ring_buffer_config *config,
227                                 struct lib_ring_buffer *buf)
228 {
229         return v_read(config, &buf->records_lost_big);
230 }
231
232 static inline
233 unsigned long lib_ring_buffer_get_records_read(
234                                 const struct lib_ring_buffer_config *config,
235                                 struct lib_ring_buffer *buf)
236 {
237         return v_read(config, &buf->backend.records_read);
238 }
239
240 #endif /* _LIB_RING_BUFFER_FRONTEND_H */