Path: blob/master/arch/powerpc/oprofile/op_model_cell.c
10817 views
/*1* Cell Broadband Engine OProfile Support2*3* (C) Copyright IBM Corporation 20064*5* Author: David Erb ([email protected])6* Modifications:7* Carl Love <[email protected]>8* Maynard Johnson <[email protected]>9*10* This program is free software; you can redistribute it and/or11* modify it under the terms of the GNU General Public License12* as published by the Free Software Foundation; either version13* 2 of the License, or (at your option) any later version.14*/1516#include <linux/cpufreq.h>17#include <linux/delay.h>18#include <linux/init.h>19#include <linux/jiffies.h>20#include <linux/kthread.h>21#include <linux/oprofile.h>22#include <linux/percpu.h>23#include <linux/smp.h>24#include <linux/spinlock.h>25#include <linux/timer.h>26#include <asm/cell-pmu.h>27#include <asm/cputable.h>28#include <asm/firmware.h>29#include <asm/io.h>30#include <asm/oprofile_impl.h>31#include <asm/processor.h>32#include <asm/prom.h>33#include <asm/ptrace.h>34#include <asm/reg.h>35#include <asm/rtas.h>36#include <asm/system.h>37#include <asm/cell-regs.h>3839#include "../platforms/cell/interrupt.h"40#include "cell/pr_util.h"4142#define PPU_PROFILING 043#define SPU_PROFILING_CYCLES 144#define SPU_PROFILING_EVENTS 24546#define SPU_EVENT_NUM_START 410047#define SPU_EVENT_NUM_STOP 439948#define SPU_PROFILE_EVENT_ADDR 4363 /* spu, address trace, decimal */49#define SPU_PROFILE_EVENT_ADDR_MASK_A 0x146 /* sub unit set to zero */50#define SPU_PROFILE_EVENT_ADDR_MASK_B 0x186 /* sub unit set to zero */5152#define NUM_SPUS_PER_NODE 853#define SPU_CYCLES_EVENT_NUM 2 /* event number for SPU_CYCLES */5455#define PPU_CYCLES_EVENT_NUM 1 /* event number for CYCLES */56#define PPU_CYCLES_GRP_NUM 1 /* special group number for identifying57* PPU_CYCLES event58*/59#define CBE_COUNT_ALL_CYCLES 0x42800000 /* PPU cycle event specifier */6061#define NUM_THREADS 2 /* number of physical threads in62* physical processor63*/64#define NUM_DEBUG_BUS_WORDS 465#define NUM_INPUT_BUS_WORDS 26667#define MAX_SPU_COUNT 0xFFFFFF /* maximum 24 bit LFSR value */6869/* Minimum HW interval timer setting to send value to trace buffer is 10 cycle.70* To configure counter to send value every N cycles set counter to71* 2^32 - 1 - N.72*/73#define NUM_INTERVAL_CYC 0xFFFFFFFF - 107475/*76* spu_cycle_reset is the number of cycles between samples.77* This variable is used for SPU profiling and should ONLY be set78* at the beginning of cell_reg_setup; otherwise, it's read-only.79*/80static unsigned int spu_cycle_reset;81static unsigned int profiling_mode;82static int spu_evnt_phys_spu_indx;8384struct pmc_cntrl_data {85unsigned long vcntr;86unsigned long evnts;87unsigned long masks;88unsigned long enabled;89};9091/*92* ibm,cbe-perftools rtas parameters93*/94struct pm_signal {95u16 cpu; /* Processor to modify */96u16 sub_unit; /* hw subunit this applies to (if applicable)*/97short int signal_group; /* Signal Group to Enable/Disable */98u8 bus_word; /* Enable/Disable on this Trace/Trigger/Event99* Bus Word(s) (bitmask)100*/101u8 bit; /* Trigger/Event bit (if applicable) */102};103104/*105* rtas call arguments106*/107enum {108SUBFUNC_RESET = 1,109SUBFUNC_ACTIVATE = 2,110SUBFUNC_DEACTIVATE = 3,111112PASSTHRU_IGNORE = 0,113PASSTHRU_ENABLE = 1,114PASSTHRU_DISABLE = 2,115};116117struct pm_cntrl {118u16 enable;119u16 stop_at_max;120u16 trace_mode;121u16 freeze;122u16 count_mode;123u16 spu_addr_trace;124u8 trace_buf_ovflw;125};126127static struct {128u32 group_control;129u32 debug_bus_control;130struct pm_cntrl pm_cntrl;131u32 pm07_cntrl[NR_PHYS_CTRS];132} pm_regs;133134#define GET_SUB_UNIT(x) ((x & 0x0000f000) >> 12)135#define GET_BUS_WORD(x) ((x & 0x000000f0) >> 4)136#define GET_BUS_TYPE(x) ((x & 0x00000300) >> 8)137#define GET_POLARITY(x) ((x & 0x00000002) >> 1)138#define GET_COUNT_CYCLES(x) (x & 0x00000001)139#define GET_INPUT_CONTROL(x) ((x & 0x00000004) >> 2)140141static DEFINE_PER_CPU(unsigned long[NR_PHYS_CTRS], pmc_values);142static unsigned long spu_pm_cnt[MAX_NUMNODES * NUM_SPUS_PER_NODE];143static struct pmc_cntrl_data pmc_cntrl[NUM_THREADS][NR_PHYS_CTRS];144145/*146* The CELL profiling code makes rtas calls to setup the debug bus to147* route the performance signals. Additionally, SPU profiling requires148* a second rtas call to setup the hardware to capture the SPU PCs.149* The EIO error value is returned if the token lookups or the rtas150* call fail. The EIO error number is the best choice of the existing151* error numbers. The probability of rtas related error is very low. But152* by returning EIO and printing additional information to dmsg the user153* will know that OProfile did not start and dmesg will tell them why.154* OProfile does not support returning errors on Stop. Not a huge issue155* since failure to reset the debug bus or stop the SPU PC collection is156* not a fatel issue. Chances are if the Stop failed, Start doesn't work157* either.158*/159160/*161* Interpetation of hdw_thread:162* 0 - even virtual cpus 0, 2, 4,...163* 1 - odd virtual cpus 1, 3, 5, ...164*165* FIXME: this is strictly wrong, we need to clean this up in a number166* of places. It works for now. -arnd167*/168static u32 hdw_thread;169170static u32 virt_cntr_inter_mask;171static struct timer_list timer_virt_cntr;172static struct timer_list timer_spu_event_swap;173174/*175* pm_signal needs to be global since it is initialized in176* cell_reg_setup at the time when the necessary information177* is available.178*/179static struct pm_signal pm_signal[NR_PHYS_CTRS];180static int pm_rtas_token; /* token for debug bus setup call */181static int spu_rtas_token; /* token for SPU cycle profiling */182183static u32 reset_value[NR_PHYS_CTRS];184static int num_counters;185static int oprofile_running;186static DEFINE_SPINLOCK(cntr_lock);187188static u32 ctr_enabled;189190static unsigned char input_bus[NUM_INPUT_BUS_WORDS];191192/*193* Firmware interface functions194*/195static int196rtas_ibm_cbe_perftools(int subfunc, int passthru,197void *address, unsigned long length)198{199u64 paddr = __pa(address);200201return rtas_call(pm_rtas_token, 5, 1, NULL, subfunc,202passthru, paddr >> 32, paddr & 0xffffffff, length);203}204205static void pm_rtas_reset_signals(u32 node)206{207int ret;208struct pm_signal pm_signal_local;209210/*211* The debug bus is being set to the passthru disable state.212* However, the FW still expects atleast one legal signal routing213* entry or it will return an error on the arguments. If we don't214* supply a valid entry, we must ignore all return values. Ignoring215* all return values means we might miss an error we should be216* concerned about.217*/218219/* fw expects physical cpu #. */220pm_signal_local.cpu = node;221pm_signal_local.signal_group = 21;222pm_signal_local.bus_word = 1;223pm_signal_local.sub_unit = 0;224pm_signal_local.bit = 0;225226ret = rtas_ibm_cbe_perftools(SUBFUNC_RESET, PASSTHRU_DISABLE,227&pm_signal_local,228sizeof(struct pm_signal));229230if (unlikely(ret))231/*232* Not a fatal error. For Oprofile stop, the oprofile233* functions do not support returning an error for234* failure to stop OProfile.235*/236printk(KERN_WARNING "%s: rtas returned: %d\n",237__func__, ret);238}239240static int pm_rtas_activate_signals(u32 node, u32 count)241{242int ret;243int i, j;244struct pm_signal pm_signal_local[NR_PHYS_CTRS];245246/*247* There is no debug setup required for the cycles event.248* Note that only events in the same group can be used.249* Otherwise, there will be conflicts in correctly routing250* the signals on the debug bus. It is the responsibility251* of the OProfile user tool to check the events are in252* the same group.253*/254i = 0;255for (j = 0; j < count; j++) {256if (pm_signal[j].signal_group != PPU_CYCLES_GRP_NUM) {257258/* fw expects physical cpu # */259pm_signal_local[i].cpu = node;260pm_signal_local[i].signal_group261= pm_signal[j].signal_group;262pm_signal_local[i].bus_word = pm_signal[j].bus_word;263pm_signal_local[i].sub_unit = pm_signal[j].sub_unit;264pm_signal_local[i].bit = pm_signal[j].bit;265i++;266}267}268269if (i != 0) {270ret = rtas_ibm_cbe_perftools(SUBFUNC_ACTIVATE, PASSTHRU_ENABLE,271pm_signal_local,272i * sizeof(struct pm_signal));273274if (unlikely(ret)) {275printk(KERN_WARNING "%s: rtas returned: %d\n",276__func__, ret);277return -EIO;278}279}280281return 0;282}283284/*285* PM Signal functions286*/287static void set_pm_event(u32 ctr, int event, u32 unit_mask)288{289struct pm_signal *p;290u32 signal_bit;291u32 bus_word, bus_type, count_cycles, polarity, input_control;292int j, i;293294if (event == PPU_CYCLES_EVENT_NUM) {295/* Special Event: Count all cpu cycles */296pm_regs.pm07_cntrl[ctr] = CBE_COUNT_ALL_CYCLES;297p = &(pm_signal[ctr]);298p->signal_group = PPU_CYCLES_GRP_NUM;299p->bus_word = 1;300p->sub_unit = 0;301p->bit = 0;302goto out;303} else {304pm_regs.pm07_cntrl[ctr] = 0;305}306307bus_word = GET_BUS_WORD(unit_mask);308bus_type = GET_BUS_TYPE(unit_mask);309count_cycles = GET_COUNT_CYCLES(unit_mask);310polarity = GET_POLARITY(unit_mask);311input_control = GET_INPUT_CONTROL(unit_mask);312signal_bit = (event % 100);313314p = &(pm_signal[ctr]);315316p->signal_group = event / 100;317p->bus_word = bus_word;318p->sub_unit = GET_SUB_UNIT(unit_mask);319320pm_regs.pm07_cntrl[ctr] = 0;321pm_regs.pm07_cntrl[ctr] |= PM07_CTR_COUNT_CYCLES(count_cycles);322pm_regs.pm07_cntrl[ctr] |= PM07_CTR_POLARITY(polarity);323pm_regs.pm07_cntrl[ctr] |= PM07_CTR_INPUT_CONTROL(input_control);324325/*326* Some of the islands signal selection is based on 64 bit words.327* The debug bus words are 32 bits, the input words to the performance328* counters are defined as 32 bits. Need to convert the 64 bit island329* specification to the appropriate 32 input bit and bus word for the330* performance counter event selection. See the CELL Performance331* monitoring signals manual and the Perf cntr hardware descriptions332* for the details.333*/334if (input_control == 0) {335if (signal_bit > 31) {336signal_bit -= 32;337if (bus_word == 0x3)338bus_word = 0x2;339else if (bus_word == 0xc)340bus_word = 0x8;341}342343if ((bus_type == 0) && p->signal_group >= 60)344bus_type = 2;345if ((bus_type == 1) && p->signal_group >= 50)346bus_type = 0;347348pm_regs.pm07_cntrl[ctr] |= PM07_CTR_INPUT_MUX(signal_bit);349} else {350pm_regs.pm07_cntrl[ctr] = 0;351p->bit = signal_bit;352}353354for (i = 0; i < NUM_DEBUG_BUS_WORDS; i++) {355if (bus_word & (1 << i)) {356pm_regs.debug_bus_control |=357(bus_type << (30 - (2 * i)));358359for (j = 0; j < NUM_INPUT_BUS_WORDS; j++) {360if (input_bus[j] == 0xff) {361input_bus[j] = i;362pm_regs.group_control |=363(i << (30 - (2 * j)));364365break;366}367}368}369}370out:371;372}373374static void write_pm_cntrl(int cpu)375{376/*377* Oprofile will use 32 bit counters, set bits 7:10 to 0378* pmregs.pm_cntrl is a global379*/380381u32 val = 0;382if (pm_regs.pm_cntrl.enable == 1)383val |= CBE_PM_ENABLE_PERF_MON;384385if (pm_regs.pm_cntrl.stop_at_max == 1)386val |= CBE_PM_STOP_AT_MAX;387388if (pm_regs.pm_cntrl.trace_mode != 0)389val |= CBE_PM_TRACE_MODE_SET(pm_regs.pm_cntrl.trace_mode);390391if (pm_regs.pm_cntrl.trace_buf_ovflw == 1)392val |= CBE_PM_TRACE_BUF_OVFLW(pm_regs.pm_cntrl.trace_buf_ovflw);393if (pm_regs.pm_cntrl.freeze == 1)394val |= CBE_PM_FREEZE_ALL_CTRS;395396val |= CBE_PM_SPU_ADDR_TRACE_SET(pm_regs.pm_cntrl.spu_addr_trace);397398/*399* Routine set_count_mode must be called previously to set400* the count mode based on the user selection of user and kernel.401*/402val |= CBE_PM_COUNT_MODE_SET(pm_regs.pm_cntrl.count_mode);403cbe_write_pm(cpu, pm_control, val);404}405406static inline void407set_count_mode(u32 kernel, u32 user)408{409/*410* The user must specify user and kernel if they want them. If411* neither is specified, OProfile will count in hypervisor mode.412* pm_regs.pm_cntrl is a global413*/414if (kernel) {415if (user)416pm_regs.pm_cntrl.count_mode = CBE_COUNT_ALL_MODES;417else418pm_regs.pm_cntrl.count_mode =419CBE_COUNT_SUPERVISOR_MODE;420} else {421if (user)422pm_regs.pm_cntrl.count_mode = CBE_COUNT_PROBLEM_MODE;423else424pm_regs.pm_cntrl.count_mode =425CBE_COUNT_HYPERVISOR_MODE;426}427}428429static inline void enable_ctr(u32 cpu, u32 ctr, u32 *pm07_cntrl)430{431432pm07_cntrl[ctr] |= CBE_PM_CTR_ENABLE;433cbe_write_pm07_control(cpu, ctr, pm07_cntrl[ctr]);434}435436/*437* Oprofile is expected to collect data on all CPUs simultaneously.438* However, there is one set of performance counters per node. There are439* two hardware threads or virtual CPUs on each node. Hence, OProfile must440* multiplex in time the performance counter collection on the two virtual441* CPUs. The multiplexing of the performance counters is done by this442* virtual counter routine.443*444* The pmc_values used below is defined as 'per-cpu' but its use is445* more akin to 'per-node'. We need to store two sets of counter446* values per node -- one for the previous run and one for the next.447* The per-cpu[NR_PHYS_CTRS] gives us the storage we need. Each odd/even448* pair of per-cpu arrays is used for storing the previous and next449* pmc values for a given node.450* NOTE: We use the per-cpu variable to improve cache performance.451*452* This routine will alternate loading the virtual counters for453* virtual CPUs454*/455static void cell_virtual_cntr(unsigned long data)456{457int i, prev_hdw_thread, next_hdw_thread;458u32 cpu;459unsigned long flags;460461/*462* Make sure that the interrupt_hander and the virt counter are463* not both playing with the counters on the same node.464*/465466spin_lock_irqsave(&cntr_lock, flags);467468prev_hdw_thread = hdw_thread;469470/* switch the cpu handling the interrupts */471hdw_thread = 1 ^ hdw_thread;472next_hdw_thread = hdw_thread;473474pm_regs.group_control = 0;475pm_regs.debug_bus_control = 0;476477for (i = 0; i < NUM_INPUT_BUS_WORDS; i++)478input_bus[i] = 0xff;479480/*481* There are some per thread events. Must do the482* set event, for the thread that is being started483*/484for (i = 0; i < num_counters; i++)485set_pm_event(i,486pmc_cntrl[next_hdw_thread][i].evnts,487pmc_cntrl[next_hdw_thread][i].masks);488489/*490* The following is done only once per each node, but491* we need cpu #, not node #, to pass to the cbe_xxx functions.492*/493for_each_online_cpu(cpu) {494if (cbe_get_hw_thread_id(cpu))495continue;496497/*498* stop counters, save counter values, restore counts499* for previous thread500*/501cbe_disable_pm(cpu);502cbe_disable_pm_interrupts(cpu);503for (i = 0; i < num_counters; i++) {504per_cpu(pmc_values, cpu + prev_hdw_thread)[i]505= cbe_read_ctr(cpu, i);506507if (per_cpu(pmc_values, cpu + next_hdw_thread)[i]508== 0xFFFFFFFF)509/* If the cntr value is 0xffffffff, we must510* reset that to 0xfffffff0 when the current511* thread is restarted. This will generate a512* new interrupt and make sure that we never513* restore the counters to the max value. If514* the counters were restored to the max value,515* they do not increment and no interrupts are516* generated. Hence no more samples will be517* collected on that cpu.518*/519cbe_write_ctr(cpu, i, 0xFFFFFFF0);520else521cbe_write_ctr(cpu, i,522per_cpu(pmc_values,523cpu +524next_hdw_thread)[i]);525}526527/*528* Switch to the other thread. Change the interrupt529* and control regs to be scheduled on the CPU530* corresponding to the thread to execute.531*/532for (i = 0; i < num_counters; i++) {533if (pmc_cntrl[next_hdw_thread][i].enabled) {534/*535* There are some per thread events.536* Must do the set event, enable_cntr537* for each cpu.538*/539enable_ctr(cpu, i,540pm_regs.pm07_cntrl);541} else {542cbe_write_pm07_control(cpu, i, 0);543}544}545546/* Enable interrupts on the CPU thread that is starting */547cbe_enable_pm_interrupts(cpu, next_hdw_thread,548virt_cntr_inter_mask);549cbe_enable_pm(cpu);550}551552spin_unlock_irqrestore(&cntr_lock, flags);553554mod_timer(&timer_virt_cntr, jiffies + HZ / 10);555}556557static void start_virt_cntrs(void)558{559init_timer(&timer_virt_cntr);560timer_virt_cntr.function = cell_virtual_cntr;561timer_virt_cntr.data = 0UL;562timer_virt_cntr.expires = jiffies + HZ / 10;563add_timer(&timer_virt_cntr);564}565566static int cell_reg_setup_spu_cycles(struct op_counter_config *ctr,567struct op_system_config *sys, int num_ctrs)568{569spu_cycle_reset = ctr[0].count;570571/*572* Each node will need to make the rtas call to start573* and stop SPU profiling. Get the token once and store it.574*/575spu_rtas_token = rtas_token("ibm,cbe-spu-perftools");576577if (unlikely(spu_rtas_token == RTAS_UNKNOWN_SERVICE)) {578printk(KERN_ERR579"%s: rtas token ibm,cbe-spu-perftools unknown\n",580__func__);581return -EIO;582}583return 0;584}585586/* Unfortunately, the hardware will only support event profiling587* on one SPU per node at a time. Therefore, we must time slice588* the profiling across all SPUs in the node. Note, we do this589* in parallel for each node. The following routine is called590* periodically based on kernel timer to switch which SPU is591* being monitored in a round robbin fashion.592*/593static void spu_evnt_swap(unsigned long data)594{595int node;596int cur_phys_spu, nxt_phys_spu, cur_spu_evnt_phys_spu_indx;597unsigned long flags;598int cpu;599int ret;600u32 interrupt_mask;601602603/* enable interrupts on cntr 0 */604interrupt_mask = CBE_PM_CTR_OVERFLOW_INTR(0);605606hdw_thread = 0;607608/* Make sure spu event interrupt handler and spu event swap609* don't access the counters simultaneously.610*/611spin_lock_irqsave(&cntr_lock, flags);612613cur_spu_evnt_phys_spu_indx = spu_evnt_phys_spu_indx;614615if (++(spu_evnt_phys_spu_indx) == NUM_SPUS_PER_NODE)616spu_evnt_phys_spu_indx = 0;617618pm_signal[0].sub_unit = spu_evnt_phys_spu_indx;619pm_signal[1].sub_unit = spu_evnt_phys_spu_indx;620pm_signal[2].sub_unit = spu_evnt_phys_spu_indx;621622/* switch the SPU being profiled on each node */623for_each_online_cpu(cpu) {624if (cbe_get_hw_thread_id(cpu))625continue;626627node = cbe_cpu_to_node(cpu);628cur_phys_spu = (node * NUM_SPUS_PER_NODE)629+ cur_spu_evnt_phys_spu_indx;630nxt_phys_spu = (node * NUM_SPUS_PER_NODE)631+ spu_evnt_phys_spu_indx;632633/*634* stop counters, save counter values, restore counts635* for previous physical SPU636*/637cbe_disable_pm(cpu);638cbe_disable_pm_interrupts(cpu);639640spu_pm_cnt[cur_phys_spu]641= cbe_read_ctr(cpu, 0);642643/* restore previous count for the next spu to sample */644/* NOTE, hardware issue, counter will not start if the645* counter value is at max (0xFFFFFFFF).646*/647if (spu_pm_cnt[nxt_phys_spu] >= 0xFFFFFFFF)648cbe_write_ctr(cpu, 0, 0xFFFFFFF0);649else650cbe_write_ctr(cpu, 0, spu_pm_cnt[nxt_phys_spu]);651652pm_rtas_reset_signals(cbe_cpu_to_node(cpu));653654/* setup the debug bus measure the one event and655* the two events to route the next SPU's PC on656* the debug bus657*/658ret = pm_rtas_activate_signals(cbe_cpu_to_node(cpu), 3);659if (ret)660printk(KERN_ERR "%s: pm_rtas_activate_signals failed, "661"SPU event swap\n", __func__);662663/* clear the trace buffer, don't want to take PC for664* previous SPU*/665cbe_write_pm(cpu, trace_address, 0);666667enable_ctr(cpu, 0, pm_regs.pm07_cntrl);668669/* Enable interrupts on the CPU thread that is starting */670cbe_enable_pm_interrupts(cpu, hdw_thread,671interrupt_mask);672cbe_enable_pm(cpu);673}674675spin_unlock_irqrestore(&cntr_lock, flags);676677/* swap approximately every 0.1 seconds */678mod_timer(&timer_spu_event_swap, jiffies + HZ / 25);679}680681static void start_spu_event_swap(void)682{683init_timer(&timer_spu_event_swap);684timer_spu_event_swap.function = spu_evnt_swap;685timer_spu_event_swap.data = 0UL;686timer_spu_event_swap.expires = jiffies + HZ / 25;687add_timer(&timer_spu_event_swap);688}689690static int cell_reg_setup_spu_events(struct op_counter_config *ctr,691struct op_system_config *sys, int num_ctrs)692{693int i;694695/* routine is called once for all nodes */696697spu_evnt_phys_spu_indx = 0;698/*699* For all events except PPU CYCLEs, each node will need to make700* the rtas cbe-perftools call to setup and reset the debug bus.701* Make the token lookup call once and store it in the global702* variable pm_rtas_token.703*/704pm_rtas_token = rtas_token("ibm,cbe-perftools");705706if (unlikely(pm_rtas_token == RTAS_UNKNOWN_SERVICE)) {707printk(KERN_ERR708"%s: rtas token ibm,cbe-perftools unknown\n",709__func__);710return -EIO;711}712713/* setup the pm_control register settings,714* settings will be written per node by the715* cell_cpu_setup() function.716*/717pm_regs.pm_cntrl.trace_buf_ovflw = 1;718719/* Use the occurrence trace mode to have SPU PC saved720* to the trace buffer. Occurrence data in trace buffer721* is not used. Bit 2 must be set to store SPU addresses.722*/723pm_regs.pm_cntrl.trace_mode = 2;724725pm_regs.pm_cntrl.spu_addr_trace = 0x1; /* using debug bus726event 2 & 3 */727728/* setup the debug bus event array with the SPU PC routing events.729* Note, pm_signal[0] will be filled in by set_pm_event() call below.730*/731pm_signal[1].signal_group = SPU_PROFILE_EVENT_ADDR / 100;732pm_signal[1].bus_word = GET_BUS_WORD(SPU_PROFILE_EVENT_ADDR_MASK_A);733pm_signal[1].bit = SPU_PROFILE_EVENT_ADDR % 100;734pm_signal[1].sub_unit = spu_evnt_phys_spu_indx;735736pm_signal[2].signal_group = SPU_PROFILE_EVENT_ADDR / 100;737pm_signal[2].bus_word = GET_BUS_WORD(SPU_PROFILE_EVENT_ADDR_MASK_B);738pm_signal[2].bit = SPU_PROFILE_EVENT_ADDR % 100;739pm_signal[2].sub_unit = spu_evnt_phys_spu_indx;740741/* Set the user selected spu event to profile on,742* note, only one SPU profiling event is supported743*/744num_counters = 1; /* Only support one SPU event at a time */745set_pm_event(0, ctr[0].event, ctr[0].unit_mask);746747reset_value[0] = 0xFFFFFFFF - ctr[0].count;748749/* global, used by cell_cpu_setup */750ctr_enabled |= 1;751752/* Initialize the count for each SPU to the reset value */753for (i=0; i < MAX_NUMNODES * NUM_SPUS_PER_NODE; i++)754spu_pm_cnt[i] = reset_value[0];755756return 0;757}758759static int cell_reg_setup_ppu(struct op_counter_config *ctr,760struct op_system_config *sys, int num_ctrs)761{762/* routine is called once for all nodes */763int i, j, cpu;764765num_counters = num_ctrs;766767if (unlikely(num_ctrs > NR_PHYS_CTRS)) {768printk(KERN_ERR769"%s: Oprofile, number of specified events " \770"exceeds number of physical counters\n",771__func__);772return -EIO;773}774775set_count_mode(sys->enable_kernel, sys->enable_user);776777/* Setup the thread 0 events */778for (i = 0; i < num_ctrs; ++i) {779780pmc_cntrl[0][i].evnts = ctr[i].event;781pmc_cntrl[0][i].masks = ctr[i].unit_mask;782pmc_cntrl[0][i].enabled = ctr[i].enabled;783pmc_cntrl[0][i].vcntr = i;784785for_each_possible_cpu(j)786per_cpu(pmc_values, j)[i] = 0;787}788789/*790* Setup the thread 1 events, map the thread 0 event to the791* equivalent thread 1 event.792*/793for (i = 0; i < num_ctrs; ++i) {794if ((ctr[i].event >= 2100) && (ctr[i].event <= 2111))795pmc_cntrl[1][i].evnts = ctr[i].event + 19;796else if (ctr[i].event == 2203)797pmc_cntrl[1][i].evnts = ctr[i].event;798else if ((ctr[i].event >= 2200) && (ctr[i].event <= 2215))799pmc_cntrl[1][i].evnts = ctr[i].event + 16;800else801pmc_cntrl[1][i].evnts = ctr[i].event;802803pmc_cntrl[1][i].masks = ctr[i].unit_mask;804pmc_cntrl[1][i].enabled = ctr[i].enabled;805pmc_cntrl[1][i].vcntr = i;806}807808for (i = 0; i < NUM_INPUT_BUS_WORDS; i++)809input_bus[i] = 0xff;810811/*812* Our counters count up, and "count" refers to813* how much before the next interrupt, and we interrupt814* on overflow. So we calculate the starting value815* which will give us "count" until overflow.816* Then we set the events on the enabled counters.817*/818for (i = 0; i < num_counters; ++i) {819/* start with virtual counter set 0 */820if (pmc_cntrl[0][i].enabled) {821/* Using 32bit counters, reset max - count */822reset_value[i] = 0xFFFFFFFF - ctr[i].count;823set_pm_event(i,824pmc_cntrl[0][i].evnts,825pmc_cntrl[0][i].masks);826827/* global, used by cell_cpu_setup */828ctr_enabled |= (1 << i);829}830}831832/* initialize the previous counts for the virtual cntrs */833for_each_online_cpu(cpu)834for (i = 0; i < num_counters; ++i) {835per_cpu(pmc_values, cpu)[i] = reset_value[i];836}837838return 0;839}840841842/* This function is called once for all cpus combined */843static int cell_reg_setup(struct op_counter_config *ctr,844struct op_system_config *sys, int num_ctrs)845{846int ret=0;847spu_cycle_reset = 0;848849/* initialize the spu_arr_trace value, will be reset if850* doing spu event profiling.851*/852pm_regs.group_control = 0;853pm_regs.debug_bus_control = 0;854pm_regs.pm_cntrl.stop_at_max = 1;855pm_regs.pm_cntrl.trace_mode = 0;856pm_regs.pm_cntrl.freeze = 1;857pm_regs.pm_cntrl.trace_buf_ovflw = 0;858pm_regs.pm_cntrl.spu_addr_trace = 0;859860/*861* For all events except PPU CYCLEs, each node will need to make862* the rtas cbe-perftools call to setup and reset the debug bus.863* Make the token lookup call once and store it in the global864* variable pm_rtas_token.865*/866pm_rtas_token = rtas_token("ibm,cbe-perftools");867868if (unlikely(pm_rtas_token == RTAS_UNKNOWN_SERVICE)) {869printk(KERN_ERR870"%s: rtas token ibm,cbe-perftools unknown\n",871__func__);872return -EIO;873}874875if (ctr[0].event == SPU_CYCLES_EVENT_NUM) {876profiling_mode = SPU_PROFILING_CYCLES;877ret = cell_reg_setup_spu_cycles(ctr, sys, num_ctrs);878} else if ((ctr[0].event >= SPU_EVENT_NUM_START) &&879(ctr[0].event <= SPU_EVENT_NUM_STOP)) {880profiling_mode = SPU_PROFILING_EVENTS;881spu_cycle_reset = ctr[0].count;882883/* for SPU event profiling, need to setup the884* pm_signal array with the events to route the885* SPU PC before making the FW call. Note, only886* one SPU event for profiling can be specified887* at a time.888*/889cell_reg_setup_spu_events(ctr, sys, num_ctrs);890} else {891profiling_mode = PPU_PROFILING;892ret = cell_reg_setup_ppu(ctr, sys, num_ctrs);893}894895return ret;896}897898899900/* This function is called once for each cpu */901static int cell_cpu_setup(struct op_counter_config *cntr)902{903u32 cpu = smp_processor_id();904u32 num_enabled = 0;905int i;906int ret;907908/* Cycle based SPU profiling does not use the performance909* counters. The trace array is configured to collect910* the data.911*/912if (profiling_mode == SPU_PROFILING_CYCLES)913return 0;914915/* There is one performance monitor per processor chip (i.e. node),916* so we only need to perform this function once per node.917*/918if (cbe_get_hw_thread_id(cpu))919return 0;920921/* Stop all counters */922cbe_disable_pm(cpu);923cbe_disable_pm_interrupts(cpu);924925cbe_write_pm(cpu, pm_start_stop, 0);926cbe_write_pm(cpu, group_control, pm_regs.group_control);927cbe_write_pm(cpu, debug_bus_control, pm_regs.debug_bus_control);928write_pm_cntrl(cpu);929930for (i = 0; i < num_counters; ++i) {931if (ctr_enabled & (1 << i)) {932pm_signal[num_enabled].cpu = cbe_cpu_to_node(cpu);933num_enabled++;934}935}936937/*938* The pm_rtas_activate_signals will return -EIO if the FW939* call failed.940*/941if (profiling_mode == SPU_PROFILING_EVENTS) {942/* For SPU event profiling also need to setup the943* pm interval timer944*/945ret = pm_rtas_activate_signals(cbe_cpu_to_node(cpu),946num_enabled+2);947/* store PC from debug bus to Trace buffer as often948* as possible (every 10 cycles)949*/950cbe_write_pm(cpu, pm_interval, NUM_INTERVAL_CYC);951return ret;952} else953return pm_rtas_activate_signals(cbe_cpu_to_node(cpu),954num_enabled);955}956957#define ENTRIES 303958#define MAXLFSR 0xFFFFFF959960/* precomputed table of 24 bit LFSR values */961static int initial_lfsr[] = {9628221349, 12579195, 5379618, 10097839, 7512963, 7519310, 3955098, 10753424,96315507573, 7458917, 285419, 2641121, 9780088, 3915503, 6668768, 1548716,9644885000, 8774424, 9650099, 2044357, 2304411, 9326253, 10332526, 4421547,9653440748, 10179459, 13332843, 10375561, 1313462, 8375100, 5198480, 6071392,9669341783, 1526887, 3985002, 1439429, 13923762, 7010104, 11969769, 4547026,9672040072, 4025602, 3437678, 7939992, 11444177, 4496094, 9803157, 10745556,9683671780, 4257846, 5662259, 13196905, 3237343, 12077182, 16222879, 7587769,96914706824, 2184640, 12591135, 10420257, 7406075, 3648978, 11042541, 15906893,97011914928, 4732944, 10695697, 12928164, 11980531, 4430912, 11939291, 2917017,9716119256, 4172004, 9373765, 8410071, 14788383, 5047459, 5474428, 1737756,97215967514, 13351758, 6691285, 8034329, 2856544, 14394753, 11310160, 12149558,9737487528, 7542781, 15668898, 12525138, 12790975, 3707933, 9106617, 1965401,97416219109, 12801644, 2443203, 4909502, 8762329, 3120803, 6360315, 9309720,97515164599, 10844842, 4456529, 6667610, 14924259, 884312, 6234963, 3326042,97615973422, 13919464, 5272099, 6414643, 3909029, 2764324, 5237926, 4774955,97710445906, 4955302, 5203726, 10798229, 11443419, 2303395, 333836, 9646934,9783464726, 4159182, 568492, 995747, 10318756, 13299332, 4836017, 8237783,9793878992, 2581665, 11394667, 5672745, 14412947, 3159169, 9094251, 16467278,9808671392, 15230076, 4843545, 7009238, 15504095, 1494895, 9627886, 14485051,9818304291, 252817, 12421642, 16085736, 4774072, 2456177, 4160695, 15409741,9824902868, 5793091, 13162925, 16039714, 782255, 11347835, 14884586, 366972,98316308990, 11913488, 13390465, 2958444, 10340278, 1177858, 1319431, 10426302,9842868597, 126119, 5784857, 5245324, 10903900, 16436004, 3389013, 1742384,98514674502, 10279218, 8536112, 10364279, 6877778, 14051163, 1025130, 6072469,9861988305, 8354440, 8216060, 16342977, 13112639, 3976679, 5913576, 8816697,9876879995, 14043764, 3339515, 9364420, 15808858, 12261651, 2141560, 5636398,98810345425, 10414756, 781725, 6155650, 4746914, 5078683, 7469001, 6799140,98910156444, 9667150, 10116470, 4133858, 2121972, 1124204, 1003577, 1611214,99014304602, 16221850, 13878465, 13577744, 3629235, 8772583, 10881308, 2410386,9917300044, 5378855, 9301235, 12755149, 4977682, 8083074, 10327581, 6395087,9929155434, 15501696, 7514362, 14520507, 15808945, 3244584, 4741962, 9658130,99314336147, 8654727, 7969093, 15759799, 14029445, 5038459, 9894848, 8659300,99413699287, 8834306, 10712885, 14753895, 10410465, 3373251, 309501, 9561475,9955526688, 14647426, 14209836, 5339224, 207299, 14069911, 8722990, 2290950,9963258216, 12505185, 6007317, 9218111, 14661019, 10537428, 11731949, 9027003,9976641507, 9490160, 200241, 9720425, 16277895, 10816638, 1554761, 10431375,9987467528, 6790302, 3429078, 14633753, 14428997, 11463204, 3576212, 2003426,9996123687, 820520, 9992513, 15784513, 5778891, 6428165, 83886071000};10011002/*1003* The hardware uses an LFSR counting sequence to determine when to capture1004* the SPU PCs. An LFSR sequence is like a puesdo random number sequence1005* where each number occurs once in the sequence but the sequence is not in1006* numerical order. The SPU PC capture is done when the LFSR sequence reaches1007* the last value in the sequence. Hence the user specified value N1008* corresponds to the LFSR number that is N from the end of the sequence.1009*1010* To avoid the time to compute the LFSR, a lookup table is used. The 24 bit1011* LFSR sequence is broken into four ranges. The spacing of the precomputed1012* values is adjusted in each range so the error between the user specifed1013* number (N) of events between samples and the actual number of events based1014* on the precomputed value will be les then about 6.2%. Note, if the user1015* specifies N < 2^16, the LFSR value that is 2^16 from the end will be used.1016* This is to prevent the loss of samples because the trace buffer is full.1017*1018* User specified N Step between Index in1019* precomputed values precomputed1020* table1021* 0 to 2^16-1 ---- 01022* 2^16 to 2^16+2^19-1 2^12 1 to 1281023* 2^16+2^19 to 2^16+2^19+2^22-1 2^15 129 to 2561024* 2^16+2^19+2^22 to 2^24-1 2^18 257 to 3021025*1026*1027* For example, the LFSR values in the second range are computed for 2^16,1028* 2^16+2^12, ... , 2^19-2^16, 2^19 and stored in the table at indicies1029* 1, 2,..., 127, 128.1030*1031* The 24 bit LFSR value for the nth number in the sequence can be1032* calculated using the following code:1033*1034* #define size 241035* int calculate_lfsr(int n)1036* {1037* int i;1038* unsigned int newlfsr0;1039* unsigned int lfsr = 0xFFFFFF;1040* unsigned int howmany = n;1041*1042* for (i = 2; i < howmany + 2; i++) {1043* newlfsr0 = (((lfsr >> (size - 1 - 0)) & 1) ^1044* ((lfsr >> (size - 1 - 1)) & 1) ^1045* (((lfsr >> (size - 1 - 6)) & 1) ^1046* ((lfsr >> (size - 1 - 23)) & 1)));1047*1048* lfsr >>= 1;1049* lfsr = lfsr | (newlfsr0 << (size - 1));1050* }1051* return lfsr;1052* }1053*/10541055#define V2_16 (0x1 << 16)1056#define V2_19 (0x1 << 19)1057#define V2_22 (0x1 << 22)10581059static int calculate_lfsr(int n)1060{1061/*1062* The ranges and steps are in powers of 2 so the calculations1063* can be done using shifts rather then divide.1064*/1065int index;10661067if ((n >> 16) == 0)1068index = 0;1069else if (((n - V2_16) >> 19) == 0)1070index = ((n - V2_16) >> 12) + 1;1071else if (((n - V2_16 - V2_19) >> 22) == 0)1072index = ((n - V2_16 - V2_19) >> 15 ) + 1 + 128;1073else if (((n - V2_16 - V2_19 - V2_22) >> 24) == 0)1074index = ((n - V2_16 - V2_19 - V2_22) >> 18 ) + 1 + 256;1075else1076index = ENTRIES-1;10771078/* make sure index is valid */1079if ((index >= ENTRIES) || (index < 0))1080index = ENTRIES-1;10811082return initial_lfsr[index];1083}10841085static int pm_rtas_activate_spu_profiling(u32 node)1086{1087int ret, i;1088struct pm_signal pm_signal_local[NUM_SPUS_PER_NODE];10891090/*1091* Set up the rtas call to configure the debug bus to1092* route the SPU PCs. Setup the pm_signal for each SPU1093*/1094for (i = 0; i < ARRAY_SIZE(pm_signal_local); i++) {1095pm_signal_local[i].cpu = node;1096pm_signal_local[i].signal_group = 41;1097/* spu i on word (i/2) */1098pm_signal_local[i].bus_word = 1 << i / 2;1099/* spu i */1100pm_signal_local[i].sub_unit = i;1101pm_signal_local[i].bit = 63;1102}11031104ret = rtas_ibm_cbe_perftools(SUBFUNC_ACTIVATE,1105PASSTHRU_ENABLE, pm_signal_local,1106(ARRAY_SIZE(pm_signal_local)1107* sizeof(struct pm_signal)));11081109if (unlikely(ret)) {1110printk(KERN_WARNING "%s: rtas returned: %d\n",1111__func__, ret);1112return -EIO;1113}11141115return 0;1116}11171118#ifdef CONFIG_CPU_FREQ1119static int1120oprof_cpufreq_notify(struct notifier_block *nb, unsigned long val, void *data)1121{1122int ret = 0;1123struct cpufreq_freqs *frq = data;1124if ((val == CPUFREQ_PRECHANGE && frq->old < frq->new) ||1125(val == CPUFREQ_POSTCHANGE && frq->old > frq->new) ||1126(val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE))1127set_spu_profiling_frequency(frq->new, spu_cycle_reset);1128return ret;1129}11301131static struct notifier_block cpu_freq_notifier_block = {1132.notifier_call = oprof_cpufreq_notify1133};1134#endif11351136/*1137* Note the generic OProfile stop calls do not support returning1138* an error on stop. Hence, will not return an error if the FW1139* calls fail on stop. Failure to reset the debug bus is not an issue.1140* Failure to disable the SPU profiling is not an issue. The FW calls1141* to enable the performance counters and debug bus will work even if1142* the hardware was not cleanly reset.1143*/1144static void cell_global_stop_spu_cycles(void)1145{1146int subfunc, rtn_value;1147unsigned int lfsr_value;1148int cpu;11491150oprofile_running = 0;1151smp_wmb();11521153#ifdef CONFIG_CPU_FREQ1154cpufreq_unregister_notifier(&cpu_freq_notifier_block,1155CPUFREQ_TRANSITION_NOTIFIER);1156#endif11571158for_each_online_cpu(cpu) {1159if (cbe_get_hw_thread_id(cpu))1160continue;11611162subfunc = 3; /*1163* 2 - activate SPU tracing,1164* 3 - deactivate1165*/1166lfsr_value = 0x8f100000;11671168rtn_value = rtas_call(spu_rtas_token, 3, 1, NULL,1169subfunc, cbe_cpu_to_node(cpu),1170lfsr_value);11711172if (unlikely(rtn_value != 0)) {1173printk(KERN_ERR1174"%s: rtas call ibm,cbe-spu-perftools " \1175"failed, return = %d\n",1176__func__, rtn_value);1177}11781179/* Deactivate the signals */1180pm_rtas_reset_signals(cbe_cpu_to_node(cpu));1181}11821183stop_spu_profiling_cycles();1184}11851186static void cell_global_stop_spu_events(void)1187{1188int cpu;1189oprofile_running = 0;11901191stop_spu_profiling_events();1192smp_wmb();11931194for_each_online_cpu(cpu) {1195if (cbe_get_hw_thread_id(cpu))1196continue;11971198cbe_sync_irq(cbe_cpu_to_node(cpu));1199/* Stop the counters */1200cbe_disable_pm(cpu);1201cbe_write_pm07_control(cpu, 0, 0);12021203/* Deactivate the signals */1204pm_rtas_reset_signals(cbe_cpu_to_node(cpu));12051206/* Deactivate interrupts */1207cbe_disable_pm_interrupts(cpu);1208}1209del_timer_sync(&timer_spu_event_swap);1210}12111212static void cell_global_stop_ppu(void)1213{1214int cpu;12151216/*1217* This routine will be called once for the system.1218* There is one performance monitor per node, so we1219* only need to perform this function once per node.1220*/1221del_timer_sync(&timer_virt_cntr);1222oprofile_running = 0;1223smp_wmb();12241225for_each_online_cpu(cpu) {1226if (cbe_get_hw_thread_id(cpu))1227continue;12281229cbe_sync_irq(cbe_cpu_to_node(cpu));1230/* Stop the counters */1231cbe_disable_pm(cpu);12321233/* Deactivate the signals */1234pm_rtas_reset_signals(cbe_cpu_to_node(cpu));12351236/* Deactivate interrupts */1237cbe_disable_pm_interrupts(cpu);1238}1239}12401241static void cell_global_stop(void)1242{1243if (profiling_mode == PPU_PROFILING)1244cell_global_stop_ppu();1245else if (profiling_mode == SPU_PROFILING_EVENTS)1246cell_global_stop_spu_events();1247else1248cell_global_stop_spu_cycles();1249}12501251static int cell_global_start_spu_cycles(struct op_counter_config *ctr)1252{1253int subfunc;1254unsigned int lfsr_value;1255int cpu;1256int ret;1257int rtas_error;1258unsigned int cpu_khzfreq = 0;12591260/* The SPU profiling uses time-based profiling based on1261* cpu frequency, so if configured with the CPU_FREQ1262* option, we should detect frequency changes and react1263* accordingly.1264*/1265#ifdef CONFIG_CPU_FREQ1266ret = cpufreq_register_notifier(&cpu_freq_notifier_block,1267CPUFREQ_TRANSITION_NOTIFIER);1268if (ret < 0)1269/* this is not a fatal error */1270printk(KERN_ERR "CPU freq change registration failed: %d\n",1271ret);12721273else1274cpu_khzfreq = cpufreq_quick_get(smp_processor_id());1275#endif12761277set_spu_profiling_frequency(cpu_khzfreq, spu_cycle_reset);12781279for_each_online_cpu(cpu) {1280if (cbe_get_hw_thread_id(cpu))1281continue;12821283/*1284* Setup SPU cycle-based profiling.1285* Set perf_mon_control bit 0 to a zero before1286* enabling spu collection hardware.1287*/1288cbe_write_pm(cpu, pm_control, 0);12891290if (spu_cycle_reset > MAX_SPU_COUNT)1291/* use largest possible value */1292lfsr_value = calculate_lfsr(MAX_SPU_COUNT-1);1293else1294lfsr_value = calculate_lfsr(spu_cycle_reset);12951296/* must use a non zero value. Zero disables data collection. */1297if (lfsr_value == 0)1298lfsr_value = calculate_lfsr(1);12991300lfsr_value = lfsr_value << 8; /* shift lfsr to correct1301* register location1302*/13031304/* debug bus setup */1305ret = pm_rtas_activate_spu_profiling(cbe_cpu_to_node(cpu));13061307if (unlikely(ret)) {1308rtas_error = ret;1309goto out;1310}131113121313subfunc = 2; /* 2 - activate SPU tracing, 3 - deactivate */13141315/* start profiling */1316ret = rtas_call(spu_rtas_token, 3, 1, NULL, subfunc,1317cbe_cpu_to_node(cpu), lfsr_value);13181319if (unlikely(ret != 0)) {1320printk(KERN_ERR1321"%s: rtas call ibm,cbe-spu-perftools failed, " \1322"return = %d\n", __func__, ret);1323rtas_error = -EIO;1324goto out;1325}1326}13271328rtas_error = start_spu_profiling_cycles(spu_cycle_reset);1329if (rtas_error)1330goto out_stop;13311332oprofile_running = 1;1333return 0;13341335out_stop:1336cell_global_stop_spu_cycles(); /* clean up the PMU/debug bus */1337out:1338return rtas_error;1339}13401341static int cell_global_start_spu_events(struct op_counter_config *ctr)1342{1343int cpu;1344u32 interrupt_mask = 0;1345int rtn = 0;13461347hdw_thread = 0;13481349/* spu event profiling, uses the performance counters to generate1350* an interrupt. The hardware is setup to store the SPU program1351* counter into the trace array. The occurrence mode is used to1352* enable storing data to the trace buffer. The bits are set1353* to send/store the SPU address in the trace buffer. The debug1354* bus must be setup to route the SPU program counter onto the1355* debug bus. The occurrence data in the trace buffer is not used.1356*/13571358/* This routine gets called once for the system.1359* There is one performance monitor per node, so we1360* only need to perform this function once per node.1361*/13621363for_each_online_cpu(cpu) {1364if (cbe_get_hw_thread_id(cpu))1365continue;13661367/*1368* Setup SPU event-based profiling.1369* Set perf_mon_control bit 0 to a zero before1370* enabling spu collection hardware.1371*1372* Only support one SPU event on one SPU per node.1373*/1374if (ctr_enabled & 1) {1375cbe_write_ctr(cpu, 0, reset_value[0]);1376enable_ctr(cpu, 0, pm_regs.pm07_cntrl);1377interrupt_mask |=1378CBE_PM_CTR_OVERFLOW_INTR(0);1379} else {1380/* Disable counter */1381cbe_write_pm07_control(cpu, 0, 0);1382}13831384cbe_get_and_clear_pm_interrupts(cpu);1385cbe_enable_pm_interrupts(cpu, hdw_thread, interrupt_mask);1386cbe_enable_pm(cpu);13871388/* clear the trace buffer */1389cbe_write_pm(cpu, trace_address, 0);1390}13911392/* Start the timer to time slice collecting the event profile1393* on each of the SPUs. Note, can collect profile on one SPU1394* per node at a time.1395*/1396start_spu_event_swap();1397start_spu_profiling_events();1398oprofile_running = 1;1399smp_wmb();14001401return rtn;1402}14031404static int cell_global_start_ppu(struct op_counter_config *ctr)1405{1406u32 cpu, i;1407u32 interrupt_mask = 0;14081409/* This routine gets called once for the system.1410* There is one performance monitor per node, so we1411* only need to perform this function once per node.1412*/1413for_each_online_cpu(cpu) {1414if (cbe_get_hw_thread_id(cpu))1415continue;14161417interrupt_mask = 0;14181419for (i = 0; i < num_counters; ++i) {1420if (ctr_enabled & (1 << i)) {1421cbe_write_ctr(cpu, i, reset_value[i]);1422enable_ctr(cpu, i, pm_regs.pm07_cntrl);1423interrupt_mask |= CBE_PM_CTR_OVERFLOW_INTR(i);1424} else {1425/* Disable counter */1426cbe_write_pm07_control(cpu, i, 0);1427}1428}14291430cbe_get_and_clear_pm_interrupts(cpu);1431cbe_enable_pm_interrupts(cpu, hdw_thread, interrupt_mask);1432cbe_enable_pm(cpu);1433}14341435virt_cntr_inter_mask = interrupt_mask;1436oprofile_running = 1;1437smp_wmb();14381439/*1440* NOTE: start_virt_cntrs will result in cell_virtual_cntr() being1441* executed which manipulates the PMU. We start the "virtual counter"1442* here so that we do not need to synchronize access to the PMU in1443* the above for-loop.1444*/1445start_virt_cntrs();14461447return 0;1448}14491450static int cell_global_start(struct op_counter_config *ctr)1451{1452if (profiling_mode == SPU_PROFILING_CYCLES)1453return cell_global_start_spu_cycles(ctr);1454else if (profiling_mode == SPU_PROFILING_EVENTS)1455return cell_global_start_spu_events(ctr);1456else1457return cell_global_start_ppu(ctr);1458}145914601461/* The SPU interrupt handler1462*1463* SPU event profiling works as follows:1464* The pm_signal[0] holds the one SPU event to be measured. It is routed on1465* the debug bus using word 0 or 1. The value of pm_signal[1] and1466* pm_signal[2] contain the necessary events to route the SPU program1467* counter for the selected SPU onto the debug bus using words 2 and 3.1468* The pm_interval register is setup to write the SPU PC value into the1469* trace buffer at the maximum rate possible. The trace buffer is configured1470* to store the PCs, wrapping when it is full. The performance counter is1471* initialized to the max hardware count minus the number of events, N, between1472* samples. Once the N events have occurred, a HW counter overflow occurs1473* causing the generation of a HW counter interrupt which also stops the1474* writing of the SPU PC values to the trace buffer. Hence the last PC1475* written to the trace buffer is the SPU PC that we want. Unfortunately,1476* we have to read from the beginning of the trace buffer to get to the1477* last value written. We just hope the PPU has nothing better to do then1478* service this interrupt. The PC for the specific SPU being profiled is1479* extracted from the trace buffer processed and stored. The trace buffer1480* is cleared, interrupts are cleared, the counter is reset to max - N.1481* A kernel timer is used to periodically call the routine spu_evnt_swap()1482* to switch to the next physical SPU in the node to profile in round robbin1483* order. This way data is collected for all SPUs on the node. It does mean1484* that we need to use a relatively small value of N to ensure enough samples1485* on each SPU are collected each SPU is being profiled 1/8 of the time.1486* It may also be necessary to use a longer sample collection period.1487*/1488static void cell_handle_interrupt_spu(struct pt_regs *regs,1489struct op_counter_config *ctr)1490{1491u32 cpu, cpu_tmp;1492u64 trace_entry;1493u32 interrupt_mask;1494u64 trace_buffer[2];1495u64 last_trace_buffer;1496u32 sample;1497u32 trace_addr;1498unsigned long sample_array_lock_flags;1499int spu_num;1500unsigned long flags;15011502/* Make sure spu event interrupt handler and spu event swap1503* don't access the counters simultaneously.1504*/1505cpu = smp_processor_id();1506spin_lock_irqsave(&cntr_lock, flags);15071508cpu_tmp = cpu;1509cbe_disable_pm(cpu);15101511interrupt_mask = cbe_get_and_clear_pm_interrupts(cpu);15121513sample = 0xABCDEF;1514trace_entry = 0xfedcba;1515last_trace_buffer = 0xdeadbeaf;15161517if ((oprofile_running == 1) && (interrupt_mask != 0)) {1518/* disable writes to trace buff */1519cbe_write_pm(cpu, pm_interval, 0);15201521/* only have one perf cntr being used, cntr 0 */1522if ((interrupt_mask & CBE_PM_CTR_OVERFLOW_INTR(0))1523&& ctr[0].enabled)1524/* The SPU PC values will be read1525* from the trace buffer, reset counter1526*/15271528cbe_write_ctr(cpu, 0, reset_value[0]);15291530trace_addr = cbe_read_pm(cpu, trace_address);15311532while (!(trace_addr & CBE_PM_TRACE_BUF_EMPTY)) {1533/* There is data in the trace buffer to process1534* Read the buffer until you get to the last1535* entry. This is the value we want.1536*/15371538cbe_read_trace_buffer(cpu, trace_buffer);1539trace_addr = cbe_read_pm(cpu, trace_address);1540}15411542/* SPU Address 16 bit count format for 128 bit1543* HW trace buffer is used for the SPU PC storage1544* HDR bits 0:151545* SPU Addr 0 bits 16:311546* SPU Addr 1 bits 32:471547* unused bits 48:1271548*1549* HDR: bit4 = 1 SPU Address 0 valid1550* HDR: bit5 = 1 SPU Address 1 valid1551* - unfortunately, the valid bits don't seem to work1552*1553* Note trace_buffer[0] holds bits 0:63 of the HW1554* trace buffer, trace_buffer[1] holds bits 64:1271555*/15561557trace_entry = trace_buffer[0]1558& 0x00000000FFFF0000;15591560/* only top 16 of the 18 bit SPU PC address1561* is stored in trace buffer, hence shift right1562* by 16 -2 bits */1563sample = trace_entry >> 14;1564last_trace_buffer = trace_buffer[0];15651566spu_num = spu_evnt_phys_spu_indx1567+ (cbe_cpu_to_node(cpu) * NUM_SPUS_PER_NODE);15681569/* make sure only one process at a time is calling1570* spu_sync_buffer()1571*/1572spin_lock_irqsave(&oprof_spu_smpl_arry_lck,1573sample_array_lock_flags);1574spu_sync_buffer(spu_num, &sample, 1);1575spin_unlock_irqrestore(&oprof_spu_smpl_arry_lck,1576sample_array_lock_flags);15771578smp_wmb(); /* insure spu event buffer updates are written1579* don't want events intermingled... */15801581/* The counters were frozen by the interrupt.1582* Reenable the interrupt and restart the counters.1583*/1584cbe_write_pm(cpu, pm_interval, NUM_INTERVAL_CYC);1585cbe_enable_pm_interrupts(cpu, hdw_thread,1586virt_cntr_inter_mask);15871588/* clear the trace buffer, re-enable writes to trace buff */1589cbe_write_pm(cpu, trace_address, 0);1590cbe_write_pm(cpu, pm_interval, NUM_INTERVAL_CYC);15911592/* The writes to the various performance counters only writes1593* to a latch. The new values (interrupt setting bits, reset1594* counter value etc.) are not copied to the actual registers1595* until the performance monitor is enabled. In order to get1596* this to work as desired, the performance monitor needs to1597* be disabled while writing to the latches. This is a1598* HW design issue.1599*/1600write_pm_cntrl(cpu);1601cbe_enable_pm(cpu);1602}1603spin_unlock_irqrestore(&cntr_lock, flags);1604}16051606static void cell_handle_interrupt_ppu(struct pt_regs *regs,1607struct op_counter_config *ctr)1608{1609u32 cpu;1610u64 pc;1611int is_kernel;1612unsigned long flags = 0;1613u32 interrupt_mask;1614int i;16151616cpu = smp_processor_id();16171618/*1619* Need to make sure the interrupt handler and the virt counter1620* routine are not running at the same time. See the1621* cell_virtual_cntr() routine for additional comments.1622*/1623spin_lock_irqsave(&cntr_lock, flags);16241625/*1626* Need to disable and reenable the performance counters1627* to get the desired behavior from the hardware. This1628* is hardware specific.1629*/16301631cbe_disable_pm(cpu);16321633interrupt_mask = cbe_get_and_clear_pm_interrupts(cpu);16341635/*1636* If the interrupt mask has been cleared, then the virt cntr1637* has cleared the interrupt. When the thread that generated1638* the interrupt is restored, the data count will be restored to1639* 0xffffff0 to cause the interrupt to be regenerated.1640*/16411642if ((oprofile_running == 1) && (interrupt_mask != 0)) {1643pc = regs->nip;1644is_kernel = is_kernel_addr(pc);16451646for (i = 0; i < num_counters; ++i) {1647if ((interrupt_mask & CBE_PM_CTR_OVERFLOW_INTR(i))1648&& ctr[i].enabled) {1649oprofile_add_ext_sample(pc, regs, i, is_kernel);1650cbe_write_ctr(cpu, i, reset_value[i]);1651}1652}16531654/*1655* The counters were frozen by the interrupt.1656* Reenable the interrupt and restart the counters.1657* If there was a race between the interrupt handler and1658* the virtual counter routine. The virtual counter1659* routine may have cleared the interrupts. Hence must1660* use the virt_cntr_inter_mask to re-enable the interrupts.1661*/1662cbe_enable_pm_interrupts(cpu, hdw_thread,1663virt_cntr_inter_mask);16641665/*1666* The writes to the various performance counters only writes1667* to a latch. The new values (interrupt setting bits, reset1668* counter value etc.) are not copied to the actual registers1669* until the performance monitor is enabled. In order to get1670* this to work as desired, the performance monitor needs to1671* be disabled while writing to the latches. This is a1672* HW design issue.1673*/1674cbe_enable_pm(cpu);1675}1676spin_unlock_irqrestore(&cntr_lock, flags);1677}16781679static void cell_handle_interrupt(struct pt_regs *regs,1680struct op_counter_config *ctr)1681{1682if (profiling_mode == PPU_PROFILING)1683cell_handle_interrupt_ppu(regs, ctr);1684else1685cell_handle_interrupt_spu(regs, ctr);1686}16871688/*1689* This function is called from the generic OProfile1690* driver. When profiling PPUs, we need to do the1691* generic sync start; otherwise, do spu_sync_start.1692*/1693static int cell_sync_start(void)1694{1695if ((profiling_mode == SPU_PROFILING_CYCLES) ||1696(profiling_mode == SPU_PROFILING_EVENTS))1697return spu_sync_start();1698else1699return DO_GENERIC_SYNC;1700}17011702static int cell_sync_stop(void)1703{1704if ((profiling_mode == SPU_PROFILING_CYCLES) ||1705(profiling_mode == SPU_PROFILING_EVENTS))1706return spu_sync_stop();1707else1708return 1;1709}17101711struct op_powerpc_model op_model_cell = {1712.reg_setup = cell_reg_setup,1713.cpu_setup = cell_cpu_setup,1714.global_start = cell_global_start,1715.global_stop = cell_global_stop,1716.sync_start = cell_sync_start,1717.sync_stop = cell_sync_stop,1718.handle_interrupt = cell_handle_interrupt,1719};172017211722