touchscreen:add calibration for touchscreen ili2102 to support it's APK
[firefly-linux-kernel-4.4.55.git] / drivers / oprofile / cpu_buffer.c
1 /**
2  * @file cpu_buffer.c
3  *
4  * @remark Copyright 2002-2009 OProfile authors
5  * @remark Read the file COPYING
6  *
7  * @author John Levon <levon@movementarian.org>
8  * @author Barry Kasindorf <barry.kasindorf@amd.com>
9  * @author Robert Richter <robert.richter@amd.com>
10  *
11  * Each CPU has a local buffer that stores PC value/event
12  * pairs. We also log context switches when we notice them.
13  * Eventually each CPU's buffer is processed into the global
14  * event buffer by sync_buffer().
15  *
16  * We use a local buffer for two reasons: an NMI or similar
17  * interrupt cannot synchronise, and high sampling rates
18  * would lead to catastrophic global synchronisation if
19  * a global buffer was used.
20  */
21
22 #include <linux/sched.h>
23 #include <linux/oprofile.h>
24 #include <linux/errno.h>
25
26 #include "event_buffer.h"
27 #include "cpu_buffer.h"
28 #include "buffer_sync.h"
29 #include "oprof.h"
30
31 #define OP_BUFFER_FLAGS 0
32
33 static struct ring_buffer *op_ring_buffer;
34 DEFINE_PER_CPU(struct oprofile_cpu_buffer, cpu_buffer);
35
36 static void wq_sync_buffer(struct work_struct *work);
37
38 #define DEFAULT_TIMER_EXPIRE (HZ / 10)
39 static int work_enabled;
40
41 unsigned long oprofile_get_cpu_buffer_size(void)
42 {
43         return oprofile_cpu_buffer_size;
44 }
45
46 void oprofile_cpu_buffer_inc_smpl_lost(void)
47 {
48         struct oprofile_cpu_buffer *cpu_buf
49                 = &__get_cpu_var(cpu_buffer);
50
51         cpu_buf->sample_lost_overflow++;
52 }
53
54 void free_cpu_buffers(void)
55 {
56         if (op_ring_buffer)
57                 ring_buffer_free(op_ring_buffer);
58         op_ring_buffer = NULL;
59 }
60
61 #define RB_EVENT_HDR_SIZE 4
62
63 int alloc_cpu_buffers(void)
64 {
65         int i;
66
67         unsigned long buffer_size = oprofile_cpu_buffer_size;
68         unsigned long byte_size = buffer_size * (sizeof(struct op_sample) +
69                                                  RB_EVENT_HDR_SIZE);
70
71         op_ring_buffer = ring_buffer_alloc(byte_size, OP_BUFFER_FLAGS);
72         if (!op_ring_buffer)
73                 goto fail;
74
75         for_each_possible_cpu(i) {
76                 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
77
78                 b->last_task = NULL;
79                 b->last_is_kernel = -1;
80                 b->tracing = 0;
81                 b->buffer_size = buffer_size;
82                 b->sample_received = 0;
83                 b->sample_lost_overflow = 0;
84                 b->backtrace_aborted = 0;
85                 b->sample_invalid_eip = 0;
86                 b->cpu = i;
87                 INIT_DELAYED_WORK(&b->work, wq_sync_buffer);
88         }
89         return 0;
90
91 fail:
92         free_cpu_buffers();
93         return -ENOMEM;
94 }
95
96 void start_cpu_work(void)
97 {
98         int i;
99
100         work_enabled = 1;
101
102         for_each_online_cpu(i) {
103                 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
104
105                 /*
106                  * Spread the work by 1 jiffy per cpu so they dont all
107                  * fire at once.
108                  */
109                 schedule_delayed_work_on(i, &b->work, DEFAULT_TIMER_EXPIRE + i);
110         }
111 }
112
113 void end_cpu_work(void)
114 {
115         int i;
116
117         work_enabled = 0;
118
119         for_each_online_cpu(i) {
120                 struct oprofile_cpu_buffer *b = &per_cpu(cpu_buffer, i);
121
122                 cancel_delayed_work(&b->work);
123         }
124 }
125
126 /*
127  * This function prepares the cpu buffer to write a sample.
128  *
129  * Struct op_entry is used during operations on the ring buffer while
130  * struct op_sample contains the data that is stored in the ring
131  * buffer. Struct entry can be uninitialized. The function reserves a
132  * data array that is specified by size. Use
133  * op_cpu_buffer_write_commit() after preparing the sample. In case of
134  * errors a null pointer is returned, otherwise the pointer to the
135  * sample.
136  *
137  */
138 struct op_sample
139 *op_cpu_buffer_write_reserve(struct op_entry *entry, unsigned long size)
140 {
141         entry->event = ring_buffer_lock_reserve
142                 (op_ring_buffer, sizeof(struct op_sample) +
143                  size * sizeof(entry->sample->data[0]));
144         if (!entry->event)
145                 return NULL;
146         entry->sample = ring_buffer_event_data(entry->event);
147         entry->size = size;
148         entry->data = entry->sample->data;
149
150         return entry->sample;
151 }
152
153 int op_cpu_buffer_write_commit(struct op_entry *entry)
154 {
155         return ring_buffer_unlock_commit(op_ring_buffer, entry->event);
156 }
157
158 struct op_sample *op_cpu_buffer_read_entry(struct op_entry *entry, int cpu)
159 {
160         struct ring_buffer_event *e;
161         e = ring_buffer_consume(op_ring_buffer, cpu, NULL);
162         if (!e)
163                 return NULL;
164
165         entry->event = e;
166         entry->sample = ring_buffer_event_data(e);
167         entry->size = (ring_buffer_event_length(e) - sizeof(struct op_sample))
168                 / sizeof(entry->sample->data[0]);
169         entry->data = entry->sample->data;
170         return entry->sample;
171 }
172
173 unsigned long op_cpu_buffer_entries(int cpu)
174 {
175         return ring_buffer_entries_cpu(op_ring_buffer, cpu);
176 }
177
178 static int
179 op_add_code(struct oprofile_cpu_buffer *cpu_buf, unsigned long backtrace,
180             int is_kernel, struct task_struct *task)
181 {
182         struct op_entry entry;
183         struct op_sample *sample;
184         unsigned long flags;
185         int size;
186
187         flags = 0;
188
189         if (backtrace)
190                 flags |= TRACE_BEGIN;
191
192         /* notice a switch from user->kernel or vice versa */
193         is_kernel = !!is_kernel;
194         if (cpu_buf->last_is_kernel != is_kernel) {
195                 cpu_buf->last_is_kernel = is_kernel;
196                 flags |= KERNEL_CTX_SWITCH;
197                 if (is_kernel)
198                         flags |= IS_KERNEL;
199         }
200
201         /* notice a task switch */
202         if (cpu_buf->last_task != task) {
203                 cpu_buf->last_task = task;
204                 flags |= USER_CTX_SWITCH;
205         }
206
207         if (!flags)
208                 /* nothing to do */
209                 return 0;
210
211         if (flags & USER_CTX_SWITCH)
212                 size = 1;
213         else
214                 size = 0;
215
216         sample = op_cpu_buffer_write_reserve(&entry, size);
217         if (!sample)
218                 return -ENOMEM;
219
220         sample->eip = ESCAPE_CODE;
221         sample->event = flags;
222
223         if (size)
224                 op_cpu_buffer_add_data(&entry, (unsigned long)task);
225
226         op_cpu_buffer_write_commit(&entry);
227
228         return 0;
229 }
230
231 static inline int
232 op_add_sample(struct oprofile_cpu_buffer *cpu_buf,
233               unsigned long pc, unsigned long event)
234 {
235         struct op_entry entry;
236         struct op_sample *sample;
237
238         sample = op_cpu_buffer_write_reserve(&entry, 0);
239         if (!sample)
240                 return -ENOMEM;
241
242         sample->eip = pc;
243         sample->event = event;
244
245         return op_cpu_buffer_write_commit(&entry);
246 }
247
248 /*
249  * This must be safe from any context.
250  *
251  * is_kernel is needed because on some architectures you cannot
252  * tell if you are in kernel or user space simply by looking at
253  * pc. We tag this in the buffer by generating kernel enter/exit
254  * events whenever is_kernel changes
255  */
256 static int
257 log_sample(struct oprofile_cpu_buffer *cpu_buf, unsigned long pc,
258            unsigned long backtrace, int is_kernel, unsigned long event)
259 {
260         cpu_buf->sample_received++;
261
262         if (pc == ESCAPE_CODE) {
263                 cpu_buf->sample_invalid_eip++;
264                 return 0;
265         }
266
267         if (op_add_code(cpu_buf, backtrace, is_kernel, current))
268                 goto fail;
269
270         if (op_add_sample(cpu_buf, pc, event))
271                 goto fail;
272
273         return 1;
274
275 fail:
276         cpu_buf->sample_lost_overflow++;
277         return 0;
278 }
279
280 static inline void oprofile_begin_trace(struct oprofile_cpu_buffer *cpu_buf)
281 {
282         cpu_buf->tracing = 1;
283 }
284
285 static inline void oprofile_end_trace(struct oprofile_cpu_buffer *cpu_buf)
286 {
287         cpu_buf->tracing = 0;
288 }
289
290 static inline void
291 __oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
292                           unsigned long event, int is_kernel)
293 {
294         struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
295         unsigned long backtrace = oprofile_backtrace_depth;
296
297         /*
298          * if log_sample() fail we can't backtrace since we lost the
299          * source of this event
300          */
301         if (!log_sample(cpu_buf, pc, backtrace, is_kernel, event))
302                 /* failed */
303                 return;
304
305         if (!backtrace)
306                 return;
307
308         oprofile_begin_trace(cpu_buf);
309         oprofile_ops.backtrace(regs, backtrace);
310         oprofile_end_trace(cpu_buf);
311 }
312
313 void oprofile_add_ext_sample(unsigned long pc, struct pt_regs * const regs,
314                              unsigned long event, int is_kernel)
315 {
316         __oprofile_add_ext_sample(pc, regs, event, is_kernel);
317 }
318
319 void oprofile_add_sample(struct pt_regs * const regs, unsigned long event)
320 {
321         int is_kernel = !user_mode(regs);
322         unsigned long pc = profile_pc(regs);
323
324         __oprofile_add_ext_sample(pc, regs, event, is_kernel);
325 }
326
327 /*
328  * Add samples with data to the ring buffer.
329  *
330  * Use oprofile_add_data(&entry, val) to add data and
331  * oprofile_write_commit(&entry) to commit the sample.
332  */
333 void
334 oprofile_write_reserve(struct op_entry *entry, struct pt_regs * const regs,
335                        unsigned long pc, int code, int size)
336 {
337         struct op_sample *sample;
338         int is_kernel = !user_mode(regs);
339         struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
340
341         cpu_buf->sample_received++;
342
343         /* no backtraces for samples with data */
344         if (op_add_code(cpu_buf, 0, is_kernel, current))
345                 goto fail;
346
347         sample = op_cpu_buffer_write_reserve(entry, size + 2);
348         if (!sample)
349                 goto fail;
350         sample->eip = ESCAPE_CODE;
351         sample->event = 0;              /* no flags */
352
353         op_cpu_buffer_add_data(entry, code);
354         op_cpu_buffer_add_data(entry, pc);
355
356         return;
357
358 fail:
359         entry->event = NULL;
360         cpu_buf->sample_lost_overflow++;
361 }
362
363 int oprofile_add_data(struct op_entry *entry, unsigned long val)
364 {
365         if (!entry->event)
366                 return 0;
367         return op_cpu_buffer_add_data(entry, val);
368 }
369
370 int oprofile_add_data64(struct op_entry *entry, u64 val)
371 {
372         if (!entry->event)
373                 return 0;
374         if (op_cpu_buffer_get_size(entry) < 2)
375                 /*
376                  * the function returns 0 to indicate a too small
377                  * buffer, even if there is some space left
378                  */
379                 return 0;
380         if (!op_cpu_buffer_add_data(entry, (u32)val))
381                 return 0;
382         return op_cpu_buffer_add_data(entry, (u32)(val >> 32));
383 }
384
385 int oprofile_write_commit(struct op_entry *entry)
386 {
387         if (!entry->event)
388                 return -EINVAL;
389         return op_cpu_buffer_write_commit(entry);
390 }
391
392 void oprofile_add_pc(unsigned long pc, int is_kernel, unsigned long event)
393 {
394         struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
395         log_sample(cpu_buf, pc, 0, is_kernel, event);
396 }
397
398 void oprofile_add_trace(unsigned long pc)
399 {
400         struct oprofile_cpu_buffer *cpu_buf = &__get_cpu_var(cpu_buffer);
401
402         if (!cpu_buf->tracing)
403                 return;
404
405         /*
406          * broken frame can give an eip with the same value as an
407          * escape code, abort the trace if we get it
408          */
409         if (pc == ESCAPE_CODE)
410                 goto fail;
411
412         if (op_add_sample(cpu_buf, pc, 0))
413                 goto fail;
414
415         return;
416 fail:
417         cpu_buf->tracing = 0;
418         cpu_buf->backtrace_aborted++;
419         return;
420 }
421
422 /*
423  * This serves to avoid cpu buffer overflow, and makes sure
424  * the task mortuary progresses
425  *
426  * By using schedule_delayed_work_on and then schedule_delayed_work
427  * we guarantee this will stay on the correct cpu
428  */
429 static void wq_sync_buffer(struct work_struct *work)
430 {
431         struct oprofile_cpu_buffer *b =
432                 container_of(work, struct oprofile_cpu_buffer, work.work);
433         if (b->cpu != smp_processor_id()) {
434                 printk(KERN_DEBUG "WQ on CPU%d, prefer CPU%d\n",
435                        smp_processor_id(), b->cpu);
436
437                 if (!cpu_online(b->cpu)) {
438                         cancel_delayed_work(&b->work);
439                         return;
440                 }
441         }
442         sync_buffer(b->cpu);
443
444         /* don't re-add the work if we're shutting down */
445         if (work_enabled)
446                 schedule_delayed_work(&b->work, DEFAULT_TIMER_EXPIRE);
447 }