Merge remote-tracking branch 'origin/upstream/linux-linaro-lsk-v3.10-android' into...
[firefly-linux-kernel-4.4.55.git] / drivers / gator / gator_main.c
index 46fe31d2505dbe4f49cfbfaba3c127e914b9d190..19f51c7cd8eea7803ab219e7fa3870761740da71 100644 (file)
@@ -8,7 +8,8 @@
  */
 
 // This version must match the gator daemon version
-static unsigned long gator_protocol_version = 14;
+#define PROTOCOL_VERSION 17
+static unsigned long gator_protocol_version = PROTOCOL_VERSION;
 
 #include <linux/slab.h>
 #include <linux/cpu.h>
@@ -22,16 +23,20 @@ static unsigned long gator_protocol_version = 14;
 #include <linux/module.h>
 #include <linux/perf_event.h>
 #include <linux/utsname.h>
+#include <linux/kthread.h>
 #include <asm/stacktrace.h>
 #include <asm/uaccess.h>
 
 #include "gator.h"
-#include "gator_events.h"
 
 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 32)
 #error kernels prior to 2.6.32 are not supported
 #endif
 
+#if defined(MODULE) && !defined(CONFIG_MODULES)
+#error Cannot build a module against a kernel that does not support modules. To resolve, either rebuild the kernel to support modules or build gator as part of the kernel.
+#endif
+
 #if !defined(CONFIG_GENERIC_TRACER) && !defined(CONFIG_TRACING)
 #error gator requires the kernel to have CONFIG_GENERIC_TRACER or CONFIG_TRACING defined
 #endif
@@ -44,15 +49,15 @@ static unsigned long gator_protocol_version = 14;
 #error gator requires the kernel to have CONFIG_HIGH_RES_TIMERS defined to support PC sampling
 #endif
 
-#if defined(__arm__) && defined(CONFIG_SMP) && !defined(CONFIG_LOCAL_TIMERS)
+#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 12, 0) && defined(__arm__) && defined(CONFIG_SMP) && !defined(CONFIG_LOCAL_TIMERS)
 #error gator requires the kernel to have CONFIG_LOCAL_TIMERS defined on SMP systems
 #endif
 
 #if (GATOR_PERF_SUPPORT) && (!(GATOR_PERF_PMU_SUPPORT))
 #ifndef CONFIG_PERF_EVENTS
-#error gator requires the kernel to have CONFIG_PERF_EVENTS defined to support pmu hardware counters
+#warning gator requires the kernel to have CONFIG_PERF_EVENTS defined to support pmu hardware counters
 #elif !defined CONFIG_HW_PERF_EVENTS
-#error gator requires the kernel to have CONFIG_HW_PERF_EVENTS defined to support pmu hardware counters
+#warning gator requires the kernel to have CONFIG_HW_PERF_EVENTS defined to support pmu hardware counters
 #endif
 #endif
 
@@ -64,13 +69,13 @@ static unsigned long gator_protocol_version = 14;
 #define NAME_BUFFER_SIZE          (64*1024)
 #define COUNTER_BUFFER_SIZE       (64*1024)    // counters have the core as part of the data and the core value in the frame header may be discarded
 #define BLOCK_COUNTER_BUFFER_SIZE (128*1024)
-#define ANNOTATE_BUFFER_SIZE      (64*1024)    // annotate counters have the core as part of the data and the core value in the frame header may be discarded
+#define ANNOTATE_BUFFER_SIZE      (128*1024)   // annotate counters have the core as part of the data and the core value in the frame header may be discarded
 #define SCHED_TRACE_BUFFER_SIZE   (128*1024)
 #define GPU_TRACE_BUFFER_SIZE     (64*1024)    // gpu trace counters have the core as part of the data and the core value in the frame header may be discarded
 #define IDLE_BUFFER_SIZE          (32*1024)    // idle counters have the core as part of the data and the core value in the frame header may be discarded
 
 #define NO_COOKIE      0U
-#define INVALID_COOKIE ~0U
+#define UNRESOLVED_COOKIE ~0U
 
 #define FRAME_SUMMARY       1
 #define FRAME_BACKTRACE     2
