perf_event.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896
  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.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/spinlock.h>
  20. #include <linux/irq.h>
  21. #include <linux/irqdesc.h>
  22. #include <asm/cputype.h>
  23. #include <asm/irq_regs.h>
  24. #include <asm/pmu.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 irqreturn_t armpmu_dispatch_irq(int irq, void *dev)
  263. {
  264. struct arm_pmu *armpmu;
  265. struct platform_device *plat_device;
  266. struct arm_pmu_platdata *plat;
  267. int ret;
  268. u64 start_clock, finish_clock;
  269. /*
  270. * we request the IRQ with a (possibly percpu) struct arm_pmu**, but
  271. * the handlers expect a struct arm_pmu*. The percpu_irq framework will
  272. * do any necessary shifting, we just need to perform the first
  273. * dereference.
  274. */
  275. armpmu = *(void **)dev;
  276. plat_device = armpmu->plat_device;
  277. plat = dev_get_platdata(&plat_device->dev);
  278. start_clock = sched_clock();
  279. if (plat && plat->handle_irq)
  280. ret = plat->handle_irq(irq, armpmu, armpmu->handle_irq);
  281. else
  282. ret = armpmu->handle_irq(irq, armpmu);
  283. finish_clock = sched_clock();
  284. perf_sample_event_took(finish_clock - start_clock);
  285. return ret;
  286. }
  287. static void
  288. armpmu_release_hardware(struct arm_pmu *armpmu)
  289. {
  290. armpmu->free_irq(armpmu);
  291. }
  292. static int
  293. armpmu_reserve_hardware(struct arm_pmu *armpmu)
  294. {
  295. int err = armpmu->request_irq(armpmu, armpmu_dispatch_irq);
  296. if (err) {
  297. armpmu_release_hardware(armpmu);
  298. return err;
  299. }
  300. return 0;
  301. }
  302. static void
  303. hw_perf_event_destroy(struct perf_event *event)
  304. {
  305. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  306. atomic_t *active_events = &armpmu->active_events;
  307. struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
  308. if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
  309. armpmu_release_hardware(armpmu);
  310. mutex_unlock(pmu_reserve_mutex);
  311. }
  312. }
  313. static int
  314. event_requires_mode_exclusion(struct perf_event_attr *attr)
  315. {
  316. return attr->exclude_idle || attr->exclude_user ||
  317. attr->exclude_kernel || attr->exclude_hv;
  318. }
  319. static int
  320. __hw_perf_event_init(struct perf_event *event)
  321. {
  322. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  323. struct hw_perf_event *hwc = &event->hw;
  324. int mapping;
  325. mapping = armpmu->map_event(event);
  326. if (mapping < 0) {
  327. pr_debug("event %x:%llx not supported\n", event->attr.type,
  328. event->attr.config);
  329. return mapping;
  330. }
  331. /*
  332. * We don't assign an index until we actually place the event onto
  333. * hardware. Use -1 to signify that we haven't decided where to put it
  334. * yet. For SMP systems, each core has it's own PMU so we can't do any
  335. * clever allocation or constraints checking at this point.
  336. */
  337. hwc->idx = -1;
  338. hwc->config_base = 0;
  339. hwc->config = 0;
  340. hwc->event_base = 0;
  341. /*
  342. * Check whether we need to exclude the counter from certain modes.
  343. */
  344. if ((!armpmu->set_event_filter ||
  345. armpmu->set_event_filter(hwc, &event->attr)) &&
  346. event_requires_mode_exclusion(&event->attr)) {
  347. pr_debug("ARM performance counters do not support "
  348. "mode exclusion\n");
  349. return -EOPNOTSUPP;
  350. }
  351. /*
  352. * Store the event encoding into the config_base field.
  353. */
  354. hwc->config_base |= (unsigned long)mapping;
  355. if (!is_sampling_event(event)) {
  356. /*
  357. * For non-sampling runs, limit the sample_period to half
  358. * of the counter width. That way, the new counter value
  359. * is far less likely to overtake the previous one unless
  360. * you have some serious IRQ latency issues.
  361. */
  362. hwc->sample_period = armpmu->max_period >> 1;
  363. hwc->last_period = hwc->sample_period;
  364. local64_set(&hwc->period_left, hwc->sample_period);
  365. }
  366. if (event->group_leader != event) {
  367. if (validate_group(event) != 0)
  368. return -EINVAL;
  369. }
  370. return 0;
  371. }
  372. static int armpmu_event_init(struct perf_event *event)
  373. {
  374. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  375. int err = 0;
  376. atomic_t *active_events = &armpmu->active_events;
  377. /*
  378. * Reject CPU-affine events for CPUs that are of a different class to
  379. * that which this PMU handles. Process-following events (where
  380. * event->cpu == -1) can be migrated between CPUs, and thus we have to
  381. * reject them later (in armpmu_add) if they're scheduled on a
  382. * different class of CPU.
  383. */
  384. if (event->cpu != -1 &&
  385. !cpumask_test_cpu(event->cpu, &armpmu->supported_cpus))
  386. return -ENOENT;
  387. /* does not support taken branch sampling */
  388. if (has_branch_stack(event))
  389. return -EOPNOTSUPP;
  390. if (armpmu->map_event(event) == -ENOENT)
  391. return -ENOENT;
  392. event->destroy = hw_perf_event_destroy;
  393. if (!atomic_inc_not_zero(active_events)) {
  394. mutex_lock(&armpmu->reserve_mutex);
  395. if (atomic_read(active_events) == 0)
  396. err = armpmu_reserve_hardware(armpmu);
  397. if (!err)
  398. atomic_inc(active_events);
  399. mutex_unlock(&armpmu->reserve_mutex);
  400. }
  401. if (err)
  402. return err;
  403. err = __hw_perf_event_init(event);
  404. if (err)
  405. hw_perf_event_destroy(event);
  406. return err;
  407. }
  408. static void armpmu_enable(struct pmu *pmu)
  409. {
  410. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  411. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  412. int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
  413. /* For task-bound events we may be called on other CPUs */
  414. if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
  415. return;
  416. if (enabled)
  417. armpmu->start(armpmu);
  418. }
  419. static void armpmu_disable(struct pmu *pmu)
  420. {
  421. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  422. /* For task-bound events we may be called on other CPUs */
  423. if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
  424. return;
  425. armpmu->stop(armpmu);
  426. }
  427. /*
  428. * In heterogeneous systems, events are specific to a particular
  429. * microarchitecture, and aren't suitable for another. Thus, only match CPUs of
  430. * the same microarchitecture.
  431. */
  432. static int armpmu_filter_match(struct perf_event *event)
  433. {
  434. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  435. unsigned int cpu = smp_processor_id();
  436. return cpumask_test_cpu(cpu, &armpmu->supported_cpus);
  437. }
  438. static void armpmu_init(struct arm_pmu *armpmu)
  439. {
  440. atomic_set(&armpmu->active_events, 0);
  441. mutex_init(&armpmu->reserve_mutex);
  442. armpmu->pmu = (struct pmu) {
  443. .pmu_enable = armpmu_enable,
  444. .pmu_disable = armpmu_disable,
  445. .event_init = armpmu_event_init,
  446. .add = armpmu_add,
  447. .del = armpmu_del,
  448. .start = armpmu_start,
  449. .stop = armpmu_stop,
  450. .read = armpmu_read,
  451. .filter_match = armpmu_filter_match,
  452. };
  453. }
  454. int armpmu_register(struct arm_pmu *armpmu, int type)
  455. {
  456. armpmu_init(armpmu);
  457. pr_info("enabled with %s PMU driver, %d counters available\n",
  458. armpmu->name, armpmu->num_events);
  459. return perf_pmu_register(&armpmu->pmu, armpmu->name, type);
  460. }
  461. /* Set at runtime when we know what CPU type we are. */
  462. static struct arm_pmu *__oprofile_cpu_pmu;
  463. /*
  464. * Despite the names, these two functions are CPU-specific and are used
  465. * by the OProfile/perf code.
  466. */
  467. const char *perf_pmu_name(void)
  468. {
  469. if (!__oprofile_cpu_pmu)
  470. return NULL;
  471. return __oprofile_cpu_pmu->name;
  472. }
  473. EXPORT_SYMBOL_GPL(perf_pmu_name);
  474. int perf_num_counters(void)
  475. {
  476. int max_events = 0;
  477. if (__oprofile_cpu_pmu != NULL)
  478. max_events = __oprofile_cpu_pmu->num_events;
  479. return max_events;
  480. }
  481. EXPORT_SYMBOL_GPL(perf_num_counters);
  482. static void cpu_pmu_enable_percpu_irq(void *data)
  483. {
  484. int irq = *(int *)data;
  485. enable_percpu_irq(irq, IRQ_TYPE_NONE);
  486. }
  487. static void cpu_pmu_disable_percpu_irq(void *data)
  488. {
  489. int irq = *(int *)data;
  490. disable_percpu_irq(irq);
  491. }
  492. static void cpu_pmu_free_irq(struct arm_pmu *cpu_pmu)
  493. {
  494. int i, irq, irqs;
  495. struct platform_device *pmu_device = cpu_pmu->plat_device;
  496. struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
  497. irqs = min(pmu_device->num_resources, num_possible_cpus());
  498. irq = platform_get_irq(pmu_device, 0);
  499. if (irq >= 0 && irq_is_percpu(irq)) {
  500. on_each_cpu(cpu_pmu_disable_percpu_irq, &irq, 1);
  501. free_percpu_irq(irq, &hw_events->percpu_pmu);
  502. } else {
  503. for (i = 0; i < irqs; ++i) {
  504. int cpu = i;
  505. if (cpu_pmu->irq_affinity)
  506. cpu = cpu_pmu->irq_affinity[i];
  507. if (!cpumask_test_and_clear_cpu(cpu, &cpu_pmu->active_irqs))
  508. continue;
  509. irq = platform_get_irq(pmu_device, i);
  510. if (irq >= 0)
  511. free_irq(irq, per_cpu_ptr(&hw_events->percpu_pmu, cpu));
  512. }
  513. }
  514. }
  515. static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
  516. {
  517. int i, err, irq, irqs;
  518. struct platform_device *pmu_device = cpu_pmu->plat_device;
  519. struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
  520. if (!pmu_device)
  521. return -ENODEV;
  522. irqs = min(pmu_device->num_resources, num_possible_cpus());
  523. if (irqs < 1) {
  524. pr_warn_once("perf/ARM: No irqs for PMU defined, sampling events not supported\n");
  525. return 0;
  526. }
  527. irq = platform_get_irq(pmu_device, 0);
  528. if (irq >= 0 && irq_is_percpu(irq)) {
  529. err = request_percpu_irq(irq, handler, "arm-pmu",
  530. &hw_events->percpu_pmu);
  531. if (err) {
  532. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  533. irq);
  534. return err;
  535. }
  536. on_each_cpu(cpu_pmu_enable_percpu_irq, &irq, 1);
  537. } else {
  538. for (i = 0; i < irqs; ++i) {
  539. int cpu = i;
  540. err = 0;
  541. irq = platform_get_irq(pmu_device, i);
  542. if (irq < 0)
  543. continue;
  544. if (cpu_pmu->irq_affinity)
  545. cpu = cpu_pmu->irq_affinity[i];
  546. /*
  547. * If we have a single PMU interrupt that we can't shift,
  548. * assume that we're running on a uniprocessor machine and
  549. * continue. Otherwise, continue without this interrupt.
  550. */
  551. if (irq_set_affinity(irq, cpumask_of(cpu)) && irqs > 1) {
  552. pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
  553. irq, cpu);
  554. continue;
  555. }
  556. err = request_irq(irq, handler,
  557. IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
  558. per_cpu_ptr(&hw_events->percpu_pmu, cpu));
  559. if (err) {
  560. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  561. irq);
  562. return err;
  563. }
  564. cpumask_set_cpu(cpu, &cpu_pmu->active_irqs);
  565. }
  566. }
  567. return 0;
  568. }
  569. /*
  570. * PMU hardware loses all context when a CPU goes offline.
  571. * When a CPU is hotplugged back in, since some hardware registers are
  572. * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
  573. * junk values out of them.
  574. */
  575. static int cpu_pmu_notify(struct notifier_block *b, unsigned long action,
  576. void *hcpu)
  577. {
  578. int cpu = (unsigned long)hcpu;
  579. struct arm_pmu *pmu = container_of(b, struct arm_pmu, hotplug_nb);
  580. if ((action & ~CPU_TASKS_FROZEN) != CPU_STARTING)
  581. return NOTIFY_DONE;
  582. if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
  583. return NOTIFY_DONE;
  584. if (pmu->reset)
  585. pmu->reset(pmu);
  586. else
  587. return NOTIFY_DONE;
  588. return NOTIFY_OK;
  589. }
  590. static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
  591. {
  592. int err;
  593. int cpu;
  594. struct pmu_hw_events __percpu *cpu_hw_events;
  595. cpu_hw_events = alloc_percpu(struct pmu_hw_events);
  596. if (!cpu_hw_events)
  597. return -ENOMEM;
  598. cpu_pmu->hotplug_nb.notifier_call = cpu_pmu_notify;
  599. err = register_cpu_notifier(&cpu_pmu->hotplug_nb);
  600. if (err)
  601. goto out_hw_events;
  602. for_each_possible_cpu(cpu) {
  603. struct pmu_hw_events *events = per_cpu_ptr(cpu_hw_events, cpu);
  604. raw_spin_lock_init(&events->pmu_lock);
  605. events->percpu_pmu = cpu_pmu;
  606. }
  607. cpu_pmu->hw_events = cpu_hw_events;
  608. cpu_pmu->request_irq = cpu_pmu_request_irq;
  609. cpu_pmu->free_irq = cpu_pmu_free_irq;
  610. /* Ensure the PMU has sane values out of reset. */
  611. if (cpu_pmu->reset)
  612. on_each_cpu_mask(&cpu_pmu->supported_cpus, cpu_pmu->reset,
  613. cpu_pmu, 1);
  614. /* If no interrupts available, set the corresponding capability flag */
  615. if (!platform_get_irq(cpu_pmu->plat_device, 0))
  616. cpu_pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  617. return 0;
  618. out_hw_events:
  619. free_percpu(cpu_hw_events);
  620. return err;
  621. }
  622. static void cpu_pmu_destroy(struct arm_pmu *cpu_pmu)
  623. {
  624. unregister_cpu_notifier(&cpu_pmu->hotplug_nb);
  625. free_percpu(cpu_pmu->hw_events);
  626. }
  627. /*
  628. * CPU PMU identification and probing.
  629. */
  630. static int probe_current_pmu(struct arm_pmu *pmu,
  631. const struct pmu_probe_info *info)
  632. {
  633. int cpu = get_cpu();
  634. unsigned int cpuid = read_cpuid_id();
  635. int ret = -ENODEV;
  636. pr_info("probing PMU on CPU %d\n", cpu);
  637. for (; info->init != NULL; info++) {
  638. if ((cpuid & info->mask) != info->cpuid)
  639. continue;
  640. ret = info->init(pmu);
  641. break;
  642. }
  643. put_cpu();
  644. return ret;
  645. }
  646. static int of_pmu_irq_cfg(struct arm_pmu *pmu)
  647. {
  648. int i, irq, *irqs;
  649. struct platform_device *pdev = pmu->plat_device;
  650. /* Don't bother with PPIs; they're already affine */
  651. irq = platform_get_irq(pdev, 0);
  652. if (irq >= 0 && irq_is_percpu(irq))
  653. return 0;
  654. irqs = kcalloc(pdev->num_resources, sizeof(*irqs), GFP_KERNEL);
  655. if (!irqs)
  656. return -ENOMEM;
  657. for (i = 0; i < pdev->num_resources; ++i) {
  658. struct device_node *dn;
  659. int cpu;
  660. dn = of_parse_phandle(pdev->dev.of_node, "interrupt-affinity",
  661. i);
  662. if (!dn) {
  663. pr_warn("Failed to parse %s/interrupt-affinity[%d]\n",
  664. of_node_full_name(pdev->dev.of_node), i);
  665. break;
  666. }
  667. for_each_possible_cpu(cpu)
  668. if (arch_find_n_match_cpu_physical_id(dn, cpu, NULL))
  669. break;
  670. if (cpu >= nr_cpu_ids) {
  671. pr_warn("Failed to find logical CPU for %s\n",
  672. dn->name);
  673. of_node_put(dn);
  674. break;
  675. }
  676. of_node_put(dn);
  677. irqs[i] = cpu;
  678. cpumask_set_cpu(cpu, &pmu->supported_cpus);
  679. }
  680. if (i == pdev->num_resources) {
  681. pmu->irq_affinity = irqs;
  682. } else {
  683. kfree(irqs);
  684. cpumask_setall(&pmu->supported_cpus);
  685. }
  686. return 0;
  687. }
  688. int arm_pmu_device_probe(struct platform_device *pdev,
  689. const struct of_device_id *of_table,
  690. const struct pmu_probe_info *probe_table)
  691. {
  692. const struct of_device_id *of_id;
  693. const int (*init_fn)(struct arm_pmu *);
  694. struct device_node *node = pdev->dev.of_node;
  695. struct arm_pmu *pmu;
  696. int ret = -ENODEV;
  697. pmu = kzalloc(sizeof(struct arm_pmu), GFP_KERNEL);
  698. if (!pmu) {
  699. pr_info("failed to allocate PMU device!\n");
  700. return -ENOMEM;
  701. }
  702. if (!__oprofile_cpu_pmu)
  703. __oprofile_cpu_pmu = pmu;
  704. pmu->plat_device = pdev;
  705. if (node && (of_id = of_match_node(of_table, pdev->dev.of_node))) {
  706. init_fn = of_id->data;
  707. ret = of_pmu_irq_cfg(pmu);
  708. if (!ret)
  709. ret = init_fn(pmu);
  710. } else {
  711. ret = probe_current_pmu(pmu, probe_table);
  712. cpumask_setall(&pmu->supported_cpus);
  713. }
  714. if (ret) {
  715. pr_info("failed to probe PMU!\n");
  716. goto out_free;
  717. }
  718. ret = cpu_pmu_init(pmu);
  719. if (ret)
  720. goto out_free;
  721. ret = armpmu_register(pmu, -1);
  722. if (ret)
  723. goto out_destroy;
  724. return 0;
  725. out_destroy:
  726. cpu_pmu_destroy(pmu);
  727. out_free:
  728. pr_info("failed to register PMU devices!\n");
  729. kfree(pmu);
  730. return ret;
  731. }