qcom_l2_pmu.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /* Copyright (c) 2015-2017 The Linux Foundation. All rights reserved.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License version 2 and
  5. * only version 2 as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. */
  12. #include <linux/acpi.h>
  13. #include <linux/bitops.h>
  14. #include <linux/bug.h>
  15. #include <linux/cpuhotplug.h>
  16. #include <linux/cpumask.h>
  17. #include <linux/device.h>
  18. #include <linux/errno.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/irq.h>
  21. #include <linux/kernel.h>
  22. #include <linux/list.h>
  23. #include <linux/percpu.h>
  24. #include <linux/perf_event.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/smp.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/sysfs.h>
  29. #include <linux/types.h>
  30. #include <asm/barrier.h>
  31. #include <asm/local64.h>
  32. #include <asm/sysreg.h>
  33. #define MAX_L2_CTRS 9
  34. #define L2PMCR_NUM_EV_SHIFT 11
  35. #define L2PMCR_NUM_EV_MASK 0x1F
  36. #define L2PMCR 0x400
  37. #define L2PMCNTENCLR 0x403
  38. #define L2PMCNTENSET 0x404
  39. #define L2PMINTENCLR 0x405
  40. #define L2PMINTENSET 0x406
  41. #define L2PMOVSCLR 0x407
  42. #define L2PMOVSSET 0x408
  43. #define L2PMCCNTCR 0x409
  44. #define L2PMCCNTR 0x40A
  45. #define L2PMCCNTSR 0x40C
  46. #define L2PMRESR 0x410
  47. #define IA_L2PMXEVCNTCR_BASE 0x420
  48. #define IA_L2PMXEVCNTR_BASE 0x421
  49. #define IA_L2PMXEVFILTER_BASE 0x423
  50. #define IA_L2PMXEVTYPER_BASE 0x424
  51. #define IA_L2_REG_OFFSET 0x10
  52. #define L2PMXEVFILTER_SUFILTER_ALL 0x000E0000
  53. #define L2PMXEVFILTER_ORGFILTER_IDINDEP 0x00000004
  54. #define L2PMXEVFILTER_ORGFILTER_ALL 0x00000003
  55. #define L2EVTYPER_REG_SHIFT 3
  56. #define L2PMRESR_GROUP_BITS 8
  57. #define L2PMRESR_GROUP_MASK GENMASK(7, 0)
  58. #define L2CYCLE_CTR_BIT 31
  59. #define L2CYCLE_CTR_RAW_CODE 0xFE
  60. #define L2PMCR_RESET_ALL 0x6
  61. #define L2PMCR_COUNTERS_ENABLE 0x1
  62. #define L2PMCR_COUNTERS_DISABLE 0x0
  63. #define L2PMRESR_EN BIT_ULL(63)
  64. #define L2_EVT_MASK 0x00000FFF
  65. #define L2_EVT_CODE_MASK 0x00000FF0
  66. #define L2_EVT_GRP_MASK 0x0000000F
  67. #define L2_EVT_CODE_SHIFT 4
  68. #define L2_EVT_GRP_SHIFT 0
  69. #define L2_EVT_CODE(event) (((event) & L2_EVT_CODE_MASK) >> L2_EVT_CODE_SHIFT)
  70. #define L2_EVT_GROUP(event) (((event) & L2_EVT_GRP_MASK) >> L2_EVT_GRP_SHIFT)
  71. #define L2_EVT_GROUP_MAX 7
  72. #define L2_COUNTER_RELOAD BIT_ULL(31)
  73. #define L2_CYCLE_COUNTER_RELOAD BIT_ULL(63)
  74. #define L2CPUSRSELR_EL1 sys_reg(3, 3, 15, 0, 6)
  75. #define L2CPUSRDR_EL1 sys_reg(3, 3, 15, 0, 7)
  76. #define reg_idx(reg, i) (((i) * IA_L2_REG_OFFSET) + reg##_BASE)
  77. /*
  78. * Events
  79. */
  80. #define L2_EVENT_CYCLES 0xfe
  81. #define L2_EVENT_DCACHE_OPS 0x400
  82. #define L2_EVENT_ICACHE_OPS 0x401
  83. #define L2_EVENT_TLBI 0x402
  84. #define L2_EVENT_BARRIERS 0x403
  85. #define L2_EVENT_TOTAL_READS 0x405
  86. #define L2_EVENT_TOTAL_WRITES 0x406
  87. #define L2_EVENT_TOTAL_REQUESTS 0x407
  88. #define L2_EVENT_LDREX 0x420
  89. #define L2_EVENT_STREX 0x421
  90. #define L2_EVENT_CLREX 0x422
  91. static DEFINE_RAW_SPINLOCK(l2_access_lock);
  92. /**
  93. * set_l2_indirect_reg: write value to an L2 register
  94. * @reg: Address of L2 register.
  95. * @value: Value to be written to register.
  96. *
  97. * Use architecturally required barriers for ordering between system register
  98. * accesses
  99. */
  100. static void set_l2_indirect_reg(u64 reg, u64 val)
  101. {
  102. unsigned long flags;
  103. raw_spin_lock_irqsave(&l2_access_lock, flags);
  104. write_sysreg_s(reg, L2CPUSRSELR_EL1);
  105. isb();
  106. write_sysreg_s(val, L2CPUSRDR_EL1);
  107. isb();
  108. raw_spin_unlock_irqrestore(&l2_access_lock, flags);
  109. }
  110. /**
  111. * get_l2_indirect_reg: read an L2 register value
  112. * @reg: Address of L2 register.
  113. *
  114. * Use architecturally required barriers for ordering between system register
  115. * accesses
  116. */
  117. static u64 get_l2_indirect_reg(u64 reg)
  118. {
  119. u64 val;
  120. unsigned long flags;
  121. raw_spin_lock_irqsave(&l2_access_lock, flags);
  122. write_sysreg_s(reg, L2CPUSRSELR_EL1);
  123. isb();
  124. val = read_sysreg_s(L2CPUSRDR_EL1);
  125. raw_spin_unlock_irqrestore(&l2_access_lock, flags);
  126. return val;
  127. }
  128. struct cluster_pmu;
  129. /*
  130. * Aggregate PMU. Implements the core pmu functions and manages
  131. * the hardware PMUs.
  132. */
  133. struct l2cache_pmu {
  134. struct hlist_node node;
  135. u32 num_pmus;
  136. struct pmu pmu;
  137. int num_counters;
  138. cpumask_t cpumask;
  139. struct platform_device *pdev;
  140. struct cluster_pmu * __percpu *pmu_cluster;
  141. struct list_head clusters;
  142. };
  143. /*
  144. * The cache is made up of one or more clusters, each cluster has its own PMU.
  145. * Each cluster is associated with one or more CPUs.
  146. * This structure represents one of the hardware PMUs.
  147. *
  148. * Events can be envisioned as a 2-dimensional array. Each column represents
  149. * a group of events. There are 8 groups. Only one entry from each
  150. * group can be in use at a time.
  151. *
  152. * Events are specified as 0xCCG, where CC is 2 hex digits specifying
  153. * the code (array row) and G specifies the group (column).
  154. *
  155. * In addition there is a cycle counter event specified by L2CYCLE_CTR_RAW_CODE
  156. * which is outside the above scheme.
  157. */
  158. struct cluster_pmu {
  159. struct list_head next;
  160. struct perf_event *events[MAX_L2_CTRS];
  161. struct l2cache_pmu *l2cache_pmu;
  162. DECLARE_BITMAP(used_counters, MAX_L2_CTRS);
  163. DECLARE_BITMAP(used_groups, L2_EVT_GROUP_MAX + 1);
  164. int irq;
  165. int cluster_id;
  166. /* The CPU that is used for collecting events on this cluster */
  167. int on_cpu;
  168. /* All the CPUs associated with this cluster */
  169. cpumask_t cluster_cpus;
  170. spinlock_t pmu_lock;
  171. };
  172. #define to_l2cache_pmu(p) (container_of(p, struct l2cache_pmu, pmu))
  173. static u32 l2_cycle_ctr_idx;
  174. static u32 l2_counter_present_mask;
  175. static inline u32 idx_to_reg_bit(u32 idx)
  176. {
  177. if (idx == l2_cycle_ctr_idx)
  178. return BIT(L2CYCLE_CTR_BIT);
  179. return BIT(idx);
  180. }
  181. static inline struct cluster_pmu *get_cluster_pmu(
  182. struct l2cache_pmu *l2cache_pmu, int cpu)
  183. {
  184. return *per_cpu_ptr(l2cache_pmu->pmu_cluster, cpu);
  185. }
  186. static void cluster_pmu_reset(void)
  187. {
  188. /* Reset all counters */
  189. set_l2_indirect_reg(L2PMCR, L2PMCR_RESET_ALL);
  190. set_l2_indirect_reg(L2PMCNTENCLR, l2_counter_present_mask);
  191. set_l2_indirect_reg(L2PMINTENCLR, l2_counter_present_mask);
  192. set_l2_indirect_reg(L2PMOVSCLR, l2_counter_present_mask);
  193. }
  194. static inline void cluster_pmu_enable(void)
  195. {
  196. set_l2_indirect_reg(L2PMCR, L2PMCR_COUNTERS_ENABLE);
  197. }
  198. static inline void cluster_pmu_disable(void)
  199. {
  200. set_l2_indirect_reg(L2PMCR, L2PMCR_COUNTERS_DISABLE);
  201. }
  202. static inline void cluster_pmu_counter_set_value(u32 idx, u64 value)
  203. {
  204. if (idx == l2_cycle_ctr_idx)
  205. set_l2_indirect_reg(L2PMCCNTR, value);
  206. else
  207. set_l2_indirect_reg(reg_idx(IA_L2PMXEVCNTR, idx), value);
  208. }
  209. static inline u64 cluster_pmu_counter_get_value(u32 idx)
  210. {
  211. u64 value;
  212. if (idx == l2_cycle_ctr_idx)
  213. value = get_l2_indirect_reg(L2PMCCNTR);
  214. else
  215. value = get_l2_indirect_reg(reg_idx(IA_L2PMXEVCNTR, idx));
  216. return value;
  217. }
  218. static inline void cluster_pmu_counter_enable(u32 idx)
  219. {
  220. set_l2_indirect_reg(L2PMCNTENSET, idx_to_reg_bit(idx));
  221. }
  222. static inline void cluster_pmu_counter_disable(u32 idx)
  223. {
  224. set_l2_indirect_reg(L2PMCNTENCLR, idx_to_reg_bit(idx));
  225. }
  226. static inline void cluster_pmu_counter_enable_interrupt(u32 idx)
  227. {
  228. set_l2_indirect_reg(L2PMINTENSET, idx_to_reg_bit(idx));
  229. }
  230. static inline void cluster_pmu_counter_disable_interrupt(u32 idx)
  231. {
  232. set_l2_indirect_reg(L2PMINTENCLR, idx_to_reg_bit(idx));
  233. }
  234. static inline void cluster_pmu_set_evccntcr(u32 val)
  235. {
  236. set_l2_indirect_reg(L2PMCCNTCR, val);
  237. }
  238. static inline void cluster_pmu_set_evcntcr(u32 ctr, u32 val)
  239. {
  240. set_l2_indirect_reg(reg_idx(IA_L2PMXEVCNTCR, ctr), val);
  241. }
  242. static inline void cluster_pmu_set_evtyper(u32 ctr, u32 val)
  243. {
  244. set_l2_indirect_reg(reg_idx(IA_L2PMXEVTYPER, ctr), val);
  245. }
  246. static void cluster_pmu_set_resr(struct cluster_pmu *cluster,
  247. u32 event_group, u32 event_cc)
  248. {
  249. u64 field;
  250. u64 resr_val;
  251. u32 shift;
  252. unsigned long flags;
  253. shift = L2PMRESR_GROUP_BITS * event_group;
  254. field = ((u64)(event_cc & L2PMRESR_GROUP_MASK) << shift);
  255. spin_lock_irqsave(&cluster->pmu_lock, flags);
  256. resr_val = get_l2_indirect_reg(L2PMRESR);
  257. resr_val &= ~(L2PMRESR_GROUP_MASK << shift);
  258. resr_val |= field;
  259. resr_val |= L2PMRESR_EN;
  260. set_l2_indirect_reg(L2PMRESR, resr_val);
  261. spin_unlock_irqrestore(&cluster->pmu_lock, flags);
  262. }
  263. /*
  264. * Hardware allows filtering of events based on the originating
  265. * CPU. Turn this off by setting filter bits to allow events from
  266. * all CPUS, subunits and ID independent events in this cluster.
  267. */
  268. static inline void cluster_pmu_set_evfilter_sys_mode(u32 ctr)
  269. {
  270. u32 val = L2PMXEVFILTER_SUFILTER_ALL |
  271. L2PMXEVFILTER_ORGFILTER_IDINDEP |
  272. L2PMXEVFILTER_ORGFILTER_ALL;
  273. set_l2_indirect_reg(reg_idx(IA_L2PMXEVFILTER, ctr), val);
  274. }
  275. static inline u32 cluster_pmu_getreset_ovsr(void)
  276. {
  277. u32 result = get_l2_indirect_reg(L2PMOVSSET);
  278. set_l2_indirect_reg(L2PMOVSCLR, result);
  279. return result;
  280. }
  281. static inline bool cluster_pmu_has_overflowed(u32 ovsr)
  282. {
  283. return !!(ovsr & l2_counter_present_mask);
  284. }
  285. static inline bool cluster_pmu_counter_has_overflowed(u32 ovsr, u32 idx)
  286. {
  287. return !!(ovsr & idx_to_reg_bit(idx));
  288. }
  289. static void l2_cache_event_update(struct perf_event *event)
  290. {
  291. struct hw_perf_event *hwc = &event->hw;
  292. u64 delta, prev, now;
  293. u32 idx = hwc->idx;
  294. do {
  295. prev = local64_read(&hwc->prev_count);
  296. now = cluster_pmu_counter_get_value(idx);
  297. } while (local64_cmpxchg(&hwc->prev_count, prev, now) != prev);
  298. /*
  299. * The cycle counter is 64-bit, but all other counters are
  300. * 32-bit, and we must handle 32-bit overflow explicitly.
  301. */
  302. delta = now - prev;
  303. if (idx != l2_cycle_ctr_idx)
  304. delta &= 0xffffffff;
  305. local64_add(delta, &event->count);
  306. }
  307. static void l2_cache_cluster_set_period(struct cluster_pmu *cluster,
  308. struct hw_perf_event *hwc)
  309. {
  310. u32 idx = hwc->idx;
  311. u64 new;
  312. /*
  313. * We limit the max period to half the max counter value so
  314. * that even in the case of extreme interrupt latency the
  315. * counter will (hopefully) not wrap past its initial value.
  316. */
  317. if (idx == l2_cycle_ctr_idx)
  318. new = L2_CYCLE_COUNTER_RELOAD;
  319. else
  320. new = L2_COUNTER_RELOAD;
  321. local64_set(&hwc->prev_count, new);
  322. cluster_pmu_counter_set_value(idx, new);
  323. }
  324. static int l2_cache_get_event_idx(struct cluster_pmu *cluster,
  325. struct perf_event *event)
  326. {
  327. struct hw_perf_event *hwc = &event->hw;
  328. int idx;
  329. int num_ctrs = cluster->l2cache_pmu->num_counters - 1;
  330. unsigned int group;
  331. if (hwc->config_base == L2CYCLE_CTR_RAW_CODE) {
  332. if (test_and_set_bit(l2_cycle_ctr_idx, cluster->used_counters))
  333. return -EAGAIN;
  334. return l2_cycle_ctr_idx;
  335. }
  336. idx = find_first_zero_bit(cluster->used_counters, num_ctrs);
  337. if (idx == num_ctrs)
  338. /* The counters are all in use. */
  339. return -EAGAIN;
  340. /*
  341. * Check for column exclusion: event column already in use by another
  342. * event. This is for events which are not in the same group.
  343. * Conflicting events in the same group are detected in event_init.
  344. */
  345. group = L2_EVT_GROUP(hwc->config_base);
  346. if (test_bit(group, cluster->used_groups))
  347. return -EAGAIN;
  348. set_bit(idx, cluster->used_counters);
  349. set_bit(group, cluster->used_groups);
  350. return idx;
  351. }
  352. static void l2_cache_clear_event_idx(struct cluster_pmu *cluster,
  353. struct perf_event *event)
  354. {
  355. struct hw_perf_event *hwc = &event->hw;
  356. int idx = hwc->idx;
  357. clear_bit(idx, cluster->used_counters);
  358. if (hwc->config_base != L2CYCLE_CTR_RAW_CODE)
  359. clear_bit(L2_EVT_GROUP(hwc->config_base), cluster->used_groups);
  360. }
  361. static irqreturn_t l2_cache_handle_irq(int irq_num, void *data)
  362. {
  363. struct cluster_pmu *cluster = data;
  364. int num_counters = cluster->l2cache_pmu->num_counters;
  365. u32 ovsr;
  366. int idx;
  367. ovsr = cluster_pmu_getreset_ovsr();
  368. if (!cluster_pmu_has_overflowed(ovsr))
  369. return IRQ_NONE;
  370. for_each_set_bit(idx, cluster->used_counters, num_counters) {
  371. struct perf_event *event = cluster->events[idx];
  372. struct hw_perf_event *hwc;
  373. if (WARN_ON_ONCE(!event))
  374. continue;
  375. if (!cluster_pmu_counter_has_overflowed(ovsr, idx))
  376. continue;
  377. l2_cache_event_update(event);
  378. hwc = &event->hw;
  379. l2_cache_cluster_set_period(cluster, hwc);
  380. }
  381. return IRQ_HANDLED;
  382. }
  383. /*
  384. * Implementation of abstract pmu functionality required by
  385. * the core perf events code.
  386. */
  387. static void l2_cache_pmu_enable(struct pmu *pmu)
  388. {
  389. /*
  390. * Although there is only one PMU (per socket) controlling multiple
  391. * physical PMUs (per cluster), because we do not support per-task mode
  392. * each event is associated with a CPU. Each event has pmu_enable
  393. * called on its CPU, so here it is only necessary to enable the
  394. * counters for the current CPU.
  395. */
  396. cluster_pmu_enable();
  397. }
  398. static void l2_cache_pmu_disable(struct pmu *pmu)
  399. {
  400. cluster_pmu_disable();
  401. }
  402. static int l2_cache_event_init(struct perf_event *event)
  403. {
  404. struct hw_perf_event *hwc = &event->hw;
  405. struct cluster_pmu *cluster;
  406. struct perf_event *sibling;
  407. struct l2cache_pmu *l2cache_pmu;
  408. if (event->attr.type != event->pmu->type)
  409. return -ENOENT;
  410. l2cache_pmu = to_l2cache_pmu(event->pmu);
  411. if (hwc->sample_period) {
  412. dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
  413. "Sampling not supported\n");
  414. return -EOPNOTSUPP;
  415. }
  416. if (event->cpu < 0) {
  417. dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
  418. "Per-task mode not supported\n");
  419. return -EOPNOTSUPP;
  420. }
  421. /* We cannot filter accurately so we just don't allow it. */
  422. if (event->attr.exclude_user || event->attr.exclude_kernel ||
  423. event->attr.exclude_hv || event->attr.exclude_idle) {
  424. dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
  425. "Can't exclude execution levels\n");
  426. return -EOPNOTSUPP;
  427. }
  428. if (((L2_EVT_GROUP(event->attr.config) > L2_EVT_GROUP_MAX) ||
  429. ((event->attr.config & ~L2_EVT_MASK) != 0)) &&
  430. (event->attr.config != L2CYCLE_CTR_RAW_CODE)) {
  431. dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
  432. "Invalid config %llx\n",
  433. event->attr.config);
  434. return -EINVAL;
  435. }
  436. /* Don't allow groups with mixed PMUs, except for s/w events */
  437. if (event->group_leader->pmu != event->pmu &&
  438. !is_software_event(event->group_leader)) {
  439. dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
  440. "Can't create mixed PMU group\n");
  441. return -EINVAL;
  442. }
  443. for_each_sibling_event(sibling, event->group_leader) {
  444. if (sibling->pmu != event->pmu &&
  445. !is_software_event(sibling)) {
  446. dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
  447. "Can't create mixed PMU group\n");
  448. return -EINVAL;
  449. }
  450. }
  451. cluster = get_cluster_pmu(l2cache_pmu, event->cpu);
  452. if (!cluster) {
  453. /* CPU has not been initialised */
  454. dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
  455. "CPU%d not associated with L2 cluster\n", event->cpu);
  456. return -EINVAL;
  457. }
  458. /* Ensure all events in a group are on the same cpu */
  459. if ((event->group_leader != event) &&
  460. (cluster->on_cpu != event->group_leader->cpu)) {
  461. dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
  462. "Can't create group on CPUs %d and %d",
  463. event->cpu, event->group_leader->cpu);
  464. return -EINVAL;
  465. }
  466. if ((event != event->group_leader) &&
  467. !is_software_event(event->group_leader) &&
  468. (L2_EVT_GROUP(event->group_leader->attr.config) ==
  469. L2_EVT_GROUP(event->attr.config))) {
  470. dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
  471. "Column exclusion: conflicting events %llx %llx\n",
  472. event->group_leader->attr.config,
  473. event->attr.config);
  474. return -EINVAL;
  475. }
  476. for_each_sibling_event(sibling, event->group_leader) {
  477. if ((sibling != event) &&
  478. !is_software_event(sibling) &&
  479. (L2_EVT_GROUP(sibling->attr.config) ==
  480. L2_EVT_GROUP(event->attr.config))) {
  481. dev_dbg_ratelimited(&l2cache_pmu->pdev->dev,
  482. "Column exclusion: conflicting events %llx %llx\n",
  483. sibling->attr.config,
  484. event->attr.config);
  485. return -EINVAL;
  486. }
  487. }
  488. hwc->idx = -1;
  489. hwc->config_base = event->attr.config;
  490. /*
  491. * Ensure all events are on the same cpu so all events are in the
  492. * same cpu context, to avoid races on pmu_enable etc.
  493. */
  494. event->cpu = cluster->on_cpu;
  495. return 0;
  496. }
  497. static void l2_cache_event_start(struct perf_event *event, int flags)
  498. {
  499. struct cluster_pmu *cluster;
  500. struct hw_perf_event *hwc = &event->hw;
  501. int idx = hwc->idx;
  502. u32 config;
  503. u32 event_cc, event_group;
  504. hwc->state = 0;
  505. cluster = get_cluster_pmu(to_l2cache_pmu(event->pmu), event->cpu);
  506. l2_cache_cluster_set_period(cluster, hwc);
  507. if (hwc->config_base == L2CYCLE_CTR_RAW_CODE) {
  508. cluster_pmu_set_evccntcr(0);
  509. } else {
  510. config = hwc->config_base;
  511. event_cc = L2_EVT_CODE(config);
  512. event_group = L2_EVT_GROUP(config);
  513. cluster_pmu_set_evcntcr(idx, 0);
  514. cluster_pmu_set_evtyper(idx, event_group);
  515. cluster_pmu_set_resr(cluster, event_group, event_cc);
  516. cluster_pmu_set_evfilter_sys_mode(idx);
  517. }
  518. cluster_pmu_counter_enable_interrupt(idx);
  519. cluster_pmu_counter_enable(idx);
  520. }
  521. static void l2_cache_event_stop(struct perf_event *event, int flags)
  522. {
  523. struct hw_perf_event *hwc = &event->hw;
  524. int idx = hwc->idx;
  525. if (hwc->state & PERF_HES_STOPPED)
  526. return;
  527. cluster_pmu_counter_disable_interrupt(idx);
  528. cluster_pmu_counter_disable(idx);
  529. if (flags & PERF_EF_UPDATE)
  530. l2_cache_event_update(event);
  531. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  532. }
  533. static int l2_cache_event_add(struct perf_event *event, int flags)
  534. {
  535. struct hw_perf_event *hwc = &event->hw;
  536. int idx;
  537. int err = 0;
  538. struct cluster_pmu *cluster;
  539. cluster = get_cluster_pmu(to_l2cache_pmu(event->pmu), event->cpu);
  540. idx = l2_cache_get_event_idx(cluster, event);
  541. if (idx < 0)
  542. return idx;
  543. hwc->idx = idx;
  544. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  545. cluster->events[idx] = event;
  546. local64_set(&hwc->prev_count, 0);
  547. if (flags & PERF_EF_START)
  548. l2_cache_event_start(event, flags);
  549. /* Propagate changes to the userspace mapping. */
  550. perf_event_update_userpage(event);
  551. return err;
  552. }
  553. static void l2_cache_event_del(struct perf_event *event, int flags)
  554. {
  555. struct hw_perf_event *hwc = &event->hw;
  556. struct cluster_pmu *cluster;
  557. int idx = hwc->idx;
  558. cluster = get_cluster_pmu(to_l2cache_pmu(event->pmu), event->cpu);
  559. l2_cache_event_stop(event, flags | PERF_EF_UPDATE);
  560. cluster->events[idx] = NULL;
  561. l2_cache_clear_event_idx(cluster, event);
  562. perf_event_update_userpage(event);
  563. }
  564. static void l2_cache_event_read(struct perf_event *event)
  565. {
  566. l2_cache_event_update(event);
  567. }
  568. static ssize_t l2_cache_pmu_cpumask_show(struct device *dev,
  569. struct device_attribute *attr,
  570. char *buf)
  571. {
  572. struct l2cache_pmu *l2cache_pmu = to_l2cache_pmu(dev_get_drvdata(dev));
  573. return cpumap_print_to_pagebuf(true, buf, &l2cache_pmu->cpumask);
  574. }
  575. static struct device_attribute l2_cache_pmu_cpumask_attr =
  576. __ATTR(cpumask, S_IRUGO, l2_cache_pmu_cpumask_show, NULL);
  577. static struct attribute *l2_cache_pmu_cpumask_attrs[] = {
  578. &l2_cache_pmu_cpumask_attr.attr,
  579. NULL,
  580. };
  581. static struct attribute_group l2_cache_pmu_cpumask_group = {
  582. .attrs = l2_cache_pmu_cpumask_attrs,
  583. };
  584. /* CCG format for perf RAW codes. */
  585. PMU_FORMAT_ATTR(l2_code, "config:4-11");
  586. PMU_FORMAT_ATTR(l2_group, "config:0-3");
  587. PMU_FORMAT_ATTR(event, "config:0-11");
  588. static struct attribute *l2_cache_pmu_formats[] = {
  589. &format_attr_l2_code.attr,
  590. &format_attr_l2_group.attr,
  591. &format_attr_event.attr,
  592. NULL,
  593. };
  594. static struct attribute_group l2_cache_pmu_format_group = {
  595. .name = "format",
  596. .attrs = l2_cache_pmu_formats,
  597. };
  598. static ssize_t l2cache_pmu_event_show(struct device *dev,
  599. struct device_attribute *attr, char *page)
  600. {
  601. struct perf_pmu_events_attr *pmu_attr;
  602. pmu_attr = container_of(attr, struct perf_pmu_events_attr, attr);
  603. return sprintf(page, "event=0x%02llx\n", pmu_attr->id);
  604. }
  605. #define L2CACHE_EVENT_ATTR(_name, _id) \
  606. (&((struct perf_pmu_events_attr[]) { \
  607. { .attr = __ATTR(_name, 0444, l2cache_pmu_event_show, NULL), \
  608. .id = _id, } \
  609. })[0].attr.attr)
  610. static struct attribute *l2_cache_pmu_events[] = {
  611. L2CACHE_EVENT_ATTR(cycles, L2_EVENT_CYCLES),
  612. L2CACHE_EVENT_ATTR(dcache-ops, L2_EVENT_DCACHE_OPS),
  613. L2CACHE_EVENT_ATTR(icache-ops, L2_EVENT_ICACHE_OPS),
  614. L2CACHE_EVENT_ATTR(tlbi, L2_EVENT_TLBI),
  615. L2CACHE_EVENT_ATTR(barriers, L2_EVENT_BARRIERS),
  616. L2CACHE_EVENT_ATTR(total-reads, L2_EVENT_TOTAL_READS),
  617. L2CACHE_EVENT_ATTR(total-writes, L2_EVENT_TOTAL_WRITES),
  618. L2CACHE_EVENT_ATTR(total-requests, L2_EVENT_TOTAL_REQUESTS),
  619. L2CACHE_EVENT_ATTR(ldrex, L2_EVENT_LDREX),
  620. L2CACHE_EVENT_ATTR(strex, L2_EVENT_STREX),
  621. L2CACHE_EVENT_ATTR(clrex, L2_EVENT_CLREX),
  622. NULL
  623. };
  624. static struct attribute_group l2_cache_pmu_events_group = {
  625. .name = "events",
  626. .attrs = l2_cache_pmu_events,
  627. };
  628. static const struct attribute_group *l2_cache_pmu_attr_grps[] = {
  629. &l2_cache_pmu_format_group,
  630. &l2_cache_pmu_cpumask_group,
  631. &l2_cache_pmu_events_group,
  632. NULL,
  633. };
  634. /*
  635. * Generic device handlers
  636. */
  637. static const struct acpi_device_id l2_cache_pmu_acpi_match[] = {
  638. { "QCOM8130", },
  639. { }
  640. };
  641. static int get_num_counters(void)
  642. {
  643. int val;
  644. val = get_l2_indirect_reg(L2PMCR);
  645. /*
  646. * Read number of counters from L2PMCR and add 1
  647. * for the cycle counter.
  648. */
  649. return ((val >> L2PMCR_NUM_EV_SHIFT) & L2PMCR_NUM_EV_MASK) + 1;
  650. }
  651. static struct cluster_pmu *l2_cache_associate_cpu_with_cluster(
  652. struct l2cache_pmu *l2cache_pmu, int cpu)
  653. {
  654. u64 mpidr;
  655. int cpu_cluster_id;
  656. struct cluster_pmu *cluster = NULL;
  657. /*
  658. * This assumes that the cluster_id is in MPIDR[aff1] for
  659. * single-threaded cores, and MPIDR[aff2] for multi-threaded
  660. * cores. This logic will have to be updated if this changes.
  661. */
  662. mpidr = read_cpuid_mpidr();
  663. if (mpidr & MPIDR_MT_BITMASK)
  664. cpu_cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 2);
  665. else
  666. cpu_cluster_id = MPIDR_AFFINITY_LEVEL(mpidr, 1);
  667. list_for_each_entry(cluster, &l2cache_pmu->clusters, next) {
  668. if (cluster->cluster_id != cpu_cluster_id)
  669. continue;
  670. dev_info(&l2cache_pmu->pdev->dev,
  671. "CPU%d associated with cluster %d\n", cpu,
  672. cluster->cluster_id);
  673. cpumask_set_cpu(cpu, &cluster->cluster_cpus);
  674. *per_cpu_ptr(l2cache_pmu->pmu_cluster, cpu) = cluster;
  675. break;
  676. }
  677. return cluster;
  678. }
  679. static int l2cache_pmu_online_cpu(unsigned int cpu, struct hlist_node *node)
  680. {
  681. struct cluster_pmu *cluster;
  682. struct l2cache_pmu *l2cache_pmu;
  683. l2cache_pmu = hlist_entry_safe(node, struct l2cache_pmu, node);
  684. cluster = get_cluster_pmu(l2cache_pmu, cpu);
  685. if (!cluster) {
  686. /* First time this CPU has come online */
  687. cluster = l2_cache_associate_cpu_with_cluster(l2cache_pmu, cpu);
  688. if (!cluster) {
  689. /* Only if broken firmware doesn't list every cluster */
  690. WARN_ONCE(1, "No L2 cache cluster for CPU%d\n", cpu);
  691. return 0;
  692. }
  693. }
  694. /* If another CPU is managing this cluster, we're done */
  695. if (cluster->on_cpu != -1)
  696. return 0;
  697. /*
  698. * All CPUs on this cluster were down, use this one.
  699. * Reset to put it into sane state.
  700. */
  701. cluster->on_cpu = cpu;
  702. cpumask_set_cpu(cpu, &l2cache_pmu->cpumask);
  703. cluster_pmu_reset();
  704. WARN_ON(irq_set_affinity(cluster->irq, cpumask_of(cpu)));
  705. enable_irq(cluster->irq);
  706. return 0;
  707. }
  708. static int l2cache_pmu_offline_cpu(unsigned int cpu, struct hlist_node *node)
  709. {
  710. struct cluster_pmu *cluster;
  711. struct l2cache_pmu *l2cache_pmu;
  712. cpumask_t cluster_online_cpus;
  713. unsigned int target;
  714. l2cache_pmu = hlist_entry_safe(node, struct l2cache_pmu, node);
  715. cluster = get_cluster_pmu(l2cache_pmu, cpu);
  716. if (!cluster)
  717. return 0;
  718. /* If this CPU is not managing the cluster, we're done */
  719. if (cluster->on_cpu != cpu)
  720. return 0;
  721. /* Give up ownership of cluster */
  722. cpumask_clear_cpu(cpu, &l2cache_pmu->cpumask);
  723. cluster->on_cpu = -1;
  724. /* Any other CPU for this cluster which is still online */
  725. cpumask_and(&cluster_online_cpus, &cluster->cluster_cpus,
  726. cpu_online_mask);
  727. target = cpumask_any_but(&cluster_online_cpus, cpu);
  728. if (target >= nr_cpu_ids) {
  729. disable_irq(cluster->irq);
  730. return 0;
  731. }
  732. perf_pmu_migrate_context(&l2cache_pmu->pmu, cpu, target);
  733. cluster->on_cpu = target;
  734. cpumask_set_cpu(target, &l2cache_pmu->cpumask);
  735. WARN_ON(irq_set_affinity(cluster->irq, cpumask_of(target)));
  736. return 0;
  737. }
  738. static int l2_cache_pmu_probe_cluster(struct device *dev, void *data)
  739. {
  740. struct platform_device *pdev = to_platform_device(dev->parent);
  741. struct platform_device *sdev = to_platform_device(dev);
  742. struct l2cache_pmu *l2cache_pmu = data;
  743. struct cluster_pmu *cluster;
  744. struct acpi_device *device;
  745. unsigned long fw_cluster_id;
  746. int err;
  747. int irq;
  748. if (acpi_bus_get_device(ACPI_HANDLE(dev), &device))
  749. return -ENODEV;
  750. if (kstrtoul(device->pnp.unique_id, 10, &fw_cluster_id) < 0) {
  751. dev_err(&pdev->dev, "unable to read ACPI uid\n");
  752. return -ENODEV;
  753. }
  754. cluster = devm_kzalloc(&pdev->dev, sizeof(*cluster), GFP_KERNEL);
  755. if (!cluster)
  756. return -ENOMEM;
  757. INIT_LIST_HEAD(&cluster->next);
  758. list_add(&cluster->next, &l2cache_pmu->clusters);
  759. cluster->cluster_id = fw_cluster_id;
  760. irq = platform_get_irq(sdev, 0);
  761. if (irq < 0) {
  762. dev_err(&pdev->dev,
  763. "Failed to get valid irq for cluster %ld\n",
  764. fw_cluster_id);
  765. return irq;
  766. }
  767. irq_set_status_flags(irq, IRQ_NOAUTOEN);
  768. cluster->irq = irq;
  769. cluster->l2cache_pmu = l2cache_pmu;
  770. cluster->on_cpu = -1;
  771. err = devm_request_irq(&pdev->dev, irq, l2_cache_handle_irq,
  772. IRQF_NOBALANCING | IRQF_NO_THREAD,
  773. "l2-cache-pmu", cluster);
  774. if (err) {
  775. dev_err(&pdev->dev,
  776. "Unable to request IRQ%d for L2 PMU counters\n", irq);
  777. return err;
  778. }
  779. dev_info(&pdev->dev,
  780. "Registered L2 cache PMU cluster %ld\n", fw_cluster_id);
  781. spin_lock_init(&cluster->pmu_lock);
  782. l2cache_pmu->num_pmus++;
  783. return 0;
  784. }
  785. static int l2_cache_pmu_probe(struct platform_device *pdev)
  786. {
  787. int err;
  788. struct l2cache_pmu *l2cache_pmu;
  789. l2cache_pmu =
  790. devm_kzalloc(&pdev->dev, sizeof(*l2cache_pmu), GFP_KERNEL);
  791. if (!l2cache_pmu)
  792. return -ENOMEM;
  793. INIT_LIST_HEAD(&l2cache_pmu->clusters);
  794. platform_set_drvdata(pdev, l2cache_pmu);
  795. l2cache_pmu->pmu = (struct pmu) {
  796. /* suffix is instance id for future use with multiple sockets */
  797. .name = "l2cache_0",
  798. .task_ctx_nr = perf_invalid_context,
  799. .pmu_enable = l2_cache_pmu_enable,
  800. .pmu_disable = l2_cache_pmu_disable,
  801. .event_init = l2_cache_event_init,
  802. .add = l2_cache_event_add,
  803. .del = l2_cache_event_del,
  804. .start = l2_cache_event_start,
  805. .stop = l2_cache_event_stop,
  806. .read = l2_cache_event_read,
  807. .attr_groups = l2_cache_pmu_attr_grps,
  808. };
  809. l2cache_pmu->num_counters = get_num_counters();
  810. l2cache_pmu->pdev = pdev;
  811. l2cache_pmu->pmu_cluster = devm_alloc_percpu(&pdev->dev,
  812. struct cluster_pmu *);
  813. if (!l2cache_pmu->pmu_cluster)
  814. return -ENOMEM;
  815. l2_cycle_ctr_idx = l2cache_pmu->num_counters - 1;
  816. l2_counter_present_mask = GENMASK(l2cache_pmu->num_counters - 2, 0) |
  817. BIT(L2CYCLE_CTR_BIT);
  818. cpumask_clear(&l2cache_pmu->cpumask);
  819. /* Read cluster info and initialize each cluster */
  820. err = device_for_each_child(&pdev->dev, l2cache_pmu,
  821. l2_cache_pmu_probe_cluster);
  822. if (err)
  823. return err;
  824. if (l2cache_pmu->num_pmus == 0) {
  825. dev_err(&pdev->dev, "No hardware L2 cache PMUs found\n");
  826. return -ENODEV;
  827. }
  828. err = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
  829. &l2cache_pmu->node);
  830. if (err) {
  831. dev_err(&pdev->dev, "Error %d registering hotplug", err);
  832. return err;
  833. }
  834. err = perf_pmu_register(&l2cache_pmu->pmu, l2cache_pmu->pmu.name, -1);
  835. if (err) {
  836. dev_err(&pdev->dev, "Error %d registering L2 cache PMU\n", err);
  837. goto out_unregister;
  838. }
  839. dev_info(&pdev->dev, "Registered L2 cache PMU using %d HW PMUs\n",
  840. l2cache_pmu->num_pmus);
  841. return err;
  842. out_unregister:
  843. cpuhp_state_remove_instance(CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
  844. &l2cache_pmu->node);
  845. return err;
  846. }
  847. static int l2_cache_pmu_remove(struct platform_device *pdev)
  848. {
  849. struct l2cache_pmu *l2cache_pmu =
  850. to_l2cache_pmu(platform_get_drvdata(pdev));
  851. perf_pmu_unregister(&l2cache_pmu->pmu);
  852. cpuhp_state_remove_instance(CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
  853. &l2cache_pmu->node);
  854. return 0;
  855. }
  856. static struct platform_driver l2_cache_pmu_driver = {
  857. .driver = {
  858. .name = "qcom-l2cache-pmu",
  859. .acpi_match_table = ACPI_PTR(l2_cache_pmu_acpi_match),
  860. },
  861. .probe = l2_cache_pmu_probe,
  862. .remove = l2_cache_pmu_remove,
  863. };
  864. static int __init register_l2_cache_pmu_driver(void)
  865. {
  866. int err;
  867. err = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_QCOM_L2_ONLINE,
  868. "AP_PERF_ARM_QCOM_L2_ONLINE",
  869. l2cache_pmu_online_cpu,
  870. l2cache_pmu_offline_cpu);
  871. if (err)
  872. return err;
  873. return platform_driver_register(&l2_cache_pmu_driver);
  874. }
  875. device_initcall(register_l2_cache_pmu_driver);