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