arm_pmu.c 21 KB

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