arm_pmu.c 21 KB

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