arm_dsu_pmu.c 22 KB

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