arm_pmu.c 24 KB

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