arm_dsu_pmu.c 22 KB

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