arm_pmu.c 21 KB

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