apple_m1_cpu_pmu.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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 <[email protected]>
  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_UNKNOWN_01 = 0x01,
  45. M1_PMU_PERFCTR_CPU_CYCLES = 0x02,
  46. M1_PMU_PERFCTR_INSTRUCTIONS = 0x8c,
  47. M1_PMU_PERFCTR_UNKNOWN_8d = 0x8d,
  48. M1_PMU_PERFCTR_UNKNOWN_8e = 0x8e,
  49. M1_PMU_PERFCTR_UNKNOWN_8f = 0x8f,
  50. M1_PMU_PERFCTR_UNKNOWN_90 = 0x90,
  51. M1_PMU_PERFCTR_UNKNOWN_93 = 0x93,
  52. M1_PMU_PERFCTR_UNKNOWN_94 = 0x94,
  53. M1_PMU_PERFCTR_UNKNOWN_95 = 0x95,
  54. M1_PMU_PERFCTR_UNKNOWN_96 = 0x96,
  55. M1_PMU_PERFCTR_UNKNOWN_97 = 0x97,
  56. M1_PMU_PERFCTR_UNKNOWN_98 = 0x98,
  57. M1_PMU_PERFCTR_UNKNOWN_99 = 0x99,
  58. M1_PMU_PERFCTR_UNKNOWN_9a = 0x9a,
  59. M1_PMU_PERFCTR_UNKNOWN_9b = 0x9b,
  60. M1_PMU_PERFCTR_UNKNOWN_9c = 0x9c,
  61. M1_PMU_PERFCTR_UNKNOWN_9f = 0x9f,
  62. M1_PMU_PERFCTR_UNKNOWN_bf = 0xbf,
  63. M1_PMU_PERFCTR_UNKNOWN_c0 = 0xc0,
  64. M1_PMU_PERFCTR_UNKNOWN_c1 = 0xc1,
  65. M1_PMU_PERFCTR_UNKNOWN_c4 = 0xc4,
  66. M1_PMU_PERFCTR_UNKNOWN_c5 = 0xc5,
  67. M1_PMU_PERFCTR_UNKNOWN_c6 = 0xc6,
  68. M1_PMU_PERFCTR_UNKNOWN_c8 = 0xc8,
  69. M1_PMU_PERFCTR_UNKNOWN_ca = 0xca,
  70. M1_PMU_PERFCTR_UNKNOWN_cb = 0xcb,
  71. M1_PMU_PERFCTR_UNKNOWN_f5 = 0xf5,
  72. M1_PMU_PERFCTR_UNKNOWN_f6 = 0xf6,
  73. M1_PMU_PERFCTR_UNKNOWN_f7 = 0xf7,
  74. M1_PMU_PERFCTR_UNKNOWN_f8 = 0xf8,
  75. M1_PMU_PERFCTR_UNKNOWN_fd = 0xfd,
  76. M1_PMU_PERFCTR_LAST = M1_PMU_CFG_EVENT,
  77. /*
  78. * From this point onwards, these are not actual HW events,
  79. * but attributes that get stored in hw->config_base.
  80. */
  81. M1_PMU_CFG_COUNT_USER = BIT(8),
  82. M1_PMU_CFG_COUNT_KERNEL = BIT(9),
  83. };
  84. /*
  85. * Per-event affinity table. Most events can be installed on counter
  86. * 2-9, but there are a number of exceptions. Note that this table
  87. * has been created experimentally, and I wouldn't be surprised if more
  88. * counters had strange affinities.
  89. */
  90. static const u16 m1_pmu_event_affinity[M1_PMU_PERFCTR_LAST + 1] = {
  91. [0 ... M1_PMU_PERFCTR_LAST] = ANY_BUT_0_1,
  92. [M1_PMU_PERFCTR_UNKNOWN_01] = BIT(7),
  93. [M1_PMU_PERFCTR_CPU_CYCLES] = ANY_BUT_0_1 | BIT(0),
  94. [M1_PMU_PERFCTR_INSTRUCTIONS] = BIT(7) | BIT(1),
  95. [M1_PMU_PERFCTR_UNKNOWN_8d] = ONLY_5_6_7,
  96. [M1_PMU_PERFCTR_UNKNOWN_8e] = ONLY_5_6_7,
  97. [M1_PMU_PERFCTR_UNKNOWN_8f] = ONLY_5_6_7,
  98. [M1_PMU_PERFCTR_UNKNOWN_90] = ONLY_5_6_7,
  99. [M1_PMU_PERFCTR_UNKNOWN_93] = ONLY_5_6_7,
  100. [M1_PMU_PERFCTR_UNKNOWN_94] = ONLY_5_6_7,
  101. [M1_PMU_PERFCTR_UNKNOWN_95] = ONLY_5_6_7,
  102. [M1_PMU_PERFCTR_UNKNOWN_96] = ONLY_5_6_7,
  103. [M1_PMU_PERFCTR_UNKNOWN_97] = BIT(7),
  104. [M1_PMU_PERFCTR_UNKNOWN_98] = ONLY_5_6_7,
  105. [M1_PMU_PERFCTR_UNKNOWN_99] = ONLY_5_6_7,
  106. [M1_PMU_PERFCTR_UNKNOWN_9a] = BIT(7),
  107. [M1_PMU_PERFCTR_UNKNOWN_9b] = ONLY_5_6_7,
  108. [M1_PMU_PERFCTR_UNKNOWN_9c] = ONLY_5_6_7,
  109. [M1_PMU_PERFCTR_UNKNOWN_9f] = BIT(7),
  110. [M1_PMU_PERFCTR_UNKNOWN_bf] = ONLY_5_6_7,
  111. [M1_PMU_PERFCTR_UNKNOWN_c0] = ONLY_5_6_7,
  112. [M1_PMU_PERFCTR_UNKNOWN_c1] = ONLY_5_6_7,
  113. [M1_PMU_PERFCTR_UNKNOWN_c4] = ONLY_5_6_7,
  114. [M1_PMU_PERFCTR_UNKNOWN_c5] = ONLY_5_6_7,
  115. [M1_PMU_PERFCTR_UNKNOWN_c6] = ONLY_5_6_7,
  116. [M1_PMU_PERFCTR_UNKNOWN_c8] = ONLY_5_6_7,
  117. [M1_PMU_PERFCTR_UNKNOWN_ca] = ONLY_5_6_7,
  118. [M1_PMU_PERFCTR_UNKNOWN_cb] = ONLY_5_6_7,
  119. [M1_PMU_PERFCTR_UNKNOWN_f5] = ONLY_2_4_6,
  120. [M1_PMU_PERFCTR_UNKNOWN_f6] = ONLY_2_4_6,
  121. [M1_PMU_PERFCTR_UNKNOWN_f7] = ONLY_2_4_6,
  122. [M1_PMU_PERFCTR_UNKNOWN_f8] = ONLY_2_TO_7,
  123. [M1_PMU_PERFCTR_UNKNOWN_fd] = ONLY_2_4_6,
  124. };
  125. static const unsigned m1_pmu_perf_map[PERF_COUNT_HW_MAX] = {
  126. PERF_MAP_ALL_UNSUPPORTED,
  127. [PERF_COUNT_HW_CPU_CYCLES] = M1_PMU_PERFCTR_CPU_CYCLES,
  128. [PERF_COUNT_HW_INSTRUCTIONS] = M1_PMU_PERFCTR_INSTRUCTIONS,
  129. /* No idea about the rest yet */
  130. };
  131. /* sysfs definitions */
  132. static ssize_t m1_pmu_events_sysfs_show(struct device *dev,
  133. struct device_attribute *attr,
  134. char *page)
  135. {
  136. struct perf_pmu_events_attr *pmu_attr;
  137. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
  138. return sprintf(page, "event=0x%04llx\n", pmu_attr->id);
  139. }
  140. #define M1_PMU_EVENT_ATTR(name, config) \
  141. PMU_EVENT_ATTR_ID(name, m1_pmu_events_sysfs_show, config)
  142. static struct attribute *m1_pmu_event_attrs[] = {
  143. M1_PMU_EVENT_ATTR(cycles, M1_PMU_PERFCTR_CPU_CYCLES),
  144. M1_PMU_EVENT_ATTR(instructions, M1_PMU_PERFCTR_INSTRUCTIONS),
  145. NULL,
  146. };
  147. static const struct attribute_group m1_pmu_events_attr_group = {
  148. .name = "events",
  149. .attrs = m1_pmu_event_attrs,
  150. };
  151. PMU_FORMAT_ATTR(event, "config:0-7");
  152. static struct attribute *m1_pmu_format_attrs[] = {
  153. &format_attr_event.attr,
  154. NULL,
  155. };
  156. static const struct attribute_group m1_pmu_format_attr_group = {
  157. .name = "format",
  158. .attrs = m1_pmu_format_attrs,
  159. };
  160. /* Low level accessors. No synchronisation. */
  161. #define PMU_READ_COUNTER(_idx) \
  162. case _idx: return read_sysreg_s(SYS_IMP_APL_PMC## _idx ##_EL1)
  163. #define PMU_WRITE_COUNTER(_val, _idx) \
  164. case _idx: \
  165. write_sysreg_s(_val, SYS_IMP_APL_PMC## _idx ##_EL1); \
  166. return
  167. static u64 m1_pmu_read_hw_counter(unsigned int index)
  168. {
  169. switch (index) {
  170. PMU_READ_COUNTER(0);
  171. PMU_READ_COUNTER(1);
  172. PMU_READ_COUNTER(2);
  173. PMU_READ_COUNTER(3);
  174. PMU_READ_COUNTER(4);
  175. PMU_READ_COUNTER(5);
  176. PMU_READ_COUNTER(6);
  177. PMU_READ_COUNTER(7);
  178. PMU_READ_COUNTER(8);
  179. PMU_READ_COUNTER(9);
  180. }
  181. BUG();
  182. }
  183. static void m1_pmu_write_hw_counter(u64 val, unsigned int index)
  184. {
  185. switch (index) {
  186. PMU_WRITE_COUNTER(val, 0);
  187. PMU_WRITE_COUNTER(val, 1);
  188. PMU_WRITE_COUNTER(val, 2);
  189. PMU_WRITE_COUNTER(val, 3);
  190. PMU_WRITE_COUNTER(val, 4);
  191. PMU_WRITE_COUNTER(val, 5);
  192. PMU_WRITE_COUNTER(val, 6);
  193. PMU_WRITE_COUNTER(val, 7);
  194. PMU_WRITE_COUNTER(val, 8);
  195. PMU_WRITE_COUNTER(val, 9);
  196. }
  197. BUG();
  198. }
  199. #define get_bit_offset(index, mask) (__ffs(mask) + (index))
  200. static void __m1_pmu_enable_counter(unsigned int index, bool en)
  201. {
  202. u64 val, bit;
  203. switch (index) {
  204. case 0 ... 7:
  205. bit = BIT(get_bit_offset(index, PMCR0_CNT_ENABLE_0_7));
  206. break;
  207. case 8 ... 9:
  208. bit = BIT(get_bit_offset(index - 8, PMCR0_CNT_ENABLE_8_9));
  209. break;
  210. default:
  211. BUG();
  212. }
  213. val = read_sysreg_s(SYS_IMP_APL_PMCR0_EL1);
  214. if (en)
  215. val |= bit;
  216. else
  217. val &= ~bit;
  218. write_sysreg_s(val, SYS_IMP_APL_PMCR0_EL1);
  219. }
  220. static void m1_pmu_enable_counter(unsigned int index)
  221. {
  222. __m1_pmu_enable_counter(index, true);
  223. }
  224. static void m1_pmu_disable_counter(unsigned int index)
  225. {
  226. __m1_pmu_enable_counter(index, false);
  227. }
  228. static void __m1_pmu_enable_counter_interrupt(unsigned int index, bool en)
  229. {
  230. u64 val, bit;
  231. switch (index) {
  232. case 0 ... 7:
  233. bit = BIT(get_bit_offset(index, PMCR0_PMI_ENABLE_0_7));
  234. break;
  235. case 8 ... 9:
  236. bit = BIT(get_bit_offset(index - 8, PMCR0_PMI_ENABLE_8_9));
  237. break;
  238. default:
  239. BUG();
  240. }
  241. val = read_sysreg_s(SYS_IMP_APL_PMCR0_EL1);
  242. if (en)
  243. val |= bit;
  244. else
  245. val &= ~bit;
  246. write_sysreg_s(val, SYS_IMP_APL_PMCR0_EL1);
  247. }
  248. static void m1_pmu_enable_counter_interrupt(unsigned int index)
  249. {
  250. __m1_pmu_enable_counter_interrupt(index, true);
  251. }
  252. static void m1_pmu_disable_counter_interrupt(unsigned int index)
  253. {
  254. __m1_pmu_enable_counter_interrupt(index, false);
  255. }
  256. static void m1_pmu_configure_counter(unsigned int index, u8 event,
  257. bool user, bool kernel)
  258. {
  259. u64 val, user_bit, kernel_bit;
  260. int shift;
  261. switch (index) {
  262. case 0 ... 7:
  263. user_bit = BIT(get_bit_offset(index, PMCR1_COUNT_A64_EL0_0_7));
  264. kernel_bit = BIT(get_bit_offset(index, PMCR1_COUNT_A64_EL1_0_7));
  265. break;
  266. case 8 ... 9:
  267. user_bit = BIT(get_bit_offset(index - 8, PMCR1_COUNT_A64_EL0_8_9));
  268. kernel_bit = BIT(get_bit_offset(index - 8, PMCR1_COUNT_A64_EL1_8_9));
  269. break;
  270. default:
  271. BUG();
  272. }
  273. val = read_sysreg_s(SYS_IMP_APL_PMCR1_EL1);
  274. if (user)
  275. val |= user_bit;
  276. else
  277. val &= ~user_bit;
  278. if (kernel)
  279. val |= kernel_bit;
  280. else
  281. val &= ~kernel_bit;
  282. write_sysreg_s(val, SYS_IMP_APL_PMCR1_EL1);
  283. /*
  284. * Counters 0 and 1 have fixed events. For anything else,
  285. * place the event at the expected location in the relevant
  286. * register (PMESR0 holds the event configuration for counters
  287. * 2-5, resp. PMESR1 for counters 6-9).
  288. */
  289. switch (index) {
  290. case 0 ... 1:
  291. break;
  292. case 2 ... 5:
  293. shift = (index - 2) * 8;
  294. val = read_sysreg_s(SYS_IMP_APL_PMESR0_EL1);
  295. val &= ~((u64)0xff << shift);
  296. val |= (u64)event << shift;
  297. write_sysreg_s(val, SYS_IMP_APL_PMESR0_EL1);
  298. break;
  299. case 6 ... 9:
  300. shift = (index - 6) * 8;
  301. val = read_sysreg_s(SYS_IMP_APL_PMESR1_EL1);
  302. val &= ~((u64)0xff << shift);
  303. val |= (u64)event << shift;
  304. write_sysreg_s(val, SYS_IMP_APL_PMESR1_EL1);
  305. break;
  306. }
  307. }
  308. /* arm_pmu backend */
  309. static void m1_pmu_enable_event(struct perf_event *event)
  310. {
  311. bool user, kernel;
  312. u8 evt;
  313. evt = event->hw.config_base & M1_PMU_CFG_EVENT;
  314. user = event->hw.config_base & M1_PMU_CFG_COUNT_USER;
  315. kernel = event->hw.config_base & M1_PMU_CFG_COUNT_KERNEL;
  316. m1_pmu_disable_counter_interrupt(event->hw.idx);
  317. m1_pmu_disable_counter(event->hw.idx);
  318. isb();
  319. m1_pmu_configure_counter(event->hw.idx, evt, user, kernel);
  320. m1_pmu_enable_counter(event->hw.idx);
  321. m1_pmu_enable_counter_interrupt(event->hw.idx);
  322. isb();
  323. }
  324. static void m1_pmu_disable_event(struct perf_event *event)
  325. {
  326. m1_pmu_disable_counter_interrupt(event->hw.idx);
  327. m1_pmu_disable_counter(event->hw.idx);
  328. isb();
  329. }
  330. static irqreturn_t m1_pmu_handle_irq(struct arm_pmu *cpu_pmu)
  331. {
  332. struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
  333. struct pt_regs *regs;
  334. u64 overflow, state;
  335. int idx;
  336. overflow = read_sysreg_s(SYS_IMP_APL_PMSR_EL1);
  337. if (!overflow) {
  338. /* Spurious interrupt? */
  339. state = read_sysreg_s(SYS_IMP_APL_PMCR0_EL1);
  340. state &= ~PMCR0_IACT;
  341. write_sysreg_s(state, SYS_IMP_APL_PMCR0_EL1);
  342. isb();
  343. return IRQ_NONE;
  344. }
  345. cpu_pmu->stop(cpu_pmu);
  346. regs = get_irq_regs();
  347. for (idx = 0; idx < cpu_pmu->num_events; idx++) {
  348. struct perf_event *event = cpuc->events[idx];
  349. struct perf_sample_data data;
  350. if (!event)
  351. continue;
  352. armpmu_event_update(event);
  353. perf_sample_data_init(&data, 0, event->hw.last_period);
  354. if (!armpmu_event_set_period(event))
  355. continue;
  356. if (perf_event_overflow(event, &data, regs))
  357. m1_pmu_disable_event(event);
  358. }
  359. cpu_pmu->start(cpu_pmu);
  360. return IRQ_HANDLED;
  361. }
  362. static u64 m1_pmu_read_counter(struct perf_event *event)
  363. {
  364. return m1_pmu_read_hw_counter(event->hw.idx);
  365. }
  366. static void m1_pmu_write_counter(struct perf_event *event, u64 value)
  367. {
  368. m1_pmu_write_hw_counter(value, event->hw.idx);
  369. isb();
  370. }
  371. static int m1_pmu_get_event_idx(struct pmu_hw_events *cpuc,
  372. struct perf_event *event)
  373. {
  374. unsigned long evtype = event->hw.config_base & M1_PMU_CFG_EVENT;
  375. unsigned long affinity = m1_pmu_event_affinity[evtype];
  376. int idx;
  377. /*
  378. * Place the event on the first free counter that can count
  379. * this event.
  380. *
  381. * We could do a better job if we had a view of all the events
  382. * counting on the PMU at any given time, and by placing the
  383. * most constraining events first.
  384. */
  385. for_each_set_bit(idx, &affinity, M1_PMU_NR_COUNTERS) {
  386. if (!test_and_set_bit(idx, cpuc->used_mask))
  387. return idx;
  388. }
  389. return -EAGAIN;
  390. }
  391. static void m1_pmu_clear_event_idx(struct pmu_hw_events *cpuc,
  392. struct perf_event *event)
  393. {
  394. clear_bit(event->hw.idx, cpuc->used_mask);
  395. }
  396. static void __m1_pmu_set_mode(u8 mode)
  397. {
  398. u64 val;
  399. val = read_sysreg_s(SYS_IMP_APL_PMCR0_EL1);
  400. val &= ~(PMCR0_IMODE | PMCR0_IACT);
  401. val |= FIELD_PREP(PMCR0_IMODE, mode);
  402. write_sysreg_s(val, SYS_IMP_APL_PMCR0_EL1);
  403. isb();
  404. }
  405. static void m1_pmu_start(struct arm_pmu *cpu_pmu)
  406. {
  407. __m1_pmu_set_mode(PMCR0_IMODE_FIQ);
  408. }
  409. static void m1_pmu_stop(struct arm_pmu *cpu_pmu)
  410. {
  411. __m1_pmu_set_mode(PMCR0_IMODE_OFF);
  412. }
  413. static int m1_pmu_map_event(struct perf_event *event)
  414. {
  415. /*
  416. * Although the counters are 48bit wide, bit 47 is what
  417. * triggers the overflow interrupt. Advertise the counters
  418. * being 47bit wide to mimick the behaviour of the ARM PMU.
  419. */
  420. event->hw.flags |= ARMPMU_EVT_47BIT;
  421. return armpmu_map_event(event, &m1_pmu_perf_map, NULL, M1_PMU_CFG_EVENT);
  422. }
  423. static void m1_pmu_reset(void *info)
  424. {
  425. int i;
  426. __m1_pmu_set_mode(PMCR0_IMODE_OFF);
  427. for (i = 0; i < M1_PMU_NR_COUNTERS; i++) {
  428. m1_pmu_disable_counter(i);
  429. m1_pmu_disable_counter_interrupt(i);
  430. m1_pmu_write_hw_counter(0, i);
  431. }
  432. isb();
  433. }
  434. static int m1_pmu_set_event_filter(struct hw_perf_event *event,
  435. struct perf_event_attr *attr)
  436. {
  437. unsigned long config_base = 0;
  438. if (!attr->exclude_guest)
  439. return -EINVAL;
  440. if (!attr->exclude_kernel)
  441. config_base |= M1_PMU_CFG_COUNT_KERNEL;
  442. if (!attr->exclude_user)
  443. config_base |= M1_PMU_CFG_COUNT_USER;
  444. event->config_base = config_base;
  445. return 0;
  446. }
  447. static int m1_pmu_init(struct arm_pmu *cpu_pmu)
  448. {
  449. cpu_pmu->handle_irq = m1_pmu_handle_irq;
  450. cpu_pmu->enable = m1_pmu_enable_event;
  451. cpu_pmu->disable = m1_pmu_disable_event;
  452. cpu_pmu->read_counter = m1_pmu_read_counter;
  453. cpu_pmu->write_counter = m1_pmu_write_counter;
  454. cpu_pmu->get_event_idx = m1_pmu_get_event_idx;
  455. cpu_pmu->clear_event_idx = m1_pmu_clear_event_idx;
  456. cpu_pmu->start = m1_pmu_start;
  457. cpu_pmu->stop = m1_pmu_stop;
  458. cpu_pmu->map_event = m1_pmu_map_event;
  459. cpu_pmu->reset = m1_pmu_reset;
  460. cpu_pmu->set_event_filter = m1_pmu_set_event_filter;
  461. cpu_pmu->num_events = M1_PMU_NR_COUNTERS;
  462. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_EVENTS] = &m1_pmu_events_attr_group;
  463. cpu_pmu->attr_groups[ARMPMU_ATTR_GROUP_FORMATS] = &m1_pmu_format_attr_group;
  464. return 0;
  465. }
  466. /* Device driver gunk */
  467. static int m1_pmu_ice_init(struct arm_pmu *cpu_pmu)
  468. {
  469. cpu_pmu->name = "apple_icestorm_pmu";
  470. return m1_pmu_init(cpu_pmu);
  471. }
  472. static int m1_pmu_fire_init(struct arm_pmu *cpu_pmu)
  473. {
  474. cpu_pmu->name = "apple_firestorm_pmu";
  475. return m1_pmu_init(cpu_pmu);
  476. }
  477. static const struct of_device_id m1_pmu_of_device_ids[] = {
  478. { .compatible = "apple,icestorm-pmu", .data = m1_pmu_ice_init, },
  479. { .compatible = "apple,firestorm-pmu", .data = m1_pmu_fire_init, },
  480. { },
  481. };
  482. MODULE_DEVICE_TABLE(of, m1_pmu_of_device_ids);
  483. static int m1_pmu_device_probe(struct platform_device *pdev)
  484. {
  485. return arm_pmu_device_probe(pdev, m1_pmu_of_device_ids, NULL);
  486. }
  487. static struct platform_driver m1_pmu_driver = {
  488. .driver = {
  489. .name = "apple-m1-cpu-pmu",
  490. .of_match_table = m1_pmu_of_device_ids,
  491. .suppress_bind_attrs = true,
  492. },
  493. .probe = m1_pmu_device_probe,
  494. };
  495. module_platform_driver(m1_pmu_driver);
  496. MODULE_LICENSE("GPL v2");