qcom_l3_pmu.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. * Driver for the L3 cache PMUs in Qualcomm Technologies chips.
  3. *
  4. * The driver supports a distributed cache architecture where the overall
  5. * cache for a socket is comprised of multiple slices each with its own PMU.
  6. * Access to each individual PMU is provided even though all CPUs share all
  7. * the slices. User space needs to aggregate to individual counts to provide
  8. * a global picture.
  9. *
  10. * See Documentation/perf/qcom_l3_pmu.txt for more details.
  11. *
  12. * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 and
  16. * only version 2 as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include <linux/acpi.h>
  24. #include <linux/bitops.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/io.h>
  27. #include <linux/list.h>
  28. #include <linux/module.h>
  29. #include <linux/perf_event.h>
  30. #include <linux/platform_device.h>
  31. /*
  32. * General constants
  33. */
  34. /* Number of counters on each PMU */
  35. #define L3_NUM_COUNTERS 8
  36. /* Mask for the event type field within perf_event_attr.config and EVTYPE reg */
  37. #define L3_EVTYPE_MASK 0xFF
  38. /*
  39. * Bit position of the 'long counter' flag within perf_event_attr.config.
  40. * Reserve some space between the event type and this flag to allow expansion
  41. * in the event type field.
  42. */
  43. #define L3_EVENT_LC_BIT 32
  44. /*
  45. * Register offsets
  46. */
  47. /* Perfmon registers */
  48. #define L3_HML3_PM_CR 0x000
  49. #define L3_HML3_PM_EVCNTR(__cntr) (0x420 + ((__cntr) & 0x7) * 8)
  50. #define L3_HML3_PM_CNTCTL(__cntr) (0x120 + ((__cntr) & 0x7) * 8)
  51. #define L3_HML3_PM_EVTYPE(__cntr) (0x220 + ((__cntr) & 0x7) * 8)
  52. #define L3_HML3_PM_FILTRA 0x300
  53. #define L3_HML3_PM_FILTRB 0x308
  54. #define L3_HML3_PM_FILTRC 0x310
  55. #define L3_HML3_PM_FILTRAM 0x304
  56. #define L3_HML3_PM_FILTRBM 0x30C
  57. #define L3_HML3_PM_FILTRCM 0x314
  58. /* Basic counter registers */
  59. #define L3_M_BC_CR 0x500
  60. #define L3_M_BC_SATROLL_CR 0x504
  61. #define L3_M_BC_CNTENSET 0x508
  62. #define L3_M_BC_CNTENCLR 0x50C
  63. #define L3_M_BC_INTENSET 0x510
  64. #define L3_M_BC_INTENCLR 0x514
  65. #define L3_M_BC_GANG 0x718
  66. #define L3_M_BC_OVSR 0x740
  67. #define L3_M_BC_IRQCTL 0x96C
  68. /*
  69. * Bit field definitions
  70. */
  71. /* L3_HML3_PM_CR */
  72. #define PM_CR_RESET (0)
  73. /* L3_HML3_PM_XCNTCTL/L3_HML3_PM_CNTCTLx */
  74. #define PMCNT_RESET (0)
  75. /* L3_HML3_PM_EVTYPEx */
  76. #define EVSEL(__val) ((__val) & L3_EVTYPE_MASK)
  77. /* Reset value for all the filter registers */
  78. #define PM_FLTR_RESET (0)
  79. /* L3_M_BC_CR */
  80. #define BC_RESET (1UL << 1)
  81. #define BC_ENABLE (1UL << 0)
  82. /* L3_M_BC_SATROLL_CR */
  83. #define BC_SATROLL_CR_RESET (0)
  84. /* L3_M_BC_CNTENSET */
  85. #define PMCNTENSET(__cntr) (1UL << ((__cntr) & 0x7))
  86. /* L3_M_BC_CNTENCLR */
  87. #define PMCNTENCLR(__cntr) (1UL << ((__cntr) & 0x7))
  88. #define BC_CNTENCLR_RESET (0xFF)
  89. /* L3_M_BC_INTENSET */
  90. #define PMINTENSET(__cntr) (1UL << ((__cntr) & 0x7))
  91. /* L3_M_BC_INTENCLR */
  92. #define PMINTENCLR(__cntr) (1UL << ((__cntr) & 0x7))
  93. #define BC_INTENCLR_RESET (0xFF)
  94. /* L3_M_BC_GANG */
  95. #define GANG_EN(__cntr) (1UL << ((__cntr) & 0x7))
  96. #define BC_GANG_RESET (0)
  97. /* L3_M_BC_OVSR */
  98. #define PMOVSRCLR(__cntr) (1UL << ((__cntr) & 0x7))
  99. #define PMOVSRCLR_RESET (0xFF)
  100. /* L3_M_BC_IRQCTL */
  101. #define PMIRQONMSBEN(__cntr) (1UL << ((__cntr) & 0x7))
  102. #define BC_IRQCTL_RESET (0x0)
  103. /*
  104. * Events
  105. */
  106. #define L3_EVENT_CYCLES 0x01
  107. #define L3_EVENT_READ_HIT 0x20
  108. #define L3_EVENT_READ_MISS 0x21
  109. #define L3_EVENT_READ_HIT_D 0x22
  110. #define L3_EVENT_READ_MISS_D 0x23
  111. #define L3_EVENT_WRITE_HIT 0x24
  112. #define L3_EVENT_WRITE_MISS 0x25
  113. /*
  114. * Decoding of settings from perf_event_attr
  115. *
  116. * The config format for perf events is:
  117. * - config: bits 0-7: event type
  118. * bit 32: HW counter size requested, 0: 32 bits, 1: 64 bits
  119. */
  120. static inline u32 get_event_type(struct perf_event *event)
  121. {
  122. return (event->attr.config) & L3_EVTYPE_MASK;
  123. }
  124. static inline bool event_uses_long_counter(struct perf_event *event)
  125. {
  126. return !!(event->attr.config & BIT_ULL(L3_EVENT_LC_BIT));
  127. }
  128. static inline int event_num_counters(struct perf_event *event)
  129. {
  130. return event_uses_long_counter(event) ? 2 : 1;
  131. }
  132. /*
  133. * Main PMU, inherits from the core perf PMU type
  134. */
  135. struct l3cache_pmu {
  136. struct pmu pmu;
  137. struct hlist_node node;
  138. void __iomem *regs;
  139. struct perf_event *events[L3_NUM_COUNTERS];
  140. unsigned long used_mask[BITS_TO_LONGS(L3_NUM_COUNTERS)];
  141. cpumask_t cpumask;
  142. };
  143. #define to_l3cache_pmu(p) (container_of(p, struct l3cache_pmu, pmu))
  144. /*
  145. * Type used to group hardware counter operations
  146. *
  147. * Used to implement two types of hardware counters, standard (32bits) and
  148. * long (64bits). The hardware supports counter chaining which we use to
  149. * implement long counters. This support is exposed via the 'lc' flag field
  150. * in perf_event_attr.config.
  151. */
  152. struct l3cache_event_ops {
  153. /* Called to start event monitoring */
  154. void (*start)(struct perf_event *event);
  155. /* Called to stop event monitoring */
  156. void (*stop)(struct perf_event *event, int flags);
  157. /* Called to update the perf_event */
  158. void (*update)(struct perf_event *event);
  159. };
  160. /*
  161. * Implementation of long counter operations
  162. *
  163. * 64bit counters are implemented by chaining two of the 32bit physical
  164. * counters. The PMU only supports chaining of adjacent even/odd pairs
  165. * and for simplicity the driver always configures the odd counter to
  166. * count the overflows of the lower-numbered even counter. Note that since
  167. * the resulting hardware counter is 64bits no IRQs are required to maintain
  168. * the software counter which is also 64bits.
  169. */
  170. static void qcom_l3_cache__64bit_counter_start(struct perf_event *event)
  171. {
  172. struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
  173. int idx = event->hw.idx;
  174. u32 evsel = get_event_type(event);
  175. u32 gang;
  176. /* Set the odd counter to count the overflows of the even counter */
  177. gang = readl_relaxed(l3pmu->regs + L3_M_BC_GANG);
  178. gang |= GANG_EN(idx + 1);
  179. writel_relaxed(gang, l3pmu->regs + L3_M_BC_GANG);
  180. /* Initialize the hardware counters and reset prev_count*/
  181. local64_set(&event->hw.prev_count, 0);
  182. writel_relaxed(0, l3pmu->regs + L3_HML3_PM_EVCNTR(idx + 1));
  183. writel_relaxed(0, l3pmu->regs + L3_HML3_PM_EVCNTR(idx));
  184. /*
  185. * Set the event types, the upper half must use zero and the lower
  186. * half the actual event type
  187. */
  188. writel_relaxed(EVSEL(0), l3pmu->regs + L3_HML3_PM_EVTYPE(idx + 1));
  189. writel_relaxed(EVSEL(evsel), l3pmu->regs + L3_HML3_PM_EVTYPE(idx));
  190. /* Finally, enable the counters */
  191. writel_relaxed(PMCNT_RESET, l3pmu->regs + L3_HML3_PM_CNTCTL(idx + 1));
  192. writel_relaxed(PMCNTENSET(idx + 1), l3pmu->regs + L3_M_BC_CNTENSET);
  193. writel_relaxed(PMCNT_RESET, l3pmu->regs + L3_HML3_PM_CNTCTL(idx));
  194. writel_relaxed(PMCNTENSET(idx), l3pmu->regs + L3_M_BC_CNTENSET);
  195. }
  196. static void qcom_l3_cache__64bit_counter_stop(struct perf_event *event,
  197. int flags)
  198. {
  199. struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
  200. int idx = event->hw.idx;
  201. u32 gang = readl_relaxed(l3pmu->regs + L3_M_BC_GANG);
  202. /* Disable the counters */
  203. writel_relaxed(PMCNTENCLR(idx), l3pmu->regs + L3_M_BC_CNTENCLR);
  204. writel_relaxed(PMCNTENCLR(idx + 1), l3pmu->regs + L3_M_BC_CNTENCLR);
  205. /* Disable chaining */
  206. writel_relaxed(gang & ~GANG_EN(idx + 1), l3pmu->regs + L3_M_BC_GANG);
  207. }
  208. static void qcom_l3_cache__64bit_counter_update(struct perf_event *event)
  209. {
  210. struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
  211. int idx = event->hw.idx;
  212. u32 hi, lo;
  213. u64 prev, new;
  214. do {
  215. prev = local64_read(&event->hw.prev_count);
  216. do {
  217. hi = readl_relaxed(l3pmu->regs + L3_HML3_PM_EVCNTR(idx + 1));
  218. lo = readl_relaxed(l3pmu->regs + L3_HML3_PM_EVCNTR(idx));
  219. } while (hi != readl_relaxed(l3pmu->regs + L3_HML3_PM_EVCNTR(idx + 1)));
  220. new = ((u64)hi << 32) | lo;
  221. } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
  222. local64_add(new - prev, &event->count);
  223. }
  224. static const struct l3cache_event_ops event_ops_long = {
  225. .start = qcom_l3_cache__64bit_counter_start,
  226. .stop = qcom_l3_cache__64bit_counter_stop,
  227. .update = qcom_l3_cache__64bit_counter_update,
  228. };
  229. /*
  230. * Implementation of standard counter operations
  231. *
  232. * 32bit counters use a single physical counter and a hardware feature that
  233. * asserts the overflow IRQ on the toggling of the most significant bit in
  234. * the counter. This feature allows the counters to be left free-running
  235. * without needing the usual reprogramming required to properly handle races
  236. * during concurrent calls to update.
  237. */
  238. static void qcom_l3_cache__32bit_counter_start(struct perf_event *event)
  239. {
  240. struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
  241. int idx = event->hw.idx;
  242. u32 evsel = get_event_type(event);
  243. u32 irqctl = readl_relaxed(l3pmu->regs + L3_M_BC_IRQCTL);
  244. /* Set the counter to assert the overflow IRQ on MSB toggling */
  245. writel_relaxed(irqctl | PMIRQONMSBEN(idx), l3pmu->regs + L3_M_BC_IRQCTL);
  246. /* Initialize the hardware counter and reset prev_count*/
  247. local64_set(&event->hw.prev_count, 0);
  248. writel_relaxed(0, l3pmu->regs + L3_HML3_PM_EVCNTR(idx));
  249. /* Set the event type */
  250. writel_relaxed(EVSEL(evsel), l3pmu->regs + L3_HML3_PM_EVTYPE(idx));
  251. /* Enable interrupt generation by this counter */
  252. writel_relaxed(PMINTENSET(idx), l3pmu->regs + L3_M_BC_INTENSET);
  253. /* Finally, enable the counter */
  254. writel_relaxed(PMCNT_RESET, l3pmu->regs + L3_HML3_PM_CNTCTL(idx));
  255. writel_relaxed(PMCNTENSET(idx), l3pmu->regs + L3_M_BC_CNTENSET);
  256. }
  257. static void qcom_l3_cache__32bit_counter_stop(struct perf_event *event,
  258. int flags)
  259. {
  260. struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
  261. int idx = event->hw.idx;
  262. u32 irqctl = readl_relaxed(l3pmu->regs + L3_M_BC_IRQCTL);
  263. /* Disable the counter */
  264. writel_relaxed(PMCNTENCLR(idx), l3pmu->regs + L3_M_BC_CNTENCLR);
  265. /* Disable interrupt generation by this counter */
  266. writel_relaxed(PMINTENCLR(idx), l3pmu->regs + L3_M_BC_INTENCLR);
  267. /* Set the counter to not assert the overflow IRQ on MSB toggling */
  268. writel_relaxed(irqctl & ~PMIRQONMSBEN(idx), l3pmu->regs + L3_M_BC_IRQCTL);
  269. }
  270. static void qcom_l3_cache__32bit_counter_update(struct perf_event *event)
  271. {
  272. struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
  273. int idx = event->hw.idx;
  274. u32 prev, new;
  275. do {
  276. prev = local64_read(&event->hw.prev_count);
  277. new = readl_relaxed(l3pmu->regs + L3_HML3_PM_EVCNTR(idx));
  278. } while (local64_cmpxchg(&event->hw.prev_count, prev, new) != prev);
  279. local64_add(new - prev, &event->count);
  280. }
  281. static const struct l3cache_event_ops event_ops_std = {
  282. .start = qcom_l3_cache__32bit_counter_start,
  283. .stop = qcom_l3_cache__32bit_counter_stop,
  284. .update = qcom_l3_cache__32bit_counter_update,
  285. };
  286. /* Retrieve the appropriate operations for the given event */
  287. static
  288. const struct l3cache_event_ops *l3cache_event_get_ops(struct perf_event *event)
  289. {
  290. if (event_uses_long_counter(event))
  291. return &event_ops_long;
  292. else
  293. return &event_ops_std;
  294. }
  295. /*
  296. * Top level PMU functions.
  297. */
  298. static inline void qcom_l3_cache__init(struct l3cache_pmu *l3pmu)
  299. {
  300. int i;
  301. writel_relaxed(BC_RESET, l3pmu->regs + L3_M_BC_CR);
  302. /*
  303. * Use writel for the first programming command to ensure the basic
  304. * counter unit is stopped before proceeding
  305. */
  306. writel(BC_SATROLL_CR_RESET, l3pmu->regs + L3_M_BC_SATROLL_CR);
  307. writel_relaxed(BC_CNTENCLR_RESET, l3pmu->regs + L3_M_BC_CNTENCLR);
  308. writel_relaxed(BC_INTENCLR_RESET, l3pmu->regs + L3_M_BC_INTENCLR);
  309. writel_relaxed(PMOVSRCLR_RESET, l3pmu->regs + L3_M_BC_OVSR);
  310. writel_relaxed(BC_GANG_RESET, l3pmu->regs + L3_M_BC_GANG);
  311. writel_relaxed(BC_IRQCTL_RESET, l3pmu->regs + L3_M_BC_IRQCTL);
  312. writel_relaxed(PM_CR_RESET, l3pmu->regs + L3_HML3_PM_CR);
  313. for (i = 0; i < L3_NUM_COUNTERS; ++i) {
  314. writel_relaxed(PMCNT_RESET, l3pmu->regs + L3_HML3_PM_CNTCTL(i));
  315. writel_relaxed(EVSEL(0), l3pmu->regs + L3_HML3_PM_EVTYPE(i));
  316. }
  317. writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRA);
  318. writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRAM);
  319. writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRB);
  320. writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRBM);
  321. writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRC);
  322. writel_relaxed(PM_FLTR_RESET, l3pmu->regs + L3_HML3_PM_FILTRCM);
  323. /*
  324. * Use writel here to ensure all programming commands are done
  325. * before proceeding
  326. */
  327. writel(BC_ENABLE, l3pmu->regs + L3_M_BC_CR);
  328. }
  329. static irqreturn_t qcom_l3_cache__handle_irq(int irq_num, void *data)
  330. {
  331. struct l3cache_pmu *l3pmu = data;
  332. /* Read the overflow status register */
  333. long status = readl_relaxed(l3pmu->regs + L3_M_BC_OVSR);
  334. int idx;
  335. if (status == 0)
  336. return IRQ_NONE;
  337. /* Clear the bits we read on the overflow status register */
  338. writel_relaxed(status, l3pmu->regs + L3_M_BC_OVSR);
  339. for_each_set_bit(idx, &status, L3_NUM_COUNTERS) {
  340. struct perf_event *event;
  341. const struct l3cache_event_ops *ops;
  342. event = l3pmu->events[idx];
  343. if (!event)
  344. continue;
  345. /*
  346. * Since the IRQ is not enabled for events using long counters
  347. * we should never see one of those here, however, be consistent
  348. * and use the ops indirections like in the other operations.
  349. */
  350. ops = l3cache_event_get_ops(event);
  351. ops->update(event);
  352. }
  353. return IRQ_HANDLED;
  354. }
  355. /*
  356. * Implementation of abstract pmu functionality required by
  357. * the core perf events code.
  358. */
  359. static void qcom_l3_cache__pmu_enable(struct pmu *pmu)
  360. {
  361. struct l3cache_pmu *l3pmu = to_l3cache_pmu(pmu);
  362. /* Ensure the other programming commands are observed before enabling */
  363. wmb();
  364. writel_relaxed(BC_ENABLE, l3pmu->regs + L3_M_BC_CR);
  365. }
  366. static void qcom_l3_cache__pmu_disable(struct pmu *pmu)
  367. {
  368. struct l3cache_pmu *l3pmu = to_l3cache_pmu(pmu);
  369. writel_relaxed(0, l3pmu->regs + L3_M_BC_CR);
  370. /* Ensure the basic counter unit is stopped before proceeding */
  371. wmb();
  372. }
  373. /*
  374. * We must NOT create groups containing events from multiple hardware PMUs,
  375. * although mixing different software and hardware PMUs is allowed.
  376. */
  377. static bool qcom_l3_cache__validate_event_group(struct perf_event *event)
  378. {
  379. struct perf_event *leader = event->group_leader;
  380. struct perf_event *sibling;
  381. int counters = 0;
  382. if (leader->pmu != event->pmu && !is_software_event(leader))
  383. return false;
  384. counters = event_num_counters(event);
  385. counters += event_num_counters(leader);
  386. for_each_sibling_event(sibling, leader) {
  387. if (is_software_event(sibling))
  388. continue;
  389. if (sibling->pmu != event->pmu)
  390. return false;
  391. counters += event_num_counters(sibling);
  392. }
  393. /*
  394. * If the group requires more counters than the HW has, it
  395. * cannot ever be scheduled.
  396. */
  397. return counters <= L3_NUM_COUNTERS;
  398. }
  399. static int qcom_l3_cache__event_init(struct perf_event *event)
  400. {
  401. struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
  402. struct hw_perf_event *hwc = &event->hw;
  403. /*
  404. * Is the event for this PMU?
  405. */
  406. if (event->attr.type != event->pmu->type)
  407. return -ENOENT;
  408. /*
  409. * There are no per-counter mode filters in the PMU.
  410. */
  411. if (event->attr.exclude_user || event->attr.exclude_kernel ||
  412. event->attr.exclude_hv || event->attr.exclude_idle)
  413. return -EINVAL;
  414. /*
  415. * Sampling not supported since these events are not core-attributable.
  416. */
  417. if (hwc->sample_period)
  418. return -EINVAL;
  419. /*
  420. * Task mode not available, we run the counters as socket counters,
  421. * not attributable to any CPU and therefore cannot attribute per-task.
  422. */
  423. if (event->cpu < 0)
  424. return -EINVAL;
  425. /* Validate the group */
  426. if (!qcom_l3_cache__validate_event_group(event))
  427. return -EINVAL;
  428. hwc->idx = -1;
  429. /*
  430. * Many perf core operations (eg. events rotation) operate on a
  431. * single CPU context. This is obvious for CPU PMUs, where one
  432. * expects the same sets of events being observed on all CPUs,
  433. * but can lead to issues for off-core PMUs, like this one, where
  434. * each event could be theoretically assigned to a different CPU.
  435. * To mitigate this, we enforce CPU assignment to one designated
  436. * processor (the one described in the "cpumask" attribute exported
  437. * by the PMU device). perf user space tools honor this and avoid
  438. * opening more than one copy of the events.
  439. */
  440. event->cpu = cpumask_first(&l3pmu->cpumask);
  441. return 0;
  442. }
  443. static void qcom_l3_cache__event_start(struct perf_event *event, int flags)
  444. {
  445. struct hw_perf_event *hwc = &event->hw;
  446. const struct l3cache_event_ops *ops = l3cache_event_get_ops(event);
  447. hwc->state = 0;
  448. ops->start(event);
  449. }
  450. static void qcom_l3_cache__event_stop(struct perf_event *event, int flags)
  451. {
  452. struct hw_perf_event *hwc = &event->hw;
  453. const struct l3cache_event_ops *ops = l3cache_event_get_ops(event);
  454. if (hwc->state & PERF_HES_STOPPED)
  455. return;
  456. ops->stop(event, flags);
  457. if (flags & PERF_EF_UPDATE)
  458. ops->update(event);
  459. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  460. }
  461. static int qcom_l3_cache__event_add(struct perf_event *event, int flags)
  462. {
  463. struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
  464. struct hw_perf_event *hwc = &event->hw;
  465. int order = event_uses_long_counter(event) ? 1 : 0;
  466. int idx;
  467. /*
  468. * Try to allocate a counter.
  469. */
  470. idx = bitmap_find_free_region(l3pmu->used_mask, L3_NUM_COUNTERS, order);
  471. if (idx < 0)
  472. /* The counters are all in use. */
  473. return -EAGAIN;
  474. hwc->idx = idx;
  475. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  476. l3pmu->events[idx] = event;
  477. if (flags & PERF_EF_START)
  478. qcom_l3_cache__event_start(event, 0);
  479. /* Propagate changes to the userspace mapping. */
  480. perf_event_update_userpage(event);
  481. return 0;
  482. }
  483. static void qcom_l3_cache__event_del(struct perf_event *event, int flags)
  484. {
  485. struct l3cache_pmu *l3pmu = to_l3cache_pmu(event->pmu);
  486. struct hw_perf_event *hwc = &event->hw;
  487. int order = event_uses_long_counter(event) ? 1 : 0;
  488. /* Stop and clean up */
  489. qcom_l3_cache__event_stop(event, flags | PERF_EF_UPDATE);
  490. l3pmu->events[hwc->idx] = NULL;
  491. bitmap_release_region(l3pmu->used_mask, hwc->idx, order);
  492. /* Propagate changes to the userspace mapping. */
  493. perf_event_update_userpage(event);
  494. }
  495. static void qcom_l3_cache__event_read(struct perf_event *event)
  496. {
  497. const struct l3cache_event_ops *ops = l3cache_event_get_ops(event);
  498. ops->update(event);
  499. }
  500. /*
  501. * Add sysfs attributes
  502. *
  503. * We export:
  504. * - formats, used by perf user space and other tools to configure events
  505. * - events, used by perf user space and other tools to create events
  506. * symbolically, e.g.:
  507. * perf stat -a -e l3cache_0_0/event=read-miss/ ls
  508. * perf stat -a -e l3cache_0_0/event=0x21/ ls
  509. * - cpumask, used by perf user space and other tools to know on which CPUs
  510. * to open the events
  511. */
  512. /* formats */
  513. static ssize_t l3cache_pmu_format_show(struct device *dev,
  514. struct device_attribute *attr, char *buf)
  515. {
  516. struct dev_ext_attribute *eattr;
  517. eattr = container_of(attr, struct dev_ext_attribute, attr);
  518. return sprintf(buf, "%s\n", (char *) eattr->var);
  519. }
  520. #define L3CACHE_PMU_FORMAT_ATTR(_name, _config) \
  521. (&((struct dev_ext_attribute[]) { \
  522. { .attr = __ATTR(_name, 0444, l3cache_pmu_format_show, NULL), \
  523. .var = (void *) _config, } \
  524. })[0].attr.attr)
  525. static struct attribute *qcom_l3_cache_pmu_formats[] = {
  526. L3CACHE_PMU_FORMAT_ATTR(event, "config:0-7"),
  527. L3CACHE_PMU_FORMAT_ATTR(lc, "config:" __stringify(L3_EVENT_LC_BIT)),
  528. NULL,
  529. };
  530. static struct attribute_group qcom_l3_cache_pmu_format_group = {
  531. .name = "format",
  532. .attrs = qcom_l3_cache_pmu_formats,
  533. };
  534. /* events */
  535. static ssize_t l3cache_pmu_event_show(struct device *dev,
  536. struct device_attribute *attr, char *page)
  537. {
  538. struct perf_pmu_events_attr *pmu_attr;
  539. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
  540. return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
  541. }
  542. #define L3CACHE_EVENT_ATTR(_name, _id) \
  543. (&((struct perf_pmu_events_attr[]) { \
  544. { .attr = __ATTR(_name, 0444, l3cache_pmu_event_show, NULL), \
  545. .id = _id, } \
  546. })[0].attr.attr)
  547. static struct attribute *qcom_l3_cache_pmu_events[] = {
  548. L3CACHE_EVENT_ATTR(cycles, L3_EVENT_CYCLES),
  549. L3CACHE_EVENT_ATTR(read-hit, L3_EVENT_READ_HIT),
  550. L3CACHE_EVENT_ATTR(read-miss, L3_EVENT_READ_MISS),
  551. L3CACHE_EVENT_ATTR(read-hit-d-side, L3_EVENT_READ_HIT_D),
  552. L3CACHE_EVENT_ATTR(read-miss-d-side, L3_EVENT_READ_MISS_D),
  553. L3CACHE_EVENT_ATTR(write-hit, L3_EVENT_WRITE_HIT),
  554. L3CACHE_EVENT_ATTR(write-miss, L3_EVENT_WRITE_MISS),
  555. NULL
  556. };
  557. static struct attribute_group qcom_l3_cache_pmu_events_group = {
  558. .name = "events",
  559. .attrs = qcom_l3_cache_pmu_events,
  560. };
  561. /* cpumask */
  562. static ssize_t qcom_l3_cache_pmu_cpumask_show(struct device *dev,
  563. struct device_attribute *attr, char *buf)
  564. {
  565. struct l3cache_pmu *l3pmu = to_l3cache_pmu(dev_get_drvdata(dev));
  566. return cpumap_print_to_pagebuf(true, buf, &l3pmu->cpumask);
  567. }
  568. static DEVICE_ATTR(cpumask, 0444, qcom_l3_cache_pmu_cpumask_show, NULL);
  569. static struct attribute *qcom_l3_cache_pmu_cpumask_attrs[] = {
  570. &dev_attr_cpumask.attr,
  571. NULL,
  572. };
  573. static struct attribute_group qcom_l3_cache_pmu_cpumask_attr_group = {
  574. .attrs = qcom_l3_cache_pmu_cpumask_attrs,
  575. };
  576. /*
  577. * Per PMU device attribute groups
  578. */
  579. static const struct attribute_group *qcom_l3_cache_pmu_attr_grps[] = {
  580. &qcom_l3_cache_pmu_format_group,
  581. &qcom_l3_cache_pmu_events_group,
  582. &qcom_l3_cache_pmu_cpumask_attr_group,
  583. NULL,
  584. };
  585. /*
  586. * Probing functions and data.
  587. */
  588. static int qcom_l3_cache_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
  589. {
  590. struct l3cache_pmu *l3pmu = hlist_entry_safe(node, struct l3cache_pmu, node);
  591. /* If there is not a CPU/PMU association pick this CPU */
  592. if (cpumask_empty(&l3pmu->cpumask))
  593. cpumask_set_cpu(cpu, &l3pmu->cpumask);
  594. return 0;
  595. }
  596. static int qcom_l3_cache_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
  597. {
  598. struct l3cache_pmu *l3pmu = hlist_entry_safe(node, struct l3cache_pmu, node);
  599. unsigned int target;
  600. if (!cpumask_test_and_clear_cpu(cpu, &l3pmu->cpumask))
  601. return 0;
  602. target = cpumask_any_but(cpu_online_mask, cpu);
  603. if (target >= nr_cpu_ids)
  604. return 0;
  605. perf_pmu_migrate_context(&l3pmu->pmu, cpu, target);
  606. cpumask_set_cpu(target, &l3pmu->cpumask);
  607. return 0;
  608. }
  609. static int qcom_l3_cache_pmu_probe(struct platform_device *pdev)
  610. {
  611. struct l3cache_pmu *l3pmu;
  612. struct acpi_device *acpi_dev;
  613. struct resource *memrc;
  614. int ret;
  615. char *name;
  616. /* Initialize the PMU data structures */
  617. acpi_dev = ACPI_COMPANION(&pdev->dev);
  618. if (!acpi_dev)
  619. return -ENODEV;
  620. l3pmu = devm_kzalloc(&pdev->dev, sizeof(*l3pmu), GFP_KERNEL);
  621. name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "l3cache_%s_%s",
  622. acpi_dev->parent->pnp.unique_id, acpi_dev->pnp.unique_id);
  623. if (!l3pmu || !name)
  624. return -ENOMEM;
  625. l3pmu->pmu = (struct pmu) {
  626. .task_ctx_nr = perf_invalid_context,
  627. .pmu_enable = qcom_l3_cache__pmu_enable,
  628. .pmu_disable = qcom_l3_cache__pmu_disable,
  629. .event_init = qcom_l3_cache__event_init,
  630. .add = qcom_l3_cache__event_add,
  631. .del = qcom_l3_cache__event_del,
  632. .start = qcom_l3_cache__event_start,
  633. .stop = qcom_l3_cache__event_stop,
  634. .read = qcom_l3_cache__event_read,
  635. .attr_groups = qcom_l3_cache_pmu_attr_grps,
  636. };
  637. memrc = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  638. l3pmu->regs = devm_ioremap_resource(&pdev->dev, memrc);
  639. if (IS_ERR(l3pmu->regs)) {
  640. dev_err(&pdev->dev, "Can't map PMU @%pa\n", &memrc->start);
  641. return PTR_ERR(l3pmu->regs);
  642. }
  643. qcom_l3_cache__init(l3pmu);
  644. ret = platform_get_irq(pdev, 0);
  645. if (ret <= 0)
  646. return ret;
  647. ret = devm_request_irq(&pdev->dev, ret, qcom_l3_cache__handle_irq, 0,
  648. name, l3pmu);
  649. if (ret) {
  650. dev_err(&pdev->dev, "Request for IRQ failed for slice @%pa\n",
  651. &memrc->start);
  652. return ret;
  653. }
  654. /* Add this instance to the list used by the offline callback */
  655. ret = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_QCOM_L3_ONLINE, &l3pmu->node);
  656. if (ret) {
  657. dev_err(&pdev->dev, "Error %d registering hotplug", ret);
  658. return ret;
  659. }
  660. ret = perf_pmu_register(&l3pmu->pmu, name, -1);
  661. if (ret < 0) {
  662. dev_err(&pdev->dev, "Failed to register L3 cache PMU (%d)\n", ret);
  663. return ret;
  664. }
  665. dev_info(&pdev->dev, "Registered %s, type: %d\n", name, l3pmu->pmu.type);
  666. return 0;
  667. }
  668. static const struct acpi_device_id qcom_l3_cache_pmu_acpi_match[] = {
  669. { "QCOM8081", },
  670. { }
  671. };
  672. MODULE_DEVICE_TABLE(acpi, qcom_l3_cache_pmu_acpi_match);
  673. static struct platform_driver qcom_l3_cache_pmu_driver = {
  674. .driver = {
  675. .name = "qcom-l3cache-pmu",
  676. .acpi_match_table = ACPI_PTR(qcom_l3_cache_pmu_acpi_match),
  677. },
  678. .probe = qcom_l3_cache_pmu_probe,
  679. };
  680. static int __init register_qcom_l3_cache_pmu_driver(void)
  681. {
  682. int ret;
  683. /* Install a hook to update the reader CPU in case it goes offline */
  684. ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_QCOM_L3_ONLINE,
  685. "perf/qcom/l3cache:online",
  686. qcom_l3_cache_pmu_online_cpu,
  687. qcom_l3_cache_pmu_offline_cpu);
  688. if (ret)
  689. return ret;
  690. return platform_driver_register(&qcom_l3_cache_pmu_driver);
  691. }
  692. device_initcall(register_qcom_l3_cache_pmu_driver);