op_model_cell.c 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713
  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/jiffies.h>
  19. #include <linux/kthread.h>
  20. #include <linux/oprofile.h>
  21. #include <linux/percpu.h>
  22. #include <linux/smp.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/timer.h>
  25. #include <asm/cell-pmu.h>
  26. #include <asm/cputable.h>
  27. #include <asm/firmware.h>
  28. #include <asm/io.h>
  29. #include <asm/oprofile_impl.h>
  30. #include <asm/processor.h>
  31. #include <asm/prom.h>
  32. #include <asm/ptrace.h>
  33. #include <asm/reg.h>
  34. #include <asm/rtas.h>
  35. #include <asm/cell-regs.h>
  36. #include "../platforms/cell/interrupt.h"
  37. #include "cell/pr_util.h"
  38. #define PPU_PROFILING 0
  39. #define SPU_PROFILING_CYCLES 1
  40. #define SPU_PROFILING_EVENTS 2
  41. #define SPU_EVENT_NUM_START 4100
  42. #define SPU_EVENT_NUM_STOP 4399
  43. #define SPU_PROFILE_EVENT_ADDR 4363 /* spu, address trace, decimal */
  44. #define SPU_PROFILE_EVENT_ADDR_MASK_A 0x146 /* sub unit set to zero */
  45. #define SPU_PROFILE_EVENT_ADDR_MASK_B 0x186 /* sub unit set to zero */
  46. #define NUM_SPUS_PER_NODE 8
  47. #define SPU_CYCLES_EVENT_NUM 2 /* event number for SPU_CYCLES */
  48. #define PPU_CYCLES_EVENT_NUM 1 /* event number for CYCLES */
  49. #define PPU_CYCLES_GRP_NUM 1 /* special group number for identifying
  50. * PPU_CYCLES event
  51. */
  52. #define CBE_COUNT_ALL_CYCLES 0x42800000 /* PPU cycle event specifier */
  53. #define NUM_THREADS 2 /* number of physical threads in
  54. * physical processor
  55. */
  56. #define NUM_DEBUG_BUS_WORDS 4
  57. #define NUM_INPUT_BUS_WORDS 2
  58. #define MAX_SPU_COUNT 0xFFFFFF /* maximum 24 bit LFSR value */
  59. /* Minimum HW interval timer setting to send value to trace buffer is 10 cycle.
  60. * To configure counter to send value every N cycles set counter to
  61. * 2^32 - 1 - N.
  62. */
  63. #define NUM_INTERVAL_CYC 0xFFFFFFFF - 10
  64. /*
  65. * spu_cycle_reset is the number of cycles between samples.
  66. * This variable is used for SPU profiling and should ONLY be set
  67. * at the beginning of cell_reg_setup; otherwise, it's read-only.
  68. */
  69. static unsigned int spu_cycle_reset;
  70. static unsigned int profiling_mode;
  71. static int spu_evnt_phys_spu_indx;
  72. struct pmc_cntrl_data {
  73. unsigned long vcntr;
  74. unsigned long evnts;
  75. unsigned long masks;
  76. unsigned long enabled;
  77. };
  78. /*
  79. * ibm,cbe-perftools rtas parameters
  80. */
  81. struct pm_signal {
  82. u16 cpu; /* Processor to modify */
  83. u16 sub_unit; /* hw subunit this applies to (if applicable)*/
  84. short int signal_group; /* Signal Group to Enable/Disable */
  85. u8 bus_word; /* Enable/Disable on this Trace/Trigger/Event
  86. * Bus Word(s) (bitmask)
  87. */
  88. u8 bit; /* Trigger/Event bit (if applicable) */
  89. };
  90. /*
  91. * rtas call arguments
  92. */
  93. enum {
  94. SUBFUNC_RESET = 1,
  95. SUBFUNC_ACTIVATE = 2,
  96. SUBFUNC_DEACTIVATE = 3,
  97. PASSTHRU_IGNORE = 0,
  98. PASSTHRU_ENABLE = 1,
  99. PASSTHRU_DISABLE = 2,
  100. };
  101. struct pm_cntrl {
  102. u16 enable;
  103. u16 stop_at_max;
  104. u16 trace_mode;
  105. u16 freeze;
  106. u16 count_mode;
  107. u16 spu_addr_trace;
  108. u8 trace_buf_ovflw;
  109. };
  110. static struct {
  111. u32 group_control;
  112. u32 debug_bus_control;
  113. struct pm_cntrl pm_cntrl;
  114. u32 pm07_cntrl[NR_PHYS_CTRS];
  115. } pm_regs;
  116. #define GET_SUB_UNIT(x) ((x & 0x0000f000) >> 12)
  117. #define GET_BUS_WORD(x) ((x & 0x000000f0) >> 4)
  118. #define GET_BUS_TYPE(x) ((x & 0x00000300) >> 8)
  119. #define GET_POLARITY(x) ((x & 0x00000002) >> 1)
  120. #define GET_COUNT_CYCLES(x) (x & 0x00000001)
  121. #define GET_INPUT_CONTROL(x) ((x & 0x00000004) >> 2)
  122. static DEFINE_PER_CPU(unsigned long[NR_PHYS_CTRS], pmc_values);
  123. static unsigned long spu_pm_cnt[MAX_NUMNODES * NUM_SPUS_PER_NODE];
  124. static struct pmc_cntrl_data pmc_cntrl[NUM_THREADS][NR_PHYS_CTRS];
  125. /*
  126. * The CELL profiling code makes rtas calls to setup the debug bus to
  127. * route the performance signals. Additionally, SPU profiling requires
  128. * a second rtas call to setup the hardware to capture the SPU PCs.
  129. * The EIO error value is returned if the token lookups or the rtas
  130. * call fail. The EIO error number is the best choice of the existing
  131. * error numbers. The probability of rtas related error is very low. But
  132. * by returning EIO and printing additional information to dmsg the user
  133. * will know that OProfile did not start and dmesg will tell them why.
  134. * OProfile does not support returning errors on Stop. Not a huge issue
  135. * since failure to reset the debug bus or stop the SPU PC collection is
  136. * not a fatel issue. Chances are if the Stop failed, Start doesn't work
  137. * either.
  138. */
  139. /*
  140. * Interpetation of hdw_thread:
  141. * 0 - even virtual cpus 0, 2, 4,...
  142. * 1 - odd virtual cpus 1, 3, 5, ...
  143. *
  144. * FIXME: this is strictly wrong, we need to clean this up in a number
  145. * of places. It works for now. -arnd
  146. */
  147. static u32 hdw_thread;
  148. static u32 virt_cntr_inter_mask;
  149. static struct timer_list timer_virt_cntr;
  150. static struct timer_list timer_spu_event_swap;
  151. /*
  152. * pm_signal needs to be global since it is initialized in
  153. * cell_reg_setup at the time when the necessary information
  154. * is available.
  155. */
  156. static struct pm_signal pm_signal[NR_PHYS_CTRS];
  157. static int pm_rtas_token; /* token for debug bus setup call */
  158. static int spu_rtas_token; /* token for SPU cycle profiling */
  159. static u32 reset_value[NR_PHYS_CTRS];
  160. static int num_counters;
  161. static int oprofile_running;
  162. static DEFINE_SPINLOCK(cntr_lock);
  163. static u32 ctr_enabled;
  164. static unsigned char input_bus[NUM_INPUT_BUS_WORDS];
  165. /*
  166. * Firmware interface functions
  167. */
  168. static int
  169. rtas_ibm_cbe_perftools(int subfunc, int passthru,
  170. void *address, unsigned long length)
  171. {
  172. u64 paddr = __pa(address);
  173. return rtas_call(pm_rtas_token, 5, 1, NULL, subfunc,
  174. passthru, paddr >> 32, paddr & 0xffffffff, length);
  175. }
  176. static void pm_rtas_reset_signals(u32 node)
  177. {
  178. int ret;
  179. struct pm_signal pm_signal_local;
  180. /*
  181. * The debug bus is being set to the passthru disable state.
  182. * However, the FW still expects at least one legal signal routing
  183. * entry or it will return an error on the arguments. If we don't
  184. * supply a valid entry, we must ignore all return values. Ignoring
  185. * all return values means we might miss an error we should be
  186. * concerned about.
  187. */
  188. /* fw expects physical cpu #. */
  189. pm_signal_local.cpu = node;
  190. pm_signal_local.signal_group = 21;
  191. pm_signal_local.bus_word = 1;
  192. pm_signal_local.sub_unit = 0;
  193. pm_signal_local.bit = 0;
  194. ret = rtas_ibm_cbe_perftools(SUBFUNC_RESET, PASSTHRU_DISABLE,
  195. &pm_signal_local,
  196. sizeof(struct pm_signal));
  197. if (unlikely(ret))
  198. /*
  199. * Not a fatal error. For Oprofile stop, the oprofile
  200. * functions do not support returning an error for
  201. * failure to stop OProfile.
  202. */
  203. printk(KERN_WARNING "%s: rtas returned: %d\n",
  204. __func__, ret);
  205. }
  206. static int pm_rtas_activate_signals(u32 node, u32 count)
  207. {
  208. int ret;
  209. int i, j;
  210. struct pm_signal pm_signal_local[NR_PHYS_CTRS];
  211. /*
  212. * There is no debug setup required for the cycles event.
  213. * Note that only events in the same group can be used.
  214. * Otherwise, there will be conflicts in correctly routing
  215. * the signals on the debug bus. It is the responsibility
  216. * of the OProfile user tool to check the events are in
  217. * the same group.
  218. */
  219. i = 0;
  220. for (j = 0; j < count; j++) {
  221. if (pm_signal[j].signal_group != PPU_CYCLES_GRP_NUM) {
  222. /* fw expects physical cpu # */
  223. pm_signal_local[i].cpu = node;
  224. pm_signal_local[i].signal_group
  225. = pm_signal[j].signal_group;
  226. pm_signal_local[i].bus_word = pm_signal[j].bus_word;
  227. pm_signal_local[i].sub_unit = pm_signal[j].sub_unit;
  228. pm_signal_local[i].bit = pm_signal[j].bit;
  229. i++;
  230. }
  231. }
  232. if (i != 0) {
  233. ret = rtas_ibm_cbe_perftools(SUBFUNC_ACTIVATE, PASSTHRU_ENABLE,
  234. pm_signal_local,
  235. i * sizeof(struct pm_signal));
  236. if (unlikely(ret)) {
  237. printk(KERN_WARNING "%s: rtas returned: %d\n",
  238. __func__, ret);
  239. return -EIO;
  240. }
  241. }
  242. return 0;
  243. }
  244. /*
  245. * PM Signal functions
  246. */
  247. static void set_pm_event(u32 ctr, int event, u32 unit_mask)
  248. {
  249. struct pm_signal *p;
  250. u32 signal_bit;
  251. u32 bus_word, bus_type, count_cycles, polarity, input_control;
  252. int j, i;
  253. if (event == PPU_CYCLES_EVENT_NUM) {
  254. /* Special Event: Count all cpu cycles */
  255. pm_regs.pm07_cntrl[ctr] = CBE_COUNT_ALL_CYCLES;
  256. p = &(pm_signal[ctr]);
  257. p->signal_group = PPU_CYCLES_GRP_NUM;
  258. p->bus_word = 1;
  259. p->sub_unit = 0;
  260. p->bit = 0;
  261. goto out;
  262. } else {
  263. pm_regs.pm07_cntrl[ctr] = 0;
  264. }
  265. bus_word = GET_BUS_WORD(unit_mask);
  266. bus_type = GET_BUS_TYPE(unit_mask);
  267. count_cycles = GET_COUNT_CYCLES(unit_mask);
  268. polarity = GET_POLARITY(unit_mask);
  269. input_control = GET_INPUT_CONTROL(unit_mask);
  270. signal_bit = (event % 100);
  271. p = &(pm_signal[ctr]);
  272. p->signal_group = event / 100;
  273. p->bus_word = bus_word;
  274. p->sub_unit = GET_SUB_UNIT(unit_mask);
  275. pm_regs.pm07_cntrl[ctr] = 0;
  276. pm_regs.pm07_cntrl[ctr] |= PM07_CTR_COUNT_CYCLES(count_cycles);
  277. pm_regs.pm07_cntrl[ctr] |= PM07_CTR_POLARITY(polarity);
  278. pm_regs.pm07_cntrl[ctr] |= PM07_CTR_INPUT_CONTROL(input_control);
  279. /*
  280. * Some of the islands signal selection is based on 64 bit words.
  281. * The debug bus words are 32 bits, the input words to the performance
  282. * counters are defined as 32 bits. Need to convert the 64 bit island
  283. * specification to the appropriate 32 input bit and bus word for the
  284. * performance counter event selection. See the CELL Performance
  285. * monitoring signals manual and the Perf cntr hardware descriptions
  286. * for the details.
  287. */
  288. if (input_control == 0) {
  289. if (signal_bit > 31) {
  290. signal_bit -= 32;
  291. if (bus_word == 0x3)
  292. bus_word = 0x2;
  293. else if (bus_word == 0xc)
  294. bus_word = 0x8;
  295. }
  296. if ((bus_type == 0) && p->signal_group >= 60)
  297. bus_type = 2;
  298. if ((bus_type == 1) && p->signal_group >= 50)
  299. bus_type = 0;
  300. pm_regs.pm07_cntrl[ctr] |= PM07_CTR_INPUT_MUX(signal_bit);
  301. } else {
  302. pm_regs.pm07_cntrl[ctr] = 0;
  303. p->bit = signal_bit;
  304. }
  305. for (i = 0; i < NUM_DEBUG_BUS_WORDS; i++) {
  306. if (bus_word & (1 << i)) {
  307. pm_regs.debug_bus_control |=
  308. (bus_type << (30 - (2 * i)));
  309. for (j = 0; j < NUM_INPUT_BUS_WORDS; j++) {
  310. if (input_bus[j] == 0xff) {
  311. input_bus[j] = i;
  312. pm_regs.group_control |=
  313. (i << (30 - (2 * j)));
  314. break;
  315. }
  316. }
  317. }
  318. }
  319. out:
  320. ;
  321. }
  322. static void write_pm_cntrl(int cpu)
  323. {
  324. /*
  325. * Oprofile will use 32 bit counters, set bits 7:10 to 0
  326. * pmregs.pm_cntrl is a global
  327. */
  328. u32 val = 0;
  329. if (pm_regs.pm_cntrl.enable == 1)
  330. val |= CBE_PM_ENABLE_PERF_MON;
  331. if (pm_regs.pm_cntrl.stop_at_max == 1)
  332. val |= CBE_PM_STOP_AT_MAX;
  333. if (pm_regs.pm_cntrl.trace_mode != 0)
  334. val |= CBE_PM_TRACE_MODE_SET(pm_regs.pm_cntrl.trace_mode);
  335. if (pm_regs.pm_cntrl.trace_buf_ovflw == 1)
  336. val |= CBE_PM_TRACE_BUF_OVFLW(pm_regs.pm_cntrl.trace_buf_ovflw);
  337. if (pm_regs.pm_cntrl.freeze == 1)
  338. val |= CBE_PM_FREEZE_ALL_CTRS;
  339. val |= CBE_PM_SPU_ADDR_TRACE_SET(pm_regs.pm_cntrl.spu_addr_trace);
  340. /*
  341. * Routine set_count_mode must be called previously to set
  342. * the count mode based on the user selection of user and kernel.
  343. */
  344. val |= CBE_PM_COUNT_MODE_SET(pm_regs.pm_cntrl.count_mode);
  345. cbe_write_pm(cpu, pm_control, val);
  346. }
  347. static inline void
  348. set_count_mode(u32 kernel, u32 user)
  349. {
  350. /*
  351. * The user must specify user and kernel if they want them. If
  352. * neither is specified, OProfile will count in hypervisor mode.
  353. * pm_regs.pm_cntrl is a global
  354. */
  355. if (kernel) {
  356. if (user)
  357. pm_regs.pm_cntrl.count_mode = CBE_COUNT_ALL_MODES;
  358. else
  359. pm_regs.pm_cntrl.count_mode =
  360. CBE_COUNT_SUPERVISOR_MODE;
  361. } else {
  362. if (user)
  363. pm_regs.pm_cntrl.count_mode = CBE_COUNT_PROBLEM_MODE;
  364. else
  365. pm_regs.pm_cntrl.count_mode =
  366. CBE_COUNT_HYPERVISOR_MODE;
  367. }
  368. }
  369. static inline void enable_ctr(u32 cpu, u32 ctr, u32 *pm07_cntrl)
  370. {
  371. pm07_cntrl[ctr] |= CBE_PM_CTR_ENABLE;
  372. cbe_write_pm07_control(cpu, ctr, pm07_cntrl[ctr]);
  373. }
  374. /*
  375. * Oprofile is expected to collect data on all CPUs simultaneously.
  376. * However, there is one set of performance counters per node. There are
  377. * two hardware threads or virtual CPUs on each node. Hence, OProfile must
  378. * multiplex in time the performance counter collection on the two virtual
  379. * CPUs. The multiplexing of the performance counters is done by this
  380. * virtual counter routine.
  381. *
  382. * The pmc_values used below is defined as 'per-cpu' but its use is
  383. * more akin to 'per-node'. We need to store two sets of counter
  384. * values per node -- one for the previous run and one for the next.
  385. * The per-cpu[NR_PHYS_CTRS] gives us the storage we need. Each odd/even
  386. * pair of per-cpu arrays is used for storing the previous and next
  387. * pmc values for a given node.
  388. * NOTE: We use the per-cpu variable to improve cache performance.
  389. *
  390. * This routine will alternate loading the virtual counters for
  391. * virtual CPUs
  392. */
  393. static void cell_virtual_cntr(struct timer_list *unused)
  394. {
  395. int i, prev_hdw_thread, next_hdw_thread;
  396. u32 cpu;
  397. unsigned long flags;
  398. /*
  399. * Make sure that the interrupt_hander and the virt counter are
  400. * not both playing with the counters on the same node.
  401. */
  402. spin_lock_irqsave(&cntr_lock, flags);
  403. prev_hdw_thread = hdw_thread;
  404. /* switch the cpu handling the interrupts */
  405. hdw_thread = 1 ^ hdw_thread;
  406. next_hdw_thread = hdw_thread;
  407. pm_regs.group_control = 0;
  408. pm_regs.debug_bus_control = 0;
  409. for (i = 0; i < NUM_INPUT_BUS_WORDS; i++)
  410. input_bus[i] = 0xff;
  411. /*
  412. * There are some per thread events. Must do the
  413. * set event, for the thread that is being started
  414. */
  415. for (i = 0; i < num_counters; i++)
  416. set_pm_event(i,
  417. pmc_cntrl[next_hdw_thread][i].evnts,
  418. pmc_cntrl[next_hdw_thread][i].masks);
  419. /*
  420. * The following is done only once per each node, but
  421. * we need cpu #, not node #, to pass to the cbe_xxx functions.
  422. */
  423. for_each_online_cpu(cpu) {
  424. if (cbe_get_hw_thread_id(cpu))
  425. continue;
  426. /*
  427. * stop counters, save counter values, restore counts
  428. * for previous thread
  429. */
  430. cbe_disable_pm(cpu);
  431. cbe_disable_pm_interrupts(cpu);
  432. for (i = 0; i < num_counters; i++) {
  433. per_cpu(pmc_values, cpu + prev_hdw_thread)[i]
  434. = cbe_read_ctr(cpu, i);
  435. if (per_cpu(pmc_values, cpu + next_hdw_thread)[i]
  436. == 0xFFFFFFFF)
  437. /* If the cntr value is 0xffffffff, we must
  438. * reset that to 0xfffffff0 when the current
  439. * thread is restarted. This will generate a
  440. * new interrupt and make sure that we never
  441. * restore the counters to the max value. If
  442. * the counters were restored to the max value,
  443. * they do not increment and no interrupts are
  444. * generated. Hence no more samples will be
  445. * collected on that cpu.
  446. */
  447. cbe_write_ctr(cpu, i, 0xFFFFFFF0);
  448. else
  449. cbe_write_ctr(cpu, i,
  450. per_cpu(pmc_values,
  451. cpu +
  452. next_hdw_thread)[i]);
  453. }
  454. /*
  455. * Switch to the other thread. Change the interrupt
  456. * and control regs to be scheduled on the CPU
  457. * corresponding to the thread to execute.
  458. */
  459. for (i = 0; i < num_counters; i++) {
  460. if (pmc_cntrl[next_hdw_thread][i].enabled) {
  461. /*
  462. * There are some per thread events.
  463. * Must do the set event, enable_cntr
  464. * for each cpu.
  465. */
  466. enable_ctr(cpu, i,
  467. pm_regs.pm07_cntrl);
  468. } else {
  469. cbe_write_pm07_control(cpu, i, 0);
  470. }
  471. }
  472. /* Enable interrupts on the CPU thread that is starting */
  473. cbe_enable_pm_interrupts(cpu, next_hdw_thread,
  474. virt_cntr_inter_mask);
  475. cbe_enable_pm(cpu);
  476. }
  477. spin_unlock_irqrestore(&cntr_lock, flags);
  478. mod_timer(&timer_virt_cntr, jiffies + HZ / 10);
  479. }
  480. static void start_virt_cntrs(void)
  481. {
  482. timer_setup(&timer_virt_cntr, cell_virtual_cntr, 0);
  483. timer_virt_cntr.expires = jiffies + HZ / 10;
  484. add_timer(&timer_virt_cntr);
  485. }
  486. static int cell_reg_setup_spu_cycles(struct op_counter_config *ctr,
  487. struct op_system_config *sys, int num_ctrs)
  488. {
  489. spu_cycle_reset = ctr[0].count;
  490. /*
  491. * Each node will need to make the rtas call to start
  492. * and stop SPU profiling. Get the token once and store it.
  493. */
  494. spu_rtas_token = rtas_token("ibm,cbe-spu-perftools");
  495. if (unlikely(spu_rtas_token == RTAS_UNKNOWN_SERVICE)) {
  496. printk(KERN_ERR
  497. "%s: rtas token ibm,cbe-spu-perftools unknown\n",
  498. __func__);
  499. return -EIO;
  500. }
  501. return 0;
  502. }
  503. /* Unfortunately, the hardware will only support event profiling
  504. * on one SPU per node at a time. Therefore, we must time slice
  505. * the profiling across all SPUs in the node. Note, we do this
  506. * in parallel for each node. The following routine is called
  507. * periodically based on kernel timer to switch which SPU is
  508. * being monitored in a round robbin fashion.
  509. */
  510. static void spu_evnt_swap(struct timer_list *unused)
  511. {
  512. int node;
  513. int cur_phys_spu, nxt_phys_spu, cur_spu_evnt_phys_spu_indx;
  514. unsigned long flags;
  515. int cpu;
  516. int ret;
  517. u32 interrupt_mask;
  518. /* enable interrupts on cntr 0 */
  519. interrupt_mask = CBE_PM_CTR_OVERFLOW_INTR(0);
  520. hdw_thread = 0;
  521. /* Make sure spu event interrupt handler and spu event swap
  522. * don't access the counters simultaneously.
  523. */
  524. spin_lock_irqsave(&cntr_lock, flags);
  525. cur_spu_evnt_phys_spu_indx = spu_evnt_phys_spu_indx;
  526. if (++(spu_evnt_phys_spu_indx) == NUM_SPUS_PER_NODE)
  527. spu_evnt_phys_spu_indx = 0;
  528. pm_signal[0].sub_unit = spu_evnt_phys_spu_indx;
  529. pm_signal[1].sub_unit = spu_evnt_phys_spu_indx;
  530. pm_signal[2].sub_unit = spu_evnt_phys_spu_indx;
  531. /* switch the SPU being profiled on each node */
  532. for_each_online_cpu(cpu) {
  533. if (cbe_get_hw_thread_id(cpu))
  534. continue;
  535. node = cbe_cpu_to_node(cpu);
  536. cur_phys_spu = (node * NUM_SPUS_PER_NODE)
  537. + cur_spu_evnt_phys_spu_indx;
  538. nxt_phys_spu = (node * NUM_SPUS_PER_NODE)
  539. + spu_evnt_phys_spu_indx;
  540. /*
  541. * stop counters, save counter values, restore counts
  542. * for previous physical SPU
  543. */
  544. cbe_disable_pm(cpu);
  545. cbe_disable_pm_interrupts(cpu);
  546. spu_pm_cnt[cur_phys_spu]
  547. = cbe_read_ctr(cpu, 0);
  548. /* restore previous count for the next spu to sample */
  549. /* NOTE, hardware issue, counter will not start if the
  550. * counter value is at max (0xFFFFFFFF).
  551. */
  552. if (spu_pm_cnt[nxt_phys_spu] >= 0xFFFFFFFF)
  553. cbe_write_ctr(cpu, 0, 0xFFFFFFF0);
  554. else
  555. cbe_write_ctr(cpu, 0, spu_pm_cnt[nxt_phys_spu]);
  556. pm_rtas_reset_signals(cbe_cpu_to_node(cpu));
  557. /* setup the debug bus measure the one event and
  558. * the two events to route the next SPU's PC on
  559. * the debug bus
  560. */
  561. ret = pm_rtas_activate_signals(cbe_cpu_to_node(cpu), 3);
  562. if (ret)
  563. printk(KERN_ERR "%s: pm_rtas_activate_signals failed, "
  564. "SPU event swap\n", __func__);
  565. /* clear the trace buffer, don't want to take PC for
  566. * previous SPU*/
  567. cbe_write_pm(cpu, trace_address, 0);
  568. enable_ctr(cpu, 0, pm_regs.pm07_cntrl);
  569. /* Enable interrupts on the CPU thread that is starting */
  570. cbe_enable_pm_interrupts(cpu, hdw_thread,
  571. interrupt_mask);
  572. cbe_enable_pm(cpu);
  573. }
  574. spin_unlock_irqrestore(&cntr_lock, flags);
  575. /* swap approximately every 0.1 seconds */
  576. mod_timer(&timer_spu_event_swap, jiffies + HZ / 25);
  577. }
  578. static void start_spu_event_swap(void)
  579. {
  580. timer_setup(&timer_spu_event_swap, spu_evnt_swap, 0);
  581. timer_spu_event_swap.expires = jiffies + HZ / 25;
  582. add_timer(&timer_spu_event_swap);
  583. }
  584. static int cell_reg_setup_spu_events(struct op_counter_config *ctr,
  585. struct op_system_config *sys, int num_ctrs)
  586. {
  587. int i;
  588. /* routine is called once for all nodes */
  589. spu_evnt_phys_spu_indx = 0;
  590. /*
  591. * For all events except PPU CYCLEs, each node will need to make
  592. * the rtas cbe-perftools call to setup and reset the debug bus.
  593. * Make the token lookup call once and store it in the global
  594. * variable pm_rtas_token.
  595. */
  596. pm_rtas_token = rtas_token("ibm,cbe-perftools");
  597. if (unlikely(pm_rtas_token == RTAS_UNKNOWN_SERVICE)) {
  598. printk(KERN_ERR
  599. "%s: rtas token ibm,cbe-perftools unknown\n",
  600. __func__);
  601. return -EIO;
  602. }
  603. /* setup the pm_control register settings,
  604. * settings will be written per node by the
  605. * cell_cpu_setup() function.
  606. */
  607. pm_regs.pm_cntrl.trace_buf_ovflw = 1;
  608. /* Use the occurrence trace mode to have SPU PC saved
  609. * to the trace buffer. Occurrence data in trace buffer
  610. * is not used. Bit 2 must be set to store SPU addresses.
  611. */
  612. pm_regs.pm_cntrl.trace_mode = 2;
  613. pm_regs.pm_cntrl.spu_addr_trace = 0x1; /* using debug bus
  614. event 2 & 3 */
  615. /* setup the debug bus event array with the SPU PC routing events.
  616. * Note, pm_signal[0] will be filled in by set_pm_event() call below.
  617. */
  618. pm_signal[1].signal_group = SPU_PROFILE_EVENT_ADDR / 100;
  619. pm_signal[1].bus_word = GET_BUS_WORD(SPU_PROFILE_EVENT_ADDR_MASK_A);
  620. pm_signal[1].bit = SPU_PROFILE_EVENT_ADDR % 100;
  621. pm_signal[1].sub_unit = spu_evnt_phys_spu_indx;
  622. pm_signal[2].signal_group = SPU_PROFILE_EVENT_ADDR / 100;
  623. pm_signal[2].bus_word = GET_BUS_WORD(SPU_PROFILE_EVENT_ADDR_MASK_B);
  624. pm_signal[2].bit = SPU_PROFILE_EVENT_ADDR % 100;
  625. pm_signal[2].sub_unit = spu_evnt_phys_spu_indx;
  626. /* Set the user selected spu event to profile on,
  627. * note, only one SPU profiling event is supported
  628. */
  629. num_counters = 1; /* Only support one SPU event at a time */
  630. set_pm_event(0, ctr[0].event, ctr[0].unit_mask);
  631. reset_value[0] = 0xFFFFFFFF - ctr[0].count;
  632. /* global, used by cell_cpu_setup */
  633. ctr_enabled |= 1;
  634. /* Initialize the count for each SPU to the reset value */
  635. for (i=0; i < MAX_NUMNODES * NUM_SPUS_PER_NODE; i++)
  636. spu_pm_cnt[i] = reset_value[0];
  637. return 0;
  638. }
  639. static int cell_reg_setup_ppu(struct op_counter_config *ctr,
  640. struct op_system_config *sys, int num_ctrs)
  641. {
  642. /* routine is called once for all nodes */
  643. int i, j, cpu;
  644. num_counters = num_ctrs;
  645. if (unlikely(num_ctrs > NR_PHYS_CTRS)) {
  646. printk(KERN_ERR
  647. "%s: Oprofile, number of specified events " \
  648. "exceeds number of physical counters\n",
  649. __func__);
  650. return -EIO;
  651. }
  652. set_count_mode(sys->enable_kernel, sys->enable_user);
  653. /* Setup the thread 0 events */
  654. for (i = 0; i < num_ctrs; ++i) {
  655. pmc_cntrl[0][i].evnts = ctr[i].event;
  656. pmc_cntrl[0][i].masks = ctr[i].unit_mask;
  657. pmc_cntrl[0][i].enabled = ctr[i].enabled;
  658. pmc_cntrl[0][i].vcntr = i;
  659. for_each_possible_cpu(j)
  660. per_cpu(pmc_values, j)[i] = 0;
  661. }
  662. /*
  663. * Setup the thread 1 events, map the thread 0 event to the
  664. * equivalent thread 1 event.
  665. */
  666. for (i = 0; i < num_ctrs; ++i) {
  667. if ((ctr[i].event >= 2100) && (ctr[i].event <= 2111))
  668. pmc_cntrl[1][i].evnts = ctr[i].event + 19;
  669. else if (ctr[i].event == 2203)
  670. pmc_cntrl[1][i].evnts = ctr[i].event;
  671. else if ((ctr[i].event >= 2200) && (ctr[i].event <= 2215))
  672. pmc_cntrl[1][i].evnts = ctr[i].event + 16;
  673. else
  674. pmc_cntrl[1][i].evnts = ctr[i].event;
  675. pmc_cntrl[1][i].masks = ctr[i].unit_mask;
  676. pmc_cntrl[1][i].enabled = ctr[i].enabled;
  677. pmc_cntrl[1][i].vcntr = i;
  678. }
  679. for (i = 0; i < NUM_INPUT_BUS_WORDS; i++)
  680. input_bus[i] = 0xff;
  681. /*
  682. * Our counters count up, and "count" refers to
  683. * how much before the next interrupt, and we interrupt
  684. * on overflow. So we calculate the starting value
  685. * which will give us "count" until overflow.
  686. * Then we set the events on the enabled counters.
  687. */
  688. for (i = 0; i < num_counters; ++i) {
  689. /* start with virtual counter set 0 */
  690. if (pmc_cntrl[0][i].enabled) {
  691. /* Using 32bit counters, reset max - count */
  692. reset_value[i] = 0xFFFFFFFF - ctr[i].count;
  693. set_pm_event(i,
  694. pmc_cntrl[0][i].evnts,
  695. pmc_cntrl[0][i].masks);
  696. /* global, used by cell_cpu_setup */
  697. ctr_enabled |= (1 << i);
  698. }
  699. }
  700. /* initialize the previous counts for the virtual cntrs */
  701. for_each_online_cpu(cpu)
  702. for (i = 0; i < num_counters; ++i) {
  703. per_cpu(pmc_values, cpu)[i] = reset_value[i];
  704. }
  705. return 0;
  706. }
  707. /* This function is called once for all cpus combined */
  708. static int cell_reg_setup(struct op_counter_config *ctr,
  709. struct op_system_config *sys, int num_ctrs)
  710. {
  711. int ret=0;
  712. spu_cycle_reset = 0;
  713. /* initialize the spu_arr_trace value, will be reset if
  714. * doing spu event profiling.
  715. */
  716. pm_regs.group_control = 0;
  717. pm_regs.debug_bus_control = 0;
  718. pm_regs.pm_cntrl.stop_at_max = 1;
  719. pm_regs.pm_cntrl.trace_mode = 0;
  720. pm_regs.pm_cntrl.freeze = 1;
  721. pm_regs.pm_cntrl.trace_buf_ovflw = 0;
  722. pm_regs.pm_cntrl.spu_addr_trace = 0;
  723. /*
  724. * For all events except PPU CYCLEs, each node will need to make
  725. * the rtas cbe-perftools call to setup and reset the debug bus.
  726. * Make the token lookup call once and store it in the global
  727. * variable pm_rtas_token.
  728. */
  729. pm_rtas_token = rtas_token("ibm,cbe-perftools");
  730. if (unlikely(pm_rtas_token == RTAS_UNKNOWN_SERVICE)) {
  731. printk(KERN_ERR
  732. "%s: rtas token ibm,cbe-perftools unknown\n",
  733. __func__);
  734. return -EIO;
  735. }
  736. if (ctr[0].event == SPU_CYCLES_EVENT_NUM) {
  737. profiling_mode = SPU_PROFILING_CYCLES;
  738. ret = cell_reg_setup_spu_cycles(ctr, sys, num_ctrs);
  739. } else if ((ctr[0].event >= SPU_EVENT_NUM_START) &&
  740. (ctr[0].event <= SPU_EVENT_NUM_STOP)) {
  741. profiling_mode = SPU_PROFILING_EVENTS;
  742. spu_cycle_reset = ctr[0].count;
  743. /* for SPU event profiling, need to setup the
  744. * pm_signal array with the events to route the
  745. * SPU PC before making the FW call. Note, only
  746. * one SPU event for profiling can be specified
  747. * at a time.
  748. */
  749. cell_reg_setup_spu_events(ctr, sys, num_ctrs);
  750. } else {
  751. profiling_mode = PPU_PROFILING;
  752. ret = cell_reg_setup_ppu(ctr, sys, num_ctrs);
  753. }
  754. return ret;
  755. }
  756. /* This function is called once for each cpu */
  757. static int cell_cpu_setup(struct op_counter_config *cntr)
  758. {
  759. u32 cpu = smp_processor_id();
  760. u32 num_enabled = 0;
  761. int i;
  762. int ret;
  763. /* Cycle based SPU profiling does not use the performance
  764. * counters. The trace array is configured to collect
  765. * the data.
  766. */
  767. if (profiling_mode == SPU_PROFILING_CYCLES)
  768. return 0;
  769. /* There is one performance monitor per processor chip (i.e. node),
  770. * so we only need to perform this function once per node.
  771. */
  772. if (cbe_get_hw_thread_id(cpu))
  773. return 0;
  774. /* Stop all counters */
  775. cbe_disable_pm(cpu);
  776. cbe_disable_pm_interrupts(cpu);
  777. cbe_write_pm(cpu, pm_start_stop, 0);
  778. cbe_write_pm(cpu, group_control, pm_regs.group_control);
  779. cbe_write_pm(cpu, debug_bus_control, pm_regs.debug_bus_control);
  780. write_pm_cntrl(cpu);
  781. for (i = 0; i < num_counters; ++i) {
  782. if (ctr_enabled & (1 << i)) {
  783. pm_signal[num_enabled].cpu = cbe_cpu_to_node(cpu);
  784. num_enabled++;
  785. }
  786. }
  787. /*
  788. * The pm_rtas_activate_signals will return -EIO if the FW
  789. * call failed.
  790. */
  791. if (profiling_mode == SPU_PROFILING_EVENTS) {
  792. /* For SPU event profiling also need to setup the
  793. * pm interval timer
  794. */
  795. ret = pm_rtas_activate_signals(cbe_cpu_to_node(cpu),
  796. num_enabled+2);
  797. /* store PC from debug bus to Trace buffer as often
  798. * as possible (every 10 cycles)
  799. */
  800. cbe_write_pm(cpu, pm_interval, NUM_INTERVAL_CYC);
  801. return ret;
  802. } else
  803. return pm_rtas_activate_signals(cbe_cpu_to_node(cpu),
  804. num_enabled);
  805. }
  806. #define ENTRIES 303
  807. #define MAXLFSR 0xFFFFFF
  808. /* precomputed table of 24 bit LFSR values */
  809. static int initial_lfsr[] = {
  810. 8221349, 12579195, 5379618, 10097839, 7512963, 7519310, 3955098, 10753424,
  811. 15507573, 7458917, 285419, 2641121, 9780088, 3915503, 6668768, 1548716,
  812. 4885000, 8774424, 9650099, 2044357, 2304411, 9326253, 10332526, 4421547,
  813. 3440748, 10179459, 13332843, 10375561, 1313462, 8375100, 5198480, 6071392,
  814. 9341783, 1526887, 3985002, 1439429, 13923762, 7010104, 11969769, 4547026,
  815. 2040072, 4025602, 3437678, 7939992, 11444177, 4496094, 9803157, 10745556,
  816. 3671780, 4257846, 5662259, 13196905, 3237343, 12077182, 16222879, 7587769,
  817. 14706824, 2184640, 12591135, 10420257, 7406075, 3648978, 11042541, 15906893,
  818. 11914928, 4732944, 10695697, 12928164, 11980531, 4430912, 11939291, 2917017,
  819. 6119256, 4172004, 9373765, 8410071, 14788383, 5047459, 5474428, 1737756,
  820. 15967514, 13351758, 6691285, 8034329, 2856544, 14394753, 11310160, 12149558,
  821. 7487528, 7542781, 15668898, 12525138, 12790975, 3707933, 9106617, 1965401,
  822. 16219109, 12801644, 2443203, 4909502, 8762329, 3120803, 6360315, 9309720,
  823. 15164599, 10844842, 4456529, 6667610, 14924259, 884312, 6234963, 3326042,
  824. 15973422, 13919464, 5272099, 6414643, 3909029, 2764324, 5237926, 4774955,
  825. 10445906, 4955302, 5203726, 10798229, 11443419, 2303395, 333836, 9646934,
  826. 3464726, 4159182, 568492, 995747, 10318756, 13299332, 4836017, 8237783,
  827. 3878992, 2581665, 11394667, 5672745, 14412947, 3159169, 9094251, 16467278,
  828. 8671392, 15230076, 4843545, 7009238, 15504095, 1494895, 9627886, 14485051,
  829. 8304291, 252817, 12421642, 16085736, 4774072, 2456177, 4160695, 15409741,
  830. 4902868, 5793091, 13162925, 16039714, 782255, 11347835, 14884586, 366972,
  831. 16308990, 11913488, 13390465, 2958444, 10340278, 1177858, 1319431, 10426302,
  832. 2868597, 126119, 5784857, 5245324, 10903900, 16436004, 3389013, 1742384,
  833. 14674502, 10279218, 8536112, 10364279, 6877778, 14051163, 1025130, 6072469,
  834. 1988305, 8354440, 8216060, 16342977, 13112639, 3976679, 5913576, 8816697,
  835. 6879995, 14043764, 3339515, 9364420, 15808858, 12261651, 2141560, 5636398,
  836. 10345425, 10414756, 781725, 6155650, 4746914, 5078683, 7469001, 6799140,
  837. 10156444, 9667150, 10116470, 4133858, 2121972, 1124204, 1003577, 1611214,
  838. 14304602, 16221850, 13878465, 13577744, 3629235, 8772583, 10881308, 2410386,
  839. 7300044, 5378855, 9301235, 12755149, 4977682, 8083074, 10327581, 6395087,
  840. 9155434, 15501696, 7514362, 14520507, 15808945, 3244584, 4741962, 9658130,
  841. 14336147, 8654727, 7969093, 15759799, 14029445, 5038459, 9894848, 8659300,
  842. 13699287, 8834306, 10712885, 14753895, 10410465, 3373251, 309501, 9561475,
  843. 5526688, 14647426, 14209836, 5339224, 207299, 14069911, 8722990, 2290950,
  844. 3258216, 12505185, 6007317, 9218111, 14661019, 10537428, 11731949, 9027003,
  845. 6641507, 9490160, 200241, 9720425, 16277895, 10816638, 1554761, 10431375,
  846. 7467528, 6790302, 3429078, 14633753, 14428997, 11463204, 3576212, 2003426,
  847. 6123687, 820520, 9992513, 15784513, 5778891, 6428165, 8388607
  848. };
  849. /*
  850. * The hardware uses an LFSR counting sequence to determine when to capture
  851. * the SPU PCs. An LFSR sequence is like a puesdo random number sequence
  852. * where each number occurs once in the sequence but the sequence is not in
  853. * numerical order. The SPU PC capture is done when the LFSR sequence reaches
  854. * the last value in the sequence. Hence the user specified value N
  855. * corresponds to the LFSR number that is N from the end of the sequence.
  856. *
  857. * To avoid the time to compute the LFSR, a lookup table is used. The 24 bit
  858. * LFSR sequence is broken into four ranges. The spacing of the precomputed
  859. * values is adjusted in each range so the error between the user specified
  860. * number (N) of events between samples and the actual number of events based
  861. * on the precomputed value will be les then about 6.2%. Note, if the user
  862. * specifies N < 2^16, the LFSR value that is 2^16 from the end will be used.
  863. * This is to prevent the loss of samples because the trace buffer is full.
  864. *
  865. * User specified N Step between Index in
  866. * precomputed values precomputed
  867. * table
  868. * 0 to 2^16-1 ---- 0
  869. * 2^16 to 2^16+2^19-1 2^12 1 to 128
  870. * 2^16+2^19 to 2^16+2^19+2^22-1 2^15 129 to 256
  871. * 2^16+2^19+2^22 to 2^24-1 2^18 257 to 302
  872. *
  873. *
  874. * For example, the LFSR values in the second range are computed for 2^16,
  875. * 2^16+2^12, ... , 2^19-2^16, 2^19 and stored in the table at indicies
  876. * 1, 2,..., 127, 128.
  877. *
  878. * The 24 bit LFSR value for the nth number in the sequence can be
  879. * calculated using the following code:
  880. *
  881. * #define size 24
  882. * int calculate_lfsr(int n)
  883. * {
  884. * int i;
  885. * unsigned int newlfsr0;
  886. * unsigned int lfsr = 0xFFFFFF;
  887. * unsigned int howmany = n;
  888. *
  889. * for (i = 2; i < howmany + 2; i++) {
  890. * newlfsr0 = (((lfsr >> (size - 1 - 0)) & 1) ^
  891. * ((lfsr >> (size - 1 - 1)) & 1) ^
  892. * (((lfsr >> (size - 1 - 6)) & 1) ^
  893. * ((lfsr >> (size - 1 - 23)) & 1)));
  894. *
  895. * lfsr >>= 1;
  896. * lfsr = lfsr | (newlfsr0 << (size - 1));
  897. * }
  898. * return lfsr;
  899. * }
  900. */
  901. #define V2_16 (0x1 << 16)
  902. #define V2_19 (0x1 << 19)
  903. #define V2_22 (0x1 << 22)
  904. static int calculate_lfsr(int n)
  905. {
  906. /*
  907. * The ranges and steps are in powers of 2 so the calculations
  908. * can be done using shifts rather then divide.
  909. */
  910. int index;
  911. if ((n >> 16) == 0)
  912. index = 0;
  913. else if (((n - V2_16) >> 19) == 0)
  914. index = ((n - V2_16) >> 12) + 1;
  915. else if (((n - V2_16 - V2_19) >> 22) == 0)
  916. index = ((n - V2_16 - V2_19) >> 15 ) + 1 + 128;
  917. else if (((n - V2_16 - V2_19 - V2_22) >> 24) == 0)
  918. index = ((n - V2_16 - V2_19 - V2_22) >> 18 ) + 1 + 256;
  919. else
  920. index = ENTRIES-1;
  921. /* make sure index is valid */
  922. if ((index >= ENTRIES) || (index < 0))
  923. index = ENTRIES-1;
  924. return initial_lfsr[index];
  925. }
  926. static int pm_rtas_activate_spu_profiling(u32 node)
  927. {
  928. int ret, i;
  929. struct pm_signal pm_signal_local[NUM_SPUS_PER_NODE];
  930. /*
  931. * Set up the rtas call to configure the debug bus to
  932. * route the SPU PCs. Setup the pm_signal for each SPU
  933. */
  934. for (i = 0; i < ARRAY_SIZE(pm_signal_local); i++) {
  935. pm_signal_local[i].cpu = node;
  936. pm_signal_local[i].signal_group = 41;
  937. /* spu i on word (i/2) */
  938. pm_signal_local[i].bus_word = 1 << i / 2;
  939. /* spu i */
  940. pm_signal_local[i].sub_unit = i;
  941. pm_signal_local[i].bit = 63;
  942. }
  943. ret = rtas_ibm_cbe_perftools(SUBFUNC_ACTIVATE,
  944. PASSTHRU_ENABLE, pm_signal_local,
  945. (ARRAY_SIZE(pm_signal_local)
  946. * sizeof(struct pm_signal)));
  947. if (unlikely(ret)) {
  948. printk(KERN_WARNING "%s: rtas returned: %d\n",
  949. __func__, ret);
  950. return -EIO;
  951. }
  952. return 0;
  953. }
  954. #ifdef CONFIG_CPU_FREQ
  955. static int
  956. oprof_cpufreq_notify(struct notifier_block *nb, unsigned long val, void *data)
  957. {
  958. int ret = 0;
  959. struct cpufreq_freqs *frq = data;
  960. if ((val == CPUFREQ_PRECHANGE && frq->old < frq->new) ||
  961. (val == CPUFREQ_POSTCHANGE && frq->old > frq->new))
  962. set_spu_profiling_frequency(frq->new, spu_cycle_reset);
  963. return ret;
  964. }
  965. static struct notifier_block cpu_freq_notifier_block = {
  966. .notifier_call = oprof_cpufreq_notify
  967. };
  968. #endif
  969. /*
  970. * Note the generic OProfile stop calls do not support returning
  971. * an error on stop. Hence, will not return an error if the FW
  972. * calls fail on stop. Failure to reset the debug bus is not an issue.
  973. * Failure to disable the SPU profiling is not an issue. The FW calls
  974. * to enable the performance counters and debug bus will work even if
  975. * the hardware was not cleanly reset.
  976. */
  977. static void cell_global_stop_spu_cycles(void)
  978. {
  979. int subfunc, rtn_value;
  980. unsigned int lfsr_value;
  981. int cpu;
  982. oprofile_running = 0;
  983. smp_wmb();
  984. #ifdef CONFIG_CPU_FREQ
  985. cpufreq_unregister_notifier(&cpu_freq_notifier_block,
  986. CPUFREQ_TRANSITION_NOTIFIER);
  987. #endif
  988. for_each_online_cpu(cpu) {
  989. if (cbe_get_hw_thread_id(cpu))
  990. continue;
  991. subfunc = 3; /*
  992. * 2 - activate SPU tracing,
  993. * 3 - deactivate
  994. */
  995. lfsr_value = 0x8f100000;
  996. rtn_value = rtas_call(spu_rtas_token, 3, 1, NULL,
  997. subfunc, cbe_cpu_to_node(cpu),
  998. lfsr_value);
  999. if (unlikely(rtn_value != 0)) {
  1000. printk(KERN_ERR
  1001. "%s: rtas call ibm,cbe-spu-perftools " \
  1002. "failed, return = %d\n",
  1003. __func__, rtn_value);
  1004. }
  1005. /* Deactivate the signals */
  1006. pm_rtas_reset_signals(cbe_cpu_to_node(cpu));
  1007. }
  1008. stop_spu_profiling_cycles();
  1009. }
  1010. static void cell_global_stop_spu_events(void)
  1011. {
  1012. int cpu;
  1013. oprofile_running = 0;
  1014. stop_spu_profiling_events();
  1015. smp_wmb();
  1016. for_each_online_cpu(cpu) {
  1017. if (cbe_get_hw_thread_id(cpu))
  1018. continue;
  1019. cbe_sync_irq(cbe_cpu_to_node(cpu));
  1020. /* Stop the counters */
  1021. cbe_disable_pm(cpu);
  1022. cbe_write_pm07_control(cpu, 0, 0);
  1023. /* Deactivate the signals */
  1024. pm_rtas_reset_signals(cbe_cpu_to_node(cpu));
  1025. /* Deactivate interrupts */
  1026. cbe_disable_pm_interrupts(cpu);
  1027. }
  1028. del_timer_sync(&timer_spu_event_swap);
  1029. }
  1030. static void cell_global_stop_ppu(void)
  1031. {
  1032. int cpu;
  1033. /*
  1034. * This routine will be called once for the system.
  1035. * There is one performance monitor per node, so we
  1036. * only need to perform this function once per node.
  1037. */
  1038. del_timer_sync(&timer_virt_cntr);
  1039. oprofile_running = 0;
  1040. smp_wmb();
  1041. for_each_online_cpu(cpu) {
  1042. if (cbe_get_hw_thread_id(cpu))
  1043. continue;
  1044. cbe_sync_irq(cbe_cpu_to_node(cpu));
  1045. /* Stop the counters */
  1046. cbe_disable_pm(cpu);
  1047. /* Deactivate the signals */
  1048. pm_rtas_reset_signals(cbe_cpu_to_node(cpu));
  1049. /* Deactivate interrupts */
  1050. cbe_disable_pm_interrupts(cpu);
  1051. }
  1052. }
  1053. static void cell_global_stop(void)
  1054. {
  1055. if (profiling_mode == PPU_PROFILING)
  1056. cell_global_stop_ppu();
  1057. else if (profiling_mode == SPU_PROFILING_EVENTS)
  1058. cell_global_stop_spu_events();
  1059. else
  1060. cell_global_stop_spu_cycles();
  1061. }
  1062. static int cell_global_start_spu_cycles(struct op_counter_config *ctr)
  1063. {
  1064. int subfunc;
  1065. unsigned int lfsr_value;
  1066. int cpu;
  1067. int ret;
  1068. int rtas_error;
  1069. unsigned int cpu_khzfreq = 0;
  1070. /* The SPU profiling uses time-based profiling based on
  1071. * cpu frequency, so if configured with the CPU_FREQ
  1072. * option, we should detect frequency changes and react
  1073. * accordingly.
  1074. */
  1075. #ifdef CONFIG_CPU_FREQ
  1076. ret = cpufreq_register_notifier(&cpu_freq_notifier_block,
  1077. CPUFREQ_TRANSITION_NOTIFIER);
  1078. if (ret < 0)
  1079. /* this is not a fatal error */
  1080. printk(KERN_ERR "CPU freq change registration failed: %d\n",
  1081. ret);
  1082. else
  1083. cpu_khzfreq = cpufreq_quick_get(smp_processor_id());
  1084. #endif
  1085. set_spu_profiling_frequency(cpu_khzfreq, spu_cycle_reset);
  1086. for_each_online_cpu(cpu) {
  1087. if (cbe_get_hw_thread_id(cpu))
  1088. continue;
  1089. /*
  1090. * Setup SPU cycle-based profiling.
  1091. * Set perf_mon_control bit 0 to a zero before
  1092. * enabling spu collection hardware.
  1093. */
  1094. cbe_write_pm(cpu, pm_control, 0);
  1095. if (spu_cycle_reset > MAX_SPU_COUNT)
  1096. /* use largest possible value */
  1097. lfsr_value = calculate_lfsr(MAX_SPU_COUNT-1);
  1098. else
  1099. lfsr_value = calculate_lfsr(spu_cycle_reset);
  1100. /* must use a non zero value. Zero disables data collection. */
  1101. if (lfsr_value == 0)
  1102. lfsr_value = calculate_lfsr(1);
  1103. lfsr_value = lfsr_value << 8; /* shift lfsr to correct
  1104. * register location
  1105. */
  1106. /* debug bus setup */
  1107. ret = pm_rtas_activate_spu_profiling(cbe_cpu_to_node(cpu));
  1108. if (unlikely(ret)) {
  1109. rtas_error = ret;
  1110. goto out;
  1111. }
  1112. subfunc = 2; /* 2 - activate SPU tracing, 3 - deactivate */
  1113. /* start profiling */
  1114. ret = rtas_call(spu_rtas_token, 3, 1, NULL, subfunc,
  1115. cbe_cpu_to_node(cpu), lfsr_value);
  1116. if (unlikely(ret != 0)) {
  1117. printk(KERN_ERR
  1118. "%s: rtas call ibm,cbe-spu-perftools failed, " \
  1119. "return = %d\n", __func__, ret);
  1120. rtas_error = -EIO;
  1121. goto out;
  1122. }
  1123. }
  1124. rtas_error = start_spu_profiling_cycles(spu_cycle_reset);
  1125. if (rtas_error)
  1126. goto out_stop;
  1127. oprofile_running = 1;
  1128. return 0;
  1129. out_stop:
  1130. cell_global_stop_spu_cycles(); /* clean up the PMU/debug bus */
  1131. out:
  1132. return rtas_error;
  1133. }
  1134. static int cell_global_start_spu_events(struct op_counter_config *ctr)
  1135. {
  1136. int cpu;
  1137. u32 interrupt_mask = 0;
  1138. int rtn = 0;
  1139. hdw_thread = 0;
  1140. /* spu event profiling, uses the performance counters to generate
  1141. * an interrupt. The hardware is setup to store the SPU program
  1142. * counter into the trace array. The occurrence mode is used to
  1143. * enable storing data to the trace buffer. The bits are set
  1144. * to send/store the SPU address in the trace buffer. The debug
  1145. * bus must be setup to route the SPU program counter onto the
  1146. * debug bus. The occurrence data in the trace buffer is not used.
  1147. */
  1148. /* This routine gets called once for the system.
  1149. * There is one performance monitor per node, so we
  1150. * only need to perform this function once per node.
  1151. */
  1152. for_each_online_cpu(cpu) {
  1153. if (cbe_get_hw_thread_id(cpu))
  1154. continue;
  1155. /*
  1156. * Setup SPU event-based profiling.
  1157. * Set perf_mon_control bit 0 to a zero before
  1158. * enabling spu collection hardware.
  1159. *
  1160. * Only support one SPU event on one SPU per node.
  1161. */
  1162. if (ctr_enabled & 1) {
  1163. cbe_write_ctr(cpu, 0, reset_value[0]);
  1164. enable_ctr(cpu, 0, pm_regs.pm07_cntrl);
  1165. interrupt_mask |=
  1166. CBE_PM_CTR_OVERFLOW_INTR(0);
  1167. } else {
  1168. /* Disable counter */
  1169. cbe_write_pm07_control(cpu, 0, 0);
  1170. }
  1171. cbe_get_and_clear_pm_interrupts(cpu);
  1172. cbe_enable_pm_interrupts(cpu, hdw_thread, interrupt_mask);
  1173. cbe_enable_pm(cpu);
  1174. /* clear the trace buffer */
  1175. cbe_write_pm(cpu, trace_address, 0);
  1176. }
  1177. /* Start the timer to time slice collecting the event profile
  1178. * on each of the SPUs. Note, can collect profile on one SPU
  1179. * per node at a time.
  1180. */
  1181. start_spu_event_swap();
  1182. start_spu_profiling_events();
  1183. oprofile_running = 1;
  1184. smp_wmb();
  1185. return rtn;
  1186. }
  1187. static int cell_global_start_ppu(struct op_counter_config *ctr)
  1188. {
  1189. u32 cpu, i;
  1190. u32 interrupt_mask = 0;
  1191. /* This routine gets called once for the system.
  1192. * There is one performance monitor per node, so we
  1193. * only need to perform this function once per node.
  1194. */
  1195. for_each_online_cpu(cpu) {
  1196. if (cbe_get_hw_thread_id(cpu))
  1197. continue;
  1198. interrupt_mask = 0;
  1199. for (i = 0; i < num_counters; ++i) {
  1200. if (ctr_enabled & (1 << i)) {
  1201. cbe_write_ctr(cpu, i, reset_value[i]);
  1202. enable_ctr(cpu, i, pm_regs.pm07_cntrl);
  1203. interrupt_mask |= CBE_PM_CTR_OVERFLOW_INTR(i);
  1204. } else {
  1205. /* Disable counter */
  1206. cbe_write_pm07_control(cpu, i, 0);
  1207. }
  1208. }
  1209. cbe_get_and_clear_pm_interrupts(cpu);
  1210. cbe_enable_pm_interrupts(cpu, hdw_thread, interrupt_mask);
  1211. cbe_enable_pm(cpu);
  1212. }
  1213. virt_cntr_inter_mask = interrupt_mask;
  1214. oprofile_running = 1;
  1215. smp_wmb();
  1216. /*
  1217. * NOTE: start_virt_cntrs will result in cell_virtual_cntr() being
  1218. * executed which manipulates the PMU. We start the "virtual counter"
  1219. * here so that we do not need to synchronize access to the PMU in
  1220. * the above for-loop.
  1221. */
  1222. start_virt_cntrs();
  1223. return 0;
  1224. }
  1225. static int cell_global_start(struct op_counter_config *ctr)
  1226. {
  1227. if (profiling_mode == SPU_PROFILING_CYCLES)
  1228. return cell_global_start_spu_cycles(ctr);
  1229. else if (profiling_mode == SPU_PROFILING_EVENTS)
  1230. return cell_global_start_spu_events(ctr);
  1231. else
  1232. return cell_global_start_ppu(ctr);
  1233. }
  1234. /* The SPU interrupt handler
  1235. *
  1236. * SPU event profiling works as follows:
  1237. * The pm_signal[0] holds the one SPU event to be measured. It is routed on
  1238. * the debug bus using word 0 or 1. The value of pm_signal[1] and
  1239. * pm_signal[2] contain the necessary events to route the SPU program
  1240. * counter for the selected SPU onto the debug bus using words 2 and 3.
  1241. * The pm_interval register is setup to write the SPU PC value into the
  1242. * trace buffer at the maximum rate possible. The trace buffer is configured
  1243. * to store the PCs, wrapping when it is full. The performance counter is
  1244. * initialized to the max hardware count minus the number of events, N, between
  1245. * samples. Once the N events have occurred, a HW counter overflow occurs
  1246. * causing the generation of a HW counter interrupt which also stops the
  1247. * writing of the SPU PC values to the trace buffer. Hence the last PC
  1248. * written to the trace buffer is the SPU PC that we want. Unfortunately,
  1249. * we have to read from the beginning of the trace buffer to get to the
  1250. * last value written. We just hope the PPU has nothing better to do then
  1251. * service this interrupt. The PC for the specific SPU being profiled is
  1252. * extracted from the trace buffer processed and stored. The trace buffer
  1253. * is cleared, interrupts are cleared, the counter is reset to max - N.
  1254. * A kernel timer is used to periodically call the routine spu_evnt_swap()
  1255. * to switch to the next physical SPU in the node to profile in round robbin
  1256. * order. This way data is collected for all SPUs on the node. It does mean
  1257. * that we need to use a relatively small value of N to ensure enough samples
  1258. * on each SPU are collected each SPU is being profiled 1/8 of the time.
  1259. * It may also be necessary to use a longer sample collection period.
  1260. */
  1261. static void cell_handle_interrupt_spu(struct pt_regs *regs,
  1262. struct op_counter_config *ctr)
  1263. {
  1264. u32 cpu, cpu_tmp;
  1265. u64 trace_entry;
  1266. u32 interrupt_mask;
  1267. u64 trace_buffer[2];
  1268. u64 last_trace_buffer;
  1269. u32 sample;
  1270. u32 trace_addr;
  1271. unsigned long sample_array_lock_flags;
  1272. int spu_num;
  1273. unsigned long flags;
  1274. /* Make sure spu event interrupt handler and spu event swap
  1275. * don't access the counters simultaneously.
  1276. */
  1277. cpu = smp_processor_id();
  1278. spin_lock_irqsave(&cntr_lock, flags);
  1279. cpu_tmp = cpu;
  1280. cbe_disable_pm(cpu);
  1281. interrupt_mask = cbe_get_and_clear_pm_interrupts(cpu);
  1282. sample = 0xABCDEF;
  1283. trace_entry = 0xfedcba;
  1284. last_trace_buffer = 0xdeadbeaf;
  1285. if ((oprofile_running == 1) && (interrupt_mask != 0)) {
  1286. /* disable writes to trace buff */
  1287. cbe_write_pm(cpu, pm_interval, 0);
  1288. /* only have one perf cntr being used, cntr 0 */
  1289. if ((interrupt_mask & CBE_PM_CTR_OVERFLOW_INTR(0))
  1290. && ctr[0].enabled)
  1291. /* The SPU PC values will be read
  1292. * from the trace buffer, reset counter
  1293. */
  1294. cbe_write_ctr(cpu, 0, reset_value[0]);
  1295. trace_addr = cbe_read_pm(cpu, trace_address);
  1296. while (!(trace_addr & CBE_PM_TRACE_BUF_EMPTY)) {
  1297. /* There is data in the trace buffer to process
  1298. * Read the buffer until you get to the last
  1299. * entry. This is the value we want.
  1300. */
  1301. cbe_read_trace_buffer(cpu, trace_buffer);
  1302. trace_addr = cbe_read_pm(cpu, trace_address);
  1303. }
  1304. /* SPU Address 16 bit count format for 128 bit
  1305. * HW trace buffer is used for the SPU PC storage
  1306. * HDR bits 0:15
  1307. * SPU Addr 0 bits 16:31
  1308. * SPU Addr 1 bits 32:47
  1309. * unused bits 48:127
  1310. *
  1311. * HDR: bit4 = 1 SPU Address 0 valid
  1312. * HDR: bit5 = 1 SPU Address 1 valid
  1313. * - unfortunately, the valid bits don't seem to work
  1314. *
  1315. * Note trace_buffer[0] holds bits 0:63 of the HW
  1316. * trace buffer, trace_buffer[1] holds bits 64:127
  1317. */
  1318. trace_entry = trace_buffer[0]
  1319. & 0x00000000FFFF0000;
  1320. /* only top 16 of the 18 bit SPU PC address
  1321. * is stored in trace buffer, hence shift right
  1322. * by 16 -2 bits */
  1323. sample = trace_entry >> 14;
  1324. last_trace_buffer = trace_buffer[0];
  1325. spu_num = spu_evnt_phys_spu_indx
  1326. + (cbe_cpu_to_node(cpu) * NUM_SPUS_PER_NODE);
  1327. /* make sure only one process at a time is calling
  1328. * spu_sync_buffer()
  1329. */
  1330. spin_lock_irqsave(&oprof_spu_smpl_arry_lck,
  1331. sample_array_lock_flags);
  1332. spu_sync_buffer(spu_num, &sample, 1);
  1333. spin_unlock_irqrestore(&oprof_spu_smpl_arry_lck,
  1334. sample_array_lock_flags);
  1335. smp_wmb(); /* insure spu event buffer updates are written
  1336. * don't want events intermingled... */
  1337. /* The counters were frozen by the interrupt.
  1338. * Reenable the interrupt and restart the counters.
  1339. */
  1340. cbe_write_pm(cpu, pm_interval, NUM_INTERVAL_CYC);
  1341. cbe_enable_pm_interrupts(cpu, hdw_thread,
  1342. virt_cntr_inter_mask);
  1343. /* clear the trace buffer, re-enable writes to trace buff */
  1344. cbe_write_pm(cpu, trace_address, 0);
  1345. cbe_write_pm(cpu, pm_interval, NUM_INTERVAL_CYC);
  1346. /* The writes to the various performance counters only writes
  1347. * to a latch. The new values (interrupt setting bits, reset
  1348. * counter value etc.) are not copied to the actual registers
  1349. * until the performance monitor is enabled. In order to get
  1350. * this to work as desired, the performance monitor needs to
  1351. * be disabled while writing to the latches. This is a
  1352. * HW design issue.
  1353. */
  1354. write_pm_cntrl(cpu);
  1355. cbe_enable_pm(cpu);
  1356. }
  1357. spin_unlock_irqrestore(&cntr_lock, flags);
  1358. }
  1359. static void cell_handle_interrupt_ppu(struct pt_regs *regs,
  1360. struct op_counter_config *ctr)
  1361. {
  1362. u32 cpu;
  1363. u64 pc;
  1364. int is_kernel;
  1365. unsigned long flags = 0;
  1366. u32 interrupt_mask;
  1367. int i;
  1368. cpu = smp_processor_id();
  1369. /*
  1370. * Need to make sure the interrupt handler and the virt counter
  1371. * routine are not running at the same time. See the
  1372. * cell_virtual_cntr() routine for additional comments.
  1373. */
  1374. spin_lock_irqsave(&cntr_lock, flags);
  1375. /*
  1376. * Need to disable and reenable the performance counters
  1377. * to get the desired behavior from the hardware. This
  1378. * is hardware specific.
  1379. */
  1380. cbe_disable_pm(cpu);
  1381. interrupt_mask = cbe_get_and_clear_pm_interrupts(cpu);
  1382. /*
  1383. * If the interrupt mask has been cleared, then the virt cntr
  1384. * has cleared the interrupt. When the thread that generated
  1385. * the interrupt is restored, the data count will be restored to
  1386. * 0xffffff0 to cause the interrupt to be regenerated.
  1387. */
  1388. if ((oprofile_running == 1) && (interrupt_mask != 0)) {
  1389. pc = regs->nip;
  1390. is_kernel = is_kernel_addr(pc);
  1391. for (i = 0; i < num_counters; ++i) {
  1392. if ((interrupt_mask & CBE_PM_CTR_OVERFLOW_INTR(i))
  1393. && ctr[i].enabled) {
  1394. oprofile_add_ext_sample(pc, regs, i, is_kernel);
  1395. cbe_write_ctr(cpu, i, reset_value[i]);
  1396. }
  1397. }
  1398. /*
  1399. * The counters were frozen by the interrupt.
  1400. * Reenable the interrupt and restart the counters.
  1401. * If there was a race between the interrupt handler and
  1402. * the virtual counter routine. The virtual counter
  1403. * routine may have cleared the interrupts. Hence must
  1404. * use the virt_cntr_inter_mask to re-enable the interrupts.
  1405. */
  1406. cbe_enable_pm_interrupts(cpu, hdw_thread,
  1407. virt_cntr_inter_mask);
  1408. /*
  1409. * The writes to the various performance counters only writes
  1410. * to a latch. The new values (interrupt setting bits, reset
  1411. * counter value etc.) are not copied to the actual registers
  1412. * until the performance monitor is enabled. In order to get
  1413. * this to work as desired, the performance monitor needs to
  1414. * be disabled while writing to the latches. This is a
  1415. * HW design issue.
  1416. */
  1417. cbe_enable_pm(cpu);
  1418. }
  1419. spin_unlock_irqrestore(&cntr_lock, flags);
  1420. }
  1421. static void cell_handle_interrupt(struct pt_regs *regs,
  1422. struct op_counter_config *ctr)
  1423. {
  1424. if (profiling_mode == PPU_PROFILING)
  1425. cell_handle_interrupt_ppu(regs, ctr);
  1426. else
  1427. cell_handle_interrupt_spu(regs, ctr);
  1428. }
  1429. /*
  1430. * This function is called from the generic OProfile
  1431. * driver. When profiling PPUs, we need to do the
  1432. * generic sync start; otherwise, do spu_sync_start.
  1433. */
  1434. static int cell_sync_start(void)
  1435. {
  1436. if ((profiling_mode == SPU_PROFILING_CYCLES) ||
  1437. (profiling_mode == SPU_PROFILING_EVENTS))
  1438. return spu_sync_start();
  1439. else
  1440. return DO_GENERIC_SYNC;
  1441. }
  1442. static int cell_sync_stop(void)
  1443. {
  1444. if ((profiling_mode == SPU_PROFILING_CYCLES) ||
  1445. (profiling_mode == SPU_PROFILING_EVENTS))
  1446. return spu_sync_stop();
  1447. else
  1448. return 1;
  1449. }
  1450. struct op_powerpc_model op_model_cell = {
  1451. .reg_setup = cell_reg_setup,
  1452. .cpu_setup = cell_cpu_setup,
  1453. .global_start = cell_global_start,
  1454. .global_stop = cell_global_stop,
  1455. .sync_start = cell_sync_start,
  1456. .sync_stop = cell_sync_stop,
  1457. .handle_interrupt = cell_handle_interrupt,
  1458. };