arm_dsu_pmu.c 22 KB

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