op_model_cell.c 20 KB

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