@@ -87,12 +92,14 @@ static unsigned long gator_protocol_version = 14;
 #define MESSAGE_COOKIE      1
 #define MESSAGE_THREAD_NAME 2
 #define HRTIMER_CORE_NAME   3
+#define MESSAGE_LINK        4
 
 #define MESSAGE_GPU_START 1
 #define MESSAGE_GPU_STOP  2
 
 #define MESSAGE_SCHED_SWITCH 1
 #define MESSAGE_SCHED_EXIT   2
+#define MESSAGE_SCHED_START  3
 
 #define MESSAGE_IDLE_ENTER 1
 #define MESSAGE_IDLE_EXIT 2
@@ -135,6 +142,7 @@ static u64 gator_live_rate;
 
 static unsigned long gator_started;
 static u64 gator_monotonic_started;
+static u64 gator_hibernate_time;
 static unsigned long gator_buffer_opened;
 static unsigned long gator_timer_count;
 static unsigned long gator_response_type;
@@ -146,6 +154,8 @@ bool event_based_sampling;
 static DECLARE_WAIT_QUEUE_HEAD(gator_buffer_wait);
 static DECLARE_WAIT_QUEUE_HEAD(gator_annotate_wait);
 static struct timer_list gator_buffer_wake_up_timer;
+static bool gator_buffer_wake_stop;
+static struct task_struct *gator_buffer_wake_thread;
 static LIST_HEAD(gator_events);
 
 static DEFINE_PER_CPU(u64, last_timestamp);
@@ -167,7 +177,7 @@ static void gator_buffer_write_packed_int64(int cpu, int buftype, long long x);
 static void gator_buffer_write_bytes(int cpu, int buftype, const char *x, int len);
 static void gator_buffer_write_string(int cpu, int buftype, const char *x);
 static void gator_add_trace(int cpu, unsigned long address);
-static void gator_add_sample(int cpu, struct pt_regs *const regs);
+static void gator_add_sample(int cpu, struct pt_regs *const regs, u64 time);
 static u64 gator_get_time(void);
 
 // Size of the buffer, must be a power of 2. Effectively constant, set in gator_op_setup.
@@ -185,11 +195,36 @@ static DEFINE_PER_CPU(int[NUM_GATOR_BUFS], gator_buffer_commit);
 static DEFINE_PER_CPU(int[NUM_GATOR_BUFS], buffer_space_available);
 // The buffer. Allocated in gator_op_setup
 static DEFINE_PER_CPU(char *[NUM_GATOR_BUFS], gator_buffer);
-
-#if GATOR_LIVE
 // The time after which the buffer should be committed for live display
 static DEFINE_PER_CPU(u64, gator_buffer_commit_time);
