Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is subject to the terms and conditions of the GNU General Public |
| 3 | * License. See the file "COPYING" in the main directory of this archive |
| 4 | * for more details. |
| 5 | * |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 6 | * Copyright (C) 2004, 05, 06 by Ralf Baechle |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 7 | * Copyright (C) 2005 by MIPS Technologies, Inc. |
| 8 | */ |
Ralf Baechle | 5e2862e | 2007-12-06 09:12:28 +0000 | [diff] [blame] | 9 | #include <linux/cpumask.h> |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 10 | #include <linux/oprofile.h> |
| 11 | #include <linux/interrupt.h> |
| 12 | #include <linux/smp.h> |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 13 | #include <asm/irq_regs.h> |
Andrew Bresticker | a669efc | 2014-09-18 14:47:12 -0700 | [diff] [blame] | 14 | #include <asm/time.h> |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 15 | |
| 16 | #include "op_impl.h" |
| 17 | |
James Hogan | 2654294 | 2017-02-06 12:37:45 +0000 | [diff] [blame] | 18 | #define M_PERFCTL_EVENT(event) (((event) << MIPS_PERFCTRL_EVENT_S) & \ |
| 19 | MIPS_PERFCTRL_EVENT) |
| 20 | #define M_PERFCTL_VPEID(vpe) ((vpe) << MIPS_PERFCTRL_VPEID_S) |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 21 | |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 22 | #define M_COUNTER_OVERFLOW (1UL << 31) |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 23 | |
Dmitri Vorobiev | 4668473 | 2008-04-02 03:58:38 +0400 | [diff] [blame] | 24 | static int (*save_perf_irq)(void); |
Andrew Bresticker | a669efc | 2014-09-18 14:47:12 -0700 | [diff] [blame] | 25 | static int perfcount_irq; |
Dmitri Vorobiev | 4668473 | 2008-04-02 03:58:38 +0400 | [diff] [blame] | 26 | |
Madhusudan Bhat | c783390 | 2012-10-31 12:01:27 +0000 | [diff] [blame] | 27 | /* |
| 28 | * XLR has only one set of counters per core. Designate the |
| 29 | * first hardware thread in the core for setup and init. |
| 30 | * Skip CPUs with non-zero hardware thread id (4 hwt per core) |
| 31 | */ |
Jayachandran C | 83a1841 | 2013-03-25 06:51:52 +0000 | [diff] [blame] | 32 | #if defined(CONFIG_CPU_XLR) && defined(CONFIG_SMP) |
Madhusudan Bhat | c783390 | 2012-10-31 12:01:27 +0000 | [diff] [blame] | 33 | #define oprofile_skip_cpu(c) ((cpu_logical_map(c) & 0x3) != 0) |
| 34 | #else |
| 35 | #define oprofile_skip_cpu(c) 0 |
| 36 | #endif |
| 37 | |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 38 | #ifdef CONFIG_MIPS_MT_SMP |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 39 | static int cpu_has_mipsmt_pertccounters; |
James Hogan | 2654294 | 2017-02-06 12:37:45 +0000 | [diff] [blame] | 40 | #define WHAT (MIPS_PERFCTRL_MT_EN_VPE | \ |
Paul Burton | f875a832 | 2017-08-12 19:49:35 -0700 | [diff] [blame] | 41 | M_PERFCTL_VPEID(cpu_vpe_id(¤t_cpu_data))) |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 42 | #define vpe_id() (cpu_has_mipsmt_pertccounters ? \ |
Paul Burton | f875a832 | 2017-08-12 19:49:35 -0700 | [diff] [blame] | 43 | 0 : cpu_vpe_id(¤t_cpu_data)) |
Ralf Baechle | 5e2862e | 2007-12-06 09:12:28 +0000 | [diff] [blame] | 44 | |
| 45 | /* |
| 46 | * The number of bits to shift to convert between counters per core and |
| 47 | * counters per VPE. There is no reasonable interface atm to obtain the |
| 48 | * number of VPEs used by Linux and in the 34K this number is fixed to two |
| 49 | * anyways so we hardcore a few things here for the moment. The way it's |
| 50 | * done here will ensure that oprofile VSMP kernel will run right on a lesser |
| 51 | * core like a 24K also or with maxcpus=1. |
| 52 | */ |
| 53 | static inline unsigned int vpe_shift(void) |
| 54 | { |
| 55 | if (num_possible_cpus() > 1) |
| 56 | return 1; |
| 57 | |
| 58 | return 0; |
| 59 | } |
| 60 | |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 61 | #else |
Ralf Baechle | 5e2862e | 2007-12-06 09:12:28 +0000 | [diff] [blame] | 62 | |
Ralf Baechle | be609f3 | 2006-10-23 13:22:06 +0100 | [diff] [blame] | 63 | #define WHAT 0 |
Ralf Baechle | 6f4c5bd | 2007-04-24 21:42:20 +0100 | [diff] [blame] | 64 | #define vpe_id() 0 |
Ralf Baechle | 5e2862e | 2007-12-06 09:12:28 +0000 | [diff] [blame] | 65 | |
| 66 | static inline unsigned int vpe_shift(void) |
| 67 | { |
| 68 | return 0; |
| 69 | } |
| 70 | |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 71 | #endif |
| 72 | |
Ralf Baechle | 5e2862e | 2007-12-06 09:12:28 +0000 | [diff] [blame] | 73 | static inline unsigned int counters_total_to_per_cpu(unsigned int counters) |
| 74 | { |
| 75 | return counters >> vpe_shift(); |
| 76 | } |
| 77 | |
| 78 | static inline unsigned int counters_per_cpu_to_total(unsigned int counters) |
| 79 | { |
| 80 | return counters << vpe_shift(); |
| 81 | } |
| 82 | |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 83 | #define __define_perf_accessors(r, n, np) \ |
| 84 | \ |
| 85 | static inline unsigned int r_c0_ ## r ## n(void) \ |
| 86 | { \ |
Ralf Baechle | be609f3 | 2006-10-23 13:22:06 +0100 | [diff] [blame] | 87 | unsigned int cpu = vpe_id(); \ |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 88 | \ |
| 89 | switch (cpu) { \ |
| 90 | case 0: \ |
| 91 | return read_c0_ ## r ## n(); \ |
| 92 | case 1: \ |
| 93 | return read_c0_ ## r ## np(); \ |
| 94 | default: \ |
| 95 | BUG(); \ |
| 96 | } \ |
Thiemo Seufer | 30f244a | 2006-07-07 10:38:51 +0100 | [diff] [blame] | 97 | return 0; \ |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 98 | } \ |
| 99 | \ |
| 100 | static inline void w_c0_ ## r ## n(unsigned int value) \ |
| 101 | { \ |
Ralf Baechle | be609f3 | 2006-10-23 13:22:06 +0100 | [diff] [blame] | 102 | unsigned int cpu = vpe_id(); \ |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 103 | \ |
| 104 | switch (cpu) { \ |
| 105 | case 0: \ |
| 106 | write_c0_ ## r ## n(value); \ |
| 107 | return; \ |
| 108 | case 1: \ |
| 109 | write_c0_ ## r ## np(value); \ |
| 110 | return; \ |
| 111 | default: \ |
| 112 | BUG(); \ |
| 113 | } \ |
Thiemo Seufer | 30f244a | 2006-07-07 10:38:51 +0100 | [diff] [blame] | 114 | return; \ |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 115 | } \ |
| 116 | |
| 117 | __define_perf_accessors(perfcntr, 0, 2) |
| 118 | __define_perf_accessors(perfcntr, 1, 3) |
Chris Dearman | 795a225 | 2007-03-01 17:58:24 +0000 | [diff] [blame] | 119 | __define_perf_accessors(perfcntr, 2, 0) |
| 120 | __define_perf_accessors(perfcntr, 3, 1) |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 121 | |
| 122 | __define_perf_accessors(perfctrl, 0, 2) |
| 123 | __define_perf_accessors(perfctrl, 1, 3) |
Chris Dearman | 795a225 | 2007-03-01 17:58:24 +0000 | [diff] [blame] | 124 | __define_perf_accessors(perfctrl, 2, 0) |
| 125 | __define_perf_accessors(perfctrl, 3, 1) |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 126 | |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 127 | struct op_mips_model op_model_mipsxx_ops; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 128 | |
| 129 | static struct mipsxx_register_config { |
| 130 | unsigned int control[4]; |
| 131 | unsigned int counter[4]; |
| 132 | } reg; |
| 133 | |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 134 | /* Compute all of the registers in preparation for enabling profiling. */ |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 135 | |
| 136 | static void mipsxx_reg_setup(struct op_counter_config *ctr) |
| 137 | { |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 138 | unsigned int counters = op_model_mipsxx_ops.num_counters; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 139 | int i; |
| 140 | |
| 141 | /* Compute the performance counter control word. */ |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 142 | for (i = 0; i < counters; i++) { |
| 143 | reg.control[i] = 0; |
| 144 | reg.counter[i] = 0; |
| 145 | |
| 146 | if (!ctr[i].enabled) |
| 147 | continue; |
| 148 | |
| 149 | reg.control[i] = M_PERFCTL_EVENT(ctr[i].event) | |
James Hogan | 2654294 | 2017-02-06 12:37:45 +0000 | [diff] [blame] | 150 | MIPS_PERFCTRL_IE; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 151 | if (ctr[i].kernel) |
James Hogan | 2654294 | 2017-02-06 12:37:45 +0000 | [diff] [blame] | 152 | reg.control[i] |= MIPS_PERFCTRL_K; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 153 | if (ctr[i].user) |
James Hogan | 2654294 | 2017-02-06 12:37:45 +0000 | [diff] [blame] | 154 | reg.control[i] |= MIPS_PERFCTRL_U; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 155 | if (ctr[i].exl) |
James Hogan | 2654294 | 2017-02-06 12:37:45 +0000 | [diff] [blame] | 156 | reg.control[i] |= MIPS_PERFCTRL_EXL; |
Ralf Baechle | cf5b2d23 | 2013-08-01 18:31:05 +0200 | [diff] [blame] | 157 | if (boot_cpu_type() == CPU_XLR) |
James Hogan | 2654294 | 2017-02-06 12:37:45 +0000 | [diff] [blame] | 158 | reg.control[i] |= XLR_PERFCTRL_ALLTHREADS; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 159 | reg.counter[i] = 0x80000000 - ctr[i].count; |
| 160 | } |
| 161 | } |
| 162 | |
Ralf Baechle | 7034228 | 2013-01-22 12:59:30 +0100 | [diff] [blame] | 163 | /* Program all of the registers in preparation for enabling profiling. */ |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 164 | |
Ralf Baechle | 49a89ef | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 165 | static void mipsxx_cpu_setup(void *args) |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 166 | { |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 167 | unsigned int counters = op_model_mipsxx_ops.num_counters; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 168 | |
Madhusudan Bhat | c783390 | 2012-10-31 12:01:27 +0000 | [diff] [blame] | 169 | if (oprofile_skip_cpu(smp_processor_id())) |
| 170 | return; |
| 171 | |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 172 | switch (counters) { |
| 173 | case 4: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 174 | w_c0_perfctrl3(0); |
| 175 | w_c0_perfcntr3(reg.counter[3]); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 176 | case 3: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 177 | w_c0_perfctrl2(0); |
| 178 | w_c0_perfcntr2(reg.counter[2]); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 179 | case 2: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 180 | w_c0_perfctrl1(0); |
| 181 | w_c0_perfcntr1(reg.counter[1]); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 182 | case 1: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 183 | w_c0_perfctrl0(0); |
| 184 | w_c0_perfcntr0(reg.counter[0]); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | /* Start all counters on current CPU */ |
| 189 | static void mipsxx_cpu_start(void *args) |
| 190 | { |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 191 | unsigned int counters = op_model_mipsxx_ops.num_counters; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 192 | |
Madhusudan Bhat | c783390 | 2012-10-31 12:01:27 +0000 | [diff] [blame] | 193 | if (oprofile_skip_cpu(smp_processor_id())) |
| 194 | return; |
| 195 | |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 196 | switch (counters) { |
| 197 | case 4: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 198 | w_c0_perfctrl3(WHAT | reg.control[3]); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 199 | case 3: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 200 | w_c0_perfctrl2(WHAT | reg.control[2]); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 201 | case 2: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 202 | w_c0_perfctrl1(WHAT | reg.control[1]); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 203 | case 1: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 204 | w_c0_perfctrl0(WHAT | reg.control[0]); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | /* Stop all counters on current CPU */ |
| 209 | static void mipsxx_cpu_stop(void *args) |
| 210 | { |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 211 | unsigned int counters = op_model_mipsxx_ops.num_counters; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 212 | |
Madhusudan Bhat | c783390 | 2012-10-31 12:01:27 +0000 | [diff] [blame] | 213 | if (oprofile_skip_cpu(smp_processor_id())) |
| 214 | return; |
| 215 | |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 216 | switch (counters) { |
| 217 | case 4: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 218 | w_c0_perfctrl3(0); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 219 | case 3: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 220 | w_c0_perfctrl2(0); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 221 | case 2: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 222 | w_c0_perfctrl1(0); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 223 | case 1: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 224 | w_c0_perfctrl0(0); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 228 | static int mipsxx_perfcount_handler(void) |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 229 | { |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 230 | unsigned int counters = op_model_mipsxx_ops.num_counters; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 231 | unsigned int control; |
| 232 | unsigned int counter; |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 233 | int handled = IRQ_NONE; |
| 234 | |
James Hogan | 3ba5040 | 2015-01-27 21:45:48 +0000 | [diff] [blame] | 235 | if (cpu_has_mips_r2 && !(read_c0_cause() & CAUSEF_PCI)) |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 236 | return handled; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 237 | |
| 238 | switch (counters) { |
| 239 | #define HANDLE_COUNTER(n) \ |
| 240 | case n + 1: \ |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 241 | control = r_c0_perfctrl ## n(); \ |
| 242 | counter = r_c0_perfcntr ## n(); \ |
James Hogan | 2654294 | 2017-02-06 12:37:45 +0000 | [diff] [blame] | 243 | if ((control & MIPS_PERFCTRL_IE) && \ |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 244 | (counter & M_COUNTER_OVERFLOW)) { \ |
Ralf Baechle | 937a801 | 2006-10-07 19:44:33 +0100 | [diff] [blame] | 245 | oprofile_add_sample(get_irq_regs(), n); \ |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 246 | w_c0_perfcntr ## n(reg.counter[n]); \ |
Chris Dearman | ffe9ee4 | 2007-05-24 22:24:20 +0100 | [diff] [blame] | 247 | handled = IRQ_HANDLED; \ |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 248 | } |
| 249 | HANDLE_COUNTER(3) |
| 250 | HANDLE_COUNTER(2) |
| 251 | HANDLE_COUNTER(1) |
| 252 | HANDLE_COUNTER(0) |
| 253 | } |
Ralf Baechle | ba339c0 | 2005-12-09 12:29:38 +0000 | [diff] [blame] | 254 | |
| 255 | return handled; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 258 | static inline int __n_counters(void) |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 259 | { |
James Hogan | 30228c4 | 2016-05-11 13:50:53 +0100 | [diff] [blame] | 260 | if (!cpu_has_perf) |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 261 | return 0; |
James Hogan | 2654294 | 2017-02-06 12:37:45 +0000 | [diff] [blame] | 262 | if (!(read_c0_perfctrl0() & MIPS_PERFCTRL_M)) |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 263 | return 1; |
James Hogan | 2654294 | 2017-02-06 12:37:45 +0000 | [diff] [blame] | 264 | if (!(read_c0_perfctrl1() & MIPS_PERFCTRL_M)) |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 265 | return 2; |
James Hogan | 2654294 | 2017-02-06 12:37:45 +0000 | [diff] [blame] | 266 | if (!(read_c0_perfctrl2() & MIPS_PERFCTRL_M)) |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 267 | return 3; |
| 268 | |
| 269 | return 4; |
| 270 | } |
| 271 | |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 272 | static inline int n_counters(void) |
| 273 | { |
Ralf Baechle | 714cfe7 | 2006-10-23 00:44:02 +0100 | [diff] [blame] | 274 | int counters; |
| 275 | |
Ralf Baechle | 10cc352 | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 276 | switch (current_cpu_type()) { |
Ralf Baechle | 714cfe7 | 2006-10-23 00:44:02 +0100 | [diff] [blame] | 277 | case CPU_R10000: |
| 278 | counters = 2; |
Ralf Baechle | 148171b | 2007-02-28 15:34:22 +0000 | [diff] [blame] | 279 | break; |
Ralf Baechle | 714cfe7 | 2006-10-23 00:44:02 +0100 | [diff] [blame] | 280 | |
| 281 | case CPU_R12000: |
| 282 | case CPU_R14000: |
Joshua Kinard | 3057739 | 2015-01-21 07:59:45 -0500 | [diff] [blame] | 283 | case CPU_R16000: |
Ralf Baechle | 714cfe7 | 2006-10-23 00:44:02 +0100 | [diff] [blame] | 284 | counters = 4; |
Ralf Baechle | 148171b | 2007-02-28 15:34:22 +0000 | [diff] [blame] | 285 | break; |
Ralf Baechle | 714cfe7 | 2006-10-23 00:44:02 +0100 | [diff] [blame] | 286 | |
| 287 | default: |
| 288 | counters = __n_counters(); |
| 289 | } |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 290 | |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 291 | return counters; |
| 292 | } |
| 293 | |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 294 | static void reset_counters(void *arg) |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 295 | { |
Thiemo Seufer | 005ca9a | 2008-05-06 11:23:33 +0100 | [diff] [blame] | 296 | int counters = (int)(long)arg; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 297 | switch (counters) { |
| 298 | case 4: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 299 | w_c0_perfctrl3(0); |
| 300 | w_c0_perfcntr3(0); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 301 | case 3: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 302 | w_c0_perfctrl2(0); |
| 303 | w_c0_perfcntr2(0); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 304 | case 2: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 305 | w_c0_perfctrl1(0); |
| 306 | w_c0_perfcntr1(0); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 307 | case 1: |
Ralf Baechle | 92c7b62 | 2006-06-23 18:39:00 +0100 | [diff] [blame] | 308 | w_c0_perfctrl0(0); |
| 309 | w_c0_perfcntr0(0); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 310 | } |
| 311 | } |
| 312 | |
Felix Fietkau | 3572a2c | 2012-05-02 17:33:04 +0200 | [diff] [blame] | 313 | static irqreturn_t mipsxx_perfcount_int(int irq, void *dev_id) |
| 314 | { |
| 315 | return mipsxx_perfcount_handler(); |
| 316 | } |
| 317 | |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 318 | static int __init mipsxx_init(void) |
| 319 | { |
| 320 | int counters; |
| 321 | |
| 322 | counters = n_counters(); |
Ralf Baechle | 9efeae9 | 2005-12-09 12:34:45 +0000 | [diff] [blame] | 323 | if (counters == 0) { |
| 324 | printk(KERN_ERR "Oprofile: CPU has no performance counters\n"); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 325 | return -ENODEV; |
Ralf Baechle | 9efeae9 | 2005-12-09 12:34:45 +0000 | [diff] [blame] | 326 | } |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 327 | |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 328 | #ifdef CONFIG_MIPS_MT_SMP |
| 329 | cpu_has_mipsmt_pertccounters = read_c0_config7() & (1<<19); |
| 330 | if (!cpu_has_mipsmt_pertccounters) |
| 331 | counters = counters_total_to_per_cpu(counters); |
| 332 | #endif |
Ingo Molnar | f6f88e9 | 2008-07-15 22:08:52 +0200 | [diff] [blame] | 333 | on_each_cpu(reset_counters, (void *)(long)counters, 1); |
Chris Dearman | 795a225 | 2007-03-01 17:58:24 +0000 | [diff] [blame] | 334 | |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 335 | op_model_mipsxx_ops.num_counters = counters; |
Ralf Baechle | 10cc352 | 2007-10-11 23:46:15 +0100 | [diff] [blame] | 336 | switch (current_cpu_type()) { |
Steven J. Hill | 113c62d | 2012-07-06 23:56:00 +0200 | [diff] [blame] | 337 | case CPU_M14KC: |
| 338 | op_model_mipsxx_ops.cpu_type = "mips/M14Kc"; |
| 339 | break; |
| 340 | |
Steven J. Hill | f8fa481 | 2012-12-07 03:51:35 +0000 | [diff] [blame] | 341 | case CPU_M14KEC: |
| 342 | op_model_mipsxx_ops.cpu_type = "mips/M14KEc"; |
| 343 | break; |
| 344 | |
Ralf Baechle | 2065988 | 2005-12-09 12:42:13 +0000 | [diff] [blame] | 345 | case CPU_20KC: |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 346 | op_model_mipsxx_ops.cpu_type = "mips/20K"; |
Ralf Baechle | 2065988 | 2005-12-09 12:42:13 +0000 | [diff] [blame] | 347 | break; |
| 348 | |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 349 | case CPU_24K: |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 350 | op_model_mipsxx_ops.cpu_type = "mips/24K"; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 351 | break; |
| 352 | |
Ralf Baechle | 2065988 | 2005-12-09 12:42:13 +0000 | [diff] [blame] | 353 | case CPU_25KF: |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 354 | op_model_mipsxx_ops.cpu_type = "mips/25K"; |
Ralf Baechle | 2065988 | 2005-12-09 12:42:13 +0000 | [diff] [blame] | 355 | break; |
| 356 | |
Ralf Baechle | 39b8d52 | 2008-04-28 17:14:26 +0100 | [diff] [blame] | 357 | case CPU_1004K: |
Ralf Baechle | fcfd980 | 2006-02-01 17:54:30 +0000 | [diff] [blame] | 358 | case CPU_34K: |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 359 | op_model_mipsxx_ops.cpu_type = "mips/34K"; |
Ralf Baechle | fcfd980 | 2006-02-01 17:54:30 +0000 | [diff] [blame] | 360 | break; |
Chris Dearman | c620953 | 2006-05-02 14:08:46 +0100 | [diff] [blame] | 361 | |
Steven J. Hill | 442e14a | 2014-01-17 15:03:50 -0600 | [diff] [blame] | 362 | case CPU_1074K: |
Chris Dearman | c620953 | 2006-05-02 14:08:46 +0100 | [diff] [blame] | 363 | case CPU_74K: |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 364 | op_model_mipsxx_ops.cpu_type = "mips/74K"; |
Chris Dearman | c620953 | 2006-05-02 14:08:46 +0100 | [diff] [blame] | 365 | break; |
Ralf Baechle | fcfd980 | 2006-02-01 17:54:30 +0000 | [diff] [blame] | 366 | |
Leonid Yegoshin | 26ab96d | 2013-11-27 10:07:53 +0000 | [diff] [blame] | 367 | case CPU_INTERAPTIV: |
| 368 | op_model_mipsxx_ops.cpu_type = "mips/interAptiv"; |
| 369 | break; |
| 370 | |
Leonid Yegoshin | 708ac4b | 2013-11-14 16:12:27 +0000 | [diff] [blame] | 371 | case CPU_PROAPTIV: |
| 372 | op_model_mipsxx_ops.cpu_type = "mips/proAptiv"; |
| 373 | break; |
| 374 | |
James Hogan | 8c7f6ba | 2014-01-22 16:19:41 +0000 | [diff] [blame] | 375 | case CPU_P5600: |
| 376 | op_model_mipsxx_ops.cpu_type = "mips/P5600"; |
| 377 | break; |
| 378 | |
Markos Chandras | 4e88a86 | 2015-07-09 10:40:36 +0100 | [diff] [blame] | 379 | case CPU_I6400: |
| 380 | op_model_mipsxx_ops.cpu_type = "mips/I6400"; |
| 381 | break; |
| 382 | |
Leonid Yegoshin | f36c472 | 2014-03-04 13:34:43 +0000 | [diff] [blame] | 383 | case CPU_M5150: |
| 384 | op_model_mipsxx_ops.cpu_type = "mips/M5150"; |
| 385 | break; |
| 386 | |
Ralf Baechle | 2065988 | 2005-12-09 12:42:13 +0000 | [diff] [blame] | 387 | case CPU_5KC: |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 388 | op_model_mipsxx_ops.cpu_type = "mips/5K"; |
Ralf Baechle | 2065988 | 2005-12-09 12:42:13 +0000 | [diff] [blame] | 389 | break; |
| 390 | |
Ralf Baechle | 714cfe7 | 2006-10-23 00:44:02 +0100 | [diff] [blame] | 391 | case CPU_R10000: |
| 392 | if ((current_cpu_data.processor_id & 0xff) == 0x20) |
| 393 | op_model_mipsxx_ops.cpu_type = "mips/r10000-v2.x"; |
| 394 | else |
| 395 | op_model_mipsxx_ops.cpu_type = "mips/r10000"; |
| 396 | break; |
| 397 | |
| 398 | case CPU_R12000: |
| 399 | case CPU_R14000: |
| 400 | op_model_mipsxx_ops.cpu_type = "mips/r12000"; |
| 401 | break; |
| 402 | |
Joshua Kinard | 3057739 | 2015-01-21 07:59:45 -0500 | [diff] [blame] | 403 | case CPU_R16000: |
| 404 | op_model_mipsxx_ops.cpu_type = "mips/r16000"; |
| 405 | break; |
| 406 | |
Mark Mason | c03bc12 | 2006-01-17 12:06:32 -0800 | [diff] [blame] | 407 | case CPU_SB1: |
| 408 | case CPU_SB1A: |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 409 | op_model_mipsxx_ops.cpu_type = "mips/sb1"; |
Mark Mason | c03bc12 | 2006-01-17 12:06:32 -0800 | [diff] [blame] | 410 | break; |
| 411 | |
Kelvin Cheung | 2fa3639 | 2012-06-20 20:05:32 +0100 | [diff] [blame] | 412 | case CPU_LOONGSON1: |
| 413 | op_model_mipsxx_ops.cpu_type = "mips/loongson1"; |
| 414 | break; |
| 415 | |
Madhusudan Bhat | c783390 | 2012-10-31 12:01:27 +0000 | [diff] [blame] | 416 | case CPU_XLR: |
| 417 | op_model_mipsxx_ops.cpu_type = "mips/xlr"; |
| 418 | break; |
| 419 | |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 420 | default: |
| 421 | printk(KERN_ERR "Profiling unsupported for this CPU\n"); |
| 422 | |
| 423 | return -ENODEV; |
| 424 | } |
| 425 | |
Dmitri Vorobiev | 4668473 | 2008-04-02 03:58:38 +0400 | [diff] [blame] | 426 | save_perf_irq = perf_irq; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 427 | perf_irq = mipsxx_perfcount_handler; |
| 428 | |
Andrew Bresticker | a669efc | 2014-09-18 14:47:12 -0700 | [diff] [blame] | 429 | if (get_c0_perfcount_int) |
| 430 | perfcount_irq = get_c0_perfcount_int(); |
James Hogan | 7eca5b1 | 2015-01-27 21:45:49 +0000 | [diff] [blame] | 431 | else if (cp0_perfcount_irq >= 0) |
Andrew Bresticker | a669efc | 2014-09-18 14:47:12 -0700 | [diff] [blame] | 432 | perfcount_irq = MIPS_CPU_IRQ_BASE + cp0_perfcount_irq; |
| 433 | else |
| 434 | perfcount_irq = -1; |
| 435 | |
| 436 | if (perfcount_irq >= 0) |
| 437 | return request_irq(perfcount_irq, mipsxx_perfcount_int, |
James Hogan | 369a93b | 2015-01-27 21:45:54 +0000 | [diff] [blame] | 438 | IRQF_PERCPU | IRQF_NOBALANCING | |
| 439 | IRQF_NO_THREAD | IRQF_NO_SUSPEND | |
| 440 | IRQF_SHARED, |
| 441 | "Perfcounter", save_perf_irq); |
Felix Fietkau | 3572a2c | 2012-05-02 17:33:04 +0200 | [diff] [blame] | 442 | |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | static void mipsxx_exit(void) |
| 447 | { |
Chris Dearman | 795a225 | 2007-03-01 17:58:24 +0000 | [diff] [blame] | 448 | int counters = op_model_mipsxx_ops.num_counters; |
Ralf Baechle | 5e2862e | 2007-12-06 09:12:28 +0000 | [diff] [blame] | 449 | |
Andrew Bresticker | a669efc | 2014-09-18 14:47:12 -0700 | [diff] [blame] | 450 | if (perfcount_irq >= 0) |
| 451 | free_irq(perfcount_irq, save_perf_irq); |
Felix Fietkau | 3572a2c | 2012-05-02 17:33:04 +0200 | [diff] [blame] | 452 | |
Ralf Baechle | 5e2862e | 2007-12-06 09:12:28 +0000 | [diff] [blame] | 453 | counters = counters_per_cpu_to_total(counters); |
Ingo Molnar | f6f88e9 | 2008-07-15 22:08:52 +0200 | [diff] [blame] | 454 | on_each_cpu(reset_counters, (void *)(long)counters, 1); |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 455 | |
Dmitri Vorobiev | 4668473 | 2008-04-02 03:58:38 +0400 | [diff] [blame] | 456 | perf_irq = save_perf_irq; |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Atsushi Nemoto | 1acf1ca | 2006-05-23 16:42:38 +0900 | [diff] [blame] | 459 | struct op_mips_model op_model_mipsxx_ops = { |
Ralf Baechle | 5417673 | 2005-02-07 02:54:29 +0000 | [diff] [blame] | 460 | .reg_setup = mipsxx_reg_setup, |
| 461 | .cpu_setup = mipsxx_cpu_setup, |
| 462 | .init = mipsxx_init, |
| 463 | .exit = mipsxx_exit, |
| 464 | .cpu_start = mipsxx_cpu_start, |
| 465 | .cpu_stop = mipsxx_cpu_stop, |
| 466 | }; |