arm_v6_pmu.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ARMv6 Performance counter handling code.
  4. *
  5. * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
  6. *
  7. * ARMv6 has 2 configurable performance counters and a single cycle counter.
  8. * They all share a single reset bit but can be written to zero so we can use
  9. * that for a reset.
  10. *
  11. * The counters can't be individually enabled or disabled so when we remove
  12. * one event and replace it with another we could get spurious counts from the
  13. * wrong event. However, we can take advantage of the fact that the
  14. * performance counters can export events to the event bus, and the event bus
  15. * itself can be monitored. This requires that we *don't* export the events to
  16. * the event bus. The procedure for disabling a configurable counter is:
  17. * - change the counter to count the ETMEXTOUT[0] signal (0x20). This
  18. * effectively stops the counter from counting.
  19. * - disable the counter's interrupt generation (each counter has it's
  20. * own interrupt enable bit).
  21. * Once stopped, the counter value can be written as 0 to reset.
  22. *
  23. * To enable a counter:
  24. * - enable the counter's interrupt generation.
  25. * - set the new event type.
  26. *
  27. * Note: the dedicated cycle counter only counts cycles and can't be
  28. * enabled/disabled independently of the others. When we want to disable the
  29. * cycle counter, we have to just disable the interrupt reporting and start
  30. * ignoring that counter. When re-enabling, we have to reset the value and
  31. * enable the interrupt.
  32. */
  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. ARMV6_NUM_COUNTERS
  64. };
  65. /*
  66. * The hardware events that we support. We do support cache operations but
  67. * we have harvard caches and no way to combine instruction and data
  68. * accesses/misses in hardware.
  69. */
  70. static const unsigned armv6_perf_map[PERF_COUNT_HW_MAX] = {
  71. PERF_MAP_ALL_UNSUPPORTED,
  72. [PERF_COUNT_HW_CPU_CYCLES] = ARMV6_PERFCTR_CPU_CYCLES,
  73. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV6_PERFCTR_INSTR_EXEC,
  74. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = ARMV6_PERFCTR_BR_EXEC,
  75. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV6_PERFCTR_BR_MISPREDICT,
  76. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = ARMV6_PERFCTR_IBUF_STALL,
  77. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = ARMV6_PERFCTR_LSU_FULL_STALL,
  78. };
  79. static const unsigned armv6_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  80. [PERF_COUNT_HW_CACHE_OP_MAX]
  81. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  82. PERF_CACHE_MAP_ALL_UNSUPPORTED,
  83. /*
  84. * The performance counters don't differentiate between read and write
  85. * accesses/misses so this isn't strictly correct, but it's the best we
  86. * can do. Writes and reads get combined.
  87. */
  88. [C(L1D)][C(OP_READ)][C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  89. [C(L1D)][C(OP_READ)][C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  90. [C(L1D)][C(OP_WRITE)][C(RESULT_ACCESS)] = ARMV6_PERFCTR_DCACHE_ACCESS,
  91. [C(L1D)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6_PERFCTR_DCACHE_MISS,
  92. [C(L1I)][C(OP_READ)][C(RESULT_MISS)] = ARMV6_PERFCTR_ICACHE_MISS,
  93. /*
  94. * The ARM performance counters can count micro DTLB misses, micro ITLB
  95. * misses and main TLB misses. There isn't an event for TLB misses, so
  96. * use the micro misses here and if users want the main TLB misses they
  97. * can use a raw counter.
  98. */
  99. [C(DTLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  100. [C(DTLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6_PERFCTR_DTLB_MISS,
  101. [C(ITLB)][C(OP_READ)][C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  102. [C(ITLB)][C(OP_WRITE)][C(RESULT_MISS)] = ARMV6_PERFCTR_ITLB_MISS,
  103. };
  104. static inline unsigned long
  105. armv6_pmcr_read(void)
  106. {
  107. u32 val;
  108. asm volatile("mrc p15, 0, %0, c15, c12, 0" : "=r"(val));
  109. return val;
  110. }
  111. static inline void
  112. armv6_pmcr_write(unsigned long val)
  113. {
  114. asm volatile("mcr p15, 0, %0, c15, c12, 0" : : "r"(val));
  115. }
  116. #define ARMV6_PMCR_ENABLE (1 << 0)
  117. #define ARMV6_PMCR_CTR01_RESET (1 << 1)
  118. #define ARMV6_PMCR_CCOUNT_RESET (1 << 2)
  119. #define ARMV6_PMCR_CCOUNT_DIV (1 << 3)
  120. #define ARMV6_PMCR_COUNT0_IEN (1 << 4)
  121. #define ARMV6_PMCR_COUNT1_IEN (1 << 5)
  122. #define ARMV6_PMCR_CCOUNT_IEN (1 << 6)
  123. #define ARMV6_PMCR_COUNT0_OVERFLOW (1 << 8)
  124. #define ARMV6_PMCR_COUNT1_OVERFLOW (1 << 9)
  125. #define ARMV6_PMCR_CCOUNT_OVERFLOW (1 << 10)
  126. #define ARMV6_PMCR_EVT_COUNT0_SHIFT 20
  127. #define ARMV6_PMCR_EVT_COUNT0_MASK (0xFF << ARMV6_PMCR_EVT_COUNT0_SHIFT)
  128. #define ARMV6_PMCR_EVT_COUNT1_SHIFT 12
  129. #define ARMV6_PMCR_EVT_COUNT1_MASK (0xFF << ARMV6_PMCR_EVT_COUNT1_SHIFT)
  130. #define ARMV6_PMCR_OVERFLOWED_MASK \
  131. (ARMV6_PMCR_COUNT0_OVERFLOW | ARMV6_PMCR_COUNT1_OVERFLOW | \
  132. ARMV6_PMCR_CCOUNT_OVERFLOW)
  133. static inline int
  134. armv6_pmcr_has_overflowed(unsigned long pmcr)
  135. {
  136. return pmcr & ARMV6_PMCR_OVERFLOWED_MASK;
  137. }
  138. static inline int
  139. armv6_pmcr_counter_has_overflowed(unsigned long pmcr,
  140. enum armv6_counters counter)
  141. {
  142. int ret = 0;
  143. if (ARMV6_CYCLE_COUNTER == counter)
  144. ret = pmcr & ARMV6_PMCR_CCOUNT_OVERFLOW;
  145. else if (ARMV6_COUNTER0 == counter)
  146. ret = pmcr & ARMV6_PMCR_COUNT0_OVERFLOW;
  147. else if (ARMV6_COUNTER1 == counter)
  148. ret = pmcr & ARMV6_PMCR_COUNT1_OVERFLOW;
  149. else
  150. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  151. return ret;
  152. }
  153. static inline u64 armv6pmu_read_counter(struct perf_event *event)
  154. {
  155. struct hw_perf_event *hwc = &event->hw;
  156. int counter = hwc->idx;
  157. unsigned long value = 0;
  158. if (ARMV6_CYCLE_COUNTER == counter)
  159. asm volatile("mrc p15, 0, %0, c15, c12, 1" : "=r"(value));
  160. else if (ARMV6_COUNTER0 == counter)
  161. asm volatile("mrc p15, 0, %0, c15, c12, 2" : "=r"(value));
  162. else if (ARMV6_COUNTER1 == counter)
  163. asm volatile("mrc p15, 0, %0, c15, c12, 3" : "=r"(value));
  164. else
  165. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  166. return value;
  167. }
  168. static inline void armv6pmu_write_counter(struct perf_event *event, u64 value)
  169. {
  170. struct hw_perf_event *hwc = &event->hw;
  171. int counter = hwc->idx;
  172. if (ARMV6_CYCLE_COUNTER == counter)
  173. asm volatile("mcr p15, 0, %0, c15, c12, 1" : : "r"(value));
  174. else if (ARMV6_COUNTER0 == counter)
  175. asm volatile("mcr p15, 0, %0, c15, c12, 2" : : "r"(value));
  176. else if (ARMV6_COUNTER1 == counter)
  177. asm volatile("mcr p15, 0, %0, c15, c12, 3" : : "r"(value));
  178. else
  179. WARN_ONCE(1, "invalid counter number (%d)\n", counter);
  180. }
  181. static void armv6pmu_enable_event(struct perf_event *event)
  182. {
  183. unsigned long val, mask, evt;
  184. struct hw_perf_event *hwc = &event->hw;
  185. int idx = hwc->idx;
  186. if (ARMV6_CYCLE_COUNTER == idx) {
  187. mask = 0;
  188. evt = ARMV6_PMCR_CCOUNT_IEN;
  189. } else if (ARMV6_COUNTER0 == idx) {
  190. mask = ARMV6_PMCR_EVT_COUNT0_MASK;
  191. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT0_SHIFT) |
  192. ARMV6_PMCR_COUNT0_IEN;
  193. } else if (ARMV6_COUNTER1 == idx) {
  194. mask = ARMV6_PMCR_EVT_COUNT1_MASK;
  195. evt = (hwc->config_base << ARMV6_PMCR_EVT_COUNT1_SHIFT) |
  196. ARMV6_PMCR_COUNT1_IEN;
  197. } else {
  198. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  199. return;
  200. }
  201. /*
  202. * Mask out the current event and set the counter to count the event
  203. * that we're interested in.
  204. */
  205. val = armv6_pmcr_read();
  206. val &= ~mask;
  207. val |= evt;
  208. armv6_pmcr_write(val);
  209. }
  210. static irqreturn_t
  211. armv6pmu_handle_irq(struct arm_pmu *cpu_pmu)
  212. {
  213. unsigned long pmcr = armv6_pmcr_read();
  214. struct perf_sample_data data;
  215. struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
  216. struct pt_regs *regs;
  217. int idx;
  218. if (!armv6_pmcr_has_overflowed(pmcr))
  219. return IRQ_NONE;
  220. regs = get_irq_regs();
  221. /*
  222. * The interrupts are cleared by writing the overflow flags back to
  223. * the control register. All of the other bits don't have any effect
  224. * if they are rewritten, so write the whole value back.
  225. */
  226. armv6_pmcr_write(pmcr);
  227. for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV6_NUM_COUNTERS) {
  228. struct perf_event *event = cpuc->events[idx];
  229. struct hw_perf_event *hwc;
  230. /* Ignore if we don't have an event. */
  231. if (!event)
  232. continue;
  233. /*
  234. * We have a single interrupt for all counters. Check that
  235. * each counter has overflowed before we process it.
  236. */
  237. if (!armv6_pmcr_counter_has_overflowed(pmcr, idx))
  238. continue;
  239. hwc = &event->hw;
  240. armpmu_event_update(event);
  241. perf_sample_data_init(&data, 0, hwc->last_period);
  242. if (!armpmu_event_set_period(event))
  243. continue;
  244. if (perf_event_overflow(event, &data, regs))
  245. cpu_pmu->disable(event);
  246. }
  247. /*
  248. * Handle the pending perf events.
  249. *
  250. * Note: this call *must* be run with interrupts disabled. For
  251. * platforms that can have the PMU interrupts raised as an NMI, this
  252. * will not work.
  253. */
  254. irq_work_run();
  255. return IRQ_HANDLED;
  256. }
  257. static void armv6pmu_start(struct arm_pmu *cpu_pmu)
  258. {
  259. unsigned long val;
  260. val = armv6_pmcr_read();
  261. val |= ARMV6_PMCR_ENABLE;
  262. armv6_pmcr_write(val);
  263. }
  264. static void armv6pmu_stop(struct arm_pmu *cpu_pmu)
  265. {
  266. unsigned long val;
  267. val = armv6_pmcr_read();
  268. val &= ~ARMV6_PMCR_ENABLE;
  269. armv6_pmcr_write(val);
  270. }
  271. static int
  272. armv6pmu_get_event_idx(struct pmu_hw_events *cpuc,
  273. struct perf_event *event)
  274. {
  275. struct hw_perf_event *hwc = &event->hw;
  276. /* Always place a cycle counter into the cycle counter. */
  277. if (ARMV6_PERFCTR_CPU_CYCLES == hwc->config_base) {
  278. if (test_and_set_bit(ARMV6_CYCLE_COUNTER, cpuc->used_mask))
  279. return -EAGAIN;
  280. return ARMV6_CYCLE_COUNTER;
  281. } else {
  282. /*
  283. * For anything other than a cycle counter, try and use
  284. * counter0 and counter1.
  285. */
  286. if (!test_and_set_bit(ARMV6_COUNTER1, cpuc->used_mask))
  287. return ARMV6_COUNTER1;
  288. if (!test_and_set_bit(ARMV6_COUNTER0, cpuc->used_mask))
  289. return ARMV6_COUNTER0;
  290. /* The counters are all in use. */
  291. return -EAGAIN;
  292. }
  293. }
  294. static void armv6pmu_clear_event_idx(struct pmu_hw_events *cpuc,
  295. struct perf_event *event)
  296. {
  297. clear_bit(event->hw.idx, cpuc->used_mask);
  298. }
  299. static void armv6pmu_disable_event(struct perf_event *event)
  300. {
  301. unsigned long val, mask, evt;
  302. struct hw_perf_event *hwc = &event->hw;
  303. int idx = hwc->idx;
  304. if (ARMV6_CYCLE_COUNTER == idx) {
  305. mask = ARMV6_PMCR_CCOUNT_IEN;
  306. evt = 0;
  307. } else if (ARMV6_COUNTER0 == idx) {
  308. mask = ARMV6_PMCR_COUNT0_IEN | ARMV6_PMCR_EVT_COUNT0_MASK;
  309. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT0_SHIFT;
  310. } else if (ARMV6_COUNTER1 == idx) {
  311. mask = ARMV6_PMCR_COUNT1_IEN | ARMV6_PMCR_EVT_COUNT1_MASK;
  312. evt = ARMV6_PERFCTR_NOP << ARMV6_PMCR_EVT_COUNT1_SHIFT;
  313. } else {
  314. WARN_ONCE(1, "invalid counter number (%d)\n", idx);
  315. return;
  316. }
  317. /*
  318. * Mask out the current event and set the counter to count the number
  319. * of ETM bus signal assertion cycles. The external reporting should
  320. * be disabled and so this should never increment.
  321. */
  322. val = armv6_pmcr_read();
  323. val &= ~mask;
  324. val |= evt;
  325. armv6_pmcr_write(val);
  326. }
  327. static int armv6_map_event(struct perf_event *event)
  328. {
  329. return armpmu_map_event(event, &armv6_perf_map,
  330. &armv6_perf_cache_map, 0xFF);
  331. }
  332. static void armv6pmu_init(struct arm_pmu *cpu_pmu)
  333. {
  334. cpu_pmu->handle_irq = armv6pmu_handle_irq;
  335. cpu_pmu->enable = armv6pmu_enable_event;
  336. cpu_pmu->disable = armv6pmu_disable_event;
  337. cpu_pmu->read_counter = armv6pmu_read_counter;
  338. cpu_pmu->write_counter = armv6pmu_write_counter;
  339. cpu_pmu->get_event_idx = armv6pmu_get_event_idx;
  340. cpu_pmu->clear_event_idx = armv6pmu_clear_event_idx;
  341. cpu_pmu->start = armv6pmu_start;
  342. cpu_pmu->stop = armv6pmu_stop;
  343. cpu_pmu->map_event = armv6_map_event;
  344. bitmap_set(cpu_pmu->cntr_mask, 0, ARMV6_NUM_COUNTERS);
  345. }
  346. static int armv6_1136_pmu_init(struct arm_pmu *cpu_pmu)
  347. {
  348. armv6pmu_init(cpu_pmu);
  349. cpu_pmu->name = "armv6_1136";
  350. return 0;
  351. }
  352. static int armv6_1176_pmu_init(struct arm_pmu *cpu_pmu)
  353. {
  354. armv6pmu_init(cpu_pmu);
  355. cpu_pmu->name = "armv6_1176";
  356. return 0;
  357. }
  358. static const struct of_device_id armv6_pmu_of_device_ids[] = {
  359. {.compatible = "arm,arm1176-pmu", .data = armv6_1176_pmu_init},
  360. {.compatible = "arm,arm1136-pmu", .data = armv6_1136_pmu_init},
  361. { /* sentinel value */ }
  362. };
  363. static int armv6_pmu_device_probe(struct platform_device *pdev)
  364. {
  365. return arm_pmu_device_probe(pdev, armv6_pmu_of_device_ids, NULL);
  366. }
  367. static struct platform_driver armv6_pmu_driver = {
  368. .driver = {
  369. .name = "armv6-pmu",
  370. .of_match_table = armv6_pmu_of_device_ids,
  371. },
  372. .probe = armv6_pmu_device_probe,
  373. };
  374. builtin_platform_driver(armv6_pmu_driver);