-#endif
+
+// List of all gator events - new events must be added to this list
+#define GATOR_EVENTS_LIST \
+       GATOR_EVENT(gator_events_armv6_init) \
+       GATOR_EVENT(gator_events_armv7_init) \
+       GATOR_EVENT(gator_events_block_init) \
+       GATOR_EVENT(gator_events_ccn504_init) \
+       GATOR_EVENT(gator_events_irq_init) \
+       GATOR_EVENT(gator_events_l2c310_init) \
+       GATOR_EVENT(gator_events_mali_init) \
+       GATOR_EVENT(gator_events_mali_t6xx_hw_init) \
+       GATOR_EVENT(gator_events_mali_t6xx_init) \
+       GATOR_EVENT(gator_events_meminfo_init) \
+       GATOR_EVENT(gator_events_mmapped_init) \
+       GATOR_EVENT(gator_events_net_init) \
+       GATOR_EVENT(gator_events_perf_pmu_init) \
+       GATOR_EVENT(gator_events_sched_init) \
+       GATOR_EVENT(gator_events_scorpion_init) \
+
+#define GATOR_EVENT(EVENT_INIT) __weak int EVENT_INIT(void);
+GATOR_EVENTS_LIST
+#undef GATOR_EVENT
+
+static int (*gator_events_list[])(void) = {
+#define GATOR_EVENT(EVENT_INIT) EVENT_INIT,
+GATOR_EVENTS_LIST
+#undef GATOR_EVENT
+};
 
 /******************************************************************************
  * Application Includes
@@ -198,11 +233,11 @@ static DEFINE_PER_CPU(u64, gator_buffer_commit_time);
 #include "gator_hrtimer_perf.c"
 #include "gator_hrtimer_gator.c"
 #include "gator_cookies.c"
+#include "gator_annotate.c"
 #include "gator_trace_sched.c"
 #include "gator_trace_power.c"
 #include "gator_trace_gpu.c"
 #include "gator_backtrace.c"
-#include "gator_annotate.c"
 #include "gator_fs.c"
 #include "gator_pack.c"
 
@@ -215,24 +250,28 @@ const struct gator_cpu gator_cpus[] = {
                .cpuid = ARM1136,
                .core_name = "ARM1136",
                .pmnc_name = "ARM_ARM11",
+               .dt_name = "arm,arm1136",
                .pmnc_counters = 3,
        },
        {
                .cpuid = ARM1156,
                .core_name = "ARM1156",
                .pmnc_name = "ARM_ARM11",
+               .dt_name = "arm,arm1156",
                .pmnc_counters = 3,
        },
        {
                .cpuid = ARM1176,
                .core_name = "ARM1176",
                .pmnc_name = "ARM_ARM11",
+               .dt_name = "arm,arm1176",
                .pmnc_counters = 3,
        },
        {
                .cpuid = ARM11MPCORE,
                .core_name = "ARM11MPCore",
                .pmnc_name = "ARM_ARM11MPCore",
+               .dt_name = "arm,arm11mpcore",
                .pmnc_counters = 3,
        },
        {
@@ -240,6 +279,7 @@ const struct gator_cpu gator_cpus[] = {
                .core_name = "Cortex-A5",
                .pmu_name = "ARMv7_Cortex_A5",
                .pmnc_name = "ARM_Cortex-A5",
+               .dt_name = "arm,cortex-a5",
                .pmnc_counters = 2,
        },
        {
@@ -247,6 +287,7 @@ const struct gator_cpu gator_cpus[] = {
                .core_name = "Cortex-A7",
                .pmu_name = "ARMv7_Cortex_A7",
                .pmnc_name = "ARM_Cortex-A7",
+               .dt_name = "arm,cortex-a7",
                .pmnc_counters = 4,
        },
        {
@@ -254,6 +295,7 @@ const struct gator_cpu gator_cpus[] = {
                .core_name = "Cortex-A8",
                .pmu_name = "ARMv7_Cortex_A8",
                .pmnc_name = "ARM_Cortex-A8",
+               .dt_name = "arm,cortex-a8",
                .pmnc_counters = 4,
        },
        {
@@ -261,6 +303,15 @@ const struct gator_cpu gator_cpus[] = {
                .core_name = "Cortex-A9",
                .pmu_name = "ARMv7_Cortex_A9",
                .pmnc_name = "ARM_Cortex-A9",
+               .dt_name = "arm,cortex-a9",
+               .pmnc_counters = 6,
+       },
+       {
+               .cpuid = CORTEX_A12,
+               .core_name = "Cortex-A12",
+               .pmu_name = "ARMv7_Cortex_A12",
+               .pmnc_name = "ARM_Cortex-A12",
+               .dt_name = "arm,cortex-a12",
                .pmnc_counters = 6,
        },
        {
@@ -268,6 +319,7 @@ const struct gator_cpu gator_cpus[] = {
                .core_name = "Cortex-A15",
                .pmu_name = "ARMv7_Cortex_A15",
                .pmnc_name = "ARM_Cortex-A15",
+               .dt_name = "arm,cortex-a15",
                .pmnc_counters = 6,
        },
        {
@@ -304,12 +356,14 @@ const struct gator_cpu gator_cpus[] = {
                .cpuid = CORTEX_A53,
                .core_name = "Cortex-A53",
                .pmnc_name = "ARM_Cortex-A53",
+               .dt_name = "arm,cortex-a53",
                .pmnc_counters = 6,
        },
        {
                .cpuid = CORTEX_A57,
                .core_name = "Cortex-A57",
                .pmnc_name = "ARM_Cortex-A57",
+               .dt_name = "arm,cortex-a57",
                .pmnc_counters = 6,
        },
        {
@@ -375,6 +429,21 @@ static void gator_buffer_wake_up(unsigned long data)
        wake_up(&gator_buffer_wait);
 }
 
+static int gator_buffer_wake_func(void *data)
+{
+       while (!gator_buffer_wake_stop) {
+               set_current_state(TASK_INTERRUPTIBLE);
+               schedule();
+               if (gator_buffer_wake_stop) {
+                       break;
+               }
+
+               gator_buffer_wake_up(0);
+       }
+
+       return 0;
+}
+
 /******************************************************************************
  * Commit interface
  ******************************************************************************/
