perf_event_v6.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * ARMv6 Performance counter handling code.
  3. *
  4. * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
  5. *
  6. * ARMv6 has 2 configurable performance counters and a single cycle counter.
  7. * They all share a single reset bit but can be written to zero so we can use
  8. * that for a reset.
  9. *
  10. * The counters can't be individually enabled or disabled so when we remove
  11. * one event and replace it with another we could get spurious counts from the
  12. * wrong event. However, we can take advantage of the fact that the
  13. * performance counters can export events to the event bus, and the event bus
  14. * itself can be monitored. This requires that we *don't* export the events to
  15. * the event bus. The procedure for disabling a configurable counter is:
  16. * - change the counter to count the ETMEXTOUT[0] signal (0x20). This
  17. * effectively stops the counter from counting.
  18. * - disable the counter's interrupt generation (each counter has it's
  19. * own interrupt enable bit).
  20. * Once stopped, the counter value can be written as 0 to reset.
  21. *
  22. * To enable a counter:
  23. * - enable the counter's interrupt generation.
  24. * - set the new event type.
  25. *
  26. * Note: the dedicated cycle counter only counts cycles and can't be
  27. * enabled/disabled independently of the others. When we want to disable the
  28. * cycle counter, we have to just disable the interrupt reporting and start
  29. * ignoring that counter. When re-enabling, we have to reset the value and
  30. * enable the interrupt.
  31. */
  32. #if defined(CONFIG_CPU_V6) || defined(CONFIG_CPU_V6K)
  33. #include <asm/cputype.h>
  34. #include <asm/irq_regs.h>
  35. #include <linux/of.h>
  36. #include <linux/perf/arm_pmu.h>
  37. #include <linux/platform_device.h>
  38. enum armv6_perf_types {
  39. ARMV6_PERFCTR_ICACHE_MISS = 0x0,
  40. ARMV6_PERFCTR_IBUF_STALL = 0x1,
  41. ARMV6_PERFCTR_DDEP_STALL = 0x2,
  42. ARMV6_PERFCTR_ITLB_MISS = 0x3,
  43. ARMV6_PERFCTR_DTLB_MISS = 0x4,
  44. ARMV6_PERFCTR_BR_EXEC = 0x5,
  45. ARMV6_PERFCTR_BR_MISPREDICT = 0x6,
  46. ARMV6_PERFCTR_INSTR_EXEC = 0x7,
  47. ARMV6_PERFCTR_DCACHE_HIT = 0x9,
  48. ARMV6_PERFCTR_DCACHE_ACCESS = 0xA,
  49. ARMV6_PERFCTR_DCACHE_MISS = 0xB,
  50. ARMV6_PERFCTR_DCACHE_WBACK = 0xC,
  51. ARMV6_PERFCTR_SW_PC_CHANGE = 0xD,
  52. ARMV6_PERFCTR_MAIN_TLB_MISS = 0xF,
  53. ARMV6_PERFCTR_EXPL_D_ACCESS = 0x10,
  54. ARMV6_PERFCTR_LSU_FULL_STALL = 0x11,
  55. ARMV6_PERFCTR_WBUF_DRAINED = 0x12,
  56. ARMV6_PERFCTR_CPU_CYCLES = 0xFF,
  57. ARMV6_PERFCTR_NOP = 0x20,
  58. };
  59. enum armv6_counters {
  60. ARMV6_CYCLE_COUNTER = 0,
  61. ARMV6_COUNTER0,
  62. ARMV6_COUNTER1,
  63. };
  64. /*
  65. * The hardware events that we support. We do support cache operations but
  66. * we have harvard caches and no way to combine instruction and data
  67. * accesses/misses in hardware.
  68. */
  69. static const unsigned armv6_perf_map[PERF_COUNT_HW_MAX] = {
  70. PERF_MAP_ALL_UNSUPPORTED,
  71. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6_PERFCTR_CPU_CYCLES,
  72. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6_PERFCTR_INSTR_EXEC,
  73. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6_PERFCTR_BR_EXEC,
  74. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6_PERFCTR_BR_MISPREDICT,
  75. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV6_PERFCTR_IBUF_STALL,
  76. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV6_PERFCTR_LSU_FULL_STALL,
  77. };
  78. static const unsigned armv6_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  79. [PERF_COUNT_HW_CACHE_OP_MAX]
  80. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  81. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  82. /*
  83. * The performance counters don't differentiate between read and write
  84. * accesses/misses so this isn't strictly correct, but it's the best we
  85. * can do. Writes and reads get combined.
  86. */
  87. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  88. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  89. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  90. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  91. [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
  92. /*
  93. * The ARM performance counters can count micro DTLB misses, micro ITLB
  94. * misses and main TLB misses. There isn't an event for TLB misses, so
  95. * use the micro misses here and if users want the main TLB misses they
  96. * can use a raw counter.
  97. */
  98. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  99. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  100. [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  101. [C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  102. };
  103. enum armv6mpcore_perf_types {
  104. ARMV6MPCORE_PERFCTR_ICACHE_MISS = 0x0,
  105. ARMV6MPCORE_PERFCTR_IBUF_STALL = 0x1,
  106. ARMV6MPCORE_PERFCTR_DDEP_STALL = 0x2,
  107. ARMV6MPCORE_PERFCTR_ITLB_MISS = 0x3,
  108. ARMV6MPCORE_PERFCTR_DTLB_MISS = 0x4,
  109. ARMV6MPCORE_PERFCTR_BR_EXEC = 0x5,
  110. ARMV6MPCORE_PERFCTR_BR_NOTPREDICT = 0x6,
  111. ARMV6MPCORE_PERFCTR_BR_MISPREDICT = 0x7,
  112. ARMV6MPCORE_PERFCTR_INSTR_EXEC = 0x8,
  113. ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS = 0xA,
  114. ARMV6MPCORE_PERFCTR_DCACHE_RDMISS = 0xB,
  115. ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS = 0xC,
  116. ARMV6MPCORE_PERFCTR_DCACHE_WRMISS = 0xD,
  117. ARMV6MPCORE_PERFCTR_DCACHE_EVICTION = 0xE,
  118. ARMV6MPCORE_PERFCTR_SW_PC_CHANGE = 0xF,
  119. ARMV6MPCORE_PERFCTR_MAIN_TLB_MISS = 0x10,
  120. ARMV6MPCORE_PERFCTR_EXPL_MEM_ACCESS = 0x11,
  121. ARMV6MPCORE_PERFCTR_LSU_FULL_STALL = 0x12,
  122. ARMV6MPCORE_PERFCTR_WBUF_DRAINED = 0x13,
  123. ARMV6MPCORE_PERFCTR_CPU_CYCLES = 0xFF,
  124. };
  125. /*
  126. * The hardware events that we support. We do support cache operations but
  127. * we have harvard caches and no way to combine instruction and data
  128. * accesses/misses in hardware.
  129. */
  130. static const unsigned armv6mpcore_perf_map[PERF_COUNT_HW_MAX] = {
  131. PERF_MAP_ALL_UNSUPPORTED,
  132. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6MPCORE_PERFCTR_CPU_CYCLES,
  133. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_INSTR_EXEC,
  134. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6MPCORE_PERFCTR_BR_EXEC,
  135. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6MPCORE_PERFCTR_BR_MISPREDICT,
  136. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV6MPCORE_PERFCTR_IBUF_STALL,
  137. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV6MPCORE_PERFCTR_LSU_FULL_STALL,
  138. };
  139. static const unsigned armv6mpcore_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  140. [PERF_COUNT_HW_CACHE_OP_MAX]
  141. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  142. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  143. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV6MPCORE_PERFCTR_DCACHE_RDACCESS,
  144. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DCACHE_RDMISS,
  145. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV6MPCORE_PERFCTR_DCACHE_WRACCESS,
  146. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DCACHE_WRMISS,
  147. [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ICACHE_MISS,
  148. /*
  149. * The ARM performance counters can count micro DTLB misses, micro ITLB
  150. * misses and main TLB misses. There isn't an event for TLB misses, so
  151. * use the micro misses here and if users want the main TLB misses they
  152. * can use a raw counter.
  153. */
  154. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
  155. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_DTLB_MISS,
  156. [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
  157. [C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6MPCORE_PERFCTR_ITLB_MISS,
  158. };
  159. static inline unsigned long
  160. armv6_pmcr_read(void)
  161. {
  162. u32 val;
  163. asm volatile("mrc p15, 0, %0, c15, c12, 0" : "=r"(val));
  164. return val;
  165. }
  166. static inline void
  167. armv6_pmcr_write(unsigned long val)
  168. {
  169. asm volatile("mcr p15, 0, %0, c15, c12, 0" : : "r"(val));
  170. }
  171. #define ARMV6_PMCR_ENABLE (1 << 0)
  172. #define ARMV6_PMCR_CTR01_RESET (1 << 1)
  173. #define ARMV6_PMCR_CCOUNT_RESET (1 << 2)
  174. #define ARMV6_PMCR_CCOUNT_DIV (1 << 3)
  175. #define ARMV6_PMCR_COUNT0_IEN (1 << 4)
  176. #define ARMV6_PMCR_COUNT1_IEN (1 << 5)
  177. #define ARMV6_PMCR_CCOUNT_IEN (1 << 6)
  178. #define ARMV6_PMCR_COUNT0_OVERFLOW (1 << 8)
  179. #define ARMV6_PMCR_COUNT1_OVERFLOW (1 << 9)
  180. #define ARMV6_PMCR_CCOUNT_OVERFLOW (1 << 10)
  181. #define ARMV6_PMCR_EVT_COUNT0_SHIFT 20
  182. #define ARMV6_PMCR_EVT_COUNT0_MASK (0xFF << ARMV6_PMCR_EVT_COUNT0_SHIFT)
  183. #define ARMV6_PMCR_EVT_COUNT1_SHIFT 12
  184. #define ARMV6_PMCR_EVT_COUNT1_MASK (0xFF << ARMV6_PMCR_EVT_COUNT1_SHIFT)
  185. #define ARMV6_PMCR_OVERFLOWED_MASK \
  186. (ARMV6_PMCR_COUNT0_OVERFLOW | ARMV6_PMCR_COUNT1_OVERFLOW | \
  187. ARMV6_PMCR_CCOUNT_OVERFLOW)
  188. static inline int
  189. armv6_pmcr_has_overflowed(unsigned long pmcr)
  190. {
  191. return pmcr & ARMV6_PMCR_OVERFLOWED_MASK;
  192. }
  193. static inline int
  194. armv6_pmcr_counter_has_overflowed(unsigned long pmcr,
  195. enum armv6_counters counter)
  196. {
  197. int ret = 0;
  198. if (ARMV6_CYCLE_COUNTER == counter)
  199. ret = pmcr & ARMV6_PMCR_CCOUNT_OVERFLOW;
  200. else if (ARMV6_COUNTER0 == counter)
  201. ret = pmcr & ARMV6_PMCR_COUNT0_OVERFLOW;
  202. else if (ARMV6_COUNTER1 == counter)
  203. ret = pmcr & ARMV6_PMCR_COUNT1_OVERFLOW;
  204. else
  205. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  206. return ret;
  207. }
  208. static inline u32 armv6pmu_read_counter(struct perf_event *event)
  209. {
  210. struct hw_perf_event *hwc = &event->hw;
  211. int counter = hwc->idx;
  212. unsigned long value = 0;
  213. if (ARMV6_CYCLE_COUNTER == counter)
  214. asm volatile("mrc p15, 0, %0, c15, c12, 1" : "=r"(value));
  215. else if (ARMV6_COUNTER0 == counter)
  216. asm volatile("mrc p15, 0, %0, c15, c12, 2" : "=r"(value));
  217. else if (ARMV6_COUNTER1 == counter)
  218. asm volatile("mrc p15, 0, %0, c15, c12, 3" : "=r"(value));
  219. else
  220. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  221. return value;
  222. }
  223. static inline void armv6pmu_write_counter(struct perf_event *event, u32 value)
  224. {
  225. struct hw_perf_event *hwc = &event->hw;
  226. int counter = hwc->idx;
  227. if (ARMV6_CYCLE_COUNTER == counter)
  228. asm volatile("mcr p15, 0, %0, c15, c12, 1" : : "r"(value));
  229. else if (ARMV6_COUNTER0 == counter)
  230. asm volatile("mcr p15, 0, %0, c15, c12, 2" : : "r"(value));
  231. else if (ARMV6_COUNTER1 == counter)
  232. asm volatile("mcr p15, 0, %0, c15, c12, 3" : : "r"(value));
  233. else
  234. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  235. }
  236. static void armv6pmu_enable_event(struct perf_event *event)
  237. {
  238. unsigned long val, mask, evt, flags;
  239. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  240. struct hw_perf_event *hwc = &event->hw;
  241. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  242. int idx = hwc->idx;
  243. if (ARMV6_CYCLE_COUNTER == idx) {
  244. mask = 0;
  245. evt = ARMV6_PMCR_CCOUNT_IEN;
  246. } else if (ARMV6_COUNTER0 == idx) {
  247. mask = ARMV6_PMCR_EVT_COUNT0_MASK;
  248. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT0_SHIFT) |
  249. ARMV6_PMCR_COUNT0_IEN;
  250. } else if (ARMV6_COUNTER1 == idx) {
  251. mask = ARMV6_PMCR_EVT_COUNT1_MASK;
  252. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT1_SHIFT) |
  253. ARMV6_PMCR_COUNT1_IEN;
  254. } else {
  255. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  256. return;
  257. }
  258. /*
  259. * Mask out the current event and set the counter to count the event
  260. * that we're interested in.
  261. */
  262. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  263. val = armv6_pmcr_read();
  264. val &= ~mask;
  265. val |= evt;
  266. armv6_pmcr_write(val);
  267. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  268. }
  269. static irqreturn_t
  270. armv6pmu_handle_irq(int irq_num,
  271. void *dev)
  272. {
  273. unsigned long pmcr = armv6_pmcr_read();
  274. struct perf_sample_data data;
  275. struct arm_pmu *cpu_pmu = (struct arm_pmu *)dev;
  276. struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
  277. struct pt_regs *regs;
  278. int idx;
  279. if (!armv6_pmcr_has_overflowed(pmcr))
  280. return IRQ_NONE;
  281. regs = get_irq_regs();
  282. /*
  283. * The interrupts are cleared by writing the overflow flags back to
  284. * the control register. All of the other bits don't have any effect
  285. * if they are rewritten, so write the whole value back.
  286. */
  287. armv6_pmcr_write(pmcr);
  288. for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
  289. struct perf_event *event = cpuc->events[idx];
  290. struct hw_perf_event *hwc;
  291. /* Ignore if we don't have an event. */
  292. if (!event)
  293. continue;
  294. /*
  295. * We have a single interrupt for all counters. Check that
  296. * each counter has overflowed before we process it.
  297. */
  298. if (!armv6_pmcr_counter_has_overflowed(pmcr, idx))
  299. continue;
  300. hwc = &event->hw;
  301. armpmu_event_update(event);
  302. perf_sample_data_init(&data, 0, hwc->last_period);
  303. if (!armpmu_event_set_period(event))
  304. continue;
  305. if (perf_event_overflow(event, &data, regs))
  306. cpu_pmu->disable(event);
  307. }
  308. /*
  309. * Handle the pending perf events.
  310. *
  311. * Note: this call *must* be run with interrupts disabled. For
  312. * platforms that can have the PMU interrupts raised as an NMI, this
  313. * will not work.
  314. */
  315. irq_work_run();
  316. return IRQ_HANDLED;
  317. }
  318. static void armv6pmu_start(struct arm_pmu *cpu_pmu)
  319. {
  320. unsigned long flags, val;
  321. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  322. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  323. val = armv6_pmcr_read();
  324. val |= ARMV6_PMCR_ENABLE;
  325. armv6_pmcr_write(val);
  326. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  327. }
  328. static void armv6pmu_stop(struct arm_pmu *cpu_pmu)
  329. {
  330. unsigned long flags, val;
  331. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  332. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  333. val = armv6_pmcr_read();
  334. val &= ~ARMV6_PMCR_ENABLE;
  335. armv6_pmcr_write(val);
  336. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  337. }
  338. static int
  339. armv6pmu_get_event_idx(struct pmu_hw_events *cpuc,
  340. struct perf_event *event)
  341. {
  342. struct hw_perf_event *hwc = &event->hw;
  343. /* Always place a cycle counter into the cycle counter. */
  344. if (ARMV6_PERFCTR_CPU_CYCLES == hwc->config_base) {
  345. if (test_and_set_bit(ARMV6_CYCLE_COUNTER, cpuc->used_mask))
  346. return -EAGAIN;
  347. return ARMV6_CYCLE_COUNTER;
  348. } else {
  349. /*
  350. * For anything other than a cycle counter, try and use
  351. * counter0 and counter1.
  352. */
  353. if (!test_and_set_bit(ARMV6_COUNTER1, cpuc->used_mask))
  354. return ARMV6_COUNTER1;
  355. if (!test_and_set_bit(ARMV6_COUNTER0, cpuc->used_mask))
  356. return ARMV6_COUNTER0;
  357. /* The counters are all in use. */
  358. return -EAGAIN;
  359. }
  360. }
  361. static void armv6pmu_disable_event(struct perf_event *event)
  362. {
  363. unsigned long val, mask, evt, flags;
  364. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  365. struct hw_perf_event *hwc = &event->hw;
  366. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  367. int idx = hwc->idx;
  368. if (ARMV6_CYCLE_COUNTER == idx) {
  369. mask = ARMV6_PMCR_CCOUNT_IEN;
  370. evt = 0;
  371. } else if (ARMV6_COUNTER0 == idx) {
  372. mask = ARMV6_PMCR_COUNT0_IEN | ARMV6_PMCR_EVT_COUNT0_MASK;
  373. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT0_SHIFT;
  374. } else if (ARMV6_COUNTER1 == idx) {
  375. mask = ARMV6_PMCR_COUNT1_IEN | ARMV6_PMCR_EVT_COUNT1_MASK;
  376. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT1_SHIFT;
  377. } else {
  378. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  379. return;
  380. }
  381. /*
  382. * Mask out the current event and set the counter to count the number
  383. * of ETM bus signal assertion cycles. The external reporting should
  384. * be disabled and so this should never increment.
  385. */
  386. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  387. val = armv6_pmcr_read();
  388. val &= ~mask;
  389. val |= evt;
  390. armv6_pmcr_write(val);
  391. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  392. }
  393. static void armv6mpcore_pmu_disable_event(struct perf_event *event)
  394. {
  395. unsigned long val, mask, flags, evt = 0;
  396. struct arm_pmu *cpu_pmu = to_arm_pmu(event->pmu);
  397. struct hw_perf_event *hwc = &event->hw;
  398. struct pmu_hw_events *events = this_cpu_ptr(cpu_pmu->hw_events);
  399. int idx = hwc->idx;
  400. if (ARMV6_CYCLE_COUNTER == idx) {
  401. mask = ARMV6_PMCR_CCOUNT_IEN;
  402. } else if (ARMV6_COUNTER0 == idx) {
  403. mask = ARMV6_PMCR_COUNT0_IEN;
  404. } else if (ARMV6_COUNTER1 == idx) {
  405. mask = ARMV6_PMCR_COUNT1_IEN;
  406. } else {
  407. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  408. return;
  409. }
  410. /*
  411. * Unlike UP ARMv6, we don't have a way of stopping the counters. We
  412. * simply disable the interrupt reporting.
  413. */
  414. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  415. val = armv6_pmcr_read();
  416. val &= ~mask;
  417. val |= evt;
  418. armv6_pmcr_write(val);
  419. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  420. }
  421. static int armv6_map_event(struct perf_event *event)
  422. {
  423. return armpmu_map_event(event, &armv6_perf_map,
  424. &armv6_perf_cache_map, 0xFF);
  425. }
  426. static void armv6pmu_init(struct arm_pmu *cpu_pmu)
  427. {
  428. cpu_pmu->handle_irq = armv6pmu_handle_irq;
  429. cpu_pmu->enable = armv6pmu_enable_event;
  430. cpu_pmu->disable = armv6pmu_disable_event;
  431. cpu_pmu->read_counter = armv6pmu_read_counter;
  432. cpu_pmu->write_counter = armv6pmu_write_counter;
  433. cpu_pmu->get_event_idx = armv6pmu_get_event_idx;
  434. cpu_pmu->start = armv6pmu_start;
  435. cpu_pmu->stop = armv6pmu_stop;
  436. cpu_pmu->map_event = armv6_map_event;
  437. cpu_pmu->num_events = 3;
  438. cpu_pmu->max_period = (1LLU << 32) - 1;
  439. }
  440. static int armv6_1136_pmu_init(struct arm_pmu *cpu_pmu)
  441. {
  442. armv6pmu_init(cpu_pmu);
  443. cpu_pmu->name = "armv6_1136";
  444. return 0;
  445. }
  446. static int armv6_1156_pmu_init(struct arm_pmu *cpu_pmu)
  447. {
  448. armv6pmu_init(cpu_pmu);
  449. cpu_pmu->name = "armv6_1156";
  450. return 0;
  451. }
  452. static int armv6_1176_pmu_init(struct arm_pmu *cpu_pmu)
  453. {
  454. armv6pmu_init(cpu_pmu);
  455. cpu_pmu->name = "armv6_1176";
  456. return 0;
  457. }
  458. /*
  459. * ARMv6mpcore is almost identical to single core ARMv6 with the exception
  460. * that some of the events have different enumerations and that there is no
  461. * *hack* to stop the programmable counters. To stop the counters we simply
  462. * disable the interrupt reporting and update the event. When unthrottling we
  463. * reset the period and enable the interrupt reporting.
  464. */
  465. static int armv6mpcore_map_event(struct perf_event *event)
  466. {
  467. return armpmu_map_event(event, &armv6mpcore_perf_map,
  468. &armv6mpcore_perf_cache_map, 0xFF);
  469. }
  470. static int armv6mpcore_pmu_init(struct arm_pmu *cpu_pmu)
  471. {
  472. cpu_pmu->name = "armv6_11mpcore";
  473. cpu_pmu->handle_irq = armv6pmu_handle_irq;
  474. cpu_pmu->enable = armv6pmu_enable_event;
  475. cpu_pmu->disable = armv6mpcore_pmu_disable_event;
  476. cpu_pmu->read_counter = armv6pmu_read_counter;
  477. cpu_pmu->write_counter = armv6pmu_write_counter;
  478. cpu_pmu->get_event_idx = armv6pmu_get_event_idx;
  479. cpu_pmu->start = armv6pmu_start;
  480. cpu_pmu->stop = armv6pmu_stop;
  481. cpu_pmu->map_event = armv6mpcore_map_event;
  482. cpu_pmu->num_events = 3;
  483. cpu_pmu->max_period = (1LLU << 32) - 1;
  484. return 0;
  485. }
  486. static struct of_device_id armv6_pmu_of_device_ids[] = {
  487. {.compatible = "arm,arm11mpcore-pmu", .data = armv6mpcore_pmu_init},
  488. {.compatible = "arm,arm1176-pmu", .data = armv6_1176_pmu_init},
  489. {.compatible = "arm,arm1136-pmu", .data = armv6_1136_pmu_init},
  490. { /* sentinel value */ }
  491. };
  492. static const struct pmu_probe_info armv6_pmu_probe_table[] = {
  493. ARM_PMU_PROBE(ARM_CPU_PART_ARM1136, armv6_1136_pmu_init),
  494. ARM_PMU_PROBE(ARM_CPU_PART_ARM1156, armv6_1156_pmu_init),
  495. ARM_PMU_PROBE(ARM_CPU_PART_ARM1176, armv6_1176_pmu_init),
  496. ARM_PMU_PROBE(ARM_CPU_PART_ARM11MPCORE, armv6mpcore_pmu_init),
  497. { /* sentinel value */ }
  498. };
  499. static int armv6_pmu_device_probe(struct platform_device *pdev)
  500. {
  501. return arm_pmu_device_probe(pdev, armv6_pmu_of_device_ids,
  502. armv6_pmu_probe_table);
  503. }
  504. static struct platform_driver armv6_pmu_driver = {
  505. .driver = {
  506. .name = "armv6-pmu",
  507. .of_match_table = armv6_pmu_of_device_ids,
  508. },
  509. .probe = armv6_pmu_device_probe,
  510. };
  511. static int __init register_armv6_pmu_driver(void)
  512. {
  513. return platform_driver_register(&armv6_pmu_driver);
  514. }
  515. device_initcall(register_armv6_pmu_driver);
  516. #endif /* CONFIG_CPU_V6 || CONFIG_CPU_V6K */