op_model_cell.c 20 KB

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