@@ -491,18 +560,23 @@ static void gator_commit_buffer(int cpu, int buftype, u64 time)
 
        per_cpu(gator_buffer_commit, cpu)[buftype] = per_cpu(gator_buffer_write, cpu)[buftype];
 
-#if GATOR_LIVE
        if (gator_live_rate > 0) {
                while (time > per_cpu(gator_buffer_commit_time, cpu)) {
                        per_cpu(gator_buffer_commit_time, cpu) += gator_live_rate;
                }
        }
-#endif
 
        marshal_frame(cpu, buftype);
 
        // had to delay scheduling work as attempting to schedule work during the context switch is illegal in kernel versions 3.5 and greater
-       mod_timer(&gator_buffer_wake_up_timer, jiffies + 1);
+       if (per_cpu(in_scheduler_context, cpu)) {
+#ifndef CONFIG_PREEMPT_RT_FULL
+               // mod_timer can not be used in interrupt context in RT-Preempt full
+               mod_timer(&gator_buffer_wake_up_timer, jiffies + 1);
+#endif
+       } else {
+               wake_up_process(gator_buffer_wake_thread);
+       }
 }
 
 static void buffer_check(int cpu, int buftype, u64 time)
@@ -521,14 +595,14 @@ static void gator_add_trace(int cpu, unsigned long address)
        off_t offset = 0;
        unsigned long cookie = get_address_cookie(cpu, current, address & ~1, &offset);
 
-       if (cookie == NO_COOKIE || cookie == INVALID_COOKIE) {
+       if (cookie == NO_COOKIE || cookie == UNRESOLVED_COOKIE) {
                offset = address;
        }
 
        marshal_backtrace(offset & ~1, cookie);
 }
 
