apple_m1_cpu_pmu.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * CPU PMU driver for the Apple M1 and derivatives
  4. *
  5. * Copyright (C) 2021 Google LLC
  6. *
  7. * Author: Marc Zyngier <maz@kernel.org>
  8. *
  9. * Most of the information used in this driver was provided by the
  10. * Asahi Linux project. The rest was experimentally discovered.
  11. */
  12. #include <linux/of.h>
  13. #include <linux/perf/arm_pmu.h>
  14. #include <linux/platform_device.h>
  15. #include <asm/apple_m1_pmu.h>
  16. #include <asm/irq_regs.h>
  17. #include <asm/perf_event.h>
  18. #define M1_PMU_NR_COUNTERS 10
  19. #define M1_PMU_CFG_EVENT GENMASK(7, 0)
  20. #define ANY_BUT_0_1 GENMASK(9, 2)
  21. #define ONLY_2_TO_7 GENMASK(7, 2)
  22. #define ONLY_2_4_6 (BIT(2) | BIT(4) | BIT(6))
  23. #define ONLY_5_6_7 (BIT(5) | BIT(6) | BIT(7))
  24. /*
  25. * Description of the events we actually know about, as well as those with
  26. * a specific counter affinity. Yes, this is a grand total of two known
  27. * counters, and the rest is anybody's guess.
  28. *
  29. * Not all counters can count all events. Counters #0 and #1 are wired to
  30. * count cycles and instructions respectively, and some events have
  31. * bizarre mappings (every other counter, or even *one* counter). These
  32. * restrictions equally apply to both P and E cores.
  33. *
  34. * It is worth noting that the PMUs attached to P and E cores are likely
  35. * to be different because the underlying uarches are different. At the
  36. * moment, we don't really need to distinguish between the two because we
  37. * know next to nothing about the events themselves, and we already have
  38. * per cpu-type PMU abstractions.
  39. *
  40. * If we eventually find out that the events are different across
  41. * implementations, we'll have to introduce per cpu-type tables.
  42. */
  43. enum m1_pmu_events {
  44. M1_PMU_PERFCTR_RETIRE_UOP = 0x1,
  45. M1_PMU_PERFCTR_CORE_ACTIVE_CYCLE = 0x2,
  46. M1_PMU_PERFCTR_L1I_TLB_FILL = 0x4,
  47. M1_PMU_PERFCTR_L1D_TLB_FILL = 0x5,
  48. M1_PMU_PERFCTR_MMU_TABLE_WALK_INSTRUCTION = 0x7,
  49. M1_PMU_PERFCTR_MMU_TABLE_WALK_DATA = 0x8,
  50. M1_PMU_PERFCTR_L2_TLB_MISS_INSTRUCTION = 0xa,
  51. M1_PMU_PERFCTR_L2_TLB_MISS_DATA = 0xb,
  52. M1_PMU_PERFCTR_MMU_VIRTUAL_MEMORY_FAULT_NONSPEC = 0xd,
  53. M1_PMU_PERFCTR_SCHEDULE_UOP = 0x52,
  54. M1_PMU_PERFCTR_INTERRUPT_PENDING = 0x6c,
  55. M1_PMU_PERFCTR_MAP_STALL_DISPATCH = 0x70,
  56. M1_PMU_PERFCTR_MAP_REWIND = 0x75,
  57. M1_PMU_PERFCTR_MAP_STALL = 0x76,
  58. M1_PMU_PERFCTR_MAP_INT_UOP = 0x7c,
  59. M1_PMU_PERFCTR_MAP_LDST_UOP = 0x7d,
  60. M1_PMU_PERFCTR_MAP_SIMD_UOP = 0x7e,
  61. M1_PMU_PERFCTR_FLUSH_RESTART_OTHER_NONSPEC = 0x84,
  62. M1_PMU_PERFCTR_INST_ALL = 0x8c,
  63. M1_PMU_PERFCTR_INST_BRANCH = 0x8d,
  64. M1_PMU_PERFCTR_INST_BRANCH_CALL = 0x8e,
  65. M1_PMU_PERFCTR_INST_BRANCH_RET = 0x8f,
  66. M1_PMU_PERFCTR_INST_BRANCH_TAKEN = 0x90,
  67. M1_PMU_PERFCTR_INST_BRANCH_INDIR = 0x93,
  68. M1_PMU_PERFCTR_INST_BRANCH_COND = 0x94,
  69. M1_PMU_PERFCTR_INST_INT_LD = 0x95,
  70. M1_PMU_PERFCTR_INST_INT_ST = 0x96,
  71. M1_PMU_PERFCTR_INST_INT_ALU = 0x97,
  72. M1_PMU_PERFCTR_INST_SIMD_LD = 0x98,
  73. M1_PMU_PERFCTR_INST_SIMD_ST = 0x99,
  74. M1_PMU_PERFCTR_INST_SIMD_ALU = 0x9a,
  75. M1_PMU_PERFCTR_INST_LDST = 0x9b,
  76. M1_PMU_PERFCTR_INST_BARRIER = 0x9c,
  77. M1_PMU_PERFCTR_UNKNOWN_9f = 0x9f,
  78. M1_PMU_PERFCTR_L1D_TLB_ACCESS = 0xa0,
  79. M1_PMU_PERFCTR_L1D_TLB_MISS = 0xa1,
  80. M1_PMU_PERFCTR_L1D_CACHE_MISS_ST = 0xa2,
  81. M1_PMU_PERFCTR_L1D_CACHE_MISS_LD = 0xa3,
  82. M1_PMU_PERFCTR_LD_UNIT_UOP = 0xa6,
  83. M1_PMU_PERFCTR_ST_UNIT_UOP = 0xa7,
  84. M1_PMU_PERFCTR_L1D_CACHE_WRITEBACK = 0xa8,
  85. M1_PMU_PERFCTR_LDST_X64_UOP = 0xb1,
  86. M1_PMU_PERFCTR_LDST_XPG_UOP = 0xb2,
  87. M1_PMU_PERFCTR_ATOMIC_OR_EXCLUSIVE_SUCC = 0xb3,
  88. M1_PMU_PERFCTR_ATOMIC_OR_EXCLUSIVE_FAIL = 0xb4,
  89. M1_PMU_PERFCTR_L1D_CACHE_MISS_LD_NONSPEC = 0xbf,
  90. M1_PMU_PERFCTR_L1D_CACHE_MISS_ST_NONSPEC = 0xc0,
  91. M1_PMU_PERFCTR_L1D_TLB_MISS_NONSPEC = 0xc1,
  92. M1_PMU_PERFCTR_ST_MEMORY_ORDER_VIOLATION_NONSPEC = 0xc4,
  93. M1_PMU_PERFCTR_BRANCH_COND_MISPRED_NONSPEC = 0xc5,
  94. M1_PMU_PERFCTR_BRANCH_INDIR_MISPRED_NONSPEC = 0xc6,
  95. M1_PMU_PERFCTR_BRANCH_RET_INDIR_MISPRED_NONSPEC = 0xc8,
  96. M1_PMU_PERFCTR_BRANCH_CALL_INDIR_MISPRED_NONSPEC = 0xca,
  97. M1_PMU_PERFCTR_BRANCH_MISPRED_NONSPEC = 0xcb,
  98. M1_PMU_PERFCTR_L1I_TLB_MISS_DEMAND = 0xd4,
  99. M1_PMU_PERFCTR_MAP_DISPATCH_BUBBLE = 0xd6,
  100. M1_PMU_PERFCTR_L1I_CACHE_MISS_DEMAND = 0xdb,
  101. M1_PMU_PERFCTR_FETCH_RESTART = 0xde,
  102. M1_PMU_PERFCTR_ST_NT_UOP = 0xe5,
  103. M1_PMU_PERFCTR_LD_NT_UOP = 0xe6,
  104. M1_PMU_PERFCTR_UNKNOWN_f5 = 0xf5,
  105. M1_PMU_PERFCTR_UNKNOWN_f6 = 0xf6,
  106. M1_PMU_PERFCTR_UNKNOWN_f7 = 0xf7,
  107. M1_PMU_PERFCTR_UNKNOWN_f8 = 0xf8,
  108. M1_PMU_PERFCTR_UNKNOWN_fd = 0xfd,
  109. M1_PMU_PERFCTR_LAST = M1_PMU_CFG_EVENT,
  110. /*
  111. * From this point onwards, these are not actual HW events,
  112. * but attributes that get stored in hw->config_base.
  113. */
  114. M1_PMU_CFG_COUNT_USER = BIT(8),
  115. M1_PMU_CFG_COUNT_KERNEL = BIT(9),
  116. };
  117. /*
  118. * Per-event affinity table. Most events can be installed on counter
  119. * 2-9, but there are a number of exceptions. Note that this table
  120. * has been created experimentally, and I wouldn't be surprised if more
  121. * counters had strange affinities.
  122. */
  123. static const u16 m1_pmu_event_affinity[M1_PMU_PERFCTR_LAST + 1] = {
  124. [0 ... M1_PMU_PERFCTR_LAST] = ANY_BUT_0_1,
  125. [M1_PMU_PERFCTR_RETIRE_UOP] = BIT(7),
  126. [M1_PMU_PERFCTR_CORE_ACTIVE_CYCLE] = ANY_BUT_0_1 | BIT(0),
  127. [M1_PMU_PERFCTR_INST_ALL] = BIT(7) | BIT(1),
  128. [M1_PMU_PERFCTR_INST_BRANCH] = ONLY_5_6_7,
  129. [M1_PMU_PERFCTR_INST_BRANCH_CALL] = ONLY_5_6_7,
  130. [M1_PMU_PERFCTR_INST_BRANCH_RET] = ONLY_5_6_7,
  131. [M1_PMU_PERFCTR_INST_BRANCH_TAKEN] = ONLY_5_6_7,
  132. [M1_PMU_PERFCTR_INST_BRANCH_INDIR] = ONLY_5_6_7,
  133. [M1_PMU_PERFCTR_INST_BRANCH_COND] = ONLY_5_6_7,
  134. [M1_PMU_PERFCTR_INST_INT_LD] = ONLY_5_6_7,
  135. [M1_PMU_PERFCTR_INST_INT_ST] = BIT(7),
  136. [M1_PMU_PERFCTR_INST_INT_ALU] = BIT(7),
  137. [M1_PMU_PERFCTR_INST_SIMD_LD] = ONLY_5_6_7,
  138. [M1_PMU_PERFCTR_INST_SIMD_ST] = ONLY_5_6_7,
  139. [M1_PMU_PERFCTR_INST_SIMD_ALU] = BIT(7),
  140. [M1_PMU_PERFCTR_INST_LDST] = BIT(7),
  141. [M1_PMU_PERFCTR_INST_BARRIER] = ONLY_5_6_7,
  142. [M1_PMU_PERFCTR_UNKNOWN_9f] = BIT(7),
  143. [M1_PMU_PERFCTR_L1D_CACHE_MISS_LD_NONSPEC] = ONLY_5_6_7,
  144. [M1_PMU_PERFCTR_L1D_CACHE_MISS_ST_NONSPEC] = ONLY_5_6_7,
  145. [M1_PMU_PERFCTR_L1D_TLB_MISS_NONSPEC] = ONLY_5_6_7,
  146. [M1_PMU_PERFCTR_ST_MEMORY_ORDER_VIOLATION_NONSPEC] = ONLY_5_6_7,
  147. [M1_PMU_PERFCTR_BRANCH_COND_MISPRED_NONSPEC] = ONLY_5_6_7,
  148. [M1_PMU_PERFCTR_BRANCH_INDIR_MISPRED_NONSPEC] = ONLY_5_6_7,
  149. [M1_PMU_PERFCTR_BRANCH_RET_INDIR_MISPRED_NONSPEC] = ONLY_5_6_7,
  150. [M1_PMU_PERFCTR_BRANCH_CALL_INDIR_MISPRED_NONSPEC] = ONLY_5_6_7,
  151. [M1_PMU_PERFCTR_BRANCH_MISPRED_NONSPEC] = ONLY_5_6_7,
  152. [M1_PMU_PERFCTR_UNKNOWN_f5] = ONLY_2_4_6,
  153. [M1_PMU_PERFCTR_UNKNOWN_f6] = ONLY_2_4_6,
  154. [M1_PMU_PERFCTR_UNKNOWN_f7] = ONLY_2_4_6,
  155. [M1_PMU_PERFCTR_UNKNOWN_f8] = ONLY_2_TO_7,
  156. [M1_PMU_PERFCTR_UNKNOWN_fd] = ONLY_2_4_6,
  157. };
  158. static const unsigned m1_pmu_perf_map[PERF_COUNT_HW_MAX] = {
  159. PERF_MAP_ALL_UNSUPPORTED,
  160. [PERF_COUNT_HW_CPU_CYCLES] = M1_PMU_PERFCTR_CORE_ACTIVE_CYCLE,
  161. [PERF_COUNT_HW_INSTRUCTIONS] = M1_PMU_PERFCTR_INST_ALL,
  162. };
  163. /* sysfs definitions */
  164. static ssize_t m1_pmu_events_sysfs_show(struct device *dev,
  165. struct device_attribute *attr,
  166. char *page)
  167. {
  168. struct perf_pmu_events_attr *pmu_attr;
  169. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
  170. return sprintf(page, "event=0x%04llx\n", pmu_attr->id);
  171. }
  172. #define M1_PMU_EVENT_ATTR(name, config) \
  173. PMU_EVENT_ATTR_ID(name, m1_pmu_events_sysfs_show, config)
  174. static struct attribute *m1_pmu_event_attrs[] = {
  175. M1_PMU_EVENT_ATTR(cycles, M1_PMU_PERFCTR_CORE_ACTIVE_CYCLE),
  176. M1_PMU_EVENT_ATTR(instructions, M1_PMU_PERFCTR_INST_ALL),
  177. NULL,
  178. };
  179. static const struct attribute_group m1_pmu_events_attr_group = {
  180. .name = "events",
  181. .attrs = m1_pmu_event_attrs,
  182. };
  183. PMU_FORMAT_ATTR(event, "config:0-7");
  184. static struct attribute *m1_pmu_format_attrs[] = {
  185. &format_attr_event.attr,
  186. NULL,
  187. };
  188. static const struct attribute_group m1_pmu_format_attr_group = {
  189. .name = "format",
  190. .attrs = m1_pmu_format_attrs,
  191. };
  192. /* Low level accessors. No synchronisation. */
  193. #define PMU_READ_COUNTER(_idx) \
  194. case _idx: return read_sysreg_s(SYS_IMP_APL_PMC## _idx ##_EL1)
  195. #define PMU_WRITE_COUNTER(_val, _idx) \
  196. case _idx: \
  197. write_sysreg_s(_val, SYS_IMP_APL_PMC## _idx ##_EL1); \
  198. return
  199. static u64 m1_pmu_read_hw_counter(unsigned int index)
  200. {
  201. switch (index) {
  202. PMU_READ_COUNTER(0);
  203. PMU_READ_COUNTER(1);
  204. PMU_READ_COUNTER(2);
  205. PMU_READ_COUNTER(3);
  206. PMU_READ_COUNTER(4);
  207. PMU_READ_COUNTER(5);
  208. PMU_READ_COUNTER(6);
  209. PMU_READ_COUNTER(7);
  210. PMU_READ_COUNTER(8);
  211. PMU_READ_COUNTER(9);
  212. }
  213. BUG();
  214. }
  215. static void m1_pmu_write_hw_counter(u64 val, unsigned int index)
  216. {
  217. switch (index) {
  218. PMU_WRITE_COUNTER(val, 0);
  219. PMU_WRITE_COUNTER(val, 1);
  220. PMU_WRITE_COUNTER(val, 2);
  221. PMU_WRITE_COUNTER(val, 3);
  222. PMU_WRITE_COUNTER(val, 4);
  223. PMU_WRITE_COUNTER(val, 5);
  224. PMU_WRITE_COUNTER(val, 6);
  225. PMU_WRITE_COUNTER(val, 7);
  226. PMU_WRITE_COUNTER(val, 8);
  227. PMU_WRITE_COUNTER(val, 9);
  228. }
  229. BUG();
  230. }
  231. #define get_bit_offset(index, mask) (__ffs(mask) + (index))
  232. static void __m1_pmu_enable_counter(unsigned int index, bool en)
  233. {
  234. u64 val, bit;
  235. switch (index) {
  236. case 0 ... 7:
  237. bit = BIT(get_bit_offset(index, PMCR0_CNT_ENABLE_0_7));
  238. break;
  239. case 8 ... 9:
  240. bit = BIT(get_bit_offset(index - 8, PMCR0_CNT_ENABLE_8_9));
  241. break;
  242. default:
  243. BUG();
  244. }
  245. val = read_sysreg_s(SYS_IMP_APL_PMCR0_EL1);
  246. if (en)
  247. val |= bit;
  248. else
  249. val &= ~bit;
  250. write_sysreg_s(val, SYS_IMP_APL_PMCR0_EL1);
  251. }
  252. static void m1_pmu_enable_counter(unsigned int index)
  253. {
  254. __m1_pmu_enable_counter(index, true);
  255. }
  256. static void m1_pmu_disable_counter(unsigned int index)
  257. {
  258. __m1_pmu_enable_counter(index, false);
  259. }
  260. static void __m1_pmu_enable_counter_interrupt(unsigned int index, bool en)
  261. {
  262. u64 val, bit;
  263. switch (index) {
  264. case 0 ... 7:
  265. bit = BIT(get_bit_offset(index, PMCR0_PMI_ENABLE_0_7));
  266. break;
  267. case 8 ... 9:
  268. bit = BIT(get_bit_offset(index - 8, PMCR0_PMI_ENABLE_8_9));
  269. break;
  270. default:
  271. BUG();
  272. }
  273. val = read_sysreg_s(SYS_IMP_APL_PMCR0_EL1);
  274. if (en)
  275. val |= bit;
  276. else
  277. val &= ~bit;
  278. write_sysreg_s(val, SYS_IMP_APL_PMCR0_EL1);
  279. }
  280. static void m1_pmu_enable_counter_interrupt(unsigned int index)
  281. {
  282. __m1_pmu_enable_counter_interrupt(index, true);
  283. }
  284. static void m1_pmu_disable_counter_interrupt(unsigned int index)
  285. {
  286. __m1_pmu_enable_counter_interrupt(index, false);
  287. }
  288. static void m1_pmu_configure_counter(unsigned int index, u8 event,
  289. bool user, bool kernel)
  290. {
  291. u64 val, user_bit, kernel_bit;
  292. int shift;
  293. switch (index) {
  294. case 0 ... 7:
  295. user_bit = BIT(get_bit_offset(index, PMCR1_COUNT_A64_EL0_0_7));
  296. kernel_bit = BIT(get_bit_offset(index, PMCR1_COUNT_A64_EL1_0_7));
  297. break;
  298. case 8 ... 9:
  299. user_bit = BIT(get_bit_offset(index - 8, PMCR1_COUNT_A64_EL0_8_9));
  300. kernel_bit = BIT(get_bit_offset(index - 8, PMCR1_COUNT_A64_EL1_8_9));
  301. break;
  302. default:
  303. BUG();
  304. }
  305. val = read_sysreg_s(SYS_IMP_APL_PMCR1_EL1);
  306. if (user)
  307. val |= user_bit;
  308. else
  309. val &= ~user_bit;
  310. if (kernel)
  311. val |= kernel_bit;
  312. else
  313. val &= ~kernel_bit;
  314. write_sysreg_s(val, SYS_IMP_APL_PMCR1_EL1);
  315. /*
  316. * Counters 0 and 1 have fixed events. For anything else,
  317. * place the event at the expected location in the relevant
  318. * register (PMESR0 holds the event configuration for counters
  319. * 2-5, resp. PMESR1 for counters 6-9).
  320. */
  321. switch (index) {
  322. case 0 ... 1:
  323. break;
  324. case 2 ... 5:
  325. shift = (index - 2) * 8;
  326. val = read_sysreg_s(SYS_IMP_APL_PMESR0_EL1);
  327. val &= ~((u64)0xff << shift);
  328. val |= (u64)event << shift;
  329. write_sysreg_s(val, SYS_IMP_APL_PMESR0_EL1);
  330. break;
  331. case 6 ... 9:
  332. shift = (index - 6) * 8;
  333. val = read_sysreg_s(SYS_IMP_APL_PMESR1_EL1);
  334. val &= ~((u64)0xff << shift);
  335. val |= (u64)event << shift;
  336. write_sysreg_s(val, SYS_IMP_APL_PMESR1_EL1);
  337. break;
  338. }
  339. }
  340. /* arm_pmu backend */
  341. static void m1_pmu_enable_event(struct perf_event *event)
  342. {
  343. bool user, kernel;
  344. u8 evt;
  345. evt = event->hw.config_base & M1_PMU_CFG_EVENT;
  346. user = event->hw.config_base & M1_PMU_CFG_COUNT_USER;
  347. kernel = event->hw.config_base & M1_PMU_CFG_COUNT_KERNEL;
  348. m1_pmu_disable_counter_interrupt(event->hw.idx);
  349. m1_pmu_disable_counter(event->hw.idx);
  350. isb();
  351. m1_pmu_configure_counter(event->hw.idx, evt, user, kernel);
  352. m1_pmu_enable_counter(event->hw.idx);
  353. m1_pmu_enable_counter_interrupt(event->hw.idx);
  354. isb();
  355. }
  356. static void m1_pmu_disable_event(struct perf_event *event)
  357. {
  358. m1_pmu_disable_counter_interrupt(event->hw.idx);
  359. m1_pmu_disable_counter(event->hw.idx);
  360. isb();
  361. }
  362. static irqreturn_t m1_pmu_handle_irq(struct arm_pmu *cpu_pmu)
  363. {
  364. struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
  365. struct pt_regs *regs;
  366. u64 overflow, state;
  367. int idx;
  368. overflow = read_sysreg_s(SYS_IMP_APL_PMSR_EL1);
  369. if (!overflow) {
  370. /* Spurious interrupt? */
  371. state = read_sysreg_s(SYS_IMP_APL_PMCR0_EL1);
  372. state &= ~PMCR0_IACT;
  373. write_sysreg_s(state, SYS_IMP_APL_PMCR0_EL1);
  374. isb();
  375. return IRQ_NONE;
  376. }
  377. cpu_pmu->stop(cpu_pmu);
  378. regs = get_irq_regs();
  379. for_each_set_bit(idx, cpu_pmu->cntr_mask, M1_PMU_NR_COUNTERS) {
  380. struct perf_event *event = cpuc->events[idx];
  381. struct perf_sample_data data;
  382. if (!event)
  383. continue;
  384. armpmu_event_update(event);
  385. perf_sample_data_init(&data, 0, event->hw.last_period);
  386. if (!armpmu_event_set_period(event))
  387. continue;
  388. if (perf_event_overflow(event, &data, regs))
  389. m1_pmu_disable_event(event);
  390. }
  391. cpu_pmu->start(cpu_pmu);
  392. return IRQ_HANDLED;
  393. }
  394. static u64 m1_pmu_read_counter(struct perf_event *event)
  395. {
  396. return m1_pmu_read_hw_counter(event->hw.idx);
  397. }
  398. static void m1_pmu_write_counter(struct perf_event *event, u64 value)
  399. {
  400. m1_pmu_write_hw_counter(value, event->hw.idx);
  401. isb();
  402. }
  403. static int m1_pmu_get_event_idx(struct pmu_hw_events *cpuc,
  404. struct perf_event *event)
  405. {
  406. unsigned long evtype = event->hw.config_base & M1_PMU_CFG_EVENT;
  407. unsigned long affinity = m1_pmu_event_affinity[evtype];
  408. int idx;
  409. /*
  410. * Place the event on the first free counter that can count
  411. * this event.
  412. *
  413. * We could do a better job if we had a view of all the events
  414. * counting on the PMU at any given time, and by placing the
  415. * most constraining events first.
  416. */
  417. for_each_set_bit(idx, &affinity, M1_PMU_NR_COUNTERS) {
  418. if (!test_and_set_bit(idx, cpuc->used_mask))
  419. return idx;
  420. }
  421. return -EAGAIN;
  422. }
  423. static void m1_pmu_clear_event_idx(struct pmu_hw_events *cpuc,
  424. struct perf_event *event)
  425. {
  426. clear_bit(event->hw.idx, cpuc->used_mask);
  427. }
  428. static void __m1_pmu_set_mode(u8 mode)
  429. {
  430. u64 val;
  431. val = read_sysreg_s(SYS_IMP_APL_PMCR0_EL1);
  432. val &= ~(PMCR0_IMODE | PMCR0_IACT);
  433. val |= FIELD_PREP(PMCR0_IMODE, mode);
  434. write_sysreg_s(val, SYS_IMP_APL_PMCR0_EL1);
  435. isb();
  436. }
  437. static void m1_pmu_start(struct arm_pmu *cpu_pmu)
  438. {
  439. __m1_pmu_set_mode(PMCR0_IMODE_FIQ);
  440. }
  441. static void m1_pmu_stop(struct arm_pmu *cpu_pmu)
  442. {
  443. __m1_pmu_set_mode(PMCR0_IMODE_OFF);
  444. }
  445. static int m1_pmu_map_event(struct perf_event *event)
  446. {
  447. /*
  448. * Although the counters are 48bit wide, bit 47 is what
  449. * triggers the overflow interrupt. Advertise the counters
  450. * being 47bit wide to mimick the behaviour of the ARM PMU.
  451. */
  452. event->hw.flags |= ARMPMU_EVT_47BIT;
  453. return armpmu_map_event(event, &m1_pmu_perf_map, NULL, M1_PMU_CFG_EVENT);
  454. }
  455. static int m2_pmu_map_event(struct perf_event *event)
  456. {
  457. /*
  458. * Same deal as the above, except that M2 has 64bit counters.
  459. * Which, as far as we're concerned, actually means 63 bits.
  460. * Yes, this is getting awkward.
  461. */
  462. event->hw.flags |= ARMPMU_EVT_63BIT;
  463. return armpmu_map_event(event, &m1_pmu_perf_map, NULL, M1_PMU_CFG_EVENT);
  464. }
  465. static void m1_pmu_reset(void *info)
  466. {
  467. int i;
  468. __m1_pmu_set_mode(PMCR0_IMODE_OFF);
  469. for (i = 0; i < M1_PMU_NR_COUNTERS; i++) {
  470. m1_pmu_disable_counter(i);
  471. m1_pmu_disable_counter_interrupt(i);
  472. m1_pmu_write_hw_counter(0, i);
  473. }
  474. isb();
  475. }
  476. static int m1_pmu_set_event_filter(struct hw_perf_event *event,
  477. struct perf_event_attr *attr)
  478. {
  479. unsigned long config_base = 0;
  480. if (!attr->exclude_guest) {
  481. pr_debug("ARM performance counters do not support mode exclusion\n");
  482. return -EOPNOTSUPP;
  483. }
  484. if (!attr->exclude_kernel)
  485. config_base |= M1_PMU_CFG_COUNT_KERNEL;
  486. if (!attr->exclude_user)
  487. config_base |= M1_PMU_CFG_COUNT_USER;
  488. event->config_base = config_base;
  489. return 0;
  490. }
  491. static int m1_pmu_init(struct arm_pmu *cpu_pmu, u32 flags)
  492. {
  493. cpu_pmu->handle_irq = m1_pmu_handle_irq;
  494. cpu_pmu->enable = m1_pmu_enable_event;
  495. cpu_pmu->disable = m1_pmu_disable_event;
  496. cpu_pmu->read_counter = m1_pmu_read_counter;
  497. cpu_pmu->write_counter = m1_pmu_write_counter;
  498. cpu_pmu->get_event_idx = m1_pmu_get_event_idx;
  499. cpu_pmu->clear_event_idx = m1_pmu_clear_event_idx;
  500. cpu_pmu->start = m1_pmu_start;
  501. cpu_pmu->stop = m1_pmu_stop;
  502. if (flags & ARMPMU_EVT_47BIT)
  503. cpu_pmu->map_event = m1_pmu_map_event;
  504. else if (flags & ARMPMU_EVT_63BIT)
  505. cpu_pmu->map_event = m2_pmu_map_event;
  506. else
  507. return WARN_ON(-EINVAL);
  508. cpu_pmu->reset = m1_pmu_reset;
  509. cpu_pmu->set_event_filter = m1_pmu_set_event_filter;
  510. bitmap_set(cpu_pmu->cntr_mask, 0, M1_PMU_NR_COUNTERS);
  511. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] = &m1_pmu_events_attr_group;
  512. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] = &m1_pmu_format_attr_group;
  513. return 0;
  514. }
  515. /* Device driver gunk */
  516. static int m1_pmu_ice_init(struct arm_pmu *cpu_pmu)
  517. {
  518. cpu_pmu->name = "apple_icestorm_pmu";
  519. return m1_pmu_init(cpu_pmu, ARMPMU_EVT_47BIT);
  520. }
  521. static int m1_pmu_fire_init(struct arm_pmu *cpu_pmu)
  522. {
  523. cpu_pmu->name = "apple_firestorm_pmu";
  524. return m1_pmu_init(cpu_pmu, ARMPMU_EVT_47BIT);
  525. }
  526. static int m2_pmu_avalanche_init(struct arm_pmu *cpu_pmu)
  527. {
  528. cpu_pmu->name = "apple_avalanche_pmu";
  529. return m1_pmu_init(cpu_pmu, ARMPMU_EVT_63BIT);
  530. }
  531. static int m2_pmu_blizzard_init(struct arm_pmu *cpu_pmu)
  532. {
  533. cpu_pmu->name = "apple_blizzard_pmu";
  534. return m1_pmu_init(cpu_pmu, ARMPMU_EVT_63BIT);
  535. }
  536. static const struct of_device_id m1_pmu_of_device_ids[] = {
  537. { .compatible = "apple,avalanche-pmu", .data = m2_pmu_avalanche_init, },
  538. { .compatible = "apple,blizzard-pmu", .data = m2_pmu_blizzard_init, },
  539. { .compatible = "apple,icestorm-pmu", .data = m1_pmu_ice_init, },
  540. { .compatible = "apple,firestorm-pmu", .data = m1_pmu_fire_init, },
  541. { },
  542. };
  543. MODULE_DEVICE_TABLE(of, m1_pmu_of_device_ids);
  544. static int m1_pmu_device_probe(struct platform_device *pdev)
  545. {
  546. return arm_pmu_device_probe(pdev, m1_pmu_of_device_ids, NULL);
  547. }
  548. static struct platform_driver m1_pmu_driver = {
  549. .driver = {
  550. .name = "apple-m1-cpu-pmu",
  551. .of_match_table = m1_pmu_of_device_ids,
  552. .suppress_bind_attrs = true,
  553. },
  554. .probe = m1_pmu_device_probe,
  555. };
  556. module_platform_driver(m1_pmu_driver);