Path: blob/master/arch/powerpc/oprofile/op_model_fsl_emb.c
10818 views
/*1* Freescale Embedded oprofile support, based on ppc64 oprofile support2* Copyright (C) 2004 Anton Blanchard <[email protected]>, IBM3*4* Copyright (c) 2004, 2010 Freescale Semiconductor, Inc5*6* Author: Andy Fleming7* Maintainer: Kumar Gala <[email protected]>8*9* This program is free software; you can redistribute it and/or10* modify it under the terms of the GNU General Public License11* as published by the Free Software Foundation; either version12* 2 of the License, or (at your option) any later version.13*/1415#include <linux/oprofile.h>16#include <linux/init.h>17#include <linux/smp.h>18#include <asm/ptrace.h>19#include <asm/system.h>20#include <asm/processor.h>21#include <asm/cputable.h>22#include <asm/reg_fsl_emb.h>23#include <asm/page.h>24#include <asm/pmc.h>25#include <asm/oprofile_impl.h>2627static unsigned long reset_value[OP_MAX_COUNTER];2829static int num_counters;30static int oprofile_running;3132static inline u32 get_pmlca(int ctr)33{34u32 pmlca;3536switch (ctr) {37case 0:38pmlca = mfpmr(PMRN_PMLCA0);39break;40case 1:41pmlca = mfpmr(PMRN_PMLCA1);42break;43case 2:44pmlca = mfpmr(PMRN_PMLCA2);45break;46case 3:47pmlca = mfpmr(PMRN_PMLCA3);48break;49default:50panic("Bad ctr number\n");51}5253return pmlca;54}5556static inline void set_pmlca(int ctr, u32 pmlca)57{58switch (ctr) {59case 0:60mtpmr(PMRN_PMLCA0, pmlca);61break;62case 1:63mtpmr(PMRN_PMLCA1, pmlca);64break;65case 2:66mtpmr(PMRN_PMLCA2, pmlca);67break;68case 3:69mtpmr(PMRN_PMLCA3, pmlca);70break;71default:72panic("Bad ctr number\n");73}74}7576static inline unsigned int ctr_read(unsigned int i)77{78switch(i) {79case 0:80return mfpmr(PMRN_PMC0);81case 1:82return mfpmr(PMRN_PMC1);83case 2:84return mfpmr(PMRN_PMC2);85case 3:86return mfpmr(PMRN_PMC3);87default:88return 0;89}90}9192static inline void ctr_write(unsigned int i, unsigned int val)93{94switch(i) {95case 0:96mtpmr(PMRN_PMC0, val);97break;98case 1:99mtpmr(PMRN_PMC1, val);100break;101case 2:102mtpmr(PMRN_PMC2, val);103break;104case 3:105mtpmr(PMRN_PMC3, val);106break;107default:108break;109}110}111112113static void init_pmc_stop(int ctr)114{115u32 pmlca = (PMLCA_FC | PMLCA_FCS | PMLCA_FCU |116PMLCA_FCM1 | PMLCA_FCM0);117u32 pmlcb = 0;118119switch (ctr) {120case 0:121mtpmr(PMRN_PMLCA0, pmlca);122mtpmr(PMRN_PMLCB0, pmlcb);123break;124case 1:125mtpmr(PMRN_PMLCA1, pmlca);126mtpmr(PMRN_PMLCB1, pmlcb);127break;128case 2:129mtpmr(PMRN_PMLCA2, pmlca);130mtpmr(PMRN_PMLCB2, pmlcb);131break;132case 3:133mtpmr(PMRN_PMLCA3, pmlca);134mtpmr(PMRN_PMLCB3, pmlcb);135break;136default:137panic("Bad ctr number!\n");138}139}140141static void set_pmc_event(int ctr, int event)142{143u32 pmlca;144145pmlca = get_pmlca(ctr);146147pmlca = (pmlca & ~PMLCA_EVENT_MASK) |148((event << PMLCA_EVENT_SHIFT) &149PMLCA_EVENT_MASK);150151set_pmlca(ctr, pmlca);152}153154static void set_pmc_user_kernel(int ctr, int user, int kernel)155{156u32 pmlca;157158pmlca = get_pmlca(ctr);159160if(user)161pmlca &= ~PMLCA_FCU;162else163pmlca |= PMLCA_FCU;164165if(kernel)166pmlca &= ~PMLCA_FCS;167else168pmlca |= PMLCA_FCS;169170set_pmlca(ctr, pmlca);171}172173static void set_pmc_marked(int ctr, int mark0, int mark1)174{175u32 pmlca = get_pmlca(ctr);176177if(mark0)178pmlca &= ~PMLCA_FCM0;179else180pmlca |= PMLCA_FCM0;181182if(mark1)183pmlca &= ~PMLCA_FCM1;184else185pmlca |= PMLCA_FCM1;186187set_pmlca(ctr, pmlca);188}189190static void pmc_start_ctr(int ctr, int enable)191{192u32 pmlca = get_pmlca(ctr);193194pmlca &= ~PMLCA_FC;195196if (enable)197pmlca |= PMLCA_CE;198else199pmlca &= ~PMLCA_CE;200201set_pmlca(ctr, pmlca);202}203204static void pmc_start_ctrs(int enable)205{206u32 pmgc0 = mfpmr(PMRN_PMGC0);207208pmgc0 &= ~PMGC0_FAC;209pmgc0 |= PMGC0_FCECE;210211if (enable)212pmgc0 |= PMGC0_PMIE;213else214pmgc0 &= ~PMGC0_PMIE;215216mtpmr(PMRN_PMGC0, pmgc0);217}218219static void pmc_stop_ctrs(void)220{221u32 pmgc0 = mfpmr(PMRN_PMGC0);222223pmgc0 |= PMGC0_FAC;224225pmgc0 &= ~(PMGC0_PMIE | PMGC0_FCECE);226227mtpmr(PMRN_PMGC0, pmgc0);228}229230static int fsl_emb_cpu_setup(struct op_counter_config *ctr)231{232int i;233234/* freeze all counters */235pmc_stop_ctrs();236237for (i = 0;i < num_counters;i++) {238init_pmc_stop(i);239240set_pmc_event(i, ctr[i].event);241242set_pmc_user_kernel(i, ctr[i].user, ctr[i].kernel);243}244245return 0;246}247248static int fsl_emb_reg_setup(struct op_counter_config *ctr,249struct op_system_config *sys,250int num_ctrs)251{252int i;253254num_counters = num_ctrs;255256/* Our counters count up, and "count" refers to257* how much before the next interrupt, and we interrupt258* on overflow. So we calculate the starting value259* which will give us "count" until overflow.260* Then we set the events on the enabled counters */261for (i = 0; i < num_counters; ++i)262reset_value[i] = 0x80000000UL - ctr[i].count;263264return 0;265}266267static int fsl_emb_start(struct op_counter_config *ctr)268{269int i;270271mtmsr(mfmsr() | MSR_PMM);272273for (i = 0; i < num_counters; ++i) {274if (ctr[i].enabled) {275ctr_write(i, reset_value[i]);276/* Set each enabled counter to only277* count when the Mark bit is *not* set */278set_pmc_marked(i, 1, 0);279pmc_start_ctr(i, 1);280} else {281ctr_write(i, 0);282283/* Set the ctr to be stopped */284pmc_start_ctr(i, 0);285}286}287288/* Clear the freeze bit, and enable the interrupt.289* The counters won't actually start until the rfi clears290* the PMM bit */291pmc_start_ctrs(1);292293oprofile_running = 1;294295pr_debug("start on cpu %d, pmgc0 %x\n", smp_processor_id(),296mfpmr(PMRN_PMGC0));297298return 0;299}300301static void fsl_emb_stop(void)302{303/* freeze counters */304pmc_stop_ctrs();305306oprofile_running = 0;307308pr_debug("stop on cpu %d, pmgc0 %x\n", smp_processor_id(),309mfpmr(PMRN_PMGC0));310311mb();312}313314315static void fsl_emb_handle_interrupt(struct pt_regs *regs,316struct op_counter_config *ctr)317{318unsigned long pc;319int is_kernel;320int val;321int i;322323pc = regs->nip;324is_kernel = is_kernel_addr(pc);325326for (i = 0; i < num_counters; ++i) {327val = ctr_read(i);328if (val < 0) {329if (oprofile_running && ctr[i].enabled) {330oprofile_add_ext_sample(pc, regs, i, is_kernel);331ctr_write(i, reset_value[i]);332} else {333ctr_write(i, 0);334}335}336}337338/* The freeze bit was set by the interrupt. */339/* Clear the freeze bit, and reenable the interrupt. The340* counters won't actually start until the rfi clears the PMM341* bit. The PMM bit should not be set until after the interrupt342* is cleared to avoid it getting lost in some hypervisor343* environments.344*/345mtmsr(mfmsr() | MSR_PMM);346pmc_start_ctrs(1);347}348349struct op_powerpc_model op_model_fsl_emb = {350.reg_setup = fsl_emb_reg_setup,351.cpu_setup = fsl_emb_cpu_setup,352.start = fsl_emb_start,353.stop = fsl_emb_stop,354.handle_interrupt = fsl_emb_handle_interrupt,355};356357358