-static void gator_add_sample(int cpu, struct pt_regs *const regs)
+static void gator_add_sample(int cpu, struct pt_regs *const regs, u64 time)
 {
        bool inKernel;
        unsigned long exec_cookie;
@@ -539,7 +613,7 @@ static void gator_add_sample(int cpu, struct pt_regs *const regs)
        inKernel = !user_mode(regs);
        exec_cookie = get_exec_cookie(cpu, current);
 
-       if (!marshal_backtrace_header(exec_cookie, current->tgid, current->pid, inKernel))
+       if (!marshal_backtrace_header(exec_cookie, current->tgid, current->pid, inKernel, time))
                return;
 
        if (inKernel) {
@@ -553,7 +627,7 @@ static void gator_add_sample(int cpu, struct pt_regs *const regs)
                        arm_backtrace_eabi(cpu, regs, gator_backtrace_depth);
        }
 
-       marshal_backtrace_footer();
+       marshal_backtrace_footer(time);
 }
 
 /******************************************************************************
@@ -567,15 +641,21 @@ static void gator_timer_interrupt(void)
 
 void gator_backtrace_handler(struct pt_regs *const regs)
 {
+       u64 time = gator_get_time();
        int cpu = get_physical_cpu();
 
        // Output backtrace
-       gator_add_sample(cpu, regs);
+       gator_add_sample(cpu, regs, time);
 
        // Collect counters
        if (!per_cpu(collecting, cpu)) {
-               collect_counters();
+               collect_counters(time, NULL);
        }
+
+       // No buffer flushing occurs during sched switch for RT-Preempt full. The block counter frame will be flushed by collect_counters, but the sched buffer needs to be explicitly flushed
+#ifdef CONFIG_PREEMPT_RT_FULL
+       buffer_check(cpu, SCHED_TRACE_BUF, time);
+#endif
 }
 
 static int gator_running;
@@ -640,6 +720,25 @@ static void gator_timer_stop(void)
        }
 }
 
+#if defined(__arm__) || defined(__aarch64__)
+static void gator_send_core_name(int cpu, const u32 cpuid, const struct gator_cpu *const gator_cpu) {
+       const char *core_name = NULL;
+       char core_name_buf[32];
+
+       if (!sent_core_name[cpu]) {
+               if (gator_cpu != NULL) {
+                       core_name = gator_cpu->core_name;
+               } else {
+                       snprintf(core_name_buf, sizeof(core_name_buf), "Unknown (0x%.3x)", cpuid);
+                       core_name = core_name_buf;
+               }
+
+               marshal_core_name(cpu, cpuid, core_name);
+               sent_core_name[cpu] = true;
+       }
+}
+#endif
+
 // This function runs in interrupt context and on the appropriate core
 static void gator_timer_online(void *migrate)
 {
@@ -669,20 +768,8 @@ static void gator_timer_online(void *migrate)
 
 #if defined(__arm__) || defined(__aarch64__)
        if (!sent_core_name[cpu]) {
-               const char *core_name = NULL;
                const u32 cpuid = gator_cpuid();
-               const struct gator_cpu *const gator_cpu = gator_find_cpu_by_cpuid(cpuid);
-               char core_name_buf[32];
-
-               if (gator_cpu != NULL) {
-                       core_name = gator_cpu->core_name;
-               } else {
-                       snprintf(core_name_buf, sizeof(core_name_buf), "Unknown (0x%.3x)", cpuid);
-                       core_name = core_name_buf;
-               }
-
-               marshal_core_name(cpuid, core_name);
-               sent_core_name[cpu] = true;
+               gator_send_core_name(cpu, cpuid, gator_find_cpu_by_cpuid(cpuid));
        }
 #endif
 }
@@ -699,6 +786,8 @@ static void gator_timer_online_dispatch(int cpu, bool migrate)
        }
 }
 
+#include "gator_iks.c"
+
 int gator_timer_start(unsigned long sample_rate)
 {
        int cpu;
@@ -718,6 +807,7 @@ int gator_timer_start(unsigned long sample_rate)
        if (gator_hrtimer_init(sample_rate, gator_timer_interrupt) == -1)
                return -1;
 
+       gator_send_iks_core_names();
        for_each_online_cpu(cpu) {
                gator_timer_online_dispatch(lcpu_to_pcpu(cpu), false);
        }
@@ -740,6 +830,7 @@ static u64 gator_get_time(void)
 
        // getrawmonotonic is not monotonic on all systems. Detect and attempt to correct these cases.
        // up to 0.5ms delta has been seen on some systems, which can skew Streamline data when viewing at high resolution.
+       // This doesn't work well with interrupts, but that it's OK - the real concern is to catch big jumps in time
        prev_timestamp = per_cpu(last_timestamp, cpu);
        if (prev_timestamp <= timestamp) {
                per_cpu(last_timestamp, cpu) = timestamp;
@@ -759,8 +850,6 @@ static u64 gator_get_time(void)
 /******************************************************************************
  * cpu hotplug and pm notifiers
  ******************************************************************************/
