perf-smp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  1. /* Copyright (c) 2010, The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. /*
  13. perf-smp.c
  14. DESCRIPTION
  15. Manipulation, initialization of the ARMV7 Performance counter register.
  16. EXTERNALIZED FUNCTIONS
  17. INITIALIZATION AND SEQUENCING REQUIREMENTS
  18. */
  19. /*
  20. INCLUDE FILES FOR MODULE
  21. */
  22. #include <linux/kernel.h>
  23. #include <linux/string.h>
  24. #include <linux/time.h>
  25. #include <linux/device.h>
  26. #include <linux/interrupt.h>
  27. #include <asm/io.h>
  28. #include <asm/irq.h>
  29. #include "l2_cp15_registers.h"
  30. /*
  31. DEFINITIONS AND DECLARATIONS FOR MODULE
  32. This section contains definitions for constants, macros, types, variables
  33. and other items needed by this module.
  34. */
  35. /*
  36. Constant / Define Declarations
  37. */
  38. #define PM_NUM_COUNTERS 4
  39. #define L2_PM_ERR -1
  40. /*------------------------------------------------------------------------
  41. * Global control bits
  42. ------------------------------------------------------------------------*/
  43. #define PM_L2_GLOBAL_ENABLE (1<<0)
  44. #define PM_L2_EVENT_RESET (1<<1)
  45. #define PM_L2_CYCLE_RESET (1<<2)
  46. #define PM_L2_CLKDIV (1<<3)
  47. #define PM_L2_GLOBAL_TRACE (1<<4)
  48. #define PM_L2_DISABLE_PROHIBIT (1<<5)
  49. /*---------------------------------------------------------------------------
  50. * Enable and clear bits for each event/trigger
  51. ----------------------------------------------------------------------------*/
  52. #define PM_L2EV0_ENABLE (1<<0)
  53. #define PM_L2EV1_ENABLE (1<<1)
  54. #define PM_L2EV2_ENABLE (1<<2)
  55. #define PM_L2EV3_ENABLE (1<<3)
  56. #define PM_L2_COUNT_ENABLE (1<<31)
  57. #define PM_L2_ALL_ENABLE (0x8000000F)
  58. /*-----------------------------------------------------------------------------
  59. * Overflow actions
  60. ------------------------------------------------------------------------------*/
  61. #define PM_L2_OVERFLOW_NOACTION (0)
  62. #define PM_L2_OVERFLOW_HALT (1)
  63. #define PM_L2_OVERFLOW_STOP (2)
  64. #define PM_L2_OVERFLOW_SKIP (3)
  65. /*
  66. * Shifts for each trigger type
  67. */
  68. #define PM_STOP_SHIFT 24
  69. #define PM_RELOAD_SHIFT 22
  70. #define PM_RESUME_SHIFT 20
  71. #define PM_SUSPEND_SHIFT 18
  72. #define PM_START_SHIFT 16
  73. #define PM_STOPALL_SHIFT 15
  74. #define PM_STOPCOND_SHIFT 12
  75. #define PM_RELOADCOND_SHIFT 9
  76. #define PM_RESUMECOND_SHIFT 6
  77. #define PM_SUSPENDCOND_SHIFT 3
  78. #define PM_STARTCOND_SHIFT 0
  79. /*---------------------------------------------------------------------------
  80. External control register. What todo when various events happen.
  81. Triggering events, etc.
  82. ----------------------------------------------------------------------------*/
  83. #define PM_EXTTR0 0
  84. #define PM_EXTTR1 1
  85. #define PM_EXTTR2 2
  86. #define PM_EXTTR3 3
  87. #define PM_COND_NO_STOP 0
  88. #define PM_COND_STOP_CNTOVRFLW 1
  89. #define PM_COND_STOP_EXTERNAL 4
  90. #define PM_COND_STOP_TRACE 5
  91. #define PM_COND_STOP_EVOVRFLW 6
  92. #define PM_COND_STOP_EVTYPER 7
  93. /*--------------------------------------------------------------------------
  94. Protect against concurrent access. There is an index register that is
  95. used to select the appropriate bank of registers. If multiple processes
  96. are writting this at different times we could have a mess...
  97. ---------------------------------------------------------------------------*/
  98. #define PM_LOCK()
  99. #define PM_UNLOCK()
  100. #define PRINT printk
  101. /*--------------------------------------------------------------------------
  102. The Event definitions
  103. --------------------------------------------------------------------------*/
  104. #define L2PM_EVT_PM0_EVT0 0x00
  105. #define L2PM_EVT_PM0_EVT1 0x01
  106. #define L2PM_EVT_PM0_EVT2 0x02
  107. #define L2PM_EVT_PM0_EVT3 0x03
  108. #define L2PM_EVT_PM1_EVT0 0x04
  109. #define L2PM_EVT_PM1_EVT1 0x05
  110. #define L2PM_EVT_PM1_EVT2 0x06
  111. #define L2PM_EVT_PM1_EVT3 0x07
  112. #define L2PM_EVT_PM2_EVT0 0x08
  113. #define L2PM_EVT_PM2_EVT1 0x09
  114. #define L2PM_EVT_PM2_EVT2 0x0a
  115. #define L2PM_EVT_PM2_EVT3 0x0b
  116. #define L2PM_EVT_PM3_EVT0 0x0c
  117. #define L2PM_EVT_PM3_EVT1 0x0d
  118. #define L2PM_EVT_PM3_EVT2 0x0e
  119. #define L2PM_EVT_PM3_EVT3 0x0f
  120. #define L2PM_EVT_PM4_EVT0 0x10
  121. #define L2PM_EVT_PM4_EVT1 0x11
  122. #define L2PM_EVT_PM4_EVT2 0x12
  123. #define L2PM_EVT_PM4_EVT3 0x13
  124. /*
  125. Type Declarations
  126. */
  127. /*
  128. Local Object Definitions
  129. */
  130. unsigned long l2_pm_cycle_overflow_count;
  131. unsigned long l2_pm_overflow_count[PM_NUM_COUNTERS];
  132. /*---------------------------------------------------------------------------
  133. Max number of events read from the config registers
  134. ---------------------------------------------------------------------------*/
  135. static int pm_l2_max_events;
  136. static int irqid;
  137. /*
  138. Function Definitions
  139. */
  140. /*
  141. FUNCTION l2_pm_group_stop
  142. DESCRIPTION Stop a group of the performance monitors. Event monitor 0 is bit
  143. 0, event monitor 1 bit 1, etc. The cycle count can also be disable with
  144. bit 31. Macros are provided for all of the indexes including an ALL.
  145. DEPENDENCIES
  146. RETURN VALUE
  147. None
  148. SIDE EFFECTS
  149. Stops the performance monitoring for the index passed.
  150. */
  151. void pm_l2_group_stop(unsigned long mask)
  152. {
  153. WCP15_L2PMCNTENCLR(mask);
  154. }
  155. /*
  156. FUNCTION l2_pm_group_start
  157. DESCRIPTION Start a group of the performance monitors. Event monitor 0 is bit
  158. 0, event monitor 1 bit 1, etc. The cycle count can also be enabled with
  159. bit 31. Macros are provided for all of the indexes including an ALL.
  160. DEPENDENCIES
  161. RETURN VALUE
  162. None
  163. SIDE EFFECTS
  164. Starts the performance monitoring for the index passed.
  165. */
  166. void pm_l2_group_start(unsigned long mask)
  167. {
  168. WCP15_L2PMCNTENSET(mask);
  169. }
  170. /*
  171. FUNCTION l2_pm_get_overflow
  172. DESCRIPTION Return the overflow condition for the index passed.
  173. DEPENDENCIES
  174. RETURN VALUE
  175. 0 no overflow
  176. !0 (anything else) overflow;
  177. SIDE EFFECTS
  178. */
  179. unsigned long l2_pm_get_overflow(int index)
  180. {
  181. unsigned long overflow = 0;
  182. /*
  183. * Range check
  184. */
  185. if (index > pm_l2_max_events)
  186. return L2_PM_ERR;
  187. RCP15_L2PMOVSR(overflow);
  188. return overflow & (1<<index);
  189. }
  190. /*
  191. FUNCTION l2_pm_get_cycle_overflow
  192. DESCRIPTION
  193. Returns if the cycle counter has overflowed or not.
  194. DEPENDENCIES
  195. RETURN VALUE
  196. 0 no overflow
  197. !0 (anything else) overflow;
  198. SIDE EFFECTS
  199. */
  200. unsigned long l2_pm_get_cycle_overflow(void)
  201. {
  202. unsigned long overflow = 0;
  203. RCP15_L2PMOVSR(overflow);
  204. return overflow & PM_L2_COUNT_ENABLE;
  205. }
  206. /*
  207. FUNCTION l2_pm_reset_overflow
  208. DESCRIPTION Reset the cycle counter overflow bit.
  209. DEPENDENCIES
  210. RETURN VALUE
  211. None
  212. SIDE EFFECTS
  213. */
  214. void l2_pm_reset_overflow(int index)
  215. {
  216. WCP15_L2PMOVSR(1<<index);
  217. }
  218. /*
  219. FUNCTION l2_pm_reset_cycle_overflow
  220. DESCRIPTION Reset the cycle counter overflow bit.
  221. DEPENDENCIES
  222. RETURN VALUE
  223. None
  224. SIDE EFFECTS
  225. */
  226. void l2_pm_reset_cycle_overflow(void)
  227. {
  228. WCP15_L2PMOVSR(PM_L2_COUNT_ENABLE);
  229. }
  230. /*
  231. FUNCTION l2_pm_get_cycle_count
  232. DESCRIPTION return the count in the cycle count register.
  233. DEPENDENCIES
  234. RETURN VALUE
  235. The value in the cycle count register.
  236. SIDE EFFECTS
  237. */
  238. unsigned long l2_pm_get_cycle_count(void)
  239. {
  240. unsigned long cnt = 0;
  241. RCP15_L2PMCCNTR(cnt);
  242. return cnt;
  243. }
  244. /*
  245. FUNCTION l2_pm_reset_cycle_count
  246. DESCRIPTION reset the value in the cycle count register
  247. DEPENDENCIES
  248. RETURN VALUE
  249. NONE
  250. SIDE EFFECTS
  251. Resets the performance monitor cycle count register.
  252. Any interrupts period based on this overflow will be changed
  253. */
  254. void l2_pm_reset_cycle_count(void)
  255. {
  256. WCP15_L2PMCNTENCLR(PM_L2_COUNT_ENABLE);
  257. }
  258. /*
  259. FUNCTION l2_pm_cycle_div_64
  260. DESCRIPTION Set the cycle counter to count every 64th cycle instead of
  261. every cycle when the value passed is 1, otherwise counts every cycle.
  262. DEPENDENCIES
  263. RETURN VALUE
  264. none
  265. SIDE EFFECTS
  266. Changes the rate at which cycles are counted. Anything that is reading
  267. the cycle count (pmGetCyucleCount) may get different results.
  268. */
  269. void l2_pm_cycle_div_64(int enable)
  270. {
  271. unsigned long enables = 0;
  272. RCP15_L2PMCR(enables);
  273. if (enable)
  274. WCP15_L2PMCR(enables | PM_L2_CLKDIV);
  275. else
  276. WCP15_L2PMCR(enables & ~PM_L2_CLKDIV);
  277. }
  278. /*
  279. FUNCTION l2_pm_enable_cycle_counter
  280. DESCRIPTION Enable the cycle counter. Sets the bit in the enable register
  281. so the performance monitor counter starts up counting.
  282. DEPENDENCIES
  283. RETURN VALUE
  284. none
  285. SIDE EFFECTS
  286. */
  287. void l2_pm_enable_cycle_counter(void)
  288. {
  289. /*
  290. * Enable the counter.
  291. */
  292. WCP15_L2PMCNTENSET(PM_L2_COUNT_ENABLE);
  293. }
  294. /*
  295. FUNCTION l2_pm_disable_counter
  296. DESCRIPTION Disable a single counter based on the index passed.
  297. DEPENDENCIES
  298. RETURN VALUE
  299. none
  300. SIDE EFFECTS
  301. Any triggers that are based on the stoped counter may not trigger...
  302. */
  303. void l2_pm_disable_counter(int index)
  304. {
  305. /*
  306. * Range check
  307. */
  308. if (index > pm_l2_max_events)
  309. return;
  310. WCP15_L2PMCNTENCLR(1<<index);
  311. }
  312. /*
  313. FUNCTION l2_pm_enable_counter
  314. DESCRIPTION Enable the counter with the index passed.
  315. DEPENDENCIES
  316. RETURN VALUE
  317. none.
  318. SIDE EFFECTS
  319. */
  320. void l2_pm_enable_counter(int index)
  321. {
  322. /*
  323. * Range check
  324. */
  325. if (index > pm_l2_max_events)
  326. return;
  327. WCP15_L2PMCNTENSET(1<<index);
  328. }
  329. /*
  330. FUNCTION l2_pm_set_count
  331. DESCRIPTION Set the number of events in a register, used for resets
  332. passed.
  333. DEPENDENCIES
  334. RETURN VALUE
  335. -1 if the index is out of range
  336. SIDE EFFECTS
  337. */
  338. int l2_pm_set_count(int index, unsigned long new_value)
  339. {
  340. unsigned long reg = 0;
  341. /*
  342. * Range check
  343. */
  344. if (index > pm_l2_max_events)
  345. return L2_PM_ERR;
  346. /*
  347. * Lock, select the index and read the count...unlock
  348. */
  349. PM_LOCK();
  350. WCP15_L2PMSELR(index);
  351. WCP15_L2PMXEVCNTR(new_value);
  352. PM_UNLOCK();
  353. return reg;
  354. }
  355. int l2_pm_reset_count(int index)
  356. {
  357. return l2_pm_set_count(index, 0);
  358. }
  359. /*
  360. FUNCTION l2_pm_get_count
  361. DESCRIPTION Return the number of events that have happened for the index
  362. passed.
  363. DEPENDENCIES
  364. RETURN VALUE
  365. -1 if the index is out of range
  366. The number of events if inrange
  367. SIDE EFFECTS
  368. */
  369. unsigned long l2_pm_get_count(int index)
  370. {
  371. unsigned long reg = 0;
  372. /*
  373. * Range check
  374. */
  375. if (index > pm_l2_max_events)
  376. return L2_PM_ERR;
  377. /*
  378. * Lock, select the index and read the count...unlock
  379. */
  380. PM_LOCK();
  381. WCP15_L2PMSELR(index);
  382. RCP15_L2PMXEVCNTR(reg);
  383. PM_UNLOCK();
  384. return reg;
  385. }
  386. unsigned long get_filter_code(unsigned long event)
  387. {
  388. if (event == 0x0 || event == 0x4 || event == 0x08
  389. || event == 0x0c || event == 0x10)
  390. return 0x0001003f;
  391. else if (event == 0x1 || event == 0x5 || event == 0x09
  392. || event == 0x0d || event == 0x11)
  393. return 0x0002003f;
  394. else if (event == 0x2 || event == 0x6 || event == 0x0a
  395. || event == 0x0e || event == 0x12)
  396. return 0x0004003f;
  397. else if (event == 0x3 || event == 0x7 || event == 0x0b
  398. || event == 0x0f || event == 0x13)
  399. return 0x0008003f;
  400. else
  401. return 0;
  402. }
  403. int l2_pm_set_event(int index, unsigned long event)
  404. {
  405. unsigned long reg = 0;
  406. /*
  407. * Range check
  408. */
  409. if (index > pm_l2_max_events)
  410. return L2_PM_ERR;
  411. /*
  412. * Lock, select the index and read the count...unlock
  413. */
  414. PM_LOCK();
  415. WCP15_L2PMSELR(index);
  416. WCP15_L2PMXEVTYPER(event);
  417. /* WCP15_L2PMXEVFILTER(get_filter_code(event)); */
  418. WCP15_L2PMXEVFILTER(0x000f003f);
  419. PM_UNLOCK();
  420. return reg;
  421. }
  422. /*
  423. FUNCTION pm_set_local_bu
  424. DESCRIPTION Set the local BU triggers. Note that the MSB determines if
  425. these are enabled or not.
  426. DEPENDENCIES
  427. RETURN VALUE
  428. NONE
  429. SIDE EFFECTS
  430. */
  431. void pm_set_local_bu(unsigned long value)
  432. {
  433. WCP15_L2PMEVTYPER0(value);
  434. }
  435. /*
  436. FUNCTION pm_set_local_cb
  437. DESCRIPTION Set the local CB triggers. Note that the MSB determines if
  438. these are enabled or not.
  439. DEPENDENCIES
  440. RETURN VALUE
  441. NONE
  442. SIDE EFFECTS
  443. */
  444. void pm_set_local_cb(unsigned long value)
  445. {
  446. WCP15_L2PMEVTYPER1(value);
  447. }
  448. /*
  449. FUNCTION pm_set_local_mp
  450. DESCRIPTION Set the local MP triggers. Note that the MSB determines if
  451. these are enabled or not.
  452. DEPENDENCIES
  453. RETURN VALUE
  454. NONE
  455. SIDE EFFECTS
  456. */
  457. void pm_set_local_mp(unsigned long value)
  458. {
  459. WCP15_L2PMEVTYPER2(value);
  460. }
  461. /*
  462. FUNCTION pm_set_local_sp
  463. DESCRIPTION Set the local SP triggers. Note that the MSB determines if
  464. these are enabled or not.
  465. DEPENDENCIES
  466. RETURN VALUE
  467. NONE
  468. SIDE EFFECTS
  469. */
  470. void pm_set_local_sp(unsigned long value)
  471. {
  472. WCP15_L2PMEVTYPER3(value);
  473. }
  474. /*
  475. FUNCTION pm_set_local_scu
  476. DESCRIPTION Set the local SCU triggers. Note that the MSB determines if
  477. these are enabled or not.
  478. DEPENDENCIES
  479. RETURN VALUE
  480. NONE
  481. SIDE EFFECTS
  482. */
  483. void pm_set_local_scu(unsigned long value)
  484. {
  485. WCP15_L2PMEVTYPER4(value);
  486. }
  487. /*
  488. FUNCTION l2_pm_isr
  489. DESCRIPTION:
  490. Performance Monitor interrupt service routine to capture overflows
  491. DEPENDENCIES
  492. RETURN VALUE
  493. SIDE EFFECTS
  494. */
  495. static irqreturn_t l2_pm_isr(int irq, void *d)
  496. {
  497. int i;
  498. for (i = 0; i < PM_NUM_COUNTERS; i++) {
  499. if (l2_pm_get_overflow(i)) {
  500. l2_pm_overflow_count[i]++;
  501. l2_pm_reset_overflow(i);
  502. }
  503. }
  504. if (l2_pm_get_cycle_overflow()) {
  505. l2_pm_cycle_overflow_count++;
  506. l2_pm_reset_cycle_overflow();
  507. }
  508. return IRQ_HANDLED;
  509. }
  510. void l2_pm_stop_all(void)
  511. {
  512. WCP15_L2PMCNTENCLR(0xFFFFFFFF);
  513. }
  514. void l2_pm_reset_all(void)
  515. {
  516. WCP15_L2PMCR(0xF);
  517. WCP15_L2PMOVSR(PM_L2_ALL_ENABLE); /* overflow clear */
  518. }
  519. void l2_pm_start_all(void)
  520. {
  521. WCP15_L2PMCNTENSET(PM_L2_ALL_ENABLE);
  522. }
  523. /*
  524. FUNCTION l2_pm_initialize
  525. DESCRIPTION Initialize the performanca monitoring for the v7 processor.
  526. Ensures the cycle count is running and the event counters are enabled.
  527. DEPENDENCIES
  528. RETURN VALUE
  529. NONE
  530. SIDE EFFECTS
  531. */
  532. void l2_pm_initialize(void)
  533. {
  534. unsigned long reg = 0;
  535. unsigned char imp;
  536. unsigned char id;
  537. unsigned char num;
  538. unsigned long enables = 0;
  539. static int initialized;
  540. if (initialized)
  541. return;
  542. initialized = 1;
  543. irqid = SC_SICL2PERFMONIRPTREQ;
  544. RCP15_L2PMCR(reg);
  545. imp = (reg>>24) & 0xFF;
  546. id = (reg>>16) & 0xFF;
  547. pm_l2_max_events = num = (reg>>11) & 0xFF;
  548. PRINT("V7 MP L2SCU Performance Monitor Capabilities\n");
  549. PRINT(" Implementor %c(%d)\n", imp, imp);
  550. PRINT(" Id %d %x\n", id, id);
  551. PRINT(" Num Events %d %x\n", num, num);
  552. PRINT("\nCycle counter enabled by default...\n");
  553. /*
  554. * Global enable, ensure the global enable is set so all
  555. * subsequent actions take effect. Also resets the counts
  556. */
  557. RCP15_L2PMCR(enables);
  558. WCP15_L2PMCR(enables | PM_L2_GLOBAL_ENABLE | PM_L2_EVENT_RESET |
  559. PM_L2_CYCLE_RESET | PM_L2_CLKDIV);
  560. /*
  561. * Enable access from user space
  562. */
  563. /*
  564. * Install interrupt handler and the enable the interrupts
  565. */
  566. l2_pm_reset_cycle_overflow();
  567. l2_pm_reset_overflow(0);
  568. l2_pm_reset_overflow(1);
  569. l2_pm_reset_overflow(2);
  570. l2_pm_reset_overflow(3);
  571. l2_pm_reset_overflow(4);
  572. if (0 != request_irq(irqid, l2_pm_isr, 0, "l2perfmon", 0))
  573. printk(KERN_ERR "%s:%d request_irq returned error\n",
  574. __FILE__, __LINE__);
  575. WCP15_L2PMINTENSET(PM_L2_ALL_ENABLE);
  576. /*
  577. * Enable the cycle counter. Default, count 1:1 no divisor.
  578. */
  579. l2_pm_enable_cycle_counter();
  580. }
  581. void l2_pm_free_irq(void)
  582. {
  583. free_irq(irqid, 0);
  584. }
  585. void l2_pm_deinitialize(void)
  586. {
  587. unsigned long enables = 0;
  588. RCP15_L2PMCR(enables);
  589. WCP15_L2PMCR(enables & ~PM_L2_GLOBAL_ENABLE);
  590. }