ARM: tegra: Add Tegra Profiler
[linux-2.6.git] / drivers / misc / tegra-profiler / comm.c
1 /*
2  * drivers/misc/tegra-profiler/comm.c
3  *
4  * Copyright (c) 2013, NVIDIA CORPORATION.  All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <linux/module.h>
20 #include <linux/fs.h>
21 #include <asm/uaccess.h>
22 #include <linux/slab.h>
23 #include <linux/vmalloc.h>
24 #include <linux/miscdevice.h>
25 #include <linux/sched.h>
26
27 #include <linux/tegra_profiler.h>
28
29 #include "comm.h"
30 #include "version.h"
31
32 #define QUADD_SIZE_RB_BUFFER    (0x100000 * 8)  /* 8 MB */
33
34 struct quadd_comm_ctx comm_ctx;
35
36 static inline void *rb_alloc(unsigned long size)
37 {
38         return vmalloc(size);
39 }
40
41 static inline void rb_free(void *addr)
42 {
43         vfree(addr);
44 }
45
46 static void rb_reset(struct quadd_ring_buffer *rb)
47 {
48         rb->pos_read = 0;
49         rb->pos_write = 0;
50         rb->fill_count = 0;
51 }
52
53 static int rb_init(struct quadd_ring_buffer *rb, size_t size)
54 {
55         spin_lock_init(&rb->lock);
56
57         rb->size = size;
58         rb->buf = NULL;
59
60         rb->buf = (char *) rb_alloc(rb->size);
61         if (!rb->buf) {
62                 pr_err("Ring buffer alloc error\n");
63                 return 1;
64         }
65         pr_debug("data buffer size: %u\n", (unsigned int)rb->size);
66
67         rb_reset(rb);
68
69         return 0;
70 }
71
72 static void rb_deinit(struct quadd_ring_buffer *rb)
73 {
74         unsigned long flags;
75
76         spin_lock_irqsave(&rb->lock, flags);
77         if (rb->buf) {
78                 rb_reset(rb);
79
80                 rb_free(rb->buf);
81                 rb->buf = NULL;
82         }
83         spin_unlock_irqrestore(&rb->lock, flags);
84 }
85
86 static __attribute__((unused)) int rb_is_full(struct quadd_ring_buffer *rb)
87 {
88         return rb->fill_count == rb->size;
89 }
90
91 static int rb_is_empty(struct quadd_ring_buffer *rb)
92 {
93         return rb->fill_count == 0;
94 }
95
96 static size_t
97 rb_get_free_space(struct quadd_ring_buffer *rb)
98 {
99         return rb->size - rb->fill_count;
100 }
101
102 static size_t
103 rb_write(struct quadd_ring_buffer *rb, char *data, size_t length)
104 {
105         size_t new_pos_write, chunk1;
106
107         if (length > rb_get_free_space(rb))
108                 return 0;
109
110         new_pos_write = (rb->pos_write + length) % rb->size;
111
112         if (new_pos_write < rb->pos_write) {
113                 chunk1 = rb->size - rb->pos_write;
114                 memcpy(rb->buf + rb->pos_write, data, chunk1);
115                 if (new_pos_write > 0)
116                         memcpy(rb->buf, data + chunk1, new_pos_write);
117         } else {
118                 memcpy(rb->buf + rb->pos_write, data, length);
119         }
120
121         rb->pos_write = new_pos_write;
122         rb->fill_count += length;
123
124         return length;
125 }
126
127 static size_t rb_read_undo(struct quadd_ring_buffer *rb, size_t length)
128 {
129         if (rb_get_free_space(rb) < length)
130                 return 0;
131
132         if (rb->pos_read > length)
133                 rb->pos_read -= length;
134         else
135                 rb->pos_read += rb->size - length;
136
137         rb->fill_count += sizeof(struct quadd_record_data);
138         return length;
139 }
140
141 static size_t rb_read(struct quadd_ring_buffer *rb, char *data, size_t length)
142 {
143         unsigned int new_pos_read, chunk1;
144
145         if (length > rb->fill_count)
146                 return 0;
147
148         new_pos_read = (rb->pos_read + length) % rb->size;
149
150         if (new_pos_read < rb->pos_read) {
151                 chunk1 = rb->size - rb->pos_read;
152                 memcpy(data, rb->buf + rb->pos_read, chunk1);
153                 if (new_pos_read > 0)
154                         memcpy(data + chunk1, rb->buf, new_pos_read);
155         } else {
156                 memcpy(data, rb->buf + rb->pos_read, length);
157         }
158
159         rb->pos_read = new_pos_read;
160         rb->fill_count -= length;
161
162         return length;
163 }
164
165 static size_t
166 rb_read_user(struct quadd_ring_buffer *rb, char __user *data, size_t length)
167 {
168         size_t new_pos_read, chunk1;
169
170         if (length > rb->fill_count)
171                 return 0;
172
173         new_pos_read = (rb->pos_read + length) % rb->size;
174
175         if (new_pos_read < rb->pos_read) {
176                 chunk1 = rb->size - rb->pos_read;
177                 if (copy_to_user(data, rb->buf + rb->pos_read, chunk1)) {
178                         pr_err_once("Error: copy_to_user\n");
179                         return 0;
180                 }
181
182                 if (new_pos_read > 0) {
183                         if (copy_to_user(data + chunk1, rb->buf,
184                                          new_pos_read)) {
185                                 pr_err_once("Error: copy_to_user\n");
186                                 return 0;
187                         }
188                 }
189         } else {
190                 if (copy_to_user(data, rb->buf + rb->pos_read, length)) {
191                         pr_err_once("Error: copy_to_user\n");
192                         return 0;
193                 }
194         }
195
196         rb->pos_read = new_pos_read;
197         rb->fill_count -= length;
198
199         return length;
200 }
201
202 static void
203 write_sample(struct quadd_record_data *sample, void *extra_data,
204              size_t extra_length)
205 {
206         unsigned long flags;
207         struct quadd_ring_buffer *rb = &comm_ctx.rb;
208         int length_sample = sizeof(struct quadd_record_data) + extra_length;
209
210         spin_lock_irqsave(&rb->lock, flags);
211
212         if (length_sample > rb_get_free_space(rb)) {
213                 pr_err_once("Error: Buffer overflowed, skip sample\n");
214                 spin_unlock_irqrestore(&rb->lock, flags);
215                 return;
216         }
217
218         if (!rb_write(rb, (char *)sample, sizeof(struct quadd_record_data))) {
219                 spin_unlock_irqrestore(&rb->lock, flags);
220                 return;
221         }
222
223         if (extra_data && extra_length > 0) {
224                 if (!rb_write(rb, extra_data, extra_length)) {
225                         pr_err_once("Buffer overflowed, skip sample\n");
226                         spin_unlock_irqrestore(&rb->lock, flags);
227                         return;
228                 }
229         }
230         spin_unlock_irqrestore(&rb->lock, flags);
231 }
232
233 static int read_sample(char __user *buffer, size_t max_length)
234 {
235         unsigned long flags;
236         struct quadd_ring_buffer *rb = &comm_ctx.rb;
237         struct quadd_record_data record;
238         size_t length_extra = 0;
239
240         spin_lock_irqsave(&rb->lock, flags);
241
242         if (rb_is_empty(rb)) {
243                 spin_unlock_irqrestore(&rb->lock, flags);
244                 return 0;
245         }
246
247         if (rb->fill_count < sizeof(struct quadd_record_data)) {
248                 pr_err_once("Error: data\n");
249                 spin_unlock_irqrestore(&rb->lock, flags);
250                 return 0;
251         }
252
253         if (!rb_read(rb, (char *)&record, sizeof(struct quadd_record_data))) {
254                 pr_err_once("Error: read sample\n");
255                 spin_unlock_irqrestore(&rb->lock, flags);
256                 return 0;
257         }
258
259         if (record.magic != QUADD_RECORD_MAGIC) {
260                 pr_err_once("Bad magic: %#x\n", record.magic);
261                 spin_unlock_irqrestore(&rb->lock, flags);
262                 return 0;
263         }
264
265         switch (record.record_type) {
266         case QUADD_RECORD_TYPE_SAMPLE:
267                 length_extra = record.sample.callchain_nr *
268                                         sizeof(record.sample.ip);
269                 break;
270
271         case QUADD_RECORD_TYPE_MMAP:
272                 if (record.mmap.filename_length > 0) {
273                         length_extra = record.mmap.filename_length;
274                 } else {
275                         length_extra = 0;
276                         pr_err_once("Error: filename\n");
277                 }
278                 break;
279
280         case QUADD_RECORD_TYPE_DEBUG:
281         case QUADD_RECORD_TYPE_HEADER:
282         case QUADD_RECORD_TYPE_MA:
283                 length_extra = 0;
284                 break;
285
286         case QUADD_RECORD_TYPE_POWER_RATE:
287                 length_extra = record.power_rate.nr_cpus * sizeof(u32);
288                 break;
289
290         case QUADD_RECORD_TYPE_ADDITIONAL_SAMPLE:
291                 length_extra = record.additional_sample.extra_length;
292                 break;
293
294         default:
295                 pr_err_once("Error: Unknown sample: %u\n", record.record_type);
296                 spin_unlock_irqrestore(&rb->lock, flags);
297                 return 0;
298         }
299
300         if (sizeof(struct quadd_record_data) + length_extra > max_length) {
301                 if (!rb_read_undo(rb, sizeof(struct quadd_record_data)))
302                         pr_err_once("Error: rb_read_undo\n");
303                 spin_unlock_irqrestore(&rb->lock, flags);
304                 return 0;
305         }
306
307         if (length_extra > rb_get_free_space(rb)) {
308                 pr_err_once("Error: Incompleted sample\n");
309                 spin_unlock_irqrestore(&rb->lock, flags);
310                 return 0;
311         }
312
313         if (copy_to_user(buffer, &record, sizeof(struct quadd_record_data))) {
314                 pr_err_once("Error: copy_to_user\n");
315                 spin_unlock_irqrestore(&rb->lock, flags);
316                 return 0;
317         }
318
319         if (length_extra > 0) {
320                 if (!rb_read_user(rb, buffer + sizeof(struct quadd_record_data),
321                                   length_extra)) {
322                         pr_err_once("Error: copy_to_user\n");
323                         spin_unlock_irqrestore(&rb->lock, flags);
324                         return 0;
325                 }
326         }
327
328         spin_unlock_irqrestore(&rb->lock, flags);
329         return sizeof(struct quadd_record_data) + length_extra;
330 }
331
332 static void put_sample(struct quadd_record_data *data, char *extra_data,
333                        unsigned int extra_length)
334 {
335         if (!atomic_read(&comm_ctx.active))
336                 return;
337
338         write_sample(data, extra_data, extra_length);
339 }
340
341 static void comm_reset(void)
342 {
343         unsigned long flags;
344
345         pr_debug("Comm reset\n");
346         spin_lock_irqsave(&comm_ctx.rb.lock, flags);
347         rb_reset(&comm_ctx.rb);
348         spin_unlock_irqrestore(&comm_ctx.rb.lock, flags);
349 }
350
351 static struct quadd_comm_data_interface comm_data = {
352         .put_sample = put_sample,
353         .reset = comm_reset,
354 };
355
356 static int check_access_permission(void)
357 {
358         struct task_struct *task;
359
360         if (capable(CAP_SYS_ADMIN))
361                 return 0;
362
363         if (!comm_ctx.params_ok || comm_ctx.process_pid == 0)
364                 return -EACCES;
365
366         rcu_read_lock();
367         task = pid_task(find_vpid(comm_ctx.process_pid), PIDTYPE_PID);
368         rcu_read_unlock();
369         if (!task)
370                 return -EACCES;
371
372         if (current_fsuid() != task_uid(task) &&
373             task_uid(task) != comm_ctx.debug_app_uid) {
374                 pr_err("Permission denied, owner/task uids: %u/%u\n",
375                            current_fsuid(), task_uid(task));
376                 return -EACCES;
377         }
378         return 0;
379 }
380
381 static int device_open(struct inode *inode, struct file *file)
382 {
383         mutex_lock(&comm_ctx.io_mutex);
384         comm_ctx.nr_users++;
385         mutex_unlock(&comm_ctx.io_mutex);
386         return 0;
387 }
388
389 static int device_release(struct inode *inode, struct file *file)
390 {
391         mutex_lock(&comm_ctx.io_mutex);
392         comm_ctx.nr_users--;
393
394         if (comm_ctx.nr_users == 0) {
395                 if (atomic_cmpxchg(&comm_ctx.active, 1, 0)) {
396                         comm_ctx.control->stop();
397                         pr_info("Stop profiling: daemon is closed\n");
398                 }
399         }
400         mutex_unlock(&comm_ctx.io_mutex);
401
402         return 0;
403 }
404
405 static ssize_t
406 device_read(struct file *filp,
407             char __user *buffer,
408             size_t length,
409             loff_t *offset)
410 {
411         int err;
412         size_t was_read = 0, res, samples_counter = 0;
413
414         err = check_access_permission();
415         if (err)
416                 return err;
417
418         mutex_lock(&comm_ctx.io_mutex);
419
420         if (!atomic_read(&comm_ctx.active)) {
421                 mutex_unlock(&comm_ctx.io_mutex);
422                 return -1;
423         }
424
425         while (was_read + sizeof(struct quadd_record_data) < length) {
426                 res = read_sample(buffer + was_read, length - was_read);
427                 if (res == 0)
428                         break;
429
430                 was_read += res;
431                 samples_counter++;
432
433                 if (!atomic_read(&comm_ctx.active))
434                         break;
435         }
436
437         mutex_unlock(&comm_ctx.io_mutex);
438         return was_read;
439 }
440
441 static long
442 device_ioctl(struct file *file,
443              unsigned int ioctl_num,
444              unsigned long ioctl_param)
445 {
446         int err;
447         struct quadd_parameters user_params;
448         struct quadd_comm_cap cap;
449         struct quadd_module_state state;
450         struct quadd_module_version versions;
451         unsigned long flags;
452         struct quadd_ring_buffer *rb = &comm_ctx.rb;
453
454         if (ioctl_num != IOCTL_SETUP &&
455             ioctl_num != IOCTL_GET_CAP &&
456             ioctl_num != IOCTL_GET_STATE &&
457             ioctl_num != IOCTL_GET_VERSION) {
458                 err = check_access_permission();
459                 if (err)
460                         return err;
461         }
462
463         mutex_lock(&comm_ctx.io_mutex);
464
465         switch (ioctl_num) {
466         case IOCTL_SETUP:
467                 if (atomic_read(&comm_ctx.active)) {
468                         pr_err("error: tegra profiler is active\n");
469                         mutex_unlock(&comm_ctx.io_mutex);
470                         return -EBUSY;
471                 }
472
473                 if (copy_from_user(&user_params, (void __user *)ioctl_param,
474                                    sizeof(struct quadd_parameters))) {
475                         pr_err("setup failed\n");
476                         mutex_unlock(&comm_ctx.io_mutex);
477                         return -EFAULT;
478                 }
479
480                 err = comm_ctx.control->set_parameters(&user_params,
481                                                        &comm_ctx.debug_app_uid);
482                 if (err) {
483                         pr_err("error: setup failed\n");
484                         mutex_unlock(&comm_ctx.io_mutex);
485                         return err;
486                 }
487                 comm_ctx.params_ok = 1;
488                 comm_ctx.process_pid = user_params.pids[0];
489
490                 pr_info("setup success: freq/mafreq: %u/%u, backtrace: %d, pid: %d\n",
491                         user_params.freq,
492                         user_params.ma_freq,
493                         user_params.backtrace,
494                         user_params.pids[0]);
495                 break;
496
497         case IOCTL_GET_CAP:
498                 comm_ctx.control->get_capabilities(&cap);
499                 if (copy_to_user((void __user *)ioctl_param, &cap,
500                                  sizeof(struct quadd_comm_cap))) {
501                         pr_err("error: get_capabilities failed\n");
502                         mutex_unlock(&comm_ctx.io_mutex);
503                         return -EFAULT;
504                 }
505                 break;
506
507         case IOCTL_GET_VERSION:
508                 strcpy(versions.branch, QUADD_MODULE_BRANCH);
509                 strcpy(versions.version, QUADD_MODULE_VERSION);
510
511                 versions.samples_version = QUADD_SAMPLES_VERSION;
512                 versions.io_version = QUADD_IO_VERSION;
513
514                 if (copy_to_user((void __user *)ioctl_param, &versions,
515                                  sizeof(struct quadd_module_version))) {
516                         pr_err("error: get version failed\n");
517                         mutex_unlock(&comm_ctx.io_mutex);
518                         return -EFAULT;
519                 }
520                 break;
521
522         case IOCTL_GET_STATE:
523                 comm_ctx.control->get_state(&state);
524
525                 state.buffer_size = QUADD_SIZE_RB_BUFFER;
526
527                 spin_lock_irqsave(&rb->lock, flags);
528                 state.buffer_fill_size =
529                         QUADD_SIZE_RB_BUFFER - rb_get_free_space(rb);
530                 spin_unlock_irqrestore(&rb->lock, flags);
531
532                 if (copy_to_user((void __user *)ioctl_param, &state,
533                                  sizeof(struct quadd_module_state))) {
534                         pr_err("error: get_state failed\n");
535                         mutex_unlock(&comm_ctx.io_mutex);
536                         return -EFAULT;
537                 }
538                 break;
539
540         case IOCTL_START:
541                 if (!atomic_cmpxchg(&comm_ctx.active, 0, 1)) {
542                         if (!comm_ctx.params_ok) {
543                                 pr_err("error: params failed\n");
544                                 atomic_set(&comm_ctx.active, 0);
545                                 mutex_unlock(&comm_ctx.io_mutex);
546                                 return -EFAULT;
547                         }
548
549                         if (comm_ctx.control->start()) {
550                                 pr_err("error: start failed\n");
551                                 atomic_set(&comm_ctx.active, 0);
552                                 mutex_unlock(&comm_ctx.io_mutex);
553                                 return -EFAULT;
554                         }
555                         pr_info("Start profiling success\n");
556                 }
557                 break;
558
559         case IOCTL_STOP:
560                 if (atomic_cmpxchg(&comm_ctx.active, 1, 0)) {
561                         comm_ctx.control->stop();
562                         pr_info("Stop profiling success\n");
563                 }
564                 break;
565
566         default:
567                 pr_err("error: ioctl %u is unsupported in this version of module\n",
568                        ioctl_num);
569                 mutex_unlock(&comm_ctx.io_mutex);
570                 return -EFAULT;
571         }
572         mutex_unlock(&comm_ctx.io_mutex);
573
574         return 0;
575 }
576
577 static void unregister(void)
578 {
579         misc_deregister(comm_ctx.misc_dev);
580         kfree(comm_ctx.misc_dev);
581 }
582
583 static void free_ctx(void)
584 {
585         rb_deinit(&comm_ctx.rb);
586 }
587
588 static const struct file_operations qm_fops = {
589         .read           = device_read,
590         .open           = device_open,
591         .release        = device_release,
592         .unlocked_ioctl = device_ioctl
593 };
594
595 static int comm_init(void)
596 {
597         int res;
598         struct miscdevice *misc_dev;
599         struct quadd_ring_buffer *rb = &comm_ctx.rb;
600
601         misc_dev = kzalloc(sizeof(*misc_dev), GFP_KERNEL);
602         if (!misc_dev) {
603                 pr_err("Error: alloc error\n");
604                 return -ENOMEM;
605         }
606
607         misc_dev->minor = MISC_DYNAMIC_MINOR;
608         misc_dev->name = QUADD_DEVICE_NAME;
609         misc_dev->fops = &qm_fops;
610
611         res = misc_register(misc_dev);
612         if (res < 0) {
613                 pr_err("Error: misc_register %d\n", res);
614                 return res;
615         }
616         comm_ctx.misc_dev = misc_dev;
617
618         mutex_init(&comm_ctx.io_mutex);
619         atomic_set(&comm_ctx.active, 0);
620
621         comm_ctx.params_ok = 0;
622         comm_ctx.process_pid = 0;
623         comm_ctx.nr_users = 0;
624
625         if (rb_init(rb, QUADD_SIZE_RB_BUFFER)) {
626                 free_ctx();
627                 unregister();
628                 return -ENOMEM;
629         }
630
631         return 0;
632 }
633
634 struct quadd_comm_data_interface *
635 quadd_comm_events_init(struct quadd_comm_control_interface *control)
636 {
637         if (comm_init() < 0)
638                 return NULL;
639
640         comm_ctx.control = control;
641         return &comm_data;
642 }
643
644 void quadd_comm_events_exit(void)
645 {
646         mutex_lock(&comm_ctx.io_mutex);
647         unregister();
648         free_ctx();
649         mutex_unlock(&comm_ctx.io_mutex);
650 }