op_model_cell.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. /*
  2. * Cell Broadband Engine OProfile Support
  3. *
  4. * (C) Copyright IBM Corporation 2006
  5. *
  6. * Author: David Erb (djerb@us.ibm.com)
  7. * Modifications:
  8. * Carl Love <carll@us.ibm.com>
  9. * Maynard Johnson <maynardj@us.ibm.com>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #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>
  38. #include "../platforms/cell/interrupt.h"
  39. #include "cell/pr_util.h"
  40. #define PPU_PROFILING 0
  41. #define SPU_PROFILING_CYCLES 1
  42. #define SPU_PROFILING_EVENTS 2
  43. #define NUM_SPUS_PER_NODE 8
  44. #define SPU_CYCLES_EVENT_NUM 2 /* event number for SPU_CYCLES */
  45. #define PPU_CYCLES_EVENT_NUM 1 /* event number for CYCLES */
  46. #define PPU_CYCLES_GRP_NUM 1 /* special group number for identifying
  47. * PPU_CYCLES event
  48. */
  49. #define CBE_COUNT_ALL_CYCLES 0x42800000 /* PPU cycle event specifier */
  50. #define NUM_THREADS 2 /* number of physical threads in
  51. * physical processor
  52. */
  53. #define NUM_DEBUG_BUS_WORDS 4
  54. #define NUM_INPUT_BUS_WORDS 2
  55. #define MAX_SPU_COUNT 0xFFFFFF /* maximum 24 bit LFSR value */
  56. /*
  57. * spu_cycle_reset is the number of cycles between samples.
  58. * This variable is used for SPU profiling and should ONLY be set
  59. * at the beginning of cell_reg_setup; otherwise, it's read-only.
  60. */
  61. static unsigned int spu_cycle_reset;
  62. static unsigned int profiling_mode;
  63. struct pmc_cntrl_data {
  64. unsigned long vcntr;
  65. unsigned long evnts;
  66. unsigned long masks;
  67. unsigned long enabled;
  68. };
  69. /*
  70. * ibm,cbe-perftools rtas parameters
  71. */
  72. struct pm_signal {
  73. u16 cpu; /* Processor to modify */
  74. u16 sub_unit; /* hw subunit this applies to (if applicable)*/
  75. short int signal_group; /* Signal Group to Enable/Disable */
  76. u8 bus_word; /* Enable/Disable on this Trace/Trigger/Event
  77. * Bus Word(s) (bitmask)
  78. */
  79. u8 bit; /* Trigger/Event bit (if applicable) */
  80. };
  81. /*
  82. * rtas call arguments
  83. */
  84. enum {
  85. SUBFUNC_RESET = 1,
  86. SUBFUNC_ACTIVATE = 2,
  87. SUBFUNC_DEACTIVATE = 3,
  88. PASSTHRU_IGNORE = 0,
  89. PASSTHRU_ENABLE = 1,
  90. PASSTHRU_DISABLE = 2,
  91. };
  92. struct pm_cntrl {
  93. u16 enable;
  94. u16 stop_at_max;
  95. u16 trace_mode;
  96. u16 freeze;
  97. u16 count_mode;
  98. };
  99. static struct {
  100. u32 group_control;
  101. u32 debug_bus_control;
  102. struct pm_cntrl pm_cntrl;
  103. u32 pm07_cntrl[NR_PHYS_CTRS];
  104. } pm_regs;
  105. #define GET_SUB_UNIT(x) ((x & 0x0000f000) >> 12)
  106. #define GET_BUS_WORD(x) ((x & 0x000000f0) >> 4)
  107. #define GET_BUS_TYPE(x) ((x & 0x00000300) >> 8)
  108. #define GET_POLARITY(x) ((x & 0x00000002) >> 1)
  109. #define GET_COUNT_CYCLES(x) (x & 0x00000001)
  110. #define GET_INPUT_CONTROL(x) ((x & 0x00000004) >> 2)
  111. static DEFINE_PER_CPU(unsigned long[NR_PHYS_CTRS], pmc_values);
  112. static struct pmc_cntrl_data pmc_cntrl[NUM_THREADS][NR_PHYS_CTRS];
  113. /*
  114. * The CELL profiling code makes rtas calls to setup the debug bus to
  115. * route the performance signals. Additionally, SPU profiling requires
  116. * a second rtas call to setup the hardware to capture the SPU PCs.
  117. * The EIO error value is returned if the token lookups or the rtas
  118. * call fail. The EIO error number is the best choice of the existing
  119. * error numbers. The probability of rtas related error is very low. But
  120. * by returning EIO and printing additional information to dmsg the user
  121. * will know that OProfile did not start and dmesg will tell them why.
  122. * OProfile does not support returning errors on Stop. Not a huge issue
  123. * since failure to reset the debug bus or stop the SPU PC collection is
  124. * not a fatel issue. Chances are if the Stop failed, Start doesn't work
  125. * either.
  126. */
  127. /*
  128. * Interpetation of hdw_thread:
  129. * 0 - even virtual cpus 0, 2, 4,...
  130. * 1 - odd virtual cpus 1, 3, 5, ...
  131. *
  132. * FIXME: this is strictly wrong, we need to clean this up in a number
  133. * of places. It works for now. -arnd
  134. */
  135. static u32 hdw_thread;
  136. static u32 virt_cntr_inter_mask;
  137. static struct timer_list timer_virt_cntr;
  138. /*
  139. * pm_signal needs to be global since it is initialized in
  140. * cell_reg_setup at the time when the necessary information
  141. * is available.
  142. */
  143. static struct pm_signal pm_signal[NR_PHYS_CTRS];
  144. static int pm_rtas_token; /* token for debug bus setup call */
  145. static int spu_rtas_token; /* token for SPU cycle profiling */
  146. static u32 reset_value[NR_PHYS_CTRS];
  147. static int num_counters;
  148. static int oprofile_running;
  149. static DEFINE_SPINLOCK(cntr_lock);
  150. static u32 ctr_enabled;
  151. static unsigned char input_bus[NUM_INPUT_BUS_WORDS];
  152. /*
  153. * Firmware interface functions
  154. */
  155. static int
  156. rtas_ibm_cbe_perftools(int subfunc, int passthru,
  157. void *address, unsigned long length)
  158. {
  159. u64 paddr = __pa(address);
  160. return rtas_call(pm_rtas_token, 5, 1, NULL, subfunc,
  161. passthru, paddr >> 32, paddr & 0xffffffff, length);
  162. }
  163. static void pm_rtas_reset_signals(u32 node)
  164. {
  165. int ret;
  166. struct pm_signal pm_signal_local;
  167. /*
  168. * The debug bus is being set to the passthru disable state.
  169. * However, the FW still expects atleast one legal signal routing
  170. * entry or it will return an error on the arguments. If we don't
  171. * supply a valid entry, we must ignore all return values. Ignoring
  172. * all return values means we might miss an error we should be
  173. * concerned about.
  174. */
  175. /* fw expects physical cpu #. */
  176. pm_signal_local.cpu = node;
  177. pm_signal_local.signal_group = 21;
  178. pm_signal_local.bus_word = 1;
  179. pm_signal_local.sub_unit = 0;
  180. pm_signal_local.bit = 0;
  181. ret = rtas_ibm_cbe_perftools(SUBFUNC_RESET, PASSTHRU_DISABLE,
  182. &pm_signal_local,
  183. sizeof(struct pm_signal));
  184. if (unlikely(ret))
  185. /*
  186. * Not a fatal error. For Oprofile stop, the oprofile
  187. * functions do not support returning an error for
  188. * failure to stop OProfile.
  189. */
  190. printk(KERN_WARNING "%s: rtas returned: %d\n",
  191. __func__, ret);
  192. }
  193. static int pm_rtas_activate_signals(u32 node, u32 count)
  194. {
  195. int ret;
  196. int i, j;
  197. struct pm_signal pm_signal_local[NR_PHYS_CTRS];
  198. /*
  199. * There is no debug setup required for the cycles event.
  200. * Note that only events in the same group can be used.
  201. * Otherwise, there will be conflicts in correctly routing
  202. * the signals on the debug bus. It is the responsiblity
  203. * of the OProfile user tool to check the events are in
  204. * the same group.
  205. */
  206. i = 0;
  207. for (j = 0; j < count; j++) {
  208. if (pm_signal[j].signal_group != PPU_CYCLES_GRP_NUM) {
  209. /* fw expects physical cpu # */
  210. pm_signal_local[i].cpu = node;
  211. pm_signal_local[i].signal_group
  212. = pm_signal[j].signal_group;
  213. pm_signal_local[i].bus_word = pm_signal[j].bus_word;
  214. pm_signal_local[i].sub_unit = pm_signal[j].sub_unit;
  215. pm_signal_local[i].bit = pm_signal[j].bit;
  216. i++;
  217. }
  218. }
  219. if (i != 0) {
  220. ret = rtas_ibm_cbe_perftools(SUBFUNC_ACTIVATE, PASSTHRU_ENABLE,
  221. pm_signal_local,
  222. i * sizeof(struct pm_signal));
  223. if (unlikely(ret)) {
  224. printk(KERN_WARNING "%s: rtas returned: %d\n",
  225. __func__, ret);
  226. return -EIO;
  227. }
  228. }
  229. return 0;
  230. }
  231. /*
  232. * PM Signal functions
  233. */
  234. static void set_pm_event(u32 ctr, int event, u32 unit_mask)
  235. {
  236. struct pm_signal *p;
  237. u32 signal_bit;
  238. u32 bus_word, bus_type, count_cycles, polarity, input_control;
  239. int j, i;
  240. if (event == PPU_CYCLES_EVENT_NUM) {
  241. /* Special Event: Count all cpu cycles */
  242. pm_regs.pm07_cntrl[ctr] = CBE_COUNT_ALL_CYCLES;
  243. p = &(pm_signal[ctr]);
  244. p->signal_group = PPU_CYCLES_GRP_NUM;
  245. p->bus_word = 1;
  246. p->sub_unit = 0;
  247. p->bit = 0;
  248. goto out;
  249. } else {
  250. pm_regs.pm07_cntrl[ctr] = 0;
  251. }
  252. bus_word = GET_BUS_WORD(unit_mask);
  253. bus_type = GET_BUS_TYPE(unit_mask);
  254. count_cycles = GET_COUNT_CYCLES(unit_mask);
  255. polarity = GET_POLARITY(unit_mask);
  256. input_control = GET_INPUT_CONTROL(unit_mask);
  257. signal_bit = (event % 100);
  258. p = &(pm_signal[ctr]);
  259. p->signal_group = event / 100;
  260. p->bus_word = bus_word;
  261. p->sub_unit = GET_SUB_UNIT(unit_mask);
  262. pm_regs.pm07_cntrl[ctr] = 0;
  263. pm_regs.pm07_cntrl[ctr] |= PM07_CTR_COUNT_CYCLES(count_cycles);
  264. pm_regs.pm07_cntrl[ctr] |= PM07_CTR_POLARITY(polarity);
  265. pm_regs.pm07_cntrl[ctr] |= PM07_CTR_INPUT_CONTROL(input_control);
  266. /*
  267. * Some of the islands signal selection is based on 64 bit words.
  268. * The debug bus words are 32 bits, the input words to the performance
  269. * counters are defined as 32 bits. Need to convert the 64 bit island
  270. * specification to the appropriate 32 input bit and bus word for the
  271. * performance counter event selection. See the CELL Performance
  272. * monitoring signals manual and the Perf cntr hardware descriptions
  273. * for the details.
  274. */
  275. if (input_control == 0) {
  276. if (signal_bit > 31) {
  277. signal_bit -= 32;
  278. if (bus_word == 0x3)
  279. bus_word = 0x2;
  280. else if (bus_word == 0xc)
  281. bus_word = 0x8;
  282. }
  283. if ((bus_type == 0) && p->signal_group >= 60)
  284. bus_type = 2;
  285. if ((bus_type == 1) && p->signal_group >= 50)
  286. bus_type = 0;
  287. pm_regs.pm07_cntrl[ctr] |= PM07_CTR_INPUT_MUX(signal_bit);
  288. } else {
  289. pm_regs.pm07_cntrl[ctr] = 0;
  290. p->bit = signal_bit;
  291. }
  292. for (i = 0; i < NUM_DEBUG_BUS_WORDS; i++) {
  293. if (bus_word & (1 << i)) {
  294. pm_regs.debug_bus_control |=
  295. (bus_type << (30 - (2 * i)));
  296. for (j = 0; j < NUM_INPUT_BUS_WORDS; j++) {
  297. if (input_bus[j] == 0xff) {
  298. input_bus[j] = i;
  299. pm_regs.group_control |=
  300. (i << (30 - (2 * j)));
  301. break;
  302. }
  303. }
  304. }
  305. }
  306. out:
  307. ;
  308. }
  309. static void write_pm_cntrl(int cpu)
  310. {
  311. /*
  312. * Oprofile will use 32 bit counters, set bits 7:10 to 0
  313. * pmregs.pm_cntrl is a global
  314. */
  315. u32 val = 0;
  316. if (pm_regs.pm_cntrl.enable == 1)
  317. val |= CBE_PM_ENABLE_PERF_MON;
  318. if (pm_regs.pm_cntrl.stop_at_max == 1)
  319. val |= CBE_PM_STOP_AT_MAX;
  320. if (pm_regs.pm_cntrl.trace_mode != 0)
  321. val |= CBE_PM_TRACE_MODE_SET(pm_regs.pm_cntrl.trace_mode);
  322. if (pm_regs.pm_cntrl.freeze == 1)
  323. val |= CBE_PM_FREEZE_ALL_CTRS;
  324. /*
  325. * Routine set_count_mode must be called previously to set
  326. * the count mode based on the user selection of user and kernel.
  327. */
  328. val |= CBE_PM_COUNT_MODE_SET(pm_regs.pm_cntrl.count_mode);
  329. cbe_write_pm(cpu, pm_control, val);
  330. }
  331. static inline void
  332. set_count_mode(u32 kernel, u32 user)
  333. {
  334. /*
  335. * The user must specify user and kernel if they want them. If
  336. * neither is specified, OProfile will count in hypervisor mode.
  337. * pm_regs.pm_cntrl is a global
  338. */
  339. if (kernel) {
  340. if (user)
  341. pm_regs.pm_cntrl.count_mode = CBE_COUNT_ALL_MODES;
  342. else
  343. pm_regs.pm_cntrl.count_mode =
  344. CBE_COUNT_SUPERVISOR_MODE;
  345. } else {
  346. if (user)
  347. pm_regs.pm_cntrl.count_mode = CBE_COUNT_PROBLEM_MODE;
  348. else
  349. pm_regs.pm_cntrl.count_mode =
  350. CBE_COUNT_HYPERVISOR_MODE;
  351. }
  352. }
  353. static inline void enable_ctr(u32 cpu, u32 ctr, u32 *pm07_cntrl)
  354. {
  355. pm07_cntrl[ctr] |= CBE_PM_CTR_ENABLE;
  356. cbe_write_pm07_control(cpu, ctr, pm07_cntrl[ctr]);
  357. }
  358. /*
  359. * Oprofile is expected to collect data on all CPUs simultaneously.
  360. * However, there is one set of performance counters per node. There are
  361. * two hardware threads or virtual CPUs on each node. Hence, OProfile must
  362. * multiplex in time the performance counter collection on the two virtual
  363. * CPUs. The multiplexing of the performance counters is done by this
  364. * virtual counter routine.
  365. *
  366. * The pmc_values used below is defined as 'per-cpu' but its use is
  367. * more akin to 'per-node'. We need to store two sets of counter
  368. * values per node -- one for the previous run and one for the next.
  369. * The per-cpu[NR_PHYS_CTRS] gives us the storage we need. Each odd/even
  370. * pair of per-cpu arrays is used for storing the previous and next
  371. * pmc values for a given node.
  372. * NOTE: We use the per-cpu variable to improve cache performance.
  373. *
  374. * This routine will alternate loading the virtual counters for
  375. * virtual CPUs
  376. */
  377. static void cell_virtual_cntr(unsigned long data)
  378. {
  379. int i, prev_hdw_thread, next_hdw_thread;
  380. u32 cpu;
  381. unsigned long flags;
  382. /*
  383. * Make sure that the interrupt_hander and the virt counter are
  384. * not both playing with the counters on the same node.
  385. */
  386. spin_lock_irqsave(&cntr_lock, flags);
  387. prev_hdw_thread = hdw_thread;
  388. /* switch the cpu handling the interrupts */
  389. hdw_thread = 1 ^ hdw_thread;
  390. next_hdw_thread = hdw_thread;
  391. pm_regs.group_control = 0;
  392. pm_regs.debug_bus_control = 0;
  393. for (i = 0; i < NUM_INPUT_BUS_WORDS; i++)
  394. input_bus[i] = 0xff;
  395. /*
  396. * There are some per thread events. Must do the
  397. * set event, for the thread that is being started
  398. */
  399. for (i = 0; i < num_counters; i++)
  400. set_pm_event(i,
  401. pmc_cntrl[next_hdw_thread][i].evnts,
  402. pmc_cntrl[next_hdw_thread][i].masks);
  403. /*
  404. * The following is done only once per each node, but
  405. * we need cpu #, not node #, to pass to the cbe_xxx functions.
  406. */
  407. for_each_online_cpu(cpu) {
  408. if (cbe_get_hw_thread_id(cpu))
  409. continue;
  410. /*
  411. * stop counters, save counter values, restore counts
  412. * for previous thread
  413. */
  414. cbe_disable_pm(cpu);
  415. cbe_disable_pm_interrupts(cpu);
  416. for (i = 0; i < num_counters; i++) {
  417. per_cpu(pmc_values, cpu + prev_hdw_thread)[i]
  418. = cbe_read_ctr(cpu, i);
  419. if (per_cpu(pmc_values, cpu + next_hdw_thread)[i]
  420. == 0xFFFFFFFF)
  421. /* If the cntr value is 0xffffffff, we must
  422. * reset that to 0xfffffff0 when the current
  423. * thread is restarted. This will generate a
  424. * new interrupt and make sure that we never
  425. * restore the counters to the max value. If
  426. * the counters were restored to the max value,
  427. * they do not increment and no interrupts are
  428. * generated. Hence no more samples will be
  429. * collected on that cpu.
  430. */
  431. cbe_write_ctr(cpu, i, 0xFFFFFFF0);
  432. else
  433. cbe_write_ctr(cpu, i,
  434. per_cpu(pmc_values,
  435. cpu +
  436. next_hdw_thread)[i]);
  437. }
  438. /*
  439. * Switch to the other thread. Change the interrupt
  440. * and control regs to be scheduled on the CPU
  441. * corresponding to the thread to execute.
  442. */
  443. for (i = 0; i < num_counters; i++) {
  444. if (pmc_cntrl[next_hdw_thread][i].enabled) {
  445. /*
  446. * There are some per thread events.
  447. * Must do the set event, enable_cntr
  448. * for each cpu.
  449. */
  450. enable_ctr(cpu, i,
  451. pm_regs.pm07_cntrl);
  452. } else {
  453. cbe_write_pm07_control(cpu, i, 0);
  454. }
  455. }
  456. /* Enable interrupts on the CPU thread that is starting */
  457. cbe_enable_pm_interrupts(cpu, next_hdw_thread,
  458. virt_cntr_inter_mask);
  459. cbe_enable_pm(cpu);
  460. }
  461. spin_unlock_irqrestore(&cntr_lock, flags);
  462. mod_timer(&timer_virt_cntr, jiffies + HZ / 10);
  463. }
  464. static void start_virt_cntrs(void)
  465. {
  466. init_timer(&timer_virt_cntr);
  467. timer_virt_cntr.function = cell_virtual_cntr;
  468. timer_virt_cntr.data = 0UL;
  469. timer_virt_cntr.expires = jiffies + HZ / 10;
  470. add_timer(&timer_virt_cntr);
  471. }
  472. static int cell_reg_setup_spu_cycles(struct op_counter_config *ctr,
  473. struct op_system_config *sys, int num_ctrs)
  474. {
  475. spu_cycle_reset = ctr[0].count;
  476. /*
  477. * Each node will need to make the rtas call to start
  478. * and stop SPU profiling. Get the token once and store it.
  479. */
  480. spu_rtas_token = rtas_token("ibm,cbe-spu-perftools");
  481. if (unlikely(spu_rtas_token == RTAS_UNKNOWN_SERVICE)) {
  482. printk(KERN_ERR
  483. "%s: rtas token ibm,cbe-spu-perftools unknown\n",
  484. __func__);
  485. return -EIO;
  486. }
  487. return 0;
  488. }
  489. static int cell_reg_setup_ppu(struct op_counter_config *ctr,
  490. struct op_system_config *sys, int num_ctrs)
  491. {
  492. int i, j, cpu;
  493. num_counters = num_ctrs;
  494. if (unlikely(num_ctrs > NR_PHYS_CTRS)) {
  495. printk(KERN_ERR
  496. "%s: Oprofile, number of specified events " \
  497. "exceeds number of physical counters\n",
  498. __func__);
  499. return -EIO;
  500. }
  501. pm_regs.group_control = 0;
  502. pm_regs.debug_bus_control = 0;
  503. /* setup the pm_control register */
  504. memset(&pm_regs.pm_cntrl, 0, sizeof(struct pm_cntrl));
  505. pm_regs.pm_cntrl.stop_at_max = 1;
  506. pm_regs.pm_cntrl.trace_mode = 0;
  507. pm_regs.pm_cntrl.freeze = 1;
  508. set_count_mode(sys->enable_kernel, sys->enable_user);
  509. /* Setup the thread 0 events */
  510. for (i = 0; i < num_ctrs; ++i) {
  511. pmc_cntrl[0][i].evnts = ctr[i].event;
  512. pmc_cntrl[0][i].masks = ctr[i].unit_mask;
  513. pmc_cntrl[0][i].enabled = ctr[i].enabled;
  514. pmc_cntrl[0][i].vcntr = i;
  515. for_each_possible_cpu(j)
  516. per_cpu(pmc_values, j)[i] = 0;
  517. }
  518. /*
  519. * Setup the thread 1 events, map the thread 0 event to the
  520. * equivalent thread 1 event.
  521. */
  522. for (i = 0; i < num_ctrs; ++i) {
  523. if ((ctr[i].event >= 2100) && (ctr[i].event <= 2111))
  524. pmc_cntrl[1][i].evnts = ctr[i].event + 19;
  525. else if (ctr[i].event == 2203)
  526. pmc_cntrl[1][i].evnts = ctr[i].event;
  527. else if ((ctr[i].event >= 2200) && (ctr[i].event <= 2215))
  528. pmc_cntrl[1][i].evnts = ctr[i].event + 16;
  529. else
  530. pmc_cntrl[1][i].evnts = ctr[i].event;
  531. pmc_cntrl[1][i].masks = ctr[i].unit_mask;
  532. pmc_cntrl[1][i].enabled = ctr[i].enabled;
  533. pmc_cntrl[1][i].vcntr = i;
  534. }
  535. for (i = 0; i < NUM_INPUT_BUS_WORDS; i++)
  536. input_bus[i] = 0xff;
  537. /*
  538. * Our counters count up, and "count" refers to
  539. * how much before the next interrupt, and we interrupt
  540. * on overflow. So we calculate the starting value
  541. * which will give us "count" until overflow.
  542. * Then we set the events on the enabled counters.
  543. */
  544. for (i = 0; i < num_counters; ++i) {
  545. /* start with virtual counter set 0 */
  546. if (pmc_cntrl[0][i].enabled) {
  547. /* Using 32bit counters, reset max - count */
  548. reset_value[i] = 0xFFFFFFFF - ctr[i].count;
  549. set_pm_event(i,
  550. pmc_cntrl[0][i].evnts,
  551. pmc_cntrl[0][i].masks);
  552. /* global, used by cell_cpu_setup */
  553. ctr_enabled |= (1 << i);
  554. }
  555. }
  556. /* initialize the previous counts for the virtual cntrs */
  557. for_each_online_cpu(cpu)
  558. for (i = 0; i < num_counters; ++i) {
  559. per_cpu(pmc_values, cpu)[i] = reset_value[i];
  560. }
  561. return 0;
  562. }
  563. /* This function is called once for all cpus combined */
  564. static int cell_reg_setup(struct op_counter_config *ctr,
  565. struct op_system_config *sys, int num_ctrs)
  566. {
  567. int ret;
  568. spu_cycle_reset = 0;
  569. /*
  570. * For all events except PPU CYCLEs, each node will need to make
  571. * the rtas cbe-perftools call to setup and reset the debug bus.
  572. * Make the token lookup call once and store it in the global
  573. * variable pm_rtas_token.
  574. */
  575. pm_rtas_token = rtas_token("ibm,cbe-perftools");
  576. if (unlikely(pm_rtas_token == RTAS_UNKNOWN_SERVICE)) {
  577. printk(KERN_ERR
  578. "%s: rtas token ibm,cbe-perftools unknown\n",
  579. __func__);
  580. return -EIO;
  581. }
  582. if (ctr[0].event == SPU_CYCLES_EVENT_NUM) {
  583. profiling_mode = SPU_PROFILING_CYCLES;
  584. ret = cell_reg_setup_spu_cycles(ctr, sys, num_ctrs);
  585. } else {
  586. profiling_mode = PPU_PROFILING;
  587. ret = cell_reg_setup_ppu(ctr, sys, num_ctrs);
  588. }
  589. return ret;
  590. }
  591. /* This function is called once for each cpu */
  592. static int cell_cpu_setup(struct op_counter_config *cntr)
  593. {
  594. u32 cpu = smp_processor_id();
  595. u32 num_enabled = 0;
  596. int i;
  597. /* Cycle based SPU profiling does not use the performance
  598. * counters. The trace array is configured to collect
  599. * the data.
  600. */
  601. if (profiling_mode == SPU_PROFILING_CYCLES)
  602. return 0;
  603. /* There is one performance monitor per processor chip (i.e. node),
  604. * so we only need to perform this function once per node.
  605. */
  606. if (cbe_get_hw_thread_id(cpu))
  607. return 0;
  608. /* Stop all counters */
  609. cbe_disable_pm(cpu);
  610. cbe_disable_pm_interrupts(cpu);
  611. cbe_write_pm(cpu, pm_start_stop, 0);
  612. cbe_write_pm(cpu, group_control, pm_regs.group_control);
  613. cbe_write_pm(cpu, debug_bus_control, pm_regs.debug_bus_control);
  614. write_pm_cntrl(cpu);
  615. for (i = 0; i < num_counters; ++i) {
  616. if (ctr_enabled & (1 << i)) {
  617. pm_signal[num_enabled].cpu = cbe_cpu_to_node(cpu);
  618. num_enabled++;
  619. }
  620. }
  621. /*
  622. * The pm_rtas_activate_signals will return -EIO if the FW
  623. * call failed.
  624. */
  625. return pm_rtas_activate_signals(cbe_cpu_to_node(cpu), num_enabled);
  626. }
  627. #define ENTRIES 303
  628. #define MAXLFSR 0xFFFFFF
  629. /* precomputed table of 24 bit LFSR values */
  630. static int initial_lfsr[] = {
  631. 8221349, 12579195, 5379618, 10097839, 7512963, 7519310, 3955098, 10753424,
  632. 15507573, 7458917, 285419, 2641121, 9780088, 3915503, 6668768, 1548716,
  633. 4885000, 8774424, 9650099, 2044357, 2304411, 9326253, 10332526, 4421547,
  634. 3440748, 10179459, 13332843, 10375561, 1313462, 8375100, 5198480, 6071392,
  635. 9341783, 1526887, 3985002, 1439429, 13923762, 7010104, 11969769, 4547026,
  636. 2040072, 4025602, 3437678, 7939992, 11444177, 4496094, 9803157, 10745556,
  637. 3671780, 4257846, 5662259, 13196905, 3237343, 12077182, 16222879, 7587769,
  638. 14706824, 2184640, 12591135, 10420257, 7406075, 3648978, 11042541, 15906893,
  639. 11914928, 4732944, 10695697, 12928164, 11980531, 4430912, 11939291, 2917017,
  640. 6119256, 4172004, 9373765, 8410071, 14788383, 5047459, 5474428, 1737756,
  641. 15967514, 13351758, 6691285, 8034329, 2856544, 14394753, 11310160, 12149558,
  642. 7487528, 7542781, 15668898, 12525138, 12790975, 3707933, 9106617, 1965401,
  643. 16219109, 12801644, 2443203, 4909502, 8762329, 3120803, 6360315, 9309720,
  644. 15164599, 10844842, 4456529, 6667610, 14924259, 884312, 6234963, 3326042,
  645. 15973422, 13919464, 5272099, 6414643, 3909029, 2764324, 5237926, 4774955,
  646. 10445906, 4955302, 5203726, 10798229, 11443419, 2303395, 333836, 9646934,
  647. 3464726, 4159182, 568492, 995747, 10318756, 13299332, 4836017, 8237783,
  648. 3878992, 2581665, 11394667, 5672745, 14412947, 3159169, 9094251, 16467278,
  649. 8671392, 15230076, 4843545, 7009238, 15504095, 1494895, 9627886, 14485051,
  650. 8304291, 252817, 12421642, 16085736, 4774072, 2456177, 4160695, 15409741,
  651. 4902868, 5793091, 13162925, 16039714, 782255, 11347835, 14884586, 366972,
  652. 16308990, 11913488, 13390465, 2958444, 10340278, 1177858, 1319431, 10426302,
  653. 2868597, 126119, 5784857, 5245324, 10903900, 16436004, 3389013, 1742384,
  654. 14674502, 10279218, 8536112, 10364279, 6877778, 14051163, 1025130, 6072469,
  655. 1988305, 8354440, 8216060, 16342977, 13112639, 3976679, 5913576, 8816697,
  656. 6879995, 14043764, 3339515, 9364420, 15808858, 12261651, 2141560, 5636398,
  657. 10345425, 10414756, 781725, 6155650, 4746914, 5078683, 7469001, 6799140,
  658. 10156444, 9667150, 10116470, 4133858, 2121972, 1124204, 1003577, 1611214,
  659. 14304602, 16221850, 13878465, 13577744, 3629235, 8772583, 10881308, 2410386,
  660. 7300044, 5378855, 9301235, 12755149, 4977682, 8083074, 10327581, 6395087,
  661. 9155434, 15501696, 7514362, 14520507, 15808945, 3244584, 4741962, 9658130,
  662. 14336147, 8654727, 7969093, 15759799, 14029445, 5038459, 9894848, 8659300,
  663. 13699287, 8834306, 10712885, 14753895, 10410465, 3373251, 309501, 9561475,
  664. 5526688, 14647426, 14209836, 5339224, 207299, 14069911, 8722990, 2290950,
  665. 3258216, 12505185, 6007317, 9218111, 14661019, 10537428, 11731949, 9027003,
  666. 6641507, 9490160, 200241, 9720425, 16277895, 10816638, 1554761, 10431375,
  667. 7467528, 6790302, 3429078, 14633753, 14428997, 11463204, 3576212, 2003426,
  668. 6123687, 820520, 9992513, 15784513, 5778891, 6428165, 8388607
  669. };
  670. /*
  671. * The hardware uses an LFSR counting sequence to determine when to capture
  672. * the SPU PCs. An LFSR sequence is like a puesdo random number sequence
  673. * where each number occurs once in the sequence but the sequence is not in
  674. * numerical order. The SPU PC capture is done when the LFSR sequence reaches
  675. * the last value in the sequence. Hence the user specified value N
  676. * corresponds to the LFSR number that is N from the end of the sequence.
  677. *
  678. * To avoid the time to compute the LFSR, a lookup table is used. The 24 bit
  679. * LFSR sequence is broken into four ranges. The spacing of the precomputed
  680. * values is adjusted in each range so the error between the user specifed
  681. * number (N) of events between samples and the actual number of events based
  682. * on the precomputed value will be les then about 6.2%. Note, if the user
  683. * specifies N < 2^16, the LFSR value that is 2^16 from the end will be used.
  684. * This is to prevent the loss of samples because the trace buffer is full.
  685. *
  686. * User specified N Step between Index in
  687. * precomputed values precomputed
  688. * table
  689. * 0 to 2^16-1 ---- 0
  690. * 2^16 to 2^16+2^19-1 2^12 1 to 128
  691. * 2^16+2^19 to 2^16+2^19+2^22-1 2^15 129 to 256
  692. * 2^16+2^19+2^22 to 2^24-1 2^18 257 to 302
  693. *
  694. *
  695. * For example, the LFSR values in the second range are computed for 2^16,
  696. * 2^16+2^12, ... , 2^19-2^16, 2^19 and stored in the table at indicies
  697. * 1, 2,..., 127, 128.
  698. *
  699. * The 24 bit LFSR value for the nth number in the sequence can be
  700. * calculated using the following code:
  701. *
  702. * #define size 24
  703. * int calculate_lfsr(int n)
  704. * {
  705. * int i;
  706. * unsigned int newlfsr0;
  707. * unsigned int lfsr = 0xFFFFFF;
  708. * unsigned int howmany = n;
  709. *
  710. * for (i = 2; i < howmany + 2; i++) {
  711. * newlfsr0 = (((lfsr >> (size - 1 - 0)) & 1) ^
  712. * ((lfsr >> (size - 1 - 1)) & 1) ^
  713. * (((lfsr >> (size - 1 - 6)) & 1) ^
  714. * ((lfsr >> (size - 1 - 23)) & 1)));
  715. *
  716. * lfsr >>= 1;
  717. * lfsr = lfsr | (newlfsr0 << (size - 1));
  718. * }
  719. * return lfsr;
  720. * }
  721. */
  722. #define V2_16 (0x1 << 16)
  723. #define V2_19 (0x1 << 19)
  724. #define V2_22 (0x1 << 22)
  725. static int calculate_lfsr(int n)
  726. {
  727. /*
  728. * The ranges and steps are in powers of 2 so the calculations
  729. * can be done using shifts rather then divide.
  730. */
  731. int index;
  732. if ((n >> 16) == 0)
  733. index = 0;
  734. else if (((n - V2_16) >> 19) == 0)
  735. index = ((n - V2_16) >> 12) + 1;
  736. else if (((n - V2_16 - V2_19) >> 22) == 0)
  737. index = ((n - V2_16 - V2_19) >> 15 ) + 1 + 128;
  738. else if (((n - V2_16 - V2_19 - V2_22) >> 24) == 0)
  739. index = ((n - V2_16 - V2_19 - V2_22) >> 18 ) + 1 + 256;
  740. else
  741. index = ENTRIES-1;
  742. /* make sure index is valid */
  743. if ((index > ENTRIES) || (index < 0))
  744. index = ENTRIES-1;
  745. return initial_lfsr[index];
  746. }
  747. static int pm_rtas_activate_spu_profiling(u32 node)
  748. {
  749. int ret, i;
  750. struct pm_signal pm_signal_local[NUM_SPUS_PER_NODE];
  751. /*
  752. * Set up the rtas call to configure the debug bus to
  753. * route the SPU PCs. Setup the pm_signal for each SPU
  754. */
  755. for (i = 0; i < ARRAY_SIZE(pm_signal_local); i++) {
  756. pm_signal_local[i].cpu = node;
  757. pm_signal_local[i].signal_group = 41;
  758. /* spu i on word (i/2) */
  759. pm_signal_local[i].bus_word = 1 << i / 2;
  760. /* spu i */
  761. pm_signal_local[i].sub_unit = i;
  762. pm_signal_local[i].bit = 63;
  763. }
  764. ret = rtas_ibm_cbe_perftools(SUBFUNC_ACTIVATE,
  765. PASSTHRU_ENABLE, pm_signal_local,
  766. (ARRAY_SIZE(pm_signal_local)
  767. * sizeof(struct pm_signal)));
  768. if (unlikely(ret)) {
  769. printk(KERN_WARNING "%s: rtas returned: %d\n",
  770. __func__, ret);
  771. return -EIO;
  772. }
  773. return 0;
  774. }
  775. #ifdef CONFIG_CPU_FREQ
  776. static int
  777. oprof_cpufreq_notify(struct notifier_block *nb, unsigned long val, void *data)
  778. {
  779. int ret = 0;
  780. struct cpufreq_freqs *frq = data;
  781. if ((val == CPUFREQ_PRECHANGE && frq->old < frq->new) ||
  782. (val == CPUFREQ_POSTCHANGE && frq->old > frq->new) ||
  783. (val == CPUFREQ_RESUMECHANGE || val == CPUFREQ_SUSPENDCHANGE))
  784. set_spu_profiling_frequency(frq->new, spu_cycle_reset);
  785. return ret;
  786. }
  787. static struct notifier_block cpu_freq_notifier_block = {
  788. .notifier_call = oprof_cpufreq_notify
  789. };
  790. #endif
  791. /*
  792. * Note the generic OProfile stop calls do not support returning
  793. * an error on stop. Hence, will not return an error if the FW
  794. * calls fail on stop. Failure to reset the debug bus is not an issue.
  795. * Failure to disable the SPU profiling is not an issue. The FW calls
  796. * to enable the performance counters and debug bus will work even if
  797. * the hardware was not cleanly reset.
  798. */
  799. static void cell_global_stop_spu_cycles(void)
  800. {
  801. int subfunc, rtn_value;
  802. unsigned int lfsr_value;
  803. int cpu;
  804. oprofile_running = 0;
  805. #ifdef CONFIG_CPU_FREQ
  806. cpufreq_unregister_notifier(&cpu_freq_notifier_block,
  807. CPUFREQ_TRANSITION_NOTIFIER);
  808. #endif
  809. for_each_online_cpu(cpu) {
  810. if (cbe_get_hw_thread_id(cpu))
  811. continue;
  812. subfunc = 3; /*
  813. * 2 - activate SPU tracing,
  814. * 3 - deactivate
  815. */
  816. lfsr_value = 0x8f100000;
  817. rtn_value = rtas_call(spu_rtas_token, 3, 1, NULL,
  818. subfunc, cbe_cpu_to_node(cpu),
  819. lfsr_value);
  820. if (unlikely(rtn_value != 0)) {
  821. printk(KERN_ERR
  822. "%s: rtas call ibm,cbe-spu-perftools " \
  823. "failed, return = %d\n",
  824. __func__, rtn_value);
  825. }
  826. /* Deactivate the signals */
  827. pm_rtas_reset_signals(cbe_cpu_to_node(cpu));
  828. }
  829. if (profiling_mode == SPU_PROFILING_CYCLES)
  830. stop_spu_profiling_cycles();
  831. }
  832. static void cell_global_stop_ppu(void)
  833. {
  834. int cpu;
  835. /*
  836. * This routine will be called once for the system.
  837. * There is one performance monitor per node, so we
  838. * only need to perform this function once per node.
  839. */
  840. del_timer_sync(&timer_virt_cntr);
  841. oprofile_running = 0;
  842. smp_wmb();
  843. for_each_online_cpu(cpu) {
  844. if (cbe_get_hw_thread_id(cpu))
  845. continue;
  846. cbe_sync_irq(cbe_cpu_to_node(cpu));
  847. /* Stop the counters */
  848. cbe_disable_pm(cpu);
  849. /* Deactivate the signals */
  850. pm_rtas_reset_signals(cbe_cpu_to_node(cpu));
  851. /* Deactivate interrupts */
  852. cbe_disable_pm_interrupts(cpu);
  853. }
  854. }
  855. static void cell_global_stop(void)
  856. {
  857. if (profiling_mode == PPU_PROFILING)
  858. cell_global_stop_ppu();
  859. else
  860. cell_global_stop_spu_cycles();
  861. }
  862. static int cell_global_start_spu_cycles(struct op_counter_config *ctr)
  863. {
  864. int subfunc;
  865. unsigned int lfsr_value;
  866. int cpu;
  867. int ret;
  868. int rtas_error;
  869. unsigned int cpu_khzfreq = 0;
  870. /* The SPU profiling uses time-based profiling based on
  871. * cpu frequency, so if configured with the CPU_FREQ
  872. * option, we should detect frequency changes and react
  873. * accordingly.
  874. */
  875. #ifdef CONFIG_CPU_FREQ
  876. ret = cpufreq_register_notifier(&cpu_freq_notifier_block,
  877. CPUFREQ_TRANSITION_NOTIFIER);
  878. if (ret < 0)
  879. /* this is not a fatal error */
  880. printk(KERN_ERR "CPU freq change registration failed: %d\n",
  881. ret);
  882. else
  883. cpu_khzfreq = cpufreq_quick_get(smp_processor_id());
  884. #endif
  885. set_spu_profiling_frequency(cpu_khzfreq, spu_cycle_reset);
  886. for_each_online_cpu(cpu) {
  887. if (cbe_get_hw_thread_id(cpu))
  888. continue;
  889. /*
  890. * Setup SPU cycle-based profiling.
  891. * Set perf_mon_control bit 0 to a zero before
  892. * enabling spu collection hardware.
  893. */
  894. cbe_write_pm(cpu, pm_control, 0);
  895. if (spu_cycle_reset > MAX_SPU_COUNT)
  896. /* use largest possible value */
  897. lfsr_value = calculate_lfsr(MAX_SPU_COUNT-1);
  898. else
  899. lfsr_value = calculate_lfsr(spu_cycle_reset);
  900. /* must use a non zero value. Zero disables data collection. */
  901. if (lfsr_value == 0)
  902. lfsr_value = calculate_lfsr(1);
  903. lfsr_value = lfsr_value << 8; /* shift lfsr to correct
  904. * register location
  905. */
  906. /* debug bus setup */
  907. ret = pm_rtas_activate_spu_profiling(cbe_cpu_to_node(cpu));
  908. if (unlikely(ret)) {
  909. rtas_error = ret;
  910. goto out;
  911. }
  912. subfunc = 2; /* 2 - activate SPU tracing, 3 - deactivate */
  913. /* start profiling */
  914. ret = rtas_call(spu_rtas_token, 3, 1, NULL, subfunc,
  915. cbe_cpu_to_node(cpu), lfsr_value);
  916. if (unlikely(ret != 0)) {
  917. printk(KERN_ERR
  918. "%s: rtas call ibm,cbe-spu-perftools failed, " \
  919. "return = %d\n", __func__, ret);
  920. rtas_error = -EIO;
  921. goto out;
  922. }
  923. }
  924. rtas_error = start_spu_profiling_cycles(spu_cycle_reset);
  925. if (rtas_error)
  926. goto out_stop;
  927. oprofile_running = 1;
  928. return 0;
  929. out_stop:
  930. cell_global_stop_spu_cycles(); /* clean up the PMU/debug bus */
  931. out:
  932. return rtas_error;
  933. }
  934. static int cell_global_start_ppu(struct op_counter_config *ctr)
  935. {
  936. u32 cpu, i;
  937. u32 interrupt_mask = 0;
  938. /* This routine gets called once for the system.
  939. * There is one performance monitor per node, so we
  940. * only need to perform this function once per node.
  941. */
  942. for_each_online_cpu(cpu) {
  943. if (cbe_get_hw_thread_id(cpu))
  944. continue;
  945. interrupt_mask = 0;
  946. for (i = 0; i < num_counters; ++i) {
  947. if (ctr_enabled & (1 << i)) {
  948. cbe_write_ctr(cpu, i, reset_value[i]);
  949. enable_ctr(cpu, i, pm_regs.pm07_cntrl);
  950. interrupt_mask |=
  951. CBE_PM_CTR_OVERFLOW_INTR(i);
  952. } else {
  953. /* Disable counter */
  954. cbe_write_pm07_control(cpu, i, 0);
  955. }
  956. }
  957. cbe_get_and_clear_pm_interrupts(cpu);
  958. cbe_enable_pm_interrupts(cpu, hdw_thread, interrupt_mask);
  959. cbe_enable_pm(cpu);
  960. }
  961. virt_cntr_inter_mask = interrupt_mask;
  962. oprofile_running = 1;
  963. smp_wmb();
  964. /*
  965. * NOTE: start_virt_cntrs will result in cell_virtual_cntr() being
  966. * executed which manipulates the PMU. We start the "virtual counter"
  967. * here so that we do not need to synchronize access to the PMU in
  968. * the above for-loop.
  969. */
  970. start_virt_cntrs();
  971. return 0;
  972. }
  973. static int cell_global_start(struct op_counter_config *ctr)
  974. {
  975. if (profiling_mode == SPU_PROFILING_CYCLES)
  976. return cell_global_start_spu_cycles(ctr);
  977. else
  978. return cell_global_start_ppu(ctr);
  979. }
  980. static void cell_handle_interrupt_ppu(struct pt_regs *regs,
  981. struct op_counter_config *ctr)
  982. {
  983. u32 cpu;
  984. u64 pc;
  985. int is_kernel;
  986. unsigned long flags = 0;
  987. u32 interrupt_mask;
  988. int i;
  989. cpu = smp_processor_id();
  990. /*
  991. * Need to make sure the interrupt handler and the virt counter
  992. * routine are not running at the same time. See the
  993. * cell_virtual_cntr() routine for additional comments.
  994. */
  995. spin_lock_irqsave(&cntr_lock, flags);
  996. /*
  997. * Need to disable and reenable the performance counters
  998. * to get the desired behavior from the hardware. This
  999. * is hardware specific.
  1000. */
  1001. cbe_disable_pm(cpu);
  1002. interrupt_mask = cbe_get_and_clear_pm_interrupts(cpu);
  1003. /*
  1004. * If the interrupt mask has been cleared, then the virt cntr
  1005. * has cleared the interrupt. When the thread that generated
  1006. * the interrupt is restored, the data count will be restored to
  1007. * 0xffffff0 to cause the interrupt to be regenerated.
  1008. */
  1009. if ((oprofile_running == 1) && (interrupt_mask != 0)) {
  1010. pc = regs->nip;
  1011. is_kernel = is_kernel_addr(pc);
  1012. for (i = 0; i < num_counters; ++i) {
  1013. if ((interrupt_mask & CBE_PM_CTR_OVERFLOW_INTR(i))
  1014. && ctr[i].enabled) {
  1015. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  1016. cbe_write_ctr(cpu, i, reset_value[i]);
  1017. }
  1018. }
  1019. /*
  1020. * The counters were frozen by the interrupt.
  1021. * Reenable the interrupt and restart the counters.
  1022. * If there was a race between the interrupt handler and
  1023. * the virtual counter routine. The virutal counter
  1024. * routine may have cleared the interrupts. Hence must
  1025. * use the virt_cntr_inter_mask to re-enable the interrupts.
  1026. */
  1027. cbe_enable_pm_interrupts(cpu, hdw_thread,
  1028. virt_cntr_inter_mask);
  1029. /*
  1030. * The writes to the various performance counters only writes
  1031. * to a latch. The new values (interrupt setting bits, reset
  1032. * counter value etc.) are not copied to the actual registers
  1033. * until the performance monitor is enabled. In order to get
  1034. * this to work as desired, the permormance monitor needs to
  1035. * be disabled while writing to the latches. This is a
  1036. * HW design issue.
  1037. */
  1038. cbe_enable_pm(cpu);
  1039. }
  1040. spin_unlock_irqrestore(&cntr_lock, flags);
  1041. }
  1042. static void cell_handle_interrupt(struct pt_regs *regs,
  1043. struct op_counter_config *ctr)
  1044. {
  1045. if (profiling_mode == PPU_PROFILING)
  1046. cell_handle_interrupt_ppu(regs, ctr);
  1047. }
  1048. /*
  1049. * This function is called from the generic OProfile
  1050. * driver. When profiling PPUs, we need to do the
  1051. * generic sync start; otherwise, do spu_sync_start.
  1052. */
  1053. static int cell_sync_start(void)
  1054. {
  1055. if ((profiling_mode == SPU_PROFILING_CYCLES) ||
  1056. (profiling_mode == SPU_PROFILING_EVENTS))
  1057. return spu_sync_start();
  1058. else
  1059. return DO_GENERIC_SYNC;
  1060. }
  1061. static int cell_sync_stop(void)
  1062. {
  1063. if ((profiling_mode == SPU_PROFILING_CYCLES) ||
  1064. (profiling_mode == SPU_PROFILING_EVENTS))
  1065. return spu_sync_stop();
  1066. else
  1067. return 1;
  1068. }
  1069. struct op_powerpc_model op_model_cell = {
  1070. .reg_setup = cell_reg_setup,
  1071. .cpu_setup = cell_cpu_setup,
  1072. .global_start = cell_global_start,
  1073. .global_stop = cell_global_stop,
  1074. .sync_start = cell_sync_start,
  1075. .sync_stop = cell_sync_stop,
  1076. .handle_interrupt = cell_handle_interrupt,
  1077. };