blob: 14cf86fdddab4373f76594509e0cc5789b33a371 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Andy Fleming39aef682008-02-04 18:27:55 -06002 * Freescale Embedded oprofile support, based on ppc64 oprofile support
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 2004 Anton Blanchard <anton@au.ibm.com>, IBM
4 *
Scott Wood4267ea72010-05-19 15:32:21 -05005 * Copyright (c) 2004, 2010 Freescale Semiconductor, Inc
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
7 * Author: Andy Fleming
Kumar Gala4c8d3d92005-11-13 16:06:30 -08008 * Maintainer: Kumar Gala <galak@kernel.crashing.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
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.
14 */
15
16#include <linux/oprofile.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/smp.h>
18#include <asm/ptrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <asm/processor.h>
20#include <asm/cputable.h>
Andy Fleming39aef682008-02-04 18:27:55 -060021#include <asm/reg_fsl_emb.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/page.h>
David Gibsonf7f6f4f2005-10-19 14:53:32 +100023#include <asm/pmc.h>
Stephen Rothwell654810e2005-09-19 23:21:15 +100024#include <asm/oprofile_impl.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26static unsigned long reset_value[OP_MAX_COUNTER];
27
28static int num_counters;
29static int oprofile_running;
30
Olof Johanssonc69b7672007-01-28 21:23:14 -060031static inline u32 get_pmlca(int ctr)
32{
33 u32 pmlca;
34
35 switch (ctr) {
36 case 0:
37 pmlca = mfpmr(PMRN_PMLCA0);
38 break;
39 case 1:
40 pmlca = mfpmr(PMRN_PMLCA1);
41 break;
42 case 2:
43 pmlca = mfpmr(PMRN_PMLCA2);
44 break;
45 case 3:
46 pmlca = mfpmr(PMRN_PMLCA3);
47 break;
Lijun Pan5815c432013-06-05 15:22:09 -050048 case 4:
49 pmlca = mfpmr(PMRN_PMLCA4);
50 break;
51 case 5:
52 pmlca = mfpmr(PMRN_PMLCA5);
53 break;
Olof Johanssonc69b7672007-01-28 21:23:14 -060054 default:
55 panic("Bad ctr number\n");
56 }
57
58 return pmlca;
59}
60
61static inline void set_pmlca(int ctr, u32 pmlca)
62{
63 switch (ctr) {
64 case 0:
65 mtpmr(PMRN_PMLCA0, pmlca);
66 break;
67 case 1:
68 mtpmr(PMRN_PMLCA1, pmlca);
69 break;
70 case 2:
71 mtpmr(PMRN_PMLCA2, pmlca);
72 break;
73 case 3:
74 mtpmr(PMRN_PMLCA3, pmlca);
75 break;
Lijun Pan5815c432013-06-05 15:22:09 -050076 case 4:
77 mtpmr(PMRN_PMLCA4, pmlca);
78 break;
79 case 5:
80 mtpmr(PMRN_PMLCA5, pmlca);
81 break;
Olof Johanssonc69b7672007-01-28 21:23:14 -060082 default:
83 panic("Bad ctr number\n");
84 }
85}
86
87static inline unsigned int ctr_read(unsigned int i)
88{
89 switch(i) {
90 case 0:
91 return mfpmr(PMRN_PMC0);
92 case 1:
93 return mfpmr(PMRN_PMC1);
94 case 2:
95 return mfpmr(PMRN_PMC2);
96 case 3:
97 return mfpmr(PMRN_PMC3);
Lijun Pan5815c432013-06-05 15:22:09 -050098 case 4:
99 return mfpmr(PMRN_PMC4);
100 case 5:
101 return mfpmr(PMRN_PMC5);
Olof Johanssonc69b7672007-01-28 21:23:14 -0600102 default:
103 return 0;
104 }
105}
106
107static inline void ctr_write(unsigned int i, unsigned int val)
108{
109 switch(i) {
110 case 0:
111 mtpmr(PMRN_PMC0, val);
112 break;
113 case 1:
114 mtpmr(PMRN_PMC1, val);
115 break;
116 case 2:
117 mtpmr(PMRN_PMC2, val);
118 break;
119 case 3:
120 mtpmr(PMRN_PMC3, val);
121 break;
Lijun Pan5815c432013-06-05 15:22:09 -0500122 case 4:
123 mtpmr(PMRN_PMC4, val);
124 break;
125 case 5:
126 mtpmr(PMRN_PMC5, val);
127 break;
Olof Johanssonc69b7672007-01-28 21:23:14 -0600128 default:
129 break;
130 }
131}
132
133
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500134static void init_pmc_stop(int ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500136 u32 pmlca = (PMLCA_FC | PMLCA_FCS | PMLCA_FCU |
137 PMLCA_FCM1 | PMLCA_FCM0);
138 u32 pmlcb = 0;
139
140 switch (ctr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 case 0:
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500142 mtpmr(PMRN_PMLCA0, pmlca);
143 mtpmr(PMRN_PMLCB0, pmlcb);
144 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 case 1:
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500146 mtpmr(PMRN_PMLCA1, pmlca);
147 mtpmr(PMRN_PMLCB1, pmlcb);
148 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 case 2:
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500150 mtpmr(PMRN_PMLCA2, pmlca);
151 mtpmr(PMRN_PMLCB2, pmlcb);
152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 case 3:
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500154 mtpmr(PMRN_PMLCA3, pmlca);
155 mtpmr(PMRN_PMLCB3, pmlcb);
156 break;
Lijun Pan5815c432013-06-05 15:22:09 -0500157 case 4:
158 mtpmr(PMRN_PMLCA4, pmlca);
159 mtpmr(PMRN_PMLCB4, pmlcb);
160 break;
161 case 5:
162 mtpmr(PMRN_PMLCA5, pmlca);
163 mtpmr(PMRN_PMLCB5, pmlcb);
164 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 default:
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500166 panic("Bad ctr number!\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168}
169
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500170static void set_pmc_event(int ctr, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171{
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500172 u32 pmlca;
173
174 pmlca = get_pmlca(ctr);
175
176 pmlca = (pmlca & ~PMLCA_EVENT_MASK) |
177 ((event << PMLCA_EVENT_SHIFT) &
178 PMLCA_EVENT_MASK);
179
180 set_pmlca(ctr, pmlca);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500183static void set_pmc_user_kernel(int ctr, int user, int kernel)
184{
185 u32 pmlca;
186
187 pmlca = get_pmlca(ctr);
188
189 if(user)
190 pmlca &= ~PMLCA_FCU;
191 else
192 pmlca |= PMLCA_FCU;
193
194 if(kernel)
195 pmlca &= ~PMLCA_FCS;
196 else
197 pmlca |= PMLCA_FCS;
198
199 set_pmlca(ctr, pmlca);
200}
201
202static void set_pmc_marked(int ctr, int mark0, int mark1)
203{
204 u32 pmlca = get_pmlca(ctr);
205
206 if(mark0)
207 pmlca &= ~PMLCA_FCM0;
208 else
209 pmlca |= PMLCA_FCM0;
210
211 if(mark1)
212 pmlca &= ~PMLCA_FCM1;
213 else
214 pmlca |= PMLCA_FCM1;
215
216 set_pmlca(ctr, pmlca);
217}
218
219static void pmc_start_ctr(int ctr, int enable)
220{
221 u32 pmlca = get_pmlca(ctr);
222
223 pmlca &= ~PMLCA_FC;
224
225 if (enable)
226 pmlca |= PMLCA_CE;
227 else
228 pmlca &= ~PMLCA_CE;
229
230 set_pmlca(ctr, pmlca);
231}
232
233static void pmc_start_ctrs(int enable)
234{
235 u32 pmgc0 = mfpmr(PMRN_PMGC0);
236
237 pmgc0 &= ~PMGC0_FAC;
238 pmgc0 |= PMGC0_FCECE;
239
240 if (enable)
241 pmgc0 |= PMGC0_PMIE;
242 else
243 pmgc0 &= ~PMGC0_PMIE;
244
245 mtpmr(PMRN_PMGC0, pmgc0);
246}
247
248static void pmc_stop_ctrs(void)
249{
250 u32 pmgc0 = mfpmr(PMRN_PMGC0);
251
252 pmgc0 |= PMGC0_FAC;
253
254 pmgc0 &= ~(PMGC0_PMIE | PMGC0_FCECE);
255
256 mtpmr(PMRN_PMGC0, pmgc0);
257}
258
Andy Fleming39aef682008-02-04 18:27:55 -0600259static int fsl_emb_cpu_setup(struct op_counter_config *ctr)
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500260{
261 int i;
262
263 /* freeze all counters */
264 pmc_stop_ctrs();
265
266 for (i = 0;i < num_counters;i++) {
267 init_pmc_stop(i);
268
269 set_pmc_event(i, ctr[i].event);
270
271 set_pmc_user_kernel(i, ctr[i].user, ctr[i].kernel);
272 }
Bob Nelson14748552007-07-20 21:39:53 +0200273
274 return 0;
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500275}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Andy Fleming39aef682008-02-04 18:27:55 -0600277static int fsl_emb_reg_setup(struct op_counter_config *ctr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct op_system_config *sys,
279 int num_ctrs)
280{
281 int i;
282
283 num_counters = num_ctrs;
284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 /* 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 */
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500290 for (i = 0; i < num_counters; ++i)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 reset_value[i] = 0x80000000UL - ctr[i].count;
292
Bob Nelson14748552007-07-20 21:39:53 +0200293 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294}
295
Andy Fleming39aef682008-02-04 18:27:55 -0600296static int fsl_emb_start(struct op_counter_config *ctr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 int i;
299
300 mtmsr(mfmsr() | MSR_PMM);
301
302 for (i = 0; i < num_counters; ++i) {
303 if (ctr[i].enabled) {
304 ctr_write(i, reset_value[i]);
Andy Flemingdd6c89f2006-10-27 15:06:32 -0500305 /* Set each enabled counter to only
306 * count when the Mark bit is *not* set */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 set_pmc_marked(i, 1, 0);
308 pmc_start_ctr(i, 1);
309 } else {
310 ctr_write(i, 0);
311
312 /* Set the ctr to be stopped */
313 pmc_start_ctr(i, 0);
314 }
315 }
316
317 /* Clear the freeze bit, and enable the interrupt.
318 * The counters won't actually start until the rfi clears
319 * the PMM bit */
320 pmc_start_ctrs(1);
321
322 oprofile_running = 1;
323
324 pr_debug("start on cpu %d, pmgc0 %x\n", smp_processor_id(),
325 mfpmr(PMRN_PMGC0));
Bob Nelson14748552007-07-20 21:39:53 +0200326
327 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
Andy Fleming39aef682008-02-04 18:27:55 -0600330static void fsl_emb_stop(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 /* freeze counters */
333 pmc_stop_ctrs();
334
335 oprofile_running = 0;
336
337 pr_debug("stop on cpu %d, pmgc0 %x\n", smp_processor_id(),
338 mfpmr(PMRN_PMGC0));
339
340 mb();
341}
342
343
Andy Fleming39aef682008-02-04 18:27:55 -0600344static void fsl_emb_handle_interrupt(struct pt_regs *regs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 struct op_counter_config *ctr)
346{
347 unsigned long pc;
348 int is_kernel;
349 int val;
350 int i;
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 pc = regs->nip;
Anton Blanchardfa465f82006-03-27 12:03:17 +1100353 is_kernel = is_kernel_addr(pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 for (i = 0; i < num_counters; ++i) {
356 val = ctr_read(i);
357 if (val < 0) {
358 if (oprofile_running && ctr[i].enabled) {
Anton Blanchardfa465f82006-03-27 12:03:17 +1100359 oprofile_add_ext_sample(pc, regs, i, is_kernel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 ctr_write(i, reset_value[i]);
361 } else {
362 ctr_write(i, 0);
363 }
364 }
365 }
366
367 /* The freeze bit was set by the interrupt. */
Scott Wood4267ea72010-05-19 15:32:21 -0500368 /* 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
372 * environments.
373 */
374 mtmsr(mfmsr() | MSR_PMM);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 pmc_start_ctrs(1);
376}
377
Andy Fleming39aef682008-02-04 18:27:55 -0600378struct 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,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384};