arm_pmu.c 22 KB

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