apple_m1_cpu_pmu.c 20 KB

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