arm_dsu_pmu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * ARM DynamIQ Shared Unit (DSU) PMU driver
  3. *
  4. * Copyright (C) ARM Limited, 2017.
  5. *
  6. * Based on ARM CCI-PMU, ARMv8 PMU-v3 drivers.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * version 2 as published by the Free Software Foundation.
  11. */
  12. #define PMUNAME "arm_dsu"
  13. #define DRVNAME PMUNAME "_pmu"
  14. #define pr_fmt(fmt) DRVNAME ": " fmt
  15. #include <linux/bitmap.h>
  16. #include <linux/bitops.h>
  17. #include <linux/bug.h>
  18. #include <linux/cpumask.h>
  19. #include <linux/device.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/kernel.h>
  22. #include <linux/module.h>
  23. #include <linux/of_device.h>
  24. #include <linux/perf_event.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/smp.h>
  28. #include <linux/sysfs.h>
  29. #include <linux/types.h>
  30. #include <asm/arm_dsu_pmu.h>
  31. #include <asm/local64.h>
  32. /* PMU event codes */
  33. #define DSU_PMU_EVT_CYCLES 0x11
  34. #define DSU_PMU_EVT_CHAIN 0x1e
  35. #define DSU_PMU_MAX_COMMON_EVENTS 0x40
  36. #define DSU_PMU_MAX_HW_CNTRS 32
  37. #define DSU_PMU_HW_COUNTER_MASK (DSU_PMU_MAX_HW_CNTRS - 1)
  38. #define CLUSTERPMCR_E BIT(0)
  39. #define CLUSTERPMCR_P BIT(1)
  40. #define CLUSTERPMCR_C BIT(2)
  41. #define CLUSTERPMCR_N_SHIFT 11
  42. #define CLUSTERPMCR_N_MASK 0x1f
  43. #define CLUSTERPMCR_IDCODE_SHIFT 16
  44. #define CLUSTERPMCR_IDCODE_MASK 0xff
  45. #define CLUSTERPMCR_IMP_SHIFT 24
  46. #define CLUSTERPMCR_IMP_MASK 0xff
  47. #define CLUSTERPMCR_RES_MASK 0x7e8
  48. #define CLUSTERPMCR_RES_VAL 0x40
  49. #define DSU_ACTIVE_CPU_MASK 0x0
  50. #define DSU_ASSOCIATED_CPU_MASK 0x1
  51. /*
  52. * We use the index of the counters as they appear in the counter
  53. * bit maps in the PMU registers (e.g CLUSTERPMSELR).
  54. * i.e,
  55. * counter 0 - Bit 0
  56. * counter 1 - Bit 1
  57. * ...
  58. * Cycle counter - Bit 31
  59. */
  60. #define DSU_PMU_IDX_CYCLE_COUNTER 31
  61. /* All event counters are 32bit, with a 64bit Cycle counter */
  62. #define DSU_PMU_COUNTER_WIDTH(idx) \
  63. (((idx) == DSU_PMU_IDX_CYCLE_COUNTER) ? 64 : 32)
  64. #define DSU_PMU_COUNTER_MASK(idx) \
  65. GENMASK_ULL((DSU_PMU_COUNTER_WIDTH((idx)) - 1), 0)
  66. #define DSU_EXT_ATTR(_name, _func, _config) \
  67. (&((struct dev_ext_attribute[]) { \
  68. { \
  69. .attr = __ATTR(_name, 0444, _func, NULL), \
  70. .var = (void *)_config \
  71. } \
  72. })[0].attr.attr)
  73. #define DSU_EVENT_ATTR(_name, _config) \
  74. DSU_EXT_ATTR(_name, dsu_pmu_sysfs_event_show, (unsigned long)_config)
  75. #define DSU_FORMAT_ATTR(_name, _config) \
  76. DSU_EXT_ATTR(_name, dsu_pmu_sysfs_format_show, (char *)_config)
  77. #define DSU_CPUMASK_ATTR(_name, _config) \
  78. DSU_EXT_ATTR(_name, dsu_pmu_cpumask_show, (unsigned long)_config)
  79. struct dsu_hw_events {
  80. DECLARE_BITMAP(used_mask, DSU_PMU_MAX_HW_CNTRS);
  81. struct perf_event *events[DSU_PMU_MAX_HW_CNTRS];
  82. };
  83. /*
  84. * struct dsu_pmu - DSU PMU descriptor
  85. *
  86. * @pmu_lock : Protects accesses to DSU PMU register from normal vs
  87. * interrupt handler contexts.
  88. * @hw_events : Holds the event counter state.
  89. * @associated_cpus : CPUs attached to the DSU.
  90. * @active_cpu : CPU to which the PMU is bound for accesses.
  91. * @cpuhp_node : Node for CPU hotplug notifier link.
  92. * @num_counters : Number of event counters implemented by the PMU,
  93. * excluding the cycle counter.
  94. * @irq : Interrupt line for counter overflow.
  95. * @cpmceid_bitmap : Bitmap for the availability of architected common
  96. * events (event_code < 0x40).
  97. */
  98. struct dsu_pmu {
  99. struct pmu pmu;
  100. struct device *dev;
  101. raw_spinlock_t pmu_lock;
  102. struct dsu_hw_events hw_events;
  103. cpumask_t associated_cpus;
  104. cpumask_t active_cpu;
  105. struct hlist_node cpuhp_node;
  106. s8 num_counters;
  107. int irq;
  108. DECLARE_BITMAP(cpmceid_bitmap, DSU_PMU_MAX_COMMON_EVENTS);
  109. };
  110. static unsigned long dsu_pmu_cpuhp_state;
  111. static inline struct dsu_pmu *to_dsu_pmu(struct pmu *pmu)
  112. {
  113. return container_of(pmu, struct dsu_pmu, pmu);
  114. }
  115. static ssize_t dsu_pmu_sysfs_event_show(struct device *dev,
  116. struct device_attribute *attr,
  117. char *buf)
  118. {
  119. struct dev_ext_attribute *eattr = container_of(attr,
  120. struct dev_ext_attribute, attr);
  121. return snprintf(buf, PAGE_SIZE, "event=0x%lx\n",
  122. (unsigned long)eattr->var);
  123. }
  124. static ssize_t dsu_pmu_sysfs_format_show(struct device *dev,
  125. struct device_attribute *attr,
  126. char *buf)
  127. {
  128. struct dev_ext_attribute *eattr = container_of(attr,
  129. struct dev_ext_attribute, attr);
  130. return snprintf(buf, PAGE_SIZE, "%s\n", (char *)eattr->var);
  131. }
  132. static ssize_t dsu_pmu_cpumask_show(struct device *dev,
  133. struct device_attribute *attr,
  134. char *buf)
  135. {
  136. struct pmu *pmu = dev_get_drvdata(dev);
  137. struct dsu_pmu *dsu_pmu = to_dsu_pmu(pmu);
  138. struct dev_ext_attribute *eattr = container_of(attr,
  139. struct dev_ext_attribute, attr);
  140. unsigned long mask_id = (unsigned long)eattr->var;
  141. const cpumask_t *cpumask;
  142. switch (mask_id) {
  143. case DSU_ACTIVE_CPU_MASK:
  144. cpumask = &dsu_pmu->active_cpu;
  145. break;
  146. case DSU_ASSOCIATED_CPU_MASK:
  147. cpumask = &dsu_pmu->associated_cpus;
  148. break;
  149. default:
  150. return 0;
  151. }
  152. return cpumap_print_to_pagebuf(true, buf, cpumask);
  153. }
  154. static struct attribute *dsu_pmu_format_attrs[] = {
  155. DSU_FORMAT_ATTR(event, "config:0-31"),
  156. NULL,
  157. };
  158. static const struct attribute_group dsu_pmu_format_attr_group = {
  159. .name = "format",
  160. .attrs = dsu_pmu_format_attrs,
  161. };
  162. static struct attribute *dsu_pmu_event_attrs[] = {
  163. DSU_EVENT_ATTR(cycles, 0x11),
  164. DSU_EVENT_ATTR(bus_access, 0x19),
  165. DSU_EVENT_ATTR(memory_error, 0x1a),
  166. DSU_EVENT_ATTR(bus_cycles, 0x1d),
  167. DSU_EVENT_ATTR(l3d_cache_allocate, 0x29),
  168. DSU_EVENT_ATTR(l3d_cache_refill, 0x2a),
  169. DSU_EVENT_ATTR(l3d_cache, 0x2b),
  170. DSU_EVENT_ATTR(l3d_cache_wb, 0x2c),
  171. NULL,
  172. };
  173. static umode_t
  174. dsu_pmu_event_attr_is_visible(struct kobject *kobj, struct attribute *attr,
  175. int unused)
  176. {
  177. struct pmu *pmu = dev_get_drvdata(kobj_to_dev(kobj));
  178. struct dsu_pmu *dsu_pmu = to_dsu_pmu(pmu);
  179. struct dev_ext_attribute *eattr = container_of(attr,
  180. struct dev_ext_attribute, attr.attr);
  181. unsigned long evt = (unsigned long)eattr->var;
  182. return test_bit(evt, dsu_pmu->cpmceid_bitmap) ? attr->mode : 0;
  183. }
  184. static const struct attribute_group dsu_pmu_events_attr_group = {
  185. .name = "events",
  186. .attrs = dsu_pmu_event_attrs,
  187. .is_visible = dsu_pmu_event_attr_is_visible,
  188. };
  189. static struct attribute *dsu_pmu_cpumask_attrs[] = {
  190. DSU_CPUMASK_ATTR(cpumask, DSU_ACTIVE_CPU_MASK),
  191. DSU_CPUMASK_ATTR(associated_cpus, DSU_ASSOCIATED_CPU_MASK),
  192. NULL,
  193. };
  194. static const struct attribute_group dsu_pmu_cpumask_attr_group = {
  195. .attrs = dsu_pmu_cpumask_attrs,
  196. };
  197. static const struct attribute_group *dsu_pmu_attr_groups[] = {
  198. &dsu_pmu_cpumask_attr_group,
  199. &dsu_pmu_events_attr_group,
  200. &dsu_pmu_format_attr_group,
  201. NULL,
  202. };
  203. static int dsu_pmu_get_online_cpu_any_but(struct dsu_pmu *dsu_pmu, int cpu)
  204. {
  205. struct cpumask online_supported;
  206. cpumask_and(&online_supported,
  207. &dsu_pmu->associated_cpus, cpu_online_mask);
  208. return cpumask_any_but(&online_supported, cpu);
  209. }
  210. static inline bool dsu_pmu_counter_valid(struct dsu_pmu *dsu_pmu, u32 idx)
  211. {
  212. return (idx < dsu_pmu->num_counters) ||
  213. (idx == DSU_PMU_IDX_CYCLE_COUNTER);
  214. }
  215. static inline u64 dsu_pmu_read_counter(struct perf_event *event)
  216. {
  217. u64 val;
  218. unsigned long flags;
  219. struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
  220. int idx = event->hw.idx;
  221. if (WARN_ON(!cpumask_test_cpu(smp_processor_id(),
  222. &dsu_pmu->associated_cpus)))
  223. return 0;
  224. if (!dsu_pmu_counter_valid(dsu_pmu, idx)) {
  225. dev_err(event->pmu->dev,
  226. "Trying reading invalid counter %d\n", idx);
  227. return 0;
  228. }
  229. raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags);
  230. if (idx == DSU_PMU_IDX_CYCLE_COUNTER)
  231. val = __dsu_pmu_read_pmccntr();
  232. else
  233. val = __dsu_pmu_read_counter(idx);
  234. raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags);
  235. return val;
  236. }
  237. static void dsu_pmu_write_counter(struct perf_event *event, u64 val)
  238. {
  239. unsigned long flags;
  240. struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
  241. int idx = event->hw.idx;
  242. if (WARN_ON(!cpumask_test_cpu(smp_processor_id(),
  243. &dsu_pmu->associated_cpus)))
  244. return;
  245. if (!dsu_pmu_counter_valid(dsu_pmu, idx)) {
  246. dev_err(event->pmu->dev,
  247. "writing to invalid counter %d\n", idx);
  248. return;
  249. }
  250. raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags);
  251. if (idx == DSU_PMU_IDX_CYCLE_COUNTER)
  252. __dsu_pmu_write_pmccntr(val);
  253. else
  254. __dsu_pmu_write_counter(idx, val);
  255. raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags);
  256. }
  257. static int dsu_pmu_get_event_idx(struct dsu_hw_events *hw_events,
  258. struct perf_event *event)
  259. {
  260. int idx;
  261. unsigned long evtype = event->attr.config;
  262. struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
  263. unsigned long *used_mask = hw_events->used_mask;
  264. if (evtype == DSU_PMU_EVT_CYCLES) {
  265. if (test_and_set_bit(DSU_PMU_IDX_CYCLE_COUNTER, used_mask))
  266. return -EAGAIN;
  267. return DSU_PMU_IDX_CYCLE_COUNTER;
  268. }
  269. idx = find_first_zero_bit(used_mask, dsu_pmu->num_counters);
  270. if (idx >= dsu_pmu->num_counters)
  271. return -EAGAIN;
  272. set_bit(idx, hw_events->used_mask);
  273. return idx;
  274. }
  275. static void dsu_pmu_enable_counter(struct dsu_pmu *dsu_pmu, int idx)
  276. {
  277. __dsu_pmu_counter_interrupt_enable(idx);
  278. __dsu_pmu_enable_counter(idx);
  279. }
  280. static void dsu_pmu_disable_counter(struct dsu_pmu *dsu_pmu, int idx)
  281. {
  282. __dsu_pmu_disable_counter(idx);
  283. __dsu_pmu_counter_interrupt_disable(idx);
  284. }
  285. static inline void dsu_pmu_set_event(struct dsu_pmu *dsu_pmu,
  286. struct perf_event *event)
  287. {
  288. int idx = event->hw.idx;
  289. unsigned long flags;
  290. if (!dsu_pmu_counter_valid(dsu_pmu, idx)) {
  291. dev_err(event->pmu->dev,
  292. "Trying to set invalid counter %d\n", idx);
  293. return;
  294. }
  295. raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags);
  296. __dsu_pmu_set_event(idx, event->hw.config_base);
  297. raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags);
  298. }
  299. static void dsu_pmu_event_update(struct perf_event *event)
  300. {
  301. struct hw_perf_event *hwc = &event->hw;
  302. u64 delta, prev_count, new_count;
  303. do {
  304. /* We may also be called from the irq handler */
  305. prev_count = local64_read(&hwc->prev_count);
  306. new_count = dsu_pmu_read_counter(event);
  307. } while (local64_cmpxchg(&hwc->prev_count, prev_count, new_count) !=
  308. prev_count);
  309. delta = (new_count - prev_count) & DSU_PMU_COUNTER_MASK(hwc->idx);
  310. local64_add(delta, &event->count);
  311. }
  312. static void dsu_pmu_read(struct perf_event *event)
  313. {
  314. dsu_pmu_event_update(event);
  315. }
  316. static inline u32 dsu_pmu_get_reset_overflow(void)
  317. {
  318. return __dsu_pmu_get_reset_overflow();
  319. }
  320. /**
  321. * dsu_pmu_set_event_period: Set the period for the counter.
  322. *
  323. * All DSU PMU event counters, except the cycle counter are 32bit
  324. * counters. To handle cases of extreme interrupt latency, we program
  325. * the counter with half of the max count for the counters.
  326. */
  327. static void dsu_pmu_set_event_period(struct perf_event *event)
  328. {
  329. int idx = event->hw.idx;
  330. u64 val = DSU_PMU_COUNTER_MASK(idx) >> 1;
  331. local64_set(&event->hw.prev_count, val);
  332. dsu_pmu_write_counter(event, val);
  333. }
  334. static irqreturn_t dsu_pmu_handle_irq(int irq_num, void *dev)
  335. {
  336. int i;
  337. bool handled = false;
  338. struct dsu_pmu *dsu_pmu = dev;
  339. struct dsu_hw_events *hw_events = &dsu_pmu->hw_events;
  340. unsigned long overflow;
  341. overflow = dsu_pmu_get_reset_overflow();
  342. if (!overflow)
  343. return IRQ_NONE;
  344. for_each_set_bit(i, &overflow, DSU_PMU_MAX_HW_CNTRS) {
  345. struct perf_event *event = hw_events->events[i];
  346. if (!event)
  347. continue;
  348. dsu_pmu_event_update(event);
  349. dsu_pmu_set_event_period(event);
  350. handled = true;
  351. }
  352. return IRQ_RETVAL(handled);
  353. }
  354. static void dsu_pmu_start(struct perf_event *event, int pmu_flags)
  355. {
  356. struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
  357. /* We always reprogram the counter */
  358. if (pmu_flags & PERF_EF_RELOAD)
  359. WARN_ON(!(event->hw.state & PERF_HES_UPTODATE));
  360. dsu_pmu_set_event_period(event);
  361. if (event->hw.idx != DSU_PMU_IDX_CYCLE_COUNTER)
  362. dsu_pmu_set_event(dsu_pmu, event);
  363. event->hw.state = 0;
  364. dsu_pmu_enable_counter(dsu_pmu, event->hw.idx);
  365. }
  366. static void dsu_pmu_stop(struct perf_event *event, int pmu_flags)
  367. {
  368. struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
  369. if (event->hw.state & PERF_HES_STOPPED)
  370. return;
  371. dsu_pmu_disable_counter(dsu_pmu, event->hw.idx);
  372. dsu_pmu_event_update(event);
  373. event->hw.state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  374. }
  375. static int dsu_pmu_add(struct perf_event *event, int flags)
  376. {
  377. struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
  378. struct dsu_hw_events *hw_events = &dsu_pmu->hw_events;
  379. struct hw_perf_event *hwc = &event->hw;
  380. int idx;
  381. if (WARN_ON_ONCE(!cpumask_test_cpu(smp_processor_id(),
  382. &dsu_pmu->associated_cpus)))
  383. return -ENOENT;
  384. idx = dsu_pmu_get_event_idx(hw_events, event);
  385. if (idx < 0)
  386. return idx;
  387. hwc->idx = idx;
  388. hw_events->events[idx] = event;
  389. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  390. if (flags & PERF_EF_START)
  391. dsu_pmu_start(event, PERF_EF_RELOAD);
  392. perf_event_update_userpage(event);
  393. return 0;
  394. }
  395. static void dsu_pmu_del(struct perf_event *event, int flags)
  396. {
  397. struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
  398. struct dsu_hw_events *hw_events = &dsu_pmu->hw_events;
  399. struct hw_perf_event *hwc = &event->hw;
  400. int idx = hwc->idx;
  401. dsu_pmu_stop(event, PERF_EF_UPDATE);
  402. hw_events->events[idx] = NULL;
  403. clear_bit(idx, hw_events->used_mask);
  404. perf_event_update_userpage(event);
  405. }
  406. static void dsu_pmu_enable(struct pmu *pmu)
  407. {
  408. u32 pmcr;
  409. unsigned long flags;
  410. struct dsu_pmu *dsu_pmu = to_dsu_pmu(pmu);
  411. /* If no counters are added, skip enabling the PMU */
  412. if (bitmap_empty(dsu_pmu->hw_events.used_mask, DSU_PMU_MAX_HW_CNTRS))
  413. return;
  414. raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags);
  415. pmcr = __dsu_pmu_read_pmcr();
  416. pmcr |= CLUSTERPMCR_E;
  417. __dsu_pmu_write_pmcr(pmcr);
  418. raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags);
  419. }
  420. static void dsu_pmu_disable(struct pmu *pmu)
  421. {
  422. u32 pmcr;
  423. unsigned long flags;
  424. struct dsu_pmu *dsu_pmu = to_dsu_pmu(pmu);
  425. raw_spin_lock_irqsave(&dsu_pmu->pmu_lock, flags);
  426. pmcr = __dsu_pmu_read_pmcr();
  427. pmcr &= ~CLUSTERPMCR_E;
  428. __dsu_pmu_write_pmcr(pmcr);
  429. raw_spin_unlock_irqrestore(&dsu_pmu->pmu_lock, flags);
  430. }
  431. static bool dsu_pmu_validate_event(struct pmu *pmu,
  432. struct dsu_hw_events *hw_events,
  433. struct perf_event *event)
  434. {
  435. if (is_software_event(event))
  436. return true;
  437. /* Reject groups spanning multiple HW PMUs. */
  438. if (event->pmu != pmu)
  439. return false;
  440. return dsu_pmu_get_event_idx(hw_events, event) >= 0;
  441. }
  442. /*
  443. * Make sure the group of events can be scheduled at once
  444. * on the PMU.
  445. */
  446. static bool dsu_pmu_validate_group(struct perf_event *event)
  447. {
  448. struct perf_event *sibling, *leader = event->group_leader;
  449. struct dsu_hw_events fake_hw;
  450. if (event->group_leader == event)
  451. return true;
  452. memset(fake_hw.used_mask, 0, sizeof(fake_hw.used_mask));
  453. if (!dsu_pmu_validate_event(event->pmu, &fake_hw, leader))
  454. return false;
  455. for_each_sibling_event(sibling, leader) {
  456. if (!dsu_pmu_validate_event(event->pmu, &fake_hw, sibling))
  457. return false;
  458. }
  459. return dsu_pmu_validate_event(event->pmu, &fake_hw, event);
  460. }
  461. static int dsu_pmu_event_init(struct perf_event *event)
  462. {
  463. struct dsu_pmu *dsu_pmu = to_dsu_pmu(event->pmu);
  464. if (event->attr.type != event->pmu->type)
  465. return -ENOENT;
  466. /* We don't support sampling */
  467. if (is_sampling_event(event)) {
  468. dev_dbg(dsu_pmu->pmu.dev, "Can't support sampling events\n");
  469. return -EOPNOTSUPP;
  470. }
  471. /* We cannot support task bound events */
  472. if (event->cpu < 0 || event->attach_state & PERF_ATTACH_TASK) {
  473. dev_dbg(dsu_pmu->pmu.dev, "Can't support per-task counters\n");
  474. return -EINVAL;
  475. }
  476. if (has_branch_stack(event) ||
  477. event->attr.exclude_user ||
  478. event->attr.exclude_kernel ||
  479. event->attr.exclude_hv ||
  480. event->attr.exclude_idle ||
  481. event->attr.exclude_host ||
  482. event->attr.exclude_guest) {
  483. dev_dbg(dsu_pmu->pmu.dev, "Can't support filtering\n");
  484. return -EINVAL;
  485. }
  486. if (!cpumask_test_cpu(event->cpu, &dsu_pmu->associated_cpus)) {
  487. dev_dbg(dsu_pmu->pmu.dev,
  488. "Requested cpu is not associated with the DSU\n");
  489. return -EINVAL;
  490. }
  491. /*
  492. * Choose the current active CPU to read the events. We don't want
  493. * to migrate the event contexts, irq handling etc to the requested
  494. * CPU. As long as the requested CPU is within the same DSU, we
  495. * are fine.
  496. */
  497. event->cpu = cpumask_first(&dsu_pmu->active_cpu);
  498. if (event->cpu >= nr_cpu_ids)
  499. return -EINVAL;
  500. if (!dsu_pmu_validate_group(event))
  501. return -EINVAL;
  502. event->hw.config_base = event->attr.config;
  503. return 0;
  504. }
  505. static struct dsu_pmu *dsu_pmu_alloc(struct platform_device *pdev)
  506. {
  507. struct dsu_pmu *dsu_pmu;
  508. dsu_pmu = devm_kzalloc(&pdev->dev, sizeof(*dsu_pmu), GFP_KERNEL);
  509. if (!dsu_pmu)
  510. return ERR_PTR(-ENOMEM);
  511. raw_spin_lock_init(&dsu_pmu->pmu_lock);
  512. /*
  513. * Initialise the number of counters to -1, until we probe
  514. * the real number on a connected CPU.
  515. */
  516. dsu_pmu->num_counters = -1;
  517. return dsu_pmu;
  518. }
  519. /**
  520. * dsu_pmu_dt_get_cpus: Get the list of CPUs in the cluster.
  521. */
  522. static int dsu_pmu_dt_get_cpus(struct device_node *dev, cpumask_t *mask)
  523. {
  524. int i = 0, n, cpu;
  525. struct device_node *cpu_node;
  526. n = of_count_phandle_with_args(dev, "cpus", NULL);
  527. if (n <= 0)
  528. return -ENODEV;
  529. for (; i < n; i++) {
  530. cpu_node = of_parse_phandle(dev, "cpus", i);
  531. if (!cpu_node)
  532. break;
  533. cpu = of_cpu_node_to_id(cpu_node);
  534. of_node_put(cpu_node);
  535. /*
  536. * We have to ignore the failures here and continue scanning
  537. * the list to handle cases where the nr_cpus could be capped
  538. * in the running kernel.
  539. */
  540. if (cpu < 0)
  541. continue;
  542. cpumask_set_cpu(cpu, mask);
  543. }
  544. return 0;
  545. }
  546. /*
  547. * dsu_pmu_probe_pmu: Probe the PMU details on a CPU in the cluster.
  548. */
  549. static void dsu_pmu_probe_pmu(struct dsu_pmu *dsu_pmu)
  550. {
  551. u64 num_counters;
  552. u32 cpmceid[2];
  553. num_counters = (__dsu_pmu_read_pmcr() >> CLUSTERPMCR_N_SHIFT) &
  554. CLUSTERPMCR_N_MASK;
  555. /* We can only support up to 31 independent counters */
  556. if (WARN_ON(num_counters > 31))
  557. num_counters = 31;
  558. dsu_pmu->num_counters = num_counters;
  559. if (!dsu_pmu->num_counters)
  560. return;
  561. cpmceid[0] = __dsu_pmu_read_pmceid(0);
  562. cpmceid[1] = __dsu_pmu_read_pmceid(1);
  563. bitmap_from_arr32(dsu_pmu->cpmceid_bitmap, cpmceid,
  564. DSU_PMU_MAX_COMMON_EVENTS);
  565. }
  566. static void dsu_pmu_set_active_cpu(int cpu, struct dsu_pmu *dsu_pmu)
  567. {
  568. cpumask_set_cpu(cpu, &dsu_pmu->active_cpu);
  569. if (irq_set_affinity_hint(dsu_pmu->irq, &dsu_pmu->active_cpu))
  570. pr_warn("Failed to set irq affinity to %d\n", cpu);
  571. }
  572. /*
  573. * dsu_pmu_init_pmu: Initialise the DSU PMU configurations if
  574. * we haven't done it already.
  575. */
  576. static void dsu_pmu_init_pmu(struct dsu_pmu *dsu_pmu)
  577. {
  578. if (dsu_pmu->num_counters == -1)
  579. dsu_pmu_probe_pmu(dsu_pmu);
  580. /* Reset the interrupt overflow mask */
  581. dsu_pmu_get_reset_overflow();
  582. }
  583. static int dsu_pmu_device_probe(struct platform_device *pdev)
  584. {
  585. int irq, rc;
  586. struct dsu_pmu *dsu_pmu;
  587. char *name;
  588. static atomic_t pmu_idx = ATOMIC_INIT(-1);
  589. dsu_pmu = dsu_pmu_alloc(pdev);
  590. if (IS_ERR(dsu_pmu))
  591. return PTR_ERR(dsu_pmu);
  592. rc = dsu_pmu_dt_get_cpus(pdev->dev.of_node, &dsu_pmu->associated_cpus);
  593. if (rc) {
  594. dev_warn(&pdev->dev, "Failed to parse the CPUs\n");
  595. return rc;
  596. }
  597. irq = platform_get_irq(pdev, 0);
  598. if (irq < 0) {
  599. dev_warn(&pdev->dev, "Failed to find IRQ\n");
  600. return -EINVAL;
  601. }
  602. name = devm_kasprintf(&pdev->dev, GFP_KERNEL, "%s_%d",
  603. PMUNAME, atomic_inc_return(&pmu_idx));
  604. if (!name)
  605. return -ENOMEM;
  606. rc = devm_request_irq(&pdev->dev, irq, dsu_pmu_handle_irq,
  607. IRQF_NOBALANCING, name, dsu_pmu);
  608. if (rc) {
  609. dev_warn(&pdev->dev, "Failed to request IRQ %d\n", irq);
  610. return rc;
  611. }
  612. dsu_pmu->irq = irq;
  613. platform_set_drvdata(pdev, dsu_pmu);
  614. rc = cpuhp_state_add_instance(dsu_pmu_cpuhp_state,
  615. &dsu_pmu->cpuhp_node);
  616. if (rc)
  617. return rc;
  618. dsu_pmu->pmu = (struct pmu) {
  619. .task_ctx_nr = perf_invalid_context,
  620. .module = THIS_MODULE,
  621. .pmu_enable = dsu_pmu_enable,
  622. .pmu_disable = dsu_pmu_disable,
  623. .event_init = dsu_pmu_event_init,
  624. .add = dsu_pmu_add,
  625. .del = dsu_pmu_del,
  626. .start = dsu_pmu_start,
  627. .stop = dsu_pmu_stop,
  628. .read = dsu_pmu_read,
  629. .attr_groups = dsu_pmu_attr_groups,
  630. };
  631. rc = perf_pmu_register(&dsu_pmu->pmu, name, -1);
  632. if (rc) {
  633. cpuhp_state_remove_instance(dsu_pmu_cpuhp_state,
  634. &dsu_pmu->cpuhp_node);
  635. irq_set_affinity_hint(dsu_pmu->irq, NULL);
  636. }
  637. return rc;
  638. }
  639. static int dsu_pmu_device_remove(struct platform_device *pdev)
  640. {
  641. struct dsu_pmu *dsu_pmu = platform_get_drvdata(pdev);
  642. perf_pmu_unregister(&dsu_pmu->pmu);
  643. cpuhp_state_remove_instance(dsu_pmu_cpuhp_state, &dsu_pmu->cpuhp_node);
  644. irq_set_affinity_hint(dsu_pmu->irq, NULL);
  645. return 0;
  646. }
  647. static const struct of_device_id dsu_pmu_of_match[] = {
  648. { .compatible = "arm,dsu-pmu", },
  649. {},
  650. };
  651. static struct platform_driver dsu_pmu_driver = {
  652. .driver = {
  653. .name = DRVNAME,
  654. .of_match_table = of_match_ptr(dsu_pmu_of_match),
  655. },
  656. .probe = dsu_pmu_device_probe,
  657. .remove = dsu_pmu_device_remove,
  658. };
  659. static int dsu_pmu_cpu_online(unsigned int cpu, struct hlist_node *node)
  660. {
  661. struct dsu_pmu *dsu_pmu = hlist_entry_safe(node, struct dsu_pmu,
  662. cpuhp_node);
  663. if (!cpumask_test_cpu(cpu, &dsu_pmu->associated_cpus))
  664. return 0;
  665. /* If the PMU is already managed, there is nothing to do */
  666. if (!cpumask_empty(&dsu_pmu->active_cpu))
  667. return 0;
  668. dsu_pmu_init_pmu(dsu_pmu);
  669. dsu_pmu_set_active_cpu(cpu, dsu_pmu);
  670. return 0;
  671. }
  672. static int dsu_pmu_cpu_teardown(unsigned int cpu, struct hlist_node *node)
  673. {
  674. int dst;
  675. struct dsu_pmu *dsu_pmu = hlist_entry_safe(node, struct dsu_pmu,
  676. cpuhp_node);
  677. if (!cpumask_test_and_clear_cpu(cpu, &dsu_pmu->active_cpu))
  678. return 0;
  679. dst = dsu_pmu_get_online_cpu_any_but(dsu_pmu, cpu);
  680. /* If there are no active CPUs in the DSU, leave IRQ disabled */
  681. if (dst >= nr_cpu_ids) {
  682. irq_set_affinity_hint(dsu_pmu->irq, NULL);
  683. return 0;
  684. }
  685. perf_pmu_migrate_context(&dsu_pmu->pmu, cpu, dst);
  686. dsu_pmu_set_active_cpu(dst, dsu_pmu);
  687. return 0;
  688. }
  689. static int __init dsu_pmu_init(void)
  690. {
  691. int ret;
  692. ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN,
  693. DRVNAME,
  694. dsu_pmu_cpu_online,
  695. dsu_pmu_cpu_teardown);
  696. if (ret < 0)
  697. return ret;
  698. dsu_pmu_cpuhp_state = ret;
  699. return platform_driver_register(&dsu_pmu_driver);
  700. }
  701. static void __exit dsu_pmu_exit(void)
  702. {
  703. platform_driver_unregister(&dsu_pmu_driver);
  704. cpuhp_remove_multi_state(dsu_pmu_cpuhp_state);
  705. }
  706. module_init(dsu_pmu_init);
  707. module_exit(dsu_pmu_exit);
  708. MODULE_DEVICE_TABLE(of, dsu_pmu_of_match);
  709. MODULE_DESCRIPTION("Perf driver for ARM DynamIQ Shared Unit");
  710. MODULE_AUTHOR("Suzuki K Poulose <suzuki.poulose@arm.com>");
  711. MODULE_LICENSE("GPL v2");