arm_pmu.c 26 KB

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