op_model_cell.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 "../platforms/cell/interrupt.h"
  38. #define PPU_CYCLES_EVENT_NUM 1 /* event number for CYCLES */
  39. #define CBE_COUNT_ALL_CYCLES 0x42800000 /* PPU cycle event specifier */
  40. #define NUM_THREADS 2
  41. #define VIRT_CNTR_SW_TIME_NS 100000000 // 0.5 seconds
  42. struct pmc_cntrl_data {
  43. unsigned long vcntr;
  44. unsigned long evnts;
  45. unsigned long masks;
  46. unsigned long enabled;
  47. };
  48. /*
  49. * ibm,cbe-perftools rtas parameters
  50. */
  51. struct pm_signal {
  52. u16 cpu; /* Processor to modify */
  53. u16 sub_unit; /* hw subunit this applies to (if applicable) */
  54. u16 signal_group; /* Signal Group to Enable/Disable */
  55. u8 bus_word; /* Enable/Disable on this Trace/Trigger/Event
  56. * Bus Word(s) (bitmask)
  57. */
  58. u8 bit; /* Trigger/Event bit (if applicable) */
  59. };
  60. /*
  61. * rtas call arguments
  62. */
  63. enum {
  64. SUBFUNC_RESET = 1,
  65. SUBFUNC_ACTIVATE = 2,
  66. SUBFUNC_DEACTIVATE = 3,
  67. PASSTHRU_IGNORE = 0,
  68. PASSTHRU_ENABLE = 1,
  69. PASSTHRU_DISABLE = 2,
  70. };
  71. struct pm_cntrl {
  72. u16 enable;
  73. u16 stop_at_max;
  74. u16 trace_mode;
  75. u16 freeze;
  76. u16 count_mode;
  77. };
  78. static struct {
  79. u32 group_control;
  80. u32 debug_bus_control;
  81. struct pm_cntrl pm_cntrl;
  82. u32 pm07_cntrl[NR_PHYS_CTRS];
  83. } pm_regs;
  84. #define GET_SUB_UNIT(x) ((x & 0x0000f000) >> 12)
  85. #define GET_BUS_WORD(x) ((x & 0x000000f0) >> 4)
  86. #define GET_BUS_TYPE(x) ((x & 0x00000300) >> 8)
  87. #define GET_POLARITY(x) ((x & 0x00000002) >> 1)
  88. #define GET_COUNT_CYCLES(x) (x & 0x00000001)
  89. #define GET_INPUT_CONTROL(x) ((x & 0x00000004) >> 2)
  90. static DEFINE_PER_CPU(unsigned long[NR_PHYS_CTRS], pmc_values);
  91. static struct pmc_cntrl_data pmc_cntrl[NUM_THREADS][NR_PHYS_CTRS];
  92. /* Interpetation of hdw_thread:
  93. * 0 - even virtual cpus 0, 2, 4,...
  94. * 1 - odd virtual cpus 1, 3, 5, ...
  95. */
  96. static u32 hdw_thread;
  97. static u32 virt_cntr_inter_mask;
  98. static struct timer_list timer_virt_cntr;
  99. /* pm_signal needs to be global since it is initialized in
  100. * cell_reg_setup at the time when the necessary information
  101. * is available.
  102. */
  103. static struct pm_signal pm_signal[NR_PHYS_CTRS];
  104. static int pm_rtas_token;
  105. static u32 reset_value[NR_PHYS_CTRS];
  106. static int num_counters;
  107. static int oprofile_running;
  108. static spinlock_t virt_cntr_lock = SPIN_LOCK_UNLOCKED;
  109. static u32 ctr_enabled;
  110. static unsigned char trace_bus[4];
  111. static unsigned char input_bus[2];
  112. /*
  113. * Firmware interface functions
  114. */
  115. static int
  116. rtas_ibm_cbe_perftools(int subfunc, int passthru,
  117. void *address, unsigned long length)
  118. {
  119. u64 paddr = __pa(address);
  120. return rtas_call(pm_rtas_token, 5, 1, NULL, subfunc, passthru,
  121. paddr >> 32, paddr & 0xffffffff, length);
  122. }
  123. static void pm_rtas_reset_signals(u32 node)
  124. {
  125. int ret;
  126. struct pm_signal pm_signal_local;
  127. /* The debug bus is being set to the passthru disable state.
  128. * However, the FW still expects atleast one legal signal routing
  129. * entry or it will return an error on the arguments. If we don't
  130. * supply a valid entry, we must ignore all return values. Ignoring
  131. * all return values means we might miss an error we should be
  132. * concerned about.
  133. */
  134. /* fw expects physical cpu #. */
  135. pm_signal_local.cpu = node;
  136. pm_signal_local.signal_group = 21;
  137. pm_signal_local.bus_word = 1;
  138. pm_signal_local.sub_unit = 0;
  139. pm_signal_local.bit = 0;
  140. ret = rtas_ibm_cbe_perftools(SUBFUNC_RESET, PASSTHRU_DISABLE,
  141. &pm_signal_local,
  142. sizeof(struct pm_signal));
  143. if (ret)
  144. printk(KERN_WARNING "%s: rtas returned: %d\n",
  145. __FUNCTION__, ret);
  146. }
  147. static void pm_rtas_activate_signals(u32 node, u32 count)
  148. {
  149. int ret;
  150. int j;
  151. struct pm_signal pm_signal_local[NR_PHYS_CTRS];
  152. for (j = 0; j < count; j++) {
  153. /* fw expects physical cpu # */
  154. pm_signal_local[j].cpu = node;
  155. pm_signal_local[j].signal_group = pm_signal[j].signal_group;
  156. pm_signal_local[j].bus_word = pm_signal[j].bus_word;
  157. pm_signal_local[j].sub_unit = pm_signal[j].sub_unit;
  158. pm_signal_local[j].bit = pm_signal[j].bit;
  159. }
  160. ret = rtas_ibm_cbe_perftools(SUBFUNC_ACTIVATE, PASSTHRU_ENABLE,
  161. pm_signal_local,
  162. count * sizeof(struct pm_signal));
  163. if (ret)
  164. printk(KERN_WARNING "%s: rtas returned: %d\n",
  165. __FUNCTION__, ret);
  166. }
  167. /*
  168. * PM Signal functions
  169. */
  170. static void set_pm_event(u32 ctr, int event, u32 unit_mask)
  171. {
  172. struct pm_signal *p;
  173. u32 signal_bit;
  174. u32 bus_word, bus_type, count_cycles, polarity, input_control;
  175. int j, i;
  176. if (event == PPU_CYCLES_EVENT_NUM) {
  177. /* Special Event: Count all cpu cycles */
  178. pm_regs.pm07_cntrl[ctr] = CBE_COUNT_ALL_CYCLES;
  179. p = &(pm_signal[ctr]);
  180. p->signal_group = 21;
  181. p->bus_word = 1;
  182. p->sub_unit = 0;
  183. p->bit = 0;
  184. goto out;
  185. } else {
  186. pm_regs.pm07_cntrl[ctr] = 0;
  187. }
  188. bus_word = GET_BUS_WORD(unit_mask);
  189. bus_type = GET_BUS_TYPE(unit_mask);
  190. count_cycles = GET_COUNT_CYCLES(unit_mask);
  191. polarity = GET_POLARITY(unit_mask);
  192. input_control = GET_INPUT_CONTROL(unit_mask);
  193. signal_bit = (event % 100);
  194. p = &(pm_signal[ctr]);
  195. p->signal_group = event / 100;
  196. p->bus_word = bus_word;
  197. p->sub_unit = unit_mask & 0x0000f000;
  198. pm_regs.pm07_cntrl[ctr] = 0;
  199. pm_regs.pm07_cntrl[ctr] |= PM07_CTR_COUNT_CYCLES(count_cycles);
  200. pm_regs.pm07_cntrl[ctr] |= PM07_CTR_POLARITY(polarity);
  201. pm_regs.pm07_cntrl[ctr] |= PM07_CTR_INPUT_CONTROL(input_control);
  202. if (input_control == 0) {
  203. if (signal_bit > 31) {
  204. signal_bit -= 32;
  205. if (bus_word == 0x3)
  206. bus_word = 0x2;
  207. else if (bus_word == 0xc)
  208. bus_word = 0x8;
  209. }
  210. if ((bus_type == 0) && p->signal_group >= 60)
  211. bus_type = 2;
  212. if ((bus_type == 1) && p->signal_group >= 50)
  213. bus_type = 0;
  214. pm_regs.pm07_cntrl[ctr] |= PM07_CTR_INPUT_MUX(signal_bit);
  215. } else {
  216. pm_regs.pm07_cntrl[ctr] = 0;
  217. p->bit = signal_bit;
  218. }
  219. for (i = 0; i < 4; i++) {
  220. if (bus_word & (1 << i)) {
  221. pm_regs.debug_bus_control |=
  222. (bus_type << (31 - (2 * i) + 1));
  223. for (j = 0; j < 2; j++) {
  224. if (input_bus[j] == 0xff) {
  225. input_bus[j] = i;
  226. pm_regs.group_control |=
  227. (i << (31 - i));
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. out:
  234. ;
  235. }
  236. static void write_pm_cntrl(int cpu, struct pm_cntrl *pm_cntrl)
  237. {
  238. /* Oprofile will use 32 bit counters, set bits 7:10 to 0 */
  239. u32 val = 0;
  240. if (pm_cntrl->enable == 1)
  241. val |= CBE_PM_ENABLE_PERF_MON;
  242. if (pm_cntrl->stop_at_max == 1)
  243. val |= CBE_PM_STOP_AT_MAX;
  244. if (pm_cntrl->trace_mode == 1)
  245. val |= CBE_PM_TRACE_MODE_SET(pm_cntrl->trace_mode);
  246. if (pm_cntrl->freeze == 1)
  247. val |= CBE_PM_FREEZE_ALL_CTRS;
  248. /* Routine set_count_mode must be called previously to set
  249. * the count mode based on the user selection of user and kernel.
  250. */
  251. val |= CBE_PM_COUNT_MODE_SET(pm_cntrl->count_mode);
  252. cbe_write_pm(cpu, pm_control, val);
  253. }
  254. static inline void
  255. set_count_mode(u32 kernel, u32 user, struct pm_cntrl *pm_cntrl)
  256. {
  257. /* The user must specify user and kernel if they want them. If
  258. * neither is specified, OProfile will count in hypervisor mode
  259. */
  260. if (kernel) {
  261. if (user)
  262. pm_cntrl->count_mode = CBE_COUNT_ALL_MODES;
  263. else
  264. pm_cntrl->count_mode = CBE_COUNT_SUPERVISOR_MODE;
  265. } else {
  266. if (user)
  267. pm_cntrl->count_mode = CBE_COUNT_PROBLEM_MODE;
  268. else
  269. pm_cntrl->count_mode = CBE_COUNT_HYPERVISOR_MODE;
  270. }
  271. }
  272. static inline void enable_ctr(u32 cpu, u32 ctr, u32 * pm07_cntrl)
  273. {
  274. pm07_cntrl[ctr] |= PM07_CTR_ENABLE(1);
  275. cbe_write_pm07_control(cpu, ctr, pm07_cntrl[ctr]);
  276. }
  277. /*
  278. * Oprofile is expected to collect data on all CPUs simultaneously.
  279. * However, there is one set of performance counters per node. There are
  280. * two hardware threads or virtual CPUs on each node. Hence, OProfile must
  281. * multiplex in time the performance counter collection on the two virtual
  282. * CPUs. The multiplexing of the performance counters is done by this
  283. * virtual counter routine.
  284. *
  285. * The pmc_values used below is defined as 'per-cpu' but its use is
  286. * more akin to 'per-node'. We need to store two sets of counter
  287. * values per node -- one for the previous run and one for the next.
  288. * The per-cpu[NR_PHYS_CTRS] gives us the storage we need. Each odd/even
  289. * pair of per-cpu arrays is used for storing the previous and next
  290. * pmc values for a given node.
  291. * NOTE: We use the per-cpu variable to improve cache performance.
  292. */
  293. static void cell_virtual_cntr(unsigned long data)
  294. {
  295. /* This routine will alternate loading the virtual counters for
  296. * virtual CPUs
  297. */
  298. int i, prev_hdw_thread, next_hdw_thread;
  299. u32 cpu;
  300. unsigned long flags;
  301. /* Make sure that the interrupt_hander and
  302. * the virt counter are not both playing with
  303. * the counters on the same node.
  304. */
  305. spin_lock_irqsave(&virt_cntr_lock, flags);
  306. prev_hdw_thread = hdw_thread;
  307. /* switch the cpu handling the interrupts */
  308. hdw_thread = 1 ^ hdw_thread;
  309. next_hdw_thread = hdw_thread;
  310. /* The following is done only once per each node, but
  311. * we need cpu #, not node #, to pass to the cbe_xxx functions.
  312. */
  313. for_each_online_cpu(cpu) {
  314. if (cbe_get_hw_thread_id(cpu))
  315. continue;
  316. /* stop counters, save counter values, restore counts
  317. * for previous thread
  318. */
  319. cbe_disable_pm(cpu);
  320. cbe_disable_pm_interrupts(cpu);
  321. for (i = 0; i < num_counters; i++) {
  322. per_cpu(pmc_values, cpu + prev_hdw_thread)[i]
  323. = cbe_read_ctr(cpu, i);
  324. if (per_cpu(pmc_values, cpu + next_hdw_thread)[i]
  325. == 0xFFFFFFFF)
  326. /* If the cntr value is 0xffffffff, we must
  327. * reset that to 0xfffffff0 when the current
  328. * thread is restarted. This will generate a new
  329. * interrupt and make sure that we never restore
  330. * the counters to the max value. If the counters
  331. * were restored to the max value, they do not
  332. * increment and no interrupts are generated. Hence
  333. * no more samples will be collected on that cpu.
  334. */
  335. cbe_write_ctr(cpu, i, 0xFFFFFFF0);
  336. else
  337. cbe_write_ctr(cpu, i,
  338. per_cpu(pmc_values,
  339. cpu +
  340. next_hdw_thread)[i]);
  341. }
  342. /* Switch to the other thread. Change the interrupt
  343. * and control regs to be scheduled on the CPU
  344. * corresponding to the thread to execute.
  345. */
  346. for (i = 0; i < num_counters; i++) {
  347. if (pmc_cntrl[next_hdw_thread][i].enabled) {
  348. /* There are some per thread events.
  349. * Must do the set event, enable_cntr
  350. * for each cpu.
  351. */
  352. set_pm_event(i,
  353. pmc_cntrl[next_hdw_thread][i].evnts,
  354. pmc_cntrl[next_hdw_thread][i].masks);
  355. enable_ctr(cpu, i,
  356. pm_regs.pm07_cntrl);
  357. } else {
  358. cbe_write_pm07_control(cpu, i, 0);
  359. }
  360. }
  361. /* Enable interrupts on the CPU thread that is starting */
  362. cbe_enable_pm_interrupts(cpu, next_hdw_thread,
  363. virt_cntr_inter_mask);
  364. cbe_enable_pm(cpu);
  365. }
  366. spin_unlock_irqrestore(&virt_cntr_lock, flags);
  367. mod_timer(&timer_virt_cntr, jiffies + HZ / 10);
  368. }
  369. static void start_virt_cntrs(void)
  370. {
  371. init_timer(&timer_virt_cntr);
  372. timer_virt_cntr.function = cell_virtual_cntr;
  373. timer_virt_cntr.data = 0UL;
  374. timer_virt_cntr.expires = jiffies + HZ / 10;
  375. add_timer(&timer_virt_cntr);
  376. }
  377. /* This function is called once for all cpus combined */
  378. static void
  379. cell_reg_setup(struct op_counter_config *ctr,
  380. struct op_system_config *sys, int num_ctrs)
  381. {
  382. int i, j, cpu;
  383. pm_rtas_token = rtas_token("ibm,cbe-perftools");
  384. if (pm_rtas_token == RTAS_UNKNOWN_SERVICE) {
  385. printk(KERN_WARNING "%s: RTAS_UNKNOWN_SERVICE\n",
  386. __FUNCTION__);
  387. goto out;
  388. }
  389. num_counters = num_ctrs;
  390. pm_regs.group_control = 0;
  391. pm_regs.debug_bus_control = 0;
  392. /* setup the pm_control register */
  393. memset(&pm_regs.pm_cntrl, 0, sizeof(struct pm_cntrl));
  394. pm_regs.pm_cntrl.stop_at_max = 1;
  395. pm_regs.pm_cntrl.trace_mode = 0;
  396. pm_regs.pm_cntrl.freeze = 1;
  397. set_count_mode(sys->enable_kernel, sys->enable_user,
  398. &pm_regs.pm_cntrl);
  399. /* Setup the thread 0 events */
  400. for (i = 0; i < num_ctrs; ++i) {
  401. pmc_cntrl[0][i].evnts = ctr[i].event;
  402. pmc_cntrl[0][i].masks = ctr[i].unit_mask;
  403. pmc_cntrl[0][i].enabled = ctr[i].enabled;
  404. pmc_cntrl[0][i].vcntr = i;
  405. for_each_possible_cpu(j)
  406. per_cpu(pmc_values, j)[i] = 0;
  407. }
  408. /* Setup the thread 1 events, map the thread 0 event to the
  409. * equivalent thread 1 event.
  410. */
  411. for (i = 0; i < num_ctrs; ++i) {
  412. if ((ctr[i].event >= 2100) && (ctr[i].event <= 2111))
  413. pmc_cntrl[1][i].evnts = ctr[i].event + 19;
  414. else if (ctr[i].event == 2203)
  415. pmc_cntrl[1][i].evnts = ctr[i].event;
  416. else if ((ctr[i].event >= 2200) && (ctr[i].event <= 2215))
  417. pmc_cntrl[1][i].evnts = ctr[i].event + 16;
  418. else
  419. pmc_cntrl[1][i].evnts = ctr[i].event;
  420. pmc_cntrl[1][i].masks = ctr[i].unit_mask;
  421. pmc_cntrl[1][i].enabled = ctr[i].enabled;
  422. pmc_cntrl[1][i].vcntr = i;
  423. }
  424. for (i = 0; i < 4; i++)
  425. trace_bus[i] = 0xff;
  426. for (i = 0; i < 2; i++)
  427. input_bus[i] = 0xff;
  428. /* Our counters count up, and "count" refers to
  429. * how much before the next interrupt, and we interrupt
  430. * on overflow. So we calculate the starting value
  431. * which will give us "count" until overflow.
  432. * Then we set the events on the enabled counters.
  433. */
  434. for (i = 0; i < num_counters; ++i) {
  435. /* start with virtual counter set 0 */
  436. if (pmc_cntrl[0][i].enabled) {
  437. /* Using 32bit counters, reset max - count */
  438. reset_value[i] = 0xFFFFFFFF - ctr[i].count;
  439. set_pm_event(i,
  440. pmc_cntrl[0][i].evnts,
  441. pmc_cntrl[0][i].masks);
  442. /* global, used by cell_cpu_setup */
  443. ctr_enabled |= (1 << i);
  444. }
  445. }
  446. /* initialize the previous counts for the virtual cntrs */
  447. for_each_online_cpu(cpu)
  448. for (i = 0; i < num_counters; ++i) {
  449. per_cpu(pmc_values, cpu)[i] = reset_value[i];
  450. }
  451. out:
  452. ;
  453. }
  454. /* This function is called once for each cpu */
  455. static void cell_cpu_setup(struct op_counter_config *cntr)
  456. {
  457. u32 cpu = smp_processor_id();
  458. u32 num_enabled = 0;
  459. int i;
  460. /* There is one performance monitor per processor chip (i.e. node),
  461. * so we only need to perform this function once per node.
  462. */
  463. if (cbe_get_hw_thread_id(cpu))
  464. goto out;
  465. if (pm_rtas_token == RTAS_UNKNOWN_SERVICE) {
  466. printk(KERN_WARNING "%s: RTAS_UNKNOWN_SERVICE\n",
  467. __FUNCTION__);
  468. goto out;
  469. }
  470. /* Stop all counters */
  471. cbe_disable_pm(cpu);
  472. cbe_disable_pm_interrupts(cpu);
  473. cbe_write_pm(cpu, pm_interval, 0);
  474. cbe_write_pm(cpu, pm_start_stop, 0);
  475. cbe_write_pm(cpu, group_control, pm_regs.group_control);
  476. cbe_write_pm(cpu, debug_bus_control, pm_regs.debug_bus_control);
  477. write_pm_cntrl(cpu, &pm_regs.pm_cntrl);
  478. for (i = 0; i < num_counters; ++i) {
  479. if (ctr_enabled & (1 << i)) {
  480. pm_signal[num_enabled].cpu = cbe_cpu_to_node(cpu);
  481. num_enabled++;
  482. }
  483. }
  484. pm_rtas_activate_signals(cbe_cpu_to_node(cpu), num_enabled);
  485. out:
  486. ;
  487. }
  488. static void cell_global_start(struct op_counter_config *ctr)
  489. {
  490. u32 cpu;
  491. u32 interrupt_mask = 0;
  492. u32 i;
  493. /* This routine gets called once for the system.
  494. * There is one performance monitor per node, so we
  495. * only need to perform this function once per node.
  496. */
  497. for_each_online_cpu(cpu) {
  498. if (cbe_get_hw_thread_id(cpu))
  499. continue;
  500. interrupt_mask = 0;
  501. for (i = 0; i < num_counters; ++i) {
  502. if (ctr_enabled & (1 << i)) {
  503. cbe_write_ctr(cpu, i, reset_value[i]);
  504. enable_ctr(cpu, i, pm_regs.pm07_cntrl);
  505. interrupt_mask |=
  506. CBE_PM_CTR_OVERFLOW_INTR(i);
  507. } else {
  508. /* Disable counter */
  509. cbe_write_pm07_control(cpu, i, 0);
  510. }
  511. }
  512. cbe_clear_pm_interrupts(cpu);
  513. cbe_enable_pm_interrupts(cpu, hdw_thread, interrupt_mask);
  514. cbe_enable_pm(cpu);
  515. }
  516. virt_cntr_inter_mask = interrupt_mask;
  517. oprofile_running = 1;
  518. smp_wmb();
  519. /* NOTE: start_virt_cntrs will result in cell_virtual_cntr() being
  520. * executed which manipulates the PMU. We start the "virtual counter"
  521. * here so that we do not need to synchronize access to the PMU in
  522. * the above for-loop.
  523. */
  524. start_virt_cntrs();
  525. }
  526. static void cell_global_stop(void)
  527. {
  528. int cpu;
  529. /* This routine will be called once for the system.
  530. * There is one performance monitor per node, so we
  531. * only need to perform this function once per node.
  532. */
  533. del_timer_sync(&timer_virt_cntr);
  534. oprofile_running = 0;
  535. smp_wmb();
  536. for_each_online_cpu(cpu) {
  537. if (cbe_get_hw_thread_id(cpu))
  538. continue;
  539. cbe_sync_irq(cbe_cpu_to_node(cpu));
  540. /* Stop the counters */
  541. cbe_disable_pm(cpu);
  542. /* Deactivate the signals */
  543. pm_rtas_reset_signals(cbe_cpu_to_node(cpu));
  544. /* Deactivate interrupts */
  545. cbe_disable_pm_interrupts(cpu);
  546. }
  547. }
  548. static void
  549. cell_handle_interrupt(struct pt_regs *regs, struct op_counter_config *ctr)
  550. {
  551. u32 cpu;
  552. u64 pc;
  553. int is_kernel;
  554. unsigned long flags = 0;
  555. u32 interrupt_mask;
  556. int i;
  557. cpu = smp_processor_id();
  558. /* Need to make sure the interrupt handler and the virt counter
  559. * routine are not running at the same time. See the
  560. * cell_virtual_cntr() routine for additional comments.
  561. */
  562. spin_lock_irqsave(&virt_cntr_lock, flags);
  563. /* Need to disable and reenable the performance counters
  564. * to get the desired behavior from the hardware. This
  565. * is hardware specific.
  566. */
  567. cbe_disable_pm(cpu);
  568. interrupt_mask = cbe_clear_pm_interrupts(cpu);
  569. /* If the interrupt mask has been cleared, then the virt cntr
  570. * has cleared the interrupt. When the thread that generated
  571. * the interrupt is restored, the data count will be restored to
  572. * 0xffffff0 to cause the interrupt to be regenerated.
  573. */
  574. if ((oprofile_running == 1) && (interrupt_mask != 0)) {
  575. pc = regs->nip;
  576. is_kernel = is_kernel_addr(pc);
  577. for (i = 0; i < num_counters; ++i) {
  578. if ((interrupt_mask & CBE_PM_CTR_OVERFLOW_INTR(i))
  579. && ctr[i].enabled) {
  580. oprofile_add_pc(pc, is_kernel, i);
  581. cbe_write_ctr(cpu, i, reset_value[i]);
  582. }
  583. }
  584. /* The counters were frozen by the interrupt.
  585. * Reenable the interrupt and restart the counters.
  586. * If there was a race between the interrupt handler and
  587. * the virtual counter routine. The virutal counter
  588. * routine may have cleared the interrupts. Hence must
  589. * use the virt_cntr_inter_mask to re-enable the interrupts.
  590. */
  591. cbe_enable_pm_interrupts(cpu, hdw_thread,
  592. virt_cntr_inter_mask);
  593. /* The writes to the various performance counters only writes
  594. * to a latch. The new values (interrupt setting bits, reset
  595. * counter value etc.) are not copied to the actual registers
  596. * until the performance monitor is enabled. In order to get
  597. * this to work as desired, the permormance monitor needs to
  598. * be disabled while writting to the latches. This is a
  599. * HW design issue.
  600. */
  601. cbe_enable_pm(cpu);
  602. }
  603. spin_unlock_irqrestore(&virt_cntr_lock, flags);
  604. }
  605. struct op_powerpc_model op_model_cell = {
  606. .reg_setup = cell_reg_setup,
  607. .cpu_setup = cell_cpu_setup,
  608. .global_start = cell_global_start,
  609. .global_stop = cell_global_stop,
  610. .handle_interrupt = cell_handle_interrupt,
  611. };