arm_pmu.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #undef DEBUG
  3. /*
  4. * ARM performance counter support.
  5. *
  6. * Copyright (C) 2009 picoChip Designs, Ltd., Jamie Iles
  7. * Copyright (C) 2010 ARM Ltd., Will Deacon <will.deacon@arm.com>
  8. *
  9. * This code is based on the sparc64 perf event code, which is in turn based
  10. * on the x86 code.
  11. */
  12. #define pr_fmt(fmt) "hw perfevents: " fmt
  13. #include <linux/bitmap.h>
  14. #include <linux/cpumask.h>
  15. #include <linux/cpu_pm.h>
  16. #include <linux/export.h>
  17. #include <linux/kernel.h>
  18. #include <linux/perf/arm_pmu.h>
  19. #include <linux/slab.h>
  20. #include <linux/sched/clock.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/irq.h>
  23. #include <linux/irqdesc.h>
  24. #include <asm/irq_regs.h>
  25. static int armpmu_count_irq_users(const int irq);
  26. struct pmu_irq_ops {
  27. void (*enable_pmuirq)(unsigned int irq);
  28. void (*disable_pmuirq)(unsigned int irq);
  29. void (*free_pmuirq)(unsigned int irq, int cpu, void __percpu *devid);
  30. };
  31. static void armpmu_free_pmuirq(unsigned int irq, int cpu, void __percpu *devid)
  32. {
  33. free_irq(irq, per_cpu_ptr(devid, cpu));
  34. }
  35. static const struct pmu_irq_ops pmuirq_ops = {
  36. .enable_pmuirq = enable_irq,
  37. .disable_pmuirq = disable_irq_nosync,
  38. .free_pmuirq = armpmu_free_pmuirq
  39. };
  40. static void armpmu_free_pmunmi(unsigned int irq, int cpu, void __percpu *devid)
  41. {
  42. free_nmi(irq, per_cpu_ptr(devid, cpu));
  43. }
  44. static const struct pmu_irq_ops pmunmi_ops = {
  45. .enable_pmuirq = enable_nmi,
  46. .disable_pmuirq = disable_nmi_nosync,
  47. .free_pmuirq = armpmu_free_pmunmi
  48. };
  49. static void armpmu_enable_percpu_pmuirq(unsigned int irq)
  50. {
  51. enable_percpu_irq(irq, IRQ_TYPE_NONE);
  52. }
  53. static void armpmu_free_percpu_pmuirq(unsigned int irq, int cpu,
  54. void __percpu *devid)
  55. {
  56. if (armpmu_count_irq_users(irq) == 1)
  57. free_percpu_irq(irq, devid);
  58. }
  59. static const struct pmu_irq_ops percpu_pmuirq_ops = {
  60. .enable_pmuirq = armpmu_enable_percpu_pmuirq,
  61. .disable_pmuirq = disable_percpu_irq,
  62. .free_pmuirq = armpmu_free_percpu_pmuirq
  63. };
  64. static void armpmu_enable_percpu_pmunmi(unsigned int irq)
  65. {
  66. if (!prepare_percpu_nmi(irq))
  67. enable_percpu_nmi(irq, IRQ_TYPE_NONE);
  68. }
  69. static void armpmu_disable_percpu_pmunmi(unsigned int irq)
  70. {
  71. disable_percpu_nmi(irq);
  72. teardown_percpu_nmi(irq);
  73. }
  74. static void armpmu_free_percpu_pmunmi(unsigned int irq, int cpu,
  75. void __percpu *devid)
  76. {
  77. if (armpmu_count_irq_users(irq) == 1)
  78. free_percpu_nmi(irq, devid);
  79. }
  80. static const struct pmu_irq_ops percpu_pmunmi_ops = {
  81. .enable_pmuirq = armpmu_enable_percpu_pmunmi,
  82. .disable_pmuirq = armpmu_disable_percpu_pmunmi,
  83. .free_pmuirq = armpmu_free_percpu_pmunmi
  84. };
  85. static DEFINE_PER_CPU(struct arm_pmu *, cpu_armpmu);
  86. static DEFINE_PER_CPU(int, cpu_irq);
  87. static DEFINE_PER_CPU(const struct pmu_irq_ops *, cpu_irq_ops);
  88. static bool has_nmi;
  89. static inline u64 arm_pmu_event_max_period(struct perf_event *event)
  90. {
  91. if (event->hw.flags & ARMPMU_EVT_64BIT)
  92. return GENMASK_ULL(63, 0);
  93. else if (event->hw.flags & ARMPMU_EVT_63BIT)
  94. return GENMASK_ULL(62, 0);
  95. else if (event->hw.flags & ARMPMU_EVT_47BIT)
  96. return GENMASK_ULL(46, 0);
  97. else
  98. return GENMASK_ULL(31, 0);
  99. }
  100. static int
  101. armpmu_map_cache_event(const unsigned (*cache_map)
  102. [PERF_COUNT_HW_CACHE_MAX]
  103. [PERF_COUNT_HW_CACHE_OP_MAX]
  104. [PERF_COUNT_HW_CACHE_RESULT_MAX],
  105. u64 config)
  106. {
  107. unsigned int cache_type, cache_op, cache_result, ret;
  108. cache_type = (config >> 0) & 0xff;
  109. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  110. return -EINVAL;
  111. cache_op = (config >> 8) & 0xff;
  112. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  113. return -EINVAL;
  114. cache_result = (config >> 16) & 0xff;
  115. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  116. return -EINVAL;
  117. if (!cache_map)
  118. return -ENOENT;
  119. ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
  120. if (ret == CACHE_OP_UNSUPPORTED)
  121. return -ENOENT;
  122. return ret;
  123. }
  124. static int
  125. armpmu_map_hw_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
  126. {
  127. int mapping;
  128. if (config >= PERF_COUNT_HW_MAX)
  129. return -EINVAL;
  130. if (!event_map)
  131. return -ENOENT;
  132. mapping = (*event_map)[config];
  133. return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
  134. }
  135. static int
  136. armpmu_map_raw_event(u32 raw_event_mask, u64 config)
  137. {
  138. return (int)(config & raw_event_mask);
  139. }
  140. int
  141. armpmu_map_event(struct perf_event *event,
  142. const unsigned (*event_map)[PERF_COUNT_HW_MAX],
  143. const unsigned (*cache_map)
  144. [PERF_COUNT_HW_CACHE_MAX]
  145. [PERF_COUNT_HW_CACHE_OP_MAX]
  146. [PERF_COUNT_HW_CACHE_RESULT_MAX],
  147. u32 raw_event_mask)
  148. {
  149. u64 config = event->attr.config;
  150. int type = event->attr.type;
  151. if (type == event->pmu->type)
  152. return armpmu_map_raw_event(raw_event_mask, config);
  153. switch (type) {
  154. case PERF_TYPE_HARDWARE:
  155. return armpmu_map_hw_event(event_map, config);
  156. case PERF_TYPE_HW_CACHE:
  157. return armpmu_map_cache_event(cache_map, config);
  158. case PERF_TYPE_RAW:
  159. return armpmu_map_raw_event(raw_event_mask, config);
  160. }
  161. return -ENOENT;
  162. }
  163. int armpmu_event_set_period(struct perf_event *event)
  164. {
  165. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  166. struct hw_perf_event *hwc = &event->hw;
  167. s64 left = local64_read(&hwc->period_left);
  168. s64 period = hwc->sample_period;
  169. u64 max_period;
  170. int ret = 0;
  171. max_period = arm_pmu_event_max_period(event);
  172. if (unlikely(left <= -period)) {
  173. left = period;
  174. local64_set(&hwc->period_left, left);
  175. hwc->last_period = period;
  176. ret = 1;
  177. }
  178. if (unlikely(left <= 0)) {
  179. left += period;
  180. local64_set(&hwc->period_left, left);
  181. hwc->last_period = period;
  182. ret = 1;
  183. }
  184. /*
  185. * Limit the maximum period to prevent the counter value
  186. * from overtaking the one we are about to program. In
  187. * effect we are reducing max_period to account for
  188. * interrupt latency (and we are being very conservative).
  189. */
  190. if (left > (max_period >> 1))
  191. left = (max_period >> 1);
  192. local64_set(&hwc->prev_count, (u64)-left);
  193. armpmu->write_counter(event, (u64)(-left) & max_period);
  194. perf_event_update_userpage(event);
  195. return ret;
  196. }
  197. u64 armpmu_event_update(struct perf_event *event)
  198. {
  199. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  200. struct hw_perf_event *hwc = &event->hw;
  201. u64 delta, prev_raw_count, new_raw_count;
  202. u64 max_period = arm_pmu_event_max_period(event);
  203. again:
  204. prev_raw_count = local64_read(&hwc->prev_count);
  205. new_raw_count = armpmu->read_counter(event);
  206. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  207. new_raw_count) != prev_raw_count)
  208. goto again;
  209. delta = (new_raw_count - prev_raw_count) & max_period;
  210. local64_add(delta, &event->count);
  211. local64_sub(delta, &hwc->period_left);
  212. return new_raw_count;
  213. }
  214. static void
  215. armpmu_read(struct perf_event *event)
  216. {
  217. armpmu_event_update(event);
  218. }
  219. static void
  220. armpmu_stop(struct perf_event *event, int flags)
  221. {
  222. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  223. struct hw_perf_event *hwc = &event->hw;
  224. /*
  225. * ARM pmu always has to update the counter, so ignore
  226. * PERF_EF_UPDATE, see comments in armpmu_start().
  227. */
  228. if (!(hwc->state & PERF_HES_STOPPED)) {
  229. armpmu->disable(event);
  230. armpmu_event_update(event);
  231. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  232. }
  233. }
  234. static void armpmu_start(struct perf_event *event, int flags)
  235. {
  236. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  237. struct hw_perf_event *hwc = &event->hw;
  238. /*
  239. * ARM pmu always has to reprogram the period, so ignore
  240. * PERF_EF_RELOAD, see the comment below.
  241. */
  242. if (flags & PERF_EF_RELOAD)
  243. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  244. hwc->state = 0;
  245. /*
  246. * Set the period again. Some counters can't be stopped, so when we
  247. * were stopped we simply disabled the IRQ source and the counter
  248. * may have been left counting. If we don't do this step then we may
  249. * get an interrupt too soon or *way* too late if the overflow has
  250. * happened since disabling.
  251. */
  252. armpmu_event_set_period(event);
  253. armpmu->enable(event);
  254. }
  255. static void
  256. armpmu_del(struct perf_event *event, int flags)
  257. {
  258. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  259. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  260. struct hw_perf_event *hwc = &event->hw;
  261. int idx = hwc->idx;
  262. armpmu_stop(event, PERF_EF_UPDATE);
  263. hw_events->events[idx] = NULL;
  264. armpmu->clear_event_idx(hw_events, event);
  265. perf_event_update_userpage(event);
  266. /* Clear the allocated counter */
  267. hwc->idx = -1;
  268. }
  269. static int
  270. armpmu_add(struct perf_event *event, int flags)
  271. {
  272. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  273. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  274. struct hw_perf_event *hwc = &event->hw;
  275. int idx;
  276. /* An event following a process won't be stopped earlier */
  277. if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
  278. return -ENOENT;
  279. /* If we don't have a space for the counter then finish early. */
  280. idx = armpmu->get_event_idx(hw_events, event);
  281. if (idx < 0)
  282. return idx;
  283. /* The newly-allocated counter should be empty */
  284. WARN_ON_ONCE(hw_events->events[idx]);
  285. event->hw.idx = idx;
  286. hw_events->events[idx] = event;
  287. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  288. if (flags & PERF_EF_START)
  289. armpmu_start(event, PERF_EF_RELOAD);
  290. /* Propagate our changes to the userspace mapping. */
  291. perf_event_update_userpage(event);
  292. return 0;
  293. }
  294. static int
  295. validate_event(struct pmu *pmu, struct pmu_hw_events *hw_events,
  296. struct perf_event *event)
  297. {
  298. struct arm_pmu *armpmu;
  299. if (is_software_event(event))
  300. return 1;
  301. /*
  302. * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
  303. * core perf code won't check that the pmu->ctx == leader->ctx
  304. * until after pmu->event_init(event).
  305. */
  306. if (event->pmu != pmu)
  307. return 0;
  308. if (event->state < PERF_EVENT_STATE_OFF)
  309. return 1;
  310. if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
  311. return 1;
  312. armpmu = to_arm_pmu(event->pmu);
  313. return armpmu->get_event_idx(hw_events, event) >= 0;
  314. }
  315. static int
  316. validate_group(struct perf_event *event)
  317. {
  318. struct perf_event *sibling, *leader = event->group_leader;
  319. struct pmu_hw_events fake_pmu;
  320. /*
  321. * Initialise the fake PMU. We only need to populate the
  322. * used_mask for the purposes of validation.
  323. */
  324. memset(&fake_pmu.used_mask, 0, sizeof(fake_pmu.used_mask));
  325. if (!validate_event(event->pmu, &fake_pmu, leader))
  326. return -EINVAL;
  327. if (event == leader)
  328. return 0;
  329. for_each_sibling_event(sibling, leader) {
  330. if (!validate_event(event->pmu, &fake_pmu, sibling))
  331. return -EINVAL;
  332. }
  333. if (!validate_event(event->pmu, &fake_pmu, event))
  334. return -EINVAL;
  335. return 0;
  336. }
  337. static irqreturn_t armpmu_dispatch_irq(int irq, void *dev)
  338. {
  339. struct arm_pmu *armpmu;
  340. int ret;
  341. u64 start_clock, finish_clock;
  342. /*
  343. * we request the IRQ with a (possibly percpu) struct arm_pmu**, but
  344. * the handlers expect a struct arm_pmu*. The percpu_irq framework will
  345. * do any necessary shifting, we just need to perform the first
  346. * dereference.
  347. */
  348. armpmu = *(void **)dev;
  349. if (WARN_ON_ONCE(!armpmu))
  350. return IRQ_NONE;
  351. start_clock = sched_clock();
  352. ret = armpmu->handle_irq(armpmu);
  353. finish_clock = sched_clock();
  354. perf_sample_event_took(finish_clock - start_clock);
  355. return ret;
  356. }
  357. static int
  358. __hw_perf_event_init(struct perf_event *event)
  359. {
  360. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  361. struct hw_perf_event *hwc = &event->hw;
  362. int mapping, ret;
  363. hwc->flags = 0;
  364. mapping = armpmu->map_event(event);
  365. if (mapping < 0) {
  366. pr_debug("event %x:%llx not supported\n", event->attr.type,
  367. event->attr.config);
  368. return mapping;
  369. }
  370. /*
  371. * We don't assign an index until we actually place the event onto
  372. * hardware. Use -1 to signify that we haven't decided where to put it
  373. * yet. For SMP systems, each core has it's own PMU so we can't do any
  374. * clever allocation or constraints checking at this point.
  375. */
  376. hwc->idx = -1;
  377. hwc->config_base = 0;
  378. hwc->config = 0;
  379. hwc->event_base = 0;
  380. /*
  381. * Check whether we need to exclude the counter from certain modes.
  382. */
  383. if (armpmu->set_event_filter) {
  384. ret = armpmu->set_event_filter(hwc, &event->attr);
  385. if (ret)
  386. return ret;
  387. }
  388. /*
  389. * Store the event encoding into the config_base field.
  390. */
  391. hwc->config_base |= (unsigned long)mapping;
  392. if (!is_sampling_event(event)) {
  393. /*
  394. * For non-sampling runs, limit the sample_period to half
  395. * of the counter width. That way, the new counter value
  396. * is far less likely to overtake the previous one unless
  397. * you have some serious IRQ latency issues.
  398. */
  399. hwc->sample_period = arm_pmu_event_max_period(event) >> 1;
  400. hwc->last_period = hwc->sample_period;
  401. local64_set(&hwc->period_left, hwc->sample_period);
  402. }
  403. return validate_group(event);
  404. }
  405. static int armpmu_event_init(struct perf_event *event)
  406. {
  407. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  408. /*
  409. * Reject CPU-affine events for CPUs that are of a different class to
  410. * that which this PMU handles. Process-following events (where
  411. * event->cpu == -1) can be migrated between CPUs, and thus we have to
  412. * reject them later (in armpmu_add) if they're scheduled on a
  413. * different class of CPU.
  414. */
  415. if (event->cpu != -1 &&
  416. !cpumask_test_cpu(event->cpu, &armpmu->supported_cpus))
  417. return -ENOENT;
  418. /* does not support taken branch sampling */
  419. if (has_branch_stack(event))
  420. return -EOPNOTSUPP;
  421. return __hw_perf_event_init(event);
  422. }
  423. static void armpmu_enable(struct pmu *pmu)
  424. {
  425. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  426. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  427. bool enabled = !bitmap_empty(hw_events->used_mask, ARMPMU_MAX_HWEVENTS);
  428. /* For task-bound events we may be called on other CPUs */
  429. if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
  430. return;
  431. if (enabled)
  432. armpmu->start(armpmu);
  433. }
  434. static void armpmu_disable(struct pmu *pmu)
  435. {
  436. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  437. /* For task-bound events we may be called on other CPUs */
  438. if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
  439. return;
  440. armpmu->stop(armpmu);
  441. }
  442. /*
  443. * In heterogeneous systems, events are specific to a particular
  444. * microarchitecture, and aren't suitable for another. Thus, only match CPUs of
  445. * the same microarchitecture.
  446. */
  447. static bool armpmu_filter(struct pmu *pmu, int cpu)
  448. {
  449. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  450. return !cpumask_test_cpu(cpu, &armpmu->supported_cpus);
  451. }
  452. static ssize_t cpus_show(struct device *dev,
  453. struct device_attribute *attr, char *buf)
  454. {
  455. struct arm_pmu *armpmu = to_arm_pmu(dev_get_drvdata(dev));
  456. return cpumap_print_to_pagebuf(true, buf, &armpmu->supported_cpus);
  457. }
  458. static DEVICE_ATTR_RO(cpus);
  459. static struct attribute *armpmu_common_attrs[] = {
  460. &dev_attr_cpus.attr,
  461. NULL,
  462. };
  463. static const struct attribute_group armpmu_common_attr_group = {
  464. .attrs = armpmu_common_attrs,
  465. };
  466. static int armpmu_count_irq_users(const int irq)
  467. {
  468. int cpu, count = 0;
  469. for_each_possible_cpu(cpu) {
  470. if (per_cpu(cpu_irq, cpu) == irq)
  471. count++;
  472. }
  473. return count;
  474. }
  475. static const struct pmu_irq_ops *armpmu_find_irq_ops(int irq)
  476. {
  477. const struct pmu_irq_ops *ops = NULL;
  478. int cpu;
  479. for_each_possible_cpu(cpu) {
  480. if (per_cpu(cpu_irq, cpu) != irq)
  481. continue;
  482. ops = per_cpu(cpu_irq_ops, cpu);
  483. if (ops)
  484. break;
  485. }
  486. return ops;
  487. }
  488. void armpmu_free_irq(int irq, int cpu)
  489. {
  490. if (per_cpu(cpu_irq, cpu) == 0)
  491. return;
  492. if (WARN_ON(irq != per_cpu(cpu_irq, cpu)))
  493. return;
  494. per_cpu(cpu_irq_ops, cpu)->free_pmuirq(irq, cpu, &cpu_armpmu);
  495. per_cpu(cpu_irq, cpu) = 0;
  496. per_cpu(cpu_irq_ops, cpu) = NULL;
  497. }
  498. int armpmu_request_irq(int irq, int cpu)
  499. {
  500. int err = 0;
  501. const irq_handler_t handler = armpmu_dispatch_irq;
  502. const struct pmu_irq_ops *irq_ops;
  503. if (!irq)
  504. return 0;
  505. if (!irq_is_percpu_devid(irq)) {
  506. unsigned long irq_flags;
  507. err = irq_force_affinity(irq, cpumask_of(cpu));
  508. if (err && num_possible_cpus() > 1) {
  509. pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
  510. irq, cpu);
  511. goto err_out;
  512. }
  513. irq_flags = IRQF_PERCPU |
  514. IRQF_NOBALANCING | IRQF_NO_AUTOEN |
  515. IRQF_NO_THREAD;
  516. err = request_nmi(irq, handler, irq_flags, "arm-pmu",
  517. per_cpu_ptr(&cpu_armpmu, cpu));
  518. /* If cannot get an NMI, get a normal interrupt */
  519. if (err) {
  520. err = request_irq(irq, handler, irq_flags, "arm-pmu",
  521. per_cpu_ptr(&cpu_armpmu, cpu));
  522. irq_ops = &pmuirq_ops;
  523. } else {
  524. has_nmi = true;
  525. irq_ops = &pmunmi_ops;
  526. }
  527. } else if (armpmu_count_irq_users(irq) == 0) {
  528. err = request_percpu_nmi(irq, handler, "arm-pmu", &cpu_armpmu);
  529. /* If cannot get an NMI, get a normal interrupt */
  530. if (err) {
  531. err = request_percpu_irq(irq, handler, "arm-pmu",
  532. &cpu_armpmu);
  533. irq_ops = &percpu_pmuirq_ops;
  534. } else {
  535. has_nmi = true;
  536. irq_ops = &percpu_pmunmi_ops;
  537. }
  538. } else {
  539. /* Per cpudevid irq was already requested by another CPU */
  540. irq_ops = armpmu_find_irq_ops(irq);
  541. if (WARN_ON(!irq_ops))
  542. err = -EINVAL;
  543. }
  544. if (err)
  545. goto err_out;
  546. per_cpu(cpu_irq, cpu) = irq;
  547. per_cpu(cpu_irq_ops, cpu) = irq_ops;
  548. return 0;
  549. err_out:
  550. pr_err("unable to request IRQ%d for ARM PMU counters\n", irq);
  551. return err;
  552. }
  553. static int armpmu_get_cpu_irq(struct arm_pmu *pmu, int cpu)
  554. {
  555. struct pmu_hw_events __percpu *hw_events = pmu->hw_events;
  556. return per_cpu(hw_events->irq, cpu);
  557. }
  558. bool arm_pmu_irq_is_nmi(void)
  559. {
  560. return has_nmi;
  561. }
  562. /*
  563. * PMU hardware loses all context when a CPU goes offline.
  564. * When a CPU is hotplugged back in, since some hardware registers are
  565. * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
  566. * junk values out of them.
  567. */
  568. static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
  569. {
  570. struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
  571. int irq;
  572. if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
  573. return 0;
  574. if (pmu->reset)
  575. pmu->reset(pmu);
  576. per_cpu(cpu_armpmu, cpu) = pmu;
  577. irq = armpmu_get_cpu_irq(pmu, cpu);
  578. if (irq)
  579. per_cpu(cpu_irq_ops, cpu)->enable_pmuirq(irq);
  580. return 0;
  581. }
  582. static int arm_perf_teardown_cpu(unsigned int cpu, struct hlist_node *node)
  583. {
  584. struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
  585. int irq;
  586. if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
  587. return 0;
  588. irq = armpmu_get_cpu_irq(pmu, cpu);
  589. if (irq)
  590. per_cpu(cpu_irq_ops, cpu)->disable_pmuirq(irq);
  591. per_cpu(cpu_armpmu, cpu) = NULL;
  592. return 0;
  593. }
  594. #ifdef CONFIG_CPU_PM
  595. static void cpu_pm_pmu_setup(struct arm_pmu *armpmu, unsigned long cmd)
  596. {
  597. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  598. struct perf_event *event;
  599. int idx;
  600. for_each_set_bit(idx, armpmu->cntr_mask, ARMPMU_MAX_HWEVENTS) {
  601. event = hw_events->events[idx];
  602. if (!event)
  603. continue;
  604. switch (cmd) {
  605. case CPU_PM_ENTER:
  606. /*
  607. * Stop and update the counter
  608. */
  609. armpmu_stop(event, PERF_EF_UPDATE);
  610. break;
  611. case CPU_PM_EXIT:
  612. case CPU_PM_ENTER_FAILED:
  613. /*
  614. * Restore and enable the counter.
  615. */
  616. armpmu_start(event, PERF_EF_RELOAD);
  617. break;
  618. default:
  619. break;
  620. }
  621. }
  622. }
  623. static int cpu_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
  624. void *v)
  625. {
  626. struct arm_pmu *armpmu = container_of(b, struct arm_pmu, cpu_pm_nb);
  627. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  628. bool enabled = !bitmap_empty(hw_events->used_mask, ARMPMU_MAX_HWEVENTS);
  629. if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
  630. return NOTIFY_DONE;
  631. /*
  632. * Always reset the PMU registers on power-up even if
  633. * there are no events running.
  634. */
  635. if (cmd == CPU_PM_EXIT && armpmu->reset)
  636. armpmu->reset(armpmu);
  637. if (!enabled)
  638. return NOTIFY_OK;
  639. switch (cmd) {
  640. case CPU_PM_ENTER:
  641. armpmu->stop(armpmu);
  642. cpu_pm_pmu_setup(armpmu, cmd);
  643. break;
  644. case CPU_PM_EXIT:
  645. case CPU_PM_ENTER_FAILED:
  646. cpu_pm_pmu_setup(armpmu, cmd);
  647. armpmu->start(armpmu);
  648. break;
  649. default:
  650. return NOTIFY_DONE;
  651. }
  652. return NOTIFY_OK;
  653. }
  654. static int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu)
  655. {
  656. cpu_pmu->cpu_pm_nb.notifier_call = cpu_pm_pmu_notify;
  657. return cpu_pm_register_notifier(&cpu_pmu->cpu_pm_nb);
  658. }
  659. static void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu)
  660. {
  661. cpu_pm_unregister_notifier(&cpu_pmu->cpu_pm_nb);
  662. }
  663. #else
  664. static inline int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu) { return 0; }
  665. static inline void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu) { }
  666. #endif
  667. static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
  668. {
  669. int err;
  670. err = cpuhp_state_add_instance(CPUHP_AP_PERF_ARM_STARTING,
  671. &cpu_pmu->node);
  672. if (err)
  673. goto out;
  674. err = cpu_pm_pmu_register(cpu_pmu);
  675. if (err)
  676. goto out_unregister;
  677. return 0;
  678. out_unregister:
  679. cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
  680. &cpu_pmu->node);
  681. out:
  682. return err;
  683. }
  684. static void cpu_pmu_destroy(struct arm_pmu *cpu_pmu)
  685. {
  686. cpu_pm_pmu_unregister(cpu_pmu);
  687. cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
  688. &cpu_pmu->node);
  689. }
  690. struct arm_pmu *armpmu_alloc(void)
  691. {
  692. struct arm_pmu *pmu;
  693. int cpu;
  694. pmu = kzalloc(sizeof(*pmu), GFP_KERNEL);
  695. if (!pmu)
  696. goto out;
  697. pmu->hw_events = alloc_percpu_gfp(struct pmu_hw_events, GFP_KERNEL);
  698. if (!pmu->hw_events) {
  699. pr_info("failed to allocate per-cpu PMU data.\n");
  700. goto out_free_pmu;
  701. }
  702. pmu->pmu = (struct pmu) {
  703. .pmu_enable = armpmu_enable,
  704. .pmu_disable = armpmu_disable,
  705. .event_init = armpmu_event_init,
  706. .add = armpmu_add,
  707. .del = armpmu_del,
  708. .start = armpmu_start,
  709. .stop = armpmu_stop,
  710. .read = armpmu_read,
  711. .filter = armpmu_filter,
  712. .attr_groups = pmu->attr_groups,
  713. /*
  714. * This is a CPU PMU potentially in a heterogeneous
  715. * configuration (e.g. big.LITTLE) so
  716. * PERF_PMU_CAP_EXTENDED_HW_TYPE is required to open
  717. * PERF_TYPE_HARDWARE and PERF_TYPE_HW_CACHE events on a
  718. * specific PMU.
  719. */
  720. .capabilities = PERF_PMU_CAP_EXTENDED_REGS |
  721. PERF_PMU_CAP_EXTENDED_HW_TYPE,
  722. };
  723. pmu->attr_groups[ARMPMU_ATTR_GROUP_COMMON] =
  724. &armpmu_common_attr_group;
  725. for_each_possible_cpu(cpu) {
  726. struct pmu_hw_events *events;
  727. events = per_cpu_ptr(pmu->hw_events, cpu);
  728. events->percpu_pmu = pmu;
  729. }
  730. return pmu;
  731. out_free_pmu:
  732. kfree(pmu);
  733. out:
  734. return NULL;
  735. }
  736. void armpmu_free(struct arm_pmu *pmu)
  737. {
  738. free_percpu(pmu->hw_events);
  739. kfree(pmu);
  740. }
  741. int armpmu_register(struct arm_pmu *pmu)
  742. {
  743. int ret;
  744. ret = cpu_pmu_init(pmu);
  745. if (ret)
  746. return ret;
  747. if (!pmu->set_event_filter)
  748. pmu->pmu.capabilities |= PERF_PMU_CAP_NO_EXCLUDE;
  749. ret = perf_pmu_register(&pmu->pmu, pmu->name, -1);
  750. if (ret)
  751. goto out_destroy;
  752. pr_info("enabled with %s PMU driver, %d (%*pb) counters available%s\n",
  753. pmu->name, bitmap_weight(pmu->cntr_mask, ARMPMU_MAX_HWEVENTS),
  754. ARMPMU_MAX_HWEVENTS, &pmu->cntr_mask,
  755. has_nmi ? ", using NMIs" : "");
  756. kvm_host_pmu_init(pmu);
  757. return 0;
  758. out_destroy:
  759. cpu_pmu_destroy(pmu);
  760. return ret;
  761. }
  762. static int arm_pmu_hp_init(void)
  763. {
  764. int ret;
  765. ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_STARTING,
  766. "perf/arm/pmu:starting",
  767. arm_perf_starting_cpu,
  768. arm_perf_teardown_cpu);
  769. if (ret)
  770. pr_err("CPU hotplug notifier for ARM PMU could not be registered: %d\n",
  771. ret);
  772. return ret;
  773. }
  774. subsys_initcall(arm_pmu_hp_init);