arm_pmu.c 22 KB

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