perf_event.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408
  1. /*
  2. * PMU support
  3. *
  4. * Copyright (C) 2012 ARM Limited
  5. * Author: Will Deacon <will.deacon@arm.com>
  6. *
  7. * This code is based heavily on the ARMv7 perf event code.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #define pr_fmt(fmt) "hw perfevents: " fmt
  22. #include <linux/bitmap.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/kernel.h>
  25. #include <linux/export.h>
  26. #include <linux/perf_event.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/spinlock.h>
  29. #include <linux/uaccess.h>
  30. #include <asm/cputype.h>
  31. #include <asm/irq.h>
  32. #include <asm/irq_regs.h>
  33. #include <asm/pmu.h>
  34. #include <asm/stacktrace.h>
  35. /*
  36. * ARMv8 supports a maximum of 32 events.
  37. * The cycle counter is included in this total.
  38. */
  39. #define ARMPMU_MAX_HWEVENTS 32
  40. static DEFINE_PER_CPU(struct perf_event * [ARMPMU_MAX_HWEVENTS], hw_events);
  41. static DEFINE_PER_CPU(unsigned long [BITS_TO_LONGS(ARMPMU_MAX_HWEVENTS)], used_mask);
  42. static DEFINE_PER_CPU(struct pmu_hw_events, cpu_hw_events);
  43. #define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu))
  44. /* Set at runtime when we know what CPU type we are. */
  45. static struct arm_pmu *cpu_pmu;
  46. int
  47. armpmu_get_max_events(void)
  48. {
  49. int max_events = 0;
  50. if (cpu_pmu != NULL)
  51. max_events = cpu_pmu->num_events;
  52. return max_events;
  53. }
  54. EXPORT_SYMBOL_GPL(armpmu_get_max_events);
  55. int perf_num_counters(void)
  56. {
  57. return armpmu_get_max_events();
  58. }
  59. EXPORT_SYMBOL_GPL(perf_num_counters);
  60. #define HW_OP_UNSUPPORTED 0xFFFF
  61. #define C(_x) \
  62. PERF_COUNT_HW_CACHE_##_x
  63. #define CACHE_OP_UNSUPPORTED 0xFFFF
  64. static int
  65. armpmu_map_cache_event(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. u64 config)
  70. {
  71. unsigned int cache_type, cache_op, cache_result, ret;
  72. cache_type = (config >> 0) & 0xff;
  73. if (cache_type >= PERF_COUNT_HW_CACHE_MAX)
  74. return -EINVAL;
  75. cache_op = (config >> 8) & 0xff;
  76. if (cache_op >= PERF_COUNT_HW_CACHE_OP_MAX)
  77. return -EINVAL;
  78. cache_result = (config >> 16) & 0xff;
  79. if (cache_result >= PERF_COUNT_HW_CACHE_RESULT_MAX)
  80. return -EINVAL;
  81. ret = (int)(*cache_map)[cache_type][cache_op][cache_result];
  82. if (ret == CACHE_OP_UNSUPPORTED)
  83. return -ENOENT;
  84. return ret;
  85. }
  86. static int
  87. armpmu_map_event(const unsigned (*event_map)[PERF_COUNT_HW_MAX], u64 config)
  88. {
  89. int mapping;
  90. if (config >= PERF_COUNT_HW_MAX)
  91. return -EINVAL;
  92. mapping = (*event_map)[config];
  93. return mapping == HW_OP_UNSUPPORTED ? -ENOENT : mapping;
  94. }
  95. static int
  96. armpmu_map_raw_event(u32 raw_event_mask, u64 config)
  97. {
  98. return (int)(config & raw_event_mask);
  99. }
  100. static int map_cpu_event(struct perf_event *event,
  101. const unsigned (*event_map)[PERF_COUNT_HW_MAX],
  102. const unsigned (*cache_map)
  103. [PERF_COUNT_HW_CACHE_MAX]
  104. [PERF_COUNT_HW_CACHE_OP_MAX]
  105. [PERF_COUNT_HW_CACHE_RESULT_MAX],
  106. u32 raw_event_mask)
  107. {
  108. u64 config = event->attr.config;
  109. switch (event->attr.type) {
  110. case PERF_TYPE_HARDWARE:
  111. return armpmu_map_event(event_map, config);
  112. case PERF_TYPE_HW_CACHE:
  113. return armpmu_map_cache_event(cache_map, config);
  114. case PERF_TYPE_RAW:
  115. return armpmu_map_raw_event(raw_event_mask, config);
  116. }
  117. return -ENOENT;
  118. }
  119. int
  120. armpmu_event_set_period(struct perf_event *event,
  121. struct hw_perf_event *hwc,
  122. int idx)
  123. {
  124. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  125. s64 left = local64_read(&hwc->period_left);
  126. s64 period = hwc->sample_period;
  127. int ret = 0;
  128. if (unlikely(left <= -period)) {
  129. left = period;
  130. local64_set(&hwc->period_left, left);
  131. hwc->last_period = period;
  132. ret = 1;
  133. }
  134. if (unlikely(left <= 0)) {
  135. left += period;
  136. local64_set(&hwc->period_left, left);
  137. hwc->last_period = period;
  138. ret = 1;
  139. }
  140. if (left > (s64)armpmu->max_period)
  141. left = armpmu->max_period;
  142. local64_set(&hwc->prev_count, (u64)-left);
  143. armpmu->write_counter(idx, (u64)(-left) & 0xffffffff);
  144. perf_event_update_userpage(event);
  145. return ret;
  146. }
  147. u64
  148. armpmu_event_update(struct perf_event *event,
  149. struct hw_perf_event *hwc,
  150. int idx)
  151. {
  152. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  153. u64 delta, prev_raw_count, new_raw_count;
  154. again:
  155. prev_raw_count = local64_read(&hwc->prev_count);
  156. new_raw_count = armpmu->read_counter(idx);
  157. if (local64_cmpxchg(&hwc->prev_count, prev_raw_count,
  158. new_raw_count) != prev_raw_count)
  159. goto again;
  160. delta = (new_raw_count - prev_raw_count) & armpmu->max_period;
  161. local64_add(delta, &event->count);
  162. local64_sub(delta, &hwc->period_left);
  163. return new_raw_count;
  164. }
  165. static void
  166. armpmu_read(struct perf_event *event)
  167. {
  168. struct hw_perf_event *hwc = &event->hw;
  169. /* Don't read disabled counters! */
  170. if (hwc->idx < 0)
  171. return;
  172. armpmu_event_update(event, hwc, hwc->idx);
  173. }
  174. static void
  175. armpmu_stop(struct perf_event *event, int flags)
  176. {
  177. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  178. struct hw_perf_event *hwc = &event->hw;
  179. /*
  180. * ARM pmu always has to update the counter, so ignore
  181. * PERF_EF_UPDATE, see comments in armpmu_start().
  182. */
  183. if (!(hwc->state & PERF_HES_STOPPED)) {
  184. armpmu->disable(hwc, hwc->idx);
  185. barrier(); /* why? */
  186. armpmu_event_update(event, hwc, hwc->idx);
  187. hwc->state |= PERF_HES_STOPPED | PERF_HES_UPTODATE;
  188. }
  189. }
  190. static void
  191. armpmu_start(struct perf_event *event, int flags)
  192. {
  193. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  194. struct hw_perf_event *hwc = &event->hw;
  195. /*
  196. * ARM pmu always has to reprogram the period, so ignore
  197. * PERF_EF_RELOAD, see the comment below.
  198. */
  199. if (flags & PERF_EF_RELOAD)
  200. WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));
  201. hwc->state = 0;
  202. /*
  203. * Set the period again. Some counters can't be stopped, so when we
  204. * were stopped we simply disabled the IRQ source and the counter
  205. * may have been left counting. If we don't do this step then we may
  206. * get an interrupt too soon or *way* too late if the overflow has
  207. * happened since disabling.
  208. */
  209. armpmu_event_set_period(event, hwc, hwc->idx);
  210. armpmu->enable(hwc, hwc->idx);
  211. }
  212. static void
  213. armpmu_del(struct perf_event *event, int flags)
  214. {
  215. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  216. struct pmu_hw_events *hw_events = armpmu->get_hw_events();
  217. struct hw_perf_event *hwc = &event->hw;
  218. int idx = hwc->idx;
  219. WARN_ON(idx < 0);
  220. armpmu_stop(event, PERF_EF_UPDATE);
  221. hw_events->events[idx] = NULL;
  222. clear_bit(idx, hw_events->used_mask);
  223. perf_event_update_userpage(event);
  224. }
  225. static int
  226. armpmu_add(struct perf_event *event, int flags)
  227. {
  228. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  229. struct pmu_hw_events *hw_events = armpmu->get_hw_events();
  230. struct hw_perf_event *hwc = &event->hw;
  231. int idx;
  232. int err = 0;
  233. perf_pmu_disable(event->pmu);
  234. /* If we don't have a space for the counter then finish early. */
  235. idx = armpmu->get_event_idx(hw_events, hwc);
  236. if (idx < 0) {
  237. err = idx;
  238. goto out;
  239. }
  240. /*
  241. * If there is an event in the counter we are going to use then make
  242. * sure it is disabled.
  243. */
  244. event->hw.idx = idx;
  245. armpmu->disable(hwc, idx);
  246. hw_events->events[idx] = event;
  247. hwc->state = PERF_HES_STOPPED | PERF_HES_UPTODATE;
  248. if (flags & PERF_EF_START)
  249. armpmu_start(event, PERF_EF_RELOAD);
  250. /* Propagate our changes to the userspace mapping. */
  251. perf_event_update_userpage(event);
  252. out:
  253. perf_pmu_enable(event->pmu);
  254. return err;
  255. }
  256. static int
  257. validate_event(struct pmu_hw_events *hw_events,
  258. struct perf_event *event)
  259. {
  260. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  261. struct hw_perf_event fake_event = event->hw;
  262. struct pmu *leader_pmu = event->group_leader->pmu;
  263. if (is_software_event(event))
  264. return 1;
  265. if (event->pmu != leader_pmu || event->state <= PERF_EVENT_STATE_OFF)
  266. return 1;
  267. return armpmu->get_event_idx(hw_events, &fake_event) >= 0;
  268. }
  269. static int
  270. validate_group(struct perf_event *event)
  271. {
  272. struct perf_event *sibling, *leader = event->group_leader;
  273. struct pmu_hw_events fake_pmu;
  274. DECLARE_BITMAP(fake_used_mask, ARMPMU_MAX_HWEVENTS);
  275. /*
  276. * Initialise the fake PMU. We only need to populate the
  277. * used_mask for the purposes of validation.
  278. */
  279. memset(fake_used_mask, 0, sizeof(fake_used_mask));
  280. fake_pmu.used_mask = fake_used_mask;
  281. if (!validate_event(&fake_pmu, leader))
  282. return -EINVAL;
  283. list_for_each_entry(sibling, &leader->sibling_list, group_entry) {
  284. if (!validate_event(&fake_pmu, sibling))
  285. return -EINVAL;
  286. }
  287. if (!validate_event(&fake_pmu, event))
  288. return -EINVAL;
  289. return 0;
  290. }
  291. static void
  292. armpmu_release_hardware(struct arm_pmu *armpmu)
  293. {
  294. int i, irq, irqs;
  295. struct platform_device *pmu_device = armpmu->plat_device;
  296. irqs = min(pmu_device->num_resources, num_possible_cpus());
  297. for (i = 0; i < irqs; ++i) {
  298. if (!cpumask_test_and_clear_cpu(i, &armpmu->active_irqs))
  299. continue;
  300. irq = platform_get_irq(pmu_device, i);
  301. if (irq >= 0)
  302. free_irq(irq, armpmu);
  303. }
  304. }
  305. static int
  306. armpmu_reserve_hardware(struct arm_pmu *armpmu)
  307. {
  308. int i, err, irq, irqs;
  309. struct platform_device *pmu_device = armpmu->plat_device;
  310. if (!pmu_device) {
  311. pr_err("no PMU device registered\n");
  312. return -ENODEV;
  313. }
  314. irqs = min(pmu_device->num_resources, num_possible_cpus());
  315. if (irqs < 1) {
  316. pr_err("no irqs for PMUs defined\n");
  317. return -ENODEV;
  318. }
  319. for (i = 0; i < irqs; ++i) {
  320. err = 0;
  321. irq = platform_get_irq(pmu_device, i);
  322. if (irq < 0)
  323. continue;
  324. /*
  325. * If we have a single PMU interrupt that we can't shift,
  326. * assume that we're running on a uniprocessor machine and
  327. * continue. Otherwise, continue without this interrupt.
  328. */
  329. if (irq_set_affinity(irq, cpumask_of(i)) && irqs > 1) {
  330. pr_warning("unable to set irq affinity (irq=%d, cpu=%u)\n",
  331. irq, i);
  332. continue;
  333. }
  334. err = request_irq(irq, armpmu->handle_irq,
  335. IRQF_NOBALANCING,
  336. "arm-pmu", armpmu);
  337. if (err) {
  338. pr_err("unable to request IRQ%d for ARM PMU counters\n",
  339. irq);
  340. armpmu_release_hardware(armpmu);
  341. return err;
  342. }
  343. cpumask_set_cpu(i, &armpmu->active_irqs);
  344. }
  345. return 0;
  346. }
  347. static void
  348. hw_perf_event_destroy(struct perf_event *event)
  349. {
  350. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  351. atomic_t *active_events = &armpmu->active_events;
  352. struct mutex *pmu_reserve_mutex = &armpmu->reserve_mutex;
  353. if (atomic_dec_and_mutex_lock(active_events, pmu_reserve_mutex)) {
  354. armpmu_release_hardware(armpmu);
  355. mutex_unlock(pmu_reserve_mutex);
  356. }
  357. }
  358. static int
  359. event_requires_mode_exclusion(struct perf_event_attr *attr)
  360. {
  361. return attr->exclude_idle || attr->exclude_user ||
  362. attr->exclude_kernel || attr->exclude_hv;
  363. }
  364. static int
  365. __hw_perf_event_init(struct perf_event *event)
  366. {
  367. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  368. struct hw_perf_event *hwc = &event->hw;
  369. int mapping, err;
  370. mapping = armpmu->map_event(event);
  371. if (mapping < 0) {
  372. pr_debug("event %x:%llx not supported\n", event->attr.type,
  373. event->attr.config);
  374. return mapping;
  375. }
  376. /*
  377. * We don't assign an index until we actually place the event onto
  378. * hardware. Use -1 to signify that we haven't decided where to put it
  379. * yet. For SMP systems, each core has it's own PMU so we can't do any
  380. * clever allocation or constraints checking at this point.
  381. */
  382. hwc->idx = -1;
  383. hwc->config_base = 0;
  384. hwc->config = 0;
  385. hwc->event_base = 0;
  386. /*
  387. * Check whether we need to exclude the counter from certain modes.
  388. */
  389. if ((!armpmu->set_event_filter ||
  390. armpmu->set_event_filter(hwc, &event->attr)) &&
  391. event_requires_mode_exclusion(&event->attr)) {
  392. pr_debug("ARM performance counters do not support mode exclusion\n");
  393. return -EPERM;
  394. }
  395. /*
  396. * Store the event encoding into the config_base field.
  397. */
  398. hwc->config_base |= (unsigned long)mapping;
  399. if (!hwc->sample_period) {
  400. /*
  401. * For non-sampling runs, limit the sample_period to half
  402. * of the counter width. That way, the new counter value
  403. * is far less likely to overtake the previous one unless
  404. * you have some serious IRQ latency issues.
  405. */
  406. hwc->sample_period = armpmu->max_period >> 1;
  407. hwc->last_period = hwc->sample_period;
  408. local64_set(&hwc->period_left, hwc->sample_period);
  409. }
  410. err = 0;
  411. if (event->group_leader != event) {
  412. err = validate_group(event);
  413. if (err)
  414. return -EINVAL;
  415. }
  416. return err;
  417. }
  418. static int armpmu_event_init(struct perf_event *event)
  419. {
  420. struct arm_pmu *armpmu = to_arm_pmu(event->pmu);
  421. int err = 0;
  422. atomic_t *active_events = &armpmu->active_events;
  423. if (armpmu->map_event(event) == -ENOENT)
  424. return -ENOENT;
  425. event->destroy = hw_perf_event_destroy;
  426. if (!atomic_inc_not_zero(active_events)) {
  427. mutex_lock(&armpmu->reserve_mutex);
  428. if (atomic_read(active_events) == 0)
  429. err = armpmu_reserve_hardware(armpmu);
  430. if (!err)
  431. atomic_inc(active_events);
  432. mutex_unlock(&armpmu->reserve_mutex);
  433. }
  434. if (err)
  435. return err;
  436. err = __hw_perf_event_init(event);
  437. if (err)
  438. hw_perf_event_destroy(event);
  439. return err;
  440. }
  441. static void armpmu_enable(struct pmu *pmu)
  442. {
  443. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  444. struct pmu_hw_events *hw_events = armpmu->get_hw_events();
  445. int enabled = bitmap_weight(hw_events->used_mask, armpmu->num_events);
  446. if (enabled)
  447. armpmu->start();
  448. }
  449. static void armpmu_disable(struct pmu *pmu)
  450. {
  451. struct arm_pmu *armpmu = to_arm_pmu(pmu);
  452. armpmu->stop();
  453. }
  454. static void __init armpmu_init(struct arm_pmu *armpmu)
  455. {
  456. atomic_set(&armpmu->active_events, 0);
  457. mutex_init(&armpmu->reserve_mutex);
  458. armpmu->pmu = (struct pmu) {
  459. .pmu_enable = armpmu_enable,
  460. .pmu_disable = armpmu_disable,
  461. .event_init = armpmu_event_init,
  462. .add = armpmu_add,
  463. .del = armpmu_del,
  464. .start = armpmu_start,
  465. .stop = armpmu_stop,
  466. .read = armpmu_read,
  467. };
  468. }
  469. int __init armpmu_register(struct arm_pmu *armpmu, char *name, int type)
  470. {
  471. armpmu_init(armpmu);
  472. return perf_pmu_register(&armpmu->pmu, name, type);
  473. }
  474. /*
  475. * ARMv8 PMUv3 Performance Events handling code.
  476. * Common event types.
  477. */
  478. enum armv8_pmuv3_perf_types {
  479. /* Required events. */
  480. ARMV8_PMUV3_PERFCTR_PMNC_SW_INCR = 0x00,
  481. ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL = 0x03,
  482. ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS = 0x04,
  483. ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED = 0x10,
  484. ARMV8_PMUV3_PERFCTR_CLOCK_CYCLES = 0x11,
  485. ARMV8_PMUV3_PERFCTR_PC_BRANCH_PRED = 0x12,
  486. /* At least one of the following is required. */
  487. ARMV8_PMUV3_PERFCTR_INSTR_EXECUTED = 0x08,
  488. ARMV8_PMUV3_PERFCTR_OP_SPEC = 0x1B,
  489. /* Common architectural events. */
  490. ARMV8_PMUV3_PERFCTR_MEM_READ = 0x06,
  491. ARMV8_PMUV3_PERFCTR_MEM_WRITE = 0x07,
  492. ARMV8_PMUV3_PERFCTR_EXC_TAKEN = 0x09,
  493. ARMV8_PMUV3_PERFCTR_EXC_EXECUTED = 0x0A,
  494. ARMV8_PMUV3_PERFCTR_CID_WRITE = 0x0B,
  495. ARMV8_PMUV3_PERFCTR_PC_WRITE = 0x0C,
  496. ARMV8_PMUV3_PERFCTR_PC_IMM_BRANCH = 0x0D,
  497. ARMV8_PMUV3_PERFCTR_PC_PROC_RETURN = 0x0E,
  498. ARMV8_PMUV3_PERFCTR_MEM_UNALIGNED_ACCESS = 0x0F,
  499. ARMV8_PMUV3_PERFCTR_TTBR_WRITE = 0x1C,
  500. /* Common microarchitectural events. */
  501. ARMV8_PMUV3_PERFCTR_L1_ICACHE_REFILL = 0x01,
  502. ARMV8_PMUV3_PERFCTR_ITLB_REFILL = 0x02,
  503. ARMV8_PMUV3_PERFCTR_DTLB_REFILL = 0x05,
  504. ARMV8_PMUV3_PERFCTR_MEM_ACCESS = 0x13,
  505. ARMV8_PMUV3_PERFCTR_L1_ICACHE_ACCESS = 0x14,
  506. ARMV8_PMUV3_PERFCTR_L1_DCACHE_WB = 0x15,
  507. ARMV8_PMUV3_PERFCTR_L2_CACHE_ACCESS = 0x16,
  508. ARMV8_PMUV3_PERFCTR_L2_CACHE_REFILL = 0x17,
  509. ARMV8_PMUV3_PERFCTR_L2_CACHE_WB = 0x18,
  510. ARMV8_PMUV3_PERFCTR_BUS_ACCESS = 0x19,
  511. ARMV8_PMUV3_PERFCTR_MEM_ERROR = 0x1A,
  512. ARMV8_PMUV3_PERFCTR_BUS_CYCLES = 0x1D,
  513. };
  514. /* PMUv3 HW events mapping. */
  515. static const unsigned armv8_pmuv3_perf_map[PERF_COUNT_HW_MAX] = {
  516. [PERF_COUNT_HW_CPU_CYCLES] = ARMV8_PMUV3_PERFCTR_CLOCK_CYCLES,
  517. [PERF_COUNT_HW_INSTRUCTIONS] = ARMV8_PMUV3_PERFCTR_INSTR_EXECUTED,
  518. [PERF_COUNT_HW_CACHE_REFERENCES] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS,
  519. [PERF_COUNT_HW_CACHE_MISSES] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL,
  520. [PERF_COUNT_HW_BRANCH_INSTRUCTIONS] = HW_OP_UNSUPPORTED,
  521. [PERF_COUNT_HW_BRANCH_MISSES] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED,
  522. [PERF_COUNT_HW_BUS_CYCLES] = HW_OP_UNSUPPORTED,
  523. [PERF_COUNT_HW_STALLED_CYCLES_FRONTEND] = HW_OP_UNSUPPORTED,
  524. [PERF_COUNT_HW_STALLED_CYCLES_BACKEND] = HW_OP_UNSUPPORTED,
  525. };
  526. static const unsigned armv8_pmuv3_perf_cache_map[PERF_COUNT_HW_CACHE_MAX]
  527. [PERF_COUNT_HW_CACHE_OP_MAX]
  528. [PERF_COUNT_HW_CACHE_RESULT_MAX] = {
  529. [C(L1D)] = {
  530. [C(OP_READ)] = {
  531. [C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS,
  532. [C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL,
  533. },
  534. [C(OP_WRITE)] = {
  535. [C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_ACCESS,
  536. [C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_L1_DCACHE_REFILL,
  537. },
  538. [C(OP_PREFETCH)] = {
  539. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  540. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  541. },
  542. },
  543. [C(L1I)] = {
  544. [C(OP_READ)] = {
  545. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  546. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  547. },
  548. [C(OP_WRITE)] = {
  549. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  550. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  551. },
  552. [C(OP_PREFETCH)] = {
  553. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  554. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  555. },
  556. },
  557. [C(LL)] = {
  558. [C(OP_READ)] = {
  559. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  560. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  561. },
  562. [C(OP_WRITE)] = {
  563. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  564. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  565. },
  566. [C(OP_PREFETCH)] = {
  567. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  568. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  569. },
  570. },
  571. [C(DTLB)] = {
  572. [C(OP_READ)] = {
  573. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  574. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  575. },
  576. [C(OP_WRITE)] = {
  577. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  578. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  579. },
  580. [C(OP_PREFETCH)] = {
  581. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  582. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  583. },
  584. },
  585. [C(ITLB)] = {
  586. [C(OP_READ)] = {
  587. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  588. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  589. },
  590. [C(OP_WRITE)] = {
  591. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  592. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  593. },
  594. [C(OP_PREFETCH)] = {
  595. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  596. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  597. },
  598. },
  599. [C(BPU)] = {
  600. [C(OP_READ)] = {
  601. [C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_PRED,
  602. [C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED,
  603. },
  604. [C(OP_WRITE)] = {
  605. [C(RESULT_ACCESS)] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_PRED,
  606. [C(RESULT_MISS)] = ARMV8_PMUV3_PERFCTR_PC_BRANCH_MIS_PRED,
  607. },
  608. [C(OP_PREFETCH)] = {
  609. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  610. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  611. },
  612. },
  613. [C(NODE)] = {
  614. [C(OP_READ)] = {
  615. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  616. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  617. },
  618. [C(OP_WRITE)] = {
  619. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  620. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  621. },
  622. [C(OP_PREFETCH)] = {
  623. [C(RESULT_ACCESS)] = CACHE_OP_UNSUPPORTED,
  624. [C(RESULT_MISS)] = CACHE_OP_UNSUPPORTED,
  625. },
  626. },
  627. };
  628. /*
  629. * Perf Events' indices
  630. */
  631. #define ARMV8_IDX_CYCLE_COUNTER 0
  632. #define ARMV8_IDX_COUNTER0 1
  633. #define ARMV8_IDX_COUNTER_LAST (ARMV8_IDX_CYCLE_COUNTER + cpu_pmu->num_events - 1)
  634. #define ARMV8_MAX_COUNTERS 32
  635. #define ARMV8_COUNTER_MASK (ARMV8_MAX_COUNTERS - 1)
  636. /*
  637. * ARMv8 low level PMU access
  638. */
  639. /*
  640. * Perf Event to low level counters mapping
  641. */
  642. #define ARMV8_IDX_TO_COUNTER(x) \
  643. (((x) - ARMV8_IDX_COUNTER0) & ARMV8_COUNTER_MASK)
  644. /*
  645. * Per-CPU PMCR: config reg
  646. */
  647. #define ARMV8_PMCR_E (1 << 0) /* Enable all counters */
  648. #define ARMV8_PMCR_P (1 << 1) /* Reset all counters */
  649. #define ARMV8_PMCR_C (1 << 2) /* Cycle counter reset */
  650. #define ARMV8_PMCR_D (1 << 3) /* CCNT counts every 64th cpu cycle */
  651. #define ARMV8_PMCR_X (1 << 4) /* Export to ETM */
  652. #define ARMV8_PMCR_DP (1 << 5) /* Disable CCNT if non-invasive debug*/
  653. #define ARMV8_PMCR_N_SHIFT 11 /* Number of counters supported */
  654. #define ARMV8_PMCR_N_MASK 0x1f
  655. #define ARMV8_PMCR_MASK 0x3f /* Mask for writable bits */
  656. /*
  657. * PMOVSR: counters overflow flag status reg
  658. */
  659. #define ARMV8_OVSR_MASK 0xffffffff /* Mask for writable bits */
  660. #define ARMV8_OVERFLOWED_MASK ARMV8_OVSR_MASK
  661. /*
  662. * PMXEVTYPER: Event selection reg
  663. */
  664. #define ARMV8_EVTYPE_MASK 0xc00000ff /* Mask for writable bits */
  665. #define ARMV8_EVTYPE_EVENT 0xff /* Mask for EVENT bits */
  666. /*
  667. * Event filters for PMUv3
  668. */
  669. #define ARMV8_EXCLUDE_EL1 (1 << 31)
  670. #define ARMV8_EXCLUDE_EL0 (1 << 30)
  671. #define ARMV8_INCLUDE_EL2 (1 << 27)
  672. static inline u32 armv8pmu_pmcr_read(void)
  673. {
  674. u32 val;
  675. asm volatile("mrs %0, pmcr_el0" : "=r" (val));
  676. return val;
  677. }
  678. static inline void armv8pmu_pmcr_write(u32 val)
  679. {
  680. val &= ARMV8_PMCR_MASK;
  681. isb();
  682. asm volatile("msr pmcr_el0, %0" :: "r" (val));
  683. }
  684. static inline int armv8pmu_has_overflowed(u32 pmovsr)
  685. {
  686. return pmovsr & ARMV8_OVERFLOWED_MASK;
  687. }
  688. static inline int armv8pmu_counter_valid(int idx)
  689. {
  690. return idx >= ARMV8_IDX_CYCLE_COUNTER && idx <= ARMV8_IDX_COUNTER_LAST;
  691. }
  692. static inline int armv8pmu_counter_has_overflowed(u32 pmnc, int idx)
  693. {
  694. int ret = 0;
  695. u32 counter;
  696. if (!armv8pmu_counter_valid(idx)) {
  697. pr_err("CPU%u checking wrong counter %d overflow status\n",
  698. smp_processor_id(), idx);
  699. } else {
  700. counter = ARMV8_IDX_TO_COUNTER(idx);
  701. ret = pmnc & BIT(counter);
  702. }
  703. return ret;
  704. }
  705. static inline int armv8pmu_select_counter(int idx)
  706. {
  707. u32 counter;
  708. if (!armv8pmu_counter_valid(idx)) {
  709. pr_err("CPU%u selecting wrong PMNC counter %d\n",
  710. smp_processor_id(), idx);
  711. return -EINVAL;
  712. }
  713. counter = ARMV8_IDX_TO_COUNTER(idx);
  714. asm volatile("msr pmselr_el0, %0" :: "r" (counter));
  715. isb();
  716. return idx;
  717. }
  718. static inline u32 armv8pmu_read_counter(int idx)
  719. {
  720. u32 value = 0;
  721. if (!armv8pmu_counter_valid(idx))
  722. pr_err("CPU%u reading wrong counter %d\n",
  723. smp_processor_id(), idx);
  724. else if (idx == ARMV8_IDX_CYCLE_COUNTER)
  725. asm volatile("mrs %0, pmccntr_el0" : "=r" (value));
  726. else if (armv8pmu_select_counter(idx) == idx)
  727. asm volatile("mrs %0, pmxevcntr_el0" : "=r" (value));
  728. return value;
  729. }
  730. static inline void armv8pmu_write_counter(int idx, u32 value)
  731. {
  732. if (!armv8pmu_counter_valid(idx))
  733. pr_err("CPU%u writing wrong counter %d\n",
  734. smp_processor_id(), idx);
  735. else if (idx == ARMV8_IDX_CYCLE_COUNTER)
  736. asm volatile("msr pmccntr_el0, %0" :: "r" (value));
  737. else if (armv8pmu_select_counter(idx) == idx)
  738. asm volatile("msr pmxevcntr_el0, %0" :: "r" (value));
  739. }
  740. static inline void armv8pmu_write_evtype(int idx, u32 val)
  741. {
  742. if (armv8pmu_select_counter(idx) == idx) {
  743. val &= ARMV8_EVTYPE_MASK;
  744. asm volatile("msr pmxevtyper_el0, %0" :: "r" (val));
  745. }
  746. }
  747. static inline int armv8pmu_enable_counter(int idx)
  748. {
  749. u32 counter;
  750. if (!armv8pmu_counter_valid(idx)) {
  751. pr_err("CPU%u enabling wrong PMNC counter %d\n",
  752. smp_processor_id(), idx);
  753. return -EINVAL;
  754. }
  755. counter = ARMV8_IDX_TO_COUNTER(idx);
  756. asm volatile("msr pmcntenset_el0, %0" :: "r" (BIT(counter)));
  757. return idx;
  758. }
  759. static inline int armv8pmu_disable_counter(int idx)
  760. {
  761. u32 counter;
  762. if (!armv8pmu_counter_valid(idx)) {
  763. pr_err("CPU%u disabling wrong PMNC counter %d\n",
  764. smp_processor_id(), idx);
  765. return -EINVAL;
  766. }
  767. counter = ARMV8_IDX_TO_COUNTER(idx);
  768. asm volatile("msr pmcntenclr_el0, %0" :: "r" (BIT(counter)));
  769. return idx;
  770. }
  771. static inline int armv8pmu_enable_intens(int idx)
  772. {
  773. u32 counter;
  774. if (!armv8pmu_counter_valid(idx)) {
  775. pr_err("CPU%u enabling wrong PMNC counter IRQ enable %d\n",
  776. smp_processor_id(), idx);
  777. return -EINVAL;
  778. }
  779. counter = ARMV8_IDX_TO_COUNTER(idx);
  780. asm volatile("msr pmintenset_el1, %0" :: "r" (BIT(counter)));
  781. return idx;
  782. }
  783. static inline int armv8pmu_disable_intens(int idx)
  784. {
  785. u32 counter;
  786. if (!armv8pmu_counter_valid(idx)) {
  787. pr_err("CPU%u disabling wrong PMNC counter IRQ enable %d\n",
  788. smp_processor_id(), idx);
  789. return -EINVAL;
  790. }
  791. counter = ARMV8_IDX_TO_COUNTER(idx);
  792. asm volatile("msr pmintenclr_el1, %0" :: "r" (BIT(counter)));
  793. isb();
  794. /* Clear the overflow flag in case an interrupt is pending. */
  795. asm volatile("msr pmovsclr_el0, %0" :: "r" (BIT(counter)));
  796. isb();
  797. return idx;
  798. }
  799. static inline u32 armv8pmu_getreset_flags(void)
  800. {
  801. u32 value;
  802. /* Read */
  803. asm volatile("mrs %0, pmovsclr_el0" : "=r" (value));
  804. /* Write to clear flags */
  805. value &= ARMV8_OVSR_MASK;
  806. asm volatile("msr pmovsclr_el0, %0" :: "r" (value));
  807. return value;
  808. }
  809. static void armv8pmu_enable_event(struct hw_perf_event *hwc, int idx)
  810. {
  811. unsigned long flags;
  812. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  813. /*
  814. * Enable counter and interrupt, and set the counter to count
  815. * the event that we're interested in.
  816. */
  817. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  818. /*
  819. * Disable counter
  820. */
  821. armv8pmu_disable_counter(idx);
  822. /*
  823. * Set event (if destined for PMNx counters).
  824. */
  825. armv8pmu_write_evtype(idx, hwc->config_base);
  826. /*
  827. * Enable interrupt for this counter
  828. */
  829. armv8pmu_enable_intens(idx);
  830. /*
  831. * Enable counter
  832. */
  833. armv8pmu_enable_counter(idx);
  834. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  835. }
  836. static void armv8pmu_disable_event(struct hw_perf_event *hwc, int idx)
  837. {
  838. unsigned long flags;
  839. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  840. /*
  841. * Disable counter and interrupt
  842. */
  843. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  844. /*
  845. * Disable counter
  846. */
  847. armv8pmu_disable_counter(idx);
  848. /*
  849. * Disable interrupt for this counter
  850. */
  851. armv8pmu_disable_intens(idx);
  852. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  853. }
  854. static irqreturn_t armv8pmu_handle_irq(int irq_num, void *dev)
  855. {
  856. u32 pmovsr;
  857. struct perf_sample_data data;
  858. struct pmu_hw_events *cpuc;
  859. struct pt_regs *regs;
  860. int idx;
  861. /*
  862. * Get and reset the IRQ flags
  863. */
  864. pmovsr = armv8pmu_getreset_flags();
  865. /*
  866. * Did an overflow occur?
  867. */
  868. if (!armv8pmu_has_overflowed(pmovsr))
  869. return IRQ_NONE;
  870. /*
  871. * Handle the counter(s) overflow(s)
  872. */
  873. regs = get_irq_regs();
  874. cpuc = &__get_cpu_var(cpu_hw_events);
  875. for (idx = 0; idx < cpu_pmu->num_events; ++idx) {
  876. struct perf_event *event = cpuc->events[idx];
  877. struct hw_perf_event *hwc;
  878. /* Ignore if we don't have an event. */
  879. if (!event)
  880. continue;
  881. /*
  882. * We have a single interrupt for all counters. Check that
  883. * each counter has overflowed before we process it.
  884. */
  885. if (!armv8pmu_counter_has_overflowed(pmovsr, idx))
  886. continue;
  887. hwc = &event->hw;
  888. armpmu_event_update(event, hwc, idx);
  889. perf_sample_data_init(&data, 0, hwc->last_period);
  890. if (!armpmu_event_set_period(event, hwc, idx))
  891. continue;
  892. if (perf_event_overflow(event, &data, regs))
  893. cpu_pmu->disable(hwc, idx);
  894. }
  895. /*
  896. * Handle the pending perf events.
  897. *
  898. * Note: this call *must* be run with interrupts disabled. For
  899. * platforms that can have the PMU interrupts raised as an NMI, this
  900. * will not work.
  901. */
  902. irq_work_run();
  903. return IRQ_HANDLED;
  904. }
  905. static void armv8pmu_start(void)
  906. {
  907. unsigned long flags;
  908. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  909. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  910. /* Enable all counters */
  911. armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMCR_E);
  912. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  913. }
  914. static void armv8pmu_stop(void)
  915. {
  916. unsigned long flags;
  917. struct pmu_hw_events *events = cpu_pmu->get_hw_events();
  918. raw_spin_lock_irqsave(&events->pmu_lock, flags);
  919. /* Disable all counters */
  920. armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMCR_E);
  921. raw_spin_unlock_irqrestore(&events->pmu_lock, flags);
  922. }
  923. static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
  924. struct hw_perf_event *event)
  925. {
  926. int idx;
  927. unsigned long evtype = event->config_base & ARMV8_EVTYPE_EVENT;
  928. /* Always place a cycle counter into the cycle counter. */
  929. if (evtype == ARMV8_PMUV3_PERFCTR_CLOCK_CYCLES) {
  930. if (test_and_set_bit(ARMV8_IDX_CYCLE_COUNTER, cpuc->used_mask))
  931. return -EAGAIN;
  932. return ARMV8_IDX_CYCLE_COUNTER;
  933. }
  934. /*
  935. * For anything other than a cycle counter, try and use
  936. * the events counters
  937. */
  938. for (idx = ARMV8_IDX_COUNTER0; idx < cpu_pmu->num_events; ++idx) {
  939. if (!test_and_set_bit(idx, cpuc->used_mask))
  940. return idx;
  941. }
  942. /* The counters are all in use. */
  943. return -EAGAIN;
  944. }
  945. /*
  946. * Add an event filter to a given event. This will only work for PMUv2 PMUs.
  947. */
  948. static int armv8pmu_set_event_filter(struct hw_perf_event *event,
  949. struct perf_event_attr *attr)
  950. {
  951. unsigned long config_base = 0;
  952. if (attr->exclude_idle)
  953. return -EPERM;
  954. if (attr->exclude_user)
  955. config_base |= ARMV8_EXCLUDE_EL0;
  956. if (attr->exclude_kernel)
  957. config_base |= ARMV8_EXCLUDE_EL1;
  958. if (!attr->exclude_hv)
  959. config_base |= ARMV8_INCLUDE_EL2;
  960. /*
  961. * Install the filter into config_base as this is used to
  962. * construct the event type.
  963. */
  964. event->config_base = config_base;
  965. return 0;
  966. }
  967. static void armv8pmu_reset(void *info)
  968. {
  969. u32 idx, nb_cnt = cpu_pmu->num_events;
  970. /* The counter and interrupt enable registers are unknown at reset. */
  971. for (idx = ARMV8_IDX_CYCLE_COUNTER; idx < nb_cnt; ++idx)
  972. armv8pmu_disable_event(NULL, idx);
  973. /* Initialize & Reset PMNC: C and P bits. */
  974. armv8pmu_pmcr_write(ARMV8_PMCR_P | ARMV8_PMCR_C);
  975. /* Disable access from userspace. */
  976. asm volatile("msr pmuserenr_el0, %0" :: "r" (0));
  977. }
  978. static int armv8_pmuv3_map_event(struct perf_event *event)
  979. {
  980. return map_cpu_event(event, &armv8_pmuv3_perf_map,
  981. &armv8_pmuv3_perf_cache_map, 0xFF);
  982. }
  983. static struct arm_pmu armv8pmu = {
  984. .handle_irq = armv8pmu_handle_irq,
  985. .enable = armv8pmu_enable_event,
  986. .disable = armv8pmu_disable_event,
  987. .read_counter = armv8pmu_read_counter,
  988. .write_counter = armv8pmu_write_counter,
  989. .get_event_idx = armv8pmu_get_event_idx,
  990. .start = armv8pmu_start,
  991. .stop = armv8pmu_stop,
  992. .reset = armv8pmu_reset,
  993. .max_period = (1LLU << 32) - 1,
  994. };
  995. static u32 __init armv8pmu_read_num_pmnc_events(void)
  996. {
  997. u32 nb_cnt;
  998. /* Read the nb of CNTx counters supported from PMNC */
  999. nb_cnt = (armv8pmu_pmcr_read() >> ARMV8_PMCR_N_SHIFT) & ARMV8_PMCR_N_MASK;
  1000. /* Add the CPU cycles counter and return */
  1001. return nb_cnt + 1;
  1002. }
  1003. static struct arm_pmu *__init armv8_pmuv3_pmu_init(void)
  1004. {
  1005. armv8pmu.name = "arm/armv8-pmuv3";
  1006. armv8pmu.map_event = armv8_pmuv3_map_event;
  1007. armv8pmu.num_events = armv8pmu_read_num_pmnc_events();
  1008. armv8pmu.set_event_filter = armv8pmu_set_event_filter;
  1009. return &armv8pmu;
  1010. }
  1011. /*
  1012. * Ensure the PMU has sane values out of reset.
  1013. * This requires SMP to be available, so exists as a separate initcall.
  1014. */
  1015. static int __init
  1016. cpu_pmu_reset(void)
  1017. {
  1018. if (cpu_pmu && cpu_pmu->reset)
  1019. return on_each_cpu(cpu_pmu->reset, NULL, 1);
  1020. return 0;
  1021. }
  1022. arch_initcall(cpu_pmu_reset);
  1023. /*
  1024. * PMU platform driver and devicetree bindings.
  1025. */
  1026. static struct of_device_id armpmu_of_device_ids[] = {
  1027. {.compatible = "arm,armv8-pmuv3"},
  1028. {},
  1029. };
  1030. static int armpmu_device_probe(struct platform_device *pdev)
  1031. {
  1032. if (!cpu_pmu)
  1033. return -ENODEV;
  1034. cpu_pmu->plat_device = pdev;
  1035. return 0;
  1036. }
  1037. static struct platform_driver armpmu_driver = {
  1038. .driver = {
  1039. .name = "arm-pmu",
  1040. .of_match_table = armpmu_of_device_ids,
  1041. },
  1042. .probe = armpmu_device_probe,
  1043. };
  1044. static int __init register_pmu_driver(void)
  1045. {
  1046. return platform_driver_register(&armpmu_driver);
  1047. }
  1048. device_initcall(register_pmu_driver);
  1049. static struct pmu_hw_events *armpmu_get_cpu_events(void)
  1050. {
  1051. return &__get_cpu_var(cpu_hw_events);
  1052. }
  1053. static void __init cpu_pmu_init(struct arm_pmu *armpmu)
  1054. {
  1055. int cpu;
  1056. for_each_possible_cpu(cpu) {
  1057. struct pmu_hw_events *events = &per_cpu(cpu_hw_events, cpu);
  1058. events->events = per_cpu(hw_events, cpu);
  1059. events->used_mask = per_cpu(used_mask, cpu);
  1060. raw_spin_lock_init(&events->pmu_lock);
  1061. }
  1062. armpmu->get_hw_events = armpmu_get_cpu_events;
  1063. }
  1064. static int __init init_hw_perf_events(void)
  1065. {
  1066. u64 dfr = read_cpuid(ID_AA64DFR0_EL1);
  1067. switch ((dfr >> 8) & 0xf) {
  1068. case 0x1: /* PMUv3 */
  1069. cpu_pmu = armv8_pmuv3_pmu_init();
  1070. break;
  1071. }
  1072. if (cpu_pmu) {
  1073. pr_info("enabled with %s PMU driver, %d counters available\n",
  1074. cpu_pmu->name, cpu_pmu->num_events);
  1075. cpu_pmu_init(cpu_pmu);
  1076. armpmu_register(cpu_pmu, "cpu", PERF_TYPE_RAW);
  1077. } else {
  1078. pr_info("no hardware support available\n");
  1079. }
  1080. return 0;
  1081. }
  1082. early_initcall(init_hw_perf_events);
  1083. /*
  1084. * Callchain handling code.
  1085. */
  1086. struct frame_tail {
  1087. struct frame_tail __user *fp;
  1088. unsigned long lr;
  1089. } __attribute__((packed));
  1090. /*
  1091. * Get the return address for a single stackframe and return a pointer to the
  1092. * next frame tail.
  1093. */
  1094. static struct frame_tail __user *
  1095. user_backtrace(struct frame_tail __user *tail,
  1096. struct perf_callchain_entry *entry)
  1097. {
  1098. struct frame_tail buftail;
  1099. unsigned long err;
  1100. /* Also check accessibility of one struct frame_tail beyond */
  1101. if (!access_ok(VERIFY_READ, tail, sizeof(buftail)))
  1102. return NULL;
  1103. pagefault_disable();
  1104. err = __copy_from_user_inatomic(&buftail, tail, sizeof(buftail));
  1105. pagefault_enable();
  1106. if (err)
  1107. return NULL;
  1108. perf_callchain_store(entry, buftail.lr);
  1109. /*
  1110. * Frame pointers should strictly progress back up the stack
  1111. * (towards higher addresses).
  1112. */
  1113. if (tail >= buftail.fp)
  1114. return NULL;
  1115. return buftail.fp;
  1116. }
  1117. void perf_callchain_user(struct perf_callchain_entry *entry,
  1118. struct pt_regs *regs)
  1119. {
  1120. struct frame_tail __user *tail;
  1121. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1122. /* We don't support guest os callchain now */
  1123. return;
  1124. }
  1125. perf_callchain_store(entry, regs->pc);
  1126. tail = (struct frame_tail __user *)regs->regs[29];
  1127. while (entry->nr < PERF_MAX_STACK_DEPTH &&
  1128. tail && !((unsigned long)tail & 0xf))
  1129. tail = user_backtrace(tail, entry);
  1130. }
  1131. /*
  1132. * Gets called by walk_stackframe() for every stackframe. This will be called
  1133. * whist unwinding the stackframe and is like a subroutine return so we use
  1134. * the PC.
  1135. */
  1136. static int callchain_trace(struct stackframe *frame, void *data)
  1137. {
  1138. struct perf_callchain_entry *entry = data;
  1139. perf_callchain_store(entry, frame->pc);
  1140. return 0;
  1141. }
  1142. void perf_callchain_kernel(struct perf_callchain_entry *entry,
  1143. struct pt_regs *regs)
  1144. {
  1145. struct stackframe frame;
  1146. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1147. /* We don't support guest os callchain now */
  1148. return;
  1149. }
  1150. frame.fp = regs->regs[29];
  1151. frame.sp = regs->sp;
  1152. frame.pc = regs->pc;
  1153. walk_stackframe(&frame, callchain_trace, entry);
  1154. }
  1155. unsigned long perf_instruction_pointer(struct pt_regs *regs)
  1156. {
  1157. if (perf_guest_cbs && perf_guest_cbs->is_in_guest())
  1158. return perf_guest_cbs->get_guest_ip();
  1159. return instruction_pointer(regs);
  1160. }
  1161. unsigned long perf_misc_flags(struct pt_regs *regs)
  1162. {
  1163. int misc = 0;
  1164. if (perf_guest_cbs && perf_guest_cbs->is_in_guest()) {
  1165. if (perf_guest_cbs->is_user_mode())
  1166. misc |= PERF_RECORD_MISC_GUEST_USER;
  1167. else
  1168. misc |= PERF_RECORD_MISC_GUEST_KERNEL;
  1169. } else {
  1170. if (user_mode(regs))
  1171. misc |= PERF_RECORD_MISC_USER;
  1172. else
  1173. misc |= PERF_RECORD_MISC_KERNEL;
  1174. }
  1175. return misc;
  1176. }