arm_pmu.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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/of_device.h>
  18. #include <linux/perf/arm_pmu.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/sched/clock.h>
  22. #include <linux/spinlock.h>
  23. #include <linux/irq.h>
  24. #include <linux/irqdesc.h>
  25. #include <asm/cputype.h>
  26. #include <asm/irq_regs.h>
  27. static int
  28. armpmu_map_cache_event(const unsigned (*cache_map)
  29. [PERF_COUNT_HW_CACHE_MAX]
  30. [PERF_COUNT_HW_CACHE_OP_MAX]
  31. [PERF_COUNT_HW_CACHE_RESULT_MAX],
  32. u64 config)
  33. {
  34. unsigned int cache_type, cache_op, cache_result, ret;
  35. cache_type = (config >> 0) & 0xff;
  36. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  37. return -EINVAL;
  38. cache_op = (config >> 8) & 0xff;
  39. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  40. return -EINVAL;
  41. cache_result = (config >> 16) & 0xff;
  42. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  43. return -EINVAL;
  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. mapping = (*event_map)[config];
  56. return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
  57. }
  58. static int
  59. armpmu_map_raw_event(u32 raw_event_mask, u64 config)
  60. {
  61. return (int)(config & raw_event_mask);
  62. }
  63. int
  64. armpmu_map_event(struct perf_event *event,
  65. const unsigned (*event_map)[PERF_COUNT_HW_MAX],
  66. const unsigned (*cache_map)
  67. [PERF_COUNT_HW_CACHE_MAX]
  68. [PERF_COUNT_HW_CACHE_OP_MAX]
  69. [PERF_COUNT_HW_CACHE_RESULT_MAX],
  70. u32 raw_event_mask)
  71. {
  72. u64 config = event->attr.config;
  73. int type = event->attr.type;
  74. if (type == event->pmu->type)
  75. return armpmu_map_raw_event(raw_event_mask, config);
  76. switch (type) {
  77. case PERF_TYPE_HARDWARE:
  78. return armpmu_map_hw_event(event_map, config);
  79. case PERF_TYPE_HW_CACHE:
  80. return armpmu_map_cache_event(cache_map, config);
  81. case PERF_TYPE_RAW:
  82. return armpmu_map_raw_event(raw_event_mask, config);
  83. }
  84. return -ENOENT;
  85. }
  86. int armpmu_event_set_period(struct perf_event *event)
  87. {
  88. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  89. struct hw_perf_event *hwc = &event->hw;
  90. s64 left = local64_read(&hwc->period_left);
  91. s64 period = hwc->sample_period;
  92. int ret = 0;
  93. if (unlikely(left <= -period)) {
  94. left = period;
  95. local64_set(&hwc->period_left, left);
  96. hwc->last_period = period;
  97. ret = 1;
  98. }
  99. if (unlikely(left <= 0)) {
  100. left += period;
  101. local64_set(&hwc->period_left, left);
  102. hwc->last_period = period;
  103. ret = 1;
  104. }
  105. /*
  106. * Limit the maximum period to prevent the counter value
  107. * from overtaking the one we are about to program. In
  108. * effect we are reducing max_period to account for
  109. * interrupt latency (and we are being very conservative).
  110. */
  111. if (left > (armpmu->max_period >> 1))
  112. left = armpmu->max_period >> 1;
  113. local64_set(&hwc->prev_count, (u64)-left);
  114. armpmu->write_counter(event, (u64)(-left) & 0xffffffff);
  115. perf_event_update_userpage(event);
  116. return ret;
  117. }
  118. u64 armpmu_event_update(struct perf_event *event)
  119. {
  120. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  121. struct hw_perf_event *hwc = &event->hw;
  122. u64 delta, prev_raw_count, new_raw_count;
  123. again:
  124. prev_raw_count = local64_read(&hwc->prev_count);
  125. new_raw_count = armpmu->read_counter(event);
  126. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  127. new_raw_count) != prev_raw_count)
  128. goto again;
  129. delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
  130. local64_add(delta, &event->count);
  131. local64_sub(delta, &hwc->period_left);
  132. return new_raw_count;
  133. }
  134. static void
  135. armpmu_read(struct perf_event *event)
  136. {
  137. armpmu_event_update(event);
  138. }
  139. static void
  140. armpmu_stop(struct perf_event *event, int flags)
  141. {
  142. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  143. struct hw_perf_event *hwc = &event->hw;
  144. /*
  145. * ARM pmu always has to update the counter, so ignore
  146. * PERF_EF_UPDATE, see comments in armpmu_start().
  147. */
  148. if (!(hwc->state & PERF_HES_STOPPED)) {
  149. armpmu->disable(event);
  150. armpmu_event_update(event);
  151. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  152. }
  153. }
  154. static void armpmu_start(struct perf_event *event, int flags)
  155. {
  156. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  157. struct hw_perf_event *hwc = &event->hw;
  158. /*
  159. * ARM pmu always has to reprogram the period, so ignore
  160. * PERF_EF_RELOAD, see the comment below.
  161. */
  162. if (flags & PERF_EF_RELOAD)
  163. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  164. hwc->state = 0;
  165. /*
  166. * Set the period again. Some counters can't be stopped, so when we
  167. * were stopped we simply disabled the IRQ source and the counter
  168. * may have been left counting. If we don't do this step then we may
  169. * get an interrupt too soon or *way* too late if the overflow has
  170. * happened since disabling.
  171. */
  172. armpmu_event_set_period(event);
  173. armpmu->enable(event);
  174. }
  175. static void
  176. armpmu_del(struct perf_event *event, int flags)
  177. {
  178. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  179. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  180. struct hw_perf_event *hwc = &event->hw;
  181. int idx = hwc->idx;
  182. armpmu_stop(event, PERF_EF_UPDATE);
  183. hw_events->events[idx] = NULL;
  184. clear_bit(idx, hw_events->used_mask);
  185. if (armpmu->clear_event_idx)
  186. armpmu->clear_event_idx(hw_events, event);
  187. perf_event_update_userpage(event);
  188. }
  189. static int
  190. armpmu_add(struct perf_event *event, int flags)
  191. {
  192. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  193. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  194. struct hw_perf_event *hwc = &event->hw;
  195. int idx;
  196. int err = 0;
  197. /* An event following a process won't be stopped earlier */
  198. if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
  199. return -ENOENT;
  200. perf_pmu_disable(event->pmu);
  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. err = idx;
  205. goto out;
  206. }
  207. /*
  208. * If there is an event in the counter we are going to use then make
  209. * sure it is disabled.
  210. */
  211. event->hw.idx = idx;
  212. armpmu->disable(event);
  213. hw_events->events[idx] = event;
  214. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  215. if (flags & PERF_EF_START)
  216. armpmu_start(event, PERF_EF_RELOAD);
  217. /* Propagate our changes to the userspace mapping. */
  218. perf_event_update_userpage(event);
  219. out:
  220. perf_pmu_enable(event->pmu);
  221. return err;
  222. }
  223. static int
  224. validate_event(struct pmu *pmu, struct pmu_hw_events *hw_events,
  225. struct perf_event *event)
  226. {
  227. struct arm_pmu *armpmu;
  228. if (is_software_event(event))
  229. return 1;
  230. /*
  231. * Reject groups spanning multiple HW PMUs (e.g. CPU + CCI). The
  232. * core perf code won't check that the pmu->ctx == leader->ctx
  233. * until after pmu->event_init(event).
  234. */
  235. if (event->pmu != pmu)
  236. return 0;
  237. if (event->state < PERF_EVENT_STATE_OFF)
  238. return 1;
  239. if (event->state == PERF_EVENT_STATE_OFF && !event->attr.enable_on_exec)
  240. return 1;
  241. armpmu = to_arm_pmu(event->pmu);
  242. return armpmu->get_event_idx(hw_events, event) >= 0;
  243. }
  244. static int
  245. validate_group(struct perf_event *event)
  246. {
  247. struct perf_event *sibling, *leader = event->group_leader;
  248. struct pmu_hw_events fake_pmu;
  249. /*
  250. * Initialise the fake PMU. We only need to populate the
  251. * used_mask for the purposes of validation.
  252. */
  253. memset(&fake_pmu.used_mask, 0, sizeof(fake_pmu.used_mask));
  254. if (!validate_event(event->pmu, &fake_pmu, leader))
  255. return -EINVAL;
  256. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  257. if (!validate_event(event->pmu, &fake_pmu, sibling))
  258. return -EINVAL;
  259. }
  260. if (!validate_event(event->pmu, &fake_pmu, event))
  261. return -EINVAL;
  262. return 0;
  263. }
  264. static irqreturn_t armpmu_dispatch_irq(int irq, void *dev)
  265. {
  266. struct arm_pmu *armpmu;
  267. struct platform_device *plat_device;
  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_device = armpmu->plat_device;
  279. plat = dev_get_platdata(&plat_device->dev);
  280. start_clock = sched_clock();
  281. if (plat && plat->handle_irq)
  282. ret = plat->handle_irq(irq, armpmu, armpmu->handle_irq);
  283. else
  284. ret = armpmu->handle_irq(irq, armpmu);
  285. finish_clock = sched_clock();
  286. perf_sample_event_took(finish_clock - start_clock);
  287. return ret;
  288. }
  289. static void
  290. armpmu_release_hardware(struct arm_pmu *armpmu)
  291. {
  292. armpmu->free_irq(armpmu);
  293. }
  294. static int
  295. armpmu_reserve_hardware(struct arm_pmu *armpmu)
  296. {
  297. int err = armpmu->request_irq(armpmu, armpmu_dispatch_irq);
  298. if (err) {
  299. armpmu_release_hardware(armpmu);
  300. return err;
  301. }
  302. return 0;
  303. }
  304. static void
  305. hw_perf_event_destroy(struct perf_event *event)
  306. {
  307. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  308. atomic_t *active_events = &armpmu->active_events;
  309. struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
  310. if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
  311. armpmu_release_hardware(armpmu);
  312. mutex_unlock(pmu_reserve_mutex);
  313. }
  314. }
  315. static int
  316. event_requires_mode_exclusion(struct perf_event_attr *attr)
  317. {
  318. return attr->exclude_idle || attr->exclude_user ||
  319. attr->exclude_kernel || attr->exclude_hv;
  320. }
  321. static int
  322. __hw_perf_event_init(struct perf_event *event)
  323. {
  324. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  325. struct hw_perf_event *hwc = &event->hw;
  326. int mapping;
  327. mapping = armpmu->map_event(event);
  328. if (mapping < 0) {
  329. pr_debug("event %x:%llx not supported\n", event->attr.type,
  330. event->attr.config);
  331. return mapping;
  332. }
  333. /*
  334. * We don't assign an index until we actually place the event onto
  335. * hardware. Use -1 to signify that we haven't decided where to put it
  336. * yet. For SMP systems, each core has it's own PMU so we can't do any
  337. * clever allocation or constraints checking at this point.
  338. */
  339. hwc->idx = -1;
  340. hwc->config_base = 0;
  341. hwc->config = 0;
  342. hwc->event_base = 0;
  343. /*
  344. * Check whether we need to exclude the counter from certain modes.
  345. */
  346. if ((!armpmu->set_event_filter ||
  347. armpmu->set_event_filter(hwc, &event->attr)) &&
  348. event_requires_mode_exclusion(&event->attr)) {
  349. pr_debug("ARM performance counters do not support "
  350. "mode exclusion\n");
  351. return -EOPNOTSUPP;
  352. }
  353. /*
  354. * Store the event encoding into the config_base field.
  355. */
  356. hwc->config_base |= (unsigned long)mapping;
  357. if (!is_sampling_event(event)) {
  358. /*
  359. * For non-sampling runs, limit the sample_period to half
  360. * of the counter width. That way, the new counter value
  361. * is far less likely to overtake the previous one unless
  362. * you have some serious IRQ latency issues.
  363. */
  364. hwc->sample_period = armpmu->max_period >> 1;
  365. hwc->last_period = hwc->sample_period;
  366. local64_set(&hwc->period_left, hwc->sample_period);
  367. }
  368. if (event->group_leader != event) {
  369. if (validate_group(event) != 0)
  370. return -EINVAL;
  371. }
  372. return 0;
  373. }
  374. static int armpmu_event_init(struct perf_event *event)
  375. {
  376. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  377. int err = 0;
  378. atomic_t *active_events = &armpmu->active_events;
  379. /*
  380. * Reject CPU-affine events for CPUs that are of a different class to
  381. * that which this PMU handles. Process-following events (where
  382. * event->cpu == -1) can be migrated between CPUs, and thus we have to
  383. * reject them later (in armpmu_add) if they're scheduled on a
  384. * different class of CPU.
  385. */
  386. if (event->cpu != -1 &&
  387. !cpumask_test_cpu(event->cpu, &armpmu->supported_cpus))
  388. return -ENOENT;
  389. /* does not support taken branch sampling */
  390. if (has_branch_stack(event))
  391. return -EOPNOTSUPP;
  392. if (armpmu->map_event(event) == -ENOENT)
  393. return -ENOENT;
  394. event->destroy = hw_perf_event_destroy;
  395. if (!atomic_inc_not_zero(active_events)) {
  396. mutex_lock(&armpmu->reserve_mutex);
  397. if (atomic_read(active_events) == 0)
  398. err = armpmu_reserve_hardware(armpmu);
  399. if (!err)
  400. atomic_inc(active_events);
  401. mutex_unlock(&armpmu->reserve_mutex);
  402. }
  403. if (err)
  404. return err;
  405. err = __hw_perf_event_init(event);
  406. if (err)
  407. hw_perf_event_destroy(event);
  408. return err;
  409. }
  410. static void armpmu_enable(struct pmu *pmu)
  411. {
  412. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  413. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  414. int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
  415. /* For task-bound events we may be called on other CPUs */
  416. if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
  417. return;
  418. if (enabled)
  419. armpmu->start(armpmu);
  420. }
  421. static void armpmu_disable(struct pmu *pmu)
  422. {
  423. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  424. /* For task-bound events we may be called on other CPUs */
  425. if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
  426. return;
  427. armpmu->stop(armpmu);
  428. }
  429. /*
  430. * In heterogeneous systems, events are specific to a particular
  431. * microarchitecture, and aren't suitable for another. Thus, only match CPUs of
  432. * the same microarchitecture.
  433. */
  434. static int armpmu_filter_match(struct perf_event *event)
  435. {
  436. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  437. unsigned int cpu = smp_processor_id();
  438. return cpumask_test_cpu(cpu, &armpmu->supported_cpus);
  439. }
  440. static ssize_t armpmu_cpumask_show(struct device *dev,
  441. struct device_attribute *attr, char *buf)
  442. {
  443. struct arm_pmu *armpmu = to_arm_pmu(dev_get_drvdata(dev));
  444. return cpumap_print_to_pagebuf(true, buf, &armpmu->supported_cpus);
  445. }
  446. static DEVICE_ATTR(cpus, S_IRUGO, armpmu_cpumask_show, NULL);
  447. static struct attribute *armpmu_common_attrs[] = {
  448. &dev_attr_cpus.attr,
  449. NULL,
  450. };
  451. static struct attribute_group armpmu_common_attr_group = {
  452. .attrs = armpmu_common_attrs,
  453. };
  454. static void armpmu_init(struct arm_pmu *armpmu)
  455. {
  456. atomic_set(&armpmu->active_events, 0);
  457. mutex_init(&armpmu->reserve_mutex);
  458. armpmu->pmu = (struct pmu) {
  459. .pmu_enable = armpmu_enable,
  460. .pmu_disable = armpmu_disable,
  461. .event_init = armpmu_event_init,
  462. .add = armpmu_add,
  463. .del = armpmu_del,
  464. .start = armpmu_start,
  465. .stop = armpmu_stop,
  466. .read = armpmu_read,
  467. .filter_match = armpmu_filter_match,
  468. .attr_groups = armpmu->attr_groups,
  469. };
  470. armpmu->attr_groups[ARMPMU_ATTR_GROUP_COMMON] =
  471. &armpmu_common_attr_group;
  472. }
  473. /* Set at runtime when we know what CPU type we are. */
  474. static struct arm_pmu *__oprofile_cpu_pmu;
  475. /*
  476. * Despite the names, these two functions are CPU-specific and are used
  477. * by the OProfile/perf code.
  478. */
  479. const char *perf_pmu_name(void)
  480. {
  481. if (!__oprofile_cpu_pmu)
  482. return NULL;
  483. return __oprofile_cpu_pmu->name;
  484. }
  485. EXPORT_SYMBOL_GPL(perf_pmu_name);
  486. int perf_num_counters(void)
  487. {
  488. int max_events = 0;
  489. if (__oprofile_cpu_pmu != NULL)
  490. max_events = __oprofile_cpu_pmu->num_events;
  491. return max_events;
  492. }
  493. EXPORT_SYMBOL_GPL(perf_num_counters);
  494. static void cpu_pmu_enable_percpu_irq(void *data)
  495. {
  496. int irq = *(int *)data;
  497. enable_percpu_irq(irq, IRQ_TYPE_NONE);
  498. }
  499. static void cpu_pmu_disable_percpu_irq(void *data)
  500. {
  501. int irq = *(int *)data;
  502. disable_percpu_irq(irq);
  503. }
  504. static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
  505. {
  506. int i, irq, irqs;
  507. struct platform_device *pmu_device = cpu_pmu->plat_device;
  508. struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
  509. irqs = min(pmu_device->num_resources, num_possible_cpus());
  510. irq = platform_get_irq(pmu_device, 0);
  511. if (irq > 0 && irq_is_percpu(irq)) {
  512. on_each_cpu_mask(&cpu_pmu->supported_cpus,
  513. cpu_pmu_disable_percpu_irq, &irq, 1);
  514. free_percpu_irq(irq, &hw_events->percpu_pmu);
  515. } else {
  516. for (i = 0; i < irqs; ++i) {
  517. int cpu = i;
  518. if (cpu_pmu->irq_affinity)
  519. cpu = cpu_pmu->irq_affinity[i];
  520. if (!cpumask_test_and_clear_cpu(cpu, &cpu_pmu->active_irqs))
  521. continue;
  522. irq = platform_get_irq(pmu_device, i);
  523. if (irq > 0)
  524. free_irq(irq, per_cpu_ptr(&hw_events->percpu_pmu, cpu));
  525. }
  526. }
  527. }
  528. static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
  529. {
  530. int i, err, irq, irqs;
  531. struct platform_device *pmu_device = cpu_pmu->plat_device;
  532. struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
  533. if (!pmu_device)
  534. return -ENODEV;
  535. irqs = min(pmu_device->num_resources, num_possible_cpus());
  536. if (irqs < 1) {
  537. pr_warn_once("perf/ARM: No irqs for PMU defined, sampling events not supported\n");
  538. return 0;
  539. }
  540. irq = platform_get_irq(pmu_device, 0);
  541. if (irq > 0 && irq_is_percpu(irq)) {
  542. err = request_percpu_irq(irq, handler, "arm-pmu",
  543. &hw_events->percpu_pmu);
  544. if (err) {
  545. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  546. irq);
  547. return err;
  548. }
  549. on_each_cpu_mask(&cpu_pmu->supported_cpus,
  550. cpu_pmu_enable_percpu_irq, &irq, 1);
  551. } else {
  552. for (i = 0; i < irqs; ++i) {
  553. int cpu = i;
  554. err = 0;
  555. irq = platform_get_irq(pmu_device, i);
  556. if (irq < 0)
  557. continue;
  558. if (cpu_pmu->irq_affinity)
  559. cpu = cpu_pmu->irq_affinity[i];
  560. /*
  561. * If we have a single PMU interrupt that we can't shift,
  562. * assume that we're running on a uniprocessor machine and
  563. * continue. Otherwise, continue without this interrupt.
  564. */
  565. if (irq_set_affinity(irq, cpumask_of(cpu)) && irqs > 1) {
  566. pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
  567. irq, cpu);
  568. continue;
  569. }
  570. err = request_irq(irq, handler,
  571. IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
  572. per_cpu_ptr(&hw_events->percpu_pmu, cpu));
  573. if (err) {
  574. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  575. irq);
  576. return err;
  577. }
  578. cpumask_set_cpu(cpu, &cpu_pmu->active_irqs);
  579. }
  580. }
  581. return 0;
  582. }
  583. /*
  584. * PMU hardware loses all context when a CPU goes offline.
  585. * When a CPU is hotplugged back in, since some hardware registers are
  586. * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
  587. * junk values out of them.
  588. */
  589. static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
  590. {
  591. struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
  592. if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
  593. return 0;
  594. if (pmu->reset)
  595. pmu->reset(pmu);
  596. return 0;
  597. }
  598. #ifdef CONFIG_CPU_PM
  599. static void cpu_pm_pmu_setup(struct arm_pmu *armpmu, unsigned long cmd)
  600. {
  601. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  602. struct perf_event *event;
  603. int idx;
  604. for (idx = 0; idx < armpmu->num_events; idx++) {
  605. /*
  606. * If the counter is not used skip it, there is no
  607. * need of stopping/restarting it.
  608. */
  609. if (!test_bit(idx, hw_events->used_mask))
  610. continue;
  611. event = hw_events->events[idx];
  612. switch (cmd) {
  613. case CPU_PM_ENTER:
  614. /*
  615. * Stop and update the counter
  616. */
  617. armpmu_stop(event, PERF_EF_UPDATE);
  618. break;
  619. case CPU_PM_EXIT:
  620. case CPU_PM_ENTER_FAILED:
  621. /*
  622. * Restore and enable the counter.
  623. * armpmu_start() indirectly calls
  624. *
  625. * perf_event_update_userpage()
  626. *
  627. * that requires RCU read locking to be functional,
  628. * wrap the call within RCU_NONIDLE to make the
  629. * RCU subsystem aware this cpu is not idle from
  630. * an RCU perspective for the armpmu_start() call
  631. * duration.
  632. */
  633. RCU_NONIDLE(armpmu_start(event, PERF_EF_RELOAD));
  634. break;
  635. default:
  636. break;
  637. }
  638. }
  639. }
  640. static int cpu_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
  641. void *v)
  642. {
  643. struct arm_pmu *armpmu = container_of(b, struct arm_pmu, cpu_pm_nb);
  644. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  645. int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
  646. if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
  647. return NOTIFY_DONE;
  648. /*
  649. * Always reset the PMU registers on power-up even if
  650. * there are no events running.
  651. */
  652. if (cmd == CPU_PM_EXIT && armpmu->reset)
  653. armpmu->reset(armpmu);
  654. if (!enabled)
  655. return NOTIFY_OK;
  656. switch (cmd) {
  657. case CPU_PM_ENTER:
  658. armpmu->stop(armpmu);
  659. cpu_pm_pmu_setup(armpmu, cmd);
  660. break;
  661. case CPU_PM_EXIT:
  662. cpu_pm_pmu_setup(armpmu, cmd);
  663. case CPU_PM_ENTER_FAILED:
  664. armpmu->start(armpmu);
  665. break;
  666. default:
  667. return NOTIFY_DONE;
  668. }
  669. return NOTIFY_OK;
  670. }
  671. static int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu)
  672. {
  673. cpu_pmu->cpu_pm_nb.notifier_call = cpu_pm_pmu_notify;
  674. return cpu_pm_register_notifier(&cpu_pmu->cpu_pm_nb);
  675. }
  676. static void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu)
  677. {
  678. cpu_pm_unregister_notifier(&cpu_pmu->cpu_pm_nb);
  679. }
  680. #else
  681. static inline int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu) { return 0; }
  682. static inline void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu) { }
  683. #endif
  684. static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
  685. {
  686. int err;
  687. int cpu;
  688. struct pmu_hw_events __percpu *cpu_hw_events;
  689. cpu_hw_events = alloc_percpu(struct pmu_hw_events);
  690. if (!cpu_hw_events)
  691. return -ENOMEM;
  692. err = cpuhp_state_add_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
  693. &cpu_pmu->node);
  694. if (err)
  695. goto out_free;
  696. err = cpu_pm_pmu_register(cpu_pmu);
  697. if (err)
  698. goto out_unregister;
  699. for_each_possible_cpu(cpu) {
  700. struct pmu_hw_events *events = per_cpu_ptr(cpu_hw_events, cpu);
  701. raw_spin_lock_init(&events->pmu_lock);
  702. events->percpu_pmu = cpu_pmu;
  703. }
  704. cpu_pmu->hw_events = cpu_hw_events;
  705. cpu_pmu->request_irq = cpu_pmu_request_irq;
  706. cpu_pmu->free_irq = cpu_pmu_free_irq;
  707. /* Ensure the PMU has sane values out of reset. */
  708. if (cpu_pmu->reset)
  709. on_each_cpu_mask(&cpu_pmu->supported_cpus, cpu_pmu->reset,
  710. cpu_pmu, 1);
  711. /* If no interrupts available, set the corresponding capability flag */
  712. if (!platform_get_irq(cpu_pmu->plat_device, 0))
  713. cpu_pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  714. /*
  715. * This is a CPU PMU potentially in a heterogeneous configuration (e.g.
  716. * big.LITTLE). This is not an uncore PMU, and we have taken ctx
  717. * sharing into account (e.g. with our pmu::filter_match callback and
  718. * pmu::event_init group validation).
  719. */
  720. cpu_pmu->pmu.capabilities |= PERF_PMU_CAP_HETEROGENEOUS_CPUS;
  721. return 0;
  722. out_unregister:
  723. cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
  724. &cpu_pmu->node);
  725. out_free:
  726. free_percpu(cpu_hw_events);
  727. return err;
  728. }
  729. static void cpu_pmu_destroy(struct arm_pmu *cpu_pmu)
  730. {
  731. cpu_pm_pmu_unregister(cpu_pmu);
  732. cpuhp_state_remove_instance_nocalls(CPUHP_AP_PERF_ARM_STARTING,
  733. &cpu_pmu->node);
  734. free_percpu(cpu_pmu->hw_events);
  735. }
  736. /*
  737. * CPU PMU identification and probing.
  738. */
  739. static int probe_current_pmu(struct arm_pmu *pmu,
  740. const struct pmu_probe_info *info)
  741. {
  742. int cpu = get_cpu();
  743. unsigned int cpuid = read_cpuid_id();
  744. int ret = -ENODEV;
  745. pr_info("probing PMU on CPU %d\n", cpu);
  746. for (; info->init != NULL; info++) {
  747. if ((cpuid & info->mask) != info->cpuid)
  748. continue;
  749. ret = info->init(pmu);
  750. break;
  751. }
  752. put_cpu();
  753. return ret;
  754. }
  755. static int of_pmu_irq_cfg(struct arm_pmu *pmu)
  756. {
  757. int *irqs, i = 0;
  758. bool using_spi = false;
  759. struct platform_device *pdev = pmu->plat_device;
  760. irqs = kcalloc(pdev->num_resources, sizeof(*irqs), GFP_KERNEL);
  761. if (!irqs)
  762. return -ENOMEM;
  763. do {
  764. struct device_node *dn;
  765. int cpu, irq;
  766. /* See if we have an affinity entry */
  767. dn = of_parse_phandle(pdev->dev.of_node, "interrupt-affinity", i);
  768. if (!dn)
  769. break;
  770. /* Check the IRQ type and prohibit a mix of PPIs and SPIs */
  771. irq = platform_get_irq(pdev, i);
  772. if (irq > 0) {
  773. bool spi = !irq_is_percpu(irq);
  774. if (i > 0 && spi != using_spi) {
  775. pr_err("PPI/SPI IRQ type mismatch for %s!\n",
  776. dn->name);
  777. of_node_put(dn);
  778. kfree(irqs);
  779. return -EINVAL;
  780. }
  781. using_spi = spi;
  782. }
  783. /* Now look up the logical CPU number */
  784. for_each_possible_cpu(cpu) {
  785. struct device_node *cpu_dn;
  786. cpu_dn = of_cpu_device_node_get(cpu);
  787. of_node_put(cpu_dn);
  788. if (dn == cpu_dn)
  789. break;
  790. }
  791. if (cpu >= nr_cpu_ids) {
  792. pr_warn("Failed to find logical CPU for %s\n",
  793. dn->name);
  794. of_node_put(dn);
  795. cpumask_setall(&pmu->supported_cpus);
  796. break;
  797. }
  798. of_node_put(dn);
  799. /* For SPIs, we need to track the affinity per IRQ */
  800. if (using_spi) {
  801. if (i >= pdev->num_resources)
  802. break;
  803. irqs[i] = cpu;
  804. }
  805. /* Keep track of the CPUs containing this PMU type */
  806. cpumask_set_cpu(cpu, &pmu->supported_cpus);
  807. i++;
  808. } while (1);
  809. /* If we didn't manage to parse anything, try the interrupt affinity */
  810. if (cpumask_weight(&pmu->supported_cpus) == 0) {
  811. int irq = platform_get_irq(pdev, 0);
  812. if (irq > 0 && irq_is_percpu(irq)) {
  813. /* If using PPIs, check the affinity of the partition */
  814. int ret;
  815. ret = irq_get_percpu_devid_partition(irq, &pmu->supported_cpus);
  816. if (ret) {
  817. kfree(irqs);
  818. return ret;
  819. }
  820. } else {
  821. /* Otherwise default to all CPUs */
  822. cpumask_setall(&pmu->supported_cpus);
  823. }
  824. }
  825. /* If we matched up the IRQ affinities, use them to route the SPIs */
  826. if (using_spi && i == pdev->num_resources)
  827. pmu->irq_affinity = irqs;
  828. else
  829. kfree(irqs);
  830. return 0;
  831. }
  832. int arm_pmu_device_probe(struct platform_device *pdev,
  833. const struct of_device_id *of_table,
  834. const struct pmu_probe_info *probe_table)
  835. {
  836. const struct of_device_id *of_id;
  837. const int (*init_fn)(struct arm_pmu *);
  838. struct device_node *node = pdev->dev.of_node;
  839. struct arm_pmu *pmu;
  840. int ret = -ENODEV;
  841. pmu = kzalloc(sizeof(struct arm_pmu), GFP_KERNEL);
  842. if (!pmu) {
  843. pr_info("failed to allocate PMU device!\n");
  844. return -ENOMEM;
  845. }
  846. armpmu_init(pmu);
  847. pmu->plat_device = pdev;
  848. if (node && (of_id = of_match_node(of_table, pdev->dev.of_node))) {
  849. init_fn = of_id->data;
  850. pmu->secure_access = of_property_read_bool(pdev->dev.of_node,
  851. "secure-reg-access");
  852. /* arm64 systems boot only as non-secure */
  853. if (IS_ENABLED(CONFIG_ARM64) && pmu->secure_access) {
  854. pr_warn("ignoring \"secure-reg-access\" property for arm64\n");
  855. pmu->secure_access = false;
  856. }
  857. ret = of_pmu_irq_cfg(pmu);
  858. if (!ret)
  859. ret = init_fn(pmu);
  860. } else if (probe_table) {
  861. cpumask_setall(&pmu->supported_cpus);
  862. ret = probe_current_pmu(pmu, probe_table);
  863. }
  864. if (ret) {
  865. pr_info("%s: failed to probe PMU!\n", of_node_full_name(node));
  866. goto out_free;
  867. }
  868. ret = cpu_pmu_init(pmu);
  869. if (ret)
  870. goto out_free;
  871. ret = perf_pmu_register(&pmu->pmu, pmu->name, -1);
  872. if (ret)
  873. goto out_destroy;
  874. if (!__oprofile_cpu_pmu)
  875. __oprofile_cpu_pmu = pmu;
  876. pr_info("enabled with %s PMU driver, %d counters available\n",
  877. pmu->name, pmu->num_events);
  878. return 0;
  879. out_destroy:
  880. cpu_pmu_destroy(pmu);
  881. out_free:
  882. pr_info("%s: failed to register PMU devices!\n",
  883. of_node_full_name(node));
  884. kfree(pmu->irq_affinity);
  885. kfree(pmu);
  886. return ret;
  887. }
  888. static int arm_pmu_hp_init(void)
  889. {
  890. int ret;
  891. ret = cpuhp_setup_state_multi(CPUHP_AP_PERF_ARM_STARTING,
  892. "perf/arm/pmu:starting",
  893. arm_perf_starting_cpu, NULL);
  894. if (ret)
  895. pr_err("CPU hotplug notifier for ARM PMU could not be registered: %d\n",
  896. ret);
  897. return ret;
  898. }
  899. subsys_initcall(arm_pmu_hp_init);