arm_pmu.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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_mask(&cpu_pmu->supported_cpus,
  495. cpu_pmu_disable_percpu_irq, &irq, 1);
  496. free_percpu_irq(irq, &hw_events->percpu_pmu);
  497. } else {
  498. for (i = 0; i < irqs; ++i) {
  499. int cpu = i;
  500. if (cpu_pmu->irq_affinity)
  501. cpu = cpu_pmu->irq_affinity[i];
  502. if (!cpumask_test_and_clear_cpu(cpu, &cpu_pmu->active_irqs))
  503. continue;
  504. irq = platform_get_irq(pmu_device, i);
  505. if (irq >= 0)
  506. free_irq(irq, per_cpu_ptr(&hw_events->percpu_pmu, cpu));
  507. }
  508. }
  509. }
  510. static int cpu_pmu_request_irq(struct arm_pmu *cpu_pmu, irq_handler_t handler)
  511. {
  512. int i, err, irq, irqs;
  513. struct platform_device *pmu_device = cpu_pmu->plat_device;
  514. struct pmu_hw_events __percpu *hw_events = cpu_pmu->hw_events;
  515. if (!pmu_device)
  516. return -ENODEV;
  517. irqs = min(pmu_device->num_resources, num_possible_cpus());
  518. if (irqs < 1) {
  519. pr_warn_once("perf/ARM: No irqs for PMU defined, sampling events not supported\n");
  520. return 0;
  521. }
  522. irq = platform_get_irq(pmu_device, 0);
  523. if (irq >= 0 && irq_is_percpu(irq)) {
  524. err = request_percpu_irq(irq, handler, "arm-pmu",
  525. &hw_events->percpu_pmu);
  526. if (err) {
  527. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  528. irq);
  529. return err;
  530. }
  531. on_each_cpu_mask(&cpu_pmu->supported_cpus,
  532. cpu_pmu_enable_percpu_irq, &irq, 1);
  533. } else {
  534. for (i = 0; i < irqs; ++i) {
  535. int cpu = i;
  536. err = 0;
  537. irq = platform_get_irq(pmu_device, i);
  538. if (irq < 0)
  539. continue;
  540. if (cpu_pmu->irq_affinity)
  541. cpu = cpu_pmu->irq_affinity[i];
  542. /*
  543. * If we have a single PMU interrupt that we can't shift,
  544. * assume that we're running on a uniprocessor machine and
  545. * continue. Otherwise, continue without this interrupt.
  546. */
  547. if (irq_set_affinity(irq, cpumask_of(cpu)) && irqs > 1) {
  548. pr_warn("unable to set irq affinity (irq=%d, cpu=%u)\n",
  549. irq, cpu);
  550. continue;
  551. }
  552. err = request_irq(irq, handler,
  553. IRQF_NOBALANCING | IRQF_NO_THREAD, "arm-pmu",
  554. per_cpu_ptr(&hw_events->percpu_pmu, cpu));
  555. if (err) {
  556. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  557. irq);
  558. return err;
  559. }
  560. cpumask_set_cpu(cpu, &cpu_pmu->active_irqs);
  561. }
  562. }
  563. return 0;
  564. }
  565. static DEFINE_SPINLOCK(arm_pmu_lock);
  566. static LIST_HEAD(arm_pmu_list);
  567. /*
  568. * PMU hardware loses all context when a CPU goes offline.
  569. * When a CPU is hotplugged back in, since some hardware registers are
  570. * UNKNOWN at reset, the PMU must be explicitly reset to avoid reading
  571. * junk values out of them.
  572. */
  573. static int arm_perf_starting_cpu(unsigned int cpu)
  574. {
  575. struct arm_pmu *pmu;
  576. spin_lock(&arm_pmu_lock);
  577. list_for_each_entry(pmu, &arm_pmu_list, entry) {
  578. if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
  579. continue;
  580. if (pmu->reset)
  581. pmu->reset(pmu);
  582. }
  583. spin_unlock(&arm_pmu_lock);
  584. return 0;
  585. }
  586. #ifdef CONFIG_CPU_PM
  587. static void cpu_pm_pmu_setup(struct arm_pmu *armpmu, unsigned long cmd)
  588. {
  589. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  590. struct perf_event *event;
  591. int idx;
  592. for (idx = 0; idx < armpmu->num_events; idx++) {
  593. /*
  594. * If the counter is not used skip it, there is no
  595. * need of stopping/restarting it.
  596. */
  597. if (!test_bit(idx, hw_events->used_mask))
  598. continue;
  599. event = hw_events->events[idx];
  600. switch (cmd) {
  601. case CPU_PM_ENTER:
  602. /*
  603. * Stop and update the counter
  604. */
  605. armpmu_stop(event, PERF_EF_UPDATE);
  606. break;
  607. case CPU_PM_EXIT:
  608. case CPU_PM_ENTER_FAILED:
  609. /*
  610. * Restore and enable the counter.
  611. * armpmu_start() indirectly calls
  612. *
  613. * perf_event_update_userpage()
  614. *
  615. * that requires RCU read locking to be functional,
  616. * wrap the call within RCU_NONIDLE to make the
  617. * RCU subsystem aware this cpu is not idle from
  618. * an RCU perspective for the armpmu_start() call
  619. * duration.
  620. */
  621. RCU_NONIDLE(armpmu_start(event, PERF_EF_RELOAD));
  622. break;
  623. default:
  624. break;
  625. }
  626. }
  627. }
  628. static int cpu_pm_pmu_notify(struct notifier_block *b, unsigned long cmd,
  629. void *v)
  630. {
  631. struct arm_pmu *armpmu = container_of(b, struct arm_pmu, cpu_pm_nb);
  632. struct pmu_hw_events *hw_events = this_cpu_ptr(armpmu->hw_events);
  633. int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
  634. if (!cpumask_test_cpu(smp_processor_id(), &armpmu->supported_cpus))
  635. return NOTIFY_DONE;
  636. /*
  637. * Always reset the PMU registers on power-up even if
  638. * there are no events running.
  639. */
  640. if (cmd == CPU_PM_EXIT && armpmu->reset)
  641. armpmu->reset(armpmu);
  642. if (!enabled)
  643. return NOTIFY_OK;
  644. switch (cmd) {
  645. case CPU_PM_ENTER:
  646. armpmu->stop(armpmu);
  647. cpu_pm_pmu_setup(armpmu, cmd);
  648. break;
  649. case CPU_PM_EXIT:
  650. cpu_pm_pmu_setup(armpmu, cmd);
  651. case CPU_PM_ENTER_FAILED:
  652. armpmu->start(armpmu);
  653. break;
  654. default:
  655. return NOTIFY_DONE;
  656. }
  657. return NOTIFY_OK;
  658. }
  659. static int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu)
  660. {
  661. cpu_pmu->cpu_pm_nb.notifier_call = cpu_pm_pmu_notify;
  662. return cpu_pm_register_notifier(&cpu_pmu->cpu_pm_nb);
  663. }
  664. static void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu)
  665. {
  666. cpu_pm_unregister_notifier(&cpu_pmu->cpu_pm_nb);
  667. }
  668. #else
  669. static inline int cpu_pm_pmu_register(struct arm_pmu *cpu_pmu) { return 0; }
  670. static inline void cpu_pm_pmu_unregister(struct arm_pmu *cpu_pmu) { }
  671. #endif
  672. static int cpu_pmu_init(struct arm_pmu *cpu_pmu)
  673. {
  674. int err;
  675. int cpu;
  676. struct pmu_hw_events __percpu *cpu_hw_events;
  677. cpu_hw_events = alloc_percpu(struct pmu_hw_events);
  678. if (!cpu_hw_events)
  679. return -ENOMEM;
  680. spin_lock(&arm_pmu_lock);
  681. list_add_tail(&cpu_pmu->entry, &arm_pmu_list);
  682. spin_unlock(&arm_pmu_lock);
  683. err = cpu_pm_pmu_register(cpu_pmu);
  684. if (err)
  685. goto out_unregister;
  686. for_each_possible_cpu(cpu) {
  687. struct pmu_hw_events *events = per_cpu_ptr(cpu_hw_events, cpu);
  688. raw_spin_lock_init(&events->pmu_lock);
  689. events->percpu_pmu = cpu_pmu;
  690. }
  691. cpu_pmu->hw_events = cpu_hw_events;
  692. cpu_pmu->request_irq = cpu_pmu_request_irq;
  693. cpu_pmu->free_irq = cpu_pmu_free_irq;
  694. /* Ensure the PMU has sane values out of reset. */
  695. if (cpu_pmu->reset)
  696. on_each_cpu_mask(&cpu_pmu->supported_cpus, cpu_pmu->reset,
  697. cpu_pmu, 1);
  698. /* If no interrupts available, set the corresponding capability flag */
  699. if (!platform_get_irq(cpu_pmu->plat_device, 0))
  700. cpu_pmu->pmu.capabilities |= PERF_PMU_CAP_NO_INTERRUPT;
  701. /*
  702. * This is a CPU PMU potentially in a heterogeneous configuration (e.g.
  703. * big.LITTLE). This is not an uncore PMU, and we have taken ctx
  704. * sharing into account (e.g. with our pmu::filter_match callback and
  705. * pmu::event_init group validation).
  706. */
  707. cpu_pmu->pmu.capabilities |= PERF_PMU_CAP_HETEROGENEOUS_CPUS;
  708. return 0;
  709. out_unregister:
  710. spin_lock(&arm_pmu_lock);
  711. list_del(&cpu_pmu->entry);
  712. spin_unlock(&arm_pmu_lock);
  713. free_percpu(cpu_hw_events);
  714. return err;
  715. }
  716. static void cpu_pmu_destroy(struct arm_pmu *cpu_pmu)
  717. {
  718. cpu_pm_pmu_unregister(cpu_pmu);
  719. spin_lock(&arm_pmu_lock);
  720. list_del(&cpu_pmu->entry);
  721. spin_unlock(&arm_pmu_lock);
  722. free_percpu(cpu_pmu->hw_events);
  723. }
  724. /*
  725. * CPU PMU identification and probing.
  726. */
  727. static int probe_current_pmu(struct arm_pmu *pmu,
  728. const struct pmu_probe_info *info)
  729. {
  730. int cpu = get_cpu();
  731. unsigned int cpuid = read_cpuid_id();
  732. int ret = -ENODEV;
  733. pr_info("probing PMU on CPU %d\n", cpu);
  734. for (; info->init != NULL; info++) {
  735. if ((cpuid & info->mask) != info->cpuid)
  736. continue;
  737. ret = info->init(pmu);
  738. break;
  739. }
  740. put_cpu();
  741. return ret;
  742. }
  743. static int of_pmu_irq_cfg(struct arm_pmu *pmu)
  744. {
  745. int *irqs, i = 0;
  746. bool using_spi = false;
  747. struct platform_device *pdev = pmu->plat_device;
  748. irqs = kcalloc(pdev->num_resources, sizeof(*irqs), GFP_KERNEL);
  749. if (!irqs)
  750. return -ENOMEM;
  751. do {
  752. struct device_node *dn;
  753. int cpu, irq;
  754. /* See if we have an affinity entry */
  755. dn = of_parse_phandle(pdev->dev.of_node, "interrupt-affinity", i);
  756. if (!dn)
  757. break;
  758. /* Check the IRQ type and prohibit a mix of PPIs and SPIs */
  759. irq = platform_get_irq(pdev, i);
  760. if (irq >= 0) {
  761. bool spi = !irq_is_percpu(irq);
  762. if (i > 0 && spi != using_spi) {
  763. pr_err("PPI/SPI IRQ type mismatch for %s!\n",
  764. dn->name);
  765. kfree(irqs);
  766. return -EINVAL;
  767. }
  768. using_spi = spi;
  769. }
  770. /* Now look up the logical CPU number */
  771. for_each_possible_cpu(cpu) {
  772. struct device_node *cpu_dn;
  773. cpu_dn = of_cpu_device_node_get(cpu);
  774. of_node_put(cpu_dn);
  775. if (dn == cpu_dn)
  776. break;
  777. }
  778. if (cpu >= nr_cpu_ids) {
  779. pr_warn("Failed to find logical CPU for %s\n",
  780. dn->name);
  781. of_node_put(dn);
  782. cpumask_setall(&pmu->supported_cpus);
  783. break;
  784. }
  785. of_node_put(dn);
  786. /* For SPIs, we need to track the affinity per IRQ */
  787. if (using_spi) {
  788. if (i >= pdev->num_resources)
  789. break;
  790. irqs[i] = cpu;
  791. }
  792. /* Keep track of the CPUs containing this PMU type */
  793. cpumask_set_cpu(cpu, &pmu->supported_cpus);
  794. i++;
  795. } while (1);
  796. /* If we didn't manage to parse anything, try the interrupt affinity */
  797. if (cpumask_weight(&pmu->supported_cpus) == 0) {
  798. int irq = platform_get_irq(pdev, 0);
  799. if (irq_is_percpu(irq)) {
  800. /* If using PPIs, check the affinity of the partition */
  801. int ret;
  802. ret = irq_get_percpu_devid_partition(irq, &pmu->supported_cpus);
  803. if (ret) {
  804. kfree(irqs);
  805. return ret;
  806. }
  807. } else {
  808. /* Otherwise default to all CPUs */
  809. cpumask_setall(&pmu->supported_cpus);
  810. }
  811. }
  812. /* If we matched up the IRQ affinities, use them to route the SPIs */
  813. if (using_spi && i == pdev->num_resources)
  814. pmu->irq_affinity = irqs;
  815. else
  816. kfree(irqs);
  817. return 0;
  818. }
  819. int arm_pmu_device_probe(struct platform_device *pdev,
  820. const struct of_device_id *of_table,
  821. const struct pmu_probe_info *probe_table)
  822. {
  823. const struct of_device_id *of_id;
  824. const int (*init_fn)(struct arm_pmu *);
  825. struct device_node *node = pdev->dev.of_node;
  826. struct arm_pmu *pmu;
  827. int ret = -ENODEV;
  828. pmu = kzalloc(sizeof(struct arm_pmu), GFP_KERNEL);
  829. if (!pmu) {
  830. pr_info("failed to allocate PMU device!\n");
  831. return -ENOMEM;
  832. }
  833. armpmu_init(pmu);
  834. pmu->plat_device = pdev;
  835. if (node && (of_id = of_match_node(of_table, pdev->dev.of_node))) {
  836. init_fn = of_id->data;
  837. pmu->secure_access = of_property_read_bool(pdev->dev.of_node,
  838. "secure-reg-access");
  839. /* arm64 systems boot only as non-secure */
  840. if (IS_ENABLED(CONFIG_ARM64) && pmu->secure_access) {
  841. pr_warn("ignoring \"secure-reg-access\" property for arm64\n");
  842. pmu->secure_access = false;
  843. }
  844. ret = of_pmu_irq_cfg(pmu);
  845. if (!ret)
  846. ret = init_fn(pmu);
  847. } else {
  848. cpumask_setall(&pmu->supported_cpus);
  849. ret = probe_current_pmu(pmu, probe_table);
  850. }
  851. if (ret) {
  852. pr_info("%s: failed to probe PMU!\n", of_node_full_name(node));
  853. goto out_free;
  854. }
  855. ret = cpu_pmu_init(pmu);
  856. if (ret)
  857. goto out_free;
  858. ret = perf_pmu_register(&pmu->pmu, pmu->name, -1);
  859. if (ret)
  860. goto out_destroy;
  861. if (!__oprofile_cpu_pmu)
  862. __oprofile_cpu_pmu = pmu;
  863. pr_info("enabled with %s PMU driver, %d counters available\n",
  864. pmu->name, pmu->num_events);
  865. return 0;
  866. out_destroy:
  867. cpu_pmu_destroy(pmu);
  868. out_free:
  869. pr_info("%s: failed to register PMU devices!\n",
  870. of_node_full_name(node));
  871. kfree(pmu->irq_affinity);
  872. kfree(pmu);
  873. return ret;
  874. }
  875. static int arm_pmu_hp_init(void)
  876. {
  877. int ret;
  878. ret = cpuhp_setup_state_nocalls(CPUHP_AP_PERF_ARM_STARTING,
  879. "AP_PERF_ARM_STARTING",
  880. arm_perf_starting_cpu, NULL);
  881. if (ret)
  882. pr_err("CPU hotplug notifier for ARM PMU could not be registered: %d\n",
  883. ret);
  884. return ret;
  885. }
  886. subsys_initcall(arm_pmu_hp_init);