2 * Freescale Embedded oprofile support, based on ppc64 oprofile support
5 * Copyright (c) 2004, 2010 Freescale Semiconductor, Inc
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
16 #include <linux/oprofile.h>
17 #include <linux/smp.h>
18 #include <asm/ptrace.h>
19 #include <asm/processor.h>
20 #include <asm/cputable.h>
21 #include <asm/reg_fsl_emb.h>
24 #include <asm/oprofile_impl.h>
26 static unsigned long reset_value[OP_MAX_COUNTER];
28 static int num_counters;
29 static int oprofile_running;
31 static inline u32 get_pmlca(int ctr)
37 pmlca = mfpmr(PMRN_PMLCA0);
40 pmlca = mfpmr(PMRN_PMLCA1);
43 pmlca = mfpmr(PMRN_PMLCA2);
46 pmlca = mfpmr(PMRN_PMLCA3);
49 pmlca = mfpmr(PMRN_PMLCA4);
52 pmlca = mfpmr(PMRN_PMLCA5);
55 panic("Bad ctr number\n");
61 static inline void set_pmlca(int ctr, u32 pmlca)
65 mtpmr(PMRN_PMLCA0, pmlca);
68 mtpmr(PMRN_PMLCA1, pmlca);
71 mtpmr(PMRN_PMLCA2, pmlca);
74 mtpmr(PMRN_PMLCA3, pmlca);
77 mtpmr(PMRN_PMLCA4, pmlca);
80 mtpmr(PMRN_PMLCA5, pmlca);
83 panic("Bad ctr number\n");
87 static inline unsigned int ctr_read(unsigned int i)
91 return mfpmr(PMRN_PMC0);
93 return mfpmr(PMRN_PMC1);
95 return mfpmr(PMRN_PMC2);
97 return mfpmr(PMRN_PMC3);
99 return mfpmr(PMRN_PMC4);
101 return mfpmr(PMRN_PMC5);
107 static inline void ctr_write(unsigned int i, unsigned int val)
111 mtpmr(PMRN_PMC0, val);
114 mtpmr(PMRN_PMC1, val);
117 mtpmr(PMRN_PMC2, val);
120 mtpmr(PMRN_PMC3, val);
123 mtpmr(PMRN_PMC4, val);
126 mtpmr(PMRN_PMC5, val);
134 static void init_pmc_stop(int ctr)
136 u32 pmlca = (PMLCA_FC | PMLCA_FCS | PMLCA_FCU |
137 PMLCA_FCM1 | PMLCA_FCM0);
142 mtpmr(PMRN_PMLCA0, pmlca);
143 mtpmr(PMRN_PMLCB0, pmlcb);
146 mtpmr(PMRN_PMLCA1, pmlca);
147 mtpmr(PMRN_PMLCB1, pmlcb);
150 mtpmr(PMRN_PMLCA2, pmlca);
151 mtpmr(PMRN_PMLCB2, pmlcb);
154 mtpmr(PMRN_PMLCA3, pmlca);
155 mtpmr(PMRN_PMLCB3, pmlcb);
158 mtpmr(PMRN_PMLCA4, pmlca);
159 mtpmr(PMRN_PMLCB4, pmlcb);
162 mtpmr(PMRN_PMLCA5, pmlca);
163 mtpmr(PMRN_PMLCB5, pmlcb);
166 panic("Bad ctr number!\n");
170 static void set_pmc_event(int ctr, int event)
174 pmlca = get_pmlca(ctr);
176 pmlca = (pmlca & ~PMLCA_EVENT_MASK) |
177 ((event << PMLCA_EVENT_SHIFT) &
180 set_pmlca(ctr, pmlca);
183 static void set_pmc_user_kernel(int ctr, int user, int kernel)
187 pmlca = get_pmlca(ctr);
199 set_pmlca(ctr, pmlca);
202 static void set_pmc_marked(int ctr, int mark0, int mark1)
204 u32 pmlca = get_pmlca(ctr);
207 pmlca &= ~PMLCA_FCM0;
212 pmlca &= ~PMLCA_FCM1;
216 set_pmlca(ctr, pmlca);
219 static void pmc_start_ctr(int ctr, int enable)
221 u32 pmlca = get_pmlca(ctr);
230 set_pmlca(ctr, pmlca);
233 static void pmc_start_ctrs(int enable)
235 u32 pmgc0 = mfpmr(PMRN_PMGC0);
238 pmgc0 |= PMGC0_FCECE;
243 pmgc0 &= ~PMGC0_PMIE;
245 mtpmr(PMRN_PMGC0, pmgc0);
248 static void pmc_stop_ctrs(void)
250 u32 pmgc0 = mfpmr(PMRN_PMGC0);
254 pmgc0 &= ~(PMGC0_PMIE | PMGC0_FCECE);
256 mtpmr(PMRN_PMGC0, pmgc0);
259 static int fsl_emb_cpu_setup(struct op_counter_config *ctr)
263 /* freeze all counters */
266 for (i = 0;i < num_counters;i++) {
269 set_pmc_event(i, ctr[i].event);
271 set_pmc_user_kernel(i, ctr[i].user, ctr[i].kernel);
277 static int fsl_emb_reg_setup(struct op_counter_config *ctr,
278 struct op_system_config *sys,
283 num_counters = num_ctrs;
285 /* Our counters count up, and "count" refers to
286 * how much before the next interrupt, and we interrupt
287 * on overflow. So we calculate the starting value
288 * which will give us "count" until overflow.
289 * Then we set the events on the enabled counters */
290 for (i = 0; i < num_counters; ++i)
291 reset_value[i] = 0x80000000UL - ctr[i].count;
296 static int fsl_emb_start(struct op_counter_config *ctr)
300 mtmsr(mfmsr() | MSR_PMM);
302 for (i = 0; i < num_counters; ++i) {
303 if (ctr[i].enabled) {
304 ctr_write(i, reset_value[i]);
305 /* Set each enabled counter to only
306 * count when the Mark bit is *not* set */
307 set_pmc_marked(i, 1, 0);
312 /* Set the ctr to be stopped */
317 /* Clear the freeze bit, and enable the interrupt.
318 * The counters won't actually start until the rfi clears
322 oprofile_running = 1;
324 pr_debug("start on cpu %d, pmgc0 %x\n", smp_processor_id(),
330 static void fsl_emb_stop(void)
332 /* freeze counters */
335 oprofile_running = 0;
337 pr_debug("stop on cpu %d, pmgc0 %x\n", smp_processor_id(),
344 static void fsl_emb_handle_interrupt(struct pt_regs *regs,
345 struct op_counter_config *ctr)
353 is_kernel = is_kernel_addr(pc);
355 for (i = 0; i < num_counters; ++i) {
358 if (oprofile_running && ctr[i].enabled) {
359 oprofile_add_ext_sample(pc, regs, i, is_kernel);
360 ctr_write(i, reset_value[i]);
367 /* The freeze bit was set by the interrupt. */
368 /* Clear the freeze bit, and reenable the interrupt. The
369 * counters won't actually start until the rfi clears the PMM
370 * bit. The PMM bit should not be set until after the interrupt
371 * is cleared to avoid it getting lost in some hypervisor
374 mtmsr(mfmsr() | MSR_PMM);
378 struct op_powerpc_model op_model_fsl_emb = {
379 .reg_setup = fsl_emb_reg_setup,
380 .cpu_setup = fsl_emb_cpu_setup,
381 .start = fsl_emb_start,
382 .stop = fsl_emb_stop,
383 .handle_interrupt = fsl_emb_handle_interrupt,