-#include "gator_iks.c"
-
 static int __cpuinit gator_hotcpu_notify(struct notifier_block *self, unsigned long action, void *hcpu)
 {
        int cpu = lcpu_to_pcpu((long)hcpu);
@@ -790,6 +879,7 @@ static struct notifier_block __refdata gator_hotcpu_notifier = {
 static int gator_pm_notify(struct notifier_block *nb, unsigned long event, void *dummy)
 {
        int cpu;
+       struct timespec ts;
 
        switch (event) {
        case PM_HIBERNATION_PREPARE:
@@ -800,9 +890,20 @@ static int gator_pm_notify(struct notifier_block *nb, unsigned long event, void
                for_each_online_cpu(cpu) {
                        gator_timer_offline_dispatch(lcpu_to_pcpu(cpu), false);
                }
+
+               // Record the wallclock hibernate time
+               getnstimeofday(&ts);
+               gator_hibernate_time = timespec_to_ns(&ts) - gator_get_time();
                break;
        case PM_POST_HIBERNATION:
        case PM_POST_SUSPEND:
+               // Adjust gator_monotonic_started for the time spent sleeping, as gator_get_time does not account for it
+               if (gator_hibernate_time > 0) {
+                       getnstimeofday(&ts);
+                       gator_monotonic_started += gator_hibernate_time + gator_get_time() - timespec_to_ns(&ts);
+                       gator_hibernate_time = 0;
+               }
+
                for_each_online_cpu(cpu) {
                        gator_timer_online_dispatch(lcpu_to_pcpu(cpu), false);
                }
@@ -865,7 +966,7 @@ static void gator_summary(void)
        gator_monotonic_started = gator_get_time();
        local_irq_restore(flags);
 
-       marshal_summary(timestamp, uptime, uname_buf);
+       marshal_summary(timestamp, uptime, gator_monotonic_started, uname_buf);
 }
 
 int gator_events_install(struct gator_interface *interface)
@@ -877,8 +978,10 @@ int gator_events_install(struct gator_interface *interface)
 
 int gator_events_get_key(void)
 {
-       // key of zero is reserved as a timestamp
-       static int key = 1;
+       // key 0 is reserved as a timestamp
+       // key 1 is reserved as the marker for thread specific counters
+       // Odd keys are assigned by the driver, even keys by the daemon
+       static int key = 3;
 
        const int ret = key;
        key += 2;
@@ -891,7 +994,7 @@ static int gator_init(void)
 
        calc_first_cluster_size();
 
-       // events sources (gator_events.h, generated by gator_events.sh)
+       // events sources
        for (i = 0; i < ARRAY_SIZE(gator_events_list); i++)
                if (gator_events_list[i])
                        gator_events_list[i]();
@@ -916,6 +1019,11 @@ static int gator_start(void)
        unsigned long cpu, i;
        struct gator_interface *gi;
 
+       gator_buffer_wake_stop = false;
+       if (IS_ERR(gator_buffer_wake_thread = kthread_run(gator_buffer_wake_func, NULL, "gator_bwake"))) {
+               goto bwake_failure;
+       }
+
        if (gator_migrate_start())
                goto migrate_failure;
 
@@ -986,6 +1094,9 @@ cookies_failure:
 events_failure:
        gator_migrate_stop();
 migrate_failure:
+       gator_buffer_wake_stop = true;
+       wake_up_process(gator_buffer_wake_thread);
+bwake_failure:
 
        return -1;
 }
@@ -1009,6 +1120,9 @@ static void gator_stop(void)
                        gi->stop();
 
        gator_migrate_stop();
+
+       gator_buffer_wake_stop = true;
+       wake_up_process(gator_buffer_wake_thread);
 }
 
 /******************************************************************************
@@ -1062,9 +1176,7 @@ static int gator_op_setup(void)
                        per_cpu(gator_buffer_write, cpu)[i] = 0;
                        per_cpu(gator_buffer_commit, cpu)[i] = 0;
                        per_cpu(buffer_space_available, cpu)[i] = true;
-#if GATOR_LIVE
                        per_cpu(gator_buffer_commit_time, cpu) = gator_live_rate;
-#endif
 
                        // Annotation is a special case that only uses a single buffer
                        if (cpu > 0 && i == ANNOTATE_BUF) {
@@ -1138,9 +1250,7 @@ static void gator_shutdown(void)
                        per_cpu(gator_buffer_write, cpu)[i] = 0;
                        per_cpu(gator_buffer_commit, cpu)[i] = 0;
                        per_cpu(buffer_space_available, cpu)[i] = true;
-#if GATOR_LIVE
                        per_cpu(gator_buffer_commit_time, cpu) = 0;
-#endif
                }
                mutex_unlock(&gator_buffer_mutex);
        }
@@ -1297,7 +1407,7 @@ static ssize_t userspace_buffer_read(struct file *file, char __user *buf, size_t
                if (buftype == ANNOTATE_BUF) {
                        wake_up(&gator_annotate_wait);
                }
-       }       while (buffer_commit_ready(&cpu, &buftype));
+       } while (buffer_commit_ready(&cpu, &buftype));
 
        mutex_unlock(&gator_buffer_mutex);
 
@@ -1417,3 +1527,6 @@ module_exit(gator_module_exit);
 MODULE_LICENSE("GPL");
 MODULE_AUTHOR("ARM Ltd");
 MODULE_DESCRIPTION("Gator system profiler");
+#define STRIFY2(ARG) #ARG
+#define STRIFY(ARG) STRIFY2(ARG)
+MODULE_VERSION(STRIFY(PROTOCOL_VERSION));